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[MAXNAMELEN];
149 char zo_dir[MAXNAMELEN];
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 {
230 typedef struct bufwad {
248 #define ZTEST_RANGE_LOCKS 64
249 #define ZTEST_OBJECT_LOCKS 64
252 * Object descriptor. Used as a template for object lookup/create/remove.
254 typedef struct ztest_od {
257 dmu_object_type_t od_type;
258 dmu_object_type_t od_crtype;
259 uint64_t od_blocksize;
260 uint64_t od_crblocksize;
263 char od_name[MAXNAMELEN];
269 typedef struct ztest_ds {
270 ztest_shared_ds_t *zd_shared;
272 rwlock_t zd_zilog_lock;
274 ztest_od_t *zd_od; /* debugging aid */
275 char zd_name[MAXNAMELEN];
276 kmutex_t zd_dirobj_lock;
277 rll_t zd_object_lock[ZTEST_OBJECT_LOCKS];
278 zll_t zd_range_lock[ZTEST_RANGE_LOCKS];
282 * Per-iteration state.
284 typedef void ztest_func_t(ztest_ds_t *zd, uint64_t id);
286 typedef struct ztest_info {
287 ztest_func_t *zi_func; /* test function */
288 uint64_t zi_iters; /* iterations per execution */
289 uint64_t *zi_interval; /* execute every <interval> seconds */
290 const char *zi_funcname; /* name of test function */
293 typedef struct ztest_shared_callstate {
294 uint64_t zc_count; /* per-pass count */
295 uint64_t zc_time; /* per-pass time */
296 uint64_t zc_next; /* next time to call this function */
297 } ztest_shared_callstate_t;
299 static ztest_shared_callstate_t *ztest_shared_callstate;
300 #define ZTEST_GET_SHARED_CALLSTATE(c) (&ztest_shared_callstate[c])
302 ztest_func_t ztest_dmu_read_write;
303 ztest_func_t ztest_dmu_write_parallel;
304 ztest_func_t ztest_dmu_object_alloc_free;
305 ztest_func_t ztest_dmu_commit_callbacks;
306 ztest_func_t ztest_zap;
307 ztest_func_t ztest_zap_parallel;
308 ztest_func_t ztest_zil_commit;
309 ztest_func_t ztest_zil_remount;
310 ztest_func_t ztest_dmu_read_write_zcopy;
311 ztest_func_t ztest_dmu_objset_create_destroy;
312 ztest_func_t ztest_dmu_prealloc;
313 ztest_func_t ztest_fzap;
314 ztest_func_t ztest_dmu_snapshot_create_destroy;
315 ztest_func_t ztest_dsl_prop_get_set;
316 ztest_func_t ztest_spa_prop_get_set;
317 ztest_func_t ztest_spa_create_destroy;
318 ztest_func_t ztest_fault_inject;
319 ztest_func_t ztest_ddt_repair;
320 ztest_func_t ztest_dmu_snapshot_hold;
321 ztest_func_t ztest_spa_rename;
322 ztest_func_t ztest_scrub;
323 ztest_func_t ztest_dsl_dataset_promote_busy;
324 ztest_func_t ztest_vdev_attach_detach;
325 ztest_func_t ztest_vdev_LUN_growth;
326 ztest_func_t ztest_vdev_add_remove;
327 ztest_func_t ztest_vdev_aux_add_remove;
328 ztest_func_t ztest_split_pool;
329 ztest_func_t ztest_reguid;
330 ztest_func_t ztest_spa_upgrade;
331 ztest_func_t ztest_fletcher;
332 ztest_func_t ztest_dmu_free_long_range;
334 uint64_t zopt_always = 0ULL * NANOSEC; /* all the time */
335 uint64_t zopt_incessant = 1ULL * NANOSEC / 10; /* every 1/10 second */
336 uint64_t zopt_often = 1ULL * NANOSEC; /* every second */
337 uint64_t zopt_sometimes = 10ULL * NANOSEC; /* every 10 seconds */
338 uint64_t zopt_rarely = 60ULL * NANOSEC; /* every 60 seconds */
340 #define ZTI_INIT(func, iters, interval) \
341 { .zi_func = (func), \
342 .zi_iters = (iters), \
343 .zi_interval = (interval), \
344 .zi_funcname = # func }
346 ztest_info_t ztest_info[] = {
347 ZTI_INIT(ztest_dmu_read_write, 1, &zopt_always),
348 ZTI_INIT(ztest_dmu_write_parallel, 10, &zopt_always),
349 ZTI_INIT(ztest_dmu_object_alloc_free, 1, &zopt_always),
350 ZTI_INIT(ztest_dmu_commit_callbacks, 1, &zopt_always),
351 ZTI_INIT(ztest_zap, 30, &zopt_always),
352 ZTI_INIT(ztest_zap_parallel, 100, &zopt_always),
353 ZTI_INIT(ztest_split_pool, 1, &zopt_always),
354 ZTI_INIT(ztest_zil_commit, 1, &zopt_incessant),
355 ZTI_INIT(ztest_zil_remount, 1, &zopt_sometimes),
356 ZTI_INIT(ztest_dmu_read_write_zcopy, 1, &zopt_often),
357 ZTI_INIT(ztest_dmu_objset_create_destroy, 1, &zopt_often),
358 ZTI_INIT(ztest_dsl_prop_get_set, 1, &zopt_often),
359 ZTI_INIT(ztest_spa_prop_get_set, 1, &zopt_sometimes),
361 ZTI_INIT(ztest_dmu_prealloc, 1, &zopt_sometimes),
363 ZTI_INIT(ztest_fzap, 1, &zopt_sometimes),
364 ZTI_INIT(ztest_dmu_snapshot_create_destroy, 1, &zopt_sometimes),
365 ZTI_INIT(ztest_spa_create_destroy, 1, &zopt_sometimes),
366 ZTI_INIT(ztest_fault_inject, 1, &zopt_sometimes),
367 ZTI_INIT(ztest_ddt_repair, 1, &zopt_sometimes),
368 ZTI_INIT(ztest_dmu_snapshot_hold, 1, &zopt_sometimes),
369 ZTI_INIT(ztest_reguid, 1, &zopt_rarely),
370 ZTI_INIT(ztest_spa_rename, 1, &zopt_rarely),
371 ZTI_INIT(ztest_scrub, 1, &zopt_rarely),
372 ZTI_INIT(ztest_spa_upgrade, 1, &zopt_rarely),
373 ZTI_INIT(ztest_dsl_dataset_promote_busy, 1, &zopt_rarely),
374 ZTI_INIT(ztest_vdev_attach_detach, 1, &zopt_sometimes),
375 ZTI_INIT(ztest_vdev_LUN_growth, 1, &zopt_rarely),
376 ZTI_INIT(ztest_vdev_add_remove, 1, &ztest_opts.zo_vdevtime),
377 ZTI_INIT(ztest_vdev_aux_add_remove, 1, &ztest_opts.zo_vdevtime),
378 ZTI_INIT(ztest_fletcher, 1, &zopt_rarely),
379 ZTI_INIT(ztest_dmu_free_long_range, 1, &zopt_always),
382 #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
385 * The following struct is used to hold a list of uncalled commit callbacks.
386 * The callbacks are ordered by txg number.
388 typedef struct ztest_cb_list {
389 kmutex_t zcl_callbacks_lock;
390 list_t zcl_callbacks;
394 * Stuff we need to share writably between parent and child.
396 typedef struct ztest_shared {
397 boolean_t zs_do_init;
398 hrtime_t zs_proc_start;
399 hrtime_t zs_proc_stop;
400 hrtime_t zs_thread_start;
401 hrtime_t zs_thread_stop;
402 hrtime_t zs_thread_kill;
403 uint64_t zs_enospc_count;
404 uint64_t zs_vdev_next_leaf;
405 uint64_t zs_vdev_aux;
410 uint64_t zs_metaslab_sz;
411 uint64_t zs_metaslab_df_alloc_threshold;
415 #define ID_PARALLEL -1ULL
417 static char ztest_dev_template[] = "%s/%s.%llua";
418 static char ztest_aux_template[] = "%s/%s.%s.%llu";
419 ztest_shared_t *ztest_shared;
421 static spa_t *ztest_spa = NULL;
422 static ztest_ds_t *ztest_ds;
424 static kmutex_t ztest_vdev_lock;
427 * The ztest_name_lock protects the pool and dataset namespace used by
428 * the individual tests. To modify the namespace, consumers must grab
429 * this lock as writer. Grabbing the lock as reader will ensure that the
430 * namespace does not change while the lock is held.
432 static rwlock_t ztest_name_lock;
434 static boolean_t ztest_dump_core = B_TRUE;
435 static boolean_t ztest_exiting;
437 /* Global commit callback list */
438 static ztest_cb_list_t zcl;
439 /* Commit cb delay */
440 static uint64_t zc_min_txg_delay = UINT64_MAX;
441 static int zc_cb_counter = 0;
444 * Minimum number of commit callbacks that need to be registered for us to check
445 * whether the minimum txg delay is acceptable.
447 #define ZTEST_COMMIT_CB_MIN_REG 100
450 * If a number of txgs equal to this threshold have been created after a commit
451 * callback has been registered but not called, then we assume there is an
452 * implementation bug.
454 #define ZTEST_COMMIT_CB_THRESH (TXG_CONCURRENT_STATES + 1000)
456 extern uint64_t metaslab_gang_bang;
457 extern uint64_t metaslab_df_alloc_threshold;
460 ZTEST_META_DNODE = 0,
465 static void usage(boolean_t) __NORETURN;
468 * These libumem hooks provide a reasonable set of defaults for the allocator's
469 * debugging facilities.
472 _umem_debug_init(void)
474 return ("default,verbose"); /* $UMEM_DEBUG setting */
478 _umem_logging_init(void)
480 return ("fail,contents"); /* $UMEM_LOGGING setting */
483 #define BACKTRACE_SZ 100
485 static void sig_handler(int signo)
487 struct sigaction action;
488 #ifdef __GLIBC__ /* backtrace() is a GNU extension */
490 void *buffer[BACKTRACE_SZ];
492 nptrs = backtrace(buffer, BACKTRACE_SZ);
493 backtrace_symbols_fd(buffer, nptrs, STDERR_FILENO);
497 * Restore default action and re-raise signal so SIGSEGV and
498 * SIGABRT can trigger a core dump.
500 action.sa_handler = SIG_DFL;
501 sigemptyset(&action.sa_mask);
503 (void) sigaction(signo, &action, NULL);
507 #define FATAL_MSG_SZ 1024
512 fatal(int do_perror, char *message, ...)
515 int save_errno = errno;
518 (void) fflush(stdout);
519 buf = umem_alloc(FATAL_MSG_SZ, UMEM_NOFAIL);
521 va_start(args, message);
522 (void) sprintf(buf, "ztest: ");
524 (void) vsprintf(buf + strlen(buf), message, args);
527 (void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
528 ": %s", strerror(save_errno));
530 (void) fprintf(stderr, "%s\n", buf);
531 fatal_msg = buf; /* to ease debugging */
538 str2shift(const char *buf)
540 const char *ends = "BKMGTPEZ";
545 for (i = 0; i < strlen(ends); i++) {
546 if (toupper(buf[0]) == ends[i])
549 if (i == strlen(ends)) {
550 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
554 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
557 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
563 nicenumtoull(const char *buf)
568 val = strtoull(buf, &end, 0);
570 (void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
572 } else if (end[0] == '.') {
573 double fval = strtod(buf, &end);
574 fval *= pow(2, str2shift(end));
575 if (fval > UINT64_MAX) {
576 (void) fprintf(stderr, "ztest: value too large: %s\n",
580 val = (uint64_t)fval;
582 int shift = str2shift(end);
583 if (shift >= 64 || (val << shift) >> shift != val) {
584 (void) fprintf(stderr, "ztest: value too large: %s\n",
594 usage(boolean_t requested)
596 const ztest_shared_opts_t *zo = &ztest_opts_defaults;
598 char nice_vdev_size[10];
599 char nice_gang_bang[10];
600 FILE *fp = requested ? stdout : stderr;
602 nicenum(zo->zo_vdev_size, nice_vdev_size);
603 nicenum(zo->zo_metaslab_gang_bang, nice_gang_bang);
605 (void) fprintf(fp, "Usage: %s\n"
606 "\t[-v vdevs (default: %llu)]\n"
607 "\t[-s size_of_each_vdev (default: %s)]\n"
608 "\t[-a alignment_shift (default: %d)] use 0 for random\n"
609 "\t[-m mirror_copies (default: %d)]\n"
610 "\t[-r raidz_disks (default: %d)]\n"
611 "\t[-R raidz_parity (default: %d)]\n"
612 "\t[-d datasets (default: %d)]\n"
613 "\t[-t threads (default: %d)]\n"
614 "\t[-g gang_block_threshold (default: %s)]\n"
615 "\t[-i init_count (default: %d)] initialize pool i times\n"
616 "\t[-k kill_percentage (default: %llu%%)]\n"
617 "\t[-p pool_name (default: %s)]\n"
618 "\t[-f dir (default: %s)] file directory for vdev files\n"
619 "\t[-V] verbose (use multiple times for ever more blather)\n"
620 "\t[-E] use existing pool instead of creating new one\n"
621 "\t[-T time (default: %llu sec)] total run time\n"
622 "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
623 "\t[-P passtime (default: %llu sec)] time per pass\n"
624 "\t[-B alt_ztest (default: <none>)] alternate ztest path\n"
625 "\t[-h] (print help)\n"
628 (u_longlong_t)zo->zo_vdevs, /* -v */
629 nice_vdev_size, /* -s */
630 zo->zo_ashift, /* -a */
631 zo->zo_mirrors, /* -m */
632 zo->zo_raidz, /* -r */
633 zo->zo_raidz_parity, /* -R */
634 zo->zo_datasets, /* -d */
635 zo->zo_threads, /* -t */
636 nice_gang_bang, /* -g */
637 zo->zo_init, /* -i */
638 (u_longlong_t)zo->zo_killrate, /* -k */
639 zo->zo_pool, /* -p */
641 (u_longlong_t)zo->zo_time, /* -T */
642 (u_longlong_t)zo->zo_maxloops, /* -F */
643 (u_longlong_t)zo->zo_passtime);
644 exit(requested ? 0 : 1);
648 process_options(int argc, char **argv)
651 ztest_shared_opts_t *zo = &ztest_opts;
655 char altdir[MAXNAMELEN] = { 0 };
657 bcopy(&ztest_opts_defaults, zo, sizeof (*zo));
659 while ((opt = getopt(argc, argv,
660 "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:hF:B:")) != EOF) {
677 value = nicenumtoull(optarg);
681 zo->zo_vdevs = value;
684 zo->zo_vdev_size = MAX(SPA_MINDEVSIZE, value);
687 zo->zo_ashift = value;
690 zo->zo_mirrors = value;
693 zo->zo_raidz = MAX(1, value);
696 zo->zo_raidz_parity = MIN(MAX(value, 1), 3);
699 zo->zo_datasets = MAX(1, value);
702 zo->zo_threads = MAX(1, value);
705 zo->zo_metaslab_gang_bang = MAX(SPA_MINBLOCKSIZE << 1,
712 zo->zo_killrate = value;
715 (void) strlcpy(zo->zo_pool, optarg,
716 sizeof (zo->zo_pool));
719 path = realpath(optarg, NULL);
721 (void) fprintf(stderr, "error: %s: %s\n",
722 optarg, strerror(errno));
725 (void) strlcpy(zo->zo_dir, path,
726 sizeof (zo->zo_dir));
739 zo->zo_passtime = MAX(1, value);
742 zo->zo_maxloops = MAX(1, value);
745 (void) strlcpy(altdir, optarg, sizeof (altdir));
757 zo->zo_raidz_parity = MIN(zo->zo_raidz_parity, zo->zo_raidz - 1);
760 (zo->zo_vdevs > 0 ? zo->zo_time * NANOSEC / zo->zo_vdevs :
763 if (strlen(altdir) > 0) {
771 cmd = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
772 realaltdir = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
774 VERIFY(NULL != realpath(getexecname(), cmd));
775 if (0 != access(altdir, F_OK)) {
776 ztest_dump_core = B_FALSE;
777 fatal(B_TRUE, "invalid alternate ztest path: %s",
780 VERIFY(NULL != realpath(altdir, realaltdir));
783 * 'cmd' should be of the form "<anything>/usr/bin/<isa>/ztest".
784 * We want to extract <isa> to determine if we should use
785 * 32 or 64 bit binaries.
787 bin = strstr(cmd, "/usr/bin/");
788 ztest = strstr(bin, "/ztest");
790 isalen = ztest - isa;
791 (void) snprintf(zo->zo_alt_ztest, sizeof (zo->zo_alt_ztest),
792 "%s/usr/bin/%.*s/ztest", realaltdir, isalen, isa);
793 (void) snprintf(zo->zo_alt_libpath, sizeof (zo->zo_alt_libpath),
794 "%s/usr/lib/%.*s", realaltdir, isalen, isa);
796 if (0 != access(zo->zo_alt_ztest, X_OK)) {
797 ztest_dump_core = B_FALSE;
798 fatal(B_TRUE, "invalid alternate ztest: %s",
800 } else if (0 != access(zo->zo_alt_libpath, X_OK)) {
801 ztest_dump_core = B_FALSE;
802 fatal(B_TRUE, "invalid alternate lib directory %s",
806 umem_free(cmd, MAXPATHLEN);
807 umem_free(realaltdir, MAXPATHLEN);
812 ztest_kill(ztest_shared_t *zs)
814 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(ztest_spa));
815 zs->zs_space = metaslab_class_get_space(spa_normal_class(ztest_spa));
818 * Before we kill off ztest, make sure that the config is updated.
819 * See comment above spa_config_sync().
821 mutex_enter(&spa_namespace_lock);
822 spa_config_sync(ztest_spa, B_FALSE, B_FALSE);
823 mutex_exit(&spa_namespace_lock);
825 (void) kill(getpid(), SIGKILL);
829 ztest_random(uint64_t range)
833 ASSERT3S(ztest_fd_rand, >=, 0);
838 if (read(ztest_fd_rand, &r, sizeof (r)) != sizeof (r))
839 fatal(1, "short read from /dev/urandom");
846 ztest_record_enospc(const char *s)
848 ztest_shared->zs_enospc_count++;
852 ztest_get_ashift(void)
854 if (ztest_opts.zo_ashift == 0)
855 return (SPA_MINBLOCKSHIFT + ztest_random(5));
856 return (ztest_opts.zo_ashift);
860 make_vdev_file(char *path, char *aux, char *pool, size_t size, uint64_t ashift)
866 pathbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
869 ashift = ztest_get_ashift();
875 vdev = ztest_shared->zs_vdev_aux;
876 (void) snprintf(path, MAXPATHLEN,
877 ztest_aux_template, ztest_opts.zo_dir,
878 pool == NULL ? ztest_opts.zo_pool : pool,
881 vdev = ztest_shared->zs_vdev_next_leaf++;
882 (void) snprintf(path, MAXPATHLEN,
883 ztest_dev_template, ztest_opts.zo_dir,
884 pool == NULL ? ztest_opts.zo_pool : pool, vdev);
889 int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
891 fatal(1, "can't open %s", path);
892 if (ftruncate(fd, size) != 0)
893 fatal(1, "can't ftruncate %s", path);
897 VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
898 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
899 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
900 VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
901 umem_free(pathbuf, MAXPATHLEN);
907 make_vdev_raidz(char *path, char *aux, char *pool, size_t size,
908 uint64_t ashift, int r)
910 nvlist_t *raidz, **child;
914 return (make_vdev_file(path, aux, pool, size, ashift));
915 child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
917 for (c = 0; c < r; c++)
918 child[c] = make_vdev_file(path, aux, pool, size, ashift);
920 VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
921 VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
922 VDEV_TYPE_RAIDZ) == 0);
923 VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
924 ztest_opts.zo_raidz_parity) == 0);
925 VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
928 for (c = 0; c < r; c++)
929 nvlist_free(child[c]);
931 umem_free(child, r * sizeof (nvlist_t *));
937 make_vdev_mirror(char *path, char *aux, char *pool, size_t size,
938 uint64_t ashift, int r, int m)
940 nvlist_t *mirror, **child;
944 return (make_vdev_raidz(path, aux, pool, size, ashift, r));
946 child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
948 for (c = 0; c < m; c++)
949 child[c] = make_vdev_raidz(path, aux, pool, size, ashift, r);
951 VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
952 VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
953 VDEV_TYPE_MIRROR) == 0);
954 VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
957 for (c = 0; c < m; c++)
958 nvlist_free(child[c]);
960 umem_free(child, m * sizeof (nvlist_t *));
966 make_vdev_root(char *path, char *aux, char *pool, size_t size, uint64_t ashift,
967 int log, int r, int m, int t)
969 nvlist_t *root, **child;
974 child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
976 for (c = 0; c < t; c++) {
977 child[c] = make_vdev_mirror(path, aux, pool, size, ashift,
979 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
983 VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
984 VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
985 VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
988 for (c = 0; c < t; c++)
989 nvlist_free(child[c]);
991 umem_free(child, t * sizeof (nvlist_t *));
997 * Find a random spa version. Returns back a random spa version in the
998 * range [initial_version, SPA_VERSION_FEATURES].
1001 ztest_random_spa_version(uint64_t initial_version)
1003 uint64_t version = initial_version;
1005 if (version <= SPA_VERSION_BEFORE_FEATURES) {
1007 ztest_random(SPA_VERSION_BEFORE_FEATURES - version + 1);
1010 if (version > SPA_VERSION_BEFORE_FEATURES)
1011 version = SPA_VERSION_FEATURES;
1013 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
1018 ztest_random_blocksize(void)
1021 * Choose a block size >= the ashift.
1022 * If the SPA supports new MAXBLOCKSIZE, test up to 1MB blocks.
1024 int maxbs = SPA_OLD_MAXBLOCKSHIFT;
1025 if (spa_maxblocksize(ztest_spa) == SPA_MAXBLOCKSIZE)
1027 uint64_t block_shift =
1028 ztest_random(maxbs - ztest_spa->spa_max_ashift + 1);
1029 return (1 << (SPA_MINBLOCKSHIFT + block_shift));
1033 ztest_random_ibshift(void)
1035 return (DN_MIN_INDBLKSHIFT +
1036 ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1));
1040 ztest_random_vdev_top(spa_t *spa, boolean_t log_ok)
1043 vdev_t *rvd = spa->spa_root_vdev;
1046 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1049 top = ztest_random(rvd->vdev_children);
1050 tvd = rvd->vdev_child[top];
1051 } while (tvd->vdev_ishole || (tvd->vdev_islog && !log_ok) ||
1052 tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL);
1058 ztest_random_dsl_prop(zfs_prop_t prop)
1063 value = zfs_prop_random_value(prop, ztest_random(-1ULL));
1064 } while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF);
1070 ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value,
1073 const char *propname = zfs_prop_to_name(prop);
1074 const char *valname;
1079 error = dsl_prop_set_int(osname, propname,
1080 (inherit ? ZPROP_SRC_NONE : ZPROP_SRC_LOCAL), value);
1082 if (error == ENOSPC) {
1083 ztest_record_enospc(FTAG);
1088 setpoint = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
1089 VERIFY0(dsl_prop_get_integer(osname, propname, &curval, setpoint));
1091 if (ztest_opts.zo_verbose >= 6) {
1094 err = zfs_prop_index_to_string(prop, curval, &valname);
1096 (void) printf("%s %s = %llu at '%s'\n",
1097 osname, propname, (unsigned long long)curval,
1100 (void) printf("%s %s = %s at '%s'\n",
1101 osname, propname, valname, setpoint);
1103 umem_free(setpoint, MAXPATHLEN);
1109 ztest_spa_prop_set_uint64(zpool_prop_t prop, uint64_t value)
1111 spa_t *spa = ztest_spa;
1112 nvlist_t *props = NULL;
1115 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
1116 VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0);
1118 error = spa_prop_set(spa, props);
1122 if (error == ENOSPC) {
1123 ztest_record_enospc(FTAG);
1133 * Object and range lock mechanics
1136 list_node_t z_lnode;
1137 refcount_t z_refcnt;
1139 zfs_rlock_t z_range_lock;
1144 ztest_znode_t *z_ztznode;
1147 static ztest_znode_t *
1148 ztest_znode_init(uint64_t object)
1150 ztest_znode_t *zp = umem_alloc(sizeof (*zp), UMEM_NOFAIL);
1152 list_link_init(&zp->z_lnode);
1153 refcount_create(&zp->z_refcnt);
1154 zp->z_object = object;
1155 zfs_rlock_init(&zp->z_range_lock);
1161 ztest_znode_fini(ztest_znode_t *zp)
1163 ASSERT(refcount_is_zero(&zp->z_refcnt));
1164 zfs_rlock_destroy(&zp->z_range_lock);
1166 refcount_destroy(&zp->z_refcnt);
1167 list_link_init(&zp->z_lnode);
1168 umem_free(zp, sizeof (*zp));
1172 ztest_zll_init(zll_t *zll)
1174 mutex_init(&zll->z_lock, NULL, MUTEX_DEFAULT, NULL);
1175 list_create(&zll->z_list, sizeof (ztest_znode_t),
1176 offsetof(ztest_znode_t, z_lnode));
1180 ztest_zll_destroy(zll_t *zll)
1182 list_destroy(&zll->z_list);
1183 mutex_destroy(&zll->z_lock);
1186 #define RL_TAG "range_lock"
1187 static ztest_znode_t *
1188 ztest_znode_get(ztest_ds_t *zd, uint64_t object)
1190 zll_t *zll = &zd->zd_range_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1191 ztest_znode_t *zp = NULL;
1192 mutex_enter(&zll->z_lock);
1193 for (zp = list_head(&zll->z_list); (zp);
1194 zp = list_next(&zll->z_list, zp)) {
1195 if (zp->z_object == object) {
1196 refcount_add(&zp->z_refcnt, RL_TAG);
1201 zp = ztest_znode_init(object);
1202 refcount_add(&zp->z_refcnt, RL_TAG);
1203 list_insert_head(&zll->z_list, zp);
1205 mutex_exit(&zll->z_lock);
1210 ztest_znode_put(ztest_ds_t *zd, ztest_znode_t *zp)
1213 ASSERT3U(zp->z_object, !=, 0);
1214 zll = &zd->zd_range_lock[zp->z_object & (ZTEST_OBJECT_LOCKS - 1)];
1215 mutex_enter(&zll->z_lock);
1216 refcount_remove(&zp->z_refcnt, RL_TAG);
1217 if (refcount_is_zero(&zp->z_refcnt)) {
1218 list_remove(&zll->z_list, zp);
1219 ztest_znode_fini(zp);
1221 mutex_exit(&zll->z_lock);
1226 ztest_rll_init(rll_t *rll)
1228 rll->rll_writer = NULL;
1229 rll->rll_readers = 0;
1230 mutex_init(&rll->rll_lock, NULL, MUTEX_DEFAULT, NULL);
1231 cv_init(&rll->rll_cv, NULL, CV_DEFAULT, NULL);
1235 ztest_rll_destroy(rll_t *rll)
1237 ASSERT(rll->rll_writer == NULL);
1238 ASSERT(rll->rll_readers == 0);
1239 mutex_destroy(&rll->rll_lock);
1240 cv_destroy(&rll->rll_cv);
1244 ztest_rll_lock(rll_t *rll, rl_type_t type)
1246 mutex_enter(&rll->rll_lock);
1248 if (type == RL_READER) {
1249 while (rll->rll_writer != NULL)
1250 (void) cv_wait(&rll->rll_cv, &rll->rll_lock);
1253 while (rll->rll_writer != NULL || rll->rll_readers)
1254 (void) cv_wait(&rll->rll_cv, &rll->rll_lock);
1255 rll->rll_writer = curthread;
1258 mutex_exit(&rll->rll_lock);
1262 ztest_rll_unlock(rll_t *rll)
1264 mutex_enter(&rll->rll_lock);
1266 if (rll->rll_writer) {
1267 ASSERT(rll->rll_readers == 0);
1268 rll->rll_writer = NULL;
1270 ASSERT(rll->rll_readers != 0);
1271 ASSERT(rll->rll_writer == NULL);
1275 if (rll->rll_writer == NULL && rll->rll_readers == 0)
1276 cv_broadcast(&rll->rll_cv);
1278 mutex_exit(&rll->rll_lock);
1282 ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type)
1284 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1286 ztest_rll_lock(rll, type);
1290 ztest_object_unlock(ztest_ds_t *zd, uint64_t object)
1292 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1294 ztest_rll_unlock(rll);
1297 static ztest_zrl_t *
1298 ztest_zrl_init(rl_t *rl, ztest_znode_t *zp)
1300 ztest_zrl_t *zrl = umem_alloc(sizeof (*zrl), UMEM_NOFAIL);
1302 zrl->z_ztznode = zp;
1307 ztest_zrl_fini(ztest_zrl_t *zrl)
1309 umem_free(zrl, sizeof (*zrl));
1312 static ztest_zrl_t *
1313 ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset,
1314 uint64_t size, rl_type_t type)
1316 ztest_znode_t *zp = ztest_znode_get(zd, object);
1317 rl_t *rl = zfs_range_lock(&zp->z_range_lock, offset,
1319 return (ztest_zrl_init(rl, zp));
1323 ztest_range_unlock(ztest_ds_t *zd, ztest_zrl_t *zrl)
1325 zfs_range_unlock(zrl->z_rl);
1326 ztest_znode_put(zd, zrl->z_ztznode);
1327 ztest_zrl_fini(zrl);
1331 ztest_zd_init(ztest_ds_t *zd, ztest_shared_ds_t *szd, objset_t *os)
1334 zd->zd_zilog = dmu_objset_zil(os);
1335 zd->zd_shared = szd;
1336 dmu_objset_name(os, zd->zd_name);
1339 if (zd->zd_shared != NULL)
1340 zd->zd_shared->zd_seq = 0;
1342 VERIFY(rwlock_init(&zd->zd_zilog_lock, USYNC_THREAD, NULL) == 0);
1343 mutex_init(&zd->zd_dirobj_lock, NULL, MUTEX_DEFAULT, NULL);
1345 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1346 ztest_rll_init(&zd->zd_object_lock[l]);
1348 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
1349 ztest_zll_init(&zd->zd_range_lock[l]);
1353 ztest_zd_fini(ztest_ds_t *zd)
1357 mutex_destroy(&zd->zd_dirobj_lock);
1358 (void) rwlock_destroy(&zd->zd_zilog_lock);
1360 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1361 ztest_rll_destroy(&zd->zd_object_lock[l]);
1363 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
1364 ztest_zll_destroy(&zd->zd_range_lock[l]);
1367 #define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1370 ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag)
1376 * Attempt to assign tx to some transaction group.
1378 error = dmu_tx_assign(tx, txg_how);
1380 if (error == ERESTART) {
1381 ASSERT(txg_how == TXG_NOWAIT);
1384 ASSERT3U(error, ==, ENOSPC);
1385 ztest_record_enospc(tag);
1390 txg = dmu_tx_get_txg(tx);
1396 ztest_pattern_set(void *buf, uint64_t size, uint64_t value)
1399 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1407 ztest_pattern_match(void *buf, uint64_t size, uint64_t value)
1410 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1414 diff |= (value - *ip++);
1421 ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1422 uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
1424 bt->bt_magic = BT_MAGIC;
1425 bt->bt_objset = dmu_objset_id(os);
1426 bt->bt_object = object;
1427 bt->bt_offset = offset;
1430 bt->bt_crtxg = crtxg;
1434 ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1435 uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
1437 ASSERT3U(bt->bt_magic, ==, BT_MAGIC);
1438 ASSERT3U(bt->bt_objset, ==, dmu_objset_id(os));
1439 ASSERT3U(bt->bt_object, ==, object);
1440 ASSERT3U(bt->bt_offset, ==, offset);
1441 ASSERT3U(bt->bt_gen, <=, gen);
1442 ASSERT3U(bt->bt_txg, <=, txg);
1443 ASSERT3U(bt->bt_crtxg, ==, crtxg);
1446 static ztest_block_tag_t *
1447 ztest_bt_bonus(dmu_buf_t *db)
1449 dmu_object_info_t doi;
1450 ztest_block_tag_t *bt;
1452 dmu_object_info_from_db(db, &doi);
1453 ASSERT3U(doi.doi_bonus_size, <=, db->db_size);
1454 ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt));
1455 bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt));
1464 #define lrz_type lr_mode
1465 #define lrz_blocksize lr_uid
1466 #define lrz_ibshift lr_gid
1467 #define lrz_bonustype lr_rdev
1468 #define lrz_bonuslen lr_crtime[1]
1471 ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr)
1473 char *name = (void *)(lr + 1); /* name follows lr */
1474 size_t namesize = strlen(name) + 1;
1477 if (zil_replaying(zd->zd_zilog, tx))
1480 itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize);
1481 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1482 sizeof (*lr) + namesize - sizeof (lr_t));
1484 zil_itx_assign(zd->zd_zilog, itx, tx);
1488 ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr, uint64_t object)
1490 char *name = (void *)(lr + 1); /* name follows lr */
1491 size_t namesize = strlen(name) + 1;
1494 if (zil_replaying(zd->zd_zilog, tx))
1497 itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize);
1498 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1499 sizeof (*lr) + namesize - sizeof (lr_t));
1501 itx->itx_oid = object;
1502 zil_itx_assign(zd->zd_zilog, itx, tx);
1506 ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr)
1509 itx_wr_state_t write_state = ztest_random(WR_NUM_STATES);
1511 if (zil_replaying(zd->zd_zilog, tx))
1514 if (lr->lr_length > ZIL_MAX_LOG_DATA)
1515 write_state = WR_INDIRECT;
1517 itx = zil_itx_create(TX_WRITE,
1518 sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0));
1520 if (write_state == WR_COPIED &&
1521 dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length,
1522 ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) {
1523 zil_itx_destroy(itx);
1524 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1525 write_state = WR_NEED_COPY;
1527 itx->itx_private = zd;
1528 itx->itx_wr_state = write_state;
1529 itx->itx_sync = (ztest_random(8) == 0);
1530 itx->itx_sod += (write_state == WR_NEED_COPY ? lr->lr_length : 0);
1532 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1533 sizeof (*lr) - sizeof (lr_t));
1535 zil_itx_assign(zd->zd_zilog, itx, tx);
1539 ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr)
1543 if (zil_replaying(zd->zd_zilog, tx))
1546 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1547 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1548 sizeof (*lr) - sizeof (lr_t));
1550 itx->itx_sync = B_FALSE;
1551 zil_itx_assign(zd->zd_zilog, itx, tx);
1555 ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr)
1559 if (zil_replaying(zd->zd_zilog, tx))
1562 itx = zil_itx_create(TX_SETATTR, sizeof (*lr));
1563 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1564 sizeof (*lr) - sizeof (lr_t));
1566 itx->itx_sync = B_FALSE;
1567 zil_itx_assign(zd->zd_zilog, itx, tx);
1574 ztest_replay_create(ztest_ds_t *zd, lr_create_t *lr, boolean_t byteswap)
1576 char *name = (void *)(lr + 1); /* name follows lr */
1577 objset_t *os = zd->zd_os;
1578 ztest_block_tag_t *bbt;
1585 byteswap_uint64_array(lr, sizeof (*lr));
1587 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1588 ASSERT(name[0] != '\0');
1590 tx = dmu_tx_create(os);
1592 dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name);
1594 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1595 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1597 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1600 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1604 ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid);
1606 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1607 if (lr->lr_foid == 0) {
1608 lr->lr_foid = zap_create(os,
1609 lr->lrz_type, lr->lrz_bonustype,
1610 lr->lrz_bonuslen, tx);
1612 error = zap_create_claim(os, lr->lr_foid,
1613 lr->lrz_type, lr->lrz_bonustype,
1614 lr->lrz_bonuslen, tx);
1617 if (lr->lr_foid == 0) {
1618 lr->lr_foid = dmu_object_alloc(os,
1619 lr->lrz_type, 0, lr->lrz_bonustype,
1620 lr->lrz_bonuslen, tx);
1622 error = dmu_object_claim(os, lr->lr_foid,
1623 lr->lrz_type, 0, lr->lrz_bonustype,
1624 lr->lrz_bonuslen, tx);
1629 ASSERT3U(error, ==, EEXIST);
1630 ASSERT(zd->zd_zilog->zl_replay);
1635 ASSERT(lr->lr_foid != 0);
1637 if (lr->lrz_type != DMU_OT_ZAP_OTHER)
1638 VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid,
1639 lr->lrz_blocksize, lr->lrz_ibshift, tx));
1641 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1642 bbt = ztest_bt_bonus(db);
1643 dmu_buf_will_dirty(db, tx);
1644 ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_gen, txg, txg);
1645 dmu_buf_rele(db, FTAG);
1647 VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1,
1650 (void) ztest_log_create(zd, tx, lr);
1658 ztest_replay_remove(ztest_ds_t *zd, lr_remove_t *lr, boolean_t byteswap)
1660 char *name = (void *)(lr + 1); /* name follows lr */
1661 objset_t *os = zd->zd_os;
1662 dmu_object_info_t doi;
1664 uint64_t object, txg;
1667 byteswap_uint64_array(lr, sizeof (*lr));
1669 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1670 ASSERT(name[0] != '\0');
1673 zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object));
1674 ASSERT(object != 0);
1676 ztest_object_lock(zd, object, RL_WRITER);
1678 VERIFY3U(0, ==, dmu_object_info(os, object, &doi));
1680 tx = dmu_tx_create(os);
1682 dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name);
1683 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1685 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1687 ztest_object_unlock(zd, object);
1691 if (doi.doi_type == DMU_OT_ZAP_OTHER) {
1692 VERIFY3U(0, ==, zap_destroy(os, object, tx));
1694 VERIFY3U(0, ==, dmu_object_free(os, object, tx));
1697 VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx));
1699 (void) ztest_log_remove(zd, tx, lr, object);
1703 ztest_object_unlock(zd, object);
1709 ztest_replay_write(ztest_ds_t *zd, lr_write_t *lr, boolean_t byteswap)
1711 objset_t *os = zd->zd_os;
1712 void *data = lr + 1; /* data follows lr */
1713 uint64_t offset, length;
1714 ztest_block_tag_t *bt = data;
1715 ztest_block_tag_t *bbt;
1716 uint64_t gen, txg, lrtxg, crtxg;
1717 dmu_object_info_t doi;
1720 arc_buf_t *abuf = NULL;
1724 byteswap_uint64_array(lr, sizeof (*lr));
1726 offset = lr->lr_offset;
1727 length = lr->lr_length;
1729 /* If it's a dmu_sync() block, write the whole block */
1730 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
1731 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
1732 if (length < blocksize) {
1733 offset -= offset % blocksize;
1738 if (bt->bt_magic == BSWAP_64(BT_MAGIC))
1739 byteswap_uint64_array(bt, sizeof (*bt));
1741 if (bt->bt_magic != BT_MAGIC)
1744 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1745 rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER);
1747 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1749 dmu_object_info_from_db(db, &doi);
1751 bbt = ztest_bt_bonus(db);
1752 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1754 crtxg = bbt->bt_crtxg;
1755 lrtxg = lr->lr_common.lrc_txg;
1757 tx = dmu_tx_create(os);
1759 dmu_tx_hold_write(tx, lr->lr_foid, offset, length);
1761 if (ztest_random(8) == 0 && length == doi.doi_data_block_size &&
1762 P2PHASE(offset, length) == 0)
1763 abuf = dmu_request_arcbuf(db, length);
1765 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1768 dmu_return_arcbuf(abuf);
1769 dmu_buf_rele(db, FTAG);
1770 ztest_range_unlock(zd, rl);
1771 ztest_object_unlock(zd, lr->lr_foid);
1777 * Usually, verify the old data before writing new data --
1778 * but not always, because we also want to verify correct
1779 * behavior when the data was not recently read into cache.
1781 ASSERT(offset % doi.doi_data_block_size == 0);
1782 if (ztest_random(4) != 0) {
1783 int prefetch = ztest_random(2) ?
1784 DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
1785 ztest_block_tag_t rbt;
1787 VERIFY(dmu_read(os, lr->lr_foid, offset,
1788 sizeof (rbt), &rbt, prefetch) == 0);
1789 if (rbt.bt_magic == BT_MAGIC) {
1790 ztest_bt_verify(&rbt, os, lr->lr_foid,
1791 offset, gen, txg, crtxg);
1796 * Writes can appear to be newer than the bonus buffer because
1797 * the ztest_get_data() callback does a dmu_read() of the
1798 * open-context data, which may be different than the data
1799 * as it was when the write was generated.
1801 if (zd->zd_zilog->zl_replay) {
1802 ztest_bt_verify(bt, os, lr->lr_foid, offset,
1803 MAX(gen, bt->bt_gen), MAX(txg, lrtxg),
1808 * Set the bt's gen/txg to the bonus buffer's gen/txg
1809 * so that all of the usual ASSERTs will work.
1811 ztest_bt_generate(bt, os, lr->lr_foid, offset, gen, txg, crtxg);
1815 dmu_write(os, lr->lr_foid, offset, length, data, tx);
1817 bcopy(data, abuf->b_data, length);
1818 dmu_assign_arcbuf(db, offset, abuf, tx);
1821 (void) ztest_log_write(zd, tx, lr);
1823 dmu_buf_rele(db, FTAG);
1827 ztest_range_unlock(zd, rl);
1828 ztest_object_unlock(zd, lr->lr_foid);
1834 ztest_replay_truncate(ztest_ds_t *zd, lr_truncate_t *lr, boolean_t byteswap)
1836 objset_t *os = zd->zd_os;
1842 byteswap_uint64_array(lr, sizeof (*lr));
1844 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1845 rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length,
1848 tx = dmu_tx_create(os);
1850 dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length);
1852 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1854 ztest_range_unlock(zd, rl);
1855 ztest_object_unlock(zd, lr->lr_foid);
1859 VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset,
1860 lr->lr_length, tx) == 0);
1862 (void) ztest_log_truncate(zd, tx, lr);
1866 ztest_range_unlock(zd, rl);
1867 ztest_object_unlock(zd, lr->lr_foid);
1873 ztest_replay_setattr(ztest_ds_t *zd, lr_setattr_t *lr, boolean_t byteswap)
1875 objset_t *os = zd->zd_os;
1878 ztest_block_tag_t *bbt;
1879 uint64_t txg, lrtxg, crtxg;
1882 byteswap_uint64_array(lr, sizeof (*lr));
1884 ztest_object_lock(zd, lr->lr_foid, RL_WRITER);
1886 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1888 tx = dmu_tx_create(os);
1889 dmu_tx_hold_bonus(tx, lr->lr_foid);
1891 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1893 dmu_buf_rele(db, FTAG);
1894 ztest_object_unlock(zd, lr->lr_foid);
1898 bbt = ztest_bt_bonus(db);
1899 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1900 crtxg = bbt->bt_crtxg;
1901 lrtxg = lr->lr_common.lrc_txg;
1903 if (zd->zd_zilog->zl_replay) {
1904 ASSERT(lr->lr_size != 0);
1905 ASSERT(lr->lr_mode != 0);
1909 * Randomly change the size and increment the generation.
1911 lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) *
1913 lr->lr_mode = bbt->bt_gen + 1;
1918 * Verify that the current bonus buffer is not newer than our txg.
1920 ztest_bt_verify(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode,
1921 MAX(txg, lrtxg), crtxg);
1923 dmu_buf_will_dirty(db, tx);
1925 ASSERT3U(lr->lr_size, >=, sizeof (*bbt));
1926 ASSERT3U(lr->lr_size, <=, db->db_size);
1927 VERIFY0(dmu_set_bonus(db, lr->lr_size, tx));
1928 bbt = ztest_bt_bonus(db);
1930 ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode, txg, crtxg);
1932 dmu_buf_rele(db, FTAG);
1934 (void) ztest_log_setattr(zd, tx, lr);
1938 ztest_object_unlock(zd, lr->lr_foid);
1943 zil_replay_func_t ztest_replay_vector[TX_MAX_TYPE] = {
1944 NULL, /* 0 no such transaction type */
1945 (zil_replay_func_t)ztest_replay_create, /* TX_CREATE */
1946 NULL, /* TX_MKDIR */
1947 NULL, /* TX_MKXATTR */
1948 NULL, /* TX_SYMLINK */
1949 (zil_replay_func_t)ztest_replay_remove, /* TX_REMOVE */
1950 NULL, /* TX_RMDIR */
1952 NULL, /* TX_RENAME */
1953 (zil_replay_func_t)ztest_replay_write, /* TX_WRITE */
1954 (zil_replay_func_t)ztest_replay_truncate, /* TX_TRUNCATE */
1955 (zil_replay_func_t)ztest_replay_setattr, /* TX_SETATTR */
1957 NULL, /* TX_CREATE_ACL */
1958 NULL, /* TX_CREATE_ATTR */
1959 NULL, /* TX_CREATE_ACL_ATTR */
1960 NULL, /* TX_MKDIR_ACL */
1961 NULL, /* TX_MKDIR_ATTR */
1962 NULL, /* TX_MKDIR_ACL_ATTR */
1963 NULL, /* TX_WRITE2 */
1967 * ZIL get_data callbacks
1969 typedef struct ztest_zgd_private {
1973 } ztest_zgd_private_t;
1976 ztest_get_done(zgd_t *zgd, int error)
1978 ztest_zgd_private_t *zzp = zgd->zgd_private;
1979 ztest_ds_t *zd = zzp->z_zd;
1980 uint64_t object = zzp->z_object;
1983 dmu_buf_rele(zgd->zgd_db, zgd);
1985 ztest_range_unlock(zd, zzp->z_rl);
1986 ztest_object_unlock(zd, object);
1988 if (error == 0 && zgd->zgd_bp)
1989 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1991 umem_free(zgd, sizeof (*zgd));
1992 umem_free(zzp, sizeof (*zzp));
1996 ztest_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1998 ztest_ds_t *zd = arg;
1999 objset_t *os = zd->zd_os;
2000 uint64_t object = lr->lr_foid;
2001 uint64_t offset = lr->lr_offset;
2002 uint64_t size = lr->lr_length;
2003 blkptr_t *bp = &lr->lr_blkptr;
2004 uint64_t txg = lr->lr_common.lrc_txg;
2006 dmu_object_info_t doi;
2010 ztest_zgd_private_t *zgd_private;
2012 ztest_object_lock(zd, object, RL_READER);
2013 error = dmu_bonus_hold(os, object, FTAG, &db);
2015 ztest_object_unlock(zd, object);
2019 crtxg = ztest_bt_bonus(db)->bt_crtxg;
2021 if (crtxg == 0 || crtxg > txg) {
2022 dmu_buf_rele(db, FTAG);
2023 ztest_object_unlock(zd, object);
2027 dmu_object_info_from_db(db, &doi);
2028 dmu_buf_rele(db, FTAG);
2031 zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL);
2032 zgd->zgd_zilog = zd->zd_zilog;
2033 zgd_private = umem_zalloc(sizeof (ztest_zgd_private_t), UMEM_NOFAIL);
2034 zgd_private->z_zd = zd;
2035 zgd_private->z_object = object;
2036 zgd->zgd_private = zgd_private;
2038 if (buf != NULL) { /* immediate write */
2039 zgd_private->z_rl = ztest_range_lock(zd, object, offset, size,
2042 error = dmu_read(os, object, offset, size, buf,
2043 DMU_READ_NO_PREFETCH);
2046 size = doi.doi_data_block_size;
2048 offset = P2ALIGN(offset, size);
2050 ASSERT(offset < size);
2054 zgd_private->z_rl = ztest_range_lock(zd, object, offset, size,
2057 error = dmu_buf_hold(os, object, offset, zgd, &db,
2058 DMU_READ_NO_PREFETCH);
2061 blkptr_t *obp = dmu_buf_get_blkptr(db);
2063 ASSERT(BP_IS_HOLE(bp));
2070 ASSERT(db->db_offset == offset);
2071 ASSERT(db->db_size == size);
2073 error = dmu_sync(zio, lr->lr_common.lrc_txg,
2074 ztest_get_done, zgd);
2081 ztest_get_done(zgd, error);
2087 ztest_lr_alloc(size_t lrsize, char *name)
2090 size_t namesize = name ? strlen(name) + 1 : 0;
2092 lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
2095 bcopy(name, lr + lrsize, namesize);
2101 ztest_lr_free(void *lr, size_t lrsize, char *name)
2103 size_t namesize = name ? strlen(name) + 1 : 0;
2105 umem_free(lr, lrsize + namesize);
2109 * Lookup a bunch of objects. Returns the number of objects not found.
2112 ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
2118 ASSERT(mutex_held(&zd->zd_dirobj_lock));
2120 for (i = 0; i < count; i++, od++) {
2122 error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
2123 sizeof (uint64_t), 1, &od->od_object);
2125 ASSERT(error == ENOENT);
2126 ASSERT(od->od_object == 0);
2130 ztest_block_tag_t *bbt;
2131 dmu_object_info_t doi;
2133 ASSERT(od->od_object != 0);
2134 ASSERT(missing == 0); /* there should be no gaps */
2136 ztest_object_lock(zd, od->od_object, RL_READER);
2137 VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
2138 od->od_object, FTAG, &db));
2139 dmu_object_info_from_db(db, &doi);
2140 bbt = ztest_bt_bonus(db);
2141 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
2142 od->od_type = doi.doi_type;
2143 od->od_blocksize = doi.doi_data_block_size;
2144 od->od_gen = bbt->bt_gen;
2145 dmu_buf_rele(db, FTAG);
2146 ztest_object_unlock(zd, od->od_object);
2154 ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
2159 ASSERT(mutex_held(&zd->zd_dirobj_lock));
2161 for (i = 0; i < count; i++, od++) {
2168 lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2170 lr->lr_doid = od->od_dir;
2171 lr->lr_foid = 0; /* 0 to allocate, > 0 to claim */
2172 lr->lrz_type = od->od_crtype;
2173 lr->lrz_blocksize = od->od_crblocksize;
2174 lr->lrz_ibshift = ztest_random_ibshift();
2175 lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
2176 lr->lrz_bonuslen = dmu_bonus_max();
2177 lr->lr_gen = od->od_crgen;
2178 lr->lr_crtime[0] = time(NULL);
2180 if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
2181 ASSERT(missing == 0);
2185 od->od_object = lr->lr_foid;
2186 od->od_type = od->od_crtype;
2187 od->od_blocksize = od->od_crblocksize;
2188 od->od_gen = od->od_crgen;
2189 ASSERT(od->od_object != 0);
2192 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2199 ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
2205 ASSERT(mutex_held(&zd->zd_dirobj_lock));
2209 for (i = count - 1; i >= 0; i--, od--) {
2216 * No object was found.
2218 if (od->od_object == 0)
2221 lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2223 lr->lr_doid = od->od_dir;
2225 if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
2226 ASSERT3U(error, ==, ENOSPC);
2231 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2238 ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
2244 lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
2246 lr->lr_foid = object;
2247 lr->lr_offset = offset;
2248 lr->lr_length = size;
2250 BP_ZERO(&lr->lr_blkptr);
2252 bcopy(data, lr + 1, size);
2254 error = ztest_replay_write(zd, lr, B_FALSE);
2256 ztest_lr_free(lr, sizeof (*lr) + size, NULL);
2262 ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2267 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2269 lr->lr_foid = object;
2270 lr->lr_offset = offset;
2271 lr->lr_length = size;
2273 error = ztest_replay_truncate(zd, lr, B_FALSE);
2275 ztest_lr_free(lr, sizeof (*lr), NULL);
2281 ztest_setattr(ztest_ds_t *zd, uint64_t object)
2286 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2288 lr->lr_foid = object;
2292 error = ztest_replay_setattr(zd, lr, B_FALSE);
2294 ztest_lr_free(lr, sizeof (*lr), NULL);
2300 ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2302 objset_t *os = zd->zd_os;
2307 txg_wait_synced(dmu_objset_pool(os), 0);
2309 ztest_object_lock(zd, object, RL_READER);
2310 rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
2312 tx = dmu_tx_create(os);
2314 dmu_tx_hold_write(tx, object, offset, size);
2316 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
2319 dmu_prealloc(os, object, offset, size, tx);
2321 txg_wait_synced(dmu_objset_pool(os), txg);
2323 (void) dmu_free_long_range(os, object, offset, size);
2326 ztest_range_unlock(zd, rl);
2327 ztest_object_unlock(zd, object);
2331 ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
2334 ztest_block_tag_t wbt;
2335 dmu_object_info_t doi;
2336 enum ztest_io_type io_type;
2340 VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
2341 blocksize = doi.doi_data_block_size;
2342 data = umem_alloc(blocksize, UMEM_NOFAIL);
2345 * Pick an i/o type at random, biased toward writing block tags.
2347 io_type = ztest_random(ZTEST_IO_TYPES);
2348 if (ztest_random(2) == 0)
2349 io_type = ZTEST_IO_WRITE_TAG;
2351 (void) rw_rdlock(&zd->zd_zilog_lock);
2355 case ZTEST_IO_WRITE_TAG:
2356 ztest_bt_generate(&wbt, zd->zd_os, object, offset, 0, 0, 0);
2357 (void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
2360 case ZTEST_IO_WRITE_PATTERN:
2361 (void) memset(data, 'a' + (object + offset) % 5, blocksize);
2362 if (ztest_random(2) == 0) {
2364 * Induce fletcher2 collisions to ensure that
2365 * zio_ddt_collision() detects and resolves them
2366 * when using fletcher2-verify for deduplication.
2368 ((uint64_t *)data)[0] ^= 1ULL << 63;
2369 ((uint64_t *)data)[4] ^= 1ULL << 63;
2371 (void) ztest_write(zd, object, offset, blocksize, data);
2374 case ZTEST_IO_WRITE_ZEROES:
2375 bzero(data, blocksize);
2376 (void) ztest_write(zd, object, offset, blocksize, data);
2379 case ZTEST_IO_TRUNCATE:
2380 (void) ztest_truncate(zd, object, offset, blocksize);
2383 case ZTEST_IO_SETATTR:
2384 (void) ztest_setattr(zd, object);
2389 case ZTEST_IO_REWRITE:
2390 (void) rw_rdlock(&ztest_name_lock);
2391 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2392 ZFS_PROP_CHECKSUM, spa_dedup_checksum(ztest_spa),
2394 VERIFY(err == 0 || err == ENOSPC);
2395 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2396 ZFS_PROP_COMPRESSION,
2397 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION),
2399 VERIFY(err == 0 || err == ENOSPC);
2400 (void) rw_unlock(&ztest_name_lock);
2402 VERIFY0(dmu_read(zd->zd_os, object, offset, blocksize, data,
2403 DMU_READ_NO_PREFETCH));
2405 (void) ztest_write(zd, object, offset, blocksize, data);
2409 (void) rw_unlock(&zd->zd_zilog_lock);
2411 umem_free(data, blocksize);
2415 * Initialize an object description template.
2418 ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
2419 dmu_object_type_t type, uint64_t blocksize, uint64_t gen)
2421 od->od_dir = ZTEST_DIROBJ;
2424 od->od_crtype = type;
2425 od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
2428 od->od_type = DMU_OT_NONE;
2429 od->od_blocksize = 0;
2432 (void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
2433 tag, (longlong_t)id, (u_longlong_t)index);
2437 * Lookup or create the objects for a test using the od template.
2438 * If the objects do not all exist, or if 'remove' is specified,
2439 * remove any existing objects and create new ones. Otherwise,
2440 * use the existing objects.
2443 ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
2445 int count = size / sizeof (*od);
2448 mutex_enter(&zd->zd_dirobj_lock);
2449 if ((ztest_lookup(zd, od, count) != 0 || remove) &&
2450 (ztest_remove(zd, od, count) != 0 ||
2451 ztest_create(zd, od, count) != 0))
2454 mutex_exit(&zd->zd_dirobj_lock);
2461 ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
2463 zilog_t *zilog = zd->zd_zilog;
2465 (void) rw_rdlock(&zd->zd_zilog_lock);
2467 zil_commit(zilog, ztest_random(ZTEST_OBJECTS));
2470 * Remember the committed values in zd, which is in parent/child
2471 * shared memory. If we die, the next iteration of ztest_run()
2472 * will verify that the log really does contain this record.
2474 mutex_enter(&zilog->zl_lock);
2475 ASSERT(zd->zd_shared != NULL);
2476 ASSERT3U(zd->zd_shared->zd_seq, <=, zilog->zl_commit_lr_seq);
2477 zd->zd_shared->zd_seq = zilog->zl_commit_lr_seq;
2478 mutex_exit(&zilog->zl_lock);
2480 (void) rw_unlock(&zd->zd_zilog_lock);
2484 * This function is designed to simulate the operations that occur during a
2485 * mount/unmount operation. We hold the dataset across these operations in an
2486 * attempt to expose any implicit assumptions about ZIL management.
2490 ztest_zil_remount(ztest_ds_t *zd, uint64_t id)
2492 objset_t *os = zd->zd_os;
2495 * We grab the zd_dirobj_lock to ensure that no other thread is
2496 * updating the zil (i.e. adding in-memory log records) and the
2497 * zd_zilog_lock to block any I/O.
2499 mutex_enter(&zd->zd_dirobj_lock);
2500 (void) rw_wrlock(&zd->zd_zilog_lock);
2502 /* zfs_sb_teardown() */
2503 zil_close(zd->zd_zilog);
2505 /* zfsvfs_setup() */
2506 VERIFY(zil_open(os, ztest_get_data) == zd->zd_zilog);
2507 zil_replay(os, zd, ztest_replay_vector);
2509 (void) rw_unlock(&zd->zd_zilog_lock);
2510 mutex_exit(&zd->zd_dirobj_lock);
2514 * Verify that we can't destroy an active pool, create an existing pool,
2515 * or create a pool with a bad vdev spec.
2519 ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2521 ztest_shared_opts_t *zo = &ztest_opts;
2526 * Attempt to create using a bad file.
2528 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2529 VERIFY3U(ENOENT, ==,
2530 spa_create("ztest_bad_file", nvroot, NULL, NULL));
2531 nvlist_free(nvroot);
2534 * Attempt to create using a bad mirror.
2536 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 2, 1);
2537 VERIFY3U(ENOENT, ==,
2538 spa_create("ztest_bad_mirror", nvroot, NULL, NULL));
2539 nvlist_free(nvroot);
2542 * Attempt to create an existing pool. It shouldn't matter
2543 * what's in the nvroot; we should fail with EEXIST.
2545 (void) rw_rdlock(&ztest_name_lock);
2546 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2547 VERIFY3U(EEXIST, ==, spa_create(zo->zo_pool, nvroot, NULL, NULL));
2548 nvlist_free(nvroot);
2549 VERIFY3U(0, ==, spa_open(zo->zo_pool, &spa, FTAG));
2550 VERIFY3U(EBUSY, ==, spa_destroy(zo->zo_pool));
2551 spa_close(spa, FTAG);
2553 (void) rw_unlock(&ztest_name_lock);
2558 ztest_spa_upgrade(ztest_ds_t *zd, uint64_t id)
2561 uint64_t initial_version = SPA_VERSION_INITIAL;
2562 uint64_t version, newversion;
2563 nvlist_t *nvroot, *props;
2566 mutex_enter(&ztest_vdev_lock);
2567 name = kmem_asprintf("%s_upgrade", ztest_opts.zo_pool);
2570 * Clean up from previous runs.
2572 (void) spa_destroy(name);
2574 nvroot = make_vdev_root(NULL, NULL, name, ztest_opts.zo_vdev_size, 0,
2575 0, ztest_opts.zo_raidz, ztest_opts.zo_mirrors, 1);
2578 * If we're configuring a RAIDZ device then make sure that the
2579 * the initial version is capable of supporting that feature.
2581 switch (ztest_opts.zo_raidz_parity) {
2584 initial_version = SPA_VERSION_INITIAL;
2587 initial_version = SPA_VERSION_RAIDZ2;
2590 initial_version = SPA_VERSION_RAIDZ3;
2595 * Create a pool with a spa version that can be upgraded. Pick
2596 * a value between initial_version and SPA_VERSION_BEFORE_FEATURES.
2599 version = ztest_random_spa_version(initial_version);
2600 } while (version > SPA_VERSION_BEFORE_FEATURES);
2602 props = fnvlist_alloc();
2603 fnvlist_add_uint64(props,
2604 zpool_prop_to_name(ZPOOL_PROP_VERSION), version);
2605 VERIFY3S(spa_create(name, nvroot, props, NULL), ==, 0);
2606 fnvlist_free(nvroot);
2607 fnvlist_free(props);
2609 VERIFY3S(spa_open(name, &spa, FTAG), ==, 0);
2610 VERIFY3U(spa_version(spa), ==, version);
2611 newversion = ztest_random_spa_version(version + 1);
2613 if (ztest_opts.zo_verbose >= 4) {
2614 (void) printf("upgrading spa version from %llu to %llu\n",
2615 (u_longlong_t)version, (u_longlong_t)newversion);
2618 spa_upgrade(spa, newversion);
2619 VERIFY3U(spa_version(spa), >, version);
2620 VERIFY3U(spa_version(spa), ==, fnvlist_lookup_uint64(spa->spa_config,
2621 zpool_prop_to_name(ZPOOL_PROP_VERSION)));
2622 spa_close(spa, FTAG);
2625 mutex_exit(&ztest_vdev_lock);
2629 vdev_lookup_by_path(vdev_t *vd, const char *path)
2634 if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
2637 for (c = 0; c < vd->vdev_children; c++)
2638 if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
2646 * Find the first available hole which can be used as a top-level.
2649 find_vdev_hole(spa_t *spa)
2651 vdev_t *rvd = spa->spa_root_vdev;
2654 ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
2656 for (c = 0; c < rvd->vdev_children; c++) {
2657 vdev_t *cvd = rvd->vdev_child[c];
2659 if (cvd->vdev_ishole)
2666 * Verify that vdev_add() works as expected.
2670 ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2672 ztest_shared_t *zs = ztest_shared;
2673 spa_t *spa = ztest_spa;
2679 mutex_enter(&ztest_vdev_lock);
2680 leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * ztest_opts.zo_raidz;
2682 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2684 ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2687 * If we have slogs then remove them 1/4 of the time.
2689 if (spa_has_slogs(spa) && ztest_random(4) == 0) {
2691 * Grab the guid from the head of the log class rotor.
2693 guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid;
2695 spa_config_exit(spa, SCL_VDEV, FTAG);
2698 * We have to grab the zs_name_lock as writer to
2699 * prevent a race between removing a slog (dmu_objset_find)
2700 * and destroying a dataset. Removing the slog will
2701 * grab a reference on the dataset which may cause
2702 * dsl_destroy_head() to fail with EBUSY thus
2703 * leaving the dataset in an inconsistent state.
2705 rw_wrlock(&ztest_name_lock);
2706 error = spa_vdev_remove(spa, guid, B_FALSE);
2707 rw_unlock(&ztest_name_lock);
2709 if (error && error != EEXIST)
2710 fatal(0, "spa_vdev_remove() = %d", error);
2712 spa_config_exit(spa, SCL_VDEV, FTAG);
2715 * Make 1/4 of the devices be log devices.
2717 nvroot = make_vdev_root(NULL, NULL, NULL,
2718 ztest_opts.zo_vdev_size, 0,
2719 ztest_random(4) == 0, ztest_opts.zo_raidz,
2722 error = spa_vdev_add(spa, nvroot);
2723 nvlist_free(nvroot);
2725 if (error == ENOSPC)
2726 ztest_record_enospc("spa_vdev_add");
2727 else if (error != 0)
2728 fatal(0, "spa_vdev_add() = %d", error);
2731 mutex_exit(&ztest_vdev_lock);
2735 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
2739 ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
2741 ztest_shared_t *zs = ztest_shared;
2742 spa_t *spa = ztest_spa;
2743 vdev_t *rvd = spa->spa_root_vdev;
2744 spa_aux_vdev_t *sav;
2750 path = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2752 if (ztest_random(2) == 0) {
2753 sav = &spa->spa_spares;
2754 aux = ZPOOL_CONFIG_SPARES;
2756 sav = &spa->spa_l2cache;
2757 aux = ZPOOL_CONFIG_L2CACHE;
2760 mutex_enter(&ztest_vdev_lock);
2762 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2764 if (sav->sav_count != 0 && ztest_random(4) == 0) {
2766 * Pick a random device to remove.
2768 guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
2771 * Find an unused device we can add.
2773 zs->zs_vdev_aux = 0;
2776 (void) snprintf(path, MAXPATHLEN, ztest_aux_template,
2777 ztest_opts.zo_dir, ztest_opts.zo_pool, aux,
2779 for (c = 0; c < sav->sav_count; c++)
2780 if (strcmp(sav->sav_vdevs[c]->vdev_path,
2783 if (c == sav->sav_count &&
2784 vdev_lookup_by_path(rvd, path) == NULL)
2790 spa_config_exit(spa, SCL_VDEV, FTAG);
2796 nvlist_t *nvroot = make_vdev_root(NULL, aux, NULL,
2797 (ztest_opts.zo_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
2798 error = spa_vdev_add(spa, nvroot);
2800 fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
2801 nvlist_free(nvroot);
2804 * Remove an existing device. Sometimes, dirty its
2805 * vdev state first to make sure we handle removal
2806 * of devices that have pending state changes.
2808 if (ztest_random(2) == 0)
2809 (void) vdev_online(spa, guid, 0, NULL);
2811 error = spa_vdev_remove(spa, guid, B_FALSE);
2812 if (error != 0 && error != EBUSY)
2813 fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
2816 mutex_exit(&ztest_vdev_lock);
2818 umem_free(path, MAXPATHLEN);
2822 * split a pool if it has mirror tlvdevs
2826 ztest_split_pool(ztest_ds_t *zd, uint64_t id)
2828 ztest_shared_t *zs = ztest_shared;
2829 spa_t *spa = ztest_spa;
2830 vdev_t *rvd = spa->spa_root_vdev;
2831 nvlist_t *tree, **child, *config, *split, **schild;
2832 uint_t c, children, schildren = 0, lastlogid = 0;
2835 mutex_enter(&ztest_vdev_lock);
2837 /* ensure we have a useable config; mirrors of raidz aren't supported */
2838 if (zs->zs_mirrors < 3 || ztest_opts.zo_raidz > 1) {
2839 mutex_exit(&ztest_vdev_lock);
2843 /* clean up the old pool, if any */
2844 (void) spa_destroy("splitp");
2846 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2848 /* generate a config from the existing config */
2849 mutex_enter(&spa->spa_props_lock);
2850 VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
2852 mutex_exit(&spa->spa_props_lock);
2854 VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2857 schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
2858 for (c = 0; c < children; c++) {
2859 vdev_t *tvd = rvd->vdev_child[c];
2863 if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
2864 VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
2866 VERIFY(nvlist_add_string(schild[schildren],
2867 ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
2868 VERIFY(nvlist_add_uint64(schild[schildren],
2869 ZPOOL_CONFIG_IS_HOLE, 1) == 0);
2871 lastlogid = schildren;
2876 VERIFY(nvlist_lookup_nvlist_array(child[c],
2877 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2878 VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
2881 /* OK, create a config that can be used to split */
2882 VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
2883 VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
2884 VDEV_TYPE_ROOT) == 0);
2885 VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
2886 lastlogid != 0 ? lastlogid : schildren) == 0);
2888 VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
2889 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
2891 for (c = 0; c < schildren; c++)
2892 nvlist_free(schild[c]);
2896 spa_config_exit(spa, SCL_VDEV, FTAG);
2898 (void) rw_wrlock(&ztest_name_lock);
2899 error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
2900 (void) rw_unlock(&ztest_name_lock);
2902 nvlist_free(config);
2905 (void) printf("successful split - results:\n");
2906 mutex_enter(&spa_namespace_lock);
2907 show_pool_stats(spa);
2908 show_pool_stats(spa_lookup("splitp"));
2909 mutex_exit(&spa_namespace_lock);
2913 mutex_exit(&ztest_vdev_lock);
2918 * Verify that we can attach and detach devices.
2922 ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
2924 ztest_shared_t *zs = ztest_shared;
2925 spa_t *spa = ztest_spa;
2926 spa_aux_vdev_t *sav = &spa->spa_spares;
2927 vdev_t *rvd = spa->spa_root_vdev;
2928 vdev_t *oldvd, *newvd, *pvd;
2932 uint64_t ashift = ztest_get_ashift();
2933 uint64_t oldguid, pguid;
2934 uint64_t oldsize, newsize;
2935 char *oldpath, *newpath;
2937 int oldvd_has_siblings = B_FALSE;
2938 int newvd_is_spare = B_FALSE;
2940 int error, expected_error;
2942 oldpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2943 newpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2945 mutex_enter(&ztest_vdev_lock);
2946 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
2948 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2951 * Decide whether to do an attach or a replace.
2953 replacing = ztest_random(2);
2956 * Pick a random top-level vdev.
2958 top = ztest_random_vdev_top(spa, B_TRUE);
2961 * Pick a random leaf within it.
2963 leaf = ztest_random(leaves);
2968 oldvd = rvd->vdev_child[top];
2969 if (zs->zs_mirrors >= 1) {
2970 ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
2971 ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
2972 oldvd = oldvd->vdev_child[leaf / ztest_opts.zo_raidz];
2974 if (ztest_opts.zo_raidz > 1) {
2975 ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
2976 ASSERT(oldvd->vdev_children == ztest_opts.zo_raidz);
2977 oldvd = oldvd->vdev_child[leaf % ztest_opts.zo_raidz];
2981 * If we're already doing an attach or replace, oldvd may be a
2982 * mirror vdev -- in which case, pick a random child.
2984 while (oldvd->vdev_children != 0) {
2985 oldvd_has_siblings = B_TRUE;
2986 ASSERT(oldvd->vdev_children >= 2);
2987 oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
2990 oldguid = oldvd->vdev_guid;
2991 oldsize = vdev_get_min_asize(oldvd);
2992 oldvd_is_log = oldvd->vdev_top->vdev_islog;
2993 (void) strcpy(oldpath, oldvd->vdev_path);
2994 pvd = oldvd->vdev_parent;
2995 pguid = pvd->vdev_guid;
2998 * If oldvd has siblings, then half of the time, detach it.
3000 if (oldvd_has_siblings && ztest_random(2) == 0) {
3001 spa_config_exit(spa, SCL_VDEV, FTAG);
3002 error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
3003 if (error != 0 && error != ENODEV && error != EBUSY &&
3005 fatal(0, "detach (%s) returned %d", oldpath, error);
3010 * For the new vdev, choose with equal probability between the two
3011 * standard paths (ending in either 'a' or 'b') or a random hot spare.
3013 if (sav->sav_count != 0 && ztest_random(3) == 0) {
3014 newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
3015 newvd_is_spare = B_TRUE;
3016 (void) strcpy(newpath, newvd->vdev_path);
3018 (void) snprintf(newpath, MAXPATHLEN, ztest_dev_template,
3019 ztest_opts.zo_dir, ztest_opts.zo_pool,
3020 top * leaves + leaf);
3021 if (ztest_random(2) == 0)
3022 newpath[strlen(newpath) - 1] = 'b';
3023 newvd = vdev_lookup_by_path(rvd, newpath);
3027 newsize = vdev_get_min_asize(newvd);
3030 * Make newsize a little bigger or smaller than oldsize.
3031 * If it's smaller, the attach should fail.
3032 * If it's larger, and we're doing a replace,
3033 * we should get dynamic LUN growth when we're done.
3035 newsize = 10 * oldsize / (9 + ztest_random(3));
3039 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
3040 * unless it's a replace; in that case any non-replacing parent is OK.
3042 * If newvd is already part of the pool, it should fail with EBUSY.
3044 * If newvd is too small, it should fail with EOVERFLOW.
3046 if (pvd->vdev_ops != &vdev_mirror_ops &&
3047 pvd->vdev_ops != &vdev_root_ops && (!replacing ||
3048 pvd->vdev_ops == &vdev_replacing_ops ||
3049 pvd->vdev_ops == &vdev_spare_ops))
3050 expected_error = ENOTSUP;
3051 else if (newvd_is_spare && (!replacing || oldvd_is_log))
3052 expected_error = ENOTSUP;
3053 else if (newvd == oldvd)
3054 expected_error = replacing ? 0 : EBUSY;
3055 else if (vdev_lookup_by_path(rvd, newpath) != NULL)
3056 expected_error = EBUSY;
3057 else if (newsize < oldsize)
3058 expected_error = EOVERFLOW;
3059 else if (ashift > oldvd->vdev_top->vdev_ashift)
3060 expected_error = EDOM;
3064 spa_config_exit(spa, SCL_VDEV, FTAG);
3067 * Build the nvlist describing newpath.
3069 root = make_vdev_root(newpath, NULL, NULL, newvd == NULL ? newsize : 0,
3070 ashift, 0, 0, 0, 1);
3072 error = spa_vdev_attach(spa, oldguid, root, replacing);
3077 * If our parent was the replacing vdev, but the replace completed,
3078 * then instead of failing with ENOTSUP we may either succeed,
3079 * fail with ENODEV, or fail with EOVERFLOW.
3081 if (expected_error == ENOTSUP &&
3082 (error == 0 || error == ENODEV || error == EOVERFLOW))
3083 expected_error = error;
3086 * If someone grew the LUN, the replacement may be too small.
3088 if (error == EOVERFLOW || error == EBUSY)
3089 expected_error = error;
3091 /* XXX workaround 6690467 */
3092 if (error != expected_error && expected_error != EBUSY) {
3093 fatal(0, "attach (%s %llu, %s %llu, %d) "
3094 "returned %d, expected %d",
3095 oldpath, oldsize, newpath,
3096 newsize, replacing, error, expected_error);
3099 mutex_exit(&ztest_vdev_lock);
3101 umem_free(oldpath, MAXPATHLEN);
3102 umem_free(newpath, MAXPATHLEN);
3106 * Callback function which expands the physical size of the vdev.
3109 grow_vdev(vdev_t *vd, void *arg)
3111 ASSERTV(spa_t *spa = vd->vdev_spa);
3112 size_t *newsize = arg;
3116 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3117 ASSERT(vd->vdev_ops->vdev_op_leaf);
3119 if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
3122 fsize = lseek(fd, 0, SEEK_END);
3123 VERIFY(ftruncate(fd, *newsize) == 0);
3125 if (ztest_opts.zo_verbose >= 6) {
3126 (void) printf("%s grew from %lu to %lu bytes\n",
3127 vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
3134 * Callback function which expands a given vdev by calling vdev_online().
3138 online_vdev(vdev_t *vd, void *arg)
3140 spa_t *spa = vd->vdev_spa;
3141 vdev_t *tvd = vd->vdev_top;
3142 uint64_t guid = vd->vdev_guid;
3143 uint64_t generation = spa->spa_config_generation + 1;
3144 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
3147 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3148 ASSERT(vd->vdev_ops->vdev_op_leaf);
3150 /* Calling vdev_online will initialize the new metaslabs */
3151 spa_config_exit(spa, SCL_STATE, spa);
3152 error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
3153 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3156 * If vdev_online returned an error or the underlying vdev_open
3157 * failed then we abort the expand. The only way to know that
3158 * vdev_open fails is by checking the returned newstate.
3160 if (error || newstate != VDEV_STATE_HEALTHY) {
3161 if (ztest_opts.zo_verbose >= 5) {
3162 (void) printf("Unable to expand vdev, state %llu, "
3163 "error %d\n", (u_longlong_t)newstate, error);
3167 ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
3170 * Since we dropped the lock we need to ensure that we're
3171 * still talking to the original vdev. It's possible this
3172 * vdev may have been detached/replaced while we were
3173 * trying to online it.
3175 if (generation != spa->spa_config_generation) {
3176 if (ztest_opts.zo_verbose >= 5) {
3177 (void) printf("vdev configuration has changed, "
3178 "guid %llu, state %llu, expected gen %llu, "
3181 (u_longlong_t)tvd->vdev_state,
3182 (u_longlong_t)generation,
3183 (u_longlong_t)spa->spa_config_generation);
3191 * Traverse the vdev tree calling the supplied function.
3192 * We continue to walk the tree until we either have walked all
3193 * children or we receive a non-NULL return from the callback.
3194 * If a NULL callback is passed, then we just return back the first
3195 * leaf vdev we encounter.
3198 vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
3202 if (vd->vdev_ops->vdev_op_leaf) {
3206 return (func(vd, arg));
3209 for (c = 0; c < vd->vdev_children; c++) {
3210 vdev_t *cvd = vd->vdev_child[c];
3211 if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
3218 * Verify that dynamic LUN growth works as expected.
3222 ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
3224 spa_t *spa = ztest_spa;
3226 metaslab_class_t *mc;
3227 metaslab_group_t *mg;
3228 size_t psize, newsize;
3230 uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
3232 mutex_enter(&ztest_vdev_lock);
3233 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3235 top = ztest_random_vdev_top(spa, B_TRUE);
3237 tvd = spa->spa_root_vdev->vdev_child[top];
3240 old_ms_count = tvd->vdev_ms_count;
3241 old_class_space = metaslab_class_get_space(mc);
3244 * Determine the size of the first leaf vdev associated with
3245 * our top-level device.
3247 vd = vdev_walk_tree(tvd, NULL, NULL);
3248 ASSERT3P(vd, !=, NULL);
3249 ASSERT(vd->vdev_ops->vdev_op_leaf);
3251 psize = vd->vdev_psize;
3254 * We only try to expand the vdev if it's healthy, less than 4x its
3255 * original size, and it has a valid psize.
3257 if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
3258 psize == 0 || psize >= 4 * ztest_opts.zo_vdev_size) {
3259 spa_config_exit(spa, SCL_STATE, spa);
3260 mutex_exit(&ztest_vdev_lock);
3264 newsize = psize + psize / 8;
3265 ASSERT3U(newsize, >, psize);
3267 if (ztest_opts.zo_verbose >= 6) {
3268 (void) printf("Expanding LUN %s from %lu to %lu\n",
3269 vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
3273 * Growing the vdev is a two step process:
3274 * 1). expand the physical size (i.e. relabel)
3275 * 2). online the vdev to create the new metaslabs
3277 if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
3278 vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
3279 tvd->vdev_state != VDEV_STATE_HEALTHY) {
3280 if (ztest_opts.zo_verbose >= 5) {
3281 (void) printf("Could not expand LUN because "
3282 "the vdev configuration changed.\n");
3284 spa_config_exit(spa, SCL_STATE, spa);
3285 mutex_exit(&ztest_vdev_lock);
3289 spa_config_exit(spa, SCL_STATE, spa);
3292 * Expanding the LUN will update the config asynchronously,
3293 * thus we must wait for the async thread to complete any
3294 * pending tasks before proceeding.
3298 mutex_enter(&spa->spa_async_lock);
3299 done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
3300 mutex_exit(&spa->spa_async_lock);
3303 txg_wait_synced(spa_get_dsl(spa), 0);
3304 (void) poll(NULL, 0, 100);
3307 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3309 tvd = spa->spa_root_vdev->vdev_child[top];
3310 new_ms_count = tvd->vdev_ms_count;
3311 new_class_space = metaslab_class_get_space(mc);
3313 if (tvd->vdev_mg != mg || mg->mg_class != mc) {
3314 if (ztest_opts.zo_verbose >= 5) {
3315 (void) printf("Could not verify LUN expansion due to "
3316 "intervening vdev offline or remove.\n");
3318 spa_config_exit(spa, SCL_STATE, spa);
3319 mutex_exit(&ztest_vdev_lock);
3324 * Make sure we were able to grow the vdev.
3326 if (new_ms_count <= old_ms_count)
3327 fatal(0, "LUN expansion failed: ms_count %llu <= %llu\n",
3328 old_ms_count, new_ms_count);
3331 * Make sure we were able to grow the pool.
3333 if (new_class_space <= old_class_space)
3334 fatal(0, "LUN expansion failed: class_space %llu <= %llu\n",
3335 old_class_space, new_class_space);
3337 if (ztest_opts.zo_verbose >= 5) {
3338 char oldnumbuf[6], newnumbuf[6];
3340 nicenum(old_class_space, oldnumbuf);
3341 nicenum(new_class_space, newnumbuf);
3342 (void) printf("%s grew from %s to %s\n",
3343 spa->spa_name, oldnumbuf, newnumbuf);
3346 spa_config_exit(spa, SCL_STATE, spa);
3347 mutex_exit(&ztest_vdev_lock);
3351 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
3355 ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3358 * Create the objects common to all ztest datasets.
3360 VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
3361 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
3365 ztest_dataset_create(char *dsname)
3367 uint64_t zilset = ztest_random(100);
3368 int err = dmu_objset_create(dsname, DMU_OST_OTHER, 0,
3369 ztest_objset_create_cb, NULL);
3371 if (err || zilset < 80)
3374 if (ztest_opts.zo_verbose >= 5)
3375 (void) printf("Setting dataset %s to sync always\n", dsname);
3376 return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC,
3377 ZFS_SYNC_ALWAYS, B_FALSE));
3382 ztest_objset_destroy_cb(const char *name, void *arg)
3385 dmu_object_info_t doi;
3389 * Verify that the dataset contains a directory object.
3391 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_TRUE, FTAG, &os));
3392 error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
3393 if (error != ENOENT) {
3394 /* We could have crashed in the middle of destroying it */
3396 ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
3397 ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
3399 dmu_objset_disown(os, FTAG);
3402 * Destroy the dataset.
3404 if (strchr(name, '@') != NULL) {
3405 VERIFY0(dsl_destroy_snapshot(name, B_FALSE));
3407 VERIFY0(dsl_destroy_head(name));
3413 ztest_snapshot_create(char *osname, uint64_t id)
3415 char snapname[MAXNAMELEN];
3418 (void) snprintf(snapname, sizeof (snapname), "%llu", (u_longlong_t)id);
3420 error = dmu_objset_snapshot_one(osname, snapname);
3421 if (error == ENOSPC) {
3422 ztest_record_enospc(FTAG);
3425 if (error != 0 && error != EEXIST) {
3426 fatal(0, "ztest_snapshot_create(%s@%s) = %d", osname,
3433 ztest_snapshot_destroy(char *osname, uint64_t id)
3435 char snapname[MAXNAMELEN];
3438 (void) snprintf(snapname, MAXNAMELEN, "%s@%llu", osname,
3441 error = dsl_destroy_snapshot(snapname, B_FALSE);
3442 if (error != 0 && error != ENOENT)
3443 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
3449 ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
3459 zdtmp = umem_alloc(sizeof (ztest_ds_t), UMEM_NOFAIL);
3460 name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3462 (void) rw_rdlock(&ztest_name_lock);
3464 (void) snprintf(name, MAXNAMELEN, "%s/temp_%llu",
3465 ztest_opts.zo_pool, (u_longlong_t)id);
3468 * If this dataset exists from a previous run, process its replay log
3469 * half of the time. If we don't replay it, then dsl_destroy_head()
3470 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
3472 if (ztest_random(2) == 0 &&
3473 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) {
3474 ztest_zd_init(zdtmp, NULL, os);
3475 zil_replay(os, zdtmp, ztest_replay_vector);
3476 ztest_zd_fini(zdtmp);
3477 dmu_objset_disown(os, FTAG);
3481 * There may be an old instance of the dataset we're about to
3482 * create lying around from a previous run. If so, destroy it
3483 * and all of its snapshots.
3485 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
3486 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
3489 * Verify that the destroyed dataset is no longer in the namespace.
3491 VERIFY3U(ENOENT, ==, dmu_objset_own(name, DMU_OST_OTHER, B_TRUE,
3495 * Verify that we can create a new dataset.
3497 error = ztest_dataset_create(name);
3499 if (error == ENOSPC) {
3500 ztest_record_enospc(FTAG);
3503 fatal(0, "dmu_objset_create(%s) = %d", name, error);
3506 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os));
3508 ztest_zd_init(zdtmp, NULL, os);
3511 * Open the intent log for it.
3513 zilog = zil_open(os, ztest_get_data);
3516 * Put some objects in there, do a little I/O to them,
3517 * and randomly take a couple of snapshots along the way.
3519 iters = ztest_random(5);
3520 for (i = 0; i < iters; i++) {
3521 ztest_dmu_object_alloc_free(zdtmp, id);
3522 if (ztest_random(iters) == 0)
3523 (void) ztest_snapshot_create(name, i);
3527 * Verify that we cannot create an existing dataset.
3529 VERIFY3U(EEXIST, ==,
3530 dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL));
3533 * Verify that we can hold an objset that is also owned.
3535 VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
3536 dmu_objset_rele(os2, FTAG);
3539 * Verify that we cannot own an objset that is already owned.
3542 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2));
3545 dmu_objset_disown(os, FTAG);
3546 ztest_zd_fini(zdtmp);
3548 (void) rw_unlock(&ztest_name_lock);
3550 umem_free(name, MAXNAMELEN);
3551 umem_free(zdtmp, sizeof (ztest_ds_t));
3555 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3558 ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
3560 (void) rw_rdlock(&ztest_name_lock);
3561 (void) ztest_snapshot_destroy(zd->zd_name, id);
3562 (void) ztest_snapshot_create(zd->zd_name, id);
3563 (void) rw_unlock(&ztest_name_lock);
3567 * Cleanup non-standard snapshots and clones.
3570 ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
3579 snap1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3580 clone1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3581 snap2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3582 clone2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3583 snap3name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3585 (void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu",
3586 osname, (u_longlong_t)id);
3587 (void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu",
3588 osname, (u_longlong_t)id);
3589 (void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu",
3590 clone1name, (u_longlong_t)id);
3591 (void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu",
3592 osname, (u_longlong_t)id);
3593 (void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu",
3594 clone1name, (u_longlong_t)id);
3596 error = dsl_destroy_head(clone2name);
3597 if (error && error != ENOENT)
3598 fatal(0, "dsl_destroy_head(%s) = %d", clone2name, error);
3599 error = dsl_destroy_snapshot(snap3name, B_FALSE);
3600 if (error && error != ENOENT)
3601 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap3name, error);
3602 error = dsl_destroy_snapshot(snap2name, B_FALSE);
3603 if (error && error != ENOENT)
3604 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap2name, error);
3605 error = dsl_destroy_head(clone1name);
3606 if (error && error != ENOENT)
3607 fatal(0, "dsl_destroy_head(%s) = %d", clone1name, error);
3608 error = dsl_destroy_snapshot(snap1name, B_FALSE);
3609 if (error && error != ENOENT)
3610 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap1name, error);
3612 umem_free(snap1name, MAXNAMELEN);
3613 umem_free(clone1name, MAXNAMELEN);
3614 umem_free(snap2name, MAXNAMELEN);
3615 umem_free(clone2name, MAXNAMELEN);
3616 umem_free(snap3name, MAXNAMELEN);
3620 * Verify dsl_dataset_promote handles EBUSY
3623 ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
3631 char *osname = zd->zd_name;
3634 snap1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3635 clone1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3636 snap2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3637 clone2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3638 snap3name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3640 (void) rw_rdlock(&ztest_name_lock);
3642 ztest_dsl_dataset_cleanup(osname, id);
3644 (void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu",
3645 osname, (u_longlong_t)id);
3646 (void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu",
3647 osname, (u_longlong_t)id);
3648 (void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu",
3649 clone1name, (u_longlong_t)id);
3650 (void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu",
3651 osname, (u_longlong_t)id);
3652 (void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu",
3653 clone1name, (u_longlong_t)id);
3655 error = dmu_objset_snapshot_one(osname, strchr(snap1name, '@') + 1);
3656 if (error && error != EEXIST) {
3657 if (error == ENOSPC) {
3658 ztest_record_enospc(FTAG);
3661 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
3664 error = dmu_objset_clone(clone1name, snap1name);
3666 if (error == ENOSPC) {
3667 ztest_record_enospc(FTAG);
3670 fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
3673 error = dmu_objset_snapshot_one(clone1name, strchr(snap2name, '@') + 1);
3674 if (error && error != EEXIST) {
3675 if (error == ENOSPC) {
3676 ztest_record_enospc(FTAG);
3679 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
3682 error = dmu_objset_snapshot_one(clone1name, strchr(snap3name, '@') + 1);
3683 if (error && error != EEXIST) {
3684 if (error == ENOSPC) {
3685 ztest_record_enospc(FTAG);
3688 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
3691 error = dmu_objset_clone(clone2name, snap3name);
3693 if (error == ENOSPC) {
3694 ztest_record_enospc(FTAG);
3697 fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
3700 error = dmu_objset_own(snap2name, DMU_OST_ANY, B_TRUE, FTAG, &os);
3702 fatal(0, "dmu_objset_own(%s) = %d", snap2name, error);
3703 error = dsl_dataset_promote(clone2name, NULL);
3704 if (error == ENOSPC) {
3705 dmu_objset_disown(os, FTAG);
3706 ztest_record_enospc(FTAG);
3710 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
3712 dmu_objset_disown(os, FTAG);
3715 ztest_dsl_dataset_cleanup(osname, id);
3717 (void) rw_unlock(&ztest_name_lock);
3719 umem_free(snap1name, MAXNAMELEN);
3720 umem_free(clone1name, MAXNAMELEN);
3721 umem_free(snap2name, MAXNAMELEN);
3722 umem_free(clone2name, MAXNAMELEN);
3723 umem_free(snap3name, MAXNAMELEN);
3726 #undef OD_ARRAY_SIZE
3727 #define OD_ARRAY_SIZE 4
3730 * Verify that dmu_object_{alloc,free} work as expected.
3733 ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
3740 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
3741 od = umem_alloc(size, UMEM_NOFAIL);
3742 batchsize = OD_ARRAY_SIZE;
3744 for (b = 0; b < batchsize; b++)
3745 ztest_od_init(od + b, id, FTAG, b, DMU_OT_UINT64_OTHER, 0, 0);
3748 * Destroy the previous batch of objects, create a new batch,
3749 * and do some I/O on the new objects.
3751 if (ztest_object_init(zd, od, size, B_TRUE) != 0)
3754 while (ztest_random(4 * batchsize) != 0)
3755 ztest_io(zd, od[ztest_random(batchsize)].od_object,
3756 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3758 umem_free(od, size);
3761 #undef OD_ARRAY_SIZE
3762 #define OD_ARRAY_SIZE 2
3765 * Verify that dmu_{read,write} work as expected.
3768 ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
3773 objset_t *os = zd->zd_os;
3774 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
3775 od = umem_alloc(size, UMEM_NOFAIL);
3777 int i, freeit, error;
3779 bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
3780 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3781 uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
3782 uint64_t regions = 997;
3783 uint64_t stride = 123456789ULL;
3784 uint64_t width = 40;
3785 int free_percent = 5;
3788 * This test uses two objects, packobj and bigobj, that are always
3789 * updated together (i.e. in the same tx) so that their contents are
3790 * in sync and can be compared. Their contents relate to each other
3791 * in a simple way: packobj is a dense array of 'bufwad' structures,
3792 * while bigobj is a sparse array of the same bufwads. Specifically,
3793 * for any index n, there are three bufwads that should be identical:
3795 * packobj, at offset n * sizeof (bufwad_t)
3796 * bigobj, at the head of the nth chunk
3797 * bigobj, at the tail of the nth chunk
3799 * The chunk size is arbitrary. It doesn't have to be a power of two,
3800 * and it doesn't have any relation to the object blocksize.
3801 * The only requirement is that it can hold at least two bufwads.
3803 * Normally, we write the bufwad to each of these locations.
3804 * However, free_percent of the time we instead write zeroes to
3805 * packobj and perform a dmu_free_range() on bigobj. By comparing
3806 * bigobj to packobj, we can verify that the DMU is correctly
3807 * tracking which parts of an object are allocated and free,
3808 * and that the contents of the allocated blocks are correct.
3812 * Read the directory info. If it's the first time, set things up.
3814 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, chunksize);
3815 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
3817 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
3818 umem_free(od, size);
3822 bigobj = od[0].od_object;
3823 packobj = od[1].od_object;
3824 chunksize = od[0].od_gen;
3825 ASSERT(chunksize == od[1].od_gen);
3828 * Prefetch a random chunk of the big object.
3829 * Our aim here is to get some async reads in flight
3830 * for blocks that we may free below; the DMU should
3831 * handle this race correctly.
3833 n = ztest_random(regions) * stride + ztest_random(width);
3834 s = 1 + ztest_random(2 * width - 1);
3835 dmu_prefetch(os, bigobj, 0, n * chunksize, s * chunksize,
3836 ZIO_PRIORITY_SYNC_READ);
3839 * Pick a random index and compute the offsets into packobj and bigobj.
3841 n = ztest_random(regions) * stride + ztest_random(width);
3842 s = 1 + ztest_random(width - 1);
3844 packoff = n * sizeof (bufwad_t);
3845 packsize = s * sizeof (bufwad_t);
3847 bigoff = n * chunksize;
3848 bigsize = s * chunksize;
3850 packbuf = umem_alloc(packsize, UMEM_NOFAIL);
3851 bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
3854 * free_percent of the time, free a range of bigobj rather than
3857 freeit = (ztest_random(100) < free_percent);
3860 * Read the current contents of our objects.
3862 error = dmu_read(os, packobj, packoff, packsize, packbuf,
3865 error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
3870 * Get a tx for the mods to both packobj and bigobj.
3872 tx = dmu_tx_create(os);
3874 dmu_tx_hold_write(tx, packobj, packoff, packsize);
3877 dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
3879 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
3881 /* This accounts for setting the checksum/compression. */
3882 dmu_tx_hold_bonus(tx, bigobj);
3884 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3886 umem_free(packbuf, packsize);
3887 umem_free(bigbuf, bigsize);
3888 umem_free(od, size);
3892 enum zio_checksum cksum;
3894 cksum = (enum zio_checksum)
3895 ztest_random_dsl_prop(ZFS_PROP_CHECKSUM);
3896 } while (cksum >= ZIO_CHECKSUM_LEGACY_FUNCTIONS);
3897 dmu_object_set_checksum(os, bigobj, cksum, tx);
3899 enum zio_compress comp;
3901 comp = (enum zio_compress)
3902 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION);
3903 } while (comp >= ZIO_COMPRESS_LEGACY_FUNCTIONS);
3904 dmu_object_set_compress(os, bigobj, comp, tx);
3907 * For each index from n to n + s, verify that the existing bufwad
3908 * in packobj matches the bufwads at the head and tail of the
3909 * corresponding chunk in bigobj. Then update all three bufwads
3910 * with the new values we want to write out.
3912 for (i = 0; i < s; i++) {
3914 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3916 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
3918 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
3920 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3921 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3923 if (pack->bw_txg > txg)
3924 fatal(0, "future leak: got %llx, open txg is %llx",
3927 if (pack->bw_data != 0 && pack->bw_index != n + i)
3928 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3929 pack->bw_index, n, i);
3931 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3932 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
3934 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
3935 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
3938 bzero(pack, sizeof (bufwad_t));
3940 pack->bw_index = n + i;
3942 pack->bw_data = 1 + ztest_random(-2ULL);
3949 * We've verified all the old bufwads, and made new ones.
3950 * Now write them out.
3952 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
3955 if (ztest_opts.zo_verbose >= 7) {
3956 (void) printf("freeing offset %llx size %llx"
3958 (u_longlong_t)bigoff,
3959 (u_longlong_t)bigsize,
3962 VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
3964 if (ztest_opts.zo_verbose >= 7) {
3965 (void) printf("writing offset %llx size %llx"
3967 (u_longlong_t)bigoff,
3968 (u_longlong_t)bigsize,
3971 dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
3977 * Sanity check the stuff we just wrote.
3980 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
3981 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
3983 VERIFY(0 == dmu_read(os, packobj, packoff,
3984 packsize, packcheck, DMU_READ_PREFETCH));
3985 VERIFY(0 == dmu_read(os, bigobj, bigoff,
3986 bigsize, bigcheck, DMU_READ_PREFETCH));
3988 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
3989 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
3991 umem_free(packcheck, packsize);
3992 umem_free(bigcheck, bigsize);
3995 umem_free(packbuf, packsize);
3996 umem_free(bigbuf, bigsize);
3997 umem_free(od, size);
4001 compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
4002 uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
4010 * For each index from n to n + s, verify that the existing bufwad
4011 * in packobj matches the bufwads at the head and tail of the
4012 * corresponding chunk in bigobj. Then update all three bufwads
4013 * with the new values we want to write out.
4015 for (i = 0; i < s; i++) {
4017 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
4019 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
4021 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
4023 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
4024 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
4026 if (pack->bw_txg > txg)
4027 fatal(0, "future leak: got %llx, open txg is %llx",
4030 if (pack->bw_data != 0 && pack->bw_index != n + i)
4031 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4032 pack->bw_index, n, i);
4034 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
4035 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
4037 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
4038 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
4040 pack->bw_index = n + i;
4042 pack->bw_data = 1 + ztest_random(-2ULL);
4049 #undef OD_ARRAY_SIZE
4050 #define OD_ARRAY_SIZE 2
4053 ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
4055 objset_t *os = zd->zd_os;
4062 bufwad_t *packbuf, *bigbuf;
4063 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
4064 uint64_t blocksize = ztest_random_blocksize();
4065 uint64_t chunksize = blocksize;
4066 uint64_t regions = 997;
4067 uint64_t stride = 123456789ULL;
4069 dmu_buf_t *bonus_db;
4070 arc_buf_t **bigbuf_arcbufs;
4071 dmu_object_info_t doi;
4073 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
4074 od = umem_alloc(size, UMEM_NOFAIL);
4077 * This test uses two objects, packobj and bigobj, that are always
4078 * updated together (i.e. in the same tx) so that their contents are
4079 * in sync and can be compared. Their contents relate to each other
4080 * in a simple way: packobj is a dense array of 'bufwad' structures,
4081 * while bigobj is a sparse array of the same bufwads. Specifically,
4082 * for any index n, there are three bufwads that should be identical:
4084 * packobj, at offset n * sizeof (bufwad_t)
4085 * bigobj, at the head of the nth chunk
4086 * bigobj, at the tail of the nth chunk
4088 * The chunk size is set equal to bigobj block size so that
4089 * dmu_assign_arcbuf() can be tested for object updates.
4093 * Read the directory info. If it's the first time, set things up.
4095 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
4096 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
4099 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
4100 umem_free(od, size);
4104 bigobj = od[0].od_object;
4105 packobj = od[1].od_object;
4106 blocksize = od[0].od_blocksize;
4107 chunksize = blocksize;
4108 ASSERT(chunksize == od[1].od_gen);
4110 VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
4111 VERIFY(ISP2(doi.doi_data_block_size));
4112 VERIFY(chunksize == doi.doi_data_block_size);
4113 VERIFY(chunksize >= 2 * sizeof (bufwad_t));
4116 * Pick a random index and compute the offsets into packobj and bigobj.
4118 n = ztest_random(regions) * stride + ztest_random(width);
4119 s = 1 + ztest_random(width - 1);
4121 packoff = n * sizeof (bufwad_t);
4122 packsize = s * sizeof (bufwad_t);
4124 bigoff = n * chunksize;
4125 bigsize = s * chunksize;
4127 packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
4128 bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
4130 VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
4132 bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
4135 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
4136 * Iteration 1 test zcopy to already referenced dbufs.
4137 * Iteration 2 test zcopy to dirty dbuf in the same txg.
4138 * Iteration 3 test zcopy to dbuf dirty in previous txg.
4139 * Iteration 4 test zcopy when dbuf is no longer dirty.
4140 * Iteration 5 test zcopy when it can't be done.
4141 * Iteration 6 one more zcopy write.
4143 for (i = 0; i < 7; i++) {
4148 * In iteration 5 (i == 5) use arcbufs
4149 * that don't match bigobj blksz to test
4150 * dmu_assign_arcbuf() when it can't directly
4151 * assign an arcbuf to a dbuf.
4153 for (j = 0; j < s; j++) {
4154 if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
4156 dmu_request_arcbuf(bonus_db, chunksize);
4158 bigbuf_arcbufs[2 * j] =
4159 dmu_request_arcbuf(bonus_db, chunksize / 2);
4160 bigbuf_arcbufs[2 * j + 1] =
4161 dmu_request_arcbuf(bonus_db, chunksize / 2);
4166 * Get a tx for the mods to both packobj and bigobj.
4168 tx = dmu_tx_create(os);
4170 dmu_tx_hold_write(tx, packobj, packoff, packsize);
4171 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
4173 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4175 umem_free(packbuf, packsize);
4176 umem_free(bigbuf, bigsize);
4177 for (j = 0; j < s; j++) {
4179 chunksize < (SPA_MINBLOCKSIZE * 2)) {
4180 dmu_return_arcbuf(bigbuf_arcbufs[j]);
4183 bigbuf_arcbufs[2 * j]);
4185 bigbuf_arcbufs[2 * j + 1]);
4188 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4189 umem_free(od, size);
4190 dmu_buf_rele(bonus_db, FTAG);
4195 * 50% of the time don't read objects in the 1st iteration to
4196 * test dmu_assign_arcbuf() for the case when there're no
4197 * existing dbufs for the specified offsets.
4199 if (i != 0 || ztest_random(2) != 0) {
4200 error = dmu_read(os, packobj, packoff,
4201 packsize, packbuf, DMU_READ_PREFETCH);
4203 error = dmu_read(os, bigobj, bigoff, bigsize,
4204 bigbuf, DMU_READ_PREFETCH);
4207 compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
4211 * We've verified all the old bufwads, and made new ones.
4212 * Now write them out.
4214 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
4215 if (ztest_opts.zo_verbose >= 7) {
4216 (void) printf("writing offset %llx size %llx"
4218 (u_longlong_t)bigoff,
4219 (u_longlong_t)bigsize,
4222 for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
4224 if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
4225 bcopy((caddr_t)bigbuf + (off - bigoff),
4226 bigbuf_arcbufs[j]->b_data, chunksize);
4228 bcopy((caddr_t)bigbuf + (off - bigoff),
4229 bigbuf_arcbufs[2 * j]->b_data,
4231 bcopy((caddr_t)bigbuf + (off - bigoff) +
4233 bigbuf_arcbufs[2 * j + 1]->b_data,
4238 VERIFY(dmu_buf_hold(os, bigobj, off,
4239 FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0);
4241 if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
4242 dmu_assign_arcbuf(bonus_db, off,
4243 bigbuf_arcbufs[j], tx);
4245 dmu_assign_arcbuf(bonus_db, off,
4246 bigbuf_arcbufs[2 * j], tx);
4247 dmu_assign_arcbuf(bonus_db,
4248 off + chunksize / 2,
4249 bigbuf_arcbufs[2 * j + 1], tx);
4252 dmu_buf_rele(dbt, FTAG);
4258 * Sanity check the stuff we just wrote.
4261 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
4262 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
4264 VERIFY(0 == dmu_read(os, packobj, packoff,
4265 packsize, packcheck, DMU_READ_PREFETCH));
4266 VERIFY(0 == dmu_read(os, bigobj, bigoff,
4267 bigsize, bigcheck, DMU_READ_PREFETCH));
4269 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
4270 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
4272 umem_free(packcheck, packsize);
4273 umem_free(bigcheck, bigsize);
4276 txg_wait_open(dmu_objset_pool(os), 0);
4277 } else if (i == 3) {
4278 txg_wait_synced(dmu_objset_pool(os), 0);
4282 dmu_buf_rele(bonus_db, FTAG);
4283 umem_free(packbuf, packsize);
4284 umem_free(bigbuf, bigsize);
4285 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4286 umem_free(od, size);
4291 ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
4295 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4296 uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
4297 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4300 * Have multiple threads write to large offsets in an object
4301 * to verify that parallel writes to an object -- even to the
4302 * same blocks within the object -- doesn't cause any trouble.
4304 ztest_od_init(od, ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
4306 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0)
4309 while (ztest_random(10) != 0)
4310 ztest_io(zd, od->od_object, offset);
4312 umem_free(od, sizeof (ztest_od_t));
4316 ztest_write_free(ztest_ds_t *zd, ztest_od_t *od, dmu_object_info_t *doi)
4318 objset_t *os = zd->zd_os;
4319 uint64_t object = od->od_object;
4320 uint64_t size = 1ULL << 40;
4321 uint64_t blocksize = doi->doi_data_block_size;
4322 uint64_t mblocksize = doi->doi_metadata_block_size;
4323 uint64_t l1_range = (mblocksize / sizeof (blkptr_t)) *
4325 void *data = umem_alloc(blocksize, UMEM_NOFAIL);
4327 (void) memset(data, 'a' + (object + l1_range) % 5, blocksize);
4329 while (ztest_random(10)) {
4330 int i, nranges = 0, minoffset = 0, maxoffset = 0;
4332 struct ztest_range {
4337 * Touch a few L1 ranges
4339 bzero((void *)range, sizeof (struct ztest_range)*10);
4340 nranges = ztest_random(10);
4344 for (i = 0; i < nranges; i++) {
4345 range[i].offset = ztest_random(size/l1_range)*l1_range;
4346 range[i].size = ztest_random(10)*l1_range;
4349 (void) rw_rdlock(&zd->zd_zilog_lock);
4350 for (i = 0; i < nranges; i++) {
4351 if ((ztest_write(zd, object, range[i].offset, blocksize,
4353 (ztest_write(zd, object, range[i].offset +
4354 range[i].size - blocksize, blocksize, data))) {
4355 fatal(0, "dmu_free_long_range() failed\n");
4358 (void) rw_unlock(&zd->zd_zilog_lock);
4361 * Free all ranges in a variable number of steps
4363 for (i = 0; i < nranges; i ++) {
4364 minoffset = MIN(minoffset, range[i].offset);
4365 maxoffset = MAX(maxoffset, range[i].offset +
4369 ztest_object_lock(zd, object, RL_READER);
4370 zrl = ztest_range_lock(zd, object, minoffset,
4371 maxoffset-minoffset, RL_WRITER);
4373 switch (ztest_random(4)) {
4375 /* Free each range separately */
4376 for (i = 0; i < nranges; i++) {
4377 if ((dmu_free_long_range(os, object,
4378 range[i].offset, range[i].size))) {
4379 fatal(0, "dmu_free_long_range() "
4385 /* Free two ranges at a time */
4386 for (i = 0; i < ((nranges % 2) ? nranges - 1 : nranges);
4389 MIN(range[i].offset, range[i+1].offset);
4391 MAX(range[i].offset + range[i].size,
4392 range[i+1].offset + range[i+1].size);
4393 if ((dmu_free_long_range(os, object, start,
4395 fatal(0, "dmu_free_long_range() "
4399 /* Free the last range for odd nranges */
4400 if ((nranges % 2) &&
4401 (dmu_free_long_range(os, object,
4402 range[nranges-1].offset,
4403 range[nranges-1].size))) {
4404 fatal(0, "dmu_free_long_range() failed\n");
4410 * Merge the ranges in two super-ranges and
4413 uint64_t start = 0, end = 0;
4414 int inranges = ztest_random(nranges);
4416 for (i = 0; i < inranges; i++) {
4417 start = MIN(start, range[i].offset);
4418 end = MAX(end, range[i].offset + range[i].size);
4420 if ((inranges != 0) &&
4421 (dmu_free_long_range(os, object, start,
4423 fatal(0, "dmu_free_long_range() failed\n");
4426 for (i = inranges; i < nranges; i++) {
4427 start = MIN(start, range[i].offset);
4428 end = MAX(end, range[i].offset + range[i].size);
4430 if ((inranges != nranges) &&
4431 (dmu_free_long_range(os, object, start,
4433 fatal(0, "dmu_free_long_range() failed\n");
4439 /* Merge in one range and free in one step */
4440 uint64_t start = minoffset, end = maxoffset;
4442 if ((dmu_free_long_range(os, object, start,
4444 fatal(0, "dmu_free_long_range() failed\n");
4450 /* Free the whole logical range of the object */
4451 if ((dmu_free_long_range(os, object, 0, size))) {
4452 fatal(0, "dmu_free_long_range() failed\n");
4457 ztest_range_unlock(zd, zrl);
4458 ztest_object_unlock(zd, object);
4461 umem_free(data, blocksize);
4465 * Test punching holes in an object to verify consistency of
4466 * the dmu structures for various corner cases
4468 * Force reallocation of dnode between iterations
4471 ztest_dmu_free_long_range(ztest_ds_t *zd, uint64_t id)
4473 ztest_od_t *od = NULL;
4474 uint64_t blocksize = ztest_random_blocksize();
4475 dmu_object_info_t doi = {0};
4477 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4479 ztest_od_init(od, ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER,
4481 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0)
4484 VERIFY3U(0, ==, dmu_object_info(zd->zd_os, od->od_object, &doi));
4485 ztest_write_free(zd, od, &doi);
4488 umem_free(od, sizeof (ztest_od_t));
4492 ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
4495 uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
4496 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4497 uint64_t count = ztest_random(20) + 1;
4498 uint64_t blocksize = ztest_random_blocksize();
4501 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4503 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
4505 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4506 !ztest_random(2)) != 0) {
4507 umem_free(od, sizeof (ztest_od_t));
4511 if (ztest_truncate(zd, od->od_object, offset, count * blocksize) != 0) {
4512 umem_free(od, sizeof (ztest_od_t));
4516 ztest_prealloc(zd, od->od_object, offset, count * blocksize);
4518 data = umem_zalloc(blocksize, UMEM_NOFAIL);
4520 while (ztest_random(count) != 0) {
4521 uint64_t randoff = offset + (ztest_random(count) * blocksize);
4522 if (ztest_write(zd, od->od_object, randoff, blocksize,
4525 while (ztest_random(4) != 0)
4526 ztest_io(zd, od->od_object, randoff);
4529 umem_free(data, blocksize);
4530 umem_free(od, sizeof (ztest_od_t));
4534 * Verify that zap_{create,destroy,add,remove,update} work as expected.
4536 #define ZTEST_ZAP_MIN_INTS 1
4537 #define ZTEST_ZAP_MAX_INTS 4
4538 #define ZTEST_ZAP_MAX_PROPS 1000
4541 ztest_zap(ztest_ds_t *zd, uint64_t id)
4543 objset_t *os = zd->zd_os;
4546 uint64_t txg, last_txg;
4547 uint64_t value[ZTEST_ZAP_MAX_INTS];
4548 uint64_t zl_ints, zl_intsize, prop;
4551 char propname[100], txgname[100];
4553 char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
4555 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4556 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
4558 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4559 !ztest_random(2)) != 0)
4562 object = od->od_object;
4565 * Generate a known hash collision, and verify that
4566 * we can lookup and remove both entries.
4568 tx = dmu_tx_create(os);
4569 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4570 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4573 for (i = 0; i < 2; i++) {
4575 VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
4578 for (i = 0; i < 2; i++) {
4579 VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
4580 sizeof (uint64_t), 1, &value[i], tx));
4582 zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
4583 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4584 ASSERT3U(zl_ints, ==, 1);
4586 for (i = 0; i < 2; i++) {
4587 VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
4592 * Generate a buch of random entries.
4594 ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
4596 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4597 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4598 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4599 bzero(value, sizeof (value));
4603 * If these zap entries already exist, validate their contents.
4605 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4607 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4608 ASSERT3U(zl_ints, ==, 1);
4610 VERIFY(zap_lookup(os, object, txgname, zl_intsize,
4611 zl_ints, &last_txg) == 0);
4613 VERIFY(zap_length(os, object, propname, &zl_intsize,
4616 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4617 ASSERT3U(zl_ints, ==, ints);
4619 VERIFY(zap_lookup(os, object, propname, zl_intsize,
4620 zl_ints, value) == 0);
4622 for (i = 0; i < ints; i++) {
4623 ASSERT3U(value[i], ==, last_txg + object + i);
4626 ASSERT3U(error, ==, ENOENT);
4630 * Atomically update two entries in our zap object.
4631 * The first is named txg_%llu, and contains the txg
4632 * in which the property was last updated. The second
4633 * is named prop_%llu, and the nth element of its value
4634 * should be txg + object + n.
4636 tx = dmu_tx_create(os);
4637 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4638 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4643 fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
4645 for (i = 0; i < ints; i++)
4646 value[i] = txg + object + i;
4648 VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
4650 VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
4656 * Remove a random pair of entries.
4658 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4659 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4660 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4662 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4664 if (error == ENOENT)
4669 tx = dmu_tx_create(os);
4670 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4671 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4674 VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
4675 VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
4678 umem_free(od, sizeof (ztest_od_t));
4682 * Testcase to test the upgrading of a microzap to fatzap.
4685 ztest_fzap(ztest_ds_t *zd, uint64_t id)
4687 objset_t *os = zd->zd_os;
4689 uint64_t object, txg;
4692 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4693 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
4695 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4696 !ztest_random(2)) != 0)
4698 object = od->od_object;
4701 * Add entries to this ZAP and make sure it spills over
4702 * and gets upgraded to a fatzap. Also, since we are adding
4703 * 2050 entries we should see ptrtbl growth and leaf-block split.
4705 for (i = 0; i < 2050; i++) {
4706 char name[MAXNAMELEN];
4711 (void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
4712 (u_longlong_t)id, (u_longlong_t)value);
4714 tx = dmu_tx_create(os);
4715 dmu_tx_hold_zap(tx, object, B_TRUE, name);
4716 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4719 error = zap_add(os, object, name, sizeof (uint64_t), 1,
4721 ASSERT(error == 0 || error == EEXIST);
4725 umem_free(od, sizeof (ztest_od_t));
4730 ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
4732 objset_t *os = zd->zd_os;
4734 uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
4736 int i, namelen, error;
4737 int micro = ztest_random(2);
4738 char name[20], string_value[20];
4741 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4742 ztest_od_init(od, ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER, 0, 0);
4744 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
4745 umem_free(od, sizeof (ztest_od_t));
4749 object = od->od_object;
4752 * Generate a random name of the form 'xxx.....' where each
4753 * x is a random printable character and the dots are dots.
4754 * There are 94 such characters, and the name length goes from
4755 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
4757 namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
4759 for (i = 0; i < 3; i++)
4760 name[i] = '!' + ztest_random('~' - '!' + 1);
4761 for (; i < namelen - 1; i++)
4765 if ((namelen & 1) || micro) {
4766 wsize = sizeof (txg);
4772 data = string_value;
4776 VERIFY0(zap_count(os, object, &count));
4777 ASSERT(count != -1ULL);
4780 * Select an operation: length, lookup, add, update, remove.
4782 i = ztest_random(5);
4785 tx = dmu_tx_create(os);
4786 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4787 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4790 bcopy(name, string_value, namelen);
4794 bzero(string_value, namelen);
4800 error = zap_length(os, object, name, &zl_wsize, &zl_wc);
4802 ASSERT3U(wsize, ==, zl_wsize);
4803 ASSERT3U(wc, ==, zl_wc);
4805 ASSERT3U(error, ==, ENOENT);
4810 error = zap_lookup(os, object, name, wsize, wc, data);
4812 if (data == string_value &&
4813 bcmp(name, data, namelen) != 0)
4814 fatal(0, "name '%s' != val '%s' len %d",
4815 name, data, namelen);
4817 ASSERT3U(error, ==, ENOENT);
4822 error = zap_add(os, object, name, wsize, wc, data, tx);
4823 ASSERT(error == 0 || error == EEXIST);
4827 VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
4831 error = zap_remove(os, object, name, tx);
4832 ASSERT(error == 0 || error == ENOENT);
4839 umem_free(od, sizeof (ztest_od_t));
4843 * Commit callback data.
4845 typedef struct ztest_cb_data {
4846 list_node_t zcd_node;
4848 int zcd_expected_err;
4849 boolean_t zcd_added;
4850 boolean_t zcd_called;
4854 /* This is the actual commit callback function */
4856 ztest_commit_callback(void *arg, int error)
4858 ztest_cb_data_t *data = arg;
4859 uint64_t synced_txg;
4861 VERIFY(data != NULL);
4862 VERIFY3S(data->zcd_expected_err, ==, error);
4863 VERIFY(!data->zcd_called);
4865 synced_txg = spa_last_synced_txg(data->zcd_spa);
4866 if (data->zcd_txg > synced_txg)
4867 fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
4868 ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
4871 data->zcd_called = B_TRUE;
4873 if (error == ECANCELED) {
4874 ASSERT0(data->zcd_txg);
4875 ASSERT(!data->zcd_added);
4878 * The private callback data should be destroyed here, but
4879 * since we are going to check the zcd_called field after
4880 * dmu_tx_abort(), we will destroy it there.
4885 ASSERT(data->zcd_added);
4886 ASSERT3U(data->zcd_txg, !=, 0);
4888 (void) mutex_enter(&zcl.zcl_callbacks_lock);
4890 /* See if this cb was called more quickly */
4891 if ((synced_txg - data->zcd_txg) < zc_min_txg_delay)
4892 zc_min_txg_delay = synced_txg - data->zcd_txg;
4894 /* Remove our callback from the list */
4895 list_remove(&zcl.zcl_callbacks, data);
4897 (void) mutex_exit(&zcl.zcl_callbacks_lock);
4899 umem_free(data, sizeof (ztest_cb_data_t));
4902 /* Allocate and initialize callback data structure */
4903 static ztest_cb_data_t *
4904 ztest_create_cb_data(objset_t *os, uint64_t txg)
4906 ztest_cb_data_t *cb_data;
4908 cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
4910 cb_data->zcd_txg = txg;
4911 cb_data->zcd_spa = dmu_objset_spa(os);
4912 list_link_init(&cb_data->zcd_node);
4918 * Commit callback test.
4921 ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
4923 objset_t *os = zd->zd_os;
4926 ztest_cb_data_t *cb_data[3], *tmp_cb;
4927 uint64_t old_txg, txg;
4930 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4931 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
4933 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
4934 umem_free(od, sizeof (ztest_od_t));
4938 tx = dmu_tx_create(os);
4940 cb_data[0] = ztest_create_cb_data(os, 0);
4941 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
4943 dmu_tx_hold_write(tx, od->od_object, 0, sizeof (uint64_t));
4945 /* Every once in a while, abort the transaction on purpose */
4946 if (ztest_random(100) == 0)
4950 error = dmu_tx_assign(tx, TXG_NOWAIT);
4952 txg = error ? 0 : dmu_tx_get_txg(tx);
4954 cb_data[0]->zcd_txg = txg;
4955 cb_data[1] = ztest_create_cb_data(os, txg);
4956 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
4960 * It's not a strict requirement to call the registered
4961 * callbacks from inside dmu_tx_abort(), but that's what
4962 * it's supposed to happen in the current implementation
4963 * so we will check for that.
4965 for (i = 0; i < 2; i++) {
4966 cb_data[i]->zcd_expected_err = ECANCELED;
4967 VERIFY(!cb_data[i]->zcd_called);
4972 for (i = 0; i < 2; i++) {
4973 VERIFY(cb_data[i]->zcd_called);
4974 umem_free(cb_data[i], sizeof (ztest_cb_data_t));
4977 umem_free(od, sizeof (ztest_od_t));
4981 cb_data[2] = ztest_create_cb_data(os, txg);
4982 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
4985 * Read existing data to make sure there isn't a future leak.
4987 VERIFY(0 == dmu_read(os, od->od_object, 0, sizeof (uint64_t),
4988 &old_txg, DMU_READ_PREFETCH));
4991 fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
4994 dmu_write(os, od->od_object, 0, sizeof (uint64_t), &txg, tx);
4996 (void) mutex_enter(&zcl.zcl_callbacks_lock);
4999 * Since commit callbacks don't have any ordering requirement and since
5000 * it is theoretically possible for a commit callback to be called
5001 * after an arbitrary amount of time has elapsed since its txg has been
5002 * synced, it is difficult to reliably determine whether a commit
5003 * callback hasn't been called due to high load or due to a flawed
5006 * In practice, we will assume that if after a certain number of txgs a
5007 * commit callback hasn't been called, then most likely there's an
5008 * implementation bug..
5010 tmp_cb = list_head(&zcl.zcl_callbacks);
5011 if (tmp_cb != NULL &&
5012 tmp_cb->zcd_txg + ZTEST_COMMIT_CB_THRESH < txg) {
5013 fatal(0, "Commit callback threshold exceeded, oldest txg: %"
5014 PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
5018 * Let's find the place to insert our callbacks.
5020 * Even though the list is ordered by txg, it is possible for the
5021 * insertion point to not be the end because our txg may already be
5022 * quiescing at this point and other callbacks in the open txg
5023 * (from other objsets) may have sneaked in.
5025 tmp_cb = list_tail(&zcl.zcl_callbacks);
5026 while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
5027 tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
5029 /* Add the 3 callbacks to the list */
5030 for (i = 0; i < 3; i++) {
5032 list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
5034 list_insert_after(&zcl.zcl_callbacks, tmp_cb,
5037 cb_data[i]->zcd_added = B_TRUE;
5038 VERIFY(!cb_data[i]->zcd_called);
5040 tmp_cb = cb_data[i];
5045 (void) mutex_exit(&zcl.zcl_callbacks_lock);
5049 umem_free(od, sizeof (ztest_od_t));
5054 ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
5056 zfs_prop_t proplist[] = {
5058 ZFS_PROP_COMPRESSION,
5064 (void) rw_rdlock(&ztest_name_lock);
5066 for (p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
5067 (void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
5068 ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
5070 VERIFY0(ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_RECORDSIZE,
5071 ztest_random_blocksize(), (int)ztest_random(2)));
5073 (void) rw_unlock(&ztest_name_lock);
5078 ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
5080 nvlist_t *props = NULL;
5082 (void) rw_rdlock(&ztest_name_lock);
5084 (void) ztest_spa_prop_set_uint64(ZPOOL_PROP_DEDUPDITTO,
5085 ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
5087 VERIFY0(spa_prop_get(ztest_spa, &props));
5089 if (ztest_opts.zo_verbose >= 6)
5090 dump_nvlist(props, 4);
5094 (void) rw_unlock(&ztest_name_lock);
5098 user_release_one(const char *snapname, const char *holdname)
5100 nvlist_t *snaps, *holds;
5103 snaps = fnvlist_alloc();
5104 holds = fnvlist_alloc();
5105 fnvlist_add_boolean(holds, holdname);
5106 fnvlist_add_nvlist(snaps, snapname, holds);
5107 fnvlist_free(holds);
5108 error = dsl_dataset_user_release(snaps, NULL);
5109 fnvlist_free(snaps);
5114 * Test snapshot hold/release and deferred destroy.
5117 ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
5120 objset_t *os = zd->zd_os;
5124 char clonename[100];
5126 char osname[MAXNAMELEN];
5129 (void) rw_rdlock(&ztest_name_lock);
5131 dmu_objset_name(os, osname);
5133 (void) snprintf(snapname, sizeof (snapname), "sh1_%llu",
5135 (void) snprintf(fullname, sizeof (fullname), "%s@%s", osname, snapname);
5136 (void) snprintf(clonename, sizeof (clonename),
5137 "%s/ch1_%llu", osname, (u_longlong_t)id);
5138 (void) snprintf(tag, sizeof (tag), "tag_%llu", (u_longlong_t)id);
5141 * Clean up from any previous run.
5143 error = dsl_destroy_head(clonename);
5144 if (error != ENOENT)
5146 error = user_release_one(fullname, tag);
5147 if (error != ESRCH && error != ENOENT)
5149 error = dsl_destroy_snapshot(fullname, B_FALSE);
5150 if (error != ENOENT)
5154 * Create snapshot, clone it, mark snap for deferred destroy,
5155 * destroy clone, verify snap was also destroyed.
5157 error = dmu_objset_snapshot_one(osname, snapname);
5159 if (error == ENOSPC) {
5160 ztest_record_enospc("dmu_objset_snapshot");
5163 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
5166 error = dmu_objset_clone(clonename, fullname);
5168 if (error == ENOSPC) {
5169 ztest_record_enospc("dmu_objset_clone");
5172 fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
5175 error = dsl_destroy_snapshot(fullname, B_TRUE);
5177 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5181 error = dsl_destroy_head(clonename);
5183 fatal(0, "dsl_destroy_head(%s) = %d", clonename, error);
5185 error = dmu_objset_hold(fullname, FTAG, &origin);
5186 if (error != ENOENT)
5187 fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
5190 * Create snapshot, add temporary hold, verify that we can't
5191 * destroy a held snapshot, mark for deferred destroy,
5192 * release hold, verify snapshot was destroyed.
5194 error = dmu_objset_snapshot_one(osname, snapname);
5196 if (error == ENOSPC) {
5197 ztest_record_enospc("dmu_objset_snapshot");
5200 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
5203 holds = fnvlist_alloc();
5204 fnvlist_add_string(holds, fullname, tag);
5205 error = dsl_dataset_user_hold(holds, 0, NULL);
5206 fnvlist_free(holds);
5208 if (error == ENOSPC) {
5209 ztest_record_enospc("dsl_dataset_user_hold");
5212 fatal(0, "dsl_dataset_user_hold(%s, %s) = %u",
5213 fullname, tag, error);
5216 error = dsl_destroy_snapshot(fullname, B_FALSE);
5217 if (error != EBUSY) {
5218 fatal(0, "dsl_destroy_snapshot(%s, B_FALSE) = %d",
5222 error = dsl_destroy_snapshot(fullname, B_TRUE);
5224 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5228 error = user_release_one(fullname, tag);
5230 fatal(0, "user_release_one(%s, %s) = %d", fullname, tag, error);
5232 VERIFY3U(dmu_objset_hold(fullname, FTAG, &origin), ==, ENOENT);
5235 (void) rw_unlock(&ztest_name_lock);
5239 * Inject random faults into the on-disk data.
5243 ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
5245 ztest_shared_t *zs = ztest_shared;
5246 spa_t *spa = ztest_spa;
5250 uint64_t bad = 0x1990c0ffeedecadeull;
5255 int bshift = SPA_MAXBLOCKSHIFT + 2; /* don't scrog all labels */
5261 boolean_t islog = B_FALSE;
5263 path0 = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
5264 pathrand = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
5266 mutex_enter(&ztest_vdev_lock);
5267 maxfaults = MAXFAULTS();
5268 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
5269 mirror_save = zs->zs_mirrors;
5270 mutex_exit(&ztest_vdev_lock);
5272 ASSERT(leaves >= 1);
5275 * Grab the name lock as reader. There are some operations
5276 * which don't like to have their vdevs changed while
5277 * they are in progress (i.e. spa_change_guid). Those
5278 * operations will have grabbed the name lock as writer.
5280 (void) rw_rdlock(&ztest_name_lock);
5283 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
5285 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5287 if (ztest_random(2) == 0) {
5289 * Inject errors on a normal data device or slog device.
5291 top = ztest_random_vdev_top(spa, B_TRUE);
5292 leaf = ztest_random(leaves) + zs->zs_splits;
5295 * Generate paths to the first leaf in this top-level vdev,
5296 * and to the random leaf we selected. We'll induce transient
5297 * write failures and random online/offline activity on leaf 0,
5298 * and we'll write random garbage to the randomly chosen leaf.
5300 (void) snprintf(path0, MAXPATHLEN, ztest_dev_template,
5301 ztest_opts.zo_dir, ztest_opts.zo_pool,
5302 top * leaves + zs->zs_splits);
5303 (void) snprintf(pathrand, MAXPATHLEN, ztest_dev_template,
5304 ztest_opts.zo_dir, ztest_opts.zo_pool,
5305 top * leaves + leaf);
5307 vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
5308 if (vd0 != NULL && vd0->vdev_top->vdev_islog)
5312 * If the top-level vdev needs to be resilvered
5313 * then we only allow faults on the device that is
5316 if (vd0 != NULL && maxfaults != 1 &&
5317 (!vdev_resilver_needed(vd0->vdev_top, NULL, NULL) ||
5318 vd0->vdev_resilver_txg != 0)) {
5320 * Make vd0 explicitly claim to be unreadable,
5321 * or unwriteable, or reach behind its back
5322 * and close the underlying fd. We can do this if
5323 * maxfaults == 0 because we'll fail and reexecute,
5324 * and we can do it if maxfaults >= 2 because we'll
5325 * have enough redundancy. If maxfaults == 1, the
5326 * combination of this with injection of random data
5327 * corruption below exceeds the pool's fault tolerance.
5329 vdev_file_t *vf = vd0->vdev_tsd;
5331 if (vf != NULL && ztest_random(3) == 0) {
5332 (void) close(vf->vf_vnode->v_fd);
5333 vf->vf_vnode->v_fd = -1;
5334 } else if (ztest_random(2) == 0) {
5335 vd0->vdev_cant_read = B_TRUE;
5337 vd0->vdev_cant_write = B_TRUE;
5339 guid0 = vd0->vdev_guid;
5343 * Inject errors on an l2cache device.
5345 spa_aux_vdev_t *sav = &spa->spa_l2cache;
5347 if (sav->sav_count == 0) {
5348 spa_config_exit(spa, SCL_STATE, FTAG);
5349 (void) rw_unlock(&ztest_name_lock);
5352 vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
5353 guid0 = vd0->vdev_guid;
5354 (void) strcpy(path0, vd0->vdev_path);
5355 (void) strcpy(pathrand, vd0->vdev_path);
5359 maxfaults = INT_MAX; /* no limit on cache devices */
5362 spa_config_exit(spa, SCL_STATE, FTAG);
5363 (void) rw_unlock(&ztest_name_lock);
5366 * If we can tolerate two or more faults, or we're dealing
5367 * with a slog, randomly online/offline vd0.
5369 if ((maxfaults >= 2 || islog) && guid0 != 0) {
5370 if (ztest_random(10) < 6) {
5371 int flags = (ztest_random(2) == 0 ?
5372 ZFS_OFFLINE_TEMPORARY : 0);
5375 * We have to grab the zs_name_lock as writer to
5376 * prevent a race between offlining a slog and
5377 * destroying a dataset. Offlining the slog will
5378 * grab a reference on the dataset which may cause
5379 * dsl_destroy_head() to fail with EBUSY thus
5380 * leaving the dataset in an inconsistent state.
5383 (void) rw_wrlock(&ztest_name_lock);
5385 VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
5388 (void) rw_unlock(&ztest_name_lock);
5391 * Ideally we would like to be able to randomly
5392 * call vdev_[on|off]line without holding locks
5393 * to force unpredictable failures but the side
5394 * effects of vdev_[on|off]line prevent us from
5395 * doing so. We grab the ztest_vdev_lock here to
5396 * prevent a race between injection testing and
5399 mutex_enter(&ztest_vdev_lock);
5400 (void) vdev_online(spa, guid0, 0, NULL);
5401 mutex_exit(&ztest_vdev_lock);
5409 * We have at least single-fault tolerance, so inject data corruption.
5411 fd = open(pathrand, O_RDWR);
5413 if (fd == -1) /* we hit a gap in the device namespace */
5416 fsize = lseek(fd, 0, SEEK_END);
5418 while (--iters != 0) {
5420 * The offset must be chosen carefully to ensure that
5421 * we do not inject a given logical block with errors
5422 * on two different leaf devices, because ZFS can not
5423 * tolerate that (if maxfaults==1).
5425 * We divide each leaf into chunks of size
5426 * (# leaves * SPA_MAXBLOCKSIZE * 4). Within each chunk
5427 * there is a series of ranges to which we can inject errors.
5428 * Each range can accept errors on only a single leaf vdev.
5429 * The error injection ranges are separated by ranges
5430 * which we will not inject errors on any device (DMZs).
5431 * Each DMZ must be large enough such that a single block
5432 * can not straddle it, so that a single block can not be
5433 * a target in two different injection ranges (on different
5436 * For example, with 3 leaves, each chunk looks like:
5437 * 0 to 32M: injection range for leaf 0
5438 * 32M to 64M: DMZ - no injection allowed
5439 * 64M to 96M: injection range for leaf 1
5440 * 96M to 128M: DMZ - no injection allowed
5441 * 128M to 160M: injection range for leaf 2
5442 * 160M to 192M: DMZ - no injection allowed
5444 offset = ztest_random(fsize / (leaves << bshift)) *
5445 (leaves << bshift) + (leaf << bshift) +
5446 (ztest_random(1ULL << (bshift - 1)) & -8ULL);
5448 if (offset >= fsize)
5451 mutex_enter(&ztest_vdev_lock);
5452 if (mirror_save != zs->zs_mirrors) {
5453 mutex_exit(&ztest_vdev_lock);
5458 if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
5459 fatal(1, "can't inject bad word at 0x%llx in %s",
5462 mutex_exit(&ztest_vdev_lock);
5464 if (ztest_opts.zo_verbose >= 7)
5465 (void) printf("injected bad word into %s,"
5466 " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
5471 umem_free(path0, MAXPATHLEN);
5472 umem_free(pathrand, MAXPATHLEN);
5476 * Verify that DDT repair works as expected.
5479 ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
5481 ztest_shared_t *zs = ztest_shared;
5482 spa_t *spa = ztest_spa;
5483 objset_t *os = zd->zd_os;
5485 uint64_t object, blocksize, txg, pattern, psize;
5486 enum zio_checksum checksum = spa_dedup_checksum(spa);
5491 int copies = 2 * ZIO_DEDUPDITTO_MIN;
5494 blocksize = ztest_random_blocksize();
5495 blocksize = MIN(blocksize, 2048); /* because we write so many */
5497 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
5498 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
5500 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
5501 umem_free(od, sizeof (ztest_od_t));
5506 * Take the name lock as writer to prevent anyone else from changing
5507 * the pool and dataset properies we need to maintain during this test.
5509 (void) rw_wrlock(&ztest_name_lock);
5511 if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
5513 ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
5515 (void) rw_unlock(&ztest_name_lock);
5516 umem_free(od, sizeof (ztest_od_t));
5520 object = od[0].od_object;
5521 blocksize = od[0].od_blocksize;
5522 pattern = zs->zs_guid ^ dmu_objset_fsid_guid(os);
5524 ASSERT(object != 0);
5526 tx = dmu_tx_create(os);
5527 dmu_tx_hold_write(tx, object, 0, copies * blocksize);
5528 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
5530 (void) rw_unlock(&ztest_name_lock);
5531 umem_free(od, sizeof (ztest_od_t));
5536 * Write all the copies of our block.
5538 for (i = 0; i < copies; i++) {
5539 uint64_t offset = i * blocksize;
5540 int error = dmu_buf_hold(os, object, offset, FTAG, &db,
5541 DMU_READ_NO_PREFETCH);
5543 fatal(B_FALSE, "dmu_buf_hold(%p, %llu, %llu) = %u",
5544 os, (long long)object, (long long) offset, error);
5546 ASSERT(db->db_offset == offset);
5547 ASSERT(db->db_size == blocksize);
5548 ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
5549 ztest_pattern_match(db->db_data, db->db_size, 0ULL));
5550 dmu_buf_will_fill(db, tx);
5551 ztest_pattern_set(db->db_data, db->db_size, pattern);
5552 dmu_buf_rele(db, FTAG);
5556 txg_wait_synced(spa_get_dsl(spa), txg);
5559 * Find out what block we got.
5561 VERIFY0(dmu_buf_hold(os, object, 0, FTAG, &db,
5562 DMU_READ_NO_PREFETCH));
5563 blk = *((dmu_buf_impl_t *)db)->db_blkptr;
5564 dmu_buf_rele(db, FTAG);
5567 * Damage the block. Dedup-ditto will save us when we read it later.
5569 psize = BP_GET_PSIZE(&blk);
5570 buf = zio_buf_alloc(psize);
5571 ztest_pattern_set(buf, psize, ~pattern);
5573 (void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
5574 buf, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
5575 ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
5577 zio_buf_free(buf, psize);
5579 (void) rw_unlock(&ztest_name_lock);
5580 umem_free(od, sizeof (ztest_od_t));
5588 ztest_scrub(ztest_ds_t *zd, uint64_t id)
5590 spa_t *spa = ztest_spa;
5592 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5593 (void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
5594 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5598 * Change the guid for the pool.
5602 ztest_reguid(ztest_ds_t *zd, uint64_t id)
5604 spa_t *spa = ztest_spa;
5605 uint64_t orig, load;
5608 orig = spa_guid(spa);
5609 load = spa_load_guid(spa);
5611 (void) rw_wrlock(&ztest_name_lock);
5612 error = spa_change_guid(spa);
5613 (void) rw_unlock(&ztest_name_lock);
5618 if (ztest_opts.zo_verbose >= 4) {
5619 (void) printf("Changed guid old %llu -> %llu\n",
5620 (u_longlong_t)orig, (u_longlong_t)spa_guid(spa));
5623 VERIFY3U(orig, !=, spa_guid(spa));
5624 VERIFY3U(load, ==, spa_load_guid(spa));
5628 * Rename the pool to a different name and then rename it back.
5632 ztest_spa_rename(ztest_ds_t *zd, uint64_t id)
5634 char *oldname, *newname;
5637 (void) rw_wrlock(&ztest_name_lock);
5639 oldname = ztest_opts.zo_pool;
5640 newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
5641 (void) strcpy(newname, oldname);
5642 (void) strcat(newname, "_tmp");
5647 VERIFY3U(0, ==, spa_rename(oldname, newname));
5650 * Try to open it under the old name, which shouldn't exist
5652 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5655 * Open it under the new name and make sure it's still the same spa_t.
5657 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
5659 ASSERT(spa == ztest_spa);
5660 spa_close(spa, FTAG);
5663 * Rename it back to the original
5665 VERIFY3U(0, ==, spa_rename(newname, oldname));
5668 * Make sure it can still be opened
5670 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
5672 ASSERT(spa == ztest_spa);
5673 spa_close(spa, FTAG);
5675 umem_free(newname, strlen(newname) + 1);
5677 (void) rw_unlock(&ztest_name_lock);
5681 ztest_fletcher(ztest_ds_t *zd, uint64_t id)
5683 hrtime_t end = gethrtime() + NANOSEC;
5685 while (gethrtime() <= end) {
5686 int run_count = 100;
5692 zio_cksum_t zc_ref_byteswap;
5694 size = ztest_random_blocksize();
5695 buf = umem_alloc(size, UMEM_NOFAIL);
5697 for (i = 0, ptr = buf; i < size / sizeof (*ptr); i++, ptr++)
5698 *ptr = ztest_random(UINT_MAX);
5700 VERIFY0(fletcher_4_impl_set("scalar"));
5701 fletcher_4_native(buf, size, &zc_ref);
5702 fletcher_4_byteswap(buf, size, &zc_ref_byteswap);
5704 VERIFY0(fletcher_4_impl_set("cycle"));
5705 while (run_count-- > 0) {
5707 zio_cksum_t zc_byteswap;
5709 fletcher_4_byteswap(buf, size, &zc_byteswap);
5710 fletcher_4_native(buf, size, &zc);
5712 VERIFY0(bcmp(&zc, &zc_ref, sizeof (zc)));
5713 VERIFY0(bcmp(&zc_byteswap, &zc_ref_byteswap,
5714 sizeof (zc_byteswap)));
5717 umem_free(buf, size);
5722 ztest_check_path(char *path)
5725 /* return true on success */
5726 return (!stat(path, &s));
5730 ztest_get_zdb_bin(char *bin, int len)
5734 * Try to use ZDB_PATH and in-tree zdb path. If not successful, just
5735 * let popen to search through PATH.
5737 if ((zdb_path = getenv("ZDB_PATH"))) {
5738 strlcpy(bin, zdb_path, len); /* In env */
5739 if (!ztest_check_path(bin)) {
5740 ztest_dump_core = 0;
5741 fatal(1, "invalid ZDB_PATH '%s'", bin);
5746 VERIFY(realpath(getexecname(), bin) != NULL);
5747 if (strstr(bin, "/ztest/")) {
5748 strstr(bin, "/ztest/")[0] = '\0'; /* In-tree */
5749 strcat(bin, "/zdb/zdb");
5750 if (ztest_check_path(bin))
5757 * Verify pool integrity by running zdb.
5760 ztest_run_zdb(char *pool)
5766 const int len = MAXPATHLEN + MAXNAMELEN + 20;
5769 bin = umem_alloc(len, UMEM_NOFAIL);
5770 zdb = umem_alloc(len, UMEM_NOFAIL);
5771 zbuf = umem_alloc(1024, UMEM_NOFAIL);
5773 ztest_get_zdb_bin(bin, len);
5776 "%s -bcc%s%s -d -U %s %s",
5778 ztest_opts.zo_verbose >= 3 ? "s" : "",
5779 ztest_opts.zo_verbose >= 4 ? "v" : "",
5783 if (ztest_opts.zo_verbose >= 5)
5784 (void) printf("Executing %s\n", strstr(zdb, "zdb "));
5786 fp = popen(zdb, "r");
5788 while (fgets(zbuf, 1024, fp) != NULL)
5789 if (ztest_opts.zo_verbose >= 3)
5790 (void) printf("%s", zbuf);
5792 status = pclose(fp);
5797 ztest_dump_core = 0;
5798 if (WIFEXITED(status))
5799 fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
5801 fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
5803 umem_free(bin, len);
5804 umem_free(zdb, len);
5805 umem_free(zbuf, 1024);
5809 ztest_walk_pool_directory(char *header)
5813 if (ztest_opts.zo_verbose >= 6)
5814 (void) printf("%s\n", header);
5816 mutex_enter(&spa_namespace_lock);
5817 while ((spa = spa_next(spa)) != NULL)
5818 if (ztest_opts.zo_verbose >= 6)
5819 (void) printf("\t%s\n", spa_name(spa));
5820 mutex_exit(&spa_namespace_lock);
5824 ztest_spa_import_export(char *oldname, char *newname)
5826 nvlist_t *config, *newconfig;
5831 if (ztest_opts.zo_verbose >= 4) {
5832 (void) printf("import/export: old = %s, new = %s\n",
5837 * Clean up from previous runs.
5839 (void) spa_destroy(newname);
5842 * Get the pool's configuration and guid.
5844 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
5847 * Kick off a scrub to tickle scrub/export races.
5849 if (ztest_random(2) == 0)
5850 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5852 pool_guid = spa_guid(spa);
5853 spa_close(spa, FTAG);
5855 ztest_walk_pool_directory("pools before export");
5860 VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
5862 ztest_walk_pool_directory("pools after export");
5867 newconfig = spa_tryimport(config);
5868 ASSERT(newconfig != NULL);
5869 nvlist_free(newconfig);
5872 * Import it under the new name.
5874 error = spa_import(newname, config, NULL, 0);
5876 dump_nvlist(config, 0);
5877 fatal(B_FALSE, "couldn't import pool %s as %s: error %u",
5878 oldname, newname, error);
5881 ztest_walk_pool_directory("pools after import");
5884 * Try to import it again -- should fail with EEXIST.
5886 VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL, 0));
5889 * Try to import it under a different name -- should fail with EEXIST.
5891 VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL, 0));
5894 * Verify that the pool is no longer visible under the old name.
5896 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5899 * Verify that we can open and close the pool using the new name.
5901 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
5902 ASSERT(pool_guid == spa_guid(spa));
5903 spa_close(spa, FTAG);
5905 nvlist_free(config);
5909 ztest_resume(spa_t *spa)
5911 if (spa_suspended(spa) && ztest_opts.zo_verbose >= 6)
5912 (void) printf("resuming from suspended state\n");
5913 spa_vdev_state_enter(spa, SCL_NONE);
5914 vdev_clear(spa, NULL);
5915 (void) spa_vdev_state_exit(spa, NULL, 0);
5916 (void) zio_resume(spa);
5920 ztest_resume_thread(void *arg)
5924 while (!ztest_exiting) {
5925 if (spa_suspended(spa))
5927 (void) poll(NULL, 0, 100);
5939 ztest_deadman_alarm(int sig)
5941 fatal(0, "failed to complete within %d seconds of deadline", GRACE);
5946 ztest_execute(int test, ztest_info_t *zi, uint64_t id)
5948 ztest_ds_t *zd = &ztest_ds[id % ztest_opts.zo_datasets];
5949 ztest_shared_callstate_t *zc = ZTEST_GET_SHARED_CALLSTATE(test);
5950 hrtime_t functime = gethrtime();
5953 for (i = 0; i < zi->zi_iters; i++)
5954 zi->zi_func(zd, id);
5956 functime = gethrtime() - functime;
5958 atomic_add_64(&zc->zc_count, 1);
5959 atomic_add_64(&zc->zc_time, functime);
5961 if (ztest_opts.zo_verbose >= 4)
5962 (void) printf("%6.2f sec in %s\n",
5963 (double)functime / NANOSEC, zi->zi_funcname);
5967 ztest_thread(void *arg)
5970 uint64_t id = (uintptr_t)arg;
5971 ztest_shared_t *zs = ztest_shared;
5975 ztest_shared_callstate_t *zc;
5977 while ((now = gethrtime()) < zs->zs_thread_stop) {
5979 * See if it's time to force a crash.
5981 if (now > zs->zs_thread_kill)
5985 * If we're getting ENOSPC with some regularity, stop.
5987 if (zs->zs_enospc_count > 10)
5991 * Pick a random function to execute.
5993 rand = ztest_random(ZTEST_FUNCS);
5994 zi = &ztest_info[rand];
5995 zc = ZTEST_GET_SHARED_CALLSTATE(rand);
5996 call_next = zc->zc_next;
5998 if (now >= call_next &&
5999 atomic_cas_64(&zc->zc_next, call_next, call_next +
6000 ztest_random(2 * zi->zi_interval[0] + 1)) == call_next) {
6001 ztest_execute(rand, zi, id);
6011 ztest_dataset_name(char *dsname, char *pool, int d)
6013 (void) snprintf(dsname, MAXNAMELEN, "%s/ds_%d", pool, d);
6017 ztest_dataset_destroy(int d)
6019 char name[MAXNAMELEN];
6022 ztest_dataset_name(name, ztest_opts.zo_pool, d);
6024 if (ztest_opts.zo_verbose >= 3)
6025 (void) printf("Destroying %s to free up space\n", name);
6028 * Cleanup any non-standard clones and snapshots. In general,
6029 * ztest thread t operates on dataset (t % zopt_datasets),
6030 * so there may be more than one thing to clean up.
6032 for (t = d; t < ztest_opts.zo_threads;
6033 t += ztest_opts.zo_datasets)
6034 ztest_dsl_dataset_cleanup(name, t);
6036 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
6037 DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
6041 ztest_dataset_dirobj_verify(ztest_ds_t *zd)
6043 uint64_t usedobjs, dirobjs, scratch;
6046 * ZTEST_DIROBJ is the object directory for the entire dataset.
6047 * Therefore, the number of objects in use should equal the
6048 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
6049 * If not, we have an object leak.
6051 * Note that we can only check this in ztest_dataset_open(),
6052 * when the open-context and syncing-context values agree.
6053 * That's because zap_count() returns the open-context value,
6054 * while dmu_objset_space() returns the rootbp fill count.
6056 VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
6057 dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
6058 ASSERT3U(dirobjs + 1, ==, usedobjs);
6062 ztest_dataset_open(int d)
6064 ztest_ds_t *zd = &ztest_ds[d];
6065 uint64_t committed_seq = ZTEST_GET_SHARED_DS(d)->zd_seq;
6068 char name[MAXNAMELEN];
6071 ztest_dataset_name(name, ztest_opts.zo_pool, d);
6073 (void) rw_rdlock(&ztest_name_lock);
6075 error = ztest_dataset_create(name);
6076 if (error == ENOSPC) {
6077 (void) rw_unlock(&ztest_name_lock);
6078 ztest_record_enospc(FTAG);
6081 ASSERT(error == 0 || error == EEXIST);
6083 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, zd, &os));
6084 (void) rw_unlock(&ztest_name_lock);
6086 ztest_zd_init(zd, ZTEST_GET_SHARED_DS(d), os);
6088 zilog = zd->zd_zilog;
6090 if (zilog->zl_header->zh_claim_lr_seq != 0 &&
6091 zilog->zl_header->zh_claim_lr_seq < committed_seq)
6092 fatal(0, "missing log records: claimed %llu < committed %llu",
6093 zilog->zl_header->zh_claim_lr_seq, committed_seq);
6095 ztest_dataset_dirobj_verify(zd);
6097 zil_replay(os, zd, ztest_replay_vector);
6099 ztest_dataset_dirobj_verify(zd);
6101 if (ztest_opts.zo_verbose >= 6)
6102 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
6104 (u_longlong_t)zilog->zl_parse_blk_count,
6105 (u_longlong_t)zilog->zl_parse_lr_count,
6106 (u_longlong_t)zilog->zl_replaying_seq);
6108 zilog = zil_open(os, ztest_get_data);
6110 if (zilog->zl_replaying_seq != 0 &&
6111 zilog->zl_replaying_seq < committed_seq)
6112 fatal(0, "missing log records: replayed %llu < committed %llu",
6113 zilog->zl_replaying_seq, committed_seq);
6119 ztest_dataset_close(int d)
6121 ztest_ds_t *zd = &ztest_ds[d];
6123 zil_close(zd->zd_zilog);
6124 dmu_objset_disown(zd->zd_os, zd);
6130 * Kick off threads to run tests on all datasets in parallel.
6133 ztest_run(ztest_shared_t *zs)
6138 kthread_t *resume_thread;
6143 ztest_exiting = B_FALSE;
6146 * Initialize parent/child shared state.
6148 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
6149 VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0);
6151 zs->zs_thread_start = gethrtime();
6152 zs->zs_thread_stop =
6153 zs->zs_thread_start + ztest_opts.zo_passtime * NANOSEC;
6154 zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
6155 zs->zs_thread_kill = zs->zs_thread_stop;
6156 if (ztest_random(100) < ztest_opts.zo_killrate) {
6157 zs->zs_thread_kill -=
6158 ztest_random(ztest_opts.zo_passtime * NANOSEC);
6161 mutex_init(&zcl.zcl_callbacks_lock, NULL, MUTEX_DEFAULT, NULL);
6163 list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
6164 offsetof(ztest_cb_data_t, zcd_node));
6169 kernel_init(FREAD | FWRITE);
6170 VERIFY0(spa_open(ztest_opts.zo_pool, &spa, FTAG));
6171 spa->spa_debug = B_TRUE;
6172 metaslab_preload_limit = ztest_random(20) + 1;
6175 VERIFY0(dmu_objset_own(ztest_opts.zo_pool,
6176 DMU_OST_ANY, B_TRUE, FTAG, &os));
6177 zs->zs_guid = dmu_objset_fsid_guid(os);
6178 dmu_objset_disown(os, FTAG);
6180 spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
6183 * We don't expect the pool to suspend unless maxfaults == 0,
6184 * in which case ztest_fault_inject() temporarily takes away
6185 * the only valid replica.
6187 if (MAXFAULTS() == 0)
6188 spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
6190 spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
6193 * Create a thread to periodically resume suspended I/O.
6195 VERIFY3P((resume_thread = zk_thread_create(NULL, 0,
6196 (thread_func_t)ztest_resume_thread, spa, TS_RUN, NULL, 0, 0,
6197 PTHREAD_CREATE_JOINABLE)), !=, NULL);
6201 * Set a deadman alarm to abort() if we hang.
6203 signal(SIGALRM, ztest_deadman_alarm);
6204 alarm((zs->zs_thread_stop - zs->zs_thread_start) / NANOSEC + GRACE);
6208 * Verify that we can safely inquire about about any object,
6209 * whether it's allocated or not. To make it interesting,
6210 * we probe a 5-wide window around each power of two.
6211 * This hits all edge cases, including zero and the max.
6213 for (t = 0; t < 64; t++) {
6214 for (d = -5; d <= 5; d++) {
6215 error = dmu_object_info(spa->spa_meta_objset,
6216 (1ULL << t) + d, NULL);
6217 ASSERT(error == 0 || error == ENOENT ||
6223 * If we got any ENOSPC errors on the previous run, destroy something.
6225 if (zs->zs_enospc_count != 0) {
6226 int d = ztest_random(ztest_opts.zo_datasets);
6227 ztest_dataset_destroy(d);
6229 zs->zs_enospc_count = 0;
6231 tid = umem_zalloc(ztest_opts.zo_threads * sizeof (kt_did_t),
6234 if (ztest_opts.zo_verbose >= 4)
6235 (void) printf("starting main threads...\n");
6238 * Kick off all the tests that run in parallel.
6240 for (t = 0; t < ztest_opts.zo_threads; t++) {
6243 if (t < ztest_opts.zo_datasets &&
6244 ztest_dataset_open(t) != 0)
6247 VERIFY3P(thread = zk_thread_create(NULL, 0,
6248 (thread_func_t)ztest_thread,
6249 (void *)(uintptr_t)t, TS_RUN, NULL, 0, 0,
6250 PTHREAD_CREATE_JOINABLE), !=, NULL);
6251 tid[t] = thread->t_tid;
6255 * Wait for all of the tests to complete. We go in reverse order
6256 * so we don't close datasets while threads are still using them.
6258 for (t = ztest_opts.zo_threads - 1; t >= 0; t--) {
6259 thread_join(tid[t]);
6260 if (t < ztest_opts.zo_datasets)
6261 ztest_dataset_close(t);
6264 txg_wait_synced(spa_get_dsl(spa), 0);
6266 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
6267 zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
6269 umem_free(tid, ztest_opts.zo_threads * sizeof (kt_did_t));
6271 /* Kill the resume thread */
6272 ztest_exiting = B_TRUE;
6273 thread_join(resume_thread->t_tid);
6277 * Right before closing the pool, kick off a bunch of async I/O;
6278 * spa_close() should wait for it to complete.
6280 for (object = 1; object < 50; object++) {
6281 dmu_prefetch(spa->spa_meta_objset, object, 0, 0, 1ULL << 20,
6282 ZIO_PRIORITY_SYNC_READ);
6285 /* Verify that at least one commit cb was called in a timely fashion */
6286 if (zc_cb_counter >= ZTEST_COMMIT_CB_MIN_REG)
6287 VERIFY0(zc_min_txg_delay);
6289 spa_close(spa, FTAG);
6292 * Verify that we can loop over all pools.
6294 mutex_enter(&spa_namespace_lock);
6295 for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
6296 if (ztest_opts.zo_verbose > 3)
6297 (void) printf("spa_next: found %s\n", spa_name(spa));
6298 mutex_exit(&spa_namespace_lock);
6301 * Verify that we can export the pool and reimport it under a
6304 if (ztest_random(2) == 0) {
6305 char name[MAXNAMELEN];
6306 (void) snprintf(name, MAXNAMELEN, "%s_import",
6307 ztest_opts.zo_pool);
6308 ztest_spa_import_export(ztest_opts.zo_pool, name);
6309 ztest_spa_import_export(name, ztest_opts.zo_pool);
6314 list_destroy(&zcl.zcl_callbacks);
6315 mutex_destroy(&zcl.zcl_callbacks_lock);
6316 (void) rwlock_destroy(&ztest_name_lock);
6317 mutex_destroy(&ztest_vdev_lock);
6323 ztest_ds_t *zd = &ztest_ds[0];
6327 if (ztest_opts.zo_verbose >= 3)
6328 (void) printf("testing spa_freeze()...\n");
6330 kernel_init(FREAD | FWRITE);
6331 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6332 VERIFY3U(0, ==, ztest_dataset_open(0));
6333 spa->spa_debug = B_TRUE;
6337 * Force the first log block to be transactionally allocated.
6338 * We have to do this before we freeze the pool -- otherwise
6339 * the log chain won't be anchored.
6341 while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
6342 ztest_dmu_object_alloc_free(zd, 0);
6343 zil_commit(zd->zd_zilog, 0);
6346 txg_wait_synced(spa_get_dsl(spa), 0);
6349 * Freeze the pool. This stops spa_sync() from doing anything,
6350 * so that the only way to record changes from now on is the ZIL.
6355 * Because it is hard to predict how much space a write will actually
6356 * require beforehand, we leave ourselves some fudge space to write over
6359 uint64_t capacity = metaslab_class_get_space(spa_normal_class(spa)) / 2;
6362 * Run tests that generate log records but don't alter the pool config
6363 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
6364 * We do a txg_wait_synced() after each iteration to force the txg
6365 * to increase well beyond the last synced value in the uberblock.
6366 * The ZIL should be OK with that.
6368 * Run a random number of times less than zo_maxloops and ensure we do
6369 * not run out of space on the pool.
6371 while (ztest_random(10) != 0 &&
6372 numloops++ < ztest_opts.zo_maxloops &&
6373 metaslab_class_get_alloc(spa_normal_class(spa)) < capacity) {
6375 ztest_od_init(&od, 0, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
6376 VERIFY0(ztest_object_init(zd, &od, sizeof (od), B_FALSE));
6377 ztest_io(zd, od.od_object,
6378 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
6379 txg_wait_synced(spa_get_dsl(spa), 0);
6383 * Commit all of the changes we just generated.
6385 zil_commit(zd->zd_zilog, 0);
6386 txg_wait_synced(spa_get_dsl(spa), 0);
6389 * Close our dataset and close the pool.
6391 ztest_dataset_close(0);
6392 spa_close(spa, FTAG);
6396 * Open and close the pool and dataset to induce log replay.
6398 kernel_init(FREAD | FWRITE);
6399 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6400 ASSERT(spa_freeze_txg(spa) == UINT64_MAX);
6401 VERIFY3U(0, ==, ztest_dataset_open(0));
6402 ztest_dataset_close(0);
6404 spa->spa_debug = B_TRUE;
6406 txg_wait_synced(spa_get_dsl(spa), 0);
6407 ztest_reguid(NULL, 0);
6409 spa_close(spa, FTAG);
6414 print_time(hrtime_t t, char *timebuf)
6416 hrtime_t s = t / NANOSEC;
6417 hrtime_t m = s / 60;
6418 hrtime_t h = m / 60;
6419 hrtime_t d = h / 24;
6428 (void) sprintf(timebuf,
6429 "%llud%02lluh%02llum%02llus", d, h, m, s);
6431 (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
6433 (void) sprintf(timebuf, "%llum%02llus", m, s);
6435 (void) sprintf(timebuf, "%llus", s);
6439 make_random_props(void)
6443 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
6444 if (ztest_random(2) == 0)
6446 VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
6452 * Create a storage pool with the given name and initial vdev size.
6453 * Then test spa_freeze() functionality.
6456 ztest_init(ztest_shared_t *zs)
6459 nvlist_t *nvroot, *props;
6462 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
6463 VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0);
6465 kernel_init(FREAD | FWRITE);
6468 * Create the storage pool.
6470 (void) spa_destroy(ztest_opts.zo_pool);
6471 ztest_shared->zs_vdev_next_leaf = 0;
6473 zs->zs_mirrors = ztest_opts.zo_mirrors;
6474 nvroot = make_vdev_root(NULL, NULL, NULL, ztest_opts.zo_vdev_size, 0,
6475 0, ztest_opts.zo_raidz, zs->zs_mirrors, 1);
6476 props = make_random_props();
6477 for (i = 0; i < SPA_FEATURES; i++) {
6479 VERIFY3S(-1, !=, asprintf(&buf, "feature@%s",
6480 spa_feature_table[i].fi_uname));
6481 VERIFY3U(0, ==, nvlist_add_uint64(props, buf, 0));
6484 VERIFY3U(0, ==, spa_create(ztest_opts.zo_pool, nvroot, props, NULL));
6485 nvlist_free(nvroot);
6488 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6489 zs->zs_metaslab_sz =
6490 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
6491 spa_close(spa, FTAG);
6495 ztest_run_zdb(ztest_opts.zo_pool);
6499 ztest_run_zdb(ztest_opts.zo_pool);
6501 (void) rwlock_destroy(&ztest_name_lock);
6502 mutex_destroy(&ztest_vdev_lock);
6508 static char ztest_name_data[] = "/tmp/ztest.data.XXXXXX";
6510 ztest_fd_data = mkstemp(ztest_name_data);
6511 ASSERT3S(ztest_fd_data, >=, 0);
6512 (void) unlink(ztest_name_data);
6516 shared_data_size(ztest_shared_hdr_t *hdr)
6520 size = hdr->zh_hdr_size;
6521 size += hdr->zh_opts_size;
6522 size += hdr->zh_size;
6523 size += hdr->zh_stats_size * hdr->zh_stats_count;
6524 size += hdr->zh_ds_size * hdr->zh_ds_count;
6533 ztest_shared_hdr_t *hdr;
6535 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6536 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6537 ASSERT(hdr != MAP_FAILED);
6539 VERIFY3U(0, ==, ftruncate(ztest_fd_data, sizeof (ztest_shared_hdr_t)));
6541 hdr->zh_hdr_size = sizeof (ztest_shared_hdr_t);
6542 hdr->zh_opts_size = sizeof (ztest_shared_opts_t);
6543 hdr->zh_size = sizeof (ztest_shared_t);
6544 hdr->zh_stats_size = sizeof (ztest_shared_callstate_t);
6545 hdr->zh_stats_count = ZTEST_FUNCS;
6546 hdr->zh_ds_size = sizeof (ztest_shared_ds_t);
6547 hdr->zh_ds_count = ztest_opts.zo_datasets;
6549 size = shared_data_size(hdr);
6550 VERIFY3U(0, ==, ftruncate(ztest_fd_data, size));
6552 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6559 ztest_shared_hdr_t *hdr;
6562 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6563 PROT_READ, MAP_SHARED, ztest_fd_data, 0);
6564 ASSERT(hdr != MAP_FAILED);
6566 size = shared_data_size(hdr);
6568 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6569 hdr = ztest_shared_hdr = (void *)mmap(0, P2ROUNDUP(size, getpagesize()),
6570 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6571 ASSERT(hdr != MAP_FAILED);
6572 buf = (uint8_t *)hdr;
6574 offset = hdr->zh_hdr_size;
6575 ztest_shared_opts = (void *)&buf[offset];
6576 offset += hdr->zh_opts_size;
6577 ztest_shared = (void *)&buf[offset];
6578 offset += hdr->zh_size;
6579 ztest_shared_callstate = (void *)&buf[offset];
6580 offset += hdr->zh_stats_size * hdr->zh_stats_count;
6581 ztest_shared_ds = (void *)&buf[offset];
6585 exec_child(char *cmd, char *libpath, boolean_t ignorekill, int *statusp)
6589 char *cmdbuf = NULL;
6594 cmdbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
6595 (void) strlcpy(cmdbuf, getexecname(), MAXPATHLEN);
6600 fatal(1, "fork failed");
6602 if (pid == 0) { /* child */
6603 char *emptyargv[2] = { cmd, NULL };
6604 char fd_data_str[12];
6606 struct rlimit rl = { 1024, 1024 };
6607 (void) setrlimit(RLIMIT_NOFILE, &rl);
6609 (void) close(ztest_fd_rand);
6610 VERIFY(11 >= snprintf(fd_data_str, 12, "%d", ztest_fd_data));
6611 VERIFY(0 == setenv("ZTEST_FD_DATA", fd_data_str, 1));
6613 (void) enable_extended_FILE_stdio(-1, -1);
6614 if (libpath != NULL)
6615 VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath, 1));
6616 (void) execv(cmd, emptyargv);
6617 ztest_dump_core = B_FALSE;
6618 fatal(B_TRUE, "exec failed: %s", cmd);
6621 if (cmdbuf != NULL) {
6622 umem_free(cmdbuf, MAXPATHLEN);
6626 while (waitpid(pid, &status, 0) != pid)
6628 if (statusp != NULL)
6631 if (WIFEXITED(status)) {
6632 if (WEXITSTATUS(status) != 0) {
6633 (void) fprintf(stderr, "child exited with code %d\n",
6634 WEXITSTATUS(status));
6638 } else if (WIFSIGNALED(status)) {
6639 if (!ignorekill || WTERMSIG(status) != SIGKILL) {
6640 (void) fprintf(stderr, "child died with signal %d\n",
6646 (void) fprintf(stderr, "something strange happened to child\n");
6653 ztest_run_init(void)
6657 ztest_shared_t *zs = ztest_shared;
6659 ASSERT(ztest_opts.zo_init != 0);
6662 * Blow away any existing copy of zpool.cache
6664 (void) remove(spa_config_path);
6667 * Create and initialize our storage pool.
6669 for (i = 1; i <= ztest_opts.zo_init; i++) {
6670 bzero(zs, sizeof (ztest_shared_t));
6671 if (ztest_opts.zo_verbose >= 3 &&
6672 ztest_opts.zo_init != 1) {
6673 (void) printf("ztest_init(), pass %d\n", i);
6680 main(int argc, char **argv)
6688 ztest_shared_callstate_t *zc;
6695 char *fd_data_str = getenv("ZTEST_FD_DATA");
6696 struct sigaction action;
6698 (void) setvbuf(stdout, NULL, _IOLBF, 0);
6700 dprintf_setup(&argc, argv);
6702 action.sa_handler = sig_handler;
6703 sigemptyset(&action.sa_mask);
6704 action.sa_flags = 0;
6706 if (sigaction(SIGSEGV, &action, NULL) < 0) {
6707 (void) fprintf(stderr, "ztest: cannot catch SIGSEGV: %s.\n",
6712 if (sigaction(SIGABRT, &action, NULL) < 0) {
6713 (void) fprintf(stderr, "ztest: cannot catch SIGABRT: %s.\n",
6718 ztest_fd_rand = open("/dev/urandom", O_RDONLY);
6719 ASSERT3S(ztest_fd_rand, >=, 0);
6722 process_options(argc, argv);
6727 bcopy(&ztest_opts, ztest_shared_opts,
6728 sizeof (*ztest_shared_opts));
6730 ztest_fd_data = atoi(fd_data_str);
6732 bcopy(ztest_shared_opts, &ztest_opts, sizeof (ztest_opts));
6734 ASSERT3U(ztest_opts.zo_datasets, ==, ztest_shared_hdr->zh_ds_count);
6736 /* Override location of zpool.cache */
6737 VERIFY(asprintf((char **)&spa_config_path, "%s/zpool.cache",
6738 ztest_opts.zo_dir) != -1);
6740 ztest_ds = umem_alloc(ztest_opts.zo_datasets * sizeof (ztest_ds_t),
6745 metaslab_gang_bang = ztest_opts.zo_metaslab_gang_bang;
6746 metaslab_df_alloc_threshold =
6747 zs->zs_metaslab_df_alloc_threshold;
6756 hasalt = (strlen(ztest_opts.zo_alt_ztest) != 0);
6758 if (ztest_opts.zo_verbose >= 1) {
6759 (void) printf("%llu vdevs, %d datasets, %d threads,"
6760 " %llu seconds...\n",
6761 (u_longlong_t)ztest_opts.zo_vdevs,
6762 ztest_opts.zo_datasets,
6763 ztest_opts.zo_threads,
6764 (u_longlong_t)ztest_opts.zo_time);
6767 cmd = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
6768 (void) strlcpy(cmd, getexecname(), MAXNAMELEN);
6770 zs->zs_do_init = B_TRUE;
6771 if (strlen(ztest_opts.zo_alt_ztest) != 0) {
6772 if (ztest_opts.zo_verbose >= 1) {
6773 (void) printf("Executing older ztest for "
6774 "initialization: %s\n", ztest_opts.zo_alt_ztest);
6776 VERIFY(!exec_child(ztest_opts.zo_alt_ztest,
6777 ztest_opts.zo_alt_libpath, B_FALSE, NULL));
6779 VERIFY(!exec_child(NULL, NULL, B_FALSE, NULL));
6781 zs->zs_do_init = B_FALSE;
6783 zs->zs_proc_start = gethrtime();
6784 zs->zs_proc_stop = zs->zs_proc_start + ztest_opts.zo_time * NANOSEC;
6786 for (f = 0; f < ZTEST_FUNCS; f++) {
6787 zi = &ztest_info[f];
6788 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6789 if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
6790 zc->zc_next = UINT64_MAX;
6792 zc->zc_next = zs->zs_proc_start +
6793 ztest_random(2 * zi->zi_interval[0] + 1);
6797 * Run the tests in a loop. These tests include fault injection
6798 * to verify that self-healing data works, and forced crashes
6799 * to verify that we never lose on-disk consistency.
6801 while (gethrtime() < zs->zs_proc_stop) {
6806 * Initialize the workload counters for each function.
6808 for (f = 0; f < ZTEST_FUNCS; f++) {
6809 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6814 /* Set the allocation switch size */
6815 zs->zs_metaslab_df_alloc_threshold =
6816 ztest_random(zs->zs_metaslab_sz / 4) + 1;
6818 if (!hasalt || ztest_random(2) == 0) {
6819 if (hasalt && ztest_opts.zo_verbose >= 1) {
6820 (void) printf("Executing newer ztest: %s\n",
6824 killed = exec_child(cmd, NULL, B_TRUE, &status);
6826 if (hasalt && ztest_opts.zo_verbose >= 1) {
6827 (void) printf("Executing older ztest: %s\n",
6828 ztest_opts.zo_alt_ztest);
6831 killed = exec_child(ztest_opts.zo_alt_ztest,
6832 ztest_opts.zo_alt_libpath, B_TRUE, &status);
6839 if (ztest_opts.zo_verbose >= 1) {
6840 hrtime_t now = gethrtime();
6842 now = MIN(now, zs->zs_proc_stop);
6843 print_time(zs->zs_proc_stop - now, timebuf);
6844 nicenum(zs->zs_space, numbuf);
6846 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
6847 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
6849 WIFEXITED(status) ? "Complete" : "SIGKILL",
6850 (u_longlong_t)zs->zs_enospc_count,
6851 100.0 * zs->zs_alloc / zs->zs_space,
6853 100.0 * (now - zs->zs_proc_start) /
6854 (ztest_opts.zo_time * NANOSEC), timebuf);
6857 if (ztest_opts.zo_verbose >= 2) {
6858 (void) printf("\nWorkload summary:\n\n");
6859 (void) printf("%7s %9s %s\n",
6860 "Calls", "Time", "Function");
6861 (void) printf("%7s %9s %s\n",
6862 "-----", "----", "--------");
6863 for (f = 0; f < ZTEST_FUNCS; f++) {
6864 zi = &ztest_info[f];
6865 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6866 print_time(zc->zc_time, timebuf);
6867 (void) printf("%7llu %9s %s\n",
6868 (u_longlong_t)zc->zc_count, timebuf,
6871 (void) printf("\n");
6875 * It's possible that we killed a child during a rename test,
6876 * in which case we'll have a 'ztest_tmp' pool lying around
6877 * instead of 'ztest'. Do a blind rename in case this happened.
6880 if (spa_open(ztest_opts.zo_pool, &spa, FTAG) == 0) {
6881 spa_close(spa, FTAG);
6883 char tmpname[MAXNAMELEN];
6885 kernel_init(FREAD | FWRITE);
6886 (void) snprintf(tmpname, sizeof (tmpname), "%s_tmp",
6887 ztest_opts.zo_pool);
6888 (void) spa_rename(tmpname, ztest_opts.zo_pool);
6892 ztest_run_zdb(ztest_opts.zo_pool);
6895 if (ztest_opts.zo_verbose >= 1) {
6897 (void) printf("%d runs of older ztest: %s\n", older,
6898 ztest_opts.zo_alt_ztest);
6899 (void) printf("%d runs of newer ztest: %s\n", newer,
6902 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
6903 kills, iters - kills, (100.0 * kills) / MAX(1, iters));
6906 umem_free(cmd, MAXNAMELEN);