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) 2012 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_sometimes },
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));
792 (void) kill(getpid(), SIGKILL);
796 ztest_random(uint64_t range)
800 ASSERT3S(ztest_fd_rand, >=, 0);
805 if (read(ztest_fd_rand, &r, sizeof (r)) != sizeof (r))
806 fatal(1, "short read from /dev/urandom");
813 ztest_record_enospc(const char *s)
815 ztest_shared->zs_enospc_count++;
819 ztest_get_ashift(void)
821 if (ztest_opts.zo_ashift == 0)
822 return (SPA_MINBLOCKSHIFT + ztest_random(3));
823 return (ztest_opts.zo_ashift);
827 make_vdev_file(char *path, char *aux, char *pool, size_t size, uint64_t ashift)
833 pathbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
836 ashift = ztest_get_ashift();
842 vdev = ztest_shared->zs_vdev_aux;
843 (void) snprintf(path, MAXPATHLEN,
844 ztest_aux_template, ztest_opts.zo_dir,
845 pool == NULL ? ztest_opts.zo_pool : pool,
848 vdev = ztest_shared->zs_vdev_next_leaf++;
849 (void) snprintf(path, MAXPATHLEN,
850 ztest_dev_template, ztest_opts.zo_dir,
851 pool == NULL ? ztest_opts.zo_pool : pool, vdev);
856 int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
858 fatal(1, "can't open %s", path);
859 if (ftruncate(fd, size) != 0)
860 fatal(1, "can't ftruncate %s", path);
864 VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
865 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
866 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
867 VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
868 umem_free(pathbuf, MAXPATHLEN);
874 make_vdev_raidz(char *path, char *aux, char *pool, size_t size,
875 uint64_t ashift, int r)
877 nvlist_t *raidz, **child;
881 return (make_vdev_file(path, aux, pool, size, ashift));
882 child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
884 for (c = 0; c < r; c++)
885 child[c] = make_vdev_file(path, aux, pool, size, ashift);
887 VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
888 VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
889 VDEV_TYPE_RAIDZ) == 0);
890 VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
891 ztest_opts.zo_raidz_parity) == 0);
892 VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
895 for (c = 0; c < r; c++)
896 nvlist_free(child[c]);
898 umem_free(child, r * sizeof (nvlist_t *));
904 make_vdev_mirror(char *path, char *aux, char *pool, size_t size,
905 uint64_t ashift, int r, int m)
907 nvlist_t *mirror, **child;
911 return (make_vdev_raidz(path, aux, pool, size, ashift, r));
913 child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
915 for (c = 0; c < m; c++)
916 child[c] = make_vdev_raidz(path, aux, pool, size, ashift, r);
918 VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
919 VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
920 VDEV_TYPE_MIRROR) == 0);
921 VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
924 for (c = 0; c < m; c++)
925 nvlist_free(child[c]);
927 umem_free(child, m * sizeof (nvlist_t *));
933 make_vdev_root(char *path, char *aux, char *pool, size_t size, uint64_t ashift,
934 int log, int r, int m, int t)
936 nvlist_t *root, **child;
941 child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
943 for (c = 0; c < t; c++) {
944 child[c] = make_vdev_mirror(path, aux, pool, size, ashift,
946 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
950 VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
951 VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
952 VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
955 for (c = 0; c < t; c++)
956 nvlist_free(child[c]);
958 umem_free(child, t * sizeof (nvlist_t *));
964 * Find a random spa version. Returns back a random spa version in the
965 * range [initial_version, SPA_VERSION_FEATURES].
968 ztest_random_spa_version(uint64_t initial_version)
970 uint64_t version = initial_version;
972 if (version <= SPA_VERSION_BEFORE_FEATURES) {
974 ztest_random(SPA_VERSION_BEFORE_FEATURES - version + 1);
977 if (version > SPA_VERSION_BEFORE_FEATURES)
978 version = SPA_VERSION_FEATURES;
980 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
985 ztest_random_blocksize(void)
987 return (1 << (SPA_MINBLOCKSHIFT +
988 ztest_random(SPA_MAXBLOCKSHIFT - SPA_MINBLOCKSHIFT + 1)));
992 ztest_random_ibshift(void)
994 return (DN_MIN_INDBLKSHIFT +
995 ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1));
999 ztest_random_vdev_top(spa_t *spa, boolean_t log_ok)
1002 vdev_t *rvd = spa->spa_root_vdev;
1005 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1008 top = ztest_random(rvd->vdev_children);
1009 tvd = rvd->vdev_child[top];
1010 } while (tvd->vdev_ishole || (tvd->vdev_islog && !log_ok) ||
1011 tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL);
1017 ztest_random_dsl_prop(zfs_prop_t prop)
1022 value = zfs_prop_random_value(prop, ztest_random(-1ULL));
1023 } while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF);
1029 ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value,
1032 const char *propname = zfs_prop_to_name(prop);
1033 const char *valname;
1038 error = dsl_prop_set_int(osname, propname,
1039 (inherit ? ZPROP_SRC_NONE : ZPROP_SRC_LOCAL), value);
1041 if (error == ENOSPC) {
1042 ztest_record_enospc(FTAG);
1047 setpoint = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
1048 VERIFY0(dsl_prop_get_integer(osname, propname, &curval, setpoint));
1050 if (ztest_opts.zo_verbose >= 6) {
1051 VERIFY(zfs_prop_index_to_string(prop, curval, &valname) == 0);
1052 (void) printf("%s %s = %s at '%s'\n",
1053 osname, propname, valname, setpoint);
1055 umem_free(setpoint, MAXPATHLEN);
1061 ztest_spa_prop_set_uint64(zpool_prop_t prop, uint64_t value)
1063 spa_t *spa = ztest_spa;
1064 nvlist_t *props = NULL;
1067 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
1068 VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0);
1070 error = spa_prop_set(spa, props);
1074 if (error == ENOSPC) {
1075 ztest_record_enospc(FTAG);
1084 ztest_rll_init(rll_t *rll)
1086 rll->rll_writer = NULL;
1087 rll->rll_readers = 0;
1088 mutex_init(&rll->rll_lock, NULL, MUTEX_DEFAULT, NULL);
1089 cv_init(&rll->rll_cv, NULL, CV_DEFAULT, NULL);
1093 ztest_rll_destroy(rll_t *rll)
1095 ASSERT(rll->rll_writer == NULL);
1096 ASSERT(rll->rll_readers == 0);
1097 mutex_destroy(&rll->rll_lock);
1098 cv_destroy(&rll->rll_cv);
1102 ztest_rll_lock(rll_t *rll, rl_type_t type)
1104 mutex_enter(&rll->rll_lock);
1106 if (type == RL_READER) {
1107 while (rll->rll_writer != NULL)
1108 (void) cv_wait(&rll->rll_cv, &rll->rll_lock);
1111 while (rll->rll_writer != NULL || rll->rll_readers)
1112 (void) cv_wait(&rll->rll_cv, &rll->rll_lock);
1113 rll->rll_writer = curthread;
1116 mutex_exit(&rll->rll_lock);
1120 ztest_rll_unlock(rll_t *rll)
1122 mutex_enter(&rll->rll_lock);
1124 if (rll->rll_writer) {
1125 ASSERT(rll->rll_readers == 0);
1126 rll->rll_writer = NULL;
1128 ASSERT(rll->rll_readers != 0);
1129 ASSERT(rll->rll_writer == NULL);
1133 if (rll->rll_writer == NULL && rll->rll_readers == 0)
1134 cv_broadcast(&rll->rll_cv);
1136 mutex_exit(&rll->rll_lock);
1140 ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type)
1142 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1144 ztest_rll_lock(rll, type);
1148 ztest_object_unlock(ztest_ds_t *zd, uint64_t object)
1150 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1152 ztest_rll_unlock(rll);
1156 ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset,
1157 uint64_t size, rl_type_t type)
1159 uint64_t hash = object ^ (offset % (ZTEST_RANGE_LOCKS + 1));
1160 rll_t *rll = &zd->zd_range_lock[hash & (ZTEST_RANGE_LOCKS - 1)];
1163 rl = umem_alloc(sizeof (*rl), UMEM_NOFAIL);
1164 rl->rl_object = object;
1165 rl->rl_offset = offset;
1169 ztest_rll_lock(rll, type);
1175 ztest_range_unlock(rl_t *rl)
1177 rll_t *rll = rl->rl_lock;
1179 ztest_rll_unlock(rll);
1181 umem_free(rl, sizeof (*rl));
1185 ztest_zd_init(ztest_ds_t *zd, ztest_shared_ds_t *szd, objset_t *os)
1188 zd->zd_zilog = dmu_objset_zil(os);
1189 zd->zd_shared = szd;
1190 dmu_objset_name(os, zd->zd_name);
1193 if (zd->zd_shared != NULL)
1194 zd->zd_shared->zd_seq = 0;
1196 rw_init(&zd->zd_zilog_lock, NULL, RW_DEFAULT, NULL);
1197 mutex_init(&zd->zd_dirobj_lock, NULL, MUTEX_DEFAULT, NULL);
1199 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1200 ztest_rll_init(&zd->zd_object_lock[l]);
1202 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
1203 ztest_rll_init(&zd->zd_range_lock[l]);
1207 ztest_zd_fini(ztest_ds_t *zd)
1211 mutex_destroy(&zd->zd_dirobj_lock);
1212 rw_destroy(&zd->zd_zilog_lock);
1214 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1215 ztest_rll_destroy(&zd->zd_object_lock[l]);
1217 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
1218 ztest_rll_destroy(&zd->zd_range_lock[l]);
1221 #define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1224 ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag)
1230 * Attempt to assign tx to some transaction group.
1232 error = dmu_tx_assign(tx, txg_how);
1234 if (error == ERESTART) {
1235 ASSERT(txg_how == TXG_NOWAIT);
1238 ASSERT3U(error, ==, ENOSPC);
1239 ztest_record_enospc(tag);
1244 txg = dmu_tx_get_txg(tx);
1250 ztest_pattern_set(void *buf, uint64_t size, uint64_t value)
1253 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1261 ztest_pattern_match(void *buf, uint64_t size, uint64_t value)
1264 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1268 diff |= (value - *ip++);
1275 ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1276 uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
1278 bt->bt_magic = BT_MAGIC;
1279 bt->bt_objset = dmu_objset_id(os);
1280 bt->bt_object = object;
1281 bt->bt_offset = offset;
1284 bt->bt_crtxg = crtxg;
1288 ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1289 uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
1291 ASSERT(bt->bt_magic == BT_MAGIC);
1292 ASSERT(bt->bt_objset == dmu_objset_id(os));
1293 ASSERT(bt->bt_object == object);
1294 ASSERT(bt->bt_offset == offset);
1295 ASSERT(bt->bt_gen <= gen);
1296 ASSERT(bt->bt_txg <= txg);
1297 ASSERT(bt->bt_crtxg == crtxg);
1300 static ztest_block_tag_t *
1301 ztest_bt_bonus(dmu_buf_t *db)
1303 dmu_object_info_t doi;
1304 ztest_block_tag_t *bt;
1306 dmu_object_info_from_db(db, &doi);
1307 ASSERT3U(doi.doi_bonus_size, <=, db->db_size);
1308 ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt));
1309 bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt));
1318 #define lrz_type lr_mode
1319 #define lrz_blocksize lr_uid
1320 #define lrz_ibshift lr_gid
1321 #define lrz_bonustype lr_rdev
1322 #define lrz_bonuslen lr_crtime[1]
1325 ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr)
1327 char *name = (void *)(lr + 1); /* name follows lr */
1328 size_t namesize = strlen(name) + 1;
1331 if (zil_replaying(zd->zd_zilog, tx))
1334 itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize);
1335 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1336 sizeof (*lr) + namesize - sizeof (lr_t));
1338 zil_itx_assign(zd->zd_zilog, itx, tx);
1342 ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr, uint64_t object)
1344 char *name = (void *)(lr + 1); /* name follows lr */
1345 size_t namesize = strlen(name) + 1;
1348 if (zil_replaying(zd->zd_zilog, tx))
1351 itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize);
1352 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1353 sizeof (*lr) + namesize - sizeof (lr_t));
1355 itx->itx_oid = object;
1356 zil_itx_assign(zd->zd_zilog, itx, tx);
1360 ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr)
1363 itx_wr_state_t write_state = ztest_random(WR_NUM_STATES);
1365 if (zil_replaying(zd->zd_zilog, tx))
1368 if (lr->lr_length > ZIL_MAX_LOG_DATA)
1369 write_state = WR_INDIRECT;
1371 itx = zil_itx_create(TX_WRITE,
1372 sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0));
1374 if (write_state == WR_COPIED &&
1375 dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length,
1376 ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) {
1377 zil_itx_destroy(itx);
1378 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1379 write_state = WR_NEED_COPY;
1381 itx->itx_private = zd;
1382 itx->itx_wr_state = write_state;
1383 itx->itx_sync = (ztest_random(8) == 0);
1384 itx->itx_sod += (write_state == WR_NEED_COPY ? lr->lr_length : 0);
1386 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1387 sizeof (*lr) - sizeof (lr_t));
1389 zil_itx_assign(zd->zd_zilog, itx, tx);
1393 ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr)
1397 if (zil_replaying(zd->zd_zilog, tx))
1400 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1401 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1402 sizeof (*lr) - sizeof (lr_t));
1404 itx->itx_sync = B_FALSE;
1405 zil_itx_assign(zd->zd_zilog, itx, tx);
1409 ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr)
1413 if (zil_replaying(zd->zd_zilog, tx))
1416 itx = zil_itx_create(TX_SETATTR, sizeof (*lr));
1417 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1418 sizeof (*lr) - sizeof (lr_t));
1420 itx->itx_sync = B_FALSE;
1421 zil_itx_assign(zd->zd_zilog, itx, tx);
1428 ztest_replay_create(ztest_ds_t *zd, lr_create_t *lr, boolean_t byteswap)
1430 char *name = (void *)(lr + 1); /* name follows lr */
1431 objset_t *os = zd->zd_os;
1432 ztest_block_tag_t *bbt;
1439 byteswap_uint64_array(lr, sizeof (*lr));
1441 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1442 ASSERT(name[0] != '\0');
1444 tx = dmu_tx_create(os);
1446 dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name);
1448 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1449 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1451 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1454 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1458 ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid);
1460 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1461 if (lr->lr_foid == 0) {
1462 lr->lr_foid = zap_create(os,
1463 lr->lrz_type, lr->lrz_bonustype,
1464 lr->lrz_bonuslen, tx);
1466 error = zap_create_claim(os, lr->lr_foid,
1467 lr->lrz_type, lr->lrz_bonustype,
1468 lr->lrz_bonuslen, tx);
1471 if (lr->lr_foid == 0) {
1472 lr->lr_foid = dmu_object_alloc(os,
1473 lr->lrz_type, 0, lr->lrz_bonustype,
1474 lr->lrz_bonuslen, tx);
1476 error = dmu_object_claim(os, lr->lr_foid,
1477 lr->lrz_type, 0, lr->lrz_bonustype,
1478 lr->lrz_bonuslen, tx);
1483 ASSERT3U(error, ==, EEXIST);
1484 ASSERT(zd->zd_zilog->zl_replay);
1489 ASSERT(lr->lr_foid != 0);
1491 if (lr->lrz_type != DMU_OT_ZAP_OTHER)
1492 VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid,
1493 lr->lrz_blocksize, lr->lrz_ibshift, tx));
1495 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1496 bbt = ztest_bt_bonus(db);
1497 dmu_buf_will_dirty(db, tx);
1498 ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_gen, txg, txg);
1499 dmu_buf_rele(db, FTAG);
1501 VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1,
1504 (void) ztest_log_create(zd, tx, lr);
1512 ztest_replay_remove(ztest_ds_t *zd, lr_remove_t *lr, boolean_t byteswap)
1514 char *name = (void *)(lr + 1); /* name follows lr */
1515 objset_t *os = zd->zd_os;
1516 dmu_object_info_t doi;
1518 uint64_t object, txg;
1521 byteswap_uint64_array(lr, sizeof (*lr));
1523 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1524 ASSERT(name[0] != '\0');
1527 zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object));
1528 ASSERT(object != 0);
1530 ztest_object_lock(zd, object, RL_WRITER);
1532 VERIFY3U(0, ==, dmu_object_info(os, object, &doi));
1534 tx = dmu_tx_create(os);
1536 dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name);
1537 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1539 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1541 ztest_object_unlock(zd, object);
1545 if (doi.doi_type == DMU_OT_ZAP_OTHER) {
1546 VERIFY3U(0, ==, zap_destroy(os, object, tx));
1548 VERIFY3U(0, ==, dmu_object_free(os, object, tx));
1551 VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx));
1553 (void) ztest_log_remove(zd, tx, lr, object);
1557 ztest_object_unlock(zd, object);
1563 ztest_replay_write(ztest_ds_t *zd, lr_write_t *lr, boolean_t byteswap)
1565 objset_t *os = zd->zd_os;
1566 void *data = lr + 1; /* data follows lr */
1567 uint64_t offset, length;
1568 ztest_block_tag_t *bt = data;
1569 ztest_block_tag_t *bbt;
1570 uint64_t gen, txg, lrtxg, crtxg;
1571 dmu_object_info_t doi;
1574 arc_buf_t *abuf = NULL;
1578 byteswap_uint64_array(lr, sizeof (*lr));
1580 offset = lr->lr_offset;
1581 length = lr->lr_length;
1583 /* If it's a dmu_sync() block, write the whole block */
1584 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
1585 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
1586 if (length < blocksize) {
1587 offset -= offset % blocksize;
1592 if (bt->bt_magic == BSWAP_64(BT_MAGIC))
1593 byteswap_uint64_array(bt, sizeof (*bt));
1595 if (bt->bt_magic != BT_MAGIC)
1598 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1599 rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER);
1601 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1603 dmu_object_info_from_db(db, &doi);
1605 bbt = ztest_bt_bonus(db);
1606 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1608 crtxg = bbt->bt_crtxg;
1609 lrtxg = lr->lr_common.lrc_txg;
1611 tx = dmu_tx_create(os);
1613 dmu_tx_hold_write(tx, lr->lr_foid, offset, length);
1615 if (ztest_random(8) == 0 && length == doi.doi_data_block_size &&
1616 P2PHASE(offset, length) == 0)
1617 abuf = dmu_request_arcbuf(db, length);
1619 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1622 dmu_return_arcbuf(abuf);
1623 dmu_buf_rele(db, FTAG);
1624 ztest_range_unlock(rl);
1625 ztest_object_unlock(zd, lr->lr_foid);
1631 * Usually, verify the old data before writing new data --
1632 * but not always, because we also want to verify correct
1633 * behavior when the data was not recently read into cache.
1635 ASSERT(offset % doi.doi_data_block_size == 0);
1636 if (ztest_random(4) != 0) {
1637 int prefetch = ztest_random(2) ?
1638 DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
1639 ztest_block_tag_t rbt;
1641 VERIFY(dmu_read(os, lr->lr_foid, offset,
1642 sizeof (rbt), &rbt, prefetch) == 0);
1643 if (rbt.bt_magic == BT_MAGIC) {
1644 ztest_bt_verify(&rbt, os, lr->lr_foid,
1645 offset, gen, txg, crtxg);
1650 * Writes can appear to be newer than the bonus buffer because
1651 * the ztest_get_data() callback does a dmu_read() of the
1652 * open-context data, which may be different than the data
1653 * as it was when the write was generated.
1655 if (zd->zd_zilog->zl_replay) {
1656 ztest_bt_verify(bt, os, lr->lr_foid, offset,
1657 MAX(gen, bt->bt_gen), MAX(txg, lrtxg),
1662 * Set the bt's gen/txg to the bonus buffer's gen/txg
1663 * so that all of the usual ASSERTs will work.
1665 ztest_bt_generate(bt, os, lr->lr_foid, offset, gen, txg, crtxg);
1669 dmu_write(os, lr->lr_foid, offset, length, data, tx);
1671 bcopy(data, abuf->b_data, length);
1672 dmu_assign_arcbuf(db, offset, abuf, tx);
1675 (void) ztest_log_write(zd, tx, lr);
1677 dmu_buf_rele(db, FTAG);
1681 ztest_range_unlock(rl);
1682 ztest_object_unlock(zd, lr->lr_foid);
1688 ztest_replay_truncate(ztest_ds_t *zd, lr_truncate_t *lr, boolean_t byteswap)
1690 objset_t *os = zd->zd_os;
1696 byteswap_uint64_array(lr, sizeof (*lr));
1698 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1699 rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length,
1702 tx = dmu_tx_create(os);
1704 dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length);
1706 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1708 ztest_range_unlock(rl);
1709 ztest_object_unlock(zd, lr->lr_foid);
1713 VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset,
1714 lr->lr_length, tx) == 0);
1716 (void) ztest_log_truncate(zd, tx, lr);
1720 ztest_range_unlock(rl);
1721 ztest_object_unlock(zd, lr->lr_foid);
1727 ztest_replay_setattr(ztest_ds_t *zd, lr_setattr_t *lr, boolean_t byteswap)
1729 objset_t *os = zd->zd_os;
1732 ztest_block_tag_t *bbt;
1733 uint64_t txg, lrtxg, crtxg;
1736 byteswap_uint64_array(lr, sizeof (*lr));
1738 ztest_object_lock(zd, lr->lr_foid, RL_WRITER);
1740 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1742 tx = dmu_tx_create(os);
1743 dmu_tx_hold_bonus(tx, lr->lr_foid);
1745 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1747 dmu_buf_rele(db, FTAG);
1748 ztest_object_unlock(zd, lr->lr_foid);
1752 bbt = ztest_bt_bonus(db);
1753 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1754 crtxg = bbt->bt_crtxg;
1755 lrtxg = lr->lr_common.lrc_txg;
1757 if (zd->zd_zilog->zl_replay) {
1758 ASSERT(lr->lr_size != 0);
1759 ASSERT(lr->lr_mode != 0);
1763 * Randomly change the size and increment the generation.
1765 lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) *
1767 lr->lr_mode = bbt->bt_gen + 1;
1772 * Verify that the current bonus buffer is not newer than our txg.
1774 ztest_bt_verify(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode,
1775 MAX(txg, lrtxg), crtxg);
1777 dmu_buf_will_dirty(db, tx);
1779 ASSERT3U(lr->lr_size, >=, sizeof (*bbt));
1780 ASSERT3U(lr->lr_size, <=, db->db_size);
1781 VERIFY0(dmu_set_bonus(db, lr->lr_size, tx));
1782 bbt = ztest_bt_bonus(db);
1784 ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode, txg, crtxg);
1786 dmu_buf_rele(db, FTAG);
1788 (void) ztest_log_setattr(zd, tx, lr);
1792 ztest_object_unlock(zd, lr->lr_foid);
1797 zil_replay_func_t ztest_replay_vector[TX_MAX_TYPE] = {
1798 NULL, /* 0 no such transaction type */
1799 (zil_replay_func_t)ztest_replay_create, /* TX_CREATE */
1800 NULL, /* TX_MKDIR */
1801 NULL, /* TX_MKXATTR */
1802 NULL, /* TX_SYMLINK */
1803 (zil_replay_func_t)ztest_replay_remove, /* TX_REMOVE */
1804 NULL, /* TX_RMDIR */
1806 NULL, /* TX_RENAME */
1807 (zil_replay_func_t)ztest_replay_write, /* TX_WRITE */
1808 (zil_replay_func_t)ztest_replay_truncate, /* TX_TRUNCATE */
1809 (zil_replay_func_t)ztest_replay_setattr, /* TX_SETATTR */
1811 NULL, /* TX_CREATE_ACL */
1812 NULL, /* TX_CREATE_ATTR */
1813 NULL, /* TX_CREATE_ACL_ATTR */
1814 NULL, /* TX_MKDIR_ACL */
1815 NULL, /* TX_MKDIR_ATTR */
1816 NULL, /* TX_MKDIR_ACL_ATTR */
1817 NULL, /* TX_WRITE2 */
1821 * ZIL get_data callbacks
1825 ztest_get_done(zgd_t *zgd, int error)
1827 ztest_ds_t *zd = zgd->zgd_private;
1828 uint64_t object = zgd->zgd_rl->rl_object;
1831 dmu_buf_rele(zgd->zgd_db, zgd);
1833 ztest_range_unlock(zgd->zgd_rl);
1834 ztest_object_unlock(zd, object);
1836 if (error == 0 && zgd->zgd_bp)
1837 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1839 umem_free(zgd, sizeof (*zgd));
1843 ztest_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1845 ztest_ds_t *zd = arg;
1846 objset_t *os = zd->zd_os;
1847 uint64_t object = lr->lr_foid;
1848 uint64_t offset = lr->lr_offset;
1849 uint64_t size = lr->lr_length;
1850 blkptr_t *bp = &lr->lr_blkptr;
1851 uint64_t txg = lr->lr_common.lrc_txg;
1853 dmu_object_info_t doi;
1858 ztest_object_lock(zd, object, RL_READER);
1859 error = dmu_bonus_hold(os, object, FTAG, &db);
1861 ztest_object_unlock(zd, object);
1865 crtxg = ztest_bt_bonus(db)->bt_crtxg;
1867 if (crtxg == 0 || crtxg > txg) {
1868 dmu_buf_rele(db, FTAG);
1869 ztest_object_unlock(zd, object);
1873 dmu_object_info_from_db(db, &doi);
1874 dmu_buf_rele(db, FTAG);
1877 zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL);
1878 zgd->zgd_zilog = zd->zd_zilog;
1879 zgd->zgd_private = zd;
1881 if (buf != NULL) { /* immediate write */
1882 zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
1885 error = dmu_read(os, object, offset, size, buf,
1886 DMU_READ_NO_PREFETCH);
1889 size = doi.doi_data_block_size;
1891 offset = P2ALIGN(offset, size);
1893 ASSERT(offset < size);
1897 zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
1900 error = dmu_buf_hold(os, object, offset, zgd, &db,
1901 DMU_READ_NO_PREFETCH);
1904 blkptr_t *obp = dmu_buf_get_blkptr(db);
1906 ASSERT(BP_IS_HOLE(bp));
1913 ASSERT(db->db_offset == offset);
1914 ASSERT(db->db_size == size);
1916 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1917 ztest_get_done, zgd);
1924 ztest_get_done(zgd, error);
1930 ztest_lr_alloc(size_t lrsize, char *name)
1933 size_t namesize = name ? strlen(name) + 1 : 0;
1935 lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
1938 bcopy(name, lr + lrsize, namesize);
1944 ztest_lr_free(void *lr, size_t lrsize, char *name)
1946 size_t namesize = name ? strlen(name) + 1 : 0;
1948 umem_free(lr, lrsize + namesize);
1952 * Lookup a bunch of objects. Returns the number of objects not found.
1955 ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
1961 ASSERT(mutex_held(&zd->zd_dirobj_lock));
1963 for (i = 0; i < count; i++, od++) {
1965 error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
1966 sizeof (uint64_t), 1, &od->od_object);
1968 ASSERT(error == ENOENT);
1969 ASSERT(od->od_object == 0);
1973 ztest_block_tag_t *bbt;
1974 dmu_object_info_t doi;
1976 ASSERT(od->od_object != 0);
1977 ASSERT(missing == 0); /* there should be no gaps */
1979 ztest_object_lock(zd, od->od_object, RL_READER);
1980 VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
1981 od->od_object, FTAG, &db));
1982 dmu_object_info_from_db(db, &doi);
1983 bbt = ztest_bt_bonus(db);
1984 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1985 od->od_type = doi.doi_type;
1986 od->od_blocksize = doi.doi_data_block_size;
1987 od->od_gen = bbt->bt_gen;
1988 dmu_buf_rele(db, FTAG);
1989 ztest_object_unlock(zd, od->od_object);
1997 ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
2002 ASSERT(mutex_held(&zd->zd_dirobj_lock));
2004 for (i = 0; i < count; i++, od++) {
2011 lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2013 lr->lr_doid = od->od_dir;
2014 lr->lr_foid = 0; /* 0 to allocate, > 0 to claim */
2015 lr->lrz_type = od->od_crtype;
2016 lr->lrz_blocksize = od->od_crblocksize;
2017 lr->lrz_ibshift = ztest_random_ibshift();
2018 lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
2019 lr->lrz_bonuslen = dmu_bonus_max();
2020 lr->lr_gen = od->od_crgen;
2021 lr->lr_crtime[0] = time(NULL);
2023 if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
2024 ASSERT(missing == 0);
2028 od->od_object = lr->lr_foid;
2029 od->od_type = od->od_crtype;
2030 od->od_blocksize = od->od_crblocksize;
2031 od->od_gen = od->od_crgen;
2032 ASSERT(od->od_object != 0);
2035 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2042 ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
2048 ASSERT(mutex_held(&zd->zd_dirobj_lock));
2052 for (i = count - 1; i >= 0; i--, od--) {
2059 * No object was found.
2061 if (od->od_object == 0)
2064 lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2066 lr->lr_doid = od->od_dir;
2068 if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
2069 ASSERT3U(error, ==, ENOSPC);
2074 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2081 ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
2087 lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
2089 lr->lr_foid = object;
2090 lr->lr_offset = offset;
2091 lr->lr_length = size;
2093 BP_ZERO(&lr->lr_blkptr);
2095 bcopy(data, lr + 1, size);
2097 error = ztest_replay_write(zd, lr, B_FALSE);
2099 ztest_lr_free(lr, sizeof (*lr) + size, NULL);
2105 ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2110 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2112 lr->lr_foid = object;
2113 lr->lr_offset = offset;
2114 lr->lr_length = size;
2116 error = ztest_replay_truncate(zd, lr, B_FALSE);
2118 ztest_lr_free(lr, sizeof (*lr), NULL);
2124 ztest_setattr(ztest_ds_t *zd, uint64_t object)
2129 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2131 lr->lr_foid = object;
2135 error = ztest_replay_setattr(zd, lr, B_FALSE);
2137 ztest_lr_free(lr, sizeof (*lr), NULL);
2143 ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2145 objset_t *os = zd->zd_os;
2150 txg_wait_synced(dmu_objset_pool(os), 0);
2152 ztest_object_lock(zd, object, RL_READER);
2153 rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
2155 tx = dmu_tx_create(os);
2157 dmu_tx_hold_write(tx, object, offset, size);
2159 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
2162 dmu_prealloc(os, object, offset, size, tx);
2164 txg_wait_synced(dmu_objset_pool(os), txg);
2166 (void) dmu_free_long_range(os, object, offset, size);
2169 ztest_range_unlock(rl);
2170 ztest_object_unlock(zd, object);
2174 ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
2177 ztest_block_tag_t wbt;
2178 dmu_object_info_t doi;
2179 enum ztest_io_type io_type;
2183 VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
2184 blocksize = doi.doi_data_block_size;
2185 data = umem_alloc(blocksize, UMEM_NOFAIL);
2188 * Pick an i/o type at random, biased toward writing block tags.
2190 io_type = ztest_random(ZTEST_IO_TYPES);
2191 if (ztest_random(2) == 0)
2192 io_type = ZTEST_IO_WRITE_TAG;
2194 (void) rw_enter(&zd->zd_zilog_lock, RW_READER);
2198 case ZTEST_IO_WRITE_TAG:
2199 ztest_bt_generate(&wbt, zd->zd_os, object, offset, 0, 0, 0);
2200 (void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
2203 case ZTEST_IO_WRITE_PATTERN:
2204 (void) memset(data, 'a' + (object + offset) % 5, blocksize);
2205 if (ztest_random(2) == 0) {
2207 * Induce fletcher2 collisions to ensure that
2208 * zio_ddt_collision() detects and resolves them
2209 * when using fletcher2-verify for deduplication.
2211 ((uint64_t *)data)[0] ^= 1ULL << 63;
2212 ((uint64_t *)data)[4] ^= 1ULL << 63;
2214 (void) ztest_write(zd, object, offset, blocksize, data);
2217 case ZTEST_IO_WRITE_ZEROES:
2218 bzero(data, blocksize);
2219 (void) ztest_write(zd, object, offset, blocksize, data);
2222 case ZTEST_IO_TRUNCATE:
2223 (void) ztest_truncate(zd, object, offset, blocksize);
2226 case ZTEST_IO_SETATTR:
2227 (void) ztest_setattr(zd, object);
2232 case ZTEST_IO_REWRITE:
2233 (void) rw_enter(&ztest_name_lock, RW_READER);
2234 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2235 ZFS_PROP_CHECKSUM, spa_dedup_checksum(ztest_spa),
2237 VERIFY(err == 0 || err == ENOSPC);
2238 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2239 ZFS_PROP_COMPRESSION,
2240 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION),
2242 VERIFY(err == 0 || err == ENOSPC);
2243 (void) rw_exit(&ztest_name_lock);
2245 VERIFY0(dmu_read(zd->zd_os, object, offset, blocksize, data,
2246 DMU_READ_NO_PREFETCH));
2248 (void) ztest_write(zd, object, offset, blocksize, data);
2252 (void) rw_exit(&zd->zd_zilog_lock);
2254 umem_free(data, blocksize);
2258 * Initialize an object description template.
2261 ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
2262 dmu_object_type_t type, uint64_t blocksize, uint64_t gen)
2264 od->od_dir = ZTEST_DIROBJ;
2267 od->od_crtype = type;
2268 od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
2271 od->od_type = DMU_OT_NONE;
2272 od->od_blocksize = 0;
2275 (void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
2276 tag, (longlong_t)id, (u_longlong_t)index);
2280 * Lookup or create the objects for a test using the od template.
2281 * If the objects do not all exist, or if 'remove' is specified,
2282 * remove any existing objects and create new ones. Otherwise,
2283 * use the existing objects.
2286 ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
2288 int count = size / sizeof (*od);
2291 mutex_enter(&zd->zd_dirobj_lock);
2292 if ((ztest_lookup(zd, od, count) != 0 || remove) &&
2293 (ztest_remove(zd, od, count) != 0 ||
2294 ztest_create(zd, od, count) != 0))
2297 mutex_exit(&zd->zd_dirobj_lock);
2304 ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
2306 zilog_t *zilog = zd->zd_zilog;
2308 (void) rw_enter(&zd->zd_zilog_lock, RW_READER);
2310 zil_commit(zilog, ztest_random(ZTEST_OBJECTS));
2313 * Remember the committed values in zd, which is in parent/child
2314 * shared memory. If we die, the next iteration of ztest_run()
2315 * will verify that the log really does contain this record.
2317 mutex_enter(&zilog->zl_lock);
2318 ASSERT(zd->zd_shared != NULL);
2319 ASSERT3U(zd->zd_shared->zd_seq, <=, zilog->zl_commit_lr_seq);
2320 zd->zd_shared->zd_seq = zilog->zl_commit_lr_seq;
2321 mutex_exit(&zilog->zl_lock);
2323 (void) rw_exit(&zd->zd_zilog_lock);
2327 * This function is designed to simulate the operations that occur during a
2328 * mount/unmount operation. We hold the dataset across these operations in an
2329 * attempt to expose any implicit assumptions about ZIL management.
2333 ztest_zil_remount(ztest_ds_t *zd, uint64_t id)
2335 objset_t *os = zd->zd_os;
2338 * We grab the zd_dirobj_lock to ensure that no other thread is
2339 * updating the zil (i.e. adding in-memory log records) and the
2340 * zd_zilog_lock to block any I/O.
2342 mutex_enter(&zd->zd_dirobj_lock);
2343 (void) rw_enter(&zd->zd_zilog_lock, RW_WRITER);
2345 /* zfs_sb_teardown() */
2346 zil_close(zd->zd_zilog);
2348 /* zfsvfs_setup() */
2349 VERIFY(zil_open(os, ztest_get_data) == zd->zd_zilog);
2350 zil_replay(os, zd, ztest_replay_vector);
2352 (void) rw_exit(&zd->zd_zilog_lock);
2353 mutex_exit(&zd->zd_dirobj_lock);
2357 * Verify that we can't destroy an active pool, create an existing pool,
2358 * or create a pool with a bad vdev spec.
2362 ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2364 ztest_shared_opts_t *zo = &ztest_opts;
2369 * Attempt to create using a bad file.
2371 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2372 VERIFY3U(ENOENT, ==,
2373 spa_create("ztest_bad_file", nvroot, NULL, NULL));
2374 nvlist_free(nvroot);
2377 * Attempt to create using a bad mirror.
2379 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 2, 1);
2380 VERIFY3U(ENOENT, ==,
2381 spa_create("ztest_bad_mirror", nvroot, NULL, NULL));
2382 nvlist_free(nvroot);
2385 * Attempt to create an existing pool. It shouldn't matter
2386 * what's in the nvroot; we should fail with EEXIST.
2388 (void) rw_enter(&ztest_name_lock, RW_READER);
2389 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2390 VERIFY3U(EEXIST, ==, spa_create(zo->zo_pool, nvroot, NULL, NULL));
2391 nvlist_free(nvroot);
2392 VERIFY3U(0, ==, spa_open(zo->zo_pool, &spa, FTAG));
2393 VERIFY3U(EBUSY, ==, spa_destroy(zo->zo_pool));
2394 spa_close(spa, FTAG);
2396 (void) rw_exit(&ztest_name_lock);
2401 ztest_spa_upgrade(ztest_ds_t *zd, uint64_t id)
2404 uint64_t initial_version = SPA_VERSION_INITIAL;
2405 uint64_t version, newversion;
2406 nvlist_t *nvroot, *props;
2409 mutex_enter(&ztest_vdev_lock);
2410 name = kmem_asprintf("%s_upgrade", ztest_opts.zo_pool);
2413 * Clean up from previous runs.
2415 (void) spa_destroy(name);
2417 nvroot = make_vdev_root(NULL, NULL, name, ztest_opts.zo_vdev_size, 0,
2418 0, ztest_opts.zo_raidz, ztest_opts.zo_mirrors, 1);
2421 * If we're configuring a RAIDZ device then make sure that the
2422 * the initial version is capable of supporting that feature.
2424 switch (ztest_opts.zo_raidz_parity) {
2427 initial_version = SPA_VERSION_INITIAL;
2430 initial_version = SPA_VERSION_RAIDZ2;
2433 initial_version = SPA_VERSION_RAIDZ3;
2438 * Create a pool with a spa version that can be upgraded. Pick
2439 * a value between initial_version and SPA_VERSION_BEFORE_FEATURES.
2442 version = ztest_random_spa_version(initial_version);
2443 } while (version > SPA_VERSION_BEFORE_FEATURES);
2445 props = fnvlist_alloc();
2446 fnvlist_add_uint64(props,
2447 zpool_prop_to_name(ZPOOL_PROP_VERSION), version);
2448 VERIFY3S(spa_create(name, nvroot, props, NULL), ==, 0);
2449 fnvlist_free(nvroot);
2450 fnvlist_free(props);
2452 VERIFY3S(spa_open(name, &spa, FTAG), ==, 0);
2453 VERIFY3U(spa_version(spa), ==, version);
2454 newversion = ztest_random_spa_version(version + 1);
2456 if (ztest_opts.zo_verbose >= 4) {
2457 (void) printf("upgrading spa version from %llu to %llu\n",
2458 (u_longlong_t)version, (u_longlong_t)newversion);
2461 spa_upgrade(spa, newversion);
2462 VERIFY3U(spa_version(spa), >, version);
2463 VERIFY3U(spa_version(spa), ==, fnvlist_lookup_uint64(spa->spa_config,
2464 zpool_prop_to_name(ZPOOL_PROP_VERSION)));
2465 spa_close(spa, FTAG);
2468 mutex_exit(&ztest_vdev_lock);
2472 vdev_lookup_by_path(vdev_t *vd, const char *path)
2477 if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
2480 for (c = 0; c < vd->vdev_children; c++)
2481 if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
2489 * Find the first available hole which can be used as a top-level.
2492 find_vdev_hole(spa_t *spa)
2494 vdev_t *rvd = spa->spa_root_vdev;
2497 ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
2499 for (c = 0; c < rvd->vdev_children; c++) {
2500 vdev_t *cvd = rvd->vdev_child[c];
2502 if (cvd->vdev_ishole)
2509 * Verify that vdev_add() works as expected.
2513 ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2515 ztest_shared_t *zs = ztest_shared;
2516 spa_t *spa = ztest_spa;
2522 mutex_enter(&ztest_vdev_lock);
2523 leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * ztest_opts.zo_raidz;
2525 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2527 ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2530 * If we have slogs then remove them 1/4 of the time.
2532 if (spa_has_slogs(spa) && ztest_random(4) == 0) {
2534 * Grab the guid from the head of the log class rotor.
2536 guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid;
2538 spa_config_exit(spa, SCL_VDEV, FTAG);
2541 * We have to grab the zs_name_lock as writer to
2542 * prevent a race between removing a slog (dmu_objset_find)
2543 * and destroying a dataset. Removing the slog will
2544 * grab a reference on the dataset which may cause
2545 * dsl_destroy_head() to fail with EBUSY thus
2546 * leaving the dataset in an inconsistent state.
2548 rw_enter(&ztest_name_lock, RW_WRITER);
2549 error = spa_vdev_remove(spa, guid, B_FALSE);
2550 rw_exit(&ztest_name_lock);
2552 if (error && error != EEXIST)
2553 fatal(0, "spa_vdev_remove() = %d", error);
2555 spa_config_exit(spa, SCL_VDEV, FTAG);
2558 * Make 1/4 of the devices be log devices.
2560 nvroot = make_vdev_root(NULL, NULL, NULL,
2561 ztest_opts.zo_vdev_size, 0,
2562 ztest_random(4) == 0, ztest_opts.zo_raidz,
2565 error = spa_vdev_add(spa, nvroot);
2566 nvlist_free(nvroot);
2568 if (error == ENOSPC)
2569 ztest_record_enospc("spa_vdev_add");
2570 else if (error != 0)
2571 fatal(0, "spa_vdev_add() = %d", error);
2574 mutex_exit(&ztest_vdev_lock);
2578 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
2582 ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
2584 ztest_shared_t *zs = ztest_shared;
2585 spa_t *spa = ztest_spa;
2586 vdev_t *rvd = spa->spa_root_vdev;
2587 spa_aux_vdev_t *sav;
2593 path = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2595 if (ztest_random(2) == 0) {
2596 sav = &spa->spa_spares;
2597 aux = ZPOOL_CONFIG_SPARES;
2599 sav = &spa->spa_l2cache;
2600 aux = ZPOOL_CONFIG_L2CACHE;
2603 mutex_enter(&ztest_vdev_lock);
2605 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2607 if (sav->sav_count != 0 && ztest_random(4) == 0) {
2609 * Pick a random device to remove.
2611 guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
2614 * Find an unused device we can add.
2616 zs->zs_vdev_aux = 0;
2619 (void) snprintf(path, MAXPATHLEN, ztest_aux_template,
2620 ztest_opts.zo_dir, ztest_opts.zo_pool, aux,
2622 for (c = 0; c < sav->sav_count; c++)
2623 if (strcmp(sav->sav_vdevs[c]->vdev_path,
2626 if (c == sav->sav_count &&
2627 vdev_lookup_by_path(rvd, path) == NULL)
2633 spa_config_exit(spa, SCL_VDEV, FTAG);
2639 nvlist_t *nvroot = make_vdev_root(NULL, aux, NULL,
2640 (ztest_opts.zo_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
2641 error = spa_vdev_add(spa, nvroot);
2643 fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
2644 nvlist_free(nvroot);
2647 * Remove an existing device. Sometimes, dirty its
2648 * vdev state first to make sure we handle removal
2649 * of devices that have pending state changes.
2651 if (ztest_random(2) == 0)
2652 (void) vdev_online(spa, guid, 0, NULL);
2654 error = spa_vdev_remove(spa, guid, B_FALSE);
2655 if (error != 0 && error != EBUSY)
2656 fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
2659 mutex_exit(&ztest_vdev_lock);
2661 umem_free(path, MAXPATHLEN);
2665 * split a pool if it has mirror tlvdevs
2669 ztest_split_pool(ztest_ds_t *zd, uint64_t id)
2671 ztest_shared_t *zs = ztest_shared;
2672 spa_t *spa = ztest_spa;
2673 vdev_t *rvd = spa->spa_root_vdev;
2674 nvlist_t *tree, **child, *config, *split, **schild;
2675 uint_t c, children, schildren = 0, lastlogid = 0;
2678 mutex_enter(&ztest_vdev_lock);
2680 /* ensure we have a useable config; mirrors of raidz aren't supported */
2681 if (zs->zs_mirrors < 3 || ztest_opts.zo_raidz > 1) {
2682 mutex_exit(&ztest_vdev_lock);
2686 /* clean up the old pool, if any */
2687 (void) spa_destroy("splitp");
2689 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2691 /* generate a config from the existing config */
2692 mutex_enter(&spa->spa_props_lock);
2693 VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
2695 mutex_exit(&spa->spa_props_lock);
2697 VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2700 schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
2701 for (c = 0; c < children; c++) {
2702 vdev_t *tvd = rvd->vdev_child[c];
2706 if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
2707 VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
2709 VERIFY(nvlist_add_string(schild[schildren],
2710 ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
2711 VERIFY(nvlist_add_uint64(schild[schildren],
2712 ZPOOL_CONFIG_IS_HOLE, 1) == 0);
2714 lastlogid = schildren;
2719 VERIFY(nvlist_lookup_nvlist_array(child[c],
2720 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2721 VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
2724 /* OK, create a config that can be used to split */
2725 VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
2726 VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
2727 VDEV_TYPE_ROOT) == 0);
2728 VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
2729 lastlogid != 0 ? lastlogid : schildren) == 0);
2731 VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
2732 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
2734 for (c = 0; c < schildren; c++)
2735 nvlist_free(schild[c]);
2739 spa_config_exit(spa, SCL_VDEV, FTAG);
2741 (void) rw_enter(&ztest_name_lock, RW_WRITER);
2742 error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
2743 (void) rw_exit(&ztest_name_lock);
2745 nvlist_free(config);
2748 (void) printf("successful split - results:\n");
2749 mutex_enter(&spa_namespace_lock);
2750 show_pool_stats(spa);
2751 show_pool_stats(spa_lookup("splitp"));
2752 mutex_exit(&spa_namespace_lock);
2756 mutex_exit(&ztest_vdev_lock);
2761 * Verify that we can attach and detach devices.
2765 ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
2767 ztest_shared_t *zs = ztest_shared;
2768 spa_t *spa = ztest_spa;
2769 spa_aux_vdev_t *sav = &spa->spa_spares;
2770 vdev_t *rvd = spa->spa_root_vdev;
2771 vdev_t *oldvd, *newvd, *pvd;
2775 uint64_t ashift = ztest_get_ashift();
2776 uint64_t oldguid, pguid;
2777 size_t oldsize, newsize;
2778 char *oldpath, *newpath;
2780 int oldvd_has_siblings = B_FALSE;
2781 int newvd_is_spare = B_FALSE;
2783 int error, expected_error;
2785 oldpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2786 newpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2788 mutex_enter(&ztest_vdev_lock);
2789 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
2791 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2794 * Decide whether to do an attach or a replace.
2796 replacing = ztest_random(2);
2799 * Pick a random top-level vdev.
2801 top = ztest_random_vdev_top(spa, B_TRUE);
2804 * Pick a random leaf within it.
2806 leaf = ztest_random(leaves);
2811 oldvd = rvd->vdev_child[top];
2812 if (zs->zs_mirrors >= 1) {
2813 ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
2814 ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
2815 oldvd = oldvd->vdev_child[leaf / ztest_opts.zo_raidz];
2817 if (ztest_opts.zo_raidz > 1) {
2818 ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
2819 ASSERT(oldvd->vdev_children == ztest_opts.zo_raidz);
2820 oldvd = oldvd->vdev_child[leaf % ztest_opts.zo_raidz];
2824 * If we're already doing an attach or replace, oldvd may be a
2825 * mirror vdev -- in which case, pick a random child.
2827 while (oldvd->vdev_children != 0) {
2828 oldvd_has_siblings = B_TRUE;
2829 ASSERT(oldvd->vdev_children >= 2);
2830 oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
2833 oldguid = oldvd->vdev_guid;
2834 oldsize = vdev_get_min_asize(oldvd);
2835 oldvd_is_log = oldvd->vdev_top->vdev_islog;
2836 (void) strcpy(oldpath, oldvd->vdev_path);
2837 pvd = oldvd->vdev_parent;
2838 pguid = pvd->vdev_guid;
2841 * If oldvd has siblings, then half of the time, detach it.
2843 if (oldvd_has_siblings && ztest_random(2) == 0) {
2844 spa_config_exit(spa, SCL_VDEV, FTAG);
2845 error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
2846 if (error != 0 && error != ENODEV && error != EBUSY &&
2848 fatal(0, "detach (%s) returned %d", oldpath, error);
2853 * For the new vdev, choose with equal probability between the two
2854 * standard paths (ending in either 'a' or 'b') or a random hot spare.
2856 if (sav->sav_count != 0 && ztest_random(3) == 0) {
2857 newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
2858 newvd_is_spare = B_TRUE;
2859 (void) strcpy(newpath, newvd->vdev_path);
2861 (void) snprintf(newpath, MAXPATHLEN, ztest_dev_template,
2862 ztest_opts.zo_dir, ztest_opts.zo_pool,
2863 top * leaves + leaf);
2864 if (ztest_random(2) == 0)
2865 newpath[strlen(newpath) - 1] = 'b';
2866 newvd = vdev_lookup_by_path(rvd, newpath);
2870 newsize = vdev_get_min_asize(newvd);
2873 * Make newsize a little bigger or smaller than oldsize.
2874 * If it's smaller, the attach should fail.
2875 * If it's larger, and we're doing a replace,
2876 * we should get dynamic LUN growth when we're done.
2878 newsize = 10 * oldsize / (9 + ztest_random(3));
2882 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
2883 * unless it's a replace; in that case any non-replacing parent is OK.
2885 * If newvd is already part of the pool, it should fail with EBUSY.
2887 * If newvd is too small, it should fail with EOVERFLOW.
2889 if (pvd->vdev_ops != &vdev_mirror_ops &&
2890 pvd->vdev_ops != &vdev_root_ops && (!replacing ||
2891 pvd->vdev_ops == &vdev_replacing_ops ||
2892 pvd->vdev_ops == &vdev_spare_ops))
2893 expected_error = ENOTSUP;
2894 else if (newvd_is_spare && (!replacing || oldvd_is_log))
2895 expected_error = ENOTSUP;
2896 else if (newvd == oldvd)
2897 expected_error = replacing ? 0 : EBUSY;
2898 else if (vdev_lookup_by_path(rvd, newpath) != NULL)
2899 expected_error = EBUSY;
2900 else if (newsize < oldsize)
2901 expected_error = EOVERFLOW;
2902 else if (ashift > oldvd->vdev_top->vdev_ashift)
2903 expected_error = EDOM;
2907 spa_config_exit(spa, SCL_VDEV, FTAG);
2910 * Build the nvlist describing newpath.
2912 root = make_vdev_root(newpath, NULL, NULL, newvd == NULL ? newsize : 0,
2913 ashift, 0, 0, 0, 1);
2915 error = spa_vdev_attach(spa, oldguid, root, replacing);
2920 * If our parent was the replacing vdev, but the replace completed,
2921 * then instead of failing with ENOTSUP we may either succeed,
2922 * fail with ENODEV, or fail with EOVERFLOW.
2924 if (expected_error == ENOTSUP &&
2925 (error == 0 || error == ENODEV || error == EOVERFLOW))
2926 expected_error = error;
2929 * If someone grew the LUN, the replacement may be too small.
2931 if (error == EOVERFLOW || error == EBUSY)
2932 expected_error = error;
2934 /* XXX workaround 6690467 */
2935 if (error != expected_error && expected_error != EBUSY) {
2936 fatal(0, "attach (%s %llu, %s %llu, %d) "
2937 "returned %d, expected %d",
2938 oldpath, (longlong_t)oldsize, newpath,
2939 (longlong_t)newsize, replacing, error, expected_error);
2942 mutex_exit(&ztest_vdev_lock);
2944 umem_free(oldpath, MAXPATHLEN);
2945 umem_free(newpath, MAXPATHLEN);
2949 * Callback function which expands the physical size of the vdev.
2952 grow_vdev(vdev_t *vd, void *arg)
2954 ASSERTV(spa_t *spa = vd->vdev_spa);
2955 size_t *newsize = arg;
2959 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
2960 ASSERT(vd->vdev_ops->vdev_op_leaf);
2962 if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
2965 fsize = lseek(fd, 0, SEEK_END);
2966 VERIFY(ftruncate(fd, *newsize) == 0);
2968 if (ztest_opts.zo_verbose >= 6) {
2969 (void) printf("%s grew from %lu to %lu bytes\n",
2970 vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
2977 * Callback function which expands a given vdev by calling vdev_online().
2981 online_vdev(vdev_t *vd, void *arg)
2983 spa_t *spa = vd->vdev_spa;
2984 vdev_t *tvd = vd->vdev_top;
2985 uint64_t guid = vd->vdev_guid;
2986 uint64_t generation = spa->spa_config_generation + 1;
2987 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
2990 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
2991 ASSERT(vd->vdev_ops->vdev_op_leaf);
2993 /* Calling vdev_online will initialize the new metaslabs */
2994 spa_config_exit(spa, SCL_STATE, spa);
2995 error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
2996 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
2999 * If vdev_online returned an error or the underlying vdev_open
3000 * failed then we abort the expand. The only way to know that
3001 * vdev_open fails is by checking the returned newstate.
3003 if (error || newstate != VDEV_STATE_HEALTHY) {
3004 if (ztest_opts.zo_verbose >= 5) {
3005 (void) printf("Unable to expand vdev, state %llu, "
3006 "error %d\n", (u_longlong_t)newstate, error);
3010 ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
3013 * Since we dropped the lock we need to ensure that we're
3014 * still talking to the original vdev. It's possible this
3015 * vdev may have been detached/replaced while we were
3016 * trying to online it.
3018 if (generation != spa->spa_config_generation) {
3019 if (ztest_opts.zo_verbose >= 5) {
3020 (void) printf("vdev configuration has changed, "
3021 "guid %llu, state %llu, expected gen %llu, "
3024 (u_longlong_t)tvd->vdev_state,
3025 (u_longlong_t)generation,
3026 (u_longlong_t)spa->spa_config_generation);
3034 * Traverse the vdev tree calling the supplied function.
3035 * We continue to walk the tree until we either have walked all
3036 * children or we receive a non-NULL return from the callback.
3037 * If a NULL callback is passed, then we just return back the first
3038 * leaf vdev we encounter.
3041 vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
3045 if (vd->vdev_ops->vdev_op_leaf) {
3049 return (func(vd, arg));
3052 for (c = 0; c < vd->vdev_children; c++) {
3053 vdev_t *cvd = vd->vdev_child[c];
3054 if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
3061 * Verify that dynamic LUN growth works as expected.
3065 ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
3067 spa_t *spa = ztest_spa;
3069 metaslab_class_t *mc;
3070 metaslab_group_t *mg;
3071 size_t psize, newsize;
3073 uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
3075 mutex_enter(&ztest_vdev_lock);
3076 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3078 top = ztest_random_vdev_top(spa, B_TRUE);
3080 tvd = spa->spa_root_vdev->vdev_child[top];
3083 old_ms_count = tvd->vdev_ms_count;
3084 old_class_space = metaslab_class_get_space(mc);
3087 * Determine the size of the first leaf vdev associated with
3088 * our top-level device.
3090 vd = vdev_walk_tree(tvd, NULL, NULL);
3091 ASSERT3P(vd, !=, NULL);
3092 ASSERT(vd->vdev_ops->vdev_op_leaf);
3094 psize = vd->vdev_psize;
3097 * We only try to expand the vdev if it's healthy, less than 4x its
3098 * original size, and it has a valid psize.
3100 if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
3101 psize == 0 || psize >= 4 * ztest_opts.zo_vdev_size) {
3102 spa_config_exit(spa, SCL_STATE, spa);
3103 mutex_exit(&ztest_vdev_lock);
3107 newsize = psize + psize / 8;
3108 ASSERT3U(newsize, >, psize);
3110 if (ztest_opts.zo_verbose >= 6) {
3111 (void) printf("Expanding LUN %s from %lu to %lu\n",
3112 vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
3116 * Growing the vdev is a two step process:
3117 * 1). expand the physical size (i.e. relabel)
3118 * 2). online the vdev to create the new metaslabs
3120 if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
3121 vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
3122 tvd->vdev_state != VDEV_STATE_HEALTHY) {
3123 if (ztest_opts.zo_verbose >= 5) {
3124 (void) printf("Could not expand LUN because "
3125 "the vdev configuration changed.\n");
3127 spa_config_exit(spa, SCL_STATE, spa);
3128 mutex_exit(&ztest_vdev_lock);
3132 spa_config_exit(spa, SCL_STATE, spa);
3135 * Expanding the LUN will update the config asynchronously,
3136 * thus we must wait for the async thread to complete any
3137 * pending tasks before proceeding.
3141 mutex_enter(&spa->spa_async_lock);
3142 done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
3143 mutex_exit(&spa->spa_async_lock);
3146 txg_wait_synced(spa_get_dsl(spa), 0);
3147 (void) poll(NULL, 0, 100);
3150 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3152 tvd = spa->spa_root_vdev->vdev_child[top];
3153 new_ms_count = tvd->vdev_ms_count;
3154 new_class_space = metaslab_class_get_space(mc);
3156 if (tvd->vdev_mg != mg || mg->mg_class != mc) {
3157 if (ztest_opts.zo_verbose >= 5) {
3158 (void) printf("Could not verify LUN expansion due to "
3159 "intervening vdev offline or remove.\n");
3161 spa_config_exit(spa, SCL_STATE, spa);
3162 mutex_exit(&ztest_vdev_lock);
3167 * Make sure we were able to grow the vdev.
3169 if (new_ms_count <= old_ms_count)
3170 fatal(0, "LUN expansion failed: ms_count %llu <= %llu\n",
3171 old_ms_count, new_ms_count);
3174 * Make sure we were able to grow the pool.
3176 if (new_class_space <= old_class_space)
3177 fatal(0, "LUN expansion failed: class_space %llu <= %llu\n",
3178 old_class_space, new_class_space);
3180 if (ztest_opts.zo_verbose >= 5) {
3181 char oldnumbuf[6], newnumbuf[6];
3183 nicenum(old_class_space, oldnumbuf);
3184 nicenum(new_class_space, newnumbuf);
3185 (void) printf("%s grew from %s to %s\n",
3186 spa->spa_name, oldnumbuf, newnumbuf);
3189 spa_config_exit(spa, SCL_STATE, spa);
3190 mutex_exit(&ztest_vdev_lock);
3194 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
3198 ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3201 * Create the objects common to all ztest datasets.
3203 VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
3204 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
3208 ztest_dataset_create(char *dsname)
3210 uint64_t zilset = ztest_random(100);
3211 int err = dmu_objset_create(dsname, DMU_OST_OTHER, 0,
3212 ztest_objset_create_cb, NULL);
3214 if (err || zilset < 80)
3217 if (ztest_opts.zo_verbose >= 5)
3218 (void) printf("Setting dataset %s to sync always\n", dsname);
3219 return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC,
3220 ZFS_SYNC_ALWAYS, B_FALSE));
3225 ztest_objset_destroy_cb(const char *name, void *arg)
3228 dmu_object_info_t doi;
3232 * Verify that the dataset contains a directory object.
3234 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_TRUE, FTAG, &os));
3235 error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
3236 if (error != ENOENT) {
3237 /* We could have crashed in the middle of destroying it */
3239 ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
3240 ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
3242 dmu_objset_disown(os, FTAG);
3245 * Destroy the dataset.
3247 if (strchr(name, '@') != NULL) {
3248 VERIFY0(dsl_destroy_snapshot(name, B_FALSE));
3250 VERIFY0(dsl_destroy_head(name));
3256 ztest_snapshot_create(char *osname, uint64_t id)
3258 char snapname[MAXNAMELEN];
3261 (void) snprintf(snapname, sizeof (snapname), "%llu", (u_longlong_t)id);
3263 error = dmu_objset_snapshot_one(osname, snapname);
3264 if (error == ENOSPC) {
3265 ztest_record_enospc(FTAG);
3268 if (error != 0 && error != EEXIST) {
3269 fatal(0, "ztest_snapshot_create(%s@%s) = %d", osname,
3276 ztest_snapshot_destroy(char *osname, uint64_t id)
3278 char snapname[MAXNAMELEN];
3281 (void) snprintf(snapname, MAXNAMELEN, "%s@%llu", osname,
3284 error = dsl_destroy_snapshot(snapname, B_FALSE);
3285 if (error != 0 && error != ENOENT)
3286 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
3292 ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
3302 zdtmp = umem_alloc(sizeof (ztest_ds_t), UMEM_NOFAIL);
3303 name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3305 (void) rw_enter(&ztest_name_lock, RW_READER);
3307 (void) snprintf(name, MAXNAMELEN, "%s/temp_%llu",
3308 ztest_opts.zo_pool, (u_longlong_t)id);
3311 * If this dataset exists from a previous run, process its replay log
3312 * half of the time. If we don't replay it, then dsl_destroy_head()
3313 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
3315 if (ztest_random(2) == 0 &&
3316 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) {
3317 ztest_zd_init(zdtmp, NULL, os);
3318 zil_replay(os, zdtmp, ztest_replay_vector);
3319 ztest_zd_fini(zdtmp);
3320 dmu_objset_disown(os, FTAG);
3324 * There may be an old instance of the dataset we're about to
3325 * create lying around from a previous run. If so, destroy it
3326 * and all of its snapshots.
3328 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
3329 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
3332 * Verify that the destroyed dataset is no longer in the namespace.
3334 VERIFY3U(ENOENT, ==, dmu_objset_own(name, DMU_OST_OTHER, B_TRUE,
3338 * Verify that we can create a new dataset.
3340 error = ztest_dataset_create(name);
3342 if (error == ENOSPC) {
3343 ztest_record_enospc(FTAG);
3346 fatal(0, "dmu_objset_create(%s) = %d", name, error);
3349 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os));
3351 ztest_zd_init(zdtmp, NULL, os);
3354 * Open the intent log for it.
3356 zilog = zil_open(os, ztest_get_data);
3359 * Put some objects in there, do a little I/O to them,
3360 * and randomly take a couple of snapshots along the way.
3362 iters = ztest_random(5);
3363 for (i = 0; i < iters; i++) {
3364 ztest_dmu_object_alloc_free(zdtmp, id);
3365 if (ztest_random(iters) == 0)
3366 (void) ztest_snapshot_create(name, i);
3370 * Verify that we cannot create an existing dataset.
3372 VERIFY3U(EEXIST, ==,
3373 dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL));
3376 * Verify that we can hold an objset that is also owned.
3378 VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
3379 dmu_objset_rele(os2, FTAG);
3382 * Verify that we cannot own an objset that is already owned.
3385 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2));
3388 dmu_objset_disown(os, FTAG);
3389 ztest_zd_fini(zdtmp);
3391 (void) rw_exit(&ztest_name_lock);
3393 umem_free(name, MAXNAMELEN);
3394 umem_free(zdtmp, sizeof (ztest_ds_t));
3398 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3401 ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
3403 (void) rw_enter(&ztest_name_lock, RW_READER);
3404 (void) ztest_snapshot_destroy(zd->zd_name, id);
3405 (void) ztest_snapshot_create(zd->zd_name, id);
3406 (void) rw_exit(&ztest_name_lock);
3410 * Cleanup non-standard snapshots and clones.
3413 ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
3422 snap1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3423 clone1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3424 snap2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3425 clone2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3426 snap3name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3428 (void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu",
3429 osname, (u_longlong_t)id);
3430 (void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu",
3431 osname, (u_longlong_t)id);
3432 (void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu",
3433 clone1name, (u_longlong_t)id);
3434 (void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu",
3435 osname, (u_longlong_t)id);
3436 (void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu",
3437 clone1name, (u_longlong_t)id);
3439 error = dsl_destroy_head(clone2name);
3440 if (error && error != ENOENT)
3441 fatal(0, "dsl_destroy_head(%s) = %d", clone2name, error);
3442 error = dsl_destroy_snapshot(snap3name, B_FALSE);
3443 if (error && error != ENOENT)
3444 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap3name, error);
3445 error = dsl_destroy_snapshot(snap2name, B_FALSE);
3446 if (error && error != ENOENT)
3447 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap2name, error);
3448 error = dsl_destroy_head(clone1name);
3449 if (error && error != ENOENT)
3450 fatal(0, "dsl_destroy_head(%s) = %d", clone1name, error);
3451 error = dsl_destroy_snapshot(snap1name, B_FALSE);
3452 if (error && error != ENOENT)
3453 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap1name, error);
3455 umem_free(snap1name, MAXNAMELEN);
3456 umem_free(clone1name, MAXNAMELEN);
3457 umem_free(snap2name, MAXNAMELEN);
3458 umem_free(clone2name, MAXNAMELEN);
3459 umem_free(snap3name, MAXNAMELEN);
3463 * Verify dsl_dataset_promote handles EBUSY
3466 ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
3474 char *osname = zd->zd_name;
3477 snap1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3478 clone1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3479 snap2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3480 clone2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3481 snap3name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3483 (void) rw_enter(&ztest_name_lock, RW_READER);
3485 ztest_dsl_dataset_cleanup(osname, id);
3487 (void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu",
3488 osname, (u_longlong_t)id);
3489 (void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu",
3490 osname, (u_longlong_t)id);
3491 (void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu",
3492 clone1name, (u_longlong_t)id);
3493 (void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu",
3494 osname, (u_longlong_t)id);
3495 (void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu",
3496 clone1name, (u_longlong_t)id);
3498 error = dmu_objset_snapshot_one(osname, strchr(snap1name, '@') + 1);
3499 if (error && error != EEXIST) {
3500 if (error == ENOSPC) {
3501 ztest_record_enospc(FTAG);
3504 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
3507 error = dmu_objset_clone(clone1name, snap1name);
3509 if (error == ENOSPC) {
3510 ztest_record_enospc(FTAG);
3513 fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
3516 error = dmu_objset_snapshot_one(clone1name, strchr(snap2name, '@') + 1);
3517 if (error && error != EEXIST) {
3518 if (error == ENOSPC) {
3519 ztest_record_enospc(FTAG);
3522 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
3525 error = dmu_objset_snapshot_one(clone1name, strchr(snap3name, '@') + 1);
3526 if (error && error != EEXIST) {
3527 if (error == ENOSPC) {
3528 ztest_record_enospc(FTAG);
3531 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
3534 error = dmu_objset_clone(clone2name, snap3name);
3536 if (error == ENOSPC) {
3537 ztest_record_enospc(FTAG);
3540 fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
3543 error = dmu_objset_own(snap2name, DMU_OST_ANY, B_TRUE, FTAG, &os);
3545 fatal(0, "dmu_objset_own(%s) = %d", snap2name, error);
3546 error = dsl_dataset_promote(clone2name, NULL);
3548 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
3550 dmu_objset_disown(os, FTAG);
3553 ztest_dsl_dataset_cleanup(osname, id);
3555 (void) rw_exit(&ztest_name_lock);
3557 umem_free(snap1name, MAXNAMELEN);
3558 umem_free(clone1name, MAXNAMELEN);
3559 umem_free(snap2name, MAXNAMELEN);
3560 umem_free(clone2name, MAXNAMELEN);
3561 umem_free(snap3name, MAXNAMELEN);
3564 #undef OD_ARRAY_SIZE
3565 #define OD_ARRAY_SIZE 4
3568 * Verify that dmu_object_{alloc,free} work as expected.
3571 ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
3578 size = sizeof(ztest_od_t) * OD_ARRAY_SIZE;
3579 od = umem_alloc(size, UMEM_NOFAIL);
3580 batchsize = OD_ARRAY_SIZE;
3582 for (b = 0; b < batchsize; b++)
3583 ztest_od_init(od + b, id, FTAG, b, DMU_OT_UINT64_OTHER, 0, 0);
3586 * Destroy the previous batch of objects, create a new batch,
3587 * and do some I/O on the new objects.
3589 if (ztest_object_init(zd, od, size, B_TRUE) != 0)
3592 while (ztest_random(4 * batchsize) != 0)
3593 ztest_io(zd, od[ztest_random(batchsize)].od_object,
3594 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3596 umem_free(od, size);
3599 #undef OD_ARRAY_SIZE
3600 #define OD_ARRAY_SIZE 2
3603 * Verify that dmu_{read,write} work as expected.
3606 ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
3611 objset_t *os = zd->zd_os;
3612 size = sizeof(ztest_od_t) * OD_ARRAY_SIZE;
3613 od = umem_alloc(size, UMEM_NOFAIL);
3615 int i, freeit, error;
3617 bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
3618 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3619 uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
3620 uint64_t regions = 997;
3621 uint64_t stride = 123456789ULL;
3622 uint64_t width = 40;
3623 int free_percent = 5;
3626 * This test uses two objects, packobj and bigobj, that are always
3627 * updated together (i.e. in the same tx) so that their contents are
3628 * in sync and can be compared. Their contents relate to each other
3629 * in a simple way: packobj is a dense array of 'bufwad' structures,
3630 * while bigobj is a sparse array of the same bufwads. Specifically,
3631 * for any index n, there are three bufwads that should be identical:
3633 * packobj, at offset n * sizeof (bufwad_t)
3634 * bigobj, at the head of the nth chunk
3635 * bigobj, at the tail of the nth chunk
3637 * The chunk size is arbitrary. It doesn't have to be a power of two,
3638 * and it doesn't have any relation to the object blocksize.
3639 * The only requirement is that it can hold at least two bufwads.
3641 * Normally, we write the bufwad to each of these locations.
3642 * However, free_percent of the time we instead write zeroes to
3643 * packobj and perform a dmu_free_range() on bigobj. By comparing
3644 * bigobj to packobj, we can verify that the DMU is correctly
3645 * tracking which parts of an object are allocated and free,
3646 * and that the contents of the allocated blocks are correct.
3650 * Read the directory info. If it's the first time, set things up.
3652 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, chunksize);
3653 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
3655 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
3656 umem_free(od, size);
3660 bigobj = od[0].od_object;
3661 packobj = od[1].od_object;
3662 chunksize = od[0].od_gen;
3663 ASSERT(chunksize == od[1].od_gen);
3666 * Prefetch a random chunk of the big object.
3667 * Our aim here is to get some async reads in flight
3668 * for blocks that we may free below; the DMU should
3669 * handle this race correctly.
3671 n = ztest_random(regions) * stride + ztest_random(width);
3672 s = 1 + ztest_random(2 * width - 1);
3673 dmu_prefetch(os, bigobj, n * chunksize, s * chunksize);
3676 * Pick a random index and compute the offsets into packobj and bigobj.
3678 n = ztest_random(regions) * stride + ztest_random(width);
3679 s = 1 + ztest_random(width - 1);
3681 packoff = n * sizeof (bufwad_t);
3682 packsize = s * sizeof (bufwad_t);
3684 bigoff = n * chunksize;
3685 bigsize = s * chunksize;
3687 packbuf = umem_alloc(packsize, UMEM_NOFAIL);
3688 bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
3691 * free_percent of the time, free a range of bigobj rather than
3694 freeit = (ztest_random(100) < free_percent);
3697 * Read the current contents of our objects.
3699 error = dmu_read(os, packobj, packoff, packsize, packbuf,
3702 error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
3707 * Get a tx for the mods to both packobj and bigobj.
3709 tx = dmu_tx_create(os);
3711 dmu_tx_hold_write(tx, packobj, packoff, packsize);
3714 dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
3716 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
3718 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3720 umem_free(packbuf, packsize);
3721 umem_free(bigbuf, bigsize);
3722 umem_free(od, size);
3726 dmu_object_set_checksum(os, bigobj,
3727 (enum zio_checksum)ztest_random_dsl_prop(ZFS_PROP_CHECKSUM), tx);
3729 dmu_object_set_compress(os, bigobj,
3730 (enum zio_compress)ztest_random_dsl_prop(ZFS_PROP_COMPRESSION), tx);
3733 * For each index from n to n + s, verify that the existing bufwad
3734 * in packobj matches the bufwads at the head and tail of the
3735 * corresponding chunk in bigobj. Then update all three bufwads
3736 * with the new values we want to write out.
3738 for (i = 0; i < s; i++) {
3740 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3742 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
3744 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
3746 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3747 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3749 if (pack->bw_txg > txg)
3750 fatal(0, "future leak: got %llx, open txg is %llx",
3753 if (pack->bw_data != 0 && pack->bw_index != n + i)
3754 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3755 pack->bw_index, n, i);
3757 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3758 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
3760 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
3761 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
3764 bzero(pack, sizeof (bufwad_t));
3766 pack->bw_index = n + i;
3768 pack->bw_data = 1 + ztest_random(-2ULL);
3775 * We've verified all the old bufwads, and made new ones.
3776 * Now write them out.
3778 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
3781 if (ztest_opts.zo_verbose >= 7) {
3782 (void) printf("freeing offset %llx size %llx"
3784 (u_longlong_t)bigoff,
3785 (u_longlong_t)bigsize,
3788 VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
3790 if (ztest_opts.zo_verbose >= 7) {
3791 (void) printf("writing offset %llx size %llx"
3793 (u_longlong_t)bigoff,
3794 (u_longlong_t)bigsize,
3797 dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
3803 * Sanity check the stuff we just wrote.
3806 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
3807 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
3809 VERIFY(0 == dmu_read(os, packobj, packoff,
3810 packsize, packcheck, DMU_READ_PREFETCH));
3811 VERIFY(0 == dmu_read(os, bigobj, bigoff,
3812 bigsize, bigcheck, DMU_READ_PREFETCH));
3814 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
3815 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
3817 umem_free(packcheck, packsize);
3818 umem_free(bigcheck, bigsize);
3821 umem_free(packbuf, packsize);
3822 umem_free(bigbuf, bigsize);
3823 umem_free(od, size);
3827 compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
3828 uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
3836 * For each index from n to n + s, verify that the existing bufwad
3837 * in packobj matches the bufwads at the head and tail of the
3838 * corresponding chunk in bigobj. Then update all three bufwads
3839 * with the new values we want to write out.
3841 for (i = 0; i < s; i++) {
3843 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3845 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
3847 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
3849 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3850 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3852 if (pack->bw_txg > txg)
3853 fatal(0, "future leak: got %llx, open txg is %llx",
3856 if (pack->bw_data != 0 && pack->bw_index != n + i)
3857 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3858 pack->bw_index, n, i);
3860 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3861 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
3863 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
3864 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
3866 pack->bw_index = n + i;
3868 pack->bw_data = 1 + ztest_random(-2ULL);
3875 #undef OD_ARRAY_SIZE
3876 #define OD_ARRAY_SIZE 2
3879 ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
3881 objset_t *os = zd->zd_os;
3888 bufwad_t *packbuf, *bigbuf;
3889 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3890 uint64_t blocksize = ztest_random_blocksize();
3891 uint64_t chunksize = blocksize;
3892 uint64_t regions = 997;
3893 uint64_t stride = 123456789ULL;
3895 dmu_buf_t *bonus_db;
3896 arc_buf_t **bigbuf_arcbufs;
3897 dmu_object_info_t doi;
3899 size = sizeof(ztest_od_t) * OD_ARRAY_SIZE;
3900 od = umem_alloc(size, UMEM_NOFAIL);
3903 * This test uses two objects, packobj and bigobj, that are always
3904 * updated together (i.e. in the same tx) so that their contents are
3905 * in sync and can be compared. Their contents relate to each other
3906 * in a simple way: packobj is a dense array of 'bufwad' structures,
3907 * while bigobj is a sparse array of the same bufwads. Specifically,
3908 * for any index n, there are three bufwads that should be identical:
3910 * packobj, at offset n * sizeof (bufwad_t)
3911 * bigobj, at the head of the nth chunk
3912 * bigobj, at the tail of the nth chunk
3914 * The chunk size is set equal to bigobj block size so that
3915 * dmu_assign_arcbuf() can be tested for object updates.
3919 * Read the directory info. If it's the first time, set things up.
3921 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
3922 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
3925 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
3926 umem_free(od, size);
3930 bigobj = od[0].od_object;
3931 packobj = od[1].od_object;
3932 blocksize = od[0].od_blocksize;
3933 chunksize = blocksize;
3934 ASSERT(chunksize == od[1].od_gen);
3936 VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
3937 VERIFY(ISP2(doi.doi_data_block_size));
3938 VERIFY(chunksize == doi.doi_data_block_size);
3939 VERIFY(chunksize >= 2 * sizeof (bufwad_t));
3942 * Pick a random index and compute the offsets into packobj and bigobj.
3944 n = ztest_random(regions) * stride + ztest_random(width);
3945 s = 1 + ztest_random(width - 1);
3947 packoff = n * sizeof (bufwad_t);
3948 packsize = s * sizeof (bufwad_t);
3950 bigoff = n * chunksize;
3951 bigsize = s * chunksize;
3953 packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
3954 bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
3956 VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
3958 bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
3961 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
3962 * Iteration 1 test zcopy to already referenced dbufs.
3963 * Iteration 2 test zcopy to dirty dbuf in the same txg.
3964 * Iteration 3 test zcopy to dbuf dirty in previous txg.
3965 * Iteration 4 test zcopy when dbuf is no longer dirty.
3966 * Iteration 5 test zcopy when it can't be done.
3967 * Iteration 6 one more zcopy write.
3969 for (i = 0; i < 7; i++) {
3974 * In iteration 5 (i == 5) use arcbufs
3975 * that don't match bigobj blksz to test
3976 * dmu_assign_arcbuf() when it can't directly
3977 * assign an arcbuf to a dbuf.
3979 for (j = 0; j < s; j++) {
3982 dmu_request_arcbuf(bonus_db, chunksize);
3984 bigbuf_arcbufs[2 * j] =
3985 dmu_request_arcbuf(bonus_db, chunksize / 2);
3986 bigbuf_arcbufs[2 * j + 1] =
3987 dmu_request_arcbuf(bonus_db, chunksize / 2);
3992 * Get a tx for the mods to both packobj and bigobj.
3994 tx = dmu_tx_create(os);
3996 dmu_tx_hold_write(tx, packobj, packoff, packsize);
3997 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
3999 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4001 umem_free(packbuf, packsize);
4002 umem_free(bigbuf, bigsize);
4003 for (j = 0; j < s; j++) {
4005 dmu_return_arcbuf(bigbuf_arcbufs[j]);
4008 bigbuf_arcbufs[2 * j]);
4010 bigbuf_arcbufs[2 * j + 1]);
4013 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4014 umem_free(od, size);
4015 dmu_buf_rele(bonus_db, FTAG);
4020 * 50% of the time don't read objects in the 1st iteration to
4021 * test dmu_assign_arcbuf() for the case when there're no
4022 * existing dbufs for the specified offsets.
4024 if (i != 0 || ztest_random(2) != 0) {
4025 error = dmu_read(os, packobj, packoff,
4026 packsize, packbuf, DMU_READ_PREFETCH);
4028 error = dmu_read(os, bigobj, bigoff, bigsize,
4029 bigbuf, DMU_READ_PREFETCH);
4032 compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
4036 * We've verified all the old bufwads, and made new ones.
4037 * Now write them out.
4039 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
4040 if (ztest_opts.zo_verbose >= 7) {
4041 (void) printf("writing offset %llx size %llx"
4043 (u_longlong_t)bigoff,
4044 (u_longlong_t)bigsize,
4047 for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
4050 bcopy((caddr_t)bigbuf + (off - bigoff),
4051 bigbuf_arcbufs[j]->b_data, chunksize);
4053 bcopy((caddr_t)bigbuf + (off - bigoff),
4054 bigbuf_arcbufs[2 * j]->b_data,
4056 bcopy((caddr_t)bigbuf + (off - bigoff) +
4058 bigbuf_arcbufs[2 * j + 1]->b_data,
4063 VERIFY(dmu_buf_hold(os, bigobj, off,
4064 FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0);
4067 dmu_assign_arcbuf(bonus_db, off,
4068 bigbuf_arcbufs[j], tx);
4070 dmu_assign_arcbuf(bonus_db, off,
4071 bigbuf_arcbufs[2 * j], tx);
4072 dmu_assign_arcbuf(bonus_db,
4073 off + chunksize / 2,
4074 bigbuf_arcbufs[2 * j + 1], tx);
4077 dmu_buf_rele(dbt, FTAG);
4083 * Sanity check the stuff we just wrote.
4086 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
4087 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
4089 VERIFY(0 == dmu_read(os, packobj, packoff,
4090 packsize, packcheck, DMU_READ_PREFETCH));
4091 VERIFY(0 == dmu_read(os, bigobj, bigoff,
4092 bigsize, bigcheck, DMU_READ_PREFETCH));
4094 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
4095 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
4097 umem_free(packcheck, packsize);
4098 umem_free(bigcheck, bigsize);
4101 txg_wait_open(dmu_objset_pool(os), 0);
4102 } else if (i == 3) {
4103 txg_wait_synced(dmu_objset_pool(os), 0);
4107 dmu_buf_rele(bonus_db, FTAG);
4108 umem_free(packbuf, packsize);
4109 umem_free(bigbuf, bigsize);
4110 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4111 umem_free(od, size);
4116 ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
4120 od = umem_alloc(sizeof(ztest_od_t), UMEM_NOFAIL);
4121 uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
4122 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4125 * Have multiple threads write to large offsets in an object
4126 * to verify that parallel writes to an object -- even to the
4127 * same blocks within the object -- doesn't cause any trouble.
4129 ztest_od_init(od, ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
4131 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0)
4134 while (ztest_random(10) != 0)
4135 ztest_io(zd, od->od_object, offset);
4137 umem_free(od, sizeof(ztest_od_t));
4141 ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
4144 uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
4145 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4146 uint64_t count = ztest_random(20) + 1;
4147 uint64_t blocksize = ztest_random_blocksize();
4150 od = umem_alloc(sizeof(ztest_od_t), UMEM_NOFAIL);
4152 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
4154 if (ztest_object_init(zd, od, sizeof (ztest_od_t), !ztest_random(2)) != 0) {
4155 umem_free(od, sizeof(ztest_od_t));
4159 if (ztest_truncate(zd, od->od_object, offset, count * blocksize) != 0) {
4160 umem_free(od, sizeof(ztest_od_t));
4164 ztest_prealloc(zd, od->od_object, offset, count * blocksize);
4166 data = umem_zalloc(blocksize, UMEM_NOFAIL);
4168 while (ztest_random(count) != 0) {
4169 uint64_t randoff = offset + (ztest_random(count) * blocksize);
4170 if (ztest_write(zd, od->od_object, randoff, blocksize,
4173 while (ztest_random(4) != 0)
4174 ztest_io(zd, od->od_object, randoff);
4177 umem_free(data, blocksize);
4178 umem_free(od, sizeof(ztest_od_t));
4182 * Verify that zap_{create,destroy,add,remove,update} work as expected.
4184 #define ZTEST_ZAP_MIN_INTS 1
4185 #define ZTEST_ZAP_MAX_INTS 4
4186 #define ZTEST_ZAP_MAX_PROPS 1000
4189 ztest_zap(ztest_ds_t *zd, uint64_t id)
4191 objset_t *os = zd->zd_os;
4194 uint64_t txg, last_txg;
4195 uint64_t value[ZTEST_ZAP_MAX_INTS];
4196 uint64_t zl_ints, zl_intsize, prop;
4199 char propname[100], txgname[100];
4201 char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
4203 od = umem_alloc(sizeof(ztest_od_t), UMEM_NOFAIL);
4204 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
4206 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4207 !ztest_random(2)) != 0)
4210 object = od->od_object;
4213 * Generate a known hash collision, and verify that
4214 * we can lookup and remove both entries.
4216 tx = dmu_tx_create(os);
4217 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4218 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4221 for (i = 0; i < 2; i++) {
4223 VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
4226 for (i = 0; i < 2; i++) {
4227 VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
4228 sizeof (uint64_t), 1, &value[i], tx));
4230 zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
4231 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4232 ASSERT3U(zl_ints, ==, 1);
4234 for (i = 0; i < 2; i++) {
4235 VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
4240 * Generate a buch of random entries.
4242 ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
4244 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4245 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4246 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4247 bzero(value, sizeof (value));
4251 * If these zap entries already exist, validate their contents.
4253 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4255 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4256 ASSERT3U(zl_ints, ==, 1);
4258 VERIFY(zap_lookup(os, object, txgname, zl_intsize,
4259 zl_ints, &last_txg) == 0);
4261 VERIFY(zap_length(os, object, propname, &zl_intsize,
4264 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4265 ASSERT3U(zl_ints, ==, ints);
4267 VERIFY(zap_lookup(os, object, propname, zl_intsize,
4268 zl_ints, value) == 0);
4270 for (i = 0; i < ints; i++) {
4271 ASSERT3U(value[i], ==, last_txg + object + i);
4274 ASSERT3U(error, ==, ENOENT);
4278 * Atomically update two entries in our zap object.
4279 * The first is named txg_%llu, and contains the txg
4280 * in which the property was last updated. The second
4281 * is named prop_%llu, and the nth element of its value
4282 * should be txg + object + n.
4284 tx = dmu_tx_create(os);
4285 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4286 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4291 fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
4293 for (i = 0; i < ints; i++)
4294 value[i] = txg + object + i;
4296 VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
4298 VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
4304 * Remove a random pair of entries.
4306 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4307 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4308 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4310 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4312 if (error == ENOENT)
4317 tx = dmu_tx_create(os);
4318 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4319 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4322 VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
4323 VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
4326 umem_free(od, sizeof(ztest_od_t));
4330 * Testcase to test the upgrading of a microzap to fatzap.
4333 ztest_fzap(ztest_ds_t *zd, uint64_t id)
4335 objset_t *os = zd->zd_os;
4337 uint64_t object, txg;
4340 od = umem_alloc(sizeof(ztest_od_t), UMEM_NOFAIL);
4341 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
4343 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4344 !ztest_random(2)) != 0)
4346 object = od->od_object;
4349 * Add entries to this ZAP and make sure it spills over
4350 * and gets upgraded to a fatzap. Also, since we are adding
4351 * 2050 entries we should see ptrtbl growth and leaf-block split.
4353 for (i = 0; i < 2050; i++) {
4354 char name[MAXNAMELEN];
4359 (void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
4360 (u_longlong_t)id, (u_longlong_t)value);
4362 tx = dmu_tx_create(os);
4363 dmu_tx_hold_zap(tx, object, B_TRUE, name);
4364 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4367 error = zap_add(os, object, name, sizeof (uint64_t), 1,
4369 ASSERT(error == 0 || error == EEXIST);
4373 umem_free(od, sizeof(ztest_od_t));
4378 ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
4380 objset_t *os = zd->zd_os;
4382 uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
4384 int i, namelen, error;
4385 int micro = ztest_random(2);
4386 char name[20], string_value[20];
4389 od = umem_alloc(sizeof(ztest_od_t), UMEM_NOFAIL);
4390 ztest_od_init(od, ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER, 0, 0);
4392 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
4393 umem_free(od, sizeof(ztest_od_t));
4397 object = od->od_object;
4400 * Generate a random name of the form 'xxx.....' where each
4401 * x is a random printable character and the dots are dots.
4402 * There are 94 such characters, and the name length goes from
4403 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
4405 namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
4407 for (i = 0; i < 3; i++)
4408 name[i] = '!' + ztest_random('~' - '!' + 1);
4409 for (; i < namelen - 1; i++)
4413 if ((namelen & 1) || micro) {
4414 wsize = sizeof (txg);
4420 data = string_value;
4424 VERIFY0(zap_count(os, object, &count));
4425 ASSERT(count != -1ULL);
4428 * Select an operation: length, lookup, add, update, remove.
4430 i = ztest_random(5);
4433 tx = dmu_tx_create(os);
4434 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4435 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4438 bcopy(name, string_value, namelen);
4442 bzero(string_value, namelen);
4448 error = zap_length(os, object, name, &zl_wsize, &zl_wc);
4450 ASSERT3U(wsize, ==, zl_wsize);
4451 ASSERT3U(wc, ==, zl_wc);
4453 ASSERT3U(error, ==, ENOENT);
4458 error = zap_lookup(os, object, name, wsize, wc, data);
4460 if (data == string_value &&
4461 bcmp(name, data, namelen) != 0)
4462 fatal(0, "name '%s' != val '%s' len %d",
4463 name, data, namelen);
4465 ASSERT3U(error, ==, ENOENT);
4470 error = zap_add(os, object, name, wsize, wc, data, tx);
4471 ASSERT(error == 0 || error == EEXIST);
4475 VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
4479 error = zap_remove(os, object, name, tx);
4480 ASSERT(error == 0 || error == ENOENT);
4487 umem_free(od, sizeof(ztest_od_t));
4491 * Commit callback data.
4493 typedef struct ztest_cb_data {
4494 list_node_t zcd_node;
4496 int zcd_expected_err;
4497 boolean_t zcd_added;
4498 boolean_t zcd_called;
4502 /* This is the actual commit callback function */
4504 ztest_commit_callback(void *arg, int error)
4506 ztest_cb_data_t *data = arg;
4507 uint64_t synced_txg;
4509 VERIFY(data != NULL);
4510 VERIFY3S(data->zcd_expected_err, ==, error);
4511 VERIFY(!data->zcd_called);
4513 synced_txg = spa_last_synced_txg(data->zcd_spa);
4514 if (data->zcd_txg > synced_txg)
4515 fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
4516 ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
4519 data->zcd_called = B_TRUE;
4521 if (error == ECANCELED) {
4522 ASSERT0(data->zcd_txg);
4523 ASSERT(!data->zcd_added);
4526 * The private callback data should be destroyed here, but
4527 * since we are going to check the zcd_called field after
4528 * dmu_tx_abort(), we will destroy it there.
4533 ASSERT(data->zcd_added);
4534 ASSERT3U(data->zcd_txg, !=, 0);
4536 (void) mutex_enter(&zcl.zcl_callbacks_lock);
4538 /* See if this cb was called more quickly */
4539 if ((synced_txg - data->zcd_txg) < zc_min_txg_delay)
4540 zc_min_txg_delay = synced_txg - data->zcd_txg;
4542 /* Remove our callback from the list */
4543 list_remove(&zcl.zcl_callbacks, data);
4545 (void) mutex_exit(&zcl.zcl_callbacks_lock);
4547 umem_free(data, sizeof (ztest_cb_data_t));
4550 /* Allocate and initialize callback data structure */
4551 static ztest_cb_data_t *
4552 ztest_create_cb_data(objset_t *os, uint64_t txg)
4554 ztest_cb_data_t *cb_data;
4556 cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
4558 cb_data->zcd_txg = txg;
4559 cb_data->zcd_spa = dmu_objset_spa(os);
4560 list_link_init(&cb_data->zcd_node);
4566 * Commit callback test.
4569 ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
4571 objset_t *os = zd->zd_os;
4574 ztest_cb_data_t *cb_data[3], *tmp_cb;
4575 uint64_t old_txg, txg;
4578 od = umem_alloc(sizeof(ztest_od_t), UMEM_NOFAIL);
4579 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
4581 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
4582 umem_free(od, sizeof(ztest_od_t));
4586 tx = dmu_tx_create(os);
4588 cb_data[0] = ztest_create_cb_data(os, 0);
4589 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
4591 dmu_tx_hold_write(tx, od->od_object, 0, sizeof (uint64_t));
4593 /* Every once in a while, abort the transaction on purpose */
4594 if (ztest_random(100) == 0)
4598 error = dmu_tx_assign(tx, TXG_NOWAIT);
4600 txg = error ? 0 : dmu_tx_get_txg(tx);
4602 cb_data[0]->zcd_txg = txg;
4603 cb_data[1] = ztest_create_cb_data(os, txg);
4604 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
4608 * It's not a strict requirement to call the registered
4609 * callbacks from inside dmu_tx_abort(), but that's what
4610 * it's supposed to happen in the current implementation
4611 * so we will check for that.
4613 for (i = 0; i < 2; i++) {
4614 cb_data[i]->zcd_expected_err = ECANCELED;
4615 VERIFY(!cb_data[i]->zcd_called);
4620 for (i = 0; i < 2; i++) {
4621 VERIFY(cb_data[i]->zcd_called);
4622 umem_free(cb_data[i], sizeof (ztest_cb_data_t));
4625 umem_free(od, sizeof(ztest_od_t));
4629 cb_data[2] = ztest_create_cb_data(os, txg);
4630 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
4633 * Read existing data to make sure there isn't a future leak.
4635 VERIFY(0 == dmu_read(os, od->od_object, 0, sizeof (uint64_t),
4636 &old_txg, DMU_READ_PREFETCH));
4639 fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
4642 dmu_write(os, od->od_object, 0, sizeof (uint64_t), &txg, tx);
4644 (void) mutex_enter(&zcl.zcl_callbacks_lock);
4647 * Since commit callbacks don't have any ordering requirement and since
4648 * it is theoretically possible for a commit callback to be called
4649 * after an arbitrary amount of time has elapsed since its txg has been
4650 * synced, it is difficult to reliably determine whether a commit
4651 * callback hasn't been called due to high load or due to a flawed
4654 * In practice, we will assume that if after a certain number of txgs a
4655 * commit callback hasn't been called, then most likely there's an
4656 * implementation bug..
4658 tmp_cb = list_head(&zcl.zcl_callbacks);
4659 if (tmp_cb != NULL &&
4660 tmp_cb->zcd_txg + ZTEST_COMMIT_CB_THRESH < txg) {
4661 fatal(0, "Commit callback threshold exceeded, oldest txg: %"
4662 PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
4666 * Let's find the place to insert our callbacks.
4668 * Even though the list is ordered by txg, it is possible for the
4669 * insertion point to not be the end because our txg may already be
4670 * quiescing at this point and other callbacks in the open txg
4671 * (from other objsets) may have sneaked in.
4673 tmp_cb = list_tail(&zcl.zcl_callbacks);
4674 while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
4675 tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
4677 /* Add the 3 callbacks to the list */
4678 for (i = 0; i < 3; i++) {
4680 list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
4682 list_insert_after(&zcl.zcl_callbacks, tmp_cb,
4685 cb_data[i]->zcd_added = B_TRUE;
4686 VERIFY(!cb_data[i]->zcd_called);
4688 tmp_cb = cb_data[i];
4693 (void) mutex_exit(&zcl.zcl_callbacks_lock);
4697 umem_free(od, sizeof(ztest_od_t));
4702 ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
4704 zfs_prop_t proplist[] = {
4706 ZFS_PROP_COMPRESSION,
4712 (void) rw_enter(&ztest_name_lock, RW_READER);
4714 for (p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
4715 (void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
4716 ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
4718 (void) rw_exit(&ztest_name_lock);
4723 ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
4725 nvlist_t *props = NULL;
4727 (void) rw_enter(&ztest_name_lock, RW_READER);
4729 (void) ztest_spa_prop_set_uint64(ZPOOL_PROP_DEDUPDITTO,
4730 ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
4732 VERIFY0(spa_prop_get(ztest_spa, &props));
4734 if (ztest_opts.zo_verbose >= 6)
4735 dump_nvlist(props, 4);
4739 (void) rw_exit(&ztest_name_lock);
4743 user_release_one(const char *snapname, const char *holdname)
4745 nvlist_t *snaps, *holds;
4748 snaps = fnvlist_alloc();
4749 holds = fnvlist_alloc();
4750 fnvlist_add_boolean(holds, holdname);
4751 fnvlist_add_nvlist(snaps, snapname, holds);
4752 fnvlist_free(holds);
4753 error = dsl_dataset_user_release(snaps, NULL);
4754 fnvlist_free(snaps);
4759 * Test snapshot hold/release and deferred destroy.
4762 ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
4765 objset_t *os = zd->zd_os;
4769 char clonename[100];
4771 char osname[MAXNAMELEN];
4774 (void) rw_enter(&ztest_name_lock, RW_READER);
4776 dmu_objset_name(os, osname);
4778 (void) snprintf(snapname, sizeof (snapname), "sh1_%llu", (long long unsigned int)id);
4779 (void) snprintf(fullname, sizeof (fullname), "%s@%s", osname, snapname);
4780 (void) snprintf(clonename, sizeof (clonename),
4781 "%s/ch1_%llu", osname, (long long unsigned int)id);
4782 (void) snprintf(tag, sizeof (tag), "tag_%llu", (long long unsigned int)id);
4785 * Clean up from any previous run.
4787 error = dsl_destroy_head(clonename);
4788 if (error != ENOENT)
4790 error = user_release_one(fullname, tag);
4791 if (error != ESRCH && error != ENOENT)
4793 error = dsl_destroy_snapshot(fullname, B_FALSE);
4794 if (error != ENOENT)
4798 * Create snapshot, clone it, mark snap for deferred destroy,
4799 * destroy clone, verify snap was also destroyed.
4801 error = dmu_objset_snapshot_one(osname, snapname);
4803 if (error == ENOSPC) {
4804 ztest_record_enospc("dmu_objset_snapshot");
4807 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
4810 error = dmu_objset_clone(clonename, fullname);
4812 if (error == ENOSPC) {
4813 ztest_record_enospc("dmu_objset_clone");
4816 fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
4819 error = dsl_destroy_snapshot(fullname, B_TRUE);
4821 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
4825 error = dsl_destroy_head(clonename);
4827 fatal(0, "dsl_destroy_head(%s) = %d", clonename, error);
4829 error = dmu_objset_hold(fullname, FTAG, &origin);
4830 if (error != ENOENT)
4831 fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
4834 * Create snapshot, add temporary hold, verify that we can't
4835 * destroy a held snapshot, mark for deferred destroy,
4836 * release hold, verify snapshot was destroyed.
4838 error = dmu_objset_snapshot_one(osname, snapname);
4840 if (error == ENOSPC) {
4841 ztest_record_enospc("dmu_objset_snapshot");
4844 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
4847 holds = fnvlist_alloc();
4848 fnvlist_add_string(holds, fullname, tag);
4849 error = dsl_dataset_user_hold(holds, 0, NULL);
4850 fnvlist_free(holds);
4853 fatal(0, "dsl_dataset_user_hold(%s)", fullname, tag);
4855 error = dsl_destroy_snapshot(fullname, B_FALSE);
4856 if (error != EBUSY) {
4857 fatal(0, "dsl_destroy_snapshot(%s, B_FALSE) = %d",
4861 error = dsl_destroy_snapshot(fullname, B_TRUE);
4863 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
4867 error = user_release_one(fullname, tag);
4869 fatal(0, "user_release_one(%s, %s) = %d", fullname, tag, error);
4871 VERIFY3U(dmu_objset_hold(fullname, FTAG, &origin), ==, ENOENT);
4874 (void) rw_exit(&ztest_name_lock);
4878 * Inject random faults into the on-disk data.
4882 ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
4884 ztest_shared_t *zs = ztest_shared;
4885 spa_t *spa = ztest_spa;
4889 uint64_t bad = 0x1990c0ffeedecadeull;
4894 int bshift = SPA_MAXBLOCKSHIFT + 2; /* don't scrog all labels */
4900 boolean_t islog = B_FALSE;
4902 path0 = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
4903 pathrand = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
4905 mutex_enter(&ztest_vdev_lock);
4906 maxfaults = MAXFAULTS();
4907 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
4908 mirror_save = zs->zs_mirrors;
4909 mutex_exit(&ztest_vdev_lock);
4911 ASSERT(leaves >= 1);
4914 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
4916 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
4918 if (ztest_random(2) == 0) {
4920 * Inject errors on a normal data device or slog device.
4922 top = ztest_random_vdev_top(spa, B_TRUE);
4923 leaf = ztest_random(leaves) + zs->zs_splits;
4926 * Generate paths to the first leaf in this top-level vdev,
4927 * and to the random leaf we selected. We'll induce transient
4928 * write failures and random online/offline activity on leaf 0,
4929 * and we'll write random garbage to the randomly chosen leaf.
4931 (void) snprintf(path0, MAXPATHLEN, ztest_dev_template,
4932 ztest_opts.zo_dir, ztest_opts.zo_pool,
4933 top * leaves + zs->zs_splits);
4934 (void) snprintf(pathrand, MAXPATHLEN, ztest_dev_template,
4935 ztest_opts.zo_dir, ztest_opts.zo_pool,
4936 top * leaves + leaf);
4938 vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
4939 if (vd0 != NULL && vd0->vdev_top->vdev_islog)
4942 if (vd0 != NULL && maxfaults != 1) {
4944 * Make vd0 explicitly claim to be unreadable,
4945 * or unwriteable, or reach behind its back
4946 * and close the underlying fd. We can do this if
4947 * maxfaults == 0 because we'll fail and reexecute,
4948 * and we can do it if maxfaults >= 2 because we'll
4949 * have enough redundancy. If maxfaults == 1, the
4950 * combination of this with injection of random data
4951 * corruption below exceeds the pool's fault tolerance.
4953 vdev_file_t *vf = vd0->vdev_tsd;
4955 if (vf != NULL && ztest_random(3) == 0) {
4956 (void) close(vf->vf_vnode->v_fd);
4957 vf->vf_vnode->v_fd = -1;
4958 } else if (ztest_random(2) == 0) {
4959 vd0->vdev_cant_read = B_TRUE;
4961 vd0->vdev_cant_write = B_TRUE;
4963 guid0 = vd0->vdev_guid;
4967 * Inject errors on an l2cache device.
4969 spa_aux_vdev_t *sav = &spa->spa_l2cache;
4971 if (sav->sav_count == 0) {
4972 spa_config_exit(spa, SCL_STATE, FTAG);
4975 vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
4976 guid0 = vd0->vdev_guid;
4977 (void) strcpy(path0, vd0->vdev_path);
4978 (void) strcpy(pathrand, vd0->vdev_path);
4982 maxfaults = INT_MAX; /* no limit on cache devices */
4985 spa_config_exit(spa, SCL_STATE, FTAG);
4988 * If we can tolerate two or more faults, or we're dealing
4989 * with a slog, randomly online/offline vd0.
4991 if ((maxfaults >= 2 || islog) && guid0 != 0) {
4992 if (ztest_random(10) < 6) {
4993 int flags = (ztest_random(2) == 0 ?
4994 ZFS_OFFLINE_TEMPORARY : 0);
4997 * We have to grab the zs_name_lock as writer to
4998 * prevent a race between offlining a slog and
4999 * destroying a dataset. Offlining the slog will
5000 * grab a reference on the dataset which may cause
5001 * dsl_destroy_head() to fail with EBUSY thus
5002 * leaving the dataset in an inconsistent state.
5005 (void) rw_enter(&ztest_name_lock,
5008 VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
5011 (void) rw_exit(&ztest_name_lock);
5014 * Ideally we would like to be able to randomly
5015 * call vdev_[on|off]line without holding locks
5016 * to force unpredictable failures but the side
5017 * effects of vdev_[on|off]line prevent us from
5018 * doing so. We grab the ztest_vdev_lock here to
5019 * prevent a race between injection testing and
5022 mutex_enter(&ztest_vdev_lock);
5023 (void) vdev_online(spa, guid0, 0, NULL);
5024 mutex_exit(&ztest_vdev_lock);
5032 * We have at least single-fault tolerance, so inject data corruption.
5034 fd = open(pathrand, O_RDWR);
5036 if (fd == -1) /* we hit a gap in the device namespace */
5039 fsize = lseek(fd, 0, SEEK_END);
5041 while (--iters != 0) {
5042 offset = ztest_random(fsize / (leaves << bshift)) *
5043 (leaves << bshift) + (leaf << bshift) +
5044 (ztest_random(1ULL << (bshift - 1)) & -8ULL);
5046 if (offset >= fsize)
5049 mutex_enter(&ztest_vdev_lock);
5050 if (mirror_save != zs->zs_mirrors) {
5051 mutex_exit(&ztest_vdev_lock);
5056 if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
5057 fatal(1, "can't inject bad word at 0x%llx in %s",
5060 mutex_exit(&ztest_vdev_lock);
5062 if (ztest_opts.zo_verbose >= 7)
5063 (void) printf("injected bad word into %s,"
5064 " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
5069 umem_free(path0, MAXPATHLEN);
5070 umem_free(pathrand, MAXPATHLEN);
5074 * Verify that DDT repair works as expected.
5077 ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
5079 ztest_shared_t *zs = ztest_shared;
5080 spa_t *spa = ztest_spa;
5081 objset_t *os = zd->zd_os;
5083 uint64_t object, blocksize, txg, pattern, psize;
5084 enum zio_checksum checksum = spa_dedup_checksum(spa);
5089 int copies = 2 * ZIO_DEDUPDITTO_MIN;
5092 blocksize = ztest_random_blocksize();
5093 blocksize = MIN(blocksize, 2048); /* because we write so many */
5095 od = umem_alloc(sizeof(ztest_od_t), UMEM_NOFAIL);
5096 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
5098 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
5099 umem_free(od, sizeof(ztest_od_t));
5104 * Take the name lock as writer to prevent anyone else from changing
5105 * the pool and dataset properies we need to maintain during this test.
5107 (void) rw_enter(&ztest_name_lock, RW_WRITER);
5109 if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
5111 ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
5113 (void) rw_exit(&ztest_name_lock);
5114 umem_free(od, sizeof(ztest_od_t));
5118 object = od[0].od_object;
5119 blocksize = od[0].od_blocksize;
5120 pattern = zs->zs_guid ^ dmu_objset_fsid_guid(os);
5122 ASSERT(object != 0);
5124 tx = dmu_tx_create(os);
5125 dmu_tx_hold_write(tx, object, 0, copies * blocksize);
5126 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
5128 (void) rw_exit(&ztest_name_lock);
5129 umem_free(od, sizeof(ztest_od_t));
5134 * Write all the copies of our block.
5136 for (i = 0; i < copies; i++) {
5137 uint64_t offset = i * blocksize;
5138 int error = dmu_buf_hold(os, object, offset, FTAG, &db,
5139 DMU_READ_NO_PREFETCH);
5141 fatal(B_FALSE, "dmu_buf_hold(%p, %llu, %llu) = %u",
5142 os, (long long)object, (long long) offset, error);
5144 ASSERT(db->db_offset == offset);
5145 ASSERT(db->db_size == blocksize);
5146 ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
5147 ztest_pattern_match(db->db_data, db->db_size, 0ULL));
5148 dmu_buf_will_fill(db, tx);
5149 ztest_pattern_set(db->db_data, db->db_size, pattern);
5150 dmu_buf_rele(db, FTAG);
5154 txg_wait_synced(spa_get_dsl(spa), txg);
5157 * Find out what block we got.
5159 VERIFY0(dmu_buf_hold(os, object, 0, FTAG, &db,
5160 DMU_READ_NO_PREFETCH));
5161 blk = *((dmu_buf_impl_t *)db)->db_blkptr;
5162 dmu_buf_rele(db, FTAG);
5165 * Damage the block. Dedup-ditto will save us when we read it later.
5167 psize = BP_GET_PSIZE(&blk);
5168 buf = zio_buf_alloc(psize);
5169 ztest_pattern_set(buf, psize, ~pattern);
5171 (void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
5172 buf, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
5173 ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
5175 zio_buf_free(buf, psize);
5177 (void) rw_exit(&ztest_name_lock);
5178 umem_free(od, sizeof(ztest_od_t));
5186 ztest_scrub(ztest_ds_t *zd, uint64_t id)
5188 spa_t *spa = ztest_spa;
5190 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5191 (void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
5192 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5196 * Change the guid for the pool.
5200 ztest_reguid(ztest_ds_t *zd, uint64_t id)
5202 spa_t *spa = ztest_spa;
5203 uint64_t orig, load;
5206 orig = spa_guid(spa);
5207 load = spa_load_guid(spa);
5209 (void) rw_enter(&ztest_name_lock, RW_WRITER);
5210 error = spa_change_guid(spa);
5211 (void) rw_exit(&ztest_name_lock);
5216 if (ztest_opts.zo_verbose >= 4) {
5217 (void) printf("Changed guid old %llu -> %llu\n",
5218 (u_longlong_t)orig, (u_longlong_t)spa_guid(spa));
5221 VERIFY3U(orig, !=, spa_guid(spa));
5222 VERIFY3U(load, ==, spa_load_guid(spa));
5226 * Rename the pool to a different name and then rename it back.
5230 ztest_spa_rename(ztest_ds_t *zd, uint64_t id)
5232 char *oldname, *newname;
5235 (void) rw_enter(&ztest_name_lock, RW_WRITER);
5237 oldname = ztest_opts.zo_pool;
5238 newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
5239 (void) strcpy(newname, oldname);
5240 (void) strcat(newname, "_tmp");
5245 VERIFY3U(0, ==, spa_rename(oldname, newname));
5248 * Try to open it under the old name, which shouldn't exist
5250 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5253 * Open it under the new name and make sure it's still the same spa_t.
5255 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
5257 ASSERT(spa == ztest_spa);
5258 spa_close(spa, FTAG);
5261 * Rename it back to the original
5263 VERIFY3U(0, ==, spa_rename(newname, oldname));
5266 * Make sure it can still be opened
5268 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
5270 ASSERT(spa == ztest_spa);
5271 spa_close(spa, FTAG);
5273 umem_free(newname, strlen(newname) + 1);
5275 (void) rw_exit(&ztest_name_lock);
5279 * Verify pool integrity by running zdb.
5282 ztest_run_zdb(char *pool)
5290 bin = umem_alloc(MAXPATHLEN + MAXNAMELEN + 20, UMEM_NOFAIL);
5291 zdb = umem_alloc(MAXPATHLEN + MAXNAMELEN + 20, UMEM_NOFAIL);
5292 zbuf = umem_alloc(1024, UMEM_NOFAIL);
5294 VERIFY(realpath(getexecname(), bin) != NULL);
5295 if (strncmp(bin, "/usr/sbin/ztest", 15) == 0) {
5296 strcpy(bin, "/usr/sbin/zdb"); /* Installed */
5297 } else if (strncmp(bin, "/sbin/ztest", 11) == 0) {
5298 strcpy(bin, "/sbin/zdb"); /* Installed */
5300 strstr(bin, "/ztest/")[0] = '\0'; /* In-tree */
5301 strcat(bin, "/zdb/zdb");
5305 "%s -bcc%s%s -U %s %s",
5307 ztest_opts.zo_verbose >= 3 ? "s" : "",
5308 ztest_opts.zo_verbose >= 4 ? "v" : "",
5312 if (ztest_opts.zo_verbose >= 5)
5313 (void) printf("Executing %s\n", strstr(zdb, "zdb "));
5315 fp = popen(zdb, "r");
5317 while (fgets(zbuf, 1024, fp) != NULL)
5318 if (ztest_opts.zo_verbose >= 3)
5319 (void) printf("%s", zbuf);
5321 status = pclose(fp);
5326 ztest_dump_core = 0;
5327 if (WIFEXITED(status))
5328 fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
5330 fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
5332 umem_free(bin, MAXPATHLEN + MAXNAMELEN + 20);
5333 umem_free(zdb, MAXPATHLEN + MAXNAMELEN + 20);
5334 umem_free(zbuf, 1024);
5338 ztest_walk_pool_directory(char *header)
5342 if (ztest_opts.zo_verbose >= 6)
5343 (void) printf("%s\n", header);
5345 mutex_enter(&spa_namespace_lock);
5346 while ((spa = spa_next(spa)) != NULL)
5347 if (ztest_opts.zo_verbose >= 6)
5348 (void) printf("\t%s\n", spa_name(spa));
5349 mutex_exit(&spa_namespace_lock);
5353 ztest_spa_import_export(char *oldname, char *newname)
5355 nvlist_t *config, *newconfig;
5360 if (ztest_opts.zo_verbose >= 4) {
5361 (void) printf("import/export: old = %s, new = %s\n",
5366 * Clean up from previous runs.
5368 (void) spa_destroy(newname);
5371 * Get the pool's configuration and guid.
5373 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
5376 * Kick off a scrub to tickle scrub/export races.
5378 if (ztest_random(2) == 0)
5379 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5381 pool_guid = spa_guid(spa);
5382 spa_close(spa, FTAG);
5384 ztest_walk_pool_directory("pools before export");
5389 VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
5391 ztest_walk_pool_directory("pools after export");
5396 newconfig = spa_tryimport(config);
5397 ASSERT(newconfig != NULL);
5398 nvlist_free(newconfig);
5401 * Import it under the new name.
5403 error = spa_import(newname, config, NULL, 0);
5405 dump_nvlist(config, 0);
5406 fatal(B_FALSE, "couldn't import pool %s as %s: error %u",
5407 oldname, newname, error);
5410 ztest_walk_pool_directory("pools after import");
5413 * Try to import it again -- should fail with EEXIST.
5415 VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL, 0));
5418 * Try to import it under a different name -- should fail with EEXIST.
5420 VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL, 0));
5423 * Verify that the pool is no longer visible under the old name.
5425 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5428 * Verify that we can open and close the pool using the new name.
5430 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
5431 ASSERT(pool_guid == spa_guid(spa));
5432 spa_close(spa, FTAG);
5434 nvlist_free(config);
5438 ztest_resume(spa_t *spa)
5440 if (spa_suspended(spa) && ztest_opts.zo_verbose >= 6)
5441 (void) printf("resuming from suspended state\n");
5442 spa_vdev_state_enter(spa, SCL_NONE);
5443 vdev_clear(spa, NULL);
5444 (void) spa_vdev_state_exit(spa, NULL, 0);
5445 (void) zio_resume(spa);
5449 ztest_resume_thread(void *arg)
5453 while (!ztest_exiting) {
5454 if (spa_suspended(spa))
5456 (void) poll(NULL, 0, 100);
5468 ztest_deadman_alarm(int sig)
5470 fatal(0, "failed to complete within %d seconds of deadline", GRACE);
5475 ztest_execute(int test, ztest_info_t *zi, uint64_t id)
5477 ztest_ds_t *zd = &ztest_ds[id % ztest_opts.zo_datasets];
5478 ztest_shared_callstate_t *zc = ZTEST_GET_SHARED_CALLSTATE(test);
5479 hrtime_t functime = gethrtime();
5482 for (i = 0; i < zi->zi_iters; i++)
5483 zi->zi_func(zd, id);
5485 functime = gethrtime() - functime;
5487 atomic_add_64(&zc->zc_count, 1);
5488 atomic_add_64(&zc->zc_time, functime);
5490 if (ztest_opts.zo_verbose >= 4) {
5492 (void) dladdr((void *)zi->zi_func, &dli);
5493 (void) printf("%6.2f sec in %s\n",
5494 (double)functime / NANOSEC, dli.dli_sname);
5499 ztest_thread(void *arg)
5502 uint64_t id = (uintptr_t)arg;
5503 ztest_shared_t *zs = ztest_shared;
5507 ztest_shared_callstate_t *zc;
5509 while ((now = gethrtime()) < zs->zs_thread_stop) {
5511 * See if it's time to force a crash.
5513 if (now > zs->zs_thread_kill)
5517 * If we're getting ENOSPC with some regularity, stop.
5519 if (zs->zs_enospc_count > 10)
5523 * Pick a random function to execute.
5525 rand = ztest_random(ZTEST_FUNCS);
5526 zi = &ztest_info[rand];
5527 zc = ZTEST_GET_SHARED_CALLSTATE(rand);
5528 call_next = zc->zc_next;
5530 if (now >= call_next &&
5531 atomic_cas_64(&zc->zc_next, call_next, call_next +
5532 ztest_random(2 * zi->zi_interval[0] + 1)) == call_next) {
5533 ztest_execute(rand, zi, id);
5543 ztest_dataset_name(char *dsname, char *pool, int d)
5545 (void) snprintf(dsname, MAXNAMELEN, "%s/ds_%d", pool, d);
5549 ztest_dataset_destroy(int d)
5551 char name[MAXNAMELEN];
5554 ztest_dataset_name(name, ztest_opts.zo_pool, d);
5556 if (ztest_opts.zo_verbose >= 3)
5557 (void) printf("Destroying %s to free up space\n", name);
5560 * Cleanup any non-standard clones and snapshots. In general,
5561 * ztest thread t operates on dataset (t % zopt_datasets),
5562 * so there may be more than one thing to clean up.
5564 for (t = d; t < ztest_opts.zo_threads;
5565 t += ztest_opts.zo_datasets)
5566 ztest_dsl_dataset_cleanup(name, t);
5568 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
5569 DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
5573 ztest_dataset_dirobj_verify(ztest_ds_t *zd)
5575 uint64_t usedobjs, dirobjs, scratch;
5578 * ZTEST_DIROBJ is the object directory for the entire dataset.
5579 * Therefore, the number of objects in use should equal the
5580 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
5581 * If not, we have an object leak.
5583 * Note that we can only check this in ztest_dataset_open(),
5584 * when the open-context and syncing-context values agree.
5585 * That's because zap_count() returns the open-context value,
5586 * while dmu_objset_space() returns the rootbp fill count.
5588 VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
5589 dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
5590 ASSERT3U(dirobjs + 1, ==, usedobjs);
5594 ztest_dataset_open(int d)
5596 ztest_ds_t *zd = &ztest_ds[d];
5597 uint64_t committed_seq = ZTEST_GET_SHARED_DS(d)->zd_seq;
5600 char name[MAXNAMELEN];
5603 ztest_dataset_name(name, ztest_opts.zo_pool, d);
5605 (void) rw_enter(&ztest_name_lock, RW_READER);
5607 error = ztest_dataset_create(name);
5608 if (error == ENOSPC) {
5609 (void) rw_exit(&ztest_name_lock);
5610 ztest_record_enospc(FTAG);
5613 ASSERT(error == 0 || error == EEXIST);
5615 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, zd, &os));
5616 (void) rw_exit(&ztest_name_lock);
5618 ztest_zd_init(zd, ZTEST_GET_SHARED_DS(d), os);
5620 zilog = zd->zd_zilog;
5622 if (zilog->zl_header->zh_claim_lr_seq != 0 &&
5623 zilog->zl_header->zh_claim_lr_seq < committed_seq)
5624 fatal(0, "missing log records: claimed %llu < committed %llu",
5625 zilog->zl_header->zh_claim_lr_seq, committed_seq);
5627 ztest_dataset_dirobj_verify(zd);
5629 zil_replay(os, zd, ztest_replay_vector);
5631 ztest_dataset_dirobj_verify(zd);
5633 if (ztest_opts.zo_verbose >= 6)
5634 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
5636 (u_longlong_t)zilog->zl_parse_blk_count,
5637 (u_longlong_t)zilog->zl_parse_lr_count,
5638 (u_longlong_t)zilog->zl_replaying_seq);
5640 zilog = zil_open(os, ztest_get_data);
5642 if (zilog->zl_replaying_seq != 0 &&
5643 zilog->zl_replaying_seq < committed_seq)
5644 fatal(0, "missing log records: replayed %llu < committed %llu",
5645 zilog->zl_replaying_seq, committed_seq);
5651 ztest_dataset_close(int d)
5653 ztest_ds_t *zd = &ztest_ds[d];
5655 zil_close(zd->zd_zilog);
5656 dmu_objset_disown(zd->zd_os, zd);
5662 * Kick off threads to run tests on all datasets in parallel.
5665 ztest_run(ztest_shared_t *zs)
5670 kthread_t *resume_thread;
5675 ztest_exiting = B_FALSE;
5678 * Initialize parent/child shared state.
5680 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
5681 rw_init(&ztest_name_lock, NULL, RW_DEFAULT, NULL);
5683 zs->zs_thread_start = gethrtime();
5684 zs->zs_thread_stop =
5685 zs->zs_thread_start + ztest_opts.zo_passtime * NANOSEC;
5686 zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
5687 zs->zs_thread_kill = zs->zs_thread_stop;
5688 if (ztest_random(100) < ztest_opts.zo_killrate) {
5689 zs->zs_thread_kill -=
5690 ztest_random(ztest_opts.zo_passtime * NANOSEC);
5693 mutex_init(&zcl.zcl_callbacks_lock, NULL, MUTEX_DEFAULT, NULL);
5695 list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
5696 offsetof(ztest_cb_data_t, zcd_node));
5701 kernel_init(FREAD | FWRITE);
5702 VERIFY0(spa_open(ztest_opts.zo_pool, &spa, FTAG));
5703 spa->spa_debug = B_TRUE;
5706 VERIFY0(dmu_objset_own(ztest_opts.zo_pool,
5707 DMU_OST_ANY, B_TRUE, FTAG, &os));
5708 zs->zs_guid = dmu_objset_fsid_guid(os);
5709 dmu_objset_disown(os, FTAG);
5711 spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
5714 * We don't expect the pool to suspend unless maxfaults == 0,
5715 * in which case ztest_fault_inject() temporarily takes away
5716 * the only valid replica.
5718 if (MAXFAULTS() == 0)
5719 spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
5721 spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
5724 * Create a thread to periodically resume suspended I/O.
5726 VERIFY3P((resume_thread = zk_thread_create(NULL, 0,
5727 (thread_func_t)ztest_resume_thread, spa, TS_RUN, NULL, 0, 0,
5728 PTHREAD_CREATE_JOINABLE)), !=, NULL);
5732 * Set a deadman alarm to abort() if we hang.
5734 signal(SIGALRM, ztest_deadman_alarm);
5735 alarm((zs->zs_thread_stop - zs->zs_thread_start) / NANOSEC + GRACE);
5739 * Verify that we can safely inquire about about any object,
5740 * whether it's allocated or not. To make it interesting,
5741 * we probe a 5-wide window around each power of two.
5742 * This hits all edge cases, including zero and the max.
5744 for (t = 0; t < 64; t++) {
5745 for (d = -5; d <= 5; d++) {
5746 error = dmu_object_info(spa->spa_meta_objset,
5747 (1ULL << t) + d, NULL);
5748 ASSERT(error == 0 || error == ENOENT ||
5754 * If we got any ENOSPC errors on the previous run, destroy something.
5756 if (zs->zs_enospc_count != 0) {
5757 int d = ztest_random(ztest_opts.zo_datasets);
5758 ztest_dataset_destroy(d);
5760 zs->zs_enospc_count = 0;
5762 tid = umem_zalloc(ztest_opts.zo_threads * sizeof (kt_did_t),
5765 if (ztest_opts.zo_verbose >= 4)
5766 (void) printf("starting main threads...\n");
5769 * Kick off all the tests that run in parallel.
5771 for (t = 0; t < ztest_opts.zo_threads; t++) {
5774 if (t < ztest_opts.zo_datasets &&
5775 ztest_dataset_open(t) != 0)
5778 VERIFY3P(thread = zk_thread_create(NULL, 0,
5779 (thread_func_t)ztest_thread,
5780 (void *)(uintptr_t)t, TS_RUN, NULL, 0, 0,
5781 PTHREAD_CREATE_JOINABLE), !=, NULL);
5782 tid[t] = thread->t_tid;
5786 * Wait for all of the tests to complete. We go in reverse order
5787 * so we don't close datasets while threads are still using them.
5789 for (t = ztest_opts.zo_threads - 1; t >= 0; t--) {
5790 thread_join(tid[t]);
5791 if (t < ztest_opts.zo_datasets)
5792 ztest_dataset_close(t);
5795 txg_wait_synced(spa_get_dsl(spa), 0);
5797 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
5798 zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
5800 umem_free(tid, ztest_opts.zo_threads * sizeof (kt_did_t));
5802 /* Kill the resume thread */
5803 ztest_exiting = B_TRUE;
5804 thread_join(resume_thread->t_tid);
5808 * Right before closing the pool, kick off a bunch of async I/O;
5809 * spa_close() should wait for it to complete.
5811 for (object = 1; object < 50; object++)
5812 dmu_prefetch(spa->spa_meta_objset, object, 0, 1ULL << 20);
5814 /* Verify that at least one commit cb was called in a timely fashion */
5815 if (zc_cb_counter >= ZTEST_COMMIT_CB_MIN_REG)
5816 VERIFY0(zc_min_txg_delay);
5818 spa_close(spa, FTAG);
5821 * Verify that we can loop over all pools.
5823 mutex_enter(&spa_namespace_lock);
5824 for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
5825 if (ztest_opts.zo_verbose > 3)
5826 (void) printf("spa_next: found %s\n", spa_name(spa));
5827 mutex_exit(&spa_namespace_lock);
5830 * Verify that we can export the pool and reimport it under a
5833 if (ztest_random(2) == 0) {
5834 char name[MAXNAMELEN];
5835 (void) snprintf(name, MAXNAMELEN, "%s_import",
5836 ztest_opts.zo_pool);
5837 ztest_spa_import_export(ztest_opts.zo_pool, name);
5838 ztest_spa_import_export(name, ztest_opts.zo_pool);
5843 list_destroy(&zcl.zcl_callbacks);
5844 mutex_destroy(&zcl.zcl_callbacks_lock);
5845 rw_destroy(&ztest_name_lock);
5846 mutex_destroy(&ztest_vdev_lock);
5852 ztest_ds_t *zd = &ztest_ds[0];
5856 if (ztest_opts.zo_verbose >= 3)
5857 (void) printf("testing spa_freeze()...\n");
5859 kernel_init(FREAD | FWRITE);
5860 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
5861 VERIFY3U(0, ==, ztest_dataset_open(0));
5862 spa->spa_debug = B_TRUE;
5866 * Force the first log block to be transactionally allocated.
5867 * We have to do this before we freeze the pool -- otherwise
5868 * the log chain won't be anchored.
5870 while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
5871 ztest_dmu_object_alloc_free(zd, 0);
5872 zil_commit(zd->zd_zilog, 0);
5875 txg_wait_synced(spa_get_dsl(spa), 0);
5878 * Freeze the pool. This stops spa_sync() from doing anything,
5879 * so that the only way to record changes from now on is the ZIL.
5884 * Run tests that generate log records but don't alter the pool config
5885 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
5886 * We do a txg_wait_synced() after each iteration to force the txg
5887 * to increase well beyond the last synced value in the uberblock.
5888 * The ZIL should be OK with that.
5890 while (ztest_random(10) != 0 &&
5891 numloops++ < ztest_opts.zo_maxloops) {
5892 ztest_dmu_write_parallel(zd, 0);
5893 ztest_dmu_object_alloc_free(zd, 0);
5894 txg_wait_synced(spa_get_dsl(spa), 0);
5898 * Commit all of the changes we just generated.
5900 zil_commit(zd->zd_zilog, 0);
5901 txg_wait_synced(spa_get_dsl(spa), 0);
5904 * Close our dataset and close the pool.
5906 ztest_dataset_close(0);
5907 spa_close(spa, FTAG);
5911 * Open and close the pool and dataset to induce log replay.
5913 kernel_init(FREAD | FWRITE);
5914 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
5915 ASSERT(spa_freeze_txg(spa) == UINT64_MAX);
5916 VERIFY3U(0, ==, ztest_dataset_open(0));
5917 ztest_dataset_close(0);
5919 spa->spa_debug = B_TRUE;
5921 txg_wait_synced(spa_get_dsl(spa), 0);
5922 ztest_reguid(NULL, 0);
5924 spa_close(spa, FTAG);
5929 print_time(hrtime_t t, char *timebuf)
5931 hrtime_t s = t / NANOSEC;
5932 hrtime_t m = s / 60;
5933 hrtime_t h = m / 60;
5934 hrtime_t d = h / 24;
5943 (void) sprintf(timebuf,
5944 "%llud%02lluh%02llum%02llus", d, h, m, s);
5946 (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
5948 (void) sprintf(timebuf, "%llum%02llus", m, s);
5950 (void) sprintf(timebuf, "%llus", s);
5954 make_random_props(void)
5958 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
5959 if (ztest_random(2) == 0)
5961 VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
5967 * Create a storage pool with the given name and initial vdev size.
5968 * Then test spa_freeze() functionality.
5971 ztest_init(ztest_shared_t *zs)
5974 nvlist_t *nvroot, *props;
5977 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
5978 rw_init(&ztest_name_lock, NULL, RW_DEFAULT, NULL);
5980 kernel_init(FREAD | FWRITE);
5983 * Create the storage pool.
5985 (void) spa_destroy(ztest_opts.zo_pool);
5986 ztest_shared->zs_vdev_next_leaf = 0;
5988 zs->zs_mirrors = ztest_opts.zo_mirrors;
5989 nvroot = make_vdev_root(NULL, NULL, NULL, ztest_opts.zo_vdev_size, 0,
5990 0, ztest_opts.zo_raidz, zs->zs_mirrors, 1);
5991 props = make_random_props();
5992 for (i = 0; i < SPA_FEATURES; i++) {
5994 VERIFY3S(-1, !=, asprintf(&buf, "feature@%s",
5995 spa_feature_table[i].fi_uname));
5996 VERIFY3U(0, ==, nvlist_add_uint64(props, buf, 0));
5999 VERIFY3U(0, ==, spa_create(ztest_opts.zo_pool, nvroot, props, NULL));
6000 nvlist_free(nvroot);
6002 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6003 zs->zs_metaslab_sz =
6004 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
6005 spa_close(spa, FTAG);
6009 ztest_run_zdb(ztest_opts.zo_pool);
6013 ztest_run_zdb(ztest_opts.zo_pool);
6015 rw_destroy(&ztest_name_lock);
6016 mutex_destroy(&ztest_vdev_lock);
6022 static char ztest_name_data[] = "/tmp/ztest.data.XXXXXX";
6024 ztest_fd_data = mkstemp(ztest_name_data);
6025 ASSERT3S(ztest_fd_data, >=, 0);
6026 (void) unlink(ztest_name_data);
6030 shared_data_size(ztest_shared_hdr_t *hdr)
6034 size = hdr->zh_hdr_size;
6035 size += hdr->zh_opts_size;
6036 size += hdr->zh_size;
6037 size += hdr->zh_stats_size * hdr->zh_stats_count;
6038 size += hdr->zh_ds_size * hdr->zh_ds_count;
6047 ztest_shared_hdr_t *hdr;
6049 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6050 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6051 ASSERT(hdr != MAP_FAILED);
6053 VERIFY3U(0, ==, ftruncate(ztest_fd_data, sizeof (ztest_shared_hdr_t)));
6055 hdr->zh_hdr_size = sizeof (ztest_shared_hdr_t);
6056 hdr->zh_opts_size = sizeof (ztest_shared_opts_t);
6057 hdr->zh_size = sizeof (ztest_shared_t);
6058 hdr->zh_stats_size = sizeof (ztest_shared_callstate_t);
6059 hdr->zh_stats_count = ZTEST_FUNCS;
6060 hdr->zh_ds_size = sizeof (ztest_shared_ds_t);
6061 hdr->zh_ds_count = ztest_opts.zo_datasets;
6063 size = shared_data_size(hdr);
6064 VERIFY3U(0, ==, ftruncate(ztest_fd_data, size));
6066 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6073 ztest_shared_hdr_t *hdr;
6076 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6077 PROT_READ, MAP_SHARED, ztest_fd_data, 0);
6078 ASSERT(hdr != MAP_FAILED);
6080 size = shared_data_size(hdr);
6082 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6083 hdr = ztest_shared_hdr = (void *)mmap(0, P2ROUNDUP(size, getpagesize()),
6084 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6085 ASSERT(hdr != MAP_FAILED);
6086 buf = (uint8_t *)hdr;
6088 offset = hdr->zh_hdr_size;
6089 ztest_shared_opts = (void *)&buf[offset];
6090 offset += hdr->zh_opts_size;
6091 ztest_shared = (void *)&buf[offset];
6092 offset += hdr->zh_size;
6093 ztest_shared_callstate = (void *)&buf[offset];
6094 offset += hdr->zh_stats_size * hdr->zh_stats_count;
6095 ztest_shared_ds = (void *)&buf[offset];
6099 exec_child(char *cmd, char *libpath, boolean_t ignorekill, int *statusp)
6103 char *cmdbuf = NULL;
6108 cmdbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
6109 (void) strlcpy(cmdbuf, getexecname(), MAXPATHLEN);
6114 fatal(1, "fork failed");
6116 if (pid == 0) { /* child */
6117 char *emptyargv[2] = { cmd, NULL };
6118 char fd_data_str[12];
6120 struct rlimit rl = { 1024, 1024 };
6121 (void) setrlimit(RLIMIT_NOFILE, &rl);
6123 (void) close(ztest_fd_rand);
6124 VERIFY(11 >= snprintf(fd_data_str, 12, "%d", ztest_fd_data));
6125 VERIFY(0 == setenv("ZTEST_FD_DATA", fd_data_str, 1));
6127 (void) enable_extended_FILE_stdio(-1, -1);
6128 if (libpath != NULL)
6129 VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath, 1));
6130 (void) execv(cmd, emptyargv);
6131 ztest_dump_core = B_FALSE;
6132 fatal(B_TRUE, "exec failed: %s", cmd);
6135 if (cmdbuf != NULL) {
6136 umem_free(cmdbuf, MAXPATHLEN);
6140 while (waitpid(pid, &status, 0) != pid)
6142 if (statusp != NULL)
6145 if (WIFEXITED(status)) {
6146 if (WEXITSTATUS(status) != 0) {
6147 (void) fprintf(stderr, "child exited with code %d\n",
6148 WEXITSTATUS(status));
6152 } else if (WIFSIGNALED(status)) {
6153 if (!ignorekill || WTERMSIG(status) != SIGKILL) {
6154 (void) fprintf(stderr, "child died with signal %d\n",
6160 (void) fprintf(stderr, "something strange happened to child\n");
6167 ztest_run_init(void)
6171 ztest_shared_t *zs = ztest_shared;
6173 ASSERT(ztest_opts.zo_init != 0);
6176 * Blow away any existing copy of zpool.cache
6178 (void) remove(spa_config_path);
6181 * Create and initialize our storage pool.
6183 for (i = 1; i <= ztest_opts.zo_init; i++) {
6184 bzero(zs, sizeof (ztest_shared_t));
6185 if (ztest_opts.zo_verbose >= 3 &&
6186 ztest_opts.zo_init != 1) {
6187 (void) printf("ztest_init(), pass %d\n", i);
6194 main(int argc, char **argv)
6202 ztest_shared_callstate_t *zc;
6209 char *fd_data_str = getenv("ZTEST_FD_DATA");
6211 (void) setvbuf(stdout, NULL, _IOLBF, 0);
6213 ztest_fd_rand = open("/dev/urandom", O_RDONLY);
6214 ASSERT3S(ztest_fd_rand, >=, 0);
6217 dprintf_setup(&argc, argv);
6218 process_options(argc, argv);
6223 bcopy(&ztest_opts, ztest_shared_opts,
6224 sizeof (*ztest_shared_opts));
6226 ztest_fd_data = atoi(fd_data_str);
6228 bcopy(ztest_shared_opts, &ztest_opts, sizeof (ztest_opts));
6230 ASSERT3U(ztest_opts.zo_datasets, ==, ztest_shared_hdr->zh_ds_count);
6232 /* Override location of zpool.cache */
6233 VERIFY(asprintf((char **)&spa_config_path, "%s/zpool.cache",
6234 ztest_opts.zo_dir) != -1);
6236 ztest_ds = umem_alloc(ztest_opts.zo_datasets * sizeof (ztest_ds_t),
6241 metaslab_gang_bang = ztest_opts.zo_metaslab_gang_bang;
6242 metaslab_df_alloc_threshold =
6243 zs->zs_metaslab_df_alloc_threshold;
6252 hasalt = (strlen(ztest_opts.zo_alt_ztest) != 0);
6254 if (ztest_opts.zo_verbose >= 1) {
6255 (void) printf("%llu vdevs, %d datasets, %d threads,"
6256 " %llu seconds...\n",
6257 (u_longlong_t)ztest_opts.zo_vdevs,
6258 ztest_opts.zo_datasets,
6259 ztest_opts.zo_threads,
6260 (u_longlong_t)ztest_opts.zo_time);
6263 cmd = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
6264 (void) strlcpy(cmd, getexecname(), MAXNAMELEN);
6266 zs->zs_do_init = B_TRUE;
6267 if (strlen(ztest_opts.zo_alt_ztest) != 0) {
6268 if (ztest_opts.zo_verbose >= 1) {
6269 (void) printf("Executing older ztest for "
6270 "initialization: %s\n", ztest_opts.zo_alt_ztest);
6272 VERIFY(!exec_child(ztest_opts.zo_alt_ztest,
6273 ztest_opts.zo_alt_libpath, B_FALSE, NULL));
6275 VERIFY(!exec_child(NULL, NULL, B_FALSE, NULL));
6277 zs->zs_do_init = B_FALSE;
6279 zs->zs_proc_start = gethrtime();
6280 zs->zs_proc_stop = zs->zs_proc_start + ztest_opts.zo_time * NANOSEC;
6282 for (f = 0; f < ZTEST_FUNCS; f++) {
6283 zi = &ztest_info[f];
6284 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6285 if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
6286 zc->zc_next = UINT64_MAX;
6288 zc->zc_next = zs->zs_proc_start +
6289 ztest_random(2 * zi->zi_interval[0] + 1);
6293 * Run the tests in a loop. These tests include fault injection
6294 * to verify that self-healing data works, and forced crashes
6295 * to verify that we never lose on-disk consistency.
6297 while (gethrtime() < zs->zs_proc_stop) {
6302 * Initialize the workload counters for each function.
6304 for (f = 0; f < ZTEST_FUNCS; f++) {
6305 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6310 /* Set the allocation switch size */
6311 zs->zs_metaslab_df_alloc_threshold =
6312 ztest_random(zs->zs_metaslab_sz / 4) + 1;
6314 if (!hasalt || ztest_random(2) == 0) {
6315 if (hasalt && ztest_opts.zo_verbose >= 1) {
6316 (void) printf("Executing newer ztest: %s\n",
6320 killed = exec_child(cmd, NULL, B_TRUE, &status);
6322 if (hasalt && ztest_opts.zo_verbose >= 1) {
6323 (void) printf("Executing older ztest: %s\n",
6324 ztest_opts.zo_alt_ztest);
6327 killed = exec_child(ztest_opts.zo_alt_ztest,
6328 ztest_opts.zo_alt_libpath, B_TRUE, &status);
6335 if (ztest_opts.zo_verbose >= 1) {
6336 hrtime_t now = gethrtime();
6338 now = MIN(now, zs->zs_proc_stop);
6339 print_time(zs->zs_proc_stop - now, timebuf);
6340 nicenum(zs->zs_space, numbuf);
6342 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
6343 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
6345 WIFEXITED(status) ? "Complete" : "SIGKILL",
6346 (u_longlong_t)zs->zs_enospc_count,
6347 100.0 * zs->zs_alloc / zs->zs_space,
6349 100.0 * (now - zs->zs_proc_start) /
6350 (ztest_opts.zo_time * NANOSEC), timebuf);
6353 if (ztest_opts.zo_verbose >= 2) {
6354 (void) printf("\nWorkload summary:\n\n");
6355 (void) printf("%7s %9s %s\n",
6356 "Calls", "Time", "Function");
6357 (void) printf("%7s %9s %s\n",
6358 "-----", "----", "--------");
6359 for (f = 0; f < ZTEST_FUNCS; f++) {
6362 zi = &ztest_info[f];
6363 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6364 print_time(zc->zc_time, timebuf);
6365 (void) dladdr((void *)zi->zi_func, &dli);
6366 (void) printf("%7llu %9s %s\n",
6367 (u_longlong_t)zc->zc_count, timebuf,
6370 (void) printf("\n");
6374 * It's possible that we killed a child during a rename test,
6375 * in which case we'll have a 'ztest_tmp' pool lying around
6376 * instead of 'ztest'. Do a blind rename in case this happened.
6379 if (spa_open(ztest_opts.zo_pool, &spa, FTAG) == 0) {
6380 spa_close(spa, FTAG);
6382 char tmpname[MAXNAMELEN];
6384 kernel_init(FREAD | FWRITE);
6385 (void) snprintf(tmpname, sizeof (tmpname), "%s_tmp",
6386 ztest_opts.zo_pool);
6387 (void) spa_rename(tmpname, ztest_opts.zo_pool);
6391 ztest_run_zdb(ztest_opts.zo_pool);
6394 if (ztest_opts.zo_verbose >= 1) {
6396 (void) printf("%d runs of older ztest: %s\n", older,
6397 ztest_opts.zo_alt_ztest);
6398 (void) printf("%d runs of newer ztest: %s\n", newer,
6401 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
6402 kills, iters - kills, (100.0 * kills) / MAX(1, iters));
6405 umem_free(cmd, MAXNAMELEN);