4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2013 Steven Hartland. All rights reserved.
26 * Copyright (c) 2014 Integros [integros.com]
30 * The objective of this program is to provide a DMU/ZAP/SPA stress test
31 * that runs entirely in userland, is easy to use, and easy to extend.
33 * The overall design of the ztest program is as follows:
35 * (1) For each major functional area (e.g. adding vdevs to a pool,
36 * creating and destroying datasets, reading and writing objects, etc)
37 * we have a simple routine to test that functionality. These
38 * individual routines do not have to do anything "stressful".
40 * (2) We turn these simple functionality tests into a stress test by
41 * running them all in parallel, with as many threads as desired,
42 * and spread across as many datasets, objects, and vdevs as desired.
44 * (3) While all this is happening, we inject faults into the pool to
45 * verify that self-healing data really works.
47 * (4) Every time we open a dataset, we change its checksum and compression
48 * functions. Thus even individual objects vary from block to block
49 * in which checksum they use and whether they're compressed.
51 * (5) To verify that we never lose on-disk consistency after a crash,
52 * we run the entire test in a child of the main process.
53 * At random times, the child self-immolates with a SIGKILL.
54 * This is the software equivalent of pulling the power cord.
55 * The parent then runs the test again, using the existing
56 * storage pool, as many times as desired. If backwards compatibility
57 * testing is enabled ztest will sometimes run the "older" version
58 * of ztest after a SIGKILL.
60 * (6) To verify that we don't have future leaks or temporal incursions,
61 * many of the functional tests record the transaction group number
62 * as part of their data. When reading old data, they verify that
63 * the transaction group number is less than the current, open txg.
64 * If you add a new test, please do this if applicable.
66 * When run with no arguments, ztest runs for about five minutes and
67 * produces no output if successful. To get a little bit of information,
68 * specify -V. To get more information, specify -VV, and so on.
70 * To turn this into an overnight stress test, use -T to specify run time.
72 * You can ask more more vdevs [-v], datasets [-d], or threads [-t]
73 * to increase the pool capacity, fanout, and overall stress level.
75 * Use the -k option to set the desired frequency of kills.
77 * When ztest invokes itself it passes all relevant information through a
78 * temporary file which is mmap-ed in the child process. This allows shared
79 * memory to survive the exec syscall. The ztest_shared_hdr_t struct is always
80 * stored at offset 0 of this file and contains information on the size and
81 * number of shared structures in the file. The information stored in this file
82 * must remain backwards compatible with older versions of ztest so that
83 * ztest can invoke them during backwards compatibility testing (-B).
86 #include <sys/zfs_context.h>
92 #include <sys/dmu_objset.h>
98 #include <sys/resource.h>
101 #include <sys/zil_impl.h>
102 #include <sys/vdev_impl.h>
103 #include <sys/vdev_file.h>
104 #include <sys/spa_impl.h>
105 #include <sys/metaslab_impl.h>
106 #include <sys/dsl_prop.h>
107 #include <sys/dsl_dataset.h>
108 #include <sys/dsl_destroy.h>
109 #include <sys/dsl_scan.h>
110 #include <sys/zio_checksum.h>
111 #include <sys/refcount.h>
112 #include <sys/zfeature.h>
113 #include <sys/dsl_userhold.h>
115 #include <stdio_ext.h>
123 #include <sys/fs/zfs.h>
124 #include <libnvpair.h>
126 static int ztest_fd_data
= -1;
127 static int ztest_fd_rand
= -1;
129 typedef struct ztest_shared_hdr
{
130 uint64_t zh_hdr_size
;
131 uint64_t zh_opts_size
;
133 uint64_t zh_stats_size
;
134 uint64_t zh_stats_count
;
136 uint64_t zh_ds_count
;
137 } ztest_shared_hdr_t
;
139 static ztest_shared_hdr_t
*ztest_shared_hdr
;
141 typedef struct ztest_shared_opts
{
142 char zo_pool
[ZFS_MAX_DATASET_NAME_LEN
];
143 char zo_dir
[ZFS_MAX_DATASET_NAME_LEN
];
144 char zo_alt_ztest
[MAXNAMELEN
];
145 char zo_alt_libpath
[MAXNAMELEN
];
147 uint64_t zo_vdevtime
;
155 uint64_t zo_passtime
;
156 uint64_t zo_killrate
;
160 uint64_t zo_maxloops
;
161 uint64_t zo_metaslab_gang_bang
;
162 } ztest_shared_opts_t
;
164 static const ztest_shared_opts_t ztest_opts_defaults
= {
165 .zo_pool
= { 'z', 't', 'e', 's', 't', '\0' },
166 .zo_dir
= { '/', 't', 'm', 'p', '\0' },
167 .zo_alt_ztest
= { '\0' },
168 .zo_alt_libpath
= { '\0' },
170 .zo_ashift
= SPA_MINBLOCKSHIFT
,
173 .zo_raidz_parity
= 1,
174 .zo_vdev_size
= SPA_MINDEVSIZE
* 2,
177 .zo_passtime
= 60, /* 60 seconds */
178 .zo_killrate
= 70, /* 70% kill rate */
181 .zo_time
= 300, /* 5 minutes */
182 .zo_maxloops
= 50, /* max loops during spa_freeze() */
183 .zo_metaslab_gang_bang
= 32 << 10
186 extern uint64_t metaslab_gang_bang
;
187 extern uint64_t metaslab_df_alloc_threshold
;
188 extern uint64_t zfs_deadman_synctime_ms
;
189 extern int metaslab_preload_limit
;
190 extern boolean_t zfs_compressed_arc_enabled
;
192 static ztest_shared_opts_t
*ztest_shared_opts
;
193 static ztest_shared_opts_t ztest_opts
;
195 typedef struct ztest_shared_ds
{
199 static ztest_shared_ds_t
*ztest_shared_ds
;
200 #define ZTEST_GET_SHARED_DS(d) (&ztest_shared_ds[d])
202 #define BT_MAGIC 0x123456789abcdefULL
203 #define MAXFAULTS() \
204 (MAX(zs->zs_mirrors, 1) * (ztest_opts.zo_raidz_parity + 1) - 1)
208 ZTEST_IO_WRITE_PATTERN
,
209 ZTEST_IO_WRITE_ZEROES
,
216 typedef struct ztest_block_tag
{
226 typedef struct bufwad
{
233 * XXX -- fix zfs range locks to be generic so we can use them here.
255 #define ZTEST_RANGE_LOCKS 64
256 #define ZTEST_OBJECT_LOCKS 64
259 * Object descriptor. Used as a template for object lookup/create/remove.
261 typedef struct ztest_od
{
264 dmu_object_type_t od_type
;
265 dmu_object_type_t od_crtype
;
266 uint64_t od_blocksize
;
267 uint64_t od_crblocksize
;
270 char od_name
[ZFS_MAX_DATASET_NAME_LEN
];
276 typedef struct ztest_ds
{
277 ztest_shared_ds_t
*zd_shared
;
279 rwlock_t zd_zilog_lock
;
281 ztest_od_t
*zd_od
; /* debugging aid */
282 char zd_name
[ZFS_MAX_DATASET_NAME_LEN
];
283 mutex_t zd_dirobj_lock
;
284 rll_t zd_object_lock
[ZTEST_OBJECT_LOCKS
];
285 rll_t zd_range_lock
[ZTEST_RANGE_LOCKS
];
289 * Per-iteration state.
291 typedef void ztest_func_t(ztest_ds_t
*zd
, uint64_t id
);
293 typedef struct ztest_info
{
294 ztest_func_t
*zi_func
; /* test function */
295 uint64_t zi_iters
; /* iterations per execution */
296 uint64_t *zi_interval
; /* execute every <interval> seconds */
299 typedef struct ztest_shared_callstate
{
300 uint64_t zc_count
; /* per-pass count */
301 uint64_t zc_time
; /* per-pass time */
302 uint64_t zc_next
; /* next time to call this function */
303 } ztest_shared_callstate_t
;
305 static ztest_shared_callstate_t
*ztest_shared_callstate
;
306 #define ZTEST_GET_SHARED_CALLSTATE(c) (&ztest_shared_callstate[c])
309 * Note: these aren't static because we want dladdr() to work.
311 ztest_func_t ztest_dmu_read_write
;
312 ztest_func_t ztest_dmu_write_parallel
;
313 ztest_func_t ztest_dmu_object_alloc_free
;
314 ztest_func_t ztest_dmu_commit_callbacks
;
315 ztest_func_t ztest_zap
;
316 ztest_func_t ztest_zap_parallel
;
317 ztest_func_t ztest_zil_commit
;
318 ztest_func_t ztest_zil_remount
;
319 ztest_func_t ztest_dmu_read_write_zcopy
;
320 ztest_func_t ztest_dmu_objset_create_destroy
;
321 ztest_func_t ztest_dmu_prealloc
;
322 ztest_func_t ztest_fzap
;
323 ztest_func_t ztest_dmu_snapshot_create_destroy
;
324 ztest_func_t ztest_dsl_prop_get_set
;
325 ztest_func_t ztest_spa_prop_get_set
;
326 ztest_func_t ztest_spa_create_destroy
;
327 ztest_func_t ztest_fault_inject
;
328 ztest_func_t ztest_ddt_repair
;
329 ztest_func_t ztest_dmu_snapshot_hold
;
330 ztest_func_t ztest_spa_rename
;
331 ztest_func_t ztest_scrub
;
332 ztest_func_t ztest_dsl_dataset_promote_busy
;
333 ztest_func_t ztest_vdev_attach_detach
;
334 ztest_func_t ztest_vdev_LUN_growth
;
335 ztest_func_t ztest_vdev_add_remove
;
336 ztest_func_t ztest_vdev_aux_add_remove
;
337 ztest_func_t ztest_split_pool
;
338 ztest_func_t ztest_reguid
;
339 ztest_func_t ztest_spa_upgrade
;
341 uint64_t zopt_always
= 0ULL * NANOSEC
; /* all the time */
342 uint64_t zopt_incessant
= 1ULL * NANOSEC
/ 10; /* every 1/10 second */
343 uint64_t zopt_often
= 1ULL * NANOSEC
; /* every second */
344 uint64_t zopt_sometimes
= 10ULL * NANOSEC
; /* every 10 seconds */
345 uint64_t zopt_rarely
= 60ULL * NANOSEC
; /* every 60 seconds */
347 ztest_info_t ztest_info
[] = {
348 { ztest_dmu_read_write
, 1, &zopt_always
},
349 { ztest_dmu_write_parallel
, 10, &zopt_always
},
350 { ztest_dmu_object_alloc_free
, 1, &zopt_always
},
351 { ztest_dmu_commit_callbacks
, 1, &zopt_always
},
352 { ztest_zap
, 30, &zopt_always
},
353 { ztest_zap_parallel
, 100, &zopt_always
},
354 { ztest_split_pool
, 1, &zopt_always
},
355 { ztest_zil_commit
, 1, &zopt_incessant
},
356 { ztest_zil_remount
, 1, &zopt_sometimes
},
357 { ztest_dmu_read_write_zcopy
, 1, &zopt_often
},
358 { ztest_dmu_objset_create_destroy
, 1, &zopt_often
},
359 { ztest_dsl_prop_get_set
, 1, &zopt_often
},
360 { ztest_spa_prop_get_set
, 1, &zopt_sometimes
},
362 { ztest_dmu_prealloc
, 1, &zopt_sometimes
},
364 { ztest_fzap
, 1, &zopt_sometimes
},
365 { ztest_dmu_snapshot_create_destroy
, 1, &zopt_sometimes
},
366 { ztest_spa_create_destroy
, 1, &zopt_sometimes
},
367 { ztest_fault_inject
, 1, &zopt_sometimes
},
368 { ztest_ddt_repair
, 1, &zopt_sometimes
},
369 { ztest_dmu_snapshot_hold
, 1, &zopt_sometimes
},
370 { ztest_reguid
, 1, &zopt_rarely
},
371 { ztest_spa_rename
, 1, &zopt_rarely
},
372 { ztest_scrub
, 1, &zopt_rarely
},
373 { ztest_spa_upgrade
, 1, &zopt_rarely
},
374 { ztest_dsl_dataset_promote_busy
, 1, &zopt_rarely
},
375 { ztest_vdev_attach_detach
, 1, &zopt_sometimes
},
376 { ztest_vdev_LUN_growth
, 1, &zopt_rarely
},
377 { ztest_vdev_add_remove
, 1,
378 &ztest_opts
.zo_vdevtime
},
379 { ztest_vdev_aux_add_remove
, 1,
380 &ztest_opts
.zo_vdevtime
},
383 #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
386 * The following struct is used to hold a list of uncalled commit callbacks.
387 * The callbacks are ordered by txg number.
389 typedef struct ztest_cb_list
{
390 mutex_t zcl_callbacks_lock
;
391 list_t zcl_callbacks
;
395 * Stuff we need to share writably between parent and child.
397 typedef struct ztest_shared
{
398 boolean_t zs_do_init
;
399 hrtime_t zs_proc_start
;
400 hrtime_t zs_proc_stop
;
401 hrtime_t zs_thread_start
;
402 hrtime_t zs_thread_stop
;
403 hrtime_t zs_thread_kill
;
404 uint64_t zs_enospc_count
;
405 uint64_t zs_vdev_next_leaf
;
406 uint64_t zs_vdev_aux
;
411 uint64_t zs_metaslab_sz
;
412 uint64_t zs_metaslab_df_alloc_threshold
;
416 #define ID_PARALLEL -1ULL
418 static char ztest_dev_template
[] = "%s/%s.%llua";
419 static char ztest_aux_template
[] = "%s/%s.%s.%llu";
420 ztest_shared_t
*ztest_shared
;
422 static spa_t
*ztest_spa
= NULL
;
423 static ztest_ds_t
*ztest_ds
;
425 static mutex_t ztest_vdev_lock
;
428 * The ztest_name_lock protects the pool and dataset namespace used by
429 * the individual tests. To modify the namespace, consumers must grab
430 * this lock as writer. Grabbing the lock as reader will ensure that the
431 * namespace does not change while the lock is held.
433 static rwlock_t ztest_name_lock
;
435 static boolean_t ztest_dump_core
= B_TRUE
;
436 static boolean_t ztest_exiting
;
438 /* Global commit callback list */
439 static ztest_cb_list_t zcl
;
442 ZTEST_META_DNODE
= 0,
447 static void usage(boolean_t
) __NORETURN
;
450 * These libumem hooks provide a reasonable set of defaults for the allocator's
451 * debugging facilities.
456 return ("default,verbose"); /* $UMEM_DEBUG setting */
460 _umem_logging_init(void)
462 return ("fail,contents"); /* $UMEM_LOGGING setting */
465 #define FATAL_MSG_SZ 1024
470 fatal(int do_perror
, char *message
, ...)
473 int save_errno
= errno
;
474 char buf
[FATAL_MSG_SZ
];
476 (void) fflush(stdout
);
478 va_start(args
, message
);
479 (void) sprintf(buf
, "ztest: ");
481 (void) vsprintf(buf
+ strlen(buf
), message
, args
);
484 (void) snprintf(buf
+ strlen(buf
), FATAL_MSG_SZ
- strlen(buf
),
485 ": %s", strerror(save_errno
));
487 (void) fprintf(stderr
, "%s\n", buf
);
488 fatal_msg
= buf
; /* to ease debugging */
495 str2shift(const char *buf
)
497 const char *ends
= "BKMGTPEZ";
502 for (i
= 0; i
< strlen(ends
); i
++) {
503 if (toupper(buf
[0]) == ends
[i
])
506 if (i
== strlen(ends
)) {
507 (void) fprintf(stderr
, "ztest: invalid bytes suffix: %s\n",
511 if (buf
[1] == '\0' || (toupper(buf
[1]) == 'B' && buf
[2] == '\0')) {
514 (void) fprintf(stderr
, "ztest: invalid bytes suffix: %s\n", buf
);
520 nicenumtoull(const char *buf
)
525 val
= strtoull(buf
, &end
, 0);
527 (void) fprintf(stderr
, "ztest: bad numeric value: %s\n", buf
);
529 } else if (end
[0] == '.') {
530 double fval
= strtod(buf
, &end
);
531 fval
*= pow(2, str2shift(end
));
532 if (fval
> UINT64_MAX
) {
533 (void) fprintf(stderr
, "ztest: value too large: %s\n",
537 val
= (uint64_t)fval
;
539 int shift
= str2shift(end
);
540 if (shift
>= 64 || (val
<< shift
) >> shift
!= val
) {
541 (void) fprintf(stderr
, "ztest: value too large: %s\n",
551 usage(boolean_t requested
)
553 const ztest_shared_opts_t
*zo
= &ztest_opts_defaults
;
555 char nice_vdev_size
[10];
556 char nice_gang_bang
[10];
557 FILE *fp
= requested
? stdout
: stderr
;
559 nicenum(zo
->zo_vdev_size
, nice_vdev_size
);
560 nicenum(zo
->zo_metaslab_gang_bang
, nice_gang_bang
);
562 (void) fprintf(fp
, "Usage: %s\n"
563 "\t[-v vdevs (default: %llu)]\n"
564 "\t[-s size_of_each_vdev (default: %s)]\n"
565 "\t[-a alignment_shift (default: %d)] use 0 for random\n"
566 "\t[-m mirror_copies (default: %d)]\n"
567 "\t[-r raidz_disks (default: %d)]\n"
568 "\t[-R raidz_parity (default: %d)]\n"
569 "\t[-d datasets (default: %d)]\n"
570 "\t[-t threads (default: %d)]\n"
571 "\t[-g gang_block_threshold (default: %s)]\n"
572 "\t[-i init_count (default: %d)] initialize pool i times\n"
573 "\t[-k kill_percentage (default: %llu%%)]\n"
574 "\t[-p pool_name (default: %s)]\n"
575 "\t[-f dir (default: %s)] file directory for vdev files\n"
576 "\t[-V] verbose (use multiple times for ever more blather)\n"
577 "\t[-E] use existing pool instead of creating new one\n"
578 "\t[-T time (default: %llu sec)] total run time\n"
579 "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
580 "\t[-P passtime (default: %llu sec)] time per pass\n"
581 "\t[-B alt_ztest (default: <none>)] alternate ztest path\n"
582 "\t[-o variable=value] ... set global variable to an unsigned\n"
583 "\t 32-bit integer value\n"
584 "\t[-h] (print help)\n"
587 (u_longlong_t
)zo
->zo_vdevs
, /* -v */
588 nice_vdev_size
, /* -s */
589 zo
->zo_ashift
, /* -a */
590 zo
->zo_mirrors
, /* -m */
591 zo
->zo_raidz
, /* -r */
592 zo
->zo_raidz_parity
, /* -R */
593 zo
->zo_datasets
, /* -d */
594 zo
->zo_threads
, /* -t */
595 nice_gang_bang
, /* -g */
596 zo
->zo_init
, /* -i */
597 (u_longlong_t
)zo
->zo_killrate
, /* -k */
598 zo
->zo_pool
, /* -p */
600 (u_longlong_t
)zo
->zo_time
, /* -T */
601 (u_longlong_t
)zo
->zo_maxloops
, /* -F */
602 (u_longlong_t
)zo
->zo_passtime
);
603 exit(requested
? 0 : 1);
607 process_options(int argc
, char **argv
)
610 ztest_shared_opts_t
*zo
= &ztest_opts
;
614 char altdir
[MAXNAMELEN
] = { 0 };
616 bcopy(&ztest_opts_defaults
, zo
, sizeof (*zo
));
618 while ((opt
= getopt(argc
, argv
,
619 "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:hF:B:o:")) != EOF
) {
636 value
= nicenumtoull(optarg
);
640 zo
->zo_vdevs
= value
;
643 zo
->zo_vdev_size
= MAX(SPA_MINDEVSIZE
, value
);
646 zo
->zo_ashift
= value
;
649 zo
->zo_mirrors
= value
;
652 zo
->zo_raidz
= MAX(1, value
);
655 zo
->zo_raidz_parity
= MIN(MAX(value
, 1), 3);
658 zo
->zo_datasets
= MAX(1, value
);
661 zo
->zo_threads
= MAX(1, value
);
664 zo
->zo_metaslab_gang_bang
= MAX(SPA_MINBLOCKSIZE
<< 1,
671 zo
->zo_killrate
= value
;
674 (void) strlcpy(zo
->zo_pool
, optarg
,
675 sizeof (zo
->zo_pool
));
678 path
= realpath(optarg
, NULL
);
680 (void) fprintf(stderr
, "error: %s: %s\n",
681 optarg
, strerror(errno
));
684 (void) strlcpy(zo
->zo_dir
, path
,
685 sizeof (zo
->zo_dir
));
698 zo
->zo_passtime
= MAX(1, value
);
701 zo
->zo_maxloops
= MAX(1, value
);
704 (void) strlcpy(altdir
, optarg
, sizeof (altdir
));
707 if (set_global_var(optarg
) != 0)
720 zo
->zo_raidz_parity
= MIN(zo
->zo_raidz_parity
, zo
->zo_raidz
- 1);
723 (zo
->zo_vdevs
> 0 ? zo
->zo_time
* NANOSEC
/ zo
->zo_vdevs
:
726 if (strlen(altdir
) > 0) {
734 cmd
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
735 realaltdir
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
737 VERIFY(NULL
!= realpath(getexecname(), cmd
));
738 if (0 != access(altdir
, F_OK
)) {
739 ztest_dump_core
= B_FALSE
;
740 fatal(B_TRUE
, "invalid alternate ztest path: %s",
743 VERIFY(NULL
!= realpath(altdir
, realaltdir
));
746 * 'cmd' should be of the form "<anything>/usr/bin/<isa>/ztest".
747 * We want to extract <isa> to determine if we should use
748 * 32 or 64 bit binaries.
750 bin
= strstr(cmd
, "/usr/bin/");
751 ztest
= strstr(bin
, "/ztest");
753 isalen
= ztest
- isa
;
754 (void) snprintf(zo
->zo_alt_ztest
, sizeof (zo
->zo_alt_ztest
),
755 "%s/usr/bin/%.*s/ztest", realaltdir
, isalen
, isa
);
756 (void) snprintf(zo
->zo_alt_libpath
, sizeof (zo
->zo_alt_libpath
),
757 "%s/usr/lib/%.*s", realaltdir
, isalen
, isa
);
759 if (0 != access(zo
->zo_alt_ztest
, X_OK
)) {
760 ztest_dump_core
= B_FALSE
;
761 fatal(B_TRUE
, "invalid alternate ztest: %s",
763 } else if (0 != access(zo
->zo_alt_libpath
, X_OK
)) {
764 ztest_dump_core
= B_FALSE
;
765 fatal(B_TRUE
, "invalid alternate lib directory %s",
769 umem_free(cmd
, MAXPATHLEN
);
770 umem_free(realaltdir
, MAXPATHLEN
);
775 ztest_kill(ztest_shared_t
*zs
)
777 zs
->zs_alloc
= metaslab_class_get_alloc(spa_normal_class(ztest_spa
));
778 zs
->zs_space
= metaslab_class_get_space(spa_normal_class(ztest_spa
));
781 * Before we kill off ztest, make sure that the config is updated.
782 * See comment above spa_config_sync().
784 mutex_enter(&spa_namespace_lock
);
785 spa_config_sync(ztest_spa
, B_FALSE
, B_FALSE
);
786 mutex_exit(&spa_namespace_lock
);
788 zfs_dbgmsg_print(FTAG
);
789 (void) kill(getpid(), SIGKILL
);
793 ztest_random(uint64_t range
)
797 ASSERT3S(ztest_fd_rand
, >=, 0);
802 if (read(ztest_fd_rand
, &r
, sizeof (r
)) != sizeof (r
))
803 fatal(1, "short read from /dev/urandom");
810 ztest_record_enospc(const char *s
)
812 ztest_shared
->zs_enospc_count
++;
816 ztest_get_ashift(void)
818 if (ztest_opts
.zo_ashift
== 0)
819 return (SPA_MINBLOCKSHIFT
+ ztest_random(5));
820 return (ztest_opts
.zo_ashift
);
824 make_vdev_file(char *path
, char *aux
, char *pool
, size_t size
, uint64_t ashift
)
826 char pathbuf
[MAXPATHLEN
];
831 ashift
= ztest_get_ashift();
837 vdev
= ztest_shared
->zs_vdev_aux
;
838 (void) snprintf(path
, sizeof (pathbuf
),
839 ztest_aux_template
, ztest_opts
.zo_dir
,
840 pool
== NULL
? ztest_opts
.zo_pool
: pool
,
843 vdev
= ztest_shared
->zs_vdev_next_leaf
++;
844 (void) snprintf(path
, sizeof (pathbuf
),
845 ztest_dev_template
, ztest_opts
.zo_dir
,
846 pool
== NULL
? ztest_opts
.zo_pool
: pool
, vdev
);
851 int fd
= open(path
, O_RDWR
| O_CREAT
| O_TRUNC
, 0666);
853 fatal(1, "can't open %s", path
);
854 if (ftruncate(fd
, size
) != 0)
855 fatal(1, "can't ftruncate %s", path
);
859 VERIFY(nvlist_alloc(&file
, NV_UNIQUE_NAME
, 0) == 0);
860 VERIFY(nvlist_add_string(file
, ZPOOL_CONFIG_TYPE
, VDEV_TYPE_FILE
) == 0);
861 VERIFY(nvlist_add_string(file
, ZPOOL_CONFIG_PATH
, path
) == 0);
862 VERIFY(nvlist_add_uint64(file
, ZPOOL_CONFIG_ASHIFT
, ashift
) == 0);
868 make_vdev_raidz(char *path
, char *aux
, char *pool
, size_t size
,
869 uint64_t ashift
, int r
)
871 nvlist_t
*raidz
, **child
;
875 return (make_vdev_file(path
, aux
, pool
, size
, ashift
));
876 child
= umem_alloc(r
* sizeof (nvlist_t
*), UMEM_NOFAIL
);
878 for (c
= 0; c
< r
; c
++)
879 child
[c
] = make_vdev_file(path
, aux
, pool
, size
, ashift
);
881 VERIFY(nvlist_alloc(&raidz
, NV_UNIQUE_NAME
, 0) == 0);
882 VERIFY(nvlist_add_string(raidz
, ZPOOL_CONFIG_TYPE
,
883 VDEV_TYPE_RAIDZ
) == 0);
884 VERIFY(nvlist_add_uint64(raidz
, ZPOOL_CONFIG_NPARITY
,
885 ztest_opts
.zo_raidz_parity
) == 0);
886 VERIFY(nvlist_add_nvlist_array(raidz
, ZPOOL_CONFIG_CHILDREN
,
889 for (c
= 0; c
< r
; c
++)
890 nvlist_free(child
[c
]);
892 umem_free(child
, r
* sizeof (nvlist_t
*));
898 make_vdev_mirror(char *path
, char *aux
, char *pool
, size_t size
,
899 uint64_t ashift
, int r
, int m
)
901 nvlist_t
*mirror
, **child
;
905 return (make_vdev_raidz(path
, aux
, pool
, size
, ashift
, r
));
907 child
= umem_alloc(m
* sizeof (nvlist_t
*), UMEM_NOFAIL
);
909 for (c
= 0; c
< m
; c
++)
910 child
[c
] = make_vdev_raidz(path
, aux
, pool
, size
, ashift
, r
);
912 VERIFY(nvlist_alloc(&mirror
, NV_UNIQUE_NAME
, 0) == 0);
913 VERIFY(nvlist_add_string(mirror
, ZPOOL_CONFIG_TYPE
,
914 VDEV_TYPE_MIRROR
) == 0);
915 VERIFY(nvlist_add_nvlist_array(mirror
, ZPOOL_CONFIG_CHILDREN
,
918 for (c
= 0; c
< m
; c
++)
919 nvlist_free(child
[c
]);
921 umem_free(child
, m
* sizeof (nvlist_t
*));
927 make_vdev_root(char *path
, char *aux
, char *pool
, size_t size
, uint64_t ashift
,
928 int log
, int r
, int m
, int t
)
930 nvlist_t
*root
, **child
;
935 child
= umem_alloc(t
* sizeof (nvlist_t
*), UMEM_NOFAIL
);
937 for (c
= 0; c
< t
; c
++) {
938 child
[c
] = make_vdev_mirror(path
, aux
, pool
, size
, ashift
,
940 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
944 VERIFY(nvlist_alloc(&root
, NV_UNIQUE_NAME
, 0) == 0);
945 VERIFY(nvlist_add_string(root
, ZPOOL_CONFIG_TYPE
, VDEV_TYPE_ROOT
) == 0);
946 VERIFY(nvlist_add_nvlist_array(root
, aux
? aux
: ZPOOL_CONFIG_CHILDREN
,
949 for (c
= 0; c
< t
; c
++)
950 nvlist_free(child
[c
]);
952 umem_free(child
, t
* sizeof (nvlist_t
*));
958 * Find a random spa version. Returns back a random spa version in the
959 * range [initial_version, SPA_VERSION_FEATURES].
962 ztest_random_spa_version(uint64_t initial_version
)
964 uint64_t version
= initial_version
;
966 if (version
<= SPA_VERSION_BEFORE_FEATURES
) {
968 ztest_random(SPA_VERSION_BEFORE_FEATURES
- version
+ 1);
971 if (version
> SPA_VERSION_BEFORE_FEATURES
)
972 version
= SPA_VERSION_FEATURES
;
974 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
979 ztest_random_blocksize(void)
981 uint64_t block_shift
;
983 * Choose a block size >= the ashift.
984 * If the SPA supports new MAXBLOCKSIZE, test up to 1MB blocks.
986 int maxbs
= SPA_OLD_MAXBLOCKSHIFT
;
987 if (spa_maxblocksize(ztest_spa
) == SPA_MAXBLOCKSIZE
)
989 block_shift
= ztest_random(maxbs
- ztest_spa
->spa_max_ashift
+ 1);
990 return (1 << (SPA_MINBLOCKSHIFT
+ block_shift
));
994 ztest_random_ibshift(void)
996 return (DN_MIN_INDBLKSHIFT
+
997 ztest_random(DN_MAX_INDBLKSHIFT
- DN_MIN_INDBLKSHIFT
+ 1));
1001 ztest_random_vdev_top(spa_t
*spa
, boolean_t log_ok
)
1004 vdev_t
*rvd
= spa
->spa_root_vdev
;
1007 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
1010 top
= ztest_random(rvd
->vdev_children
);
1011 tvd
= rvd
->vdev_child
[top
];
1012 } while (tvd
->vdev_ishole
|| (tvd
->vdev_islog
&& !log_ok
) ||
1013 tvd
->vdev_mg
== NULL
|| tvd
->vdev_mg
->mg_class
== NULL
);
1019 ztest_random_dsl_prop(zfs_prop_t prop
)
1024 value
= zfs_prop_random_value(prop
, ztest_random(-1ULL));
1025 } while (prop
== ZFS_PROP_CHECKSUM
&& value
== ZIO_CHECKSUM_OFF
);
1031 ztest_dsl_prop_set_uint64(char *osname
, zfs_prop_t prop
, uint64_t value
,
1034 const char *propname
= zfs_prop_to_name(prop
);
1035 const char *valname
;
1036 char setpoint
[MAXPATHLEN
];
1040 error
= dsl_prop_set_int(osname
, propname
,
1041 (inherit
? ZPROP_SRC_NONE
: ZPROP_SRC_LOCAL
), value
);
1043 if (error
== ENOSPC
) {
1044 ztest_record_enospc(FTAG
);
1049 VERIFY0(dsl_prop_get_integer(osname
, propname
, &curval
, setpoint
));
1051 if (ztest_opts
.zo_verbose
>= 6) {
1052 VERIFY(zfs_prop_index_to_string(prop
, curval
, &valname
) == 0);
1053 (void) printf("%s %s = %s at '%s'\n",
1054 osname
, propname
, valname
, setpoint
);
1061 ztest_spa_prop_set_uint64(zpool_prop_t prop
, uint64_t value
)
1063 spa_t
*spa
= ztest_spa
;
1064 nvlist_t
*props
= NULL
;
1067 VERIFY(nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) == 0);
1068 VERIFY(nvlist_add_uint64(props
, zpool_prop_to_name(prop
), value
) == 0);
1070 error
= spa_prop_set(spa
, props
);
1074 if (error
== ENOSPC
) {
1075 ztest_record_enospc(FTAG
);
1084 ztest_rll_init(rll_t
*rll
)
1086 rll
->rll_writer
= NULL
;
1087 rll
->rll_readers
= 0;
1088 VERIFY(_mutex_init(&rll
->rll_lock
, USYNC_THREAD
, NULL
) == 0);
1089 VERIFY(cond_init(&rll
->rll_cv
, USYNC_THREAD
, NULL
) == 0);
1093 ztest_rll_destroy(rll_t
*rll
)
1095 ASSERT(rll
->rll_writer
== NULL
);
1096 ASSERT(rll
->rll_readers
== 0);
1097 VERIFY(_mutex_destroy(&rll
->rll_lock
) == 0);
1098 VERIFY(cond_destroy(&rll
->rll_cv
) == 0);
1102 ztest_rll_lock(rll_t
*rll
, rl_type_t type
)
1104 VERIFY(mutex_lock(&rll
->rll_lock
) == 0);
1106 if (type
== RL_READER
) {
1107 while (rll
->rll_writer
!= NULL
)
1108 (void) cond_wait(&rll
->rll_cv
, &rll
->rll_lock
);
1111 while (rll
->rll_writer
!= NULL
|| rll
->rll_readers
)
1112 (void) cond_wait(&rll
->rll_cv
, &rll
->rll_lock
);
1113 rll
->rll_writer
= curthread
;
1116 VERIFY(mutex_unlock(&rll
->rll_lock
) == 0);
1120 ztest_rll_unlock(rll_t
*rll
)
1122 VERIFY(mutex_lock(&rll
->rll_lock
) == 0);
1124 if (rll
->rll_writer
) {
1125 ASSERT(rll
->rll_readers
== 0);
1126 rll
->rll_writer
= NULL
;
1128 ASSERT(rll
->rll_readers
!= 0);
1129 ASSERT(rll
->rll_writer
== NULL
);
1133 if (rll
->rll_writer
== NULL
&& rll
->rll_readers
== 0)
1134 VERIFY(cond_broadcast(&rll
->rll_cv
) == 0);
1136 VERIFY(mutex_unlock(&rll
->rll_lock
) == 0);
1140 ztest_object_lock(ztest_ds_t
*zd
, uint64_t object
, rl_type_t type
)
1142 rll_t
*rll
= &zd
->zd_object_lock
[object
& (ZTEST_OBJECT_LOCKS
- 1)];
1144 ztest_rll_lock(rll
, type
);
1148 ztest_object_unlock(ztest_ds_t
*zd
, uint64_t object
)
1150 rll_t
*rll
= &zd
->zd_object_lock
[object
& (ZTEST_OBJECT_LOCKS
- 1)];
1152 ztest_rll_unlock(rll
);
1156 ztest_range_lock(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
,
1157 uint64_t size
, rl_type_t type
)
1159 uint64_t hash
= object
^ (offset
% (ZTEST_RANGE_LOCKS
+ 1));
1160 rll_t
*rll
= &zd
->zd_range_lock
[hash
& (ZTEST_RANGE_LOCKS
- 1)];
1163 rl
= umem_alloc(sizeof (*rl
), UMEM_NOFAIL
);
1164 rl
->rl_object
= object
;
1165 rl
->rl_offset
= offset
;
1169 ztest_rll_lock(rll
, type
);
1175 ztest_range_unlock(rl_t
*rl
)
1177 rll_t
*rll
= rl
->rl_lock
;
1179 ztest_rll_unlock(rll
);
1181 umem_free(rl
, sizeof (*rl
));
1185 ztest_zd_init(ztest_ds_t
*zd
, ztest_shared_ds_t
*szd
, objset_t
*os
)
1188 zd
->zd_zilog
= dmu_objset_zil(os
);
1189 zd
->zd_shared
= szd
;
1190 dmu_objset_name(os
, zd
->zd_name
);
1192 if (zd
->zd_shared
!= NULL
)
1193 zd
->zd_shared
->zd_seq
= 0;
1195 VERIFY(rwlock_init(&zd
->zd_zilog_lock
, USYNC_THREAD
, NULL
) == 0);
1196 VERIFY(_mutex_init(&zd
->zd_dirobj_lock
, USYNC_THREAD
, NULL
) == 0);
1198 for (int l
= 0; l
< ZTEST_OBJECT_LOCKS
; l
++)
1199 ztest_rll_init(&zd
->zd_object_lock
[l
]);
1201 for (int l
= 0; l
< ZTEST_RANGE_LOCKS
; l
++)
1202 ztest_rll_init(&zd
->zd_range_lock
[l
]);
1206 ztest_zd_fini(ztest_ds_t
*zd
)
1208 VERIFY(_mutex_destroy(&zd
->zd_dirobj_lock
) == 0);
1210 for (int l
= 0; l
< ZTEST_OBJECT_LOCKS
; l
++)
1211 ztest_rll_destroy(&zd
->zd_object_lock
[l
]);
1213 for (int l
= 0; l
< ZTEST_RANGE_LOCKS
; l
++)
1214 ztest_rll_destroy(&zd
->zd_range_lock
[l
]);
1217 #define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1220 ztest_tx_assign(dmu_tx_t
*tx
, uint64_t txg_how
, const char *tag
)
1226 * Attempt to assign tx to some transaction group.
1228 error
= dmu_tx_assign(tx
, txg_how
);
1230 if (error
== ERESTART
) {
1231 ASSERT(txg_how
== TXG_NOWAIT
);
1234 ASSERT3U(error
, ==, ENOSPC
);
1235 ztest_record_enospc(tag
);
1240 txg
= dmu_tx_get_txg(tx
);
1246 ztest_pattern_set(void *buf
, uint64_t size
, uint64_t value
)
1249 uint64_t *ip_end
= (uint64_t *)((uintptr_t)buf
+ (uintptr_t)size
);
1256 ztest_pattern_match(void *buf
, uint64_t size
, uint64_t value
)
1259 uint64_t *ip_end
= (uint64_t *)((uintptr_t)buf
+ (uintptr_t)size
);
1263 diff
|= (value
- *ip
++);
1269 ztest_bt_generate(ztest_block_tag_t
*bt
, objset_t
*os
, uint64_t object
,
1270 uint64_t offset
, uint64_t gen
, uint64_t txg
, uint64_t crtxg
)
1272 bt
->bt_magic
= BT_MAGIC
;
1273 bt
->bt_objset
= dmu_objset_id(os
);
1274 bt
->bt_object
= object
;
1275 bt
->bt_offset
= offset
;
1278 bt
->bt_crtxg
= crtxg
;
1282 ztest_bt_verify(ztest_block_tag_t
*bt
, objset_t
*os
, uint64_t object
,
1283 uint64_t offset
, uint64_t gen
, uint64_t txg
, uint64_t crtxg
)
1285 ASSERT3U(bt
->bt_magic
, ==, BT_MAGIC
);
1286 ASSERT3U(bt
->bt_objset
, ==, dmu_objset_id(os
));
1287 ASSERT3U(bt
->bt_object
, ==, object
);
1288 ASSERT3U(bt
->bt_offset
, ==, offset
);
1289 ASSERT3U(bt
->bt_gen
, <=, gen
);
1290 ASSERT3U(bt
->bt_txg
, <=, txg
);
1291 ASSERT3U(bt
->bt_crtxg
, ==, crtxg
);
1294 static ztest_block_tag_t
*
1295 ztest_bt_bonus(dmu_buf_t
*db
)
1297 dmu_object_info_t doi
;
1298 ztest_block_tag_t
*bt
;
1300 dmu_object_info_from_db(db
, &doi
);
1301 ASSERT3U(doi
.doi_bonus_size
, <=, db
->db_size
);
1302 ASSERT3U(doi
.doi_bonus_size
, >=, sizeof (*bt
));
1303 bt
= (void *)((char *)db
->db_data
+ doi
.doi_bonus_size
- sizeof (*bt
));
1312 #define lrz_type lr_mode
1313 #define lrz_blocksize lr_uid
1314 #define lrz_ibshift lr_gid
1315 #define lrz_bonustype lr_rdev
1316 #define lrz_bonuslen lr_crtime[1]
1319 ztest_log_create(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_create_t
*lr
)
1321 char *name
= (void *)(lr
+ 1); /* name follows lr */
1322 size_t namesize
= strlen(name
) + 1;
1325 if (zil_replaying(zd
->zd_zilog
, tx
))
1328 itx
= zil_itx_create(TX_CREATE
, sizeof (*lr
) + namesize
);
1329 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1330 sizeof (*lr
) + namesize
- sizeof (lr_t
));
1332 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1336 ztest_log_remove(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_remove_t
*lr
, uint64_t object
)
1338 char *name
= (void *)(lr
+ 1); /* name follows lr */
1339 size_t namesize
= strlen(name
) + 1;
1342 if (zil_replaying(zd
->zd_zilog
, tx
))
1345 itx
= zil_itx_create(TX_REMOVE
, sizeof (*lr
) + namesize
);
1346 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1347 sizeof (*lr
) + namesize
- sizeof (lr_t
));
1349 itx
->itx_oid
= object
;
1350 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1354 ztest_log_write(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_write_t
*lr
)
1357 itx_wr_state_t write_state
= ztest_random(WR_NUM_STATES
);
1359 if (zil_replaying(zd
->zd_zilog
, tx
))
1362 if (lr
->lr_length
> ZIL_MAX_LOG_DATA
)
1363 write_state
= WR_INDIRECT
;
1365 itx
= zil_itx_create(TX_WRITE
,
1366 sizeof (*lr
) + (write_state
== WR_COPIED
? lr
->lr_length
: 0));
1368 if (write_state
== WR_COPIED
&&
1369 dmu_read(zd
->zd_os
, lr
->lr_foid
, lr
->lr_offset
, lr
->lr_length
,
1370 ((lr_write_t
*)&itx
->itx_lr
) + 1, DMU_READ_NO_PREFETCH
) != 0) {
1371 zil_itx_destroy(itx
);
1372 itx
= zil_itx_create(TX_WRITE
, sizeof (*lr
));
1373 write_state
= WR_NEED_COPY
;
1375 itx
->itx_private
= zd
;
1376 itx
->itx_wr_state
= write_state
;
1377 itx
->itx_sync
= (ztest_random(8) == 0);
1378 itx
->itx_sod
+= (write_state
== WR_NEED_COPY
? lr
->lr_length
: 0);
1380 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1381 sizeof (*lr
) - sizeof (lr_t
));
1383 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1387 ztest_log_truncate(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_truncate_t
*lr
)
1391 if (zil_replaying(zd
->zd_zilog
, tx
))
1394 itx
= zil_itx_create(TX_TRUNCATE
, sizeof (*lr
));
1395 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1396 sizeof (*lr
) - sizeof (lr_t
));
1398 itx
->itx_sync
= B_FALSE
;
1399 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1403 ztest_log_setattr(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_setattr_t
*lr
)
1407 if (zil_replaying(zd
->zd_zilog
, tx
))
1410 itx
= zil_itx_create(TX_SETATTR
, sizeof (*lr
));
1411 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1412 sizeof (*lr
) - sizeof (lr_t
));
1414 itx
->itx_sync
= B_FALSE
;
1415 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1422 ztest_replay_create(ztest_ds_t
*zd
, lr_create_t
*lr
, boolean_t byteswap
)
1424 char *name
= (void *)(lr
+ 1); /* name follows lr */
1425 objset_t
*os
= zd
->zd_os
;
1426 ztest_block_tag_t
*bbt
;
1433 byteswap_uint64_array(lr
, sizeof (*lr
));
1435 ASSERT(lr
->lr_doid
== ZTEST_DIROBJ
);
1436 ASSERT(name
[0] != '\0');
1438 tx
= dmu_tx_create(os
);
1440 dmu_tx_hold_zap(tx
, lr
->lr_doid
, B_TRUE
, name
);
1442 if (lr
->lrz_type
== DMU_OT_ZAP_OTHER
) {
1443 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, B_TRUE
, NULL
);
1445 dmu_tx_hold_bonus(tx
, DMU_NEW_OBJECT
);
1448 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
1452 ASSERT(dmu_objset_zil(os
)->zl_replay
== !!lr
->lr_foid
);
1454 if (lr
->lrz_type
== DMU_OT_ZAP_OTHER
) {
1455 if (lr
->lr_foid
== 0) {
1456 lr
->lr_foid
= zap_create(os
,
1457 lr
->lrz_type
, lr
->lrz_bonustype
,
1458 lr
->lrz_bonuslen
, tx
);
1460 error
= zap_create_claim(os
, lr
->lr_foid
,
1461 lr
->lrz_type
, lr
->lrz_bonustype
,
1462 lr
->lrz_bonuslen
, tx
);
1465 if (lr
->lr_foid
== 0) {
1466 lr
->lr_foid
= dmu_object_alloc(os
,
1467 lr
->lrz_type
, 0, lr
->lrz_bonustype
,
1468 lr
->lrz_bonuslen
, tx
);
1470 error
= dmu_object_claim(os
, lr
->lr_foid
,
1471 lr
->lrz_type
, 0, lr
->lrz_bonustype
,
1472 lr
->lrz_bonuslen
, tx
);
1477 ASSERT3U(error
, ==, EEXIST
);
1478 ASSERT(zd
->zd_zilog
->zl_replay
);
1483 ASSERT(lr
->lr_foid
!= 0);
1485 if (lr
->lrz_type
!= DMU_OT_ZAP_OTHER
)
1486 VERIFY3U(0, ==, dmu_object_set_blocksize(os
, lr
->lr_foid
,
1487 lr
->lrz_blocksize
, lr
->lrz_ibshift
, tx
));
1489 VERIFY3U(0, ==, dmu_bonus_hold(os
, lr
->lr_foid
, FTAG
, &db
));
1490 bbt
= ztest_bt_bonus(db
);
1491 dmu_buf_will_dirty(db
, tx
);
1492 ztest_bt_generate(bbt
, os
, lr
->lr_foid
, -1ULL, lr
->lr_gen
, txg
, txg
);
1493 dmu_buf_rele(db
, FTAG
);
1495 VERIFY3U(0, ==, zap_add(os
, lr
->lr_doid
, name
, sizeof (uint64_t), 1,
1498 (void) ztest_log_create(zd
, tx
, lr
);
1506 ztest_replay_remove(ztest_ds_t
*zd
, lr_remove_t
*lr
, boolean_t byteswap
)
1508 char *name
= (void *)(lr
+ 1); /* name follows lr */
1509 objset_t
*os
= zd
->zd_os
;
1510 dmu_object_info_t doi
;
1512 uint64_t object
, txg
;
1515 byteswap_uint64_array(lr
, sizeof (*lr
));
1517 ASSERT(lr
->lr_doid
== ZTEST_DIROBJ
);
1518 ASSERT(name
[0] != '\0');
1521 zap_lookup(os
, lr
->lr_doid
, name
, sizeof (object
), 1, &object
));
1522 ASSERT(object
!= 0);
1524 ztest_object_lock(zd
, object
, RL_WRITER
);
1526 VERIFY3U(0, ==, dmu_object_info(os
, object
, &doi
));
1528 tx
= dmu_tx_create(os
);
1530 dmu_tx_hold_zap(tx
, lr
->lr_doid
, B_FALSE
, name
);
1531 dmu_tx_hold_free(tx
, object
, 0, DMU_OBJECT_END
);
1533 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
1535 ztest_object_unlock(zd
, object
);
1539 if (doi
.doi_type
== DMU_OT_ZAP_OTHER
) {
1540 VERIFY3U(0, ==, zap_destroy(os
, object
, tx
));
1542 VERIFY3U(0, ==, dmu_object_free(os
, object
, tx
));
1545 VERIFY3U(0, ==, zap_remove(os
, lr
->lr_doid
, name
, tx
));
1547 (void) ztest_log_remove(zd
, tx
, lr
, object
);
1551 ztest_object_unlock(zd
, object
);
1557 ztest_replay_write(ztest_ds_t
*zd
, lr_write_t
*lr
, boolean_t byteswap
)
1559 objset_t
*os
= zd
->zd_os
;
1560 void *data
= lr
+ 1; /* data follows lr */
1561 uint64_t offset
, length
;
1562 ztest_block_tag_t
*bt
= data
;
1563 ztest_block_tag_t
*bbt
;
1564 uint64_t gen
, txg
, lrtxg
, crtxg
;
1565 dmu_object_info_t doi
;
1568 arc_buf_t
*abuf
= NULL
;
1572 byteswap_uint64_array(lr
, sizeof (*lr
));
1574 offset
= lr
->lr_offset
;
1575 length
= lr
->lr_length
;
1577 /* If it's a dmu_sync() block, write the whole block */
1578 if (lr
->lr_common
.lrc_reclen
== sizeof (lr_write_t
)) {
1579 uint64_t blocksize
= BP_GET_LSIZE(&lr
->lr_blkptr
);
1580 if (length
< blocksize
) {
1581 offset
-= offset
% blocksize
;
1586 if (bt
->bt_magic
== BSWAP_64(BT_MAGIC
))
1587 byteswap_uint64_array(bt
, sizeof (*bt
));
1589 if (bt
->bt_magic
!= BT_MAGIC
)
1592 ztest_object_lock(zd
, lr
->lr_foid
, RL_READER
);
1593 rl
= ztest_range_lock(zd
, lr
->lr_foid
, offset
, length
, RL_WRITER
);
1595 VERIFY3U(0, ==, dmu_bonus_hold(os
, lr
->lr_foid
, FTAG
, &db
));
1597 dmu_object_info_from_db(db
, &doi
);
1599 bbt
= ztest_bt_bonus(db
);
1600 ASSERT3U(bbt
->bt_magic
, ==, BT_MAGIC
);
1602 crtxg
= bbt
->bt_crtxg
;
1603 lrtxg
= lr
->lr_common
.lrc_txg
;
1605 tx
= dmu_tx_create(os
);
1607 dmu_tx_hold_write(tx
, lr
->lr_foid
, offset
, length
);
1609 if (ztest_random(8) == 0 && length
== doi
.doi_data_block_size
&&
1610 P2PHASE(offset
, length
) == 0)
1611 abuf
= dmu_request_arcbuf(db
, length
);
1613 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
1616 dmu_return_arcbuf(abuf
);
1617 dmu_buf_rele(db
, FTAG
);
1618 ztest_range_unlock(rl
);
1619 ztest_object_unlock(zd
, lr
->lr_foid
);
1625 * Usually, verify the old data before writing new data --
1626 * but not always, because we also want to verify correct
1627 * behavior when the data was not recently read into cache.
1629 ASSERT(offset
% doi
.doi_data_block_size
== 0);
1630 if (ztest_random(4) != 0) {
1631 int prefetch
= ztest_random(2) ?
1632 DMU_READ_PREFETCH
: DMU_READ_NO_PREFETCH
;
1633 ztest_block_tag_t rbt
;
1635 VERIFY(dmu_read(os
, lr
->lr_foid
, offset
,
1636 sizeof (rbt
), &rbt
, prefetch
) == 0);
1637 if (rbt
.bt_magic
== BT_MAGIC
) {
1638 ztest_bt_verify(&rbt
, os
, lr
->lr_foid
,
1639 offset
, gen
, txg
, crtxg
);
1644 * Writes can appear to be newer than the bonus buffer because
1645 * the ztest_get_data() callback does a dmu_read() of the
1646 * open-context data, which may be different than the data
1647 * as it was when the write was generated.
1649 if (zd
->zd_zilog
->zl_replay
) {
1650 ztest_bt_verify(bt
, os
, lr
->lr_foid
, offset
,
1651 MAX(gen
, bt
->bt_gen
), MAX(txg
, lrtxg
),
1656 * Set the bt's gen/txg to the bonus buffer's gen/txg
1657 * so that all of the usual ASSERTs will work.
1659 ztest_bt_generate(bt
, os
, lr
->lr_foid
, offset
, gen
, txg
, crtxg
);
1663 dmu_write(os
, lr
->lr_foid
, offset
, length
, data
, tx
);
1665 bcopy(data
, abuf
->b_data
, length
);
1666 dmu_assign_arcbuf(db
, offset
, abuf
, tx
);
1669 (void) ztest_log_write(zd
, tx
, lr
);
1671 dmu_buf_rele(db
, FTAG
);
1675 ztest_range_unlock(rl
);
1676 ztest_object_unlock(zd
, lr
->lr_foid
);
1682 ztest_replay_truncate(ztest_ds_t
*zd
, lr_truncate_t
*lr
, boolean_t byteswap
)
1684 objset_t
*os
= zd
->zd_os
;
1690 byteswap_uint64_array(lr
, sizeof (*lr
));
1692 ztest_object_lock(zd
, lr
->lr_foid
, RL_READER
);
1693 rl
= ztest_range_lock(zd
, lr
->lr_foid
, lr
->lr_offset
, lr
->lr_length
,
1696 tx
= dmu_tx_create(os
);
1698 dmu_tx_hold_free(tx
, lr
->lr_foid
, lr
->lr_offset
, lr
->lr_length
);
1700 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
1702 ztest_range_unlock(rl
);
1703 ztest_object_unlock(zd
, lr
->lr_foid
);
1707 VERIFY(dmu_free_range(os
, lr
->lr_foid
, lr
->lr_offset
,
1708 lr
->lr_length
, tx
) == 0);
1710 (void) ztest_log_truncate(zd
, tx
, lr
);
1714 ztest_range_unlock(rl
);
1715 ztest_object_unlock(zd
, lr
->lr_foid
);
1721 ztest_replay_setattr(ztest_ds_t
*zd
, lr_setattr_t
*lr
, boolean_t byteswap
)
1723 objset_t
*os
= zd
->zd_os
;
1726 ztest_block_tag_t
*bbt
;
1727 uint64_t txg
, lrtxg
, crtxg
;
1730 byteswap_uint64_array(lr
, sizeof (*lr
));
1732 ztest_object_lock(zd
, lr
->lr_foid
, RL_WRITER
);
1734 VERIFY3U(0, ==, dmu_bonus_hold(os
, lr
->lr_foid
, FTAG
, &db
));
1736 tx
= dmu_tx_create(os
);
1737 dmu_tx_hold_bonus(tx
, lr
->lr_foid
);
1739 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
1741 dmu_buf_rele(db
, FTAG
);
1742 ztest_object_unlock(zd
, lr
->lr_foid
);
1746 bbt
= ztest_bt_bonus(db
);
1747 ASSERT3U(bbt
->bt_magic
, ==, BT_MAGIC
);
1748 crtxg
= bbt
->bt_crtxg
;
1749 lrtxg
= lr
->lr_common
.lrc_txg
;
1751 if (zd
->zd_zilog
->zl_replay
) {
1752 ASSERT(lr
->lr_size
!= 0);
1753 ASSERT(lr
->lr_mode
!= 0);
1757 * Randomly change the size and increment the generation.
1759 lr
->lr_size
= (ztest_random(db
->db_size
/ sizeof (*bbt
)) + 1) *
1761 lr
->lr_mode
= bbt
->bt_gen
+ 1;
1766 * Verify that the current bonus buffer is not newer than our txg.
1768 ztest_bt_verify(bbt
, os
, lr
->lr_foid
, -1ULL, lr
->lr_mode
,
1769 MAX(txg
, lrtxg
), crtxg
);
1771 dmu_buf_will_dirty(db
, tx
);
1773 ASSERT3U(lr
->lr_size
, >=, sizeof (*bbt
));
1774 ASSERT3U(lr
->lr_size
, <=, db
->db_size
);
1775 VERIFY0(dmu_set_bonus(db
, lr
->lr_size
, tx
));
1776 bbt
= ztest_bt_bonus(db
);
1778 ztest_bt_generate(bbt
, os
, lr
->lr_foid
, -1ULL, lr
->lr_mode
, txg
, crtxg
);
1780 dmu_buf_rele(db
, FTAG
);
1782 (void) ztest_log_setattr(zd
, tx
, lr
);
1786 ztest_object_unlock(zd
, lr
->lr_foid
);
1791 zil_replay_func_t
*ztest_replay_vector
[TX_MAX_TYPE
] = {
1792 NULL
, /* 0 no such transaction type */
1793 ztest_replay_create
, /* TX_CREATE */
1794 NULL
, /* TX_MKDIR */
1795 NULL
, /* TX_MKXATTR */
1796 NULL
, /* TX_SYMLINK */
1797 ztest_replay_remove
, /* TX_REMOVE */
1798 NULL
, /* TX_RMDIR */
1800 NULL
, /* TX_RENAME */
1801 ztest_replay_write
, /* TX_WRITE */
1802 ztest_replay_truncate
, /* TX_TRUNCATE */
1803 ztest_replay_setattr
, /* TX_SETATTR */
1805 NULL
, /* TX_CREATE_ACL */
1806 NULL
, /* TX_CREATE_ATTR */
1807 NULL
, /* TX_CREATE_ACL_ATTR */
1808 NULL
, /* TX_MKDIR_ACL */
1809 NULL
, /* TX_MKDIR_ATTR */
1810 NULL
, /* TX_MKDIR_ACL_ATTR */
1811 NULL
, /* TX_WRITE2 */
1815 * ZIL get_data callbacks
1819 ztest_get_done(zgd_t
*zgd
, int error
)
1821 ztest_ds_t
*zd
= zgd
->zgd_private
;
1822 uint64_t object
= zgd
->zgd_rl
->rl_object
;
1825 dmu_buf_rele(zgd
->zgd_db
, zgd
);
1827 ztest_range_unlock(zgd
->zgd_rl
);
1828 ztest_object_unlock(zd
, object
);
1830 if (error
== 0 && zgd
->zgd_bp
)
1831 zil_add_block(zgd
->zgd_zilog
, zgd
->zgd_bp
);
1833 umem_free(zgd
, sizeof (*zgd
));
1837 ztest_get_data(void *arg
, lr_write_t
*lr
, char *buf
, zio_t
*zio
)
1839 ztest_ds_t
*zd
= arg
;
1840 objset_t
*os
= zd
->zd_os
;
1841 uint64_t object
= lr
->lr_foid
;
1842 uint64_t offset
= lr
->lr_offset
;
1843 uint64_t size
= lr
->lr_length
;
1844 blkptr_t
*bp
= &lr
->lr_blkptr
;
1845 uint64_t txg
= lr
->lr_common
.lrc_txg
;
1847 dmu_object_info_t doi
;
1852 ztest_object_lock(zd
, object
, RL_READER
);
1853 error
= dmu_bonus_hold(os
, object
, FTAG
, &db
);
1855 ztest_object_unlock(zd
, object
);
1859 crtxg
= ztest_bt_bonus(db
)->bt_crtxg
;
1861 if (crtxg
== 0 || crtxg
> txg
) {
1862 dmu_buf_rele(db
, FTAG
);
1863 ztest_object_unlock(zd
, object
);
1867 dmu_object_info_from_db(db
, &doi
);
1868 dmu_buf_rele(db
, FTAG
);
1871 zgd
= umem_zalloc(sizeof (*zgd
), UMEM_NOFAIL
);
1872 zgd
->zgd_zilog
= zd
->zd_zilog
;
1873 zgd
->zgd_private
= zd
;
1875 if (buf
!= NULL
) { /* immediate write */
1876 zgd
->zgd_rl
= ztest_range_lock(zd
, object
, offset
, size
,
1879 error
= dmu_read(os
, object
, offset
, size
, buf
,
1880 DMU_READ_NO_PREFETCH
);
1883 size
= doi
.doi_data_block_size
;
1885 offset
= P2ALIGN(offset
, size
);
1887 ASSERT(offset
< size
);
1891 zgd
->zgd_rl
= ztest_range_lock(zd
, object
, offset
, size
,
1894 error
= dmu_buf_hold(os
, object
, offset
, zgd
, &db
,
1895 DMU_READ_NO_PREFETCH
);
1898 blkptr_t
*obp
= dmu_buf_get_blkptr(db
);
1900 ASSERT(BP_IS_HOLE(bp
));
1907 ASSERT(db
->db_offset
== offset
);
1908 ASSERT(db
->db_size
== size
);
1910 error
= dmu_sync(zio
, lr
->lr_common
.lrc_txg
,
1911 ztest_get_done
, zgd
);
1918 ztest_get_done(zgd
, error
);
1924 ztest_lr_alloc(size_t lrsize
, char *name
)
1927 size_t namesize
= name
? strlen(name
) + 1 : 0;
1929 lr
= umem_zalloc(lrsize
+ namesize
, UMEM_NOFAIL
);
1932 bcopy(name
, lr
+ lrsize
, namesize
);
1938 ztest_lr_free(void *lr
, size_t lrsize
, char *name
)
1940 size_t namesize
= name
? strlen(name
) + 1 : 0;
1942 umem_free(lr
, lrsize
+ namesize
);
1946 * Lookup a bunch of objects. Returns the number of objects not found.
1949 ztest_lookup(ztest_ds_t
*zd
, ztest_od_t
*od
, int count
)
1954 ASSERT(_mutex_held(&zd
->zd_dirobj_lock
));
1956 for (int i
= 0; i
< count
; i
++, od
++) {
1958 error
= zap_lookup(zd
->zd_os
, od
->od_dir
, od
->od_name
,
1959 sizeof (uint64_t), 1, &od
->od_object
);
1961 ASSERT(error
== ENOENT
);
1962 ASSERT(od
->od_object
== 0);
1966 ztest_block_tag_t
*bbt
;
1967 dmu_object_info_t doi
;
1969 ASSERT(od
->od_object
!= 0);
1970 ASSERT(missing
== 0); /* there should be no gaps */
1972 ztest_object_lock(zd
, od
->od_object
, RL_READER
);
1973 VERIFY3U(0, ==, dmu_bonus_hold(zd
->zd_os
,
1974 od
->od_object
, FTAG
, &db
));
1975 dmu_object_info_from_db(db
, &doi
);
1976 bbt
= ztest_bt_bonus(db
);
1977 ASSERT3U(bbt
->bt_magic
, ==, BT_MAGIC
);
1978 od
->od_type
= doi
.doi_type
;
1979 od
->od_blocksize
= doi
.doi_data_block_size
;
1980 od
->od_gen
= bbt
->bt_gen
;
1981 dmu_buf_rele(db
, FTAG
);
1982 ztest_object_unlock(zd
, od
->od_object
);
1990 ztest_create(ztest_ds_t
*zd
, ztest_od_t
*od
, int count
)
1994 ASSERT(_mutex_held(&zd
->zd_dirobj_lock
));
1996 for (int i
= 0; i
< count
; i
++, od
++) {
2003 lr_create_t
*lr
= ztest_lr_alloc(sizeof (*lr
), od
->od_name
);
2005 lr
->lr_doid
= od
->od_dir
;
2006 lr
->lr_foid
= 0; /* 0 to allocate, > 0 to claim */
2007 lr
->lrz_type
= od
->od_crtype
;
2008 lr
->lrz_blocksize
= od
->od_crblocksize
;
2009 lr
->lrz_ibshift
= ztest_random_ibshift();
2010 lr
->lrz_bonustype
= DMU_OT_UINT64_OTHER
;
2011 lr
->lrz_bonuslen
= dmu_bonus_max();
2012 lr
->lr_gen
= od
->od_crgen
;
2013 lr
->lr_crtime
[0] = time(NULL
);
2015 if (ztest_replay_create(zd
, lr
, B_FALSE
) != 0) {
2016 ASSERT(missing
== 0);
2020 od
->od_object
= lr
->lr_foid
;
2021 od
->od_type
= od
->od_crtype
;
2022 od
->od_blocksize
= od
->od_crblocksize
;
2023 od
->od_gen
= od
->od_crgen
;
2024 ASSERT(od
->od_object
!= 0);
2027 ztest_lr_free(lr
, sizeof (*lr
), od
->od_name
);
2034 ztest_remove(ztest_ds_t
*zd
, ztest_od_t
*od
, int count
)
2039 ASSERT(_mutex_held(&zd
->zd_dirobj_lock
));
2043 for (int i
= count
- 1; i
>= 0; i
--, od
--) {
2050 * No object was found.
2052 if (od
->od_object
== 0)
2055 lr_remove_t
*lr
= ztest_lr_alloc(sizeof (*lr
), od
->od_name
);
2057 lr
->lr_doid
= od
->od_dir
;
2059 if ((error
= ztest_replay_remove(zd
, lr
, B_FALSE
)) != 0) {
2060 ASSERT3U(error
, ==, ENOSPC
);
2065 ztest_lr_free(lr
, sizeof (*lr
), od
->od_name
);
2072 ztest_write(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
, uint64_t size
,
2078 lr
= ztest_lr_alloc(sizeof (*lr
) + size
, NULL
);
2080 lr
->lr_foid
= object
;
2081 lr
->lr_offset
= offset
;
2082 lr
->lr_length
= size
;
2084 BP_ZERO(&lr
->lr_blkptr
);
2086 bcopy(data
, lr
+ 1, size
);
2088 error
= ztest_replay_write(zd
, lr
, B_FALSE
);
2090 ztest_lr_free(lr
, sizeof (*lr
) + size
, NULL
);
2096 ztest_truncate(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
, uint64_t size
)
2101 lr
= ztest_lr_alloc(sizeof (*lr
), NULL
);
2103 lr
->lr_foid
= object
;
2104 lr
->lr_offset
= offset
;
2105 lr
->lr_length
= size
;
2107 error
= ztest_replay_truncate(zd
, lr
, B_FALSE
);
2109 ztest_lr_free(lr
, sizeof (*lr
), NULL
);
2115 ztest_setattr(ztest_ds_t
*zd
, uint64_t object
)
2120 lr
= ztest_lr_alloc(sizeof (*lr
), NULL
);
2122 lr
->lr_foid
= object
;
2126 error
= ztest_replay_setattr(zd
, lr
, B_FALSE
);
2128 ztest_lr_free(lr
, sizeof (*lr
), NULL
);
2134 ztest_prealloc(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
, uint64_t size
)
2136 objset_t
*os
= zd
->zd_os
;
2141 txg_wait_synced(dmu_objset_pool(os
), 0);
2143 ztest_object_lock(zd
, object
, RL_READER
);
2144 rl
= ztest_range_lock(zd
, object
, offset
, size
, RL_WRITER
);
2146 tx
= dmu_tx_create(os
);
2148 dmu_tx_hold_write(tx
, object
, offset
, size
);
2150 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
2153 dmu_prealloc(os
, object
, offset
, size
, tx
);
2155 txg_wait_synced(dmu_objset_pool(os
), txg
);
2157 (void) dmu_free_long_range(os
, object
, offset
, size
);
2160 ztest_range_unlock(rl
);
2161 ztest_object_unlock(zd
, object
);
2165 ztest_io(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
)
2168 ztest_block_tag_t wbt
;
2169 dmu_object_info_t doi
;
2170 enum ztest_io_type io_type
;
2174 VERIFY(dmu_object_info(zd
->zd_os
, object
, &doi
) == 0);
2175 blocksize
= doi
.doi_data_block_size
;
2176 data
= umem_alloc(blocksize
, UMEM_NOFAIL
);
2179 * Pick an i/o type at random, biased toward writing block tags.
2181 io_type
= ztest_random(ZTEST_IO_TYPES
);
2182 if (ztest_random(2) == 0)
2183 io_type
= ZTEST_IO_WRITE_TAG
;
2185 (void) rw_rdlock(&zd
->zd_zilog_lock
);
2189 case ZTEST_IO_WRITE_TAG
:
2190 ztest_bt_generate(&wbt
, zd
->zd_os
, object
, offset
, 0, 0, 0);
2191 (void) ztest_write(zd
, object
, offset
, sizeof (wbt
), &wbt
);
2194 case ZTEST_IO_WRITE_PATTERN
:
2195 (void) memset(data
, 'a' + (object
+ offset
) % 5, blocksize
);
2196 if (ztest_random(2) == 0) {
2198 * Induce fletcher2 collisions to ensure that
2199 * zio_ddt_collision() detects and resolves them
2200 * when using fletcher2-verify for deduplication.
2202 ((uint64_t *)data
)[0] ^= 1ULL << 63;
2203 ((uint64_t *)data
)[4] ^= 1ULL << 63;
2205 (void) ztest_write(zd
, object
, offset
, blocksize
, data
);
2208 case ZTEST_IO_WRITE_ZEROES
:
2209 bzero(data
, blocksize
);
2210 (void) ztest_write(zd
, object
, offset
, blocksize
, data
);
2213 case ZTEST_IO_TRUNCATE
:
2214 (void) ztest_truncate(zd
, object
, offset
, blocksize
);
2217 case ZTEST_IO_SETATTR
:
2218 (void) ztest_setattr(zd
, object
);
2221 case ZTEST_IO_REWRITE
:
2222 (void) rw_rdlock(&ztest_name_lock
);
2223 err
= ztest_dsl_prop_set_uint64(zd
->zd_name
,
2224 ZFS_PROP_CHECKSUM
, spa_dedup_checksum(ztest_spa
),
2226 VERIFY(err
== 0 || err
== ENOSPC
);
2227 err
= ztest_dsl_prop_set_uint64(zd
->zd_name
,
2228 ZFS_PROP_COMPRESSION
,
2229 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION
),
2231 VERIFY(err
== 0 || err
== ENOSPC
);
2232 (void) rw_unlock(&ztest_name_lock
);
2234 VERIFY0(dmu_read(zd
->zd_os
, object
, offset
, blocksize
, data
,
2235 DMU_READ_NO_PREFETCH
));
2237 (void) ztest_write(zd
, object
, offset
, blocksize
, data
);
2241 (void) rw_unlock(&zd
->zd_zilog_lock
);
2243 umem_free(data
, blocksize
);
2247 * Initialize an object description template.
2250 ztest_od_init(ztest_od_t
*od
, uint64_t id
, char *tag
, uint64_t index
,
2251 dmu_object_type_t type
, uint64_t blocksize
, uint64_t gen
)
2253 od
->od_dir
= ZTEST_DIROBJ
;
2256 od
->od_crtype
= type
;
2257 od
->od_crblocksize
= blocksize
? blocksize
: ztest_random_blocksize();
2260 od
->od_type
= DMU_OT_NONE
;
2261 od
->od_blocksize
= 0;
2264 (void) snprintf(od
->od_name
, sizeof (od
->od_name
), "%s(%lld)[%llu]",
2265 tag
, (int64_t)id
, index
);
2269 * Lookup or create the objects for a test using the od template.
2270 * If the objects do not all exist, or if 'remove' is specified,
2271 * remove any existing objects and create new ones. Otherwise,
2272 * use the existing objects.
2275 ztest_object_init(ztest_ds_t
*zd
, ztest_od_t
*od
, size_t size
, boolean_t remove
)
2277 int count
= size
/ sizeof (*od
);
2280 VERIFY(mutex_lock(&zd
->zd_dirobj_lock
) == 0);
2281 if ((ztest_lookup(zd
, od
, count
) != 0 || remove
) &&
2282 (ztest_remove(zd
, od
, count
) != 0 ||
2283 ztest_create(zd
, od
, count
) != 0))
2286 VERIFY(mutex_unlock(&zd
->zd_dirobj_lock
) == 0);
2293 ztest_zil_commit(ztest_ds_t
*zd
, uint64_t id
)
2295 zilog_t
*zilog
= zd
->zd_zilog
;
2297 (void) rw_rdlock(&zd
->zd_zilog_lock
);
2299 zil_commit(zilog
, ztest_random(ZTEST_OBJECTS
));
2302 * Remember the committed values in zd, which is in parent/child
2303 * shared memory. If we die, the next iteration of ztest_run()
2304 * will verify that the log really does contain this record.
2306 mutex_enter(&zilog
->zl_lock
);
2307 ASSERT(zd
->zd_shared
!= NULL
);
2308 ASSERT3U(zd
->zd_shared
->zd_seq
, <=, zilog
->zl_commit_lr_seq
);
2309 zd
->zd_shared
->zd_seq
= zilog
->zl_commit_lr_seq
;
2310 mutex_exit(&zilog
->zl_lock
);
2312 (void) rw_unlock(&zd
->zd_zilog_lock
);
2316 * This function is designed to simulate the operations that occur during a
2317 * mount/unmount operation. We hold the dataset across these operations in an
2318 * attempt to expose any implicit assumptions about ZIL management.
2322 ztest_zil_remount(ztest_ds_t
*zd
, uint64_t id
)
2324 objset_t
*os
= zd
->zd_os
;
2327 * We grab the zd_dirobj_lock to ensure that no other thread is
2328 * updating the zil (i.e. adding in-memory log records) and the
2329 * zd_zilog_lock to block any I/O.
2331 VERIFY0(mutex_lock(&zd
->zd_dirobj_lock
));
2332 (void) rw_wrlock(&zd
->zd_zilog_lock
);
2334 /* zfsvfs_teardown() */
2335 zil_close(zd
->zd_zilog
);
2337 /* zfsvfs_setup() */
2338 VERIFY(zil_open(os
, ztest_get_data
) == zd
->zd_zilog
);
2339 zil_replay(os
, zd
, ztest_replay_vector
);
2341 (void) rw_unlock(&zd
->zd_zilog_lock
);
2342 VERIFY(mutex_unlock(&zd
->zd_dirobj_lock
) == 0);
2346 * Verify that we can't destroy an active pool, create an existing pool,
2347 * or create a pool with a bad vdev spec.
2351 ztest_spa_create_destroy(ztest_ds_t
*zd
, uint64_t id
)
2353 ztest_shared_opts_t
*zo
= &ztest_opts
;
2358 * Attempt to create using a bad file.
2360 nvroot
= make_vdev_root("/dev/bogus", NULL
, NULL
, 0, 0, 0, 0, 0, 1);
2361 VERIFY3U(ENOENT
, ==,
2362 spa_create("ztest_bad_file", nvroot
, NULL
, NULL
));
2363 nvlist_free(nvroot
);
2366 * Attempt to create using a bad mirror.
2368 nvroot
= make_vdev_root("/dev/bogus", NULL
, NULL
, 0, 0, 0, 0, 2, 1);
2369 VERIFY3U(ENOENT
, ==,
2370 spa_create("ztest_bad_mirror", nvroot
, NULL
, NULL
));
2371 nvlist_free(nvroot
);
2374 * Attempt to create an existing pool. It shouldn't matter
2375 * what's in the nvroot; we should fail with EEXIST.
2377 (void) rw_rdlock(&ztest_name_lock
);
2378 nvroot
= make_vdev_root("/dev/bogus", NULL
, NULL
, 0, 0, 0, 0, 0, 1);
2379 VERIFY3U(EEXIST
, ==, spa_create(zo
->zo_pool
, nvroot
, NULL
, NULL
));
2380 nvlist_free(nvroot
);
2381 VERIFY3U(0, ==, spa_open(zo
->zo_pool
, &spa
, FTAG
));
2382 VERIFY3U(EBUSY
, ==, spa_destroy(zo
->zo_pool
));
2383 spa_close(spa
, FTAG
);
2385 (void) rw_unlock(&ztest_name_lock
);
2390 ztest_spa_upgrade(ztest_ds_t
*zd
, uint64_t id
)
2393 uint64_t initial_version
= SPA_VERSION_INITIAL
;
2394 uint64_t version
, newversion
;
2395 nvlist_t
*nvroot
, *props
;
2398 VERIFY0(mutex_lock(&ztest_vdev_lock
));
2399 name
= kmem_asprintf("%s_upgrade", ztest_opts
.zo_pool
);
2402 * Clean up from previous runs.
2404 (void) spa_destroy(name
);
2406 nvroot
= make_vdev_root(NULL
, NULL
, name
, ztest_opts
.zo_vdev_size
, 0,
2407 0, ztest_opts
.zo_raidz
, ztest_opts
.zo_mirrors
, 1);
2410 * If we're configuring a RAIDZ device then make sure that the
2411 * the initial version is capable of supporting that feature.
2413 switch (ztest_opts
.zo_raidz_parity
) {
2416 initial_version
= SPA_VERSION_INITIAL
;
2419 initial_version
= SPA_VERSION_RAIDZ2
;
2422 initial_version
= SPA_VERSION_RAIDZ3
;
2427 * Create a pool with a spa version that can be upgraded. Pick
2428 * a value between initial_version and SPA_VERSION_BEFORE_FEATURES.
2431 version
= ztest_random_spa_version(initial_version
);
2432 } while (version
> SPA_VERSION_BEFORE_FEATURES
);
2434 props
= fnvlist_alloc();
2435 fnvlist_add_uint64(props
,
2436 zpool_prop_to_name(ZPOOL_PROP_VERSION
), version
);
2437 VERIFY0(spa_create(name
, nvroot
, props
, NULL
));
2438 fnvlist_free(nvroot
);
2439 fnvlist_free(props
);
2441 VERIFY0(spa_open(name
, &spa
, FTAG
));
2442 VERIFY3U(spa_version(spa
), ==, version
);
2443 newversion
= ztest_random_spa_version(version
+ 1);
2445 if (ztest_opts
.zo_verbose
>= 4) {
2446 (void) printf("upgrading spa version from %llu to %llu\n",
2447 (u_longlong_t
)version
, (u_longlong_t
)newversion
);
2450 spa_upgrade(spa
, newversion
);
2451 VERIFY3U(spa_version(spa
), >, version
);
2452 VERIFY3U(spa_version(spa
), ==, fnvlist_lookup_uint64(spa
->spa_config
,
2453 zpool_prop_to_name(ZPOOL_PROP_VERSION
)));
2454 spa_close(spa
, FTAG
);
2457 VERIFY0(mutex_unlock(&ztest_vdev_lock
));
2461 vdev_lookup_by_path(vdev_t
*vd
, const char *path
)
2465 if (vd
->vdev_path
!= NULL
&& strcmp(path
, vd
->vdev_path
) == 0)
2468 for (int c
= 0; c
< vd
->vdev_children
; c
++)
2469 if ((mvd
= vdev_lookup_by_path(vd
->vdev_child
[c
], path
)) !=
2477 * Find the first available hole which can be used as a top-level.
2480 find_vdev_hole(spa_t
*spa
)
2482 vdev_t
*rvd
= spa
->spa_root_vdev
;
2485 ASSERT(spa_config_held(spa
, SCL_VDEV
, RW_READER
) == SCL_VDEV
);
2487 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
2488 vdev_t
*cvd
= rvd
->vdev_child
[c
];
2490 if (cvd
->vdev_ishole
)
2497 * Verify that vdev_add() works as expected.
2501 ztest_vdev_add_remove(ztest_ds_t
*zd
, uint64_t id
)
2503 ztest_shared_t
*zs
= ztest_shared
;
2504 spa_t
*spa
= ztest_spa
;
2510 VERIFY(mutex_lock(&ztest_vdev_lock
) == 0);
2511 leaves
= MAX(zs
->zs_mirrors
+ zs
->zs_splits
, 1) * ztest_opts
.zo_raidz
;
2513 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
2515 ztest_shared
->zs_vdev_next_leaf
= find_vdev_hole(spa
) * leaves
;
2518 * If we have slogs then remove them 1/4 of the time.
2520 if (spa_has_slogs(spa
) && ztest_random(4) == 0) {
2522 * Grab the guid from the head of the log class rotor.
2524 guid
= spa_log_class(spa
)->mc_rotor
->mg_vd
->vdev_guid
;
2526 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
2529 * We have to grab the zs_name_lock as writer to
2530 * prevent a race between removing a slog (dmu_objset_find)
2531 * and destroying a dataset. Removing the slog will
2532 * grab a reference on the dataset which may cause
2533 * dmu_objset_destroy() to fail with EBUSY thus
2534 * leaving the dataset in an inconsistent state.
2536 VERIFY(rw_wrlock(&ztest_name_lock
) == 0);
2537 error
= spa_vdev_remove(spa
, guid
, B_FALSE
);
2538 VERIFY(rw_unlock(&ztest_name_lock
) == 0);
2540 if (error
&& error
!= EEXIST
)
2541 fatal(0, "spa_vdev_remove() = %d", error
);
2543 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
2546 * Make 1/4 of the devices be log devices.
2548 nvroot
= make_vdev_root(NULL
, NULL
, NULL
,
2549 ztest_opts
.zo_vdev_size
, 0,
2550 ztest_random(4) == 0, ztest_opts
.zo_raidz
,
2553 error
= spa_vdev_add(spa
, nvroot
);
2554 nvlist_free(nvroot
);
2556 if (error
== ENOSPC
)
2557 ztest_record_enospc("spa_vdev_add");
2558 else if (error
!= 0)
2559 fatal(0, "spa_vdev_add() = %d", error
);
2562 VERIFY(mutex_unlock(&ztest_vdev_lock
) == 0);
2566 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
2570 ztest_vdev_aux_add_remove(ztest_ds_t
*zd
, uint64_t id
)
2572 ztest_shared_t
*zs
= ztest_shared
;
2573 spa_t
*spa
= ztest_spa
;
2574 vdev_t
*rvd
= spa
->spa_root_vdev
;
2575 spa_aux_vdev_t
*sav
;
2580 if (ztest_random(2) == 0) {
2581 sav
= &spa
->spa_spares
;
2582 aux
= ZPOOL_CONFIG_SPARES
;
2584 sav
= &spa
->spa_l2cache
;
2585 aux
= ZPOOL_CONFIG_L2CACHE
;
2588 VERIFY(mutex_lock(&ztest_vdev_lock
) == 0);
2590 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
2592 if (sav
->sav_count
!= 0 && ztest_random(4) == 0) {
2594 * Pick a random device to remove.
2596 guid
= sav
->sav_vdevs
[ztest_random(sav
->sav_count
)]->vdev_guid
;
2599 * Find an unused device we can add.
2601 zs
->zs_vdev_aux
= 0;
2603 char path
[MAXPATHLEN
];
2605 (void) snprintf(path
, sizeof (path
), ztest_aux_template
,
2606 ztest_opts
.zo_dir
, ztest_opts
.zo_pool
, aux
,
2608 for (c
= 0; c
< sav
->sav_count
; c
++)
2609 if (strcmp(sav
->sav_vdevs
[c
]->vdev_path
,
2612 if (c
== sav
->sav_count
&&
2613 vdev_lookup_by_path(rvd
, path
) == NULL
)
2619 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
2625 nvlist_t
*nvroot
= make_vdev_root(NULL
, aux
, NULL
,
2626 (ztest_opts
.zo_vdev_size
* 5) / 4, 0, 0, 0, 0, 1);
2627 error
= spa_vdev_add(spa
, nvroot
);
2629 fatal(0, "spa_vdev_add(%p) = %d", nvroot
, error
);
2630 nvlist_free(nvroot
);
2633 * Remove an existing device. Sometimes, dirty its
2634 * vdev state first to make sure we handle removal
2635 * of devices that have pending state changes.
2637 if (ztest_random(2) == 0)
2638 (void) vdev_online(spa
, guid
, 0, NULL
);
2640 error
= spa_vdev_remove(spa
, guid
, B_FALSE
);
2641 if (error
!= 0 && error
!= EBUSY
)
2642 fatal(0, "spa_vdev_remove(%llu) = %d", guid
, error
);
2645 VERIFY(mutex_unlock(&ztest_vdev_lock
) == 0);
2649 * split a pool if it has mirror tlvdevs
2653 ztest_split_pool(ztest_ds_t
*zd
, uint64_t id
)
2655 ztest_shared_t
*zs
= ztest_shared
;
2656 spa_t
*spa
= ztest_spa
;
2657 vdev_t
*rvd
= spa
->spa_root_vdev
;
2658 nvlist_t
*tree
, **child
, *config
, *split
, **schild
;
2659 uint_t c
, children
, schildren
= 0, lastlogid
= 0;
2662 VERIFY(mutex_lock(&ztest_vdev_lock
) == 0);
2664 /* ensure we have a useable config; mirrors of raidz aren't supported */
2665 if (zs
->zs_mirrors
< 3 || ztest_opts
.zo_raidz
> 1) {
2666 VERIFY(mutex_unlock(&ztest_vdev_lock
) == 0);
2670 /* clean up the old pool, if any */
2671 (void) spa_destroy("splitp");
2673 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
2675 /* generate a config from the existing config */
2676 mutex_enter(&spa
->spa_props_lock
);
2677 VERIFY(nvlist_lookup_nvlist(spa
->spa_config
, ZPOOL_CONFIG_VDEV_TREE
,
2679 mutex_exit(&spa
->spa_props_lock
);
2681 VERIFY(nvlist_lookup_nvlist_array(tree
, ZPOOL_CONFIG_CHILDREN
, &child
,
2684 schild
= malloc(rvd
->vdev_children
* sizeof (nvlist_t
*));
2685 for (c
= 0; c
< children
; c
++) {
2686 vdev_t
*tvd
= rvd
->vdev_child
[c
];
2690 if (tvd
->vdev_islog
|| tvd
->vdev_ops
== &vdev_hole_ops
) {
2691 VERIFY(nvlist_alloc(&schild
[schildren
], NV_UNIQUE_NAME
,
2693 VERIFY(nvlist_add_string(schild
[schildren
],
2694 ZPOOL_CONFIG_TYPE
, VDEV_TYPE_HOLE
) == 0);
2695 VERIFY(nvlist_add_uint64(schild
[schildren
],
2696 ZPOOL_CONFIG_IS_HOLE
, 1) == 0);
2698 lastlogid
= schildren
;
2703 VERIFY(nvlist_lookup_nvlist_array(child
[c
],
2704 ZPOOL_CONFIG_CHILDREN
, &mchild
, &mchildren
) == 0);
2705 VERIFY(nvlist_dup(mchild
[0], &schild
[schildren
++], 0) == 0);
2708 /* OK, create a config that can be used to split */
2709 VERIFY(nvlist_alloc(&split
, NV_UNIQUE_NAME
, 0) == 0);
2710 VERIFY(nvlist_add_string(split
, ZPOOL_CONFIG_TYPE
,
2711 VDEV_TYPE_ROOT
) == 0);
2712 VERIFY(nvlist_add_nvlist_array(split
, ZPOOL_CONFIG_CHILDREN
, schild
,
2713 lastlogid
!= 0 ? lastlogid
: schildren
) == 0);
2715 VERIFY(nvlist_alloc(&config
, NV_UNIQUE_NAME
, 0) == 0);
2716 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, split
) == 0);
2718 for (c
= 0; c
< schildren
; c
++)
2719 nvlist_free(schild
[c
]);
2723 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
2725 (void) rw_wrlock(&ztest_name_lock
);
2726 error
= spa_vdev_split_mirror(spa
, "splitp", config
, NULL
, B_FALSE
);
2727 (void) rw_unlock(&ztest_name_lock
);
2729 nvlist_free(config
);
2732 (void) printf("successful split - results:\n");
2733 mutex_enter(&spa_namespace_lock
);
2734 show_pool_stats(spa
);
2735 show_pool_stats(spa_lookup("splitp"));
2736 mutex_exit(&spa_namespace_lock
);
2740 VERIFY(mutex_unlock(&ztest_vdev_lock
) == 0);
2745 * Verify that we can attach and detach devices.
2749 ztest_vdev_attach_detach(ztest_ds_t
*zd
, uint64_t id
)
2751 ztest_shared_t
*zs
= ztest_shared
;
2752 spa_t
*spa
= ztest_spa
;
2753 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
2754 vdev_t
*rvd
= spa
->spa_root_vdev
;
2755 vdev_t
*oldvd
, *newvd
, *pvd
;
2759 uint64_t ashift
= ztest_get_ashift();
2760 uint64_t oldguid
, pguid
;
2761 uint64_t oldsize
, newsize
;
2762 char oldpath
[MAXPATHLEN
], newpath
[MAXPATHLEN
];
2764 int oldvd_has_siblings
= B_FALSE
;
2765 int newvd_is_spare
= B_FALSE
;
2767 int error
, expected_error
;
2769 VERIFY(mutex_lock(&ztest_vdev_lock
) == 0);
2770 leaves
= MAX(zs
->zs_mirrors
, 1) * ztest_opts
.zo_raidz
;
2772 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
2775 * Decide whether to do an attach or a replace.
2777 replacing
= ztest_random(2);
2780 * Pick a random top-level vdev.
2782 top
= ztest_random_vdev_top(spa
, B_TRUE
);
2785 * Pick a random leaf within it.
2787 leaf
= ztest_random(leaves
);
2792 oldvd
= rvd
->vdev_child
[top
];
2793 if (zs
->zs_mirrors
>= 1) {
2794 ASSERT(oldvd
->vdev_ops
== &vdev_mirror_ops
);
2795 ASSERT(oldvd
->vdev_children
>= zs
->zs_mirrors
);
2796 oldvd
= oldvd
->vdev_child
[leaf
/ ztest_opts
.zo_raidz
];
2798 if (ztest_opts
.zo_raidz
> 1) {
2799 ASSERT(oldvd
->vdev_ops
== &vdev_raidz_ops
);
2800 ASSERT(oldvd
->vdev_children
== ztest_opts
.zo_raidz
);
2801 oldvd
= oldvd
->vdev_child
[leaf
% ztest_opts
.zo_raidz
];
2805 * If we're already doing an attach or replace, oldvd may be a
2806 * mirror vdev -- in which case, pick a random child.
2808 while (oldvd
->vdev_children
!= 0) {
2809 oldvd_has_siblings
= B_TRUE
;
2810 ASSERT(oldvd
->vdev_children
>= 2);
2811 oldvd
= oldvd
->vdev_child
[ztest_random(oldvd
->vdev_children
)];
2814 oldguid
= oldvd
->vdev_guid
;
2815 oldsize
= vdev_get_min_asize(oldvd
);
2816 oldvd_is_log
= oldvd
->vdev_top
->vdev_islog
;
2817 (void) strcpy(oldpath
, oldvd
->vdev_path
);
2818 pvd
= oldvd
->vdev_parent
;
2819 pguid
= pvd
->vdev_guid
;
2822 * If oldvd has siblings, then half of the time, detach it.
2824 if (oldvd_has_siblings
&& ztest_random(2) == 0) {
2825 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
2826 error
= spa_vdev_detach(spa
, oldguid
, pguid
, B_FALSE
);
2827 if (error
!= 0 && error
!= ENODEV
&& error
!= EBUSY
&&
2829 fatal(0, "detach (%s) returned %d", oldpath
, error
);
2830 VERIFY(mutex_unlock(&ztest_vdev_lock
) == 0);
2835 * For the new vdev, choose with equal probability between the two
2836 * standard paths (ending in either 'a' or 'b') or a random hot spare.
2838 if (sav
->sav_count
!= 0 && ztest_random(3) == 0) {
2839 newvd
= sav
->sav_vdevs
[ztest_random(sav
->sav_count
)];
2840 newvd_is_spare
= B_TRUE
;
2841 (void) strcpy(newpath
, newvd
->vdev_path
);
2843 (void) snprintf(newpath
, sizeof (newpath
), ztest_dev_template
,
2844 ztest_opts
.zo_dir
, ztest_opts
.zo_pool
,
2845 top
* leaves
+ leaf
);
2846 if (ztest_random(2) == 0)
2847 newpath
[strlen(newpath
) - 1] = 'b';
2848 newvd
= vdev_lookup_by_path(rvd
, newpath
);
2852 newsize
= vdev_get_min_asize(newvd
);
2855 * Make newsize a little bigger or smaller than oldsize.
2856 * If it's smaller, the attach should fail.
2857 * If it's larger, and we're doing a replace,
2858 * we should get dynamic LUN growth when we're done.
2860 newsize
= 10 * oldsize
/ (9 + ztest_random(3));
2864 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
2865 * unless it's a replace; in that case any non-replacing parent is OK.
2867 * If newvd is already part of the pool, it should fail with EBUSY.
2869 * If newvd is too small, it should fail with EOVERFLOW.
2871 if (pvd
->vdev_ops
!= &vdev_mirror_ops
&&
2872 pvd
->vdev_ops
!= &vdev_root_ops
&& (!replacing
||
2873 pvd
->vdev_ops
== &vdev_replacing_ops
||
2874 pvd
->vdev_ops
== &vdev_spare_ops
))
2875 expected_error
= ENOTSUP
;
2876 else if (newvd_is_spare
&& (!replacing
|| oldvd_is_log
))
2877 expected_error
= ENOTSUP
;
2878 else if (newvd
== oldvd
)
2879 expected_error
= replacing
? 0 : EBUSY
;
2880 else if (vdev_lookup_by_path(rvd
, newpath
) != NULL
)
2881 expected_error
= EBUSY
;
2882 else if (newsize
< oldsize
)
2883 expected_error
= EOVERFLOW
;
2884 else if (ashift
> oldvd
->vdev_top
->vdev_ashift
)
2885 expected_error
= EDOM
;
2889 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
2892 * Build the nvlist describing newpath.
2894 root
= make_vdev_root(newpath
, NULL
, NULL
, newvd
== NULL
? newsize
: 0,
2895 ashift
, 0, 0, 0, 1);
2897 error
= spa_vdev_attach(spa
, oldguid
, root
, replacing
);
2902 * If our parent was the replacing vdev, but the replace completed,
2903 * then instead of failing with ENOTSUP we may either succeed,
2904 * fail with ENODEV, or fail with EOVERFLOW.
2906 if (expected_error
== ENOTSUP
&&
2907 (error
== 0 || error
== ENODEV
|| error
== EOVERFLOW
))
2908 expected_error
= error
;
2911 * If someone grew the LUN, the replacement may be too small.
2913 if (error
== EOVERFLOW
|| error
== EBUSY
)
2914 expected_error
= error
;
2916 /* XXX workaround 6690467 */
2917 if (error
!= expected_error
&& expected_error
!= EBUSY
) {
2918 fatal(0, "attach (%s %llu, %s %llu, %d) "
2919 "returned %d, expected %d",
2920 oldpath
, oldsize
, newpath
,
2921 newsize
, replacing
, error
, expected_error
);
2924 VERIFY(mutex_unlock(&ztest_vdev_lock
) == 0);
2928 * Callback function which expands the physical size of the vdev.
2931 grow_vdev(vdev_t
*vd
, void *arg
)
2933 spa_t
*spa
= vd
->vdev_spa
;
2934 size_t *newsize
= arg
;
2938 ASSERT(spa_config_held(spa
, SCL_STATE
, RW_READER
) == SCL_STATE
);
2939 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
2941 if ((fd
= open(vd
->vdev_path
, O_RDWR
)) == -1)
2944 fsize
= lseek(fd
, 0, SEEK_END
);
2945 (void) ftruncate(fd
, *newsize
);
2947 if (ztest_opts
.zo_verbose
>= 6) {
2948 (void) printf("%s grew from %lu to %lu bytes\n",
2949 vd
->vdev_path
, (ulong_t
)fsize
, (ulong_t
)*newsize
);
2956 * Callback function which expands a given vdev by calling vdev_online().
2960 online_vdev(vdev_t
*vd
, void *arg
)
2962 spa_t
*spa
= vd
->vdev_spa
;
2963 vdev_t
*tvd
= vd
->vdev_top
;
2964 uint64_t guid
= vd
->vdev_guid
;
2965 uint64_t generation
= spa
->spa_config_generation
+ 1;
2966 vdev_state_t newstate
= VDEV_STATE_UNKNOWN
;
2969 ASSERT(spa_config_held(spa
, SCL_STATE
, RW_READER
) == SCL_STATE
);
2970 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
2972 /* Calling vdev_online will initialize the new metaslabs */
2973 spa_config_exit(spa
, SCL_STATE
, spa
);
2974 error
= vdev_online(spa
, guid
, ZFS_ONLINE_EXPAND
, &newstate
);
2975 spa_config_enter(spa
, SCL_STATE
, spa
, RW_READER
);
2978 * If vdev_online returned an error or the underlying vdev_open
2979 * failed then we abort the expand. The only way to know that
2980 * vdev_open fails is by checking the returned newstate.
2982 if (error
|| newstate
!= VDEV_STATE_HEALTHY
) {
2983 if (ztest_opts
.zo_verbose
>= 5) {
2984 (void) printf("Unable to expand vdev, state %llu, "
2985 "error %d\n", (u_longlong_t
)newstate
, error
);
2989 ASSERT3U(newstate
, ==, VDEV_STATE_HEALTHY
);
2992 * Since we dropped the lock we need to ensure that we're
2993 * still talking to the original vdev. It's possible this
2994 * vdev may have been detached/replaced while we were
2995 * trying to online it.
2997 if (generation
!= spa
->spa_config_generation
) {
2998 if (ztest_opts
.zo_verbose
>= 5) {
2999 (void) printf("vdev configuration has changed, "
3000 "guid %llu, state %llu, expected gen %llu, "
3003 (u_longlong_t
)tvd
->vdev_state
,
3004 (u_longlong_t
)generation
,
3005 (u_longlong_t
)spa
->spa_config_generation
);
3013 * Traverse the vdev tree calling the supplied function.
3014 * We continue to walk the tree until we either have walked all
3015 * children or we receive a non-NULL return from the callback.
3016 * If a NULL callback is passed, then we just return back the first
3017 * leaf vdev we encounter.
3020 vdev_walk_tree(vdev_t
*vd
, vdev_t
*(*func
)(vdev_t
*, void *), void *arg
)
3022 if (vd
->vdev_ops
->vdev_op_leaf
) {
3026 return (func(vd
, arg
));
3029 for (uint_t c
= 0; c
< vd
->vdev_children
; c
++) {
3030 vdev_t
*cvd
= vd
->vdev_child
[c
];
3031 if ((cvd
= vdev_walk_tree(cvd
, func
, arg
)) != NULL
)
3038 * Verify that dynamic LUN growth works as expected.
3042 ztest_vdev_LUN_growth(ztest_ds_t
*zd
, uint64_t id
)
3044 spa_t
*spa
= ztest_spa
;
3046 metaslab_class_t
*mc
;
3047 metaslab_group_t
*mg
;
3048 size_t psize
, newsize
;
3050 uint64_t old_class_space
, new_class_space
, old_ms_count
, new_ms_count
;
3052 VERIFY(mutex_lock(&ztest_vdev_lock
) == 0);
3053 spa_config_enter(spa
, SCL_STATE
, spa
, RW_READER
);
3055 top
= ztest_random_vdev_top(spa
, B_TRUE
);
3057 tvd
= spa
->spa_root_vdev
->vdev_child
[top
];
3060 old_ms_count
= tvd
->vdev_ms_count
;
3061 old_class_space
= metaslab_class_get_space(mc
);
3064 * Determine the size of the first leaf vdev associated with
3065 * our top-level device.
3067 vd
= vdev_walk_tree(tvd
, NULL
, NULL
);
3068 ASSERT3P(vd
, !=, NULL
);
3069 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
3071 psize
= vd
->vdev_psize
;
3074 * We only try to expand the vdev if it's healthy, less than 4x its
3075 * original size, and it has a valid psize.
3077 if (tvd
->vdev_state
!= VDEV_STATE_HEALTHY
||
3078 psize
== 0 || psize
>= 4 * ztest_opts
.zo_vdev_size
) {
3079 spa_config_exit(spa
, SCL_STATE
, spa
);
3080 VERIFY(mutex_unlock(&ztest_vdev_lock
) == 0);
3084 newsize
= psize
+ psize
/ 8;
3085 ASSERT3U(newsize
, >, psize
);
3087 if (ztest_opts
.zo_verbose
>= 6) {
3088 (void) printf("Expanding LUN %s from %lu to %lu\n",
3089 vd
->vdev_path
, (ulong_t
)psize
, (ulong_t
)newsize
);
3093 * Growing the vdev is a two step process:
3094 * 1). expand the physical size (i.e. relabel)
3095 * 2). online the vdev to create the new metaslabs
3097 if (vdev_walk_tree(tvd
, grow_vdev
, &newsize
) != NULL
||
3098 vdev_walk_tree(tvd
, online_vdev
, NULL
) != NULL
||
3099 tvd
->vdev_state
!= VDEV_STATE_HEALTHY
) {
3100 if (ztest_opts
.zo_verbose
>= 5) {
3101 (void) printf("Could not expand LUN because "
3102 "the vdev configuration changed.\n");
3104 spa_config_exit(spa
, SCL_STATE
, spa
);
3105 VERIFY(mutex_unlock(&ztest_vdev_lock
) == 0);
3109 spa_config_exit(spa
, SCL_STATE
, spa
);
3112 * Expanding the LUN will update the config asynchronously,
3113 * thus we must wait for the async thread to complete any
3114 * pending tasks before proceeding.
3118 mutex_enter(&spa
->spa_async_lock
);
3119 done
= (spa
->spa_async_thread
== NULL
&& !spa
->spa_async_tasks
);
3120 mutex_exit(&spa
->spa_async_lock
);
3123 txg_wait_synced(spa_get_dsl(spa
), 0);
3124 (void) poll(NULL
, 0, 100);
3127 spa_config_enter(spa
, SCL_STATE
, spa
, RW_READER
);
3129 tvd
= spa
->spa_root_vdev
->vdev_child
[top
];
3130 new_ms_count
= tvd
->vdev_ms_count
;
3131 new_class_space
= metaslab_class_get_space(mc
);
3133 if (tvd
->vdev_mg
!= mg
|| mg
->mg_class
!= mc
) {
3134 if (ztest_opts
.zo_verbose
>= 5) {
3135 (void) printf("Could not verify LUN expansion due to "
3136 "intervening vdev offline or remove.\n");
3138 spa_config_exit(spa
, SCL_STATE
, spa
);
3139 VERIFY(mutex_unlock(&ztest_vdev_lock
) == 0);
3144 * Make sure we were able to grow the vdev.
3146 if (new_ms_count
<= old_ms_count
)
3147 fatal(0, "LUN expansion failed: ms_count %llu <= %llu\n",
3148 old_ms_count
, new_ms_count
);
3151 * Make sure we were able to grow the pool.
3153 if (new_class_space
<= old_class_space
)
3154 fatal(0, "LUN expansion failed: class_space %llu <= %llu\n",
3155 old_class_space
, new_class_space
);
3157 if (ztest_opts
.zo_verbose
>= 5) {
3158 char oldnumbuf
[6], newnumbuf
[6];
3160 nicenum(old_class_space
, oldnumbuf
);
3161 nicenum(new_class_space
, newnumbuf
);
3162 (void) printf("%s grew from %s to %s\n",
3163 spa
->spa_name
, oldnumbuf
, newnumbuf
);
3166 spa_config_exit(spa
, SCL_STATE
, spa
);
3167 VERIFY(mutex_unlock(&ztest_vdev_lock
) == 0);
3171 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
3175 ztest_objset_create_cb(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
)
3178 * Create the objects common to all ztest datasets.
3180 VERIFY(zap_create_claim(os
, ZTEST_DIROBJ
,
3181 DMU_OT_ZAP_OTHER
, DMU_OT_NONE
, 0, tx
) == 0);
3185 ztest_dataset_create(char *dsname
)
3187 uint64_t zilset
= ztest_random(100);
3188 int err
= dmu_objset_create(dsname
, DMU_OST_OTHER
, 0,
3189 ztest_objset_create_cb
, NULL
);
3191 if (err
|| zilset
< 80)
3194 if (ztest_opts
.zo_verbose
>= 6)
3195 (void) printf("Setting dataset %s to sync always\n", dsname
);
3196 return (ztest_dsl_prop_set_uint64(dsname
, ZFS_PROP_SYNC
,
3197 ZFS_SYNC_ALWAYS
, B_FALSE
));
3202 ztest_objset_destroy_cb(const char *name
, void *arg
)
3205 dmu_object_info_t doi
;
3209 * Verify that the dataset contains a directory object.
3211 VERIFY0(dmu_objset_own(name
, DMU_OST_OTHER
, B_TRUE
, FTAG
, &os
));
3212 error
= dmu_object_info(os
, ZTEST_DIROBJ
, &doi
);
3213 if (error
!= ENOENT
) {
3214 /* We could have crashed in the middle of destroying it */
3216 ASSERT3U(doi
.doi_type
, ==, DMU_OT_ZAP_OTHER
);
3217 ASSERT3S(doi
.doi_physical_blocks_512
, >=, 0);
3219 dmu_objset_disown(os
, FTAG
);
3222 * Destroy the dataset.
3224 if (strchr(name
, '@') != NULL
) {
3225 VERIFY0(dsl_destroy_snapshot(name
, B_TRUE
));
3227 error
= dsl_destroy_head(name
);
3228 /* There could be a hold on this dataset */
3236 ztest_snapshot_create(char *osname
, uint64_t id
)
3238 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
3241 (void) snprintf(snapname
, sizeof (snapname
), "%llu", (u_longlong_t
)id
);
3243 error
= dmu_objset_snapshot_one(osname
, snapname
);
3244 if (error
== ENOSPC
) {
3245 ztest_record_enospc(FTAG
);
3248 if (error
!= 0 && error
!= EEXIST
) {
3249 fatal(0, "ztest_snapshot_create(%s@%s) = %d", osname
,
3256 ztest_snapshot_destroy(char *osname
, uint64_t id
)
3258 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
3261 (void) snprintf(snapname
, sizeof (snapname
), "%s@%llu", osname
,
3264 error
= dsl_destroy_snapshot(snapname
, B_FALSE
);
3265 if (error
!= 0 && error
!= ENOENT
)
3266 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname
, error
);
3272 ztest_dmu_objset_create_destroy(ztest_ds_t
*zd
, uint64_t id
)
3278 char name
[ZFS_MAX_DATASET_NAME_LEN
];
3281 (void) rw_rdlock(&ztest_name_lock
);
3283 (void) snprintf(name
, sizeof (name
), "%s/temp_%llu",
3284 ztest_opts
.zo_pool
, (u_longlong_t
)id
);
3287 * If this dataset exists from a previous run, process its replay log
3288 * half of the time. If we don't replay it, then dmu_objset_destroy()
3289 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
3291 if (ztest_random(2) == 0 &&
3292 dmu_objset_own(name
, DMU_OST_OTHER
, B_FALSE
, FTAG
, &os
) == 0) {
3293 ztest_zd_init(&zdtmp
, NULL
, os
);
3294 zil_replay(os
, &zdtmp
, ztest_replay_vector
);
3295 ztest_zd_fini(&zdtmp
);
3296 dmu_objset_disown(os
, FTAG
);
3300 * There may be an old instance of the dataset we're about to
3301 * create lying around from a previous run. If so, destroy it
3302 * and all of its snapshots.
3304 (void) dmu_objset_find(name
, ztest_objset_destroy_cb
, NULL
,
3305 DS_FIND_CHILDREN
| DS_FIND_SNAPSHOTS
);
3308 * Verify that the destroyed dataset is no longer in the namespace.
3310 VERIFY3U(ENOENT
, ==, dmu_objset_own(name
, DMU_OST_OTHER
, B_TRUE
,
3314 * Verify that we can create a new dataset.
3316 error
= ztest_dataset_create(name
);
3318 if (error
== ENOSPC
) {
3319 ztest_record_enospc(FTAG
);
3320 (void) rw_unlock(&ztest_name_lock
);
3323 fatal(0, "dmu_objset_create(%s) = %d", name
, error
);
3326 VERIFY0(dmu_objset_own(name
, DMU_OST_OTHER
, B_FALSE
, FTAG
, &os
));
3328 ztest_zd_init(&zdtmp
, NULL
, os
);
3331 * Open the intent log for it.
3333 zilog
= zil_open(os
, ztest_get_data
);
3336 * Put some objects in there, do a little I/O to them,
3337 * and randomly take a couple of snapshots along the way.
3339 iters
= ztest_random(5);
3340 for (int i
= 0; i
< iters
; i
++) {
3341 ztest_dmu_object_alloc_free(&zdtmp
, id
);
3342 if (ztest_random(iters
) == 0)
3343 (void) ztest_snapshot_create(name
, i
);
3347 * Verify that we cannot create an existing dataset.
3349 VERIFY3U(EEXIST
, ==,
3350 dmu_objset_create(name
, DMU_OST_OTHER
, 0, NULL
, NULL
));
3353 * Verify that we can hold an objset that is also owned.
3355 VERIFY3U(0, ==, dmu_objset_hold(name
, FTAG
, &os2
));
3356 dmu_objset_rele(os2
, FTAG
);
3359 * Verify that we cannot own an objset that is already owned.
3362 dmu_objset_own(name
, DMU_OST_OTHER
, B_FALSE
, FTAG
, &os2
));
3365 dmu_objset_disown(os
, FTAG
);
3366 ztest_zd_fini(&zdtmp
);
3368 (void) rw_unlock(&ztest_name_lock
);
3372 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3375 ztest_dmu_snapshot_create_destroy(ztest_ds_t
*zd
, uint64_t id
)
3377 (void) rw_rdlock(&ztest_name_lock
);
3378 (void) ztest_snapshot_destroy(zd
->zd_name
, id
);
3379 (void) ztest_snapshot_create(zd
->zd_name
, id
);
3380 (void) rw_unlock(&ztest_name_lock
);
3384 * Cleanup non-standard snapshots and clones.
3387 ztest_dsl_dataset_cleanup(char *osname
, uint64_t id
)
3389 char snap1name
[ZFS_MAX_DATASET_NAME_LEN
];
3390 char clone1name
[ZFS_MAX_DATASET_NAME_LEN
];
3391 char snap2name
[ZFS_MAX_DATASET_NAME_LEN
];
3392 char clone2name
[ZFS_MAX_DATASET_NAME_LEN
];
3393 char snap3name
[ZFS_MAX_DATASET_NAME_LEN
];
3396 (void) snprintf(snap1name
, sizeof (snap1name
),
3397 "%s@s1_%llu", osname
, id
);
3398 (void) snprintf(clone1name
, sizeof (clone1name
),
3399 "%s/c1_%llu", osname
, id
);
3400 (void) snprintf(snap2name
, sizeof (snap2name
),
3401 "%s@s2_%llu", clone1name
, id
);
3402 (void) snprintf(clone2name
, sizeof (clone2name
),
3403 "%s/c2_%llu", osname
, id
);
3404 (void) snprintf(snap3name
, sizeof (snap3name
),
3405 "%s@s3_%llu", clone1name
, id
);
3407 error
= dsl_destroy_head(clone2name
);
3408 if (error
&& error
!= ENOENT
)
3409 fatal(0, "dsl_destroy_head(%s) = %d", clone2name
, error
);
3410 error
= dsl_destroy_snapshot(snap3name
, B_FALSE
);
3411 if (error
&& error
!= ENOENT
)
3412 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap3name
, error
);
3413 error
= dsl_destroy_snapshot(snap2name
, B_FALSE
);
3414 if (error
&& error
!= ENOENT
)
3415 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap2name
, error
);
3416 error
= dsl_destroy_head(clone1name
);
3417 if (error
&& error
!= ENOENT
)
3418 fatal(0, "dsl_destroy_head(%s) = %d", clone1name
, error
);
3419 error
= dsl_destroy_snapshot(snap1name
, B_FALSE
);
3420 if (error
&& error
!= ENOENT
)
3421 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap1name
, error
);
3425 * Verify dsl_dataset_promote handles EBUSY
3428 ztest_dsl_dataset_promote_busy(ztest_ds_t
*zd
, uint64_t id
)
3431 char snap1name
[ZFS_MAX_DATASET_NAME_LEN
];
3432 char clone1name
[ZFS_MAX_DATASET_NAME_LEN
];
3433 char snap2name
[ZFS_MAX_DATASET_NAME_LEN
];
3434 char clone2name
[ZFS_MAX_DATASET_NAME_LEN
];
3435 char snap3name
[ZFS_MAX_DATASET_NAME_LEN
];
3436 char *osname
= zd
->zd_name
;
3439 (void) rw_rdlock(&ztest_name_lock
);
3441 ztest_dsl_dataset_cleanup(osname
, id
);
3443 (void) snprintf(snap1name
, sizeof (snap1name
),
3444 "%s@s1_%llu", osname
, id
);
3445 (void) snprintf(clone1name
, sizeof (clone1name
),
3446 "%s/c1_%llu", osname
, id
);
3447 (void) snprintf(snap2name
, sizeof (snap2name
),
3448 "%s@s2_%llu", clone1name
, id
);
3449 (void) snprintf(clone2name
, sizeof (clone2name
),
3450 "%s/c2_%llu", osname
, id
);
3451 (void) snprintf(snap3name
, sizeof (snap3name
),
3452 "%s@s3_%llu", clone1name
, id
);
3454 error
= dmu_objset_snapshot_one(osname
, strchr(snap1name
, '@') + 1);
3455 if (error
&& error
!= EEXIST
) {
3456 if (error
== ENOSPC
) {
3457 ztest_record_enospc(FTAG
);
3460 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name
, error
);
3463 error
= dmu_objset_clone(clone1name
, snap1name
);
3465 if (error
== ENOSPC
) {
3466 ztest_record_enospc(FTAG
);
3469 fatal(0, "dmu_objset_create(%s) = %d", clone1name
, error
);
3472 error
= dmu_objset_snapshot_one(clone1name
, strchr(snap2name
, '@') + 1);
3473 if (error
&& error
!= EEXIST
) {
3474 if (error
== ENOSPC
) {
3475 ztest_record_enospc(FTAG
);
3478 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name
, error
);
3481 error
= dmu_objset_snapshot_one(clone1name
, strchr(snap3name
, '@') + 1);
3482 if (error
&& error
!= EEXIST
) {
3483 if (error
== ENOSPC
) {
3484 ztest_record_enospc(FTAG
);
3487 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name
, error
);
3490 error
= dmu_objset_clone(clone2name
, snap3name
);
3492 if (error
== ENOSPC
) {
3493 ztest_record_enospc(FTAG
);
3496 fatal(0, "dmu_objset_create(%s) = %d", clone2name
, error
);
3499 error
= dmu_objset_own(snap2name
, DMU_OST_ANY
, B_TRUE
, FTAG
, &os
);
3501 fatal(0, "dmu_objset_own(%s) = %d", snap2name
, error
);
3502 error
= dsl_dataset_promote(clone2name
, NULL
);
3503 if (error
== ENOSPC
) {
3504 dmu_objset_disown(os
, FTAG
);
3505 ztest_record_enospc(FTAG
);
3509 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name
,
3511 dmu_objset_disown(os
, FTAG
);
3514 ztest_dsl_dataset_cleanup(osname
, id
);
3516 (void) rw_unlock(&ztest_name_lock
);
3520 * Verify that dmu_object_{alloc,free} work as expected.
3523 ztest_dmu_object_alloc_free(ztest_ds_t
*zd
, uint64_t id
)
3526 int batchsize
= sizeof (od
) / sizeof (od
[0]);
3528 for (int b
= 0; b
< batchsize
; b
++)
3529 ztest_od_init(&od
[b
], id
, FTAG
, b
, DMU_OT_UINT64_OTHER
, 0, 0);
3532 * Destroy the previous batch of objects, create a new batch,
3533 * and do some I/O on the new objects.
3535 if (ztest_object_init(zd
, od
, sizeof (od
), B_TRUE
) != 0)
3538 while (ztest_random(4 * batchsize
) != 0)
3539 ztest_io(zd
, od
[ztest_random(batchsize
)].od_object
,
3540 ztest_random(ZTEST_RANGE_LOCKS
) << SPA_MAXBLOCKSHIFT
);
3544 * Verify that dmu_{read,write} work as expected.
3547 ztest_dmu_read_write(ztest_ds_t
*zd
, uint64_t id
)
3549 objset_t
*os
= zd
->zd_os
;
3552 int i
, freeit
, error
;
3554 bufwad_t
*packbuf
, *bigbuf
, *pack
, *bigH
, *bigT
;
3555 uint64_t packobj
, packoff
, packsize
, bigobj
, bigoff
, bigsize
;
3556 uint64_t chunksize
= (1000 + ztest_random(1000)) * sizeof (uint64_t);
3557 uint64_t regions
= 997;
3558 uint64_t stride
= 123456789ULL;
3559 uint64_t width
= 40;
3560 int free_percent
= 5;
3563 * This test uses two objects, packobj and bigobj, that are always
3564 * updated together (i.e. in the same tx) so that their contents are
3565 * in sync and can be compared. Their contents relate to each other
3566 * in a simple way: packobj is a dense array of 'bufwad' structures,
3567 * while bigobj is a sparse array of the same bufwads. Specifically,
3568 * for any index n, there are three bufwads that should be identical:
3570 * packobj, at offset n * sizeof (bufwad_t)
3571 * bigobj, at the head of the nth chunk
3572 * bigobj, at the tail of the nth chunk
3574 * The chunk size is arbitrary. It doesn't have to be a power of two,
3575 * and it doesn't have any relation to the object blocksize.
3576 * The only requirement is that it can hold at least two bufwads.
3578 * Normally, we write the bufwad to each of these locations.
3579 * However, free_percent of the time we instead write zeroes to
3580 * packobj and perform a dmu_free_range() on bigobj. By comparing
3581 * bigobj to packobj, we can verify that the DMU is correctly
3582 * tracking which parts of an object are allocated and free,
3583 * and that the contents of the allocated blocks are correct.
3587 * Read the directory info. If it's the first time, set things up.
3589 ztest_od_init(&od
[0], id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, 0, chunksize
);
3590 ztest_od_init(&od
[1], id
, FTAG
, 1, DMU_OT_UINT64_OTHER
, 0, chunksize
);
3592 if (ztest_object_init(zd
, od
, sizeof (od
), B_FALSE
) != 0)
3595 bigobj
= od
[0].od_object
;
3596 packobj
= od
[1].od_object
;
3597 chunksize
= od
[0].od_gen
;
3598 ASSERT(chunksize
== od
[1].od_gen
);
3601 * Prefetch a random chunk of the big object.
3602 * Our aim here is to get some async reads in flight
3603 * for blocks that we may free below; the DMU should
3604 * handle this race correctly.
3606 n
= ztest_random(regions
) * stride
+ ztest_random(width
);
3607 s
= 1 + ztest_random(2 * width
- 1);
3608 dmu_prefetch(os
, bigobj
, 0, n
* chunksize
, s
* chunksize
,
3609 ZIO_PRIORITY_SYNC_READ
);
3612 * Pick a random index and compute the offsets into packobj and bigobj.
3614 n
= ztest_random(regions
) * stride
+ ztest_random(width
);
3615 s
= 1 + ztest_random(width
- 1);
3617 packoff
= n
* sizeof (bufwad_t
);
3618 packsize
= s
* sizeof (bufwad_t
);
3620 bigoff
= n
* chunksize
;
3621 bigsize
= s
* chunksize
;
3623 packbuf
= umem_alloc(packsize
, UMEM_NOFAIL
);
3624 bigbuf
= umem_alloc(bigsize
, UMEM_NOFAIL
);
3627 * free_percent of the time, free a range of bigobj rather than
3630 freeit
= (ztest_random(100) < free_percent
);
3633 * Read the current contents of our objects.
3635 error
= dmu_read(os
, packobj
, packoff
, packsize
, packbuf
,
3638 error
= dmu_read(os
, bigobj
, bigoff
, bigsize
, bigbuf
,
3643 * Get a tx for the mods to both packobj and bigobj.
3645 tx
= dmu_tx_create(os
);
3647 dmu_tx_hold_write(tx
, packobj
, packoff
, packsize
);
3650 dmu_tx_hold_free(tx
, bigobj
, bigoff
, bigsize
);
3652 dmu_tx_hold_write(tx
, bigobj
, bigoff
, bigsize
);
3654 /* This accounts for setting the checksum/compression. */
3655 dmu_tx_hold_bonus(tx
, bigobj
);
3657 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
3659 umem_free(packbuf
, packsize
);
3660 umem_free(bigbuf
, bigsize
);
3664 enum zio_checksum cksum
;
3666 cksum
= (enum zio_checksum
)
3667 ztest_random_dsl_prop(ZFS_PROP_CHECKSUM
);
3668 } while (cksum
>= ZIO_CHECKSUM_LEGACY_FUNCTIONS
);
3669 dmu_object_set_checksum(os
, bigobj
, cksum
, tx
);
3671 enum zio_compress comp
;
3673 comp
= (enum zio_compress
)
3674 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION
);
3675 } while (comp
>= ZIO_COMPRESS_LEGACY_FUNCTIONS
);
3676 dmu_object_set_compress(os
, bigobj
, comp
, tx
);
3679 * For each index from n to n + s, verify that the existing bufwad
3680 * in packobj matches the bufwads at the head and tail of the
3681 * corresponding chunk in bigobj. Then update all three bufwads
3682 * with the new values we want to write out.
3684 for (i
= 0; i
< s
; i
++) {
3686 pack
= (bufwad_t
*)((char *)packbuf
+ i
* sizeof (bufwad_t
));
3688 bigH
= (bufwad_t
*)((char *)bigbuf
+ i
* chunksize
);
3690 bigT
= (bufwad_t
*)((char *)bigH
+ chunksize
) - 1;
3692 ASSERT((uintptr_t)bigH
- (uintptr_t)bigbuf
< bigsize
);
3693 ASSERT((uintptr_t)bigT
- (uintptr_t)bigbuf
< bigsize
);
3695 if (pack
->bw_txg
> txg
)
3696 fatal(0, "future leak: got %llx, open txg is %llx",
3699 if (pack
->bw_data
!= 0 && pack
->bw_index
!= n
+ i
)
3700 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3701 pack
->bw_index
, n
, i
);
3703 if (bcmp(pack
, bigH
, sizeof (bufwad_t
)) != 0)
3704 fatal(0, "pack/bigH mismatch in %p/%p", pack
, bigH
);
3706 if (bcmp(pack
, bigT
, sizeof (bufwad_t
)) != 0)
3707 fatal(0, "pack/bigT mismatch in %p/%p", pack
, bigT
);
3710 bzero(pack
, sizeof (bufwad_t
));
3712 pack
->bw_index
= n
+ i
;
3714 pack
->bw_data
= 1 + ztest_random(-2ULL);
3721 * We've verified all the old bufwads, and made new ones.
3722 * Now write them out.
3724 dmu_write(os
, packobj
, packoff
, packsize
, packbuf
, tx
);
3727 if (ztest_opts
.zo_verbose
>= 7) {
3728 (void) printf("freeing offset %llx size %llx"
3730 (u_longlong_t
)bigoff
,
3731 (u_longlong_t
)bigsize
,
3734 VERIFY(0 == dmu_free_range(os
, bigobj
, bigoff
, bigsize
, tx
));
3736 if (ztest_opts
.zo_verbose
>= 7) {
3737 (void) printf("writing offset %llx size %llx"
3739 (u_longlong_t
)bigoff
,
3740 (u_longlong_t
)bigsize
,
3743 dmu_write(os
, bigobj
, bigoff
, bigsize
, bigbuf
, tx
);
3749 * Sanity check the stuff we just wrote.
3752 void *packcheck
= umem_alloc(packsize
, UMEM_NOFAIL
);
3753 void *bigcheck
= umem_alloc(bigsize
, UMEM_NOFAIL
);
3755 VERIFY(0 == dmu_read(os
, packobj
, packoff
,
3756 packsize
, packcheck
, DMU_READ_PREFETCH
));
3757 VERIFY(0 == dmu_read(os
, bigobj
, bigoff
,
3758 bigsize
, bigcheck
, DMU_READ_PREFETCH
));
3760 ASSERT(bcmp(packbuf
, packcheck
, packsize
) == 0);
3761 ASSERT(bcmp(bigbuf
, bigcheck
, bigsize
) == 0);
3763 umem_free(packcheck
, packsize
);
3764 umem_free(bigcheck
, bigsize
);
3767 umem_free(packbuf
, packsize
);
3768 umem_free(bigbuf
, bigsize
);
3772 compare_and_update_pbbufs(uint64_t s
, bufwad_t
*packbuf
, bufwad_t
*bigbuf
,
3773 uint64_t bigsize
, uint64_t n
, uint64_t chunksize
, uint64_t txg
)
3781 * For each index from n to n + s, verify that the existing bufwad
3782 * in packobj matches the bufwads at the head and tail of the
3783 * corresponding chunk in bigobj. Then update all three bufwads
3784 * with the new values we want to write out.
3786 for (i
= 0; i
< s
; i
++) {
3788 pack
= (bufwad_t
*)((char *)packbuf
+ i
* sizeof (bufwad_t
));
3790 bigH
= (bufwad_t
*)((char *)bigbuf
+ i
* chunksize
);
3792 bigT
= (bufwad_t
*)((char *)bigH
+ chunksize
) - 1;
3794 ASSERT((uintptr_t)bigH
- (uintptr_t)bigbuf
< bigsize
);
3795 ASSERT((uintptr_t)bigT
- (uintptr_t)bigbuf
< bigsize
);
3797 if (pack
->bw_txg
> txg
)
3798 fatal(0, "future leak: got %llx, open txg is %llx",
3801 if (pack
->bw_data
!= 0 && pack
->bw_index
!= n
+ i
)
3802 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3803 pack
->bw_index
, n
, i
);
3805 if (bcmp(pack
, bigH
, sizeof (bufwad_t
)) != 0)
3806 fatal(0, "pack/bigH mismatch in %p/%p", pack
, bigH
);
3808 if (bcmp(pack
, bigT
, sizeof (bufwad_t
)) != 0)
3809 fatal(0, "pack/bigT mismatch in %p/%p", pack
, bigT
);
3811 pack
->bw_index
= n
+ i
;
3813 pack
->bw_data
= 1 + ztest_random(-2ULL);
3821 ztest_dmu_read_write_zcopy(ztest_ds_t
*zd
, uint64_t id
)
3823 objset_t
*os
= zd
->zd_os
;
3829 bufwad_t
*packbuf
, *bigbuf
;
3830 uint64_t packobj
, packoff
, packsize
, bigobj
, bigoff
, bigsize
;
3831 uint64_t blocksize
= ztest_random_blocksize();
3832 uint64_t chunksize
= blocksize
;
3833 uint64_t regions
= 997;
3834 uint64_t stride
= 123456789ULL;
3836 dmu_buf_t
*bonus_db
;
3837 arc_buf_t
**bigbuf_arcbufs
;
3838 dmu_object_info_t doi
;
3841 * This test uses two objects, packobj and bigobj, that are always
3842 * updated together (i.e. in the same tx) so that their contents are
3843 * in sync and can be compared. Their contents relate to each other
3844 * in a simple way: packobj is a dense array of 'bufwad' structures,
3845 * while bigobj is a sparse array of the same bufwads. Specifically,
3846 * for any index n, there are three bufwads that should be identical:
3848 * packobj, at offset n * sizeof (bufwad_t)
3849 * bigobj, at the head of the nth chunk
3850 * bigobj, at the tail of the nth chunk
3852 * The chunk size is set equal to bigobj block size so that
3853 * dmu_assign_arcbuf() can be tested for object updates.
3857 * Read the directory info. If it's the first time, set things up.
3859 ztest_od_init(&od
[0], id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, blocksize
, 0);
3860 ztest_od_init(&od
[1], id
, FTAG
, 1, DMU_OT_UINT64_OTHER
, 0, chunksize
);
3862 if (ztest_object_init(zd
, od
, sizeof (od
), B_FALSE
) != 0)
3865 bigobj
= od
[0].od_object
;
3866 packobj
= od
[1].od_object
;
3867 blocksize
= od
[0].od_blocksize
;
3868 chunksize
= blocksize
;
3869 ASSERT(chunksize
== od
[1].od_gen
);
3871 VERIFY(dmu_object_info(os
, bigobj
, &doi
) == 0);
3872 VERIFY(ISP2(doi
.doi_data_block_size
));
3873 VERIFY(chunksize
== doi
.doi_data_block_size
);
3874 VERIFY(chunksize
>= 2 * sizeof (bufwad_t
));
3877 * Pick a random index and compute the offsets into packobj and bigobj.
3879 n
= ztest_random(regions
) * stride
+ ztest_random(width
);
3880 s
= 1 + ztest_random(width
- 1);
3882 packoff
= n
* sizeof (bufwad_t
);
3883 packsize
= s
* sizeof (bufwad_t
);
3885 bigoff
= n
* chunksize
;
3886 bigsize
= s
* chunksize
;
3888 packbuf
= umem_zalloc(packsize
, UMEM_NOFAIL
);
3889 bigbuf
= umem_zalloc(bigsize
, UMEM_NOFAIL
);
3891 VERIFY3U(0, ==, dmu_bonus_hold(os
, bigobj
, FTAG
, &bonus_db
));
3893 bigbuf_arcbufs
= umem_zalloc(2 * s
* sizeof (arc_buf_t
*), UMEM_NOFAIL
);
3896 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
3897 * Iteration 1 test zcopy to already referenced dbufs.
3898 * Iteration 2 test zcopy to dirty dbuf in the same txg.
3899 * Iteration 3 test zcopy to dbuf dirty in previous txg.
3900 * Iteration 4 test zcopy when dbuf is no longer dirty.
3901 * Iteration 5 test zcopy when it can't be done.
3902 * Iteration 6 one more zcopy write.
3904 for (i
= 0; i
< 7; i
++) {
3909 * In iteration 5 (i == 5) use arcbufs
3910 * that don't match bigobj blksz to test
3911 * dmu_assign_arcbuf() when it can't directly
3912 * assign an arcbuf to a dbuf.
3914 for (j
= 0; j
< s
; j
++) {
3915 if (i
!= 5 || chunksize
< (SPA_MINBLOCKSIZE
* 2)) {
3917 dmu_request_arcbuf(bonus_db
, chunksize
);
3919 bigbuf_arcbufs
[2 * j
] =
3920 dmu_request_arcbuf(bonus_db
, chunksize
/ 2);
3921 bigbuf_arcbufs
[2 * j
+ 1] =
3922 dmu_request_arcbuf(bonus_db
, chunksize
/ 2);
3927 * Get a tx for the mods to both packobj and bigobj.
3929 tx
= dmu_tx_create(os
);
3931 dmu_tx_hold_write(tx
, packobj
, packoff
, packsize
);
3932 dmu_tx_hold_write(tx
, bigobj
, bigoff
, bigsize
);
3934 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
3936 umem_free(packbuf
, packsize
);
3937 umem_free(bigbuf
, bigsize
);
3938 for (j
= 0; j
< s
; j
++) {
3940 chunksize
< (SPA_MINBLOCKSIZE
* 2)) {
3941 dmu_return_arcbuf(bigbuf_arcbufs
[j
]);
3944 bigbuf_arcbufs
[2 * j
]);
3946 bigbuf_arcbufs
[2 * j
+ 1]);
3949 umem_free(bigbuf_arcbufs
, 2 * s
* sizeof (arc_buf_t
*));
3950 dmu_buf_rele(bonus_db
, FTAG
);
3955 * 50% of the time don't read objects in the 1st iteration to
3956 * test dmu_assign_arcbuf() for the case when there're no
3957 * existing dbufs for the specified offsets.
3959 if (i
!= 0 || ztest_random(2) != 0) {
3960 error
= dmu_read(os
, packobj
, packoff
,
3961 packsize
, packbuf
, DMU_READ_PREFETCH
);
3963 error
= dmu_read(os
, bigobj
, bigoff
, bigsize
,
3964 bigbuf
, DMU_READ_PREFETCH
);
3967 compare_and_update_pbbufs(s
, packbuf
, bigbuf
, bigsize
,
3971 * We've verified all the old bufwads, and made new ones.
3972 * Now write them out.
3974 dmu_write(os
, packobj
, packoff
, packsize
, packbuf
, tx
);
3975 if (ztest_opts
.zo_verbose
>= 7) {
3976 (void) printf("writing offset %llx size %llx"
3978 (u_longlong_t
)bigoff
,
3979 (u_longlong_t
)bigsize
,
3982 for (off
= bigoff
, j
= 0; j
< s
; j
++, off
+= chunksize
) {
3984 if (i
!= 5 || chunksize
< (SPA_MINBLOCKSIZE
* 2)) {
3985 bcopy((caddr_t
)bigbuf
+ (off
- bigoff
),
3986 bigbuf_arcbufs
[j
]->b_data
, chunksize
);
3988 bcopy((caddr_t
)bigbuf
+ (off
- bigoff
),
3989 bigbuf_arcbufs
[2 * j
]->b_data
,
3991 bcopy((caddr_t
)bigbuf
+ (off
- bigoff
) +
3993 bigbuf_arcbufs
[2 * j
+ 1]->b_data
,
3998 VERIFY(dmu_buf_hold(os
, bigobj
, off
,
3999 FTAG
, &dbt
, DMU_READ_NO_PREFETCH
) == 0);
4001 if (i
!= 5 || chunksize
< (SPA_MINBLOCKSIZE
* 2)) {
4002 dmu_assign_arcbuf(bonus_db
, off
,
4003 bigbuf_arcbufs
[j
], tx
);
4005 dmu_assign_arcbuf(bonus_db
, off
,
4006 bigbuf_arcbufs
[2 * j
], tx
);
4007 dmu_assign_arcbuf(bonus_db
,
4008 off
+ chunksize
/ 2,
4009 bigbuf_arcbufs
[2 * j
+ 1], tx
);
4012 dmu_buf_rele(dbt
, FTAG
);
4018 * Sanity check the stuff we just wrote.
4021 void *packcheck
= umem_alloc(packsize
, UMEM_NOFAIL
);
4022 void *bigcheck
= umem_alloc(bigsize
, UMEM_NOFAIL
);
4024 VERIFY(0 == dmu_read(os
, packobj
, packoff
,
4025 packsize
, packcheck
, DMU_READ_PREFETCH
));
4026 VERIFY(0 == dmu_read(os
, bigobj
, bigoff
,
4027 bigsize
, bigcheck
, DMU_READ_PREFETCH
));
4029 ASSERT(bcmp(packbuf
, packcheck
, packsize
) == 0);
4030 ASSERT(bcmp(bigbuf
, bigcheck
, bigsize
) == 0);
4032 umem_free(packcheck
, packsize
);
4033 umem_free(bigcheck
, bigsize
);
4036 txg_wait_open(dmu_objset_pool(os
), 0);
4037 } else if (i
== 3) {
4038 txg_wait_synced(dmu_objset_pool(os
), 0);
4042 dmu_buf_rele(bonus_db
, FTAG
);
4043 umem_free(packbuf
, packsize
);
4044 umem_free(bigbuf
, bigsize
);
4045 umem_free(bigbuf_arcbufs
, 2 * s
* sizeof (arc_buf_t
*));
4050 ztest_dmu_write_parallel(ztest_ds_t
*zd
, uint64_t id
)
4053 uint64_t offset
= (1ULL << (ztest_random(20) + 43)) +
4054 (ztest_random(ZTEST_RANGE_LOCKS
) << SPA_MAXBLOCKSHIFT
);
4057 * Have multiple threads write to large offsets in an object
4058 * to verify that parallel writes to an object -- even to the
4059 * same blocks within the object -- doesn't cause any trouble.
4061 ztest_od_init(&od
[0], ID_PARALLEL
, FTAG
, 0, DMU_OT_UINT64_OTHER
, 0, 0);
4063 if (ztest_object_init(zd
, od
, sizeof (od
), B_FALSE
) != 0)
4066 while (ztest_random(10) != 0)
4067 ztest_io(zd
, od
[0].od_object
, offset
);
4071 ztest_dmu_prealloc(ztest_ds_t
*zd
, uint64_t id
)
4074 uint64_t offset
= (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT
)) +
4075 (ztest_random(ZTEST_RANGE_LOCKS
) << SPA_MAXBLOCKSHIFT
);
4076 uint64_t count
= ztest_random(20) + 1;
4077 uint64_t blocksize
= ztest_random_blocksize();
4080 ztest_od_init(&od
[0], id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, blocksize
, 0);
4082 if (ztest_object_init(zd
, od
, sizeof (od
), !ztest_random(2)) != 0)
4085 if (ztest_truncate(zd
, od
[0].od_object
, offset
, count
* blocksize
) != 0)
4088 ztest_prealloc(zd
, od
[0].od_object
, offset
, count
* blocksize
);
4090 data
= umem_zalloc(blocksize
, UMEM_NOFAIL
);
4092 while (ztest_random(count
) != 0) {
4093 uint64_t randoff
= offset
+ (ztest_random(count
) * blocksize
);
4094 if (ztest_write(zd
, od
[0].od_object
, randoff
, blocksize
,
4097 while (ztest_random(4) != 0)
4098 ztest_io(zd
, od
[0].od_object
, randoff
);
4101 umem_free(data
, blocksize
);
4105 * Verify that zap_{create,destroy,add,remove,update} work as expected.
4107 #define ZTEST_ZAP_MIN_INTS 1
4108 #define ZTEST_ZAP_MAX_INTS 4
4109 #define ZTEST_ZAP_MAX_PROPS 1000
4112 ztest_zap(ztest_ds_t
*zd
, uint64_t id
)
4114 objset_t
*os
= zd
->zd_os
;
4117 uint64_t txg
, last_txg
;
4118 uint64_t value
[ZTEST_ZAP_MAX_INTS
];
4119 uint64_t zl_ints
, zl_intsize
, prop
;
4122 char propname
[100], txgname
[100];
4124 char *hc
[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
4126 ztest_od_init(&od
[0], id
, FTAG
, 0, DMU_OT_ZAP_OTHER
, 0, 0);
4128 if (ztest_object_init(zd
, od
, sizeof (od
), !ztest_random(2)) != 0)
4131 object
= od
[0].od_object
;
4134 * Generate a known hash collision, and verify that
4135 * we can lookup and remove both entries.
4137 tx
= dmu_tx_create(os
);
4138 dmu_tx_hold_zap(tx
, object
, B_TRUE
, NULL
);
4139 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
4142 for (i
= 0; i
< 2; i
++) {
4144 VERIFY3U(0, ==, zap_add(os
, object
, hc
[i
], sizeof (uint64_t),
4147 for (i
= 0; i
< 2; i
++) {
4148 VERIFY3U(EEXIST
, ==, zap_add(os
, object
, hc
[i
],
4149 sizeof (uint64_t), 1, &value
[i
], tx
));
4151 zap_length(os
, object
, hc
[i
], &zl_intsize
, &zl_ints
));
4152 ASSERT3U(zl_intsize
, ==, sizeof (uint64_t));
4153 ASSERT3U(zl_ints
, ==, 1);
4155 for (i
= 0; i
< 2; i
++) {
4156 VERIFY3U(0, ==, zap_remove(os
, object
, hc
[i
], tx
));
4161 * Generate a buch of random entries.
4163 ints
= MAX(ZTEST_ZAP_MIN_INTS
, object
% ZTEST_ZAP_MAX_INTS
);
4165 prop
= ztest_random(ZTEST_ZAP_MAX_PROPS
);
4166 (void) sprintf(propname
, "prop_%llu", (u_longlong_t
)prop
);
4167 (void) sprintf(txgname
, "txg_%llu", (u_longlong_t
)prop
);
4168 bzero(value
, sizeof (value
));
4172 * If these zap entries already exist, validate their contents.
4174 error
= zap_length(os
, object
, txgname
, &zl_intsize
, &zl_ints
);
4176 ASSERT3U(zl_intsize
, ==, sizeof (uint64_t));
4177 ASSERT3U(zl_ints
, ==, 1);
4179 VERIFY(zap_lookup(os
, object
, txgname
, zl_intsize
,
4180 zl_ints
, &last_txg
) == 0);
4182 VERIFY(zap_length(os
, object
, propname
, &zl_intsize
,
4185 ASSERT3U(zl_intsize
, ==, sizeof (uint64_t));
4186 ASSERT3U(zl_ints
, ==, ints
);
4188 VERIFY(zap_lookup(os
, object
, propname
, zl_intsize
,
4189 zl_ints
, value
) == 0);
4191 for (i
= 0; i
< ints
; i
++) {
4192 ASSERT3U(value
[i
], ==, last_txg
+ object
+ i
);
4195 ASSERT3U(error
, ==, ENOENT
);
4199 * Atomically update two entries in our zap object.
4200 * The first is named txg_%llu, and contains the txg
4201 * in which the property was last updated. The second
4202 * is named prop_%llu, and the nth element of its value
4203 * should be txg + object + n.
4205 tx
= dmu_tx_create(os
);
4206 dmu_tx_hold_zap(tx
, object
, B_TRUE
, NULL
);
4207 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
4212 fatal(0, "zap future leak: old %llu new %llu", last_txg
, txg
);
4214 for (i
= 0; i
< ints
; i
++)
4215 value
[i
] = txg
+ object
+ i
;
4217 VERIFY3U(0, ==, zap_update(os
, object
, txgname
, sizeof (uint64_t),
4219 VERIFY3U(0, ==, zap_update(os
, object
, propname
, sizeof (uint64_t),
4225 * Remove a random pair of entries.
4227 prop
= ztest_random(ZTEST_ZAP_MAX_PROPS
);
4228 (void) sprintf(propname
, "prop_%llu", (u_longlong_t
)prop
);
4229 (void) sprintf(txgname
, "txg_%llu", (u_longlong_t
)prop
);
4231 error
= zap_length(os
, object
, txgname
, &zl_intsize
, &zl_ints
);
4233 if (error
== ENOENT
)
4238 tx
= dmu_tx_create(os
);
4239 dmu_tx_hold_zap(tx
, object
, B_TRUE
, NULL
);
4240 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
4243 VERIFY3U(0, ==, zap_remove(os
, object
, txgname
, tx
));
4244 VERIFY3U(0, ==, zap_remove(os
, object
, propname
, tx
));
4249 * Testcase to test the upgrading of a microzap to fatzap.
4252 ztest_fzap(ztest_ds_t
*zd
, uint64_t id
)
4254 objset_t
*os
= zd
->zd_os
;
4256 uint64_t object
, txg
;
4258 ztest_od_init(&od
[0], id
, FTAG
, 0, DMU_OT_ZAP_OTHER
, 0, 0);
4260 if (ztest_object_init(zd
, od
, sizeof (od
), !ztest_random(2)) != 0)
4263 object
= od
[0].od_object
;
4266 * Add entries to this ZAP and make sure it spills over
4267 * and gets upgraded to a fatzap. Also, since we are adding
4268 * 2050 entries we should see ptrtbl growth and leaf-block split.
4270 for (int i
= 0; i
< 2050; i
++) {
4271 char name
[ZFS_MAX_DATASET_NAME_LEN
];
4276 (void) snprintf(name
, sizeof (name
), "fzap-%llu-%llu",
4279 tx
= dmu_tx_create(os
);
4280 dmu_tx_hold_zap(tx
, object
, B_TRUE
, name
);
4281 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
4284 error
= zap_add(os
, object
, name
, sizeof (uint64_t), 1,
4286 ASSERT(error
== 0 || error
== EEXIST
);
4293 ztest_zap_parallel(ztest_ds_t
*zd
, uint64_t id
)
4295 objset_t
*os
= zd
->zd_os
;
4297 uint64_t txg
, object
, count
, wsize
, wc
, zl_wsize
, zl_wc
;
4299 int i
, namelen
, error
;
4300 int micro
= ztest_random(2);
4301 char name
[20], string_value
[20];
4304 ztest_od_init(&od
[0], ID_PARALLEL
, FTAG
, micro
, DMU_OT_ZAP_OTHER
, 0, 0);
4306 if (ztest_object_init(zd
, od
, sizeof (od
), B_FALSE
) != 0)
4309 object
= od
[0].od_object
;
4312 * Generate a random name of the form 'xxx.....' where each
4313 * x is a random printable character and the dots are dots.
4314 * There are 94 such characters, and the name length goes from
4315 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
4317 namelen
= ztest_random(sizeof (name
) - 5) + 5 + 1;
4319 for (i
= 0; i
< 3; i
++)
4320 name
[i
] = '!' + ztest_random('~' - '!' + 1);
4321 for (; i
< namelen
- 1; i
++)
4325 if ((namelen
& 1) || micro
) {
4326 wsize
= sizeof (txg
);
4332 data
= string_value
;
4336 VERIFY0(zap_count(os
, object
, &count
));
4337 ASSERT(count
!= -1ULL);
4340 * Select an operation: length, lookup, add, update, remove.
4342 i
= ztest_random(5);
4345 tx
= dmu_tx_create(os
);
4346 dmu_tx_hold_zap(tx
, object
, B_TRUE
, NULL
);
4347 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
4350 bcopy(name
, string_value
, namelen
);
4354 bzero(string_value
, namelen
);
4360 error
= zap_length(os
, object
, name
, &zl_wsize
, &zl_wc
);
4362 ASSERT3U(wsize
, ==, zl_wsize
);
4363 ASSERT3U(wc
, ==, zl_wc
);
4365 ASSERT3U(error
, ==, ENOENT
);
4370 error
= zap_lookup(os
, object
, name
, wsize
, wc
, data
);
4372 if (data
== string_value
&&
4373 bcmp(name
, data
, namelen
) != 0)
4374 fatal(0, "name '%s' != val '%s' len %d",
4375 name
, data
, namelen
);
4377 ASSERT3U(error
, ==, ENOENT
);
4382 error
= zap_add(os
, object
, name
, wsize
, wc
, data
, tx
);
4383 ASSERT(error
== 0 || error
== EEXIST
);
4387 VERIFY(zap_update(os
, object
, name
, wsize
, wc
, data
, tx
) == 0);
4391 error
= zap_remove(os
, object
, name
, tx
);
4392 ASSERT(error
== 0 || error
== ENOENT
);
4401 * Commit callback data.
4403 typedef struct ztest_cb_data
{
4404 list_node_t zcd_node
;
4406 int zcd_expected_err
;
4407 boolean_t zcd_added
;
4408 boolean_t zcd_called
;
4412 /* This is the actual commit callback function */
4414 ztest_commit_callback(void *arg
, int error
)
4416 ztest_cb_data_t
*data
= arg
;
4417 uint64_t synced_txg
;
4419 VERIFY(data
!= NULL
);
4420 VERIFY3S(data
->zcd_expected_err
, ==, error
);
4421 VERIFY(!data
->zcd_called
);
4423 synced_txg
= spa_last_synced_txg(data
->zcd_spa
);
4424 if (data
->zcd_txg
> synced_txg
)
4425 fatal(0, "commit callback of txg %" PRIu64
" called prematurely"
4426 ", last synced txg = %" PRIu64
"\n", data
->zcd_txg
,
4429 data
->zcd_called
= B_TRUE
;
4431 if (error
== ECANCELED
) {
4432 ASSERT0(data
->zcd_txg
);
4433 ASSERT(!data
->zcd_added
);
4436 * The private callback data should be destroyed here, but
4437 * since we are going to check the zcd_called field after
4438 * dmu_tx_abort(), we will destroy it there.
4443 /* Was this callback added to the global callback list? */
4444 if (!data
->zcd_added
)
4447 ASSERT3U(data
->zcd_txg
, !=, 0);
4449 /* Remove our callback from the list */
4450 (void) mutex_lock(&zcl
.zcl_callbacks_lock
);
4451 list_remove(&zcl
.zcl_callbacks
, data
);
4452 (void) mutex_unlock(&zcl
.zcl_callbacks_lock
);
4455 umem_free(data
, sizeof (ztest_cb_data_t
));
4458 /* Allocate and initialize callback data structure */
4459 static ztest_cb_data_t
*
4460 ztest_create_cb_data(objset_t
*os
, uint64_t txg
)
4462 ztest_cb_data_t
*cb_data
;
4464 cb_data
= umem_zalloc(sizeof (ztest_cb_data_t
), UMEM_NOFAIL
);
4466 cb_data
->zcd_txg
= txg
;
4467 cb_data
->zcd_spa
= dmu_objset_spa(os
);
4473 * If a number of txgs equal to this threshold have been created after a commit
4474 * callback has been registered but not called, then we assume there is an
4475 * implementation bug.
4477 #define ZTEST_COMMIT_CALLBACK_THRESH (TXG_CONCURRENT_STATES + 2)
4480 * Commit callback test.
4483 ztest_dmu_commit_callbacks(ztest_ds_t
*zd
, uint64_t id
)
4485 objset_t
*os
= zd
->zd_os
;
4488 ztest_cb_data_t
*cb_data
[3], *tmp_cb
;
4489 uint64_t old_txg
, txg
;
4492 ztest_od_init(&od
[0], id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, 0, 0);
4494 if (ztest_object_init(zd
, od
, sizeof (od
), B_FALSE
) != 0)
4497 tx
= dmu_tx_create(os
);
4499 cb_data
[0] = ztest_create_cb_data(os
, 0);
4500 dmu_tx_callback_register(tx
, ztest_commit_callback
, cb_data
[0]);
4502 dmu_tx_hold_write(tx
, od
[0].od_object
, 0, sizeof (uint64_t));
4504 /* Every once in a while, abort the transaction on purpose */
4505 if (ztest_random(100) == 0)
4509 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
4511 txg
= error
? 0 : dmu_tx_get_txg(tx
);
4513 cb_data
[0]->zcd_txg
= txg
;
4514 cb_data
[1] = ztest_create_cb_data(os
, txg
);
4515 dmu_tx_callback_register(tx
, ztest_commit_callback
, cb_data
[1]);
4519 * It's not a strict requirement to call the registered
4520 * callbacks from inside dmu_tx_abort(), but that's what
4521 * it's supposed to happen in the current implementation
4522 * so we will check for that.
4524 for (i
= 0; i
< 2; i
++) {
4525 cb_data
[i
]->zcd_expected_err
= ECANCELED
;
4526 VERIFY(!cb_data
[i
]->zcd_called
);
4531 for (i
= 0; i
< 2; i
++) {
4532 VERIFY(cb_data
[i
]->zcd_called
);
4533 umem_free(cb_data
[i
], sizeof (ztest_cb_data_t
));
4539 cb_data
[2] = ztest_create_cb_data(os
, txg
);
4540 dmu_tx_callback_register(tx
, ztest_commit_callback
, cb_data
[2]);
4543 * Read existing data to make sure there isn't a future leak.
4545 VERIFY(0 == dmu_read(os
, od
[0].od_object
, 0, sizeof (uint64_t),
4546 &old_txg
, DMU_READ_PREFETCH
));
4549 fatal(0, "future leak: got %" PRIu64
", open txg is %" PRIu64
,
4552 dmu_write(os
, od
[0].od_object
, 0, sizeof (uint64_t), &txg
, tx
);
4554 (void) mutex_lock(&zcl
.zcl_callbacks_lock
);
4557 * Since commit callbacks don't have any ordering requirement and since
4558 * it is theoretically possible for a commit callback to be called
4559 * after an arbitrary amount of time has elapsed since its txg has been
4560 * synced, it is difficult to reliably determine whether a commit
4561 * callback hasn't been called due to high load or due to a flawed
4564 * In practice, we will assume that if after a certain number of txgs a
4565 * commit callback hasn't been called, then most likely there's an
4566 * implementation bug..
4568 tmp_cb
= list_head(&zcl
.zcl_callbacks
);
4569 if (tmp_cb
!= NULL
&&
4570 (txg
- ZTEST_COMMIT_CALLBACK_THRESH
) > tmp_cb
->zcd_txg
) {
4571 fatal(0, "Commit callback threshold exceeded, oldest txg: %"
4572 PRIu64
", open txg: %" PRIu64
"\n", tmp_cb
->zcd_txg
, txg
);
4576 * Let's find the place to insert our callbacks.
4578 * Even though the list is ordered by txg, it is possible for the
4579 * insertion point to not be the end because our txg may already be
4580 * quiescing at this point and other callbacks in the open txg
4581 * (from other objsets) may have sneaked in.
4583 tmp_cb
= list_tail(&zcl
.zcl_callbacks
);
4584 while (tmp_cb
!= NULL
&& tmp_cb
->zcd_txg
> txg
)
4585 tmp_cb
= list_prev(&zcl
.zcl_callbacks
, tmp_cb
);
4587 /* Add the 3 callbacks to the list */
4588 for (i
= 0; i
< 3; i
++) {
4590 list_insert_head(&zcl
.zcl_callbacks
, cb_data
[i
]);
4592 list_insert_after(&zcl
.zcl_callbacks
, tmp_cb
,
4595 cb_data
[i
]->zcd_added
= B_TRUE
;
4596 VERIFY(!cb_data
[i
]->zcd_called
);
4598 tmp_cb
= cb_data
[i
];
4601 (void) mutex_unlock(&zcl
.zcl_callbacks_lock
);
4608 ztest_dsl_prop_get_set(ztest_ds_t
*zd
, uint64_t id
)
4610 zfs_prop_t proplist
[] = {
4612 ZFS_PROP_COMPRESSION
,
4617 (void) rw_rdlock(&ztest_name_lock
);
4619 for (int p
= 0; p
< sizeof (proplist
) / sizeof (proplist
[0]); p
++)
4620 (void) ztest_dsl_prop_set_uint64(zd
->zd_name
, proplist
[p
],
4621 ztest_random_dsl_prop(proplist
[p
]), (int)ztest_random(2));
4623 (void) rw_unlock(&ztest_name_lock
);
4628 ztest_spa_prop_get_set(ztest_ds_t
*zd
, uint64_t id
)
4630 nvlist_t
*props
= NULL
;
4632 (void) rw_rdlock(&ztest_name_lock
);
4634 (void) ztest_spa_prop_set_uint64(ZPOOL_PROP_DEDUPDITTO
,
4635 ZIO_DEDUPDITTO_MIN
+ ztest_random(ZIO_DEDUPDITTO_MIN
));
4637 VERIFY0(spa_prop_get(ztest_spa
, &props
));
4639 if (ztest_opts
.zo_verbose
>= 6)
4640 dump_nvlist(props
, 4);
4644 (void) rw_unlock(&ztest_name_lock
);
4648 user_release_one(const char *snapname
, const char *holdname
)
4650 nvlist_t
*snaps
, *holds
;
4653 snaps
= fnvlist_alloc();
4654 holds
= fnvlist_alloc();
4655 fnvlist_add_boolean(holds
, holdname
);
4656 fnvlist_add_nvlist(snaps
, snapname
, holds
);
4657 fnvlist_free(holds
);
4658 error
= dsl_dataset_user_release(snaps
, NULL
);
4659 fnvlist_free(snaps
);
4664 * Test snapshot hold/release and deferred destroy.
4667 ztest_dmu_snapshot_hold(ztest_ds_t
*zd
, uint64_t id
)
4670 objset_t
*os
= zd
->zd_os
;
4674 char clonename
[100];
4676 char osname
[ZFS_MAX_DATASET_NAME_LEN
];
4679 (void) rw_rdlock(&ztest_name_lock
);
4681 dmu_objset_name(os
, osname
);
4683 (void) snprintf(snapname
, sizeof (snapname
), "sh1_%llu", id
);
4684 (void) snprintf(fullname
, sizeof (fullname
), "%s@%s", osname
, snapname
);
4685 (void) snprintf(clonename
, sizeof (clonename
),
4686 "%s/ch1_%llu", osname
, id
);
4687 (void) snprintf(tag
, sizeof (tag
), "tag_%llu", id
);
4690 * Clean up from any previous run.
4692 error
= dsl_destroy_head(clonename
);
4693 if (error
!= ENOENT
)
4695 error
= user_release_one(fullname
, tag
);
4696 if (error
!= ESRCH
&& error
!= ENOENT
)
4698 error
= dsl_destroy_snapshot(fullname
, B_FALSE
);
4699 if (error
!= ENOENT
)
4703 * Create snapshot, clone it, mark snap for deferred destroy,
4704 * destroy clone, verify snap was also destroyed.
4706 error
= dmu_objset_snapshot_one(osname
, snapname
);
4708 if (error
== ENOSPC
) {
4709 ztest_record_enospc("dmu_objset_snapshot");
4712 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname
, error
);
4715 error
= dmu_objset_clone(clonename
, fullname
);
4717 if (error
== ENOSPC
) {
4718 ztest_record_enospc("dmu_objset_clone");
4721 fatal(0, "dmu_objset_clone(%s) = %d", clonename
, error
);
4724 error
= dsl_destroy_snapshot(fullname
, B_TRUE
);
4726 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
4730 error
= dsl_destroy_head(clonename
);
4732 fatal(0, "dsl_destroy_head(%s) = %d", clonename
, error
);
4734 error
= dmu_objset_hold(fullname
, FTAG
, &origin
);
4735 if (error
!= ENOENT
)
4736 fatal(0, "dmu_objset_hold(%s) = %d", fullname
, error
);
4739 * Create snapshot, add temporary hold, verify that we can't
4740 * destroy a held snapshot, mark for deferred destroy,
4741 * release hold, verify snapshot was destroyed.
4743 error
= dmu_objset_snapshot_one(osname
, snapname
);
4745 if (error
== ENOSPC
) {
4746 ztest_record_enospc("dmu_objset_snapshot");
4749 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname
, error
);
4752 holds
= fnvlist_alloc();
4753 fnvlist_add_string(holds
, fullname
, tag
);
4754 error
= dsl_dataset_user_hold(holds
, 0, NULL
);
4755 fnvlist_free(holds
);
4757 if (error
== ENOSPC
) {
4758 ztest_record_enospc("dsl_dataset_user_hold");
4761 fatal(0, "dsl_dataset_user_hold(%s, %s) = %u",
4762 fullname
, tag
, error
);
4765 error
= dsl_destroy_snapshot(fullname
, B_FALSE
);
4766 if (error
!= EBUSY
) {
4767 fatal(0, "dsl_destroy_snapshot(%s, B_FALSE) = %d",
4771 error
= dsl_destroy_snapshot(fullname
, B_TRUE
);
4773 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
4777 error
= user_release_one(fullname
, tag
);
4779 fatal(0, "user_release_one(%s, %s) = %d", fullname
, tag
, error
);
4781 VERIFY3U(dmu_objset_hold(fullname
, FTAG
, &origin
), ==, ENOENT
);
4784 (void) rw_unlock(&ztest_name_lock
);
4788 * Inject random faults into the on-disk data.
4792 ztest_fault_inject(ztest_ds_t
*zd
, uint64_t id
)
4794 ztest_shared_t
*zs
= ztest_shared
;
4795 spa_t
*spa
= ztest_spa
;
4799 uint64_t bad
= 0x1990c0ffeedecade;
4801 char path0
[MAXPATHLEN
];
4802 char pathrand
[MAXPATHLEN
];
4804 int bshift
= SPA_MAXBLOCKSHIFT
+ 2;
4810 boolean_t islog
= B_FALSE
;
4812 VERIFY(mutex_lock(&ztest_vdev_lock
) == 0);
4813 maxfaults
= MAXFAULTS();
4814 leaves
= MAX(zs
->zs_mirrors
, 1) * ztest_opts
.zo_raidz
;
4815 mirror_save
= zs
->zs_mirrors
;
4816 VERIFY(mutex_unlock(&ztest_vdev_lock
) == 0);
4818 ASSERT(leaves
>= 1);
4821 * Grab the name lock as reader. There are some operations
4822 * which don't like to have their vdevs changed while
4823 * they are in progress (i.e. spa_change_guid). Those
4824 * operations will have grabbed the name lock as writer.
4826 (void) rw_rdlock(&ztest_name_lock
);
4829 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
4831 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
4833 if (ztest_random(2) == 0) {
4835 * Inject errors on a normal data device or slog device.
4837 top
= ztest_random_vdev_top(spa
, B_TRUE
);
4838 leaf
= ztest_random(leaves
) + zs
->zs_splits
;
4841 * Generate paths to the first leaf in this top-level vdev,
4842 * and to the random leaf we selected. We'll induce transient
4843 * write failures and random online/offline activity on leaf 0,
4844 * and we'll write random garbage to the randomly chosen leaf.
4846 (void) snprintf(path0
, sizeof (path0
), ztest_dev_template
,
4847 ztest_opts
.zo_dir
, ztest_opts
.zo_pool
,
4848 top
* leaves
+ zs
->zs_splits
);
4849 (void) snprintf(pathrand
, sizeof (pathrand
), ztest_dev_template
,
4850 ztest_opts
.zo_dir
, ztest_opts
.zo_pool
,
4851 top
* leaves
+ leaf
);
4853 vd0
= vdev_lookup_by_path(spa
->spa_root_vdev
, path0
);
4854 if (vd0
!= NULL
&& vd0
->vdev_top
->vdev_islog
)
4858 * If the top-level vdev needs to be resilvered
4859 * then we only allow faults on the device that is
4862 if (vd0
!= NULL
&& maxfaults
!= 1 &&
4863 (!vdev_resilver_needed(vd0
->vdev_top
, NULL
, NULL
) ||
4864 vd0
->vdev_resilver_txg
!= 0)) {
4866 * Make vd0 explicitly claim to be unreadable,
4867 * or unwriteable, or reach behind its back
4868 * and close the underlying fd. We can do this if
4869 * maxfaults == 0 because we'll fail and reexecute,
4870 * and we can do it if maxfaults >= 2 because we'll
4871 * have enough redundancy. If maxfaults == 1, the
4872 * combination of this with injection of random data
4873 * corruption below exceeds the pool's fault tolerance.
4875 vdev_file_t
*vf
= vd0
->vdev_tsd
;
4877 if (vf
!= NULL
&& ztest_random(3) == 0) {
4878 (void) close(vf
->vf_vnode
->v_fd
);
4879 vf
->vf_vnode
->v_fd
= -1;
4880 } else if (ztest_random(2) == 0) {
4881 vd0
->vdev_cant_read
= B_TRUE
;
4883 vd0
->vdev_cant_write
= B_TRUE
;
4885 guid0
= vd0
->vdev_guid
;
4889 * Inject errors on an l2cache device.
4891 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
4893 if (sav
->sav_count
== 0) {
4894 spa_config_exit(spa
, SCL_STATE
, FTAG
);
4895 (void) rw_unlock(&ztest_name_lock
);
4898 vd0
= sav
->sav_vdevs
[ztest_random(sav
->sav_count
)];
4899 guid0
= vd0
->vdev_guid
;
4900 (void) strcpy(path0
, vd0
->vdev_path
);
4901 (void) strcpy(pathrand
, vd0
->vdev_path
);
4905 maxfaults
= INT_MAX
; /* no limit on cache devices */
4908 spa_config_exit(spa
, SCL_STATE
, FTAG
);
4909 (void) rw_unlock(&ztest_name_lock
);
4912 * If we can tolerate two or more faults, or we're dealing
4913 * with a slog, randomly online/offline vd0.
4915 if ((maxfaults
>= 2 || islog
) && guid0
!= 0) {
4916 if (ztest_random(10) < 6) {
4917 int flags
= (ztest_random(2) == 0 ?
4918 ZFS_OFFLINE_TEMPORARY
: 0);
4921 * We have to grab the zs_name_lock as writer to
4922 * prevent a race between offlining a slog and
4923 * destroying a dataset. Offlining the slog will
4924 * grab a reference on the dataset which may cause
4925 * dmu_objset_destroy() to fail with EBUSY thus
4926 * leaving the dataset in an inconsistent state.
4929 (void) rw_wrlock(&ztest_name_lock
);
4931 VERIFY(vdev_offline(spa
, guid0
, flags
) != EBUSY
);
4934 (void) rw_unlock(&ztest_name_lock
);
4937 * Ideally we would like to be able to randomly
4938 * call vdev_[on|off]line without holding locks
4939 * to force unpredictable failures but the side
4940 * effects of vdev_[on|off]line prevent us from
4941 * doing so. We grab the ztest_vdev_lock here to
4942 * prevent a race between injection testing and
4945 VERIFY(mutex_lock(&ztest_vdev_lock
) == 0);
4946 (void) vdev_online(spa
, guid0
, 0, NULL
);
4947 VERIFY(mutex_unlock(&ztest_vdev_lock
) == 0);
4955 * We have at least single-fault tolerance, so inject data corruption.
4957 fd
= open(pathrand
, O_RDWR
);
4959 if (fd
== -1) /* we hit a gap in the device namespace */
4962 fsize
= lseek(fd
, 0, SEEK_END
);
4964 while (--iters
!= 0) {
4966 * The offset must be chosen carefully to ensure that
4967 * we do not inject a given logical block with errors
4968 * on two different leaf devices, because ZFS can not
4969 * tolerate that (if maxfaults==1).
4971 * We divide each leaf into chunks of size
4972 * (# leaves * SPA_MAXBLOCKSIZE * 4). Within each chunk
4973 * there is a series of ranges to which we can inject errors.
4974 * Each range can accept errors on only a single leaf vdev.
4975 * The error injection ranges are separated by ranges
4976 * which we will not inject errors on any device (DMZs).
4977 * Each DMZ must be large enough such that a single block
4978 * can not straddle it, so that a single block can not be
4979 * a target in two different injection ranges (on different
4982 * For example, with 3 leaves, each chunk looks like:
4983 * 0 to 32M: injection range for leaf 0
4984 * 32M to 64M: DMZ - no injection allowed
4985 * 64M to 96M: injection range for leaf 1
4986 * 96M to 128M: DMZ - no injection allowed
4987 * 128M to 160M: injection range for leaf 2
4988 * 160M to 192M: DMZ - no injection allowed
4990 offset
= ztest_random(fsize
/ (leaves
<< bshift
)) *
4991 (leaves
<< bshift
) + (leaf
<< bshift
) +
4992 (ztest_random(1ULL << (bshift
- 1)) & -8ULL);
4995 * Only allow damage to the labels at one end of the vdev.
4997 * If all labels are damaged, the device will be totally
4998 * inaccessible, which will result in loss of data,
4999 * because we also damage (parts of) the other side of
5002 * Additionally, we will always have both an even and an
5003 * odd label, so that we can handle crashes in the
5004 * middle of vdev_config_sync().
5006 if ((leaf
& 1) == 0 && offset
< VDEV_LABEL_START_SIZE
)
5010 * The two end labels are stored at the "end" of the disk, but
5011 * the end of the disk (vdev_psize) is aligned to
5012 * sizeof (vdev_label_t).
5014 uint64_t psize
= P2ALIGN(fsize
, sizeof (vdev_label_t
));
5015 if ((leaf
& 1) == 1 &&
5016 offset
+ sizeof (bad
) > psize
- VDEV_LABEL_END_SIZE
)
5019 VERIFY(mutex_lock(&ztest_vdev_lock
) == 0);
5020 if (mirror_save
!= zs
->zs_mirrors
) {
5021 VERIFY(mutex_unlock(&ztest_vdev_lock
) == 0);
5026 if (pwrite(fd
, &bad
, sizeof (bad
), offset
) != sizeof (bad
))
5027 fatal(1, "can't inject bad word at 0x%llx in %s",
5030 VERIFY(mutex_unlock(&ztest_vdev_lock
) == 0);
5032 if (ztest_opts
.zo_verbose
>= 7)
5033 (void) printf("injected bad word into %s,"
5034 " offset 0x%llx\n", pathrand
, (u_longlong_t
)offset
);
5041 * Verify that DDT repair works as expected.
5044 ztest_ddt_repair(ztest_ds_t
*zd
, uint64_t id
)
5046 ztest_shared_t
*zs
= ztest_shared
;
5047 spa_t
*spa
= ztest_spa
;
5048 objset_t
*os
= zd
->zd_os
;
5050 uint64_t object
, blocksize
, txg
, pattern
, psize
;
5051 enum zio_checksum checksum
= spa_dedup_checksum(spa
);
5056 int copies
= 2 * ZIO_DEDUPDITTO_MIN
;
5058 blocksize
= ztest_random_blocksize();
5059 blocksize
= MIN(blocksize
, 2048); /* because we write so many */
5061 ztest_od_init(&od
[0], id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, blocksize
, 0);
5063 if (ztest_object_init(zd
, od
, sizeof (od
), B_FALSE
) != 0)
5067 * Take the name lock as writer to prevent anyone else from changing
5068 * the pool and dataset properies we need to maintain during this test.
5070 (void) rw_wrlock(&ztest_name_lock
);
5072 if (ztest_dsl_prop_set_uint64(zd
->zd_name
, ZFS_PROP_DEDUP
, checksum
,
5074 ztest_dsl_prop_set_uint64(zd
->zd_name
, ZFS_PROP_COPIES
, 1,
5076 (void) rw_unlock(&ztest_name_lock
);
5080 dmu_objset_stats_t dds
;
5081 dsl_pool_config_enter(dmu_objset_pool(os
), FTAG
);
5082 dmu_objset_fast_stat(os
, &dds
);
5083 dsl_pool_config_exit(dmu_objset_pool(os
), FTAG
);
5085 object
= od
[0].od_object
;
5086 blocksize
= od
[0].od_blocksize
;
5087 pattern
= zs
->zs_guid
^ dds
.dds_guid
;
5089 ASSERT(object
!= 0);
5091 tx
= dmu_tx_create(os
);
5092 dmu_tx_hold_write(tx
, object
, 0, copies
* blocksize
);
5093 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
5095 (void) rw_unlock(&ztest_name_lock
);
5100 * Write all the copies of our block.
5102 for (int i
= 0; i
< copies
; i
++) {
5103 uint64_t offset
= i
* blocksize
;
5104 int error
= dmu_buf_hold(os
, object
, offset
, FTAG
, &db
,
5105 DMU_READ_NO_PREFETCH
);
5107 fatal(B_FALSE
, "dmu_buf_hold(%p, %llu, %llu) = %u",
5108 os
, (long long)object
, (long long) offset
, error
);
5110 ASSERT(db
->db_offset
== offset
);
5111 ASSERT(db
->db_size
== blocksize
);
5112 ASSERT(ztest_pattern_match(db
->db_data
, db
->db_size
, pattern
) ||
5113 ztest_pattern_match(db
->db_data
, db
->db_size
, 0ULL));
5114 dmu_buf_will_fill(db
, tx
);
5115 ztest_pattern_set(db
->db_data
, db
->db_size
, pattern
);
5116 dmu_buf_rele(db
, FTAG
);
5120 txg_wait_synced(spa_get_dsl(spa
), txg
);
5123 * Find out what block we got.
5125 VERIFY0(dmu_buf_hold(os
, object
, 0, FTAG
, &db
,
5126 DMU_READ_NO_PREFETCH
));
5127 blk
= *((dmu_buf_impl_t
*)db
)->db_blkptr
;
5128 dmu_buf_rele(db
, FTAG
);
5131 * Damage the block. Dedup-ditto will save us when we read it later.
5133 psize
= BP_GET_PSIZE(&blk
);
5134 buf
= zio_buf_alloc(psize
);
5135 ztest_pattern_set(buf
, psize
, ~pattern
);
5137 (void) zio_wait(zio_rewrite(NULL
, spa
, 0, &blk
,
5138 buf
, psize
, NULL
, NULL
, ZIO_PRIORITY_SYNC_WRITE
,
5139 ZIO_FLAG_CANFAIL
| ZIO_FLAG_INDUCE_DAMAGE
, NULL
));
5141 zio_buf_free(buf
, psize
);
5143 (void) rw_unlock(&ztest_name_lock
);
5151 ztest_scrub(ztest_ds_t
*zd
, uint64_t id
)
5153 spa_t
*spa
= ztest_spa
;
5155 (void) spa_scan(spa
, POOL_SCAN_SCRUB
);
5156 (void) poll(NULL
, 0, 100); /* wait a moment, then force a restart */
5157 (void) spa_scan(spa
, POOL_SCAN_SCRUB
);
5161 * Change the guid for the pool.
5165 ztest_reguid(ztest_ds_t
*zd
, uint64_t id
)
5167 spa_t
*spa
= ztest_spa
;
5168 uint64_t orig
, load
;
5171 orig
= spa_guid(spa
);
5172 load
= spa_load_guid(spa
);
5174 (void) rw_wrlock(&ztest_name_lock
);
5175 error
= spa_change_guid(spa
);
5176 (void) rw_unlock(&ztest_name_lock
);
5181 if (ztest_opts
.zo_verbose
>= 4) {
5182 (void) printf("Changed guid old %llu -> %llu\n",
5183 (u_longlong_t
)orig
, (u_longlong_t
)spa_guid(spa
));
5186 VERIFY3U(orig
, !=, spa_guid(spa
));
5187 VERIFY3U(load
, ==, spa_load_guid(spa
));
5191 * Rename the pool to a different name and then rename it back.
5195 ztest_spa_rename(ztest_ds_t
*zd
, uint64_t id
)
5197 char *oldname
, *newname
;
5200 (void) rw_wrlock(&ztest_name_lock
);
5202 oldname
= ztest_opts
.zo_pool
;
5203 newname
= umem_alloc(strlen(oldname
) + 5, UMEM_NOFAIL
);
5204 (void) strcpy(newname
, oldname
);
5205 (void) strcat(newname
, "_tmp");
5210 VERIFY3U(0, ==, spa_rename(oldname
, newname
));
5213 * Try to open it under the old name, which shouldn't exist
5215 VERIFY3U(ENOENT
, ==, spa_open(oldname
, &spa
, FTAG
));
5218 * Open it under the new name and make sure it's still the same spa_t.
5220 VERIFY3U(0, ==, spa_open(newname
, &spa
, FTAG
));
5222 ASSERT(spa
== ztest_spa
);
5223 spa_close(spa
, FTAG
);
5226 * Rename it back to the original
5228 VERIFY3U(0, ==, spa_rename(newname
, oldname
));
5231 * Make sure it can still be opened
5233 VERIFY3U(0, ==, spa_open(oldname
, &spa
, FTAG
));
5235 ASSERT(spa
== ztest_spa
);
5236 spa_close(spa
, FTAG
);
5238 umem_free(newname
, strlen(newname
) + 1);
5240 (void) rw_unlock(&ztest_name_lock
);
5244 * Verify pool integrity by running zdb.
5247 ztest_run_zdb(char *pool
)
5250 char zdb
[MAXPATHLEN
+ MAXNAMELEN
+ 20];
5258 (void) realpath(getexecname(), zdb
);
5260 /* zdb lives in /usr/sbin, while ztest lives in /usr/bin */
5261 bin
= strstr(zdb
, "/usr/bin/");
5262 ztest
= strstr(bin
, "/ztest");
5264 isalen
= ztest
- isa
;
5268 "/usr/sbin%.*s/zdb -bcc%s%s -G -d -U %s %s",
5271 ztest_opts
.zo_verbose
>= 3 ? "s" : "",
5272 ztest_opts
.zo_verbose
>= 4 ? "v" : "",
5277 if (ztest_opts
.zo_verbose
>= 5)
5278 (void) printf("Executing %s\n", strstr(zdb
, "zdb "));
5280 fp
= popen(zdb
, "r");
5282 while (fgets(zbuf
, sizeof (zbuf
), fp
) != NULL
)
5283 if (ztest_opts
.zo_verbose
>= 3)
5284 (void) printf("%s", zbuf
);
5286 status
= pclose(fp
);
5291 ztest_dump_core
= 0;
5292 if (WIFEXITED(status
))
5293 fatal(0, "'%s' exit code %d", zdb
, WEXITSTATUS(status
));
5295 fatal(0, "'%s' died with signal %d", zdb
, WTERMSIG(status
));
5299 ztest_walk_pool_directory(char *header
)
5303 if (ztest_opts
.zo_verbose
>= 6)
5304 (void) printf("%s\n", header
);
5306 mutex_enter(&spa_namespace_lock
);
5307 while ((spa
= spa_next(spa
)) != NULL
)
5308 if (ztest_opts
.zo_verbose
>= 6)
5309 (void) printf("\t%s\n", spa_name(spa
));
5310 mutex_exit(&spa_namespace_lock
);
5314 ztest_spa_import_export(char *oldname
, char *newname
)
5316 nvlist_t
*config
, *newconfig
;
5321 if (ztest_opts
.zo_verbose
>= 4) {
5322 (void) printf("import/export: old = %s, new = %s\n",
5327 * Clean up from previous runs.
5329 (void) spa_destroy(newname
);
5332 * Get the pool's configuration and guid.
5334 VERIFY3U(0, ==, spa_open(oldname
, &spa
, FTAG
));
5337 * Kick off a scrub to tickle scrub/export races.
5339 if (ztest_random(2) == 0)
5340 (void) spa_scan(spa
, POOL_SCAN_SCRUB
);
5342 pool_guid
= spa_guid(spa
);
5343 spa_close(spa
, FTAG
);
5345 ztest_walk_pool_directory("pools before export");
5350 VERIFY3U(0, ==, spa_export(oldname
, &config
, B_FALSE
, B_FALSE
));
5352 ztest_walk_pool_directory("pools after export");
5357 newconfig
= spa_tryimport(config
);
5358 ASSERT(newconfig
!= NULL
);
5359 nvlist_free(newconfig
);
5362 * Import it under the new name.
5364 error
= spa_import(newname
, config
, NULL
, 0);
5366 dump_nvlist(config
, 0);
5367 fatal(B_FALSE
, "couldn't import pool %s as %s: error %u",
5368 oldname
, newname
, error
);
5371 ztest_walk_pool_directory("pools after import");
5374 * Try to import it again -- should fail with EEXIST.
5376 VERIFY3U(EEXIST
, ==, spa_import(newname
, config
, NULL
, 0));
5379 * Try to import it under a different name -- should fail with EEXIST.
5381 VERIFY3U(EEXIST
, ==, spa_import(oldname
, config
, NULL
, 0));
5384 * Verify that the pool is no longer visible under the old name.
5386 VERIFY3U(ENOENT
, ==, spa_open(oldname
, &spa
, FTAG
));
5389 * Verify that we can open and close the pool using the new name.
5391 VERIFY3U(0, ==, spa_open(newname
, &spa
, FTAG
));
5392 ASSERT(pool_guid
== spa_guid(spa
));
5393 spa_close(spa
, FTAG
);
5395 nvlist_free(config
);
5399 ztest_resume(spa_t
*spa
)
5401 if (spa_suspended(spa
) && ztest_opts
.zo_verbose
>= 6)
5402 (void) printf("resuming from suspended state\n");
5403 spa_vdev_state_enter(spa
, SCL_NONE
);
5404 vdev_clear(spa
, NULL
);
5405 (void) spa_vdev_state_exit(spa
, NULL
, 0);
5406 (void) zio_resume(spa
);
5410 ztest_resume_thread(void *arg
)
5414 while (!ztest_exiting
) {
5415 if (spa_suspended(spa
))
5417 (void) poll(NULL
, 0, 100);
5420 * Periodically change the zfs_compressed_arc_enabled setting.
5422 if (ztest_random(10) == 0)
5423 zfs_compressed_arc_enabled
= ztest_random(2);
5429 ztest_deadman_thread(void *arg
)
5431 ztest_shared_t
*zs
= arg
;
5432 spa_t
*spa
= ztest_spa
;
5433 hrtime_t delta
, total
= 0;
5436 delta
= zs
->zs_thread_stop
- zs
->zs_thread_start
+
5437 MSEC2NSEC(zfs_deadman_synctime_ms
);
5439 (void) poll(NULL
, 0, (int)NSEC2MSEC(delta
));
5442 * If the pool is suspended then fail immediately. Otherwise,
5443 * check to see if the pool is making any progress. If
5444 * vdev_deadman() discovers that there hasn't been any recent
5445 * I/Os then it will end up aborting the tests.
5447 if (spa_suspended(spa
) || spa
->spa_root_vdev
== NULL
) {
5448 fatal(0, "aborting test after %llu seconds because "
5449 "pool has transitioned to a suspended state.",
5450 zfs_deadman_synctime_ms
/ 1000);
5453 vdev_deadman(spa
->spa_root_vdev
);
5455 total
+= zfs_deadman_synctime_ms
/1000;
5456 (void) printf("ztest has been running for %lld seconds\n",
5462 ztest_execute(int test
, ztest_info_t
*zi
, uint64_t id
)
5464 ztest_ds_t
*zd
= &ztest_ds
[id
% ztest_opts
.zo_datasets
];
5465 ztest_shared_callstate_t
*zc
= ZTEST_GET_SHARED_CALLSTATE(test
);
5466 hrtime_t functime
= gethrtime();
5468 for (int i
= 0; i
< zi
->zi_iters
; i
++)
5469 zi
->zi_func(zd
, id
);
5471 functime
= gethrtime() - functime
;
5473 atomic_add_64(&zc
->zc_count
, 1);
5474 atomic_add_64(&zc
->zc_time
, functime
);
5476 if (ztest_opts
.zo_verbose
>= 4) {
5478 (void) dladdr((void *)zi
->zi_func
, &dli
);
5479 (void) printf("%6.2f sec in %s\n",
5480 (double)functime
/ NANOSEC
, dli
.dli_sname
);
5485 ztest_thread(void *arg
)
5488 uint64_t id
= (uintptr_t)arg
;
5489 ztest_shared_t
*zs
= ztest_shared
;
5493 ztest_shared_callstate_t
*zc
;
5495 while ((now
= gethrtime()) < zs
->zs_thread_stop
) {
5497 * See if it's time to force a crash.
5499 if (now
> zs
->zs_thread_kill
)
5503 * If we're getting ENOSPC with some regularity, stop.
5505 if (zs
->zs_enospc_count
> 10)
5509 * Pick a random function to execute.
5511 rand
= ztest_random(ZTEST_FUNCS
);
5512 zi
= &ztest_info
[rand
];
5513 zc
= ZTEST_GET_SHARED_CALLSTATE(rand
);
5514 call_next
= zc
->zc_next
;
5516 if (now
>= call_next
&&
5517 atomic_cas_64(&zc
->zc_next
, call_next
, call_next
+
5518 ztest_random(2 * zi
->zi_interval
[0] + 1)) == call_next
) {
5519 ztest_execute(rand
, zi
, id
);
5527 ztest_dataset_name(char *dsname
, char *pool
, int d
)
5529 (void) snprintf(dsname
, ZFS_MAX_DATASET_NAME_LEN
, "%s/ds_%d", pool
, d
);
5533 ztest_dataset_destroy(int d
)
5535 char name
[ZFS_MAX_DATASET_NAME_LEN
];
5537 ztest_dataset_name(name
, ztest_opts
.zo_pool
, d
);
5539 if (ztest_opts
.zo_verbose
>= 3)
5540 (void) printf("Destroying %s to free up space\n", name
);
5543 * Cleanup any non-standard clones and snapshots. In general,
5544 * ztest thread t operates on dataset (t % zopt_datasets),
5545 * so there may be more than one thing to clean up.
5547 for (int t
= d
; t
< ztest_opts
.zo_threads
;
5548 t
+= ztest_opts
.zo_datasets
) {
5549 ztest_dsl_dataset_cleanup(name
, t
);
5552 (void) dmu_objset_find(name
, ztest_objset_destroy_cb
, NULL
,
5553 DS_FIND_SNAPSHOTS
| DS_FIND_CHILDREN
);
5557 ztest_dataset_dirobj_verify(ztest_ds_t
*zd
)
5559 uint64_t usedobjs
, dirobjs
, scratch
;
5562 * ZTEST_DIROBJ is the object directory for the entire dataset.
5563 * Therefore, the number of objects in use should equal the
5564 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
5565 * If not, we have an object leak.
5567 * Note that we can only check this in ztest_dataset_open(),
5568 * when the open-context and syncing-context values agree.
5569 * That's because zap_count() returns the open-context value,
5570 * while dmu_objset_space() returns the rootbp fill count.
5572 VERIFY3U(0, ==, zap_count(zd
->zd_os
, ZTEST_DIROBJ
, &dirobjs
));
5573 dmu_objset_space(zd
->zd_os
, &scratch
, &scratch
, &usedobjs
, &scratch
);
5574 ASSERT3U(dirobjs
+ 1, ==, usedobjs
);
5578 ztest_dataset_open(int d
)
5580 ztest_ds_t
*zd
= &ztest_ds
[d
];
5581 uint64_t committed_seq
= ZTEST_GET_SHARED_DS(d
)->zd_seq
;
5584 char name
[ZFS_MAX_DATASET_NAME_LEN
];
5587 ztest_dataset_name(name
, ztest_opts
.zo_pool
, d
);
5589 (void) rw_rdlock(&ztest_name_lock
);
5591 error
= ztest_dataset_create(name
);
5592 if (error
== ENOSPC
) {
5593 (void) rw_unlock(&ztest_name_lock
);
5594 ztest_record_enospc(FTAG
);
5597 ASSERT(error
== 0 || error
== EEXIST
);
5599 VERIFY0(dmu_objset_own(name
, DMU_OST_OTHER
, B_FALSE
, zd
, &os
));
5600 (void) rw_unlock(&ztest_name_lock
);
5602 ztest_zd_init(zd
, ZTEST_GET_SHARED_DS(d
), os
);
5604 zilog
= zd
->zd_zilog
;
5606 if (zilog
->zl_header
->zh_claim_lr_seq
!= 0 &&
5607 zilog
->zl_header
->zh_claim_lr_seq
< committed_seq
)
5608 fatal(0, "missing log records: claimed %llu < committed %llu",
5609 zilog
->zl_header
->zh_claim_lr_seq
, committed_seq
);
5611 ztest_dataset_dirobj_verify(zd
);
5613 zil_replay(os
, zd
, ztest_replay_vector
);
5615 ztest_dataset_dirobj_verify(zd
);
5617 if (ztest_opts
.zo_verbose
>= 6)
5618 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
5620 (u_longlong_t
)zilog
->zl_parse_blk_count
,
5621 (u_longlong_t
)zilog
->zl_parse_lr_count
,
5622 (u_longlong_t
)zilog
->zl_replaying_seq
);
5624 zilog
= zil_open(os
, ztest_get_data
);
5626 if (zilog
->zl_replaying_seq
!= 0 &&
5627 zilog
->zl_replaying_seq
< committed_seq
)
5628 fatal(0, "missing log records: replayed %llu < committed %llu",
5629 zilog
->zl_replaying_seq
, committed_seq
);
5635 ztest_dataset_close(int d
)
5637 ztest_ds_t
*zd
= &ztest_ds
[d
];
5639 zil_close(zd
->zd_zilog
);
5640 dmu_objset_disown(zd
->zd_os
, zd
);
5646 * Kick off threads to run tests on all datasets in parallel.
5649 ztest_run(ztest_shared_t
*zs
)
5654 thread_t resume_tid
;
5657 ztest_exiting
= B_FALSE
;
5660 * Initialize parent/child shared state.
5662 VERIFY(_mutex_init(&ztest_vdev_lock
, USYNC_THREAD
, NULL
) == 0);
5663 VERIFY(rwlock_init(&ztest_name_lock
, USYNC_THREAD
, NULL
) == 0);
5665 zs
->zs_thread_start
= gethrtime();
5666 zs
->zs_thread_stop
=
5667 zs
->zs_thread_start
+ ztest_opts
.zo_passtime
* NANOSEC
;
5668 zs
->zs_thread_stop
= MIN(zs
->zs_thread_stop
, zs
->zs_proc_stop
);
5669 zs
->zs_thread_kill
= zs
->zs_thread_stop
;
5670 if (ztest_random(100) < ztest_opts
.zo_killrate
) {
5671 zs
->zs_thread_kill
-=
5672 ztest_random(ztest_opts
.zo_passtime
* NANOSEC
);
5675 (void) _mutex_init(&zcl
.zcl_callbacks_lock
, USYNC_THREAD
, NULL
);
5677 list_create(&zcl
.zcl_callbacks
, sizeof (ztest_cb_data_t
),
5678 offsetof(ztest_cb_data_t
, zcd_node
));
5683 kernel_init(FREAD
| FWRITE
);
5684 VERIFY0(spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
));
5685 spa
->spa_debug
= B_TRUE
;
5686 metaslab_preload_limit
= ztest_random(20) + 1;
5689 dmu_objset_stats_t dds
;
5690 VERIFY0(dmu_objset_own(ztest_opts
.zo_pool
,
5691 DMU_OST_ANY
, B_TRUE
, FTAG
, &os
));
5692 dsl_pool_config_enter(dmu_objset_pool(os
), FTAG
);
5693 dmu_objset_fast_stat(os
, &dds
);
5694 dsl_pool_config_exit(dmu_objset_pool(os
), FTAG
);
5695 zs
->zs_guid
= dds
.dds_guid
;
5696 dmu_objset_disown(os
, FTAG
);
5698 spa
->spa_dedup_ditto
= 2 * ZIO_DEDUPDITTO_MIN
;
5701 * We don't expect the pool to suspend unless maxfaults == 0,
5702 * in which case ztest_fault_inject() temporarily takes away
5703 * the only valid replica.
5705 if (MAXFAULTS() == 0)
5706 spa
->spa_failmode
= ZIO_FAILURE_MODE_WAIT
;
5708 spa
->spa_failmode
= ZIO_FAILURE_MODE_PANIC
;
5711 * Create a thread to periodically resume suspended I/O.
5713 VERIFY(thr_create(0, 0, ztest_resume_thread
, spa
, THR_BOUND
,
5717 * Create a deadman thread to abort() if we hang.
5719 VERIFY(thr_create(0, 0, ztest_deadman_thread
, zs
, THR_BOUND
,
5723 * Verify that we can safely inquire about about any object,
5724 * whether it's allocated or not. To make it interesting,
5725 * we probe a 5-wide window around each power of two.
5726 * This hits all edge cases, including zero and the max.
5728 for (int t
= 0; t
< 64; t
++) {
5729 for (int d
= -5; d
<= 5; d
++) {
5730 error
= dmu_object_info(spa
->spa_meta_objset
,
5731 (1ULL << t
) + d
, NULL
);
5732 ASSERT(error
== 0 || error
== ENOENT
||
5738 * If we got any ENOSPC errors on the previous run, destroy something.
5740 if (zs
->zs_enospc_count
!= 0) {
5741 int d
= ztest_random(ztest_opts
.zo_datasets
);
5742 ztest_dataset_destroy(d
);
5744 zs
->zs_enospc_count
= 0;
5746 tid
= umem_zalloc(ztest_opts
.zo_threads
* sizeof (thread_t
),
5749 if (ztest_opts
.zo_verbose
>= 4)
5750 (void) printf("starting main threads...\n");
5753 * Kick off all the tests that run in parallel.
5755 for (int t
= 0; t
< ztest_opts
.zo_threads
; t
++) {
5756 if (t
< ztest_opts
.zo_datasets
&&
5757 ztest_dataset_open(t
) != 0)
5759 VERIFY(thr_create(0, 0, ztest_thread
, (void *)(uintptr_t)t
,
5760 THR_BOUND
, &tid
[t
]) == 0);
5764 * Wait for all of the tests to complete. We go in reverse order
5765 * so we don't close datasets while threads are still using them.
5767 for (int t
= ztest_opts
.zo_threads
- 1; t
>= 0; t
--) {
5768 VERIFY(thr_join(tid
[t
], NULL
, NULL
) == 0);
5769 if (t
< ztest_opts
.zo_datasets
)
5770 ztest_dataset_close(t
);
5773 txg_wait_synced(spa_get_dsl(spa
), 0);
5775 zs
->zs_alloc
= metaslab_class_get_alloc(spa_normal_class(spa
));
5776 zs
->zs_space
= metaslab_class_get_space(spa_normal_class(spa
));
5777 zfs_dbgmsg_print(FTAG
);
5779 umem_free(tid
, ztest_opts
.zo_threads
* sizeof (thread_t
));
5781 /* Kill the resume thread */
5782 ztest_exiting
= B_TRUE
;
5783 VERIFY(thr_join(resume_tid
, NULL
, NULL
) == 0);
5787 * Right before closing the pool, kick off a bunch of async I/O;
5788 * spa_close() should wait for it to complete.
5790 for (uint64_t object
= 1; object
< 50; object
++) {
5791 dmu_prefetch(spa
->spa_meta_objset
, object
, 0, 0, 1ULL << 20,
5792 ZIO_PRIORITY_SYNC_READ
);
5795 spa_close(spa
, FTAG
);
5798 * Verify that we can loop over all pools.
5800 mutex_enter(&spa_namespace_lock
);
5801 for (spa
= spa_next(NULL
); spa
!= NULL
; spa
= spa_next(spa
))
5802 if (ztest_opts
.zo_verbose
> 3)
5803 (void) printf("spa_next: found %s\n", spa_name(spa
));
5804 mutex_exit(&spa_namespace_lock
);
5807 * Verify that we can export the pool and reimport it under a
5810 if (ztest_random(2) == 0) {
5811 char name
[ZFS_MAX_DATASET_NAME_LEN
];
5812 (void) snprintf(name
, sizeof (name
), "%s_import",
5813 ztest_opts
.zo_pool
);
5814 ztest_spa_import_export(ztest_opts
.zo_pool
, name
);
5815 ztest_spa_import_export(name
, ztest_opts
.zo_pool
);
5820 list_destroy(&zcl
.zcl_callbacks
);
5822 (void) _mutex_destroy(&zcl
.zcl_callbacks_lock
);
5824 (void) rwlock_destroy(&ztest_name_lock
);
5825 (void) _mutex_destroy(&ztest_vdev_lock
);
5831 ztest_ds_t
*zd
= &ztest_ds
[0];
5835 if (ztest_opts
.zo_verbose
>= 3)
5836 (void) printf("testing spa_freeze()...\n");
5838 kernel_init(FREAD
| FWRITE
);
5839 VERIFY3U(0, ==, spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
));
5840 VERIFY3U(0, ==, ztest_dataset_open(0));
5841 spa
->spa_debug
= B_TRUE
;
5845 * Force the first log block to be transactionally allocated.
5846 * We have to do this before we freeze the pool -- otherwise
5847 * the log chain won't be anchored.
5849 while (BP_IS_HOLE(&zd
->zd_zilog
->zl_header
->zh_log
)) {
5850 ztest_dmu_object_alloc_free(zd
, 0);
5851 zil_commit(zd
->zd_zilog
, 0);
5854 txg_wait_synced(spa_get_dsl(spa
), 0);
5857 * Freeze the pool. This stops spa_sync() from doing anything,
5858 * so that the only way to record changes from now on is the ZIL.
5863 * Because it is hard to predict how much space a write will actually
5864 * require beforehand, we leave ourselves some fudge space to write over
5867 uint64_t capacity
= metaslab_class_get_space(spa_normal_class(spa
)) / 2;
5870 * Run tests that generate log records but don't alter the pool config
5871 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
5872 * We do a txg_wait_synced() after each iteration to force the txg
5873 * to increase well beyond the last synced value in the uberblock.
5874 * The ZIL should be OK with that.
5876 * Run a random number of times less than zo_maxloops and ensure we do
5877 * not run out of space on the pool.
5879 while (ztest_random(10) != 0 &&
5880 numloops
++ < ztest_opts
.zo_maxloops
&&
5881 metaslab_class_get_alloc(spa_normal_class(spa
)) < capacity
) {
5883 ztest_od_init(&od
, 0, FTAG
, 0, DMU_OT_UINT64_OTHER
, 0, 0);
5884 VERIFY0(ztest_object_init(zd
, &od
, sizeof (od
), B_FALSE
));
5885 ztest_io(zd
, od
.od_object
,
5886 ztest_random(ZTEST_RANGE_LOCKS
) << SPA_MAXBLOCKSHIFT
);
5887 txg_wait_synced(spa_get_dsl(spa
), 0);
5891 * Commit all of the changes we just generated.
5893 zil_commit(zd
->zd_zilog
, 0);
5894 txg_wait_synced(spa_get_dsl(spa
), 0);
5897 * Close our dataset and close the pool.
5899 ztest_dataset_close(0);
5900 spa_close(spa
, FTAG
);
5904 * Open and close the pool and dataset to induce log replay.
5906 kernel_init(FREAD
| FWRITE
);
5907 VERIFY3U(0, ==, spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
));
5908 ASSERT(spa_freeze_txg(spa
) == UINT64_MAX
);
5909 VERIFY3U(0, ==, ztest_dataset_open(0));
5910 ztest_dataset_close(0);
5912 spa
->spa_debug
= B_TRUE
;
5914 txg_wait_synced(spa_get_dsl(spa
), 0);
5915 ztest_reguid(NULL
, 0);
5917 spa_close(spa
, FTAG
);
5922 print_time(hrtime_t t
, char *timebuf
)
5924 hrtime_t s
= t
/ NANOSEC
;
5925 hrtime_t m
= s
/ 60;
5926 hrtime_t h
= m
/ 60;
5927 hrtime_t d
= h
/ 24;
5936 (void) sprintf(timebuf
,
5937 "%llud%02lluh%02llum%02llus", d
, h
, m
, s
);
5939 (void) sprintf(timebuf
, "%lluh%02llum%02llus", h
, m
, s
);
5941 (void) sprintf(timebuf
, "%llum%02llus", m
, s
);
5943 (void) sprintf(timebuf
, "%llus", s
);
5951 VERIFY(nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) == 0);
5952 if (ztest_random(2) == 0)
5954 VERIFY(nvlist_add_uint64(props
, "autoreplace", 1) == 0);
5960 * Create a storage pool with the given name and initial vdev size.
5961 * Then test spa_freeze() functionality.
5964 ztest_init(ztest_shared_t
*zs
)
5967 nvlist_t
*nvroot
, *props
;
5969 VERIFY(_mutex_init(&ztest_vdev_lock
, USYNC_THREAD
, NULL
) == 0);
5970 VERIFY(rwlock_init(&ztest_name_lock
, USYNC_THREAD
, NULL
) == 0);
5972 kernel_init(FREAD
| FWRITE
);
5975 * Create the storage pool.
5977 (void) spa_destroy(ztest_opts
.zo_pool
);
5978 ztest_shared
->zs_vdev_next_leaf
= 0;
5980 zs
->zs_mirrors
= ztest_opts
.zo_mirrors
;
5981 nvroot
= make_vdev_root(NULL
, NULL
, NULL
, ztest_opts
.zo_vdev_size
, 0,
5982 0, ztest_opts
.zo_raidz
, zs
->zs_mirrors
, 1);
5983 props
= make_random_props();
5984 for (int i
= 0; i
< SPA_FEATURES
; i
++) {
5986 (void) snprintf(buf
, sizeof (buf
), "feature@%s",
5987 spa_feature_table
[i
].fi_uname
);
5988 VERIFY3U(0, ==, nvlist_add_uint64(props
, buf
, 0));
5990 VERIFY3U(0, ==, spa_create(ztest_opts
.zo_pool
, nvroot
, props
, NULL
));
5991 nvlist_free(nvroot
);
5994 VERIFY3U(0, ==, spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
));
5995 zs
->zs_metaslab_sz
=
5996 1ULL << spa
->spa_root_vdev
->vdev_child
[0]->vdev_ms_shift
;
5998 spa_close(spa
, FTAG
);
6002 ztest_run_zdb(ztest_opts
.zo_pool
);
6006 ztest_run_zdb(ztest_opts
.zo_pool
);
6008 (void) rwlock_destroy(&ztest_name_lock
);
6009 (void) _mutex_destroy(&ztest_vdev_lock
);
6015 static char ztest_name_data
[] = "/tmp/ztest.data.XXXXXX";
6017 ztest_fd_data
= mkstemp(ztest_name_data
);
6018 ASSERT3S(ztest_fd_data
, >=, 0);
6019 (void) unlink(ztest_name_data
);
6024 shared_data_size(ztest_shared_hdr_t
*hdr
)
6028 size
= hdr
->zh_hdr_size
;
6029 size
+= hdr
->zh_opts_size
;
6030 size
+= hdr
->zh_size
;
6031 size
+= hdr
->zh_stats_size
* hdr
->zh_stats_count
;
6032 size
+= hdr
->zh_ds_size
* hdr
->zh_ds_count
;
6041 ztest_shared_hdr_t
*hdr
;
6043 hdr
= (void *)mmap(0, P2ROUNDUP(sizeof (*hdr
), getpagesize()),
6044 PROT_READ
| PROT_WRITE
, MAP_SHARED
, ztest_fd_data
, 0);
6045 ASSERT(hdr
!= MAP_FAILED
);
6047 VERIFY3U(0, ==, ftruncate(ztest_fd_data
, sizeof (ztest_shared_hdr_t
)));
6049 hdr
->zh_hdr_size
= sizeof (ztest_shared_hdr_t
);
6050 hdr
->zh_opts_size
= sizeof (ztest_shared_opts_t
);
6051 hdr
->zh_size
= sizeof (ztest_shared_t
);
6052 hdr
->zh_stats_size
= sizeof (ztest_shared_callstate_t
);
6053 hdr
->zh_stats_count
= ZTEST_FUNCS
;
6054 hdr
->zh_ds_size
= sizeof (ztest_shared_ds_t
);
6055 hdr
->zh_ds_count
= ztest_opts
.zo_datasets
;
6057 size
= shared_data_size(hdr
);
6058 VERIFY3U(0, ==, ftruncate(ztest_fd_data
, size
));
6060 (void) munmap((caddr_t
)hdr
, P2ROUNDUP(sizeof (*hdr
), getpagesize()));
6067 ztest_shared_hdr_t
*hdr
;
6070 hdr
= (void *)mmap(0, P2ROUNDUP(sizeof (*hdr
), getpagesize()),
6071 PROT_READ
, MAP_SHARED
, ztest_fd_data
, 0);
6072 ASSERT(hdr
!= MAP_FAILED
);
6074 size
= shared_data_size(hdr
);
6076 (void) munmap((caddr_t
)hdr
, P2ROUNDUP(sizeof (*hdr
), getpagesize()));
6077 hdr
= ztest_shared_hdr
= (void *)mmap(0, P2ROUNDUP(size
, getpagesize()),
6078 PROT_READ
| PROT_WRITE
, MAP_SHARED
, ztest_fd_data
, 0);
6079 ASSERT(hdr
!= MAP_FAILED
);
6080 buf
= (uint8_t *)hdr
;
6082 offset
= hdr
->zh_hdr_size
;
6083 ztest_shared_opts
= (void *)&buf
[offset
];
6084 offset
+= hdr
->zh_opts_size
;
6085 ztest_shared
= (void *)&buf
[offset
];
6086 offset
+= hdr
->zh_size
;
6087 ztest_shared_callstate
= (void *)&buf
[offset
];
6088 offset
+= hdr
->zh_stats_size
* hdr
->zh_stats_count
;
6089 ztest_shared_ds
= (void *)&buf
[offset
];
6093 exec_child(char *cmd
, char *libpath
, boolean_t ignorekill
, int *statusp
)
6097 char *cmdbuf
= NULL
;
6102 cmdbuf
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
6103 (void) strlcpy(cmdbuf
, getexecname(), MAXPATHLEN
);
6108 fatal(1, "fork failed");
6110 if (pid
== 0) { /* child */
6111 char *emptyargv
[2] = { cmd
, NULL
};
6112 char fd_data_str
[12];
6114 struct rlimit rl
= { 1024, 1024 };
6115 (void) setrlimit(RLIMIT_NOFILE
, &rl
);
6117 (void) close(ztest_fd_rand
);
6119 snprintf(fd_data_str
, 12, "%d", ztest_fd_data
));
6120 VERIFY0(setenv("ZTEST_FD_DATA", fd_data_str
, 1));
6122 (void) enable_extended_FILE_stdio(-1, -1);
6123 if (libpath
!= NULL
)
6124 VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath
, 1));
6125 (void) execv(cmd
, emptyargv
);
6126 ztest_dump_core
= B_FALSE
;
6127 fatal(B_TRUE
, "exec failed: %s", cmd
);
6130 if (cmdbuf
!= NULL
) {
6131 umem_free(cmdbuf
, MAXPATHLEN
);
6135 while (waitpid(pid
, &status
, 0) != pid
)
6137 if (statusp
!= NULL
)
6140 if (WIFEXITED(status
)) {
6141 if (WEXITSTATUS(status
) != 0) {
6142 (void) fprintf(stderr
, "child exited with code %d\n",
6143 WEXITSTATUS(status
));
6147 } else if (WIFSIGNALED(status
)) {
6148 if (!ignorekill
|| WTERMSIG(status
) != SIGKILL
) {
6149 (void) fprintf(stderr
, "child died with signal %d\n",
6155 (void) fprintf(stderr
, "something strange happened to child\n");
6162 ztest_run_init(void)
6164 ztest_shared_t
*zs
= ztest_shared
;
6166 ASSERT(ztest_opts
.zo_init
!= 0);
6169 * Blow away any existing copy of zpool.cache
6171 (void) remove(spa_config_path
);
6174 * Create and initialize our storage pool.
6176 for (int i
= 1; i
<= ztest_opts
.zo_init
; i
++) {
6177 bzero(zs
, sizeof (ztest_shared_t
));
6178 if (ztest_opts
.zo_verbose
>= 3 &&
6179 ztest_opts
.zo_init
!= 1) {
6180 (void) printf("ztest_init(), pass %d\n", i
);
6187 main(int argc
, char **argv
)
6195 ztest_shared_callstate_t
*zc
;
6201 char *fd_data_str
= getenv("ZTEST_FD_DATA");
6203 (void) setvbuf(stdout
, NULL
, _IOLBF
, 0);
6205 dprintf_setup(&argc
, argv
);
6206 zfs_deadman_synctime_ms
= 300000;
6208 ztest_fd_rand
= open("/dev/urandom", O_RDONLY
);
6209 ASSERT3S(ztest_fd_rand
, >=, 0);
6212 process_options(argc
, argv
);
6217 bcopy(&ztest_opts
, ztest_shared_opts
,
6218 sizeof (*ztest_shared_opts
));
6220 ztest_fd_data
= atoi(fd_data_str
);
6222 bcopy(ztest_shared_opts
, &ztest_opts
, sizeof (ztest_opts
));
6224 ASSERT3U(ztest_opts
.zo_datasets
, ==, ztest_shared_hdr
->zh_ds_count
);
6226 /* Override location of zpool.cache */
6227 VERIFY3U(asprintf((char **)&spa_config_path
, "%s/zpool.cache",
6228 ztest_opts
.zo_dir
), !=, -1);
6230 ztest_ds
= umem_alloc(ztest_opts
.zo_datasets
* sizeof (ztest_ds_t
),
6235 metaslab_gang_bang
= ztest_opts
.zo_metaslab_gang_bang
;
6236 metaslab_df_alloc_threshold
=
6237 zs
->zs_metaslab_df_alloc_threshold
;
6246 hasalt
= (strlen(ztest_opts
.zo_alt_ztest
) != 0);
6248 if (ztest_opts
.zo_verbose
>= 1) {
6249 (void) printf("%llu vdevs, %d datasets, %d threads,"
6250 " %llu seconds...\n",
6251 (u_longlong_t
)ztest_opts
.zo_vdevs
,
6252 ztest_opts
.zo_datasets
,
6253 ztest_opts
.zo_threads
,
6254 (u_longlong_t
)ztest_opts
.zo_time
);
6257 cmd
= umem_alloc(MAXNAMELEN
, UMEM_NOFAIL
);
6258 (void) strlcpy(cmd
, getexecname(), MAXNAMELEN
);
6260 zs
->zs_do_init
= B_TRUE
;
6261 if (strlen(ztest_opts
.zo_alt_ztest
) != 0) {
6262 if (ztest_opts
.zo_verbose
>= 1) {
6263 (void) printf("Executing older ztest for "
6264 "initialization: %s\n", ztest_opts
.zo_alt_ztest
);
6266 VERIFY(!exec_child(ztest_opts
.zo_alt_ztest
,
6267 ztest_opts
.zo_alt_libpath
, B_FALSE
, NULL
));
6269 VERIFY(!exec_child(NULL
, NULL
, B_FALSE
, NULL
));
6271 zs
->zs_do_init
= B_FALSE
;
6273 zs
->zs_proc_start
= gethrtime();
6274 zs
->zs_proc_stop
= zs
->zs_proc_start
+ ztest_opts
.zo_time
* NANOSEC
;
6276 for (int f
= 0; f
< ZTEST_FUNCS
; f
++) {
6277 zi
= &ztest_info
[f
];
6278 zc
= ZTEST_GET_SHARED_CALLSTATE(f
);
6279 if (zs
->zs_proc_start
+ zi
->zi_interval
[0] > zs
->zs_proc_stop
)
6280 zc
->zc_next
= UINT64_MAX
;
6282 zc
->zc_next
= zs
->zs_proc_start
+
6283 ztest_random(2 * zi
->zi_interval
[0] + 1);
6287 * Run the tests in a loop. These tests include fault injection
6288 * to verify that self-healing data works, and forced crashes
6289 * to verify that we never lose on-disk consistency.
6291 while (gethrtime() < zs
->zs_proc_stop
) {
6296 * Initialize the workload counters for each function.
6298 for (int f
= 0; f
< ZTEST_FUNCS
; f
++) {
6299 zc
= ZTEST_GET_SHARED_CALLSTATE(f
);
6304 /* Set the allocation switch size */
6305 zs
->zs_metaslab_df_alloc_threshold
=
6306 ztest_random(zs
->zs_metaslab_sz
/ 4) + 1;
6308 if (!hasalt
|| ztest_random(2) == 0) {
6309 if (hasalt
&& ztest_opts
.zo_verbose
>= 1) {
6310 (void) printf("Executing newer ztest: %s\n",
6314 killed
= exec_child(cmd
, NULL
, B_TRUE
, &status
);
6316 if (hasalt
&& ztest_opts
.zo_verbose
>= 1) {
6317 (void) printf("Executing older ztest: %s\n",
6318 ztest_opts
.zo_alt_ztest
);
6321 killed
= exec_child(ztest_opts
.zo_alt_ztest
,
6322 ztest_opts
.zo_alt_libpath
, B_TRUE
, &status
);
6329 if (ztest_opts
.zo_verbose
>= 1) {
6330 hrtime_t now
= gethrtime();
6332 now
= MIN(now
, zs
->zs_proc_stop
);
6333 print_time(zs
->zs_proc_stop
- now
, timebuf
);
6334 nicenum(zs
->zs_space
, numbuf
);
6336 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
6337 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
6339 WIFEXITED(status
) ? "Complete" : "SIGKILL",
6340 (u_longlong_t
)zs
->zs_enospc_count
,
6341 100.0 * zs
->zs_alloc
/ zs
->zs_space
,
6343 100.0 * (now
- zs
->zs_proc_start
) /
6344 (ztest_opts
.zo_time
* NANOSEC
), timebuf
);
6347 if (ztest_opts
.zo_verbose
>= 2) {
6348 (void) printf("\nWorkload summary:\n\n");
6349 (void) printf("%7s %9s %s\n",
6350 "Calls", "Time", "Function");
6351 (void) printf("%7s %9s %s\n",
6352 "-----", "----", "--------");
6353 for (int f
= 0; f
< ZTEST_FUNCS
; f
++) {
6356 zi
= &ztest_info
[f
];
6357 zc
= ZTEST_GET_SHARED_CALLSTATE(f
);
6358 print_time(zc
->zc_time
, timebuf
);
6359 (void) dladdr((void *)zi
->zi_func
, &dli
);
6360 (void) printf("%7llu %9s %s\n",
6361 (u_longlong_t
)zc
->zc_count
, timebuf
,
6364 (void) printf("\n");
6368 * It's possible that we killed a child during a rename test,
6369 * in which case we'll have a 'ztest_tmp' pool lying around
6370 * instead of 'ztest'. Do a blind rename in case this happened.
6373 if (spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
) == 0) {
6374 spa_close(spa
, FTAG
);
6376 char tmpname
[ZFS_MAX_DATASET_NAME_LEN
];
6378 kernel_init(FREAD
| FWRITE
);
6379 (void) snprintf(tmpname
, sizeof (tmpname
), "%s_tmp",
6380 ztest_opts
.zo_pool
);
6381 (void) spa_rename(tmpname
, ztest_opts
.zo_pool
);
6385 ztest_run_zdb(ztest_opts
.zo_pool
);
6388 if (ztest_opts
.zo_verbose
>= 1) {
6390 (void) printf("%d runs of older ztest: %s\n", older
,
6391 ztest_opts
.zo_alt_ztest
);
6392 (void) printf("%d runs of newer ztest: %s\n", newer
,
6395 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
6396 kills
, iters
- kills
, (100.0 * kills
) / MAX(1, iters
));
6399 umem_free(cmd
, MAXNAMELEN
);