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.
28 * The objective of this program is to provide a DMU/ZAP/SPA stress test
29 * that runs entirely in userland, is easy to use, and easy to extend.
31 * The overall design of the ztest program is as follows:
33 * (1) For each major functional area (e.g. adding vdevs to a pool,
34 * creating and destroying datasets, reading and writing objects, etc)
35 * we have a simple routine to test that functionality. These
36 * individual routines do not have to do anything "stressful".
38 * (2) We turn these simple functionality tests into a stress test by
39 * running them all in parallel, with as many threads as desired,
40 * and spread across as many datasets, objects, and vdevs as desired.
42 * (3) While all this is happening, we inject faults into the pool to
43 * verify that self-healing data really works.
45 * (4) Every time we open a dataset, we change its checksum and compression
46 * functions. Thus even individual objects vary from block to block
47 * in which checksum they use and whether they're compressed.
49 * (5) To verify that we never lose on-disk consistency after a crash,
50 * we run the entire test in a child of the main process.
51 * At random times, the child self-immolates with a SIGKILL.
52 * This is the software equivalent of pulling the power cord.
53 * The parent then runs the test again, using the existing
54 * storage pool, as many times as desired. If backwards compatability
55 * testing is enabled ztest will sometimes run the "older" version
56 * of ztest after a SIGKILL.
58 * (6) To verify that we don't have future leaks or temporal incursions,
59 * many of the functional tests record the transaction group number
60 * as part of their data. When reading old data, they verify that
61 * the transaction group number is less than the current, open txg.
62 * If you add a new test, please do this if applicable.
64 * (7) Threads are created with a reduced stack size, for sanity checking.
65 * Therefore, it's important not to allocate huge buffers on the stack.
67 * When run with no arguments, ztest runs for about five minutes and
68 * produces no output if successful. To get a little bit of information,
69 * specify -V. To get more information, specify -VV, and so on.
71 * To turn this into an overnight stress test, use -T to specify run time.
73 * You can ask more more vdevs [-v], datasets [-d], or threads [-t]
74 * to increase the pool capacity, fanout, and overall stress level.
76 * Use the -k option to set the desired frequency of kills.
78 * When ztest invokes itself it passes all relevant information through a
79 * temporary file which is mmap-ed in the child process. This allows shared
80 * memory to survive the exec syscall. The ztest_shared_hdr_t struct is always
81 * stored at offset 0 of this file and contains information on the size and
82 * number of shared structures in the file. The information stored in this file
83 * must remain backwards compatible with older versions of ztest so that
84 * ztest can invoke them during backwards compatibility testing (-B).
87 #include <sys/zfs_context.h>
93 #include <sys/dmu_objset.h>
99 #include <sys/resource.h>
102 #include <sys/zil_impl.h>
103 #include <sys/vdev_impl.h>
104 #include <sys/vdev_file.h>
105 #include <sys/spa_impl.h>
106 #include <sys/metaslab_impl.h>
107 #include <sys/dsl_prop.h>
108 #include <sys/dsl_dataset.h>
109 #include <sys/dsl_scan.h>
110 #include <sys/zio_checksum.h>
111 #include <sys/refcount.h>
112 #include <sys/zfeature.h>
114 #include <stdio_ext.h>
122 #include <sys/fs/zfs.h>
123 #include <libnvpair.h>
125 static int ztest_fd_data = -1;
126 static int ztest_fd_rand = -1;
128 typedef struct ztest_shared_hdr {
129 uint64_t zh_hdr_size;
130 uint64_t zh_opts_size;
132 uint64_t zh_stats_size;
133 uint64_t zh_stats_count;
135 uint64_t zh_ds_count;
136 } ztest_shared_hdr_t;
138 static ztest_shared_hdr_t *ztest_shared_hdr;
140 typedef struct ztest_shared_opts {
141 char zo_pool[MAXNAMELEN];
142 char zo_dir[MAXNAMELEN];
143 char zo_alt_ztest[MAXNAMELEN];
144 char zo_alt_libpath[MAXNAMELEN];
146 uint64_t zo_vdevtime;
154 uint64_t zo_passtime;
155 uint64_t zo_killrate;
159 uint64_t zo_maxloops;
160 uint64_t zo_metaslab_gang_bang;
161 } ztest_shared_opts_t;
163 static const ztest_shared_opts_t ztest_opts_defaults = {
164 .zo_pool = { 'z', 't', 'e', 's', 't', '\0' },
165 .zo_dir = { '/', 't', 'm', 'p', '\0' },
166 .zo_alt_ztest = { '\0' },
167 .zo_alt_libpath = { '\0' },
169 .zo_ashift = SPA_MINBLOCKSHIFT,
172 .zo_raidz_parity = 1,
173 .zo_vdev_size = SPA_MINDEVSIZE,
176 .zo_passtime = 60, /* 60 seconds */
177 .zo_killrate = 70, /* 70% kill rate */
180 .zo_time = 300, /* 5 minutes */
181 .zo_maxloops = 50, /* max loops during spa_freeze() */
182 .zo_metaslab_gang_bang = 32 << 10
185 extern uint64_t metaslab_gang_bang;
186 extern uint64_t metaslab_df_alloc_threshold;
188 static ztest_shared_opts_t *ztest_shared_opts;
189 static ztest_shared_opts_t ztest_opts;
191 typedef struct ztest_shared_ds {
195 static ztest_shared_ds_t *ztest_shared_ds;
196 #define ZTEST_GET_SHARED_DS(d) (&ztest_shared_ds[d])
198 #define BT_MAGIC 0x123456789abcdefULL
199 #define MAXFAULTS() \
200 (MAX(zs->zs_mirrors, 1) * (ztest_opts.zo_raidz_parity + 1) - 1)
204 ZTEST_IO_WRITE_PATTERN,
205 ZTEST_IO_WRITE_ZEROES,
211 typedef struct ztest_block_tag {
221 typedef struct bufwad {
228 * XXX -- fix zfs range locks to be generic so we can use them here.
250 #define ZTEST_RANGE_LOCKS 64
251 #define ZTEST_OBJECT_LOCKS 64
254 * Object descriptor. Used as a template for object lookup/create/remove.
256 typedef struct ztest_od {
259 dmu_object_type_t od_type;
260 dmu_object_type_t od_crtype;
261 uint64_t od_blocksize;
262 uint64_t od_crblocksize;
265 char od_name[MAXNAMELEN];
271 typedef struct ztest_ds {
272 ztest_shared_ds_t *zd_shared;
274 krwlock_t zd_zilog_lock;
276 ztest_od_t *zd_od; /* debugging aid */
277 char zd_name[MAXNAMELEN];
278 kmutex_t zd_dirobj_lock;
279 rll_t zd_object_lock[ZTEST_OBJECT_LOCKS];
280 rll_t zd_range_lock[ZTEST_RANGE_LOCKS];
284 * Per-iteration state.
286 typedef void ztest_func_t(ztest_ds_t *zd, uint64_t id);
288 typedef struct ztest_info {
289 ztest_func_t *zi_func; /* test function */
290 uint64_t zi_iters; /* iterations per execution */
291 uint64_t *zi_interval; /* execute every <interval> seconds */
294 typedef struct ztest_shared_callstate {
295 uint64_t zc_count; /* per-pass count */
296 uint64_t zc_time; /* per-pass time */
297 uint64_t zc_next; /* next time to call this function */
298 } ztest_shared_callstate_t;
300 static ztest_shared_callstate_t *ztest_shared_callstate;
301 #define ZTEST_GET_SHARED_CALLSTATE(c) (&ztest_shared_callstate[c])
304 * Note: these aren't static because we want dladdr() to work.
306 ztest_func_t ztest_dmu_read_write;
307 ztest_func_t ztest_dmu_write_parallel;
308 ztest_func_t ztest_dmu_object_alloc_free;
309 ztest_func_t ztest_dmu_commit_callbacks;
310 ztest_func_t ztest_zap;
311 ztest_func_t ztest_zap_parallel;
312 ztest_func_t ztest_zil_commit;
313 ztest_func_t ztest_zil_remount;
314 ztest_func_t ztest_dmu_read_write_zcopy;
315 ztest_func_t ztest_dmu_objset_create_destroy;
316 ztest_func_t ztest_dmu_prealloc;
317 ztest_func_t ztest_fzap;
318 ztest_func_t ztest_dmu_snapshot_create_destroy;
319 ztest_func_t ztest_dsl_prop_get_set;
320 ztest_func_t ztest_spa_prop_get_set;
321 ztest_func_t ztest_spa_create_destroy;
322 ztest_func_t ztest_fault_inject;
323 ztest_func_t ztest_ddt_repair;
324 ztest_func_t ztest_dmu_snapshot_hold;
325 ztest_func_t ztest_spa_rename;
326 ztest_func_t ztest_scrub;
327 ztest_func_t ztest_dsl_dataset_promote_busy;
328 ztest_func_t ztest_vdev_attach_detach;
329 ztest_func_t ztest_vdev_LUN_growth;
330 ztest_func_t ztest_vdev_add_remove;
331 ztest_func_t ztest_vdev_aux_add_remove;
332 ztest_func_t ztest_split_pool;
333 ztest_func_t ztest_reguid;
335 uint64_t zopt_always = 0ULL * NANOSEC; /* all the time */
336 uint64_t zopt_incessant = 1ULL * NANOSEC / 10; /* every 1/10 second */
337 uint64_t zopt_often = 1ULL * NANOSEC; /* every second */
338 uint64_t zopt_sometimes = 10ULL * NANOSEC; /* every 10 seconds */
339 uint64_t zopt_rarely = 60ULL * NANOSEC; /* every 60 seconds */
341 ztest_info_t ztest_info[] = {
342 { ztest_dmu_read_write, 1, &zopt_always },
343 { ztest_dmu_write_parallel, 10, &zopt_always },
344 { ztest_dmu_object_alloc_free, 1, &zopt_always },
345 { ztest_dmu_commit_callbacks, 1, &zopt_always },
346 { ztest_zap, 30, &zopt_always },
347 { ztest_zap_parallel, 100, &zopt_always },
348 { ztest_split_pool, 1, &zopt_always },
349 { ztest_zil_commit, 1, &zopt_incessant },
350 { ztest_zil_remount, 1, &zopt_sometimes },
351 { ztest_dmu_read_write_zcopy, 1, &zopt_often },
352 { ztest_dmu_objset_create_destroy, 1, &zopt_often },
353 { ztest_dsl_prop_get_set, 1, &zopt_often },
354 { ztest_spa_prop_get_set, 1, &zopt_sometimes },
356 { ztest_dmu_prealloc, 1, &zopt_sometimes },
358 { ztest_fzap, 1, &zopt_sometimes },
359 { ztest_dmu_snapshot_create_destroy, 1, &zopt_sometimes },
360 { ztest_spa_create_destroy, 1, &zopt_sometimes },
361 { ztest_fault_inject, 1, &zopt_sometimes },
362 { ztest_ddt_repair, 1, &zopt_sometimes },
363 { ztest_dmu_snapshot_hold, 1, &zopt_sometimes },
365 * The reguid test is currently broken. Disable it until
366 * we get around to fixing it.
369 { ztest_reguid, 1, &zopt_sometimes },
371 { ztest_spa_rename, 1, &zopt_rarely },
372 { ztest_scrub, 1, &zopt_rarely },
373 { ztest_dsl_dataset_promote_busy, 1, &zopt_rarely },
374 { ztest_vdev_attach_detach, 1, &zopt_rarely },
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;
425 static krwlock_t ztest_name_lock;
427 static boolean_t ztest_dump_core = B_TRUE;
428 static boolean_t ztest_exiting;
430 /* Global commit callback list */
431 static ztest_cb_list_t zcl;
432 /* Commit cb delay */
433 static uint64_t zc_min_txg_delay = UINT64_MAX;
434 static int zc_cb_counter = 0;
437 * Minimum number of commit callbacks that need to be registered for us to check
438 * whether the minimum txg delay is acceptable.
440 #define ZTEST_COMMIT_CB_MIN_REG 100
443 * If a number of txgs equal to this threshold have been created after a commit
444 * callback has been registered but not called, then we assume there is an
445 * implementation bug.
447 #define ZTEST_COMMIT_CB_THRESH (TXG_CONCURRENT_STATES + 1000)
449 extern uint64_t metaslab_gang_bang;
450 extern uint64_t metaslab_df_alloc_threshold;
453 ZTEST_META_DNODE = 0,
458 static void usage(boolean_t) __NORETURN;
461 * These libumem hooks provide a reasonable set of defaults for the allocator's
462 * debugging facilities.
465 _umem_debug_init(void)
467 return ("default,verbose"); /* $UMEM_DEBUG setting */
471 _umem_logging_init(void)
473 return ("fail,contents"); /* $UMEM_LOGGING setting */
476 #define FATAL_MSG_SZ 1024
481 fatal(int do_perror, char *message, ...)
484 int save_errno = errno;
487 (void) fflush(stdout);
488 buf = umem_alloc(FATAL_MSG_SZ, UMEM_NOFAIL);
490 va_start(args, message);
491 (void) sprintf(buf, "ztest: ");
493 (void) vsprintf(buf + strlen(buf), message, args);
496 (void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
497 ": %s", strerror(save_errno));
499 (void) fprintf(stderr, "%s\n", buf);
500 fatal_msg = buf; /* to ease debugging */
507 str2shift(const char *buf)
509 const char *ends = "BKMGTPEZ";
514 for (i = 0; i < strlen(ends); i++) {
515 if (toupper(buf[0]) == ends[i])
518 if (i == strlen(ends)) {
519 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
523 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
526 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
532 nicenumtoull(const char *buf)
537 val = strtoull(buf, &end, 0);
539 (void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
541 } else if (end[0] == '.') {
542 double fval = strtod(buf, &end);
543 fval *= pow(2, str2shift(end));
544 if (fval > UINT64_MAX) {
545 (void) fprintf(stderr, "ztest: value too large: %s\n",
549 val = (uint64_t)fval;
551 int shift = str2shift(end);
552 if (shift >= 64 || (val << shift) >> shift != val) {
553 (void) fprintf(stderr, "ztest: value too large: %s\n",
563 usage(boolean_t requested)
565 const ztest_shared_opts_t *zo = &ztest_opts_defaults;
567 char nice_vdev_size[10];
568 char nice_gang_bang[10];
569 FILE *fp = requested ? stdout : stderr;
571 nicenum(zo->zo_vdev_size, nice_vdev_size);
572 nicenum(zo->zo_metaslab_gang_bang, nice_gang_bang);
574 (void) fprintf(fp, "Usage: %s\n"
575 "\t[-v vdevs (default: %llu)]\n"
576 "\t[-s size_of_each_vdev (default: %s)]\n"
577 "\t[-a alignment_shift (default: %d)] use 0 for random\n"
578 "\t[-m mirror_copies (default: %d)]\n"
579 "\t[-r raidz_disks (default: %d)]\n"
580 "\t[-R raidz_parity (default: %d)]\n"
581 "\t[-d datasets (default: %d)]\n"
582 "\t[-t threads (default: %d)]\n"
583 "\t[-g gang_block_threshold (default: %s)]\n"
584 "\t[-i init_count (default: %d)] initialize pool i times\n"
585 "\t[-k kill_percentage (default: %llu%%)]\n"
586 "\t[-p pool_name (default: %s)]\n"
587 "\t[-f dir (default: %s)] file directory for vdev files\n"
588 "\t[-V] verbose (use multiple times for ever more blather)\n"
589 "\t[-E] use existing pool instead of creating new one\n"
590 "\t[-T time (default: %llu sec)] total run time\n"
591 "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
592 "\t[-P passtime (default: %llu sec)] time per pass\n"
593 "\t[-B alt_ztest (default: <none>)] alternate ztest path\n"
594 "\t[-h] (print help)\n"
597 (u_longlong_t)zo->zo_vdevs, /* -v */
598 nice_vdev_size, /* -s */
599 zo->zo_ashift, /* -a */
600 zo->zo_mirrors, /* -m */
601 zo->zo_raidz, /* -r */
602 zo->zo_raidz_parity, /* -R */
603 zo->zo_datasets, /* -d */
604 zo->zo_threads, /* -t */
605 nice_gang_bang, /* -g */
606 zo->zo_init, /* -i */
607 (u_longlong_t)zo->zo_killrate, /* -k */
608 zo->zo_pool, /* -p */
610 (u_longlong_t)zo->zo_time, /* -T */
611 (u_longlong_t)zo->zo_maxloops, /* -F */
612 (u_longlong_t)zo->zo_passtime);
613 exit(requested ? 0 : 1);
617 process_options(int argc, char **argv)
620 ztest_shared_opts_t *zo = &ztest_opts;
624 char altdir[MAXNAMELEN] = { 0 };
626 bcopy(&ztest_opts_defaults, zo, sizeof (*zo));
628 while ((opt = getopt(argc, argv,
629 "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:hF:B:")) != EOF) {
646 value = nicenumtoull(optarg);
650 zo->zo_vdevs = value;
653 zo->zo_vdev_size = MAX(SPA_MINDEVSIZE, value);
656 zo->zo_ashift = value;
659 zo->zo_mirrors = value;
662 zo->zo_raidz = MAX(1, value);
665 zo->zo_raidz_parity = MIN(MAX(value, 1), 3);
668 zo->zo_datasets = MAX(1, value);
671 zo->zo_threads = MAX(1, value);
674 zo->zo_metaslab_gang_bang = MAX(SPA_MINBLOCKSIZE << 1,
681 zo->zo_killrate = value;
684 (void) strlcpy(zo->zo_pool, optarg,
685 sizeof (zo->zo_pool));
688 path = realpath(optarg, NULL);
690 (void) fprintf(stderr, "error: %s: %s\n",
691 optarg, strerror(errno));
694 (void) strlcpy(zo->zo_dir, path,
695 sizeof (zo->zo_dir));
708 zo->zo_passtime = MAX(1, value);
711 zo->zo_maxloops = MAX(1, value);
714 (void) strlcpy(altdir, optarg, sizeof (altdir));
726 zo->zo_raidz_parity = MIN(zo->zo_raidz_parity, zo->zo_raidz - 1);
729 (zo->zo_vdevs > 0 ? zo->zo_time * NANOSEC / zo->zo_vdevs :
732 if (strlen(altdir) > 0) {
740 cmd = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
741 realaltdir = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
743 VERIFY(NULL != realpath(getexecname(), cmd));
744 if (0 != access(altdir, F_OK)) {
745 ztest_dump_core = B_FALSE;
746 fatal(B_TRUE, "invalid alternate ztest path: %s",
749 VERIFY(NULL != realpath(altdir, realaltdir));
752 * 'cmd' should be of the form "<anything>/usr/bin/<isa>/ztest".
753 * We want to extract <isa> to determine if we should use
754 * 32 or 64 bit binaries.
756 bin = strstr(cmd, "/usr/bin/");
757 ztest = strstr(bin, "/ztest");
759 isalen = ztest - isa;
760 (void) snprintf(zo->zo_alt_ztest, sizeof (zo->zo_alt_ztest),
761 "%s/usr/bin/%.*s/ztest", realaltdir, isalen, isa);
762 (void) snprintf(zo->zo_alt_libpath, sizeof (zo->zo_alt_libpath),
763 "%s/usr/lib/%.*s", realaltdir, isalen, isa);
765 if (0 != access(zo->zo_alt_ztest, X_OK)) {
766 ztest_dump_core = B_FALSE;
767 fatal(B_TRUE, "invalid alternate ztest: %s",
769 } else if (0 != access(zo->zo_alt_libpath, X_OK)) {
770 ztest_dump_core = B_FALSE;
771 fatal(B_TRUE, "invalid alternate lib directory %s",
775 umem_free(cmd, MAXPATHLEN);
776 umem_free(realaltdir, MAXPATHLEN);
781 ztest_kill(ztest_shared_t *zs)
783 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(ztest_spa));
784 zs->zs_space = metaslab_class_get_space(spa_normal_class(ztest_spa));
785 (void) kill(getpid(), SIGKILL);
789 ztest_random(uint64_t range)
793 ASSERT3S(ztest_fd_rand, >=, 0);
798 if (read(ztest_fd_rand, &r, sizeof (r)) != sizeof (r))
799 fatal(1, "short read from /dev/urandom");
806 ztest_record_enospc(const char *s)
808 ztest_shared->zs_enospc_count++;
812 ztest_get_ashift(void)
814 if (ztest_opts.zo_ashift == 0)
815 return (SPA_MINBLOCKSHIFT + ztest_random(3));
816 return (ztest_opts.zo_ashift);
820 make_vdev_file(char *path, char *aux, size_t size, uint64_t ashift)
826 pathbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
829 ashift = ztest_get_ashift();
835 vdev = ztest_shared->zs_vdev_aux;
836 (void) snprintf(path, MAXPATHLEN,
837 ztest_aux_template, ztest_opts.zo_dir,
838 ztest_opts.zo_pool, aux, vdev);
840 vdev = ztest_shared->zs_vdev_next_leaf++;
841 (void) snprintf(path, MAXPATHLEN,
842 ztest_dev_template, ztest_opts.zo_dir,
843 ztest_opts.zo_pool, vdev);
848 int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
850 fatal(1, "can't open %s", path);
851 if (ftruncate(fd, size) != 0)
852 fatal(1, "can't ftruncate %s", path);
856 VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
857 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
858 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
859 VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
860 umem_free(pathbuf, MAXPATHLEN);
866 make_vdev_raidz(char *path, char *aux, size_t size, uint64_t ashift, int r)
868 nvlist_t *raidz, **child;
872 return (make_vdev_file(path, aux, size, ashift));
873 child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
875 for (c = 0; c < r; c++)
876 child[c] = make_vdev_file(path, aux, size, ashift);
878 VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
879 VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
880 VDEV_TYPE_RAIDZ) == 0);
881 VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
882 ztest_opts.zo_raidz_parity) == 0);
883 VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
886 for (c = 0; c < r; c++)
887 nvlist_free(child[c]);
889 umem_free(child, r * sizeof (nvlist_t *));
895 make_vdev_mirror(char *path, char *aux, size_t size, uint64_t ashift,
898 nvlist_t *mirror, **child;
902 return (make_vdev_raidz(path, aux, size, ashift, r));
904 child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
906 for (c = 0; c < m; c++)
907 child[c] = make_vdev_raidz(path, aux, size, ashift, r);
909 VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
910 VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
911 VDEV_TYPE_MIRROR) == 0);
912 VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
915 for (c = 0; c < m; c++)
916 nvlist_free(child[c]);
918 umem_free(child, m * sizeof (nvlist_t *));
924 make_vdev_root(char *path, char *aux, size_t size, uint64_t ashift,
925 int log, int r, int m, int t)
927 nvlist_t *root, **child;
932 child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
934 for (c = 0; c < t; c++) {
935 child[c] = make_vdev_mirror(path, aux, size, ashift, r, m);
936 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
940 VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
941 VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
942 VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
945 for (c = 0; c < t; c++)
946 nvlist_free(child[c]);
948 umem_free(child, t * sizeof (nvlist_t *));
954 ztest_random_blocksize(void)
956 return (1 << (SPA_MINBLOCKSHIFT +
957 ztest_random(SPA_MAXBLOCKSHIFT - SPA_MINBLOCKSHIFT + 1)));
961 ztest_random_ibshift(void)
963 return (DN_MIN_INDBLKSHIFT +
964 ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1));
968 ztest_random_vdev_top(spa_t *spa, boolean_t log_ok)
971 vdev_t *rvd = spa->spa_root_vdev;
974 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
977 top = ztest_random(rvd->vdev_children);
978 tvd = rvd->vdev_child[top];
979 } while (tvd->vdev_ishole || (tvd->vdev_islog && !log_ok) ||
980 tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL);
986 ztest_random_dsl_prop(zfs_prop_t prop)
991 value = zfs_prop_random_value(prop, ztest_random(-1ULL));
992 } while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF);
998 ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value,
1001 const char *propname = zfs_prop_to_name(prop);
1002 const char *valname;
1007 error = dsl_prop_set(osname, propname,
1008 (inherit ? ZPROP_SRC_NONE : ZPROP_SRC_LOCAL),
1009 sizeof (value), 1, &value);
1011 if (error == ENOSPC) {
1012 ztest_record_enospc(FTAG);
1015 ASSERT3U(error, ==, 0);
1017 setpoint = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
1018 VERIFY3U(dsl_prop_get(osname, propname, sizeof (curval),
1019 1, &curval, setpoint), ==, 0);
1021 if (ztest_opts.zo_verbose >= 6) {
1022 VERIFY(zfs_prop_index_to_string(prop, curval, &valname) == 0);
1023 (void) printf("%s %s = %s at '%s'\n",
1024 osname, propname, valname, setpoint);
1026 umem_free(setpoint, MAXPATHLEN);
1032 ztest_spa_prop_set_uint64(zpool_prop_t prop, uint64_t value)
1034 spa_t *spa = ztest_spa;
1035 nvlist_t *props = NULL;
1038 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
1039 VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0);
1041 error = spa_prop_set(spa, props);
1045 if (error == ENOSPC) {
1046 ztest_record_enospc(FTAG);
1049 ASSERT3U(error, ==, 0);
1055 ztest_rll_init(rll_t *rll)
1057 rll->rll_writer = NULL;
1058 rll->rll_readers = 0;
1059 mutex_init(&rll->rll_lock, NULL, MUTEX_DEFAULT, NULL);
1060 cv_init(&rll->rll_cv, NULL, CV_DEFAULT, NULL);
1064 ztest_rll_destroy(rll_t *rll)
1066 ASSERT(rll->rll_writer == NULL);
1067 ASSERT(rll->rll_readers == 0);
1068 mutex_destroy(&rll->rll_lock);
1069 cv_destroy(&rll->rll_cv);
1073 ztest_rll_lock(rll_t *rll, rl_type_t type)
1075 mutex_enter(&rll->rll_lock);
1077 if (type == RL_READER) {
1078 while (rll->rll_writer != NULL)
1079 (void) cv_wait(&rll->rll_cv, &rll->rll_lock);
1082 while (rll->rll_writer != NULL || rll->rll_readers)
1083 (void) cv_wait(&rll->rll_cv, &rll->rll_lock);
1084 rll->rll_writer = curthread;
1087 mutex_exit(&rll->rll_lock);
1091 ztest_rll_unlock(rll_t *rll)
1093 mutex_enter(&rll->rll_lock);
1095 if (rll->rll_writer) {
1096 ASSERT(rll->rll_readers == 0);
1097 rll->rll_writer = NULL;
1099 ASSERT(rll->rll_readers != 0);
1100 ASSERT(rll->rll_writer == NULL);
1104 if (rll->rll_writer == NULL && rll->rll_readers == 0)
1105 cv_broadcast(&rll->rll_cv);
1107 mutex_exit(&rll->rll_lock);
1111 ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type)
1113 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1115 ztest_rll_lock(rll, type);
1119 ztest_object_unlock(ztest_ds_t *zd, uint64_t object)
1121 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1123 ztest_rll_unlock(rll);
1127 ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset,
1128 uint64_t size, rl_type_t type)
1130 uint64_t hash = object ^ (offset % (ZTEST_RANGE_LOCKS + 1));
1131 rll_t *rll = &zd->zd_range_lock[hash & (ZTEST_RANGE_LOCKS - 1)];
1134 rl = umem_alloc(sizeof (*rl), UMEM_NOFAIL);
1135 rl->rl_object = object;
1136 rl->rl_offset = offset;
1140 ztest_rll_lock(rll, type);
1146 ztest_range_unlock(rl_t *rl)
1148 rll_t *rll = rl->rl_lock;
1150 ztest_rll_unlock(rll);
1152 umem_free(rl, sizeof (*rl));
1156 ztest_zd_init(ztest_ds_t *zd, ztest_shared_ds_t *szd, objset_t *os)
1159 zd->zd_zilog = dmu_objset_zil(os);
1160 zd->zd_shared = szd;
1161 dmu_objset_name(os, zd->zd_name);
1164 if (zd->zd_shared != NULL)
1165 zd->zd_shared->zd_seq = 0;
1167 rw_init(&zd->zd_zilog_lock, NULL, RW_DEFAULT, NULL);
1168 mutex_init(&zd->zd_dirobj_lock, NULL, MUTEX_DEFAULT, NULL);
1170 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1171 ztest_rll_init(&zd->zd_object_lock[l]);
1173 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
1174 ztest_rll_init(&zd->zd_range_lock[l]);
1178 ztest_zd_fini(ztest_ds_t *zd)
1182 mutex_destroy(&zd->zd_dirobj_lock);
1183 rw_destroy(&zd->zd_zilog_lock);
1185 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1186 ztest_rll_destroy(&zd->zd_object_lock[l]);
1188 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
1189 ztest_rll_destroy(&zd->zd_range_lock[l]);
1192 #define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1195 ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag)
1201 * Attempt to assign tx to some transaction group.
1203 error = dmu_tx_assign(tx, txg_how);
1205 if (error == ERESTART) {
1206 ASSERT(txg_how == TXG_NOWAIT);
1209 ASSERT3U(error, ==, ENOSPC);
1210 ztest_record_enospc(tag);
1215 txg = dmu_tx_get_txg(tx);
1221 ztest_pattern_set(void *buf, uint64_t size, uint64_t value)
1224 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1232 ztest_pattern_match(void *buf, uint64_t size, uint64_t value)
1235 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1239 diff |= (value - *ip++);
1246 ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1247 uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
1249 bt->bt_magic = BT_MAGIC;
1250 bt->bt_objset = dmu_objset_id(os);
1251 bt->bt_object = object;
1252 bt->bt_offset = offset;
1255 bt->bt_crtxg = crtxg;
1259 ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1260 uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
1262 ASSERT(bt->bt_magic == BT_MAGIC);
1263 ASSERT(bt->bt_objset == dmu_objset_id(os));
1264 ASSERT(bt->bt_object == object);
1265 ASSERT(bt->bt_offset == offset);
1266 ASSERT(bt->bt_gen <= gen);
1267 ASSERT(bt->bt_txg <= txg);
1268 ASSERT(bt->bt_crtxg == crtxg);
1271 static ztest_block_tag_t *
1272 ztest_bt_bonus(dmu_buf_t *db)
1274 dmu_object_info_t doi;
1275 ztest_block_tag_t *bt;
1277 dmu_object_info_from_db(db, &doi);
1278 ASSERT3U(doi.doi_bonus_size, <=, db->db_size);
1279 ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt));
1280 bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt));
1289 #define lrz_type lr_mode
1290 #define lrz_blocksize lr_uid
1291 #define lrz_ibshift lr_gid
1292 #define lrz_bonustype lr_rdev
1293 #define lrz_bonuslen lr_crtime[1]
1296 ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr)
1298 char *name = (void *)(lr + 1); /* name follows lr */
1299 size_t namesize = strlen(name) + 1;
1302 if (zil_replaying(zd->zd_zilog, tx))
1305 itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize);
1306 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1307 sizeof (*lr) + namesize - sizeof (lr_t));
1309 zil_itx_assign(zd->zd_zilog, itx, tx);
1313 ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr, uint64_t object)
1315 char *name = (void *)(lr + 1); /* name follows lr */
1316 size_t namesize = strlen(name) + 1;
1319 if (zil_replaying(zd->zd_zilog, tx))
1322 itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize);
1323 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1324 sizeof (*lr) + namesize - sizeof (lr_t));
1326 itx->itx_oid = object;
1327 zil_itx_assign(zd->zd_zilog, itx, tx);
1331 ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr)
1334 itx_wr_state_t write_state = ztest_random(WR_NUM_STATES);
1336 if (zil_replaying(zd->zd_zilog, tx))
1339 if (lr->lr_length > ZIL_MAX_LOG_DATA)
1340 write_state = WR_INDIRECT;
1342 itx = zil_itx_create(TX_WRITE,
1343 sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0));
1345 if (write_state == WR_COPIED &&
1346 dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length,
1347 ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) {
1348 zil_itx_destroy(itx);
1349 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1350 write_state = WR_NEED_COPY;
1352 itx->itx_private = zd;
1353 itx->itx_wr_state = write_state;
1354 itx->itx_sync = (ztest_random(8) == 0);
1355 itx->itx_sod += (write_state == WR_NEED_COPY ? lr->lr_length : 0);
1357 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1358 sizeof (*lr) - sizeof (lr_t));
1360 zil_itx_assign(zd->zd_zilog, itx, tx);
1364 ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr)
1368 if (zil_replaying(zd->zd_zilog, tx))
1371 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1372 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1373 sizeof (*lr) - sizeof (lr_t));
1375 itx->itx_sync = B_FALSE;
1376 zil_itx_assign(zd->zd_zilog, itx, tx);
1380 ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr)
1384 if (zil_replaying(zd->zd_zilog, tx))
1387 itx = zil_itx_create(TX_SETATTR, sizeof (*lr));
1388 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1389 sizeof (*lr) - sizeof (lr_t));
1391 itx->itx_sync = B_FALSE;
1392 zil_itx_assign(zd->zd_zilog, itx, tx);
1399 ztest_replay_create(ztest_ds_t *zd, lr_create_t *lr, boolean_t byteswap)
1401 char *name = (void *)(lr + 1); /* name follows lr */
1402 objset_t *os = zd->zd_os;
1403 ztest_block_tag_t *bbt;
1410 byteswap_uint64_array(lr, sizeof (*lr));
1412 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1413 ASSERT(name[0] != '\0');
1415 tx = dmu_tx_create(os);
1417 dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name);
1419 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1420 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1422 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1425 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1429 ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid);
1431 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1432 if (lr->lr_foid == 0) {
1433 lr->lr_foid = zap_create(os,
1434 lr->lrz_type, lr->lrz_bonustype,
1435 lr->lrz_bonuslen, tx);
1437 error = zap_create_claim(os, lr->lr_foid,
1438 lr->lrz_type, lr->lrz_bonustype,
1439 lr->lrz_bonuslen, tx);
1442 if (lr->lr_foid == 0) {
1443 lr->lr_foid = dmu_object_alloc(os,
1444 lr->lrz_type, 0, lr->lrz_bonustype,
1445 lr->lrz_bonuslen, tx);
1447 error = dmu_object_claim(os, lr->lr_foid,
1448 lr->lrz_type, 0, lr->lrz_bonustype,
1449 lr->lrz_bonuslen, tx);
1454 ASSERT3U(error, ==, EEXIST);
1455 ASSERT(zd->zd_zilog->zl_replay);
1460 ASSERT(lr->lr_foid != 0);
1462 if (lr->lrz_type != DMU_OT_ZAP_OTHER)
1463 VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid,
1464 lr->lrz_blocksize, lr->lrz_ibshift, tx));
1466 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1467 bbt = ztest_bt_bonus(db);
1468 dmu_buf_will_dirty(db, tx);
1469 ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_gen, txg, txg);
1470 dmu_buf_rele(db, FTAG);
1472 VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1,
1475 (void) ztest_log_create(zd, tx, lr);
1483 ztest_replay_remove(ztest_ds_t *zd, lr_remove_t *lr, boolean_t byteswap)
1485 char *name = (void *)(lr + 1); /* name follows lr */
1486 objset_t *os = zd->zd_os;
1487 dmu_object_info_t doi;
1489 uint64_t object, txg;
1492 byteswap_uint64_array(lr, sizeof (*lr));
1494 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1495 ASSERT(name[0] != '\0');
1498 zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object));
1499 ASSERT(object != 0);
1501 ztest_object_lock(zd, object, RL_WRITER);
1503 VERIFY3U(0, ==, dmu_object_info(os, object, &doi));
1505 tx = dmu_tx_create(os);
1507 dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name);
1508 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1510 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1512 ztest_object_unlock(zd, object);
1516 if (doi.doi_type == DMU_OT_ZAP_OTHER) {
1517 VERIFY3U(0, ==, zap_destroy(os, object, tx));
1519 VERIFY3U(0, ==, dmu_object_free(os, object, tx));
1522 VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx));
1524 (void) ztest_log_remove(zd, tx, lr, object);
1528 ztest_object_unlock(zd, object);
1534 ztest_replay_write(ztest_ds_t *zd, lr_write_t *lr, boolean_t byteswap)
1536 objset_t *os = zd->zd_os;
1537 void *data = lr + 1; /* data follows lr */
1538 uint64_t offset, length;
1539 ztest_block_tag_t *bt = data;
1540 ztest_block_tag_t *bbt;
1541 uint64_t gen, txg, lrtxg, crtxg;
1542 dmu_object_info_t doi;
1545 arc_buf_t *abuf = NULL;
1549 byteswap_uint64_array(lr, sizeof (*lr));
1551 offset = lr->lr_offset;
1552 length = lr->lr_length;
1554 /* If it's a dmu_sync() block, write the whole block */
1555 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
1556 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
1557 if (length < blocksize) {
1558 offset -= offset % blocksize;
1563 if (bt->bt_magic == BSWAP_64(BT_MAGIC))
1564 byteswap_uint64_array(bt, sizeof (*bt));
1566 if (bt->bt_magic != BT_MAGIC)
1569 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1570 rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER);
1572 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1574 dmu_object_info_from_db(db, &doi);
1576 bbt = ztest_bt_bonus(db);
1577 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1579 crtxg = bbt->bt_crtxg;
1580 lrtxg = lr->lr_common.lrc_txg;
1582 tx = dmu_tx_create(os);
1584 dmu_tx_hold_write(tx, lr->lr_foid, offset, length);
1586 if (ztest_random(8) == 0 && length == doi.doi_data_block_size &&
1587 P2PHASE(offset, length) == 0)
1588 abuf = dmu_request_arcbuf(db, length);
1590 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1593 dmu_return_arcbuf(abuf);
1594 dmu_buf_rele(db, FTAG);
1595 ztest_range_unlock(rl);
1596 ztest_object_unlock(zd, lr->lr_foid);
1602 * Usually, verify the old data before writing new data --
1603 * but not always, because we also want to verify correct
1604 * behavior when the data was not recently read into cache.
1606 ASSERT(offset % doi.doi_data_block_size == 0);
1607 if (ztest_random(4) != 0) {
1608 int prefetch = ztest_random(2) ?
1609 DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
1610 ztest_block_tag_t rbt;
1612 VERIFY(dmu_read(os, lr->lr_foid, offset,
1613 sizeof (rbt), &rbt, prefetch) == 0);
1614 if (rbt.bt_magic == BT_MAGIC) {
1615 ztest_bt_verify(&rbt, os, lr->lr_foid,
1616 offset, gen, txg, crtxg);
1621 * Writes can appear to be newer than the bonus buffer because
1622 * the ztest_get_data() callback does a dmu_read() of the
1623 * open-context data, which may be different than the data
1624 * as it was when the write was generated.
1626 if (zd->zd_zilog->zl_replay) {
1627 ztest_bt_verify(bt, os, lr->lr_foid, offset,
1628 MAX(gen, bt->bt_gen), MAX(txg, lrtxg),
1633 * Set the bt's gen/txg to the bonus buffer's gen/txg
1634 * so that all of the usual ASSERTs will work.
1636 ztest_bt_generate(bt, os, lr->lr_foid, offset, gen, txg, crtxg);
1640 dmu_write(os, lr->lr_foid, offset, length, data, tx);
1642 bcopy(data, abuf->b_data, length);
1643 dmu_assign_arcbuf(db, offset, abuf, tx);
1646 (void) ztest_log_write(zd, tx, lr);
1648 dmu_buf_rele(db, FTAG);
1652 ztest_range_unlock(rl);
1653 ztest_object_unlock(zd, lr->lr_foid);
1659 ztest_replay_truncate(ztest_ds_t *zd, lr_truncate_t *lr, boolean_t byteswap)
1661 objset_t *os = zd->zd_os;
1667 byteswap_uint64_array(lr, sizeof (*lr));
1669 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1670 rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length,
1673 tx = dmu_tx_create(os);
1675 dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length);
1677 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1679 ztest_range_unlock(rl);
1680 ztest_object_unlock(zd, lr->lr_foid);
1684 VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset,
1685 lr->lr_length, tx) == 0);
1687 (void) ztest_log_truncate(zd, tx, lr);
1691 ztest_range_unlock(rl);
1692 ztest_object_unlock(zd, lr->lr_foid);
1698 ztest_replay_setattr(ztest_ds_t *zd, lr_setattr_t *lr, boolean_t byteswap)
1700 objset_t *os = zd->zd_os;
1703 ztest_block_tag_t *bbt;
1704 uint64_t txg, lrtxg, crtxg;
1707 byteswap_uint64_array(lr, sizeof (*lr));
1709 ztest_object_lock(zd, lr->lr_foid, RL_WRITER);
1711 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1713 tx = dmu_tx_create(os);
1714 dmu_tx_hold_bonus(tx, lr->lr_foid);
1716 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1718 dmu_buf_rele(db, FTAG);
1719 ztest_object_unlock(zd, lr->lr_foid);
1723 bbt = ztest_bt_bonus(db);
1724 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1725 crtxg = bbt->bt_crtxg;
1726 lrtxg = lr->lr_common.lrc_txg;
1728 if (zd->zd_zilog->zl_replay) {
1729 ASSERT(lr->lr_size != 0);
1730 ASSERT(lr->lr_mode != 0);
1734 * Randomly change the size and increment the generation.
1736 lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) *
1738 lr->lr_mode = bbt->bt_gen + 1;
1743 * Verify that the current bonus buffer is not newer than our txg.
1745 ztest_bt_verify(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode,
1746 MAX(txg, lrtxg), crtxg);
1748 dmu_buf_will_dirty(db, tx);
1750 ASSERT3U(lr->lr_size, >=, sizeof (*bbt));
1751 ASSERT3U(lr->lr_size, <=, db->db_size);
1752 VERIFY3U(dmu_set_bonus(db, lr->lr_size, tx), ==, 0);
1753 bbt = ztest_bt_bonus(db);
1755 ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode, txg, crtxg);
1757 dmu_buf_rele(db, FTAG);
1759 (void) ztest_log_setattr(zd, tx, lr);
1763 ztest_object_unlock(zd, lr->lr_foid);
1768 zil_replay_func_t *ztest_replay_vector[TX_MAX_TYPE] = {
1769 NULL, /* 0 no such transaction type */
1770 (zil_replay_func_t *)ztest_replay_create, /* TX_CREATE */
1771 NULL, /* TX_MKDIR */
1772 NULL, /* TX_MKXATTR */
1773 NULL, /* TX_SYMLINK */
1774 (zil_replay_func_t *)ztest_replay_remove, /* TX_REMOVE */
1775 NULL, /* TX_RMDIR */
1777 NULL, /* TX_RENAME */
1778 (zil_replay_func_t *)ztest_replay_write, /* TX_WRITE */
1779 (zil_replay_func_t *)ztest_replay_truncate, /* TX_TRUNCATE */
1780 (zil_replay_func_t *)ztest_replay_setattr, /* TX_SETATTR */
1782 NULL, /* TX_CREATE_ACL */
1783 NULL, /* TX_CREATE_ATTR */
1784 NULL, /* TX_CREATE_ACL_ATTR */
1785 NULL, /* TX_MKDIR_ACL */
1786 NULL, /* TX_MKDIR_ATTR */
1787 NULL, /* TX_MKDIR_ACL_ATTR */
1788 NULL, /* TX_WRITE2 */
1792 * ZIL get_data callbacks
1796 ztest_get_done(zgd_t *zgd, int error)
1798 ztest_ds_t *zd = zgd->zgd_private;
1799 uint64_t object = zgd->zgd_rl->rl_object;
1802 dmu_buf_rele(zgd->zgd_db, zgd);
1804 ztest_range_unlock(zgd->zgd_rl);
1805 ztest_object_unlock(zd, object);
1807 if (error == 0 && zgd->zgd_bp)
1808 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1810 umem_free(zgd, sizeof (*zgd));
1814 ztest_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1816 ztest_ds_t *zd = arg;
1817 objset_t *os = zd->zd_os;
1818 uint64_t object = lr->lr_foid;
1819 uint64_t offset = lr->lr_offset;
1820 uint64_t size = lr->lr_length;
1821 blkptr_t *bp = &lr->lr_blkptr;
1822 uint64_t txg = lr->lr_common.lrc_txg;
1824 dmu_object_info_t doi;
1829 ztest_object_lock(zd, object, RL_READER);
1830 error = dmu_bonus_hold(os, object, FTAG, &db);
1832 ztest_object_unlock(zd, object);
1836 crtxg = ztest_bt_bonus(db)->bt_crtxg;
1838 if (crtxg == 0 || crtxg > txg) {
1839 dmu_buf_rele(db, FTAG);
1840 ztest_object_unlock(zd, object);
1844 dmu_object_info_from_db(db, &doi);
1845 dmu_buf_rele(db, FTAG);
1848 zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL);
1849 zgd->zgd_zilog = zd->zd_zilog;
1850 zgd->zgd_private = zd;
1852 if (buf != NULL) { /* immediate write */
1853 zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
1856 error = dmu_read(os, object, offset, size, buf,
1857 DMU_READ_NO_PREFETCH);
1860 size = doi.doi_data_block_size;
1862 offset = P2ALIGN(offset, size);
1864 ASSERT(offset < size);
1868 zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
1871 error = dmu_buf_hold(os, object, offset, zgd, &db,
1872 DMU_READ_NO_PREFETCH);
1878 ASSERT(db->db_offset == offset);
1879 ASSERT(db->db_size == size);
1881 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1882 ztest_get_done, zgd);
1889 ztest_get_done(zgd, error);
1895 ztest_lr_alloc(size_t lrsize, char *name)
1898 size_t namesize = name ? strlen(name) + 1 : 0;
1900 lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
1903 bcopy(name, lr + lrsize, namesize);
1909 ztest_lr_free(void *lr, size_t lrsize, char *name)
1911 size_t namesize = name ? strlen(name) + 1 : 0;
1913 umem_free(lr, lrsize + namesize);
1917 * Lookup a bunch of objects. Returns the number of objects not found.
1920 ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
1926 ASSERT(mutex_held(&zd->zd_dirobj_lock));
1928 for (i = 0; i < count; i++, od++) {
1930 error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
1931 sizeof (uint64_t), 1, &od->od_object);
1933 ASSERT(error == ENOENT);
1934 ASSERT(od->od_object == 0);
1938 ztest_block_tag_t *bbt;
1939 dmu_object_info_t doi;
1941 ASSERT(od->od_object != 0);
1942 ASSERT(missing == 0); /* there should be no gaps */
1944 ztest_object_lock(zd, od->od_object, RL_READER);
1945 VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
1946 od->od_object, FTAG, &db));
1947 dmu_object_info_from_db(db, &doi);
1948 bbt = ztest_bt_bonus(db);
1949 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1950 od->od_type = doi.doi_type;
1951 od->od_blocksize = doi.doi_data_block_size;
1952 od->od_gen = bbt->bt_gen;
1953 dmu_buf_rele(db, FTAG);
1954 ztest_object_unlock(zd, od->od_object);
1962 ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
1967 ASSERT(mutex_held(&zd->zd_dirobj_lock));
1969 for (i = 0; i < count; i++, od++) {
1976 lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
1978 lr->lr_doid = od->od_dir;
1979 lr->lr_foid = 0; /* 0 to allocate, > 0 to claim */
1980 lr->lrz_type = od->od_crtype;
1981 lr->lrz_blocksize = od->od_crblocksize;
1982 lr->lrz_ibshift = ztest_random_ibshift();
1983 lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
1984 lr->lrz_bonuslen = dmu_bonus_max();
1985 lr->lr_gen = od->od_crgen;
1986 lr->lr_crtime[0] = time(NULL);
1988 if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
1989 ASSERT(missing == 0);
1993 od->od_object = lr->lr_foid;
1994 od->od_type = od->od_crtype;
1995 od->od_blocksize = od->od_crblocksize;
1996 od->od_gen = od->od_crgen;
1997 ASSERT(od->od_object != 0);
2000 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2007 ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
2013 ASSERT(mutex_held(&zd->zd_dirobj_lock));
2017 for (i = count - 1; i >= 0; i--, od--) {
2023 if (od->od_object == 0)
2026 lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2028 lr->lr_doid = od->od_dir;
2030 if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
2031 ASSERT3U(error, ==, ENOSPC);
2036 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2043 ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
2049 lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
2051 lr->lr_foid = object;
2052 lr->lr_offset = offset;
2053 lr->lr_length = size;
2055 BP_ZERO(&lr->lr_blkptr);
2057 bcopy(data, lr + 1, size);
2059 error = ztest_replay_write(zd, lr, B_FALSE);
2061 ztest_lr_free(lr, sizeof (*lr) + size, NULL);
2067 ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2072 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2074 lr->lr_foid = object;
2075 lr->lr_offset = offset;
2076 lr->lr_length = size;
2078 error = ztest_replay_truncate(zd, lr, B_FALSE);
2080 ztest_lr_free(lr, sizeof (*lr), NULL);
2086 ztest_setattr(ztest_ds_t *zd, uint64_t object)
2091 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2093 lr->lr_foid = object;
2097 error = ztest_replay_setattr(zd, lr, B_FALSE);
2099 ztest_lr_free(lr, sizeof (*lr), NULL);
2105 ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2107 objset_t *os = zd->zd_os;
2112 txg_wait_synced(dmu_objset_pool(os), 0);
2114 ztest_object_lock(zd, object, RL_READER);
2115 rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
2117 tx = dmu_tx_create(os);
2119 dmu_tx_hold_write(tx, object, offset, size);
2121 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
2124 dmu_prealloc(os, object, offset, size, tx);
2126 txg_wait_synced(dmu_objset_pool(os), txg);
2128 (void) dmu_free_long_range(os, object, offset, size);
2131 ztest_range_unlock(rl);
2132 ztest_object_unlock(zd, object);
2136 ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
2138 ztest_block_tag_t wbt;
2139 dmu_object_info_t doi;
2140 enum ztest_io_type io_type;
2144 VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
2145 blocksize = doi.doi_data_block_size;
2146 data = umem_alloc(blocksize, UMEM_NOFAIL);
2149 * Pick an i/o type at random, biased toward writing block tags.
2151 io_type = ztest_random(ZTEST_IO_TYPES);
2152 if (ztest_random(2) == 0)
2153 io_type = ZTEST_IO_WRITE_TAG;
2155 (void) rw_enter(&zd->zd_zilog_lock, RW_READER);
2159 case ZTEST_IO_WRITE_TAG:
2160 ztest_bt_generate(&wbt, zd->zd_os, object, offset, 0, 0, 0);
2161 (void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
2164 case ZTEST_IO_WRITE_PATTERN:
2165 (void) memset(data, 'a' + (object + offset) % 5, blocksize);
2166 if (ztest_random(2) == 0) {
2168 * Induce fletcher2 collisions to ensure that
2169 * zio_ddt_collision() detects and resolves them
2170 * when using fletcher2-verify for deduplication.
2172 ((uint64_t *)data)[0] ^= 1ULL << 63;
2173 ((uint64_t *)data)[4] ^= 1ULL << 63;
2175 (void) ztest_write(zd, object, offset, blocksize, data);
2178 case ZTEST_IO_WRITE_ZEROES:
2179 bzero(data, blocksize);
2180 (void) ztest_write(zd, object, offset, blocksize, data);
2183 case ZTEST_IO_TRUNCATE:
2184 (void) ztest_truncate(zd, object, offset, blocksize);
2187 case ZTEST_IO_SETATTR:
2188 (void) ztest_setattr(zd, object);
2194 (void) rw_exit(&zd->zd_zilog_lock);
2196 umem_free(data, blocksize);
2200 * Initialize an object description template.
2203 ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
2204 dmu_object_type_t type, uint64_t blocksize, uint64_t gen)
2206 od->od_dir = ZTEST_DIROBJ;
2209 od->od_crtype = type;
2210 od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
2213 od->od_type = DMU_OT_NONE;
2214 od->od_blocksize = 0;
2217 (void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
2218 tag, (longlong_t)id, (u_longlong_t)index);
2222 * Lookup or create the objects for a test using the od template.
2223 * If the objects do not all exist, or if 'remove' is specified,
2224 * remove any existing objects and create new ones. Otherwise,
2225 * use the existing objects.
2228 ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
2230 int count = size / sizeof (*od);
2233 mutex_enter(&zd->zd_dirobj_lock);
2234 if ((ztest_lookup(zd, od, count) != 0 || remove) &&
2235 (ztest_remove(zd, od, count) != 0 ||
2236 ztest_create(zd, od, count) != 0))
2239 mutex_exit(&zd->zd_dirobj_lock);
2246 ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
2248 zilog_t *zilog = zd->zd_zilog;
2250 (void) rw_enter(&zd->zd_zilog_lock, RW_READER);
2252 zil_commit(zilog, ztest_random(ZTEST_OBJECTS));
2255 * Remember the committed values in zd, which is in parent/child
2256 * shared memory. If we die, the next iteration of ztest_run()
2257 * will verify that the log really does contain this record.
2259 mutex_enter(&zilog->zl_lock);
2260 ASSERT(zd->zd_shared != NULL);
2261 ASSERT3U(zd->zd_shared->zd_seq, <=, zilog->zl_commit_lr_seq);
2262 zd->zd_shared->zd_seq = zilog->zl_commit_lr_seq;
2263 mutex_exit(&zilog->zl_lock);
2265 (void) rw_exit(&zd->zd_zilog_lock);
2269 * This function is designed to simulate the operations that occur during a
2270 * mount/unmount operation. We hold the dataset across these operations in an
2271 * attempt to expose any implicit assumptions about ZIL management.
2275 ztest_zil_remount(ztest_ds_t *zd, uint64_t id)
2277 objset_t *os = zd->zd_os;
2279 (void) rw_enter(&zd->zd_zilog_lock, RW_WRITER);
2281 /* zfs_sb_teardown() */
2282 zil_close(zd->zd_zilog);
2284 /* zfsvfs_setup() */
2285 VERIFY(zil_open(os, ztest_get_data) == zd->zd_zilog);
2286 zil_replay(os, zd, ztest_replay_vector);
2288 (void) rw_exit(&zd->zd_zilog_lock);
2292 * Verify that we can't destroy an active pool, create an existing pool,
2293 * or create a pool with a bad vdev spec.
2297 ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2299 ztest_shared_opts_t *zo = &ztest_opts;
2304 * Attempt to create using a bad file.
2306 nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
2307 VERIFY3U(ENOENT, ==,
2308 spa_create("ztest_bad_file", nvroot, NULL, NULL, NULL));
2309 nvlist_free(nvroot);
2312 * Attempt to create using a bad mirror.
2314 nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 2, 1);
2315 VERIFY3U(ENOENT, ==,
2316 spa_create("ztest_bad_mirror", nvroot, NULL, NULL, NULL));
2317 nvlist_free(nvroot);
2320 * Attempt to create an existing pool. It shouldn't matter
2321 * what's in the nvroot; we should fail with EEXIST.
2323 (void) rw_enter(&ztest_name_lock, RW_READER);
2324 nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
2325 VERIFY3U(EEXIST, ==, spa_create(zo->zo_pool, nvroot, NULL, NULL, NULL));
2326 nvlist_free(nvroot);
2327 VERIFY3U(0, ==, spa_open(zo->zo_pool, &spa, FTAG));
2328 VERIFY3U(EBUSY, ==, spa_destroy(zo->zo_pool));
2329 spa_close(spa, FTAG);
2331 (void) rw_exit(&ztest_name_lock);
2335 vdev_lookup_by_path(vdev_t *vd, const char *path)
2340 if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
2343 for (c = 0; c < vd->vdev_children; c++)
2344 if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
2352 * Find the first available hole which can be used as a top-level.
2355 find_vdev_hole(spa_t *spa)
2357 vdev_t *rvd = spa->spa_root_vdev;
2360 ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
2362 for (c = 0; c < rvd->vdev_children; c++) {
2363 vdev_t *cvd = rvd->vdev_child[c];
2365 if (cvd->vdev_ishole)
2372 * Verify that vdev_add() works as expected.
2376 ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2378 ztest_shared_t *zs = ztest_shared;
2379 spa_t *spa = ztest_spa;
2385 mutex_enter(&ztest_vdev_lock);
2387 MAX(zs->zs_mirrors + zs->zs_splits, 1) * ztest_opts.zo_raidz;
2389 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2391 ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2394 * If we have slogs then remove them 1/4 of the time.
2396 if (spa_has_slogs(spa) && ztest_random(4) == 0) {
2398 * Grab the guid from the head of the log class rotor.
2400 guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid;
2402 spa_config_exit(spa, SCL_VDEV, FTAG);
2405 * We have to grab the zs_name_lock as writer to
2406 * prevent a race between removing a slog (dmu_objset_find)
2407 * and destroying a dataset. Removing the slog will
2408 * grab a reference on the dataset which may cause
2409 * dmu_objset_destroy() to fail with EBUSY thus
2410 * leaving the dataset in an inconsistent state.
2412 rw_enter(&ztest_name_lock, RW_WRITER);
2413 error = spa_vdev_remove(spa, guid, B_FALSE);
2414 rw_exit(&ztest_name_lock);
2416 if (error && error != EEXIST)
2417 fatal(0, "spa_vdev_remove() = %d", error);
2419 spa_config_exit(spa, SCL_VDEV, FTAG);
2422 * Make 1/4 of the devices be log devices.
2424 nvroot = make_vdev_root(NULL, NULL,
2425 ztest_opts.zo_vdev_size, 0,
2426 ztest_random(4) == 0, ztest_opts.zo_raidz,
2429 error = spa_vdev_add(spa, nvroot);
2430 nvlist_free(nvroot);
2432 if (error == ENOSPC)
2433 ztest_record_enospc("spa_vdev_add");
2434 else if (error != 0)
2435 fatal(0, "spa_vdev_add() = %d", error);
2438 mutex_exit(&ztest_vdev_lock);
2442 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
2446 ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
2448 ztest_shared_t *zs = ztest_shared;
2449 spa_t *spa = ztest_spa;
2450 vdev_t *rvd = spa->spa_root_vdev;
2451 spa_aux_vdev_t *sav;
2457 path = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2459 if (ztest_random(2) == 0) {
2460 sav = &spa->spa_spares;
2461 aux = ZPOOL_CONFIG_SPARES;
2463 sav = &spa->spa_l2cache;
2464 aux = ZPOOL_CONFIG_L2CACHE;
2467 mutex_enter(&ztest_vdev_lock);
2469 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2471 if (sav->sav_count != 0 && ztest_random(4) == 0) {
2473 * Pick a random device to remove.
2475 guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
2478 * Find an unused device we can add.
2480 zs->zs_vdev_aux = 0;
2483 (void) snprintf(path, sizeof (path), ztest_aux_template,
2484 ztest_opts.zo_dir, ztest_opts.zo_pool, aux,
2486 for (c = 0; c < sav->sav_count; c++)
2487 if (strcmp(sav->sav_vdevs[c]->vdev_path,
2490 if (c == sav->sav_count &&
2491 vdev_lookup_by_path(rvd, path) == NULL)
2497 spa_config_exit(spa, SCL_VDEV, FTAG);
2503 nvlist_t *nvroot = make_vdev_root(NULL, aux,
2504 (ztest_opts.zo_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
2505 error = spa_vdev_add(spa, nvroot);
2507 fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
2508 nvlist_free(nvroot);
2511 * Remove an existing device. Sometimes, dirty its
2512 * vdev state first to make sure we handle removal
2513 * of devices that have pending state changes.
2515 if (ztest_random(2) == 0)
2516 (void) vdev_online(spa, guid, 0, NULL);
2518 error = spa_vdev_remove(spa, guid, B_FALSE);
2519 if (error != 0 && error != EBUSY)
2520 fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
2523 mutex_exit(&ztest_vdev_lock);
2525 umem_free(path, MAXPATHLEN);
2529 * split a pool if it has mirror tlvdevs
2533 ztest_split_pool(ztest_ds_t *zd, uint64_t id)
2535 ztest_shared_t *zs = ztest_shared;
2536 spa_t *spa = ztest_spa;
2537 vdev_t *rvd = spa->spa_root_vdev;
2538 nvlist_t *tree, **child, *config, *split, **schild;
2539 uint_t c, children, schildren = 0, lastlogid = 0;
2542 mutex_enter(&ztest_vdev_lock);
2544 /* ensure we have a useable config; mirrors of raidz aren't supported */
2545 if (zs->zs_mirrors < 3 || ztest_opts.zo_raidz > 1) {
2546 mutex_exit(&ztest_vdev_lock);
2550 /* clean up the old pool, if any */
2551 (void) spa_destroy("splitp");
2553 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2555 /* generate a config from the existing config */
2556 mutex_enter(&spa->spa_props_lock);
2557 VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
2559 mutex_exit(&spa->spa_props_lock);
2561 VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2564 schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
2565 for (c = 0; c < children; c++) {
2566 vdev_t *tvd = rvd->vdev_child[c];
2570 if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
2571 VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
2573 VERIFY(nvlist_add_string(schild[schildren],
2574 ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
2575 VERIFY(nvlist_add_uint64(schild[schildren],
2576 ZPOOL_CONFIG_IS_HOLE, 1) == 0);
2578 lastlogid = schildren;
2583 VERIFY(nvlist_lookup_nvlist_array(child[c],
2584 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2585 VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
2588 /* OK, create a config that can be used to split */
2589 VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
2590 VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
2591 VDEV_TYPE_ROOT) == 0);
2592 VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
2593 lastlogid != 0 ? lastlogid : schildren) == 0);
2595 VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
2596 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
2598 for (c = 0; c < schildren; c++)
2599 nvlist_free(schild[c]);
2603 spa_config_exit(spa, SCL_VDEV, FTAG);
2605 (void) rw_enter(&ztest_name_lock, RW_WRITER);
2606 error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
2607 (void) rw_exit(&ztest_name_lock);
2609 nvlist_free(config);
2612 (void) printf("successful split - results:\n");
2613 mutex_enter(&spa_namespace_lock);
2614 show_pool_stats(spa);
2615 show_pool_stats(spa_lookup("splitp"));
2616 mutex_exit(&spa_namespace_lock);
2620 mutex_exit(&ztest_vdev_lock);
2625 * Verify that we can attach and detach devices.
2629 ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
2631 ztest_shared_t *zs = ztest_shared;
2632 spa_t *spa = ztest_spa;
2633 spa_aux_vdev_t *sav = &spa->spa_spares;
2634 vdev_t *rvd = spa->spa_root_vdev;
2635 vdev_t *oldvd, *newvd, *pvd;
2639 uint64_t ashift = ztest_get_ashift();
2640 uint64_t oldguid, pguid;
2641 size_t oldsize, newsize;
2642 char *oldpath, *newpath;
2644 int oldvd_has_siblings = B_FALSE;
2645 int newvd_is_spare = B_FALSE;
2647 int error, expected_error;
2649 oldpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2650 newpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2652 mutex_enter(&ztest_vdev_lock);
2653 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
2655 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2658 * Decide whether to do an attach or a replace.
2660 replacing = ztest_random(2);
2663 * Pick a random top-level vdev.
2665 top = ztest_random_vdev_top(spa, B_TRUE);
2668 * Pick a random leaf within it.
2670 leaf = ztest_random(leaves);
2675 oldvd = rvd->vdev_child[top];
2676 if (zs->zs_mirrors >= 1) {
2677 ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
2678 ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
2679 oldvd = oldvd->vdev_child[leaf / ztest_opts.zo_raidz];
2681 if (ztest_opts.zo_raidz > 1) {
2682 ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
2683 ASSERT(oldvd->vdev_children == ztest_opts.zo_raidz);
2684 oldvd = oldvd->vdev_child[leaf % ztest_opts.zo_raidz];
2688 * If we're already doing an attach or replace, oldvd may be a
2689 * mirror vdev -- in which case, pick a random child.
2691 while (oldvd->vdev_children != 0) {
2692 oldvd_has_siblings = B_TRUE;
2693 ASSERT(oldvd->vdev_children >= 2);
2694 oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
2697 oldguid = oldvd->vdev_guid;
2698 oldsize = vdev_get_min_asize(oldvd);
2699 oldvd_is_log = oldvd->vdev_top->vdev_islog;
2700 (void) strcpy(oldpath, oldvd->vdev_path);
2701 pvd = oldvd->vdev_parent;
2702 pguid = pvd->vdev_guid;
2705 * If oldvd has siblings, then half of the time, detach it.
2707 if (oldvd_has_siblings && ztest_random(2) == 0) {
2708 spa_config_exit(spa, SCL_VDEV, FTAG);
2709 error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
2710 if (error != 0 && error != ENODEV && error != EBUSY &&
2712 fatal(0, "detach (%s) returned %d", oldpath, error);
2717 * For the new vdev, choose with equal probability between the two
2718 * standard paths (ending in either 'a' or 'b') or a random hot spare.
2720 if (sav->sav_count != 0 && ztest_random(3) == 0) {
2721 newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
2722 newvd_is_spare = B_TRUE;
2723 (void) strcpy(newpath, newvd->vdev_path);
2725 (void) snprintf(newpath, MAXPATHLEN, ztest_dev_template,
2726 ztest_opts.zo_dir, ztest_opts.zo_pool,
2727 top * leaves + leaf);
2728 if (ztest_random(2) == 0)
2729 newpath[strlen(newpath) - 1] = 'b';
2730 newvd = vdev_lookup_by_path(rvd, newpath);
2734 newsize = vdev_get_min_asize(newvd);
2737 * Make newsize a little bigger or smaller than oldsize.
2738 * If it's smaller, the attach should fail.
2739 * If it's larger, and we're doing a replace,
2740 * we should get dynamic LUN growth when we're done.
2742 newsize = 10 * oldsize / (9 + ztest_random(3));
2746 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
2747 * unless it's a replace; in that case any non-replacing parent is OK.
2749 * If newvd is already part of the pool, it should fail with EBUSY.
2751 * If newvd is too small, it should fail with EOVERFLOW.
2753 if (pvd->vdev_ops != &vdev_mirror_ops &&
2754 pvd->vdev_ops != &vdev_root_ops && (!replacing ||
2755 pvd->vdev_ops == &vdev_replacing_ops ||
2756 pvd->vdev_ops == &vdev_spare_ops))
2757 expected_error = ENOTSUP;
2758 else if (newvd_is_spare && (!replacing || oldvd_is_log))
2759 expected_error = ENOTSUP;
2760 else if (newvd == oldvd)
2761 expected_error = replacing ? 0 : EBUSY;
2762 else if (vdev_lookup_by_path(rvd, newpath) != NULL)
2763 expected_error = EBUSY;
2764 else if (newsize < oldsize)
2765 expected_error = EOVERFLOW;
2766 else if (ashift > oldvd->vdev_top->vdev_ashift)
2767 expected_error = EDOM;
2771 spa_config_exit(spa, SCL_VDEV, FTAG);
2774 * Build the nvlist describing newpath.
2776 root = make_vdev_root(newpath, NULL, newvd == NULL ? newsize : 0,
2777 ashift, 0, 0, 0, 1);
2779 error = spa_vdev_attach(spa, oldguid, root, replacing);
2784 * If our parent was the replacing vdev, but the replace completed,
2785 * then instead of failing with ENOTSUP we may either succeed,
2786 * fail with ENODEV, or fail with EOVERFLOW.
2788 if (expected_error == ENOTSUP &&
2789 (error == 0 || error == ENODEV || error == EOVERFLOW))
2790 expected_error = error;
2793 * If someone grew the LUN, the replacement may be too small.
2795 if (error == EOVERFLOW || error == EBUSY)
2796 expected_error = error;
2798 /* XXX workaround 6690467 */
2799 if (error != expected_error && expected_error != EBUSY) {
2800 fatal(0, "attach (%s %llu, %s %llu, %d) "
2801 "returned %d, expected %d",
2802 oldpath, (longlong_t)oldsize, newpath,
2803 (longlong_t)newsize, replacing, error, expected_error);
2806 mutex_exit(&ztest_vdev_lock);
2808 umem_free(oldpath, MAXPATHLEN);
2809 umem_free(newpath, MAXPATHLEN);
2813 * Callback function which expands the physical size of the vdev.
2816 grow_vdev(vdev_t *vd, void *arg)
2818 ASSERTV(spa_t *spa = vd->vdev_spa);
2819 size_t *newsize = arg;
2823 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
2824 ASSERT(vd->vdev_ops->vdev_op_leaf);
2826 if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
2829 fsize = lseek(fd, 0, SEEK_END);
2830 VERIFY(ftruncate(fd, *newsize) == 0);
2832 if (ztest_opts.zo_verbose >= 6) {
2833 (void) printf("%s grew from %lu to %lu bytes\n",
2834 vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
2841 * Callback function which expands a given vdev by calling vdev_online().
2845 online_vdev(vdev_t *vd, void *arg)
2847 spa_t *spa = vd->vdev_spa;
2848 vdev_t *tvd = vd->vdev_top;
2849 uint64_t guid = vd->vdev_guid;
2850 uint64_t generation = spa->spa_config_generation + 1;
2851 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
2854 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
2855 ASSERT(vd->vdev_ops->vdev_op_leaf);
2857 /* Calling vdev_online will initialize the new metaslabs */
2858 spa_config_exit(spa, SCL_STATE, spa);
2859 error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
2860 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
2863 * If vdev_online returned an error or the underlying vdev_open
2864 * failed then we abort the expand. The only way to know that
2865 * vdev_open fails is by checking the returned newstate.
2867 if (error || newstate != VDEV_STATE_HEALTHY) {
2868 if (ztest_opts.zo_verbose >= 5) {
2869 (void) printf("Unable to expand vdev, state %llu, "
2870 "error %d\n", (u_longlong_t)newstate, error);
2874 ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
2877 * Since we dropped the lock we need to ensure that we're
2878 * still talking to the original vdev. It's possible this
2879 * vdev may have been detached/replaced while we were
2880 * trying to online it.
2882 if (generation != spa->spa_config_generation) {
2883 if (ztest_opts.zo_verbose >= 5) {
2884 (void) printf("vdev configuration has changed, "
2885 "guid %llu, state %llu, expected gen %llu, "
2888 (u_longlong_t)tvd->vdev_state,
2889 (u_longlong_t)generation,
2890 (u_longlong_t)spa->spa_config_generation);
2898 * Traverse the vdev tree calling the supplied function.
2899 * We continue to walk the tree until we either have walked all
2900 * children or we receive a non-NULL return from the callback.
2901 * If a NULL callback is passed, then we just return back the first
2902 * leaf vdev we encounter.
2905 vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
2909 if (vd->vdev_ops->vdev_op_leaf) {
2913 return (func(vd, arg));
2916 for (c = 0; c < vd->vdev_children; c++) {
2917 vdev_t *cvd = vd->vdev_child[c];
2918 if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
2925 * Verify that dynamic LUN growth works as expected.
2929 ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
2931 spa_t *spa = ztest_spa;
2933 metaslab_class_t *mc;
2934 metaslab_group_t *mg;
2935 size_t psize, newsize;
2937 uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
2939 mutex_enter(&ztest_vdev_lock);
2940 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
2942 top = ztest_random_vdev_top(spa, B_TRUE);
2944 tvd = spa->spa_root_vdev->vdev_child[top];
2947 old_ms_count = tvd->vdev_ms_count;
2948 old_class_space = metaslab_class_get_space(mc);
2951 * Determine the size of the first leaf vdev associated with
2952 * our top-level device.
2954 vd = vdev_walk_tree(tvd, NULL, NULL);
2955 ASSERT3P(vd, !=, NULL);
2956 ASSERT(vd->vdev_ops->vdev_op_leaf);
2958 psize = vd->vdev_psize;
2961 * We only try to expand the vdev if it's healthy, less than 4x its
2962 * original size, and it has a valid psize.
2964 if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
2965 psize == 0 || psize >= 4 * ztest_opts.zo_vdev_size) {
2966 spa_config_exit(spa, SCL_STATE, spa);
2967 mutex_exit(&ztest_vdev_lock);
2971 newsize = psize + psize / 8;
2972 ASSERT3U(newsize, >, psize);
2974 if (ztest_opts.zo_verbose >= 6) {
2975 (void) printf("Expanding LUN %s from %lu to %lu\n",
2976 vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
2980 * Growing the vdev is a two step process:
2981 * 1). expand the physical size (i.e. relabel)
2982 * 2). online the vdev to create the new metaslabs
2984 if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
2985 vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
2986 tvd->vdev_state != VDEV_STATE_HEALTHY) {
2987 if (ztest_opts.zo_verbose >= 5) {
2988 (void) printf("Could not expand LUN because "
2989 "the vdev configuration changed.\n");
2991 spa_config_exit(spa, SCL_STATE, spa);
2992 mutex_exit(&ztest_vdev_lock);
2996 spa_config_exit(spa, SCL_STATE, spa);
2999 * Expanding the LUN will update the config asynchronously,
3000 * thus we must wait for the async thread to complete any
3001 * pending tasks before proceeding.
3005 mutex_enter(&spa->spa_async_lock);
3006 done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
3007 mutex_exit(&spa->spa_async_lock);
3010 txg_wait_synced(spa_get_dsl(spa), 0);
3011 (void) poll(NULL, 0, 100);
3014 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3016 tvd = spa->spa_root_vdev->vdev_child[top];
3017 new_ms_count = tvd->vdev_ms_count;
3018 new_class_space = metaslab_class_get_space(mc);
3020 if (tvd->vdev_mg != mg || mg->mg_class != mc) {
3021 if (ztest_opts.zo_verbose >= 5) {
3022 (void) printf("Could not verify LUN expansion due to "
3023 "intervening vdev offline or remove.\n");
3025 spa_config_exit(spa, SCL_STATE, spa);
3026 mutex_exit(&ztest_vdev_lock);
3031 * Make sure we were able to grow the vdev.
3033 if (new_ms_count <= old_ms_count)
3034 fatal(0, "LUN expansion failed: ms_count %llu <= %llu\n",
3035 old_ms_count, new_ms_count);
3038 * Make sure we were able to grow the pool.
3040 if (new_class_space <= old_class_space)
3041 fatal(0, "LUN expansion failed: class_space %llu <= %llu\n",
3042 old_class_space, new_class_space);
3044 if (ztest_opts.zo_verbose >= 5) {
3045 char oldnumbuf[6], newnumbuf[6];
3047 nicenum(old_class_space, oldnumbuf);
3048 nicenum(new_class_space, newnumbuf);
3049 (void) printf("%s grew from %s to %s\n",
3050 spa->spa_name, oldnumbuf, newnumbuf);
3053 spa_config_exit(spa, SCL_STATE, spa);
3054 mutex_exit(&ztest_vdev_lock);
3058 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
3062 ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3065 * Create the objects common to all ztest datasets.
3067 VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
3068 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
3072 ztest_dataset_create(char *dsname)
3074 uint64_t zilset = ztest_random(100);
3075 int err = dmu_objset_create(dsname, DMU_OST_OTHER, 0,
3076 ztest_objset_create_cb, NULL);
3078 if (err || zilset < 80)
3081 if (ztest_opts.zo_verbose >= 5)
3082 (void) printf("Setting dataset %s to sync always\n", dsname);
3083 return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC,
3084 ZFS_SYNC_ALWAYS, B_FALSE));
3089 ztest_objset_destroy_cb(const char *name, void *arg)
3092 dmu_object_info_t doi;
3096 * Verify that the dataset contains a directory object.
3098 VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os));
3099 error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
3100 if (error != ENOENT) {
3101 /* We could have crashed in the middle of destroying it */
3102 ASSERT3U(error, ==, 0);
3103 ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
3104 ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
3106 dmu_objset_rele(os, FTAG);
3109 * Destroy the dataset.
3111 VERIFY3U(0, ==, dmu_objset_destroy(name, B_FALSE));
3116 ztest_snapshot_create(char *osname, uint64_t id)
3118 char snapname[MAXNAMELEN];
3121 (void) snprintf(snapname, MAXNAMELEN, "%s@%llu", osname,
3124 error = dmu_objset_snapshot(osname, strchr(snapname, '@') + 1,
3125 NULL, NULL, B_FALSE, B_FALSE, -1);
3126 if (error == ENOSPC) {
3127 ztest_record_enospc(FTAG);
3130 if (error != 0 && error != EEXIST)
3131 fatal(0, "ztest_snapshot_create(%s) = %d", snapname, error);
3136 ztest_snapshot_destroy(char *osname, uint64_t id)
3138 char snapname[MAXNAMELEN];
3141 (void) snprintf(snapname, MAXNAMELEN, "%s@%llu", osname,
3144 error = dmu_objset_destroy(snapname, B_FALSE);
3145 if (error != 0 && error != ENOENT)
3146 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
3152 ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
3162 zdtmp = umem_alloc(sizeof (ztest_ds_t), UMEM_NOFAIL);
3163 name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3165 (void) rw_enter(&ztest_name_lock, RW_READER);
3167 (void) snprintf(name, MAXNAMELEN, "%s/temp_%llu",
3168 ztest_opts.zo_pool, (u_longlong_t)id);
3171 * If this dataset exists from a previous run, process its replay log
3172 * half of the time. If we don't replay it, then dmu_objset_destroy()
3173 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
3175 if (ztest_random(2) == 0 &&
3176 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) {
3177 ztest_zd_init(zdtmp, NULL, os);
3178 zil_replay(os, zdtmp, ztest_replay_vector);
3179 ztest_zd_fini(zdtmp);
3180 dmu_objset_disown(os, FTAG);
3184 * There may be an old instance of the dataset we're about to
3185 * create lying around from a previous run. If so, destroy it
3186 * and all of its snapshots.
3188 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
3189 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
3192 * Verify that the destroyed dataset is no longer in the namespace.
3194 VERIFY3U(ENOENT, ==, dmu_objset_hold(name, FTAG, &os));
3197 * Verify that we can create a new dataset.
3199 error = ztest_dataset_create(name);
3201 if (error == ENOSPC) {
3202 ztest_record_enospc(FTAG);
3205 fatal(0, "dmu_objset_create(%s) = %d", name, error);
3209 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os));
3211 ztest_zd_init(zdtmp, NULL, os);
3214 * Open the intent log for it.
3216 zilog = zil_open(os, ztest_get_data);
3219 * Put some objects in there, do a little I/O to them,
3220 * and randomly take a couple of snapshots along the way.
3222 iters = ztest_random(5);
3223 for (i = 0; i < iters; i++) {
3224 ztest_dmu_object_alloc_free(zdtmp, id);
3225 if (ztest_random(iters) == 0)
3226 (void) ztest_snapshot_create(name, i);
3230 * Verify that we cannot create an existing dataset.
3232 VERIFY3U(EEXIST, ==,
3233 dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL));
3236 * Verify that we can hold an objset that is also owned.
3238 VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
3239 dmu_objset_rele(os2, FTAG);
3242 * Verify that we cannot own an objset that is already owned.
3245 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2));
3248 dmu_objset_disown(os, FTAG);
3249 ztest_zd_fini(zdtmp);
3251 (void) rw_exit(&ztest_name_lock);
3253 umem_free(name, MAXNAMELEN);
3254 umem_free(zdtmp, sizeof (ztest_ds_t));
3258 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3261 ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
3263 (void) rw_enter(&ztest_name_lock, RW_READER);
3264 (void) ztest_snapshot_destroy(zd->zd_name, id);
3265 (void) ztest_snapshot_create(zd->zd_name, id);
3266 (void) rw_exit(&ztest_name_lock);
3270 * Cleanup non-standard snapshots and clones.
3273 ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
3282 snap1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3283 clone1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3284 snap2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3285 clone2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3286 snap3name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3288 (void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu",
3289 osname, (u_longlong_t)id);
3290 (void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu",
3291 osname, (u_longlong_t)id);
3292 (void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu",
3293 clone1name, (u_longlong_t)id);
3294 (void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu",
3295 osname, (u_longlong_t)id);
3296 (void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu",
3297 clone1name, (u_longlong_t)id);
3299 error = dmu_objset_destroy(clone2name, B_FALSE);
3300 if (error && error != ENOENT)
3301 fatal(0, "dmu_objset_destroy(%s) = %d", clone2name, error);
3302 error = dmu_objset_destroy(snap3name, B_FALSE);
3303 if (error && error != ENOENT)
3304 fatal(0, "dmu_objset_destroy(%s) = %d", snap3name, error);
3305 error = dmu_objset_destroy(snap2name, B_FALSE);
3306 if (error && error != ENOENT)
3307 fatal(0, "dmu_objset_destroy(%s) = %d", snap2name, error);
3308 error = dmu_objset_destroy(clone1name, B_FALSE);
3309 if (error && error != ENOENT)
3310 fatal(0, "dmu_objset_destroy(%s) = %d", clone1name, error);
3311 error = dmu_objset_destroy(snap1name, B_FALSE);
3312 if (error && error != ENOENT)
3313 fatal(0, "dmu_objset_destroy(%s) = %d", snap1name, error);
3315 umem_free(snap1name, MAXNAMELEN);
3316 umem_free(clone1name, MAXNAMELEN);
3317 umem_free(snap2name, MAXNAMELEN);
3318 umem_free(clone2name, MAXNAMELEN);
3319 umem_free(snap3name, MAXNAMELEN);
3323 * Verify dsl_dataset_promote handles EBUSY
3326 ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
3335 char *osname = zd->zd_name;
3338 snap1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3339 clone1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3340 snap2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3341 clone2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3342 snap3name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3344 (void) rw_enter(&ztest_name_lock, RW_READER);
3346 ztest_dsl_dataset_cleanup(osname, id);
3348 (void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu",
3349 osname, (u_longlong_t)id);
3350 (void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu",
3351 osname, (u_longlong_t)id);
3352 (void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu",
3353 clone1name, (u_longlong_t)id);
3354 (void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu",
3355 osname, (u_longlong_t)id);
3356 (void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu",
3357 clone1name, (u_longlong_t)id);
3359 error = dmu_objset_snapshot(osname, strchr(snap1name, '@')+1,
3360 NULL, NULL, B_FALSE, B_FALSE, -1);
3361 if (error && error != EEXIST) {
3362 if (error == ENOSPC) {
3363 ztest_record_enospc(FTAG);
3366 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
3369 error = dmu_objset_hold(snap1name, FTAG, &clone);
3371 fatal(0, "dmu_open_snapshot(%s) = %d", snap1name, error);
3373 error = dmu_objset_clone(clone1name, dmu_objset_ds(clone), 0);
3374 dmu_objset_rele(clone, FTAG);
3376 if (error == ENOSPC) {
3377 ztest_record_enospc(FTAG);
3380 fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
3383 error = dmu_objset_snapshot(clone1name, strchr(snap2name, '@')+1,
3384 NULL, NULL, B_FALSE, B_FALSE, -1);
3385 if (error && error != EEXIST) {
3386 if (error == ENOSPC) {
3387 ztest_record_enospc(FTAG);
3390 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
3393 error = dmu_objset_snapshot(clone1name, strchr(snap3name, '@')+1,
3394 NULL, NULL, B_FALSE, B_FALSE, -1);
3395 if (error && error != EEXIST) {
3396 if (error == ENOSPC) {
3397 ztest_record_enospc(FTAG);
3400 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
3403 error = dmu_objset_hold(snap3name, FTAG, &clone);
3405 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
3407 error = dmu_objset_clone(clone2name, dmu_objset_ds(clone), 0);
3408 dmu_objset_rele(clone, FTAG);
3410 if (error == ENOSPC) {
3411 ztest_record_enospc(FTAG);
3414 fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
3417 error = dsl_dataset_own(snap2name, B_FALSE, FTAG, &ds);
3419 fatal(0, "dsl_dataset_own(%s) = %d", snap2name, error);
3420 error = dsl_dataset_promote(clone2name, NULL);
3422 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
3424 dsl_dataset_disown(ds, FTAG);
3427 ztest_dsl_dataset_cleanup(osname, id);
3429 (void) rw_exit(&ztest_name_lock);
3431 umem_free(snap1name, MAXNAMELEN);
3432 umem_free(clone1name, MAXNAMELEN);
3433 umem_free(snap2name, MAXNAMELEN);
3434 umem_free(clone2name, MAXNAMELEN);
3435 umem_free(snap3name, MAXNAMELEN);
3438 #undef OD_ARRAY_SIZE
3439 #define OD_ARRAY_SIZE 4
3442 * Verify that dmu_object_{alloc,free} work as expected.
3445 ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
3452 size = sizeof(ztest_od_t) * OD_ARRAY_SIZE;
3453 od = umem_alloc(size, UMEM_NOFAIL);
3454 batchsize = OD_ARRAY_SIZE;
3456 for (b = 0; b < batchsize; b++)
3457 ztest_od_init(od + b, id, FTAG, b, DMU_OT_UINT64_OTHER, 0, 0);
3460 * Destroy the previous batch of objects, create a new batch,
3461 * and do some I/O on the new objects.
3463 if (ztest_object_init(zd, od, size, B_TRUE) != 0)
3466 while (ztest_random(4 * batchsize) != 0)
3467 ztest_io(zd, od[ztest_random(batchsize)].od_object,
3468 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3470 umem_free(od, size);
3473 #undef OD_ARRAY_SIZE
3474 #define OD_ARRAY_SIZE 2
3477 * Verify that dmu_{read,write} work as expected.
3480 ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
3485 objset_t *os = zd->zd_os;
3486 size = sizeof(ztest_od_t) * OD_ARRAY_SIZE;
3487 od = umem_alloc(size, UMEM_NOFAIL);
3489 int i, freeit, error;
3491 bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
3492 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3493 uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
3494 uint64_t regions = 997;
3495 uint64_t stride = 123456789ULL;
3496 uint64_t width = 40;
3497 int free_percent = 5;
3500 * This test uses two objects, packobj and bigobj, that are always
3501 * updated together (i.e. in the same tx) so that their contents are
3502 * in sync and can be compared. Their contents relate to each other
3503 * in a simple way: packobj is a dense array of 'bufwad' structures,
3504 * while bigobj is a sparse array of the same bufwads. Specifically,
3505 * for any index n, there are three bufwads that should be identical:
3507 * packobj, at offset n * sizeof (bufwad_t)
3508 * bigobj, at the head of the nth chunk
3509 * bigobj, at the tail of the nth chunk
3511 * The chunk size is arbitrary. It doesn't have to be a power of two,
3512 * and it doesn't have any relation to the object blocksize.
3513 * The only requirement is that it can hold at least two bufwads.
3515 * Normally, we write the bufwad to each of these locations.
3516 * However, free_percent of the time we instead write zeroes to
3517 * packobj and perform a dmu_free_range() on bigobj. By comparing
3518 * bigobj to packobj, we can verify that the DMU is correctly
3519 * tracking which parts of an object are allocated and free,
3520 * and that the contents of the allocated blocks are correct.
3524 * Read the directory info. If it's the first time, set things up.
3526 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, chunksize);
3527 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
3529 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
3530 umem_free(od, size);
3534 bigobj = od[0].od_object;
3535 packobj = od[1].od_object;
3536 chunksize = od[0].od_gen;
3537 ASSERT(chunksize == od[1].od_gen);
3540 * Prefetch a random chunk of the big object.
3541 * Our aim here is to get some async reads in flight
3542 * for blocks that we may free below; the DMU should
3543 * handle this race correctly.
3545 n = ztest_random(regions) * stride + ztest_random(width);
3546 s = 1 + ztest_random(2 * width - 1);
3547 dmu_prefetch(os, bigobj, n * chunksize, s * chunksize);
3550 * Pick a random index and compute the offsets into packobj and bigobj.
3552 n = ztest_random(regions) * stride + ztest_random(width);
3553 s = 1 + ztest_random(width - 1);
3555 packoff = n * sizeof (bufwad_t);
3556 packsize = s * sizeof (bufwad_t);
3558 bigoff = n * chunksize;
3559 bigsize = s * chunksize;
3561 packbuf = umem_alloc(packsize, UMEM_NOFAIL);
3562 bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
3565 * free_percent of the time, free a range of bigobj rather than
3568 freeit = (ztest_random(100) < free_percent);
3571 * Read the current contents of our objects.
3573 error = dmu_read(os, packobj, packoff, packsize, packbuf,
3575 ASSERT3U(error, ==, 0);
3576 error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
3578 ASSERT3U(error, ==, 0);
3581 * Get a tx for the mods to both packobj and bigobj.
3583 tx = dmu_tx_create(os);
3585 dmu_tx_hold_write(tx, packobj, packoff, packsize);
3588 dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
3590 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
3592 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3594 umem_free(packbuf, packsize);
3595 umem_free(bigbuf, bigsize);
3596 umem_free(od, size);
3600 dmu_object_set_checksum(os, bigobj,
3601 (enum zio_checksum)ztest_random_dsl_prop(ZFS_PROP_CHECKSUM), tx);
3603 dmu_object_set_compress(os, bigobj,
3604 (enum zio_compress)ztest_random_dsl_prop(ZFS_PROP_COMPRESSION), tx);
3607 * For each index from n to n + s, verify that the existing bufwad
3608 * in packobj matches the bufwads at the head and tail of the
3609 * corresponding chunk in bigobj. Then update all three bufwads
3610 * with the new values we want to write out.
3612 for (i = 0; i < s; i++) {
3614 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3616 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
3618 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
3620 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3621 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3623 if (pack->bw_txg > txg)
3624 fatal(0, "future leak: got %llx, open txg is %llx",
3627 if (pack->bw_data != 0 && pack->bw_index != n + i)
3628 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3629 pack->bw_index, n, i);
3631 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3632 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
3634 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
3635 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
3638 bzero(pack, sizeof (bufwad_t));
3640 pack->bw_index = n + i;
3642 pack->bw_data = 1 + ztest_random(-2ULL);
3649 * We've verified all the old bufwads, and made new ones.
3650 * Now write them out.
3652 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
3655 if (ztest_opts.zo_verbose >= 7) {
3656 (void) printf("freeing offset %llx size %llx"
3658 (u_longlong_t)bigoff,
3659 (u_longlong_t)bigsize,
3662 VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
3664 if (ztest_opts.zo_verbose >= 7) {
3665 (void) printf("writing offset %llx size %llx"
3667 (u_longlong_t)bigoff,
3668 (u_longlong_t)bigsize,
3671 dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
3677 * Sanity check the stuff we just wrote.
3680 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
3681 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
3683 VERIFY(0 == dmu_read(os, packobj, packoff,
3684 packsize, packcheck, DMU_READ_PREFETCH));
3685 VERIFY(0 == dmu_read(os, bigobj, bigoff,
3686 bigsize, bigcheck, DMU_READ_PREFETCH));
3688 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
3689 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
3691 umem_free(packcheck, packsize);
3692 umem_free(bigcheck, bigsize);
3695 umem_free(packbuf, packsize);
3696 umem_free(bigbuf, bigsize);
3697 umem_free(od, size);
3701 compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
3702 uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
3710 * For each index from n to n + s, verify that the existing bufwad
3711 * in packobj matches the bufwads at the head and tail of the
3712 * corresponding chunk in bigobj. Then update all three bufwads
3713 * with the new values we want to write out.
3715 for (i = 0; i < s; i++) {
3717 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3719 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
3721 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
3723 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3724 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3726 if (pack->bw_txg > txg)
3727 fatal(0, "future leak: got %llx, open txg is %llx",
3730 if (pack->bw_data != 0 && pack->bw_index != n + i)
3731 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3732 pack->bw_index, n, i);
3734 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3735 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
3737 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
3738 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
3740 pack->bw_index = n + i;
3742 pack->bw_data = 1 + ztest_random(-2ULL);
3749 #undef OD_ARRAY_SIZE
3750 #define OD_ARRAY_SIZE 2
3753 ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
3755 objset_t *os = zd->zd_os;
3762 bufwad_t *packbuf, *bigbuf;
3763 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3764 uint64_t blocksize = ztest_random_blocksize();
3765 uint64_t chunksize = blocksize;
3766 uint64_t regions = 997;
3767 uint64_t stride = 123456789ULL;
3769 dmu_buf_t *bonus_db;
3770 arc_buf_t **bigbuf_arcbufs;
3771 dmu_object_info_t doi;
3773 size = sizeof(ztest_od_t) * OD_ARRAY_SIZE;
3774 od = umem_alloc(size, UMEM_NOFAIL);
3777 * This test uses two objects, packobj and bigobj, that are always
3778 * updated together (i.e. in the same tx) so that their contents are
3779 * in sync and can be compared. Their contents relate to each other
3780 * in a simple way: packobj is a dense array of 'bufwad' structures,
3781 * while bigobj is a sparse array of the same bufwads. Specifically,
3782 * for any index n, there are three bufwads that should be identical:
3784 * packobj, at offset n * sizeof (bufwad_t)
3785 * bigobj, at the head of the nth chunk
3786 * bigobj, at the tail of the nth chunk
3788 * The chunk size is set equal to bigobj block size so that
3789 * dmu_assign_arcbuf() can be tested for object updates.
3793 * Read the directory info. If it's the first time, set things up.
3795 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
3796 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
3799 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
3800 umem_free(od, size);
3804 bigobj = od[0].od_object;
3805 packobj = od[1].od_object;
3806 blocksize = od[0].od_blocksize;
3807 chunksize = blocksize;
3808 ASSERT(chunksize == od[1].od_gen);
3810 VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
3811 VERIFY(ISP2(doi.doi_data_block_size));
3812 VERIFY(chunksize == doi.doi_data_block_size);
3813 VERIFY(chunksize >= 2 * sizeof (bufwad_t));
3816 * Pick a random index and compute the offsets into packobj and bigobj.
3818 n = ztest_random(regions) * stride + ztest_random(width);
3819 s = 1 + ztest_random(width - 1);
3821 packoff = n * sizeof (bufwad_t);
3822 packsize = s * sizeof (bufwad_t);
3824 bigoff = n * chunksize;
3825 bigsize = s * chunksize;
3827 packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
3828 bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
3830 VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
3832 bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
3835 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
3836 * Iteration 1 test zcopy to already referenced dbufs.
3837 * Iteration 2 test zcopy to dirty dbuf in the same txg.
3838 * Iteration 3 test zcopy to dbuf dirty in previous txg.
3839 * Iteration 4 test zcopy when dbuf is no longer dirty.
3840 * Iteration 5 test zcopy when it can't be done.
3841 * Iteration 6 one more zcopy write.
3843 for (i = 0; i < 7; i++) {
3848 * In iteration 5 (i == 5) use arcbufs
3849 * that don't match bigobj blksz to test
3850 * dmu_assign_arcbuf() when it can't directly
3851 * assign an arcbuf to a dbuf.
3853 for (j = 0; j < s; j++) {
3856 dmu_request_arcbuf(bonus_db, chunksize);
3858 bigbuf_arcbufs[2 * j] =
3859 dmu_request_arcbuf(bonus_db, chunksize / 2);
3860 bigbuf_arcbufs[2 * j + 1] =
3861 dmu_request_arcbuf(bonus_db, chunksize / 2);
3866 * Get a tx for the mods to both packobj and bigobj.
3868 tx = dmu_tx_create(os);
3870 dmu_tx_hold_write(tx, packobj, packoff, packsize);
3871 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
3873 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3875 umem_free(packbuf, packsize);
3876 umem_free(bigbuf, bigsize);
3877 for (j = 0; j < s; j++) {
3879 dmu_return_arcbuf(bigbuf_arcbufs[j]);
3882 bigbuf_arcbufs[2 * j]);
3884 bigbuf_arcbufs[2 * j + 1]);
3887 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
3888 umem_free(od, size);
3889 dmu_buf_rele(bonus_db, FTAG);
3894 * 50% of the time don't read objects in the 1st iteration to
3895 * test dmu_assign_arcbuf() for the case when there're no
3896 * existing dbufs for the specified offsets.
3898 if (i != 0 || ztest_random(2) != 0) {
3899 error = dmu_read(os, packobj, packoff,
3900 packsize, packbuf, DMU_READ_PREFETCH);
3901 ASSERT3U(error, ==, 0);
3902 error = dmu_read(os, bigobj, bigoff, bigsize,
3903 bigbuf, DMU_READ_PREFETCH);
3904 ASSERT3U(error, ==, 0);
3906 compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
3910 * We've verified all the old bufwads, and made new ones.
3911 * Now write them out.
3913 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
3914 if (ztest_opts.zo_verbose >= 7) {
3915 (void) printf("writing offset %llx size %llx"
3917 (u_longlong_t)bigoff,
3918 (u_longlong_t)bigsize,
3921 for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
3924 bcopy((caddr_t)bigbuf + (off - bigoff),
3925 bigbuf_arcbufs[j]->b_data, chunksize);
3927 bcopy((caddr_t)bigbuf + (off - bigoff),
3928 bigbuf_arcbufs[2 * j]->b_data,
3930 bcopy((caddr_t)bigbuf + (off - bigoff) +
3932 bigbuf_arcbufs[2 * j + 1]->b_data,
3937 VERIFY(dmu_buf_hold(os, bigobj, off,
3938 FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0);
3941 dmu_assign_arcbuf(bonus_db, off,
3942 bigbuf_arcbufs[j], tx);
3944 dmu_assign_arcbuf(bonus_db, off,
3945 bigbuf_arcbufs[2 * j], tx);
3946 dmu_assign_arcbuf(bonus_db,
3947 off + chunksize / 2,
3948 bigbuf_arcbufs[2 * j + 1], tx);
3951 dmu_buf_rele(dbt, FTAG);
3957 * Sanity check the stuff we just wrote.
3960 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
3961 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
3963 VERIFY(0 == dmu_read(os, packobj, packoff,
3964 packsize, packcheck, DMU_READ_PREFETCH));
3965 VERIFY(0 == dmu_read(os, bigobj, bigoff,
3966 bigsize, bigcheck, DMU_READ_PREFETCH));
3968 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
3969 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
3971 umem_free(packcheck, packsize);
3972 umem_free(bigcheck, bigsize);
3975 txg_wait_open(dmu_objset_pool(os), 0);
3976 } else if (i == 3) {
3977 txg_wait_synced(dmu_objset_pool(os), 0);
3981 dmu_buf_rele(bonus_db, FTAG);
3982 umem_free(packbuf, packsize);
3983 umem_free(bigbuf, bigsize);
3984 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
3985 umem_free(od, size);
3990 ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
3994 od = umem_alloc(sizeof(ztest_od_t), UMEM_NOFAIL);
3995 uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
3996 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3999 * Have multiple threads write to large offsets in an object
4000 * to verify that parallel writes to an object -- even to the
4001 * same blocks within the object -- doesn't cause any trouble.
4003 ztest_od_init(od, ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
4005 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0)
4008 while (ztest_random(10) != 0)
4009 ztest_io(zd, od->od_object, offset);
4011 umem_free(od, sizeof(ztest_od_t));
4015 ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
4018 uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
4019 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4020 uint64_t count = ztest_random(20) + 1;
4021 uint64_t blocksize = ztest_random_blocksize();
4024 od = umem_alloc(sizeof(ztest_od_t), UMEM_NOFAIL);
4026 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
4028 if (ztest_object_init(zd, od, sizeof (ztest_od_t), !ztest_random(2)) != 0) {
4029 umem_free(od, sizeof(ztest_od_t));
4033 if (ztest_truncate(zd, od->od_object, offset, count * blocksize) != 0) {
4034 umem_free(od, sizeof(ztest_od_t));
4038 ztest_prealloc(zd, od->od_object, offset, count * blocksize);
4040 data = umem_zalloc(blocksize, UMEM_NOFAIL);
4042 while (ztest_random(count) != 0) {
4043 uint64_t randoff = offset + (ztest_random(count) * blocksize);
4044 if (ztest_write(zd, od->od_object, randoff, blocksize,
4047 while (ztest_random(4) != 0)
4048 ztest_io(zd, od->od_object, randoff);
4051 umem_free(data, blocksize);
4052 umem_free(od, sizeof(ztest_od_t));
4056 * Verify that zap_{create,destroy,add,remove,update} work as expected.
4058 #define ZTEST_ZAP_MIN_INTS 1
4059 #define ZTEST_ZAP_MAX_INTS 4
4060 #define ZTEST_ZAP_MAX_PROPS 1000
4063 ztest_zap(ztest_ds_t *zd, uint64_t id)
4065 objset_t *os = zd->zd_os;
4068 uint64_t txg, last_txg;
4069 uint64_t value[ZTEST_ZAP_MAX_INTS];
4070 uint64_t zl_ints, zl_intsize, prop;
4073 char propname[100], txgname[100];
4075 char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
4077 od = umem_alloc(sizeof(ztest_od_t), UMEM_NOFAIL);
4078 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
4080 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4081 !ztest_random(2)) != 0)
4084 object = od->od_object;
4087 * Generate a known hash collision, and verify that
4088 * we can lookup and remove both entries.
4090 tx = dmu_tx_create(os);
4091 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4092 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4095 for (i = 0; i < 2; i++) {
4097 VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
4100 for (i = 0; i < 2; i++) {
4101 VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
4102 sizeof (uint64_t), 1, &value[i], tx));
4104 zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
4105 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4106 ASSERT3U(zl_ints, ==, 1);
4108 for (i = 0; i < 2; i++) {
4109 VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
4114 * Generate a buch of random entries.
4116 ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
4118 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4119 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4120 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4121 bzero(value, sizeof (value));
4125 * If these zap entries already exist, validate their contents.
4127 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4129 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4130 ASSERT3U(zl_ints, ==, 1);
4132 VERIFY(zap_lookup(os, object, txgname, zl_intsize,
4133 zl_ints, &last_txg) == 0);
4135 VERIFY(zap_length(os, object, propname, &zl_intsize,
4138 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4139 ASSERT3U(zl_ints, ==, ints);
4141 VERIFY(zap_lookup(os, object, propname, zl_intsize,
4142 zl_ints, value) == 0);
4144 for (i = 0; i < ints; i++) {
4145 ASSERT3U(value[i], ==, last_txg + object + i);
4148 ASSERT3U(error, ==, ENOENT);
4152 * Atomically update two entries in our zap object.
4153 * The first is named txg_%llu, and contains the txg
4154 * in which the property was last updated. The second
4155 * is named prop_%llu, and the nth element of its value
4156 * should be txg + object + n.
4158 tx = dmu_tx_create(os);
4159 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4160 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4165 fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
4167 for (i = 0; i < ints; i++)
4168 value[i] = txg + object + i;
4170 VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
4172 VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
4178 * Remove a random pair of entries.
4180 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4181 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4182 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4184 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4186 if (error == ENOENT)
4189 ASSERT3U(error, ==, 0);
4191 tx = dmu_tx_create(os);
4192 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4193 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4196 VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
4197 VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
4200 umem_free(od, sizeof(ztest_od_t));
4204 * Testcase to test the upgrading of a microzap to fatzap.
4207 ztest_fzap(ztest_ds_t *zd, uint64_t id)
4209 objset_t *os = zd->zd_os;
4211 uint64_t object, txg;
4214 od = umem_alloc(sizeof(ztest_od_t), UMEM_NOFAIL);
4215 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
4217 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4218 !ztest_random(2)) != 0)
4220 object = od->od_object;
4223 * Add entries to this ZAP and make sure it spills over
4224 * and gets upgraded to a fatzap. Also, since we are adding
4225 * 2050 entries we should see ptrtbl growth and leaf-block split.
4227 for (i = 0; i < 2050; i++) {
4228 char name[MAXNAMELEN];
4233 (void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
4234 (u_longlong_t)id, (u_longlong_t)value);
4236 tx = dmu_tx_create(os);
4237 dmu_tx_hold_zap(tx, object, B_TRUE, name);
4238 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4241 error = zap_add(os, object, name, sizeof (uint64_t), 1,
4243 ASSERT(error == 0 || error == EEXIST);
4247 umem_free(od, sizeof(ztest_od_t));
4252 ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
4254 objset_t *os = zd->zd_os;
4256 uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
4258 int i, namelen, error;
4259 int micro = ztest_random(2);
4260 char name[20], string_value[20];
4263 od = umem_alloc(sizeof(ztest_od_t), UMEM_NOFAIL);
4264 ztest_od_init(od, ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER, 0, 0);
4266 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
4267 umem_free(od, sizeof(ztest_od_t));
4271 object = od->od_object;
4274 * Generate a random name of the form 'xxx.....' where each
4275 * x is a random printable character and the dots are dots.
4276 * There are 94 such characters, and the name length goes from
4277 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
4279 namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
4281 for (i = 0; i < 3; i++)
4282 name[i] = '!' + ztest_random('~' - '!' + 1);
4283 for (; i < namelen - 1; i++)
4287 if ((namelen & 1) || micro) {
4288 wsize = sizeof (txg);
4294 data = string_value;
4298 VERIFY(zap_count(os, object, &count) == 0);
4299 ASSERT(count != -1ULL);
4302 * Select an operation: length, lookup, add, update, remove.
4304 i = ztest_random(5);
4307 tx = dmu_tx_create(os);
4308 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4309 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4312 bcopy(name, string_value, namelen);
4316 bzero(string_value, namelen);
4322 error = zap_length(os, object, name, &zl_wsize, &zl_wc);
4324 ASSERT3U(wsize, ==, zl_wsize);
4325 ASSERT3U(wc, ==, zl_wc);
4327 ASSERT3U(error, ==, ENOENT);
4332 error = zap_lookup(os, object, name, wsize, wc, data);
4334 if (data == string_value &&
4335 bcmp(name, data, namelen) != 0)
4336 fatal(0, "name '%s' != val '%s' len %d",
4337 name, data, namelen);
4339 ASSERT3U(error, ==, ENOENT);
4344 error = zap_add(os, object, name, wsize, wc, data, tx);
4345 ASSERT(error == 0 || error == EEXIST);
4349 VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
4353 error = zap_remove(os, object, name, tx);
4354 ASSERT(error == 0 || error == ENOENT);
4361 umem_free(od, sizeof(ztest_od_t));
4365 * Commit callback data.
4367 typedef struct ztest_cb_data {
4368 list_node_t zcd_node;
4370 int zcd_expected_err;
4371 boolean_t zcd_added;
4372 boolean_t zcd_called;
4376 /* This is the actual commit callback function */
4378 ztest_commit_callback(void *arg, int error)
4380 ztest_cb_data_t *data = arg;
4381 uint64_t synced_txg;
4383 VERIFY(data != NULL);
4384 VERIFY3S(data->zcd_expected_err, ==, error);
4385 VERIFY(!data->zcd_called);
4387 synced_txg = spa_last_synced_txg(data->zcd_spa);
4388 if (data->zcd_txg > synced_txg)
4389 fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
4390 ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
4393 data->zcd_called = B_TRUE;
4395 if (error == ECANCELED) {
4396 ASSERT3U(data->zcd_txg, ==, 0);
4397 ASSERT(!data->zcd_added);
4400 * The private callback data should be destroyed here, but
4401 * since we are going to check the zcd_called field after
4402 * dmu_tx_abort(), we will destroy it there.
4407 ASSERT(data->zcd_added);
4408 ASSERT3U(data->zcd_txg, !=, 0);
4410 (void) mutex_enter(&zcl.zcl_callbacks_lock);
4412 /* See if this cb was called more quickly */
4413 if ((synced_txg - data->zcd_txg) < zc_min_txg_delay)
4414 zc_min_txg_delay = synced_txg - data->zcd_txg;
4416 /* Remove our callback from the list */
4417 list_remove(&zcl.zcl_callbacks, data);
4419 (void) mutex_exit(&zcl.zcl_callbacks_lock);
4421 umem_free(data, sizeof (ztest_cb_data_t));
4424 /* Allocate and initialize callback data structure */
4425 static ztest_cb_data_t *
4426 ztest_create_cb_data(objset_t *os, uint64_t txg)
4428 ztest_cb_data_t *cb_data;
4430 cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
4432 cb_data->zcd_txg = txg;
4433 cb_data->zcd_spa = dmu_objset_spa(os);
4434 list_link_init(&cb_data->zcd_node);
4440 * Commit callback test.
4443 ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
4445 objset_t *os = zd->zd_os;
4448 ztest_cb_data_t *cb_data[3], *tmp_cb;
4449 uint64_t old_txg, txg;
4452 od = umem_alloc(sizeof(ztest_od_t), UMEM_NOFAIL);
4453 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
4455 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
4456 umem_free(od, sizeof(ztest_od_t));
4460 tx = dmu_tx_create(os);
4462 cb_data[0] = ztest_create_cb_data(os, 0);
4463 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
4465 dmu_tx_hold_write(tx, od->od_object, 0, sizeof (uint64_t));
4467 /* Every once in a while, abort the transaction on purpose */
4468 if (ztest_random(100) == 0)
4472 error = dmu_tx_assign(tx, TXG_NOWAIT);
4474 txg = error ? 0 : dmu_tx_get_txg(tx);
4476 cb_data[0]->zcd_txg = txg;
4477 cb_data[1] = ztest_create_cb_data(os, txg);
4478 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
4482 * It's not a strict requirement to call the registered
4483 * callbacks from inside dmu_tx_abort(), but that's what
4484 * it's supposed to happen in the current implementation
4485 * so we will check for that.
4487 for (i = 0; i < 2; i++) {
4488 cb_data[i]->zcd_expected_err = ECANCELED;
4489 VERIFY(!cb_data[i]->zcd_called);
4494 for (i = 0; i < 2; i++) {
4495 VERIFY(cb_data[i]->zcd_called);
4496 umem_free(cb_data[i], sizeof (ztest_cb_data_t));
4499 umem_free(od, sizeof(ztest_od_t));
4503 cb_data[2] = ztest_create_cb_data(os, txg);
4504 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
4507 * Read existing data to make sure there isn't a future leak.
4509 VERIFY(0 == dmu_read(os, od->od_object, 0, sizeof (uint64_t),
4510 &old_txg, DMU_READ_PREFETCH));
4513 fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
4516 dmu_write(os, od->od_object, 0, sizeof (uint64_t), &txg, tx);
4518 (void) mutex_enter(&zcl.zcl_callbacks_lock);
4521 * Since commit callbacks don't have any ordering requirement and since
4522 * it is theoretically possible for a commit callback to be called
4523 * after an arbitrary amount of time has elapsed since its txg has been
4524 * synced, it is difficult to reliably determine whether a commit
4525 * callback hasn't been called due to high load or due to a flawed
4528 * In practice, we will assume that if after a certain number of txgs a
4529 * commit callback hasn't been called, then most likely there's an
4530 * implementation bug..
4532 tmp_cb = list_head(&zcl.zcl_callbacks);
4533 if (tmp_cb != NULL &&
4534 tmp_cb->zcd_txg + ZTEST_COMMIT_CB_THRESH < txg) {
4535 fatal(0, "Commit callback threshold exceeded, oldest txg: %"
4536 PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
4540 * Let's find the place to insert our callbacks.
4542 * Even though the list is ordered by txg, it is possible for the
4543 * insertion point to not be the end because our txg may already be
4544 * quiescing at this point and other callbacks in the open txg
4545 * (from other objsets) may have sneaked in.
4547 tmp_cb = list_tail(&zcl.zcl_callbacks);
4548 while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
4549 tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
4551 /* Add the 3 callbacks to the list */
4552 for (i = 0; i < 3; i++) {
4554 list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
4556 list_insert_after(&zcl.zcl_callbacks, tmp_cb,
4559 cb_data[i]->zcd_added = B_TRUE;
4560 VERIFY(!cb_data[i]->zcd_called);
4562 tmp_cb = cb_data[i];
4567 (void) mutex_exit(&zcl.zcl_callbacks_lock);
4571 umem_free(od, sizeof(ztest_od_t));
4576 ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
4578 zfs_prop_t proplist[] = {
4580 ZFS_PROP_COMPRESSION,
4586 (void) rw_enter(&ztest_name_lock, RW_READER);
4588 for (p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
4589 (void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
4590 ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
4592 (void) rw_exit(&ztest_name_lock);
4597 ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
4599 nvlist_t *props = NULL;
4601 (void) rw_enter(&ztest_name_lock, RW_READER);
4603 (void) ztest_spa_prop_set_uint64(ZPOOL_PROP_DEDUPDITTO,
4604 ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
4606 VERIFY3U(spa_prop_get(ztest_spa, &props), ==, 0);
4608 if (ztest_opts.zo_verbose >= 6)
4609 dump_nvlist(props, 4);
4613 (void) rw_exit(&ztest_name_lock);
4617 * Test snapshot hold/release and deferred destroy.
4620 ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
4623 objset_t *os = zd->zd_os;
4627 char clonename[100];
4629 char osname[MAXNAMELEN];
4631 (void) rw_enter(&ztest_name_lock, RW_READER);
4633 dmu_objset_name(os, osname);
4635 (void) snprintf(snapname, 100, "sh1_%llu", (u_longlong_t)id);
4636 (void) snprintf(fullname, 100, "%s@%s", osname, snapname);
4637 (void) snprintf(clonename, 100, "%s/ch1_%llu",osname,(u_longlong_t)id);
4638 (void) snprintf(tag, 100, "tag_%llu", (u_longlong_t)id);
4641 * Clean up from any previous run.
4643 (void) dmu_objset_destroy(clonename, B_FALSE);
4644 (void) dsl_dataset_user_release(osname, snapname, tag, B_FALSE);
4645 (void) dmu_objset_destroy(fullname, B_FALSE);
4648 * Create snapshot, clone it, mark snap for deferred destroy,
4649 * destroy clone, verify snap was also destroyed.
4651 error = dmu_objset_snapshot(osname, snapname, NULL, NULL, FALSE,
4654 if (error == ENOSPC) {
4655 ztest_record_enospc("dmu_objset_snapshot");
4658 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
4661 error = dmu_objset_hold(fullname, FTAG, &origin);
4663 fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
4665 error = dmu_objset_clone(clonename, dmu_objset_ds(origin), 0);
4666 dmu_objset_rele(origin, FTAG);
4668 if (error == ENOSPC) {
4669 ztest_record_enospc("dmu_objset_clone");
4672 fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
4675 error = dmu_objset_destroy(fullname, B_TRUE);
4677 fatal(0, "dmu_objset_destroy(%s, B_TRUE) = %d",
4681 error = dmu_objset_destroy(clonename, B_FALSE);
4683 fatal(0, "dmu_objset_destroy(%s) = %d", clonename, error);
4685 error = dmu_objset_hold(fullname, FTAG, &origin);
4686 if (error != ENOENT)
4687 fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
4690 * Create snapshot, add temporary hold, verify that we can't
4691 * destroy a held snapshot, mark for deferred destroy,
4692 * release hold, verify snapshot was destroyed.
4694 error = dmu_objset_snapshot(osname, snapname, NULL, NULL, FALSE,
4697 if (error == ENOSPC) {
4698 ztest_record_enospc("dmu_objset_snapshot");
4701 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
4704 error = dsl_dataset_user_hold(osname, snapname, tag, B_FALSE,
4707 fatal(0, "dsl_dataset_user_hold(%s)", fullname, tag);
4709 error = dmu_objset_destroy(fullname, B_FALSE);
4710 if (error != EBUSY) {
4711 fatal(0, "dmu_objset_destroy(%s, B_FALSE) = %d",
4715 error = dmu_objset_destroy(fullname, B_TRUE);
4717 fatal(0, "dmu_objset_destroy(%s, B_TRUE) = %d",
4721 error = dsl_dataset_user_release(osname, snapname, tag, B_FALSE);
4723 fatal(0, "dsl_dataset_user_release(%s)", fullname, tag);
4725 VERIFY(dmu_objset_hold(fullname, FTAG, &origin) == ENOENT);
4728 (void) rw_exit(&ztest_name_lock);
4732 * Inject random faults into the on-disk data.
4736 ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
4738 ztest_shared_t *zs = ztest_shared;
4739 spa_t *spa = ztest_spa;
4743 uint64_t bad = 0x1990c0ffeedecadeull;
4748 int bshift = SPA_MAXBLOCKSHIFT + 2; /* don't scrog all labels */
4754 boolean_t islog = B_FALSE;
4756 path0 = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
4757 pathrand = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
4759 mutex_enter(&ztest_vdev_lock);
4760 maxfaults = MAXFAULTS();
4761 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
4762 mirror_save = zs->zs_mirrors;
4763 mutex_exit(&ztest_vdev_lock);
4765 ASSERT(leaves >= 1);
4768 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
4770 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
4772 if (ztest_random(2) == 0) {
4774 * Inject errors on a normal data device or slog device.
4776 top = ztest_random_vdev_top(spa, B_TRUE);
4777 leaf = ztest_random(leaves) + zs->zs_splits;
4780 * Generate paths to the first leaf in this top-level vdev,
4781 * and to the random leaf we selected. We'll induce transient
4782 * write failures and random online/offline activity on leaf 0,
4783 * and we'll write random garbage to the randomly chosen leaf.
4785 (void) snprintf(path0, MAXPATHLEN, ztest_dev_template,
4786 ztest_opts.zo_dir, ztest_opts.zo_pool,
4787 top * leaves + zs->zs_splits);
4788 (void) snprintf(pathrand, MAXPATHLEN, ztest_dev_template,
4789 ztest_opts.zo_dir, ztest_opts.zo_pool,
4790 top * leaves + leaf);
4792 vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
4793 if (vd0 != NULL && vd0->vdev_top->vdev_islog)
4796 if (vd0 != NULL && maxfaults != 1) {
4798 * Make vd0 explicitly claim to be unreadable,
4799 * or unwriteable, or reach behind its back
4800 * and close the underlying fd. We can do this if
4801 * maxfaults == 0 because we'll fail and reexecute,
4802 * and we can do it if maxfaults >= 2 because we'll
4803 * have enough redundancy. If maxfaults == 1, the
4804 * combination of this with injection of random data
4805 * corruption below exceeds the pool's fault tolerance.
4807 vdev_file_t *vf = vd0->vdev_tsd;
4809 if (vf != NULL && ztest_random(3) == 0) {
4810 (void) close(vf->vf_vnode->v_fd);
4811 vf->vf_vnode->v_fd = -1;
4812 } else if (ztest_random(2) == 0) {
4813 vd0->vdev_cant_read = B_TRUE;
4815 vd0->vdev_cant_write = B_TRUE;
4817 guid0 = vd0->vdev_guid;
4821 * Inject errors on an l2cache device.
4823 spa_aux_vdev_t *sav = &spa->spa_l2cache;
4825 if (sav->sav_count == 0) {
4826 spa_config_exit(spa, SCL_STATE, FTAG);
4829 vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
4830 guid0 = vd0->vdev_guid;
4831 (void) strcpy(path0, vd0->vdev_path);
4832 (void) strcpy(pathrand, vd0->vdev_path);
4836 maxfaults = INT_MAX; /* no limit on cache devices */
4839 spa_config_exit(spa, SCL_STATE, FTAG);
4842 * If we can tolerate two or more faults, or we're dealing
4843 * with a slog, randomly online/offline vd0.
4845 if ((maxfaults >= 2 || islog) && guid0 != 0) {
4846 if (ztest_random(10) < 6) {
4847 int flags = (ztest_random(2) == 0 ?
4848 ZFS_OFFLINE_TEMPORARY : 0);
4851 * We have to grab the zs_name_lock as writer to
4852 * prevent a race between offlining a slog and
4853 * destroying a dataset. Offlining the slog will
4854 * grab a reference on the dataset which may cause
4855 * dmu_objset_destroy() to fail with EBUSY thus
4856 * leaving the dataset in an inconsistent state.
4859 (void) rw_enter(&ztest_name_lock,
4862 VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
4865 (void) rw_exit(&ztest_name_lock);
4867 (void) vdev_online(spa, guid0, 0, NULL);
4875 * We have at least single-fault tolerance, so inject data corruption.
4877 fd = open(pathrand, O_RDWR);
4879 if (fd == -1) /* we hit a gap in the device namespace */
4882 fsize = lseek(fd, 0, SEEK_END);
4884 while (--iters != 0) {
4885 offset = ztest_random(fsize / (leaves << bshift)) *
4886 (leaves << bshift) + (leaf << bshift) +
4887 (ztest_random(1ULL << (bshift - 1)) & -8ULL);
4889 if (offset >= fsize)
4892 mutex_enter(&ztest_vdev_lock);
4893 if (mirror_save != zs->zs_mirrors) {
4894 mutex_exit(&ztest_vdev_lock);
4899 if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
4900 fatal(1, "can't inject bad word at 0x%llx in %s",
4903 mutex_exit(&ztest_vdev_lock);
4905 if (ztest_opts.zo_verbose >= 7)
4906 (void) printf("injected bad word into %s,"
4907 " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
4912 umem_free(path0, MAXPATHLEN);
4913 umem_free(pathrand, MAXPATHLEN);
4917 * Verify that DDT repair works as expected.
4920 ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
4922 ztest_shared_t *zs = ztest_shared;
4923 spa_t *spa = ztest_spa;
4924 objset_t *os = zd->zd_os;
4926 uint64_t object, blocksize, txg, pattern, psize;
4927 enum zio_checksum checksum = spa_dedup_checksum(spa);
4932 int copies = 2 * ZIO_DEDUPDITTO_MIN;
4935 blocksize = ztest_random_blocksize();
4936 blocksize = MIN(blocksize, 2048); /* because we write so many */
4938 od = umem_alloc(sizeof(ztest_od_t), UMEM_NOFAIL);
4939 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
4941 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
4942 umem_free(od, sizeof(ztest_od_t));
4947 * Take the name lock as writer to prevent anyone else from changing
4948 * the pool and dataset properies we need to maintain during this test.
4950 (void) rw_enter(&ztest_name_lock, RW_WRITER);
4952 if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
4954 ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
4956 (void) rw_exit(&ztest_name_lock);
4957 umem_free(od, sizeof(ztest_od_t));
4961 object = od[0].od_object;
4962 blocksize = od[0].od_blocksize;
4963 pattern = zs->zs_guid ^ dmu_objset_fsid_guid(os);
4965 ASSERT(object != 0);
4967 tx = dmu_tx_create(os);
4968 dmu_tx_hold_write(tx, object, 0, copies * blocksize);
4969 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
4971 (void) rw_exit(&ztest_name_lock);
4972 umem_free(od, sizeof(ztest_od_t));
4977 * Write all the copies of our block.
4979 for (i = 0; i < copies; i++) {
4980 uint64_t offset = i * blocksize;
4981 VERIFY(dmu_buf_hold(os, object, offset, FTAG, &db,
4982 DMU_READ_NO_PREFETCH) == 0);
4983 ASSERT(db->db_offset == offset);
4984 ASSERT(db->db_size == blocksize);
4985 ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
4986 ztest_pattern_match(db->db_data, db->db_size, 0ULL));
4987 dmu_buf_will_fill(db, tx);
4988 ztest_pattern_set(db->db_data, db->db_size, pattern);
4989 dmu_buf_rele(db, FTAG);
4993 txg_wait_synced(spa_get_dsl(spa), txg);
4996 * Find out what block we got.
4998 VERIFY(dmu_buf_hold(os, object, 0, FTAG, &db,
4999 DMU_READ_NO_PREFETCH) == 0);
5000 blk = *((dmu_buf_impl_t *)db)->db_blkptr;
5001 dmu_buf_rele(db, FTAG);
5004 * Damage the block. Dedup-ditto will save us when we read it later.
5006 psize = BP_GET_PSIZE(&blk);
5007 buf = zio_buf_alloc(psize);
5008 ztest_pattern_set(buf, psize, ~pattern);
5010 (void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
5011 buf, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
5012 ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
5014 zio_buf_free(buf, psize);
5016 (void) rw_exit(&ztest_name_lock);
5017 umem_free(od, sizeof(ztest_od_t));
5025 ztest_scrub(ztest_ds_t *zd, uint64_t id)
5027 spa_t *spa = ztest_spa;
5029 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5030 (void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
5031 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5035 * Change the guid for the pool.
5039 ztest_reguid(ztest_ds_t *zd, uint64_t id)
5041 spa_t *spa = ztest_spa;
5042 uint64_t orig, load;
5044 orig = spa_guid(spa);
5045 load = spa_load_guid(spa);
5046 if (spa_change_guid(spa) != 0)
5049 if (ztest_opts.zo_verbose >= 3) {
5050 (void) printf("Changed guid old %llu -> %llu\n",
5051 (u_longlong_t)orig, (u_longlong_t)spa_guid(spa));
5054 VERIFY3U(orig, !=, spa_guid(spa));
5055 VERIFY3U(load, ==, spa_load_guid(spa));
5059 * Rename the pool to a different name and then rename it back.
5063 ztest_spa_rename(ztest_ds_t *zd, uint64_t id)
5065 char *oldname, *newname;
5068 (void) rw_enter(&ztest_name_lock, RW_WRITER);
5070 oldname = ztest_opts.zo_pool;
5071 newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
5072 (void) strcpy(newname, oldname);
5073 (void) strcat(newname, "_tmp");
5078 VERIFY3U(0, ==, spa_rename(oldname, newname));
5081 * Try to open it under the old name, which shouldn't exist
5083 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5086 * Open it under the new name and make sure it's still the same spa_t.
5088 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
5090 ASSERT(spa == ztest_spa);
5091 spa_close(spa, FTAG);
5094 * Rename it back to the original
5096 VERIFY3U(0, ==, spa_rename(newname, oldname));
5099 * Make sure it can still be opened
5101 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
5103 ASSERT(spa == ztest_spa);
5104 spa_close(spa, FTAG);
5106 umem_free(newname, strlen(newname) + 1);
5108 (void) rw_exit(&ztest_name_lock);
5112 * Verify pool integrity by running zdb.
5115 ztest_run_zdb(char *pool)
5123 bin = umem_alloc(MAXPATHLEN + MAXNAMELEN + 20, UMEM_NOFAIL);
5124 zdb = umem_alloc(MAXPATHLEN + MAXNAMELEN + 20, UMEM_NOFAIL);
5125 zbuf = umem_alloc(1024, UMEM_NOFAIL);
5127 VERIFY(realpath(getexecname(), bin) != NULL);
5128 if (strncmp(bin, "/usr/sbin/ztest", 15) == 0) {
5129 strcpy(bin, "/usr/sbin/zdb"); /* Installed */
5130 } else if (strncmp(bin, "/sbin/ztest", 11) == 0) {
5131 strcpy(bin, "/sbin/zdb"); /* Installed */
5133 strstr(bin, "/ztest/")[0] = '\0'; /* In-tree */
5134 strcat(bin, "/zdb/zdb");
5138 "%s -bcc%s%s -U %s %s",
5140 ztest_opts.zo_verbose >= 3 ? "s" : "",
5141 ztest_opts.zo_verbose >= 4 ? "v" : "",
5145 if (ztest_opts.zo_verbose >= 5)
5146 (void) printf("Executing %s\n", strstr(zdb, "zdb "));
5148 fp = popen(zdb, "r");
5150 while (fgets(zbuf, 1024, fp) != NULL)
5151 if (ztest_opts.zo_verbose >= 3)
5152 (void) printf("%s", zbuf);
5154 status = pclose(fp);
5159 ztest_dump_core = 0;
5160 if (WIFEXITED(status))
5161 fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
5163 fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
5165 umem_free(bin, MAXPATHLEN + MAXNAMELEN + 20);
5166 umem_free(zdb, MAXPATHLEN + MAXNAMELEN + 20);
5167 umem_free(zbuf, 1024);
5171 ztest_walk_pool_directory(char *header)
5175 if (ztest_opts.zo_verbose >= 6)
5176 (void) printf("%s\n", header);
5178 mutex_enter(&spa_namespace_lock);
5179 while ((spa = spa_next(spa)) != NULL)
5180 if (ztest_opts.zo_verbose >= 6)
5181 (void) printf("\t%s\n", spa_name(spa));
5182 mutex_exit(&spa_namespace_lock);
5186 ztest_spa_import_export(char *oldname, char *newname)
5188 nvlist_t *config, *newconfig;
5192 if (ztest_opts.zo_verbose >= 4) {
5193 (void) printf("import/export: old = %s, new = %s\n",
5198 * Clean up from previous runs.
5200 (void) spa_destroy(newname);
5203 * Get the pool's configuration and guid.
5205 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
5208 * Kick off a scrub to tickle scrub/export races.
5210 if (ztest_random(2) == 0)
5211 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5213 pool_guid = spa_guid(spa);
5214 spa_close(spa, FTAG);
5216 ztest_walk_pool_directory("pools before export");
5221 VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
5223 ztest_walk_pool_directory("pools after export");
5228 newconfig = spa_tryimport(config);
5229 ASSERT(newconfig != NULL);
5230 nvlist_free(newconfig);
5233 * Import it under the new name.
5235 VERIFY3U(0, ==, spa_import(newname, config, NULL, 0));
5237 ztest_walk_pool_directory("pools after import");
5240 * Try to import it again -- should fail with EEXIST.
5242 VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL, 0));
5245 * Try to import it under a different name -- should fail with EEXIST.
5247 VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL, 0));
5250 * Verify that the pool is no longer visible under the old name.
5252 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5255 * Verify that we can open and close the pool using the new name.
5257 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
5258 ASSERT(pool_guid == spa_guid(spa));
5259 spa_close(spa, FTAG);
5261 nvlist_free(config);
5265 ztest_resume(spa_t *spa)
5267 if (spa_suspended(spa) && ztest_opts.zo_verbose >= 6)
5268 (void) printf("resuming from suspended state\n");
5269 spa_vdev_state_enter(spa, SCL_NONE);
5270 vdev_clear(spa, NULL);
5271 (void) spa_vdev_state_exit(spa, NULL, 0);
5272 (void) zio_resume(spa);
5276 ztest_resume_thread(void *arg)
5280 while (!ztest_exiting) {
5281 if (spa_suspended(spa))
5283 (void) poll(NULL, 0, 100);
5295 ztest_deadman_alarm(int sig)
5297 fatal(0, "failed to complete within %d seconds of deadline", GRACE);
5302 ztest_execute(int test, ztest_info_t *zi, uint64_t id)
5304 ztest_ds_t *zd = &ztest_ds[id % ztest_opts.zo_datasets];
5305 ztest_shared_callstate_t *zc = ZTEST_GET_SHARED_CALLSTATE(test);
5306 hrtime_t functime = gethrtime();
5309 for (i = 0; i < zi->zi_iters; i++)
5310 zi->zi_func(zd, id);
5312 functime = gethrtime() - functime;
5314 atomic_add_64(&zc->zc_count, 1);
5315 atomic_add_64(&zc->zc_time, functime);
5317 if (ztest_opts.zo_verbose >= 4) {
5319 (void) dladdr((void *)zi->zi_func, &dli);
5320 (void) printf("%6.2f sec in %s\n",
5321 (double)functime / NANOSEC, dli.dli_sname);
5326 ztest_thread(void *arg)
5329 uint64_t id = (uintptr_t)arg;
5330 ztest_shared_t *zs = ztest_shared;
5334 ztest_shared_callstate_t *zc;
5336 while ((now = gethrtime()) < zs->zs_thread_stop) {
5338 * See if it's time to force a crash.
5340 if (now > zs->zs_thread_kill)
5344 * If we're getting ENOSPC with some regularity, stop.
5346 if (zs->zs_enospc_count > 10)
5350 * Pick a random function to execute.
5352 rand = ztest_random(ZTEST_FUNCS);
5353 zi = &ztest_info[rand];
5354 zc = ZTEST_GET_SHARED_CALLSTATE(rand);
5355 call_next = zc->zc_next;
5357 if (now >= call_next &&
5358 atomic_cas_64(&zc->zc_next, call_next, call_next +
5359 ztest_random(2 * zi->zi_interval[0] + 1)) == call_next) {
5360 ztest_execute(rand, zi, id);
5370 ztest_dataset_name(char *dsname, char *pool, int d)
5372 (void) snprintf(dsname, MAXNAMELEN, "%s/ds_%d", pool, d);
5376 ztest_dataset_destroy(int d)
5378 char name[MAXNAMELEN];
5381 ztest_dataset_name(name, ztest_opts.zo_pool, d);
5383 if (ztest_opts.zo_verbose >= 3)
5384 (void) printf("Destroying %s to free up space\n", name);
5387 * Cleanup any non-standard clones and snapshots. In general,
5388 * ztest thread t operates on dataset (t % zopt_datasets),
5389 * so there may be more than one thing to clean up.
5391 for (t = d; t < ztest_opts.zo_threads;
5392 t += ztest_opts.zo_datasets)
5393 ztest_dsl_dataset_cleanup(name, t);
5395 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
5396 DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
5400 ztest_dataset_dirobj_verify(ztest_ds_t *zd)
5402 uint64_t usedobjs, dirobjs, scratch;
5405 * ZTEST_DIROBJ is the object directory for the entire dataset.
5406 * Therefore, the number of objects in use should equal the
5407 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
5408 * If not, we have an object leak.
5410 * Note that we can only check this in ztest_dataset_open(),
5411 * when the open-context and syncing-context values agree.
5412 * That's because zap_count() returns the open-context value,
5413 * while dmu_objset_space() returns the rootbp fill count.
5415 VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
5416 dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
5417 ASSERT3U(dirobjs + 1, ==, usedobjs);
5421 ztest_dataset_open(int d)
5423 ztest_ds_t *zd = &ztest_ds[d];
5424 uint64_t committed_seq = ZTEST_GET_SHARED_DS(d)->zd_seq;
5427 char name[MAXNAMELEN];
5430 ztest_dataset_name(name, ztest_opts.zo_pool, d);
5432 (void) rw_enter(&ztest_name_lock, RW_READER);
5434 error = ztest_dataset_create(name);
5435 if (error == ENOSPC) {
5436 (void) rw_exit(&ztest_name_lock);
5437 ztest_record_enospc(FTAG);
5440 ASSERT(error == 0 || error == EEXIST);
5442 VERIFY3U(dmu_objset_hold(name, zd, &os), ==, 0);
5443 (void) rw_exit(&ztest_name_lock);
5445 ztest_zd_init(zd, ZTEST_GET_SHARED_DS(d), os);
5447 zilog = zd->zd_zilog;
5449 if (zilog->zl_header->zh_claim_lr_seq != 0 &&
5450 zilog->zl_header->zh_claim_lr_seq < committed_seq)
5451 fatal(0, "missing log records: claimed %llu < committed %llu",
5452 zilog->zl_header->zh_claim_lr_seq, committed_seq);
5454 ztest_dataset_dirobj_verify(zd);
5456 zil_replay(os, zd, ztest_replay_vector);
5458 ztest_dataset_dirobj_verify(zd);
5460 if (ztest_opts.zo_verbose >= 6)
5461 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
5463 (u_longlong_t)zilog->zl_parse_blk_count,
5464 (u_longlong_t)zilog->zl_parse_lr_count,
5465 (u_longlong_t)zilog->zl_replaying_seq);
5467 zilog = zil_open(os, ztest_get_data);
5469 if (zilog->zl_replaying_seq != 0 &&
5470 zilog->zl_replaying_seq < committed_seq)
5471 fatal(0, "missing log records: replayed %llu < committed %llu",
5472 zilog->zl_replaying_seq, committed_seq);
5478 ztest_dataset_close(int d)
5480 ztest_ds_t *zd = &ztest_ds[d];
5482 zil_close(zd->zd_zilog);
5483 dmu_objset_rele(zd->zd_os, zd);
5489 * Kick off threads to run tests on all datasets in parallel.
5492 ztest_run(ztest_shared_t *zs)
5497 kthread_t *resume_thread;
5502 ztest_exiting = B_FALSE;
5505 * Initialize parent/child shared state.
5507 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
5508 rw_init(&ztest_name_lock, NULL, RW_DEFAULT, NULL);
5510 zs->zs_thread_start = gethrtime();
5511 zs->zs_thread_stop =
5512 zs->zs_thread_start + ztest_opts.zo_passtime * NANOSEC;
5513 zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
5514 zs->zs_thread_kill = zs->zs_thread_stop;
5515 if (ztest_random(100) < ztest_opts.zo_killrate) {
5516 zs->zs_thread_kill -=
5517 ztest_random(ztest_opts.zo_passtime * NANOSEC);
5520 mutex_init(&zcl.zcl_callbacks_lock, NULL, MUTEX_DEFAULT, NULL);
5522 list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
5523 offsetof(ztest_cb_data_t, zcd_node));
5528 kernel_init(FREAD | FWRITE);
5529 VERIFY(spa_open(ztest_opts.zo_pool, &spa, FTAG) == 0);
5530 spa->spa_debug = B_TRUE;
5533 VERIFY3U(0, ==, dmu_objset_hold(ztest_opts.zo_pool, FTAG, &os));
5534 zs->zs_guid = dmu_objset_fsid_guid(os);
5535 dmu_objset_rele(os, FTAG);
5537 spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
5540 * We don't expect the pool to suspend unless maxfaults == 0,
5541 * in which case ztest_fault_inject() temporarily takes away
5542 * the only valid replica.
5544 if (MAXFAULTS() == 0)
5545 spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
5547 spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
5550 * Create a thread to periodically resume suspended I/O.
5552 VERIFY3P((resume_thread = zk_thread_create(NULL, 0,
5553 (thread_func_t)ztest_resume_thread, spa, TS_RUN, NULL, 0, 0,
5554 PTHREAD_CREATE_JOINABLE)), !=, NULL);
5558 * Set a deadman alarm to abort() if we hang.
5560 signal(SIGALRM, ztest_deadman_alarm);
5561 alarm((zs->zs_thread_stop - zs->zs_thread_start) / NANOSEC + GRACE);
5565 * Verify that we can safely inquire about about any object,
5566 * whether it's allocated or not. To make it interesting,
5567 * we probe a 5-wide window around each power of two.
5568 * This hits all edge cases, including zero and the max.
5570 for (t = 0; t < 64; t++) {
5571 for (d = -5; d <= 5; d++) {
5572 error = dmu_object_info(spa->spa_meta_objset,
5573 (1ULL << t) + d, NULL);
5574 ASSERT(error == 0 || error == ENOENT ||
5580 * If we got any ENOSPC errors on the previous run, destroy something.
5582 if (zs->zs_enospc_count != 0) {
5583 int d = ztest_random(ztest_opts.zo_datasets);
5584 ztest_dataset_destroy(d);
5586 zs->zs_enospc_count = 0;
5588 tid = umem_zalloc(ztest_opts.zo_threads * sizeof (kt_did_t),
5591 if (ztest_opts.zo_verbose >= 4)
5592 (void) printf("starting main threads...\n");
5595 * Kick off all the tests that run in parallel.
5597 for (t = 0; t < ztest_opts.zo_threads; t++) {
5600 if (t < ztest_opts.zo_datasets &&
5601 ztest_dataset_open(t) != 0)
5604 VERIFY3P(thread = zk_thread_create(NULL, 0,
5605 (thread_func_t)ztest_thread,
5606 (void *)(uintptr_t)t, TS_RUN, NULL, 0, 0,
5607 PTHREAD_CREATE_JOINABLE), !=, NULL);
5608 tid[t] = thread->t_tid;
5612 * Wait for all of the tests to complete. We go in reverse order
5613 * so we don't close datasets while threads are still using them.
5615 for (t = ztest_opts.zo_threads - 1; t >= 0; t--) {
5616 thread_join(tid[t]);
5617 if (t < ztest_opts.zo_datasets)
5618 ztest_dataset_close(t);
5621 txg_wait_synced(spa_get_dsl(spa), 0);
5623 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
5624 zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
5626 umem_free(tid, ztest_opts.zo_threads * sizeof (kt_did_t));
5628 /* Kill the resume thread */
5629 ztest_exiting = B_TRUE;
5630 thread_join(resume_thread->t_tid);
5634 * Right before closing the pool, kick off a bunch of async I/O;
5635 * spa_close() should wait for it to complete.
5637 for (object = 1; object < 50; object++)
5638 dmu_prefetch(spa->spa_meta_objset, object, 0, 1ULL << 20);
5640 /* Verify that at least one commit cb was called in a timely fashion */
5641 if (zc_cb_counter >= ZTEST_COMMIT_CB_MIN_REG)
5642 VERIFY3U(zc_min_txg_delay, ==, 0);
5644 spa_close(spa, FTAG);
5647 * Verify that we can loop over all pools.
5649 mutex_enter(&spa_namespace_lock);
5650 for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
5651 if (ztest_opts.zo_verbose > 3)
5652 (void) printf("spa_next: found %s\n", spa_name(spa));
5653 mutex_exit(&spa_namespace_lock);
5656 * Verify that we can export the pool and reimport it under a
5659 if (ztest_random(2) == 0) {
5660 char name[MAXNAMELEN];
5661 (void) snprintf(name, MAXNAMELEN, "%s_import",
5662 ztest_opts.zo_pool);
5663 ztest_spa_import_export(ztest_opts.zo_pool, name);
5664 ztest_spa_import_export(name, ztest_opts.zo_pool);
5669 list_destroy(&zcl.zcl_callbacks);
5670 mutex_destroy(&zcl.zcl_callbacks_lock);
5671 rw_destroy(&ztest_name_lock);
5672 mutex_destroy(&ztest_vdev_lock);
5678 ztest_ds_t *zd = &ztest_ds[0];
5682 if (ztest_opts.zo_verbose >= 3)
5683 (void) printf("testing spa_freeze()...\n");
5685 kernel_init(FREAD | FWRITE);
5686 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
5687 VERIFY3U(0, ==, ztest_dataset_open(0));
5690 * Force the first log block to be transactionally allocated.
5691 * We have to do this before we freeze the pool -- otherwise
5692 * the log chain won't be anchored.
5694 while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
5695 ztest_dmu_object_alloc_free(zd, 0);
5696 zil_commit(zd->zd_zilog, 0);
5699 txg_wait_synced(spa_get_dsl(spa), 0);
5702 * Freeze the pool. This stops spa_sync() from doing anything,
5703 * so that the only way to record changes from now on is the ZIL.
5708 * Run tests that generate log records but don't alter the pool config
5709 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
5710 * We do a txg_wait_synced() after each iteration to force the txg
5711 * to increase well beyond the last synced value in the uberblock.
5712 * The ZIL should be OK with that.
5714 while (ztest_random(10) != 0 &&
5715 numloops++ < ztest_opts.zo_maxloops) {
5716 ztest_dmu_write_parallel(zd, 0);
5717 ztest_dmu_object_alloc_free(zd, 0);
5718 txg_wait_synced(spa_get_dsl(spa), 0);
5722 * Commit all of the changes we just generated.
5724 zil_commit(zd->zd_zilog, 0);
5725 txg_wait_synced(spa_get_dsl(spa), 0);
5728 * Close our dataset and close the pool.
5730 ztest_dataset_close(0);
5731 spa_close(spa, FTAG);
5735 * Open and close the pool and dataset to induce log replay.
5737 kernel_init(FREAD | FWRITE);
5738 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
5739 VERIFY3U(0, ==, ztest_dataset_open(0));
5740 ztest_dataset_close(0);
5741 spa_close(spa, FTAG);
5746 print_time(hrtime_t t, char *timebuf)
5748 hrtime_t s = t / NANOSEC;
5749 hrtime_t m = s / 60;
5750 hrtime_t h = m / 60;
5751 hrtime_t d = h / 24;
5760 (void) sprintf(timebuf,
5761 "%llud%02lluh%02llum%02llus", d, h, m, s);
5763 (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
5765 (void) sprintf(timebuf, "%llum%02llus", m, s);
5767 (void) sprintf(timebuf, "%llus", s);
5771 make_random_props(void)
5775 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
5776 if (ztest_random(2) == 0)
5778 VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
5784 * Create a storage pool with the given name and initial vdev size.
5785 * Then test spa_freeze() functionality.
5788 ztest_init(ztest_shared_t *zs)
5791 nvlist_t *nvroot, *props;
5794 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
5795 rw_init(&ztest_name_lock, NULL, RW_DEFAULT, NULL);
5797 kernel_init(FREAD | FWRITE);
5800 * Create the storage pool.
5802 (void) spa_destroy(ztest_opts.zo_pool);
5803 ztest_shared->zs_vdev_next_leaf = 0;
5805 zs->zs_mirrors = ztest_opts.zo_mirrors;
5806 nvroot = make_vdev_root(NULL, NULL, ztest_opts.zo_vdev_size, 0,
5807 0, ztest_opts.zo_raidz, zs->zs_mirrors, 1);
5808 props = make_random_props();
5809 for (i = 0; i < SPA_FEATURES; i++) {
5811 VERIFY3S(-1, !=, asprintf(&buf, "feature@%s",
5812 spa_feature_table[i].fi_uname));
5813 VERIFY3U(0, ==, nvlist_add_uint64(props, buf, 0));
5816 VERIFY3U(0, ==, spa_create(ztest_opts.zo_pool, nvroot, props,
5818 nvlist_free(nvroot);
5820 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
5821 zs->zs_metaslab_sz =
5822 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
5823 spa_close(spa, FTAG);
5827 ztest_run_zdb(ztest_opts.zo_pool);
5831 ztest_run_zdb(ztest_opts.zo_pool);
5833 rw_destroy(&ztest_name_lock);
5834 mutex_destroy(&ztest_vdev_lock);
5840 static char ztest_name_data[] = "/tmp/ztest.data.XXXXXX";
5842 ztest_fd_data = mkstemp(ztest_name_data);
5843 ASSERT3S(ztest_fd_data, >=, 0);
5844 (void) unlink(ztest_name_data);
5848 shared_data_size(ztest_shared_hdr_t *hdr)
5852 size = hdr->zh_hdr_size;
5853 size += hdr->zh_opts_size;
5854 size += hdr->zh_size;
5855 size += hdr->zh_stats_size * hdr->zh_stats_count;
5856 size += hdr->zh_ds_size * hdr->zh_ds_count;
5865 ztest_shared_hdr_t *hdr;
5867 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
5868 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
5869 ASSERT(hdr != MAP_FAILED);
5871 VERIFY3U(0, ==, ftruncate(ztest_fd_data, sizeof (ztest_shared_hdr_t)));
5873 hdr->zh_hdr_size = sizeof (ztest_shared_hdr_t);
5874 hdr->zh_opts_size = sizeof (ztest_shared_opts_t);
5875 hdr->zh_size = sizeof (ztest_shared_t);
5876 hdr->zh_stats_size = sizeof (ztest_shared_callstate_t);
5877 hdr->zh_stats_count = ZTEST_FUNCS;
5878 hdr->zh_ds_size = sizeof (ztest_shared_ds_t);
5879 hdr->zh_ds_count = ztest_opts.zo_datasets;
5881 size = shared_data_size(hdr);
5882 VERIFY3U(0, ==, ftruncate(ztest_fd_data, size));
5884 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
5891 ztest_shared_hdr_t *hdr;
5894 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
5895 PROT_READ, MAP_SHARED, ztest_fd_data, 0);
5896 ASSERT(hdr != MAP_FAILED);
5898 size = shared_data_size(hdr);
5900 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
5901 hdr = ztest_shared_hdr = (void *)mmap(0, P2ROUNDUP(size, getpagesize()),
5902 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
5903 ASSERT(hdr != MAP_FAILED);
5904 buf = (uint8_t *)hdr;
5906 offset = hdr->zh_hdr_size;
5907 ztest_shared_opts = (void *)&buf[offset];
5908 offset += hdr->zh_opts_size;
5909 ztest_shared = (void *)&buf[offset];
5910 offset += hdr->zh_size;
5911 ztest_shared_callstate = (void *)&buf[offset];
5912 offset += hdr->zh_stats_size * hdr->zh_stats_count;
5913 ztest_shared_ds = (void *)&buf[offset];
5917 exec_child(char *cmd, char *libpath, boolean_t ignorekill, int *statusp)
5921 char *cmdbuf = NULL;
5926 cmdbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
5927 (void) strlcpy(cmdbuf, getexecname(), MAXPATHLEN);
5932 fatal(1, "fork failed");
5934 if (pid == 0) { /* child */
5935 char *emptyargv[2] = { cmd, NULL };
5936 char fd_data_str[12];
5938 struct rlimit rl = { 1024, 1024 };
5939 (void) setrlimit(RLIMIT_NOFILE, &rl);
5941 (void) close(ztest_fd_rand);
5942 VERIFY(11 >= snprintf(fd_data_str, 12, "%d", ztest_fd_data));
5943 VERIFY(0 == setenv("ZTEST_FD_DATA", fd_data_str, 1));
5945 (void) enable_extended_FILE_stdio(-1, -1);
5946 if (libpath != NULL)
5947 VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath, 1));
5948 (void) execv(cmd, emptyargv);
5949 ztest_dump_core = B_FALSE;
5950 fatal(B_TRUE, "exec failed: %s", cmd);
5953 if (cmdbuf != NULL) {
5954 umem_free(cmdbuf, MAXPATHLEN);
5958 while (waitpid(pid, &status, 0) != pid)
5960 if (statusp != NULL)
5963 if (WIFEXITED(status)) {
5964 if (WEXITSTATUS(status) != 0) {
5965 (void) fprintf(stderr, "child exited with code %d\n",
5966 WEXITSTATUS(status));
5970 } else if (WIFSIGNALED(status)) {
5971 if (!ignorekill || WTERMSIG(status) != SIGKILL) {
5972 (void) fprintf(stderr, "child died with signal %d\n",
5978 (void) fprintf(stderr, "something strange happened to child\n");
5985 ztest_run_init(void)
5989 ztest_shared_t *zs = ztest_shared;
5991 ASSERT(ztest_opts.zo_init != 0);
5994 * Blow away any existing copy of zpool.cache
5996 (void) remove(spa_config_path);
5999 * Create and initialize our storage pool.
6001 for (i = 1; i <= ztest_opts.zo_init; i++) {
6002 bzero(zs, sizeof (ztest_shared_t));
6003 if (ztest_opts.zo_verbose >= 3 &&
6004 ztest_opts.zo_init != 1) {
6005 (void) printf("ztest_init(), pass %d\n", i);
6012 main(int argc, char **argv)
6020 ztest_shared_callstate_t *zc;
6027 char *fd_data_str = getenv("ZTEST_FD_DATA");
6029 (void) setvbuf(stdout, NULL, _IOLBF, 0);
6031 ztest_fd_rand = open("/dev/urandom", O_RDONLY);
6032 ASSERT3S(ztest_fd_rand, >=, 0);
6035 dprintf_setup(&argc, argv);
6036 process_options(argc, argv);
6041 bcopy(&ztest_opts, ztest_shared_opts,
6042 sizeof (*ztest_shared_opts));
6044 ztest_fd_data = atoi(fd_data_str);
6046 bcopy(ztest_shared_opts, &ztest_opts, sizeof (ztest_opts));
6048 ASSERT3U(ztest_opts.zo_datasets, ==, ztest_shared_hdr->zh_ds_count);
6050 /* Override location of zpool.cache */
6051 VERIFY(asprintf((char **)&spa_config_path, "%s/zpool.cache",
6052 ztest_opts.zo_dir) != -1);
6054 ztest_ds = umem_alloc(ztest_opts.zo_datasets * sizeof (ztest_ds_t),
6059 metaslab_gang_bang = ztest_opts.zo_metaslab_gang_bang;
6060 metaslab_df_alloc_threshold =
6061 zs->zs_metaslab_df_alloc_threshold;
6070 hasalt = (strlen(ztest_opts.zo_alt_ztest) != 0);
6072 if (ztest_opts.zo_verbose >= 1) {
6073 (void) printf("%llu vdevs, %d datasets, %d threads,"
6074 " %llu seconds...\n",
6075 (u_longlong_t)ztest_opts.zo_vdevs,
6076 ztest_opts.zo_datasets,
6077 ztest_opts.zo_threads,
6078 (u_longlong_t)ztest_opts.zo_time);
6081 cmd = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
6082 (void) strlcpy(cmd, getexecname(), MAXNAMELEN);
6084 zs->zs_do_init = B_TRUE;
6085 if (strlen(ztest_opts.zo_alt_ztest) != 0) {
6086 if (ztest_opts.zo_verbose >= 1) {
6087 (void) printf("Executing older ztest for "
6088 "initialization: %s\n", ztest_opts.zo_alt_ztest);
6090 VERIFY(!exec_child(ztest_opts.zo_alt_ztest,
6091 ztest_opts.zo_alt_libpath, B_FALSE, NULL));
6093 VERIFY(!exec_child(NULL, NULL, B_FALSE, NULL));
6095 zs->zs_do_init = B_FALSE;
6097 zs->zs_proc_start = gethrtime();
6098 zs->zs_proc_stop = zs->zs_proc_start + ztest_opts.zo_time * NANOSEC;
6100 for (f = 0; f < ZTEST_FUNCS; f++) {
6101 zi = &ztest_info[f];
6102 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6103 if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
6104 zc->zc_next = UINT64_MAX;
6106 zc->zc_next = zs->zs_proc_start +
6107 ztest_random(2 * zi->zi_interval[0] + 1);
6111 * Run the tests in a loop. These tests include fault injection
6112 * to verify that self-healing data works, and forced crashes
6113 * to verify that we never lose on-disk consistency.
6115 while (gethrtime() < zs->zs_proc_stop) {
6120 * Initialize the workload counters for each function.
6122 for (f = 0; f < ZTEST_FUNCS; f++) {
6123 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6128 /* Set the allocation switch size */
6129 zs->zs_metaslab_df_alloc_threshold =
6130 ztest_random(zs->zs_metaslab_sz / 4) + 1;
6132 if (!hasalt || ztest_random(2) == 0) {
6133 if (hasalt && ztest_opts.zo_verbose >= 1) {
6134 (void) printf("Executing newer ztest: %s\n",
6138 killed = exec_child(cmd, NULL, B_TRUE, &status);
6140 if (hasalt && ztest_opts.zo_verbose >= 1) {
6141 (void) printf("Executing older ztest: %s\n",
6142 ztest_opts.zo_alt_ztest);
6145 killed = exec_child(ztest_opts.zo_alt_ztest,
6146 ztest_opts.zo_alt_libpath, B_TRUE, &status);
6153 if (ztest_opts.zo_verbose >= 1) {
6154 hrtime_t now = gethrtime();
6156 now = MIN(now, zs->zs_proc_stop);
6157 print_time(zs->zs_proc_stop - now, timebuf);
6158 nicenum(zs->zs_space, numbuf);
6160 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
6161 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
6163 WIFEXITED(status) ? "Complete" : "SIGKILL",
6164 (u_longlong_t)zs->zs_enospc_count,
6165 100.0 * zs->zs_alloc / zs->zs_space,
6167 100.0 * (now - zs->zs_proc_start) /
6168 (ztest_opts.zo_time * NANOSEC), timebuf);
6171 if (ztest_opts.zo_verbose >= 2) {
6172 (void) printf("\nWorkload summary:\n\n");
6173 (void) printf("%7s %9s %s\n",
6174 "Calls", "Time", "Function");
6175 (void) printf("%7s %9s %s\n",
6176 "-----", "----", "--------");
6177 for (f = 0; f < ZTEST_FUNCS; f++) {
6180 zi = &ztest_info[f];
6181 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6182 print_time(zc->zc_time, timebuf);
6183 (void) dladdr((void *)zi->zi_func, &dli);
6184 (void) printf("%7llu %9s %s\n",
6185 (u_longlong_t)zc->zc_count, timebuf,
6188 (void) printf("\n");
6192 * It's possible that we killed a child during a rename test,
6193 * in which case we'll have a 'ztest_tmp' pool lying around
6194 * instead of 'ztest'. Do a blind rename in case this happened.
6197 if (spa_open(ztest_opts.zo_pool, &spa, FTAG) == 0) {
6198 spa_close(spa, FTAG);
6200 char tmpname[MAXNAMELEN];
6202 kernel_init(FREAD | FWRITE);
6203 (void) snprintf(tmpname, sizeof (tmpname), "%s_tmp",
6204 ztest_opts.zo_pool);
6205 (void) spa_rename(tmpname, ztest_opts.zo_pool);
6209 ztest_run_zdb(ztest_opts.zo_pool);
6212 if (ztest_opts.zo_verbose >= 1) {
6214 (void) printf("%d runs of older ztest: %s\n", older,
6215 ztest_opts.zo_alt_ztest);
6216 (void) printf("%d runs of newer ztest: %s\n", newer,
6219 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
6220 kills, iters - kills, (100.0 * kills) / MAX(1, iters));
6223 umem_free(cmd, MAXNAMELEN);