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) 2013 by Delphix. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2013 Steven Hartland. All rights reserved.
29 * The objective of this program is to provide a DMU/ZAP/SPA stress test
30 * that runs entirely in userland, is easy to use, and easy to extend.
32 * The overall design of the ztest program is as follows:
34 * (1) For each major functional area (e.g. adding vdevs to a pool,
35 * creating and destroying datasets, reading and writing objects, etc)
36 * we have a simple routine to test that functionality. These
37 * individual routines do not have to do anything "stressful".
39 * (2) We turn these simple functionality tests into a stress test by
40 * running them all in parallel, with as many threads as desired,
41 * and spread across as many datasets, objects, and vdevs as desired.
43 * (3) While all this is happening, we inject faults into the pool to
44 * verify that self-healing data really works.
46 * (4) Every time we open a dataset, we change its checksum and compression
47 * functions. Thus even individual objects vary from block to block
48 * in which checksum they use and whether they're compressed.
50 * (5) To verify that we never lose on-disk consistency after a crash,
51 * we run the entire test in a child of the main process.
52 * At random times, the child self-immolates with a SIGKILL.
53 * This is the software equivalent of pulling the power cord.
54 * The parent then runs the test again, using the existing
55 * storage pool, as many times as desired. If backwards compatability
56 * testing is enabled ztest will sometimes run the "older" version
57 * of ztest after a SIGKILL.
59 * (6) To verify that we don't have future leaks or temporal incursions,
60 * many of the functional tests record the transaction group number
61 * as part of their data. When reading old data, they verify that
62 * the transaction group number is less than the current, open txg.
63 * If you add a new test, please do this if applicable.
65 * (7) Threads are created with a reduced stack size, for sanity checking.
66 * Therefore, it's important not to allocate huge buffers on the stack.
68 * When run with no arguments, ztest runs for about five minutes and
69 * produces no output if successful. To get a little bit of information,
70 * specify -V. To get more information, specify -VV, and so on.
72 * To turn this into an overnight stress test, use -T to specify run time.
74 * You can ask more more vdevs [-v], datasets [-d], or threads [-t]
75 * to increase the pool capacity, fanout, and overall stress level.
77 * Use the -k option to set the desired frequency of kills.
79 * When ztest invokes itself it passes all relevant information through a
80 * temporary file which is mmap-ed in the child process. This allows shared
81 * memory to survive the exec syscall. The ztest_shared_hdr_t struct is always
82 * stored at offset 0 of this file and contains information on the size and
83 * number of shared structures in the file. The information stored in this file
84 * must remain backwards compatible with older versions of ztest so that
85 * ztest can invoke them during backwards compatibility testing (-B).
88 #include <sys/zfs_context.h>
94 #include <sys/dmu_objset.h>
100 #include <sys/resource.h>
103 #include <sys/zil_impl.h>
104 #include <sys/vdev_impl.h>
105 #include <sys/vdev_file.h>
106 #include <sys/spa_impl.h>
107 #include <sys/metaslab_impl.h>
108 #include <sys/dsl_prop.h>
109 #include <sys/dsl_dataset.h>
110 #include <sys/dsl_destroy.h>
111 #include <sys/dsl_scan.h>
112 #include <sys/zio_checksum.h>
113 #include <sys/refcount.h>
114 #include <sys/zfeature.h>
115 #include <sys/dsl_userhold.h>
117 #include <stdio_ext.h>
125 #include <sys/fs/zfs.h>
126 #include <libnvpair.h>
128 static int ztest_fd_data = -1;
129 static int ztest_fd_rand = -1;
131 typedef struct ztest_shared_hdr {
132 uint64_t zh_hdr_size;
133 uint64_t zh_opts_size;
135 uint64_t zh_stats_size;
136 uint64_t zh_stats_count;
138 uint64_t zh_ds_count;
139 } ztest_shared_hdr_t;
141 static ztest_shared_hdr_t *ztest_shared_hdr;
143 typedef struct ztest_shared_opts {
144 char zo_pool[MAXNAMELEN];
145 char zo_dir[MAXNAMELEN];
146 char zo_alt_ztest[MAXNAMELEN];
147 char zo_alt_libpath[MAXNAMELEN];
149 uint64_t zo_vdevtime;
157 uint64_t zo_passtime;
158 uint64_t zo_killrate;
162 uint64_t zo_maxloops;
163 uint64_t zo_metaslab_gang_bang;
164 } ztest_shared_opts_t;
166 static const ztest_shared_opts_t ztest_opts_defaults = {
167 .zo_pool = { 'z', 't', 'e', 's', 't', '\0' },
168 .zo_dir = { '/', 't', 'm', 'p', '\0' },
169 .zo_alt_ztest = { '\0' },
170 .zo_alt_libpath = { '\0' },
172 .zo_ashift = SPA_MINBLOCKSHIFT,
175 .zo_raidz_parity = 1,
176 .zo_vdev_size = SPA_MINDEVSIZE,
179 .zo_passtime = 60, /* 60 seconds */
180 .zo_killrate = 70, /* 70% kill rate */
183 .zo_time = 300, /* 5 minutes */
184 .zo_maxloops = 50, /* max loops during spa_freeze() */
185 .zo_metaslab_gang_bang = 32 << 10
188 extern uint64_t metaslab_gang_bang;
189 extern uint64_t metaslab_df_alloc_threshold;
191 static ztest_shared_opts_t *ztest_shared_opts;
192 static ztest_shared_opts_t ztest_opts;
194 typedef struct ztest_shared_ds {
198 static ztest_shared_ds_t *ztest_shared_ds;
199 #define ZTEST_GET_SHARED_DS(d) (&ztest_shared_ds[d])
201 #define BT_MAGIC 0x123456789abcdefULL
202 #define MAXFAULTS() \
203 (MAX(zs->zs_mirrors, 1) * (ztest_opts.zo_raidz_parity + 1) - 1)
207 ZTEST_IO_WRITE_PATTERN,
208 ZTEST_IO_WRITE_ZEROES,
215 typedef struct ztest_block_tag {
225 typedef struct bufwad {
232 * XXX -- fix zfs range locks to be generic so we can use them here.
254 #define ZTEST_RANGE_LOCKS 64
255 #define ZTEST_OBJECT_LOCKS 64
258 * Object descriptor. Used as a template for object lookup/create/remove.
260 typedef struct ztest_od {
263 dmu_object_type_t od_type;
264 dmu_object_type_t od_crtype;
265 uint64_t od_blocksize;
266 uint64_t od_crblocksize;
269 char od_name[MAXNAMELEN];
275 typedef struct ztest_ds {
276 ztest_shared_ds_t *zd_shared;
278 krwlock_t zd_zilog_lock;
280 ztest_od_t *zd_od; /* debugging aid */
281 char zd_name[MAXNAMELEN];
282 kmutex_t zd_dirobj_lock;
283 rll_t zd_object_lock[ZTEST_OBJECT_LOCKS];
284 rll_t zd_range_lock[ZTEST_RANGE_LOCKS];
288 * Per-iteration state.
290 typedef void ztest_func_t(ztest_ds_t *zd, uint64_t id);
292 typedef struct ztest_info {
293 ztest_func_t *zi_func; /* test function */
294 uint64_t zi_iters; /* iterations per execution */
295 uint64_t *zi_interval; /* execute every <interval> seconds */
298 typedef struct ztest_shared_callstate {
299 uint64_t zc_count; /* per-pass count */
300 uint64_t zc_time; /* per-pass time */
301 uint64_t zc_next; /* next time to call this function */
302 } ztest_shared_callstate_t;
304 static ztest_shared_callstate_t *ztest_shared_callstate;
305 #define ZTEST_GET_SHARED_CALLSTATE(c) (&ztest_shared_callstate[c])
308 * Note: these aren't static because we want dladdr() to work.
310 ztest_func_t ztest_dmu_read_write;
311 ztest_func_t ztest_dmu_write_parallel;
312 ztest_func_t ztest_dmu_object_alloc_free;
313 ztest_func_t ztest_dmu_commit_callbacks;
314 ztest_func_t ztest_zap;
315 ztest_func_t ztest_zap_parallel;
316 ztest_func_t ztest_zil_commit;
317 ztest_func_t ztest_zil_remount;
318 ztest_func_t ztest_dmu_read_write_zcopy;
319 ztest_func_t ztest_dmu_objset_create_destroy;
320 ztest_func_t ztest_dmu_prealloc;
321 ztest_func_t ztest_fzap;
322 ztest_func_t ztest_dmu_snapshot_create_destroy;
323 ztest_func_t ztest_dsl_prop_get_set;
324 ztest_func_t ztest_spa_prop_get_set;
325 ztest_func_t ztest_spa_create_destroy;
326 ztest_func_t ztest_fault_inject;
327 ztest_func_t ztest_ddt_repair;
328 ztest_func_t ztest_dmu_snapshot_hold;
329 ztest_func_t ztest_spa_rename;
330 ztest_func_t ztest_scrub;
331 ztest_func_t ztest_dsl_dataset_promote_busy;
332 ztest_func_t ztest_vdev_attach_detach;
333 ztest_func_t ztest_vdev_LUN_growth;
334 ztest_func_t ztest_vdev_add_remove;
335 ztest_func_t ztest_vdev_aux_add_remove;
336 ztest_func_t ztest_split_pool;
337 ztest_func_t ztest_reguid;
338 ztest_func_t ztest_spa_upgrade;
340 uint64_t zopt_always = 0ULL * NANOSEC; /* all the time */
341 uint64_t zopt_incessant = 1ULL * NANOSEC / 10; /* every 1/10 second */
342 uint64_t zopt_often = 1ULL * NANOSEC; /* every second */
343 uint64_t zopt_sometimes = 10ULL * NANOSEC; /* every 10 seconds */
344 uint64_t zopt_rarely = 60ULL * NANOSEC; /* every 60 seconds */
346 ztest_info_t ztest_info[] = {
347 { ztest_dmu_read_write, 1, &zopt_always },
348 { ztest_dmu_write_parallel, 10, &zopt_always },
349 { ztest_dmu_object_alloc_free, 1, &zopt_always },
350 { ztest_dmu_commit_callbacks, 1, &zopt_always },
351 { ztest_zap, 30, &zopt_always },
352 { ztest_zap_parallel, 100, &zopt_always },
353 { ztest_split_pool, 1, &zopt_always },
354 { ztest_zil_commit, 1, &zopt_incessant },
355 { ztest_zil_remount, 1, &zopt_sometimes },
356 { ztest_dmu_read_write_zcopy, 1, &zopt_often },
357 { ztest_dmu_objset_create_destroy, 1, &zopt_often },
358 { ztest_dsl_prop_get_set, 1, &zopt_often },
359 { ztest_spa_prop_get_set, 1, &zopt_sometimes },
361 { ztest_dmu_prealloc, 1, &zopt_sometimes },
363 { ztest_fzap, 1, &zopt_sometimes },
364 { ztest_dmu_snapshot_create_destroy, 1, &zopt_sometimes },
365 { ztest_spa_create_destroy, 1, &zopt_sometimes },
366 { ztest_fault_inject, 1, &zopt_sometimes },
367 { ztest_ddt_repair, 1, &zopt_sometimes },
368 { ztest_dmu_snapshot_hold, 1, &zopt_sometimes },
369 { ztest_reguid, 1, &zopt_rarely },
370 { ztest_spa_rename, 1, &zopt_rarely },
371 { ztest_scrub, 1, &zopt_rarely },
372 { ztest_spa_upgrade, 1, &zopt_rarely },
373 { ztest_dsl_dataset_promote_busy, 1, &zopt_rarely },
374 { ztest_vdev_attach_detach, 1, &zopt_sometimes },
375 { ztest_vdev_LUN_growth, 1, &zopt_rarely },
376 { ztest_vdev_add_remove, 1,
377 &ztest_opts.zo_vdevtime },
378 { ztest_vdev_aux_add_remove, 1,
379 &ztest_opts.zo_vdevtime },
382 #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
385 * The following struct is used to hold a list of uncalled commit callbacks.
386 * The callbacks are ordered by txg number.
388 typedef struct ztest_cb_list {
389 kmutex_t zcl_callbacks_lock;
390 list_t zcl_callbacks;
394 * Stuff we need to share writably between parent and child.
396 typedef struct ztest_shared {
397 boolean_t zs_do_init;
398 hrtime_t zs_proc_start;
399 hrtime_t zs_proc_stop;
400 hrtime_t zs_thread_start;
401 hrtime_t zs_thread_stop;
402 hrtime_t zs_thread_kill;
403 uint64_t zs_enospc_count;
404 uint64_t zs_vdev_next_leaf;
405 uint64_t zs_vdev_aux;
410 uint64_t zs_metaslab_sz;
411 uint64_t zs_metaslab_df_alloc_threshold;
415 #define ID_PARALLEL -1ULL
417 static char ztest_dev_template[] = "%s/%s.%llua";
418 static char ztest_aux_template[] = "%s/%s.%s.%llu";
419 ztest_shared_t *ztest_shared;
421 static spa_t *ztest_spa = NULL;
422 static ztest_ds_t *ztest_ds;
424 static kmutex_t ztest_vdev_lock;
427 * The ztest_name_lock protects the pool and dataset namespace used by
428 * the individual tests. To modify the namespace, consumers must grab
429 * this lock as writer. Grabbing the lock as reader will ensure that the
430 * namespace does not change while the lock is held.
432 static krwlock_t ztest_name_lock;
434 static boolean_t ztest_dump_core = B_TRUE;
435 static boolean_t ztest_exiting;
437 /* Global commit callback list */
438 static ztest_cb_list_t zcl;
439 /* Commit cb delay */
440 static uint64_t zc_min_txg_delay = UINT64_MAX;
441 static int zc_cb_counter = 0;
444 * Minimum number of commit callbacks that need to be registered for us to check
445 * whether the minimum txg delay is acceptable.
447 #define ZTEST_COMMIT_CB_MIN_REG 100
450 * If a number of txgs equal to this threshold have been created after a commit
451 * callback has been registered but not called, then we assume there is an
452 * implementation bug.
454 #define ZTEST_COMMIT_CB_THRESH (TXG_CONCURRENT_STATES + 1000)
456 extern uint64_t metaslab_gang_bang;
457 extern uint64_t metaslab_df_alloc_threshold;
460 ZTEST_META_DNODE = 0,
465 static void usage(boolean_t) __NORETURN;
468 * These libumem hooks provide a reasonable set of defaults for the allocator's
469 * debugging facilities.
472 _umem_debug_init(void)
474 return ("default,verbose"); /* $UMEM_DEBUG setting */
478 _umem_logging_init(void)
480 return ("fail,contents"); /* $UMEM_LOGGING setting */
483 #define FATAL_MSG_SZ 1024
488 fatal(int do_perror, char *message, ...)
491 int save_errno = errno;
494 (void) fflush(stdout);
495 buf = umem_alloc(FATAL_MSG_SZ, UMEM_NOFAIL);
497 va_start(args, message);
498 (void) sprintf(buf, "ztest: ");
500 (void) vsprintf(buf + strlen(buf), message, args);
503 (void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
504 ": %s", strerror(save_errno));
506 (void) fprintf(stderr, "%s\n", buf);
507 fatal_msg = buf; /* to ease debugging */
514 str2shift(const char *buf)
516 const char *ends = "BKMGTPEZ";
521 for (i = 0; i < strlen(ends); i++) {
522 if (toupper(buf[0]) == ends[i])
525 if (i == strlen(ends)) {
526 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
530 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
533 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
539 nicenumtoull(const char *buf)
544 val = strtoull(buf, &end, 0);
546 (void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
548 } else if (end[0] == '.') {
549 double fval = strtod(buf, &end);
550 fval *= pow(2, str2shift(end));
551 if (fval > UINT64_MAX) {
552 (void) fprintf(stderr, "ztest: value too large: %s\n",
556 val = (uint64_t)fval;
558 int shift = str2shift(end);
559 if (shift >= 64 || (val << shift) >> shift != val) {
560 (void) fprintf(stderr, "ztest: value too large: %s\n",
570 usage(boolean_t requested)
572 const ztest_shared_opts_t *zo = &ztest_opts_defaults;
574 char nice_vdev_size[10];
575 char nice_gang_bang[10];
576 FILE *fp = requested ? stdout : stderr;
578 nicenum(zo->zo_vdev_size, nice_vdev_size);
579 nicenum(zo->zo_metaslab_gang_bang, nice_gang_bang);
581 (void) fprintf(fp, "Usage: %s\n"
582 "\t[-v vdevs (default: %llu)]\n"
583 "\t[-s size_of_each_vdev (default: %s)]\n"
584 "\t[-a alignment_shift (default: %d)] use 0 for random\n"
585 "\t[-m mirror_copies (default: %d)]\n"
586 "\t[-r raidz_disks (default: %d)]\n"
587 "\t[-R raidz_parity (default: %d)]\n"
588 "\t[-d datasets (default: %d)]\n"
589 "\t[-t threads (default: %d)]\n"
590 "\t[-g gang_block_threshold (default: %s)]\n"
591 "\t[-i init_count (default: %d)] initialize pool i times\n"
592 "\t[-k kill_percentage (default: %llu%%)]\n"
593 "\t[-p pool_name (default: %s)]\n"
594 "\t[-f dir (default: %s)] file directory for vdev files\n"
595 "\t[-V] verbose (use multiple times for ever more blather)\n"
596 "\t[-E] use existing pool instead of creating new one\n"
597 "\t[-T time (default: %llu sec)] total run time\n"
598 "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
599 "\t[-P passtime (default: %llu sec)] time per pass\n"
600 "\t[-B alt_ztest (default: <none>)] alternate ztest path\n"
601 "\t[-h] (print help)\n"
604 (u_longlong_t)zo->zo_vdevs, /* -v */
605 nice_vdev_size, /* -s */
606 zo->zo_ashift, /* -a */
607 zo->zo_mirrors, /* -m */
608 zo->zo_raidz, /* -r */
609 zo->zo_raidz_parity, /* -R */
610 zo->zo_datasets, /* -d */
611 zo->zo_threads, /* -t */
612 nice_gang_bang, /* -g */
613 zo->zo_init, /* -i */
614 (u_longlong_t)zo->zo_killrate, /* -k */
615 zo->zo_pool, /* -p */
617 (u_longlong_t)zo->zo_time, /* -T */
618 (u_longlong_t)zo->zo_maxloops, /* -F */
619 (u_longlong_t)zo->zo_passtime);
620 exit(requested ? 0 : 1);
624 process_options(int argc, char **argv)
627 ztest_shared_opts_t *zo = &ztest_opts;
631 char altdir[MAXNAMELEN] = { 0 };
633 bcopy(&ztest_opts_defaults, zo, sizeof (*zo));
635 while ((opt = getopt(argc, argv,
636 "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:hF:B:")) != EOF) {
653 value = nicenumtoull(optarg);
657 zo->zo_vdevs = value;
660 zo->zo_vdev_size = MAX(SPA_MINDEVSIZE, value);
663 zo->zo_ashift = value;
666 zo->zo_mirrors = value;
669 zo->zo_raidz = MAX(1, value);
672 zo->zo_raidz_parity = MIN(MAX(value, 1), 3);
675 zo->zo_datasets = MAX(1, value);
678 zo->zo_threads = MAX(1, value);
681 zo->zo_metaslab_gang_bang = MAX(SPA_MINBLOCKSIZE << 1,
688 zo->zo_killrate = value;
691 (void) strlcpy(zo->zo_pool, optarg,
692 sizeof (zo->zo_pool));
695 path = realpath(optarg, NULL);
697 (void) fprintf(stderr, "error: %s: %s\n",
698 optarg, strerror(errno));
701 (void) strlcpy(zo->zo_dir, path,
702 sizeof (zo->zo_dir));
715 zo->zo_passtime = MAX(1, value);
718 zo->zo_maxloops = MAX(1, value);
721 (void) strlcpy(altdir, optarg, sizeof (altdir));
733 zo->zo_raidz_parity = MIN(zo->zo_raidz_parity, zo->zo_raidz - 1);
736 (zo->zo_vdevs > 0 ? zo->zo_time * NANOSEC / zo->zo_vdevs :
739 if (strlen(altdir) > 0) {
747 cmd = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
748 realaltdir = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
750 VERIFY(NULL != realpath(getexecname(), cmd));
751 if (0 != access(altdir, F_OK)) {
752 ztest_dump_core = B_FALSE;
753 fatal(B_TRUE, "invalid alternate ztest path: %s",
756 VERIFY(NULL != realpath(altdir, realaltdir));
759 * 'cmd' should be of the form "<anything>/usr/bin/<isa>/ztest".
760 * We want to extract <isa> to determine if we should use
761 * 32 or 64 bit binaries.
763 bin = strstr(cmd, "/usr/bin/");
764 ztest = strstr(bin, "/ztest");
766 isalen = ztest - isa;
767 (void) snprintf(zo->zo_alt_ztest, sizeof (zo->zo_alt_ztest),
768 "%s/usr/bin/%.*s/ztest", realaltdir, isalen, isa);
769 (void) snprintf(zo->zo_alt_libpath, sizeof (zo->zo_alt_libpath),
770 "%s/usr/lib/%.*s", realaltdir, isalen, isa);
772 if (0 != access(zo->zo_alt_ztest, X_OK)) {
773 ztest_dump_core = B_FALSE;
774 fatal(B_TRUE, "invalid alternate ztest: %s",
776 } else if (0 != access(zo->zo_alt_libpath, X_OK)) {
777 ztest_dump_core = B_FALSE;
778 fatal(B_TRUE, "invalid alternate lib directory %s",
782 umem_free(cmd, MAXPATHLEN);
783 umem_free(realaltdir, MAXPATHLEN);
788 ztest_kill(ztest_shared_t *zs)
790 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(ztest_spa));
791 zs->zs_space = metaslab_class_get_space(spa_normal_class(ztest_spa));
794 * Before we kill off ztest, make sure that the config is updated.
795 * See comment above spa_config_sync().
797 mutex_enter(&spa_namespace_lock);
798 spa_config_sync(ztest_spa, B_FALSE, B_FALSE);
799 mutex_exit(&spa_namespace_lock);
801 if (ztest_opts.zo_verbose >= 3)
802 zfs_dbgmsg_print(FTAG);
804 (void) kill(getpid(), SIGKILL);
808 ztest_random(uint64_t range)
812 ASSERT3S(ztest_fd_rand, >=, 0);
817 if (read(ztest_fd_rand, &r, sizeof (r)) != sizeof (r))
818 fatal(1, "short read from /dev/urandom");
825 ztest_record_enospc(const char *s)
827 ztest_shared->zs_enospc_count++;
831 ztest_get_ashift(void)
833 if (ztest_opts.zo_ashift == 0)
834 return (SPA_MINBLOCKSHIFT + ztest_random(3));
835 return (ztest_opts.zo_ashift);
839 make_vdev_file(char *path, char *aux, char *pool, size_t size, uint64_t ashift)
845 pathbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
848 ashift = ztest_get_ashift();
854 vdev = ztest_shared->zs_vdev_aux;
855 (void) snprintf(path, MAXPATHLEN,
856 ztest_aux_template, ztest_opts.zo_dir,
857 pool == NULL ? ztest_opts.zo_pool : pool,
860 vdev = ztest_shared->zs_vdev_next_leaf++;
861 (void) snprintf(path, MAXPATHLEN,
862 ztest_dev_template, ztest_opts.zo_dir,
863 pool == NULL ? ztest_opts.zo_pool : pool, vdev);
868 int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
870 fatal(1, "can't open %s", path);
871 if (ftruncate(fd, size) != 0)
872 fatal(1, "can't ftruncate %s", path);
876 VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
877 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
878 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
879 VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
880 umem_free(pathbuf, MAXPATHLEN);
886 make_vdev_raidz(char *path, char *aux, char *pool, size_t size,
887 uint64_t ashift, int r)
889 nvlist_t *raidz, **child;
893 return (make_vdev_file(path, aux, pool, size, ashift));
894 child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
896 for (c = 0; c < r; c++)
897 child[c] = make_vdev_file(path, aux, pool, size, ashift);
899 VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
900 VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
901 VDEV_TYPE_RAIDZ) == 0);
902 VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
903 ztest_opts.zo_raidz_parity) == 0);
904 VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
907 for (c = 0; c < r; c++)
908 nvlist_free(child[c]);
910 umem_free(child, r * sizeof (nvlist_t *));
916 make_vdev_mirror(char *path, char *aux, char *pool, size_t size,
917 uint64_t ashift, int r, int m)
919 nvlist_t *mirror, **child;
923 return (make_vdev_raidz(path, aux, pool, size, ashift, r));
925 child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
927 for (c = 0; c < m; c++)
928 child[c] = make_vdev_raidz(path, aux, pool, size, ashift, r);
930 VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
931 VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
932 VDEV_TYPE_MIRROR) == 0);
933 VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
936 for (c = 0; c < m; c++)
937 nvlist_free(child[c]);
939 umem_free(child, m * sizeof (nvlist_t *));
945 make_vdev_root(char *path, char *aux, char *pool, size_t size, uint64_t ashift,
946 int log, int r, int m, int t)
948 nvlist_t *root, **child;
953 child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
955 for (c = 0; c < t; c++) {
956 child[c] = make_vdev_mirror(path, aux, pool, size, ashift,
958 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
962 VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
963 VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
964 VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
967 for (c = 0; c < t; c++)
968 nvlist_free(child[c]);
970 umem_free(child, t * sizeof (nvlist_t *));
976 * Find a random spa version. Returns back a random spa version in the
977 * range [initial_version, SPA_VERSION_FEATURES].
980 ztest_random_spa_version(uint64_t initial_version)
982 uint64_t version = initial_version;
984 if (version <= SPA_VERSION_BEFORE_FEATURES) {
986 ztest_random(SPA_VERSION_BEFORE_FEATURES - version + 1);
989 if (version > SPA_VERSION_BEFORE_FEATURES)
990 version = SPA_VERSION_FEATURES;
992 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
997 ztest_random_blocksize(void)
999 return (1 << (SPA_MINBLOCKSHIFT +
1000 ztest_random(SPA_MAXBLOCKSHIFT - SPA_MINBLOCKSHIFT + 1)));
1004 ztest_random_ibshift(void)
1006 return (DN_MIN_INDBLKSHIFT +
1007 ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1));
1011 ztest_random_vdev_top(spa_t *spa, boolean_t log_ok)
1014 vdev_t *rvd = spa->spa_root_vdev;
1017 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1020 top = ztest_random(rvd->vdev_children);
1021 tvd = rvd->vdev_child[top];
1022 } while (tvd->vdev_ishole || (tvd->vdev_islog && !log_ok) ||
1023 tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL);
1029 ztest_random_dsl_prop(zfs_prop_t prop)
1034 value = zfs_prop_random_value(prop, ztest_random(-1ULL));
1035 } while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF);
1041 ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value,
1044 const char *propname = zfs_prop_to_name(prop);
1045 const char *valname;
1050 error = dsl_prop_set_int(osname, propname,
1051 (inherit ? ZPROP_SRC_NONE : ZPROP_SRC_LOCAL), value);
1053 if (error == ENOSPC) {
1054 ztest_record_enospc(FTAG);
1059 setpoint = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
1060 VERIFY0(dsl_prop_get_integer(osname, propname, &curval, setpoint));
1062 if (ztest_opts.zo_verbose >= 6) {
1063 VERIFY(zfs_prop_index_to_string(prop, curval, &valname) == 0);
1064 (void) printf("%s %s = %s at '%s'\n",
1065 osname, propname, valname, setpoint);
1067 umem_free(setpoint, MAXPATHLEN);
1073 ztest_spa_prop_set_uint64(zpool_prop_t prop, uint64_t value)
1075 spa_t *spa = ztest_spa;
1076 nvlist_t *props = NULL;
1079 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
1080 VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0);
1082 error = spa_prop_set(spa, props);
1086 if (error == ENOSPC) {
1087 ztest_record_enospc(FTAG);
1096 ztest_rll_init(rll_t *rll)
1098 rll->rll_writer = NULL;
1099 rll->rll_readers = 0;
1100 mutex_init(&rll->rll_lock, NULL, MUTEX_DEFAULT, NULL);
1101 cv_init(&rll->rll_cv, NULL, CV_DEFAULT, NULL);
1105 ztest_rll_destroy(rll_t *rll)
1107 ASSERT(rll->rll_writer == NULL);
1108 ASSERT(rll->rll_readers == 0);
1109 mutex_destroy(&rll->rll_lock);
1110 cv_destroy(&rll->rll_cv);
1114 ztest_rll_lock(rll_t *rll, rl_type_t type)
1116 mutex_enter(&rll->rll_lock);
1118 if (type == RL_READER) {
1119 while (rll->rll_writer != NULL)
1120 (void) cv_wait(&rll->rll_cv, &rll->rll_lock);
1123 while (rll->rll_writer != NULL || rll->rll_readers)
1124 (void) cv_wait(&rll->rll_cv, &rll->rll_lock);
1125 rll->rll_writer = curthread;
1128 mutex_exit(&rll->rll_lock);
1132 ztest_rll_unlock(rll_t *rll)
1134 mutex_enter(&rll->rll_lock);
1136 if (rll->rll_writer) {
1137 ASSERT(rll->rll_readers == 0);
1138 rll->rll_writer = NULL;
1140 ASSERT(rll->rll_readers != 0);
1141 ASSERT(rll->rll_writer == NULL);
1145 if (rll->rll_writer == NULL && rll->rll_readers == 0)
1146 cv_broadcast(&rll->rll_cv);
1148 mutex_exit(&rll->rll_lock);
1152 ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type)
1154 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1156 ztest_rll_lock(rll, type);
1160 ztest_object_unlock(ztest_ds_t *zd, uint64_t object)
1162 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1164 ztest_rll_unlock(rll);
1168 ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset,
1169 uint64_t size, rl_type_t type)
1171 uint64_t hash = object ^ (offset % (ZTEST_RANGE_LOCKS + 1));
1172 rll_t *rll = &zd->zd_range_lock[hash & (ZTEST_RANGE_LOCKS - 1)];
1175 rl = umem_alloc(sizeof (*rl), UMEM_NOFAIL);
1176 rl->rl_object = object;
1177 rl->rl_offset = offset;
1181 ztest_rll_lock(rll, type);
1187 ztest_range_unlock(rl_t *rl)
1189 rll_t *rll = rl->rl_lock;
1191 ztest_rll_unlock(rll);
1193 umem_free(rl, sizeof (*rl));
1197 ztest_zd_init(ztest_ds_t *zd, ztest_shared_ds_t *szd, objset_t *os)
1200 zd->zd_zilog = dmu_objset_zil(os);
1201 zd->zd_shared = szd;
1202 dmu_objset_name(os, zd->zd_name);
1205 if (zd->zd_shared != NULL)
1206 zd->zd_shared->zd_seq = 0;
1208 rw_init(&zd->zd_zilog_lock, NULL, RW_DEFAULT, NULL);
1209 mutex_init(&zd->zd_dirobj_lock, NULL, MUTEX_DEFAULT, NULL);
1211 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1212 ztest_rll_init(&zd->zd_object_lock[l]);
1214 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
1215 ztest_rll_init(&zd->zd_range_lock[l]);
1219 ztest_zd_fini(ztest_ds_t *zd)
1223 mutex_destroy(&zd->zd_dirobj_lock);
1224 rw_destroy(&zd->zd_zilog_lock);
1226 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1227 ztest_rll_destroy(&zd->zd_object_lock[l]);
1229 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
1230 ztest_rll_destroy(&zd->zd_range_lock[l]);
1233 #define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1236 ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag)
1242 * Attempt to assign tx to some transaction group.
1244 error = dmu_tx_assign(tx, txg_how);
1246 if (error == ERESTART) {
1247 ASSERT(txg_how == TXG_NOWAIT);
1250 ASSERT3U(error, ==, ENOSPC);
1251 ztest_record_enospc(tag);
1256 txg = dmu_tx_get_txg(tx);
1262 ztest_pattern_set(void *buf, uint64_t size, uint64_t value)
1265 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1273 ztest_pattern_match(void *buf, uint64_t size, uint64_t value)
1276 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1280 diff |= (value - *ip++);
1287 ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1288 uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
1290 bt->bt_magic = BT_MAGIC;
1291 bt->bt_objset = dmu_objset_id(os);
1292 bt->bt_object = object;
1293 bt->bt_offset = offset;
1296 bt->bt_crtxg = crtxg;
1300 ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1301 uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
1303 ASSERT(bt->bt_magic == BT_MAGIC);
1304 ASSERT(bt->bt_objset == dmu_objset_id(os));
1305 ASSERT(bt->bt_object == object);
1306 ASSERT(bt->bt_offset == offset);
1307 ASSERT(bt->bt_gen <= gen);
1308 ASSERT(bt->bt_txg <= txg);
1309 ASSERT(bt->bt_crtxg == crtxg);
1312 static ztest_block_tag_t *
1313 ztest_bt_bonus(dmu_buf_t *db)
1315 dmu_object_info_t doi;
1316 ztest_block_tag_t *bt;
1318 dmu_object_info_from_db(db, &doi);
1319 ASSERT3U(doi.doi_bonus_size, <=, db->db_size);
1320 ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt));
1321 bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt));
1330 #define lrz_type lr_mode
1331 #define lrz_blocksize lr_uid
1332 #define lrz_ibshift lr_gid
1333 #define lrz_bonustype lr_rdev
1334 #define lrz_bonuslen lr_crtime[1]
1337 ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr)
1339 char *name = (void *)(lr + 1); /* name follows lr */
1340 size_t namesize = strlen(name) + 1;
1343 if (zil_replaying(zd->zd_zilog, tx))
1346 itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize);
1347 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1348 sizeof (*lr) + namesize - sizeof (lr_t));
1350 zil_itx_assign(zd->zd_zilog, itx, tx);
1354 ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr, uint64_t object)
1356 char *name = (void *)(lr + 1); /* name follows lr */
1357 size_t namesize = strlen(name) + 1;
1360 if (zil_replaying(zd->zd_zilog, tx))
1363 itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize);
1364 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1365 sizeof (*lr) + namesize - sizeof (lr_t));
1367 itx->itx_oid = object;
1368 zil_itx_assign(zd->zd_zilog, itx, tx);
1372 ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr)
1375 itx_wr_state_t write_state = ztest_random(WR_NUM_STATES);
1377 if (zil_replaying(zd->zd_zilog, tx))
1380 if (lr->lr_length > ZIL_MAX_LOG_DATA)
1381 write_state = WR_INDIRECT;
1383 itx = zil_itx_create(TX_WRITE,
1384 sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0));
1386 if (write_state == WR_COPIED &&
1387 dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length,
1388 ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) {
1389 zil_itx_destroy(itx);
1390 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1391 write_state = WR_NEED_COPY;
1393 itx->itx_private = zd;
1394 itx->itx_wr_state = write_state;
1395 itx->itx_sync = (ztest_random(8) == 0);
1396 itx->itx_sod += (write_state == WR_NEED_COPY ? lr->lr_length : 0);
1398 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1399 sizeof (*lr) - sizeof (lr_t));
1401 zil_itx_assign(zd->zd_zilog, itx, tx);
1405 ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr)
1409 if (zil_replaying(zd->zd_zilog, tx))
1412 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1413 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1414 sizeof (*lr) - sizeof (lr_t));
1416 itx->itx_sync = B_FALSE;
1417 zil_itx_assign(zd->zd_zilog, itx, tx);
1421 ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr)
1425 if (zil_replaying(zd->zd_zilog, tx))
1428 itx = zil_itx_create(TX_SETATTR, sizeof (*lr));
1429 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1430 sizeof (*lr) - sizeof (lr_t));
1432 itx->itx_sync = B_FALSE;
1433 zil_itx_assign(zd->zd_zilog, itx, tx);
1440 ztest_replay_create(ztest_ds_t *zd, lr_create_t *lr, boolean_t byteswap)
1442 char *name = (void *)(lr + 1); /* name follows lr */
1443 objset_t *os = zd->zd_os;
1444 ztest_block_tag_t *bbt;
1451 byteswap_uint64_array(lr, sizeof (*lr));
1453 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1454 ASSERT(name[0] != '\0');
1456 tx = dmu_tx_create(os);
1458 dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name);
1460 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1461 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1463 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1466 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1470 ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid);
1472 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1473 if (lr->lr_foid == 0) {
1474 lr->lr_foid = zap_create(os,
1475 lr->lrz_type, lr->lrz_bonustype,
1476 lr->lrz_bonuslen, tx);
1478 error = zap_create_claim(os, lr->lr_foid,
1479 lr->lrz_type, lr->lrz_bonustype,
1480 lr->lrz_bonuslen, tx);
1483 if (lr->lr_foid == 0) {
1484 lr->lr_foid = dmu_object_alloc(os,
1485 lr->lrz_type, 0, lr->lrz_bonustype,
1486 lr->lrz_bonuslen, tx);
1488 error = dmu_object_claim(os, lr->lr_foid,
1489 lr->lrz_type, 0, lr->lrz_bonustype,
1490 lr->lrz_bonuslen, tx);
1495 ASSERT3U(error, ==, EEXIST);
1496 ASSERT(zd->zd_zilog->zl_replay);
1501 ASSERT(lr->lr_foid != 0);
1503 if (lr->lrz_type != DMU_OT_ZAP_OTHER)
1504 VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid,
1505 lr->lrz_blocksize, lr->lrz_ibshift, tx));
1507 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1508 bbt = ztest_bt_bonus(db);
1509 dmu_buf_will_dirty(db, tx);
1510 ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_gen, txg, txg);
1511 dmu_buf_rele(db, FTAG);
1513 VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1,
1516 (void) ztest_log_create(zd, tx, lr);
1524 ztest_replay_remove(ztest_ds_t *zd, lr_remove_t *lr, boolean_t byteswap)
1526 char *name = (void *)(lr + 1); /* name follows lr */
1527 objset_t *os = zd->zd_os;
1528 dmu_object_info_t doi;
1530 uint64_t object, txg;
1533 byteswap_uint64_array(lr, sizeof (*lr));
1535 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1536 ASSERT(name[0] != '\0');
1539 zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object));
1540 ASSERT(object != 0);
1542 ztest_object_lock(zd, object, RL_WRITER);
1544 VERIFY3U(0, ==, dmu_object_info(os, object, &doi));
1546 tx = dmu_tx_create(os);
1548 dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name);
1549 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1551 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1553 ztest_object_unlock(zd, object);
1557 if (doi.doi_type == DMU_OT_ZAP_OTHER) {
1558 VERIFY3U(0, ==, zap_destroy(os, object, tx));
1560 VERIFY3U(0, ==, dmu_object_free(os, object, tx));
1563 VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx));
1565 (void) ztest_log_remove(zd, tx, lr, object);
1569 ztest_object_unlock(zd, object);
1575 ztest_replay_write(ztest_ds_t *zd, lr_write_t *lr, boolean_t byteswap)
1577 objset_t *os = zd->zd_os;
1578 void *data = lr + 1; /* data follows lr */
1579 uint64_t offset, length;
1580 ztest_block_tag_t *bt = data;
1581 ztest_block_tag_t *bbt;
1582 uint64_t gen, txg, lrtxg, crtxg;
1583 dmu_object_info_t doi;
1586 arc_buf_t *abuf = NULL;
1590 byteswap_uint64_array(lr, sizeof (*lr));
1592 offset = lr->lr_offset;
1593 length = lr->lr_length;
1595 /* If it's a dmu_sync() block, write the whole block */
1596 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
1597 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
1598 if (length < blocksize) {
1599 offset -= offset % blocksize;
1604 if (bt->bt_magic == BSWAP_64(BT_MAGIC))
1605 byteswap_uint64_array(bt, sizeof (*bt));
1607 if (bt->bt_magic != BT_MAGIC)
1610 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1611 rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER);
1613 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1615 dmu_object_info_from_db(db, &doi);
1617 bbt = ztest_bt_bonus(db);
1618 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1620 crtxg = bbt->bt_crtxg;
1621 lrtxg = lr->lr_common.lrc_txg;
1623 tx = dmu_tx_create(os);
1625 dmu_tx_hold_write(tx, lr->lr_foid, offset, length);
1627 if (ztest_random(8) == 0 && length == doi.doi_data_block_size &&
1628 P2PHASE(offset, length) == 0)
1629 abuf = dmu_request_arcbuf(db, length);
1631 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1634 dmu_return_arcbuf(abuf);
1635 dmu_buf_rele(db, FTAG);
1636 ztest_range_unlock(rl);
1637 ztest_object_unlock(zd, lr->lr_foid);
1643 * Usually, verify the old data before writing new data --
1644 * but not always, because we also want to verify correct
1645 * behavior when the data was not recently read into cache.
1647 ASSERT(offset % doi.doi_data_block_size == 0);
1648 if (ztest_random(4) != 0) {
1649 int prefetch = ztest_random(2) ?
1650 DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
1651 ztest_block_tag_t rbt;
1653 VERIFY(dmu_read(os, lr->lr_foid, offset,
1654 sizeof (rbt), &rbt, prefetch) == 0);
1655 if (rbt.bt_magic == BT_MAGIC) {
1656 ztest_bt_verify(&rbt, os, lr->lr_foid,
1657 offset, gen, txg, crtxg);
1662 * Writes can appear to be newer than the bonus buffer because
1663 * the ztest_get_data() callback does a dmu_read() of the
1664 * open-context data, which may be different than the data
1665 * as it was when the write was generated.
1667 if (zd->zd_zilog->zl_replay) {
1668 ztest_bt_verify(bt, os, lr->lr_foid, offset,
1669 MAX(gen, bt->bt_gen), MAX(txg, lrtxg),
1674 * Set the bt's gen/txg to the bonus buffer's gen/txg
1675 * so that all of the usual ASSERTs will work.
1677 ztest_bt_generate(bt, os, lr->lr_foid, offset, gen, txg, crtxg);
1681 dmu_write(os, lr->lr_foid, offset, length, data, tx);
1683 bcopy(data, abuf->b_data, length);
1684 dmu_assign_arcbuf(db, offset, abuf, tx);
1687 (void) ztest_log_write(zd, tx, lr);
1689 dmu_buf_rele(db, FTAG);
1693 ztest_range_unlock(rl);
1694 ztest_object_unlock(zd, lr->lr_foid);
1700 ztest_replay_truncate(ztest_ds_t *zd, lr_truncate_t *lr, boolean_t byteswap)
1702 objset_t *os = zd->zd_os;
1708 byteswap_uint64_array(lr, sizeof (*lr));
1710 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1711 rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length,
1714 tx = dmu_tx_create(os);
1716 dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length);
1718 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1720 ztest_range_unlock(rl);
1721 ztest_object_unlock(zd, lr->lr_foid);
1725 VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset,
1726 lr->lr_length, tx) == 0);
1728 (void) ztest_log_truncate(zd, tx, lr);
1732 ztest_range_unlock(rl);
1733 ztest_object_unlock(zd, lr->lr_foid);
1739 ztest_replay_setattr(ztest_ds_t *zd, lr_setattr_t *lr, boolean_t byteswap)
1741 objset_t *os = zd->zd_os;
1744 ztest_block_tag_t *bbt;
1745 uint64_t txg, lrtxg, crtxg;
1748 byteswap_uint64_array(lr, sizeof (*lr));
1750 ztest_object_lock(zd, lr->lr_foid, RL_WRITER);
1752 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1754 tx = dmu_tx_create(os);
1755 dmu_tx_hold_bonus(tx, lr->lr_foid);
1757 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1759 dmu_buf_rele(db, FTAG);
1760 ztest_object_unlock(zd, lr->lr_foid);
1764 bbt = ztest_bt_bonus(db);
1765 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1766 crtxg = bbt->bt_crtxg;
1767 lrtxg = lr->lr_common.lrc_txg;
1769 if (zd->zd_zilog->zl_replay) {
1770 ASSERT(lr->lr_size != 0);
1771 ASSERT(lr->lr_mode != 0);
1775 * Randomly change the size and increment the generation.
1777 lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) *
1779 lr->lr_mode = bbt->bt_gen + 1;
1784 * Verify that the current bonus buffer is not newer than our txg.
1786 ztest_bt_verify(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode,
1787 MAX(txg, lrtxg), crtxg);
1789 dmu_buf_will_dirty(db, tx);
1791 ASSERT3U(lr->lr_size, >=, sizeof (*bbt));
1792 ASSERT3U(lr->lr_size, <=, db->db_size);
1793 VERIFY0(dmu_set_bonus(db, lr->lr_size, tx));
1794 bbt = ztest_bt_bonus(db);
1796 ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode, txg, crtxg);
1798 dmu_buf_rele(db, FTAG);
1800 (void) ztest_log_setattr(zd, tx, lr);
1804 ztest_object_unlock(zd, lr->lr_foid);
1809 zil_replay_func_t ztest_replay_vector[TX_MAX_TYPE] = {
1810 NULL, /* 0 no such transaction type */
1811 (zil_replay_func_t)ztest_replay_create, /* TX_CREATE */
1812 NULL, /* TX_MKDIR */
1813 NULL, /* TX_MKXATTR */
1814 NULL, /* TX_SYMLINK */
1815 (zil_replay_func_t)ztest_replay_remove, /* TX_REMOVE */
1816 NULL, /* TX_RMDIR */
1818 NULL, /* TX_RENAME */
1819 (zil_replay_func_t)ztest_replay_write, /* TX_WRITE */
1820 (zil_replay_func_t)ztest_replay_truncate, /* TX_TRUNCATE */
1821 (zil_replay_func_t)ztest_replay_setattr, /* TX_SETATTR */
1823 NULL, /* TX_CREATE_ACL */
1824 NULL, /* TX_CREATE_ATTR */
1825 NULL, /* TX_CREATE_ACL_ATTR */
1826 NULL, /* TX_MKDIR_ACL */
1827 NULL, /* TX_MKDIR_ATTR */
1828 NULL, /* TX_MKDIR_ACL_ATTR */
1829 NULL, /* TX_WRITE2 */
1833 * ZIL get_data callbacks
1837 ztest_get_done(zgd_t *zgd, int error)
1839 ztest_ds_t *zd = zgd->zgd_private;
1840 uint64_t object = zgd->zgd_rl->rl_object;
1843 dmu_buf_rele(zgd->zgd_db, zgd);
1845 ztest_range_unlock(zgd->zgd_rl);
1846 ztest_object_unlock(zd, object);
1848 if (error == 0 && zgd->zgd_bp)
1849 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1851 umem_free(zgd, sizeof (*zgd));
1855 ztest_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1857 ztest_ds_t *zd = arg;
1858 objset_t *os = zd->zd_os;
1859 uint64_t object = lr->lr_foid;
1860 uint64_t offset = lr->lr_offset;
1861 uint64_t size = lr->lr_length;
1862 blkptr_t *bp = &lr->lr_blkptr;
1863 uint64_t txg = lr->lr_common.lrc_txg;
1865 dmu_object_info_t doi;
1870 ztest_object_lock(zd, object, RL_READER);
1871 error = dmu_bonus_hold(os, object, FTAG, &db);
1873 ztest_object_unlock(zd, object);
1877 crtxg = ztest_bt_bonus(db)->bt_crtxg;
1879 if (crtxg == 0 || crtxg > txg) {
1880 dmu_buf_rele(db, FTAG);
1881 ztest_object_unlock(zd, object);
1885 dmu_object_info_from_db(db, &doi);
1886 dmu_buf_rele(db, FTAG);
1889 zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL);
1890 zgd->zgd_zilog = zd->zd_zilog;
1891 zgd->zgd_private = zd;
1893 if (buf != NULL) { /* immediate write */
1894 zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
1897 error = dmu_read(os, object, offset, size, buf,
1898 DMU_READ_NO_PREFETCH);
1901 size = doi.doi_data_block_size;
1903 offset = P2ALIGN(offset, size);
1905 ASSERT(offset < size);
1909 zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
1912 error = dmu_buf_hold(os, object, offset, zgd, &db,
1913 DMU_READ_NO_PREFETCH);
1916 blkptr_t *obp = dmu_buf_get_blkptr(db);
1918 ASSERT(BP_IS_HOLE(bp));
1925 ASSERT(db->db_offset == offset);
1926 ASSERT(db->db_size == size);
1928 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1929 ztest_get_done, zgd);
1936 ztest_get_done(zgd, error);
1942 ztest_lr_alloc(size_t lrsize, char *name)
1945 size_t namesize = name ? strlen(name) + 1 : 0;
1947 lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
1950 bcopy(name, lr + lrsize, namesize);
1956 ztest_lr_free(void *lr, size_t lrsize, char *name)
1958 size_t namesize = name ? strlen(name) + 1 : 0;
1960 umem_free(lr, lrsize + namesize);
1964 * Lookup a bunch of objects. Returns the number of objects not found.
1967 ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
1973 ASSERT(mutex_held(&zd->zd_dirobj_lock));
1975 for (i = 0; i < count; i++, od++) {
1977 error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
1978 sizeof (uint64_t), 1, &od->od_object);
1980 ASSERT(error == ENOENT);
1981 ASSERT(od->od_object == 0);
1985 ztest_block_tag_t *bbt;
1986 dmu_object_info_t doi;
1988 ASSERT(od->od_object != 0);
1989 ASSERT(missing == 0); /* there should be no gaps */
1991 ztest_object_lock(zd, od->od_object, RL_READER);
1992 VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
1993 od->od_object, FTAG, &db));
1994 dmu_object_info_from_db(db, &doi);
1995 bbt = ztest_bt_bonus(db);
1996 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1997 od->od_type = doi.doi_type;
1998 od->od_blocksize = doi.doi_data_block_size;
1999 od->od_gen = bbt->bt_gen;
2000 dmu_buf_rele(db, FTAG);
2001 ztest_object_unlock(zd, od->od_object);
2009 ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
2014 ASSERT(mutex_held(&zd->zd_dirobj_lock));
2016 for (i = 0; i < count; i++, od++) {
2023 lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2025 lr->lr_doid = od->od_dir;
2026 lr->lr_foid = 0; /* 0 to allocate, > 0 to claim */
2027 lr->lrz_type = od->od_crtype;
2028 lr->lrz_blocksize = od->od_crblocksize;
2029 lr->lrz_ibshift = ztest_random_ibshift();
2030 lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
2031 lr->lrz_bonuslen = dmu_bonus_max();
2032 lr->lr_gen = od->od_crgen;
2033 lr->lr_crtime[0] = time(NULL);
2035 if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
2036 ASSERT(missing == 0);
2040 od->od_object = lr->lr_foid;
2041 od->od_type = od->od_crtype;
2042 od->od_blocksize = od->od_crblocksize;
2043 od->od_gen = od->od_crgen;
2044 ASSERT(od->od_object != 0);
2047 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2054 ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
2060 ASSERT(mutex_held(&zd->zd_dirobj_lock));
2064 for (i = count - 1; i >= 0; i--, od--) {
2071 * No object was found.
2073 if (od->od_object == 0)
2076 lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2078 lr->lr_doid = od->od_dir;
2080 if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
2081 ASSERT3U(error, ==, ENOSPC);
2086 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2093 ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
2099 lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
2101 lr->lr_foid = object;
2102 lr->lr_offset = offset;
2103 lr->lr_length = size;
2105 BP_ZERO(&lr->lr_blkptr);
2107 bcopy(data, lr + 1, size);
2109 error = ztest_replay_write(zd, lr, B_FALSE);
2111 ztest_lr_free(lr, sizeof (*lr) + size, NULL);
2117 ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2122 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2124 lr->lr_foid = object;
2125 lr->lr_offset = offset;
2126 lr->lr_length = size;
2128 error = ztest_replay_truncate(zd, lr, B_FALSE);
2130 ztest_lr_free(lr, sizeof (*lr), NULL);
2136 ztest_setattr(ztest_ds_t *zd, uint64_t object)
2141 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2143 lr->lr_foid = object;
2147 error = ztest_replay_setattr(zd, lr, B_FALSE);
2149 ztest_lr_free(lr, sizeof (*lr), NULL);
2155 ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2157 objset_t *os = zd->zd_os;
2162 txg_wait_synced(dmu_objset_pool(os), 0);
2164 ztest_object_lock(zd, object, RL_READER);
2165 rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
2167 tx = dmu_tx_create(os);
2169 dmu_tx_hold_write(tx, object, offset, size);
2171 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
2174 dmu_prealloc(os, object, offset, size, tx);
2176 txg_wait_synced(dmu_objset_pool(os), txg);
2178 (void) dmu_free_long_range(os, object, offset, size);
2181 ztest_range_unlock(rl);
2182 ztest_object_unlock(zd, object);
2186 ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
2189 ztest_block_tag_t wbt;
2190 dmu_object_info_t doi;
2191 enum ztest_io_type io_type;
2195 VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
2196 blocksize = doi.doi_data_block_size;
2197 data = umem_alloc(blocksize, UMEM_NOFAIL);
2200 * Pick an i/o type at random, biased toward writing block tags.
2202 io_type = ztest_random(ZTEST_IO_TYPES);
2203 if (ztest_random(2) == 0)
2204 io_type = ZTEST_IO_WRITE_TAG;
2206 (void) rw_enter(&zd->zd_zilog_lock, RW_READER);
2210 case ZTEST_IO_WRITE_TAG:
2211 ztest_bt_generate(&wbt, zd->zd_os, object, offset, 0, 0, 0);
2212 (void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
2215 case ZTEST_IO_WRITE_PATTERN:
2216 (void) memset(data, 'a' + (object + offset) % 5, blocksize);
2217 if (ztest_random(2) == 0) {
2219 * Induce fletcher2 collisions to ensure that
2220 * zio_ddt_collision() detects and resolves them
2221 * when using fletcher2-verify for deduplication.
2223 ((uint64_t *)data)[0] ^= 1ULL << 63;
2224 ((uint64_t *)data)[4] ^= 1ULL << 63;
2226 (void) ztest_write(zd, object, offset, blocksize, data);
2229 case ZTEST_IO_WRITE_ZEROES:
2230 bzero(data, blocksize);
2231 (void) ztest_write(zd, object, offset, blocksize, data);
2234 case ZTEST_IO_TRUNCATE:
2235 (void) ztest_truncate(zd, object, offset, blocksize);
2238 case ZTEST_IO_SETATTR:
2239 (void) ztest_setattr(zd, object);
2244 case ZTEST_IO_REWRITE:
2245 (void) rw_enter(&ztest_name_lock, RW_READER);
2246 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2247 ZFS_PROP_CHECKSUM, spa_dedup_checksum(ztest_spa),
2249 VERIFY(err == 0 || err == ENOSPC);
2250 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2251 ZFS_PROP_COMPRESSION,
2252 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION),
2254 VERIFY(err == 0 || err == ENOSPC);
2255 (void) rw_exit(&ztest_name_lock);
2257 VERIFY0(dmu_read(zd->zd_os, object, offset, blocksize, data,
2258 DMU_READ_NO_PREFETCH));
2260 (void) ztest_write(zd, object, offset, blocksize, data);
2264 (void) rw_exit(&zd->zd_zilog_lock);
2266 umem_free(data, blocksize);
2270 * Initialize an object description template.
2273 ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
2274 dmu_object_type_t type, uint64_t blocksize, uint64_t gen)
2276 od->od_dir = ZTEST_DIROBJ;
2279 od->od_crtype = type;
2280 od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
2283 od->od_type = DMU_OT_NONE;
2284 od->od_blocksize = 0;
2287 (void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
2288 tag, (longlong_t)id, (u_longlong_t)index);
2292 * Lookup or create the objects for a test using the od template.
2293 * If the objects do not all exist, or if 'remove' is specified,
2294 * remove any existing objects and create new ones. Otherwise,
2295 * use the existing objects.
2298 ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
2300 int count = size / sizeof (*od);
2303 mutex_enter(&zd->zd_dirobj_lock);
2304 if ((ztest_lookup(zd, od, count) != 0 || remove) &&
2305 (ztest_remove(zd, od, count) != 0 ||
2306 ztest_create(zd, od, count) != 0))
2309 mutex_exit(&zd->zd_dirobj_lock);
2316 ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
2318 zilog_t *zilog = zd->zd_zilog;
2320 (void) rw_enter(&zd->zd_zilog_lock, RW_READER);
2322 zil_commit(zilog, ztest_random(ZTEST_OBJECTS));
2325 * Remember the committed values in zd, which is in parent/child
2326 * shared memory. If we die, the next iteration of ztest_run()
2327 * will verify that the log really does contain this record.
2329 mutex_enter(&zilog->zl_lock);
2330 ASSERT(zd->zd_shared != NULL);
2331 ASSERT3U(zd->zd_shared->zd_seq, <=, zilog->zl_commit_lr_seq);
2332 zd->zd_shared->zd_seq = zilog->zl_commit_lr_seq;
2333 mutex_exit(&zilog->zl_lock);
2335 (void) rw_exit(&zd->zd_zilog_lock);
2339 * This function is designed to simulate the operations that occur during a
2340 * mount/unmount operation. We hold the dataset across these operations in an
2341 * attempt to expose any implicit assumptions about ZIL management.
2345 ztest_zil_remount(ztest_ds_t *zd, uint64_t id)
2347 objset_t *os = zd->zd_os;
2350 * We grab the zd_dirobj_lock to ensure that no other thread is
2351 * updating the zil (i.e. adding in-memory log records) and the
2352 * zd_zilog_lock to block any I/O.
2354 mutex_enter(&zd->zd_dirobj_lock);
2355 (void) rw_enter(&zd->zd_zilog_lock, RW_WRITER);
2357 /* zfs_sb_teardown() */
2358 zil_close(zd->zd_zilog);
2360 /* zfsvfs_setup() */
2361 VERIFY(zil_open(os, ztest_get_data) == zd->zd_zilog);
2362 zil_replay(os, zd, ztest_replay_vector);
2364 (void) rw_exit(&zd->zd_zilog_lock);
2365 mutex_exit(&zd->zd_dirobj_lock);
2369 * Verify that we can't destroy an active pool, create an existing pool,
2370 * or create a pool with a bad vdev spec.
2374 ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2376 ztest_shared_opts_t *zo = &ztest_opts;
2381 * Attempt to create using a bad file.
2383 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2384 VERIFY3U(ENOENT, ==,
2385 spa_create("ztest_bad_file", nvroot, NULL, NULL));
2386 nvlist_free(nvroot);
2389 * Attempt to create using a bad mirror.
2391 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 2, 1);
2392 VERIFY3U(ENOENT, ==,
2393 spa_create("ztest_bad_mirror", nvroot, NULL, NULL));
2394 nvlist_free(nvroot);
2397 * Attempt to create an existing pool. It shouldn't matter
2398 * what's in the nvroot; we should fail with EEXIST.
2400 (void) rw_enter(&ztest_name_lock, RW_READER);
2401 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2402 VERIFY3U(EEXIST, ==, spa_create(zo->zo_pool, nvroot, NULL, NULL));
2403 nvlist_free(nvroot);
2404 VERIFY3U(0, ==, spa_open(zo->zo_pool, &spa, FTAG));
2405 VERIFY3U(EBUSY, ==, spa_destroy(zo->zo_pool));
2406 spa_close(spa, FTAG);
2408 (void) rw_exit(&ztest_name_lock);
2413 ztest_spa_upgrade(ztest_ds_t *zd, uint64_t id)
2416 uint64_t initial_version = SPA_VERSION_INITIAL;
2417 uint64_t version, newversion;
2418 nvlist_t *nvroot, *props;
2421 mutex_enter(&ztest_vdev_lock);
2422 name = kmem_asprintf("%s_upgrade", ztest_opts.zo_pool);
2425 * Clean up from previous runs.
2427 (void) spa_destroy(name);
2429 nvroot = make_vdev_root(NULL, NULL, name, ztest_opts.zo_vdev_size, 0,
2430 0, ztest_opts.zo_raidz, ztest_opts.zo_mirrors, 1);
2433 * If we're configuring a RAIDZ device then make sure that the
2434 * the initial version is capable of supporting that feature.
2436 switch (ztest_opts.zo_raidz_parity) {
2439 initial_version = SPA_VERSION_INITIAL;
2442 initial_version = SPA_VERSION_RAIDZ2;
2445 initial_version = SPA_VERSION_RAIDZ3;
2450 * Create a pool with a spa version that can be upgraded. Pick
2451 * a value between initial_version and SPA_VERSION_BEFORE_FEATURES.
2454 version = ztest_random_spa_version(initial_version);
2455 } while (version > SPA_VERSION_BEFORE_FEATURES);
2457 props = fnvlist_alloc();
2458 fnvlist_add_uint64(props,
2459 zpool_prop_to_name(ZPOOL_PROP_VERSION), version);
2460 VERIFY3S(spa_create(name, nvroot, props, NULL), ==, 0);
2461 fnvlist_free(nvroot);
2462 fnvlist_free(props);
2464 VERIFY3S(spa_open(name, &spa, FTAG), ==, 0);
2465 VERIFY3U(spa_version(spa), ==, version);
2466 newversion = ztest_random_spa_version(version + 1);
2468 if (ztest_opts.zo_verbose >= 4) {
2469 (void) printf("upgrading spa version from %llu to %llu\n",
2470 (u_longlong_t)version, (u_longlong_t)newversion);
2473 spa_upgrade(spa, newversion);
2474 VERIFY3U(spa_version(spa), >, version);
2475 VERIFY3U(spa_version(spa), ==, fnvlist_lookup_uint64(spa->spa_config,
2476 zpool_prop_to_name(ZPOOL_PROP_VERSION)));
2477 spa_close(spa, FTAG);
2480 mutex_exit(&ztest_vdev_lock);
2484 vdev_lookup_by_path(vdev_t *vd, const char *path)
2489 if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
2492 for (c = 0; c < vd->vdev_children; c++)
2493 if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
2501 * Find the first available hole which can be used as a top-level.
2504 find_vdev_hole(spa_t *spa)
2506 vdev_t *rvd = spa->spa_root_vdev;
2509 ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
2511 for (c = 0; c < rvd->vdev_children; c++) {
2512 vdev_t *cvd = rvd->vdev_child[c];
2514 if (cvd->vdev_ishole)
2521 * Verify that vdev_add() works as expected.
2525 ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2527 ztest_shared_t *zs = ztest_shared;
2528 spa_t *spa = ztest_spa;
2534 mutex_enter(&ztest_vdev_lock);
2535 leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * ztest_opts.zo_raidz;
2537 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2539 ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2542 * If we have slogs then remove them 1/4 of the time.
2544 if (spa_has_slogs(spa) && ztest_random(4) == 0) {
2546 * Grab the guid from the head of the log class rotor.
2548 guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid;
2550 spa_config_exit(spa, SCL_VDEV, FTAG);
2553 * We have to grab the zs_name_lock as writer to
2554 * prevent a race between removing a slog (dmu_objset_find)
2555 * and destroying a dataset. Removing the slog will
2556 * grab a reference on the dataset which may cause
2557 * dsl_destroy_head() to fail with EBUSY thus
2558 * leaving the dataset in an inconsistent state.
2560 rw_enter(&ztest_name_lock, RW_WRITER);
2561 error = spa_vdev_remove(spa, guid, B_FALSE);
2562 rw_exit(&ztest_name_lock);
2564 if (error && error != EEXIST)
2565 fatal(0, "spa_vdev_remove() = %d", error);
2567 spa_config_exit(spa, SCL_VDEV, FTAG);
2570 * Make 1/4 of the devices be log devices.
2572 nvroot = make_vdev_root(NULL, NULL, NULL,
2573 ztest_opts.zo_vdev_size, 0,
2574 ztest_random(4) == 0, ztest_opts.zo_raidz,
2577 error = spa_vdev_add(spa, nvroot);
2578 nvlist_free(nvroot);
2580 if (error == ENOSPC)
2581 ztest_record_enospc("spa_vdev_add");
2582 else if (error != 0)
2583 fatal(0, "spa_vdev_add() = %d", error);
2586 mutex_exit(&ztest_vdev_lock);
2590 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
2594 ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
2596 ztest_shared_t *zs = ztest_shared;
2597 spa_t *spa = ztest_spa;
2598 vdev_t *rvd = spa->spa_root_vdev;
2599 spa_aux_vdev_t *sav;
2605 path = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2607 if (ztest_random(2) == 0) {
2608 sav = &spa->spa_spares;
2609 aux = ZPOOL_CONFIG_SPARES;
2611 sav = &spa->spa_l2cache;
2612 aux = ZPOOL_CONFIG_L2CACHE;
2615 mutex_enter(&ztest_vdev_lock);
2617 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2619 if (sav->sav_count != 0 && ztest_random(4) == 0) {
2621 * Pick a random device to remove.
2623 guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
2626 * Find an unused device we can add.
2628 zs->zs_vdev_aux = 0;
2631 (void) snprintf(path, MAXPATHLEN, ztest_aux_template,
2632 ztest_opts.zo_dir, ztest_opts.zo_pool, aux,
2634 for (c = 0; c < sav->sav_count; c++)
2635 if (strcmp(sav->sav_vdevs[c]->vdev_path,
2638 if (c == sav->sav_count &&
2639 vdev_lookup_by_path(rvd, path) == NULL)
2645 spa_config_exit(spa, SCL_VDEV, FTAG);
2651 nvlist_t *nvroot = make_vdev_root(NULL, aux, NULL,
2652 (ztest_opts.zo_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
2653 error = spa_vdev_add(spa, nvroot);
2655 fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
2656 nvlist_free(nvroot);
2659 * Remove an existing device. Sometimes, dirty its
2660 * vdev state first to make sure we handle removal
2661 * of devices that have pending state changes.
2663 if (ztest_random(2) == 0)
2664 (void) vdev_online(spa, guid, 0, NULL);
2666 error = spa_vdev_remove(spa, guid, B_FALSE);
2667 if (error != 0 && error != EBUSY)
2668 fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
2671 mutex_exit(&ztest_vdev_lock);
2673 umem_free(path, MAXPATHLEN);
2677 * split a pool if it has mirror tlvdevs
2681 ztest_split_pool(ztest_ds_t *zd, uint64_t id)
2683 ztest_shared_t *zs = ztest_shared;
2684 spa_t *spa = ztest_spa;
2685 vdev_t *rvd = spa->spa_root_vdev;
2686 nvlist_t *tree, **child, *config, *split, **schild;
2687 uint_t c, children, schildren = 0, lastlogid = 0;
2690 mutex_enter(&ztest_vdev_lock);
2692 /* ensure we have a useable config; mirrors of raidz aren't supported */
2693 if (zs->zs_mirrors < 3 || ztest_opts.zo_raidz > 1) {
2694 mutex_exit(&ztest_vdev_lock);
2698 /* clean up the old pool, if any */
2699 (void) spa_destroy("splitp");
2701 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2703 /* generate a config from the existing config */
2704 mutex_enter(&spa->spa_props_lock);
2705 VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
2707 mutex_exit(&spa->spa_props_lock);
2709 VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2712 schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
2713 for (c = 0; c < children; c++) {
2714 vdev_t *tvd = rvd->vdev_child[c];
2718 if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
2719 VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
2721 VERIFY(nvlist_add_string(schild[schildren],
2722 ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
2723 VERIFY(nvlist_add_uint64(schild[schildren],
2724 ZPOOL_CONFIG_IS_HOLE, 1) == 0);
2726 lastlogid = schildren;
2731 VERIFY(nvlist_lookup_nvlist_array(child[c],
2732 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2733 VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
2736 /* OK, create a config that can be used to split */
2737 VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
2738 VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
2739 VDEV_TYPE_ROOT) == 0);
2740 VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
2741 lastlogid != 0 ? lastlogid : schildren) == 0);
2743 VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
2744 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
2746 for (c = 0; c < schildren; c++)
2747 nvlist_free(schild[c]);
2751 spa_config_exit(spa, SCL_VDEV, FTAG);
2753 (void) rw_enter(&ztest_name_lock, RW_WRITER);
2754 error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
2755 (void) rw_exit(&ztest_name_lock);
2757 nvlist_free(config);
2760 (void) printf("successful split - results:\n");
2761 mutex_enter(&spa_namespace_lock);
2762 show_pool_stats(spa);
2763 show_pool_stats(spa_lookup("splitp"));
2764 mutex_exit(&spa_namespace_lock);
2768 mutex_exit(&ztest_vdev_lock);
2773 * Verify that we can attach and detach devices.
2777 ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
2779 ztest_shared_t *zs = ztest_shared;
2780 spa_t *spa = ztest_spa;
2781 spa_aux_vdev_t *sav = &spa->spa_spares;
2782 vdev_t *rvd = spa->spa_root_vdev;
2783 vdev_t *oldvd, *newvd, *pvd;
2787 uint64_t ashift = ztest_get_ashift();
2788 uint64_t oldguid, pguid;
2789 uint64_t oldsize, newsize;
2790 char *oldpath, *newpath;
2792 int oldvd_has_siblings = B_FALSE;
2793 int newvd_is_spare = B_FALSE;
2795 int error, expected_error;
2797 oldpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2798 newpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2800 mutex_enter(&ztest_vdev_lock);
2801 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
2803 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2806 * Decide whether to do an attach or a replace.
2808 replacing = ztest_random(2);
2811 * Pick a random top-level vdev.
2813 top = ztest_random_vdev_top(spa, B_TRUE);
2816 * Pick a random leaf within it.
2818 leaf = ztest_random(leaves);
2823 oldvd = rvd->vdev_child[top];
2824 if (zs->zs_mirrors >= 1) {
2825 ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
2826 ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
2827 oldvd = oldvd->vdev_child[leaf / ztest_opts.zo_raidz];
2829 if (ztest_opts.zo_raidz > 1) {
2830 ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
2831 ASSERT(oldvd->vdev_children == ztest_opts.zo_raidz);
2832 oldvd = oldvd->vdev_child[leaf % ztest_opts.zo_raidz];
2836 * If we're already doing an attach or replace, oldvd may be a
2837 * mirror vdev -- in which case, pick a random child.
2839 while (oldvd->vdev_children != 0) {
2840 oldvd_has_siblings = B_TRUE;
2841 ASSERT(oldvd->vdev_children >= 2);
2842 oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
2845 oldguid = oldvd->vdev_guid;
2846 oldsize = vdev_get_min_asize(oldvd);
2847 oldvd_is_log = oldvd->vdev_top->vdev_islog;
2848 (void) strcpy(oldpath, oldvd->vdev_path);
2849 pvd = oldvd->vdev_parent;
2850 pguid = pvd->vdev_guid;
2853 * If oldvd has siblings, then half of the time, detach it.
2855 if (oldvd_has_siblings && ztest_random(2) == 0) {
2856 spa_config_exit(spa, SCL_VDEV, FTAG);
2857 error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
2858 if (error != 0 && error != ENODEV && error != EBUSY &&
2860 fatal(0, "detach (%s) returned %d", oldpath, error);
2865 * For the new vdev, choose with equal probability between the two
2866 * standard paths (ending in either 'a' or 'b') or a random hot spare.
2868 if (sav->sav_count != 0 && ztest_random(3) == 0) {
2869 newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
2870 newvd_is_spare = B_TRUE;
2871 (void) strcpy(newpath, newvd->vdev_path);
2873 (void) snprintf(newpath, MAXPATHLEN, ztest_dev_template,
2874 ztest_opts.zo_dir, ztest_opts.zo_pool,
2875 top * leaves + leaf);
2876 if (ztest_random(2) == 0)
2877 newpath[strlen(newpath) - 1] = 'b';
2878 newvd = vdev_lookup_by_path(rvd, newpath);
2882 newsize = vdev_get_min_asize(newvd);
2885 * Make newsize a little bigger or smaller than oldsize.
2886 * If it's smaller, the attach should fail.
2887 * If it's larger, and we're doing a replace,
2888 * we should get dynamic LUN growth when we're done.
2890 newsize = 10 * oldsize / (9 + ztest_random(3));
2894 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
2895 * unless it's a replace; in that case any non-replacing parent is OK.
2897 * If newvd is already part of the pool, it should fail with EBUSY.
2899 * If newvd is too small, it should fail with EOVERFLOW.
2901 if (pvd->vdev_ops != &vdev_mirror_ops &&
2902 pvd->vdev_ops != &vdev_root_ops && (!replacing ||
2903 pvd->vdev_ops == &vdev_replacing_ops ||
2904 pvd->vdev_ops == &vdev_spare_ops))
2905 expected_error = ENOTSUP;
2906 else if (newvd_is_spare && (!replacing || oldvd_is_log))
2907 expected_error = ENOTSUP;
2908 else if (newvd == oldvd)
2909 expected_error = replacing ? 0 : EBUSY;
2910 else if (vdev_lookup_by_path(rvd, newpath) != NULL)
2911 expected_error = EBUSY;
2912 else if (newsize < oldsize)
2913 expected_error = EOVERFLOW;
2914 else if (ashift > oldvd->vdev_top->vdev_ashift)
2915 expected_error = EDOM;
2919 spa_config_exit(spa, SCL_VDEV, FTAG);
2922 * Build the nvlist describing newpath.
2924 root = make_vdev_root(newpath, NULL, NULL, newvd == NULL ? newsize : 0,
2925 ashift, 0, 0, 0, 1);
2927 error = spa_vdev_attach(spa, oldguid, root, replacing);
2932 * If our parent was the replacing vdev, but the replace completed,
2933 * then instead of failing with ENOTSUP we may either succeed,
2934 * fail with ENODEV, or fail with EOVERFLOW.
2936 if (expected_error == ENOTSUP &&
2937 (error == 0 || error == ENODEV || error == EOVERFLOW))
2938 expected_error = error;
2941 * If someone grew the LUN, the replacement may be too small.
2943 if (error == EOVERFLOW || error == EBUSY)
2944 expected_error = error;
2946 /* XXX workaround 6690467 */
2947 if (error != expected_error && expected_error != EBUSY) {
2948 fatal(0, "attach (%s %llu, %s %llu, %d) "
2949 "returned %d, expected %d",
2950 oldpath, oldsize, newpath,
2951 newsize, replacing, error, expected_error);
2954 mutex_exit(&ztest_vdev_lock);
2956 umem_free(oldpath, MAXPATHLEN);
2957 umem_free(newpath, MAXPATHLEN);
2961 * Callback function which expands the physical size of the vdev.
2964 grow_vdev(vdev_t *vd, void *arg)
2966 ASSERTV(spa_t *spa = vd->vdev_spa);
2967 size_t *newsize = arg;
2971 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
2972 ASSERT(vd->vdev_ops->vdev_op_leaf);
2974 if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
2977 fsize = lseek(fd, 0, SEEK_END);
2978 VERIFY(ftruncate(fd, *newsize) == 0);
2980 if (ztest_opts.zo_verbose >= 6) {
2981 (void) printf("%s grew from %lu to %lu bytes\n",
2982 vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
2989 * Callback function which expands a given vdev by calling vdev_online().
2993 online_vdev(vdev_t *vd, void *arg)
2995 spa_t *spa = vd->vdev_spa;
2996 vdev_t *tvd = vd->vdev_top;
2997 uint64_t guid = vd->vdev_guid;
2998 uint64_t generation = spa->spa_config_generation + 1;
2999 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
3002 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3003 ASSERT(vd->vdev_ops->vdev_op_leaf);
3005 /* Calling vdev_online will initialize the new metaslabs */
3006 spa_config_exit(spa, SCL_STATE, spa);
3007 error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
3008 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3011 * If vdev_online returned an error or the underlying vdev_open
3012 * failed then we abort the expand. The only way to know that
3013 * vdev_open fails is by checking the returned newstate.
3015 if (error || newstate != VDEV_STATE_HEALTHY) {
3016 if (ztest_opts.zo_verbose >= 5) {
3017 (void) printf("Unable to expand vdev, state %llu, "
3018 "error %d\n", (u_longlong_t)newstate, error);
3022 ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
3025 * Since we dropped the lock we need to ensure that we're
3026 * still talking to the original vdev. It's possible this
3027 * vdev may have been detached/replaced while we were
3028 * trying to online it.
3030 if (generation != spa->spa_config_generation) {
3031 if (ztest_opts.zo_verbose >= 5) {
3032 (void) printf("vdev configuration has changed, "
3033 "guid %llu, state %llu, expected gen %llu, "
3036 (u_longlong_t)tvd->vdev_state,
3037 (u_longlong_t)generation,
3038 (u_longlong_t)spa->spa_config_generation);
3046 * Traverse the vdev tree calling the supplied function.
3047 * We continue to walk the tree until we either have walked all
3048 * children or we receive a non-NULL return from the callback.
3049 * If a NULL callback is passed, then we just return back the first
3050 * leaf vdev we encounter.
3053 vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
3057 if (vd->vdev_ops->vdev_op_leaf) {
3061 return (func(vd, arg));
3064 for (c = 0; c < vd->vdev_children; c++) {
3065 vdev_t *cvd = vd->vdev_child[c];
3066 if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
3073 * Verify that dynamic LUN growth works as expected.
3077 ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
3079 spa_t *spa = ztest_spa;
3081 metaslab_class_t *mc;
3082 metaslab_group_t *mg;
3083 size_t psize, newsize;
3085 uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
3087 mutex_enter(&ztest_vdev_lock);
3088 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3090 top = ztest_random_vdev_top(spa, B_TRUE);
3092 tvd = spa->spa_root_vdev->vdev_child[top];
3095 old_ms_count = tvd->vdev_ms_count;
3096 old_class_space = metaslab_class_get_space(mc);
3099 * Determine the size of the first leaf vdev associated with
3100 * our top-level device.
3102 vd = vdev_walk_tree(tvd, NULL, NULL);
3103 ASSERT3P(vd, !=, NULL);
3104 ASSERT(vd->vdev_ops->vdev_op_leaf);
3106 psize = vd->vdev_psize;
3109 * We only try to expand the vdev if it's healthy, less than 4x its
3110 * original size, and it has a valid psize.
3112 if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
3113 psize == 0 || psize >= 4 * ztest_opts.zo_vdev_size) {
3114 spa_config_exit(spa, SCL_STATE, spa);
3115 mutex_exit(&ztest_vdev_lock);
3119 newsize = psize + psize / 8;
3120 ASSERT3U(newsize, >, psize);
3122 if (ztest_opts.zo_verbose >= 6) {
3123 (void) printf("Expanding LUN %s from %lu to %lu\n",
3124 vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
3128 * Growing the vdev is a two step process:
3129 * 1). expand the physical size (i.e. relabel)
3130 * 2). online the vdev to create the new metaslabs
3132 if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
3133 vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
3134 tvd->vdev_state != VDEV_STATE_HEALTHY) {
3135 if (ztest_opts.zo_verbose >= 5) {
3136 (void) printf("Could not expand LUN because "
3137 "the vdev configuration changed.\n");
3139 spa_config_exit(spa, SCL_STATE, spa);
3140 mutex_exit(&ztest_vdev_lock);
3144 spa_config_exit(spa, SCL_STATE, spa);
3147 * Expanding the LUN will update the config asynchronously,
3148 * thus we must wait for the async thread to complete any
3149 * pending tasks before proceeding.
3153 mutex_enter(&spa->spa_async_lock);
3154 done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
3155 mutex_exit(&spa->spa_async_lock);
3158 txg_wait_synced(spa_get_dsl(spa), 0);
3159 (void) poll(NULL, 0, 100);
3162 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3164 tvd = spa->spa_root_vdev->vdev_child[top];
3165 new_ms_count = tvd->vdev_ms_count;
3166 new_class_space = metaslab_class_get_space(mc);
3168 if (tvd->vdev_mg != mg || mg->mg_class != mc) {
3169 if (ztest_opts.zo_verbose >= 5) {
3170 (void) printf("Could not verify LUN expansion due to "
3171 "intervening vdev offline or remove.\n");
3173 spa_config_exit(spa, SCL_STATE, spa);
3174 mutex_exit(&ztest_vdev_lock);
3179 * Make sure we were able to grow the vdev.
3181 if (new_ms_count <= old_ms_count)
3182 fatal(0, "LUN expansion failed: ms_count %llu <= %llu\n",
3183 old_ms_count, new_ms_count);
3186 * Make sure we were able to grow the pool.
3188 if (new_class_space <= old_class_space)
3189 fatal(0, "LUN expansion failed: class_space %llu <= %llu\n",
3190 old_class_space, new_class_space);
3192 if (ztest_opts.zo_verbose >= 5) {
3193 char oldnumbuf[6], newnumbuf[6];
3195 nicenum(old_class_space, oldnumbuf);
3196 nicenum(new_class_space, newnumbuf);
3197 (void) printf("%s grew from %s to %s\n",
3198 spa->spa_name, oldnumbuf, newnumbuf);
3201 spa_config_exit(spa, SCL_STATE, spa);
3202 mutex_exit(&ztest_vdev_lock);
3206 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
3210 ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3213 * Create the objects common to all ztest datasets.
3215 VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
3216 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
3220 ztest_dataset_create(char *dsname)
3222 uint64_t zilset = ztest_random(100);
3223 int err = dmu_objset_create(dsname, DMU_OST_OTHER, 0,
3224 ztest_objset_create_cb, NULL);
3226 if (err || zilset < 80)
3229 if (ztest_opts.zo_verbose >= 5)
3230 (void) printf("Setting dataset %s to sync always\n", dsname);
3231 return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC,
3232 ZFS_SYNC_ALWAYS, B_FALSE));
3237 ztest_objset_destroy_cb(const char *name, void *arg)
3240 dmu_object_info_t doi;
3244 * Verify that the dataset contains a directory object.
3246 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_TRUE, FTAG, &os));
3247 error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
3248 if (error != ENOENT) {
3249 /* We could have crashed in the middle of destroying it */
3251 ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
3252 ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
3254 dmu_objset_disown(os, FTAG);
3257 * Destroy the dataset.
3259 if (strchr(name, '@') != NULL) {
3260 VERIFY0(dsl_destroy_snapshot(name, B_FALSE));
3262 VERIFY0(dsl_destroy_head(name));
3268 ztest_snapshot_create(char *osname, uint64_t id)
3270 char snapname[MAXNAMELEN];
3273 (void) snprintf(snapname, sizeof (snapname), "%llu", (u_longlong_t)id);
3275 error = dmu_objset_snapshot_one(osname, snapname);
3276 if (error == ENOSPC) {
3277 ztest_record_enospc(FTAG);
3280 if (error != 0 && error != EEXIST) {
3281 fatal(0, "ztest_snapshot_create(%s@%s) = %d", osname,
3288 ztest_snapshot_destroy(char *osname, uint64_t id)
3290 char snapname[MAXNAMELEN];
3293 (void) snprintf(snapname, MAXNAMELEN, "%s@%llu", osname,
3296 error = dsl_destroy_snapshot(snapname, B_FALSE);
3297 if (error != 0 && error != ENOENT)
3298 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
3304 ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
3314 zdtmp = umem_alloc(sizeof (ztest_ds_t), UMEM_NOFAIL);
3315 name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3317 (void) rw_enter(&ztest_name_lock, RW_READER);
3319 (void) snprintf(name, MAXNAMELEN, "%s/temp_%llu",
3320 ztest_opts.zo_pool, (u_longlong_t)id);
3323 * If this dataset exists from a previous run, process its replay log
3324 * half of the time. If we don't replay it, then dsl_destroy_head()
3325 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
3327 if (ztest_random(2) == 0 &&
3328 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) {
3329 ztest_zd_init(zdtmp, NULL, os);
3330 zil_replay(os, zdtmp, ztest_replay_vector);
3331 ztest_zd_fini(zdtmp);
3332 dmu_objset_disown(os, FTAG);
3336 * There may be an old instance of the dataset we're about to
3337 * create lying around from a previous run. If so, destroy it
3338 * and all of its snapshots.
3340 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
3341 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
3344 * Verify that the destroyed dataset is no longer in the namespace.
3346 VERIFY3U(ENOENT, ==, dmu_objset_own(name, DMU_OST_OTHER, B_TRUE,
3350 * Verify that we can create a new dataset.
3352 error = ztest_dataset_create(name);
3354 if (error == ENOSPC) {
3355 ztest_record_enospc(FTAG);
3358 fatal(0, "dmu_objset_create(%s) = %d", name, error);
3361 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os));
3363 ztest_zd_init(zdtmp, NULL, os);
3366 * Open the intent log for it.
3368 zilog = zil_open(os, ztest_get_data);
3371 * Put some objects in there, do a little I/O to them,
3372 * and randomly take a couple of snapshots along the way.
3374 iters = ztest_random(5);
3375 for (i = 0; i < iters; i++) {
3376 ztest_dmu_object_alloc_free(zdtmp, id);
3377 if (ztest_random(iters) == 0)
3378 (void) ztest_snapshot_create(name, i);
3382 * Verify that we cannot create an existing dataset.
3384 VERIFY3U(EEXIST, ==,
3385 dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL));
3388 * Verify that we can hold an objset that is also owned.
3390 VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
3391 dmu_objset_rele(os2, FTAG);
3394 * Verify that we cannot own an objset that is already owned.
3397 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2));
3400 dmu_objset_disown(os, FTAG);
3401 ztest_zd_fini(zdtmp);
3403 (void) rw_exit(&ztest_name_lock);
3405 umem_free(name, MAXNAMELEN);
3406 umem_free(zdtmp, sizeof (ztest_ds_t));
3410 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3413 ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
3415 (void) rw_enter(&ztest_name_lock, RW_READER);
3416 (void) ztest_snapshot_destroy(zd->zd_name, id);
3417 (void) ztest_snapshot_create(zd->zd_name, id);
3418 (void) rw_exit(&ztest_name_lock);
3422 * Cleanup non-standard snapshots and clones.
3425 ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
3434 snap1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3435 clone1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3436 snap2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3437 clone2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3438 snap3name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3440 (void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu",
3441 osname, (u_longlong_t)id);
3442 (void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu",
3443 osname, (u_longlong_t)id);
3444 (void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu",
3445 clone1name, (u_longlong_t)id);
3446 (void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu",
3447 osname, (u_longlong_t)id);
3448 (void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu",
3449 clone1name, (u_longlong_t)id);
3451 error = dsl_destroy_head(clone2name);
3452 if (error && error != ENOENT)
3453 fatal(0, "dsl_destroy_head(%s) = %d", clone2name, error);
3454 error = dsl_destroy_snapshot(snap3name, B_FALSE);
3455 if (error && error != ENOENT)
3456 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap3name, error);
3457 error = dsl_destroy_snapshot(snap2name, B_FALSE);
3458 if (error && error != ENOENT)
3459 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap2name, error);
3460 error = dsl_destroy_head(clone1name);
3461 if (error && error != ENOENT)
3462 fatal(0, "dsl_destroy_head(%s) = %d", clone1name, error);
3463 error = dsl_destroy_snapshot(snap1name, B_FALSE);
3464 if (error && error != ENOENT)
3465 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap1name, error);
3467 umem_free(snap1name, MAXNAMELEN);
3468 umem_free(clone1name, MAXNAMELEN);
3469 umem_free(snap2name, MAXNAMELEN);
3470 umem_free(clone2name, MAXNAMELEN);
3471 umem_free(snap3name, MAXNAMELEN);
3475 * Verify dsl_dataset_promote handles EBUSY
3478 ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
3486 char *osname = zd->zd_name;
3489 snap1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3490 clone1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3491 snap2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3492 clone2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3493 snap3name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3495 (void) rw_enter(&ztest_name_lock, RW_READER);
3497 ztest_dsl_dataset_cleanup(osname, id);
3499 (void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu",
3500 osname, (u_longlong_t)id);
3501 (void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu",
3502 osname, (u_longlong_t)id);
3503 (void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu",
3504 clone1name, (u_longlong_t)id);
3505 (void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu",
3506 osname, (u_longlong_t)id);
3507 (void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu",
3508 clone1name, (u_longlong_t)id);
3510 error = dmu_objset_snapshot_one(osname, strchr(snap1name, '@') + 1);
3511 if (error && error != EEXIST) {
3512 if (error == ENOSPC) {
3513 ztest_record_enospc(FTAG);
3516 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
3519 error = dmu_objset_clone(clone1name, snap1name);
3521 if (error == ENOSPC) {
3522 ztest_record_enospc(FTAG);
3525 fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
3528 error = dmu_objset_snapshot_one(clone1name, strchr(snap2name, '@') + 1);
3529 if (error && error != EEXIST) {
3530 if (error == ENOSPC) {
3531 ztest_record_enospc(FTAG);
3534 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
3537 error = dmu_objset_snapshot_one(clone1name, strchr(snap3name, '@') + 1);
3538 if (error && error != EEXIST) {
3539 if (error == ENOSPC) {
3540 ztest_record_enospc(FTAG);
3543 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
3546 error = dmu_objset_clone(clone2name, snap3name);
3548 if (error == ENOSPC) {
3549 ztest_record_enospc(FTAG);
3552 fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
3555 error = dmu_objset_own(snap2name, DMU_OST_ANY, B_TRUE, FTAG, &os);
3557 fatal(0, "dmu_objset_own(%s) = %d", snap2name, error);
3558 error = dsl_dataset_promote(clone2name, NULL);
3560 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
3562 dmu_objset_disown(os, FTAG);
3565 ztest_dsl_dataset_cleanup(osname, id);
3567 (void) rw_exit(&ztest_name_lock);
3569 umem_free(snap1name, MAXNAMELEN);
3570 umem_free(clone1name, MAXNAMELEN);
3571 umem_free(snap2name, MAXNAMELEN);
3572 umem_free(clone2name, MAXNAMELEN);
3573 umem_free(snap3name, MAXNAMELEN);
3576 #undef OD_ARRAY_SIZE
3577 #define OD_ARRAY_SIZE 4
3580 * Verify that dmu_object_{alloc,free} work as expected.
3583 ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
3590 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
3591 od = umem_alloc(size, UMEM_NOFAIL);
3592 batchsize = OD_ARRAY_SIZE;
3594 for (b = 0; b < batchsize; b++)
3595 ztest_od_init(od + b, id, FTAG, b, DMU_OT_UINT64_OTHER, 0, 0);
3598 * Destroy the previous batch of objects, create a new batch,
3599 * and do some I/O on the new objects.
3601 if (ztest_object_init(zd, od, size, B_TRUE) != 0)
3604 while (ztest_random(4 * batchsize) != 0)
3605 ztest_io(zd, od[ztest_random(batchsize)].od_object,
3606 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3608 umem_free(od, size);
3611 #undef OD_ARRAY_SIZE
3612 #define OD_ARRAY_SIZE 2
3615 * Verify that dmu_{read,write} work as expected.
3618 ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
3623 objset_t *os = zd->zd_os;
3624 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
3625 od = umem_alloc(size, UMEM_NOFAIL);
3627 int i, freeit, error;
3629 bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
3630 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3631 uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
3632 uint64_t regions = 997;
3633 uint64_t stride = 123456789ULL;
3634 uint64_t width = 40;
3635 int free_percent = 5;
3638 * This test uses two objects, packobj and bigobj, that are always
3639 * updated together (i.e. in the same tx) so that their contents are
3640 * in sync and can be compared. Their contents relate to each other
3641 * in a simple way: packobj is a dense array of 'bufwad' structures,
3642 * while bigobj is a sparse array of the same bufwads. Specifically,
3643 * for any index n, there are three bufwads that should be identical:
3645 * packobj, at offset n * sizeof (bufwad_t)
3646 * bigobj, at the head of the nth chunk
3647 * bigobj, at the tail of the nth chunk
3649 * The chunk size is arbitrary. It doesn't have to be a power of two,
3650 * and it doesn't have any relation to the object blocksize.
3651 * The only requirement is that it can hold at least two bufwads.
3653 * Normally, we write the bufwad to each of these locations.
3654 * However, free_percent of the time we instead write zeroes to
3655 * packobj and perform a dmu_free_range() on bigobj. By comparing
3656 * bigobj to packobj, we can verify that the DMU is correctly
3657 * tracking which parts of an object are allocated and free,
3658 * and that the contents of the allocated blocks are correct.
3662 * Read the directory info. If it's the first time, set things up.
3664 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, chunksize);
3665 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
3667 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
3668 umem_free(od, size);
3672 bigobj = od[0].od_object;
3673 packobj = od[1].od_object;
3674 chunksize = od[0].od_gen;
3675 ASSERT(chunksize == od[1].od_gen);
3678 * Prefetch a random chunk of the big object.
3679 * Our aim here is to get some async reads in flight
3680 * for blocks that we may free below; the DMU should
3681 * handle this race correctly.
3683 n = ztest_random(regions) * stride + ztest_random(width);
3684 s = 1 + ztest_random(2 * width - 1);
3685 dmu_prefetch(os, bigobj, n * chunksize, s * chunksize);
3688 * Pick a random index and compute the offsets into packobj and bigobj.
3690 n = ztest_random(regions) * stride + ztest_random(width);
3691 s = 1 + ztest_random(width - 1);
3693 packoff = n * sizeof (bufwad_t);
3694 packsize = s * sizeof (bufwad_t);
3696 bigoff = n * chunksize;
3697 bigsize = s * chunksize;
3699 packbuf = umem_alloc(packsize, UMEM_NOFAIL);
3700 bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
3703 * free_percent of the time, free a range of bigobj rather than
3706 freeit = (ztest_random(100) < free_percent);
3709 * Read the current contents of our objects.
3711 error = dmu_read(os, packobj, packoff, packsize, packbuf,
3714 error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
3719 * Get a tx for the mods to both packobj and bigobj.
3721 tx = dmu_tx_create(os);
3723 dmu_tx_hold_write(tx, packobj, packoff, packsize);
3726 dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
3728 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
3730 /* This accounts for setting the checksum/compression. */
3731 dmu_tx_hold_bonus(tx, bigobj);
3733 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3735 umem_free(packbuf, packsize);
3736 umem_free(bigbuf, bigsize);
3737 umem_free(od, size);
3741 dmu_object_set_checksum(os, bigobj,
3742 (enum zio_checksum)ztest_random_dsl_prop(ZFS_PROP_CHECKSUM), tx);
3744 dmu_object_set_compress(os, bigobj,
3745 (enum zio_compress)ztest_random_dsl_prop(ZFS_PROP_COMPRESSION), tx);
3748 * For each index from n to n + s, verify that the existing bufwad
3749 * in packobj matches the bufwads at the head and tail of the
3750 * corresponding chunk in bigobj. Then update all three bufwads
3751 * with the new values we want to write out.
3753 for (i = 0; i < s; i++) {
3755 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3757 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
3759 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
3761 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3762 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3764 if (pack->bw_txg > txg)
3765 fatal(0, "future leak: got %llx, open txg is %llx",
3768 if (pack->bw_data != 0 && pack->bw_index != n + i)
3769 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3770 pack->bw_index, n, i);
3772 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3773 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
3775 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
3776 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
3779 bzero(pack, sizeof (bufwad_t));
3781 pack->bw_index = n + i;
3783 pack->bw_data = 1 + ztest_random(-2ULL);
3790 * We've verified all the old bufwads, and made new ones.
3791 * Now write them out.
3793 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
3796 if (ztest_opts.zo_verbose >= 7) {
3797 (void) printf("freeing offset %llx size %llx"
3799 (u_longlong_t)bigoff,
3800 (u_longlong_t)bigsize,
3803 VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
3805 if (ztest_opts.zo_verbose >= 7) {
3806 (void) printf("writing offset %llx size %llx"
3808 (u_longlong_t)bigoff,
3809 (u_longlong_t)bigsize,
3812 dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
3818 * Sanity check the stuff we just wrote.
3821 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
3822 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
3824 VERIFY(0 == dmu_read(os, packobj, packoff,
3825 packsize, packcheck, DMU_READ_PREFETCH));
3826 VERIFY(0 == dmu_read(os, bigobj, bigoff,
3827 bigsize, bigcheck, DMU_READ_PREFETCH));
3829 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
3830 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
3832 umem_free(packcheck, packsize);
3833 umem_free(bigcheck, bigsize);
3836 umem_free(packbuf, packsize);
3837 umem_free(bigbuf, bigsize);
3838 umem_free(od, size);
3842 compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
3843 uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
3851 * For each index from n to n + s, verify that the existing bufwad
3852 * in packobj matches the bufwads at the head and tail of the
3853 * corresponding chunk in bigobj. Then update all three bufwads
3854 * with the new values we want to write out.
3856 for (i = 0; i < s; i++) {
3858 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3860 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
3862 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
3864 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3865 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3867 if (pack->bw_txg > txg)
3868 fatal(0, "future leak: got %llx, open txg is %llx",
3871 if (pack->bw_data != 0 && pack->bw_index != n + i)
3872 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3873 pack->bw_index, n, i);
3875 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3876 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
3878 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
3879 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
3881 pack->bw_index = n + i;
3883 pack->bw_data = 1 + ztest_random(-2ULL);
3890 #undef OD_ARRAY_SIZE
3891 #define OD_ARRAY_SIZE 2
3894 ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
3896 objset_t *os = zd->zd_os;
3903 bufwad_t *packbuf, *bigbuf;
3904 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3905 uint64_t blocksize = ztest_random_blocksize();
3906 uint64_t chunksize = blocksize;
3907 uint64_t regions = 997;
3908 uint64_t stride = 123456789ULL;
3910 dmu_buf_t *bonus_db;
3911 arc_buf_t **bigbuf_arcbufs;
3912 dmu_object_info_t doi;
3914 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
3915 od = umem_alloc(size, UMEM_NOFAIL);
3918 * This test uses two objects, packobj and bigobj, that are always
3919 * updated together (i.e. in the same tx) so that their contents are
3920 * in sync and can be compared. Their contents relate to each other
3921 * in a simple way: packobj is a dense array of 'bufwad' structures,
3922 * while bigobj is a sparse array of the same bufwads. Specifically,
3923 * for any index n, there are three bufwads that should be identical:
3925 * packobj, at offset n * sizeof (bufwad_t)
3926 * bigobj, at the head of the nth chunk
3927 * bigobj, at the tail of the nth chunk
3929 * The chunk size is set equal to bigobj block size so that
3930 * dmu_assign_arcbuf() can be tested for object updates.
3934 * Read the directory info. If it's the first time, set things up.
3936 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
3937 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
3940 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
3941 umem_free(od, size);
3945 bigobj = od[0].od_object;
3946 packobj = od[1].od_object;
3947 blocksize = od[0].od_blocksize;
3948 chunksize = blocksize;
3949 ASSERT(chunksize == od[1].od_gen);
3951 VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
3952 VERIFY(ISP2(doi.doi_data_block_size));
3953 VERIFY(chunksize == doi.doi_data_block_size);
3954 VERIFY(chunksize >= 2 * sizeof (bufwad_t));
3957 * Pick a random index and compute the offsets into packobj and bigobj.
3959 n = ztest_random(regions) * stride + ztest_random(width);
3960 s = 1 + ztest_random(width - 1);
3962 packoff = n * sizeof (bufwad_t);
3963 packsize = s * sizeof (bufwad_t);
3965 bigoff = n * chunksize;
3966 bigsize = s * chunksize;
3968 packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
3969 bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
3971 VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
3973 bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
3976 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
3977 * Iteration 1 test zcopy to already referenced dbufs.
3978 * Iteration 2 test zcopy to dirty dbuf in the same txg.
3979 * Iteration 3 test zcopy to dbuf dirty in previous txg.
3980 * Iteration 4 test zcopy when dbuf is no longer dirty.
3981 * Iteration 5 test zcopy when it can't be done.
3982 * Iteration 6 one more zcopy write.
3984 for (i = 0; i < 7; i++) {
3989 * In iteration 5 (i == 5) use arcbufs
3990 * that don't match bigobj blksz to test
3991 * dmu_assign_arcbuf() when it can't directly
3992 * assign an arcbuf to a dbuf.
3994 for (j = 0; j < s; j++) {
3997 dmu_request_arcbuf(bonus_db, chunksize);
3999 bigbuf_arcbufs[2 * j] =
4000 dmu_request_arcbuf(bonus_db, chunksize / 2);
4001 bigbuf_arcbufs[2 * j + 1] =
4002 dmu_request_arcbuf(bonus_db, chunksize / 2);
4007 * Get a tx for the mods to both packobj and bigobj.
4009 tx = dmu_tx_create(os);
4011 dmu_tx_hold_write(tx, packobj, packoff, packsize);
4012 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
4014 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4016 umem_free(packbuf, packsize);
4017 umem_free(bigbuf, bigsize);
4018 for (j = 0; j < s; j++) {
4020 dmu_return_arcbuf(bigbuf_arcbufs[j]);
4023 bigbuf_arcbufs[2 * j]);
4025 bigbuf_arcbufs[2 * j + 1]);
4028 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4029 umem_free(od, size);
4030 dmu_buf_rele(bonus_db, FTAG);
4035 * 50% of the time don't read objects in the 1st iteration to
4036 * test dmu_assign_arcbuf() for the case when there're no
4037 * existing dbufs for the specified offsets.
4039 if (i != 0 || ztest_random(2) != 0) {
4040 error = dmu_read(os, packobj, packoff,
4041 packsize, packbuf, DMU_READ_PREFETCH);
4043 error = dmu_read(os, bigobj, bigoff, bigsize,
4044 bigbuf, DMU_READ_PREFETCH);
4047 compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
4051 * We've verified all the old bufwads, and made new ones.
4052 * Now write them out.
4054 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
4055 if (ztest_opts.zo_verbose >= 7) {
4056 (void) printf("writing offset %llx size %llx"
4058 (u_longlong_t)bigoff,
4059 (u_longlong_t)bigsize,
4062 for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
4065 bcopy((caddr_t)bigbuf + (off - bigoff),
4066 bigbuf_arcbufs[j]->b_data, chunksize);
4068 bcopy((caddr_t)bigbuf + (off - bigoff),
4069 bigbuf_arcbufs[2 * j]->b_data,
4071 bcopy((caddr_t)bigbuf + (off - bigoff) +
4073 bigbuf_arcbufs[2 * j + 1]->b_data,
4078 VERIFY(dmu_buf_hold(os, bigobj, off,
4079 FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0);
4082 dmu_assign_arcbuf(bonus_db, off,
4083 bigbuf_arcbufs[j], tx);
4085 dmu_assign_arcbuf(bonus_db, off,
4086 bigbuf_arcbufs[2 * j], tx);
4087 dmu_assign_arcbuf(bonus_db,
4088 off + chunksize / 2,
4089 bigbuf_arcbufs[2 * j + 1], tx);
4092 dmu_buf_rele(dbt, FTAG);
4098 * Sanity check the stuff we just wrote.
4101 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
4102 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
4104 VERIFY(0 == dmu_read(os, packobj, packoff,
4105 packsize, packcheck, DMU_READ_PREFETCH));
4106 VERIFY(0 == dmu_read(os, bigobj, bigoff,
4107 bigsize, bigcheck, DMU_READ_PREFETCH));
4109 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
4110 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
4112 umem_free(packcheck, packsize);
4113 umem_free(bigcheck, bigsize);
4116 txg_wait_open(dmu_objset_pool(os), 0);
4117 } else if (i == 3) {
4118 txg_wait_synced(dmu_objset_pool(os), 0);
4122 dmu_buf_rele(bonus_db, FTAG);
4123 umem_free(packbuf, packsize);
4124 umem_free(bigbuf, bigsize);
4125 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4126 umem_free(od, size);
4131 ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
4135 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4136 uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
4137 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4140 * Have multiple threads write to large offsets in an object
4141 * to verify that parallel writes to an object -- even to the
4142 * same blocks within the object -- doesn't cause any trouble.
4144 ztest_od_init(od, ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
4146 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0)
4149 while (ztest_random(10) != 0)
4150 ztest_io(zd, od->od_object, offset);
4152 umem_free(od, sizeof (ztest_od_t));
4156 ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
4159 uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
4160 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4161 uint64_t count = ztest_random(20) + 1;
4162 uint64_t blocksize = ztest_random_blocksize();
4165 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4167 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
4169 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4170 !ztest_random(2)) != 0) {
4171 umem_free(od, sizeof (ztest_od_t));
4175 if (ztest_truncate(zd, od->od_object, offset, count * blocksize) != 0) {
4176 umem_free(od, sizeof (ztest_od_t));
4180 ztest_prealloc(zd, od->od_object, offset, count * blocksize);
4182 data = umem_zalloc(blocksize, UMEM_NOFAIL);
4184 while (ztest_random(count) != 0) {
4185 uint64_t randoff = offset + (ztest_random(count) * blocksize);
4186 if (ztest_write(zd, od->od_object, randoff, blocksize,
4189 while (ztest_random(4) != 0)
4190 ztest_io(zd, od->od_object, randoff);
4193 umem_free(data, blocksize);
4194 umem_free(od, sizeof (ztest_od_t));
4198 * Verify that zap_{create,destroy,add,remove,update} work as expected.
4200 #define ZTEST_ZAP_MIN_INTS 1
4201 #define ZTEST_ZAP_MAX_INTS 4
4202 #define ZTEST_ZAP_MAX_PROPS 1000
4205 ztest_zap(ztest_ds_t *zd, uint64_t id)
4207 objset_t *os = zd->zd_os;
4210 uint64_t txg, last_txg;
4211 uint64_t value[ZTEST_ZAP_MAX_INTS];
4212 uint64_t zl_ints, zl_intsize, prop;
4215 char propname[100], txgname[100];
4217 char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
4219 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4220 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
4222 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4223 !ztest_random(2)) != 0)
4226 object = od->od_object;
4229 * Generate a known hash collision, and verify that
4230 * we can lookup and remove both entries.
4232 tx = dmu_tx_create(os);
4233 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4234 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4237 for (i = 0; i < 2; i++) {
4239 VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
4242 for (i = 0; i < 2; i++) {
4243 VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
4244 sizeof (uint64_t), 1, &value[i], tx));
4246 zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
4247 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4248 ASSERT3U(zl_ints, ==, 1);
4250 for (i = 0; i < 2; i++) {
4251 VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
4256 * Generate a buch of random entries.
4258 ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
4260 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4261 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4262 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4263 bzero(value, sizeof (value));
4267 * If these zap entries already exist, validate their contents.
4269 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4271 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4272 ASSERT3U(zl_ints, ==, 1);
4274 VERIFY(zap_lookup(os, object, txgname, zl_intsize,
4275 zl_ints, &last_txg) == 0);
4277 VERIFY(zap_length(os, object, propname, &zl_intsize,
4280 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4281 ASSERT3U(zl_ints, ==, ints);
4283 VERIFY(zap_lookup(os, object, propname, zl_intsize,
4284 zl_ints, value) == 0);
4286 for (i = 0; i < ints; i++) {
4287 ASSERT3U(value[i], ==, last_txg + object + i);
4290 ASSERT3U(error, ==, ENOENT);
4294 * Atomically update two entries in our zap object.
4295 * The first is named txg_%llu, and contains the txg
4296 * in which the property was last updated. The second
4297 * is named prop_%llu, and the nth element of its value
4298 * should be txg + object + n.
4300 tx = dmu_tx_create(os);
4301 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4302 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4307 fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
4309 for (i = 0; i < ints; i++)
4310 value[i] = txg + object + i;
4312 VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
4314 VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
4320 * Remove a random pair of entries.
4322 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4323 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4324 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4326 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4328 if (error == ENOENT)
4333 tx = dmu_tx_create(os);
4334 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4335 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4338 VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
4339 VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
4342 umem_free(od, sizeof (ztest_od_t));
4346 * Testcase to test the upgrading of a microzap to fatzap.
4349 ztest_fzap(ztest_ds_t *zd, uint64_t id)
4351 objset_t *os = zd->zd_os;
4353 uint64_t object, txg;
4356 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4357 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
4359 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4360 !ztest_random(2)) != 0)
4362 object = od->od_object;
4365 * Add entries to this ZAP and make sure it spills over
4366 * and gets upgraded to a fatzap. Also, since we are adding
4367 * 2050 entries we should see ptrtbl growth and leaf-block split.
4369 for (i = 0; i < 2050; i++) {
4370 char name[MAXNAMELEN];
4375 (void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
4376 (u_longlong_t)id, (u_longlong_t)value);
4378 tx = dmu_tx_create(os);
4379 dmu_tx_hold_zap(tx, object, B_TRUE, name);
4380 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4383 error = zap_add(os, object, name, sizeof (uint64_t), 1,
4385 ASSERT(error == 0 || error == EEXIST);
4389 umem_free(od, sizeof (ztest_od_t));
4394 ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
4396 objset_t *os = zd->zd_os;
4398 uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
4400 int i, namelen, error;
4401 int micro = ztest_random(2);
4402 char name[20], string_value[20];
4405 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4406 ztest_od_init(od, ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER, 0, 0);
4408 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
4409 umem_free(od, sizeof (ztest_od_t));
4413 object = od->od_object;
4416 * Generate a random name of the form 'xxx.....' where each
4417 * x is a random printable character and the dots are dots.
4418 * There are 94 such characters, and the name length goes from
4419 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
4421 namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
4423 for (i = 0; i < 3; i++)
4424 name[i] = '!' + ztest_random('~' - '!' + 1);
4425 for (; i < namelen - 1; i++)
4429 if ((namelen & 1) || micro) {
4430 wsize = sizeof (txg);
4436 data = string_value;
4440 VERIFY0(zap_count(os, object, &count));
4441 ASSERT(count != -1ULL);
4444 * Select an operation: length, lookup, add, update, remove.
4446 i = ztest_random(5);
4449 tx = dmu_tx_create(os);
4450 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4451 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4454 bcopy(name, string_value, namelen);
4458 bzero(string_value, namelen);
4464 error = zap_length(os, object, name, &zl_wsize, &zl_wc);
4466 ASSERT3U(wsize, ==, zl_wsize);
4467 ASSERT3U(wc, ==, zl_wc);
4469 ASSERT3U(error, ==, ENOENT);
4474 error = zap_lookup(os, object, name, wsize, wc, data);
4476 if (data == string_value &&
4477 bcmp(name, data, namelen) != 0)
4478 fatal(0, "name '%s' != val '%s' len %d",
4479 name, data, namelen);
4481 ASSERT3U(error, ==, ENOENT);
4486 error = zap_add(os, object, name, wsize, wc, data, tx);
4487 ASSERT(error == 0 || error == EEXIST);
4491 VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
4495 error = zap_remove(os, object, name, tx);
4496 ASSERT(error == 0 || error == ENOENT);
4503 umem_free(od, sizeof (ztest_od_t));
4507 * Commit callback data.
4509 typedef struct ztest_cb_data {
4510 list_node_t zcd_node;
4512 int zcd_expected_err;
4513 boolean_t zcd_added;
4514 boolean_t zcd_called;
4518 /* This is the actual commit callback function */
4520 ztest_commit_callback(void *arg, int error)
4522 ztest_cb_data_t *data = arg;
4523 uint64_t synced_txg;
4525 VERIFY(data != NULL);
4526 VERIFY3S(data->zcd_expected_err, ==, error);
4527 VERIFY(!data->zcd_called);
4529 synced_txg = spa_last_synced_txg(data->zcd_spa);
4530 if (data->zcd_txg > synced_txg)
4531 fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
4532 ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
4535 data->zcd_called = B_TRUE;
4537 if (error == ECANCELED) {
4538 ASSERT0(data->zcd_txg);
4539 ASSERT(!data->zcd_added);
4542 * The private callback data should be destroyed here, but
4543 * since we are going to check the zcd_called field after
4544 * dmu_tx_abort(), we will destroy it there.
4549 ASSERT(data->zcd_added);
4550 ASSERT3U(data->zcd_txg, !=, 0);
4552 (void) mutex_enter(&zcl.zcl_callbacks_lock);
4554 /* See if this cb was called more quickly */
4555 if ((synced_txg - data->zcd_txg) < zc_min_txg_delay)
4556 zc_min_txg_delay = synced_txg - data->zcd_txg;
4558 /* Remove our callback from the list */
4559 list_remove(&zcl.zcl_callbacks, data);
4561 (void) mutex_exit(&zcl.zcl_callbacks_lock);
4563 umem_free(data, sizeof (ztest_cb_data_t));
4566 /* Allocate and initialize callback data structure */
4567 static ztest_cb_data_t *
4568 ztest_create_cb_data(objset_t *os, uint64_t txg)
4570 ztest_cb_data_t *cb_data;
4572 cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
4574 cb_data->zcd_txg = txg;
4575 cb_data->zcd_spa = dmu_objset_spa(os);
4576 list_link_init(&cb_data->zcd_node);
4582 * Commit callback test.
4585 ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
4587 objset_t *os = zd->zd_os;
4590 ztest_cb_data_t *cb_data[3], *tmp_cb;
4591 uint64_t old_txg, txg;
4594 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4595 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
4597 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
4598 umem_free(od, sizeof (ztest_od_t));
4602 tx = dmu_tx_create(os);
4604 cb_data[0] = ztest_create_cb_data(os, 0);
4605 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
4607 dmu_tx_hold_write(tx, od->od_object, 0, sizeof (uint64_t));
4609 /* Every once in a while, abort the transaction on purpose */
4610 if (ztest_random(100) == 0)
4614 error = dmu_tx_assign(tx, TXG_NOWAIT);
4616 txg = error ? 0 : dmu_tx_get_txg(tx);
4618 cb_data[0]->zcd_txg = txg;
4619 cb_data[1] = ztest_create_cb_data(os, txg);
4620 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
4624 * It's not a strict requirement to call the registered
4625 * callbacks from inside dmu_tx_abort(), but that's what
4626 * it's supposed to happen in the current implementation
4627 * so we will check for that.
4629 for (i = 0; i < 2; i++) {
4630 cb_data[i]->zcd_expected_err = ECANCELED;
4631 VERIFY(!cb_data[i]->zcd_called);
4636 for (i = 0; i < 2; i++) {
4637 VERIFY(cb_data[i]->zcd_called);
4638 umem_free(cb_data[i], sizeof (ztest_cb_data_t));
4641 umem_free(od, sizeof (ztest_od_t));
4645 cb_data[2] = ztest_create_cb_data(os, txg);
4646 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
4649 * Read existing data to make sure there isn't a future leak.
4651 VERIFY(0 == dmu_read(os, od->od_object, 0, sizeof (uint64_t),
4652 &old_txg, DMU_READ_PREFETCH));
4655 fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
4658 dmu_write(os, od->od_object, 0, sizeof (uint64_t), &txg, tx);
4660 (void) mutex_enter(&zcl.zcl_callbacks_lock);
4663 * Since commit callbacks don't have any ordering requirement and since
4664 * it is theoretically possible for a commit callback to be called
4665 * after an arbitrary amount of time has elapsed since its txg has been
4666 * synced, it is difficult to reliably determine whether a commit
4667 * callback hasn't been called due to high load or due to a flawed
4670 * In practice, we will assume that if after a certain number of txgs a
4671 * commit callback hasn't been called, then most likely there's an
4672 * implementation bug..
4674 tmp_cb = list_head(&zcl.zcl_callbacks);
4675 if (tmp_cb != NULL &&
4676 tmp_cb->zcd_txg + ZTEST_COMMIT_CB_THRESH < txg) {
4677 fatal(0, "Commit callback threshold exceeded, oldest txg: %"
4678 PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
4682 * Let's find the place to insert our callbacks.
4684 * Even though the list is ordered by txg, it is possible for the
4685 * insertion point to not be the end because our txg may already be
4686 * quiescing at this point and other callbacks in the open txg
4687 * (from other objsets) may have sneaked in.
4689 tmp_cb = list_tail(&zcl.zcl_callbacks);
4690 while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
4691 tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
4693 /* Add the 3 callbacks to the list */
4694 for (i = 0; i < 3; i++) {
4696 list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
4698 list_insert_after(&zcl.zcl_callbacks, tmp_cb,
4701 cb_data[i]->zcd_added = B_TRUE;
4702 VERIFY(!cb_data[i]->zcd_called);
4704 tmp_cb = cb_data[i];
4709 (void) mutex_exit(&zcl.zcl_callbacks_lock);
4713 umem_free(od, sizeof (ztest_od_t));
4718 ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
4720 zfs_prop_t proplist[] = {
4722 ZFS_PROP_COMPRESSION,
4728 (void) rw_enter(&ztest_name_lock, RW_READER);
4730 for (p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
4731 (void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
4732 ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
4734 (void) rw_exit(&ztest_name_lock);
4739 ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
4741 nvlist_t *props = NULL;
4743 (void) rw_enter(&ztest_name_lock, RW_READER);
4745 (void) ztest_spa_prop_set_uint64(ZPOOL_PROP_DEDUPDITTO,
4746 ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
4748 VERIFY0(spa_prop_get(ztest_spa, &props));
4750 if (ztest_opts.zo_verbose >= 6)
4751 dump_nvlist(props, 4);
4755 (void) rw_exit(&ztest_name_lock);
4759 user_release_one(const char *snapname, const char *holdname)
4761 nvlist_t *snaps, *holds;
4764 snaps = fnvlist_alloc();
4765 holds = fnvlist_alloc();
4766 fnvlist_add_boolean(holds, holdname);
4767 fnvlist_add_nvlist(snaps, snapname, holds);
4768 fnvlist_free(holds);
4769 error = dsl_dataset_user_release(snaps, NULL);
4770 fnvlist_free(snaps);
4775 * Test snapshot hold/release and deferred destroy.
4778 ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
4781 objset_t *os = zd->zd_os;
4785 char clonename[100];
4787 char osname[MAXNAMELEN];
4790 (void) rw_enter(&ztest_name_lock, RW_READER);
4792 dmu_objset_name(os, osname);
4794 (void) snprintf(snapname, sizeof (snapname), "sh1_%llu",
4796 (void) snprintf(fullname, sizeof (fullname), "%s@%s", osname, snapname);
4797 (void) snprintf(clonename, sizeof (clonename),
4798 "%s/ch1_%llu", osname, (u_longlong_t)id);
4799 (void) snprintf(tag, sizeof (tag), "tag_%llu", (u_longlong_t)id);
4802 * Clean up from any previous run.
4804 error = dsl_destroy_head(clonename);
4805 if (error != ENOENT)
4807 error = user_release_one(fullname, tag);
4808 if (error != ESRCH && error != ENOENT)
4810 error = dsl_destroy_snapshot(fullname, B_FALSE);
4811 if (error != ENOENT)
4815 * Create snapshot, clone it, mark snap for deferred destroy,
4816 * destroy clone, verify snap was also destroyed.
4818 error = dmu_objset_snapshot_one(osname, snapname);
4820 if (error == ENOSPC) {
4821 ztest_record_enospc("dmu_objset_snapshot");
4824 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
4827 error = dmu_objset_clone(clonename, fullname);
4829 if (error == ENOSPC) {
4830 ztest_record_enospc("dmu_objset_clone");
4833 fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
4836 error = dsl_destroy_snapshot(fullname, B_TRUE);
4838 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
4842 error = dsl_destroy_head(clonename);
4844 fatal(0, "dsl_destroy_head(%s) = %d", clonename, error);
4846 error = dmu_objset_hold(fullname, FTAG, &origin);
4847 if (error != ENOENT)
4848 fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
4851 * Create snapshot, add temporary hold, verify that we can't
4852 * destroy a held snapshot, mark for deferred destroy,
4853 * release hold, verify snapshot was destroyed.
4855 error = dmu_objset_snapshot_one(osname, snapname);
4857 if (error == ENOSPC) {
4858 ztest_record_enospc("dmu_objset_snapshot");
4861 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
4864 holds = fnvlist_alloc();
4865 fnvlist_add_string(holds, fullname, tag);
4866 error = dsl_dataset_user_hold(holds, 0, NULL);
4867 fnvlist_free(holds);
4870 fatal(0, "dsl_dataset_user_hold(%s)", fullname, tag);
4872 error = dsl_destroy_snapshot(fullname, B_FALSE);
4873 if (error != EBUSY) {
4874 fatal(0, "dsl_destroy_snapshot(%s, B_FALSE) = %d",
4878 error = dsl_destroy_snapshot(fullname, B_TRUE);
4880 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
4884 error = user_release_one(fullname, tag);
4886 fatal(0, "user_release_one(%s, %s) = %d", fullname, tag, error);
4888 VERIFY3U(dmu_objset_hold(fullname, FTAG, &origin), ==, ENOENT);
4891 (void) rw_exit(&ztest_name_lock);
4895 * Inject random faults into the on-disk data.
4899 ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
4901 ztest_shared_t *zs = ztest_shared;
4902 spa_t *spa = ztest_spa;
4906 uint64_t bad = 0x1990c0ffeedecadeull;
4911 int bshift = SPA_MAXBLOCKSHIFT + 2; /* don't scrog all labels */
4917 boolean_t islog = B_FALSE;
4919 path0 = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
4920 pathrand = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
4922 mutex_enter(&ztest_vdev_lock);
4923 maxfaults = MAXFAULTS();
4924 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
4925 mirror_save = zs->zs_mirrors;
4926 mutex_exit(&ztest_vdev_lock);
4928 ASSERT(leaves >= 1);
4931 * Grab the name lock as reader. There are some operations
4932 * which don't like to have their vdevs changed while
4933 * they are in progress (i.e. spa_change_guid). Those
4934 * operations will have grabbed the name lock as writer.
4936 (void) rw_enter(&ztest_name_lock, RW_READER);
4939 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
4941 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
4943 if (ztest_random(2) == 0) {
4945 * Inject errors on a normal data device or slog device.
4947 top = ztest_random_vdev_top(spa, B_TRUE);
4948 leaf = ztest_random(leaves) + zs->zs_splits;
4951 * Generate paths to the first leaf in this top-level vdev,
4952 * and to the random leaf we selected. We'll induce transient
4953 * write failures and random online/offline activity on leaf 0,
4954 * and we'll write random garbage to the randomly chosen leaf.
4956 (void) snprintf(path0, MAXPATHLEN, ztest_dev_template,
4957 ztest_opts.zo_dir, ztest_opts.zo_pool,
4958 top * leaves + zs->zs_splits);
4959 (void) snprintf(pathrand, MAXPATHLEN, ztest_dev_template,
4960 ztest_opts.zo_dir, ztest_opts.zo_pool,
4961 top * leaves + leaf);
4963 vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
4964 if (vd0 != NULL && vd0->vdev_top->vdev_islog)
4968 * If the top-level vdev needs to be resilvered
4969 * then we only allow faults on the device that is
4972 if (vd0 != NULL && maxfaults != 1 &&
4973 (!vdev_resilver_needed(vd0->vdev_top, NULL, NULL) ||
4974 vd0->vdev_resilver_txg != 0)) {
4976 * Make vd0 explicitly claim to be unreadable,
4977 * or unwriteable, or reach behind its back
4978 * and close the underlying fd. We can do this if
4979 * maxfaults == 0 because we'll fail and reexecute,
4980 * and we can do it if maxfaults >= 2 because we'll
4981 * have enough redundancy. If maxfaults == 1, the
4982 * combination of this with injection of random data
4983 * corruption below exceeds the pool's fault tolerance.
4985 vdev_file_t *vf = vd0->vdev_tsd;
4987 if (vf != NULL && ztest_random(3) == 0) {
4988 (void) close(vf->vf_vnode->v_fd);
4989 vf->vf_vnode->v_fd = -1;
4990 } else if (ztest_random(2) == 0) {
4991 vd0->vdev_cant_read = B_TRUE;
4993 vd0->vdev_cant_write = B_TRUE;
4995 guid0 = vd0->vdev_guid;
4999 * Inject errors on an l2cache device.
5001 spa_aux_vdev_t *sav = &spa->spa_l2cache;
5003 if (sav->sav_count == 0) {
5004 spa_config_exit(spa, SCL_STATE, FTAG);
5005 (void) rw_exit(&ztest_name_lock);
5008 vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
5009 guid0 = vd0->vdev_guid;
5010 (void) strcpy(path0, vd0->vdev_path);
5011 (void) strcpy(pathrand, vd0->vdev_path);
5015 maxfaults = INT_MAX; /* no limit on cache devices */
5018 spa_config_exit(spa, SCL_STATE, FTAG);
5019 (void) rw_exit(&ztest_name_lock);
5022 * If we can tolerate two or more faults, or we're dealing
5023 * with a slog, randomly online/offline vd0.
5025 if ((maxfaults >= 2 || islog) && guid0 != 0) {
5026 if (ztest_random(10) < 6) {
5027 int flags = (ztest_random(2) == 0 ?
5028 ZFS_OFFLINE_TEMPORARY : 0);
5031 * We have to grab the zs_name_lock as writer to
5032 * prevent a race between offlining a slog and
5033 * destroying a dataset. Offlining the slog will
5034 * grab a reference on the dataset which may cause
5035 * dsl_destroy_head() to fail with EBUSY thus
5036 * leaving the dataset in an inconsistent state.
5039 (void) rw_enter(&ztest_name_lock,
5042 VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
5045 (void) rw_exit(&ztest_name_lock);
5048 * Ideally we would like to be able to randomly
5049 * call vdev_[on|off]line without holding locks
5050 * to force unpredictable failures but the side
5051 * effects of vdev_[on|off]line prevent us from
5052 * doing so. We grab the ztest_vdev_lock here to
5053 * prevent a race between injection testing and
5056 mutex_enter(&ztest_vdev_lock);
5057 (void) vdev_online(spa, guid0, 0, NULL);
5058 mutex_exit(&ztest_vdev_lock);
5066 * We have at least single-fault tolerance, so inject data corruption.
5068 fd = open(pathrand, O_RDWR);
5070 if (fd == -1) /* we hit a gap in the device namespace */
5073 fsize = lseek(fd, 0, SEEK_END);
5075 while (--iters != 0) {
5076 offset = ztest_random(fsize / (leaves << bshift)) *
5077 (leaves << bshift) + (leaf << bshift) +
5078 (ztest_random(1ULL << (bshift - 1)) & -8ULL);
5080 if (offset >= fsize)
5083 mutex_enter(&ztest_vdev_lock);
5084 if (mirror_save != zs->zs_mirrors) {
5085 mutex_exit(&ztest_vdev_lock);
5090 if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
5091 fatal(1, "can't inject bad word at 0x%llx in %s",
5094 mutex_exit(&ztest_vdev_lock);
5096 if (ztest_opts.zo_verbose >= 7)
5097 (void) printf("injected bad word into %s,"
5098 " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
5103 umem_free(path0, MAXPATHLEN);
5104 umem_free(pathrand, MAXPATHLEN);
5108 * Verify that DDT repair works as expected.
5111 ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
5113 ztest_shared_t *zs = ztest_shared;
5114 spa_t *spa = ztest_spa;
5115 objset_t *os = zd->zd_os;
5117 uint64_t object, blocksize, txg, pattern, psize;
5118 enum zio_checksum checksum = spa_dedup_checksum(spa);
5123 int copies = 2 * ZIO_DEDUPDITTO_MIN;
5126 blocksize = ztest_random_blocksize();
5127 blocksize = MIN(blocksize, 2048); /* because we write so many */
5129 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
5130 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
5132 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
5133 umem_free(od, sizeof (ztest_od_t));
5138 * Take the name lock as writer to prevent anyone else from changing
5139 * the pool and dataset properies we need to maintain during this test.
5141 (void) rw_enter(&ztest_name_lock, RW_WRITER);
5143 if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
5145 ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
5147 (void) rw_exit(&ztest_name_lock);
5148 umem_free(od, sizeof (ztest_od_t));
5152 object = od[0].od_object;
5153 blocksize = od[0].od_blocksize;
5154 pattern = zs->zs_guid ^ dmu_objset_fsid_guid(os);
5156 ASSERT(object != 0);
5158 tx = dmu_tx_create(os);
5159 dmu_tx_hold_write(tx, object, 0, copies * blocksize);
5160 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
5162 (void) rw_exit(&ztest_name_lock);
5163 umem_free(od, sizeof (ztest_od_t));
5168 * Write all the copies of our block.
5170 for (i = 0; i < copies; i++) {
5171 uint64_t offset = i * blocksize;
5172 int error = dmu_buf_hold(os, object, offset, FTAG, &db,
5173 DMU_READ_NO_PREFETCH);
5175 fatal(B_FALSE, "dmu_buf_hold(%p, %llu, %llu) = %u",
5176 os, (long long)object, (long long) offset, error);
5178 ASSERT(db->db_offset == offset);
5179 ASSERT(db->db_size == blocksize);
5180 ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
5181 ztest_pattern_match(db->db_data, db->db_size, 0ULL));
5182 dmu_buf_will_fill(db, tx);
5183 ztest_pattern_set(db->db_data, db->db_size, pattern);
5184 dmu_buf_rele(db, FTAG);
5188 txg_wait_synced(spa_get_dsl(spa), txg);
5191 * Find out what block we got.
5193 VERIFY0(dmu_buf_hold(os, object, 0, FTAG, &db,
5194 DMU_READ_NO_PREFETCH));
5195 blk = *((dmu_buf_impl_t *)db)->db_blkptr;
5196 dmu_buf_rele(db, FTAG);
5199 * Damage the block. Dedup-ditto will save us when we read it later.
5201 psize = BP_GET_PSIZE(&blk);
5202 buf = zio_buf_alloc(psize);
5203 ztest_pattern_set(buf, psize, ~pattern);
5205 (void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
5206 buf, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
5207 ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
5209 zio_buf_free(buf, psize);
5211 (void) rw_exit(&ztest_name_lock);
5212 umem_free(od, sizeof (ztest_od_t));
5220 ztest_scrub(ztest_ds_t *zd, uint64_t id)
5222 spa_t *spa = ztest_spa;
5224 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5225 (void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
5226 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5230 * Change the guid for the pool.
5234 ztest_reguid(ztest_ds_t *zd, uint64_t id)
5236 spa_t *spa = ztest_spa;
5237 uint64_t orig, load;
5240 orig = spa_guid(spa);
5241 load = spa_load_guid(spa);
5243 (void) rw_enter(&ztest_name_lock, RW_WRITER);
5244 error = spa_change_guid(spa);
5245 (void) rw_exit(&ztest_name_lock);
5250 if (ztest_opts.zo_verbose >= 4) {
5251 (void) printf("Changed guid old %llu -> %llu\n",
5252 (u_longlong_t)orig, (u_longlong_t)spa_guid(spa));
5255 VERIFY3U(orig, !=, spa_guid(spa));
5256 VERIFY3U(load, ==, spa_load_guid(spa));
5260 * Rename the pool to a different name and then rename it back.
5264 ztest_spa_rename(ztest_ds_t *zd, uint64_t id)
5266 char *oldname, *newname;
5269 (void) rw_enter(&ztest_name_lock, RW_WRITER);
5271 oldname = ztest_opts.zo_pool;
5272 newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
5273 (void) strcpy(newname, oldname);
5274 (void) strcat(newname, "_tmp");
5279 VERIFY3U(0, ==, spa_rename(oldname, newname));
5282 * Try to open it under the old name, which shouldn't exist
5284 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5287 * Open it under the new name and make sure it's still the same spa_t.
5289 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
5291 ASSERT(spa == ztest_spa);
5292 spa_close(spa, FTAG);
5295 * Rename it back to the original
5297 VERIFY3U(0, ==, spa_rename(newname, oldname));
5300 * Make sure it can still be opened
5302 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
5304 ASSERT(spa == ztest_spa);
5305 spa_close(spa, FTAG);
5307 umem_free(newname, strlen(newname) + 1);
5309 (void) rw_exit(&ztest_name_lock);
5313 * Verify pool integrity by running zdb.
5316 ztest_run_zdb(char *pool)
5324 bin = umem_alloc(MAXPATHLEN + MAXNAMELEN + 20, UMEM_NOFAIL);
5325 zdb = umem_alloc(MAXPATHLEN + MAXNAMELEN + 20, UMEM_NOFAIL);
5326 zbuf = umem_alloc(1024, UMEM_NOFAIL);
5328 VERIFY(realpath(getexecname(), bin) != NULL);
5329 if (strncmp(bin, "/usr/sbin/ztest", 15) == 0) {
5330 strcpy(bin, "/usr/sbin/zdb"); /* Installed */
5331 } else if (strncmp(bin, "/sbin/ztest", 11) == 0) {
5332 strcpy(bin, "/sbin/zdb"); /* Installed */
5334 strstr(bin, "/ztest/")[0] = '\0'; /* In-tree */
5335 strcat(bin, "/zdb/zdb");
5339 "%s -bcc%s%s -U %s %s",
5341 ztest_opts.zo_verbose >= 3 ? "s" : "",
5342 ztest_opts.zo_verbose >= 4 ? "v" : "",
5346 if (ztest_opts.zo_verbose >= 5)
5347 (void) printf("Executing %s\n", strstr(zdb, "zdb "));
5349 fp = popen(zdb, "r");
5351 while (fgets(zbuf, 1024, fp) != NULL)
5352 if (ztest_opts.zo_verbose >= 3)
5353 (void) printf("%s", zbuf);
5355 status = pclose(fp);
5360 ztest_dump_core = 0;
5361 if (WIFEXITED(status))
5362 fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
5364 fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
5366 umem_free(bin, MAXPATHLEN + MAXNAMELEN + 20);
5367 umem_free(zdb, MAXPATHLEN + MAXNAMELEN + 20);
5368 umem_free(zbuf, 1024);
5372 ztest_walk_pool_directory(char *header)
5376 if (ztest_opts.zo_verbose >= 6)
5377 (void) printf("%s\n", header);
5379 mutex_enter(&spa_namespace_lock);
5380 while ((spa = spa_next(spa)) != NULL)
5381 if (ztest_opts.zo_verbose >= 6)
5382 (void) printf("\t%s\n", spa_name(spa));
5383 mutex_exit(&spa_namespace_lock);
5387 ztest_spa_import_export(char *oldname, char *newname)
5389 nvlist_t *config, *newconfig;
5394 if (ztest_opts.zo_verbose >= 4) {
5395 (void) printf("import/export: old = %s, new = %s\n",
5400 * Clean up from previous runs.
5402 (void) spa_destroy(newname);
5405 * Get the pool's configuration and guid.
5407 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
5410 * Kick off a scrub to tickle scrub/export races.
5412 if (ztest_random(2) == 0)
5413 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5415 pool_guid = spa_guid(spa);
5416 spa_close(spa, FTAG);
5418 ztest_walk_pool_directory("pools before export");
5423 VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
5425 ztest_walk_pool_directory("pools after export");
5430 newconfig = spa_tryimport(config);
5431 ASSERT(newconfig != NULL);
5432 nvlist_free(newconfig);
5435 * Import it under the new name.
5437 error = spa_import(newname, config, NULL, 0);
5439 dump_nvlist(config, 0);
5440 fatal(B_FALSE, "couldn't import pool %s as %s: error %u",
5441 oldname, newname, error);
5444 ztest_walk_pool_directory("pools after import");
5447 * Try to import it again -- should fail with EEXIST.
5449 VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL, 0));
5452 * Try to import it under a different name -- should fail with EEXIST.
5454 VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL, 0));
5457 * Verify that the pool is no longer visible under the old name.
5459 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5462 * Verify that we can open and close the pool using the new name.
5464 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
5465 ASSERT(pool_guid == spa_guid(spa));
5466 spa_close(spa, FTAG);
5468 nvlist_free(config);
5472 ztest_resume(spa_t *spa)
5474 if (spa_suspended(spa) && ztest_opts.zo_verbose >= 6)
5475 (void) printf("resuming from suspended state\n");
5476 spa_vdev_state_enter(spa, SCL_NONE);
5477 vdev_clear(spa, NULL);
5478 (void) spa_vdev_state_exit(spa, NULL, 0);
5479 (void) zio_resume(spa);
5483 ztest_resume_thread(void *arg)
5487 while (!ztest_exiting) {
5488 if (spa_suspended(spa))
5490 (void) poll(NULL, 0, 100);
5502 ztest_deadman_alarm(int sig)
5504 fatal(0, "failed to complete within %d seconds of deadline", GRACE);
5509 ztest_execute(int test, ztest_info_t *zi, uint64_t id)
5511 ztest_ds_t *zd = &ztest_ds[id % ztest_opts.zo_datasets];
5512 ztest_shared_callstate_t *zc = ZTEST_GET_SHARED_CALLSTATE(test);
5513 hrtime_t functime = gethrtime();
5516 for (i = 0; i < zi->zi_iters; i++)
5517 zi->zi_func(zd, id);
5519 functime = gethrtime() - functime;
5521 atomic_add_64(&zc->zc_count, 1);
5522 atomic_add_64(&zc->zc_time, functime);
5524 if (ztest_opts.zo_verbose >= 4) {
5526 (void) dladdr((void *)zi->zi_func, &dli);
5527 (void) printf("%6.2f sec in %s\n",
5528 (double)functime / NANOSEC, dli.dli_sname);
5533 ztest_thread(void *arg)
5536 uint64_t id = (uintptr_t)arg;
5537 ztest_shared_t *zs = ztest_shared;
5541 ztest_shared_callstate_t *zc;
5543 while ((now = gethrtime()) < zs->zs_thread_stop) {
5545 * See if it's time to force a crash.
5547 if (now > zs->zs_thread_kill)
5551 * If we're getting ENOSPC with some regularity, stop.
5553 if (zs->zs_enospc_count > 10)
5557 * Pick a random function to execute.
5559 rand = ztest_random(ZTEST_FUNCS);
5560 zi = &ztest_info[rand];
5561 zc = ZTEST_GET_SHARED_CALLSTATE(rand);
5562 call_next = zc->zc_next;
5564 if (now >= call_next &&
5565 atomic_cas_64(&zc->zc_next, call_next, call_next +
5566 ztest_random(2 * zi->zi_interval[0] + 1)) == call_next) {
5567 ztest_execute(rand, zi, id);
5577 ztest_dataset_name(char *dsname, char *pool, int d)
5579 (void) snprintf(dsname, MAXNAMELEN, "%s/ds_%d", pool, d);
5583 ztest_dataset_destroy(int d)
5585 char name[MAXNAMELEN];
5588 ztest_dataset_name(name, ztest_opts.zo_pool, d);
5590 if (ztest_opts.zo_verbose >= 3)
5591 (void) printf("Destroying %s to free up space\n", name);
5594 * Cleanup any non-standard clones and snapshots. In general,
5595 * ztest thread t operates on dataset (t % zopt_datasets),
5596 * so there may be more than one thing to clean up.
5598 for (t = d; t < ztest_opts.zo_threads;
5599 t += ztest_opts.zo_datasets)
5600 ztest_dsl_dataset_cleanup(name, t);
5602 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
5603 DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
5607 ztest_dataset_dirobj_verify(ztest_ds_t *zd)
5609 uint64_t usedobjs, dirobjs, scratch;
5612 * ZTEST_DIROBJ is the object directory for the entire dataset.
5613 * Therefore, the number of objects in use should equal the
5614 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
5615 * If not, we have an object leak.
5617 * Note that we can only check this in ztest_dataset_open(),
5618 * when the open-context and syncing-context values agree.
5619 * That's because zap_count() returns the open-context value,
5620 * while dmu_objset_space() returns the rootbp fill count.
5622 VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
5623 dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
5624 ASSERT3U(dirobjs + 1, ==, usedobjs);
5628 ztest_dataset_open(int d)
5630 ztest_ds_t *zd = &ztest_ds[d];
5631 uint64_t committed_seq = ZTEST_GET_SHARED_DS(d)->zd_seq;
5634 char name[MAXNAMELEN];
5637 ztest_dataset_name(name, ztest_opts.zo_pool, d);
5639 (void) rw_enter(&ztest_name_lock, RW_READER);
5641 error = ztest_dataset_create(name);
5642 if (error == ENOSPC) {
5643 (void) rw_exit(&ztest_name_lock);
5644 ztest_record_enospc(FTAG);
5647 ASSERT(error == 0 || error == EEXIST);
5649 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, zd, &os));
5650 (void) rw_exit(&ztest_name_lock);
5652 ztest_zd_init(zd, ZTEST_GET_SHARED_DS(d), os);
5654 zilog = zd->zd_zilog;
5656 if (zilog->zl_header->zh_claim_lr_seq != 0 &&
5657 zilog->zl_header->zh_claim_lr_seq < committed_seq)
5658 fatal(0, "missing log records: claimed %llu < committed %llu",
5659 zilog->zl_header->zh_claim_lr_seq, committed_seq);
5661 ztest_dataset_dirobj_verify(zd);
5663 zil_replay(os, zd, ztest_replay_vector);
5665 ztest_dataset_dirobj_verify(zd);
5667 if (ztest_opts.zo_verbose >= 6)
5668 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
5670 (u_longlong_t)zilog->zl_parse_blk_count,
5671 (u_longlong_t)zilog->zl_parse_lr_count,
5672 (u_longlong_t)zilog->zl_replaying_seq);
5674 zilog = zil_open(os, ztest_get_data);
5676 if (zilog->zl_replaying_seq != 0 &&
5677 zilog->zl_replaying_seq < committed_seq)
5678 fatal(0, "missing log records: replayed %llu < committed %llu",
5679 zilog->zl_replaying_seq, committed_seq);
5685 ztest_dataset_close(int d)
5687 ztest_ds_t *zd = &ztest_ds[d];
5689 zil_close(zd->zd_zilog);
5690 dmu_objset_disown(zd->zd_os, zd);
5696 * Kick off threads to run tests on all datasets in parallel.
5699 ztest_run(ztest_shared_t *zs)
5704 kthread_t *resume_thread;
5709 ztest_exiting = B_FALSE;
5712 * Initialize parent/child shared state.
5714 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
5715 rw_init(&ztest_name_lock, NULL, RW_DEFAULT, NULL);
5717 zs->zs_thread_start = gethrtime();
5718 zs->zs_thread_stop =
5719 zs->zs_thread_start + ztest_opts.zo_passtime * NANOSEC;
5720 zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
5721 zs->zs_thread_kill = zs->zs_thread_stop;
5722 if (ztest_random(100) < ztest_opts.zo_killrate) {
5723 zs->zs_thread_kill -=
5724 ztest_random(ztest_opts.zo_passtime * NANOSEC);
5727 mutex_init(&zcl.zcl_callbacks_lock, NULL, MUTEX_DEFAULT, NULL);
5729 list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
5730 offsetof(ztest_cb_data_t, zcd_node));
5735 kernel_init(FREAD | FWRITE);
5736 VERIFY0(spa_open(ztest_opts.zo_pool, &spa, FTAG));
5737 spa->spa_debug = B_TRUE;
5740 VERIFY0(dmu_objset_own(ztest_opts.zo_pool,
5741 DMU_OST_ANY, B_TRUE, FTAG, &os));
5742 zs->zs_guid = dmu_objset_fsid_guid(os);
5743 dmu_objset_disown(os, FTAG);
5745 spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
5748 * We don't expect the pool to suspend unless maxfaults == 0,
5749 * in which case ztest_fault_inject() temporarily takes away
5750 * the only valid replica.
5752 if (MAXFAULTS() == 0)
5753 spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
5755 spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
5758 * Create a thread to periodically resume suspended I/O.
5760 VERIFY3P((resume_thread = zk_thread_create(NULL, 0,
5761 (thread_func_t)ztest_resume_thread, spa, TS_RUN, NULL, 0, 0,
5762 PTHREAD_CREATE_JOINABLE)), !=, NULL);
5766 * Set a deadman alarm to abort() if we hang.
5768 signal(SIGALRM, ztest_deadman_alarm);
5769 alarm((zs->zs_thread_stop - zs->zs_thread_start) / NANOSEC + GRACE);
5773 * Verify that we can safely inquire about about any object,
5774 * whether it's allocated or not. To make it interesting,
5775 * we probe a 5-wide window around each power of two.
5776 * This hits all edge cases, including zero and the max.
5778 for (t = 0; t < 64; t++) {
5779 for (d = -5; d <= 5; d++) {
5780 error = dmu_object_info(spa->spa_meta_objset,
5781 (1ULL << t) + d, NULL);
5782 ASSERT(error == 0 || error == ENOENT ||
5788 * If we got any ENOSPC errors on the previous run, destroy something.
5790 if (zs->zs_enospc_count != 0) {
5791 int d = ztest_random(ztest_opts.zo_datasets);
5792 ztest_dataset_destroy(d);
5794 zs->zs_enospc_count = 0;
5796 tid = umem_zalloc(ztest_opts.zo_threads * sizeof (kt_did_t),
5799 if (ztest_opts.zo_verbose >= 4)
5800 (void) printf("starting main threads...\n");
5803 * Kick off all the tests that run in parallel.
5805 for (t = 0; t < ztest_opts.zo_threads; t++) {
5808 if (t < ztest_opts.zo_datasets &&
5809 ztest_dataset_open(t) != 0)
5812 VERIFY3P(thread = zk_thread_create(NULL, 0,
5813 (thread_func_t)ztest_thread,
5814 (void *)(uintptr_t)t, TS_RUN, NULL, 0, 0,
5815 PTHREAD_CREATE_JOINABLE), !=, NULL);
5816 tid[t] = thread->t_tid;
5820 * Wait for all of the tests to complete. We go in reverse order
5821 * so we don't close datasets while threads are still using them.
5823 for (t = ztest_opts.zo_threads - 1; t >= 0; t--) {
5824 thread_join(tid[t]);
5825 if (t < ztest_opts.zo_datasets)
5826 ztest_dataset_close(t);
5829 txg_wait_synced(spa_get_dsl(spa), 0);
5831 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
5832 zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
5834 if (ztest_opts.zo_verbose >= 3)
5835 zfs_dbgmsg_print(FTAG);
5837 umem_free(tid, ztest_opts.zo_threads * sizeof (kt_did_t));
5839 /* Kill the resume thread */
5840 ztest_exiting = B_TRUE;
5841 thread_join(resume_thread->t_tid);
5845 * Right before closing the pool, kick off a bunch of async I/O;
5846 * spa_close() should wait for it to complete.
5848 for (object = 1; object < 50; object++)
5849 dmu_prefetch(spa->spa_meta_objset, object, 0, 1ULL << 20);
5851 /* Verify that at least one commit cb was called in a timely fashion */
5852 if (zc_cb_counter >= ZTEST_COMMIT_CB_MIN_REG)
5853 VERIFY0(zc_min_txg_delay);
5855 spa_close(spa, FTAG);
5858 * Verify that we can loop over all pools.
5860 mutex_enter(&spa_namespace_lock);
5861 for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
5862 if (ztest_opts.zo_verbose > 3)
5863 (void) printf("spa_next: found %s\n", spa_name(spa));
5864 mutex_exit(&spa_namespace_lock);
5867 * Verify that we can export the pool and reimport it under a
5870 if (ztest_random(2) == 0) {
5871 char name[MAXNAMELEN];
5872 (void) snprintf(name, MAXNAMELEN, "%s_import",
5873 ztest_opts.zo_pool);
5874 ztest_spa_import_export(ztest_opts.zo_pool, name);
5875 ztest_spa_import_export(name, ztest_opts.zo_pool);
5880 list_destroy(&zcl.zcl_callbacks);
5881 mutex_destroy(&zcl.zcl_callbacks_lock);
5882 rw_destroy(&ztest_name_lock);
5883 mutex_destroy(&ztest_vdev_lock);
5889 ztest_ds_t *zd = &ztest_ds[0];
5893 if (ztest_opts.zo_verbose >= 3)
5894 (void) printf("testing spa_freeze()...\n");
5896 kernel_init(FREAD | FWRITE);
5897 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
5898 VERIFY3U(0, ==, ztest_dataset_open(0));
5899 spa->spa_debug = B_TRUE;
5903 * Force the first log block to be transactionally allocated.
5904 * We have to do this before we freeze the pool -- otherwise
5905 * the log chain won't be anchored.
5907 while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
5908 ztest_dmu_object_alloc_free(zd, 0);
5909 zil_commit(zd->zd_zilog, 0);
5912 txg_wait_synced(spa_get_dsl(spa), 0);
5915 * Freeze the pool. This stops spa_sync() from doing anything,
5916 * so that the only way to record changes from now on is the ZIL.
5921 * Run tests that generate log records but don't alter the pool config
5922 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
5923 * We do a txg_wait_synced() after each iteration to force the txg
5924 * to increase well beyond the last synced value in the uberblock.
5925 * The ZIL should be OK with that.
5927 while (ztest_random(10) != 0 &&
5928 numloops++ < ztest_opts.zo_maxloops) {
5929 ztest_dmu_write_parallel(zd, 0);
5930 ztest_dmu_object_alloc_free(zd, 0);
5931 txg_wait_synced(spa_get_dsl(spa), 0);
5935 * Commit all of the changes we just generated.
5937 zil_commit(zd->zd_zilog, 0);
5938 txg_wait_synced(spa_get_dsl(spa), 0);
5941 * Close our dataset and close the pool.
5943 ztest_dataset_close(0);
5944 spa_close(spa, FTAG);
5948 * Open and close the pool and dataset to induce log replay.
5950 kernel_init(FREAD | FWRITE);
5951 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
5952 ASSERT(spa_freeze_txg(spa) == UINT64_MAX);
5953 VERIFY3U(0, ==, ztest_dataset_open(0));
5954 ztest_dataset_close(0);
5956 spa->spa_debug = B_TRUE;
5958 txg_wait_synced(spa_get_dsl(spa), 0);
5959 ztest_reguid(NULL, 0);
5961 spa_close(spa, FTAG);
5966 print_time(hrtime_t t, char *timebuf)
5968 hrtime_t s = t / NANOSEC;
5969 hrtime_t m = s / 60;
5970 hrtime_t h = m / 60;
5971 hrtime_t d = h / 24;
5980 (void) sprintf(timebuf,
5981 "%llud%02lluh%02llum%02llus", d, h, m, s);
5983 (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
5985 (void) sprintf(timebuf, "%llum%02llus", m, s);
5987 (void) sprintf(timebuf, "%llus", s);
5991 make_random_props(void)
5995 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
5996 if (ztest_random(2) == 0)
5998 VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
6004 * Create a storage pool with the given name and initial vdev size.
6005 * Then test spa_freeze() functionality.
6008 ztest_init(ztest_shared_t *zs)
6011 nvlist_t *nvroot, *props;
6014 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
6015 rw_init(&ztest_name_lock, NULL, RW_DEFAULT, NULL);
6017 kernel_init(FREAD | FWRITE);
6020 * Create the storage pool.
6022 (void) spa_destroy(ztest_opts.zo_pool);
6023 ztest_shared->zs_vdev_next_leaf = 0;
6025 zs->zs_mirrors = ztest_opts.zo_mirrors;
6026 nvroot = make_vdev_root(NULL, NULL, NULL, ztest_opts.zo_vdev_size, 0,
6027 0, ztest_opts.zo_raidz, zs->zs_mirrors, 1);
6028 props = make_random_props();
6029 for (i = 0; i < SPA_FEATURES; i++) {
6031 VERIFY3S(-1, !=, asprintf(&buf, "feature@%s",
6032 spa_feature_table[i].fi_uname));
6033 VERIFY3U(0, ==, nvlist_add_uint64(props, buf, 0));
6036 VERIFY3U(0, ==, spa_create(ztest_opts.zo_pool, nvroot, props, NULL));
6037 nvlist_free(nvroot);
6039 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6040 zs->zs_metaslab_sz =
6041 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
6042 spa_close(spa, FTAG);
6046 ztest_run_zdb(ztest_opts.zo_pool);
6050 ztest_run_zdb(ztest_opts.zo_pool);
6052 rw_destroy(&ztest_name_lock);
6053 mutex_destroy(&ztest_vdev_lock);
6059 static char ztest_name_data[] = "/tmp/ztest.data.XXXXXX";
6061 ztest_fd_data = mkstemp(ztest_name_data);
6062 ASSERT3S(ztest_fd_data, >=, 0);
6063 (void) unlink(ztest_name_data);
6067 shared_data_size(ztest_shared_hdr_t *hdr)
6071 size = hdr->zh_hdr_size;
6072 size += hdr->zh_opts_size;
6073 size += hdr->zh_size;
6074 size += hdr->zh_stats_size * hdr->zh_stats_count;
6075 size += hdr->zh_ds_size * hdr->zh_ds_count;
6084 ztest_shared_hdr_t *hdr;
6086 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6087 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6088 ASSERT(hdr != MAP_FAILED);
6090 VERIFY3U(0, ==, ftruncate(ztest_fd_data, sizeof (ztest_shared_hdr_t)));
6092 hdr->zh_hdr_size = sizeof (ztest_shared_hdr_t);
6093 hdr->zh_opts_size = sizeof (ztest_shared_opts_t);
6094 hdr->zh_size = sizeof (ztest_shared_t);
6095 hdr->zh_stats_size = sizeof (ztest_shared_callstate_t);
6096 hdr->zh_stats_count = ZTEST_FUNCS;
6097 hdr->zh_ds_size = sizeof (ztest_shared_ds_t);
6098 hdr->zh_ds_count = ztest_opts.zo_datasets;
6100 size = shared_data_size(hdr);
6101 VERIFY3U(0, ==, ftruncate(ztest_fd_data, size));
6103 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6110 ztest_shared_hdr_t *hdr;
6113 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6114 PROT_READ, MAP_SHARED, ztest_fd_data, 0);
6115 ASSERT(hdr != MAP_FAILED);
6117 size = shared_data_size(hdr);
6119 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6120 hdr = ztest_shared_hdr = (void *)mmap(0, P2ROUNDUP(size, getpagesize()),
6121 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6122 ASSERT(hdr != MAP_FAILED);
6123 buf = (uint8_t *)hdr;
6125 offset = hdr->zh_hdr_size;
6126 ztest_shared_opts = (void *)&buf[offset];
6127 offset += hdr->zh_opts_size;
6128 ztest_shared = (void *)&buf[offset];
6129 offset += hdr->zh_size;
6130 ztest_shared_callstate = (void *)&buf[offset];
6131 offset += hdr->zh_stats_size * hdr->zh_stats_count;
6132 ztest_shared_ds = (void *)&buf[offset];
6136 exec_child(char *cmd, char *libpath, boolean_t ignorekill, int *statusp)
6140 char *cmdbuf = NULL;
6145 cmdbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
6146 (void) strlcpy(cmdbuf, getexecname(), MAXPATHLEN);
6151 fatal(1, "fork failed");
6153 if (pid == 0) { /* child */
6154 char *emptyargv[2] = { cmd, NULL };
6155 char fd_data_str[12];
6157 struct rlimit rl = { 1024, 1024 };
6158 (void) setrlimit(RLIMIT_NOFILE, &rl);
6160 (void) close(ztest_fd_rand);
6161 VERIFY(11 >= snprintf(fd_data_str, 12, "%d", ztest_fd_data));
6162 VERIFY(0 == setenv("ZTEST_FD_DATA", fd_data_str, 1));
6164 (void) enable_extended_FILE_stdio(-1, -1);
6165 if (libpath != NULL)
6166 VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath, 1));
6167 (void) execv(cmd, emptyargv);
6168 ztest_dump_core = B_FALSE;
6169 fatal(B_TRUE, "exec failed: %s", cmd);
6172 if (cmdbuf != NULL) {
6173 umem_free(cmdbuf, MAXPATHLEN);
6177 while (waitpid(pid, &status, 0) != pid)
6179 if (statusp != NULL)
6182 if (WIFEXITED(status)) {
6183 if (WEXITSTATUS(status) != 0) {
6184 (void) fprintf(stderr, "child exited with code %d\n",
6185 WEXITSTATUS(status));
6189 } else if (WIFSIGNALED(status)) {
6190 if (!ignorekill || WTERMSIG(status) != SIGKILL) {
6191 (void) fprintf(stderr, "child died with signal %d\n",
6197 (void) fprintf(stderr, "something strange happened to child\n");
6204 ztest_run_init(void)
6208 ztest_shared_t *zs = ztest_shared;
6210 ASSERT(ztest_opts.zo_init != 0);
6213 * Blow away any existing copy of zpool.cache
6215 (void) remove(spa_config_path);
6218 * Create and initialize our storage pool.
6220 for (i = 1; i <= ztest_opts.zo_init; i++) {
6221 bzero(zs, sizeof (ztest_shared_t));
6222 if (ztest_opts.zo_verbose >= 3 &&
6223 ztest_opts.zo_init != 1) {
6224 (void) printf("ztest_init(), pass %d\n", i);
6231 main(int argc, char **argv)
6239 ztest_shared_callstate_t *zc;
6246 char *fd_data_str = getenv("ZTEST_FD_DATA");
6248 (void) setvbuf(stdout, NULL, _IOLBF, 0);
6250 dprintf_setup(&argc, argv);
6252 ztest_fd_rand = open("/dev/urandom", O_RDONLY);
6253 ASSERT3S(ztest_fd_rand, >=, 0);
6256 process_options(argc, argv);
6261 bcopy(&ztest_opts, ztest_shared_opts,
6262 sizeof (*ztest_shared_opts));
6264 ztest_fd_data = atoi(fd_data_str);
6266 bcopy(ztest_shared_opts, &ztest_opts, sizeof (ztest_opts));
6268 ASSERT3U(ztest_opts.zo_datasets, ==, ztest_shared_hdr->zh_ds_count);
6270 /* Override location of zpool.cache */
6271 VERIFY(asprintf((char **)&spa_config_path, "%s/zpool.cache",
6272 ztest_opts.zo_dir) != -1);
6274 ztest_ds = umem_alloc(ztest_opts.zo_datasets * sizeof (ztest_ds_t),
6279 metaslab_gang_bang = ztest_opts.zo_metaslab_gang_bang;
6280 metaslab_df_alloc_threshold =
6281 zs->zs_metaslab_df_alloc_threshold;
6290 hasalt = (strlen(ztest_opts.zo_alt_ztest) != 0);
6292 if (ztest_opts.zo_verbose >= 1) {
6293 (void) printf("%llu vdevs, %d datasets, %d threads,"
6294 " %llu seconds...\n",
6295 (u_longlong_t)ztest_opts.zo_vdevs,
6296 ztest_opts.zo_datasets,
6297 ztest_opts.zo_threads,
6298 (u_longlong_t)ztest_opts.zo_time);
6301 cmd = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
6302 (void) strlcpy(cmd, getexecname(), MAXNAMELEN);
6304 zs->zs_do_init = B_TRUE;
6305 if (strlen(ztest_opts.zo_alt_ztest) != 0) {
6306 if (ztest_opts.zo_verbose >= 1) {
6307 (void) printf("Executing older ztest for "
6308 "initialization: %s\n", ztest_opts.zo_alt_ztest);
6310 VERIFY(!exec_child(ztest_opts.zo_alt_ztest,
6311 ztest_opts.zo_alt_libpath, B_FALSE, NULL));
6313 VERIFY(!exec_child(NULL, NULL, B_FALSE, NULL));
6315 zs->zs_do_init = B_FALSE;
6317 zs->zs_proc_start = gethrtime();
6318 zs->zs_proc_stop = zs->zs_proc_start + ztest_opts.zo_time * NANOSEC;
6320 for (f = 0; f < ZTEST_FUNCS; f++) {
6321 zi = &ztest_info[f];
6322 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6323 if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
6324 zc->zc_next = UINT64_MAX;
6326 zc->zc_next = zs->zs_proc_start +
6327 ztest_random(2 * zi->zi_interval[0] + 1);
6331 * Run the tests in a loop. These tests include fault injection
6332 * to verify that self-healing data works, and forced crashes
6333 * to verify that we never lose on-disk consistency.
6335 while (gethrtime() < zs->zs_proc_stop) {
6340 * Initialize the workload counters for each function.
6342 for (f = 0; f < ZTEST_FUNCS; f++) {
6343 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6348 /* Set the allocation switch size */
6349 zs->zs_metaslab_df_alloc_threshold =
6350 ztest_random(zs->zs_metaslab_sz / 4) + 1;
6352 if (!hasalt || ztest_random(2) == 0) {
6353 if (hasalt && ztest_opts.zo_verbose >= 1) {
6354 (void) printf("Executing newer ztest: %s\n",
6358 killed = exec_child(cmd, NULL, B_TRUE, &status);
6360 if (hasalt && ztest_opts.zo_verbose >= 1) {
6361 (void) printf("Executing older ztest: %s\n",
6362 ztest_opts.zo_alt_ztest);
6365 killed = exec_child(ztest_opts.zo_alt_ztest,
6366 ztest_opts.zo_alt_libpath, B_TRUE, &status);
6373 if (ztest_opts.zo_verbose >= 1) {
6374 hrtime_t now = gethrtime();
6376 now = MIN(now, zs->zs_proc_stop);
6377 print_time(zs->zs_proc_stop - now, timebuf);
6378 nicenum(zs->zs_space, numbuf);
6380 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
6381 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
6383 WIFEXITED(status) ? "Complete" : "SIGKILL",
6384 (u_longlong_t)zs->zs_enospc_count,
6385 100.0 * zs->zs_alloc / zs->zs_space,
6387 100.0 * (now - zs->zs_proc_start) /
6388 (ztest_opts.zo_time * NANOSEC), timebuf);
6391 if (ztest_opts.zo_verbose >= 2) {
6392 (void) printf("\nWorkload summary:\n\n");
6393 (void) printf("%7s %9s %s\n",
6394 "Calls", "Time", "Function");
6395 (void) printf("%7s %9s %s\n",
6396 "-----", "----", "--------");
6397 for (f = 0; f < ZTEST_FUNCS; f++) {
6400 zi = &ztest_info[f];
6401 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6402 print_time(zc->zc_time, timebuf);
6403 (void) dladdr((void *)zi->zi_func, &dli);
6404 (void) printf("%7llu %9s %s\n",
6405 (u_longlong_t)zc->zc_count, timebuf,
6408 (void) printf("\n");
6412 * It's possible that we killed a child during a rename test,
6413 * in which case we'll have a 'ztest_tmp' pool lying around
6414 * instead of 'ztest'. Do a blind rename in case this happened.
6417 if (spa_open(ztest_opts.zo_pool, &spa, FTAG) == 0) {
6418 spa_close(spa, FTAG);
6420 char tmpname[MAXNAMELEN];
6422 kernel_init(FREAD | FWRITE);
6423 (void) snprintf(tmpname, sizeof (tmpname), "%s_tmp",
6424 ztest_opts.zo_pool);
6425 (void) spa_rename(tmpname, ztest_opts.zo_pool);
6429 ztest_run_zdb(ztest_opts.zo_pool);
6432 if (ztest_opts.zo_verbose >= 1) {
6434 (void) printf("%d runs of older ztest: %s\n", older,
6435 ztest_opts.zo_alt_ztest);
6436 (void) printf("%d runs of newer ztest: %s\n", newer,
6439 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
6440 kills, iters - kills, (100.0 * kills) / MAX(1, iters));
6443 umem_free(cmd, MAXNAMELEN);