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) 2012 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
26 * Copyright (c) 2013 Steven Hartland. All rights reserved.
27 * Copyright (c) 2014 Integros [integros.com]
28 * Copyright 2017 Joyent, Inc.
29 * Copyright 2017 RackTop Systems.
33 * The objective of this program is to provide a DMU/ZAP/SPA stress test
34 * that runs entirely in userland, is easy to use, and easy to extend.
36 * The overall design of the ztest program is as follows:
38 * (1) For each major functional area (e.g. adding vdevs to a pool,
39 * creating and destroying datasets, reading and writing objects, etc)
40 * we have a simple routine to test that functionality. These
41 * individual routines do not have to do anything "stressful".
43 * (2) We turn these simple functionality tests into a stress test by
44 * running them all in parallel, with as many threads as desired,
45 * and spread across as many datasets, objects, and vdevs as desired.
47 * (3) While all this is happening, we inject faults into the pool to
48 * verify that self-healing data really works.
50 * (4) Every time we open a dataset, we change its checksum and compression
51 * functions. Thus even individual objects vary from block to block
52 * in which checksum they use and whether they're compressed.
54 * (5) To verify that we never lose on-disk consistency after a crash,
55 * we run the entire test in a child of the main process.
56 * At random times, the child self-immolates with a SIGKILL.
57 * This is the software equivalent of pulling the power cord.
58 * The parent then runs the test again, using the existing
59 * storage pool, as many times as desired. If backwards compatibility
60 * testing is enabled ztest will sometimes run the "older" version
61 * of ztest after a SIGKILL.
63 * (6) To verify that we don't have future leaks or temporal incursions,
64 * many of the functional tests record the transaction group number
65 * as part of their data. When reading old data, they verify that
66 * the transaction group number is less than the current, open txg.
67 * If you add a new test, please do this if applicable.
69 * When run with no arguments, ztest runs for about five minutes and
70 * produces no output if successful. To get a little bit of information,
71 * specify -V. To get more information, specify -VV, and so on.
73 * To turn this into an overnight stress test, use -T to specify run time.
75 * You can ask more more vdevs [-v], datasets [-d], or threads [-t]
76 * to increase the pool capacity, fanout, and overall stress level.
78 * Use the -k option to set the desired frequency of kills.
80 * When ztest invokes itself it passes all relevant information through a
81 * temporary file which is mmap-ed in the child process. This allows shared
82 * memory to survive the exec syscall. The ztest_shared_hdr_t struct is always
83 * stored at offset 0 of this file and contains information on the size and
84 * number of shared structures in the file. The information stored in this file
85 * must remain backwards compatible with older versions of ztest so that
86 * ztest can invoke them during backwards compatibility testing (-B).
89 #include <sys/zfs_context.h>
95 #include <sys/dmu_objset.h>
100 #include <sys/mman.h>
101 #include <sys/resource.h>
104 #include <sys/zil_impl.h>
105 #include <sys/vdev_impl.h>
106 #include <sys/vdev_file.h>
107 #include <sys/vdev_initialize.h>
108 #include <sys/spa_impl.h>
109 #include <sys/metaslab_impl.h>
110 #include <sys/dsl_prop.h>
111 #include <sys/dsl_dataset.h>
112 #include <sys/dsl_destroy.h>
113 #include <sys/dsl_scan.h>
114 #include <sys/zio_checksum.h>
115 #include <sys/refcount.h>
116 #include <sys/zfeature.h>
117 #include <sys/dsl_userhold.h>
120 #include <stdio_ext.h>
129 #include <sys/fs/zfs.h>
130 #include <libnvpair.h>
131 #include <libcmdutils.h>
133 static int ztest_fd_data = -1;
134 static int ztest_fd_rand = -1;
136 typedef struct ztest_shared_hdr {
137 uint64_t zh_hdr_size;
138 uint64_t zh_opts_size;
140 uint64_t zh_stats_size;
141 uint64_t zh_stats_count;
143 uint64_t zh_ds_count;
144 } ztest_shared_hdr_t;
146 static ztest_shared_hdr_t *ztest_shared_hdr;
148 typedef struct ztest_shared_opts {
149 char zo_pool[ZFS_MAX_DATASET_NAME_LEN];
150 char zo_dir[ZFS_MAX_DATASET_NAME_LEN];
151 char zo_alt_ztest[MAXNAMELEN];
152 char zo_alt_libpath[MAXNAMELEN];
154 uint64_t zo_vdevtime;
162 uint64_t zo_passtime;
163 uint64_t zo_killrate;
167 uint64_t zo_maxloops;
168 uint64_t zo_metaslab_force_ganging;
169 } ztest_shared_opts_t;
171 static const ztest_shared_opts_t ztest_opts_defaults = {
172 .zo_pool = { 'z', 't', 'e', 's', 't', '\0' },
173 .zo_dir = { '/', 't', 'm', 'p', '\0' },
174 .zo_alt_ztest = { '\0' },
175 .zo_alt_libpath = { '\0' },
177 .zo_ashift = SPA_MINBLOCKSHIFT,
180 .zo_raidz_parity = 1,
181 .zo_vdev_size = SPA_MINDEVSIZE * 4, /* 256m default size */
184 .zo_passtime = 60, /* 60 seconds */
185 .zo_killrate = 70, /* 70% kill rate */
188 .zo_time = 300, /* 5 minutes */
189 .zo_maxloops = 50, /* max loops during spa_freeze() */
190 .zo_metaslab_force_ganging = 32 << 10
193 extern uint64_t metaslab_force_ganging;
194 extern uint64_t metaslab_df_alloc_threshold;
195 extern uint64_t zfs_deadman_synctime_ms;
196 extern int metaslab_preload_limit;
197 extern boolean_t zfs_compressed_arc_enabled;
198 extern boolean_t zfs_abd_scatter_enabled;
199 extern int dmu_object_alloc_chunk_shift;
200 extern boolean_t zfs_force_some_double_word_sm_entries;
201 extern unsigned long zfs_reconstruct_indirect_damage_fraction;
203 static ztest_shared_opts_t *ztest_shared_opts;
204 static ztest_shared_opts_t ztest_opts;
206 typedef struct ztest_shared_ds {
210 static ztest_shared_ds_t *ztest_shared_ds;
211 #define ZTEST_GET_SHARED_DS(d) (&ztest_shared_ds[d])
213 #define BT_MAGIC 0x123456789abcdefULL
214 #define MAXFAULTS() \
215 (MAX(zs->zs_mirrors, 1) * (ztest_opts.zo_raidz_parity + 1) - 1)
219 ZTEST_IO_WRITE_PATTERN,
220 ZTEST_IO_WRITE_ZEROES,
227 typedef struct ztest_block_tag {
231 uint64_t bt_dnodesize;
238 typedef struct bufwad {
245 * It would be better to use a rangelock_t per object. Unfortunately
246 * the rangelock_t is not a drop-in replacement for rl_t, because we
247 * still need to map from object ID to rangelock_t.
269 #define ZTEST_RANGE_LOCKS 64
270 #define ZTEST_OBJECT_LOCKS 64
273 * Object descriptor. Used as a template for object lookup/create/remove.
275 typedef struct ztest_od {
278 dmu_object_type_t od_type;
279 dmu_object_type_t od_crtype;
280 uint64_t od_blocksize;
281 uint64_t od_crblocksize;
282 uint64_t od_crdnodesize;
285 char od_name[ZFS_MAX_DATASET_NAME_LEN];
291 typedef struct ztest_ds {
292 ztest_shared_ds_t *zd_shared;
294 krwlock_t zd_zilog_lock;
296 ztest_od_t *zd_od; /* debugging aid */
297 char zd_name[ZFS_MAX_DATASET_NAME_LEN];
298 kmutex_t zd_dirobj_lock;
299 rll_t zd_object_lock[ZTEST_OBJECT_LOCKS];
300 rll_t zd_range_lock[ZTEST_RANGE_LOCKS];
304 * Per-iteration state.
306 typedef void ztest_func_t(ztest_ds_t *zd, uint64_t id);
308 typedef struct ztest_info {
309 ztest_func_t *zi_func; /* test function */
310 uint64_t zi_iters; /* iterations per execution */
311 uint64_t *zi_interval; /* execute every <interval> seconds */
314 typedef struct ztest_shared_callstate {
315 uint64_t zc_count; /* per-pass count */
316 uint64_t zc_time; /* per-pass time */
317 uint64_t zc_next; /* next time to call this function */
318 } ztest_shared_callstate_t;
320 static ztest_shared_callstate_t *ztest_shared_callstate;
321 #define ZTEST_GET_SHARED_CALLSTATE(c) (&ztest_shared_callstate[c])
324 * Note: these aren't static because we want dladdr() to work.
326 ztest_func_t ztest_dmu_read_write;
327 ztest_func_t ztest_dmu_write_parallel;
328 ztest_func_t ztest_dmu_object_alloc_free;
329 ztest_func_t ztest_dmu_object_next_chunk;
330 ztest_func_t ztest_dmu_commit_callbacks;
331 ztest_func_t ztest_zap;
332 ztest_func_t ztest_zap_parallel;
333 ztest_func_t ztest_zil_commit;
334 ztest_func_t ztest_zil_remount;
335 ztest_func_t ztest_dmu_read_write_zcopy;
336 ztest_func_t ztest_dmu_objset_create_destroy;
337 ztest_func_t ztest_dmu_prealloc;
338 ztest_func_t ztest_fzap;
339 ztest_func_t ztest_dmu_snapshot_create_destroy;
340 ztest_func_t ztest_dsl_prop_get_set;
341 ztest_func_t ztest_spa_prop_get_set;
342 ztest_func_t ztest_spa_create_destroy;
343 ztest_func_t ztest_fault_inject;
344 ztest_func_t ztest_ddt_repair;
345 ztest_func_t ztest_dmu_snapshot_hold;
346 ztest_func_t ztest_scrub;
347 ztest_func_t ztest_dsl_dataset_promote_busy;
348 ztest_func_t ztest_vdev_attach_detach;
349 ztest_func_t ztest_vdev_LUN_growth;
350 ztest_func_t ztest_vdev_add_remove;
351 ztest_func_t ztest_vdev_aux_add_remove;
352 ztest_func_t ztest_split_pool;
353 ztest_func_t ztest_reguid;
354 ztest_func_t ztest_spa_upgrade;
355 ztest_func_t ztest_device_removal;
356 ztest_func_t ztest_remap_blocks;
357 ztest_func_t ztest_spa_checkpoint_create_discard;
358 ztest_func_t ztest_initialize;
359 ztest_func_t ztest_verify_dnode_bt;
361 uint64_t zopt_always = 0ULL * NANOSEC; /* all the time */
362 uint64_t zopt_incessant = 1ULL * NANOSEC / 10; /* every 1/10 second */
363 uint64_t zopt_often = 1ULL * NANOSEC; /* every second */
364 uint64_t zopt_sometimes = 10ULL * NANOSEC; /* every 10 seconds */
365 uint64_t zopt_rarely = 60ULL * NANOSEC; /* every 60 seconds */
367 ztest_info_t ztest_info[] = {
368 { ztest_dmu_read_write, 1, &zopt_always },
369 { ztest_dmu_write_parallel, 10, &zopt_always },
370 { ztest_dmu_object_alloc_free, 1, &zopt_always },
371 { ztest_dmu_object_next_chunk, 1, &zopt_sometimes },
372 { ztest_dmu_commit_callbacks, 1, &zopt_always },
373 { ztest_zap, 30, &zopt_always },
374 { ztest_zap_parallel, 100, &zopt_always },
375 { ztest_split_pool, 1, &zopt_always },
376 { ztest_zil_commit, 1, &zopt_incessant },
377 { ztest_zil_remount, 1, &zopt_sometimes },
378 { ztest_dmu_read_write_zcopy, 1, &zopt_often },
379 { ztest_dmu_objset_create_destroy, 1, &zopt_often },
380 { ztest_dsl_prop_get_set, 1, &zopt_often },
381 { ztest_spa_prop_get_set, 1, &zopt_sometimes },
383 { ztest_dmu_prealloc, 1, &zopt_sometimes },
385 { ztest_fzap, 1, &zopt_sometimes },
386 { ztest_dmu_snapshot_create_destroy, 1, &zopt_sometimes },
387 { ztest_spa_create_destroy, 1, &zopt_sometimes },
388 { ztest_fault_inject, 1, &zopt_incessant },
389 { ztest_ddt_repair, 1, &zopt_sometimes },
390 { ztest_dmu_snapshot_hold, 1, &zopt_sometimes },
391 { ztest_reguid, 1, &zopt_rarely },
392 { ztest_scrub, 1, &zopt_often },
393 { ztest_spa_upgrade, 1, &zopt_rarely },
394 { ztest_dsl_dataset_promote_busy, 1, &zopt_rarely },
395 { ztest_vdev_attach_detach, 1, &zopt_incessant },
396 { ztest_vdev_LUN_growth, 1, &zopt_rarely },
397 { ztest_vdev_add_remove, 1,
398 &ztest_opts.zo_vdevtime },
399 { ztest_vdev_aux_add_remove, 1,
400 &ztest_opts.zo_vdevtime },
401 { ztest_device_removal, 1, &zopt_sometimes },
402 { ztest_remap_blocks, 1, &zopt_sometimes },
403 { ztest_spa_checkpoint_create_discard, 1, &zopt_rarely },
404 { ztest_initialize, 1, &zopt_sometimes },
405 { ztest_verify_dnode_bt, 1, &zopt_sometimes }
408 #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
411 * The following struct is used to hold a list of uncalled commit callbacks.
412 * The callbacks are ordered by txg number.
414 typedef struct ztest_cb_list {
415 kmutex_t zcl_callbacks_lock;
416 list_t zcl_callbacks;
420 * Stuff we need to share writably between parent and child.
422 typedef struct ztest_shared {
423 boolean_t zs_do_init;
424 hrtime_t zs_proc_start;
425 hrtime_t zs_proc_stop;
426 hrtime_t zs_thread_start;
427 hrtime_t zs_thread_stop;
428 hrtime_t zs_thread_kill;
429 uint64_t zs_enospc_count;
430 uint64_t zs_vdev_next_leaf;
431 uint64_t zs_vdev_aux;
436 uint64_t zs_metaslab_sz;
437 uint64_t zs_metaslab_df_alloc_threshold;
441 #define ID_PARALLEL -1ULL
443 static char ztest_dev_template[] = "%s/%s.%llua";
444 static char ztest_aux_template[] = "%s/%s.%s.%llu";
445 ztest_shared_t *ztest_shared;
447 static spa_t *ztest_spa = NULL;
448 static ztest_ds_t *ztest_ds;
450 static kmutex_t ztest_vdev_lock;
451 static boolean_t ztest_device_removal_active = B_FALSE;
452 static kmutex_t ztest_checkpoint_lock;
455 * The ztest_name_lock protects the pool and dataset namespace used by
456 * the individual tests. To modify the namespace, consumers must grab
457 * this lock as writer. Grabbing the lock as reader will ensure that the
458 * namespace does not change while the lock is held.
460 static krwlock_t ztest_name_lock;
462 static boolean_t ztest_dump_core = B_TRUE;
463 static boolean_t ztest_exiting;
465 /* Global commit callback list */
466 static ztest_cb_list_t zcl;
469 ZTEST_META_DNODE = 0,
474 static void usage(boolean_t) __NORETURN;
477 * These libumem hooks provide a reasonable set of defaults for the allocator's
478 * debugging facilities.
483 return ("default,verbose"); /* $UMEM_DEBUG setting */
487 _umem_logging_init(void)
489 return ("fail,contents"); /* $UMEM_LOGGING setting */
492 #define FATAL_MSG_SZ 1024
497 fatal(int do_perror, char *message, ...)
500 int save_errno = errno;
501 char buf[FATAL_MSG_SZ];
503 (void) fflush(stdout);
505 va_start(args, message);
506 (void) sprintf(buf, "ztest: ");
508 (void) vsprintf(buf + strlen(buf), message, args);
511 (void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
512 ": %s", strerror(save_errno));
514 (void) fprintf(stderr, "%s\n", buf);
515 fatal_msg = buf; /* to ease debugging */
522 str2shift(const char *buf)
524 const char *ends = "BKMGTPEZ";
529 for (i = 0; i < strlen(ends); i++) {
530 if (toupper(buf[0]) == ends[i])
533 if (i == strlen(ends)) {
534 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
538 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
541 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
547 nicenumtoull(const char *buf)
552 val = strtoull(buf, &end, 0);
554 (void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
556 } else if (end[0] == '.') {
557 double fval = strtod(buf, &end);
558 fval *= pow(2, str2shift(end));
559 if (fval > UINT64_MAX) {
560 (void) fprintf(stderr, "ztest: value too large: %s\n",
564 val = (uint64_t)fval;
566 int shift = str2shift(end);
567 if (shift >= 64 || (val << shift) >> shift != val) {
568 (void) fprintf(stderr, "ztest: value too large: %s\n",
578 usage(boolean_t requested)
580 const ztest_shared_opts_t *zo = &ztest_opts_defaults;
582 char nice_vdev_size[NN_NUMBUF_SZ];
583 char nice_force_ganging[NN_NUMBUF_SZ];
584 FILE *fp = requested ? stdout : stderr;
586 nicenum(zo->zo_vdev_size, nice_vdev_size, sizeof (nice_vdev_size));
587 nicenum(zo->zo_metaslab_force_ganging, nice_force_ganging,
588 sizeof (nice_force_ganging));
590 (void) fprintf(fp, "Usage: %s\n"
591 "\t[-v vdevs (default: %llu)]\n"
592 "\t[-s size_of_each_vdev (default: %s)]\n"
593 "\t[-a alignment_shift (default: %d)] use 0 for random\n"
594 "\t[-m mirror_copies (default: %d)]\n"
595 "\t[-r raidz_disks (default: %d)]\n"
596 "\t[-R raidz_parity (default: %d)]\n"
597 "\t[-d datasets (default: %d)]\n"
598 "\t[-t threads (default: %d)]\n"
599 "\t[-g gang_block_threshold (default: %s)]\n"
600 "\t[-i init_count (default: %d)] initialize pool i times\n"
601 "\t[-k kill_percentage (default: %llu%%)]\n"
602 "\t[-p pool_name (default: %s)]\n"
603 "\t[-f dir (default: %s)] file directory for vdev files\n"
604 "\t[-V] verbose (use multiple times for ever more blather)\n"
605 "\t[-E] use existing pool instead of creating new one\n"
606 "\t[-T time (default: %llu sec)] total run time\n"
607 "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
608 "\t[-P passtime (default: %llu sec)] time per pass\n"
609 "\t[-B alt_ztest (default: <none>)] alternate ztest path\n"
610 "\t[-o variable=value] ... set global variable to an unsigned\n"
611 "\t 32-bit integer value\n"
612 "\t[-h] (print help)\n"
615 (u_longlong_t)zo->zo_vdevs, /* -v */
616 nice_vdev_size, /* -s */
617 zo->zo_ashift, /* -a */
618 zo->zo_mirrors, /* -m */
619 zo->zo_raidz, /* -r */
620 zo->zo_raidz_parity, /* -R */
621 zo->zo_datasets, /* -d */
622 zo->zo_threads, /* -t */
623 nice_force_ganging, /* -g */
624 zo->zo_init, /* -i */
625 (u_longlong_t)zo->zo_killrate, /* -k */
626 zo->zo_pool, /* -p */
628 (u_longlong_t)zo->zo_time, /* -T */
629 (u_longlong_t)zo->zo_maxloops, /* -F */
630 (u_longlong_t)zo->zo_passtime);
631 exit(requested ? 0 : 1);
635 process_options(int argc, char **argv)
638 ztest_shared_opts_t *zo = &ztest_opts;
642 char altdir[MAXNAMELEN] = { 0 };
644 bcopy(&ztest_opts_defaults, zo, sizeof (*zo));
646 while ((opt = getopt(argc, argv,
647 "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:hF:B:o:")) != EOF) {
664 value = nicenumtoull(optarg);
668 zo->zo_vdevs = value;
671 zo->zo_vdev_size = MAX(SPA_MINDEVSIZE, value);
674 zo->zo_ashift = value;
677 zo->zo_mirrors = value;
680 zo->zo_raidz = MAX(1, value);
683 zo->zo_raidz_parity = MIN(MAX(value, 1), 3);
686 zo->zo_datasets = MAX(1, value);
689 zo->zo_threads = MAX(1, value);
692 zo->zo_metaslab_force_ganging =
693 MAX(SPA_MINBLOCKSIZE << 1, value);
699 zo->zo_killrate = value;
702 (void) strlcpy(zo->zo_pool, optarg,
703 sizeof (zo->zo_pool));
706 path = realpath(optarg, NULL);
708 (void) fprintf(stderr, "error: %s: %s\n",
709 optarg, strerror(errno));
712 (void) strlcpy(zo->zo_dir, path,
713 sizeof (zo->zo_dir));
726 zo->zo_passtime = MAX(1, value);
729 zo->zo_maxloops = MAX(1, value);
732 (void) strlcpy(altdir, optarg, sizeof (altdir));
735 if (set_global_var(optarg) != 0)
748 zo->zo_raidz_parity = MIN(zo->zo_raidz_parity, zo->zo_raidz - 1);
751 (zo->zo_vdevs > 0 ? zo->zo_time * NANOSEC / zo->zo_vdevs :
754 if (strlen(altdir) > 0) {
762 cmd = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
763 realaltdir = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
765 VERIFY(NULL != realpath(getexecname(), cmd));
766 if (0 != access(altdir, F_OK)) {
767 ztest_dump_core = B_FALSE;
768 fatal(B_TRUE, "invalid alternate ztest path: %s",
771 VERIFY(NULL != realpath(altdir, realaltdir));
774 * 'cmd' should be of the form "<anything>/usr/bin/<isa>/ztest".
775 * We want to extract <isa> to determine if we should use
776 * 32 or 64 bit binaries.
778 bin = strstr(cmd, "/usr/bin/");
779 ztest = strstr(bin, "/ztest");
781 isalen = ztest - isa;
782 (void) snprintf(zo->zo_alt_ztest, sizeof (zo->zo_alt_ztest),
783 "%s/usr/bin/%.*s/ztest", realaltdir, isalen, isa);
784 (void) snprintf(zo->zo_alt_libpath, sizeof (zo->zo_alt_libpath),
785 "%s/usr/lib/%.*s", realaltdir, isalen, isa);
787 if (0 != access(zo->zo_alt_ztest, X_OK)) {
788 ztest_dump_core = B_FALSE;
789 fatal(B_TRUE, "invalid alternate ztest: %s",
791 } else if (0 != access(zo->zo_alt_libpath, X_OK)) {
792 ztest_dump_core = B_FALSE;
793 fatal(B_TRUE, "invalid alternate lib directory %s",
797 umem_free(cmd, MAXPATHLEN);
798 umem_free(realaltdir, MAXPATHLEN);
803 ztest_kill(ztest_shared_t *zs)
805 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(ztest_spa));
806 zs->zs_space = metaslab_class_get_space(spa_normal_class(ztest_spa));
809 * Before we kill off ztest, make sure that the config is updated.
810 * See comment above spa_write_cachefile().
812 mutex_enter(&spa_namespace_lock);
813 spa_write_cachefile(ztest_spa, B_FALSE, B_FALSE);
814 mutex_exit(&spa_namespace_lock);
816 zfs_dbgmsg_print(FTAG);
817 (void) kill(getpid(), SIGKILL);
821 ztest_random(uint64_t range)
825 ASSERT3S(ztest_fd_rand, >=, 0);
830 if (read(ztest_fd_rand, &r, sizeof (r)) != sizeof (r))
831 fatal(1, "short read from /dev/urandom");
838 ztest_record_enospc(const char *s)
840 ztest_shared->zs_enospc_count++;
844 ztest_get_ashift(void)
846 if (ztest_opts.zo_ashift == 0)
847 return (SPA_MINBLOCKSHIFT + ztest_random(5));
848 return (ztest_opts.zo_ashift);
852 make_vdev_file(char *path, char *aux, char *pool, size_t size, uint64_t ashift)
854 char pathbuf[MAXPATHLEN];
859 ashift = ztest_get_ashift();
865 vdev = ztest_shared->zs_vdev_aux;
866 (void) snprintf(path, sizeof (pathbuf),
867 ztest_aux_template, ztest_opts.zo_dir,
868 pool == NULL ? ztest_opts.zo_pool : pool,
871 vdev = ztest_shared->zs_vdev_next_leaf++;
872 (void) snprintf(path, sizeof (pathbuf),
873 ztest_dev_template, ztest_opts.zo_dir,
874 pool == NULL ? ztest_opts.zo_pool : pool, vdev);
879 int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
881 fatal(1, "can't open %s", path);
882 if (ftruncate(fd, size) != 0)
883 fatal(1, "can't ftruncate %s", path);
887 VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
888 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
889 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
890 VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
896 make_vdev_raidz(char *path, char *aux, char *pool, size_t size,
897 uint64_t ashift, int r)
899 nvlist_t *raidz, **child;
903 return (make_vdev_file(path, aux, pool, size, ashift));
904 child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
906 for (c = 0; c < r; c++)
907 child[c] = make_vdev_file(path, aux, pool, size, ashift);
909 VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
910 VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
911 VDEV_TYPE_RAIDZ) == 0);
912 VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
913 ztest_opts.zo_raidz_parity) == 0);
914 VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
917 for (c = 0; c < r; c++)
918 nvlist_free(child[c]);
920 umem_free(child, r * sizeof (nvlist_t *));
926 make_vdev_mirror(char *path, char *aux, char *pool, size_t size,
927 uint64_t ashift, int r, int m)
929 nvlist_t *mirror, **child;
933 return (make_vdev_raidz(path, aux, pool, size, ashift, r));
935 child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
937 for (c = 0; c < m; c++)
938 child[c] = make_vdev_raidz(path, aux, pool, size, ashift, r);
940 VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
941 VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
942 VDEV_TYPE_MIRROR) == 0);
943 VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
946 for (c = 0; c < m; c++)
947 nvlist_free(child[c]);
949 umem_free(child, m * sizeof (nvlist_t *));
955 make_vdev_root(char *path, char *aux, char *pool, size_t size, uint64_t ashift,
956 int log, int r, int m, int t)
958 nvlist_t *root, **child;
963 child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
965 for (c = 0; c < t; c++) {
966 child[c] = make_vdev_mirror(path, aux, pool, size, ashift,
968 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
972 VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
973 VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
974 VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
977 for (c = 0; c < t; c++)
978 nvlist_free(child[c]);
980 umem_free(child, t * sizeof (nvlist_t *));
986 * Find a random spa version. Returns back a random spa version in the
987 * range [initial_version, SPA_VERSION_FEATURES].
990 ztest_random_spa_version(uint64_t initial_version)
992 uint64_t version = initial_version;
994 if (version <= SPA_VERSION_BEFORE_FEATURES) {
996 ztest_random(SPA_VERSION_BEFORE_FEATURES - version + 1);
999 if (version > SPA_VERSION_BEFORE_FEATURES)
1000 version = SPA_VERSION_FEATURES;
1002 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
1007 ztest_random_blocksize(void)
1009 uint64_t block_shift;
1011 * Choose a block size >= the ashift.
1012 * If the SPA supports new MAXBLOCKSIZE, test up to 1MB blocks.
1014 int maxbs = SPA_OLD_MAXBLOCKSHIFT;
1015 if (spa_maxblocksize(ztest_spa) == SPA_MAXBLOCKSIZE)
1017 block_shift = ztest_random(maxbs - ztest_spa->spa_max_ashift + 1);
1018 return (1 << (SPA_MINBLOCKSHIFT + block_shift));
1022 ztest_random_dnodesize(void)
1025 int max_slots = spa_maxdnodesize(ztest_spa) >> DNODE_SHIFT;
1027 if (max_slots == DNODE_MIN_SLOTS)
1028 return (DNODE_MIN_SIZE);
1031 * Weight the random distribution more heavily toward smaller
1032 * dnode sizes since that is more likely to reflect real-world
1035 ASSERT3U(max_slots, >, 4);
1036 switch (ztest_random(10)) {
1038 slots = 5 + ztest_random(max_slots - 4);
1041 slots = 2 + ztest_random(3);
1048 return (slots << DNODE_SHIFT);
1052 ztest_random_ibshift(void)
1054 return (DN_MIN_INDBLKSHIFT +
1055 ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1));
1059 ztest_random_vdev_top(spa_t *spa, boolean_t log_ok)
1062 vdev_t *rvd = spa->spa_root_vdev;
1065 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1068 top = ztest_random(rvd->vdev_children);
1069 tvd = rvd->vdev_child[top];
1070 } while (!vdev_is_concrete(tvd) || (tvd->vdev_islog && !log_ok) ||
1071 tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL);
1077 ztest_random_dsl_prop(zfs_prop_t prop)
1082 value = zfs_prop_random_value(prop, ztest_random(-1ULL));
1083 } while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF);
1089 ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value,
1092 const char *propname = zfs_prop_to_name(prop);
1093 const char *valname;
1094 char setpoint[MAXPATHLEN];
1098 error = dsl_prop_set_int(osname, propname,
1099 (inherit ? ZPROP_SRC_NONE : ZPROP_SRC_LOCAL), value);
1101 if (error == ENOSPC) {
1102 ztest_record_enospc(FTAG);
1107 VERIFY0(dsl_prop_get_integer(osname, propname, &curval, setpoint));
1109 if (ztest_opts.zo_verbose >= 6) {
1110 VERIFY(zfs_prop_index_to_string(prop, curval, &valname) == 0);
1111 (void) printf("%s %s = %s at '%s'\n",
1112 osname, propname, valname, setpoint);
1119 ztest_spa_prop_set_uint64(zpool_prop_t prop, uint64_t value)
1121 spa_t *spa = ztest_spa;
1122 nvlist_t *props = NULL;
1125 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
1126 VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0);
1128 error = spa_prop_set(spa, props);
1132 if (error == ENOSPC) {
1133 ztest_record_enospc(FTAG);
1142 ztest_rll_init(rll_t *rll)
1144 rll->rll_writer = NULL;
1145 rll->rll_readers = 0;
1146 mutex_init(&rll->rll_lock, NULL, USYNC_THREAD, NULL);
1147 cv_init(&rll->rll_cv, NULL, USYNC_THREAD, NULL);
1151 ztest_rll_destroy(rll_t *rll)
1153 ASSERT(rll->rll_writer == NULL);
1154 ASSERT(rll->rll_readers == 0);
1155 mutex_destroy(&rll->rll_lock);
1156 cv_destroy(&rll->rll_cv);
1160 ztest_rll_lock(rll_t *rll, rl_type_t type)
1162 mutex_enter(&rll->rll_lock);
1164 if (type == RL_READER) {
1165 while (rll->rll_writer != NULL)
1166 cv_wait(&rll->rll_cv, &rll->rll_lock);
1169 while (rll->rll_writer != NULL || rll->rll_readers)
1170 cv_wait(&rll->rll_cv, &rll->rll_lock);
1171 rll->rll_writer = curthread;
1174 mutex_exit(&rll->rll_lock);
1178 ztest_rll_unlock(rll_t *rll)
1180 mutex_enter(&rll->rll_lock);
1182 if (rll->rll_writer) {
1183 ASSERT(rll->rll_readers == 0);
1184 rll->rll_writer = NULL;
1186 ASSERT(rll->rll_readers != 0);
1187 ASSERT(rll->rll_writer == NULL);
1191 if (rll->rll_writer == NULL && rll->rll_readers == 0)
1192 cv_broadcast(&rll->rll_cv);
1194 mutex_exit(&rll->rll_lock);
1198 ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type)
1200 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1202 ztest_rll_lock(rll, type);
1206 ztest_object_unlock(ztest_ds_t *zd, uint64_t object)
1208 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1210 ztest_rll_unlock(rll);
1214 ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset,
1215 uint64_t size, rl_type_t type)
1217 uint64_t hash = object ^ (offset % (ZTEST_RANGE_LOCKS + 1));
1218 rll_t *rll = &zd->zd_range_lock[hash & (ZTEST_RANGE_LOCKS - 1)];
1221 rl = umem_alloc(sizeof (*rl), UMEM_NOFAIL);
1222 rl->rl_object = object;
1223 rl->rl_offset = offset;
1227 ztest_rll_lock(rll, type);
1233 ztest_range_unlock(rl_t *rl)
1235 rll_t *rll = rl->rl_lock;
1237 ztest_rll_unlock(rll);
1239 umem_free(rl, sizeof (*rl));
1243 ztest_zd_init(ztest_ds_t *zd, ztest_shared_ds_t *szd, objset_t *os)
1246 zd->zd_zilog = dmu_objset_zil(os);
1247 zd->zd_shared = szd;
1248 dmu_objset_name(os, zd->zd_name);
1250 if (zd->zd_shared != NULL)
1251 zd->zd_shared->zd_seq = 0;
1253 rw_init(&zd->zd_zilog_lock, NULL, USYNC_THREAD, NULL);
1254 mutex_init(&zd->zd_dirobj_lock, NULL, USYNC_THREAD, NULL);
1256 for (int l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1257 ztest_rll_init(&zd->zd_object_lock[l]);
1259 for (int l = 0; l < ZTEST_RANGE_LOCKS; l++)
1260 ztest_rll_init(&zd->zd_range_lock[l]);
1264 ztest_zd_fini(ztest_ds_t *zd)
1266 mutex_destroy(&zd->zd_dirobj_lock);
1268 for (int l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1269 ztest_rll_destroy(&zd->zd_object_lock[l]);
1271 for (int l = 0; l < ZTEST_RANGE_LOCKS; l++)
1272 ztest_rll_destroy(&zd->zd_range_lock[l]);
1275 #define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1278 ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag)
1284 * Attempt to assign tx to some transaction group.
1286 error = dmu_tx_assign(tx, txg_how);
1288 if (error == ERESTART) {
1289 ASSERT(txg_how == TXG_NOWAIT);
1292 ASSERT3U(error, ==, ENOSPC);
1293 ztest_record_enospc(tag);
1298 txg = dmu_tx_get_txg(tx);
1304 ztest_pattern_set(void *buf, uint64_t size, uint64_t value)
1307 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1314 ztest_pattern_match(void *buf, uint64_t size, uint64_t value)
1317 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1321 diff |= (value - *ip++);
1327 ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1328 uint64_t dnodesize, uint64_t offset, uint64_t gen, uint64_t txg,
1331 bt->bt_magic = BT_MAGIC;
1332 bt->bt_objset = dmu_objset_id(os);
1333 bt->bt_object = object;
1334 bt->bt_dnodesize = dnodesize;
1335 bt->bt_offset = offset;
1338 bt->bt_crtxg = crtxg;
1342 ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1343 uint64_t dnodesize, uint64_t offset, uint64_t gen, uint64_t txg,
1346 ASSERT3U(bt->bt_magic, ==, BT_MAGIC);
1347 ASSERT3U(bt->bt_objset, ==, dmu_objset_id(os));
1348 ASSERT3U(bt->bt_object, ==, object);
1349 ASSERT3U(bt->bt_dnodesize, ==, dnodesize);
1350 ASSERT3U(bt->bt_offset, ==, offset);
1351 ASSERT3U(bt->bt_gen, <=, gen);
1352 ASSERT3U(bt->bt_txg, <=, txg);
1353 ASSERT3U(bt->bt_crtxg, ==, crtxg);
1356 static ztest_block_tag_t *
1357 ztest_bt_bonus(dmu_buf_t *db)
1359 dmu_object_info_t doi;
1360 ztest_block_tag_t *bt;
1362 dmu_object_info_from_db(db, &doi);
1363 ASSERT3U(doi.doi_bonus_size, <=, db->db_size);
1364 ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt));
1365 bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt));
1371 * Generate a token to fill up unused bonus buffer space. Try to make
1372 * it unique to the object, generation, and offset to verify that data
1373 * is not getting overwritten by data from other dnodes.
1375 #define ZTEST_BONUS_FILL_TOKEN(obj, ds, gen, offset) \
1376 (((ds) << 48) | ((gen) << 32) | ((obj) << 8) | (offset))
1379 * Fill up the unused bonus buffer region before the block tag with a
1380 * verifiable pattern. Filling the whole bonus area with non-zero data
1381 * helps ensure that all dnode traversal code properly skips the
1382 * interior regions of large dnodes.
1385 ztest_fill_unused_bonus(dmu_buf_t *db, void *end, uint64_t obj,
1386 objset_t *os, uint64_t gen)
1390 ASSERT(IS_P2ALIGNED((char *)end - (char *)db->db_data, 8));
1392 for (bonusp = db->db_data; bonusp < (uint64_t *)end; bonusp++) {
1393 uint64_t token = ZTEST_BONUS_FILL_TOKEN(obj, dmu_objset_id(os),
1394 gen, bonusp - (uint64_t *)db->db_data);
1400 * Verify that the unused area of a bonus buffer is filled with the
1404 ztest_verify_unused_bonus(dmu_buf_t *db, void *end, uint64_t obj,
1405 objset_t *os, uint64_t gen)
1409 for (bonusp = db->db_data; bonusp < (uint64_t *)end; bonusp++) {
1410 uint64_t token = ZTEST_BONUS_FILL_TOKEN(obj, dmu_objset_id(os),
1411 gen, bonusp - (uint64_t *)db->db_data);
1412 VERIFY3U(*bonusp, ==, token);
1420 #define lrz_type lr_mode
1421 #define lrz_blocksize lr_uid
1422 #define lrz_ibshift lr_gid
1423 #define lrz_bonustype lr_rdev
1424 #define lrz_dnodesize lr_crtime[1]
1427 ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr)
1429 char *name = (void *)(lr + 1); /* name follows lr */
1430 size_t namesize = strlen(name) + 1;
1433 if (zil_replaying(zd->zd_zilog, tx))
1436 itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize);
1437 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1438 sizeof (*lr) + namesize - sizeof (lr_t));
1440 zil_itx_assign(zd->zd_zilog, itx, tx);
1444 ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr, uint64_t object)
1446 char *name = (void *)(lr + 1); /* name follows lr */
1447 size_t namesize = strlen(name) + 1;
1450 if (zil_replaying(zd->zd_zilog, tx))
1453 itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize);
1454 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1455 sizeof (*lr) + namesize - sizeof (lr_t));
1457 itx->itx_oid = object;
1458 zil_itx_assign(zd->zd_zilog, itx, tx);
1462 ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr)
1465 itx_wr_state_t write_state = ztest_random(WR_NUM_STATES);
1467 if (zil_replaying(zd->zd_zilog, tx))
1470 if (lr->lr_length > zil_max_log_data(zd->zd_zilog))
1471 write_state = WR_INDIRECT;
1473 itx = zil_itx_create(TX_WRITE,
1474 sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0));
1476 if (write_state == WR_COPIED &&
1477 dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length,
1478 ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) {
1479 zil_itx_destroy(itx);
1480 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1481 write_state = WR_NEED_COPY;
1483 itx->itx_private = zd;
1484 itx->itx_wr_state = write_state;
1485 itx->itx_sync = (ztest_random(8) == 0);
1487 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1488 sizeof (*lr) - sizeof (lr_t));
1490 zil_itx_assign(zd->zd_zilog, itx, tx);
1494 ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr)
1498 if (zil_replaying(zd->zd_zilog, tx))
1501 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1502 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1503 sizeof (*lr) - sizeof (lr_t));
1505 itx->itx_sync = B_FALSE;
1506 zil_itx_assign(zd->zd_zilog, itx, tx);
1510 ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr)
1514 if (zil_replaying(zd->zd_zilog, tx))
1517 itx = zil_itx_create(TX_SETATTR, sizeof (*lr));
1518 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1519 sizeof (*lr) - sizeof (lr_t));
1521 itx->itx_sync = B_FALSE;
1522 zil_itx_assign(zd->zd_zilog, itx, tx);
1529 ztest_replay_create(void *arg1, void *arg2, boolean_t byteswap)
1531 ztest_ds_t *zd = arg1;
1532 lr_create_t *lr = arg2;
1533 char *name = (void *)(lr + 1); /* name follows lr */
1534 objset_t *os = zd->zd_os;
1535 ztest_block_tag_t *bbt;
1543 byteswap_uint64_array(lr, sizeof (*lr));
1545 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1546 ASSERT(name[0] != '\0');
1548 tx = dmu_tx_create(os);
1550 dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name);
1552 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1553 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1555 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1558 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1562 ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid);
1563 bonuslen = DN_BONUS_SIZE(lr->lrz_dnodesize);
1565 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1566 if (lr->lr_foid == 0) {
1567 lr->lr_foid = zap_create_dnsize(os,
1568 lr->lrz_type, lr->lrz_bonustype,
1569 bonuslen, lr->lrz_dnodesize, tx);
1571 error = zap_create_claim_dnsize(os, lr->lr_foid,
1572 lr->lrz_type, lr->lrz_bonustype,
1573 bonuslen, lr->lrz_dnodesize, tx);
1576 if (lr->lr_foid == 0) {
1577 lr->lr_foid = dmu_object_alloc_dnsize(os,
1578 lr->lrz_type, 0, lr->lrz_bonustype,
1579 bonuslen, lr->lrz_dnodesize, tx);
1581 error = dmu_object_claim_dnsize(os, lr->lr_foid,
1582 lr->lrz_type, 0, lr->lrz_bonustype,
1583 bonuslen, lr->lrz_dnodesize, tx);
1588 ASSERT3U(error, ==, EEXIST);
1589 ASSERT(zd->zd_zilog->zl_replay);
1594 ASSERT(lr->lr_foid != 0);
1596 if (lr->lrz_type != DMU_OT_ZAP_OTHER)
1597 VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid,
1598 lr->lrz_blocksize, lr->lrz_ibshift, tx));
1600 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1601 bbt = ztest_bt_bonus(db);
1602 dmu_buf_will_dirty(db, tx);
1603 ztest_bt_generate(bbt, os, lr->lr_foid, lr->lrz_dnodesize, -1ULL,
1604 lr->lr_gen, txg, txg);
1605 ztest_fill_unused_bonus(db, bbt, lr->lr_foid, os, lr->lr_gen);
1606 dmu_buf_rele(db, FTAG);
1608 VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1,
1611 (void) ztest_log_create(zd, tx, lr);
1619 ztest_replay_remove(void *arg1, void *arg2, boolean_t byteswap)
1621 ztest_ds_t *zd = arg1;
1622 lr_remove_t *lr = arg2;
1623 char *name = (void *)(lr + 1); /* name follows lr */
1624 objset_t *os = zd->zd_os;
1625 dmu_object_info_t doi;
1627 uint64_t object, txg;
1630 byteswap_uint64_array(lr, sizeof (*lr));
1632 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1633 ASSERT(name[0] != '\0');
1636 zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object));
1637 ASSERT(object != 0);
1639 ztest_object_lock(zd, object, RL_WRITER);
1641 VERIFY3U(0, ==, dmu_object_info(os, object, &doi));
1643 tx = dmu_tx_create(os);
1645 dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name);
1646 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1648 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1650 ztest_object_unlock(zd, object);
1654 if (doi.doi_type == DMU_OT_ZAP_OTHER) {
1655 VERIFY3U(0, ==, zap_destroy(os, object, tx));
1657 VERIFY3U(0, ==, dmu_object_free(os, object, tx));
1660 VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx));
1662 (void) ztest_log_remove(zd, tx, lr, object);
1666 ztest_object_unlock(zd, object);
1672 ztest_replay_write(void *arg1, void *arg2, boolean_t byteswap)
1674 ztest_ds_t *zd = arg1;
1675 lr_write_t *lr = arg2;
1676 objset_t *os = zd->zd_os;
1677 void *data = lr + 1; /* data follows lr */
1678 uint64_t offset, length;
1679 ztest_block_tag_t *bt = data;
1680 ztest_block_tag_t *bbt;
1681 uint64_t gen, txg, lrtxg, crtxg;
1682 dmu_object_info_t doi;
1685 arc_buf_t *abuf = NULL;
1689 byteswap_uint64_array(lr, sizeof (*lr));
1691 offset = lr->lr_offset;
1692 length = lr->lr_length;
1694 /* If it's a dmu_sync() block, write the whole block */
1695 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
1696 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
1697 if (length < blocksize) {
1698 offset -= offset % blocksize;
1703 if (bt->bt_magic == BSWAP_64(BT_MAGIC))
1704 byteswap_uint64_array(bt, sizeof (*bt));
1706 if (bt->bt_magic != BT_MAGIC)
1709 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1710 rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER);
1712 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1714 dmu_object_info_from_db(db, &doi);
1716 bbt = ztest_bt_bonus(db);
1717 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1719 crtxg = bbt->bt_crtxg;
1720 lrtxg = lr->lr_common.lrc_txg;
1722 tx = dmu_tx_create(os);
1724 dmu_tx_hold_write(tx, lr->lr_foid, offset, length);
1726 if (ztest_random(8) == 0 && length == doi.doi_data_block_size &&
1727 P2PHASE(offset, length) == 0)
1728 abuf = dmu_request_arcbuf(db, length);
1730 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1733 dmu_return_arcbuf(abuf);
1734 dmu_buf_rele(db, FTAG);
1735 ztest_range_unlock(rl);
1736 ztest_object_unlock(zd, lr->lr_foid);
1742 * Usually, verify the old data before writing new data --
1743 * but not always, because we also want to verify correct
1744 * behavior when the data was not recently read into cache.
1746 ASSERT(offset % doi.doi_data_block_size == 0);
1747 if (ztest_random(4) != 0) {
1748 int prefetch = ztest_random(2) ?
1749 DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
1750 ztest_block_tag_t rbt;
1752 VERIFY(dmu_read(os, lr->lr_foid, offset,
1753 sizeof (rbt), &rbt, prefetch) == 0);
1754 if (rbt.bt_magic == BT_MAGIC) {
1755 ztest_bt_verify(&rbt, os, lr->lr_foid, 0,
1756 offset, gen, txg, crtxg);
1761 * Writes can appear to be newer than the bonus buffer because
1762 * the ztest_get_data() callback does a dmu_read() of the
1763 * open-context data, which may be different than the data
1764 * as it was when the write was generated.
1766 if (zd->zd_zilog->zl_replay) {
1767 ztest_bt_verify(bt, os, lr->lr_foid, 0, offset,
1768 MAX(gen, bt->bt_gen), MAX(txg, lrtxg),
1773 * Set the bt's gen/txg to the bonus buffer's gen/txg
1774 * so that all of the usual ASSERTs will work.
1776 ztest_bt_generate(bt, os, lr->lr_foid, 0, offset, gen, txg,
1781 dmu_write(os, lr->lr_foid, offset, length, data, tx);
1783 bcopy(data, abuf->b_data, length);
1784 dmu_assign_arcbuf(db, offset, abuf, tx);
1787 (void) ztest_log_write(zd, tx, lr);
1789 dmu_buf_rele(db, FTAG);
1793 ztest_range_unlock(rl);
1794 ztest_object_unlock(zd, lr->lr_foid);
1800 ztest_replay_truncate(void *arg1, void *arg2, boolean_t byteswap)
1802 ztest_ds_t *zd = arg1;
1803 lr_truncate_t *lr = arg2;
1804 objset_t *os = zd->zd_os;
1810 byteswap_uint64_array(lr, sizeof (*lr));
1812 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1813 rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length,
1816 tx = dmu_tx_create(os);
1818 dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length);
1820 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1822 ztest_range_unlock(rl);
1823 ztest_object_unlock(zd, lr->lr_foid);
1827 VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset,
1828 lr->lr_length, tx) == 0);
1830 (void) ztest_log_truncate(zd, tx, lr);
1834 ztest_range_unlock(rl);
1835 ztest_object_unlock(zd, lr->lr_foid);
1841 ztest_replay_setattr(void *arg1, void *arg2, boolean_t byteswap)
1843 ztest_ds_t *zd = arg1;
1844 lr_setattr_t *lr = arg2;
1845 objset_t *os = zd->zd_os;
1848 ztest_block_tag_t *bbt;
1849 uint64_t txg, lrtxg, crtxg, dnodesize;
1852 byteswap_uint64_array(lr, sizeof (*lr));
1854 ztest_object_lock(zd, lr->lr_foid, RL_WRITER);
1856 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1858 tx = dmu_tx_create(os);
1859 dmu_tx_hold_bonus(tx, lr->lr_foid);
1861 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1863 dmu_buf_rele(db, FTAG);
1864 ztest_object_unlock(zd, lr->lr_foid);
1868 bbt = ztest_bt_bonus(db);
1869 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1870 crtxg = bbt->bt_crtxg;
1871 lrtxg = lr->lr_common.lrc_txg;
1872 dnodesize = bbt->bt_dnodesize;
1874 if (zd->zd_zilog->zl_replay) {
1875 ASSERT(lr->lr_size != 0);
1876 ASSERT(lr->lr_mode != 0);
1880 * Randomly change the size and increment the generation.
1882 lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) *
1884 lr->lr_mode = bbt->bt_gen + 1;
1889 * Verify that the current bonus buffer is not newer than our txg.
1891 ztest_bt_verify(bbt, os, lr->lr_foid, dnodesize, -1ULL, lr->lr_mode,
1892 MAX(txg, lrtxg), crtxg);
1894 dmu_buf_will_dirty(db, tx);
1896 ASSERT3U(lr->lr_size, >=, sizeof (*bbt));
1897 ASSERT3U(lr->lr_size, <=, db->db_size);
1898 VERIFY0(dmu_set_bonus(db, lr->lr_size, tx));
1899 bbt = ztest_bt_bonus(db);
1901 ztest_bt_generate(bbt, os, lr->lr_foid, dnodesize, -1ULL, lr->lr_mode,
1903 ztest_fill_unused_bonus(db, bbt, lr->lr_foid, os, bbt->bt_gen);
1905 dmu_buf_rele(db, FTAG);
1907 (void) ztest_log_setattr(zd, tx, lr);
1911 ztest_object_unlock(zd, lr->lr_foid);
1916 zil_replay_func_t *ztest_replay_vector[TX_MAX_TYPE] = {
1917 NULL, /* 0 no such transaction type */
1918 ztest_replay_create, /* TX_CREATE */
1919 NULL, /* TX_MKDIR */
1920 NULL, /* TX_MKXATTR */
1921 NULL, /* TX_SYMLINK */
1922 ztest_replay_remove, /* TX_REMOVE */
1923 NULL, /* TX_RMDIR */
1925 NULL, /* TX_RENAME */
1926 ztest_replay_write, /* TX_WRITE */
1927 ztest_replay_truncate, /* TX_TRUNCATE */
1928 ztest_replay_setattr, /* TX_SETATTR */
1930 NULL, /* TX_CREATE_ACL */
1931 NULL, /* TX_CREATE_ATTR */
1932 NULL, /* TX_CREATE_ACL_ATTR */
1933 NULL, /* TX_MKDIR_ACL */
1934 NULL, /* TX_MKDIR_ATTR */
1935 NULL, /* TX_MKDIR_ACL_ATTR */
1936 NULL, /* TX_WRITE2 */
1940 * ZIL get_data callbacks
1945 ztest_get_done(zgd_t *zgd, int error)
1947 ztest_ds_t *zd = zgd->zgd_private;
1948 uint64_t object = ((rl_t *)zgd->zgd_lr)->rl_object;
1951 dmu_buf_rele(zgd->zgd_db, zgd);
1953 ztest_range_unlock((rl_t *)zgd->zgd_lr);
1954 ztest_object_unlock(zd, object);
1956 umem_free(zgd, sizeof (*zgd));
1960 ztest_get_data(void *arg, lr_write_t *lr, char *buf, struct lwb *lwb,
1963 ztest_ds_t *zd = arg;
1964 objset_t *os = zd->zd_os;
1965 uint64_t object = lr->lr_foid;
1966 uint64_t offset = lr->lr_offset;
1967 uint64_t size = lr->lr_length;
1968 uint64_t txg = lr->lr_common.lrc_txg;
1970 dmu_object_info_t doi;
1975 ASSERT3P(lwb, !=, NULL);
1976 ASSERT3P(zio, !=, NULL);
1977 ASSERT3U(size, !=, 0);
1979 ztest_object_lock(zd, object, RL_READER);
1980 error = dmu_bonus_hold(os, object, FTAG, &db);
1982 ztest_object_unlock(zd, object);
1986 crtxg = ztest_bt_bonus(db)->bt_crtxg;
1988 if (crtxg == 0 || crtxg > txg) {
1989 dmu_buf_rele(db, FTAG);
1990 ztest_object_unlock(zd, object);
1994 dmu_object_info_from_db(db, &doi);
1995 dmu_buf_rele(db, FTAG);
1998 zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL);
2000 zgd->zgd_private = zd;
2002 if (buf != NULL) { /* immediate write */
2003 zgd->zgd_lr = (struct locked_range *)ztest_range_lock(zd,
2004 object, offset, size, RL_READER);
2006 error = dmu_read(os, object, offset, size, buf,
2007 DMU_READ_NO_PREFETCH);
2010 size = doi.doi_data_block_size;
2012 offset = P2ALIGN(offset, size);
2014 ASSERT(offset < size);
2018 zgd->zgd_lr = (struct locked_range *)ztest_range_lock(zd,
2019 object, offset, size, RL_READER);
2021 error = dmu_buf_hold(os, object, offset, zgd, &db,
2022 DMU_READ_NO_PREFETCH);
2025 blkptr_t *bp = &lr->lr_blkptr;
2030 ASSERT(db->db_offset == offset);
2031 ASSERT(db->db_size == size);
2033 error = dmu_sync(zio, lr->lr_common.lrc_txg,
2034 ztest_get_done, zgd);
2041 ztest_get_done(zgd, error);
2047 ztest_lr_alloc(size_t lrsize, char *name)
2050 size_t namesize = name ? strlen(name) + 1 : 0;
2052 lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
2055 bcopy(name, lr + lrsize, namesize);
2061 ztest_lr_free(void *lr, size_t lrsize, char *name)
2063 size_t namesize = name ? strlen(name) + 1 : 0;
2065 umem_free(lr, lrsize + namesize);
2069 * Lookup a bunch of objects. Returns the number of objects not found.
2072 ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
2077 ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
2079 for (int i = 0; i < count; i++, od++) {
2081 error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
2082 sizeof (uint64_t), 1, &od->od_object);
2084 ASSERT(error == ENOENT);
2085 ASSERT(od->od_object == 0);
2089 ztest_block_tag_t *bbt;
2090 dmu_object_info_t doi;
2092 ASSERT(od->od_object != 0);
2093 ASSERT(missing == 0); /* there should be no gaps */
2095 ztest_object_lock(zd, od->od_object, RL_READER);
2096 VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
2097 od->od_object, FTAG, &db));
2098 dmu_object_info_from_db(db, &doi);
2099 bbt = ztest_bt_bonus(db);
2100 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
2101 od->od_type = doi.doi_type;
2102 od->od_blocksize = doi.doi_data_block_size;
2103 od->od_gen = bbt->bt_gen;
2104 dmu_buf_rele(db, FTAG);
2105 ztest_object_unlock(zd, od->od_object);
2113 ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
2117 ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
2119 for (int i = 0; i < count; i++, od++) {
2126 lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2128 lr->lr_doid = od->od_dir;
2129 lr->lr_foid = 0; /* 0 to allocate, > 0 to claim */
2130 lr->lrz_type = od->od_crtype;
2131 lr->lrz_blocksize = od->od_crblocksize;
2132 lr->lrz_ibshift = ztest_random_ibshift();
2133 lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
2134 lr->lrz_dnodesize = od->od_crdnodesize;
2135 lr->lr_gen = od->od_crgen;
2136 lr->lr_crtime[0] = time(NULL);
2138 if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
2139 ASSERT(missing == 0);
2143 od->od_object = lr->lr_foid;
2144 od->od_type = od->od_crtype;
2145 od->od_blocksize = od->od_crblocksize;
2146 od->od_gen = od->od_crgen;
2147 ASSERT(od->od_object != 0);
2150 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2157 ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
2162 ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
2166 for (int i = count - 1; i >= 0; i--, od--) {
2173 * No object was found.
2175 if (od->od_object == 0)
2178 lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2180 lr->lr_doid = od->od_dir;
2182 if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
2183 ASSERT3U(error, ==, ENOSPC);
2188 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2195 ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
2201 lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
2203 lr->lr_foid = object;
2204 lr->lr_offset = offset;
2205 lr->lr_length = size;
2207 BP_ZERO(&lr->lr_blkptr);
2209 bcopy(data, lr + 1, size);
2211 error = ztest_replay_write(zd, lr, B_FALSE);
2213 ztest_lr_free(lr, sizeof (*lr) + size, NULL);
2219 ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2224 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2226 lr->lr_foid = object;
2227 lr->lr_offset = offset;
2228 lr->lr_length = size;
2230 error = ztest_replay_truncate(zd, lr, B_FALSE);
2232 ztest_lr_free(lr, sizeof (*lr), NULL);
2238 ztest_setattr(ztest_ds_t *zd, uint64_t object)
2243 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2245 lr->lr_foid = object;
2249 error = ztest_replay_setattr(zd, lr, B_FALSE);
2251 ztest_lr_free(lr, sizeof (*lr), NULL);
2257 ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2259 objset_t *os = zd->zd_os;
2264 txg_wait_synced(dmu_objset_pool(os), 0);
2266 ztest_object_lock(zd, object, RL_READER);
2267 rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
2269 tx = dmu_tx_create(os);
2271 dmu_tx_hold_write(tx, object, offset, size);
2273 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
2276 dmu_prealloc(os, object, offset, size, tx);
2278 txg_wait_synced(dmu_objset_pool(os), txg);
2280 (void) dmu_free_long_range(os, object, offset, size);
2283 ztest_range_unlock(rl);
2284 ztest_object_unlock(zd, object);
2288 ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
2291 ztest_block_tag_t wbt;
2292 dmu_object_info_t doi;
2293 enum ztest_io_type io_type;
2297 VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
2298 blocksize = doi.doi_data_block_size;
2299 data = umem_alloc(blocksize, UMEM_NOFAIL);
2302 * Pick an i/o type at random, biased toward writing block tags.
2304 io_type = ztest_random(ZTEST_IO_TYPES);
2305 if (ztest_random(2) == 0)
2306 io_type = ZTEST_IO_WRITE_TAG;
2308 rw_enter(&zd->zd_zilog_lock, RW_READER);
2312 case ZTEST_IO_WRITE_TAG:
2313 ztest_bt_generate(&wbt, zd->zd_os, object, doi.doi_dnodesize,
2315 (void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
2318 case ZTEST_IO_WRITE_PATTERN:
2319 (void) memset(data, 'a' + (object + offset) % 5, blocksize);
2320 if (ztest_random(2) == 0) {
2322 * Induce fletcher2 collisions to ensure that
2323 * zio_ddt_collision() detects and resolves them
2324 * when using fletcher2-verify for deduplication.
2326 ((uint64_t *)data)[0] ^= 1ULL << 63;
2327 ((uint64_t *)data)[4] ^= 1ULL << 63;
2329 (void) ztest_write(zd, object, offset, blocksize, data);
2332 case ZTEST_IO_WRITE_ZEROES:
2333 bzero(data, blocksize);
2334 (void) ztest_write(zd, object, offset, blocksize, data);
2337 case ZTEST_IO_TRUNCATE:
2338 (void) ztest_truncate(zd, object, offset, blocksize);
2341 case ZTEST_IO_SETATTR:
2342 (void) ztest_setattr(zd, object);
2345 case ZTEST_IO_REWRITE:
2346 rw_enter(&ztest_name_lock, RW_READER);
2347 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2348 ZFS_PROP_CHECKSUM, spa_dedup_checksum(ztest_spa),
2350 VERIFY(err == 0 || err == ENOSPC);
2351 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2352 ZFS_PROP_COMPRESSION,
2353 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION),
2355 VERIFY(err == 0 || err == ENOSPC);
2356 rw_exit(&ztest_name_lock);
2358 VERIFY0(dmu_read(zd->zd_os, object, offset, blocksize, data,
2359 DMU_READ_NO_PREFETCH));
2361 (void) ztest_write(zd, object, offset, blocksize, data);
2365 rw_exit(&zd->zd_zilog_lock);
2367 umem_free(data, blocksize);
2371 * Initialize an object description template.
2374 ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
2375 dmu_object_type_t type, uint64_t blocksize, uint64_t dnodesize,
2378 od->od_dir = ZTEST_DIROBJ;
2381 od->od_crtype = type;
2382 od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
2383 od->od_crdnodesize = dnodesize ? dnodesize : ztest_random_dnodesize();
2386 od->od_type = DMU_OT_NONE;
2387 od->od_blocksize = 0;
2390 (void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
2391 tag, (int64_t)id, index);
2395 * Lookup or create the objects for a test using the od template.
2396 * If the objects do not all exist, or if 'remove' is specified,
2397 * remove any existing objects and create new ones. Otherwise,
2398 * use the existing objects.
2401 ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
2403 int count = size / sizeof (*od);
2406 mutex_enter(&zd->zd_dirobj_lock);
2407 if ((ztest_lookup(zd, od, count) != 0 || remove) &&
2408 (ztest_remove(zd, od, count) != 0 ||
2409 ztest_create(zd, od, count) != 0))
2412 mutex_exit(&zd->zd_dirobj_lock);
2419 ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
2421 zilog_t *zilog = zd->zd_zilog;
2423 rw_enter(&zd->zd_zilog_lock, RW_READER);
2425 zil_commit(zilog, ztest_random(ZTEST_OBJECTS));
2428 * Remember the committed values in zd, which is in parent/child
2429 * shared memory. If we die, the next iteration of ztest_run()
2430 * will verify that the log really does contain this record.
2432 mutex_enter(&zilog->zl_lock);
2433 ASSERT(zd->zd_shared != NULL);
2434 ASSERT3U(zd->zd_shared->zd_seq, <=, zilog->zl_commit_lr_seq);
2435 zd->zd_shared->zd_seq = zilog->zl_commit_lr_seq;
2436 mutex_exit(&zilog->zl_lock);
2438 rw_exit(&zd->zd_zilog_lock);
2442 * This function is designed to simulate the operations that occur during a
2443 * mount/unmount operation. We hold the dataset across these operations in an
2444 * attempt to expose any implicit assumptions about ZIL management.
2448 ztest_zil_remount(ztest_ds_t *zd, uint64_t id)
2450 objset_t *os = zd->zd_os;
2453 * We grab the zd_dirobj_lock to ensure that no other thread is
2454 * updating the zil (i.e. adding in-memory log records) and the
2455 * zd_zilog_lock to block any I/O.
2457 mutex_enter(&zd->zd_dirobj_lock);
2458 rw_enter(&zd->zd_zilog_lock, RW_WRITER);
2460 /* zfsvfs_teardown() */
2461 zil_close(zd->zd_zilog);
2463 /* zfsvfs_setup() */
2464 VERIFY(zil_open(os, ztest_get_data) == zd->zd_zilog);
2465 zil_replay(os, zd, ztest_replay_vector);
2467 rw_exit(&zd->zd_zilog_lock);
2468 mutex_exit(&zd->zd_dirobj_lock);
2472 * Verify that we can't destroy an active pool, create an existing pool,
2473 * or create a pool with a bad vdev spec.
2477 ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2479 ztest_shared_opts_t *zo = &ztest_opts;
2484 * Attempt to create using a bad file.
2486 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2487 VERIFY3U(ENOENT, ==,
2488 spa_create("ztest_bad_file", nvroot, NULL, NULL));
2489 nvlist_free(nvroot);
2492 * Attempt to create using a bad mirror.
2494 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 2, 1);
2495 VERIFY3U(ENOENT, ==,
2496 spa_create("ztest_bad_mirror", nvroot, NULL, NULL));
2497 nvlist_free(nvroot);
2500 * Attempt to create an existing pool. It shouldn't matter
2501 * what's in the nvroot; we should fail with EEXIST.
2503 rw_enter(&ztest_name_lock, RW_READER);
2504 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2505 VERIFY3U(EEXIST, ==, spa_create(zo->zo_pool, nvroot, NULL, NULL));
2506 nvlist_free(nvroot);
2507 VERIFY3U(0, ==, spa_open(zo->zo_pool, &spa, FTAG));
2508 VERIFY3U(EBUSY, ==, spa_destroy(zo->zo_pool));
2509 spa_close(spa, FTAG);
2511 rw_exit(&ztest_name_lock);
2516 ztest_spa_upgrade(ztest_ds_t *zd, uint64_t id)
2519 uint64_t initial_version = SPA_VERSION_INITIAL;
2520 uint64_t version, newversion;
2521 nvlist_t *nvroot, *props;
2524 mutex_enter(&ztest_vdev_lock);
2525 name = kmem_asprintf("%s_upgrade", ztest_opts.zo_pool);
2528 * Clean up from previous runs.
2530 (void) spa_destroy(name);
2532 nvroot = make_vdev_root(NULL, NULL, name, ztest_opts.zo_vdev_size, 0,
2533 0, ztest_opts.zo_raidz, ztest_opts.zo_mirrors, 1);
2536 * If we're configuring a RAIDZ device then make sure that the
2537 * the initial version is capable of supporting that feature.
2539 switch (ztest_opts.zo_raidz_parity) {
2542 initial_version = SPA_VERSION_INITIAL;
2545 initial_version = SPA_VERSION_RAIDZ2;
2548 initial_version = SPA_VERSION_RAIDZ3;
2553 * Create a pool with a spa version that can be upgraded. Pick
2554 * a value between initial_version and SPA_VERSION_BEFORE_FEATURES.
2557 version = ztest_random_spa_version(initial_version);
2558 } while (version > SPA_VERSION_BEFORE_FEATURES);
2560 props = fnvlist_alloc();
2561 fnvlist_add_uint64(props,
2562 zpool_prop_to_name(ZPOOL_PROP_VERSION), version);
2563 VERIFY0(spa_create(name, nvroot, props, NULL));
2564 fnvlist_free(nvroot);
2565 fnvlist_free(props);
2567 VERIFY0(spa_open(name, &spa, FTAG));
2568 VERIFY3U(spa_version(spa), ==, version);
2569 newversion = ztest_random_spa_version(version + 1);
2571 if (ztest_opts.zo_verbose >= 4) {
2572 (void) printf("upgrading spa version from %llu to %llu\n",
2573 (u_longlong_t)version, (u_longlong_t)newversion);
2576 spa_upgrade(spa, newversion);
2577 VERIFY3U(spa_version(spa), >, version);
2578 VERIFY3U(spa_version(spa), ==, fnvlist_lookup_uint64(spa->spa_config,
2579 zpool_prop_to_name(ZPOOL_PROP_VERSION)));
2580 spa_close(spa, FTAG);
2583 mutex_exit(&ztest_vdev_lock);
2587 ztest_spa_checkpoint(spa_t *spa)
2589 ASSERT(MUTEX_HELD(&ztest_checkpoint_lock));
2591 int error = spa_checkpoint(spa->spa_name);
2595 case ZFS_ERR_DEVRM_IN_PROGRESS:
2596 case ZFS_ERR_DISCARDING_CHECKPOINT:
2597 case ZFS_ERR_CHECKPOINT_EXISTS:
2600 ztest_record_enospc(FTAG);
2603 fatal(0, "spa_checkpoint(%s) = %d", spa->spa_name, error);
2608 ztest_spa_discard_checkpoint(spa_t *spa)
2610 ASSERT(MUTEX_HELD(&ztest_checkpoint_lock));
2612 int error = spa_checkpoint_discard(spa->spa_name);
2616 case ZFS_ERR_DISCARDING_CHECKPOINT:
2617 case ZFS_ERR_NO_CHECKPOINT:
2620 fatal(0, "spa_discard_checkpoint(%s) = %d",
2621 spa->spa_name, error);
2628 ztest_spa_checkpoint_create_discard(ztest_ds_t *zd, uint64_t id)
2630 spa_t *spa = ztest_spa;
2632 mutex_enter(&ztest_checkpoint_lock);
2633 if (ztest_random(2) == 0) {
2634 ztest_spa_checkpoint(spa);
2636 ztest_spa_discard_checkpoint(spa);
2638 mutex_exit(&ztest_checkpoint_lock);
2643 vdev_lookup_by_path(vdev_t *vd, const char *path)
2647 if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
2650 for (int c = 0; c < vd->vdev_children; c++)
2651 if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
2659 * Find the first available hole which can be used as a top-level.
2662 find_vdev_hole(spa_t *spa)
2664 vdev_t *rvd = spa->spa_root_vdev;
2667 ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
2669 for (c = 0; c < rvd->vdev_children; c++) {
2670 vdev_t *cvd = rvd->vdev_child[c];
2672 if (cvd->vdev_ishole)
2679 * Verify that vdev_add() works as expected.
2683 ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2685 ztest_shared_t *zs = ztest_shared;
2686 spa_t *spa = ztest_spa;
2692 mutex_enter(&ztest_vdev_lock);
2693 leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * ztest_opts.zo_raidz;
2695 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2697 ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2700 * If we have slogs then remove them 1/4 of the time.
2702 if (spa_has_slogs(spa) && ztest_random(4) == 0) {
2704 * Grab the guid from the head of the log class rotor.
2706 guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid;
2708 spa_config_exit(spa, SCL_VDEV, FTAG);
2711 * We have to grab the zs_name_lock as writer to
2712 * prevent a race between removing a slog (dmu_objset_find)
2713 * and destroying a dataset. Removing the slog will
2714 * grab a reference on the dataset which may cause
2715 * dmu_objset_destroy() to fail with EBUSY thus
2716 * leaving the dataset in an inconsistent state.
2718 rw_enter(&ztest_name_lock, RW_WRITER);
2719 error = spa_vdev_remove(spa, guid, B_FALSE);
2720 rw_exit(&ztest_name_lock);
2725 case ZFS_ERR_CHECKPOINT_EXISTS:
2726 case ZFS_ERR_DISCARDING_CHECKPOINT:
2729 fatal(0, "spa_vdev_remove() = %d", error);
2732 spa_config_exit(spa, SCL_VDEV, FTAG);
2735 * Make 1/4 of the devices be log devices.
2737 nvroot = make_vdev_root(NULL, NULL, NULL,
2738 ztest_opts.zo_vdev_size, 0,
2739 ztest_random(4) == 0, ztest_opts.zo_raidz,
2742 error = spa_vdev_add(spa, nvroot);
2743 nvlist_free(nvroot);
2749 ztest_record_enospc("spa_vdev_add");
2752 fatal(0, "spa_vdev_add() = %d", error);
2756 mutex_exit(&ztest_vdev_lock);
2760 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
2764 ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
2766 ztest_shared_t *zs = ztest_shared;
2767 spa_t *spa = ztest_spa;
2768 vdev_t *rvd = spa->spa_root_vdev;
2769 spa_aux_vdev_t *sav;
2774 if (ztest_random(2) == 0) {
2775 sav = &spa->spa_spares;
2776 aux = ZPOOL_CONFIG_SPARES;
2778 sav = &spa->spa_l2cache;
2779 aux = ZPOOL_CONFIG_L2CACHE;
2782 mutex_enter(&ztest_vdev_lock);
2784 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2786 if (sav->sav_count != 0 && ztest_random(4) == 0) {
2788 * Pick a random device to remove.
2790 guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
2793 * Find an unused device we can add.
2795 zs->zs_vdev_aux = 0;
2797 char path[MAXPATHLEN];
2799 (void) snprintf(path, sizeof (path), ztest_aux_template,
2800 ztest_opts.zo_dir, ztest_opts.zo_pool, aux,
2802 for (c = 0; c < sav->sav_count; c++)
2803 if (strcmp(sav->sav_vdevs[c]->vdev_path,
2806 if (c == sav->sav_count &&
2807 vdev_lookup_by_path(rvd, path) == NULL)
2813 spa_config_exit(spa, SCL_VDEV, FTAG);
2819 nvlist_t *nvroot = make_vdev_root(NULL, aux, NULL,
2820 (ztest_opts.zo_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
2821 error = spa_vdev_add(spa, nvroot);
2827 fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
2829 nvlist_free(nvroot);
2832 * Remove an existing device. Sometimes, dirty its
2833 * vdev state first to make sure we handle removal
2834 * of devices that have pending state changes.
2836 if (ztest_random(2) == 0)
2837 (void) vdev_online(spa, guid, 0, NULL);
2839 error = spa_vdev_remove(spa, guid, B_FALSE);
2844 case ZFS_ERR_CHECKPOINT_EXISTS:
2845 case ZFS_ERR_DISCARDING_CHECKPOINT:
2848 fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
2852 mutex_exit(&ztest_vdev_lock);
2856 * split a pool if it has mirror tlvdevs
2860 ztest_split_pool(ztest_ds_t *zd, uint64_t id)
2862 ztest_shared_t *zs = ztest_shared;
2863 spa_t *spa = ztest_spa;
2864 vdev_t *rvd = spa->spa_root_vdev;
2865 nvlist_t *tree, **child, *config, *split, **schild;
2866 uint_t c, children, schildren = 0, lastlogid = 0;
2869 mutex_enter(&ztest_vdev_lock);
2871 /* ensure we have a useable config; mirrors of raidz aren't supported */
2872 if (zs->zs_mirrors < 3 || ztest_opts.zo_raidz > 1) {
2873 mutex_exit(&ztest_vdev_lock);
2877 /* clean up the old pool, if any */
2878 (void) spa_destroy("splitp");
2880 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2882 /* generate a config from the existing config */
2883 mutex_enter(&spa->spa_props_lock);
2884 VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
2886 mutex_exit(&spa->spa_props_lock);
2888 VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2891 schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
2892 for (c = 0; c < children; c++) {
2893 vdev_t *tvd = rvd->vdev_child[c];
2897 if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
2898 VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
2900 VERIFY(nvlist_add_string(schild[schildren],
2901 ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
2902 VERIFY(nvlist_add_uint64(schild[schildren],
2903 ZPOOL_CONFIG_IS_HOLE, 1) == 0);
2905 lastlogid = schildren;
2910 VERIFY(nvlist_lookup_nvlist_array(child[c],
2911 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2912 VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
2915 /* OK, create a config that can be used to split */
2916 VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
2917 VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
2918 VDEV_TYPE_ROOT) == 0);
2919 VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
2920 lastlogid != 0 ? lastlogid : schildren) == 0);
2922 VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
2923 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
2925 for (c = 0; c < schildren; c++)
2926 nvlist_free(schild[c]);
2930 spa_config_exit(spa, SCL_VDEV, FTAG);
2932 rw_enter(&ztest_name_lock, RW_WRITER);
2933 error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
2934 rw_exit(&ztest_name_lock);
2936 nvlist_free(config);
2939 (void) printf("successful split - results:\n");
2940 mutex_enter(&spa_namespace_lock);
2941 show_pool_stats(spa);
2942 show_pool_stats(spa_lookup("splitp"));
2943 mutex_exit(&spa_namespace_lock);
2947 mutex_exit(&ztest_vdev_lock);
2951 * Verify that we can attach and detach devices.
2955 ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
2957 ztest_shared_t *zs = ztest_shared;
2958 spa_t *spa = ztest_spa;
2959 spa_aux_vdev_t *sav = &spa->spa_spares;
2960 vdev_t *rvd = spa->spa_root_vdev;
2961 vdev_t *oldvd, *newvd, *pvd;
2965 uint64_t ashift = ztest_get_ashift();
2966 uint64_t oldguid, pguid;
2967 uint64_t oldsize, newsize;
2968 char oldpath[MAXPATHLEN], newpath[MAXPATHLEN];
2970 int oldvd_has_siblings = B_FALSE;
2971 int newvd_is_spare = B_FALSE;
2973 int error, expected_error;
2975 mutex_enter(&ztest_vdev_lock);
2976 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
2978 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2981 * If a vdev is in the process of being removed, its removal may
2982 * finish while we are in progress, leading to an unexpected error
2983 * value. Don't bother trying to attach while we are in the middle
2986 if (ztest_device_removal_active) {
2987 spa_config_exit(spa, SCL_ALL, FTAG);
2988 mutex_exit(&ztest_vdev_lock);
2993 * Decide whether to do an attach or a replace.
2995 replacing = ztest_random(2);
2998 * Pick a random top-level vdev.
3000 top = ztest_random_vdev_top(spa, B_TRUE);
3003 * Pick a random leaf within it.
3005 leaf = ztest_random(leaves);
3010 oldvd = rvd->vdev_child[top];
3011 if (zs->zs_mirrors >= 1) {
3012 ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
3013 ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
3014 oldvd = oldvd->vdev_child[leaf / ztest_opts.zo_raidz];
3016 if (ztest_opts.zo_raidz > 1) {
3017 ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
3018 ASSERT(oldvd->vdev_children == ztest_opts.zo_raidz);
3019 oldvd = oldvd->vdev_child[leaf % ztest_opts.zo_raidz];
3023 * If we're already doing an attach or replace, oldvd may be a
3024 * mirror vdev -- in which case, pick a random child.
3026 while (oldvd->vdev_children != 0) {
3027 oldvd_has_siblings = B_TRUE;
3028 ASSERT(oldvd->vdev_children >= 2);
3029 oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
3032 oldguid = oldvd->vdev_guid;
3033 oldsize = vdev_get_min_asize(oldvd);
3034 oldvd_is_log = oldvd->vdev_top->vdev_islog;
3035 (void) strcpy(oldpath, oldvd->vdev_path);
3036 pvd = oldvd->vdev_parent;
3037 pguid = pvd->vdev_guid;
3040 * If oldvd has siblings, then half of the time, detach it.
3042 if (oldvd_has_siblings && ztest_random(2) == 0) {
3043 spa_config_exit(spa, SCL_ALL, FTAG);
3044 error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
3045 if (error != 0 && error != ENODEV && error != EBUSY &&
3046 error != ENOTSUP && error != ZFS_ERR_CHECKPOINT_EXISTS &&
3047 error != ZFS_ERR_DISCARDING_CHECKPOINT)
3048 fatal(0, "detach (%s) returned %d", oldpath, error);
3049 mutex_exit(&ztest_vdev_lock);
3054 * For the new vdev, choose with equal probability between the two
3055 * standard paths (ending in either 'a' or 'b') or a random hot spare.
3057 if (sav->sav_count != 0 && ztest_random(3) == 0) {
3058 newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
3059 newvd_is_spare = B_TRUE;
3060 (void) strcpy(newpath, newvd->vdev_path);
3062 (void) snprintf(newpath, sizeof (newpath), ztest_dev_template,
3063 ztest_opts.zo_dir, ztest_opts.zo_pool,
3064 top * leaves + leaf);
3065 if (ztest_random(2) == 0)
3066 newpath[strlen(newpath) - 1] = 'b';
3067 newvd = vdev_lookup_by_path(rvd, newpath);
3072 * Reopen to ensure the vdev's asize field isn't stale.
3075 newsize = vdev_get_min_asize(newvd);
3078 * Make newsize a little bigger or smaller than oldsize.
3079 * If it's smaller, the attach should fail.
3080 * If it's larger, and we're doing a replace,
3081 * we should get dynamic LUN growth when we're done.
3083 newsize = 10 * oldsize / (9 + ztest_random(3));
3087 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
3088 * unless it's a replace; in that case any non-replacing parent is OK.
3090 * If newvd is already part of the pool, it should fail with EBUSY.
3092 * If newvd is too small, it should fail with EOVERFLOW.
3094 if (pvd->vdev_ops != &vdev_mirror_ops &&
3095 pvd->vdev_ops != &vdev_root_ops && (!replacing ||
3096 pvd->vdev_ops == &vdev_replacing_ops ||
3097 pvd->vdev_ops == &vdev_spare_ops))
3098 expected_error = ENOTSUP;
3099 else if (newvd_is_spare && (!replacing || oldvd_is_log))
3100 expected_error = ENOTSUP;
3101 else if (newvd == oldvd)
3102 expected_error = replacing ? 0 : EBUSY;
3103 else if (vdev_lookup_by_path(rvd, newpath) != NULL)
3104 expected_error = EBUSY;
3105 else if (newsize < oldsize)
3106 expected_error = EOVERFLOW;
3107 else if (ashift > oldvd->vdev_top->vdev_ashift)
3108 expected_error = EDOM;
3112 spa_config_exit(spa, SCL_ALL, FTAG);
3115 * Build the nvlist describing newpath.
3117 root = make_vdev_root(newpath, NULL, NULL, newvd == NULL ? newsize : 0,
3118 ashift, 0, 0, 0, 1);
3120 error = spa_vdev_attach(spa, oldguid, root, replacing);
3125 * If our parent was the replacing vdev, but the replace completed,
3126 * then instead of failing with ENOTSUP we may either succeed,
3127 * fail with ENODEV, or fail with EOVERFLOW.
3129 if (expected_error == ENOTSUP &&
3130 (error == 0 || error == ENODEV || error == EOVERFLOW))
3131 expected_error = error;
3134 * If someone grew the LUN, the replacement may be too small.
3136 if (error == EOVERFLOW || error == EBUSY)
3137 expected_error = error;
3139 if (error == ZFS_ERR_CHECKPOINT_EXISTS ||
3140 error == ZFS_ERR_DISCARDING_CHECKPOINT)
3141 expected_error = error;
3143 /* XXX workaround 6690467 */
3144 if (error != expected_error && expected_error != EBUSY) {
3145 fatal(0, "attach (%s %llu, %s %llu, %d) "
3146 "returned %d, expected %d",
3147 oldpath, oldsize, newpath,
3148 newsize, replacing, error, expected_error);
3151 mutex_exit(&ztest_vdev_lock);
3156 ztest_device_removal(ztest_ds_t *zd, uint64_t id)
3158 spa_t *spa = ztest_spa;
3163 mutex_enter(&ztest_vdev_lock);
3165 if (ztest_device_removal_active) {
3166 mutex_exit(&ztest_vdev_lock);
3171 * Remove a random top-level vdev and wait for removal to finish.
3173 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
3174 vd = vdev_lookup_top(spa, ztest_random_vdev_top(spa, B_FALSE));
3175 guid = vd->vdev_guid;
3176 spa_config_exit(spa, SCL_VDEV, FTAG);
3178 error = spa_vdev_remove(spa, guid, B_FALSE);
3180 ztest_device_removal_active = B_TRUE;
3181 mutex_exit(&ztest_vdev_lock);
3183 while (spa->spa_vdev_removal != NULL)
3184 txg_wait_synced(spa_get_dsl(spa), 0);
3186 mutex_exit(&ztest_vdev_lock);
3191 * The pool needs to be scrubbed after completing device removal.
3192 * Failure to do so may result in checksum errors due to the
3193 * strategy employed by ztest_fault_inject() when selecting which
3194 * offset are redundant and can be damaged.
3196 error = spa_scan(spa, POOL_SCAN_SCRUB);
3198 while (dsl_scan_scrubbing(spa_get_dsl(spa)))
3199 txg_wait_synced(spa_get_dsl(spa), 0);
3202 mutex_enter(&ztest_vdev_lock);
3203 ztest_device_removal_active = B_FALSE;
3204 mutex_exit(&ztest_vdev_lock);
3208 * Callback function which expands the physical size of the vdev.
3211 grow_vdev(vdev_t *vd, void *arg)
3213 spa_t *spa = vd->vdev_spa;
3214 size_t *newsize = arg;
3218 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3219 ASSERT(vd->vdev_ops->vdev_op_leaf);
3221 if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
3224 fsize = lseek(fd, 0, SEEK_END);
3225 (void) ftruncate(fd, *newsize);
3227 if (ztest_opts.zo_verbose >= 6) {
3228 (void) printf("%s grew from %lu to %lu bytes\n",
3229 vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
3236 * Callback function which expands a given vdev by calling vdev_online().
3240 online_vdev(vdev_t *vd, void *arg)
3242 spa_t *spa = vd->vdev_spa;
3243 vdev_t *tvd = vd->vdev_top;
3244 uint64_t guid = vd->vdev_guid;
3245 uint64_t generation = spa->spa_config_generation + 1;
3246 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
3249 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3250 ASSERT(vd->vdev_ops->vdev_op_leaf);
3252 /* Calling vdev_online will initialize the new metaslabs */
3253 spa_config_exit(spa, SCL_STATE, spa);
3254 error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
3255 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3258 * If vdev_online returned an error or the underlying vdev_open
3259 * failed then we abort the expand. The only way to know that
3260 * vdev_open fails is by checking the returned newstate.
3262 if (error || newstate != VDEV_STATE_HEALTHY) {
3263 if (ztest_opts.zo_verbose >= 5) {
3264 (void) printf("Unable to expand vdev, state %llu, "
3265 "error %d\n", (u_longlong_t)newstate, error);
3269 ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
3272 * Since we dropped the lock we need to ensure that we're
3273 * still talking to the original vdev. It's possible this
3274 * vdev may have been detached/replaced while we were
3275 * trying to online it.
3277 if (generation != spa->spa_config_generation) {
3278 if (ztest_opts.zo_verbose >= 5) {
3279 (void) printf("vdev configuration has changed, "
3280 "guid %llu, state %llu, expected gen %llu, "
3283 (u_longlong_t)tvd->vdev_state,
3284 (u_longlong_t)generation,
3285 (u_longlong_t)spa->spa_config_generation);
3293 * Traverse the vdev tree calling the supplied function.
3294 * We continue to walk the tree until we either have walked all
3295 * children or we receive a non-NULL return from the callback.
3296 * If a NULL callback is passed, then we just return back the first
3297 * leaf vdev we encounter.
3300 vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
3302 if (vd->vdev_ops->vdev_op_leaf) {
3306 return (func(vd, arg));
3309 for (uint_t c = 0; c < vd->vdev_children; c++) {
3310 vdev_t *cvd = vd->vdev_child[c];
3311 if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
3318 * Verify that dynamic LUN growth works as expected.
3322 ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
3324 spa_t *spa = ztest_spa;
3326 metaslab_class_t *mc;
3327 metaslab_group_t *mg;
3328 size_t psize, newsize;
3330 uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
3332 mutex_enter(&ztest_checkpoint_lock);
3333 mutex_enter(&ztest_vdev_lock);
3334 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3337 * If there is a vdev removal in progress, it could complete while
3338 * we are running, in which case we would not be able to verify
3339 * that the metaslab_class space increased (because it decreases
3340 * when the device removal completes).
3342 if (ztest_device_removal_active) {
3343 spa_config_exit(spa, SCL_STATE, spa);
3344 mutex_exit(&ztest_vdev_lock);
3345 mutex_exit(&ztest_checkpoint_lock);
3349 top = ztest_random_vdev_top(spa, B_TRUE);
3351 tvd = spa->spa_root_vdev->vdev_child[top];
3354 old_ms_count = tvd->vdev_ms_count;
3355 old_class_space = metaslab_class_get_space(mc);
3358 * Determine the size of the first leaf vdev associated with
3359 * our top-level device.
3361 vd = vdev_walk_tree(tvd, NULL, NULL);
3362 ASSERT3P(vd, !=, NULL);
3363 ASSERT(vd->vdev_ops->vdev_op_leaf);
3365 psize = vd->vdev_psize;
3368 * We only try to expand the vdev if it's healthy, less than 4x its
3369 * original size, and it has a valid psize.
3371 if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
3372 psize == 0 || psize >= 4 * ztest_opts.zo_vdev_size) {
3373 spa_config_exit(spa, SCL_STATE, spa);
3374 mutex_exit(&ztest_vdev_lock);
3375 mutex_exit(&ztest_checkpoint_lock);
3379 newsize = psize + psize / 8;
3380 ASSERT3U(newsize, >, psize);
3382 if (ztest_opts.zo_verbose >= 6) {
3383 (void) printf("Expanding LUN %s from %lu to %lu\n",
3384 vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
3388 * Growing the vdev is a two step process:
3389 * 1). expand the physical size (i.e. relabel)
3390 * 2). online the vdev to create the new metaslabs
3392 if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
3393 vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
3394 tvd->vdev_state != VDEV_STATE_HEALTHY) {
3395 if (ztest_opts.zo_verbose >= 5) {
3396 (void) printf("Could not expand LUN because "
3397 "the vdev configuration changed.\n");
3399 spa_config_exit(spa, SCL_STATE, spa);
3400 mutex_exit(&ztest_vdev_lock);
3401 mutex_exit(&ztest_checkpoint_lock);
3405 spa_config_exit(spa, SCL_STATE, spa);
3408 * Expanding the LUN will update the config asynchronously,
3409 * thus we must wait for the async thread to complete any
3410 * pending tasks before proceeding.
3414 mutex_enter(&spa->spa_async_lock);
3415 done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
3416 mutex_exit(&spa->spa_async_lock);
3419 txg_wait_synced(spa_get_dsl(spa), 0);
3420 (void) poll(NULL, 0, 100);
3423 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3425 tvd = spa->spa_root_vdev->vdev_child[top];
3426 new_ms_count = tvd->vdev_ms_count;
3427 new_class_space = metaslab_class_get_space(mc);
3429 if (tvd->vdev_mg != mg || mg->mg_class != mc) {
3430 if (ztest_opts.zo_verbose >= 5) {
3431 (void) printf("Could not verify LUN expansion due to "
3432 "intervening vdev offline or remove.\n");
3434 spa_config_exit(spa, SCL_STATE, spa);
3435 mutex_exit(&ztest_vdev_lock);
3436 mutex_exit(&ztest_checkpoint_lock);
3441 * Make sure we were able to grow the vdev.
3443 if (new_ms_count <= old_ms_count) {
3444 fatal(0, "LUN expansion failed: ms_count %llu < %llu\n",
3445 old_ms_count, new_ms_count);
3449 * Make sure we were able to grow the pool.
3451 if (new_class_space <= old_class_space) {
3452 fatal(0, "LUN expansion failed: class_space %llu < %llu\n",
3453 old_class_space, new_class_space);
3456 if (ztest_opts.zo_verbose >= 5) {
3457 char oldnumbuf[NN_NUMBUF_SZ], newnumbuf[NN_NUMBUF_SZ];
3459 nicenum(old_class_space, oldnumbuf, sizeof (oldnumbuf));
3460 nicenum(new_class_space, newnumbuf, sizeof (newnumbuf));
3461 (void) printf("%s grew from %s to %s\n",
3462 spa->spa_name, oldnumbuf, newnumbuf);
3465 spa_config_exit(spa, SCL_STATE, spa);
3466 mutex_exit(&ztest_vdev_lock);
3467 mutex_exit(&ztest_checkpoint_lock);
3471 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
3475 ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3478 * Create the objects common to all ztest datasets.
3480 VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
3481 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
3485 ztest_dataset_create(char *dsname)
3487 uint64_t zilset = ztest_random(100);
3488 int err = dmu_objset_create(dsname, DMU_OST_OTHER, 0,
3489 ztest_objset_create_cb, NULL);
3491 if (err || zilset < 80)
3494 if (ztest_opts.zo_verbose >= 6)
3495 (void) printf("Setting dataset %s to sync always\n", dsname);
3496 return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC,
3497 ZFS_SYNC_ALWAYS, B_FALSE));
3502 ztest_objset_destroy_cb(const char *name, void *arg)
3505 dmu_object_info_t doi;
3509 * Verify that the dataset contains a directory object.
3511 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_TRUE, FTAG, &os));
3512 error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
3513 if (error != ENOENT) {
3514 /* We could have crashed in the middle of destroying it */
3516 ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
3517 ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
3519 dmu_objset_disown(os, FTAG);
3522 * Destroy the dataset.
3524 if (strchr(name, '@') != NULL) {
3525 VERIFY0(dsl_destroy_snapshot(name, B_FALSE));
3527 VERIFY0(dsl_destroy_head(name));
3533 ztest_snapshot_create(char *osname, uint64_t id)
3535 char snapname[ZFS_MAX_DATASET_NAME_LEN];
3538 (void) snprintf(snapname, sizeof (snapname), "%llu", (u_longlong_t)id);
3540 error = dmu_objset_snapshot_one(osname, snapname);
3541 if (error == ENOSPC) {
3542 ztest_record_enospc(FTAG);
3545 if (error != 0 && error != EEXIST) {
3546 fatal(0, "ztest_snapshot_create(%s@%s) = %d", osname,
3553 ztest_snapshot_destroy(char *osname, uint64_t id)
3555 char snapname[ZFS_MAX_DATASET_NAME_LEN];
3558 (void) snprintf(snapname, sizeof (snapname), "%s@%llu", osname,
3561 error = dsl_destroy_snapshot(snapname, B_FALSE);
3562 if (error != 0 && error != ENOENT)
3563 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
3569 ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
3575 char name[ZFS_MAX_DATASET_NAME_LEN];
3578 rw_enter(&ztest_name_lock, RW_READER);
3580 (void) snprintf(name, sizeof (name), "%s/temp_%llu",
3581 ztest_opts.zo_pool, (u_longlong_t)id);
3584 * If this dataset exists from a previous run, process its replay log
3585 * half of the time. If we don't replay it, then dmu_objset_destroy()
3586 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
3588 if (ztest_random(2) == 0 &&
3589 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) {
3590 ztest_zd_init(&zdtmp, NULL, os);
3591 zil_replay(os, &zdtmp, ztest_replay_vector);
3592 ztest_zd_fini(&zdtmp);
3593 dmu_objset_disown(os, FTAG);
3597 * There may be an old instance of the dataset we're about to
3598 * create lying around from a previous run. If so, destroy it
3599 * and all of its snapshots.
3601 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
3602 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
3605 * Verify that the destroyed dataset is no longer in the namespace.
3607 VERIFY3U(ENOENT, ==, dmu_objset_own(name, DMU_OST_OTHER, B_TRUE,
3611 * Verify that we can create a new dataset.
3613 error = ztest_dataset_create(name);
3615 if (error == ENOSPC) {
3616 ztest_record_enospc(FTAG);
3617 rw_exit(&ztest_name_lock);
3620 fatal(0, "dmu_objset_create(%s) = %d", name, error);
3623 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os));
3625 ztest_zd_init(&zdtmp, NULL, os);
3628 * Open the intent log for it.
3630 zilog = zil_open(os, ztest_get_data);
3633 * Put some objects in there, do a little I/O to them,
3634 * and randomly take a couple of snapshots along the way.
3636 iters = ztest_random(5);
3637 for (int i = 0; i < iters; i++) {
3638 ztest_dmu_object_alloc_free(&zdtmp, id);
3639 if (ztest_random(iters) == 0)
3640 (void) ztest_snapshot_create(name, i);
3644 * Verify that we cannot create an existing dataset.
3646 VERIFY3U(EEXIST, ==,
3647 dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL));
3650 * Verify that we can hold an objset that is also owned.
3652 VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
3653 dmu_objset_rele(os2, FTAG);
3656 * Verify that we cannot own an objset that is already owned.
3659 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2));
3662 dmu_objset_disown(os, FTAG);
3663 ztest_zd_fini(&zdtmp);
3665 rw_exit(&ztest_name_lock);
3669 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3672 ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
3674 rw_enter(&ztest_name_lock, RW_READER);
3675 (void) ztest_snapshot_destroy(zd->zd_name, id);
3676 (void) ztest_snapshot_create(zd->zd_name, id);
3677 rw_exit(&ztest_name_lock);
3681 * Cleanup non-standard snapshots and clones.
3684 ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
3686 char snap1name[ZFS_MAX_DATASET_NAME_LEN];
3687 char clone1name[ZFS_MAX_DATASET_NAME_LEN];
3688 char snap2name[ZFS_MAX_DATASET_NAME_LEN];
3689 char clone2name[ZFS_MAX_DATASET_NAME_LEN];
3690 char snap3name[ZFS_MAX_DATASET_NAME_LEN];
3693 (void) snprintf(snap1name, sizeof (snap1name),
3694 "%s@s1_%llu", osname, id);
3695 (void) snprintf(clone1name, sizeof (clone1name),
3696 "%s/c1_%llu", osname, id);
3697 (void) snprintf(snap2name, sizeof (snap2name),
3698 "%s@s2_%llu", clone1name, id);
3699 (void) snprintf(clone2name, sizeof (clone2name),
3700 "%s/c2_%llu", osname, id);
3701 (void) snprintf(snap3name, sizeof (snap3name),
3702 "%s@s3_%llu", clone1name, id);
3704 error = dsl_destroy_head(clone2name);
3705 if (error && error != ENOENT)
3706 fatal(0, "dsl_destroy_head(%s) = %d", clone2name, error);
3707 error = dsl_destroy_snapshot(snap3name, B_FALSE);
3708 if (error && error != ENOENT)
3709 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap3name, error);
3710 error = dsl_destroy_snapshot(snap2name, B_FALSE);
3711 if (error && error != ENOENT)
3712 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap2name, error);
3713 error = dsl_destroy_head(clone1name);
3714 if (error && error != ENOENT)
3715 fatal(0, "dsl_destroy_head(%s) = %d", clone1name, error);
3716 error = dsl_destroy_snapshot(snap1name, B_FALSE);
3717 if (error && error != ENOENT)
3718 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap1name, error);
3722 * Verify dsl_dataset_promote handles EBUSY
3725 ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
3728 char snap1name[ZFS_MAX_DATASET_NAME_LEN];
3729 char clone1name[ZFS_MAX_DATASET_NAME_LEN];
3730 char snap2name[ZFS_MAX_DATASET_NAME_LEN];
3731 char clone2name[ZFS_MAX_DATASET_NAME_LEN];
3732 char snap3name[ZFS_MAX_DATASET_NAME_LEN];
3733 char *osname = zd->zd_name;
3736 rw_enter(&ztest_name_lock, RW_READER);
3738 ztest_dsl_dataset_cleanup(osname, id);
3740 (void) snprintf(snap1name, sizeof (snap1name),
3741 "%s@s1_%llu", osname, id);
3742 (void) snprintf(clone1name, sizeof (clone1name),
3743 "%s/c1_%llu", osname, id);
3744 (void) snprintf(snap2name, sizeof (snap2name),
3745 "%s@s2_%llu", clone1name, id);
3746 (void) snprintf(clone2name, sizeof (clone2name),
3747 "%s/c2_%llu", osname, id);
3748 (void) snprintf(snap3name, sizeof (snap3name),
3749 "%s@s3_%llu", clone1name, id);
3751 error = dmu_objset_snapshot_one(osname, strchr(snap1name, '@') + 1);
3752 if (error && error != EEXIST) {
3753 if (error == ENOSPC) {
3754 ztest_record_enospc(FTAG);
3757 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
3760 error = dmu_objset_clone(clone1name, snap1name);
3762 if (error == ENOSPC) {
3763 ztest_record_enospc(FTAG);
3766 fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
3769 error = dmu_objset_snapshot_one(clone1name, strchr(snap2name, '@') + 1);
3770 if (error && error != EEXIST) {
3771 if (error == ENOSPC) {
3772 ztest_record_enospc(FTAG);
3775 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
3778 error = dmu_objset_snapshot_one(clone1name, strchr(snap3name, '@') + 1);
3779 if (error && error != EEXIST) {
3780 if (error == ENOSPC) {
3781 ztest_record_enospc(FTAG);
3784 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
3787 error = dmu_objset_clone(clone2name, snap3name);
3789 if (error == ENOSPC) {
3790 ztest_record_enospc(FTAG);
3793 fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
3796 error = dmu_objset_own(snap2name, DMU_OST_ANY, B_TRUE, FTAG, &os);
3798 fatal(0, "dmu_objset_own(%s) = %d", snap2name, error);
3799 error = dsl_dataset_promote(clone2name, NULL);
3800 if (error == ENOSPC) {
3801 dmu_objset_disown(os, FTAG);
3802 ztest_record_enospc(FTAG);
3806 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
3808 dmu_objset_disown(os, FTAG);
3811 ztest_dsl_dataset_cleanup(osname, id);
3813 rw_exit(&ztest_name_lock);
3817 * Verify that dmu_object_{alloc,free} work as expected.
3820 ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
3823 int batchsize = sizeof (od) / sizeof (od[0]);
3825 for (int b = 0; b < batchsize; b++) {
3826 ztest_od_init(&od[b], id, FTAG, b, DMU_OT_UINT64_OTHER,
3831 * Destroy the previous batch of objects, create a new batch,
3832 * and do some I/O on the new objects.
3834 if (ztest_object_init(zd, od, sizeof (od), B_TRUE) != 0)
3837 while (ztest_random(4 * batchsize) != 0)
3838 ztest_io(zd, od[ztest_random(batchsize)].od_object,
3839 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3843 * Rewind the global allocator to verify object allocation backfilling.
3846 ztest_dmu_object_next_chunk(ztest_ds_t *zd, uint64_t id)
3848 objset_t *os = zd->zd_os;
3849 int dnodes_per_chunk = 1 << dmu_object_alloc_chunk_shift;
3853 * Rewind the global allocator randomly back to a lower object number
3854 * to force backfilling and reclamation of recently freed dnodes.
3856 mutex_enter(&os->os_obj_lock);
3857 object = ztest_random(os->os_obj_next_chunk);
3858 os->os_obj_next_chunk = P2ALIGN(object, dnodes_per_chunk);
3859 mutex_exit(&os->os_obj_lock);
3863 * Verify that dmu_{read,write} work as expected.
3866 ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
3868 objset_t *os = zd->zd_os;
3871 int i, freeit, error;
3873 bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
3874 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3875 uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
3876 uint64_t regions = 997;
3877 uint64_t stride = 123456789ULL;
3878 uint64_t width = 40;
3879 int free_percent = 5;
3882 * This test uses two objects, packobj and bigobj, that are always
3883 * updated together (i.e. in the same tx) so that their contents are
3884 * in sync and can be compared. Their contents relate to each other
3885 * in a simple way: packobj is a dense array of 'bufwad' structures,
3886 * while bigobj is a sparse array of the same bufwads. Specifically,
3887 * for any index n, there are three bufwads that should be identical:
3889 * packobj, at offset n * sizeof (bufwad_t)
3890 * bigobj, at the head of the nth chunk
3891 * bigobj, at the tail of the nth chunk
3893 * The chunk size is arbitrary. It doesn't have to be a power of two,
3894 * and it doesn't have any relation to the object blocksize.
3895 * The only requirement is that it can hold at least two bufwads.
3897 * Normally, we write the bufwad to each of these locations.
3898 * However, free_percent of the time we instead write zeroes to
3899 * packobj and perform a dmu_free_range() on bigobj. By comparing
3900 * bigobj to packobj, we can verify that the DMU is correctly
3901 * tracking which parts of an object are allocated and free,
3902 * and that the contents of the allocated blocks are correct.
3906 * Read the directory info. If it's the first time, set things up.
3908 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0,
3910 ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, 0,
3913 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
3916 bigobj = od[0].od_object;
3917 packobj = od[1].od_object;
3918 chunksize = od[0].od_gen;
3919 ASSERT(chunksize == od[1].od_gen);
3922 * Prefetch a random chunk of the big object.
3923 * Our aim here is to get some async reads in flight
3924 * for blocks that we may free below; the DMU should
3925 * handle this race correctly.
3927 n = ztest_random(regions) * stride + ztest_random(width);
3928 s = 1 + ztest_random(2 * width - 1);
3929 dmu_prefetch(os, bigobj, 0, n * chunksize, s * chunksize,
3930 ZIO_PRIORITY_SYNC_READ);
3933 * Pick a random index and compute the offsets into packobj and bigobj.
3935 n = ztest_random(regions) * stride + ztest_random(width);
3936 s = 1 + ztest_random(width - 1);
3938 packoff = n * sizeof (bufwad_t);
3939 packsize = s * sizeof (bufwad_t);
3941 bigoff = n * chunksize;
3942 bigsize = s * chunksize;
3944 packbuf = umem_alloc(packsize, UMEM_NOFAIL);
3945 bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
3948 * free_percent of the time, free a range of bigobj rather than
3951 freeit = (ztest_random(100) < free_percent);
3954 * Read the current contents of our objects.
3956 error = dmu_read(os, packobj, packoff, packsize, packbuf,
3959 error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
3964 * Get a tx for the mods to both packobj and bigobj.
3966 tx = dmu_tx_create(os);
3968 dmu_tx_hold_write(tx, packobj, packoff, packsize);
3971 dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
3973 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
3975 /* This accounts for setting the checksum/compression. */
3976 dmu_tx_hold_bonus(tx, bigobj);
3978 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3980 umem_free(packbuf, packsize);
3981 umem_free(bigbuf, bigsize);
3985 enum zio_checksum cksum;
3987 cksum = (enum zio_checksum)
3988 ztest_random_dsl_prop(ZFS_PROP_CHECKSUM);
3989 } while (cksum >= ZIO_CHECKSUM_LEGACY_FUNCTIONS);
3990 dmu_object_set_checksum(os, bigobj, cksum, tx);
3992 enum zio_compress comp;
3994 comp = (enum zio_compress)
3995 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION);
3996 } while (comp >= ZIO_COMPRESS_LEGACY_FUNCTIONS);
3997 dmu_object_set_compress(os, bigobj, comp, tx);
4000 * For each index from n to n + s, verify that the existing bufwad
4001 * in packobj matches the bufwads at the head and tail of the
4002 * corresponding chunk in bigobj. Then update all three bufwads
4003 * with the new values we want to write out.
4005 for (i = 0; i < s; i++) {
4007 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
4009 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
4011 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
4013 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
4014 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
4016 if (pack->bw_txg > txg)
4017 fatal(0, "future leak: got %llx, open txg is %llx",
4020 if (pack->bw_data != 0 && pack->bw_index != n + i)
4021 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4022 pack->bw_index, n, i);
4024 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
4025 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
4027 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
4028 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
4031 bzero(pack, sizeof (bufwad_t));
4033 pack->bw_index = n + i;
4035 pack->bw_data = 1 + ztest_random(-2ULL);
4042 * We've verified all the old bufwads, and made new ones.
4043 * Now write them out.
4045 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
4048 if (ztest_opts.zo_verbose >= 7) {
4049 (void) printf("freeing offset %llx size %llx"
4051 (u_longlong_t)bigoff,
4052 (u_longlong_t)bigsize,
4055 VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
4057 if (ztest_opts.zo_verbose >= 7) {
4058 (void) printf("writing offset %llx size %llx"
4060 (u_longlong_t)bigoff,
4061 (u_longlong_t)bigsize,
4064 dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
4070 * Sanity check the stuff we just wrote.
4073 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
4074 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
4076 VERIFY(0 == dmu_read(os, packobj, packoff,
4077 packsize, packcheck, DMU_READ_PREFETCH));
4078 VERIFY(0 == dmu_read(os, bigobj, bigoff,
4079 bigsize, bigcheck, DMU_READ_PREFETCH));
4081 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
4082 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
4084 umem_free(packcheck, packsize);
4085 umem_free(bigcheck, bigsize);
4088 umem_free(packbuf, packsize);
4089 umem_free(bigbuf, bigsize);
4093 compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
4094 uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
4102 * For each index from n to n + s, verify that the existing bufwad
4103 * in packobj matches the bufwads at the head and tail of the
4104 * corresponding chunk in bigobj. Then update all three bufwads
4105 * with the new values we want to write out.
4107 for (i = 0; i < s; i++) {
4109 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
4111 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
4113 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
4115 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
4116 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
4118 if (pack->bw_txg > txg)
4119 fatal(0, "future leak: got %llx, open txg is %llx",
4122 if (pack->bw_data != 0 && pack->bw_index != n + i)
4123 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4124 pack->bw_index, n, i);
4126 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
4127 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
4129 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
4130 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
4132 pack->bw_index = n + i;
4134 pack->bw_data = 1 + ztest_random(-2ULL);
4142 ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
4144 objset_t *os = zd->zd_os;
4150 bufwad_t *packbuf, *bigbuf;
4151 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
4152 uint64_t blocksize = ztest_random_blocksize();
4153 uint64_t chunksize = blocksize;
4154 uint64_t regions = 997;
4155 uint64_t stride = 123456789ULL;
4157 dmu_buf_t *bonus_db;
4158 arc_buf_t **bigbuf_arcbufs;
4159 dmu_object_info_t doi;
4162 * This test uses two objects, packobj and bigobj, that are always
4163 * updated together (i.e. in the same tx) so that their contents are
4164 * in sync and can be compared. Their contents relate to each other
4165 * in a simple way: packobj is a dense array of 'bufwad' structures,
4166 * while bigobj is a sparse array of the same bufwads. Specifically,
4167 * for any index n, there are three bufwads that should be identical:
4169 * packobj, at offset n * sizeof (bufwad_t)
4170 * bigobj, at the head of the nth chunk
4171 * bigobj, at the tail of the nth chunk
4173 * The chunk size is set equal to bigobj block size so that
4174 * dmu_assign_arcbuf() can be tested for object updates.
4178 * Read the directory info. If it's the first time, set things up.
4180 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize,
4182 ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, 0,
4185 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4188 bigobj = od[0].od_object;
4189 packobj = od[1].od_object;
4190 blocksize = od[0].od_blocksize;
4191 chunksize = blocksize;
4192 ASSERT(chunksize == od[1].od_gen);
4194 VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
4195 VERIFY(ISP2(doi.doi_data_block_size));
4196 VERIFY(chunksize == doi.doi_data_block_size);
4197 VERIFY(chunksize >= 2 * sizeof (bufwad_t));
4200 * Pick a random index and compute the offsets into packobj and bigobj.
4202 n = ztest_random(regions) * stride + ztest_random(width);
4203 s = 1 + ztest_random(width - 1);
4205 packoff = n * sizeof (bufwad_t);
4206 packsize = s * sizeof (bufwad_t);
4208 bigoff = n * chunksize;
4209 bigsize = s * chunksize;
4211 packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
4212 bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
4214 VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
4216 bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
4219 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
4220 * Iteration 1 test zcopy to already referenced dbufs.
4221 * Iteration 2 test zcopy to dirty dbuf in the same txg.
4222 * Iteration 3 test zcopy to dbuf dirty in previous txg.
4223 * Iteration 4 test zcopy when dbuf is no longer dirty.
4224 * Iteration 5 test zcopy when it can't be done.
4225 * Iteration 6 one more zcopy write.
4227 for (i = 0; i < 7; i++) {
4232 * In iteration 5 (i == 5) use arcbufs
4233 * that don't match bigobj blksz to test
4234 * dmu_assign_arcbuf() when it can't directly
4235 * assign an arcbuf to a dbuf.
4237 for (j = 0; j < s; j++) {
4240 dmu_request_arcbuf(bonus_db, chunksize);
4242 bigbuf_arcbufs[2 * j] =
4243 dmu_request_arcbuf(bonus_db, chunksize / 2);
4244 bigbuf_arcbufs[2 * j + 1] =
4245 dmu_request_arcbuf(bonus_db, chunksize / 2);
4250 * Get a tx for the mods to both packobj and bigobj.
4252 tx = dmu_tx_create(os);
4254 dmu_tx_hold_write(tx, packobj, packoff, packsize);
4255 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
4257 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4259 umem_free(packbuf, packsize);
4260 umem_free(bigbuf, bigsize);
4261 for (j = 0; j < s; j++) {
4263 dmu_return_arcbuf(bigbuf_arcbufs[j]);
4266 bigbuf_arcbufs[2 * j]);
4268 bigbuf_arcbufs[2 * j + 1]);
4271 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4272 dmu_buf_rele(bonus_db, FTAG);
4277 * 50% of the time don't read objects in the 1st iteration to
4278 * test dmu_assign_arcbuf() for the case when there're no
4279 * existing dbufs for the specified offsets.
4281 if (i != 0 || ztest_random(2) != 0) {
4282 error = dmu_read(os, packobj, packoff,
4283 packsize, packbuf, DMU_READ_PREFETCH);
4285 error = dmu_read(os, bigobj, bigoff, bigsize,
4286 bigbuf, DMU_READ_PREFETCH);
4289 compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
4293 * We've verified all the old bufwads, and made new ones.
4294 * Now write them out.
4296 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
4297 if (ztest_opts.zo_verbose >= 7) {
4298 (void) printf("writing offset %llx size %llx"
4300 (u_longlong_t)bigoff,
4301 (u_longlong_t)bigsize,
4304 for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
4307 bcopy((caddr_t)bigbuf + (off - bigoff),
4308 bigbuf_arcbufs[j]->b_data, chunksize);
4310 bcopy((caddr_t)bigbuf + (off - bigoff),
4311 bigbuf_arcbufs[2 * j]->b_data,
4313 bcopy((caddr_t)bigbuf + (off - bigoff) +
4315 bigbuf_arcbufs[2 * j + 1]->b_data,
4320 VERIFY(dmu_buf_hold(os, bigobj, off,
4321 FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0);
4324 dmu_assign_arcbuf(bonus_db, off,
4325 bigbuf_arcbufs[j], tx);
4327 dmu_assign_arcbuf(bonus_db, off,
4328 bigbuf_arcbufs[2 * j], tx);
4329 dmu_assign_arcbuf(bonus_db,
4330 off + chunksize / 2,
4331 bigbuf_arcbufs[2 * j + 1], tx);
4334 dmu_buf_rele(dbt, FTAG);
4340 * Sanity check the stuff we just wrote.
4343 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
4344 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
4346 VERIFY(0 == dmu_read(os, packobj, packoff,
4347 packsize, packcheck, DMU_READ_PREFETCH));
4348 VERIFY(0 == dmu_read(os, bigobj, bigoff,
4349 bigsize, bigcheck, DMU_READ_PREFETCH));
4351 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
4352 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
4354 umem_free(packcheck, packsize);
4355 umem_free(bigcheck, bigsize);
4358 txg_wait_open(dmu_objset_pool(os), 0);
4359 } else if (i == 3) {
4360 txg_wait_synced(dmu_objset_pool(os), 0);
4364 dmu_buf_rele(bonus_db, FTAG);
4365 umem_free(packbuf, packsize);
4366 umem_free(bigbuf, bigsize);
4367 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4372 ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
4375 uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
4376 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4379 * Have multiple threads write to large offsets in an object
4380 * to verify that parallel writes to an object -- even to the
4381 * same blocks within the object -- doesn't cause any trouble.
4383 ztest_od_init(&od[0], ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER,
4386 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4389 while (ztest_random(10) != 0)
4390 ztest_io(zd, od[0].od_object, offset);
4394 ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
4397 uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
4398 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4399 uint64_t count = ztest_random(20) + 1;
4400 uint64_t blocksize = ztest_random_blocksize();
4403 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize,
4406 if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
4409 if (ztest_truncate(zd, od[0].od_object, offset, count * blocksize) != 0)
4412 ztest_prealloc(zd, od[0].od_object, offset, count * blocksize);
4414 data = umem_zalloc(blocksize, UMEM_NOFAIL);
4416 while (ztest_random(count) != 0) {
4417 uint64_t randoff = offset + (ztest_random(count) * blocksize);
4418 if (ztest_write(zd, od[0].od_object, randoff, blocksize,
4421 while (ztest_random(4) != 0)
4422 ztest_io(zd, od[0].od_object, randoff);
4425 umem_free(data, blocksize);
4429 * Verify that zap_{create,destroy,add,remove,update} work as expected.
4431 #define ZTEST_ZAP_MIN_INTS 1
4432 #define ZTEST_ZAP_MAX_INTS 4
4433 #define ZTEST_ZAP_MAX_PROPS 1000
4436 ztest_zap(ztest_ds_t *zd, uint64_t id)
4438 objset_t *os = zd->zd_os;
4441 uint64_t txg, last_txg;
4442 uint64_t value[ZTEST_ZAP_MAX_INTS];
4443 uint64_t zl_ints, zl_intsize, prop;
4446 char propname[100], txgname[100];
4448 char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
4450 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0, 0);
4452 if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
4455 object = od[0].od_object;
4458 * Generate a known hash collision, and verify that
4459 * we can lookup and remove both entries.
4461 tx = dmu_tx_create(os);
4462 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4463 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4466 for (i = 0; i < 2; i++) {
4468 VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
4471 for (i = 0; i < 2; i++) {
4472 VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
4473 sizeof (uint64_t), 1, &value[i], tx));
4475 zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
4476 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4477 ASSERT3U(zl_ints, ==, 1);
4479 for (i = 0; i < 2; i++) {
4480 VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
4485 * Generate a buch of random entries.
4487 ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
4489 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4490 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4491 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4492 bzero(value, sizeof (value));
4496 * If these zap entries already exist, validate their contents.
4498 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4500 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4501 ASSERT3U(zl_ints, ==, 1);
4503 VERIFY(zap_lookup(os, object, txgname, zl_intsize,
4504 zl_ints, &last_txg) == 0);
4506 VERIFY(zap_length(os, object, propname, &zl_intsize,
4509 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4510 ASSERT3U(zl_ints, ==, ints);
4512 VERIFY(zap_lookup(os, object, propname, zl_intsize,
4513 zl_ints, value) == 0);
4515 for (i = 0; i < ints; i++) {
4516 ASSERT3U(value[i], ==, last_txg + object + i);
4519 ASSERT3U(error, ==, ENOENT);
4523 * Atomically update two entries in our zap object.
4524 * The first is named txg_%llu, and contains the txg
4525 * in which the property was last updated. The second
4526 * is named prop_%llu, and the nth element of its value
4527 * should be txg + object + n.
4529 tx = dmu_tx_create(os);
4530 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4531 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4536 fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
4538 for (i = 0; i < ints; i++)
4539 value[i] = txg + object + i;
4541 VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
4543 VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
4549 * Remove a random pair of entries.
4551 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4552 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4553 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4555 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4557 if (error == ENOENT)
4562 tx = dmu_tx_create(os);
4563 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4564 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4567 VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
4568 VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
4573 * Testcase to test the upgrading of a microzap to fatzap.
4576 ztest_fzap(ztest_ds_t *zd, uint64_t id)
4578 objset_t *os = zd->zd_os;
4580 uint64_t object, txg;
4582 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0, 0);
4584 if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
4587 object = od[0].od_object;
4590 * Add entries to this ZAP and make sure it spills over
4591 * and gets upgraded to a fatzap. Also, since we are adding
4592 * 2050 entries we should see ptrtbl growth and leaf-block split.
4594 for (int i = 0; i < 2050; i++) {
4595 char name[ZFS_MAX_DATASET_NAME_LEN];
4600 (void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
4603 tx = dmu_tx_create(os);
4604 dmu_tx_hold_zap(tx, object, B_TRUE, name);
4605 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4608 error = zap_add(os, object, name, sizeof (uint64_t), 1,
4610 ASSERT(error == 0 || error == EEXIST);
4617 ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
4619 objset_t *os = zd->zd_os;
4621 uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
4623 int i, namelen, error;
4624 int micro = ztest_random(2);
4625 char name[20], string_value[20];
4628 ztest_od_init(&od[0], ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER,
4631 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4634 object = od[0].od_object;
4637 * Generate a random name of the form 'xxx.....' where each
4638 * x is a random printable character and the dots are dots.
4639 * There are 94 such characters, and the name length goes from
4640 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
4642 namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
4644 for (i = 0; i < 3; i++)
4645 name[i] = '!' + ztest_random('~' - '!' + 1);
4646 for (; i < namelen - 1; i++)
4650 if ((namelen & 1) || micro) {
4651 wsize = sizeof (txg);
4657 data = string_value;
4661 VERIFY0(zap_count(os, object, &count));
4662 ASSERT(count != -1ULL);
4665 * Select an operation: length, lookup, add, update, remove.
4667 i = ztest_random(5);
4670 tx = dmu_tx_create(os);
4671 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4672 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4675 bcopy(name, string_value, namelen);
4679 bzero(string_value, namelen);
4685 error = zap_length(os, object, name, &zl_wsize, &zl_wc);
4687 ASSERT3U(wsize, ==, zl_wsize);
4688 ASSERT3U(wc, ==, zl_wc);
4690 ASSERT3U(error, ==, ENOENT);
4695 error = zap_lookup(os, object, name, wsize, wc, data);
4697 if (data == string_value &&
4698 bcmp(name, data, namelen) != 0)
4699 fatal(0, "name '%s' != val '%s' len %d",
4700 name, data, namelen);
4702 ASSERT3U(error, ==, ENOENT);
4707 error = zap_add(os, object, name, wsize, wc, data, tx);
4708 ASSERT(error == 0 || error == EEXIST);
4712 VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
4716 error = zap_remove(os, object, name, tx);
4717 ASSERT(error == 0 || error == ENOENT);
4726 * Commit callback data.
4728 typedef struct ztest_cb_data {
4729 list_node_t zcd_node;
4731 int zcd_expected_err;
4732 boolean_t zcd_added;
4733 boolean_t zcd_called;
4737 /* This is the actual commit callback function */
4739 ztest_commit_callback(void *arg, int error)
4741 ztest_cb_data_t *data = arg;
4742 uint64_t synced_txg;
4744 VERIFY(data != NULL);
4745 VERIFY3S(data->zcd_expected_err, ==, error);
4746 VERIFY(!data->zcd_called);
4748 synced_txg = spa_last_synced_txg(data->zcd_spa);
4749 if (data->zcd_txg > synced_txg)
4750 fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
4751 ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
4754 data->zcd_called = B_TRUE;
4756 if (error == ECANCELED) {
4757 ASSERT0(data->zcd_txg);
4758 ASSERT(!data->zcd_added);
4761 * The private callback data should be destroyed here, but
4762 * since we are going to check the zcd_called field after
4763 * dmu_tx_abort(), we will destroy it there.
4768 /* Was this callback added to the global callback list? */
4769 if (!data->zcd_added)
4772 ASSERT3U(data->zcd_txg, !=, 0);
4774 /* Remove our callback from the list */
4775 mutex_enter(&zcl.zcl_callbacks_lock);
4776 list_remove(&zcl.zcl_callbacks, data);
4777 mutex_exit(&zcl.zcl_callbacks_lock);
4780 umem_free(data, sizeof (ztest_cb_data_t));
4783 /* Allocate and initialize callback data structure */
4784 static ztest_cb_data_t *
4785 ztest_create_cb_data(objset_t *os, uint64_t txg)
4787 ztest_cb_data_t *cb_data;
4789 cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
4791 cb_data->zcd_txg = txg;
4792 cb_data->zcd_spa = dmu_objset_spa(os);
4798 * If a number of txgs equal to this threshold have been created after a commit
4799 * callback has been registered but not called, then we assume there is an
4800 * implementation bug.
4802 #define ZTEST_COMMIT_CALLBACK_THRESH (TXG_CONCURRENT_STATES + 2)
4805 * Commit callback test.
4808 ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
4810 objset_t *os = zd->zd_os;
4813 ztest_cb_data_t *cb_data[3], *tmp_cb;
4814 uint64_t old_txg, txg;
4817 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
4819 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4822 tx = dmu_tx_create(os);
4824 cb_data[0] = ztest_create_cb_data(os, 0);
4825 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
4827 dmu_tx_hold_write(tx, od[0].od_object, 0, sizeof (uint64_t));
4829 /* Every once in a while, abort the transaction on purpose */
4830 if (ztest_random(100) == 0)
4834 error = dmu_tx_assign(tx, TXG_NOWAIT);
4836 txg = error ? 0 : dmu_tx_get_txg(tx);
4838 cb_data[0]->zcd_txg = txg;
4839 cb_data[1] = ztest_create_cb_data(os, txg);
4840 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
4844 * It's not a strict requirement to call the registered
4845 * callbacks from inside dmu_tx_abort(), but that's what
4846 * it's supposed to happen in the current implementation
4847 * so we will check for that.
4849 for (i = 0; i < 2; i++) {
4850 cb_data[i]->zcd_expected_err = ECANCELED;
4851 VERIFY(!cb_data[i]->zcd_called);
4856 for (i = 0; i < 2; i++) {
4857 VERIFY(cb_data[i]->zcd_called);
4858 umem_free(cb_data[i], sizeof (ztest_cb_data_t));
4864 cb_data[2] = ztest_create_cb_data(os, txg);
4865 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
4868 * Read existing data to make sure there isn't a future leak.
4870 VERIFY(0 == dmu_read(os, od[0].od_object, 0, sizeof (uint64_t),
4871 &old_txg, DMU_READ_PREFETCH));
4874 fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
4877 dmu_write(os, od[0].od_object, 0, sizeof (uint64_t), &txg, tx);
4879 mutex_enter(&zcl.zcl_callbacks_lock);
4882 * Since commit callbacks don't have any ordering requirement and since
4883 * it is theoretically possible for a commit callback to be called
4884 * after an arbitrary amount of time has elapsed since its txg has been
4885 * synced, it is difficult to reliably determine whether a commit
4886 * callback hasn't been called due to high load or due to a flawed
4889 * In practice, we will assume that if after a certain number of txgs a
4890 * commit callback hasn't been called, then most likely there's an
4891 * implementation bug..
4893 tmp_cb = list_head(&zcl.zcl_callbacks);
4894 if (tmp_cb != NULL &&
4895 (txg - ZTEST_COMMIT_CALLBACK_THRESH) > tmp_cb->zcd_txg) {
4896 fatal(0, "Commit callback threshold exceeded, oldest txg: %"
4897 PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
4901 * Let's find the place to insert our callbacks.
4903 * Even though the list is ordered by txg, it is possible for the
4904 * insertion point to not be the end because our txg may already be
4905 * quiescing at this point and other callbacks in the open txg
4906 * (from other objsets) may have sneaked in.
4908 tmp_cb = list_tail(&zcl.zcl_callbacks);
4909 while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
4910 tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
4912 /* Add the 3 callbacks to the list */
4913 for (i = 0; i < 3; i++) {
4915 list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
4917 list_insert_after(&zcl.zcl_callbacks, tmp_cb,
4920 cb_data[i]->zcd_added = B_TRUE;
4921 VERIFY(!cb_data[i]->zcd_called);
4923 tmp_cb = cb_data[i];
4926 mutex_exit(&zcl.zcl_callbacks_lock);
4932 * Visit each object in the dataset. Verify that its properties
4933 * are consistent what was stored in the block tag when it was created,
4934 * and that its unused bonus buffer space has not been overwritten.
4937 ztest_verify_dnode_bt(ztest_ds_t *zd, uint64_t id)
4939 objset_t *os = zd->zd_os;
4943 for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
4944 ztest_block_tag_t *bt = NULL;
4945 dmu_object_info_t doi;
4948 if (dmu_bonus_hold(os, obj, FTAG, &db) != 0)
4951 dmu_object_info_from_db(db, &doi);
4952 if (doi.doi_bonus_size >= sizeof (*bt))
4953 bt = ztest_bt_bonus(db);
4955 if (bt && bt->bt_magic == BT_MAGIC) {
4956 ztest_bt_verify(bt, os, obj, doi.doi_dnodesize,
4957 bt->bt_offset, bt->bt_gen, bt->bt_txg,
4959 ztest_verify_unused_bonus(db, bt, obj, os, bt->bt_gen);
4962 dmu_buf_rele(db, FTAG);
4968 ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
4970 zfs_prop_t proplist[] = {
4972 ZFS_PROP_COMPRESSION,
4977 rw_enter(&ztest_name_lock, RW_READER);
4979 for (int p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
4980 (void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
4981 ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
4983 rw_exit(&ztest_name_lock);
4988 ztest_remap_blocks(ztest_ds_t *zd, uint64_t id)
4990 rw_enter(&ztest_name_lock, RW_READER);
4992 int error = dmu_objset_remap_indirects(zd->zd_name);
4993 if (error == ENOSPC)
4997 rw_exit(&ztest_name_lock);
5002 ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
5004 nvlist_t *props = NULL;
5006 rw_enter(&ztest_name_lock, RW_READER);
5008 (void) ztest_spa_prop_set_uint64(ZPOOL_PROP_DEDUPDITTO,
5009 ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
5011 VERIFY0(spa_prop_get(ztest_spa, &props));
5013 if (ztest_opts.zo_verbose >= 6)
5014 dump_nvlist(props, 4);
5018 rw_exit(&ztest_name_lock);
5022 user_release_one(const char *snapname, const char *holdname)
5024 nvlist_t *snaps, *holds;
5027 snaps = fnvlist_alloc();
5028 holds = fnvlist_alloc();
5029 fnvlist_add_boolean(holds, holdname);
5030 fnvlist_add_nvlist(snaps, snapname, holds);
5031 fnvlist_free(holds);
5032 error = dsl_dataset_user_release(snaps, NULL);
5033 fnvlist_free(snaps);
5038 * Test snapshot hold/release and deferred destroy.
5041 ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
5044 objset_t *os = zd->zd_os;
5048 char clonename[100];
5050 char osname[ZFS_MAX_DATASET_NAME_LEN];
5053 rw_enter(&ztest_name_lock, RW_READER);
5055 dmu_objset_name(os, osname);
5057 (void) snprintf(snapname, sizeof (snapname), "sh1_%llu", id);
5058 (void) snprintf(fullname, sizeof (fullname), "%s@%s", osname, snapname);
5059 (void) snprintf(clonename, sizeof (clonename),
5060 "%s/ch1_%llu", osname, id);
5061 (void) snprintf(tag, sizeof (tag), "tag_%llu", id);
5064 * Clean up from any previous run.
5066 error = dsl_destroy_head(clonename);
5067 if (error != ENOENT)
5069 error = user_release_one(fullname, tag);
5070 if (error != ESRCH && error != ENOENT)
5072 error = dsl_destroy_snapshot(fullname, B_FALSE);
5073 if (error != ENOENT)
5077 * Create snapshot, clone it, mark snap for deferred destroy,
5078 * destroy clone, verify snap was also destroyed.
5080 error = dmu_objset_snapshot_one(osname, snapname);
5082 if (error == ENOSPC) {
5083 ztest_record_enospc("dmu_objset_snapshot");
5086 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
5089 error = dmu_objset_clone(clonename, fullname);
5091 if (error == ENOSPC) {
5092 ztest_record_enospc("dmu_objset_clone");
5095 fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
5098 error = dsl_destroy_snapshot(fullname, B_TRUE);
5100 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5104 error = dsl_destroy_head(clonename);
5106 fatal(0, "dsl_destroy_head(%s) = %d", clonename, error);
5108 error = dmu_objset_hold(fullname, FTAG, &origin);
5109 if (error != ENOENT)
5110 fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
5113 * Create snapshot, add temporary hold, verify that we can't
5114 * destroy a held snapshot, mark for deferred destroy,
5115 * release hold, verify snapshot was destroyed.
5117 error = dmu_objset_snapshot_one(osname, snapname);
5119 if (error == ENOSPC) {
5120 ztest_record_enospc("dmu_objset_snapshot");
5123 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
5126 holds = fnvlist_alloc();
5127 fnvlist_add_string(holds, fullname, tag);
5128 error = dsl_dataset_user_hold(holds, 0, NULL);
5129 fnvlist_free(holds);
5131 if (error == ENOSPC) {
5132 ztest_record_enospc("dsl_dataset_user_hold");
5135 fatal(0, "dsl_dataset_user_hold(%s, %s) = %u",
5136 fullname, tag, error);
5139 error = dsl_destroy_snapshot(fullname, B_FALSE);
5140 if (error != EBUSY) {
5141 fatal(0, "dsl_destroy_snapshot(%s, B_FALSE) = %d",
5145 error = dsl_destroy_snapshot(fullname, B_TRUE);
5147 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5151 error = user_release_one(fullname, tag);
5153 fatal(0, "user_release_one(%s, %s) = %d", fullname, tag, error);
5155 VERIFY3U(dmu_objset_hold(fullname, FTAG, &origin), ==, ENOENT);
5158 rw_exit(&ztest_name_lock);
5162 * Inject random faults into the on-disk data.
5166 ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
5168 ztest_shared_t *zs = ztest_shared;
5169 spa_t *spa = ztest_spa;
5173 uint64_t bad = 0x1990c0ffeedecadeULL;
5175 char path0[MAXPATHLEN];
5176 char pathrand[MAXPATHLEN];
5178 int bshift = SPA_MAXBLOCKSHIFT + 2;
5184 boolean_t islog = B_FALSE;
5186 mutex_enter(&ztest_vdev_lock);
5189 * Device removal is in progress, fault injection must be disabled
5190 * until it completes and the pool is scrubbed. The fault injection
5191 * strategy for damaging blocks does not take in to account evacuated
5192 * blocks which may have already been damaged.
5194 if (ztest_device_removal_active) {
5195 mutex_exit(&ztest_vdev_lock);
5199 maxfaults = MAXFAULTS();
5200 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
5201 mirror_save = zs->zs_mirrors;
5202 mutex_exit(&ztest_vdev_lock);
5204 ASSERT(leaves >= 1);
5207 * Grab the name lock as reader. There are some operations
5208 * which don't like to have their vdevs changed while
5209 * they are in progress (i.e. spa_change_guid). Those
5210 * operations will have grabbed the name lock as writer.
5212 rw_enter(&ztest_name_lock, RW_READER);
5215 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
5217 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5219 if (ztest_random(2) == 0) {
5221 * Inject errors on a normal data device or slog device.
5223 top = ztest_random_vdev_top(spa, B_TRUE);
5224 leaf = ztest_random(leaves) + zs->zs_splits;
5227 * Generate paths to the first leaf in this top-level vdev,
5228 * and to the random leaf we selected. We'll induce transient
5229 * write failures and random online/offline activity on leaf 0,
5230 * and we'll write random garbage to the randomly chosen leaf.
5232 (void) snprintf(path0, sizeof (path0), ztest_dev_template,
5233 ztest_opts.zo_dir, ztest_opts.zo_pool,
5234 top * leaves + zs->zs_splits);
5235 (void) snprintf(pathrand, sizeof (pathrand), ztest_dev_template,
5236 ztest_opts.zo_dir, ztest_opts.zo_pool,
5237 top * leaves + leaf);
5239 vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
5240 if (vd0 != NULL && vd0->vdev_top->vdev_islog)
5244 * If the top-level vdev needs to be resilvered
5245 * then we only allow faults on the device that is
5248 if (vd0 != NULL && maxfaults != 1 &&
5249 (!vdev_resilver_needed(vd0->vdev_top, NULL, NULL) ||
5250 vd0->vdev_resilver_txg != 0)) {
5252 * Make vd0 explicitly claim to be unreadable,
5253 * or unwriteable, or reach behind its back
5254 * and close the underlying fd. We can do this if
5255 * maxfaults == 0 because we'll fail and reexecute,
5256 * and we can do it if maxfaults >= 2 because we'll
5257 * have enough redundancy. If maxfaults == 1, the
5258 * combination of this with injection of random data
5259 * corruption below exceeds the pool's fault tolerance.
5261 vdev_file_t *vf = vd0->vdev_tsd;
5263 zfs_dbgmsg("injecting fault to vdev %llu; maxfaults=%d",
5264 (long long)vd0->vdev_id, (int)maxfaults);
5266 if (vf != NULL && ztest_random(3) == 0) {
5267 (void) close(vf->vf_vnode->v_fd);
5268 vf->vf_vnode->v_fd = -1;
5269 } else if (ztest_random(2) == 0) {
5270 vd0->vdev_cant_read = B_TRUE;
5272 vd0->vdev_cant_write = B_TRUE;
5274 guid0 = vd0->vdev_guid;
5278 * Inject errors on an l2cache device.
5280 spa_aux_vdev_t *sav = &spa->spa_l2cache;
5282 if (sav->sav_count == 0) {
5283 spa_config_exit(spa, SCL_STATE, FTAG);
5284 rw_exit(&ztest_name_lock);
5287 vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
5288 guid0 = vd0->vdev_guid;
5289 (void) strcpy(path0, vd0->vdev_path);
5290 (void) strcpy(pathrand, vd0->vdev_path);
5294 maxfaults = INT_MAX; /* no limit on cache devices */
5297 spa_config_exit(spa, SCL_STATE, FTAG);
5298 rw_exit(&ztest_name_lock);
5301 * If we can tolerate two or more faults, or we're dealing
5302 * with a slog, randomly online/offline vd0.
5304 if ((maxfaults >= 2 || islog) && guid0 != 0) {
5305 if (ztest_random(10) < 6) {
5306 int flags = (ztest_random(2) == 0 ?
5307 ZFS_OFFLINE_TEMPORARY : 0);
5310 * We have to grab the zs_name_lock as writer to
5311 * prevent a race between offlining a slog and
5312 * destroying a dataset. Offlining the slog will
5313 * grab a reference on the dataset which may cause
5314 * dmu_objset_destroy() to fail with EBUSY thus
5315 * leaving the dataset in an inconsistent state.
5318 rw_enter(&ztest_name_lock, RW_WRITER);
5320 VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
5323 rw_exit(&ztest_name_lock);
5326 * Ideally we would like to be able to randomly
5327 * call vdev_[on|off]line without holding locks
5328 * to force unpredictable failures but the side
5329 * effects of vdev_[on|off]line prevent us from
5330 * doing so. We grab the ztest_vdev_lock here to
5331 * prevent a race between injection testing and
5334 mutex_enter(&ztest_vdev_lock);
5335 (void) vdev_online(spa, guid0, 0, NULL);
5336 mutex_exit(&ztest_vdev_lock);
5344 * We have at least single-fault tolerance, so inject data corruption.
5346 fd = open(pathrand, O_RDWR);
5348 if (fd == -1) /* we hit a gap in the device namespace */
5351 fsize = lseek(fd, 0, SEEK_END);
5353 while (--iters != 0) {
5355 * The offset must be chosen carefully to ensure that
5356 * we do not inject a given logical block with errors
5357 * on two different leaf devices, because ZFS can not
5358 * tolerate that (if maxfaults==1).
5360 * We divide each leaf into chunks of size
5361 * (# leaves * SPA_MAXBLOCKSIZE * 4). Within each chunk
5362 * there is a series of ranges to which we can inject errors.
5363 * Each range can accept errors on only a single leaf vdev.
5364 * The error injection ranges are separated by ranges
5365 * which we will not inject errors on any device (DMZs).
5366 * Each DMZ must be large enough such that a single block
5367 * can not straddle it, so that a single block can not be
5368 * a target in two different injection ranges (on different
5371 * For example, with 3 leaves, each chunk looks like:
5372 * 0 to 32M: injection range for leaf 0
5373 * 32M to 64M: DMZ - no injection allowed
5374 * 64M to 96M: injection range for leaf 1
5375 * 96M to 128M: DMZ - no injection allowed
5376 * 128M to 160M: injection range for leaf 2
5377 * 160M to 192M: DMZ - no injection allowed
5379 offset = ztest_random(fsize / (leaves << bshift)) *
5380 (leaves << bshift) + (leaf << bshift) +
5381 (ztest_random(1ULL << (bshift - 1)) & -8ULL);
5384 * Only allow damage to the labels at one end of the vdev.
5386 * If all labels are damaged, the device will be totally
5387 * inaccessible, which will result in loss of data,
5388 * because we also damage (parts of) the other side of
5391 * Additionally, we will always have both an even and an
5392 * odd label, so that we can handle crashes in the
5393 * middle of vdev_config_sync().
5395 if ((leaf & 1) == 0 && offset < VDEV_LABEL_START_SIZE)
5399 * The two end labels are stored at the "end" of the disk, but
5400 * the end of the disk (vdev_psize) is aligned to
5401 * sizeof (vdev_label_t).
5403 uint64_t psize = P2ALIGN(fsize, sizeof (vdev_label_t));
5404 if ((leaf & 1) == 1 &&
5405 offset + sizeof (bad) > psize - VDEV_LABEL_END_SIZE)
5408 mutex_enter(&ztest_vdev_lock);
5409 if (mirror_save != zs->zs_mirrors) {
5410 mutex_exit(&ztest_vdev_lock);
5415 if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
5416 fatal(1, "can't inject bad word at 0x%llx in %s",
5419 mutex_exit(&ztest_vdev_lock);
5421 if (ztest_opts.zo_verbose >= 7)
5422 (void) printf("injected bad word into %s,"
5423 " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
5430 * Verify that DDT repair works as expected.
5433 ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
5435 ztest_shared_t *zs = ztest_shared;
5436 spa_t *spa = ztest_spa;
5437 objset_t *os = zd->zd_os;
5439 uint64_t object, blocksize, txg, pattern, psize;
5440 enum zio_checksum checksum = spa_dedup_checksum(spa);
5445 int copies = 2 * ZIO_DEDUPDITTO_MIN;
5447 blocksize = ztest_random_blocksize();
5448 blocksize = MIN(blocksize, 2048); /* because we write so many */
5450 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize,
5453 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
5457 * Take the name lock as writer to prevent anyone else from changing
5458 * the pool and dataset properies we need to maintain during this test.
5460 rw_enter(&ztest_name_lock, RW_WRITER);
5462 if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
5464 ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
5466 rw_exit(&ztest_name_lock);
5470 dmu_objset_stats_t dds;
5471 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
5472 dmu_objset_fast_stat(os, &dds);
5473 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
5475 object = od[0].od_object;
5476 blocksize = od[0].od_blocksize;
5477 pattern = zs->zs_guid ^ dds.dds_guid;
5479 ASSERT(object != 0);
5481 tx = dmu_tx_create(os);
5482 dmu_tx_hold_write(tx, object, 0, copies * blocksize);
5483 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
5485 rw_exit(&ztest_name_lock);
5490 * Write all the copies of our block.
5492 for (int i = 0; i < copies; i++) {
5493 uint64_t offset = i * blocksize;
5494 int error = dmu_buf_hold(os, object, offset, FTAG, &db,
5495 DMU_READ_NO_PREFETCH);
5497 fatal(B_FALSE, "dmu_buf_hold(%p, %llu, %llu) = %u",
5498 os, (long long)object, (long long) offset, error);
5500 ASSERT(db->db_offset == offset);
5501 ASSERT(db->db_size == blocksize);
5502 ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
5503 ztest_pattern_match(db->db_data, db->db_size, 0ULL));
5504 dmu_buf_will_fill(db, tx);
5505 ztest_pattern_set(db->db_data, db->db_size, pattern);
5506 dmu_buf_rele(db, FTAG);
5510 txg_wait_synced(spa_get_dsl(spa), txg);
5513 * Find out what block we got.
5515 VERIFY0(dmu_buf_hold(os, object, 0, FTAG, &db,
5516 DMU_READ_NO_PREFETCH));
5517 blk = *((dmu_buf_impl_t *)db)->db_blkptr;
5518 dmu_buf_rele(db, FTAG);
5521 * Damage the block. Dedup-ditto will save us when we read it later.
5523 psize = BP_GET_PSIZE(&blk);
5524 abd = abd_alloc_linear(psize, B_TRUE);
5525 ztest_pattern_set(abd_to_buf(abd), psize, ~pattern);
5527 (void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
5528 abd, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
5529 ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
5533 rw_exit(&ztest_name_lock);
5541 ztest_scrub(ztest_ds_t *zd, uint64_t id)
5543 spa_t *spa = ztest_spa;
5546 * Scrub in progress by device removal.
5548 if (ztest_device_removal_active)
5551 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5552 (void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
5553 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5557 * Change the guid for the pool.
5561 ztest_reguid(ztest_ds_t *zd, uint64_t id)
5563 spa_t *spa = ztest_spa;
5564 uint64_t orig, load;
5567 orig = spa_guid(spa);
5568 load = spa_load_guid(spa);
5570 rw_enter(&ztest_name_lock, RW_WRITER);
5571 error = spa_change_guid(spa);
5572 rw_exit(&ztest_name_lock);
5577 if (ztest_opts.zo_verbose >= 4) {
5578 (void) printf("Changed guid old %llu -> %llu\n",
5579 (u_longlong_t)orig, (u_longlong_t)spa_guid(spa));
5582 VERIFY3U(orig, !=, spa_guid(spa));
5583 VERIFY3U(load, ==, spa_load_guid(spa));
5587 ztest_random_concrete_vdev_leaf(vdev_t *vd)
5592 if (vd->vdev_children == 0)
5595 vdev_t *eligible[vd->vdev_children];
5596 int eligible_idx = 0, i;
5597 for (i = 0; i < vd->vdev_children; i++) {
5598 vdev_t *cvd = vd->vdev_child[i];
5599 if (cvd->vdev_top->vdev_removing)
5601 if (cvd->vdev_children > 0 ||
5602 (vdev_is_concrete(cvd) && !cvd->vdev_detached)) {
5603 eligible[eligible_idx++] = cvd;
5606 VERIFY(eligible_idx > 0);
5608 uint64_t child_no = ztest_random(eligible_idx);
5609 return (ztest_random_concrete_vdev_leaf(eligible[child_no]));
5614 ztest_initialize(ztest_ds_t *zd, uint64_t id)
5616 spa_t *spa = ztest_spa;
5619 mutex_enter(&ztest_vdev_lock);
5621 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
5623 /* Random leaf vdev */
5624 vdev_t *rand_vd = ztest_random_concrete_vdev_leaf(spa->spa_root_vdev);
5625 if (rand_vd == NULL) {
5626 spa_config_exit(spa, SCL_VDEV, FTAG);
5627 mutex_exit(&ztest_vdev_lock);
5632 * The random vdev we've selected may change as soon as we
5633 * drop the spa_config_lock. We create local copies of things
5634 * we're interested in.
5636 uint64_t guid = rand_vd->vdev_guid;
5637 char *path = strdup(rand_vd->vdev_path);
5638 boolean_t active = rand_vd->vdev_initialize_thread != NULL;
5640 zfs_dbgmsg("vd %p, guid %llu", rand_vd, guid);
5641 spa_config_exit(spa, SCL_VDEV, FTAG);
5643 uint64_t cmd = ztest_random(POOL_INITIALIZE_FUNCS);
5644 error = spa_vdev_initialize(spa, guid, cmd);
5646 case POOL_INITIALIZE_CANCEL:
5647 if (ztest_opts.zo_verbose >= 4) {
5648 (void) printf("Cancel initialize %s", path);
5650 (void) printf(" failed (no initialize active)");
5651 (void) printf("\n");
5654 case POOL_INITIALIZE_DO:
5655 if (ztest_opts.zo_verbose >= 4) {
5656 (void) printf("Start initialize %s", path);
5657 if (active && error == 0)
5658 (void) printf(" failed (already active)");
5659 else if (error != 0)
5660 (void) printf(" failed (error %d)", error);
5661 (void) printf("\n");
5664 case POOL_INITIALIZE_SUSPEND:
5665 if (ztest_opts.zo_verbose >= 4) {
5666 (void) printf("Suspend initialize %s", path);
5668 (void) printf(" failed (no initialize active)");
5669 (void) printf("\n");
5674 mutex_exit(&ztest_vdev_lock);
5678 * Verify pool integrity by running zdb.
5681 ztest_run_zdb(char *pool)
5684 char zdb[MAXPATHLEN + MAXNAMELEN + 20];
5692 strlcpy(zdb, "/usr/bin/ztest", sizeof(zdb));
5694 /* zdb lives in /usr/sbin, while ztest lives in /usr/bin */
5695 bin = strstr(zdb, "/usr/bin/");
5696 ztest = strstr(bin, "/ztest");
5698 isalen = ztest - isa;
5702 "/usr/sbin%.*s/zdb -bcc%s%s -G -d -U %s "
5703 "-o zfs_reconstruct_indirect_combinations_max=65536 %s",
5706 ztest_opts.zo_verbose >= 3 ? "s" : "",
5707 ztest_opts.zo_verbose >= 4 ? "v" : "",
5712 if (ztest_opts.zo_verbose >= 5)
5713 (void) printf("Executing %s\n", strstr(zdb, "zdb "));
5715 fp = popen(zdb, "r");
5718 while (fgets(zbuf, sizeof (zbuf), fp) != NULL)
5719 if (ztest_opts.zo_verbose >= 3)
5720 (void) printf("%s", zbuf);
5722 status = pclose(fp);
5727 ztest_dump_core = 0;
5728 if (WIFEXITED(status))
5729 fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
5731 fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
5735 ztest_walk_pool_directory(char *header)
5739 if (ztest_opts.zo_verbose >= 6)
5740 (void) printf("%s\n", header);
5742 mutex_enter(&spa_namespace_lock);
5743 while ((spa = spa_next(spa)) != NULL)
5744 if (ztest_opts.zo_verbose >= 6)
5745 (void) printf("\t%s\n", spa_name(spa));
5746 mutex_exit(&spa_namespace_lock);
5750 ztest_spa_import_export(char *oldname, char *newname)
5752 nvlist_t *config, *newconfig;
5757 if (ztest_opts.zo_verbose >= 4) {
5758 (void) printf("import/export: old = %s, new = %s\n",
5763 * Clean up from previous runs.
5765 (void) spa_destroy(newname);
5768 * Get the pool's configuration and guid.
5770 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
5773 * Kick off a scrub to tickle scrub/export races.
5775 if (ztest_random(2) == 0)
5776 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5778 pool_guid = spa_guid(spa);
5779 spa_close(spa, FTAG);
5781 ztest_walk_pool_directory("pools before export");
5786 VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
5788 ztest_walk_pool_directory("pools after export");
5793 newconfig = spa_tryimport(config);
5794 ASSERT(newconfig != NULL);
5795 nvlist_free(newconfig);
5798 * Import it under the new name.
5800 error = spa_import(newname, config, NULL, 0);
5802 dump_nvlist(config, 0);
5803 fatal(B_FALSE, "couldn't import pool %s as %s: error %u",
5804 oldname, newname, error);
5807 ztest_walk_pool_directory("pools after import");
5810 * Try to import it again -- should fail with EEXIST.
5812 VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL, 0));
5815 * Try to import it under a different name -- should fail with EEXIST.
5817 VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL, 0));
5820 * Verify that the pool is no longer visible under the old name.
5822 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5825 * Verify that we can open and close the pool using the new name.
5827 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
5828 ASSERT(pool_guid == spa_guid(spa));
5829 spa_close(spa, FTAG);
5831 nvlist_free(config);
5835 ztest_resume(spa_t *spa)
5837 if (spa_suspended(spa) && ztest_opts.zo_verbose >= 6)
5838 (void) printf("resuming from suspended state\n");
5839 spa_vdev_state_enter(spa, SCL_NONE);
5840 vdev_clear(spa, NULL);
5841 (void) spa_vdev_state_exit(spa, NULL, 0);
5842 (void) zio_resume(spa);
5846 ztest_resume_thread(void *arg)
5850 while (!ztest_exiting) {
5851 if (spa_suspended(spa))
5853 (void) poll(NULL, 0, 100);
5856 * Periodically change the zfs_compressed_arc_enabled setting.
5858 if (ztest_random(10) == 0)
5859 zfs_compressed_arc_enabled = ztest_random(2);
5862 * Periodically change the zfs_abd_scatter_enabled setting.
5864 if (ztest_random(10) == 0)
5865 zfs_abd_scatter_enabled = ztest_random(2);
5871 ztest_deadman_thread(void *arg)
5873 ztest_shared_t *zs = arg;
5874 spa_t *spa = ztest_spa;
5875 hrtime_t delta, total = 0;
5878 delta = zs->zs_thread_stop - zs->zs_thread_start +
5879 MSEC2NSEC(zfs_deadman_synctime_ms);
5881 (void) poll(NULL, 0, (int)NSEC2MSEC(delta));
5884 * If the pool is suspended then fail immediately. Otherwise,
5885 * check to see if the pool is making any progress. If
5886 * vdev_deadman() discovers that there hasn't been any recent
5887 * I/Os then it will end up aborting the tests.
5889 if (spa_suspended(spa) || spa->spa_root_vdev == NULL) {
5890 fatal(0, "aborting test after %llu seconds because "
5891 "pool has transitioned to a suspended state.",
5892 zfs_deadman_synctime_ms / 1000);
5895 vdev_deadman(spa->spa_root_vdev);
5897 total += zfs_deadman_synctime_ms/1000;
5898 (void) printf("ztest has been running for %lld seconds\n",
5904 ztest_execute(int test, ztest_info_t *zi, uint64_t id)
5906 ztest_ds_t *zd = &ztest_ds[id % ztest_opts.zo_datasets];
5907 ztest_shared_callstate_t *zc = ZTEST_GET_SHARED_CALLSTATE(test);
5908 hrtime_t functime = gethrtime();
5910 for (int i = 0; i < zi->zi_iters; i++)
5911 zi->zi_func(zd, id);
5913 functime = gethrtime() - functime;
5915 atomic_add_64(&zc->zc_count, 1);
5916 atomic_add_64(&zc->zc_time, functime);
5918 if (ztest_opts.zo_verbose >= 4) {
5920 (void) dladdr((void *)zi->zi_func, &dli);
5921 (void) printf("%6.2f sec in %s\n",
5922 (double)functime / NANOSEC, dli.dli_sname);
5927 ztest_thread(void *arg)
5930 uint64_t id = (uintptr_t)arg;
5931 ztest_shared_t *zs = ztest_shared;
5935 ztest_shared_callstate_t *zc;
5937 while ((now = gethrtime()) < zs->zs_thread_stop) {
5939 * See if it's time to force a crash.
5941 if (now > zs->zs_thread_kill)
5945 * If we're getting ENOSPC with some regularity, stop.
5947 if (zs->zs_enospc_count > 10)
5951 * Pick a random function to execute.
5953 rand = ztest_random(ZTEST_FUNCS);
5954 zi = &ztest_info[rand];
5955 zc = ZTEST_GET_SHARED_CALLSTATE(rand);
5956 call_next = zc->zc_next;
5958 if (now >= call_next &&
5959 atomic_cas_64(&zc->zc_next, call_next, call_next +
5960 ztest_random(2 * zi->zi_interval[0] + 1)) == call_next) {
5961 ztest_execute(rand, zi, id);
5969 ztest_dataset_name(char *dsname, char *pool, int d)
5971 (void) snprintf(dsname, ZFS_MAX_DATASET_NAME_LEN, "%s/ds_%d", pool, d);
5975 ztest_dataset_destroy(int d)
5977 char name[ZFS_MAX_DATASET_NAME_LEN];
5979 ztest_dataset_name(name, ztest_opts.zo_pool, d);
5981 if (ztest_opts.zo_verbose >= 3)
5982 (void) printf("Destroying %s to free up space\n", name);
5985 * Cleanup any non-standard clones and snapshots. In general,
5986 * ztest thread t operates on dataset (t % zopt_datasets),
5987 * so there may be more than one thing to clean up.
5989 for (int t = d; t < ztest_opts.zo_threads;
5990 t += ztest_opts.zo_datasets) {
5991 ztest_dsl_dataset_cleanup(name, t);
5994 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
5995 DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
5999 ztest_dataset_dirobj_verify(ztest_ds_t *zd)
6001 uint64_t usedobjs, dirobjs, scratch;
6004 * ZTEST_DIROBJ is the object directory for the entire dataset.
6005 * Therefore, the number of objects in use should equal the
6006 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
6007 * If not, we have an object leak.
6009 * Note that we can only check this in ztest_dataset_open(),
6010 * when the open-context and syncing-context values agree.
6011 * That's because zap_count() returns the open-context value,
6012 * while dmu_objset_space() returns the rootbp fill count.
6014 VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
6015 dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
6016 ASSERT3U(dirobjs + 1, ==, usedobjs);
6020 ztest_dataset_open(int d)
6022 ztest_ds_t *zd = &ztest_ds[d];
6023 uint64_t committed_seq = ZTEST_GET_SHARED_DS(d)->zd_seq;
6026 char name[ZFS_MAX_DATASET_NAME_LEN];
6029 ztest_dataset_name(name, ztest_opts.zo_pool, d);
6031 rw_enter(&ztest_name_lock, RW_READER);
6033 error = ztest_dataset_create(name);
6034 if (error == ENOSPC) {
6035 rw_exit(&ztest_name_lock);
6036 ztest_record_enospc(FTAG);
6039 ASSERT(error == 0 || error == EEXIST);
6041 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, zd, &os));
6042 rw_exit(&ztest_name_lock);
6044 ztest_zd_init(zd, ZTEST_GET_SHARED_DS(d), os);
6046 zilog = zd->zd_zilog;
6048 if (zilog->zl_header->zh_claim_lr_seq != 0 &&
6049 zilog->zl_header->zh_claim_lr_seq < committed_seq)
6050 fatal(0, "missing log records: claimed %llu < committed %llu",
6051 zilog->zl_header->zh_claim_lr_seq, committed_seq);
6053 ztest_dataset_dirobj_verify(zd);
6055 zil_replay(os, zd, ztest_replay_vector);
6057 ztest_dataset_dirobj_verify(zd);
6059 if (ztest_opts.zo_verbose >= 6)
6060 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
6062 (u_longlong_t)zilog->zl_parse_blk_count,
6063 (u_longlong_t)zilog->zl_parse_lr_count,
6064 (u_longlong_t)zilog->zl_replaying_seq);
6066 zilog = zil_open(os, ztest_get_data);
6068 if (zilog->zl_replaying_seq != 0 &&
6069 zilog->zl_replaying_seq < committed_seq)
6070 fatal(0, "missing log records: replayed %llu < committed %llu",
6071 zilog->zl_replaying_seq, committed_seq);
6077 ztest_dataset_close(int d)
6079 ztest_ds_t *zd = &ztest_ds[d];
6081 zil_close(zd->zd_zilog);
6082 dmu_objset_disown(zd->zd_os, zd);
6088 * Kick off threads to run tests on all datasets in parallel.
6091 ztest_run(ztest_shared_t *zs)
6096 thread_t resume_tid;
6099 ztest_exiting = B_FALSE;
6102 * Initialize parent/child shared state.
6104 mutex_init(&ztest_checkpoint_lock, NULL, USYNC_THREAD, NULL);
6105 mutex_init(&ztest_vdev_lock, NULL, USYNC_THREAD, NULL);
6106 rw_init(&ztest_name_lock, NULL, USYNC_THREAD, NULL);
6108 zs->zs_thread_start = gethrtime();
6109 zs->zs_thread_stop =
6110 zs->zs_thread_start + ztest_opts.zo_passtime * NANOSEC;
6111 zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
6112 zs->zs_thread_kill = zs->zs_thread_stop;
6113 if (ztest_random(100) < ztest_opts.zo_killrate) {
6114 zs->zs_thread_kill -=
6115 ztest_random(ztest_opts.zo_passtime * NANOSEC);
6118 mutex_init(&zcl.zcl_callbacks_lock, NULL, USYNC_THREAD, NULL);
6120 list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
6121 offsetof(ztest_cb_data_t, zcd_node));
6126 kernel_init(FREAD | FWRITE);
6127 VERIFY0(spa_open(ztest_opts.zo_pool, &spa, FTAG));
6128 metaslab_preload_limit = ztest_random(20) + 1;
6131 dmu_objset_stats_t dds;
6132 VERIFY0(dmu_objset_own(ztest_opts.zo_pool,
6133 DMU_OST_ANY, B_TRUE, FTAG, &os));
6134 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
6135 dmu_objset_fast_stat(os, &dds);
6136 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
6137 zs->zs_guid = dds.dds_guid;
6138 dmu_objset_disown(os, FTAG);
6140 spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
6143 * We don't expect the pool to suspend unless maxfaults == 0,
6144 * in which case ztest_fault_inject() temporarily takes away
6145 * the only valid replica.
6147 if (MAXFAULTS() == 0)
6148 spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
6150 spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
6153 * Create a thread to periodically resume suspended I/O.
6155 VERIFY(thr_create(0, 0, ztest_resume_thread, spa, THR_BOUND,
6159 * Create a deadman thread to abort() if we hang.
6161 VERIFY(thr_create(0, 0, ztest_deadman_thread, zs, THR_BOUND,
6165 * Verify that we can safely inquire about any object,
6166 * whether it's allocated or not. To make it interesting,
6167 * we probe a 5-wide window around each power of two.
6168 * This hits all edge cases, including zero and the max.
6170 for (int t = 0; t < 64; t++) {
6171 for (int d = -5; d <= 5; d++) {
6172 error = dmu_object_info(spa->spa_meta_objset,
6173 (1ULL << t) + d, NULL);
6174 ASSERT(error == 0 || error == ENOENT ||
6180 * If we got any ENOSPC errors on the previous run, destroy something.
6182 if (zs->zs_enospc_count != 0) {
6183 int d = ztest_random(ztest_opts.zo_datasets);
6184 ztest_dataset_destroy(d);
6186 zs->zs_enospc_count = 0;
6188 tid = umem_zalloc(ztest_opts.zo_threads * sizeof (thread_t),
6191 if (ztest_opts.zo_verbose >= 4)
6192 (void) printf("starting main threads...\n");
6195 * Kick off all the tests that run in parallel.
6197 for (int t = 0; t < ztest_opts.zo_threads; t++) {
6198 if (t < ztest_opts.zo_datasets &&
6199 ztest_dataset_open(t) != 0)
6201 VERIFY(thr_create(0, 0, ztest_thread, (void *)(uintptr_t)t,
6202 THR_BOUND, &tid[t]) == 0);
6206 * Wait for all of the tests to complete. We go in reverse order
6207 * so we don't close datasets while threads are still using them.
6209 for (int t = ztest_opts.zo_threads - 1; t >= 0; t--) {
6210 VERIFY(thr_join(tid[t], NULL, NULL) == 0);
6211 if (t < ztest_opts.zo_datasets)
6212 ztest_dataset_close(t);
6215 txg_wait_synced(spa_get_dsl(spa), 0);
6217 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
6218 zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
6219 zfs_dbgmsg_print(FTAG);
6221 umem_free(tid, ztest_opts.zo_threads * sizeof (thread_t));
6223 /* Kill the resume thread */
6224 ztest_exiting = B_TRUE;
6225 VERIFY(thr_join(resume_tid, NULL, NULL) == 0);
6229 * Right before closing the pool, kick off a bunch of async I/O;
6230 * spa_close() should wait for it to complete.
6232 for (uint64_t object = 1; object < 50; object++) {
6233 dmu_prefetch(spa->spa_meta_objset, object, 0, 0, 1ULL << 20,
6234 ZIO_PRIORITY_SYNC_READ);
6237 spa_close(spa, FTAG);
6240 * Verify that we can loop over all pools.
6242 mutex_enter(&spa_namespace_lock);
6243 for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
6244 if (ztest_opts.zo_verbose > 3)
6245 (void) printf("spa_next: found %s\n", spa_name(spa));
6246 mutex_exit(&spa_namespace_lock);
6249 * Verify that we can export the pool and reimport it under a
6252 if (ztest_random(2) == 0) {
6253 char name[ZFS_MAX_DATASET_NAME_LEN];
6254 (void) snprintf(name, sizeof (name), "%s_import",
6255 ztest_opts.zo_pool);
6256 ztest_spa_import_export(ztest_opts.zo_pool, name);
6257 ztest_spa_import_export(name, ztest_opts.zo_pool);
6262 list_destroy(&zcl.zcl_callbacks);
6264 mutex_destroy(&zcl.zcl_callbacks_lock);
6266 rw_destroy(&ztest_name_lock);
6267 mutex_destroy(&ztest_vdev_lock);
6268 mutex_destroy(&ztest_checkpoint_lock);
6274 ztest_ds_t *zd = &ztest_ds[0];
6278 if (ztest_opts.zo_verbose >= 3)
6279 (void) printf("testing spa_freeze()...\n");
6281 kernel_init(FREAD | FWRITE);
6282 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6283 VERIFY3U(0, ==, ztest_dataset_open(0));
6287 * Force the first log block to be transactionally allocated.
6288 * We have to do this before we freeze the pool -- otherwise
6289 * the log chain won't be anchored.
6291 while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
6292 ztest_dmu_object_alloc_free(zd, 0);
6293 zil_commit(zd->zd_zilog, 0);
6296 txg_wait_synced(spa_get_dsl(spa), 0);
6299 * Freeze the pool. This stops spa_sync() from doing anything,
6300 * so that the only way to record changes from now on is the ZIL.
6305 * Because it is hard to predict how much space a write will actually
6306 * require beforehand, we leave ourselves some fudge space to write over
6309 uint64_t capacity = metaslab_class_get_space(spa_normal_class(spa)) / 2;
6312 * Run tests that generate log records but don't alter the pool config
6313 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
6314 * We do a txg_wait_synced() after each iteration to force the txg
6315 * to increase well beyond the last synced value in the uberblock.
6316 * The ZIL should be OK with that.
6318 * Run a random number of times less than zo_maxloops and ensure we do
6319 * not run out of space on the pool.
6321 while (ztest_random(10) != 0 &&
6322 numloops++ < ztest_opts.zo_maxloops &&
6323 metaslab_class_get_alloc(spa_normal_class(spa)) < capacity) {
6325 ztest_od_init(&od, 0, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
6326 VERIFY0(ztest_object_init(zd, &od, sizeof (od), B_FALSE));
6327 ztest_io(zd, od.od_object,
6328 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
6329 txg_wait_synced(spa_get_dsl(spa), 0);
6333 * Commit all of the changes we just generated.
6335 zil_commit(zd->zd_zilog, 0);
6336 txg_wait_synced(spa_get_dsl(spa), 0);
6339 * Close our dataset and close the pool.
6341 ztest_dataset_close(0);
6342 spa_close(spa, FTAG);
6346 * Open and close the pool and dataset to induce log replay.
6348 kernel_init(FREAD | FWRITE);
6349 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6350 ASSERT(spa_freeze_txg(spa) == UINT64_MAX);
6351 VERIFY3U(0, ==, ztest_dataset_open(0));
6352 ztest_dataset_close(0);
6355 txg_wait_synced(spa_get_dsl(spa), 0);
6356 ztest_reguid(NULL, 0);
6358 spa_close(spa, FTAG);
6363 print_time(hrtime_t t, char *timebuf)
6365 hrtime_t s = t / NANOSEC;
6366 hrtime_t m = s / 60;
6367 hrtime_t h = m / 60;
6368 hrtime_t d = h / 24;
6377 (void) sprintf(timebuf,
6378 "%llud%02lluh%02llum%02llus", d, h, m, s);
6380 (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
6382 (void) sprintf(timebuf, "%llum%02llus", m, s);
6384 (void) sprintf(timebuf, "%llus", s);
6392 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
6393 if (ztest_random(2) == 0)
6395 VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
6401 * Create a storage pool with the given name and initial vdev size.
6402 * Then test spa_freeze() functionality.
6405 ztest_init(ztest_shared_t *zs)
6408 nvlist_t *nvroot, *props;
6410 mutex_init(&ztest_vdev_lock, NULL, USYNC_THREAD, NULL);
6411 mutex_init(&ztest_checkpoint_lock, NULL, USYNC_THREAD, NULL);
6412 rw_init(&ztest_name_lock, NULL, USYNC_THREAD, NULL);
6414 kernel_init(FREAD | FWRITE);
6417 * Create the storage pool.
6419 (void) spa_destroy(ztest_opts.zo_pool);
6420 ztest_shared->zs_vdev_next_leaf = 0;
6422 zs->zs_mirrors = ztest_opts.zo_mirrors;
6423 nvroot = make_vdev_root(NULL, NULL, NULL, ztest_opts.zo_vdev_size, 0,
6424 0, ztest_opts.zo_raidz, zs->zs_mirrors, 1);
6425 props = make_random_props();
6426 for (int i = 0; i < SPA_FEATURES; i++) {
6428 (void) snprintf(buf, sizeof (buf), "feature@%s",
6429 spa_feature_table[i].fi_uname);
6430 VERIFY3U(0, ==, nvlist_add_uint64(props, buf, 0));
6432 VERIFY3U(0, ==, spa_create(ztest_opts.zo_pool, nvroot, props, NULL));
6433 nvlist_free(nvroot);
6436 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6437 zs->zs_metaslab_sz =
6438 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
6440 spa_close(spa, FTAG);
6444 ztest_run_zdb(ztest_opts.zo_pool);
6448 ztest_run_zdb(ztest_opts.zo_pool);
6450 rw_destroy(&ztest_name_lock);
6451 mutex_destroy(&ztest_vdev_lock);
6452 mutex_destroy(&ztest_checkpoint_lock);
6458 static char ztest_name_data[] = "/tmp/ztest.data.XXXXXX";
6460 ztest_fd_data = mkstemp(ztest_name_data);
6461 ASSERT3S(ztest_fd_data, >=, 0);
6462 (void) unlink(ztest_name_data);
6467 shared_data_size(ztest_shared_hdr_t *hdr)
6471 size = hdr->zh_hdr_size;
6472 size += hdr->zh_opts_size;
6473 size += hdr->zh_size;
6474 size += hdr->zh_stats_size * hdr->zh_stats_count;
6475 size += hdr->zh_ds_size * hdr->zh_ds_count;
6484 ztest_shared_hdr_t *hdr;
6486 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6487 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6488 ASSERT(hdr != MAP_FAILED);
6490 VERIFY3U(0, ==, ftruncate(ztest_fd_data, sizeof (ztest_shared_hdr_t)));
6492 hdr->zh_hdr_size = sizeof (ztest_shared_hdr_t);
6493 hdr->zh_opts_size = sizeof (ztest_shared_opts_t);
6494 hdr->zh_size = sizeof (ztest_shared_t);
6495 hdr->zh_stats_size = sizeof (ztest_shared_callstate_t);
6496 hdr->zh_stats_count = ZTEST_FUNCS;
6497 hdr->zh_ds_size = sizeof (ztest_shared_ds_t);
6498 hdr->zh_ds_count = ztest_opts.zo_datasets;
6500 size = shared_data_size(hdr);
6501 VERIFY3U(0, ==, ftruncate(ztest_fd_data, size));
6503 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6510 ztest_shared_hdr_t *hdr;
6513 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6514 PROT_READ, MAP_SHARED, ztest_fd_data, 0);
6515 ASSERT(hdr != MAP_FAILED);
6517 size = shared_data_size(hdr);
6519 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6520 hdr = ztest_shared_hdr = (void *)mmap(0, P2ROUNDUP(size, getpagesize()),
6521 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6522 ASSERT(hdr != MAP_FAILED);
6523 buf = (uint8_t *)hdr;
6525 offset = hdr->zh_hdr_size;
6526 ztest_shared_opts = (void *)&buf[offset];
6527 offset += hdr->zh_opts_size;
6528 ztest_shared = (void *)&buf[offset];
6529 offset += hdr->zh_size;
6530 ztest_shared_callstate = (void *)&buf[offset];
6531 offset += hdr->zh_stats_size * hdr->zh_stats_count;
6532 ztest_shared_ds = (void *)&buf[offset];
6536 exec_child(char *cmd, char *libpath, boolean_t ignorekill, int *statusp)
6540 char *cmdbuf = NULL;
6545 cmdbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
6546 (void) strlcpy(cmdbuf, getexecname(), MAXPATHLEN);
6551 fatal(1, "fork failed");
6553 if (pid == 0) { /* child */
6554 char *emptyargv[2] = { cmd, NULL };
6555 char fd_data_str[12];
6557 struct rlimit rl = { 1024, 1024 };
6558 (void) setrlimit(RLIMIT_NOFILE, &rl);
6560 (void) close(ztest_fd_rand);
6562 snprintf(fd_data_str, 12, "%d", ztest_fd_data));
6563 VERIFY0(setenv("ZTEST_FD_DATA", fd_data_str, 1));
6565 (void) enable_extended_FILE_stdio(-1, -1);
6566 if (libpath != NULL)
6567 VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath, 1));
6569 (void) execv(cmd, emptyargv);
6571 (void) execvp(cmd, emptyargv);
6573 ztest_dump_core = B_FALSE;
6574 fatal(B_TRUE, "exec failed: %s", cmd);
6577 if (cmdbuf != NULL) {
6578 umem_free(cmdbuf, MAXPATHLEN);
6582 while (waitpid(pid, &status, 0) != pid)
6584 if (statusp != NULL)
6587 if (WIFEXITED(status)) {
6588 if (WEXITSTATUS(status) != 0) {
6589 (void) fprintf(stderr, "child exited with code %d\n",
6590 WEXITSTATUS(status));
6594 } else if (WIFSIGNALED(status)) {
6595 if (!ignorekill || WTERMSIG(status) != SIGKILL) {
6596 (void) fprintf(stderr, "child died with signal %d\n",
6602 (void) fprintf(stderr, "something strange happened to child\n");
6609 ztest_run_init(void)
6611 ztest_shared_t *zs = ztest_shared;
6613 ASSERT(ztest_opts.zo_init != 0);
6616 * Blow away any existing copy of zpool.cache
6618 (void) remove(spa_config_path);
6621 * Create and initialize our storage pool.
6623 for (int i = 1; i <= ztest_opts.zo_init; i++) {
6624 bzero(zs, sizeof (ztest_shared_t));
6625 if (ztest_opts.zo_verbose >= 3 &&
6626 ztest_opts.zo_init != 1) {
6627 (void) printf("ztest_init(), pass %d\n", i);
6634 main(int argc, char **argv)
6642 ztest_shared_callstate_t *zc;
6644 char numbuf[NN_NUMBUF_SZ];
6647 char *fd_data_str = getenv("ZTEST_FD_DATA");
6649 (void) setvbuf(stdout, NULL, _IOLBF, 0);
6651 dprintf_setup(&argc, argv);
6652 zfs_deadman_synctime_ms = 300000;
6654 * As two-word space map entries may not come up often (especially
6655 * if pool and vdev sizes are small) we want to force at least some
6656 * of them so the feature get tested.
6658 zfs_force_some_double_word_sm_entries = B_TRUE;
6661 * Verify that even extensively damaged split blocks with many
6662 * segments can be reconstructed in a reasonable amount of time
6663 * when reconstruction is known to be possible.
6665 zfs_reconstruct_indirect_damage_fraction = 4;
6667 ztest_fd_rand = open("/dev/urandom", O_RDONLY);
6668 ASSERT3S(ztest_fd_rand, >=, 0);
6671 process_options(argc, argv);
6676 bcopy(&ztest_opts, ztest_shared_opts,
6677 sizeof (*ztest_shared_opts));
6679 ztest_fd_data = atoi(fd_data_str);
6681 bcopy(ztest_shared_opts, &ztest_opts, sizeof (ztest_opts));
6683 ASSERT3U(ztest_opts.zo_datasets, ==, ztest_shared_hdr->zh_ds_count);
6685 /* Override location of zpool.cache */
6686 VERIFY3U(asprintf((char **)&spa_config_path, "%s/zpool.cache",
6687 ztest_opts.zo_dir), !=, -1);
6689 ztest_ds = umem_alloc(ztest_opts.zo_datasets * sizeof (ztest_ds_t),
6694 metaslab_force_ganging = ztest_opts.zo_metaslab_force_ganging;
6695 metaslab_df_alloc_threshold =
6696 zs->zs_metaslab_df_alloc_threshold;
6705 hasalt = (strlen(ztest_opts.zo_alt_ztest) != 0);
6707 if (ztest_opts.zo_verbose >= 1) {
6708 (void) printf("%llu vdevs, %d datasets, %d threads,"
6709 " %llu seconds...\n",
6710 (u_longlong_t)ztest_opts.zo_vdevs,
6711 ztest_opts.zo_datasets,
6712 ztest_opts.zo_threads,
6713 (u_longlong_t)ztest_opts.zo_time);
6716 cmd = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
6717 (void) strlcpy(cmd, getexecname(), MAXNAMELEN);
6719 zs->zs_do_init = B_TRUE;
6720 if (strlen(ztest_opts.zo_alt_ztest) != 0) {
6721 if (ztest_opts.zo_verbose >= 1) {
6722 (void) printf("Executing older ztest for "
6723 "initialization: %s\n", ztest_opts.zo_alt_ztest);
6725 VERIFY(!exec_child(ztest_opts.zo_alt_ztest,
6726 ztest_opts.zo_alt_libpath, B_FALSE, NULL));
6728 VERIFY(!exec_child(NULL, NULL, B_FALSE, NULL));
6730 zs->zs_do_init = B_FALSE;
6732 zs->zs_proc_start = gethrtime();
6733 zs->zs_proc_stop = zs->zs_proc_start + ztest_opts.zo_time * NANOSEC;
6735 for (int f = 0; f < ZTEST_FUNCS; f++) {
6736 zi = &ztest_info[f];
6737 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6738 if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
6739 zc->zc_next = UINT64_MAX;
6741 zc->zc_next = zs->zs_proc_start +
6742 ztest_random(2 * zi->zi_interval[0] + 1);
6746 * Run the tests in a loop. These tests include fault injection
6747 * to verify that self-healing data works, and forced crashes
6748 * to verify that we never lose on-disk consistency.
6750 while (gethrtime() < zs->zs_proc_stop) {
6755 * Initialize the workload counters for each function.
6757 for (int f = 0; f < ZTEST_FUNCS; f++) {
6758 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6763 /* Set the allocation switch size */
6764 zs->zs_metaslab_df_alloc_threshold =
6765 ztest_random(zs->zs_metaslab_sz / 4) + 1;
6767 if (!hasalt || ztest_random(2) == 0) {
6768 if (hasalt && ztest_opts.zo_verbose >= 1) {
6769 (void) printf("Executing newer ztest: %s\n",
6773 killed = exec_child(cmd, NULL, B_TRUE, &status);
6775 if (hasalt && ztest_opts.zo_verbose >= 1) {
6776 (void) printf("Executing older ztest: %s\n",
6777 ztest_opts.zo_alt_ztest);
6780 killed = exec_child(ztest_opts.zo_alt_ztest,
6781 ztest_opts.zo_alt_libpath, B_TRUE, &status);
6788 if (ztest_opts.zo_verbose >= 1) {
6789 hrtime_t now = gethrtime();
6791 now = MIN(now, zs->zs_proc_stop);
6792 print_time(zs->zs_proc_stop - now, timebuf);
6793 nicenum(zs->zs_space, numbuf, sizeof (numbuf));
6795 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
6796 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
6798 WIFEXITED(status) ? "Complete" : "SIGKILL",
6799 (u_longlong_t)zs->zs_enospc_count,
6800 100.0 * zs->zs_alloc / zs->zs_space,
6802 100.0 * (now - zs->zs_proc_start) /
6803 (ztest_opts.zo_time * NANOSEC), timebuf);
6806 if (ztest_opts.zo_verbose >= 2) {
6807 (void) printf("\nWorkload summary:\n\n");
6808 (void) printf("%7s %9s %s\n",
6809 "Calls", "Time", "Function");
6810 (void) printf("%7s %9s %s\n",
6811 "-----", "----", "--------");
6812 for (int f = 0; f < ZTEST_FUNCS; f++) {
6815 zi = &ztest_info[f];
6816 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6817 print_time(zc->zc_time, timebuf);
6818 (void) dladdr((void *)zi->zi_func, &dli);
6819 (void) printf("%7llu %9s %s\n",
6820 (u_longlong_t)zc->zc_count, timebuf,
6823 (void) printf("\n");
6826 ztest_run_zdb(ztest_opts.zo_pool);
6829 if (ztest_opts.zo_verbose >= 1) {
6831 (void) printf("%d runs of older ztest: %s\n", older,
6832 ztest_opts.zo_alt_ztest);
6833 (void) printf("%d runs of newer ztest: %s\n", newer,
6836 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
6837 kills, iters - kills, (100.0 * kills) / MAX(1, iters));
6840 umem_free(cmd, MAXNAMELEN);