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>
113 #include <stdio_ext.h>
121 #include <sys/fs/zfs.h>
122 #include <libnvpair.h>
124 static int ztest_fd_data = -1;
125 static int ztest_fd_rand = -1;
127 typedef struct ztest_shared_hdr {
128 uint64_t zh_hdr_size;
129 uint64_t zh_opts_size;
131 uint64_t zh_stats_size;
132 uint64_t zh_stats_count;
134 uint64_t zh_ds_count;
135 } ztest_shared_hdr_t;
137 static ztest_shared_hdr_t *ztest_shared_hdr;
139 typedef struct ztest_shared_opts {
140 char zo_pool[MAXNAMELEN];
141 char zo_dir[MAXNAMELEN];
142 char zo_alt_ztest[MAXNAMELEN];
143 char zo_alt_libpath[MAXNAMELEN];
145 uint64_t zo_vdevtime;
153 uint64_t zo_passtime;
154 uint64_t zo_killrate;
158 uint64_t zo_maxloops;
159 uint64_t zo_metaslab_gang_bang;
160 } ztest_shared_opts_t;
162 static const ztest_shared_opts_t ztest_opts_defaults = {
163 .zo_pool = { 'z', 't', 'e', 's', 't', '\0' },
164 .zo_dir = { '/', 't', 'm', 'p', '\0' },
165 .zo_alt_ztest = { '\0' },
166 .zo_alt_libpath = { '\0' },
168 .zo_ashift = SPA_MINBLOCKSHIFT,
171 .zo_raidz_parity = 1,
172 .zo_vdev_size = SPA_MINDEVSIZE,
175 .zo_passtime = 60, /* 60 seconds */
176 .zo_killrate = 70, /* 70% kill rate */
179 .zo_time = 300, /* 5 minutes */
180 .zo_maxloops = 50, /* max loops during spa_freeze() */
181 .zo_metaslab_gang_bang = 32 << 10
184 extern uint64_t metaslab_gang_bang;
185 extern uint64_t metaslab_df_alloc_threshold;
187 static ztest_shared_opts_t *ztest_shared_opts;
188 static ztest_shared_opts_t ztest_opts;
190 typedef struct ztest_shared_ds {
194 static ztest_shared_ds_t *ztest_shared_ds;
195 #define ZTEST_GET_SHARED_DS(d) (&ztest_shared_ds[d])
197 #define BT_MAGIC 0x123456789abcdefULL
198 #define MAXFAULTS() \
199 (MAX(zs->zs_mirrors, 1) * (ztest_opts.zo_raidz_parity + 1) - 1)
203 ZTEST_IO_WRITE_PATTERN,
204 ZTEST_IO_WRITE_ZEROES,
210 typedef struct ztest_block_tag {
220 typedef struct bufwad {
227 * XXX -- fix zfs range locks to be generic so we can use them here.
249 #define ZTEST_RANGE_LOCKS 64
250 #define ZTEST_OBJECT_LOCKS 64
253 * Object descriptor. Used as a template for object lookup/create/remove.
255 typedef struct ztest_od {
258 dmu_object_type_t od_type;
259 dmu_object_type_t od_crtype;
260 uint64_t od_blocksize;
261 uint64_t od_crblocksize;
264 char od_name[MAXNAMELEN];
270 typedef struct ztest_ds {
271 ztest_shared_ds_t *zd_shared;
273 krwlock_t zd_zilog_lock;
275 ztest_od_t *zd_od; /* debugging aid */
276 char zd_name[MAXNAMELEN];
277 kmutex_t zd_dirobj_lock;
278 rll_t zd_object_lock[ZTEST_OBJECT_LOCKS];
279 rll_t zd_range_lock[ZTEST_RANGE_LOCKS];
283 * Per-iteration state.
285 typedef void ztest_func_t(ztest_ds_t *zd, uint64_t id);
287 typedef struct ztest_info {
288 ztest_func_t *zi_func; /* test function */
289 uint64_t zi_iters; /* iterations per execution */
290 uint64_t *zi_interval; /* execute every <interval> seconds */
293 typedef struct ztest_shared_callstate {
294 uint64_t zc_count; /* per-pass count */
295 uint64_t zc_time; /* per-pass time */
296 uint64_t zc_next; /* next time to call this function */
297 } ztest_shared_callstate_t;
299 static ztest_shared_callstate_t *ztest_shared_callstate;
300 #define ZTEST_GET_SHARED_CALLSTATE(c) (&ztest_shared_callstate[c])
303 * Note: these aren't static because we want dladdr() to work.
305 ztest_func_t ztest_dmu_read_write;
306 ztest_func_t ztest_dmu_write_parallel;
307 ztest_func_t ztest_dmu_object_alloc_free;
308 ztest_func_t ztest_dmu_commit_callbacks;
309 ztest_func_t ztest_zap;
310 ztest_func_t ztest_zap_parallel;
311 ztest_func_t ztest_zil_commit;
312 ztest_func_t ztest_zil_remount;
313 ztest_func_t ztest_dmu_read_write_zcopy;
314 ztest_func_t ztest_dmu_objset_create_destroy;
315 ztest_func_t ztest_dmu_prealloc;
316 ztest_func_t ztest_fzap;
317 ztest_func_t ztest_dmu_snapshot_create_destroy;
318 ztest_func_t ztest_dsl_prop_get_set;
319 ztest_func_t ztest_spa_prop_get_set;
320 ztest_func_t ztest_spa_create_destroy;
321 ztest_func_t ztest_fault_inject;
322 ztest_func_t ztest_ddt_repair;
323 ztest_func_t ztest_dmu_snapshot_hold;
324 ztest_func_t ztest_spa_rename;
325 ztest_func_t ztest_scrub;
326 ztest_func_t ztest_dsl_dataset_promote_busy;
327 ztest_func_t ztest_vdev_attach_detach;
328 ztest_func_t ztest_vdev_LUN_growth;
329 ztest_func_t ztest_vdev_add_remove;
330 ztest_func_t ztest_vdev_aux_add_remove;
331 ztest_func_t ztest_split_pool;
332 ztest_func_t ztest_reguid;
334 uint64_t zopt_always = 0ULL * NANOSEC; /* all the time */
335 uint64_t zopt_incessant = 1ULL * NANOSEC / 10; /* every 1/10 second */
336 uint64_t zopt_often = 1ULL * NANOSEC; /* every second */
337 uint64_t zopt_sometimes = 10ULL * NANOSEC; /* every 10 seconds */
338 uint64_t zopt_rarely = 60ULL * NANOSEC; /* every 60 seconds */
340 ztest_info_t ztest_info[] = {
341 { ztest_dmu_read_write, 1, &zopt_always },
342 { ztest_dmu_write_parallel, 10, &zopt_always },
343 { ztest_dmu_object_alloc_free, 1, &zopt_always },
344 { ztest_dmu_commit_callbacks, 1, &zopt_always },
345 { ztest_zap, 30, &zopt_always },
346 { ztest_zap_parallel, 100, &zopt_always },
347 { ztest_split_pool, 1, &zopt_always },
348 { ztest_zil_commit, 1, &zopt_incessant },
349 { ztest_zil_remount, 1, &zopt_sometimes },
350 { ztest_dmu_read_write_zcopy, 1, &zopt_often },
351 { ztest_dmu_objset_create_destroy, 1, &zopt_often },
352 { ztest_dsl_prop_get_set, 1, &zopt_often },
353 { ztest_spa_prop_get_set, 1, &zopt_sometimes },
355 { ztest_dmu_prealloc, 1, &zopt_sometimes },
357 { ztest_fzap, 1, &zopt_sometimes },
358 { ztest_dmu_snapshot_create_destroy, 1, &zopt_sometimes },
359 { ztest_spa_create_destroy, 1, &zopt_sometimes },
360 { ztest_fault_inject, 1, &zopt_sometimes },
361 { ztest_ddt_repair, 1, &zopt_sometimes },
362 { ztest_dmu_snapshot_hold, 1, &zopt_sometimes },
364 * The reguid test is currently broken. Disable it until
365 * we get around to fixing it.
368 { ztest_reguid, 1, &zopt_sometimes },
370 { ztest_spa_rename, 1, &zopt_rarely },
371 { ztest_scrub, 1, &zopt_rarely },
372 { ztest_dsl_dataset_promote_busy, 1, &zopt_rarely },
373 { ztest_vdev_attach_detach, 1, &zopt_rarely },
374 { ztest_vdev_LUN_growth, 1, &zopt_rarely },
375 { ztest_vdev_add_remove, 1,
376 &ztest_opts.zo_vdevtime },
377 { ztest_vdev_aux_add_remove, 1,
378 &ztest_opts.zo_vdevtime },
381 #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
384 * The following struct is used to hold a list of uncalled commit callbacks.
385 * The callbacks are ordered by txg number.
387 typedef struct ztest_cb_list {
388 kmutex_t zcl_callbacks_lock;
389 list_t zcl_callbacks;
393 * Stuff we need to share writably between parent and child.
395 typedef struct ztest_shared {
396 boolean_t zs_do_init;
397 hrtime_t zs_proc_start;
398 hrtime_t zs_proc_stop;
399 hrtime_t zs_thread_start;
400 hrtime_t zs_thread_stop;
401 hrtime_t zs_thread_kill;
402 uint64_t zs_enospc_count;
403 uint64_t zs_vdev_next_leaf;
404 uint64_t zs_vdev_aux;
409 uint64_t zs_metaslab_sz;
410 uint64_t zs_metaslab_df_alloc_threshold;
414 #define ID_PARALLEL -1ULL
416 static char ztest_dev_template[] = "%s/%s.%llua";
417 static char ztest_aux_template[] = "%s/%s.%s.%llu";
418 ztest_shared_t *ztest_shared;
420 static spa_t *ztest_spa = NULL;
421 static ztest_ds_t *ztest_ds;
423 static kmutex_t ztest_vdev_lock;
424 static krwlock_t ztest_name_lock;
426 static boolean_t ztest_dump_core = B_TRUE;
427 static boolean_t ztest_exiting;
429 /* Global commit callback list */
430 static ztest_cb_list_t zcl;
431 /* Commit cb delay */
432 static uint64_t zc_min_txg_delay = UINT64_MAX;
433 static int zc_cb_counter = 0;
436 * Minimum number of commit callbacks that need to be registered for us to check
437 * whether the minimum txg delay is acceptable.
439 #define ZTEST_COMMIT_CB_MIN_REG 100
442 * If a number of txgs equal to this threshold have been created after a commit
443 * callback has been registered but not called, then we assume there is an
444 * implementation bug.
446 #define ZTEST_COMMIT_CB_THRESH (TXG_CONCURRENT_STATES + 1000)
448 extern uint64_t metaslab_gang_bang;
449 extern uint64_t metaslab_df_alloc_threshold;
452 ZTEST_META_DNODE = 0,
457 static void usage(boolean_t) __NORETURN;
460 * These libumem hooks provide a reasonable set of defaults for the allocator's
461 * debugging facilities.
464 _umem_debug_init(void)
466 return ("default,verbose"); /* $UMEM_DEBUG setting */
470 _umem_logging_init(void)
472 return ("fail,contents"); /* $UMEM_LOGGING setting */
475 #define FATAL_MSG_SZ 1024
480 fatal(int do_perror, char *message, ...)
483 int save_errno = errno;
486 (void) fflush(stdout);
487 buf = umem_alloc(FATAL_MSG_SZ, UMEM_NOFAIL);
489 va_start(args, message);
490 (void) sprintf(buf, "ztest: ");
492 (void) vsprintf(buf + strlen(buf), message, args);
495 (void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
496 ": %s", strerror(save_errno));
498 (void) fprintf(stderr, "%s\n", buf);
499 fatal_msg = buf; /* to ease debugging */
506 str2shift(const char *buf)
508 const char *ends = "BKMGTPEZ";
513 for (i = 0; i < strlen(ends); i++) {
514 if (toupper(buf[0]) == ends[i])
517 if (i == strlen(ends)) {
518 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
522 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
525 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
531 nicenumtoull(const char *buf)
536 val = strtoull(buf, &end, 0);
538 (void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
540 } else if (end[0] == '.') {
541 double fval = strtod(buf, &end);
542 fval *= pow(2, str2shift(end));
543 if (fval > UINT64_MAX) {
544 (void) fprintf(stderr, "ztest: value too large: %s\n",
548 val = (uint64_t)fval;
550 int shift = str2shift(end);
551 if (shift >= 64 || (val << shift) >> shift != val) {
552 (void) fprintf(stderr, "ztest: value too large: %s\n",
562 usage(boolean_t requested)
564 const ztest_shared_opts_t *zo = &ztest_opts_defaults;
566 char nice_vdev_size[10];
567 char nice_gang_bang[10];
568 FILE *fp = requested ? stdout : stderr;
570 nicenum(zo->zo_vdev_size, nice_vdev_size);
571 nicenum(zo->zo_metaslab_gang_bang, nice_gang_bang);
573 (void) fprintf(fp, "Usage: %s\n"
574 "\t[-v vdevs (default: %llu)]\n"
575 "\t[-s size_of_each_vdev (default: %s)]\n"
576 "\t[-a alignment_shift (default: %d)] use 0 for random\n"
577 "\t[-m mirror_copies (default: %d)]\n"
578 "\t[-r raidz_disks (default: %d)]\n"
579 "\t[-R raidz_parity (default: %d)]\n"
580 "\t[-d datasets (default: %d)]\n"
581 "\t[-t threads (default: %d)]\n"
582 "\t[-g gang_block_threshold (default: %s)]\n"
583 "\t[-i init_count (default: %d)] initialize pool i times\n"
584 "\t[-k kill_percentage (default: %llu%%)]\n"
585 "\t[-p pool_name (default: %s)]\n"
586 "\t[-f dir (default: %s)] file directory for vdev files\n"
587 "\t[-V] verbose (use multiple times for ever more blather)\n"
588 "\t[-E] use existing pool instead of creating new one\n"
589 "\t[-T time (default: %llu sec)] total run time\n"
590 "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
591 "\t[-P passtime (default: %llu sec)] time per pass\n"
592 "\t[-B alt_ztest (default: <none>)] alternate ztest path\n"
593 "\t[-h] (print help)\n"
596 (u_longlong_t)zo->zo_vdevs, /* -v */
597 nice_vdev_size, /* -s */
598 zo->zo_ashift, /* -a */
599 zo->zo_mirrors, /* -m */
600 zo->zo_raidz, /* -r */
601 zo->zo_raidz_parity, /* -R */
602 zo->zo_datasets, /* -d */
603 zo->zo_threads, /* -t */
604 nice_gang_bang, /* -g */
605 zo->zo_init, /* -i */
606 (u_longlong_t)zo->zo_killrate, /* -k */
607 zo->zo_pool, /* -p */
609 (u_longlong_t)zo->zo_time, /* -T */
610 (u_longlong_t)zo->zo_maxloops, /* -F */
611 (u_longlong_t)zo->zo_passtime);
612 exit(requested ? 0 : 1);
616 process_options(int argc, char **argv)
619 ztest_shared_opts_t *zo = &ztest_opts;
623 char altdir[MAXNAMELEN] = { 0 };
625 bcopy(&ztest_opts_defaults, zo, sizeof (*zo));
627 while ((opt = getopt(argc, argv,
628 "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:hF:B:")) != EOF) {
645 value = nicenumtoull(optarg);
649 zo->zo_vdevs = value;
652 zo->zo_vdev_size = MAX(SPA_MINDEVSIZE, value);
655 zo->zo_ashift = value;
658 zo->zo_mirrors = value;
661 zo->zo_raidz = MAX(1, value);
664 zo->zo_raidz_parity = MIN(MAX(value, 1), 3);
667 zo->zo_datasets = MAX(1, value);
670 zo->zo_threads = MAX(1, value);
673 zo->zo_metaslab_gang_bang = MAX(SPA_MINBLOCKSIZE << 1,
680 zo->zo_killrate = value;
683 (void) strlcpy(zo->zo_pool, optarg,
684 sizeof (zo->zo_pool));
687 path = realpath(optarg, NULL);
689 (void) fprintf(stderr, "error: %s: %s\n",
690 optarg, strerror(errno));
693 (void) strlcpy(zo->zo_dir, path,
694 sizeof (zo->zo_dir));
707 zo->zo_passtime = MAX(1, value);
710 zo->zo_maxloops = MAX(1, value);
713 (void) strlcpy(altdir, optarg, sizeof (altdir));
725 zo->zo_raidz_parity = MIN(zo->zo_raidz_parity, zo->zo_raidz - 1);
728 (zo->zo_vdevs > 0 ? zo->zo_time * NANOSEC / zo->zo_vdevs :
731 if (strlen(altdir) > 0) {
739 cmd = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
740 realaltdir = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
742 VERIFY(NULL != realpath(getexecname(), cmd));
743 if (0 != access(altdir, F_OK)) {
744 ztest_dump_core = B_FALSE;
745 fatal(B_TRUE, "invalid alternate ztest path: %s",
748 VERIFY(NULL != realpath(altdir, realaltdir));
751 * 'cmd' should be of the form "<anything>/usr/bin/<isa>/ztest".
752 * We want to extract <isa> to determine if we should use
753 * 32 or 64 bit binaries.
755 bin = strstr(cmd, "/usr/bin/");
756 ztest = strstr(bin, "/ztest");
758 isalen = ztest - isa;
759 (void) snprintf(zo->zo_alt_ztest, sizeof (zo->zo_alt_ztest),
760 "%s/usr/bin/%.*s/ztest", realaltdir, isalen, isa);
761 (void) snprintf(zo->zo_alt_libpath, sizeof (zo->zo_alt_libpath),
762 "%s/usr/lib/%.*s", realaltdir, isalen, isa);
764 if (0 != access(zo->zo_alt_ztest, X_OK)) {
765 ztest_dump_core = B_FALSE;
766 fatal(B_TRUE, "invalid alternate ztest: %s",
768 } else if (0 != access(zo->zo_alt_libpath, X_OK)) {
769 ztest_dump_core = B_FALSE;
770 fatal(B_TRUE, "invalid alternate lib directory %s",
774 umem_free(cmd, MAXPATHLEN);
775 umem_free(realaltdir, MAXPATHLEN);
780 ztest_kill(ztest_shared_t *zs)
782 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(ztest_spa));
783 zs->zs_space = metaslab_class_get_space(spa_normal_class(ztest_spa));
784 (void) kill(getpid(), SIGKILL);
788 ztest_random(uint64_t range)
792 ASSERT3S(ztest_fd_rand, >=, 0);
797 if (read(ztest_fd_rand, &r, sizeof (r)) != sizeof (r))
798 fatal(1, "short read from /dev/urandom");
805 ztest_record_enospc(const char *s)
807 ztest_shared->zs_enospc_count++;
811 ztest_get_ashift(void)
813 if (ztest_opts.zo_ashift == 0)
814 return (SPA_MINBLOCKSHIFT + ztest_random(3));
815 return (ztest_opts.zo_ashift);
819 make_vdev_file(char *path, char *aux, size_t size, uint64_t ashift)
825 pathbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
828 ashift = ztest_get_ashift();
834 vdev = ztest_shared->zs_vdev_aux;
835 (void) snprintf(path, MAXPATHLEN,
836 ztest_aux_template, ztest_opts.zo_dir,
837 ztest_opts.zo_pool, aux, vdev);
839 vdev = ztest_shared->zs_vdev_next_leaf++;
840 (void) snprintf(path, MAXPATHLEN,
841 ztest_dev_template, ztest_opts.zo_dir,
842 ztest_opts.zo_pool, vdev);
847 int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
849 fatal(1, "can't open %s", path);
850 if (ftruncate(fd, size) != 0)
851 fatal(1, "can't ftruncate %s", path);
855 VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
856 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
857 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
858 VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
859 umem_free(pathbuf, MAXPATHLEN);
865 make_vdev_raidz(char *path, char *aux, size_t size, uint64_t ashift, int r)
867 nvlist_t *raidz, **child;
871 return (make_vdev_file(path, aux, size, ashift));
872 child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
874 for (c = 0; c < r; c++)
875 child[c] = make_vdev_file(path, aux, size, ashift);
877 VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
878 VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
879 VDEV_TYPE_RAIDZ) == 0);
880 VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
881 ztest_opts.zo_raidz_parity) == 0);
882 VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
885 for (c = 0; c < r; c++)
886 nvlist_free(child[c]);
888 umem_free(child, r * sizeof (nvlist_t *));
894 make_vdev_mirror(char *path, char *aux, size_t size, uint64_t ashift,
897 nvlist_t *mirror, **child;
901 return (make_vdev_raidz(path, aux, size, ashift, r));
903 child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
905 for (c = 0; c < m; c++)
906 child[c] = make_vdev_raidz(path, aux, size, ashift, r);
908 VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
909 VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
910 VDEV_TYPE_MIRROR) == 0);
911 VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
914 for (c = 0; c < m; c++)
915 nvlist_free(child[c]);
917 umem_free(child, m * sizeof (nvlist_t *));
923 make_vdev_root(char *path, char *aux, size_t size, uint64_t ashift,
924 int log, int r, int m, int t)
926 nvlist_t *root, **child;
931 child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
933 for (c = 0; c < t; c++) {
934 child[c] = make_vdev_mirror(path, aux, size, ashift, r, m);
935 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
939 VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
940 VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
941 VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
944 for (c = 0; c < t; c++)
945 nvlist_free(child[c]);
947 umem_free(child, t * sizeof (nvlist_t *));
953 ztest_random_blocksize(void)
955 return (1 << (SPA_MINBLOCKSHIFT +
956 ztest_random(SPA_MAXBLOCKSHIFT - SPA_MINBLOCKSHIFT + 1)));
960 ztest_random_ibshift(void)
962 return (DN_MIN_INDBLKSHIFT +
963 ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1));
967 ztest_random_vdev_top(spa_t *spa, boolean_t log_ok)
970 vdev_t *rvd = spa->spa_root_vdev;
973 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
976 top = ztest_random(rvd->vdev_children);
977 tvd = rvd->vdev_child[top];
978 } while (tvd->vdev_ishole || (tvd->vdev_islog && !log_ok) ||
979 tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL);
985 ztest_random_dsl_prop(zfs_prop_t prop)
990 value = zfs_prop_random_value(prop, ztest_random(-1ULL));
991 } while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF);
997 ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value,
1000 const char *propname = zfs_prop_to_name(prop);
1001 const char *valname;
1006 error = dsl_prop_set(osname, propname,
1007 (inherit ? ZPROP_SRC_NONE : ZPROP_SRC_LOCAL),
1008 sizeof (value), 1, &value);
1010 if (error == ENOSPC) {
1011 ztest_record_enospc(FTAG);
1014 ASSERT3U(error, ==, 0);
1016 setpoint = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
1017 VERIFY3U(dsl_prop_get(osname, propname, sizeof (curval),
1018 1, &curval, setpoint), ==, 0);
1020 if (ztest_opts.zo_verbose >= 6) {
1021 VERIFY(zfs_prop_index_to_string(prop, curval, &valname) == 0);
1022 (void) printf("%s %s = %s at '%s'\n",
1023 osname, propname, valname, setpoint);
1025 umem_free(setpoint, MAXPATHLEN);
1031 ztest_spa_prop_set_uint64(zpool_prop_t prop, uint64_t value)
1033 spa_t *spa = ztest_spa;
1034 nvlist_t *props = NULL;
1037 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
1038 VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0);
1040 error = spa_prop_set(spa, props);
1044 if (error == ENOSPC) {
1045 ztest_record_enospc(FTAG);
1048 ASSERT3U(error, ==, 0);
1054 ztest_rll_init(rll_t *rll)
1056 rll->rll_writer = NULL;
1057 rll->rll_readers = 0;
1058 mutex_init(&rll->rll_lock, NULL, MUTEX_DEFAULT, NULL);
1059 cv_init(&rll->rll_cv, NULL, CV_DEFAULT, NULL);
1063 ztest_rll_destroy(rll_t *rll)
1065 ASSERT(rll->rll_writer == NULL);
1066 ASSERT(rll->rll_readers == 0);
1067 mutex_destroy(&rll->rll_lock);
1068 cv_destroy(&rll->rll_cv);
1072 ztest_rll_lock(rll_t *rll, rl_type_t type)
1074 mutex_enter(&rll->rll_lock);
1076 if (type == RL_READER) {
1077 while (rll->rll_writer != NULL)
1078 (void) cv_wait(&rll->rll_cv, &rll->rll_lock);
1081 while (rll->rll_writer != NULL || rll->rll_readers)
1082 (void) cv_wait(&rll->rll_cv, &rll->rll_lock);
1083 rll->rll_writer = curthread;
1086 mutex_exit(&rll->rll_lock);
1090 ztest_rll_unlock(rll_t *rll)
1092 mutex_enter(&rll->rll_lock);
1094 if (rll->rll_writer) {
1095 ASSERT(rll->rll_readers == 0);
1096 rll->rll_writer = NULL;
1098 ASSERT(rll->rll_readers != 0);
1099 ASSERT(rll->rll_writer == NULL);
1103 if (rll->rll_writer == NULL && rll->rll_readers == 0)
1104 cv_broadcast(&rll->rll_cv);
1106 mutex_exit(&rll->rll_lock);
1110 ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type)
1112 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1114 ztest_rll_lock(rll, type);
1118 ztest_object_unlock(ztest_ds_t *zd, uint64_t object)
1120 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1122 ztest_rll_unlock(rll);
1126 ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset,
1127 uint64_t size, rl_type_t type)
1129 uint64_t hash = object ^ (offset % (ZTEST_RANGE_LOCKS + 1));
1130 rll_t *rll = &zd->zd_range_lock[hash & (ZTEST_RANGE_LOCKS - 1)];
1133 rl = umem_alloc(sizeof (*rl), UMEM_NOFAIL);
1134 rl->rl_object = object;
1135 rl->rl_offset = offset;
1139 ztest_rll_lock(rll, type);
1145 ztest_range_unlock(rl_t *rl)
1147 rll_t *rll = rl->rl_lock;
1149 ztest_rll_unlock(rll);
1151 umem_free(rl, sizeof (*rl));
1155 ztest_zd_init(ztest_ds_t *zd, ztest_shared_ds_t *szd, objset_t *os)
1158 zd->zd_zilog = dmu_objset_zil(os);
1159 zd->zd_shared = szd;
1160 dmu_objset_name(os, zd->zd_name);
1163 if (zd->zd_shared != NULL)
1164 zd->zd_shared->zd_seq = 0;
1166 rw_init(&zd->zd_zilog_lock, NULL, RW_DEFAULT, NULL);
1167 mutex_init(&zd->zd_dirobj_lock, NULL, MUTEX_DEFAULT, NULL);
1169 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1170 ztest_rll_init(&zd->zd_object_lock[l]);
1172 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
1173 ztest_rll_init(&zd->zd_range_lock[l]);
1177 ztest_zd_fini(ztest_ds_t *zd)
1181 mutex_destroy(&zd->zd_dirobj_lock);
1182 rw_destroy(&zd->zd_zilog_lock);
1184 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1185 ztest_rll_destroy(&zd->zd_object_lock[l]);
1187 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
1188 ztest_rll_destroy(&zd->zd_range_lock[l]);
1191 #define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1194 ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag)
1200 * Attempt to assign tx to some transaction group.
1202 error = dmu_tx_assign(tx, txg_how);
1204 if (error == ERESTART) {
1205 ASSERT(txg_how == TXG_NOWAIT);
1208 ASSERT3U(error, ==, ENOSPC);
1209 ztest_record_enospc(tag);
1214 txg = dmu_tx_get_txg(tx);
1220 ztest_pattern_set(void *buf, uint64_t size, uint64_t value)
1223 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1231 ztest_pattern_match(void *buf, uint64_t size, uint64_t value)
1234 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1238 diff |= (value - *ip++);
1245 ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1246 uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
1248 bt->bt_magic = BT_MAGIC;
1249 bt->bt_objset = dmu_objset_id(os);
1250 bt->bt_object = object;
1251 bt->bt_offset = offset;
1254 bt->bt_crtxg = crtxg;
1258 ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1259 uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
1261 ASSERT(bt->bt_magic == BT_MAGIC);
1262 ASSERT(bt->bt_objset == dmu_objset_id(os));
1263 ASSERT(bt->bt_object == object);
1264 ASSERT(bt->bt_offset == offset);
1265 ASSERT(bt->bt_gen <= gen);
1266 ASSERT(bt->bt_txg <= txg);
1267 ASSERT(bt->bt_crtxg == crtxg);
1270 static ztest_block_tag_t *
1271 ztest_bt_bonus(dmu_buf_t *db)
1273 dmu_object_info_t doi;
1274 ztest_block_tag_t *bt;
1276 dmu_object_info_from_db(db, &doi);
1277 ASSERT3U(doi.doi_bonus_size, <=, db->db_size);
1278 ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt));
1279 bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt));
1288 #define lrz_type lr_mode
1289 #define lrz_blocksize lr_uid
1290 #define lrz_ibshift lr_gid
1291 #define lrz_bonustype lr_rdev
1292 #define lrz_bonuslen lr_crtime[1]
1295 ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr)
1297 char *name = (void *)(lr + 1); /* name follows lr */
1298 size_t namesize = strlen(name) + 1;
1301 if (zil_replaying(zd->zd_zilog, tx))
1304 itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize);
1305 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1306 sizeof (*lr) + namesize - sizeof (lr_t));
1308 zil_itx_assign(zd->zd_zilog, itx, tx);
1312 ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr, uint64_t object)
1314 char *name = (void *)(lr + 1); /* name follows lr */
1315 size_t namesize = strlen(name) + 1;
1318 if (zil_replaying(zd->zd_zilog, tx))
1321 itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize);
1322 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1323 sizeof (*lr) + namesize - sizeof (lr_t));
1325 itx->itx_oid = object;
1326 zil_itx_assign(zd->zd_zilog, itx, tx);
1330 ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr)
1333 itx_wr_state_t write_state = ztest_random(WR_NUM_STATES);
1335 if (zil_replaying(zd->zd_zilog, tx))
1338 if (lr->lr_length > ZIL_MAX_LOG_DATA)
1339 write_state = WR_INDIRECT;
1341 itx = zil_itx_create(TX_WRITE,
1342 sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0));
1344 if (write_state == WR_COPIED &&
1345 dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length,
1346 ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) {
1347 zil_itx_destroy(itx);
1348 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1349 write_state = WR_NEED_COPY;
1351 itx->itx_private = zd;
1352 itx->itx_wr_state = write_state;
1353 itx->itx_sync = (ztest_random(8) == 0);
1354 itx->itx_sod += (write_state == WR_NEED_COPY ? lr->lr_length : 0);
1356 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1357 sizeof (*lr) - sizeof (lr_t));
1359 zil_itx_assign(zd->zd_zilog, itx, tx);
1363 ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr)
1367 if (zil_replaying(zd->zd_zilog, tx))
1370 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1371 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1372 sizeof (*lr) - sizeof (lr_t));
1374 itx->itx_sync = B_FALSE;
1375 zil_itx_assign(zd->zd_zilog, itx, tx);
1379 ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr)
1383 if (zil_replaying(zd->zd_zilog, tx))
1386 itx = zil_itx_create(TX_SETATTR, sizeof (*lr));
1387 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1388 sizeof (*lr) - sizeof (lr_t));
1390 itx->itx_sync = B_FALSE;
1391 zil_itx_assign(zd->zd_zilog, itx, tx);
1398 ztest_replay_create(ztest_ds_t *zd, lr_create_t *lr, boolean_t byteswap)
1400 char *name = (void *)(lr + 1); /* name follows lr */
1401 objset_t *os = zd->zd_os;
1402 ztest_block_tag_t *bbt;
1409 byteswap_uint64_array(lr, sizeof (*lr));
1411 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1412 ASSERT(name[0] != '\0');
1414 tx = dmu_tx_create(os);
1416 dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name);
1418 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1419 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1421 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1424 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1428 ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid);
1430 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1431 if (lr->lr_foid == 0) {
1432 lr->lr_foid = zap_create(os,
1433 lr->lrz_type, lr->lrz_bonustype,
1434 lr->lrz_bonuslen, tx);
1436 error = zap_create_claim(os, lr->lr_foid,
1437 lr->lrz_type, lr->lrz_bonustype,
1438 lr->lrz_bonuslen, tx);
1441 if (lr->lr_foid == 0) {
1442 lr->lr_foid = dmu_object_alloc(os,
1443 lr->lrz_type, 0, lr->lrz_bonustype,
1444 lr->lrz_bonuslen, tx);
1446 error = dmu_object_claim(os, lr->lr_foid,
1447 lr->lrz_type, 0, lr->lrz_bonustype,
1448 lr->lrz_bonuslen, tx);
1453 ASSERT3U(error, ==, EEXIST);
1454 ASSERT(zd->zd_zilog->zl_replay);
1459 ASSERT(lr->lr_foid != 0);
1461 if (lr->lrz_type != DMU_OT_ZAP_OTHER)
1462 VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid,
1463 lr->lrz_blocksize, lr->lrz_ibshift, tx));
1465 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1466 bbt = ztest_bt_bonus(db);
1467 dmu_buf_will_dirty(db, tx);
1468 ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_gen, txg, txg);
1469 dmu_buf_rele(db, FTAG);
1471 VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1,
1474 (void) ztest_log_create(zd, tx, lr);
1482 ztest_replay_remove(ztest_ds_t *zd, lr_remove_t *lr, boolean_t byteswap)
1484 char *name = (void *)(lr + 1); /* name follows lr */
1485 objset_t *os = zd->zd_os;
1486 dmu_object_info_t doi;
1488 uint64_t object, txg;
1491 byteswap_uint64_array(lr, sizeof (*lr));
1493 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1494 ASSERT(name[0] != '\0');
1497 zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object));
1498 ASSERT(object != 0);
1500 ztest_object_lock(zd, object, RL_WRITER);
1502 VERIFY3U(0, ==, dmu_object_info(os, object, &doi));
1504 tx = dmu_tx_create(os);
1506 dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name);
1507 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1509 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1511 ztest_object_unlock(zd, object);
1515 if (doi.doi_type == DMU_OT_ZAP_OTHER) {
1516 VERIFY3U(0, ==, zap_destroy(os, object, tx));
1518 VERIFY3U(0, ==, dmu_object_free(os, object, tx));
1521 VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx));
1523 (void) ztest_log_remove(zd, tx, lr, object);
1527 ztest_object_unlock(zd, object);
1533 ztest_replay_write(ztest_ds_t *zd, lr_write_t *lr, boolean_t byteswap)
1535 objset_t *os = zd->zd_os;
1536 void *data = lr + 1; /* data follows lr */
1537 uint64_t offset, length;
1538 ztest_block_tag_t *bt = data;
1539 ztest_block_tag_t *bbt;
1540 uint64_t gen, txg, lrtxg, crtxg;
1541 dmu_object_info_t doi;
1544 arc_buf_t *abuf = NULL;
1548 byteswap_uint64_array(lr, sizeof (*lr));
1550 offset = lr->lr_offset;
1551 length = lr->lr_length;
1553 /* If it's a dmu_sync() block, write the whole block */
1554 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
1555 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
1556 if (length < blocksize) {
1557 offset -= offset % blocksize;
1562 if (bt->bt_magic == BSWAP_64(BT_MAGIC))
1563 byteswap_uint64_array(bt, sizeof (*bt));
1565 if (bt->bt_magic != BT_MAGIC)
1568 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1569 rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER);
1571 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1573 dmu_object_info_from_db(db, &doi);
1575 bbt = ztest_bt_bonus(db);
1576 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1578 crtxg = bbt->bt_crtxg;
1579 lrtxg = lr->lr_common.lrc_txg;
1581 tx = dmu_tx_create(os);
1583 dmu_tx_hold_write(tx, lr->lr_foid, offset, length);
1585 if (ztest_random(8) == 0 && length == doi.doi_data_block_size &&
1586 P2PHASE(offset, length) == 0)
1587 abuf = dmu_request_arcbuf(db, length);
1589 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1592 dmu_return_arcbuf(abuf);
1593 dmu_buf_rele(db, FTAG);
1594 ztest_range_unlock(rl);
1595 ztest_object_unlock(zd, lr->lr_foid);
1601 * Usually, verify the old data before writing new data --
1602 * but not always, because we also want to verify correct
1603 * behavior when the data was not recently read into cache.
1605 ASSERT(offset % doi.doi_data_block_size == 0);
1606 if (ztest_random(4) != 0) {
1607 int prefetch = ztest_random(2) ?
1608 DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
1609 ztest_block_tag_t rbt;
1611 VERIFY(dmu_read(os, lr->lr_foid, offset,
1612 sizeof (rbt), &rbt, prefetch) == 0);
1613 if (rbt.bt_magic == BT_MAGIC) {
1614 ztest_bt_verify(&rbt, os, lr->lr_foid,
1615 offset, gen, txg, crtxg);
1620 * Writes can appear to be newer than the bonus buffer because
1621 * the ztest_get_data() callback does a dmu_read() of the
1622 * open-context data, which may be different than the data
1623 * as it was when the write was generated.
1625 if (zd->zd_zilog->zl_replay) {
1626 ztest_bt_verify(bt, os, lr->lr_foid, offset,
1627 MAX(gen, bt->bt_gen), MAX(txg, lrtxg),
1632 * Set the bt's gen/txg to the bonus buffer's gen/txg
1633 * so that all of the usual ASSERTs will work.
1635 ztest_bt_generate(bt, os, lr->lr_foid, offset, gen, txg, crtxg);
1639 dmu_write(os, lr->lr_foid, offset, length, data, tx);
1641 bcopy(data, abuf->b_data, length);
1642 dmu_assign_arcbuf(db, offset, abuf, tx);
1645 (void) ztest_log_write(zd, tx, lr);
1647 dmu_buf_rele(db, FTAG);
1651 ztest_range_unlock(rl);
1652 ztest_object_unlock(zd, lr->lr_foid);
1658 ztest_replay_truncate(ztest_ds_t *zd, lr_truncate_t *lr, boolean_t byteswap)
1660 objset_t *os = zd->zd_os;
1666 byteswap_uint64_array(lr, sizeof (*lr));
1668 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1669 rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length,
1672 tx = dmu_tx_create(os);
1674 dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length);
1676 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1678 ztest_range_unlock(rl);
1679 ztest_object_unlock(zd, lr->lr_foid);
1683 VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset,
1684 lr->lr_length, tx) == 0);
1686 (void) ztest_log_truncate(zd, tx, lr);
1690 ztest_range_unlock(rl);
1691 ztest_object_unlock(zd, lr->lr_foid);
1697 ztest_replay_setattr(ztest_ds_t *zd, lr_setattr_t *lr, boolean_t byteswap)
1699 objset_t *os = zd->zd_os;
1702 ztest_block_tag_t *bbt;
1703 uint64_t txg, lrtxg, crtxg;
1706 byteswap_uint64_array(lr, sizeof (*lr));
1708 ztest_object_lock(zd, lr->lr_foid, RL_WRITER);
1710 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1712 tx = dmu_tx_create(os);
1713 dmu_tx_hold_bonus(tx, lr->lr_foid);
1715 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1717 dmu_buf_rele(db, FTAG);
1718 ztest_object_unlock(zd, lr->lr_foid);
1722 bbt = ztest_bt_bonus(db);
1723 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1724 crtxg = bbt->bt_crtxg;
1725 lrtxg = lr->lr_common.lrc_txg;
1727 if (zd->zd_zilog->zl_replay) {
1728 ASSERT(lr->lr_size != 0);
1729 ASSERT(lr->lr_mode != 0);
1733 * Randomly change the size and increment the generation.
1735 lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) *
1737 lr->lr_mode = bbt->bt_gen + 1;
1742 * Verify that the current bonus buffer is not newer than our txg.
1744 ztest_bt_verify(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode,
1745 MAX(txg, lrtxg), crtxg);
1747 dmu_buf_will_dirty(db, tx);
1749 ASSERT3U(lr->lr_size, >=, sizeof (*bbt));
1750 ASSERT3U(lr->lr_size, <=, db->db_size);
1751 VERIFY3U(dmu_set_bonus(db, lr->lr_size, tx), ==, 0);
1752 bbt = ztest_bt_bonus(db);
1754 ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode, txg, crtxg);
1756 dmu_buf_rele(db, FTAG);
1758 (void) ztest_log_setattr(zd, tx, lr);
1762 ztest_object_unlock(zd, lr->lr_foid);
1767 zil_replay_func_t *ztest_replay_vector[TX_MAX_TYPE] = {
1768 NULL, /* 0 no such transaction type */
1769 (zil_replay_func_t *)ztest_replay_create, /* TX_CREATE */
1770 NULL, /* TX_MKDIR */
1771 NULL, /* TX_MKXATTR */
1772 NULL, /* TX_SYMLINK */
1773 (zil_replay_func_t *)ztest_replay_remove, /* TX_REMOVE */
1774 NULL, /* TX_RMDIR */
1776 NULL, /* TX_RENAME */
1777 (zil_replay_func_t *)ztest_replay_write, /* TX_WRITE */
1778 (zil_replay_func_t *)ztest_replay_truncate, /* TX_TRUNCATE */
1779 (zil_replay_func_t *)ztest_replay_setattr, /* TX_SETATTR */
1781 NULL, /* TX_CREATE_ACL */
1782 NULL, /* TX_CREATE_ATTR */
1783 NULL, /* TX_CREATE_ACL_ATTR */
1784 NULL, /* TX_MKDIR_ACL */
1785 NULL, /* TX_MKDIR_ATTR */
1786 NULL, /* TX_MKDIR_ACL_ATTR */
1787 NULL, /* TX_WRITE2 */
1791 * ZIL get_data callbacks
1795 ztest_get_done(zgd_t *zgd, int error)
1797 ztest_ds_t *zd = zgd->zgd_private;
1798 uint64_t object = zgd->zgd_rl->rl_object;
1801 dmu_buf_rele(zgd->zgd_db, zgd);
1803 ztest_range_unlock(zgd->zgd_rl);
1804 ztest_object_unlock(zd, object);
1806 if (error == 0 && zgd->zgd_bp)
1807 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1809 umem_free(zgd, sizeof (*zgd));
1813 ztest_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1815 ztest_ds_t *zd = arg;
1816 objset_t *os = zd->zd_os;
1817 uint64_t object = lr->lr_foid;
1818 uint64_t offset = lr->lr_offset;
1819 uint64_t size = lr->lr_length;
1820 blkptr_t *bp = &lr->lr_blkptr;
1821 uint64_t txg = lr->lr_common.lrc_txg;
1823 dmu_object_info_t doi;
1828 ztest_object_lock(zd, object, RL_READER);
1829 error = dmu_bonus_hold(os, object, FTAG, &db);
1831 ztest_object_unlock(zd, object);
1835 crtxg = ztest_bt_bonus(db)->bt_crtxg;
1837 if (crtxg == 0 || crtxg > txg) {
1838 dmu_buf_rele(db, FTAG);
1839 ztest_object_unlock(zd, object);
1843 dmu_object_info_from_db(db, &doi);
1844 dmu_buf_rele(db, FTAG);
1847 zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL);
1848 zgd->zgd_zilog = zd->zd_zilog;
1849 zgd->zgd_private = zd;
1851 if (buf != NULL) { /* immediate write */
1852 zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
1855 error = dmu_read(os, object, offset, size, buf,
1856 DMU_READ_NO_PREFETCH);
1859 size = doi.doi_data_block_size;
1861 offset = P2ALIGN(offset, size);
1863 ASSERT(offset < size);
1867 zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
1870 error = dmu_buf_hold(os, object, offset, zgd, &db,
1871 DMU_READ_NO_PREFETCH);
1877 ASSERT(db->db_offset == offset);
1878 ASSERT(db->db_size == size);
1880 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1881 ztest_get_done, zgd);
1888 ztest_get_done(zgd, error);
1894 ztest_lr_alloc(size_t lrsize, char *name)
1897 size_t namesize = name ? strlen(name) + 1 : 0;
1899 lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
1902 bcopy(name, lr + lrsize, namesize);
1908 ztest_lr_free(void *lr, size_t lrsize, char *name)
1910 size_t namesize = name ? strlen(name) + 1 : 0;
1912 umem_free(lr, lrsize + namesize);
1916 * Lookup a bunch of objects. Returns the number of objects not found.
1919 ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
1925 ASSERT(mutex_held(&zd->zd_dirobj_lock));
1927 for (i = 0; i < count; i++, od++) {
1929 error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
1930 sizeof (uint64_t), 1, &od->od_object);
1932 ASSERT(error == ENOENT);
1933 ASSERT(od->od_object == 0);
1937 ztest_block_tag_t *bbt;
1938 dmu_object_info_t doi;
1940 ASSERT(od->od_object != 0);
1941 ASSERT(missing == 0); /* there should be no gaps */
1943 ztest_object_lock(zd, od->od_object, RL_READER);
1944 VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
1945 od->od_object, FTAG, &db));
1946 dmu_object_info_from_db(db, &doi);
1947 bbt = ztest_bt_bonus(db);
1948 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1949 od->od_type = doi.doi_type;
1950 od->od_blocksize = doi.doi_data_block_size;
1951 od->od_gen = bbt->bt_gen;
1952 dmu_buf_rele(db, FTAG);
1953 ztest_object_unlock(zd, od->od_object);
1961 ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
1966 ASSERT(mutex_held(&zd->zd_dirobj_lock));
1968 for (i = 0; i < count; i++, od++) {
1975 lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
1977 lr->lr_doid = od->od_dir;
1978 lr->lr_foid = 0; /* 0 to allocate, > 0 to claim */
1979 lr->lrz_type = od->od_crtype;
1980 lr->lrz_blocksize = od->od_crblocksize;
1981 lr->lrz_ibshift = ztest_random_ibshift();
1982 lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
1983 lr->lrz_bonuslen = dmu_bonus_max();
1984 lr->lr_gen = od->od_crgen;
1985 lr->lr_crtime[0] = time(NULL);
1987 if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
1988 ASSERT(missing == 0);
1992 od->od_object = lr->lr_foid;
1993 od->od_type = od->od_crtype;
1994 od->od_blocksize = od->od_crblocksize;
1995 od->od_gen = od->od_crgen;
1996 ASSERT(od->od_object != 0);
1999 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2006 ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
2012 ASSERT(mutex_held(&zd->zd_dirobj_lock));
2016 for (i = count - 1; i >= 0; i--, od--) {
2022 if (od->od_object == 0)
2025 lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2027 lr->lr_doid = od->od_dir;
2029 if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
2030 ASSERT3U(error, ==, ENOSPC);
2035 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2042 ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
2048 lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
2050 lr->lr_foid = object;
2051 lr->lr_offset = offset;
2052 lr->lr_length = size;
2054 BP_ZERO(&lr->lr_blkptr);
2056 bcopy(data, lr + 1, size);
2058 error = ztest_replay_write(zd, lr, B_FALSE);
2060 ztest_lr_free(lr, sizeof (*lr) + size, NULL);
2066 ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2071 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2073 lr->lr_foid = object;
2074 lr->lr_offset = offset;
2075 lr->lr_length = size;
2077 error = ztest_replay_truncate(zd, lr, B_FALSE);
2079 ztest_lr_free(lr, sizeof (*lr), NULL);
2085 ztest_setattr(ztest_ds_t *zd, uint64_t object)
2090 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2092 lr->lr_foid = object;
2096 error = ztest_replay_setattr(zd, lr, B_FALSE);
2098 ztest_lr_free(lr, sizeof (*lr), NULL);
2104 ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2106 objset_t *os = zd->zd_os;
2111 txg_wait_synced(dmu_objset_pool(os), 0);
2113 ztest_object_lock(zd, object, RL_READER);
2114 rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
2116 tx = dmu_tx_create(os);
2118 dmu_tx_hold_write(tx, object, offset, size);
2120 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
2123 dmu_prealloc(os, object, offset, size, tx);
2125 txg_wait_synced(dmu_objset_pool(os), txg);
2127 (void) dmu_free_long_range(os, object, offset, size);
2130 ztest_range_unlock(rl);
2131 ztest_object_unlock(zd, object);
2135 ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
2137 ztest_block_tag_t wbt;
2138 dmu_object_info_t doi;
2139 enum ztest_io_type io_type;
2143 VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
2144 blocksize = doi.doi_data_block_size;
2145 data = umem_alloc(blocksize, UMEM_NOFAIL);
2148 * Pick an i/o type at random, biased toward writing block tags.
2150 io_type = ztest_random(ZTEST_IO_TYPES);
2151 if (ztest_random(2) == 0)
2152 io_type = ZTEST_IO_WRITE_TAG;
2154 (void) rw_enter(&zd->zd_zilog_lock, RW_READER);
2158 case ZTEST_IO_WRITE_TAG:
2159 ztest_bt_generate(&wbt, zd->zd_os, object, offset, 0, 0, 0);
2160 (void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
2163 case ZTEST_IO_WRITE_PATTERN:
2164 (void) memset(data, 'a' + (object + offset) % 5, blocksize);
2165 if (ztest_random(2) == 0) {
2167 * Induce fletcher2 collisions to ensure that
2168 * zio_ddt_collision() detects and resolves them
2169 * when using fletcher2-verify for deduplication.
2171 ((uint64_t *)data)[0] ^= 1ULL << 63;
2172 ((uint64_t *)data)[4] ^= 1ULL << 63;
2174 (void) ztest_write(zd, object, offset, blocksize, data);
2177 case ZTEST_IO_WRITE_ZEROES:
2178 bzero(data, blocksize);
2179 (void) ztest_write(zd, object, offset, blocksize, data);
2182 case ZTEST_IO_TRUNCATE:
2183 (void) ztest_truncate(zd, object, offset, blocksize);
2186 case ZTEST_IO_SETATTR:
2187 (void) ztest_setattr(zd, object);
2193 (void) rw_exit(&zd->zd_zilog_lock);
2195 umem_free(data, blocksize);
2199 * Initialize an object description template.
2202 ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
2203 dmu_object_type_t type, uint64_t blocksize, uint64_t gen)
2205 od->od_dir = ZTEST_DIROBJ;
2208 od->od_crtype = type;
2209 od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
2212 od->od_type = DMU_OT_NONE;
2213 od->od_blocksize = 0;
2216 (void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
2217 tag, (longlong_t)id, (u_longlong_t)index);
2221 * Lookup or create the objects for a test using the od template.
2222 * If the objects do not all exist, or if 'remove' is specified,
2223 * remove any existing objects and create new ones. Otherwise,
2224 * use the existing objects.
2227 ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
2229 int count = size / sizeof (*od);
2232 mutex_enter(&zd->zd_dirobj_lock);
2233 if ((ztest_lookup(zd, od, count) != 0 || remove) &&
2234 (ztest_remove(zd, od, count) != 0 ||
2235 ztest_create(zd, od, count) != 0))
2238 mutex_exit(&zd->zd_dirobj_lock);
2245 ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
2247 zilog_t *zilog = zd->zd_zilog;
2249 (void) rw_enter(&zd->zd_zilog_lock, RW_READER);
2251 zil_commit(zilog, ztest_random(ZTEST_OBJECTS));
2254 * Remember the committed values in zd, which is in parent/child
2255 * shared memory. If we die, the next iteration of ztest_run()
2256 * will verify that the log really does contain this record.
2258 mutex_enter(&zilog->zl_lock);
2259 ASSERT(zd->zd_shared != NULL);
2260 ASSERT3U(zd->zd_shared->zd_seq, <=, zilog->zl_commit_lr_seq);
2261 zd->zd_shared->zd_seq = zilog->zl_commit_lr_seq;
2262 mutex_exit(&zilog->zl_lock);
2264 (void) rw_exit(&zd->zd_zilog_lock);
2268 * This function is designed to simulate the operations that occur during a
2269 * mount/unmount operation. We hold the dataset across these operations in an
2270 * attempt to expose any implicit assumptions about ZIL management.
2274 ztest_zil_remount(ztest_ds_t *zd, uint64_t id)
2276 objset_t *os = zd->zd_os;
2278 (void) rw_enter(&zd->zd_zilog_lock, RW_WRITER);
2280 /* zfs_sb_teardown() */
2281 zil_close(zd->zd_zilog);
2283 /* zfsvfs_setup() */
2284 VERIFY(zil_open(os, ztest_get_data) == zd->zd_zilog);
2285 zil_replay(os, zd, ztest_replay_vector);
2287 (void) rw_exit(&zd->zd_zilog_lock);
2291 * Verify that we can't destroy an active pool, create an existing pool,
2292 * or create a pool with a bad vdev spec.
2296 ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2298 ztest_shared_opts_t *zo = &ztest_opts;
2303 * Attempt to create using a bad file.
2305 nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
2306 VERIFY3U(ENOENT, ==,
2307 spa_create("ztest_bad_file", nvroot, NULL, NULL, NULL));
2308 nvlist_free(nvroot);
2311 * Attempt to create using a bad mirror.
2313 nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 2, 1);
2314 VERIFY3U(ENOENT, ==,
2315 spa_create("ztest_bad_mirror", nvroot, NULL, NULL, NULL));
2316 nvlist_free(nvroot);
2319 * Attempt to create an existing pool. It shouldn't matter
2320 * what's in the nvroot; we should fail with EEXIST.
2322 (void) rw_enter(&ztest_name_lock, RW_READER);
2323 nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
2324 VERIFY3U(EEXIST, ==, spa_create(zo->zo_pool, nvroot, NULL, NULL, NULL));
2325 nvlist_free(nvroot);
2326 VERIFY3U(0, ==, spa_open(zo->zo_pool, &spa, FTAG));
2327 VERIFY3U(EBUSY, ==, spa_destroy(zo->zo_pool));
2328 spa_close(spa, FTAG);
2330 (void) rw_exit(&ztest_name_lock);
2334 vdev_lookup_by_path(vdev_t *vd, const char *path)
2339 if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
2342 for (c = 0; c < vd->vdev_children; c++)
2343 if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
2351 * Find the first available hole which can be used as a top-level.
2354 find_vdev_hole(spa_t *spa)
2356 vdev_t *rvd = spa->spa_root_vdev;
2359 ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
2361 for (c = 0; c < rvd->vdev_children; c++) {
2362 vdev_t *cvd = rvd->vdev_child[c];
2364 if (cvd->vdev_ishole)
2371 * Verify that vdev_add() works as expected.
2375 ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2377 ztest_shared_t *zs = ztest_shared;
2378 spa_t *spa = ztest_spa;
2384 mutex_enter(&ztest_vdev_lock);
2386 MAX(zs->zs_mirrors + zs->zs_splits, 1) * ztest_opts.zo_raidz;
2388 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2390 ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2393 * If we have slogs then remove them 1/4 of the time.
2395 if (spa_has_slogs(spa) && ztest_random(4) == 0) {
2397 * Grab the guid from the head of the log class rotor.
2399 guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid;
2401 spa_config_exit(spa, SCL_VDEV, FTAG);
2404 * We have to grab the zs_name_lock as writer to
2405 * prevent a race between removing a slog (dmu_objset_find)
2406 * and destroying a dataset. Removing the slog will
2407 * grab a reference on the dataset which may cause
2408 * dmu_objset_destroy() to fail with EBUSY thus
2409 * leaving the dataset in an inconsistent state.
2411 rw_enter(&ztest_name_lock, RW_WRITER);
2412 error = spa_vdev_remove(spa, guid, B_FALSE);
2413 rw_exit(&ztest_name_lock);
2415 if (error && error != EEXIST)
2416 fatal(0, "spa_vdev_remove() = %d", error);
2418 spa_config_exit(spa, SCL_VDEV, FTAG);
2421 * Make 1/4 of the devices be log devices.
2423 nvroot = make_vdev_root(NULL, NULL,
2424 ztest_opts.zo_vdev_size, 0,
2425 ztest_random(4) == 0, ztest_opts.zo_raidz,
2428 error = spa_vdev_add(spa, nvroot);
2429 nvlist_free(nvroot);
2431 if (error == ENOSPC)
2432 ztest_record_enospc("spa_vdev_add");
2433 else if (error != 0)
2434 fatal(0, "spa_vdev_add() = %d", error);
2437 mutex_exit(&ztest_vdev_lock);
2441 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
2445 ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
2447 ztest_shared_t *zs = ztest_shared;
2448 spa_t *spa = ztest_spa;
2449 vdev_t *rvd = spa->spa_root_vdev;
2450 spa_aux_vdev_t *sav;
2456 path = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2458 if (ztest_random(2) == 0) {
2459 sav = &spa->spa_spares;
2460 aux = ZPOOL_CONFIG_SPARES;
2462 sav = &spa->spa_l2cache;
2463 aux = ZPOOL_CONFIG_L2CACHE;
2466 mutex_enter(&ztest_vdev_lock);
2468 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2470 if (sav->sav_count != 0 && ztest_random(4) == 0) {
2472 * Pick a random device to remove.
2474 guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
2477 * Find an unused device we can add.
2479 zs->zs_vdev_aux = 0;
2482 (void) snprintf(path, sizeof (path), ztest_aux_template,
2483 ztest_opts.zo_dir, ztest_opts.zo_pool, aux,
2485 for (c = 0; c < sav->sav_count; c++)
2486 if (strcmp(sav->sav_vdevs[c]->vdev_path,
2489 if (c == sav->sav_count &&
2490 vdev_lookup_by_path(rvd, path) == NULL)
2496 spa_config_exit(spa, SCL_VDEV, FTAG);
2502 nvlist_t *nvroot = make_vdev_root(NULL, aux,
2503 (ztest_opts.zo_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
2504 error = spa_vdev_add(spa, nvroot);
2506 fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
2507 nvlist_free(nvroot);
2510 * Remove an existing device. Sometimes, dirty its
2511 * vdev state first to make sure we handle removal
2512 * of devices that have pending state changes.
2514 if (ztest_random(2) == 0)
2515 (void) vdev_online(spa, guid, 0, NULL);
2517 error = spa_vdev_remove(spa, guid, B_FALSE);
2518 if (error != 0 && error != EBUSY)
2519 fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
2522 mutex_exit(&ztest_vdev_lock);
2524 umem_free(path, MAXPATHLEN);
2528 * split a pool if it has mirror tlvdevs
2532 ztest_split_pool(ztest_ds_t *zd, uint64_t id)
2534 ztest_shared_t *zs = ztest_shared;
2535 spa_t *spa = ztest_spa;
2536 vdev_t *rvd = spa->spa_root_vdev;
2537 nvlist_t *tree, **child, *config, *split, **schild;
2538 uint_t c, children, schildren = 0, lastlogid = 0;
2541 mutex_enter(&ztest_vdev_lock);
2543 /* ensure we have a useable config; mirrors of raidz aren't supported */
2544 if (zs->zs_mirrors < 3 || ztest_opts.zo_raidz > 1) {
2545 mutex_exit(&ztest_vdev_lock);
2549 /* clean up the old pool, if any */
2550 (void) spa_destroy("splitp");
2552 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2554 /* generate a config from the existing config */
2555 mutex_enter(&spa->spa_props_lock);
2556 VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
2558 mutex_exit(&spa->spa_props_lock);
2560 VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2563 schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
2564 for (c = 0; c < children; c++) {
2565 vdev_t *tvd = rvd->vdev_child[c];
2569 if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
2570 VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
2572 VERIFY(nvlist_add_string(schild[schildren],
2573 ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
2574 VERIFY(nvlist_add_uint64(schild[schildren],
2575 ZPOOL_CONFIG_IS_HOLE, 1) == 0);
2577 lastlogid = schildren;
2582 VERIFY(nvlist_lookup_nvlist_array(child[c],
2583 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2584 VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
2587 /* OK, create a config that can be used to split */
2588 VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
2589 VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
2590 VDEV_TYPE_ROOT) == 0);
2591 VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
2592 lastlogid != 0 ? lastlogid : schildren) == 0);
2594 VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
2595 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
2597 for (c = 0; c < schildren; c++)
2598 nvlist_free(schild[c]);
2602 spa_config_exit(spa, SCL_VDEV, FTAG);
2604 (void) rw_enter(&ztest_name_lock, RW_WRITER);
2605 error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
2606 (void) rw_exit(&ztest_name_lock);
2608 nvlist_free(config);
2611 (void) printf("successful split - results:\n");
2612 mutex_enter(&spa_namespace_lock);
2613 show_pool_stats(spa);
2614 show_pool_stats(spa_lookup("splitp"));
2615 mutex_exit(&spa_namespace_lock);
2619 mutex_exit(&ztest_vdev_lock);
2624 * Verify that we can attach and detach devices.
2628 ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
2630 ztest_shared_t *zs = ztest_shared;
2631 spa_t *spa = ztest_spa;
2632 spa_aux_vdev_t *sav = &spa->spa_spares;
2633 vdev_t *rvd = spa->spa_root_vdev;
2634 vdev_t *oldvd, *newvd, *pvd;
2638 uint64_t ashift = ztest_get_ashift();
2639 uint64_t oldguid, pguid;
2640 size_t oldsize, newsize;
2641 char *oldpath, *newpath;
2643 int oldvd_has_siblings = B_FALSE;
2644 int newvd_is_spare = B_FALSE;
2646 int error, expected_error;
2648 oldpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2649 newpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2651 mutex_enter(&ztest_vdev_lock);
2652 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
2654 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2657 * Decide whether to do an attach or a replace.
2659 replacing = ztest_random(2);
2662 * Pick a random top-level vdev.
2664 top = ztest_random_vdev_top(spa, B_TRUE);
2667 * Pick a random leaf within it.
2669 leaf = ztest_random(leaves);
2674 oldvd = rvd->vdev_child[top];
2675 if (zs->zs_mirrors >= 1) {
2676 ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
2677 ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
2678 oldvd = oldvd->vdev_child[leaf / ztest_opts.zo_raidz];
2680 if (ztest_opts.zo_raidz > 1) {
2681 ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
2682 ASSERT(oldvd->vdev_children == ztest_opts.zo_raidz);
2683 oldvd = oldvd->vdev_child[leaf % ztest_opts.zo_raidz];
2687 * If we're already doing an attach or replace, oldvd may be a
2688 * mirror vdev -- in which case, pick a random child.
2690 while (oldvd->vdev_children != 0) {
2691 oldvd_has_siblings = B_TRUE;
2692 ASSERT(oldvd->vdev_children >= 2);
2693 oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
2696 oldguid = oldvd->vdev_guid;
2697 oldsize = vdev_get_min_asize(oldvd);
2698 oldvd_is_log = oldvd->vdev_top->vdev_islog;
2699 (void) strcpy(oldpath, oldvd->vdev_path);
2700 pvd = oldvd->vdev_parent;
2701 pguid = pvd->vdev_guid;
2704 * If oldvd has siblings, then half of the time, detach it.
2706 if (oldvd_has_siblings && ztest_random(2) == 0) {
2707 spa_config_exit(spa, SCL_VDEV, FTAG);
2708 error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
2709 if (error != 0 && error != ENODEV && error != EBUSY &&
2711 fatal(0, "detach (%s) returned %d", oldpath, error);
2716 * For the new vdev, choose with equal probability between the two
2717 * standard paths (ending in either 'a' or 'b') or a random hot spare.
2719 if (sav->sav_count != 0 && ztest_random(3) == 0) {
2720 newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
2721 newvd_is_spare = B_TRUE;
2722 (void) strcpy(newpath, newvd->vdev_path);
2724 (void) snprintf(newpath, MAXPATHLEN, ztest_dev_template,
2725 ztest_opts.zo_dir, ztest_opts.zo_pool,
2726 top * leaves + leaf);
2727 if (ztest_random(2) == 0)
2728 newpath[strlen(newpath) - 1] = 'b';
2729 newvd = vdev_lookup_by_path(rvd, newpath);
2733 newsize = vdev_get_min_asize(newvd);
2736 * Make newsize a little bigger or smaller than oldsize.
2737 * If it's smaller, the attach should fail.
2738 * If it's larger, and we're doing a replace,
2739 * we should get dynamic LUN growth when we're done.
2741 newsize = 10 * oldsize / (9 + ztest_random(3));
2745 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
2746 * unless it's a replace; in that case any non-replacing parent is OK.
2748 * If newvd is already part of the pool, it should fail with EBUSY.
2750 * If newvd is too small, it should fail with EOVERFLOW.
2752 if (pvd->vdev_ops != &vdev_mirror_ops &&
2753 pvd->vdev_ops != &vdev_root_ops && (!replacing ||
2754 pvd->vdev_ops == &vdev_replacing_ops ||
2755 pvd->vdev_ops == &vdev_spare_ops))
2756 expected_error = ENOTSUP;
2757 else if (newvd_is_spare && (!replacing || oldvd_is_log))
2758 expected_error = ENOTSUP;
2759 else if (newvd == oldvd)
2760 expected_error = replacing ? 0 : EBUSY;
2761 else if (vdev_lookup_by_path(rvd, newpath) != NULL)
2762 expected_error = EBUSY;
2763 else if (newsize < oldsize)
2764 expected_error = EOVERFLOW;
2765 else if (ashift > oldvd->vdev_top->vdev_ashift)
2766 expected_error = EDOM;
2770 spa_config_exit(spa, SCL_VDEV, FTAG);
2773 * Build the nvlist describing newpath.
2775 root = make_vdev_root(newpath, NULL, newvd == NULL ? newsize : 0,
2776 ashift, 0, 0, 0, 1);
2778 error = spa_vdev_attach(spa, oldguid, root, replacing);
2783 * If our parent was the replacing vdev, but the replace completed,
2784 * then instead of failing with ENOTSUP we may either succeed,
2785 * fail with ENODEV, or fail with EOVERFLOW.
2787 if (expected_error == ENOTSUP &&
2788 (error == 0 || error == ENODEV || error == EOVERFLOW))
2789 expected_error = error;
2792 * If someone grew the LUN, the replacement may be too small.
2794 if (error == EOVERFLOW || error == EBUSY)
2795 expected_error = error;
2797 /* XXX workaround 6690467 */
2798 if (error != expected_error && expected_error != EBUSY) {
2799 fatal(0, "attach (%s %llu, %s %llu, %d) "
2800 "returned %d, expected %d",
2801 oldpath, (longlong_t)oldsize, newpath,
2802 (longlong_t)newsize, replacing, error, expected_error);
2805 mutex_exit(&ztest_vdev_lock);
2807 umem_free(oldpath, MAXPATHLEN);
2808 umem_free(newpath, MAXPATHLEN);
2812 * Callback function which expands the physical size of the vdev.
2815 grow_vdev(vdev_t *vd, void *arg)
2817 ASSERTV(spa_t *spa = vd->vdev_spa);
2818 size_t *newsize = arg;
2822 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
2823 ASSERT(vd->vdev_ops->vdev_op_leaf);
2825 if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
2828 fsize = lseek(fd, 0, SEEK_END);
2829 VERIFY(ftruncate(fd, *newsize) == 0);
2831 if (ztest_opts.zo_verbose >= 6) {
2832 (void) printf("%s grew from %lu to %lu bytes\n",
2833 vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
2840 * Callback function which expands a given vdev by calling vdev_online().
2844 online_vdev(vdev_t *vd, void *arg)
2846 spa_t *spa = vd->vdev_spa;
2847 vdev_t *tvd = vd->vdev_top;
2848 uint64_t guid = vd->vdev_guid;
2849 uint64_t generation = spa->spa_config_generation + 1;
2850 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
2853 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
2854 ASSERT(vd->vdev_ops->vdev_op_leaf);
2856 /* Calling vdev_online will initialize the new metaslabs */
2857 spa_config_exit(spa, SCL_STATE, spa);
2858 error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
2859 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
2862 * If vdev_online returned an error or the underlying vdev_open
2863 * failed then we abort the expand. The only way to know that
2864 * vdev_open fails is by checking the returned newstate.
2866 if (error || newstate != VDEV_STATE_HEALTHY) {
2867 if (ztest_opts.zo_verbose >= 5) {
2868 (void) printf("Unable to expand vdev, state %llu, "
2869 "error %d\n", (u_longlong_t)newstate, error);
2873 ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
2876 * Since we dropped the lock we need to ensure that we're
2877 * still talking to the original vdev. It's possible this
2878 * vdev may have been detached/replaced while we were
2879 * trying to online it.
2881 if (generation != spa->spa_config_generation) {
2882 if (ztest_opts.zo_verbose >= 5) {
2883 (void) printf("vdev configuration has changed, "
2884 "guid %llu, state %llu, expected gen %llu, "
2887 (u_longlong_t)tvd->vdev_state,
2888 (u_longlong_t)generation,
2889 (u_longlong_t)spa->spa_config_generation);
2897 * Traverse the vdev tree calling the supplied function.
2898 * We continue to walk the tree until we either have walked all
2899 * children or we receive a non-NULL return from the callback.
2900 * If a NULL callback is passed, then we just return back the first
2901 * leaf vdev we encounter.
2904 vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
2908 if (vd->vdev_ops->vdev_op_leaf) {
2912 return (func(vd, arg));
2915 for (c = 0; c < vd->vdev_children; c++) {
2916 vdev_t *cvd = vd->vdev_child[c];
2917 if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
2924 * Verify that dynamic LUN growth works as expected.
2928 ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
2930 spa_t *spa = ztest_spa;
2932 metaslab_class_t *mc;
2933 metaslab_group_t *mg;
2934 size_t psize, newsize;
2936 uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
2938 mutex_enter(&ztest_vdev_lock);
2939 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
2941 top = ztest_random_vdev_top(spa, B_TRUE);
2943 tvd = spa->spa_root_vdev->vdev_child[top];
2946 old_ms_count = tvd->vdev_ms_count;
2947 old_class_space = metaslab_class_get_space(mc);
2950 * Determine the size of the first leaf vdev associated with
2951 * our top-level device.
2953 vd = vdev_walk_tree(tvd, NULL, NULL);
2954 ASSERT3P(vd, !=, NULL);
2955 ASSERT(vd->vdev_ops->vdev_op_leaf);
2957 psize = vd->vdev_psize;
2960 * We only try to expand the vdev if it's healthy, less than 4x its
2961 * original size, and it has a valid psize.
2963 if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
2964 psize == 0 || psize >= 4 * ztest_opts.zo_vdev_size) {
2965 spa_config_exit(spa, SCL_STATE, spa);
2966 mutex_exit(&ztest_vdev_lock);
2970 newsize = psize + psize / 8;
2971 ASSERT3U(newsize, >, psize);
2973 if (ztest_opts.zo_verbose >= 6) {
2974 (void) printf("Expanding LUN %s from %lu to %lu\n",
2975 vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
2979 * Growing the vdev is a two step process:
2980 * 1). expand the physical size (i.e. relabel)
2981 * 2). online the vdev to create the new metaslabs
2983 if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
2984 vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
2985 tvd->vdev_state != VDEV_STATE_HEALTHY) {
2986 if (ztest_opts.zo_verbose >= 5) {
2987 (void) printf("Could not expand LUN because "
2988 "the vdev configuration changed.\n");
2990 spa_config_exit(spa, SCL_STATE, spa);
2991 mutex_exit(&ztest_vdev_lock);
2995 spa_config_exit(spa, SCL_STATE, spa);
2998 * Expanding the LUN will update the config asynchronously,
2999 * thus we must wait for the async thread to complete any
3000 * pending tasks before proceeding.
3004 mutex_enter(&spa->spa_async_lock);
3005 done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
3006 mutex_exit(&spa->spa_async_lock);
3009 txg_wait_synced(spa_get_dsl(spa), 0);
3010 (void) poll(NULL, 0, 100);
3013 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3015 tvd = spa->spa_root_vdev->vdev_child[top];
3016 new_ms_count = tvd->vdev_ms_count;
3017 new_class_space = metaslab_class_get_space(mc);
3019 if (tvd->vdev_mg != mg || mg->mg_class != mc) {
3020 if (ztest_opts.zo_verbose >= 5) {
3021 (void) printf("Could not verify LUN expansion due to "
3022 "intervening vdev offline or remove.\n");
3024 spa_config_exit(spa, SCL_STATE, spa);
3025 mutex_exit(&ztest_vdev_lock);
3030 * Make sure we were able to grow the vdev.
3032 if (new_ms_count <= old_ms_count)
3033 fatal(0, "LUN expansion failed: ms_count %llu <= %llu\n",
3034 old_ms_count, new_ms_count);
3037 * Make sure we were able to grow the pool.
3039 if (new_class_space <= old_class_space)
3040 fatal(0, "LUN expansion failed: class_space %llu <= %llu\n",
3041 old_class_space, new_class_space);
3043 if (ztest_opts.zo_verbose >= 5) {
3044 char oldnumbuf[6], newnumbuf[6];
3046 nicenum(old_class_space, oldnumbuf);
3047 nicenum(new_class_space, newnumbuf);
3048 (void) printf("%s grew from %s to %s\n",
3049 spa->spa_name, oldnumbuf, newnumbuf);
3052 spa_config_exit(spa, SCL_STATE, spa);
3053 mutex_exit(&ztest_vdev_lock);
3057 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
3061 ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3064 * Create the objects common to all ztest datasets.
3066 VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
3067 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
3071 ztest_dataset_create(char *dsname)
3073 uint64_t zilset = ztest_random(100);
3074 int err = dmu_objset_create(dsname, DMU_OST_OTHER, 0,
3075 ztest_objset_create_cb, NULL);
3077 if (err || zilset < 80)
3080 if (ztest_opts.zo_verbose >= 5)
3081 (void) printf("Setting dataset %s to sync always\n", dsname);
3082 return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC,
3083 ZFS_SYNC_ALWAYS, B_FALSE));
3088 ztest_objset_destroy_cb(const char *name, void *arg)
3091 dmu_object_info_t doi;
3095 * Verify that the dataset contains a directory object.
3097 VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os));
3098 error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
3099 if (error != ENOENT) {
3100 /* We could have crashed in the middle of destroying it */
3101 ASSERT3U(error, ==, 0);
3102 ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
3103 ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
3105 dmu_objset_rele(os, FTAG);
3108 * Destroy the dataset.
3110 VERIFY3U(0, ==, dmu_objset_destroy(name, B_FALSE));
3115 ztest_snapshot_create(char *osname, uint64_t id)
3117 char snapname[MAXNAMELEN];
3120 (void) snprintf(snapname, MAXNAMELEN, "%s@%llu", osname,
3123 error = dmu_objset_snapshot(osname, strchr(snapname, '@') + 1,
3124 NULL, NULL, B_FALSE, B_FALSE, -1);
3125 if (error == ENOSPC) {
3126 ztest_record_enospc(FTAG);
3129 if (error != 0 && error != EEXIST)
3130 fatal(0, "ztest_snapshot_create(%s) = %d", snapname, error);
3135 ztest_snapshot_destroy(char *osname, uint64_t id)
3137 char snapname[MAXNAMELEN];
3140 (void) snprintf(snapname, MAXNAMELEN, "%s@%llu", osname,
3143 error = dmu_objset_destroy(snapname, B_FALSE);
3144 if (error != 0 && error != ENOENT)
3145 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
3151 ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
3161 zdtmp = umem_alloc(sizeof (ztest_ds_t), UMEM_NOFAIL);
3162 name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3164 (void) rw_enter(&ztest_name_lock, RW_READER);
3166 (void) snprintf(name, MAXNAMELEN, "%s/temp_%llu",
3167 ztest_opts.zo_pool, (u_longlong_t)id);
3170 * If this dataset exists from a previous run, process its replay log
3171 * half of the time. If we don't replay it, then dmu_objset_destroy()
3172 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
3174 if (ztest_random(2) == 0 &&
3175 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) {
3176 ztest_zd_init(zdtmp, NULL, os);
3177 zil_replay(os, zdtmp, ztest_replay_vector);
3178 ztest_zd_fini(zdtmp);
3179 dmu_objset_disown(os, FTAG);
3183 * There may be an old instance of the dataset we're about to
3184 * create lying around from a previous run. If so, destroy it
3185 * and all of its snapshots.
3187 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
3188 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
3191 * Verify that the destroyed dataset is no longer in the namespace.
3193 VERIFY3U(ENOENT, ==, dmu_objset_hold(name, FTAG, &os));
3196 * Verify that we can create a new dataset.
3198 error = ztest_dataset_create(name);
3200 if (error == ENOSPC) {
3201 ztest_record_enospc(FTAG);
3204 fatal(0, "dmu_objset_create(%s) = %d", name, error);
3208 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os));
3210 ztest_zd_init(zdtmp, NULL, os);
3213 * Open the intent log for it.
3215 zilog = zil_open(os, ztest_get_data);
3218 * Put some objects in there, do a little I/O to them,
3219 * and randomly take a couple of snapshots along the way.
3221 iters = ztest_random(5);
3222 for (i = 0; i < iters; i++) {
3223 ztest_dmu_object_alloc_free(zdtmp, id);
3224 if (ztest_random(iters) == 0)
3225 (void) ztest_snapshot_create(name, i);
3229 * Verify that we cannot create an existing dataset.
3231 VERIFY3U(EEXIST, ==,
3232 dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL));
3235 * Verify that we can hold an objset that is also owned.
3237 VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
3238 dmu_objset_rele(os2, FTAG);
3241 * Verify that we cannot own an objset that is already owned.
3244 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2));
3247 dmu_objset_disown(os, FTAG);
3248 ztest_zd_fini(zdtmp);
3250 (void) rw_exit(&ztest_name_lock);
3252 umem_free(name, MAXNAMELEN);
3253 umem_free(zdtmp, sizeof (ztest_ds_t));
3257 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3260 ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
3262 (void) rw_enter(&ztest_name_lock, RW_READER);
3263 (void) ztest_snapshot_destroy(zd->zd_name, id);
3264 (void) ztest_snapshot_create(zd->zd_name, id);
3265 (void) rw_exit(&ztest_name_lock);
3269 * Cleanup non-standard snapshots and clones.
3272 ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
3281 snap1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3282 clone1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3283 snap2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3284 clone2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3285 snap3name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3287 (void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu",
3288 osname, (u_longlong_t)id);
3289 (void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu",
3290 osname, (u_longlong_t)id);
3291 (void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu",
3292 clone1name, (u_longlong_t)id);
3293 (void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu",
3294 osname, (u_longlong_t)id);
3295 (void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu",
3296 clone1name, (u_longlong_t)id);
3298 error = dmu_objset_destroy(clone2name, B_FALSE);
3299 if (error && error != ENOENT)
3300 fatal(0, "dmu_objset_destroy(%s) = %d", clone2name, error);
3301 error = dmu_objset_destroy(snap3name, B_FALSE);
3302 if (error && error != ENOENT)
3303 fatal(0, "dmu_objset_destroy(%s) = %d", snap3name, error);
3304 error = dmu_objset_destroy(snap2name, B_FALSE);
3305 if (error && error != ENOENT)
3306 fatal(0, "dmu_objset_destroy(%s) = %d", snap2name, error);
3307 error = dmu_objset_destroy(clone1name, B_FALSE);
3308 if (error && error != ENOENT)
3309 fatal(0, "dmu_objset_destroy(%s) = %d", clone1name, error);
3310 error = dmu_objset_destroy(snap1name, B_FALSE);
3311 if (error && error != ENOENT)
3312 fatal(0, "dmu_objset_destroy(%s) = %d", snap1name, error);
3314 umem_free(snap1name, MAXNAMELEN);
3315 umem_free(clone1name, MAXNAMELEN);
3316 umem_free(snap2name, MAXNAMELEN);
3317 umem_free(clone2name, MAXNAMELEN);
3318 umem_free(snap3name, MAXNAMELEN);
3322 * Verify dsl_dataset_promote handles EBUSY
3325 ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
3334 char *osname = zd->zd_name;
3337 snap1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3338 clone1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3339 snap2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3340 clone2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3341 snap3name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3343 (void) rw_enter(&ztest_name_lock, RW_READER);
3345 ztest_dsl_dataset_cleanup(osname, id);
3347 (void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu",
3348 osname, (u_longlong_t)id);
3349 (void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu",
3350 osname, (u_longlong_t)id);
3351 (void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu",
3352 clone1name, (u_longlong_t)id);
3353 (void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu",
3354 osname, (u_longlong_t)id);
3355 (void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu",
3356 clone1name, (u_longlong_t)id);
3358 error = dmu_objset_snapshot(osname, strchr(snap1name, '@')+1,
3359 NULL, NULL, B_FALSE, B_FALSE, -1);
3360 if (error && error != EEXIST) {
3361 if (error == ENOSPC) {
3362 ztest_record_enospc(FTAG);
3365 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
3368 error = dmu_objset_hold(snap1name, FTAG, &clone);
3370 fatal(0, "dmu_open_snapshot(%s) = %d", snap1name, error);
3372 error = dmu_objset_clone(clone1name, dmu_objset_ds(clone), 0);
3373 dmu_objset_rele(clone, FTAG);
3375 if (error == ENOSPC) {
3376 ztest_record_enospc(FTAG);
3379 fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
3382 error = dmu_objset_snapshot(clone1name, strchr(snap2name, '@')+1,
3383 NULL, NULL, B_FALSE, B_FALSE, -1);
3384 if (error && error != EEXIST) {
3385 if (error == ENOSPC) {
3386 ztest_record_enospc(FTAG);
3389 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
3392 error = dmu_objset_snapshot(clone1name, strchr(snap3name, '@')+1,
3393 NULL, NULL, B_FALSE, B_FALSE, -1);
3394 if (error && error != EEXIST) {
3395 if (error == ENOSPC) {
3396 ztest_record_enospc(FTAG);
3399 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
3402 error = dmu_objset_hold(snap3name, FTAG, &clone);
3404 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
3406 error = dmu_objset_clone(clone2name, dmu_objset_ds(clone), 0);
3407 dmu_objset_rele(clone, FTAG);
3409 if (error == ENOSPC) {
3410 ztest_record_enospc(FTAG);
3413 fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
3416 error = dsl_dataset_own(snap2name, B_FALSE, FTAG, &ds);
3418 fatal(0, "dsl_dataset_own(%s) = %d", snap2name, error);
3419 error = dsl_dataset_promote(clone2name, NULL);
3421 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
3423 dsl_dataset_disown(ds, FTAG);
3426 ztest_dsl_dataset_cleanup(osname, id);
3428 (void) rw_exit(&ztest_name_lock);
3430 umem_free(snap1name, MAXNAMELEN);
3431 umem_free(clone1name, MAXNAMELEN);
3432 umem_free(snap2name, MAXNAMELEN);
3433 umem_free(clone2name, MAXNAMELEN);
3434 umem_free(snap3name, MAXNAMELEN);
3437 #undef OD_ARRAY_SIZE
3438 #define OD_ARRAY_SIZE 4
3441 * Verify that dmu_object_{alloc,free} work as expected.
3444 ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
3451 size = sizeof(ztest_od_t) * OD_ARRAY_SIZE;
3452 od = umem_alloc(size, UMEM_NOFAIL);
3453 batchsize = OD_ARRAY_SIZE;
3455 for (b = 0; b < batchsize; b++)
3456 ztest_od_init(od + b, id, FTAG, b, DMU_OT_UINT64_OTHER, 0, 0);
3459 * Destroy the previous batch of objects, create a new batch,
3460 * and do some I/O on the new objects.
3462 if (ztest_object_init(zd, od, size, B_TRUE) != 0)
3465 while (ztest_random(4 * batchsize) != 0)
3466 ztest_io(zd, od[ztest_random(batchsize)].od_object,
3467 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3469 umem_free(od, size);
3472 #undef OD_ARRAY_SIZE
3473 #define OD_ARRAY_SIZE 2
3476 * Verify that dmu_{read,write} work as expected.
3479 ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
3484 objset_t *os = zd->zd_os;
3485 size = sizeof(ztest_od_t) * OD_ARRAY_SIZE;
3486 od = umem_alloc(size, UMEM_NOFAIL);
3488 int i, freeit, error;
3490 bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
3491 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3492 uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
3493 uint64_t regions = 997;
3494 uint64_t stride = 123456789ULL;
3495 uint64_t width = 40;
3496 int free_percent = 5;
3499 * This test uses two objects, packobj and bigobj, that are always
3500 * updated together (i.e. in the same tx) so that their contents are
3501 * in sync and can be compared. Their contents relate to each other
3502 * in a simple way: packobj is a dense array of 'bufwad' structures,
3503 * while bigobj is a sparse array of the same bufwads. Specifically,
3504 * for any index n, there are three bufwads that should be identical:
3506 * packobj, at offset n * sizeof (bufwad_t)
3507 * bigobj, at the head of the nth chunk
3508 * bigobj, at the tail of the nth chunk
3510 * The chunk size is arbitrary. It doesn't have to be a power of two,
3511 * and it doesn't have any relation to the object blocksize.
3512 * The only requirement is that it can hold at least two bufwads.
3514 * Normally, we write the bufwad to each of these locations.
3515 * However, free_percent of the time we instead write zeroes to
3516 * packobj and perform a dmu_free_range() on bigobj. By comparing
3517 * bigobj to packobj, we can verify that the DMU is correctly
3518 * tracking which parts of an object are allocated and free,
3519 * and that the contents of the allocated blocks are correct.
3523 * Read the directory info. If it's the first time, set things up.
3525 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, chunksize);
3526 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
3528 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
3529 umem_free(od, size);
3533 bigobj = od[0].od_object;
3534 packobj = od[1].od_object;
3535 chunksize = od[0].od_gen;
3536 ASSERT(chunksize == od[1].od_gen);
3539 * Prefetch a random chunk of the big object.
3540 * Our aim here is to get some async reads in flight
3541 * for blocks that we may free below; the DMU should
3542 * handle this race correctly.
3544 n = ztest_random(regions) * stride + ztest_random(width);
3545 s = 1 + ztest_random(2 * width - 1);
3546 dmu_prefetch(os, bigobj, n * chunksize, s * chunksize);
3549 * Pick a random index and compute the offsets into packobj and bigobj.
3551 n = ztest_random(regions) * stride + ztest_random(width);
3552 s = 1 + ztest_random(width - 1);
3554 packoff = n * sizeof (bufwad_t);
3555 packsize = s * sizeof (bufwad_t);
3557 bigoff = n * chunksize;
3558 bigsize = s * chunksize;
3560 packbuf = umem_alloc(packsize, UMEM_NOFAIL);
3561 bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
3564 * free_percent of the time, free a range of bigobj rather than
3567 freeit = (ztest_random(100) < free_percent);
3570 * Read the current contents of our objects.
3572 error = dmu_read(os, packobj, packoff, packsize, packbuf,
3574 ASSERT3U(error, ==, 0);
3575 error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
3577 ASSERT3U(error, ==, 0);
3580 * Get a tx for the mods to both packobj and bigobj.
3582 tx = dmu_tx_create(os);
3584 dmu_tx_hold_write(tx, packobj, packoff, packsize);
3587 dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
3589 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
3591 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3593 umem_free(packbuf, packsize);
3594 umem_free(bigbuf, bigsize);
3595 umem_free(od, size);
3599 dmu_object_set_checksum(os, bigobj,
3600 (enum zio_checksum)ztest_random_dsl_prop(ZFS_PROP_CHECKSUM), tx);
3602 dmu_object_set_compress(os, bigobj,
3603 (enum zio_compress)ztest_random_dsl_prop(ZFS_PROP_COMPRESSION), tx);
3606 * For each index from n to n + s, verify that the existing bufwad
3607 * in packobj matches the bufwads at the head and tail of the
3608 * corresponding chunk in bigobj. Then update all three bufwads
3609 * with the new values we want to write out.
3611 for (i = 0; i < s; i++) {
3613 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3615 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
3617 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
3619 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3620 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3622 if (pack->bw_txg > txg)
3623 fatal(0, "future leak: got %llx, open txg is %llx",
3626 if (pack->bw_data != 0 && pack->bw_index != n + i)
3627 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3628 pack->bw_index, n, i);
3630 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3631 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
3633 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
3634 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
3637 bzero(pack, sizeof (bufwad_t));
3639 pack->bw_index = n + i;
3641 pack->bw_data = 1 + ztest_random(-2ULL);
3648 * We've verified all the old bufwads, and made new ones.
3649 * Now write them out.
3651 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
3654 if (ztest_opts.zo_verbose >= 7) {
3655 (void) printf("freeing offset %llx size %llx"
3657 (u_longlong_t)bigoff,
3658 (u_longlong_t)bigsize,
3661 VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
3663 if (ztest_opts.zo_verbose >= 7) {
3664 (void) printf("writing offset %llx size %llx"
3666 (u_longlong_t)bigoff,
3667 (u_longlong_t)bigsize,
3670 dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
3676 * Sanity check the stuff we just wrote.
3679 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
3680 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
3682 VERIFY(0 == dmu_read(os, packobj, packoff,
3683 packsize, packcheck, DMU_READ_PREFETCH));
3684 VERIFY(0 == dmu_read(os, bigobj, bigoff,
3685 bigsize, bigcheck, DMU_READ_PREFETCH));
3687 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
3688 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
3690 umem_free(packcheck, packsize);
3691 umem_free(bigcheck, bigsize);
3694 umem_free(packbuf, packsize);
3695 umem_free(bigbuf, bigsize);
3696 umem_free(od, size);
3700 compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
3701 uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
3709 * For each index from n to n + s, verify that the existing bufwad
3710 * in packobj matches the bufwads at the head and tail of the
3711 * corresponding chunk in bigobj. Then update all three bufwads
3712 * with the new values we want to write out.
3714 for (i = 0; i < s; i++) {
3716 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3718 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
3720 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
3722 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3723 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3725 if (pack->bw_txg > txg)
3726 fatal(0, "future leak: got %llx, open txg is %llx",
3729 if (pack->bw_data != 0 && pack->bw_index != n + i)
3730 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3731 pack->bw_index, n, i);
3733 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3734 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
3736 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
3737 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
3739 pack->bw_index = n + i;
3741 pack->bw_data = 1 + ztest_random(-2ULL);
3748 #undef OD_ARRAY_SIZE
3749 #define OD_ARRAY_SIZE 2
3752 ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
3754 objset_t *os = zd->zd_os;
3761 bufwad_t *packbuf, *bigbuf;
3762 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3763 uint64_t blocksize = ztest_random_blocksize();
3764 uint64_t chunksize = blocksize;
3765 uint64_t regions = 997;
3766 uint64_t stride = 123456789ULL;
3768 dmu_buf_t *bonus_db;
3769 arc_buf_t **bigbuf_arcbufs;
3770 dmu_object_info_t doi;
3772 size = sizeof(ztest_od_t) * OD_ARRAY_SIZE;
3773 od = umem_alloc(size, UMEM_NOFAIL);
3776 * This test uses two objects, packobj and bigobj, that are always
3777 * updated together (i.e. in the same tx) so that their contents are
3778 * in sync and can be compared. Their contents relate to each other
3779 * in a simple way: packobj is a dense array of 'bufwad' structures,
3780 * while bigobj is a sparse array of the same bufwads. Specifically,
3781 * for any index n, there are three bufwads that should be identical:
3783 * packobj, at offset n * sizeof (bufwad_t)
3784 * bigobj, at the head of the nth chunk
3785 * bigobj, at the tail of the nth chunk
3787 * The chunk size is set equal to bigobj block size so that
3788 * dmu_assign_arcbuf() can be tested for object updates.
3792 * Read the directory info. If it's the first time, set things up.
3794 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
3795 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
3798 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
3799 umem_free(od, size);
3803 bigobj = od[0].od_object;
3804 packobj = od[1].od_object;
3805 blocksize = od[0].od_blocksize;
3806 chunksize = blocksize;
3807 ASSERT(chunksize == od[1].od_gen);
3809 VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
3810 VERIFY(ISP2(doi.doi_data_block_size));
3811 VERIFY(chunksize == doi.doi_data_block_size);
3812 VERIFY(chunksize >= 2 * sizeof (bufwad_t));
3815 * Pick a random index and compute the offsets into packobj and bigobj.
3817 n = ztest_random(regions) * stride + ztest_random(width);
3818 s = 1 + ztest_random(width - 1);
3820 packoff = n * sizeof (bufwad_t);
3821 packsize = s * sizeof (bufwad_t);
3823 bigoff = n * chunksize;
3824 bigsize = s * chunksize;
3826 packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
3827 bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
3829 VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
3831 bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
3834 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
3835 * Iteration 1 test zcopy to already referenced dbufs.
3836 * Iteration 2 test zcopy to dirty dbuf in the same txg.
3837 * Iteration 3 test zcopy to dbuf dirty in previous txg.
3838 * Iteration 4 test zcopy when dbuf is no longer dirty.
3839 * Iteration 5 test zcopy when it can't be done.
3840 * Iteration 6 one more zcopy write.
3842 for (i = 0; i < 7; i++) {
3847 * In iteration 5 (i == 5) use arcbufs
3848 * that don't match bigobj blksz to test
3849 * dmu_assign_arcbuf() when it can't directly
3850 * assign an arcbuf to a dbuf.
3852 for (j = 0; j < s; j++) {
3855 dmu_request_arcbuf(bonus_db, chunksize);
3857 bigbuf_arcbufs[2 * j] =
3858 dmu_request_arcbuf(bonus_db, chunksize / 2);
3859 bigbuf_arcbufs[2 * j + 1] =
3860 dmu_request_arcbuf(bonus_db, chunksize / 2);
3865 * Get a tx for the mods to both packobj and bigobj.
3867 tx = dmu_tx_create(os);
3869 dmu_tx_hold_write(tx, packobj, packoff, packsize);
3870 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
3872 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3874 umem_free(packbuf, packsize);
3875 umem_free(bigbuf, bigsize);
3876 for (j = 0; j < s; j++) {
3878 dmu_return_arcbuf(bigbuf_arcbufs[j]);
3881 bigbuf_arcbufs[2 * j]);
3883 bigbuf_arcbufs[2 * j + 1]);
3886 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
3887 umem_free(od, size);
3888 dmu_buf_rele(bonus_db, FTAG);
3893 * 50% of the time don't read objects in the 1st iteration to
3894 * test dmu_assign_arcbuf() for the case when there're no
3895 * existing dbufs for the specified offsets.
3897 if (i != 0 || ztest_random(2) != 0) {
3898 error = dmu_read(os, packobj, packoff,
3899 packsize, packbuf, DMU_READ_PREFETCH);
3900 ASSERT3U(error, ==, 0);
3901 error = dmu_read(os, bigobj, bigoff, bigsize,
3902 bigbuf, DMU_READ_PREFETCH);
3903 ASSERT3U(error, ==, 0);
3905 compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
3909 * We've verified all the old bufwads, and made new ones.
3910 * Now write them out.
3912 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
3913 if (ztest_opts.zo_verbose >= 7) {
3914 (void) printf("writing offset %llx size %llx"
3916 (u_longlong_t)bigoff,
3917 (u_longlong_t)bigsize,
3920 for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
3923 bcopy((caddr_t)bigbuf + (off - bigoff),
3924 bigbuf_arcbufs[j]->b_data, chunksize);
3926 bcopy((caddr_t)bigbuf + (off - bigoff),
3927 bigbuf_arcbufs[2 * j]->b_data,
3929 bcopy((caddr_t)bigbuf + (off - bigoff) +
3931 bigbuf_arcbufs[2 * j + 1]->b_data,
3936 VERIFY(dmu_buf_hold(os, bigobj, off,
3937 FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0);
3940 dmu_assign_arcbuf(bonus_db, off,
3941 bigbuf_arcbufs[j], tx);
3943 dmu_assign_arcbuf(bonus_db, off,
3944 bigbuf_arcbufs[2 * j], tx);
3945 dmu_assign_arcbuf(bonus_db,
3946 off + chunksize / 2,
3947 bigbuf_arcbufs[2 * j + 1], tx);
3950 dmu_buf_rele(dbt, FTAG);
3956 * Sanity check the stuff we just wrote.
3959 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
3960 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
3962 VERIFY(0 == dmu_read(os, packobj, packoff,
3963 packsize, packcheck, DMU_READ_PREFETCH));
3964 VERIFY(0 == dmu_read(os, bigobj, bigoff,
3965 bigsize, bigcheck, DMU_READ_PREFETCH));
3967 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
3968 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
3970 umem_free(packcheck, packsize);
3971 umem_free(bigcheck, bigsize);
3974 txg_wait_open(dmu_objset_pool(os), 0);
3975 } else if (i == 3) {
3976 txg_wait_synced(dmu_objset_pool(os), 0);
3980 dmu_buf_rele(bonus_db, FTAG);
3981 umem_free(packbuf, packsize);
3982 umem_free(bigbuf, bigsize);
3983 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
3984 umem_free(od, size);
3989 ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
3993 od = umem_alloc(sizeof(ztest_od_t), UMEM_NOFAIL);
3994 uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
3995 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3998 * Have multiple threads write to large offsets in an object
3999 * to verify that parallel writes to an object -- even to the
4000 * same blocks within the object -- doesn't cause any trouble.
4002 ztest_od_init(od, ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
4004 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0)
4007 while (ztest_random(10) != 0)
4008 ztest_io(zd, od->od_object, offset);
4010 umem_free(od, sizeof(ztest_od_t));
4014 ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
4017 uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
4018 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4019 uint64_t count = ztest_random(20) + 1;
4020 uint64_t blocksize = ztest_random_blocksize();
4023 od = umem_alloc(sizeof(ztest_od_t), UMEM_NOFAIL);
4025 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
4027 if (ztest_object_init(zd, od, sizeof (ztest_od_t), !ztest_random(2)) != 0) {
4028 umem_free(od, sizeof(ztest_od_t));
4032 if (ztest_truncate(zd, od->od_object, offset, count * blocksize) != 0) {
4033 umem_free(od, sizeof(ztest_od_t));
4037 ztest_prealloc(zd, od->od_object, offset, count * blocksize);
4039 data = umem_zalloc(blocksize, UMEM_NOFAIL);
4041 while (ztest_random(count) != 0) {
4042 uint64_t randoff = offset + (ztest_random(count) * blocksize);
4043 if (ztest_write(zd, od->od_object, randoff, blocksize,
4046 while (ztest_random(4) != 0)
4047 ztest_io(zd, od->od_object, randoff);
4050 umem_free(data, blocksize);
4051 umem_free(od, sizeof(ztest_od_t));
4055 * Verify that zap_{create,destroy,add,remove,update} work as expected.
4057 #define ZTEST_ZAP_MIN_INTS 1
4058 #define ZTEST_ZAP_MAX_INTS 4
4059 #define ZTEST_ZAP_MAX_PROPS 1000
4062 ztest_zap(ztest_ds_t *zd, uint64_t id)
4064 objset_t *os = zd->zd_os;
4067 uint64_t txg, last_txg;
4068 uint64_t value[ZTEST_ZAP_MAX_INTS];
4069 uint64_t zl_ints, zl_intsize, prop;
4072 char propname[100], txgname[100];
4074 char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
4076 od = umem_alloc(sizeof(ztest_od_t), UMEM_NOFAIL);
4077 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
4079 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4080 !ztest_random(2)) != 0)
4083 object = od->od_object;
4086 * Generate a known hash collision, and verify that
4087 * we can lookup and remove both entries.
4089 tx = dmu_tx_create(os);
4090 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4091 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4094 for (i = 0; i < 2; i++) {
4096 VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
4099 for (i = 0; i < 2; i++) {
4100 VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
4101 sizeof (uint64_t), 1, &value[i], tx));
4103 zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
4104 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4105 ASSERT3U(zl_ints, ==, 1);
4107 for (i = 0; i < 2; i++) {
4108 VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
4113 * Generate a buch of random entries.
4115 ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
4117 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4118 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4119 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4120 bzero(value, sizeof (value));
4124 * If these zap entries already exist, validate their contents.
4126 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4128 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4129 ASSERT3U(zl_ints, ==, 1);
4131 VERIFY(zap_lookup(os, object, txgname, zl_intsize,
4132 zl_ints, &last_txg) == 0);
4134 VERIFY(zap_length(os, object, propname, &zl_intsize,
4137 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4138 ASSERT3U(zl_ints, ==, ints);
4140 VERIFY(zap_lookup(os, object, propname, zl_intsize,
4141 zl_ints, value) == 0);
4143 for (i = 0; i < ints; i++) {
4144 ASSERT3U(value[i], ==, last_txg + object + i);
4147 ASSERT3U(error, ==, ENOENT);
4151 * Atomically update two entries in our zap object.
4152 * The first is named txg_%llu, and contains the txg
4153 * in which the property was last updated. The second
4154 * is named prop_%llu, and the nth element of its value
4155 * should be txg + object + n.
4157 tx = dmu_tx_create(os);
4158 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4159 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4164 fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
4166 for (i = 0; i < ints; i++)
4167 value[i] = txg + object + i;
4169 VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
4171 VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
4177 * Remove a random pair of entries.
4179 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4180 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4181 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4183 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4185 if (error == ENOENT)
4188 ASSERT3U(error, ==, 0);
4190 tx = dmu_tx_create(os);
4191 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4192 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4195 VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
4196 VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
4199 umem_free(od, sizeof(ztest_od_t));
4203 * Testcase to test the upgrading of a microzap to fatzap.
4206 ztest_fzap(ztest_ds_t *zd, uint64_t id)
4208 objset_t *os = zd->zd_os;
4210 uint64_t object, txg;
4213 od = umem_alloc(sizeof(ztest_od_t), UMEM_NOFAIL);
4214 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
4216 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4217 !ztest_random(2)) != 0)
4219 object = od->od_object;
4222 * Add entries to this ZAP and make sure it spills over
4223 * and gets upgraded to a fatzap. Also, since we are adding
4224 * 2050 entries we should see ptrtbl growth and leaf-block split.
4226 for (i = 0; i < 2050; i++) {
4227 char name[MAXNAMELEN];
4232 (void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
4233 (u_longlong_t)id, (u_longlong_t)value);
4235 tx = dmu_tx_create(os);
4236 dmu_tx_hold_zap(tx, object, B_TRUE, name);
4237 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4240 error = zap_add(os, object, name, sizeof (uint64_t), 1,
4242 ASSERT(error == 0 || error == EEXIST);
4246 umem_free(od, sizeof(ztest_od_t));
4251 ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
4253 objset_t *os = zd->zd_os;
4255 uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
4257 int i, namelen, error;
4258 int micro = ztest_random(2);
4259 char name[20], string_value[20];
4262 od = umem_alloc(sizeof(ztest_od_t), UMEM_NOFAIL);
4263 ztest_od_init(od, ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER, 0, 0);
4265 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
4266 umem_free(od, sizeof(ztest_od_t));
4270 object = od->od_object;
4273 * Generate a random name of the form 'xxx.....' where each
4274 * x is a random printable character and the dots are dots.
4275 * There are 94 such characters, and the name length goes from
4276 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
4278 namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
4280 for (i = 0; i < 3; i++)
4281 name[i] = '!' + ztest_random('~' - '!' + 1);
4282 for (; i < namelen - 1; i++)
4286 if ((namelen & 1) || micro) {
4287 wsize = sizeof (txg);
4293 data = string_value;
4297 VERIFY(zap_count(os, object, &count) == 0);
4298 ASSERT(count != -1ULL);
4301 * Select an operation: length, lookup, add, update, remove.
4303 i = ztest_random(5);
4306 tx = dmu_tx_create(os);
4307 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4308 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4311 bcopy(name, string_value, namelen);
4315 bzero(string_value, namelen);
4321 error = zap_length(os, object, name, &zl_wsize, &zl_wc);
4323 ASSERT3U(wsize, ==, zl_wsize);
4324 ASSERT3U(wc, ==, zl_wc);
4326 ASSERT3U(error, ==, ENOENT);
4331 error = zap_lookup(os, object, name, wsize, wc, data);
4333 if (data == string_value &&
4334 bcmp(name, data, namelen) != 0)
4335 fatal(0, "name '%s' != val '%s' len %d",
4336 name, data, namelen);
4338 ASSERT3U(error, ==, ENOENT);
4343 error = zap_add(os, object, name, wsize, wc, data, tx);
4344 ASSERT(error == 0 || error == EEXIST);
4348 VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
4352 error = zap_remove(os, object, name, tx);
4353 ASSERT(error == 0 || error == ENOENT);
4360 umem_free(od, sizeof(ztest_od_t));
4364 * Commit callback data.
4366 typedef struct ztest_cb_data {
4367 list_node_t zcd_node;
4369 int zcd_expected_err;
4370 boolean_t zcd_added;
4371 boolean_t zcd_called;
4375 /* This is the actual commit callback function */
4377 ztest_commit_callback(void *arg, int error)
4379 ztest_cb_data_t *data = arg;
4380 uint64_t synced_txg;
4382 VERIFY(data != NULL);
4383 VERIFY3S(data->zcd_expected_err, ==, error);
4384 VERIFY(!data->zcd_called);
4386 synced_txg = spa_last_synced_txg(data->zcd_spa);
4387 if (data->zcd_txg > synced_txg)
4388 fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
4389 ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
4392 data->zcd_called = B_TRUE;
4394 if (error == ECANCELED) {
4395 ASSERT3U(data->zcd_txg, ==, 0);
4396 ASSERT(!data->zcd_added);
4399 * The private callback data should be destroyed here, but
4400 * since we are going to check the zcd_called field after
4401 * dmu_tx_abort(), we will destroy it there.
4406 ASSERT(data->zcd_added);
4407 ASSERT3U(data->zcd_txg, !=, 0);
4409 (void) mutex_enter(&zcl.zcl_callbacks_lock);
4411 /* See if this cb was called more quickly */
4412 if ((synced_txg - data->zcd_txg) < zc_min_txg_delay)
4413 zc_min_txg_delay = synced_txg - data->zcd_txg;
4415 /* Remove our callback from the list */
4416 list_remove(&zcl.zcl_callbacks, data);
4418 (void) mutex_exit(&zcl.zcl_callbacks_lock);
4420 umem_free(data, sizeof (ztest_cb_data_t));
4423 /* Allocate and initialize callback data structure */
4424 static ztest_cb_data_t *
4425 ztest_create_cb_data(objset_t *os, uint64_t txg)
4427 ztest_cb_data_t *cb_data;
4429 cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
4431 cb_data->zcd_txg = txg;
4432 cb_data->zcd_spa = dmu_objset_spa(os);
4433 list_link_init(&cb_data->zcd_node);
4439 * Commit callback test.
4442 ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
4444 objset_t *os = zd->zd_os;
4447 ztest_cb_data_t *cb_data[3], *tmp_cb;
4448 uint64_t old_txg, txg;
4451 od = umem_alloc(sizeof(ztest_od_t), UMEM_NOFAIL);
4452 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
4454 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
4455 umem_free(od, sizeof(ztest_od_t));
4459 tx = dmu_tx_create(os);
4461 cb_data[0] = ztest_create_cb_data(os, 0);
4462 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
4464 dmu_tx_hold_write(tx, od->od_object, 0, sizeof (uint64_t));
4466 /* Every once in a while, abort the transaction on purpose */
4467 if (ztest_random(100) == 0)
4471 error = dmu_tx_assign(tx, TXG_NOWAIT);
4473 txg = error ? 0 : dmu_tx_get_txg(tx);
4475 cb_data[0]->zcd_txg = txg;
4476 cb_data[1] = ztest_create_cb_data(os, txg);
4477 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
4481 * It's not a strict requirement to call the registered
4482 * callbacks from inside dmu_tx_abort(), but that's what
4483 * it's supposed to happen in the current implementation
4484 * so we will check for that.
4486 for (i = 0; i < 2; i++) {
4487 cb_data[i]->zcd_expected_err = ECANCELED;
4488 VERIFY(!cb_data[i]->zcd_called);
4493 for (i = 0; i < 2; i++) {
4494 VERIFY(cb_data[i]->zcd_called);
4495 umem_free(cb_data[i], sizeof (ztest_cb_data_t));
4498 umem_free(od, sizeof(ztest_od_t));
4502 cb_data[2] = ztest_create_cb_data(os, txg);
4503 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
4506 * Read existing data to make sure there isn't a future leak.
4508 VERIFY(0 == dmu_read(os, od->od_object, 0, sizeof (uint64_t),
4509 &old_txg, DMU_READ_PREFETCH));
4512 fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
4515 dmu_write(os, od->od_object, 0, sizeof (uint64_t), &txg, tx);
4517 (void) mutex_enter(&zcl.zcl_callbacks_lock);
4520 * Since commit callbacks don't have any ordering requirement and since
4521 * it is theoretically possible for a commit callback to be called
4522 * after an arbitrary amount of time has elapsed since its txg has been
4523 * synced, it is difficult to reliably determine whether a commit
4524 * callback hasn't been called due to high load or due to a flawed
4527 * In practice, we will assume that if after a certain number of txgs a
4528 * commit callback hasn't been called, then most likely there's an
4529 * implementation bug..
4531 tmp_cb = list_head(&zcl.zcl_callbacks);
4532 if (tmp_cb != NULL &&
4533 tmp_cb->zcd_txg + ZTEST_COMMIT_CB_THRESH < txg) {
4534 fatal(0, "Commit callback threshold exceeded, oldest txg: %"
4535 PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
4539 * Let's find the place to insert our callbacks.
4541 * Even though the list is ordered by txg, it is possible for the
4542 * insertion point to not be the end because our txg may already be
4543 * quiescing at this point and other callbacks in the open txg
4544 * (from other objsets) may have sneaked in.
4546 tmp_cb = list_tail(&zcl.zcl_callbacks);
4547 while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
4548 tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
4550 /* Add the 3 callbacks to the list */
4551 for (i = 0; i < 3; i++) {
4553 list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
4555 list_insert_after(&zcl.zcl_callbacks, tmp_cb,
4558 cb_data[i]->zcd_added = B_TRUE;
4559 VERIFY(!cb_data[i]->zcd_called);
4561 tmp_cb = cb_data[i];
4566 (void) mutex_exit(&zcl.zcl_callbacks_lock);
4570 umem_free(od, sizeof(ztest_od_t));
4575 ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
4577 zfs_prop_t proplist[] = {
4579 ZFS_PROP_COMPRESSION,
4585 (void) rw_enter(&ztest_name_lock, RW_READER);
4587 for (p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
4588 (void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
4589 ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
4591 (void) rw_exit(&ztest_name_lock);
4596 ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
4598 nvlist_t *props = NULL;
4600 (void) rw_enter(&ztest_name_lock, RW_READER);
4602 (void) ztest_spa_prop_set_uint64(ZPOOL_PROP_DEDUPDITTO,
4603 ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
4605 VERIFY3U(spa_prop_get(ztest_spa, &props), ==, 0);
4607 if (ztest_opts.zo_verbose >= 6)
4608 dump_nvlist(props, 4);
4612 (void) rw_exit(&ztest_name_lock);
4616 * Test snapshot hold/release and deferred destroy.
4619 ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
4622 objset_t *os = zd->zd_os;
4626 char clonename[100];
4628 char osname[MAXNAMELEN];
4630 (void) rw_enter(&ztest_name_lock, RW_READER);
4632 dmu_objset_name(os, osname);
4634 (void) snprintf(snapname, 100, "sh1_%llu", (u_longlong_t)id);
4635 (void) snprintf(fullname, 100, "%s@%s", osname, snapname);
4636 (void) snprintf(clonename, 100, "%s/ch1_%llu",osname,(u_longlong_t)id);
4637 (void) snprintf(tag, 100, "tag_%llu", (u_longlong_t)id);
4640 * Clean up from any previous run.
4642 (void) dmu_objset_destroy(clonename, B_FALSE);
4643 (void) dsl_dataset_user_release(osname, snapname, tag, B_FALSE);
4644 (void) dmu_objset_destroy(fullname, B_FALSE);
4647 * Create snapshot, clone it, mark snap for deferred destroy,
4648 * destroy clone, verify snap was also destroyed.
4650 error = dmu_objset_snapshot(osname, snapname, NULL, NULL, FALSE,
4653 if (error == ENOSPC) {
4654 ztest_record_enospc("dmu_objset_snapshot");
4657 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
4660 error = dmu_objset_hold(fullname, FTAG, &origin);
4662 fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
4664 error = dmu_objset_clone(clonename, dmu_objset_ds(origin), 0);
4665 dmu_objset_rele(origin, FTAG);
4667 if (error == ENOSPC) {
4668 ztest_record_enospc("dmu_objset_clone");
4671 fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
4674 error = dmu_objset_destroy(fullname, B_TRUE);
4676 fatal(0, "dmu_objset_destroy(%s, B_TRUE) = %d",
4680 error = dmu_objset_destroy(clonename, B_FALSE);
4682 fatal(0, "dmu_objset_destroy(%s) = %d", clonename, error);
4684 error = dmu_objset_hold(fullname, FTAG, &origin);
4685 if (error != ENOENT)
4686 fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
4689 * Create snapshot, add temporary hold, verify that we can't
4690 * destroy a held snapshot, mark for deferred destroy,
4691 * release hold, verify snapshot was destroyed.
4693 error = dmu_objset_snapshot(osname, snapname, NULL, NULL, FALSE,
4696 if (error == ENOSPC) {
4697 ztest_record_enospc("dmu_objset_snapshot");
4700 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
4703 error = dsl_dataset_user_hold(osname, snapname, tag, B_FALSE,
4706 fatal(0, "dsl_dataset_user_hold(%s)", fullname, tag);
4708 error = dmu_objset_destroy(fullname, B_FALSE);
4709 if (error != EBUSY) {
4710 fatal(0, "dmu_objset_destroy(%s, B_FALSE) = %d",
4714 error = dmu_objset_destroy(fullname, B_TRUE);
4716 fatal(0, "dmu_objset_destroy(%s, B_TRUE) = %d",
4720 error = dsl_dataset_user_release(osname, snapname, tag, B_FALSE);
4722 fatal(0, "dsl_dataset_user_release(%s)", fullname, tag);
4724 VERIFY(dmu_objset_hold(fullname, FTAG, &origin) == ENOENT);
4727 (void) rw_exit(&ztest_name_lock);
4731 * Inject random faults into the on-disk data.
4735 ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
4737 ztest_shared_t *zs = ztest_shared;
4738 spa_t *spa = ztest_spa;
4742 uint64_t bad = 0x1990c0ffeedecadeull;
4747 int bshift = SPA_MAXBLOCKSHIFT + 2; /* don't scrog all labels */
4753 boolean_t islog = B_FALSE;
4755 path0 = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
4756 pathrand = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
4758 mutex_enter(&ztest_vdev_lock);
4759 maxfaults = MAXFAULTS();
4760 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
4761 mirror_save = zs->zs_mirrors;
4762 mutex_exit(&ztest_vdev_lock);
4764 ASSERT(leaves >= 1);
4767 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
4769 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
4771 if (ztest_random(2) == 0) {
4773 * Inject errors on a normal data device or slog device.
4775 top = ztest_random_vdev_top(spa, B_TRUE);
4776 leaf = ztest_random(leaves) + zs->zs_splits;
4779 * Generate paths to the first leaf in this top-level vdev,
4780 * and to the random leaf we selected. We'll induce transient
4781 * write failures and random online/offline activity on leaf 0,
4782 * and we'll write random garbage to the randomly chosen leaf.
4784 (void) snprintf(path0, MAXPATHLEN, ztest_dev_template,
4785 ztest_opts.zo_dir, ztest_opts.zo_pool,
4786 top * leaves + zs->zs_splits);
4787 (void) snprintf(pathrand, MAXPATHLEN, ztest_dev_template,
4788 ztest_opts.zo_dir, ztest_opts.zo_pool,
4789 top * leaves + leaf);
4791 vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
4792 if (vd0 != NULL && vd0->vdev_top->vdev_islog)
4795 if (vd0 != NULL && maxfaults != 1) {
4797 * Make vd0 explicitly claim to be unreadable,
4798 * or unwriteable, or reach behind its back
4799 * and close the underlying fd. We can do this if
4800 * maxfaults == 0 because we'll fail and reexecute,
4801 * and we can do it if maxfaults >= 2 because we'll
4802 * have enough redundancy. If maxfaults == 1, the
4803 * combination of this with injection of random data
4804 * corruption below exceeds the pool's fault tolerance.
4806 vdev_file_t *vf = vd0->vdev_tsd;
4808 if (vf != NULL && ztest_random(3) == 0) {
4809 (void) close(vf->vf_vnode->v_fd);
4810 vf->vf_vnode->v_fd = -1;
4811 } else if (ztest_random(2) == 0) {
4812 vd0->vdev_cant_read = B_TRUE;
4814 vd0->vdev_cant_write = B_TRUE;
4816 guid0 = vd0->vdev_guid;
4820 * Inject errors on an l2cache device.
4822 spa_aux_vdev_t *sav = &spa->spa_l2cache;
4824 if (sav->sav_count == 0) {
4825 spa_config_exit(spa, SCL_STATE, FTAG);
4828 vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
4829 guid0 = vd0->vdev_guid;
4830 (void) strcpy(path0, vd0->vdev_path);
4831 (void) strcpy(pathrand, vd0->vdev_path);
4835 maxfaults = INT_MAX; /* no limit on cache devices */
4838 spa_config_exit(spa, SCL_STATE, FTAG);
4841 * If we can tolerate two or more faults, or we're dealing
4842 * with a slog, randomly online/offline vd0.
4844 if ((maxfaults >= 2 || islog) && guid0 != 0) {
4845 if (ztest_random(10) < 6) {
4846 int flags = (ztest_random(2) == 0 ?
4847 ZFS_OFFLINE_TEMPORARY : 0);
4850 * We have to grab the zs_name_lock as writer to
4851 * prevent a race between offlining a slog and
4852 * destroying a dataset. Offlining the slog will
4853 * grab a reference on the dataset which may cause
4854 * dmu_objset_destroy() to fail with EBUSY thus
4855 * leaving the dataset in an inconsistent state.
4858 (void) rw_enter(&ztest_name_lock,
4861 VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
4864 (void) rw_exit(&ztest_name_lock);
4866 (void) vdev_online(spa, guid0, 0, NULL);
4874 * We have at least single-fault tolerance, so inject data corruption.
4876 fd = open(pathrand, O_RDWR);
4878 if (fd == -1) /* we hit a gap in the device namespace */
4881 fsize = lseek(fd, 0, SEEK_END);
4883 while (--iters != 0) {
4884 offset = ztest_random(fsize / (leaves << bshift)) *
4885 (leaves << bshift) + (leaf << bshift) +
4886 (ztest_random(1ULL << (bshift - 1)) & -8ULL);
4888 if (offset >= fsize)
4891 mutex_enter(&ztest_vdev_lock);
4892 if (mirror_save != zs->zs_mirrors) {
4893 mutex_exit(&ztest_vdev_lock);
4898 if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
4899 fatal(1, "can't inject bad word at 0x%llx in %s",
4902 mutex_exit(&ztest_vdev_lock);
4904 if (ztest_opts.zo_verbose >= 7)
4905 (void) printf("injected bad word into %s,"
4906 " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
4911 umem_free(path0, MAXPATHLEN);
4912 umem_free(pathrand, MAXPATHLEN);
4916 * Verify that DDT repair works as expected.
4919 ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
4921 ztest_shared_t *zs = ztest_shared;
4922 spa_t *spa = ztest_spa;
4923 objset_t *os = zd->zd_os;
4925 uint64_t object, blocksize, txg, pattern, psize;
4926 enum zio_checksum checksum = spa_dedup_checksum(spa);
4931 int copies = 2 * ZIO_DEDUPDITTO_MIN;
4934 blocksize = ztest_random_blocksize();
4935 blocksize = MIN(blocksize, 2048); /* because we write so many */
4937 od = umem_alloc(sizeof(ztest_od_t), UMEM_NOFAIL);
4938 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
4940 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
4941 umem_free(od, sizeof(ztest_od_t));
4946 * Take the name lock as writer to prevent anyone else from changing
4947 * the pool and dataset properies we need to maintain during this test.
4949 (void) rw_enter(&ztest_name_lock, RW_WRITER);
4951 if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
4953 ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
4955 (void) rw_exit(&ztest_name_lock);
4956 umem_free(od, sizeof(ztest_od_t));
4960 object = od[0].od_object;
4961 blocksize = od[0].od_blocksize;
4962 pattern = zs->zs_guid ^ dmu_objset_fsid_guid(os);
4964 ASSERT(object != 0);
4966 tx = dmu_tx_create(os);
4967 dmu_tx_hold_write(tx, object, 0, copies * blocksize);
4968 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
4970 (void) rw_exit(&ztest_name_lock);
4971 umem_free(od, sizeof(ztest_od_t));
4976 * Write all the copies of our block.
4978 for (i = 0; i < copies; i++) {
4979 uint64_t offset = i * blocksize;
4980 VERIFY(dmu_buf_hold(os, object, offset, FTAG, &db,
4981 DMU_READ_NO_PREFETCH) == 0);
4982 ASSERT(db->db_offset == offset);
4983 ASSERT(db->db_size == blocksize);
4984 ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
4985 ztest_pattern_match(db->db_data, db->db_size, 0ULL));
4986 dmu_buf_will_fill(db, tx);
4987 ztest_pattern_set(db->db_data, db->db_size, pattern);
4988 dmu_buf_rele(db, FTAG);
4992 txg_wait_synced(spa_get_dsl(spa), txg);
4995 * Find out what block we got.
4997 VERIFY(dmu_buf_hold(os, object, 0, FTAG, &db,
4998 DMU_READ_NO_PREFETCH) == 0);
4999 blk = *((dmu_buf_impl_t *)db)->db_blkptr;
5000 dmu_buf_rele(db, FTAG);
5003 * Damage the block. Dedup-ditto will save us when we read it later.
5005 psize = BP_GET_PSIZE(&blk);
5006 buf = zio_buf_alloc(psize);
5007 ztest_pattern_set(buf, psize, ~pattern);
5009 (void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
5010 buf, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
5011 ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
5013 zio_buf_free(buf, psize);
5015 (void) rw_exit(&ztest_name_lock);
5016 umem_free(od, sizeof(ztest_od_t));
5024 ztest_scrub(ztest_ds_t *zd, uint64_t id)
5026 spa_t *spa = ztest_spa;
5028 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5029 (void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
5030 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5034 * Change the guid for the pool.
5038 ztest_reguid(ztest_ds_t *zd, uint64_t id)
5040 spa_t *spa = ztest_spa;
5041 uint64_t orig, load;
5043 orig = spa_guid(spa);
5044 load = spa_load_guid(spa);
5045 if (spa_change_guid(spa) != 0)
5048 if (ztest_opts.zo_verbose >= 3) {
5049 (void) printf("Changed guid old %llu -> %llu\n",
5050 (u_longlong_t)orig, (u_longlong_t)spa_guid(spa));
5053 VERIFY3U(orig, !=, spa_guid(spa));
5054 VERIFY3U(load, ==, spa_load_guid(spa));
5058 * Rename the pool to a different name and then rename it back.
5062 ztest_spa_rename(ztest_ds_t *zd, uint64_t id)
5064 char *oldname, *newname;
5067 (void) rw_enter(&ztest_name_lock, RW_WRITER);
5069 oldname = ztest_opts.zo_pool;
5070 newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
5071 (void) strcpy(newname, oldname);
5072 (void) strcat(newname, "_tmp");
5077 VERIFY3U(0, ==, spa_rename(oldname, newname));
5080 * Try to open it under the old name, which shouldn't exist
5082 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5085 * Open it under the new name and make sure it's still the same spa_t.
5087 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
5089 ASSERT(spa == ztest_spa);
5090 spa_close(spa, FTAG);
5093 * Rename it back to the original
5095 VERIFY3U(0, ==, spa_rename(newname, oldname));
5098 * Make sure it can still be opened
5100 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
5102 ASSERT(spa == ztest_spa);
5103 spa_close(spa, FTAG);
5105 umem_free(newname, strlen(newname) + 1);
5107 (void) rw_exit(&ztest_name_lock);
5111 * Verify pool integrity by running zdb.
5114 ztest_run_zdb(char *pool)
5122 bin = umem_alloc(MAXPATHLEN + MAXNAMELEN + 20, UMEM_NOFAIL);
5123 zdb = umem_alloc(MAXPATHLEN + MAXNAMELEN + 20, UMEM_NOFAIL);
5124 zbuf = umem_alloc(1024, UMEM_NOFAIL);
5126 VERIFY(realpath(getexecname(), bin) != NULL);
5127 if (strncmp(bin, "/usr/sbin/ztest", 15) == 0) {
5128 strcpy(bin, "/usr/sbin/zdb"); /* Installed */
5129 } else if (strncmp(bin, "/sbin/ztest", 11) == 0) {
5130 strcpy(bin, "/sbin/zdb"); /* Installed */
5132 strstr(bin, "/ztest/")[0] = '\0'; /* In-tree */
5133 strcat(bin, "/zdb/zdb");
5137 "%s -bcc%s%s -U %s %s",
5139 ztest_opts.zo_verbose >= 3 ? "s" : "",
5140 ztest_opts.zo_verbose >= 4 ? "v" : "",
5144 if (ztest_opts.zo_verbose >= 5)
5145 (void) printf("Executing %s\n", strstr(zdb, "zdb "));
5147 fp = popen(zdb, "r");
5149 while (fgets(zbuf, 1024, fp) != NULL)
5150 if (ztest_opts.zo_verbose >= 3)
5151 (void) printf("%s", zbuf);
5153 status = pclose(fp);
5158 ztest_dump_core = 0;
5159 if (WIFEXITED(status))
5160 fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
5162 fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
5164 umem_free(bin, MAXPATHLEN + MAXNAMELEN + 20);
5165 umem_free(zdb, MAXPATHLEN + MAXNAMELEN + 20);
5166 umem_free(zbuf, 1024);
5170 ztest_walk_pool_directory(char *header)
5174 if (ztest_opts.zo_verbose >= 6)
5175 (void) printf("%s\n", header);
5177 mutex_enter(&spa_namespace_lock);
5178 while ((spa = spa_next(spa)) != NULL)
5179 if (ztest_opts.zo_verbose >= 6)
5180 (void) printf("\t%s\n", spa_name(spa));
5181 mutex_exit(&spa_namespace_lock);
5185 ztest_spa_import_export(char *oldname, char *newname)
5187 nvlist_t *config, *newconfig;
5191 if (ztest_opts.zo_verbose >= 4) {
5192 (void) printf("import/export: old = %s, new = %s\n",
5197 * Clean up from previous runs.
5199 (void) spa_destroy(newname);
5202 * Get the pool's configuration and guid.
5204 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
5207 * Kick off a scrub to tickle scrub/export races.
5209 if (ztest_random(2) == 0)
5210 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5212 pool_guid = spa_guid(spa);
5213 spa_close(spa, FTAG);
5215 ztest_walk_pool_directory("pools before export");
5220 VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
5222 ztest_walk_pool_directory("pools after export");
5227 newconfig = spa_tryimport(config);
5228 ASSERT(newconfig != NULL);
5229 nvlist_free(newconfig);
5232 * Import it under the new name.
5234 VERIFY3U(0, ==, spa_import(newname, config, NULL, 0));
5236 ztest_walk_pool_directory("pools after import");
5239 * Try to import it again -- should fail with EEXIST.
5241 VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL, 0));
5244 * Try to import it under a different name -- should fail with EEXIST.
5246 VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL, 0));
5249 * Verify that the pool is no longer visible under the old name.
5251 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5254 * Verify that we can open and close the pool using the new name.
5256 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
5257 ASSERT(pool_guid == spa_guid(spa));
5258 spa_close(spa, FTAG);
5260 nvlist_free(config);
5264 ztest_resume(spa_t *spa)
5266 if (spa_suspended(spa) && ztest_opts.zo_verbose >= 6)
5267 (void) printf("resuming from suspended state\n");
5268 spa_vdev_state_enter(spa, SCL_NONE);
5269 vdev_clear(spa, NULL);
5270 (void) spa_vdev_state_exit(spa, NULL, 0);
5271 (void) zio_resume(spa);
5275 ztest_resume_thread(void *arg)
5279 while (!ztest_exiting) {
5280 if (spa_suspended(spa))
5282 (void) poll(NULL, 0, 100);
5293 ztest_deadman_alarm(int sig)
5295 fatal(0, "failed to complete within %d seconds of deadline", GRACE);
5299 ztest_execute(int test, ztest_info_t *zi, uint64_t id)
5301 ztest_ds_t *zd = &ztest_ds[id % ztest_opts.zo_datasets];
5302 ztest_shared_callstate_t *zc = ZTEST_GET_SHARED_CALLSTATE(test);
5303 hrtime_t functime = gethrtime();
5306 for (i = 0; i < zi->zi_iters; i++)
5307 zi->zi_func(zd, id);
5309 functime = gethrtime() - functime;
5311 atomic_add_64(&zc->zc_count, 1);
5312 atomic_add_64(&zc->zc_time, functime);
5314 if (ztest_opts.zo_verbose >= 4) {
5316 (void) dladdr((void *)zi->zi_func, &dli);
5317 (void) printf("%6.2f sec in %s\n",
5318 (double)functime / NANOSEC, dli.dli_sname);
5323 ztest_thread(void *arg)
5326 uint64_t id = (uintptr_t)arg;
5327 ztest_shared_t *zs = ztest_shared;
5331 ztest_shared_callstate_t *zc;
5333 while ((now = gethrtime()) < zs->zs_thread_stop) {
5335 * See if it's time to force a crash.
5337 if (now > zs->zs_thread_kill)
5341 * If we're getting ENOSPC with some regularity, stop.
5343 if (zs->zs_enospc_count > 10)
5347 * Pick a random function to execute.
5349 rand = ztest_random(ZTEST_FUNCS);
5350 zi = &ztest_info[rand];
5351 zc = ZTEST_GET_SHARED_CALLSTATE(rand);
5352 call_next = zc->zc_next;
5354 if (now >= call_next &&
5355 atomic_cas_64(&zc->zc_next, call_next, call_next +
5356 ztest_random(2 * zi->zi_interval[0] + 1)) == call_next) {
5357 ztest_execute(rand, zi, id);
5367 ztest_dataset_name(char *dsname, char *pool, int d)
5369 (void) snprintf(dsname, MAXNAMELEN, "%s/ds_%d", pool, d);
5373 ztest_dataset_destroy(int d)
5375 char name[MAXNAMELEN];
5378 ztest_dataset_name(name, ztest_opts.zo_pool, d);
5380 if (ztest_opts.zo_verbose >= 3)
5381 (void) printf("Destroying %s to free up space\n", name);
5384 * Cleanup any non-standard clones and snapshots. In general,
5385 * ztest thread t operates on dataset (t % zopt_datasets),
5386 * so there may be more than one thing to clean up.
5388 for (t = d; t < ztest_opts.zo_threads;
5389 t += ztest_opts.zo_datasets)
5390 ztest_dsl_dataset_cleanup(name, t);
5392 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
5393 DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
5397 ztest_dataset_dirobj_verify(ztest_ds_t *zd)
5399 uint64_t usedobjs, dirobjs, scratch;
5402 * ZTEST_DIROBJ is the object directory for the entire dataset.
5403 * Therefore, the number of objects in use should equal the
5404 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
5405 * If not, we have an object leak.
5407 * Note that we can only check this in ztest_dataset_open(),
5408 * when the open-context and syncing-context values agree.
5409 * That's because zap_count() returns the open-context value,
5410 * while dmu_objset_space() returns the rootbp fill count.
5412 VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
5413 dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
5414 ASSERT3U(dirobjs + 1, ==, usedobjs);
5418 ztest_dataset_open(int d)
5420 ztest_ds_t *zd = &ztest_ds[d];
5421 uint64_t committed_seq = ZTEST_GET_SHARED_DS(d)->zd_seq;
5424 char name[MAXNAMELEN];
5427 ztest_dataset_name(name, ztest_opts.zo_pool, d);
5429 (void) rw_enter(&ztest_name_lock, RW_READER);
5431 error = ztest_dataset_create(name);
5432 if (error == ENOSPC) {
5433 (void) rw_exit(&ztest_name_lock);
5434 ztest_record_enospc(FTAG);
5437 ASSERT(error == 0 || error == EEXIST);
5439 VERIFY3U(dmu_objset_hold(name, zd, &os), ==, 0);
5440 (void) rw_exit(&ztest_name_lock);
5442 ztest_zd_init(zd, ZTEST_GET_SHARED_DS(d), os);
5444 zilog = zd->zd_zilog;
5446 if (zilog->zl_header->zh_claim_lr_seq != 0 &&
5447 zilog->zl_header->zh_claim_lr_seq < committed_seq)
5448 fatal(0, "missing log records: claimed %llu < committed %llu",
5449 zilog->zl_header->zh_claim_lr_seq, committed_seq);
5451 ztest_dataset_dirobj_verify(zd);
5453 zil_replay(os, zd, ztest_replay_vector);
5455 ztest_dataset_dirobj_verify(zd);
5457 if (ztest_opts.zo_verbose >= 6)
5458 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
5460 (u_longlong_t)zilog->zl_parse_blk_count,
5461 (u_longlong_t)zilog->zl_parse_lr_count,
5462 (u_longlong_t)zilog->zl_replaying_seq);
5464 zilog = zil_open(os, ztest_get_data);
5466 if (zilog->zl_replaying_seq != 0 &&
5467 zilog->zl_replaying_seq < committed_seq)
5468 fatal(0, "missing log records: replayed %llu < committed %llu",
5469 zilog->zl_replaying_seq, committed_seq);
5475 ztest_dataset_close(int d)
5477 ztest_ds_t *zd = &ztest_ds[d];
5479 zil_close(zd->zd_zilog);
5480 dmu_objset_rele(zd->zd_os, zd);
5486 * Kick off threads to run tests on all datasets in parallel.
5489 ztest_run(ztest_shared_t *zs)
5494 kthread_t *resume_thread;
5499 ztest_exiting = B_FALSE;
5502 * Initialize parent/child shared state.
5504 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
5505 rw_init(&ztest_name_lock, NULL, RW_DEFAULT, NULL);
5507 zs->zs_thread_start = gethrtime();
5508 zs->zs_thread_stop =
5509 zs->zs_thread_start + ztest_opts.zo_passtime * NANOSEC;
5510 zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
5511 zs->zs_thread_kill = zs->zs_thread_stop;
5512 if (ztest_random(100) < ztest_opts.zo_killrate) {
5513 zs->zs_thread_kill -=
5514 ztest_random(ztest_opts.zo_passtime * NANOSEC);
5517 mutex_init(&zcl.zcl_callbacks_lock, NULL, MUTEX_DEFAULT, NULL);
5519 list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
5520 offsetof(ztest_cb_data_t, zcd_node));
5525 kernel_init(FREAD | FWRITE);
5526 VERIFY(spa_open(ztest_opts.zo_pool, &spa, FTAG) == 0);
5527 spa->spa_debug = B_TRUE;
5530 VERIFY3U(0, ==, dmu_objset_hold(ztest_opts.zo_pool, FTAG, &os));
5531 zs->zs_guid = dmu_objset_fsid_guid(os);
5532 dmu_objset_rele(os, FTAG);
5534 spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
5537 * We don't expect the pool to suspend unless maxfaults == 0,
5538 * in which case ztest_fault_inject() temporarily takes away
5539 * the only valid replica.
5541 if (MAXFAULTS() == 0)
5542 spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
5544 spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
5547 * Create a thread to periodically resume suspended I/O.
5549 VERIFY3P((resume_thread = zk_thread_create(NULL, 0,
5550 (thread_func_t)ztest_resume_thread, spa, TS_RUN, NULL, 0, 0,
5551 PTHREAD_CREATE_JOINABLE)), !=, NULL);
5554 * Set a deadman alarm to abort() if we hang.
5556 signal(SIGALRM, ztest_deadman_alarm);
5557 alarm((zs->zs_thread_stop - zs->zs_thread_start) / NANOSEC + GRACE);
5560 * Verify that we can safely inquire about about any object,
5561 * whether it's allocated or not. To make it interesting,
5562 * we probe a 5-wide window around each power of two.
5563 * This hits all edge cases, including zero and the max.
5565 for (t = 0; t < 64; t++) {
5566 for (d = -5; d <= 5; d++) {
5567 error = dmu_object_info(spa->spa_meta_objset,
5568 (1ULL << t) + d, NULL);
5569 ASSERT(error == 0 || error == ENOENT ||
5575 * If we got any ENOSPC errors on the previous run, destroy something.
5577 if (zs->zs_enospc_count != 0) {
5578 int d = ztest_random(ztest_opts.zo_datasets);
5579 ztest_dataset_destroy(d);
5581 zs->zs_enospc_count = 0;
5583 tid = umem_zalloc(ztest_opts.zo_threads * sizeof (kt_did_t),
5586 if (ztest_opts.zo_verbose >= 4)
5587 (void) printf("starting main threads...\n");
5590 * Kick off all the tests that run in parallel.
5592 for (t = 0; t < ztest_opts.zo_threads; t++) {
5595 if (t < ztest_opts.zo_datasets &&
5596 ztest_dataset_open(t) != 0)
5599 VERIFY3P(thread = zk_thread_create(NULL, 0,
5600 (thread_func_t)ztest_thread,
5601 (void *)(uintptr_t)t, TS_RUN, NULL, 0, 0,
5602 PTHREAD_CREATE_JOINABLE), !=, NULL);
5603 tid[t] = thread->t_tid;
5607 * Wait for all of the tests to complete. We go in reverse order
5608 * so we don't close datasets while threads are still using them.
5610 for (t = ztest_opts.zo_threads - 1; t >= 0; t--) {
5611 thread_join(tid[t]);
5612 if (t < ztest_opts.zo_datasets)
5613 ztest_dataset_close(t);
5616 txg_wait_synced(spa_get_dsl(spa), 0);
5618 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
5619 zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
5621 umem_free(tid, ztest_opts.zo_threads * sizeof (kt_did_t));
5623 /* Kill the resume thread */
5624 ztest_exiting = B_TRUE;
5625 thread_join(resume_thread->t_tid);
5629 * Right before closing the pool, kick off a bunch of async I/O;
5630 * spa_close() should wait for it to complete.
5632 for (object = 1; object < 50; object++)
5633 dmu_prefetch(spa->spa_meta_objset, object, 0, 1ULL << 20);
5635 /* Verify that at least one commit cb was called in a timely fashion */
5636 if (zc_cb_counter >= ZTEST_COMMIT_CB_MIN_REG)
5637 VERIFY3U(zc_min_txg_delay, ==, 0);
5639 spa_close(spa, FTAG);
5642 * Verify that we can loop over all pools.
5644 mutex_enter(&spa_namespace_lock);
5645 for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
5646 if (ztest_opts.zo_verbose > 3)
5647 (void) printf("spa_next: found %s\n", spa_name(spa));
5648 mutex_exit(&spa_namespace_lock);
5651 * Verify that we can export the pool and reimport it under a
5654 if (ztest_random(2) == 0) {
5655 char name[MAXNAMELEN];
5656 (void) snprintf(name, MAXNAMELEN, "%s_import",
5657 ztest_opts.zo_pool);
5658 ztest_spa_import_export(ztest_opts.zo_pool, name);
5659 ztest_spa_import_export(name, ztest_opts.zo_pool);
5664 list_destroy(&zcl.zcl_callbacks);
5665 mutex_destroy(&zcl.zcl_callbacks_lock);
5666 rw_destroy(&ztest_name_lock);
5667 mutex_destroy(&ztest_vdev_lock);
5673 ztest_ds_t *zd = &ztest_ds[0];
5677 if (ztest_opts.zo_verbose >= 3)
5678 (void) printf("testing spa_freeze()...\n");
5680 kernel_init(FREAD | FWRITE);
5681 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
5682 VERIFY3U(0, ==, ztest_dataset_open(0));
5685 * Force the first log block to be transactionally allocated.
5686 * We have to do this before we freeze the pool -- otherwise
5687 * the log chain won't be anchored.
5689 while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
5690 ztest_dmu_object_alloc_free(zd, 0);
5691 zil_commit(zd->zd_zilog, 0);
5694 txg_wait_synced(spa_get_dsl(spa), 0);
5697 * Freeze the pool. This stops spa_sync() from doing anything,
5698 * so that the only way to record changes from now on is the ZIL.
5703 * Run tests that generate log records but don't alter the pool config
5704 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
5705 * We do a txg_wait_synced() after each iteration to force the txg
5706 * to increase well beyond the last synced value in the uberblock.
5707 * The ZIL should be OK with that.
5709 while (ztest_random(10) != 0 &&
5710 numloops++ < ztest_opts.zo_maxloops) {
5711 ztest_dmu_write_parallel(zd, 0);
5712 ztest_dmu_object_alloc_free(zd, 0);
5713 txg_wait_synced(spa_get_dsl(spa), 0);
5717 * Commit all of the changes we just generated.
5719 zil_commit(zd->zd_zilog, 0);
5720 txg_wait_synced(spa_get_dsl(spa), 0);
5723 * Close our dataset and close the pool.
5725 ztest_dataset_close(0);
5726 spa_close(spa, FTAG);
5730 * Open and close the pool and dataset to induce log replay.
5732 kernel_init(FREAD | FWRITE);
5733 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
5734 VERIFY3U(0, ==, ztest_dataset_open(0));
5735 ztest_dataset_close(0);
5736 spa_close(spa, FTAG);
5741 print_time(hrtime_t t, char *timebuf)
5743 hrtime_t s = t / NANOSEC;
5744 hrtime_t m = s / 60;
5745 hrtime_t h = m / 60;
5746 hrtime_t d = h / 24;
5755 (void) sprintf(timebuf,
5756 "%llud%02lluh%02llum%02llus", d, h, m, s);
5758 (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
5760 (void) sprintf(timebuf, "%llum%02llus", m, s);
5762 (void) sprintf(timebuf, "%llus", s);
5766 make_random_props(void)
5770 if (ztest_random(2) == 0)
5773 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
5774 VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
5780 * Create a storage pool with the given name and initial vdev size.
5781 * Then test spa_freeze() functionality.
5784 ztest_init(ztest_shared_t *zs)
5787 nvlist_t *nvroot, *props;
5789 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
5790 rw_init(&ztest_name_lock, NULL, RW_DEFAULT, NULL);
5792 kernel_init(FREAD | FWRITE);
5795 * Create the storage pool.
5797 (void) spa_destroy(ztest_opts.zo_pool);
5798 ztest_shared->zs_vdev_next_leaf = 0;
5800 zs->zs_mirrors = ztest_opts.zo_mirrors;
5801 nvroot = make_vdev_root(NULL, NULL, ztest_opts.zo_vdev_size, 0,
5802 0, ztest_opts.zo_raidz, zs->zs_mirrors, 1);
5803 props = make_random_props();
5804 VERIFY3U(0, ==, spa_create(ztest_opts.zo_pool, nvroot, props,
5806 nvlist_free(nvroot);
5808 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
5809 zs->zs_metaslab_sz =
5810 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
5811 spa_close(spa, FTAG);
5815 ztest_run_zdb(ztest_opts.zo_pool);
5819 ztest_run_zdb(ztest_opts.zo_pool);
5821 rw_destroy(&ztest_name_lock);
5822 mutex_destroy(&ztest_vdev_lock);
5828 static char ztest_name_data[] = "/tmp/ztest.data.XXXXXX";
5830 ztest_fd_data = mkstemp(ztest_name_data);
5831 ASSERT3S(ztest_fd_data, >=, 0);
5832 (void) unlink(ztest_name_data);
5836 shared_data_size(ztest_shared_hdr_t *hdr)
5840 size = hdr->zh_hdr_size;
5841 size += hdr->zh_opts_size;
5842 size += hdr->zh_size;
5843 size += hdr->zh_stats_size * hdr->zh_stats_count;
5844 size += hdr->zh_ds_size * hdr->zh_ds_count;
5853 ztest_shared_hdr_t *hdr;
5855 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
5856 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
5857 ASSERT(hdr != MAP_FAILED);
5859 VERIFY3U(0, ==, ftruncate(ztest_fd_data, sizeof (ztest_shared_hdr_t)));
5861 hdr->zh_hdr_size = sizeof (ztest_shared_hdr_t);
5862 hdr->zh_opts_size = sizeof (ztest_shared_opts_t);
5863 hdr->zh_size = sizeof (ztest_shared_t);
5864 hdr->zh_stats_size = sizeof (ztest_shared_callstate_t);
5865 hdr->zh_stats_count = ZTEST_FUNCS;
5866 hdr->zh_ds_size = sizeof (ztest_shared_ds_t);
5867 hdr->zh_ds_count = ztest_opts.zo_datasets;
5869 size = shared_data_size(hdr);
5870 VERIFY3U(0, ==, ftruncate(ztest_fd_data, size));
5872 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
5879 ztest_shared_hdr_t *hdr;
5882 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
5883 PROT_READ, MAP_SHARED, ztest_fd_data, 0);
5884 ASSERT(hdr != MAP_FAILED);
5886 size = shared_data_size(hdr);
5888 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
5889 hdr = ztest_shared_hdr = (void *)mmap(0, P2ROUNDUP(size, getpagesize()),
5890 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
5891 ASSERT(hdr != MAP_FAILED);
5892 buf = (uint8_t *)hdr;
5894 offset = hdr->zh_hdr_size;
5895 ztest_shared_opts = (void *)&buf[offset];
5896 offset += hdr->zh_opts_size;
5897 ztest_shared = (void *)&buf[offset];
5898 offset += hdr->zh_size;
5899 ztest_shared_callstate = (void *)&buf[offset];
5900 offset += hdr->zh_stats_size * hdr->zh_stats_count;
5901 ztest_shared_ds = (void *)&buf[offset];
5905 exec_child(char *cmd, char *libpath, boolean_t ignorekill, int *statusp)
5909 char *cmdbuf = NULL;
5914 cmdbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
5915 (void) strlcpy(cmdbuf, getexecname(), MAXPATHLEN);
5920 fatal(1, "fork failed");
5922 if (pid == 0) { /* child */
5923 char *emptyargv[2] = { cmd, NULL };
5924 char fd_data_str[12];
5926 struct rlimit rl = { 1024, 1024 };
5927 (void) setrlimit(RLIMIT_NOFILE, &rl);
5929 (void) close(ztest_fd_rand);
5930 VERIFY(11 >= snprintf(fd_data_str, 12, "%d", ztest_fd_data));
5931 VERIFY(0 == setenv("ZTEST_FD_DATA", fd_data_str, 1));
5933 (void) enable_extended_FILE_stdio(-1, -1);
5934 if (libpath != NULL)
5935 VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath, 1));
5936 (void) execv(cmd, emptyargv);
5937 ztest_dump_core = B_FALSE;
5938 fatal(B_TRUE, "exec failed: %s", cmd);
5941 if (cmdbuf != NULL) {
5942 umem_free(cmdbuf, MAXPATHLEN);
5946 while (waitpid(pid, &status, 0) != pid)
5948 if (statusp != NULL)
5951 if (WIFEXITED(status)) {
5952 if (WEXITSTATUS(status) != 0) {
5953 (void) fprintf(stderr, "child exited with code %d\n",
5954 WEXITSTATUS(status));
5958 } else if (WIFSIGNALED(status)) {
5959 if (!ignorekill || WTERMSIG(status) != SIGKILL) {
5960 (void) fprintf(stderr, "child died with signal %d\n",
5966 (void) fprintf(stderr, "something strange happened to child\n");
5973 ztest_run_init(void)
5977 ztest_shared_t *zs = ztest_shared;
5979 ASSERT(ztest_opts.zo_init != 0);
5982 * Blow away any existing copy of zpool.cache
5984 (void) remove(spa_config_path);
5987 * Create and initialize our storage pool.
5989 for (i = 1; i <= ztest_opts.zo_init; i++) {
5990 bzero(zs, sizeof (ztest_shared_t));
5991 if (ztest_opts.zo_verbose >= 3 &&
5992 ztest_opts.zo_init != 1) {
5993 (void) printf("ztest_init(), pass %d\n", i);
6000 main(int argc, char **argv)
6008 ztest_shared_callstate_t *zc;
6015 char *fd_data_str = getenv("ZTEST_FD_DATA");
6017 (void) setvbuf(stdout, NULL, _IOLBF, 0);
6019 ztest_fd_rand = open("/dev/urandom", O_RDONLY);
6020 ASSERT3S(ztest_fd_rand, >=, 0);
6023 dprintf_setup(&argc, argv);
6024 process_options(argc, argv);
6029 bcopy(&ztest_opts, ztest_shared_opts,
6030 sizeof (*ztest_shared_opts));
6032 ztest_fd_data = atoi(fd_data_str);
6034 bcopy(ztest_shared_opts, &ztest_opts, sizeof (ztest_opts));
6036 ASSERT3U(ztest_opts.zo_datasets, ==, ztest_shared_hdr->zh_ds_count);
6038 /* Override location of zpool.cache */
6039 VERIFY(asprintf((char **)&spa_config_path, "%s/zpool.cache",
6040 ztest_opts.zo_dir) != -1);
6042 ztest_ds = umem_alloc(ztest_opts.zo_datasets * sizeof (ztest_ds_t),
6047 metaslab_gang_bang = ztest_opts.zo_metaslab_gang_bang;
6048 metaslab_df_alloc_threshold =
6049 zs->zs_metaslab_df_alloc_threshold;
6058 hasalt = (strlen(ztest_opts.zo_alt_ztest) != 0);
6060 if (ztest_opts.zo_verbose >= 1) {
6061 (void) printf("%llu vdevs, %d datasets, %d threads,"
6062 " %llu seconds...\n",
6063 (u_longlong_t)ztest_opts.zo_vdevs,
6064 ztest_opts.zo_datasets,
6065 ztest_opts.zo_threads,
6066 (u_longlong_t)ztest_opts.zo_time);
6069 cmd = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
6070 (void) strlcpy(cmd, getexecname(), MAXNAMELEN);
6072 zs->zs_do_init = B_TRUE;
6073 if (strlen(ztest_opts.zo_alt_ztest) != 0) {
6074 if (ztest_opts.zo_verbose >= 1) {
6075 (void) printf("Executing older ztest for "
6076 "initialization: %s\n", ztest_opts.zo_alt_ztest);
6078 VERIFY(!exec_child(ztest_opts.zo_alt_ztest,
6079 ztest_opts.zo_alt_libpath, B_FALSE, NULL));
6081 VERIFY(!exec_child(NULL, NULL, B_FALSE, NULL));
6083 zs->zs_do_init = B_FALSE;
6085 zs->zs_proc_start = gethrtime();
6086 zs->zs_proc_stop = zs->zs_proc_start + ztest_opts.zo_time * NANOSEC;
6088 for (f = 0; f < ZTEST_FUNCS; f++) {
6089 zi = &ztest_info[f];
6090 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6091 if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
6092 zc->zc_next = UINT64_MAX;
6094 zc->zc_next = zs->zs_proc_start +
6095 ztest_random(2 * zi->zi_interval[0] + 1);
6099 * Run the tests in a loop. These tests include fault injection
6100 * to verify that self-healing data works, and forced crashes
6101 * to verify that we never lose on-disk consistency.
6103 while (gethrtime() < zs->zs_proc_stop) {
6108 * Initialize the workload counters for each function.
6110 for (f = 0; f < ZTEST_FUNCS; f++) {
6111 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6116 /* Set the allocation switch size */
6117 zs->zs_metaslab_df_alloc_threshold =
6118 ztest_random(zs->zs_metaslab_sz / 4) + 1;
6120 if (!hasalt || ztest_random(2) == 0) {
6121 if (hasalt && ztest_opts.zo_verbose >= 1) {
6122 (void) printf("Executing newer ztest: %s\n",
6126 killed = exec_child(cmd, NULL, B_TRUE, &status);
6128 if (hasalt && ztest_opts.zo_verbose >= 1) {
6129 (void) printf("Executing older ztest: %s\n",
6130 ztest_opts.zo_alt_ztest);
6133 killed = exec_child(ztest_opts.zo_alt_ztest,
6134 ztest_opts.zo_alt_libpath, B_TRUE, &status);
6141 if (ztest_opts.zo_verbose >= 1) {
6142 hrtime_t now = gethrtime();
6144 now = MIN(now, zs->zs_proc_stop);
6145 print_time(zs->zs_proc_stop - now, timebuf);
6146 nicenum(zs->zs_space, numbuf);
6148 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
6149 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
6151 WIFEXITED(status) ? "Complete" : "SIGKILL",
6152 (u_longlong_t)zs->zs_enospc_count,
6153 100.0 * zs->zs_alloc / zs->zs_space,
6155 100.0 * (now - zs->zs_proc_start) /
6156 (ztest_opts.zo_time * NANOSEC), timebuf);
6159 if (ztest_opts.zo_verbose >= 2) {
6160 (void) printf("\nWorkload summary:\n\n");
6161 (void) printf("%7s %9s %s\n",
6162 "Calls", "Time", "Function");
6163 (void) printf("%7s %9s %s\n",
6164 "-----", "----", "--------");
6165 for (f = 0; f < ZTEST_FUNCS; f++) {
6168 zi = &ztest_info[f];
6169 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6170 print_time(zc->zc_time, timebuf);
6171 (void) dladdr((void *)zi->zi_func, &dli);
6172 (void) printf("%7llu %9s %s\n",
6173 (u_longlong_t)zc->zc_count, timebuf,
6176 (void) printf("\n");
6180 * It's possible that we killed a child during a rename test,
6181 * in which case we'll have a 'ztest_tmp' pool lying around
6182 * instead of 'ztest'. Do a blind rename in case this happened.
6185 if (spa_open(ztest_opts.zo_pool, &spa, FTAG) == 0) {
6186 spa_close(spa, FTAG);
6188 char tmpname[MAXNAMELEN];
6190 kernel_init(FREAD | FWRITE);
6191 (void) snprintf(tmpname, sizeof (tmpname), "%s_tmp",
6192 ztest_opts.zo_pool);
6193 (void) spa_rename(tmpname, ztest_opts.zo_pool);
6197 ztest_run_zdb(ztest_opts.zo_pool);
6200 if (ztest_opts.zo_verbose >= 1) {
6202 (void) printf("%d runs of older ztest: %s\n", older,
6203 ztest_opts.zo_alt_ztest);
6204 (void) printf("%d runs of newer ztest: %s\n", newer,
6207 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
6208 kills, iters - kills, (100.0 * kills) / MAX(1, iters));
6211 umem_free(cmd, MAXNAMELEN);