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_spa_rename;
342 ztest_func_t ztest_scrub;
343 ztest_func_t ztest_dsl_dataset_promote_busy;
344 ztest_func_t ztest_vdev_attach_detach;
345 ztest_func_t ztest_vdev_LUN_growth;
346 ztest_func_t ztest_vdev_add_remove;
347 ztest_func_t ztest_vdev_aux_add_remove;
348 ztest_func_t ztest_split_pool;
349 ztest_func_t ztest_reguid;
350 ztest_func_t ztest_spa_upgrade;
351 ztest_func_t ztest_device_removal;
352 ztest_func_t ztest_remap_blocks;
353 ztest_func_t ztest_spa_checkpoint_create_discard;
354 ztest_func_t ztest_initialize;
355 ztest_func_t ztest_verify_dnode_bt;
357 uint64_t zopt_always = 0ULL * NANOSEC; /* all the time */
358 uint64_t zopt_incessant = 1ULL * NANOSEC / 10; /* every 1/10 second */
359 uint64_t zopt_often = 1ULL * NANOSEC; /* every second */
360 uint64_t zopt_sometimes = 10ULL * NANOSEC; /* every 10 seconds */
361 uint64_t zopt_rarely = 60ULL * NANOSEC; /* every 60 seconds */
363 ztest_info_t ztest_info[] = {
364 { ztest_dmu_read_write, 1, &zopt_always },
365 { ztest_dmu_write_parallel, 10, &zopt_always },
366 { ztest_dmu_object_alloc_free, 1, &zopt_always },
367 { ztest_dmu_commit_callbacks, 1, &zopt_always },
368 { ztest_zap, 30, &zopt_always },
369 { ztest_zap_parallel, 100, &zopt_always },
370 { ztest_split_pool, 1, &zopt_always },
371 { ztest_zil_commit, 1, &zopt_incessant },
372 { ztest_zil_remount, 1, &zopt_sometimes },
373 { ztest_dmu_read_write_zcopy, 1, &zopt_often },
374 { ztest_dmu_objset_create_destroy, 1, &zopt_often },
375 { ztest_dsl_prop_get_set, 1, &zopt_often },
376 { ztest_spa_prop_get_set, 1, &zopt_sometimes },
378 { ztest_dmu_prealloc, 1, &zopt_sometimes },
380 { ztest_fzap, 1, &zopt_sometimes },
381 { ztest_dmu_snapshot_create_destroy, 1, &zopt_sometimes },
382 { ztest_spa_create_destroy, 1, &zopt_sometimes },
383 { ztest_fault_inject, 1, &zopt_incessant },
384 { ztest_ddt_repair, 1, &zopt_sometimes },
385 { ztest_dmu_snapshot_hold, 1, &zopt_sometimes },
386 { ztest_reguid, 1, &zopt_rarely },
387 { ztest_spa_rename, 1, &zopt_rarely },
388 { ztest_scrub, 1, &zopt_often },
389 { ztest_spa_upgrade, 1, &zopt_rarely },
390 { ztest_dsl_dataset_promote_busy, 1, &zopt_rarely },
391 { ztest_vdev_attach_detach, 1, &zopt_incessant },
392 { ztest_vdev_LUN_growth, 1, &zopt_rarely },
393 { ztest_vdev_add_remove, 1,
394 &ztest_opts.zo_vdevtime },
395 { ztest_vdev_aux_add_remove, 1,
396 &ztest_opts.zo_vdevtime },
397 { ztest_device_removal, 1, &zopt_sometimes },
398 { ztest_remap_blocks, 1, &zopt_sometimes },
399 { ztest_spa_checkpoint_create_discard, 1, &zopt_rarely },
400 { ztest_initialize, 1, &zopt_sometimes },
401 { ztest_verify_dnode_bt, 1, &zopt_sometimes }
404 #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
407 * The following struct is used to hold a list of uncalled commit callbacks.
408 * The callbacks are ordered by txg number.
410 typedef struct ztest_cb_list {
411 kmutex_t zcl_callbacks_lock;
412 list_t zcl_callbacks;
416 * Stuff we need to share writably between parent and child.
418 typedef struct ztest_shared {
419 boolean_t zs_do_init;
420 hrtime_t zs_proc_start;
421 hrtime_t zs_proc_stop;
422 hrtime_t zs_thread_start;
423 hrtime_t zs_thread_stop;
424 hrtime_t zs_thread_kill;
425 uint64_t zs_enospc_count;
426 uint64_t zs_vdev_next_leaf;
427 uint64_t zs_vdev_aux;
432 uint64_t zs_metaslab_sz;
433 uint64_t zs_metaslab_df_alloc_threshold;
437 #define ID_PARALLEL -1ULL
439 static char ztest_dev_template[] = "%s/%s.%llua";
440 static char ztest_aux_template[] = "%s/%s.%s.%llu";
441 ztest_shared_t *ztest_shared;
443 static spa_t *ztest_spa = NULL;
444 static ztest_ds_t *ztest_ds;
446 static kmutex_t ztest_vdev_lock;
447 static boolean_t ztest_device_removal_active = B_FALSE;
448 static kmutex_t ztest_checkpoint_lock;
451 * The ztest_name_lock protects the pool and dataset namespace used by
452 * the individual tests. To modify the namespace, consumers must grab
453 * this lock as writer. Grabbing the lock as reader will ensure that the
454 * namespace does not change while the lock is held.
456 static krwlock_t ztest_name_lock;
458 static boolean_t ztest_dump_core = B_TRUE;
459 static boolean_t ztest_exiting;
461 /* Global commit callback list */
462 static ztest_cb_list_t zcl;
465 ZTEST_META_DNODE = 0,
470 static void usage(boolean_t) __NORETURN;
473 * These libumem hooks provide a reasonable set of defaults for the allocator's
474 * debugging facilities.
479 return ("default,verbose"); /* $UMEM_DEBUG setting */
483 _umem_logging_init(void)
485 return ("fail,contents"); /* $UMEM_LOGGING setting */
488 #define FATAL_MSG_SZ 1024
493 fatal(int do_perror, char *message, ...)
496 int save_errno = errno;
497 char buf[FATAL_MSG_SZ];
499 (void) fflush(stdout);
501 va_start(args, message);
502 (void) sprintf(buf, "ztest: ");
504 (void) vsprintf(buf + strlen(buf), message, args);
507 (void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
508 ": %s", strerror(save_errno));
510 (void) fprintf(stderr, "%s\n", buf);
511 fatal_msg = buf; /* to ease debugging */
518 str2shift(const char *buf)
520 const char *ends = "BKMGTPEZ";
525 for (i = 0; i < strlen(ends); i++) {
526 if (toupper(buf[0]) == ends[i])
529 if (i == strlen(ends)) {
530 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
534 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
537 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
543 nicenumtoull(const char *buf)
548 val = strtoull(buf, &end, 0);
550 (void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
552 } else if (end[0] == '.') {
553 double fval = strtod(buf, &end);
554 fval *= pow(2, str2shift(end));
555 if (fval > UINT64_MAX) {
556 (void) fprintf(stderr, "ztest: value too large: %s\n",
560 val = (uint64_t)fval;
562 int shift = str2shift(end);
563 if (shift >= 64 || (val << shift) >> shift != val) {
564 (void) fprintf(stderr, "ztest: value too large: %s\n",
574 usage(boolean_t requested)
576 const ztest_shared_opts_t *zo = &ztest_opts_defaults;
578 char nice_vdev_size[NN_NUMBUF_SZ];
579 char nice_force_ganging[NN_NUMBUF_SZ];
580 FILE *fp = requested ? stdout : stderr;
582 nicenum(zo->zo_vdev_size, nice_vdev_size, sizeof (nice_vdev_size));
583 nicenum(zo->zo_metaslab_force_ganging, nice_force_ganging,
584 sizeof (nice_force_ganging));
586 (void) fprintf(fp, "Usage: %s\n"
587 "\t[-v vdevs (default: %llu)]\n"
588 "\t[-s size_of_each_vdev (default: %s)]\n"
589 "\t[-a alignment_shift (default: %d)] use 0 for random\n"
590 "\t[-m mirror_copies (default: %d)]\n"
591 "\t[-r raidz_disks (default: %d)]\n"
592 "\t[-R raidz_parity (default: %d)]\n"
593 "\t[-d datasets (default: %d)]\n"
594 "\t[-t threads (default: %d)]\n"
595 "\t[-g gang_block_threshold (default: %s)]\n"
596 "\t[-i init_count (default: %d)] initialize pool i times\n"
597 "\t[-k kill_percentage (default: %llu%%)]\n"
598 "\t[-p pool_name (default: %s)]\n"
599 "\t[-f dir (default: %s)] file directory for vdev files\n"
600 "\t[-V] verbose (use multiple times for ever more blather)\n"
601 "\t[-E] use existing pool instead of creating new one\n"
602 "\t[-T time (default: %llu sec)] total run time\n"
603 "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
604 "\t[-P passtime (default: %llu sec)] time per pass\n"
605 "\t[-B alt_ztest (default: <none>)] alternate ztest path\n"
606 "\t[-o variable=value] ... set global variable to an unsigned\n"
607 "\t 32-bit integer value\n"
608 "\t[-h] (print help)\n"
611 (u_longlong_t)zo->zo_vdevs, /* -v */
612 nice_vdev_size, /* -s */
613 zo->zo_ashift, /* -a */
614 zo->zo_mirrors, /* -m */
615 zo->zo_raidz, /* -r */
616 zo->zo_raidz_parity, /* -R */
617 zo->zo_datasets, /* -d */
618 zo->zo_threads, /* -t */
619 nice_force_ganging, /* -g */
620 zo->zo_init, /* -i */
621 (u_longlong_t)zo->zo_killrate, /* -k */
622 zo->zo_pool, /* -p */
624 (u_longlong_t)zo->zo_time, /* -T */
625 (u_longlong_t)zo->zo_maxloops, /* -F */
626 (u_longlong_t)zo->zo_passtime);
627 exit(requested ? 0 : 1);
631 process_options(int argc, char **argv)
634 ztest_shared_opts_t *zo = &ztest_opts;
638 char altdir[MAXNAMELEN] = { 0 };
640 bcopy(&ztest_opts_defaults, zo, sizeof (*zo));
642 while ((opt = getopt(argc, argv,
643 "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:hF:B:o:")) != EOF) {
660 value = nicenumtoull(optarg);
664 zo->zo_vdevs = value;
667 zo->zo_vdev_size = MAX(SPA_MINDEVSIZE, value);
670 zo->zo_ashift = value;
673 zo->zo_mirrors = value;
676 zo->zo_raidz = MAX(1, value);
679 zo->zo_raidz_parity = MIN(MAX(value, 1), 3);
682 zo->zo_datasets = MAX(1, value);
685 zo->zo_threads = MAX(1, value);
688 zo->zo_metaslab_force_ganging =
689 MAX(SPA_MINBLOCKSIZE << 1, value);
695 zo->zo_killrate = value;
698 (void) strlcpy(zo->zo_pool, optarg,
699 sizeof (zo->zo_pool));
702 path = realpath(optarg, NULL);
704 (void) fprintf(stderr, "error: %s: %s\n",
705 optarg, strerror(errno));
708 (void) strlcpy(zo->zo_dir, path,
709 sizeof (zo->zo_dir));
722 zo->zo_passtime = MAX(1, value);
725 zo->zo_maxloops = MAX(1, value);
728 (void) strlcpy(altdir, optarg, sizeof (altdir));
731 if (set_global_var(optarg) != 0)
744 zo->zo_raidz_parity = MIN(zo->zo_raidz_parity, zo->zo_raidz - 1);
747 (zo->zo_vdevs > 0 ? zo->zo_time * NANOSEC / zo->zo_vdevs :
750 if (strlen(altdir) > 0) {
758 cmd = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
759 realaltdir = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
761 VERIFY(NULL != realpath(getexecname(), cmd));
762 if (0 != access(altdir, F_OK)) {
763 ztest_dump_core = B_FALSE;
764 fatal(B_TRUE, "invalid alternate ztest path: %s",
767 VERIFY(NULL != realpath(altdir, realaltdir));
770 * 'cmd' should be of the form "<anything>/usr/bin/<isa>/ztest".
771 * We want to extract <isa> to determine if we should use
772 * 32 or 64 bit binaries.
774 bin = strstr(cmd, "/usr/bin/");
775 ztest = strstr(bin, "/ztest");
777 isalen = ztest - isa;
778 (void) snprintf(zo->zo_alt_ztest, sizeof (zo->zo_alt_ztest),
779 "%s/usr/bin/%.*s/ztest", realaltdir, isalen, isa);
780 (void) snprintf(zo->zo_alt_libpath, sizeof (zo->zo_alt_libpath),
781 "%s/usr/lib/%.*s", realaltdir, isalen, isa);
783 if (0 != access(zo->zo_alt_ztest, X_OK)) {
784 ztest_dump_core = B_FALSE;
785 fatal(B_TRUE, "invalid alternate ztest: %s",
787 } else if (0 != access(zo->zo_alt_libpath, X_OK)) {
788 ztest_dump_core = B_FALSE;
789 fatal(B_TRUE, "invalid alternate lib directory %s",
793 umem_free(cmd, MAXPATHLEN);
794 umem_free(realaltdir, MAXPATHLEN);
799 ztest_kill(ztest_shared_t *zs)
801 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(ztest_spa));
802 zs->zs_space = metaslab_class_get_space(spa_normal_class(ztest_spa));
805 * Before we kill off ztest, make sure that the config is updated.
806 * See comment above spa_write_cachefile().
808 mutex_enter(&spa_namespace_lock);
809 spa_write_cachefile(ztest_spa, B_FALSE, B_FALSE);
810 mutex_exit(&spa_namespace_lock);
812 zfs_dbgmsg_print(FTAG);
813 (void) kill(getpid(), SIGKILL);
817 ztest_random(uint64_t range)
821 ASSERT3S(ztest_fd_rand, >=, 0);
826 if (read(ztest_fd_rand, &r, sizeof (r)) != sizeof (r))
827 fatal(1, "short read from /dev/urandom");
834 ztest_record_enospc(const char *s)
836 ztest_shared->zs_enospc_count++;
840 ztest_get_ashift(void)
842 if (ztest_opts.zo_ashift == 0)
843 return (SPA_MINBLOCKSHIFT + ztest_random(5));
844 return (ztest_opts.zo_ashift);
848 make_vdev_file(char *path, char *aux, char *pool, size_t size, uint64_t ashift)
850 char pathbuf[MAXPATHLEN];
855 ashift = ztest_get_ashift();
861 vdev = ztest_shared->zs_vdev_aux;
862 (void) snprintf(path, sizeof (pathbuf),
863 ztest_aux_template, ztest_opts.zo_dir,
864 pool == NULL ? ztest_opts.zo_pool : pool,
867 vdev = ztest_shared->zs_vdev_next_leaf++;
868 (void) snprintf(path, sizeof (pathbuf),
869 ztest_dev_template, ztest_opts.zo_dir,
870 pool == NULL ? ztest_opts.zo_pool : pool, vdev);
875 int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
877 fatal(1, "can't open %s", path);
878 if (ftruncate(fd, size) != 0)
879 fatal(1, "can't ftruncate %s", path);
883 VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
884 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
885 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
886 VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
892 make_vdev_raidz(char *path, char *aux, char *pool, size_t size,
893 uint64_t ashift, int r)
895 nvlist_t *raidz, **child;
899 return (make_vdev_file(path, aux, pool, size, ashift));
900 child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
902 for (c = 0; c < r; c++)
903 child[c] = make_vdev_file(path, aux, pool, size, ashift);
905 VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
906 VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
907 VDEV_TYPE_RAIDZ) == 0);
908 VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
909 ztest_opts.zo_raidz_parity) == 0);
910 VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
913 for (c = 0; c < r; c++)
914 nvlist_free(child[c]);
916 umem_free(child, r * sizeof (nvlist_t *));
922 make_vdev_mirror(char *path, char *aux, char *pool, size_t size,
923 uint64_t ashift, int r, int m)
925 nvlist_t *mirror, **child;
929 return (make_vdev_raidz(path, aux, pool, size, ashift, r));
931 child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
933 for (c = 0; c < m; c++)
934 child[c] = make_vdev_raidz(path, aux, pool, size, ashift, r);
936 VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
937 VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
938 VDEV_TYPE_MIRROR) == 0);
939 VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
942 for (c = 0; c < m; c++)
943 nvlist_free(child[c]);
945 umem_free(child, m * sizeof (nvlist_t *));
951 make_vdev_root(char *path, char *aux, char *pool, size_t size, uint64_t ashift,
952 int log, int r, int m, int t)
954 nvlist_t *root, **child;
959 child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
961 for (c = 0; c < t; c++) {
962 child[c] = make_vdev_mirror(path, aux, pool, size, ashift,
964 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
968 VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
969 VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
970 VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
973 for (c = 0; c < t; c++)
974 nvlist_free(child[c]);
976 umem_free(child, t * sizeof (nvlist_t *));
982 * Find a random spa version. Returns back a random spa version in the
983 * range [initial_version, SPA_VERSION_FEATURES].
986 ztest_random_spa_version(uint64_t initial_version)
988 uint64_t version = initial_version;
990 if (version <= SPA_VERSION_BEFORE_FEATURES) {
992 ztest_random(SPA_VERSION_BEFORE_FEATURES - version + 1);
995 if (version > SPA_VERSION_BEFORE_FEATURES)
996 version = SPA_VERSION_FEATURES;
998 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
1003 ztest_random_blocksize(void)
1005 uint64_t block_shift;
1007 * Choose a block size >= the ashift.
1008 * If the SPA supports new MAXBLOCKSIZE, test up to 1MB blocks.
1010 int maxbs = SPA_OLD_MAXBLOCKSHIFT;
1011 if (spa_maxblocksize(ztest_spa) == SPA_MAXBLOCKSIZE)
1013 block_shift = ztest_random(maxbs - ztest_spa->spa_max_ashift + 1);
1014 return (1 << (SPA_MINBLOCKSHIFT + block_shift));
1018 ztest_random_dnodesize(void)
1021 int max_slots = spa_maxdnodesize(ztest_spa) >> DNODE_SHIFT;
1023 if (max_slots == DNODE_MIN_SLOTS)
1024 return (DNODE_MIN_SIZE);
1027 * Weight the random distribution more heavily toward smaller
1028 * dnode sizes since that is more likely to reflect real-world
1031 ASSERT3U(max_slots, >, 4);
1032 switch (ztest_random(10)) {
1034 slots = 5 + ztest_random(max_slots - 4);
1037 slots = 2 + ztest_random(3);
1044 return (slots << DNODE_SHIFT);
1048 ztest_random_ibshift(void)
1050 return (DN_MIN_INDBLKSHIFT +
1051 ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1));
1055 ztest_random_vdev_top(spa_t *spa, boolean_t log_ok)
1058 vdev_t *rvd = spa->spa_root_vdev;
1061 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1064 top = ztest_random(rvd->vdev_children);
1065 tvd = rvd->vdev_child[top];
1066 } while (!vdev_is_concrete(tvd) || (tvd->vdev_islog && !log_ok) ||
1067 tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL);
1073 ztest_random_dsl_prop(zfs_prop_t prop)
1078 value = zfs_prop_random_value(prop, ztest_random(-1ULL));
1079 } while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF);
1085 ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value,
1088 const char *propname = zfs_prop_to_name(prop);
1089 const char *valname;
1090 char setpoint[MAXPATHLEN];
1094 error = dsl_prop_set_int(osname, propname,
1095 (inherit ? ZPROP_SRC_NONE : ZPROP_SRC_LOCAL), value);
1097 if (error == ENOSPC) {
1098 ztest_record_enospc(FTAG);
1103 VERIFY0(dsl_prop_get_integer(osname, propname, &curval, setpoint));
1105 if (ztest_opts.zo_verbose >= 6) {
1106 VERIFY(zfs_prop_index_to_string(prop, curval, &valname) == 0);
1107 (void) printf("%s %s = %s at '%s'\n",
1108 osname, propname, valname, setpoint);
1115 ztest_spa_prop_set_uint64(zpool_prop_t prop, uint64_t value)
1117 spa_t *spa = ztest_spa;
1118 nvlist_t *props = NULL;
1121 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
1122 VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0);
1124 error = spa_prop_set(spa, props);
1128 if (error == ENOSPC) {
1129 ztest_record_enospc(FTAG);
1138 ztest_rll_init(rll_t *rll)
1140 rll->rll_writer = NULL;
1141 rll->rll_readers = 0;
1142 mutex_init(&rll->rll_lock, NULL, USYNC_THREAD, NULL);
1143 cv_init(&rll->rll_cv, NULL, USYNC_THREAD, NULL);
1147 ztest_rll_destroy(rll_t *rll)
1149 ASSERT(rll->rll_writer == NULL);
1150 ASSERT(rll->rll_readers == 0);
1151 mutex_destroy(&rll->rll_lock);
1152 cv_destroy(&rll->rll_cv);
1156 ztest_rll_lock(rll_t *rll, rl_type_t type)
1158 mutex_enter(&rll->rll_lock);
1160 if (type == RL_READER) {
1161 while (rll->rll_writer != NULL)
1162 cv_wait(&rll->rll_cv, &rll->rll_lock);
1165 while (rll->rll_writer != NULL || rll->rll_readers)
1166 cv_wait(&rll->rll_cv, &rll->rll_lock);
1167 rll->rll_writer = curthread;
1170 mutex_exit(&rll->rll_lock);
1174 ztest_rll_unlock(rll_t *rll)
1176 mutex_enter(&rll->rll_lock);
1178 if (rll->rll_writer) {
1179 ASSERT(rll->rll_readers == 0);
1180 rll->rll_writer = NULL;
1182 ASSERT(rll->rll_readers != 0);
1183 ASSERT(rll->rll_writer == NULL);
1187 if (rll->rll_writer == NULL && rll->rll_readers == 0)
1188 cv_broadcast(&rll->rll_cv);
1190 mutex_exit(&rll->rll_lock);
1194 ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type)
1196 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1198 ztest_rll_lock(rll, type);
1202 ztest_object_unlock(ztest_ds_t *zd, uint64_t object)
1204 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1206 ztest_rll_unlock(rll);
1210 ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset,
1211 uint64_t size, rl_type_t type)
1213 uint64_t hash = object ^ (offset % (ZTEST_RANGE_LOCKS + 1));
1214 rll_t *rll = &zd->zd_range_lock[hash & (ZTEST_RANGE_LOCKS - 1)];
1217 rl = umem_alloc(sizeof (*rl), UMEM_NOFAIL);
1218 rl->rl_object = object;
1219 rl->rl_offset = offset;
1223 ztest_rll_lock(rll, type);
1229 ztest_range_unlock(rl_t *rl)
1231 rll_t *rll = rl->rl_lock;
1233 ztest_rll_unlock(rll);
1235 umem_free(rl, sizeof (*rl));
1239 ztest_zd_init(ztest_ds_t *zd, ztest_shared_ds_t *szd, objset_t *os)
1242 zd->zd_zilog = dmu_objset_zil(os);
1243 zd->zd_shared = szd;
1244 dmu_objset_name(os, zd->zd_name);
1246 if (zd->zd_shared != NULL)
1247 zd->zd_shared->zd_seq = 0;
1249 rw_init(&zd->zd_zilog_lock, NULL, USYNC_THREAD, NULL);
1250 mutex_init(&zd->zd_dirobj_lock, NULL, USYNC_THREAD, NULL);
1252 for (int l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1253 ztest_rll_init(&zd->zd_object_lock[l]);
1255 for (int l = 0; l < ZTEST_RANGE_LOCKS; l++)
1256 ztest_rll_init(&zd->zd_range_lock[l]);
1260 ztest_zd_fini(ztest_ds_t *zd)
1262 mutex_destroy(&zd->zd_dirobj_lock);
1264 for (int l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1265 ztest_rll_destroy(&zd->zd_object_lock[l]);
1267 for (int l = 0; l < ZTEST_RANGE_LOCKS; l++)
1268 ztest_rll_destroy(&zd->zd_range_lock[l]);
1271 #define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1274 ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag)
1280 * Attempt to assign tx to some transaction group.
1282 error = dmu_tx_assign(tx, txg_how);
1284 if (error == ERESTART) {
1285 ASSERT(txg_how == TXG_NOWAIT);
1288 ASSERT3U(error, ==, ENOSPC);
1289 ztest_record_enospc(tag);
1294 txg = dmu_tx_get_txg(tx);
1300 ztest_pattern_set(void *buf, uint64_t size, uint64_t value)
1303 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1310 ztest_pattern_match(void *buf, uint64_t size, uint64_t value)
1313 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1317 diff |= (value - *ip++);
1323 ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1324 uint64_t dnodesize, uint64_t offset, uint64_t gen, uint64_t txg,
1327 bt->bt_magic = BT_MAGIC;
1328 bt->bt_objset = dmu_objset_id(os);
1329 bt->bt_object = object;
1330 bt->bt_dnodesize = dnodesize;
1331 bt->bt_offset = offset;
1334 bt->bt_crtxg = crtxg;
1338 ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1339 uint64_t dnodesize, uint64_t offset, uint64_t gen, uint64_t txg,
1342 ASSERT3U(bt->bt_magic, ==, BT_MAGIC);
1343 ASSERT3U(bt->bt_objset, ==, dmu_objset_id(os));
1344 ASSERT3U(bt->bt_object, ==, object);
1345 ASSERT3U(bt->bt_dnodesize, ==, dnodesize);
1346 ASSERT3U(bt->bt_offset, ==, offset);
1347 ASSERT3U(bt->bt_gen, <=, gen);
1348 ASSERT3U(bt->bt_txg, <=, txg);
1349 ASSERT3U(bt->bt_crtxg, ==, crtxg);
1352 static ztest_block_tag_t *
1353 ztest_bt_bonus(dmu_buf_t *db)
1355 dmu_object_info_t doi;
1356 ztest_block_tag_t *bt;
1358 dmu_object_info_from_db(db, &doi);
1359 ASSERT3U(doi.doi_bonus_size, <=, db->db_size);
1360 ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt));
1361 bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt));
1367 * Generate a token to fill up unused bonus buffer space. Try to make
1368 * it unique to the object, generation, and offset to verify that data
1369 * is not getting overwritten by data from other dnodes.
1371 #define ZTEST_BONUS_FILL_TOKEN(obj, ds, gen, offset) \
1372 (((ds) << 48) | ((gen) << 32) | ((obj) << 8) | (offset))
1375 * Fill up the unused bonus buffer region before the block tag with a
1376 * verifiable pattern. Filling the whole bonus area with non-zero data
1377 * helps ensure that all dnode traversal code properly skips the
1378 * interior regions of large dnodes.
1381 ztest_fill_unused_bonus(dmu_buf_t *db, void *end, uint64_t obj,
1382 objset_t *os, uint64_t gen)
1386 ASSERT(IS_P2ALIGNED((char *)end - (char *)db->db_data, 8));
1388 for (bonusp = db->db_data; bonusp < (uint64_t *)end; bonusp++) {
1389 uint64_t token = ZTEST_BONUS_FILL_TOKEN(obj, dmu_objset_id(os),
1390 gen, bonusp - (uint64_t *)db->db_data);
1396 * Verify that the unused area of a bonus buffer is filled with the
1400 ztest_verify_unused_bonus(dmu_buf_t *db, void *end, uint64_t obj,
1401 objset_t *os, uint64_t gen)
1405 for (bonusp = db->db_data; bonusp < (uint64_t *)end; bonusp++) {
1406 uint64_t token = ZTEST_BONUS_FILL_TOKEN(obj, dmu_objset_id(os),
1407 gen, bonusp - (uint64_t *)db->db_data);
1408 VERIFY3U(*bonusp, ==, token);
1416 #define lrz_type lr_mode
1417 #define lrz_blocksize lr_uid
1418 #define lrz_ibshift lr_gid
1419 #define lrz_bonustype lr_rdev
1420 #define lrz_dnodesize lr_crtime[1]
1423 ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr)
1425 char *name = (void *)(lr + 1); /* name follows lr */
1426 size_t namesize = strlen(name) + 1;
1429 if (zil_replaying(zd->zd_zilog, tx))
1432 itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize);
1433 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1434 sizeof (*lr) + namesize - sizeof (lr_t));
1436 zil_itx_assign(zd->zd_zilog, itx, tx);
1440 ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr, uint64_t object)
1442 char *name = (void *)(lr + 1); /* name follows lr */
1443 size_t namesize = strlen(name) + 1;
1446 if (zil_replaying(zd->zd_zilog, tx))
1449 itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize);
1450 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1451 sizeof (*lr) + namesize - sizeof (lr_t));
1453 itx->itx_oid = object;
1454 zil_itx_assign(zd->zd_zilog, itx, tx);
1458 ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr)
1461 itx_wr_state_t write_state = ztest_random(WR_NUM_STATES);
1463 if (zil_replaying(zd->zd_zilog, tx))
1466 if (lr->lr_length > ZIL_MAX_LOG_DATA)
1467 write_state = WR_INDIRECT;
1469 itx = zil_itx_create(TX_WRITE,
1470 sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0));
1472 if (write_state == WR_COPIED &&
1473 dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length,
1474 ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) {
1475 zil_itx_destroy(itx);
1476 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1477 write_state = WR_NEED_COPY;
1479 itx->itx_private = zd;
1480 itx->itx_wr_state = write_state;
1481 itx->itx_sync = (ztest_random(8) == 0);
1483 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1484 sizeof (*lr) - sizeof (lr_t));
1486 zil_itx_assign(zd->zd_zilog, itx, tx);
1490 ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr)
1494 if (zil_replaying(zd->zd_zilog, tx))
1497 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1498 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1499 sizeof (*lr) - sizeof (lr_t));
1501 itx->itx_sync = B_FALSE;
1502 zil_itx_assign(zd->zd_zilog, itx, tx);
1506 ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr)
1510 if (zil_replaying(zd->zd_zilog, tx))
1513 itx = zil_itx_create(TX_SETATTR, sizeof (*lr));
1514 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1515 sizeof (*lr) - sizeof (lr_t));
1517 itx->itx_sync = B_FALSE;
1518 zil_itx_assign(zd->zd_zilog, itx, tx);
1525 ztest_replay_create(void *arg1, void *arg2, boolean_t byteswap)
1527 ztest_ds_t *zd = arg1;
1528 lr_create_t *lr = arg2;
1529 char *name = (void *)(lr + 1); /* name follows lr */
1530 objset_t *os = zd->zd_os;
1531 ztest_block_tag_t *bbt;
1539 byteswap_uint64_array(lr, sizeof (*lr));
1541 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1542 ASSERT(name[0] != '\0');
1544 tx = dmu_tx_create(os);
1546 dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name);
1548 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1549 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1551 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1554 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1558 ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid);
1559 bonuslen = DN_BONUS_SIZE(lr->lrz_dnodesize);
1561 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1562 if (lr->lr_foid == 0) {
1563 lr->lr_foid = zap_create_dnsize(os,
1564 lr->lrz_type, lr->lrz_bonustype,
1565 bonuslen, lr->lrz_dnodesize, tx);
1567 error = zap_create_claim_dnsize(os, lr->lr_foid,
1568 lr->lrz_type, lr->lrz_bonustype,
1569 bonuslen, lr->lrz_dnodesize, tx);
1572 if (lr->lr_foid == 0) {
1573 lr->lr_foid = dmu_object_alloc_dnsize(os,
1574 lr->lrz_type, 0, lr->lrz_bonustype,
1575 bonuslen, lr->lrz_dnodesize, tx);
1577 error = dmu_object_claim_dnsize(os, lr->lr_foid,
1578 lr->lrz_type, 0, lr->lrz_bonustype,
1579 bonuslen, lr->lrz_dnodesize, tx);
1584 ASSERT3U(error, ==, EEXIST);
1585 ASSERT(zd->zd_zilog->zl_replay);
1590 ASSERT(lr->lr_foid != 0);
1592 if (lr->lrz_type != DMU_OT_ZAP_OTHER)
1593 VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid,
1594 lr->lrz_blocksize, lr->lrz_ibshift, tx));
1596 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1597 bbt = ztest_bt_bonus(db);
1598 dmu_buf_will_dirty(db, tx);
1599 ztest_bt_generate(bbt, os, lr->lr_foid, lr->lrz_dnodesize, -1ULL,
1600 lr->lr_gen, txg, txg);
1601 ztest_fill_unused_bonus(db, bbt, lr->lr_foid, os, lr->lr_gen);
1602 dmu_buf_rele(db, FTAG);
1604 VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1,
1607 (void) ztest_log_create(zd, tx, lr);
1615 ztest_replay_remove(void *arg1, void *arg2, boolean_t byteswap)
1617 ztest_ds_t *zd = arg1;
1618 lr_remove_t *lr = arg2;
1619 char *name = (void *)(lr + 1); /* name follows lr */
1620 objset_t *os = zd->zd_os;
1621 dmu_object_info_t doi;
1623 uint64_t object, txg;
1626 byteswap_uint64_array(lr, sizeof (*lr));
1628 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1629 ASSERT(name[0] != '\0');
1632 zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object));
1633 ASSERT(object != 0);
1635 ztest_object_lock(zd, object, RL_WRITER);
1637 VERIFY3U(0, ==, dmu_object_info(os, object, &doi));
1639 tx = dmu_tx_create(os);
1641 dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name);
1642 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1644 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1646 ztest_object_unlock(zd, object);
1650 if (doi.doi_type == DMU_OT_ZAP_OTHER) {
1651 VERIFY3U(0, ==, zap_destroy(os, object, tx));
1653 VERIFY3U(0, ==, dmu_object_free(os, object, tx));
1656 VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx));
1658 (void) ztest_log_remove(zd, tx, lr, object);
1662 ztest_object_unlock(zd, object);
1668 ztest_replay_write(void *arg1, void *arg2, boolean_t byteswap)
1670 ztest_ds_t *zd = arg1;
1671 lr_write_t *lr = arg2;
1672 objset_t *os = zd->zd_os;
1673 void *data = lr + 1; /* data follows lr */
1674 uint64_t offset, length;
1675 ztest_block_tag_t *bt = data;
1676 ztest_block_tag_t *bbt;
1677 uint64_t gen, txg, lrtxg, crtxg;
1678 dmu_object_info_t doi;
1681 arc_buf_t *abuf = NULL;
1685 byteswap_uint64_array(lr, sizeof (*lr));
1687 offset = lr->lr_offset;
1688 length = lr->lr_length;
1690 /* If it's a dmu_sync() block, write the whole block */
1691 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
1692 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
1693 if (length < blocksize) {
1694 offset -= offset % blocksize;
1699 if (bt->bt_magic == BSWAP_64(BT_MAGIC))
1700 byteswap_uint64_array(bt, sizeof (*bt));
1702 if (bt->bt_magic != BT_MAGIC)
1705 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1706 rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER);
1708 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1710 dmu_object_info_from_db(db, &doi);
1712 bbt = ztest_bt_bonus(db);
1713 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1715 crtxg = bbt->bt_crtxg;
1716 lrtxg = lr->lr_common.lrc_txg;
1718 tx = dmu_tx_create(os);
1720 dmu_tx_hold_write(tx, lr->lr_foid, offset, length);
1722 if (ztest_random(8) == 0 && length == doi.doi_data_block_size &&
1723 P2PHASE(offset, length) == 0)
1724 abuf = dmu_request_arcbuf(db, length);
1726 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1729 dmu_return_arcbuf(abuf);
1730 dmu_buf_rele(db, FTAG);
1731 ztest_range_unlock(rl);
1732 ztest_object_unlock(zd, lr->lr_foid);
1738 * Usually, verify the old data before writing new data --
1739 * but not always, because we also want to verify correct
1740 * behavior when the data was not recently read into cache.
1742 ASSERT(offset % doi.doi_data_block_size == 0);
1743 if (ztest_random(4) != 0) {
1744 int prefetch = ztest_random(2) ?
1745 DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
1746 ztest_block_tag_t rbt;
1748 VERIFY(dmu_read(os, lr->lr_foid, offset,
1749 sizeof (rbt), &rbt, prefetch) == 0);
1750 if (rbt.bt_magic == BT_MAGIC) {
1751 ztest_bt_verify(&rbt, os, lr->lr_foid, 0,
1752 offset, gen, txg, crtxg);
1757 * Writes can appear to be newer than the bonus buffer because
1758 * the ztest_get_data() callback does a dmu_read() of the
1759 * open-context data, which may be different than the data
1760 * as it was when the write was generated.
1762 if (zd->zd_zilog->zl_replay) {
1763 ztest_bt_verify(bt, os, lr->lr_foid, 0, offset,
1764 MAX(gen, bt->bt_gen), MAX(txg, lrtxg),
1769 * Set the bt's gen/txg to the bonus buffer's gen/txg
1770 * so that all of the usual ASSERTs will work.
1772 ztest_bt_generate(bt, os, lr->lr_foid, 0, offset, gen, txg,
1777 dmu_write(os, lr->lr_foid, offset, length, data, tx);
1779 bcopy(data, abuf->b_data, length);
1780 dmu_assign_arcbuf(db, offset, abuf, tx);
1783 (void) ztest_log_write(zd, tx, lr);
1785 dmu_buf_rele(db, FTAG);
1789 ztest_range_unlock(rl);
1790 ztest_object_unlock(zd, lr->lr_foid);
1796 ztest_replay_truncate(void *arg1, void *arg2, boolean_t byteswap)
1798 ztest_ds_t *zd = arg1;
1799 lr_truncate_t *lr = arg2;
1800 objset_t *os = zd->zd_os;
1806 byteswap_uint64_array(lr, sizeof (*lr));
1808 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1809 rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length,
1812 tx = dmu_tx_create(os);
1814 dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length);
1816 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1818 ztest_range_unlock(rl);
1819 ztest_object_unlock(zd, lr->lr_foid);
1823 VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset,
1824 lr->lr_length, tx) == 0);
1826 (void) ztest_log_truncate(zd, tx, lr);
1830 ztest_range_unlock(rl);
1831 ztest_object_unlock(zd, lr->lr_foid);
1837 ztest_replay_setattr(void *arg1, void *arg2, boolean_t byteswap)
1839 ztest_ds_t *zd = arg1;
1840 lr_setattr_t *lr = arg2;
1841 objset_t *os = zd->zd_os;
1844 ztest_block_tag_t *bbt;
1845 uint64_t txg, lrtxg, crtxg, dnodesize;
1848 byteswap_uint64_array(lr, sizeof (*lr));
1850 ztest_object_lock(zd, lr->lr_foid, RL_WRITER);
1852 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1854 tx = dmu_tx_create(os);
1855 dmu_tx_hold_bonus(tx, lr->lr_foid);
1857 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1859 dmu_buf_rele(db, FTAG);
1860 ztest_object_unlock(zd, lr->lr_foid);
1864 bbt = ztest_bt_bonus(db);
1865 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1866 crtxg = bbt->bt_crtxg;
1867 lrtxg = lr->lr_common.lrc_txg;
1868 dnodesize = bbt->bt_dnodesize;
1870 if (zd->zd_zilog->zl_replay) {
1871 ASSERT(lr->lr_size != 0);
1872 ASSERT(lr->lr_mode != 0);
1876 * Randomly change the size and increment the generation.
1878 lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) *
1880 lr->lr_mode = bbt->bt_gen + 1;
1885 * Verify that the current bonus buffer is not newer than our txg.
1887 ztest_bt_verify(bbt, os, lr->lr_foid, dnodesize, -1ULL, lr->lr_mode,
1888 MAX(txg, lrtxg), crtxg);
1890 dmu_buf_will_dirty(db, tx);
1892 ASSERT3U(lr->lr_size, >=, sizeof (*bbt));
1893 ASSERT3U(lr->lr_size, <=, db->db_size);
1894 VERIFY0(dmu_set_bonus(db, lr->lr_size, tx));
1895 bbt = ztest_bt_bonus(db);
1897 ztest_bt_generate(bbt, os, lr->lr_foid, dnodesize, -1ULL, lr->lr_mode,
1899 ztest_fill_unused_bonus(db, bbt, lr->lr_foid, os, bbt->bt_gen);
1900 dmu_buf_rele(db, FTAG);
1902 (void) ztest_log_setattr(zd, tx, lr);
1906 ztest_object_unlock(zd, lr->lr_foid);
1911 zil_replay_func_t *ztest_replay_vector[TX_MAX_TYPE] = {
1912 NULL, /* 0 no such transaction type */
1913 ztest_replay_create, /* TX_CREATE */
1914 NULL, /* TX_MKDIR */
1915 NULL, /* TX_MKXATTR */
1916 NULL, /* TX_SYMLINK */
1917 ztest_replay_remove, /* TX_REMOVE */
1918 NULL, /* TX_RMDIR */
1920 NULL, /* TX_RENAME */
1921 ztest_replay_write, /* TX_WRITE */
1922 ztest_replay_truncate, /* TX_TRUNCATE */
1923 ztest_replay_setattr, /* TX_SETATTR */
1925 NULL, /* TX_CREATE_ACL */
1926 NULL, /* TX_CREATE_ATTR */
1927 NULL, /* TX_CREATE_ACL_ATTR */
1928 NULL, /* TX_MKDIR_ACL */
1929 NULL, /* TX_MKDIR_ATTR */
1930 NULL, /* TX_MKDIR_ACL_ATTR */
1931 NULL, /* TX_WRITE2 */
1935 * ZIL get_data callbacks
1939 ztest_get_done(zgd_t *zgd, int error)
1941 ztest_ds_t *zd = zgd->zgd_private;
1942 uint64_t object = zgd->zgd_rl->rl_object;
1945 dmu_buf_rele(zgd->zgd_db, zgd);
1947 ztest_range_unlock(zgd->zgd_rl);
1948 ztest_object_unlock(zd, object);
1950 if (error == 0 && zgd->zgd_bp)
1951 zil_lwb_add_block(zgd->zgd_lwb, zgd->zgd_bp);
1953 umem_free(zgd, sizeof (*zgd));
1957 ztest_get_data(void *arg, lr_write_t *lr, char *buf, struct lwb *lwb,
1960 ztest_ds_t *zd = arg;
1961 objset_t *os = zd->zd_os;
1962 uint64_t object = lr->lr_foid;
1963 uint64_t offset = lr->lr_offset;
1964 uint64_t size = lr->lr_length;
1965 uint64_t txg = lr->lr_common.lrc_txg;
1967 dmu_object_info_t doi;
1972 ASSERT3P(lwb, !=, NULL);
1973 ASSERT3P(zio, !=, NULL);
1974 ASSERT3U(size, !=, 0);
1976 ztest_object_lock(zd, object, RL_READER);
1977 error = dmu_bonus_hold(os, object, FTAG, &db);
1979 ztest_object_unlock(zd, object);
1983 crtxg = ztest_bt_bonus(db)->bt_crtxg;
1985 if (crtxg == 0 || crtxg > txg) {
1986 dmu_buf_rele(db, FTAG);
1987 ztest_object_unlock(zd, object);
1991 dmu_object_info_from_db(db, &doi);
1992 dmu_buf_rele(db, FTAG);
1995 zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL);
1997 zgd->zgd_private = zd;
1999 if (buf != NULL) { /* immediate write */
2000 zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
2003 error = dmu_read(os, object, offset, size, buf,
2004 DMU_READ_NO_PREFETCH);
2007 size = doi.doi_data_block_size;
2009 offset = P2ALIGN(offset, size);
2011 ASSERT(offset < size);
2015 zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
2018 error = dmu_buf_hold(os, object, offset, zgd, &db,
2019 DMU_READ_NO_PREFETCH);
2022 blkptr_t *bp = &lr->lr_blkptr;
2027 ASSERT(db->db_offset == offset);
2028 ASSERT(db->db_size == size);
2030 error = dmu_sync(zio, lr->lr_common.lrc_txg,
2031 ztest_get_done, zgd);
2038 ztest_get_done(zgd, error);
2044 ztest_lr_alloc(size_t lrsize, char *name)
2047 size_t namesize = name ? strlen(name) + 1 : 0;
2049 lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
2052 bcopy(name, lr + lrsize, namesize);
2058 ztest_lr_free(void *lr, size_t lrsize, char *name)
2060 size_t namesize = name ? strlen(name) + 1 : 0;
2062 umem_free(lr, lrsize + namesize);
2066 * Lookup a bunch of objects. Returns the number of objects not found.
2069 ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
2074 ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
2076 for (int i = 0; i < count; i++, od++) {
2078 error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
2079 sizeof (uint64_t), 1, &od->od_object);
2081 ASSERT(error == ENOENT);
2082 ASSERT(od->od_object == 0);
2086 ztest_block_tag_t *bbt;
2087 dmu_object_info_t doi;
2089 ASSERT(od->od_object != 0);
2090 ASSERT(missing == 0); /* there should be no gaps */
2092 ztest_object_lock(zd, od->od_object, RL_READER);
2093 VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
2094 od->od_object, FTAG, &db));
2095 dmu_object_info_from_db(db, &doi);
2096 bbt = ztest_bt_bonus(db);
2097 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
2098 od->od_type = doi.doi_type;
2099 od->od_blocksize = doi.doi_data_block_size;
2100 od->od_gen = bbt->bt_gen;
2101 dmu_buf_rele(db, FTAG);
2102 ztest_object_unlock(zd, od->od_object);
2110 ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
2114 ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
2116 for (int i = 0; i < count; i++, od++) {
2123 lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2125 lr->lr_doid = od->od_dir;
2126 lr->lr_foid = 0; /* 0 to allocate, > 0 to claim */
2127 lr->lrz_type = od->od_crtype;
2128 lr->lrz_blocksize = od->od_crblocksize;
2129 lr->lrz_ibshift = ztest_random_ibshift();
2130 lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
2131 lr->lrz_dnodesize = od->od_crdnodesize;
2132 lr->lr_gen = od->od_crgen;
2133 lr->lr_crtime[0] = time(NULL);
2135 if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
2136 ASSERT(missing == 0);
2140 od->od_object = lr->lr_foid;
2141 od->od_type = od->od_crtype;
2142 od->od_blocksize = od->od_crblocksize;
2143 od->od_gen = od->od_crgen;
2144 ASSERT(od->od_object != 0);
2147 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2154 ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
2159 ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
2163 for (int i = count - 1; i >= 0; i--, od--) {
2170 * No object was found.
2172 if (od->od_object == 0)
2175 lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2177 lr->lr_doid = od->od_dir;
2179 if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
2180 ASSERT3U(error, ==, ENOSPC);
2185 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2192 ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
2198 lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
2200 lr->lr_foid = object;
2201 lr->lr_offset = offset;
2202 lr->lr_length = size;
2204 BP_ZERO(&lr->lr_blkptr);
2206 bcopy(data, lr + 1, size);
2208 error = ztest_replay_write(zd, lr, B_FALSE);
2210 ztest_lr_free(lr, sizeof (*lr) + size, NULL);
2216 ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2221 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2223 lr->lr_foid = object;
2224 lr->lr_offset = offset;
2225 lr->lr_length = size;
2227 error = ztest_replay_truncate(zd, lr, B_FALSE);
2229 ztest_lr_free(lr, sizeof (*lr), NULL);
2235 ztest_setattr(ztest_ds_t *zd, uint64_t object)
2240 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2242 lr->lr_foid = object;
2246 error = ztest_replay_setattr(zd, lr, B_FALSE);
2248 ztest_lr_free(lr, sizeof (*lr), NULL);
2254 ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2256 objset_t *os = zd->zd_os;
2261 txg_wait_synced(dmu_objset_pool(os), 0);
2263 ztest_object_lock(zd, object, RL_READER);
2264 rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
2266 tx = dmu_tx_create(os);
2268 dmu_tx_hold_write(tx, object, offset, size);
2270 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
2273 dmu_prealloc(os, object, offset, size, tx);
2275 txg_wait_synced(dmu_objset_pool(os), txg);
2277 (void) dmu_free_long_range(os, object, offset, size);
2280 ztest_range_unlock(rl);
2281 ztest_object_unlock(zd, object);
2285 ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
2288 ztest_block_tag_t wbt;
2289 dmu_object_info_t doi;
2290 enum ztest_io_type io_type;
2294 VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
2295 blocksize = doi.doi_data_block_size;
2296 data = umem_alloc(blocksize, UMEM_NOFAIL);
2299 * Pick an i/o type at random, biased toward writing block tags.
2301 io_type = ztest_random(ZTEST_IO_TYPES);
2302 if (ztest_random(2) == 0)
2303 io_type = ZTEST_IO_WRITE_TAG;
2305 rw_enter(&zd->zd_zilog_lock, RW_READER);
2309 case ZTEST_IO_WRITE_TAG:
2310 ztest_bt_generate(&wbt, zd->zd_os, object, doi.doi_dnodesize,
2312 (void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
2315 case ZTEST_IO_WRITE_PATTERN:
2316 (void) memset(data, 'a' + (object + offset) % 5, blocksize);
2317 if (ztest_random(2) == 0) {
2319 * Induce fletcher2 collisions to ensure that
2320 * zio_ddt_collision() detects and resolves them
2321 * when using fletcher2-verify for deduplication.
2323 ((uint64_t *)data)[0] ^= 1ULL << 63;
2324 ((uint64_t *)data)[4] ^= 1ULL << 63;
2326 (void) ztest_write(zd, object, offset, blocksize, data);
2329 case ZTEST_IO_WRITE_ZEROES:
2330 bzero(data, blocksize);
2331 (void) ztest_write(zd, object, offset, blocksize, data);
2334 case ZTEST_IO_TRUNCATE:
2335 (void) ztest_truncate(zd, object, offset, blocksize);
2338 case ZTEST_IO_SETATTR:
2339 (void) ztest_setattr(zd, object);
2342 case ZTEST_IO_REWRITE:
2343 rw_enter(&ztest_name_lock, RW_READER);
2344 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2345 ZFS_PROP_CHECKSUM, spa_dedup_checksum(ztest_spa),
2347 VERIFY(err == 0 || err == ENOSPC);
2348 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2349 ZFS_PROP_COMPRESSION,
2350 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION),
2352 VERIFY(err == 0 || err == ENOSPC);
2353 rw_exit(&ztest_name_lock);
2355 VERIFY0(dmu_read(zd->zd_os, object, offset, blocksize, data,
2356 DMU_READ_NO_PREFETCH));
2358 (void) ztest_write(zd, object, offset, blocksize, data);
2362 rw_exit(&zd->zd_zilog_lock);
2364 umem_free(data, blocksize);
2368 * Initialize an object description template.
2371 ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
2372 dmu_object_type_t type, uint64_t blocksize, uint64_t dnodesize,
2375 od->od_dir = ZTEST_DIROBJ;
2378 od->od_crtype = type;
2379 od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
2380 od->od_crdnodesize = dnodesize ? dnodesize : ztest_random_dnodesize();
2383 od->od_type = DMU_OT_NONE;
2384 od->od_blocksize = 0;
2387 (void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
2388 tag, (int64_t)id, index);
2392 * Lookup or create the objects for a test using the od template.
2393 * If the objects do not all exist, or if 'remove' is specified,
2394 * remove any existing objects and create new ones. Otherwise,
2395 * use the existing objects.
2398 ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
2400 int count = size / sizeof (*od);
2403 mutex_enter(&zd->zd_dirobj_lock);
2404 if ((ztest_lookup(zd, od, count) != 0 || remove) &&
2405 (ztest_remove(zd, od, count) != 0 ||
2406 ztest_create(zd, od, count) != 0))
2409 mutex_exit(&zd->zd_dirobj_lock);
2416 ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
2418 zilog_t *zilog = zd->zd_zilog;
2420 rw_enter(&zd->zd_zilog_lock, RW_READER);
2422 zil_commit(zilog, ztest_random(ZTEST_OBJECTS));
2425 * Remember the committed values in zd, which is in parent/child
2426 * shared memory. If we die, the next iteration of ztest_run()
2427 * will verify that the log really does contain this record.
2429 mutex_enter(&zilog->zl_lock);
2430 ASSERT(zd->zd_shared != NULL);
2431 ASSERT3U(zd->zd_shared->zd_seq, <=, zilog->zl_commit_lr_seq);
2432 zd->zd_shared->zd_seq = zilog->zl_commit_lr_seq;
2433 mutex_exit(&zilog->zl_lock);
2435 rw_exit(&zd->zd_zilog_lock);
2439 * This function is designed to simulate the operations that occur during a
2440 * mount/unmount operation. We hold the dataset across these operations in an
2441 * attempt to expose any implicit assumptions about ZIL management.
2445 ztest_zil_remount(ztest_ds_t *zd, uint64_t id)
2447 objset_t *os = zd->zd_os;
2450 * We grab the zd_dirobj_lock to ensure that no other thread is
2451 * updating the zil (i.e. adding in-memory log records) and the
2452 * zd_zilog_lock to block any I/O.
2454 mutex_enter(&zd->zd_dirobj_lock);
2455 rw_enter(&zd->zd_zilog_lock, RW_WRITER);
2457 /* zfsvfs_teardown() */
2458 zil_close(zd->zd_zilog);
2460 /* zfsvfs_setup() */
2461 VERIFY(zil_open(os, ztest_get_data) == zd->zd_zilog);
2462 zil_replay(os, zd, ztest_replay_vector);
2464 rw_exit(&zd->zd_zilog_lock);
2465 mutex_exit(&zd->zd_dirobj_lock);
2469 * Verify that we can't destroy an active pool, create an existing pool,
2470 * or create a pool with a bad vdev spec.
2474 ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2476 ztest_shared_opts_t *zo = &ztest_opts;
2481 * Attempt to create using a bad file.
2483 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2484 VERIFY3U(ENOENT, ==,
2485 spa_create("ztest_bad_file", nvroot, NULL, NULL));
2486 nvlist_free(nvroot);
2489 * Attempt to create using a bad mirror.
2491 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 2, 1);
2492 VERIFY3U(ENOENT, ==,
2493 spa_create("ztest_bad_mirror", nvroot, NULL, NULL));
2494 nvlist_free(nvroot);
2497 * Attempt to create an existing pool. It shouldn't matter
2498 * what's in the nvroot; we should fail with EEXIST.
2500 rw_enter(&ztest_name_lock, RW_READER);
2501 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2502 VERIFY3U(EEXIST, ==, spa_create(zo->zo_pool, nvroot, NULL, NULL));
2503 nvlist_free(nvroot);
2504 VERIFY3U(0, ==, spa_open(zo->zo_pool, &spa, FTAG));
2505 VERIFY3U(EBUSY, ==, spa_destroy(zo->zo_pool));
2506 spa_close(spa, FTAG);
2508 rw_exit(&ztest_name_lock);
2513 ztest_spa_upgrade(ztest_ds_t *zd, uint64_t id)
2516 uint64_t initial_version = SPA_VERSION_INITIAL;
2517 uint64_t version, newversion;
2518 nvlist_t *nvroot, *props;
2521 mutex_enter(&ztest_vdev_lock);
2522 name = kmem_asprintf("%s_upgrade", ztest_opts.zo_pool);
2525 * Clean up from previous runs.
2527 (void) spa_destroy(name);
2529 nvroot = make_vdev_root(NULL, NULL, name, ztest_opts.zo_vdev_size, 0,
2530 0, ztest_opts.zo_raidz, ztest_opts.zo_mirrors, 1);
2533 * If we're configuring a RAIDZ device then make sure that the
2534 * the initial version is capable of supporting that feature.
2536 switch (ztest_opts.zo_raidz_parity) {
2539 initial_version = SPA_VERSION_INITIAL;
2542 initial_version = SPA_VERSION_RAIDZ2;
2545 initial_version = SPA_VERSION_RAIDZ3;
2550 * Create a pool with a spa version that can be upgraded. Pick
2551 * a value between initial_version and SPA_VERSION_BEFORE_FEATURES.
2554 version = ztest_random_spa_version(initial_version);
2555 } while (version > SPA_VERSION_BEFORE_FEATURES);
2557 props = fnvlist_alloc();
2558 fnvlist_add_uint64(props,
2559 zpool_prop_to_name(ZPOOL_PROP_VERSION), version);
2560 VERIFY0(spa_create(name, nvroot, props, NULL));
2561 fnvlist_free(nvroot);
2562 fnvlist_free(props);
2564 VERIFY0(spa_open(name, &spa, FTAG));
2565 VERIFY3U(spa_version(spa), ==, version);
2566 newversion = ztest_random_spa_version(version + 1);
2568 if (ztest_opts.zo_verbose >= 4) {
2569 (void) printf("upgrading spa version from %llu to %llu\n",
2570 (u_longlong_t)version, (u_longlong_t)newversion);
2573 spa_upgrade(spa, newversion);
2574 VERIFY3U(spa_version(spa), >, version);
2575 VERIFY3U(spa_version(spa), ==, fnvlist_lookup_uint64(spa->spa_config,
2576 zpool_prop_to_name(ZPOOL_PROP_VERSION)));
2577 spa_close(spa, FTAG);
2580 mutex_exit(&ztest_vdev_lock);
2584 ztest_spa_checkpoint(spa_t *spa)
2586 ASSERT(MUTEX_HELD(&ztest_checkpoint_lock));
2588 int error = spa_checkpoint(spa->spa_name);
2592 case ZFS_ERR_DEVRM_IN_PROGRESS:
2593 case ZFS_ERR_DISCARDING_CHECKPOINT:
2594 case ZFS_ERR_CHECKPOINT_EXISTS:
2597 ztest_record_enospc(FTAG);
2600 fatal(0, "spa_checkpoint(%s) = %d", spa->spa_name, error);
2605 ztest_spa_discard_checkpoint(spa_t *spa)
2607 ASSERT(MUTEX_HELD(&ztest_checkpoint_lock));
2609 int error = spa_checkpoint_discard(spa->spa_name);
2613 case ZFS_ERR_DISCARDING_CHECKPOINT:
2614 case ZFS_ERR_NO_CHECKPOINT:
2617 fatal(0, "spa_discard_checkpoint(%s) = %d",
2618 spa->spa_name, error);
2625 ztest_spa_checkpoint_create_discard(ztest_ds_t *zd, uint64_t id)
2627 spa_t *spa = ztest_spa;
2629 mutex_enter(&ztest_checkpoint_lock);
2630 if (ztest_random(2) == 0) {
2631 ztest_spa_checkpoint(spa);
2633 ztest_spa_discard_checkpoint(spa);
2635 mutex_exit(&ztest_checkpoint_lock);
2640 vdev_lookup_by_path(vdev_t *vd, const char *path)
2644 if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
2647 for (int c = 0; c < vd->vdev_children; c++)
2648 if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
2656 * Find the first available hole which can be used as a top-level.
2659 find_vdev_hole(spa_t *spa)
2661 vdev_t *rvd = spa->spa_root_vdev;
2664 ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
2666 for (c = 0; c < rvd->vdev_children; c++) {
2667 vdev_t *cvd = rvd->vdev_child[c];
2669 if (cvd->vdev_ishole)
2676 * Verify that vdev_add() works as expected.
2680 ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2682 ztest_shared_t *zs = ztest_shared;
2683 spa_t *spa = ztest_spa;
2689 mutex_enter(&ztest_vdev_lock);
2690 leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * ztest_opts.zo_raidz;
2692 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2694 ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2697 * If we have slogs then remove them 1/4 of the time.
2699 if (spa_has_slogs(spa) && ztest_random(4) == 0) {
2701 * Grab the guid from the head of the log class rotor.
2703 guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid;
2705 spa_config_exit(spa, SCL_VDEV, FTAG);
2708 * We have to grab the zs_name_lock as writer to
2709 * prevent a race between removing a slog (dmu_objset_find)
2710 * and destroying a dataset. Removing the slog will
2711 * grab a reference on the dataset which may cause
2712 * dmu_objset_destroy() to fail with EBUSY thus
2713 * leaving the dataset in an inconsistent state.
2715 rw_enter(&ztest_name_lock, RW_WRITER);
2716 error = spa_vdev_remove(spa, guid, B_FALSE);
2717 rw_exit(&ztest_name_lock);
2722 case ZFS_ERR_CHECKPOINT_EXISTS:
2723 case ZFS_ERR_DISCARDING_CHECKPOINT:
2726 fatal(0, "spa_vdev_remove() = %d", error);
2729 spa_config_exit(spa, SCL_VDEV, FTAG);
2732 * Make 1/4 of the devices be log devices.
2734 nvroot = make_vdev_root(NULL, NULL, NULL,
2735 ztest_opts.zo_vdev_size, 0,
2736 ztest_random(4) == 0, ztest_opts.zo_raidz,
2739 error = spa_vdev_add(spa, nvroot);
2740 nvlist_free(nvroot);
2746 ztest_record_enospc("spa_vdev_add");
2749 fatal(0, "spa_vdev_add() = %d", error);
2753 mutex_exit(&ztest_vdev_lock);
2757 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
2761 ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
2763 ztest_shared_t *zs = ztest_shared;
2764 spa_t *spa = ztest_spa;
2765 vdev_t *rvd = spa->spa_root_vdev;
2766 spa_aux_vdev_t *sav;
2771 if (ztest_random(2) == 0) {
2772 sav = &spa->spa_spares;
2773 aux = ZPOOL_CONFIG_SPARES;
2775 sav = &spa->spa_l2cache;
2776 aux = ZPOOL_CONFIG_L2CACHE;
2779 mutex_enter(&ztest_vdev_lock);
2781 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2783 if (sav->sav_count != 0 && ztest_random(4) == 0) {
2785 * Pick a random device to remove.
2787 guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
2790 * Find an unused device we can add.
2792 zs->zs_vdev_aux = 0;
2794 char path[MAXPATHLEN];
2796 (void) snprintf(path, sizeof (path), ztest_aux_template,
2797 ztest_opts.zo_dir, ztest_opts.zo_pool, aux,
2799 for (c = 0; c < sav->sav_count; c++)
2800 if (strcmp(sav->sav_vdevs[c]->vdev_path,
2803 if (c == sav->sav_count &&
2804 vdev_lookup_by_path(rvd, path) == NULL)
2810 spa_config_exit(spa, SCL_VDEV, FTAG);
2816 nvlist_t *nvroot = make_vdev_root(NULL, aux, NULL,
2817 (ztest_opts.zo_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
2818 error = spa_vdev_add(spa, nvroot);
2824 fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
2826 nvlist_free(nvroot);
2829 * Remove an existing device. Sometimes, dirty its
2830 * vdev state first to make sure we handle removal
2831 * of devices that have pending state changes.
2833 if (ztest_random(2) == 0)
2834 (void) vdev_online(spa, guid, 0, NULL);
2836 error = spa_vdev_remove(spa, guid, B_FALSE);
2841 case ZFS_ERR_CHECKPOINT_EXISTS:
2842 case ZFS_ERR_DISCARDING_CHECKPOINT:
2845 fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
2849 mutex_exit(&ztest_vdev_lock);
2853 * split a pool if it has mirror tlvdevs
2857 ztest_split_pool(ztest_ds_t *zd, uint64_t id)
2859 ztest_shared_t *zs = ztest_shared;
2860 spa_t *spa = ztest_spa;
2861 vdev_t *rvd = spa->spa_root_vdev;
2862 nvlist_t *tree, **child, *config, *split, **schild;
2863 uint_t c, children, schildren = 0, lastlogid = 0;
2866 mutex_enter(&ztest_vdev_lock);
2868 /* ensure we have a useable config; mirrors of raidz aren't supported */
2869 if (zs->zs_mirrors < 3 || ztest_opts.zo_raidz > 1) {
2870 mutex_exit(&ztest_vdev_lock);
2874 /* clean up the old pool, if any */
2875 (void) spa_destroy("splitp");
2877 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2879 /* generate a config from the existing config */
2880 mutex_enter(&spa->spa_props_lock);
2881 VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
2883 mutex_exit(&spa->spa_props_lock);
2885 VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2888 schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
2889 for (c = 0; c < children; c++) {
2890 vdev_t *tvd = rvd->vdev_child[c];
2894 if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
2895 VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
2897 VERIFY(nvlist_add_string(schild[schildren],
2898 ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
2899 VERIFY(nvlist_add_uint64(schild[schildren],
2900 ZPOOL_CONFIG_IS_HOLE, 1) == 0);
2902 lastlogid = schildren;
2907 VERIFY(nvlist_lookup_nvlist_array(child[c],
2908 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2909 VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
2912 /* OK, create a config that can be used to split */
2913 VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
2914 VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
2915 VDEV_TYPE_ROOT) == 0);
2916 VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
2917 lastlogid != 0 ? lastlogid : schildren) == 0);
2919 VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
2920 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
2922 for (c = 0; c < schildren; c++)
2923 nvlist_free(schild[c]);
2927 spa_config_exit(spa, SCL_VDEV, FTAG);
2929 rw_enter(&ztest_name_lock, RW_WRITER);
2930 error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
2931 rw_exit(&ztest_name_lock);
2933 nvlist_free(config);
2936 (void) printf("successful split - results:\n");
2937 mutex_enter(&spa_namespace_lock);
2938 show_pool_stats(spa);
2939 show_pool_stats(spa_lookup("splitp"));
2940 mutex_exit(&spa_namespace_lock);
2944 mutex_exit(&ztest_vdev_lock);
2948 * Verify that we can attach and detach devices.
2952 ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
2954 ztest_shared_t *zs = ztest_shared;
2955 spa_t *spa = ztest_spa;
2956 spa_aux_vdev_t *sav = &spa->spa_spares;
2957 vdev_t *rvd = spa->spa_root_vdev;
2958 vdev_t *oldvd, *newvd, *pvd;
2962 uint64_t ashift = ztest_get_ashift();
2963 uint64_t oldguid, pguid;
2964 uint64_t oldsize, newsize;
2965 char oldpath[MAXPATHLEN], newpath[MAXPATHLEN];
2967 int oldvd_has_siblings = B_FALSE;
2968 int newvd_is_spare = B_FALSE;
2970 int error, expected_error;
2972 mutex_enter(&ztest_vdev_lock);
2973 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
2975 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2978 * If a vdev is in the process of being removed, its removal may
2979 * finish while we are in progress, leading to an unexpected error
2980 * value. Don't bother trying to attach while we are in the middle
2983 if (ztest_device_removal_active) {
2984 spa_config_exit(spa, SCL_ALL, FTAG);
2985 mutex_exit(&ztest_vdev_lock);
2990 * Decide whether to do an attach or a replace.
2992 replacing = ztest_random(2);
2995 * Pick a random top-level vdev.
2997 top = ztest_random_vdev_top(spa, B_TRUE);
3000 * Pick a random leaf within it.
3002 leaf = ztest_random(leaves);
3007 oldvd = rvd->vdev_child[top];
3008 if (zs->zs_mirrors >= 1) {
3009 ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
3010 ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
3011 oldvd = oldvd->vdev_child[leaf / ztest_opts.zo_raidz];
3013 if (ztest_opts.zo_raidz > 1) {
3014 ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
3015 ASSERT(oldvd->vdev_children == ztest_opts.zo_raidz);
3016 oldvd = oldvd->vdev_child[leaf % ztest_opts.zo_raidz];
3020 * If we're already doing an attach or replace, oldvd may be a
3021 * mirror vdev -- in which case, pick a random child.
3023 while (oldvd->vdev_children != 0) {
3024 oldvd_has_siblings = B_TRUE;
3025 ASSERT(oldvd->vdev_children >= 2);
3026 oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
3029 oldguid = oldvd->vdev_guid;
3030 oldsize = vdev_get_min_asize(oldvd);
3031 oldvd_is_log = oldvd->vdev_top->vdev_islog;
3032 (void) strcpy(oldpath, oldvd->vdev_path);
3033 pvd = oldvd->vdev_parent;
3034 pguid = pvd->vdev_guid;
3037 * If oldvd has siblings, then half of the time, detach it.
3039 if (oldvd_has_siblings && ztest_random(2) == 0) {
3040 spa_config_exit(spa, SCL_ALL, FTAG);
3041 error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
3042 if (error != 0 && error != ENODEV && error != EBUSY &&
3043 error != ENOTSUP && error != ZFS_ERR_CHECKPOINT_EXISTS &&
3044 error != ZFS_ERR_DISCARDING_CHECKPOINT)
3045 fatal(0, "detach (%s) returned %d", oldpath, error);
3046 mutex_exit(&ztest_vdev_lock);
3051 * For the new vdev, choose with equal probability between the two
3052 * standard paths (ending in either 'a' or 'b') or a random hot spare.
3054 if (sav->sav_count != 0 && ztest_random(3) == 0) {
3055 newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
3056 newvd_is_spare = B_TRUE;
3057 (void) strcpy(newpath, newvd->vdev_path);
3059 (void) snprintf(newpath, sizeof (newpath), ztest_dev_template,
3060 ztest_opts.zo_dir, ztest_opts.zo_pool,
3061 top * leaves + leaf);
3062 if (ztest_random(2) == 0)
3063 newpath[strlen(newpath) - 1] = 'b';
3064 newvd = vdev_lookup_by_path(rvd, newpath);
3069 * Reopen to ensure the vdev's asize field isn't stale.
3072 newsize = vdev_get_min_asize(newvd);
3075 * Make newsize a little bigger or smaller than oldsize.
3076 * If it's smaller, the attach should fail.
3077 * If it's larger, and we're doing a replace,
3078 * we should get dynamic LUN growth when we're done.
3080 newsize = 10 * oldsize / (9 + ztest_random(3));
3084 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
3085 * unless it's a replace; in that case any non-replacing parent is OK.
3087 * If newvd is already part of the pool, it should fail with EBUSY.
3089 * If newvd is too small, it should fail with EOVERFLOW.
3091 if (pvd->vdev_ops != &vdev_mirror_ops &&
3092 pvd->vdev_ops != &vdev_root_ops && (!replacing ||
3093 pvd->vdev_ops == &vdev_replacing_ops ||
3094 pvd->vdev_ops == &vdev_spare_ops))
3095 expected_error = ENOTSUP;
3096 else if (newvd_is_spare && (!replacing || oldvd_is_log))
3097 expected_error = ENOTSUP;
3098 else if (newvd == oldvd)
3099 expected_error = replacing ? 0 : EBUSY;
3100 else if (vdev_lookup_by_path(rvd, newpath) != NULL)
3101 expected_error = EBUSY;
3102 else if (newsize < oldsize)
3103 expected_error = EOVERFLOW;
3104 else if (ashift > oldvd->vdev_top->vdev_ashift)
3105 expected_error = EDOM;
3109 spa_config_exit(spa, SCL_ALL, FTAG);
3112 * Build the nvlist describing newpath.
3114 root = make_vdev_root(newpath, NULL, NULL, newvd == NULL ? newsize : 0,
3115 ashift, 0, 0, 0, 1);
3117 error = spa_vdev_attach(spa, oldguid, root, replacing);
3122 * If our parent was the replacing vdev, but the replace completed,
3123 * then instead of failing with ENOTSUP we may either succeed,
3124 * fail with ENODEV, or fail with EOVERFLOW.
3126 if (expected_error == ENOTSUP &&
3127 (error == 0 || error == ENODEV || error == EOVERFLOW))
3128 expected_error = error;
3131 * If someone grew the LUN, the replacement may be too small.
3133 if (error == EOVERFLOW || error == EBUSY)
3134 expected_error = error;
3136 if (error == ZFS_ERR_CHECKPOINT_EXISTS ||
3137 error == ZFS_ERR_DISCARDING_CHECKPOINT)
3138 expected_error = error;
3140 /* XXX workaround 6690467 */
3141 if (error != expected_error && expected_error != EBUSY) {
3142 fatal(0, "attach (%s %llu, %s %llu, %d) "
3143 "returned %d, expected %d",
3144 oldpath, oldsize, newpath,
3145 newsize, replacing, error, expected_error);
3148 mutex_exit(&ztest_vdev_lock);
3153 ztest_device_removal(ztest_ds_t *zd, uint64_t id)
3155 spa_t *spa = ztest_spa;
3160 mutex_enter(&ztest_vdev_lock);
3162 if (ztest_device_removal_active) {
3163 mutex_exit(&ztest_vdev_lock);
3168 * Remove a random top-level vdev and wait for removal to finish.
3170 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
3171 vd = vdev_lookup_top(spa, ztest_random_vdev_top(spa, B_FALSE));
3172 guid = vd->vdev_guid;
3173 spa_config_exit(spa, SCL_VDEV, FTAG);
3175 error = spa_vdev_remove(spa, guid, B_FALSE);
3177 ztest_device_removal_active = B_TRUE;
3178 mutex_exit(&ztest_vdev_lock);
3180 while (spa->spa_vdev_removal != NULL)
3181 txg_wait_synced(spa_get_dsl(spa), 0);
3183 mutex_exit(&ztest_vdev_lock);
3188 * The pool needs to be scrubbed after completing device removal.
3189 * Failure to do so may result in checksum errors due to the
3190 * strategy employed by ztest_fault_inject() when selecting which
3191 * offset are redundant and can be damaged.
3193 error = spa_scan(spa, POOL_SCAN_SCRUB);
3195 while (dsl_scan_scrubbing(spa_get_dsl(spa)))
3196 txg_wait_synced(spa_get_dsl(spa), 0);
3199 mutex_enter(&ztest_vdev_lock);
3200 ztest_device_removal_active = B_FALSE;
3201 mutex_exit(&ztest_vdev_lock);
3205 * Callback function which expands the physical size of the vdev.
3208 grow_vdev(vdev_t *vd, void *arg)
3210 spa_t *spa = vd->vdev_spa;
3211 size_t *newsize = arg;
3215 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3216 ASSERT(vd->vdev_ops->vdev_op_leaf);
3218 if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
3221 fsize = lseek(fd, 0, SEEK_END);
3222 (void) ftruncate(fd, *newsize);
3224 if (ztest_opts.zo_verbose >= 6) {
3225 (void) printf("%s grew from %lu to %lu bytes\n",
3226 vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
3233 * Callback function which expands a given vdev by calling vdev_online().
3237 online_vdev(vdev_t *vd, void *arg)
3239 spa_t *spa = vd->vdev_spa;
3240 vdev_t *tvd = vd->vdev_top;
3241 uint64_t guid = vd->vdev_guid;
3242 uint64_t generation = spa->spa_config_generation + 1;
3243 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
3246 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3247 ASSERT(vd->vdev_ops->vdev_op_leaf);
3249 /* Calling vdev_online will initialize the new metaslabs */
3250 spa_config_exit(spa, SCL_STATE, spa);
3251 error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
3252 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3255 * If vdev_online returned an error or the underlying vdev_open
3256 * failed then we abort the expand. The only way to know that
3257 * vdev_open fails is by checking the returned newstate.
3259 if (error || newstate != VDEV_STATE_HEALTHY) {
3260 if (ztest_opts.zo_verbose >= 5) {
3261 (void) printf("Unable to expand vdev, state %llu, "
3262 "error %d\n", (u_longlong_t)newstate, error);
3266 ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
3269 * Since we dropped the lock we need to ensure that we're
3270 * still talking to the original vdev. It's possible this
3271 * vdev may have been detached/replaced while we were
3272 * trying to online it.
3274 if (generation != spa->spa_config_generation) {
3275 if (ztest_opts.zo_verbose >= 5) {
3276 (void) printf("vdev configuration has changed, "
3277 "guid %llu, state %llu, expected gen %llu, "
3280 (u_longlong_t)tvd->vdev_state,
3281 (u_longlong_t)generation,
3282 (u_longlong_t)spa->spa_config_generation);
3290 * Traverse the vdev tree calling the supplied function.
3291 * We continue to walk the tree until we either have walked all
3292 * children or we receive a non-NULL return from the callback.
3293 * If a NULL callback is passed, then we just return back the first
3294 * leaf vdev we encounter.
3297 vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
3299 if (vd->vdev_ops->vdev_op_leaf) {
3303 return (func(vd, arg));
3306 for (uint_t c = 0; c < vd->vdev_children; c++) {
3307 vdev_t *cvd = vd->vdev_child[c];
3308 if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
3315 * Verify that dynamic LUN growth works as expected.
3319 ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
3321 spa_t *spa = ztest_spa;
3323 metaslab_class_t *mc;
3324 metaslab_group_t *mg;
3325 size_t psize, newsize;
3327 uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
3329 mutex_enter(&ztest_checkpoint_lock);
3330 mutex_enter(&ztest_vdev_lock);
3331 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3334 * If there is a vdev removal in progress, it could complete while
3335 * we are running, in which case we would not be able to verify
3336 * that the metaslab_class space increased (because it decreases
3337 * when the device removal completes).
3339 if (ztest_device_removal_active) {
3340 spa_config_exit(spa, SCL_STATE, spa);
3341 mutex_exit(&ztest_vdev_lock);
3342 mutex_exit(&ztest_checkpoint_lock);
3346 top = ztest_random_vdev_top(spa, B_TRUE);
3348 tvd = spa->spa_root_vdev->vdev_child[top];
3351 old_ms_count = tvd->vdev_ms_count;
3352 old_class_space = metaslab_class_get_space(mc);
3355 * Determine the size of the first leaf vdev associated with
3356 * our top-level device.
3358 vd = vdev_walk_tree(tvd, NULL, NULL);
3359 ASSERT3P(vd, !=, NULL);
3360 ASSERT(vd->vdev_ops->vdev_op_leaf);
3362 psize = vd->vdev_psize;
3365 * We only try to expand the vdev if it's healthy, less than 4x its
3366 * original size, and it has a valid psize.
3368 if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
3369 psize == 0 || psize >= 4 * ztest_opts.zo_vdev_size) {
3370 spa_config_exit(spa, SCL_STATE, spa);
3371 mutex_exit(&ztest_vdev_lock);
3372 mutex_exit(&ztest_checkpoint_lock);
3376 newsize = psize + psize / 8;
3377 ASSERT3U(newsize, >, psize);
3379 if (ztest_opts.zo_verbose >= 6) {
3380 (void) printf("Expanding LUN %s from %lu to %lu\n",
3381 vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
3385 * Growing the vdev is a two step process:
3386 * 1). expand the physical size (i.e. relabel)
3387 * 2). online the vdev to create the new metaslabs
3389 if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
3390 vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
3391 tvd->vdev_state != VDEV_STATE_HEALTHY) {
3392 if (ztest_opts.zo_verbose >= 5) {
3393 (void) printf("Could not expand LUN because "
3394 "the vdev configuration changed.\n");
3396 spa_config_exit(spa, SCL_STATE, spa);
3397 mutex_exit(&ztest_vdev_lock);
3398 mutex_exit(&ztest_checkpoint_lock);
3402 spa_config_exit(spa, SCL_STATE, spa);
3405 * Expanding the LUN will update the config asynchronously,
3406 * thus we must wait for the async thread to complete any
3407 * pending tasks before proceeding.
3411 mutex_enter(&spa->spa_async_lock);
3412 done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
3413 mutex_exit(&spa->spa_async_lock);
3416 txg_wait_synced(spa_get_dsl(spa), 0);
3417 (void) poll(NULL, 0, 100);
3420 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3422 tvd = spa->spa_root_vdev->vdev_child[top];
3423 new_ms_count = tvd->vdev_ms_count;
3424 new_class_space = metaslab_class_get_space(mc);
3426 if (tvd->vdev_mg != mg || mg->mg_class != mc) {
3427 if (ztest_opts.zo_verbose >= 5) {
3428 (void) printf("Could not verify LUN expansion due to "
3429 "intervening vdev offline or remove.\n");
3431 spa_config_exit(spa, SCL_STATE, spa);
3432 mutex_exit(&ztest_vdev_lock);
3433 mutex_exit(&ztest_checkpoint_lock);
3438 * Make sure we were able to grow the vdev.
3440 if (new_ms_count <= old_ms_count) {
3441 fatal(0, "LUN expansion failed: ms_count %llu < %llu\n",
3442 old_ms_count, new_ms_count);
3446 * Make sure we were able to grow the pool.
3448 if (new_class_space <= old_class_space) {
3449 fatal(0, "LUN expansion failed: class_space %llu < %llu\n",
3450 old_class_space, new_class_space);
3453 if (ztest_opts.zo_verbose >= 5) {
3454 char oldnumbuf[NN_NUMBUF_SZ], newnumbuf[NN_NUMBUF_SZ];
3456 nicenum(old_class_space, oldnumbuf, sizeof (oldnumbuf));
3457 nicenum(new_class_space, newnumbuf, sizeof (newnumbuf));
3458 (void) printf("%s grew from %s to %s\n",
3459 spa->spa_name, oldnumbuf, newnumbuf);
3462 spa_config_exit(spa, SCL_STATE, spa);
3463 mutex_exit(&ztest_vdev_lock);
3464 mutex_exit(&ztest_checkpoint_lock);
3468 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
3472 ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3475 * Create the objects common to all ztest datasets.
3477 VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
3478 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
3482 ztest_dataset_create(char *dsname)
3484 uint64_t zilset = ztest_random(100);
3485 int err = dmu_objset_create(dsname, DMU_OST_OTHER, 0,
3486 ztest_objset_create_cb, NULL);
3488 if (err || zilset < 80)
3491 if (ztest_opts.zo_verbose >= 6)
3492 (void) printf("Setting dataset %s to sync always\n", dsname);
3493 return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC,
3494 ZFS_SYNC_ALWAYS, B_FALSE));
3499 ztest_objset_destroy_cb(const char *name, void *arg)
3502 dmu_object_info_t doi;
3506 * Verify that the dataset contains a directory object.
3508 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_TRUE, FTAG, &os));
3509 error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
3510 if (error != ENOENT) {
3511 /* We could have crashed in the middle of destroying it */
3513 ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
3514 ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
3516 dmu_objset_disown(os, FTAG);
3519 * Destroy the dataset.
3521 if (strchr(name, '@') != NULL) {
3522 VERIFY0(dsl_destroy_snapshot(name, B_FALSE));
3524 VERIFY0(dsl_destroy_head(name));
3530 ztest_snapshot_create(char *osname, uint64_t id)
3532 char snapname[ZFS_MAX_DATASET_NAME_LEN];
3535 (void) snprintf(snapname, sizeof (snapname), "%llu", (u_longlong_t)id);
3537 error = dmu_objset_snapshot_one(osname, snapname);
3538 if (error == ENOSPC) {
3539 ztest_record_enospc(FTAG);
3542 if (error != 0 && error != EEXIST) {
3543 fatal(0, "ztest_snapshot_create(%s@%s) = %d", osname,
3550 ztest_snapshot_destroy(char *osname, uint64_t id)
3552 char snapname[ZFS_MAX_DATASET_NAME_LEN];
3555 (void) snprintf(snapname, sizeof (snapname), "%s@%llu", osname,
3558 error = dsl_destroy_snapshot(snapname, B_FALSE);
3559 if (error != 0 && error != ENOENT)
3560 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
3566 ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
3572 char name[ZFS_MAX_DATASET_NAME_LEN];
3575 rw_enter(&ztest_name_lock, RW_READER);
3577 (void) snprintf(name, sizeof (name), "%s/temp_%llu",
3578 ztest_opts.zo_pool, (u_longlong_t)id);
3581 * If this dataset exists from a previous run, process its replay log
3582 * half of the time. If we don't replay it, then dmu_objset_destroy()
3583 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
3585 if (ztest_random(2) == 0 &&
3586 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) {
3587 ztest_zd_init(&zdtmp, NULL, os);
3588 zil_replay(os, &zdtmp, ztest_replay_vector);
3589 ztest_zd_fini(&zdtmp);
3590 dmu_objset_disown(os, FTAG);
3594 * There may be an old instance of the dataset we're about to
3595 * create lying around from a previous run. If so, destroy it
3596 * and all of its snapshots.
3598 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
3599 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
3602 * Verify that the destroyed dataset is no longer in the namespace.
3604 VERIFY3U(ENOENT, ==, dmu_objset_own(name, DMU_OST_OTHER, B_TRUE,
3608 * Verify that we can create a new dataset.
3610 error = ztest_dataset_create(name);
3612 if (error == ENOSPC) {
3613 ztest_record_enospc(FTAG);
3614 rw_exit(&ztest_name_lock);
3617 fatal(0, "dmu_objset_create(%s) = %d", name, error);
3620 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os));
3622 ztest_zd_init(&zdtmp, NULL, os);
3625 * Open the intent log for it.
3627 zilog = zil_open(os, ztest_get_data);
3630 * Put some objects in there, do a little I/O to them,
3631 * and randomly take a couple of snapshots along the way.
3633 iters = ztest_random(5);
3634 for (int i = 0; i < iters; i++) {
3635 ztest_dmu_object_alloc_free(&zdtmp, id);
3636 if (ztest_random(iters) == 0)
3637 (void) ztest_snapshot_create(name, i);
3641 * Verify that we cannot create an existing dataset.
3643 VERIFY3U(EEXIST, ==,
3644 dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL));
3647 * Verify that we can hold an objset that is also owned.
3649 VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
3650 dmu_objset_rele(os2, FTAG);
3653 * Verify that we cannot own an objset that is already owned.
3656 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2));
3659 dmu_objset_disown(os, FTAG);
3660 ztest_zd_fini(&zdtmp);
3662 rw_exit(&ztest_name_lock);
3666 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3669 ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
3671 rw_enter(&ztest_name_lock, RW_READER);
3672 (void) ztest_snapshot_destroy(zd->zd_name, id);
3673 (void) ztest_snapshot_create(zd->zd_name, id);
3674 rw_exit(&ztest_name_lock);
3678 * Cleanup non-standard snapshots and clones.
3681 ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
3683 char snap1name[ZFS_MAX_DATASET_NAME_LEN];
3684 char clone1name[ZFS_MAX_DATASET_NAME_LEN];
3685 char snap2name[ZFS_MAX_DATASET_NAME_LEN];
3686 char clone2name[ZFS_MAX_DATASET_NAME_LEN];
3687 char snap3name[ZFS_MAX_DATASET_NAME_LEN];
3690 (void) snprintf(snap1name, sizeof (snap1name),
3691 "%s@s1_%llu", osname, id);
3692 (void) snprintf(clone1name, sizeof (clone1name),
3693 "%s/c1_%llu", osname, id);
3694 (void) snprintf(snap2name, sizeof (snap2name),
3695 "%s@s2_%llu", clone1name, id);
3696 (void) snprintf(clone2name, sizeof (clone2name),
3697 "%s/c2_%llu", osname, id);
3698 (void) snprintf(snap3name, sizeof (snap3name),
3699 "%s@s3_%llu", clone1name, id);
3701 error = dsl_destroy_head(clone2name);
3702 if (error && error != ENOENT)
3703 fatal(0, "dsl_destroy_head(%s) = %d", clone2name, error);
3704 error = dsl_destroy_snapshot(snap3name, B_FALSE);
3705 if (error && error != ENOENT)
3706 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap3name, error);
3707 error = dsl_destroy_snapshot(snap2name, B_FALSE);
3708 if (error && error != ENOENT)
3709 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap2name, error);
3710 error = dsl_destroy_head(clone1name);
3711 if (error && error != ENOENT)
3712 fatal(0, "dsl_destroy_head(%s) = %d", clone1name, error);
3713 error = dsl_destroy_snapshot(snap1name, B_FALSE);
3714 if (error && error != ENOENT)
3715 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap1name, error);
3719 * Verify dsl_dataset_promote handles EBUSY
3722 ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
3725 char snap1name[ZFS_MAX_DATASET_NAME_LEN];
3726 char clone1name[ZFS_MAX_DATASET_NAME_LEN];
3727 char snap2name[ZFS_MAX_DATASET_NAME_LEN];
3728 char clone2name[ZFS_MAX_DATASET_NAME_LEN];
3729 char snap3name[ZFS_MAX_DATASET_NAME_LEN];
3730 char *osname = zd->zd_name;
3733 rw_enter(&ztest_name_lock, RW_READER);
3735 ztest_dsl_dataset_cleanup(osname, id);
3737 (void) snprintf(snap1name, sizeof (snap1name),
3738 "%s@s1_%llu", osname, id);
3739 (void) snprintf(clone1name, sizeof (clone1name),
3740 "%s/c1_%llu", osname, id);
3741 (void) snprintf(snap2name, sizeof (snap2name),
3742 "%s@s2_%llu", clone1name, id);
3743 (void) snprintf(clone2name, sizeof (clone2name),
3744 "%s/c2_%llu", osname, id);
3745 (void) snprintf(snap3name, sizeof (snap3name),
3746 "%s@s3_%llu", clone1name, id);
3748 error = dmu_objset_snapshot_one(osname, strchr(snap1name, '@') + 1);
3749 if (error && error != EEXIST) {
3750 if (error == ENOSPC) {
3751 ztest_record_enospc(FTAG);
3754 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
3757 error = dmu_objset_clone(clone1name, snap1name);
3759 if (error == ENOSPC) {
3760 ztest_record_enospc(FTAG);
3763 fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
3766 error = dmu_objset_snapshot_one(clone1name, strchr(snap2name, '@') + 1);
3767 if (error && error != EEXIST) {
3768 if (error == ENOSPC) {
3769 ztest_record_enospc(FTAG);
3772 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
3775 error = dmu_objset_snapshot_one(clone1name, strchr(snap3name, '@') + 1);
3776 if (error && error != EEXIST) {
3777 if (error == ENOSPC) {
3778 ztest_record_enospc(FTAG);
3781 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
3784 error = dmu_objset_clone(clone2name, snap3name);
3786 if (error == ENOSPC) {
3787 ztest_record_enospc(FTAG);
3790 fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
3793 error = dmu_objset_own(snap2name, DMU_OST_ANY, B_TRUE, FTAG, &os);
3795 fatal(0, "dmu_objset_own(%s) = %d", snap2name, error);
3796 error = dsl_dataset_promote(clone2name, NULL);
3797 if (error == ENOSPC) {
3798 dmu_objset_disown(os, FTAG);
3799 ztest_record_enospc(FTAG);
3803 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
3805 dmu_objset_disown(os, FTAG);
3808 ztest_dsl_dataset_cleanup(osname, id);
3810 rw_exit(&ztest_name_lock);
3814 * Verify that dmu_object_{alloc,free} work as expected.
3817 ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
3820 int batchsize = sizeof (od) / sizeof (od[0]);
3822 for (int b = 0; b < batchsize; b++)
3823 ztest_od_init(&od[b], id, FTAG, b, DMU_OT_UINT64_OTHER, 0, 0, 0);
3826 * Destroy the previous batch of objects, create a new batch,
3827 * and do some I/O on the new objects.
3829 if (ztest_object_init(zd, od, sizeof (od), B_TRUE) != 0)
3832 while (ztest_random(4 * batchsize) != 0)
3833 ztest_io(zd, od[ztest_random(batchsize)].od_object,
3834 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3838 * Verify that dmu_{read,write} work as expected.
3841 ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
3843 objset_t *os = zd->zd_os;
3846 int i, freeit, error;
3848 bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
3849 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3850 uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
3851 uint64_t regions = 997;
3852 uint64_t stride = 123456789ULL;
3853 uint64_t width = 40;
3854 int free_percent = 5;
3857 * This test uses two objects, packobj and bigobj, that are always
3858 * updated together (i.e. in the same tx) so that their contents are
3859 * in sync and can be compared. Their contents relate to each other
3860 * in a simple way: packobj is a dense array of 'bufwad' structures,
3861 * while bigobj is a sparse array of the same bufwads. Specifically,
3862 * for any index n, there are three bufwads that should be identical:
3864 * packobj, at offset n * sizeof (bufwad_t)
3865 * bigobj, at the head of the nth chunk
3866 * bigobj, at the tail of the nth chunk
3868 * The chunk size is arbitrary. It doesn't have to be a power of two,
3869 * and it doesn't have any relation to the object blocksize.
3870 * The only requirement is that it can hold at least two bufwads.
3872 * Normally, we write the bufwad to each of these locations.
3873 * However, free_percent of the time we instead write zeroes to
3874 * packobj and perform a dmu_free_range() on bigobj. By comparing
3875 * bigobj to packobj, we can verify that the DMU is correctly
3876 * tracking which parts of an object are allocated and free,
3877 * and that the contents of the allocated blocks are correct.
3881 * Read the directory info. If it's the first time, set things up.
3883 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, chunksize);
3884 ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, 0, chunksize);
3886 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
3889 bigobj = od[0].od_object;
3890 packobj = od[1].od_object;
3891 chunksize = od[0].od_gen;
3892 ASSERT(chunksize == od[1].od_gen);
3895 * Prefetch a random chunk of the big object.
3896 * Our aim here is to get some async reads in flight
3897 * for blocks that we may free below; the DMU should
3898 * handle this race correctly.
3900 n = ztest_random(regions) * stride + ztest_random(width);
3901 s = 1 + ztest_random(2 * width - 1);
3902 dmu_prefetch(os, bigobj, 0, n * chunksize, s * chunksize,
3903 ZIO_PRIORITY_SYNC_READ);
3906 * Pick a random index and compute the offsets into packobj and bigobj.
3908 n = ztest_random(regions) * stride + ztest_random(width);
3909 s = 1 + ztest_random(width - 1);
3911 packoff = n * sizeof (bufwad_t);
3912 packsize = s * sizeof (bufwad_t);
3914 bigoff = n * chunksize;
3915 bigsize = s * chunksize;
3917 packbuf = umem_alloc(packsize, UMEM_NOFAIL);
3918 bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
3921 * free_percent of the time, free a range of bigobj rather than
3924 freeit = (ztest_random(100) < free_percent);
3927 * Read the current contents of our objects.
3929 error = dmu_read(os, packobj, packoff, packsize, packbuf,
3932 error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
3937 * Get a tx for the mods to both packobj and bigobj.
3939 tx = dmu_tx_create(os);
3941 dmu_tx_hold_write(tx, packobj, packoff, packsize);
3944 dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
3946 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
3948 /* This accounts for setting the checksum/compression. */
3949 dmu_tx_hold_bonus(tx, bigobj);
3951 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3953 umem_free(packbuf, packsize);
3954 umem_free(bigbuf, bigsize);
3958 enum zio_checksum cksum;
3960 cksum = (enum zio_checksum)
3961 ztest_random_dsl_prop(ZFS_PROP_CHECKSUM);
3962 } while (cksum >= ZIO_CHECKSUM_LEGACY_FUNCTIONS);
3963 dmu_object_set_checksum(os, bigobj, cksum, tx);
3965 enum zio_compress comp;
3967 comp = (enum zio_compress)
3968 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION);
3969 } while (comp >= ZIO_COMPRESS_LEGACY_FUNCTIONS);
3970 dmu_object_set_compress(os, bigobj, comp, tx);
3973 * For each index from n to n + s, verify that the existing bufwad
3974 * in packobj matches the bufwads at the head and tail of the
3975 * corresponding chunk in bigobj. Then update all three bufwads
3976 * with the new values we want to write out.
3978 for (i = 0; i < s; i++) {
3980 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3982 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
3984 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
3986 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3987 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3989 if (pack->bw_txg > txg)
3990 fatal(0, "future leak: got %llx, open txg is %llx",
3993 if (pack->bw_data != 0 && pack->bw_index != n + i)
3994 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3995 pack->bw_index, n, i);
3997 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3998 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
4000 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
4001 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
4004 bzero(pack, sizeof (bufwad_t));
4006 pack->bw_index = n + i;
4008 pack->bw_data = 1 + ztest_random(-2ULL);
4015 * We've verified all the old bufwads, and made new ones.
4016 * Now write them out.
4018 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
4021 if (ztest_opts.zo_verbose >= 7) {
4022 (void) printf("freeing offset %llx size %llx"
4024 (u_longlong_t)bigoff,
4025 (u_longlong_t)bigsize,
4028 VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
4030 if (ztest_opts.zo_verbose >= 7) {
4031 (void) printf("writing offset %llx size %llx"
4033 (u_longlong_t)bigoff,
4034 (u_longlong_t)bigsize,
4037 dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
4043 * Sanity check the stuff we just wrote.
4046 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
4047 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
4049 VERIFY(0 == dmu_read(os, packobj, packoff,
4050 packsize, packcheck, DMU_READ_PREFETCH));
4051 VERIFY(0 == dmu_read(os, bigobj, bigoff,
4052 bigsize, bigcheck, DMU_READ_PREFETCH));
4054 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
4055 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
4057 umem_free(packcheck, packsize);
4058 umem_free(bigcheck, bigsize);
4061 umem_free(packbuf, packsize);
4062 umem_free(bigbuf, bigsize);
4066 compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
4067 uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
4075 * For each index from n to n + s, verify that the existing bufwad
4076 * in packobj matches the bufwads at the head and tail of the
4077 * corresponding chunk in bigobj. Then update all three bufwads
4078 * with the new values we want to write out.
4080 for (i = 0; i < s; i++) {
4082 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
4084 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
4086 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
4088 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
4089 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
4091 if (pack->bw_txg > txg)
4092 fatal(0, "future leak: got %llx, open txg is %llx",
4095 if (pack->bw_data != 0 && pack->bw_index != n + i)
4096 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4097 pack->bw_index, n, i);
4099 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
4100 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
4102 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
4103 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
4105 pack->bw_index = n + i;
4107 pack->bw_data = 1 + ztest_random(-2ULL);
4115 ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
4117 objset_t *os = zd->zd_os;
4123 bufwad_t *packbuf, *bigbuf;
4124 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
4125 uint64_t blocksize = ztest_random_blocksize();
4126 uint64_t chunksize = blocksize;
4127 uint64_t regions = 997;
4128 uint64_t stride = 123456789ULL;
4130 dmu_buf_t *bonus_db;
4131 arc_buf_t **bigbuf_arcbufs;
4132 dmu_object_info_t doi;
4135 * This test uses two objects, packobj and bigobj, that are always
4136 * updated together (i.e. in the same tx) so that their contents are
4137 * in sync and can be compared. Their contents relate to each other
4138 * in a simple way: packobj is a dense array of 'bufwad' structures,
4139 * while bigobj is a sparse array of the same bufwads. Specifically,
4140 * for any index n, there are three bufwads that should be identical:
4142 * packobj, at offset n * sizeof (bufwad_t)
4143 * bigobj, at the head of the nth chunk
4144 * bigobj, at the tail of the nth chunk
4146 * The chunk size is set equal to bigobj block size so that
4147 * dmu_assign_arcbuf() can be tested for object updates.
4151 * Read the directory info. If it's the first time, set things up.
4153 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0, 0);
4154 ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, 0, chunksize);
4156 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4159 bigobj = od[0].od_object;
4160 packobj = od[1].od_object;
4161 blocksize = od[0].od_blocksize;
4162 chunksize = blocksize;
4163 ASSERT(chunksize == od[1].od_gen);
4165 VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
4166 VERIFY(ISP2(doi.doi_data_block_size));
4167 VERIFY(chunksize == doi.doi_data_block_size);
4168 VERIFY(chunksize >= 2 * sizeof (bufwad_t));
4171 * Pick a random index and compute the offsets into packobj and bigobj.
4173 n = ztest_random(regions) * stride + ztest_random(width);
4174 s = 1 + ztest_random(width - 1);
4176 packoff = n * sizeof (bufwad_t);
4177 packsize = s * sizeof (bufwad_t);
4179 bigoff = n * chunksize;
4180 bigsize = s * chunksize;
4182 packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
4183 bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
4185 VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
4187 bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
4190 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
4191 * Iteration 1 test zcopy to already referenced dbufs.
4192 * Iteration 2 test zcopy to dirty dbuf in the same txg.
4193 * Iteration 3 test zcopy to dbuf dirty in previous txg.
4194 * Iteration 4 test zcopy when dbuf is no longer dirty.
4195 * Iteration 5 test zcopy when it can't be done.
4196 * Iteration 6 one more zcopy write.
4198 for (i = 0; i < 7; i++) {
4203 * In iteration 5 (i == 5) use arcbufs
4204 * that don't match bigobj blksz to test
4205 * dmu_assign_arcbuf() when it can't directly
4206 * assign an arcbuf to a dbuf.
4208 for (j = 0; j < s; j++) {
4211 dmu_request_arcbuf(bonus_db, chunksize);
4213 bigbuf_arcbufs[2 * j] =
4214 dmu_request_arcbuf(bonus_db, chunksize / 2);
4215 bigbuf_arcbufs[2 * j + 1] =
4216 dmu_request_arcbuf(bonus_db, chunksize / 2);
4221 * Get a tx for the mods to both packobj and bigobj.
4223 tx = dmu_tx_create(os);
4225 dmu_tx_hold_write(tx, packobj, packoff, packsize);
4226 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
4228 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4230 umem_free(packbuf, packsize);
4231 umem_free(bigbuf, bigsize);
4232 for (j = 0; j < s; j++) {
4234 dmu_return_arcbuf(bigbuf_arcbufs[j]);
4237 bigbuf_arcbufs[2 * j]);
4239 bigbuf_arcbufs[2 * j + 1]);
4242 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4243 dmu_buf_rele(bonus_db, FTAG);
4248 * 50% of the time don't read objects in the 1st iteration to
4249 * test dmu_assign_arcbuf() for the case when there're no
4250 * existing dbufs for the specified offsets.
4252 if (i != 0 || ztest_random(2) != 0) {
4253 error = dmu_read(os, packobj, packoff,
4254 packsize, packbuf, DMU_READ_PREFETCH);
4256 error = dmu_read(os, bigobj, bigoff, bigsize,
4257 bigbuf, DMU_READ_PREFETCH);
4260 compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
4264 * We've verified all the old bufwads, and made new ones.
4265 * Now write them out.
4267 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
4268 if (ztest_opts.zo_verbose >= 7) {
4269 (void) printf("writing offset %llx size %llx"
4271 (u_longlong_t)bigoff,
4272 (u_longlong_t)bigsize,
4275 for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
4278 bcopy((caddr_t)bigbuf + (off - bigoff),
4279 bigbuf_arcbufs[j]->b_data, chunksize);
4281 bcopy((caddr_t)bigbuf + (off - bigoff),
4282 bigbuf_arcbufs[2 * j]->b_data,
4284 bcopy((caddr_t)bigbuf + (off - bigoff) +
4286 bigbuf_arcbufs[2 * j + 1]->b_data,
4291 VERIFY(dmu_buf_hold(os, bigobj, off,
4292 FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0);
4295 dmu_assign_arcbuf(bonus_db, off,
4296 bigbuf_arcbufs[j], tx);
4298 dmu_assign_arcbuf(bonus_db, off,
4299 bigbuf_arcbufs[2 * j], tx);
4300 dmu_assign_arcbuf(bonus_db,
4301 off + chunksize / 2,
4302 bigbuf_arcbufs[2 * j + 1], tx);
4305 dmu_buf_rele(dbt, FTAG);
4311 * Sanity check the stuff we just wrote.
4314 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
4315 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
4317 VERIFY(0 == dmu_read(os, packobj, packoff,
4318 packsize, packcheck, DMU_READ_PREFETCH));
4319 VERIFY(0 == dmu_read(os, bigobj, bigoff,
4320 bigsize, bigcheck, DMU_READ_PREFETCH));
4322 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
4323 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
4325 umem_free(packcheck, packsize);
4326 umem_free(bigcheck, bigsize);
4329 txg_wait_open(dmu_objset_pool(os), 0);
4330 } else if (i == 3) {
4331 txg_wait_synced(dmu_objset_pool(os), 0);
4335 dmu_buf_rele(bonus_db, FTAG);
4336 umem_free(packbuf, packsize);
4337 umem_free(bigbuf, bigsize);
4338 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4343 ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
4346 uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
4347 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4350 * Have multiple threads write to large offsets in an object
4351 * to verify that parallel writes to an object -- even to the
4352 * same blocks within the object -- doesn't cause any trouble.
4354 ztest_od_init(&od[0], ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
4356 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4359 while (ztest_random(10) != 0)
4360 ztest_io(zd, od[0].od_object, offset);
4364 ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
4367 uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
4368 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4369 uint64_t count = ztest_random(20) + 1;
4370 uint64_t blocksize = ztest_random_blocksize();
4373 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0, 0);
4375 if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
4378 if (ztest_truncate(zd, od[0].od_object, offset, count * blocksize) != 0)
4381 ztest_prealloc(zd, od[0].od_object, offset, count * blocksize);
4383 data = umem_zalloc(blocksize, UMEM_NOFAIL);
4385 while (ztest_random(count) != 0) {
4386 uint64_t randoff = offset + (ztest_random(count) * blocksize);
4387 if (ztest_write(zd, od[0].od_object, randoff, blocksize,
4390 while (ztest_random(4) != 0)
4391 ztest_io(zd, od[0].od_object, randoff);
4394 umem_free(data, blocksize);
4398 * Verify that zap_{create,destroy,add,remove,update} work as expected.
4400 #define ZTEST_ZAP_MIN_INTS 1
4401 #define ZTEST_ZAP_MAX_INTS 4
4402 #define ZTEST_ZAP_MAX_PROPS 1000
4405 ztest_zap(ztest_ds_t *zd, uint64_t id)
4407 objset_t *os = zd->zd_os;
4410 uint64_t txg, last_txg;
4411 uint64_t value[ZTEST_ZAP_MAX_INTS];
4412 uint64_t zl_ints, zl_intsize, prop;
4415 char propname[100], txgname[100];
4417 char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
4419 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0, 0);
4421 if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
4424 object = od[0].od_object;
4427 * Generate a known hash collision, and verify that
4428 * we can lookup and remove both entries.
4430 tx = dmu_tx_create(os);
4431 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4432 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4435 for (i = 0; i < 2; i++) {
4437 VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
4440 for (i = 0; i < 2; i++) {
4441 VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
4442 sizeof (uint64_t), 1, &value[i], tx));
4444 zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
4445 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4446 ASSERT3U(zl_ints, ==, 1);
4448 for (i = 0; i < 2; i++) {
4449 VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
4454 * Generate a buch of random entries.
4456 ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
4458 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4459 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4460 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4461 bzero(value, sizeof (value));
4465 * If these zap entries already exist, validate their contents.
4467 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4469 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4470 ASSERT3U(zl_ints, ==, 1);
4472 VERIFY(zap_lookup(os, object, txgname, zl_intsize,
4473 zl_ints, &last_txg) == 0);
4475 VERIFY(zap_length(os, object, propname, &zl_intsize,
4478 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4479 ASSERT3U(zl_ints, ==, ints);
4481 VERIFY(zap_lookup(os, object, propname, zl_intsize,
4482 zl_ints, value) == 0);
4484 for (i = 0; i < ints; i++) {
4485 ASSERT3U(value[i], ==, last_txg + object + i);
4488 ASSERT3U(error, ==, ENOENT);
4492 * Atomically update two entries in our zap object.
4493 * The first is named txg_%llu, and contains the txg
4494 * in which the property was last updated. The second
4495 * is named prop_%llu, and the nth element of its value
4496 * should be txg + object + n.
4498 tx = dmu_tx_create(os);
4499 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4500 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4505 fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
4507 for (i = 0; i < ints; i++)
4508 value[i] = txg + object + i;
4510 VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
4512 VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
4518 * Remove a random pair of entries.
4520 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4521 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4522 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4524 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4526 if (error == ENOENT)
4531 tx = dmu_tx_create(os);
4532 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4533 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4536 VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
4537 VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
4542 * Testcase to test the upgrading of a microzap to fatzap.
4545 ztest_fzap(ztest_ds_t *zd, uint64_t id)
4547 objset_t *os = zd->zd_os;
4549 uint64_t object, txg;
4551 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0, 0);
4553 if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
4556 object = od[0].od_object;
4559 * Add entries to this ZAP and make sure it spills over
4560 * and gets upgraded to a fatzap. Also, since we are adding
4561 * 2050 entries we should see ptrtbl growth and leaf-block split.
4563 for (int i = 0; i < 2050; i++) {
4564 char name[ZFS_MAX_DATASET_NAME_LEN];
4569 (void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
4572 tx = dmu_tx_create(os);
4573 dmu_tx_hold_zap(tx, object, B_TRUE, name);
4574 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4577 error = zap_add(os, object, name, sizeof (uint64_t), 1,
4579 ASSERT(error == 0 || error == EEXIST);
4586 ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
4588 objset_t *os = zd->zd_os;
4590 uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
4592 int i, namelen, error;
4593 int micro = ztest_random(2);
4594 char name[20], string_value[20];
4597 ztest_od_init(&od[0], ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER, 0, 0, 0);
4599 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4602 object = od[0].od_object;
4605 * Generate a random name of the form 'xxx.....' where each
4606 * x is a random printable character and the dots are dots.
4607 * There are 94 such characters, and the name length goes from
4608 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
4610 namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
4612 for (i = 0; i < 3; i++)
4613 name[i] = '!' + ztest_random('~' - '!' + 1);
4614 for (; i < namelen - 1; i++)
4618 if ((namelen & 1) || micro) {
4619 wsize = sizeof (txg);
4625 data = string_value;
4629 VERIFY0(zap_count(os, object, &count));
4630 ASSERT(count != -1ULL);
4633 * Select an operation: length, lookup, add, update, remove.
4635 i = ztest_random(5);
4638 tx = dmu_tx_create(os);
4639 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4640 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4643 bcopy(name, string_value, namelen);
4647 bzero(string_value, namelen);
4653 error = zap_length(os, object, name, &zl_wsize, &zl_wc);
4655 ASSERT3U(wsize, ==, zl_wsize);
4656 ASSERT3U(wc, ==, zl_wc);
4658 ASSERT3U(error, ==, ENOENT);
4663 error = zap_lookup(os, object, name, wsize, wc, data);
4665 if (data == string_value &&
4666 bcmp(name, data, namelen) != 0)
4667 fatal(0, "name '%s' != val '%s' len %d",
4668 name, data, namelen);
4670 ASSERT3U(error, ==, ENOENT);
4675 error = zap_add(os, object, name, wsize, wc, data, tx);
4676 ASSERT(error == 0 || error == EEXIST);
4680 VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
4684 error = zap_remove(os, object, name, tx);
4685 ASSERT(error == 0 || error == ENOENT);
4694 * Commit callback data.
4696 typedef struct ztest_cb_data {
4697 list_node_t zcd_node;
4699 int zcd_expected_err;
4700 boolean_t zcd_added;
4701 boolean_t zcd_called;
4705 /* This is the actual commit callback function */
4707 ztest_commit_callback(void *arg, int error)
4709 ztest_cb_data_t *data = arg;
4710 uint64_t synced_txg;
4712 VERIFY(data != NULL);
4713 VERIFY3S(data->zcd_expected_err, ==, error);
4714 VERIFY(!data->zcd_called);
4716 synced_txg = spa_last_synced_txg(data->zcd_spa);
4717 if (data->zcd_txg > synced_txg)
4718 fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
4719 ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
4722 data->zcd_called = B_TRUE;
4724 if (error == ECANCELED) {
4725 ASSERT0(data->zcd_txg);
4726 ASSERT(!data->zcd_added);
4729 * The private callback data should be destroyed here, but
4730 * since we are going to check the zcd_called field after
4731 * dmu_tx_abort(), we will destroy it there.
4736 /* Was this callback added to the global callback list? */
4737 if (!data->zcd_added)
4740 ASSERT3U(data->zcd_txg, !=, 0);
4742 /* Remove our callback from the list */
4743 mutex_enter(&zcl.zcl_callbacks_lock);
4744 list_remove(&zcl.zcl_callbacks, data);
4745 mutex_exit(&zcl.zcl_callbacks_lock);
4748 umem_free(data, sizeof (ztest_cb_data_t));
4751 /* Allocate and initialize callback data structure */
4752 static ztest_cb_data_t *
4753 ztest_create_cb_data(objset_t *os, uint64_t txg)
4755 ztest_cb_data_t *cb_data;
4757 cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
4759 cb_data->zcd_txg = txg;
4760 cb_data->zcd_spa = dmu_objset_spa(os);
4766 * If a number of txgs equal to this threshold have been created after a commit
4767 * callback has been registered but not called, then we assume there is an
4768 * implementation bug.
4770 #define ZTEST_COMMIT_CALLBACK_THRESH (TXG_CONCURRENT_STATES + 2)
4773 * Commit callback test.
4776 ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
4778 objset_t *os = zd->zd_os;
4781 ztest_cb_data_t *cb_data[3], *tmp_cb;
4782 uint64_t old_txg, txg;
4785 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
4787 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4790 tx = dmu_tx_create(os);
4792 cb_data[0] = ztest_create_cb_data(os, 0);
4793 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
4795 dmu_tx_hold_write(tx, od[0].od_object, 0, sizeof (uint64_t));
4797 /* Every once in a while, abort the transaction on purpose */
4798 if (ztest_random(100) == 0)
4802 error = dmu_tx_assign(tx, TXG_NOWAIT);
4804 txg = error ? 0 : dmu_tx_get_txg(tx);
4806 cb_data[0]->zcd_txg = txg;
4807 cb_data[1] = ztest_create_cb_data(os, txg);
4808 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
4812 * It's not a strict requirement to call the registered
4813 * callbacks from inside dmu_tx_abort(), but that's what
4814 * it's supposed to happen in the current implementation
4815 * so we will check for that.
4817 for (i = 0; i < 2; i++) {
4818 cb_data[i]->zcd_expected_err = ECANCELED;
4819 VERIFY(!cb_data[i]->zcd_called);
4824 for (i = 0; i < 2; i++) {
4825 VERIFY(cb_data[i]->zcd_called);
4826 umem_free(cb_data[i], sizeof (ztest_cb_data_t));
4832 cb_data[2] = ztest_create_cb_data(os, txg);
4833 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
4836 * Read existing data to make sure there isn't a future leak.
4838 VERIFY(0 == dmu_read(os, od[0].od_object, 0, sizeof (uint64_t),
4839 &old_txg, DMU_READ_PREFETCH));
4842 fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
4845 dmu_write(os, od[0].od_object, 0, sizeof (uint64_t), &txg, tx);
4847 mutex_enter(&zcl.zcl_callbacks_lock);
4850 * Since commit callbacks don't have any ordering requirement and since
4851 * it is theoretically possible for a commit callback to be called
4852 * after an arbitrary amount of time has elapsed since its txg has been
4853 * synced, it is difficult to reliably determine whether a commit
4854 * callback hasn't been called due to high load or due to a flawed
4857 * In practice, we will assume that if after a certain number of txgs a
4858 * commit callback hasn't been called, then most likely there's an
4859 * implementation bug..
4861 tmp_cb = list_head(&zcl.zcl_callbacks);
4862 if (tmp_cb != NULL &&
4863 (txg - ZTEST_COMMIT_CALLBACK_THRESH) > tmp_cb->zcd_txg) {
4864 fatal(0, "Commit callback threshold exceeded, oldest txg: %"
4865 PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
4869 * Let's find the place to insert our callbacks.
4871 * Even though the list is ordered by txg, it is possible for the
4872 * insertion point to not be the end because our txg may already be
4873 * quiescing at this point and other callbacks in the open txg
4874 * (from other objsets) may have sneaked in.
4876 tmp_cb = list_tail(&zcl.zcl_callbacks);
4877 while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
4878 tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
4880 /* Add the 3 callbacks to the list */
4881 for (i = 0; i < 3; i++) {
4883 list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
4885 list_insert_after(&zcl.zcl_callbacks, tmp_cb,
4888 cb_data[i]->zcd_added = B_TRUE;
4889 VERIFY(!cb_data[i]->zcd_called);
4891 tmp_cb = cb_data[i];
4894 mutex_exit(&zcl.zcl_callbacks_lock);
4900 * Visit each object in the dataset. Verify that its properties
4901 * are consistent what was stored in the block tag when it was created,
4902 * and that its unused bonus buffer space has not been overwritten.
4905 ztest_verify_dnode_bt(ztest_ds_t *zd, uint64_t id)
4907 objset_t *os = zd->zd_os;
4911 for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
4912 ztest_block_tag_t *bt = NULL;
4913 dmu_object_info_t doi;
4916 if (dmu_bonus_hold(os, obj, FTAG, &db) != 0)
4919 dmu_object_info_from_db(db, &doi);
4920 if (doi.doi_bonus_size >= sizeof (*bt))
4921 bt = ztest_bt_bonus(db);
4923 if (bt && bt->bt_magic == BT_MAGIC) {
4924 ztest_bt_verify(bt, os, obj, doi.doi_dnodesize,
4925 bt->bt_offset, bt->bt_gen, bt->bt_txg,
4927 ztest_verify_unused_bonus(db, bt, obj, os, bt->bt_gen);
4930 dmu_buf_rele(db, FTAG);
4936 ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
4938 zfs_prop_t proplist[] = {
4940 ZFS_PROP_COMPRESSION,
4945 rw_enter(&ztest_name_lock, RW_READER);
4947 for (int p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
4948 (void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
4949 ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
4951 rw_exit(&ztest_name_lock);
4956 ztest_remap_blocks(ztest_ds_t *zd, uint64_t id)
4958 rw_enter(&ztest_name_lock, RW_READER);
4960 int error = dmu_objset_remap_indirects(zd->zd_name);
4961 if (error == ENOSPC)
4965 rw_exit(&ztest_name_lock);
4970 ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
4972 nvlist_t *props = NULL;
4974 rw_enter(&ztest_name_lock, RW_READER);
4976 (void) ztest_spa_prop_set_uint64(ZPOOL_PROP_DEDUPDITTO,
4977 ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
4979 VERIFY0(spa_prop_get(ztest_spa, &props));
4981 if (ztest_opts.zo_verbose >= 6)
4982 dump_nvlist(props, 4);
4986 rw_exit(&ztest_name_lock);
4990 user_release_one(const char *snapname, const char *holdname)
4992 nvlist_t *snaps, *holds;
4995 snaps = fnvlist_alloc();
4996 holds = fnvlist_alloc();
4997 fnvlist_add_boolean(holds, holdname);
4998 fnvlist_add_nvlist(snaps, snapname, holds);
4999 fnvlist_free(holds);
5000 error = dsl_dataset_user_release(snaps, NULL);
5001 fnvlist_free(snaps);
5006 * Test snapshot hold/release and deferred destroy.
5009 ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
5012 objset_t *os = zd->zd_os;
5016 char clonename[100];
5018 char osname[ZFS_MAX_DATASET_NAME_LEN];
5021 rw_enter(&ztest_name_lock, RW_READER);
5023 dmu_objset_name(os, osname);
5025 (void) snprintf(snapname, sizeof (snapname), "sh1_%llu", id);
5026 (void) snprintf(fullname, sizeof (fullname), "%s@%s", osname, snapname);
5027 (void) snprintf(clonename, sizeof (clonename),
5028 "%s/ch1_%llu", osname, id);
5029 (void) snprintf(tag, sizeof (tag), "tag_%llu", id);
5032 * Clean up from any previous run.
5034 error = dsl_destroy_head(clonename);
5035 if (error != ENOENT)
5037 error = user_release_one(fullname, tag);
5038 if (error != ESRCH && error != ENOENT)
5040 error = dsl_destroy_snapshot(fullname, B_FALSE);
5041 if (error != ENOENT)
5045 * Create snapshot, clone it, mark snap for deferred destroy,
5046 * destroy clone, verify snap was also destroyed.
5048 error = dmu_objset_snapshot_one(osname, snapname);
5050 if (error == ENOSPC) {
5051 ztest_record_enospc("dmu_objset_snapshot");
5054 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
5057 error = dmu_objset_clone(clonename, fullname);
5059 if (error == ENOSPC) {
5060 ztest_record_enospc("dmu_objset_clone");
5063 fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
5066 error = dsl_destroy_snapshot(fullname, B_TRUE);
5068 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5072 error = dsl_destroy_head(clonename);
5074 fatal(0, "dsl_destroy_head(%s) = %d", clonename, error);
5076 error = dmu_objset_hold(fullname, FTAG, &origin);
5077 if (error != ENOENT)
5078 fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
5081 * Create snapshot, add temporary hold, verify that we can't
5082 * destroy a held snapshot, mark for deferred destroy,
5083 * release hold, verify snapshot was destroyed.
5085 error = dmu_objset_snapshot_one(osname, snapname);
5087 if (error == ENOSPC) {
5088 ztest_record_enospc("dmu_objset_snapshot");
5091 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
5094 holds = fnvlist_alloc();
5095 fnvlist_add_string(holds, fullname, tag);
5096 error = dsl_dataset_user_hold(holds, 0, NULL);
5097 fnvlist_free(holds);
5099 if (error == ENOSPC) {
5100 ztest_record_enospc("dsl_dataset_user_hold");
5103 fatal(0, "dsl_dataset_user_hold(%s, %s) = %u",
5104 fullname, tag, error);
5107 error = dsl_destroy_snapshot(fullname, B_FALSE);
5108 if (error != EBUSY) {
5109 fatal(0, "dsl_destroy_snapshot(%s, B_FALSE) = %d",
5113 error = dsl_destroy_snapshot(fullname, B_TRUE);
5115 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5119 error = user_release_one(fullname, tag);
5121 fatal(0, "user_release_one(%s, %s) = %d", fullname, tag, error);
5123 VERIFY3U(dmu_objset_hold(fullname, FTAG, &origin), ==, ENOENT);
5126 rw_exit(&ztest_name_lock);
5130 * Inject random faults into the on-disk data.
5134 ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
5136 ztest_shared_t *zs = ztest_shared;
5137 spa_t *spa = ztest_spa;
5141 uint64_t bad = 0x1990c0ffeedecadeULL;
5143 char path0[MAXPATHLEN];
5144 char pathrand[MAXPATHLEN];
5146 int bshift = SPA_MAXBLOCKSHIFT + 2;
5152 boolean_t islog = B_FALSE;
5154 mutex_enter(&ztest_vdev_lock);
5157 * Device removal is in progress, fault injection must be disabled
5158 * until it completes and the pool is scrubbed. The fault injection
5159 * strategy for damaging blocks does not take in to account evacuated
5160 * blocks which may have already been damaged.
5162 if (ztest_device_removal_active) {
5163 mutex_exit(&ztest_vdev_lock);
5167 maxfaults = MAXFAULTS();
5168 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
5169 mirror_save = zs->zs_mirrors;
5170 mutex_exit(&ztest_vdev_lock);
5172 ASSERT(leaves >= 1);
5175 * Grab the name lock as reader. There are some operations
5176 * which don't like to have their vdevs changed while
5177 * they are in progress (i.e. spa_change_guid). Those
5178 * operations will have grabbed the name lock as writer.
5180 rw_enter(&ztest_name_lock, RW_READER);
5183 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
5185 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5187 if (ztest_random(2) == 0) {
5189 * Inject errors on a normal data device or slog device.
5191 top = ztest_random_vdev_top(spa, B_TRUE);
5192 leaf = ztest_random(leaves) + zs->zs_splits;
5195 * Generate paths to the first leaf in this top-level vdev,
5196 * and to the random leaf we selected. We'll induce transient
5197 * write failures and random online/offline activity on leaf 0,
5198 * and we'll write random garbage to the randomly chosen leaf.
5200 (void) snprintf(path0, sizeof (path0), ztest_dev_template,
5201 ztest_opts.zo_dir, ztest_opts.zo_pool,
5202 top * leaves + zs->zs_splits);
5203 (void) snprintf(pathrand, sizeof (pathrand), ztest_dev_template,
5204 ztest_opts.zo_dir, ztest_opts.zo_pool,
5205 top * leaves + leaf);
5207 vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
5208 if (vd0 != NULL && vd0->vdev_top->vdev_islog)
5212 * If the top-level vdev needs to be resilvered
5213 * then we only allow faults on the device that is
5216 if (vd0 != NULL && maxfaults != 1 &&
5217 (!vdev_resilver_needed(vd0->vdev_top, NULL, NULL) ||
5218 vd0->vdev_resilver_txg != 0)) {
5220 * Make vd0 explicitly claim to be unreadable,
5221 * or unwriteable, or reach behind its back
5222 * and close the underlying fd. We can do this if
5223 * maxfaults == 0 because we'll fail and reexecute,
5224 * and we can do it if maxfaults >= 2 because we'll
5225 * have enough redundancy. If maxfaults == 1, the
5226 * combination of this with injection of random data
5227 * corruption below exceeds the pool's fault tolerance.
5229 vdev_file_t *vf = vd0->vdev_tsd;
5231 zfs_dbgmsg("injecting fault to vdev %llu; maxfaults=%d",
5232 (long long)vd0->vdev_id, (int)maxfaults);
5234 if (vf != NULL && ztest_random(3) == 0) {
5235 (void) close(vf->vf_vnode->v_fd);
5236 vf->vf_vnode->v_fd = -1;
5237 } else if (ztest_random(2) == 0) {
5238 vd0->vdev_cant_read = B_TRUE;
5240 vd0->vdev_cant_write = B_TRUE;
5242 guid0 = vd0->vdev_guid;
5246 * Inject errors on an l2cache device.
5248 spa_aux_vdev_t *sav = &spa->spa_l2cache;
5250 if (sav->sav_count == 0) {
5251 spa_config_exit(spa, SCL_STATE, FTAG);
5252 rw_exit(&ztest_name_lock);
5255 vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
5256 guid0 = vd0->vdev_guid;
5257 (void) strcpy(path0, vd0->vdev_path);
5258 (void) strcpy(pathrand, vd0->vdev_path);
5262 maxfaults = INT_MAX; /* no limit on cache devices */
5265 spa_config_exit(spa, SCL_STATE, FTAG);
5266 rw_exit(&ztest_name_lock);
5269 * If we can tolerate two or more faults, or we're dealing
5270 * with a slog, randomly online/offline vd0.
5272 if ((maxfaults >= 2 || islog) && guid0 != 0) {
5273 if (ztest_random(10) < 6) {
5274 int flags = (ztest_random(2) == 0 ?
5275 ZFS_OFFLINE_TEMPORARY : 0);
5278 * We have to grab the zs_name_lock as writer to
5279 * prevent a race between offlining a slog and
5280 * destroying a dataset. Offlining the slog will
5281 * grab a reference on the dataset which may cause
5282 * dmu_objset_destroy() to fail with EBUSY thus
5283 * leaving the dataset in an inconsistent state.
5286 rw_enter(&ztest_name_lock, RW_WRITER);
5288 VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
5291 rw_exit(&ztest_name_lock);
5294 * Ideally we would like to be able to randomly
5295 * call vdev_[on|off]line without holding locks
5296 * to force unpredictable failures but the side
5297 * effects of vdev_[on|off]line prevent us from
5298 * doing so. We grab the ztest_vdev_lock here to
5299 * prevent a race between injection testing and
5302 mutex_enter(&ztest_vdev_lock);
5303 (void) vdev_online(spa, guid0, 0, NULL);
5304 mutex_exit(&ztest_vdev_lock);
5312 * We have at least single-fault tolerance, so inject data corruption.
5314 fd = open(pathrand, O_RDWR);
5316 if (fd == -1) /* we hit a gap in the device namespace */
5319 fsize = lseek(fd, 0, SEEK_END);
5321 while (--iters != 0) {
5323 * The offset must be chosen carefully to ensure that
5324 * we do not inject a given logical block with errors
5325 * on two different leaf devices, because ZFS can not
5326 * tolerate that (if maxfaults==1).
5328 * We divide each leaf into chunks of size
5329 * (# leaves * SPA_MAXBLOCKSIZE * 4). Within each chunk
5330 * there is a series of ranges to which we can inject errors.
5331 * Each range can accept errors on only a single leaf vdev.
5332 * The error injection ranges are separated by ranges
5333 * which we will not inject errors on any device (DMZs).
5334 * Each DMZ must be large enough such that a single block
5335 * can not straddle it, so that a single block can not be
5336 * a target in two different injection ranges (on different
5339 * For example, with 3 leaves, each chunk looks like:
5340 * 0 to 32M: injection range for leaf 0
5341 * 32M to 64M: DMZ - no injection allowed
5342 * 64M to 96M: injection range for leaf 1
5343 * 96M to 128M: DMZ - no injection allowed
5344 * 128M to 160M: injection range for leaf 2
5345 * 160M to 192M: DMZ - no injection allowed
5347 offset = ztest_random(fsize / (leaves << bshift)) *
5348 (leaves << bshift) + (leaf << bshift) +
5349 (ztest_random(1ULL << (bshift - 1)) & -8ULL);
5352 * Only allow damage to the labels at one end of the vdev.
5354 * If all labels are damaged, the device will be totally
5355 * inaccessible, which will result in loss of data,
5356 * because we also damage (parts of) the other side of
5359 * Additionally, we will always have both an even and an
5360 * odd label, so that we can handle crashes in the
5361 * middle of vdev_config_sync().
5363 if ((leaf & 1) == 0 && offset < VDEV_LABEL_START_SIZE)
5367 * The two end labels are stored at the "end" of the disk, but
5368 * the end of the disk (vdev_psize) is aligned to
5369 * sizeof (vdev_label_t).
5371 uint64_t psize = P2ALIGN(fsize, sizeof (vdev_label_t));
5372 if ((leaf & 1) == 1 &&
5373 offset + sizeof (bad) > psize - VDEV_LABEL_END_SIZE)
5376 mutex_enter(&ztest_vdev_lock);
5377 if (mirror_save != zs->zs_mirrors) {
5378 mutex_exit(&ztest_vdev_lock);
5383 if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
5384 fatal(1, "can't inject bad word at 0x%llx in %s",
5387 mutex_exit(&ztest_vdev_lock);
5389 if (ztest_opts.zo_verbose >= 7)
5390 (void) printf("injected bad word into %s,"
5391 " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
5398 * Verify that DDT repair works as expected.
5401 ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
5403 ztest_shared_t *zs = ztest_shared;
5404 spa_t *spa = ztest_spa;
5405 objset_t *os = zd->zd_os;
5407 uint64_t object, blocksize, txg, pattern, psize;
5408 enum zio_checksum checksum = spa_dedup_checksum(spa);
5413 int copies = 2 * ZIO_DEDUPDITTO_MIN;
5415 blocksize = ztest_random_blocksize();
5416 blocksize = MIN(blocksize, 2048); /* because we write so many */
5418 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0, 0);
5420 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
5424 * Take the name lock as writer to prevent anyone else from changing
5425 * the pool and dataset properies we need to maintain during this test.
5427 rw_enter(&ztest_name_lock, RW_WRITER);
5429 if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
5431 ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
5433 rw_exit(&ztest_name_lock);
5437 dmu_objset_stats_t dds;
5438 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
5439 dmu_objset_fast_stat(os, &dds);
5440 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
5442 object = od[0].od_object;
5443 blocksize = od[0].od_blocksize;
5444 pattern = zs->zs_guid ^ dds.dds_guid;
5446 ASSERT(object != 0);
5448 tx = dmu_tx_create(os);
5449 dmu_tx_hold_write(tx, object, 0, copies * blocksize);
5450 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
5452 rw_exit(&ztest_name_lock);
5457 * Write all the copies of our block.
5459 for (int i = 0; i < copies; i++) {
5460 uint64_t offset = i * blocksize;
5461 int error = dmu_buf_hold(os, object, offset, FTAG, &db,
5462 DMU_READ_NO_PREFETCH);
5464 fatal(B_FALSE, "dmu_buf_hold(%p, %llu, %llu) = %u",
5465 os, (long long)object, (long long) offset, error);
5467 ASSERT(db->db_offset == offset);
5468 ASSERT(db->db_size == blocksize);
5469 ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
5470 ztest_pattern_match(db->db_data, db->db_size, 0ULL));
5471 dmu_buf_will_fill(db, tx);
5472 ztest_pattern_set(db->db_data, db->db_size, pattern);
5473 dmu_buf_rele(db, FTAG);
5477 txg_wait_synced(spa_get_dsl(spa), txg);
5480 * Find out what block we got.
5482 VERIFY0(dmu_buf_hold(os, object, 0, FTAG, &db,
5483 DMU_READ_NO_PREFETCH));
5484 blk = *((dmu_buf_impl_t *)db)->db_blkptr;
5485 dmu_buf_rele(db, FTAG);
5488 * Damage the block. Dedup-ditto will save us when we read it later.
5490 psize = BP_GET_PSIZE(&blk);
5491 abd = abd_alloc_linear(psize, B_TRUE);
5492 ztest_pattern_set(abd_to_buf(abd), psize, ~pattern);
5494 (void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
5495 abd, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
5496 ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
5500 rw_exit(&ztest_name_lock);
5508 ztest_scrub(ztest_ds_t *zd, uint64_t id)
5510 spa_t *spa = ztest_spa;
5513 * Scrub in progress by device removal.
5515 if (ztest_device_removal_active)
5518 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5519 (void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
5520 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5524 * Change the guid for the pool.
5528 ztest_reguid(ztest_ds_t *zd, uint64_t id)
5530 spa_t *spa = ztest_spa;
5531 uint64_t orig, load;
5534 orig = spa_guid(spa);
5535 load = spa_load_guid(spa);
5537 rw_enter(&ztest_name_lock, RW_WRITER);
5538 error = spa_change_guid(spa);
5539 rw_exit(&ztest_name_lock);
5544 if (ztest_opts.zo_verbose >= 4) {
5545 (void) printf("Changed guid old %llu -> %llu\n",
5546 (u_longlong_t)orig, (u_longlong_t)spa_guid(spa));
5549 VERIFY3U(orig, !=, spa_guid(spa));
5550 VERIFY3U(load, ==, spa_load_guid(spa));
5554 * Rename the pool to a different name and then rename it back.
5558 ztest_spa_rename(ztest_ds_t *zd, uint64_t id)
5560 char *oldname, *newname;
5563 rw_enter(&ztest_name_lock, RW_WRITER);
5565 oldname = ztest_opts.zo_pool;
5566 newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
5567 (void) strcpy(newname, oldname);
5568 (void) strcat(newname, "_tmp");
5573 VERIFY3U(0, ==, spa_rename(oldname, newname));
5576 * Try to open it under the old name, which shouldn't exist
5578 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5581 * Open it under the new name and make sure it's still the same spa_t.
5583 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
5585 ASSERT(spa == ztest_spa);
5586 spa_close(spa, FTAG);
5589 * Rename it back to the original
5591 VERIFY3U(0, ==, spa_rename(newname, oldname));
5594 * Make sure it can still be opened
5596 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
5598 ASSERT(spa == ztest_spa);
5599 spa_close(spa, FTAG);
5601 umem_free(newname, strlen(newname) + 1);
5603 rw_exit(&ztest_name_lock);
5607 ztest_random_concrete_vdev_leaf(vdev_t *vd)
5612 if (vd->vdev_children == 0)
5615 vdev_t *eligible[vd->vdev_children];
5616 int eligible_idx = 0, i;
5617 for (i = 0; i < vd->vdev_children; i++) {
5618 vdev_t *cvd = vd->vdev_child[i];
5619 if (cvd->vdev_top->vdev_removing)
5621 if (cvd->vdev_children > 0 ||
5622 (vdev_is_concrete(cvd) && !cvd->vdev_detached)) {
5623 eligible[eligible_idx++] = cvd;
5626 VERIFY(eligible_idx > 0);
5628 uint64_t child_no = ztest_random(eligible_idx);
5629 return (ztest_random_concrete_vdev_leaf(eligible[child_no]));
5634 ztest_initialize(ztest_ds_t *zd, uint64_t id)
5636 spa_t *spa = ztest_spa;
5639 mutex_enter(&ztest_vdev_lock);
5641 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
5643 /* Random leaf vdev */
5644 vdev_t *rand_vd = ztest_random_concrete_vdev_leaf(spa->spa_root_vdev);
5645 if (rand_vd == NULL) {
5646 spa_config_exit(spa, SCL_VDEV, FTAG);
5647 mutex_exit(&ztest_vdev_lock);
5652 * The random vdev we've selected may change as soon as we
5653 * drop the spa_config_lock. We create local copies of things
5654 * we're interested in.
5656 uint64_t guid = rand_vd->vdev_guid;
5657 char *path = strdup(rand_vd->vdev_path);
5658 boolean_t active = rand_vd->vdev_initialize_thread != NULL;
5660 zfs_dbgmsg("vd %p, guid %llu", rand_vd, guid);
5661 spa_config_exit(spa, SCL_VDEV, FTAG);
5663 uint64_t cmd = ztest_random(POOL_INITIALIZE_FUNCS);
5664 error = spa_vdev_initialize(spa, guid, cmd);
5666 case POOL_INITIALIZE_CANCEL:
5667 if (ztest_opts.zo_verbose >= 4) {
5668 (void) printf("Cancel initialize %s", path);
5670 (void) printf(" failed (no initialize active)");
5671 (void) printf("\n");
5674 case POOL_INITIALIZE_DO:
5675 if (ztest_opts.zo_verbose >= 4) {
5676 (void) printf("Start initialize %s", path);
5677 if (active && error == 0)
5678 (void) printf(" failed (already active)");
5679 else if (error != 0)
5680 (void) printf(" failed (error %d)", error);
5681 (void) printf("\n");
5684 case POOL_INITIALIZE_SUSPEND:
5685 if (ztest_opts.zo_verbose >= 4) {
5686 (void) printf("Suspend initialize %s", path);
5688 (void) printf(" failed (no initialize active)");
5689 (void) printf("\n");
5694 mutex_exit(&ztest_vdev_lock);
5698 * Verify pool integrity by running zdb.
5701 ztest_run_zdb(char *pool)
5704 char zdb[MAXPATHLEN + MAXNAMELEN + 20];
5712 strlcpy(zdb, "/usr/bin/ztest", sizeof(zdb));
5714 /* zdb lives in /usr/sbin, while ztest lives in /usr/bin */
5715 bin = strstr(zdb, "/usr/bin/");
5716 ztest = strstr(bin, "/ztest");
5718 isalen = ztest - isa;
5722 "/usr/sbin%.*s/zdb -bcc%s%s -G -d -U %s %s",
5725 ztest_opts.zo_verbose >= 3 ? "s" : "",
5726 ztest_opts.zo_verbose >= 4 ? "v" : "",
5731 if (ztest_opts.zo_verbose >= 5)
5732 (void) printf("Executing %s\n", strstr(zdb, "zdb "));
5734 fp = popen(zdb, "r");
5737 while (fgets(zbuf, sizeof (zbuf), fp) != NULL)
5738 if (ztest_opts.zo_verbose >= 3)
5739 (void) printf("%s", zbuf);
5741 status = pclose(fp);
5746 ztest_dump_core = 0;
5747 if (WIFEXITED(status))
5748 fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
5750 fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
5754 ztest_walk_pool_directory(char *header)
5758 if (ztest_opts.zo_verbose >= 6)
5759 (void) printf("%s\n", header);
5761 mutex_enter(&spa_namespace_lock);
5762 while ((spa = spa_next(spa)) != NULL)
5763 if (ztest_opts.zo_verbose >= 6)
5764 (void) printf("\t%s\n", spa_name(spa));
5765 mutex_exit(&spa_namespace_lock);
5769 ztest_spa_import_export(char *oldname, char *newname)
5771 nvlist_t *config, *newconfig;
5776 if (ztest_opts.zo_verbose >= 4) {
5777 (void) printf("import/export: old = %s, new = %s\n",
5782 * Clean up from previous runs.
5784 (void) spa_destroy(newname);
5787 * Get the pool's configuration and guid.
5789 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
5792 * Kick off a scrub to tickle scrub/export races.
5794 if (ztest_random(2) == 0)
5795 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5797 pool_guid = spa_guid(spa);
5798 spa_close(spa, FTAG);
5800 ztest_walk_pool_directory("pools before export");
5805 VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
5807 ztest_walk_pool_directory("pools after export");
5812 newconfig = spa_tryimport(config);
5813 ASSERT(newconfig != NULL);
5814 nvlist_free(newconfig);
5817 * Import it under the new name.
5819 error = spa_import(newname, config, NULL, 0);
5821 dump_nvlist(config, 0);
5822 fatal(B_FALSE, "couldn't import pool %s as %s: error %u",
5823 oldname, newname, error);
5826 ztest_walk_pool_directory("pools after import");
5829 * Try to import it again -- should fail with EEXIST.
5831 VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL, 0));
5834 * Try to import it under a different name -- should fail with EEXIST.
5836 VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL, 0));
5839 * Verify that the pool is no longer visible under the old name.
5841 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5844 * Verify that we can open and close the pool using the new name.
5846 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
5847 ASSERT(pool_guid == spa_guid(spa));
5848 spa_close(spa, FTAG);
5850 nvlist_free(config);
5854 ztest_resume(spa_t *spa)
5856 if (spa_suspended(spa) && ztest_opts.zo_verbose >= 6)
5857 (void) printf("resuming from suspended state\n");
5858 spa_vdev_state_enter(spa, SCL_NONE);
5859 vdev_clear(spa, NULL);
5860 (void) spa_vdev_state_exit(spa, NULL, 0);
5861 (void) zio_resume(spa);
5865 ztest_resume_thread(void *arg)
5869 while (!ztest_exiting) {
5870 if (spa_suspended(spa))
5872 (void) poll(NULL, 0, 100);
5875 * Periodically change the zfs_compressed_arc_enabled setting.
5877 if (ztest_random(10) == 0)
5878 zfs_compressed_arc_enabled = ztest_random(2);
5881 * Periodically change the zfs_abd_scatter_enabled setting.
5883 if (ztest_random(10) == 0)
5884 zfs_abd_scatter_enabled = ztest_random(2);
5890 ztest_deadman_thread(void *arg)
5892 ztest_shared_t *zs = arg;
5893 spa_t *spa = ztest_spa;
5894 hrtime_t delta, total = 0;
5897 delta = zs->zs_thread_stop - zs->zs_thread_start +
5898 MSEC2NSEC(zfs_deadman_synctime_ms);
5900 (void) poll(NULL, 0, (int)NSEC2MSEC(delta));
5903 * If the pool is suspended then fail immediately. Otherwise,
5904 * check to see if the pool is making any progress. If
5905 * vdev_deadman() discovers that there hasn't been any recent
5906 * I/Os then it will end up aborting the tests.
5908 if (spa_suspended(spa) || spa->spa_root_vdev == NULL) {
5909 fatal(0, "aborting test after %llu seconds because "
5910 "pool has transitioned to a suspended state.",
5911 zfs_deadman_synctime_ms / 1000);
5914 vdev_deadman(spa->spa_root_vdev);
5916 total += zfs_deadman_synctime_ms/1000;
5917 (void) printf("ztest has been running for %lld seconds\n",
5923 ztest_execute(int test, ztest_info_t *zi, uint64_t id)
5925 ztest_ds_t *zd = &ztest_ds[id % ztest_opts.zo_datasets];
5926 ztest_shared_callstate_t *zc = ZTEST_GET_SHARED_CALLSTATE(test);
5927 hrtime_t functime = gethrtime();
5929 for (int i = 0; i < zi->zi_iters; i++)
5930 zi->zi_func(zd, id);
5932 functime = gethrtime() - functime;
5934 atomic_add_64(&zc->zc_count, 1);
5935 atomic_add_64(&zc->zc_time, functime);
5937 if (ztest_opts.zo_verbose >= 4) {
5939 (void) dladdr((void *)zi->zi_func, &dli);
5940 (void) printf("%6.2f sec in %s\n",
5941 (double)functime / NANOSEC, dli.dli_sname);
5946 ztest_thread(void *arg)
5949 uint64_t id = (uintptr_t)arg;
5950 ztest_shared_t *zs = ztest_shared;
5954 ztest_shared_callstate_t *zc;
5956 while ((now = gethrtime()) < zs->zs_thread_stop) {
5958 * See if it's time to force a crash.
5960 if (now > zs->zs_thread_kill)
5964 * If we're getting ENOSPC with some regularity, stop.
5966 if (zs->zs_enospc_count > 10)
5970 * Pick a random function to execute.
5972 rand = ztest_random(ZTEST_FUNCS);
5973 zi = &ztest_info[rand];
5974 zc = ZTEST_GET_SHARED_CALLSTATE(rand);
5975 call_next = zc->zc_next;
5977 if (now >= call_next &&
5978 atomic_cas_64(&zc->zc_next, call_next, call_next +
5979 ztest_random(2 * zi->zi_interval[0] + 1)) == call_next) {
5980 ztest_execute(rand, zi, id);
5988 ztest_dataset_name(char *dsname, char *pool, int d)
5990 (void) snprintf(dsname, ZFS_MAX_DATASET_NAME_LEN, "%s/ds_%d", pool, d);
5994 ztest_dataset_destroy(int d)
5996 char name[ZFS_MAX_DATASET_NAME_LEN];
5998 ztest_dataset_name(name, ztest_opts.zo_pool, d);
6000 if (ztest_opts.zo_verbose >= 3)
6001 (void) printf("Destroying %s to free up space\n", name);
6004 * Cleanup any non-standard clones and snapshots. In general,
6005 * ztest thread t operates on dataset (t % zopt_datasets),
6006 * so there may be more than one thing to clean up.
6008 for (int t = d; t < ztest_opts.zo_threads;
6009 t += ztest_opts.zo_datasets) {
6010 ztest_dsl_dataset_cleanup(name, t);
6013 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
6014 DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
6018 ztest_dataset_dirobj_verify(ztest_ds_t *zd)
6020 uint64_t usedobjs, dirobjs, scratch;
6023 * ZTEST_DIROBJ is the object directory for the entire dataset.
6024 * Therefore, the number of objects in use should equal the
6025 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
6026 * If not, we have an object leak.
6028 * Note that we can only check this in ztest_dataset_open(),
6029 * when the open-context and syncing-context values agree.
6030 * That's because zap_count() returns the open-context value,
6031 * while dmu_objset_space() returns the rootbp fill count.
6033 VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
6034 dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
6035 ASSERT3U(dirobjs + 1, ==, usedobjs);
6039 ztest_dataset_open(int d)
6041 ztest_ds_t *zd = &ztest_ds[d];
6042 uint64_t committed_seq = ZTEST_GET_SHARED_DS(d)->zd_seq;
6045 char name[ZFS_MAX_DATASET_NAME_LEN];
6048 ztest_dataset_name(name, ztest_opts.zo_pool, d);
6050 rw_enter(&ztest_name_lock, RW_READER);
6052 error = ztest_dataset_create(name);
6053 if (error == ENOSPC) {
6054 rw_exit(&ztest_name_lock);
6055 ztest_record_enospc(FTAG);
6058 ASSERT(error == 0 || error == EEXIST);
6060 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, zd, &os));
6061 rw_exit(&ztest_name_lock);
6063 ztest_zd_init(zd, ZTEST_GET_SHARED_DS(d), os);
6065 zilog = zd->zd_zilog;
6067 if (zilog->zl_header->zh_claim_lr_seq != 0 &&
6068 zilog->zl_header->zh_claim_lr_seq < committed_seq)
6069 fatal(0, "missing log records: claimed %llu < committed %llu",
6070 zilog->zl_header->zh_claim_lr_seq, committed_seq);
6072 ztest_dataset_dirobj_verify(zd);
6074 zil_replay(os, zd, ztest_replay_vector);
6076 ztest_dataset_dirobj_verify(zd);
6078 if (ztest_opts.zo_verbose >= 6)
6079 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
6081 (u_longlong_t)zilog->zl_parse_blk_count,
6082 (u_longlong_t)zilog->zl_parse_lr_count,
6083 (u_longlong_t)zilog->zl_replaying_seq);
6085 zilog = zil_open(os, ztest_get_data);
6087 if (zilog->zl_replaying_seq != 0 &&
6088 zilog->zl_replaying_seq < committed_seq)
6089 fatal(0, "missing log records: replayed %llu < committed %llu",
6090 zilog->zl_replaying_seq, committed_seq);
6096 ztest_dataset_close(int d)
6098 ztest_ds_t *zd = &ztest_ds[d];
6100 zil_close(zd->zd_zilog);
6101 dmu_objset_disown(zd->zd_os, zd);
6107 * Kick off threads to run tests on all datasets in parallel.
6110 ztest_run(ztest_shared_t *zs)
6115 thread_t resume_tid;
6118 ztest_exiting = B_FALSE;
6121 * Initialize parent/child shared state.
6123 mutex_init(&ztest_checkpoint_lock, NULL, USYNC_THREAD, NULL);
6124 mutex_init(&ztest_vdev_lock, NULL, USYNC_THREAD, NULL);
6125 rw_init(&ztest_name_lock, NULL, USYNC_THREAD, NULL);
6127 zs->zs_thread_start = gethrtime();
6128 zs->zs_thread_stop =
6129 zs->zs_thread_start + ztest_opts.zo_passtime * NANOSEC;
6130 zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
6131 zs->zs_thread_kill = zs->zs_thread_stop;
6132 if (ztest_random(100) < ztest_opts.zo_killrate) {
6133 zs->zs_thread_kill -=
6134 ztest_random(ztest_opts.zo_passtime * NANOSEC);
6137 mutex_init(&zcl.zcl_callbacks_lock, NULL, USYNC_THREAD, NULL);
6139 list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
6140 offsetof(ztest_cb_data_t, zcd_node));
6145 kernel_init(FREAD | FWRITE);
6146 VERIFY0(spa_open(ztest_opts.zo_pool, &spa, FTAG));
6147 metaslab_preload_limit = ztest_random(20) + 1;
6150 dmu_objset_stats_t dds;
6151 VERIFY0(dmu_objset_own(ztest_opts.zo_pool,
6152 DMU_OST_ANY, B_TRUE, FTAG, &os));
6153 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
6154 dmu_objset_fast_stat(os, &dds);
6155 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
6156 zs->zs_guid = dds.dds_guid;
6157 dmu_objset_disown(os, FTAG);
6159 spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
6162 * We don't expect the pool to suspend unless maxfaults == 0,
6163 * in which case ztest_fault_inject() temporarily takes away
6164 * the only valid replica.
6166 if (MAXFAULTS() == 0)
6167 spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
6169 spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
6172 * Create a thread to periodically resume suspended I/O.
6174 VERIFY(thr_create(0, 0, ztest_resume_thread, spa, THR_BOUND,
6178 * Create a deadman thread to abort() if we hang.
6180 VERIFY(thr_create(0, 0, ztest_deadman_thread, zs, THR_BOUND,
6184 * Verify that we can safely inquire about any object,
6185 * whether it's allocated or not. To make it interesting,
6186 * we probe a 5-wide window around each power of two.
6187 * This hits all edge cases, including zero and the max.
6189 for (int t = 0; t < 64; t++) {
6190 for (int d = -5; d <= 5; d++) {
6191 error = dmu_object_info(spa->spa_meta_objset,
6192 (1ULL << t) + d, NULL);
6193 ASSERT(error == 0 || error == ENOENT ||
6199 * If we got any ENOSPC errors on the previous run, destroy something.
6201 if (zs->zs_enospc_count != 0) {
6202 int d = ztest_random(ztest_opts.zo_datasets);
6203 ztest_dataset_destroy(d);
6205 zs->zs_enospc_count = 0;
6207 tid = umem_zalloc(ztest_opts.zo_threads * sizeof (thread_t),
6210 if (ztest_opts.zo_verbose >= 4)
6211 (void) printf("starting main threads...\n");
6214 * Kick off all the tests that run in parallel.
6216 for (int t = 0; t < ztest_opts.zo_threads; t++) {
6217 if (t < ztest_opts.zo_datasets &&
6218 ztest_dataset_open(t) != 0)
6220 VERIFY(thr_create(0, 0, ztest_thread, (void *)(uintptr_t)t,
6221 THR_BOUND, &tid[t]) == 0);
6225 * Wait for all of the tests to complete. We go in reverse order
6226 * so we don't close datasets while threads are still using them.
6228 for (int t = ztest_opts.zo_threads - 1; t >= 0; t--) {
6229 VERIFY(thr_join(tid[t], NULL, NULL) == 0);
6230 if (t < ztest_opts.zo_datasets)
6231 ztest_dataset_close(t);
6234 txg_wait_synced(spa_get_dsl(spa), 0);
6236 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
6237 zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
6238 zfs_dbgmsg_print(FTAG);
6240 umem_free(tid, ztest_opts.zo_threads * sizeof (thread_t));
6242 /* Kill the resume thread */
6243 ztest_exiting = B_TRUE;
6244 VERIFY(thr_join(resume_tid, NULL, NULL) == 0);
6248 * Right before closing the pool, kick off a bunch of async I/O;
6249 * spa_close() should wait for it to complete.
6251 for (uint64_t object = 1; object < 50; object++) {
6252 dmu_prefetch(spa->spa_meta_objset, object, 0, 0, 1ULL << 20,
6253 ZIO_PRIORITY_SYNC_READ);
6256 spa_close(spa, FTAG);
6259 * Verify that we can loop over all pools.
6261 mutex_enter(&spa_namespace_lock);
6262 for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
6263 if (ztest_opts.zo_verbose > 3)
6264 (void) printf("spa_next: found %s\n", spa_name(spa));
6265 mutex_exit(&spa_namespace_lock);
6268 * Verify that we can export the pool and reimport it under a
6271 if (ztest_random(2) == 0) {
6272 char name[ZFS_MAX_DATASET_NAME_LEN];
6273 (void) snprintf(name, sizeof (name), "%s_import",
6274 ztest_opts.zo_pool);
6275 ztest_spa_import_export(ztest_opts.zo_pool, name);
6276 ztest_spa_import_export(name, ztest_opts.zo_pool);
6281 list_destroy(&zcl.zcl_callbacks);
6283 mutex_destroy(&zcl.zcl_callbacks_lock);
6285 rw_destroy(&ztest_name_lock);
6286 mutex_destroy(&ztest_vdev_lock);
6287 mutex_destroy(&ztest_checkpoint_lock);
6293 ztest_ds_t *zd = &ztest_ds[0];
6297 if (ztest_opts.zo_verbose >= 3)
6298 (void) printf("testing spa_freeze()...\n");
6300 kernel_init(FREAD | FWRITE);
6301 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6302 VERIFY3U(0, ==, ztest_dataset_open(0));
6306 * Force the first log block to be transactionally allocated.
6307 * We have to do this before we freeze the pool -- otherwise
6308 * the log chain won't be anchored.
6310 while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
6311 ztest_dmu_object_alloc_free(zd, 0);
6312 zil_commit(zd->zd_zilog, 0);
6315 txg_wait_synced(spa_get_dsl(spa), 0);
6318 * Freeze the pool. This stops spa_sync() from doing anything,
6319 * so that the only way to record changes from now on is the ZIL.
6324 * Because it is hard to predict how much space a write will actually
6325 * require beforehand, we leave ourselves some fudge space to write over
6328 uint64_t capacity = metaslab_class_get_space(spa_normal_class(spa)) / 2;
6331 * Run tests that generate log records but don't alter the pool config
6332 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
6333 * We do a txg_wait_synced() after each iteration to force the txg
6334 * to increase well beyond the last synced value in the uberblock.
6335 * The ZIL should be OK with that.
6337 * Run a random number of times less than zo_maxloops and ensure we do
6338 * not run out of space on the pool.
6340 while (ztest_random(10) != 0 &&
6341 numloops++ < ztest_opts.zo_maxloops &&
6342 metaslab_class_get_alloc(spa_normal_class(spa)) < capacity) {
6344 ztest_od_init(&od, 0, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
6345 VERIFY0(ztest_object_init(zd, &od, sizeof (od), B_FALSE));
6346 ztest_io(zd, od.od_object,
6347 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
6348 txg_wait_synced(spa_get_dsl(spa), 0);
6352 * Commit all of the changes we just generated.
6354 zil_commit(zd->zd_zilog, 0);
6355 txg_wait_synced(spa_get_dsl(spa), 0);
6358 * Close our dataset and close the pool.
6360 ztest_dataset_close(0);
6361 spa_close(spa, FTAG);
6365 * Open and close the pool and dataset to induce log replay.
6367 kernel_init(FREAD | FWRITE);
6368 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6369 ASSERT(spa_freeze_txg(spa) == UINT64_MAX);
6370 VERIFY3U(0, ==, ztest_dataset_open(0));
6371 ztest_dataset_close(0);
6374 txg_wait_synced(spa_get_dsl(spa), 0);
6375 ztest_reguid(NULL, 0);
6377 spa_close(spa, FTAG);
6382 print_time(hrtime_t t, char *timebuf)
6384 hrtime_t s = t / NANOSEC;
6385 hrtime_t m = s / 60;
6386 hrtime_t h = m / 60;
6387 hrtime_t d = h / 24;
6396 (void) sprintf(timebuf,
6397 "%llud%02lluh%02llum%02llus", d, h, m, s);
6399 (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
6401 (void) sprintf(timebuf, "%llum%02llus", m, s);
6403 (void) sprintf(timebuf, "%llus", s);
6411 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
6412 if (ztest_random(2) == 0)
6414 VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
6420 * Create a storage pool with the given name and initial vdev size.
6421 * Then test spa_freeze() functionality.
6424 ztest_init(ztest_shared_t *zs)
6427 nvlist_t *nvroot, *props;
6429 mutex_init(&ztest_vdev_lock, NULL, USYNC_THREAD, NULL);
6430 mutex_init(&ztest_checkpoint_lock, NULL, USYNC_THREAD, NULL);
6431 rw_init(&ztest_name_lock, NULL, USYNC_THREAD, NULL);
6433 kernel_init(FREAD | FWRITE);
6436 * Create the storage pool.
6438 (void) spa_destroy(ztest_opts.zo_pool);
6439 ztest_shared->zs_vdev_next_leaf = 0;
6441 zs->zs_mirrors = ztest_opts.zo_mirrors;
6442 nvroot = make_vdev_root(NULL, NULL, NULL, ztest_opts.zo_vdev_size, 0,
6443 0, ztest_opts.zo_raidz, zs->zs_mirrors, 1);
6444 props = make_random_props();
6445 for (int i = 0; i < SPA_FEATURES; i++) {
6447 (void) snprintf(buf, sizeof (buf), "feature@%s",
6448 spa_feature_table[i].fi_uname);
6449 VERIFY3U(0, ==, nvlist_add_uint64(props, buf, 0));
6451 VERIFY3U(0, ==, spa_create(ztest_opts.zo_pool, nvroot, props, NULL));
6452 nvlist_free(nvroot);
6455 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6456 zs->zs_metaslab_sz =
6457 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
6459 spa_close(spa, FTAG);
6463 ztest_run_zdb(ztest_opts.zo_pool);
6467 ztest_run_zdb(ztest_opts.zo_pool);
6469 rw_destroy(&ztest_name_lock);
6470 mutex_destroy(&ztest_vdev_lock);
6471 mutex_destroy(&ztest_checkpoint_lock);
6477 static char ztest_name_data[] = "/tmp/ztest.data.XXXXXX";
6479 ztest_fd_data = mkstemp(ztest_name_data);
6480 ASSERT3S(ztest_fd_data, >=, 0);
6481 (void) unlink(ztest_name_data);
6486 shared_data_size(ztest_shared_hdr_t *hdr)
6490 size = hdr->zh_hdr_size;
6491 size += hdr->zh_opts_size;
6492 size += hdr->zh_size;
6493 size += hdr->zh_stats_size * hdr->zh_stats_count;
6494 size += hdr->zh_ds_size * hdr->zh_ds_count;
6503 ztest_shared_hdr_t *hdr;
6505 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6506 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6507 ASSERT(hdr != MAP_FAILED);
6509 VERIFY3U(0, ==, ftruncate(ztest_fd_data, sizeof (ztest_shared_hdr_t)));
6511 hdr->zh_hdr_size = sizeof (ztest_shared_hdr_t);
6512 hdr->zh_opts_size = sizeof (ztest_shared_opts_t);
6513 hdr->zh_size = sizeof (ztest_shared_t);
6514 hdr->zh_stats_size = sizeof (ztest_shared_callstate_t);
6515 hdr->zh_stats_count = ZTEST_FUNCS;
6516 hdr->zh_ds_size = sizeof (ztest_shared_ds_t);
6517 hdr->zh_ds_count = ztest_opts.zo_datasets;
6519 size = shared_data_size(hdr);
6520 VERIFY3U(0, ==, ftruncate(ztest_fd_data, size));
6522 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6529 ztest_shared_hdr_t *hdr;
6532 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6533 PROT_READ, MAP_SHARED, ztest_fd_data, 0);
6534 ASSERT(hdr != MAP_FAILED);
6536 size = shared_data_size(hdr);
6538 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6539 hdr = ztest_shared_hdr = (void *)mmap(0, P2ROUNDUP(size, getpagesize()),
6540 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6541 ASSERT(hdr != MAP_FAILED);
6542 buf = (uint8_t *)hdr;
6544 offset = hdr->zh_hdr_size;
6545 ztest_shared_opts = (void *)&buf[offset];
6546 offset += hdr->zh_opts_size;
6547 ztest_shared = (void *)&buf[offset];
6548 offset += hdr->zh_size;
6549 ztest_shared_callstate = (void *)&buf[offset];
6550 offset += hdr->zh_stats_size * hdr->zh_stats_count;
6551 ztest_shared_ds = (void *)&buf[offset];
6555 exec_child(char *cmd, char *libpath, boolean_t ignorekill, int *statusp)
6559 char *cmdbuf = NULL;
6564 cmdbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
6565 (void) strlcpy(cmdbuf, getexecname(), MAXPATHLEN);
6570 fatal(1, "fork failed");
6572 if (pid == 0) { /* child */
6573 char *emptyargv[2] = { cmd, NULL };
6574 char fd_data_str[12];
6576 struct rlimit rl = { 1024, 1024 };
6577 (void) setrlimit(RLIMIT_NOFILE, &rl);
6579 (void) close(ztest_fd_rand);
6581 snprintf(fd_data_str, 12, "%d", ztest_fd_data));
6582 VERIFY0(setenv("ZTEST_FD_DATA", fd_data_str, 1));
6584 (void) enable_extended_FILE_stdio(-1, -1);
6585 if (libpath != NULL)
6586 VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath, 1));
6588 (void) execv(cmd, emptyargv);
6590 (void) execvp(cmd, emptyargv);
6592 ztest_dump_core = B_FALSE;
6593 fatal(B_TRUE, "exec failed: %s", cmd);
6596 if (cmdbuf != NULL) {
6597 umem_free(cmdbuf, MAXPATHLEN);
6601 while (waitpid(pid, &status, 0) != pid)
6603 if (statusp != NULL)
6606 if (WIFEXITED(status)) {
6607 if (WEXITSTATUS(status) != 0) {
6608 (void) fprintf(stderr, "child exited with code %d\n",
6609 WEXITSTATUS(status));
6613 } else if (WIFSIGNALED(status)) {
6614 if (!ignorekill || WTERMSIG(status) != SIGKILL) {
6615 (void) fprintf(stderr, "child died with signal %d\n",
6621 (void) fprintf(stderr, "something strange happened to child\n");
6628 ztest_run_init(void)
6630 ztest_shared_t *zs = ztest_shared;
6632 ASSERT(ztest_opts.zo_init != 0);
6635 * Blow away any existing copy of zpool.cache
6637 (void) remove(spa_config_path);
6640 * Create and initialize our storage pool.
6642 for (int i = 1; i <= ztest_opts.zo_init; i++) {
6643 bzero(zs, sizeof (ztest_shared_t));
6644 if (ztest_opts.zo_verbose >= 3 &&
6645 ztest_opts.zo_init != 1) {
6646 (void) printf("ztest_init(), pass %d\n", i);
6653 main(int argc, char **argv)
6661 ztest_shared_callstate_t *zc;
6663 char numbuf[NN_NUMBUF_SZ];
6667 char *fd_data_str = getenv("ZTEST_FD_DATA");
6669 (void) setvbuf(stdout, NULL, _IOLBF, 0);
6671 dprintf_setup(&argc, argv);
6672 zfs_deadman_synctime_ms = 300000;
6674 * As two-word space map entries may not come up often (especially
6675 * if pool and vdev sizes are small) we want to force at least some
6676 * of them so the feature get tested.
6678 zfs_force_some_double_word_sm_entries = B_TRUE;
6680 ztest_fd_rand = open("/dev/urandom", O_RDONLY);
6681 ASSERT3S(ztest_fd_rand, >=, 0);
6684 process_options(argc, argv);
6689 bcopy(&ztest_opts, ztest_shared_opts,
6690 sizeof (*ztest_shared_opts));
6692 ztest_fd_data = atoi(fd_data_str);
6694 bcopy(ztest_shared_opts, &ztest_opts, sizeof (ztest_opts));
6696 ASSERT3U(ztest_opts.zo_datasets, ==, ztest_shared_hdr->zh_ds_count);
6698 /* Override location of zpool.cache */
6699 VERIFY3U(asprintf((char **)&spa_config_path, "%s/zpool.cache",
6700 ztest_opts.zo_dir), !=, -1);
6702 ztest_ds = umem_alloc(ztest_opts.zo_datasets * sizeof (ztest_ds_t),
6707 metaslab_force_ganging = ztest_opts.zo_metaslab_force_ganging;
6708 metaslab_df_alloc_threshold =
6709 zs->zs_metaslab_df_alloc_threshold;
6718 hasalt = (strlen(ztest_opts.zo_alt_ztest) != 0);
6720 if (ztest_opts.zo_verbose >= 1) {
6721 (void) printf("%llu vdevs, %d datasets, %d threads,"
6722 " %llu seconds...\n",
6723 (u_longlong_t)ztest_opts.zo_vdevs,
6724 ztest_opts.zo_datasets,
6725 ztest_opts.zo_threads,
6726 (u_longlong_t)ztest_opts.zo_time);
6729 cmd = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
6730 (void) strlcpy(cmd, getexecname(), MAXNAMELEN);
6732 zs->zs_do_init = B_TRUE;
6733 if (strlen(ztest_opts.zo_alt_ztest) != 0) {
6734 if (ztest_opts.zo_verbose >= 1) {
6735 (void) printf("Executing older ztest for "
6736 "initialization: %s\n", ztest_opts.zo_alt_ztest);
6738 VERIFY(!exec_child(ztest_opts.zo_alt_ztest,
6739 ztest_opts.zo_alt_libpath, B_FALSE, NULL));
6741 VERIFY(!exec_child(NULL, NULL, B_FALSE, NULL));
6743 zs->zs_do_init = B_FALSE;
6745 zs->zs_proc_start = gethrtime();
6746 zs->zs_proc_stop = zs->zs_proc_start + ztest_opts.zo_time * NANOSEC;
6748 for (int f = 0; f < ZTEST_FUNCS; f++) {
6749 zi = &ztest_info[f];
6750 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6751 if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
6752 zc->zc_next = UINT64_MAX;
6754 zc->zc_next = zs->zs_proc_start +
6755 ztest_random(2 * zi->zi_interval[0] + 1);
6759 * Run the tests in a loop. These tests include fault injection
6760 * to verify that self-healing data works, and forced crashes
6761 * to verify that we never lose on-disk consistency.
6763 while (gethrtime() < zs->zs_proc_stop) {
6768 * Initialize the workload counters for each function.
6770 for (int f = 0; f < ZTEST_FUNCS; f++) {
6771 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6776 /* Set the allocation switch size */
6777 zs->zs_metaslab_df_alloc_threshold =
6778 ztest_random(zs->zs_metaslab_sz / 4) + 1;
6780 if (!hasalt || ztest_random(2) == 0) {
6781 if (hasalt && ztest_opts.zo_verbose >= 1) {
6782 (void) printf("Executing newer ztest: %s\n",
6786 killed = exec_child(cmd, NULL, B_TRUE, &status);
6788 if (hasalt && ztest_opts.zo_verbose >= 1) {
6789 (void) printf("Executing older ztest: %s\n",
6790 ztest_opts.zo_alt_ztest);
6793 killed = exec_child(ztest_opts.zo_alt_ztest,
6794 ztest_opts.zo_alt_libpath, B_TRUE, &status);
6801 if (ztest_opts.zo_verbose >= 1) {
6802 hrtime_t now = gethrtime();
6804 now = MIN(now, zs->zs_proc_stop);
6805 print_time(zs->zs_proc_stop - now, timebuf);
6806 nicenum(zs->zs_space, numbuf, sizeof (numbuf));
6808 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
6809 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
6811 WIFEXITED(status) ? "Complete" : "SIGKILL",
6812 (u_longlong_t)zs->zs_enospc_count,
6813 100.0 * zs->zs_alloc / zs->zs_space,
6815 100.0 * (now - zs->zs_proc_start) /
6816 (ztest_opts.zo_time * NANOSEC), timebuf);
6819 if (ztest_opts.zo_verbose >= 2) {
6820 (void) printf("\nWorkload summary:\n\n");
6821 (void) printf("%7s %9s %s\n",
6822 "Calls", "Time", "Function");
6823 (void) printf("%7s %9s %s\n",
6824 "-----", "----", "--------");
6825 for (int f = 0; f < ZTEST_FUNCS; f++) {
6828 zi = &ztest_info[f];
6829 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6830 print_time(zc->zc_time, timebuf);
6831 (void) dladdr((void *)zi->zi_func, &dli);
6832 (void) printf("%7llu %9s %s\n",
6833 (u_longlong_t)zc->zc_count, timebuf,
6836 (void) printf("\n");
6840 * It's possible that we killed a child during a rename test,
6841 * in which case we'll have a 'ztest_tmp' pool lying around
6842 * instead of 'ztest'. Do a blind rename in case this happened.
6845 if (spa_open(ztest_opts.zo_pool, &spa, FTAG) == 0) {
6846 spa_close(spa, FTAG);
6848 char tmpname[ZFS_MAX_DATASET_NAME_LEN];
6850 kernel_init(FREAD | FWRITE);
6851 (void) snprintf(tmpname, sizeof (tmpname), "%s_tmp",
6852 ztest_opts.zo_pool);
6853 (void) spa_rename(tmpname, ztest_opts.zo_pool);
6857 ztest_run_zdb(ztest_opts.zo_pool);
6860 if (ztest_opts.zo_verbose >= 1) {
6862 (void) printf("%d runs of older ztest: %s\n", older,
6863 ztest_opts.zo_alt_ztest);
6864 (void) printf("%d runs of newer ztest: %s\n", newer,
6867 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
6868 kills, iters - kills, (100.0 * kills) / MAX(1, iters));
6871 umem_free(cmd, MAXNAMELEN);