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 boolean_t zfs_force_some_double_word_sm_entries;
201 static ztest_shared_opts_t *ztest_shared_opts;
202 static ztest_shared_opts_t ztest_opts;
204 typedef struct ztest_shared_ds {
208 static ztest_shared_ds_t *ztest_shared_ds;
209 #define ZTEST_GET_SHARED_DS(d) (&ztest_shared_ds[d])
211 #define BT_MAGIC 0x123456789abcdefULL
212 #define MAXFAULTS() \
213 (MAX(zs->zs_mirrors, 1) * (ztest_opts.zo_raidz_parity + 1) - 1)
217 ZTEST_IO_WRITE_PATTERN,
218 ZTEST_IO_WRITE_ZEROES,
225 typedef struct ztest_block_tag {
229 uint64_t bt_dnodesize;
236 typedef struct bufwad {
243 * XXX -- fix zfs range locks to be generic so we can use them here.
265 #define ZTEST_RANGE_LOCKS 64
266 #define ZTEST_OBJECT_LOCKS 64
269 * Object descriptor. Used as a template for object lookup/create/remove.
271 typedef struct ztest_od {
274 dmu_object_type_t od_type;
275 dmu_object_type_t od_crtype;
276 uint64_t od_blocksize;
277 uint64_t od_crblocksize;
278 uint64_t od_crdnodesize;
281 char od_name[ZFS_MAX_DATASET_NAME_LEN];
287 typedef struct ztest_ds {
288 ztest_shared_ds_t *zd_shared;
290 krwlock_t zd_zilog_lock;
292 ztest_od_t *zd_od; /* debugging aid */
293 char zd_name[ZFS_MAX_DATASET_NAME_LEN];
294 kmutex_t zd_dirobj_lock;
295 rll_t zd_object_lock[ZTEST_OBJECT_LOCKS];
296 rll_t zd_range_lock[ZTEST_RANGE_LOCKS];
300 * Per-iteration state.
302 typedef void ztest_func_t(ztest_ds_t *zd, uint64_t id);
304 typedef struct ztest_info {
305 ztest_func_t *zi_func; /* test function */
306 uint64_t zi_iters; /* iterations per execution */
307 uint64_t *zi_interval; /* execute every <interval> seconds */
310 typedef struct ztest_shared_callstate {
311 uint64_t zc_count; /* per-pass count */
312 uint64_t zc_time; /* per-pass time */
313 uint64_t zc_next; /* next time to call this function */
314 } ztest_shared_callstate_t;
316 static ztest_shared_callstate_t *ztest_shared_callstate;
317 #define ZTEST_GET_SHARED_CALLSTATE(c) (&ztest_shared_callstate[c])
320 * Note: these aren't static because we want dladdr() to work.
322 ztest_func_t ztest_dmu_read_write;
323 ztest_func_t ztest_dmu_write_parallel;
324 ztest_func_t ztest_dmu_object_alloc_free;
325 ztest_func_t ztest_dmu_commit_callbacks;
326 ztest_func_t ztest_zap;
327 ztest_func_t ztest_zap_parallel;
328 ztest_func_t ztest_zil_commit;
329 ztest_func_t ztest_zil_remount;
330 ztest_func_t ztest_dmu_read_write_zcopy;
331 ztest_func_t ztest_dmu_objset_create_destroy;
332 ztest_func_t ztest_dmu_prealloc;
333 ztest_func_t ztest_fzap;
334 ztest_func_t ztest_dmu_snapshot_create_destroy;
335 ztest_func_t ztest_dsl_prop_get_set;
336 ztest_func_t ztest_spa_prop_get_set;
337 ztest_func_t ztest_spa_create_destroy;
338 ztest_func_t ztest_fault_inject;
339 ztest_func_t ztest_ddt_repair;
340 ztest_func_t ztest_dmu_snapshot_hold;
341 ztest_func_t ztest_scrub;
342 ztest_func_t ztest_dsl_dataset_promote_busy;
343 ztest_func_t ztest_vdev_attach_detach;
344 ztest_func_t ztest_vdev_LUN_growth;
345 ztest_func_t ztest_vdev_add_remove;
346 ztest_func_t ztest_vdev_aux_add_remove;
347 ztest_func_t ztest_split_pool;
348 ztest_func_t ztest_reguid;
349 ztest_func_t ztest_spa_upgrade;
350 ztest_func_t ztest_device_removal;
351 ztest_func_t ztest_remap_blocks;
352 ztest_func_t ztest_spa_checkpoint_create_discard;
353 ztest_func_t ztest_initialize;
354 ztest_func_t ztest_verify_dnode_bt;
356 uint64_t zopt_always = 0ULL * NANOSEC; /* all the time */
357 uint64_t zopt_incessant = 1ULL * NANOSEC / 10; /* every 1/10 second */
358 uint64_t zopt_often = 1ULL * NANOSEC; /* every second */
359 uint64_t zopt_sometimes = 10ULL * NANOSEC; /* every 10 seconds */
360 uint64_t zopt_rarely = 60ULL * NANOSEC; /* every 60 seconds */
362 ztest_info_t ztest_info[] = {
363 { ztest_dmu_read_write, 1, &zopt_always },
364 { ztest_dmu_write_parallel, 10, &zopt_always },
365 { ztest_dmu_object_alloc_free, 1, &zopt_always },
366 { ztest_dmu_commit_callbacks, 1, &zopt_always },
367 { ztest_zap, 30, &zopt_always },
368 { ztest_zap_parallel, 100, &zopt_always },
369 { ztest_split_pool, 1, &zopt_always },
370 { ztest_zil_commit, 1, &zopt_incessant },
371 { ztest_zil_remount, 1, &zopt_sometimes },
372 { ztest_dmu_read_write_zcopy, 1, &zopt_often },
373 { ztest_dmu_objset_create_destroy, 1, &zopt_often },
374 { ztest_dsl_prop_get_set, 1, &zopt_often },
375 { ztest_spa_prop_get_set, 1, &zopt_sometimes },
377 { ztest_dmu_prealloc, 1, &zopt_sometimes },
379 { ztest_fzap, 1, &zopt_sometimes },
380 { ztest_dmu_snapshot_create_destroy, 1, &zopt_sometimes },
381 { ztest_spa_create_destroy, 1, &zopt_sometimes },
382 { ztest_fault_inject, 1, &zopt_incessant },
383 { ztest_ddt_repair, 1, &zopt_sometimes },
384 { ztest_dmu_snapshot_hold, 1, &zopt_sometimes },
385 { ztest_reguid, 1, &zopt_rarely },
386 { ztest_scrub, 1, &zopt_often },
387 { ztest_spa_upgrade, 1, &zopt_rarely },
388 { ztest_dsl_dataset_promote_busy, 1, &zopt_rarely },
389 { ztest_vdev_attach_detach, 1, &zopt_incessant },
390 { ztest_vdev_LUN_growth, 1, &zopt_rarely },
391 { ztest_vdev_add_remove, 1,
392 &ztest_opts.zo_vdevtime },
393 { ztest_vdev_aux_add_remove, 1,
394 &ztest_opts.zo_vdevtime },
395 { ztest_device_removal, 1, &zopt_sometimes },
396 { ztest_remap_blocks, 1, &zopt_sometimes },
397 { ztest_spa_checkpoint_create_discard, 1, &zopt_rarely },
398 { ztest_initialize, 1, &zopt_sometimes },
399 { ztest_verify_dnode_bt, 1, &zopt_sometimes }
402 #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
405 * The following struct is used to hold a list of uncalled commit callbacks.
406 * The callbacks are ordered by txg number.
408 typedef struct ztest_cb_list {
409 kmutex_t zcl_callbacks_lock;
410 list_t zcl_callbacks;
414 * Stuff we need to share writably between parent and child.
416 typedef struct ztest_shared {
417 boolean_t zs_do_init;
418 hrtime_t zs_proc_start;
419 hrtime_t zs_proc_stop;
420 hrtime_t zs_thread_start;
421 hrtime_t zs_thread_stop;
422 hrtime_t zs_thread_kill;
423 uint64_t zs_enospc_count;
424 uint64_t zs_vdev_next_leaf;
425 uint64_t zs_vdev_aux;
430 uint64_t zs_metaslab_sz;
431 uint64_t zs_metaslab_df_alloc_threshold;
435 #define ID_PARALLEL -1ULL
437 static char ztest_dev_template[] = "%s/%s.%llua";
438 static char ztest_aux_template[] = "%s/%s.%s.%llu";
439 ztest_shared_t *ztest_shared;
441 static spa_t *ztest_spa = NULL;
442 static ztest_ds_t *ztest_ds;
444 static kmutex_t ztest_vdev_lock;
445 static boolean_t ztest_device_removal_active = B_FALSE;
446 static kmutex_t ztest_checkpoint_lock;
449 * The ztest_name_lock protects the pool and dataset namespace used by
450 * the individual tests. To modify the namespace, consumers must grab
451 * this lock as writer. Grabbing the lock as reader will ensure that the
452 * namespace does not change while the lock is held.
454 static krwlock_t ztest_name_lock;
456 static boolean_t ztest_dump_core = B_TRUE;
457 static boolean_t ztest_exiting;
459 /* Global commit callback list */
460 static ztest_cb_list_t zcl;
463 ZTEST_META_DNODE = 0,
468 static void usage(boolean_t) __NORETURN;
471 * These libumem hooks provide a reasonable set of defaults for the allocator's
472 * debugging facilities.
477 return ("default,verbose"); /* $UMEM_DEBUG setting */
481 _umem_logging_init(void)
483 return ("fail,contents"); /* $UMEM_LOGGING setting */
486 #define FATAL_MSG_SZ 1024
491 fatal(int do_perror, char *message, ...)
494 int save_errno = errno;
495 char buf[FATAL_MSG_SZ];
497 (void) fflush(stdout);
499 va_start(args, message);
500 (void) sprintf(buf, "ztest: ");
502 (void) vsprintf(buf + strlen(buf), message, args);
505 (void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
506 ": %s", strerror(save_errno));
508 (void) fprintf(stderr, "%s\n", buf);
509 fatal_msg = buf; /* to ease debugging */
516 str2shift(const char *buf)
518 const char *ends = "BKMGTPEZ";
523 for (i = 0; i < strlen(ends); i++) {
524 if (toupper(buf[0]) == ends[i])
527 if (i == strlen(ends)) {
528 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
532 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
535 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
541 nicenumtoull(const char *buf)
546 val = strtoull(buf, &end, 0);
548 (void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
550 } else if (end[0] == '.') {
551 double fval = strtod(buf, &end);
552 fval *= pow(2, str2shift(end));
553 if (fval > UINT64_MAX) {
554 (void) fprintf(stderr, "ztest: value too large: %s\n",
558 val = (uint64_t)fval;
560 int shift = str2shift(end);
561 if (shift >= 64 || (val << shift) >> shift != val) {
562 (void) fprintf(stderr, "ztest: value too large: %s\n",
572 usage(boolean_t requested)
574 const ztest_shared_opts_t *zo = &ztest_opts_defaults;
576 char nice_vdev_size[NN_NUMBUF_SZ];
577 char nice_force_ganging[NN_NUMBUF_SZ];
578 FILE *fp = requested ? stdout : stderr;
580 nicenum(zo->zo_vdev_size, nice_vdev_size, sizeof (nice_vdev_size));
581 nicenum(zo->zo_metaslab_force_ganging, nice_force_ganging,
582 sizeof (nice_force_ganging));
584 (void) fprintf(fp, "Usage: %s\n"
585 "\t[-v vdevs (default: %llu)]\n"
586 "\t[-s size_of_each_vdev (default: %s)]\n"
587 "\t[-a alignment_shift (default: %d)] use 0 for random\n"
588 "\t[-m mirror_copies (default: %d)]\n"
589 "\t[-r raidz_disks (default: %d)]\n"
590 "\t[-R raidz_parity (default: %d)]\n"
591 "\t[-d datasets (default: %d)]\n"
592 "\t[-t threads (default: %d)]\n"
593 "\t[-g gang_block_threshold (default: %s)]\n"
594 "\t[-i init_count (default: %d)] initialize pool i times\n"
595 "\t[-k kill_percentage (default: %llu%%)]\n"
596 "\t[-p pool_name (default: %s)]\n"
597 "\t[-f dir (default: %s)] file directory for vdev files\n"
598 "\t[-V] verbose (use multiple times for ever more blather)\n"
599 "\t[-E] use existing pool instead of creating new one\n"
600 "\t[-T time (default: %llu sec)] total run time\n"
601 "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
602 "\t[-P passtime (default: %llu sec)] time per pass\n"
603 "\t[-B alt_ztest (default: <none>)] alternate ztest path\n"
604 "\t[-o variable=value] ... set global variable to an unsigned\n"
605 "\t 32-bit integer value\n"
606 "\t[-h] (print help)\n"
609 (u_longlong_t)zo->zo_vdevs, /* -v */
610 nice_vdev_size, /* -s */
611 zo->zo_ashift, /* -a */
612 zo->zo_mirrors, /* -m */
613 zo->zo_raidz, /* -r */
614 zo->zo_raidz_parity, /* -R */
615 zo->zo_datasets, /* -d */
616 zo->zo_threads, /* -t */
617 nice_force_ganging, /* -g */
618 zo->zo_init, /* -i */
619 (u_longlong_t)zo->zo_killrate, /* -k */
620 zo->zo_pool, /* -p */
622 (u_longlong_t)zo->zo_time, /* -T */
623 (u_longlong_t)zo->zo_maxloops, /* -F */
624 (u_longlong_t)zo->zo_passtime);
625 exit(requested ? 0 : 1);
629 process_options(int argc, char **argv)
632 ztest_shared_opts_t *zo = &ztest_opts;
636 char altdir[MAXNAMELEN] = { 0 };
638 bcopy(&ztest_opts_defaults, zo, sizeof (*zo));
640 while ((opt = getopt(argc, argv,
641 "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:hF:B:o:")) != EOF) {
658 value = nicenumtoull(optarg);
662 zo->zo_vdevs = value;
665 zo->zo_vdev_size = MAX(SPA_MINDEVSIZE, value);
668 zo->zo_ashift = value;
671 zo->zo_mirrors = value;
674 zo->zo_raidz = MAX(1, value);
677 zo->zo_raidz_parity = MIN(MAX(value, 1), 3);
680 zo->zo_datasets = MAX(1, value);
683 zo->zo_threads = MAX(1, value);
686 zo->zo_metaslab_force_ganging =
687 MAX(SPA_MINBLOCKSIZE << 1, value);
693 zo->zo_killrate = value;
696 (void) strlcpy(zo->zo_pool, optarg,
697 sizeof (zo->zo_pool));
700 path = realpath(optarg, NULL);
702 (void) fprintf(stderr, "error: %s: %s\n",
703 optarg, strerror(errno));
706 (void) strlcpy(zo->zo_dir, path,
707 sizeof (zo->zo_dir));
720 zo->zo_passtime = MAX(1, value);
723 zo->zo_maxloops = MAX(1, value);
726 (void) strlcpy(altdir, optarg, sizeof (altdir));
729 if (set_global_var(optarg) != 0)
742 zo->zo_raidz_parity = MIN(zo->zo_raidz_parity, zo->zo_raidz - 1);
745 (zo->zo_vdevs > 0 ? zo->zo_time * NANOSEC / zo->zo_vdevs :
748 if (strlen(altdir) > 0) {
756 cmd = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
757 realaltdir = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
759 VERIFY(NULL != realpath(getexecname(), cmd));
760 if (0 != access(altdir, F_OK)) {
761 ztest_dump_core = B_FALSE;
762 fatal(B_TRUE, "invalid alternate ztest path: %s",
765 VERIFY(NULL != realpath(altdir, realaltdir));
768 * 'cmd' should be of the form "<anything>/usr/bin/<isa>/ztest".
769 * We want to extract <isa> to determine if we should use
770 * 32 or 64 bit binaries.
772 bin = strstr(cmd, "/usr/bin/");
773 ztest = strstr(bin, "/ztest");
775 isalen = ztest - isa;
776 (void) snprintf(zo->zo_alt_ztest, sizeof (zo->zo_alt_ztest),
777 "%s/usr/bin/%.*s/ztest", realaltdir, isalen, isa);
778 (void) snprintf(zo->zo_alt_libpath, sizeof (zo->zo_alt_libpath),
779 "%s/usr/lib/%.*s", realaltdir, isalen, isa);
781 if (0 != access(zo->zo_alt_ztest, X_OK)) {
782 ztest_dump_core = B_FALSE;
783 fatal(B_TRUE, "invalid alternate ztest: %s",
785 } else if (0 != access(zo->zo_alt_libpath, X_OK)) {
786 ztest_dump_core = B_FALSE;
787 fatal(B_TRUE, "invalid alternate lib directory %s",
791 umem_free(cmd, MAXPATHLEN);
792 umem_free(realaltdir, MAXPATHLEN);
797 ztest_kill(ztest_shared_t *zs)
799 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(ztest_spa));
800 zs->zs_space = metaslab_class_get_space(spa_normal_class(ztest_spa));
803 * Before we kill off ztest, make sure that the config is updated.
804 * See comment above spa_write_cachefile().
806 mutex_enter(&spa_namespace_lock);
807 spa_write_cachefile(ztest_spa, B_FALSE, B_FALSE);
808 mutex_exit(&spa_namespace_lock);
810 zfs_dbgmsg_print(FTAG);
811 (void) kill(getpid(), SIGKILL);
815 ztest_random(uint64_t range)
819 ASSERT3S(ztest_fd_rand, >=, 0);
824 if (read(ztest_fd_rand, &r, sizeof (r)) != sizeof (r))
825 fatal(1, "short read from /dev/urandom");
832 ztest_record_enospc(const char *s)
834 ztest_shared->zs_enospc_count++;
838 ztest_get_ashift(void)
840 if (ztest_opts.zo_ashift == 0)
841 return (SPA_MINBLOCKSHIFT + ztest_random(5));
842 return (ztest_opts.zo_ashift);
846 make_vdev_file(char *path, char *aux, char *pool, size_t size, uint64_t ashift)
848 char pathbuf[MAXPATHLEN];
853 ashift = ztest_get_ashift();
859 vdev = ztest_shared->zs_vdev_aux;
860 (void) snprintf(path, sizeof (pathbuf),
861 ztest_aux_template, ztest_opts.zo_dir,
862 pool == NULL ? ztest_opts.zo_pool : pool,
865 vdev = ztest_shared->zs_vdev_next_leaf++;
866 (void) snprintf(path, sizeof (pathbuf),
867 ztest_dev_template, ztest_opts.zo_dir,
868 pool == NULL ? ztest_opts.zo_pool : pool, vdev);
873 int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
875 fatal(1, "can't open %s", path);
876 if (ftruncate(fd, size) != 0)
877 fatal(1, "can't ftruncate %s", path);
881 VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
882 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
883 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
884 VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
890 make_vdev_raidz(char *path, char *aux, char *pool, size_t size,
891 uint64_t ashift, int r)
893 nvlist_t *raidz, **child;
897 return (make_vdev_file(path, aux, pool, size, ashift));
898 child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
900 for (c = 0; c < r; c++)
901 child[c] = make_vdev_file(path, aux, pool, size, ashift);
903 VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
904 VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
905 VDEV_TYPE_RAIDZ) == 0);
906 VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
907 ztest_opts.zo_raidz_parity) == 0);
908 VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
911 for (c = 0; c < r; c++)
912 nvlist_free(child[c]);
914 umem_free(child, r * sizeof (nvlist_t *));
920 make_vdev_mirror(char *path, char *aux, char *pool, size_t size,
921 uint64_t ashift, int r, int m)
923 nvlist_t *mirror, **child;
927 return (make_vdev_raidz(path, aux, pool, size, ashift, r));
929 child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
931 for (c = 0; c < m; c++)
932 child[c] = make_vdev_raidz(path, aux, pool, size, ashift, r);
934 VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
935 VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
936 VDEV_TYPE_MIRROR) == 0);
937 VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
940 for (c = 0; c < m; c++)
941 nvlist_free(child[c]);
943 umem_free(child, m * sizeof (nvlist_t *));
949 make_vdev_root(char *path, char *aux, char *pool, size_t size, uint64_t ashift,
950 int log, int r, int m, int t)
952 nvlist_t *root, **child;
957 child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
959 for (c = 0; c < t; c++) {
960 child[c] = make_vdev_mirror(path, aux, pool, size, ashift,
962 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
966 VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
967 VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
968 VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
971 for (c = 0; c < t; c++)
972 nvlist_free(child[c]);
974 umem_free(child, t * sizeof (nvlist_t *));
980 * Find a random spa version. Returns back a random spa version in the
981 * range [initial_version, SPA_VERSION_FEATURES].
984 ztest_random_spa_version(uint64_t initial_version)
986 uint64_t version = initial_version;
988 if (version <= SPA_VERSION_BEFORE_FEATURES) {
990 ztest_random(SPA_VERSION_BEFORE_FEATURES - version + 1);
993 if (version > SPA_VERSION_BEFORE_FEATURES)
994 version = SPA_VERSION_FEATURES;
996 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
1001 ztest_random_blocksize(void)
1003 uint64_t block_shift;
1005 * Choose a block size >= the ashift.
1006 * If the SPA supports new MAXBLOCKSIZE, test up to 1MB blocks.
1008 int maxbs = SPA_OLD_MAXBLOCKSHIFT;
1009 if (spa_maxblocksize(ztest_spa) == SPA_MAXBLOCKSIZE)
1011 block_shift = ztest_random(maxbs - ztest_spa->spa_max_ashift + 1);
1012 return (1 << (SPA_MINBLOCKSHIFT + block_shift));
1016 ztest_random_dnodesize(void)
1019 int max_slots = spa_maxdnodesize(ztest_spa) >> DNODE_SHIFT;
1021 if (max_slots == DNODE_MIN_SLOTS)
1022 return (DNODE_MIN_SIZE);
1025 * Weight the random distribution more heavily toward smaller
1026 * dnode sizes since that is more likely to reflect real-world
1029 ASSERT3U(max_slots, >, 4);
1030 switch (ztest_random(10)) {
1032 slots = 5 + ztest_random(max_slots - 4);
1035 slots = 2 + ztest_random(3);
1042 return (slots << DNODE_SHIFT);
1046 ztest_random_ibshift(void)
1048 return (DN_MIN_INDBLKSHIFT +
1049 ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1));
1053 ztest_random_vdev_top(spa_t *spa, boolean_t log_ok)
1056 vdev_t *rvd = spa->spa_root_vdev;
1059 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1062 top = ztest_random(rvd->vdev_children);
1063 tvd = rvd->vdev_child[top];
1064 } while (!vdev_is_concrete(tvd) || (tvd->vdev_islog && !log_ok) ||
1065 tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL);
1071 ztest_random_dsl_prop(zfs_prop_t prop)
1076 value = zfs_prop_random_value(prop, ztest_random(-1ULL));
1077 } while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF);
1083 ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value,
1086 const char *propname = zfs_prop_to_name(prop);
1087 const char *valname;
1088 char setpoint[MAXPATHLEN];
1092 error = dsl_prop_set_int(osname, propname,
1093 (inherit ? ZPROP_SRC_NONE : ZPROP_SRC_LOCAL), value);
1095 if (error == ENOSPC) {
1096 ztest_record_enospc(FTAG);
1101 VERIFY0(dsl_prop_get_integer(osname, propname, &curval, setpoint));
1103 if (ztest_opts.zo_verbose >= 6) {
1104 VERIFY(zfs_prop_index_to_string(prop, curval, &valname) == 0);
1105 (void) printf("%s %s = %s at '%s'\n",
1106 osname, propname, valname, setpoint);
1113 ztest_spa_prop_set_uint64(zpool_prop_t prop, uint64_t value)
1115 spa_t *spa = ztest_spa;
1116 nvlist_t *props = NULL;
1119 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
1120 VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0);
1122 error = spa_prop_set(spa, props);
1126 if (error == ENOSPC) {
1127 ztest_record_enospc(FTAG);
1136 ztest_rll_init(rll_t *rll)
1138 rll->rll_writer = NULL;
1139 rll->rll_readers = 0;
1140 mutex_init(&rll->rll_lock, NULL, USYNC_THREAD, NULL);
1141 cv_init(&rll->rll_cv, NULL, USYNC_THREAD, NULL);
1145 ztest_rll_destroy(rll_t *rll)
1147 ASSERT(rll->rll_writer == NULL);
1148 ASSERT(rll->rll_readers == 0);
1149 mutex_destroy(&rll->rll_lock);
1150 cv_destroy(&rll->rll_cv);
1154 ztest_rll_lock(rll_t *rll, rl_type_t type)
1156 mutex_enter(&rll->rll_lock);
1158 if (type == RL_READER) {
1159 while (rll->rll_writer != NULL)
1160 cv_wait(&rll->rll_cv, &rll->rll_lock);
1163 while (rll->rll_writer != NULL || rll->rll_readers)
1164 cv_wait(&rll->rll_cv, &rll->rll_lock);
1165 rll->rll_writer = curthread;
1168 mutex_exit(&rll->rll_lock);
1172 ztest_rll_unlock(rll_t *rll)
1174 mutex_enter(&rll->rll_lock);
1176 if (rll->rll_writer) {
1177 ASSERT(rll->rll_readers == 0);
1178 rll->rll_writer = NULL;
1180 ASSERT(rll->rll_readers != 0);
1181 ASSERT(rll->rll_writer == NULL);
1185 if (rll->rll_writer == NULL && rll->rll_readers == 0)
1186 cv_broadcast(&rll->rll_cv);
1188 mutex_exit(&rll->rll_lock);
1192 ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type)
1194 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1196 ztest_rll_lock(rll, type);
1200 ztest_object_unlock(ztest_ds_t *zd, uint64_t object)
1202 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1204 ztest_rll_unlock(rll);
1208 ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset,
1209 uint64_t size, rl_type_t type)
1211 uint64_t hash = object ^ (offset % (ZTEST_RANGE_LOCKS + 1));
1212 rll_t *rll = &zd->zd_range_lock[hash & (ZTEST_RANGE_LOCKS - 1)];
1215 rl = umem_alloc(sizeof (*rl), UMEM_NOFAIL);
1216 rl->rl_object = object;
1217 rl->rl_offset = offset;
1221 ztest_rll_lock(rll, type);
1227 ztest_range_unlock(rl_t *rl)
1229 rll_t *rll = rl->rl_lock;
1231 ztest_rll_unlock(rll);
1233 umem_free(rl, sizeof (*rl));
1237 ztest_zd_init(ztest_ds_t *zd, ztest_shared_ds_t *szd, objset_t *os)
1240 zd->zd_zilog = dmu_objset_zil(os);
1241 zd->zd_shared = szd;
1242 dmu_objset_name(os, zd->zd_name);
1244 if (zd->zd_shared != NULL)
1245 zd->zd_shared->zd_seq = 0;
1247 rw_init(&zd->zd_zilog_lock, NULL, USYNC_THREAD, NULL);
1248 mutex_init(&zd->zd_dirobj_lock, NULL, USYNC_THREAD, NULL);
1250 for (int l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1251 ztest_rll_init(&zd->zd_object_lock[l]);
1253 for (int l = 0; l < ZTEST_RANGE_LOCKS; l++)
1254 ztest_rll_init(&zd->zd_range_lock[l]);
1258 ztest_zd_fini(ztest_ds_t *zd)
1260 mutex_destroy(&zd->zd_dirobj_lock);
1262 for (int l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1263 ztest_rll_destroy(&zd->zd_object_lock[l]);
1265 for (int l = 0; l < ZTEST_RANGE_LOCKS; l++)
1266 ztest_rll_destroy(&zd->zd_range_lock[l]);
1269 #define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1272 ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag)
1278 * Attempt to assign tx to some transaction group.
1280 error = dmu_tx_assign(tx, txg_how);
1282 if (error == ERESTART) {
1283 ASSERT(txg_how == TXG_NOWAIT);
1286 ASSERT3U(error, ==, ENOSPC);
1287 ztest_record_enospc(tag);
1292 txg = dmu_tx_get_txg(tx);
1298 ztest_pattern_set(void *buf, uint64_t size, uint64_t value)
1301 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1308 ztest_pattern_match(void *buf, uint64_t size, uint64_t value)
1311 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1315 diff |= (value - *ip++);
1321 ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1322 uint64_t dnodesize, uint64_t offset, uint64_t gen, uint64_t txg,
1325 bt->bt_magic = BT_MAGIC;
1326 bt->bt_objset = dmu_objset_id(os);
1327 bt->bt_object = object;
1328 bt->bt_dnodesize = dnodesize;
1329 bt->bt_offset = offset;
1332 bt->bt_crtxg = crtxg;
1336 ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1337 uint64_t dnodesize, uint64_t offset, uint64_t gen, uint64_t txg,
1340 ASSERT3U(bt->bt_magic, ==, BT_MAGIC);
1341 ASSERT3U(bt->bt_objset, ==, dmu_objset_id(os));
1342 ASSERT3U(bt->bt_object, ==, object);
1343 ASSERT3U(bt->bt_dnodesize, ==, dnodesize);
1344 ASSERT3U(bt->bt_offset, ==, offset);
1345 ASSERT3U(bt->bt_gen, <=, gen);
1346 ASSERT3U(bt->bt_txg, <=, txg);
1347 ASSERT3U(bt->bt_crtxg, ==, crtxg);
1350 static ztest_block_tag_t *
1351 ztest_bt_bonus(dmu_buf_t *db)
1353 dmu_object_info_t doi;
1354 ztest_block_tag_t *bt;
1356 dmu_object_info_from_db(db, &doi);
1357 ASSERT3U(doi.doi_bonus_size, <=, db->db_size);
1358 ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt));
1359 bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt));
1365 * Generate a token to fill up unused bonus buffer space. Try to make
1366 * it unique to the object, generation, and offset to verify that data
1367 * is not getting overwritten by data from other dnodes.
1369 #define ZTEST_BONUS_FILL_TOKEN(obj, ds, gen, offset) \
1370 (((ds) << 48) | ((gen) << 32) | ((obj) << 8) | (offset))
1373 * Fill up the unused bonus buffer region before the block tag with a
1374 * verifiable pattern. Filling the whole bonus area with non-zero data
1375 * helps ensure that all dnode traversal code properly skips the
1376 * interior regions of large dnodes.
1379 ztest_fill_unused_bonus(dmu_buf_t *db, void *end, uint64_t obj,
1380 objset_t *os, uint64_t gen)
1384 ASSERT(IS_P2ALIGNED((char *)end - (char *)db->db_data, 8));
1386 for (bonusp = db->db_data; bonusp < (uint64_t *)end; bonusp++) {
1387 uint64_t token = ZTEST_BONUS_FILL_TOKEN(obj, dmu_objset_id(os),
1388 gen, bonusp - (uint64_t *)db->db_data);
1394 * Verify that the unused area of a bonus buffer is filled with the
1398 ztest_verify_unused_bonus(dmu_buf_t *db, void *end, uint64_t obj,
1399 objset_t *os, uint64_t gen)
1403 for (bonusp = db->db_data; bonusp < (uint64_t *)end; bonusp++) {
1404 uint64_t token = ZTEST_BONUS_FILL_TOKEN(obj, dmu_objset_id(os),
1405 gen, bonusp - (uint64_t *)db->db_data);
1406 VERIFY3U(*bonusp, ==, token);
1414 #define lrz_type lr_mode
1415 #define lrz_blocksize lr_uid
1416 #define lrz_ibshift lr_gid
1417 #define lrz_bonustype lr_rdev
1418 #define lrz_dnodesize lr_crtime[1]
1421 ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr)
1423 char *name = (void *)(lr + 1); /* name follows lr */
1424 size_t namesize = strlen(name) + 1;
1427 if (zil_replaying(zd->zd_zilog, tx))
1430 itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize);
1431 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1432 sizeof (*lr) + namesize - sizeof (lr_t));
1434 zil_itx_assign(zd->zd_zilog, itx, tx);
1438 ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr, uint64_t object)
1440 char *name = (void *)(lr + 1); /* name follows lr */
1441 size_t namesize = strlen(name) + 1;
1444 if (zil_replaying(zd->zd_zilog, tx))
1447 itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize);
1448 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1449 sizeof (*lr) + namesize - sizeof (lr_t));
1451 itx->itx_oid = object;
1452 zil_itx_assign(zd->zd_zilog, itx, tx);
1456 ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr)
1459 itx_wr_state_t write_state = ztest_random(WR_NUM_STATES);
1461 if (zil_replaying(zd->zd_zilog, tx))
1464 if (lr->lr_length > ZIL_MAX_LOG_DATA)
1465 write_state = WR_INDIRECT;
1467 itx = zil_itx_create(TX_WRITE,
1468 sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0));
1470 if (write_state == WR_COPIED &&
1471 dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length,
1472 ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) {
1473 zil_itx_destroy(itx);
1474 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1475 write_state = WR_NEED_COPY;
1477 itx->itx_private = zd;
1478 itx->itx_wr_state = write_state;
1479 itx->itx_sync = (ztest_random(8) == 0);
1481 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1482 sizeof (*lr) - sizeof (lr_t));
1484 zil_itx_assign(zd->zd_zilog, itx, tx);
1488 ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr)
1492 if (zil_replaying(zd->zd_zilog, tx))
1495 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1496 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1497 sizeof (*lr) - sizeof (lr_t));
1499 itx->itx_sync = B_FALSE;
1500 zil_itx_assign(zd->zd_zilog, itx, tx);
1504 ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr)
1508 if (zil_replaying(zd->zd_zilog, tx))
1511 itx = zil_itx_create(TX_SETATTR, sizeof (*lr));
1512 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1513 sizeof (*lr) - sizeof (lr_t));
1515 itx->itx_sync = B_FALSE;
1516 zil_itx_assign(zd->zd_zilog, itx, tx);
1523 ztest_replay_create(void *arg1, void *arg2, boolean_t byteswap)
1525 ztest_ds_t *zd = arg1;
1526 lr_create_t *lr = arg2;
1527 char *name = (void *)(lr + 1); /* name follows lr */
1528 objset_t *os = zd->zd_os;
1529 ztest_block_tag_t *bbt;
1537 byteswap_uint64_array(lr, sizeof (*lr));
1539 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1540 ASSERT(name[0] != '\0');
1542 tx = dmu_tx_create(os);
1544 dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name);
1546 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1547 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1549 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1552 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1556 ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid);
1557 bonuslen = DN_BONUS_SIZE(lr->lrz_dnodesize);
1559 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1560 if (lr->lr_foid == 0) {
1561 lr->lr_foid = zap_create_dnsize(os,
1562 lr->lrz_type, lr->lrz_bonustype,
1563 bonuslen, lr->lrz_dnodesize, tx);
1565 error = zap_create_claim_dnsize(os, lr->lr_foid,
1566 lr->lrz_type, lr->lrz_bonustype,
1567 bonuslen, lr->lrz_dnodesize, tx);
1570 if (lr->lr_foid == 0) {
1571 lr->lr_foid = dmu_object_alloc_dnsize(os,
1572 lr->lrz_type, 0, lr->lrz_bonustype,
1573 bonuslen, lr->lrz_dnodesize, tx);
1575 error = dmu_object_claim_dnsize(os, lr->lr_foid,
1576 lr->lrz_type, 0, lr->lrz_bonustype,
1577 bonuslen, lr->lrz_dnodesize, tx);
1582 ASSERT3U(error, ==, EEXIST);
1583 ASSERT(zd->zd_zilog->zl_replay);
1588 ASSERT(lr->lr_foid != 0);
1590 if (lr->lrz_type != DMU_OT_ZAP_OTHER)
1591 VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid,
1592 lr->lrz_blocksize, lr->lrz_ibshift, tx));
1594 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1595 bbt = ztest_bt_bonus(db);
1596 dmu_buf_will_dirty(db, tx);
1597 ztest_bt_generate(bbt, os, lr->lr_foid, lr->lrz_dnodesize, -1ULL,
1598 lr->lr_gen, txg, txg);
1599 ztest_fill_unused_bonus(db, bbt, lr->lr_foid, os, lr->lr_gen);
1600 dmu_buf_rele(db, FTAG);
1602 VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1,
1605 (void) ztest_log_create(zd, tx, lr);
1613 ztest_replay_remove(void *arg1, void *arg2, boolean_t byteswap)
1615 ztest_ds_t *zd = arg1;
1616 lr_remove_t *lr = arg2;
1617 char *name = (void *)(lr + 1); /* name follows lr */
1618 objset_t *os = zd->zd_os;
1619 dmu_object_info_t doi;
1621 uint64_t object, txg;
1624 byteswap_uint64_array(lr, sizeof (*lr));
1626 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1627 ASSERT(name[0] != '\0');
1630 zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object));
1631 ASSERT(object != 0);
1633 ztest_object_lock(zd, object, RL_WRITER);
1635 VERIFY3U(0, ==, dmu_object_info(os, object, &doi));
1637 tx = dmu_tx_create(os);
1639 dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name);
1640 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1642 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1644 ztest_object_unlock(zd, object);
1648 if (doi.doi_type == DMU_OT_ZAP_OTHER) {
1649 VERIFY3U(0, ==, zap_destroy(os, object, tx));
1651 VERIFY3U(0, ==, dmu_object_free(os, object, tx));
1654 VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx));
1656 (void) ztest_log_remove(zd, tx, lr, object);
1660 ztest_object_unlock(zd, object);
1666 ztest_replay_write(void *arg1, void *arg2, boolean_t byteswap)
1668 ztest_ds_t *zd = arg1;
1669 lr_write_t *lr = arg2;
1670 objset_t *os = zd->zd_os;
1671 void *data = lr + 1; /* data follows lr */
1672 uint64_t offset, length;
1673 ztest_block_tag_t *bt = data;
1674 ztest_block_tag_t *bbt;
1675 uint64_t gen, txg, lrtxg, crtxg;
1676 dmu_object_info_t doi;
1679 arc_buf_t *abuf = NULL;
1683 byteswap_uint64_array(lr, sizeof (*lr));
1685 offset = lr->lr_offset;
1686 length = lr->lr_length;
1688 /* If it's a dmu_sync() block, write the whole block */
1689 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
1690 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
1691 if (length < blocksize) {
1692 offset -= offset % blocksize;
1697 if (bt->bt_magic == BSWAP_64(BT_MAGIC))
1698 byteswap_uint64_array(bt, sizeof (*bt));
1700 if (bt->bt_magic != BT_MAGIC)
1703 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1704 rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER);
1706 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1708 dmu_object_info_from_db(db, &doi);
1710 bbt = ztest_bt_bonus(db);
1711 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1713 crtxg = bbt->bt_crtxg;
1714 lrtxg = lr->lr_common.lrc_txg;
1716 tx = dmu_tx_create(os);
1718 dmu_tx_hold_write(tx, lr->lr_foid, offset, length);
1720 if (ztest_random(8) == 0 && length == doi.doi_data_block_size &&
1721 P2PHASE(offset, length) == 0)
1722 abuf = dmu_request_arcbuf(db, length);
1724 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1727 dmu_return_arcbuf(abuf);
1728 dmu_buf_rele(db, FTAG);
1729 ztest_range_unlock(rl);
1730 ztest_object_unlock(zd, lr->lr_foid);
1736 * Usually, verify the old data before writing new data --
1737 * but not always, because we also want to verify correct
1738 * behavior when the data was not recently read into cache.
1740 ASSERT(offset % doi.doi_data_block_size == 0);
1741 if (ztest_random(4) != 0) {
1742 int prefetch = ztest_random(2) ?
1743 DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
1744 ztest_block_tag_t rbt;
1746 VERIFY(dmu_read(os, lr->lr_foid, offset,
1747 sizeof (rbt), &rbt, prefetch) == 0);
1748 if (rbt.bt_magic == BT_MAGIC) {
1749 ztest_bt_verify(&rbt, os, lr->lr_foid, 0,
1750 offset, gen, txg, crtxg);
1755 * Writes can appear to be newer than the bonus buffer because
1756 * the ztest_get_data() callback does a dmu_read() of the
1757 * open-context data, which may be different than the data
1758 * as it was when the write was generated.
1760 if (zd->zd_zilog->zl_replay) {
1761 ztest_bt_verify(bt, os, lr->lr_foid, 0, offset,
1762 MAX(gen, bt->bt_gen), MAX(txg, lrtxg),
1767 * Set the bt's gen/txg to the bonus buffer's gen/txg
1768 * so that all of the usual ASSERTs will work.
1770 ztest_bt_generate(bt, os, lr->lr_foid, 0, offset, gen, txg,
1775 dmu_write(os, lr->lr_foid, offset, length, data, tx);
1777 bcopy(data, abuf->b_data, length);
1778 dmu_assign_arcbuf(db, offset, abuf, tx);
1781 (void) ztest_log_write(zd, tx, lr);
1783 dmu_buf_rele(db, FTAG);
1787 ztest_range_unlock(rl);
1788 ztest_object_unlock(zd, lr->lr_foid);
1794 ztest_replay_truncate(void *arg1, void *arg2, boolean_t byteswap)
1796 ztest_ds_t *zd = arg1;
1797 lr_truncate_t *lr = arg2;
1798 objset_t *os = zd->zd_os;
1804 byteswap_uint64_array(lr, sizeof (*lr));
1806 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1807 rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length,
1810 tx = dmu_tx_create(os);
1812 dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length);
1814 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1816 ztest_range_unlock(rl);
1817 ztest_object_unlock(zd, lr->lr_foid);
1821 VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset,
1822 lr->lr_length, tx) == 0);
1824 (void) ztest_log_truncate(zd, tx, lr);
1828 ztest_range_unlock(rl);
1829 ztest_object_unlock(zd, lr->lr_foid);
1835 ztest_replay_setattr(void *arg1, void *arg2, boolean_t byteswap)
1837 ztest_ds_t *zd = arg1;
1838 lr_setattr_t *lr = arg2;
1839 objset_t *os = zd->zd_os;
1842 ztest_block_tag_t *bbt;
1843 uint64_t txg, lrtxg, crtxg, dnodesize;
1846 byteswap_uint64_array(lr, sizeof (*lr));
1848 ztest_object_lock(zd, lr->lr_foid, RL_WRITER);
1850 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1852 tx = dmu_tx_create(os);
1853 dmu_tx_hold_bonus(tx, lr->lr_foid);
1855 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1857 dmu_buf_rele(db, FTAG);
1858 ztest_object_unlock(zd, lr->lr_foid);
1862 bbt = ztest_bt_bonus(db);
1863 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1864 crtxg = bbt->bt_crtxg;
1865 lrtxg = lr->lr_common.lrc_txg;
1866 dnodesize = bbt->bt_dnodesize;
1868 if (zd->zd_zilog->zl_replay) {
1869 ASSERT(lr->lr_size != 0);
1870 ASSERT(lr->lr_mode != 0);
1874 * Randomly change the size and increment the generation.
1876 lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) *
1878 lr->lr_mode = bbt->bt_gen + 1;
1883 * Verify that the current bonus buffer is not newer than our txg.
1885 ztest_bt_verify(bbt, os, lr->lr_foid, dnodesize, -1ULL, lr->lr_mode,
1886 MAX(txg, lrtxg), crtxg);
1888 dmu_buf_will_dirty(db, tx);
1890 ASSERT3U(lr->lr_size, >=, sizeof (*bbt));
1891 ASSERT3U(lr->lr_size, <=, db->db_size);
1892 VERIFY0(dmu_set_bonus(db, lr->lr_size, tx));
1893 bbt = ztest_bt_bonus(db);
1895 ztest_bt_generate(bbt, os, lr->lr_foid, dnodesize, -1ULL, lr->lr_mode,
1897 ztest_fill_unused_bonus(db, bbt, lr->lr_foid, os, bbt->bt_gen);
1898 dmu_buf_rele(db, FTAG);
1900 (void) ztest_log_setattr(zd, tx, lr);
1904 ztest_object_unlock(zd, lr->lr_foid);
1909 zil_replay_func_t *ztest_replay_vector[TX_MAX_TYPE] = {
1910 NULL, /* 0 no such transaction type */
1911 ztest_replay_create, /* TX_CREATE */
1912 NULL, /* TX_MKDIR */
1913 NULL, /* TX_MKXATTR */
1914 NULL, /* TX_SYMLINK */
1915 ztest_replay_remove, /* TX_REMOVE */
1916 NULL, /* TX_RMDIR */
1918 NULL, /* TX_RENAME */
1919 ztest_replay_write, /* TX_WRITE */
1920 ztest_replay_truncate, /* TX_TRUNCATE */
1921 ztest_replay_setattr, /* TX_SETATTR */
1923 NULL, /* TX_CREATE_ACL */
1924 NULL, /* TX_CREATE_ATTR */
1925 NULL, /* TX_CREATE_ACL_ATTR */
1926 NULL, /* TX_MKDIR_ACL */
1927 NULL, /* TX_MKDIR_ATTR */
1928 NULL, /* TX_MKDIR_ACL_ATTR */
1929 NULL, /* TX_WRITE2 */
1933 * ZIL get_data callbacks
1937 ztest_get_done(zgd_t *zgd, int error)
1939 ztest_ds_t *zd = zgd->zgd_private;
1940 uint64_t object = zgd->zgd_rl->rl_object;
1943 dmu_buf_rele(zgd->zgd_db, zgd);
1945 ztest_range_unlock(zgd->zgd_rl);
1946 ztest_object_unlock(zd, object);
1948 if (error == 0 && zgd->zgd_bp)
1949 zil_lwb_add_block(zgd->zgd_lwb, zgd->zgd_bp);
1951 umem_free(zgd, sizeof (*zgd));
1955 ztest_get_data(void *arg, lr_write_t *lr, char *buf, struct lwb *lwb,
1958 ztest_ds_t *zd = arg;
1959 objset_t *os = zd->zd_os;
1960 uint64_t object = lr->lr_foid;
1961 uint64_t offset = lr->lr_offset;
1962 uint64_t size = lr->lr_length;
1963 uint64_t txg = lr->lr_common.lrc_txg;
1965 dmu_object_info_t doi;
1970 ASSERT3P(lwb, !=, NULL);
1971 ASSERT3P(zio, !=, NULL);
1972 ASSERT3U(size, !=, 0);
1974 ztest_object_lock(zd, object, RL_READER);
1975 error = dmu_bonus_hold(os, object, FTAG, &db);
1977 ztest_object_unlock(zd, object);
1981 crtxg = ztest_bt_bonus(db)->bt_crtxg;
1983 if (crtxg == 0 || crtxg > txg) {
1984 dmu_buf_rele(db, FTAG);
1985 ztest_object_unlock(zd, object);
1989 dmu_object_info_from_db(db, &doi);
1990 dmu_buf_rele(db, FTAG);
1993 zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL);
1995 zgd->zgd_private = zd;
1997 if (buf != NULL) { /* immediate write */
1998 zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
2001 error = dmu_read(os, object, offset, size, buf,
2002 DMU_READ_NO_PREFETCH);
2005 size = doi.doi_data_block_size;
2007 offset = P2ALIGN(offset, size);
2009 ASSERT(offset < size);
2013 zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
2016 error = dmu_buf_hold(os, object, offset, zgd, &db,
2017 DMU_READ_NO_PREFETCH);
2020 blkptr_t *bp = &lr->lr_blkptr;
2025 ASSERT(db->db_offset == offset);
2026 ASSERT(db->db_size == size);
2028 error = dmu_sync(zio, lr->lr_common.lrc_txg,
2029 ztest_get_done, zgd);
2036 ztest_get_done(zgd, error);
2042 ztest_lr_alloc(size_t lrsize, char *name)
2045 size_t namesize = name ? strlen(name) + 1 : 0;
2047 lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
2050 bcopy(name, lr + lrsize, namesize);
2056 ztest_lr_free(void *lr, size_t lrsize, char *name)
2058 size_t namesize = name ? strlen(name) + 1 : 0;
2060 umem_free(lr, lrsize + namesize);
2064 * Lookup a bunch of objects. Returns the number of objects not found.
2067 ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
2072 ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
2074 for (int i = 0; i < count; i++, od++) {
2076 error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
2077 sizeof (uint64_t), 1, &od->od_object);
2079 ASSERT(error == ENOENT);
2080 ASSERT(od->od_object == 0);
2084 ztest_block_tag_t *bbt;
2085 dmu_object_info_t doi;
2087 ASSERT(od->od_object != 0);
2088 ASSERT(missing == 0); /* there should be no gaps */
2090 ztest_object_lock(zd, od->od_object, RL_READER);
2091 VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
2092 od->od_object, FTAG, &db));
2093 dmu_object_info_from_db(db, &doi);
2094 bbt = ztest_bt_bonus(db);
2095 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
2096 od->od_type = doi.doi_type;
2097 od->od_blocksize = doi.doi_data_block_size;
2098 od->od_gen = bbt->bt_gen;
2099 dmu_buf_rele(db, FTAG);
2100 ztest_object_unlock(zd, od->od_object);
2108 ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
2112 ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
2114 for (int i = 0; i < count; i++, od++) {
2121 lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2123 lr->lr_doid = od->od_dir;
2124 lr->lr_foid = 0; /* 0 to allocate, > 0 to claim */
2125 lr->lrz_type = od->od_crtype;
2126 lr->lrz_blocksize = od->od_crblocksize;
2127 lr->lrz_ibshift = ztest_random_ibshift();
2128 lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
2129 lr->lrz_dnodesize = od->od_crdnodesize;
2130 lr->lr_gen = od->od_crgen;
2131 lr->lr_crtime[0] = time(NULL);
2133 if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
2134 ASSERT(missing == 0);
2138 od->od_object = lr->lr_foid;
2139 od->od_type = od->od_crtype;
2140 od->od_blocksize = od->od_crblocksize;
2141 od->od_gen = od->od_crgen;
2142 ASSERT(od->od_object != 0);
2145 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2152 ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
2157 ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
2161 for (int i = count - 1; i >= 0; i--, od--) {
2168 * No object was found.
2170 if (od->od_object == 0)
2173 lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2175 lr->lr_doid = od->od_dir;
2177 if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
2178 ASSERT3U(error, ==, ENOSPC);
2183 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2190 ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
2196 lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
2198 lr->lr_foid = object;
2199 lr->lr_offset = offset;
2200 lr->lr_length = size;
2202 BP_ZERO(&lr->lr_blkptr);
2204 bcopy(data, lr + 1, size);
2206 error = ztest_replay_write(zd, lr, B_FALSE);
2208 ztest_lr_free(lr, sizeof (*lr) + size, NULL);
2214 ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2219 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2221 lr->lr_foid = object;
2222 lr->lr_offset = offset;
2223 lr->lr_length = size;
2225 error = ztest_replay_truncate(zd, lr, B_FALSE);
2227 ztest_lr_free(lr, sizeof (*lr), NULL);
2233 ztest_setattr(ztest_ds_t *zd, uint64_t object)
2238 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2240 lr->lr_foid = object;
2244 error = ztest_replay_setattr(zd, lr, B_FALSE);
2246 ztest_lr_free(lr, sizeof (*lr), NULL);
2252 ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2254 objset_t *os = zd->zd_os;
2259 txg_wait_synced(dmu_objset_pool(os), 0);
2261 ztest_object_lock(zd, object, RL_READER);
2262 rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
2264 tx = dmu_tx_create(os);
2266 dmu_tx_hold_write(tx, object, offset, size);
2268 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
2271 dmu_prealloc(os, object, offset, size, tx);
2273 txg_wait_synced(dmu_objset_pool(os), txg);
2275 (void) dmu_free_long_range(os, object, offset, size);
2278 ztest_range_unlock(rl);
2279 ztest_object_unlock(zd, object);
2283 ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
2286 ztest_block_tag_t wbt;
2287 dmu_object_info_t doi;
2288 enum ztest_io_type io_type;
2292 VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
2293 blocksize = doi.doi_data_block_size;
2294 data = umem_alloc(blocksize, UMEM_NOFAIL);
2297 * Pick an i/o type at random, biased toward writing block tags.
2299 io_type = ztest_random(ZTEST_IO_TYPES);
2300 if (ztest_random(2) == 0)
2301 io_type = ZTEST_IO_WRITE_TAG;
2303 rw_enter(&zd->zd_zilog_lock, RW_READER);
2307 case ZTEST_IO_WRITE_TAG:
2308 ztest_bt_generate(&wbt, zd->zd_os, object, doi.doi_dnodesize,
2310 (void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
2313 case ZTEST_IO_WRITE_PATTERN:
2314 (void) memset(data, 'a' + (object + offset) % 5, blocksize);
2315 if (ztest_random(2) == 0) {
2317 * Induce fletcher2 collisions to ensure that
2318 * zio_ddt_collision() detects and resolves them
2319 * when using fletcher2-verify for deduplication.
2321 ((uint64_t *)data)[0] ^= 1ULL << 63;
2322 ((uint64_t *)data)[4] ^= 1ULL << 63;
2324 (void) ztest_write(zd, object, offset, blocksize, data);
2327 case ZTEST_IO_WRITE_ZEROES:
2328 bzero(data, blocksize);
2329 (void) ztest_write(zd, object, offset, blocksize, data);
2332 case ZTEST_IO_TRUNCATE:
2333 (void) ztest_truncate(zd, object, offset, blocksize);
2336 case ZTEST_IO_SETATTR:
2337 (void) ztest_setattr(zd, object);
2340 case ZTEST_IO_REWRITE:
2341 rw_enter(&ztest_name_lock, RW_READER);
2342 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2343 ZFS_PROP_CHECKSUM, spa_dedup_checksum(ztest_spa),
2345 VERIFY(err == 0 || err == ENOSPC);
2346 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2347 ZFS_PROP_COMPRESSION,
2348 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION),
2350 VERIFY(err == 0 || err == ENOSPC);
2351 rw_exit(&ztest_name_lock);
2353 VERIFY0(dmu_read(zd->zd_os, object, offset, blocksize, data,
2354 DMU_READ_NO_PREFETCH));
2356 (void) ztest_write(zd, object, offset, blocksize, data);
2360 rw_exit(&zd->zd_zilog_lock);
2362 umem_free(data, blocksize);
2366 * Initialize an object description template.
2369 ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
2370 dmu_object_type_t type, uint64_t blocksize, uint64_t dnodesize,
2373 od->od_dir = ZTEST_DIROBJ;
2376 od->od_crtype = type;
2377 od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
2378 od->od_crdnodesize = dnodesize ? dnodesize : ztest_random_dnodesize();
2381 od->od_type = DMU_OT_NONE;
2382 od->od_blocksize = 0;
2385 (void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
2386 tag, (int64_t)id, index);
2390 * Lookup or create the objects for a test using the od template.
2391 * If the objects do not all exist, or if 'remove' is specified,
2392 * remove any existing objects and create new ones. Otherwise,
2393 * use the existing objects.
2396 ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
2398 int count = size / sizeof (*od);
2401 mutex_enter(&zd->zd_dirobj_lock);
2402 if ((ztest_lookup(zd, od, count) != 0 || remove) &&
2403 (ztest_remove(zd, od, count) != 0 ||
2404 ztest_create(zd, od, count) != 0))
2407 mutex_exit(&zd->zd_dirobj_lock);
2414 ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
2416 zilog_t *zilog = zd->zd_zilog;
2418 rw_enter(&zd->zd_zilog_lock, RW_READER);
2420 zil_commit(zilog, ztest_random(ZTEST_OBJECTS));
2423 * Remember the committed values in zd, which is in parent/child
2424 * shared memory. If we die, the next iteration of ztest_run()
2425 * will verify that the log really does contain this record.
2427 mutex_enter(&zilog->zl_lock);
2428 ASSERT(zd->zd_shared != NULL);
2429 ASSERT3U(zd->zd_shared->zd_seq, <=, zilog->zl_commit_lr_seq);
2430 zd->zd_shared->zd_seq = zilog->zl_commit_lr_seq;
2431 mutex_exit(&zilog->zl_lock);
2433 rw_exit(&zd->zd_zilog_lock);
2437 * This function is designed to simulate the operations that occur during a
2438 * mount/unmount operation. We hold the dataset across these operations in an
2439 * attempt to expose any implicit assumptions about ZIL management.
2443 ztest_zil_remount(ztest_ds_t *zd, uint64_t id)
2445 objset_t *os = zd->zd_os;
2448 * We grab the zd_dirobj_lock to ensure that no other thread is
2449 * updating the zil (i.e. adding in-memory log records) and the
2450 * zd_zilog_lock to block any I/O.
2452 mutex_enter(&zd->zd_dirobj_lock);
2453 rw_enter(&zd->zd_zilog_lock, RW_WRITER);
2455 /* zfsvfs_teardown() */
2456 zil_close(zd->zd_zilog);
2458 /* zfsvfs_setup() */
2459 VERIFY(zil_open(os, ztest_get_data) == zd->zd_zilog);
2460 zil_replay(os, zd, ztest_replay_vector);
2462 rw_exit(&zd->zd_zilog_lock);
2463 mutex_exit(&zd->zd_dirobj_lock);
2467 * Verify that we can't destroy an active pool, create an existing pool,
2468 * or create a pool with a bad vdev spec.
2472 ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2474 ztest_shared_opts_t *zo = &ztest_opts;
2479 * Attempt to create using a bad file.
2481 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2482 VERIFY3U(ENOENT, ==,
2483 spa_create("ztest_bad_file", nvroot, NULL, NULL));
2484 nvlist_free(nvroot);
2487 * Attempt to create using a bad mirror.
2489 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 2, 1);
2490 VERIFY3U(ENOENT, ==,
2491 spa_create("ztest_bad_mirror", nvroot, NULL, NULL));
2492 nvlist_free(nvroot);
2495 * Attempt to create an existing pool. It shouldn't matter
2496 * what's in the nvroot; we should fail with EEXIST.
2498 rw_enter(&ztest_name_lock, RW_READER);
2499 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2500 VERIFY3U(EEXIST, ==, spa_create(zo->zo_pool, nvroot, NULL, NULL));
2501 nvlist_free(nvroot);
2502 VERIFY3U(0, ==, spa_open(zo->zo_pool, &spa, FTAG));
2503 VERIFY3U(EBUSY, ==, spa_destroy(zo->zo_pool));
2504 spa_close(spa, FTAG);
2506 rw_exit(&ztest_name_lock);
2511 ztest_spa_upgrade(ztest_ds_t *zd, uint64_t id)
2514 uint64_t initial_version = SPA_VERSION_INITIAL;
2515 uint64_t version, newversion;
2516 nvlist_t *nvroot, *props;
2519 mutex_enter(&ztest_vdev_lock);
2520 name = kmem_asprintf("%s_upgrade", ztest_opts.zo_pool);
2523 * Clean up from previous runs.
2525 (void) spa_destroy(name);
2527 nvroot = make_vdev_root(NULL, NULL, name, ztest_opts.zo_vdev_size, 0,
2528 0, ztest_opts.zo_raidz, ztest_opts.zo_mirrors, 1);
2531 * If we're configuring a RAIDZ device then make sure that the
2532 * the initial version is capable of supporting that feature.
2534 switch (ztest_opts.zo_raidz_parity) {
2537 initial_version = SPA_VERSION_INITIAL;
2540 initial_version = SPA_VERSION_RAIDZ2;
2543 initial_version = SPA_VERSION_RAIDZ3;
2548 * Create a pool with a spa version that can be upgraded. Pick
2549 * a value between initial_version and SPA_VERSION_BEFORE_FEATURES.
2552 version = ztest_random_spa_version(initial_version);
2553 } while (version > SPA_VERSION_BEFORE_FEATURES);
2555 props = fnvlist_alloc();
2556 fnvlist_add_uint64(props,
2557 zpool_prop_to_name(ZPOOL_PROP_VERSION), version);
2558 VERIFY0(spa_create(name, nvroot, props, NULL));
2559 fnvlist_free(nvroot);
2560 fnvlist_free(props);
2562 VERIFY0(spa_open(name, &spa, FTAG));
2563 VERIFY3U(spa_version(spa), ==, version);
2564 newversion = ztest_random_spa_version(version + 1);
2566 if (ztest_opts.zo_verbose >= 4) {
2567 (void) printf("upgrading spa version from %llu to %llu\n",
2568 (u_longlong_t)version, (u_longlong_t)newversion);
2571 spa_upgrade(spa, newversion);
2572 VERIFY3U(spa_version(spa), >, version);
2573 VERIFY3U(spa_version(spa), ==, fnvlist_lookup_uint64(spa->spa_config,
2574 zpool_prop_to_name(ZPOOL_PROP_VERSION)));
2575 spa_close(spa, FTAG);
2578 mutex_exit(&ztest_vdev_lock);
2582 ztest_spa_checkpoint(spa_t *spa)
2584 ASSERT(MUTEX_HELD(&ztest_checkpoint_lock));
2586 int error = spa_checkpoint(spa->spa_name);
2590 case ZFS_ERR_DEVRM_IN_PROGRESS:
2591 case ZFS_ERR_DISCARDING_CHECKPOINT:
2592 case ZFS_ERR_CHECKPOINT_EXISTS:
2595 ztest_record_enospc(FTAG);
2598 fatal(0, "spa_checkpoint(%s) = %d", spa->spa_name, error);
2603 ztest_spa_discard_checkpoint(spa_t *spa)
2605 ASSERT(MUTEX_HELD(&ztest_checkpoint_lock));
2607 int error = spa_checkpoint_discard(spa->spa_name);
2611 case ZFS_ERR_DISCARDING_CHECKPOINT:
2612 case ZFS_ERR_NO_CHECKPOINT:
2615 fatal(0, "spa_discard_checkpoint(%s) = %d",
2616 spa->spa_name, error);
2623 ztest_spa_checkpoint_create_discard(ztest_ds_t *zd, uint64_t id)
2625 spa_t *spa = ztest_spa;
2627 mutex_enter(&ztest_checkpoint_lock);
2628 if (ztest_random(2) == 0) {
2629 ztest_spa_checkpoint(spa);
2631 ztest_spa_discard_checkpoint(spa);
2633 mutex_exit(&ztest_checkpoint_lock);
2638 vdev_lookup_by_path(vdev_t *vd, const char *path)
2642 if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
2645 for (int c = 0; c < vd->vdev_children; c++)
2646 if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
2654 * Find the first available hole which can be used as a top-level.
2657 find_vdev_hole(spa_t *spa)
2659 vdev_t *rvd = spa->spa_root_vdev;
2662 ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
2664 for (c = 0; c < rvd->vdev_children; c++) {
2665 vdev_t *cvd = rvd->vdev_child[c];
2667 if (cvd->vdev_ishole)
2674 * Verify that vdev_add() works as expected.
2678 ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2680 ztest_shared_t *zs = ztest_shared;
2681 spa_t *spa = ztest_spa;
2687 mutex_enter(&ztest_vdev_lock);
2688 leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * ztest_opts.zo_raidz;
2690 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2692 ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2695 * If we have slogs then remove them 1/4 of the time.
2697 if (spa_has_slogs(spa) && ztest_random(4) == 0) {
2699 * Grab the guid from the head of the log class rotor.
2701 guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid;
2703 spa_config_exit(spa, SCL_VDEV, FTAG);
2706 * We have to grab the zs_name_lock as writer to
2707 * prevent a race between removing a slog (dmu_objset_find)
2708 * and destroying a dataset. Removing the slog will
2709 * grab a reference on the dataset which may cause
2710 * dmu_objset_destroy() to fail with EBUSY thus
2711 * leaving the dataset in an inconsistent state.
2713 rw_enter(&ztest_name_lock, RW_WRITER);
2714 error = spa_vdev_remove(spa, guid, B_FALSE);
2715 rw_exit(&ztest_name_lock);
2720 case ZFS_ERR_CHECKPOINT_EXISTS:
2721 case ZFS_ERR_DISCARDING_CHECKPOINT:
2724 fatal(0, "spa_vdev_remove() = %d", error);
2727 spa_config_exit(spa, SCL_VDEV, FTAG);
2730 * Make 1/4 of the devices be log devices.
2732 nvroot = make_vdev_root(NULL, NULL, NULL,
2733 ztest_opts.zo_vdev_size, 0,
2734 ztest_random(4) == 0, ztest_opts.zo_raidz,
2737 error = spa_vdev_add(spa, nvroot);
2738 nvlist_free(nvroot);
2744 ztest_record_enospc("spa_vdev_add");
2747 fatal(0, "spa_vdev_add() = %d", error);
2751 mutex_exit(&ztest_vdev_lock);
2755 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
2759 ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
2761 ztest_shared_t *zs = ztest_shared;
2762 spa_t *spa = ztest_spa;
2763 vdev_t *rvd = spa->spa_root_vdev;
2764 spa_aux_vdev_t *sav;
2769 if (ztest_random(2) == 0) {
2770 sav = &spa->spa_spares;
2771 aux = ZPOOL_CONFIG_SPARES;
2773 sav = &spa->spa_l2cache;
2774 aux = ZPOOL_CONFIG_L2CACHE;
2777 mutex_enter(&ztest_vdev_lock);
2779 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2781 if (sav->sav_count != 0 && ztest_random(4) == 0) {
2783 * Pick a random device to remove.
2785 guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
2788 * Find an unused device we can add.
2790 zs->zs_vdev_aux = 0;
2792 char path[MAXPATHLEN];
2794 (void) snprintf(path, sizeof (path), ztest_aux_template,
2795 ztest_opts.zo_dir, ztest_opts.zo_pool, aux,
2797 for (c = 0; c < sav->sav_count; c++)
2798 if (strcmp(sav->sav_vdevs[c]->vdev_path,
2801 if (c == sav->sav_count &&
2802 vdev_lookup_by_path(rvd, path) == NULL)
2808 spa_config_exit(spa, SCL_VDEV, FTAG);
2814 nvlist_t *nvroot = make_vdev_root(NULL, aux, NULL,
2815 (ztest_opts.zo_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
2816 error = spa_vdev_add(spa, nvroot);
2822 fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
2824 nvlist_free(nvroot);
2827 * Remove an existing device. Sometimes, dirty its
2828 * vdev state first to make sure we handle removal
2829 * of devices that have pending state changes.
2831 if (ztest_random(2) == 0)
2832 (void) vdev_online(spa, guid, 0, NULL);
2834 error = spa_vdev_remove(spa, guid, B_FALSE);
2839 case ZFS_ERR_CHECKPOINT_EXISTS:
2840 case ZFS_ERR_DISCARDING_CHECKPOINT:
2843 fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
2847 mutex_exit(&ztest_vdev_lock);
2851 * split a pool if it has mirror tlvdevs
2855 ztest_split_pool(ztest_ds_t *zd, uint64_t id)
2857 ztest_shared_t *zs = ztest_shared;
2858 spa_t *spa = ztest_spa;
2859 vdev_t *rvd = spa->spa_root_vdev;
2860 nvlist_t *tree, **child, *config, *split, **schild;
2861 uint_t c, children, schildren = 0, lastlogid = 0;
2864 mutex_enter(&ztest_vdev_lock);
2866 /* ensure we have a useable config; mirrors of raidz aren't supported */
2867 if (zs->zs_mirrors < 3 || ztest_opts.zo_raidz > 1) {
2868 mutex_exit(&ztest_vdev_lock);
2872 /* clean up the old pool, if any */
2873 (void) spa_destroy("splitp");
2875 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2877 /* generate a config from the existing config */
2878 mutex_enter(&spa->spa_props_lock);
2879 VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
2881 mutex_exit(&spa->spa_props_lock);
2883 VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2886 schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
2887 for (c = 0; c < children; c++) {
2888 vdev_t *tvd = rvd->vdev_child[c];
2892 if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
2893 VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
2895 VERIFY(nvlist_add_string(schild[schildren],
2896 ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
2897 VERIFY(nvlist_add_uint64(schild[schildren],
2898 ZPOOL_CONFIG_IS_HOLE, 1) == 0);
2900 lastlogid = schildren;
2905 VERIFY(nvlist_lookup_nvlist_array(child[c],
2906 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2907 VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
2910 /* OK, create a config that can be used to split */
2911 VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
2912 VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
2913 VDEV_TYPE_ROOT) == 0);
2914 VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
2915 lastlogid != 0 ? lastlogid : schildren) == 0);
2917 VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
2918 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
2920 for (c = 0; c < schildren; c++)
2921 nvlist_free(schild[c]);
2925 spa_config_exit(spa, SCL_VDEV, FTAG);
2927 rw_enter(&ztest_name_lock, RW_WRITER);
2928 error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
2929 rw_exit(&ztest_name_lock);
2931 nvlist_free(config);
2934 (void) printf("successful split - results:\n");
2935 mutex_enter(&spa_namespace_lock);
2936 show_pool_stats(spa);
2937 show_pool_stats(spa_lookup("splitp"));
2938 mutex_exit(&spa_namespace_lock);
2942 mutex_exit(&ztest_vdev_lock);
2946 * Verify that we can attach and detach devices.
2950 ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
2952 ztest_shared_t *zs = ztest_shared;
2953 spa_t *spa = ztest_spa;
2954 spa_aux_vdev_t *sav = &spa->spa_spares;
2955 vdev_t *rvd = spa->spa_root_vdev;
2956 vdev_t *oldvd, *newvd, *pvd;
2960 uint64_t ashift = ztest_get_ashift();
2961 uint64_t oldguid, pguid;
2962 uint64_t oldsize, newsize;
2963 char oldpath[MAXPATHLEN], newpath[MAXPATHLEN];
2965 int oldvd_has_siblings = B_FALSE;
2966 int newvd_is_spare = B_FALSE;
2968 int error, expected_error;
2970 mutex_enter(&ztest_vdev_lock);
2971 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
2973 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2976 * If a vdev is in the process of being removed, its removal may
2977 * finish while we are in progress, leading to an unexpected error
2978 * value. Don't bother trying to attach while we are in the middle
2981 if (ztest_device_removal_active) {
2982 spa_config_exit(spa, SCL_ALL, FTAG);
2983 mutex_exit(&ztest_vdev_lock);
2988 * Decide whether to do an attach or a replace.
2990 replacing = ztest_random(2);
2993 * Pick a random top-level vdev.
2995 top = ztest_random_vdev_top(spa, B_TRUE);
2998 * Pick a random leaf within it.
3000 leaf = ztest_random(leaves);
3005 oldvd = rvd->vdev_child[top];
3006 if (zs->zs_mirrors >= 1) {
3007 ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
3008 ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
3009 oldvd = oldvd->vdev_child[leaf / ztest_opts.zo_raidz];
3011 if (ztest_opts.zo_raidz > 1) {
3012 ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
3013 ASSERT(oldvd->vdev_children == ztest_opts.zo_raidz);
3014 oldvd = oldvd->vdev_child[leaf % ztest_opts.zo_raidz];
3018 * If we're already doing an attach or replace, oldvd may be a
3019 * mirror vdev -- in which case, pick a random child.
3021 while (oldvd->vdev_children != 0) {
3022 oldvd_has_siblings = B_TRUE;
3023 ASSERT(oldvd->vdev_children >= 2);
3024 oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
3027 oldguid = oldvd->vdev_guid;
3028 oldsize = vdev_get_min_asize(oldvd);
3029 oldvd_is_log = oldvd->vdev_top->vdev_islog;
3030 (void) strcpy(oldpath, oldvd->vdev_path);
3031 pvd = oldvd->vdev_parent;
3032 pguid = pvd->vdev_guid;
3035 * If oldvd has siblings, then half of the time, detach it.
3037 if (oldvd_has_siblings && ztest_random(2) == 0) {
3038 spa_config_exit(spa, SCL_ALL, FTAG);
3039 error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
3040 if (error != 0 && error != ENODEV && error != EBUSY &&
3041 error != ENOTSUP && error != ZFS_ERR_CHECKPOINT_EXISTS &&
3042 error != ZFS_ERR_DISCARDING_CHECKPOINT)
3043 fatal(0, "detach (%s) returned %d", oldpath, error);
3044 mutex_exit(&ztest_vdev_lock);
3049 * For the new vdev, choose with equal probability between the two
3050 * standard paths (ending in either 'a' or 'b') or a random hot spare.
3052 if (sav->sav_count != 0 && ztest_random(3) == 0) {
3053 newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
3054 newvd_is_spare = B_TRUE;
3055 (void) strcpy(newpath, newvd->vdev_path);
3057 (void) snprintf(newpath, sizeof (newpath), ztest_dev_template,
3058 ztest_opts.zo_dir, ztest_opts.zo_pool,
3059 top * leaves + leaf);
3060 if (ztest_random(2) == 0)
3061 newpath[strlen(newpath) - 1] = 'b';
3062 newvd = vdev_lookup_by_path(rvd, newpath);
3067 * Reopen to ensure the vdev's asize field isn't stale.
3070 newsize = vdev_get_min_asize(newvd);
3073 * Make newsize a little bigger or smaller than oldsize.
3074 * If it's smaller, the attach should fail.
3075 * If it's larger, and we're doing a replace,
3076 * we should get dynamic LUN growth when we're done.
3078 newsize = 10 * oldsize / (9 + ztest_random(3));
3082 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
3083 * unless it's a replace; in that case any non-replacing parent is OK.
3085 * If newvd is already part of the pool, it should fail with EBUSY.
3087 * If newvd is too small, it should fail with EOVERFLOW.
3089 if (pvd->vdev_ops != &vdev_mirror_ops &&
3090 pvd->vdev_ops != &vdev_root_ops && (!replacing ||
3091 pvd->vdev_ops == &vdev_replacing_ops ||
3092 pvd->vdev_ops == &vdev_spare_ops))
3093 expected_error = ENOTSUP;
3094 else if (newvd_is_spare && (!replacing || oldvd_is_log))
3095 expected_error = ENOTSUP;
3096 else if (newvd == oldvd)
3097 expected_error = replacing ? 0 : EBUSY;
3098 else if (vdev_lookup_by_path(rvd, newpath) != NULL)
3099 expected_error = EBUSY;
3100 else if (newsize < oldsize)
3101 expected_error = EOVERFLOW;
3102 else if (ashift > oldvd->vdev_top->vdev_ashift)
3103 expected_error = EDOM;
3107 spa_config_exit(spa, SCL_ALL, FTAG);
3110 * Build the nvlist describing newpath.
3112 root = make_vdev_root(newpath, NULL, NULL, newvd == NULL ? newsize : 0,
3113 ashift, 0, 0, 0, 1);
3115 error = spa_vdev_attach(spa, oldguid, root, replacing);
3120 * If our parent was the replacing vdev, but the replace completed,
3121 * then instead of failing with ENOTSUP we may either succeed,
3122 * fail with ENODEV, or fail with EOVERFLOW.
3124 if (expected_error == ENOTSUP &&
3125 (error == 0 || error == ENODEV || error == EOVERFLOW))
3126 expected_error = error;
3129 * If someone grew the LUN, the replacement may be too small.
3131 if (error == EOVERFLOW || error == EBUSY)
3132 expected_error = error;
3134 if (error == ZFS_ERR_CHECKPOINT_EXISTS ||
3135 error == ZFS_ERR_DISCARDING_CHECKPOINT)
3136 expected_error = error;
3138 /* XXX workaround 6690467 */
3139 if (error != expected_error && expected_error != EBUSY) {
3140 fatal(0, "attach (%s %llu, %s %llu, %d) "
3141 "returned %d, expected %d",
3142 oldpath, oldsize, newpath,
3143 newsize, replacing, error, expected_error);
3146 mutex_exit(&ztest_vdev_lock);
3151 ztest_device_removal(ztest_ds_t *zd, uint64_t id)
3153 spa_t *spa = ztest_spa;
3158 mutex_enter(&ztest_vdev_lock);
3160 if (ztest_device_removal_active) {
3161 mutex_exit(&ztest_vdev_lock);
3166 * Remove a random top-level vdev and wait for removal to finish.
3168 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
3169 vd = vdev_lookup_top(spa, ztest_random_vdev_top(spa, B_FALSE));
3170 guid = vd->vdev_guid;
3171 spa_config_exit(spa, SCL_VDEV, FTAG);
3173 error = spa_vdev_remove(spa, guid, B_FALSE);
3175 ztest_device_removal_active = B_TRUE;
3176 mutex_exit(&ztest_vdev_lock);
3178 while (spa->spa_vdev_removal != NULL)
3179 txg_wait_synced(spa_get_dsl(spa), 0);
3181 mutex_exit(&ztest_vdev_lock);
3186 * The pool needs to be scrubbed after completing device removal.
3187 * Failure to do so may result in checksum errors due to the
3188 * strategy employed by ztest_fault_inject() when selecting which
3189 * offset are redundant and can be damaged.
3191 error = spa_scan(spa, POOL_SCAN_SCRUB);
3193 while (dsl_scan_scrubbing(spa_get_dsl(spa)))
3194 txg_wait_synced(spa_get_dsl(spa), 0);
3197 mutex_enter(&ztest_vdev_lock);
3198 ztest_device_removal_active = B_FALSE;
3199 mutex_exit(&ztest_vdev_lock);
3203 * Callback function which expands the physical size of the vdev.
3206 grow_vdev(vdev_t *vd, void *arg)
3208 spa_t *spa = vd->vdev_spa;
3209 size_t *newsize = arg;
3213 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3214 ASSERT(vd->vdev_ops->vdev_op_leaf);
3216 if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
3219 fsize = lseek(fd, 0, SEEK_END);
3220 (void) ftruncate(fd, *newsize);
3222 if (ztest_opts.zo_verbose >= 6) {
3223 (void) printf("%s grew from %lu to %lu bytes\n",
3224 vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
3231 * Callback function which expands a given vdev by calling vdev_online().
3235 online_vdev(vdev_t *vd, void *arg)
3237 spa_t *spa = vd->vdev_spa;
3238 vdev_t *tvd = vd->vdev_top;
3239 uint64_t guid = vd->vdev_guid;
3240 uint64_t generation = spa->spa_config_generation + 1;
3241 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
3244 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3245 ASSERT(vd->vdev_ops->vdev_op_leaf);
3247 /* Calling vdev_online will initialize the new metaslabs */
3248 spa_config_exit(spa, SCL_STATE, spa);
3249 error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
3250 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3253 * If vdev_online returned an error or the underlying vdev_open
3254 * failed then we abort the expand. The only way to know that
3255 * vdev_open fails is by checking the returned newstate.
3257 if (error || newstate != VDEV_STATE_HEALTHY) {
3258 if (ztest_opts.zo_verbose >= 5) {
3259 (void) printf("Unable to expand vdev, state %llu, "
3260 "error %d\n", (u_longlong_t)newstate, error);
3264 ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
3267 * Since we dropped the lock we need to ensure that we're
3268 * still talking to the original vdev. It's possible this
3269 * vdev may have been detached/replaced while we were
3270 * trying to online it.
3272 if (generation != spa->spa_config_generation) {
3273 if (ztest_opts.zo_verbose >= 5) {
3274 (void) printf("vdev configuration has changed, "
3275 "guid %llu, state %llu, expected gen %llu, "
3278 (u_longlong_t)tvd->vdev_state,
3279 (u_longlong_t)generation,
3280 (u_longlong_t)spa->spa_config_generation);
3288 * Traverse the vdev tree calling the supplied function.
3289 * We continue to walk the tree until we either have walked all
3290 * children or we receive a non-NULL return from the callback.
3291 * If a NULL callback is passed, then we just return back the first
3292 * leaf vdev we encounter.
3295 vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
3297 if (vd->vdev_ops->vdev_op_leaf) {
3301 return (func(vd, arg));
3304 for (uint_t c = 0; c < vd->vdev_children; c++) {
3305 vdev_t *cvd = vd->vdev_child[c];
3306 if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
3313 * Verify that dynamic LUN growth works as expected.
3317 ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
3319 spa_t *spa = ztest_spa;
3321 metaslab_class_t *mc;
3322 metaslab_group_t *mg;
3323 size_t psize, newsize;
3325 uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
3327 mutex_enter(&ztest_checkpoint_lock);
3328 mutex_enter(&ztest_vdev_lock);
3329 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3332 * If there is a vdev removal in progress, it could complete while
3333 * we are running, in which case we would not be able to verify
3334 * that the metaslab_class space increased (because it decreases
3335 * when the device removal completes).
3337 if (ztest_device_removal_active) {
3338 spa_config_exit(spa, SCL_STATE, spa);
3339 mutex_exit(&ztest_vdev_lock);
3340 mutex_exit(&ztest_checkpoint_lock);
3344 top = ztest_random_vdev_top(spa, B_TRUE);
3346 tvd = spa->spa_root_vdev->vdev_child[top];
3349 old_ms_count = tvd->vdev_ms_count;
3350 old_class_space = metaslab_class_get_space(mc);
3353 * Determine the size of the first leaf vdev associated with
3354 * our top-level device.
3356 vd = vdev_walk_tree(tvd, NULL, NULL);
3357 ASSERT3P(vd, !=, NULL);
3358 ASSERT(vd->vdev_ops->vdev_op_leaf);
3360 psize = vd->vdev_psize;
3363 * We only try to expand the vdev if it's healthy, less than 4x its
3364 * original size, and it has a valid psize.
3366 if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
3367 psize == 0 || psize >= 4 * ztest_opts.zo_vdev_size) {
3368 spa_config_exit(spa, SCL_STATE, spa);
3369 mutex_exit(&ztest_vdev_lock);
3370 mutex_exit(&ztest_checkpoint_lock);
3374 newsize = psize + psize / 8;
3375 ASSERT3U(newsize, >, psize);
3377 if (ztest_opts.zo_verbose >= 6) {
3378 (void) printf("Expanding LUN %s from %lu to %lu\n",
3379 vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
3383 * Growing the vdev is a two step process:
3384 * 1). expand the physical size (i.e. relabel)
3385 * 2). online the vdev to create the new metaslabs
3387 if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
3388 vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
3389 tvd->vdev_state != VDEV_STATE_HEALTHY) {
3390 if (ztest_opts.zo_verbose >= 5) {
3391 (void) printf("Could not expand LUN because "
3392 "the vdev configuration changed.\n");
3394 spa_config_exit(spa, SCL_STATE, spa);
3395 mutex_exit(&ztest_vdev_lock);
3396 mutex_exit(&ztest_checkpoint_lock);
3400 spa_config_exit(spa, SCL_STATE, spa);
3403 * Expanding the LUN will update the config asynchronously,
3404 * thus we must wait for the async thread to complete any
3405 * pending tasks before proceeding.
3409 mutex_enter(&spa->spa_async_lock);
3410 done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
3411 mutex_exit(&spa->spa_async_lock);
3414 txg_wait_synced(spa_get_dsl(spa), 0);
3415 (void) poll(NULL, 0, 100);
3418 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3420 tvd = spa->spa_root_vdev->vdev_child[top];
3421 new_ms_count = tvd->vdev_ms_count;
3422 new_class_space = metaslab_class_get_space(mc);
3424 if (tvd->vdev_mg != mg || mg->mg_class != mc) {
3425 if (ztest_opts.zo_verbose >= 5) {
3426 (void) printf("Could not verify LUN expansion due to "
3427 "intervening vdev offline or remove.\n");
3429 spa_config_exit(spa, SCL_STATE, spa);
3430 mutex_exit(&ztest_vdev_lock);
3431 mutex_exit(&ztest_checkpoint_lock);
3436 * Make sure we were able to grow the vdev.
3438 if (new_ms_count <= old_ms_count) {
3439 fatal(0, "LUN expansion failed: ms_count %llu < %llu\n",
3440 old_ms_count, new_ms_count);
3444 * Make sure we were able to grow the pool.
3446 if (new_class_space <= old_class_space) {
3447 fatal(0, "LUN expansion failed: class_space %llu < %llu\n",
3448 old_class_space, new_class_space);
3451 if (ztest_opts.zo_verbose >= 5) {
3452 char oldnumbuf[NN_NUMBUF_SZ], newnumbuf[NN_NUMBUF_SZ];
3454 nicenum(old_class_space, oldnumbuf, sizeof (oldnumbuf));
3455 nicenum(new_class_space, newnumbuf, sizeof (newnumbuf));
3456 (void) printf("%s grew from %s to %s\n",
3457 spa->spa_name, oldnumbuf, newnumbuf);
3460 spa_config_exit(spa, SCL_STATE, spa);
3461 mutex_exit(&ztest_vdev_lock);
3462 mutex_exit(&ztest_checkpoint_lock);
3466 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
3470 ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3473 * Create the objects common to all ztest datasets.
3475 VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
3476 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
3480 ztest_dataset_create(char *dsname)
3482 uint64_t zilset = ztest_random(100);
3483 int err = dmu_objset_create(dsname, DMU_OST_OTHER, 0,
3484 ztest_objset_create_cb, NULL);
3486 if (err || zilset < 80)
3489 if (ztest_opts.zo_verbose >= 6)
3490 (void) printf("Setting dataset %s to sync always\n", dsname);
3491 return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC,
3492 ZFS_SYNC_ALWAYS, B_FALSE));
3497 ztest_objset_destroy_cb(const char *name, void *arg)
3500 dmu_object_info_t doi;
3504 * Verify that the dataset contains a directory object.
3506 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_TRUE, FTAG, &os));
3507 error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
3508 if (error != ENOENT) {
3509 /* We could have crashed in the middle of destroying it */
3511 ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
3512 ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
3514 dmu_objset_disown(os, FTAG);
3517 * Destroy the dataset.
3519 if (strchr(name, '@') != NULL) {
3520 VERIFY0(dsl_destroy_snapshot(name, B_FALSE));
3522 VERIFY0(dsl_destroy_head(name));
3528 ztest_snapshot_create(char *osname, uint64_t id)
3530 char snapname[ZFS_MAX_DATASET_NAME_LEN];
3533 (void) snprintf(snapname, sizeof (snapname), "%llu", (u_longlong_t)id);
3535 error = dmu_objset_snapshot_one(osname, snapname);
3536 if (error == ENOSPC) {
3537 ztest_record_enospc(FTAG);
3540 if (error != 0 && error != EEXIST) {
3541 fatal(0, "ztest_snapshot_create(%s@%s) = %d", osname,
3548 ztest_snapshot_destroy(char *osname, uint64_t id)
3550 char snapname[ZFS_MAX_DATASET_NAME_LEN];
3553 (void) snprintf(snapname, sizeof (snapname), "%s@%llu", osname,
3556 error = dsl_destroy_snapshot(snapname, B_FALSE);
3557 if (error != 0 && error != ENOENT)
3558 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
3564 ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
3570 char name[ZFS_MAX_DATASET_NAME_LEN];
3573 rw_enter(&ztest_name_lock, RW_READER);
3575 (void) snprintf(name, sizeof (name), "%s/temp_%llu",
3576 ztest_opts.zo_pool, (u_longlong_t)id);
3579 * If this dataset exists from a previous run, process its replay log
3580 * half of the time. If we don't replay it, then dmu_objset_destroy()
3581 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
3583 if (ztest_random(2) == 0 &&
3584 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) {
3585 ztest_zd_init(&zdtmp, NULL, os);
3586 zil_replay(os, &zdtmp, ztest_replay_vector);
3587 ztest_zd_fini(&zdtmp);
3588 dmu_objset_disown(os, FTAG);
3592 * There may be an old instance of the dataset we're about to
3593 * create lying around from a previous run. If so, destroy it
3594 * and all of its snapshots.
3596 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
3597 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
3600 * Verify that the destroyed dataset is no longer in the namespace.
3602 VERIFY3U(ENOENT, ==, dmu_objset_own(name, DMU_OST_OTHER, B_TRUE,
3606 * Verify that we can create a new dataset.
3608 error = ztest_dataset_create(name);
3610 if (error == ENOSPC) {
3611 ztest_record_enospc(FTAG);
3612 rw_exit(&ztest_name_lock);
3615 fatal(0, "dmu_objset_create(%s) = %d", name, error);
3618 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os));
3620 ztest_zd_init(&zdtmp, NULL, os);
3623 * Open the intent log for it.
3625 zilog = zil_open(os, ztest_get_data);
3628 * Put some objects in there, do a little I/O to them,
3629 * and randomly take a couple of snapshots along the way.
3631 iters = ztest_random(5);
3632 for (int i = 0; i < iters; i++) {
3633 ztest_dmu_object_alloc_free(&zdtmp, id);
3634 if (ztest_random(iters) == 0)
3635 (void) ztest_snapshot_create(name, i);
3639 * Verify that we cannot create an existing dataset.
3641 VERIFY3U(EEXIST, ==,
3642 dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL));
3645 * Verify that we can hold an objset that is also owned.
3647 VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
3648 dmu_objset_rele(os2, FTAG);
3651 * Verify that we cannot own an objset that is already owned.
3654 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2));
3657 dmu_objset_disown(os, FTAG);
3658 ztest_zd_fini(&zdtmp);
3660 rw_exit(&ztest_name_lock);
3664 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3667 ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
3669 rw_enter(&ztest_name_lock, RW_READER);
3670 (void) ztest_snapshot_destroy(zd->zd_name, id);
3671 (void) ztest_snapshot_create(zd->zd_name, id);
3672 rw_exit(&ztest_name_lock);
3676 * Cleanup non-standard snapshots and clones.
3679 ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
3681 char snap1name[ZFS_MAX_DATASET_NAME_LEN];
3682 char clone1name[ZFS_MAX_DATASET_NAME_LEN];
3683 char snap2name[ZFS_MAX_DATASET_NAME_LEN];
3684 char clone2name[ZFS_MAX_DATASET_NAME_LEN];
3685 char snap3name[ZFS_MAX_DATASET_NAME_LEN];
3688 (void) snprintf(snap1name, sizeof (snap1name),
3689 "%s@s1_%llu", osname, id);
3690 (void) snprintf(clone1name, sizeof (clone1name),
3691 "%s/c1_%llu", osname, id);
3692 (void) snprintf(snap2name, sizeof (snap2name),
3693 "%s@s2_%llu", clone1name, id);
3694 (void) snprintf(clone2name, sizeof (clone2name),
3695 "%s/c2_%llu", osname, id);
3696 (void) snprintf(snap3name, sizeof (snap3name),
3697 "%s@s3_%llu", clone1name, id);
3699 error = dsl_destroy_head(clone2name);
3700 if (error && error != ENOENT)
3701 fatal(0, "dsl_destroy_head(%s) = %d", clone2name, error);
3702 error = dsl_destroy_snapshot(snap3name, B_FALSE);
3703 if (error && error != ENOENT)
3704 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap3name, error);
3705 error = dsl_destroy_snapshot(snap2name, B_FALSE);
3706 if (error && error != ENOENT)
3707 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap2name, error);
3708 error = dsl_destroy_head(clone1name);
3709 if (error && error != ENOENT)
3710 fatal(0, "dsl_destroy_head(%s) = %d", clone1name, error);
3711 error = dsl_destroy_snapshot(snap1name, B_FALSE);
3712 if (error && error != ENOENT)
3713 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap1name, error);
3717 * Verify dsl_dataset_promote handles EBUSY
3720 ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
3723 char snap1name[ZFS_MAX_DATASET_NAME_LEN];
3724 char clone1name[ZFS_MAX_DATASET_NAME_LEN];
3725 char snap2name[ZFS_MAX_DATASET_NAME_LEN];
3726 char clone2name[ZFS_MAX_DATASET_NAME_LEN];
3727 char snap3name[ZFS_MAX_DATASET_NAME_LEN];
3728 char *osname = zd->zd_name;
3731 rw_enter(&ztest_name_lock, RW_READER);
3733 ztest_dsl_dataset_cleanup(osname, id);
3735 (void) snprintf(snap1name, sizeof (snap1name),
3736 "%s@s1_%llu", osname, id);
3737 (void) snprintf(clone1name, sizeof (clone1name),
3738 "%s/c1_%llu", osname, id);
3739 (void) snprintf(snap2name, sizeof (snap2name),
3740 "%s@s2_%llu", clone1name, id);
3741 (void) snprintf(clone2name, sizeof (clone2name),
3742 "%s/c2_%llu", osname, id);
3743 (void) snprintf(snap3name, sizeof (snap3name),
3744 "%s@s3_%llu", clone1name, id);
3746 error = dmu_objset_snapshot_one(osname, strchr(snap1name, '@') + 1);
3747 if (error && error != EEXIST) {
3748 if (error == ENOSPC) {
3749 ztest_record_enospc(FTAG);
3752 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
3755 error = dmu_objset_clone(clone1name, snap1name);
3757 if (error == ENOSPC) {
3758 ztest_record_enospc(FTAG);
3761 fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
3764 error = dmu_objset_snapshot_one(clone1name, strchr(snap2name, '@') + 1);
3765 if (error && error != EEXIST) {
3766 if (error == ENOSPC) {
3767 ztest_record_enospc(FTAG);
3770 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
3773 error = dmu_objset_snapshot_one(clone1name, strchr(snap3name, '@') + 1);
3774 if (error && error != EEXIST) {
3775 if (error == ENOSPC) {
3776 ztest_record_enospc(FTAG);
3779 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
3782 error = dmu_objset_clone(clone2name, snap3name);
3784 if (error == ENOSPC) {
3785 ztest_record_enospc(FTAG);
3788 fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
3791 error = dmu_objset_own(snap2name, DMU_OST_ANY, B_TRUE, FTAG, &os);
3793 fatal(0, "dmu_objset_own(%s) = %d", snap2name, error);
3794 error = dsl_dataset_promote(clone2name, NULL);
3795 if (error == ENOSPC) {
3796 dmu_objset_disown(os, FTAG);
3797 ztest_record_enospc(FTAG);
3801 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
3803 dmu_objset_disown(os, FTAG);
3806 ztest_dsl_dataset_cleanup(osname, id);
3808 rw_exit(&ztest_name_lock);
3812 * Verify that dmu_object_{alloc,free} work as expected.
3815 ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
3818 int batchsize = sizeof (od) / sizeof (od[0]);
3820 for (int b = 0; b < batchsize; b++)
3821 ztest_od_init(&od[b], id, FTAG, b, DMU_OT_UINT64_OTHER, 0, 0, 0);
3824 * Destroy the previous batch of objects, create a new batch,
3825 * and do some I/O on the new objects.
3827 if (ztest_object_init(zd, od, sizeof (od), B_TRUE) != 0)
3830 while (ztest_random(4 * batchsize) != 0)
3831 ztest_io(zd, od[ztest_random(batchsize)].od_object,
3832 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3836 * Verify that dmu_{read,write} work as expected.
3839 ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
3841 objset_t *os = zd->zd_os;
3844 int i, freeit, error;
3846 bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
3847 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3848 uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
3849 uint64_t regions = 997;
3850 uint64_t stride = 123456789ULL;
3851 uint64_t width = 40;
3852 int free_percent = 5;
3855 * This test uses two objects, packobj and bigobj, that are always
3856 * updated together (i.e. in the same tx) so that their contents are
3857 * in sync and can be compared. Their contents relate to each other
3858 * in a simple way: packobj is a dense array of 'bufwad' structures,
3859 * while bigobj is a sparse array of the same bufwads. Specifically,
3860 * for any index n, there are three bufwads that should be identical:
3862 * packobj, at offset n * sizeof (bufwad_t)
3863 * bigobj, at the head of the nth chunk
3864 * bigobj, at the tail of the nth chunk
3866 * The chunk size is arbitrary. It doesn't have to be a power of two,
3867 * and it doesn't have any relation to the object blocksize.
3868 * The only requirement is that it can hold at least two bufwads.
3870 * Normally, we write the bufwad to each of these locations.
3871 * However, free_percent of the time we instead write zeroes to
3872 * packobj and perform a dmu_free_range() on bigobj. By comparing
3873 * bigobj to packobj, we can verify that the DMU is correctly
3874 * tracking which parts of an object are allocated and free,
3875 * and that the contents of the allocated blocks are correct.
3879 * Read the directory info. If it's the first time, set things up.
3881 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, chunksize);
3882 ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, 0, chunksize);
3884 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
3887 bigobj = od[0].od_object;
3888 packobj = od[1].od_object;
3889 chunksize = od[0].od_gen;
3890 ASSERT(chunksize == od[1].od_gen);
3893 * Prefetch a random chunk of the big object.
3894 * Our aim here is to get some async reads in flight
3895 * for blocks that we may free below; the DMU should
3896 * handle this race correctly.
3898 n = ztest_random(regions) * stride + ztest_random(width);
3899 s = 1 + ztest_random(2 * width - 1);
3900 dmu_prefetch(os, bigobj, 0, n * chunksize, s * chunksize,
3901 ZIO_PRIORITY_SYNC_READ);
3904 * Pick a random index and compute the offsets into packobj and bigobj.
3906 n = ztest_random(regions) * stride + ztest_random(width);
3907 s = 1 + ztest_random(width - 1);
3909 packoff = n * sizeof (bufwad_t);
3910 packsize = s * sizeof (bufwad_t);
3912 bigoff = n * chunksize;
3913 bigsize = s * chunksize;
3915 packbuf = umem_alloc(packsize, UMEM_NOFAIL);
3916 bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
3919 * free_percent of the time, free a range of bigobj rather than
3922 freeit = (ztest_random(100) < free_percent);
3925 * Read the current contents of our objects.
3927 error = dmu_read(os, packobj, packoff, packsize, packbuf,
3930 error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
3935 * Get a tx for the mods to both packobj and bigobj.
3937 tx = dmu_tx_create(os);
3939 dmu_tx_hold_write(tx, packobj, packoff, packsize);
3942 dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
3944 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
3946 /* This accounts for setting the checksum/compression. */
3947 dmu_tx_hold_bonus(tx, bigobj);
3949 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3951 umem_free(packbuf, packsize);
3952 umem_free(bigbuf, bigsize);
3956 enum zio_checksum cksum;
3958 cksum = (enum zio_checksum)
3959 ztest_random_dsl_prop(ZFS_PROP_CHECKSUM);
3960 } while (cksum >= ZIO_CHECKSUM_LEGACY_FUNCTIONS);
3961 dmu_object_set_checksum(os, bigobj, cksum, tx);
3963 enum zio_compress comp;
3965 comp = (enum zio_compress)
3966 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION);
3967 } while (comp >= ZIO_COMPRESS_LEGACY_FUNCTIONS);
3968 dmu_object_set_compress(os, bigobj, comp, tx);
3971 * For each index from n to n + s, verify that the existing bufwad
3972 * in packobj matches the bufwads at the head and tail of the
3973 * corresponding chunk in bigobj. Then update all three bufwads
3974 * with the new values we want to write out.
3976 for (i = 0; i < s; i++) {
3978 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3980 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
3982 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
3984 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3985 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3987 if (pack->bw_txg > txg)
3988 fatal(0, "future leak: got %llx, open txg is %llx",
3991 if (pack->bw_data != 0 && pack->bw_index != n + i)
3992 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3993 pack->bw_index, n, i);
3995 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3996 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
3998 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
3999 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
4002 bzero(pack, sizeof (bufwad_t));
4004 pack->bw_index = n + i;
4006 pack->bw_data = 1 + ztest_random(-2ULL);
4013 * We've verified all the old bufwads, and made new ones.
4014 * Now write them out.
4016 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
4019 if (ztest_opts.zo_verbose >= 7) {
4020 (void) printf("freeing offset %llx size %llx"
4022 (u_longlong_t)bigoff,
4023 (u_longlong_t)bigsize,
4026 VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
4028 if (ztest_opts.zo_verbose >= 7) {
4029 (void) printf("writing offset %llx size %llx"
4031 (u_longlong_t)bigoff,
4032 (u_longlong_t)bigsize,
4035 dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
4041 * Sanity check the stuff we just wrote.
4044 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
4045 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
4047 VERIFY(0 == dmu_read(os, packobj, packoff,
4048 packsize, packcheck, DMU_READ_PREFETCH));
4049 VERIFY(0 == dmu_read(os, bigobj, bigoff,
4050 bigsize, bigcheck, DMU_READ_PREFETCH));
4052 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
4053 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
4055 umem_free(packcheck, packsize);
4056 umem_free(bigcheck, bigsize);
4059 umem_free(packbuf, packsize);
4060 umem_free(bigbuf, bigsize);
4064 compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
4065 uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
4073 * For each index from n to n + s, verify that the existing bufwad
4074 * in packobj matches the bufwads at the head and tail of the
4075 * corresponding chunk in bigobj. Then update all three bufwads
4076 * with the new values we want to write out.
4078 for (i = 0; i < s; i++) {
4080 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
4082 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
4084 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
4086 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
4087 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
4089 if (pack->bw_txg > txg)
4090 fatal(0, "future leak: got %llx, open txg is %llx",
4093 if (pack->bw_data != 0 && pack->bw_index != n + i)
4094 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4095 pack->bw_index, n, i);
4097 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
4098 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
4100 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
4101 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
4103 pack->bw_index = n + i;
4105 pack->bw_data = 1 + ztest_random(-2ULL);
4113 ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
4115 objset_t *os = zd->zd_os;
4121 bufwad_t *packbuf, *bigbuf;
4122 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
4123 uint64_t blocksize = ztest_random_blocksize();
4124 uint64_t chunksize = blocksize;
4125 uint64_t regions = 997;
4126 uint64_t stride = 123456789ULL;
4128 dmu_buf_t *bonus_db;
4129 arc_buf_t **bigbuf_arcbufs;
4130 dmu_object_info_t doi;
4133 * This test uses two objects, packobj and bigobj, that are always
4134 * updated together (i.e. in the same tx) so that their contents are
4135 * in sync and can be compared. Their contents relate to each other
4136 * in a simple way: packobj is a dense array of 'bufwad' structures,
4137 * while bigobj is a sparse array of the same bufwads. Specifically,
4138 * for any index n, there are three bufwads that should be identical:
4140 * packobj, at offset n * sizeof (bufwad_t)
4141 * bigobj, at the head of the nth chunk
4142 * bigobj, at the tail of the nth chunk
4144 * The chunk size is set equal to bigobj block size so that
4145 * dmu_assign_arcbuf() can be tested for object updates.
4149 * Read the directory info. If it's the first time, set things up.
4151 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0, 0);
4152 ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, 0, chunksize);
4154 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4157 bigobj = od[0].od_object;
4158 packobj = od[1].od_object;
4159 blocksize = od[0].od_blocksize;
4160 chunksize = blocksize;
4161 ASSERT(chunksize == od[1].od_gen);
4163 VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
4164 VERIFY(ISP2(doi.doi_data_block_size));
4165 VERIFY(chunksize == doi.doi_data_block_size);
4166 VERIFY(chunksize >= 2 * sizeof (bufwad_t));
4169 * Pick a random index and compute the offsets into packobj and bigobj.
4171 n = ztest_random(regions) * stride + ztest_random(width);
4172 s = 1 + ztest_random(width - 1);
4174 packoff = n * sizeof (bufwad_t);
4175 packsize = s * sizeof (bufwad_t);
4177 bigoff = n * chunksize;
4178 bigsize = s * chunksize;
4180 packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
4181 bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
4183 VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
4185 bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
4188 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
4189 * Iteration 1 test zcopy to already referenced dbufs.
4190 * Iteration 2 test zcopy to dirty dbuf in the same txg.
4191 * Iteration 3 test zcopy to dbuf dirty in previous txg.
4192 * Iteration 4 test zcopy when dbuf is no longer dirty.
4193 * Iteration 5 test zcopy when it can't be done.
4194 * Iteration 6 one more zcopy write.
4196 for (i = 0; i < 7; i++) {
4201 * In iteration 5 (i == 5) use arcbufs
4202 * that don't match bigobj blksz to test
4203 * dmu_assign_arcbuf() when it can't directly
4204 * assign an arcbuf to a dbuf.
4206 for (j = 0; j < s; j++) {
4209 dmu_request_arcbuf(bonus_db, chunksize);
4211 bigbuf_arcbufs[2 * j] =
4212 dmu_request_arcbuf(bonus_db, chunksize / 2);
4213 bigbuf_arcbufs[2 * j + 1] =
4214 dmu_request_arcbuf(bonus_db, chunksize / 2);
4219 * Get a tx for the mods to both packobj and bigobj.
4221 tx = dmu_tx_create(os);
4223 dmu_tx_hold_write(tx, packobj, packoff, packsize);
4224 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
4226 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4228 umem_free(packbuf, packsize);
4229 umem_free(bigbuf, bigsize);
4230 for (j = 0; j < s; j++) {
4232 dmu_return_arcbuf(bigbuf_arcbufs[j]);
4235 bigbuf_arcbufs[2 * j]);
4237 bigbuf_arcbufs[2 * j + 1]);
4240 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4241 dmu_buf_rele(bonus_db, FTAG);
4246 * 50% of the time don't read objects in the 1st iteration to
4247 * test dmu_assign_arcbuf() for the case when there're no
4248 * existing dbufs for the specified offsets.
4250 if (i != 0 || ztest_random(2) != 0) {
4251 error = dmu_read(os, packobj, packoff,
4252 packsize, packbuf, DMU_READ_PREFETCH);
4254 error = dmu_read(os, bigobj, bigoff, bigsize,
4255 bigbuf, DMU_READ_PREFETCH);
4258 compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
4262 * We've verified all the old bufwads, and made new ones.
4263 * Now write them out.
4265 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
4266 if (ztest_opts.zo_verbose >= 7) {
4267 (void) printf("writing offset %llx size %llx"
4269 (u_longlong_t)bigoff,
4270 (u_longlong_t)bigsize,
4273 for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
4276 bcopy((caddr_t)bigbuf + (off - bigoff),
4277 bigbuf_arcbufs[j]->b_data, chunksize);
4279 bcopy((caddr_t)bigbuf + (off - bigoff),
4280 bigbuf_arcbufs[2 * j]->b_data,
4282 bcopy((caddr_t)bigbuf + (off - bigoff) +
4284 bigbuf_arcbufs[2 * j + 1]->b_data,
4289 VERIFY(dmu_buf_hold(os, bigobj, off,
4290 FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0);
4293 dmu_assign_arcbuf(bonus_db, off,
4294 bigbuf_arcbufs[j], tx);
4296 dmu_assign_arcbuf(bonus_db, off,
4297 bigbuf_arcbufs[2 * j], tx);
4298 dmu_assign_arcbuf(bonus_db,
4299 off + chunksize / 2,
4300 bigbuf_arcbufs[2 * j + 1], tx);
4303 dmu_buf_rele(dbt, FTAG);
4309 * Sanity check the stuff we just wrote.
4312 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
4313 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
4315 VERIFY(0 == dmu_read(os, packobj, packoff,
4316 packsize, packcheck, DMU_READ_PREFETCH));
4317 VERIFY(0 == dmu_read(os, bigobj, bigoff,
4318 bigsize, bigcheck, DMU_READ_PREFETCH));
4320 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
4321 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
4323 umem_free(packcheck, packsize);
4324 umem_free(bigcheck, bigsize);
4327 txg_wait_open(dmu_objset_pool(os), 0);
4328 } else if (i == 3) {
4329 txg_wait_synced(dmu_objset_pool(os), 0);
4333 dmu_buf_rele(bonus_db, FTAG);
4334 umem_free(packbuf, packsize);
4335 umem_free(bigbuf, bigsize);
4336 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4341 ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
4344 uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
4345 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4348 * Have multiple threads write to large offsets in an object
4349 * to verify that parallel writes to an object -- even to the
4350 * same blocks within the object -- doesn't cause any trouble.
4352 ztest_od_init(&od[0], ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
4354 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4357 while (ztest_random(10) != 0)
4358 ztest_io(zd, od[0].od_object, offset);
4362 ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
4365 uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
4366 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4367 uint64_t count = ztest_random(20) + 1;
4368 uint64_t blocksize = ztest_random_blocksize();
4371 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0, 0);
4373 if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
4376 if (ztest_truncate(zd, od[0].od_object, offset, count * blocksize) != 0)
4379 ztest_prealloc(zd, od[0].od_object, offset, count * blocksize);
4381 data = umem_zalloc(blocksize, UMEM_NOFAIL);
4383 while (ztest_random(count) != 0) {
4384 uint64_t randoff = offset + (ztest_random(count) * blocksize);
4385 if (ztest_write(zd, od[0].od_object, randoff, blocksize,
4388 while (ztest_random(4) != 0)
4389 ztest_io(zd, od[0].od_object, randoff);
4392 umem_free(data, blocksize);
4396 * Verify that zap_{create,destroy,add,remove,update} work as expected.
4398 #define ZTEST_ZAP_MIN_INTS 1
4399 #define ZTEST_ZAP_MAX_INTS 4
4400 #define ZTEST_ZAP_MAX_PROPS 1000
4403 ztest_zap(ztest_ds_t *zd, uint64_t id)
4405 objset_t *os = zd->zd_os;
4408 uint64_t txg, last_txg;
4409 uint64_t value[ZTEST_ZAP_MAX_INTS];
4410 uint64_t zl_ints, zl_intsize, prop;
4413 char propname[100], txgname[100];
4415 char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
4417 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0, 0);
4419 if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
4422 object = od[0].od_object;
4425 * Generate a known hash collision, and verify that
4426 * we can lookup and remove both entries.
4428 tx = dmu_tx_create(os);
4429 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4430 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4433 for (i = 0; i < 2; i++) {
4435 VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
4438 for (i = 0; i < 2; i++) {
4439 VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
4440 sizeof (uint64_t), 1, &value[i], tx));
4442 zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
4443 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4444 ASSERT3U(zl_ints, ==, 1);
4446 for (i = 0; i < 2; i++) {
4447 VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
4452 * Generate a buch of random entries.
4454 ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
4456 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4457 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4458 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4459 bzero(value, sizeof (value));
4463 * If these zap entries already exist, validate their contents.
4465 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4467 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4468 ASSERT3U(zl_ints, ==, 1);
4470 VERIFY(zap_lookup(os, object, txgname, zl_intsize,
4471 zl_ints, &last_txg) == 0);
4473 VERIFY(zap_length(os, object, propname, &zl_intsize,
4476 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4477 ASSERT3U(zl_ints, ==, ints);
4479 VERIFY(zap_lookup(os, object, propname, zl_intsize,
4480 zl_ints, value) == 0);
4482 for (i = 0; i < ints; i++) {
4483 ASSERT3U(value[i], ==, last_txg + object + i);
4486 ASSERT3U(error, ==, ENOENT);
4490 * Atomically update two entries in our zap object.
4491 * The first is named txg_%llu, and contains the txg
4492 * in which the property was last updated. The second
4493 * is named prop_%llu, and the nth element of its value
4494 * should be txg + object + n.
4496 tx = dmu_tx_create(os);
4497 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4498 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4503 fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
4505 for (i = 0; i < ints; i++)
4506 value[i] = txg + object + i;
4508 VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
4510 VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
4516 * Remove a random pair of entries.
4518 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4519 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4520 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4522 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4524 if (error == ENOENT)
4529 tx = dmu_tx_create(os);
4530 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4531 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4534 VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
4535 VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
4540 * Testcase to test the upgrading of a microzap to fatzap.
4543 ztest_fzap(ztest_ds_t *zd, uint64_t id)
4545 objset_t *os = zd->zd_os;
4547 uint64_t object, txg;
4549 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0, 0);
4551 if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
4554 object = od[0].od_object;
4557 * Add entries to this ZAP and make sure it spills over
4558 * and gets upgraded to a fatzap. Also, since we are adding
4559 * 2050 entries we should see ptrtbl growth and leaf-block split.
4561 for (int i = 0; i < 2050; i++) {
4562 char name[ZFS_MAX_DATASET_NAME_LEN];
4567 (void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
4570 tx = dmu_tx_create(os);
4571 dmu_tx_hold_zap(tx, object, B_TRUE, name);
4572 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4575 error = zap_add(os, object, name, sizeof (uint64_t), 1,
4577 ASSERT(error == 0 || error == EEXIST);
4584 ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
4586 objset_t *os = zd->zd_os;
4588 uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
4590 int i, namelen, error;
4591 int micro = ztest_random(2);
4592 char name[20], string_value[20];
4595 ztest_od_init(&od[0], ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER, 0, 0, 0);
4597 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4600 object = od[0].od_object;
4603 * Generate a random name of the form 'xxx.....' where each
4604 * x is a random printable character and the dots are dots.
4605 * There are 94 such characters, and the name length goes from
4606 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
4608 namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
4610 for (i = 0; i < 3; i++)
4611 name[i] = '!' + ztest_random('~' - '!' + 1);
4612 for (; i < namelen - 1; i++)
4616 if ((namelen & 1) || micro) {
4617 wsize = sizeof (txg);
4623 data = string_value;
4627 VERIFY0(zap_count(os, object, &count));
4628 ASSERT(count != -1ULL);
4631 * Select an operation: length, lookup, add, update, remove.
4633 i = ztest_random(5);
4636 tx = dmu_tx_create(os);
4637 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4638 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4641 bcopy(name, string_value, namelen);
4645 bzero(string_value, namelen);
4651 error = zap_length(os, object, name, &zl_wsize, &zl_wc);
4653 ASSERT3U(wsize, ==, zl_wsize);
4654 ASSERT3U(wc, ==, zl_wc);
4656 ASSERT3U(error, ==, ENOENT);
4661 error = zap_lookup(os, object, name, wsize, wc, data);
4663 if (data == string_value &&
4664 bcmp(name, data, namelen) != 0)
4665 fatal(0, "name '%s' != val '%s' len %d",
4666 name, data, namelen);
4668 ASSERT3U(error, ==, ENOENT);
4673 error = zap_add(os, object, name, wsize, wc, data, tx);
4674 ASSERT(error == 0 || error == EEXIST);
4678 VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
4682 error = zap_remove(os, object, name, tx);
4683 ASSERT(error == 0 || error == ENOENT);
4692 * Commit callback data.
4694 typedef struct ztest_cb_data {
4695 list_node_t zcd_node;
4697 int zcd_expected_err;
4698 boolean_t zcd_added;
4699 boolean_t zcd_called;
4703 /* This is the actual commit callback function */
4705 ztest_commit_callback(void *arg, int error)
4707 ztest_cb_data_t *data = arg;
4708 uint64_t synced_txg;
4710 VERIFY(data != NULL);
4711 VERIFY3S(data->zcd_expected_err, ==, error);
4712 VERIFY(!data->zcd_called);
4714 synced_txg = spa_last_synced_txg(data->zcd_spa);
4715 if (data->zcd_txg > synced_txg)
4716 fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
4717 ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
4720 data->zcd_called = B_TRUE;
4722 if (error == ECANCELED) {
4723 ASSERT0(data->zcd_txg);
4724 ASSERT(!data->zcd_added);
4727 * The private callback data should be destroyed here, but
4728 * since we are going to check the zcd_called field after
4729 * dmu_tx_abort(), we will destroy it there.
4734 /* Was this callback added to the global callback list? */
4735 if (!data->zcd_added)
4738 ASSERT3U(data->zcd_txg, !=, 0);
4740 /* Remove our callback from the list */
4741 mutex_enter(&zcl.zcl_callbacks_lock);
4742 list_remove(&zcl.zcl_callbacks, data);
4743 mutex_exit(&zcl.zcl_callbacks_lock);
4746 umem_free(data, sizeof (ztest_cb_data_t));
4749 /* Allocate and initialize callback data structure */
4750 static ztest_cb_data_t *
4751 ztest_create_cb_data(objset_t *os, uint64_t txg)
4753 ztest_cb_data_t *cb_data;
4755 cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
4757 cb_data->zcd_txg = txg;
4758 cb_data->zcd_spa = dmu_objset_spa(os);
4764 * If a number of txgs equal to this threshold have been created after a commit
4765 * callback has been registered but not called, then we assume there is an
4766 * implementation bug.
4768 #define ZTEST_COMMIT_CALLBACK_THRESH (TXG_CONCURRENT_STATES + 2)
4771 * Commit callback test.
4774 ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
4776 objset_t *os = zd->zd_os;
4779 ztest_cb_data_t *cb_data[3], *tmp_cb;
4780 uint64_t old_txg, txg;
4783 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
4785 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4788 tx = dmu_tx_create(os);
4790 cb_data[0] = ztest_create_cb_data(os, 0);
4791 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
4793 dmu_tx_hold_write(tx, od[0].od_object, 0, sizeof (uint64_t));
4795 /* Every once in a while, abort the transaction on purpose */
4796 if (ztest_random(100) == 0)
4800 error = dmu_tx_assign(tx, TXG_NOWAIT);
4802 txg = error ? 0 : dmu_tx_get_txg(tx);
4804 cb_data[0]->zcd_txg = txg;
4805 cb_data[1] = ztest_create_cb_data(os, txg);
4806 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
4810 * It's not a strict requirement to call the registered
4811 * callbacks from inside dmu_tx_abort(), but that's what
4812 * it's supposed to happen in the current implementation
4813 * so we will check for that.
4815 for (i = 0; i < 2; i++) {
4816 cb_data[i]->zcd_expected_err = ECANCELED;
4817 VERIFY(!cb_data[i]->zcd_called);
4822 for (i = 0; i < 2; i++) {
4823 VERIFY(cb_data[i]->zcd_called);
4824 umem_free(cb_data[i], sizeof (ztest_cb_data_t));
4830 cb_data[2] = ztest_create_cb_data(os, txg);
4831 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
4834 * Read existing data to make sure there isn't a future leak.
4836 VERIFY(0 == dmu_read(os, od[0].od_object, 0, sizeof (uint64_t),
4837 &old_txg, DMU_READ_PREFETCH));
4840 fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
4843 dmu_write(os, od[0].od_object, 0, sizeof (uint64_t), &txg, tx);
4845 mutex_enter(&zcl.zcl_callbacks_lock);
4848 * Since commit callbacks don't have any ordering requirement and since
4849 * it is theoretically possible for a commit callback to be called
4850 * after an arbitrary amount of time has elapsed since its txg has been
4851 * synced, it is difficult to reliably determine whether a commit
4852 * callback hasn't been called due to high load or due to a flawed
4855 * In practice, we will assume that if after a certain number of txgs a
4856 * commit callback hasn't been called, then most likely there's an
4857 * implementation bug..
4859 tmp_cb = list_head(&zcl.zcl_callbacks);
4860 if (tmp_cb != NULL &&
4861 (txg - ZTEST_COMMIT_CALLBACK_THRESH) > tmp_cb->zcd_txg) {
4862 fatal(0, "Commit callback threshold exceeded, oldest txg: %"
4863 PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
4867 * Let's find the place to insert our callbacks.
4869 * Even though the list is ordered by txg, it is possible for the
4870 * insertion point to not be the end because our txg may already be
4871 * quiescing at this point and other callbacks in the open txg
4872 * (from other objsets) may have sneaked in.
4874 tmp_cb = list_tail(&zcl.zcl_callbacks);
4875 while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
4876 tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
4878 /* Add the 3 callbacks to the list */
4879 for (i = 0; i < 3; i++) {
4881 list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
4883 list_insert_after(&zcl.zcl_callbacks, tmp_cb,
4886 cb_data[i]->zcd_added = B_TRUE;
4887 VERIFY(!cb_data[i]->zcd_called);
4889 tmp_cb = cb_data[i];
4892 mutex_exit(&zcl.zcl_callbacks_lock);
4898 * Visit each object in the dataset. Verify that its properties
4899 * are consistent what was stored in the block tag when it was created,
4900 * and that its unused bonus buffer space has not been overwritten.
4903 ztest_verify_dnode_bt(ztest_ds_t *zd, uint64_t id)
4905 objset_t *os = zd->zd_os;
4909 for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
4910 ztest_block_tag_t *bt = NULL;
4911 dmu_object_info_t doi;
4914 if (dmu_bonus_hold(os, obj, FTAG, &db) != 0)
4917 dmu_object_info_from_db(db, &doi);
4918 if (doi.doi_bonus_size >= sizeof (*bt))
4919 bt = ztest_bt_bonus(db);
4921 if (bt && bt->bt_magic == BT_MAGIC) {
4922 ztest_bt_verify(bt, os, obj, doi.doi_dnodesize,
4923 bt->bt_offset, bt->bt_gen, bt->bt_txg,
4925 ztest_verify_unused_bonus(db, bt, obj, os, bt->bt_gen);
4928 dmu_buf_rele(db, FTAG);
4934 ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
4936 zfs_prop_t proplist[] = {
4938 ZFS_PROP_COMPRESSION,
4943 rw_enter(&ztest_name_lock, RW_READER);
4945 for (int p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
4946 (void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
4947 ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
4949 rw_exit(&ztest_name_lock);
4954 ztest_remap_blocks(ztest_ds_t *zd, uint64_t id)
4956 rw_enter(&ztest_name_lock, RW_READER);
4958 int error = dmu_objset_remap_indirects(zd->zd_name);
4959 if (error == ENOSPC)
4963 rw_exit(&ztest_name_lock);
4968 ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
4970 nvlist_t *props = NULL;
4972 rw_enter(&ztest_name_lock, RW_READER);
4974 (void) ztest_spa_prop_set_uint64(ZPOOL_PROP_DEDUPDITTO,
4975 ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
4977 VERIFY0(spa_prop_get(ztest_spa, &props));
4979 if (ztest_opts.zo_verbose >= 6)
4980 dump_nvlist(props, 4);
4984 rw_exit(&ztest_name_lock);
4988 user_release_one(const char *snapname, const char *holdname)
4990 nvlist_t *snaps, *holds;
4993 snaps = fnvlist_alloc();
4994 holds = fnvlist_alloc();
4995 fnvlist_add_boolean(holds, holdname);
4996 fnvlist_add_nvlist(snaps, snapname, holds);
4997 fnvlist_free(holds);
4998 error = dsl_dataset_user_release(snaps, NULL);
4999 fnvlist_free(snaps);
5004 * Test snapshot hold/release and deferred destroy.
5007 ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
5010 objset_t *os = zd->zd_os;
5014 char clonename[100];
5016 char osname[ZFS_MAX_DATASET_NAME_LEN];
5019 rw_enter(&ztest_name_lock, RW_READER);
5021 dmu_objset_name(os, osname);
5023 (void) snprintf(snapname, sizeof (snapname), "sh1_%llu", id);
5024 (void) snprintf(fullname, sizeof (fullname), "%s@%s", osname, snapname);
5025 (void) snprintf(clonename, sizeof (clonename),
5026 "%s/ch1_%llu", osname, id);
5027 (void) snprintf(tag, sizeof (tag), "tag_%llu", id);
5030 * Clean up from any previous run.
5032 error = dsl_destroy_head(clonename);
5033 if (error != ENOENT)
5035 error = user_release_one(fullname, tag);
5036 if (error != ESRCH && error != ENOENT)
5038 error = dsl_destroy_snapshot(fullname, B_FALSE);
5039 if (error != ENOENT)
5043 * Create snapshot, clone it, mark snap for deferred destroy,
5044 * destroy clone, verify snap was also destroyed.
5046 error = dmu_objset_snapshot_one(osname, snapname);
5048 if (error == ENOSPC) {
5049 ztest_record_enospc("dmu_objset_snapshot");
5052 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
5055 error = dmu_objset_clone(clonename, fullname);
5057 if (error == ENOSPC) {
5058 ztest_record_enospc("dmu_objset_clone");
5061 fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
5064 error = dsl_destroy_snapshot(fullname, B_TRUE);
5066 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5070 error = dsl_destroy_head(clonename);
5072 fatal(0, "dsl_destroy_head(%s) = %d", clonename, error);
5074 error = dmu_objset_hold(fullname, FTAG, &origin);
5075 if (error != ENOENT)
5076 fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
5079 * Create snapshot, add temporary hold, verify that we can't
5080 * destroy a held snapshot, mark for deferred destroy,
5081 * release hold, verify snapshot was destroyed.
5083 error = dmu_objset_snapshot_one(osname, snapname);
5085 if (error == ENOSPC) {
5086 ztest_record_enospc("dmu_objset_snapshot");
5089 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
5092 holds = fnvlist_alloc();
5093 fnvlist_add_string(holds, fullname, tag);
5094 error = dsl_dataset_user_hold(holds, 0, NULL);
5095 fnvlist_free(holds);
5097 if (error == ENOSPC) {
5098 ztest_record_enospc("dsl_dataset_user_hold");
5101 fatal(0, "dsl_dataset_user_hold(%s, %s) = %u",
5102 fullname, tag, error);
5105 error = dsl_destroy_snapshot(fullname, B_FALSE);
5106 if (error != EBUSY) {
5107 fatal(0, "dsl_destroy_snapshot(%s, B_FALSE) = %d",
5111 error = dsl_destroy_snapshot(fullname, B_TRUE);
5113 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5117 error = user_release_one(fullname, tag);
5119 fatal(0, "user_release_one(%s, %s) = %d", fullname, tag, error);
5121 VERIFY3U(dmu_objset_hold(fullname, FTAG, &origin), ==, ENOENT);
5124 rw_exit(&ztest_name_lock);
5128 * Inject random faults into the on-disk data.
5132 ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
5134 ztest_shared_t *zs = ztest_shared;
5135 spa_t *spa = ztest_spa;
5139 uint64_t bad = 0x1990c0ffeedecadeULL;
5141 char path0[MAXPATHLEN];
5142 char pathrand[MAXPATHLEN];
5144 int bshift = SPA_MAXBLOCKSHIFT + 2;
5150 boolean_t islog = B_FALSE;
5152 mutex_enter(&ztest_vdev_lock);
5155 * Device removal is in progress, fault injection must be disabled
5156 * until it completes and the pool is scrubbed. The fault injection
5157 * strategy for damaging blocks does not take in to account evacuated
5158 * blocks which may have already been damaged.
5160 if (ztest_device_removal_active) {
5161 mutex_exit(&ztest_vdev_lock);
5165 maxfaults = MAXFAULTS();
5166 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
5167 mirror_save = zs->zs_mirrors;
5168 mutex_exit(&ztest_vdev_lock);
5170 ASSERT(leaves >= 1);
5173 * Grab the name lock as reader. There are some operations
5174 * which don't like to have their vdevs changed while
5175 * they are in progress (i.e. spa_change_guid). Those
5176 * operations will have grabbed the name lock as writer.
5178 rw_enter(&ztest_name_lock, RW_READER);
5181 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
5183 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5185 if (ztest_random(2) == 0) {
5187 * Inject errors on a normal data device or slog device.
5189 top = ztest_random_vdev_top(spa, B_TRUE);
5190 leaf = ztest_random(leaves) + zs->zs_splits;
5193 * Generate paths to the first leaf in this top-level vdev,
5194 * and to the random leaf we selected. We'll induce transient
5195 * write failures and random online/offline activity on leaf 0,
5196 * and we'll write random garbage to the randomly chosen leaf.
5198 (void) snprintf(path0, sizeof (path0), ztest_dev_template,
5199 ztest_opts.zo_dir, ztest_opts.zo_pool,
5200 top * leaves + zs->zs_splits);
5201 (void) snprintf(pathrand, sizeof (pathrand), ztest_dev_template,
5202 ztest_opts.zo_dir, ztest_opts.zo_pool,
5203 top * leaves + leaf);
5205 vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
5206 if (vd0 != NULL && vd0->vdev_top->vdev_islog)
5210 * If the top-level vdev needs to be resilvered
5211 * then we only allow faults on the device that is
5214 if (vd0 != NULL && maxfaults != 1 &&
5215 (!vdev_resilver_needed(vd0->vdev_top, NULL, NULL) ||
5216 vd0->vdev_resilver_txg != 0)) {
5218 * Make vd0 explicitly claim to be unreadable,
5219 * or unwriteable, or reach behind its back
5220 * and close the underlying fd. We can do this if
5221 * maxfaults == 0 because we'll fail and reexecute,
5222 * and we can do it if maxfaults >= 2 because we'll
5223 * have enough redundancy. If maxfaults == 1, the
5224 * combination of this with injection of random data
5225 * corruption below exceeds the pool's fault tolerance.
5227 vdev_file_t *vf = vd0->vdev_tsd;
5229 zfs_dbgmsg("injecting fault to vdev %llu; maxfaults=%d",
5230 (long long)vd0->vdev_id, (int)maxfaults);
5232 if (vf != NULL && ztest_random(3) == 0) {
5233 (void) close(vf->vf_vnode->v_fd);
5234 vf->vf_vnode->v_fd = -1;
5235 } else if (ztest_random(2) == 0) {
5236 vd0->vdev_cant_read = B_TRUE;
5238 vd0->vdev_cant_write = B_TRUE;
5240 guid0 = vd0->vdev_guid;
5244 * Inject errors on an l2cache device.
5246 spa_aux_vdev_t *sav = &spa->spa_l2cache;
5248 if (sav->sav_count == 0) {
5249 spa_config_exit(spa, SCL_STATE, FTAG);
5250 rw_exit(&ztest_name_lock);
5253 vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
5254 guid0 = vd0->vdev_guid;
5255 (void) strcpy(path0, vd0->vdev_path);
5256 (void) strcpy(pathrand, vd0->vdev_path);
5260 maxfaults = INT_MAX; /* no limit on cache devices */
5263 spa_config_exit(spa, SCL_STATE, FTAG);
5264 rw_exit(&ztest_name_lock);
5267 * If we can tolerate two or more faults, or we're dealing
5268 * with a slog, randomly online/offline vd0.
5270 if ((maxfaults >= 2 || islog) && guid0 != 0) {
5271 if (ztest_random(10) < 6) {
5272 int flags = (ztest_random(2) == 0 ?
5273 ZFS_OFFLINE_TEMPORARY : 0);
5276 * We have to grab the zs_name_lock as writer to
5277 * prevent a race between offlining a slog and
5278 * destroying a dataset. Offlining the slog will
5279 * grab a reference on the dataset which may cause
5280 * dmu_objset_destroy() to fail with EBUSY thus
5281 * leaving the dataset in an inconsistent state.
5284 rw_enter(&ztest_name_lock, RW_WRITER);
5286 VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
5289 rw_exit(&ztest_name_lock);
5292 * Ideally we would like to be able to randomly
5293 * call vdev_[on|off]line without holding locks
5294 * to force unpredictable failures but the side
5295 * effects of vdev_[on|off]line prevent us from
5296 * doing so. We grab the ztest_vdev_lock here to
5297 * prevent a race between injection testing and
5300 mutex_enter(&ztest_vdev_lock);
5301 (void) vdev_online(spa, guid0, 0, NULL);
5302 mutex_exit(&ztest_vdev_lock);
5310 * We have at least single-fault tolerance, so inject data corruption.
5312 fd = open(pathrand, O_RDWR);
5314 if (fd == -1) /* we hit a gap in the device namespace */
5317 fsize = lseek(fd, 0, SEEK_END);
5319 while (--iters != 0) {
5321 * The offset must be chosen carefully to ensure that
5322 * we do not inject a given logical block with errors
5323 * on two different leaf devices, because ZFS can not
5324 * tolerate that (if maxfaults==1).
5326 * We divide each leaf into chunks of size
5327 * (# leaves * SPA_MAXBLOCKSIZE * 4). Within each chunk
5328 * there is a series of ranges to which we can inject errors.
5329 * Each range can accept errors on only a single leaf vdev.
5330 * The error injection ranges are separated by ranges
5331 * which we will not inject errors on any device (DMZs).
5332 * Each DMZ must be large enough such that a single block
5333 * can not straddle it, so that a single block can not be
5334 * a target in two different injection ranges (on different
5337 * For example, with 3 leaves, each chunk looks like:
5338 * 0 to 32M: injection range for leaf 0
5339 * 32M to 64M: DMZ - no injection allowed
5340 * 64M to 96M: injection range for leaf 1
5341 * 96M to 128M: DMZ - no injection allowed
5342 * 128M to 160M: injection range for leaf 2
5343 * 160M to 192M: DMZ - no injection allowed
5345 offset = ztest_random(fsize / (leaves << bshift)) *
5346 (leaves << bshift) + (leaf << bshift) +
5347 (ztest_random(1ULL << (bshift - 1)) & -8ULL);
5350 * Only allow damage to the labels at one end of the vdev.
5352 * If all labels are damaged, the device will be totally
5353 * inaccessible, which will result in loss of data,
5354 * because we also damage (parts of) the other side of
5357 * Additionally, we will always have both an even and an
5358 * odd label, so that we can handle crashes in the
5359 * middle of vdev_config_sync().
5361 if ((leaf & 1) == 0 && offset < VDEV_LABEL_START_SIZE)
5365 * The two end labels are stored at the "end" of the disk, but
5366 * the end of the disk (vdev_psize) is aligned to
5367 * sizeof (vdev_label_t).
5369 uint64_t psize = P2ALIGN(fsize, sizeof (vdev_label_t));
5370 if ((leaf & 1) == 1 &&
5371 offset + sizeof (bad) > psize - VDEV_LABEL_END_SIZE)
5374 mutex_enter(&ztest_vdev_lock);
5375 if (mirror_save != zs->zs_mirrors) {
5376 mutex_exit(&ztest_vdev_lock);
5381 if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
5382 fatal(1, "can't inject bad word at 0x%llx in %s",
5385 mutex_exit(&ztest_vdev_lock);
5387 if (ztest_opts.zo_verbose >= 7)
5388 (void) printf("injected bad word into %s,"
5389 " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
5396 * Verify that DDT repair works as expected.
5399 ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
5401 ztest_shared_t *zs = ztest_shared;
5402 spa_t *spa = ztest_spa;
5403 objset_t *os = zd->zd_os;
5405 uint64_t object, blocksize, txg, pattern, psize;
5406 enum zio_checksum checksum = spa_dedup_checksum(spa);
5411 int copies = 2 * ZIO_DEDUPDITTO_MIN;
5413 blocksize = ztest_random_blocksize();
5414 blocksize = MIN(blocksize, 2048); /* because we write so many */
5416 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0, 0);
5418 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
5422 * Take the name lock as writer to prevent anyone else from changing
5423 * the pool and dataset properies we need to maintain during this test.
5425 rw_enter(&ztest_name_lock, RW_WRITER);
5427 if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
5429 ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
5431 rw_exit(&ztest_name_lock);
5435 dmu_objset_stats_t dds;
5436 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
5437 dmu_objset_fast_stat(os, &dds);
5438 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
5440 object = od[0].od_object;
5441 blocksize = od[0].od_blocksize;
5442 pattern = zs->zs_guid ^ dds.dds_guid;
5444 ASSERT(object != 0);
5446 tx = dmu_tx_create(os);
5447 dmu_tx_hold_write(tx, object, 0, copies * blocksize);
5448 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
5450 rw_exit(&ztest_name_lock);
5455 * Write all the copies of our block.
5457 for (int i = 0; i < copies; i++) {
5458 uint64_t offset = i * blocksize;
5459 int error = dmu_buf_hold(os, object, offset, FTAG, &db,
5460 DMU_READ_NO_PREFETCH);
5462 fatal(B_FALSE, "dmu_buf_hold(%p, %llu, %llu) = %u",
5463 os, (long long)object, (long long) offset, error);
5465 ASSERT(db->db_offset == offset);
5466 ASSERT(db->db_size == blocksize);
5467 ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
5468 ztest_pattern_match(db->db_data, db->db_size, 0ULL));
5469 dmu_buf_will_fill(db, tx);
5470 ztest_pattern_set(db->db_data, db->db_size, pattern);
5471 dmu_buf_rele(db, FTAG);
5475 txg_wait_synced(spa_get_dsl(spa), txg);
5478 * Find out what block we got.
5480 VERIFY0(dmu_buf_hold(os, object, 0, FTAG, &db,
5481 DMU_READ_NO_PREFETCH));
5482 blk = *((dmu_buf_impl_t *)db)->db_blkptr;
5483 dmu_buf_rele(db, FTAG);
5486 * Damage the block. Dedup-ditto will save us when we read it later.
5488 psize = BP_GET_PSIZE(&blk);
5489 abd = abd_alloc_linear(psize, B_TRUE);
5490 ztest_pattern_set(abd_to_buf(abd), psize, ~pattern);
5492 (void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
5493 abd, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
5494 ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
5498 rw_exit(&ztest_name_lock);
5506 ztest_scrub(ztest_ds_t *zd, uint64_t id)
5508 spa_t *spa = ztest_spa;
5511 * Scrub in progress by device removal.
5513 if (ztest_device_removal_active)
5516 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5517 (void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
5518 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5522 * Change the guid for the pool.
5526 ztest_reguid(ztest_ds_t *zd, uint64_t id)
5528 spa_t *spa = ztest_spa;
5529 uint64_t orig, load;
5532 orig = spa_guid(spa);
5533 load = spa_load_guid(spa);
5535 rw_enter(&ztest_name_lock, RW_WRITER);
5536 error = spa_change_guid(spa);
5537 rw_exit(&ztest_name_lock);
5542 if (ztest_opts.zo_verbose >= 4) {
5543 (void) printf("Changed guid old %llu -> %llu\n",
5544 (u_longlong_t)orig, (u_longlong_t)spa_guid(spa));
5547 VERIFY3U(orig, !=, spa_guid(spa));
5548 VERIFY3U(load, ==, spa_load_guid(spa));
5552 ztest_random_concrete_vdev_leaf(vdev_t *vd)
5557 if (vd->vdev_children == 0)
5560 vdev_t *eligible[vd->vdev_children];
5561 int eligible_idx = 0, i;
5562 for (i = 0; i < vd->vdev_children; i++) {
5563 vdev_t *cvd = vd->vdev_child[i];
5564 if (cvd->vdev_top->vdev_removing)
5566 if (cvd->vdev_children > 0 ||
5567 (vdev_is_concrete(cvd) && !cvd->vdev_detached)) {
5568 eligible[eligible_idx++] = cvd;
5571 VERIFY(eligible_idx > 0);
5573 uint64_t child_no = ztest_random(eligible_idx);
5574 return (ztest_random_concrete_vdev_leaf(eligible[child_no]));
5579 ztest_initialize(ztest_ds_t *zd, uint64_t id)
5581 spa_t *spa = ztest_spa;
5584 mutex_enter(&ztest_vdev_lock);
5586 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
5588 /* Random leaf vdev */
5589 vdev_t *rand_vd = ztest_random_concrete_vdev_leaf(spa->spa_root_vdev);
5590 if (rand_vd == NULL) {
5591 spa_config_exit(spa, SCL_VDEV, FTAG);
5592 mutex_exit(&ztest_vdev_lock);
5597 * The random vdev we've selected may change as soon as we
5598 * drop the spa_config_lock. We create local copies of things
5599 * we're interested in.
5601 uint64_t guid = rand_vd->vdev_guid;
5602 char *path = strdup(rand_vd->vdev_path);
5603 boolean_t active = rand_vd->vdev_initialize_thread != NULL;
5605 zfs_dbgmsg("vd %p, guid %llu", rand_vd, guid);
5606 spa_config_exit(spa, SCL_VDEV, FTAG);
5608 uint64_t cmd = ztest_random(POOL_INITIALIZE_FUNCS);
5609 error = spa_vdev_initialize(spa, guid, cmd);
5611 case POOL_INITIALIZE_CANCEL:
5612 if (ztest_opts.zo_verbose >= 4) {
5613 (void) printf("Cancel initialize %s", path);
5615 (void) printf(" failed (no initialize active)");
5616 (void) printf("\n");
5619 case POOL_INITIALIZE_DO:
5620 if (ztest_opts.zo_verbose >= 4) {
5621 (void) printf("Start initialize %s", path);
5622 if (active && error == 0)
5623 (void) printf(" failed (already active)");
5624 else if (error != 0)
5625 (void) printf(" failed (error %d)", error);
5626 (void) printf("\n");
5629 case POOL_INITIALIZE_SUSPEND:
5630 if (ztest_opts.zo_verbose >= 4) {
5631 (void) printf("Suspend initialize %s", path);
5633 (void) printf(" failed (no initialize active)");
5634 (void) printf("\n");
5639 mutex_exit(&ztest_vdev_lock);
5643 * Verify pool integrity by running zdb.
5646 ztest_run_zdb(char *pool)
5649 char zdb[MAXPATHLEN + MAXNAMELEN + 20];
5657 strlcpy(zdb, "/usr/bin/ztest", sizeof(zdb));
5659 /* zdb lives in /usr/sbin, while ztest lives in /usr/bin */
5660 bin = strstr(zdb, "/usr/bin/");
5661 ztest = strstr(bin, "/ztest");
5663 isalen = ztest - isa;
5667 "/usr/sbin%.*s/zdb -bcc%s%s -G -d -U %s %s",
5670 ztest_opts.zo_verbose >= 3 ? "s" : "",
5671 ztest_opts.zo_verbose >= 4 ? "v" : "",
5676 if (ztest_opts.zo_verbose >= 5)
5677 (void) printf("Executing %s\n", strstr(zdb, "zdb "));
5679 fp = popen(zdb, "r");
5682 while (fgets(zbuf, sizeof (zbuf), fp) != NULL)
5683 if (ztest_opts.zo_verbose >= 3)
5684 (void) printf("%s", zbuf);
5686 status = pclose(fp);
5691 ztest_dump_core = 0;
5692 if (WIFEXITED(status))
5693 fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
5695 fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
5699 ztest_walk_pool_directory(char *header)
5703 if (ztest_opts.zo_verbose >= 6)
5704 (void) printf("%s\n", header);
5706 mutex_enter(&spa_namespace_lock);
5707 while ((spa = spa_next(spa)) != NULL)
5708 if (ztest_opts.zo_verbose >= 6)
5709 (void) printf("\t%s\n", spa_name(spa));
5710 mutex_exit(&spa_namespace_lock);
5714 ztest_spa_import_export(char *oldname, char *newname)
5716 nvlist_t *config, *newconfig;
5721 if (ztest_opts.zo_verbose >= 4) {
5722 (void) printf("import/export: old = %s, new = %s\n",
5727 * Clean up from previous runs.
5729 (void) spa_destroy(newname);
5732 * Get the pool's configuration and guid.
5734 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
5737 * Kick off a scrub to tickle scrub/export races.
5739 if (ztest_random(2) == 0)
5740 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5742 pool_guid = spa_guid(spa);
5743 spa_close(spa, FTAG);
5745 ztest_walk_pool_directory("pools before export");
5750 VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
5752 ztest_walk_pool_directory("pools after export");
5757 newconfig = spa_tryimport(config);
5758 ASSERT(newconfig != NULL);
5759 nvlist_free(newconfig);
5762 * Import it under the new name.
5764 error = spa_import(newname, config, NULL, 0);
5766 dump_nvlist(config, 0);
5767 fatal(B_FALSE, "couldn't import pool %s as %s: error %u",
5768 oldname, newname, error);
5771 ztest_walk_pool_directory("pools after import");
5774 * Try to import it again -- should fail with EEXIST.
5776 VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL, 0));
5779 * Try to import it under a different name -- should fail with EEXIST.
5781 VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL, 0));
5784 * Verify that the pool is no longer visible under the old name.
5786 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5789 * Verify that we can open and close the pool using the new name.
5791 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
5792 ASSERT(pool_guid == spa_guid(spa));
5793 spa_close(spa, FTAG);
5795 nvlist_free(config);
5799 ztest_resume(spa_t *spa)
5801 if (spa_suspended(spa) && ztest_opts.zo_verbose >= 6)
5802 (void) printf("resuming from suspended state\n");
5803 spa_vdev_state_enter(spa, SCL_NONE);
5804 vdev_clear(spa, NULL);
5805 (void) spa_vdev_state_exit(spa, NULL, 0);
5806 (void) zio_resume(spa);
5810 ztest_resume_thread(void *arg)
5814 while (!ztest_exiting) {
5815 if (spa_suspended(spa))
5817 (void) poll(NULL, 0, 100);
5820 * Periodically change the zfs_compressed_arc_enabled setting.
5822 if (ztest_random(10) == 0)
5823 zfs_compressed_arc_enabled = ztest_random(2);
5826 * Periodically change the zfs_abd_scatter_enabled setting.
5828 if (ztest_random(10) == 0)
5829 zfs_abd_scatter_enabled = ztest_random(2);
5835 ztest_deadman_thread(void *arg)
5837 ztest_shared_t *zs = arg;
5838 spa_t *spa = ztest_spa;
5839 hrtime_t delta, total = 0;
5842 delta = zs->zs_thread_stop - zs->zs_thread_start +
5843 MSEC2NSEC(zfs_deadman_synctime_ms);
5845 (void) poll(NULL, 0, (int)NSEC2MSEC(delta));
5848 * If the pool is suspended then fail immediately. Otherwise,
5849 * check to see if the pool is making any progress. If
5850 * vdev_deadman() discovers that there hasn't been any recent
5851 * I/Os then it will end up aborting the tests.
5853 if (spa_suspended(spa) || spa->spa_root_vdev == NULL) {
5854 fatal(0, "aborting test after %llu seconds because "
5855 "pool has transitioned to a suspended state.",
5856 zfs_deadman_synctime_ms / 1000);
5859 vdev_deadman(spa->spa_root_vdev);
5861 total += zfs_deadman_synctime_ms/1000;
5862 (void) printf("ztest has been running for %lld seconds\n",
5868 ztest_execute(int test, ztest_info_t *zi, uint64_t id)
5870 ztest_ds_t *zd = &ztest_ds[id % ztest_opts.zo_datasets];
5871 ztest_shared_callstate_t *zc = ZTEST_GET_SHARED_CALLSTATE(test);
5872 hrtime_t functime = gethrtime();
5874 for (int i = 0; i < zi->zi_iters; i++)
5875 zi->zi_func(zd, id);
5877 functime = gethrtime() - functime;
5879 atomic_add_64(&zc->zc_count, 1);
5880 atomic_add_64(&zc->zc_time, functime);
5882 if (ztest_opts.zo_verbose >= 4) {
5884 (void) dladdr((void *)zi->zi_func, &dli);
5885 (void) printf("%6.2f sec in %s\n",
5886 (double)functime / NANOSEC, dli.dli_sname);
5891 ztest_thread(void *arg)
5894 uint64_t id = (uintptr_t)arg;
5895 ztest_shared_t *zs = ztest_shared;
5899 ztest_shared_callstate_t *zc;
5901 while ((now = gethrtime()) < zs->zs_thread_stop) {
5903 * See if it's time to force a crash.
5905 if (now > zs->zs_thread_kill)
5909 * If we're getting ENOSPC with some regularity, stop.
5911 if (zs->zs_enospc_count > 10)
5915 * Pick a random function to execute.
5917 rand = ztest_random(ZTEST_FUNCS);
5918 zi = &ztest_info[rand];
5919 zc = ZTEST_GET_SHARED_CALLSTATE(rand);
5920 call_next = zc->zc_next;
5922 if (now >= call_next &&
5923 atomic_cas_64(&zc->zc_next, call_next, call_next +
5924 ztest_random(2 * zi->zi_interval[0] + 1)) == call_next) {
5925 ztest_execute(rand, zi, id);
5933 ztest_dataset_name(char *dsname, char *pool, int d)
5935 (void) snprintf(dsname, ZFS_MAX_DATASET_NAME_LEN, "%s/ds_%d", pool, d);
5939 ztest_dataset_destroy(int d)
5941 char name[ZFS_MAX_DATASET_NAME_LEN];
5943 ztest_dataset_name(name, ztest_opts.zo_pool, d);
5945 if (ztest_opts.zo_verbose >= 3)
5946 (void) printf("Destroying %s to free up space\n", name);
5949 * Cleanup any non-standard clones and snapshots. In general,
5950 * ztest thread t operates on dataset (t % zopt_datasets),
5951 * so there may be more than one thing to clean up.
5953 for (int t = d; t < ztest_opts.zo_threads;
5954 t += ztest_opts.zo_datasets) {
5955 ztest_dsl_dataset_cleanup(name, t);
5958 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
5959 DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
5963 ztest_dataset_dirobj_verify(ztest_ds_t *zd)
5965 uint64_t usedobjs, dirobjs, scratch;
5968 * ZTEST_DIROBJ is the object directory for the entire dataset.
5969 * Therefore, the number of objects in use should equal the
5970 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
5971 * If not, we have an object leak.
5973 * Note that we can only check this in ztest_dataset_open(),
5974 * when the open-context and syncing-context values agree.
5975 * That's because zap_count() returns the open-context value,
5976 * while dmu_objset_space() returns the rootbp fill count.
5978 VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
5979 dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
5980 ASSERT3U(dirobjs + 1, ==, usedobjs);
5984 ztest_dataset_open(int d)
5986 ztest_ds_t *zd = &ztest_ds[d];
5987 uint64_t committed_seq = ZTEST_GET_SHARED_DS(d)->zd_seq;
5990 char name[ZFS_MAX_DATASET_NAME_LEN];
5993 ztest_dataset_name(name, ztest_opts.zo_pool, d);
5995 rw_enter(&ztest_name_lock, RW_READER);
5997 error = ztest_dataset_create(name);
5998 if (error == ENOSPC) {
5999 rw_exit(&ztest_name_lock);
6000 ztest_record_enospc(FTAG);
6003 ASSERT(error == 0 || error == EEXIST);
6005 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, zd, &os));
6006 rw_exit(&ztest_name_lock);
6008 ztest_zd_init(zd, ZTEST_GET_SHARED_DS(d), os);
6010 zilog = zd->zd_zilog;
6012 if (zilog->zl_header->zh_claim_lr_seq != 0 &&
6013 zilog->zl_header->zh_claim_lr_seq < committed_seq)
6014 fatal(0, "missing log records: claimed %llu < committed %llu",
6015 zilog->zl_header->zh_claim_lr_seq, committed_seq);
6017 ztest_dataset_dirobj_verify(zd);
6019 zil_replay(os, zd, ztest_replay_vector);
6021 ztest_dataset_dirobj_verify(zd);
6023 if (ztest_opts.zo_verbose >= 6)
6024 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
6026 (u_longlong_t)zilog->zl_parse_blk_count,
6027 (u_longlong_t)zilog->zl_parse_lr_count,
6028 (u_longlong_t)zilog->zl_replaying_seq);
6030 zilog = zil_open(os, ztest_get_data);
6032 if (zilog->zl_replaying_seq != 0 &&
6033 zilog->zl_replaying_seq < committed_seq)
6034 fatal(0, "missing log records: replayed %llu < committed %llu",
6035 zilog->zl_replaying_seq, committed_seq);
6041 ztest_dataset_close(int d)
6043 ztest_ds_t *zd = &ztest_ds[d];
6045 zil_close(zd->zd_zilog);
6046 dmu_objset_disown(zd->zd_os, zd);
6052 * Kick off threads to run tests on all datasets in parallel.
6055 ztest_run(ztest_shared_t *zs)
6060 thread_t resume_tid;
6063 ztest_exiting = B_FALSE;
6066 * Initialize parent/child shared state.
6068 mutex_init(&ztest_checkpoint_lock, NULL, USYNC_THREAD, NULL);
6069 mutex_init(&ztest_vdev_lock, NULL, USYNC_THREAD, NULL);
6070 rw_init(&ztest_name_lock, NULL, USYNC_THREAD, NULL);
6072 zs->zs_thread_start = gethrtime();
6073 zs->zs_thread_stop =
6074 zs->zs_thread_start + ztest_opts.zo_passtime * NANOSEC;
6075 zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
6076 zs->zs_thread_kill = zs->zs_thread_stop;
6077 if (ztest_random(100) < ztest_opts.zo_killrate) {
6078 zs->zs_thread_kill -=
6079 ztest_random(ztest_opts.zo_passtime * NANOSEC);
6082 mutex_init(&zcl.zcl_callbacks_lock, NULL, USYNC_THREAD, NULL);
6084 list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
6085 offsetof(ztest_cb_data_t, zcd_node));
6090 kernel_init(FREAD | FWRITE);
6091 VERIFY0(spa_open(ztest_opts.zo_pool, &spa, FTAG));
6092 metaslab_preload_limit = ztest_random(20) + 1;
6095 dmu_objset_stats_t dds;
6096 VERIFY0(dmu_objset_own(ztest_opts.zo_pool,
6097 DMU_OST_ANY, B_TRUE, FTAG, &os));
6098 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
6099 dmu_objset_fast_stat(os, &dds);
6100 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
6101 zs->zs_guid = dds.dds_guid;
6102 dmu_objset_disown(os, FTAG);
6104 spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
6107 * We don't expect the pool to suspend unless maxfaults == 0,
6108 * in which case ztest_fault_inject() temporarily takes away
6109 * the only valid replica.
6111 if (MAXFAULTS() == 0)
6112 spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
6114 spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
6117 * Create a thread to periodically resume suspended I/O.
6119 VERIFY(thr_create(0, 0, ztest_resume_thread, spa, THR_BOUND,
6123 * Create a deadman thread to abort() if we hang.
6125 VERIFY(thr_create(0, 0, ztest_deadman_thread, zs, THR_BOUND,
6129 * Verify that we can safely inquire about any object,
6130 * whether it's allocated or not. To make it interesting,
6131 * we probe a 5-wide window around each power of two.
6132 * This hits all edge cases, including zero and the max.
6134 for (int t = 0; t < 64; t++) {
6135 for (int d = -5; d <= 5; d++) {
6136 error = dmu_object_info(spa->spa_meta_objset,
6137 (1ULL << t) + d, NULL);
6138 ASSERT(error == 0 || error == ENOENT ||
6144 * If we got any ENOSPC errors on the previous run, destroy something.
6146 if (zs->zs_enospc_count != 0) {
6147 int d = ztest_random(ztest_opts.zo_datasets);
6148 ztest_dataset_destroy(d);
6150 zs->zs_enospc_count = 0;
6152 tid = umem_zalloc(ztest_opts.zo_threads * sizeof (thread_t),
6155 if (ztest_opts.zo_verbose >= 4)
6156 (void) printf("starting main threads...\n");
6159 * Kick off all the tests that run in parallel.
6161 for (int t = 0; t < ztest_opts.zo_threads; t++) {
6162 if (t < ztest_opts.zo_datasets &&
6163 ztest_dataset_open(t) != 0)
6165 VERIFY(thr_create(0, 0, ztest_thread, (void *)(uintptr_t)t,
6166 THR_BOUND, &tid[t]) == 0);
6170 * Wait for all of the tests to complete. We go in reverse order
6171 * so we don't close datasets while threads are still using them.
6173 for (int t = ztest_opts.zo_threads - 1; t >= 0; t--) {
6174 VERIFY(thr_join(tid[t], NULL, NULL) == 0);
6175 if (t < ztest_opts.zo_datasets)
6176 ztest_dataset_close(t);
6179 txg_wait_synced(spa_get_dsl(spa), 0);
6181 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
6182 zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
6183 zfs_dbgmsg_print(FTAG);
6185 umem_free(tid, ztest_opts.zo_threads * sizeof (thread_t));
6187 /* Kill the resume thread */
6188 ztest_exiting = B_TRUE;
6189 VERIFY(thr_join(resume_tid, NULL, NULL) == 0);
6193 * Right before closing the pool, kick off a bunch of async I/O;
6194 * spa_close() should wait for it to complete.
6196 for (uint64_t object = 1; object < 50; object++) {
6197 dmu_prefetch(spa->spa_meta_objset, object, 0, 0, 1ULL << 20,
6198 ZIO_PRIORITY_SYNC_READ);
6201 spa_close(spa, FTAG);
6204 * Verify that we can loop over all pools.
6206 mutex_enter(&spa_namespace_lock);
6207 for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
6208 if (ztest_opts.zo_verbose > 3)
6209 (void) printf("spa_next: found %s\n", spa_name(spa));
6210 mutex_exit(&spa_namespace_lock);
6213 * Verify that we can export the pool and reimport it under a
6216 if (ztest_random(2) == 0) {
6217 char name[ZFS_MAX_DATASET_NAME_LEN];
6218 (void) snprintf(name, sizeof (name), "%s_import",
6219 ztest_opts.zo_pool);
6220 ztest_spa_import_export(ztest_opts.zo_pool, name);
6221 ztest_spa_import_export(name, ztest_opts.zo_pool);
6226 list_destroy(&zcl.zcl_callbacks);
6228 mutex_destroy(&zcl.zcl_callbacks_lock);
6230 rw_destroy(&ztest_name_lock);
6231 mutex_destroy(&ztest_vdev_lock);
6232 mutex_destroy(&ztest_checkpoint_lock);
6238 ztest_ds_t *zd = &ztest_ds[0];
6242 if (ztest_opts.zo_verbose >= 3)
6243 (void) printf("testing spa_freeze()...\n");
6245 kernel_init(FREAD | FWRITE);
6246 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6247 VERIFY3U(0, ==, ztest_dataset_open(0));
6251 * Force the first log block to be transactionally allocated.
6252 * We have to do this before we freeze the pool -- otherwise
6253 * the log chain won't be anchored.
6255 while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
6256 ztest_dmu_object_alloc_free(zd, 0);
6257 zil_commit(zd->zd_zilog, 0);
6260 txg_wait_synced(spa_get_dsl(spa), 0);
6263 * Freeze the pool. This stops spa_sync() from doing anything,
6264 * so that the only way to record changes from now on is the ZIL.
6269 * Because it is hard to predict how much space a write will actually
6270 * require beforehand, we leave ourselves some fudge space to write over
6273 uint64_t capacity = metaslab_class_get_space(spa_normal_class(spa)) / 2;
6276 * Run tests that generate log records but don't alter the pool config
6277 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
6278 * We do a txg_wait_synced() after each iteration to force the txg
6279 * to increase well beyond the last synced value in the uberblock.
6280 * The ZIL should be OK with that.
6282 * Run a random number of times less than zo_maxloops and ensure we do
6283 * not run out of space on the pool.
6285 while (ztest_random(10) != 0 &&
6286 numloops++ < ztest_opts.zo_maxloops &&
6287 metaslab_class_get_alloc(spa_normal_class(spa)) < capacity) {
6289 ztest_od_init(&od, 0, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
6290 VERIFY0(ztest_object_init(zd, &od, sizeof (od), B_FALSE));
6291 ztest_io(zd, od.od_object,
6292 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
6293 txg_wait_synced(spa_get_dsl(spa), 0);
6297 * Commit all of the changes we just generated.
6299 zil_commit(zd->zd_zilog, 0);
6300 txg_wait_synced(spa_get_dsl(spa), 0);
6303 * Close our dataset and close the pool.
6305 ztest_dataset_close(0);
6306 spa_close(spa, FTAG);
6310 * Open and close the pool and dataset to induce log replay.
6312 kernel_init(FREAD | FWRITE);
6313 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6314 ASSERT(spa_freeze_txg(spa) == UINT64_MAX);
6315 VERIFY3U(0, ==, ztest_dataset_open(0));
6316 ztest_dataset_close(0);
6319 txg_wait_synced(spa_get_dsl(spa), 0);
6320 ztest_reguid(NULL, 0);
6322 spa_close(spa, FTAG);
6327 print_time(hrtime_t t, char *timebuf)
6329 hrtime_t s = t / NANOSEC;
6330 hrtime_t m = s / 60;
6331 hrtime_t h = m / 60;
6332 hrtime_t d = h / 24;
6341 (void) sprintf(timebuf,
6342 "%llud%02lluh%02llum%02llus", d, h, m, s);
6344 (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
6346 (void) sprintf(timebuf, "%llum%02llus", m, s);
6348 (void) sprintf(timebuf, "%llus", s);
6356 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
6357 if (ztest_random(2) == 0)
6359 VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
6365 * Create a storage pool with the given name and initial vdev size.
6366 * Then test spa_freeze() functionality.
6369 ztest_init(ztest_shared_t *zs)
6372 nvlist_t *nvroot, *props;
6374 mutex_init(&ztest_vdev_lock, NULL, USYNC_THREAD, NULL);
6375 mutex_init(&ztest_checkpoint_lock, NULL, USYNC_THREAD, NULL);
6376 rw_init(&ztest_name_lock, NULL, USYNC_THREAD, NULL);
6378 kernel_init(FREAD | FWRITE);
6381 * Create the storage pool.
6383 (void) spa_destroy(ztest_opts.zo_pool);
6384 ztest_shared->zs_vdev_next_leaf = 0;
6386 zs->zs_mirrors = ztest_opts.zo_mirrors;
6387 nvroot = make_vdev_root(NULL, NULL, NULL, ztest_opts.zo_vdev_size, 0,
6388 0, ztest_opts.zo_raidz, zs->zs_mirrors, 1);
6389 props = make_random_props();
6390 for (int i = 0; i < SPA_FEATURES; i++) {
6392 (void) snprintf(buf, sizeof (buf), "feature@%s",
6393 spa_feature_table[i].fi_uname);
6394 VERIFY3U(0, ==, nvlist_add_uint64(props, buf, 0));
6396 VERIFY3U(0, ==, spa_create(ztest_opts.zo_pool, nvroot, props, NULL));
6397 nvlist_free(nvroot);
6400 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6401 zs->zs_metaslab_sz =
6402 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
6404 spa_close(spa, FTAG);
6408 ztest_run_zdb(ztest_opts.zo_pool);
6412 ztest_run_zdb(ztest_opts.zo_pool);
6414 rw_destroy(&ztest_name_lock);
6415 mutex_destroy(&ztest_vdev_lock);
6416 mutex_destroy(&ztest_checkpoint_lock);
6422 static char ztest_name_data[] = "/tmp/ztest.data.XXXXXX";
6424 ztest_fd_data = mkstemp(ztest_name_data);
6425 ASSERT3S(ztest_fd_data, >=, 0);
6426 (void) unlink(ztest_name_data);
6431 shared_data_size(ztest_shared_hdr_t *hdr)
6435 size = hdr->zh_hdr_size;
6436 size += hdr->zh_opts_size;
6437 size += hdr->zh_size;
6438 size += hdr->zh_stats_size * hdr->zh_stats_count;
6439 size += hdr->zh_ds_size * hdr->zh_ds_count;
6448 ztest_shared_hdr_t *hdr;
6450 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6451 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6452 ASSERT(hdr != MAP_FAILED);
6454 VERIFY3U(0, ==, ftruncate(ztest_fd_data, sizeof (ztest_shared_hdr_t)));
6456 hdr->zh_hdr_size = sizeof (ztest_shared_hdr_t);
6457 hdr->zh_opts_size = sizeof (ztest_shared_opts_t);
6458 hdr->zh_size = sizeof (ztest_shared_t);
6459 hdr->zh_stats_size = sizeof (ztest_shared_callstate_t);
6460 hdr->zh_stats_count = ZTEST_FUNCS;
6461 hdr->zh_ds_size = sizeof (ztest_shared_ds_t);
6462 hdr->zh_ds_count = ztest_opts.zo_datasets;
6464 size = shared_data_size(hdr);
6465 VERIFY3U(0, ==, ftruncate(ztest_fd_data, size));
6467 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6474 ztest_shared_hdr_t *hdr;
6477 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6478 PROT_READ, MAP_SHARED, ztest_fd_data, 0);
6479 ASSERT(hdr != MAP_FAILED);
6481 size = shared_data_size(hdr);
6483 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6484 hdr = ztest_shared_hdr = (void *)mmap(0, P2ROUNDUP(size, getpagesize()),
6485 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6486 ASSERT(hdr != MAP_FAILED);
6487 buf = (uint8_t *)hdr;
6489 offset = hdr->zh_hdr_size;
6490 ztest_shared_opts = (void *)&buf[offset];
6491 offset += hdr->zh_opts_size;
6492 ztest_shared = (void *)&buf[offset];
6493 offset += hdr->zh_size;
6494 ztest_shared_callstate = (void *)&buf[offset];
6495 offset += hdr->zh_stats_size * hdr->zh_stats_count;
6496 ztest_shared_ds = (void *)&buf[offset];
6500 exec_child(char *cmd, char *libpath, boolean_t ignorekill, int *statusp)
6504 char *cmdbuf = NULL;
6509 cmdbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
6510 (void) strlcpy(cmdbuf, getexecname(), MAXPATHLEN);
6515 fatal(1, "fork failed");
6517 if (pid == 0) { /* child */
6518 char *emptyargv[2] = { cmd, NULL };
6519 char fd_data_str[12];
6521 struct rlimit rl = { 1024, 1024 };
6522 (void) setrlimit(RLIMIT_NOFILE, &rl);
6524 (void) close(ztest_fd_rand);
6526 snprintf(fd_data_str, 12, "%d", ztest_fd_data));
6527 VERIFY0(setenv("ZTEST_FD_DATA", fd_data_str, 1));
6529 (void) enable_extended_FILE_stdio(-1, -1);
6530 if (libpath != NULL)
6531 VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath, 1));
6533 (void) execv(cmd, emptyargv);
6535 (void) execvp(cmd, emptyargv);
6537 ztest_dump_core = B_FALSE;
6538 fatal(B_TRUE, "exec failed: %s", cmd);
6541 if (cmdbuf != NULL) {
6542 umem_free(cmdbuf, MAXPATHLEN);
6546 while (waitpid(pid, &status, 0) != pid)
6548 if (statusp != NULL)
6551 if (WIFEXITED(status)) {
6552 if (WEXITSTATUS(status) != 0) {
6553 (void) fprintf(stderr, "child exited with code %d\n",
6554 WEXITSTATUS(status));
6558 } else if (WIFSIGNALED(status)) {
6559 if (!ignorekill || WTERMSIG(status) != SIGKILL) {
6560 (void) fprintf(stderr, "child died with signal %d\n",
6566 (void) fprintf(stderr, "something strange happened to child\n");
6573 ztest_run_init(void)
6575 ztest_shared_t *zs = ztest_shared;
6577 ASSERT(ztest_opts.zo_init != 0);
6580 * Blow away any existing copy of zpool.cache
6582 (void) remove(spa_config_path);
6585 * Create and initialize our storage pool.
6587 for (int i = 1; i <= ztest_opts.zo_init; i++) {
6588 bzero(zs, sizeof (ztest_shared_t));
6589 if (ztest_opts.zo_verbose >= 3 &&
6590 ztest_opts.zo_init != 1) {
6591 (void) printf("ztest_init(), pass %d\n", i);
6598 main(int argc, char **argv)
6606 ztest_shared_callstate_t *zc;
6608 char numbuf[NN_NUMBUF_SZ];
6611 char *fd_data_str = getenv("ZTEST_FD_DATA");
6613 (void) setvbuf(stdout, NULL, _IOLBF, 0);
6615 dprintf_setup(&argc, argv);
6616 zfs_deadman_synctime_ms = 300000;
6618 * As two-word space map entries may not come up often (especially
6619 * if pool and vdev sizes are small) we want to force at least some
6620 * of them so the feature get tested.
6622 zfs_force_some_double_word_sm_entries = B_TRUE;
6624 ztest_fd_rand = open("/dev/urandom", O_RDONLY);
6625 ASSERT3S(ztest_fd_rand, >=, 0);
6628 process_options(argc, argv);
6633 bcopy(&ztest_opts, ztest_shared_opts,
6634 sizeof (*ztest_shared_opts));
6636 ztest_fd_data = atoi(fd_data_str);
6638 bcopy(ztest_shared_opts, &ztest_opts, sizeof (ztest_opts));
6640 ASSERT3U(ztest_opts.zo_datasets, ==, ztest_shared_hdr->zh_ds_count);
6642 /* Override location of zpool.cache */
6643 VERIFY3U(asprintf((char **)&spa_config_path, "%s/zpool.cache",
6644 ztest_opts.zo_dir), !=, -1);
6646 ztest_ds = umem_alloc(ztest_opts.zo_datasets * sizeof (ztest_ds_t),
6651 metaslab_force_ganging = ztest_opts.zo_metaslab_force_ganging;
6652 metaslab_df_alloc_threshold =
6653 zs->zs_metaslab_df_alloc_threshold;
6662 hasalt = (strlen(ztest_opts.zo_alt_ztest) != 0);
6664 if (ztest_opts.zo_verbose >= 1) {
6665 (void) printf("%llu vdevs, %d datasets, %d threads,"
6666 " %llu seconds...\n",
6667 (u_longlong_t)ztest_opts.zo_vdevs,
6668 ztest_opts.zo_datasets,
6669 ztest_opts.zo_threads,
6670 (u_longlong_t)ztest_opts.zo_time);
6673 cmd = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
6674 (void) strlcpy(cmd, getexecname(), MAXNAMELEN);
6676 zs->zs_do_init = B_TRUE;
6677 if (strlen(ztest_opts.zo_alt_ztest) != 0) {
6678 if (ztest_opts.zo_verbose >= 1) {
6679 (void) printf("Executing older ztest for "
6680 "initialization: %s\n", ztest_opts.zo_alt_ztest);
6682 VERIFY(!exec_child(ztest_opts.zo_alt_ztest,
6683 ztest_opts.zo_alt_libpath, B_FALSE, NULL));
6685 VERIFY(!exec_child(NULL, NULL, B_FALSE, NULL));
6687 zs->zs_do_init = B_FALSE;
6689 zs->zs_proc_start = gethrtime();
6690 zs->zs_proc_stop = zs->zs_proc_start + ztest_opts.zo_time * NANOSEC;
6692 for (int f = 0; f < ZTEST_FUNCS; f++) {
6693 zi = &ztest_info[f];
6694 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6695 if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
6696 zc->zc_next = UINT64_MAX;
6698 zc->zc_next = zs->zs_proc_start +
6699 ztest_random(2 * zi->zi_interval[0] + 1);
6703 * Run the tests in a loop. These tests include fault injection
6704 * to verify that self-healing data works, and forced crashes
6705 * to verify that we never lose on-disk consistency.
6707 while (gethrtime() < zs->zs_proc_stop) {
6712 * Initialize the workload counters for each function.
6714 for (int f = 0; f < ZTEST_FUNCS; f++) {
6715 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6720 /* Set the allocation switch size */
6721 zs->zs_metaslab_df_alloc_threshold =
6722 ztest_random(zs->zs_metaslab_sz / 4) + 1;
6724 if (!hasalt || ztest_random(2) == 0) {
6725 if (hasalt && ztest_opts.zo_verbose >= 1) {
6726 (void) printf("Executing newer ztest: %s\n",
6730 killed = exec_child(cmd, NULL, B_TRUE, &status);
6732 if (hasalt && ztest_opts.zo_verbose >= 1) {
6733 (void) printf("Executing older ztest: %s\n",
6734 ztest_opts.zo_alt_ztest);
6737 killed = exec_child(ztest_opts.zo_alt_ztest,
6738 ztest_opts.zo_alt_libpath, B_TRUE, &status);
6745 if (ztest_opts.zo_verbose >= 1) {
6746 hrtime_t now = gethrtime();
6748 now = MIN(now, zs->zs_proc_stop);
6749 print_time(zs->zs_proc_stop - now, timebuf);
6750 nicenum(zs->zs_space, numbuf, sizeof (numbuf));
6752 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
6753 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
6755 WIFEXITED(status) ? "Complete" : "SIGKILL",
6756 (u_longlong_t)zs->zs_enospc_count,
6757 100.0 * zs->zs_alloc / zs->zs_space,
6759 100.0 * (now - zs->zs_proc_start) /
6760 (ztest_opts.zo_time * NANOSEC), timebuf);
6763 if (ztest_opts.zo_verbose >= 2) {
6764 (void) printf("\nWorkload summary:\n\n");
6765 (void) printf("%7s %9s %s\n",
6766 "Calls", "Time", "Function");
6767 (void) printf("%7s %9s %s\n",
6768 "-----", "----", "--------");
6769 for (int f = 0; f < ZTEST_FUNCS; f++) {
6772 zi = &ztest_info[f];
6773 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6774 print_time(zc->zc_time, timebuf);
6775 (void) dladdr((void *)zi->zi_func, &dli);
6776 (void) printf("%7llu %9s %s\n",
6777 (u_longlong_t)zc->zc_count, timebuf,
6780 (void) printf("\n");
6783 ztest_run_zdb(ztest_opts.zo_pool);
6786 if (ztest_opts.zo_verbose >= 1) {
6788 (void) printf("%d runs of older ztest: %s\n", older,
6789 ztest_opts.zo_alt_ztest);
6790 (void) printf("%d runs of newer ztest: %s\n", newer,
6793 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
6794 kills, iters - kills, (100.0 * kills) / MAX(1, iters));
6797 umem_free(cmd, MAXNAMELEN);