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, 2018 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]
27 * Copyright 2017 Joyent, Inc.
28 * Copyright (c) 2017, Intel Corporation.
32 * The objective of this program is to provide a DMU/ZAP/SPA stress test
33 * that runs entirely in userland, is easy to use, and easy to extend.
35 * The overall design of the ztest program is as follows:
37 * (1) For each major functional area (e.g. adding vdevs to a pool,
38 * creating and destroying datasets, reading and writing objects, etc)
39 * we have a simple routine to test that functionality. These
40 * individual routines do not have to do anything "stressful".
42 * (2) We turn these simple functionality tests into a stress test by
43 * running them all in parallel, with as many threads as desired,
44 * and spread across as many datasets, objects, and vdevs as desired.
46 * (3) While all this is happening, we inject faults into the pool to
47 * verify that self-healing data really works.
49 * (4) Every time we open a dataset, we change its checksum and compression
50 * functions. Thus even individual objects vary from block to block
51 * in which checksum they use and whether they're compressed.
53 * (5) To verify that we never lose on-disk consistency after a crash,
54 * we run the entire test in a child of the main process.
55 * At random times, the child self-immolates with a SIGKILL.
56 * This is the software equivalent of pulling the power cord.
57 * The parent then runs the test again, using the existing
58 * storage pool, as many times as desired. If backwards compatibility
59 * testing is enabled ztest will sometimes run the "older" version
60 * of ztest after a SIGKILL.
62 * (6) To verify that we don't have future leaks or temporal incursions,
63 * many of the functional tests record the transaction group number
64 * as part of their data. When reading old data, they verify that
65 * the transaction group number is less than the current, open txg.
66 * If you add a new test, please do this if applicable.
68 * (7) Threads are created with a reduced stack size, for sanity checking.
69 * Therefore, it's important not to allocate huge buffers on the stack.
71 * When run with no arguments, ztest runs for about five minutes and
72 * produces no output if successful. To get a little bit of information,
73 * specify -V. To get more information, specify -VV, and so on.
75 * To turn this into an overnight stress test, use -T to specify run time.
77 * You can ask more more vdevs [-v], datasets [-d], or threads [-t]
78 * to increase the pool capacity, fanout, and overall stress level.
80 * Use the -k option to set the desired frequency of kills.
82 * When ztest invokes itself it passes all relevant information through a
83 * temporary file which is mmap-ed in the child process. This allows shared
84 * memory to survive the exec syscall. The ztest_shared_hdr_t struct is always
85 * stored at offset 0 of this file and contains information on the size and
86 * number of shared structures in the file. The information stored in this file
87 * must remain backwards compatible with older versions of ztest so that
88 * ztest can invoke them during backwards compatibility testing (-B).
91 #include <sys/zfs_context.h>
97 #include <sys/dmu_objset.h>
100 #include <sys/time.h>
101 #include <sys/wait.h>
102 #include <sys/mman.h>
103 #include <sys/resource.h>
106 #include <sys/zil_impl.h>
107 #include <sys/zfs_rlock.h>
108 #include <sys/vdev_impl.h>
109 #include <sys/vdev_file.h>
110 #include <sys/spa_impl.h>
111 #include <sys/metaslab_impl.h>
112 #include <sys/dsl_prop.h>
113 #include <sys/dsl_dataset.h>
114 #include <sys/dsl_destroy.h>
115 #include <sys/dsl_scan.h>
116 #include <sys/zio_checksum.h>
117 #include <sys/refcount.h>
118 #include <sys/zfeature.h>
119 #include <sys/dsl_userhold.h>
122 #include <stdio_ext.h>
129 #include <sys/fs/zfs.h>
130 #include <zfs_fletcher.h>
131 #include <libnvpair.h>
133 #include <sys/crypto/icp.h>
135 #include <execinfo.h> /* for backtrace() */
138 static int ztest_fd_data
= -1;
139 static int ztest_fd_rand
= -1;
141 typedef struct ztest_shared_hdr
{
142 uint64_t zh_hdr_size
;
143 uint64_t zh_opts_size
;
145 uint64_t zh_stats_size
;
146 uint64_t zh_stats_count
;
148 uint64_t zh_ds_count
;
149 } ztest_shared_hdr_t
;
151 static ztest_shared_hdr_t
*ztest_shared_hdr
;
153 enum ztest_class_state
{
154 ZTEST_VDEV_CLASS_OFF
,
159 typedef struct ztest_shared_opts
{
160 char zo_pool
[ZFS_MAX_DATASET_NAME_LEN
];
161 char zo_dir
[ZFS_MAX_DATASET_NAME_LEN
];
162 char zo_alt_ztest
[MAXNAMELEN
];
163 char zo_alt_libpath
[MAXNAMELEN
];
165 uint64_t zo_vdevtime
;
173 uint64_t zo_passtime
;
174 uint64_t zo_killrate
;
178 uint64_t zo_maxloops
;
179 uint64_t zo_metaslab_force_ganging
;
181 int zo_special_vdevs
;
182 } ztest_shared_opts_t
;
184 static const ztest_shared_opts_t ztest_opts_defaults
= {
185 .zo_pool
= { 'z', 't', 'e', 's', 't', '\0' },
186 .zo_dir
= { '/', 't', 'm', 'p', '\0' },
187 .zo_alt_ztest
= { '\0' },
188 .zo_alt_libpath
= { '\0' },
190 .zo_ashift
= SPA_MINBLOCKSHIFT
,
193 .zo_raidz_parity
= 1,
194 .zo_vdev_size
= SPA_MINDEVSIZE
* 4, /* 256m default size */
197 .zo_passtime
= 60, /* 60 seconds */
198 .zo_killrate
= 70, /* 70% kill rate */
202 .zo_time
= 300, /* 5 minutes */
203 .zo_maxloops
= 50, /* max loops during spa_freeze() */
204 .zo_metaslab_force_ganging
= 32 << 10,
205 .zo_special_vdevs
= ZTEST_VDEV_CLASS_RND
,
208 extern uint64_t metaslab_force_ganging
;
209 extern uint64_t metaslab_df_alloc_threshold
;
210 extern unsigned long zfs_deadman_synctime_ms
;
211 extern int metaslab_preload_limit
;
212 extern boolean_t zfs_compressed_arc_enabled
;
213 extern int zfs_abd_scatter_enabled
;
214 extern int dmu_object_alloc_chunk_shift
;
215 extern boolean_t zfs_force_some_double_word_sm_entries
;
216 extern unsigned long zio_decompress_fail_fraction
;
217 extern unsigned long zfs_reconstruct_indirect_damage_fraction
;
219 static ztest_shared_opts_t
*ztest_shared_opts
;
220 static ztest_shared_opts_t ztest_opts
;
221 static char *ztest_wkeydata
= "abcdefghijklmnopqrstuvwxyz012345";
223 typedef struct ztest_shared_ds
{
227 static ztest_shared_ds_t
*ztest_shared_ds
;
228 #define ZTEST_GET_SHARED_DS(d) (&ztest_shared_ds[d])
230 #define BT_MAGIC 0x123456789abcdefULL
231 #define MAXFAULTS(zs) \
232 (MAX((zs)->zs_mirrors, 1) * (ztest_opts.zo_raidz_parity + 1) - 1)
236 ZTEST_IO_WRITE_PATTERN
,
237 ZTEST_IO_WRITE_ZEROES
,
244 typedef struct ztest_block_tag
{
248 uint64_t bt_dnodesize
;
255 typedef struct bufwad
{
273 #define ZTEST_RANGE_LOCKS 64
274 #define ZTEST_OBJECT_LOCKS 64
277 * Object descriptor. Used as a template for object lookup/create/remove.
279 typedef struct ztest_od
{
282 dmu_object_type_t od_type
;
283 dmu_object_type_t od_crtype
;
284 uint64_t od_blocksize
;
285 uint64_t od_crblocksize
;
286 uint64_t od_crdnodesize
;
289 char od_name
[ZFS_MAX_DATASET_NAME_LEN
];
295 typedef struct ztest_ds
{
296 ztest_shared_ds_t
*zd_shared
;
298 pthread_rwlock_t zd_zilog_lock
;
300 ztest_od_t
*zd_od
; /* debugging aid */
301 char zd_name
[ZFS_MAX_DATASET_NAME_LEN
];
302 kmutex_t zd_dirobj_lock
;
303 rll_t zd_object_lock
[ZTEST_OBJECT_LOCKS
];
304 zll_t zd_range_lock
[ZTEST_RANGE_LOCKS
];
308 * Per-iteration state.
310 typedef void ztest_func_t(ztest_ds_t
*zd
, uint64_t id
);
312 typedef struct ztest_info
{
313 ztest_func_t
*zi_func
; /* test function */
314 uint64_t zi_iters
; /* iterations per execution */
315 uint64_t *zi_interval
; /* execute every <interval> seconds */
316 const char *zi_funcname
; /* name of test function */
319 typedef struct ztest_shared_callstate
{
320 uint64_t zc_count
; /* per-pass count */
321 uint64_t zc_time
; /* per-pass time */
322 uint64_t zc_next
; /* next time to call this function */
323 } ztest_shared_callstate_t
;
325 static ztest_shared_callstate_t
*ztest_shared_callstate
;
326 #define ZTEST_GET_SHARED_CALLSTATE(c) (&ztest_shared_callstate[c])
328 ztest_func_t ztest_dmu_read_write
;
329 ztest_func_t ztest_dmu_write_parallel
;
330 ztest_func_t ztest_dmu_object_alloc_free
;
331 ztest_func_t ztest_dmu_object_next_chunk
;
332 ztest_func_t ztest_dmu_commit_callbacks
;
333 ztest_func_t ztest_zap
;
334 ztest_func_t ztest_zap_parallel
;
335 ztest_func_t ztest_zil_commit
;
336 ztest_func_t ztest_zil_remount
;
337 ztest_func_t ztest_dmu_read_write_zcopy
;
338 ztest_func_t ztest_dmu_objset_create_destroy
;
339 ztest_func_t ztest_dmu_prealloc
;
340 ztest_func_t ztest_fzap
;
341 ztest_func_t ztest_dmu_snapshot_create_destroy
;
342 ztest_func_t ztest_dsl_prop_get_set
;
343 ztest_func_t ztest_spa_prop_get_set
;
344 ztest_func_t ztest_spa_create_destroy
;
345 ztest_func_t ztest_fault_inject
;
346 ztest_func_t ztest_ddt_repair
;
347 ztest_func_t ztest_dmu_snapshot_hold
;
348 ztest_func_t ztest_mmp_enable_disable
;
349 ztest_func_t ztest_spa_rename
;
350 ztest_func_t ztest_scrub
;
351 ztest_func_t ztest_dsl_dataset_promote_busy
;
352 ztest_func_t ztest_vdev_attach_detach
;
353 ztest_func_t ztest_vdev_LUN_growth
;
354 ztest_func_t ztest_vdev_add_remove
;
355 ztest_func_t ztest_vdev_class_add
;
356 ztest_func_t ztest_vdev_aux_add_remove
;
357 ztest_func_t ztest_split_pool
;
358 ztest_func_t ztest_reguid
;
359 ztest_func_t ztest_spa_upgrade
;
360 ztest_func_t ztest_device_removal
;
361 ztest_func_t ztest_remap_blocks
;
362 ztest_func_t ztest_spa_checkpoint_create_discard
;
363 ztest_func_t ztest_fletcher
;
364 ztest_func_t ztest_fletcher_incr
;
365 ztest_func_t ztest_verify_dnode_bt
;
367 uint64_t zopt_always
= 0ULL * NANOSEC
; /* all the time */
368 uint64_t zopt_incessant
= 1ULL * NANOSEC
/ 10; /* every 1/10 second */
369 uint64_t zopt_often
= 1ULL * NANOSEC
; /* every second */
370 uint64_t zopt_sometimes
= 10ULL * NANOSEC
; /* every 10 seconds */
371 uint64_t zopt_rarely
= 60ULL * NANOSEC
; /* every 60 seconds */
373 #define ZTI_INIT(func, iters, interval) \
374 { .zi_func = (func), \
375 .zi_iters = (iters), \
376 .zi_interval = (interval), \
377 .zi_funcname = # func }
379 ztest_info_t ztest_info
[] = {
380 ZTI_INIT(ztest_dmu_read_write
, 1, &zopt_always
),
381 ZTI_INIT(ztest_dmu_write_parallel
, 10, &zopt_always
),
382 ZTI_INIT(ztest_dmu_object_alloc_free
, 1, &zopt_always
),
383 ZTI_INIT(ztest_dmu_object_next_chunk
, 1, &zopt_sometimes
),
384 ZTI_INIT(ztest_dmu_commit_callbacks
, 1, &zopt_always
),
385 ZTI_INIT(ztest_zap
, 30, &zopt_always
),
386 ZTI_INIT(ztest_zap_parallel
, 100, &zopt_always
),
387 ZTI_INIT(ztest_split_pool
, 1, &zopt_always
),
388 ZTI_INIT(ztest_zil_commit
, 1, &zopt_incessant
),
389 ZTI_INIT(ztest_zil_remount
, 1, &zopt_sometimes
),
390 ZTI_INIT(ztest_dmu_read_write_zcopy
, 1, &zopt_often
),
391 ZTI_INIT(ztest_dmu_objset_create_destroy
, 1, &zopt_often
),
392 ZTI_INIT(ztest_dsl_prop_get_set
, 1, &zopt_often
),
393 ZTI_INIT(ztest_spa_prop_get_set
, 1, &zopt_sometimes
),
395 ZTI_INIT(ztest_dmu_prealloc
, 1, &zopt_sometimes
),
397 ZTI_INIT(ztest_fzap
, 1, &zopt_sometimes
),
398 ZTI_INIT(ztest_dmu_snapshot_create_destroy
, 1, &zopt_sometimes
),
399 ZTI_INIT(ztest_spa_create_destroy
, 1, &zopt_sometimes
),
400 ZTI_INIT(ztest_fault_inject
, 1, &zopt_sometimes
),
401 ZTI_INIT(ztest_ddt_repair
, 1, &zopt_sometimes
),
402 ZTI_INIT(ztest_dmu_snapshot_hold
, 1, &zopt_sometimes
),
403 ZTI_INIT(ztest_mmp_enable_disable
, 1, &zopt_sometimes
),
404 ZTI_INIT(ztest_reguid
, 1, &zopt_rarely
),
405 ZTI_INIT(ztest_spa_rename
, 1, &zopt_rarely
),
406 ZTI_INIT(ztest_scrub
, 1, &zopt_rarely
),
407 ZTI_INIT(ztest_spa_upgrade
, 1, &zopt_rarely
),
408 ZTI_INIT(ztest_dsl_dataset_promote_busy
, 1, &zopt_rarely
),
409 ZTI_INIT(ztest_vdev_attach_detach
, 1, &zopt_sometimes
),
410 ZTI_INIT(ztest_vdev_LUN_growth
, 1, &zopt_rarely
),
411 ZTI_INIT(ztest_vdev_add_remove
, 1, &ztest_opts
.zo_vdevtime
),
412 ZTI_INIT(ztest_vdev_class_add
, 1, &ztest_opts
.zo_vdevtime
),
413 ZTI_INIT(ztest_vdev_aux_add_remove
, 1, &ztest_opts
.zo_vdevtime
),
414 ZTI_INIT(ztest_device_removal
, 1, &zopt_sometimes
),
415 ZTI_INIT(ztest_remap_blocks
, 1, &zopt_sometimes
),
416 ZTI_INIT(ztest_spa_checkpoint_create_discard
, 1, &zopt_rarely
),
417 ZTI_INIT(ztest_fletcher
, 1, &zopt_rarely
),
418 ZTI_INIT(ztest_fletcher_incr
, 1, &zopt_rarely
),
419 ZTI_INIT(ztest_verify_dnode_bt
, 1, &zopt_sometimes
),
422 #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
425 * The following struct is used to hold a list of uncalled commit callbacks.
426 * The callbacks are ordered by txg number.
428 typedef struct ztest_cb_list
{
429 kmutex_t zcl_callbacks_lock
;
430 list_t zcl_callbacks
;
434 * Stuff we need to share writably between parent and child.
436 typedef struct ztest_shared
{
437 boolean_t zs_do_init
;
438 hrtime_t zs_proc_start
;
439 hrtime_t zs_proc_stop
;
440 hrtime_t zs_thread_start
;
441 hrtime_t zs_thread_stop
;
442 hrtime_t zs_thread_kill
;
443 uint64_t zs_enospc_count
;
444 uint64_t zs_vdev_next_leaf
;
445 uint64_t zs_vdev_aux
;
450 uint64_t zs_metaslab_sz
;
451 uint64_t zs_metaslab_df_alloc_threshold
;
455 #define ID_PARALLEL -1ULL
457 static char ztest_dev_template
[] = "%s/%s.%llua";
458 static char ztest_aux_template
[] = "%s/%s.%s.%llu";
459 ztest_shared_t
*ztest_shared
;
461 static spa_t
*ztest_spa
= NULL
;
462 static ztest_ds_t
*ztest_ds
;
464 static kmutex_t ztest_vdev_lock
;
465 static boolean_t ztest_device_removal_active
= B_FALSE
;
466 static kmutex_t ztest_checkpoint_lock
;
469 * The ztest_name_lock protects the pool and dataset namespace used by
470 * the individual tests. To modify the namespace, consumers must grab
471 * this lock as writer. Grabbing the lock as reader will ensure that the
472 * namespace does not change while the lock is held.
474 static pthread_rwlock_t ztest_name_lock
;
476 static boolean_t ztest_dump_core
= B_TRUE
;
477 static boolean_t ztest_dump_debug_buffer
= B_FALSE
;
478 static boolean_t ztest_exiting
;
480 /* Global commit callback list */
481 static ztest_cb_list_t zcl
;
482 /* Commit cb delay */
483 static uint64_t zc_min_txg_delay
= UINT64_MAX
;
484 static int zc_cb_counter
= 0;
487 * Minimum number of commit callbacks that need to be registered for us to check
488 * whether the minimum txg delay is acceptable.
490 #define ZTEST_COMMIT_CB_MIN_REG 100
493 * If a number of txgs equal to this threshold have been created after a commit
494 * callback has been registered but not called, then we assume there is an
495 * implementation bug.
497 #define ZTEST_COMMIT_CB_THRESH (TXG_CONCURRENT_STATES + 1000)
500 ZTEST_META_DNODE
= 0,
505 static void usage(boolean_t
) __NORETURN
;
508 * These libumem hooks provide a reasonable set of defaults for the allocator's
509 * debugging facilities.
512 _umem_debug_init(void)
514 return ("default,verbose"); /* $UMEM_DEBUG setting */
518 _umem_logging_init(void)
520 return ("fail,contents"); /* $UMEM_LOGGING setting */
524 dump_debug_buffer(void)
526 if (!ztest_dump_debug_buffer
)
530 zfs_dbgmsg_print("ztest");
533 #define BACKTRACE_SZ 100
535 static void sig_handler(int signo
)
537 struct sigaction action
;
538 #ifdef __GLIBC__ /* backtrace() is a GNU extension */
540 void *buffer
[BACKTRACE_SZ
];
542 nptrs
= backtrace(buffer
, BACKTRACE_SZ
);
543 backtrace_symbols_fd(buffer
, nptrs
, STDERR_FILENO
);
548 * Restore default action and re-raise signal so SIGSEGV and
549 * SIGABRT can trigger a core dump.
551 action
.sa_handler
= SIG_DFL
;
552 sigemptyset(&action
.sa_mask
);
554 (void) sigaction(signo
, &action
, NULL
);
558 #define FATAL_MSG_SZ 1024
563 fatal(int do_perror
, char *message
, ...)
566 int save_errno
= errno
;
569 (void) fflush(stdout
);
570 buf
= umem_alloc(FATAL_MSG_SZ
, UMEM_NOFAIL
);
572 va_start(args
, message
);
573 (void) sprintf(buf
, "ztest: ");
575 (void) vsprintf(buf
+ strlen(buf
), message
, args
);
578 (void) snprintf(buf
+ strlen(buf
), FATAL_MSG_SZ
- strlen(buf
),
579 ": %s", strerror(save_errno
));
581 (void) fprintf(stderr
, "%s\n", buf
);
582 fatal_msg
= buf
; /* to ease debugging */
592 str2shift(const char *buf
)
594 const char *ends
= "BKMGTPEZ";
599 for (i
= 0; i
< strlen(ends
); i
++) {
600 if (toupper(buf
[0]) == ends
[i
])
603 if (i
== strlen(ends
)) {
604 (void) fprintf(stderr
, "ztest: invalid bytes suffix: %s\n",
608 if (buf
[1] == '\0' || (toupper(buf
[1]) == 'B' && buf
[2] == '\0')) {
611 (void) fprintf(stderr
, "ztest: invalid bytes suffix: %s\n", buf
);
617 nicenumtoull(const char *buf
)
622 val
= strtoull(buf
, &end
, 0);
624 (void) fprintf(stderr
, "ztest: bad numeric value: %s\n", buf
);
626 } else if (end
[0] == '.') {
627 double fval
= strtod(buf
, &end
);
628 fval
*= pow(2, str2shift(end
));
629 if (fval
> UINT64_MAX
) {
630 (void) fprintf(stderr
, "ztest: value too large: %s\n",
634 val
= (uint64_t)fval
;
636 int shift
= str2shift(end
);
637 if (shift
>= 64 || (val
<< shift
) >> shift
!= val
) {
638 (void) fprintf(stderr
, "ztest: value too large: %s\n",
648 usage(boolean_t requested
)
650 const ztest_shared_opts_t
*zo
= &ztest_opts_defaults
;
652 char nice_vdev_size
[NN_NUMBUF_SZ
];
653 char nice_force_ganging
[NN_NUMBUF_SZ
];
654 FILE *fp
= requested
? stdout
: stderr
;
656 nicenum(zo
->zo_vdev_size
, nice_vdev_size
, sizeof (nice_vdev_size
));
657 nicenum(zo
->zo_metaslab_force_ganging
, nice_force_ganging
,
658 sizeof (nice_force_ganging
));
660 (void) fprintf(fp
, "Usage: %s\n"
661 "\t[-v vdevs (default: %llu)]\n"
662 "\t[-s size_of_each_vdev (default: %s)]\n"
663 "\t[-a alignment_shift (default: %d)] use 0 for random\n"
664 "\t[-m mirror_copies (default: %d)]\n"
665 "\t[-r raidz_disks (default: %d)]\n"
666 "\t[-R raidz_parity (default: %d)]\n"
667 "\t[-d datasets (default: %d)]\n"
668 "\t[-t threads (default: %d)]\n"
669 "\t[-g gang_block_threshold (default: %s)]\n"
670 "\t[-i init_count (default: %d)] initialize pool i times\n"
671 "\t[-k kill_percentage (default: %llu%%)]\n"
672 "\t[-p pool_name (default: %s)]\n"
673 "\t[-f dir (default: %s)] file directory for vdev files\n"
674 "\t[-M] Multi-host simulate pool imported on remote host\n"
675 "\t[-V] verbose (use multiple times for ever more blather)\n"
676 "\t[-E] use existing pool instead of creating new one\n"
677 "\t[-T time (default: %llu sec)] total run time\n"
678 "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
679 "\t[-P passtime (default: %llu sec)] time per pass\n"
680 "\t[-B alt_ztest (default: <none>)] alternate ztest path\n"
681 "\t[-C vdev class state (default: random)] special=on|off|random\n"
682 "\t[-o variable=value] ... set global variable to an unsigned\n"
683 "\t 32-bit integer value\n"
684 "\t[-G dump zfs_dbgmsg buffer before exiting due to an error\n"
685 "\t[-h] (print help)\n"
688 (u_longlong_t
)zo
->zo_vdevs
, /* -v */
689 nice_vdev_size
, /* -s */
690 zo
->zo_ashift
, /* -a */
691 zo
->zo_mirrors
, /* -m */
692 zo
->zo_raidz
, /* -r */
693 zo
->zo_raidz_parity
, /* -R */
694 zo
->zo_datasets
, /* -d */
695 zo
->zo_threads
, /* -t */
696 nice_force_ganging
, /* -g */
697 zo
->zo_init
, /* -i */
698 (u_longlong_t
)zo
->zo_killrate
, /* -k */
699 zo
->zo_pool
, /* -p */
701 (u_longlong_t
)zo
->zo_time
, /* -T */
702 (u_longlong_t
)zo
->zo_maxloops
, /* -F */
703 (u_longlong_t
)zo
->zo_passtime
);
704 exit(requested
? 0 : 1);
709 ztest_parse_name_value(const char *input
, ztest_shared_opts_t
*zo
)
713 int state
= ZTEST_VDEV_CLASS_RND
;
715 (void) strlcpy(name
, input
, sizeof (name
));
717 value
= strchr(name
, '=');
719 (void) fprintf(stderr
, "missing value in property=value "
720 "'-C' argument (%s)\n", input
);
726 if (strcmp(value
, "on") == 0) {
727 state
= ZTEST_VDEV_CLASS_ON
;
728 } else if (strcmp(value
, "off") == 0) {
729 state
= ZTEST_VDEV_CLASS_OFF
;
730 } else if (strcmp(value
, "random") == 0) {
731 state
= ZTEST_VDEV_CLASS_RND
;
733 (void) fprintf(stderr
, "invalid property value '%s'\n", value
);
737 if (strcmp(name
, "special") == 0) {
738 zo
->zo_special_vdevs
= state
;
740 (void) fprintf(stderr
, "invalid property name '%s'\n", name
);
743 if (zo
->zo_verbose
>= 3)
744 (void) printf("%s vdev state is '%s'\n", name
, value
);
748 process_options(int argc
, char **argv
)
751 ztest_shared_opts_t
*zo
= &ztest_opts
;
755 char altdir
[MAXNAMELEN
] = { 0 };
757 bcopy(&ztest_opts_defaults
, zo
, sizeof (*zo
));
759 while ((opt
= getopt(argc
, argv
,
760 "v:s:a:m:r:R:d:t:g:i:k:p:f:MVET:P:hF:B:C:o:G")) != EOF
) {
777 value
= nicenumtoull(optarg
);
781 zo
->zo_vdevs
= value
;
784 zo
->zo_vdev_size
= MAX(SPA_MINDEVSIZE
, value
);
787 zo
->zo_ashift
= value
;
790 zo
->zo_mirrors
= value
;
793 zo
->zo_raidz
= MAX(1, value
);
796 zo
->zo_raidz_parity
= MIN(MAX(value
, 1), 3);
799 zo
->zo_datasets
= MAX(1, value
);
802 zo
->zo_threads
= MAX(1, value
);
805 zo
->zo_metaslab_force_ganging
=
806 MAX(SPA_MINBLOCKSIZE
<< 1, value
);
812 zo
->zo_killrate
= value
;
815 (void) strlcpy(zo
->zo_pool
, optarg
,
816 sizeof (zo
->zo_pool
));
819 path
= realpath(optarg
, NULL
);
821 (void) fprintf(stderr
, "error: %s: %s\n",
822 optarg
, strerror(errno
));
825 (void) strlcpy(zo
->zo_dir
, path
,
826 sizeof (zo
->zo_dir
));
843 zo
->zo_passtime
= MAX(1, value
);
846 zo
->zo_maxloops
= MAX(1, value
);
849 (void) strlcpy(altdir
, optarg
, sizeof (altdir
));
852 ztest_parse_name_value(optarg
, zo
);
855 if (set_global_var(optarg
) != 0)
859 ztest_dump_debug_buffer
= B_TRUE
;
871 zo
->zo_raidz_parity
= MIN(zo
->zo_raidz_parity
, zo
->zo_raidz
- 1);
874 (zo
->zo_vdevs
> 0 ? zo
->zo_time
* NANOSEC
/ zo
->zo_vdevs
:
877 if (strlen(altdir
) > 0) {
885 cmd
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
886 realaltdir
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
888 VERIFY(NULL
!= realpath(getexecname(), cmd
));
889 if (0 != access(altdir
, F_OK
)) {
890 ztest_dump_core
= B_FALSE
;
891 fatal(B_TRUE
, "invalid alternate ztest path: %s",
894 VERIFY(NULL
!= realpath(altdir
, realaltdir
));
897 * 'cmd' should be of the form "<anything>/usr/bin/<isa>/ztest".
898 * We want to extract <isa> to determine if we should use
899 * 32 or 64 bit binaries.
901 bin
= strstr(cmd
, "/usr/bin/");
902 ztest
= strstr(bin
, "/ztest");
904 isalen
= ztest
- isa
;
905 (void) snprintf(zo
->zo_alt_ztest
, sizeof (zo
->zo_alt_ztest
),
906 "%s/usr/bin/%.*s/ztest", realaltdir
, isalen
, isa
);
907 (void) snprintf(zo
->zo_alt_libpath
, sizeof (zo
->zo_alt_libpath
),
908 "%s/usr/lib/%.*s", realaltdir
, isalen
, isa
);
910 if (0 != access(zo
->zo_alt_ztest
, X_OK
)) {
911 ztest_dump_core
= B_FALSE
;
912 fatal(B_TRUE
, "invalid alternate ztest: %s",
914 } else if (0 != access(zo
->zo_alt_libpath
, X_OK
)) {
915 ztest_dump_core
= B_FALSE
;
916 fatal(B_TRUE
, "invalid alternate lib directory %s",
920 umem_free(cmd
, MAXPATHLEN
);
921 umem_free(realaltdir
, MAXPATHLEN
);
926 ztest_kill(ztest_shared_t
*zs
)
928 zs
->zs_alloc
= metaslab_class_get_alloc(spa_normal_class(ztest_spa
));
929 zs
->zs_space
= metaslab_class_get_space(spa_normal_class(ztest_spa
));
932 * Before we kill off ztest, make sure that the config is updated.
933 * See comment above spa_write_cachefile().
935 mutex_enter(&spa_namespace_lock
);
936 spa_write_cachefile(ztest_spa
, B_FALSE
, B_FALSE
);
937 mutex_exit(&spa_namespace_lock
);
939 (void) kill(getpid(), SIGKILL
);
943 ztest_random(uint64_t range
)
947 ASSERT3S(ztest_fd_rand
, >=, 0);
952 if (read(ztest_fd_rand
, &r
, sizeof (r
)) != sizeof (r
))
953 fatal(1, "short read from /dev/urandom");
960 ztest_record_enospc(const char *s
)
962 ztest_shared
->zs_enospc_count
++;
966 ztest_get_ashift(void)
968 if (ztest_opts
.zo_ashift
== 0)
969 return (SPA_MINBLOCKSHIFT
+ ztest_random(5));
970 return (ztest_opts
.zo_ashift
);
974 make_vdev_file(char *path
, char *aux
, char *pool
, size_t size
, uint64_t ashift
)
980 pathbuf
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
983 ashift
= ztest_get_ashift();
989 vdev
= ztest_shared
->zs_vdev_aux
;
990 (void) snprintf(path
, MAXPATHLEN
,
991 ztest_aux_template
, ztest_opts
.zo_dir
,
992 pool
== NULL
? ztest_opts
.zo_pool
: pool
,
995 vdev
= ztest_shared
->zs_vdev_next_leaf
++;
996 (void) snprintf(path
, MAXPATHLEN
,
997 ztest_dev_template
, ztest_opts
.zo_dir
,
998 pool
== NULL
? ztest_opts
.zo_pool
: pool
, vdev
);
1003 int fd
= open(path
, O_RDWR
| O_CREAT
| O_TRUNC
, 0666);
1005 fatal(1, "can't open %s", path
);
1006 if (ftruncate(fd
, size
) != 0)
1007 fatal(1, "can't ftruncate %s", path
);
1011 VERIFY(nvlist_alloc(&file
, NV_UNIQUE_NAME
, 0) == 0);
1012 VERIFY(nvlist_add_string(file
, ZPOOL_CONFIG_TYPE
, VDEV_TYPE_FILE
) == 0);
1013 VERIFY(nvlist_add_string(file
, ZPOOL_CONFIG_PATH
, path
) == 0);
1014 VERIFY(nvlist_add_uint64(file
, ZPOOL_CONFIG_ASHIFT
, ashift
) == 0);
1015 umem_free(pathbuf
, MAXPATHLEN
);
1021 make_vdev_raidz(char *path
, char *aux
, char *pool
, size_t size
,
1022 uint64_t ashift
, int r
)
1024 nvlist_t
*raidz
, **child
;
1028 return (make_vdev_file(path
, aux
, pool
, size
, ashift
));
1029 child
= umem_alloc(r
* sizeof (nvlist_t
*), UMEM_NOFAIL
);
1031 for (c
= 0; c
< r
; c
++)
1032 child
[c
] = make_vdev_file(path
, aux
, pool
, size
, ashift
);
1034 VERIFY(nvlist_alloc(&raidz
, NV_UNIQUE_NAME
, 0) == 0);
1035 VERIFY(nvlist_add_string(raidz
, ZPOOL_CONFIG_TYPE
,
1036 VDEV_TYPE_RAIDZ
) == 0);
1037 VERIFY(nvlist_add_uint64(raidz
, ZPOOL_CONFIG_NPARITY
,
1038 ztest_opts
.zo_raidz_parity
) == 0);
1039 VERIFY(nvlist_add_nvlist_array(raidz
, ZPOOL_CONFIG_CHILDREN
,
1042 for (c
= 0; c
< r
; c
++)
1043 nvlist_free(child
[c
]);
1045 umem_free(child
, r
* sizeof (nvlist_t
*));
1051 make_vdev_mirror(char *path
, char *aux
, char *pool
, size_t size
,
1052 uint64_t ashift
, int r
, int m
)
1054 nvlist_t
*mirror
, **child
;
1058 return (make_vdev_raidz(path
, aux
, pool
, size
, ashift
, r
));
1060 child
= umem_alloc(m
* sizeof (nvlist_t
*), UMEM_NOFAIL
);
1062 for (c
= 0; c
< m
; c
++)
1063 child
[c
] = make_vdev_raidz(path
, aux
, pool
, size
, ashift
, r
);
1065 VERIFY(nvlist_alloc(&mirror
, NV_UNIQUE_NAME
, 0) == 0);
1066 VERIFY(nvlist_add_string(mirror
, ZPOOL_CONFIG_TYPE
,
1067 VDEV_TYPE_MIRROR
) == 0);
1068 VERIFY(nvlist_add_nvlist_array(mirror
, ZPOOL_CONFIG_CHILDREN
,
1071 for (c
= 0; c
< m
; c
++)
1072 nvlist_free(child
[c
]);
1074 umem_free(child
, m
* sizeof (nvlist_t
*));
1080 make_vdev_root(char *path
, char *aux
, char *pool
, size_t size
, uint64_t ashift
,
1081 const char *class, int r
, int m
, int t
)
1083 nvlist_t
*root
, **child
;
1089 log
= (class != NULL
&& strcmp(class, "log") == 0);
1091 child
= umem_alloc(t
* sizeof (nvlist_t
*), UMEM_NOFAIL
);
1093 for (c
= 0; c
< t
; c
++) {
1094 child
[c
] = make_vdev_mirror(path
, aux
, pool
, size
, ashift
,
1096 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1099 if (class != NULL
&& class[0] != '\0') {
1100 ASSERT(m
> 1 || log
); /* expecting a mirror */
1101 VERIFY(nvlist_add_string(child
[c
],
1102 ZPOOL_CONFIG_ALLOCATION_BIAS
, class) == 0);
1106 VERIFY(nvlist_alloc(&root
, NV_UNIQUE_NAME
, 0) == 0);
1107 VERIFY(nvlist_add_string(root
, ZPOOL_CONFIG_TYPE
, VDEV_TYPE_ROOT
) == 0);
1108 VERIFY(nvlist_add_nvlist_array(root
, aux
? aux
: ZPOOL_CONFIG_CHILDREN
,
1111 for (c
= 0; c
< t
; c
++)
1112 nvlist_free(child
[c
]);
1114 umem_free(child
, t
* sizeof (nvlist_t
*));
1120 * Find a random spa version. Returns back a random spa version in the
1121 * range [initial_version, SPA_VERSION_FEATURES].
1124 ztest_random_spa_version(uint64_t initial_version
)
1126 uint64_t version
= initial_version
;
1128 if (version
<= SPA_VERSION_BEFORE_FEATURES
) {
1130 ztest_random(SPA_VERSION_BEFORE_FEATURES
- version
+ 1);
1133 if (version
> SPA_VERSION_BEFORE_FEATURES
)
1134 version
= SPA_VERSION_FEATURES
;
1136 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
1141 ztest_random_blocksize(void)
1143 ASSERT(ztest_spa
->spa_max_ashift
!= 0);
1146 * Choose a block size >= the ashift.
1147 * If the SPA supports new MAXBLOCKSIZE, test up to 1MB blocks.
1149 int maxbs
= SPA_OLD_MAXBLOCKSHIFT
;
1150 if (spa_maxblocksize(ztest_spa
) == SPA_MAXBLOCKSIZE
)
1152 uint64_t block_shift
=
1153 ztest_random(maxbs
- ztest_spa
->spa_max_ashift
+ 1);
1154 return (1 << (SPA_MINBLOCKSHIFT
+ block_shift
));
1158 ztest_random_dnodesize(void)
1161 int max_slots
= spa_maxdnodesize(ztest_spa
) >> DNODE_SHIFT
;
1163 if (max_slots
== DNODE_MIN_SLOTS
)
1164 return (DNODE_MIN_SIZE
);
1167 * Weight the random distribution more heavily toward smaller
1168 * dnode sizes since that is more likely to reflect real-world
1171 ASSERT3U(max_slots
, >, 4);
1172 switch (ztest_random(10)) {
1174 slots
= 5 + ztest_random(max_slots
- 4);
1177 slots
= 2 + ztest_random(3);
1184 return (slots
<< DNODE_SHIFT
);
1188 ztest_random_ibshift(void)
1190 return (DN_MIN_INDBLKSHIFT
+
1191 ztest_random(DN_MAX_INDBLKSHIFT
- DN_MIN_INDBLKSHIFT
+ 1));
1195 ztest_random_vdev_top(spa_t
*spa
, boolean_t log_ok
)
1198 vdev_t
*rvd
= spa
->spa_root_vdev
;
1201 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
1204 top
= ztest_random(rvd
->vdev_children
);
1205 tvd
= rvd
->vdev_child
[top
];
1206 } while (!vdev_is_concrete(tvd
) || (tvd
->vdev_islog
&& !log_ok
) ||
1207 tvd
->vdev_mg
== NULL
|| tvd
->vdev_mg
->mg_class
== NULL
);
1213 ztest_random_dsl_prop(zfs_prop_t prop
)
1218 value
= zfs_prop_random_value(prop
, ztest_random(-1ULL));
1219 } while (prop
== ZFS_PROP_CHECKSUM
&& value
== ZIO_CHECKSUM_OFF
);
1225 ztest_dsl_prop_set_uint64(char *osname
, zfs_prop_t prop
, uint64_t value
,
1228 const char *propname
= zfs_prop_to_name(prop
);
1229 const char *valname
;
1234 error
= dsl_prop_set_int(osname
, propname
,
1235 (inherit
? ZPROP_SRC_NONE
: ZPROP_SRC_LOCAL
), value
);
1237 if (error
== ENOSPC
) {
1238 ztest_record_enospc(FTAG
);
1243 setpoint
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
1244 VERIFY0(dsl_prop_get_integer(osname
, propname
, &curval
, setpoint
));
1246 if (ztest_opts
.zo_verbose
>= 6) {
1249 err
= zfs_prop_index_to_string(prop
, curval
, &valname
);
1251 (void) printf("%s %s = %llu at '%s'\n", osname
,
1252 propname
, (unsigned long long)curval
, setpoint
);
1254 (void) printf("%s %s = %s at '%s'\n",
1255 osname
, propname
, valname
, setpoint
);
1257 umem_free(setpoint
, MAXPATHLEN
);
1263 ztest_spa_prop_set_uint64(zpool_prop_t prop
, uint64_t value
)
1265 spa_t
*spa
= ztest_spa
;
1266 nvlist_t
*props
= NULL
;
1269 VERIFY(nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) == 0);
1270 VERIFY(nvlist_add_uint64(props
, zpool_prop_to_name(prop
), value
) == 0);
1272 error
= spa_prop_set(spa
, props
);
1276 if (error
== ENOSPC
) {
1277 ztest_record_enospc(FTAG
);
1286 ztest_dmu_objset_own(const char *name
, dmu_objset_type_t type
,
1287 boolean_t readonly
, boolean_t decrypt
, void *tag
, objset_t
**osp
)
1291 err
= dmu_objset_own(name
, type
, readonly
, decrypt
, tag
, osp
);
1292 if (decrypt
&& err
== EACCES
) {
1293 char ddname
[ZFS_MAX_DATASET_NAME_LEN
];
1294 dsl_crypto_params_t
*dcp
;
1295 nvlist_t
*crypto_args
= fnvlist_alloc();
1298 /* spa_keystore_load_wkey() expects a dsl dir name */
1299 strcpy(ddname
, name
);
1300 cp
= strchr(ddname
, '@');
1304 fnvlist_add_uint8_array(crypto_args
, "wkeydata",
1305 (uint8_t *)ztest_wkeydata
, WRAPPING_KEY_LEN
);
1306 VERIFY0(dsl_crypto_params_create_nvlist(DCP_CMD_NONE
, NULL
,
1307 crypto_args
, &dcp
));
1308 err
= spa_keystore_load_wkey(ddname
, dcp
, B_FALSE
);
1309 dsl_crypto_params_free(dcp
, B_FALSE
);
1310 fnvlist_free(crypto_args
);
1315 err
= dmu_objset_own(name
, type
, readonly
, decrypt
, tag
, osp
);
1323 * Object and range lock mechanics
1326 list_node_t z_lnode
;
1327 zfs_refcount_t z_refcnt
;
1329 zfs_rlock_t z_range_lock
;
1334 ztest_znode_t
*z_ztznode
;
1337 static ztest_znode_t
*
1338 ztest_znode_init(uint64_t object
)
1340 ztest_znode_t
*zp
= umem_alloc(sizeof (*zp
), UMEM_NOFAIL
);
1342 list_link_init(&zp
->z_lnode
);
1343 zfs_refcount_create(&zp
->z_refcnt
);
1344 zp
->z_object
= object
;
1345 zfs_rlock_init(&zp
->z_range_lock
);
1351 ztest_znode_fini(ztest_znode_t
*zp
)
1353 ASSERT(zfs_refcount_is_zero(&zp
->z_refcnt
));
1354 zfs_rlock_destroy(&zp
->z_range_lock
);
1356 zfs_refcount_destroy(&zp
->z_refcnt
);
1357 list_link_init(&zp
->z_lnode
);
1358 umem_free(zp
, sizeof (*zp
));
1362 ztest_zll_init(zll_t
*zll
)
1364 mutex_init(&zll
->z_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1365 list_create(&zll
->z_list
, sizeof (ztest_znode_t
),
1366 offsetof(ztest_znode_t
, z_lnode
));
1370 ztest_zll_destroy(zll_t
*zll
)
1372 list_destroy(&zll
->z_list
);
1373 mutex_destroy(&zll
->z_lock
);
1376 #define RL_TAG "range_lock"
1377 static ztest_znode_t
*
1378 ztest_znode_get(ztest_ds_t
*zd
, uint64_t object
)
1380 zll_t
*zll
= &zd
->zd_range_lock
[object
& (ZTEST_OBJECT_LOCKS
- 1)];
1381 ztest_znode_t
*zp
= NULL
;
1382 mutex_enter(&zll
->z_lock
);
1383 for (zp
= list_head(&zll
->z_list
); (zp
);
1384 zp
= list_next(&zll
->z_list
, zp
)) {
1385 if (zp
->z_object
== object
) {
1386 zfs_refcount_add(&zp
->z_refcnt
, RL_TAG
);
1391 zp
= ztest_znode_init(object
);
1392 zfs_refcount_add(&zp
->z_refcnt
, RL_TAG
);
1393 list_insert_head(&zll
->z_list
, zp
);
1395 mutex_exit(&zll
->z_lock
);
1400 ztest_znode_put(ztest_ds_t
*zd
, ztest_znode_t
*zp
)
1403 ASSERT3U(zp
->z_object
, !=, 0);
1404 zll
= &zd
->zd_range_lock
[zp
->z_object
& (ZTEST_OBJECT_LOCKS
- 1)];
1405 mutex_enter(&zll
->z_lock
);
1406 zfs_refcount_remove(&zp
->z_refcnt
, RL_TAG
);
1407 if (zfs_refcount_is_zero(&zp
->z_refcnt
)) {
1408 list_remove(&zll
->z_list
, zp
);
1409 ztest_znode_fini(zp
);
1411 mutex_exit(&zll
->z_lock
);
1416 ztest_rll_init(rll_t
*rll
)
1418 rll
->rll_writer
= NULL
;
1419 rll
->rll_readers
= 0;
1420 mutex_init(&rll
->rll_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1421 cv_init(&rll
->rll_cv
, NULL
, CV_DEFAULT
, NULL
);
1425 ztest_rll_destroy(rll_t
*rll
)
1427 ASSERT(rll
->rll_writer
== NULL
);
1428 ASSERT(rll
->rll_readers
== 0);
1429 mutex_destroy(&rll
->rll_lock
);
1430 cv_destroy(&rll
->rll_cv
);
1434 ztest_rll_lock(rll_t
*rll
, rl_type_t type
)
1436 mutex_enter(&rll
->rll_lock
);
1438 if (type
== RL_READER
) {
1439 while (rll
->rll_writer
!= NULL
)
1440 (void) cv_wait(&rll
->rll_cv
, &rll
->rll_lock
);
1443 while (rll
->rll_writer
!= NULL
|| rll
->rll_readers
)
1444 (void) cv_wait(&rll
->rll_cv
, &rll
->rll_lock
);
1445 rll
->rll_writer
= curthread
;
1448 mutex_exit(&rll
->rll_lock
);
1452 ztest_rll_unlock(rll_t
*rll
)
1454 mutex_enter(&rll
->rll_lock
);
1456 if (rll
->rll_writer
) {
1457 ASSERT(rll
->rll_readers
== 0);
1458 rll
->rll_writer
= NULL
;
1460 ASSERT(rll
->rll_readers
!= 0);
1461 ASSERT(rll
->rll_writer
== NULL
);
1465 if (rll
->rll_writer
== NULL
&& rll
->rll_readers
== 0)
1466 cv_broadcast(&rll
->rll_cv
);
1468 mutex_exit(&rll
->rll_lock
);
1472 ztest_object_lock(ztest_ds_t
*zd
, uint64_t object
, rl_type_t type
)
1474 rll_t
*rll
= &zd
->zd_object_lock
[object
& (ZTEST_OBJECT_LOCKS
- 1)];
1476 ztest_rll_lock(rll
, type
);
1480 ztest_object_unlock(ztest_ds_t
*zd
, uint64_t object
)
1482 rll_t
*rll
= &zd
->zd_object_lock
[object
& (ZTEST_OBJECT_LOCKS
- 1)];
1484 ztest_rll_unlock(rll
);
1487 static ztest_zrl_t
*
1488 ztest_zrl_init(rl_t
*rl
, ztest_znode_t
*zp
)
1490 ztest_zrl_t
*zrl
= umem_alloc(sizeof (*zrl
), UMEM_NOFAIL
);
1492 zrl
->z_ztznode
= zp
;
1497 ztest_zrl_fini(ztest_zrl_t
*zrl
)
1499 umem_free(zrl
, sizeof (*zrl
));
1502 static ztest_zrl_t
*
1503 ztest_range_lock(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
,
1504 uint64_t size
, rl_type_t type
)
1506 ztest_znode_t
*zp
= ztest_znode_get(zd
, object
);
1507 rl_t
*rl
= zfs_range_lock(&zp
->z_range_lock
, offset
,
1509 return (ztest_zrl_init(rl
, zp
));
1513 ztest_range_unlock(ztest_ds_t
*zd
, ztest_zrl_t
*zrl
)
1515 zfs_range_unlock(zrl
->z_rl
);
1516 ztest_znode_put(zd
, zrl
->z_ztznode
);
1517 ztest_zrl_fini(zrl
);
1521 ztest_zd_init(ztest_ds_t
*zd
, ztest_shared_ds_t
*szd
, objset_t
*os
)
1524 zd
->zd_zilog
= dmu_objset_zil(os
);
1525 zd
->zd_shared
= szd
;
1526 dmu_objset_name(os
, zd
->zd_name
);
1529 if (zd
->zd_shared
!= NULL
)
1530 zd
->zd_shared
->zd_seq
= 0;
1532 VERIFY0(pthread_rwlock_init(&zd
->zd_zilog_lock
, NULL
));
1533 mutex_init(&zd
->zd_dirobj_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1535 for (l
= 0; l
< ZTEST_OBJECT_LOCKS
; l
++)
1536 ztest_rll_init(&zd
->zd_object_lock
[l
]);
1538 for (l
= 0; l
< ZTEST_RANGE_LOCKS
; l
++)
1539 ztest_zll_init(&zd
->zd_range_lock
[l
]);
1543 ztest_zd_fini(ztest_ds_t
*zd
)
1547 mutex_destroy(&zd
->zd_dirobj_lock
);
1548 (void) pthread_rwlock_destroy(&zd
->zd_zilog_lock
);
1550 for (l
= 0; l
< ZTEST_OBJECT_LOCKS
; l
++)
1551 ztest_rll_destroy(&zd
->zd_object_lock
[l
]);
1553 for (l
= 0; l
< ZTEST_RANGE_LOCKS
; l
++)
1554 ztest_zll_destroy(&zd
->zd_range_lock
[l
]);
1557 #define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1560 ztest_tx_assign(dmu_tx_t
*tx
, uint64_t txg_how
, const char *tag
)
1566 * Attempt to assign tx to some transaction group.
1568 error
= dmu_tx_assign(tx
, txg_how
);
1570 if (error
== ERESTART
) {
1571 ASSERT(txg_how
== TXG_NOWAIT
);
1574 ASSERT3U(error
, ==, ENOSPC
);
1575 ztest_record_enospc(tag
);
1580 txg
= dmu_tx_get_txg(tx
);
1586 ztest_pattern_set(void *buf
, uint64_t size
, uint64_t value
)
1589 uint64_t *ip_end
= (uint64_t *)((uintptr_t)buf
+ (uintptr_t)size
);
1597 ztest_pattern_match(void *buf
, uint64_t size
, uint64_t value
)
1600 uint64_t *ip_end
= (uint64_t *)((uintptr_t)buf
+ (uintptr_t)size
);
1604 diff
|= (value
- *ip
++);
1611 ztest_bt_generate(ztest_block_tag_t
*bt
, objset_t
*os
, uint64_t object
,
1612 uint64_t dnodesize
, uint64_t offset
, uint64_t gen
, uint64_t txg
,
1615 bt
->bt_magic
= BT_MAGIC
;
1616 bt
->bt_objset
= dmu_objset_id(os
);
1617 bt
->bt_object
= object
;
1618 bt
->bt_dnodesize
= dnodesize
;
1619 bt
->bt_offset
= offset
;
1622 bt
->bt_crtxg
= crtxg
;
1626 ztest_bt_verify(ztest_block_tag_t
*bt
, objset_t
*os
, uint64_t object
,
1627 uint64_t dnodesize
, uint64_t offset
, uint64_t gen
, uint64_t txg
,
1630 ASSERT3U(bt
->bt_magic
, ==, BT_MAGIC
);
1631 ASSERT3U(bt
->bt_objset
, ==, dmu_objset_id(os
));
1632 ASSERT3U(bt
->bt_object
, ==, object
);
1633 ASSERT3U(bt
->bt_dnodesize
, ==, dnodesize
);
1634 ASSERT3U(bt
->bt_offset
, ==, offset
);
1635 ASSERT3U(bt
->bt_gen
, <=, gen
);
1636 ASSERT3U(bt
->bt_txg
, <=, txg
);
1637 ASSERT3U(bt
->bt_crtxg
, ==, crtxg
);
1640 static ztest_block_tag_t
*
1641 ztest_bt_bonus(dmu_buf_t
*db
)
1643 dmu_object_info_t doi
;
1644 ztest_block_tag_t
*bt
;
1646 dmu_object_info_from_db(db
, &doi
);
1647 ASSERT3U(doi
.doi_bonus_size
, <=, db
->db_size
);
1648 ASSERT3U(doi
.doi_bonus_size
, >=, sizeof (*bt
));
1649 bt
= (void *)((char *)db
->db_data
+ doi
.doi_bonus_size
- sizeof (*bt
));
1655 * Generate a token to fill up unused bonus buffer space. Try to make
1656 * it unique to the object, generation, and offset to verify that data
1657 * is not getting overwritten by data from other dnodes.
1659 #define ZTEST_BONUS_FILL_TOKEN(obj, ds, gen, offset) \
1660 (((ds) << 48) | ((gen) << 32) | ((obj) << 8) | (offset))
1663 * Fill up the unused bonus buffer region before the block tag with a
1664 * verifiable pattern. Filling the whole bonus area with non-zero data
1665 * helps ensure that all dnode traversal code properly skips the
1666 * interior regions of large dnodes.
1669 ztest_fill_unused_bonus(dmu_buf_t
*db
, void *end
, uint64_t obj
,
1670 objset_t
*os
, uint64_t gen
)
1674 ASSERT(IS_P2ALIGNED((char *)end
- (char *)db
->db_data
, 8));
1676 for (bonusp
= db
->db_data
; bonusp
< (uint64_t *)end
; bonusp
++) {
1677 uint64_t token
= ZTEST_BONUS_FILL_TOKEN(obj
, dmu_objset_id(os
),
1678 gen
, bonusp
- (uint64_t *)db
->db_data
);
1684 * Verify that the unused area of a bonus buffer is filled with the
1688 ztest_verify_unused_bonus(dmu_buf_t
*db
, void *end
, uint64_t obj
,
1689 objset_t
*os
, uint64_t gen
)
1693 for (bonusp
= db
->db_data
; bonusp
< (uint64_t *)end
; bonusp
++) {
1694 uint64_t token
= ZTEST_BONUS_FILL_TOKEN(obj
, dmu_objset_id(os
),
1695 gen
, bonusp
- (uint64_t *)db
->db_data
);
1696 VERIFY3U(*bonusp
, ==, token
);
1704 #define lrz_type lr_mode
1705 #define lrz_blocksize lr_uid
1706 #define lrz_ibshift lr_gid
1707 #define lrz_bonustype lr_rdev
1708 #define lrz_dnodesize lr_crtime[1]
1711 ztest_log_create(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_create_t
*lr
)
1713 char *name
= (void *)(lr
+ 1); /* name follows lr */
1714 size_t namesize
= strlen(name
) + 1;
1717 if (zil_replaying(zd
->zd_zilog
, tx
))
1720 itx
= zil_itx_create(TX_CREATE
, sizeof (*lr
) + namesize
);
1721 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1722 sizeof (*lr
) + namesize
- sizeof (lr_t
));
1724 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1728 ztest_log_remove(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_remove_t
*lr
, uint64_t object
)
1730 char *name
= (void *)(lr
+ 1); /* name follows lr */
1731 size_t namesize
= strlen(name
) + 1;
1734 if (zil_replaying(zd
->zd_zilog
, tx
))
1737 itx
= zil_itx_create(TX_REMOVE
, sizeof (*lr
) + namesize
);
1738 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1739 sizeof (*lr
) + namesize
- sizeof (lr_t
));
1741 itx
->itx_oid
= object
;
1742 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1746 ztest_log_write(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_write_t
*lr
)
1749 itx_wr_state_t write_state
= ztest_random(WR_NUM_STATES
);
1751 if (zil_replaying(zd
->zd_zilog
, tx
))
1754 if (lr
->lr_length
> ZIL_MAX_LOG_DATA
)
1755 write_state
= WR_INDIRECT
;
1757 itx
= zil_itx_create(TX_WRITE
,
1758 sizeof (*lr
) + (write_state
== WR_COPIED
? lr
->lr_length
: 0));
1760 if (write_state
== WR_COPIED
&&
1761 dmu_read(zd
->zd_os
, lr
->lr_foid
, lr
->lr_offset
, lr
->lr_length
,
1762 ((lr_write_t
*)&itx
->itx_lr
) + 1, DMU_READ_NO_PREFETCH
) != 0) {
1763 zil_itx_destroy(itx
);
1764 itx
= zil_itx_create(TX_WRITE
, sizeof (*lr
));
1765 write_state
= WR_NEED_COPY
;
1767 itx
->itx_private
= zd
;
1768 itx
->itx_wr_state
= write_state
;
1769 itx
->itx_sync
= (ztest_random(8) == 0);
1771 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1772 sizeof (*lr
) - sizeof (lr_t
));
1774 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1778 ztest_log_truncate(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_truncate_t
*lr
)
1782 if (zil_replaying(zd
->zd_zilog
, tx
))
1785 itx
= zil_itx_create(TX_TRUNCATE
, sizeof (*lr
));
1786 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1787 sizeof (*lr
) - sizeof (lr_t
));
1789 itx
->itx_sync
= B_FALSE
;
1790 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1794 ztest_log_setattr(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_setattr_t
*lr
)
1798 if (zil_replaying(zd
->zd_zilog
, tx
))
1801 itx
= zil_itx_create(TX_SETATTR
, sizeof (*lr
));
1802 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1803 sizeof (*lr
) - sizeof (lr_t
));
1805 itx
->itx_sync
= B_FALSE
;
1806 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1813 ztest_replay_create(void *arg1
, void *arg2
, boolean_t byteswap
)
1815 ztest_ds_t
*zd
= arg1
;
1816 lr_create_t
*lr
= arg2
;
1817 char *name
= (void *)(lr
+ 1); /* name follows lr */
1818 objset_t
*os
= zd
->zd_os
;
1819 ztest_block_tag_t
*bbt
;
1827 byteswap_uint64_array(lr
, sizeof (*lr
));
1829 ASSERT(lr
->lr_doid
== ZTEST_DIROBJ
);
1830 ASSERT(name
[0] != '\0');
1832 tx
= dmu_tx_create(os
);
1834 dmu_tx_hold_zap(tx
, lr
->lr_doid
, B_TRUE
, name
);
1836 if (lr
->lrz_type
== DMU_OT_ZAP_OTHER
) {
1837 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, B_TRUE
, NULL
);
1839 dmu_tx_hold_bonus(tx
, DMU_NEW_OBJECT
);
1842 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
1846 ASSERT(dmu_objset_zil(os
)->zl_replay
== !!lr
->lr_foid
);
1847 bonuslen
= DN_BONUS_SIZE(lr
->lrz_dnodesize
);
1849 if (lr
->lrz_type
== DMU_OT_ZAP_OTHER
) {
1850 if (lr
->lr_foid
== 0) {
1851 lr
->lr_foid
= zap_create_dnsize(os
,
1852 lr
->lrz_type
, lr
->lrz_bonustype
,
1853 bonuslen
, lr
->lrz_dnodesize
, tx
);
1855 error
= zap_create_claim_dnsize(os
, lr
->lr_foid
,
1856 lr
->lrz_type
, lr
->lrz_bonustype
,
1857 bonuslen
, lr
->lrz_dnodesize
, tx
);
1860 if (lr
->lr_foid
== 0) {
1861 lr
->lr_foid
= dmu_object_alloc_dnsize(os
,
1862 lr
->lrz_type
, 0, lr
->lrz_bonustype
,
1863 bonuslen
, lr
->lrz_dnodesize
, tx
);
1865 error
= dmu_object_claim_dnsize(os
, lr
->lr_foid
,
1866 lr
->lrz_type
, 0, lr
->lrz_bonustype
,
1867 bonuslen
, lr
->lrz_dnodesize
, tx
);
1872 ASSERT3U(error
, ==, EEXIST
);
1873 ASSERT(zd
->zd_zilog
->zl_replay
);
1878 ASSERT(lr
->lr_foid
!= 0);
1880 if (lr
->lrz_type
!= DMU_OT_ZAP_OTHER
)
1881 VERIFY3U(0, ==, dmu_object_set_blocksize(os
, lr
->lr_foid
,
1882 lr
->lrz_blocksize
, lr
->lrz_ibshift
, tx
));
1884 VERIFY3U(0, ==, dmu_bonus_hold(os
, lr
->lr_foid
, FTAG
, &db
));
1885 bbt
= ztest_bt_bonus(db
);
1886 dmu_buf_will_dirty(db
, tx
);
1887 ztest_bt_generate(bbt
, os
, lr
->lr_foid
, lr
->lrz_dnodesize
, -1ULL,
1888 lr
->lr_gen
, txg
, txg
);
1889 ztest_fill_unused_bonus(db
, bbt
, lr
->lr_foid
, os
, lr
->lr_gen
);
1890 dmu_buf_rele(db
, FTAG
);
1892 VERIFY3U(0, ==, zap_add(os
, lr
->lr_doid
, name
, sizeof (uint64_t), 1,
1895 (void) ztest_log_create(zd
, tx
, lr
);
1903 ztest_replay_remove(void *arg1
, void *arg2
, boolean_t byteswap
)
1905 ztest_ds_t
*zd
= arg1
;
1906 lr_remove_t
*lr
= arg2
;
1907 char *name
= (void *)(lr
+ 1); /* name follows lr */
1908 objset_t
*os
= zd
->zd_os
;
1909 dmu_object_info_t doi
;
1911 uint64_t object
, txg
;
1914 byteswap_uint64_array(lr
, sizeof (*lr
));
1916 ASSERT(lr
->lr_doid
== ZTEST_DIROBJ
);
1917 ASSERT(name
[0] != '\0');
1920 zap_lookup(os
, lr
->lr_doid
, name
, sizeof (object
), 1, &object
));
1921 ASSERT(object
!= 0);
1923 ztest_object_lock(zd
, object
, RL_WRITER
);
1925 VERIFY3U(0, ==, dmu_object_info(os
, object
, &doi
));
1927 tx
= dmu_tx_create(os
);
1929 dmu_tx_hold_zap(tx
, lr
->lr_doid
, B_FALSE
, name
);
1930 dmu_tx_hold_free(tx
, object
, 0, DMU_OBJECT_END
);
1932 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
1934 ztest_object_unlock(zd
, object
);
1938 if (doi
.doi_type
== DMU_OT_ZAP_OTHER
) {
1939 VERIFY3U(0, ==, zap_destroy(os
, object
, tx
));
1941 VERIFY3U(0, ==, dmu_object_free(os
, object
, tx
));
1944 VERIFY3U(0, ==, zap_remove(os
, lr
->lr_doid
, name
, tx
));
1946 (void) ztest_log_remove(zd
, tx
, lr
, object
);
1950 ztest_object_unlock(zd
, object
);
1956 ztest_replay_write(void *arg1
, void *arg2
, boolean_t byteswap
)
1958 ztest_ds_t
*zd
= arg1
;
1959 lr_write_t
*lr
= arg2
;
1960 objset_t
*os
= zd
->zd_os
;
1961 void *data
= lr
+ 1; /* data follows lr */
1962 uint64_t offset
, length
;
1963 ztest_block_tag_t
*bt
= data
;
1964 ztest_block_tag_t
*bbt
;
1965 uint64_t gen
, txg
, lrtxg
, crtxg
;
1966 dmu_object_info_t doi
;
1969 arc_buf_t
*abuf
= NULL
;
1973 byteswap_uint64_array(lr
, sizeof (*lr
));
1975 offset
= lr
->lr_offset
;
1976 length
= lr
->lr_length
;
1978 /* If it's a dmu_sync() block, write the whole block */
1979 if (lr
->lr_common
.lrc_reclen
== sizeof (lr_write_t
)) {
1980 uint64_t blocksize
= BP_GET_LSIZE(&lr
->lr_blkptr
);
1981 if (length
< blocksize
) {
1982 offset
-= offset
% blocksize
;
1987 if (bt
->bt_magic
== BSWAP_64(BT_MAGIC
))
1988 byteswap_uint64_array(bt
, sizeof (*bt
));
1990 if (bt
->bt_magic
!= BT_MAGIC
)
1993 ztest_object_lock(zd
, lr
->lr_foid
, RL_READER
);
1994 rl
= ztest_range_lock(zd
, lr
->lr_foid
, offset
, length
, RL_WRITER
);
1996 VERIFY3U(0, ==, dmu_bonus_hold(os
, lr
->lr_foid
, FTAG
, &db
));
1998 dmu_object_info_from_db(db
, &doi
);
2000 bbt
= ztest_bt_bonus(db
);
2001 ASSERT3U(bbt
->bt_magic
, ==, BT_MAGIC
);
2003 crtxg
= bbt
->bt_crtxg
;
2004 lrtxg
= lr
->lr_common
.lrc_txg
;
2006 tx
= dmu_tx_create(os
);
2008 dmu_tx_hold_write(tx
, lr
->lr_foid
, offset
, length
);
2010 if (ztest_random(8) == 0 && length
== doi
.doi_data_block_size
&&
2011 P2PHASE(offset
, length
) == 0)
2012 abuf
= dmu_request_arcbuf(db
, length
);
2014 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
2017 dmu_return_arcbuf(abuf
);
2018 dmu_buf_rele(db
, FTAG
);
2019 ztest_range_unlock(zd
, rl
);
2020 ztest_object_unlock(zd
, lr
->lr_foid
);
2026 * Usually, verify the old data before writing new data --
2027 * but not always, because we also want to verify correct
2028 * behavior when the data was not recently read into cache.
2030 ASSERT(offset
% doi
.doi_data_block_size
== 0);
2031 if (ztest_random(4) != 0) {
2032 int prefetch
= ztest_random(2) ?
2033 DMU_READ_PREFETCH
: DMU_READ_NO_PREFETCH
;
2034 ztest_block_tag_t rbt
;
2036 VERIFY(dmu_read(os
, lr
->lr_foid
, offset
,
2037 sizeof (rbt
), &rbt
, prefetch
) == 0);
2038 if (rbt
.bt_magic
== BT_MAGIC
) {
2039 ztest_bt_verify(&rbt
, os
, lr
->lr_foid
, 0,
2040 offset
, gen
, txg
, crtxg
);
2045 * Writes can appear to be newer than the bonus buffer because
2046 * the ztest_get_data() callback does a dmu_read() of the
2047 * open-context data, which may be different than the data
2048 * as it was when the write was generated.
2050 if (zd
->zd_zilog
->zl_replay
) {
2051 ztest_bt_verify(bt
, os
, lr
->lr_foid
, 0, offset
,
2052 MAX(gen
, bt
->bt_gen
), MAX(txg
, lrtxg
),
2057 * Set the bt's gen/txg to the bonus buffer's gen/txg
2058 * so that all of the usual ASSERTs will work.
2060 ztest_bt_generate(bt
, os
, lr
->lr_foid
, 0, offset
, gen
, txg
,
2065 dmu_write(os
, lr
->lr_foid
, offset
, length
, data
, tx
);
2067 bcopy(data
, abuf
->b_data
, length
);
2068 dmu_assign_arcbuf_by_dbuf(db
, offset
, abuf
, tx
);
2071 (void) ztest_log_write(zd
, tx
, lr
);
2073 dmu_buf_rele(db
, FTAG
);
2077 ztest_range_unlock(zd
, rl
);
2078 ztest_object_unlock(zd
, lr
->lr_foid
);
2084 ztest_replay_truncate(void *arg1
, void *arg2
, boolean_t byteswap
)
2086 ztest_ds_t
*zd
= arg1
;
2087 lr_truncate_t
*lr
= arg2
;
2088 objset_t
*os
= zd
->zd_os
;
2094 byteswap_uint64_array(lr
, sizeof (*lr
));
2096 ztest_object_lock(zd
, lr
->lr_foid
, RL_READER
);
2097 rl
= ztest_range_lock(zd
, lr
->lr_foid
, lr
->lr_offset
, lr
->lr_length
,
2100 tx
= dmu_tx_create(os
);
2102 dmu_tx_hold_free(tx
, lr
->lr_foid
, lr
->lr_offset
, lr
->lr_length
);
2104 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
2106 ztest_range_unlock(zd
, rl
);
2107 ztest_object_unlock(zd
, lr
->lr_foid
);
2111 VERIFY(dmu_free_range(os
, lr
->lr_foid
, lr
->lr_offset
,
2112 lr
->lr_length
, tx
) == 0);
2114 (void) ztest_log_truncate(zd
, tx
, lr
);
2118 ztest_range_unlock(zd
, rl
);
2119 ztest_object_unlock(zd
, lr
->lr_foid
);
2125 ztest_replay_setattr(void *arg1
, void *arg2
, boolean_t byteswap
)
2127 ztest_ds_t
*zd
= arg1
;
2128 lr_setattr_t
*lr
= arg2
;
2129 objset_t
*os
= zd
->zd_os
;
2132 ztest_block_tag_t
*bbt
;
2133 uint64_t txg
, lrtxg
, crtxg
, dnodesize
;
2136 byteswap_uint64_array(lr
, sizeof (*lr
));
2138 ztest_object_lock(zd
, lr
->lr_foid
, RL_WRITER
);
2140 VERIFY3U(0, ==, dmu_bonus_hold(os
, lr
->lr_foid
, FTAG
, &db
));
2142 tx
= dmu_tx_create(os
);
2143 dmu_tx_hold_bonus(tx
, lr
->lr_foid
);
2145 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
2147 dmu_buf_rele(db
, FTAG
);
2148 ztest_object_unlock(zd
, lr
->lr_foid
);
2152 bbt
= ztest_bt_bonus(db
);
2153 ASSERT3U(bbt
->bt_magic
, ==, BT_MAGIC
);
2154 crtxg
= bbt
->bt_crtxg
;
2155 lrtxg
= lr
->lr_common
.lrc_txg
;
2156 dnodesize
= bbt
->bt_dnodesize
;
2158 if (zd
->zd_zilog
->zl_replay
) {
2159 ASSERT(lr
->lr_size
!= 0);
2160 ASSERT(lr
->lr_mode
!= 0);
2164 * Randomly change the size and increment the generation.
2166 lr
->lr_size
= (ztest_random(db
->db_size
/ sizeof (*bbt
)) + 1) *
2168 lr
->lr_mode
= bbt
->bt_gen
+ 1;
2173 * Verify that the current bonus buffer is not newer than our txg.
2175 ztest_bt_verify(bbt
, os
, lr
->lr_foid
, dnodesize
, -1ULL, lr
->lr_mode
,
2176 MAX(txg
, lrtxg
), crtxg
);
2178 dmu_buf_will_dirty(db
, tx
);
2180 ASSERT3U(lr
->lr_size
, >=, sizeof (*bbt
));
2181 ASSERT3U(lr
->lr_size
, <=, db
->db_size
);
2182 VERIFY0(dmu_set_bonus(db
, lr
->lr_size
, tx
));
2183 bbt
= ztest_bt_bonus(db
);
2185 ztest_bt_generate(bbt
, os
, lr
->lr_foid
, dnodesize
, -1ULL, lr
->lr_mode
,
2187 ztest_fill_unused_bonus(db
, bbt
, lr
->lr_foid
, os
, bbt
->bt_gen
);
2188 dmu_buf_rele(db
, FTAG
);
2190 (void) ztest_log_setattr(zd
, tx
, lr
);
2194 ztest_object_unlock(zd
, lr
->lr_foid
);
2199 zil_replay_func_t
*ztest_replay_vector
[TX_MAX_TYPE
] = {
2200 NULL
, /* 0 no such transaction type */
2201 ztest_replay_create
, /* TX_CREATE */
2202 NULL
, /* TX_MKDIR */
2203 NULL
, /* TX_MKXATTR */
2204 NULL
, /* TX_SYMLINK */
2205 ztest_replay_remove
, /* TX_REMOVE */
2206 NULL
, /* TX_RMDIR */
2208 NULL
, /* TX_RENAME */
2209 ztest_replay_write
, /* TX_WRITE */
2210 ztest_replay_truncate
, /* TX_TRUNCATE */
2211 ztest_replay_setattr
, /* TX_SETATTR */
2213 NULL
, /* TX_CREATE_ACL */
2214 NULL
, /* TX_CREATE_ATTR */
2215 NULL
, /* TX_CREATE_ACL_ATTR */
2216 NULL
, /* TX_MKDIR_ACL */
2217 NULL
, /* TX_MKDIR_ATTR */
2218 NULL
, /* TX_MKDIR_ACL_ATTR */
2219 NULL
, /* TX_WRITE2 */
2223 * ZIL get_data callbacks
2225 typedef struct ztest_zgd_private
{
2229 } ztest_zgd_private_t
;
2232 ztest_get_done(zgd_t
*zgd
, int error
)
2234 ztest_zgd_private_t
*zzp
= zgd
->zgd_private
;
2235 ztest_ds_t
*zd
= zzp
->z_zd
;
2236 uint64_t object
= zzp
->z_object
;
2239 dmu_buf_rele(zgd
->zgd_db
, zgd
);
2241 ztest_range_unlock(zd
, zzp
->z_rl
);
2242 ztest_object_unlock(zd
, object
);
2244 if (error
== 0 && zgd
->zgd_bp
)
2245 zil_lwb_add_block(zgd
->zgd_lwb
, zgd
->zgd_bp
);
2247 umem_free(zgd
, sizeof (*zgd
));
2248 umem_free(zzp
, sizeof (*zzp
));
2252 ztest_get_data(void *arg
, lr_write_t
*lr
, char *buf
, struct lwb
*lwb
,
2255 ztest_ds_t
*zd
= arg
;
2256 objset_t
*os
= zd
->zd_os
;
2257 uint64_t object
= lr
->lr_foid
;
2258 uint64_t offset
= lr
->lr_offset
;
2259 uint64_t size
= lr
->lr_length
;
2260 uint64_t txg
= lr
->lr_common
.lrc_txg
;
2262 dmu_object_info_t doi
;
2266 ztest_zgd_private_t
*zgd_private
;
2268 ASSERT3P(lwb
, !=, NULL
);
2269 ASSERT3P(zio
, !=, NULL
);
2270 ASSERT3U(size
, !=, 0);
2272 ztest_object_lock(zd
, object
, RL_READER
);
2273 error
= dmu_bonus_hold(os
, object
, FTAG
, &db
);
2275 ztest_object_unlock(zd
, object
);
2279 crtxg
= ztest_bt_bonus(db
)->bt_crtxg
;
2281 if (crtxg
== 0 || crtxg
> txg
) {
2282 dmu_buf_rele(db
, FTAG
);
2283 ztest_object_unlock(zd
, object
);
2287 dmu_object_info_from_db(db
, &doi
);
2288 dmu_buf_rele(db
, FTAG
);
2291 zgd
= umem_zalloc(sizeof (*zgd
), UMEM_NOFAIL
);
2293 zgd_private
= umem_zalloc(sizeof (ztest_zgd_private_t
), UMEM_NOFAIL
);
2294 zgd_private
->z_zd
= zd
;
2295 zgd_private
->z_object
= object
;
2296 zgd
->zgd_private
= zgd_private
;
2298 if (buf
!= NULL
) { /* immediate write */
2299 zgd_private
->z_rl
= ztest_range_lock(zd
, object
, offset
, size
,
2301 zgd
->zgd_rl
= zgd_private
->z_rl
->z_rl
;
2303 error
= dmu_read(os
, object
, offset
, size
, buf
,
2304 DMU_READ_NO_PREFETCH
);
2307 size
= doi
.doi_data_block_size
;
2309 offset
= P2ALIGN(offset
, size
);
2311 ASSERT(offset
< size
);
2315 zgd_private
->z_rl
= ztest_range_lock(zd
, object
, offset
, size
,
2317 zgd
->zgd_rl
= zgd_private
->z_rl
->z_rl
;
2319 error
= dmu_buf_hold(os
, object
, offset
, zgd
, &db
,
2320 DMU_READ_NO_PREFETCH
);
2323 blkptr_t
*bp
= &lr
->lr_blkptr
;
2328 ASSERT(db
->db_offset
== offset
);
2329 ASSERT(db
->db_size
== size
);
2331 error
= dmu_sync(zio
, lr
->lr_common
.lrc_txg
,
2332 ztest_get_done
, zgd
);
2339 ztest_get_done(zgd
, error
);
2345 ztest_lr_alloc(size_t lrsize
, char *name
)
2348 size_t namesize
= name
? strlen(name
) + 1 : 0;
2350 lr
= umem_zalloc(lrsize
+ namesize
, UMEM_NOFAIL
);
2353 bcopy(name
, lr
+ lrsize
, namesize
);
2359 ztest_lr_free(void *lr
, size_t lrsize
, char *name
)
2361 size_t namesize
= name
? strlen(name
) + 1 : 0;
2363 umem_free(lr
, lrsize
+ namesize
);
2367 * Lookup a bunch of objects. Returns the number of objects not found.
2370 ztest_lookup(ztest_ds_t
*zd
, ztest_od_t
*od
, int count
)
2376 ASSERT(MUTEX_HELD(&zd
->zd_dirobj_lock
));
2378 for (i
= 0; i
< count
; i
++, od
++) {
2380 error
= zap_lookup(zd
->zd_os
, od
->od_dir
, od
->od_name
,
2381 sizeof (uint64_t), 1, &od
->od_object
);
2383 ASSERT(error
== ENOENT
);
2384 ASSERT(od
->od_object
== 0);
2388 ztest_block_tag_t
*bbt
;
2389 dmu_object_info_t doi
;
2391 ASSERT(od
->od_object
!= 0);
2392 ASSERT(missing
== 0); /* there should be no gaps */
2394 ztest_object_lock(zd
, od
->od_object
, RL_READER
);
2395 VERIFY3U(0, ==, dmu_bonus_hold(zd
->zd_os
,
2396 od
->od_object
, FTAG
, &db
));
2397 dmu_object_info_from_db(db
, &doi
);
2398 bbt
= ztest_bt_bonus(db
);
2399 ASSERT3U(bbt
->bt_magic
, ==, BT_MAGIC
);
2400 od
->od_type
= doi
.doi_type
;
2401 od
->od_blocksize
= doi
.doi_data_block_size
;
2402 od
->od_gen
= bbt
->bt_gen
;
2403 dmu_buf_rele(db
, FTAG
);
2404 ztest_object_unlock(zd
, od
->od_object
);
2412 ztest_create(ztest_ds_t
*zd
, ztest_od_t
*od
, int count
)
2417 ASSERT(MUTEX_HELD(&zd
->zd_dirobj_lock
));
2419 for (i
= 0; i
< count
; i
++, od
++) {
2426 lr_create_t
*lr
= ztest_lr_alloc(sizeof (*lr
), od
->od_name
);
2428 lr
->lr_doid
= od
->od_dir
;
2429 lr
->lr_foid
= 0; /* 0 to allocate, > 0 to claim */
2430 lr
->lrz_type
= od
->od_crtype
;
2431 lr
->lrz_blocksize
= od
->od_crblocksize
;
2432 lr
->lrz_ibshift
= ztest_random_ibshift();
2433 lr
->lrz_bonustype
= DMU_OT_UINT64_OTHER
;
2434 lr
->lrz_dnodesize
= od
->od_crdnodesize
;
2435 lr
->lr_gen
= od
->od_crgen
;
2436 lr
->lr_crtime
[0] = time(NULL
);
2438 if (ztest_replay_create(zd
, lr
, B_FALSE
) != 0) {
2439 ASSERT(missing
== 0);
2443 od
->od_object
= lr
->lr_foid
;
2444 od
->od_type
= od
->od_crtype
;
2445 od
->od_blocksize
= od
->od_crblocksize
;
2446 od
->od_gen
= od
->od_crgen
;
2447 ASSERT(od
->od_object
!= 0);
2450 ztest_lr_free(lr
, sizeof (*lr
), od
->od_name
);
2457 ztest_remove(ztest_ds_t
*zd
, ztest_od_t
*od
, int count
)
2463 ASSERT(MUTEX_HELD(&zd
->zd_dirobj_lock
));
2467 for (i
= count
- 1; i
>= 0; i
--, od
--) {
2474 * No object was found.
2476 if (od
->od_object
== 0)
2479 lr_remove_t
*lr
= ztest_lr_alloc(sizeof (*lr
), od
->od_name
);
2481 lr
->lr_doid
= od
->od_dir
;
2483 if ((error
= ztest_replay_remove(zd
, lr
, B_FALSE
)) != 0) {
2484 ASSERT3U(error
, ==, ENOSPC
);
2489 ztest_lr_free(lr
, sizeof (*lr
), od
->od_name
);
2496 ztest_write(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
, uint64_t size
,
2502 lr
= ztest_lr_alloc(sizeof (*lr
) + size
, NULL
);
2504 lr
->lr_foid
= object
;
2505 lr
->lr_offset
= offset
;
2506 lr
->lr_length
= size
;
2508 BP_ZERO(&lr
->lr_blkptr
);
2510 bcopy(data
, lr
+ 1, size
);
2512 error
= ztest_replay_write(zd
, lr
, B_FALSE
);
2514 ztest_lr_free(lr
, sizeof (*lr
) + size
, NULL
);
2520 ztest_truncate(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
, uint64_t size
)
2525 lr
= ztest_lr_alloc(sizeof (*lr
), NULL
);
2527 lr
->lr_foid
= object
;
2528 lr
->lr_offset
= offset
;
2529 lr
->lr_length
= size
;
2531 error
= ztest_replay_truncate(zd
, lr
, B_FALSE
);
2533 ztest_lr_free(lr
, sizeof (*lr
), NULL
);
2539 ztest_setattr(ztest_ds_t
*zd
, uint64_t object
)
2544 lr
= ztest_lr_alloc(sizeof (*lr
), NULL
);
2546 lr
->lr_foid
= object
;
2550 error
= ztest_replay_setattr(zd
, lr
, B_FALSE
);
2552 ztest_lr_free(lr
, sizeof (*lr
), NULL
);
2558 ztest_prealloc(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
, uint64_t size
)
2560 objset_t
*os
= zd
->zd_os
;
2565 txg_wait_synced(dmu_objset_pool(os
), 0);
2567 ztest_object_lock(zd
, object
, RL_READER
);
2568 rl
= ztest_range_lock(zd
, object
, offset
, size
, RL_WRITER
);
2570 tx
= dmu_tx_create(os
);
2572 dmu_tx_hold_write(tx
, object
, offset
, size
);
2574 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
2577 dmu_prealloc(os
, object
, offset
, size
, tx
);
2579 txg_wait_synced(dmu_objset_pool(os
), txg
);
2581 (void) dmu_free_long_range(os
, object
, offset
, size
);
2584 ztest_range_unlock(zd
, rl
);
2585 ztest_object_unlock(zd
, object
);
2589 ztest_io(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
)
2592 ztest_block_tag_t wbt
;
2593 dmu_object_info_t doi
;
2594 enum ztest_io_type io_type
;
2598 VERIFY(dmu_object_info(zd
->zd_os
, object
, &doi
) == 0);
2599 blocksize
= doi
.doi_data_block_size
;
2600 data
= umem_alloc(blocksize
, UMEM_NOFAIL
);
2603 * Pick an i/o type at random, biased toward writing block tags.
2605 io_type
= ztest_random(ZTEST_IO_TYPES
);
2606 if (ztest_random(2) == 0)
2607 io_type
= ZTEST_IO_WRITE_TAG
;
2609 (void) pthread_rwlock_rdlock(&zd
->zd_zilog_lock
);
2613 case ZTEST_IO_WRITE_TAG
:
2614 ztest_bt_generate(&wbt
, zd
->zd_os
, object
, doi
.doi_dnodesize
,
2616 (void) ztest_write(zd
, object
, offset
, sizeof (wbt
), &wbt
);
2619 case ZTEST_IO_WRITE_PATTERN
:
2620 (void) memset(data
, 'a' + (object
+ offset
) % 5, blocksize
);
2621 if (ztest_random(2) == 0) {
2623 * Induce fletcher2 collisions to ensure that
2624 * zio_ddt_collision() detects and resolves them
2625 * when using fletcher2-verify for deduplication.
2627 ((uint64_t *)data
)[0] ^= 1ULL << 63;
2628 ((uint64_t *)data
)[4] ^= 1ULL << 63;
2630 (void) ztest_write(zd
, object
, offset
, blocksize
, data
);
2633 case ZTEST_IO_WRITE_ZEROES
:
2634 bzero(data
, blocksize
);
2635 (void) ztest_write(zd
, object
, offset
, blocksize
, data
);
2638 case ZTEST_IO_TRUNCATE
:
2639 (void) ztest_truncate(zd
, object
, offset
, blocksize
);
2642 case ZTEST_IO_SETATTR
:
2643 (void) ztest_setattr(zd
, object
);
2648 case ZTEST_IO_REWRITE
:
2649 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
2650 err
= ztest_dsl_prop_set_uint64(zd
->zd_name
,
2651 ZFS_PROP_CHECKSUM
, spa_dedup_checksum(ztest_spa
),
2653 VERIFY(err
== 0 || err
== ENOSPC
);
2654 err
= ztest_dsl_prop_set_uint64(zd
->zd_name
,
2655 ZFS_PROP_COMPRESSION
,
2656 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION
),
2658 VERIFY(err
== 0 || err
== ENOSPC
);
2659 (void) pthread_rwlock_unlock(&ztest_name_lock
);
2661 VERIFY0(dmu_read(zd
->zd_os
, object
, offset
, blocksize
, data
,
2662 DMU_READ_NO_PREFETCH
));
2664 (void) ztest_write(zd
, object
, offset
, blocksize
, data
);
2668 (void) pthread_rwlock_unlock(&zd
->zd_zilog_lock
);
2670 umem_free(data
, blocksize
);
2674 * Initialize an object description template.
2677 ztest_od_init(ztest_od_t
*od
, uint64_t id
, char *tag
, uint64_t index
,
2678 dmu_object_type_t type
, uint64_t blocksize
, uint64_t dnodesize
,
2681 od
->od_dir
= ZTEST_DIROBJ
;
2684 od
->od_crtype
= type
;
2685 od
->od_crblocksize
= blocksize
? blocksize
: ztest_random_blocksize();
2686 od
->od_crdnodesize
= dnodesize
? dnodesize
: ztest_random_dnodesize();
2689 od
->od_type
= DMU_OT_NONE
;
2690 od
->od_blocksize
= 0;
2693 (void) snprintf(od
->od_name
, sizeof (od
->od_name
), "%s(%lld)[%llu]",
2694 tag
, (longlong_t
)id
, (u_longlong_t
)index
);
2698 * Lookup or create the objects for a test using the od template.
2699 * If the objects do not all exist, or if 'remove' is specified,
2700 * remove any existing objects and create new ones. Otherwise,
2701 * use the existing objects.
2704 ztest_object_init(ztest_ds_t
*zd
, ztest_od_t
*od
, size_t size
, boolean_t remove
)
2706 int count
= size
/ sizeof (*od
);
2709 mutex_enter(&zd
->zd_dirobj_lock
);
2710 if ((ztest_lookup(zd
, od
, count
) != 0 || remove
) &&
2711 (ztest_remove(zd
, od
, count
) != 0 ||
2712 ztest_create(zd
, od
, count
) != 0))
2715 mutex_exit(&zd
->zd_dirobj_lock
);
2722 ztest_zil_commit(ztest_ds_t
*zd
, uint64_t id
)
2724 zilog_t
*zilog
= zd
->zd_zilog
;
2726 (void) pthread_rwlock_rdlock(&zd
->zd_zilog_lock
);
2728 zil_commit(zilog
, ztest_random(ZTEST_OBJECTS
));
2731 * Remember the committed values in zd, which is in parent/child
2732 * shared memory. If we die, the next iteration of ztest_run()
2733 * will verify that the log really does contain this record.
2735 mutex_enter(&zilog
->zl_lock
);
2736 ASSERT(zd
->zd_shared
!= NULL
);
2737 ASSERT3U(zd
->zd_shared
->zd_seq
, <=, zilog
->zl_commit_lr_seq
);
2738 zd
->zd_shared
->zd_seq
= zilog
->zl_commit_lr_seq
;
2739 mutex_exit(&zilog
->zl_lock
);
2741 (void) pthread_rwlock_unlock(&zd
->zd_zilog_lock
);
2745 * This function is designed to simulate the operations that occur during a
2746 * mount/unmount operation. We hold the dataset across these operations in an
2747 * attempt to expose any implicit assumptions about ZIL management.
2751 ztest_zil_remount(ztest_ds_t
*zd
, uint64_t id
)
2753 objset_t
*os
= zd
->zd_os
;
2756 * We grab the zd_dirobj_lock to ensure that no other thread is
2757 * updating the zil (i.e. adding in-memory log records) and the
2758 * zd_zilog_lock to block any I/O.
2760 mutex_enter(&zd
->zd_dirobj_lock
);
2761 (void) pthread_rwlock_wrlock(&zd
->zd_zilog_lock
);
2763 /* zfsvfs_teardown() */
2764 zil_close(zd
->zd_zilog
);
2766 /* zfsvfs_setup() */
2767 VERIFY(zil_open(os
, ztest_get_data
) == zd
->zd_zilog
);
2768 zil_replay(os
, zd
, ztest_replay_vector
);
2770 (void) pthread_rwlock_unlock(&zd
->zd_zilog_lock
);
2771 mutex_exit(&zd
->zd_dirobj_lock
);
2775 * Verify that we can't destroy an active pool, create an existing pool,
2776 * or create a pool with a bad vdev spec.
2780 ztest_spa_create_destroy(ztest_ds_t
*zd
, uint64_t id
)
2782 ztest_shared_opts_t
*zo
= &ztest_opts
;
2786 if (zo
->zo_mmp_test
)
2790 * Attempt to create using a bad file.
2792 nvroot
= make_vdev_root("/dev/bogus", NULL
, NULL
, 0, 0, NULL
, 0, 0, 1);
2793 VERIFY3U(ENOENT
, ==,
2794 spa_create("ztest_bad_file", nvroot
, NULL
, NULL
, NULL
));
2795 nvlist_free(nvroot
);
2798 * Attempt to create using a bad mirror.
2800 nvroot
= make_vdev_root("/dev/bogus", NULL
, NULL
, 0, 0, NULL
, 0, 2, 1);
2801 VERIFY3U(ENOENT
, ==,
2802 spa_create("ztest_bad_mirror", nvroot
, NULL
, NULL
, NULL
));
2803 nvlist_free(nvroot
);
2806 * Attempt to create an existing pool. It shouldn't matter
2807 * what's in the nvroot; we should fail with EEXIST.
2809 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
2810 nvroot
= make_vdev_root("/dev/bogus", NULL
, NULL
, 0, 0, NULL
, 0, 0, 1);
2811 VERIFY3U(EEXIST
, ==,
2812 spa_create(zo
->zo_pool
, nvroot
, NULL
, NULL
, NULL
));
2813 nvlist_free(nvroot
);
2814 VERIFY3U(0, ==, spa_open(zo
->zo_pool
, &spa
, FTAG
));
2815 VERIFY3U(EBUSY
, ==, spa_destroy(zo
->zo_pool
));
2816 spa_close(spa
, FTAG
);
2818 (void) pthread_rwlock_unlock(&ztest_name_lock
);
2822 * Start and then stop the MMP threads to ensure the startup and shutdown code
2823 * works properly. Actual protection and property-related code tested via ZTS.
2827 ztest_mmp_enable_disable(ztest_ds_t
*zd
, uint64_t id
)
2829 ztest_shared_opts_t
*zo
= &ztest_opts
;
2830 spa_t
*spa
= ztest_spa
;
2832 if (zo
->zo_mmp_test
)
2835 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
2836 mutex_enter(&spa
->spa_props_lock
);
2838 zfs_multihost_fail_intervals
= 0;
2840 if (!spa_multihost(spa
)) {
2841 spa
->spa_multihost
= B_TRUE
;
2842 mmp_thread_start(spa
);
2845 mutex_exit(&spa
->spa_props_lock
);
2846 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
2848 txg_wait_synced(spa_get_dsl(spa
), 0);
2849 mmp_signal_all_threads();
2850 txg_wait_synced(spa_get_dsl(spa
), 0);
2852 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
2853 mutex_enter(&spa
->spa_props_lock
);
2855 if (spa_multihost(spa
)) {
2856 mmp_thread_stop(spa
);
2857 spa
->spa_multihost
= B_FALSE
;
2860 mutex_exit(&spa
->spa_props_lock
);
2861 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
2866 ztest_spa_upgrade(ztest_ds_t
*zd
, uint64_t id
)
2869 uint64_t initial_version
= SPA_VERSION_INITIAL
;
2870 uint64_t version
, newversion
;
2871 nvlist_t
*nvroot
, *props
;
2874 if (ztest_opts
.zo_mmp_test
)
2877 mutex_enter(&ztest_vdev_lock
);
2878 name
= kmem_asprintf("%s_upgrade", ztest_opts
.zo_pool
);
2881 * Clean up from previous runs.
2883 (void) spa_destroy(name
);
2885 nvroot
= make_vdev_root(NULL
, NULL
, name
, ztest_opts
.zo_vdev_size
, 0,
2886 NULL
, ztest_opts
.zo_raidz
, ztest_opts
.zo_mirrors
, 1);
2889 * If we're configuring a RAIDZ device then make sure that the
2890 * the initial version is capable of supporting that feature.
2892 switch (ztest_opts
.zo_raidz_parity
) {
2895 initial_version
= SPA_VERSION_INITIAL
;
2898 initial_version
= SPA_VERSION_RAIDZ2
;
2901 initial_version
= SPA_VERSION_RAIDZ3
;
2906 * Create a pool with a spa version that can be upgraded. Pick
2907 * a value between initial_version and SPA_VERSION_BEFORE_FEATURES.
2910 version
= ztest_random_spa_version(initial_version
);
2911 } while (version
> SPA_VERSION_BEFORE_FEATURES
);
2913 props
= fnvlist_alloc();
2914 fnvlist_add_uint64(props
,
2915 zpool_prop_to_name(ZPOOL_PROP_VERSION
), version
);
2916 VERIFY3S(spa_create(name
, nvroot
, props
, NULL
, NULL
), ==, 0);
2917 fnvlist_free(nvroot
);
2918 fnvlist_free(props
);
2920 VERIFY3S(spa_open(name
, &spa
, FTAG
), ==, 0);
2921 VERIFY3U(spa_version(spa
), ==, version
);
2922 newversion
= ztest_random_spa_version(version
+ 1);
2924 if (ztest_opts
.zo_verbose
>= 4) {
2925 (void) printf("upgrading spa version from %llu to %llu\n",
2926 (u_longlong_t
)version
, (u_longlong_t
)newversion
);
2929 spa_upgrade(spa
, newversion
);
2930 VERIFY3U(spa_version(spa
), >, version
);
2931 VERIFY3U(spa_version(spa
), ==, fnvlist_lookup_uint64(spa
->spa_config
,
2932 zpool_prop_to_name(ZPOOL_PROP_VERSION
)));
2933 spa_close(spa
, FTAG
);
2936 mutex_exit(&ztest_vdev_lock
);
2940 ztest_spa_checkpoint(spa_t
*spa
)
2942 ASSERT(MUTEX_HELD(&ztest_checkpoint_lock
));
2944 int error
= spa_checkpoint(spa
->spa_name
);
2948 case ZFS_ERR_DEVRM_IN_PROGRESS
:
2949 case ZFS_ERR_DISCARDING_CHECKPOINT
:
2950 case ZFS_ERR_CHECKPOINT_EXISTS
:
2953 ztest_record_enospc(FTAG
);
2956 fatal(0, "spa_checkpoint(%s) = %d", spa
->spa_name
, error
);
2961 ztest_spa_discard_checkpoint(spa_t
*spa
)
2963 ASSERT(MUTEX_HELD(&ztest_checkpoint_lock
));
2965 int error
= spa_checkpoint_discard(spa
->spa_name
);
2969 case ZFS_ERR_DISCARDING_CHECKPOINT
:
2970 case ZFS_ERR_NO_CHECKPOINT
:
2973 fatal(0, "spa_discard_checkpoint(%s) = %d",
2974 spa
->spa_name
, error
);
2981 ztest_spa_checkpoint_create_discard(ztest_ds_t
*zd
, uint64_t id
)
2983 spa_t
*spa
= ztest_spa
;
2985 mutex_enter(&ztest_checkpoint_lock
);
2986 if (ztest_random(2) == 0) {
2987 ztest_spa_checkpoint(spa
);
2989 ztest_spa_discard_checkpoint(spa
);
2991 mutex_exit(&ztest_checkpoint_lock
);
2996 vdev_lookup_by_path(vdev_t
*vd
, const char *path
)
3001 if (vd
->vdev_path
!= NULL
&& strcmp(path
, vd
->vdev_path
) == 0)
3004 for (c
= 0; c
< vd
->vdev_children
; c
++)
3005 if ((mvd
= vdev_lookup_by_path(vd
->vdev_child
[c
], path
)) !=
3013 * Find the first available hole which can be used as a top-level.
3016 find_vdev_hole(spa_t
*spa
)
3018 vdev_t
*rvd
= spa
->spa_root_vdev
;
3021 ASSERT(spa_config_held(spa
, SCL_VDEV
, RW_READER
) == SCL_VDEV
);
3023 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
3024 vdev_t
*cvd
= rvd
->vdev_child
[c
];
3026 if (cvd
->vdev_ishole
)
3033 * Verify that vdev_add() works as expected.
3037 ztest_vdev_add_remove(ztest_ds_t
*zd
, uint64_t id
)
3039 ztest_shared_t
*zs
= ztest_shared
;
3040 spa_t
*spa
= ztest_spa
;
3046 if (ztest_opts
.zo_mmp_test
)
3049 mutex_enter(&ztest_vdev_lock
);
3050 leaves
= MAX(zs
->zs_mirrors
+ zs
->zs_splits
, 1) * ztest_opts
.zo_raidz
;
3052 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
3054 ztest_shared
->zs_vdev_next_leaf
= find_vdev_hole(spa
) * leaves
;
3057 * If we have slogs then remove them 1/4 of the time.
3059 if (spa_has_slogs(spa
) && ztest_random(4) == 0) {
3060 metaslab_group_t
*mg
;
3063 * find the first real slog in log allocation class
3065 mg
= spa_log_class(spa
)->mc_rotor
;
3066 while (!mg
->mg_vd
->vdev_islog
)
3069 guid
= mg
->mg_vd
->vdev_guid
;
3071 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3074 * We have to grab the zs_name_lock as writer to
3075 * prevent a race between removing a slog (dmu_objset_find)
3076 * and destroying a dataset. Removing the slog will
3077 * grab a reference on the dataset which may cause
3078 * dsl_destroy_head() to fail with EBUSY thus
3079 * leaving the dataset in an inconsistent state.
3081 pthread_rwlock_wrlock(&ztest_name_lock
);
3082 error
= spa_vdev_remove(spa
, guid
, B_FALSE
);
3083 pthread_rwlock_unlock(&ztest_name_lock
);
3087 case EEXIST
: /* Generic zil_reset() error */
3088 case EBUSY
: /* Replay required */
3089 case EACCES
: /* Crypto key not loaded */
3090 case ZFS_ERR_CHECKPOINT_EXISTS
:
3091 case ZFS_ERR_DISCARDING_CHECKPOINT
:
3094 fatal(0, "spa_vdev_remove() = %d", error
);
3097 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3100 * Make 1/4 of the devices be log devices
3102 nvroot
= make_vdev_root(NULL
, NULL
, NULL
,
3103 ztest_opts
.zo_vdev_size
, 0, (ztest_random(4) == 0) ?
3104 "log" : NULL
, ztest_opts
.zo_raidz
, zs
->zs_mirrors
, 1);
3106 error
= spa_vdev_add(spa
, nvroot
);
3107 nvlist_free(nvroot
);
3113 ztest_record_enospc("spa_vdev_add");
3116 fatal(0, "spa_vdev_add() = %d", error
);
3120 mutex_exit(&ztest_vdev_lock
);
3125 ztest_vdev_class_add(ztest_ds_t
*zd
, uint64_t id
)
3127 ztest_shared_t
*zs
= ztest_shared
;
3128 spa_t
*spa
= ztest_spa
;
3131 const char *class = (ztest_random(2) == 0) ?
3132 VDEV_ALLOC_BIAS_SPECIAL
: VDEV_ALLOC_BIAS_DEDUP
;
3136 * By default add a special vdev 50% of the time
3138 if ((ztest_opts
.zo_special_vdevs
== ZTEST_VDEV_CLASS_OFF
) ||
3139 (ztest_opts
.zo_special_vdevs
== ZTEST_VDEV_CLASS_RND
&&
3140 ztest_random(2) == 0)) {
3144 mutex_enter(&ztest_vdev_lock
);
3146 /* Only test with mirrors */
3147 if (zs
->zs_mirrors
< 2) {
3148 mutex_exit(&ztest_vdev_lock
);
3152 /* requires feature@allocation_classes */
3153 if (!spa_feature_is_enabled(spa
, SPA_FEATURE_ALLOCATION_CLASSES
)) {
3154 mutex_exit(&ztest_vdev_lock
);
3158 leaves
= MAX(zs
->zs_mirrors
+ zs
->zs_splits
, 1) * ztest_opts
.zo_raidz
;
3160 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
3161 ztest_shared
->zs_vdev_next_leaf
= find_vdev_hole(spa
) * leaves
;
3162 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3164 nvroot
= make_vdev_root(NULL
, NULL
, NULL
, ztest_opts
.zo_vdev_size
, 0,
3165 class, ztest_opts
.zo_raidz
, zs
->zs_mirrors
, 1);
3167 error
= spa_vdev_add(spa
, nvroot
);
3168 nvlist_free(nvroot
);
3170 if (error
== ENOSPC
)
3171 ztest_record_enospc("spa_vdev_add");
3172 else if (error
!= 0)
3173 fatal(0, "spa_vdev_add() = %d", error
);
3176 * 50% of the time allow small blocks in the special class
3179 spa_special_class(spa
)->mc_groups
== 1 && ztest_random(2) == 0) {
3180 if (ztest_opts
.zo_verbose
>= 3)
3181 (void) printf("Enabling special VDEV small blocks\n");
3182 (void) ztest_dsl_prop_set_uint64(zd
->zd_name
,
3183 ZFS_PROP_SPECIAL_SMALL_BLOCKS
, 32768, B_FALSE
);
3186 mutex_exit(&ztest_vdev_lock
);
3188 if (ztest_opts
.zo_verbose
>= 3) {
3189 metaslab_class_t
*mc
;
3191 if (strcmp(class, VDEV_ALLOC_BIAS_SPECIAL
) == 0)
3192 mc
= spa_special_class(spa
);
3194 mc
= spa_dedup_class(spa
);
3195 (void) printf("Added a %s mirrored vdev (of %d)\n",
3196 class, (int)mc
->mc_groups
);
3201 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
3205 ztest_vdev_aux_add_remove(ztest_ds_t
*zd
, uint64_t id
)
3207 ztest_shared_t
*zs
= ztest_shared
;
3208 spa_t
*spa
= ztest_spa
;
3209 vdev_t
*rvd
= spa
->spa_root_vdev
;
3210 spa_aux_vdev_t
*sav
;
3216 if (ztest_opts
.zo_mmp_test
)
3219 path
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
3221 if (ztest_random(2) == 0) {
3222 sav
= &spa
->spa_spares
;
3223 aux
= ZPOOL_CONFIG_SPARES
;
3225 sav
= &spa
->spa_l2cache
;
3226 aux
= ZPOOL_CONFIG_L2CACHE
;
3229 mutex_enter(&ztest_vdev_lock
);
3231 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
3233 if (sav
->sav_count
!= 0 && ztest_random(4) == 0) {
3235 * Pick a random device to remove.
3237 guid
= sav
->sav_vdevs
[ztest_random(sav
->sav_count
)]->vdev_guid
;
3240 * Find an unused device we can add.
3242 zs
->zs_vdev_aux
= 0;
3245 (void) snprintf(path
, MAXPATHLEN
, ztest_aux_template
,
3246 ztest_opts
.zo_dir
, ztest_opts
.zo_pool
, aux
,
3248 for (c
= 0; c
< sav
->sav_count
; c
++)
3249 if (strcmp(sav
->sav_vdevs
[c
]->vdev_path
,
3252 if (c
== sav
->sav_count
&&
3253 vdev_lookup_by_path(rvd
, path
) == NULL
)
3259 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3265 nvlist_t
*nvroot
= make_vdev_root(NULL
, aux
, NULL
,
3266 (ztest_opts
.zo_vdev_size
* 5) / 4, 0, NULL
, 0, 0, 1);
3267 error
= spa_vdev_add(spa
, nvroot
);
3273 fatal(0, "spa_vdev_add(%p) = %d", nvroot
, error
);
3275 nvlist_free(nvroot
);
3278 * Remove an existing device. Sometimes, dirty its
3279 * vdev state first to make sure we handle removal
3280 * of devices that have pending state changes.
3282 if (ztest_random(2) == 0)
3283 (void) vdev_online(spa
, guid
, 0, NULL
);
3285 error
= spa_vdev_remove(spa
, guid
, B_FALSE
);
3290 case ZFS_ERR_CHECKPOINT_EXISTS
:
3291 case ZFS_ERR_DISCARDING_CHECKPOINT
:
3294 fatal(0, "spa_vdev_remove(%llu) = %d", guid
, error
);
3298 mutex_exit(&ztest_vdev_lock
);
3300 umem_free(path
, MAXPATHLEN
);
3304 * split a pool if it has mirror tlvdevs
3308 ztest_split_pool(ztest_ds_t
*zd
, uint64_t id
)
3310 ztest_shared_t
*zs
= ztest_shared
;
3311 spa_t
*spa
= ztest_spa
;
3312 vdev_t
*rvd
= spa
->spa_root_vdev
;
3313 nvlist_t
*tree
, **child
, *config
, *split
, **schild
;
3314 uint_t c
, children
, schildren
= 0, lastlogid
= 0;
3317 if (ztest_opts
.zo_mmp_test
)
3320 mutex_enter(&ztest_vdev_lock
);
3322 /* ensure we have a useable config; mirrors of raidz aren't supported */
3323 if (zs
->zs_mirrors
< 3 || ztest_opts
.zo_raidz
> 1) {
3324 mutex_exit(&ztest_vdev_lock
);
3328 /* clean up the old pool, if any */
3329 (void) spa_destroy("splitp");
3331 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
3333 /* generate a config from the existing config */
3334 mutex_enter(&spa
->spa_props_lock
);
3335 VERIFY(nvlist_lookup_nvlist(spa
->spa_config
, ZPOOL_CONFIG_VDEV_TREE
,
3337 mutex_exit(&spa
->spa_props_lock
);
3339 VERIFY(nvlist_lookup_nvlist_array(tree
, ZPOOL_CONFIG_CHILDREN
, &child
,
3342 schild
= malloc(rvd
->vdev_children
* sizeof (nvlist_t
*));
3343 for (c
= 0; c
< children
; c
++) {
3344 vdev_t
*tvd
= rvd
->vdev_child
[c
];
3348 if (tvd
->vdev_islog
|| tvd
->vdev_ops
== &vdev_hole_ops
) {
3349 VERIFY(nvlist_alloc(&schild
[schildren
], NV_UNIQUE_NAME
,
3351 VERIFY(nvlist_add_string(schild
[schildren
],
3352 ZPOOL_CONFIG_TYPE
, VDEV_TYPE_HOLE
) == 0);
3353 VERIFY(nvlist_add_uint64(schild
[schildren
],
3354 ZPOOL_CONFIG_IS_HOLE
, 1) == 0);
3356 lastlogid
= schildren
;
3361 VERIFY(nvlist_lookup_nvlist_array(child
[c
],
3362 ZPOOL_CONFIG_CHILDREN
, &mchild
, &mchildren
) == 0);
3363 VERIFY(nvlist_dup(mchild
[0], &schild
[schildren
++], 0) == 0);
3366 /* OK, create a config that can be used to split */
3367 VERIFY(nvlist_alloc(&split
, NV_UNIQUE_NAME
, 0) == 0);
3368 VERIFY(nvlist_add_string(split
, ZPOOL_CONFIG_TYPE
,
3369 VDEV_TYPE_ROOT
) == 0);
3370 VERIFY(nvlist_add_nvlist_array(split
, ZPOOL_CONFIG_CHILDREN
, schild
,
3371 lastlogid
!= 0 ? lastlogid
: schildren
) == 0);
3373 VERIFY(nvlist_alloc(&config
, NV_UNIQUE_NAME
, 0) == 0);
3374 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, split
) == 0);
3376 for (c
= 0; c
< schildren
; c
++)
3377 nvlist_free(schild
[c
]);
3381 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3383 (void) pthread_rwlock_wrlock(&ztest_name_lock
);
3384 error
= spa_vdev_split_mirror(spa
, "splitp", config
, NULL
, B_FALSE
);
3385 (void) pthread_rwlock_unlock(&ztest_name_lock
);
3387 nvlist_free(config
);
3390 (void) printf("successful split - results:\n");
3391 mutex_enter(&spa_namespace_lock
);
3392 show_pool_stats(spa
);
3393 show_pool_stats(spa_lookup("splitp"));
3394 mutex_exit(&spa_namespace_lock
);
3398 mutex_exit(&ztest_vdev_lock
);
3402 * Verify that we can attach and detach devices.
3406 ztest_vdev_attach_detach(ztest_ds_t
*zd
, uint64_t id
)
3408 ztest_shared_t
*zs
= ztest_shared
;
3409 spa_t
*spa
= ztest_spa
;
3410 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
3411 vdev_t
*rvd
= spa
->spa_root_vdev
;
3412 vdev_t
*oldvd
, *newvd
, *pvd
;
3416 uint64_t ashift
= ztest_get_ashift();
3417 uint64_t oldguid
, pguid
;
3418 uint64_t oldsize
, newsize
;
3419 char *oldpath
, *newpath
;
3421 int oldvd_has_siblings
= B_FALSE
;
3422 int newvd_is_spare
= B_FALSE
;
3424 int error
, expected_error
;
3426 if (ztest_opts
.zo_mmp_test
)
3429 oldpath
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
3430 newpath
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
3432 mutex_enter(&ztest_vdev_lock
);
3433 leaves
= MAX(zs
->zs_mirrors
, 1) * ztest_opts
.zo_raidz
;
3435 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3438 * If a vdev is in the process of being removed, its removal may
3439 * finish while we are in progress, leading to an unexpected error
3440 * value. Don't bother trying to attach while we are in the middle
3443 if (ztest_device_removal_active
) {
3444 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3445 mutex_exit(&ztest_vdev_lock
);
3450 * Decide whether to do an attach or a replace.
3452 replacing
= ztest_random(2);
3455 * Pick a random top-level vdev.
3457 top
= ztest_random_vdev_top(spa
, B_TRUE
);
3460 * Pick a random leaf within it.
3462 leaf
= ztest_random(leaves
);
3467 oldvd
= rvd
->vdev_child
[top
];
3469 /* pick a child from the mirror */
3470 if (zs
->zs_mirrors
>= 1) {
3471 ASSERT(oldvd
->vdev_ops
== &vdev_mirror_ops
);
3472 ASSERT(oldvd
->vdev_children
>= zs
->zs_mirrors
);
3473 oldvd
= oldvd
->vdev_child
[leaf
/ ztest_opts
.zo_raidz
];
3476 /* pick a child out of the raidz group */
3477 if (ztest_opts
.zo_raidz
> 1) {
3478 ASSERT(oldvd
->vdev_ops
== &vdev_raidz_ops
);
3479 ASSERT(oldvd
->vdev_children
== ztest_opts
.zo_raidz
);
3480 oldvd
= oldvd
->vdev_child
[leaf
% ztest_opts
.zo_raidz
];
3484 * If we're already doing an attach or replace, oldvd may be a
3485 * mirror vdev -- in which case, pick a random child.
3487 while (oldvd
->vdev_children
!= 0) {
3488 oldvd_has_siblings
= B_TRUE
;
3489 ASSERT(oldvd
->vdev_children
>= 2);
3490 oldvd
= oldvd
->vdev_child
[ztest_random(oldvd
->vdev_children
)];
3493 oldguid
= oldvd
->vdev_guid
;
3494 oldsize
= vdev_get_min_asize(oldvd
);
3495 oldvd_is_log
= oldvd
->vdev_top
->vdev_islog
;
3496 (void) strcpy(oldpath
, oldvd
->vdev_path
);
3497 pvd
= oldvd
->vdev_parent
;
3498 pguid
= pvd
->vdev_guid
;
3501 * If oldvd has siblings, then half of the time, detach it.
3503 if (oldvd_has_siblings
&& ztest_random(2) == 0) {
3504 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3505 error
= spa_vdev_detach(spa
, oldguid
, pguid
, B_FALSE
);
3506 if (error
!= 0 && error
!= ENODEV
&& error
!= EBUSY
&&
3507 error
!= ENOTSUP
&& error
!= ZFS_ERR_CHECKPOINT_EXISTS
&&
3508 error
!= ZFS_ERR_DISCARDING_CHECKPOINT
)
3509 fatal(0, "detach (%s) returned %d", oldpath
, error
);
3514 * For the new vdev, choose with equal probability between the two
3515 * standard paths (ending in either 'a' or 'b') or a random hot spare.
3517 if (sav
->sav_count
!= 0 && ztest_random(3) == 0) {
3518 newvd
= sav
->sav_vdevs
[ztest_random(sav
->sav_count
)];
3519 newvd_is_spare
= B_TRUE
;
3520 (void) strcpy(newpath
, newvd
->vdev_path
);
3522 (void) snprintf(newpath
, MAXPATHLEN
, ztest_dev_template
,
3523 ztest_opts
.zo_dir
, ztest_opts
.zo_pool
,
3524 top
* leaves
+ leaf
);
3525 if (ztest_random(2) == 0)
3526 newpath
[strlen(newpath
) - 1] = 'b';
3527 newvd
= vdev_lookup_by_path(rvd
, newpath
);
3532 * Reopen to ensure the vdev's asize field isn't stale.
3535 newsize
= vdev_get_min_asize(newvd
);
3538 * Make newsize a little bigger or smaller than oldsize.
3539 * If it's smaller, the attach should fail.
3540 * If it's larger, and we're doing a replace,
3541 * we should get dynamic LUN growth when we're done.
3543 newsize
= 10 * oldsize
/ (9 + ztest_random(3));
3547 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
3548 * unless it's a replace; in that case any non-replacing parent is OK.
3550 * If newvd is already part of the pool, it should fail with EBUSY.
3552 * If newvd is too small, it should fail with EOVERFLOW.
3554 if (pvd
->vdev_ops
!= &vdev_mirror_ops
&&
3555 pvd
->vdev_ops
!= &vdev_root_ops
&& (!replacing
||
3556 pvd
->vdev_ops
== &vdev_replacing_ops
||
3557 pvd
->vdev_ops
== &vdev_spare_ops
))
3558 expected_error
= ENOTSUP
;
3559 else if (newvd_is_spare
&& (!replacing
|| oldvd_is_log
))
3560 expected_error
= ENOTSUP
;
3561 else if (newvd
== oldvd
)
3562 expected_error
= replacing
? 0 : EBUSY
;
3563 else if (vdev_lookup_by_path(rvd
, newpath
) != NULL
)
3564 expected_error
= EBUSY
;
3565 else if (newsize
< oldsize
)
3566 expected_error
= EOVERFLOW
;
3567 else if (ashift
> oldvd
->vdev_top
->vdev_ashift
)
3568 expected_error
= EDOM
;
3572 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3575 * Build the nvlist describing newpath.
3577 root
= make_vdev_root(newpath
, NULL
, NULL
, newvd
== NULL
? newsize
: 0,
3578 ashift
, NULL
, 0, 0, 1);
3580 error
= spa_vdev_attach(spa
, oldguid
, root
, replacing
);
3585 * If our parent was the replacing vdev, but the replace completed,
3586 * then instead of failing with ENOTSUP we may either succeed,
3587 * fail with ENODEV, or fail with EOVERFLOW.
3589 if (expected_error
== ENOTSUP
&&
3590 (error
== 0 || error
== ENODEV
|| error
== EOVERFLOW
))
3591 expected_error
= error
;
3594 * If someone grew the LUN, the replacement may be too small.
3596 if (error
== EOVERFLOW
|| error
== EBUSY
)
3597 expected_error
= error
;
3599 if (error
== ZFS_ERR_CHECKPOINT_EXISTS
||
3600 error
== ZFS_ERR_DISCARDING_CHECKPOINT
)
3601 expected_error
= error
;
3603 /* XXX workaround 6690467 */
3604 if (error
!= expected_error
&& expected_error
!= EBUSY
) {
3605 fatal(0, "attach (%s %llu, %s %llu, %d) "
3606 "returned %d, expected %d",
3607 oldpath
, oldsize
, newpath
,
3608 newsize
, replacing
, error
, expected_error
);
3611 mutex_exit(&ztest_vdev_lock
);
3613 umem_free(oldpath
, MAXPATHLEN
);
3614 umem_free(newpath
, MAXPATHLEN
);
3619 ztest_device_removal(ztest_ds_t
*zd
, uint64_t id
)
3621 spa_t
*spa
= ztest_spa
;
3626 mutex_enter(&ztest_vdev_lock
);
3628 if (ztest_device_removal_active
) {
3629 mutex_exit(&ztest_vdev_lock
);
3634 * Remove a random top-level vdev and wait for removal to finish.
3636 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
3637 vd
= vdev_lookup_top(spa
, ztest_random_vdev_top(spa
, B_FALSE
));
3638 guid
= vd
->vdev_guid
;
3639 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3641 error
= spa_vdev_remove(spa
, guid
, B_FALSE
);
3643 ztest_device_removal_active
= B_TRUE
;
3644 mutex_exit(&ztest_vdev_lock
);
3646 while (spa
->spa_vdev_removal
!= NULL
)
3647 txg_wait_synced(spa_get_dsl(spa
), 0);
3649 mutex_exit(&ztest_vdev_lock
);
3654 * The pool needs to be scrubbed after completing device removal.
3655 * Failure to do so may result in checksum errors due to the
3656 * strategy employed by ztest_fault_inject() when selecting which
3657 * offset are redundant and can be damaged.
3659 error
= spa_scan(spa
, POOL_SCAN_SCRUB
);
3661 while (dsl_scan_scrubbing(spa_get_dsl(spa
)))
3662 txg_wait_synced(spa_get_dsl(spa
), 0);
3665 mutex_enter(&ztest_vdev_lock
);
3666 ztest_device_removal_active
= B_FALSE
;
3667 mutex_exit(&ztest_vdev_lock
);
3671 * Callback function which expands the physical size of the vdev.
3674 grow_vdev(vdev_t
*vd
, void *arg
)
3676 ASSERTV(spa_t
*spa
= vd
->vdev_spa
);
3677 size_t *newsize
= arg
;
3681 ASSERT(spa_config_held(spa
, SCL_STATE
, RW_READER
) == SCL_STATE
);
3682 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
3684 if ((fd
= open(vd
->vdev_path
, O_RDWR
)) == -1)
3687 fsize
= lseek(fd
, 0, SEEK_END
);
3688 VERIFY(ftruncate(fd
, *newsize
) == 0);
3690 if (ztest_opts
.zo_verbose
>= 6) {
3691 (void) printf("%s grew from %lu to %lu bytes\n",
3692 vd
->vdev_path
, (ulong_t
)fsize
, (ulong_t
)*newsize
);
3699 * Callback function which expands a given vdev by calling vdev_online().
3703 online_vdev(vdev_t
*vd
, void *arg
)
3705 spa_t
*spa
= vd
->vdev_spa
;
3706 vdev_t
*tvd
= vd
->vdev_top
;
3707 uint64_t guid
= vd
->vdev_guid
;
3708 uint64_t generation
= spa
->spa_config_generation
+ 1;
3709 vdev_state_t newstate
= VDEV_STATE_UNKNOWN
;
3712 ASSERT(spa_config_held(spa
, SCL_STATE
, RW_READER
) == SCL_STATE
);
3713 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
3715 /* Calling vdev_online will initialize the new metaslabs */
3716 spa_config_exit(spa
, SCL_STATE
, spa
);
3717 error
= vdev_online(spa
, guid
, ZFS_ONLINE_EXPAND
, &newstate
);
3718 spa_config_enter(spa
, SCL_STATE
, spa
, RW_READER
);
3721 * If vdev_online returned an error or the underlying vdev_open
3722 * failed then we abort the expand. The only way to know that
3723 * vdev_open fails is by checking the returned newstate.
3725 if (error
|| newstate
!= VDEV_STATE_HEALTHY
) {
3726 if (ztest_opts
.zo_verbose
>= 5) {
3727 (void) printf("Unable to expand vdev, state %llu, "
3728 "error %d\n", (u_longlong_t
)newstate
, error
);
3732 ASSERT3U(newstate
, ==, VDEV_STATE_HEALTHY
);
3735 * Since we dropped the lock we need to ensure that we're
3736 * still talking to the original vdev. It's possible this
3737 * vdev may have been detached/replaced while we were
3738 * trying to online it.
3740 if (generation
!= spa
->spa_config_generation
) {
3741 if (ztest_opts
.zo_verbose
>= 5) {
3742 (void) printf("vdev configuration has changed, "
3743 "guid %llu, state %llu, expected gen %llu, "
3746 (u_longlong_t
)tvd
->vdev_state
,
3747 (u_longlong_t
)generation
,
3748 (u_longlong_t
)spa
->spa_config_generation
);
3756 * Traverse the vdev tree calling the supplied function.
3757 * We continue to walk the tree until we either have walked all
3758 * children or we receive a non-NULL return from the callback.
3759 * If a NULL callback is passed, then we just return back the first
3760 * leaf vdev we encounter.
3763 vdev_walk_tree(vdev_t
*vd
, vdev_t
*(*func
)(vdev_t
*, void *), void *arg
)
3767 if (vd
->vdev_ops
->vdev_op_leaf
) {
3771 return (func(vd
, arg
));
3774 for (c
= 0; c
< vd
->vdev_children
; c
++) {
3775 vdev_t
*cvd
= vd
->vdev_child
[c
];
3776 if ((cvd
= vdev_walk_tree(cvd
, func
, arg
)) != NULL
)
3783 * Verify that dynamic LUN growth works as expected.
3787 ztest_vdev_LUN_growth(ztest_ds_t
*zd
, uint64_t id
)
3789 spa_t
*spa
= ztest_spa
;
3791 metaslab_class_t
*mc
;
3792 metaslab_group_t
*mg
;
3793 size_t psize
, newsize
;
3795 uint64_t old_class_space
, new_class_space
, old_ms_count
, new_ms_count
;
3797 mutex_enter(&ztest_checkpoint_lock
);
3798 mutex_enter(&ztest_vdev_lock
);
3799 spa_config_enter(spa
, SCL_STATE
, spa
, RW_READER
);
3802 * If there is a vdev removal in progress, it could complete while
3803 * we are running, in which case we would not be able to verify
3804 * that the metaslab_class space increased (because it decreases
3805 * when the device removal completes).
3807 if (ztest_device_removal_active
) {
3808 spa_config_exit(spa
, SCL_STATE
, spa
);
3809 mutex_exit(&ztest_vdev_lock
);
3810 mutex_exit(&ztest_checkpoint_lock
);
3814 top
= ztest_random_vdev_top(spa
, B_TRUE
);
3816 tvd
= spa
->spa_root_vdev
->vdev_child
[top
];
3819 old_ms_count
= tvd
->vdev_ms_count
;
3820 old_class_space
= metaslab_class_get_space(mc
);
3823 * Determine the size of the first leaf vdev associated with
3824 * our top-level device.
3826 vd
= vdev_walk_tree(tvd
, NULL
, NULL
);
3827 ASSERT3P(vd
, !=, NULL
);
3828 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
3830 psize
= vd
->vdev_psize
;
3833 * We only try to expand the vdev if it's healthy, less than 4x its
3834 * original size, and it has a valid psize.
3836 if (tvd
->vdev_state
!= VDEV_STATE_HEALTHY
||
3837 psize
== 0 || psize
>= 4 * ztest_opts
.zo_vdev_size
) {
3838 spa_config_exit(spa
, SCL_STATE
, spa
);
3839 mutex_exit(&ztest_vdev_lock
);
3840 mutex_exit(&ztest_checkpoint_lock
);
3844 newsize
= psize
+ MAX(psize
/ 8, SPA_MAXBLOCKSIZE
);
3845 ASSERT3U(newsize
, >, psize
);
3847 if (ztest_opts
.zo_verbose
>= 6) {
3848 (void) printf("Expanding LUN %s from %lu to %lu\n",
3849 vd
->vdev_path
, (ulong_t
)psize
, (ulong_t
)newsize
);
3853 * Growing the vdev is a two step process:
3854 * 1). expand the physical size (i.e. relabel)
3855 * 2). online the vdev to create the new metaslabs
3857 if (vdev_walk_tree(tvd
, grow_vdev
, &newsize
) != NULL
||
3858 vdev_walk_tree(tvd
, online_vdev
, NULL
) != NULL
||
3859 tvd
->vdev_state
!= VDEV_STATE_HEALTHY
) {
3860 if (ztest_opts
.zo_verbose
>= 5) {
3861 (void) printf("Could not expand LUN because "
3862 "the vdev configuration changed.\n");
3864 spa_config_exit(spa
, SCL_STATE
, spa
);
3865 mutex_exit(&ztest_vdev_lock
);
3866 mutex_exit(&ztest_checkpoint_lock
);
3870 spa_config_exit(spa
, SCL_STATE
, spa
);
3873 * Expanding the LUN will update the config asynchronously,
3874 * thus we must wait for the async thread to complete any
3875 * pending tasks before proceeding.
3879 mutex_enter(&spa
->spa_async_lock
);
3880 done
= (spa
->spa_async_thread
== NULL
&& !spa
->spa_async_tasks
);
3881 mutex_exit(&spa
->spa_async_lock
);
3884 txg_wait_synced(spa_get_dsl(spa
), 0);
3885 (void) poll(NULL
, 0, 100);
3888 spa_config_enter(spa
, SCL_STATE
, spa
, RW_READER
);
3890 tvd
= spa
->spa_root_vdev
->vdev_child
[top
];
3891 new_ms_count
= tvd
->vdev_ms_count
;
3892 new_class_space
= metaslab_class_get_space(mc
);
3894 if (tvd
->vdev_mg
!= mg
|| mg
->mg_class
!= mc
) {
3895 if (ztest_opts
.zo_verbose
>= 5) {
3896 (void) printf("Could not verify LUN expansion due to "
3897 "intervening vdev offline or remove.\n");
3899 spa_config_exit(spa
, SCL_STATE
, spa
);
3900 mutex_exit(&ztest_vdev_lock
);
3901 mutex_exit(&ztest_checkpoint_lock
);
3906 * Make sure we were able to grow the vdev.
3908 if (new_ms_count
<= old_ms_count
) {
3909 fatal(0, "LUN expansion failed: ms_count %llu < %llu\n",
3910 old_ms_count
, new_ms_count
);
3914 * Make sure we were able to grow the pool.
3916 if (new_class_space
<= old_class_space
) {
3917 fatal(0, "LUN expansion failed: class_space %llu < %llu\n",
3918 old_class_space
, new_class_space
);
3921 if (ztest_opts
.zo_verbose
>= 5) {
3922 char oldnumbuf
[NN_NUMBUF_SZ
], newnumbuf
[NN_NUMBUF_SZ
];
3924 nicenum(old_class_space
, oldnumbuf
, sizeof (oldnumbuf
));
3925 nicenum(new_class_space
, newnumbuf
, sizeof (newnumbuf
));
3926 (void) printf("%s grew from %s to %s\n",
3927 spa
->spa_name
, oldnumbuf
, newnumbuf
);
3930 spa_config_exit(spa
, SCL_STATE
, spa
);
3931 mutex_exit(&ztest_vdev_lock
);
3932 mutex_exit(&ztest_checkpoint_lock
);
3936 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
3940 ztest_objset_create_cb(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
)
3943 * Create the objects common to all ztest datasets.
3945 VERIFY(zap_create_claim(os
, ZTEST_DIROBJ
,
3946 DMU_OT_ZAP_OTHER
, DMU_OT_NONE
, 0, tx
) == 0);
3950 ztest_dataset_create(char *dsname
)
3954 dsl_crypto_params_t
*dcp
= NULL
;
3957 * 50% of the time, we create encrypted datasets
3958 * using a random cipher suite and a hard-coded
3961 rand
= ztest_random(2);
3963 nvlist_t
*crypto_args
= fnvlist_alloc();
3964 nvlist_t
*props
= fnvlist_alloc();
3966 /* slight bias towards the default cipher suite */
3967 rand
= ztest_random(ZIO_CRYPT_FUNCTIONS
);
3968 if (rand
< ZIO_CRYPT_AES_128_CCM
)
3969 rand
= ZIO_CRYPT_ON
;
3971 fnvlist_add_uint64(props
,
3972 zfs_prop_to_name(ZFS_PROP_ENCRYPTION
), rand
);
3973 fnvlist_add_uint8_array(crypto_args
, "wkeydata",
3974 (uint8_t *)ztest_wkeydata
, WRAPPING_KEY_LEN
);
3977 * These parameters aren't really used by the kernel. They
3978 * are simply stored so that userspace knows how to load
3981 fnvlist_add_uint64(props
,
3982 zfs_prop_to_name(ZFS_PROP_KEYFORMAT
), ZFS_KEYFORMAT_RAW
);
3983 fnvlist_add_string(props
,
3984 zfs_prop_to_name(ZFS_PROP_KEYLOCATION
), "prompt");
3985 fnvlist_add_uint64(props
,
3986 zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT
), 0ULL);
3987 fnvlist_add_uint64(props
,
3988 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS
), 0ULL);
3990 VERIFY0(dsl_crypto_params_create_nvlist(DCP_CMD_NONE
, props
,
3991 crypto_args
, &dcp
));
3994 * Cycle through all available encryption implementations
3995 * to verify interoperability.
3997 VERIFY0(gcm_impl_set("cycle"));
3998 VERIFY0(aes_impl_set("cycle"));
4000 fnvlist_free(crypto_args
);
4001 fnvlist_free(props
);
4004 err
= dmu_objset_create(dsname
, DMU_OST_OTHER
, 0, dcp
,
4005 ztest_objset_create_cb
, NULL
);
4006 dsl_crypto_params_free(dcp
, !!err
);
4008 rand
= ztest_random(100);
4009 if (err
|| rand
< 80)
4012 if (ztest_opts
.zo_verbose
>= 5)
4013 (void) printf("Setting dataset %s to sync always\n", dsname
);
4014 return (ztest_dsl_prop_set_uint64(dsname
, ZFS_PROP_SYNC
,
4015 ZFS_SYNC_ALWAYS
, B_FALSE
));
4020 ztest_objset_destroy_cb(const char *name
, void *arg
)
4023 dmu_object_info_t doi
;
4027 * Verify that the dataset contains a directory object.
4029 VERIFY0(ztest_dmu_objset_own(name
, DMU_OST_OTHER
, B_TRUE
,
4030 B_TRUE
, FTAG
, &os
));
4031 error
= dmu_object_info(os
, ZTEST_DIROBJ
, &doi
);
4032 if (error
!= ENOENT
) {
4033 /* We could have crashed in the middle of destroying it */
4035 ASSERT3U(doi
.doi_type
, ==, DMU_OT_ZAP_OTHER
);
4036 ASSERT3S(doi
.doi_physical_blocks_512
, >=, 0);
4038 dmu_objset_disown(os
, B_TRUE
, FTAG
);
4041 * Destroy the dataset.
4043 if (strchr(name
, '@') != NULL
) {
4044 VERIFY0(dsl_destroy_snapshot(name
, B_TRUE
));
4046 error
= dsl_destroy_head(name
);
4047 /* There could be a hold on this dataset */
4055 ztest_snapshot_create(char *osname
, uint64_t id
)
4057 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
4060 (void) snprintf(snapname
, sizeof (snapname
), "%llu", (u_longlong_t
)id
);
4062 error
= dmu_objset_snapshot_one(osname
, snapname
);
4063 if (error
== ENOSPC
) {
4064 ztest_record_enospc(FTAG
);
4067 if (error
!= 0 && error
!= EEXIST
) {
4068 fatal(0, "ztest_snapshot_create(%s@%s) = %d", osname
,
4075 ztest_snapshot_destroy(char *osname
, uint64_t id
)
4077 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
4080 (void) snprintf(snapname
, sizeof (snapname
), "%s@%llu", osname
,
4083 error
= dsl_destroy_snapshot(snapname
, B_FALSE
);
4084 if (error
!= 0 && error
!= ENOENT
)
4085 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname
, error
);
4091 ztest_dmu_objset_create_destroy(ztest_ds_t
*zd
, uint64_t id
)
4097 char name
[ZFS_MAX_DATASET_NAME_LEN
];
4101 zdtmp
= umem_alloc(sizeof (ztest_ds_t
), UMEM_NOFAIL
);
4103 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
4105 (void) snprintf(name
, sizeof (name
), "%s/temp_%llu",
4106 ztest_opts
.zo_pool
, (u_longlong_t
)id
);
4109 * If this dataset exists from a previous run, process its replay log
4110 * half of the time. If we don't replay it, then dsl_destroy_head()
4111 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
4113 if (ztest_random(2) == 0 &&
4114 ztest_dmu_objset_own(name
, DMU_OST_OTHER
, B_FALSE
,
4115 B_TRUE
, FTAG
, &os
) == 0) {
4116 ztest_zd_init(zdtmp
, NULL
, os
);
4117 zil_replay(os
, zdtmp
, ztest_replay_vector
);
4118 ztest_zd_fini(zdtmp
);
4119 txg_wait_synced(dmu_objset_pool(os
), 0);
4120 dmu_objset_disown(os
, B_TRUE
, FTAG
);
4124 * There may be an old instance of the dataset we're about to
4125 * create lying around from a previous run. If so, destroy it
4126 * and all of its snapshots.
4128 (void) dmu_objset_find(name
, ztest_objset_destroy_cb
, NULL
,
4129 DS_FIND_CHILDREN
| DS_FIND_SNAPSHOTS
);
4132 * Verify that the destroyed dataset is no longer in the namespace.
4134 VERIFY3U(ENOENT
, ==, ztest_dmu_objset_own(name
, DMU_OST_OTHER
, B_TRUE
,
4135 B_TRUE
, FTAG
, &os
));
4138 * Verify that we can create a new dataset.
4140 error
= ztest_dataset_create(name
);
4142 if (error
== ENOSPC
) {
4143 ztest_record_enospc(FTAG
);
4146 fatal(0, "dmu_objset_create(%s) = %d", name
, error
);
4149 VERIFY0(ztest_dmu_objset_own(name
, DMU_OST_OTHER
, B_FALSE
, B_TRUE
,
4152 ztest_zd_init(zdtmp
, NULL
, os
);
4155 * Open the intent log for it.
4157 zilog
= zil_open(os
, ztest_get_data
);
4160 * Put some objects in there, do a little I/O to them,
4161 * and randomly take a couple of snapshots along the way.
4163 iters
= ztest_random(5);
4164 for (i
= 0; i
< iters
; i
++) {
4165 ztest_dmu_object_alloc_free(zdtmp
, id
);
4166 if (ztest_random(iters
) == 0)
4167 (void) ztest_snapshot_create(name
, i
);
4171 * Verify that we cannot create an existing dataset.
4173 VERIFY3U(EEXIST
, ==,
4174 dmu_objset_create(name
, DMU_OST_OTHER
, 0, NULL
, NULL
, NULL
));
4177 * Verify that we can hold an objset that is also owned.
4179 VERIFY3U(0, ==, dmu_objset_hold(name
, FTAG
, &os2
));
4180 dmu_objset_rele(os2
, FTAG
);
4183 * Verify that we cannot own an objset that is already owned.
4185 VERIFY3U(EBUSY
, ==, ztest_dmu_objset_own(name
, DMU_OST_OTHER
,
4186 B_FALSE
, B_TRUE
, FTAG
, &os2
));
4189 txg_wait_synced(spa_get_dsl(os
->os_spa
), 0);
4190 dmu_objset_disown(os
, B_TRUE
, FTAG
);
4191 ztest_zd_fini(zdtmp
);
4193 (void) pthread_rwlock_unlock(&ztest_name_lock
);
4195 umem_free(zdtmp
, sizeof (ztest_ds_t
));
4199 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
4202 ztest_dmu_snapshot_create_destroy(ztest_ds_t
*zd
, uint64_t id
)
4204 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
4205 (void) ztest_snapshot_destroy(zd
->zd_name
, id
);
4206 (void) ztest_snapshot_create(zd
->zd_name
, id
);
4207 (void) pthread_rwlock_unlock(&ztest_name_lock
);
4211 * Cleanup non-standard snapshots and clones.
4214 ztest_dsl_dataset_cleanup(char *osname
, uint64_t id
)
4223 snap1name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4224 clone1name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4225 snap2name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4226 clone2name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4227 snap3name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4229 (void) snprintf(snap1name
, ZFS_MAX_DATASET_NAME_LEN
,
4230 "%s@s1_%llu", osname
, (u_longlong_t
)id
);
4231 (void) snprintf(clone1name
, ZFS_MAX_DATASET_NAME_LEN
,
4232 "%s/c1_%llu", osname
, (u_longlong_t
)id
);
4233 (void) snprintf(snap2name
, ZFS_MAX_DATASET_NAME_LEN
,
4234 "%s@s2_%llu", clone1name
, (u_longlong_t
)id
);
4235 (void) snprintf(clone2name
, ZFS_MAX_DATASET_NAME_LEN
,
4236 "%s/c2_%llu", osname
, (u_longlong_t
)id
);
4237 (void) snprintf(snap3name
, ZFS_MAX_DATASET_NAME_LEN
,
4238 "%s@s3_%llu", clone1name
, (u_longlong_t
)id
);
4240 error
= dsl_destroy_head(clone2name
);
4241 if (error
&& error
!= ENOENT
)
4242 fatal(0, "dsl_destroy_head(%s) = %d", clone2name
, error
);
4243 error
= dsl_destroy_snapshot(snap3name
, B_FALSE
);
4244 if (error
&& error
!= ENOENT
)
4245 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap3name
, error
);
4246 error
= dsl_destroy_snapshot(snap2name
, B_FALSE
);
4247 if (error
&& error
!= ENOENT
)
4248 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap2name
, error
);
4249 error
= dsl_destroy_head(clone1name
);
4250 if (error
&& error
!= ENOENT
)
4251 fatal(0, "dsl_destroy_head(%s) = %d", clone1name
, error
);
4252 error
= dsl_destroy_snapshot(snap1name
, B_FALSE
);
4253 if (error
&& error
!= ENOENT
)
4254 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap1name
, error
);
4256 umem_free(snap1name
, ZFS_MAX_DATASET_NAME_LEN
);
4257 umem_free(clone1name
, ZFS_MAX_DATASET_NAME_LEN
);
4258 umem_free(snap2name
, ZFS_MAX_DATASET_NAME_LEN
);
4259 umem_free(clone2name
, ZFS_MAX_DATASET_NAME_LEN
);
4260 umem_free(snap3name
, ZFS_MAX_DATASET_NAME_LEN
);
4264 * Verify dsl_dataset_promote handles EBUSY
4267 ztest_dsl_dataset_promote_busy(ztest_ds_t
*zd
, uint64_t id
)
4275 char *osname
= zd
->zd_name
;
4278 snap1name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4279 clone1name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4280 snap2name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4281 clone2name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4282 snap3name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4284 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
4286 ztest_dsl_dataset_cleanup(osname
, id
);
4288 (void) snprintf(snap1name
, ZFS_MAX_DATASET_NAME_LEN
,
4289 "%s@s1_%llu", osname
, (u_longlong_t
)id
);
4290 (void) snprintf(clone1name
, ZFS_MAX_DATASET_NAME_LEN
,
4291 "%s/c1_%llu", osname
, (u_longlong_t
)id
);
4292 (void) snprintf(snap2name
, ZFS_MAX_DATASET_NAME_LEN
,
4293 "%s@s2_%llu", clone1name
, (u_longlong_t
)id
);
4294 (void) snprintf(clone2name
, ZFS_MAX_DATASET_NAME_LEN
,
4295 "%s/c2_%llu", osname
, (u_longlong_t
)id
);
4296 (void) snprintf(snap3name
, ZFS_MAX_DATASET_NAME_LEN
,
4297 "%s@s3_%llu", clone1name
, (u_longlong_t
)id
);
4299 error
= dmu_objset_snapshot_one(osname
, strchr(snap1name
, '@') + 1);
4300 if (error
&& error
!= EEXIST
) {
4301 if (error
== ENOSPC
) {
4302 ztest_record_enospc(FTAG
);
4305 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name
, error
);
4308 error
= dmu_objset_clone(clone1name
, snap1name
);
4310 if (error
== ENOSPC
) {
4311 ztest_record_enospc(FTAG
);
4314 fatal(0, "dmu_objset_create(%s) = %d", clone1name
, error
);
4317 error
= dmu_objset_snapshot_one(clone1name
, strchr(snap2name
, '@') + 1);
4318 if (error
&& error
!= EEXIST
) {
4319 if (error
== ENOSPC
) {
4320 ztest_record_enospc(FTAG
);
4323 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name
, error
);
4326 error
= dmu_objset_snapshot_one(clone1name
, strchr(snap3name
, '@') + 1);
4327 if (error
&& error
!= EEXIST
) {
4328 if (error
== ENOSPC
) {
4329 ztest_record_enospc(FTAG
);
4332 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name
, error
);
4335 error
= dmu_objset_clone(clone2name
, snap3name
);
4337 if (error
== ENOSPC
) {
4338 ztest_record_enospc(FTAG
);
4341 fatal(0, "dmu_objset_create(%s) = %d", clone2name
, error
);
4344 error
= ztest_dmu_objset_own(snap2name
, DMU_OST_ANY
, B_TRUE
, B_TRUE
,
4347 fatal(0, "dmu_objset_own(%s) = %d", snap2name
, error
);
4348 error
= dsl_dataset_promote(clone2name
, NULL
);
4349 if (error
== ENOSPC
) {
4350 dmu_objset_disown(os
, B_TRUE
, FTAG
);
4351 ztest_record_enospc(FTAG
);
4355 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name
,
4357 dmu_objset_disown(os
, B_TRUE
, FTAG
);
4360 ztest_dsl_dataset_cleanup(osname
, id
);
4362 (void) pthread_rwlock_unlock(&ztest_name_lock
);
4364 umem_free(snap1name
, ZFS_MAX_DATASET_NAME_LEN
);
4365 umem_free(clone1name
, ZFS_MAX_DATASET_NAME_LEN
);
4366 umem_free(snap2name
, ZFS_MAX_DATASET_NAME_LEN
);
4367 umem_free(clone2name
, ZFS_MAX_DATASET_NAME_LEN
);
4368 umem_free(snap3name
, ZFS_MAX_DATASET_NAME_LEN
);
4371 #undef OD_ARRAY_SIZE
4372 #define OD_ARRAY_SIZE 4
4375 * Verify that dmu_object_{alloc,free} work as expected.
4378 ztest_dmu_object_alloc_free(ztest_ds_t
*zd
, uint64_t id
)
4385 size
= sizeof (ztest_od_t
) * OD_ARRAY_SIZE
;
4386 od
= umem_alloc(size
, UMEM_NOFAIL
);
4387 batchsize
= OD_ARRAY_SIZE
;
4389 for (b
= 0; b
< batchsize
; b
++)
4390 ztest_od_init(od
+ b
, id
, FTAG
, b
, DMU_OT_UINT64_OTHER
,
4394 * Destroy the previous batch of objects, create a new batch,
4395 * and do some I/O on the new objects.
4397 if (ztest_object_init(zd
, od
, size
, B_TRUE
) != 0)
4400 while (ztest_random(4 * batchsize
) != 0)
4401 ztest_io(zd
, od
[ztest_random(batchsize
)].od_object
,
4402 ztest_random(ZTEST_RANGE_LOCKS
) << SPA_MAXBLOCKSHIFT
);
4404 umem_free(od
, size
);
4408 * Rewind the global allocator to verify object allocation backfilling.
4411 ztest_dmu_object_next_chunk(ztest_ds_t
*zd
, uint64_t id
)
4413 objset_t
*os
= zd
->zd_os
;
4414 int dnodes_per_chunk
= 1 << dmu_object_alloc_chunk_shift
;
4418 * Rewind the global allocator randomly back to a lower object number
4419 * to force backfilling and reclamation of recently freed dnodes.
4421 mutex_enter(&os
->os_obj_lock
);
4422 object
= ztest_random(os
->os_obj_next_chunk
);
4423 os
->os_obj_next_chunk
= P2ALIGN(object
, dnodes_per_chunk
);
4424 mutex_exit(&os
->os_obj_lock
);
4427 #undef OD_ARRAY_SIZE
4428 #define OD_ARRAY_SIZE 2
4431 * Verify that dmu_{read,write} work as expected.
4434 ztest_dmu_read_write(ztest_ds_t
*zd
, uint64_t id
)
4439 objset_t
*os
= zd
->zd_os
;
4440 size
= sizeof (ztest_od_t
) * OD_ARRAY_SIZE
;
4441 od
= umem_alloc(size
, UMEM_NOFAIL
);
4443 int i
, freeit
, error
;
4445 bufwad_t
*packbuf
, *bigbuf
, *pack
, *bigH
, *bigT
;
4446 uint64_t packobj
, packoff
, packsize
, bigobj
, bigoff
, bigsize
;
4447 uint64_t chunksize
= (1000 + ztest_random(1000)) * sizeof (uint64_t);
4448 uint64_t regions
= 997;
4449 uint64_t stride
= 123456789ULL;
4450 uint64_t width
= 40;
4451 int free_percent
= 5;
4454 * This test uses two objects, packobj and bigobj, that are always
4455 * updated together (i.e. in the same tx) so that their contents are
4456 * in sync and can be compared. Their contents relate to each other
4457 * in a simple way: packobj is a dense array of 'bufwad' structures,
4458 * while bigobj is a sparse array of the same bufwads. Specifically,
4459 * for any index n, there are three bufwads that should be identical:
4461 * packobj, at offset n * sizeof (bufwad_t)
4462 * bigobj, at the head of the nth chunk
4463 * bigobj, at the tail of the nth chunk
4465 * The chunk size is arbitrary. It doesn't have to be a power of two,
4466 * and it doesn't have any relation to the object blocksize.
4467 * The only requirement is that it can hold at least two bufwads.
4469 * Normally, we write the bufwad to each of these locations.
4470 * However, free_percent of the time we instead write zeroes to
4471 * packobj and perform a dmu_free_range() on bigobj. By comparing
4472 * bigobj to packobj, we can verify that the DMU is correctly
4473 * tracking which parts of an object are allocated and free,
4474 * and that the contents of the allocated blocks are correct.
4478 * Read the directory info. If it's the first time, set things up.
4480 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, 0, 0, chunksize
);
4481 ztest_od_init(od
+ 1, id
, FTAG
, 1, DMU_OT_UINT64_OTHER
, 0, 0,
4484 if (ztest_object_init(zd
, od
, size
, B_FALSE
) != 0) {
4485 umem_free(od
, size
);
4489 bigobj
= od
[0].od_object
;
4490 packobj
= od
[1].od_object
;
4491 chunksize
= od
[0].od_gen
;
4492 ASSERT(chunksize
== od
[1].od_gen
);
4495 * Prefetch a random chunk of the big object.
4496 * Our aim here is to get some async reads in flight
4497 * for blocks that we may free below; the DMU should
4498 * handle this race correctly.
4500 n
= ztest_random(regions
) * stride
+ ztest_random(width
);
4501 s
= 1 + ztest_random(2 * width
- 1);
4502 dmu_prefetch(os
, bigobj
, 0, n
* chunksize
, s
* chunksize
,
4503 ZIO_PRIORITY_SYNC_READ
);
4506 * Pick a random index and compute the offsets into packobj and bigobj.
4508 n
= ztest_random(regions
) * stride
+ ztest_random(width
);
4509 s
= 1 + ztest_random(width
- 1);
4511 packoff
= n
* sizeof (bufwad_t
);
4512 packsize
= s
* sizeof (bufwad_t
);
4514 bigoff
= n
* chunksize
;
4515 bigsize
= s
* chunksize
;
4517 packbuf
= umem_alloc(packsize
, UMEM_NOFAIL
);
4518 bigbuf
= umem_alloc(bigsize
, UMEM_NOFAIL
);
4521 * free_percent of the time, free a range of bigobj rather than
4524 freeit
= (ztest_random(100) < free_percent
);
4527 * Read the current contents of our objects.
4529 error
= dmu_read(os
, packobj
, packoff
, packsize
, packbuf
,
4532 error
= dmu_read(os
, bigobj
, bigoff
, bigsize
, bigbuf
,
4537 * Get a tx for the mods to both packobj and bigobj.
4539 tx
= dmu_tx_create(os
);
4541 dmu_tx_hold_write(tx
, packobj
, packoff
, packsize
);
4544 dmu_tx_hold_free(tx
, bigobj
, bigoff
, bigsize
);
4546 dmu_tx_hold_write(tx
, bigobj
, bigoff
, bigsize
);
4548 /* This accounts for setting the checksum/compression. */
4549 dmu_tx_hold_bonus(tx
, bigobj
);
4551 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
4553 umem_free(packbuf
, packsize
);
4554 umem_free(bigbuf
, bigsize
);
4555 umem_free(od
, size
);
4559 enum zio_checksum cksum
;
4561 cksum
= (enum zio_checksum
)
4562 ztest_random_dsl_prop(ZFS_PROP_CHECKSUM
);
4563 } while (cksum
>= ZIO_CHECKSUM_LEGACY_FUNCTIONS
);
4564 dmu_object_set_checksum(os
, bigobj
, cksum
, tx
);
4566 enum zio_compress comp
;
4568 comp
= (enum zio_compress
)
4569 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION
);
4570 } while (comp
>= ZIO_COMPRESS_LEGACY_FUNCTIONS
);
4571 dmu_object_set_compress(os
, bigobj
, comp
, tx
);
4574 * For each index from n to n + s, verify that the existing bufwad
4575 * in packobj matches the bufwads at the head and tail of the
4576 * corresponding chunk in bigobj. Then update all three bufwads
4577 * with the new values we want to write out.
4579 for (i
= 0; i
< s
; i
++) {
4581 pack
= (bufwad_t
*)((char *)packbuf
+ i
* sizeof (bufwad_t
));
4583 bigH
= (bufwad_t
*)((char *)bigbuf
+ i
* chunksize
);
4585 bigT
= (bufwad_t
*)((char *)bigH
+ chunksize
) - 1;
4587 ASSERT((uintptr_t)bigH
- (uintptr_t)bigbuf
< bigsize
);
4588 ASSERT((uintptr_t)bigT
- (uintptr_t)bigbuf
< bigsize
);
4590 if (pack
->bw_txg
> txg
)
4591 fatal(0, "future leak: got %llx, open txg is %llx",
4594 if (pack
->bw_data
!= 0 && pack
->bw_index
!= n
+ i
)
4595 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4596 pack
->bw_index
, n
, i
);
4598 if (bcmp(pack
, bigH
, sizeof (bufwad_t
)) != 0)
4599 fatal(0, "pack/bigH mismatch in %p/%p", pack
, bigH
);
4601 if (bcmp(pack
, bigT
, sizeof (bufwad_t
)) != 0)
4602 fatal(0, "pack/bigT mismatch in %p/%p", pack
, bigT
);
4605 bzero(pack
, sizeof (bufwad_t
));
4607 pack
->bw_index
= n
+ i
;
4609 pack
->bw_data
= 1 + ztest_random(-2ULL);
4616 * We've verified all the old bufwads, and made new ones.
4617 * Now write them out.
4619 dmu_write(os
, packobj
, packoff
, packsize
, packbuf
, tx
);
4622 if (ztest_opts
.zo_verbose
>= 7) {
4623 (void) printf("freeing offset %llx size %llx"
4625 (u_longlong_t
)bigoff
,
4626 (u_longlong_t
)bigsize
,
4629 VERIFY(0 == dmu_free_range(os
, bigobj
, bigoff
, bigsize
, tx
));
4631 if (ztest_opts
.zo_verbose
>= 7) {
4632 (void) printf("writing offset %llx size %llx"
4634 (u_longlong_t
)bigoff
,
4635 (u_longlong_t
)bigsize
,
4638 dmu_write(os
, bigobj
, bigoff
, bigsize
, bigbuf
, tx
);
4644 * Sanity check the stuff we just wrote.
4647 void *packcheck
= umem_alloc(packsize
, UMEM_NOFAIL
);
4648 void *bigcheck
= umem_alloc(bigsize
, UMEM_NOFAIL
);
4650 VERIFY(0 == dmu_read(os
, packobj
, packoff
,
4651 packsize
, packcheck
, DMU_READ_PREFETCH
));
4652 VERIFY(0 == dmu_read(os
, bigobj
, bigoff
,
4653 bigsize
, bigcheck
, DMU_READ_PREFETCH
));
4655 ASSERT(bcmp(packbuf
, packcheck
, packsize
) == 0);
4656 ASSERT(bcmp(bigbuf
, bigcheck
, bigsize
) == 0);
4658 umem_free(packcheck
, packsize
);
4659 umem_free(bigcheck
, bigsize
);
4662 umem_free(packbuf
, packsize
);
4663 umem_free(bigbuf
, bigsize
);
4664 umem_free(od
, size
);
4668 compare_and_update_pbbufs(uint64_t s
, bufwad_t
*packbuf
, bufwad_t
*bigbuf
,
4669 uint64_t bigsize
, uint64_t n
, uint64_t chunksize
, uint64_t txg
)
4677 * For each index from n to n + s, verify that the existing bufwad
4678 * in packobj matches the bufwads at the head and tail of the
4679 * corresponding chunk in bigobj. Then update all three bufwads
4680 * with the new values we want to write out.
4682 for (i
= 0; i
< s
; i
++) {
4684 pack
= (bufwad_t
*)((char *)packbuf
+ i
* sizeof (bufwad_t
));
4686 bigH
= (bufwad_t
*)((char *)bigbuf
+ i
* chunksize
);
4688 bigT
= (bufwad_t
*)((char *)bigH
+ chunksize
) - 1;
4690 ASSERT((uintptr_t)bigH
- (uintptr_t)bigbuf
< bigsize
);
4691 ASSERT((uintptr_t)bigT
- (uintptr_t)bigbuf
< bigsize
);
4693 if (pack
->bw_txg
> txg
)
4694 fatal(0, "future leak: got %llx, open txg is %llx",
4697 if (pack
->bw_data
!= 0 && pack
->bw_index
!= n
+ i
)
4698 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4699 pack
->bw_index
, n
, i
);
4701 if (bcmp(pack
, bigH
, sizeof (bufwad_t
)) != 0)
4702 fatal(0, "pack/bigH mismatch in %p/%p", pack
, bigH
);
4704 if (bcmp(pack
, bigT
, sizeof (bufwad_t
)) != 0)
4705 fatal(0, "pack/bigT mismatch in %p/%p", pack
, bigT
);
4707 pack
->bw_index
= n
+ i
;
4709 pack
->bw_data
= 1 + ztest_random(-2ULL);
4716 #undef OD_ARRAY_SIZE
4717 #define OD_ARRAY_SIZE 2
4720 ztest_dmu_read_write_zcopy(ztest_ds_t
*zd
, uint64_t id
)
4722 objset_t
*os
= zd
->zd_os
;
4729 bufwad_t
*packbuf
, *bigbuf
;
4730 uint64_t packobj
, packoff
, packsize
, bigobj
, bigoff
, bigsize
;
4731 uint64_t blocksize
= ztest_random_blocksize();
4732 uint64_t chunksize
= blocksize
;
4733 uint64_t regions
= 997;
4734 uint64_t stride
= 123456789ULL;
4736 dmu_buf_t
*bonus_db
;
4737 arc_buf_t
**bigbuf_arcbufs
;
4738 dmu_object_info_t doi
;
4740 size
= sizeof (ztest_od_t
) * OD_ARRAY_SIZE
;
4741 od
= umem_alloc(size
, UMEM_NOFAIL
);
4744 * This test uses two objects, packobj and bigobj, that are always
4745 * updated together (i.e. in the same tx) so that their contents are
4746 * in sync and can be compared. Their contents relate to each other
4747 * in a simple way: packobj is a dense array of 'bufwad' structures,
4748 * while bigobj is a sparse array of the same bufwads. Specifically,
4749 * for any index n, there are three bufwads that should be identical:
4751 * packobj, at offset n * sizeof (bufwad_t)
4752 * bigobj, at the head of the nth chunk
4753 * bigobj, at the tail of the nth chunk
4755 * The chunk size is set equal to bigobj block size so that
4756 * dmu_assign_arcbuf_by_dbuf() can be tested for object updates.
4760 * Read the directory info. If it's the first time, set things up.
4762 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, blocksize
, 0, 0);
4763 ztest_od_init(od
+ 1, id
, FTAG
, 1, DMU_OT_UINT64_OTHER
, 0, 0,
4767 if (ztest_object_init(zd
, od
, size
, B_FALSE
) != 0) {
4768 umem_free(od
, size
);
4772 bigobj
= od
[0].od_object
;
4773 packobj
= od
[1].od_object
;
4774 blocksize
= od
[0].od_blocksize
;
4775 chunksize
= blocksize
;
4776 ASSERT(chunksize
== od
[1].od_gen
);
4778 VERIFY(dmu_object_info(os
, bigobj
, &doi
) == 0);
4779 VERIFY(ISP2(doi
.doi_data_block_size
));
4780 VERIFY(chunksize
== doi
.doi_data_block_size
);
4781 VERIFY(chunksize
>= 2 * sizeof (bufwad_t
));
4784 * Pick a random index and compute the offsets into packobj and bigobj.
4786 n
= ztest_random(regions
) * stride
+ ztest_random(width
);
4787 s
= 1 + ztest_random(width
- 1);
4789 packoff
= n
* sizeof (bufwad_t
);
4790 packsize
= s
* sizeof (bufwad_t
);
4792 bigoff
= n
* chunksize
;
4793 bigsize
= s
* chunksize
;
4795 packbuf
= umem_zalloc(packsize
, UMEM_NOFAIL
);
4796 bigbuf
= umem_zalloc(bigsize
, UMEM_NOFAIL
);
4798 VERIFY3U(0, ==, dmu_bonus_hold(os
, bigobj
, FTAG
, &bonus_db
));
4800 bigbuf_arcbufs
= umem_zalloc(2 * s
* sizeof (arc_buf_t
*), UMEM_NOFAIL
);
4803 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
4804 * Iteration 1 test zcopy to already referenced dbufs.
4805 * Iteration 2 test zcopy to dirty dbuf in the same txg.
4806 * Iteration 3 test zcopy to dbuf dirty in previous txg.
4807 * Iteration 4 test zcopy when dbuf is no longer dirty.
4808 * Iteration 5 test zcopy when it can't be done.
4809 * Iteration 6 one more zcopy write.
4811 for (i
= 0; i
< 7; i
++) {
4816 * In iteration 5 (i == 5) use arcbufs
4817 * that don't match bigobj blksz to test
4818 * dmu_assign_arcbuf_by_dbuf() when it can't directly
4819 * assign an arcbuf to a dbuf.
4821 for (j
= 0; j
< s
; j
++) {
4822 if (i
!= 5 || chunksize
< (SPA_MINBLOCKSIZE
* 2)) {
4824 dmu_request_arcbuf(bonus_db
, chunksize
);
4826 bigbuf_arcbufs
[2 * j
] =
4827 dmu_request_arcbuf(bonus_db
, chunksize
/ 2);
4828 bigbuf_arcbufs
[2 * j
+ 1] =
4829 dmu_request_arcbuf(bonus_db
, chunksize
/ 2);
4834 * Get a tx for the mods to both packobj and bigobj.
4836 tx
= dmu_tx_create(os
);
4838 dmu_tx_hold_write(tx
, packobj
, packoff
, packsize
);
4839 dmu_tx_hold_write(tx
, bigobj
, bigoff
, bigsize
);
4841 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
4843 umem_free(packbuf
, packsize
);
4844 umem_free(bigbuf
, bigsize
);
4845 for (j
= 0; j
< s
; j
++) {
4847 chunksize
< (SPA_MINBLOCKSIZE
* 2)) {
4848 dmu_return_arcbuf(bigbuf_arcbufs
[j
]);
4851 bigbuf_arcbufs
[2 * j
]);
4853 bigbuf_arcbufs
[2 * j
+ 1]);
4856 umem_free(bigbuf_arcbufs
, 2 * s
* sizeof (arc_buf_t
*));
4857 umem_free(od
, size
);
4858 dmu_buf_rele(bonus_db
, FTAG
);
4863 * 50% of the time don't read objects in the 1st iteration to
4864 * test dmu_assign_arcbuf_by_dbuf() for the case when there are
4865 * no existing dbufs for the specified offsets.
4867 if (i
!= 0 || ztest_random(2) != 0) {
4868 error
= dmu_read(os
, packobj
, packoff
,
4869 packsize
, packbuf
, DMU_READ_PREFETCH
);
4871 error
= dmu_read(os
, bigobj
, bigoff
, bigsize
,
4872 bigbuf
, DMU_READ_PREFETCH
);
4875 compare_and_update_pbbufs(s
, packbuf
, bigbuf
, bigsize
,
4879 * We've verified all the old bufwads, and made new ones.
4880 * Now write them out.
4882 dmu_write(os
, packobj
, packoff
, packsize
, packbuf
, tx
);
4883 if (ztest_opts
.zo_verbose
>= 7) {
4884 (void) printf("writing offset %llx size %llx"
4886 (u_longlong_t
)bigoff
,
4887 (u_longlong_t
)bigsize
,
4890 for (off
= bigoff
, j
= 0; j
< s
; j
++, off
+= chunksize
) {
4892 if (i
!= 5 || chunksize
< (SPA_MINBLOCKSIZE
* 2)) {
4893 bcopy((caddr_t
)bigbuf
+ (off
- bigoff
),
4894 bigbuf_arcbufs
[j
]->b_data
, chunksize
);
4896 bcopy((caddr_t
)bigbuf
+ (off
- bigoff
),
4897 bigbuf_arcbufs
[2 * j
]->b_data
,
4899 bcopy((caddr_t
)bigbuf
+ (off
- bigoff
) +
4901 bigbuf_arcbufs
[2 * j
+ 1]->b_data
,
4906 VERIFY(dmu_buf_hold(os
, bigobj
, off
,
4907 FTAG
, &dbt
, DMU_READ_NO_PREFETCH
) == 0);
4909 if (i
!= 5 || chunksize
< (SPA_MINBLOCKSIZE
* 2)) {
4910 dmu_assign_arcbuf_by_dbuf(bonus_db
, off
,
4911 bigbuf_arcbufs
[j
], tx
);
4913 dmu_assign_arcbuf_by_dbuf(bonus_db
, off
,
4914 bigbuf_arcbufs
[2 * j
], tx
);
4915 dmu_assign_arcbuf_by_dbuf(bonus_db
,
4916 off
+ chunksize
/ 2,
4917 bigbuf_arcbufs
[2 * j
+ 1], tx
);
4920 dmu_buf_rele(dbt
, FTAG
);
4926 * Sanity check the stuff we just wrote.
4929 void *packcheck
= umem_alloc(packsize
, UMEM_NOFAIL
);
4930 void *bigcheck
= umem_alloc(bigsize
, UMEM_NOFAIL
);
4932 VERIFY(0 == dmu_read(os
, packobj
, packoff
,
4933 packsize
, packcheck
, DMU_READ_PREFETCH
));
4934 VERIFY(0 == dmu_read(os
, bigobj
, bigoff
,
4935 bigsize
, bigcheck
, DMU_READ_PREFETCH
));
4937 ASSERT(bcmp(packbuf
, packcheck
, packsize
) == 0);
4938 ASSERT(bcmp(bigbuf
, bigcheck
, bigsize
) == 0);
4940 umem_free(packcheck
, packsize
);
4941 umem_free(bigcheck
, bigsize
);
4944 txg_wait_open(dmu_objset_pool(os
), 0);
4945 } else if (i
== 3) {
4946 txg_wait_synced(dmu_objset_pool(os
), 0);
4950 dmu_buf_rele(bonus_db
, FTAG
);
4951 umem_free(packbuf
, packsize
);
4952 umem_free(bigbuf
, bigsize
);
4953 umem_free(bigbuf_arcbufs
, 2 * s
* sizeof (arc_buf_t
*));
4954 umem_free(od
, size
);
4959 ztest_dmu_write_parallel(ztest_ds_t
*zd
, uint64_t id
)
4963 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
4964 uint64_t offset
= (1ULL << (ztest_random(20) + 43)) +
4965 (ztest_random(ZTEST_RANGE_LOCKS
) << SPA_MAXBLOCKSHIFT
);
4968 * Have multiple threads write to large offsets in an object
4969 * to verify that parallel writes to an object -- even to the
4970 * same blocks within the object -- doesn't cause any trouble.
4972 ztest_od_init(od
, ID_PARALLEL
, FTAG
, 0, DMU_OT_UINT64_OTHER
, 0, 0, 0);
4974 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
), B_FALSE
) != 0)
4977 while (ztest_random(10) != 0)
4978 ztest_io(zd
, od
->od_object
, offset
);
4980 umem_free(od
, sizeof (ztest_od_t
));
4984 ztest_dmu_prealloc(ztest_ds_t
*zd
, uint64_t id
)
4987 uint64_t offset
= (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT
)) +
4988 (ztest_random(ZTEST_RANGE_LOCKS
) << SPA_MAXBLOCKSHIFT
);
4989 uint64_t count
= ztest_random(20) + 1;
4990 uint64_t blocksize
= ztest_random_blocksize();
4993 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
4995 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, blocksize
, 0, 0);
4997 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
),
4998 !ztest_random(2)) != 0) {
4999 umem_free(od
, sizeof (ztest_od_t
));
5003 if (ztest_truncate(zd
, od
->od_object
, offset
, count
* blocksize
) != 0) {
5004 umem_free(od
, sizeof (ztest_od_t
));
5008 ztest_prealloc(zd
, od
->od_object
, offset
, count
* blocksize
);
5010 data
= umem_zalloc(blocksize
, UMEM_NOFAIL
);
5012 while (ztest_random(count
) != 0) {
5013 uint64_t randoff
= offset
+ (ztest_random(count
) * blocksize
);
5014 if (ztest_write(zd
, od
->od_object
, randoff
, blocksize
,
5017 while (ztest_random(4) != 0)
5018 ztest_io(zd
, od
->od_object
, randoff
);
5021 umem_free(data
, blocksize
);
5022 umem_free(od
, sizeof (ztest_od_t
));
5026 * Verify that zap_{create,destroy,add,remove,update} work as expected.
5028 #define ZTEST_ZAP_MIN_INTS 1
5029 #define ZTEST_ZAP_MAX_INTS 4
5030 #define ZTEST_ZAP_MAX_PROPS 1000
5033 ztest_zap(ztest_ds_t
*zd
, uint64_t id
)
5035 objset_t
*os
= zd
->zd_os
;
5038 uint64_t txg
, last_txg
;
5039 uint64_t value
[ZTEST_ZAP_MAX_INTS
];
5040 uint64_t zl_ints
, zl_intsize
, prop
;
5043 char propname
[100], txgname
[100];
5045 char *hc
[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
5047 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
5048 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_ZAP_OTHER
, 0, 0, 0);
5050 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
),
5051 !ztest_random(2)) != 0)
5054 object
= od
->od_object
;
5057 * Generate a known hash collision, and verify that
5058 * we can lookup and remove both entries.
5060 tx
= dmu_tx_create(os
);
5061 dmu_tx_hold_zap(tx
, object
, B_TRUE
, NULL
);
5062 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
5065 for (i
= 0; i
< 2; i
++) {
5067 VERIFY3U(0, ==, zap_add(os
, object
, hc
[i
], sizeof (uint64_t),
5070 for (i
= 0; i
< 2; i
++) {
5071 VERIFY3U(EEXIST
, ==, zap_add(os
, object
, hc
[i
],
5072 sizeof (uint64_t), 1, &value
[i
], tx
));
5074 zap_length(os
, object
, hc
[i
], &zl_intsize
, &zl_ints
));
5075 ASSERT3U(zl_intsize
, ==, sizeof (uint64_t));
5076 ASSERT3U(zl_ints
, ==, 1);
5078 for (i
= 0; i
< 2; i
++) {
5079 VERIFY3U(0, ==, zap_remove(os
, object
, hc
[i
], tx
));
5084 * Generate a buch of random entries.
5086 ints
= MAX(ZTEST_ZAP_MIN_INTS
, object
% ZTEST_ZAP_MAX_INTS
);
5088 prop
= ztest_random(ZTEST_ZAP_MAX_PROPS
);
5089 (void) sprintf(propname
, "prop_%llu", (u_longlong_t
)prop
);
5090 (void) sprintf(txgname
, "txg_%llu", (u_longlong_t
)prop
);
5091 bzero(value
, sizeof (value
));
5095 * If these zap entries already exist, validate their contents.
5097 error
= zap_length(os
, object
, txgname
, &zl_intsize
, &zl_ints
);
5099 ASSERT3U(zl_intsize
, ==, sizeof (uint64_t));
5100 ASSERT3U(zl_ints
, ==, 1);
5102 VERIFY(zap_lookup(os
, object
, txgname
, zl_intsize
,
5103 zl_ints
, &last_txg
) == 0);
5105 VERIFY(zap_length(os
, object
, propname
, &zl_intsize
,
5108 ASSERT3U(zl_intsize
, ==, sizeof (uint64_t));
5109 ASSERT3U(zl_ints
, ==, ints
);
5111 VERIFY(zap_lookup(os
, object
, propname
, zl_intsize
,
5112 zl_ints
, value
) == 0);
5114 for (i
= 0; i
< ints
; i
++) {
5115 ASSERT3U(value
[i
], ==, last_txg
+ object
+ i
);
5118 ASSERT3U(error
, ==, ENOENT
);
5122 * Atomically update two entries in our zap object.
5123 * The first is named txg_%llu, and contains the txg
5124 * in which the property was last updated. The second
5125 * is named prop_%llu, and the nth element of its value
5126 * should be txg + object + n.
5128 tx
= dmu_tx_create(os
);
5129 dmu_tx_hold_zap(tx
, object
, B_TRUE
, NULL
);
5130 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
5135 fatal(0, "zap future leak: old %llu new %llu", last_txg
, txg
);
5137 for (i
= 0; i
< ints
; i
++)
5138 value
[i
] = txg
+ object
+ i
;
5140 VERIFY3U(0, ==, zap_update(os
, object
, txgname
, sizeof (uint64_t),
5142 VERIFY3U(0, ==, zap_update(os
, object
, propname
, sizeof (uint64_t),
5148 * Remove a random pair of entries.
5150 prop
= ztest_random(ZTEST_ZAP_MAX_PROPS
);
5151 (void) sprintf(propname
, "prop_%llu", (u_longlong_t
)prop
);
5152 (void) sprintf(txgname
, "txg_%llu", (u_longlong_t
)prop
);
5154 error
= zap_length(os
, object
, txgname
, &zl_intsize
, &zl_ints
);
5156 if (error
== ENOENT
)
5161 tx
= dmu_tx_create(os
);
5162 dmu_tx_hold_zap(tx
, object
, B_TRUE
, NULL
);
5163 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
5166 VERIFY3U(0, ==, zap_remove(os
, object
, txgname
, tx
));
5167 VERIFY3U(0, ==, zap_remove(os
, object
, propname
, tx
));
5170 umem_free(od
, sizeof (ztest_od_t
));
5174 * Testcase to test the upgrading of a microzap to fatzap.
5177 ztest_fzap(ztest_ds_t
*zd
, uint64_t id
)
5179 objset_t
*os
= zd
->zd_os
;
5181 uint64_t object
, txg
;
5184 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
5185 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_ZAP_OTHER
, 0, 0, 0);
5187 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
),
5188 !ztest_random(2)) != 0)
5190 object
= od
->od_object
;
5193 * Add entries to this ZAP and make sure it spills over
5194 * and gets upgraded to a fatzap. Also, since we are adding
5195 * 2050 entries we should see ptrtbl growth and leaf-block split.
5197 for (i
= 0; i
< 2050; i
++) {
5198 char name
[ZFS_MAX_DATASET_NAME_LEN
];
5203 (void) snprintf(name
, sizeof (name
), "fzap-%llu-%llu",
5204 (u_longlong_t
)id
, (u_longlong_t
)value
);
5206 tx
= dmu_tx_create(os
);
5207 dmu_tx_hold_zap(tx
, object
, B_TRUE
, name
);
5208 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
5211 error
= zap_add(os
, object
, name
, sizeof (uint64_t), 1,
5213 ASSERT(error
== 0 || error
== EEXIST
);
5217 umem_free(od
, sizeof (ztest_od_t
));
5222 ztest_zap_parallel(ztest_ds_t
*zd
, uint64_t id
)
5224 objset_t
*os
= zd
->zd_os
;
5226 uint64_t txg
, object
, count
, wsize
, wc
, zl_wsize
, zl_wc
;
5228 int i
, namelen
, error
;
5229 int micro
= ztest_random(2);
5230 char name
[20], string_value
[20];
5233 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
5234 ztest_od_init(od
, ID_PARALLEL
, FTAG
, micro
, DMU_OT_ZAP_OTHER
, 0, 0, 0);
5236 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
), B_FALSE
) != 0) {
5237 umem_free(od
, sizeof (ztest_od_t
));
5241 object
= od
->od_object
;
5244 * Generate a random name of the form 'xxx.....' where each
5245 * x is a random printable character and the dots are dots.
5246 * There are 94 such characters, and the name length goes from
5247 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
5249 namelen
= ztest_random(sizeof (name
) - 5) + 5 + 1;
5251 for (i
= 0; i
< 3; i
++)
5252 name
[i
] = '!' + ztest_random('~' - '!' + 1);
5253 for (; i
< namelen
- 1; i
++)
5257 if ((namelen
& 1) || micro
) {
5258 wsize
= sizeof (txg
);
5264 data
= string_value
;
5268 VERIFY0(zap_count(os
, object
, &count
));
5269 ASSERT(count
!= -1ULL);
5272 * Select an operation: length, lookup, add, update, remove.
5274 i
= ztest_random(5);
5277 tx
= dmu_tx_create(os
);
5278 dmu_tx_hold_zap(tx
, object
, B_TRUE
, NULL
);
5279 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
5281 umem_free(od
, sizeof (ztest_od_t
));
5284 bcopy(name
, string_value
, namelen
);
5288 bzero(string_value
, namelen
);
5294 error
= zap_length(os
, object
, name
, &zl_wsize
, &zl_wc
);
5296 ASSERT3U(wsize
, ==, zl_wsize
);
5297 ASSERT3U(wc
, ==, zl_wc
);
5299 ASSERT3U(error
, ==, ENOENT
);
5304 error
= zap_lookup(os
, object
, name
, wsize
, wc
, data
);
5306 if (data
== string_value
&&
5307 bcmp(name
, data
, namelen
) != 0)
5308 fatal(0, "name '%s' != val '%s' len %d",
5309 name
, data
, namelen
);
5311 ASSERT3U(error
, ==, ENOENT
);
5316 error
= zap_add(os
, object
, name
, wsize
, wc
, data
, tx
);
5317 ASSERT(error
== 0 || error
== EEXIST
);
5321 VERIFY(zap_update(os
, object
, name
, wsize
, wc
, data
, tx
) == 0);
5325 error
= zap_remove(os
, object
, name
, tx
);
5326 ASSERT(error
== 0 || error
== ENOENT
);
5333 umem_free(od
, sizeof (ztest_od_t
));
5337 * Commit callback data.
5339 typedef struct ztest_cb_data
{
5340 list_node_t zcd_node
;
5342 int zcd_expected_err
;
5343 boolean_t zcd_added
;
5344 boolean_t zcd_called
;
5348 /* This is the actual commit callback function */
5350 ztest_commit_callback(void *arg
, int error
)
5352 ztest_cb_data_t
*data
= arg
;
5353 uint64_t synced_txg
;
5355 VERIFY(data
!= NULL
);
5356 VERIFY3S(data
->zcd_expected_err
, ==, error
);
5357 VERIFY(!data
->zcd_called
);
5359 synced_txg
= spa_last_synced_txg(data
->zcd_spa
);
5360 if (data
->zcd_txg
> synced_txg
)
5361 fatal(0, "commit callback of txg %" PRIu64
" called prematurely"
5362 ", last synced txg = %" PRIu64
"\n", data
->zcd_txg
,
5365 data
->zcd_called
= B_TRUE
;
5367 if (error
== ECANCELED
) {
5368 ASSERT0(data
->zcd_txg
);
5369 ASSERT(!data
->zcd_added
);
5372 * The private callback data should be destroyed here, but
5373 * since we are going to check the zcd_called field after
5374 * dmu_tx_abort(), we will destroy it there.
5379 ASSERT(data
->zcd_added
);
5380 ASSERT3U(data
->zcd_txg
, !=, 0);
5382 (void) mutex_enter(&zcl
.zcl_callbacks_lock
);
5384 /* See if this cb was called more quickly */
5385 if ((synced_txg
- data
->zcd_txg
) < zc_min_txg_delay
)
5386 zc_min_txg_delay
= synced_txg
- data
->zcd_txg
;
5388 /* Remove our callback from the list */
5389 list_remove(&zcl
.zcl_callbacks
, data
);
5391 (void) mutex_exit(&zcl
.zcl_callbacks_lock
);
5393 umem_free(data
, sizeof (ztest_cb_data_t
));
5396 /* Allocate and initialize callback data structure */
5397 static ztest_cb_data_t
*
5398 ztest_create_cb_data(objset_t
*os
, uint64_t txg
)
5400 ztest_cb_data_t
*cb_data
;
5402 cb_data
= umem_zalloc(sizeof (ztest_cb_data_t
), UMEM_NOFAIL
);
5404 cb_data
->zcd_txg
= txg
;
5405 cb_data
->zcd_spa
= dmu_objset_spa(os
);
5406 list_link_init(&cb_data
->zcd_node
);
5412 * Commit callback test.
5415 ztest_dmu_commit_callbacks(ztest_ds_t
*zd
, uint64_t id
)
5417 objset_t
*os
= zd
->zd_os
;
5420 ztest_cb_data_t
*cb_data
[3], *tmp_cb
;
5421 uint64_t old_txg
, txg
;
5424 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
5425 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, 0, 0, 0);
5427 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
), B_FALSE
) != 0) {
5428 umem_free(od
, sizeof (ztest_od_t
));
5432 tx
= dmu_tx_create(os
);
5434 cb_data
[0] = ztest_create_cb_data(os
, 0);
5435 dmu_tx_callback_register(tx
, ztest_commit_callback
, cb_data
[0]);
5437 dmu_tx_hold_write(tx
, od
->od_object
, 0, sizeof (uint64_t));
5439 /* Every once in a while, abort the transaction on purpose */
5440 if (ztest_random(100) == 0)
5444 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
5446 txg
= error
? 0 : dmu_tx_get_txg(tx
);
5448 cb_data
[0]->zcd_txg
= txg
;
5449 cb_data
[1] = ztest_create_cb_data(os
, txg
);
5450 dmu_tx_callback_register(tx
, ztest_commit_callback
, cb_data
[1]);
5454 * It's not a strict requirement to call the registered
5455 * callbacks from inside dmu_tx_abort(), but that's what
5456 * it's supposed to happen in the current implementation
5457 * so we will check for that.
5459 for (i
= 0; i
< 2; i
++) {
5460 cb_data
[i
]->zcd_expected_err
= ECANCELED
;
5461 VERIFY(!cb_data
[i
]->zcd_called
);
5466 for (i
= 0; i
< 2; i
++) {
5467 VERIFY(cb_data
[i
]->zcd_called
);
5468 umem_free(cb_data
[i
], sizeof (ztest_cb_data_t
));
5471 umem_free(od
, sizeof (ztest_od_t
));
5475 cb_data
[2] = ztest_create_cb_data(os
, txg
);
5476 dmu_tx_callback_register(tx
, ztest_commit_callback
, cb_data
[2]);
5479 * Read existing data to make sure there isn't a future leak.
5481 VERIFY(0 == dmu_read(os
, od
->od_object
, 0, sizeof (uint64_t),
5482 &old_txg
, DMU_READ_PREFETCH
));
5485 fatal(0, "future leak: got %" PRIu64
", open txg is %" PRIu64
,
5488 dmu_write(os
, od
->od_object
, 0, sizeof (uint64_t), &txg
, tx
);
5490 (void) mutex_enter(&zcl
.zcl_callbacks_lock
);
5493 * Since commit callbacks don't have any ordering requirement and since
5494 * it is theoretically possible for a commit callback to be called
5495 * after an arbitrary amount of time has elapsed since its txg has been
5496 * synced, it is difficult to reliably determine whether a commit
5497 * callback hasn't been called due to high load or due to a flawed
5500 * In practice, we will assume that if after a certain number of txgs a
5501 * commit callback hasn't been called, then most likely there's an
5502 * implementation bug..
5504 tmp_cb
= list_head(&zcl
.zcl_callbacks
);
5505 if (tmp_cb
!= NULL
&&
5506 tmp_cb
->zcd_txg
+ ZTEST_COMMIT_CB_THRESH
< txg
) {
5507 fatal(0, "Commit callback threshold exceeded, oldest txg: %"
5508 PRIu64
", open txg: %" PRIu64
"\n", tmp_cb
->zcd_txg
, txg
);
5512 * Let's find the place to insert our callbacks.
5514 * Even though the list is ordered by txg, it is possible for the
5515 * insertion point to not be the end because our txg may already be
5516 * quiescing at this point and other callbacks in the open txg
5517 * (from other objsets) may have sneaked in.
5519 tmp_cb
= list_tail(&zcl
.zcl_callbacks
);
5520 while (tmp_cb
!= NULL
&& tmp_cb
->zcd_txg
> txg
)
5521 tmp_cb
= list_prev(&zcl
.zcl_callbacks
, tmp_cb
);
5523 /* Add the 3 callbacks to the list */
5524 for (i
= 0; i
< 3; i
++) {
5526 list_insert_head(&zcl
.zcl_callbacks
, cb_data
[i
]);
5528 list_insert_after(&zcl
.zcl_callbacks
, tmp_cb
,
5531 cb_data
[i
]->zcd_added
= B_TRUE
;
5532 VERIFY(!cb_data
[i
]->zcd_called
);
5534 tmp_cb
= cb_data
[i
];
5539 (void) mutex_exit(&zcl
.zcl_callbacks_lock
);
5543 umem_free(od
, sizeof (ztest_od_t
));
5547 * Visit each object in the dataset. Verify that its properties
5548 * are consistent what was stored in the block tag when it was created,
5549 * and that its unused bonus buffer space has not been overwritten.
5553 ztest_verify_dnode_bt(ztest_ds_t
*zd
, uint64_t id
)
5555 objset_t
*os
= zd
->zd_os
;
5559 for (obj
= 0; err
== 0; err
= dmu_object_next(os
, &obj
, FALSE
, 0)) {
5560 ztest_block_tag_t
*bt
= NULL
;
5561 dmu_object_info_t doi
;
5564 ztest_object_lock(zd
, obj
, RL_READER
);
5565 if (dmu_bonus_hold(os
, obj
, FTAG
, &db
) != 0) {
5566 ztest_object_unlock(zd
, obj
);
5570 dmu_object_info_from_db(db
, &doi
);
5571 if (doi
.doi_bonus_size
>= sizeof (*bt
))
5572 bt
= ztest_bt_bonus(db
);
5574 if (bt
&& bt
->bt_magic
== BT_MAGIC
) {
5575 ztest_bt_verify(bt
, os
, obj
, doi
.doi_dnodesize
,
5576 bt
->bt_offset
, bt
->bt_gen
, bt
->bt_txg
,
5578 ztest_verify_unused_bonus(db
, bt
, obj
, os
, bt
->bt_gen
);
5581 dmu_buf_rele(db
, FTAG
);
5582 ztest_object_unlock(zd
, obj
);
5588 ztest_dsl_prop_get_set(ztest_ds_t
*zd
, uint64_t id
)
5590 zfs_prop_t proplist
[] = {
5592 ZFS_PROP_COMPRESSION
,
5598 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
5600 for (p
= 0; p
< sizeof (proplist
) / sizeof (proplist
[0]); p
++)
5601 (void) ztest_dsl_prop_set_uint64(zd
->zd_name
, proplist
[p
],
5602 ztest_random_dsl_prop(proplist
[p
]), (int)ztest_random(2));
5604 VERIFY0(ztest_dsl_prop_set_uint64(zd
->zd_name
, ZFS_PROP_RECORDSIZE
,
5605 ztest_random_blocksize(), (int)ztest_random(2)));
5607 (void) pthread_rwlock_unlock(&ztest_name_lock
);
5612 ztest_remap_blocks(ztest_ds_t
*zd
, uint64_t id
)
5614 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
5616 int error
= dmu_objset_remap_indirects(zd
->zd_name
);
5617 if (error
== ENOSPC
)
5621 (void) pthread_rwlock_unlock(&ztest_name_lock
);
5626 ztest_spa_prop_get_set(ztest_ds_t
*zd
, uint64_t id
)
5628 nvlist_t
*props
= NULL
;
5630 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
5632 (void) ztest_spa_prop_set_uint64(ZPOOL_PROP_DEDUPDITTO
,
5633 ZIO_DEDUPDITTO_MIN
+ ztest_random(ZIO_DEDUPDITTO_MIN
));
5635 VERIFY0(spa_prop_get(ztest_spa
, &props
));
5637 if (ztest_opts
.zo_verbose
>= 6)
5638 dump_nvlist(props
, 4);
5642 (void) pthread_rwlock_unlock(&ztest_name_lock
);
5646 user_release_one(const char *snapname
, const char *holdname
)
5648 nvlist_t
*snaps
, *holds
;
5651 snaps
= fnvlist_alloc();
5652 holds
= fnvlist_alloc();
5653 fnvlist_add_boolean(holds
, holdname
);
5654 fnvlist_add_nvlist(snaps
, snapname
, holds
);
5655 fnvlist_free(holds
);
5656 error
= dsl_dataset_user_release(snaps
, NULL
);
5657 fnvlist_free(snaps
);
5662 * Test snapshot hold/release and deferred destroy.
5665 ztest_dmu_snapshot_hold(ztest_ds_t
*zd
, uint64_t id
)
5668 objset_t
*os
= zd
->zd_os
;
5672 char clonename
[100];
5674 char osname
[ZFS_MAX_DATASET_NAME_LEN
];
5677 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
5679 dmu_objset_name(os
, osname
);
5681 (void) snprintf(snapname
, sizeof (snapname
), "sh1_%llu",
5683 (void) snprintf(fullname
, sizeof (fullname
), "%s@%s", osname
, snapname
);
5684 (void) snprintf(clonename
, sizeof (clonename
),
5685 "%s/ch1_%llu", osname
, (u_longlong_t
)id
);
5686 (void) snprintf(tag
, sizeof (tag
), "tag_%llu", (u_longlong_t
)id
);
5689 * Clean up from any previous run.
5691 error
= dsl_destroy_head(clonename
);
5692 if (error
!= ENOENT
)
5694 error
= user_release_one(fullname
, tag
);
5695 if (error
!= ESRCH
&& error
!= ENOENT
)
5697 error
= dsl_destroy_snapshot(fullname
, B_FALSE
);
5698 if (error
!= ENOENT
)
5702 * Create snapshot, clone it, mark snap for deferred destroy,
5703 * destroy clone, verify snap was also destroyed.
5705 error
= dmu_objset_snapshot_one(osname
, snapname
);
5707 if (error
== ENOSPC
) {
5708 ztest_record_enospc("dmu_objset_snapshot");
5711 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname
, error
);
5714 error
= dmu_objset_clone(clonename
, fullname
);
5716 if (error
== ENOSPC
) {
5717 ztest_record_enospc("dmu_objset_clone");
5720 fatal(0, "dmu_objset_clone(%s) = %d", clonename
, error
);
5723 error
= dsl_destroy_snapshot(fullname
, B_TRUE
);
5725 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5729 error
= dsl_destroy_head(clonename
);
5731 fatal(0, "dsl_destroy_head(%s) = %d", clonename
, error
);
5733 error
= dmu_objset_hold(fullname
, FTAG
, &origin
);
5734 if (error
!= ENOENT
)
5735 fatal(0, "dmu_objset_hold(%s) = %d", fullname
, error
);
5738 * Create snapshot, add temporary hold, verify that we can't
5739 * destroy a held snapshot, mark for deferred destroy,
5740 * release hold, verify snapshot was destroyed.
5742 error
= dmu_objset_snapshot_one(osname
, snapname
);
5744 if (error
== ENOSPC
) {
5745 ztest_record_enospc("dmu_objset_snapshot");
5748 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname
, error
);
5751 holds
= fnvlist_alloc();
5752 fnvlist_add_string(holds
, fullname
, tag
);
5753 error
= dsl_dataset_user_hold(holds
, 0, NULL
);
5754 fnvlist_free(holds
);
5756 if (error
== ENOSPC
) {
5757 ztest_record_enospc("dsl_dataset_user_hold");
5760 fatal(0, "dsl_dataset_user_hold(%s, %s) = %u",
5761 fullname
, tag
, error
);
5764 error
= dsl_destroy_snapshot(fullname
, B_FALSE
);
5765 if (error
!= EBUSY
) {
5766 fatal(0, "dsl_destroy_snapshot(%s, B_FALSE) = %d",
5770 error
= dsl_destroy_snapshot(fullname
, B_TRUE
);
5772 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5776 error
= user_release_one(fullname
, tag
);
5778 fatal(0, "user_release_one(%s, %s) = %d", fullname
, tag
, error
);
5780 VERIFY3U(dmu_objset_hold(fullname
, FTAG
, &origin
), ==, ENOENT
);
5783 (void) pthread_rwlock_unlock(&ztest_name_lock
);
5787 * Inject random faults into the on-disk data.
5791 ztest_fault_inject(ztest_ds_t
*zd
, uint64_t id
)
5793 ztest_shared_t
*zs
= ztest_shared
;
5794 spa_t
*spa
= ztest_spa
;
5798 uint64_t bad
= 0x1990c0ffeedecadeull
;
5803 int bshift
= SPA_MAXBLOCKSHIFT
+ 2;
5809 boolean_t islog
= B_FALSE
;
5811 path0
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
5812 pathrand
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
5814 mutex_enter(&ztest_vdev_lock
);
5817 * Device removal is in progress, fault injection must be disabled
5818 * until it completes and the pool is scrubbed. The fault injection
5819 * strategy for damaging blocks does not take in to account evacuated
5820 * blocks which may have already been damaged.
5822 if (ztest_device_removal_active
) {
5823 mutex_exit(&ztest_vdev_lock
);
5827 maxfaults
= MAXFAULTS(zs
);
5828 leaves
= MAX(zs
->zs_mirrors
, 1) * ztest_opts
.zo_raidz
;
5829 mirror_save
= zs
->zs_mirrors
;
5830 mutex_exit(&ztest_vdev_lock
);
5832 ASSERT(leaves
>= 1);
5835 * Grab the name lock as reader. There are some operations
5836 * which don't like to have their vdevs changed while
5837 * they are in progress (i.e. spa_change_guid). Those
5838 * operations will have grabbed the name lock as writer.
5840 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
5843 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
5845 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
5847 if (ztest_random(2) == 0) {
5849 * Inject errors on a normal data device or slog device.
5851 top
= ztest_random_vdev_top(spa
, B_TRUE
);
5852 leaf
= ztest_random(leaves
) + zs
->zs_splits
;
5855 * Generate paths to the first leaf in this top-level vdev,
5856 * and to the random leaf we selected. We'll induce transient
5857 * write failures and random online/offline activity on leaf 0,
5858 * and we'll write random garbage to the randomly chosen leaf.
5860 (void) snprintf(path0
, MAXPATHLEN
, ztest_dev_template
,
5861 ztest_opts
.zo_dir
, ztest_opts
.zo_pool
,
5862 top
* leaves
+ zs
->zs_splits
);
5863 (void) snprintf(pathrand
, MAXPATHLEN
, ztest_dev_template
,
5864 ztest_opts
.zo_dir
, ztest_opts
.zo_pool
,
5865 top
* leaves
+ leaf
);
5867 vd0
= vdev_lookup_by_path(spa
->spa_root_vdev
, path0
);
5868 if (vd0
!= NULL
&& vd0
->vdev_top
->vdev_islog
)
5872 * If the top-level vdev needs to be resilvered
5873 * then we only allow faults on the device that is
5876 if (vd0
!= NULL
&& maxfaults
!= 1 &&
5877 (!vdev_resilver_needed(vd0
->vdev_top
, NULL
, NULL
) ||
5878 vd0
->vdev_resilver_txg
!= 0)) {
5880 * Make vd0 explicitly claim to be unreadable,
5881 * or unwriteable, or reach behind its back
5882 * and close the underlying fd. We can do this if
5883 * maxfaults == 0 because we'll fail and reexecute,
5884 * and we can do it if maxfaults >= 2 because we'll
5885 * have enough redundancy. If maxfaults == 1, the
5886 * combination of this with injection of random data
5887 * corruption below exceeds the pool's fault tolerance.
5889 vdev_file_t
*vf
= vd0
->vdev_tsd
;
5891 zfs_dbgmsg("injecting fault to vdev %llu; maxfaults=%d",
5892 (long long)vd0
->vdev_id
, (int)maxfaults
);
5894 if (vf
!= NULL
&& ztest_random(3) == 0) {
5895 (void) close(vf
->vf_vnode
->v_fd
);
5896 vf
->vf_vnode
->v_fd
= -1;
5897 } else if (ztest_random(2) == 0) {
5898 vd0
->vdev_cant_read
= B_TRUE
;
5900 vd0
->vdev_cant_write
= B_TRUE
;
5902 guid0
= vd0
->vdev_guid
;
5906 * Inject errors on an l2cache device.
5908 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
5910 if (sav
->sav_count
== 0) {
5911 spa_config_exit(spa
, SCL_STATE
, FTAG
);
5912 (void) pthread_rwlock_unlock(&ztest_name_lock
);
5915 vd0
= sav
->sav_vdevs
[ztest_random(sav
->sav_count
)];
5916 guid0
= vd0
->vdev_guid
;
5917 (void) strcpy(path0
, vd0
->vdev_path
);
5918 (void) strcpy(pathrand
, vd0
->vdev_path
);
5922 maxfaults
= INT_MAX
; /* no limit on cache devices */
5925 spa_config_exit(spa
, SCL_STATE
, FTAG
);
5926 (void) pthread_rwlock_unlock(&ztest_name_lock
);
5929 * If we can tolerate two or more faults, or we're dealing
5930 * with a slog, randomly online/offline vd0.
5932 if ((maxfaults
>= 2 || islog
) && guid0
!= 0) {
5933 if (ztest_random(10) < 6) {
5934 int flags
= (ztest_random(2) == 0 ?
5935 ZFS_OFFLINE_TEMPORARY
: 0);
5938 * We have to grab the zs_name_lock as writer to
5939 * prevent a race between offlining a slog and
5940 * destroying a dataset. Offlining the slog will
5941 * grab a reference on the dataset which may cause
5942 * dsl_destroy_head() to fail with EBUSY thus
5943 * leaving the dataset in an inconsistent state.
5946 (void) pthread_rwlock_wrlock(&ztest_name_lock
);
5948 VERIFY(vdev_offline(spa
, guid0
, flags
) != EBUSY
);
5951 (void) pthread_rwlock_unlock(&ztest_name_lock
);
5954 * Ideally we would like to be able to randomly
5955 * call vdev_[on|off]line without holding locks
5956 * to force unpredictable failures but the side
5957 * effects of vdev_[on|off]line prevent us from
5958 * doing so. We grab the ztest_vdev_lock here to
5959 * prevent a race between injection testing and
5962 mutex_enter(&ztest_vdev_lock
);
5963 (void) vdev_online(spa
, guid0
, 0, NULL
);
5964 mutex_exit(&ztest_vdev_lock
);
5972 * We have at least single-fault tolerance, so inject data corruption.
5974 fd
= open(pathrand
, O_RDWR
);
5976 if (fd
== -1) /* we hit a gap in the device namespace */
5979 fsize
= lseek(fd
, 0, SEEK_END
);
5981 while (--iters
!= 0) {
5983 * The offset must be chosen carefully to ensure that
5984 * we do not inject a given logical block with errors
5985 * on two different leaf devices, because ZFS can not
5986 * tolerate that (if maxfaults==1).
5988 * We divide each leaf into chunks of size
5989 * (# leaves * SPA_MAXBLOCKSIZE * 4). Within each chunk
5990 * there is a series of ranges to which we can inject errors.
5991 * Each range can accept errors on only a single leaf vdev.
5992 * The error injection ranges are separated by ranges
5993 * which we will not inject errors on any device (DMZs).
5994 * Each DMZ must be large enough such that a single block
5995 * can not straddle it, so that a single block can not be
5996 * a target in two different injection ranges (on different
5999 * For example, with 3 leaves, each chunk looks like:
6000 * 0 to 32M: injection range for leaf 0
6001 * 32M to 64M: DMZ - no injection allowed
6002 * 64M to 96M: injection range for leaf 1
6003 * 96M to 128M: DMZ - no injection allowed
6004 * 128M to 160M: injection range for leaf 2
6005 * 160M to 192M: DMZ - no injection allowed
6007 offset
= ztest_random(fsize
/ (leaves
<< bshift
)) *
6008 (leaves
<< bshift
) + (leaf
<< bshift
) +
6009 (ztest_random(1ULL << (bshift
- 1)) & -8ULL);
6012 * Only allow damage to the labels at one end of the vdev.
6014 * If all labels are damaged, the device will be totally
6015 * inaccessible, which will result in loss of data,
6016 * because we also damage (parts of) the other side of
6019 * Additionally, we will always have both an even and an
6020 * odd label, so that we can handle crashes in the
6021 * middle of vdev_config_sync().
6023 if ((leaf
& 1) == 0 && offset
< VDEV_LABEL_START_SIZE
)
6027 * The two end labels are stored at the "end" of the disk, but
6028 * the end of the disk (vdev_psize) is aligned to
6029 * sizeof (vdev_label_t).
6031 uint64_t psize
= P2ALIGN(fsize
, sizeof (vdev_label_t
));
6032 if ((leaf
& 1) == 1 &&
6033 offset
+ sizeof (bad
) > psize
- VDEV_LABEL_END_SIZE
)
6036 mutex_enter(&ztest_vdev_lock
);
6037 if (mirror_save
!= zs
->zs_mirrors
) {
6038 mutex_exit(&ztest_vdev_lock
);
6043 if (pwrite(fd
, &bad
, sizeof (bad
), offset
) != sizeof (bad
))
6044 fatal(1, "can't inject bad word at 0x%llx in %s",
6047 mutex_exit(&ztest_vdev_lock
);
6049 if (ztest_opts
.zo_verbose
>= 7)
6050 (void) printf("injected bad word into %s,"
6051 " offset 0x%llx\n", pathrand
, (u_longlong_t
)offset
);
6056 umem_free(path0
, MAXPATHLEN
);
6057 umem_free(pathrand
, MAXPATHLEN
);
6061 * Verify that DDT repair works as expected.
6064 ztest_ddt_repair(ztest_ds_t
*zd
, uint64_t id
)
6066 ztest_shared_t
*zs
= ztest_shared
;
6067 spa_t
*spa
= ztest_spa
;
6068 objset_t
*os
= zd
->zd_os
;
6070 uint64_t object
, blocksize
, txg
, pattern
, psize
;
6071 enum zio_checksum checksum
= spa_dedup_checksum(spa
);
6076 int copies
= 2 * ZIO_DEDUPDITTO_MIN
;
6079 blocksize
= ztest_random_blocksize();
6080 blocksize
= MIN(blocksize
, 2048); /* because we write so many */
6082 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
6083 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, blocksize
, 0, 0);
6085 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
), B_FALSE
) != 0) {
6086 umem_free(od
, sizeof (ztest_od_t
));
6091 * Take the name lock as writer to prevent anyone else from changing
6092 * the pool and dataset properies we need to maintain during this test.
6094 (void) pthread_rwlock_wrlock(&ztest_name_lock
);
6096 if (ztest_dsl_prop_set_uint64(zd
->zd_name
, ZFS_PROP_DEDUP
, checksum
,
6098 ztest_dsl_prop_set_uint64(zd
->zd_name
, ZFS_PROP_COPIES
, 1,
6100 (void) pthread_rwlock_unlock(&ztest_name_lock
);
6101 umem_free(od
, sizeof (ztest_od_t
));
6105 dmu_objset_stats_t dds
;
6106 dsl_pool_config_enter(dmu_objset_pool(os
), FTAG
);
6107 dmu_objset_fast_stat(os
, &dds
);
6108 dsl_pool_config_exit(dmu_objset_pool(os
), FTAG
);
6110 object
= od
[0].od_object
;
6111 blocksize
= od
[0].od_blocksize
;
6112 pattern
= zs
->zs_guid
^ dds
.dds_guid
;
6114 ASSERT(object
!= 0);
6116 tx
= dmu_tx_create(os
);
6117 dmu_tx_hold_write(tx
, object
, 0, copies
* blocksize
);
6118 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
6120 (void) pthread_rwlock_unlock(&ztest_name_lock
);
6121 umem_free(od
, sizeof (ztest_od_t
));
6126 * Write all the copies of our block.
6128 for (i
= 0; i
< copies
; i
++) {
6129 uint64_t offset
= i
* blocksize
;
6130 int error
= dmu_buf_hold(os
, object
, offset
, FTAG
, &db
,
6131 DMU_READ_NO_PREFETCH
);
6133 fatal(B_FALSE
, "dmu_buf_hold(%p, %llu, %llu) = %u",
6134 os
, (long long)object
, (long long) offset
, error
);
6136 ASSERT(db
->db_offset
== offset
);
6137 ASSERT(db
->db_size
== blocksize
);
6138 ASSERT(ztest_pattern_match(db
->db_data
, db
->db_size
, pattern
) ||
6139 ztest_pattern_match(db
->db_data
, db
->db_size
, 0ULL));
6140 dmu_buf_will_fill(db
, tx
);
6141 ztest_pattern_set(db
->db_data
, db
->db_size
, pattern
);
6142 dmu_buf_rele(db
, FTAG
);
6146 txg_wait_synced(spa_get_dsl(spa
), txg
);
6149 * Find out what block we got.
6151 VERIFY0(dmu_buf_hold(os
, object
, 0, FTAG
, &db
,
6152 DMU_READ_NO_PREFETCH
));
6153 blk
= *((dmu_buf_impl_t
*)db
)->db_blkptr
;
6154 dmu_buf_rele(db
, FTAG
);
6157 * Damage the block. Dedup-ditto will save us when we read it later.
6159 psize
= BP_GET_PSIZE(&blk
);
6160 abd
= abd_alloc_linear(psize
, B_TRUE
);
6161 ztest_pattern_set(abd_to_buf(abd
), psize
, ~pattern
);
6163 (void) zio_wait(zio_rewrite(NULL
, spa
, 0, &blk
,
6164 abd
, psize
, NULL
, NULL
, ZIO_PRIORITY_SYNC_WRITE
,
6165 ZIO_FLAG_CANFAIL
| ZIO_FLAG_INDUCE_DAMAGE
, NULL
));
6169 (void) pthread_rwlock_unlock(&ztest_name_lock
);
6170 umem_free(od
, sizeof (ztest_od_t
));
6178 ztest_scrub(ztest_ds_t
*zd
, uint64_t id
)
6180 spa_t
*spa
= ztest_spa
;
6183 * Scrub in progress by device removal.
6185 if (ztest_device_removal_active
)
6188 (void) spa_scan(spa
, POOL_SCAN_SCRUB
);
6189 (void) poll(NULL
, 0, 100); /* wait a moment, then force a restart */
6190 (void) spa_scan(spa
, POOL_SCAN_SCRUB
);
6194 * Change the guid for the pool.
6198 ztest_reguid(ztest_ds_t
*zd
, uint64_t id
)
6200 spa_t
*spa
= ztest_spa
;
6201 uint64_t orig
, load
;
6204 if (ztest_opts
.zo_mmp_test
)
6207 orig
= spa_guid(spa
);
6208 load
= spa_load_guid(spa
);
6210 (void) pthread_rwlock_wrlock(&ztest_name_lock
);
6211 error
= spa_change_guid(spa
);
6212 (void) pthread_rwlock_unlock(&ztest_name_lock
);
6217 if (ztest_opts
.zo_verbose
>= 4) {
6218 (void) printf("Changed guid old %llu -> %llu\n",
6219 (u_longlong_t
)orig
, (u_longlong_t
)spa_guid(spa
));
6222 VERIFY3U(orig
, !=, spa_guid(spa
));
6223 VERIFY3U(load
, ==, spa_load_guid(spa
));
6227 * Rename the pool to a different name and then rename it back.
6231 ztest_spa_rename(ztest_ds_t
*zd
, uint64_t id
)
6233 char *oldname
, *newname
;
6236 if (ztest_opts
.zo_mmp_test
)
6239 (void) pthread_rwlock_wrlock(&ztest_name_lock
);
6241 oldname
= ztest_opts
.zo_pool
;
6242 newname
= umem_alloc(strlen(oldname
) + 5, UMEM_NOFAIL
);
6243 (void) strcpy(newname
, oldname
);
6244 (void) strcat(newname
, "_tmp");
6249 VERIFY3U(0, ==, spa_rename(oldname
, newname
));
6252 * Try to open it under the old name, which shouldn't exist
6254 VERIFY3U(ENOENT
, ==, spa_open(oldname
, &spa
, FTAG
));
6257 * Open it under the new name and make sure it's still the same spa_t.
6259 VERIFY3U(0, ==, spa_open(newname
, &spa
, FTAG
));
6261 ASSERT(spa
== ztest_spa
);
6262 spa_close(spa
, FTAG
);
6265 * Rename it back to the original
6267 VERIFY3U(0, ==, spa_rename(newname
, oldname
));
6270 * Make sure it can still be opened
6272 VERIFY3U(0, ==, spa_open(oldname
, &spa
, FTAG
));
6274 ASSERT(spa
== ztest_spa
);
6275 spa_close(spa
, FTAG
);
6277 umem_free(newname
, strlen(newname
) + 1);
6279 (void) pthread_rwlock_unlock(&ztest_name_lock
);
6283 ztest_fletcher(ztest_ds_t
*zd
, uint64_t id
)
6285 hrtime_t end
= gethrtime() + NANOSEC
;
6287 while (gethrtime() <= end
) {
6288 int run_count
= 100;
6290 struct abd
*abd_data
, *abd_meta
;
6295 zio_cksum_t zc_ref_byteswap
;
6297 size
= ztest_random_blocksize();
6299 buf
= umem_alloc(size
, UMEM_NOFAIL
);
6300 abd_data
= abd_alloc(size
, B_FALSE
);
6301 abd_meta
= abd_alloc(size
, B_TRUE
);
6303 for (i
= 0, ptr
= buf
; i
< size
/ sizeof (*ptr
); i
++, ptr
++)
6304 *ptr
= ztest_random(UINT_MAX
);
6306 abd_copy_from_buf_off(abd_data
, buf
, 0, size
);
6307 abd_copy_from_buf_off(abd_meta
, buf
, 0, size
);
6309 VERIFY0(fletcher_4_impl_set("scalar"));
6310 fletcher_4_native(buf
, size
, NULL
, &zc_ref
);
6311 fletcher_4_byteswap(buf
, size
, NULL
, &zc_ref_byteswap
);
6313 VERIFY0(fletcher_4_impl_set("cycle"));
6314 while (run_count
-- > 0) {
6316 zio_cksum_t zc_byteswap
;
6318 fletcher_4_byteswap(buf
, size
, NULL
, &zc_byteswap
);
6319 fletcher_4_native(buf
, size
, NULL
, &zc
);
6321 VERIFY0(bcmp(&zc
, &zc_ref
, sizeof (zc
)));
6322 VERIFY0(bcmp(&zc_byteswap
, &zc_ref_byteswap
,
6323 sizeof (zc_byteswap
)));
6325 /* Test ABD - data */
6326 abd_fletcher_4_byteswap(abd_data
, size
, NULL
,
6328 abd_fletcher_4_native(abd_data
, size
, NULL
, &zc
);
6330 VERIFY0(bcmp(&zc
, &zc_ref
, sizeof (zc
)));
6331 VERIFY0(bcmp(&zc_byteswap
, &zc_ref_byteswap
,
6332 sizeof (zc_byteswap
)));
6334 /* Test ABD - metadata */
6335 abd_fletcher_4_byteswap(abd_meta
, size
, NULL
,
6337 abd_fletcher_4_native(abd_meta
, size
, NULL
, &zc
);
6339 VERIFY0(bcmp(&zc
, &zc_ref
, sizeof (zc
)));
6340 VERIFY0(bcmp(&zc_byteswap
, &zc_ref_byteswap
,
6341 sizeof (zc_byteswap
)));
6345 umem_free(buf
, size
);
6352 ztest_fletcher_incr(ztest_ds_t
*zd
, uint64_t id
)
6359 zio_cksum_t zc_ref_bswap
;
6361 hrtime_t end
= gethrtime() + NANOSEC
;
6363 while (gethrtime() <= end
) {
6364 int run_count
= 100;
6366 size
= ztest_random_blocksize();
6367 buf
= umem_alloc(size
, UMEM_NOFAIL
);
6369 for (i
= 0, ptr
= buf
; i
< size
/ sizeof (*ptr
); i
++, ptr
++)
6370 *ptr
= ztest_random(UINT_MAX
);
6372 VERIFY0(fletcher_4_impl_set("scalar"));
6373 fletcher_4_native(buf
, size
, NULL
, &zc_ref
);
6374 fletcher_4_byteswap(buf
, size
, NULL
, &zc_ref_bswap
);
6376 VERIFY0(fletcher_4_impl_set("cycle"));
6378 while (run_count
-- > 0) {
6380 zio_cksum_t zc_bswap
;
6383 ZIO_SET_CHECKSUM(&zc
, 0, 0, 0, 0);
6384 ZIO_SET_CHECKSUM(&zc_bswap
, 0, 0, 0, 0);
6386 while (pos
< size
) {
6387 size_t inc
= 64 * ztest_random(size
/ 67);
6388 /* sometimes add few bytes to test non-simd */
6389 if (ztest_random(100) < 10)
6390 inc
+= P2ALIGN(ztest_random(64),
6393 if (inc
> (size
- pos
))
6396 fletcher_4_incremental_native(buf
+ pos
, inc
,
6398 fletcher_4_incremental_byteswap(buf
+ pos
, inc
,
6404 VERIFY3U(pos
, ==, size
);
6406 VERIFY(ZIO_CHECKSUM_EQUAL(zc
, zc_ref
));
6407 VERIFY(ZIO_CHECKSUM_EQUAL(zc_bswap
, zc_ref_bswap
));
6410 * verify if incremental on the whole buffer is
6411 * equivalent to non-incremental version
6413 ZIO_SET_CHECKSUM(&zc
, 0, 0, 0, 0);
6414 ZIO_SET_CHECKSUM(&zc_bswap
, 0, 0, 0, 0);
6416 fletcher_4_incremental_native(buf
, size
, &zc
);
6417 fletcher_4_incremental_byteswap(buf
, size
, &zc_bswap
);
6419 VERIFY(ZIO_CHECKSUM_EQUAL(zc
, zc_ref
));
6420 VERIFY(ZIO_CHECKSUM_EQUAL(zc_bswap
, zc_ref_bswap
));
6423 umem_free(buf
, size
);
6428 ztest_check_path(char *path
)
6431 /* return true on success */
6432 return (!stat(path
, &s
));
6436 ztest_get_zdb_bin(char *bin
, int len
)
6440 * Try to use ZDB_PATH and in-tree zdb path. If not successful, just
6441 * let popen to search through PATH.
6443 if ((zdb_path
= getenv("ZDB_PATH"))) {
6444 strlcpy(bin
, zdb_path
, len
); /* In env */
6445 if (!ztest_check_path(bin
)) {
6446 ztest_dump_core
= 0;
6447 fatal(1, "invalid ZDB_PATH '%s'", bin
);
6452 VERIFY(realpath(getexecname(), bin
) != NULL
);
6453 if (strstr(bin
, "/ztest/")) {
6454 strstr(bin
, "/ztest/")[0] = '\0'; /* In-tree */
6455 strcat(bin
, "/zdb/zdb");
6456 if (ztest_check_path(bin
))
6463 * Verify pool integrity by running zdb.
6466 ztest_run_zdb(char *pool
)
6472 const int len
= MAXPATHLEN
+ MAXNAMELEN
+ 20;
6475 bin
= umem_alloc(len
, UMEM_NOFAIL
);
6476 zdb
= umem_alloc(len
, UMEM_NOFAIL
);
6477 zbuf
= umem_alloc(1024, UMEM_NOFAIL
);
6479 ztest_get_zdb_bin(bin
, len
);
6482 "%s -bcc%s%s -G -d -U %s "
6483 "-o zfs_reconstruct_indirect_combinations_max=65536 %s",
6485 ztest_opts
.zo_verbose
>= 3 ? "s" : "",
6486 ztest_opts
.zo_verbose
>= 4 ? "v" : "",
6490 if (ztest_opts
.zo_verbose
>= 5)
6491 (void) printf("Executing %s\n", strstr(zdb
, "zdb "));
6493 fp
= popen(zdb
, "r");
6495 while (fgets(zbuf
, 1024, fp
) != NULL
)
6496 if (ztest_opts
.zo_verbose
>= 3)
6497 (void) printf("%s", zbuf
);
6499 status
= pclose(fp
);
6504 ztest_dump_core
= 0;
6505 if (WIFEXITED(status
))
6506 fatal(0, "'%s' exit code %d", zdb
, WEXITSTATUS(status
));
6508 fatal(0, "'%s' died with signal %d", zdb
, WTERMSIG(status
));
6510 umem_free(bin
, len
);
6511 umem_free(zdb
, len
);
6512 umem_free(zbuf
, 1024);
6516 ztest_walk_pool_directory(char *header
)
6520 if (ztest_opts
.zo_verbose
>= 6)
6521 (void) printf("%s\n", header
);
6523 mutex_enter(&spa_namespace_lock
);
6524 while ((spa
= spa_next(spa
)) != NULL
)
6525 if (ztest_opts
.zo_verbose
>= 6)
6526 (void) printf("\t%s\n", spa_name(spa
));
6527 mutex_exit(&spa_namespace_lock
);
6531 ztest_spa_import_export(char *oldname
, char *newname
)
6533 nvlist_t
*config
, *newconfig
;
6538 if (ztest_opts
.zo_verbose
>= 4) {
6539 (void) printf("import/export: old = %s, new = %s\n",
6544 * Clean up from previous runs.
6546 (void) spa_destroy(newname
);
6549 * Get the pool's configuration and guid.
6551 VERIFY3U(0, ==, spa_open(oldname
, &spa
, FTAG
));
6554 * Kick off a scrub to tickle scrub/export races.
6556 if (ztest_random(2) == 0)
6557 (void) spa_scan(spa
, POOL_SCAN_SCRUB
);
6559 pool_guid
= spa_guid(spa
);
6560 spa_close(spa
, FTAG
);
6562 ztest_walk_pool_directory("pools before export");
6567 VERIFY3U(0, ==, spa_export(oldname
, &config
, B_FALSE
, B_FALSE
));
6569 ztest_walk_pool_directory("pools after export");
6574 newconfig
= spa_tryimport(config
);
6575 ASSERT(newconfig
!= NULL
);
6576 nvlist_free(newconfig
);
6579 * Import it under the new name.
6581 error
= spa_import(newname
, config
, NULL
, 0);
6583 dump_nvlist(config
, 0);
6584 fatal(B_FALSE
, "couldn't import pool %s as %s: error %u",
6585 oldname
, newname
, error
);
6588 ztest_walk_pool_directory("pools after import");
6591 * Try to import it again -- should fail with EEXIST.
6593 VERIFY3U(EEXIST
, ==, spa_import(newname
, config
, NULL
, 0));
6596 * Try to import it under a different name -- should fail with EEXIST.
6598 VERIFY3U(EEXIST
, ==, spa_import(oldname
, config
, NULL
, 0));
6601 * Verify that the pool is no longer visible under the old name.
6603 VERIFY3U(ENOENT
, ==, spa_open(oldname
, &spa
, FTAG
));
6606 * Verify that we can open and close the pool using the new name.
6608 VERIFY3U(0, ==, spa_open(newname
, &spa
, FTAG
));
6609 ASSERT(pool_guid
== spa_guid(spa
));
6610 spa_close(spa
, FTAG
);
6612 nvlist_free(config
);
6616 ztest_resume(spa_t
*spa
)
6618 if (spa_suspended(spa
) && ztest_opts
.zo_verbose
>= 6)
6619 (void) printf("resuming from suspended state\n");
6620 spa_vdev_state_enter(spa
, SCL_NONE
);
6621 vdev_clear(spa
, NULL
);
6622 (void) spa_vdev_state_exit(spa
, NULL
, 0);
6623 (void) zio_resume(spa
);
6627 ztest_resume_thread(void *arg
)
6631 while (!ztest_exiting
) {
6632 if (spa_suspended(spa
))
6634 (void) poll(NULL
, 0, 100);
6637 * Periodically change the zfs_compressed_arc_enabled setting.
6639 if (ztest_random(10) == 0)
6640 zfs_compressed_arc_enabled
= ztest_random(2);
6643 * Periodically change the zfs_abd_scatter_enabled setting.
6645 if (ztest_random(10) == 0)
6646 zfs_abd_scatter_enabled
= ztest_random(2);
6653 ztest_deadman_thread(void *arg
)
6655 ztest_shared_t
*zs
= arg
;
6656 spa_t
*spa
= ztest_spa
;
6657 hrtime_t delta
, overdue
, total
= 0;
6660 delta
= zs
->zs_thread_stop
- zs
->zs_thread_start
+
6661 MSEC2NSEC(zfs_deadman_synctime_ms
);
6663 (void) poll(NULL
, 0, (int)NSEC2MSEC(delta
));
6666 * If the pool is suspended then fail immediately. Otherwise,
6667 * check to see if the pool is making any progress. If
6668 * vdev_deadman() discovers that there hasn't been any recent
6669 * I/Os then it will end up aborting the tests.
6671 if (spa_suspended(spa
) || spa
->spa_root_vdev
== NULL
) {
6672 fatal(0, "aborting test after %llu seconds because "
6673 "pool has transitioned to a suspended state.",
6674 zfs_deadman_synctime_ms
/ 1000);
6676 vdev_deadman(spa
->spa_root_vdev
, FTAG
);
6679 * If the process doesn't complete within a grace period of
6680 * zfs_deadman_synctime_ms over the expected finish time,
6681 * then it may be hung and is terminated.
6683 overdue
= zs
->zs_proc_stop
+ MSEC2NSEC(zfs_deadman_synctime_ms
);
6684 total
+= zfs_deadman_synctime_ms
/ 1000;
6685 if (gethrtime() > overdue
) {
6686 fatal(0, "aborting test after %llu seconds because "
6687 "the process is overdue for termination.", total
);
6690 (void) printf("ztest has been running for %lld seconds\n",
6696 ztest_execute(int test
, ztest_info_t
*zi
, uint64_t id
)
6698 ztest_ds_t
*zd
= &ztest_ds
[id
% ztest_opts
.zo_datasets
];
6699 ztest_shared_callstate_t
*zc
= ZTEST_GET_SHARED_CALLSTATE(test
);
6700 hrtime_t functime
= gethrtime();
6703 for (i
= 0; i
< zi
->zi_iters
; i
++)
6704 zi
->zi_func(zd
, id
);
6706 functime
= gethrtime() - functime
;
6708 atomic_add_64(&zc
->zc_count
, 1);
6709 atomic_add_64(&zc
->zc_time
, functime
);
6711 if (ztest_opts
.zo_verbose
>= 4)
6712 (void) printf("%6.2f sec in %s\n",
6713 (double)functime
/ NANOSEC
, zi
->zi_funcname
);
6717 ztest_thread(void *arg
)
6720 uint64_t id
= (uintptr_t)arg
;
6721 ztest_shared_t
*zs
= ztest_shared
;
6725 ztest_shared_callstate_t
*zc
;
6727 while ((now
= gethrtime()) < zs
->zs_thread_stop
) {
6729 * See if it's time to force a crash.
6731 if (now
> zs
->zs_thread_kill
)
6735 * If we're getting ENOSPC with some regularity, stop.
6737 if (zs
->zs_enospc_count
> 10)
6741 * Pick a random function to execute.
6743 rand
= ztest_random(ZTEST_FUNCS
);
6744 zi
= &ztest_info
[rand
];
6745 zc
= ZTEST_GET_SHARED_CALLSTATE(rand
);
6746 call_next
= zc
->zc_next
;
6748 if (now
>= call_next
&&
6749 atomic_cas_64(&zc
->zc_next
, call_next
, call_next
+
6750 ztest_random(2 * zi
->zi_interval
[0] + 1)) == call_next
) {
6751 ztest_execute(rand
, zi
, id
);
6759 ztest_dataset_name(char *dsname
, char *pool
, int d
)
6761 (void) snprintf(dsname
, ZFS_MAX_DATASET_NAME_LEN
, "%s/ds_%d", pool
, d
);
6765 ztest_dataset_destroy(int d
)
6767 char name
[ZFS_MAX_DATASET_NAME_LEN
];
6770 ztest_dataset_name(name
, ztest_opts
.zo_pool
, d
);
6772 if (ztest_opts
.zo_verbose
>= 3)
6773 (void) printf("Destroying %s to free up space\n", name
);
6776 * Cleanup any non-standard clones and snapshots. In general,
6777 * ztest thread t operates on dataset (t % zopt_datasets),
6778 * so there may be more than one thing to clean up.
6780 for (t
= d
; t
< ztest_opts
.zo_threads
;
6781 t
+= ztest_opts
.zo_datasets
)
6782 ztest_dsl_dataset_cleanup(name
, t
);
6784 (void) dmu_objset_find(name
, ztest_objset_destroy_cb
, NULL
,
6785 DS_FIND_SNAPSHOTS
| DS_FIND_CHILDREN
);
6789 ztest_dataset_dirobj_verify(ztest_ds_t
*zd
)
6791 uint64_t usedobjs
, dirobjs
, scratch
;
6794 * ZTEST_DIROBJ is the object directory for the entire dataset.
6795 * Therefore, the number of objects in use should equal the
6796 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
6797 * If not, we have an object leak.
6799 * Note that we can only check this in ztest_dataset_open(),
6800 * when the open-context and syncing-context values agree.
6801 * That's because zap_count() returns the open-context value,
6802 * while dmu_objset_space() returns the rootbp fill count.
6804 VERIFY3U(0, ==, zap_count(zd
->zd_os
, ZTEST_DIROBJ
, &dirobjs
));
6805 dmu_objset_space(zd
->zd_os
, &scratch
, &scratch
, &usedobjs
, &scratch
);
6806 ASSERT3U(dirobjs
+ 1, ==, usedobjs
);
6810 ztest_dataset_open(int d
)
6812 ztest_ds_t
*zd
= &ztest_ds
[d
];
6813 uint64_t committed_seq
= ZTEST_GET_SHARED_DS(d
)->zd_seq
;
6816 char name
[ZFS_MAX_DATASET_NAME_LEN
];
6819 ztest_dataset_name(name
, ztest_opts
.zo_pool
, d
);
6821 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
6823 error
= ztest_dataset_create(name
);
6824 if (error
== ENOSPC
) {
6825 (void) pthread_rwlock_unlock(&ztest_name_lock
);
6826 ztest_record_enospc(FTAG
);
6829 ASSERT(error
== 0 || error
== EEXIST
);
6831 VERIFY0(ztest_dmu_objset_own(name
, DMU_OST_OTHER
, B_FALSE
,
6833 (void) pthread_rwlock_unlock(&ztest_name_lock
);
6835 ztest_zd_init(zd
, ZTEST_GET_SHARED_DS(d
), os
);
6837 zilog
= zd
->zd_zilog
;
6839 if (zilog
->zl_header
->zh_claim_lr_seq
!= 0 &&
6840 zilog
->zl_header
->zh_claim_lr_seq
< committed_seq
)
6841 fatal(0, "missing log records: claimed %llu < committed %llu",
6842 zilog
->zl_header
->zh_claim_lr_seq
, committed_seq
);
6844 ztest_dataset_dirobj_verify(zd
);
6846 zil_replay(os
, zd
, ztest_replay_vector
);
6848 ztest_dataset_dirobj_verify(zd
);
6850 if (ztest_opts
.zo_verbose
>= 6)
6851 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
6853 (u_longlong_t
)zilog
->zl_parse_blk_count
,
6854 (u_longlong_t
)zilog
->zl_parse_lr_count
,
6855 (u_longlong_t
)zilog
->zl_replaying_seq
);
6857 zilog
= zil_open(os
, ztest_get_data
);
6859 if (zilog
->zl_replaying_seq
!= 0 &&
6860 zilog
->zl_replaying_seq
< committed_seq
)
6861 fatal(0, "missing log records: replayed %llu < committed %llu",
6862 zilog
->zl_replaying_seq
, committed_seq
);
6868 ztest_dataset_close(int d
)
6870 ztest_ds_t
*zd
= &ztest_ds
[d
];
6872 zil_close(zd
->zd_zilog
);
6873 txg_wait_synced(spa_get_dsl(zd
->zd_os
->os_spa
), 0);
6874 dmu_objset_disown(zd
->zd_os
, B_TRUE
, zd
);
6880 * Kick off threads to run tests on all datasets in parallel.
6883 ztest_run(ztest_shared_t
*zs
)
6887 kthread_t
*resume_thread
;
6888 kthread_t
**run_threads
;
6893 ztest_exiting
= B_FALSE
;
6896 * Initialize parent/child shared state.
6898 mutex_init(&ztest_vdev_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
6899 mutex_init(&ztest_checkpoint_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
6900 VERIFY0(pthread_rwlock_init(&ztest_name_lock
, NULL
));
6902 zs
->zs_thread_start
= gethrtime();
6903 zs
->zs_thread_stop
=
6904 zs
->zs_thread_start
+ ztest_opts
.zo_passtime
* NANOSEC
;
6905 zs
->zs_thread_stop
= MIN(zs
->zs_thread_stop
, zs
->zs_proc_stop
);
6906 zs
->zs_thread_kill
= zs
->zs_thread_stop
;
6907 if (ztest_random(100) < ztest_opts
.zo_killrate
) {
6908 zs
->zs_thread_kill
-=
6909 ztest_random(ztest_opts
.zo_passtime
* NANOSEC
);
6912 mutex_init(&zcl
.zcl_callbacks_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
6914 list_create(&zcl
.zcl_callbacks
, sizeof (ztest_cb_data_t
),
6915 offsetof(ztest_cb_data_t
, zcd_node
));
6920 kernel_init(FREAD
| FWRITE
);
6921 VERIFY0(spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
));
6922 metaslab_preload_limit
= ztest_random(20) + 1;
6925 dmu_objset_stats_t dds
;
6926 VERIFY0(ztest_dmu_objset_own(ztest_opts
.zo_pool
,
6927 DMU_OST_ANY
, B_TRUE
, B_TRUE
, FTAG
, &os
));
6928 dsl_pool_config_enter(dmu_objset_pool(os
), FTAG
);
6929 dmu_objset_fast_stat(os
, &dds
);
6930 dsl_pool_config_exit(dmu_objset_pool(os
), FTAG
);
6931 zs
->zs_guid
= dds
.dds_guid
;
6932 dmu_objset_disown(os
, B_TRUE
, FTAG
);
6934 spa
->spa_dedup_ditto
= 2 * ZIO_DEDUPDITTO_MIN
;
6937 * Create a thread to periodically resume suspended I/O.
6939 resume_thread
= thread_create(NULL
, 0, ztest_resume_thread
,
6940 spa
, 0, NULL
, TS_RUN
| TS_JOINABLE
, defclsyspri
);
6943 * Create a deadman thread and set to panic if we hang.
6945 (void) thread_create(NULL
, 0, ztest_deadman_thread
,
6946 zs
, 0, NULL
, TS_RUN
| TS_JOINABLE
, defclsyspri
);
6948 spa
->spa_deadman_failmode
= ZIO_FAILURE_MODE_PANIC
;
6951 * Verify that we can safely inquire about any object,
6952 * whether it's allocated or not. To make it interesting,
6953 * we probe a 5-wide window around each power of two.
6954 * This hits all edge cases, including zero and the max.
6956 for (t
= 0; t
< 64; t
++) {
6957 for (d
= -5; d
<= 5; d
++) {
6958 error
= dmu_object_info(spa
->spa_meta_objset
,
6959 (1ULL << t
) + d
, NULL
);
6960 ASSERT(error
== 0 || error
== ENOENT
||
6966 * If we got any ENOSPC errors on the previous run, destroy something.
6968 if (zs
->zs_enospc_count
!= 0) {
6969 int d
= ztest_random(ztest_opts
.zo_datasets
);
6970 ztest_dataset_destroy(d
);
6972 zs
->zs_enospc_count
= 0;
6974 run_threads
= umem_zalloc(ztest_opts
.zo_threads
* sizeof (kthread_t
*),
6977 if (ztest_opts
.zo_verbose
>= 4)
6978 (void) printf("starting main threads...\n");
6981 * Kick off all the tests that run in parallel.
6983 for (t
= 0; t
< ztest_opts
.zo_threads
; t
++) {
6984 if (t
< ztest_opts
.zo_datasets
&& ztest_dataset_open(t
) != 0) {
6985 umem_free(run_threads
, ztest_opts
.zo_threads
*
6986 sizeof (kthread_t
*));
6990 run_threads
[t
] = thread_create(NULL
, 0, ztest_thread
,
6991 (void *)(uintptr_t)t
, 0, NULL
, TS_RUN
| TS_JOINABLE
,
6996 * Wait for all of the tests to complete. We go in reverse order
6997 * so we don't close datasets while threads are still using them.
6999 for (t
= ztest_opts
.zo_threads
- 1; t
>= 0; t
--) {
7000 VERIFY0(thread_join(run_threads
[t
]));
7001 if (t
< ztest_opts
.zo_datasets
)
7002 ztest_dataset_close(t
);
7005 txg_wait_synced(spa_get_dsl(spa
), 0);
7007 zs
->zs_alloc
= metaslab_class_get_alloc(spa_normal_class(spa
));
7008 zs
->zs_space
= metaslab_class_get_space(spa_normal_class(spa
));
7010 umem_free(run_threads
, ztest_opts
.zo_threads
* sizeof (kthread_t
*));
7012 /* Kill the resume thread */
7013 ztest_exiting
= B_TRUE
;
7014 VERIFY0(thread_join(resume_thread
));
7018 * Right before closing the pool, kick off a bunch of async I/O;
7019 * spa_close() should wait for it to complete.
7021 for (object
= 1; object
< 50; object
++) {
7022 dmu_prefetch(spa
->spa_meta_objset
, object
, 0, 0, 1ULL << 20,
7023 ZIO_PRIORITY_SYNC_READ
);
7026 /* Verify that at least one commit cb was called in a timely fashion */
7027 if (zc_cb_counter
>= ZTEST_COMMIT_CB_MIN_REG
)
7028 VERIFY0(zc_min_txg_delay
);
7030 spa_close(spa
, FTAG
);
7033 * Verify that we can loop over all pools.
7035 mutex_enter(&spa_namespace_lock
);
7036 for (spa
= spa_next(NULL
); spa
!= NULL
; spa
= spa_next(spa
))
7037 if (ztest_opts
.zo_verbose
> 3)
7038 (void) printf("spa_next: found %s\n", spa_name(spa
));
7039 mutex_exit(&spa_namespace_lock
);
7042 * Verify that we can export the pool and reimport it under a
7045 if ((ztest_random(2) == 0) && !ztest_opts
.zo_mmp_test
) {
7046 char name
[ZFS_MAX_DATASET_NAME_LEN
];
7047 (void) snprintf(name
, sizeof (name
), "%s_import",
7048 ztest_opts
.zo_pool
);
7049 ztest_spa_import_export(ztest_opts
.zo_pool
, name
);
7050 ztest_spa_import_export(name
, ztest_opts
.zo_pool
);
7055 list_destroy(&zcl
.zcl_callbacks
);
7056 mutex_destroy(&zcl
.zcl_callbacks_lock
);
7057 (void) pthread_rwlock_destroy(&ztest_name_lock
);
7058 mutex_destroy(&ztest_vdev_lock
);
7059 mutex_destroy(&ztest_checkpoint_lock
);
7065 ztest_ds_t
*zd
= &ztest_ds
[0];
7069 if (ztest_opts
.zo_verbose
>= 3)
7070 (void) printf("testing spa_freeze()...\n");
7072 kernel_init(FREAD
| FWRITE
);
7073 VERIFY3U(0, ==, spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
));
7074 VERIFY3U(0, ==, ztest_dataset_open(0));
7078 * Force the first log block to be transactionally allocated.
7079 * We have to do this before we freeze the pool -- otherwise
7080 * the log chain won't be anchored.
7082 while (BP_IS_HOLE(&zd
->zd_zilog
->zl_header
->zh_log
)) {
7083 ztest_dmu_object_alloc_free(zd
, 0);
7084 zil_commit(zd
->zd_zilog
, 0);
7087 txg_wait_synced(spa_get_dsl(spa
), 0);
7090 * Freeze the pool. This stops spa_sync() from doing anything,
7091 * so that the only way to record changes from now on is the ZIL.
7096 * Because it is hard to predict how much space a write will actually
7097 * require beforehand, we leave ourselves some fudge space to write over
7100 uint64_t capacity
= metaslab_class_get_space(spa_normal_class(spa
)) / 2;
7103 * Run tests that generate log records but don't alter the pool config
7104 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
7105 * We do a txg_wait_synced() after each iteration to force the txg
7106 * to increase well beyond the last synced value in the uberblock.
7107 * The ZIL should be OK with that.
7109 * Run a random number of times less than zo_maxloops and ensure we do
7110 * not run out of space on the pool.
7112 while (ztest_random(10) != 0 &&
7113 numloops
++ < ztest_opts
.zo_maxloops
&&
7114 metaslab_class_get_alloc(spa_normal_class(spa
)) < capacity
) {
7116 ztest_od_init(&od
, 0, FTAG
, 0, DMU_OT_UINT64_OTHER
, 0, 0, 0);
7117 VERIFY0(ztest_object_init(zd
, &od
, sizeof (od
), B_FALSE
));
7118 ztest_io(zd
, od
.od_object
,
7119 ztest_random(ZTEST_RANGE_LOCKS
) << SPA_MAXBLOCKSHIFT
);
7120 txg_wait_synced(spa_get_dsl(spa
), 0);
7124 * Commit all of the changes we just generated.
7126 zil_commit(zd
->zd_zilog
, 0);
7127 txg_wait_synced(spa_get_dsl(spa
), 0);
7130 * Close our dataset and close the pool.
7132 ztest_dataset_close(0);
7133 spa_close(spa
, FTAG
);
7137 * Open and close the pool and dataset to induce log replay.
7139 kernel_init(FREAD
| FWRITE
);
7140 VERIFY3U(0, ==, spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
));
7141 ASSERT(spa_freeze_txg(spa
) == UINT64_MAX
);
7142 VERIFY3U(0, ==, ztest_dataset_open(0));
7144 txg_wait_synced(spa_get_dsl(spa
), 0);
7145 ztest_dataset_close(0);
7146 ztest_reguid(NULL
, 0);
7148 spa_close(spa
, FTAG
);
7153 print_time(hrtime_t t
, char *timebuf
)
7155 hrtime_t s
= t
/ NANOSEC
;
7156 hrtime_t m
= s
/ 60;
7157 hrtime_t h
= m
/ 60;
7158 hrtime_t d
= h
/ 24;
7167 (void) sprintf(timebuf
,
7168 "%llud%02lluh%02llum%02llus", d
, h
, m
, s
);
7170 (void) sprintf(timebuf
, "%lluh%02llum%02llus", h
, m
, s
);
7172 (void) sprintf(timebuf
, "%llum%02llus", m
, s
);
7174 (void) sprintf(timebuf
, "%llus", s
);
7178 make_random_props(void)
7182 VERIFY0(nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0));
7184 if (ztest_random(2) == 0)
7187 VERIFY0(nvlist_add_uint64(props
,
7188 zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE
), 1));
7194 * Import a storage pool with the given name.
7197 ztest_import(ztest_shared_t
*zs
)
7199 libzfs_handle_t
*hdl
;
7200 importargs_t args
= { 0 };
7202 nvlist_t
*cfg
= NULL
;
7204 char *searchdirs
[nsearch
];
7205 char *name
= ztest_opts
.zo_pool
;
7206 int flags
= ZFS_IMPORT_MISSING_LOG
;
7209 mutex_init(&ztest_vdev_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
7210 mutex_init(&ztest_checkpoint_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
7211 VERIFY0(pthread_rwlock_init(&ztest_name_lock
, NULL
));
7213 kernel_init(FREAD
| FWRITE
);
7214 hdl
= libzfs_init();
7216 searchdirs
[0] = ztest_opts
.zo_dir
;
7217 args
.paths
= nsearch
;
7218 args
.path
= searchdirs
;
7219 args
.can_be_active
= B_FALSE
;
7221 error
= zpool_tryimport(hdl
, name
, &cfg
, &args
);
7223 (void) fatal(0, "No pools found\n");
7225 VERIFY0(spa_import(name
, cfg
, NULL
, flags
));
7226 VERIFY0(spa_open(name
, &spa
, FTAG
));
7227 zs
->zs_metaslab_sz
=
7228 1ULL << spa
->spa_root_vdev
->vdev_child
[0]->vdev_ms_shift
;
7229 spa_close(spa
, FTAG
);
7234 if (!ztest_opts
.zo_mmp_test
) {
7235 ztest_run_zdb(ztest_opts
.zo_pool
);
7237 ztest_run_zdb(ztest_opts
.zo_pool
);
7240 (void) pthread_rwlock_destroy(&ztest_name_lock
);
7241 mutex_destroy(&ztest_vdev_lock
);
7242 mutex_destroy(&ztest_checkpoint_lock
);
7246 * Create a storage pool with the given name and initial vdev size.
7247 * Then test spa_freeze() functionality.
7250 ztest_init(ztest_shared_t
*zs
)
7253 nvlist_t
*nvroot
, *props
;
7256 mutex_init(&ztest_vdev_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
7257 mutex_init(&ztest_checkpoint_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
7258 VERIFY0(pthread_rwlock_init(&ztest_name_lock
, NULL
));
7260 kernel_init(FREAD
| FWRITE
);
7263 * Create the storage pool.
7265 (void) spa_destroy(ztest_opts
.zo_pool
);
7266 ztest_shared
->zs_vdev_next_leaf
= 0;
7268 zs
->zs_mirrors
= ztest_opts
.zo_mirrors
;
7269 nvroot
= make_vdev_root(NULL
, NULL
, NULL
, ztest_opts
.zo_vdev_size
, 0,
7270 NULL
, ztest_opts
.zo_raidz
, zs
->zs_mirrors
, 1);
7271 props
= make_random_props();
7274 * We don't expect the pool to suspend unless maxfaults == 0,
7275 * in which case ztest_fault_inject() temporarily takes away
7276 * the only valid replica.
7278 VERIFY0(nvlist_add_uint64(props
,
7279 zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE
),
7280 MAXFAULTS(zs
) ? ZIO_FAILURE_MODE_PANIC
: ZIO_FAILURE_MODE_WAIT
));
7282 for (i
= 0; i
< SPA_FEATURES
; i
++) {
7284 VERIFY3S(-1, !=, asprintf(&buf
, "feature@%s",
7285 spa_feature_table
[i
].fi_uname
));
7286 VERIFY3U(0, ==, nvlist_add_uint64(props
, buf
, 0));
7290 VERIFY0(spa_create(ztest_opts
.zo_pool
, nvroot
, props
, NULL
, NULL
));
7291 nvlist_free(nvroot
);
7294 VERIFY3U(0, ==, spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
));
7295 zs
->zs_metaslab_sz
=
7296 1ULL << spa
->spa_root_vdev
->vdev_child
[0]->vdev_ms_shift
;
7297 spa_close(spa
, FTAG
);
7301 if (!ztest_opts
.zo_mmp_test
) {
7302 ztest_run_zdb(ztest_opts
.zo_pool
);
7304 ztest_run_zdb(ztest_opts
.zo_pool
);
7307 (void) pthread_rwlock_destroy(&ztest_name_lock
);
7308 mutex_destroy(&ztest_vdev_lock
);
7309 mutex_destroy(&ztest_checkpoint_lock
);
7315 static char ztest_name_data
[] = "/tmp/ztest.data.XXXXXX";
7317 ztest_fd_data
= mkstemp(ztest_name_data
);
7318 ASSERT3S(ztest_fd_data
, >=, 0);
7319 (void) unlink(ztest_name_data
);
7323 shared_data_size(ztest_shared_hdr_t
*hdr
)
7327 size
= hdr
->zh_hdr_size
;
7328 size
+= hdr
->zh_opts_size
;
7329 size
+= hdr
->zh_size
;
7330 size
+= hdr
->zh_stats_size
* hdr
->zh_stats_count
;
7331 size
+= hdr
->zh_ds_size
* hdr
->zh_ds_count
;
7340 ztest_shared_hdr_t
*hdr
;
7342 hdr
= (void *)mmap(0, P2ROUNDUP(sizeof (*hdr
), getpagesize()),
7343 PROT_READ
| PROT_WRITE
, MAP_SHARED
, ztest_fd_data
, 0);
7344 ASSERT(hdr
!= MAP_FAILED
);
7346 VERIFY3U(0, ==, ftruncate(ztest_fd_data
, sizeof (ztest_shared_hdr_t
)));
7348 hdr
->zh_hdr_size
= sizeof (ztest_shared_hdr_t
);
7349 hdr
->zh_opts_size
= sizeof (ztest_shared_opts_t
);
7350 hdr
->zh_size
= sizeof (ztest_shared_t
);
7351 hdr
->zh_stats_size
= sizeof (ztest_shared_callstate_t
);
7352 hdr
->zh_stats_count
= ZTEST_FUNCS
;
7353 hdr
->zh_ds_size
= sizeof (ztest_shared_ds_t
);
7354 hdr
->zh_ds_count
= ztest_opts
.zo_datasets
;
7356 size
= shared_data_size(hdr
);
7357 VERIFY3U(0, ==, ftruncate(ztest_fd_data
, size
));
7359 (void) munmap((caddr_t
)hdr
, P2ROUNDUP(sizeof (*hdr
), getpagesize()));
7366 ztest_shared_hdr_t
*hdr
;
7369 hdr
= (void *)mmap(0, P2ROUNDUP(sizeof (*hdr
), getpagesize()),
7370 PROT_READ
, MAP_SHARED
, ztest_fd_data
, 0);
7371 ASSERT(hdr
!= MAP_FAILED
);
7373 size
= shared_data_size(hdr
);
7375 (void) munmap((caddr_t
)hdr
, P2ROUNDUP(sizeof (*hdr
), getpagesize()));
7376 hdr
= ztest_shared_hdr
= (void *)mmap(0, P2ROUNDUP(size
, getpagesize()),
7377 PROT_READ
| PROT_WRITE
, MAP_SHARED
, ztest_fd_data
, 0);
7378 ASSERT(hdr
!= MAP_FAILED
);
7379 buf
= (uint8_t *)hdr
;
7381 offset
= hdr
->zh_hdr_size
;
7382 ztest_shared_opts
= (void *)&buf
[offset
];
7383 offset
+= hdr
->zh_opts_size
;
7384 ztest_shared
= (void *)&buf
[offset
];
7385 offset
+= hdr
->zh_size
;
7386 ztest_shared_callstate
= (void *)&buf
[offset
];
7387 offset
+= hdr
->zh_stats_size
* hdr
->zh_stats_count
;
7388 ztest_shared_ds
= (void *)&buf
[offset
];
7392 exec_child(char *cmd
, char *libpath
, boolean_t ignorekill
, int *statusp
)
7396 char *cmdbuf
= NULL
;
7401 cmdbuf
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
7402 (void) strlcpy(cmdbuf
, getexecname(), MAXPATHLEN
);
7407 fatal(1, "fork failed");
7409 if (pid
== 0) { /* child */
7410 char *emptyargv
[2] = { cmd
, NULL
};
7411 char fd_data_str
[12];
7413 struct rlimit rl
= { 1024, 1024 };
7414 (void) setrlimit(RLIMIT_NOFILE
, &rl
);
7416 (void) close(ztest_fd_rand
);
7417 VERIFY(11 >= snprintf(fd_data_str
, 12, "%d", ztest_fd_data
));
7418 VERIFY(0 == setenv("ZTEST_FD_DATA", fd_data_str
, 1));
7420 (void) enable_extended_FILE_stdio(-1, -1);
7421 if (libpath
!= NULL
)
7422 VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath
, 1));
7423 (void) execv(cmd
, emptyargv
);
7424 ztest_dump_core
= B_FALSE
;
7425 fatal(B_TRUE
, "exec failed: %s", cmd
);
7428 if (cmdbuf
!= NULL
) {
7429 umem_free(cmdbuf
, MAXPATHLEN
);
7433 while (waitpid(pid
, &status
, 0) != pid
)
7435 if (statusp
!= NULL
)
7438 if (WIFEXITED(status
)) {
7439 if (WEXITSTATUS(status
) != 0) {
7440 (void) fprintf(stderr
, "child exited with code %d\n",
7441 WEXITSTATUS(status
));
7445 } else if (WIFSIGNALED(status
)) {
7446 if (!ignorekill
|| WTERMSIG(status
) != SIGKILL
) {
7447 (void) fprintf(stderr
, "child died with signal %d\n",
7453 (void) fprintf(stderr
, "something strange happened to child\n");
7460 ztest_run_init(void)
7464 ztest_shared_t
*zs
= ztest_shared
;
7467 * Blow away any existing copy of zpool.cache
7469 (void) remove(spa_config_path
);
7471 if (ztest_opts
.zo_init
== 0) {
7472 if (ztest_opts
.zo_verbose
>= 1)
7473 (void) printf("Importing pool %s\n",
7474 ztest_opts
.zo_pool
);
7480 * Create and initialize our storage pool.
7482 for (i
= 1; i
<= ztest_opts
.zo_init
; i
++) {
7483 bzero(zs
, sizeof (ztest_shared_t
));
7484 if (ztest_opts
.zo_verbose
>= 3 &&
7485 ztest_opts
.zo_init
!= 1) {
7486 (void) printf("ztest_init(), pass %d\n", i
);
7493 main(int argc
, char **argv
)
7501 ztest_shared_callstate_t
*zc
;
7503 char numbuf
[NN_NUMBUF_SZ
];
7508 char *fd_data_str
= getenv("ZTEST_FD_DATA");
7509 struct sigaction action
;
7511 (void) setvbuf(stdout
, NULL
, _IOLBF
, 0);
7513 dprintf_setup(&argc
, argv
);
7514 zfs_deadman_synctime_ms
= 300000;
7516 * As two-word space map entries may not come up often (especially
7517 * if pool and vdev sizes are small) we want to force at least some
7518 * of them so the feature get tested.
7520 zfs_force_some_double_word_sm_entries
= B_TRUE
;
7523 * Verify that even extensively damaged split blocks with many
7524 * segments can be reconstructed in a reasonable amount of time
7525 * when reconstruction is known to be possible.
7527 zfs_reconstruct_indirect_damage_fraction
= 4;
7529 action
.sa_handler
= sig_handler
;
7530 sigemptyset(&action
.sa_mask
);
7531 action
.sa_flags
= 0;
7533 if (sigaction(SIGSEGV
, &action
, NULL
) < 0) {
7534 (void) fprintf(stderr
, "ztest: cannot catch SIGSEGV: %s.\n",
7539 if (sigaction(SIGABRT
, &action
, NULL
) < 0) {
7540 (void) fprintf(stderr
, "ztest: cannot catch SIGABRT: %s.\n",
7546 * Force random_get_bytes() to use /dev/urandom in order to prevent
7547 * ztest from needlessly depleting the system entropy pool.
7549 random_path
= "/dev/urandom";
7550 ztest_fd_rand
= open(random_path
, O_RDONLY
);
7551 ASSERT3S(ztest_fd_rand
, >=, 0);
7554 process_options(argc
, argv
);
7559 bcopy(&ztest_opts
, ztest_shared_opts
,
7560 sizeof (*ztest_shared_opts
));
7562 ztest_fd_data
= atoi(fd_data_str
);
7564 bcopy(ztest_shared_opts
, &ztest_opts
, sizeof (ztest_opts
));
7566 ASSERT3U(ztest_opts
.zo_datasets
, ==, ztest_shared_hdr
->zh_ds_count
);
7568 /* Override location of zpool.cache */
7569 VERIFY(asprintf((char **)&spa_config_path
, "%s/zpool.cache",
7570 ztest_opts
.zo_dir
) != -1);
7572 ztest_ds
= umem_alloc(ztest_opts
.zo_datasets
* sizeof (ztest_ds_t
),
7577 metaslab_force_ganging
= ztest_opts
.zo_metaslab_force_ganging
;
7578 metaslab_df_alloc_threshold
=
7579 zs
->zs_metaslab_df_alloc_threshold
;
7588 hasalt
= (strlen(ztest_opts
.zo_alt_ztest
) != 0);
7590 if (ztest_opts
.zo_verbose
>= 1) {
7591 (void) printf("%llu vdevs, %d datasets, %d threads,"
7592 " %llu seconds...\n",
7593 (u_longlong_t
)ztest_opts
.zo_vdevs
,
7594 ztest_opts
.zo_datasets
,
7595 ztest_opts
.zo_threads
,
7596 (u_longlong_t
)ztest_opts
.zo_time
);
7599 cmd
= umem_alloc(MAXNAMELEN
, UMEM_NOFAIL
);
7600 (void) strlcpy(cmd
, getexecname(), MAXNAMELEN
);
7602 zs
->zs_do_init
= B_TRUE
;
7603 if (strlen(ztest_opts
.zo_alt_ztest
) != 0) {
7604 if (ztest_opts
.zo_verbose
>= 1) {
7605 (void) printf("Executing older ztest for "
7606 "initialization: %s\n", ztest_opts
.zo_alt_ztest
);
7608 VERIFY(!exec_child(ztest_opts
.zo_alt_ztest
,
7609 ztest_opts
.zo_alt_libpath
, B_FALSE
, NULL
));
7611 VERIFY(!exec_child(NULL
, NULL
, B_FALSE
, NULL
));
7613 zs
->zs_do_init
= B_FALSE
;
7615 zs
->zs_proc_start
= gethrtime();
7616 zs
->zs_proc_stop
= zs
->zs_proc_start
+ ztest_opts
.zo_time
* NANOSEC
;
7618 for (f
= 0; f
< ZTEST_FUNCS
; f
++) {
7619 zi
= &ztest_info
[f
];
7620 zc
= ZTEST_GET_SHARED_CALLSTATE(f
);
7621 if (zs
->zs_proc_start
+ zi
->zi_interval
[0] > zs
->zs_proc_stop
)
7622 zc
->zc_next
= UINT64_MAX
;
7624 zc
->zc_next
= zs
->zs_proc_start
+
7625 ztest_random(2 * zi
->zi_interval
[0] + 1);
7629 * Run the tests in a loop. These tests include fault injection
7630 * to verify that self-healing data works, and forced crashes
7631 * to verify that we never lose on-disk consistency.
7633 while (gethrtime() < zs
->zs_proc_stop
) {
7638 * Initialize the workload counters for each function.
7640 for (f
= 0; f
< ZTEST_FUNCS
; f
++) {
7641 zc
= ZTEST_GET_SHARED_CALLSTATE(f
);
7646 /* Set the allocation switch size */
7647 zs
->zs_metaslab_df_alloc_threshold
=
7648 ztest_random(zs
->zs_metaslab_sz
/ 4) + 1;
7650 if (!hasalt
|| ztest_random(2) == 0) {
7651 if (hasalt
&& ztest_opts
.zo_verbose
>= 1) {
7652 (void) printf("Executing newer ztest: %s\n",
7656 killed
= exec_child(cmd
, NULL
, B_TRUE
, &status
);
7658 if (hasalt
&& ztest_opts
.zo_verbose
>= 1) {
7659 (void) printf("Executing older ztest: %s\n",
7660 ztest_opts
.zo_alt_ztest
);
7663 killed
= exec_child(ztest_opts
.zo_alt_ztest
,
7664 ztest_opts
.zo_alt_libpath
, B_TRUE
, &status
);
7671 if (ztest_opts
.zo_verbose
>= 1) {
7672 hrtime_t now
= gethrtime();
7674 now
= MIN(now
, zs
->zs_proc_stop
);
7675 print_time(zs
->zs_proc_stop
- now
, timebuf
);
7676 nicenum(zs
->zs_space
, numbuf
, sizeof (numbuf
));
7678 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
7679 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
7681 WIFEXITED(status
) ? "Complete" : "SIGKILL",
7682 (u_longlong_t
)zs
->zs_enospc_count
,
7683 100.0 * zs
->zs_alloc
/ zs
->zs_space
,
7685 100.0 * (now
- zs
->zs_proc_start
) /
7686 (ztest_opts
.zo_time
* NANOSEC
), timebuf
);
7689 if (ztest_opts
.zo_verbose
>= 2) {
7690 (void) printf("\nWorkload summary:\n\n");
7691 (void) printf("%7s %9s %s\n",
7692 "Calls", "Time", "Function");
7693 (void) printf("%7s %9s %s\n",
7694 "-----", "----", "--------");
7695 for (f
= 0; f
< ZTEST_FUNCS
; f
++) {
7696 zi
= &ztest_info
[f
];
7697 zc
= ZTEST_GET_SHARED_CALLSTATE(f
);
7698 print_time(zc
->zc_time
, timebuf
);
7699 (void) printf("%7llu %9s %s\n",
7700 (u_longlong_t
)zc
->zc_count
, timebuf
,
7703 (void) printf("\n");
7707 * It's possible that we killed a child during a rename test,
7708 * in which case we'll have a 'ztest_tmp' pool lying around
7709 * instead of 'ztest'. Do a blind rename in case this happened.
7712 if (spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
) == 0) {
7713 spa_close(spa
, FTAG
);
7715 char tmpname
[ZFS_MAX_DATASET_NAME_LEN
];
7717 kernel_init(FREAD
| FWRITE
);
7718 (void) snprintf(tmpname
, sizeof (tmpname
), "%s_tmp",
7719 ztest_opts
.zo_pool
);
7720 (void) spa_rename(tmpname
, ztest_opts
.zo_pool
);
7724 if (!ztest_opts
.zo_mmp_test
)
7725 ztest_run_zdb(ztest_opts
.zo_pool
);
7728 if (ztest_opts
.zo_verbose
>= 1) {
7730 (void) printf("%d runs of older ztest: %s\n", older
,
7731 ztest_opts
.zo_alt_ztest
);
7732 (void) printf("%d runs of newer ztest: %s\n", newer
,
7735 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
7736 kills
, iters
- kills
, (100.0 * kills
) / MAX(1, iters
));
7739 umem_free(cmd
, MAXNAMELEN
);