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;
202 static ztest_shared_opts_t *ztest_shared_opts;
203 static ztest_shared_opts_t ztest_opts;
205 typedef struct ztest_shared_ds {
209 static ztest_shared_ds_t *ztest_shared_ds;
210 #define ZTEST_GET_SHARED_DS(d) (&ztest_shared_ds[d])
212 #define BT_MAGIC 0x123456789abcdefULL
213 #define MAXFAULTS() \
214 (MAX(zs->zs_mirrors, 1) * (ztest_opts.zo_raidz_parity + 1) - 1)
218 ZTEST_IO_WRITE_PATTERN,
219 ZTEST_IO_WRITE_ZEROES,
226 typedef struct ztest_block_tag {
230 uint64_t bt_dnodesize;
237 typedef struct bufwad {
244 * XXX -- fix zfs range locks to be generic so we can use them here.
266 #define ZTEST_RANGE_LOCKS 64
267 #define ZTEST_OBJECT_LOCKS 64
270 * Object descriptor. Used as a template for object lookup/create/remove.
272 typedef struct ztest_od {
275 dmu_object_type_t od_type;
276 dmu_object_type_t od_crtype;
277 uint64_t od_blocksize;
278 uint64_t od_crblocksize;
279 uint64_t od_crdnodesize;
282 char od_name[ZFS_MAX_DATASET_NAME_LEN];
288 typedef struct ztest_ds {
289 ztest_shared_ds_t *zd_shared;
291 krwlock_t zd_zilog_lock;
293 ztest_od_t *zd_od; /* debugging aid */
294 char zd_name[ZFS_MAX_DATASET_NAME_LEN];
295 kmutex_t zd_dirobj_lock;
296 rll_t zd_object_lock[ZTEST_OBJECT_LOCKS];
297 rll_t zd_range_lock[ZTEST_RANGE_LOCKS];
301 * Per-iteration state.
303 typedef void ztest_func_t(ztest_ds_t *zd, uint64_t id);
305 typedef struct ztest_info {
306 ztest_func_t *zi_func; /* test function */
307 uint64_t zi_iters; /* iterations per execution */
308 uint64_t *zi_interval; /* execute every <interval> seconds */
311 typedef struct ztest_shared_callstate {
312 uint64_t zc_count; /* per-pass count */
313 uint64_t zc_time; /* per-pass time */
314 uint64_t zc_next; /* next time to call this function */
315 } ztest_shared_callstate_t;
317 static ztest_shared_callstate_t *ztest_shared_callstate;
318 #define ZTEST_GET_SHARED_CALLSTATE(c) (&ztest_shared_callstate[c])
321 * Note: these aren't static because we want dladdr() to work.
323 ztest_func_t ztest_dmu_read_write;
324 ztest_func_t ztest_dmu_write_parallel;
325 ztest_func_t ztest_dmu_object_alloc_free;
326 ztest_func_t ztest_dmu_object_next_chunk;
327 ztest_func_t ztest_dmu_commit_callbacks;
328 ztest_func_t ztest_zap;
329 ztest_func_t ztest_zap_parallel;
330 ztest_func_t ztest_zil_commit;
331 ztest_func_t ztest_zil_remount;
332 ztest_func_t ztest_dmu_read_write_zcopy;
333 ztest_func_t ztest_dmu_objset_create_destroy;
334 ztest_func_t ztest_dmu_prealloc;
335 ztest_func_t ztest_fzap;
336 ztest_func_t ztest_dmu_snapshot_create_destroy;
337 ztest_func_t ztest_dsl_prop_get_set;
338 ztest_func_t ztest_spa_prop_get_set;
339 ztest_func_t ztest_spa_create_destroy;
340 ztest_func_t ztest_fault_inject;
341 ztest_func_t ztest_ddt_repair;
342 ztest_func_t ztest_dmu_snapshot_hold;
343 ztest_func_t ztest_scrub;
344 ztest_func_t ztest_dsl_dataset_promote_busy;
345 ztest_func_t ztest_vdev_attach_detach;
346 ztest_func_t ztest_vdev_LUN_growth;
347 ztest_func_t ztest_vdev_add_remove;
348 ztest_func_t ztest_vdev_aux_add_remove;
349 ztest_func_t ztest_split_pool;
350 ztest_func_t ztest_reguid;
351 ztest_func_t ztest_spa_upgrade;
352 ztest_func_t ztest_device_removal;
353 ztest_func_t ztest_remap_blocks;
354 ztest_func_t ztest_spa_checkpoint_create_discard;
355 ztest_func_t ztest_initialize;
356 ztest_func_t ztest_verify_dnode_bt;
358 uint64_t zopt_always = 0ULL * NANOSEC; /* all the time */
359 uint64_t zopt_incessant = 1ULL * NANOSEC / 10; /* every 1/10 second */
360 uint64_t zopt_often = 1ULL * NANOSEC; /* every second */
361 uint64_t zopt_sometimes = 10ULL * NANOSEC; /* every 10 seconds */
362 uint64_t zopt_rarely = 60ULL * NANOSEC; /* every 60 seconds */
364 ztest_info_t ztest_info[] = {
365 { ztest_dmu_read_write, 1, &zopt_always },
366 { ztest_dmu_write_parallel, 10, &zopt_always },
367 { ztest_dmu_object_alloc_free, 1, &zopt_always },
368 { ztest_dmu_object_next_chunk, 1, &zopt_sometimes },
369 { ztest_dmu_commit_callbacks, 1, &zopt_always },
370 { ztest_zap, 30, &zopt_always },
371 { ztest_zap_parallel, 100, &zopt_always },
372 { ztest_split_pool, 1, &zopt_always },
373 { ztest_zil_commit, 1, &zopt_incessant },
374 { ztest_zil_remount, 1, &zopt_sometimes },
375 { ztest_dmu_read_write_zcopy, 1, &zopt_often },
376 { ztest_dmu_objset_create_destroy, 1, &zopt_often },
377 { ztest_dsl_prop_get_set, 1, &zopt_often },
378 { ztest_spa_prop_get_set, 1, &zopt_sometimes },
380 { ztest_dmu_prealloc, 1, &zopt_sometimes },
382 { ztest_fzap, 1, &zopt_sometimes },
383 { ztest_dmu_snapshot_create_destroy, 1, &zopt_sometimes },
384 { ztest_spa_create_destroy, 1, &zopt_sometimes },
385 { ztest_fault_inject, 1, &zopt_incessant },
386 { ztest_ddt_repair, 1, &zopt_sometimes },
387 { ztest_dmu_snapshot_hold, 1, &zopt_sometimes },
388 { ztest_reguid, 1, &zopt_rarely },
389 { ztest_scrub, 1, &zopt_often },
390 { ztest_spa_upgrade, 1, &zopt_rarely },
391 { ztest_dsl_dataset_promote_busy, 1, &zopt_rarely },
392 { ztest_vdev_attach_detach, 1, &zopt_incessant },
393 { ztest_vdev_LUN_growth, 1, &zopt_rarely },
394 { ztest_vdev_add_remove, 1,
395 &ztest_opts.zo_vdevtime },
396 { ztest_vdev_aux_add_remove, 1,
397 &ztest_opts.zo_vdevtime },
398 { ztest_device_removal, 1, &zopt_sometimes },
399 { ztest_remap_blocks, 1, &zopt_sometimes },
400 { ztest_spa_checkpoint_create_discard, 1, &zopt_rarely },
401 { ztest_initialize, 1, &zopt_sometimes },
402 { ztest_verify_dnode_bt, 1, &zopt_sometimes }
405 #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
408 * The following struct is used to hold a list of uncalled commit callbacks.
409 * The callbacks are ordered by txg number.
411 typedef struct ztest_cb_list {
412 kmutex_t zcl_callbacks_lock;
413 list_t zcl_callbacks;
417 * Stuff we need to share writably between parent and child.
419 typedef struct ztest_shared {
420 boolean_t zs_do_init;
421 hrtime_t zs_proc_start;
422 hrtime_t zs_proc_stop;
423 hrtime_t zs_thread_start;
424 hrtime_t zs_thread_stop;
425 hrtime_t zs_thread_kill;
426 uint64_t zs_enospc_count;
427 uint64_t zs_vdev_next_leaf;
428 uint64_t zs_vdev_aux;
433 uint64_t zs_metaslab_sz;
434 uint64_t zs_metaslab_df_alloc_threshold;
438 #define ID_PARALLEL -1ULL
440 static char ztest_dev_template[] = "%s/%s.%llua";
441 static char ztest_aux_template[] = "%s/%s.%s.%llu";
442 ztest_shared_t *ztest_shared;
444 static spa_t *ztest_spa = NULL;
445 static ztest_ds_t *ztest_ds;
447 static kmutex_t ztest_vdev_lock;
448 static boolean_t ztest_device_removal_active = B_FALSE;
449 static kmutex_t ztest_checkpoint_lock;
452 * The ztest_name_lock protects the pool and dataset namespace used by
453 * the individual tests. To modify the namespace, consumers must grab
454 * this lock as writer. Grabbing the lock as reader will ensure that the
455 * namespace does not change while the lock is held.
457 static krwlock_t ztest_name_lock;
459 static boolean_t ztest_dump_core = B_TRUE;
460 static boolean_t ztest_exiting;
462 /* Global commit callback list */
463 static ztest_cb_list_t zcl;
466 ZTEST_META_DNODE = 0,
471 static void usage(boolean_t) __NORETURN;
474 * These libumem hooks provide a reasonable set of defaults for the allocator's
475 * debugging facilities.
480 return ("default,verbose"); /* $UMEM_DEBUG setting */
484 _umem_logging_init(void)
486 return ("fail,contents"); /* $UMEM_LOGGING setting */
489 #define FATAL_MSG_SZ 1024
494 fatal(int do_perror, char *message, ...)
497 int save_errno = errno;
498 char buf[FATAL_MSG_SZ];
500 (void) fflush(stdout);
502 va_start(args, message);
503 (void) sprintf(buf, "ztest: ");
505 (void) vsprintf(buf + strlen(buf), message, args);
508 (void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
509 ": %s", strerror(save_errno));
511 (void) fprintf(stderr, "%s\n", buf);
512 fatal_msg = buf; /* to ease debugging */
519 str2shift(const char *buf)
521 const char *ends = "BKMGTPEZ";
526 for (i = 0; i < strlen(ends); i++) {
527 if (toupper(buf[0]) == ends[i])
530 if (i == strlen(ends)) {
531 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
535 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
538 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
544 nicenumtoull(const char *buf)
549 val = strtoull(buf, &end, 0);
551 (void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
553 } else if (end[0] == '.') {
554 double fval = strtod(buf, &end);
555 fval *= pow(2, str2shift(end));
556 if (fval > UINT64_MAX) {
557 (void) fprintf(stderr, "ztest: value too large: %s\n",
561 val = (uint64_t)fval;
563 int shift = str2shift(end);
564 if (shift >= 64 || (val << shift) >> shift != val) {
565 (void) fprintf(stderr, "ztest: value too large: %s\n",
575 usage(boolean_t requested)
577 const ztest_shared_opts_t *zo = &ztest_opts_defaults;
579 char nice_vdev_size[NN_NUMBUF_SZ];
580 char nice_force_ganging[NN_NUMBUF_SZ];
581 FILE *fp = requested ? stdout : stderr;
583 nicenum(zo->zo_vdev_size, nice_vdev_size, sizeof (nice_vdev_size));
584 nicenum(zo->zo_metaslab_force_ganging, nice_force_ganging,
585 sizeof (nice_force_ganging));
587 (void) fprintf(fp, "Usage: %s\n"
588 "\t[-v vdevs (default: %llu)]\n"
589 "\t[-s size_of_each_vdev (default: %s)]\n"
590 "\t[-a alignment_shift (default: %d)] use 0 for random\n"
591 "\t[-m mirror_copies (default: %d)]\n"
592 "\t[-r raidz_disks (default: %d)]\n"
593 "\t[-R raidz_parity (default: %d)]\n"
594 "\t[-d datasets (default: %d)]\n"
595 "\t[-t threads (default: %d)]\n"
596 "\t[-g gang_block_threshold (default: %s)]\n"
597 "\t[-i init_count (default: %d)] initialize pool i times\n"
598 "\t[-k kill_percentage (default: %llu%%)]\n"
599 "\t[-p pool_name (default: %s)]\n"
600 "\t[-f dir (default: %s)] file directory for vdev files\n"
601 "\t[-V] verbose (use multiple times for ever more blather)\n"
602 "\t[-E] use existing pool instead of creating new one\n"
603 "\t[-T time (default: %llu sec)] total run time\n"
604 "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
605 "\t[-P passtime (default: %llu sec)] time per pass\n"
606 "\t[-B alt_ztest (default: <none>)] alternate ztest path\n"
607 "\t[-o variable=value] ... set global variable to an unsigned\n"
608 "\t 32-bit integer value\n"
609 "\t[-h] (print help)\n"
612 (u_longlong_t)zo->zo_vdevs, /* -v */
613 nice_vdev_size, /* -s */
614 zo->zo_ashift, /* -a */
615 zo->zo_mirrors, /* -m */
616 zo->zo_raidz, /* -r */
617 zo->zo_raidz_parity, /* -R */
618 zo->zo_datasets, /* -d */
619 zo->zo_threads, /* -t */
620 nice_force_ganging, /* -g */
621 zo->zo_init, /* -i */
622 (u_longlong_t)zo->zo_killrate, /* -k */
623 zo->zo_pool, /* -p */
625 (u_longlong_t)zo->zo_time, /* -T */
626 (u_longlong_t)zo->zo_maxloops, /* -F */
627 (u_longlong_t)zo->zo_passtime);
628 exit(requested ? 0 : 1);
632 process_options(int argc, char **argv)
635 ztest_shared_opts_t *zo = &ztest_opts;
639 char altdir[MAXNAMELEN] = { 0 };
641 bcopy(&ztest_opts_defaults, zo, sizeof (*zo));
643 while ((opt = getopt(argc, argv,
644 "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:hF:B:o:")) != EOF) {
661 value = nicenumtoull(optarg);
665 zo->zo_vdevs = value;
668 zo->zo_vdev_size = MAX(SPA_MINDEVSIZE, value);
671 zo->zo_ashift = value;
674 zo->zo_mirrors = value;
677 zo->zo_raidz = MAX(1, value);
680 zo->zo_raidz_parity = MIN(MAX(value, 1), 3);
683 zo->zo_datasets = MAX(1, value);
686 zo->zo_threads = MAX(1, value);
689 zo->zo_metaslab_force_ganging =
690 MAX(SPA_MINBLOCKSIZE << 1, value);
696 zo->zo_killrate = value;
699 (void) strlcpy(zo->zo_pool, optarg,
700 sizeof (zo->zo_pool));
703 path = realpath(optarg, NULL);
705 (void) fprintf(stderr, "error: %s: %s\n",
706 optarg, strerror(errno));
709 (void) strlcpy(zo->zo_dir, path,
710 sizeof (zo->zo_dir));
723 zo->zo_passtime = MAX(1, value);
726 zo->zo_maxloops = MAX(1, value);
729 (void) strlcpy(altdir, optarg, sizeof (altdir));
732 if (set_global_var(optarg) != 0)
745 zo->zo_raidz_parity = MIN(zo->zo_raidz_parity, zo->zo_raidz - 1);
748 (zo->zo_vdevs > 0 ? zo->zo_time * NANOSEC / zo->zo_vdevs :
751 if (strlen(altdir) > 0) {
759 cmd = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
760 realaltdir = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
762 VERIFY(NULL != realpath(getexecname(), cmd));
763 if (0 != access(altdir, F_OK)) {
764 ztest_dump_core = B_FALSE;
765 fatal(B_TRUE, "invalid alternate ztest path: %s",
768 VERIFY(NULL != realpath(altdir, realaltdir));
771 * 'cmd' should be of the form "<anything>/usr/bin/<isa>/ztest".
772 * We want to extract <isa> to determine if we should use
773 * 32 or 64 bit binaries.
775 bin = strstr(cmd, "/usr/bin/");
776 ztest = strstr(bin, "/ztest");
778 isalen = ztest - isa;
779 (void) snprintf(zo->zo_alt_ztest, sizeof (zo->zo_alt_ztest),
780 "%s/usr/bin/%.*s/ztest", realaltdir, isalen, isa);
781 (void) snprintf(zo->zo_alt_libpath, sizeof (zo->zo_alt_libpath),
782 "%s/usr/lib/%.*s", realaltdir, isalen, isa);
784 if (0 != access(zo->zo_alt_ztest, X_OK)) {
785 ztest_dump_core = B_FALSE;
786 fatal(B_TRUE, "invalid alternate ztest: %s",
788 } else if (0 != access(zo->zo_alt_libpath, X_OK)) {
789 ztest_dump_core = B_FALSE;
790 fatal(B_TRUE, "invalid alternate lib directory %s",
794 umem_free(cmd, MAXPATHLEN);
795 umem_free(realaltdir, MAXPATHLEN);
800 ztest_kill(ztest_shared_t *zs)
802 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(ztest_spa));
803 zs->zs_space = metaslab_class_get_space(spa_normal_class(ztest_spa));
806 * Before we kill off ztest, make sure that the config is updated.
807 * See comment above spa_write_cachefile().
809 mutex_enter(&spa_namespace_lock);
810 spa_write_cachefile(ztest_spa, B_FALSE, B_FALSE);
811 mutex_exit(&spa_namespace_lock);
813 zfs_dbgmsg_print(FTAG);
814 (void) kill(getpid(), SIGKILL);
818 ztest_random(uint64_t range)
822 ASSERT3S(ztest_fd_rand, >=, 0);
827 if (read(ztest_fd_rand, &r, sizeof (r)) != sizeof (r))
828 fatal(1, "short read from /dev/urandom");
835 ztest_record_enospc(const char *s)
837 ztest_shared->zs_enospc_count++;
841 ztest_get_ashift(void)
843 if (ztest_opts.zo_ashift == 0)
844 return (SPA_MINBLOCKSHIFT + ztest_random(5));
845 return (ztest_opts.zo_ashift);
849 make_vdev_file(char *path, char *aux, char *pool, size_t size, uint64_t ashift)
851 char pathbuf[MAXPATHLEN];
856 ashift = ztest_get_ashift();
862 vdev = ztest_shared->zs_vdev_aux;
863 (void) snprintf(path, sizeof (pathbuf),
864 ztest_aux_template, ztest_opts.zo_dir,
865 pool == NULL ? ztest_opts.zo_pool : pool,
868 vdev = ztest_shared->zs_vdev_next_leaf++;
869 (void) snprintf(path, sizeof (pathbuf),
870 ztest_dev_template, ztest_opts.zo_dir,
871 pool == NULL ? ztest_opts.zo_pool : pool, vdev);
876 int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
878 fatal(1, "can't open %s", path);
879 if (ftruncate(fd, size) != 0)
880 fatal(1, "can't ftruncate %s", path);
884 VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
885 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
886 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
887 VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
893 make_vdev_raidz(char *path, char *aux, char *pool, size_t size,
894 uint64_t ashift, int r)
896 nvlist_t *raidz, **child;
900 return (make_vdev_file(path, aux, pool, size, ashift));
901 child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
903 for (c = 0; c < r; c++)
904 child[c] = make_vdev_file(path, aux, pool, size, ashift);
906 VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
907 VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
908 VDEV_TYPE_RAIDZ) == 0);
909 VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
910 ztest_opts.zo_raidz_parity) == 0);
911 VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
914 for (c = 0; c < r; c++)
915 nvlist_free(child[c]);
917 umem_free(child, r * sizeof (nvlist_t *));
923 make_vdev_mirror(char *path, char *aux, char *pool, size_t size,
924 uint64_t ashift, int r, int m)
926 nvlist_t *mirror, **child;
930 return (make_vdev_raidz(path, aux, pool, size, ashift, r));
932 child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
934 for (c = 0; c < m; c++)
935 child[c] = make_vdev_raidz(path, aux, pool, size, ashift, r);
937 VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
938 VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
939 VDEV_TYPE_MIRROR) == 0);
940 VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
943 for (c = 0; c < m; c++)
944 nvlist_free(child[c]);
946 umem_free(child, m * sizeof (nvlist_t *));
952 make_vdev_root(char *path, char *aux, char *pool, size_t size, uint64_t ashift,
953 int log, int r, int m, int t)
955 nvlist_t *root, **child;
960 child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
962 for (c = 0; c < t; c++) {
963 child[c] = make_vdev_mirror(path, aux, pool, size, ashift,
965 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
969 VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
970 VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
971 VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
974 for (c = 0; c < t; c++)
975 nvlist_free(child[c]);
977 umem_free(child, t * sizeof (nvlist_t *));
983 * Find a random spa version. Returns back a random spa version in the
984 * range [initial_version, SPA_VERSION_FEATURES].
987 ztest_random_spa_version(uint64_t initial_version)
989 uint64_t version = initial_version;
991 if (version <= SPA_VERSION_BEFORE_FEATURES) {
993 ztest_random(SPA_VERSION_BEFORE_FEATURES - version + 1);
996 if (version > SPA_VERSION_BEFORE_FEATURES)
997 version = SPA_VERSION_FEATURES;
999 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
1004 ztest_random_blocksize(void)
1006 uint64_t block_shift;
1008 * Choose a block size >= the ashift.
1009 * If the SPA supports new MAXBLOCKSIZE, test up to 1MB blocks.
1011 int maxbs = SPA_OLD_MAXBLOCKSHIFT;
1012 if (spa_maxblocksize(ztest_spa) == SPA_MAXBLOCKSIZE)
1014 block_shift = ztest_random(maxbs - ztest_spa->spa_max_ashift + 1);
1015 return (1 << (SPA_MINBLOCKSHIFT + block_shift));
1019 ztest_random_dnodesize(void)
1022 int max_slots = spa_maxdnodesize(ztest_spa) >> DNODE_SHIFT;
1024 if (max_slots == DNODE_MIN_SLOTS)
1025 return (DNODE_MIN_SIZE);
1028 * Weight the random distribution more heavily toward smaller
1029 * dnode sizes since that is more likely to reflect real-world
1032 ASSERT3U(max_slots, >, 4);
1033 switch (ztest_random(10)) {
1035 slots = 5 + ztest_random(max_slots - 4);
1038 slots = 2 + ztest_random(3);
1045 return (slots << DNODE_SHIFT);
1049 ztest_random_ibshift(void)
1051 return (DN_MIN_INDBLKSHIFT +
1052 ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1));
1056 ztest_random_vdev_top(spa_t *spa, boolean_t log_ok)
1059 vdev_t *rvd = spa->spa_root_vdev;
1062 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1065 top = ztest_random(rvd->vdev_children);
1066 tvd = rvd->vdev_child[top];
1067 } while (!vdev_is_concrete(tvd) || (tvd->vdev_islog && !log_ok) ||
1068 tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL);
1074 ztest_random_dsl_prop(zfs_prop_t prop)
1079 value = zfs_prop_random_value(prop, ztest_random(-1ULL));
1080 } while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF);
1086 ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value,
1089 const char *propname = zfs_prop_to_name(prop);
1090 const char *valname;
1091 char setpoint[MAXPATHLEN];
1095 error = dsl_prop_set_int(osname, propname,
1096 (inherit ? ZPROP_SRC_NONE : ZPROP_SRC_LOCAL), value);
1098 if (error == ENOSPC) {
1099 ztest_record_enospc(FTAG);
1104 VERIFY0(dsl_prop_get_integer(osname, propname, &curval, setpoint));
1106 if (ztest_opts.zo_verbose >= 6) {
1107 VERIFY(zfs_prop_index_to_string(prop, curval, &valname) == 0);
1108 (void) printf("%s %s = %s at '%s'\n",
1109 osname, propname, valname, setpoint);
1116 ztest_spa_prop_set_uint64(zpool_prop_t prop, uint64_t value)
1118 spa_t *spa = ztest_spa;
1119 nvlist_t *props = NULL;
1122 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
1123 VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0);
1125 error = spa_prop_set(spa, props);
1129 if (error == ENOSPC) {
1130 ztest_record_enospc(FTAG);
1139 ztest_rll_init(rll_t *rll)
1141 rll->rll_writer = NULL;
1142 rll->rll_readers = 0;
1143 mutex_init(&rll->rll_lock, NULL, USYNC_THREAD, NULL);
1144 cv_init(&rll->rll_cv, NULL, USYNC_THREAD, NULL);
1148 ztest_rll_destroy(rll_t *rll)
1150 ASSERT(rll->rll_writer == NULL);
1151 ASSERT(rll->rll_readers == 0);
1152 mutex_destroy(&rll->rll_lock);
1153 cv_destroy(&rll->rll_cv);
1157 ztest_rll_lock(rll_t *rll, rl_type_t type)
1159 mutex_enter(&rll->rll_lock);
1161 if (type == RL_READER) {
1162 while (rll->rll_writer != NULL)
1163 cv_wait(&rll->rll_cv, &rll->rll_lock);
1166 while (rll->rll_writer != NULL || rll->rll_readers)
1167 cv_wait(&rll->rll_cv, &rll->rll_lock);
1168 rll->rll_writer = curthread;
1171 mutex_exit(&rll->rll_lock);
1175 ztest_rll_unlock(rll_t *rll)
1177 mutex_enter(&rll->rll_lock);
1179 if (rll->rll_writer) {
1180 ASSERT(rll->rll_readers == 0);
1181 rll->rll_writer = NULL;
1183 ASSERT(rll->rll_readers != 0);
1184 ASSERT(rll->rll_writer == NULL);
1188 if (rll->rll_writer == NULL && rll->rll_readers == 0)
1189 cv_broadcast(&rll->rll_cv);
1191 mutex_exit(&rll->rll_lock);
1195 ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type)
1197 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1199 ztest_rll_lock(rll, type);
1203 ztest_object_unlock(ztest_ds_t *zd, uint64_t object)
1205 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1207 ztest_rll_unlock(rll);
1211 ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset,
1212 uint64_t size, rl_type_t type)
1214 uint64_t hash = object ^ (offset % (ZTEST_RANGE_LOCKS + 1));
1215 rll_t *rll = &zd->zd_range_lock[hash & (ZTEST_RANGE_LOCKS - 1)];
1218 rl = umem_alloc(sizeof (*rl), UMEM_NOFAIL);
1219 rl->rl_object = object;
1220 rl->rl_offset = offset;
1224 ztest_rll_lock(rll, type);
1230 ztest_range_unlock(rl_t *rl)
1232 rll_t *rll = rl->rl_lock;
1234 ztest_rll_unlock(rll);
1236 umem_free(rl, sizeof (*rl));
1240 ztest_zd_init(ztest_ds_t *zd, ztest_shared_ds_t *szd, objset_t *os)
1243 zd->zd_zilog = dmu_objset_zil(os);
1244 zd->zd_shared = szd;
1245 dmu_objset_name(os, zd->zd_name);
1247 if (zd->zd_shared != NULL)
1248 zd->zd_shared->zd_seq = 0;
1250 rw_init(&zd->zd_zilog_lock, NULL, USYNC_THREAD, NULL);
1251 mutex_init(&zd->zd_dirobj_lock, NULL, USYNC_THREAD, NULL);
1253 for (int l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1254 ztest_rll_init(&zd->zd_object_lock[l]);
1256 for (int l = 0; l < ZTEST_RANGE_LOCKS; l++)
1257 ztest_rll_init(&zd->zd_range_lock[l]);
1261 ztest_zd_fini(ztest_ds_t *zd)
1263 mutex_destroy(&zd->zd_dirobj_lock);
1265 for (int l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1266 ztest_rll_destroy(&zd->zd_object_lock[l]);
1268 for (int l = 0; l < ZTEST_RANGE_LOCKS; l++)
1269 ztest_rll_destroy(&zd->zd_range_lock[l]);
1272 #define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1275 ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag)
1281 * Attempt to assign tx to some transaction group.
1283 error = dmu_tx_assign(tx, txg_how);
1285 if (error == ERESTART) {
1286 ASSERT(txg_how == TXG_NOWAIT);
1289 ASSERT3U(error, ==, ENOSPC);
1290 ztest_record_enospc(tag);
1295 txg = dmu_tx_get_txg(tx);
1301 ztest_pattern_set(void *buf, uint64_t size, uint64_t value)
1304 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1311 ztest_pattern_match(void *buf, uint64_t size, uint64_t value)
1314 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1318 diff |= (value - *ip++);
1324 ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1325 uint64_t dnodesize, uint64_t offset, uint64_t gen, uint64_t txg,
1328 bt->bt_magic = BT_MAGIC;
1329 bt->bt_objset = dmu_objset_id(os);
1330 bt->bt_object = object;
1331 bt->bt_dnodesize = dnodesize;
1332 bt->bt_offset = offset;
1335 bt->bt_crtxg = crtxg;
1339 ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1340 uint64_t dnodesize, uint64_t offset, uint64_t gen, uint64_t txg,
1343 ASSERT3U(bt->bt_magic, ==, BT_MAGIC);
1344 ASSERT3U(bt->bt_objset, ==, dmu_objset_id(os));
1345 ASSERT3U(bt->bt_object, ==, object);
1346 ASSERT3U(bt->bt_dnodesize, ==, dnodesize);
1347 ASSERT3U(bt->bt_offset, ==, offset);
1348 ASSERT3U(bt->bt_gen, <=, gen);
1349 ASSERT3U(bt->bt_txg, <=, txg);
1350 ASSERT3U(bt->bt_crtxg, ==, crtxg);
1353 static ztest_block_tag_t *
1354 ztest_bt_bonus(dmu_buf_t *db)
1356 dmu_object_info_t doi;
1357 ztest_block_tag_t *bt;
1359 dmu_object_info_from_db(db, &doi);
1360 ASSERT3U(doi.doi_bonus_size, <=, db->db_size);
1361 ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt));
1362 bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt));
1368 * Generate a token to fill up unused bonus buffer space. Try to make
1369 * it unique to the object, generation, and offset to verify that data
1370 * is not getting overwritten by data from other dnodes.
1372 #define ZTEST_BONUS_FILL_TOKEN(obj, ds, gen, offset) \
1373 (((ds) << 48) | ((gen) << 32) | ((obj) << 8) | (offset))
1376 * Fill up the unused bonus buffer region before the block tag with a
1377 * verifiable pattern. Filling the whole bonus area with non-zero data
1378 * helps ensure that all dnode traversal code properly skips the
1379 * interior regions of large dnodes.
1382 ztest_fill_unused_bonus(dmu_buf_t *db, void *end, uint64_t obj,
1383 objset_t *os, uint64_t gen)
1387 ASSERT(IS_P2ALIGNED((char *)end - (char *)db->db_data, 8));
1389 for (bonusp = db->db_data; bonusp < (uint64_t *)end; bonusp++) {
1390 uint64_t token = ZTEST_BONUS_FILL_TOKEN(obj, dmu_objset_id(os),
1391 gen, bonusp - (uint64_t *)db->db_data);
1397 * Verify that the unused area of a bonus buffer is filled with the
1401 ztest_verify_unused_bonus(dmu_buf_t *db, void *end, uint64_t obj,
1402 objset_t *os, uint64_t gen)
1406 for (bonusp = db->db_data; bonusp < (uint64_t *)end; bonusp++) {
1407 uint64_t token = ZTEST_BONUS_FILL_TOKEN(obj, dmu_objset_id(os),
1408 gen, bonusp - (uint64_t *)db->db_data);
1409 VERIFY3U(*bonusp, ==, token);
1417 #define lrz_type lr_mode
1418 #define lrz_blocksize lr_uid
1419 #define lrz_ibshift lr_gid
1420 #define lrz_bonustype lr_rdev
1421 #define lrz_dnodesize lr_crtime[1]
1424 ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr)
1426 char *name = (void *)(lr + 1); /* name follows lr */
1427 size_t namesize = strlen(name) + 1;
1430 if (zil_replaying(zd->zd_zilog, tx))
1433 itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize);
1434 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1435 sizeof (*lr) + namesize - sizeof (lr_t));
1437 zil_itx_assign(zd->zd_zilog, itx, tx);
1441 ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr, uint64_t object)
1443 char *name = (void *)(lr + 1); /* name follows lr */
1444 size_t namesize = strlen(name) + 1;
1447 if (zil_replaying(zd->zd_zilog, tx))
1450 itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize);
1451 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1452 sizeof (*lr) + namesize - sizeof (lr_t));
1454 itx->itx_oid = object;
1455 zil_itx_assign(zd->zd_zilog, itx, tx);
1459 ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr)
1462 itx_wr_state_t write_state = ztest_random(WR_NUM_STATES);
1464 if (zil_replaying(zd->zd_zilog, tx))
1467 if (lr->lr_length > ZIL_MAX_LOG_DATA)
1468 write_state = WR_INDIRECT;
1470 itx = zil_itx_create(TX_WRITE,
1471 sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0));
1473 if (write_state == WR_COPIED &&
1474 dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length,
1475 ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) {
1476 zil_itx_destroy(itx);
1477 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1478 write_state = WR_NEED_COPY;
1480 itx->itx_private = zd;
1481 itx->itx_wr_state = write_state;
1482 itx->itx_sync = (ztest_random(8) == 0);
1484 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1485 sizeof (*lr) - sizeof (lr_t));
1487 zil_itx_assign(zd->zd_zilog, itx, tx);
1491 ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr)
1495 if (zil_replaying(zd->zd_zilog, tx))
1498 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1499 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1500 sizeof (*lr) - sizeof (lr_t));
1502 itx->itx_sync = B_FALSE;
1503 zil_itx_assign(zd->zd_zilog, itx, tx);
1507 ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr)
1511 if (zil_replaying(zd->zd_zilog, tx))
1514 itx = zil_itx_create(TX_SETATTR, sizeof (*lr));
1515 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1516 sizeof (*lr) - sizeof (lr_t));
1518 itx->itx_sync = B_FALSE;
1519 zil_itx_assign(zd->zd_zilog, itx, tx);
1526 ztest_replay_create(void *arg1, void *arg2, boolean_t byteswap)
1528 ztest_ds_t *zd = arg1;
1529 lr_create_t *lr = arg2;
1530 char *name = (void *)(lr + 1); /* name follows lr */
1531 objset_t *os = zd->zd_os;
1532 ztest_block_tag_t *bbt;
1540 byteswap_uint64_array(lr, sizeof (*lr));
1542 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1543 ASSERT(name[0] != '\0');
1545 tx = dmu_tx_create(os);
1547 dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name);
1549 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1550 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1552 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1555 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1559 ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid);
1560 bonuslen = DN_BONUS_SIZE(lr->lrz_dnodesize);
1562 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1563 if (lr->lr_foid == 0) {
1564 lr->lr_foid = zap_create_dnsize(os,
1565 lr->lrz_type, lr->lrz_bonustype,
1566 bonuslen, lr->lrz_dnodesize, tx);
1568 error = zap_create_claim_dnsize(os, lr->lr_foid,
1569 lr->lrz_type, lr->lrz_bonustype,
1570 bonuslen, lr->lrz_dnodesize, tx);
1573 if (lr->lr_foid == 0) {
1574 lr->lr_foid = dmu_object_alloc_dnsize(os,
1575 lr->lrz_type, 0, lr->lrz_bonustype,
1576 bonuslen, lr->lrz_dnodesize, tx);
1578 error = dmu_object_claim_dnsize(os, lr->lr_foid,
1579 lr->lrz_type, 0, lr->lrz_bonustype,
1580 bonuslen, lr->lrz_dnodesize, tx);
1585 ASSERT3U(error, ==, EEXIST);
1586 ASSERT(zd->zd_zilog->zl_replay);
1591 ASSERT(lr->lr_foid != 0);
1593 if (lr->lrz_type != DMU_OT_ZAP_OTHER)
1594 VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid,
1595 lr->lrz_blocksize, lr->lrz_ibshift, tx));
1597 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1598 bbt = ztest_bt_bonus(db);
1599 dmu_buf_will_dirty(db, tx);
1600 ztest_bt_generate(bbt, os, lr->lr_foid, lr->lrz_dnodesize, -1ULL,
1601 lr->lr_gen, txg, txg);
1602 ztest_fill_unused_bonus(db, bbt, lr->lr_foid, os, lr->lr_gen);
1603 dmu_buf_rele(db, FTAG);
1605 VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1,
1608 (void) ztest_log_create(zd, tx, lr);
1616 ztest_replay_remove(void *arg1, void *arg2, boolean_t byteswap)
1618 ztest_ds_t *zd = arg1;
1619 lr_remove_t *lr = arg2;
1620 char *name = (void *)(lr + 1); /* name follows lr */
1621 objset_t *os = zd->zd_os;
1622 dmu_object_info_t doi;
1624 uint64_t object, txg;
1627 byteswap_uint64_array(lr, sizeof (*lr));
1629 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1630 ASSERT(name[0] != '\0');
1633 zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object));
1634 ASSERT(object != 0);
1636 ztest_object_lock(zd, object, RL_WRITER);
1638 VERIFY3U(0, ==, dmu_object_info(os, object, &doi));
1640 tx = dmu_tx_create(os);
1642 dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name);
1643 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1645 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1647 ztest_object_unlock(zd, object);
1651 if (doi.doi_type == DMU_OT_ZAP_OTHER) {
1652 VERIFY3U(0, ==, zap_destroy(os, object, tx));
1654 VERIFY3U(0, ==, dmu_object_free(os, object, tx));
1657 VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx));
1659 (void) ztest_log_remove(zd, tx, lr, object);
1663 ztest_object_unlock(zd, object);
1669 ztest_replay_write(void *arg1, void *arg2, boolean_t byteswap)
1671 ztest_ds_t *zd = arg1;
1672 lr_write_t *lr = arg2;
1673 objset_t *os = zd->zd_os;
1674 void *data = lr + 1; /* data follows lr */
1675 uint64_t offset, length;
1676 ztest_block_tag_t *bt = data;
1677 ztest_block_tag_t *bbt;
1678 uint64_t gen, txg, lrtxg, crtxg;
1679 dmu_object_info_t doi;
1682 arc_buf_t *abuf = NULL;
1686 byteswap_uint64_array(lr, sizeof (*lr));
1688 offset = lr->lr_offset;
1689 length = lr->lr_length;
1691 /* If it's a dmu_sync() block, write the whole block */
1692 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
1693 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
1694 if (length < blocksize) {
1695 offset -= offset % blocksize;
1700 if (bt->bt_magic == BSWAP_64(BT_MAGIC))
1701 byteswap_uint64_array(bt, sizeof (*bt));
1703 if (bt->bt_magic != BT_MAGIC)
1706 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1707 rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER);
1709 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1711 dmu_object_info_from_db(db, &doi);
1713 bbt = ztest_bt_bonus(db);
1714 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1716 crtxg = bbt->bt_crtxg;
1717 lrtxg = lr->lr_common.lrc_txg;
1719 tx = dmu_tx_create(os);
1721 dmu_tx_hold_write(tx, lr->lr_foid, offset, length);
1723 if (ztest_random(8) == 0 && length == doi.doi_data_block_size &&
1724 P2PHASE(offset, length) == 0)
1725 abuf = dmu_request_arcbuf(db, length);
1727 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1730 dmu_return_arcbuf(abuf);
1731 dmu_buf_rele(db, FTAG);
1732 ztest_range_unlock(rl);
1733 ztest_object_unlock(zd, lr->lr_foid);
1739 * Usually, verify the old data before writing new data --
1740 * but not always, because we also want to verify correct
1741 * behavior when the data was not recently read into cache.
1743 ASSERT(offset % doi.doi_data_block_size == 0);
1744 if (ztest_random(4) != 0) {
1745 int prefetch = ztest_random(2) ?
1746 DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
1747 ztest_block_tag_t rbt;
1749 VERIFY(dmu_read(os, lr->lr_foid, offset,
1750 sizeof (rbt), &rbt, prefetch) == 0);
1751 if (rbt.bt_magic == BT_MAGIC) {
1752 ztest_bt_verify(&rbt, os, lr->lr_foid, 0,
1753 offset, gen, txg, crtxg);
1758 * Writes can appear to be newer than the bonus buffer because
1759 * the ztest_get_data() callback does a dmu_read() of the
1760 * open-context data, which may be different than the data
1761 * as it was when the write was generated.
1763 if (zd->zd_zilog->zl_replay) {
1764 ztest_bt_verify(bt, os, lr->lr_foid, 0, offset,
1765 MAX(gen, bt->bt_gen), MAX(txg, lrtxg),
1770 * Set the bt's gen/txg to the bonus buffer's gen/txg
1771 * so that all of the usual ASSERTs will work.
1773 ztest_bt_generate(bt, os, lr->lr_foid, 0, offset, gen, txg,
1778 dmu_write(os, lr->lr_foid, offset, length, data, tx);
1780 bcopy(data, abuf->b_data, length);
1781 dmu_assign_arcbuf(db, offset, abuf, tx);
1784 (void) ztest_log_write(zd, tx, lr);
1786 dmu_buf_rele(db, FTAG);
1790 ztest_range_unlock(rl);
1791 ztest_object_unlock(zd, lr->lr_foid);
1797 ztest_replay_truncate(void *arg1, void *arg2, boolean_t byteswap)
1799 ztest_ds_t *zd = arg1;
1800 lr_truncate_t *lr = arg2;
1801 objset_t *os = zd->zd_os;
1807 byteswap_uint64_array(lr, sizeof (*lr));
1809 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1810 rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length,
1813 tx = dmu_tx_create(os);
1815 dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length);
1817 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1819 ztest_range_unlock(rl);
1820 ztest_object_unlock(zd, lr->lr_foid);
1824 VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset,
1825 lr->lr_length, tx) == 0);
1827 (void) ztest_log_truncate(zd, tx, lr);
1831 ztest_range_unlock(rl);
1832 ztest_object_unlock(zd, lr->lr_foid);
1838 ztest_replay_setattr(void *arg1, void *arg2, boolean_t byteswap)
1840 ztest_ds_t *zd = arg1;
1841 lr_setattr_t *lr = arg2;
1842 objset_t *os = zd->zd_os;
1845 ztest_block_tag_t *bbt;
1846 uint64_t txg, lrtxg, crtxg, dnodesize;
1849 byteswap_uint64_array(lr, sizeof (*lr));
1851 ztest_object_lock(zd, lr->lr_foid, RL_WRITER);
1853 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1855 tx = dmu_tx_create(os);
1856 dmu_tx_hold_bonus(tx, lr->lr_foid);
1858 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1860 dmu_buf_rele(db, FTAG);
1861 ztest_object_unlock(zd, lr->lr_foid);
1865 bbt = ztest_bt_bonus(db);
1866 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1867 crtxg = bbt->bt_crtxg;
1868 lrtxg = lr->lr_common.lrc_txg;
1869 dnodesize = bbt->bt_dnodesize;
1871 if (zd->zd_zilog->zl_replay) {
1872 ASSERT(lr->lr_size != 0);
1873 ASSERT(lr->lr_mode != 0);
1877 * Randomly change the size and increment the generation.
1879 lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) *
1881 lr->lr_mode = bbt->bt_gen + 1;
1886 * Verify that the current bonus buffer is not newer than our txg.
1888 ztest_bt_verify(bbt, os, lr->lr_foid, dnodesize, -1ULL, lr->lr_mode,
1889 MAX(txg, lrtxg), crtxg);
1891 dmu_buf_will_dirty(db, tx);
1893 ASSERT3U(lr->lr_size, >=, sizeof (*bbt));
1894 ASSERT3U(lr->lr_size, <=, db->db_size);
1895 VERIFY0(dmu_set_bonus(db, lr->lr_size, tx));
1896 bbt = ztest_bt_bonus(db);
1898 ztest_bt_generate(bbt, os, lr->lr_foid, dnodesize, -1ULL, lr->lr_mode,
1900 ztest_fill_unused_bonus(db, bbt, lr->lr_foid, os, bbt->bt_gen);
1902 dmu_buf_rele(db, FTAG);
1904 (void) ztest_log_setattr(zd, tx, lr);
1908 ztest_object_unlock(zd, lr->lr_foid);
1913 zil_replay_func_t *ztest_replay_vector[TX_MAX_TYPE] = {
1914 NULL, /* 0 no such transaction type */
1915 ztest_replay_create, /* TX_CREATE */
1916 NULL, /* TX_MKDIR */
1917 NULL, /* TX_MKXATTR */
1918 NULL, /* TX_SYMLINK */
1919 ztest_replay_remove, /* TX_REMOVE */
1920 NULL, /* TX_RMDIR */
1922 NULL, /* TX_RENAME */
1923 ztest_replay_write, /* TX_WRITE */
1924 ztest_replay_truncate, /* TX_TRUNCATE */
1925 ztest_replay_setattr, /* TX_SETATTR */
1927 NULL, /* TX_CREATE_ACL */
1928 NULL, /* TX_CREATE_ATTR */
1929 NULL, /* TX_CREATE_ACL_ATTR */
1930 NULL, /* TX_MKDIR_ACL */
1931 NULL, /* TX_MKDIR_ATTR */
1932 NULL, /* TX_MKDIR_ACL_ATTR */
1933 NULL, /* TX_WRITE2 */
1937 * ZIL get_data callbacks
1942 ztest_get_done(zgd_t *zgd, int error)
1944 ztest_ds_t *zd = zgd->zgd_private;
1945 uint64_t object = zgd->zgd_rl->rl_object;
1948 dmu_buf_rele(zgd->zgd_db, zgd);
1950 ztest_range_unlock(zgd->zgd_rl);
1951 ztest_object_unlock(zd, object);
1953 umem_free(zgd, sizeof (*zgd));
1957 ztest_get_data(void *arg, lr_write_t *lr, char *buf, struct lwb *lwb,
1960 ztest_ds_t *zd = arg;
1961 objset_t *os = zd->zd_os;
1962 uint64_t object = lr->lr_foid;
1963 uint64_t offset = lr->lr_offset;
1964 uint64_t size = lr->lr_length;
1965 uint64_t txg = lr->lr_common.lrc_txg;
1967 dmu_object_info_t doi;
1972 ASSERT3P(lwb, !=, NULL);
1973 ASSERT3P(zio, !=, NULL);
1974 ASSERT3U(size, !=, 0);
1976 ztest_object_lock(zd, object, RL_READER);
1977 error = dmu_bonus_hold(os, object, FTAG, &db);
1979 ztest_object_unlock(zd, object);
1983 crtxg = ztest_bt_bonus(db)->bt_crtxg;
1985 if (crtxg == 0 || crtxg > txg) {
1986 dmu_buf_rele(db, FTAG);
1987 ztest_object_unlock(zd, object);
1991 dmu_object_info_from_db(db, &doi);
1992 dmu_buf_rele(db, FTAG);
1995 zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL);
1997 zgd->zgd_private = zd;
1999 if (buf != NULL) { /* immediate write */
2000 zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
2003 error = dmu_read(os, object, offset, size, buf,
2004 DMU_READ_NO_PREFETCH);
2007 size = doi.doi_data_block_size;
2009 offset = P2ALIGN(offset, size);
2011 ASSERT(offset < size);
2015 zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
2018 error = dmu_buf_hold(os, object, offset, zgd, &db,
2019 DMU_READ_NO_PREFETCH);
2022 blkptr_t *bp = &lr->lr_blkptr;
2027 ASSERT(db->db_offset == offset);
2028 ASSERT(db->db_size == size);
2030 error = dmu_sync(zio, lr->lr_common.lrc_txg,
2031 ztest_get_done, zgd);
2038 ztest_get_done(zgd, error);
2044 ztest_lr_alloc(size_t lrsize, char *name)
2047 size_t namesize = name ? strlen(name) + 1 : 0;
2049 lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
2052 bcopy(name, lr + lrsize, namesize);
2058 ztest_lr_free(void *lr, size_t lrsize, char *name)
2060 size_t namesize = name ? strlen(name) + 1 : 0;
2062 umem_free(lr, lrsize + namesize);
2066 * Lookup a bunch of objects. Returns the number of objects not found.
2069 ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
2074 ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
2076 for (int i = 0; i < count; i++, od++) {
2078 error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
2079 sizeof (uint64_t), 1, &od->od_object);
2081 ASSERT(error == ENOENT);
2082 ASSERT(od->od_object == 0);
2086 ztest_block_tag_t *bbt;
2087 dmu_object_info_t doi;
2089 ASSERT(od->od_object != 0);
2090 ASSERT(missing == 0); /* there should be no gaps */
2092 ztest_object_lock(zd, od->od_object, RL_READER);
2093 VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
2094 od->od_object, FTAG, &db));
2095 dmu_object_info_from_db(db, &doi);
2096 bbt = ztest_bt_bonus(db);
2097 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
2098 od->od_type = doi.doi_type;
2099 od->od_blocksize = doi.doi_data_block_size;
2100 od->od_gen = bbt->bt_gen;
2101 dmu_buf_rele(db, FTAG);
2102 ztest_object_unlock(zd, od->od_object);
2110 ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
2114 ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
2116 for (int i = 0; i < count; i++, od++) {
2123 lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2125 lr->lr_doid = od->od_dir;
2126 lr->lr_foid = 0; /* 0 to allocate, > 0 to claim */
2127 lr->lrz_type = od->od_crtype;
2128 lr->lrz_blocksize = od->od_crblocksize;
2129 lr->lrz_ibshift = ztest_random_ibshift();
2130 lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
2131 lr->lrz_dnodesize = od->od_crdnodesize;
2132 lr->lr_gen = od->od_crgen;
2133 lr->lr_crtime[0] = time(NULL);
2135 if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
2136 ASSERT(missing == 0);
2140 od->od_object = lr->lr_foid;
2141 od->od_type = od->od_crtype;
2142 od->od_blocksize = od->od_crblocksize;
2143 od->od_gen = od->od_crgen;
2144 ASSERT(od->od_object != 0);
2147 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2154 ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
2159 ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
2163 for (int i = count - 1; i >= 0; i--, od--) {
2170 * No object was found.
2172 if (od->od_object == 0)
2175 lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2177 lr->lr_doid = od->od_dir;
2179 if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
2180 ASSERT3U(error, ==, ENOSPC);
2185 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2192 ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
2198 lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
2200 lr->lr_foid = object;
2201 lr->lr_offset = offset;
2202 lr->lr_length = size;
2204 BP_ZERO(&lr->lr_blkptr);
2206 bcopy(data, lr + 1, size);
2208 error = ztest_replay_write(zd, lr, B_FALSE);
2210 ztest_lr_free(lr, sizeof (*lr) + size, NULL);
2216 ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2221 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2223 lr->lr_foid = object;
2224 lr->lr_offset = offset;
2225 lr->lr_length = size;
2227 error = ztest_replay_truncate(zd, lr, B_FALSE);
2229 ztest_lr_free(lr, sizeof (*lr), NULL);
2235 ztest_setattr(ztest_ds_t *zd, uint64_t object)
2240 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2242 lr->lr_foid = object;
2246 error = ztest_replay_setattr(zd, lr, B_FALSE);
2248 ztest_lr_free(lr, sizeof (*lr), NULL);
2254 ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2256 objset_t *os = zd->zd_os;
2261 txg_wait_synced(dmu_objset_pool(os), 0);
2263 ztest_object_lock(zd, object, RL_READER);
2264 rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
2266 tx = dmu_tx_create(os);
2268 dmu_tx_hold_write(tx, object, offset, size);
2270 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
2273 dmu_prealloc(os, object, offset, size, tx);
2275 txg_wait_synced(dmu_objset_pool(os), txg);
2277 (void) dmu_free_long_range(os, object, offset, size);
2280 ztest_range_unlock(rl);
2281 ztest_object_unlock(zd, object);
2285 ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
2288 ztest_block_tag_t wbt;
2289 dmu_object_info_t doi;
2290 enum ztest_io_type io_type;
2294 VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
2295 blocksize = doi.doi_data_block_size;
2296 data = umem_alloc(blocksize, UMEM_NOFAIL);
2299 * Pick an i/o type at random, biased toward writing block tags.
2301 io_type = ztest_random(ZTEST_IO_TYPES);
2302 if (ztest_random(2) == 0)
2303 io_type = ZTEST_IO_WRITE_TAG;
2305 rw_enter(&zd->zd_zilog_lock, RW_READER);
2309 case ZTEST_IO_WRITE_TAG:
2310 ztest_bt_generate(&wbt, zd->zd_os, object, doi.doi_dnodesize,
2312 (void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
2315 case ZTEST_IO_WRITE_PATTERN:
2316 (void) memset(data, 'a' + (object + offset) % 5, blocksize);
2317 if (ztest_random(2) == 0) {
2319 * Induce fletcher2 collisions to ensure that
2320 * zio_ddt_collision() detects and resolves them
2321 * when using fletcher2-verify for deduplication.
2323 ((uint64_t *)data)[0] ^= 1ULL << 63;
2324 ((uint64_t *)data)[4] ^= 1ULL << 63;
2326 (void) ztest_write(zd, object, offset, blocksize, data);
2329 case ZTEST_IO_WRITE_ZEROES:
2330 bzero(data, blocksize);
2331 (void) ztest_write(zd, object, offset, blocksize, data);
2334 case ZTEST_IO_TRUNCATE:
2335 (void) ztest_truncate(zd, object, offset, blocksize);
2338 case ZTEST_IO_SETATTR:
2339 (void) ztest_setattr(zd, object);
2342 case ZTEST_IO_REWRITE:
2343 rw_enter(&ztest_name_lock, RW_READER);
2344 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2345 ZFS_PROP_CHECKSUM, spa_dedup_checksum(ztest_spa),
2347 VERIFY(err == 0 || err == ENOSPC);
2348 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2349 ZFS_PROP_COMPRESSION,
2350 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION),
2352 VERIFY(err == 0 || err == ENOSPC);
2353 rw_exit(&ztest_name_lock);
2355 VERIFY0(dmu_read(zd->zd_os, object, offset, blocksize, data,
2356 DMU_READ_NO_PREFETCH));
2358 (void) ztest_write(zd, object, offset, blocksize, data);
2362 rw_exit(&zd->zd_zilog_lock);
2364 umem_free(data, blocksize);
2368 * Initialize an object description template.
2371 ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
2372 dmu_object_type_t type, uint64_t blocksize, uint64_t dnodesize,
2375 od->od_dir = ZTEST_DIROBJ;
2378 od->od_crtype = type;
2379 od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
2380 od->od_crdnodesize = dnodesize ? dnodesize : ztest_random_dnodesize();
2383 od->od_type = DMU_OT_NONE;
2384 od->od_blocksize = 0;
2387 (void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
2388 tag, (int64_t)id, index);
2392 * Lookup or create the objects for a test using the od template.
2393 * If the objects do not all exist, or if 'remove' is specified,
2394 * remove any existing objects and create new ones. Otherwise,
2395 * use the existing objects.
2398 ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
2400 int count = size / sizeof (*od);
2403 mutex_enter(&zd->zd_dirobj_lock);
2404 if ((ztest_lookup(zd, od, count) != 0 || remove) &&
2405 (ztest_remove(zd, od, count) != 0 ||
2406 ztest_create(zd, od, count) != 0))
2409 mutex_exit(&zd->zd_dirobj_lock);
2416 ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
2418 zilog_t *zilog = zd->zd_zilog;
2420 rw_enter(&zd->zd_zilog_lock, RW_READER);
2422 zil_commit(zilog, ztest_random(ZTEST_OBJECTS));
2425 * Remember the committed values in zd, which is in parent/child
2426 * shared memory. If we die, the next iteration of ztest_run()
2427 * will verify that the log really does contain this record.
2429 mutex_enter(&zilog->zl_lock);
2430 ASSERT(zd->zd_shared != NULL);
2431 ASSERT3U(zd->zd_shared->zd_seq, <=, zilog->zl_commit_lr_seq);
2432 zd->zd_shared->zd_seq = zilog->zl_commit_lr_seq;
2433 mutex_exit(&zilog->zl_lock);
2435 rw_exit(&zd->zd_zilog_lock);
2439 * This function is designed to simulate the operations that occur during a
2440 * mount/unmount operation. We hold the dataset across these operations in an
2441 * attempt to expose any implicit assumptions about ZIL management.
2445 ztest_zil_remount(ztest_ds_t *zd, uint64_t id)
2447 objset_t *os = zd->zd_os;
2450 * We grab the zd_dirobj_lock to ensure that no other thread is
2451 * updating the zil (i.e. adding in-memory log records) and the
2452 * zd_zilog_lock to block any I/O.
2454 mutex_enter(&zd->zd_dirobj_lock);
2455 rw_enter(&zd->zd_zilog_lock, RW_WRITER);
2457 /* zfsvfs_teardown() */
2458 zil_close(zd->zd_zilog);
2460 /* zfsvfs_setup() */
2461 VERIFY(zil_open(os, ztest_get_data) == zd->zd_zilog);
2462 zil_replay(os, zd, ztest_replay_vector);
2464 rw_exit(&zd->zd_zilog_lock);
2465 mutex_exit(&zd->zd_dirobj_lock);
2469 * Verify that we can't destroy an active pool, create an existing pool,
2470 * or create a pool with a bad vdev spec.
2474 ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2476 ztest_shared_opts_t *zo = &ztest_opts;
2481 * Attempt to create using a bad file.
2483 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2484 VERIFY3U(ENOENT, ==,
2485 spa_create("ztest_bad_file", nvroot, NULL, NULL));
2486 nvlist_free(nvroot);
2489 * Attempt to create using a bad mirror.
2491 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 2, 1);
2492 VERIFY3U(ENOENT, ==,
2493 spa_create("ztest_bad_mirror", nvroot, NULL, NULL));
2494 nvlist_free(nvroot);
2497 * Attempt to create an existing pool. It shouldn't matter
2498 * what's in the nvroot; we should fail with EEXIST.
2500 rw_enter(&ztest_name_lock, RW_READER);
2501 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2502 VERIFY3U(EEXIST, ==, spa_create(zo->zo_pool, nvroot, NULL, NULL));
2503 nvlist_free(nvroot);
2504 VERIFY3U(0, ==, spa_open(zo->zo_pool, &spa, FTAG));
2505 VERIFY3U(EBUSY, ==, spa_destroy(zo->zo_pool));
2506 spa_close(spa, FTAG);
2508 rw_exit(&ztest_name_lock);
2513 ztest_spa_upgrade(ztest_ds_t *zd, uint64_t id)
2516 uint64_t initial_version = SPA_VERSION_INITIAL;
2517 uint64_t version, newversion;
2518 nvlist_t *nvroot, *props;
2521 mutex_enter(&ztest_vdev_lock);
2522 name = kmem_asprintf("%s_upgrade", ztest_opts.zo_pool);
2525 * Clean up from previous runs.
2527 (void) spa_destroy(name);
2529 nvroot = make_vdev_root(NULL, NULL, name, ztest_opts.zo_vdev_size, 0,
2530 0, ztest_opts.zo_raidz, ztest_opts.zo_mirrors, 1);
2533 * If we're configuring a RAIDZ device then make sure that the
2534 * the initial version is capable of supporting that feature.
2536 switch (ztest_opts.zo_raidz_parity) {
2539 initial_version = SPA_VERSION_INITIAL;
2542 initial_version = SPA_VERSION_RAIDZ2;
2545 initial_version = SPA_VERSION_RAIDZ3;
2550 * Create a pool with a spa version that can be upgraded. Pick
2551 * a value between initial_version and SPA_VERSION_BEFORE_FEATURES.
2554 version = ztest_random_spa_version(initial_version);
2555 } while (version > SPA_VERSION_BEFORE_FEATURES);
2557 props = fnvlist_alloc();
2558 fnvlist_add_uint64(props,
2559 zpool_prop_to_name(ZPOOL_PROP_VERSION), version);
2560 VERIFY0(spa_create(name, nvroot, props, NULL));
2561 fnvlist_free(nvroot);
2562 fnvlist_free(props);
2564 VERIFY0(spa_open(name, &spa, FTAG));
2565 VERIFY3U(spa_version(spa), ==, version);
2566 newversion = ztest_random_spa_version(version + 1);
2568 if (ztest_opts.zo_verbose >= 4) {
2569 (void) printf("upgrading spa version from %llu to %llu\n",
2570 (u_longlong_t)version, (u_longlong_t)newversion);
2573 spa_upgrade(spa, newversion);
2574 VERIFY3U(spa_version(spa), >, version);
2575 VERIFY3U(spa_version(spa), ==, fnvlist_lookup_uint64(spa->spa_config,
2576 zpool_prop_to_name(ZPOOL_PROP_VERSION)));
2577 spa_close(spa, FTAG);
2580 mutex_exit(&ztest_vdev_lock);
2584 ztest_spa_checkpoint(spa_t *spa)
2586 ASSERT(MUTEX_HELD(&ztest_checkpoint_lock));
2588 int error = spa_checkpoint(spa->spa_name);
2592 case ZFS_ERR_DEVRM_IN_PROGRESS:
2593 case ZFS_ERR_DISCARDING_CHECKPOINT:
2594 case ZFS_ERR_CHECKPOINT_EXISTS:
2597 ztest_record_enospc(FTAG);
2600 fatal(0, "spa_checkpoint(%s) = %d", spa->spa_name, error);
2605 ztest_spa_discard_checkpoint(spa_t *spa)
2607 ASSERT(MUTEX_HELD(&ztest_checkpoint_lock));
2609 int error = spa_checkpoint_discard(spa->spa_name);
2613 case ZFS_ERR_DISCARDING_CHECKPOINT:
2614 case ZFS_ERR_NO_CHECKPOINT:
2617 fatal(0, "spa_discard_checkpoint(%s) = %d",
2618 spa->spa_name, error);
2625 ztest_spa_checkpoint_create_discard(ztest_ds_t *zd, uint64_t id)
2627 spa_t *spa = ztest_spa;
2629 mutex_enter(&ztest_checkpoint_lock);
2630 if (ztest_random(2) == 0) {
2631 ztest_spa_checkpoint(spa);
2633 ztest_spa_discard_checkpoint(spa);
2635 mutex_exit(&ztest_checkpoint_lock);
2640 vdev_lookup_by_path(vdev_t *vd, const char *path)
2644 if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
2647 for (int c = 0; c < vd->vdev_children; c++)
2648 if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
2656 * Find the first available hole which can be used as a top-level.
2659 find_vdev_hole(spa_t *spa)
2661 vdev_t *rvd = spa->spa_root_vdev;
2664 ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
2666 for (c = 0; c < rvd->vdev_children; c++) {
2667 vdev_t *cvd = rvd->vdev_child[c];
2669 if (cvd->vdev_ishole)
2676 * Verify that vdev_add() works as expected.
2680 ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2682 ztest_shared_t *zs = ztest_shared;
2683 spa_t *spa = ztest_spa;
2689 mutex_enter(&ztest_vdev_lock);
2690 leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * ztest_opts.zo_raidz;
2692 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2694 ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2697 * If we have slogs then remove them 1/4 of the time.
2699 if (spa_has_slogs(spa) && ztest_random(4) == 0) {
2701 * Grab the guid from the head of the log class rotor.
2703 guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid;
2705 spa_config_exit(spa, SCL_VDEV, FTAG);
2708 * We have to grab the zs_name_lock as writer to
2709 * prevent a race between removing a slog (dmu_objset_find)
2710 * and destroying a dataset. Removing the slog will
2711 * grab a reference on the dataset which may cause
2712 * dmu_objset_destroy() to fail with EBUSY thus
2713 * leaving the dataset in an inconsistent state.
2715 rw_enter(&ztest_name_lock, RW_WRITER);
2716 error = spa_vdev_remove(spa, guid, B_FALSE);
2717 rw_exit(&ztest_name_lock);
2722 case ZFS_ERR_CHECKPOINT_EXISTS:
2723 case ZFS_ERR_DISCARDING_CHECKPOINT:
2726 fatal(0, "spa_vdev_remove() = %d", error);
2729 spa_config_exit(spa, SCL_VDEV, FTAG);
2732 * Make 1/4 of the devices be log devices.
2734 nvroot = make_vdev_root(NULL, NULL, NULL,
2735 ztest_opts.zo_vdev_size, 0,
2736 ztest_random(4) == 0, ztest_opts.zo_raidz,
2739 error = spa_vdev_add(spa, nvroot);
2740 nvlist_free(nvroot);
2746 ztest_record_enospc("spa_vdev_add");
2749 fatal(0, "spa_vdev_add() = %d", error);
2753 mutex_exit(&ztest_vdev_lock);
2757 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
2761 ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
2763 ztest_shared_t *zs = ztest_shared;
2764 spa_t *spa = ztest_spa;
2765 vdev_t *rvd = spa->spa_root_vdev;
2766 spa_aux_vdev_t *sav;
2771 if (ztest_random(2) == 0) {
2772 sav = &spa->spa_spares;
2773 aux = ZPOOL_CONFIG_SPARES;
2775 sav = &spa->spa_l2cache;
2776 aux = ZPOOL_CONFIG_L2CACHE;
2779 mutex_enter(&ztest_vdev_lock);
2781 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2783 if (sav->sav_count != 0 && ztest_random(4) == 0) {
2785 * Pick a random device to remove.
2787 guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
2790 * Find an unused device we can add.
2792 zs->zs_vdev_aux = 0;
2794 char path[MAXPATHLEN];
2796 (void) snprintf(path, sizeof (path), ztest_aux_template,
2797 ztest_opts.zo_dir, ztest_opts.zo_pool, aux,
2799 for (c = 0; c < sav->sav_count; c++)
2800 if (strcmp(sav->sav_vdevs[c]->vdev_path,
2803 if (c == sav->sav_count &&
2804 vdev_lookup_by_path(rvd, path) == NULL)
2810 spa_config_exit(spa, SCL_VDEV, FTAG);
2816 nvlist_t *nvroot = make_vdev_root(NULL, aux, NULL,
2817 (ztest_opts.zo_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
2818 error = spa_vdev_add(spa, nvroot);
2824 fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
2826 nvlist_free(nvroot);
2829 * Remove an existing device. Sometimes, dirty its
2830 * vdev state first to make sure we handle removal
2831 * of devices that have pending state changes.
2833 if (ztest_random(2) == 0)
2834 (void) vdev_online(spa, guid, 0, NULL);
2836 error = spa_vdev_remove(spa, guid, B_FALSE);
2841 case ZFS_ERR_CHECKPOINT_EXISTS:
2842 case ZFS_ERR_DISCARDING_CHECKPOINT:
2845 fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
2849 mutex_exit(&ztest_vdev_lock);
2853 * split a pool if it has mirror tlvdevs
2857 ztest_split_pool(ztest_ds_t *zd, uint64_t id)
2859 ztest_shared_t *zs = ztest_shared;
2860 spa_t *spa = ztest_spa;
2861 vdev_t *rvd = spa->spa_root_vdev;
2862 nvlist_t *tree, **child, *config, *split, **schild;
2863 uint_t c, children, schildren = 0, lastlogid = 0;
2866 mutex_enter(&ztest_vdev_lock);
2868 /* ensure we have a useable config; mirrors of raidz aren't supported */
2869 if (zs->zs_mirrors < 3 || ztest_opts.zo_raidz > 1) {
2870 mutex_exit(&ztest_vdev_lock);
2874 /* clean up the old pool, if any */
2875 (void) spa_destroy("splitp");
2877 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2879 /* generate a config from the existing config */
2880 mutex_enter(&spa->spa_props_lock);
2881 VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
2883 mutex_exit(&spa->spa_props_lock);
2885 VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2888 schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
2889 for (c = 0; c < children; c++) {
2890 vdev_t *tvd = rvd->vdev_child[c];
2894 if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
2895 VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
2897 VERIFY(nvlist_add_string(schild[schildren],
2898 ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
2899 VERIFY(nvlist_add_uint64(schild[schildren],
2900 ZPOOL_CONFIG_IS_HOLE, 1) == 0);
2902 lastlogid = schildren;
2907 VERIFY(nvlist_lookup_nvlist_array(child[c],
2908 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2909 VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
2912 /* OK, create a config that can be used to split */
2913 VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
2914 VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
2915 VDEV_TYPE_ROOT) == 0);
2916 VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
2917 lastlogid != 0 ? lastlogid : schildren) == 0);
2919 VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
2920 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
2922 for (c = 0; c < schildren; c++)
2923 nvlist_free(schild[c]);
2927 spa_config_exit(spa, SCL_VDEV, FTAG);
2929 rw_enter(&ztest_name_lock, RW_WRITER);
2930 error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
2931 rw_exit(&ztest_name_lock);
2933 nvlist_free(config);
2936 (void) printf("successful split - results:\n");
2937 mutex_enter(&spa_namespace_lock);
2938 show_pool_stats(spa);
2939 show_pool_stats(spa_lookup("splitp"));
2940 mutex_exit(&spa_namespace_lock);
2944 mutex_exit(&ztest_vdev_lock);
2948 * Verify that we can attach and detach devices.
2952 ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
2954 ztest_shared_t *zs = ztest_shared;
2955 spa_t *spa = ztest_spa;
2956 spa_aux_vdev_t *sav = &spa->spa_spares;
2957 vdev_t *rvd = spa->spa_root_vdev;
2958 vdev_t *oldvd, *newvd, *pvd;
2962 uint64_t ashift = ztest_get_ashift();
2963 uint64_t oldguid, pguid;
2964 uint64_t oldsize, newsize;
2965 char oldpath[MAXPATHLEN], newpath[MAXPATHLEN];
2967 int oldvd_has_siblings = B_FALSE;
2968 int newvd_is_spare = B_FALSE;
2970 int error, expected_error;
2972 mutex_enter(&ztest_vdev_lock);
2973 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
2975 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2978 * If a vdev is in the process of being removed, its removal may
2979 * finish while we are in progress, leading to an unexpected error
2980 * value. Don't bother trying to attach while we are in the middle
2983 if (ztest_device_removal_active) {
2984 spa_config_exit(spa, SCL_ALL, FTAG);
2985 mutex_exit(&ztest_vdev_lock);
2990 * Decide whether to do an attach or a replace.
2992 replacing = ztest_random(2);
2995 * Pick a random top-level vdev.
2997 top = ztest_random_vdev_top(spa, B_TRUE);
3000 * Pick a random leaf within it.
3002 leaf = ztest_random(leaves);
3007 oldvd = rvd->vdev_child[top];
3008 if (zs->zs_mirrors >= 1) {
3009 ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
3010 ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
3011 oldvd = oldvd->vdev_child[leaf / ztest_opts.zo_raidz];
3013 if (ztest_opts.zo_raidz > 1) {
3014 ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
3015 ASSERT(oldvd->vdev_children == ztest_opts.zo_raidz);
3016 oldvd = oldvd->vdev_child[leaf % ztest_opts.zo_raidz];
3020 * If we're already doing an attach or replace, oldvd may be a
3021 * mirror vdev -- in which case, pick a random child.
3023 while (oldvd->vdev_children != 0) {
3024 oldvd_has_siblings = B_TRUE;
3025 ASSERT(oldvd->vdev_children >= 2);
3026 oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
3029 oldguid = oldvd->vdev_guid;
3030 oldsize = vdev_get_min_asize(oldvd);
3031 oldvd_is_log = oldvd->vdev_top->vdev_islog;
3032 (void) strcpy(oldpath, oldvd->vdev_path);
3033 pvd = oldvd->vdev_parent;
3034 pguid = pvd->vdev_guid;
3037 * If oldvd has siblings, then half of the time, detach it.
3039 if (oldvd_has_siblings && ztest_random(2) == 0) {
3040 spa_config_exit(spa, SCL_ALL, FTAG);
3041 error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
3042 if (error != 0 && error != ENODEV && error != EBUSY &&
3043 error != ENOTSUP && error != ZFS_ERR_CHECKPOINT_EXISTS &&
3044 error != ZFS_ERR_DISCARDING_CHECKPOINT)
3045 fatal(0, "detach (%s) returned %d", oldpath, error);
3046 mutex_exit(&ztest_vdev_lock);
3051 * For the new vdev, choose with equal probability between the two
3052 * standard paths (ending in either 'a' or 'b') or a random hot spare.
3054 if (sav->sav_count != 0 && ztest_random(3) == 0) {
3055 newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
3056 newvd_is_spare = B_TRUE;
3057 (void) strcpy(newpath, newvd->vdev_path);
3059 (void) snprintf(newpath, sizeof (newpath), ztest_dev_template,
3060 ztest_opts.zo_dir, ztest_opts.zo_pool,
3061 top * leaves + leaf);
3062 if (ztest_random(2) == 0)
3063 newpath[strlen(newpath) - 1] = 'b';
3064 newvd = vdev_lookup_by_path(rvd, newpath);
3069 * Reopen to ensure the vdev's asize field isn't stale.
3072 newsize = vdev_get_min_asize(newvd);
3075 * Make newsize a little bigger or smaller than oldsize.
3076 * If it's smaller, the attach should fail.
3077 * If it's larger, and we're doing a replace,
3078 * we should get dynamic LUN growth when we're done.
3080 newsize = 10 * oldsize / (9 + ztest_random(3));
3084 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
3085 * unless it's a replace; in that case any non-replacing parent is OK.
3087 * If newvd is already part of the pool, it should fail with EBUSY.
3089 * If newvd is too small, it should fail with EOVERFLOW.
3091 if (pvd->vdev_ops != &vdev_mirror_ops &&
3092 pvd->vdev_ops != &vdev_root_ops && (!replacing ||
3093 pvd->vdev_ops == &vdev_replacing_ops ||
3094 pvd->vdev_ops == &vdev_spare_ops))
3095 expected_error = ENOTSUP;
3096 else if (newvd_is_spare && (!replacing || oldvd_is_log))
3097 expected_error = ENOTSUP;
3098 else if (newvd == oldvd)
3099 expected_error = replacing ? 0 : EBUSY;
3100 else if (vdev_lookup_by_path(rvd, newpath) != NULL)
3101 expected_error = EBUSY;
3102 else if (newsize < oldsize)
3103 expected_error = EOVERFLOW;
3104 else if (ashift > oldvd->vdev_top->vdev_ashift)
3105 expected_error = EDOM;
3109 spa_config_exit(spa, SCL_ALL, FTAG);
3112 * Build the nvlist describing newpath.
3114 root = make_vdev_root(newpath, NULL, NULL, newvd == NULL ? newsize : 0,
3115 ashift, 0, 0, 0, 1);
3117 error = spa_vdev_attach(spa, oldguid, root, replacing);
3122 * If our parent was the replacing vdev, but the replace completed,
3123 * then instead of failing with ENOTSUP we may either succeed,
3124 * fail with ENODEV, or fail with EOVERFLOW.
3126 if (expected_error == ENOTSUP &&
3127 (error == 0 || error == ENODEV || error == EOVERFLOW))
3128 expected_error = error;
3131 * If someone grew the LUN, the replacement may be too small.
3133 if (error == EOVERFLOW || error == EBUSY)
3134 expected_error = error;
3136 if (error == ZFS_ERR_CHECKPOINT_EXISTS ||
3137 error == ZFS_ERR_DISCARDING_CHECKPOINT)
3138 expected_error = error;
3140 /* XXX workaround 6690467 */
3141 if (error != expected_error && expected_error != EBUSY) {
3142 fatal(0, "attach (%s %llu, %s %llu, %d) "
3143 "returned %d, expected %d",
3144 oldpath, oldsize, newpath,
3145 newsize, replacing, error, expected_error);
3148 mutex_exit(&ztest_vdev_lock);
3153 ztest_device_removal(ztest_ds_t *zd, uint64_t id)
3155 spa_t *spa = ztest_spa;
3160 mutex_enter(&ztest_vdev_lock);
3162 if (ztest_device_removal_active) {
3163 mutex_exit(&ztest_vdev_lock);
3168 * Remove a random top-level vdev and wait for removal to finish.
3170 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
3171 vd = vdev_lookup_top(spa, ztest_random_vdev_top(spa, B_FALSE));
3172 guid = vd->vdev_guid;
3173 spa_config_exit(spa, SCL_VDEV, FTAG);
3175 error = spa_vdev_remove(spa, guid, B_FALSE);
3177 ztest_device_removal_active = B_TRUE;
3178 mutex_exit(&ztest_vdev_lock);
3180 while (spa->spa_vdev_removal != NULL)
3181 txg_wait_synced(spa_get_dsl(spa), 0);
3183 mutex_exit(&ztest_vdev_lock);
3188 * The pool needs to be scrubbed after completing device removal.
3189 * Failure to do so may result in checksum errors due to the
3190 * strategy employed by ztest_fault_inject() when selecting which
3191 * offset are redundant and can be damaged.
3193 error = spa_scan(spa, POOL_SCAN_SCRUB);
3195 while (dsl_scan_scrubbing(spa_get_dsl(spa)))
3196 txg_wait_synced(spa_get_dsl(spa), 0);
3199 mutex_enter(&ztest_vdev_lock);
3200 ztest_device_removal_active = B_FALSE;
3201 mutex_exit(&ztest_vdev_lock);
3205 * Callback function which expands the physical size of the vdev.
3208 grow_vdev(vdev_t *vd, void *arg)
3210 spa_t *spa = vd->vdev_spa;
3211 size_t *newsize = arg;
3215 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3216 ASSERT(vd->vdev_ops->vdev_op_leaf);
3218 if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
3221 fsize = lseek(fd, 0, SEEK_END);
3222 (void) ftruncate(fd, *newsize);
3224 if (ztest_opts.zo_verbose >= 6) {
3225 (void) printf("%s grew from %lu to %lu bytes\n",
3226 vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
3233 * Callback function which expands a given vdev by calling vdev_online().
3237 online_vdev(vdev_t *vd, void *arg)
3239 spa_t *spa = vd->vdev_spa;
3240 vdev_t *tvd = vd->vdev_top;
3241 uint64_t guid = vd->vdev_guid;
3242 uint64_t generation = spa->spa_config_generation + 1;
3243 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
3246 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3247 ASSERT(vd->vdev_ops->vdev_op_leaf);
3249 /* Calling vdev_online will initialize the new metaslabs */
3250 spa_config_exit(spa, SCL_STATE, spa);
3251 error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
3252 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3255 * If vdev_online returned an error or the underlying vdev_open
3256 * failed then we abort the expand. The only way to know that
3257 * vdev_open fails is by checking the returned newstate.
3259 if (error || newstate != VDEV_STATE_HEALTHY) {
3260 if (ztest_opts.zo_verbose >= 5) {
3261 (void) printf("Unable to expand vdev, state %llu, "
3262 "error %d\n", (u_longlong_t)newstate, error);
3266 ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
3269 * Since we dropped the lock we need to ensure that we're
3270 * still talking to the original vdev. It's possible this
3271 * vdev may have been detached/replaced while we were
3272 * trying to online it.
3274 if (generation != spa->spa_config_generation) {
3275 if (ztest_opts.zo_verbose >= 5) {
3276 (void) printf("vdev configuration has changed, "
3277 "guid %llu, state %llu, expected gen %llu, "
3280 (u_longlong_t)tvd->vdev_state,
3281 (u_longlong_t)generation,
3282 (u_longlong_t)spa->spa_config_generation);
3290 * Traverse the vdev tree calling the supplied function.
3291 * We continue to walk the tree until we either have walked all
3292 * children or we receive a non-NULL return from the callback.
3293 * If a NULL callback is passed, then we just return back the first
3294 * leaf vdev we encounter.
3297 vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
3299 if (vd->vdev_ops->vdev_op_leaf) {
3303 return (func(vd, arg));
3306 for (uint_t c = 0; c < vd->vdev_children; c++) {
3307 vdev_t *cvd = vd->vdev_child[c];
3308 if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
3315 * Verify that dynamic LUN growth works as expected.
3319 ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
3321 spa_t *spa = ztest_spa;
3323 metaslab_class_t *mc;
3324 metaslab_group_t *mg;
3325 size_t psize, newsize;
3327 uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
3329 mutex_enter(&ztest_checkpoint_lock);
3330 mutex_enter(&ztest_vdev_lock);
3331 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3334 * If there is a vdev removal in progress, it could complete while
3335 * we are running, in which case we would not be able to verify
3336 * that the metaslab_class space increased (because it decreases
3337 * when the device removal completes).
3339 if (ztest_device_removal_active) {
3340 spa_config_exit(spa, SCL_STATE, spa);
3341 mutex_exit(&ztest_vdev_lock);
3342 mutex_exit(&ztest_checkpoint_lock);
3346 top = ztest_random_vdev_top(spa, B_TRUE);
3348 tvd = spa->spa_root_vdev->vdev_child[top];
3351 old_ms_count = tvd->vdev_ms_count;
3352 old_class_space = metaslab_class_get_space(mc);
3355 * Determine the size of the first leaf vdev associated with
3356 * our top-level device.
3358 vd = vdev_walk_tree(tvd, NULL, NULL);
3359 ASSERT3P(vd, !=, NULL);
3360 ASSERT(vd->vdev_ops->vdev_op_leaf);
3362 psize = vd->vdev_psize;
3365 * We only try to expand the vdev if it's healthy, less than 4x its
3366 * original size, and it has a valid psize.
3368 if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
3369 psize == 0 || psize >= 4 * ztest_opts.zo_vdev_size) {
3370 spa_config_exit(spa, SCL_STATE, spa);
3371 mutex_exit(&ztest_vdev_lock);
3372 mutex_exit(&ztest_checkpoint_lock);
3376 newsize = psize + psize / 8;
3377 ASSERT3U(newsize, >, psize);
3379 if (ztest_opts.zo_verbose >= 6) {
3380 (void) printf("Expanding LUN %s from %lu to %lu\n",
3381 vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
3385 * Growing the vdev is a two step process:
3386 * 1). expand the physical size (i.e. relabel)
3387 * 2). online the vdev to create the new metaslabs
3389 if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
3390 vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
3391 tvd->vdev_state != VDEV_STATE_HEALTHY) {
3392 if (ztest_opts.zo_verbose >= 5) {
3393 (void) printf("Could not expand LUN because "
3394 "the vdev configuration changed.\n");
3396 spa_config_exit(spa, SCL_STATE, spa);
3397 mutex_exit(&ztest_vdev_lock);
3398 mutex_exit(&ztest_checkpoint_lock);
3402 spa_config_exit(spa, SCL_STATE, spa);
3405 * Expanding the LUN will update the config asynchronously,
3406 * thus we must wait for the async thread to complete any
3407 * pending tasks before proceeding.
3411 mutex_enter(&spa->spa_async_lock);
3412 done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
3413 mutex_exit(&spa->spa_async_lock);
3416 txg_wait_synced(spa_get_dsl(spa), 0);
3417 (void) poll(NULL, 0, 100);
3420 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3422 tvd = spa->spa_root_vdev->vdev_child[top];
3423 new_ms_count = tvd->vdev_ms_count;
3424 new_class_space = metaslab_class_get_space(mc);
3426 if (tvd->vdev_mg != mg || mg->mg_class != mc) {
3427 if (ztest_opts.zo_verbose >= 5) {
3428 (void) printf("Could not verify LUN expansion due to "
3429 "intervening vdev offline or remove.\n");
3431 spa_config_exit(spa, SCL_STATE, spa);
3432 mutex_exit(&ztest_vdev_lock);
3433 mutex_exit(&ztest_checkpoint_lock);
3438 * Make sure we were able to grow the vdev.
3440 if (new_ms_count <= old_ms_count) {
3441 fatal(0, "LUN expansion failed: ms_count %llu < %llu\n",
3442 old_ms_count, new_ms_count);
3446 * Make sure we were able to grow the pool.
3448 if (new_class_space <= old_class_space) {
3449 fatal(0, "LUN expansion failed: class_space %llu < %llu\n",
3450 old_class_space, new_class_space);
3453 if (ztest_opts.zo_verbose >= 5) {
3454 char oldnumbuf[NN_NUMBUF_SZ], newnumbuf[NN_NUMBUF_SZ];
3456 nicenum(old_class_space, oldnumbuf, sizeof (oldnumbuf));
3457 nicenum(new_class_space, newnumbuf, sizeof (newnumbuf));
3458 (void) printf("%s grew from %s to %s\n",
3459 spa->spa_name, oldnumbuf, newnumbuf);
3462 spa_config_exit(spa, SCL_STATE, spa);
3463 mutex_exit(&ztest_vdev_lock);
3464 mutex_exit(&ztest_checkpoint_lock);
3468 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
3472 ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3475 * Create the objects common to all ztest datasets.
3477 VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
3478 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
3482 ztest_dataset_create(char *dsname)
3484 uint64_t zilset = ztest_random(100);
3485 int err = dmu_objset_create(dsname, DMU_OST_OTHER, 0,
3486 ztest_objset_create_cb, NULL);
3488 if (err || zilset < 80)
3491 if (ztest_opts.zo_verbose >= 6)
3492 (void) printf("Setting dataset %s to sync always\n", dsname);
3493 return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC,
3494 ZFS_SYNC_ALWAYS, B_FALSE));
3499 ztest_objset_destroy_cb(const char *name, void *arg)
3502 dmu_object_info_t doi;
3506 * Verify that the dataset contains a directory object.
3508 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_TRUE, FTAG, &os));
3509 error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
3510 if (error != ENOENT) {
3511 /* We could have crashed in the middle of destroying it */
3513 ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
3514 ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
3516 dmu_objset_disown(os, FTAG);
3519 * Destroy the dataset.
3521 if (strchr(name, '@') != NULL) {
3522 VERIFY0(dsl_destroy_snapshot(name, B_FALSE));
3524 VERIFY0(dsl_destroy_head(name));
3530 ztest_snapshot_create(char *osname, uint64_t id)
3532 char snapname[ZFS_MAX_DATASET_NAME_LEN];
3535 (void) snprintf(snapname, sizeof (snapname), "%llu", (u_longlong_t)id);
3537 error = dmu_objset_snapshot_one(osname, snapname);
3538 if (error == ENOSPC) {
3539 ztest_record_enospc(FTAG);
3542 if (error != 0 && error != EEXIST) {
3543 fatal(0, "ztest_snapshot_create(%s@%s) = %d", osname,
3550 ztest_snapshot_destroy(char *osname, uint64_t id)
3552 char snapname[ZFS_MAX_DATASET_NAME_LEN];
3555 (void) snprintf(snapname, sizeof (snapname), "%s@%llu", osname,
3558 error = dsl_destroy_snapshot(snapname, B_FALSE);
3559 if (error != 0 && error != ENOENT)
3560 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
3566 ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
3572 char name[ZFS_MAX_DATASET_NAME_LEN];
3575 rw_enter(&ztest_name_lock, RW_READER);
3577 (void) snprintf(name, sizeof (name), "%s/temp_%llu",
3578 ztest_opts.zo_pool, (u_longlong_t)id);
3581 * If this dataset exists from a previous run, process its replay log
3582 * half of the time. If we don't replay it, then dmu_objset_destroy()
3583 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
3585 if (ztest_random(2) == 0 &&
3586 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) {
3587 ztest_zd_init(&zdtmp, NULL, os);
3588 zil_replay(os, &zdtmp, ztest_replay_vector);
3589 ztest_zd_fini(&zdtmp);
3590 dmu_objset_disown(os, FTAG);
3594 * There may be an old instance of the dataset we're about to
3595 * create lying around from a previous run. If so, destroy it
3596 * and all of its snapshots.
3598 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
3599 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
3602 * Verify that the destroyed dataset is no longer in the namespace.
3604 VERIFY3U(ENOENT, ==, dmu_objset_own(name, DMU_OST_OTHER, B_TRUE,
3608 * Verify that we can create a new dataset.
3610 error = ztest_dataset_create(name);
3612 if (error == ENOSPC) {
3613 ztest_record_enospc(FTAG);
3614 rw_exit(&ztest_name_lock);
3617 fatal(0, "dmu_objset_create(%s) = %d", name, error);
3620 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os));
3622 ztest_zd_init(&zdtmp, NULL, os);
3625 * Open the intent log for it.
3627 zilog = zil_open(os, ztest_get_data);
3630 * Put some objects in there, do a little I/O to them,
3631 * and randomly take a couple of snapshots along the way.
3633 iters = ztest_random(5);
3634 for (int i = 0; i < iters; i++) {
3635 ztest_dmu_object_alloc_free(&zdtmp, id);
3636 if (ztest_random(iters) == 0)
3637 (void) ztest_snapshot_create(name, i);
3641 * Verify that we cannot create an existing dataset.
3643 VERIFY3U(EEXIST, ==,
3644 dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL));
3647 * Verify that we can hold an objset that is also owned.
3649 VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
3650 dmu_objset_rele(os2, FTAG);
3653 * Verify that we cannot own an objset that is already owned.
3656 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2));
3659 dmu_objset_disown(os, FTAG);
3660 ztest_zd_fini(&zdtmp);
3662 rw_exit(&ztest_name_lock);
3666 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3669 ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
3671 rw_enter(&ztest_name_lock, RW_READER);
3672 (void) ztest_snapshot_destroy(zd->zd_name, id);
3673 (void) ztest_snapshot_create(zd->zd_name, id);
3674 rw_exit(&ztest_name_lock);
3678 * Cleanup non-standard snapshots and clones.
3681 ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
3683 char snap1name[ZFS_MAX_DATASET_NAME_LEN];
3684 char clone1name[ZFS_MAX_DATASET_NAME_LEN];
3685 char snap2name[ZFS_MAX_DATASET_NAME_LEN];
3686 char clone2name[ZFS_MAX_DATASET_NAME_LEN];
3687 char snap3name[ZFS_MAX_DATASET_NAME_LEN];
3690 (void) snprintf(snap1name, sizeof (snap1name),
3691 "%s@s1_%llu", osname, id);
3692 (void) snprintf(clone1name, sizeof (clone1name),
3693 "%s/c1_%llu", osname, id);
3694 (void) snprintf(snap2name, sizeof (snap2name),
3695 "%s@s2_%llu", clone1name, id);
3696 (void) snprintf(clone2name, sizeof (clone2name),
3697 "%s/c2_%llu", osname, id);
3698 (void) snprintf(snap3name, sizeof (snap3name),
3699 "%s@s3_%llu", clone1name, id);
3701 error = dsl_destroy_head(clone2name);
3702 if (error && error != ENOENT)
3703 fatal(0, "dsl_destroy_head(%s) = %d", clone2name, error);
3704 error = dsl_destroy_snapshot(snap3name, B_FALSE);
3705 if (error && error != ENOENT)
3706 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap3name, error);
3707 error = dsl_destroy_snapshot(snap2name, B_FALSE);
3708 if (error && error != ENOENT)
3709 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap2name, error);
3710 error = dsl_destroy_head(clone1name);
3711 if (error && error != ENOENT)
3712 fatal(0, "dsl_destroy_head(%s) = %d", clone1name, error);
3713 error = dsl_destroy_snapshot(snap1name, B_FALSE);
3714 if (error && error != ENOENT)
3715 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap1name, error);
3719 * Verify dsl_dataset_promote handles EBUSY
3722 ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
3725 char snap1name[ZFS_MAX_DATASET_NAME_LEN];
3726 char clone1name[ZFS_MAX_DATASET_NAME_LEN];
3727 char snap2name[ZFS_MAX_DATASET_NAME_LEN];
3728 char clone2name[ZFS_MAX_DATASET_NAME_LEN];
3729 char snap3name[ZFS_MAX_DATASET_NAME_LEN];
3730 char *osname = zd->zd_name;
3733 rw_enter(&ztest_name_lock, RW_READER);
3735 ztest_dsl_dataset_cleanup(osname, id);
3737 (void) snprintf(snap1name, sizeof (snap1name),
3738 "%s@s1_%llu", osname, id);
3739 (void) snprintf(clone1name, sizeof (clone1name),
3740 "%s/c1_%llu", osname, id);
3741 (void) snprintf(snap2name, sizeof (snap2name),
3742 "%s@s2_%llu", clone1name, id);
3743 (void) snprintf(clone2name, sizeof (clone2name),
3744 "%s/c2_%llu", osname, id);
3745 (void) snprintf(snap3name, sizeof (snap3name),
3746 "%s@s3_%llu", clone1name, id);
3748 error = dmu_objset_snapshot_one(osname, strchr(snap1name, '@') + 1);
3749 if (error && error != EEXIST) {
3750 if (error == ENOSPC) {
3751 ztest_record_enospc(FTAG);
3754 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
3757 error = dmu_objset_clone(clone1name, snap1name);
3759 if (error == ENOSPC) {
3760 ztest_record_enospc(FTAG);
3763 fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
3766 error = dmu_objset_snapshot_one(clone1name, strchr(snap2name, '@') + 1);
3767 if (error && error != EEXIST) {
3768 if (error == ENOSPC) {
3769 ztest_record_enospc(FTAG);
3772 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
3775 error = dmu_objset_snapshot_one(clone1name, strchr(snap3name, '@') + 1);
3776 if (error && error != EEXIST) {
3777 if (error == ENOSPC) {
3778 ztest_record_enospc(FTAG);
3781 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
3784 error = dmu_objset_clone(clone2name, snap3name);
3786 if (error == ENOSPC) {
3787 ztest_record_enospc(FTAG);
3790 fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
3793 error = dmu_objset_own(snap2name, DMU_OST_ANY, B_TRUE, FTAG, &os);
3795 fatal(0, "dmu_objset_own(%s) = %d", snap2name, error);
3796 error = dsl_dataset_promote(clone2name, NULL);
3797 if (error == ENOSPC) {
3798 dmu_objset_disown(os, FTAG);
3799 ztest_record_enospc(FTAG);
3803 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
3805 dmu_objset_disown(os, FTAG);
3808 ztest_dsl_dataset_cleanup(osname, id);
3810 rw_exit(&ztest_name_lock);
3814 * Verify that dmu_object_{alloc,free} work as expected.
3817 ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
3820 int batchsize = sizeof (od) / sizeof (od[0]);
3822 for (int b = 0; b < batchsize; b++) {
3823 ztest_od_init(&od[b], id, FTAG, b, DMU_OT_UINT64_OTHER,
3828 * Destroy the previous batch of objects, create a new batch,
3829 * and do some I/O on the new objects.
3831 if (ztest_object_init(zd, od, sizeof (od), B_TRUE) != 0)
3834 while (ztest_random(4 * batchsize) != 0)
3835 ztest_io(zd, od[ztest_random(batchsize)].od_object,
3836 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3840 * Rewind the global allocator to verify object allocation backfilling.
3843 ztest_dmu_object_next_chunk(ztest_ds_t *zd, uint64_t id)
3845 objset_t *os = zd->zd_os;
3846 int dnodes_per_chunk = 1 << dmu_object_alloc_chunk_shift;
3850 * Rewind the global allocator randomly back to a lower object number
3851 * to force backfilling and reclamation of recently freed dnodes.
3853 mutex_enter(&os->os_obj_lock);
3854 object = ztest_random(os->os_obj_next_chunk);
3855 os->os_obj_next_chunk = P2ALIGN(object, dnodes_per_chunk);
3856 mutex_exit(&os->os_obj_lock);
3860 * Verify that dmu_{read,write} work as expected.
3863 ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
3865 objset_t *os = zd->zd_os;
3868 int i, freeit, error;
3870 bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
3871 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3872 uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
3873 uint64_t regions = 997;
3874 uint64_t stride = 123456789ULL;
3875 uint64_t width = 40;
3876 int free_percent = 5;
3879 * This test uses two objects, packobj and bigobj, that are always
3880 * updated together (i.e. in the same tx) so that their contents are
3881 * in sync and can be compared. Their contents relate to each other
3882 * in a simple way: packobj is a dense array of 'bufwad' structures,
3883 * while bigobj is a sparse array of the same bufwads. Specifically,
3884 * for any index n, there are three bufwads that should be identical:
3886 * packobj, at offset n * sizeof (bufwad_t)
3887 * bigobj, at the head of the nth chunk
3888 * bigobj, at the tail of the nth chunk
3890 * The chunk size is arbitrary. It doesn't have to be a power of two,
3891 * and it doesn't have any relation to the object blocksize.
3892 * The only requirement is that it can hold at least two bufwads.
3894 * Normally, we write the bufwad to each of these locations.
3895 * However, free_percent of the time we instead write zeroes to
3896 * packobj and perform a dmu_free_range() on bigobj. By comparing
3897 * bigobj to packobj, we can verify that the DMU is correctly
3898 * tracking which parts of an object are allocated and free,
3899 * and that the contents of the allocated blocks are correct.
3903 * Read the directory info. If it's the first time, set things up.
3905 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0,
3907 ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, 0,
3910 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
3913 bigobj = od[0].od_object;
3914 packobj = od[1].od_object;
3915 chunksize = od[0].od_gen;
3916 ASSERT(chunksize == od[1].od_gen);
3919 * Prefetch a random chunk of the big object.
3920 * Our aim here is to get some async reads in flight
3921 * for blocks that we may free below; the DMU should
3922 * handle this race correctly.
3924 n = ztest_random(regions) * stride + ztest_random(width);
3925 s = 1 + ztest_random(2 * width - 1);
3926 dmu_prefetch(os, bigobj, 0, n * chunksize, s * chunksize,
3927 ZIO_PRIORITY_SYNC_READ);
3930 * Pick a random index and compute the offsets into packobj and bigobj.
3932 n = ztest_random(regions) * stride + ztest_random(width);
3933 s = 1 + ztest_random(width - 1);
3935 packoff = n * sizeof (bufwad_t);
3936 packsize = s * sizeof (bufwad_t);
3938 bigoff = n * chunksize;
3939 bigsize = s * chunksize;
3941 packbuf = umem_alloc(packsize, UMEM_NOFAIL);
3942 bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
3945 * free_percent of the time, free a range of bigobj rather than
3948 freeit = (ztest_random(100) < free_percent);
3951 * Read the current contents of our objects.
3953 error = dmu_read(os, packobj, packoff, packsize, packbuf,
3956 error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
3961 * Get a tx for the mods to both packobj and bigobj.
3963 tx = dmu_tx_create(os);
3965 dmu_tx_hold_write(tx, packobj, packoff, packsize);
3968 dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
3970 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
3972 /* This accounts for setting the checksum/compression. */
3973 dmu_tx_hold_bonus(tx, bigobj);
3975 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3977 umem_free(packbuf, packsize);
3978 umem_free(bigbuf, bigsize);
3982 enum zio_checksum cksum;
3984 cksum = (enum zio_checksum)
3985 ztest_random_dsl_prop(ZFS_PROP_CHECKSUM);
3986 } while (cksum >= ZIO_CHECKSUM_LEGACY_FUNCTIONS);
3987 dmu_object_set_checksum(os, bigobj, cksum, tx);
3989 enum zio_compress comp;
3991 comp = (enum zio_compress)
3992 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION);
3993 } while (comp >= ZIO_COMPRESS_LEGACY_FUNCTIONS);
3994 dmu_object_set_compress(os, bigobj, comp, tx);
3997 * For each index from n to n + s, verify that the existing bufwad
3998 * in packobj matches the bufwads at the head and tail of the
3999 * corresponding chunk in bigobj. Then update all three bufwads
4000 * with the new values we want to write out.
4002 for (i = 0; i < s; i++) {
4004 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
4006 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
4008 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
4010 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
4011 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
4013 if (pack->bw_txg > txg)
4014 fatal(0, "future leak: got %llx, open txg is %llx",
4017 if (pack->bw_data != 0 && pack->bw_index != n + i)
4018 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4019 pack->bw_index, n, i);
4021 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
4022 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
4024 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
4025 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
4028 bzero(pack, sizeof (bufwad_t));
4030 pack->bw_index = n + i;
4032 pack->bw_data = 1 + ztest_random(-2ULL);
4039 * We've verified all the old bufwads, and made new ones.
4040 * Now write them out.
4042 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
4045 if (ztest_opts.zo_verbose >= 7) {
4046 (void) printf("freeing offset %llx size %llx"
4048 (u_longlong_t)bigoff,
4049 (u_longlong_t)bigsize,
4052 VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
4054 if (ztest_opts.zo_verbose >= 7) {
4055 (void) printf("writing offset %llx size %llx"
4057 (u_longlong_t)bigoff,
4058 (u_longlong_t)bigsize,
4061 dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
4067 * Sanity check the stuff we just wrote.
4070 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
4071 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
4073 VERIFY(0 == dmu_read(os, packobj, packoff,
4074 packsize, packcheck, DMU_READ_PREFETCH));
4075 VERIFY(0 == dmu_read(os, bigobj, bigoff,
4076 bigsize, bigcheck, DMU_READ_PREFETCH));
4078 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
4079 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
4081 umem_free(packcheck, packsize);
4082 umem_free(bigcheck, bigsize);
4085 umem_free(packbuf, packsize);
4086 umem_free(bigbuf, bigsize);
4090 compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
4091 uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
4099 * For each index from n to n + s, verify that the existing bufwad
4100 * in packobj matches the bufwads at the head and tail of the
4101 * corresponding chunk in bigobj. Then update all three bufwads
4102 * with the new values we want to write out.
4104 for (i = 0; i < s; i++) {
4106 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
4108 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
4110 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
4112 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
4113 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
4115 if (pack->bw_txg > txg)
4116 fatal(0, "future leak: got %llx, open txg is %llx",
4119 if (pack->bw_data != 0 && pack->bw_index != n + i)
4120 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4121 pack->bw_index, n, i);
4123 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
4124 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
4126 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
4127 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
4129 pack->bw_index = n + i;
4131 pack->bw_data = 1 + ztest_random(-2ULL);
4139 ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
4141 objset_t *os = zd->zd_os;
4147 bufwad_t *packbuf, *bigbuf;
4148 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
4149 uint64_t blocksize = ztest_random_blocksize();
4150 uint64_t chunksize = blocksize;
4151 uint64_t regions = 997;
4152 uint64_t stride = 123456789ULL;
4154 dmu_buf_t *bonus_db;
4155 arc_buf_t **bigbuf_arcbufs;
4156 dmu_object_info_t doi;
4159 * This test uses two objects, packobj and bigobj, that are always
4160 * updated together (i.e. in the same tx) so that their contents are
4161 * in sync and can be compared. Their contents relate to each other
4162 * in a simple way: packobj is a dense array of 'bufwad' structures,
4163 * while bigobj is a sparse array of the same bufwads. Specifically,
4164 * for any index n, there are three bufwads that should be identical:
4166 * packobj, at offset n * sizeof (bufwad_t)
4167 * bigobj, at the head of the nth chunk
4168 * bigobj, at the tail of the nth chunk
4170 * The chunk size is set equal to bigobj block size so that
4171 * dmu_assign_arcbuf() can be tested for object updates.
4175 * Read the directory info. If it's the first time, set things up.
4177 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize,
4179 ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, 0,
4182 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4185 bigobj = od[0].od_object;
4186 packobj = od[1].od_object;
4187 blocksize = od[0].od_blocksize;
4188 chunksize = blocksize;
4189 ASSERT(chunksize == od[1].od_gen);
4191 VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
4192 VERIFY(ISP2(doi.doi_data_block_size));
4193 VERIFY(chunksize == doi.doi_data_block_size);
4194 VERIFY(chunksize >= 2 * sizeof (bufwad_t));
4197 * Pick a random index and compute the offsets into packobj and bigobj.
4199 n = ztest_random(regions) * stride + ztest_random(width);
4200 s = 1 + ztest_random(width - 1);
4202 packoff = n * sizeof (bufwad_t);
4203 packsize = s * sizeof (bufwad_t);
4205 bigoff = n * chunksize;
4206 bigsize = s * chunksize;
4208 packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
4209 bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
4211 VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
4213 bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
4216 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
4217 * Iteration 1 test zcopy to already referenced dbufs.
4218 * Iteration 2 test zcopy to dirty dbuf in the same txg.
4219 * Iteration 3 test zcopy to dbuf dirty in previous txg.
4220 * Iteration 4 test zcopy when dbuf is no longer dirty.
4221 * Iteration 5 test zcopy when it can't be done.
4222 * Iteration 6 one more zcopy write.
4224 for (i = 0; i < 7; i++) {
4229 * In iteration 5 (i == 5) use arcbufs
4230 * that don't match bigobj blksz to test
4231 * dmu_assign_arcbuf() when it can't directly
4232 * assign an arcbuf to a dbuf.
4234 for (j = 0; j < s; j++) {
4237 dmu_request_arcbuf(bonus_db, chunksize);
4239 bigbuf_arcbufs[2 * j] =
4240 dmu_request_arcbuf(bonus_db, chunksize / 2);
4241 bigbuf_arcbufs[2 * j + 1] =
4242 dmu_request_arcbuf(bonus_db, chunksize / 2);
4247 * Get a tx for the mods to both packobj and bigobj.
4249 tx = dmu_tx_create(os);
4251 dmu_tx_hold_write(tx, packobj, packoff, packsize);
4252 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
4254 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4256 umem_free(packbuf, packsize);
4257 umem_free(bigbuf, bigsize);
4258 for (j = 0; j < s; j++) {
4260 dmu_return_arcbuf(bigbuf_arcbufs[j]);
4263 bigbuf_arcbufs[2 * j]);
4265 bigbuf_arcbufs[2 * j + 1]);
4268 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4269 dmu_buf_rele(bonus_db, FTAG);
4274 * 50% of the time don't read objects in the 1st iteration to
4275 * test dmu_assign_arcbuf() for the case when there're no
4276 * existing dbufs for the specified offsets.
4278 if (i != 0 || ztest_random(2) != 0) {
4279 error = dmu_read(os, packobj, packoff,
4280 packsize, packbuf, DMU_READ_PREFETCH);
4282 error = dmu_read(os, bigobj, bigoff, bigsize,
4283 bigbuf, DMU_READ_PREFETCH);
4286 compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
4290 * We've verified all the old bufwads, and made new ones.
4291 * Now write them out.
4293 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
4294 if (ztest_opts.zo_verbose >= 7) {
4295 (void) printf("writing offset %llx size %llx"
4297 (u_longlong_t)bigoff,
4298 (u_longlong_t)bigsize,
4301 for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
4304 bcopy((caddr_t)bigbuf + (off - bigoff),
4305 bigbuf_arcbufs[j]->b_data, chunksize);
4307 bcopy((caddr_t)bigbuf + (off - bigoff),
4308 bigbuf_arcbufs[2 * j]->b_data,
4310 bcopy((caddr_t)bigbuf + (off - bigoff) +
4312 bigbuf_arcbufs[2 * j + 1]->b_data,
4317 VERIFY(dmu_buf_hold(os, bigobj, off,
4318 FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0);
4321 dmu_assign_arcbuf(bonus_db, off,
4322 bigbuf_arcbufs[j], tx);
4324 dmu_assign_arcbuf(bonus_db, off,
4325 bigbuf_arcbufs[2 * j], tx);
4326 dmu_assign_arcbuf(bonus_db,
4327 off + chunksize / 2,
4328 bigbuf_arcbufs[2 * j + 1], tx);
4331 dmu_buf_rele(dbt, FTAG);
4337 * Sanity check the stuff we just wrote.
4340 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
4341 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
4343 VERIFY(0 == dmu_read(os, packobj, packoff,
4344 packsize, packcheck, DMU_READ_PREFETCH));
4345 VERIFY(0 == dmu_read(os, bigobj, bigoff,
4346 bigsize, bigcheck, DMU_READ_PREFETCH));
4348 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
4349 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
4351 umem_free(packcheck, packsize);
4352 umem_free(bigcheck, bigsize);
4355 txg_wait_open(dmu_objset_pool(os), 0);
4356 } else if (i == 3) {
4357 txg_wait_synced(dmu_objset_pool(os), 0);
4361 dmu_buf_rele(bonus_db, FTAG);
4362 umem_free(packbuf, packsize);
4363 umem_free(bigbuf, bigsize);
4364 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4369 ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
4372 uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
4373 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4376 * Have multiple threads write to large offsets in an object
4377 * to verify that parallel writes to an object -- even to the
4378 * same blocks within the object -- doesn't cause any trouble.
4380 ztest_od_init(&od[0], ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER,
4383 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4386 while (ztest_random(10) != 0)
4387 ztest_io(zd, od[0].od_object, offset);
4391 ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
4394 uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
4395 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4396 uint64_t count = ztest_random(20) + 1;
4397 uint64_t blocksize = ztest_random_blocksize();
4400 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize,
4403 if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
4406 if (ztest_truncate(zd, od[0].od_object, offset, count * blocksize) != 0)
4409 ztest_prealloc(zd, od[0].od_object, offset, count * blocksize);
4411 data = umem_zalloc(blocksize, UMEM_NOFAIL);
4413 while (ztest_random(count) != 0) {
4414 uint64_t randoff = offset + (ztest_random(count) * blocksize);
4415 if (ztest_write(zd, od[0].od_object, randoff, blocksize,
4418 while (ztest_random(4) != 0)
4419 ztest_io(zd, od[0].od_object, randoff);
4422 umem_free(data, blocksize);
4426 * Verify that zap_{create,destroy,add,remove,update} work as expected.
4428 #define ZTEST_ZAP_MIN_INTS 1
4429 #define ZTEST_ZAP_MAX_INTS 4
4430 #define ZTEST_ZAP_MAX_PROPS 1000
4433 ztest_zap(ztest_ds_t *zd, uint64_t id)
4435 objset_t *os = zd->zd_os;
4438 uint64_t txg, last_txg;
4439 uint64_t value[ZTEST_ZAP_MAX_INTS];
4440 uint64_t zl_ints, zl_intsize, prop;
4443 char propname[100], txgname[100];
4445 char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
4447 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0, 0);
4449 if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
4452 object = od[0].od_object;
4455 * Generate a known hash collision, and verify that
4456 * we can lookup and remove both entries.
4458 tx = dmu_tx_create(os);
4459 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4460 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4463 for (i = 0; i < 2; i++) {
4465 VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
4468 for (i = 0; i < 2; i++) {
4469 VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
4470 sizeof (uint64_t), 1, &value[i], tx));
4472 zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
4473 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4474 ASSERT3U(zl_ints, ==, 1);
4476 for (i = 0; i < 2; i++) {
4477 VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
4482 * Generate a buch of random entries.
4484 ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
4486 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4487 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4488 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4489 bzero(value, sizeof (value));
4493 * If these zap entries already exist, validate their contents.
4495 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4497 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4498 ASSERT3U(zl_ints, ==, 1);
4500 VERIFY(zap_lookup(os, object, txgname, zl_intsize,
4501 zl_ints, &last_txg) == 0);
4503 VERIFY(zap_length(os, object, propname, &zl_intsize,
4506 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4507 ASSERT3U(zl_ints, ==, ints);
4509 VERIFY(zap_lookup(os, object, propname, zl_intsize,
4510 zl_ints, value) == 0);
4512 for (i = 0; i < ints; i++) {
4513 ASSERT3U(value[i], ==, last_txg + object + i);
4516 ASSERT3U(error, ==, ENOENT);
4520 * Atomically update two entries in our zap object.
4521 * The first is named txg_%llu, and contains the txg
4522 * in which the property was last updated. The second
4523 * is named prop_%llu, and the nth element of its value
4524 * should be txg + object + n.
4526 tx = dmu_tx_create(os);
4527 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4528 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4533 fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
4535 for (i = 0; i < ints; i++)
4536 value[i] = txg + object + i;
4538 VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
4540 VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
4546 * Remove a random pair of entries.
4548 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4549 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4550 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4552 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4554 if (error == ENOENT)
4559 tx = dmu_tx_create(os);
4560 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4561 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4564 VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
4565 VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
4570 * Testcase to test the upgrading of a microzap to fatzap.
4573 ztest_fzap(ztest_ds_t *zd, uint64_t id)
4575 objset_t *os = zd->zd_os;
4577 uint64_t object, txg;
4579 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0, 0);
4581 if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
4584 object = od[0].od_object;
4587 * Add entries to this ZAP and make sure it spills over
4588 * and gets upgraded to a fatzap. Also, since we are adding
4589 * 2050 entries we should see ptrtbl growth and leaf-block split.
4591 for (int i = 0; i < 2050; i++) {
4592 char name[ZFS_MAX_DATASET_NAME_LEN];
4597 (void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
4600 tx = dmu_tx_create(os);
4601 dmu_tx_hold_zap(tx, object, B_TRUE, name);
4602 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4605 error = zap_add(os, object, name, sizeof (uint64_t), 1,
4607 ASSERT(error == 0 || error == EEXIST);
4614 ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
4616 objset_t *os = zd->zd_os;
4618 uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
4620 int i, namelen, error;
4621 int micro = ztest_random(2);
4622 char name[20], string_value[20];
4625 ztest_od_init(&od[0], ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER,
4628 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4631 object = od[0].od_object;
4634 * Generate a random name of the form 'xxx.....' where each
4635 * x is a random printable character and the dots are dots.
4636 * There are 94 such characters, and the name length goes from
4637 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
4639 namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
4641 for (i = 0; i < 3; i++)
4642 name[i] = '!' + ztest_random('~' - '!' + 1);
4643 for (; i < namelen - 1; i++)
4647 if ((namelen & 1) || micro) {
4648 wsize = sizeof (txg);
4654 data = string_value;
4658 VERIFY0(zap_count(os, object, &count));
4659 ASSERT(count != -1ULL);
4662 * Select an operation: length, lookup, add, update, remove.
4664 i = ztest_random(5);
4667 tx = dmu_tx_create(os);
4668 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4669 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4672 bcopy(name, string_value, namelen);
4676 bzero(string_value, namelen);
4682 error = zap_length(os, object, name, &zl_wsize, &zl_wc);
4684 ASSERT3U(wsize, ==, zl_wsize);
4685 ASSERT3U(wc, ==, zl_wc);
4687 ASSERT3U(error, ==, ENOENT);
4692 error = zap_lookup(os, object, name, wsize, wc, data);
4694 if (data == string_value &&
4695 bcmp(name, data, namelen) != 0)
4696 fatal(0, "name '%s' != val '%s' len %d",
4697 name, data, namelen);
4699 ASSERT3U(error, ==, ENOENT);
4704 error = zap_add(os, object, name, wsize, wc, data, tx);
4705 ASSERT(error == 0 || error == EEXIST);
4709 VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
4713 error = zap_remove(os, object, name, tx);
4714 ASSERT(error == 0 || error == ENOENT);
4723 * Commit callback data.
4725 typedef struct ztest_cb_data {
4726 list_node_t zcd_node;
4728 int zcd_expected_err;
4729 boolean_t zcd_added;
4730 boolean_t zcd_called;
4734 /* This is the actual commit callback function */
4736 ztest_commit_callback(void *arg, int error)
4738 ztest_cb_data_t *data = arg;
4739 uint64_t synced_txg;
4741 VERIFY(data != NULL);
4742 VERIFY3S(data->zcd_expected_err, ==, error);
4743 VERIFY(!data->zcd_called);
4745 synced_txg = spa_last_synced_txg(data->zcd_spa);
4746 if (data->zcd_txg > synced_txg)
4747 fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
4748 ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
4751 data->zcd_called = B_TRUE;
4753 if (error == ECANCELED) {
4754 ASSERT0(data->zcd_txg);
4755 ASSERT(!data->zcd_added);
4758 * The private callback data should be destroyed here, but
4759 * since we are going to check the zcd_called field after
4760 * dmu_tx_abort(), we will destroy it there.
4765 /* Was this callback added to the global callback list? */
4766 if (!data->zcd_added)
4769 ASSERT3U(data->zcd_txg, !=, 0);
4771 /* Remove our callback from the list */
4772 mutex_enter(&zcl.zcl_callbacks_lock);
4773 list_remove(&zcl.zcl_callbacks, data);
4774 mutex_exit(&zcl.zcl_callbacks_lock);
4777 umem_free(data, sizeof (ztest_cb_data_t));
4780 /* Allocate and initialize callback data structure */
4781 static ztest_cb_data_t *
4782 ztest_create_cb_data(objset_t *os, uint64_t txg)
4784 ztest_cb_data_t *cb_data;
4786 cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
4788 cb_data->zcd_txg = txg;
4789 cb_data->zcd_spa = dmu_objset_spa(os);
4795 * If a number of txgs equal to this threshold have been created after a commit
4796 * callback has been registered but not called, then we assume there is an
4797 * implementation bug.
4799 #define ZTEST_COMMIT_CALLBACK_THRESH (TXG_CONCURRENT_STATES + 2)
4802 * Commit callback test.
4805 ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
4807 objset_t *os = zd->zd_os;
4810 ztest_cb_data_t *cb_data[3], *tmp_cb;
4811 uint64_t old_txg, txg;
4814 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
4816 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4819 tx = dmu_tx_create(os);
4821 cb_data[0] = ztest_create_cb_data(os, 0);
4822 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
4824 dmu_tx_hold_write(tx, od[0].od_object, 0, sizeof (uint64_t));
4826 /* Every once in a while, abort the transaction on purpose */
4827 if (ztest_random(100) == 0)
4831 error = dmu_tx_assign(tx, TXG_NOWAIT);
4833 txg = error ? 0 : dmu_tx_get_txg(tx);
4835 cb_data[0]->zcd_txg = txg;
4836 cb_data[1] = ztest_create_cb_data(os, txg);
4837 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
4841 * It's not a strict requirement to call the registered
4842 * callbacks from inside dmu_tx_abort(), but that's what
4843 * it's supposed to happen in the current implementation
4844 * so we will check for that.
4846 for (i = 0; i < 2; i++) {
4847 cb_data[i]->zcd_expected_err = ECANCELED;
4848 VERIFY(!cb_data[i]->zcd_called);
4853 for (i = 0; i < 2; i++) {
4854 VERIFY(cb_data[i]->zcd_called);
4855 umem_free(cb_data[i], sizeof (ztest_cb_data_t));
4861 cb_data[2] = ztest_create_cb_data(os, txg);
4862 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
4865 * Read existing data to make sure there isn't a future leak.
4867 VERIFY(0 == dmu_read(os, od[0].od_object, 0, sizeof (uint64_t),
4868 &old_txg, DMU_READ_PREFETCH));
4871 fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
4874 dmu_write(os, od[0].od_object, 0, sizeof (uint64_t), &txg, tx);
4876 mutex_enter(&zcl.zcl_callbacks_lock);
4879 * Since commit callbacks don't have any ordering requirement and since
4880 * it is theoretically possible for a commit callback to be called
4881 * after an arbitrary amount of time has elapsed since its txg has been
4882 * synced, it is difficult to reliably determine whether a commit
4883 * callback hasn't been called due to high load or due to a flawed
4886 * In practice, we will assume that if after a certain number of txgs a
4887 * commit callback hasn't been called, then most likely there's an
4888 * implementation bug..
4890 tmp_cb = list_head(&zcl.zcl_callbacks);
4891 if (tmp_cb != NULL &&
4892 (txg - ZTEST_COMMIT_CALLBACK_THRESH) > tmp_cb->zcd_txg) {
4893 fatal(0, "Commit callback threshold exceeded, oldest txg: %"
4894 PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
4898 * Let's find the place to insert our callbacks.
4900 * Even though the list is ordered by txg, it is possible for the
4901 * insertion point to not be the end because our txg may already be
4902 * quiescing at this point and other callbacks in the open txg
4903 * (from other objsets) may have sneaked in.
4905 tmp_cb = list_tail(&zcl.zcl_callbacks);
4906 while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
4907 tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
4909 /* Add the 3 callbacks to the list */
4910 for (i = 0; i < 3; i++) {
4912 list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
4914 list_insert_after(&zcl.zcl_callbacks, tmp_cb,
4917 cb_data[i]->zcd_added = B_TRUE;
4918 VERIFY(!cb_data[i]->zcd_called);
4920 tmp_cb = cb_data[i];
4923 mutex_exit(&zcl.zcl_callbacks_lock);
4929 * Visit each object in the dataset. Verify that its properties
4930 * are consistent what was stored in the block tag when it was created,
4931 * and that its unused bonus buffer space has not been overwritten.
4934 ztest_verify_dnode_bt(ztest_ds_t *zd, uint64_t id)
4936 objset_t *os = zd->zd_os;
4940 for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
4941 ztest_block_tag_t *bt = NULL;
4942 dmu_object_info_t doi;
4945 if (dmu_bonus_hold(os, obj, FTAG, &db) != 0)
4948 dmu_object_info_from_db(db, &doi);
4949 if (doi.doi_bonus_size >= sizeof (*bt))
4950 bt = ztest_bt_bonus(db);
4952 if (bt && bt->bt_magic == BT_MAGIC) {
4953 ztest_bt_verify(bt, os, obj, doi.doi_dnodesize,
4954 bt->bt_offset, bt->bt_gen, bt->bt_txg,
4956 ztest_verify_unused_bonus(db, bt, obj, os, bt->bt_gen);
4959 dmu_buf_rele(db, FTAG);
4965 ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
4967 zfs_prop_t proplist[] = {
4969 ZFS_PROP_COMPRESSION,
4974 rw_enter(&ztest_name_lock, RW_READER);
4976 for (int p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
4977 (void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
4978 ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
4980 rw_exit(&ztest_name_lock);
4985 ztest_remap_blocks(ztest_ds_t *zd, uint64_t id)
4987 rw_enter(&ztest_name_lock, RW_READER);
4989 int error = dmu_objset_remap_indirects(zd->zd_name);
4990 if (error == ENOSPC)
4994 rw_exit(&ztest_name_lock);
4999 ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
5001 nvlist_t *props = NULL;
5003 rw_enter(&ztest_name_lock, RW_READER);
5005 (void) ztest_spa_prop_set_uint64(ZPOOL_PROP_DEDUPDITTO,
5006 ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
5008 VERIFY0(spa_prop_get(ztest_spa, &props));
5010 if (ztest_opts.zo_verbose >= 6)
5011 dump_nvlist(props, 4);
5015 rw_exit(&ztest_name_lock);
5019 user_release_one(const char *snapname, const char *holdname)
5021 nvlist_t *snaps, *holds;
5024 snaps = fnvlist_alloc();
5025 holds = fnvlist_alloc();
5026 fnvlist_add_boolean(holds, holdname);
5027 fnvlist_add_nvlist(snaps, snapname, holds);
5028 fnvlist_free(holds);
5029 error = dsl_dataset_user_release(snaps, NULL);
5030 fnvlist_free(snaps);
5035 * Test snapshot hold/release and deferred destroy.
5038 ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
5041 objset_t *os = zd->zd_os;
5045 char clonename[100];
5047 char osname[ZFS_MAX_DATASET_NAME_LEN];
5050 rw_enter(&ztest_name_lock, RW_READER);
5052 dmu_objset_name(os, osname);
5054 (void) snprintf(snapname, sizeof (snapname), "sh1_%llu", id);
5055 (void) snprintf(fullname, sizeof (fullname), "%s@%s", osname, snapname);
5056 (void) snprintf(clonename, sizeof (clonename),
5057 "%s/ch1_%llu", osname, id);
5058 (void) snprintf(tag, sizeof (tag), "tag_%llu", id);
5061 * Clean up from any previous run.
5063 error = dsl_destroy_head(clonename);
5064 if (error != ENOENT)
5066 error = user_release_one(fullname, tag);
5067 if (error != ESRCH && error != ENOENT)
5069 error = dsl_destroy_snapshot(fullname, B_FALSE);
5070 if (error != ENOENT)
5074 * Create snapshot, clone it, mark snap for deferred destroy,
5075 * destroy clone, verify snap was also destroyed.
5077 error = dmu_objset_snapshot_one(osname, snapname);
5079 if (error == ENOSPC) {
5080 ztest_record_enospc("dmu_objset_snapshot");
5083 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
5086 error = dmu_objset_clone(clonename, fullname);
5088 if (error == ENOSPC) {
5089 ztest_record_enospc("dmu_objset_clone");
5092 fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
5095 error = dsl_destroy_snapshot(fullname, B_TRUE);
5097 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5101 error = dsl_destroy_head(clonename);
5103 fatal(0, "dsl_destroy_head(%s) = %d", clonename, error);
5105 error = dmu_objset_hold(fullname, FTAG, &origin);
5106 if (error != ENOENT)
5107 fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
5110 * Create snapshot, add temporary hold, verify that we can't
5111 * destroy a held snapshot, mark for deferred destroy,
5112 * release hold, verify snapshot was destroyed.
5114 error = dmu_objset_snapshot_one(osname, snapname);
5116 if (error == ENOSPC) {
5117 ztest_record_enospc("dmu_objset_snapshot");
5120 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
5123 holds = fnvlist_alloc();
5124 fnvlist_add_string(holds, fullname, tag);
5125 error = dsl_dataset_user_hold(holds, 0, NULL);
5126 fnvlist_free(holds);
5128 if (error == ENOSPC) {
5129 ztest_record_enospc("dsl_dataset_user_hold");
5132 fatal(0, "dsl_dataset_user_hold(%s, %s) = %u",
5133 fullname, tag, error);
5136 error = dsl_destroy_snapshot(fullname, B_FALSE);
5137 if (error != EBUSY) {
5138 fatal(0, "dsl_destroy_snapshot(%s, B_FALSE) = %d",
5142 error = dsl_destroy_snapshot(fullname, B_TRUE);
5144 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5148 error = user_release_one(fullname, tag);
5150 fatal(0, "user_release_one(%s, %s) = %d", fullname, tag, error);
5152 VERIFY3U(dmu_objset_hold(fullname, FTAG, &origin), ==, ENOENT);
5155 rw_exit(&ztest_name_lock);
5159 * Inject random faults into the on-disk data.
5163 ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
5165 ztest_shared_t *zs = ztest_shared;
5166 spa_t *spa = ztest_spa;
5170 uint64_t bad = 0x1990c0ffeedecadeULL;
5172 char path0[MAXPATHLEN];
5173 char pathrand[MAXPATHLEN];
5175 int bshift = SPA_MAXBLOCKSHIFT + 2;
5181 boolean_t islog = B_FALSE;
5183 mutex_enter(&ztest_vdev_lock);
5186 * Device removal is in progress, fault injection must be disabled
5187 * until it completes and the pool is scrubbed. The fault injection
5188 * strategy for damaging blocks does not take in to account evacuated
5189 * blocks which may have already been damaged.
5191 if (ztest_device_removal_active) {
5192 mutex_exit(&ztest_vdev_lock);
5196 maxfaults = MAXFAULTS();
5197 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
5198 mirror_save = zs->zs_mirrors;
5199 mutex_exit(&ztest_vdev_lock);
5201 ASSERT(leaves >= 1);
5204 * Grab the name lock as reader. There are some operations
5205 * which don't like to have their vdevs changed while
5206 * they are in progress (i.e. spa_change_guid). Those
5207 * operations will have grabbed the name lock as writer.
5209 rw_enter(&ztest_name_lock, RW_READER);
5212 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
5214 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5216 if (ztest_random(2) == 0) {
5218 * Inject errors on a normal data device or slog device.
5220 top = ztest_random_vdev_top(spa, B_TRUE);
5221 leaf = ztest_random(leaves) + zs->zs_splits;
5224 * Generate paths to the first leaf in this top-level vdev,
5225 * and to the random leaf we selected. We'll induce transient
5226 * write failures and random online/offline activity on leaf 0,
5227 * and we'll write random garbage to the randomly chosen leaf.
5229 (void) snprintf(path0, sizeof (path0), ztest_dev_template,
5230 ztest_opts.zo_dir, ztest_opts.zo_pool,
5231 top * leaves + zs->zs_splits);
5232 (void) snprintf(pathrand, sizeof (pathrand), ztest_dev_template,
5233 ztest_opts.zo_dir, ztest_opts.zo_pool,
5234 top * leaves + leaf);
5236 vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
5237 if (vd0 != NULL && vd0->vdev_top->vdev_islog)
5241 * If the top-level vdev needs to be resilvered
5242 * then we only allow faults on the device that is
5245 if (vd0 != NULL && maxfaults != 1 &&
5246 (!vdev_resilver_needed(vd0->vdev_top, NULL, NULL) ||
5247 vd0->vdev_resilver_txg != 0)) {
5249 * Make vd0 explicitly claim to be unreadable,
5250 * or unwriteable, or reach behind its back
5251 * and close the underlying fd. We can do this if
5252 * maxfaults == 0 because we'll fail and reexecute,
5253 * and we can do it if maxfaults >= 2 because we'll
5254 * have enough redundancy. If maxfaults == 1, the
5255 * combination of this with injection of random data
5256 * corruption below exceeds the pool's fault tolerance.
5258 vdev_file_t *vf = vd0->vdev_tsd;
5260 zfs_dbgmsg("injecting fault to vdev %llu; maxfaults=%d",
5261 (long long)vd0->vdev_id, (int)maxfaults);
5263 if (vf != NULL && ztest_random(3) == 0) {
5264 (void) close(vf->vf_vnode->v_fd);
5265 vf->vf_vnode->v_fd = -1;
5266 } else if (ztest_random(2) == 0) {
5267 vd0->vdev_cant_read = B_TRUE;
5269 vd0->vdev_cant_write = B_TRUE;
5271 guid0 = vd0->vdev_guid;
5275 * Inject errors on an l2cache device.
5277 spa_aux_vdev_t *sav = &spa->spa_l2cache;
5279 if (sav->sav_count == 0) {
5280 spa_config_exit(spa, SCL_STATE, FTAG);
5281 rw_exit(&ztest_name_lock);
5284 vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
5285 guid0 = vd0->vdev_guid;
5286 (void) strcpy(path0, vd0->vdev_path);
5287 (void) strcpy(pathrand, vd0->vdev_path);
5291 maxfaults = INT_MAX; /* no limit on cache devices */
5294 spa_config_exit(spa, SCL_STATE, FTAG);
5295 rw_exit(&ztest_name_lock);
5298 * If we can tolerate two or more faults, or we're dealing
5299 * with a slog, randomly online/offline vd0.
5301 if ((maxfaults >= 2 || islog) && guid0 != 0) {
5302 if (ztest_random(10) < 6) {
5303 int flags = (ztest_random(2) == 0 ?
5304 ZFS_OFFLINE_TEMPORARY : 0);
5307 * We have to grab the zs_name_lock as writer to
5308 * prevent a race between offlining a slog and
5309 * destroying a dataset. Offlining the slog will
5310 * grab a reference on the dataset which may cause
5311 * dmu_objset_destroy() to fail with EBUSY thus
5312 * leaving the dataset in an inconsistent state.
5315 rw_enter(&ztest_name_lock, RW_WRITER);
5317 VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
5320 rw_exit(&ztest_name_lock);
5323 * Ideally we would like to be able to randomly
5324 * call vdev_[on|off]line without holding locks
5325 * to force unpredictable failures but the side
5326 * effects of vdev_[on|off]line prevent us from
5327 * doing so. We grab the ztest_vdev_lock here to
5328 * prevent a race between injection testing and
5331 mutex_enter(&ztest_vdev_lock);
5332 (void) vdev_online(spa, guid0, 0, NULL);
5333 mutex_exit(&ztest_vdev_lock);
5341 * We have at least single-fault tolerance, so inject data corruption.
5343 fd = open(pathrand, O_RDWR);
5345 if (fd == -1) /* we hit a gap in the device namespace */
5348 fsize = lseek(fd, 0, SEEK_END);
5350 while (--iters != 0) {
5352 * The offset must be chosen carefully to ensure that
5353 * we do not inject a given logical block with errors
5354 * on two different leaf devices, because ZFS can not
5355 * tolerate that (if maxfaults==1).
5357 * We divide each leaf into chunks of size
5358 * (# leaves * SPA_MAXBLOCKSIZE * 4). Within each chunk
5359 * there is a series of ranges to which we can inject errors.
5360 * Each range can accept errors on only a single leaf vdev.
5361 * The error injection ranges are separated by ranges
5362 * which we will not inject errors on any device (DMZs).
5363 * Each DMZ must be large enough such that a single block
5364 * can not straddle it, so that a single block can not be
5365 * a target in two different injection ranges (on different
5368 * For example, with 3 leaves, each chunk looks like:
5369 * 0 to 32M: injection range for leaf 0
5370 * 32M to 64M: DMZ - no injection allowed
5371 * 64M to 96M: injection range for leaf 1
5372 * 96M to 128M: DMZ - no injection allowed
5373 * 128M to 160M: injection range for leaf 2
5374 * 160M to 192M: DMZ - no injection allowed
5376 offset = ztest_random(fsize / (leaves << bshift)) *
5377 (leaves << bshift) + (leaf << bshift) +
5378 (ztest_random(1ULL << (bshift - 1)) & -8ULL);
5381 * Only allow damage to the labels at one end of the vdev.
5383 * If all labels are damaged, the device will be totally
5384 * inaccessible, which will result in loss of data,
5385 * because we also damage (parts of) the other side of
5388 * Additionally, we will always have both an even and an
5389 * odd label, so that we can handle crashes in the
5390 * middle of vdev_config_sync().
5392 if ((leaf & 1) == 0 && offset < VDEV_LABEL_START_SIZE)
5396 * The two end labels are stored at the "end" of the disk, but
5397 * the end of the disk (vdev_psize) is aligned to
5398 * sizeof (vdev_label_t).
5400 uint64_t psize = P2ALIGN(fsize, sizeof (vdev_label_t));
5401 if ((leaf & 1) == 1 &&
5402 offset + sizeof (bad) > psize - VDEV_LABEL_END_SIZE)
5405 mutex_enter(&ztest_vdev_lock);
5406 if (mirror_save != zs->zs_mirrors) {
5407 mutex_exit(&ztest_vdev_lock);
5412 if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
5413 fatal(1, "can't inject bad word at 0x%llx in %s",
5416 mutex_exit(&ztest_vdev_lock);
5418 if (ztest_opts.zo_verbose >= 7)
5419 (void) printf("injected bad word into %s,"
5420 " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
5427 * Verify that DDT repair works as expected.
5430 ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
5432 ztest_shared_t *zs = ztest_shared;
5433 spa_t *spa = ztest_spa;
5434 objset_t *os = zd->zd_os;
5436 uint64_t object, blocksize, txg, pattern, psize;
5437 enum zio_checksum checksum = spa_dedup_checksum(spa);
5442 int copies = 2 * ZIO_DEDUPDITTO_MIN;
5444 blocksize = ztest_random_blocksize();
5445 blocksize = MIN(blocksize, 2048); /* because we write so many */
5447 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize,
5450 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
5454 * Take the name lock as writer to prevent anyone else from changing
5455 * the pool and dataset properies we need to maintain during this test.
5457 rw_enter(&ztest_name_lock, RW_WRITER);
5459 if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
5461 ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
5463 rw_exit(&ztest_name_lock);
5467 dmu_objset_stats_t dds;
5468 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
5469 dmu_objset_fast_stat(os, &dds);
5470 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
5472 object = od[0].od_object;
5473 blocksize = od[0].od_blocksize;
5474 pattern = zs->zs_guid ^ dds.dds_guid;
5476 ASSERT(object != 0);
5478 tx = dmu_tx_create(os);
5479 dmu_tx_hold_write(tx, object, 0, copies * blocksize);
5480 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
5482 rw_exit(&ztest_name_lock);
5487 * Write all the copies of our block.
5489 for (int i = 0; i < copies; i++) {
5490 uint64_t offset = i * blocksize;
5491 int error = dmu_buf_hold(os, object, offset, FTAG, &db,
5492 DMU_READ_NO_PREFETCH);
5494 fatal(B_FALSE, "dmu_buf_hold(%p, %llu, %llu) = %u",
5495 os, (long long)object, (long long) offset, error);
5497 ASSERT(db->db_offset == offset);
5498 ASSERT(db->db_size == blocksize);
5499 ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
5500 ztest_pattern_match(db->db_data, db->db_size, 0ULL));
5501 dmu_buf_will_fill(db, tx);
5502 ztest_pattern_set(db->db_data, db->db_size, pattern);
5503 dmu_buf_rele(db, FTAG);
5507 txg_wait_synced(spa_get_dsl(spa), txg);
5510 * Find out what block we got.
5512 VERIFY0(dmu_buf_hold(os, object, 0, FTAG, &db,
5513 DMU_READ_NO_PREFETCH));
5514 blk = *((dmu_buf_impl_t *)db)->db_blkptr;
5515 dmu_buf_rele(db, FTAG);
5518 * Damage the block. Dedup-ditto will save us when we read it later.
5520 psize = BP_GET_PSIZE(&blk);
5521 abd = abd_alloc_linear(psize, B_TRUE);
5522 ztest_pattern_set(abd_to_buf(abd), psize, ~pattern);
5524 (void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
5525 abd, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
5526 ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
5530 rw_exit(&ztest_name_lock);
5538 ztest_scrub(ztest_ds_t *zd, uint64_t id)
5540 spa_t *spa = ztest_spa;
5543 * Scrub in progress by device removal.
5545 if (ztest_device_removal_active)
5548 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5549 (void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
5550 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5554 * Change the guid for the pool.
5558 ztest_reguid(ztest_ds_t *zd, uint64_t id)
5560 spa_t *spa = ztest_spa;
5561 uint64_t orig, load;
5564 orig = spa_guid(spa);
5565 load = spa_load_guid(spa);
5567 rw_enter(&ztest_name_lock, RW_WRITER);
5568 error = spa_change_guid(spa);
5569 rw_exit(&ztest_name_lock);
5574 if (ztest_opts.zo_verbose >= 4) {
5575 (void) printf("Changed guid old %llu -> %llu\n",
5576 (u_longlong_t)orig, (u_longlong_t)spa_guid(spa));
5579 VERIFY3U(orig, !=, spa_guid(spa));
5580 VERIFY3U(load, ==, spa_load_guid(spa));
5584 ztest_random_concrete_vdev_leaf(vdev_t *vd)
5589 if (vd->vdev_children == 0)
5592 vdev_t *eligible[vd->vdev_children];
5593 int eligible_idx = 0, i;
5594 for (i = 0; i < vd->vdev_children; i++) {
5595 vdev_t *cvd = vd->vdev_child[i];
5596 if (cvd->vdev_top->vdev_removing)
5598 if (cvd->vdev_children > 0 ||
5599 (vdev_is_concrete(cvd) && !cvd->vdev_detached)) {
5600 eligible[eligible_idx++] = cvd;
5603 VERIFY(eligible_idx > 0);
5605 uint64_t child_no = ztest_random(eligible_idx);
5606 return (ztest_random_concrete_vdev_leaf(eligible[child_no]));
5611 ztest_initialize(ztest_ds_t *zd, uint64_t id)
5613 spa_t *spa = ztest_spa;
5616 mutex_enter(&ztest_vdev_lock);
5618 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
5620 /* Random leaf vdev */
5621 vdev_t *rand_vd = ztest_random_concrete_vdev_leaf(spa->spa_root_vdev);
5622 if (rand_vd == NULL) {
5623 spa_config_exit(spa, SCL_VDEV, FTAG);
5624 mutex_exit(&ztest_vdev_lock);
5629 * The random vdev we've selected may change as soon as we
5630 * drop the spa_config_lock. We create local copies of things
5631 * we're interested in.
5633 uint64_t guid = rand_vd->vdev_guid;
5634 char *path = strdup(rand_vd->vdev_path);
5635 boolean_t active = rand_vd->vdev_initialize_thread != NULL;
5637 zfs_dbgmsg("vd %p, guid %llu", rand_vd, guid);
5638 spa_config_exit(spa, SCL_VDEV, FTAG);
5640 uint64_t cmd = ztest_random(POOL_INITIALIZE_FUNCS);
5641 error = spa_vdev_initialize(spa, guid, cmd);
5643 case POOL_INITIALIZE_CANCEL:
5644 if (ztest_opts.zo_verbose >= 4) {
5645 (void) printf("Cancel initialize %s", path);
5647 (void) printf(" failed (no initialize active)");
5648 (void) printf("\n");
5651 case POOL_INITIALIZE_DO:
5652 if (ztest_opts.zo_verbose >= 4) {
5653 (void) printf("Start initialize %s", path);
5654 if (active && error == 0)
5655 (void) printf(" failed (already active)");
5656 else if (error != 0)
5657 (void) printf(" failed (error %d)", error);
5658 (void) printf("\n");
5661 case POOL_INITIALIZE_SUSPEND:
5662 if (ztest_opts.zo_verbose >= 4) {
5663 (void) printf("Suspend initialize %s", path);
5665 (void) printf(" failed (no initialize active)");
5666 (void) printf("\n");
5671 mutex_exit(&ztest_vdev_lock);
5675 * Verify pool integrity by running zdb.
5678 ztest_run_zdb(char *pool)
5681 char zdb[MAXPATHLEN + MAXNAMELEN + 20];
5689 strlcpy(zdb, "/usr/bin/ztest", sizeof(zdb));
5691 /* zdb lives in /usr/sbin, while ztest lives in /usr/bin */
5692 bin = strstr(zdb, "/usr/bin/");
5693 ztest = strstr(bin, "/ztest");
5695 isalen = ztest - isa;
5699 "/usr/sbin%.*s/zdb -bcc%s%s -G -d -U %s %s",
5702 ztest_opts.zo_verbose >= 3 ? "s" : "",
5703 ztest_opts.zo_verbose >= 4 ? "v" : "",
5708 if (ztest_opts.zo_verbose >= 5)
5709 (void) printf("Executing %s\n", strstr(zdb, "zdb "));
5711 fp = popen(zdb, "r");
5714 while (fgets(zbuf, sizeof (zbuf), fp) != NULL)
5715 if (ztest_opts.zo_verbose >= 3)
5716 (void) printf("%s", zbuf);
5718 status = pclose(fp);
5723 ztest_dump_core = 0;
5724 if (WIFEXITED(status))
5725 fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
5727 fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
5731 ztest_walk_pool_directory(char *header)
5735 if (ztest_opts.zo_verbose >= 6)
5736 (void) printf("%s\n", header);
5738 mutex_enter(&spa_namespace_lock);
5739 while ((spa = spa_next(spa)) != NULL)
5740 if (ztest_opts.zo_verbose >= 6)
5741 (void) printf("\t%s\n", spa_name(spa));
5742 mutex_exit(&spa_namespace_lock);
5746 ztest_spa_import_export(char *oldname, char *newname)
5748 nvlist_t *config, *newconfig;
5753 if (ztest_opts.zo_verbose >= 4) {
5754 (void) printf("import/export: old = %s, new = %s\n",
5759 * Clean up from previous runs.
5761 (void) spa_destroy(newname);
5764 * Get the pool's configuration and guid.
5766 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
5769 * Kick off a scrub to tickle scrub/export races.
5771 if (ztest_random(2) == 0)
5772 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5774 pool_guid = spa_guid(spa);
5775 spa_close(spa, FTAG);
5777 ztest_walk_pool_directory("pools before export");
5782 VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
5784 ztest_walk_pool_directory("pools after export");
5789 newconfig = spa_tryimport(config);
5790 ASSERT(newconfig != NULL);
5791 nvlist_free(newconfig);
5794 * Import it under the new name.
5796 error = spa_import(newname, config, NULL, 0);
5798 dump_nvlist(config, 0);
5799 fatal(B_FALSE, "couldn't import pool %s as %s: error %u",
5800 oldname, newname, error);
5803 ztest_walk_pool_directory("pools after import");
5806 * Try to import it again -- should fail with EEXIST.
5808 VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL, 0));
5811 * Try to import it under a different name -- should fail with EEXIST.
5813 VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL, 0));
5816 * Verify that the pool is no longer visible under the old name.
5818 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5821 * Verify that we can open and close the pool using the new name.
5823 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
5824 ASSERT(pool_guid == spa_guid(spa));
5825 spa_close(spa, FTAG);
5827 nvlist_free(config);
5831 ztest_resume(spa_t *spa)
5833 if (spa_suspended(spa) && ztest_opts.zo_verbose >= 6)
5834 (void) printf("resuming from suspended state\n");
5835 spa_vdev_state_enter(spa, SCL_NONE);
5836 vdev_clear(spa, NULL);
5837 (void) spa_vdev_state_exit(spa, NULL, 0);
5838 (void) zio_resume(spa);
5842 ztest_resume_thread(void *arg)
5846 while (!ztest_exiting) {
5847 if (spa_suspended(spa))
5849 (void) poll(NULL, 0, 100);
5852 * Periodically change the zfs_compressed_arc_enabled setting.
5854 if (ztest_random(10) == 0)
5855 zfs_compressed_arc_enabled = ztest_random(2);
5858 * Periodically change the zfs_abd_scatter_enabled setting.
5860 if (ztest_random(10) == 0)
5861 zfs_abd_scatter_enabled = ztest_random(2);
5867 ztest_deadman_thread(void *arg)
5869 ztest_shared_t *zs = arg;
5870 spa_t *spa = ztest_spa;
5871 hrtime_t delta, total = 0;
5874 delta = zs->zs_thread_stop - zs->zs_thread_start +
5875 MSEC2NSEC(zfs_deadman_synctime_ms);
5877 (void) poll(NULL, 0, (int)NSEC2MSEC(delta));
5880 * If the pool is suspended then fail immediately. Otherwise,
5881 * check to see if the pool is making any progress. If
5882 * vdev_deadman() discovers that there hasn't been any recent
5883 * I/Os then it will end up aborting the tests.
5885 if (spa_suspended(spa) || spa->spa_root_vdev == NULL) {
5886 fatal(0, "aborting test after %llu seconds because "
5887 "pool has transitioned to a suspended state.",
5888 zfs_deadman_synctime_ms / 1000);
5891 vdev_deadman(spa->spa_root_vdev);
5893 total += zfs_deadman_synctime_ms/1000;
5894 (void) printf("ztest has been running for %lld seconds\n",
5900 ztest_execute(int test, ztest_info_t *zi, uint64_t id)
5902 ztest_ds_t *zd = &ztest_ds[id % ztest_opts.zo_datasets];
5903 ztest_shared_callstate_t *zc = ZTEST_GET_SHARED_CALLSTATE(test);
5904 hrtime_t functime = gethrtime();
5906 for (int i = 0; i < zi->zi_iters; i++)
5907 zi->zi_func(zd, id);
5909 functime = gethrtime() - functime;
5911 atomic_add_64(&zc->zc_count, 1);
5912 atomic_add_64(&zc->zc_time, functime);
5914 if (ztest_opts.zo_verbose >= 4) {
5916 (void) dladdr((void *)zi->zi_func, &dli);
5917 (void) printf("%6.2f sec in %s\n",
5918 (double)functime / NANOSEC, dli.dli_sname);
5923 ztest_thread(void *arg)
5926 uint64_t id = (uintptr_t)arg;
5927 ztest_shared_t *zs = ztest_shared;
5931 ztest_shared_callstate_t *zc;
5933 while ((now = gethrtime()) < zs->zs_thread_stop) {
5935 * See if it's time to force a crash.
5937 if (now > zs->zs_thread_kill)
5941 * If we're getting ENOSPC with some regularity, stop.
5943 if (zs->zs_enospc_count > 10)
5947 * Pick a random function to execute.
5949 rand = ztest_random(ZTEST_FUNCS);
5950 zi = &ztest_info[rand];
5951 zc = ZTEST_GET_SHARED_CALLSTATE(rand);
5952 call_next = zc->zc_next;
5954 if (now >= call_next &&
5955 atomic_cas_64(&zc->zc_next, call_next, call_next +
5956 ztest_random(2 * zi->zi_interval[0] + 1)) == call_next) {
5957 ztest_execute(rand, zi, id);
5965 ztest_dataset_name(char *dsname, char *pool, int d)
5967 (void) snprintf(dsname, ZFS_MAX_DATASET_NAME_LEN, "%s/ds_%d", pool, d);
5971 ztest_dataset_destroy(int d)
5973 char name[ZFS_MAX_DATASET_NAME_LEN];
5975 ztest_dataset_name(name, ztest_opts.zo_pool, d);
5977 if (ztest_opts.zo_verbose >= 3)
5978 (void) printf("Destroying %s to free up space\n", name);
5981 * Cleanup any non-standard clones and snapshots. In general,
5982 * ztest thread t operates on dataset (t % zopt_datasets),
5983 * so there may be more than one thing to clean up.
5985 for (int t = d; t < ztest_opts.zo_threads;
5986 t += ztest_opts.zo_datasets) {
5987 ztest_dsl_dataset_cleanup(name, t);
5990 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
5991 DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
5995 ztest_dataset_dirobj_verify(ztest_ds_t *zd)
5997 uint64_t usedobjs, dirobjs, scratch;
6000 * ZTEST_DIROBJ is the object directory for the entire dataset.
6001 * Therefore, the number of objects in use should equal the
6002 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
6003 * If not, we have an object leak.
6005 * Note that we can only check this in ztest_dataset_open(),
6006 * when the open-context and syncing-context values agree.
6007 * That's because zap_count() returns the open-context value,
6008 * while dmu_objset_space() returns the rootbp fill count.
6010 VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
6011 dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
6012 ASSERT3U(dirobjs + 1, ==, usedobjs);
6016 ztest_dataset_open(int d)
6018 ztest_ds_t *zd = &ztest_ds[d];
6019 uint64_t committed_seq = ZTEST_GET_SHARED_DS(d)->zd_seq;
6022 char name[ZFS_MAX_DATASET_NAME_LEN];
6025 ztest_dataset_name(name, ztest_opts.zo_pool, d);
6027 rw_enter(&ztest_name_lock, RW_READER);
6029 error = ztest_dataset_create(name);
6030 if (error == ENOSPC) {
6031 rw_exit(&ztest_name_lock);
6032 ztest_record_enospc(FTAG);
6035 ASSERT(error == 0 || error == EEXIST);
6037 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, zd, &os));
6038 rw_exit(&ztest_name_lock);
6040 ztest_zd_init(zd, ZTEST_GET_SHARED_DS(d), os);
6042 zilog = zd->zd_zilog;
6044 if (zilog->zl_header->zh_claim_lr_seq != 0 &&
6045 zilog->zl_header->zh_claim_lr_seq < committed_seq)
6046 fatal(0, "missing log records: claimed %llu < committed %llu",
6047 zilog->zl_header->zh_claim_lr_seq, committed_seq);
6049 ztest_dataset_dirobj_verify(zd);
6051 zil_replay(os, zd, ztest_replay_vector);
6053 ztest_dataset_dirobj_verify(zd);
6055 if (ztest_opts.zo_verbose >= 6)
6056 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
6058 (u_longlong_t)zilog->zl_parse_blk_count,
6059 (u_longlong_t)zilog->zl_parse_lr_count,
6060 (u_longlong_t)zilog->zl_replaying_seq);
6062 zilog = zil_open(os, ztest_get_data);
6064 if (zilog->zl_replaying_seq != 0 &&
6065 zilog->zl_replaying_seq < committed_seq)
6066 fatal(0, "missing log records: replayed %llu < committed %llu",
6067 zilog->zl_replaying_seq, committed_seq);
6073 ztest_dataset_close(int d)
6075 ztest_ds_t *zd = &ztest_ds[d];
6077 zil_close(zd->zd_zilog);
6078 dmu_objset_disown(zd->zd_os, zd);
6084 * Kick off threads to run tests on all datasets in parallel.
6087 ztest_run(ztest_shared_t *zs)
6092 thread_t resume_tid;
6095 ztest_exiting = B_FALSE;
6098 * Initialize parent/child shared state.
6100 mutex_init(&ztest_checkpoint_lock, NULL, USYNC_THREAD, NULL);
6101 mutex_init(&ztest_vdev_lock, NULL, USYNC_THREAD, NULL);
6102 rw_init(&ztest_name_lock, NULL, USYNC_THREAD, NULL);
6104 zs->zs_thread_start = gethrtime();
6105 zs->zs_thread_stop =
6106 zs->zs_thread_start + ztest_opts.zo_passtime * NANOSEC;
6107 zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
6108 zs->zs_thread_kill = zs->zs_thread_stop;
6109 if (ztest_random(100) < ztest_opts.zo_killrate) {
6110 zs->zs_thread_kill -=
6111 ztest_random(ztest_opts.zo_passtime * NANOSEC);
6114 mutex_init(&zcl.zcl_callbacks_lock, NULL, USYNC_THREAD, NULL);
6116 list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
6117 offsetof(ztest_cb_data_t, zcd_node));
6122 kernel_init(FREAD | FWRITE);
6123 VERIFY0(spa_open(ztest_opts.zo_pool, &spa, FTAG));
6124 metaslab_preload_limit = ztest_random(20) + 1;
6127 dmu_objset_stats_t dds;
6128 VERIFY0(dmu_objset_own(ztest_opts.zo_pool,
6129 DMU_OST_ANY, B_TRUE, FTAG, &os));
6130 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
6131 dmu_objset_fast_stat(os, &dds);
6132 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
6133 zs->zs_guid = dds.dds_guid;
6134 dmu_objset_disown(os, FTAG);
6136 spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
6139 * We don't expect the pool to suspend unless maxfaults == 0,
6140 * in which case ztest_fault_inject() temporarily takes away
6141 * the only valid replica.
6143 if (MAXFAULTS() == 0)
6144 spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
6146 spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
6149 * Create a thread to periodically resume suspended I/O.
6151 VERIFY(thr_create(0, 0, ztest_resume_thread, spa, THR_BOUND,
6155 * Create a deadman thread to abort() if we hang.
6157 VERIFY(thr_create(0, 0, ztest_deadman_thread, zs, THR_BOUND,
6161 * Verify that we can safely inquire about any object,
6162 * whether it's allocated or not. To make it interesting,
6163 * we probe a 5-wide window around each power of two.
6164 * This hits all edge cases, including zero and the max.
6166 for (int t = 0; t < 64; t++) {
6167 for (int d = -5; d <= 5; d++) {
6168 error = dmu_object_info(spa->spa_meta_objset,
6169 (1ULL << t) + d, NULL);
6170 ASSERT(error == 0 || error == ENOENT ||
6176 * If we got any ENOSPC errors on the previous run, destroy something.
6178 if (zs->zs_enospc_count != 0) {
6179 int d = ztest_random(ztest_opts.zo_datasets);
6180 ztest_dataset_destroy(d);
6182 zs->zs_enospc_count = 0;
6184 tid = umem_zalloc(ztest_opts.zo_threads * sizeof (thread_t),
6187 if (ztest_opts.zo_verbose >= 4)
6188 (void) printf("starting main threads...\n");
6191 * Kick off all the tests that run in parallel.
6193 for (int t = 0; t < ztest_opts.zo_threads; t++) {
6194 if (t < ztest_opts.zo_datasets &&
6195 ztest_dataset_open(t) != 0)
6197 VERIFY(thr_create(0, 0, ztest_thread, (void *)(uintptr_t)t,
6198 THR_BOUND, &tid[t]) == 0);
6202 * Wait for all of the tests to complete. We go in reverse order
6203 * so we don't close datasets while threads are still using them.
6205 for (int t = ztest_opts.zo_threads - 1; t >= 0; t--) {
6206 VERIFY(thr_join(tid[t], NULL, NULL) == 0);
6207 if (t < ztest_opts.zo_datasets)
6208 ztest_dataset_close(t);
6211 txg_wait_synced(spa_get_dsl(spa), 0);
6213 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
6214 zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
6215 zfs_dbgmsg_print(FTAG);
6217 umem_free(tid, ztest_opts.zo_threads * sizeof (thread_t));
6219 /* Kill the resume thread */
6220 ztest_exiting = B_TRUE;
6221 VERIFY(thr_join(resume_tid, NULL, NULL) == 0);
6225 * Right before closing the pool, kick off a bunch of async I/O;
6226 * spa_close() should wait for it to complete.
6228 for (uint64_t object = 1; object < 50; object++) {
6229 dmu_prefetch(spa->spa_meta_objset, object, 0, 0, 1ULL << 20,
6230 ZIO_PRIORITY_SYNC_READ);
6233 spa_close(spa, FTAG);
6236 * Verify that we can loop over all pools.
6238 mutex_enter(&spa_namespace_lock);
6239 for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
6240 if (ztest_opts.zo_verbose > 3)
6241 (void) printf("spa_next: found %s\n", spa_name(spa));
6242 mutex_exit(&spa_namespace_lock);
6245 * Verify that we can export the pool and reimport it under a
6248 if (ztest_random(2) == 0) {
6249 char name[ZFS_MAX_DATASET_NAME_LEN];
6250 (void) snprintf(name, sizeof (name), "%s_import",
6251 ztest_opts.zo_pool);
6252 ztest_spa_import_export(ztest_opts.zo_pool, name);
6253 ztest_spa_import_export(name, ztest_opts.zo_pool);
6258 list_destroy(&zcl.zcl_callbacks);
6260 mutex_destroy(&zcl.zcl_callbacks_lock);
6262 rw_destroy(&ztest_name_lock);
6263 mutex_destroy(&ztest_vdev_lock);
6264 mutex_destroy(&ztest_checkpoint_lock);
6270 ztest_ds_t *zd = &ztest_ds[0];
6274 if (ztest_opts.zo_verbose >= 3)
6275 (void) printf("testing spa_freeze()...\n");
6277 kernel_init(FREAD | FWRITE);
6278 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6279 VERIFY3U(0, ==, ztest_dataset_open(0));
6283 * Force the first log block to be transactionally allocated.
6284 * We have to do this before we freeze the pool -- otherwise
6285 * the log chain won't be anchored.
6287 while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
6288 ztest_dmu_object_alloc_free(zd, 0);
6289 zil_commit(zd->zd_zilog, 0);
6292 txg_wait_synced(spa_get_dsl(spa), 0);
6295 * Freeze the pool. This stops spa_sync() from doing anything,
6296 * so that the only way to record changes from now on is the ZIL.
6301 * Because it is hard to predict how much space a write will actually
6302 * require beforehand, we leave ourselves some fudge space to write over
6305 uint64_t capacity = metaslab_class_get_space(spa_normal_class(spa)) / 2;
6308 * Run tests that generate log records but don't alter the pool config
6309 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
6310 * We do a txg_wait_synced() after each iteration to force the txg
6311 * to increase well beyond the last synced value in the uberblock.
6312 * The ZIL should be OK with that.
6314 * Run a random number of times less than zo_maxloops and ensure we do
6315 * not run out of space on the pool.
6317 while (ztest_random(10) != 0 &&
6318 numloops++ < ztest_opts.zo_maxloops &&
6319 metaslab_class_get_alloc(spa_normal_class(spa)) < capacity) {
6321 ztest_od_init(&od, 0, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
6322 VERIFY0(ztest_object_init(zd, &od, sizeof (od), B_FALSE));
6323 ztest_io(zd, od.od_object,
6324 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
6325 txg_wait_synced(spa_get_dsl(spa), 0);
6329 * Commit all of the changes we just generated.
6331 zil_commit(zd->zd_zilog, 0);
6332 txg_wait_synced(spa_get_dsl(spa), 0);
6335 * Close our dataset and close the pool.
6337 ztest_dataset_close(0);
6338 spa_close(spa, FTAG);
6342 * Open and close the pool and dataset to induce log replay.
6344 kernel_init(FREAD | FWRITE);
6345 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6346 ASSERT(spa_freeze_txg(spa) == UINT64_MAX);
6347 VERIFY3U(0, ==, ztest_dataset_open(0));
6348 ztest_dataset_close(0);
6351 txg_wait_synced(spa_get_dsl(spa), 0);
6352 ztest_reguid(NULL, 0);
6354 spa_close(spa, FTAG);
6359 print_time(hrtime_t t, char *timebuf)
6361 hrtime_t s = t / NANOSEC;
6362 hrtime_t m = s / 60;
6363 hrtime_t h = m / 60;
6364 hrtime_t d = h / 24;
6373 (void) sprintf(timebuf,
6374 "%llud%02lluh%02llum%02llus", d, h, m, s);
6376 (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
6378 (void) sprintf(timebuf, "%llum%02llus", m, s);
6380 (void) sprintf(timebuf, "%llus", s);
6388 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
6389 if (ztest_random(2) == 0)
6391 VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
6397 * Create a storage pool with the given name and initial vdev size.
6398 * Then test spa_freeze() functionality.
6401 ztest_init(ztest_shared_t *zs)
6404 nvlist_t *nvroot, *props;
6406 mutex_init(&ztest_vdev_lock, NULL, USYNC_THREAD, NULL);
6407 mutex_init(&ztest_checkpoint_lock, NULL, USYNC_THREAD, NULL);
6408 rw_init(&ztest_name_lock, NULL, USYNC_THREAD, NULL);
6410 kernel_init(FREAD | FWRITE);
6413 * Create the storage pool.
6415 (void) spa_destroy(ztest_opts.zo_pool);
6416 ztest_shared->zs_vdev_next_leaf = 0;
6418 zs->zs_mirrors = ztest_opts.zo_mirrors;
6419 nvroot = make_vdev_root(NULL, NULL, NULL, ztest_opts.zo_vdev_size, 0,
6420 0, ztest_opts.zo_raidz, zs->zs_mirrors, 1);
6421 props = make_random_props();
6422 for (int i = 0; i < SPA_FEATURES; i++) {
6424 (void) snprintf(buf, sizeof (buf), "feature@%s",
6425 spa_feature_table[i].fi_uname);
6426 VERIFY3U(0, ==, nvlist_add_uint64(props, buf, 0));
6428 VERIFY3U(0, ==, spa_create(ztest_opts.zo_pool, nvroot, props, NULL));
6429 nvlist_free(nvroot);
6432 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6433 zs->zs_metaslab_sz =
6434 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
6436 spa_close(spa, FTAG);
6440 ztest_run_zdb(ztest_opts.zo_pool);
6444 ztest_run_zdb(ztest_opts.zo_pool);
6446 rw_destroy(&ztest_name_lock);
6447 mutex_destroy(&ztest_vdev_lock);
6448 mutex_destroy(&ztest_checkpoint_lock);
6454 static char ztest_name_data[] = "/tmp/ztest.data.XXXXXX";
6456 ztest_fd_data = mkstemp(ztest_name_data);
6457 ASSERT3S(ztest_fd_data, >=, 0);
6458 (void) unlink(ztest_name_data);
6463 shared_data_size(ztest_shared_hdr_t *hdr)
6467 size = hdr->zh_hdr_size;
6468 size += hdr->zh_opts_size;
6469 size += hdr->zh_size;
6470 size += hdr->zh_stats_size * hdr->zh_stats_count;
6471 size += hdr->zh_ds_size * hdr->zh_ds_count;
6480 ztest_shared_hdr_t *hdr;
6482 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6483 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6484 ASSERT(hdr != MAP_FAILED);
6486 VERIFY3U(0, ==, ftruncate(ztest_fd_data, sizeof (ztest_shared_hdr_t)));
6488 hdr->zh_hdr_size = sizeof (ztest_shared_hdr_t);
6489 hdr->zh_opts_size = sizeof (ztest_shared_opts_t);
6490 hdr->zh_size = sizeof (ztest_shared_t);
6491 hdr->zh_stats_size = sizeof (ztest_shared_callstate_t);
6492 hdr->zh_stats_count = ZTEST_FUNCS;
6493 hdr->zh_ds_size = sizeof (ztest_shared_ds_t);
6494 hdr->zh_ds_count = ztest_opts.zo_datasets;
6496 size = shared_data_size(hdr);
6497 VERIFY3U(0, ==, ftruncate(ztest_fd_data, size));
6499 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6506 ztest_shared_hdr_t *hdr;
6509 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6510 PROT_READ, MAP_SHARED, ztest_fd_data, 0);
6511 ASSERT(hdr != MAP_FAILED);
6513 size = shared_data_size(hdr);
6515 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6516 hdr = ztest_shared_hdr = (void *)mmap(0, P2ROUNDUP(size, getpagesize()),
6517 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6518 ASSERT(hdr != MAP_FAILED);
6519 buf = (uint8_t *)hdr;
6521 offset = hdr->zh_hdr_size;
6522 ztest_shared_opts = (void *)&buf[offset];
6523 offset += hdr->zh_opts_size;
6524 ztest_shared = (void *)&buf[offset];
6525 offset += hdr->zh_size;
6526 ztest_shared_callstate = (void *)&buf[offset];
6527 offset += hdr->zh_stats_size * hdr->zh_stats_count;
6528 ztest_shared_ds = (void *)&buf[offset];
6532 exec_child(char *cmd, char *libpath, boolean_t ignorekill, int *statusp)
6536 char *cmdbuf = NULL;
6541 cmdbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
6542 (void) strlcpy(cmdbuf, getexecname(), MAXPATHLEN);
6547 fatal(1, "fork failed");
6549 if (pid == 0) { /* child */
6550 char *emptyargv[2] = { cmd, NULL };
6551 char fd_data_str[12];
6553 struct rlimit rl = { 1024, 1024 };
6554 (void) setrlimit(RLIMIT_NOFILE, &rl);
6556 (void) close(ztest_fd_rand);
6558 snprintf(fd_data_str, 12, "%d", ztest_fd_data));
6559 VERIFY0(setenv("ZTEST_FD_DATA", fd_data_str, 1));
6561 (void) enable_extended_FILE_stdio(-1, -1);
6562 if (libpath != NULL)
6563 VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath, 1));
6565 (void) execv(cmd, emptyargv);
6567 (void) execvp(cmd, emptyargv);
6569 ztest_dump_core = B_FALSE;
6570 fatal(B_TRUE, "exec failed: %s", cmd);
6573 if (cmdbuf != NULL) {
6574 umem_free(cmdbuf, MAXPATHLEN);
6578 while (waitpid(pid, &status, 0) != pid)
6580 if (statusp != NULL)
6583 if (WIFEXITED(status)) {
6584 if (WEXITSTATUS(status) != 0) {
6585 (void) fprintf(stderr, "child exited with code %d\n",
6586 WEXITSTATUS(status));
6590 } else if (WIFSIGNALED(status)) {
6591 if (!ignorekill || WTERMSIG(status) != SIGKILL) {
6592 (void) fprintf(stderr, "child died with signal %d\n",
6598 (void) fprintf(stderr, "something strange happened to child\n");
6605 ztest_run_init(void)
6607 ztest_shared_t *zs = ztest_shared;
6609 ASSERT(ztest_opts.zo_init != 0);
6612 * Blow away any existing copy of zpool.cache
6614 (void) remove(spa_config_path);
6617 * Create and initialize our storage pool.
6619 for (int i = 1; i <= ztest_opts.zo_init; i++) {
6620 bzero(zs, sizeof (ztest_shared_t));
6621 if (ztest_opts.zo_verbose >= 3 &&
6622 ztest_opts.zo_init != 1) {
6623 (void) printf("ztest_init(), pass %d\n", i);
6630 main(int argc, char **argv)
6638 ztest_shared_callstate_t *zc;
6640 char numbuf[NN_NUMBUF_SZ];
6643 char *fd_data_str = getenv("ZTEST_FD_DATA");
6645 (void) setvbuf(stdout, NULL, _IOLBF, 0);
6647 dprintf_setup(&argc, argv);
6648 zfs_deadman_synctime_ms = 300000;
6650 * As two-word space map entries may not come up often (especially
6651 * if pool and vdev sizes are small) we want to force at least some
6652 * of them so the feature get tested.
6654 zfs_force_some_double_word_sm_entries = B_TRUE;
6656 ztest_fd_rand = open("/dev/urandom", O_RDONLY);
6657 ASSERT3S(ztest_fd_rand, >=, 0);
6660 process_options(argc, argv);
6665 bcopy(&ztest_opts, ztest_shared_opts,
6666 sizeof (*ztest_shared_opts));
6668 ztest_fd_data = atoi(fd_data_str);
6670 bcopy(ztest_shared_opts, &ztest_opts, sizeof (ztest_opts));
6672 ASSERT3U(ztest_opts.zo_datasets, ==, ztest_shared_hdr->zh_ds_count);
6674 /* Override location of zpool.cache */
6675 VERIFY3U(asprintf((char **)&spa_config_path, "%s/zpool.cache",
6676 ztest_opts.zo_dir), !=, -1);
6678 ztest_ds = umem_alloc(ztest_opts.zo_datasets * sizeof (ztest_ds_t),
6683 metaslab_force_ganging = ztest_opts.zo_metaslab_force_ganging;
6684 metaslab_df_alloc_threshold =
6685 zs->zs_metaslab_df_alloc_threshold;
6694 hasalt = (strlen(ztest_opts.zo_alt_ztest) != 0);
6696 if (ztest_opts.zo_verbose >= 1) {
6697 (void) printf("%llu vdevs, %d datasets, %d threads,"
6698 " %llu seconds...\n",
6699 (u_longlong_t)ztest_opts.zo_vdevs,
6700 ztest_opts.zo_datasets,
6701 ztest_opts.zo_threads,
6702 (u_longlong_t)ztest_opts.zo_time);
6705 cmd = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
6706 (void) strlcpy(cmd, getexecname(), MAXNAMELEN);
6708 zs->zs_do_init = B_TRUE;
6709 if (strlen(ztest_opts.zo_alt_ztest) != 0) {
6710 if (ztest_opts.zo_verbose >= 1) {
6711 (void) printf("Executing older ztest for "
6712 "initialization: %s\n", ztest_opts.zo_alt_ztest);
6714 VERIFY(!exec_child(ztest_opts.zo_alt_ztest,
6715 ztest_opts.zo_alt_libpath, B_FALSE, NULL));
6717 VERIFY(!exec_child(NULL, NULL, B_FALSE, NULL));
6719 zs->zs_do_init = B_FALSE;
6721 zs->zs_proc_start = gethrtime();
6722 zs->zs_proc_stop = zs->zs_proc_start + ztest_opts.zo_time * NANOSEC;
6724 for (int f = 0; f < ZTEST_FUNCS; f++) {
6725 zi = &ztest_info[f];
6726 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6727 if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
6728 zc->zc_next = UINT64_MAX;
6730 zc->zc_next = zs->zs_proc_start +
6731 ztest_random(2 * zi->zi_interval[0] + 1);
6735 * Run the tests in a loop. These tests include fault injection
6736 * to verify that self-healing data works, and forced crashes
6737 * to verify that we never lose on-disk consistency.
6739 while (gethrtime() < zs->zs_proc_stop) {
6744 * Initialize the workload counters for each function.
6746 for (int f = 0; f < ZTEST_FUNCS; f++) {
6747 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6752 /* Set the allocation switch size */
6753 zs->zs_metaslab_df_alloc_threshold =
6754 ztest_random(zs->zs_metaslab_sz / 4) + 1;
6756 if (!hasalt || ztest_random(2) == 0) {
6757 if (hasalt && ztest_opts.zo_verbose >= 1) {
6758 (void) printf("Executing newer ztest: %s\n",
6762 killed = exec_child(cmd, NULL, B_TRUE, &status);
6764 if (hasalt && ztest_opts.zo_verbose >= 1) {
6765 (void) printf("Executing older ztest: %s\n",
6766 ztest_opts.zo_alt_ztest);
6769 killed = exec_child(ztest_opts.zo_alt_ztest,
6770 ztest_opts.zo_alt_libpath, B_TRUE, &status);
6777 if (ztest_opts.zo_verbose >= 1) {
6778 hrtime_t now = gethrtime();
6780 now = MIN(now, zs->zs_proc_stop);
6781 print_time(zs->zs_proc_stop - now, timebuf);
6782 nicenum(zs->zs_space, numbuf, sizeof (numbuf));
6784 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
6785 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
6787 WIFEXITED(status) ? "Complete" : "SIGKILL",
6788 (u_longlong_t)zs->zs_enospc_count,
6789 100.0 * zs->zs_alloc / zs->zs_space,
6791 100.0 * (now - zs->zs_proc_start) /
6792 (ztest_opts.zo_time * NANOSEC), timebuf);
6795 if (ztest_opts.zo_verbose >= 2) {
6796 (void) printf("\nWorkload summary:\n\n");
6797 (void) printf("%7s %9s %s\n",
6798 "Calls", "Time", "Function");
6799 (void) printf("%7s %9s %s\n",
6800 "-----", "----", "--------");
6801 for (int f = 0; f < ZTEST_FUNCS; f++) {
6804 zi = &ztest_info[f];
6805 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6806 print_time(zc->zc_time, timebuf);
6807 (void) dladdr((void *)zi->zi_func, &dli);
6808 (void) printf("%7llu %9s %s\n",
6809 (u_longlong_t)zc->zc_count, timebuf,
6812 (void) printf("\n");
6815 ztest_run_zdb(ztest_opts.zo_pool);
6818 if (ztest_opts.zo_verbose >= 1) {
6820 (void) printf("%d runs of older ztest: %s\n", older,
6821 ztest_opts.zo_alt_ztest);
6822 (void) printf("%d runs of newer ztest: %s\n", newer,
6825 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
6826 kills, iters - kills, (100.0 * kills) / MAX(1, iters));
6829 umem_free(cmd, MAXNAMELEN);