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;
196 static ztest_shared_opts_t *ztest_shared_opts;
197 static ztest_shared_opts_t ztest_opts;
199 typedef struct ztest_shared_ds {
203 static ztest_shared_ds_t *ztest_shared_ds;
204 #define ZTEST_GET_SHARED_DS(d) (&ztest_shared_ds[d])
206 #define BT_MAGIC 0x123456789abcdefULL
207 #define MAXFAULTS() \
208 (MAX(zs->zs_mirrors, 1) * (ztest_opts.zo_raidz_parity + 1) - 1)
212 ZTEST_IO_WRITE_PATTERN,
213 ZTEST_IO_WRITE_ZEROES,
220 typedef struct ztest_block_tag {
224 uint64_t bt_dnodesize;
231 typedef struct bufwad {
249 #define ZTEST_RANGE_LOCKS 64
250 #define ZTEST_OBJECT_LOCKS 64
253 * Object descriptor. Used as a template for object lookup/create/remove.
255 typedef struct ztest_od {
258 dmu_object_type_t od_type;
259 dmu_object_type_t od_crtype;
260 uint64_t od_blocksize;
261 uint64_t od_crblocksize;
262 uint64_t od_crdnodesize;
265 char od_name[ZFS_MAX_DATASET_NAME_LEN];
271 typedef struct ztest_ds {
272 ztest_shared_ds_t *zd_shared;
274 rwlock_t zd_zilog_lock;
276 ztest_od_t *zd_od; /* debugging aid */
277 char zd_name[ZFS_MAX_DATASET_NAME_LEN];
278 kmutex_t zd_dirobj_lock;
279 rll_t zd_object_lock[ZTEST_OBJECT_LOCKS];
280 zll_t zd_range_lock[ZTEST_RANGE_LOCKS];
284 * Per-iteration state.
286 typedef void ztest_func_t(ztest_ds_t *zd, uint64_t id);
288 typedef struct ztest_info {
289 ztest_func_t *zi_func; /* test function */
290 uint64_t zi_iters; /* iterations per execution */
291 uint64_t *zi_interval; /* execute every <interval> seconds */
292 const char *zi_funcname; /* name of test function */
295 typedef struct ztest_shared_callstate {
296 uint64_t zc_count; /* per-pass count */
297 uint64_t zc_time; /* per-pass time */
298 uint64_t zc_next; /* next time to call this function */
299 } ztest_shared_callstate_t;
301 static ztest_shared_callstate_t *ztest_shared_callstate;
302 #define ZTEST_GET_SHARED_CALLSTATE(c) (&ztest_shared_callstate[c])
304 ztest_func_t ztest_dmu_read_write;
305 ztest_func_t ztest_dmu_write_parallel;
306 ztest_func_t ztest_dmu_object_alloc_free;
307 ztest_func_t ztest_dmu_commit_callbacks;
308 ztest_func_t ztest_zap;
309 ztest_func_t ztest_zap_parallel;
310 ztest_func_t ztest_zil_commit;
311 ztest_func_t ztest_zil_remount;
312 ztest_func_t ztest_dmu_read_write_zcopy;
313 ztest_func_t ztest_dmu_objset_create_destroy;
314 ztest_func_t ztest_dmu_prealloc;
315 ztest_func_t ztest_fzap;
316 ztest_func_t ztest_dmu_snapshot_create_destroy;
317 ztest_func_t ztest_dsl_prop_get_set;
318 ztest_func_t ztest_spa_prop_get_set;
319 ztest_func_t ztest_spa_create_destroy;
320 ztest_func_t ztest_fault_inject;
321 ztest_func_t ztest_ddt_repair;
322 ztest_func_t ztest_dmu_snapshot_hold;
323 ztest_func_t ztest_spa_rename;
324 ztest_func_t ztest_scrub;
325 ztest_func_t ztest_dsl_dataset_promote_busy;
326 ztest_func_t ztest_vdev_attach_detach;
327 ztest_func_t ztest_vdev_LUN_growth;
328 ztest_func_t ztest_vdev_add_remove;
329 ztest_func_t ztest_vdev_aux_add_remove;
330 ztest_func_t ztest_split_pool;
331 ztest_func_t ztest_reguid;
332 ztest_func_t ztest_spa_upgrade;
333 ztest_func_t ztest_fletcher;
334 ztest_func_t ztest_verify_dnode_bt;
336 uint64_t zopt_always = 0ULL * NANOSEC; /* all the time */
337 uint64_t zopt_incessant = 1ULL * NANOSEC / 10; /* every 1/10 second */
338 uint64_t zopt_often = 1ULL * NANOSEC; /* every second */
339 uint64_t zopt_sometimes = 10ULL * NANOSEC; /* every 10 seconds */
340 uint64_t zopt_rarely = 60ULL * NANOSEC; /* every 60 seconds */
342 #define ZTI_INIT(func, iters, interval) \
343 { .zi_func = (func), \
344 .zi_iters = (iters), \
345 .zi_interval = (interval), \
346 .zi_funcname = # func }
348 ztest_info_t ztest_info[] = {
349 ZTI_INIT(ztest_dmu_read_write, 1, &zopt_always),
350 ZTI_INIT(ztest_dmu_write_parallel, 10, &zopt_always),
351 ZTI_INIT(ztest_dmu_object_alloc_free, 1, &zopt_always),
352 ZTI_INIT(ztest_dmu_commit_callbacks, 1, &zopt_always),
353 ZTI_INIT(ztest_zap, 30, &zopt_always),
354 ZTI_INIT(ztest_zap_parallel, 100, &zopt_always),
355 ZTI_INIT(ztest_split_pool, 1, &zopt_always),
356 ZTI_INIT(ztest_zil_commit, 1, &zopt_incessant),
357 ZTI_INIT(ztest_zil_remount, 1, &zopt_sometimes),
358 ZTI_INIT(ztest_dmu_read_write_zcopy, 1, &zopt_often),
359 ZTI_INIT(ztest_dmu_objset_create_destroy, 1, &zopt_often),
360 ZTI_INIT(ztest_dsl_prop_get_set, 1, &zopt_often),
361 ZTI_INIT(ztest_spa_prop_get_set, 1, &zopt_sometimes),
363 ZTI_INIT(ztest_dmu_prealloc, 1, &zopt_sometimes),
365 ZTI_INIT(ztest_fzap, 1, &zopt_sometimes),
366 ZTI_INIT(ztest_dmu_snapshot_create_destroy, 1, &zopt_sometimes),
367 ZTI_INIT(ztest_spa_create_destroy, 1, &zopt_sometimes),
368 ZTI_INIT(ztest_fault_inject, 1, &zopt_sometimes),
369 ZTI_INIT(ztest_ddt_repair, 1, &zopt_sometimes),
370 ZTI_INIT(ztest_dmu_snapshot_hold, 1, &zopt_sometimes),
371 ZTI_INIT(ztest_reguid, 1, &zopt_rarely),
372 ZTI_INIT(ztest_spa_rename, 1, &zopt_rarely),
373 ZTI_INIT(ztest_scrub, 1, &zopt_rarely),
374 ZTI_INIT(ztest_spa_upgrade, 1, &zopt_rarely),
375 ZTI_INIT(ztest_dsl_dataset_promote_busy, 1, &zopt_rarely),
376 ZTI_INIT(ztest_vdev_attach_detach, 1, &zopt_sometimes),
377 ZTI_INIT(ztest_vdev_LUN_growth, 1, &zopt_rarely),
378 ZTI_INIT(ztest_vdev_add_remove, 1, &ztest_opts.zo_vdevtime),
379 ZTI_INIT(ztest_vdev_aux_add_remove, 1, &ztest_opts.zo_vdevtime),
380 ZTI_INIT(ztest_fletcher, 1, &zopt_rarely),
381 ZTI_INIT(ztest_verify_dnode_bt, 1, &zopt_sometimes),
384 #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
387 * The following struct is used to hold a list of uncalled commit callbacks.
388 * The callbacks are ordered by txg number.
390 typedef struct ztest_cb_list {
391 kmutex_t zcl_callbacks_lock;
392 list_t zcl_callbacks;
396 * Stuff we need to share writably between parent and child.
398 typedef struct ztest_shared {
399 boolean_t zs_do_init;
400 hrtime_t zs_proc_start;
401 hrtime_t zs_proc_stop;
402 hrtime_t zs_thread_start;
403 hrtime_t zs_thread_stop;
404 hrtime_t zs_thread_kill;
405 uint64_t zs_enospc_count;
406 uint64_t zs_vdev_next_leaf;
407 uint64_t zs_vdev_aux;
412 uint64_t zs_metaslab_sz;
413 uint64_t zs_metaslab_df_alloc_threshold;
417 #define ID_PARALLEL -1ULL
419 static char ztest_dev_template[] = "%s/%s.%llua";
420 static char ztest_aux_template[] = "%s/%s.%s.%llu";
421 ztest_shared_t *ztest_shared;
423 static spa_t *ztest_spa = NULL;
424 static ztest_ds_t *ztest_ds;
426 static kmutex_t ztest_vdev_lock;
429 * The ztest_name_lock protects the pool and dataset namespace used by
430 * the individual tests. To modify the namespace, consumers must grab
431 * this lock as writer. Grabbing the lock as reader will ensure that the
432 * namespace does not change while the lock is held.
434 static rwlock_t ztest_name_lock;
436 static boolean_t ztest_dump_core = B_TRUE;
437 static boolean_t ztest_exiting;
439 /* Global commit callback list */
440 static ztest_cb_list_t zcl;
441 /* Commit cb delay */
442 static uint64_t zc_min_txg_delay = UINT64_MAX;
443 static int zc_cb_counter = 0;
446 * Minimum number of commit callbacks that need to be registered for us to check
447 * whether the minimum txg delay is acceptable.
449 #define ZTEST_COMMIT_CB_MIN_REG 100
452 * If a number of txgs equal to this threshold have been created after a commit
453 * callback has been registered but not called, then we assume there is an
454 * implementation bug.
456 #define ZTEST_COMMIT_CB_THRESH (TXG_CONCURRENT_STATES + 1000)
458 extern uint64_t metaslab_gang_bang;
459 extern uint64_t metaslab_df_alloc_threshold;
462 ZTEST_META_DNODE = 0,
467 static void usage(boolean_t) __NORETURN;
470 * These libumem hooks provide a reasonable set of defaults for the allocator's
471 * debugging facilities.
474 _umem_debug_init(void)
476 return ("default,verbose"); /* $UMEM_DEBUG setting */
480 _umem_logging_init(void)
482 return ("fail,contents"); /* $UMEM_LOGGING setting */
485 #define BACKTRACE_SZ 100
487 static void sig_handler(int signo)
489 struct sigaction action;
490 #ifdef __GLIBC__ /* backtrace() is a GNU extension */
492 void *buffer[BACKTRACE_SZ];
494 nptrs = backtrace(buffer, BACKTRACE_SZ);
495 backtrace_symbols_fd(buffer, nptrs, STDERR_FILENO);
499 * Restore default action and re-raise signal so SIGSEGV and
500 * SIGABRT can trigger a core dump.
502 action.sa_handler = SIG_DFL;
503 sigemptyset(&action.sa_mask);
505 (void) sigaction(signo, &action, NULL);
509 #define FATAL_MSG_SZ 1024
514 fatal(int do_perror, char *message, ...)
517 int save_errno = errno;
520 (void) fflush(stdout);
521 buf = umem_alloc(FATAL_MSG_SZ, UMEM_NOFAIL);
523 va_start(args, message);
524 (void) sprintf(buf, "ztest: ");
526 (void) vsprintf(buf + strlen(buf), message, args);
529 (void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
530 ": %s", strerror(save_errno));
532 (void) fprintf(stderr, "%s\n", buf);
533 fatal_msg = buf; /* to ease debugging */
540 str2shift(const char *buf)
542 const char *ends = "BKMGTPEZ";
547 for (i = 0; i < strlen(ends); i++) {
548 if (toupper(buf[0]) == ends[i])
551 if (i == strlen(ends)) {
552 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
556 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
559 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
565 nicenumtoull(const char *buf)
570 val = strtoull(buf, &end, 0);
572 (void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
574 } else if (end[0] == '.') {
575 double fval = strtod(buf, &end);
576 fval *= pow(2, str2shift(end));
577 if (fval > UINT64_MAX) {
578 (void) fprintf(stderr, "ztest: value too large: %s\n",
582 val = (uint64_t)fval;
584 int shift = str2shift(end);
585 if (shift >= 64 || (val << shift) >> shift != val) {
586 (void) fprintf(stderr, "ztest: value too large: %s\n",
596 usage(boolean_t requested)
598 const ztest_shared_opts_t *zo = &ztest_opts_defaults;
600 char nice_vdev_size[10];
601 char nice_gang_bang[10];
602 FILE *fp = requested ? stdout : stderr;
604 nicenum(zo->zo_vdev_size, nice_vdev_size);
605 nicenum(zo->zo_metaslab_gang_bang, nice_gang_bang);
607 (void) fprintf(fp, "Usage: %s\n"
608 "\t[-v vdevs (default: %llu)]\n"
609 "\t[-s size_of_each_vdev (default: %s)]\n"
610 "\t[-a alignment_shift (default: %d)] use 0 for random\n"
611 "\t[-m mirror_copies (default: %d)]\n"
612 "\t[-r raidz_disks (default: %d)]\n"
613 "\t[-R raidz_parity (default: %d)]\n"
614 "\t[-d datasets (default: %d)]\n"
615 "\t[-t threads (default: %d)]\n"
616 "\t[-g gang_block_threshold (default: %s)]\n"
617 "\t[-i init_count (default: %d)] initialize pool i times\n"
618 "\t[-k kill_percentage (default: %llu%%)]\n"
619 "\t[-p pool_name (default: %s)]\n"
620 "\t[-f dir (default: %s)] file directory for vdev files\n"
621 "\t[-V] verbose (use multiple times for ever more blather)\n"
622 "\t[-E] use existing pool instead of creating new one\n"
623 "\t[-T time (default: %llu sec)] total run time\n"
624 "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
625 "\t[-P passtime (default: %llu sec)] time per pass\n"
626 "\t[-B alt_ztest (default: <none>)] alternate ztest path\n"
627 "\t[-h] (print help)\n"
630 (u_longlong_t)zo->zo_vdevs, /* -v */
631 nice_vdev_size, /* -s */
632 zo->zo_ashift, /* -a */
633 zo->zo_mirrors, /* -m */
634 zo->zo_raidz, /* -r */
635 zo->zo_raidz_parity, /* -R */
636 zo->zo_datasets, /* -d */
637 zo->zo_threads, /* -t */
638 nice_gang_bang, /* -g */
639 zo->zo_init, /* -i */
640 (u_longlong_t)zo->zo_killrate, /* -k */
641 zo->zo_pool, /* -p */
643 (u_longlong_t)zo->zo_time, /* -T */
644 (u_longlong_t)zo->zo_maxloops, /* -F */
645 (u_longlong_t)zo->zo_passtime);
646 exit(requested ? 0 : 1);
650 process_options(int argc, char **argv)
653 ztest_shared_opts_t *zo = &ztest_opts;
657 char altdir[MAXNAMELEN] = { 0 };
659 bcopy(&ztest_opts_defaults, zo, sizeof (*zo));
661 while ((opt = getopt(argc, argv,
662 "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:hF:B:")) != EOF) {
679 value = nicenumtoull(optarg);
683 zo->zo_vdevs = value;
686 zo->zo_vdev_size = MAX(SPA_MINDEVSIZE, value);
689 zo->zo_ashift = value;
692 zo->zo_mirrors = value;
695 zo->zo_raidz = MAX(1, value);
698 zo->zo_raidz_parity = MIN(MAX(value, 1), 3);
701 zo->zo_datasets = MAX(1, value);
704 zo->zo_threads = MAX(1, value);
707 zo->zo_metaslab_gang_bang = MAX(SPA_MINBLOCKSIZE << 1,
714 zo->zo_killrate = value;
717 (void) strlcpy(zo->zo_pool, optarg,
718 sizeof (zo->zo_pool));
721 path = realpath(optarg, NULL);
723 (void) fprintf(stderr, "error: %s: %s\n",
724 optarg, strerror(errno));
727 (void) strlcpy(zo->zo_dir, path,
728 sizeof (zo->zo_dir));
741 zo->zo_passtime = MAX(1, value);
744 zo->zo_maxloops = MAX(1, value);
747 (void) strlcpy(altdir, optarg, sizeof (altdir));
759 zo->zo_raidz_parity = MIN(zo->zo_raidz_parity, zo->zo_raidz - 1);
762 (zo->zo_vdevs > 0 ? zo->zo_time * NANOSEC / zo->zo_vdevs :
765 if (strlen(altdir) > 0) {
773 cmd = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
774 realaltdir = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
776 VERIFY(NULL != realpath(getexecname(), cmd));
777 if (0 != access(altdir, F_OK)) {
778 ztest_dump_core = B_FALSE;
779 fatal(B_TRUE, "invalid alternate ztest path: %s",
782 VERIFY(NULL != realpath(altdir, realaltdir));
785 * 'cmd' should be of the form "<anything>/usr/bin/<isa>/ztest".
786 * We want to extract <isa> to determine if we should use
787 * 32 or 64 bit binaries.
789 bin = strstr(cmd, "/usr/bin/");
790 ztest = strstr(bin, "/ztest");
792 isalen = ztest - isa;
793 (void) snprintf(zo->zo_alt_ztest, sizeof (zo->zo_alt_ztest),
794 "%s/usr/bin/%.*s/ztest", realaltdir, isalen, isa);
795 (void) snprintf(zo->zo_alt_libpath, sizeof (zo->zo_alt_libpath),
796 "%s/usr/lib/%.*s", realaltdir, isalen, isa);
798 if (0 != access(zo->zo_alt_ztest, X_OK)) {
799 ztest_dump_core = B_FALSE;
800 fatal(B_TRUE, "invalid alternate ztest: %s",
802 } else if (0 != access(zo->zo_alt_libpath, X_OK)) {
803 ztest_dump_core = B_FALSE;
804 fatal(B_TRUE, "invalid alternate lib directory %s",
808 umem_free(cmd, MAXPATHLEN);
809 umem_free(realaltdir, MAXPATHLEN);
814 ztest_kill(ztest_shared_t *zs)
816 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(ztest_spa));
817 zs->zs_space = metaslab_class_get_space(spa_normal_class(ztest_spa));
820 * Before we kill off ztest, make sure that the config is updated.
821 * See comment above spa_config_sync().
823 mutex_enter(&spa_namespace_lock);
824 spa_config_sync(ztest_spa, B_FALSE, B_FALSE);
825 mutex_exit(&spa_namespace_lock);
827 (void) kill(getpid(), SIGKILL);
831 ztest_random(uint64_t range)
835 ASSERT3S(ztest_fd_rand, >=, 0);
840 if (read(ztest_fd_rand, &r, sizeof (r)) != sizeof (r))
841 fatal(1, "short read from /dev/urandom");
848 ztest_record_enospc(const char *s)
850 ztest_shared->zs_enospc_count++;
854 ztest_get_ashift(void)
856 if (ztest_opts.zo_ashift == 0)
857 return (SPA_MINBLOCKSHIFT + ztest_random(5));
858 return (ztest_opts.zo_ashift);
862 make_vdev_file(char *path, char *aux, char *pool, size_t size, uint64_t ashift)
868 pathbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
871 ashift = ztest_get_ashift();
877 vdev = ztest_shared->zs_vdev_aux;
878 (void) snprintf(path, MAXPATHLEN,
879 ztest_aux_template, ztest_opts.zo_dir,
880 pool == NULL ? ztest_opts.zo_pool : pool,
883 vdev = ztest_shared->zs_vdev_next_leaf++;
884 (void) snprintf(path, MAXPATHLEN,
885 ztest_dev_template, ztest_opts.zo_dir,
886 pool == NULL ? ztest_opts.zo_pool : pool, vdev);
891 int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
893 fatal(1, "can't open %s", path);
894 if (ftruncate(fd, size) != 0)
895 fatal(1, "can't ftruncate %s", path);
899 VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
900 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
901 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
902 VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
903 umem_free(pathbuf, MAXPATHLEN);
909 make_vdev_raidz(char *path, char *aux, char *pool, size_t size,
910 uint64_t ashift, int r)
912 nvlist_t *raidz, **child;
916 return (make_vdev_file(path, aux, pool, size, ashift));
917 child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
919 for (c = 0; c < r; c++)
920 child[c] = make_vdev_file(path, aux, pool, size, ashift);
922 VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
923 VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
924 VDEV_TYPE_RAIDZ) == 0);
925 VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
926 ztest_opts.zo_raidz_parity) == 0);
927 VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
930 for (c = 0; c < r; c++)
931 nvlist_free(child[c]);
933 umem_free(child, r * sizeof (nvlist_t *));
939 make_vdev_mirror(char *path, char *aux, char *pool, size_t size,
940 uint64_t ashift, int r, int m)
942 nvlist_t *mirror, **child;
946 return (make_vdev_raidz(path, aux, pool, size, ashift, r));
948 child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
950 for (c = 0; c < m; c++)
951 child[c] = make_vdev_raidz(path, aux, pool, size, ashift, r);
953 VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
954 VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
955 VDEV_TYPE_MIRROR) == 0);
956 VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
959 for (c = 0; c < m; c++)
960 nvlist_free(child[c]);
962 umem_free(child, m * sizeof (nvlist_t *));
968 make_vdev_root(char *path, char *aux, char *pool, size_t size, uint64_t ashift,
969 int log, int r, int m, int t)
971 nvlist_t *root, **child;
976 child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
978 for (c = 0; c < t; c++) {
979 child[c] = make_vdev_mirror(path, aux, pool, size, ashift,
981 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
985 VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
986 VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
987 VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
990 for (c = 0; c < t; c++)
991 nvlist_free(child[c]);
993 umem_free(child, t * sizeof (nvlist_t *));
999 * Find a random spa version. Returns back a random spa version in the
1000 * range [initial_version, SPA_VERSION_FEATURES].
1003 ztest_random_spa_version(uint64_t initial_version)
1005 uint64_t version = initial_version;
1007 if (version <= SPA_VERSION_BEFORE_FEATURES) {
1009 ztest_random(SPA_VERSION_BEFORE_FEATURES - version + 1);
1012 if (version > SPA_VERSION_BEFORE_FEATURES)
1013 version = SPA_VERSION_FEATURES;
1015 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
1020 ztest_random_blocksize(void)
1023 * Choose a block size >= the ashift.
1024 * If the SPA supports new MAXBLOCKSIZE, test up to 1MB blocks.
1026 int maxbs = SPA_OLD_MAXBLOCKSHIFT;
1027 if (spa_maxblocksize(ztest_spa) == SPA_MAXBLOCKSIZE)
1029 uint64_t block_shift =
1030 ztest_random(maxbs - ztest_spa->spa_max_ashift + 1);
1031 return (1 << (SPA_MINBLOCKSHIFT + block_shift));
1035 ztest_random_dnodesize(void)
1038 int max_slots = spa_maxdnodesize(ztest_spa) >> DNODE_SHIFT;
1040 if (max_slots == DNODE_MIN_SLOTS)
1041 return (DNODE_MIN_SIZE);
1044 * Weight the random distribution more heavily toward smaller
1045 * dnode sizes since that is more likely to reflect real-world
1048 ASSERT3U(max_slots, >, 4);
1049 switch (ztest_random(10)) {
1051 slots = 5 + ztest_random(max_slots - 4);
1054 slots = 2 + ztest_random(3);
1061 return (slots << DNODE_SHIFT);
1065 ztest_random_ibshift(void)
1067 return (DN_MIN_INDBLKSHIFT +
1068 ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1));
1072 ztest_random_vdev_top(spa_t *spa, boolean_t log_ok)
1075 vdev_t *rvd = spa->spa_root_vdev;
1078 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1081 top = ztest_random(rvd->vdev_children);
1082 tvd = rvd->vdev_child[top];
1083 } while (tvd->vdev_ishole || (tvd->vdev_islog && !log_ok) ||
1084 tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL);
1090 ztest_random_dsl_prop(zfs_prop_t prop)
1095 value = zfs_prop_random_value(prop, ztest_random(-1ULL));
1096 } while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF);
1102 ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value,
1105 const char *propname = zfs_prop_to_name(prop);
1106 const char *valname;
1111 error = dsl_prop_set_int(osname, propname,
1112 (inherit ? ZPROP_SRC_NONE : ZPROP_SRC_LOCAL), value);
1114 if (error == ENOSPC) {
1115 ztest_record_enospc(FTAG);
1120 setpoint = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
1121 VERIFY0(dsl_prop_get_integer(osname, propname, &curval, setpoint));
1123 if (ztest_opts.zo_verbose >= 6) {
1126 err = zfs_prop_index_to_string(prop, curval, &valname);
1128 (void) printf("%s %s = %llu at '%s'\n",
1129 osname, propname, (unsigned long long)curval,
1132 (void) printf("%s %s = %s at '%s'\n",
1133 osname, propname, valname, setpoint);
1135 umem_free(setpoint, MAXPATHLEN);
1141 ztest_spa_prop_set_uint64(zpool_prop_t prop, uint64_t value)
1143 spa_t *spa = ztest_spa;
1144 nvlist_t *props = NULL;
1147 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
1148 VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0);
1150 error = spa_prop_set(spa, props);
1154 if (error == ENOSPC) {
1155 ztest_record_enospc(FTAG);
1165 * Object and range lock mechanics
1168 list_node_t z_lnode;
1169 refcount_t z_refcnt;
1171 zfs_rlock_t z_range_lock;
1176 ztest_znode_t *z_ztznode;
1179 static ztest_znode_t *
1180 ztest_znode_init(uint64_t object)
1182 ztest_znode_t *zp = umem_alloc(sizeof (*zp), UMEM_NOFAIL);
1184 list_link_init(&zp->z_lnode);
1185 refcount_create(&zp->z_refcnt);
1186 zp->z_object = object;
1187 zfs_rlock_init(&zp->z_range_lock);
1193 ztest_znode_fini(ztest_znode_t *zp)
1195 ASSERT(refcount_is_zero(&zp->z_refcnt));
1196 zfs_rlock_destroy(&zp->z_range_lock);
1198 refcount_destroy(&zp->z_refcnt);
1199 list_link_init(&zp->z_lnode);
1200 umem_free(zp, sizeof (*zp));
1204 ztest_zll_init(zll_t *zll)
1206 mutex_init(&zll->z_lock, NULL, MUTEX_DEFAULT, NULL);
1207 list_create(&zll->z_list, sizeof (ztest_znode_t),
1208 offsetof(ztest_znode_t, z_lnode));
1212 ztest_zll_destroy(zll_t *zll)
1214 list_destroy(&zll->z_list);
1215 mutex_destroy(&zll->z_lock);
1218 #define RL_TAG "range_lock"
1219 static ztest_znode_t *
1220 ztest_znode_get(ztest_ds_t *zd, uint64_t object)
1222 zll_t *zll = &zd->zd_range_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1223 ztest_znode_t *zp = NULL;
1224 mutex_enter(&zll->z_lock);
1225 for (zp = list_head(&zll->z_list); (zp);
1226 zp = list_next(&zll->z_list, zp)) {
1227 if (zp->z_object == object) {
1228 refcount_add(&zp->z_refcnt, RL_TAG);
1233 zp = ztest_znode_init(object);
1234 refcount_add(&zp->z_refcnt, RL_TAG);
1235 list_insert_head(&zll->z_list, zp);
1237 mutex_exit(&zll->z_lock);
1242 ztest_znode_put(ztest_ds_t *zd, ztest_znode_t *zp)
1245 ASSERT3U(zp->z_object, !=, 0);
1246 zll = &zd->zd_range_lock[zp->z_object & (ZTEST_OBJECT_LOCKS - 1)];
1247 mutex_enter(&zll->z_lock);
1248 refcount_remove(&zp->z_refcnt, RL_TAG);
1249 if (refcount_is_zero(&zp->z_refcnt)) {
1250 list_remove(&zll->z_list, zp);
1251 ztest_znode_fini(zp);
1253 mutex_exit(&zll->z_lock);
1258 ztest_rll_init(rll_t *rll)
1260 rll->rll_writer = NULL;
1261 rll->rll_readers = 0;
1262 mutex_init(&rll->rll_lock, NULL, MUTEX_DEFAULT, NULL);
1263 cv_init(&rll->rll_cv, NULL, CV_DEFAULT, NULL);
1267 ztest_rll_destroy(rll_t *rll)
1269 ASSERT(rll->rll_writer == NULL);
1270 ASSERT(rll->rll_readers == 0);
1271 mutex_destroy(&rll->rll_lock);
1272 cv_destroy(&rll->rll_cv);
1276 ztest_rll_lock(rll_t *rll, rl_type_t type)
1278 mutex_enter(&rll->rll_lock);
1280 if (type == RL_READER) {
1281 while (rll->rll_writer != NULL)
1282 (void) cv_wait(&rll->rll_cv, &rll->rll_lock);
1285 while (rll->rll_writer != NULL || rll->rll_readers)
1286 (void) cv_wait(&rll->rll_cv, &rll->rll_lock);
1287 rll->rll_writer = curthread;
1290 mutex_exit(&rll->rll_lock);
1294 ztest_rll_unlock(rll_t *rll)
1296 mutex_enter(&rll->rll_lock);
1298 if (rll->rll_writer) {
1299 ASSERT(rll->rll_readers == 0);
1300 rll->rll_writer = NULL;
1302 ASSERT(rll->rll_readers != 0);
1303 ASSERT(rll->rll_writer == NULL);
1307 if (rll->rll_writer == NULL && rll->rll_readers == 0)
1308 cv_broadcast(&rll->rll_cv);
1310 mutex_exit(&rll->rll_lock);
1314 ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type)
1316 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1318 ztest_rll_lock(rll, type);
1322 ztest_object_unlock(ztest_ds_t *zd, uint64_t object)
1324 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1326 ztest_rll_unlock(rll);
1329 static ztest_zrl_t *
1330 ztest_zrl_init(rl_t *rl, ztest_znode_t *zp)
1332 ztest_zrl_t *zrl = umem_alloc(sizeof (*zrl), UMEM_NOFAIL);
1334 zrl->z_ztznode = zp;
1339 ztest_zrl_fini(ztest_zrl_t *zrl)
1341 umem_free(zrl, sizeof (*zrl));
1344 static ztest_zrl_t *
1345 ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset,
1346 uint64_t size, rl_type_t type)
1348 ztest_znode_t *zp = ztest_znode_get(zd, object);
1349 rl_t *rl = zfs_range_lock(&zp->z_range_lock, offset,
1351 return (ztest_zrl_init(rl, zp));
1355 ztest_range_unlock(ztest_ds_t *zd, ztest_zrl_t *zrl)
1357 zfs_range_unlock(zrl->z_rl);
1358 ztest_znode_put(zd, zrl->z_ztznode);
1359 ztest_zrl_fini(zrl);
1363 ztest_zd_init(ztest_ds_t *zd, ztest_shared_ds_t *szd, objset_t *os)
1366 zd->zd_zilog = dmu_objset_zil(os);
1367 zd->zd_shared = szd;
1368 dmu_objset_name(os, zd->zd_name);
1371 if (zd->zd_shared != NULL)
1372 zd->zd_shared->zd_seq = 0;
1374 VERIFY(rwlock_init(&zd->zd_zilog_lock, USYNC_THREAD, NULL) == 0);
1375 mutex_init(&zd->zd_dirobj_lock, NULL, MUTEX_DEFAULT, NULL);
1377 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1378 ztest_rll_init(&zd->zd_object_lock[l]);
1380 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
1381 ztest_zll_init(&zd->zd_range_lock[l]);
1385 ztest_zd_fini(ztest_ds_t *zd)
1389 mutex_destroy(&zd->zd_dirobj_lock);
1390 (void) rwlock_destroy(&zd->zd_zilog_lock);
1392 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1393 ztest_rll_destroy(&zd->zd_object_lock[l]);
1395 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
1396 ztest_zll_destroy(&zd->zd_range_lock[l]);
1399 #define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1402 ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag)
1408 * Attempt to assign tx to some transaction group.
1410 error = dmu_tx_assign(tx, txg_how);
1412 if (error == ERESTART) {
1413 ASSERT(txg_how == TXG_NOWAIT);
1416 ASSERT3U(error, ==, ENOSPC);
1417 ztest_record_enospc(tag);
1422 txg = dmu_tx_get_txg(tx);
1428 ztest_pattern_set(void *buf, uint64_t size, uint64_t value)
1431 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1439 ztest_pattern_match(void *buf, uint64_t size, uint64_t value)
1442 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1446 diff |= (value - *ip++);
1453 ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1454 uint64_t dnodesize, uint64_t offset, uint64_t gen, uint64_t txg,
1457 bt->bt_magic = BT_MAGIC;
1458 bt->bt_objset = dmu_objset_id(os);
1459 bt->bt_object = object;
1460 bt->bt_dnodesize = dnodesize;
1461 bt->bt_offset = offset;
1464 bt->bt_crtxg = crtxg;
1468 ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1469 uint64_t dnodesize, uint64_t offset, uint64_t gen, uint64_t txg,
1472 ASSERT3U(bt->bt_magic, ==, BT_MAGIC);
1473 ASSERT3U(bt->bt_objset, ==, dmu_objset_id(os));
1474 ASSERT3U(bt->bt_object, ==, object);
1475 ASSERT3U(bt->bt_dnodesize, ==, dnodesize);
1476 ASSERT3U(bt->bt_offset, ==, offset);
1477 ASSERT3U(bt->bt_gen, <=, gen);
1478 ASSERT3U(bt->bt_txg, <=, txg);
1479 ASSERT3U(bt->bt_crtxg, ==, crtxg);
1482 static ztest_block_tag_t *
1483 ztest_bt_bonus(dmu_buf_t *db)
1485 dmu_object_info_t doi;
1486 ztest_block_tag_t *bt;
1488 dmu_object_info_from_db(db, &doi);
1489 ASSERT3U(doi.doi_bonus_size, <=, db->db_size);
1490 ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt));
1491 bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt));
1497 * Generate a token to fill up unused bonus buffer space. Try to make
1498 * it unique to the object, generation, and offset to verify that data
1499 * is not getting overwritten by data from other dnodes.
1501 #define ZTEST_BONUS_FILL_TOKEN(obj, ds, gen, offset) \
1502 (((ds) << 48) | ((gen) << 32) | ((obj) << 8) | (offset))
1505 * Fill up the unused bonus buffer region before the block tag with a
1506 * verifiable pattern. Filling the whole bonus area with non-zero data
1507 * helps ensure that all dnode traversal code properly skips the
1508 * interior regions of large dnodes.
1511 ztest_fill_unused_bonus(dmu_buf_t *db, void *end, uint64_t obj,
1512 objset_t *os, uint64_t gen)
1516 ASSERT(IS_P2ALIGNED((char *)end - (char *)db->db_data, 8));
1518 for (bonusp = db->db_data; bonusp < (uint64_t *)end; bonusp++) {
1519 uint64_t token = ZTEST_BONUS_FILL_TOKEN(obj, dmu_objset_id(os),
1520 gen, bonusp - (uint64_t *)db->db_data);
1526 * Verify that the unused area of a bonus buffer is filled with the
1530 ztest_verify_unused_bonus(dmu_buf_t *db, void *end, uint64_t obj,
1531 objset_t *os, uint64_t gen)
1535 for (bonusp = db->db_data; bonusp < (uint64_t *)end; bonusp++) {
1536 uint64_t token = ZTEST_BONUS_FILL_TOKEN(obj, dmu_objset_id(os),
1537 gen, bonusp - (uint64_t *)db->db_data);
1538 VERIFY3U(*bonusp, ==, token);
1546 #define lrz_type lr_mode
1547 #define lrz_blocksize lr_uid
1548 #define lrz_ibshift lr_gid
1549 #define lrz_bonustype lr_rdev
1550 #define lrz_dnodesize lr_crtime[1]
1553 ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr)
1555 char *name = (void *)(lr + 1); /* name follows lr */
1556 size_t namesize = strlen(name) + 1;
1559 if (zil_replaying(zd->zd_zilog, tx))
1562 itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize);
1563 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1564 sizeof (*lr) + namesize - sizeof (lr_t));
1566 zil_itx_assign(zd->zd_zilog, itx, tx);
1570 ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr, uint64_t object)
1572 char *name = (void *)(lr + 1); /* name follows lr */
1573 size_t namesize = strlen(name) + 1;
1576 if (zil_replaying(zd->zd_zilog, tx))
1579 itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize);
1580 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1581 sizeof (*lr) + namesize - sizeof (lr_t));
1583 itx->itx_oid = object;
1584 zil_itx_assign(zd->zd_zilog, itx, tx);
1588 ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr)
1591 itx_wr_state_t write_state = ztest_random(WR_NUM_STATES);
1593 if (zil_replaying(zd->zd_zilog, tx))
1596 if (lr->lr_length > ZIL_MAX_LOG_DATA)
1597 write_state = WR_INDIRECT;
1599 itx = zil_itx_create(TX_WRITE,
1600 sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0));
1602 if (write_state == WR_COPIED &&
1603 dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length,
1604 ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) {
1605 zil_itx_destroy(itx);
1606 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1607 write_state = WR_NEED_COPY;
1609 itx->itx_private = zd;
1610 itx->itx_wr_state = write_state;
1611 itx->itx_sync = (ztest_random(8) == 0);
1612 itx->itx_sod += (write_state == WR_NEED_COPY ? lr->lr_length : 0);
1614 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1615 sizeof (*lr) - sizeof (lr_t));
1617 zil_itx_assign(zd->zd_zilog, itx, tx);
1621 ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr)
1625 if (zil_replaying(zd->zd_zilog, tx))
1628 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1629 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1630 sizeof (*lr) - sizeof (lr_t));
1632 itx->itx_sync = B_FALSE;
1633 zil_itx_assign(zd->zd_zilog, itx, tx);
1637 ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr)
1641 if (zil_replaying(zd->zd_zilog, tx))
1644 itx = zil_itx_create(TX_SETATTR, sizeof (*lr));
1645 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1646 sizeof (*lr) - sizeof (lr_t));
1648 itx->itx_sync = B_FALSE;
1649 zil_itx_assign(zd->zd_zilog, itx, tx);
1656 ztest_replay_create(ztest_ds_t *zd, lr_create_t *lr, boolean_t byteswap)
1658 char *name = (void *)(lr + 1); /* name follows lr */
1659 objset_t *os = zd->zd_os;
1660 ztest_block_tag_t *bbt;
1668 byteswap_uint64_array(lr, sizeof (*lr));
1670 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1671 ASSERT(name[0] != '\0');
1673 tx = dmu_tx_create(os);
1675 dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name);
1677 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1678 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1680 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1683 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1687 ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid);
1688 bonuslen = DN_BONUS_SIZE(lr->lrz_dnodesize);
1690 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1691 if (lr->lr_foid == 0) {
1692 lr->lr_foid = zap_create_dnsize(os,
1693 lr->lrz_type, lr->lrz_bonustype,
1694 bonuslen, lr->lrz_dnodesize, tx);
1696 error = zap_create_claim_dnsize(os, lr->lr_foid,
1697 lr->lrz_type, lr->lrz_bonustype,
1698 bonuslen, lr->lrz_dnodesize, tx);
1701 if (lr->lr_foid == 0) {
1702 lr->lr_foid = dmu_object_alloc_dnsize(os,
1703 lr->lrz_type, 0, lr->lrz_bonustype,
1704 bonuslen, lr->lrz_dnodesize, tx);
1706 error = dmu_object_claim_dnsize(os, lr->lr_foid,
1707 lr->lrz_type, 0, lr->lrz_bonustype,
1708 bonuslen, lr->lrz_dnodesize, tx);
1713 ASSERT3U(error, ==, EEXIST);
1714 ASSERT(zd->zd_zilog->zl_replay);
1719 ASSERT(lr->lr_foid != 0);
1721 if (lr->lrz_type != DMU_OT_ZAP_OTHER)
1722 VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid,
1723 lr->lrz_blocksize, lr->lrz_ibshift, tx));
1725 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1726 bbt = ztest_bt_bonus(db);
1727 dmu_buf_will_dirty(db, tx);
1728 ztest_bt_generate(bbt, os, lr->lr_foid, lr->lrz_dnodesize, -1ULL,
1729 lr->lr_gen, txg, txg);
1730 ztest_fill_unused_bonus(db, bbt, lr->lr_foid, os, lr->lr_gen);
1731 dmu_buf_rele(db, FTAG);
1733 VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1,
1736 (void) ztest_log_create(zd, tx, lr);
1744 ztest_replay_remove(ztest_ds_t *zd, lr_remove_t *lr, boolean_t byteswap)
1746 char *name = (void *)(lr + 1); /* name follows lr */
1747 objset_t *os = zd->zd_os;
1748 dmu_object_info_t doi;
1750 uint64_t object, txg;
1753 byteswap_uint64_array(lr, sizeof (*lr));
1755 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1756 ASSERT(name[0] != '\0');
1759 zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object));
1760 ASSERT(object != 0);
1762 ztest_object_lock(zd, object, RL_WRITER);
1764 VERIFY3U(0, ==, dmu_object_info(os, object, &doi));
1766 tx = dmu_tx_create(os);
1768 dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name);
1769 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1771 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1773 ztest_object_unlock(zd, object);
1777 if (doi.doi_type == DMU_OT_ZAP_OTHER) {
1778 VERIFY3U(0, ==, zap_destroy(os, object, tx));
1780 VERIFY3U(0, ==, dmu_object_free(os, object, tx));
1783 VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx));
1785 (void) ztest_log_remove(zd, tx, lr, object);
1789 ztest_object_unlock(zd, object);
1795 ztest_replay_write(ztest_ds_t *zd, lr_write_t *lr, boolean_t byteswap)
1797 objset_t *os = zd->zd_os;
1798 void *data = lr + 1; /* data follows lr */
1799 uint64_t offset, length;
1800 ztest_block_tag_t *bt = data;
1801 ztest_block_tag_t *bbt;
1802 uint64_t gen, txg, lrtxg, crtxg;
1803 dmu_object_info_t doi;
1806 arc_buf_t *abuf = NULL;
1810 byteswap_uint64_array(lr, sizeof (*lr));
1812 offset = lr->lr_offset;
1813 length = lr->lr_length;
1815 /* If it's a dmu_sync() block, write the whole block */
1816 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
1817 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
1818 if (length < blocksize) {
1819 offset -= offset % blocksize;
1824 if (bt->bt_magic == BSWAP_64(BT_MAGIC))
1825 byteswap_uint64_array(bt, sizeof (*bt));
1827 if (bt->bt_magic != BT_MAGIC)
1830 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1831 rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER);
1833 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1835 dmu_object_info_from_db(db, &doi);
1837 bbt = ztest_bt_bonus(db);
1838 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1840 crtxg = bbt->bt_crtxg;
1841 lrtxg = lr->lr_common.lrc_txg;
1843 tx = dmu_tx_create(os);
1845 dmu_tx_hold_write(tx, lr->lr_foid, offset, length);
1847 if (ztest_random(8) == 0 && length == doi.doi_data_block_size &&
1848 P2PHASE(offset, length) == 0)
1849 abuf = dmu_request_arcbuf(db, length);
1851 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1854 dmu_return_arcbuf(abuf);
1855 dmu_buf_rele(db, FTAG);
1856 ztest_range_unlock(zd, rl);
1857 ztest_object_unlock(zd, lr->lr_foid);
1863 * Usually, verify the old data before writing new data --
1864 * but not always, because we also want to verify correct
1865 * behavior when the data was not recently read into cache.
1867 ASSERT(offset % doi.doi_data_block_size == 0);
1868 if (ztest_random(4) != 0) {
1869 int prefetch = ztest_random(2) ?
1870 DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
1871 ztest_block_tag_t rbt;
1873 VERIFY(dmu_read(os, lr->lr_foid, offset,
1874 sizeof (rbt), &rbt, prefetch) == 0);
1875 if (rbt.bt_magic == BT_MAGIC) {
1876 ztest_bt_verify(&rbt, os, lr->lr_foid, 0,
1877 offset, gen, txg, crtxg);
1882 * Writes can appear to be newer than the bonus buffer because
1883 * the ztest_get_data() callback does a dmu_read() of the
1884 * open-context data, which may be different than the data
1885 * as it was when the write was generated.
1887 if (zd->zd_zilog->zl_replay) {
1888 ztest_bt_verify(bt, os, lr->lr_foid, 0, offset,
1889 MAX(gen, bt->bt_gen), MAX(txg, lrtxg),
1894 * Set the bt's gen/txg to the bonus buffer's gen/txg
1895 * so that all of the usual ASSERTs will work.
1897 ztest_bt_generate(bt, os, lr->lr_foid, 0, offset, gen, txg,
1902 dmu_write(os, lr->lr_foid, offset, length, data, tx);
1904 bcopy(data, abuf->b_data, length);
1905 dmu_assign_arcbuf(db, offset, abuf, tx);
1908 (void) ztest_log_write(zd, tx, lr);
1910 dmu_buf_rele(db, FTAG);
1914 ztest_range_unlock(zd, rl);
1915 ztest_object_unlock(zd, lr->lr_foid);
1921 ztest_replay_truncate(ztest_ds_t *zd, lr_truncate_t *lr, boolean_t byteswap)
1923 objset_t *os = zd->zd_os;
1929 byteswap_uint64_array(lr, sizeof (*lr));
1931 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1932 rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length,
1935 tx = dmu_tx_create(os);
1937 dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length);
1939 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1941 ztest_range_unlock(zd, rl);
1942 ztest_object_unlock(zd, lr->lr_foid);
1946 VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset,
1947 lr->lr_length, tx) == 0);
1949 (void) ztest_log_truncate(zd, tx, lr);
1953 ztest_range_unlock(zd, rl);
1954 ztest_object_unlock(zd, lr->lr_foid);
1960 ztest_replay_setattr(ztest_ds_t *zd, lr_setattr_t *lr, boolean_t byteswap)
1962 objset_t *os = zd->zd_os;
1965 ztest_block_tag_t *bbt;
1966 uint64_t txg, lrtxg, crtxg, dnodesize;
1969 byteswap_uint64_array(lr, sizeof (*lr));
1971 ztest_object_lock(zd, lr->lr_foid, RL_WRITER);
1973 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1975 tx = dmu_tx_create(os);
1976 dmu_tx_hold_bonus(tx, lr->lr_foid);
1978 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1980 dmu_buf_rele(db, FTAG);
1981 ztest_object_unlock(zd, lr->lr_foid);
1985 bbt = ztest_bt_bonus(db);
1986 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1987 crtxg = bbt->bt_crtxg;
1988 lrtxg = lr->lr_common.lrc_txg;
1989 dnodesize = bbt->bt_dnodesize;
1991 if (zd->zd_zilog->zl_replay) {
1992 ASSERT(lr->lr_size != 0);
1993 ASSERT(lr->lr_mode != 0);
1997 * Randomly change the size and increment the generation.
1999 lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) *
2001 lr->lr_mode = bbt->bt_gen + 1;
2006 * Verify that the current bonus buffer is not newer than our txg.
2008 ztest_bt_verify(bbt, os, lr->lr_foid, dnodesize, -1ULL, lr->lr_mode,
2009 MAX(txg, lrtxg), crtxg);
2011 dmu_buf_will_dirty(db, tx);
2013 ASSERT3U(lr->lr_size, >=, sizeof (*bbt));
2014 ASSERT3U(lr->lr_size, <=, db->db_size);
2015 VERIFY0(dmu_set_bonus(db, lr->lr_size, tx));
2016 bbt = ztest_bt_bonus(db);
2018 ztest_bt_generate(bbt, os, lr->lr_foid, dnodesize, -1ULL, lr->lr_mode,
2020 ztest_fill_unused_bonus(db, bbt, lr->lr_foid, os, bbt->bt_gen);
2021 dmu_buf_rele(db, FTAG);
2023 (void) ztest_log_setattr(zd, tx, lr);
2027 ztest_object_unlock(zd, lr->lr_foid);
2032 zil_replay_func_t ztest_replay_vector[TX_MAX_TYPE] = {
2033 NULL, /* 0 no such transaction type */
2034 (zil_replay_func_t)ztest_replay_create, /* TX_CREATE */
2035 NULL, /* TX_MKDIR */
2036 NULL, /* TX_MKXATTR */
2037 NULL, /* TX_SYMLINK */
2038 (zil_replay_func_t)ztest_replay_remove, /* TX_REMOVE */
2039 NULL, /* TX_RMDIR */
2041 NULL, /* TX_RENAME */
2042 (zil_replay_func_t)ztest_replay_write, /* TX_WRITE */
2043 (zil_replay_func_t)ztest_replay_truncate, /* TX_TRUNCATE */
2044 (zil_replay_func_t)ztest_replay_setattr, /* TX_SETATTR */
2046 NULL, /* TX_CREATE_ACL */
2047 NULL, /* TX_CREATE_ATTR */
2048 NULL, /* TX_CREATE_ACL_ATTR */
2049 NULL, /* TX_MKDIR_ACL */
2050 NULL, /* TX_MKDIR_ATTR */
2051 NULL, /* TX_MKDIR_ACL_ATTR */
2052 NULL, /* TX_WRITE2 */
2056 * ZIL get_data callbacks
2058 typedef struct ztest_zgd_private {
2062 } ztest_zgd_private_t;
2065 ztest_get_done(zgd_t *zgd, int error)
2067 ztest_zgd_private_t *zzp = zgd->zgd_private;
2068 ztest_ds_t *zd = zzp->z_zd;
2069 uint64_t object = zzp->z_object;
2072 dmu_buf_rele(zgd->zgd_db, zgd);
2074 ztest_range_unlock(zd, zzp->z_rl);
2075 ztest_object_unlock(zd, object);
2077 if (error == 0 && zgd->zgd_bp)
2078 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
2080 umem_free(zgd, sizeof (*zgd));
2081 umem_free(zzp, sizeof (*zzp));
2085 ztest_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
2087 ztest_ds_t *zd = arg;
2088 objset_t *os = zd->zd_os;
2089 uint64_t object = lr->lr_foid;
2090 uint64_t offset = lr->lr_offset;
2091 uint64_t size = lr->lr_length;
2092 blkptr_t *bp = &lr->lr_blkptr;
2093 uint64_t txg = lr->lr_common.lrc_txg;
2095 dmu_object_info_t doi;
2099 ztest_zgd_private_t *zgd_private;
2101 ztest_object_lock(zd, object, RL_READER);
2102 error = dmu_bonus_hold(os, object, FTAG, &db);
2104 ztest_object_unlock(zd, object);
2108 crtxg = ztest_bt_bonus(db)->bt_crtxg;
2110 if (crtxg == 0 || crtxg > txg) {
2111 dmu_buf_rele(db, FTAG);
2112 ztest_object_unlock(zd, object);
2116 dmu_object_info_from_db(db, &doi);
2117 dmu_buf_rele(db, FTAG);
2120 zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL);
2121 zgd->zgd_zilog = zd->zd_zilog;
2122 zgd_private = umem_zalloc(sizeof (ztest_zgd_private_t), UMEM_NOFAIL);
2123 zgd_private->z_zd = zd;
2124 zgd_private->z_object = object;
2125 zgd->zgd_private = zgd_private;
2127 if (buf != NULL) { /* immediate write */
2128 zgd_private->z_rl = ztest_range_lock(zd, object, offset, size,
2131 error = dmu_read(os, object, offset, size, buf,
2132 DMU_READ_NO_PREFETCH);
2135 size = doi.doi_data_block_size;
2137 offset = P2ALIGN(offset, size);
2139 ASSERT(offset < size);
2143 zgd_private->z_rl = ztest_range_lock(zd, object, offset, size,
2146 error = dmu_buf_hold(os, object, offset, zgd, &db,
2147 DMU_READ_NO_PREFETCH);
2150 blkptr_t *obp = dmu_buf_get_blkptr(db);
2152 ASSERT(BP_IS_HOLE(bp));
2159 ASSERT(db->db_offset == offset);
2160 ASSERT(db->db_size == size);
2162 error = dmu_sync(zio, lr->lr_common.lrc_txg,
2163 ztest_get_done, zgd);
2170 ztest_get_done(zgd, error);
2176 ztest_lr_alloc(size_t lrsize, char *name)
2179 size_t namesize = name ? strlen(name) + 1 : 0;
2181 lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
2184 bcopy(name, lr + lrsize, namesize);
2190 ztest_lr_free(void *lr, size_t lrsize, char *name)
2192 size_t namesize = name ? strlen(name) + 1 : 0;
2194 umem_free(lr, lrsize + namesize);
2198 * Lookup a bunch of objects. Returns the number of objects not found.
2201 ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
2207 ASSERT(mutex_held(&zd->zd_dirobj_lock));
2209 for (i = 0; i < count; i++, od++) {
2211 error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
2212 sizeof (uint64_t), 1, &od->od_object);
2214 ASSERT(error == ENOENT);
2215 ASSERT(od->od_object == 0);
2219 ztest_block_tag_t *bbt;
2220 dmu_object_info_t doi;
2222 ASSERT(od->od_object != 0);
2223 ASSERT(missing == 0); /* there should be no gaps */
2225 ztest_object_lock(zd, od->od_object, RL_READER);
2226 VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
2227 od->od_object, FTAG, &db));
2228 dmu_object_info_from_db(db, &doi);
2229 bbt = ztest_bt_bonus(db);
2230 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
2231 od->od_type = doi.doi_type;
2232 od->od_blocksize = doi.doi_data_block_size;
2233 od->od_gen = bbt->bt_gen;
2234 dmu_buf_rele(db, FTAG);
2235 ztest_object_unlock(zd, od->od_object);
2243 ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
2248 ASSERT(mutex_held(&zd->zd_dirobj_lock));
2250 for (i = 0; i < count; i++, od++) {
2257 lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2259 lr->lr_doid = od->od_dir;
2260 lr->lr_foid = 0; /* 0 to allocate, > 0 to claim */
2261 lr->lrz_type = od->od_crtype;
2262 lr->lrz_blocksize = od->od_crblocksize;
2263 lr->lrz_ibshift = ztest_random_ibshift();
2264 lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
2265 lr->lrz_dnodesize = od->od_crdnodesize;
2266 lr->lr_gen = od->od_crgen;
2267 lr->lr_crtime[0] = time(NULL);
2269 if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
2270 ASSERT(missing == 0);
2274 od->od_object = lr->lr_foid;
2275 od->od_type = od->od_crtype;
2276 od->od_blocksize = od->od_crblocksize;
2277 od->od_gen = od->od_crgen;
2278 ASSERT(od->od_object != 0);
2281 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2288 ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
2294 ASSERT(mutex_held(&zd->zd_dirobj_lock));
2298 for (i = count - 1; i >= 0; i--, od--) {
2305 * No object was found.
2307 if (od->od_object == 0)
2310 lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2312 lr->lr_doid = od->od_dir;
2314 if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
2315 ASSERT3U(error, ==, ENOSPC);
2320 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2327 ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
2333 lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
2335 lr->lr_foid = object;
2336 lr->lr_offset = offset;
2337 lr->lr_length = size;
2339 BP_ZERO(&lr->lr_blkptr);
2341 bcopy(data, lr + 1, size);
2343 error = ztest_replay_write(zd, lr, B_FALSE);
2345 ztest_lr_free(lr, sizeof (*lr) + size, NULL);
2351 ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2356 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2358 lr->lr_foid = object;
2359 lr->lr_offset = offset;
2360 lr->lr_length = size;
2362 error = ztest_replay_truncate(zd, lr, B_FALSE);
2364 ztest_lr_free(lr, sizeof (*lr), NULL);
2370 ztest_setattr(ztest_ds_t *zd, uint64_t object)
2375 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2377 lr->lr_foid = object;
2381 error = ztest_replay_setattr(zd, lr, B_FALSE);
2383 ztest_lr_free(lr, sizeof (*lr), NULL);
2389 ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2391 objset_t *os = zd->zd_os;
2396 txg_wait_synced(dmu_objset_pool(os), 0);
2398 ztest_object_lock(zd, object, RL_READER);
2399 rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
2401 tx = dmu_tx_create(os);
2403 dmu_tx_hold_write(tx, object, offset, size);
2405 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
2408 dmu_prealloc(os, object, offset, size, tx);
2410 txg_wait_synced(dmu_objset_pool(os), txg);
2412 (void) dmu_free_long_range(os, object, offset, size);
2415 ztest_range_unlock(zd, rl);
2416 ztest_object_unlock(zd, object);
2420 ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
2423 ztest_block_tag_t wbt;
2424 dmu_object_info_t doi;
2425 enum ztest_io_type io_type;
2429 VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
2430 blocksize = doi.doi_data_block_size;
2431 data = umem_alloc(blocksize, UMEM_NOFAIL);
2434 * Pick an i/o type at random, biased toward writing block tags.
2436 io_type = ztest_random(ZTEST_IO_TYPES);
2437 if (ztest_random(2) == 0)
2438 io_type = ZTEST_IO_WRITE_TAG;
2440 (void) rw_rdlock(&zd->zd_zilog_lock);
2444 case ZTEST_IO_WRITE_TAG:
2445 ztest_bt_generate(&wbt, zd->zd_os, object, doi.doi_dnodesize,
2447 (void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
2450 case ZTEST_IO_WRITE_PATTERN:
2451 (void) memset(data, 'a' + (object + offset) % 5, blocksize);
2452 if (ztest_random(2) == 0) {
2454 * Induce fletcher2 collisions to ensure that
2455 * zio_ddt_collision() detects and resolves them
2456 * when using fletcher2-verify for deduplication.
2458 ((uint64_t *)data)[0] ^= 1ULL << 63;
2459 ((uint64_t *)data)[4] ^= 1ULL << 63;
2461 (void) ztest_write(zd, object, offset, blocksize, data);
2464 case ZTEST_IO_WRITE_ZEROES:
2465 bzero(data, blocksize);
2466 (void) ztest_write(zd, object, offset, blocksize, data);
2469 case ZTEST_IO_TRUNCATE:
2470 (void) ztest_truncate(zd, object, offset, blocksize);
2473 case ZTEST_IO_SETATTR:
2474 (void) ztest_setattr(zd, object);
2479 case ZTEST_IO_REWRITE:
2480 (void) rw_rdlock(&ztest_name_lock);
2481 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2482 ZFS_PROP_CHECKSUM, spa_dedup_checksum(ztest_spa),
2484 VERIFY(err == 0 || err == ENOSPC);
2485 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2486 ZFS_PROP_COMPRESSION,
2487 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION),
2489 VERIFY(err == 0 || err == ENOSPC);
2490 (void) rw_unlock(&ztest_name_lock);
2492 VERIFY0(dmu_read(zd->zd_os, object, offset, blocksize, data,
2493 DMU_READ_NO_PREFETCH));
2495 (void) ztest_write(zd, object, offset, blocksize, data);
2499 (void) rw_unlock(&zd->zd_zilog_lock);
2501 umem_free(data, blocksize);
2505 * Initialize an object description template.
2508 ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
2509 dmu_object_type_t type, uint64_t blocksize, uint64_t dnodesize,
2512 od->od_dir = ZTEST_DIROBJ;
2515 od->od_crtype = type;
2516 od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
2517 od->od_crdnodesize = dnodesize ? dnodesize : ztest_random_dnodesize();
2520 od->od_type = DMU_OT_NONE;
2521 od->od_blocksize = 0;
2524 (void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
2525 tag, (longlong_t)id, (u_longlong_t)index);
2529 * Lookup or create the objects for a test using the od template.
2530 * If the objects do not all exist, or if 'remove' is specified,
2531 * remove any existing objects and create new ones. Otherwise,
2532 * use the existing objects.
2535 ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
2537 int count = size / sizeof (*od);
2540 mutex_enter(&zd->zd_dirobj_lock);
2541 if ((ztest_lookup(zd, od, count) != 0 || remove) &&
2542 (ztest_remove(zd, od, count) != 0 ||
2543 ztest_create(zd, od, count) != 0))
2546 mutex_exit(&zd->zd_dirobj_lock);
2553 ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
2555 zilog_t *zilog = zd->zd_zilog;
2557 (void) rw_rdlock(&zd->zd_zilog_lock);
2559 zil_commit(zilog, ztest_random(ZTEST_OBJECTS));
2562 * Remember the committed values in zd, which is in parent/child
2563 * shared memory. If we die, the next iteration of ztest_run()
2564 * will verify that the log really does contain this record.
2566 mutex_enter(&zilog->zl_lock);
2567 ASSERT(zd->zd_shared != NULL);
2568 ASSERT3U(zd->zd_shared->zd_seq, <=, zilog->zl_commit_lr_seq);
2569 zd->zd_shared->zd_seq = zilog->zl_commit_lr_seq;
2570 mutex_exit(&zilog->zl_lock);
2572 (void) rw_unlock(&zd->zd_zilog_lock);
2576 * This function is designed to simulate the operations that occur during a
2577 * mount/unmount operation. We hold the dataset across these operations in an
2578 * attempt to expose any implicit assumptions about ZIL management.
2582 ztest_zil_remount(ztest_ds_t *zd, uint64_t id)
2584 objset_t *os = zd->zd_os;
2587 * We grab the zd_dirobj_lock to ensure that no other thread is
2588 * updating the zil (i.e. adding in-memory log records) and the
2589 * zd_zilog_lock to block any I/O.
2591 mutex_enter(&zd->zd_dirobj_lock);
2592 (void) rw_wrlock(&zd->zd_zilog_lock);
2594 /* zfs_sb_teardown() */
2595 zil_close(zd->zd_zilog);
2597 /* zfsvfs_setup() */
2598 VERIFY(zil_open(os, ztest_get_data) == zd->zd_zilog);
2599 zil_replay(os, zd, ztest_replay_vector);
2601 (void) rw_unlock(&zd->zd_zilog_lock);
2602 mutex_exit(&zd->zd_dirobj_lock);
2606 * Verify that we can't destroy an active pool, create an existing pool,
2607 * or create a pool with a bad vdev spec.
2611 ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2613 ztest_shared_opts_t *zo = &ztest_opts;
2618 * Attempt to create using a bad file.
2620 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2621 VERIFY3U(ENOENT, ==,
2622 spa_create("ztest_bad_file", nvroot, NULL, NULL));
2623 nvlist_free(nvroot);
2626 * Attempt to create using a bad mirror.
2628 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 2, 1);
2629 VERIFY3U(ENOENT, ==,
2630 spa_create("ztest_bad_mirror", nvroot, NULL, NULL));
2631 nvlist_free(nvroot);
2634 * Attempt to create an existing pool. It shouldn't matter
2635 * what's in the nvroot; we should fail with EEXIST.
2637 (void) rw_rdlock(&ztest_name_lock);
2638 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2639 VERIFY3U(EEXIST, ==, spa_create(zo->zo_pool, nvroot, NULL, NULL));
2640 nvlist_free(nvroot);
2641 VERIFY3U(0, ==, spa_open(zo->zo_pool, &spa, FTAG));
2642 VERIFY3U(EBUSY, ==, spa_destroy(zo->zo_pool));
2643 spa_close(spa, FTAG);
2645 (void) rw_unlock(&ztest_name_lock);
2650 ztest_spa_upgrade(ztest_ds_t *zd, uint64_t id)
2653 uint64_t initial_version = SPA_VERSION_INITIAL;
2654 uint64_t version, newversion;
2655 nvlist_t *nvroot, *props;
2658 mutex_enter(&ztest_vdev_lock);
2659 name = kmem_asprintf("%s_upgrade", ztest_opts.zo_pool);
2662 * Clean up from previous runs.
2664 (void) spa_destroy(name);
2666 nvroot = make_vdev_root(NULL, NULL, name, ztest_opts.zo_vdev_size, 0,
2667 0, ztest_opts.zo_raidz, ztest_opts.zo_mirrors, 1);
2670 * If we're configuring a RAIDZ device then make sure that the
2671 * the initial version is capable of supporting that feature.
2673 switch (ztest_opts.zo_raidz_parity) {
2676 initial_version = SPA_VERSION_INITIAL;
2679 initial_version = SPA_VERSION_RAIDZ2;
2682 initial_version = SPA_VERSION_RAIDZ3;
2687 * Create a pool with a spa version that can be upgraded. Pick
2688 * a value between initial_version and SPA_VERSION_BEFORE_FEATURES.
2691 version = ztest_random_spa_version(initial_version);
2692 } while (version > SPA_VERSION_BEFORE_FEATURES);
2694 props = fnvlist_alloc();
2695 fnvlist_add_uint64(props,
2696 zpool_prop_to_name(ZPOOL_PROP_VERSION), version);
2697 VERIFY3S(spa_create(name, nvroot, props, NULL), ==, 0);
2698 fnvlist_free(nvroot);
2699 fnvlist_free(props);
2701 VERIFY3S(spa_open(name, &spa, FTAG), ==, 0);
2702 VERIFY3U(spa_version(spa), ==, version);
2703 newversion = ztest_random_spa_version(version + 1);
2705 if (ztest_opts.zo_verbose >= 4) {
2706 (void) printf("upgrading spa version from %llu to %llu\n",
2707 (u_longlong_t)version, (u_longlong_t)newversion);
2710 spa_upgrade(spa, newversion);
2711 VERIFY3U(spa_version(spa), >, version);
2712 VERIFY3U(spa_version(spa), ==, fnvlist_lookup_uint64(spa->spa_config,
2713 zpool_prop_to_name(ZPOOL_PROP_VERSION)));
2714 spa_close(spa, FTAG);
2717 mutex_exit(&ztest_vdev_lock);
2721 vdev_lookup_by_path(vdev_t *vd, const char *path)
2726 if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
2729 for (c = 0; c < vd->vdev_children; c++)
2730 if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
2738 * Find the first available hole which can be used as a top-level.
2741 find_vdev_hole(spa_t *spa)
2743 vdev_t *rvd = spa->spa_root_vdev;
2746 ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
2748 for (c = 0; c < rvd->vdev_children; c++) {
2749 vdev_t *cvd = rvd->vdev_child[c];
2751 if (cvd->vdev_ishole)
2758 * Verify that vdev_add() works as expected.
2762 ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2764 ztest_shared_t *zs = ztest_shared;
2765 spa_t *spa = ztest_spa;
2771 mutex_enter(&ztest_vdev_lock);
2772 leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * ztest_opts.zo_raidz;
2774 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2776 ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2779 * If we have slogs then remove them 1/4 of the time.
2781 if (spa_has_slogs(spa) && ztest_random(4) == 0) {
2783 * Grab the guid from the head of the log class rotor.
2785 guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid;
2787 spa_config_exit(spa, SCL_VDEV, FTAG);
2790 * We have to grab the zs_name_lock as writer to
2791 * prevent a race between removing a slog (dmu_objset_find)
2792 * and destroying a dataset. Removing the slog will
2793 * grab a reference on the dataset which may cause
2794 * dsl_destroy_head() to fail with EBUSY thus
2795 * leaving the dataset in an inconsistent state.
2797 rw_wrlock(&ztest_name_lock);
2798 error = spa_vdev_remove(spa, guid, B_FALSE);
2799 rw_unlock(&ztest_name_lock);
2801 if (error && error != EEXIST)
2802 fatal(0, "spa_vdev_remove() = %d", error);
2804 spa_config_exit(spa, SCL_VDEV, FTAG);
2807 * Make 1/4 of the devices be log devices.
2809 nvroot = make_vdev_root(NULL, NULL, NULL,
2810 ztest_opts.zo_vdev_size, 0,
2811 ztest_random(4) == 0, ztest_opts.zo_raidz,
2814 error = spa_vdev_add(spa, nvroot);
2815 nvlist_free(nvroot);
2817 if (error == ENOSPC)
2818 ztest_record_enospc("spa_vdev_add");
2819 else if (error != 0)
2820 fatal(0, "spa_vdev_add() = %d", error);
2823 mutex_exit(&ztest_vdev_lock);
2827 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
2831 ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
2833 ztest_shared_t *zs = ztest_shared;
2834 spa_t *spa = ztest_spa;
2835 vdev_t *rvd = spa->spa_root_vdev;
2836 spa_aux_vdev_t *sav;
2842 path = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2844 if (ztest_random(2) == 0) {
2845 sav = &spa->spa_spares;
2846 aux = ZPOOL_CONFIG_SPARES;
2848 sav = &spa->spa_l2cache;
2849 aux = ZPOOL_CONFIG_L2CACHE;
2852 mutex_enter(&ztest_vdev_lock);
2854 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2856 if (sav->sav_count != 0 && ztest_random(4) == 0) {
2858 * Pick a random device to remove.
2860 guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
2863 * Find an unused device we can add.
2865 zs->zs_vdev_aux = 0;
2868 (void) snprintf(path, MAXPATHLEN, ztest_aux_template,
2869 ztest_opts.zo_dir, ztest_opts.zo_pool, aux,
2871 for (c = 0; c < sav->sav_count; c++)
2872 if (strcmp(sav->sav_vdevs[c]->vdev_path,
2875 if (c == sav->sav_count &&
2876 vdev_lookup_by_path(rvd, path) == NULL)
2882 spa_config_exit(spa, SCL_VDEV, FTAG);
2888 nvlist_t *nvroot = make_vdev_root(NULL, aux, NULL,
2889 (ztest_opts.zo_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
2890 error = spa_vdev_add(spa, nvroot);
2892 fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
2893 nvlist_free(nvroot);
2896 * Remove an existing device. Sometimes, dirty its
2897 * vdev state first to make sure we handle removal
2898 * of devices that have pending state changes.
2900 if (ztest_random(2) == 0)
2901 (void) vdev_online(spa, guid, 0, NULL);
2903 error = spa_vdev_remove(spa, guid, B_FALSE);
2904 if (error != 0 && error != EBUSY)
2905 fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
2908 mutex_exit(&ztest_vdev_lock);
2910 umem_free(path, MAXPATHLEN);
2914 * split a pool if it has mirror tlvdevs
2918 ztest_split_pool(ztest_ds_t *zd, uint64_t id)
2920 ztest_shared_t *zs = ztest_shared;
2921 spa_t *spa = ztest_spa;
2922 vdev_t *rvd = spa->spa_root_vdev;
2923 nvlist_t *tree, **child, *config, *split, **schild;
2924 uint_t c, children, schildren = 0, lastlogid = 0;
2927 mutex_enter(&ztest_vdev_lock);
2929 /* ensure we have a useable config; mirrors of raidz aren't supported */
2930 if (zs->zs_mirrors < 3 || ztest_opts.zo_raidz > 1) {
2931 mutex_exit(&ztest_vdev_lock);
2935 /* clean up the old pool, if any */
2936 (void) spa_destroy("splitp");
2938 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2940 /* generate a config from the existing config */
2941 mutex_enter(&spa->spa_props_lock);
2942 VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
2944 mutex_exit(&spa->spa_props_lock);
2946 VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2949 schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
2950 for (c = 0; c < children; c++) {
2951 vdev_t *tvd = rvd->vdev_child[c];
2955 if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
2956 VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
2958 VERIFY(nvlist_add_string(schild[schildren],
2959 ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
2960 VERIFY(nvlist_add_uint64(schild[schildren],
2961 ZPOOL_CONFIG_IS_HOLE, 1) == 0);
2963 lastlogid = schildren;
2968 VERIFY(nvlist_lookup_nvlist_array(child[c],
2969 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2970 VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
2973 /* OK, create a config that can be used to split */
2974 VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
2975 VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
2976 VDEV_TYPE_ROOT) == 0);
2977 VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
2978 lastlogid != 0 ? lastlogid : schildren) == 0);
2980 VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
2981 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
2983 for (c = 0; c < schildren; c++)
2984 nvlist_free(schild[c]);
2988 spa_config_exit(spa, SCL_VDEV, FTAG);
2990 (void) rw_wrlock(&ztest_name_lock);
2991 error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
2992 (void) rw_unlock(&ztest_name_lock);
2994 nvlist_free(config);
2997 (void) printf("successful split - results:\n");
2998 mutex_enter(&spa_namespace_lock);
2999 show_pool_stats(spa);
3000 show_pool_stats(spa_lookup("splitp"));
3001 mutex_exit(&spa_namespace_lock);
3005 mutex_exit(&ztest_vdev_lock);
3010 * Verify that we can attach and detach devices.
3014 ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
3016 ztest_shared_t *zs = ztest_shared;
3017 spa_t *spa = ztest_spa;
3018 spa_aux_vdev_t *sav = &spa->spa_spares;
3019 vdev_t *rvd = spa->spa_root_vdev;
3020 vdev_t *oldvd, *newvd, *pvd;
3024 uint64_t ashift = ztest_get_ashift();
3025 uint64_t oldguid, pguid;
3026 uint64_t oldsize, newsize;
3027 char *oldpath, *newpath;
3029 int oldvd_has_siblings = B_FALSE;
3030 int newvd_is_spare = B_FALSE;
3032 int error, expected_error;
3034 oldpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
3035 newpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
3037 mutex_enter(&ztest_vdev_lock);
3038 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
3040 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
3043 * Decide whether to do an attach or a replace.
3045 replacing = ztest_random(2);
3048 * Pick a random top-level vdev.
3050 top = ztest_random_vdev_top(spa, B_TRUE);
3053 * Pick a random leaf within it.
3055 leaf = ztest_random(leaves);
3060 oldvd = rvd->vdev_child[top];
3061 if (zs->zs_mirrors >= 1) {
3062 ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
3063 ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
3064 oldvd = oldvd->vdev_child[leaf / ztest_opts.zo_raidz];
3066 if (ztest_opts.zo_raidz > 1) {
3067 ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
3068 ASSERT(oldvd->vdev_children == ztest_opts.zo_raidz);
3069 oldvd = oldvd->vdev_child[leaf % ztest_opts.zo_raidz];
3073 * If we're already doing an attach or replace, oldvd may be a
3074 * mirror vdev -- in which case, pick a random child.
3076 while (oldvd->vdev_children != 0) {
3077 oldvd_has_siblings = B_TRUE;
3078 ASSERT(oldvd->vdev_children >= 2);
3079 oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
3082 oldguid = oldvd->vdev_guid;
3083 oldsize = vdev_get_min_asize(oldvd);
3084 oldvd_is_log = oldvd->vdev_top->vdev_islog;
3085 (void) strcpy(oldpath, oldvd->vdev_path);
3086 pvd = oldvd->vdev_parent;
3087 pguid = pvd->vdev_guid;
3090 * If oldvd has siblings, then half of the time, detach it.
3092 if (oldvd_has_siblings && ztest_random(2) == 0) {
3093 spa_config_exit(spa, SCL_VDEV, FTAG);
3094 error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
3095 if (error != 0 && error != ENODEV && error != EBUSY &&
3097 fatal(0, "detach (%s) returned %d", oldpath, error);
3102 * For the new vdev, choose with equal probability between the two
3103 * standard paths (ending in either 'a' or 'b') or a random hot spare.
3105 if (sav->sav_count != 0 && ztest_random(3) == 0) {
3106 newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
3107 newvd_is_spare = B_TRUE;
3108 (void) strcpy(newpath, newvd->vdev_path);
3110 (void) snprintf(newpath, MAXPATHLEN, ztest_dev_template,
3111 ztest_opts.zo_dir, ztest_opts.zo_pool,
3112 top * leaves + leaf);
3113 if (ztest_random(2) == 0)
3114 newpath[strlen(newpath) - 1] = 'b';
3115 newvd = vdev_lookup_by_path(rvd, newpath);
3119 newsize = vdev_get_min_asize(newvd);
3122 * Make newsize a little bigger or smaller than oldsize.
3123 * If it's smaller, the attach should fail.
3124 * If it's larger, and we're doing a replace,
3125 * we should get dynamic LUN growth when we're done.
3127 newsize = 10 * oldsize / (9 + ztest_random(3));
3131 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
3132 * unless it's a replace; in that case any non-replacing parent is OK.
3134 * If newvd is already part of the pool, it should fail with EBUSY.
3136 * If newvd is too small, it should fail with EOVERFLOW.
3138 if (pvd->vdev_ops != &vdev_mirror_ops &&
3139 pvd->vdev_ops != &vdev_root_ops && (!replacing ||
3140 pvd->vdev_ops == &vdev_replacing_ops ||
3141 pvd->vdev_ops == &vdev_spare_ops))
3142 expected_error = ENOTSUP;
3143 else if (newvd_is_spare && (!replacing || oldvd_is_log))
3144 expected_error = ENOTSUP;
3145 else if (newvd == oldvd)
3146 expected_error = replacing ? 0 : EBUSY;
3147 else if (vdev_lookup_by_path(rvd, newpath) != NULL)
3148 expected_error = EBUSY;
3149 else if (newsize < oldsize)
3150 expected_error = EOVERFLOW;
3151 else if (ashift > oldvd->vdev_top->vdev_ashift)
3152 expected_error = EDOM;
3156 spa_config_exit(spa, SCL_VDEV, FTAG);
3159 * Build the nvlist describing newpath.
3161 root = make_vdev_root(newpath, NULL, NULL, newvd == NULL ? newsize : 0,
3162 ashift, 0, 0, 0, 1);
3164 error = spa_vdev_attach(spa, oldguid, root, replacing);
3169 * If our parent was the replacing vdev, but the replace completed,
3170 * then instead of failing with ENOTSUP we may either succeed,
3171 * fail with ENODEV, or fail with EOVERFLOW.
3173 if (expected_error == ENOTSUP &&
3174 (error == 0 || error == ENODEV || error == EOVERFLOW))
3175 expected_error = error;
3178 * If someone grew the LUN, the replacement may be too small.
3180 if (error == EOVERFLOW || error == EBUSY)
3181 expected_error = error;
3183 /* XXX workaround 6690467 */
3184 if (error != expected_error && expected_error != EBUSY) {
3185 fatal(0, "attach (%s %llu, %s %llu, %d) "
3186 "returned %d, expected %d",
3187 oldpath, oldsize, newpath,
3188 newsize, replacing, error, expected_error);
3191 mutex_exit(&ztest_vdev_lock);
3193 umem_free(oldpath, MAXPATHLEN);
3194 umem_free(newpath, MAXPATHLEN);
3198 * Callback function which expands the physical size of the vdev.
3201 grow_vdev(vdev_t *vd, void *arg)
3203 ASSERTV(spa_t *spa = vd->vdev_spa);
3204 size_t *newsize = arg;
3208 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3209 ASSERT(vd->vdev_ops->vdev_op_leaf);
3211 if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
3214 fsize = lseek(fd, 0, SEEK_END);
3215 VERIFY(ftruncate(fd, *newsize) == 0);
3217 if (ztest_opts.zo_verbose >= 6) {
3218 (void) printf("%s grew from %lu to %lu bytes\n",
3219 vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
3226 * Callback function which expands a given vdev by calling vdev_online().
3230 online_vdev(vdev_t *vd, void *arg)
3232 spa_t *spa = vd->vdev_spa;
3233 vdev_t *tvd = vd->vdev_top;
3234 uint64_t guid = vd->vdev_guid;
3235 uint64_t generation = spa->spa_config_generation + 1;
3236 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
3239 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3240 ASSERT(vd->vdev_ops->vdev_op_leaf);
3242 /* Calling vdev_online will initialize the new metaslabs */
3243 spa_config_exit(spa, SCL_STATE, spa);
3244 error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
3245 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3248 * If vdev_online returned an error or the underlying vdev_open
3249 * failed then we abort the expand. The only way to know that
3250 * vdev_open fails is by checking the returned newstate.
3252 if (error || newstate != VDEV_STATE_HEALTHY) {
3253 if (ztest_opts.zo_verbose >= 5) {
3254 (void) printf("Unable to expand vdev, state %llu, "
3255 "error %d\n", (u_longlong_t)newstate, error);
3259 ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
3262 * Since we dropped the lock we need to ensure that we're
3263 * still talking to the original vdev. It's possible this
3264 * vdev may have been detached/replaced while we were
3265 * trying to online it.
3267 if (generation != spa->spa_config_generation) {
3268 if (ztest_opts.zo_verbose >= 5) {
3269 (void) printf("vdev configuration has changed, "
3270 "guid %llu, state %llu, expected gen %llu, "
3273 (u_longlong_t)tvd->vdev_state,
3274 (u_longlong_t)generation,
3275 (u_longlong_t)spa->spa_config_generation);
3283 * Traverse the vdev tree calling the supplied function.
3284 * We continue to walk the tree until we either have walked all
3285 * children or we receive a non-NULL return from the callback.
3286 * If a NULL callback is passed, then we just return back the first
3287 * leaf vdev we encounter.
3290 vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
3294 if (vd->vdev_ops->vdev_op_leaf) {
3298 return (func(vd, arg));
3301 for (c = 0; c < vd->vdev_children; c++) {
3302 vdev_t *cvd = vd->vdev_child[c];
3303 if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
3310 * Verify that dynamic LUN growth works as expected.
3314 ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
3316 spa_t *spa = ztest_spa;
3318 metaslab_class_t *mc;
3319 metaslab_group_t *mg;
3320 size_t psize, newsize;
3322 uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
3324 mutex_enter(&ztest_vdev_lock);
3325 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3327 top = ztest_random_vdev_top(spa, B_TRUE);
3329 tvd = spa->spa_root_vdev->vdev_child[top];
3332 old_ms_count = tvd->vdev_ms_count;
3333 old_class_space = metaslab_class_get_space(mc);
3336 * Determine the size of the first leaf vdev associated with
3337 * our top-level device.
3339 vd = vdev_walk_tree(tvd, NULL, NULL);
3340 ASSERT3P(vd, !=, NULL);
3341 ASSERT(vd->vdev_ops->vdev_op_leaf);
3343 psize = vd->vdev_psize;
3346 * We only try to expand the vdev if it's healthy, less than 4x its
3347 * original size, and it has a valid psize.
3349 if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
3350 psize == 0 || psize >= 4 * ztest_opts.zo_vdev_size) {
3351 spa_config_exit(spa, SCL_STATE, spa);
3352 mutex_exit(&ztest_vdev_lock);
3356 newsize = psize + psize / 8;
3357 ASSERT3U(newsize, >, psize);
3359 if (ztest_opts.zo_verbose >= 6) {
3360 (void) printf("Expanding LUN %s from %lu to %lu\n",
3361 vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
3365 * Growing the vdev is a two step process:
3366 * 1). expand the physical size (i.e. relabel)
3367 * 2). online the vdev to create the new metaslabs
3369 if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
3370 vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
3371 tvd->vdev_state != VDEV_STATE_HEALTHY) {
3372 if (ztest_opts.zo_verbose >= 5) {
3373 (void) printf("Could not expand LUN because "
3374 "the vdev configuration changed.\n");
3376 spa_config_exit(spa, SCL_STATE, spa);
3377 mutex_exit(&ztest_vdev_lock);
3381 spa_config_exit(spa, SCL_STATE, spa);
3384 * Expanding the LUN will update the config asynchronously,
3385 * thus we must wait for the async thread to complete any
3386 * pending tasks before proceeding.
3390 mutex_enter(&spa->spa_async_lock);
3391 done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
3392 mutex_exit(&spa->spa_async_lock);
3395 txg_wait_synced(spa_get_dsl(spa), 0);
3396 (void) poll(NULL, 0, 100);
3399 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3401 tvd = spa->spa_root_vdev->vdev_child[top];
3402 new_ms_count = tvd->vdev_ms_count;
3403 new_class_space = metaslab_class_get_space(mc);
3405 if (tvd->vdev_mg != mg || mg->mg_class != mc) {
3406 if (ztest_opts.zo_verbose >= 5) {
3407 (void) printf("Could not verify LUN expansion due to "
3408 "intervening vdev offline or remove.\n");
3410 spa_config_exit(spa, SCL_STATE, spa);
3411 mutex_exit(&ztest_vdev_lock);
3416 * Make sure we were able to grow the vdev.
3418 if (new_ms_count <= old_ms_count)
3419 fatal(0, "LUN expansion failed: ms_count %llu <= %llu\n",
3420 old_ms_count, new_ms_count);
3423 * Make sure we were able to grow the pool.
3425 if (new_class_space <= old_class_space)
3426 fatal(0, "LUN expansion failed: class_space %llu <= %llu\n",
3427 old_class_space, new_class_space);
3429 if (ztest_opts.zo_verbose >= 5) {
3430 char oldnumbuf[6], newnumbuf[6];
3432 nicenum(old_class_space, oldnumbuf);
3433 nicenum(new_class_space, newnumbuf);
3434 (void) printf("%s grew from %s to %s\n",
3435 spa->spa_name, oldnumbuf, newnumbuf);
3438 spa_config_exit(spa, SCL_STATE, spa);
3439 mutex_exit(&ztest_vdev_lock);
3443 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
3447 ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3450 * Create the objects common to all ztest datasets.
3452 VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
3453 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
3457 ztest_dataset_create(char *dsname)
3459 uint64_t zilset = ztest_random(100);
3460 int err = dmu_objset_create(dsname, DMU_OST_OTHER, 0,
3461 ztest_objset_create_cb, NULL);
3463 if (err || zilset < 80)
3466 if (ztest_opts.zo_verbose >= 5)
3467 (void) printf("Setting dataset %s to sync always\n", dsname);
3468 return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC,
3469 ZFS_SYNC_ALWAYS, B_FALSE));
3474 ztest_objset_destroy_cb(const char *name, void *arg)
3477 dmu_object_info_t doi;
3481 * Verify that the dataset contains a directory object.
3483 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_TRUE, FTAG, &os));
3484 error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
3485 if (error != ENOENT) {
3486 /* We could have crashed in the middle of destroying it */
3488 ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
3489 ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
3491 dmu_objset_disown(os, FTAG);
3494 * Destroy the dataset.
3496 if (strchr(name, '@') != NULL) {
3497 VERIFY0(dsl_destroy_snapshot(name, B_FALSE));
3499 VERIFY0(dsl_destroy_head(name));
3505 ztest_snapshot_create(char *osname, uint64_t id)
3507 char snapname[ZFS_MAX_DATASET_NAME_LEN];
3510 (void) snprintf(snapname, sizeof (snapname), "%llu", (u_longlong_t)id);
3512 error = dmu_objset_snapshot_one(osname, snapname);
3513 if (error == ENOSPC) {
3514 ztest_record_enospc(FTAG);
3517 if (error != 0 && error != EEXIST) {
3518 fatal(0, "ztest_snapshot_create(%s@%s) = %d", osname,
3525 ztest_snapshot_destroy(char *osname, uint64_t id)
3527 char snapname[ZFS_MAX_DATASET_NAME_LEN];
3530 (void) snprintf(snapname, sizeof (snapname), "%s@%llu", osname,
3533 error = dsl_destroy_snapshot(snapname, B_FALSE);
3534 if (error != 0 && error != ENOENT)
3535 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
3541 ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
3547 char name[ZFS_MAX_DATASET_NAME_LEN];
3551 zdtmp = umem_alloc(sizeof (ztest_ds_t), UMEM_NOFAIL);
3553 (void) rw_rdlock(&ztest_name_lock);
3555 (void) snprintf(name, sizeof (name), "%s/temp_%llu",
3556 ztest_opts.zo_pool, (u_longlong_t)id);
3559 * If this dataset exists from a previous run, process its replay log
3560 * half of the time. If we don't replay it, then dsl_destroy_head()
3561 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
3563 if (ztest_random(2) == 0 &&
3564 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) {
3565 ztest_zd_init(zdtmp, NULL, os);
3566 zil_replay(os, zdtmp, ztest_replay_vector);
3567 ztest_zd_fini(zdtmp);
3568 dmu_objset_disown(os, FTAG);
3572 * There may be an old instance of the dataset we're about to
3573 * create lying around from a previous run. If so, destroy it
3574 * and all of its snapshots.
3576 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
3577 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
3580 * Verify that the destroyed dataset is no longer in the namespace.
3582 VERIFY3U(ENOENT, ==, dmu_objset_own(name, DMU_OST_OTHER, B_TRUE,
3586 * Verify that we can create a new dataset.
3588 error = ztest_dataset_create(name);
3590 if (error == ENOSPC) {
3591 ztest_record_enospc(FTAG);
3594 fatal(0, "dmu_objset_create(%s) = %d", name, error);
3597 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os));
3599 ztest_zd_init(zdtmp, NULL, os);
3602 * Open the intent log for it.
3604 zilog = zil_open(os, ztest_get_data);
3607 * Put some objects in there, do a little I/O to them,
3608 * and randomly take a couple of snapshots along the way.
3610 iters = ztest_random(5);
3611 for (i = 0; i < iters; i++) {
3612 ztest_dmu_object_alloc_free(zdtmp, id);
3613 if (ztest_random(iters) == 0)
3614 (void) ztest_snapshot_create(name, i);
3618 * Verify that we cannot create an existing dataset.
3620 VERIFY3U(EEXIST, ==,
3621 dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL));
3624 * Verify that we can hold an objset that is also owned.
3626 VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
3627 dmu_objset_rele(os2, FTAG);
3630 * Verify that we cannot own an objset that is already owned.
3633 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2));
3636 dmu_objset_disown(os, FTAG);
3637 ztest_zd_fini(zdtmp);
3639 (void) rw_unlock(&ztest_name_lock);
3641 umem_free(zdtmp, sizeof (ztest_ds_t));
3645 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3648 ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
3650 (void) rw_rdlock(&ztest_name_lock);
3651 (void) ztest_snapshot_destroy(zd->zd_name, id);
3652 (void) ztest_snapshot_create(zd->zd_name, id);
3653 (void) rw_unlock(&ztest_name_lock);
3657 * Cleanup non-standard snapshots and clones.
3660 ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
3669 snap1name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3670 clone1name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3671 snap2name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3672 clone2name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3673 snap3name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3675 (void) snprintf(snap1name, ZFS_MAX_DATASET_NAME_LEN,
3676 "%s@s1_%llu", osname, (u_longlong_t)id);
3677 (void) snprintf(clone1name, ZFS_MAX_DATASET_NAME_LEN,
3678 "%s/c1_%llu", osname, (u_longlong_t)id);
3679 (void) snprintf(snap2name, ZFS_MAX_DATASET_NAME_LEN,
3680 "%s@s2_%llu", clone1name, (u_longlong_t)id);
3681 (void) snprintf(clone2name, ZFS_MAX_DATASET_NAME_LEN,
3682 "%s/c2_%llu", osname, (u_longlong_t)id);
3683 (void) snprintf(snap3name, ZFS_MAX_DATASET_NAME_LEN,
3684 "%s@s3_%llu", clone1name, (u_longlong_t)id);
3686 error = dsl_destroy_head(clone2name);
3687 if (error && error != ENOENT)
3688 fatal(0, "dsl_destroy_head(%s) = %d", clone2name, error);
3689 error = dsl_destroy_snapshot(snap3name, B_FALSE);
3690 if (error && error != ENOENT)
3691 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap3name, error);
3692 error = dsl_destroy_snapshot(snap2name, B_FALSE);
3693 if (error && error != ENOENT)
3694 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap2name, error);
3695 error = dsl_destroy_head(clone1name);
3696 if (error && error != ENOENT)
3697 fatal(0, "dsl_destroy_head(%s) = %d", clone1name, error);
3698 error = dsl_destroy_snapshot(snap1name, B_FALSE);
3699 if (error && error != ENOENT)
3700 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap1name, error);
3702 umem_free(snap1name, ZFS_MAX_DATASET_NAME_LEN);
3703 umem_free(clone1name, ZFS_MAX_DATASET_NAME_LEN);
3704 umem_free(snap2name, ZFS_MAX_DATASET_NAME_LEN);
3705 umem_free(clone2name, ZFS_MAX_DATASET_NAME_LEN);
3706 umem_free(snap3name, ZFS_MAX_DATASET_NAME_LEN);
3710 * Verify dsl_dataset_promote handles EBUSY
3713 ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
3721 char *osname = zd->zd_name;
3724 snap1name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3725 clone1name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3726 snap2name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3727 clone2name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3728 snap3name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3730 (void) rw_rdlock(&ztest_name_lock);
3732 ztest_dsl_dataset_cleanup(osname, id);
3734 (void) snprintf(snap1name, ZFS_MAX_DATASET_NAME_LEN,
3735 "%s@s1_%llu", osname, (u_longlong_t)id);
3736 (void) snprintf(clone1name, ZFS_MAX_DATASET_NAME_LEN,
3737 "%s/c1_%llu", osname, (u_longlong_t)id);
3738 (void) snprintf(snap2name, ZFS_MAX_DATASET_NAME_LEN,
3739 "%s@s2_%llu", clone1name, (u_longlong_t)id);
3740 (void) snprintf(clone2name, ZFS_MAX_DATASET_NAME_LEN,
3741 "%s/c2_%llu", osname, (u_longlong_t)id);
3742 (void) snprintf(snap3name, ZFS_MAX_DATASET_NAME_LEN,
3743 "%s@s3_%llu", clone1name, (u_longlong_t)id);
3745 error = dmu_objset_snapshot_one(osname, strchr(snap1name, '@') + 1);
3746 if (error && error != EEXIST) {
3747 if (error == ENOSPC) {
3748 ztest_record_enospc(FTAG);
3751 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
3754 error = dmu_objset_clone(clone1name, snap1name);
3756 if (error == ENOSPC) {
3757 ztest_record_enospc(FTAG);
3760 fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
3763 error = dmu_objset_snapshot_one(clone1name, strchr(snap2name, '@') + 1);
3764 if (error && error != EEXIST) {
3765 if (error == ENOSPC) {
3766 ztest_record_enospc(FTAG);
3769 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
3772 error = dmu_objset_snapshot_one(clone1name, strchr(snap3name, '@') + 1);
3773 if (error && error != EEXIST) {
3774 if (error == ENOSPC) {
3775 ztest_record_enospc(FTAG);
3778 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
3781 error = dmu_objset_clone(clone2name, snap3name);
3783 if (error == ENOSPC) {
3784 ztest_record_enospc(FTAG);
3787 fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
3790 error = dmu_objset_own(snap2name, DMU_OST_ANY, B_TRUE, FTAG, &os);
3792 fatal(0, "dmu_objset_own(%s) = %d", snap2name, error);
3793 error = dsl_dataset_promote(clone2name, NULL);
3794 if (error == ENOSPC) {
3795 dmu_objset_disown(os, FTAG);
3796 ztest_record_enospc(FTAG);
3800 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
3802 dmu_objset_disown(os, FTAG);
3805 ztest_dsl_dataset_cleanup(osname, id);
3807 (void) rw_unlock(&ztest_name_lock);
3809 umem_free(snap1name, ZFS_MAX_DATASET_NAME_LEN);
3810 umem_free(clone1name, ZFS_MAX_DATASET_NAME_LEN);
3811 umem_free(snap2name, ZFS_MAX_DATASET_NAME_LEN);
3812 umem_free(clone2name, ZFS_MAX_DATASET_NAME_LEN);
3813 umem_free(snap3name, ZFS_MAX_DATASET_NAME_LEN);
3816 #undef OD_ARRAY_SIZE
3817 #define OD_ARRAY_SIZE 4
3820 * Verify that dmu_object_{alloc,free} work as expected.
3823 ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
3830 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
3831 od = umem_alloc(size, UMEM_NOFAIL);
3832 batchsize = OD_ARRAY_SIZE;
3834 for (b = 0; b < batchsize; b++)
3835 ztest_od_init(od + b, id, FTAG, b, DMU_OT_UINT64_OTHER,
3839 * Destroy the previous batch of objects, create a new batch,
3840 * and do some I/O on the new objects.
3842 if (ztest_object_init(zd, od, size, B_TRUE) != 0)
3845 while (ztest_random(4 * batchsize) != 0)
3846 ztest_io(zd, od[ztest_random(batchsize)].od_object,
3847 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3849 umem_free(od, size);
3852 #undef OD_ARRAY_SIZE
3853 #define OD_ARRAY_SIZE 2
3856 * Verify that dmu_{read,write} work as expected.
3859 ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
3864 objset_t *os = zd->zd_os;
3865 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
3866 od = umem_alloc(size, UMEM_NOFAIL);
3868 int i, freeit, error;
3870 bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
3871 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3872 uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
3873 uint64_t regions = 997;
3874 uint64_t stride = 123456789ULL;
3875 uint64_t width = 40;
3876 int free_percent = 5;
3879 * This test uses two objects, packobj and bigobj, that are always
3880 * updated together (i.e. in the same tx) so that their contents are
3881 * in sync and can be compared. Their contents relate to each other
3882 * in a simple way: packobj is a dense array of 'bufwad' structures,
3883 * while bigobj is a sparse array of the same bufwads. Specifically,
3884 * for any index n, there are three bufwads that should be identical:
3886 * packobj, at offset n * sizeof (bufwad_t)
3887 * bigobj, at the head of the nth chunk
3888 * bigobj, at the tail of the nth chunk
3890 * The chunk size is arbitrary. It doesn't have to be a power of two,
3891 * and it doesn't have any relation to the object blocksize.
3892 * The only requirement is that it can hold at least two bufwads.
3894 * Normally, we write the bufwad to each of these locations.
3895 * However, free_percent of the time we instead write zeroes to
3896 * packobj and perform a dmu_free_range() on bigobj. By comparing
3897 * bigobj to packobj, we can verify that the DMU is correctly
3898 * tracking which parts of an object are allocated and free,
3899 * and that the contents of the allocated blocks are correct.
3903 * Read the directory info. If it's the first time, set things up.
3905 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, chunksize);
3906 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, 0,
3909 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
3910 umem_free(od, size);
3914 bigobj = od[0].od_object;
3915 packobj = od[1].od_object;
3916 chunksize = od[0].od_gen;
3917 ASSERT(chunksize == od[1].od_gen);
3920 * Prefetch a random chunk of the big object.
3921 * Our aim here is to get some async reads in flight
3922 * for blocks that we may free below; the DMU should
3923 * handle this race correctly.
3925 n = ztest_random(regions) * stride + ztest_random(width);
3926 s = 1 + ztest_random(2 * width - 1);
3927 dmu_prefetch(os, bigobj, 0, n * chunksize, s * chunksize,
3928 ZIO_PRIORITY_SYNC_READ);
3931 * Pick a random index and compute the offsets into packobj and bigobj.
3933 n = ztest_random(regions) * stride + ztest_random(width);
3934 s = 1 + ztest_random(width - 1);
3936 packoff = n * sizeof (bufwad_t);
3937 packsize = s * sizeof (bufwad_t);
3939 bigoff = n * chunksize;
3940 bigsize = s * chunksize;
3942 packbuf = umem_alloc(packsize, UMEM_NOFAIL);
3943 bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
3946 * free_percent of the time, free a range of bigobj rather than
3949 freeit = (ztest_random(100) < free_percent);
3952 * Read the current contents of our objects.
3954 error = dmu_read(os, packobj, packoff, packsize, packbuf,
3957 error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
3962 * Get a tx for the mods to both packobj and bigobj.
3964 tx = dmu_tx_create(os);
3966 dmu_tx_hold_write(tx, packobj, packoff, packsize);
3969 dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
3971 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
3973 /* This accounts for setting the checksum/compression. */
3974 dmu_tx_hold_bonus(tx, bigobj);
3976 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3978 umem_free(packbuf, packsize);
3979 umem_free(bigbuf, bigsize);
3980 umem_free(od, size);
3984 enum zio_checksum cksum;
3986 cksum = (enum zio_checksum)
3987 ztest_random_dsl_prop(ZFS_PROP_CHECKSUM);
3988 } while (cksum >= ZIO_CHECKSUM_LEGACY_FUNCTIONS);
3989 dmu_object_set_checksum(os, bigobj, cksum, tx);
3991 enum zio_compress comp;
3993 comp = (enum zio_compress)
3994 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION);
3995 } while (comp >= ZIO_COMPRESS_LEGACY_FUNCTIONS);
3996 dmu_object_set_compress(os, bigobj, comp, tx);
3999 * For each index from n to n + s, verify that the existing bufwad
4000 * in packobj matches the bufwads at the head and tail of the
4001 * corresponding chunk in bigobj. Then update all three bufwads
4002 * with the new values we want to write out.
4004 for (i = 0; i < s; i++) {
4006 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
4008 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
4010 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
4012 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
4013 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
4015 if (pack->bw_txg > txg)
4016 fatal(0, "future leak: got %llx, open txg is %llx",
4019 if (pack->bw_data != 0 && pack->bw_index != n + i)
4020 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4021 pack->bw_index, n, i);
4023 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
4024 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
4026 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
4027 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
4030 bzero(pack, sizeof (bufwad_t));
4032 pack->bw_index = n + i;
4034 pack->bw_data = 1 + ztest_random(-2ULL);
4041 * We've verified all the old bufwads, and made new ones.
4042 * Now write them out.
4044 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
4047 if (ztest_opts.zo_verbose >= 7) {
4048 (void) printf("freeing offset %llx size %llx"
4050 (u_longlong_t)bigoff,
4051 (u_longlong_t)bigsize,
4054 VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
4056 if (ztest_opts.zo_verbose >= 7) {
4057 (void) printf("writing offset %llx size %llx"
4059 (u_longlong_t)bigoff,
4060 (u_longlong_t)bigsize,
4063 dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
4069 * Sanity check the stuff we just wrote.
4072 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
4073 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
4075 VERIFY(0 == dmu_read(os, packobj, packoff,
4076 packsize, packcheck, DMU_READ_PREFETCH));
4077 VERIFY(0 == dmu_read(os, bigobj, bigoff,
4078 bigsize, bigcheck, DMU_READ_PREFETCH));
4080 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
4081 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
4083 umem_free(packcheck, packsize);
4084 umem_free(bigcheck, bigsize);
4087 umem_free(packbuf, packsize);
4088 umem_free(bigbuf, bigsize);
4089 umem_free(od, size);
4093 compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
4094 uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
4102 * For each index from n to n + s, verify that the existing bufwad
4103 * in packobj matches the bufwads at the head and tail of the
4104 * corresponding chunk in bigobj. Then update all three bufwads
4105 * with the new values we want to write out.
4107 for (i = 0; i < s; i++) {
4109 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
4111 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
4113 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
4115 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
4116 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
4118 if (pack->bw_txg > txg)
4119 fatal(0, "future leak: got %llx, open txg is %llx",
4122 if (pack->bw_data != 0 && pack->bw_index != n + i)
4123 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4124 pack->bw_index, n, i);
4126 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
4127 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
4129 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
4130 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
4132 pack->bw_index = n + i;
4134 pack->bw_data = 1 + ztest_random(-2ULL);
4141 #undef OD_ARRAY_SIZE
4142 #define OD_ARRAY_SIZE 2
4145 ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
4147 objset_t *os = zd->zd_os;
4154 bufwad_t *packbuf, *bigbuf;
4155 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
4156 uint64_t blocksize = ztest_random_blocksize();
4157 uint64_t chunksize = blocksize;
4158 uint64_t regions = 997;
4159 uint64_t stride = 123456789ULL;
4161 dmu_buf_t *bonus_db;
4162 arc_buf_t **bigbuf_arcbufs;
4163 dmu_object_info_t doi;
4165 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
4166 od = umem_alloc(size, UMEM_NOFAIL);
4169 * This test uses two objects, packobj and bigobj, that are always
4170 * updated together (i.e. in the same tx) so that their contents are
4171 * in sync and can be compared. Their contents relate to each other
4172 * in a simple way: packobj is a dense array of 'bufwad' structures,
4173 * while bigobj is a sparse array of the same bufwads. Specifically,
4174 * for any index n, there are three bufwads that should be identical:
4176 * packobj, at offset n * sizeof (bufwad_t)
4177 * bigobj, at the head of the nth chunk
4178 * bigobj, at the tail of the nth chunk
4180 * The chunk size is set equal to bigobj block size so that
4181 * dmu_assign_arcbuf() can be tested for object updates.
4185 * Read the directory info. If it's the first time, set things up.
4187 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0, 0);
4188 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, 0,
4192 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
4193 umem_free(od, size);
4197 bigobj = od[0].od_object;
4198 packobj = od[1].od_object;
4199 blocksize = od[0].od_blocksize;
4200 chunksize = blocksize;
4201 ASSERT(chunksize == od[1].od_gen);
4203 VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
4204 VERIFY(ISP2(doi.doi_data_block_size));
4205 VERIFY(chunksize == doi.doi_data_block_size);
4206 VERIFY(chunksize >= 2 * sizeof (bufwad_t));
4209 * Pick a random index and compute the offsets into packobj and bigobj.
4211 n = ztest_random(regions) * stride + ztest_random(width);
4212 s = 1 + ztest_random(width - 1);
4214 packoff = n * sizeof (bufwad_t);
4215 packsize = s * sizeof (bufwad_t);
4217 bigoff = n * chunksize;
4218 bigsize = s * chunksize;
4220 packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
4221 bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
4223 VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
4225 bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
4228 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
4229 * Iteration 1 test zcopy to already referenced dbufs.
4230 * Iteration 2 test zcopy to dirty dbuf in the same txg.
4231 * Iteration 3 test zcopy to dbuf dirty in previous txg.
4232 * Iteration 4 test zcopy when dbuf is no longer dirty.
4233 * Iteration 5 test zcopy when it can't be done.
4234 * Iteration 6 one more zcopy write.
4236 for (i = 0; i < 7; i++) {
4241 * In iteration 5 (i == 5) use arcbufs
4242 * that don't match bigobj blksz to test
4243 * dmu_assign_arcbuf() when it can't directly
4244 * assign an arcbuf to a dbuf.
4246 for (j = 0; j < s; j++) {
4247 if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
4249 dmu_request_arcbuf(bonus_db, chunksize);
4251 bigbuf_arcbufs[2 * j] =
4252 dmu_request_arcbuf(bonus_db, chunksize / 2);
4253 bigbuf_arcbufs[2 * j + 1] =
4254 dmu_request_arcbuf(bonus_db, chunksize / 2);
4259 * Get a tx for the mods to both packobj and bigobj.
4261 tx = dmu_tx_create(os);
4263 dmu_tx_hold_write(tx, packobj, packoff, packsize);
4264 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
4266 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4268 umem_free(packbuf, packsize);
4269 umem_free(bigbuf, bigsize);
4270 for (j = 0; j < s; j++) {
4272 chunksize < (SPA_MINBLOCKSIZE * 2)) {
4273 dmu_return_arcbuf(bigbuf_arcbufs[j]);
4276 bigbuf_arcbufs[2 * j]);
4278 bigbuf_arcbufs[2 * j + 1]);
4281 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4282 umem_free(od, size);
4283 dmu_buf_rele(bonus_db, FTAG);
4288 * 50% of the time don't read objects in the 1st iteration to
4289 * test dmu_assign_arcbuf() for the case when there're no
4290 * existing dbufs for the specified offsets.
4292 if (i != 0 || ztest_random(2) != 0) {
4293 error = dmu_read(os, packobj, packoff,
4294 packsize, packbuf, DMU_READ_PREFETCH);
4296 error = dmu_read(os, bigobj, bigoff, bigsize,
4297 bigbuf, DMU_READ_PREFETCH);
4300 compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
4304 * We've verified all the old bufwads, and made new ones.
4305 * Now write them out.
4307 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
4308 if (ztest_opts.zo_verbose >= 7) {
4309 (void) printf("writing offset %llx size %llx"
4311 (u_longlong_t)bigoff,
4312 (u_longlong_t)bigsize,
4315 for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
4317 if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
4318 bcopy((caddr_t)bigbuf + (off - bigoff),
4319 bigbuf_arcbufs[j]->b_data, chunksize);
4321 bcopy((caddr_t)bigbuf + (off - bigoff),
4322 bigbuf_arcbufs[2 * j]->b_data,
4324 bcopy((caddr_t)bigbuf + (off - bigoff) +
4326 bigbuf_arcbufs[2 * j + 1]->b_data,
4331 VERIFY(dmu_buf_hold(os, bigobj, off,
4332 FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0);
4334 if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
4335 dmu_assign_arcbuf(bonus_db, off,
4336 bigbuf_arcbufs[j], tx);
4338 dmu_assign_arcbuf(bonus_db, off,
4339 bigbuf_arcbufs[2 * j], tx);
4340 dmu_assign_arcbuf(bonus_db,
4341 off + chunksize / 2,
4342 bigbuf_arcbufs[2 * j + 1], tx);
4345 dmu_buf_rele(dbt, FTAG);
4351 * Sanity check the stuff we just wrote.
4354 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
4355 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
4357 VERIFY(0 == dmu_read(os, packobj, packoff,
4358 packsize, packcheck, DMU_READ_PREFETCH));
4359 VERIFY(0 == dmu_read(os, bigobj, bigoff,
4360 bigsize, bigcheck, DMU_READ_PREFETCH));
4362 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
4363 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
4365 umem_free(packcheck, packsize);
4366 umem_free(bigcheck, bigsize);
4369 txg_wait_open(dmu_objset_pool(os), 0);
4370 } else if (i == 3) {
4371 txg_wait_synced(dmu_objset_pool(os), 0);
4375 dmu_buf_rele(bonus_db, FTAG);
4376 umem_free(packbuf, packsize);
4377 umem_free(bigbuf, bigsize);
4378 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4379 umem_free(od, size);
4384 ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
4388 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4389 uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
4390 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4393 * Have multiple threads write to large offsets in an object
4394 * to verify that parallel writes to an object -- even to the
4395 * same blocks within the object -- doesn't cause any trouble.
4397 ztest_od_init(od, ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
4399 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0)
4402 while (ztest_random(10) != 0)
4403 ztest_io(zd, od->od_object, offset);
4405 umem_free(od, sizeof (ztest_od_t));
4409 ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
4412 uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
4413 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4414 uint64_t count = ztest_random(20) + 1;
4415 uint64_t blocksize = ztest_random_blocksize();
4418 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4420 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0, 0);
4422 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4423 !ztest_random(2)) != 0) {
4424 umem_free(od, sizeof (ztest_od_t));
4428 if (ztest_truncate(zd, od->od_object, offset, count * blocksize) != 0) {
4429 umem_free(od, sizeof (ztest_od_t));
4433 ztest_prealloc(zd, od->od_object, offset, count * blocksize);
4435 data = umem_zalloc(blocksize, UMEM_NOFAIL);
4437 while (ztest_random(count) != 0) {
4438 uint64_t randoff = offset + (ztest_random(count) * blocksize);
4439 if (ztest_write(zd, od->od_object, randoff, blocksize,
4442 while (ztest_random(4) != 0)
4443 ztest_io(zd, od->od_object, randoff);
4446 umem_free(data, blocksize);
4447 umem_free(od, sizeof (ztest_od_t));
4451 * Verify that zap_{create,destroy,add,remove,update} work as expected.
4453 #define ZTEST_ZAP_MIN_INTS 1
4454 #define ZTEST_ZAP_MAX_INTS 4
4455 #define ZTEST_ZAP_MAX_PROPS 1000
4458 ztest_zap(ztest_ds_t *zd, uint64_t id)
4460 objset_t *os = zd->zd_os;
4463 uint64_t txg, last_txg;
4464 uint64_t value[ZTEST_ZAP_MAX_INTS];
4465 uint64_t zl_ints, zl_intsize, prop;
4468 char propname[100], txgname[100];
4470 char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
4472 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4473 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0, 0);
4475 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4476 !ztest_random(2)) != 0)
4479 object = od->od_object;
4482 * Generate a known hash collision, and verify that
4483 * we can lookup and remove both entries.
4485 tx = dmu_tx_create(os);
4486 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4487 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4490 for (i = 0; i < 2; i++) {
4492 VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
4495 for (i = 0; i < 2; i++) {
4496 VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
4497 sizeof (uint64_t), 1, &value[i], tx));
4499 zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
4500 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4501 ASSERT3U(zl_ints, ==, 1);
4503 for (i = 0; i < 2; i++) {
4504 VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
4509 * Generate a buch of random entries.
4511 ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
4513 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4514 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4515 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4516 bzero(value, sizeof (value));
4520 * If these zap entries already exist, validate their contents.
4522 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4524 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4525 ASSERT3U(zl_ints, ==, 1);
4527 VERIFY(zap_lookup(os, object, txgname, zl_intsize,
4528 zl_ints, &last_txg) == 0);
4530 VERIFY(zap_length(os, object, propname, &zl_intsize,
4533 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4534 ASSERT3U(zl_ints, ==, ints);
4536 VERIFY(zap_lookup(os, object, propname, zl_intsize,
4537 zl_ints, value) == 0);
4539 for (i = 0; i < ints; i++) {
4540 ASSERT3U(value[i], ==, last_txg + object + i);
4543 ASSERT3U(error, ==, ENOENT);
4547 * Atomically update two entries in our zap object.
4548 * The first is named txg_%llu, and contains the txg
4549 * in which the property was last updated. The second
4550 * is named prop_%llu, and the nth element of its value
4551 * should be txg + object + n.
4553 tx = dmu_tx_create(os);
4554 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4555 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4560 fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
4562 for (i = 0; i < ints; i++)
4563 value[i] = txg + object + i;
4565 VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
4567 VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
4573 * Remove a random pair of entries.
4575 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4576 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4577 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4579 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4581 if (error == ENOENT)
4586 tx = dmu_tx_create(os);
4587 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4588 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4591 VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
4592 VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
4595 umem_free(od, sizeof (ztest_od_t));
4599 * Testcase to test the upgrading of a microzap to fatzap.
4602 ztest_fzap(ztest_ds_t *zd, uint64_t id)
4604 objset_t *os = zd->zd_os;
4606 uint64_t object, txg;
4609 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4610 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0, 0);
4612 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4613 !ztest_random(2)) != 0)
4615 object = od->od_object;
4618 * Add entries to this ZAP and make sure it spills over
4619 * and gets upgraded to a fatzap. Also, since we are adding
4620 * 2050 entries we should see ptrtbl growth and leaf-block split.
4622 for (i = 0; i < 2050; i++) {
4623 char name[ZFS_MAX_DATASET_NAME_LEN];
4628 (void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
4629 (u_longlong_t)id, (u_longlong_t)value);
4631 tx = dmu_tx_create(os);
4632 dmu_tx_hold_zap(tx, object, B_TRUE, name);
4633 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4636 error = zap_add(os, object, name, sizeof (uint64_t), 1,
4638 ASSERT(error == 0 || error == EEXIST);
4642 umem_free(od, sizeof (ztest_od_t));
4647 ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
4649 objset_t *os = zd->zd_os;
4651 uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
4653 int i, namelen, error;
4654 int micro = ztest_random(2);
4655 char name[20], string_value[20];
4658 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4659 ztest_od_init(od, ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER, 0, 0, 0);
4661 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
4662 umem_free(od, sizeof (ztest_od_t));
4666 object = od->od_object;
4669 * Generate a random name of the form 'xxx.....' where each
4670 * x is a random printable character and the dots are dots.
4671 * There are 94 such characters, and the name length goes from
4672 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
4674 namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
4676 for (i = 0; i < 3; i++)
4677 name[i] = '!' + ztest_random('~' - '!' + 1);
4678 for (; i < namelen - 1; i++)
4682 if ((namelen & 1) || micro) {
4683 wsize = sizeof (txg);
4689 data = string_value;
4693 VERIFY0(zap_count(os, object, &count));
4694 ASSERT(count != -1ULL);
4697 * Select an operation: length, lookup, add, update, remove.
4699 i = ztest_random(5);
4702 tx = dmu_tx_create(os);
4703 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4704 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4707 bcopy(name, string_value, namelen);
4711 bzero(string_value, namelen);
4717 error = zap_length(os, object, name, &zl_wsize, &zl_wc);
4719 ASSERT3U(wsize, ==, zl_wsize);
4720 ASSERT3U(wc, ==, zl_wc);
4722 ASSERT3U(error, ==, ENOENT);
4727 error = zap_lookup(os, object, name, wsize, wc, data);
4729 if (data == string_value &&
4730 bcmp(name, data, namelen) != 0)
4731 fatal(0, "name '%s' != val '%s' len %d",
4732 name, data, namelen);
4734 ASSERT3U(error, ==, ENOENT);
4739 error = zap_add(os, object, name, wsize, wc, data, tx);
4740 ASSERT(error == 0 || error == EEXIST);
4744 VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
4748 error = zap_remove(os, object, name, tx);
4749 ASSERT(error == 0 || error == ENOENT);
4756 umem_free(od, sizeof (ztest_od_t));
4760 * Commit callback data.
4762 typedef struct ztest_cb_data {
4763 list_node_t zcd_node;
4765 int zcd_expected_err;
4766 boolean_t zcd_added;
4767 boolean_t zcd_called;
4771 /* This is the actual commit callback function */
4773 ztest_commit_callback(void *arg, int error)
4775 ztest_cb_data_t *data = arg;
4776 uint64_t synced_txg;
4778 VERIFY(data != NULL);
4779 VERIFY3S(data->zcd_expected_err, ==, error);
4780 VERIFY(!data->zcd_called);
4782 synced_txg = spa_last_synced_txg(data->zcd_spa);
4783 if (data->zcd_txg > synced_txg)
4784 fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
4785 ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
4788 data->zcd_called = B_TRUE;
4790 if (error == ECANCELED) {
4791 ASSERT0(data->zcd_txg);
4792 ASSERT(!data->zcd_added);
4795 * The private callback data should be destroyed here, but
4796 * since we are going to check the zcd_called field after
4797 * dmu_tx_abort(), we will destroy it there.
4802 ASSERT(data->zcd_added);
4803 ASSERT3U(data->zcd_txg, !=, 0);
4805 (void) mutex_enter(&zcl.zcl_callbacks_lock);
4807 /* See if this cb was called more quickly */
4808 if ((synced_txg - data->zcd_txg) < zc_min_txg_delay)
4809 zc_min_txg_delay = synced_txg - data->zcd_txg;
4811 /* Remove our callback from the list */
4812 list_remove(&zcl.zcl_callbacks, data);
4814 (void) mutex_exit(&zcl.zcl_callbacks_lock);
4816 umem_free(data, sizeof (ztest_cb_data_t));
4819 /* Allocate and initialize callback data structure */
4820 static ztest_cb_data_t *
4821 ztest_create_cb_data(objset_t *os, uint64_t txg)
4823 ztest_cb_data_t *cb_data;
4825 cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
4827 cb_data->zcd_txg = txg;
4828 cb_data->zcd_spa = dmu_objset_spa(os);
4829 list_link_init(&cb_data->zcd_node);
4835 * Commit callback test.
4838 ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
4840 objset_t *os = zd->zd_os;
4843 ztest_cb_data_t *cb_data[3], *tmp_cb;
4844 uint64_t old_txg, txg;
4847 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4848 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
4850 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
4851 umem_free(od, sizeof (ztest_od_t));
4855 tx = dmu_tx_create(os);
4857 cb_data[0] = ztest_create_cb_data(os, 0);
4858 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
4860 dmu_tx_hold_write(tx, od->od_object, 0, sizeof (uint64_t));
4862 /* Every once in a while, abort the transaction on purpose */
4863 if (ztest_random(100) == 0)
4867 error = dmu_tx_assign(tx, TXG_NOWAIT);
4869 txg = error ? 0 : dmu_tx_get_txg(tx);
4871 cb_data[0]->zcd_txg = txg;
4872 cb_data[1] = ztest_create_cb_data(os, txg);
4873 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
4877 * It's not a strict requirement to call the registered
4878 * callbacks from inside dmu_tx_abort(), but that's what
4879 * it's supposed to happen in the current implementation
4880 * so we will check for that.
4882 for (i = 0; i < 2; i++) {
4883 cb_data[i]->zcd_expected_err = ECANCELED;
4884 VERIFY(!cb_data[i]->zcd_called);
4889 for (i = 0; i < 2; i++) {
4890 VERIFY(cb_data[i]->zcd_called);
4891 umem_free(cb_data[i], sizeof (ztest_cb_data_t));
4894 umem_free(od, sizeof (ztest_od_t));
4898 cb_data[2] = ztest_create_cb_data(os, txg);
4899 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
4902 * Read existing data to make sure there isn't a future leak.
4904 VERIFY(0 == dmu_read(os, od->od_object, 0, sizeof (uint64_t),
4905 &old_txg, DMU_READ_PREFETCH));
4908 fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
4911 dmu_write(os, od->od_object, 0, sizeof (uint64_t), &txg, tx);
4913 (void) mutex_enter(&zcl.zcl_callbacks_lock);
4916 * Since commit callbacks don't have any ordering requirement and since
4917 * it is theoretically possible for a commit callback to be called
4918 * after an arbitrary amount of time has elapsed since its txg has been
4919 * synced, it is difficult to reliably determine whether a commit
4920 * callback hasn't been called due to high load or due to a flawed
4923 * In practice, we will assume that if after a certain number of txgs a
4924 * commit callback hasn't been called, then most likely there's an
4925 * implementation bug..
4927 tmp_cb = list_head(&zcl.zcl_callbacks);
4928 if (tmp_cb != NULL &&
4929 tmp_cb->zcd_txg + ZTEST_COMMIT_CB_THRESH < txg) {
4930 fatal(0, "Commit callback threshold exceeded, oldest txg: %"
4931 PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
4935 * Let's find the place to insert our callbacks.
4937 * Even though the list is ordered by txg, it is possible for the
4938 * insertion point to not be the end because our txg may already be
4939 * quiescing at this point and other callbacks in the open txg
4940 * (from other objsets) may have sneaked in.
4942 tmp_cb = list_tail(&zcl.zcl_callbacks);
4943 while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
4944 tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
4946 /* Add the 3 callbacks to the list */
4947 for (i = 0; i < 3; i++) {
4949 list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
4951 list_insert_after(&zcl.zcl_callbacks, tmp_cb,
4954 cb_data[i]->zcd_added = B_TRUE;
4955 VERIFY(!cb_data[i]->zcd_called);
4957 tmp_cb = cb_data[i];
4962 (void) mutex_exit(&zcl.zcl_callbacks_lock);
4966 umem_free(od, sizeof (ztest_od_t));
4970 * Visit each object in the dataset. Verify that its properties
4971 * are consistent what was stored in the block tag when it was created,
4972 * and that its unused bonus buffer space has not been overwritten.
4975 ztest_verify_dnode_bt(ztest_ds_t *zd, uint64_t id)
4977 objset_t *os = zd->zd_os;
4981 for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
4982 ztest_block_tag_t *bt = NULL;
4983 dmu_object_info_t doi;
4986 if (dmu_bonus_hold(os, obj, FTAG, &db) != 0)
4989 dmu_object_info_from_db(db, &doi);
4990 if (doi.doi_bonus_size >= sizeof (*bt))
4991 bt = ztest_bt_bonus(db);
4993 if (bt && bt->bt_magic == BT_MAGIC) {
4994 ztest_bt_verify(bt, os, obj, doi.doi_dnodesize,
4995 bt->bt_offset, bt->bt_gen, bt->bt_txg,
4997 ztest_verify_unused_bonus(db, bt, obj, os, bt->bt_gen);
5000 dmu_buf_rele(db, FTAG);
5006 ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
5008 zfs_prop_t proplist[] = {
5010 ZFS_PROP_COMPRESSION,
5016 (void) rw_rdlock(&ztest_name_lock);
5018 for (p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
5019 (void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
5020 ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
5022 VERIFY0(ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_RECORDSIZE,
5023 ztest_random_blocksize(), (int)ztest_random(2)));
5025 (void) rw_unlock(&ztest_name_lock);
5030 ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
5032 nvlist_t *props = NULL;
5034 (void) rw_rdlock(&ztest_name_lock);
5036 (void) ztest_spa_prop_set_uint64(ZPOOL_PROP_DEDUPDITTO,
5037 ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
5039 VERIFY0(spa_prop_get(ztest_spa, &props));
5041 if (ztest_opts.zo_verbose >= 6)
5042 dump_nvlist(props, 4);
5046 (void) rw_unlock(&ztest_name_lock);
5050 user_release_one(const char *snapname, const char *holdname)
5052 nvlist_t *snaps, *holds;
5055 snaps = fnvlist_alloc();
5056 holds = fnvlist_alloc();
5057 fnvlist_add_boolean(holds, holdname);
5058 fnvlist_add_nvlist(snaps, snapname, holds);
5059 fnvlist_free(holds);
5060 error = dsl_dataset_user_release(snaps, NULL);
5061 fnvlist_free(snaps);
5066 * Test snapshot hold/release and deferred destroy.
5069 ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
5072 objset_t *os = zd->zd_os;
5076 char clonename[100];
5078 char osname[ZFS_MAX_DATASET_NAME_LEN];
5081 (void) rw_rdlock(&ztest_name_lock);
5083 dmu_objset_name(os, osname);
5085 (void) snprintf(snapname, sizeof (snapname), "sh1_%llu",
5087 (void) snprintf(fullname, sizeof (fullname), "%s@%s", osname, snapname);
5088 (void) snprintf(clonename, sizeof (clonename),
5089 "%s/ch1_%llu", osname, (u_longlong_t)id);
5090 (void) snprintf(tag, sizeof (tag), "tag_%llu", (u_longlong_t)id);
5093 * Clean up from any previous run.
5095 error = dsl_destroy_head(clonename);
5096 if (error != ENOENT)
5098 error = user_release_one(fullname, tag);
5099 if (error != ESRCH && error != ENOENT)
5101 error = dsl_destroy_snapshot(fullname, B_FALSE);
5102 if (error != ENOENT)
5106 * Create snapshot, clone it, mark snap for deferred destroy,
5107 * destroy clone, verify snap was also destroyed.
5109 error = dmu_objset_snapshot_one(osname, snapname);
5111 if (error == ENOSPC) {
5112 ztest_record_enospc("dmu_objset_snapshot");
5115 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
5118 error = dmu_objset_clone(clonename, fullname);
5120 if (error == ENOSPC) {
5121 ztest_record_enospc("dmu_objset_clone");
5124 fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
5127 error = dsl_destroy_snapshot(fullname, B_TRUE);
5129 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5133 error = dsl_destroy_head(clonename);
5135 fatal(0, "dsl_destroy_head(%s) = %d", clonename, error);
5137 error = dmu_objset_hold(fullname, FTAG, &origin);
5138 if (error != ENOENT)
5139 fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
5142 * Create snapshot, add temporary hold, verify that we can't
5143 * destroy a held snapshot, mark for deferred destroy,
5144 * release hold, verify snapshot was destroyed.
5146 error = dmu_objset_snapshot_one(osname, snapname);
5148 if (error == ENOSPC) {
5149 ztest_record_enospc("dmu_objset_snapshot");
5152 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
5155 holds = fnvlist_alloc();
5156 fnvlist_add_string(holds, fullname, tag);
5157 error = dsl_dataset_user_hold(holds, 0, NULL);
5158 fnvlist_free(holds);
5160 if (error == ENOSPC) {
5161 ztest_record_enospc("dsl_dataset_user_hold");
5164 fatal(0, "dsl_dataset_user_hold(%s, %s) = %u",
5165 fullname, tag, error);
5168 error = dsl_destroy_snapshot(fullname, B_FALSE);
5169 if (error != EBUSY) {
5170 fatal(0, "dsl_destroy_snapshot(%s, B_FALSE) = %d",
5174 error = dsl_destroy_snapshot(fullname, B_TRUE);
5176 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5180 error = user_release_one(fullname, tag);
5182 fatal(0, "user_release_one(%s, %s) = %d", fullname, tag, error);
5184 VERIFY3U(dmu_objset_hold(fullname, FTAG, &origin), ==, ENOENT);
5187 (void) rw_unlock(&ztest_name_lock);
5191 * Inject random faults into the on-disk data.
5195 ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
5197 ztest_shared_t *zs = ztest_shared;
5198 spa_t *spa = ztest_spa;
5202 uint64_t bad = 0x1990c0ffeedecadeull;
5207 int bshift = SPA_MAXBLOCKSHIFT + 2; /* don't scrog all labels */
5213 boolean_t islog = B_FALSE;
5215 path0 = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
5216 pathrand = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
5218 mutex_enter(&ztest_vdev_lock);
5219 maxfaults = MAXFAULTS();
5220 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
5221 mirror_save = zs->zs_mirrors;
5222 mutex_exit(&ztest_vdev_lock);
5224 ASSERT(leaves >= 1);
5227 * Grab the name lock as reader. There are some operations
5228 * which don't like to have their vdevs changed while
5229 * they are in progress (i.e. spa_change_guid). Those
5230 * operations will have grabbed the name lock as writer.
5232 (void) rw_rdlock(&ztest_name_lock);
5235 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
5237 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5239 if (ztest_random(2) == 0) {
5241 * Inject errors on a normal data device or slog device.
5243 top = ztest_random_vdev_top(spa, B_TRUE);
5244 leaf = ztest_random(leaves) + zs->zs_splits;
5247 * Generate paths to the first leaf in this top-level vdev,
5248 * and to the random leaf we selected. We'll induce transient
5249 * write failures and random online/offline activity on leaf 0,
5250 * and we'll write random garbage to the randomly chosen leaf.
5252 (void) snprintf(path0, MAXPATHLEN, ztest_dev_template,
5253 ztest_opts.zo_dir, ztest_opts.zo_pool,
5254 top * leaves + zs->zs_splits);
5255 (void) snprintf(pathrand, MAXPATHLEN, ztest_dev_template,
5256 ztest_opts.zo_dir, ztest_opts.zo_pool,
5257 top * leaves + leaf);
5259 vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
5260 if (vd0 != NULL && vd0->vdev_top->vdev_islog)
5264 * If the top-level vdev needs to be resilvered
5265 * then we only allow faults on the device that is
5268 if (vd0 != NULL && maxfaults != 1 &&
5269 (!vdev_resilver_needed(vd0->vdev_top, NULL, NULL) ||
5270 vd0->vdev_resilver_txg != 0)) {
5272 * Make vd0 explicitly claim to be unreadable,
5273 * or unwriteable, or reach behind its back
5274 * and close the underlying fd. We can do this if
5275 * maxfaults == 0 because we'll fail and reexecute,
5276 * and we can do it if maxfaults >= 2 because we'll
5277 * have enough redundancy. If maxfaults == 1, the
5278 * combination of this with injection of random data
5279 * corruption below exceeds the pool's fault tolerance.
5281 vdev_file_t *vf = vd0->vdev_tsd;
5283 if (vf != NULL && ztest_random(3) == 0) {
5284 (void) close(vf->vf_vnode->v_fd);
5285 vf->vf_vnode->v_fd = -1;
5286 } else if (ztest_random(2) == 0) {
5287 vd0->vdev_cant_read = B_TRUE;
5289 vd0->vdev_cant_write = B_TRUE;
5291 guid0 = vd0->vdev_guid;
5295 * Inject errors on an l2cache device.
5297 spa_aux_vdev_t *sav = &spa->spa_l2cache;
5299 if (sav->sav_count == 0) {
5300 spa_config_exit(spa, SCL_STATE, FTAG);
5301 (void) rw_unlock(&ztest_name_lock);
5304 vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
5305 guid0 = vd0->vdev_guid;
5306 (void) strcpy(path0, vd0->vdev_path);
5307 (void) strcpy(pathrand, vd0->vdev_path);
5311 maxfaults = INT_MAX; /* no limit on cache devices */
5314 spa_config_exit(spa, SCL_STATE, FTAG);
5315 (void) rw_unlock(&ztest_name_lock);
5318 * If we can tolerate two or more faults, or we're dealing
5319 * with a slog, randomly online/offline vd0.
5321 if ((maxfaults >= 2 || islog) && guid0 != 0) {
5322 if (ztest_random(10) < 6) {
5323 int flags = (ztest_random(2) == 0 ?
5324 ZFS_OFFLINE_TEMPORARY : 0);
5327 * We have to grab the zs_name_lock as writer to
5328 * prevent a race between offlining a slog and
5329 * destroying a dataset. Offlining the slog will
5330 * grab a reference on the dataset which may cause
5331 * dsl_destroy_head() to fail with EBUSY thus
5332 * leaving the dataset in an inconsistent state.
5335 (void) rw_wrlock(&ztest_name_lock);
5337 VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
5340 (void) rw_unlock(&ztest_name_lock);
5343 * Ideally we would like to be able to randomly
5344 * call vdev_[on|off]line without holding locks
5345 * to force unpredictable failures but the side
5346 * effects of vdev_[on|off]line prevent us from
5347 * doing so. We grab the ztest_vdev_lock here to
5348 * prevent a race between injection testing and
5351 mutex_enter(&ztest_vdev_lock);
5352 (void) vdev_online(spa, guid0, 0, NULL);
5353 mutex_exit(&ztest_vdev_lock);
5361 * We have at least single-fault tolerance, so inject data corruption.
5363 fd = open(pathrand, O_RDWR);
5365 if (fd == -1) /* we hit a gap in the device namespace */
5368 fsize = lseek(fd, 0, SEEK_END);
5370 while (--iters != 0) {
5372 * The offset must be chosen carefully to ensure that
5373 * we do not inject a given logical block with errors
5374 * on two different leaf devices, because ZFS can not
5375 * tolerate that (if maxfaults==1).
5377 * We divide each leaf into chunks of size
5378 * (# leaves * SPA_MAXBLOCKSIZE * 4). Within each chunk
5379 * there is a series of ranges to which we can inject errors.
5380 * Each range can accept errors on only a single leaf vdev.
5381 * The error injection ranges are separated by ranges
5382 * which we will not inject errors on any device (DMZs).
5383 * Each DMZ must be large enough such that a single block
5384 * can not straddle it, so that a single block can not be
5385 * a target in two different injection ranges (on different
5388 * For example, with 3 leaves, each chunk looks like:
5389 * 0 to 32M: injection range for leaf 0
5390 * 32M to 64M: DMZ - no injection allowed
5391 * 64M to 96M: injection range for leaf 1
5392 * 96M to 128M: DMZ - no injection allowed
5393 * 128M to 160M: injection range for leaf 2
5394 * 160M to 192M: DMZ - no injection allowed
5396 offset = ztest_random(fsize / (leaves << bshift)) *
5397 (leaves << bshift) + (leaf << bshift) +
5398 (ztest_random(1ULL << (bshift - 1)) & -8ULL);
5400 if (offset >= fsize)
5403 mutex_enter(&ztest_vdev_lock);
5404 if (mirror_save != zs->zs_mirrors) {
5405 mutex_exit(&ztest_vdev_lock);
5410 if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
5411 fatal(1, "can't inject bad word at 0x%llx in %s",
5414 mutex_exit(&ztest_vdev_lock);
5416 if (ztest_opts.zo_verbose >= 7)
5417 (void) printf("injected bad word into %s,"
5418 " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
5423 umem_free(path0, MAXPATHLEN);
5424 umem_free(pathrand, MAXPATHLEN);
5428 * Verify that DDT repair works as expected.
5431 ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
5433 ztest_shared_t *zs = ztest_shared;
5434 spa_t *spa = ztest_spa;
5435 objset_t *os = zd->zd_os;
5437 uint64_t object, blocksize, txg, pattern, psize;
5438 enum zio_checksum checksum = spa_dedup_checksum(spa);
5443 int copies = 2 * ZIO_DEDUPDITTO_MIN;
5446 blocksize = ztest_random_blocksize();
5447 blocksize = MIN(blocksize, 2048); /* because we write so many */
5449 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
5450 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0, 0);
5452 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
5453 umem_free(od, sizeof (ztest_od_t));
5458 * Take the name lock as writer to prevent anyone else from changing
5459 * the pool and dataset properies we need to maintain during this test.
5461 (void) rw_wrlock(&ztest_name_lock);
5463 if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
5465 ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
5467 (void) rw_unlock(&ztest_name_lock);
5468 umem_free(od, sizeof (ztest_od_t));
5472 object = od[0].od_object;
5473 blocksize = od[0].od_blocksize;
5474 pattern = zs->zs_guid ^ dmu_objset_fsid_guid(os);
5476 ASSERT(object != 0);
5478 tx = dmu_tx_create(os);
5479 dmu_tx_hold_write(tx, object, 0, copies * blocksize);
5480 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
5482 (void) rw_unlock(&ztest_name_lock);
5483 umem_free(od, sizeof (ztest_od_t));
5488 * Write all the copies of our block.
5490 for (i = 0; i < copies; i++) {
5491 uint64_t offset = i * blocksize;
5492 int error = dmu_buf_hold(os, object, offset, FTAG, &db,
5493 DMU_READ_NO_PREFETCH);
5495 fatal(B_FALSE, "dmu_buf_hold(%p, %llu, %llu) = %u",
5496 os, (long long)object, (long long) offset, error);
5498 ASSERT(db->db_offset == offset);
5499 ASSERT(db->db_size == blocksize);
5500 ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
5501 ztest_pattern_match(db->db_data, db->db_size, 0ULL));
5502 dmu_buf_will_fill(db, tx);
5503 ztest_pattern_set(db->db_data, db->db_size, pattern);
5504 dmu_buf_rele(db, FTAG);
5508 txg_wait_synced(spa_get_dsl(spa), txg);
5511 * Find out what block we got.
5513 VERIFY0(dmu_buf_hold(os, object, 0, FTAG, &db,
5514 DMU_READ_NO_PREFETCH));
5515 blk = *((dmu_buf_impl_t *)db)->db_blkptr;
5516 dmu_buf_rele(db, FTAG);
5519 * Damage the block. Dedup-ditto will save us when we read it later.
5521 psize = BP_GET_PSIZE(&blk);
5522 buf = zio_buf_alloc(psize);
5523 ztest_pattern_set(buf, psize, ~pattern);
5525 (void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
5526 buf, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
5527 ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
5529 zio_buf_free(buf, psize);
5531 (void) rw_unlock(&ztest_name_lock);
5532 umem_free(od, sizeof (ztest_od_t));
5540 ztest_scrub(ztest_ds_t *zd, uint64_t id)
5542 spa_t *spa = ztest_spa;
5544 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5545 (void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
5546 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5550 * Change the guid for the pool.
5554 ztest_reguid(ztest_ds_t *zd, uint64_t id)
5556 spa_t *spa = ztest_spa;
5557 uint64_t orig, load;
5560 orig = spa_guid(spa);
5561 load = spa_load_guid(spa);
5563 (void) rw_wrlock(&ztest_name_lock);
5564 error = spa_change_guid(spa);
5565 (void) rw_unlock(&ztest_name_lock);
5570 if (ztest_opts.zo_verbose >= 4) {
5571 (void) printf("Changed guid old %llu -> %llu\n",
5572 (u_longlong_t)orig, (u_longlong_t)spa_guid(spa));
5575 VERIFY3U(orig, !=, spa_guid(spa));
5576 VERIFY3U(load, ==, spa_load_guid(spa));
5580 * Rename the pool to a different name and then rename it back.
5584 ztest_spa_rename(ztest_ds_t *zd, uint64_t id)
5586 char *oldname, *newname;
5589 (void) rw_wrlock(&ztest_name_lock);
5591 oldname = ztest_opts.zo_pool;
5592 newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
5593 (void) strcpy(newname, oldname);
5594 (void) strcat(newname, "_tmp");
5599 VERIFY3U(0, ==, spa_rename(oldname, newname));
5602 * Try to open it under the old name, which shouldn't exist
5604 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5607 * Open it under the new name and make sure it's still the same spa_t.
5609 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
5611 ASSERT(spa == ztest_spa);
5612 spa_close(spa, FTAG);
5615 * Rename it back to the original
5617 VERIFY3U(0, ==, spa_rename(newname, oldname));
5620 * Make sure it can still be opened
5622 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
5624 ASSERT(spa == ztest_spa);
5625 spa_close(spa, FTAG);
5627 umem_free(newname, strlen(newname) + 1);
5629 (void) rw_unlock(&ztest_name_lock);
5633 ztest_fletcher(ztest_ds_t *zd, uint64_t id)
5635 hrtime_t end = gethrtime() + NANOSEC;
5637 while (gethrtime() <= end) {
5638 int run_count = 100;
5644 zio_cksum_t zc_ref_byteswap;
5646 size = ztest_random_blocksize();
5647 buf = umem_alloc(size, UMEM_NOFAIL);
5649 for (i = 0, ptr = buf; i < size / sizeof (*ptr); i++, ptr++)
5650 *ptr = ztest_random(UINT_MAX);
5652 VERIFY0(fletcher_4_impl_set("scalar"));
5653 fletcher_4_native(buf, size, &zc_ref);
5654 fletcher_4_byteswap(buf, size, &zc_ref_byteswap);
5656 VERIFY0(fletcher_4_impl_set("cycle"));
5657 while (run_count-- > 0) {
5659 zio_cksum_t zc_byteswap;
5661 fletcher_4_byteswap(buf, size, &zc_byteswap);
5662 fletcher_4_native(buf, size, &zc);
5664 VERIFY0(bcmp(&zc, &zc_ref, sizeof (zc)));
5665 VERIFY0(bcmp(&zc_byteswap, &zc_ref_byteswap,
5666 sizeof (zc_byteswap)));
5669 umem_free(buf, size);
5674 ztest_check_path(char *path)
5677 /* return true on success */
5678 return (!stat(path, &s));
5682 ztest_get_zdb_bin(char *bin, int len)
5686 * Try to use ZDB_PATH and in-tree zdb path. If not successful, just
5687 * let popen to search through PATH.
5689 if ((zdb_path = getenv("ZDB_PATH"))) {
5690 strlcpy(bin, zdb_path, len); /* In env */
5691 if (!ztest_check_path(bin)) {
5692 ztest_dump_core = 0;
5693 fatal(1, "invalid ZDB_PATH '%s'", bin);
5698 VERIFY(realpath(getexecname(), bin) != NULL);
5699 if (strstr(bin, "/ztest/")) {
5700 strstr(bin, "/ztest/")[0] = '\0'; /* In-tree */
5701 strcat(bin, "/zdb/zdb");
5702 if (ztest_check_path(bin))
5709 * Verify pool integrity by running zdb.
5712 ztest_run_zdb(char *pool)
5718 const int len = MAXPATHLEN + MAXNAMELEN + 20;
5721 bin = umem_alloc(len, UMEM_NOFAIL);
5722 zdb = umem_alloc(len, UMEM_NOFAIL);
5723 zbuf = umem_alloc(1024, UMEM_NOFAIL);
5725 ztest_get_zdb_bin(bin, len);
5728 "%s -bcc%s%s -d -U %s %s",
5730 ztest_opts.zo_verbose >= 3 ? "s" : "",
5731 ztest_opts.zo_verbose >= 4 ? "v" : "",
5735 if (ztest_opts.zo_verbose >= 5)
5736 (void) printf("Executing %s\n", strstr(zdb, "zdb "));
5738 fp = popen(zdb, "r");
5740 while (fgets(zbuf, 1024, fp) != NULL)
5741 if (ztest_opts.zo_verbose >= 3)
5742 (void) printf("%s", zbuf);
5744 status = pclose(fp);
5749 ztest_dump_core = 0;
5750 if (WIFEXITED(status))
5751 fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
5753 fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
5755 umem_free(bin, len);
5756 umem_free(zdb, len);
5757 umem_free(zbuf, 1024);
5761 ztest_walk_pool_directory(char *header)
5765 if (ztest_opts.zo_verbose >= 6)
5766 (void) printf("%s\n", header);
5768 mutex_enter(&spa_namespace_lock);
5769 while ((spa = spa_next(spa)) != NULL)
5770 if (ztest_opts.zo_verbose >= 6)
5771 (void) printf("\t%s\n", spa_name(spa));
5772 mutex_exit(&spa_namespace_lock);
5776 ztest_spa_import_export(char *oldname, char *newname)
5778 nvlist_t *config, *newconfig;
5783 if (ztest_opts.zo_verbose >= 4) {
5784 (void) printf("import/export: old = %s, new = %s\n",
5789 * Clean up from previous runs.
5791 (void) spa_destroy(newname);
5794 * Get the pool's configuration and guid.
5796 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
5799 * Kick off a scrub to tickle scrub/export races.
5801 if (ztest_random(2) == 0)
5802 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5804 pool_guid = spa_guid(spa);
5805 spa_close(spa, FTAG);
5807 ztest_walk_pool_directory("pools before export");
5812 VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
5814 ztest_walk_pool_directory("pools after export");
5819 newconfig = spa_tryimport(config);
5820 ASSERT(newconfig != NULL);
5821 nvlist_free(newconfig);
5824 * Import it under the new name.
5826 error = spa_import(newname, config, NULL, 0);
5828 dump_nvlist(config, 0);
5829 fatal(B_FALSE, "couldn't import pool %s as %s: error %u",
5830 oldname, newname, error);
5833 ztest_walk_pool_directory("pools after import");
5836 * Try to import it again -- should fail with EEXIST.
5838 VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL, 0));
5841 * Try to import it under a different name -- should fail with EEXIST.
5843 VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL, 0));
5846 * Verify that the pool is no longer visible under the old name.
5848 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5851 * Verify that we can open and close the pool using the new name.
5853 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
5854 ASSERT(pool_guid == spa_guid(spa));
5855 spa_close(spa, FTAG);
5857 nvlist_free(config);
5861 ztest_resume(spa_t *spa)
5863 if (spa_suspended(spa) && ztest_opts.zo_verbose >= 6)
5864 (void) printf("resuming from suspended state\n");
5865 spa_vdev_state_enter(spa, SCL_NONE);
5866 vdev_clear(spa, NULL);
5867 (void) spa_vdev_state_exit(spa, NULL, 0);
5868 (void) zio_resume(spa);
5872 ztest_resume_thread(void *arg)
5876 while (!ztest_exiting) {
5877 if (spa_suspended(spa))
5879 (void) poll(NULL, 0, 100);
5891 ztest_deadman_alarm(int sig)
5893 fatal(0, "failed to complete within %d seconds of deadline", GRACE);
5898 ztest_execute(int test, ztest_info_t *zi, uint64_t id)
5900 ztest_ds_t *zd = &ztest_ds[id % ztest_opts.zo_datasets];
5901 ztest_shared_callstate_t *zc = ZTEST_GET_SHARED_CALLSTATE(test);
5902 hrtime_t functime = gethrtime();
5905 for (i = 0; i < zi->zi_iters; i++)
5906 zi->zi_func(zd, id);
5908 functime = gethrtime() - functime;
5910 atomic_add_64(&zc->zc_count, 1);
5911 atomic_add_64(&zc->zc_time, functime);
5913 if (ztest_opts.zo_verbose >= 4)
5914 (void) printf("%6.2f sec in %s\n",
5915 (double)functime / NANOSEC, zi->zi_funcname);
5919 ztest_thread(void *arg)
5922 uint64_t id = (uintptr_t)arg;
5923 ztest_shared_t *zs = ztest_shared;
5927 ztest_shared_callstate_t *zc;
5929 while ((now = gethrtime()) < zs->zs_thread_stop) {
5931 * See if it's time to force a crash.
5933 if (now > zs->zs_thread_kill)
5937 * If we're getting ENOSPC with some regularity, stop.
5939 if (zs->zs_enospc_count > 10)
5943 * Pick a random function to execute.
5945 rand = ztest_random(ZTEST_FUNCS);
5946 zi = &ztest_info[rand];
5947 zc = ZTEST_GET_SHARED_CALLSTATE(rand);
5948 call_next = zc->zc_next;
5950 if (now >= call_next &&
5951 atomic_cas_64(&zc->zc_next, call_next, call_next +
5952 ztest_random(2 * zi->zi_interval[0] + 1)) == call_next) {
5953 ztest_execute(rand, zi, id);
5963 ztest_dataset_name(char *dsname, char *pool, int d)
5965 (void) snprintf(dsname, ZFS_MAX_DATASET_NAME_LEN, "%s/ds_%d", pool, d);
5969 ztest_dataset_destroy(int d)
5971 char name[ZFS_MAX_DATASET_NAME_LEN];
5974 ztest_dataset_name(name, ztest_opts.zo_pool, d);
5976 if (ztest_opts.zo_verbose >= 3)
5977 (void) printf("Destroying %s to free up space\n", name);
5980 * Cleanup any non-standard clones and snapshots. In general,
5981 * ztest thread t operates on dataset (t % zopt_datasets),
5982 * so there may be more than one thing to clean up.
5984 for (t = d; t < ztest_opts.zo_threads;
5985 t += ztest_opts.zo_datasets)
5986 ztest_dsl_dataset_cleanup(name, t);
5988 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
5989 DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
5993 ztest_dataset_dirobj_verify(ztest_ds_t *zd)
5995 uint64_t usedobjs, dirobjs, scratch;
5998 * ZTEST_DIROBJ is the object directory for the entire dataset.
5999 * Therefore, the number of objects in use should equal the
6000 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
6001 * If not, we have an object leak.
6003 * Note that we can only check this in ztest_dataset_open(),
6004 * when the open-context and syncing-context values agree.
6005 * That's because zap_count() returns the open-context value,
6006 * while dmu_objset_space() returns the rootbp fill count.
6008 VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
6009 dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
6010 ASSERT3U(dirobjs + 1, ==, usedobjs);
6014 ztest_dataset_open(int d)
6016 ztest_ds_t *zd = &ztest_ds[d];
6017 uint64_t committed_seq = ZTEST_GET_SHARED_DS(d)->zd_seq;
6020 char name[ZFS_MAX_DATASET_NAME_LEN];
6023 ztest_dataset_name(name, ztest_opts.zo_pool, d);
6025 (void) rw_rdlock(&ztest_name_lock);
6027 error = ztest_dataset_create(name);
6028 if (error == ENOSPC) {
6029 (void) rw_unlock(&ztest_name_lock);
6030 ztest_record_enospc(FTAG);
6033 ASSERT(error == 0 || error == EEXIST);
6035 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, zd, &os));
6036 (void) rw_unlock(&ztest_name_lock);
6038 ztest_zd_init(zd, ZTEST_GET_SHARED_DS(d), os);
6040 zilog = zd->zd_zilog;
6042 if (zilog->zl_header->zh_claim_lr_seq != 0 &&
6043 zilog->zl_header->zh_claim_lr_seq < committed_seq)
6044 fatal(0, "missing log records: claimed %llu < committed %llu",
6045 zilog->zl_header->zh_claim_lr_seq, committed_seq);
6047 ztest_dataset_dirobj_verify(zd);
6049 zil_replay(os, zd, ztest_replay_vector);
6051 ztest_dataset_dirobj_verify(zd);
6053 if (ztest_opts.zo_verbose >= 6)
6054 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
6056 (u_longlong_t)zilog->zl_parse_blk_count,
6057 (u_longlong_t)zilog->zl_parse_lr_count,
6058 (u_longlong_t)zilog->zl_replaying_seq);
6060 zilog = zil_open(os, ztest_get_data);
6062 if (zilog->zl_replaying_seq != 0 &&
6063 zilog->zl_replaying_seq < committed_seq)
6064 fatal(0, "missing log records: replayed %llu < committed %llu",
6065 zilog->zl_replaying_seq, committed_seq);
6071 ztest_dataset_close(int d)
6073 ztest_ds_t *zd = &ztest_ds[d];
6075 zil_close(zd->zd_zilog);
6076 dmu_objset_disown(zd->zd_os, zd);
6082 * Kick off threads to run tests on all datasets in parallel.
6085 ztest_run(ztest_shared_t *zs)
6090 kthread_t *resume_thread;
6095 ztest_exiting = B_FALSE;
6098 * Initialize parent/child shared state.
6100 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
6101 VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0);
6103 zs->zs_thread_start = gethrtime();
6104 zs->zs_thread_stop =
6105 zs->zs_thread_start + ztest_opts.zo_passtime * NANOSEC;
6106 zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
6107 zs->zs_thread_kill = zs->zs_thread_stop;
6108 if (ztest_random(100) < ztest_opts.zo_killrate) {
6109 zs->zs_thread_kill -=
6110 ztest_random(ztest_opts.zo_passtime * NANOSEC);
6113 mutex_init(&zcl.zcl_callbacks_lock, NULL, MUTEX_DEFAULT, NULL);
6115 list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
6116 offsetof(ztest_cb_data_t, zcd_node));
6121 kernel_init(FREAD | FWRITE);
6122 VERIFY0(spa_open(ztest_opts.zo_pool, &spa, FTAG));
6123 spa->spa_debug = B_TRUE;
6124 metaslab_preload_limit = ztest_random(20) + 1;
6127 VERIFY0(dmu_objset_own(ztest_opts.zo_pool,
6128 DMU_OST_ANY, B_TRUE, FTAG, &os));
6129 zs->zs_guid = dmu_objset_fsid_guid(os);
6130 dmu_objset_disown(os, FTAG);
6132 spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
6135 * We don't expect the pool to suspend unless maxfaults == 0,
6136 * in which case ztest_fault_inject() temporarily takes away
6137 * the only valid replica.
6139 if (MAXFAULTS() == 0)
6140 spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
6142 spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
6145 * Create a thread to periodically resume suspended I/O.
6147 VERIFY3P((resume_thread = zk_thread_create(NULL, 0,
6148 (thread_func_t)ztest_resume_thread, spa, TS_RUN, NULL, 0, 0,
6149 PTHREAD_CREATE_JOINABLE)), !=, NULL);
6153 * Set a deadman alarm to abort() if we hang.
6155 signal(SIGALRM, ztest_deadman_alarm);
6156 alarm((zs->zs_thread_stop - zs->zs_thread_start) / NANOSEC + GRACE);
6160 * Verify that we can safely inquire about about any object,
6161 * whether it's allocated or not. To make it interesting,
6162 * we probe a 5-wide window around each power of two.
6163 * This hits all edge cases, including zero and the max.
6165 for (t = 0; t < 64; t++) {
6166 for (d = -5; d <= 5; d++) {
6167 error = dmu_object_info(spa->spa_meta_objset,
6168 (1ULL << t) + d, NULL);
6169 ASSERT(error == 0 || error == ENOENT ||
6175 * If we got any ENOSPC errors on the previous run, destroy something.
6177 if (zs->zs_enospc_count != 0) {
6178 int d = ztest_random(ztest_opts.zo_datasets);
6179 ztest_dataset_destroy(d);
6181 zs->zs_enospc_count = 0;
6183 tid = umem_zalloc(ztest_opts.zo_threads * sizeof (kt_did_t),
6186 if (ztest_opts.zo_verbose >= 4)
6187 (void) printf("starting main threads...\n");
6190 * Kick off all the tests that run in parallel.
6192 for (t = 0; t < ztest_opts.zo_threads; t++) {
6195 if (t < ztest_opts.zo_datasets &&
6196 ztest_dataset_open(t) != 0)
6199 VERIFY3P(thread = zk_thread_create(NULL, 0,
6200 (thread_func_t)ztest_thread,
6201 (void *)(uintptr_t)t, TS_RUN, NULL, 0, 0,
6202 PTHREAD_CREATE_JOINABLE), !=, NULL);
6203 tid[t] = thread->t_tid;
6207 * Wait for all of the tests to complete. We go in reverse order
6208 * so we don't close datasets while threads are still using them.
6210 for (t = ztest_opts.zo_threads - 1; t >= 0; t--) {
6211 thread_join(tid[t]);
6212 if (t < ztest_opts.zo_datasets)
6213 ztest_dataset_close(t);
6216 txg_wait_synced(spa_get_dsl(spa), 0);
6218 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
6219 zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
6221 umem_free(tid, ztest_opts.zo_threads * sizeof (kt_did_t));
6223 /* Kill the resume thread */
6224 ztest_exiting = B_TRUE;
6225 thread_join(resume_thread->t_tid);
6229 * Right before closing the pool, kick off a bunch of async I/O;
6230 * spa_close() should wait for it to complete.
6232 for (object = 1; object < 50; object++) {
6233 dmu_prefetch(spa->spa_meta_objset, object, 0, 0, 1ULL << 20,
6234 ZIO_PRIORITY_SYNC_READ);
6237 /* Verify that at least one commit cb was called in a timely fashion */
6238 if (zc_cb_counter >= ZTEST_COMMIT_CB_MIN_REG)
6239 VERIFY0(zc_min_txg_delay);
6241 spa_close(spa, FTAG);
6244 * Verify that we can loop over all pools.
6246 mutex_enter(&spa_namespace_lock);
6247 for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
6248 if (ztest_opts.zo_verbose > 3)
6249 (void) printf("spa_next: found %s\n", spa_name(spa));
6250 mutex_exit(&spa_namespace_lock);
6253 * Verify that we can export the pool and reimport it under a
6256 if (ztest_random(2) == 0) {
6257 char name[ZFS_MAX_DATASET_NAME_LEN];
6258 (void) snprintf(name, sizeof (name), "%s_import",
6259 ztest_opts.zo_pool);
6260 ztest_spa_import_export(ztest_opts.zo_pool, name);
6261 ztest_spa_import_export(name, ztest_opts.zo_pool);
6266 list_destroy(&zcl.zcl_callbacks);
6267 mutex_destroy(&zcl.zcl_callbacks_lock);
6268 (void) rwlock_destroy(&ztest_name_lock);
6269 mutex_destroy(&ztest_vdev_lock);
6275 ztest_ds_t *zd = &ztest_ds[0];
6279 if (ztest_opts.zo_verbose >= 3)
6280 (void) printf("testing spa_freeze()...\n");
6282 kernel_init(FREAD | FWRITE);
6283 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6284 VERIFY3U(0, ==, ztest_dataset_open(0));
6285 spa->spa_debug = B_TRUE;
6289 * Force the first log block to be transactionally allocated.
6290 * We have to do this before we freeze the pool -- otherwise
6291 * the log chain won't be anchored.
6293 while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
6294 ztest_dmu_object_alloc_free(zd, 0);
6295 zil_commit(zd->zd_zilog, 0);
6298 txg_wait_synced(spa_get_dsl(spa), 0);
6301 * Freeze the pool. This stops spa_sync() from doing anything,
6302 * so that the only way to record changes from now on is the ZIL.
6307 * Because it is hard to predict how much space a write will actually
6308 * require beforehand, we leave ourselves some fudge space to write over
6311 uint64_t capacity = metaslab_class_get_space(spa_normal_class(spa)) / 2;
6314 * Run tests that generate log records but don't alter the pool config
6315 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
6316 * We do a txg_wait_synced() after each iteration to force the txg
6317 * to increase well beyond the last synced value in the uberblock.
6318 * The ZIL should be OK with that.
6320 * Run a random number of times less than zo_maxloops and ensure we do
6321 * not run out of space on the pool.
6323 while (ztest_random(10) != 0 &&
6324 numloops++ < ztest_opts.zo_maxloops &&
6325 metaslab_class_get_alloc(spa_normal_class(spa)) < capacity) {
6327 ztest_od_init(&od, 0, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
6328 VERIFY0(ztest_object_init(zd, &od, sizeof (od), B_FALSE));
6329 ztest_io(zd, od.od_object,
6330 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
6331 txg_wait_synced(spa_get_dsl(spa), 0);
6335 * Commit all of the changes we just generated.
6337 zil_commit(zd->zd_zilog, 0);
6338 txg_wait_synced(spa_get_dsl(spa), 0);
6341 * Close our dataset and close the pool.
6343 ztest_dataset_close(0);
6344 spa_close(spa, FTAG);
6348 * Open and close the pool and dataset to induce log replay.
6350 kernel_init(FREAD | FWRITE);
6351 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6352 ASSERT(spa_freeze_txg(spa) == UINT64_MAX);
6353 VERIFY3U(0, ==, ztest_dataset_open(0));
6354 ztest_dataset_close(0);
6356 spa->spa_debug = B_TRUE;
6358 txg_wait_synced(spa_get_dsl(spa), 0);
6359 ztest_reguid(NULL, 0);
6361 spa_close(spa, FTAG);
6366 print_time(hrtime_t t, char *timebuf)
6368 hrtime_t s = t / NANOSEC;
6369 hrtime_t m = s / 60;
6370 hrtime_t h = m / 60;
6371 hrtime_t d = h / 24;
6380 (void) sprintf(timebuf,
6381 "%llud%02lluh%02llum%02llus", d, h, m, s);
6383 (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
6385 (void) sprintf(timebuf, "%llum%02llus", m, s);
6387 (void) sprintf(timebuf, "%llus", s);
6391 make_random_props(void)
6395 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
6396 if (ztest_random(2) == 0)
6398 VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
6404 * Create a storage pool with the given name and initial vdev size.
6405 * Then test spa_freeze() functionality.
6408 ztest_init(ztest_shared_t *zs)
6411 nvlist_t *nvroot, *props;
6414 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
6415 VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0);
6417 kernel_init(FREAD | FWRITE);
6420 * Create the storage pool.
6422 (void) spa_destroy(ztest_opts.zo_pool);
6423 ztest_shared->zs_vdev_next_leaf = 0;
6425 zs->zs_mirrors = ztest_opts.zo_mirrors;
6426 nvroot = make_vdev_root(NULL, NULL, NULL, ztest_opts.zo_vdev_size, 0,
6427 0, ztest_opts.zo_raidz, zs->zs_mirrors, 1);
6428 props = make_random_props();
6429 for (i = 0; i < SPA_FEATURES; i++) {
6431 VERIFY3S(-1, !=, asprintf(&buf, "feature@%s",
6432 spa_feature_table[i].fi_uname));
6433 VERIFY3U(0, ==, nvlist_add_uint64(props, buf, 0));
6436 VERIFY3U(0, ==, spa_create(ztest_opts.zo_pool, nvroot, props, NULL));
6437 nvlist_free(nvroot);
6440 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6441 zs->zs_metaslab_sz =
6442 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
6443 spa_close(spa, FTAG);
6447 ztest_run_zdb(ztest_opts.zo_pool);
6451 ztest_run_zdb(ztest_opts.zo_pool);
6453 (void) rwlock_destroy(&ztest_name_lock);
6454 mutex_destroy(&ztest_vdev_lock);
6460 static char ztest_name_data[] = "/tmp/ztest.data.XXXXXX";
6462 ztest_fd_data = mkstemp(ztest_name_data);
6463 ASSERT3S(ztest_fd_data, >=, 0);
6464 (void) unlink(ztest_name_data);
6468 shared_data_size(ztest_shared_hdr_t *hdr)
6472 size = hdr->zh_hdr_size;
6473 size += hdr->zh_opts_size;
6474 size += hdr->zh_size;
6475 size += hdr->zh_stats_size * hdr->zh_stats_count;
6476 size += hdr->zh_ds_size * hdr->zh_ds_count;
6485 ztest_shared_hdr_t *hdr;
6487 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6488 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6489 ASSERT(hdr != MAP_FAILED);
6491 VERIFY3U(0, ==, ftruncate(ztest_fd_data, sizeof (ztest_shared_hdr_t)));
6493 hdr->zh_hdr_size = sizeof (ztest_shared_hdr_t);
6494 hdr->zh_opts_size = sizeof (ztest_shared_opts_t);
6495 hdr->zh_size = sizeof (ztest_shared_t);
6496 hdr->zh_stats_size = sizeof (ztest_shared_callstate_t);
6497 hdr->zh_stats_count = ZTEST_FUNCS;
6498 hdr->zh_ds_size = sizeof (ztest_shared_ds_t);
6499 hdr->zh_ds_count = ztest_opts.zo_datasets;
6501 size = shared_data_size(hdr);
6502 VERIFY3U(0, ==, ftruncate(ztest_fd_data, size));
6504 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6511 ztest_shared_hdr_t *hdr;
6514 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6515 PROT_READ, MAP_SHARED, ztest_fd_data, 0);
6516 ASSERT(hdr != MAP_FAILED);
6518 size = shared_data_size(hdr);
6520 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6521 hdr = ztest_shared_hdr = (void *)mmap(0, P2ROUNDUP(size, getpagesize()),
6522 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6523 ASSERT(hdr != MAP_FAILED);
6524 buf = (uint8_t *)hdr;
6526 offset = hdr->zh_hdr_size;
6527 ztest_shared_opts = (void *)&buf[offset];
6528 offset += hdr->zh_opts_size;
6529 ztest_shared = (void *)&buf[offset];
6530 offset += hdr->zh_size;
6531 ztest_shared_callstate = (void *)&buf[offset];
6532 offset += hdr->zh_stats_size * hdr->zh_stats_count;
6533 ztest_shared_ds = (void *)&buf[offset];
6537 exec_child(char *cmd, char *libpath, boolean_t ignorekill, int *statusp)
6541 char *cmdbuf = NULL;
6546 cmdbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
6547 (void) strlcpy(cmdbuf, getexecname(), MAXPATHLEN);
6552 fatal(1, "fork failed");
6554 if (pid == 0) { /* child */
6555 char *emptyargv[2] = { cmd, NULL };
6556 char fd_data_str[12];
6558 struct rlimit rl = { 1024, 1024 };
6559 (void) setrlimit(RLIMIT_NOFILE, &rl);
6561 (void) close(ztest_fd_rand);
6562 VERIFY(11 >= snprintf(fd_data_str, 12, "%d", ztest_fd_data));
6563 VERIFY(0 == setenv("ZTEST_FD_DATA", fd_data_str, 1));
6565 (void) enable_extended_FILE_stdio(-1, -1);
6566 if (libpath != NULL)
6567 VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath, 1));
6568 (void) execv(cmd, emptyargv);
6569 ztest_dump_core = B_FALSE;
6570 fatal(B_TRUE, "exec failed: %s", cmd);
6573 if (cmdbuf != NULL) {
6574 umem_free(cmdbuf, MAXPATHLEN);
6578 while (waitpid(pid, &status, 0) != pid)
6580 if (statusp != NULL)
6583 if (WIFEXITED(status)) {
6584 if (WEXITSTATUS(status) != 0) {
6585 (void) fprintf(stderr, "child exited with code %d\n",
6586 WEXITSTATUS(status));
6590 } else if (WIFSIGNALED(status)) {
6591 if (!ignorekill || WTERMSIG(status) != SIGKILL) {
6592 (void) fprintf(stderr, "child died with signal %d\n",
6598 (void) fprintf(stderr, "something strange happened to child\n");
6605 ztest_run_init(void)
6609 ztest_shared_t *zs = ztest_shared;
6611 ASSERT(ztest_opts.zo_init != 0);
6614 * Blow away any existing copy of zpool.cache
6616 (void) remove(spa_config_path);
6619 * Create and initialize our storage pool.
6621 for (i = 1; i <= ztest_opts.zo_init; i++) {
6622 bzero(zs, sizeof (ztest_shared_t));
6623 if (ztest_opts.zo_verbose >= 3 &&
6624 ztest_opts.zo_init != 1) {
6625 (void) printf("ztest_init(), pass %d\n", i);
6632 main(int argc, char **argv)
6640 ztest_shared_callstate_t *zc;
6647 char *fd_data_str = getenv("ZTEST_FD_DATA");
6648 struct sigaction action;
6650 (void) setvbuf(stdout, NULL, _IOLBF, 0);
6652 dprintf_setup(&argc, argv);
6654 action.sa_handler = sig_handler;
6655 sigemptyset(&action.sa_mask);
6656 action.sa_flags = 0;
6658 if (sigaction(SIGSEGV, &action, NULL) < 0) {
6659 (void) fprintf(stderr, "ztest: cannot catch SIGSEGV: %s.\n",
6664 if (sigaction(SIGABRT, &action, NULL) < 0) {
6665 (void) fprintf(stderr, "ztest: cannot catch SIGABRT: %s.\n",
6670 ztest_fd_rand = open("/dev/urandom", O_RDONLY);
6671 ASSERT3S(ztest_fd_rand, >=, 0);
6674 process_options(argc, argv);
6679 bcopy(&ztest_opts, ztest_shared_opts,
6680 sizeof (*ztest_shared_opts));
6682 ztest_fd_data = atoi(fd_data_str);
6684 bcopy(ztest_shared_opts, &ztest_opts, sizeof (ztest_opts));
6686 ASSERT3U(ztest_opts.zo_datasets, ==, ztest_shared_hdr->zh_ds_count);
6688 /* Override location of zpool.cache */
6689 VERIFY(asprintf((char **)&spa_config_path, "%s/zpool.cache",
6690 ztest_opts.zo_dir) != -1);
6692 ztest_ds = umem_alloc(ztest_opts.zo_datasets * sizeof (ztest_ds_t),
6697 metaslab_gang_bang = ztest_opts.zo_metaslab_gang_bang;
6698 metaslab_df_alloc_threshold =
6699 zs->zs_metaslab_df_alloc_threshold;
6708 hasalt = (strlen(ztest_opts.zo_alt_ztest) != 0);
6710 if (ztest_opts.zo_verbose >= 1) {
6711 (void) printf("%llu vdevs, %d datasets, %d threads,"
6712 " %llu seconds...\n",
6713 (u_longlong_t)ztest_opts.zo_vdevs,
6714 ztest_opts.zo_datasets,
6715 ztest_opts.zo_threads,
6716 (u_longlong_t)ztest_opts.zo_time);
6719 cmd = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
6720 (void) strlcpy(cmd, getexecname(), MAXNAMELEN);
6722 zs->zs_do_init = B_TRUE;
6723 if (strlen(ztest_opts.zo_alt_ztest) != 0) {
6724 if (ztest_opts.zo_verbose >= 1) {
6725 (void) printf("Executing older ztest for "
6726 "initialization: %s\n", ztest_opts.zo_alt_ztest);
6728 VERIFY(!exec_child(ztest_opts.zo_alt_ztest,
6729 ztest_opts.zo_alt_libpath, B_FALSE, NULL));
6731 VERIFY(!exec_child(NULL, NULL, B_FALSE, NULL));
6733 zs->zs_do_init = B_FALSE;
6735 zs->zs_proc_start = gethrtime();
6736 zs->zs_proc_stop = zs->zs_proc_start + ztest_opts.zo_time * NANOSEC;
6738 for (f = 0; f < ZTEST_FUNCS; f++) {
6739 zi = &ztest_info[f];
6740 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6741 if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
6742 zc->zc_next = UINT64_MAX;
6744 zc->zc_next = zs->zs_proc_start +
6745 ztest_random(2 * zi->zi_interval[0] + 1);
6749 * Run the tests in a loop. These tests include fault injection
6750 * to verify that self-healing data works, and forced crashes
6751 * to verify that we never lose on-disk consistency.
6753 while (gethrtime() < zs->zs_proc_stop) {
6758 * Initialize the workload counters for each function.
6760 for (f = 0; f < ZTEST_FUNCS; f++) {
6761 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6766 /* Set the allocation switch size */
6767 zs->zs_metaslab_df_alloc_threshold =
6768 ztest_random(zs->zs_metaslab_sz / 4) + 1;
6770 if (!hasalt || ztest_random(2) == 0) {
6771 if (hasalt && ztest_opts.zo_verbose >= 1) {
6772 (void) printf("Executing newer ztest: %s\n",
6776 killed = exec_child(cmd, NULL, B_TRUE, &status);
6778 if (hasalt && ztest_opts.zo_verbose >= 1) {
6779 (void) printf("Executing older ztest: %s\n",
6780 ztest_opts.zo_alt_ztest);
6783 killed = exec_child(ztest_opts.zo_alt_ztest,
6784 ztest_opts.zo_alt_libpath, B_TRUE, &status);
6791 if (ztest_opts.zo_verbose >= 1) {
6792 hrtime_t now = gethrtime();
6794 now = MIN(now, zs->zs_proc_stop);
6795 print_time(zs->zs_proc_stop - now, timebuf);
6796 nicenum(zs->zs_space, numbuf);
6798 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
6799 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
6801 WIFEXITED(status) ? "Complete" : "SIGKILL",
6802 (u_longlong_t)zs->zs_enospc_count,
6803 100.0 * zs->zs_alloc / zs->zs_space,
6805 100.0 * (now - zs->zs_proc_start) /
6806 (ztest_opts.zo_time * NANOSEC), timebuf);
6809 if (ztest_opts.zo_verbose >= 2) {
6810 (void) printf("\nWorkload summary:\n\n");
6811 (void) printf("%7s %9s %s\n",
6812 "Calls", "Time", "Function");
6813 (void) printf("%7s %9s %s\n",
6814 "-----", "----", "--------");
6815 for (f = 0; f < ZTEST_FUNCS; f++) {
6816 zi = &ztest_info[f];
6817 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6818 print_time(zc->zc_time, timebuf);
6819 (void) printf("%7llu %9s %s\n",
6820 (u_longlong_t)zc->zc_count, timebuf,
6823 (void) printf("\n");
6827 * It's possible that we killed a child during a rename test,
6828 * in which case we'll have a 'ztest_tmp' pool lying around
6829 * instead of 'ztest'. Do a blind rename in case this happened.
6832 if (spa_open(ztest_opts.zo_pool, &spa, FTAG) == 0) {
6833 spa_close(spa, FTAG);
6835 char tmpname[ZFS_MAX_DATASET_NAME_LEN];
6837 kernel_init(FREAD | FWRITE);
6838 (void) snprintf(tmpname, sizeof (tmpname), "%s_tmp",
6839 ztest_opts.zo_pool);
6840 (void) spa_rename(tmpname, ztest_opts.zo_pool);
6844 ztest_run_zdb(ztest_opts.zo_pool);
6847 if (ztest_opts.zo_verbose >= 1) {
6849 (void) printf("%d runs of older ztest: %s\n", older,
6850 ztest_opts.zo_alt_ztest);
6851 (void) printf("%d runs of newer ztest: %s\n", newer,
6854 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
6855 kills, iters - kills, (100.0 * kills) / MAX(1, iters));
6858 umem_free(cmd, MAXNAMELEN);