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, 2015 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>
118 #include <stdio_ext.h>
125 #include <sys/fs/zfs.h>
126 #include <zfs_fletcher.h>
127 #include <libnvpair.h>
129 #include <execinfo.h> /* for backtrace() */
132 static int ztest_fd_data = -1;
133 static int ztest_fd_rand = -1;
135 typedef struct ztest_shared_hdr {
136 uint64_t zh_hdr_size;
137 uint64_t zh_opts_size;
139 uint64_t zh_stats_size;
140 uint64_t zh_stats_count;
142 uint64_t zh_ds_count;
143 } ztest_shared_hdr_t;
145 static ztest_shared_hdr_t *ztest_shared_hdr;
147 typedef struct ztest_shared_opts {
148 char zo_pool[ZFS_MAX_DATASET_NAME_LEN];
149 char zo_dir[ZFS_MAX_DATASET_NAME_LEN];
150 char zo_alt_ztest[MAXNAMELEN];
151 char zo_alt_libpath[MAXNAMELEN];
153 uint64_t zo_vdevtime;
161 uint64_t zo_passtime;
162 uint64_t zo_killrate;
166 uint64_t zo_maxloops;
167 uint64_t zo_metaslab_gang_bang;
168 } ztest_shared_opts_t;
170 static const ztest_shared_opts_t ztest_opts_defaults = {
171 .zo_pool = { 'z', 't', 'e', 's', 't', '\0' },
172 .zo_dir = { '/', 't', 'm', 'p', '\0' },
173 .zo_alt_ztest = { '\0' },
174 .zo_alt_libpath = { '\0' },
176 .zo_ashift = SPA_MINBLOCKSHIFT,
179 .zo_raidz_parity = 1,
180 .zo_vdev_size = SPA_MINDEVSIZE * 2,
183 .zo_passtime = 60, /* 60 seconds */
184 .zo_killrate = 70, /* 70% kill rate */
187 .zo_time = 300, /* 5 minutes */
188 .zo_maxloops = 50, /* max loops during spa_freeze() */
189 .zo_metaslab_gang_bang = 32 << 10
192 extern uint64_t metaslab_gang_bang;
193 extern uint64_t metaslab_df_alloc_threshold;
194 extern int metaslab_preload_limit;
195 extern boolean_t zfs_compressed_arc_enabled;
197 static ztest_shared_opts_t *ztest_shared_opts;
198 static ztest_shared_opts_t ztest_opts;
200 typedef struct ztest_shared_ds {
204 static ztest_shared_ds_t *ztest_shared_ds;
205 #define ZTEST_GET_SHARED_DS(d) (&ztest_shared_ds[d])
207 #define BT_MAGIC 0x123456789abcdefULL
208 #define MAXFAULTS() \
209 (MAX(zs->zs_mirrors, 1) * (ztest_opts.zo_raidz_parity + 1) - 1)
213 ZTEST_IO_WRITE_PATTERN,
214 ZTEST_IO_WRITE_ZEROES,
221 typedef struct ztest_block_tag {
225 uint64_t bt_dnodesize;
232 typedef struct bufwad {
250 #define ZTEST_RANGE_LOCKS 64
251 #define ZTEST_OBJECT_LOCKS 64
254 * Object descriptor. Used as a template for object lookup/create/remove.
256 typedef struct ztest_od {
259 dmu_object_type_t od_type;
260 dmu_object_type_t od_crtype;
261 uint64_t od_blocksize;
262 uint64_t od_crblocksize;
263 uint64_t od_crdnodesize;
266 char od_name[ZFS_MAX_DATASET_NAME_LEN];
272 typedef struct ztest_ds {
273 ztest_shared_ds_t *zd_shared;
275 rwlock_t zd_zilog_lock;
277 ztest_od_t *zd_od; /* debugging aid */
278 char zd_name[ZFS_MAX_DATASET_NAME_LEN];
279 kmutex_t zd_dirobj_lock;
280 rll_t zd_object_lock[ZTEST_OBJECT_LOCKS];
281 zll_t zd_range_lock[ZTEST_RANGE_LOCKS];
285 * Per-iteration state.
287 typedef void ztest_func_t(ztest_ds_t *zd, uint64_t id);
289 typedef struct ztest_info {
290 ztest_func_t *zi_func; /* test function */
291 uint64_t zi_iters; /* iterations per execution */
292 uint64_t *zi_interval; /* execute every <interval> seconds */
293 const char *zi_funcname; /* name of test function */
296 typedef struct ztest_shared_callstate {
297 uint64_t zc_count; /* per-pass count */
298 uint64_t zc_time; /* per-pass time */
299 uint64_t zc_next; /* next time to call this function */
300 } ztest_shared_callstate_t;
302 static ztest_shared_callstate_t *ztest_shared_callstate;
303 #define ZTEST_GET_SHARED_CALLSTATE(c) (&ztest_shared_callstate[c])
305 ztest_func_t ztest_dmu_read_write;
306 ztest_func_t ztest_dmu_write_parallel;
307 ztest_func_t ztest_dmu_object_alloc_free;
308 ztest_func_t ztest_dmu_commit_callbacks;
309 ztest_func_t ztest_zap;
310 ztest_func_t ztest_zap_parallel;
311 ztest_func_t ztest_zil_commit;
312 ztest_func_t ztest_zil_remount;
313 ztest_func_t ztest_dmu_read_write_zcopy;
314 ztest_func_t ztest_dmu_objset_create_destroy;
315 ztest_func_t ztest_dmu_prealloc;
316 ztest_func_t ztest_fzap;
317 ztest_func_t ztest_dmu_snapshot_create_destroy;
318 ztest_func_t ztest_dsl_prop_get_set;
319 ztest_func_t ztest_spa_prop_get_set;
320 ztest_func_t ztest_spa_create_destroy;
321 ztest_func_t ztest_fault_inject;
322 ztest_func_t ztest_ddt_repair;
323 ztest_func_t ztest_dmu_snapshot_hold;
324 ztest_func_t ztest_spa_rename;
325 ztest_func_t ztest_scrub;
326 ztest_func_t ztest_dsl_dataset_promote_busy;
327 ztest_func_t ztest_vdev_attach_detach;
328 ztest_func_t ztest_vdev_LUN_growth;
329 ztest_func_t ztest_vdev_add_remove;
330 ztest_func_t ztest_vdev_aux_add_remove;
331 ztest_func_t ztest_split_pool;
332 ztest_func_t ztest_reguid;
333 ztest_func_t ztest_spa_upgrade;
334 ztest_func_t ztest_fletcher;
335 ztest_func_t ztest_fletcher_incr;
336 ztest_func_t ztest_verify_dnode_bt;
338 uint64_t zopt_always = 0ULL * NANOSEC; /* all the time */
339 uint64_t zopt_incessant = 1ULL * NANOSEC / 10; /* every 1/10 second */
340 uint64_t zopt_often = 1ULL * NANOSEC; /* every second */
341 uint64_t zopt_sometimes = 10ULL * NANOSEC; /* every 10 seconds */
342 uint64_t zopt_rarely = 60ULL * NANOSEC; /* every 60 seconds */
344 #define ZTI_INIT(func, iters, interval) \
345 { .zi_func = (func), \
346 .zi_iters = (iters), \
347 .zi_interval = (interval), \
348 .zi_funcname = # func }
350 ztest_info_t ztest_info[] = {
351 ZTI_INIT(ztest_dmu_read_write, 1, &zopt_always),
352 ZTI_INIT(ztest_dmu_write_parallel, 10, &zopt_always),
353 ZTI_INIT(ztest_dmu_object_alloc_free, 1, &zopt_always),
354 ZTI_INIT(ztest_dmu_commit_callbacks, 1, &zopt_always),
355 ZTI_INIT(ztest_zap, 30, &zopt_always),
356 ZTI_INIT(ztest_zap_parallel, 100, &zopt_always),
357 ZTI_INIT(ztest_split_pool, 1, &zopt_always),
358 ZTI_INIT(ztest_zil_commit, 1, &zopt_incessant),
359 ZTI_INIT(ztest_zil_remount, 1, &zopt_sometimes),
360 ZTI_INIT(ztest_dmu_read_write_zcopy, 1, &zopt_often),
361 ZTI_INIT(ztest_dmu_objset_create_destroy, 1, &zopt_often),
362 ZTI_INIT(ztest_dsl_prop_get_set, 1, &zopt_often),
363 ZTI_INIT(ztest_spa_prop_get_set, 1, &zopt_sometimes),
365 ZTI_INIT(ztest_dmu_prealloc, 1, &zopt_sometimes),
367 ZTI_INIT(ztest_fzap, 1, &zopt_sometimes),
368 ZTI_INIT(ztest_dmu_snapshot_create_destroy, 1, &zopt_sometimes),
369 ZTI_INIT(ztest_spa_create_destroy, 1, &zopt_sometimes),
370 ZTI_INIT(ztest_fault_inject, 1, &zopt_sometimes),
371 ZTI_INIT(ztest_ddt_repair, 1, &zopt_sometimes),
372 ZTI_INIT(ztest_dmu_snapshot_hold, 1, &zopt_sometimes),
373 ZTI_INIT(ztest_reguid, 1, &zopt_rarely),
374 ZTI_INIT(ztest_spa_rename, 1, &zopt_rarely),
375 ZTI_INIT(ztest_scrub, 1, &zopt_rarely),
376 ZTI_INIT(ztest_spa_upgrade, 1, &zopt_rarely),
377 ZTI_INIT(ztest_dsl_dataset_promote_busy, 1, &zopt_rarely),
378 ZTI_INIT(ztest_vdev_attach_detach, 1, &zopt_sometimes),
379 ZTI_INIT(ztest_vdev_LUN_growth, 1, &zopt_rarely),
380 ZTI_INIT(ztest_vdev_add_remove, 1, &ztest_opts.zo_vdevtime),
381 ZTI_INIT(ztest_vdev_aux_add_remove, 1, &ztest_opts.zo_vdevtime),
382 ZTI_INIT(ztest_fletcher, 1, &zopt_rarely),
383 ZTI_INIT(ztest_fletcher_incr, 1, &zopt_rarely),
384 ZTI_INIT(ztest_verify_dnode_bt, 1, &zopt_sometimes),
387 #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
390 * The following struct is used to hold a list of uncalled commit callbacks.
391 * The callbacks are ordered by txg number.
393 typedef struct ztest_cb_list {
394 kmutex_t zcl_callbacks_lock;
395 list_t zcl_callbacks;
399 * Stuff we need to share writably between parent and child.
401 typedef struct ztest_shared {
402 boolean_t zs_do_init;
403 hrtime_t zs_proc_start;
404 hrtime_t zs_proc_stop;
405 hrtime_t zs_thread_start;
406 hrtime_t zs_thread_stop;
407 hrtime_t zs_thread_kill;
408 uint64_t zs_enospc_count;
409 uint64_t zs_vdev_next_leaf;
410 uint64_t zs_vdev_aux;
415 uint64_t zs_metaslab_sz;
416 uint64_t zs_metaslab_df_alloc_threshold;
420 #define ID_PARALLEL -1ULL
422 static char ztest_dev_template[] = "%s/%s.%llua";
423 static char ztest_aux_template[] = "%s/%s.%s.%llu";
424 ztest_shared_t *ztest_shared;
426 static spa_t *ztest_spa = NULL;
427 static ztest_ds_t *ztest_ds;
429 static kmutex_t ztest_vdev_lock;
432 * The ztest_name_lock protects the pool and dataset namespace used by
433 * the individual tests. To modify the namespace, consumers must grab
434 * this lock as writer. Grabbing the lock as reader will ensure that the
435 * namespace does not change while the lock is held.
437 static rwlock_t ztest_name_lock;
439 static boolean_t ztest_dump_core = B_TRUE;
440 static boolean_t ztest_exiting;
442 /* Global commit callback list */
443 static ztest_cb_list_t zcl;
444 /* Commit cb delay */
445 static uint64_t zc_min_txg_delay = UINT64_MAX;
446 static int zc_cb_counter = 0;
449 * Minimum number of commit callbacks that need to be registered for us to check
450 * whether the minimum txg delay is acceptable.
452 #define ZTEST_COMMIT_CB_MIN_REG 100
455 * If a number of txgs equal to this threshold have been created after a commit
456 * callback has been registered but not called, then we assume there is an
457 * implementation bug.
459 #define ZTEST_COMMIT_CB_THRESH (TXG_CONCURRENT_STATES + 1000)
461 extern uint64_t metaslab_gang_bang;
462 extern uint64_t metaslab_df_alloc_threshold;
465 ZTEST_META_DNODE = 0,
470 static void usage(boolean_t) __NORETURN;
473 * These libumem hooks provide a reasonable set of defaults for the allocator's
474 * debugging facilities.
477 _umem_debug_init(void)
479 return ("default,verbose"); /* $UMEM_DEBUG setting */
483 _umem_logging_init(void)
485 return ("fail,contents"); /* $UMEM_LOGGING setting */
488 #define BACKTRACE_SZ 100
490 static void sig_handler(int signo)
492 struct sigaction action;
493 #ifdef __GLIBC__ /* backtrace() is a GNU extension */
495 void *buffer[BACKTRACE_SZ];
497 nptrs = backtrace(buffer, BACKTRACE_SZ);
498 backtrace_symbols_fd(buffer, nptrs, STDERR_FILENO);
502 * Restore default action and re-raise signal so SIGSEGV and
503 * SIGABRT can trigger a core dump.
505 action.sa_handler = SIG_DFL;
506 sigemptyset(&action.sa_mask);
508 (void) sigaction(signo, &action, NULL);
512 #define FATAL_MSG_SZ 1024
517 fatal(int do_perror, char *message, ...)
520 int save_errno = errno;
523 (void) fflush(stdout);
524 buf = umem_alloc(FATAL_MSG_SZ, UMEM_NOFAIL);
526 va_start(args, message);
527 (void) sprintf(buf, "ztest: ");
529 (void) vsprintf(buf + strlen(buf), message, args);
532 (void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
533 ": %s", strerror(save_errno));
535 (void) fprintf(stderr, "%s\n", buf);
536 fatal_msg = buf; /* to ease debugging */
543 str2shift(const char *buf)
545 const char *ends = "BKMGTPEZ";
550 for (i = 0; i < strlen(ends); i++) {
551 if (toupper(buf[0]) == ends[i])
554 if (i == strlen(ends)) {
555 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
559 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
562 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
568 nicenumtoull(const char *buf)
573 val = strtoull(buf, &end, 0);
575 (void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
577 } else if (end[0] == '.') {
578 double fval = strtod(buf, &end);
579 fval *= pow(2, str2shift(end));
580 if (fval > UINT64_MAX) {
581 (void) fprintf(stderr, "ztest: value too large: %s\n",
585 val = (uint64_t)fval;
587 int shift = str2shift(end);
588 if (shift >= 64 || (val << shift) >> shift != val) {
589 (void) fprintf(stderr, "ztest: value too large: %s\n",
599 usage(boolean_t requested)
601 const ztest_shared_opts_t *zo = &ztest_opts_defaults;
603 char nice_vdev_size[10];
604 char nice_gang_bang[10];
605 FILE *fp = requested ? stdout : stderr;
607 nicenum(zo->zo_vdev_size, nice_vdev_size);
608 nicenum(zo->zo_metaslab_gang_bang, nice_gang_bang);
610 (void) fprintf(fp, "Usage: %s\n"
611 "\t[-v vdevs (default: %llu)]\n"
612 "\t[-s size_of_each_vdev (default: %s)]\n"
613 "\t[-a alignment_shift (default: %d)] use 0 for random\n"
614 "\t[-m mirror_copies (default: %d)]\n"
615 "\t[-r raidz_disks (default: %d)]\n"
616 "\t[-R raidz_parity (default: %d)]\n"
617 "\t[-d datasets (default: %d)]\n"
618 "\t[-t threads (default: %d)]\n"
619 "\t[-g gang_block_threshold (default: %s)]\n"
620 "\t[-i init_count (default: %d)] initialize pool i times\n"
621 "\t[-k kill_percentage (default: %llu%%)]\n"
622 "\t[-p pool_name (default: %s)]\n"
623 "\t[-f dir (default: %s)] file directory for vdev files\n"
624 "\t[-V] verbose (use multiple times for ever more blather)\n"
625 "\t[-E] use existing pool instead of creating new one\n"
626 "\t[-T time (default: %llu sec)] total run time\n"
627 "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
628 "\t[-P passtime (default: %llu sec)] time per pass\n"
629 "\t[-B alt_ztest (default: <none>)] alternate ztest path\n"
630 "\t[-h] (print help)\n"
633 (u_longlong_t)zo->zo_vdevs, /* -v */
634 nice_vdev_size, /* -s */
635 zo->zo_ashift, /* -a */
636 zo->zo_mirrors, /* -m */
637 zo->zo_raidz, /* -r */
638 zo->zo_raidz_parity, /* -R */
639 zo->zo_datasets, /* -d */
640 zo->zo_threads, /* -t */
641 nice_gang_bang, /* -g */
642 zo->zo_init, /* -i */
643 (u_longlong_t)zo->zo_killrate, /* -k */
644 zo->zo_pool, /* -p */
646 (u_longlong_t)zo->zo_time, /* -T */
647 (u_longlong_t)zo->zo_maxloops, /* -F */
648 (u_longlong_t)zo->zo_passtime);
649 exit(requested ? 0 : 1);
653 process_options(int argc, char **argv)
656 ztest_shared_opts_t *zo = &ztest_opts;
660 char altdir[MAXNAMELEN] = { 0 };
662 bcopy(&ztest_opts_defaults, zo, sizeof (*zo));
664 while ((opt = getopt(argc, argv,
665 "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:hF:B:")) != EOF) {
682 value = nicenumtoull(optarg);
686 zo->zo_vdevs = value;
689 zo->zo_vdev_size = MAX(SPA_MINDEVSIZE, value);
692 zo->zo_ashift = value;
695 zo->zo_mirrors = value;
698 zo->zo_raidz = MAX(1, value);
701 zo->zo_raidz_parity = MIN(MAX(value, 1), 3);
704 zo->zo_datasets = MAX(1, value);
707 zo->zo_threads = MAX(1, value);
710 zo->zo_metaslab_gang_bang = MAX(SPA_MINBLOCKSIZE << 1,
717 zo->zo_killrate = value;
720 (void) strlcpy(zo->zo_pool, optarg,
721 sizeof (zo->zo_pool));
724 path = realpath(optarg, NULL);
726 (void) fprintf(stderr, "error: %s: %s\n",
727 optarg, strerror(errno));
730 (void) strlcpy(zo->zo_dir, path,
731 sizeof (zo->zo_dir));
745 zo->zo_passtime = MAX(1, value);
748 zo->zo_maxloops = MAX(1, value);
751 (void) strlcpy(altdir, optarg, sizeof (altdir));
763 zo->zo_raidz_parity = MIN(zo->zo_raidz_parity, zo->zo_raidz - 1);
766 (zo->zo_vdevs > 0 ? zo->zo_time * NANOSEC / zo->zo_vdevs :
769 if (strlen(altdir) > 0) {
777 cmd = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
778 realaltdir = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
780 VERIFY(NULL != realpath(getexecname(), cmd));
781 if (0 != access(altdir, F_OK)) {
782 ztest_dump_core = B_FALSE;
783 fatal(B_TRUE, "invalid alternate ztest path: %s",
786 VERIFY(NULL != realpath(altdir, realaltdir));
789 * 'cmd' should be of the form "<anything>/usr/bin/<isa>/ztest".
790 * We want to extract <isa> to determine if we should use
791 * 32 or 64 bit binaries.
793 bin = strstr(cmd, "/usr/bin/");
794 ztest = strstr(bin, "/ztest");
796 isalen = ztest - isa;
797 (void) snprintf(zo->zo_alt_ztest, sizeof (zo->zo_alt_ztest),
798 "%s/usr/bin/%.*s/ztest", realaltdir, isalen, isa);
799 (void) snprintf(zo->zo_alt_libpath, sizeof (zo->zo_alt_libpath),
800 "%s/usr/lib/%.*s", realaltdir, isalen, isa);
802 if (0 != access(zo->zo_alt_ztest, X_OK)) {
803 ztest_dump_core = B_FALSE;
804 fatal(B_TRUE, "invalid alternate ztest: %s",
806 } else if (0 != access(zo->zo_alt_libpath, X_OK)) {
807 ztest_dump_core = B_FALSE;
808 fatal(B_TRUE, "invalid alternate lib directory %s",
812 umem_free(cmd, MAXPATHLEN);
813 umem_free(realaltdir, MAXPATHLEN);
818 ztest_kill(ztest_shared_t *zs)
820 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(ztest_spa));
821 zs->zs_space = metaslab_class_get_space(spa_normal_class(ztest_spa));
824 * Before we kill off ztest, make sure that the config is updated.
825 * See comment above spa_config_sync().
827 mutex_enter(&spa_namespace_lock);
828 spa_config_sync(ztest_spa, B_FALSE, B_FALSE);
829 mutex_exit(&spa_namespace_lock);
831 (void) kill(getpid(), SIGKILL);
835 ztest_random(uint64_t range)
839 ASSERT3S(ztest_fd_rand, >=, 0);
844 if (read(ztest_fd_rand, &r, sizeof (r)) != sizeof (r))
845 fatal(1, "short read from /dev/urandom");
852 ztest_record_enospc(const char *s)
854 ztest_shared->zs_enospc_count++;
858 ztest_get_ashift(void)
860 if (ztest_opts.zo_ashift == 0)
861 return (SPA_MINBLOCKSHIFT + ztest_random(5));
862 return (ztest_opts.zo_ashift);
866 make_vdev_file(char *path, char *aux, char *pool, size_t size, uint64_t ashift)
872 pathbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
875 ashift = ztest_get_ashift();
881 vdev = ztest_shared->zs_vdev_aux;
882 (void) snprintf(path, MAXPATHLEN,
883 ztest_aux_template, ztest_opts.zo_dir,
884 pool == NULL ? ztest_opts.zo_pool : pool,
887 vdev = ztest_shared->zs_vdev_next_leaf++;
888 (void) snprintf(path, MAXPATHLEN,
889 ztest_dev_template, ztest_opts.zo_dir,
890 pool == NULL ? ztest_opts.zo_pool : pool, vdev);
895 int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
897 fatal(1, "can't open %s", path);
898 if (ftruncate(fd, size) != 0)
899 fatal(1, "can't ftruncate %s", path);
903 VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
904 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
905 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
906 VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
907 umem_free(pathbuf, MAXPATHLEN);
913 make_vdev_raidz(char *path, char *aux, char *pool, size_t size,
914 uint64_t ashift, int r)
916 nvlist_t *raidz, **child;
920 return (make_vdev_file(path, aux, pool, size, ashift));
921 child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
923 for (c = 0; c < r; c++)
924 child[c] = make_vdev_file(path, aux, pool, size, ashift);
926 VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
927 VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
928 VDEV_TYPE_RAIDZ) == 0);
929 VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
930 ztest_opts.zo_raidz_parity) == 0);
931 VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
934 for (c = 0; c < r; c++)
935 nvlist_free(child[c]);
937 umem_free(child, r * sizeof (nvlist_t *));
943 make_vdev_mirror(char *path, char *aux, char *pool, size_t size,
944 uint64_t ashift, int r, int m)
946 nvlist_t *mirror, **child;
950 return (make_vdev_raidz(path, aux, pool, size, ashift, r));
952 child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
954 for (c = 0; c < m; c++)
955 child[c] = make_vdev_raidz(path, aux, pool, size, ashift, r);
957 VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
958 VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
959 VDEV_TYPE_MIRROR) == 0);
960 VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
963 for (c = 0; c < m; c++)
964 nvlist_free(child[c]);
966 umem_free(child, m * sizeof (nvlist_t *));
972 make_vdev_root(char *path, char *aux, char *pool, size_t size, uint64_t ashift,
973 int log, int r, int m, int t)
975 nvlist_t *root, **child;
980 child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
982 for (c = 0; c < t; c++) {
983 child[c] = make_vdev_mirror(path, aux, pool, size, ashift,
985 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
989 VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
990 VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
991 VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
994 for (c = 0; c < t; c++)
995 nvlist_free(child[c]);
997 umem_free(child, t * sizeof (nvlist_t *));
1003 * Find a random spa version. Returns back a random spa version in the
1004 * range [initial_version, SPA_VERSION_FEATURES].
1007 ztest_random_spa_version(uint64_t initial_version)
1009 uint64_t version = initial_version;
1011 if (version <= SPA_VERSION_BEFORE_FEATURES) {
1013 ztest_random(SPA_VERSION_BEFORE_FEATURES - version + 1);
1016 if (version > SPA_VERSION_BEFORE_FEATURES)
1017 version = SPA_VERSION_FEATURES;
1019 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
1024 ztest_random_blocksize(void)
1027 * Choose a block size >= the ashift.
1028 * If the SPA supports new MAXBLOCKSIZE, test up to 1MB blocks.
1030 int maxbs = SPA_OLD_MAXBLOCKSHIFT;
1031 if (spa_maxblocksize(ztest_spa) == SPA_MAXBLOCKSIZE)
1033 uint64_t block_shift =
1034 ztest_random(maxbs - ztest_spa->spa_max_ashift + 1);
1035 return (1 << (SPA_MINBLOCKSHIFT + block_shift));
1039 ztest_random_dnodesize(void)
1042 int max_slots = spa_maxdnodesize(ztest_spa) >> DNODE_SHIFT;
1044 if (max_slots == DNODE_MIN_SLOTS)
1045 return (DNODE_MIN_SIZE);
1048 * Weight the random distribution more heavily toward smaller
1049 * dnode sizes since that is more likely to reflect real-world
1052 ASSERT3U(max_slots, >, 4);
1053 switch (ztest_random(10)) {
1055 slots = 5 + ztest_random(max_slots - 4);
1058 slots = 2 + ztest_random(3);
1065 return (slots << DNODE_SHIFT);
1069 ztest_random_ibshift(void)
1071 return (DN_MIN_INDBLKSHIFT +
1072 ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1));
1076 ztest_random_vdev_top(spa_t *spa, boolean_t log_ok)
1079 vdev_t *rvd = spa->spa_root_vdev;
1082 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1085 top = ztest_random(rvd->vdev_children);
1086 tvd = rvd->vdev_child[top];
1087 } while (tvd->vdev_ishole || (tvd->vdev_islog && !log_ok) ||
1088 tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL);
1094 ztest_random_dsl_prop(zfs_prop_t prop)
1099 value = zfs_prop_random_value(prop, ztest_random(-1ULL));
1100 } while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF);
1106 ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value,
1109 const char *propname = zfs_prop_to_name(prop);
1110 const char *valname;
1115 error = dsl_prop_set_int(osname, propname,
1116 (inherit ? ZPROP_SRC_NONE : ZPROP_SRC_LOCAL), value);
1118 if (error == ENOSPC) {
1119 ztest_record_enospc(FTAG);
1124 setpoint = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
1125 VERIFY0(dsl_prop_get_integer(osname, propname, &curval, setpoint));
1127 if (ztest_opts.zo_verbose >= 6) {
1130 err = zfs_prop_index_to_string(prop, curval, &valname);
1132 (void) printf("%s %s = %llu at '%s'\n",
1133 osname, propname, (unsigned long long)curval,
1136 (void) printf("%s %s = %s at '%s'\n",
1137 osname, propname, valname, setpoint);
1139 umem_free(setpoint, MAXPATHLEN);
1145 ztest_spa_prop_set_uint64(zpool_prop_t prop, uint64_t value)
1147 spa_t *spa = ztest_spa;
1148 nvlist_t *props = NULL;
1151 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
1152 VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0);
1154 error = spa_prop_set(spa, props);
1158 if (error == ENOSPC) {
1159 ztest_record_enospc(FTAG);
1169 * Object and range lock mechanics
1172 list_node_t z_lnode;
1173 refcount_t z_refcnt;
1175 zfs_rlock_t z_range_lock;
1180 ztest_znode_t *z_ztznode;
1183 static ztest_znode_t *
1184 ztest_znode_init(uint64_t object)
1186 ztest_znode_t *zp = umem_alloc(sizeof (*zp), UMEM_NOFAIL);
1188 list_link_init(&zp->z_lnode);
1189 refcount_create(&zp->z_refcnt);
1190 zp->z_object = object;
1191 zfs_rlock_init(&zp->z_range_lock);
1197 ztest_znode_fini(ztest_znode_t *zp)
1199 ASSERT(refcount_is_zero(&zp->z_refcnt));
1200 zfs_rlock_destroy(&zp->z_range_lock);
1202 refcount_destroy(&zp->z_refcnt);
1203 list_link_init(&zp->z_lnode);
1204 umem_free(zp, sizeof (*zp));
1208 ztest_zll_init(zll_t *zll)
1210 mutex_init(&zll->z_lock, NULL, MUTEX_DEFAULT, NULL);
1211 list_create(&zll->z_list, sizeof (ztest_znode_t),
1212 offsetof(ztest_znode_t, z_lnode));
1216 ztest_zll_destroy(zll_t *zll)
1218 list_destroy(&zll->z_list);
1219 mutex_destroy(&zll->z_lock);
1222 #define RL_TAG "range_lock"
1223 static ztest_znode_t *
1224 ztest_znode_get(ztest_ds_t *zd, uint64_t object)
1226 zll_t *zll = &zd->zd_range_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1227 ztest_znode_t *zp = NULL;
1228 mutex_enter(&zll->z_lock);
1229 for (zp = list_head(&zll->z_list); (zp);
1230 zp = list_next(&zll->z_list, zp)) {
1231 if (zp->z_object == object) {
1232 refcount_add(&zp->z_refcnt, RL_TAG);
1237 zp = ztest_znode_init(object);
1238 refcount_add(&zp->z_refcnt, RL_TAG);
1239 list_insert_head(&zll->z_list, zp);
1241 mutex_exit(&zll->z_lock);
1246 ztest_znode_put(ztest_ds_t *zd, ztest_znode_t *zp)
1249 ASSERT3U(zp->z_object, !=, 0);
1250 zll = &zd->zd_range_lock[zp->z_object & (ZTEST_OBJECT_LOCKS - 1)];
1251 mutex_enter(&zll->z_lock);
1252 refcount_remove(&zp->z_refcnt, RL_TAG);
1253 if (refcount_is_zero(&zp->z_refcnt)) {
1254 list_remove(&zll->z_list, zp);
1255 ztest_znode_fini(zp);
1257 mutex_exit(&zll->z_lock);
1262 ztest_rll_init(rll_t *rll)
1264 rll->rll_writer = NULL;
1265 rll->rll_readers = 0;
1266 mutex_init(&rll->rll_lock, NULL, MUTEX_DEFAULT, NULL);
1267 cv_init(&rll->rll_cv, NULL, CV_DEFAULT, NULL);
1271 ztest_rll_destroy(rll_t *rll)
1273 ASSERT(rll->rll_writer == NULL);
1274 ASSERT(rll->rll_readers == 0);
1275 mutex_destroy(&rll->rll_lock);
1276 cv_destroy(&rll->rll_cv);
1280 ztest_rll_lock(rll_t *rll, rl_type_t type)
1282 mutex_enter(&rll->rll_lock);
1284 if (type == RL_READER) {
1285 while (rll->rll_writer != NULL)
1286 (void) cv_wait(&rll->rll_cv, &rll->rll_lock);
1289 while (rll->rll_writer != NULL || rll->rll_readers)
1290 (void) cv_wait(&rll->rll_cv, &rll->rll_lock);
1291 rll->rll_writer = curthread;
1294 mutex_exit(&rll->rll_lock);
1298 ztest_rll_unlock(rll_t *rll)
1300 mutex_enter(&rll->rll_lock);
1302 if (rll->rll_writer) {
1303 ASSERT(rll->rll_readers == 0);
1304 rll->rll_writer = NULL;
1306 ASSERT(rll->rll_readers != 0);
1307 ASSERT(rll->rll_writer == NULL);
1311 if (rll->rll_writer == NULL && rll->rll_readers == 0)
1312 cv_broadcast(&rll->rll_cv);
1314 mutex_exit(&rll->rll_lock);
1318 ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type)
1320 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1322 ztest_rll_lock(rll, type);
1326 ztest_object_unlock(ztest_ds_t *zd, uint64_t object)
1328 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1330 ztest_rll_unlock(rll);
1333 static ztest_zrl_t *
1334 ztest_zrl_init(rl_t *rl, ztest_znode_t *zp)
1336 ztest_zrl_t *zrl = umem_alloc(sizeof (*zrl), UMEM_NOFAIL);
1338 zrl->z_ztznode = zp;
1343 ztest_zrl_fini(ztest_zrl_t *zrl)
1345 umem_free(zrl, sizeof (*zrl));
1348 static ztest_zrl_t *
1349 ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset,
1350 uint64_t size, rl_type_t type)
1352 ztest_znode_t *zp = ztest_znode_get(zd, object);
1353 rl_t *rl = zfs_range_lock(&zp->z_range_lock, offset,
1355 return (ztest_zrl_init(rl, zp));
1359 ztest_range_unlock(ztest_ds_t *zd, ztest_zrl_t *zrl)
1361 zfs_range_unlock(zrl->z_rl);
1362 ztest_znode_put(zd, zrl->z_ztznode);
1363 ztest_zrl_fini(zrl);
1367 ztest_zd_init(ztest_ds_t *zd, ztest_shared_ds_t *szd, objset_t *os)
1370 zd->zd_zilog = dmu_objset_zil(os);
1371 zd->zd_shared = szd;
1372 dmu_objset_name(os, zd->zd_name);
1375 if (zd->zd_shared != NULL)
1376 zd->zd_shared->zd_seq = 0;
1378 VERIFY(rwlock_init(&zd->zd_zilog_lock, USYNC_THREAD, NULL) == 0);
1379 mutex_init(&zd->zd_dirobj_lock, NULL, MUTEX_DEFAULT, NULL);
1381 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1382 ztest_rll_init(&zd->zd_object_lock[l]);
1384 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
1385 ztest_zll_init(&zd->zd_range_lock[l]);
1389 ztest_zd_fini(ztest_ds_t *zd)
1393 mutex_destroy(&zd->zd_dirobj_lock);
1394 (void) rwlock_destroy(&zd->zd_zilog_lock);
1396 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1397 ztest_rll_destroy(&zd->zd_object_lock[l]);
1399 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
1400 ztest_zll_destroy(&zd->zd_range_lock[l]);
1403 #define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1406 ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag)
1412 * Attempt to assign tx to some transaction group.
1414 error = dmu_tx_assign(tx, txg_how);
1416 if (error == ERESTART) {
1417 ASSERT(txg_how == TXG_NOWAIT);
1420 ASSERT3U(error, ==, ENOSPC);
1421 ztest_record_enospc(tag);
1426 txg = dmu_tx_get_txg(tx);
1432 ztest_pattern_set(void *buf, uint64_t size, uint64_t value)
1435 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1443 ztest_pattern_match(void *buf, uint64_t size, uint64_t value)
1446 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1450 diff |= (value - *ip++);
1457 ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1458 uint64_t dnodesize, uint64_t offset, uint64_t gen, uint64_t txg,
1461 bt->bt_magic = BT_MAGIC;
1462 bt->bt_objset = dmu_objset_id(os);
1463 bt->bt_object = object;
1464 bt->bt_dnodesize = dnodesize;
1465 bt->bt_offset = offset;
1468 bt->bt_crtxg = crtxg;
1472 ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1473 uint64_t dnodesize, uint64_t offset, uint64_t gen, uint64_t txg,
1476 ASSERT3U(bt->bt_magic, ==, BT_MAGIC);
1477 ASSERT3U(bt->bt_objset, ==, dmu_objset_id(os));
1478 ASSERT3U(bt->bt_object, ==, object);
1479 ASSERT3U(bt->bt_dnodesize, ==, dnodesize);
1480 ASSERT3U(bt->bt_offset, ==, offset);
1481 ASSERT3U(bt->bt_gen, <=, gen);
1482 ASSERT3U(bt->bt_txg, <=, txg);
1483 ASSERT3U(bt->bt_crtxg, ==, crtxg);
1486 static ztest_block_tag_t *
1487 ztest_bt_bonus(dmu_buf_t *db)
1489 dmu_object_info_t doi;
1490 ztest_block_tag_t *bt;
1492 dmu_object_info_from_db(db, &doi);
1493 ASSERT3U(doi.doi_bonus_size, <=, db->db_size);
1494 ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt));
1495 bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt));
1501 * Generate a token to fill up unused bonus buffer space. Try to make
1502 * it unique to the object, generation, and offset to verify that data
1503 * is not getting overwritten by data from other dnodes.
1505 #define ZTEST_BONUS_FILL_TOKEN(obj, ds, gen, offset) \
1506 (((ds) << 48) | ((gen) << 32) | ((obj) << 8) | (offset))
1509 * Fill up the unused bonus buffer region before the block tag with a
1510 * verifiable pattern. Filling the whole bonus area with non-zero data
1511 * helps ensure that all dnode traversal code properly skips the
1512 * interior regions of large dnodes.
1515 ztest_fill_unused_bonus(dmu_buf_t *db, void *end, uint64_t obj,
1516 objset_t *os, uint64_t gen)
1520 ASSERT(IS_P2ALIGNED((char *)end - (char *)db->db_data, 8));
1522 for (bonusp = db->db_data; bonusp < (uint64_t *)end; bonusp++) {
1523 uint64_t token = ZTEST_BONUS_FILL_TOKEN(obj, dmu_objset_id(os),
1524 gen, bonusp - (uint64_t *)db->db_data);
1530 * Verify that the unused area of a bonus buffer is filled with the
1534 ztest_verify_unused_bonus(dmu_buf_t *db, void *end, uint64_t obj,
1535 objset_t *os, uint64_t gen)
1539 for (bonusp = db->db_data; bonusp < (uint64_t *)end; bonusp++) {
1540 uint64_t token = ZTEST_BONUS_FILL_TOKEN(obj, dmu_objset_id(os),
1541 gen, bonusp - (uint64_t *)db->db_data);
1542 VERIFY3U(*bonusp, ==, token);
1550 #define lrz_type lr_mode
1551 #define lrz_blocksize lr_uid
1552 #define lrz_ibshift lr_gid
1553 #define lrz_bonustype lr_rdev
1554 #define lrz_dnodesize lr_crtime[1]
1557 ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr)
1559 char *name = (void *)(lr + 1); /* name follows lr */
1560 size_t namesize = strlen(name) + 1;
1563 if (zil_replaying(zd->zd_zilog, tx))
1566 itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize);
1567 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1568 sizeof (*lr) + namesize - sizeof (lr_t));
1570 zil_itx_assign(zd->zd_zilog, itx, tx);
1574 ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr, uint64_t object)
1576 char *name = (void *)(lr + 1); /* name follows lr */
1577 size_t namesize = strlen(name) + 1;
1580 if (zil_replaying(zd->zd_zilog, tx))
1583 itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize);
1584 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1585 sizeof (*lr) + namesize - sizeof (lr_t));
1587 itx->itx_oid = object;
1588 zil_itx_assign(zd->zd_zilog, itx, tx);
1592 ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr)
1595 itx_wr_state_t write_state = ztest_random(WR_NUM_STATES);
1597 if (zil_replaying(zd->zd_zilog, tx))
1600 if (lr->lr_length > ZIL_MAX_LOG_DATA)
1601 write_state = WR_INDIRECT;
1603 itx = zil_itx_create(TX_WRITE,
1604 sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0));
1606 if (write_state == WR_COPIED &&
1607 dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length,
1608 ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) {
1609 zil_itx_destroy(itx);
1610 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1611 write_state = WR_NEED_COPY;
1613 itx->itx_private = zd;
1614 itx->itx_wr_state = write_state;
1615 itx->itx_sync = (ztest_random(8) == 0);
1616 itx->itx_sod += (write_state == WR_NEED_COPY ? lr->lr_length : 0);
1618 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1619 sizeof (*lr) - sizeof (lr_t));
1621 zil_itx_assign(zd->zd_zilog, itx, tx);
1625 ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr)
1629 if (zil_replaying(zd->zd_zilog, tx))
1632 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1633 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1634 sizeof (*lr) - sizeof (lr_t));
1636 itx->itx_sync = B_FALSE;
1637 zil_itx_assign(zd->zd_zilog, itx, tx);
1641 ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr)
1645 if (zil_replaying(zd->zd_zilog, tx))
1648 itx = zil_itx_create(TX_SETATTR, sizeof (*lr));
1649 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1650 sizeof (*lr) - sizeof (lr_t));
1652 itx->itx_sync = B_FALSE;
1653 zil_itx_assign(zd->zd_zilog, itx, tx);
1660 ztest_replay_create(ztest_ds_t *zd, lr_create_t *lr, boolean_t byteswap)
1662 char *name = (void *)(lr + 1); /* name follows lr */
1663 objset_t *os = zd->zd_os;
1664 ztest_block_tag_t *bbt;
1672 byteswap_uint64_array(lr, sizeof (*lr));
1674 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1675 ASSERT(name[0] != '\0');
1677 tx = dmu_tx_create(os);
1679 dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name);
1681 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1682 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1684 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1687 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1691 ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid);
1692 bonuslen = DN_BONUS_SIZE(lr->lrz_dnodesize);
1694 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1695 if (lr->lr_foid == 0) {
1696 lr->lr_foid = zap_create_dnsize(os,
1697 lr->lrz_type, lr->lrz_bonustype,
1698 bonuslen, lr->lrz_dnodesize, tx);
1700 error = zap_create_claim_dnsize(os, lr->lr_foid,
1701 lr->lrz_type, lr->lrz_bonustype,
1702 bonuslen, lr->lrz_dnodesize, tx);
1705 if (lr->lr_foid == 0) {
1706 lr->lr_foid = dmu_object_alloc_dnsize(os,
1707 lr->lrz_type, 0, lr->lrz_bonustype,
1708 bonuslen, lr->lrz_dnodesize, tx);
1710 error = dmu_object_claim_dnsize(os, lr->lr_foid,
1711 lr->lrz_type, 0, lr->lrz_bonustype,
1712 bonuslen, lr->lrz_dnodesize, tx);
1717 ASSERT3U(error, ==, EEXIST);
1718 ASSERT(zd->zd_zilog->zl_replay);
1723 ASSERT(lr->lr_foid != 0);
1725 if (lr->lrz_type != DMU_OT_ZAP_OTHER)
1726 VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid,
1727 lr->lrz_blocksize, lr->lrz_ibshift, tx));
1729 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1730 bbt = ztest_bt_bonus(db);
1731 dmu_buf_will_dirty(db, tx);
1732 ztest_bt_generate(bbt, os, lr->lr_foid, lr->lrz_dnodesize, -1ULL,
1733 lr->lr_gen, txg, txg);
1734 ztest_fill_unused_bonus(db, bbt, lr->lr_foid, os, lr->lr_gen);
1735 dmu_buf_rele(db, FTAG);
1737 VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1,
1740 (void) ztest_log_create(zd, tx, lr);
1748 ztest_replay_remove(ztest_ds_t *zd, lr_remove_t *lr, boolean_t byteswap)
1750 char *name = (void *)(lr + 1); /* name follows lr */
1751 objset_t *os = zd->zd_os;
1752 dmu_object_info_t doi;
1754 uint64_t object, txg;
1757 byteswap_uint64_array(lr, sizeof (*lr));
1759 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1760 ASSERT(name[0] != '\0');
1763 zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object));
1764 ASSERT(object != 0);
1766 ztest_object_lock(zd, object, RL_WRITER);
1768 VERIFY3U(0, ==, dmu_object_info(os, object, &doi));
1770 tx = dmu_tx_create(os);
1772 dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name);
1773 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1775 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1777 ztest_object_unlock(zd, object);
1781 if (doi.doi_type == DMU_OT_ZAP_OTHER) {
1782 VERIFY3U(0, ==, zap_destroy(os, object, tx));
1784 VERIFY3U(0, ==, dmu_object_free(os, object, tx));
1787 VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx));
1789 (void) ztest_log_remove(zd, tx, lr, object);
1793 ztest_object_unlock(zd, object);
1799 ztest_replay_write(ztest_ds_t *zd, lr_write_t *lr, boolean_t byteswap)
1801 objset_t *os = zd->zd_os;
1802 void *data = lr + 1; /* data follows lr */
1803 uint64_t offset, length;
1804 ztest_block_tag_t *bt = data;
1805 ztest_block_tag_t *bbt;
1806 uint64_t gen, txg, lrtxg, crtxg;
1807 dmu_object_info_t doi;
1810 arc_buf_t *abuf = NULL;
1814 byteswap_uint64_array(lr, sizeof (*lr));
1816 offset = lr->lr_offset;
1817 length = lr->lr_length;
1819 /* If it's a dmu_sync() block, write the whole block */
1820 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
1821 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
1822 if (length < blocksize) {
1823 offset -= offset % blocksize;
1828 if (bt->bt_magic == BSWAP_64(BT_MAGIC))
1829 byteswap_uint64_array(bt, sizeof (*bt));
1831 if (bt->bt_magic != BT_MAGIC)
1834 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1835 rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER);
1837 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1839 dmu_object_info_from_db(db, &doi);
1841 bbt = ztest_bt_bonus(db);
1842 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1844 crtxg = bbt->bt_crtxg;
1845 lrtxg = lr->lr_common.lrc_txg;
1847 tx = dmu_tx_create(os);
1849 dmu_tx_hold_write(tx, lr->lr_foid, offset, length);
1851 if (ztest_random(8) == 0 && length == doi.doi_data_block_size &&
1852 P2PHASE(offset, length) == 0)
1853 abuf = dmu_request_arcbuf(db, length);
1855 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1858 dmu_return_arcbuf(abuf);
1859 dmu_buf_rele(db, FTAG);
1860 ztest_range_unlock(zd, rl);
1861 ztest_object_unlock(zd, lr->lr_foid);
1867 * Usually, verify the old data before writing new data --
1868 * but not always, because we also want to verify correct
1869 * behavior when the data was not recently read into cache.
1871 ASSERT(offset % doi.doi_data_block_size == 0);
1872 if (ztest_random(4) != 0) {
1873 int prefetch = ztest_random(2) ?
1874 DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
1875 ztest_block_tag_t rbt;
1877 VERIFY(dmu_read(os, lr->lr_foid, offset,
1878 sizeof (rbt), &rbt, prefetch) == 0);
1879 if (rbt.bt_magic == BT_MAGIC) {
1880 ztest_bt_verify(&rbt, os, lr->lr_foid, 0,
1881 offset, gen, txg, crtxg);
1886 * Writes can appear to be newer than the bonus buffer because
1887 * the ztest_get_data() callback does a dmu_read() of the
1888 * open-context data, which may be different than the data
1889 * as it was when the write was generated.
1891 if (zd->zd_zilog->zl_replay) {
1892 ztest_bt_verify(bt, os, lr->lr_foid, 0, offset,
1893 MAX(gen, bt->bt_gen), MAX(txg, lrtxg),
1898 * Set the bt's gen/txg to the bonus buffer's gen/txg
1899 * so that all of the usual ASSERTs will work.
1901 ztest_bt_generate(bt, os, lr->lr_foid, 0, offset, gen, txg,
1906 dmu_write(os, lr->lr_foid, offset, length, data, tx);
1908 bcopy(data, abuf->b_data, length);
1909 dmu_assign_arcbuf(db, offset, abuf, tx);
1912 (void) ztest_log_write(zd, tx, lr);
1914 dmu_buf_rele(db, FTAG);
1918 ztest_range_unlock(zd, rl);
1919 ztest_object_unlock(zd, lr->lr_foid);
1925 ztest_replay_truncate(ztest_ds_t *zd, lr_truncate_t *lr, boolean_t byteswap)
1927 objset_t *os = zd->zd_os;
1933 byteswap_uint64_array(lr, sizeof (*lr));
1935 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1936 rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length,
1939 tx = dmu_tx_create(os);
1941 dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length);
1943 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1945 ztest_range_unlock(zd, rl);
1946 ztest_object_unlock(zd, lr->lr_foid);
1950 VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset,
1951 lr->lr_length, tx) == 0);
1953 (void) ztest_log_truncate(zd, tx, lr);
1957 ztest_range_unlock(zd, rl);
1958 ztest_object_unlock(zd, lr->lr_foid);
1964 ztest_replay_setattr(ztest_ds_t *zd, lr_setattr_t *lr, boolean_t byteswap)
1966 objset_t *os = zd->zd_os;
1969 ztest_block_tag_t *bbt;
1970 uint64_t txg, lrtxg, crtxg, dnodesize;
1973 byteswap_uint64_array(lr, sizeof (*lr));
1975 ztest_object_lock(zd, lr->lr_foid, RL_WRITER);
1977 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1979 tx = dmu_tx_create(os);
1980 dmu_tx_hold_bonus(tx, lr->lr_foid);
1982 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1984 dmu_buf_rele(db, FTAG);
1985 ztest_object_unlock(zd, lr->lr_foid);
1989 bbt = ztest_bt_bonus(db);
1990 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1991 crtxg = bbt->bt_crtxg;
1992 lrtxg = lr->lr_common.lrc_txg;
1993 dnodesize = bbt->bt_dnodesize;
1995 if (zd->zd_zilog->zl_replay) {
1996 ASSERT(lr->lr_size != 0);
1997 ASSERT(lr->lr_mode != 0);
2001 * Randomly change the size and increment the generation.
2003 lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) *
2005 lr->lr_mode = bbt->bt_gen + 1;
2010 * Verify that the current bonus buffer is not newer than our txg.
2012 ztest_bt_verify(bbt, os, lr->lr_foid, dnodesize, -1ULL, lr->lr_mode,
2013 MAX(txg, lrtxg), crtxg);
2015 dmu_buf_will_dirty(db, tx);
2017 ASSERT3U(lr->lr_size, >=, sizeof (*bbt));
2018 ASSERT3U(lr->lr_size, <=, db->db_size);
2019 VERIFY0(dmu_set_bonus(db, lr->lr_size, tx));
2020 bbt = ztest_bt_bonus(db);
2022 ztest_bt_generate(bbt, os, lr->lr_foid, dnodesize, -1ULL, lr->lr_mode,
2024 ztest_fill_unused_bonus(db, bbt, lr->lr_foid, os, bbt->bt_gen);
2025 dmu_buf_rele(db, FTAG);
2027 (void) ztest_log_setattr(zd, tx, lr);
2031 ztest_object_unlock(zd, lr->lr_foid);
2036 zil_replay_func_t ztest_replay_vector[TX_MAX_TYPE] = {
2037 NULL, /* 0 no such transaction type */
2038 (zil_replay_func_t)ztest_replay_create, /* TX_CREATE */
2039 NULL, /* TX_MKDIR */
2040 NULL, /* TX_MKXATTR */
2041 NULL, /* TX_SYMLINK */
2042 (zil_replay_func_t)ztest_replay_remove, /* TX_REMOVE */
2043 NULL, /* TX_RMDIR */
2045 NULL, /* TX_RENAME */
2046 (zil_replay_func_t)ztest_replay_write, /* TX_WRITE */
2047 (zil_replay_func_t)ztest_replay_truncate, /* TX_TRUNCATE */
2048 (zil_replay_func_t)ztest_replay_setattr, /* TX_SETATTR */
2050 NULL, /* TX_CREATE_ACL */
2051 NULL, /* TX_CREATE_ATTR */
2052 NULL, /* TX_CREATE_ACL_ATTR */
2053 NULL, /* TX_MKDIR_ACL */
2054 NULL, /* TX_MKDIR_ATTR */
2055 NULL, /* TX_MKDIR_ACL_ATTR */
2056 NULL, /* TX_WRITE2 */
2060 * ZIL get_data callbacks
2062 typedef struct ztest_zgd_private {
2066 } ztest_zgd_private_t;
2069 ztest_get_done(zgd_t *zgd, int error)
2071 ztest_zgd_private_t *zzp = zgd->zgd_private;
2072 ztest_ds_t *zd = zzp->z_zd;
2073 uint64_t object = zzp->z_object;
2076 dmu_buf_rele(zgd->zgd_db, zgd);
2078 ztest_range_unlock(zd, zzp->z_rl);
2079 ztest_object_unlock(zd, object);
2081 if (error == 0 && zgd->zgd_bp)
2082 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
2084 umem_free(zgd, sizeof (*zgd));
2085 umem_free(zzp, sizeof (*zzp));
2089 ztest_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
2091 ztest_ds_t *zd = arg;
2092 objset_t *os = zd->zd_os;
2093 uint64_t object = lr->lr_foid;
2094 uint64_t offset = lr->lr_offset;
2095 uint64_t size = lr->lr_length;
2096 blkptr_t *bp = &lr->lr_blkptr;
2097 uint64_t txg = lr->lr_common.lrc_txg;
2099 dmu_object_info_t doi;
2103 ztest_zgd_private_t *zgd_private;
2105 ztest_object_lock(zd, object, RL_READER);
2106 error = dmu_bonus_hold(os, object, FTAG, &db);
2108 ztest_object_unlock(zd, object);
2112 crtxg = ztest_bt_bonus(db)->bt_crtxg;
2114 if (crtxg == 0 || crtxg > txg) {
2115 dmu_buf_rele(db, FTAG);
2116 ztest_object_unlock(zd, object);
2120 dmu_object_info_from_db(db, &doi);
2121 dmu_buf_rele(db, FTAG);
2124 zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL);
2125 zgd->zgd_zilog = zd->zd_zilog;
2126 zgd_private = umem_zalloc(sizeof (ztest_zgd_private_t), UMEM_NOFAIL);
2127 zgd_private->z_zd = zd;
2128 zgd_private->z_object = object;
2129 zgd->zgd_private = zgd_private;
2131 if (buf != NULL) { /* immediate write */
2132 zgd_private->z_rl = ztest_range_lock(zd, object, offset, size,
2135 error = dmu_read(os, object, offset, size, buf,
2136 DMU_READ_NO_PREFETCH);
2139 size = doi.doi_data_block_size;
2141 offset = P2ALIGN(offset, size);
2143 ASSERT(offset < size);
2147 zgd_private->z_rl = ztest_range_lock(zd, object, offset, size,
2150 error = dmu_buf_hold(os, object, offset, zgd, &db,
2151 DMU_READ_NO_PREFETCH);
2154 blkptr_t *obp = dmu_buf_get_blkptr(db);
2156 ASSERT(BP_IS_HOLE(bp));
2163 ASSERT(db->db_offset == offset);
2164 ASSERT(db->db_size == size);
2166 error = dmu_sync(zio, lr->lr_common.lrc_txg,
2167 ztest_get_done, zgd);
2174 ztest_get_done(zgd, error);
2180 ztest_lr_alloc(size_t lrsize, char *name)
2183 size_t namesize = name ? strlen(name) + 1 : 0;
2185 lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
2188 bcopy(name, lr + lrsize, namesize);
2194 ztest_lr_free(void *lr, size_t lrsize, char *name)
2196 size_t namesize = name ? strlen(name) + 1 : 0;
2198 umem_free(lr, lrsize + namesize);
2202 * Lookup a bunch of objects. Returns the number of objects not found.
2205 ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
2211 ASSERT(mutex_held(&zd->zd_dirobj_lock));
2213 for (i = 0; i < count; i++, od++) {
2215 error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
2216 sizeof (uint64_t), 1, &od->od_object);
2218 ASSERT(error == ENOENT);
2219 ASSERT(od->od_object == 0);
2223 ztest_block_tag_t *bbt;
2224 dmu_object_info_t doi;
2226 ASSERT(od->od_object != 0);
2227 ASSERT(missing == 0); /* there should be no gaps */
2229 ztest_object_lock(zd, od->od_object, RL_READER);
2230 VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
2231 od->od_object, FTAG, &db));
2232 dmu_object_info_from_db(db, &doi);
2233 bbt = ztest_bt_bonus(db);
2234 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
2235 od->od_type = doi.doi_type;
2236 od->od_blocksize = doi.doi_data_block_size;
2237 od->od_gen = bbt->bt_gen;
2238 dmu_buf_rele(db, FTAG);
2239 ztest_object_unlock(zd, od->od_object);
2247 ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
2252 ASSERT(mutex_held(&zd->zd_dirobj_lock));
2254 for (i = 0; i < count; i++, od++) {
2261 lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2263 lr->lr_doid = od->od_dir;
2264 lr->lr_foid = 0; /* 0 to allocate, > 0 to claim */
2265 lr->lrz_type = od->od_crtype;
2266 lr->lrz_blocksize = od->od_crblocksize;
2267 lr->lrz_ibshift = ztest_random_ibshift();
2268 lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
2269 lr->lrz_dnodesize = od->od_crdnodesize;
2270 lr->lr_gen = od->od_crgen;
2271 lr->lr_crtime[0] = time(NULL);
2273 if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
2274 ASSERT(missing == 0);
2278 od->od_object = lr->lr_foid;
2279 od->od_type = od->od_crtype;
2280 od->od_blocksize = od->od_crblocksize;
2281 od->od_gen = od->od_crgen;
2282 ASSERT(od->od_object != 0);
2285 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2292 ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
2298 ASSERT(mutex_held(&zd->zd_dirobj_lock));
2302 for (i = count - 1; i >= 0; i--, od--) {
2309 * No object was found.
2311 if (od->od_object == 0)
2314 lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2316 lr->lr_doid = od->od_dir;
2318 if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
2319 ASSERT3U(error, ==, ENOSPC);
2324 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2331 ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
2337 lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
2339 lr->lr_foid = object;
2340 lr->lr_offset = offset;
2341 lr->lr_length = size;
2343 BP_ZERO(&lr->lr_blkptr);
2345 bcopy(data, lr + 1, size);
2347 error = ztest_replay_write(zd, lr, B_FALSE);
2349 ztest_lr_free(lr, sizeof (*lr) + size, NULL);
2355 ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2360 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2362 lr->lr_foid = object;
2363 lr->lr_offset = offset;
2364 lr->lr_length = size;
2366 error = ztest_replay_truncate(zd, lr, B_FALSE);
2368 ztest_lr_free(lr, sizeof (*lr), NULL);
2374 ztest_setattr(ztest_ds_t *zd, uint64_t object)
2379 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2381 lr->lr_foid = object;
2385 error = ztest_replay_setattr(zd, lr, B_FALSE);
2387 ztest_lr_free(lr, sizeof (*lr), NULL);
2393 ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2395 objset_t *os = zd->zd_os;
2400 txg_wait_synced(dmu_objset_pool(os), 0);
2402 ztest_object_lock(zd, object, RL_READER);
2403 rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
2405 tx = dmu_tx_create(os);
2407 dmu_tx_hold_write(tx, object, offset, size);
2409 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
2412 dmu_prealloc(os, object, offset, size, tx);
2414 txg_wait_synced(dmu_objset_pool(os), txg);
2416 (void) dmu_free_long_range(os, object, offset, size);
2419 ztest_range_unlock(zd, rl);
2420 ztest_object_unlock(zd, object);
2424 ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
2427 ztest_block_tag_t wbt;
2428 dmu_object_info_t doi;
2429 enum ztest_io_type io_type;
2433 VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
2434 blocksize = doi.doi_data_block_size;
2435 data = umem_alloc(blocksize, UMEM_NOFAIL);
2438 * Pick an i/o type at random, biased toward writing block tags.
2440 io_type = ztest_random(ZTEST_IO_TYPES);
2441 if (ztest_random(2) == 0)
2442 io_type = ZTEST_IO_WRITE_TAG;
2444 (void) rw_rdlock(&zd->zd_zilog_lock);
2448 case ZTEST_IO_WRITE_TAG:
2449 ztest_bt_generate(&wbt, zd->zd_os, object, doi.doi_dnodesize,
2451 (void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
2454 case ZTEST_IO_WRITE_PATTERN:
2455 (void) memset(data, 'a' + (object + offset) % 5, blocksize);
2456 if (ztest_random(2) == 0) {
2458 * Induce fletcher2 collisions to ensure that
2459 * zio_ddt_collision() detects and resolves them
2460 * when using fletcher2-verify for deduplication.
2462 ((uint64_t *)data)[0] ^= 1ULL << 63;
2463 ((uint64_t *)data)[4] ^= 1ULL << 63;
2465 (void) ztest_write(zd, object, offset, blocksize, data);
2468 case ZTEST_IO_WRITE_ZEROES:
2469 bzero(data, blocksize);
2470 (void) ztest_write(zd, object, offset, blocksize, data);
2473 case ZTEST_IO_TRUNCATE:
2474 (void) ztest_truncate(zd, object, offset, blocksize);
2477 case ZTEST_IO_SETATTR:
2478 (void) ztest_setattr(zd, object);
2483 case ZTEST_IO_REWRITE:
2484 (void) rw_rdlock(&ztest_name_lock);
2485 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2486 ZFS_PROP_CHECKSUM, spa_dedup_checksum(ztest_spa),
2488 VERIFY(err == 0 || err == ENOSPC);
2489 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2490 ZFS_PROP_COMPRESSION,
2491 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION),
2493 VERIFY(err == 0 || err == ENOSPC);
2494 (void) rw_unlock(&ztest_name_lock);
2496 VERIFY0(dmu_read(zd->zd_os, object, offset, blocksize, data,
2497 DMU_READ_NO_PREFETCH));
2499 (void) ztest_write(zd, object, offset, blocksize, data);
2503 (void) rw_unlock(&zd->zd_zilog_lock);
2505 umem_free(data, blocksize);
2509 * Initialize an object description template.
2512 ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
2513 dmu_object_type_t type, uint64_t blocksize, uint64_t dnodesize,
2516 od->od_dir = ZTEST_DIROBJ;
2519 od->od_crtype = type;
2520 od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
2521 od->od_crdnodesize = dnodesize ? dnodesize : ztest_random_dnodesize();
2524 od->od_type = DMU_OT_NONE;
2525 od->od_blocksize = 0;
2528 (void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
2529 tag, (longlong_t)id, (u_longlong_t)index);
2533 * Lookup or create the objects for a test using the od template.
2534 * If the objects do not all exist, or if 'remove' is specified,
2535 * remove any existing objects and create new ones. Otherwise,
2536 * use the existing objects.
2539 ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
2541 int count = size / sizeof (*od);
2544 mutex_enter(&zd->zd_dirobj_lock);
2545 if ((ztest_lookup(zd, od, count) != 0 || remove) &&
2546 (ztest_remove(zd, od, count) != 0 ||
2547 ztest_create(zd, od, count) != 0))
2550 mutex_exit(&zd->zd_dirobj_lock);
2557 ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
2559 zilog_t *zilog = zd->zd_zilog;
2561 (void) rw_rdlock(&zd->zd_zilog_lock);
2563 zil_commit(zilog, ztest_random(ZTEST_OBJECTS));
2566 * Remember the committed values in zd, which is in parent/child
2567 * shared memory. If we die, the next iteration of ztest_run()
2568 * will verify that the log really does contain this record.
2570 mutex_enter(&zilog->zl_lock);
2571 ASSERT(zd->zd_shared != NULL);
2572 ASSERT3U(zd->zd_shared->zd_seq, <=, zilog->zl_commit_lr_seq);
2573 zd->zd_shared->zd_seq = zilog->zl_commit_lr_seq;
2574 mutex_exit(&zilog->zl_lock);
2576 (void) rw_unlock(&zd->zd_zilog_lock);
2580 * This function is designed to simulate the operations that occur during a
2581 * mount/unmount operation. We hold the dataset across these operations in an
2582 * attempt to expose any implicit assumptions about ZIL management.
2586 ztest_zil_remount(ztest_ds_t *zd, uint64_t id)
2588 objset_t *os = zd->zd_os;
2591 * We grab the zd_dirobj_lock to ensure that no other thread is
2592 * updating the zil (i.e. adding in-memory log records) and the
2593 * zd_zilog_lock to block any I/O.
2595 mutex_enter(&zd->zd_dirobj_lock);
2596 (void) rw_wrlock(&zd->zd_zilog_lock);
2598 /* zfs_sb_teardown() */
2599 zil_close(zd->zd_zilog);
2601 /* zfsvfs_setup() */
2602 VERIFY(zil_open(os, ztest_get_data) == zd->zd_zilog);
2603 zil_replay(os, zd, ztest_replay_vector);
2605 (void) rw_unlock(&zd->zd_zilog_lock);
2606 mutex_exit(&zd->zd_dirobj_lock);
2610 * Verify that we can't destroy an active pool, create an existing pool,
2611 * or create a pool with a bad vdev spec.
2615 ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2617 ztest_shared_opts_t *zo = &ztest_opts;
2622 * Attempt to create using a bad file.
2624 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2625 VERIFY3U(ENOENT, ==,
2626 spa_create("ztest_bad_file", nvroot, NULL, NULL));
2627 nvlist_free(nvroot);
2630 * Attempt to create using a bad mirror.
2632 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 2, 1);
2633 VERIFY3U(ENOENT, ==,
2634 spa_create("ztest_bad_mirror", nvroot, NULL, NULL));
2635 nvlist_free(nvroot);
2638 * Attempt to create an existing pool. It shouldn't matter
2639 * what's in the nvroot; we should fail with EEXIST.
2641 (void) rw_rdlock(&ztest_name_lock);
2642 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2643 VERIFY3U(EEXIST, ==, spa_create(zo->zo_pool, nvroot, NULL, NULL));
2644 nvlist_free(nvroot);
2645 VERIFY3U(0, ==, spa_open(zo->zo_pool, &spa, FTAG));
2646 VERIFY3U(EBUSY, ==, spa_destroy(zo->zo_pool));
2647 spa_close(spa, FTAG);
2649 (void) rw_unlock(&ztest_name_lock);
2654 ztest_spa_upgrade(ztest_ds_t *zd, uint64_t id)
2657 uint64_t initial_version = SPA_VERSION_INITIAL;
2658 uint64_t version, newversion;
2659 nvlist_t *nvroot, *props;
2662 mutex_enter(&ztest_vdev_lock);
2663 name = kmem_asprintf("%s_upgrade", ztest_opts.zo_pool);
2666 * Clean up from previous runs.
2668 (void) spa_destroy(name);
2670 nvroot = make_vdev_root(NULL, NULL, name, ztest_opts.zo_vdev_size, 0,
2671 0, ztest_opts.zo_raidz, ztest_opts.zo_mirrors, 1);
2674 * If we're configuring a RAIDZ device then make sure that the
2675 * the initial version is capable of supporting that feature.
2677 switch (ztest_opts.zo_raidz_parity) {
2680 initial_version = SPA_VERSION_INITIAL;
2683 initial_version = SPA_VERSION_RAIDZ2;
2686 initial_version = SPA_VERSION_RAIDZ3;
2691 * Create a pool with a spa version that can be upgraded. Pick
2692 * a value between initial_version and SPA_VERSION_BEFORE_FEATURES.
2695 version = ztest_random_spa_version(initial_version);
2696 } while (version > SPA_VERSION_BEFORE_FEATURES);
2698 props = fnvlist_alloc();
2699 fnvlist_add_uint64(props,
2700 zpool_prop_to_name(ZPOOL_PROP_VERSION), version);
2701 VERIFY3S(spa_create(name, nvroot, props, NULL), ==, 0);
2702 fnvlist_free(nvroot);
2703 fnvlist_free(props);
2705 VERIFY3S(spa_open(name, &spa, FTAG), ==, 0);
2706 VERIFY3U(spa_version(spa), ==, version);
2707 newversion = ztest_random_spa_version(version + 1);
2709 if (ztest_opts.zo_verbose >= 4) {
2710 (void) printf("upgrading spa version from %llu to %llu\n",
2711 (u_longlong_t)version, (u_longlong_t)newversion);
2714 spa_upgrade(spa, newversion);
2715 VERIFY3U(spa_version(spa), >, version);
2716 VERIFY3U(spa_version(spa), ==, fnvlist_lookup_uint64(spa->spa_config,
2717 zpool_prop_to_name(ZPOOL_PROP_VERSION)));
2718 spa_close(spa, FTAG);
2721 mutex_exit(&ztest_vdev_lock);
2725 vdev_lookup_by_path(vdev_t *vd, const char *path)
2730 if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
2733 for (c = 0; c < vd->vdev_children; c++)
2734 if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
2742 * Find the first available hole which can be used as a top-level.
2745 find_vdev_hole(spa_t *spa)
2747 vdev_t *rvd = spa->spa_root_vdev;
2750 ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
2752 for (c = 0; c < rvd->vdev_children; c++) {
2753 vdev_t *cvd = rvd->vdev_child[c];
2755 if (cvd->vdev_ishole)
2762 * Verify that vdev_add() works as expected.
2766 ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2768 ztest_shared_t *zs = ztest_shared;
2769 spa_t *spa = ztest_spa;
2775 mutex_enter(&ztest_vdev_lock);
2776 leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * ztest_opts.zo_raidz;
2778 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2780 ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2783 * If we have slogs then remove them 1/4 of the time.
2785 if (spa_has_slogs(spa) && ztest_random(4) == 0) {
2787 * Grab the guid from the head of the log class rotor.
2789 guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid;
2791 spa_config_exit(spa, SCL_VDEV, FTAG);
2794 * We have to grab the zs_name_lock as writer to
2795 * prevent a race between removing a slog (dmu_objset_find)
2796 * and destroying a dataset. Removing the slog will
2797 * grab a reference on the dataset which may cause
2798 * dsl_destroy_head() to fail with EBUSY thus
2799 * leaving the dataset in an inconsistent state.
2801 rw_wrlock(&ztest_name_lock);
2802 error = spa_vdev_remove(spa, guid, B_FALSE);
2803 rw_unlock(&ztest_name_lock);
2805 if (error && error != EEXIST)
2806 fatal(0, "spa_vdev_remove() = %d", error);
2808 spa_config_exit(spa, SCL_VDEV, FTAG);
2811 * Make 1/4 of the devices be log devices.
2813 nvroot = make_vdev_root(NULL, NULL, NULL,
2814 ztest_opts.zo_vdev_size, 0,
2815 ztest_random(4) == 0, ztest_opts.zo_raidz,
2818 error = spa_vdev_add(spa, nvroot);
2819 nvlist_free(nvroot);
2821 if (error == ENOSPC)
2822 ztest_record_enospc("spa_vdev_add");
2823 else if (error != 0)
2824 fatal(0, "spa_vdev_add() = %d", error);
2827 mutex_exit(&ztest_vdev_lock);
2831 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
2835 ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
2837 ztest_shared_t *zs = ztest_shared;
2838 spa_t *spa = ztest_spa;
2839 vdev_t *rvd = spa->spa_root_vdev;
2840 spa_aux_vdev_t *sav;
2846 path = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2848 if (ztest_random(2) == 0) {
2849 sav = &spa->spa_spares;
2850 aux = ZPOOL_CONFIG_SPARES;
2852 sav = &spa->spa_l2cache;
2853 aux = ZPOOL_CONFIG_L2CACHE;
2856 mutex_enter(&ztest_vdev_lock);
2858 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2860 if (sav->sav_count != 0 && ztest_random(4) == 0) {
2862 * Pick a random device to remove.
2864 guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
2867 * Find an unused device we can add.
2869 zs->zs_vdev_aux = 0;
2872 (void) snprintf(path, MAXPATHLEN, ztest_aux_template,
2873 ztest_opts.zo_dir, ztest_opts.zo_pool, aux,
2875 for (c = 0; c < sav->sav_count; c++)
2876 if (strcmp(sav->sav_vdevs[c]->vdev_path,
2879 if (c == sav->sav_count &&
2880 vdev_lookup_by_path(rvd, path) == NULL)
2886 spa_config_exit(spa, SCL_VDEV, FTAG);
2892 nvlist_t *nvroot = make_vdev_root(NULL, aux, NULL,
2893 (ztest_opts.zo_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
2894 error = spa_vdev_add(spa, nvroot);
2896 fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
2897 nvlist_free(nvroot);
2900 * Remove an existing device. Sometimes, dirty its
2901 * vdev state first to make sure we handle removal
2902 * of devices that have pending state changes.
2904 if (ztest_random(2) == 0)
2905 (void) vdev_online(spa, guid, 0, NULL);
2907 error = spa_vdev_remove(spa, guid, B_FALSE);
2908 if (error != 0 && error != EBUSY)
2909 fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
2912 mutex_exit(&ztest_vdev_lock);
2914 umem_free(path, MAXPATHLEN);
2918 * split a pool if it has mirror tlvdevs
2922 ztest_split_pool(ztest_ds_t *zd, uint64_t id)
2924 ztest_shared_t *zs = ztest_shared;
2925 spa_t *spa = ztest_spa;
2926 vdev_t *rvd = spa->spa_root_vdev;
2927 nvlist_t *tree, **child, *config, *split, **schild;
2928 uint_t c, children, schildren = 0, lastlogid = 0;
2931 mutex_enter(&ztest_vdev_lock);
2933 /* ensure we have a useable config; mirrors of raidz aren't supported */
2934 if (zs->zs_mirrors < 3 || ztest_opts.zo_raidz > 1) {
2935 mutex_exit(&ztest_vdev_lock);
2939 /* clean up the old pool, if any */
2940 (void) spa_destroy("splitp");
2942 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2944 /* generate a config from the existing config */
2945 mutex_enter(&spa->spa_props_lock);
2946 VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
2948 mutex_exit(&spa->spa_props_lock);
2950 VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2953 schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
2954 for (c = 0; c < children; c++) {
2955 vdev_t *tvd = rvd->vdev_child[c];
2959 if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
2960 VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
2962 VERIFY(nvlist_add_string(schild[schildren],
2963 ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
2964 VERIFY(nvlist_add_uint64(schild[schildren],
2965 ZPOOL_CONFIG_IS_HOLE, 1) == 0);
2967 lastlogid = schildren;
2972 VERIFY(nvlist_lookup_nvlist_array(child[c],
2973 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2974 VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
2977 /* OK, create a config that can be used to split */
2978 VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
2979 VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
2980 VDEV_TYPE_ROOT) == 0);
2981 VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
2982 lastlogid != 0 ? lastlogid : schildren) == 0);
2984 VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
2985 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
2987 for (c = 0; c < schildren; c++)
2988 nvlist_free(schild[c]);
2992 spa_config_exit(spa, SCL_VDEV, FTAG);
2994 (void) rw_wrlock(&ztest_name_lock);
2995 error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
2996 (void) rw_unlock(&ztest_name_lock);
2998 nvlist_free(config);
3001 (void) printf("successful split - results:\n");
3002 mutex_enter(&spa_namespace_lock);
3003 show_pool_stats(spa);
3004 show_pool_stats(spa_lookup("splitp"));
3005 mutex_exit(&spa_namespace_lock);
3009 mutex_exit(&ztest_vdev_lock);
3014 * Verify that we can attach and detach devices.
3018 ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
3020 ztest_shared_t *zs = ztest_shared;
3021 spa_t *spa = ztest_spa;
3022 spa_aux_vdev_t *sav = &spa->spa_spares;
3023 vdev_t *rvd = spa->spa_root_vdev;
3024 vdev_t *oldvd, *newvd, *pvd;
3028 uint64_t ashift = ztest_get_ashift();
3029 uint64_t oldguid, pguid;
3030 uint64_t oldsize, newsize;
3031 char *oldpath, *newpath;
3033 int oldvd_has_siblings = B_FALSE;
3034 int newvd_is_spare = B_FALSE;
3036 int error, expected_error;
3038 oldpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
3039 newpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
3041 mutex_enter(&ztest_vdev_lock);
3042 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
3044 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
3047 * Decide whether to do an attach or a replace.
3049 replacing = ztest_random(2);
3052 * Pick a random top-level vdev.
3054 top = ztest_random_vdev_top(spa, B_TRUE);
3057 * Pick a random leaf within it.
3059 leaf = ztest_random(leaves);
3064 oldvd = rvd->vdev_child[top];
3065 if (zs->zs_mirrors >= 1) {
3066 ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
3067 ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
3068 oldvd = oldvd->vdev_child[leaf / ztest_opts.zo_raidz];
3070 if (ztest_opts.zo_raidz > 1) {
3071 ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
3072 ASSERT(oldvd->vdev_children == ztest_opts.zo_raidz);
3073 oldvd = oldvd->vdev_child[leaf % ztest_opts.zo_raidz];
3077 * If we're already doing an attach or replace, oldvd may be a
3078 * mirror vdev -- in which case, pick a random child.
3080 while (oldvd->vdev_children != 0) {
3081 oldvd_has_siblings = B_TRUE;
3082 ASSERT(oldvd->vdev_children >= 2);
3083 oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
3086 oldguid = oldvd->vdev_guid;
3087 oldsize = vdev_get_min_asize(oldvd);
3088 oldvd_is_log = oldvd->vdev_top->vdev_islog;
3089 (void) strcpy(oldpath, oldvd->vdev_path);
3090 pvd = oldvd->vdev_parent;
3091 pguid = pvd->vdev_guid;
3094 * If oldvd has siblings, then half of the time, detach it.
3096 if (oldvd_has_siblings && ztest_random(2) == 0) {
3097 spa_config_exit(spa, SCL_VDEV, FTAG);
3098 error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
3099 if (error != 0 && error != ENODEV && error != EBUSY &&
3101 fatal(0, "detach (%s) returned %d", oldpath, error);
3106 * For the new vdev, choose with equal probability between the two
3107 * standard paths (ending in either 'a' or 'b') or a random hot spare.
3109 if (sav->sav_count != 0 && ztest_random(3) == 0) {
3110 newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
3111 newvd_is_spare = B_TRUE;
3112 (void) strcpy(newpath, newvd->vdev_path);
3114 (void) snprintf(newpath, MAXPATHLEN, ztest_dev_template,
3115 ztest_opts.zo_dir, ztest_opts.zo_pool,
3116 top * leaves + leaf);
3117 if (ztest_random(2) == 0)
3118 newpath[strlen(newpath) - 1] = 'b';
3119 newvd = vdev_lookup_by_path(rvd, newpath);
3123 newsize = vdev_get_min_asize(newvd);
3126 * Make newsize a little bigger or smaller than oldsize.
3127 * If it's smaller, the attach should fail.
3128 * If it's larger, and we're doing a replace,
3129 * we should get dynamic LUN growth when we're done.
3131 newsize = 10 * oldsize / (9 + ztest_random(3));
3135 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
3136 * unless it's a replace; in that case any non-replacing parent is OK.
3138 * If newvd is already part of the pool, it should fail with EBUSY.
3140 * If newvd is too small, it should fail with EOVERFLOW.
3142 if (pvd->vdev_ops != &vdev_mirror_ops &&
3143 pvd->vdev_ops != &vdev_root_ops && (!replacing ||
3144 pvd->vdev_ops == &vdev_replacing_ops ||
3145 pvd->vdev_ops == &vdev_spare_ops))
3146 expected_error = ENOTSUP;
3147 else if (newvd_is_spare && (!replacing || oldvd_is_log))
3148 expected_error = ENOTSUP;
3149 else if (newvd == oldvd)
3150 expected_error = replacing ? 0 : EBUSY;
3151 else if (vdev_lookup_by_path(rvd, newpath) != NULL)
3152 expected_error = EBUSY;
3153 else if (newsize < oldsize)
3154 expected_error = EOVERFLOW;
3155 else if (ashift > oldvd->vdev_top->vdev_ashift)
3156 expected_error = EDOM;
3160 spa_config_exit(spa, SCL_VDEV, FTAG);
3163 * Build the nvlist describing newpath.
3165 root = make_vdev_root(newpath, NULL, NULL, newvd == NULL ? newsize : 0,
3166 ashift, 0, 0, 0, 1);
3168 error = spa_vdev_attach(spa, oldguid, root, replacing);
3173 * If our parent was the replacing vdev, but the replace completed,
3174 * then instead of failing with ENOTSUP we may either succeed,
3175 * fail with ENODEV, or fail with EOVERFLOW.
3177 if (expected_error == ENOTSUP &&
3178 (error == 0 || error == ENODEV || error == EOVERFLOW))
3179 expected_error = error;
3182 * If someone grew the LUN, the replacement may be too small.
3184 if (error == EOVERFLOW || error == EBUSY)
3185 expected_error = error;
3187 /* XXX workaround 6690467 */
3188 if (error != expected_error && expected_error != EBUSY) {
3189 fatal(0, "attach (%s %llu, %s %llu, %d) "
3190 "returned %d, expected %d",
3191 oldpath, oldsize, newpath,
3192 newsize, replacing, error, expected_error);
3195 mutex_exit(&ztest_vdev_lock);
3197 umem_free(oldpath, MAXPATHLEN);
3198 umem_free(newpath, MAXPATHLEN);
3202 * Callback function which expands the physical size of the vdev.
3205 grow_vdev(vdev_t *vd, void *arg)
3207 ASSERTV(spa_t *spa = vd->vdev_spa);
3208 size_t *newsize = arg;
3212 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3213 ASSERT(vd->vdev_ops->vdev_op_leaf);
3215 if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
3218 fsize = lseek(fd, 0, SEEK_END);
3219 VERIFY(ftruncate(fd, *newsize) == 0);
3221 if (ztest_opts.zo_verbose >= 6) {
3222 (void) printf("%s grew from %lu to %lu bytes\n",
3223 vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
3230 * Callback function which expands a given vdev by calling vdev_online().
3234 online_vdev(vdev_t *vd, void *arg)
3236 spa_t *spa = vd->vdev_spa;
3237 vdev_t *tvd = vd->vdev_top;
3238 uint64_t guid = vd->vdev_guid;
3239 uint64_t generation = spa->spa_config_generation + 1;
3240 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
3243 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3244 ASSERT(vd->vdev_ops->vdev_op_leaf);
3246 /* Calling vdev_online will initialize the new metaslabs */
3247 spa_config_exit(spa, SCL_STATE, spa);
3248 error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
3249 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3252 * If vdev_online returned an error or the underlying vdev_open
3253 * failed then we abort the expand. The only way to know that
3254 * vdev_open fails is by checking the returned newstate.
3256 if (error || newstate != VDEV_STATE_HEALTHY) {
3257 if (ztest_opts.zo_verbose >= 5) {
3258 (void) printf("Unable to expand vdev, state %llu, "
3259 "error %d\n", (u_longlong_t)newstate, error);
3263 ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
3266 * Since we dropped the lock we need to ensure that we're
3267 * still talking to the original vdev. It's possible this
3268 * vdev may have been detached/replaced while we were
3269 * trying to online it.
3271 if (generation != spa->spa_config_generation) {
3272 if (ztest_opts.zo_verbose >= 5) {
3273 (void) printf("vdev configuration has changed, "
3274 "guid %llu, state %llu, expected gen %llu, "
3277 (u_longlong_t)tvd->vdev_state,
3278 (u_longlong_t)generation,
3279 (u_longlong_t)spa->spa_config_generation);
3287 * Traverse the vdev tree calling the supplied function.
3288 * We continue to walk the tree until we either have walked all
3289 * children or we receive a non-NULL return from the callback.
3290 * If a NULL callback is passed, then we just return back the first
3291 * leaf vdev we encounter.
3294 vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
3298 if (vd->vdev_ops->vdev_op_leaf) {
3302 return (func(vd, arg));
3305 for (c = 0; c < vd->vdev_children; c++) {
3306 vdev_t *cvd = vd->vdev_child[c];
3307 if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
3314 * Verify that dynamic LUN growth works as expected.
3318 ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
3320 spa_t *spa = ztest_spa;
3322 metaslab_class_t *mc;
3323 metaslab_group_t *mg;
3324 size_t psize, newsize;
3326 uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
3328 mutex_enter(&ztest_vdev_lock);
3329 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3331 top = ztest_random_vdev_top(spa, B_TRUE);
3333 tvd = spa->spa_root_vdev->vdev_child[top];
3336 old_ms_count = tvd->vdev_ms_count;
3337 old_class_space = metaslab_class_get_space(mc);
3340 * Determine the size of the first leaf vdev associated with
3341 * our top-level device.
3343 vd = vdev_walk_tree(tvd, NULL, NULL);
3344 ASSERT3P(vd, !=, NULL);
3345 ASSERT(vd->vdev_ops->vdev_op_leaf);
3347 psize = vd->vdev_psize;
3350 * We only try to expand the vdev if it's healthy, less than 4x its
3351 * original size, and it has a valid psize.
3353 if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
3354 psize == 0 || psize >= 4 * ztest_opts.zo_vdev_size) {
3355 spa_config_exit(spa, SCL_STATE, spa);
3356 mutex_exit(&ztest_vdev_lock);
3360 newsize = psize + psize / 8;
3361 ASSERT3U(newsize, >, psize);
3363 if (ztest_opts.zo_verbose >= 6) {
3364 (void) printf("Expanding LUN %s from %lu to %lu\n",
3365 vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
3369 * Growing the vdev is a two step process:
3370 * 1). expand the physical size (i.e. relabel)
3371 * 2). online the vdev to create the new metaslabs
3373 if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
3374 vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
3375 tvd->vdev_state != VDEV_STATE_HEALTHY) {
3376 if (ztest_opts.zo_verbose >= 5) {
3377 (void) printf("Could not expand LUN because "
3378 "the vdev configuration changed.\n");
3380 spa_config_exit(spa, SCL_STATE, spa);
3381 mutex_exit(&ztest_vdev_lock);
3385 spa_config_exit(spa, SCL_STATE, spa);
3388 * Expanding the LUN will update the config asynchronously,
3389 * thus we must wait for the async thread to complete any
3390 * pending tasks before proceeding.
3394 mutex_enter(&spa->spa_async_lock);
3395 done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
3396 mutex_exit(&spa->spa_async_lock);
3399 txg_wait_synced(spa_get_dsl(spa), 0);
3400 (void) poll(NULL, 0, 100);
3403 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3405 tvd = spa->spa_root_vdev->vdev_child[top];
3406 new_ms_count = tvd->vdev_ms_count;
3407 new_class_space = metaslab_class_get_space(mc);
3409 if (tvd->vdev_mg != mg || mg->mg_class != mc) {
3410 if (ztest_opts.zo_verbose >= 5) {
3411 (void) printf("Could not verify LUN expansion due to "
3412 "intervening vdev offline or remove.\n");
3414 spa_config_exit(spa, SCL_STATE, spa);
3415 mutex_exit(&ztest_vdev_lock);
3420 * Make sure we were able to grow the vdev.
3422 if (new_ms_count <= old_ms_count)
3423 fatal(0, "LUN expansion failed: ms_count %llu <= %llu\n",
3424 old_ms_count, new_ms_count);
3427 * Make sure we were able to grow the pool.
3429 if (new_class_space <= old_class_space)
3430 fatal(0, "LUN expansion failed: class_space %llu <= %llu\n",
3431 old_class_space, new_class_space);
3433 if (ztest_opts.zo_verbose >= 5) {
3434 char oldnumbuf[6], newnumbuf[6];
3436 nicenum(old_class_space, oldnumbuf);
3437 nicenum(new_class_space, newnumbuf);
3438 (void) printf("%s grew from %s to %s\n",
3439 spa->spa_name, oldnumbuf, newnumbuf);
3442 spa_config_exit(spa, SCL_STATE, spa);
3443 mutex_exit(&ztest_vdev_lock);
3447 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
3451 ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3454 * Create the objects common to all ztest datasets.
3456 VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
3457 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
3461 ztest_dataset_create(char *dsname)
3463 uint64_t zilset = ztest_random(100);
3464 int err = dmu_objset_create(dsname, DMU_OST_OTHER, 0,
3465 ztest_objset_create_cb, NULL);
3467 if (err || zilset < 80)
3470 if (ztest_opts.zo_verbose >= 5)
3471 (void) printf("Setting dataset %s to sync always\n", dsname);
3472 return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC,
3473 ZFS_SYNC_ALWAYS, B_FALSE));
3478 ztest_objset_destroy_cb(const char *name, void *arg)
3481 dmu_object_info_t doi;
3485 * Verify that the dataset contains a directory object.
3487 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_TRUE, FTAG, &os));
3488 error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
3489 if (error != ENOENT) {
3490 /* We could have crashed in the middle of destroying it */
3492 ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
3493 ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
3495 dmu_objset_disown(os, FTAG);
3498 * Destroy the dataset.
3500 if (strchr(name, '@') != NULL) {
3501 VERIFY0(dsl_destroy_snapshot(name, B_FALSE));
3503 VERIFY0(dsl_destroy_head(name));
3509 ztest_snapshot_create(char *osname, uint64_t id)
3511 char snapname[ZFS_MAX_DATASET_NAME_LEN];
3514 (void) snprintf(snapname, sizeof (snapname), "%llu", (u_longlong_t)id);
3516 error = dmu_objset_snapshot_one(osname, snapname);
3517 if (error == ENOSPC) {
3518 ztest_record_enospc(FTAG);
3521 if (error != 0 && error != EEXIST) {
3522 fatal(0, "ztest_snapshot_create(%s@%s) = %d", osname,
3529 ztest_snapshot_destroy(char *osname, uint64_t id)
3531 char snapname[ZFS_MAX_DATASET_NAME_LEN];
3534 (void) snprintf(snapname, sizeof (snapname), "%s@%llu", osname,
3537 error = dsl_destroy_snapshot(snapname, B_FALSE);
3538 if (error != 0 && error != ENOENT)
3539 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
3545 ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
3551 char name[ZFS_MAX_DATASET_NAME_LEN];
3555 zdtmp = umem_alloc(sizeof (ztest_ds_t), UMEM_NOFAIL);
3557 (void) rw_rdlock(&ztest_name_lock);
3559 (void) snprintf(name, sizeof (name), "%s/temp_%llu",
3560 ztest_opts.zo_pool, (u_longlong_t)id);
3563 * If this dataset exists from a previous run, process its replay log
3564 * half of the time. If we don't replay it, then dsl_destroy_head()
3565 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
3567 if (ztest_random(2) == 0 &&
3568 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) {
3569 ztest_zd_init(zdtmp, NULL, os);
3570 zil_replay(os, zdtmp, ztest_replay_vector);
3571 ztest_zd_fini(zdtmp);
3572 dmu_objset_disown(os, FTAG);
3576 * There may be an old instance of the dataset we're about to
3577 * create lying around from a previous run. If so, destroy it
3578 * and all of its snapshots.
3580 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
3581 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
3584 * Verify that the destroyed dataset is no longer in the namespace.
3586 VERIFY3U(ENOENT, ==, dmu_objset_own(name, DMU_OST_OTHER, B_TRUE,
3590 * Verify that we can create a new dataset.
3592 error = ztest_dataset_create(name);
3594 if (error == ENOSPC) {
3595 ztest_record_enospc(FTAG);
3598 fatal(0, "dmu_objset_create(%s) = %d", name, error);
3601 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os));
3603 ztest_zd_init(zdtmp, NULL, os);
3606 * Open the intent log for it.
3608 zilog = zil_open(os, ztest_get_data);
3611 * Put some objects in there, do a little I/O to them,
3612 * and randomly take a couple of snapshots along the way.
3614 iters = ztest_random(5);
3615 for (i = 0; i < iters; i++) {
3616 ztest_dmu_object_alloc_free(zdtmp, id);
3617 if (ztest_random(iters) == 0)
3618 (void) ztest_snapshot_create(name, i);
3622 * Verify that we cannot create an existing dataset.
3624 VERIFY3U(EEXIST, ==,
3625 dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL));
3628 * Verify that we can hold an objset that is also owned.
3630 VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
3631 dmu_objset_rele(os2, FTAG);
3634 * Verify that we cannot own an objset that is already owned.
3637 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2));
3640 dmu_objset_disown(os, FTAG);
3641 ztest_zd_fini(zdtmp);
3643 (void) rw_unlock(&ztest_name_lock);
3645 umem_free(zdtmp, sizeof (ztest_ds_t));
3649 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3652 ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
3654 (void) rw_rdlock(&ztest_name_lock);
3655 (void) ztest_snapshot_destroy(zd->zd_name, id);
3656 (void) ztest_snapshot_create(zd->zd_name, id);
3657 (void) rw_unlock(&ztest_name_lock);
3661 * Cleanup non-standard snapshots and clones.
3664 ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
3673 snap1name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3674 clone1name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3675 snap2name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3676 clone2name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3677 snap3name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3679 (void) snprintf(snap1name, ZFS_MAX_DATASET_NAME_LEN,
3680 "%s@s1_%llu", osname, (u_longlong_t)id);
3681 (void) snprintf(clone1name, ZFS_MAX_DATASET_NAME_LEN,
3682 "%s/c1_%llu", osname, (u_longlong_t)id);
3683 (void) snprintf(snap2name, ZFS_MAX_DATASET_NAME_LEN,
3684 "%s@s2_%llu", clone1name, (u_longlong_t)id);
3685 (void) snprintf(clone2name, ZFS_MAX_DATASET_NAME_LEN,
3686 "%s/c2_%llu", osname, (u_longlong_t)id);
3687 (void) snprintf(snap3name, ZFS_MAX_DATASET_NAME_LEN,
3688 "%s@s3_%llu", clone1name, (u_longlong_t)id);
3690 error = dsl_destroy_head(clone2name);
3691 if (error && error != ENOENT)
3692 fatal(0, "dsl_destroy_head(%s) = %d", clone2name, error);
3693 error = dsl_destroy_snapshot(snap3name, B_FALSE);
3694 if (error && error != ENOENT)
3695 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap3name, error);
3696 error = dsl_destroy_snapshot(snap2name, B_FALSE);
3697 if (error && error != ENOENT)
3698 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap2name, error);
3699 error = dsl_destroy_head(clone1name);
3700 if (error && error != ENOENT)
3701 fatal(0, "dsl_destroy_head(%s) = %d", clone1name, error);
3702 error = dsl_destroy_snapshot(snap1name, B_FALSE);
3703 if (error && error != ENOENT)
3704 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap1name, error);
3706 umem_free(snap1name, ZFS_MAX_DATASET_NAME_LEN);
3707 umem_free(clone1name, ZFS_MAX_DATASET_NAME_LEN);
3708 umem_free(snap2name, ZFS_MAX_DATASET_NAME_LEN);
3709 umem_free(clone2name, ZFS_MAX_DATASET_NAME_LEN);
3710 umem_free(snap3name, ZFS_MAX_DATASET_NAME_LEN);
3714 * Verify dsl_dataset_promote handles EBUSY
3717 ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
3725 char *osname = zd->zd_name;
3728 snap1name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3729 clone1name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3730 snap2name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3731 clone2name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3732 snap3name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3734 (void) rw_rdlock(&ztest_name_lock);
3736 ztest_dsl_dataset_cleanup(osname, id);
3738 (void) snprintf(snap1name, ZFS_MAX_DATASET_NAME_LEN,
3739 "%s@s1_%llu", osname, (u_longlong_t)id);
3740 (void) snprintf(clone1name, ZFS_MAX_DATASET_NAME_LEN,
3741 "%s/c1_%llu", osname, (u_longlong_t)id);
3742 (void) snprintf(snap2name, ZFS_MAX_DATASET_NAME_LEN,
3743 "%s@s2_%llu", clone1name, (u_longlong_t)id);
3744 (void) snprintf(clone2name, ZFS_MAX_DATASET_NAME_LEN,
3745 "%s/c2_%llu", osname, (u_longlong_t)id);
3746 (void) snprintf(snap3name, ZFS_MAX_DATASET_NAME_LEN,
3747 "%s@s3_%llu", clone1name, (u_longlong_t)id);
3749 error = dmu_objset_snapshot_one(osname, strchr(snap1name, '@') + 1);
3750 if (error && error != EEXIST) {
3751 if (error == ENOSPC) {
3752 ztest_record_enospc(FTAG);
3755 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
3758 error = dmu_objset_clone(clone1name, snap1name);
3760 if (error == ENOSPC) {
3761 ztest_record_enospc(FTAG);
3764 fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
3767 error = dmu_objset_snapshot_one(clone1name, strchr(snap2name, '@') + 1);
3768 if (error && error != EEXIST) {
3769 if (error == ENOSPC) {
3770 ztest_record_enospc(FTAG);
3773 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
3776 error = dmu_objset_snapshot_one(clone1name, strchr(snap3name, '@') + 1);
3777 if (error && error != EEXIST) {
3778 if (error == ENOSPC) {
3779 ztest_record_enospc(FTAG);
3782 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
3785 error = dmu_objset_clone(clone2name, snap3name);
3787 if (error == ENOSPC) {
3788 ztest_record_enospc(FTAG);
3791 fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
3794 error = dmu_objset_own(snap2name, DMU_OST_ANY, B_TRUE, FTAG, &os);
3796 fatal(0, "dmu_objset_own(%s) = %d", snap2name, error);
3797 error = dsl_dataset_promote(clone2name, NULL);
3798 if (error == ENOSPC) {
3799 dmu_objset_disown(os, FTAG);
3800 ztest_record_enospc(FTAG);
3804 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
3806 dmu_objset_disown(os, FTAG);
3809 ztest_dsl_dataset_cleanup(osname, id);
3811 (void) rw_unlock(&ztest_name_lock);
3813 umem_free(snap1name, ZFS_MAX_DATASET_NAME_LEN);
3814 umem_free(clone1name, ZFS_MAX_DATASET_NAME_LEN);
3815 umem_free(snap2name, ZFS_MAX_DATASET_NAME_LEN);
3816 umem_free(clone2name, ZFS_MAX_DATASET_NAME_LEN);
3817 umem_free(snap3name, ZFS_MAX_DATASET_NAME_LEN);
3820 #undef OD_ARRAY_SIZE
3821 #define OD_ARRAY_SIZE 4
3824 * Verify that dmu_object_{alloc,free} work as expected.
3827 ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
3834 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
3835 od = umem_alloc(size, UMEM_NOFAIL);
3836 batchsize = OD_ARRAY_SIZE;
3838 for (b = 0; b < batchsize; b++)
3839 ztest_od_init(od + b, id, FTAG, b, DMU_OT_UINT64_OTHER,
3843 * Destroy the previous batch of objects, create a new batch,
3844 * and do some I/O on the new objects.
3846 if (ztest_object_init(zd, od, size, B_TRUE) != 0)
3849 while (ztest_random(4 * batchsize) != 0)
3850 ztest_io(zd, od[ztest_random(batchsize)].od_object,
3851 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3853 umem_free(od, size);
3856 #undef OD_ARRAY_SIZE
3857 #define OD_ARRAY_SIZE 2
3860 * Verify that dmu_{read,write} work as expected.
3863 ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
3868 objset_t *os = zd->zd_os;
3869 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
3870 od = umem_alloc(size, UMEM_NOFAIL);
3872 int i, freeit, error;
3874 bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
3875 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3876 uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
3877 uint64_t regions = 997;
3878 uint64_t stride = 123456789ULL;
3879 uint64_t width = 40;
3880 int free_percent = 5;
3883 * This test uses two objects, packobj and bigobj, that are always
3884 * updated together (i.e. in the same tx) so that their contents are
3885 * in sync and can be compared. Their contents relate to each other
3886 * in a simple way: packobj is a dense array of 'bufwad' structures,
3887 * while bigobj is a sparse array of the same bufwads. Specifically,
3888 * for any index n, there are three bufwads that should be identical:
3890 * packobj, at offset n * sizeof (bufwad_t)
3891 * bigobj, at the head of the nth chunk
3892 * bigobj, at the tail of the nth chunk
3894 * The chunk size is arbitrary. It doesn't have to be a power of two,
3895 * and it doesn't have any relation to the object blocksize.
3896 * The only requirement is that it can hold at least two bufwads.
3898 * Normally, we write the bufwad to each of these locations.
3899 * However, free_percent of the time we instead write zeroes to
3900 * packobj and perform a dmu_free_range() on bigobj. By comparing
3901 * bigobj to packobj, we can verify that the DMU is correctly
3902 * tracking which parts of an object are allocated and free,
3903 * and that the contents of the allocated blocks are correct.
3907 * Read the directory info. If it's the first time, set things up.
3909 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, chunksize);
3910 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, 0,
3913 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
3914 umem_free(od, size);
3918 bigobj = od[0].od_object;
3919 packobj = od[1].od_object;
3920 chunksize = od[0].od_gen;
3921 ASSERT(chunksize == od[1].od_gen);
3924 * Prefetch a random chunk of the big object.
3925 * Our aim here is to get some async reads in flight
3926 * for blocks that we may free below; the DMU should
3927 * handle this race correctly.
3929 n = ztest_random(regions) * stride + ztest_random(width);
3930 s = 1 + ztest_random(2 * width - 1);
3931 dmu_prefetch(os, bigobj, 0, n * chunksize, s * chunksize,
3932 ZIO_PRIORITY_SYNC_READ);
3935 * Pick a random index and compute the offsets into packobj and bigobj.
3937 n = ztest_random(regions) * stride + ztest_random(width);
3938 s = 1 + ztest_random(width - 1);
3940 packoff = n * sizeof (bufwad_t);
3941 packsize = s * sizeof (bufwad_t);
3943 bigoff = n * chunksize;
3944 bigsize = s * chunksize;
3946 packbuf = umem_alloc(packsize, UMEM_NOFAIL);
3947 bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
3950 * free_percent of the time, free a range of bigobj rather than
3953 freeit = (ztest_random(100) < free_percent);
3956 * Read the current contents of our objects.
3958 error = dmu_read(os, packobj, packoff, packsize, packbuf,
3961 error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
3966 * Get a tx for the mods to both packobj and bigobj.
3968 tx = dmu_tx_create(os);
3970 dmu_tx_hold_write(tx, packobj, packoff, packsize);
3973 dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
3975 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
3977 /* This accounts for setting the checksum/compression. */
3978 dmu_tx_hold_bonus(tx, bigobj);
3980 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3982 umem_free(packbuf, packsize);
3983 umem_free(bigbuf, bigsize);
3984 umem_free(od, size);
3988 enum zio_checksum cksum;
3990 cksum = (enum zio_checksum)
3991 ztest_random_dsl_prop(ZFS_PROP_CHECKSUM);
3992 } while (cksum >= ZIO_CHECKSUM_LEGACY_FUNCTIONS);
3993 dmu_object_set_checksum(os, bigobj, cksum, tx);
3995 enum zio_compress comp;
3997 comp = (enum zio_compress)
3998 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION);
3999 } while (comp >= ZIO_COMPRESS_LEGACY_FUNCTIONS);
4000 dmu_object_set_compress(os, bigobj, comp, tx);
4003 * For each index from n to n + s, verify that the existing bufwad
4004 * in packobj matches the bufwads at the head and tail of the
4005 * corresponding chunk in bigobj. Then update all three bufwads
4006 * with the new values we want to write out.
4008 for (i = 0; i < s; i++) {
4010 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
4012 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
4014 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
4016 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
4017 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
4019 if (pack->bw_txg > txg)
4020 fatal(0, "future leak: got %llx, open txg is %llx",
4023 if (pack->bw_data != 0 && pack->bw_index != n + i)
4024 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4025 pack->bw_index, n, i);
4027 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
4028 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
4030 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
4031 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
4034 bzero(pack, sizeof (bufwad_t));
4036 pack->bw_index = n + i;
4038 pack->bw_data = 1 + ztest_random(-2ULL);
4045 * We've verified all the old bufwads, and made new ones.
4046 * Now write them out.
4048 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
4051 if (ztest_opts.zo_verbose >= 7) {
4052 (void) printf("freeing offset %llx size %llx"
4054 (u_longlong_t)bigoff,
4055 (u_longlong_t)bigsize,
4058 VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
4060 if (ztest_opts.zo_verbose >= 7) {
4061 (void) printf("writing offset %llx size %llx"
4063 (u_longlong_t)bigoff,
4064 (u_longlong_t)bigsize,
4067 dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
4073 * Sanity check the stuff we just wrote.
4076 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
4077 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
4079 VERIFY(0 == dmu_read(os, packobj, packoff,
4080 packsize, packcheck, DMU_READ_PREFETCH));
4081 VERIFY(0 == dmu_read(os, bigobj, bigoff,
4082 bigsize, bigcheck, DMU_READ_PREFETCH));
4084 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
4085 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
4087 umem_free(packcheck, packsize);
4088 umem_free(bigcheck, bigsize);
4091 umem_free(packbuf, packsize);
4092 umem_free(bigbuf, bigsize);
4093 umem_free(od, size);
4097 compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
4098 uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
4106 * For each index from n to n + s, verify that the existing bufwad
4107 * in packobj matches the bufwads at the head and tail of the
4108 * corresponding chunk in bigobj. Then update all three bufwads
4109 * with the new values we want to write out.
4111 for (i = 0; i < s; i++) {
4113 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
4115 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
4117 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
4119 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
4120 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
4122 if (pack->bw_txg > txg)
4123 fatal(0, "future leak: got %llx, open txg is %llx",
4126 if (pack->bw_data != 0 && pack->bw_index != n + i)
4127 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4128 pack->bw_index, n, i);
4130 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
4131 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
4133 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
4134 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
4136 pack->bw_index = n + i;
4138 pack->bw_data = 1 + ztest_random(-2ULL);
4145 #undef OD_ARRAY_SIZE
4146 #define OD_ARRAY_SIZE 2
4149 ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
4151 objset_t *os = zd->zd_os;
4158 bufwad_t *packbuf, *bigbuf;
4159 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
4160 uint64_t blocksize = ztest_random_blocksize();
4161 uint64_t chunksize = blocksize;
4162 uint64_t regions = 997;
4163 uint64_t stride = 123456789ULL;
4165 dmu_buf_t *bonus_db;
4166 arc_buf_t **bigbuf_arcbufs;
4167 dmu_object_info_t doi;
4169 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
4170 od = umem_alloc(size, UMEM_NOFAIL);
4173 * This test uses two objects, packobj and bigobj, that are always
4174 * updated together (i.e. in the same tx) so that their contents are
4175 * in sync and can be compared. Their contents relate to each other
4176 * in a simple way: packobj is a dense array of 'bufwad' structures,
4177 * while bigobj is a sparse array of the same bufwads. Specifically,
4178 * for any index n, there are three bufwads that should be identical:
4180 * packobj, at offset n * sizeof (bufwad_t)
4181 * bigobj, at the head of the nth chunk
4182 * bigobj, at the tail of the nth chunk
4184 * The chunk size is set equal to bigobj block size so that
4185 * dmu_assign_arcbuf() can be tested for object updates.
4189 * Read the directory info. If it's the first time, set things up.
4191 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0, 0);
4192 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, 0,
4196 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
4197 umem_free(od, size);
4201 bigobj = od[0].od_object;
4202 packobj = od[1].od_object;
4203 blocksize = od[0].od_blocksize;
4204 chunksize = blocksize;
4205 ASSERT(chunksize == od[1].od_gen);
4207 VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
4208 VERIFY(ISP2(doi.doi_data_block_size));
4209 VERIFY(chunksize == doi.doi_data_block_size);
4210 VERIFY(chunksize >= 2 * sizeof (bufwad_t));
4213 * Pick a random index and compute the offsets into packobj and bigobj.
4215 n = ztest_random(regions) * stride + ztest_random(width);
4216 s = 1 + ztest_random(width - 1);
4218 packoff = n * sizeof (bufwad_t);
4219 packsize = s * sizeof (bufwad_t);
4221 bigoff = n * chunksize;
4222 bigsize = s * chunksize;
4224 packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
4225 bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
4227 VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
4229 bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
4232 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
4233 * Iteration 1 test zcopy to already referenced dbufs.
4234 * Iteration 2 test zcopy to dirty dbuf in the same txg.
4235 * Iteration 3 test zcopy to dbuf dirty in previous txg.
4236 * Iteration 4 test zcopy when dbuf is no longer dirty.
4237 * Iteration 5 test zcopy when it can't be done.
4238 * Iteration 6 one more zcopy write.
4240 for (i = 0; i < 7; i++) {
4245 * In iteration 5 (i == 5) use arcbufs
4246 * that don't match bigobj blksz to test
4247 * dmu_assign_arcbuf() when it can't directly
4248 * assign an arcbuf to a dbuf.
4250 for (j = 0; j < s; j++) {
4251 if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
4253 dmu_request_arcbuf(bonus_db, chunksize);
4255 bigbuf_arcbufs[2 * j] =
4256 dmu_request_arcbuf(bonus_db, chunksize / 2);
4257 bigbuf_arcbufs[2 * j + 1] =
4258 dmu_request_arcbuf(bonus_db, chunksize / 2);
4263 * Get a tx for the mods to both packobj and bigobj.
4265 tx = dmu_tx_create(os);
4267 dmu_tx_hold_write(tx, packobj, packoff, packsize);
4268 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
4270 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4272 umem_free(packbuf, packsize);
4273 umem_free(bigbuf, bigsize);
4274 for (j = 0; j < s; j++) {
4276 chunksize < (SPA_MINBLOCKSIZE * 2)) {
4277 dmu_return_arcbuf(bigbuf_arcbufs[j]);
4280 bigbuf_arcbufs[2 * j]);
4282 bigbuf_arcbufs[2 * j + 1]);
4285 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4286 umem_free(od, size);
4287 dmu_buf_rele(bonus_db, FTAG);
4292 * 50% of the time don't read objects in the 1st iteration to
4293 * test dmu_assign_arcbuf() for the case when there're no
4294 * existing dbufs for the specified offsets.
4296 if (i != 0 || ztest_random(2) != 0) {
4297 error = dmu_read(os, packobj, packoff,
4298 packsize, packbuf, DMU_READ_PREFETCH);
4300 error = dmu_read(os, bigobj, bigoff, bigsize,
4301 bigbuf, DMU_READ_PREFETCH);
4304 compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
4308 * We've verified all the old bufwads, and made new ones.
4309 * Now write them out.
4311 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
4312 if (ztest_opts.zo_verbose >= 7) {
4313 (void) printf("writing offset %llx size %llx"
4315 (u_longlong_t)bigoff,
4316 (u_longlong_t)bigsize,
4319 for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
4321 if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
4322 bcopy((caddr_t)bigbuf + (off - bigoff),
4323 bigbuf_arcbufs[j]->b_data, chunksize);
4325 bcopy((caddr_t)bigbuf + (off - bigoff),
4326 bigbuf_arcbufs[2 * j]->b_data,
4328 bcopy((caddr_t)bigbuf + (off - bigoff) +
4330 bigbuf_arcbufs[2 * j + 1]->b_data,
4335 VERIFY(dmu_buf_hold(os, bigobj, off,
4336 FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0);
4338 if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
4339 dmu_assign_arcbuf(bonus_db, off,
4340 bigbuf_arcbufs[j], tx);
4342 dmu_assign_arcbuf(bonus_db, off,
4343 bigbuf_arcbufs[2 * j], tx);
4344 dmu_assign_arcbuf(bonus_db,
4345 off + chunksize / 2,
4346 bigbuf_arcbufs[2 * j + 1], tx);
4349 dmu_buf_rele(dbt, FTAG);
4355 * Sanity check the stuff we just wrote.
4358 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
4359 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
4361 VERIFY(0 == dmu_read(os, packobj, packoff,
4362 packsize, packcheck, DMU_READ_PREFETCH));
4363 VERIFY(0 == dmu_read(os, bigobj, bigoff,
4364 bigsize, bigcheck, DMU_READ_PREFETCH));
4366 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
4367 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
4369 umem_free(packcheck, packsize);
4370 umem_free(bigcheck, bigsize);
4373 txg_wait_open(dmu_objset_pool(os), 0);
4374 } else if (i == 3) {
4375 txg_wait_synced(dmu_objset_pool(os), 0);
4379 dmu_buf_rele(bonus_db, FTAG);
4380 umem_free(packbuf, packsize);
4381 umem_free(bigbuf, bigsize);
4382 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4383 umem_free(od, size);
4388 ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
4392 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4393 uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
4394 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4397 * Have multiple threads write to large offsets in an object
4398 * to verify that parallel writes to an object -- even to the
4399 * same blocks within the object -- doesn't cause any trouble.
4401 ztest_od_init(od, ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
4403 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0)
4406 while (ztest_random(10) != 0)
4407 ztest_io(zd, od->od_object, offset);
4409 umem_free(od, sizeof (ztest_od_t));
4413 ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
4416 uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
4417 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4418 uint64_t count = ztest_random(20) + 1;
4419 uint64_t blocksize = ztest_random_blocksize();
4422 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4424 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0, 0);
4426 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4427 !ztest_random(2)) != 0) {
4428 umem_free(od, sizeof (ztest_od_t));
4432 if (ztest_truncate(zd, od->od_object, offset, count * blocksize) != 0) {
4433 umem_free(od, sizeof (ztest_od_t));
4437 ztest_prealloc(zd, od->od_object, offset, count * blocksize);
4439 data = umem_zalloc(blocksize, UMEM_NOFAIL);
4441 while (ztest_random(count) != 0) {
4442 uint64_t randoff = offset + (ztest_random(count) * blocksize);
4443 if (ztest_write(zd, od->od_object, randoff, blocksize,
4446 while (ztest_random(4) != 0)
4447 ztest_io(zd, od->od_object, randoff);
4450 umem_free(data, blocksize);
4451 umem_free(od, sizeof (ztest_od_t));
4455 * Verify that zap_{create,destroy,add,remove,update} work as expected.
4457 #define ZTEST_ZAP_MIN_INTS 1
4458 #define ZTEST_ZAP_MAX_INTS 4
4459 #define ZTEST_ZAP_MAX_PROPS 1000
4462 ztest_zap(ztest_ds_t *zd, uint64_t id)
4464 objset_t *os = zd->zd_os;
4467 uint64_t txg, last_txg;
4468 uint64_t value[ZTEST_ZAP_MAX_INTS];
4469 uint64_t zl_ints, zl_intsize, prop;
4472 char propname[100], txgname[100];
4474 char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
4476 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4477 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0, 0);
4479 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4480 !ztest_random(2)) != 0)
4483 object = od->od_object;
4486 * Generate a known hash collision, and verify that
4487 * we can lookup and remove both entries.
4489 tx = dmu_tx_create(os);
4490 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4491 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4494 for (i = 0; i < 2; i++) {
4496 VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
4499 for (i = 0; i < 2; i++) {
4500 VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
4501 sizeof (uint64_t), 1, &value[i], tx));
4503 zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
4504 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4505 ASSERT3U(zl_ints, ==, 1);
4507 for (i = 0; i < 2; i++) {
4508 VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
4513 * Generate a buch of random entries.
4515 ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
4517 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4518 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4519 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4520 bzero(value, sizeof (value));
4524 * If these zap entries already exist, validate their contents.
4526 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4528 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4529 ASSERT3U(zl_ints, ==, 1);
4531 VERIFY(zap_lookup(os, object, txgname, zl_intsize,
4532 zl_ints, &last_txg) == 0);
4534 VERIFY(zap_length(os, object, propname, &zl_intsize,
4537 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4538 ASSERT3U(zl_ints, ==, ints);
4540 VERIFY(zap_lookup(os, object, propname, zl_intsize,
4541 zl_ints, value) == 0);
4543 for (i = 0; i < ints; i++) {
4544 ASSERT3U(value[i], ==, last_txg + object + i);
4547 ASSERT3U(error, ==, ENOENT);
4551 * Atomically update two entries in our zap object.
4552 * The first is named txg_%llu, and contains the txg
4553 * in which the property was last updated. The second
4554 * is named prop_%llu, and the nth element of its value
4555 * should be txg + object + n.
4557 tx = dmu_tx_create(os);
4558 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4559 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4564 fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
4566 for (i = 0; i < ints; i++)
4567 value[i] = txg + object + i;
4569 VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
4571 VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
4577 * Remove a random pair of entries.
4579 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4580 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4581 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4583 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4585 if (error == ENOENT)
4590 tx = dmu_tx_create(os);
4591 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4592 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4595 VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
4596 VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
4599 umem_free(od, sizeof (ztest_od_t));
4603 * Testcase to test the upgrading of a microzap to fatzap.
4606 ztest_fzap(ztest_ds_t *zd, uint64_t id)
4608 objset_t *os = zd->zd_os;
4610 uint64_t object, txg;
4613 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4614 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0, 0);
4616 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4617 !ztest_random(2)) != 0)
4619 object = od->od_object;
4622 * Add entries to this ZAP and make sure it spills over
4623 * and gets upgraded to a fatzap. Also, since we are adding
4624 * 2050 entries we should see ptrtbl growth and leaf-block split.
4626 for (i = 0; i < 2050; i++) {
4627 char name[ZFS_MAX_DATASET_NAME_LEN];
4632 (void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
4633 (u_longlong_t)id, (u_longlong_t)value);
4635 tx = dmu_tx_create(os);
4636 dmu_tx_hold_zap(tx, object, B_TRUE, name);
4637 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4640 error = zap_add(os, object, name, sizeof (uint64_t), 1,
4642 ASSERT(error == 0 || error == EEXIST);
4646 umem_free(od, sizeof (ztest_od_t));
4651 ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
4653 objset_t *os = zd->zd_os;
4655 uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
4657 int i, namelen, error;
4658 int micro = ztest_random(2);
4659 char name[20], string_value[20];
4662 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4663 ztest_od_init(od, ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER, 0, 0, 0);
4665 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
4666 umem_free(od, sizeof (ztest_od_t));
4670 object = od->od_object;
4673 * Generate a random name of the form 'xxx.....' where each
4674 * x is a random printable character and the dots are dots.
4675 * There are 94 such characters, and the name length goes from
4676 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
4678 namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
4680 for (i = 0; i < 3; i++)
4681 name[i] = '!' + ztest_random('~' - '!' + 1);
4682 for (; i < namelen - 1; i++)
4686 if ((namelen & 1) || micro) {
4687 wsize = sizeof (txg);
4693 data = string_value;
4697 VERIFY0(zap_count(os, object, &count));
4698 ASSERT(count != -1ULL);
4701 * Select an operation: length, lookup, add, update, remove.
4703 i = ztest_random(5);
4706 tx = dmu_tx_create(os);
4707 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4708 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4710 umem_free(od, sizeof (ztest_od_t));
4713 bcopy(name, string_value, namelen);
4717 bzero(string_value, namelen);
4723 error = zap_length(os, object, name, &zl_wsize, &zl_wc);
4725 ASSERT3U(wsize, ==, zl_wsize);
4726 ASSERT3U(wc, ==, zl_wc);
4728 ASSERT3U(error, ==, ENOENT);
4733 error = zap_lookup(os, object, name, wsize, wc, data);
4735 if (data == string_value &&
4736 bcmp(name, data, namelen) != 0)
4737 fatal(0, "name '%s' != val '%s' len %d",
4738 name, data, namelen);
4740 ASSERT3U(error, ==, ENOENT);
4745 error = zap_add(os, object, name, wsize, wc, data, tx);
4746 ASSERT(error == 0 || error == EEXIST);
4750 VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
4754 error = zap_remove(os, object, name, tx);
4755 ASSERT(error == 0 || error == ENOENT);
4762 umem_free(od, sizeof (ztest_od_t));
4766 * Commit callback data.
4768 typedef struct ztest_cb_data {
4769 list_node_t zcd_node;
4771 int zcd_expected_err;
4772 boolean_t zcd_added;
4773 boolean_t zcd_called;
4777 /* This is the actual commit callback function */
4779 ztest_commit_callback(void *arg, int error)
4781 ztest_cb_data_t *data = arg;
4782 uint64_t synced_txg;
4784 VERIFY(data != NULL);
4785 VERIFY3S(data->zcd_expected_err, ==, error);
4786 VERIFY(!data->zcd_called);
4788 synced_txg = spa_last_synced_txg(data->zcd_spa);
4789 if (data->zcd_txg > synced_txg)
4790 fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
4791 ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
4794 data->zcd_called = B_TRUE;
4796 if (error == ECANCELED) {
4797 ASSERT0(data->zcd_txg);
4798 ASSERT(!data->zcd_added);
4801 * The private callback data should be destroyed here, but
4802 * since we are going to check the zcd_called field after
4803 * dmu_tx_abort(), we will destroy it there.
4808 ASSERT(data->zcd_added);
4809 ASSERT3U(data->zcd_txg, !=, 0);
4811 (void) mutex_enter(&zcl.zcl_callbacks_lock);
4813 /* See if this cb was called more quickly */
4814 if ((synced_txg - data->zcd_txg) < zc_min_txg_delay)
4815 zc_min_txg_delay = synced_txg - data->zcd_txg;
4817 /* Remove our callback from the list */
4818 list_remove(&zcl.zcl_callbacks, data);
4820 (void) mutex_exit(&zcl.zcl_callbacks_lock);
4822 umem_free(data, sizeof (ztest_cb_data_t));
4825 /* Allocate and initialize callback data structure */
4826 static ztest_cb_data_t *
4827 ztest_create_cb_data(objset_t *os, uint64_t txg)
4829 ztest_cb_data_t *cb_data;
4831 cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
4833 cb_data->zcd_txg = txg;
4834 cb_data->zcd_spa = dmu_objset_spa(os);
4835 list_link_init(&cb_data->zcd_node);
4841 * Commit callback test.
4844 ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
4846 objset_t *os = zd->zd_os;
4849 ztest_cb_data_t *cb_data[3], *tmp_cb;
4850 uint64_t old_txg, txg;
4853 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4854 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
4856 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
4857 umem_free(od, sizeof (ztest_od_t));
4861 tx = dmu_tx_create(os);
4863 cb_data[0] = ztest_create_cb_data(os, 0);
4864 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
4866 dmu_tx_hold_write(tx, od->od_object, 0, sizeof (uint64_t));
4868 /* Every once in a while, abort the transaction on purpose */
4869 if (ztest_random(100) == 0)
4873 error = dmu_tx_assign(tx, TXG_NOWAIT);
4875 txg = error ? 0 : dmu_tx_get_txg(tx);
4877 cb_data[0]->zcd_txg = txg;
4878 cb_data[1] = ztest_create_cb_data(os, txg);
4879 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
4883 * It's not a strict requirement to call the registered
4884 * callbacks from inside dmu_tx_abort(), but that's what
4885 * it's supposed to happen in the current implementation
4886 * so we will check for that.
4888 for (i = 0; i < 2; i++) {
4889 cb_data[i]->zcd_expected_err = ECANCELED;
4890 VERIFY(!cb_data[i]->zcd_called);
4895 for (i = 0; i < 2; i++) {
4896 VERIFY(cb_data[i]->zcd_called);
4897 umem_free(cb_data[i], sizeof (ztest_cb_data_t));
4900 umem_free(od, sizeof (ztest_od_t));
4904 cb_data[2] = ztest_create_cb_data(os, txg);
4905 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
4908 * Read existing data to make sure there isn't a future leak.
4910 VERIFY(0 == dmu_read(os, od->od_object, 0, sizeof (uint64_t),
4911 &old_txg, DMU_READ_PREFETCH));
4914 fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
4917 dmu_write(os, od->od_object, 0, sizeof (uint64_t), &txg, tx);
4919 (void) mutex_enter(&zcl.zcl_callbacks_lock);
4922 * Since commit callbacks don't have any ordering requirement and since
4923 * it is theoretically possible for a commit callback to be called
4924 * after an arbitrary amount of time has elapsed since its txg has been
4925 * synced, it is difficult to reliably determine whether a commit
4926 * callback hasn't been called due to high load or due to a flawed
4929 * In practice, we will assume that if after a certain number of txgs a
4930 * commit callback hasn't been called, then most likely there's an
4931 * implementation bug..
4933 tmp_cb = list_head(&zcl.zcl_callbacks);
4934 if (tmp_cb != NULL &&
4935 tmp_cb->zcd_txg + ZTEST_COMMIT_CB_THRESH < txg) {
4936 fatal(0, "Commit callback threshold exceeded, oldest txg: %"
4937 PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
4941 * Let's find the place to insert our callbacks.
4943 * Even though the list is ordered by txg, it is possible for the
4944 * insertion point to not be the end because our txg may already be
4945 * quiescing at this point and other callbacks in the open txg
4946 * (from other objsets) may have sneaked in.
4948 tmp_cb = list_tail(&zcl.zcl_callbacks);
4949 while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
4950 tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
4952 /* Add the 3 callbacks to the list */
4953 for (i = 0; i < 3; i++) {
4955 list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
4957 list_insert_after(&zcl.zcl_callbacks, tmp_cb,
4960 cb_data[i]->zcd_added = B_TRUE;
4961 VERIFY(!cb_data[i]->zcd_called);
4963 tmp_cb = cb_data[i];
4968 (void) mutex_exit(&zcl.zcl_callbacks_lock);
4972 umem_free(od, sizeof (ztest_od_t));
4976 * Visit each object in the dataset. Verify that its properties
4977 * are consistent what was stored in the block tag when it was created,
4978 * and that its unused bonus buffer space has not been overwritten.
4981 ztest_verify_dnode_bt(ztest_ds_t *zd, uint64_t id)
4983 objset_t *os = zd->zd_os;
4987 for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
4988 ztest_block_tag_t *bt = NULL;
4989 dmu_object_info_t doi;
4992 if (dmu_bonus_hold(os, obj, FTAG, &db) != 0)
4995 dmu_object_info_from_db(db, &doi);
4996 if (doi.doi_bonus_size >= sizeof (*bt))
4997 bt = ztest_bt_bonus(db);
4999 if (bt && bt->bt_magic == BT_MAGIC) {
5000 ztest_bt_verify(bt, os, obj, doi.doi_dnodesize,
5001 bt->bt_offset, bt->bt_gen, bt->bt_txg,
5003 ztest_verify_unused_bonus(db, bt, obj, os, bt->bt_gen);
5006 dmu_buf_rele(db, FTAG);
5012 ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
5014 zfs_prop_t proplist[] = {
5016 ZFS_PROP_COMPRESSION,
5022 (void) rw_rdlock(&ztest_name_lock);
5024 for (p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
5025 (void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
5026 ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
5028 VERIFY0(ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_RECORDSIZE,
5029 ztest_random_blocksize(), (int)ztest_random(2)));
5031 (void) rw_unlock(&ztest_name_lock);
5036 ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
5038 nvlist_t *props = NULL;
5040 (void) rw_rdlock(&ztest_name_lock);
5042 (void) ztest_spa_prop_set_uint64(ZPOOL_PROP_DEDUPDITTO,
5043 ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
5045 VERIFY0(spa_prop_get(ztest_spa, &props));
5047 if (ztest_opts.zo_verbose >= 6)
5048 dump_nvlist(props, 4);
5052 (void) rw_unlock(&ztest_name_lock);
5056 user_release_one(const char *snapname, const char *holdname)
5058 nvlist_t *snaps, *holds;
5061 snaps = fnvlist_alloc();
5062 holds = fnvlist_alloc();
5063 fnvlist_add_boolean(holds, holdname);
5064 fnvlist_add_nvlist(snaps, snapname, holds);
5065 fnvlist_free(holds);
5066 error = dsl_dataset_user_release(snaps, NULL);
5067 fnvlist_free(snaps);
5072 * Test snapshot hold/release and deferred destroy.
5075 ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
5078 objset_t *os = zd->zd_os;
5082 char clonename[100];
5084 char osname[ZFS_MAX_DATASET_NAME_LEN];
5087 (void) rw_rdlock(&ztest_name_lock);
5089 dmu_objset_name(os, osname);
5091 (void) snprintf(snapname, sizeof (snapname), "sh1_%llu",
5093 (void) snprintf(fullname, sizeof (fullname), "%s@%s", osname, snapname);
5094 (void) snprintf(clonename, sizeof (clonename),
5095 "%s/ch1_%llu", osname, (u_longlong_t)id);
5096 (void) snprintf(tag, sizeof (tag), "tag_%llu", (u_longlong_t)id);
5099 * Clean up from any previous run.
5101 error = dsl_destroy_head(clonename);
5102 if (error != ENOENT)
5104 error = user_release_one(fullname, tag);
5105 if (error != ESRCH && error != ENOENT)
5107 error = dsl_destroy_snapshot(fullname, B_FALSE);
5108 if (error != ENOENT)
5112 * Create snapshot, clone it, mark snap for deferred destroy,
5113 * destroy clone, verify snap was also destroyed.
5115 error = dmu_objset_snapshot_one(osname, snapname);
5117 if (error == ENOSPC) {
5118 ztest_record_enospc("dmu_objset_snapshot");
5121 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
5124 error = dmu_objset_clone(clonename, fullname);
5126 if (error == ENOSPC) {
5127 ztest_record_enospc("dmu_objset_clone");
5130 fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
5133 error = dsl_destroy_snapshot(fullname, B_TRUE);
5135 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5139 error = dsl_destroy_head(clonename);
5141 fatal(0, "dsl_destroy_head(%s) = %d", clonename, error);
5143 error = dmu_objset_hold(fullname, FTAG, &origin);
5144 if (error != ENOENT)
5145 fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
5148 * Create snapshot, add temporary hold, verify that we can't
5149 * destroy a held snapshot, mark for deferred destroy,
5150 * release hold, verify snapshot was destroyed.
5152 error = dmu_objset_snapshot_one(osname, snapname);
5154 if (error == ENOSPC) {
5155 ztest_record_enospc("dmu_objset_snapshot");
5158 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
5161 holds = fnvlist_alloc();
5162 fnvlist_add_string(holds, fullname, tag);
5163 error = dsl_dataset_user_hold(holds, 0, NULL);
5164 fnvlist_free(holds);
5166 if (error == ENOSPC) {
5167 ztest_record_enospc("dsl_dataset_user_hold");
5170 fatal(0, "dsl_dataset_user_hold(%s, %s) = %u",
5171 fullname, tag, error);
5174 error = dsl_destroy_snapshot(fullname, B_FALSE);
5175 if (error != EBUSY) {
5176 fatal(0, "dsl_destroy_snapshot(%s, B_FALSE) = %d",
5180 error = dsl_destroy_snapshot(fullname, B_TRUE);
5182 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5186 error = user_release_one(fullname, tag);
5188 fatal(0, "user_release_one(%s, %s) = %d", fullname, tag, error);
5190 VERIFY3U(dmu_objset_hold(fullname, FTAG, &origin), ==, ENOENT);
5193 (void) rw_unlock(&ztest_name_lock);
5197 * Inject random faults into the on-disk data.
5201 ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
5203 ztest_shared_t *zs = ztest_shared;
5204 spa_t *spa = ztest_spa;
5208 uint64_t bad = 0x1990c0ffeedecadeull;
5213 int bshift = SPA_MAXBLOCKSHIFT + 2; /* don't scrog all labels */
5219 boolean_t islog = B_FALSE;
5221 path0 = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
5222 pathrand = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
5224 mutex_enter(&ztest_vdev_lock);
5225 maxfaults = MAXFAULTS();
5226 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
5227 mirror_save = zs->zs_mirrors;
5228 mutex_exit(&ztest_vdev_lock);
5230 ASSERT(leaves >= 1);
5233 * Grab the name lock as reader. There are some operations
5234 * which don't like to have their vdevs changed while
5235 * they are in progress (i.e. spa_change_guid). Those
5236 * operations will have grabbed the name lock as writer.
5238 (void) rw_rdlock(&ztest_name_lock);
5241 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
5243 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5245 if (ztest_random(2) == 0) {
5247 * Inject errors on a normal data device or slog device.
5249 top = ztest_random_vdev_top(spa, B_TRUE);
5250 leaf = ztest_random(leaves) + zs->zs_splits;
5253 * Generate paths to the first leaf in this top-level vdev,
5254 * and to the random leaf we selected. We'll induce transient
5255 * write failures and random online/offline activity on leaf 0,
5256 * and we'll write random garbage to the randomly chosen leaf.
5258 (void) snprintf(path0, MAXPATHLEN, ztest_dev_template,
5259 ztest_opts.zo_dir, ztest_opts.zo_pool,
5260 top * leaves + zs->zs_splits);
5261 (void) snprintf(pathrand, MAXPATHLEN, ztest_dev_template,
5262 ztest_opts.zo_dir, ztest_opts.zo_pool,
5263 top * leaves + leaf);
5265 vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
5266 if (vd0 != NULL && vd0->vdev_top->vdev_islog)
5270 * If the top-level vdev needs to be resilvered
5271 * then we only allow faults on the device that is
5274 if (vd0 != NULL && maxfaults != 1 &&
5275 (!vdev_resilver_needed(vd0->vdev_top, NULL, NULL) ||
5276 vd0->vdev_resilver_txg != 0)) {
5278 * Make vd0 explicitly claim to be unreadable,
5279 * or unwriteable, or reach behind its back
5280 * and close the underlying fd. We can do this if
5281 * maxfaults == 0 because we'll fail and reexecute,
5282 * and we can do it if maxfaults >= 2 because we'll
5283 * have enough redundancy. If maxfaults == 1, the
5284 * combination of this with injection of random data
5285 * corruption below exceeds the pool's fault tolerance.
5287 vdev_file_t *vf = vd0->vdev_tsd;
5289 if (vf != NULL && ztest_random(3) == 0) {
5290 (void) close(vf->vf_vnode->v_fd);
5291 vf->vf_vnode->v_fd = -1;
5292 } else if (ztest_random(2) == 0) {
5293 vd0->vdev_cant_read = B_TRUE;
5295 vd0->vdev_cant_write = B_TRUE;
5297 guid0 = vd0->vdev_guid;
5301 * Inject errors on an l2cache device.
5303 spa_aux_vdev_t *sav = &spa->spa_l2cache;
5305 if (sav->sav_count == 0) {
5306 spa_config_exit(spa, SCL_STATE, FTAG);
5307 (void) rw_unlock(&ztest_name_lock);
5310 vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
5311 guid0 = vd0->vdev_guid;
5312 (void) strcpy(path0, vd0->vdev_path);
5313 (void) strcpy(pathrand, vd0->vdev_path);
5317 maxfaults = INT_MAX; /* no limit on cache devices */
5320 spa_config_exit(spa, SCL_STATE, FTAG);
5321 (void) rw_unlock(&ztest_name_lock);
5324 * If we can tolerate two or more faults, or we're dealing
5325 * with a slog, randomly online/offline vd0.
5327 if ((maxfaults >= 2 || islog) && guid0 != 0) {
5328 if (ztest_random(10) < 6) {
5329 int flags = (ztest_random(2) == 0 ?
5330 ZFS_OFFLINE_TEMPORARY : 0);
5333 * We have to grab the zs_name_lock as writer to
5334 * prevent a race between offlining a slog and
5335 * destroying a dataset. Offlining the slog will
5336 * grab a reference on the dataset which may cause
5337 * dsl_destroy_head() to fail with EBUSY thus
5338 * leaving the dataset in an inconsistent state.
5341 (void) rw_wrlock(&ztest_name_lock);
5343 VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
5346 (void) rw_unlock(&ztest_name_lock);
5349 * Ideally we would like to be able to randomly
5350 * call vdev_[on|off]line without holding locks
5351 * to force unpredictable failures but the side
5352 * effects of vdev_[on|off]line prevent us from
5353 * doing so. We grab the ztest_vdev_lock here to
5354 * prevent a race between injection testing and
5357 mutex_enter(&ztest_vdev_lock);
5358 (void) vdev_online(spa, guid0, 0, NULL);
5359 mutex_exit(&ztest_vdev_lock);
5367 * We have at least single-fault tolerance, so inject data corruption.
5369 fd = open(pathrand, O_RDWR);
5371 if (fd == -1) /* we hit a gap in the device namespace */
5374 fsize = lseek(fd, 0, SEEK_END);
5376 while (--iters != 0) {
5378 * The offset must be chosen carefully to ensure that
5379 * we do not inject a given logical block with errors
5380 * on two different leaf devices, because ZFS can not
5381 * tolerate that (if maxfaults==1).
5383 * We divide each leaf into chunks of size
5384 * (# leaves * SPA_MAXBLOCKSIZE * 4). Within each chunk
5385 * there is a series of ranges to which we can inject errors.
5386 * Each range can accept errors on only a single leaf vdev.
5387 * The error injection ranges are separated by ranges
5388 * which we will not inject errors on any device (DMZs).
5389 * Each DMZ must be large enough such that a single block
5390 * can not straddle it, so that a single block can not be
5391 * a target in two different injection ranges (on different
5394 * For example, with 3 leaves, each chunk looks like:
5395 * 0 to 32M: injection range for leaf 0
5396 * 32M to 64M: DMZ - no injection allowed
5397 * 64M to 96M: injection range for leaf 1
5398 * 96M to 128M: DMZ - no injection allowed
5399 * 128M to 160M: injection range for leaf 2
5400 * 160M to 192M: DMZ - no injection allowed
5402 offset = ztest_random(fsize / (leaves << bshift)) *
5403 (leaves << bshift) + (leaf << bshift) +
5404 (ztest_random(1ULL << (bshift - 1)) & -8ULL);
5406 if (offset >= fsize)
5409 mutex_enter(&ztest_vdev_lock);
5410 if (mirror_save != zs->zs_mirrors) {
5411 mutex_exit(&ztest_vdev_lock);
5416 if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
5417 fatal(1, "can't inject bad word at 0x%llx in %s",
5420 mutex_exit(&ztest_vdev_lock);
5422 if (ztest_opts.zo_verbose >= 7)
5423 (void) printf("injected bad word into %s,"
5424 " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
5429 umem_free(path0, MAXPATHLEN);
5430 umem_free(pathrand, MAXPATHLEN);
5434 * Verify that DDT repair works as expected.
5437 ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
5439 ztest_shared_t *zs = ztest_shared;
5440 spa_t *spa = ztest_spa;
5441 objset_t *os = zd->zd_os;
5443 uint64_t object, blocksize, txg, pattern, psize;
5444 enum zio_checksum checksum = spa_dedup_checksum(spa);
5449 int copies = 2 * ZIO_DEDUPDITTO_MIN;
5452 blocksize = ztest_random_blocksize();
5453 blocksize = MIN(blocksize, 2048); /* because we write so many */
5455 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
5456 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0, 0);
5458 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
5459 umem_free(od, sizeof (ztest_od_t));
5464 * Take the name lock as writer to prevent anyone else from changing
5465 * the pool and dataset properies we need to maintain during this test.
5467 (void) rw_wrlock(&ztest_name_lock);
5469 if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
5471 ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
5473 (void) rw_unlock(&ztest_name_lock);
5474 umem_free(od, sizeof (ztest_od_t));
5478 object = od[0].od_object;
5479 blocksize = od[0].od_blocksize;
5480 pattern = zs->zs_guid ^ dmu_objset_fsid_guid(os);
5482 ASSERT(object != 0);
5484 tx = dmu_tx_create(os);
5485 dmu_tx_hold_write(tx, object, 0, copies * blocksize);
5486 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
5488 (void) rw_unlock(&ztest_name_lock);
5489 umem_free(od, sizeof (ztest_od_t));
5494 * Write all the copies of our block.
5496 for (i = 0; i < copies; i++) {
5497 uint64_t offset = i * blocksize;
5498 int error = dmu_buf_hold(os, object, offset, FTAG, &db,
5499 DMU_READ_NO_PREFETCH);
5501 fatal(B_FALSE, "dmu_buf_hold(%p, %llu, %llu) = %u",
5502 os, (long long)object, (long long) offset, error);
5504 ASSERT(db->db_offset == offset);
5505 ASSERT(db->db_size == blocksize);
5506 ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
5507 ztest_pattern_match(db->db_data, db->db_size, 0ULL));
5508 dmu_buf_will_fill(db, tx);
5509 ztest_pattern_set(db->db_data, db->db_size, pattern);
5510 dmu_buf_rele(db, FTAG);
5514 txg_wait_synced(spa_get_dsl(spa), txg);
5517 * Find out what block we got.
5519 VERIFY0(dmu_buf_hold(os, object, 0, FTAG, &db,
5520 DMU_READ_NO_PREFETCH));
5521 blk = *((dmu_buf_impl_t *)db)->db_blkptr;
5522 dmu_buf_rele(db, FTAG);
5525 * Damage the block. Dedup-ditto will save us when we read it later.
5527 psize = BP_GET_PSIZE(&blk);
5528 buf = zio_buf_alloc(psize);
5529 ztest_pattern_set(buf, psize, ~pattern);
5531 (void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
5532 buf, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
5533 ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
5535 zio_buf_free(buf, psize);
5537 (void) rw_unlock(&ztest_name_lock);
5538 umem_free(od, sizeof (ztest_od_t));
5546 ztest_scrub(ztest_ds_t *zd, uint64_t id)
5548 spa_t *spa = ztest_spa;
5550 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5551 (void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
5552 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5556 * Change the guid for the pool.
5560 ztest_reguid(ztest_ds_t *zd, uint64_t id)
5562 spa_t *spa = ztest_spa;
5563 uint64_t orig, load;
5566 orig = spa_guid(spa);
5567 load = spa_load_guid(spa);
5569 (void) rw_wrlock(&ztest_name_lock);
5570 error = spa_change_guid(spa);
5571 (void) rw_unlock(&ztest_name_lock);
5576 if (ztest_opts.zo_verbose >= 4) {
5577 (void) printf("Changed guid old %llu -> %llu\n",
5578 (u_longlong_t)orig, (u_longlong_t)spa_guid(spa));
5581 VERIFY3U(orig, !=, spa_guid(spa));
5582 VERIFY3U(load, ==, spa_load_guid(spa));
5586 * Rename the pool to a different name and then rename it back.
5590 ztest_spa_rename(ztest_ds_t *zd, uint64_t id)
5592 char *oldname, *newname;
5595 (void) rw_wrlock(&ztest_name_lock);
5597 oldname = ztest_opts.zo_pool;
5598 newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
5599 (void) strcpy(newname, oldname);
5600 (void) strcat(newname, "_tmp");
5605 VERIFY3U(0, ==, spa_rename(oldname, newname));
5608 * Try to open it under the old name, which shouldn't exist
5610 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5613 * Open it under the new name and make sure it's still the same spa_t.
5615 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
5617 ASSERT(spa == ztest_spa);
5618 spa_close(spa, FTAG);
5621 * Rename it back to the original
5623 VERIFY3U(0, ==, spa_rename(newname, oldname));
5626 * Make sure it can still be opened
5628 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
5630 ASSERT(spa == ztest_spa);
5631 spa_close(spa, FTAG);
5633 umem_free(newname, strlen(newname) + 1);
5635 (void) rw_unlock(&ztest_name_lock);
5639 ztest_fletcher(ztest_ds_t *zd, uint64_t id)
5641 hrtime_t end = gethrtime() + NANOSEC;
5643 while (gethrtime() <= end) {
5644 int run_count = 100;
5650 zio_cksum_t zc_ref_byteswap;
5652 size = ztest_random_blocksize();
5653 buf = umem_alloc(size, UMEM_NOFAIL);
5655 for (i = 0, ptr = buf; i < size / sizeof (*ptr); i++, ptr++)
5656 *ptr = ztest_random(UINT_MAX);
5658 VERIFY0(fletcher_4_impl_set("scalar"));
5659 fletcher_4_native(buf, size, NULL, &zc_ref);
5660 fletcher_4_byteswap(buf, size, NULL, &zc_ref_byteswap);
5662 VERIFY0(fletcher_4_impl_set("cycle"));
5663 while (run_count-- > 0) {
5665 zio_cksum_t zc_byteswap;
5667 fletcher_4_byteswap(buf, size, NULL, &zc_byteswap);
5668 fletcher_4_native(buf, size, NULL, &zc);
5670 VERIFY0(bcmp(&zc, &zc_ref, sizeof (zc)));
5671 VERIFY0(bcmp(&zc_byteswap, &zc_ref_byteswap,
5672 sizeof (zc_byteswap)));
5675 umem_free(buf, size);
5680 ztest_fletcher_incr(ztest_ds_t *zd, uint64_t id)
5687 zio_cksum_t zc_ref_bswap;
5689 hrtime_t end = gethrtime() + NANOSEC;
5691 while (gethrtime() <= end) {
5692 int run_count = 100;
5694 size = ztest_random_blocksize();
5695 buf = umem_alloc(size, UMEM_NOFAIL);
5697 for (i = 0, ptr = buf; i < size / sizeof (*ptr); i++, ptr++)
5698 *ptr = ztest_random(UINT_MAX);
5700 VERIFY0(fletcher_4_impl_set("scalar"));
5701 fletcher_4_native(buf, size, NULL, &zc_ref);
5702 fletcher_4_byteswap(buf, size, NULL, &zc_ref_bswap);
5704 VERIFY0(fletcher_4_impl_set("cycle"));
5706 while (run_count-- > 0) {
5708 zio_cksum_t zc_bswap;
5711 ZIO_SET_CHECKSUM(&zc, 0, 0, 0, 0);
5712 ZIO_SET_CHECKSUM(&zc_bswap, 0, 0, 0, 0);
5714 while (pos < size) {
5715 size_t inc = 64 * ztest_random(size / 67);
5716 /* sometimes add few bytes to test non-simd */
5717 if (ztest_random(100) < 10)
5718 inc += P2ALIGN(ztest_random(64),
5721 if (inc > (size - pos))
5724 fletcher_4_incremental_native(buf + pos, inc,
5726 fletcher_4_incremental_byteswap(buf + pos, inc,
5732 VERIFY3U(pos, ==, size);
5734 VERIFY(ZIO_CHECKSUM_EQUAL(zc, zc_ref));
5735 VERIFY(ZIO_CHECKSUM_EQUAL(zc_bswap, zc_ref_bswap));
5738 * verify if incremental on the whole buffer is
5739 * equivalent to non-incremental version
5741 ZIO_SET_CHECKSUM(&zc, 0, 0, 0, 0);
5742 ZIO_SET_CHECKSUM(&zc_bswap, 0, 0, 0, 0);
5744 fletcher_4_incremental_native(buf, size, &zc);
5745 fletcher_4_incremental_byteswap(buf, size, &zc_bswap);
5747 VERIFY(ZIO_CHECKSUM_EQUAL(zc, zc_ref));
5748 VERIFY(ZIO_CHECKSUM_EQUAL(zc_bswap, zc_ref_bswap));
5751 umem_free(buf, size);
5756 ztest_check_path(char *path)
5759 /* return true on success */
5760 return (!stat(path, &s));
5764 ztest_get_zdb_bin(char *bin, int len)
5768 * Try to use ZDB_PATH and in-tree zdb path. If not successful, just
5769 * let popen to search through PATH.
5771 if ((zdb_path = getenv("ZDB_PATH"))) {
5772 strlcpy(bin, zdb_path, len); /* In env */
5773 if (!ztest_check_path(bin)) {
5774 ztest_dump_core = 0;
5775 fatal(1, "invalid ZDB_PATH '%s'", bin);
5780 VERIFY(realpath(getexecname(), bin) != NULL);
5781 if (strstr(bin, "/ztest/")) {
5782 strstr(bin, "/ztest/")[0] = '\0'; /* In-tree */
5783 strcat(bin, "/zdb/zdb");
5784 if (ztest_check_path(bin))
5791 * Verify pool integrity by running zdb.
5794 ztest_run_zdb(char *pool)
5800 const int len = MAXPATHLEN + MAXNAMELEN + 20;
5803 bin = umem_alloc(len, UMEM_NOFAIL);
5804 zdb = umem_alloc(len, UMEM_NOFAIL);
5805 zbuf = umem_alloc(1024, UMEM_NOFAIL);
5807 ztest_get_zdb_bin(bin, len);
5810 "%s -bcc%s%s -d -U %s %s",
5812 ztest_opts.zo_verbose >= 3 ? "s" : "",
5813 ztest_opts.zo_verbose >= 4 ? "v" : "",
5817 if (ztest_opts.zo_verbose >= 5)
5818 (void) printf("Executing %s\n", strstr(zdb, "zdb "));
5820 fp = popen(zdb, "r");
5822 while (fgets(zbuf, 1024, fp) != NULL)
5823 if (ztest_opts.zo_verbose >= 3)
5824 (void) printf("%s", zbuf);
5826 status = pclose(fp);
5831 ztest_dump_core = 0;
5832 if (WIFEXITED(status))
5833 fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
5835 fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
5837 umem_free(bin, len);
5838 umem_free(zdb, len);
5839 umem_free(zbuf, 1024);
5843 ztest_walk_pool_directory(char *header)
5847 if (ztest_opts.zo_verbose >= 6)
5848 (void) printf("%s\n", header);
5850 mutex_enter(&spa_namespace_lock);
5851 while ((spa = spa_next(spa)) != NULL)
5852 if (ztest_opts.zo_verbose >= 6)
5853 (void) printf("\t%s\n", spa_name(spa));
5854 mutex_exit(&spa_namespace_lock);
5858 ztest_spa_import_export(char *oldname, char *newname)
5860 nvlist_t *config, *newconfig;
5865 if (ztest_opts.zo_verbose >= 4) {
5866 (void) printf("import/export: old = %s, new = %s\n",
5871 * Clean up from previous runs.
5873 (void) spa_destroy(newname);
5876 * Get the pool's configuration and guid.
5878 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
5881 * Kick off a scrub to tickle scrub/export races.
5883 if (ztest_random(2) == 0)
5884 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5886 pool_guid = spa_guid(spa);
5887 spa_close(spa, FTAG);
5889 ztest_walk_pool_directory("pools before export");
5894 VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
5896 ztest_walk_pool_directory("pools after export");
5901 newconfig = spa_tryimport(config);
5902 ASSERT(newconfig != NULL);
5903 nvlist_free(newconfig);
5906 * Import it under the new name.
5908 error = spa_import(newname, config, NULL, 0);
5910 dump_nvlist(config, 0);
5911 fatal(B_FALSE, "couldn't import pool %s as %s: error %u",
5912 oldname, newname, error);
5915 ztest_walk_pool_directory("pools after import");
5918 * Try to import it again -- should fail with EEXIST.
5920 VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL, 0));
5923 * Try to import it under a different name -- should fail with EEXIST.
5925 VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL, 0));
5928 * Verify that the pool is no longer visible under the old name.
5930 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5933 * Verify that we can open and close the pool using the new name.
5935 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
5936 ASSERT(pool_guid == spa_guid(spa));
5937 spa_close(spa, FTAG);
5939 nvlist_free(config);
5943 ztest_resume(spa_t *spa)
5945 if (spa_suspended(spa) && ztest_opts.zo_verbose >= 6)
5946 (void) printf("resuming from suspended state\n");
5947 spa_vdev_state_enter(spa, SCL_NONE);
5948 vdev_clear(spa, NULL);
5949 (void) spa_vdev_state_exit(spa, NULL, 0);
5950 (void) zio_resume(spa);
5954 ztest_resume_thread(void *arg)
5958 while (!ztest_exiting) {
5959 if (spa_suspended(spa))
5961 (void) poll(NULL, 0, 100);
5964 * Periodically change the zfs_compressed_arc_enabled setting.
5966 if (ztest_random(10) == 0)
5967 zfs_compressed_arc_enabled = ztest_random(2);
5979 ztest_deadman_alarm(int sig)
5981 fatal(0, "failed to complete within %d seconds of deadline", GRACE);
5986 ztest_execute(int test, ztest_info_t *zi, uint64_t id)
5988 ztest_ds_t *zd = &ztest_ds[id % ztest_opts.zo_datasets];
5989 ztest_shared_callstate_t *zc = ZTEST_GET_SHARED_CALLSTATE(test);
5990 hrtime_t functime = gethrtime();
5993 for (i = 0; i < zi->zi_iters; i++)
5994 zi->zi_func(zd, id);
5996 functime = gethrtime() - functime;
5998 atomic_add_64(&zc->zc_count, 1);
5999 atomic_add_64(&zc->zc_time, functime);
6001 if (ztest_opts.zo_verbose >= 4)
6002 (void) printf("%6.2f sec in %s\n",
6003 (double)functime / NANOSEC, zi->zi_funcname);
6007 ztest_thread(void *arg)
6010 uint64_t id = (uintptr_t)arg;
6011 ztest_shared_t *zs = ztest_shared;
6015 ztest_shared_callstate_t *zc;
6017 while ((now = gethrtime()) < zs->zs_thread_stop) {
6019 * See if it's time to force a crash.
6021 if (now > zs->zs_thread_kill)
6025 * If we're getting ENOSPC with some regularity, stop.
6027 if (zs->zs_enospc_count > 10)
6031 * Pick a random function to execute.
6033 rand = ztest_random(ZTEST_FUNCS);
6034 zi = &ztest_info[rand];
6035 zc = ZTEST_GET_SHARED_CALLSTATE(rand);
6036 call_next = zc->zc_next;
6038 if (now >= call_next &&
6039 atomic_cas_64(&zc->zc_next, call_next, call_next +
6040 ztest_random(2 * zi->zi_interval[0] + 1)) == call_next) {
6041 ztest_execute(rand, zi, id);
6051 ztest_dataset_name(char *dsname, char *pool, int d)
6053 (void) snprintf(dsname, ZFS_MAX_DATASET_NAME_LEN, "%s/ds_%d", pool, d);
6057 ztest_dataset_destroy(int d)
6059 char name[ZFS_MAX_DATASET_NAME_LEN];
6062 ztest_dataset_name(name, ztest_opts.zo_pool, d);
6064 if (ztest_opts.zo_verbose >= 3)
6065 (void) printf("Destroying %s to free up space\n", name);
6068 * Cleanup any non-standard clones and snapshots. In general,
6069 * ztest thread t operates on dataset (t % zopt_datasets),
6070 * so there may be more than one thing to clean up.
6072 for (t = d; t < ztest_opts.zo_threads;
6073 t += ztest_opts.zo_datasets)
6074 ztest_dsl_dataset_cleanup(name, t);
6076 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
6077 DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
6081 ztest_dataset_dirobj_verify(ztest_ds_t *zd)
6083 uint64_t usedobjs, dirobjs, scratch;
6086 * ZTEST_DIROBJ is the object directory for the entire dataset.
6087 * Therefore, the number of objects in use should equal the
6088 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
6089 * If not, we have an object leak.
6091 * Note that we can only check this in ztest_dataset_open(),
6092 * when the open-context and syncing-context values agree.
6093 * That's because zap_count() returns the open-context value,
6094 * while dmu_objset_space() returns the rootbp fill count.
6096 VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
6097 dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
6098 ASSERT3U(dirobjs + 1, ==, usedobjs);
6102 ztest_dataset_open(int d)
6104 ztest_ds_t *zd = &ztest_ds[d];
6105 uint64_t committed_seq = ZTEST_GET_SHARED_DS(d)->zd_seq;
6108 char name[ZFS_MAX_DATASET_NAME_LEN];
6111 ztest_dataset_name(name, ztest_opts.zo_pool, d);
6113 (void) rw_rdlock(&ztest_name_lock);
6115 error = ztest_dataset_create(name);
6116 if (error == ENOSPC) {
6117 (void) rw_unlock(&ztest_name_lock);
6118 ztest_record_enospc(FTAG);
6121 ASSERT(error == 0 || error == EEXIST);
6123 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, zd, &os));
6124 (void) rw_unlock(&ztest_name_lock);
6126 ztest_zd_init(zd, ZTEST_GET_SHARED_DS(d), os);
6128 zilog = zd->zd_zilog;
6130 if (zilog->zl_header->zh_claim_lr_seq != 0 &&
6131 zilog->zl_header->zh_claim_lr_seq < committed_seq)
6132 fatal(0, "missing log records: claimed %llu < committed %llu",
6133 zilog->zl_header->zh_claim_lr_seq, committed_seq);
6135 ztest_dataset_dirobj_verify(zd);
6137 zil_replay(os, zd, ztest_replay_vector);
6139 ztest_dataset_dirobj_verify(zd);
6141 if (ztest_opts.zo_verbose >= 6)
6142 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
6144 (u_longlong_t)zilog->zl_parse_blk_count,
6145 (u_longlong_t)zilog->zl_parse_lr_count,
6146 (u_longlong_t)zilog->zl_replaying_seq);
6148 zilog = zil_open(os, ztest_get_data);
6150 if (zilog->zl_replaying_seq != 0 &&
6151 zilog->zl_replaying_seq < committed_seq)
6152 fatal(0, "missing log records: replayed %llu < committed %llu",
6153 zilog->zl_replaying_seq, committed_seq);
6159 ztest_dataset_close(int d)
6161 ztest_ds_t *zd = &ztest_ds[d];
6163 zil_close(zd->zd_zilog);
6164 dmu_objset_disown(zd->zd_os, zd);
6170 * Kick off threads to run tests on all datasets in parallel.
6173 ztest_run(ztest_shared_t *zs)
6178 kthread_t *resume_thread;
6183 ztest_exiting = B_FALSE;
6186 * Initialize parent/child shared state.
6188 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
6189 VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0);
6191 zs->zs_thread_start = gethrtime();
6192 zs->zs_thread_stop =
6193 zs->zs_thread_start + ztest_opts.zo_passtime * NANOSEC;
6194 zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
6195 zs->zs_thread_kill = zs->zs_thread_stop;
6196 if (ztest_random(100) < ztest_opts.zo_killrate) {
6197 zs->zs_thread_kill -=
6198 ztest_random(ztest_opts.zo_passtime * NANOSEC);
6201 mutex_init(&zcl.zcl_callbacks_lock, NULL, MUTEX_DEFAULT, NULL);
6203 list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
6204 offsetof(ztest_cb_data_t, zcd_node));
6209 kernel_init(FREAD | FWRITE);
6210 VERIFY0(spa_open(ztest_opts.zo_pool, &spa, FTAG));
6211 spa->spa_debug = B_TRUE;
6212 metaslab_preload_limit = ztest_random(20) + 1;
6215 VERIFY0(dmu_objset_own(ztest_opts.zo_pool,
6216 DMU_OST_ANY, B_TRUE, FTAG, &os));
6217 zs->zs_guid = dmu_objset_fsid_guid(os);
6218 dmu_objset_disown(os, FTAG);
6220 spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
6223 * We don't expect the pool to suspend unless maxfaults == 0,
6224 * in which case ztest_fault_inject() temporarily takes away
6225 * the only valid replica.
6227 if (MAXFAULTS() == 0)
6228 spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
6230 spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
6233 * Create a thread to periodically resume suspended I/O.
6235 VERIFY3P((resume_thread = zk_thread_create(NULL, 0,
6236 (thread_func_t)ztest_resume_thread, spa, TS_RUN, NULL, 0, 0,
6237 PTHREAD_CREATE_JOINABLE)), !=, NULL);
6241 * Set a deadman alarm to abort() if we hang.
6243 signal(SIGALRM, ztest_deadman_alarm);
6244 alarm((zs->zs_thread_stop - zs->zs_thread_start) / NANOSEC + GRACE);
6248 * Verify that we can safely inquire about about any object,
6249 * whether it's allocated or not. To make it interesting,
6250 * we probe a 5-wide window around each power of two.
6251 * This hits all edge cases, including zero and the max.
6253 for (t = 0; t < 64; t++) {
6254 for (d = -5; d <= 5; d++) {
6255 error = dmu_object_info(spa->spa_meta_objset,
6256 (1ULL << t) + d, NULL);
6257 ASSERT(error == 0 || error == ENOENT ||
6263 * If we got any ENOSPC errors on the previous run, destroy something.
6265 if (zs->zs_enospc_count != 0) {
6266 int d = ztest_random(ztest_opts.zo_datasets);
6267 ztest_dataset_destroy(d);
6269 zs->zs_enospc_count = 0;
6271 tid = umem_zalloc(ztest_opts.zo_threads * sizeof (kt_did_t),
6274 if (ztest_opts.zo_verbose >= 4)
6275 (void) printf("starting main threads...\n");
6278 * Kick off all the tests that run in parallel.
6280 for (t = 0; t < ztest_opts.zo_threads; t++) {
6283 if (t < ztest_opts.zo_datasets &&
6284 ztest_dataset_open(t) != 0)
6287 VERIFY3P(thread = zk_thread_create(NULL, 0,
6288 (thread_func_t)ztest_thread,
6289 (void *)(uintptr_t)t, TS_RUN, NULL, 0, 0,
6290 PTHREAD_CREATE_JOINABLE), !=, NULL);
6291 tid[t] = thread->t_tid;
6295 * Wait for all of the tests to complete. We go in reverse order
6296 * so we don't close datasets while threads are still using them.
6298 for (t = ztest_opts.zo_threads - 1; t >= 0; t--) {
6299 thread_join(tid[t]);
6300 if (t < ztest_opts.zo_datasets)
6301 ztest_dataset_close(t);
6304 txg_wait_synced(spa_get_dsl(spa), 0);
6306 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
6307 zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
6309 umem_free(tid, ztest_opts.zo_threads * sizeof (kt_did_t));
6311 /* Kill the resume thread */
6312 ztest_exiting = B_TRUE;
6313 thread_join(resume_thread->t_tid);
6317 * Right before closing the pool, kick off a bunch of async I/O;
6318 * spa_close() should wait for it to complete.
6320 for (object = 1; object < 50; object++) {
6321 dmu_prefetch(spa->spa_meta_objset, object, 0, 0, 1ULL << 20,
6322 ZIO_PRIORITY_SYNC_READ);
6325 /* Verify that at least one commit cb was called in a timely fashion */
6326 if (zc_cb_counter >= ZTEST_COMMIT_CB_MIN_REG)
6327 VERIFY0(zc_min_txg_delay);
6329 spa_close(spa, FTAG);
6332 * Verify that we can loop over all pools.
6334 mutex_enter(&spa_namespace_lock);
6335 for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
6336 if (ztest_opts.zo_verbose > 3)
6337 (void) printf("spa_next: found %s\n", spa_name(spa));
6338 mutex_exit(&spa_namespace_lock);
6341 * Verify that we can export the pool and reimport it under a
6344 if (ztest_random(2) == 0) {
6345 char name[ZFS_MAX_DATASET_NAME_LEN];
6346 (void) snprintf(name, sizeof (name), "%s_import",
6347 ztest_opts.zo_pool);
6348 ztest_spa_import_export(ztest_opts.zo_pool, name);
6349 ztest_spa_import_export(name, ztest_opts.zo_pool);
6354 list_destroy(&zcl.zcl_callbacks);
6355 mutex_destroy(&zcl.zcl_callbacks_lock);
6356 (void) rwlock_destroy(&ztest_name_lock);
6357 mutex_destroy(&ztest_vdev_lock);
6363 ztest_ds_t *zd = &ztest_ds[0];
6367 if (ztest_opts.zo_verbose >= 3)
6368 (void) printf("testing spa_freeze()...\n");
6370 kernel_init(FREAD | FWRITE);
6371 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6372 VERIFY3U(0, ==, ztest_dataset_open(0));
6373 spa->spa_debug = B_TRUE;
6377 * Force the first log block to be transactionally allocated.
6378 * We have to do this before we freeze the pool -- otherwise
6379 * the log chain won't be anchored.
6381 while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
6382 ztest_dmu_object_alloc_free(zd, 0);
6383 zil_commit(zd->zd_zilog, 0);
6386 txg_wait_synced(spa_get_dsl(spa), 0);
6389 * Freeze the pool. This stops spa_sync() from doing anything,
6390 * so that the only way to record changes from now on is the ZIL.
6395 * Because it is hard to predict how much space a write will actually
6396 * require beforehand, we leave ourselves some fudge space to write over
6399 uint64_t capacity = metaslab_class_get_space(spa_normal_class(spa)) / 2;
6402 * Run tests that generate log records but don't alter the pool config
6403 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
6404 * We do a txg_wait_synced() after each iteration to force the txg
6405 * to increase well beyond the last synced value in the uberblock.
6406 * The ZIL should be OK with that.
6408 * Run a random number of times less than zo_maxloops and ensure we do
6409 * not run out of space on the pool.
6411 while (ztest_random(10) != 0 &&
6412 numloops++ < ztest_opts.zo_maxloops &&
6413 metaslab_class_get_alloc(spa_normal_class(spa)) < capacity) {
6415 ztest_od_init(&od, 0, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
6416 VERIFY0(ztest_object_init(zd, &od, sizeof (od), B_FALSE));
6417 ztest_io(zd, od.od_object,
6418 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
6419 txg_wait_synced(spa_get_dsl(spa), 0);
6423 * Commit all of the changes we just generated.
6425 zil_commit(zd->zd_zilog, 0);
6426 txg_wait_synced(spa_get_dsl(spa), 0);
6429 * Close our dataset and close the pool.
6431 ztest_dataset_close(0);
6432 spa_close(spa, FTAG);
6436 * Open and close the pool and dataset to induce log replay.
6438 kernel_init(FREAD | FWRITE);
6439 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6440 ASSERT(spa_freeze_txg(spa) == UINT64_MAX);
6441 VERIFY3U(0, ==, ztest_dataset_open(0));
6442 ztest_dataset_close(0);
6444 spa->spa_debug = B_TRUE;
6446 txg_wait_synced(spa_get_dsl(spa), 0);
6447 ztest_reguid(NULL, 0);
6449 spa_close(spa, FTAG);
6454 print_time(hrtime_t t, char *timebuf)
6456 hrtime_t s = t / NANOSEC;
6457 hrtime_t m = s / 60;
6458 hrtime_t h = m / 60;
6459 hrtime_t d = h / 24;
6468 (void) sprintf(timebuf,
6469 "%llud%02lluh%02llum%02llus", d, h, m, s);
6471 (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
6473 (void) sprintf(timebuf, "%llum%02llus", m, s);
6475 (void) sprintf(timebuf, "%llus", s);
6479 make_random_props(void)
6483 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
6484 if (ztest_random(2) == 0)
6486 VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
6492 * Create a storage pool with the given name and initial vdev size.
6493 * Then test spa_freeze() functionality.
6496 ztest_init(ztest_shared_t *zs)
6499 nvlist_t *nvroot, *props;
6502 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
6503 VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0);
6505 kernel_init(FREAD | FWRITE);
6508 * Create the storage pool.
6510 (void) spa_destroy(ztest_opts.zo_pool);
6511 ztest_shared->zs_vdev_next_leaf = 0;
6513 zs->zs_mirrors = ztest_opts.zo_mirrors;
6514 nvroot = make_vdev_root(NULL, NULL, NULL, ztest_opts.zo_vdev_size, 0,
6515 0, ztest_opts.zo_raidz, zs->zs_mirrors, 1);
6516 props = make_random_props();
6517 for (i = 0; i < SPA_FEATURES; i++) {
6519 VERIFY3S(-1, !=, asprintf(&buf, "feature@%s",
6520 spa_feature_table[i].fi_uname));
6521 VERIFY3U(0, ==, nvlist_add_uint64(props, buf, 0));
6524 VERIFY3U(0, ==, spa_create(ztest_opts.zo_pool, nvroot, props, NULL));
6525 nvlist_free(nvroot);
6528 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6529 zs->zs_metaslab_sz =
6530 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
6531 spa_close(spa, FTAG);
6535 ztest_run_zdb(ztest_opts.zo_pool);
6539 ztest_run_zdb(ztest_opts.zo_pool);
6541 (void) rwlock_destroy(&ztest_name_lock);
6542 mutex_destroy(&ztest_vdev_lock);
6548 static char ztest_name_data[] = "/tmp/ztest.data.XXXXXX";
6550 ztest_fd_data = mkstemp(ztest_name_data);
6551 ASSERT3S(ztest_fd_data, >=, 0);
6552 (void) unlink(ztest_name_data);
6556 shared_data_size(ztest_shared_hdr_t *hdr)
6560 size = hdr->zh_hdr_size;
6561 size += hdr->zh_opts_size;
6562 size += hdr->zh_size;
6563 size += hdr->zh_stats_size * hdr->zh_stats_count;
6564 size += hdr->zh_ds_size * hdr->zh_ds_count;
6573 ztest_shared_hdr_t *hdr;
6575 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6576 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6577 ASSERT(hdr != MAP_FAILED);
6579 VERIFY3U(0, ==, ftruncate(ztest_fd_data, sizeof (ztest_shared_hdr_t)));
6581 hdr->zh_hdr_size = sizeof (ztest_shared_hdr_t);
6582 hdr->zh_opts_size = sizeof (ztest_shared_opts_t);
6583 hdr->zh_size = sizeof (ztest_shared_t);
6584 hdr->zh_stats_size = sizeof (ztest_shared_callstate_t);
6585 hdr->zh_stats_count = ZTEST_FUNCS;
6586 hdr->zh_ds_size = sizeof (ztest_shared_ds_t);
6587 hdr->zh_ds_count = ztest_opts.zo_datasets;
6589 size = shared_data_size(hdr);
6590 VERIFY3U(0, ==, ftruncate(ztest_fd_data, size));
6592 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6599 ztest_shared_hdr_t *hdr;
6602 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6603 PROT_READ, MAP_SHARED, ztest_fd_data, 0);
6604 ASSERT(hdr != MAP_FAILED);
6606 size = shared_data_size(hdr);
6608 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6609 hdr = ztest_shared_hdr = (void *)mmap(0, P2ROUNDUP(size, getpagesize()),
6610 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6611 ASSERT(hdr != MAP_FAILED);
6612 buf = (uint8_t *)hdr;
6614 offset = hdr->zh_hdr_size;
6615 ztest_shared_opts = (void *)&buf[offset];
6616 offset += hdr->zh_opts_size;
6617 ztest_shared = (void *)&buf[offset];
6618 offset += hdr->zh_size;
6619 ztest_shared_callstate = (void *)&buf[offset];
6620 offset += hdr->zh_stats_size * hdr->zh_stats_count;
6621 ztest_shared_ds = (void *)&buf[offset];
6625 exec_child(char *cmd, char *libpath, boolean_t ignorekill, int *statusp)
6629 char *cmdbuf = NULL;
6634 cmdbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
6635 (void) strlcpy(cmdbuf, getexecname(), MAXPATHLEN);
6640 fatal(1, "fork failed");
6642 if (pid == 0) { /* child */
6643 char *emptyargv[2] = { cmd, NULL };
6644 char fd_data_str[12];
6646 struct rlimit rl = { 1024, 1024 };
6647 (void) setrlimit(RLIMIT_NOFILE, &rl);
6649 (void) close(ztest_fd_rand);
6650 VERIFY(11 >= snprintf(fd_data_str, 12, "%d", ztest_fd_data));
6651 VERIFY(0 == setenv("ZTEST_FD_DATA", fd_data_str, 1));
6653 (void) enable_extended_FILE_stdio(-1, -1);
6654 if (libpath != NULL)
6655 VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath, 1));
6656 (void) execv(cmd, emptyargv);
6657 ztest_dump_core = B_FALSE;
6658 fatal(B_TRUE, "exec failed: %s", cmd);
6661 if (cmdbuf != NULL) {
6662 umem_free(cmdbuf, MAXPATHLEN);
6666 while (waitpid(pid, &status, 0) != pid)
6668 if (statusp != NULL)
6671 if (WIFEXITED(status)) {
6672 if (WEXITSTATUS(status) != 0) {
6673 (void) fprintf(stderr, "child exited with code %d\n",
6674 WEXITSTATUS(status));
6678 } else if (WIFSIGNALED(status)) {
6679 if (!ignorekill || WTERMSIG(status) != SIGKILL) {
6680 (void) fprintf(stderr, "child died with signal %d\n",
6686 (void) fprintf(stderr, "something strange happened to child\n");
6693 ztest_run_init(void)
6697 ztest_shared_t *zs = ztest_shared;
6699 ASSERT(ztest_opts.zo_init != 0);
6702 * Blow away any existing copy of zpool.cache
6704 (void) remove(spa_config_path);
6707 * Create and initialize our storage pool.
6709 for (i = 1; i <= ztest_opts.zo_init; i++) {
6710 bzero(zs, sizeof (ztest_shared_t));
6711 if (ztest_opts.zo_verbose >= 3 &&
6712 ztest_opts.zo_init != 1) {
6713 (void) printf("ztest_init(), pass %d\n", i);
6720 main(int argc, char **argv)
6728 ztest_shared_callstate_t *zc;
6735 char *fd_data_str = getenv("ZTEST_FD_DATA");
6736 struct sigaction action;
6738 (void) setvbuf(stdout, NULL, _IOLBF, 0);
6740 dprintf_setup(&argc, argv);
6742 action.sa_handler = sig_handler;
6743 sigemptyset(&action.sa_mask);
6744 action.sa_flags = 0;
6746 if (sigaction(SIGSEGV, &action, NULL) < 0) {
6747 (void) fprintf(stderr, "ztest: cannot catch SIGSEGV: %s.\n",
6752 if (sigaction(SIGABRT, &action, NULL) < 0) {
6753 (void) fprintf(stderr, "ztest: cannot catch SIGABRT: %s.\n",
6758 ztest_fd_rand = open("/dev/urandom", O_RDONLY);
6759 ASSERT3S(ztest_fd_rand, >=, 0);
6762 process_options(argc, argv);
6767 bcopy(&ztest_opts, ztest_shared_opts,
6768 sizeof (*ztest_shared_opts));
6770 ztest_fd_data = atoi(fd_data_str);
6772 bcopy(ztest_shared_opts, &ztest_opts, sizeof (ztest_opts));
6774 ASSERT3U(ztest_opts.zo_datasets, ==, ztest_shared_hdr->zh_ds_count);
6776 /* Override location of zpool.cache */
6777 VERIFY(asprintf((char **)&spa_config_path, "%s/zpool.cache",
6778 ztest_opts.zo_dir) != -1);
6780 ztest_ds = umem_alloc(ztest_opts.zo_datasets * sizeof (ztest_ds_t),
6785 metaslab_gang_bang = ztest_opts.zo_metaslab_gang_bang;
6786 metaslab_df_alloc_threshold =
6787 zs->zs_metaslab_df_alloc_threshold;
6796 hasalt = (strlen(ztest_opts.zo_alt_ztest) != 0);
6798 if (ztest_opts.zo_verbose >= 1) {
6799 (void) printf("%llu vdevs, %d datasets, %d threads,"
6800 " %llu seconds...\n",
6801 (u_longlong_t)ztest_opts.zo_vdevs,
6802 ztest_opts.zo_datasets,
6803 ztest_opts.zo_threads,
6804 (u_longlong_t)ztest_opts.zo_time);
6807 cmd = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
6808 (void) strlcpy(cmd, getexecname(), MAXNAMELEN);
6810 zs->zs_do_init = B_TRUE;
6811 if (strlen(ztest_opts.zo_alt_ztest) != 0) {
6812 if (ztest_opts.zo_verbose >= 1) {
6813 (void) printf("Executing older ztest for "
6814 "initialization: %s\n", ztest_opts.zo_alt_ztest);
6816 VERIFY(!exec_child(ztest_opts.zo_alt_ztest,
6817 ztest_opts.zo_alt_libpath, B_FALSE, NULL));
6819 VERIFY(!exec_child(NULL, NULL, B_FALSE, NULL));
6821 zs->zs_do_init = B_FALSE;
6823 zs->zs_proc_start = gethrtime();
6824 zs->zs_proc_stop = zs->zs_proc_start + ztest_opts.zo_time * NANOSEC;
6826 for (f = 0; f < ZTEST_FUNCS; f++) {
6827 zi = &ztest_info[f];
6828 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6829 if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
6830 zc->zc_next = UINT64_MAX;
6832 zc->zc_next = zs->zs_proc_start +
6833 ztest_random(2 * zi->zi_interval[0] + 1);
6837 * Run the tests in a loop. These tests include fault injection
6838 * to verify that self-healing data works, and forced crashes
6839 * to verify that we never lose on-disk consistency.
6841 while (gethrtime() < zs->zs_proc_stop) {
6846 * Initialize the workload counters for each function.
6848 for (f = 0; f < ZTEST_FUNCS; f++) {
6849 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6854 /* Set the allocation switch size */
6855 zs->zs_metaslab_df_alloc_threshold =
6856 ztest_random(zs->zs_metaslab_sz / 4) + 1;
6858 if (!hasalt || ztest_random(2) == 0) {
6859 if (hasalt && ztest_opts.zo_verbose >= 1) {
6860 (void) printf("Executing newer ztest: %s\n",
6864 killed = exec_child(cmd, NULL, B_TRUE, &status);
6866 if (hasalt && ztest_opts.zo_verbose >= 1) {
6867 (void) printf("Executing older ztest: %s\n",
6868 ztest_opts.zo_alt_ztest);
6871 killed = exec_child(ztest_opts.zo_alt_ztest,
6872 ztest_opts.zo_alt_libpath, B_TRUE, &status);
6879 if (ztest_opts.zo_verbose >= 1) {
6880 hrtime_t now = gethrtime();
6882 now = MIN(now, zs->zs_proc_stop);
6883 print_time(zs->zs_proc_stop - now, timebuf);
6884 nicenum(zs->zs_space, numbuf);
6886 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
6887 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
6889 WIFEXITED(status) ? "Complete" : "SIGKILL",
6890 (u_longlong_t)zs->zs_enospc_count,
6891 100.0 * zs->zs_alloc / zs->zs_space,
6893 100.0 * (now - zs->zs_proc_start) /
6894 (ztest_opts.zo_time * NANOSEC), timebuf);
6897 if (ztest_opts.zo_verbose >= 2) {
6898 (void) printf("\nWorkload summary:\n\n");
6899 (void) printf("%7s %9s %s\n",
6900 "Calls", "Time", "Function");
6901 (void) printf("%7s %9s %s\n",
6902 "-----", "----", "--------");
6903 for (f = 0; f < ZTEST_FUNCS; f++) {
6904 zi = &ztest_info[f];
6905 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6906 print_time(zc->zc_time, timebuf);
6907 (void) printf("%7llu %9s %s\n",
6908 (u_longlong_t)zc->zc_count, timebuf,
6911 (void) printf("\n");
6915 * It's possible that we killed a child during a rename test,
6916 * in which case we'll have a 'ztest_tmp' pool lying around
6917 * instead of 'ztest'. Do a blind rename in case this happened.
6920 if (spa_open(ztest_opts.zo_pool, &spa, FTAG) == 0) {
6921 spa_close(spa, FTAG);
6923 char tmpname[ZFS_MAX_DATASET_NAME_LEN];
6925 kernel_init(FREAD | FWRITE);
6926 (void) snprintf(tmpname, sizeof (tmpname), "%s_tmp",
6927 ztest_opts.zo_pool);
6928 (void) spa_rename(tmpname, ztest_opts.zo_pool);
6932 ztest_run_zdb(ztest_opts.zo_pool);
6935 if (ztest_opts.zo_verbose >= 1) {
6937 (void) printf("%d runs of older ztest: %s\n", older,
6938 ztest_opts.zo_alt_ztest);
6939 (void) printf("%d runs of newer ztest: %s\n", newer,
6942 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
6943 kills, iters - kills, (100.0 * kills) / MAX(1, iters));
6946 umem_free(cmd, MAXNAMELEN);