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, 2017 by Delphix. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2013 Steven Hartland. All rights reserved.
26 * Copyright (c) 2014 Integros [integros.com]
27 * Copyright 2017 Joyent, Inc.
31 * The objective of this program is to provide a DMU/ZAP/SPA stress test
32 * that runs entirely in userland, is easy to use, and easy to extend.
34 * The overall design of the ztest program is as follows:
36 * (1) For each major functional area (e.g. adding vdevs to a pool,
37 * creating and destroying datasets, reading and writing objects, etc)
38 * we have a simple routine to test that functionality. These
39 * individual routines do not have to do anything "stressful".
41 * (2) We turn these simple functionality tests into a stress test by
42 * running them all in parallel, with as many threads as desired,
43 * and spread across as many datasets, objects, and vdevs as desired.
45 * (3) While all this is happening, we inject faults into the pool to
46 * verify that self-healing data really works.
48 * (4) Every time we open a dataset, we change its checksum and compression
49 * functions. Thus even individual objects vary from block to block
50 * in which checksum they use and whether they're compressed.
52 * (5) To verify that we never lose on-disk consistency after a crash,
53 * we run the entire test in a child of the main process.
54 * At random times, the child self-immolates with a SIGKILL.
55 * This is the software equivalent of pulling the power cord.
56 * The parent then runs the test again, using the existing
57 * storage pool, as many times as desired. If backwards compatibility
58 * testing is enabled ztest will sometimes run the "older" version
59 * of ztest after a SIGKILL.
61 * (6) To verify that we don't have future leaks or temporal incursions,
62 * many of the functional tests record the transaction group number
63 * as part of their data. When reading old data, they verify that
64 * the transaction group number is less than the current, open txg.
65 * If you add a new test, please do this if applicable.
67 * (7) Threads are created with a reduced stack size, for sanity checking.
68 * Therefore, it's important not to allocate huge buffers on the stack.
70 * When run with no arguments, ztest runs for about five minutes and
71 * produces no output if successful. To get a little bit of information,
72 * specify -V. To get more information, specify -VV, and so on.
74 * To turn this into an overnight stress test, use -T to specify run time.
76 * You can ask more more vdevs [-v], datasets [-d], or threads [-t]
77 * to increase the pool capacity, fanout, and overall stress level.
79 * Use the -k option to set the desired frequency of kills.
81 * When ztest invokes itself it passes all relevant information through a
82 * temporary file which is mmap-ed in the child process. This allows shared
83 * memory to survive the exec syscall. The ztest_shared_hdr_t struct is always
84 * stored at offset 0 of this file and contains information on the size and
85 * number of shared structures in the file. The information stored in this file
86 * must remain backwards compatible with older versions of ztest so that
87 * ztest can invoke them during backwards compatibility testing (-B).
90 #include <sys/zfs_context.h>
96 #include <sys/dmu_objset.h>
100 #include <sys/wait.h>
101 #include <sys/mman.h>
102 #include <sys/resource.h>
105 #include <sys/zil_impl.h>
106 #include <sys/zfs_rlock.h>
107 #include <sys/vdev_impl.h>
108 #include <sys/vdev_file.h>
109 #include <sys/spa_impl.h>
110 #include <sys/metaslab_impl.h>
111 #include <sys/dsl_prop.h>
112 #include <sys/dsl_dataset.h>
113 #include <sys/dsl_destroy.h>
114 #include <sys/dsl_scan.h>
115 #include <sys/zio_checksum.h>
116 #include <sys/refcount.h>
117 #include <sys/zfeature.h>
118 #include <sys/dsl_userhold.h>
121 #include <stdio_ext.h>
128 #include <sys/fs/zfs.h>
129 #include <zfs_fletcher.h>
130 #include <libnvpair.h>
132 #include <sys/crypto/icp.h>
134 #include <execinfo.h> /* for backtrace() */
137 static int ztest_fd_data = -1;
138 static int ztest_fd_rand = -1;
140 typedef struct ztest_shared_hdr {
141 uint64_t zh_hdr_size;
142 uint64_t zh_opts_size;
144 uint64_t zh_stats_size;
145 uint64_t zh_stats_count;
147 uint64_t zh_ds_count;
148 } ztest_shared_hdr_t;
150 static ztest_shared_hdr_t *ztest_shared_hdr;
152 typedef struct ztest_shared_opts {
153 char zo_pool[ZFS_MAX_DATASET_NAME_LEN];
154 char zo_dir[ZFS_MAX_DATASET_NAME_LEN];
155 char zo_alt_ztest[MAXNAMELEN];
156 char zo_alt_libpath[MAXNAMELEN];
158 uint64_t zo_vdevtime;
166 uint64_t zo_passtime;
167 uint64_t zo_killrate;
171 uint64_t zo_maxloops;
172 uint64_t zo_metaslab_force_ganging;
174 } ztest_shared_opts_t;
176 static const ztest_shared_opts_t ztest_opts_defaults = {
177 .zo_pool = { 'z', 't', 'e', 's', 't', '\0' },
178 .zo_dir = { '/', 't', 'm', 'p', '\0' },
179 .zo_alt_ztest = { '\0' },
180 .zo_alt_libpath = { '\0' },
182 .zo_ashift = SPA_MINBLOCKSHIFT,
185 .zo_raidz_parity = 1,
186 .zo_vdev_size = SPA_MINDEVSIZE * 4, /* 256m default size */
189 .zo_passtime = 60, /* 60 seconds */
190 .zo_killrate = 70, /* 70% kill rate */
194 .zo_time = 300, /* 5 minutes */
195 .zo_maxloops = 50, /* max loops during spa_freeze() */
196 .zo_metaslab_force_ganging = 32 << 10
199 extern uint64_t metaslab_force_ganging;
200 extern uint64_t metaslab_df_alloc_threshold;
201 extern unsigned long zfs_deadman_synctime_ms;
202 extern int metaslab_preload_limit;
203 extern boolean_t zfs_compressed_arc_enabled;
204 extern int zfs_abd_scatter_enabled;
205 extern int dmu_object_alloc_chunk_shift;
206 extern boolean_t zfs_force_some_double_word_sm_entries;
208 static ztest_shared_opts_t *ztest_shared_opts;
209 static ztest_shared_opts_t ztest_opts;
210 static char *ztest_wkeydata = "abcdefghijklmnopqrstuvwxyz012345";
212 typedef struct ztest_shared_ds {
216 static ztest_shared_ds_t *ztest_shared_ds;
217 #define ZTEST_GET_SHARED_DS(d) (&ztest_shared_ds[d])
219 #define BT_MAGIC 0x123456789abcdefULL
220 #define MAXFAULTS(zs) \
221 (MAX((zs)->zs_mirrors, 1) * (ztest_opts.zo_raidz_parity + 1) - 1)
225 ZTEST_IO_WRITE_PATTERN,
226 ZTEST_IO_WRITE_ZEROES,
233 typedef struct ztest_block_tag {
237 uint64_t bt_dnodesize;
244 typedef struct bufwad {
262 #define ZTEST_RANGE_LOCKS 64
263 #define ZTEST_OBJECT_LOCKS 64
266 * Object descriptor. Used as a template for object lookup/create/remove.
268 typedef struct ztest_od {
271 dmu_object_type_t od_type;
272 dmu_object_type_t od_crtype;
273 uint64_t od_blocksize;
274 uint64_t od_crblocksize;
275 uint64_t od_crdnodesize;
278 char od_name[ZFS_MAX_DATASET_NAME_LEN];
284 typedef struct ztest_ds {
285 ztest_shared_ds_t *zd_shared;
287 pthread_rwlock_t zd_zilog_lock;
289 ztest_od_t *zd_od; /* debugging aid */
290 char zd_name[ZFS_MAX_DATASET_NAME_LEN];
291 kmutex_t zd_dirobj_lock;
292 rll_t zd_object_lock[ZTEST_OBJECT_LOCKS];
293 zll_t zd_range_lock[ZTEST_RANGE_LOCKS];
297 * Per-iteration state.
299 typedef void ztest_func_t(ztest_ds_t *zd, uint64_t id);
301 typedef struct ztest_info {
302 ztest_func_t *zi_func; /* test function */
303 uint64_t zi_iters; /* iterations per execution */
304 uint64_t *zi_interval; /* execute every <interval> seconds */
305 const char *zi_funcname; /* name of test function */
308 typedef struct ztest_shared_callstate {
309 uint64_t zc_count; /* per-pass count */
310 uint64_t zc_time; /* per-pass time */
311 uint64_t zc_next; /* next time to call this function */
312 } ztest_shared_callstate_t;
314 static ztest_shared_callstate_t *ztest_shared_callstate;
315 #define ZTEST_GET_SHARED_CALLSTATE(c) (&ztest_shared_callstate[c])
317 ztest_func_t ztest_dmu_read_write;
318 ztest_func_t ztest_dmu_write_parallel;
319 ztest_func_t ztest_dmu_object_alloc_free;
320 ztest_func_t ztest_dmu_object_next_chunk;
321 ztest_func_t ztest_dmu_commit_callbacks;
322 ztest_func_t ztest_zap;
323 ztest_func_t ztest_zap_parallel;
324 ztest_func_t ztest_zil_commit;
325 ztest_func_t ztest_zil_remount;
326 ztest_func_t ztest_dmu_read_write_zcopy;
327 ztest_func_t ztest_dmu_objset_create_destroy;
328 ztest_func_t ztest_dmu_prealloc;
329 ztest_func_t ztest_fzap;
330 ztest_func_t ztest_dmu_snapshot_create_destroy;
331 ztest_func_t ztest_dsl_prop_get_set;
332 ztest_func_t ztest_spa_prop_get_set;
333 ztest_func_t ztest_spa_create_destroy;
334 ztest_func_t ztest_fault_inject;
335 ztest_func_t ztest_ddt_repair;
336 ztest_func_t ztest_dmu_snapshot_hold;
337 ztest_func_t ztest_mmp_enable_disable;
338 ztest_func_t ztest_spa_rename;
339 ztest_func_t ztest_scrub;
340 ztest_func_t ztest_dsl_dataset_promote_busy;
341 ztest_func_t ztest_vdev_attach_detach;
342 ztest_func_t ztest_vdev_LUN_growth;
343 ztest_func_t ztest_vdev_add_remove;
344 ztest_func_t ztest_vdev_aux_add_remove;
345 ztest_func_t ztest_split_pool;
346 ztest_func_t ztest_reguid;
347 ztest_func_t ztest_spa_upgrade;
348 ztest_func_t ztest_device_removal;
349 ztest_func_t ztest_remap_blocks;
350 ztest_func_t ztest_spa_checkpoint_create_discard;
351 ztest_func_t ztest_fletcher;
352 ztest_func_t ztest_fletcher_incr;
353 ztest_func_t ztest_verify_dnode_bt;
355 uint64_t zopt_always = 0ULL * NANOSEC; /* all the time */
356 uint64_t zopt_incessant = 1ULL * NANOSEC / 10; /* every 1/10 second */
357 uint64_t zopt_often = 1ULL * NANOSEC; /* every second */
358 uint64_t zopt_sometimes = 10ULL * NANOSEC; /* every 10 seconds */
359 uint64_t zopt_rarely = 60ULL * NANOSEC; /* every 60 seconds */
361 #define ZTI_INIT(func, iters, interval) \
362 { .zi_func = (func), \
363 .zi_iters = (iters), \
364 .zi_interval = (interval), \
365 .zi_funcname = # func }
367 ztest_info_t ztest_info[] = {
368 ZTI_INIT(ztest_dmu_read_write, 1, &zopt_always),
369 ZTI_INIT(ztest_dmu_write_parallel, 10, &zopt_always),
370 ZTI_INIT(ztest_dmu_object_alloc_free, 1, &zopt_always),
371 ZTI_INIT(ztest_dmu_object_next_chunk, 1, &zopt_sometimes),
372 ZTI_INIT(ztest_dmu_commit_callbacks, 1, &zopt_always),
373 ZTI_INIT(ztest_zap, 30, &zopt_always),
374 ZTI_INIT(ztest_zap_parallel, 100, &zopt_always),
375 ZTI_INIT(ztest_split_pool, 1, &zopt_always),
376 ZTI_INIT(ztest_zil_commit, 1, &zopt_incessant),
377 ZTI_INIT(ztest_zil_remount, 1, &zopt_sometimes),
378 ZTI_INIT(ztest_dmu_read_write_zcopy, 1, &zopt_often),
379 ZTI_INIT(ztest_dmu_objset_create_destroy, 1, &zopt_often),
380 ZTI_INIT(ztest_dsl_prop_get_set, 1, &zopt_often),
381 ZTI_INIT(ztest_spa_prop_get_set, 1, &zopt_sometimes),
383 ZTI_INIT(ztest_dmu_prealloc, 1, &zopt_sometimes),
385 ZTI_INIT(ztest_fzap, 1, &zopt_sometimes),
386 ZTI_INIT(ztest_dmu_snapshot_create_destroy, 1, &zopt_sometimes),
387 ZTI_INIT(ztest_spa_create_destroy, 1, &zopt_sometimes),
388 ZTI_INIT(ztest_fault_inject, 1, &zopt_sometimes),
389 ZTI_INIT(ztest_ddt_repair, 1, &zopt_sometimes),
390 ZTI_INIT(ztest_dmu_snapshot_hold, 1, &zopt_sometimes),
391 ZTI_INIT(ztest_mmp_enable_disable, 1, &zopt_sometimes),
392 ZTI_INIT(ztest_reguid, 1, &zopt_rarely),
393 ZTI_INIT(ztest_spa_rename, 1, &zopt_rarely),
394 ZTI_INIT(ztest_scrub, 1, &zopt_rarely),
395 ZTI_INIT(ztest_spa_upgrade, 1, &zopt_rarely),
396 ZTI_INIT(ztest_dsl_dataset_promote_busy, 1, &zopt_rarely),
397 ZTI_INIT(ztest_vdev_attach_detach, 1, &zopt_sometimes),
398 ZTI_INIT(ztest_vdev_LUN_growth, 1, &zopt_rarely),
399 ZTI_INIT(ztest_vdev_add_remove, 1, &ztest_opts.zo_vdevtime),
400 ZTI_INIT(ztest_vdev_aux_add_remove, 1, &ztest_opts.zo_vdevtime),
401 ZTI_INIT(ztest_device_removal, 1, &zopt_sometimes),
402 ZTI_INIT(ztest_remap_blocks, 1, &zopt_sometimes),
403 ZTI_INIT(ztest_spa_checkpoint_create_discard, 1, &zopt_rarely),
404 ZTI_INIT(ztest_fletcher, 1, &zopt_rarely),
405 ZTI_INIT(ztest_fletcher_incr, 1, &zopt_rarely),
406 ZTI_INIT(ztest_verify_dnode_bt, 1, &zopt_sometimes),
409 #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
412 * The following struct is used to hold a list of uncalled commit callbacks.
413 * The callbacks are ordered by txg number.
415 typedef struct ztest_cb_list {
416 kmutex_t zcl_callbacks_lock;
417 list_t zcl_callbacks;
421 * Stuff we need to share writably between parent and child.
423 typedef struct ztest_shared {
424 boolean_t zs_do_init;
425 hrtime_t zs_proc_start;
426 hrtime_t zs_proc_stop;
427 hrtime_t zs_thread_start;
428 hrtime_t zs_thread_stop;
429 hrtime_t zs_thread_kill;
430 uint64_t zs_enospc_count;
431 uint64_t zs_vdev_next_leaf;
432 uint64_t zs_vdev_aux;
437 uint64_t zs_metaslab_sz;
438 uint64_t zs_metaslab_df_alloc_threshold;
442 #define ID_PARALLEL -1ULL
444 static char ztest_dev_template[] = "%s/%s.%llua";
445 static char ztest_aux_template[] = "%s/%s.%s.%llu";
446 ztest_shared_t *ztest_shared;
448 static spa_t *ztest_spa = NULL;
449 static ztest_ds_t *ztest_ds;
451 static kmutex_t ztest_vdev_lock;
452 static boolean_t ztest_device_removal_active = B_FALSE;
453 static kmutex_t ztest_checkpoint_lock;
456 * The ztest_name_lock protects the pool and dataset namespace used by
457 * the individual tests. To modify the namespace, consumers must grab
458 * this lock as writer. Grabbing the lock as reader will ensure that the
459 * namespace does not change while the lock is held.
461 static pthread_rwlock_t ztest_name_lock;
463 static boolean_t ztest_dump_core = B_TRUE;
464 static boolean_t ztest_dump_debug_buffer = B_FALSE;
465 static boolean_t ztest_exiting;
467 /* Global commit callback list */
468 static ztest_cb_list_t zcl;
469 /* Commit cb delay */
470 static uint64_t zc_min_txg_delay = UINT64_MAX;
471 static int zc_cb_counter = 0;
474 * Minimum number of commit callbacks that need to be registered for us to check
475 * whether the minimum txg delay is acceptable.
477 #define ZTEST_COMMIT_CB_MIN_REG 100
480 * If a number of txgs equal to this threshold have been created after a commit
481 * callback has been registered but not called, then we assume there is an
482 * implementation bug.
484 #define ZTEST_COMMIT_CB_THRESH (TXG_CONCURRENT_STATES + 1000)
486 extern uint64_t metaslab_gang_bang;
487 extern uint64_t metaslab_df_alloc_threshold;
490 ZTEST_META_DNODE = 0,
495 static void usage(boolean_t) __NORETURN;
498 * These libumem hooks provide a reasonable set of defaults for the allocator's
499 * debugging facilities.
502 _umem_debug_init(void)
504 return ("default,verbose"); /* $UMEM_DEBUG setting */
508 _umem_logging_init(void)
510 return ("fail,contents"); /* $UMEM_LOGGING setting */
514 dump_debug_buffer(void)
516 if (!ztest_dump_debug_buffer)
520 zfs_dbgmsg_print("ztest");
523 #define BACKTRACE_SZ 100
525 static void sig_handler(int signo)
527 struct sigaction action;
528 #ifdef __GLIBC__ /* backtrace() is a GNU extension */
530 void *buffer[BACKTRACE_SZ];
532 nptrs = backtrace(buffer, BACKTRACE_SZ);
533 backtrace_symbols_fd(buffer, nptrs, STDERR_FILENO);
538 * Restore default action and re-raise signal so SIGSEGV and
539 * SIGABRT can trigger a core dump.
541 action.sa_handler = SIG_DFL;
542 sigemptyset(&action.sa_mask);
544 (void) sigaction(signo, &action, NULL);
548 #define FATAL_MSG_SZ 1024
553 fatal(int do_perror, char *message, ...)
556 int save_errno = errno;
559 (void) fflush(stdout);
560 buf = umem_alloc(FATAL_MSG_SZ, UMEM_NOFAIL);
562 va_start(args, message);
563 (void) sprintf(buf, "ztest: ");
565 (void) vsprintf(buf + strlen(buf), message, args);
568 (void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
569 ": %s", strerror(save_errno));
571 (void) fprintf(stderr, "%s\n", buf);
572 fatal_msg = buf; /* to ease debugging */
582 str2shift(const char *buf)
584 const char *ends = "BKMGTPEZ";
589 for (i = 0; i < strlen(ends); i++) {
590 if (toupper(buf[0]) == ends[i])
593 if (i == strlen(ends)) {
594 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
598 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
601 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
607 nicenumtoull(const char *buf)
612 val = strtoull(buf, &end, 0);
614 (void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
616 } else if (end[0] == '.') {
617 double fval = strtod(buf, &end);
618 fval *= pow(2, str2shift(end));
619 if (fval > UINT64_MAX) {
620 (void) fprintf(stderr, "ztest: value too large: %s\n",
624 val = (uint64_t)fval;
626 int shift = str2shift(end);
627 if (shift >= 64 || (val << shift) >> shift != val) {
628 (void) fprintf(stderr, "ztest: value too large: %s\n",
638 usage(boolean_t requested)
640 const ztest_shared_opts_t *zo = &ztest_opts_defaults;
642 char nice_vdev_size[NN_NUMBUF_SZ];
643 char nice_force_ganging[NN_NUMBUF_SZ];
644 FILE *fp = requested ? stdout : stderr;
646 nicenum(zo->zo_vdev_size, nice_vdev_size, sizeof (nice_vdev_size));
647 nicenum(zo->zo_metaslab_force_ganging, nice_force_ganging,
648 sizeof (nice_force_ganging));
650 (void) fprintf(fp, "Usage: %s\n"
651 "\t[-v vdevs (default: %llu)]\n"
652 "\t[-s size_of_each_vdev (default: %s)]\n"
653 "\t[-a alignment_shift (default: %d)] use 0 for random\n"
654 "\t[-m mirror_copies (default: %d)]\n"
655 "\t[-r raidz_disks (default: %d)]\n"
656 "\t[-R raidz_parity (default: %d)]\n"
657 "\t[-d datasets (default: %d)]\n"
658 "\t[-t threads (default: %d)]\n"
659 "\t[-g gang_block_threshold (default: %s)]\n"
660 "\t[-i init_count (default: %d)] initialize pool i times\n"
661 "\t[-k kill_percentage (default: %llu%%)]\n"
662 "\t[-p pool_name (default: %s)]\n"
663 "\t[-f dir (default: %s)] file directory for vdev files\n"
664 "\t[-M] Multi-host simulate pool imported on remote host\n"
665 "\t[-V] verbose (use multiple times for ever more blather)\n"
666 "\t[-E] use existing pool instead of creating new one\n"
667 "\t[-T time (default: %llu sec)] total run time\n"
668 "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
669 "\t[-P passtime (default: %llu sec)] time per pass\n"
670 "\t[-B alt_ztest (default: <none>)] alternate ztest path\n"
671 "\t[-o variable=value] ... set global variable to an unsigned\n"
672 "\t 32-bit integer value\n"
673 "\t[-G dump zfs_dbgmsg buffer before exiting due to an error\n"
674 "\t[-h] (print help)\n"
677 (u_longlong_t)zo->zo_vdevs, /* -v */
678 nice_vdev_size, /* -s */
679 zo->zo_ashift, /* -a */
680 zo->zo_mirrors, /* -m */
681 zo->zo_raidz, /* -r */
682 zo->zo_raidz_parity, /* -R */
683 zo->zo_datasets, /* -d */
684 zo->zo_threads, /* -t */
685 nice_force_ganging, /* -g */
686 zo->zo_init, /* -i */
687 (u_longlong_t)zo->zo_killrate, /* -k */
688 zo->zo_pool, /* -p */
690 (u_longlong_t)zo->zo_time, /* -T */
691 (u_longlong_t)zo->zo_maxloops, /* -F */
692 (u_longlong_t)zo->zo_passtime);
693 exit(requested ? 0 : 1);
697 process_options(int argc, char **argv)
700 ztest_shared_opts_t *zo = &ztest_opts;
704 char altdir[MAXNAMELEN] = { 0 };
706 bcopy(&ztest_opts_defaults, zo, sizeof (*zo));
708 while ((opt = getopt(argc, argv,
709 "v:s:a:m:r:R:d:t:g:i:k:p:f:MVET:P:hF:B:o:G")) != EOF) {
726 value = nicenumtoull(optarg);
730 zo->zo_vdevs = value;
733 zo->zo_vdev_size = MAX(SPA_MINDEVSIZE, value);
736 zo->zo_ashift = value;
739 zo->zo_mirrors = value;
742 zo->zo_raidz = MAX(1, value);
745 zo->zo_raidz_parity = MIN(MAX(value, 1), 3);
748 zo->zo_datasets = MAX(1, value);
751 zo->zo_threads = MAX(1, value);
754 zo->zo_metaslab_force_ganging =
755 MAX(SPA_MINBLOCKSIZE << 1, value);
761 zo->zo_killrate = value;
764 (void) strlcpy(zo->zo_pool, optarg,
765 sizeof (zo->zo_pool));
768 path = realpath(optarg, NULL);
770 (void) fprintf(stderr, "error: %s: %s\n",
771 optarg, strerror(errno));
774 (void) strlcpy(zo->zo_dir, path,
775 sizeof (zo->zo_dir));
792 zo->zo_passtime = MAX(1, value);
795 zo->zo_maxloops = MAX(1, value);
798 (void) strlcpy(altdir, optarg, sizeof (altdir));
801 if (set_global_var(optarg) != 0)
805 ztest_dump_debug_buffer = B_TRUE;
817 zo->zo_raidz_parity = MIN(zo->zo_raidz_parity, zo->zo_raidz - 1);
820 (zo->zo_vdevs > 0 ? zo->zo_time * NANOSEC / zo->zo_vdevs :
823 if (strlen(altdir) > 0) {
831 cmd = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
832 realaltdir = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
834 VERIFY(NULL != realpath(getexecname(), cmd));
835 if (0 != access(altdir, F_OK)) {
836 ztest_dump_core = B_FALSE;
837 fatal(B_TRUE, "invalid alternate ztest path: %s",
840 VERIFY(NULL != realpath(altdir, realaltdir));
843 * 'cmd' should be of the form "<anything>/usr/bin/<isa>/ztest".
844 * We want to extract <isa> to determine if we should use
845 * 32 or 64 bit binaries.
847 bin = strstr(cmd, "/usr/bin/");
848 ztest = strstr(bin, "/ztest");
850 isalen = ztest - isa;
851 (void) snprintf(zo->zo_alt_ztest, sizeof (zo->zo_alt_ztest),
852 "%s/usr/bin/%.*s/ztest", realaltdir, isalen, isa);
853 (void) snprintf(zo->zo_alt_libpath, sizeof (zo->zo_alt_libpath),
854 "%s/usr/lib/%.*s", realaltdir, isalen, isa);
856 if (0 != access(zo->zo_alt_ztest, X_OK)) {
857 ztest_dump_core = B_FALSE;
858 fatal(B_TRUE, "invalid alternate ztest: %s",
860 } else if (0 != access(zo->zo_alt_libpath, X_OK)) {
861 ztest_dump_core = B_FALSE;
862 fatal(B_TRUE, "invalid alternate lib directory %s",
866 umem_free(cmd, MAXPATHLEN);
867 umem_free(realaltdir, MAXPATHLEN);
872 ztest_kill(ztest_shared_t *zs)
874 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(ztest_spa));
875 zs->zs_space = metaslab_class_get_space(spa_normal_class(ztest_spa));
878 * Before we kill off ztest, make sure that the config is updated.
879 * See comment above spa_write_cachefile().
881 mutex_enter(&spa_namespace_lock);
882 spa_write_cachefile(ztest_spa, B_FALSE, B_FALSE);
883 mutex_exit(&spa_namespace_lock);
885 (void) kill(getpid(), SIGKILL);
889 ztest_random(uint64_t range)
893 ASSERT3S(ztest_fd_rand, >=, 0);
898 if (read(ztest_fd_rand, &r, sizeof (r)) != sizeof (r))
899 fatal(1, "short read from /dev/urandom");
906 ztest_record_enospc(const char *s)
908 ztest_shared->zs_enospc_count++;
912 ztest_get_ashift(void)
914 if (ztest_opts.zo_ashift == 0)
915 return (SPA_MINBLOCKSHIFT + ztest_random(5));
916 return (ztest_opts.zo_ashift);
920 make_vdev_file(char *path, char *aux, char *pool, size_t size, uint64_t ashift)
926 pathbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
929 ashift = ztest_get_ashift();
935 vdev = ztest_shared->zs_vdev_aux;
936 (void) snprintf(path, MAXPATHLEN,
937 ztest_aux_template, ztest_opts.zo_dir,
938 pool == NULL ? ztest_opts.zo_pool : pool,
941 vdev = ztest_shared->zs_vdev_next_leaf++;
942 (void) snprintf(path, MAXPATHLEN,
943 ztest_dev_template, ztest_opts.zo_dir,
944 pool == NULL ? ztest_opts.zo_pool : pool, vdev);
949 int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
951 fatal(1, "can't open %s", path);
952 if (ftruncate(fd, size) != 0)
953 fatal(1, "can't ftruncate %s", path);
957 VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
958 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
959 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
960 VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
961 umem_free(pathbuf, MAXPATHLEN);
967 make_vdev_raidz(char *path, char *aux, char *pool, size_t size,
968 uint64_t ashift, int r)
970 nvlist_t *raidz, **child;
974 return (make_vdev_file(path, aux, pool, size, ashift));
975 child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
977 for (c = 0; c < r; c++)
978 child[c] = make_vdev_file(path, aux, pool, size, ashift);
980 VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
981 VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
982 VDEV_TYPE_RAIDZ) == 0);
983 VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
984 ztest_opts.zo_raidz_parity) == 0);
985 VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
988 for (c = 0; c < r; c++)
989 nvlist_free(child[c]);
991 umem_free(child, r * sizeof (nvlist_t *));
997 make_vdev_mirror(char *path, char *aux, char *pool, size_t size,
998 uint64_t ashift, int r, int m)
1000 nvlist_t *mirror, **child;
1004 return (make_vdev_raidz(path, aux, pool, size, ashift, r));
1006 child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
1008 for (c = 0; c < m; c++)
1009 child[c] = make_vdev_raidz(path, aux, pool, size, ashift, r);
1011 VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
1012 VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
1013 VDEV_TYPE_MIRROR) == 0);
1014 VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
1017 for (c = 0; c < m; c++)
1018 nvlist_free(child[c]);
1020 umem_free(child, m * sizeof (nvlist_t *));
1026 make_vdev_root(char *path, char *aux, char *pool, size_t size, uint64_t ashift,
1027 int log, int r, int m, int t)
1029 nvlist_t *root, **child;
1034 child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
1036 for (c = 0; c < t; c++) {
1037 child[c] = make_vdev_mirror(path, aux, pool, size, ashift,
1039 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1043 VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
1044 VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
1045 VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
1048 for (c = 0; c < t; c++)
1049 nvlist_free(child[c]);
1051 umem_free(child, t * sizeof (nvlist_t *));
1057 * Find a random spa version. Returns back a random spa version in the
1058 * range [initial_version, SPA_VERSION_FEATURES].
1061 ztest_random_spa_version(uint64_t initial_version)
1063 uint64_t version = initial_version;
1065 if (version <= SPA_VERSION_BEFORE_FEATURES) {
1067 ztest_random(SPA_VERSION_BEFORE_FEATURES - version + 1);
1070 if (version > SPA_VERSION_BEFORE_FEATURES)
1071 version = SPA_VERSION_FEATURES;
1073 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
1078 ztest_random_blocksize(void)
1081 * Choose a block size >= the ashift.
1082 * If the SPA supports new MAXBLOCKSIZE, test up to 1MB blocks.
1084 int maxbs = SPA_OLD_MAXBLOCKSHIFT;
1085 if (spa_maxblocksize(ztest_spa) == SPA_MAXBLOCKSIZE)
1087 uint64_t block_shift =
1088 ztest_random(maxbs - ztest_spa->spa_max_ashift + 1);
1089 return (1 << (SPA_MINBLOCKSHIFT + block_shift));
1093 ztest_random_dnodesize(void)
1096 int max_slots = spa_maxdnodesize(ztest_spa) >> DNODE_SHIFT;
1098 if (max_slots == DNODE_MIN_SLOTS)
1099 return (DNODE_MIN_SIZE);
1102 * Weight the random distribution more heavily toward smaller
1103 * dnode sizes since that is more likely to reflect real-world
1106 ASSERT3U(max_slots, >, 4);
1107 switch (ztest_random(10)) {
1109 slots = 5 + ztest_random(max_slots - 4);
1112 slots = 2 + ztest_random(3);
1119 return (slots << DNODE_SHIFT);
1123 ztest_random_ibshift(void)
1125 return (DN_MIN_INDBLKSHIFT +
1126 ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1));
1130 ztest_random_vdev_top(spa_t *spa, boolean_t log_ok)
1133 vdev_t *rvd = spa->spa_root_vdev;
1136 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1139 top = ztest_random(rvd->vdev_children);
1140 tvd = rvd->vdev_child[top];
1141 } while (!vdev_is_concrete(tvd) || (tvd->vdev_islog && !log_ok) ||
1142 tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL);
1148 ztest_random_dsl_prop(zfs_prop_t prop)
1153 value = zfs_prop_random_value(prop, ztest_random(-1ULL));
1154 } while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF);
1160 ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value,
1163 const char *propname = zfs_prop_to_name(prop);
1164 const char *valname;
1169 error = dsl_prop_set_int(osname, propname,
1170 (inherit ? ZPROP_SRC_NONE : ZPROP_SRC_LOCAL), value);
1172 if (error == ENOSPC) {
1173 ztest_record_enospc(FTAG);
1178 setpoint = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
1179 VERIFY0(dsl_prop_get_integer(osname, propname, &curval, setpoint));
1181 if (ztest_opts.zo_verbose >= 6) {
1184 err = zfs_prop_index_to_string(prop, curval, &valname);
1186 (void) printf("%s %s = %llu at '%s'\n", osname,
1187 propname, (unsigned long long)curval, setpoint);
1189 (void) printf("%s %s = %s at '%s'\n",
1190 osname, propname, valname, setpoint);
1192 umem_free(setpoint, MAXPATHLEN);
1198 ztest_spa_prop_set_uint64(zpool_prop_t prop, uint64_t value)
1200 spa_t *spa = ztest_spa;
1201 nvlist_t *props = NULL;
1204 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
1205 VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0);
1207 error = spa_prop_set(spa, props);
1211 if (error == ENOSPC) {
1212 ztest_record_enospc(FTAG);
1221 ztest_dmu_objset_own(const char *name, dmu_objset_type_t type,
1222 boolean_t readonly, boolean_t decrypt, void *tag, objset_t **osp)
1226 err = dmu_objset_own(name, type, readonly, decrypt, tag, osp);
1227 if (decrypt && err == EACCES) {
1228 char ddname[ZFS_MAX_DATASET_NAME_LEN];
1229 dsl_crypto_params_t *dcp;
1230 nvlist_t *crypto_args = fnvlist_alloc();
1233 /* spa_keystore_load_wkey() expects a dsl dir name */
1234 strcpy(ddname, name);
1235 cp = strchr(ddname, '@');
1239 fnvlist_add_uint8_array(crypto_args, "wkeydata",
1240 (uint8_t *)ztest_wkeydata, WRAPPING_KEY_LEN);
1241 VERIFY0(dsl_crypto_params_create_nvlist(DCP_CMD_NONE, NULL,
1242 crypto_args, &dcp));
1243 err = spa_keystore_load_wkey(ddname, dcp, B_FALSE);
1244 dsl_crypto_params_free(dcp, B_FALSE);
1245 fnvlist_free(crypto_args);
1250 err = dmu_objset_own(name, type, readonly, decrypt, tag, osp);
1258 * Object and range lock mechanics
1261 list_node_t z_lnode;
1262 refcount_t z_refcnt;
1264 zfs_rlock_t z_range_lock;
1269 ztest_znode_t *z_ztznode;
1272 static ztest_znode_t *
1273 ztest_znode_init(uint64_t object)
1275 ztest_znode_t *zp = umem_alloc(sizeof (*zp), UMEM_NOFAIL);
1277 list_link_init(&zp->z_lnode);
1278 refcount_create(&zp->z_refcnt);
1279 zp->z_object = object;
1280 zfs_rlock_init(&zp->z_range_lock);
1286 ztest_znode_fini(ztest_znode_t *zp)
1288 ASSERT(refcount_is_zero(&zp->z_refcnt));
1289 zfs_rlock_destroy(&zp->z_range_lock);
1291 refcount_destroy(&zp->z_refcnt);
1292 list_link_init(&zp->z_lnode);
1293 umem_free(zp, sizeof (*zp));
1297 ztest_zll_init(zll_t *zll)
1299 mutex_init(&zll->z_lock, NULL, MUTEX_DEFAULT, NULL);
1300 list_create(&zll->z_list, sizeof (ztest_znode_t),
1301 offsetof(ztest_znode_t, z_lnode));
1305 ztest_zll_destroy(zll_t *zll)
1307 list_destroy(&zll->z_list);
1308 mutex_destroy(&zll->z_lock);
1311 #define RL_TAG "range_lock"
1312 static ztest_znode_t *
1313 ztest_znode_get(ztest_ds_t *zd, uint64_t object)
1315 zll_t *zll = &zd->zd_range_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1316 ztest_znode_t *zp = NULL;
1317 mutex_enter(&zll->z_lock);
1318 for (zp = list_head(&zll->z_list); (zp);
1319 zp = list_next(&zll->z_list, zp)) {
1320 if (zp->z_object == object) {
1321 refcount_add(&zp->z_refcnt, RL_TAG);
1326 zp = ztest_znode_init(object);
1327 refcount_add(&zp->z_refcnt, RL_TAG);
1328 list_insert_head(&zll->z_list, zp);
1330 mutex_exit(&zll->z_lock);
1335 ztest_znode_put(ztest_ds_t *zd, ztest_znode_t *zp)
1338 ASSERT3U(zp->z_object, !=, 0);
1339 zll = &zd->zd_range_lock[zp->z_object & (ZTEST_OBJECT_LOCKS - 1)];
1340 mutex_enter(&zll->z_lock);
1341 refcount_remove(&zp->z_refcnt, RL_TAG);
1342 if (refcount_is_zero(&zp->z_refcnt)) {
1343 list_remove(&zll->z_list, zp);
1344 ztest_znode_fini(zp);
1346 mutex_exit(&zll->z_lock);
1351 ztest_rll_init(rll_t *rll)
1353 rll->rll_writer = NULL;
1354 rll->rll_readers = 0;
1355 mutex_init(&rll->rll_lock, NULL, MUTEX_DEFAULT, NULL);
1356 cv_init(&rll->rll_cv, NULL, CV_DEFAULT, NULL);
1360 ztest_rll_destroy(rll_t *rll)
1362 ASSERT(rll->rll_writer == NULL);
1363 ASSERT(rll->rll_readers == 0);
1364 mutex_destroy(&rll->rll_lock);
1365 cv_destroy(&rll->rll_cv);
1369 ztest_rll_lock(rll_t *rll, rl_type_t type)
1371 mutex_enter(&rll->rll_lock);
1373 if (type == RL_READER) {
1374 while (rll->rll_writer != NULL)
1375 (void) cv_wait(&rll->rll_cv, &rll->rll_lock);
1378 while (rll->rll_writer != NULL || rll->rll_readers)
1379 (void) cv_wait(&rll->rll_cv, &rll->rll_lock);
1380 rll->rll_writer = curthread;
1383 mutex_exit(&rll->rll_lock);
1387 ztest_rll_unlock(rll_t *rll)
1389 mutex_enter(&rll->rll_lock);
1391 if (rll->rll_writer) {
1392 ASSERT(rll->rll_readers == 0);
1393 rll->rll_writer = NULL;
1395 ASSERT(rll->rll_readers != 0);
1396 ASSERT(rll->rll_writer == NULL);
1400 if (rll->rll_writer == NULL && rll->rll_readers == 0)
1401 cv_broadcast(&rll->rll_cv);
1403 mutex_exit(&rll->rll_lock);
1407 ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type)
1409 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1411 ztest_rll_lock(rll, type);
1415 ztest_object_unlock(ztest_ds_t *zd, uint64_t object)
1417 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1419 ztest_rll_unlock(rll);
1422 static ztest_zrl_t *
1423 ztest_zrl_init(rl_t *rl, ztest_znode_t *zp)
1425 ztest_zrl_t *zrl = umem_alloc(sizeof (*zrl), UMEM_NOFAIL);
1427 zrl->z_ztznode = zp;
1432 ztest_zrl_fini(ztest_zrl_t *zrl)
1434 umem_free(zrl, sizeof (*zrl));
1437 static ztest_zrl_t *
1438 ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset,
1439 uint64_t size, rl_type_t type)
1441 ztest_znode_t *zp = ztest_znode_get(zd, object);
1442 rl_t *rl = zfs_range_lock(&zp->z_range_lock, offset,
1444 return (ztest_zrl_init(rl, zp));
1448 ztest_range_unlock(ztest_ds_t *zd, ztest_zrl_t *zrl)
1450 zfs_range_unlock(zrl->z_rl);
1451 ztest_znode_put(zd, zrl->z_ztznode);
1452 ztest_zrl_fini(zrl);
1456 ztest_zd_init(ztest_ds_t *zd, ztest_shared_ds_t *szd, objset_t *os)
1459 zd->zd_zilog = dmu_objset_zil(os);
1460 zd->zd_shared = szd;
1461 dmu_objset_name(os, zd->zd_name);
1464 if (zd->zd_shared != NULL)
1465 zd->zd_shared->zd_seq = 0;
1467 VERIFY0(pthread_rwlock_init(&zd->zd_zilog_lock, NULL));
1468 mutex_init(&zd->zd_dirobj_lock, NULL, MUTEX_DEFAULT, NULL);
1470 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1471 ztest_rll_init(&zd->zd_object_lock[l]);
1473 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
1474 ztest_zll_init(&zd->zd_range_lock[l]);
1478 ztest_zd_fini(ztest_ds_t *zd)
1482 mutex_destroy(&zd->zd_dirobj_lock);
1483 (void) pthread_rwlock_destroy(&zd->zd_zilog_lock);
1485 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1486 ztest_rll_destroy(&zd->zd_object_lock[l]);
1488 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
1489 ztest_zll_destroy(&zd->zd_range_lock[l]);
1492 #define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1495 ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag)
1501 * Attempt to assign tx to some transaction group.
1503 error = dmu_tx_assign(tx, txg_how);
1505 if (error == ERESTART) {
1506 ASSERT(txg_how == TXG_NOWAIT);
1509 ASSERT3U(error, ==, ENOSPC);
1510 ztest_record_enospc(tag);
1515 txg = dmu_tx_get_txg(tx);
1521 ztest_pattern_set(void *buf, uint64_t size, uint64_t value)
1524 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1532 ztest_pattern_match(void *buf, uint64_t size, uint64_t value)
1535 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1539 diff |= (value - *ip++);
1546 ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1547 uint64_t dnodesize, uint64_t offset, uint64_t gen, uint64_t txg,
1550 bt->bt_magic = BT_MAGIC;
1551 bt->bt_objset = dmu_objset_id(os);
1552 bt->bt_object = object;
1553 bt->bt_dnodesize = dnodesize;
1554 bt->bt_offset = offset;
1557 bt->bt_crtxg = crtxg;
1561 ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1562 uint64_t dnodesize, uint64_t offset, uint64_t gen, uint64_t txg,
1565 ASSERT3U(bt->bt_magic, ==, BT_MAGIC);
1566 ASSERT3U(bt->bt_objset, ==, dmu_objset_id(os));
1567 ASSERT3U(bt->bt_object, ==, object);
1568 ASSERT3U(bt->bt_dnodesize, ==, dnodesize);
1569 ASSERT3U(bt->bt_offset, ==, offset);
1570 ASSERT3U(bt->bt_gen, <=, gen);
1571 ASSERT3U(bt->bt_txg, <=, txg);
1572 ASSERT3U(bt->bt_crtxg, ==, crtxg);
1575 static ztest_block_tag_t *
1576 ztest_bt_bonus(dmu_buf_t *db)
1578 dmu_object_info_t doi;
1579 ztest_block_tag_t *bt;
1581 dmu_object_info_from_db(db, &doi);
1582 ASSERT3U(doi.doi_bonus_size, <=, db->db_size);
1583 ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt));
1584 bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt));
1590 * Generate a token to fill up unused bonus buffer space. Try to make
1591 * it unique to the object, generation, and offset to verify that data
1592 * is not getting overwritten by data from other dnodes.
1594 #define ZTEST_BONUS_FILL_TOKEN(obj, ds, gen, offset) \
1595 (((ds) << 48) | ((gen) << 32) | ((obj) << 8) | (offset))
1598 * Fill up the unused bonus buffer region before the block tag with a
1599 * verifiable pattern. Filling the whole bonus area with non-zero data
1600 * helps ensure that all dnode traversal code properly skips the
1601 * interior regions of large dnodes.
1604 ztest_fill_unused_bonus(dmu_buf_t *db, void *end, uint64_t obj,
1605 objset_t *os, uint64_t gen)
1609 ASSERT(IS_P2ALIGNED((char *)end - (char *)db->db_data, 8));
1611 for (bonusp = db->db_data; bonusp < (uint64_t *)end; bonusp++) {
1612 uint64_t token = ZTEST_BONUS_FILL_TOKEN(obj, dmu_objset_id(os),
1613 gen, bonusp - (uint64_t *)db->db_data);
1619 * Verify that the unused area of a bonus buffer is filled with the
1623 ztest_verify_unused_bonus(dmu_buf_t *db, void *end, uint64_t obj,
1624 objset_t *os, uint64_t gen)
1628 for (bonusp = db->db_data; bonusp < (uint64_t *)end; bonusp++) {
1629 uint64_t token = ZTEST_BONUS_FILL_TOKEN(obj, dmu_objset_id(os),
1630 gen, bonusp - (uint64_t *)db->db_data);
1631 VERIFY3U(*bonusp, ==, token);
1639 #define lrz_type lr_mode
1640 #define lrz_blocksize lr_uid
1641 #define lrz_ibshift lr_gid
1642 #define lrz_bonustype lr_rdev
1643 #define lrz_dnodesize lr_crtime[1]
1646 ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr)
1648 char *name = (void *)(lr + 1); /* name follows lr */
1649 size_t namesize = strlen(name) + 1;
1652 if (zil_replaying(zd->zd_zilog, tx))
1655 itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize);
1656 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1657 sizeof (*lr) + namesize - sizeof (lr_t));
1659 zil_itx_assign(zd->zd_zilog, itx, tx);
1663 ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr, uint64_t object)
1665 char *name = (void *)(lr + 1); /* name follows lr */
1666 size_t namesize = strlen(name) + 1;
1669 if (zil_replaying(zd->zd_zilog, tx))
1672 itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize);
1673 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1674 sizeof (*lr) + namesize - sizeof (lr_t));
1676 itx->itx_oid = object;
1677 zil_itx_assign(zd->zd_zilog, itx, tx);
1681 ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr)
1684 itx_wr_state_t write_state = ztest_random(WR_NUM_STATES);
1686 if (zil_replaying(zd->zd_zilog, tx))
1689 if (lr->lr_length > ZIL_MAX_LOG_DATA)
1690 write_state = WR_INDIRECT;
1692 itx = zil_itx_create(TX_WRITE,
1693 sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0));
1695 if (write_state == WR_COPIED &&
1696 dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length,
1697 ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) {
1698 zil_itx_destroy(itx);
1699 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1700 write_state = WR_NEED_COPY;
1702 itx->itx_private = zd;
1703 itx->itx_wr_state = write_state;
1704 itx->itx_sync = (ztest_random(8) == 0);
1706 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1707 sizeof (*lr) - sizeof (lr_t));
1709 zil_itx_assign(zd->zd_zilog, itx, tx);
1713 ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr)
1717 if (zil_replaying(zd->zd_zilog, tx))
1720 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1721 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1722 sizeof (*lr) - sizeof (lr_t));
1724 itx->itx_sync = B_FALSE;
1725 zil_itx_assign(zd->zd_zilog, itx, tx);
1729 ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr)
1733 if (zil_replaying(zd->zd_zilog, tx))
1736 itx = zil_itx_create(TX_SETATTR, sizeof (*lr));
1737 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1738 sizeof (*lr) - sizeof (lr_t));
1740 itx->itx_sync = B_FALSE;
1741 zil_itx_assign(zd->zd_zilog, itx, tx);
1748 ztest_replay_create(void *arg1, void *arg2, boolean_t byteswap)
1750 ztest_ds_t *zd = arg1;
1751 lr_create_t *lr = arg2;
1752 char *name = (void *)(lr + 1); /* name follows lr */
1753 objset_t *os = zd->zd_os;
1754 ztest_block_tag_t *bbt;
1762 byteswap_uint64_array(lr, sizeof (*lr));
1764 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1765 ASSERT(name[0] != '\0');
1767 tx = dmu_tx_create(os);
1769 dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name);
1771 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1772 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1774 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1777 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1781 ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid);
1782 bonuslen = DN_BONUS_SIZE(lr->lrz_dnodesize);
1784 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1785 if (lr->lr_foid == 0) {
1786 lr->lr_foid = zap_create_dnsize(os,
1787 lr->lrz_type, lr->lrz_bonustype,
1788 bonuslen, lr->lrz_dnodesize, tx);
1790 error = zap_create_claim_dnsize(os, lr->lr_foid,
1791 lr->lrz_type, lr->lrz_bonustype,
1792 bonuslen, lr->lrz_dnodesize, tx);
1795 if (lr->lr_foid == 0) {
1796 lr->lr_foid = dmu_object_alloc_dnsize(os,
1797 lr->lrz_type, 0, lr->lrz_bonustype,
1798 bonuslen, lr->lrz_dnodesize, tx);
1800 error = dmu_object_claim_dnsize(os, lr->lr_foid,
1801 lr->lrz_type, 0, lr->lrz_bonustype,
1802 bonuslen, lr->lrz_dnodesize, tx);
1807 ASSERT3U(error, ==, EEXIST);
1808 ASSERT(zd->zd_zilog->zl_replay);
1813 ASSERT(lr->lr_foid != 0);
1815 if (lr->lrz_type != DMU_OT_ZAP_OTHER)
1816 VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid,
1817 lr->lrz_blocksize, lr->lrz_ibshift, tx));
1819 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1820 bbt = ztest_bt_bonus(db);
1821 dmu_buf_will_dirty(db, tx);
1822 ztest_bt_generate(bbt, os, lr->lr_foid, lr->lrz_dnodesize, -1ULL,
1823 lr->lr_gen, txg, txg);
1824 ztest_fill_unused_bonus(db, bbt, lr->lr_foid, os, lr->lr_gen);
1825 dmu_buf_rele(db, FTAG);
1827 VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1,
1830 (void) ztest_log_create(zd, tx, lr);
1838 ztest_replay_remove(void *arg1, void *arg2, boolean_t byteswap)
1840 ztest_ds_t *zd = arg1;
1841 lr_remove_t *lr = arg2;
1842 char *name = (void *)(lr + 1); /* name follows lr */
1843 objset_t *os = zd->zd_os;
1844 dmu_object_info_t doi;
1846 uint64_t object, txg;
1849 byteswap_uint64_array(lr, sizeof (*lr));
1851 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1852 ASSERT(name[0] != '\0');
1855 zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object));
1856 ASSERT(object != 0);
1858 ztest_object_lock(zd, object, RL_WRITER);
1860 VERIFY3U(0, ==, dmu_object_info(os, object, &doi));
1862 tx = dmu_tx_create(os);
1864 dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name);
1865 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1867 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1869 ztest_object_unlock(zd, object);
1873 if (doi.doi_type == DMU_OT_ZAP_OTHER) {
1874 VERIFY3U(0, ==, zap_destroy(os, object, tx));
1876 VERIFY3U(0, ==, dmu_object_free(os, object, tx));
1879 VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx));
1881 (void) ztest_log_remove(zd, tx, lr, object);
1885 ztest_object_unlock(zd, object);
1891 ztest_replay_write(void *arg1, void *arg2, boolean_t byteswap)
1893 ztest_ds_t *zd = arg1;
1894 lr_write_t *lr = arg2;
1895 objset_t *os = zd->zd_os;
1896 void *data = lr + 1; /* data follows lr */
1897 uint64_t offset, length;
1898 ztest_block_tag_t *bt = data;
1899 ztest_block_tag_t *bbt;
1900 uint64_t gen, txg, lrtxg, crtxg;
1901 dmu_object_info_t doi;
1904 arc_buf_t *abuf = NULL;
1908 byteswap_uint64_array(lr, sizeof (*lr));
1910 offset = lr->lr_offset;
1911 length = lr->lr_length;
1913 /* If it's a dmu_sync() block, write the whole block */
1914 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
1915 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
1916 if (length < blocksize) {
1917 offset -= offset % blocksize;
1922 if (bt->bt_magic == BSWAP_64(BT_MAGIC))
1923 byteswap_uint64_array(bt, sizeof (*bt));
1925 if (bt->bt_magic != BT_MAGIC)
1928 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1929 rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER);
1931 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1933 dmu_object_info_from_db(db, &doi);
1935 bbt = ztest_bt_bonus(db);
1936 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1938 crtxg = bbt->bt_crtxg;
1939 lrtxg = lr->lr_common.lrc_txg;
1941 tx = dmu_tx_create(os);
1943 dmu_tx_hold_write(tx, lr->lr_foid, offset, length);
1945 if (ztest_random(8) == 0 && length == doi.doi_data_block_size &&
1946 P2PHASE(offset, length) == 0)
1947 abuf = dmu_request_arcbuf(db, length);
1949 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1952 dmu_return_arcbuf(abuf);
1953 dmu_buf_rele(db, FTAG);
1954 ztest_range_unlock(zd, rl);
1955 ztest_object_unlock(zd, lr->lr_foid);
1961 * Usually, verify the old data before writing new data --
1962 * but not always, because we also want to verify correct
1963 * behavior when the data was not recently read into cache.
1965 ASSERT(offset % doi.doi_data_block_size == 0);
1966 if (ztest_random(4) != 0) {
1967 int prefetch = ztest_random(2) ?
1968 DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
1969 ztest_block_tag_t rbt;
1971 VERIFY(dmu_read(os, lr->lr_foid, offset,
1972 sizeof (rbt), &rbt, prefetch) == 0);
1973 if (rbt.bt_magic == BT_MAGIC) {
1974 ztest_bt_verify(&rbt, os, lr->lr_foid, 0,
1975 offset, gen, txg, crtxg);
1980 * Writes can appear to be newer than the bonus buffer because
1981 * the ztest_get_data() callback does a dmu_read() of the
1982 * open-context data, which may be different than the data
1983 * as it was when the write was generated.
1985 if (zd->zd_zilog->zl_replay) {
1986 ztest_bt_verify(bt, os, lr->lr_foid, 0, offset,
1987 MAX(gen, bt->bt_gen), MAX(txg, lrtxg),
1992 * Set the bt's gen/txg to the bonus buffer's gen/txg
1993 * so that all of the usual ASSERTs will work.
1995 ztest_bt_generate(bt, os, lr->lr_foid, 0, offset, gen, txg,
2000 dmu_write(os, lr->lr_foid, offset, length, data, tx);
2002 bcopy(data, abuf->b_data, length);
2003 dmu_assign_arcbuf_by_dbuf(db, offset, abuf, tx);
2006 (void) ztest_log_write(zd, tx, lr);
2008 dmu_buf_rele(db, FTAG);
2012 ztest_range_unlock(zd, rl);
2013 ztest_object_unlock(zd, lr->lr_foid);
2019 ztest_replay_truncate(void *arg1, void *arg2, boolean_t byteswap)
2021 ztest_ds_t *zd = arg1;
2022 lr_truncate_t *lr = arg2;
2023 objset_t *os = zd->zd_os;
2029 byteswap_uint64_array(lr, sizeof (*lr));
2031 ztest_object_lock(zd, lr->lr_foid, RL_READER);
2032 rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length,
2035 tx = dmu_tx_create(os);
2037 dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length);
2039 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
2041 ztest_range_unlock(zd, rl);
2042 ztest_object_unlock(zd, lr->lr_foid);
2046 VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset,
2047 lr->lr_length, tx) == 0);
2049 (void) ztest_log_truncate(zd, tx, lr);
2053 ztest_range_unlock(zd, rl);
2054 ztest_object_unlock(zd, lr->lr_foid);
2060 ztest_replay_setattr(void *arg1, void *arg2, boolean_t byteswap)
2062 ztest_ds_t *zd = arg1;
2063 lr_setattr_t *lr = arg2;
2064 objset_t *os = zd->zd_os;
2067 ztest_block_tag_t *bbt;
2068 uint64_t txg, lrtxg, crtxg, dnodesize;
2071 byteswap_uint64_array(lr, sizeof (*lr));
2073 ztest_object_lock(zd, lr->lr_foid, RL_WRITER);
2075 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
2077 tx = dmu_tx_create(os);
2078 dmu_tx_hold_bonus(tx, lr->lr_foid);
2080 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
2082 dmu_buf_rele(db, FTAG);
2083 ztest_object_unlock(zd, lr->lr_foid);
2087 bbt = ztest_bt_bonus(db);
2088 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
2089 crtxg = bbt->bt_crtxg;
2090 lrtxg = lr->lr_common.lrc_txg;
2091 dnodesize = bbt->bt_dnodesize;
2093 if (zd->zd_zilog->zl_replay) {
2094 ASSERT(lr->lr_size != 0);
2095 ASSERT(lr->lr_mode != 0);
2099 * Randomly change the size and increment the generation.
2101 lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) *
2103 lr->lr_mode = bbt->bt_gen + 1;
2108 * Verify that the current bonus buffer is not newer than our txg.
2110 ztest_bt_verify(bbt, os, lr->lr_foid, dnodesize, -1ULL, lr->lr_mode,
2111 MAX(txg, lrtxg), crtxg);
2113 dmu_buf_will_dirty(db, tx);
2115 ASSERT3U(lr->lr_size, >=, sizeof (*bbt));
2116 ASSERT3U(lr->lr_size, <=, db->db_size);
2117 VERIFY0(dmu_set_bonus(db, lr->lr_size, tx));
2118 bbt = ztest_bt_bonus(db);
2120 ztest_bt_generate(bbt, os, lr->lr_foid, dnodesize, -1ULL, lr->lr_mode,
2122 ztest_fill_unused_bonus(db, bbt, lr->lr_foid, os, bbt->bt_gen);
2123 dmu_buf_rele(db, FTAG);
2125 (void) ztest_log_setattr(zd, tx, lr);
2129 ztest_object_unlock(zd, lr->lr_foid);
2134 zil_replay_func_t *ztest_replay_vector[TX_MAX_TYPE] = {
2135 NULL, /* 0 no such transaction type */
2136 ztest_replay_create, /* TX_CREATE */
2137 NULL, /* TX_MKDIR */
2138 NULL, /* TX_MKXATTR */
2139 NULL, /* TX_SYMLINK */
2140 ztest_replay_remove, /* TX_REMOVE */
2141 NULL, /* TX_RMDIR */
2143 NULL, /* TX_RENAME */
2144 ztest_replay_write, /* TX_WRITE */
2145 ztest_replay_truncate, /* TX_TRUNCATE */
2146 ztest_replay_setattr, /* TX_SETATTR */
2148 NULL, /* TX_CREATE_ACL */
2149 NULL, /* TX_CREATE_ATTR */
2150 NULL, /* TX_CREATE_ACL_ATTR */
2151 NULL, /* TX_MKDIR_ACL */
2152 NULL, /* TX_MKDIR_ATTR */
2153 NULL, /* TX_MKDIR_ACL_ATTR */
2154 NULL, /* TX_WRITE2 */
2158 * ZIL get_data callbacks
2160 typedef struct ztest_zgd_private {
2164 } ztest_zgd_private_t;
2167 ztest_get_done(zgd_t *zgd, int error)
2169 ztest_zgd_private_t *zzp = zgd->zgd_private;
2170 ztest_ds_t *zd = zzp->z_zd;
2171 uint64_t object = zzp->z_object;
2174 dmu_buf_rele(zgd->zgd_db, zgd);
2176 ztest_range_unlock(zd, zzp->z_rl);
2177 ztest_object_unlock(zd, object);
2179 if (error == 0 && zgd->zgd_bp)
2180 zil_lwb_add_block(zgd->zgd_lwb, zgd->zgd_bp);
2182 umem_free(zgd, sizeof (*zgd));
2183 umem_free(zzp, sizeof (*zzp));
2187 ztest_get_data(void *arg, lr_write_t *lr, char *buf, struct lwb *lwb,
2190 ztest_ds_t *zd = arg;
2191 objset_t *os = zd->zd_os;
2192 uint64_t object = lr->lr_foid;
2193 uint64_t offset = lr->lr_offset;
2194 uint64_t size = lr->lr_length;
2195 uint64_t txg = lr->lr_common.lrc_txg;
2197 dmu_object_info_t doi;
2201 ztest_zgd_private_t *zgd_private;
2203 ASSERT3P(lwb, !=, NULL);
2204 ASSERT3P(zio, !=, NULL);
2205 ASSERT3U(size, !=, 0);
2207 ztest_object_lock(zd, object, RL_READER);
2208 error = dmu_bonus_hold(os, object, FTAG, &db);
2210 ztest_object_unlock(zd, object);
2214 crtxg = ztest_bt_bonus(db)->bt_crtxg;
2216 if (crtxg == 0 || crtxg > txg) {
2217 dmu_buf_rele(db, FTAG);
2218 ztest_object_unlock(zd, object);
2222 dmu_object_info_from_db(db, &doi);
2223 dmu_buf_rele(db, FTAG);
2226 zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL);
2228 zgd_private = umem_zalloc(sizeof (ztest_zgd_private_t), UMEM_NOFAIL);
2229 zgd_private->z_zd = zd;
2230 zgd_private->z_object = object;
2231 zgd->zgd_private = zgd_private;
2233 if (buf != NULL) { /* immediate write */
2234 zgd_private->z_rl = ztest_range_lock(zd, object, offset, size,
2236 zgd->zgd_rl = zgd_private->z_rl->z_rl;
2238 error = dmu_read(os, object, offset, size, buf,
2239 DMU_READ_NO_PREFETCH);
2242 size = doi.doi_data_block_size;
2244 offset = P2ALIGN(offset, size);
2246 ASSERT(offset < size);
2250 zgd_private->z_rl = ztest_range_lock(zd, object, offset, size,
2252 zgd->zgd_rl = zgd_private->z_rl->z_rl;
2254 error = dmu_buf_hold(os, object, offset, zgd, &db,
2255 DMU_READ_NO_PREFETCH);
2258 blkptr_t *bp = &lr->lr_blkptr;
2263 ASSERT(db->db_offset == offset);
2264 ASSERT(db->db_size == size);
2266 error = dmu_sync(zio, lr->lr_common.lrc_txg,
2267 ztest_get_done, zgd);
2274 ztest_get_done(zgd, error);
2280 ztest_lr_alloc(size_t lrsize, char *name)
2283 size_t namesize = name ? strlen(name) + 1 : 0;
2285 lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
2288 bcopy(name, lr + lrsize, namesize);
2294 ztest_lr_free(void *lr, size_t lrsize, char *name)
2296 size_t namesize = name ? strlen(name) + 1 : 0;
2298 umem_free(lr, lrsize + namesize);
2302 * Lookup a bunch of objects. Returns the number of objects not found.
2305 ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
2311 ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
2313 for (i = 0; i < count; i++, od++) {
2315 error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
2316 sizeof (uint64_t), 1, &od->od_object);
2318 ASSERT(error == ENOENT);
2319 ASSERT(od->od_object == 0);
2323 ztest_block_tag_t *bbt;
2324 dmu_object_info_t doi;
2326 ASSERT(od->od_object != 0);
2327 ASSERT(missing == 0); /* there should be no gaps */
2329 ztest_object_lock(zd, od->od_object, RL_READER);
2330 VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
2331 od->od_object, FTAG, &db));
2332 dmu_object_info_from_db(db, &doi);
2333 bbt = ztest_bt_bonus(db);
2334 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
2335 od->od_type = doi.doi_type;
2336 od->od_blocksize = doi.doi_data_block_size;
2337 od->od_gen = bbt->bt_gen;
2338 dmu_buf_rele(db, FTAG);
2339 ztest_object_unlock(zd, od->od_object);
2347 ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
2352 ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
2354 for (i = 0; i < count; i++, od++) {
2361 lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2363 lr->lr_doid = od->od_dir;
2364 lr->lr_foid = 0; /* 0 to allocate, > 0 to claim */
2365 lr->lrz_type = od->od_crtype;
2366 lr->lrz_blocksize = od->od_crblocksize;
2367 lr->lrz_ibshift = ztest_random_ibshift();
2368 lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
2369 lr->lrz_dnodesize = od->od_crdnodesize;
2370 lr->lr_gen = od->od_crgen;
2371 lr->lr_crtime[0] = time(NULL);
2373 if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
2374 ASSERT(missing == 0);
2378 od->od_object = lr->lr_foid;
2379 od->od_type = od->od_crtype;
2380 od->od_blocksize = od->od_crblocksize;
2381 od->od_gen = od->od_crgen;
2382 ASSERT(od->od_object != 0);
2385 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2392 ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
2398 ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
2402 for (i = count - 1; i >= 0; i--, od--) {
2409 * No object was found.
2411 if (od->od_object == 0)
2414 lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2416 lr->lr_doid = od->od_dir;
2418 if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
2419 ASSERT3U(error, ==, ENOSPC);
2424 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2431 ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
2437 lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
2439 lr->lr_foid = object;
2440 lr->lr_offset = offset;
2441 lr->lr_length = size;
2443 BP_ZERO(&lr->lr_blkptr);
2445 bcopy(data, lr + 1, size);
2447 error = ztest_replay_write(zd, lr, B_FALSE);
2449 ztest_lr_free(lr, sizeof (*lr) + size, NULL);
2455 ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2460 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2462 lr->lr_foid = object;
2463 lr->lr_offset = offset;
2464 lr->lr_length = size;
2466 error = ztest_replay_truncate(zd, lr, B_FALSE);
2468 ztest_lr_free(lr, sizeof (*lr), NULL);
2474 ztest_setattr(ztest_ds_t *zd, uint64_t object)
2479 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2481 lr->lr_foid = object;
2485 error = ztest_replay_setattr(zd, lr, B_FALSE);
2487 ztest_lr_free(lr, sizeof (*lr), NULL);
2493 ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2495 objset_t *os = zd->zd_os;
2500 txg_wait_synced(dmu_objset_pool(os), 0);
2502 ztest_object_lock(zd, object, RL_READER);
2503 rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
2505 tx = dmu_tx_create(os);
2507 dmu_tx_hold_write(tx, object, offset, size);
2509 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
2512 dmu_prealloc(os, object, offset, size, tx);
2514 txg_wait_synced(dmu_objset_pool(os), txg);
2516 (void) dmu_free_long_range(os, object, offset, size);
2519 ztest_range_unlock(zd, rl);
2520 ztest_object_unlock(zd, object);
2524 ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
2527 ztest_block_tag_t wbt;
2528 dmu_object_info_t doi;
2529 enum ztest_io_type io_type;
2533 VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
2534 blocksize = doi.doi_data_block_size;
2535 data = umem_alloc(blocksize, UMEM_NOFAIL);
2538 * Pick an i/o type at random, biased toward writing block tags.
2540 io_type = ztest_random(ZTEST_IO_TYPES);
2541 if (ztest_random(2) == 0)
2542 io_type = ZTEST_IO_WRITE_TAG;
2544 (void) pthread_rwlock_rdlock(&zd->zd_zilog_lock);
2548 case ZTEST_IO_WRITE_TAG:
2549 ztest_bt_generate(&wbt, zd->zd_os, object, doi.doi_dnodesize,
2551 (void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
2554 case ZTEST_IO_WRITE_PATTERN:
2555 (void) memset(data, 'a' + (object + offset) % 5, blocksize);
2556 if (ztest_random(2) == 0) {
2558 * Induce fletcher2 collisions to ensure that
2559 * zio_ddt_collision() detects and resolves them
2560 * when using fletcher2-verify for deduplication.
2562 ((uint64_t *)data)[0] ^= 1ULL << 63;
2563 ((uint64_t *)data)[4] ^= 1ULL << 63;
2565 (void) ztest_write(zd, object, offset, blocksize, data);
2568 case ZTEST_IO_WRITE_ZEROES:
2569 bzero(data, blocksize);
2570 (void) ztest_write(zd, object, offset, blocksize, data);
2573 case ZTEST_IO_TRUNCATE:
2574 (void) ztest_truncate(zd, object, offset, blocksize);
2577 case ZTEST_IO_SETATTR:
2578 (void) ztest_setattr(zd, object);
2583 case ZTEST_IO_REWRITE:
2584 (void) pthread_rwlock_rdlock(&ztest_name_lock);
2585 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2586 ZFS_PROP_CHECKSUM, spa_dedup_checksum(ztest_spa),
2588 VERIFY(err == 0 || err == ENOSPC);
2589 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2590 ZFS_PROP_COMPRESSION,
2591 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION),
2593 VERIFY(err == 0 || err == ENOSPC);
2594 (void) pthread_rwlock_unlock(&ztest_name_lock);
2596 VERIFY0(dmu_read(zd->zd_os, object, offset, blocksize, data,
2597 DMU_READ_NO_PREFETCH));
2599 (void) ztest_write(zd, object, offset, blocksize, data);
2603 (void) pthread_rwlock_unlock(&zd->zd_zilog_lock);
2605 umem_free(data, blocksize);
2609 * Initialize an object description template.
2612 ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
2613 dmu_object_type_t type, uint64_t blocksize, uint64_t dnodesize,
2616 od->od_dir = ZTEST_DIROBJ;
2619 od->od_crtype = type;
2620 od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
2621 od->od_crdnodesize = dnodesize ? dnodesize : ztest_random_dnodesize();
2624 od->od_type = DMU_OT_NONE;
2625 od->od_blocksize = 0;
2628 (void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
2629 tag, (longlong_t)id, (u_longlong_t)index);
2633 * Lookup or create the objects for a test using the od template.
2634 * If the objects do not all exist, or if 'remove' is specified,
2635 * remove any existing objects and create new ones. Otherwise,
2636 * use the existing objects.
2639 ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
2641 int count = size / sizeof (*od);
2644 mutex_enter(&zd->zd_dirobj_lock);
2645 if ((ztest_lookup(zd, od, count) != 0 || remove) &&
2646 (ztest_remove(zd, od, count) != 0 ||
2647 ztest_create(zd, od, count) != 0))
2650 mutex_exit(&zd->zd_dirobj_lock);
2657 ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
2659 zilog_t *zilog = zd->zd_zilog;
2661 (void) pthread_rwlock_rdlock(&zd->zd_zilog_lock);
2663 zil_commit(zilog, ztest_random(ZTEST_OBJECTS));
2666 * Remember the committed values in zd, which is in parent/child
2667 * shared memory. If we die, the next iteration of ztest_run()
2668 * will verify that the log really does contain this record.
2670 mutex_enter(&zilog->zl_lock);
2671 ASSERT(zd->zd_shared != NULL);
2672 ASSERT3U(zd->zd_shared->zd_seq, <=, zilog->zl_commit_lr_seq);
2673 zd->zd_shared->zd_seq = zilog->zl_commit_lr_seq;
2674 mutex_exit(&zilog->zl_lock);
2676 (void) pthread_rwlock_unlock(&zd->zd_zilog_lock);
2680 * This function is designed to simulate the operations that occur during a
2681 * mount/unmount operation. We hold the dataset across these operations in an
2682 * attempt to expose any implicit assumptions about ZIL management.
2686 ztest_zil_remount(ztest_ds_t *zd, uint64_t id)
2688 objset_t *os = zd->zd_os;
2691 * We grab the zd_dirobj_lock to ensure that no other thread is
2692 * updating the zil (i.e. adding in-memory log records) and the
2693 * zd_zilog_lock to block any I/O.
2695 mutex_enter(&zd->zd_dirobj_lock);
2696 (void) pthread_rwlock_wrlock(&zd->zd_zilog_lock);
2698 /* zfsvfs_teardown() */
2699 zil_close(zd->zd_zilog);
2701 /* zfsvfs_setup() */
2702 VERIFY(zil_open(os, ztest_get_data) == zd->zd_zilog);
2703 zil_replay(os, zd, ztest_replay_vector);
2705 (void) pthread_rwlock_unlock(&zd->zd_zilog_lock);
2706 mutex_exit(&zd->zd_dirobj_lock);
2710 * Verify that we can't destroy an active pool, create an existing pool,
2711 * or create a pool with a bad vdev spec.
2715 ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2717 ztest_shared_opts_t *zo = &ztest_opts;
2721 if (zo->zo_mmp_test)
2725 * Attempt to create using a bad file.
2727 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2728 VERIFY3U(ENOENT, ==,
2729 spa_create("ztest_bad_file", nvroot, NULL, NULL, NULL));
2730 nvlist_free(nvroot);
2733 * Attempt to create using a bad mirror.
2735 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 2, 1);
2736 VERIFY3U(ENOENT, ==,
2737 spa_create("ztest_bad_mirror", nvroot, NULL, NULL, NULL));
2738 nvlist_free(nvroot);
2741 * Attempt to create an existing pool. It shouldn't matter
2742 * what's in the nvroot; we should fail with EEXIST.
2744 (void) pthread_rwlock_rdlock(&ztest_name_lock);
2745 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2746 VERIFY3U(EEXIST, ==,
2747 spa_create(zo->zo_pool, nvroot, NULL, NULL, NULL));
2748 nvlist_free(nvroot);
2749 VERIFY3U(0, ==, spa_open(zo->zo_pool, &spa, FTAG));
2750 VERIFY3U(EBUSY, ==, spa_destroy(zo->zo_pool));
2751 spa_close(spa, FTAG);
2753 (void) pthread_rwlock_unlock(&ztest_name_lock);
2757 * Start and then stop the MMP threads to ensure the startup and shutdown code
2758 * works properly. Actual protection and property-related code tested via ZTS.
2762 ztest_mmp_enable_disable(ztest_ds_t *zd, uint64_t id)
2764 ztest_shared_opts_t *zo = &ztest_opts;
2765 spa_t *spa = ztest_spa;
2767 if (zo->zo_mmp_test)
2770 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2771 mutex_enter(&spa->spa_props_lock);
2773 if (!spa_multihost(spa)) {
2774 spa->spa_multihost = B_TRUE;
2775 mmp_thread_start(spa);
2778 mutex_exit(&spa->spa_props_lock);
2779 spa_config_exit(spa, SCL_CONFIG, FTAG);
2781 txg_wait_synced(spa_get_dsl(spa), 0);
2782 mmp_signal_all_threads();
2783 txg_wait_synced(spa_get_dsl(spa), 0);
2785 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2786 mutex_enter(&spa->spa_props_lock);
2788 if (spa_multihost(spa)) {
2789 mmp_thread_stop(spa);
2790 spa->spa_multihost = B_FALSE;
2793 mutex_exit(&spa->spa_props_lock);
2794 spa_config_exit(spa, SCL_CONFIG, FTAG);
2799 ztest_spa_upgrade(ztest_ds_t *zd, uint64_t id)
2802 uint64_t initial_version = SPA_VERSION_INITIAL;
2803 uint64_t version, newversion;
2804 nvlist_t *nvroot, *props;
2807 if (ztest_opts.zo_mmp_test)
2810 mutex_enter(&ztest_vdev_lock);
2811 name = kmem_asprintf("%s_upgrade", ztest_opts.zo_pool);
2814 * Clean up from previous runs.
2816 (void) spa_destroy(name);
2818 nvroot = make_vdev_root(NULL, NULL, name, ztest_opts.zo_vdev_size, 0,
2819 0, ztest_opts.zo_raidz, ztest_opts.zo_mirrors, 1);
2822 * If we're configuring a RAIDZ device then make sure that the
2823 * the initial version is capable of supporting that feature.
2825 switch (ztest_opts.zo_raidz_parity) {
2828 initial_version = SPA_VERSION_INITIAL;
2831 initial_version = SPA_VERSION_RAIDZ2;
2834 initial_version = SPA_VERSION_RAIDZ3;
2839 * Create a pool with a spa version that can be upgraded. Pick
2840 * a value between initial_version and SPA_VERSION_BEFORE_FEATURES.
2843 version = ztest_random_spa_version(initial_version);
2844 } while (version > SPA_VERSION_BEFORE_FEATURES);
2846 props = fnvlist_alloc();
2847 fnvlist_add_uint64(props,
2848 zpool_prop_to_name(ZPOOL_PROP_VERSION), version);
2849 VERIFY3S(spa_create(name, nvroot, props, NULL, NULL), ==, 0);
2850 fnvlist_free(nvroot);
2851 fnvlist_free(props);
2853 VERIFY3S(spa_open(name, &spa, FTAG), ==, 0);
2854 VERIFY3U(spa_version(spa), ==, version);
2855 newversion = ztest_random_spa_version(version + 1);
2857 if (ztest_opts.zo_verbose >= 4) {
2858 (void) printf("upgrading spa version from %llu to %llu\n",
2859 (u_longlong_t)version, (u_longlong_t)newversion);
2862 spa_upgrade(spa, newversion);
2863 VERIFY3U(spa_version(spa), >, version);
2864 VERIFY3U(spa_version(spa), ==, fnvlist_lookup_uint64(spa->spa_config,
2865 zpool_prop_to_name(ZPOOL_PROP_VERSION)));
2866 spa_close(spa, FTAG);
2869 mutex_exit(&ztest_vdev_lock);
2873 ztest_spa_checkpoint(spa_t *spa)
2875 ASSERT(MUTEX_HELD(&ztest_checkpoint_lock));
2877 int error = spa_checkpoint(spa->spa_name);
2881 case ZFS_ERR_DEVRM_IN_PROGRESS:
2882 case ZFS_ERR_DISCARDING_CHECKPOINT:
2883 case ZFS_ERR_CHECKPOINT_EXISTS:
2886 ztest_record_enospc(FTAG);
2889 fatal(0, "spa_checkpoint(%s) = %d", spa->spa_name, error);
2894 ztest_spa_discard_checkpoint(spa_t *spa)
2896 ASSERT(MUTEX_HELD(&ztest_checkpoint_lock));
2898 int error = spa_checkpoint_discard(spa->spa_name);
2902 case ZFS_ERR_DISCARDING_CHECKPOINT:
2903 case ZFS_ERR_NO_CHECKPOINT:
2906 fatal(0, "spa_discard_checkpoint(%s) = %d",
2907 spa->spa_name, error);
2914 ztest_spa_checkpoint_create_discard(ztest_ds_t *zd, uint64_t id)
2916 spa_t *spa = ztest_spa;
2918 mutex_enter(&ztest_checkpoint_lock);
2919 if (ztest_random(2) == 0) {
2920 ztest_spa_checkpoint(spa);
2922 ztest_spa_discard_checkpoint(spa);
2924 mutex_exit(&ztest_checkpoint_lock);
2929 vdev_lookup_by_path(vdev_t *vd, const char *path)
2934 if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
2937 for (c = 0; c < vd->vdev_children; c++)
2938 if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
2946 * Find the first available hole which can be used as a top-level.
2949 find_vdev_hole(spa_t *spa)
2951 vdev_t *rvd = spa->spa_root_vdev;
2954 ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
2956 for (c = 0; c < rvd->vdev_children; c++) {
2957 vdev_t *cvd = rvd->vdev_child[c];
2959 if (cvd->vdev_ishole)
2966 * Verify that vdev_add() works as expected.
2970 ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2972 ztest_shared_t *zs = ztest_shared;
2973 spa_t *spa = ztest_spa;
2979 if (ztest_opts.zo_mmp_test)
2982 mutex_enter(&ztest_vdev_lock);
2983 leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * ztest_opts.zo_raidz;
2985 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2987 ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2990 * If we have slogs then remove them 1/4 of the time.
2992 if (spa_has_slogs(spa) && ztest_random(4) == 0) {
2994 * Grab the guid from the head of the log class rotor.
2996 guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid;
2998 spa_config_exit(spa, SCL_VDEV, FTAG);
3001 * We have to grab the zs_name_lock as writer to
3002 * prevent a race between removing a slog (dmu_objset_find)
3003 * and destroying a dataset. Removing the slog will
3004 * grab a reference on the dataset which may cause
3005 * dsl_destroy_head() to fail with EBUSY thus
3006 * leaving the dataset in an inconsistent state.
3008 pthread_rwlock_wrlock(&ztest_name_lock);
3009 error = spa_vdev_remove(spa, guid, B_FALSE);
3010 pthread_rwlock_unlock(&ztest_name_lock);
3014 case EEXIST: /* Generic zil_reset() error */
3015 case EBUSY: /* Replay required */
3016 case EACCES: /* Crypto key not loaded */
3017 case ZFS_ERR_CHECKPOINT_EXISTS:
3018 case ZFS_ERR_DISCARDING_CHECKPOINT:
3021 fatal(0, "spa_vdev_remove() = %d", error);
3024 spa_config_exit(spa, SCL_VDEV, FTAG);
3027 * Make 1/4 of the devices be log devices.
3029 nvroot = make_vdev_root(NULL, NULL, NULL,
3030 ztest_opts.zo_vdev_size, 0,
3031 ztest_random(4) == 0, ztest_opts.zo_raidz,
3034 error = spa_vdev_add(spa, nvroot);
3035 nvlist_free(nvroot);
3041 ztest_record_enospc("spa_vdev_add");
3044 fatal(0, "spa_vdev_add() = %d", error);
3048 mutex_exit(&ztest_vdev_lock);
3052 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
3056 ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
3058 ztest_shared_t *zs = ztest_shared;
3059 spa_t *spa = ztest_spa;
3060 vdev_t *rvd = spa->spa_root_vdev;
3061 spa_aux_vdev_t *sav;
3067 if (ztest_opts.zo_mmp_test)
3070 path = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
3072 if (ztest_random(2) == 0) {
3073 sav = &spa->spa_spares;
3074 aux = ZPOOL_CONFIG_SPARES;
3076 sav = &spa->spa_l2cache;
3077 aux = ZPOOL_CONFIG_L2CACHE;
3080 mutex_enter(&ztest_vdev_lock);
3082 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
3084 if (sav->sav_count != 0 && ztest_random(4) == 0) {
3086 * Pick a random device to remove.
3088 guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
3091 * Find an unused device we can add.
3093 zs->zs_vdev_aux = 0;
3096 (void) snprintf(path, MAXPATHLEN, ztest_aux_template,
3097 ztest_opts.zo_dir, ztest_opts.zo_pool, aux,
3099 for (c = 0; c < sav->sav_count; c++)
3100 if (strcmp(sav->sav_vdevs[c]->vdev_path,
3103 if (c == sav->sav_count &&
3104 vdev_lookup_by_path(rvd, path) == NULL)
3110 spa_config_exit(spa, SCL_VDEV, FTAG);
3116 nvlist_t *nvroot = make_vdev_root(NULL, aux, NULL,
3117 (ztest_opts.zo_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
3118 error = spa_vdev_add(spa, nvroot);
3124 fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
3126 nvlist_free(nvroot);
3129 * Remove an existing device. Sometimes, dirty its
3130 * vdev state first to make sure we handle removal
3131 * of devices that have pending state changes.
3133 if (ztest_random(2) == 0)
3134 (void) vdev_online(spa, guid, 0, NULL);
3136 error = spa_vdev_remove(spa, guid, B_FALSE);
3141 case ZFS_ERR_CHECKPOINT_EXISTS:
3142 case ZFS_ERR_DISCARDING_CHECKPOINT:
3145 fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
3149 mutex_exit(&ztest_vdev_lock);
3151 umem_free(path, MAXPATHLEN);
3155 * split a pool if it has mirror tlvdevs
3159 ztest_split_pool(ztest_ds_t *zd, uint64_t id)
3161 ztest_shared_t *zs = ztest_shared;
3162 spa_t *spa = ztest_spa;
3163 vdev_t *rvd = spa->spa_root_vdev;
3164 nvlist_t *tree, **child, *config, *split, **schild;
3165 uint_t c, children, schildren = 0, lastlogid = 0;
3168 if (ztest_opts.zo_mmp_test)
3171 mutex_enter(&ztest_vdev_lock);
3173 /* ensure we have a useable config; mirrors of raidz aren't supported */
3174 if (zs->zs_mirrors < 3 || ztest_opts.zo_raidz > 1) {
3175 mutex_exit(&ztest_vdev_lock);
3179 /* clean up the old pool, if any */
3180 (void) spa_destroy("splitp");
3182 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
3184 /* generate a config from the existing config */
3185 mutex_enter(&spa->spa_props_lock);
3186 VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
3188 mutex_exit(&spa->spa_props_lock);
3190 VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
3193 schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
3194 for (c = 0; c < children; c++) {
3195 vdev_t *tvd = rvd->vdev_child[c];
3199 if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
3200 VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
3202 VERIFY(nvlist_add_string(schild[schildren],
3203 ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
3204 VERIFY(nvlist_add_uint64(schild[schildren],
3205 ZPOOL_CONFIG_IS_HOLE, 1) == 0);
3207 lastlogid = schildren;
3212 VERIFY(nvlist_lookup_nvlist_array(child[c],
3213 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
3214 VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
3217 /* OK, create a config that can be used to split */
3218 VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
3219 VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
3220 VDEV_TYPE_ROOT) == 0);
3221 VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
3222 lastlogid != 0 ? lastlogid : schildren) == 0);
3224 VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
3225 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
3227 for (c = 0; c < schildren; c++)
3228 nvlist_free(schild[c]);
3232 spa_config_exit(spa, SCL_VDEV, FTAG);
3234 (void) pthread_rwlock_wrlock(&ztest_name_lock);
3235 error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
3236 (void) pthread_rwlock_unlock(&ztest_name_lock);
3238 nvlist_free(config);
3241 (void) printf("successful split - results:\n");
3242 mutex_enter(&spa_namespace_lock);
3243 show_pool_stats(spa);
3244 show_pool_stats(spa_lookup("splitp"));
3245 mutex_exit(&spa_namespace_lock);
3249 mutex_exit(&ztest_vdev_lock);
3253 * Verify that we can attach and detach devices.
3257 ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
3259 ztest_shared_t *zs = ztest_shared;
3260 spa_t *spa = ztest_spa;
3261 spa_aux_vdev_t *sav = &spa->spa_spares;
3262 vdev_t *rvd = spa->spa_root_vdev;
3263 vdev_t *oldvd, *newvd, *pvd;
3267 uint64_t ashift = ztest_get_ashift();
3268 uint64_t oldguid, pguid;
3269 uint64_t oldsize, newsize;
3270 char *oldpath, *newpath;
3272 int oldvd_has_siblings = B_FALSE;
3273 int newvd_is_spare = B_FALSE;
3275 int error, expected_error;
3277 if (ztest_opts.zo_mmp_test)
3280 oldpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
3281 newpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
3283 mutex_enter(&ztest_vdev_lock);
3284 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
3286 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3289 * If a vdev is in the process of being removed, its removal may
3290 * finish while we are in progress, leading to an unexpected error
3291 * value. Don't bother trying to attach while we are in the middle
3294 if (ztest_device_removal_active) {
3295 spa_config_exit(spa, SCL_ALL, FTAG);
3296 mutex_exit(&ztest_vdev_lock);
3301 * Decide whether to do an attach or a replace.
3303 replacing = ztest_random(2);
3306 * Pick a random top-level vdev.
3308 top = ztest_random_vdev_top(spa, B_TRUE);
3311 * Pick a random leaf within it.
3313 leaf = ztest_random(leaves);
3318 oldvd = rvd->vdev_child[top];
3319 if (zs->zs_mirrors >= 1) {
3320 ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
3321 ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
3322 oldvd = oldvd->vdev_child[leaf / ztest_opts.zo_raidz];
3324 if (ztest_opts.zo_raidz > 1) {
3325 ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
3326 ASSERT(oldvd->vdev_children == ztest_opts.zo_raidz);
3327 oldvd = oldvd->vdev_child[leaf % ztest_opts.zo_raidz];
3331 * If we're already doing an attach or replace, oldvd may be a
3332 * mirror vdev -- in which case, pick a random child.
3334 while (oldvd->vdev_children != 0) {
3335 oldvd_has_siblings = B_TRUE;
3336 ASSERT(oldvd->vdev_children >= 2);
3337 oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
3340 oldguid = oldvd->vdev_guid;
3341 oldsize = vdev_get_min_asize(oldvd);
3342 oldvd_is_log = oldvd->vdev_top->vdev_islog;
3343 (void) strcpy(oldpath, oldvd->vdev_path);
3344 pvd = oldvd->vdev_parent;
3345 pguid = pvd->vdev_guid;
3348 * If oldvd has siblings, then half of the time, detach it.
3350 if (oldvd_has_siblings && ztest_random(2) == 0) {
3351 spa_config_exit(spa, SCL_ALL, FTAG);
3352 error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
3353 if (error != 0 && error != ENODEV && error != EBUSY &&
3354 error != ENOTSUP && error != ZFS_ERR_CHECKPOINT_EXISTS &&
3355 error != ZFS_ERR_DISCARDING_CHECKPOINT)
3356 fatal(0, "detach (%s) returned %d", oldpath, error);
3361 * For the new vdev, choose with equal probability between the two
3362 * standard paths (ending in either 'a' or 'b') or a random hot spare.
3364 if (sav->sav_count != 0 && ztest_random(3) == 0) {
3365 newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
3366 newvd_is_spare = B_TRUE;
3367 (void) strcpy(newpath, newvd->vdev_path);
3369 (void) snprintf(newpath, MAXPATHLEN, ztest_dev_template,
3370 ztest_opts.zo_dir, ztest_opts.zo_pool,
3371 top * leaves + leaf);
3372 if (ztest_random(2) == 0)
3373 newpath[strlen(newpath) - 1] = 'b';
3374 newvd = vdev_lookup_by_path(rvd, newpath);
3379 * Reopen to ensure the vdev's asize field isn't stale.
3382 newsize = vdev_get_min_asize(newvd);
3385 * Make newsize a little bigger or smaller than oldsize.
3386 * If it's smaller, the attach should fail.
3387 * If it's larger, and we're doing a replace,
3388 * we should get dynamic LUN growth when we're done.
3390 newsize = 10 * oldsize / (9 + ztest_random(3));
3394 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
3395 * unless it's a replace; in that case any non-replacing parent is OK.
3397 * If newvd is already part of the pool, it should fail with EBUSY.
3399 * If newvd is too small, it should fail with EOVERFLOW.
3401 if (pvd->vdev_ops != &vdev_mirror_ops &&
3402 pvd->vdev_ops != &vdev_root_ops && (!replacing ||
3403 pvd->vdev_ops == &vdev_replacing_ops ||
3404 pvd->vdev_ops == &vdev_spare_ops))
3405 expected_error = ENOTSUP;
3406 else if (newvd_is_spare && (!replacing || oldvd_is_log))
3407 expected_error = ENOTSUP;
3408 else if (newvd == oldvd)
3409 expected_error = replacing ? 0 : EBUSY;
3410 else if (vdev_lookup_by_path(rvd, newpath) != NULL)
3411 expected_error = EBUSY;
3412 else if (newsize < oldsize)
3413 expected_error = EOVERFLOW;
3414 else if (ashift > oldvd->vdev_top->vdev_ashift)
3415 expected_error = EDOM;
3419 spa_config_exit(spa, SCL_ALL, FTAG);
3422 * Build the nvlist describing newpath.
3424 root = make_vdev_root(newpath, NULL, NULL, newvd == NULL ? newsize : 0,
3425 ashift, 0, 0, 0, 1);
3427 error = spa_vdev_attach(spa, oldguid, root, replacing);
3432 * If our parent was the replacing vdev, but the replace completed,
3433 * then instead of failing with ENOTSUP we may either succeed,
3434 * fail with ENODEV, or fail with EOVERFLOW.
3436 if (expected_error == ENOTSUP &&
3437 (error == 0 || error == ENODEV || error == EOVERFLOW))
3438 expected_error = error;
3441 * If someone grew the LUN, the replacement may be too small.
3443 if (error == EOVERFLOW || error == EBUSY)
3444 expected_error = error;
3446 if (error == ZFS_ERR_CHECKPOINT_EXISTS ||
3447 error == ZFS_ERR_DISCARDING_CHECKPOINT)
3448 expected_error = error;
3450 /* XXX workaround 6690467 */
3451 if (error != expected_error && expected_error != EBUSY) {
3452 fatal(0, "attach (%s %llu, %s %llu, %d) "
3453 "returned %d, expected %d",
3454 oldpath, oldsize, newpath,
3455 newsize, replacing, error, expected_error);
3458 mutex_exit(&ztest_vdev_lock);
3460 umem_free(oldpath, MAXPATHLEN);
3461 umem_free(newpath, MAXPATHLEN);
3466 ztest_device_removal(ztest_ds_t *zd, uint64_t id)
3468 spa_t *spa = ztest_spa;
3473 mutex_enter(&ztest_vdev_lock);
3475 if (ztest_device_removal_active) {
3476 mutex_exit(&ztest_vdev_lock);
3481 * Remove a random top-level vdev and wait for removal to finish.
3483 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
3484 vd = vdev_lookup_top(spa, ztest_random_vdev_top(spa, B_FALSE));
3485 guid = vd->vdev_guid;
3486 spa_config_exit(spa, SCL_VDEV, FTAG);
3488 error = spa_vdev_remove(spa, guid, B_FALSE);
3490 ztest_device_removal_active = B_TRUE;
3491 mutex_exit(&ztest_vdev_lock);
3493 while (spa->spa_vdev_removal != NULL)
3494 txg_wait_synced(spa_get_dsl(spa), 0);
3496 mutex_exit(&ztest_vdev_lock);
3501 * The pool needs to be scrubbed after completing device removal.
3502 * Failure to do so may result in checksum errors due to the
3503 * strategy employed by ztest_fault_inject() when selecting which
3504 * offset are redundant and can be damaged.
3506 error = spa_scan(spa, POOL_SCAN_SCRUB);
3508 while (dsl_scan_scrubbing(spa_get_dsl(spa)))
3509 txg_wait_synced(spa_get_dsl(spa), 0);
3512 mutex_enter(&ztest_vdev_lock);
3513 ztest_device_removal_active = B_FALSE;
3514 mutex_exit(&ztest_vdev_lock);
3518 * Callback function which expands the physical size of the vdev.
3521 grow_vdev(vdev_t *vd, void *arg)
3523 ASSERTV(spa_t *spa = vd->vdev_spa);
3524 size_t *newsize = arg;
3528 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3529 ASSERT(vd->vdev_ops->vdev_op_leaf);
3531 if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
3534 fsize = lseek(fd, 0, SEEK_END);
3535 VERIFY(ftruncate(fd, *newsize) == 0);
3537 if (ztest_opts.zo_verbose >= 6) {
3538 (void) printf("%s grew from %lu to %lu bytes\n",
3539 vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
3546 * Callback function which expands a given vdev by calling vdev_online().
3550 online_vdev(vdev_t *vd, void *arg)
3552 spa_t *spa = vd->vdev_spa;
3553 vdev_t *tvd = vd->vdev_top;
3554 uint64_t guid = vd->vdev_guid;
3555 uint64_t generation = spa->spa_config_generation + 1;
3556 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
3559 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3560 ASSERT(vd->vdev_ops->vdev_op_leaf);
3562 /* Calling vdev_online will initialize the new metaslabs */
3563 spa_config_exit(spa, SCL_STATE, spa);
3564 error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
3565 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3568 * If vdev_online returned an error or the underlying vdev_open
3569 * failed then we abort the expand. The only way to know that
3570 * vdev_open fails is by checking the returned newstate.
3572 if (error || newstate != VDEV_STATE_HEALTHY) {
3573 if (ztest_opts.zo_verbose >= 5) {
3574 (void) printf("Unable to expand vdev, state %llu, "
3575 "error %d\n", (u_longlong_t)newstate, error);
3579 ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
3582 * Since we dropped the lock we need to ensure that we're
3583 * still talking to the original vdev. It's possible this
3584 * vdev may have been detached/replaced while we were
3585 * trying to online it.
3587 if (generation != spa->spa_config_generation) {
3588 if (ztest_opts.zo_verbose >= 5) {
3589 (void) printf("vdev configuration has changed, "
3590 "guid %llu, state %llu, expected gen %llu, "
3593 (u_longlong_t)tvd->vdev_state,
3594 (u_longlong_t)generation,
3595 (u_longlong_t)spa->spa_config_generation);
3603 * Traverse the vdev tree calling the supplied function.
3604 * We continue to walk the tree until we either have walked all
3605 * children or we receive a non-NULL return from the callback.
3606 * If a NULL callback is passed, then we just return back the first
3607 * leaf vdev we encounter.
3610 vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
3614 if (vd->vdev_ops->vdev_op_leaf) {
3618 return (func(vd, arg));
3621 for (c = 0; c < vd->vdev_children; c++) {
3622 vdev_t *cvd = vd->vdev_child[c];
3623 if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
3630 * Verify that dynamic LUN growth works as expected.
3634 ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
3636 spa_t *spa = ztest_spa;
3638 metaslab_class_t *mc;
3639 metaslab_group_t *mg;
3640 size_t psize, newsize;
3642 uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
3644 mutex_enter(&ztest_checkpoint_lock);
3645 mutex_enter(&ztest_vdev_lock);
3646 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3649 * If there is a vdev removal in progress, it could complete while
3650 * we are running, in which case we would not be able to verify
3651 * that the metaslab_class space increased (because it decreases
3652 * when the device removal completes).
3654 if (ztest_device_removal_active) {
3655 spa_config_exit(spa, SCL_STATE, spa);
3656 mutex_exit(&ztest_vdev_lock);
3657 mutex_exit(&ztest_checkpoint_lock);
3661 top = ztest_random_vdev_top(spa, B_TRUE);
3663 tvd = spa->spa_root_vdev->vdev_child[top];
3666 old_ms_count = tvd->vdev_ms_count;
3667 old_class_space = metaslab_class_get_space(mc);
3670 * Determine the size of the first leaf vdev associated with
3671 * our top-level device.
3673 vd = vdev_walk_tree(tvd, NULL, NULL);
3674 ASSERT3P(vd, !=, NULL);
3675 ASSERT(vd->vdev_ops->vdev_op_leaf);
3677 psize = vd->vdev_psize;
3680 * We only try to expand the vdev if it's healthy, less than 4x its
3681 * original size, and it has a valid psize.
3683 if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
3684 psize == 0 || psize >= 4 * ztest_opts.zo_vdev_size) {
3685 spa_config_exit(spa, SCL_STATE, spa);
3686 mutex_exit(&ztest_vdev_lock);
3687 mutex_exit(&ztest_checkpoint_lock);
3691 newsize = psize + psize / 8;
3692 ASSERT3U(newsize, >, psize);
3694 if (ztest_opts.zo_verbose >= 6) {
3695 (void) printf("Expanding LUN %s from %lu to %lu\n",
3696 vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
3700 * Growing the vdev is a two step process:
3701 * 1). expand the physical size (i.e. relabel)
3702 * 2). online the vdev to create the new metaslabs
3704 if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
3705 vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
3706 tvd->vdev_state != VDEV_STATE_HEALTHY) {
3707 if (ztest_opts.zo_verbose >= 5) {
3708 (void) printf("Could not expand LUN because "
3709 "the vdev configuration changed.\n");
3711 spa_config_exit(spa, SCL_STATE, spa);
3712 mutex_exit(&ztest_vdev_lock);
3713 mutex_exit(&ztest_checkpoint_lock);
3717 spa_config_exit(spa, SCL_STATE, spa);
3720 * Expanding the LUN will update the config asynchronously,
3721 * thus we must wait for the async thread to complete any
3722 * pending tasks before proceeding.
3726 mutex_enter(&spa->spa_async_lock);
3727 done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
3728 mutex_exit(&spa->spa_async_lock);
3731 txg_wait_synced(spa_get_dsl(spa), 0);
3732 (void) poll(NULL, 0, 100);
3735 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3737 tvd = spa->spa_root_vdev->vdev_child[top];
3738 new_ms_count = tvd->vdev_ms_count;
3739 new_class_space = metaslab_class_get_space(mc);
3741 if (tvd->vdev_mg != mg || mg->mg_class != mc) {
3742 if (ztest_opts.zo_verbose >= 5) {
3743 (void) printf("Could not verify LUN expansion due to "
3744 "intervening vdev offline or remove.\n");
3746 spa_config_exit(spa, SCL_STATE, spa);
3747 mutex_exit(&ztest_vdev_lock);
3748 mutex_exit(&ztest_checkpoint_lock);
3753 * Make sure we were able to grow the vdev.
3755 if (new_ms_count <= old_ms_count) {
3756 fatal(0, "LUN expansion failed: ms_count %llu < %llu\n",
3757 old_ms_count, new_ms_count);
3761 * Make sure we were able to grow the pool.
3763 if (new_class_space <= old_class_space) {
3764 fatal(0, "LUN expansion failed: class_space %llu < %llu\n",
3765 old_class_space, new_class_space);
3768 if (ztest_opts.zo_verbose >= 5) {
3769 char oldnumbuf[NN_NUMBUF_SZ], newnumbuf[NN_NUMBUF_SZ];
3771 nicenum(old_class_space, oldnumbuf, sizeof (oldnumbuf));
3772 nicenum(new_class_space, newnumbuf, sizeof (newnumbuf));
3773 (void) printf("%s grew from %s to %s\n",
3774 spa->spa_name, oldnumbuf, newnumbuf);
3777 spa_config_exit(spa, SCL_STATE, spa);
3778 mutex_exit(&ztest_vdev_lock);
3779 mutex_exit(&ztest_checkpoint_lock);
3783 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
3787 ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3790 * Create the objects common to all ztest datasets.
3792 VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
3793 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
3797 ztest_dataset_create(char *dsname)
3801 dsl_crypto_params_t *dcp = NULL;
3804 * 50% of the time, we create encrypted datasets
3805 * using a random cipher suite and a hard-coded
3808 rand = ztest_random(2);
3810 nvlist_t *crypto_args = fnvlist_alloc();
3811 nvlist_t *props = fnvlist_alloc();
3813 /* slight bias towards the default cipher suite */
3814 rand = ztest_random(ZIO_CRYPT_FUNCTIONS);
3815 if (rand < ZIO_CRYPT_AES_128_CCM)
3816 rand = ZIO_CRYPT_ON;
3818 fnvlist_add_uint64(props,
3819 zfs_prop_to_name(ZFS_PROP_ENCRYPTION), rand);
3820 fnvlist_add_uint8_array(crypto_args, "wkeydata",
3821 (uint8_t *)ztest_wkeydata, WRAPPING_KEY_LEN);
3824 * These parameters aren't really used by the kernel. They
3825 * are simply stored so that userspace knows how to load
3828 fnvlist_add_uint64(props,
3829 zfs_prop_to_name(ZFS_PROP_KEYFORMAT), ZFS_KEYFORMAT_RAW);
3830 fnvlist_add_string(props,
3831 zfs_prop_to_name(ZFS_PROP_KEYLOCATION), "prompt");
3832 fnvlist_add_uint64(props,
3833 zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT), 0ULL);
3834 fnvlist_add_uint64(props,
3835 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS), 0ULL);
3837 VERIFY0(dsl_crypto_params_create_nvlist(DCP_CMD_NONE, props,
3838 crypto_args, &dcp));
3841 * Cycle through all available encryption implementations
3842 * to verify interoperability.
3844 VERIFY0(gcm_impl_set("cycle"));
3845 VERIFY0(aes_impl_set("cycle"));
3847 fnvlist_free(crypto_args);
3848 fnvlist_free(props);
3851 err = dmu_objset_create(dsname, DMU_OST_OTHER, 0, dcp,
3852 ztest_objset_create_cb, NULL);
3853 dsl_crypto_params_free(dcp, !!err);
3855 rand = ztest_random(100);
3856 if (err || rand < 80)
3859 if (ztest_opts.zo_verbose >= 5)
3860 (void) printf("Setting dataset %s to sync always\n", dsname);
3861 return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC,
3862 ZFS_SYNC_ALWAYS, B_FALSE));
3867 ztest_objset_destroy_cb(const char *name, void *arg)
3870 dmu_object_info_t doi;
3874 * Verify that the dataset contains a directory object.
3876 VERIFY0(ztest_dmu_objset_own(name, DMU_OST_OTHER, B_TRUE,
3877 B_TRUE, FTAG, &os));
3878 error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
3879 if (error != ENOENT) {
3880 /* We could have crashed in the middle of destroying it */
3882 ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
3883 ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
3885 dmu_objset_disown(os, B_TRUE, FTAG);
3888 * Destroy the dataset.
3890 if (strchr(name, '@') != NULL) {
3891 VERIFY0(dsl_destroy_snapshot(name, B_TRUE));
3893 error = dsl_destroy_head(name);
3894 /* There could be a hold on this dataset */
3902 ztest_snapshot_create(char *osname, uint64_t id)
3904 char snapname[ZFS_MAX_DATASET_NAME_LEN];
3907 (void) snprintf(snapname, sizeof (snapname), "%llu", (u_longlong_t)id);
3909 error = dmu_objset_snapshot_one(osname, snapname);
3910 if (error == ENOSPC) {
3911 ztest_record_enospc(FTAG);
3914 if (error != 0 && error != EEXIST) {
3915 fatal(0, "ztest_snapshot_create(%s@%s) = %d", osname,
3922 ztest_snapshot_destroy(char *osname, uint64_t id)
3924 char snapname[ZFS_MAX_DATASET_NAME_LEN];
3927 (void) snprintf(snapname, sizeof (snapname), "%s@%llu", osname,
3930 error = dsl_destroy_snapshot(snapname, B_FALSE);
3931 if (error != 0 && error != ENOENT)
3932 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
3938 ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
3944 char name[ZFS_MAX_DATASET_NAME_LEN];
3948 zdtmp = umem_alloc(sizeof (ztest_ds_t), UMEM_NOFAIL);
3950 (void) pthread_rwlock_rdlock(&ztest_name_lock);
3952 (void) snprintf(name, sizeof (name), "%s/temp_%llu",
3953 ztest_opts.zo_pool, (u_longlong_t)id);
3956 * If this dataset exists from a previous run, process its replay log
3957 * half of the time. If we don't replay it, then dsl_destroy_head()
3958 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
3960 if (ztest_random(2) == 0 &&
3961 ztest_dmu_objset_own(name, DMU_OST_OTHER, B_FALSE,
3962 B_TRUE, FTAG, &os) == 0) {
3963 ztest_zd_init(zdtmp, NULL, os);
3964 zil_replay(os, zdtmp, ztest_replay_vector);
3965 ztest_zd_fini(zdtmp);
3966 txg_wait_synced(dmu_objset_pool(os), 0);
3967 dmu_objset_disown(os, B_TRUE, FTAG);
3971 * There may be an old instance of the dataset we're about to
3972 * create lying around from a previous run. If so, destroy it
3973 * and all of its snapshots.
3975 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
3976 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
3979 * Verify that the destroyed dataset is no longer in the namespace.
3981 VERIFY3U(ENOENT, ==, ztest_dmu_objset_own(name, DMU_OST_OTHER, B_TRUE,
3982 B_TRUE, FTAG, &os));
3985 * Verify that we can create a new dataset.
3987 error = ztest_dataset_create(name);
3989 if (error == ENOSPC) {
3990 ztest_record_enospc(FTAG);
3993 fatal(0, "dmu_objset_create(%s) = %d", name, error);
3996 VERIFY0(ztest_dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, B_TRUE,
3999 ztest_zd_init(zdtmp, NULL, os);
4002 * Open the intent log for it.
4004 zilog = zil_open(os, ztest_get_data);
4007 * Put some objects in there, do a little I/O to them,
4008 * and randomly take a couple of snapshots along the way.
4010 iters = ztest_random(5);
4011 for (i = 0; i < iters; i++) {
4012 ztest_dmu_object_alloc_free(zdtmp, id);
4013 if (ztest_random(iters) == 0)
4014 (void) ztest_snapshot_create(name, i);
4018 * Verify that we cannot create an existing dataset.
4020 VERIFY3U(EEXIST, ==,
4021 dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL, NULL));
4024 * Verify that we can hold an objset that is also owned.
4026 VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
4027 dmu_objset_rele(os2, FTAG);
4030 * Verify that we cannot own an objset that is already owned.
4032 VERIFY3U(EBUSY, ==, ztest_dmu_objset_own(name, DMU_OST_OTHER,
4033 B_FALSE, B_TRUE, FTAG, &os2));
4036 txg_wait_synced(spa_get_dsl(os->os_spa), 0);
4037 dmu_objset_disown(os, B_TRUE, FTAG);
4038 ztest_zd_fini(zdtmp);
4040 (void) pthread_rwlock_unlock(&ztest_name_lock);
4042 umem_free(zdtmp, sizeof (ztest_ds_t));
4046 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
4049 ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
4051 (void) pthread_rwlock_rdlock(&ztest_name_lock);
4052 (void) ztest_snapshot_destroy(zd->zd_name, id);
4053 (void) ztest_snapshot_create(zd->zd_name, id);
4054 (void) pthread_rwlock_unlock(&ztest_name_lock);
4058 * Cleanup non-standard snapshots and clones.
4061 ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
4070 snap1name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
4071 clone1name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
4072 snap2name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
4073 clone2name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
4074 snap3name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
4076 (void) snprintf(snap1name, ZFS_MAX_DATASET_NAME_LEN,
4077 "%s@s1_%llu", osname, (u_longlong_t)id);
4078 (void) snprintf(clone1name, ZFS_MAX_DATASET_NAME_LEN,
4079 "%s/c1_%llu", osname, (u_longlong_t)id);
4080 (void) snprintf(snap2name, ZFS_MAX_DATASET_NAME_LEN,
4081 "%s@s2_%llu", clone1name, (u_longlong_t)id);
4082 (void) snprintf(clone2name, ZFS_MAX_DATASET_NAME_LEN,
4083 "%s/c2_%llu", osname, (u_longlong_t)id);
4084 (void) snprintf(snap3name, ZFS_MAX_DATASET_NAME_LEN,
4085 "%s@s3_%llu", clone1name, (u_longlong_t)id);
4087 error = dsl_destroy_head(clone2name);
4088 if (error && error != ENOENT)
4089 fatal(0, "dsl_destroy_head(%s) = %d", clone2name, error);
4090 error = dsl_destroy_snapshot(snap3name, B_FALSE);
4091 if (error && error != ENOENT)
4092 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap3name, error);
4093 error = dsl_destroy_snapshot(snap2name, B_FALSE);
4094 if (error && error != ENOENT)
4095 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap2name, error);
4096 error = dsl_destroy_head(clone1name);
4097 if (error && error != ENOENT)
4098 fatal(0, "dsl_destroy_head(%s) = %d", clone1name, error);
4099 error = dsl_destroy_snapshot(snap1name, B_FALSE);
4100 if (error && error != ENOENT)
4101 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap1name, error);
4103 umem_free(snap1name, ZFS_MAX_DATASET_NAME_LEN);
4104 umem_free(clone1name, ZFS_MAX_DATASET_NAME_LEN);
4105 umem_free(snap2name, ZFS_MAX_DATASET_NAME_LEN);
4106 umem_free(clone2name, ZFS_MAX_DATASET_NAME_LEN);
4107 umem_free(snap3name, ZFS_MAX_DATASET_NAME_LEN);
4111 * Verify dsl_dataset_promote handles EBUSY
4114 ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
4122 char *osname = zd->zd_name;
4125 snap1name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
4126 clone1name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
4127 snap2name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
4128 clone2name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
4129 snap3name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
4131 (void) pthread_rwlock_rdlock(&ztest_name_lock);
4133 ztest_dsl_dataset_cleanup(osname, id);
4135 (void) snprintf(snap1name, ZFS_MAX_DATASET_NAME_LEN,
4136 "%s@s1_%llu", osname, (u_longlong_t)id);
4137 (void) snprintf(clone1name, ZFS_MAX_DATASET_NAME_LEN,
4138 "%s/c1_%llu", osname, (u_longlong_t)id);
4139 (void) snprintf(snap2name, ZFS_MAX_DATASET_NAME_LEN,
4140 "%s@s2_%llu", clone1name, (u_longlong_t)id);
4141 (void) snprintf(clone2name, ZFS_MAX_DATASET_NAME_LEN,
4142 "%s/c2_%llu", osname, (u_longlong_t)id);
4143 (void) snprintf(snap3name, ZFS_MAX_DATASET_NAME_LEN,
4144 "%s@s3_%llu", clone1name, (u_longlong_t)id);
4146 error = dmu_objset_snapshot_one(osname, strchr(snap1name, '@') + 1);
4147 if (error && error != EEXIST) {
4148 if (error == ENOSPC) {
4149 ztest_record_enospc(FTAG);
4152 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
4155 error = dmu_objset_clone(clone1name, snap1name);
4157 if (error == ENOSPC) {
4158 ztest_record_enospc(FTAG);
4161 fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
4164 error = dmu_objset_snapshot_one(clone1name, strchr(snap2name, '@') + 1);
4165 if (error && error != EEXIST) {
4166 if (error == ENOSPC) {
4167 ztest_record_enospc(FTAG);
4170 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
4173 error = dmu_objset_snapshot_one(clone1name, strchr(snap3name, '@') + 1);
4174 if (error && error != EEXIST) {
4175 if (error == ENOSPC) {
4176 ztest_record_enospc(FTAG);
4179 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
4182 error = dmu_objset_clone(clone2name, snap3name);
4184 if (error == ENOSPC) {
4185 ztest_record_enospc(FTAG);
4188 fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
4191 error = ztest_dmu_objset_own(snap2name, DMU_OST_ANY, B_TRUE, B_TRUE,
4194 fatal(0, "dmu_objset_own(%s) = %d", snap2name, error);
4195 error = dsl_dataset_promote(clone2name, NULL);
4196 if (error == ENOSPC) {
4197 dmu_objset_disown(os, B_TRUE, FTAG);
4198 ztest_record_enospc(FTAG);
4202 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
4204 dmu_objset_disown(os, B_TRUE, FTAG);
4207 ztest_dsl_dataset_cleanup(osname, id);
4209 (void) pthread_rwlock_unlock(&ztest_name_lock);
4211 umem_free(snap1name, ZFS_MAX_DATASET_NAME_LEN);
4212 umem_free(clone1name, ZFS_MAX_DATASET_NAME_LEN);
4213 umem_free(snap2name, ZFS_MAX_DATASET_NAME_LEN);
4214 umem_free(clone2name, ZFS_MAX_DATASET_NAME_LEN);
4215 umem_free(snap3name, ZFS_MAX_DATASET_NAME_LEN);
4218 #undef OD_ARRAY_SIZE
4219 #define OD_ARRAY_SIZE 4
4222 * Verify that dmu_object_{alloc,free} work as expected.
4225 ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
4232 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
4233 od = umem_alloc(size, UMEM_NOFAIL);
4234 batchsize = OD_ARRAY_SIZE;
4236 for (b = 0; b < batchsize; b++)
4237 ztest_od_init(od + b, id, FTAG, b, DMU_OT_UINT64_OTHER,
4241 * Destroy the previous batch of objects, create a new batch,
4242 * and do some I/O on the new objects.
4244 if (ztest_object_init(zd, od, size, B_TRUE) != 0)
4247 while (ztest_random(4 * batchsize) != 0)
4248 ztest_io(zd, od[ztest_random(batchsize)].od_object,
4249 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4251 umem_free(od, size);
4255 * Rewind the global allocator to verify object allocation backfilling.
4258 ztest_dmu_object_next_chunk(ztest_ds_t *zd, uint64_t id)
4260 objset_t *os = zd->zd_os;
4261 int dnodes_per_chunk = 1 << dmu_object_alloc_chunk_shift;
4265 * Rewind the global allocator randomly back to a lower object number
4266 * to force backfilling and reclamation of recently freed dnodes.
4268 mutex_enter(&os->os_obj_lock);
4269 object = ztest_random(os->os_obj_next_chunk);
4270 os->os_obj_next_chunk = P2ALIGN(object, dnodes_per_chunk);
4271 mutex_exit(&os->os_obj_lock);
4274 #undef OD_ARRAY_SIZE
4275 #define OD_ARRAY_SIZE 2
4278 * Verify that dmu_{read,write} work as expected.
4281 ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
4286 objset_t *os = zd->zd_os;
4287 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
4288 od = umem_alloc(size, UMEM_NOFAIL);
4290 int i, freeit, error;
4292 bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
4293 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
4294 uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
4295 uint64_t regions = 997;
4296 uint64_t stride = 123456789ULL;
4297 uint64_t width = 40;
4298 int free_percent = 5;
4301 * This test uses two objects, packobj and bigobj, that are always
4302 * updated together (i.e. in the same tx) so that their contents are
4303 * in sync and can be compared. Their contents relate to each other
4304 * in a simple way: packobj is a dense array of 'bufwad' structures,
4305 * while bigobj is a sparse array of the same bufwads. Specifically,
4306 * for any index n, there are three bufwads that should be identical:
4308 * packobj, at offset n * sizeof (bufwad_t)
4309 * bigobj, at the head of the nth chunk
4310 * bigobj, at the tail of the nth chunk
4312 * The chunk size is arbitrary. It doesn't have to be a power of two,
4313 * and it doesn't have any relation to the object blocksize.
4314 * The only requirement is that it can hold at least two bufwads.
4316 * Normally, we write the bufwad to each of these locations.
4317 * However, free_percent of the time we instead write zeroes to
4318 * packobj and perform a dmu_free_range() on bigobj. By comparing
4319 * bigobj to packobj, we can verify that the DMU is correctly
4320 * tracking which parts of an object are allocated and free,
4321 * and that the contents of the allocated blocks are correct.
4325 * Read the directory info. If it's the first time, set things up.
4327 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, chunksize);
4328 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, 0,
4331 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
4332 umem_free(od, size);
4336 bigobj = od[0].od_object;
4337 packobj = od[1].od_object;
4338 chunksize = od[0].od_gen;
4339 ASSERT(chunksize == od[1].od_gen);
4342 * Prefetch a random chunk of the big object.
4343 * Our aim here is to get some async reads in flight
4344 * for blocks that we may free below; the DMU should
4345 * handle this race correctly.
4347 n = ztest_random(regions) * stride + ztest_random(width);
4348 s = 1 + ztest_random(2 * width - 1);
4349 dmu_prefetch(os, bigobj, 0, n * chunksize, s * chunksize,
4350 ZIO_PRIORITY_SYNC_READ);
4353 * Pick a random index and compute the offsets into packobj and bigobj.
4355 n = ztest_random(regions) * stride + ztest_random(width);
4356 s = 1 + ztest_random(width - 1);
4358 packoff = n * sizeof (bufwad_t);
4359 packsize = s * sizeof (bufwad_t);
4361 bigoff = n * chunksize;
4362 bigsize = s * chunksize;
4364 packbuf = umem_alloc(packsize, UMEM_NOFAIL);
4365 bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
4368 * free_percent of the time, free a range of bigobj rather than
4371 freeit = (ztest_random(100) < free_percent);
4374 * Read the current contents of our objects.
4376 error = dmu_read(os, packobj, packoff, packsize, packbuf,
4379 error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
4384 * Get a tx for the mods to both packobj and bigobj.
4386 tx = dmu_tx_create(os);
4388 dmu_tx_hold_write(tx, packobj, packoff, packsize);
4391 dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
4393 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
4395 /* This accounts for setting the checksum/compression. */
4396 dmu_tx_hold_bonus(tx, bigobj);
4398 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4400 umem_free(packbuf, packsize);
4401 umem_free(bigbuf, bigsize);
4402 umem_free(od, size);
4406 enum zio_checksum cksum;
4408 cksum = (enum zio_checksum)
4409 ztest_random_dsl_prop(ZFS_PROP_CHECKSUM);
4410 } while (cksum >= ZIO_CHECKSUM_LEGACY_FUNCTIONS);
4411 dmu_object_set_checksum(os, bigobj, cksum, tx);
4413 enum zio_compress comp;
4415 comp = (enum zio_compress)
4416 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION);
4417 } while (comp >= ZIO_COMPRESS_LEGACY_FUNCTIONS);
4418 dmu_object_set_compress(os, bigobj, comp, tx);
4421 * For each index from n to n + s, verify that the existing bufwad
4422 * in packobj matches the bufwads at the head and tail of the
4423 * corresponding chunk in bigobj. Then update all three bufwads
4424 * with the new values we want to write out.
4426 for (i = 0; i < s; i++) {
4428 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
4430 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
4432 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
4434 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
4435 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
4437 if (pack->bw_txg > txg)
4438 fatal(0, "future leak: got %llx, open txg is %llx",
4441 if (pack->bw_data != 0 && pack->bw_index != n + i)
4442 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4443 pack->bw_index, n, i);
4445 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
4446 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
4448 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
4449 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
4452 bzero(pack, sizeof (bufwad_t));
4454 pack->bw_index = n + i;
4456 pack->bw_data = 1 + ztest_random(-2ULL);
4463 * We've verified all the old bufwads, and made new ones.
4464 * Now write them out.
4466 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
4469 if (ztest_opts.zo_verbose >= 7) {
4470 (void) printf("freeing offset %llx size %llx"
4472 (u_longlong_t)bigoff,
4473 (u_longlong_t)bigsize,
4476 VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
4478 if (ztest_opts.zo_verbose >= 7) {
4479 (void) printf("writing offset %llx size %llx"
4481 (u_longlong_t)bigoff,
4482 (u_longlong_t)bigsize,
4485 dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
4491 * Sanity check the stuff we just wrote.
4494 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
4495 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
4497 VERIFY(0 == dmu_read(os, packobj, packoff,
4498 packsize, packcheck, DMU_READ_PREFETCH));
4499 VERIFY(0 == dmu_read(os, bigobj, bigoff,
4500 bigsize, bigcheck, DMU_READ_PREFETCH));
4502 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
4503 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
4505 umem_free(packcheck, packsize);
4506 umem_free(bigcheck, bigsize);
4509 umem_free(packbuf, packsize);
4510 umem_free(bigbuf, bigsize);
4511 umem_free(od, size);
4515 compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
4516 uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
4524 * For each index from n to n + s, verify that the existing bufwad
4525 * in packobj matches the bufwads at the head and tail of the
4526 * corresponding chunk in bigobj. Then update all three bufwads
4527 * with the new values we want to write out.
4529 for (i = 0; i < s; i++) {
4531 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
4533 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
4535 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
4537 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
4538 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
4540 if (pack->bw_txg > txg)
4541 fatal(0, "future leak: got %llx, open txg is %llx",
4544 if (pack->bw_data != 0 && pack->bw_index != n + i)
4545 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4546 pack->bw_index, n, i);
4548 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
4549 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
4551 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
4552 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
4554 pack->bw_index = n + i;
4556 pack->bw_data = 1 + ztest_random(-2ULL);
4563 #undef OD_ARRAY_SIZE
4564 #define OD_ARRAY_SIZE 2
4567 ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
4569 objset_t *os = zd->zd_os;
4576 bufwad_t *packbuf, *bigbuf;
4577 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
4578 uint64_t blocksize = ztest_random_blocksize();
4579 uint64_t chunksize = blocksize;
4580 uint64_t regions = 997;
4581 uint64_t stride = 123456789ULL;
4583 dmu_buf_t *bonus_db;
4584 arc_buf_t **bigbuf_arcbufs;
4585 dmu_object_info_t doi;
4587 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
4588 od = umem_alloc(size, UMEM_NOFAIL);
4591 * This test uses two objects, packobj and bigobj, that are always
4592 * updated together (i.e. in the same tx) so that their contents are
4593 * in sync and can be compared. Their contents relate to each other
4594 * in a simple way: packobj is a dense array of 'bufwad' structures,
4595 * while bigobj is a sparse array of the same bufwads. Specifically,
4596 * for any index n, there are three bufwads that should be identical:
4598 * packobj, at offset n * sizeof (bufwad_t)
4599 * bigobj, at the head of the nth chunk
4600 * bigobj, at the tail of the nth chunk
4602 * The chunk size is set equal to bigobj block size so that
4603 * dmu_assign_arcbuf_by_dbuf() can be tested for object updates.
4607 * Read the directory info. If it's the first time, set things up.
4609 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0, 0);
4610 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, 0,
4614 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
4615 umem_free(od, size);
4619 bigobj = od[0].od_object;
4620 packobj = od[1].od_object;
4621 blocksize = od[0].od_blocksize;
4622 chunksize = blocksize;
4623 ASSERT(chunksize == od[1].od_gen);
4625 VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
4626 VERIFY(ISP2(doi.doi_data_block_size));
4627 VERIFY(chunksize == doi.doi_data_block_size);
4628 VERIFY(chunksize >= 2 * sizeof (bufwad_t));
4631 * Pick a random index and compute the offsets into packobj and bigobj.
4633 n = ztest_random(regions) * stride + ztest_random(width);
4634 s = 1 + ztest_random(width - 1);
4636 packoff = n * sizeof (bufwad_t);
4637 packsize = s * sizeof (bufwad_t);
4639 bigoff = n * chunksize;
4640 bigsize = s * chunksize;
4642 packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
4643 bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
4645 VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
4647 bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
4650 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
4651 * Iteration 1 test zcopy to already referenced dbufs.
4652 * Iteration 2 test zcopy to dirty dbuf in the same txg.
4653 * Iteration 3 test zcopy to dbuf dirty in previous txg.
4654 * Iteration 4 test zcopy when dbuf is no longer dirty.
4655 * Iteration 5 test zcopy when it can't be done.
4656 * Iteration 6 one more zcopy write.
4658 for (i = 0; i < 7; i++) {
4663 * In iteration 5 (i == 5) use arcbufs
4664 * that don't match bigobj blksz to test
4665 * dmu_assign_arcbuf_by_dbuf() when it can't directly
4666 * assign an arcbuf to a dbuf.
4668 for (j = 0; j < s; j++) {
4669 if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
4671 dmu_request_arcbuf(bonus_db, chunksize);
4673 bigbuf_arcbufs[2 * j] =
4674 dmu_request_arcbuf(bonus_db, chunksize / 2);
4675 bigbuf_arcbufs[2 * j + 1] =
4676 dmu_request_arcbuf(bonus_db, chunksize / 2);
4681 * Get a tx for the mods to both packobj and bigobj.
4683 tx = dmu_tx_create(os);
4685 dmu_tx_hold_write(tx, packobj, packoff, packsize);
4686 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
4688 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4690 umem_free(packbuf, packsize);
4691 umem_free(bigbuf, bigsize);
4692 for (j = 0; j < s; j++) {
4694 chunksize < (SPA_MINBLOCKSIZE * 2)) {
4695 dmu_return_arcbuf(bigbuf_arcbufs[j]);
4698 bigbuf_arcbufs[2 * j]);
4700 bigbuf_arcbufs[2 * j + 1]);
4703 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4704 umem_free(od, size);
4705 dmu_buf_rele(bonus_db, FTAG);
4710 * 50% of the time don't read objects in the 1st iteration to
4711 * test dmu_assign_arcbuf_by_dbuf() for the case when there are
4712 * no existing dbufs for the specified offsets.
4714 if (i != 0 || ztest_random(2) != 0) {
4715 error = dmu_read(os, packobj, packoff,
4716 packsize, packbuf, DMU_READ_PREFETCH);
4718 error = dmu_read(os, bigobj, bigoff, bigsize,
4719 bigbuf, DMU_READ_PREFETCH);
4722 compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
4726 * We've verified all the old bufwads, and made new ones.
4727 * Now write them out.
4729 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
4730 if (ztest_opts.zo_verbose >= 7) {
4731 (void) printf("writing offset %llx size %llx"
4733 (u_longlong_t)bigoff,
4734 (u_longlong_t)bigsize,
4737 for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
4739 if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
4740 bcopy((caddr_t)bigbuf + (off - bigoff),
4741 bigbuf_arcbufs[j]->b_data, chunksize);
4743 bcopy((caddr_t)bigbuf + (off - bigoff),
4744 bigbuf_arcbufs[2 * j]->b_data,
4746 bcopy((caddr_t)bigbuf + (off - bigoff) +
4748 bigbuf_arcbufs[2 * j + 1]->b_data,
4753 VERIFY(dmu_buf_hold(os, bigobj, off,
4754 FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0);
4756 if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
4757 dmu_assign_arcbuf_by_dbuf(bonus_db, off,
4758 bigbuf_arcbufs[j], tx);
4760 dmu_assign_arcbuf_by_dbuf(bonus_db, off,
4761 bigbuf_arcbufs[2 * j], tx);
4762 dmu_assign_arcbuf_by_dbuf(bonus_db,
4763 off + chunksize / 2,
4764 bigbuf_arcbufs[2 * j + 1], tx);
4767 dmu_buf_rele(dbt, FTAG);
4773 * Sanity check the stuff we just wrote.
4776 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
4777 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
4779 VERIFY(0 == dmu_read(os, packobj, packoff,
4780 packsize, packcheck, DMU_READ_PREFETCH));
4781 VERIFY(0 == dmu_read(os, bigobj, bigoff,
4782 bigsize, bigcheck, DMU_READ_PREFETCH));
4784 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
4785 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
4787 umem_free(packcheck, packsize);
4788 umem_free(bigcheck, bigsize);
4791 txg_wait_open(dmu_objset_pool(os), 0);
4792 } else if (i == 3) {
4793 txg_wait_synced(dmu_objset_pool(os), 0);
4797 dmu_buf_rele(bonus_db, FTAG);
4798 umem_free(packbuf, packsize);
4799 umem_free(bigbuf, bigsize);
4800 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4801 umem_free(od, size);
4806 ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
4810 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4811 uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
4812 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4815 * Have multiple threads write to large offsets in an object
4816 * to verify that parallel writes to an object -- even to the
4817 * same blocks within the object -- doesn't cause any trouble.
4819 ztest_od_init(od, ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
4821 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0)
4824 while (ztest_random(10) != 0)
4825 ztest_io(zd, od->od_object, offset);
4827 umem_free(od, sizeof (ztest_od_t));
4831 ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
4834 uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
4835 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4836 uint64_t count = ztest_random(20) + 1;
4837 uint64_t blocksize = ztest_random_blocksize();
4840 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4842 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0, 0);
4844 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4845 !ztest_random(2)) != 0) {
4846 umem_free(od, sizeof (ztest_od_t));
4850 if (ztest_truncate(zd, od->od_object, offset, count * blocksize) != 0) {
4851 umem_free(od, sizeof (ztest_od_t));
4855 ztest_prealloc(zd, od->od_object, offset, count * blocksize);
4857 data = umem_zalloc(blocksize, UMEM_NOFAIL);
4859 while (ztest_random(count) != 0) {
4860 uint64_t randoff = offset + (ztest_random(count) * blocksize);
4861 if (ztest_write(zd, od->od_object, randoff, blocksize,
4864 while (ztest_random(4) != 0)
4865 ztest_io(zd, od->od_object, randoff);
4868 umem_free(data, blocksize);
4869 umem_free(od, sizeof (ztest_od_t));
4873 * Verify that zap_{create,destroy,add,remove,update} work as expected.
4875 #define ZTEST_ZAP_MIN_INTS 1
4876 #define ZTEST_ZAP_MAX_INTS 4
4877 #define ZTEST_ZAP_MAX_PROPS 1000
4880 ztest_zap(ztest_ds_t *zd, uint64_t id)
4882 objset_t *os = zd->zd_os;
4885 uint64_t txg, last_txg;
4886 uint64_t value[ZTEST_ZAP_MAX_INTS];
4887 uint64_t zl_ints, zl_intsize, prop;
4890 char propname[100], txgname[100];
4892 char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
4894 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4895 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0, 0);
4897 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4898 !ztest_random(2)) != 0)
4901 object = od->od_object;
4904 * Generate a known hash collision, and verify that
4905 * we can lookup and remove both entries.
4907 tx = dmu_tx_create(os);
4908 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4909 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4912 for (i = 0; i < 2; i++) {
4914 VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
4917 for (i = 0; i < 2; i++) {
4918 VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
4919 sizeof (uint64_t), 1, &value[i], tx));
4921 zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
4922 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4923 ASSERT3U(zl_ints, ==, 1);
4925 for (i = 0; i < 2; i++) {
4926 VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
4931 * Generate a buch of random entries.
4933 ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
4935 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4936 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4937 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4938 bzero(value, sizeof (value));
4942 * If these zap entries already exist, validate their contents.
4944 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4946 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4947 ASSERT3U(zl_ints, ==, 1);
4949 VERIFY(zap_lookup(os, object, txgname, zl_intsize,
4950 zl_ints, &last_txg) == 0);
4952 VERIFY(zap_length(os, object, propname, &zl_intsize,
4955 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4956 ASSERT3U(zl_ints, ==, ints);
4958 VERIFY(zap_lookup(os, object, propname, zl_intsize,
4959 zl_ints, value) == 0);
4961 for (i = 0; i < ints; i++) {
4962 ASSERT3U(value[i], ==, last_txg + object + i);
4965 ASSERT3U(error, ==, ENOENT);
4969 * Atomically update two entries in our zap object.
4970 * The first is named txg_%llu, and contains the txg
4971 * in which the property was last updated. The second
4972 * is named prop_%llu, and the nth element of its value
4973 * should be txg + object + n.
4975 tx = dmu_tx_create(os);
4976 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4977 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4982 fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
4984 for (i = 0; i < ints; i++)
4985 value[i] = txg + object + i;
4987 VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
4989 VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
4995 * Remove a random pair of entries.
4997 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4998 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4999 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
5001 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
5003 if (error == ENOENT)
5008 tx = dmu_tx_create(os);
5009 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
5010 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
5013 VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
5014 VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
5017 umem_free(od, sizeof (ztest_od_t));
5021 * Testcase to test the upgrading of a microzap to fatzap.
5024 ztest_fzap(ztest_ds_t *zd, uint64_t id)
5026 objset_t *os = zd->zd_os;
5028 uint64_t object, txg;
5031 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
5032 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0, 0);
5034 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
5035 !ztest_random(2)) != 0)
5037 object = od->od_object;
5040 * Add entries to this ZAP and make sure it spills over
5041 * and gets upgraded to a fatzap. Also, since we are adding
5042 * 2050 entries we should see ptrtbl growth and leaf-block split.
5044 for (i = 0; i < 2050; i++) {
5045 char name[ZFS_MAX_DATASET_NAME_LEN];
5050 (void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
5051 (u_longlong_t)id, (u_longlong_t)value);
5053 tx = dmu_tx_create(os);
5054 dmu_tx_hold_zap(tx, object, B_TRUE, name);
5055 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
5058 error = zap_add(os, object, name, sizeof (uint64_t), 1,
5060 ASSERT(error == 0 || error == EEXIST);
5064 umem_free(od, sizeof (ztest_od_t));
5069 ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
5071 objset_t *os = zd->zd_os;
5073 uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
5075 int i, namelen, error;
5076 int micro = ztest_random(2);
5077 char name[20], string_value[20];
5080 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
5081 ztest_od_init(od, ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER, 0, 0, 0);
5083 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
5084 umem_free(od, sizeof (ztest_od_t));
5088 object = od->od_object;
5091 * Generate a random name of the form 'xxx.....' where each
5092 * x is a random printable character and the dots are dots.
5093 * There are 94 such characters, and the name length goes from
5094 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
5096 namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
5098 for (i = 0; i < 3; i++)
5099 name[i] = '!' + ztest_random('~' - '!' + 1);
5100 for (; i < namelen - 1; i++)
5104 if ((namelen & 1) || micro) {
5105 wsize = sizeof (txg);
5111 data = string_value;
5115 VERIFY0(zap_count(os, object, &count));
5116 ASSERT(count != -1ULL);
5119 * Select an operation: length, lookup, add, update, remove.
5121 i = ztest_random(5);
5124 tx = dmu_tx_create(os);
5125 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
5126 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
5128 umem_free(od, sizeof (ztest_od_t));
5131 bcopy(name, string_value, namelen);
5135 bzero(string_value, namelen);
5141 error = zap_length(os, object, name, &zl_wsize, &zl_wc);
5143 ASSERT3U(wsize, ==, zl_wsize);
5144 ASSERT3U(wc, ==, zl_wc);
5146 ASSERT3U(error, ==, ENOENT);
5151 error = zap_lookup(os, object, name, wsize, wc, data);
5153 if (data == string_value &&
5154 bcmp(name, data, namelen) != 0)
5155 fatal(0, "name '%s' != val '%s' len %d",
5156 name, data, namelen);
5158 ASSERT3U(error, ==, ENOENT);
5163 error = zap_add(os, object, name, wsize, wc, data, tx);
5164 ASSERT(error == 0 || error == EEXIST);
5168 VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
5172 error = zap_remove(os, object, name, tx);
5173 ASSERT(error == 0 || error == ENOENT);
5180 umem_free(od, sizeof (ztest_od_t));
5184 * Commit callback data.
5186 typedef struct ztest_cb_data {
5187 list_node_t zcd_node;
5189 int zcd_expected_err;
5190 boolean_t zcd_added;
5191 boolean_t zcd_called;
5195 /* This is the actual commit callback function */
5197 ztest_commit_callback(void *arg, int error)
5199 ztest_cb_data_t *data = arg;
5200 uint64_t synced_txg;
5202 VERIFY(data != NULL);
5203 VERIFY3S(data->zcd_expected_err, ==, error);
5204 VERIFY(!data->zcd_called);
5206 synced_txg = spa_last_synced_txg(data->zcd_spa);
5207 if (data->zcd_txg > synced_txg)
5208 fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
5209 ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
5212 data->zcd_called = B_TRUE;
5214 if (error == ECANCELED) {
5215 ASSERT0(data->zcd_txg);
5216 ASSERT(!data->zcd_added);
5219 * The private callback data should be destroyed here, but
5220 * since we are going to check the zcd_called field after
5221 * dmu_tx_abort(), we will destroy it there.
5226 ASSERT(data->zcd_added);
5227 ASSERT3U(data->zcd_txg, !=, 0);
5229 (void) mutex_enter(&zcl.zcl_callbacks_lock);
5231 /* See if this cb was called more quickly */
5232 if ((synced_txg - data->zcd_txg) < zc_min_txg_delay)
5233 zc_min_txg_delay = synced_txg - data->zcd_txg;
5235 /* Remove our callback from the list */
5236 list_remove(&zcl.zcl_callbacks, data);
5238 (void) mutex_exit(&zcl.zcl_callbacks_lock);
5240 umem_free(data, sizeof (ztest_cb_data_t));
5243 /* Allocate and initialize callback data structure */
5244 static ztest_cb_data_t *
5245 ztest_create_cb_data(objset_t *os, uint64_t txg)
5247 ztest_cb_data_t *cb_data;
5249 cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
5251 cb_data->zcd_txg = txg;
5252 cb_data->zcd_spa = dmu_objset_spa(os);
5253 list_link_init(&cb_data->zcd_node);
5259 * Commit callback test.
5262 ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
5264 objset_t *os = zd->zd_os;
5267 ztest_cb_data_t *cb_data[3], *tmp_cb;
5268 uint64_t old_txg, txg;
5271 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
5272 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
5274 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
5275 umem_free(od, sizeof (ztest_od_t));
5279 tx = dmu_tx_create(os);
5281 cb_data[0] = ztest_create_cb_data(os, 0);
5282 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
5284 dmu_tx_hold_write(tx, od->od_object, 0, sizeof (uint64_t));
5286 /* Every once in a while, abort the transaction on purpose */
5287 if (ztest_random(100) == 0)
5291 error = dmu_tx_assign(tx, TXG_NOWAIT);
5293 txg = error ? 0 : dmu_tx_get_txg(tx);
5295 cb_data[0]->zcd_txg = txg;
5296 cb_data[1] = ztest_create_cb_data(os, txg);
5297 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
5301 * It's not a strict requirement to call the registered
5302 * callbacks from inside dmu_tx_abort(), but that's what
5303 * it's supposed to happen in the current implementation
5304 * so we will check for that.
5306 for (i = 0; i < 2; i++) {
5307 cb_data[i]->zcd_expected_err = ECANCELED;
5308 VERIFY(!cb_data[i]->zcd_called);
5313 for (i = 0; i < 2; i++) {
5314 VERIFY(cb_data[i]->zcd_called);
5315 umem_free(cb_data[i], sizeof (ztest_cb_data_t));
5318 umem_free(od, sizeof (ztest_od_t));
5322 cb_data[2] = ztest_create_cb_data(os, txg);
5323 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
5326 * Read existing data to make sure there isn't a future leak.
5328 VERIFY(0 == dmu_read(os, od->od_object, 0, sizeof (uint64_t),
5329 &old_txg, DMU_READ_PREFETCH));
5332 fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
5335 dmu_write(os, od->od_object, 0, sizeof (uint64_t), &txg, tx);
5337 (void) mutex_enter(&zcl.zcl_callbacks_lock);
5340 * Since commit callbacks don't have any ordering requirement and since
5341 * it is theoretically possible for a commit callback to be called
5342 * after an arbitrary amount of time has elapsed since its txg has been
5343 * synced, it is difficult to reliably determine whether a commit
5344 * callback hasn't been called due to high load or due to a flawed
5347 * In practice, we will assume that if after a certain number of txgs a
5348 * commit callback hasn't been called, then most likely there's an
5349 * implementation bug..
5351 tmp_cb = list_head(&zcl.zcl_callbacks);
5352 if (tmp_cb != NULL &&
5353 tmp_cb->zcd_txg + ZTEST_COMMIT_CB_THRESH < txg) {
5354 fatal(0, "Commit callback threshold exceeded, oldest txg: %"
5355 PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
5359 * Let's find the place to insert our callbacks.
5361 * Even though the list is ordered by txg, it is possible for the
5362 * insertion point to not be the end because our txg may already be
5363 * quiescing at this point and other callbacks in the open txg
5364 * (from other objsets) may have sneaked in.
5366 tmp_cb = list_tail(&zcl.zcl_callbacks);
5367 while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
5368 tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
5370 /* Add the 3 callbacks to the list */
5371 for (i = 0; i < 3; i++) {
5373 list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
5375 list_insert_after(&zcl.zcl_callbacks, tmp_cb,
5378 cb_data[i]->zcd_added = B_TRUE;
5379 VERIFY(!cb_data[i]->zcd_called);
5381 tmp_cb = cb_data[i];
5386 (void) mutex_exit(&zcl.zcl_callbacks_lock);
5390 umem_free(od, sizeof (ztest_od_t));
5394 * Visit each object in the dataset. Verify that its properties
5395 * are consistent what was stored in the block tag when it was created,
5396 * and that its unused bonus buffer space has not been overwritten.
5400 ztest_verify_dnode_bt(ztest_ds_t *zd, uint64_t id)
5402 objset_t *os = zd->zd_os;
5406 for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
5407 ztest_block_tag_t *bt = NULL;
5408 dmu_object_info_t doi;
5411 ztest_object_lock(zd, obj, RL_READER);
5412 if (dmu_bonus_hold(os, obj, FTAG, &db) != 0) {
5413 ztest_object_unlock(zd, obj);
5417 dmu_object_info_from_db(db, &doi);
5418 if (doi.doi_bonus_size >= sizeof (*bt))
5419 bt = ztest_bt_bonus(db);
5421 if (bt && bt->bt_magic == BT_MAGIC) {
5422 ztest_bt_verify(bt, os, obj, doi.doi_dnodesize,
5423 bt->bt_offset, bt->bt_gen, bt->bt_txg,
5425 ztest_verify_unused_bonus(db, bt, obj, os, bt->bt_gen);
5428 dmu_buf_rele(db, FTAG);
5429 ztest_object_unlock(zd, obj);
5435 ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
5437 zfs_prop_t proplist[] = {
5439 ZFS_PROP_COMPRESSION,
5445 (void) pthread_rwlock_rdlock(&ztest_name_lock);
5447 for (p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
5448 (void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
5449 ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
5451 VERIFY0(ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_RECORDSIZE,
5452 ztest_random_blocksize(), (int)ztest_random(2)));
5454 (void) pthread_rwlock_unlock(&ztest_name_lock);
5459 ztest_remap_blocks(ztest_ds_t *zd, uint64_t id)
5461 (void) pthread_rwlock_rdlock(&ztest_name_lock);
5463 int error = dmu_objset_remap_indirects(zd->zd_name);
5464 if (error == ENOSPC)
5468 (void) pthread_rwlock_unlock(&ztest_name_lock);
5473 ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
5475 nvlist_t *props = NULL;
5477 (void) pthread_rwlock_rdlock(&ztest_name_lock);
5479 (void) ztest_spa_prop_set_uint64(ZPOOL_PROP_DEDUPDITTO,
5480 ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
5482 VERIFY0(spa_prop_get(ztest_spa, &props));
5484 if (ztest_opts.zo_verbose >= 6)
5485 dump_nvlist(props, 4);
5489 (void) pthread_rwlock_unlock(&ztest_name_lock);
5493 user_release_one(const char *snapname, const char *holdname)
5495 nvlist_t *snaps, *holds;
5498 snaps = fnvlist_alloc();
5499 holds = fnvlist_alloc();
5500 fnvlist_add_boolean(holds, holdname);
5501 fnvlist_add_nvlist(snaps, snapname, holds);
5502 fnvlist_free(holds);
5503 error = dsl_dataset_user_release(snaps, NULL);
5504 fnvlist_free(snaps);
5509 * Test snapshot hold/release and deferred destroy.
5512 ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
5515 objset_t *os = zd->zd_os;
5519 char clonename[100];
5521 char osname[ZFS_MAX_DATASET_NAME_LEN];
5524 (void) pthread_rwlock_rdlock(&ztest_name_lock);
5526 dmu_objset_name(os, osname);
5528 (void) snprintf(snapname, sizeof (snapname), "sh1_%llu",
5530 (void) snprintf(fullname, sizeof (fullname), "%s@%s", osname, snapname);
5531 (void) snprintf(clonename, sizeof (clonename),
5532 "%s/ch1_%llu", osname, (u_longlong_t)id);
5533 (void) snprintf(tag, sizeof (tag), "tag_%llu", (u_longlong_t)id);
5536 * Clean up from any previous run.
5538 error = dsl_destroy_head(clonename);
5539 if (error != ENOENT)
5541 error = user_release_one(fullname, tag);
5542 if (error != ESRCH && error != ENOENT)
5544 error = dsl_destroy_snapshot(fullname, B_FALSE);
5545 if (error != ENOENT)
5549 * Create snapshot, clone it, mark snap for deferred destroy,
5550 * destroy clone, verify snap was also destroyed.
5552 error = dmu_objset_snapshot_one(osname, snapname);
5554 if (error == ENOSPC) {
5555 ztest_record_enospc("dmu_objset_snapshot");
5558 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
5561 error = dmu_objset_clone(clonename, fullname);
5563 if (error == ENOSPC) {
5564 ztest_record_enospc("dmu_objset_clone");
5567 fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
5570 error = dsl_destroy_snapshot(fullname, B_TRUE);
5572 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5576 error = dsl_destroy_head(clonename);
5578 fatal(0, "dsl_destroy_head(%s) = %d", clonename, error);
5580 error = dmu_objset_hold(fullname, FTAG, &origin);
5581 if (error != ENOENT)
5582 fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
5585 * Create snapshot, add temporary hold, verify that we can't
5586 * destroy a held snapshot, mark for deferred destroy,
5587 * release hold, verify snapshot was destroyed.
5589 error = dmu_objset_snapshot_one(osname, snapname);
5591 if (error == ENOSPC) {
5592 ztest_record_enospc("dmu_objset_snapshot");
5595 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
5598 holds = fnvlist_alloc();
5599 fnvlist_add_string(holds, fullname, tag);
5600 error = dsl_dataset_user_hold(holds, 0, NULL);
5601 fnvlist_free(holds);
5603 if (error == ENOSPC) {
5604 ztest_record_enospc("dsl_dataset_user_hold");
5607 fatal(0, "dsl_dataset_user_hold(%s, %s) = %u",
5608 fullname, tag, error);
5611 error = dsl_destroy_snapshot(fullname, B_FALSE);
5612 if (error != EBUSY) {
5613 fatal(0, "dsl_destroy_snapshot(%s, B_FALSE) = %d",
5617 error = dsl_destroy_snapshot(fullname, B_TRUE);
5619 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5623 error = user_release_one(fullname, tag);
5625 fatal(0, "user_release_one(%s, %s) = %d", fullname, tag, error);
5627 VERIFY3U(dmu_objset_hold(fullname, FTAG, &origin), ==, ENOENT);
5630 (void) pthread_rwlock_unlock(&ztest_name_lock);
5634 * Inject random faults into the on-disk data.
5638 ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
5640 ztest_shared_t *zs = ztest_shared;
5641 spa_t *spa = ztest_spa;
5645 uint64_t bad = 0x1990c0ffeedecadeull;
5650 int bshift = SPA_MAXBLOCKSHIFT + 2;
5656 boolean_t islog = B_FALSE;
5658 path0 = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
5659 pathrand = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
5661 mutex_enter(&ztest_vdev_lock);
5664 * Device removal is in progress, fault injection must be disabled
5665 * until it completes and the pool is scrubbed. The fault injection
5666 * strategy for damaging blocks does not take in to account evacuated
5667 * blocks which may have already been damaged.
5669 if (ztest_device_removal_active) {
5670 mutex_exit(&ztest_vdev_lock);
5674 maxfaults = MAXFAULTS(zs);
5675 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
5676 mirror_save = zs->zs_mirrors;
5677 mutex_exit(&ztest_vdev_lock);
5679 ASSERT(leaves >= 1);
5682 * Grab the name lock as reader. There are some operations
5683 * which don't like to have their vdevs changed while
5684 * they are in progress (i.e. spa_change_guid). Those
5685 * operations will have grabbed the name lock as writer.
5687 (void) pthread_rwlock_rdlock(&ztest_name_lock);
5690 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
5692 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5694 if (ztest_random(2) == 0) {
5696 * Inject errors on a normal data device or slog device.
5698 top = ztest_random_vdev_top(spa, B_TRUE);
5699 leaf = ztest_random(leaves) + zs->zs_splits;
5702 * Generate paths to the first leaf in this top-level vdev,
5703 * and to the random leaf we selected. We'll induce transient
5704 * write failures and random online/offline activity on leaf 0,
5705 * and we'll write random garbage to the randomly chosen leaf.
5707 (void) snprintf(path0, MAXPATHLEN, ztest_dev_template,
5708 ztest_opts.zo_dir, ztest_opts.zo_pool,
5709 top * leaves + zs->zs_splits);
5710 (void) snprintf(pathrand, MAXPATHLEN, ztest_dev_template,
5711 ztest_opts.zo_dir, ztest_opts.zo_pool,
5712 top * leaves + leaf);
5714 vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
5715 if (vd0 != NULL && vd0->vdev_top->vdev_islog)
5719 * If the top-level vdev needs to be resilvered
5720 * then we only allow faults on the device that is
5723 if (vd0 != NULL && maxfaults != 1 &&
5724 (!vdev_resilver_needed(vd0->vdev_top, NULL, NULL) ||
5725 vd0->vdev_resilver_txg != 0)) {
5727 * Make vd0 explicitly claim to be unreadable,
5728 * or unwriteable, or reach behind its back
5729 * and close the underlying fd. We can do this if
5730 * maxfaults == 0 because we'll fail and reexecute,
5731 * and we can do it if maxfaults >= 2 because we'll
5732 * have enough redundancy. If maxfaults == 1, the
5733 * combination of this with injection of random data
5734 * corruption below exceeds the pool's fault tolerance.
5736 vdev_file_t *vf = vd0->vdev_tsd;
5738 zfs_dbgmsg("injecting fault to vdev %llu; maxfaults=%d",
5739 (long long)vd0->vdev_id, (int)maxfaults);
5741 if (vf != NULL && ztest_random(3) == 0) {
5742 (void) close(vf->vf_vnode->v_fd);
5743 vf->vf_vnode->v_fd = -1;
5744 } else if (ztest_random(2) == 0) {
5745 vd0->vdev_cant_read = B_TRUE;
5747 vd0->vdev_cant_write = B_TRUE;
5749 guid0 = vd0->vdev_guid;
5753 * Inject errors on an l2cache device.
5755 spa_aux_vdev_t *sav = &spa->spa_l2cache;
5757 if (sav->sav_count == 0) {
5758 spa_config_exit(spa, SCL_STATE, FTAG);
5759 (void) pthread_rwlock_unlock(&ztest_name_lock);
5762 vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
5763 guid0 = vd0->vdev_guid;
5764 (void) strcpy(path0, vd0->vdev_path);
5765 (void) strcpy(pathrand, vd0->vdev_path);
5769 maxfaults = INT_MAX; /* no limit on cache devices */
5772 spa_config_exit(spa, SCL_STATE, FTAG);
5773 (void) pthread_rwlock_unlock(&ztest_name_lock);
5776 * If we can tolerate two or more faults, or we're dealing
5777 * with a slog, randomly online/offline vd0.
5779 if ((maxfaults >= 2 || islog) && guid0 != 0) {
5780 if (ztest_random(10) < 6) {
5781 int flags = (ztest_random(2) == 0 ?
5782 ZFS_OFFLINE_TEMPORARY : 0);
5785 * We have to grab the zs_name_lock as writer to
5786 * prevent a race between offlining a slog and
5787 * destroying a dataset. Offlining the slog will
5788 * grab a reference on the dataset which may cause
5789 * dsl_destroy_head() to fail with EBUSY thus
5790 * leaving the dataset in an inconsistent state.
5793 (void) pthread_rwlock_wrlock(&ztest_name_lock);
5795 VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
5798 (void) pthread_rwlock_unlock(&ztest_name_lock);
5801 * Ideally we would like to be able to randomly
5802 * call vdev_[on|off]line without holding locks
5803 * to force unpredictable failures but the side
5804 * effects of vdev_[on|off]line prevent us from
5805 * doing so. We grab the ztest_vdev_lock here to
5806 * prevent a race between injection testing and
5809 mutex_enter(&ztest_vdev_lock);
5810 (void) vdev_online(spa, guid0, 0, NULL);
5811 mutex_exit(&ztest_vdev_lock);
5819 * We have at least single-fault tolerance, so inject data corruption.
5821 fd = open(pathrand, O_RDWR);
5823 if (fd == -1) /* we hit a gap in the device namespace */
5826 fsize = lseek(fd, 0, SEEK_END);
5828 while (--iters != 0) {
5830 * The offset must be chosen carefully to ensure that
5831 * we do not inject a given logical block with errors
5832 * on two different leaf devices, because ZFS can not
5833 * tolerate that (if maxfaults==1).
5835 * We divide each leaf into chunks of size
5836 * (# leaves * SPA_MAXBLOCKSIZE * 4). Within each chunk
5837 * there is a series of ranges to which we can inject errors.
5838 * Each range can accept errors on only a single leaf vdev.
5839 * The error injection ranges are separated by ranges
5840 * which we will not inject errors on any device (DMZs).
5841 * Each DMZ must be large enough such that a single block
5842 * can not straddle it, so that a single block can not be
5843 * a target in two different injection ranges (on different
5846 * For example, with 3 leaves, each chunk looks like:
5847 * 0 to 32M: injection range for leaf 0
5848 * 32M to 64M: DMZ - no injection allowed
5849 * 64M to 96M: injection range for leaf 1
5850 * 96M to 128M: DMZ - no injection allowed
5851 * 128M to 160M: injection range for leaf 2
5852 * 160M to 192M: DMZ - no injection allowed
5854 offset = ztest_random(fsize / (leaves << bshift)) *
5855 (leaves << bshift) + (leaf << bshift) +
5856 (ztest_random(1ULL << (bshift - 1)) & -8ULL);
5859 * Only allow damage to the labels at one end of the vdev.
5861 * If all labels are damaged, the device will be totally
5862 * inaccessible, which will result in loss of data,
5863 * because we also damage (parts of) the other side of
5866 * Additionally, we will always have both an even and an
5867 * odd label, so that we can handle crashes in the
5868 * middle of vdev_config_sync().
5870 if ((leaf & 1) == 0 && offset < VDEV_LABEL_START_SIZE)
5874 * The two end labels are stored at the "end" of the disk, but
5875 * the end of the disk (vdev_psize) is aligned to
5876 * sizeof (vdev_label_t).
5878 uint64_t psize = P2ALIGN(fsize, sizeof (vdev_label_t));
5879 if ((leaf & 1) == 1 &&
5880 offset + sizeof (bad) > psize - VDEV_LABEL_END_SIZE)
5883 mutex_enter(&ztest_vdev_lock);
5884 if (mirror_save != zs->zs_mirrors) {
5885 mutex_exit(&ztest_vdev_lock);
5890 if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
5891 fatal(1, "can't inject bad word at 0x%llx in %s",
5894 mutex_exit(&ztest_vdev_lock);
5896 if (ztest_opts.zo_verbose >= 7)
5897 (void) printf("injected bad word into %s,"
5898 " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
5903 umem_free(path0, MAXPATHLEN);
5904 umem_free(pathrand, MAXPATHLEN);
5908 * Verify that DDT repair works as expected.
5911 ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
5913 ztest_shared_t *zs = ztest_shared;
5914 spa_t *spa = ztest_spa;
5915 objset_t *os = zd->zd_os;
5917 uint64_t object, blocksize, txg, pattern, psize;
5918 enum zio_checksum checksum = spa_dedup_checksum(spa);
5923 int copies = 2 * ZIO_DEDUPDITTO_MIN;
5926 blocksize = ztest_random_blocksize();
5927 blocksize = MIN(blocksize, 2048); /* because we write so many */
5929 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
5930 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0, 0);
5932 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
5933 umem_free(od, sizeof (ztest_od_t));
5938 * Take the name lock as writer to prevent anyone else from changing
5939 * the pool and dataset properies we need to maintain during this test.
5941 (void) pthread_rwlock_wrlock(&ztest_name_lock);
5943 if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
5945 ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
5947 (void) pthread_rwlock_unlock(&ztest_name_lock);
5948 umem_free(od, sizeof (ztest_od_t));
5952 dmu_objset_stats_t dds;
5953 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
5954 dmu_objset_fast_stat(os, &dds);
5955 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
5957 object = od[0].od_object;
5958 blocksize = od[0].od_blocksize;
5959 pattern = zs->zs_guid ^ dds.dds_guid;
5961 ASSERT(object != 0);
5963 tx = dmu_tx_create(os);
5964 dmu_tx_hold_write(tx, object, 0, copies * blocksize);
5965 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
5967 (void) pthread_rwlock_unlock(&ztest_name_lock);
5968 umem_free(od, sizeof (ztest_od_t));
5973 * Write all the copies of our block.
5975 for (i = 0; i < copies; i++) {
5976 uint64_t offset = i * blocksize;
5977 int error = dmu_buf_hold(os, object, offset, FTAG, &db,
5978 DMU_READ_NO_PREFETCH);
5980 fatal(B_FALSE, "dmu_buf_hold(%p, %llu, %llu) = %u",
5981 os, (long long)object, (long long) offset, error);
5983 ASSERT(db->db_offset == offset);
5984 ASSERT(db->db_size == blocksize);
5985 ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
5986 ztest_pattern_match(db->db_data, db->db_size, 0ULL));
5987 dmu_buf_will_fill(db, tx);
5988 ztest_pattern_set(db->db_data, db->db_size, pattern);
5989 dmu_buf_rele(db, FTAG);
5993 txg_wait_synced(spa_get_dsl(spa), txg);
5996 * Find out what block we got.
5998 VERIFY0(dmu_buf_hold(os, object, 0, FTAG, &db,
5999 DMU_READ_NO_PREFETCH));
6000 blk = *((dmu_buf_impl_t *)db)->db_blkptr;
6001 dmu_buf_rele(db, FTAG);
6004 * Damage the block. Dedup-ditto will save us when we read it later.
6006 psize = BP_GET_PSIZE(&blk);
6007 abd = abd_alloc_linear(psize, B_TRUE);
6008 ztest_pattern_set(abd_to_buf(abd), psize, ~pattern);
6010 (void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
6011 abd, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
6012 ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
6016 (void) pthread_rwlock_unlock(&ztest_name_lock);
6017 umem_free(od, sizeof (ztest_od_t));
6025 ztest_scrub(ztest_ds_t *zd, uint64_t id)
6027 spa_t *spa = ztest_spa;
6030 * Scrub in progress by device removal.
6032 if (ztest_device_removal_active)
6035 (void) spa_scan(spa, POOL_SCAN_SCRUB);
6036 (void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
6037 (void) spa_scan(spa, POOL_SCAN_SCRUB);
6041 * Change the guid for the pool.
6045 ztest_reguid(ztest_ds_t *zd, uint64_t id)
6047 spa_t *spa = ztest_spa;
6048 uint64_t orig, load;
6051 if (ztest_opts.zo_mmp_test)
6054 orig = spa_guid(spa);
6055 load = spa_load_guid(spa);
6057 (void) pthread_rwlock_wrlock(&ztest_name_lock);
6058 error = spa_change_guid(spa);
6059 (void) pthread_rwlock_unlock(&ztest_name_lock);
6064 if (ztest_opts.zo_verbose >= 4) {
6065 (void) printf("Changed guid old %llu -> %llu\n",
6066 (u_longlong_t)orig, (u_longlong_t)spa_guid(spa));
6069 VERIFY3U(orig, !=, spa_guid(spa));
6070 VERIFY3U(load, ==, spa_load_guid(spa));
6074 * Rename the pool to a different name and then rename it back.
6078 ztest_spa_rename(ztest_ds_t *zd, uint64_t id)
6080 char *oldname, *newname;
6083 if (ztest_opts.zo_mmp_test)
6086 (void) pthread_rwlock_wrlock(&ztest_name_lock);
6088 oldname = ztest_opts.zo_pool;
6089 newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
6090 (void) strcpy(newname, oldname);
6091 (void) strcat(newname, "_tmp");
6096 VERIFY3U(0, ==, spa_rename(oldname, newname));
6099 * Try to open it under the old name, which shouldn't exist
6101 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
6104 * Open it under the new name and make sure it's still the same spa_t.
6106 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
6108 ASSERT(spa == ztest_spa);
6109 spa_close(spa, FTAG);
6112 * Rename it back to the original
6114 VERIFY3U(0, ==, spa_rename(newname, oldname));
6117 * Make sure it can still be opened
6119 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
6121 ASSERT(spa == ztest_spa);
6122 spa_close(spa, FTAG);
6124 umem_free(newname, strlen(newname) + 1);
6126 (void) pthread_rwlock_unlock(&ztest_name_lock);
6130 ztest_fletcher(ztest_ds_t *zd, uint64_t id)
6132 hrtime_t end = gethrtime() + NANOSEC;
6134 while (gethrtime() <= end) {
6135 int run_count = 100;
6137 struct abd *abd_data, *abd_meta;
6142 zio_cksum_t zc_ref_byteswap;
6144 size = ztest_random_blocksize();
6146 buf = umem_alloc(size, UMEM_NOFAIL);
6147 abd_data = abd_alloc(size, B_FALSE);
6148 abd_meta = abd_alloc(size, B_TRUE);
6150 for (i = 0, ptr = buf; i < size / sizeof (*ptr); i++, ptr++)
6151 *ptr = ztest_random(UINT_MAX);
6153 abd_copy_from_buf_off(abd_data, buf, 0, size);
6154 abd_copy_from_buf_off(abd_meta, buf, 0, size);
6156 VERIFY0(fletcher_4_impl_set("scalar"));
6157 fletcher_4_native(buf, size, NULL, &zc_ref);
6158 fletcher_4_byteswap(buf, size, NULL, &zc_ref_byteswap);
6160 VERIFY0(fletcher_4_impl_set("cycle"));
6161 while (run_count-- > 0) {
6163 zio_cksum_t zc_byteswap;
6165 fletcher_4_byteswap(buf, size, NULL, &zc_byteswap);
6166 fletcher_4_native(buf, size, NULL, &zc);
6168 VERIFY0(bcmp(&zc, &zc_ref, sizeof (zc)));
6169 VERIFY0(bcmp(&zc_byteswap, &zc_ref_byteswap,
6170 sizeof (zc_byteswap)));
6172 /* Test ABD - data */
6173 abd_fletcher_4_byteswap(abd_data, size, NULL,
6175 abd_fletcher_4_native(abd_data, size, NULL, &zc);
6177 VERIFY0(bcmp(&zc, &zc_ref, sizeof (zc)));
6178 VERIFY0(bcmp(&zc_byteswap, &zc_ref_byteswap,
6179 sizeof (zc_byteswap)));
6181 /* Test ABD - metadata */
6182 abd_fletcher_4_byteswap(abd_meta, size, NULL,
6184 abd_fletcher_4_native(abd_meta, size, NULL, &zc);
6186 VERIFY0(bcmp(&zc, &zc_ref, sizeof (zc)));
6187 VERIFY0(bcmp(&zc_byteswap, &zc_ref_byteswap,
6188 sizeof (zc_byteswap)));
6192 umem_free(buf, size);
6199 ztest_fletcher_incr(ztest_ds_t *zd, uint64_t id)
6206 zio_cksum_t zc_ref_bswap;
6208 hrtime_t end = gethrtime() + NANOSEC;
6210 while (gethrtime() <= end) {
6211 int run_count = 100;
6213 size = ztest_random_blocksize();
6214 buf = umem_alloc(size, UMEM_NOFAIL);
6216 for (i = 0, ptr = buf; i < size / sizeof (*ptr); i++, ptr++)
6217 *ptr = ztest_random(UINT_MAX);
6219 VERIFY0(fletcher_4_impl_set("scalar"));
6220 fletcher_4_native(buf, size, NULL, &zc_ref);
6221 fletcher_4_byteswap(buf, size, NULL, &zc_ref_bswap);
6223 VERIFY0(fletcher_4_impl_set("cycle"));
6225 while (run_count-- > 0) {
6227 zio_cksum_t zc_bswap;
6230 ZIO_SET_CHECKSUM(&zc, 0, 0, 0, 0);
6231 ZIO_SET_CHECKSUM(&zc_bswap, 0, 0, 0, 0);
6233 while (pos < size) {
6234 size_t inc = 64 * ztest_random(size / 67);
6235 /* sometimes add few bytes to test non-simd */
6236 if (ztest_random(100) < 10)
6237 inc += P2ALIGN(ztest_random(64),
6240 if (inc > (size - pos))
6243 fletcher_4_incremental_native(buf + pos, inc,
6245 fletcher_4_incremental_byteswap(buf + pos, inc,
6251 VERIFY3U(pos, ==, size);
6253 VERIFY(ZIO_CHECKSUM_EQUAL(zc, zc_ref));
6254 VERIFY(ZIO_CHECKSUM_EQUAL(zc_bswap, zc_ref_bswap));
6257 * verify if incremental on the whole buffer is
6258 * equivalent to non-incremental version
6260 ZIO_SET_CHECKSUM(&zc, 0, 0, 0, 0);
6261 ZIO_SET_CHECKSUM(&zc_bswap, 0, 0, 0, 0);
6263 fletcher_4_incremental_native(buf, size, &zc);
6264 fletcher_4_incremental_byteswap(buf, size, &zc_bswap);
6266 VERIFY(ZIO_CHECKSUM_EQUAL(zc, zc_ref));
6267 VERIFY(ZIO_CHECKSUM_EQUAL(zc_bswap, zc_ref_bswap));
6270 umem_free(buf, size);
6275 ztest_check_path(char *path)
6278 /* return true on success */
6279 return (!stat(path, &s));
6283 ztest_get_zdb_bin(char *bin, int len)
6287 * Try to use ZDB_PATH and in-tree zdb path. If not successful, just
6288 * let popen to search through PATH.
6290 if ((zdb_path = getenv("ZDB_PATH"))) {
6291 strlcpy(bin, zdb_path, len); /* In env */
6292 if (!ztest_check_path(bin)) {
6293 ztest_dump_core = 0;
6294 fatal(1, "invalid ZDB_PATH '%s'", bin);
6299 VERIFY(realpath(getexecname(), bin) != NULL);
6300 if (strstr(bin, "/ztest/")) {
6301 strstr(bin, "/ztest/")[0] = '\0'; /* In-tree */
6302 strcat(bin, "/zdb/zdb");
6303 if (ztest_check_path(bin))
6310 * Verify pool integrity by running zdb.
6313 ztest_run_zdb(char *pool)
6319 const int len = MAXPATHLEN + MAXNAMELEN + 20;
6322 bin = umem_alloc(len, UMEM_NOFAIL);
6323 zdb = umem_alloc(len, UMEM_NOFAIL);
6324 zbuf = umem_alloc(1024, UMEM_NOFAIL);
6326 ztest_get_zdb_bin(bin, len);
6329 "%s -bcc%s%s -G -d -U %s "
6330 "-o zfs_reconstruct_indirect_combinations_max=1000000 %s",
6332 ztest_opts.zo_verbose >= 3 ? "s" : "",
6333 ztest_opts.zo_verbose >= 4 ? "v" : "",
6337 if (ztest_opts.zo_verbose >= 5)
6338 (void) printf("Executing %s\n", strstr(zdb, "zdb "));
6340 fp = popen(zdb, "r");
6342 while (fgets(zbuf, 1024, fp) != NULL)
6343 if (ztest_opts.zo_verbose >= 3)
6344 (void) printf("%s", zbuf);
6346 status = pclose(fp);
6351 ztest_dump_core = 0;
6352 if (WIFEXITED(status))
6353 fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
6355 fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
6357 umem_free(bin, len);
6358 umem_free(zdb, len);
6359 umem_free(zbuf, 1024);
6363 ztest_walk_pool_directory(char *header)
6367 if (ztest_opts.zo_verbose >= 6)
6368 (void) printf("%s\n", header);
6370 mutex_enter(&spa_namespace_lock);
6371 while ((spa = spa_next(spa)) != NULL)
6372 if (ztest_opts.zo_verbose >= 6)
6373 (void) printf("\t%s\n", spa_name(spa));
6374 mutex_exit(&spa_namespace_lock);
6378 ztest_spa_import_export(char *oldname, char *newname)
6380 nvlist_t *config, *newconfig;
6385 if (ztest_opts.zo_verbose >= 4) {
6386 (void) printf("import/export: old = %s, new = %s\n",
6391 * Clean up from previous runs.
6393 (void) spa_destroy(newname);
6396 * Get the pool's configuration and guid.
6398 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
6401 * Kick off a scrub to tickle scrub/export races.
6403 if (ztest_random(2) == 0)
6404 (void) spa_scan(spa, POOL_SCAN_SCRUB);
6406 pool_guid = spa_guid(spa);
6407 spa_close(spa, FTAG);
6409 ztest_walk_pool_directory("pools before export");
6414 VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
6416 ztest_walk_pool_directory("pools after export");
6421 newconfig = spa_tryimport(config);
6422 ASSERT(newconfig != NULL);
6423 nvlist_free(newconfig);
6426 * Import it under the new name.
6428 error = spa_import(newname, config, NULL, 0);
6430 dump_nvlist(config, 0);
6431 fatal(B_FALSE, "couldn't import pool %s as %s: error %u",
6432 oldname, newname, error);
6435 ztest_walk_pool_directory("pools after import");
6438 * Try to import it again -- should fail with EEXIST.
6440 VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL, 0));
6443 * Try to import it under a different name -- should fail with EEXIST.
6445 VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL, 0));
6448 * Verify that the pool is no longer visible under the old name.
6450 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
6453 * Verify that we can open and close the pool using the new name.
6455 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
6456 ASSERT(pool_guid == spa_guid(spa));
6457 spa_close(spa, FTAG);
6459 nvlist_free(config);
6463 ztest_resume(spa_t *spa)
6465 if (spa_suspended(spa) && ztest_opts.zo_verbose >= 6)
6466 (void) printf("resuming from suspended state\n");
6467 spa_vdev_state_enter(spa, SCL_NONE);
6468 vdev_clear(spa, NULL);
6469 (void) spa_vdev_state_exit(spa, NULL, 0);
6470 (void) zio_resume(spa);
6474 ztest_resume_thread(void *arg)
6478 while (!ztest_exiting) {
6479 if (spa_suspended(spa))
6481 (void) poll(NULL, 0, 100);
6484 * Periodically change the zfs_compressed_arc_enabled setting.
6486 if (ztest_random(10) == 0)
6487 zfs_compressed_arc_enabled = ztest_random(2);
6490 * Periodically change the zfs_abd_scatter_enabled setting.
6492 if (ztest_random(10) == 0)
6493 zfs_abd_scatter_enabled = ztest_random(2);
6500 ztest_deadman_thread(void *arg)
6502 ztest_shared_t *zs = arg;
6503 spa_t *spa = ztest_spa;
6504 hrtime_t delta, overdue, total = 0;
6507 delta = zs->zs_thread_stop - zs->zs_thread_start +
6508 MSEC2NSEC(zfs_deadman_synctime_ms);
6510 (void) poll(NULL, 0, (int)NSEC2MSEC(delta));
6513 * If the pool is suspended then fail immediately. Otherwise,
6514 * check to see if the pool is making any progress. If
6515 * vdev_deadman() discovers that there hasn't been any recent
6516 * I/Os then it will end up aborting the tests.
6518 if (spa_suspended(spa) || spa->spa_root_vdev == NULL) {
6519 fatal(0, "aborting test after %llu seconds because "
6520 "pool has transitioned to a suspended state.",
6521 zfs_deadman_synctime_ms / 1000);
6523 vdev_deadman(spa->spa_root_vdev, FTAG);
6526 * If the process doesn't complete within a grace period of
6527 * zfs_deadman_synctime_ms over the expected finish time,
6528 * then it may be hung and is terminated.
6530 overdue = zs->zs_proc_stop + MSEC2NSEC(zfs_deadman_synctime_ms);
6531 total += zfs_deadman_synctime_ms / 1000;
6532 if (gethrtime() > overdue) {
6533 fatal(0, "aborting test after %llu seconds because "
6534 "the process is overdue for termination.", total);
6537 (void) printf("ztest has been running for %lld seconds\n",
6543 ztest_execute(int test, ztest_info_t *zi, uint64_t id)
6545 ztest_ds_t *zd = &ztest_ds[id % ztest_opts.zo_datasets];
6546 ztest_shared_callstate_t *zc = ZTEST_GET_SHARED_CALLSTATE(test);
6547 hrtime_t functime = gethrtime();
6550 for (i = 0; i < zi->zi_iters; i++)
6551 zi->zi_func(zd, id);
6553 functime = gethrtime() - functime;
6555 atomic_add_64(&zc->zc_count, 1);
6556 atomic_add_64(&zc->zc_time, functime);
6558 if (ztest_opts.zo_verbose >= 4)
6559 (void) printf("%6.2f sec in %s\n",
6560 (double)functime / NANOSEC, zi->zi_funcname);
6564 ztest_thread(void *arg)
6567 uint64_t id = (uintptr_t)arg;
6568 ztest_shared_t *zs = ztest_shared;
6572 ztest_shared_callstate_t *zc;
6574 while ((now = gethrtime()) < zs->zs_thread_stop) {
6576 * See if it's time to force a crash.
6578 if (now > zs->zs_thread_kill)
6582 * If we're getting ENOSPC with some regularity, stop.
6584 if (zs->zs_enospc_count > 10)
6588 * Pick a random function to execute.
6590 rand = ztest_random(ZTEST_FUNCS);
6591 zi = &ztest_info[rand];
6592 zc = ZTEST_GET_SHARED_CALLSTATE(rand);
6593 call_next = zc->zc_next;
6595 if (now >= call_next &&
6596 atomic_cas_64(&zc->zc_next, call_next, call_next +
6597 ztest_random(2 * zi->zi_interval[0] + 1)) == call_next) {
6598 ztest_execute(rand, zi, id);
6606 ztest_dataset_name(char *dsname, char *pool, int d)
6608 (void) snprintf(dsname, ZFS_MAX_DATASET_NAME_LEN, "%s/ds_%d", pool, d);
6612 ztest_dataset_destroy(int d)
6614 char name[ZFS_MAX_DATASET_NAME_LEN];
6617 ztest_dataset_name(name, ztest_opts.zo_pool, d);
6619 if (ztest_opts.zo_verbose >= 3)
6620 (void) printf("Destroying %s to free up space\n", name);
6623 * Cleanup any non-standard clones and snapshots. In general,
6624 * ztest thread t operates on dataset (t % zopt_datasets),
6625 * so there may be more than one thing to clean up.
6627 for (t = d; t < ztest_opts.zo_threads;
6628 t += ztest_opts.zo_datasets)
6629 ztest_dsl_dataset_cleanup(name, t);
6631 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
6632 DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
6636 ztest_dataset_dirobj_verify(ztest_ds_t *zd)
6638 uint64_t usedobjs, dirobjs, scratch;
6641 * ZTEST_DIROBJ is the object directory for the entire dataset.
6642 * Therefore, the number of objects in use should equal the
6643 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
6644 * If not, we have an object leak.
6646 * Note that we can only check this in ztest_dataset_open(),
6647 * when the open-context and syncing-context values agree.
6648 * That's because zap_count() returns the open-context value,
6649 * while dmu_objset_space() returns the rootbp fill count.
6651 VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
6652 dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
6653 ASSERT3U(dirobjs + 1, ==, usedobjs);
6657 ztest_dataset_open(int d)
6659 ztest_ds_t *zd = &ztest_ds[d];
6660 uint64_t committed_seq = ZTEST_GET_SHARED_DS(d)->zd_seq;
6663 char name[ZFS_MAX_DATASET_NAME_LEN];
6666 ztest_dataset_name(name, ztest_opts.zo_pool, d);
6668 (void) pthread_rwlock_rdlock(&ztest_name_lock);
6670 error = ztest_dataset_create(name);
6671 if (error == ENOSPC) {
6672 (void) pthread_rwlock_unlock(&ztest_name_lock);
6673 ztest_record_enospc(FTAG);
6676 ASSERT(error == 0 || error == EEXIST);
6678 VERIFY0(ztest_dmu_objset_own(name, DMU_OST_OTHER, B_FALSE,
6680 (void) pthread_rwlock_unlock(&ztest_name_lock);
6682 ztest_zd_init(zd, ZTEST_GET_SHARED_DS(d), os);
6684 zilog = zd->zd_zilog;
6686 if (zilog->zl_header->zh_claim_lr_seq != 0 &&
6687 zilog->zl_header->zh_claim_lr_seq < committed_seq)
6688 fatal(0, "missing log records: claimed %llu < committed %llu",
6689 zilog->zl_header->zh_claim_lr_seq, committed_seq);
6691 ztest_dataset_dirobj_verify(zd);
6693 zil_replay(os, zd, ztest_replay_vector);
6695 ztest_dataset_dirobj_verify(zd);
6697 if (ztest_opts.zo_verbose >= 6)
6698 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
6700 (u_longlong_t)zilog->zl_parse_blk_count,
6701 (u_longlong_t)zilog->zl_parse_lr_count,
6702 (u_longlong_t)zilog->zl_replaying_seq);
6704 zilog = zil_open(os, ztest_get_data);
6706 if (zilog->zl_replaying_seq != 0 &&
6707 zilog->zl_replaying_seq < committed_seq)
6708 fatal(0, "missing log records: replayed %llu < committed %llu",
6709 zilog->zl_replaying_seq, committed_seq);
6715 ztest_dataset_close(int d)
6717 ztest_ds_t *zd = &ztest_ds[d];
6719 zil_close(zd->zd_zilog);
6720 txg_wait_synced(spa_get_dsl(zd->zd_os->os_spa), 0);
6721 dmu_objset_disown(zd->zd_os, B_TRUE, zd);
6727 * Kick off threads to run tests on all datasets in parallel.
6730 ztest_run(ztest_shared_t *zs)
6734 kthread_t *resume_thread;
6735 kthread_t **run_threads;
6740 ztest_exiting = B_FALSE;
6743 * Initialize parent/child shared state.
6745 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
6746 mutex_init(&ztest_checkpoint_lock, NULL, MUTEX_DEFAULT, NULL);
6747 VERIFY0(pthread_rwlock_init(&ztest_name_lock, NULL));
6749 zs->zs_thread_start = gethrtime();
6750 zs->zs_thread_stop =
6751 zs->zs_thread_start + ztest_opts.zo_passtime * NANOSEC;
6752 zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
6753 zs->zs_thread_kill = zs->zs_thread_stop;
6754 if (ztest_random(100) < ztest_opts.zo_killrate) {
6755 zs->zs_thread_kill -=
6756 ztest_random(ztest_opts.zo_passtime * NANOSEC);
6759 mutex_init(&zcl.zcl_callbacks_lock, NULL, MUTEX_DEFAULT, NULL);
6761 list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
6762 offsetof(ztest_cb_data_t, zcd_node));
6767 kernel_init(FREAD | FWRITE);
6768 VERIFY0(spa_open(ztest_opts.zo_pool, &spa, FTAG));
6769 metaslab_preload_limit = ztest_random(20) + 1;
6772 dmu_objset_stats_t dds;
6773 VERIFY0(ztest_dmu_objset_own(ztest_opts.zo_pool,
6774 DMU_OST_ANY, B_TRUE, B_TRUE, FTAG, &os));
6775 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
6776 dmu_objset_fast_stat(os, &dds);
6777 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
6778 zs->zs_guid = dds.dds_guid;
6779 dmu_objset_disown(os, B_TRUE, FTAG);
6781 spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
6784 * Create a thread to periodically resume suspended I/O.
6786 resume_thread = thread_create(NULL, 0, ztest_resume_thread,
6787 spa, 0, NULL, TS_RUN | TS_JOINABLE, defclsyspri);
6790 * Create a deadman thread and set to panic if we hang.
6792 (void) thread_create(NULL, 0, ztest_deadman_thread,
6793 zs, 0, NULL, TS_RUN | TS_JOINABLE, defclsyspri);
6795 spa->spa_deadman_failmode = ZIO_FAILURE_MODE_PANIC;
6798 * Verify that we can safely inquire about any object,
6799 * whether it's allocated or not. To make it interesting,
6800 * we probe a 5-wide window around each power of two.
6801 * This hits all edge cases, including zero and the max.
6803 for (t = 0; t < 64; t++) {
6804 for (d = -5; d <= 5; d++) {
6805 error = dmu_object_info(spa->spa_meta_objset,
6806 (1ULL << t) + d, NULL);
6807 ASSERT(error == 0 || error == ENOENT ||
6813 * If we got any ENOSPC errors on the previous run, destroy something.
6815 if (zs->zs_enospc_count != 0) {
6816 int d = ztest_random(ztest_opts.zo_datasets);
6817 ztest_dataset_destroy(d);
6819 zs->zs_enospc_count = 0;
6821 run_threads = umem_zalloc(ztest_opts.zo_threads * sizeof (kthread_t *),
6824 if (ztest_opts.zo_verbose >= 4)
6825 (void) printf("starting main threads...\n");
6828 * Kick off all the tests that run in parallel.
6830 for (t = 0; t < ztest_opts.zo_threads; t++) {
6831 if (t < ztest_opts.zo_datasets && ztest_dataset_open(t) != 0) {
6832 umem_free(run_threads, ztest_opts.zo_threads *
6833 sizeof (kthread_t *));
6837 run_threads[t] = thread_create(NULL, 0, ztest_thread,
6838 (void *)(uintptr_t)t, 0, NULL, TS_RUN | TS_JOINABLE,
6843 * Wait for all of the tests to complete. We go in reverse order
6844 * so we don't close datasets while threads are still using them.
6846 for (t = ztest_opts.zo_threads - 1; t >= 0; t--) {
6847 VERIFY0(thread_join(run_threads[t]));
6848 if (t < ztest_opts.zo_datasets)
6849 ztest_dataset_close(t);
6852 txg_wait_synced(spa_get_dsl(spa), 0);
6854 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
6855 zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
6857 umem_free(run_threads, ztest_opts.zo_threads * sizeof (kthread_t *));
6859 /* Kill the resume thread */
6860 ztest_exiting = B_TRUE;
6861 VERIFY0(thread_join(resume_thread));
6865 * Right before closing the pool, kick off a bunch of async I/O;
6866 * spa_close() should wait for it to complete.
6868 for (object = 1; object < 50; object++) {
6869 dmu_prefetch(spa->spa_meta_objset, object, 0, 0, 1ULL << 20,
6870 ZIO_PRIORITY_SYNC_READ);
6873 /* Verify that at least one commit cb was called in a timely fashion */
6874 if (zc_cb_counter >= ZTEST_COMMIT_CB_MIN_REG)
6875 VERIFY0(zc_min_txg_delay);
6877 spa_close(spa, FTAG);
6880 * Verify that we can loop over all pools.
6882 mutex_enter(&spa_namespace_lock);
6883 for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
6884 if (ztest_opts.zo_verbose > 3)
6885 (void) printf("spa_next: found %s\n", spa_name(spa));
6886 mutex_exit(&spa_namespace_lock);
6889 * Verify that we can export the pool and reimport it under a
6892 if ((ztest_random(2) == 0) && !ztest_opts.zo_mmp_test) {
6893 char name[ZFS_MAX_DATASET_NAME_LEN];
6894 (void) snprintf(name, sizeof (name), "%s_import",
6895 ztest_opts.zo_pool);
6896 ztest_spa_import_export(ztest_opts.zo_pool, name);
6897 ztest_spa_import_export(name, ztest_opts.zo_pool);
6902 list_destroy(&zcl.zcl_callbacks);
6903 mutex_destroy(&zcl.zcl_callbacks_lock);
6904 (void) pthread_rwlock_destroy(&ztest_name_lock);
6905 mutex_destroy(&ztest_vdev_lock);
6906 mutex_destroy(&ztest_checkpoint_lock);
6912 ztest_ds_t *zd = &ztest_ds[0];
6916 if (ztest_opts.zo_verbose >= 3)
6917 (void) printf("testing spa_freeze()...\n");
6919 kernel_init(FREAD | FWRITE);
6920 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6921 VERIFY3U(0, ==, ztest_dataset_open(0));
6925 * Force the first log block to be transactionally allocated.
6926 * We have to do this before we freeze the pool -- otherwise
6927 * the log chain won't be anchored.
6929 while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
6930 ztest_dmu_object_alloc_free(zd, 0);
6931 zil_commit(zd->zd_zilog, 0);
6934 txg_wait_synced(spa_get_dsl(spa), 0);
6937 * Freeze the pool. This stops spa_sync() from doing anything,
6938 * so that the only way to record changes from now on is the ZIL.
6943 * Because it is hard to predict how much space a write will actually
6944 * require beforehand, we leave ourselves some fudge space to write over
6947 uint64_t capacity = metaslab_class_get_space(spa_normal_class(spa)) / 2;
6950 * Run tests that generate log records but don't alter the pool config
6951 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
6952 * We do a txg_wait_synced() after each iteration to force the txg
6953 * to increase well beyond the last synced value in the uberblock.
6954 * The ZIL should be OK with that.
6956 * Run a random number of times less than zo_maxloops and ensure we do
6957 * not run out of space on the pool.
6959 while (ztest_random(10) != 0 &&
6960 numloops++ < ztest_opts.zo_maxloops &&
6961 metaslab_class_get_alloc(spa_normal_class(spa)) < capacity) {
6963 ztest_od_init(&od, 0, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
6964 VERIFY0(ztest_object_init(zd, &od, sizeof (od), B_FALSE));
6965 ztest_io(zd, od.od_object,
6966 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
6967 txg_wait_synced(spa_get_dsl(spa), 0);
6971 * Commit all of the changes we just generated.
6973 zil_commit(zd->zd_zilog, 0);
6974 txg_wait_synced(spa_get_dsl(spa), 0);
6977 * Close our dataset and close the pool.
6979 ztest_dataset_close(0);
6980 spa_close(spa, FTAG);
6984 * Open and close the pool and dataset to induce log replay.
6986 kernel_init(FREAD | FWRITE);
6987 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6988 ASSERT(spa_freeze_txg(spa) == UINT64_MAX);
6989 VERIFY3U(0, ==, ztest_dataset_open(0));
6991 txg_wait_synced(spa_get_dsl(spa), 0);
6992 ztest_dataset_close(0);
6993 ztest_reguid(NULL, 0);
6995 spa_close(spa, FTAG);
7000 print_time(hrtime_t t, char *timebuf)
7002 hrtime_t s = t / NANOSEC;
7003 hrtime_t m = s / 60;
7004 hrtime_t h = m / 60;
7005 hrtime_t d = h / 24;
7014 (void) sprintf(timebuf,
7015 "%llud%02lluh%02llum%02llus", d, h, m, s);
7017 (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
7019 (void) sprintf(timebuf, "%llum%02llus", m, s);
7021 (void) sprintf(timebuf, "%llus", s);
7025 make_random_props(void)
7029 VERIFY0(nvlist_alloc(&props, NV_UNIQUE_NAME, 0));
7030 if (ztest_random(2) == 0)
7033 VERIFY0(nvlist_add_uint64(props,
7034 zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE), 1));
7040 * Import a storage pool with the given name.
7043 ztest_import(ztest_shared_t *zs)
7045 libzfs_handle_t *hdl;
7046 importargs_t args = { 0 };
7048 nvlist_t *cfg = NULL;
7050 char *searchdirs[nsearch];
7051 char *name = ztest_opts.zo_pool;
7052 int flags = ZFS_IMPORT_MISSING_LOG;
7055 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
7056 mutex_init(&ztest_checkpoint_lock, NULL, MUTEX_DEFAULT, NULL);
7057 VERIFY0(pthread_rwlock_init(&ztest_name_lock, NULL));
7059 kernel_init(FREAD | FWRITE);
7060 hdl = libzfs_init();
7062 searchdirs[0] = ztest_opts.zo_dir;
7063 args.paths = nsearch;
7064 args.path = searchdirs;
7065 args.can_be_active = B_FALSE;
7067 error = zpool_tryimport(hdl, name, &cfg, &args);
7069 (void) fatal(0, "No pools found\n");
7071 VERIFY0(spa_import(name, cfg, NULL, flags));
7072 VERIFY0(spa_open(name, &spa, FTAG));
7073 zs->zs_metaslab_sz =
7074 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
7075 spa_close(spa, FTAG);
7080 if (!ztest_opts.zo_mmp_test) {
7081 ztest_run_zdb(ztest_opts.zo_pool);
7083 ztest_run_zdb(ztest_opts.zo_pool);
7086 (void) pthread_rwlock_destroy(&ztest_name_lock);
7087 mutex_destroy(&ztest_vdev_lock);
7088 mutex_destroy(&ztest_checkpoint_lock);
7092 * Create a storage pool with the given name and initial vdev size.
7093 * Then test spa_freeze() functionality.
7096 ztest_init(ztest_shared_t *zs)
7099 nvlist_t *nvroot, *props;
7102 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
7103 mutex_init(&ztest_checkpoint_lock, NULL, MUTEX_DEFAULT, NULL);
7104 VERIFY0(pthread_rwlock_init(&ztest_name_lock, NULL));
7106 kernel_init(FREAD | FWRITE);
7109 * Create the storage pool.
7111 (void) spa_destroy(ztest_opts.zo_pool);
7112 ztest_shared->zs_vdev_next_leaf = 0;
7114 zs->zs_mirrors = ztest_opts.zo_mirrors;
7115 nvroot = make_vdev_root(NULL, NULL, NULL, ztest_opts.zo_vdev_size, 0,
7116 0, ztest_opts.zo_raidz, zs->zs_mirrors, 1);
7117 props = make_random_props();
7120 * We don't expect the pool to suspend unless maxfaults == 0,
7121 * in which case ztest_fault_inject() temporarily takes away
7122 * the only valid replica.
7124 VERIFY0(nvlist_add_uint64(props,
7125 zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE),
7126 MAXFAULTS(zs) ? ZIO_FAILURE_MODE_PANIC : ZIO_FAILURE_MODE_WAIT));
7128 for (i = 0; i < SPA_FEATURES; i++) {
7130 VERIFY3S(-1, !=, asprintf(&buf, "feature@%s",
7131 spa_feature_table[i].fi_uname));
7132 VERIFY3U(0, ==, nvlist_add_uint64(props, buf, 0));
7136 VERIFY0(spa_create(ztest_opts.zo_pool, nvroot, props, NULL, NULL));
7137 nvlist_free(nvroot);
7140 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
7141 zs->zs_metaslab_sz =
7142 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
7143 spa_close(spa, FTAG);
7147 if (!ztest_opts.zo_mmp_test) {
7148 ztest_run_zdb(ztest_opts.zo_pool);
7150 ztest_run_zdb(ztest_opts.zo_pool);
7153 (void) pthread_rwlock_destroy(&ztest_name_lock);
7154 mutex_destroy(&ztest_vdev_lock);
7155 mutex_destroy(&ztest_checkpoint_lock);
7161 static char ztest_name_data[] = "/tmp/ztest.data.XXXXXX";
7163 ztest_fd_data = mkstemp(ztest_name_data);
7164 ASSERT3S(ztest_fd_data, >=, 0);
7165 (void) unlink(ztest_name_data);
7169 shared_data_size(ztest_shared_hdr_t *hdr)
7173 size = hdr->zh_hdr_size;
7174 size += hdr->zh_opts_size;
7175 size += hdr->zh_size;
7176 size += hdr->zh_stats_size * hdr->zh_stats_count;
7177 size += hdr->zh_ds_size * hdr->zh_ds_count;
7186 ztest_shared_hdr_t *hdr;
7188 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
7189 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
7190 ASSERT(hdr != MAP_FAILED);
7192 VERIFY3U(0, ==, ftruncate(ztest_fd_data, sizeof (ztest_shared_hdr_t)));
7194 hdr->zh_hdr_size = sizeof (ztest_shared_hdr_t);
7195 hdr->zh_opts_size = sizeof (ztest_shared_opts_t);
7196 hdr->zh_size = sizeof (ztest_shared_t);
7197 hdr->zh_stats_size = sizeof (ztest_shared_callstate_t);
7198 hdr->zh_stats_count = ZTEST_FUNCS;
7199 hdr->zh_ds_size = sizeof (ztest_shared_ds_t);
7200 hdr->zh_ds_count = ztest_opts.zo_datasets;
7202 size = shared_data_size(hdr);
7203 VERIFY3U(0, ==, ftruncate(ztest_fd_data, size));
7205 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
7212 ztest_shared_hdr_t *hdr;
7215 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
7216 PROT_READ, MAP_SHARED, ztest_fd_data, 0);
7217 ASSERT(hdr != MAP_FAILED);
7219 size = shared_data_size(hdr);
7221 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
7222 hdr = ztest_shared_hdr = (void *)mmap(0, P2ROUNDUP(size, getpagesize()),
7223 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
7224 ASSERT(hdr != MAP_FAILED);
7225 buf = (uint8_t *)hdr;
7227 offset = hdr->zh_hdr_size;
7228 ztest_shared_opts = (void *)&buf[offset];
7229 offset += hdr->zh_opts_size;
7230 ztest_shared = (void *)&buf[offset];
7231 offset += hdr->zh_size;
7232 ztest_shared_callstate = (void *)&buf[offset];
7233 offset += hdr->zh_stats_size * hdr->zh_stats_count;
7234 ztest_shared_ds = (void *)&buf[offset];
7238 exec_child(char *cmd, char *libpath, boolean_t ignorekill, int *statusp)
7242 char *cmdbuf = NULL;
7247 cmdbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
7248 (void) strlcpy(cmdbuf, getexecname(), MAXPATHLEN);
7253 fatal(1, "fork failed");
7255 if (pid == 0) { /* child */
7256 char *emptyargv[2] = { cmd, NULL };
7257 char fd_data_str[12];
7259 struct rlimit rl = { 1024, 1024 };
7260 (void) setrlimit(RLIMIT_NOFILE, &rl);
7262 (void) close(ztest_fd_rand);
7263 VERIFY(11 >= snprintf(fd_data_str, 12, "%d", ztest_fd_data));
7264 VERIFY(0 == setenv("ZTEST_FD_DATA", fd_data_str, 1));
7266 (void) enable_extended_FILE_stdio(-1, -1);
7267 if (libpath != NULL)
7268 VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath, 1));
7269 (void) execv(cmd, emptyargv);
7270 ztest_dump_core = B_FALSE;
7271 fatal(B_TRUE, "exec failed: %s", cmd);
7274 if (cmdbuf != NULL) {
7275 umem_free(cmdbuf, MAXPATHLEN);
7279 while (waitpid(pid, &status, 0) != pid)
7281 if (statusp != NULL)
7284 if (WIFEXITED(status)) {
7285 if (WEXITSTATUS(status) != 0) {
7286 (void) fprintf(stderr, "child exited with code %d\n",
7287 WEXITSTATUS(status));
7291 } else if (WIFSIGNALED(status)) {
7292 if (!ignorekill || WTERMSIG(status) != SIGKILL) {
7293 (void) fprintf(stderr, "child died with signal %d\n",
7299 (void) fprintf(stderr, "something strange happened to child\n");
7306 ztest_run_init(void)
7310 ztest_shared_t *zs = ztest_shared;
7313 * Blow away any existing copy of zpool.cache
7315 (void) remove(spa_config_path);
7317 if (ztest_opts.zo_init == 0) {
7318 if (ztest_opts.zo_verbose >= 1)
7319 (void) printf("Importing pool %s\n",
7320 ztest_opts.zo_pool);
7326 * Create and initialize our storage pool.
7328 for (i = 1; i <= ztest_opts.zo_init; i++) {
7329 bzero(zs, sizeof (ztest_shared_t));
7330 if (ztest_opts.zo_verbose >= 3 &&
7331 ztest_opts.zo_init != 1) {
7332 (void) printf("ztest_init(), pass %d\n", i);
7339 main(int argc, char **argv)
7347 ztest_shared_callstate_t *zc;
7349 char numbuf[NN_NUMBUF_SZ];
7354 char *fd_data_str = getenv("ZTEST_FD_DATA");
7355 struct sigaction action;
7357 (void) setvbuf(stdout, NULL, _IOLBF, 0);
7359 dprintf_setup(&argc, argv);
7360 zfs_deadman_synctime_ms = 300000;
7362 * As two-word space map entries may not come up often (especially
7363 * if pool and vdev sizes are small) we want to force at least some
7364 * of them so the feature get tested.
7366 zfs_force_some_double_word_sm_entries = B_TRUE;
7368 action.sa_handler = sig_handler;
7369 sigemptyset(&action.sa_mask);
7370 action.sa_flags = 0;
7372 if (sigaction(SIGSEGV, &action, NULL) < 0) {
7373 (void) fprintf(stderr, "ztest: cannot catch SIGSEGV: %s.\n",
7378 if (sigaction(SIGABRT, &action, NULL) < 0) {
7379 (void) fprintf(stderr, "ztest: cannot catch SIGABRT: %s.\n",
7385 * Force random_get_bytes() to use /dev/urandom in order to prevent
7386 * ztest from needlessly depleting the system entropy pool.
7388 random_path = "/dev/urandom";
7389 ztest_fd_rand = open(random_path, O_RDONLY);
7390 ASSERT3S(ztest_fd_rand, >=, 0);
7393 process_options(argc, argv);
7398 bcopy(&ztest_opts, ztest_shared_opts,
7399 sizeof (*ztest_shared_opts));
7401 ztest_fd_data = atoi(fd_data_str);
7403 bcopy(ztest_shared_opts, &ztest_opts, sizeof (ztest_opts));
7405 ASSERT3U(ztest_opts.zo_datasets, ==, ztest_shared_hdr->zh_ds_count);
7407 /* Override location of zpool.cache */
7408 VERIFY(asprintf((char **)&spa_config_path, "%s/zpool.cache",
7409 ztest_opts.zo_dir) != -1);
7411 ztest_ds = umem_alloc(ztest_opts.zo_datasets * sizeof (ztest_ds_t),
7416 metaslab_force_ganging = ztest_opts.zo_metaslab_force_ganging;
7417 metaslab_df_alloc_threshold =
7418 zs->zs_metaslab_df_alloc_threshold;
7427 hasalt = (strlen(ztest_opts.zo_alt_ztest) != 0);
7429 if (ztest_opts.zo_verbose >= 1) {
7430 (void) printf("%llu vdevs, %d datasets, %d threads,"
7431 " %llu seconds...\n",
7432 (u_longlong_t)ztest_opts.zo_vdevs,
7433 ztest_opts.zo_datasets,
7434 ztest_opts.zo_threads,
7435 (u_longlong_t)ztest_opts.zo_time);
7438 cmd = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
7439 (void) strlcpy(cmd, getexecname(), MAXNAMELEN);
7441 zs->zs_do_init = B_TRUE;
7442 if (strlen(ztest_opts.zo_alt_ztest) != 0) {
7443 if (ztest_opts.zo_verbose >= 1) {
7444 (void) printf("Executing older ztest for "
7445 "initialization: %s\n", ztest_opts.zo_alt_ztest);
7447 VERIFY(!exec_child(ztest_opts.zo_alt_ztest,
7448 ztest_opts.zo_alt_libpath, B_FALSE, NULL));
7450 VERIFY(!exec_child(NULL, NULL, B_FALSE, NULL));
7452 zs->zs_do_init = B_FALSE;
7454 zs->zs_proc_start = gethrtime();
7455 zs->zs_proc_stop = zs->zs_proc_start + ztest_opts.zo_time * NANOSEC;
7457 for (f = 0; f < ZTEST_FUNCS; f++) {
7458 zi = &ztest_info[f];
7459 zc = ZTEST_GET_SHARED_CALLSTATE(f);
7460 if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
7461 zc->zc_next = UINT64_MAX;
7463 zc->zc_next = zs->zs_proc_start +
7464 ztest_random(2 * zi->zi_interval[0] + 1);
7468 * Run the tests in a loop. These tests include fault injection
7469 * to verify that self-healing data works, and forced crashes
7470 * to verify that we never lose on-disk consistency.
7472 while (gethrtime() < zs->zs_proc_stop) {
7477 * Initialize the workload counters for each function.
7479 for (f = 0; f < ZTEST_FUNCS; f++) {
7480 zc = ZTEST_GET_SHARED_CALLSTATE(f);
7485 /* Set the allocation switch size */
7486 zs->zs_metaslab_df_alloc_threshold =
7487 ztest_random(zs->zs_metaslab_sz / 4) + 1;
7489 if (!hasalt || ztest_random(2) == 0) {
7490 if (hasalt && ztest_opts.zo_verbose >= 1) {
7491 (void) printf("Executing newer ztest: %s\n",
7495 killed = exec_child(cmd, NULL, B_TRUE, &status);
7497 if (hasalt && ztest_opts.zo_verbose >= 1) {
7498 (void) printf("Executing older ztest: %s\n",
7499 ztest_opts.zo_alt_ztest);
7502 killed = exec_child(ztest_opts.zo_alt_ztest,
7503 ztest_opts.zo_alt_libpath, B_TRUE, &status);
7510 if (ztest_opts.zo_verbose >= 1) {
7511 hrtime_t now = gethrtime();
7513 now = MIN(now, zs->zs_proc_stop);
7514 print_time(zs->zs_proc_stop - now, timebuf);
7515 nicenum(zs->zs_space, numbuf, sizeof (numbuf));
7517 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
7518 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
7520 WIFEXITED(status) ? "Complete" : "SIGKILL",
7521 (u_longlong_t)zs->zs_enospc_count,
7522 100.0 * zs->zs_alloc / zs->zs_space,
7524 100.0 * (now - zs->zs_proc_start) /
7525 (ztest_opts.zo_time * NANOSEC), timebuf);
7528 if (ztest_opts.zo_verbose >= 2) {
7529 (void) printf("\nWorkload summary:\n\n");
7530 (void) printf("%7s %9s %s\n",
7531 "Calls", "Time", "Function");
7532 (void) printf("%7s %9s %s\n",
7533 "-----", "----", "--------");
7534 for (f = 0; f < ZTEST_FUNCS; f++) {
7535 zi = &ztest_info[f];
7536 zc = ZTEST_GET_SHARED_CALLSTATE(f);
7537 print_time(zc->zc_time, timebuf);
7538 (void) printf("%7llu %9s %s\n",
7539 (u_longlong_t)zc->zc_count, timebuf,
7542 (void) printf("\n");
7546 * It's possible that we killed a child during a rename test,
7547 * in which case we'll have a 'ztest_tmp' pool lying around
7548 * instead of 'ztest'. Do a blind rename in case this happened.
7551 if (spa_open(ztest_opts.zo_pool, &spa, FTAG) == 0) {
7552 spa_close(spa, FTAG);
7554 char tmpname[ZFS_MAX_DATASET_NAME_LEN];
7556 kernel_init(FREAD | FWRITE);
7557 (void) snprintf(tmpname, sizeof (tmpname), "%s_tmp",
7558 ztest_opts.zo_pool);
7559 (void) spa_rename(tmpname, ztest_opts.zo_pool);
7563 if (!ztest_opts.zo_mmp_test)
7564 ztest_run_zdb(ztest_opts.zo_pool);
7567 if (ztest_opts.zo_verbose >= 1) {
7569 (void) printf("%d runs of older ztest: %s\n", older,
7570 ztest_opts.zo_alt_ztest);
7571 (void) printf("%d runs of newer ztest: %s\n", newer,
7574 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
7575 kills, iters - kills, (100.0 * kills) / MAX(1, iters));
7578 umem_free(cmd, MAXNAMELEN);