4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2013 Steven Hartland. All rights reserved.
29 * The objective of this program is to provide a DMU/ZAP/SPA stress test
30 * that runs entirely in userland, is easy to use, and easy to extend.
32 * The overall design of the ztest program is as follows:
34 * (1) For each major functional area (e.g. adding vdevs to a pool,
35 * creating and destroying datasets, reading and writing objects, etc)
36 * we have a simple routine to test that functionality. These
37 * individual routines do not have to do anything "stressful".
39 * (2) We turn these simple functionality tests into a stress test by
40 * running them all in parallel, with as many threads as desired,
41 * and spread across as many datasets, objects, and vdevs as desired.
43 * (3) While all this is happening, we inject faults into the pool to
44 * verify that self-healing data really works.
46 * (4) Every time we open a dataset, we change its checksum and compression
47 * functions. Thus even individual objects vary from block to block
48 * in which checksum they use and whether they're compressed.
50 * (5) To verify that we never lose on-disk consistency after a crash,
51 * we run the entire test in a child of the main process.
52 * At random times, the child self-immolates with a SIGKILL.
53 * This is the software equivalent of pulling the power cord.
54 * The parent then runs the test again, using the existing
55 * storage pool, as many times as desired. If backwards compatibility
56 * testing is enabled ztest will sometimes run the "older" version
57 * of ztest after a SIGKILL.
59 * (6) To verify that we don't have future leaks or temporal incursions,
60 * many of the functional tests record the transaction group number
61 * as part of their data. When reading old data, they verify that
62 * the transaction group number is less than the current, open txg.
63 * If you add a new test, please do this if applicable.
65 * (7) Threads are created with a reduced stack size, for sanity checking.
66 * Therefore, it's important not to allocate huge buffers on the stack.
68 * When run with no arguments, ztest runs for about five minutes and
69 * produces no output if successful. To get a little bit of information,
70 * specify -V. To get more information, specify -VV, and so on.
72 * To turn this into an overnight stress test, use -T to specify run time.
74 * You can ask more more vdevs [-v], datasets [-d], or threads [-t]
75 * to increase the pool capacity, fanout, and overall stress level.
77 * Use the -k option to set the desired frequency of kills.
79 * When ztest invokes itself it passes all relevant information through a
80 * temporary file which is mmap-ed in the child process. This allows shared
81 * memory to survive the exec syscall. The ztest_shared_hdr_t struct is always
82 * stored at offset 0 of this file and contains information on the size and
83 * number of shared structures in the file. The information stored in this file
84 * must remain backwards compatible with older versions of ztest so that
85 * ztest can invoke them during backwards compatibility testing (-B).
88 #include <sys/zfs_context.h>
94 #include <sys/dmu_objset.h>
100 #include <sys/resource.h>
103 #include <sys/zil_impl.h>
104 #include <sys/zfs_rlock.h>
105 #include <sys/vdev_impl.h>
106 #include <sys/vdev_file.h>
107 #include <sys/spa_impl.h>
108 #include <sys/metaslab_impl.h>
109 #include <sys/dsl_prop.h>
110 #include <sys/dsl_dataset.h>
111 #include <sys/dsl_destroy.h>
112 #include <sys/dsl_scan.h>
113 #include <sys/zio_checksum.h>
114 #include <sys/refcount.h>
115 #include <sys/zfeature.h>
116 #include <sys/dsl_userhold.h>
119 #include <stdio_ext.h>
126 #include <sys/fs/zfs.h>
127 #include <zfs_fletcher.h>
128 #include <libnvpair.h>
131 #include <execinfo.h> /* for backtrace() */
134 static int ztest_fd_data
= -1;
135 static int ztest_fd_rand
= -1;
137 typedef struct ztest_shared_hdr
{
138 uint64_t zh_hdr_size
;
139 uint64_t zh_opts_size
;
141 uint64_t zh_stats_size
;
142 uint64_t zh_stats_count
;
144 uint64_t zh_ds_count
;
145 } ztest_shared_hdr_t
;
147 static ztest_shared_hdr_t
*ztest_shared_hdr
;
149 typedef struct ztest_shared_opts
{
150 char zo_pool
[ZFS_MAX_DATASET_NAME_LEN
];
151 char zo_dir
[ZFS_MAX_DATASET_NAME_LEN
];
152 char zo_alt_ztest
[MAXNAMELEN
];
153 char zo_alt_libpath
[MAXNAMELEN
];
155 uint64_t zo_vdevtime
;
163 uint64_t zo_passtime
;
164 uint64_t zo_killrate
;
168 uint64_t zo_maxloops
;
169 uint64_t zo_metaslab_gang_bang
;
171 } ztest_shared_opts_t
;
173 static const ztest_shared_opts_t ztest_opts_defaults
= {
174 .zo_pool
= { 'z', 't', 'e', 's', 't', '\0' },
175 .zo_dir
= { '/', 't', 'm', 'p', '\0' },
176 .zo_alt_ztest
= { '\0' },
177 .zo_alt_libpath
= { '\0' },
179 .zo_ashift
= SPA_MINBLOCKSHIFT
,
182 .zo_raidz_parity
= 1,
183 .zo_vdev_size
= SPA_MINDEVSIZE
* 4, /* 256m default size */
186 .zo_passtime
= 60, /* 60 seconds */
187 .zo_killrate
= 70, /* 70% kill rate */
191 .zo_time
= 300, /* 5 minutes */
192 .zo_maxloops
= 50, /* max loops during spa_freeze() */
193 .zo_metaslab_gang_bang
= 32 << 10
196 extern uint64_t metaslab_gang_bang
;
197 extern uint64_t metaslab_df_alloc_threshold
;
198 extern int metaslab_preload_limit
;
199 extern boolean_t zfs_compressed_arc_enabled
;
200 extern int zfs_abd_scatter_enabled
;
202 static ztest_shared_opts_t
*ztest_shared_opts
;
203 static ztest_shared_opts_t ztest_opts
;
205 typedef struct ztest_shared_ds
{
209 static ztest_shared_ds_t
*ztest_shared_ds
;
210 #define ZTEST_GET_SHARED_DS(d) (&ztest_shared_ds[d])
212 #define BT_MAGIC 0x123456789abcdefULL
213 #define MAXFAULTS() \
214 (MAX(zs->zs_mirrors, 1) * (ztest_opts.zo_raidz_parity + 1) - 1)
218 ZTEST_IO_WRITE_PATTERN
,
219 ZTEST_IO_WRITE_ZEROES
,
226 typedef struct ztest_block_tag
{
230 uint64_t bt_dnodesize
;
237 typedef struct bufwad
{
255 #define ZTEST_RANGE_LOCKS 64
256 #define ZTEST_OBJECT_LOCKS 64
259 * Object descriptor. Used as a template for object lookup/create/remove.
261 typedef struct ztest_od
{
264 dmu_object_type_t od_type
;
265 dmu_object_type_t od_crtype
;
266 uint64_t od_blocksize
;
267 uint64_t od_crblocksize
;
268 uint64_t od_crdnodesize
;
271 char od_name
[ZFS_MAX_DATASET_NAME_LEN
];
277 typedef struct ztest_ds
{
278 ztest_shared_ds_t
*zd_shared
;
280 rwlock_t zd_zilog_lock
;
282 ztest_od_t
*zd_od
; /* debugging aid */
283 char zd_name
[ZFS_MAX_DATASET_NAME_LEN
];
284 kmutex_t zd_dirobj_lock
;
285 rll_t zd_object_lock
[ZTEST_OBJECT_LOCKS
];
286 zll_t zd_range_lock
[ZTEST_RANGE_LOCKS
];
290 * Per-iteration state.
292 typedef void ztest_func_t(ztest_ds_t
*zd
, uint64_t id
);
294 typedef struct ztest_info
{
295 ztest_func_t
*zi_func
; /* test function */
296 uint64_t zi_iters
; /* iterations per execution */
297 uint64_t *zi_interval
; /* execute every <interval> seconds */
298 const char *zi_funcname
; /* name of test function */
301 typedef struct ztest_shared_callstate
{
302 uint64_t zc_count
; /* per-pass count */
303 uint64_t zc_time
; /* per-pass time */
304 uint64_t zc_next
; /* next time to call this function */
305 } ztest_shared_callstate_t
;
307 static ztest_shared_callstate_t
*ztest_shared_callstate
;
308 #define ZTEST_GET_SHARED_CALLSTATE(c) (&ztest_shared_callstate[c])
310 ztest_func_t ztest_dmu_read_write
;
311 ztest_func_t ztest_dmu_write_parallel
;
312 ztest_func_t ztest_dmu_object_alloc_free
;
313 ztest_func_t ztest_dmu_commit_callbacks
;
314 ztest_func_t ztest_zap
;
315 ztest_func_t ztest_zap_parallel
;
316 ztest_func_t ztest_zil_commit
;
317 ztest_func_t ztest_zil_remount
;
318 ztest_func_t ztest_dmu_read_write_zcopy
;
319 ztest_func_t ztest_dmu_objset_create_destroy
;
320 ztest_func_t ztest_dmu_prealloc
;
321 ztest_func_t ztest_fzap
;
322 ztest_func_t ztest_dmu_snapshot_create_destroy
;
323 ztest_func_t ztest_dsl_prop_get_set
;
324 ztest_func_t ztest_spa_prop_get_set
;
325 ztest_func_t ztest_spa_create_destroy
;
326 ztest_func_t ztest_fault_inject
;
327 ztest_func_t ztest_ddt_repair
;
328 ztest_func_t ztest_dmu_snapshot_hold
;
329 ztest_func_t ztest_mmp_enable_disable
;
330 ztest_func_t ztest_spa_rename
;
331 ztest_func_t ztest_scrub
;
332 ztest_func_t ztest_dsl_dataset_promote_busy
;
333 ztest_func_t ztest_vdev_attach_detach
;
334 ztest_func_t ztest_vdev_LUN_growth
;
335 ztest_func_t ztest_vdev_add_remove
;
336 ztest_func_t ztest_vdev_aux_add_remove
;
337 ztest_func_t ztest_split_pool
;
338 ztest_func_t ztest_reguid
;
339 ztest_func_t ztest_spa_upgrade
;
340 ztest_func_t ztest_fletcher
;
341 ztest_func_t ztest_fletcher_incr
;
342 ztest_func_t ztest_verify_dnode_bt
;
344 uint64_t zopt_always
= 0ULL * NANOSEC
; /* all the time */
345 uint64_t zopt_incessant
= 1ULL * NANOSEC
/ 10; /* every 1/10 second */
346 uint64_t zopt_often
= 1ULL * NANOSEC
; /* every second */
347 uint64_t zopt_sometimes
= 10ULL * NANOSEC
; /* every 10 seconds */
348 uint64_t zopt_rarely
= 60ULL * NANOSEC
; /* every 60 seconds */
350 #define ZTI_INIT(func, iters, interval) \
351 { .zi_func = (func), \
352 .zi_iters = (iters), \
353 .zi_interval = (interval), \
354 .zi_funcname = # func }
356 ztest_info_t ztest_info
[] = {
357 ZTI_INIT(ztest_dmu_read_write
, 1, &zopt_always
),
358 ZTI_INIT(ztest_dmu_write_parallel
, 10, &zopt_always
),
359 ZTI_INIT(ztest_dmu_object_alloc_free
, 1, &zopt_always
),
360 ZTI_INIT(ztest_dmu_commit_callbacks
, 1, &zopt_always
),
361 ZTI_INIT(ztest_zap
, 30, &zopt_always
),
362 ZTI_INIT(ztest_zap_parallel
, 100, &zopt_always
),
363 ZTI_INIT(ztest_split_pool
, 1, &zopt_always
),
364 ZTI_INIT(ztest_zil_commit
, 1, &zopt_incessant
),
365 ZTI_INIT(ztest_zil_remount
, 1, &zopt_sometimes
),
366 ZTI_INIT(ztest_dmu_read_write_zcopy
, 1, &zopt_often
),
367 ZTI_INIT(ztest_dmu_objset_create_destroy
, 1, &zopt_often
),
368 ZTI_INIT(ztest_dsl_prop_get_set
, 1, &zopt_often
),
369 ZTI_INIT(ztest_spa_prop_get_set
, 1, &zopt_sometimes
),
371 ZTI_INIT(ztest_dmu_prealloc
, 1, &zopt_sometimes
),
373 ZTI_INIT(ztest_fzap
, 1, &zopt_sometimes
),
374 ZTI_INIT(ztest_dmu_snapshot_create_destroy
, 1, &zopt_sometimes
),
375 ZTI_INIT(ztest_spa_create_destroy
, 1, &zopt_sometimes
),
376 ZTI_INIT(ztest_fault_inject
, 1, &zopt_sometimes
),
377 ZTI_INIT(ztest_ddt_repair
, 1, &zopt_sometimes
),
378 ZTI_INIT(ztest_dmu_snapshot_hold
, 1, &zopt_sometimes
),
379 ZTI_INIT(ztest_mmp_enable_disable
, 1, &zopt_sometimes
),
380 ZTI_INIT(ztest_reguid
, 1, &zopt_rarely
),
381 ZTI_INIT(ztest_spa_rename
, 1, &zopt_rarely
),
382 ZTI_INIT(ztest_scrub
, 1, &zopt_rarely
),
383 ZTI_INIT(ztest_spa_upgrade
, 1, &zopt_rarely
),
384 ZTI_INIT(ztest_dsl_dataset_promote_busy
, 1, &zopt_rarely
),
385 ZTI_INIT(ztest_vdev_attach_detach
, 1, &zopt_sometimes
),
386 ZTI_INIT(ztest_vdev_LUN_growth
, 1, &zopt_rarely
),
387 ZTI_INIT(ztest_vdev_add_remove
, 1, &ztest_opts
.zo_vdevtime
),
388 ZTI_INIT(ztest_vdev_aux_add_remove
, 1, &ztest_opts
.zo_vdevtime
),
389 ZTI_INIT(ztest_fletcher
, 1, &zopt_rarely
),
390 ZTI_INIT(ztest_fletcher_incr
, 1, &zopt_rarely
),
391 ZTI_INIT(ztest_verify_dnode_bt
, 1, &zopt_sometimes
),
394 #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
397 * The following struct is used to hold a list of uncalled commit callbacks.
398 * The callbacks are ordered by txg number.
400 typedef struct ztest_cb_list
{
401 kmutex_t zcl_callbacks_lock
;
402 list_t zcl_callbacks
;
406 * Stuff we need to share writably between parent and child.
408 typedef struct ztest_shared
{
409 boolean_t zs_do_init
;
410 hrtime_t zs_proc_start
;
411 hrtime_t zs_proc_stop
;
412 hrtime_t zs_thread_start
;
413 hrtime_t zs_thread_stop
;
414 hrtime_t zs_thread_kill
;
415 uint64_t zs_enospc_count
;
416 uint64_t zs_vdev_next_leaf
;
417 uint64_t zs_vdev_aux
;
422 uint64_t zs_metaslab_sz
;
423 uint64_t zs_metaslab_df_alloc_threshold
;
427 #define ID_PARALLEL -1ULL
429 static char ztest_dev_template
[] = "%s/%s.%llua";
430 static char ztest_aux_template
[] = "%s/%s.%s.%llu";
431 ztest_shared_t
*ztest_shared
;
433 static spa_t
*ztest_spa
= NULL
;
434 static ztest_ds_t
*ztest_ds
;
436 static kmutex_t ztest_vdev_lock
;
439 * The ztest_name_lock protects the pool and dataset namespace used by
440 * the individual tests. To modify the namespace, consumers must grab
441 * this lock as writer. Grabbing the lock as reader will ensure that the
442 * namespace does not change while the lock is held.
444 static rwlock_t ztest_name_lock
;
446 static boolean_t ztest_dump_core
= B_TRUE
;
447 static boolean_t ztest_exiting
;
449 /* Global commit callback list */
450 static ztest_cb_list_t zcl
;
451 /* Commit cb delay */
452 static uint64_t zc_min_txg_delay
= UINT64_MAX
;
453 static int zc_cb_counter
= 0;
456 * Minimum number of commit callbacks that need to be registered for us to check
457 * whether the minimum txg delay is acceptable.
459 #define ZTEST_COMMIT_CB_MIN_REG 100
462 * If a number of txgs equal to this threshold have been created after a commit
463 * callback has been registered but not called, then we assume there is an
464 * implementation bug.
466 #define ZTEST_COMMIT_CB_THRESH (TXG_CONCURRENT_STATES + 1000)
468 extern uint64_t metaslab_gang_bang
;
469 extern uint64_t metaslab_df_alloc_threshold
;
472 ZTEST_META_DNODE
= 0,
477 static void usage(boolean_t
) __NORETURN
;
480 * These libumem hooks provide a reasonable set of defaults for the allocator's
481 * debugging facilities.
484 _umem_debug_init(void)
486 return ("default,verbose"); /* $UMEM_DEBUG setting */
490 _umem_logging_init(void)
492 return ("fail,contents"); /* $UMEM_LOGGING setting */
495 #define BACKTRACE_SZ 100
497 static void sig_handler(int signo
)
499 struct sigaction action
;
500 #ifdef __GLIBC__ /* backtrace() is a GNU extension */
502 void *buffer
[BACKTRACE_SZ
];
504 nptrs
= backtrace(buffer
, BACKTRACE_SZ
);
505 backtrace_symbols_fd(buffer
, nptrs
, STDERR_FILENO
);
509 * Restore default action and re-raise signal so SIGSEGV and
510 * SIGABRT can trigger a core dump.
512 action
.sa_handler
= SIG_DFL
;
513 sigemptyset(&action
.sa_mask
);
515 (void) sigaction(signo
, &action
, NULL
);
519 #define FATAL_MSG_SZ 1024
524 fatal(int do_perror
, char *message
, ...)
527 int save_errno
= errno
;
530 (void) fflush(stdout
);
531 buf
= umem_alloc(FATAL_MSG_SZ
, UMEM_NOFAIL
);
533 va_start(args
, message
);
534 (void) sprintf(buf
, "ztest: ");
536 (void) vsprintf(buf
+ strlen(buf
), message
, args
);
539 (void) snprintf(buf
+ strlen(buf
), FATAL_MSG_SZ
- strlen(buf
),
540 ": %s", strerror(save_errno
));
542 (void) fprintf(stderr
, "%s\n", buf
);
543 fatal_msg
= buf
; /* to ease debugging */
550 str2shift(const char *buf
)
552 const char *ends
= "BKMGTPEZ";
557 for (i
= 0; i
< strlen(ends
); i
++) {
558 if (toupper(buf
[0]) == ends
[i
])
561 if (i
== strlen(ends
)) {
562 (void) fprintf(stderr
, "ztest: invalid bytes suffix: %s\n",
566 if (buf
[1] == '\0' || (toupper(buf
[1]) == 'B' && buf
[2] == '\0')) {
569 (void) fprintf(stderr
, "ztest: invalid bytes suffix: %s\n", buf
);
575 nicenumtoull(const char *buf
)
580 val
= strtoull(buf
, &end
, 0);
582 (void) fprintf(stderr
, "ztest: bad numeric value: %s\n", buf
);
584 } else if (end
[0] == '.') {
585 double fval
= strtod(buf
, &end
);
586 fval
*= pow(2, str2shift(end
));
587 if (fval
> UINT64_MAX
) {
588 (void) fprintf(stderr
, "ztest: value too large: %s\n",
592 val
= (uint64_t)fval
;
594 int shift
= str2shift(end
);
595 if (shift
>= 64 || (val
<< shift
) >> shift
!= val
) {
596 (void) fprintf(stderr
, "ztest: value too large: %s\n",
606 usage(boolean_t requested
)
608 const ztest_shared_opts_t
*zo
= &ztest_opts_defaults
;
610 char nice_vdev_size
[10];
611 char nice_gang_bang
[10];
612 FILE *fp
= requested
? stdout
: stderr
;
614 nicenum(zo
->zo_vdev_size
, nice_vdev_size
);
615 nicenum(zo
->zo_metaslab_gang_bang
, nice_gang_bang
);
617 (void) fprintf(fp
, "Usage: %s\n"
618 "\t[-v vdevs (default: %llu)]\n"
619 "\t[-s size_of_each_vdev (default: %s)]\n"
620 "\t[-a alignment_shift (default: %d)] use 0 for random\n"
621 "\t[-m mirror_copies (default: %d)]\n"
622 "\t[-r raidz_disks (default: %d)]\n"
623 "\t[-R raidz_parity (default: %d)]\n"
624 "\t[-d datasets (default: %d)]\n"
625 "\t[-t threads (default: %d)]\n"
626 "\t[-g gang_block_threshold (default: %s)]\n"
627 "\t[-i init_count (default: %d)] initialize pool i times\n"
628 "\t[-k kill_percentage (default: %llu%%)]\n"
629 "\t[-p pool_name (default: %s)]\n"
630 "\t[-f dir (default: %s)] file directory for vdev files\n"
631 "\t[-M] Multi-host simulate pool imported on remote host\n"
632 "\t[-V] verbose (use multiple times for ever more blather)\n"
633 "\t[-E] use existing pool instead of creating new one\n"
634 "\t[-T time (default: %llu sec)] total run time\n"
635 "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
636 "\t[-P passtime (default: %llu sec)] time per pass\n"
637 "\t[-B alt_ztest (default: <none>)] alternate ztest path\n"
638 "\t[-o variable=value] ... set global variable to an unsigned\n"
639 "\t 32-bit integer value\n"
640 "\t[-h] (print help)\n"
643 (u_longlong_t
)zo
->zo_vdevs
, /* -v */
644 nice_vdev_size
, /* -s */
645 zo
->zo_ashift
, /* -a */
646 zo
->zo_mirrors
, /* -m */
647 zo
->zo_raidz
, /* -r */
648 zo
->zo_raidz_parity
, /* -R */
649 zo
->zo_datasets
, /* -d */
650 zo
->zo_threads
, /* -t */
651 nice_gang_bang
, /* -g */
652 zo
->zo_init
, /* -i */
653 (u_longlong_t
)zo
->zo_killrate
, /* -k */
654 zo
->zo_pool
, /* -p */
656 (u_longlong_t
)zo
->zo_time
, /* -T */
657 (u_longlong_t
)zo
->zo_maxloops
, /* -F */
658 (u_longlong_t
)zo
->zo_passtime
);
659 exit(requested
? 0 : 1);
663 process_options(int argc
, char **argv
)
666 ztest_shared_opts_t
*zo
= &ztest_opts
;
670 char altdir
[MAXNAMELEN
] = { 0 };
672 bcopy(&ztest_opts_defaults
, zo
, sizeof (*zo
));
674 while ((opt
= getopt(argc
, argv
,
675 "v:s:a:m:r:R:d:t:g:i:k:p:f:MVET:P:hF:B:o:")) != EOF
) {
692 value
= nicenumtoull(optarg
);
696 zo
->zo_vdevs
= value
;
699 zo
->zo_vdev_size
= MAX(SPA_MINDEVSIZE
, value
);
702 zo
->zo_ashift
= value
;
705 zo
->zo_mirrors
= value
;
708 zo
->zo_raidz
= MAX(1, value
);
711 zo
->zo_raidz_parity
= MIN(MAX(value
, 1), 3);
714 zo
->zo_datasets
= MAX(1, value
);
717 zo
->zo_threads
= MAX(1, value
);
720 zo
->zo_metaslab_gang_bang
= MAX(SPA_MINBLOCKSIZE
<< 1,
727 zo
->zo_killrate
= value
;
730 (void) strlcpy(zo
->zo_pool
, optarg
,
731 sizeof (zo
->zo_pool
));
734 path
= realpath(optarg
, NULL
);
736 (void) fprintf(stderr
, "error: %s: %s\n",
737 optarg
, strerror(errno
));
740 (void) strlcpy(zo
->zo_dir
, path
,
741 sizeof (zo
->zo_dir
));
758 zo
->zo_passtime
= MAX(1, value
);
761 zo
->zo_maxloops
= MAX(1, value
);
764 (void) strlcpy(altdir
, optarg
, sizeof (altdir
));
767 if (set_global_var(optarg
) != 0)
780 zo
->zo_raidz_parity
= MIN(zo
->zo_raidz_parity
, zo
->zo_raidz
- 1);
783 (zo
->zo_vdevs
> 0 ? zo
->zo_time
* NANOSEC
/ zo
->zo_vdevs
:
786 if (strlen(altdir
) > 0) {
794 cmd
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
795 realaltdir
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
797 VERIFY(NULL
!= realpath(getexecname(), cmd
));
798 if (0 != access(altdir
, F_OK
)) {
799 ztest_dump_core
= B_FALSE
;
800 fatal(B_TRUE
, "invalid alternate ztest path: %s",
803 VERIFY(NULL
!= realpath(altdir
, realaltdir
));
806 * 'cmd' should be of the form "<anything>/usr/bin/<isa>/ztest".
807 * We want to extract <isa> to determine if we should use
808 * 32 or 64 bit binaries.
810 bin
= strstr(cmd
, "/usr/bin/");
811 ztest
= strstr(bin
, "/ztest");
813 isalen
= ztest
- isa
;
814 (void) snprintf(zo
->zo_alt_ztest
, sizeof (zo
->zo_alt_ztest
),
815 "%s/usr/bin/%.*s/ztest", realaltdir
, isalen
, isa
);
816 (void) snprintf(zo
->zo_alt_libpath
, sizeof (zo
->zo_alt_libpath
),
817 "%s/usr/lib/%.*s", realaltdir
, isalen
, isa
);
819 if (0 != access(zo
->zo_alt_ztest
, X_OK
)) {
820 ztest_dump_core
= B_FALSE
;
821 fatal(B_TRUE
, "invalid alternate ztest: %s",
823 } else if (0 != access(zo
->zo_alt_libpath
, X_OK
)) {
824 ztest_dump_core
= B_FALSE
;
825 fatal(B_TRUE
, "invalid alternate lib directory %s",
829 umem_free(cmd
, MAXPATHLEN
);
830 umem_free(realaltdir
, MAXPATHLEN
);
835 ztest_kill(ztest_shared_t
*zs
)
837 zs
->zs_alloc
= metaslab_class_get_alloc(spa_normal_class(ztest_spa
));
838 zs
->zs_space
= metaslab_class_get_space(spa_normal_class(ztest_spa
));
841 * Before we kill off ztest, make sure that the config is updated.
842 * See comment above spa_config_sync().
844 mutex_enter(&spa_namespace_lock
);
845 spa_config_sync(ztest_spa
, B_FALSE
, B_FALSE
);
846 mutex_exit(&spa_namespace_lock
);
848 (void) kill(getpid(), SIGKILL
);
852 ztest_random(uint64_t range
)
856 ASSERT3S(ztest_fd_rand
, >=, 0);
861 if (read(ztest_fd_rand
, &r
, sizeof (r
)) != sizeof (r
))
862 fatal(1, "short read from /dev/urandom");
869 ztest_record_enospc(const char *s
)
871 ztest_shared
->zs_enospc_count
++;
875 ztest_get_ashift(void)
877 if (ztest_opts
.zo_ashift
== 0)
878 return (SPA_MINBLOCKSHIFT
+ ztest_random(5));
879 return (ztest_opts
.zo_ashift
);
883 make_vdev_file(char *path
, char *aux
, char *pool
, size_t size
, uint64_t ashift
)
889 pathbuf
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
892 ashift
= ztest_get_ashift();
898 vdev
= ztest_shared
->zs_vdev_aux
;
899 (void) snprintf(path
, MAXPATHLEN
,
900 ztest_aux_template
, ztest_opts
.zo_dir
,
901 pool
== NULL
? ztest_opts
.zo_pool
: pool
,
904 vdev
= ztest_shared
->zs_vdev_next_leaf
++;
905 (void) snprintf(path
, MAXPATHLEN
,
906 ztest_dev_template
, ztest_opts
.zo_dir
,
907 pool
== NULL
? ztest_opts
.zo_pool
: pool
, vdev
);
912 int fd
= open(path
, O_RDWR
| O_CREAT
| O_TRUNC
, 0666);
914 fatal(1, "can't open %s", path
);
915 if (ftruncate(fd
, size
) != 0)
916 fatal(1, "can't ftruncate %s", path
);
920 VERIFY(nvlist_alloc(&file
, NV_UNIQUE_NAME
, 0) == 0);
921 VERIFY(nvlist_add_string(file
, ZPOOL_CONFIG_TYPE
, VDEV_TYPE_FILE
) == 0);
922 VERIFY(nvlist_add_string(file
, ZPOOL_CONFIG_PATH
, path
) == 0);
923 VERIFY(nvlist_add_uint64(file
, ZPOOL_CONFIG_ASHIFT
, ashift
) == 0);
924 umem_free(pathbuf
, MAXPATHLEN
);
930 make_vdev_raidz(char *path
, char *aux
, char *pool
, size_t size
,
931 uint64_t ashift
, int r
)
933 nvlist_t
*raidz
, **child
;
937 return (make_vdev_file(path
, aux
, pool
, size
, ashift
));
938 child
= umem_alloc(r
* sizeof (nvlist_t
*), UMEM_NOFAIL
);
940 for (c
= 0; c
< r
; c
++)
941 child
[c
] = make_vdev_file(path
, aux
, pool
, size
, ashift
);
943 VERIFY(nvlist_alloc(&raidz
, NV_UNIQUE_NAME
, 0) == 0);
944 VERIFY(nvlist_add_string(raidz
, ZPOOL_CONFIG_TYPE
,
945 VDEV_TYPE_RAIDZ
) == 0);
946 VERIFY(nvlist_add_uint64(raidz
, ZPOOL_CONFIG_NPARITY
,
947 ztest_opts
.zo_raidz_parity
) == 0);
948 VERIFY(nvlist_add_nvlist_array(raidz
, ZPOOL_CONFIG_CHILDREN
,
951 for (c
= 0; c
< r
; c
++)
952 nvlist_free(child
[c
]);
954 umem_free(child
, r
* sizeof (nvlist_t
*));
960 make_vdev_mirror(char *path
, char *aux
, char *pool
, size_t size
,
961 uint64_t ashift
, int r
, int m
)
963 nvlist_t
*mirror
, **child
;
967 return (make_vdev_raidz(path
, aux
, pool
, size
, ashift
, r
));
969 child
= umem_alloc(m
* sizeof (nvlist_t
*), UMEM_NOFAIL
);
971 for (c
= 0; c
< m
; c
++)
972 child
[c
] = make_vdev_raidz(path
, aux
, pool
, size
, ashift
, r
);
974 VERIFY(nvlist_alloc(&mirror
, NV_UNIQUE_NAME
, 0) == 0);
975 VERIFY(nvlist_add_string(mirror
, ZPOOL_CONFIG_TYPE
,
976 VDEV_TYPE_MIRROR
) == 0);
977 VERIFY(nvlist_add_nvlist_array(mirror
, ZPOOL_CONFIG_CHILDREN
,
980 for (c
= 0; c
< m
; c
++)
981 nvlist_free(child
[c
]);
983 umem_free(child
, m
* sizeof (nvlist_t
*));
989 make_vdev_root(char *path
, char *aux
, char *pool
, size_t size
, uint64_t ashift
,
990 int log
, int r
, int m
, int t
)
992 nvlist_t
*root
, **child
;
997 child
= umem_alloc(t
* sizeof (nvlist_t
*), UMEM_NOFAIL
);
999 for (c
= 0; c
< t
; c
++) {
1000 child
[c
] = make_vdev_mirror(path
, aux
, pool
, size
, ashift
,
1002 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1006 VERIFY(nvlist_alloc(&root
, NV_UNIQUE_NAME
, 0) == 0);
1007 VERIFY(nvlist_add_string(root
, ZPOOL_CONFIG_TYPE
, VDEV_TYPE_ROOT
) == 0);
1008 VERIFY(nvlist_add_nvlist_array(root
, aux
? aux
: ZPOOL_CONFIG_CHILDREN
,
1011 for (c
= 0; c
< t
; c
++)
1012 nvlist_free(child
[c
]);
1014 umem_free(child
, t
* sizeof (nvlist_t
*));
1020 * Find a random spa version. Returns back a random spa version in the
1021 * range [initial_version, SPA_VERSION_FEATURES].
1024 ztest_random_spa_version(uint64_t initial_version
)
1026 uint64_t version
= initial_version
;
1028 if (version
<= SPA_VERSION_BEFORE_FEATURES
) {
1030 ztest_random(SPA_VERSION_BEFORE_FEATURES
- version
+ 1);
1033 if (version
> SPA_VERSION_BEFORE_FEATURES
)
1034 version
= SPA_VERSION_FEATURES
;
1036 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
1041 ztest_random_blocksize(void)
1044 * Choose a block size >= the ashift.
1045 * If the SPA supports new MAXBLOCKSIZE, test up to 1MB blocks.
1047 int maxbs
= SPA_OLD_MAXBLOCKSHIFT
;
1048 if (spa_maxblocksize(ztest_spa
) == SPA_MAXBLOCKSIZE
)
1050 uint64_t block_shift
=
1051 ztest_random(maxbs
- ztest_spa
->spa_max_ashift
+ 1);
1052 return (1 << (SPA_MINBLOCKSHIFT
+ block_shift
));
1056 ztest_random_dnodesize(void)
1059 int max_slots
= spa_maxdnodesize(ztest_spa
) >> DNODE_SHIFT
;
1061 if (max_slots
== DNODE_MIN_SLOTS
)
1062 return (DNODE_MIN_SIZE
);
1065 * Weight the random distribution more heavily toward smaller
1066 * dnode sizes since that is more likely to reflect real-world
1069 ASSERT3U(max_slots
, >, 4);
1070 switch (ztest_random(10)) {
1072 slots
= 5 + ztest_random(max_slots
- 4);
1075 slots
= 2 + ztest_random(3);
1082 return (slots
<< DNODE_SHIFT
);
1086 ztest_random_ibshift(void)
1088 return (DN_MIN_INDBLKSHIFT
+
1089 ztest_random(DN_MAX_INDBLKSHIFT
- DN_MIN_INDBLKSHIFT
+ 1));
1093 ztest_random_vdev_top(spa_t
*spa
, boolean_t log_ok
)
1096 vdev_t
*rvd
= spa
->spa_root_vdev
;
1099 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
1102 top
= ztest_random(rvd
->vdev_children
);
1103 tvd
= rvd
->vdev_child
[top
];
1104 } while (tvd
->vdev_ishole
|| (tvd
->vdev_islog
&& !log_ok
) ||
1105 tvd
->vdev_mg
== NULL
|| tvd
->vdev_mg
->mg_class
== NULL
);
1111 ztest_random_dsl_prop(zfs_prop_t prop
)
1116 value
= zfs_prop_random_value(prop
, ztest_random(-1ULL));
1117 } while (prop
== ZFS_PROP_CHECKSUM
&& value
== ZIO_CHECKSUM_OFF
);
1123 ztest_dsl_prop_set_uint64(char *osname
, zfs_prop_t prop
, uint64_t value
,
1126 const char *propname
= zfs_prop_to_name(prop
);
1127 const char *valname
;
1132 error
= dsl_prop_set_int(osname
, propname
,
1133 (inherit
? ZPROP_SRC_NONE
: ZPROP_SRC_LOCAL
), value
);
1135 if (error
== ENOSPC
) {
1136 ztest_record_enospc(FTAG
);
1141 setpoint
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
1142 VERIFY0(dsl_prop_get_integer(osname
, propname
, &curval
, setpoint
));
1144 if (ztest_opts
.zo_verbose
>= 6) {
1147 err
= zfs_prop_index_to_string(prop
, curval
, &valname
);
1149 (void) printf("%s %s = %llu at '%s'\n", osname
,
1150 propname
, (unsigned long long)curval
, setpoint
);
1152 (void) printf("%s %s = %s at '%s'\n",
1153 osname
, propname
, valname
, setpoint
);
1155 umem_free(setpoint
, MAXPATHLEN
);
1161 ztest_spa_prop_set_uint64(zpool_prop_t prop
, uint64_t value
)
1163 spa_t
*spa
= ztest_spa
;
1164 nvlist_t
*props
= NULL
;
1167 VERIFY(nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) == 0);
1168 VERIFY(nvlist_add_uint64(props
, zpool_prop_to_name(prop
), value
) == 0);
1170 error
= spa_prop_set(spa
, props
);
1174 if (error
== ENOSPC
) {
1175 ztest_record_enospc(FTAG
);
1185 * Object and range lock mechanics
1188 list_node_t z_lnode
;
1189 refcount_t z_refcnt
;
1191 zfs_rlock_t z_range_lock
;
1196 ztest_znode_t
*z_ztznode
;
1199 static ztest_znode_t
*
1200 ztest_znode_init(uint64_t object
)
1202 ztest_znode_t
*zp
= umem_alloc(sizeof (*zp
), UMEM_NOFAIL
);
1204 list_link_init(&zp
->z_lnode
);
1205 refcount_create(&zp
->z_refcnt
);
1206 zp
->z_object
= object
;
1207 zfs_rlock_init(&zp
->z_range_lock
);
1213 ztest_znode_fini(ztest_znode_t
*zp
)
1215 ASSERT(refcount_is_zero(&zp
->z_refcnt
));
1216 zfs_rlock_destroy(&zp
->z_range_lock
);
1218 refcount_destroy(&zp
->z_refcnt
);
1219 list_link_init(&zp
->z_lnode
);
1220 umem_free(zp
, sizeof (*zp
));
1224 ztest_zll_init(zll_t
*zll
)
1226 mutex_init(&zll
->z_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1227 list_create(&zll
->z_list
, sizeof (ztest_znode_t
),
1228 offsetof(ztest_znode_t
, z_lnode
));
1232 ztest_zll_destroy(zll_t
*zll
)
1234 list_destroy(&zll
->z_list
);
1235 mutex_destroy(&zll
->z_lock
);
1238 #define RL_TAG "range_lock"
1239 static ztest_znode_t
*
1240 ztest_znode_get(ztest_ds_t
*zd
, uint64_t object
)
1242 zll_t
*zll
= &zd
->zd_range_lock
[object
& (ZTEST_OBJECT_LOCKS
- 1)];
1243 ztest_znode_t
*zp
= NULL
;
1244 mutex_enter(&zll
->z_lock
);
1245 for (zp
= list_head(&zll
->z_list
); (zp
);
1246 zp
= list_next(&zll
->z_list
, zp
)) {
1247 if (zp
->z_object
== object
) {
1248 refcount_add(&zp
->z_refcnt
, RL_TAG
);
1253 zp
= ztest_znode_init(object
);
1254 refcount_add(&zp
->z_refcnt
, RL_TAG
);
1255 list_insert_head(&zll
->z_list
, zp
);
1257 mutex_exit(&zll
->z_lock
);
1262 ztest_znode_put(ztest_ds_t
*zd
, ztest_znode_t
*zp
)
1265 ASSERT3U(zp
->z_object
, !=, 0);
1266 zll
= &zd
->zd_range_lock
[zp
->z_object
& (ZTEST_OBJECT_LOCKS
- 1)];
1267 mutex_enter(&zll
->z_lock
);
1268 refcount_remove(&zp
->z_refcnt
, RL_TAG
);
1269 if (refcount_is_zero(&zp
->z_refcnt
)) {
1270 list_remove(&zll
->z_list
, zp
);
1271 ztest_znode_fini(zp
);
1273 mutex_exit(&zll
->z_lock
);
1278 ztest_rll_init(rll_t
*rll
)
1280 rll
->rll_writer
= NULL
;
1281 rll
->rll_readers
= 0;
1282 mutex_init(&rll
->rll_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1283 cv_init(&rll
->rll_cv
, NULL
, CV_DEFAULT
, NULL
);
1287 ztest_rll_destroy(rll_t
*rll
)
1289 ASSERT(rll
->rll_writer
== NULL
);
1290 ASSERT(rll
->rll_readers
== 0);
1291 mutex_destroy(&rll
->rll_lock
);
1292 cv_destroy(&rll
->rll_cv
);
1296 ztest_rll_lock(rll_t
*rll
, rl_type_t type
)
1298 mutex_enter(&rll
->rll_lock
);
1300 if (type
== RL_READER
) {
1301 while (rll
->rll_writer
!= NULL
)
1302 (void) cv_wait(&rll
->rll_cv
, &rll
->rll_lock
);
1305 while (rll
->rll_writer
!= NULL
|| rll
->rll_readers
)
1306 (void) cv_wait(&rll
->rll_cv
, &rll
->rll_lock
);
1307 rll
->rll_writer
= curthread
;
1310 mutex_exit(&rll
->rll_lock
);
1314 ztest_rll_unlock(rll_t
*rll
)
1316 mutex_enter(&rll
->rll_lock
);
1318 if (rll
->rll_writer
) {
1319 ASSERT(rll
->rll_readers
== 0);
1320 rll
->rll_writer
= NULL
;
1322 ASSERT(rll
->rll_readers
!= 0);
1323 ASSERT(rll
->rll_writer
== NULL
);
1327 if (rll
->rll_writer
== NULL
&& rll
->rll_readers
== 0)
1328 cv_broadcast(&rll
->rll_cv
);
1330 mutex_exit(&rll
->rll_lock
);
1334 ztest_object_lock(ztest_ds_t
*zd
, uint64_t object
, rl_type_t type
)
1336 rll_t
*rll
= &zd
->zd_object_lock
[object
& (ZTEST_OBJECT_LOCKS
- 1)];
1338 ztest_rll_lock(rll
, type
);
1342 ztest_object_unlock(ztest_ds_t
*zd
, uint64_t object
)
1344 rll_t
*rll
= &zd
->zd_object_lock
[object
& (ZTEST_OBJECT_LOCKS
- 1)];
1346 ztest_rll_unlock(rll
);
1349 static ztest_zrl_t
*
1350 ztest_zrl_init(rl_t
*rl
, ztest_znode_t
*zp
)
1352 ztest_zrl_t
*zrl
= umem_alloc(sizeof (*zrl
), UMEM_NOFAIL
);
1354 zrl
->z_ztznode
= zp
;
1359 ztest_zrl_fini(ztest_zrl_t
*zrl
)
1361 umem_free(zrl
, sizeof (*zrl
));
1364 static ztest_zrl_t
*
1365 ztest_range_lock(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
,
1366 uint64_t size
, rl_type_t type
)
1368 ztest_znode_t
*zp
= ztest_znode_get(zd
, object
);
1369 rl_t
*rl
= zfs_range_lock(&zp
->z_range_lock
, offset
,
1371 return (ztest_zrl_init(rl
, zp
));
1375 ztest_range_unlock(ztest_ds_t
*zd
, ztest_zrl_t
*zrl
)
1377 zfs_range_unlock(zrl
->z_rl
);
1378 ztest_znode_put(zd
, zrl
->z_ztznode
);
1379 ztest_zrl_fini(zrl
);
1383 ztest_zd_init(ztest_ds_t
*zd
, ztest_shared_ds_t
*szd
, objset_t
*os
)
1386 zd
->zd_zilog
= dmu_objset_zil(os
);
1387 zd
->zd_shared
= szd
;
1388 dmu_objset_name(os
, zd
->zd_name
);
1391 if (zd
->zd_shared
!= NULL
)
1392 zd
->zd_shared
->zd_seq
= 0;
1394 VERIFY(rwlock_init(&zd
->zd_zilog_lock
, USYNC_THREAD
, NULL
) == 0);
1395 mutex_init(&zd
->zd_dirobj_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1397 for (l
= 0; l
< ZTEST_OBJECT_LOCKS
; l
++)
1398 ztest_rll_init(&zd
->zd_object_lock
[l
]);
1400 for (l
= 0; l
< ZTEST_RANGE_LOCKS
; l
++)
1401 ztest_zll_init(&zd
->zd_range_lock
[l
]);
1405 ztest_zd_fini(ztest_ds_t
*zd
)
1409 mutex_destroy(&zd
->zd_dirobj_lock
);
1410 (void) rwlock_destroy(&zd
->zd_zilog_lock
);
1412 for (l
= 0; l
< ZTEST_OBJECT_LOCKS
; l
++)
1413 ztest_rll_destroy(&zd
->zd_object_lock
[l
]);
1415 for (l
= 0; l
< ZTEST_RANGE_LOCKS
; l
++)
1416 ztest_zll_destroy(&zd
->zd_range_lock
[l
]);
1419 #define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1422 ztest_tx_assign(dmu_tx_t
*tx
, uint64_t txg_how
, const char *tag
)
1428 * Attempt to assign tx to some transaction group.
1430 error
= dmu_tx_assign(tx
, txg_how
);
1432 if (error
== ERESTART
) {
1433 ASSERT(txg_how
== TXG_NOWAIT
);
1436 ASSERT3U(error
, ==, ENOSPC
);
1437 ztest_record_enospc(tag
);
1442 txg
= dmu_tx_get_txg(tx
);
1448 ztest_pattern_set(void *buf
, uint64_t size
, uint64_t value
)
1451 uint64_t *ip_end
= (uint64_t *)((uintptr_t)buf
+ (uintptr_t)size
);
1459 ztest_pattern_match(void *buf
, uint64_t size
, uint64_t value
)
1462 uint64_t *ip_end
= (uint64_t *)((uintptr_t)buf
+ (uintptr_t)size
);
1466 diff
|= (value
- *ip
++);
1473 ztest_bt_generate(ztest_block_tag_t
*bt
, objset_t
*os
, uint64_t object
,
1474 uint64_t dnodesize
, uint64_t offset
, uint64_t gen
, uint64_t txg
,
1477 bt
->bt_magic
= BT_MAGIC
;
1478 bt
->bt_objset
= dmu_objset_id(os
);
1479 bt
->bt_object
= object
;
1480 bt
->bt_dnodesize
= dnodesize
;
1481 bt
->bt_offset
= offset
;
1484 bt
->bt_crtxg
= crtxg
;
1488 ztest_bt_verify(ztest_block_tag_t
*bt
, objset_t
*os
, uint64_t object
,
1489 uint64_t dnodesize
, uint64_t offset
, uint64_t gen
, uint64_t txg
,
1492 ASSERT3U(bt
->bt_magic
, ==, BT_MAGIC
);
1493 ASSERT3U(bt
->bt_objset
, ==, dmu_objset_id(os
));
1494 ASSERT3U(bt
->bt_object
, ==, object
);
1495 ASSERT3U(bt
->bt_dnodesize
, ==, dnodesize
);
1496 ASSERT3U(bt
->bt_offset
, ==, offset
);
1497 ASSERT3U(bt
->bt_gen
, <=, gen
);
1498 ASSERT3U(bt
->bt_txg
, <=, txg
);
1499 ASSERT3U(bt
->bt_crtxg
, ==, crtxg
);
1502 static ztest_block_tag_t
*
1503 ztest_bt_bonus(dmu_buf_t
*db
)
1505 dmu_object_info_t doi
;
1506 ztest_block_tag_t
*bt
;
1508 dmu_object_info_from_db(db
, &doi
);
1509 ASSERT3U(doi
.doi_bonus_size
, <=, db
->db_size
);
1510 ASSERT3U(doi
.doi_bonus_size
, >=, sizeof (*bt
));
1511 bt
= (void *)((char *)db
->db_data
+ doi
.doi_bonus_size
- sizeof (*bt
));
1517 * Generate a token to fill up unused bonus buffer space. Try to make
1518 * it unique to the object, generation, and offset to verify that data
1519 * is not getting overwritten by data from other dnodes.
1521 #define ZTEST_BONUS_FILL_TOKEN(obj, ds, gen, offset) \
1522 (((ds) << 48) | ((gen) << 32) | ((obj) << 8) | (offset))
1525 * Fill up the unused bonus buffer region before the block tag with a
1526 * verifiable pattern. Filling the whole bonus area with non-zero data
1527 * helps ensure that all dnode traversal code properly skips the
1528 * interior regions of large dnodes.
1531 ztest_fill_unused_bonus(dmu_buf_t
*db
, void *end
, uint64_t obj
,
1532 objset_t
*os
, uint64_t gen
)
1536 ASSERT(IS_P2ALIGNED((char *)end
- (char *)db
->db_data
, 8));
1538 for (bonusp
= db
->db_data
; bonusp
< (uint64_t *)end
; bonusp
++) {
1539 uint64_t token
= ZTEST_BONUS_FILL_TOKEN(obj
, dmu_objset_id(os
),
1540 gen
, bonusp
- (uint64_t *)db
->db_data
);
1546 * Verify that the unused area of a bonus buffer is filled with the
1550 ztest_verify_unused_bonus(dmu_buf_t
*db
, void *end
, uint64_t obj
,
1551 objset_t
*os
, uint64_t gen
)
1555 for (bonusp
= db
->db_data
; bonusp
< (uint64_t *)end
; bonusp
++) {
1556 uint64_t token
= ZTEST_BONUS_FILL_TOKEN(obj
, dmu_objset_id(os
),
1557 gen
, bonusp
- (uint64_t *)db
->db_data
);
1558 VERIFY3U(*bonusp
, ==, token
);
1566 #define lrz_type lr_mode
1567 #define lrz_blocksize lr_uid
1568 #define lrz_ibshift lr_gid
1569 #define lrz_bonustype lr_rdev
1570 #define lrz_dnodesize lr_crtime[1]
1573 ztest_log_create(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_create_t
*lr
)
1575 char *name
= (void *)(lr
+ 1); /* name follows lr */
1576 size_t namesize
= strlen(name
) + 1;
1579 if (zil_replaying(zd
->zd_zilog
, tx
))
1582 itx
= zil_itx_create(TX_CREATE
, sizeof (*lr
) + namesize
);
1583 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1584 sizeof (*lr
) + namesize
- sizeof (lr_t
));
1586 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1590 ztest_log_remove(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_remove_t
*lr
, uint64_t object
)
1592 char *name
= (void *)(lr
+ 1); /* name follows lr */
1593 size_t namesize
= strlen(name
) + 1;
1596 if (zil_replaying(zd
->zd_zilog
, tx
))
1599 itx
= zil_itx_create(TX_REMOVE
, sizeof (*lr
) + namesize
);
1600 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1601 sizeof (*lr
) + namesize
- sizeof (lr_t
));
1603 itx
->itx_oid
= object
;
1604 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1608 ztest_log_write(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_write_t
*lr
)
1611 itx_wr_state_t write_state
= ztest_random(WR_NUM_STATES
);
1613 if (zil_replaying(zd
->zd_zilog
, tx
))
1616 if (lr
->lr_length
> ZIL_MAX_LOG_DATA
)
1617 write_state
= WR_INDIRECT
;
1619 itx
= zil_itx_create(TX_WRITE
,
1620 sizeof (*lr
) + (write_state
== WR_COPIED
? lr
->lr_length
: 0));
1622 if (write_state
== WR_COPIED
&&
1623 dmu_read(zd
->zd_os
, lr
->lr_foid
, lr
->lr_offset
, lr
->lr_length
,
1624 ((lr_write_t
*)&itx
->itx_lr
) + 1, DMU_READ_NO_PREFETCH
) != 0) {
1625 zil_itx_destroy(itx
);
1626 itx
= zil_itx_create(TX_WRITE
, sizeof (*lr
));
1627 write_state
= WR_NEED_COPY
;
1629 itx
->itx_private
= zd
;
1630 itx
->itx_wr_state
= write_state
;
1631 itx
->itx_sync
= (ztest_random(8) == 0);
1633 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1634 sizeof (*lr
) - sizeof (lr_t
));
1636 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1640 ztest_log_truncate(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_truncate_t
*lr
)
1644 if (zil_replaying(zd
->zd_zilog
, tx
))
1647 itx
= zil_itx_create(TX_TRUNCATE
, sizeof (*lr
));
1648 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1649 sizeof (*lr
) - sizeof (lr_t
));
1651 itx
->itx_sync
= B_FALSE
;
1652 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1656 ztest_log_setattr(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_setattr_t
*lr
)
1660 if (zil_replaying(zd
->zd_zilog
, tx
))
1663 itx
= zil_itx_create(TX_SETATTR
, sizeof (*lr
));
1664 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1665 sizeof (*lr
) - sizeof (lr_t
));
1667 itx
->itx_sync
= B_FALSE
;
1668 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1675 ztest_replay_create(ztest_ds_t
*zd
, lr_create_t
*lr
, boolean_t byteswap
)
1677 char *name
= (void *)(lr
+ 1); /* name follows lr */
1678 objset_t
*os
= zd
->zd_os
;
1679 ztest_block_tag_t
*bbt
;
1687 byteswap_uint64_array(lr
, sizeof (*lr
));
1689 ASSERT(lr
->lr_doid
== ZTEST_DIROBJ
);
1690 ASSERT(name
[0] != '\0');
1692 tx
= dmu_tx_create(os
);
1694 dmu_tx_hold_zap(tx
, lr
->lr_doid
, B_TRUE
, name
);
1696 if (lr
->lrz_type
== DMU_OT_ZAP_OTHER
) {
1697 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, B_TRUE
, NULL
);
1699 dmu_tx_hold_bonus(tx
, DMU_NEW_OBJECT
);
1702 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
1706 ASSERT(dmu_objset_zil(os
)->zl_replay
== !!lr
->lr_foid
);
1707 bonuslen
= DN_BONUS_SIZE(lr
->lrz_dnodesize
);
1709 if (lr
->lrz_type
== DMU_OT_ZAP_OTHER
) {
1710 if (lr
->lr_foid
== 0) {
1711 lr
->lr_foid
= zap_create_dnsize(os
,
1712 lr
->lrz_type
, lr
->lrz_bonustype
,
1713 bonuslen
, lr
->lrz_dnodesize
, tx
);
1715 error
= zap_create_claim_dnsize(os
, lr
->lr_foid
,
1716 lr
->lrz_type
, lr
->lrz_bonustype
,
1717 bonuslen
, lr
->lrz_dnodesize
, tx
);
1720 if (lr
->lr_foid
== 0) {
1721 lr
->lr_foid
= dmu_object_alloc_dnsize(os
,
1722 lr
->lrz_type
, 0, lr
->lrz_bonustype
,
1723 bonuslen
, lr
->lrz_dnodesize
, tx
);
1725 error
= dmu_object_claim_dnsize(os
, lr
->lr_foid
,
1726 lr
->lrz_type
, 0, lr
->lrz_bonustype
,
1727 bonuslen
, lr
->lrz_dnodesize
, tx
);
1732 ASSERT3U(error
, ==, EEXIST
);
1733 ASSERT(zd
->zd_zilog
->zl_replay
);
1738 ASSERT(lr
->lr_foid
!= 0);
1740 if (lr
->lrz_type
!= DMU_OT_ZAP_OTHER
)
1741 VERIFY3U(0, ==, dmu_object_set_blocksize(os
, lr
->lr_foid
,
1742 lr
->lrz_blocksize
, lr
->lrz_ibshift
, tx
));
1744 VERIFY3U(0, ==, dmu_bonus_hold(os
, lr
->lr_foid
, FTAG
, &db
));
1745 bbt
= ztest_bt_bonus(db
);
1746 dmu_buf_will_dirty(db
, tx
);
1747 ztest_bt_generate(bbt
, os
, lr
->lr_foid
, lr
->lrz_dnodesize
, -1ULL,
1748 lr
->lr_gen
, txg
, txg
);
1749 ztest_fill_unused_bonus(db
, bbt
, lr
->lr_foid
, os
, lr
->lr_gen
);
1750 dmu_buf_rele(db
, FTAG
);
1752 VERIFY3U(0, ==, zap_add(os
, lr
->lr_doid
, name
, sizeof (uint64_t), 1,
1755 (void) ztest_log_create(zd
, tx
, lr
);
1763 ztest_replay_remove(ztest_ds_t
*zd
, lr_remove_t
*lr
, boolean_t byteswap
)
1765 char *name
= (void *)(lr
+ 1); /* name follows lr */
1766 objset_t
*os
= zd
->zd_os
;
1767 dmu_object_info_t doi
;
1769 uint64_t object
, txg
;
1772 byteswap_uint64_array(lr
, sizeof (*lr
));
1774 ASSERT(lr
->lr_doid
== ZTEST_DIROBJ
);
1775 ASSERT(name
[0] != '\0');
1778 zap_lookup(os
, lr
->lr_doid
, name
, sizeof (object
), 1, &object
));
1779 ASSERT(object
!= 0);
1781 ztest_object_lock(zd
, object
, RL_WRITER
);
1783 VERIFY3U(0, ==, dmu_object_info(os
, object
, &doi
));
1785 tx
= dmu_tx_create(os
);
1787 dmu_tx_hold_zap(tx
, lr
->lr_doid
, B_FALSE
, name
);
1788 dmu_tx_hold_free(tx
, object
, 0, DMU_OBJECT_END
);
1790 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
1792 ztest_object_unlock(zd
, object
);
1796 if (doi
.doi_type
== DMU_OT_ZAP_OTHER
) {
1797 VERIFY3U(0, ==, zap_destroy(os
, object
, tx
));
1799 VERIFY3U(0, ==, dmu_object_free(os
, object
, tx
));
1802 VERIFY3U(0, ==, zap_remove(os
, lr
->lr_doid
, name
, tx
));
1804 (void) ztest_log_remove(zd
, tx
, lr
, object
);
1808 ztest_object_unlock(zd
, object
);
1814 ztest_replay_write(ztest_ds_t
*zd
, lr_write_t
*lr
, boolean_t byteswap
)
1816 objset_t
*os
= zd
->zd_os
;
1817 void *data
= lr
+ 1; /* data follows lr */
1818 uint64_t offset
, length
;
1819 ztest_block_tag_t
*bt
= data
;
1820 ztest_block_tag_t
*bbt
;
1821 uint64_t gen
, txg
, lrtxg
, crtxg
;
1822 dmu_object_info_t doi
;
1825 arc_buf_t
*abuf
= NULL
;
1829 byteswap_uint64_array(lr
, sizeof (*lr
));
1831 offset
= lr
->lr_offset
;
1832 length
= lr
->lr_length
;
1834 /* If it's a dmu_sync() block, write the whole block */
1835 if (lr
->lr_common
.lrc_reclen
== sizeof (lr_write_t
)) {
1836 uint64_t blocksize
= BP_GET_LSIZE(&lr
->lr_blkptr
);
1837 if (length
< blocksize
) {
1838 offset
-= offset
% blocksize
;
1843 if (bt
->bt_magic
== BSWAP_64(BT_MAGIC
))
1844 byteswap_uint64_array(bt
, sizeof (*bt
));
1846 if (bt
->bt_magic
!= BT_MAGIC
)
1849 ztest_object_lock(zd
, lr
->lr_foid
, RL_READER
);
1850 rl
= ztest_range_lock(zd
, lr
->lr_foid
, offset
, length
, RL_WRITER
);
1852 VERIFY3U(0, ==, dmu_bonus_hold(os
, lr
->lr_foid
, FTAG
, &db
));
1854 dmu_object_info_from_db(db
, &doi
);
1856 bbt
= ztest_bt_bonus(db
);
1857 ASSERT3U(bbt
->bt_magic
, ==, BT_MAGIC
);
1859 crtxg
= bbt
->bt_crtxg
;
1860 lrtxg
= lr
->lr_common
.lrc_txg
;
1862 tx
= dmu_tx_create(os
);
1864 dmu_tx_hold_write(tx
, lr
->lr_foid
, offset
, length
);
1866 if (ztest_random(8) == 0 && length
== doi
.doi_data_block_size
&&
1867 P2PHASE(offset
, length
) == 0)
1868 abuf
= dmu_request_arcbuf(db
, length
);
1870 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
1873 dmu_return_arcbuf(abuf
);
1874 dmu_buf_rele(db
, FTAG
);
1875 ztest_range_unlock(zd
, rl
);
1876 ztest_object_unlock(zd
, lr
->lr_foid
);
1882 * Usually, verify the old data before writing new data --
1883 * but not always, because we also want to verify correct
1884 * behavior when the data was not recently read into cache.
1886 ASSERT(offset
% doi
.doi_data_block_size
== 0);
1887 if (ztest_random(4) != 0) {
1888 int prefetch
= ztest_random(2) ?
1889 DMU_READ_PREFETCH
: DMU_READ_NO_PREFETCH
;
1890 ztest_block_tag_t rbt
;
1892 VERIFY(dmu_read(os
, lr
->lr_foid
, offset
,
1893 sizeof (rbt
), &rbt
, prefetch
) == 0);
1894 if (rbt
.bt_magic
== BT_MAGIC
) {
1895 ztest_bt_verify(&rbt
, os
, lr
->lr_foid
, 0,
1896 offset
, gen
, txg
, crtxg
);
1901 * Writes can appear to be newer than the bonus buffer because
1902 * the ztest_get_data() callback does a dmu_read() of the
1903 * open-context data, which may be different than the data
1904 * as it was when the write was generated.
1906 if (zd
->zd_zilog
->zl_replay
) {
1907 ztest_bt_verify(bt
, os
, lr
->lr_foid
, 0, offset
,
1908 MAX(gen
, bt
->bt_gen
), MAX(txg
, lrtxg
),
1913 * Set the bt's gen/txg to the bonus buffer's gen/txg
1914 * so that all of the usual ASSERTs will work.
1916 ztest_bt_generate(bt
, os
, lr
->lr_foid
, 0, offset
, gen
, txg
,
1921 dmu_write(os
, lr
->lr_foid
, offset
, length
, data
, tx
);
1923 bcopy(data
, abuf
->b_data
, length
);
1924 dmu_assign_arcbuf(db
, offset
, abuf
, tx
);
1927 (void) ztest_log_write(zd
, tx
, lr
);
1929 dmu_buf_rele(db
, FTAG
);
1933 ztest_range_unlock(zd
, rl
);
1934 ztest_object_unlock(zd
, lr
->lr_foid
);
1940 ztest_replay_truncate(ztest_ds_t
*zd
, lr_truncate_t
*lr
, boolean_t byteswap
)
1942 objset_t
*os
= zd
->zd_os
;
1948 byteswap_uint64_array(lr
, sizeof (*lr
));
1950 ztest_object_lock(zd
, lr
->lr_foid
, RL_READER
);
1951 rl
= ztest_range_lock(zd
, lr
->lr_foid
, lr
->lr_offset
, lr
->lr_length
,
1954 tx
= dmu_tx_create(os
);
1956 dmu_tx_hold_free(tx
, lr
->lr_foid
, lr
->lr_offset
, lr
->lr_length
);
1958 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
1960 ztest_range_unlock(zd
, rl
);
1961 ztest_object_unlock(zd
, lr
->lr_foid
);
1965 VERIFY(dmu_free_range(os
, lr
->lr_foid
, lr
->lr_offset
,
1966 lr
->lr_length
, tx
) == 0);
1968 (void) ztest_log_truncate(zd
, tx
, lr
);
1972 ztest_range_unlock(zd
, rl
);
1973 ztest_object_unlock(zd
, lr
->lr_foid
);
1979 ztest_replay_setattr(ztest_ds_t
*zd
, lr_setattr_t
*lr
, boolean_t byteswap
)
1981 objset_t
*os
= zd
->zd_os
;
1984 ztest_block_tag_t
*bbt
;
1985 uint64_t txg
, lrtxg
, crtxg
, dnodesize
;
1988 byteswap_uint64_array(lr
, sizeof (*lr
));
1990 ztest_object_lock(zd
, lr
->lr_foid
, RL_WRITER
);
1992 VERIFY3U(0, ==, dmu_bonus_hold(os
, lr
->lr_foid
, FTAG
, &db
));
1994 tx
= dmu_tx_create(os
);
1995 dmu_tx_hold_bonus(tx
, lr
->lr_foid
);
1997 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
1999 dmu_buf_rele(db
, FTAG
);
2000 ztest_object_unlock(zd
, lr
->lr_foid
);
2004 bbt
= ztest_bt_bonus(db
);
2005 ASSERT3U(bbt
->bt_magic
, ==, BT_MAGIC
);
2006 crtxg
= bbt
->bt_crtxg
;
2007 lrtxg
= lr
->lr_common
.lrc_txg
;
2008 dnodesize
= bbt
->bt_dnodesize
;
2010 if (zd
->zd_zilog
->zl_replay
) {
2011 ASSERT(lr
->lr_size
!= 0);
2012 ASSERT(lr
->lr_mode
!= 0);
2016 * Randomly change the size and increment the generation.
2018 lr
->lr_size
= (ztest_random(db
->db_size
/ sizeof (*bbt
)) + 1) *
2020 lr
->lr_mode
= bbt
->bt_gen
+ 1;
2025 * Verify that the current bonus buffer is not newer than our txg.
2027 ztest_bt_verify(bbt
, os
, lr
->lr_foid
, dnodesize
, -1ULL, lr
->lr_mode
,
2028 MAX(txg
, lrtxg
), crtxg
);
2030 dmu_buf_will_dirty(db
, tx
);
2032 ASSERT3U(lr
->lr_size
, >=, sizeof (*bbt
));
2033 ASSERT3U(lr
->lr_size
, <=, db
->db_size
);
2034 VERIFY0(dmu_set_bonus(db
, lr
->lr_size
, tx
));
2035 bbt
= ztest_bt_bonus(db
);
2037 ztest_bt_generate(bbt
, os
, lr
->lr_foid
, dnodesize
, -1ULL, lr
->lr_mode
,
2039 ztest_fill_unused_bonus(db
, bbt
, lr
->lr_foid
, os
, bbt
->bt_gen
);
2040 dmu_buf_rele(db
, FTAG
);
2042 (void) ztest_log_setattr(zd
, tx
, lr
);
2046 ztest_object_unlock(zd
, lr
->lr_foid
);
2051 zil_replay_func_t ztest_replay_vector
[TX_MAX_TYPE
] = {
2052 NULL
, /* 0 no such transaction type */
2053 (zil_replay_func_t
)ztest_replay_create
, /* TX_CREATE */
2054 NULL
, /* TX_MKDIR */
2055 NULL
, /* TX_MKXATTR */
2056 NULL
, /* TX_SYMLINK */
2057 (zil_replay_func_t
)ztest_replay_remove
, /* TX_REMOVE */
2058 NULL
, /* TX_RMDIR */
2060 NULL
, /* TX_RENAME */
2061 (zil_replay_func_t
)ztest_replay_write
, /* TX_WRITE */
2062 (zil_replay_func_t
)ztest_replay_truncate
, /* TX_TRUNCATE */
2063 (zil_replay_func_t
)ztest_replay_setattr
, /* TX_SETATTR */
2065 NULL
, /* TX_CREATE_ACL */
2066 NULL
, /* TX_CREATE_ATTR */
2067 NULL
, /* TX_CREATE_ACL_ATTR */
2068 NULL
, /* TX_MKDIR_ACL */
2069 NULL
, /* TX_MKDIR_ATTR */
2070 NULL
, /* TX_MKDIR_ACL_ATTR */
2071 NULL
, /* TX_WRITE2 */
2075 * ZIL get_data callbacks
2077 typedef struct ztest_zgd_private
{
2081 } ztest_zgd_private_t
;
2084 ztest_get_done(zgd_t
*zgd
, int error
)
2086 ztest_zgd_private_t
*zzp
= zgd
->zgd_private
;
2087 ztest_ds_t
*zd
= zzp
->z_zd
;
2088 uint64_t object
= zzp
->z_object
;
2091 dmu_buf_rele(zgd
->zgd_db
, zgd
);
2093 ztest_range_unlock(zd
, zzp
->z_rl
);
2094 ztest_object_unlock(zd
, object
);
2096 if (error
== 0 && zgd
->zgd_bp
)
2097 zil_add_block(zgd
->zgd_zilog
, zgd
->zgd_bp
);
2099 umem_free(zgd
, sizeof (*zgd
));
2100 umem_free(zzp
, sizeof (*zzp
));
2104 ztest_get_data(void *arg
, lr_write_t
*lr
, char *buf
, zio_t
*zio
)
2106 ztest_ds_t
*zd
= arg
;
2107 objset_t
*os
= zd
->zd_os
;
2108 uint64_t object
= lr
->lr_foid
;
2109 uint64_t offset
= lr
->lr_offset
;
2110 uint64_t size
= lr
->lr_length
;
2111 uint64_t txg
= lr
->lr_common
.lrc_txg
;
2113 dmu_object_info_t doi
;
2117 ztest_zgd_private_t
*zgd_private
;
2119 ztest_object_lock(zd
, object
, RL_READER
);
2120 error
= dmu_bonus_hold(os
, object
, FTAG
, &db
);
2122 ztest_object_unlock(zd
, object
);
2126 crtxg
= ztest_bt_bonus(db
)->bt_crtxg
;
2128 if (crtxg
== 0 || crtxg
> txg
) {
2129 dmu_buf_rele(db
, FTAG
);
2130 ztest_object_unlock(zd
, object
);
2134 dmu_object_info_from_db(db
, &doi
);
2135 dmu_buf_rele(db
, FTAG
);
2138 zgd
= umem_zalloc(sizeof (*zgd
), UMEM_NOFAIL
);
2139 zgd
->zgd_zilog
= zd
->zd_zilog
;
2140 zgd_private
= umem_zalloc(sizeof (ztest_zgd_private_t
), UMEM_NOFAIL
);
2141 zgd_private
->z_zd
= zd
;
2142 zgd_private
->z_object
= object
;
2143 zgd
->zgd_private
= zgd_private
;
2145 if (buf
!= NULL
) { /* immediate write */
2146 zgd_private
->z_rl
= ztest_range_lock(zd
, object
, offset
, size
,
2148 zgd
->zgd_rl
= zgd_private
->z_rl
->z_rl
;
2150 error
= dmu_read(os
, object
, offset
, size
, buf
,
2151 DMU_READ_NO_PREFETCH
);
2154 size
= doi
.doi_data_block_size
;
2156 offset
= P2ALIGN(offset
, size
);
2158 ASSERT(offset
< size
);
2162 zgd_private
->z_rl
= ztest_range_lock(zd
, object
, offset
, size
,
2164 zgd
->zgd_rl
= zgd_private
->z_rl
->z_rl
;
2166 error
= dmu_buf_hold(os
, object
, offset
, zgd
, &db
,
2167 DMU_READ_NO_PREFETCH
);
2170 blkptr_t
*bp
= &lr
->lr_blkptr
;
2175 ASSERT(db
->db_offset
== offset
);
2176 ASSERT(db
->db_size
== size
);
2178 error
= dmu_sync(zio
, lr
->lr_common
.lrc_txg
,
2179 ztest_get_done
, zgd
);
2186 ztest_get_done(zgd
, error
);
2192 ztest_lr_alloc(size_t lrsize
, char *name
)
2195 size_t namesize
= name
? strlen(name
) + 1 : 0;
2197 lr
= umem_zalloc(lrsize
+ namesize
, UMEM_NOFAIL
);
2200 bcopy(name
, lr
+ lrsize
, namesize
);
2206 ztest_lr_free(void *lr
, size_t lrsize
, char *name
)
2208 size_t namesize
= name
? strlen(name
) + 1 : 0;
2210 umem_free(lr
, lrsize
+ namesize
);
2214 * Lookup a bunch of objects. Returns the number of objects not found.
2217 ztest_lookup(ztest_ds_t
*zd
, ztest_od_t
*od
, int count
)
2223 ASSERT(mutex_held(&zd
->zd_dirobj_lock
));
2225 for (i
= 0; i
< count
; i
++, od
++) {
2227 error
= zap_lookup(zd
->zd_os
, od
->od_dir
, od
->od_name
,
2228 sizeof (uint64_t), 1, &od
->od_object
);
2230 ASSERT(error
== ENOENT
);
2231 ASSERT(od
->od_object
== 0);
2235 ztest_block_tag_t
*bbt
;
2236 dmu_object_info_t doi
;
2238 ASSERT(od
->od_object
!= 0);
2239 ASSERT(missing
== 0); /* there should be no gaps */
2241 ztest_object_lock(zd
, od
->od_object
, RL_READER
);
2242 VERIFY3U(0, ==, dmu_bonus_hold(zd
->zd_os
,
2243 od
->od_object
, FTAG
, &db
));
2244 dmu_object_info_from_db(db
, &doi
);
2245 bbt
= ztest_bt_bonus(db
);
2246 ASSERT3U(bbt
->bt_magic
, ==, BT_MAGIC
);
2247 od
->od_type
= doi
.doi_type
;
2248 od
->od_blocksize
= doi
.doi_data_block_size
;
2249 od
->od_gen
= bbt
->bt_gen
;
2250 dmu_buf_rele(db
, FTAG
);
2251 ztest_object_unlock(zd
, od
->od_object
);
2259 ztest_create(ztest_ds_t
*zd
, ztest_od_t
*od
, int count
)
2264 ASSERT(mutex_held(&zd
->zd_dirobj_lock
));
2266 for (i
= 0; i
< count
; i
++, od
++) {
2273 lr_create_t
*lr
= ztest_lr_alloc(sizeof (*lr
), od
->od_name
);
2275 lr
->lr_doid
= od
->od_dir
;
2276 lr
->lr_foid
= 0; /* 0 to allocate, > 0 to claim */
2277 lr
->lrz_type
= od
->od_crtype
;
2278 lr
->lrz_blocksize
= od
->od_crblocksize
;
2279 lr
->lrz_ibshift
= ztest_random_ibshift();
2280 lr
->lrz_bonustype
= DMU_OT_UINT64_OTHER
;
2281 lr
->lrz_dnodesize
= od
->od_crdnodesize
;
2282 lr
->lr_gen
= od
->od_crgen
;
2283 lr
->lr_crtime
[0] = time(NULL
);
2285 if (ztest_replay_create(zd
, lr
, B_FALSE
) != 0) {
2286 ASSERT(missing
== 0);
2290 od
->od_object
= lr
->lr_foid
;
2291 od
->od_type
= od
->od_crtype
;
2292 od
->od_blocksize
= od
->od_crblocksize
;
2293 od
->od_gen
= od
->od_crgen
;
2294 ASSERT(od
->od_object
!= 0);
2297 ztest_lr_free(lr
, sizeof (*lr
), od
->od_name
);
2304 ztest_remove(ztest_ds_t
*zd
, ztest_od_t
*od
, int count
)
2310 ASSERT(mutex_held(&zd
->zd_dirobj_lock
));
2314 for (i
= count
- 1; i
>= 0; i
--, od
--) {
2321 * No object was found.
2323 if (od
->od_object
== 0)
2326 lr_remove_t
*lr
= ztest_lr_alloc(sizeof (*lr
), od
->od_name
);
2328 lr
->lr_doid
= od
->od_dir
;
2330 if ((error
= ztest_replay_remove(zd
, lr
, B_FALSE
)) != 0) {
2331 ASSERT3U(error
, ==, ENOSPC
);
2336 ztest_lr_free(lr
, sizeof (*lr
), od
->od_name
);
2343 ztest_write(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
, uint64_t size
,
2349 lr
= ztest_lr_alloc(sizeof (*lr
) + size
, NULL
);
2351 lr
->lr_foid
= object
;
2352 lr
->lr_offset
= offset
;
2353 lr
->lr_length
= size
;
2355 BP_ZERO(&lr
->lr_blkptr
);
2357 bcopy(data
, lr
+ 1, size
);
2359 error
= ztest_replay_write(zd
, lr
, B_FALSE
);
2361 ztest_lr_free(lr
, sizeof (*lr
) + size
, NULL
);
2367 ztest_truncate(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
, uint64_t size
)
2372 lr
= ztest_lr_alloc(sizeof (*lr
), NULL
);
2374 lr
->lr_foid
= object
;
2375 lr
->lr_offset
= offset
;
2376 lr
->lr_length
= size
;
2378 error
= ztest_replay_truncate(zd
, lr
, B_FALSE
);
2380 ztest_lr_free(lr
, sizeof (*lr
), NULL
);
2386 ztest_setattr(ztest_ds_t
*zd
, uint64_t object
)
2391 lr
= ztest_lr_alloc(sizeof (*lr
), NULL
);
2393 lr
->lr_foid
= object
;
2397 error
= ztest_replay_setattr(zd
, lr
, B_FALSE
);
2399 ztest_lr_free(lr
, sizeof (*lr
), NULL
);
2405 ztest_prealloc(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
, uint64_t size
)
2407 objset_t
*os
= zd
->zd_os
;
2412 txg_wait_synced(dmu_objset_pool(os
), 0);
2414 ztest_object_lock(zd
, object
, RL_READER
);
2415 rl
= ztest_range_lock(zd
, object
, offset
, size
, RL_WRITER
);
2417 tx
= dmu_tx_create(os
);
2419 dmu_tx_hold_write(tx
, object
, offset
, size
);
2421 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
2424 dmu_prealloc(os
, object
, offset
, size
, tx
);
2426 txg_wait_synced(dmu_objset_pool(os
), txg
);
2428 (void) dmu_free_long_range(os
, object
, offset
, size
);
2431 ztest_range_unlock(zd
, rl
);
2432 ztest_object_unlock(zd
, object
);
2436 ztest_io(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
)
2439 ztest_block_tag_t wbt
;
2440 dmu_object_info_t doi
;
2441 enum ztest_io_type io_type
;
2445 VERIFY(dmu_object_info(zd
->zd_os
, object
, &doi
) == 0);
2446 blocksize
= doi
.doi_data_block_size
;
2447 data
= umem_alloc(blocksize
, UMEM_NOFAIL
);
2450 * Pick an i/o type at random, biased toward writing block tags.
2452 io_type
= ztest_random(ZTEST_IO_TYPES
);
2453 if (ztest_random(2) == 0)
2454 io_type
= ZTEST_IO_WRITE_TAG
;
2456 (void) rw_rdlock(&zd
->zd_zilog_lock
);
2460 case ZTEST_IO_WRITE_TAG
:
2461 ztest_bt_generate(&wbt
, zd
->zd_os
, object
, doi
.doi_dnodesize
,
2463 (void) ztest_write(zd
, object
, offset
, sizeof (wbt
), &wbt
);
2466 case ZTEST_IO_WRITE_PATTERN
:
2467 (void) memset(data
, 'a' + (object
+ offset
) % 5, blocksize
);
2468 if (ztest_random(2) == 0) {
2470 * Induce fletcher2 collisions to ensure that
2471 * zio_ddt_collision() detects and resolves them
2472 * when using fletcher2-verify for deduplication.
2474 ((uint64_t *)data
)[0] ^= 1ULL << 63;
2475 ((uint64_t *)data
)[4] ^= 1ULL << 63;
2477 (void) ztest_write(zd
, object
, offset
, blocksize
, data
);
2480 case ZTEST_IO_WRITE_ZEROES
:
2481 bzero(data
, blocksize
);
2482 (void) ztest_write(zd
, object
, offset
, blocksize
, data
);
2485 case ZTEST_IO_TRUNCATE
:
2486 (void) ztest_truncate(zd
, object
, offset
, blocksize
);
2489 case ZTEST_IO_SETATTR
:
2490 (void) ztest_setattr(zd
, object
);
2495 case ZTEST_IO_REWRITE
:
2496 (void) rw_rdlock(&ztest_name_lock
);
2497 err
= ztest_dsl_prop_set_uint64(zd
->zd_name
,
2498 ZFS_PROP_CHECKSUM
, spa_dedup_checksum(ztest_spa
),
2500 VERIFY(err
== 0 || err
== ENOSPC
);
2501 err
= ztest_dsl_prop_set_uint64(zd
->zd_name
,
2502 ZFS_PROP_COMPRESSION
,
2503 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION
),
2505 VERIFY(err
== 0 || err
== ENOSPC
);
2506 (void) rw_unlock(&ztest_name_lock
);
2508 VERIFY0(dmu_read(zd
->zd_os
, object
, offset
, blocksize
, data
,
2509 DMU_READ_NO_PREFETCH
));
2511 (void) ztest_write(zd
, object
, offset
, blocksize
, data
);
2515 (void) rw_unlock(&zd
->zd_zilog_lock
);
2517 umem_free(data
, blocksize
);
2521 * Initialize an object description template.
2524 ztest_od_init(ztest_od_t
*od
, uint64_t id
, char *tag
, uint64_t index
,
2525 dmu_object_type_t type
, uint64_t blocksize
, uint64_t dnodesize
,
2528 od
->od_dir
= ZTEST_DIROBJ
;
2531 od
->od_crtype
= type
;
2532 od
->od_crblocksize
= blocksize
? blocksize
: ztest_random_blocksize();
2533 od
->od_crdnodesize
= dnodesize
? dnodesize
: ztest_random_dnodesize();
2536 od
->od_type
= DMU_OT_NONE
;
2537 od
->od_blocksize
= 0;
2540 (void) snprintf(od
->od_name
, sizeof (od
->od_name
), "%s(%lld)[%llu]",
2541 tag
, (longlong_t
)id
, (u_longlong_t
)index
);
2545 * Lookup or create the objects for a test using the od template.
2546 * If the objects do not all exist, or if 'remove' is specified,
2547 * remove any existing objects and create new ones. Otherwise,
2548 * use the existing objects.
2551 ztest_object_init(ztest_ds_t
*zd
, ztest_od_t
*od
, size_t size
, boolean_t remove
)
2553 int count
= size
/ sizeof (*od
);
2556 mutex_enter(&zd
->zd_dirobj_lock
);
2557 if ((ztest_lookup(zd
, od
, count
) != 0 || remove
) &&
2558 (ztest_remove(zd
, od
, count
) != 0 ||
2559 ztest_create(zd
, od
, count
) != 0))
2562 mutex_exit(&zd
->zd_dirobj_lock
);
2569 ztest_zil_commit(ztest_ds_t
*zd
, uint64_t id
)
2571 zilog_t
*zilog
= zd
->zd_zilog
;
2573 (void) rw_rdlock(&zd
->zd_zilog_lock
);
2575 zil_commit(zilog
, ztest_random(ZTEST_OBJECTS
));
2578 * Remember the committed values in zd, which is in parent/child
2579 * shared memory. If we die, the next iteration of ztest_run()
2580 * will verify that the log really does contain this record.
2582 mutex_enter(&zilog
->zl_lock
);
2583 ASSERT(zd
->zd_shared
!= NULL
);
2584 ASSERT3U(zd
->zd_shared
->zd_seq
, <=, zilog
->zl_commit_lr_seq
);
2585 zd
->zd_shared
->zd_seq
= zilog
->zl_commit_lr_seq
;
2586 mutex_exit(&zilog
->zl_lock
);
2588 (void) rw_unlock(&zd
->zd_zilog_lock
);
2592 * This function is designed to simulate the operations that occur during a
2593 * mount/unmount operation. We hold the dataset across these operations in an
2594 * attempt to expose any implicit assumptions about ZIL management.
2598 ztest_zil_remount(ztest_ds_t
*zd
, uint64_t id
)
2600 objset_t
*os
= zd
->zd_os
;
2603 * We grab the zd_dirobj_lock to ensure that no other thread is
2604 * updating the zil (i.e. adding in-memory log records) and the
2605 * zd_zilog_lock to block any I/O.
2607 mutex_enter(&zd
->zd_dirobj_lock
);
2608 (void) rw_wrlock(&zd
->zd_zilog_lock
);
2610 /* zfsvfs_teardown() */
2611 zil_close(zd
->zd_zilog
);
2613 /* zfsvfs_setup() */
2614 VERIFY(zil_open(os
, ztest_get_data
) == zd
->zd_zilog
);
2615 zil_replay(os
, zd
, ztest_replay_vector
);
2617 (void) rw_unlock(&zd
->zd_zilog_lock
);
2618 mutex_exit(&zd
->zd_dirobj_lock
);
2622 * Verify that we can't destroy an active pool, create an existing pool,
2623 * or create a pool with a bad vdev spec.
2627 ztest_spa_create_destroy(ztest_ds_t
*zd
, uint64_t id
)
2629 ztest_shared_opts_t
*zo
= &ztest_opts
;
2633 if (zo
->zo_mmp_test
)
2637 * Attempt to create using a bad file.
2639 nvroot
= make_vdev_root("/dev/bogus", NULL
, NULL
, 0, 0, 0, 0, 0, 1);
2640 VERIFY3U(ENOENT
, ==,
2641 spa_create("ztest_bad_file", nvroot
, NULL
, NULL
));
2642 nvlist_free(nvroot
);
2645 * Attempt to create using a bad mirror.
2647 nvroot
= make_vdev_root("/dev/bogus", NULL
, NULL
, 0, 0, 0, 0, 2, 1);
2648 VERIFY3U(ENOENT
, ==,
2649 spa_create("ztest_bad_mirror", nvroot
, NULL
, NULL
));
2650 nvlist_free(nvroot
);
2653 * Attempt to create an existing pool. It shouldn't matter
2654 * what's in the nvroot; we should fail with EEXIST.
2656 (void) rw_rdlock(&ztest_name_lock
);
2657 nvroot
= make_vdev_root("/dev/bogus", NULL
, NULL
, 0, 0, 0, 0, 0, 1);
2658 VERIFY3U(EEXIST
, ==, spa_create(zo
->zo_pool
, nvroot
, NULL
, NULL
));
2659 nvlist_free(nvroot
);
2660 VERIFY3U(0, ==, spa_open(zo
->zo_pool
, &spa
, FTAG
));
2661 VERIFY3U(EBUSY
, ==, spa_destroy(zo
->zo_pool
));
2662 spa_close(spa
, FTAG
);
2664 (void) rw_unlock(&ztest_name_lock
);
2668 * Start and then stop the MMP threads to ensure the startup and shutdown code
2669 * works properly. Actual protection and property-related code tested via ZTS.
2673 ztest_mmp_enable_disable(ztest_ds_t
*zd
, uint64_t id
)
2675 ztest_shared_opts_t
*zo
= &ztest_opts
;
2676 spa_t
*spa
= ztest_spa
;
2678 if (zo
->zo_mmp_test
)
2681 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
2682 mutex_enter(&spa
->spa_props_lock
);
2684 if (!spa_multihost(spa
)) {
2685 spa
->spa_multihost
= B_TRUE
;
2686 mmp_thread_start(spa
);
2689 mutex_exit(&spa
->spa_props_lock
);
2690 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
2692 txg_wait_synced(spa_get_dsl(spa
), 0);
2693 mmp_signal_all_threads();
2694 txg_wait_synced(spa_get_dsl(spa
), 0);
2696 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
2697 mutex_enter(&spa
->spa_props_lock
);
2699 if (spa_multihost(spa
)) {
2700 mmp_thread_stop(spa
);
2701 spa
->spa_multihost
= B_FALSE
;
2704 mutex_exit(&spa
->spa_props_lock
);
2705 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
2710 ztest_spa_upgrade(ztest_ds_t
*zd
, uint64_t id
)
2713 uint64_t initial_version
= SPA_VERSION_INITIAL
;
2714 uint64_t version
, newversion
;
2715 nvlist_t
*nvroot
, *props
;
2718 if (ztest_opts
.zo_mmp_test
)
2721 mutex_enter(&ztest_vdev_lock
);
2722 name
= kmem_asprintf("%s_upgrade", ztest_opts
.zo_pool
);
2725 * Clean up from previous runs.
2727 (void) spa_destroy(name
);
2729 nvroot
= make_vdev_root(NULL
, NULL
, name
, ztest_opts
.zo_vdev_size
, 0,
2730 0, ztest_opts
.zo_raidz
, ztest_opts
.zo_mirrors
, 1);
2733 * If we're configuring a RAIDZ device then make sure that the
2734 * the initial version is capable of supporting that feature.
2736 switch (ztest_opts
.zo_raidz_parity
) {
2739 initial_version
= SPA_VERSION_INITIAL
;
2742 initial_version
= SPA_VERSION_RAIDZ2
;
2745 initial_version
= SPA_VERSION_RAIDZ3
;
2750 * Create a pool with a spa version that can be upgraded. Pick
2751 * a value between initial_version and SPA_VERSION_BEFORE_FEATURES.
2754 version
= ztest_random_spa_version(initial_version
);
2755 } while (version
> SPA_VERSION_BEFORE_FEATURES
);
2757 props
= fnvlist_alloc();
2758 fnvlist_add_uint64(props
,
2759 zpool_prop_to_name(ZPOOL_PROP_VERSION
), version
);
2760 VERIFY3S(spa_create(name
, nvroot
, props
, NULL
), ==, 0);
2761 fnvlist_free(nvroot
);
2762 fnvlist_free(props
);
2764 VERIFY3S(spa_open(name
, &spa
, FTAG
), ==, 0);
2765 VERIFY3U(spa_version(spa
), ==, version
);
2766 newversion
= ztest_random_spa_version(version
+ 1);
2768 if (ztest_opts
.zo_verbose
>= 4) {
2769 (void) printf("upgrading spa version from %llu to %llu\n",
2770 (u_longlong_t
)version
, (u_longlong_t
)newversion
);
2773 spa_upgrade(spa
, newversion
);
2774 VERIFY3U(spa_version(spa
), >, version
);
2775 VERIFY3U(spa_version(spa
), ==, fnvlist_lookup_uint64(spa
->spa_config
,
2776 zpool_prop_to_name(ZPOOL_PROP_VERSION
)));
2777 spa_close(spa
, FTAG
);
2780 mutex_exit(&ztest_vdev_lock
);
2784 vdev_lookup_by_path(vdev_t
*vd
, const char *path
)
2789 if (vd
->vdev_path
!= NULL
&& strcmp(path
, vd
->vdev_path
) == 0)
2792 for (c
= 0; c
< vd
->vdev_children
; c
++)
2793 if ((mvd
= vdev_lookup_by_path(vd
->vdev_child
[c
], path
)) !=
2801 * Find the first available hole which can be used as a top-level.
2804 find_vdev_hole(spa_t
*spa
)
2806 vdev_t
*rvd
= spa
->spa_root_vdev
;
2809 ASSERT(spa_config_held(spa
, SCL_VDEV
, RW_READER
) == SCL_VDEV
);
2811 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
2812 vdev_t
*cvd
= rvd
->vdev_child
[c
];
2814 if (cvd
->vdev_ishole
)
2821 * Verify that vdev_add() works as expected.
2825 ztest_vdev_add_remove(ztest_ds_t
*zd
, uint64_t id
)
2827 ztest_shared_t
*zs
= ztest_shared
;
2828 spa_t
*spa
= ztest_spa
;
2834 if (ztest_opts
.zo_mmp_test
)
2837 mutex_enter(&ztest_vdev_lock
);
2838 leaves
= MAX(zs
->zs_mirrors
+ zs
->zs_splits
, 1) * ztest_opts
.zo_raidz
;
2840 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
2842 ztest_shared
->zs_vdev_next_leaf
= find_vdev_hole(spa
) * leaves
;
2845 * If we have slogs then remove them 1/4 of the time.
2847 if (spa_has_slogs(spa
) && ztest_random(4) == 0) {
2849 * Grab the guid from the head of the log class rotor.
2851 guid
= spa_log_class(spa
)->mc_rotor
->mg_vd
->vdev_guid
;
2853 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
2856 * We have to grab the zs_name_lock as writer to
2857 * prevent a race between removing a slog (dmu_objset_find)
2858 * and destroying a dataset. Removing the slog will
2859 * grab a reference on the dataset which may cause
2860 * dsl_destroy_head() to fail with EBUSY thus
2861 * leaving the dataset in an inconsistent state.
2863 rw_wrlock(&ztest_name_lock
);
2864 error
= spa_vdev_remove(spa
, guid
, B_FALSE
);
2865 rw_unlock(&ztest_name_lock
);
2867 if (error
&& error
!= EEXIST
)
2868 fatal(0, "spa_vdev_remove() = %d", error
);
2870 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
2873 * Make 1/4 of the devices be log devices.
2875 nvroot
= make_vdev_root(NULL
, NULL
, NULL
,
2876 ztest_opts
.zo_vdev_size
, 0,
2877 ztest_random(4) == 0, ztest_opts
.zo_raidz
,
2880 error
= spa_vdev_add(spa
, nvroot
);
2881 nvlist_free(nvroot
);
2883 if (error
== ENOSPC
)
2884 ztest_record_enospc("spa_vdev_add");
2885 else if (error
!= 0)
2886 fatal(0, "spa_vdev_add() = %d", error
);
2889 mutex_exit(&ztest_vdev_lock
);
2893 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
2897 ztest_vdev_aux_add_remove(ztest_ds_t
*zd
, uint64_t id
)
2899 ztest_shared_t
*zs
= ztest_shared
;
2900 spa_t
*spa
= ztest_spa
;
2901 vdev_t
*rvd
= spa
->spa_root_vdev
;
2902 spa_aux_vdev_t
*sav
;
2908 if (ztest_opts
.zo_mmp_test
)
2911 path
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
2913 if (ztest_random(2) == 0) {
2914 sav
= &spa
->spa_spares
;
2915 aux
= ZPOOL_CONFIG_SPARES
;
2917 sav
= &spa
->spa_l2cache
;
2918 aux
= ZPOOL_CONFIG_L2CACHE
;
2921 mutex_enter(&ztest_vdev_lock
);
2923 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
2925 if (sav
->sav_count
!= 0 && ztest_random(4) == 0) {
2927 * Pick a random device to remove.
2929 guid
= sav
->sav_vdevs
[ztest_random(sav
->sav_count
)]->vdev_guid
;
2932 * Find an unused device we can add.
2934 zs
->zs_vdev_aux
= 0;
2937 (void) snprintf(path
, MAXPATHLEN
, ztest_aux_template
,
2938 ztest_opts
.zo_dir
, ztest_opts
.zo_pool
, aux
,
2940 for (c
= 0; c
< sav
->sav_count
; c
++)
2941 if (strcmp(sav
->sav_vdevs
[c
]->vdev_path
,
2944 if (c
== sav
->sav_count
&&
2945 vdev_lookup_by_path(rvd
, path
) == NULL
)
2951 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
2957 nvlist_t
*nvroot
= make_vdev_root(NULL
, aux
, NULL
,
2958 (ztest_opts
.zo_vdev_size
* 5) / 4, 0, 0, 0, 0, 1);
2959 error
= spa_vdev_add(spa
, nvroot
);
2961 fatal(0, "spa_vdev_add(%p) = %d", nvroot
, error
);
2962 nvlist_free(nvroot
);
2965 * Remove an existing device. Sometimes, dirty its
2966 * vdev state first to make sure we handle removal
2967 * of devices that have pending state changes.
2969 if (ztest_random(2) == 0)
2970 (void) vdev_online(spa
, guid
, 0, NULL
);
2972 error
= spa_vdev_remove(spa
, guid
, B_FALSE
);
2973 if (error
!= 0 && error
!= EBUSY
)
2974 fatal(0, "spa_vdev_remove(%llu) = %d", guid
, error
);
2977 mutex_exit(&ztest_vdev_lock
);
2979 umem_free(path
, MAXPATHLEN
);
2983 * split a pool if it has mirror tlvdevs
2987 ztest_split_pool(ztest_ds_t
*zd
, uint64_t id
)
2989 ztest_shared_t
*zs
= ztest_shared
;
2990 spa_t
*spa
= ztest_spa
;
2991 vdev_t
*rvd
= spa
->spa_root_vdev
;
2992 nvlist_t
*tree
, **child
, *config
, *split
, **schild
;
2993 uint_t c
, children
, schildren
= 0, lastlogid
= 0;
2996 if (ztest_opts
.zo_mmp_test
)
2999 mutex_enter(&ztest_vdev_lock
);
3001 /* ensure we have a useable config; mirrors of raidz aren't supported */
3002 if (zs
->zs_mirrors
< 3 || ztest_opts
.zo_raidz
> 1) {
3003 mutex_exit(&ztest_vdev_lock
);
3007 /* clean up the old pool, if any */
3008 (void) spa_destroy("splitp");
3010 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
3012 /* generate a config from the existing config */
3013 mutex_enter(&spa
->spa_props_lock
);
3014 VERIFY(nvlist_lookup_nvlist(spa
->spa_config
, ZPOOL_CONFIG_VDEV_TREE
,
3016 mutex_exit(&spa
->spa_props_lock
);
3018 VERIFY(nvlist_lookup_nvlist_array(tree
, ZPOOL_CONFIG_CHILDREN
, &child
,
3021 schild
= malloc(rvd
->vdev_children
* sizeof (nvlist_t
*));
3022 for (c
= 0; c
< children
; c
++) {
3023 vdev_t
*tvd
= rvd
->vdev_child
[c
];
3027 if (tvd
->vdev_islog
|| tvd
->vdev_ops
== &vdev_hole_ops
) {
3028 VERIFY(nvlist_alloc(&schild
[schildren
], NV_UNIQUE_NAME
,
3030 VERIFY(nvlist_add_string(schild
[schildren
],
3031 ZPOOL_CONFIG_TYPE
, VDEV_TYPE_HOLE
) == 0);
3032 VERIFY(nvlist_add_uint64(schild
[schildren
],
3033 ZPOOL_CONFIG_IS_HOLE
, 1) == 0);
3035 lastlogid
= schildren
;
3040 VERIFY(nvlist_lookup_nvlist_array(child
[c
],
3041 ZPOOL_CONFIG_CHILDREN
, &mchild
, &mchildren
) == 0);
3042 VERIFY(nvlist_dup(mchild
[0], &schild
[schildren
++], 0) == 0);
3045 /* OK, create a config that can be used to split */
3046 VERIFY(nvlist_alloc(&split
, NV_UNIQUE_NAME
, 0) == 0);
3047 VERIFY(nvlist_add_string(split
, ZPOOL_CONFIG_TYPE
,
3048 VDEV_TYPE_ROOT
) == 0);
3049 VERIFY(nvlist_add_nvlist_array(split
, ZPOOL_CONFIG_CHILDREN
, schild
,
3050 lastlogid
!= 0 ? lastlogid
: schildren
) == 0);
3052 VERIFY(nvlist_alloc(&config
, NV_UNIQUE_NAME
, 0) == 0);
3053 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, split
) == 0);
3055 for (c
= 0; c
< schildren
; c
++)
3056 nvlist_free(schild
[c
]);
3060 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3062 (void) rw_wrlock(&ztest_name_lock
);
3063 error
= spa_vdev_split_mirror(spa
, "splitp", config
, NULL
, B_FALSE
);
3064 (void) rw_unlock(&ztest_name_lock
);
3066 nvlist_free(config
);
3069 (void) printf("successful split - results:\n");
3070 mutex_enter(&spa_namespace_lock
);
3071 show_pool_stats(spa
);
3072 show_pool_stats(spa_lookup("splitp"));
3073 mutex_exit(&spa_namespace_lock
);
3077 mutex_exit(&ztest_vdev_lock
);
3082 * Verify that we can attach and detach devices.
3086 ztest_vdev_attach_detach(ztest_ds_t
*zd
, uint64_t id
)
3088 ztest_shared_t
*zs
= ztest_shared
;
3089 spa_t
*spa
= ztest_spa
;
3090 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
3091 vdev_t
*rvd
= spa
->spa_root_vdev
;
3092 vdev_t
*oldvd
, *newvd
, *pvd
;
3096 uint64_t ashift
= ztest_get_ashift();
3097 uint64_t oldguid
, pguid
;
3098 uint64_t oldsize
, newsize
;
3099 char *oldpath
, *newpath
;
3101 int oldvd_has_siblings
= B_FALSE
;
3102 int newvd_is_spare
= B_FALSE
;
3104 int error
, expected_error
;
3106 if (ztest_opts
.zo_mmp_test
)
3109 oldpath
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
3110 newpath
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
3112 mutex_enter(&ztest_vdev_lock
);
3113 leaves
= MAX(zs
->zs_mirrors
, 1) * ztest_opts
.zo_raidz
;
3115 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
3118 * Decide whether to do an attach or a replace.
3120 replacing
= ztest_random(2);
3123 * Pick a random top-level vdev.
3125 top
= ztest_random_vdev_top(spa
, B_TRUE
);
3128 * Pick a random leaf within it.
3130 leaf
= ztest_random(leaves
);
3135 oldvd
= rvd
->vdev_child
[top
];
3136 if (zs
->zs_mirrors
>= 1) {
3137 ASSERT(oldvd
->vdev_ops
== &vdev_mirror_ops
);
3138 ASSERT(oldvd
->vdev_children
>= zs
->zs_mirrors
);
3139 oldvd
= oldvd
->vdev_child
[leaf
/ ztest_opts
.zo_raidz
];
3141 if (ztest_opts
.zo_raidz
> 1) {
3142 ASSERT(oldvd
->vdev_ops
== &vdev_raidz_ops
);
3143 ASSERT(oldvd
->vdev_children
== ztest_opts
.zo_raidz
);
3144 oldvd
= oldvd
->vdev_child
[leaf
% ztest_opts
.zo_raidz
];
3148 * If we're already doing an attach or replace, oldvd may be a
3149 * mirror vdev -- in which case, pick a random child.
3151 while (oldvd
->vdev_children
!= 0) {
3152 oldvd_has_siblings
= B_TRUE
;
3153 ASSERT(oldvd
->vdev_children
>= 2);
3154 oldvd
= oldvd
->vdev_child
[ztest_random(oldvd
->vdev_children
)];
3157 oldguid
= oldvd
->vdev_guid
;
3158 oldsize
= vdev_get_min_asize(oldvd
);
3159 oldvd_is_log
= oldvd
->vdev_top
->vdev_islog
;
3160 (void) strcpy(oldpath
, oldvd
->vdev_path
);
3161 pvd
= oldvd
->vdev_parent
;
3162 pguid
= pvd
->vdev_guid
;
3165 * If oldvd has siblings, then half of the time, detach it.
3167 if (oldvd_has_siblings
&& ztest_random(2) == 0) {
3168 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3169 error
= spa_vdev_detach(spa
, oldguid
, pguid
, B_FALSE
);
3170 if (error
!= 0 && error
!= ENODEV
&& error
!= EBUSY
&&
3172 fatal(0, "detach (%s) returned %d", oldpath
, error
);
3177 * For the new vdev, choose with equal probability between the two
3178 * standard paths (ending in either 'a' or 'b') or a random hot spare.
3180 if (sav
->sav_count
!= 0 && ztest_random(3) == 0) {
3181 newvd
= sav
->sav_vdevs
[ztest_random(sav
->sav_count
)];
3182 newvd_is_spare
= B_TRUE
;
3183 (void) strcpy(newpath
, newvd
->vdev_path
);
3185 (void) snprintf(newpath
, MAXPATHLEN
, ztest_dev_template
,
3186 ztest_opts
.zo_dir
, ztest_opts
.zo_pool
,
3187 top
* leaves
+ leaf
);
3188 if (ztest_random(2) == 0)
3189 newpath
[strlen(newpath
) - 1] = 'b';
3190 newvd
= vdev_lookup_by_path(rvd
, newpath
);
3194 newsize
= vdev_get_min_asize(newvd
);
3197 * Make newsize a little bigger or smaller than oldsize.
3198 * If it's smaller, the attach should fail.
3199 * If it's larger, and we're doing a replace,
3200 * we should get dynamic LUN growth when we're done.
3202 newsize
= 10 * oldsize
/ (9 + ztest_random(3));
3206 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
3207 * unless it's a replace; in that case any non-replacing parent is OK.
3209 * If newvd is already part of the pool, it should fail with EBUSY.
3211 * If newvd is too small, it should fail with EOVERFLOW.
3213 if (pvd
->vdev_ops
!= &vdev_mirror_ops
&&
3214 pvd
->vdev_ops
!= &vdev_root_ops
&& (!replacing
||
3215 pvd
->vdev_ops
== &vdev_replacing_ops
||
3216 pvd
->vdev_ops
== &vdev_spare_ops
))
3217 expected_error
= ENOTSUP
;
3218 else if (newvd_is_spare
&& (!replacing
|| oldvd_is_log
))
3219 expected_error
= ENOTSUP
;
3220 else if (newvd
== oldvd
)
3221 expected_error
= replacing
? 0 : EBUSY
;
3222 else if (vdev_lookup_by_path(rvd
, newpath
) != NULL
)
3223 expected_error
= EBUSY
;
3224 else if (newsize
< oldsize
)
3225 expected_error
= EOVERFLOW
;
3226 else if (ashift
> oldvd
->vdev_top
->vdev_ashift
)
3227 expected_error
= EDOM
;
3231 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3234 * Build the nvlist describing newpath.
3236 root
= make_vdev_root(newpath
, NULL
, NULL
, newvd
== NULL
? newsize
: 0,
3237 ashift
, 0, 0, 0, 1);
3239 error
= spa_vdev_attach(spa
, oldguid
, root
, replacing
);
3244 * If our parent was the replacing vdev, but the replace completed,
3245 * then instead of failing with ENOTSUP we may either succeed,
3246 * fail with ENODEV, or fail with EOVERFLOW.
3248 if (expected_error
== ENOTSUP
&&
3249 (error
== 0 || error
== ENODEV
|| error
== EOVERFLOW
))
3250 expected_error
= error
;
3253 * If someone grew the LUN, the replacement may be too small.
3255 if (error
== EOVERFLOW
|| error
== EBUSY
)
3256 expected_error
= error
;
3258 /* XXX workaround 6690467 */
3259 if (error
!= expected_error
&& expected_error
!= EBUSY
) {
3260 fatal(0, "attach (%s %llu, %s %llu, %d) "
3261 "returned %d, expected %d",
3262 oldpath
, oldsize
, newpath
,
3263 newsize
, replacing
, error
, expected_error
);
3266 mutex_exit(&ztest_vdev_lock
);
3268 umem_free(oldpath
, MAXPATHLEN
);
3269 umem_free(newpath
, MAXPATHLEN
);
3273 * Callback function which expands the physical size of the vdev.
3276 grow_vdev(vdev_t
*vd
, void *arg
)
3278 ASSERTV(spa_t
*spa
= vd
->vdev_spa
);
3279 size_t *newsize
= arg
;
3283 ASSERT(spa_config_held(spa
, SCL_STATE
, RW_READER
) == SCL_STATE
);
3284 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
3286 if ((fd
= open(vd
->vdev_path
, O_RDWR
)) == -1)
3289 fsize
= lseek(fd
, 0, SEEK_END
);
3290 VERIFY(ftruncate(fd
, *newsize
) == 0);
3292 if (ztest_opts
.zo_verbose
>= 6) {
3293 (void) printf("%s grew from %lu to %lu bytes\n",
3294 vd
->vdev_path
, (ulong_t
)fsize
, (ulong_t
)*newsize
);
3301 * Callback function which expands a given vdev by calling vdev_online().
3305 online_vdev(vdev_t
*vd
, void *arg
)
3307 spa_t
*spa
= vd
->vdev_spa
;
3308 vdev_t
*tvd
= vd
->vdev_top
;
3309 uint64_t guid
= vd
->vdev_guid
;
3310 uint64_t generation
= spa
->spa_config_generation
+ 1;
3311 vdev_state_t newstate
= VDEV_STATE_UNKNOWN
;
3314 ASSERT(spa_config_held(spa
, SCL_STATE
, RW_READER
) == SCL_STATE
);
3315 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
3317 /* Calling vdev_online will initialize the new metaslabs */
3318 spa_config_exit(spa
, SCL_STATE
, spa
);
3319 error
= vdev_online(spa
, guid
, ZFS_ONLINE_EXPAND
, &newstate
);
3320 spa_config_enter(spa
, SCL_STATE
, spa
, RW_READER
);
3323 * If vdev_online returned an error or the underlying vdev_open
3324 * failed then we abort the expand. The only way to know that
3325 * vdev_open fails is by checking the returned newstate.
3327 if (error
|| newstate
!= VDEV_STATE_HEALTHY
) {
3328 if (ztest_opts
.zo_verbose
>= 5) {
3329 (void) printf("Unable to expand vdev, state %llu, "
3330 "error %d\n", (u_longlong_t
)newstate
, error
);
3334 ASSERT3U(newstate
, ==, VDEV_STATE_HEALTHY
);
3337 * Since we dropped the lock we need to ensure that we're
3338 * still talking to the original vdev. It's possible this
3339 * vdev may have been detached/replaced while we were
3340 * trying to online it.
3342 if (generation
!= spa
->spa_config_generation
) {
3343 if (ztest_opts
.zo_verbose
>= 5) {
3344 (void) printf("vdev configuration has changed, "
3345 "guid %llu, state %llu, expected gen %llu, "
3348 (u_longlong_t
)tvd
->vdev_state
,
3349 (u_longlong_t
)generation
,
3350 (u_longlong_t
)spa
->spa_config_generation
);
3358 * Traverse the vdev tree calling the supplied function.
3359 * We continue to walk the tree until we either have walked all
3360 * children or we receive a non-NULL return from the callback.
3361 * If a NULL callback is passed, then we just return back the first
3362 * leaf vdev we encounter.
3365 vdev_walk_tree(vdev_t
*vd
, vdev_t
*(*func
)(vdev_t
*, void *), void *arg
)
3369 if (vd
->vdev_ops
->vdev_op_leaf
) {
3373 return (func(vd
, arg
));
3376 for (c
= 0; c
< vd
->vdev_children
; c
++) {
3377 vdev_t
*cvd
= vd
->vdev_child
[c
];
3378 if ((cvd
= vdev_walk_tree(cvd
, func
, arg
)) != NULL
)
3385 * Verify that dynamic LUN growth works as expected.
3389 ztest_vdev_LUN_growth(ztest_ds_t
*zd
, uint64_t id
)
3391 spa_t
*spa
= ztest_spa
;
3393 metaslab_class_t
*mc
;
3394 metaslab_group_t
*mg
;
3395 size_t psize
, newsize
;
3397 uint64_t old_class_space
, new_class_space
, old_ms_count
, new_ms_count
;
3399 mutex_enter(&ztest_vdev_lock
);
3400 spa_config_enter(spa
, SCL_STATE
, spa
, RW_READER
);
3402 top
= ztest_random_vdev_top(spa
, B_TRUE
);
3404 tvd
= spa
->spa_root_vdev
->vdev_child
[top
];
3407 old_ms_count
= tvd
->vdev_ms_count
;
3408 old_class_space
= metaslab_class_get_space(mc
);
3411 * Determine the size of the first leaf vdev associated with
3412 * our top-level device.
3414 vd
= vdev_walk_tree(tvd
, NULL
, NULL
);
3415 ASSERT3P(vd
, !=, NULL
);
3416 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
3418 psize
= vd
->vdev_psize
;
3421 * We only try to expand the vdev if it's healthy, less than 4x its
3422 * original size, and it has a valid psize.
3424 if (tvd
->vdev_state
!= VDEV_STATE_HEALTHY
||
3425 psize
== 0 || psize
>= 4 * ztest_opts
.zo_vdev_size
) {
3426 spa_config_exit(spa
, SCL_STATE
, spa
);
3427 mutex_exit(&ztest_vdev_lock
);
3431 newsize
= psize
+ psize
/ 8;
3432 ASSERT3U(newsize
, >, psize
);
3434 if (ztest_opts
.zo_verbose
>= 6) {
3435 (void) printf("Expanding LUN %s from %lu to %lu\n",
3436 vd
->vdev_path
, (ulong_t
)psize
, (ulong_t
)newsize
);
3440 * Growing the vdev is a two step process:
3441 * 1). expand the physical size (i.e. relabel)
3442 * 2). online the vdev to create the new metaslabs
3444 if (vdev_walk_tree(tvd
, grow_vdev
, &newsize
) != NULL
||
3445 vdev_walk_tree(tvd
, online_vdev
, NULL
) != NULL
||
3446 tvd
->vdev_state
!= VDEV_STATE_HEALTHY
) {
3447 if (ztest_opts
.zo_verbose
>= 5) {
3448 (void) printf("Could not expand LUN because "
3449 "the vdev configuration changed.\n");
3451 spa_config_exit(spa
, SCL_STATE
, spa
);
3452 mutex_exit(&ztest_vdev_lock
);
3456 spa_config_exit(spa
, SCL_STATE
, spa
);
3459 * Expanding the LUN will update the config asynchronously,
3460 * thus we must wait for the async thread to complete any
3461 * pending tasks before proceeding.
3465 mutex_enter(&spa
->spa_async_lock
);
3466 done
= (spa
->spa_async_thread
== NULL
&& !spa
->spa_async_tasks
);
3467 mutex_exit(&spa
->spa_async_lock
);
3470 txg_wait_synced(spa_get_dsl(spa
), 0);
3471 (void) poll(NULL
, 0, 100);
3474 spa_config_enter(spa
, SCL_STATE
, spa
, RW_READER
);
3476 tvd
= spa
->spa_root_vdev
->vdev_child
[top
];
3477 new_ms_count
= tvd
->vdev_ms_count
;
3478 new_class_space
= metaslab_class_get_space(mc
);
3480 if (tvd
->vdev_mg
!= mg
|| mg
->mg_class
!= mc
) {
3481 if (ztest_opts
.zo_verbose
>= 5) {
3482 (void) printf("Could not verify LUN expansion due to "
3483 "intervening vdev offline or remove.\n");
3485 spa_config_exit(spa
, SCL_STATE
, spa
);
3486 mutex_exit(&ztest_vdev_lock
);
3491 * Make sure we were able to grow the vdev.
3493 if (new_ms_count
<= old_ms_count
)
3494 fatal(0, "LUN expansion failed: ms_count %llu <= %llu\n",
3495 old_ms_count
, new_ms_count
);
3498 * Make sure we were able to grow the pool.
3500 if (new_class_space
<= old_class_space
)
3501 fatal(0, "LUN expansion failed: class_space %llu <= %llu\n",
3502 old_class_space
, new_class_space
);
3504 if (ztest_opts
.zo_verbose
>= 5) {
3505 char oldnumbuf
[6], newnumbuf
[6];
3507 nicenum(old_class_space
, oldnumbuf
);
3508 nicenum(new_class_space
, newnumbuf
);
3509 (void) printf("%s grew from %s to %s\n",
3510 spa
->spa_name
, oldnumbuf
, newnumbuf
);
3513 spa_config_exit(spa
, SCL_STATE
, spa
);
3514 mutex_exit(&ztest_vdev_lock
);
3518 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
3522 ztest_objset_create_cb(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
)
3525 * Create the objects common to all ztest datasets.
3527 VERIFY(zap_create_claim(os
, ZTEST_DIROBJ
,
3528 DMU_OT_ZAP_OTHER
, DMU_OT_NONE
, 0, tx
) == 0);
3532 ztest_dataset_create(char *dsname
)
3534 uint64_t zilset
= ztest_random(100);
3535 int err
= dmu_objset_create(dsname
, DMU_OST_OTHER
, 0,
3536 ztest_objset_create_cb
, NULL
);
3538 if (err
|| zilset
< 80)
3541 if (ztest_opts
.zo_verbose
>= 5)
3542 (void) printf("Setting dataset %s to sync always\n", dsname
);
3543 return (ztest_dsl_prop_set_uint64(dsname
, ZFS_PROP_SYNC
,
3544 ZFS_SYNC_ALWAYS
, B_FALSE
));
3549 ztest_objset_destroy_cb(const char *name
, void *arg
)
3552 dmu_object_info_t doi
;
3556 * Verify that the dataset contains a directory object.
3558 VERIFY0(dmu_objset_own(name
, DMU_OST_OTHER
, B_TRUE
, FTAG
, &os
));
3559 error
= dmu_object_info(os
, ZTEST_DIROBJ
, &doi
);
3560 if (error
!= ENOENT
) {
3561 /* We could have crashed in the middle of destroying it */
3563 ASSERT3U(doi
.doi_type
, ==, DMU_OT_ZAP_OTHER
);
3564 ASSERT3S(doi
.doi_physical_blocks_512
, >=, 0);
3566 dmu_objset_disown(os
, FTAG
);
3569 * Destroy the dataset.
3571 if (strchr(name
, '@') != NULL
) {
3572 VERIFY0(dsl_destroy_snapshot(name
, B_TRUE
));
3574 error
= dsl_destroy_head(name
);
3575 /* There could be a hold on this dataset */
3583 ztest_snapshot_create(char *osname
, uint64_t id
)
3585 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
3588 (void) snprintf(snapname
, sizeof (snapname
), "%llu", (u_longlong_t
)id
);
3590 error
= dmu_objset_snapshot_one(osname
, snapname
);
3591 if (error
== ENOSPC
) {
3592 ztest_record_enospc(FTAG
);
3595 if (error
!= 0 && error
!= EEXIST
) {
3596 fatal(0, "ztest_snapshot_create(%s@%s) = %d", osname
,
3603 ztest_snapshot_destroy(char *osname
, uint64_t id
)
3605 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
3608 (void) snprintf(snapname
, sizeof (snapname
), "%s@%llu", osname
,
3611 error
= dsl_destroy_snapshot(snapname
, B_FALSE
);
3612 if (error
!= 0 && error
!= ENOENT
)
3613 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname
, error
);
3619 ztest_dmu_objset_create_destroy(ztest_ds_t
*zd
, uint64_t id
)
3625 char name
[ZFS_MAX_DATASET_NAME_LEN
];
3629 zdtmp
= umem_alloc(sizeof (ztest_ds_t
), UMEM_NOFAIL
);
3631 (void) rw_rdlock(&ztest_name_lock
);
3633 (void) snprintf(name
, sizeof (name
), "%s/temp_%llu",
3634 ztest_opts
.zo_pool
, (u_longlong_t
)id
);
3637 * If this dataset exists from a previous run, process its replay log
3638 * half of the time. If we don't replay it, then dsl_destroy_head()
3639 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
3641 if (ztest_random(2) == 0 &&
3642 dmu_objset_own(name
, DMU_OST_OTHER
, B_FALSE
, FTAG
, &os
) == 0) {
3643 ztest_zd_init(zdtmp
, NULL
, os
);
3644 zil_replay(os
, zdtmp
, ztest_replay_vector
);
3645 ztest_zd_fini(zdtmp
);
3646 dmu_objset_disown(os
, FTAG
);
3650 * There may be an old instance of the dataset we're about to
3651 * create lying around from a previous run. If so, destroy it
3652 * and all of its snapshots.
3654 (void) dmu_objset_find(name
, ztest_objset_destroy_cb
, NULL
,
3655 DS_FIND_CHILDREN
| DS_FIND_SNAPSHOTS
);
3658 * Verify that the destroyed dataset is no longer in the namespace.
3660 VERIFY3U(ENOENT
, ==, dmu_objset_own(name
, DMU_OST_OTHER
, B_TRUE
,
3664 * Verify that we can create a new dataset.
3666 error
= ztest_dataset_create(name
);
3668 if (error
== ENOSPC
) {
3669 ztest_record_enospc(FTAG
);
3672 fatal(0, "dmu_objset_create(%s) = %d", name
, error
);
3675 VERIFY0(dmu_objset_own(name
, DMU_OST_OTHER
, B_FALSE
, FTAG
, &os
));
3677 ztest_zd_init(zdtmp
, NULL
, os
);
3680 * Open the intent log for it.
3682 zilog
= zil_open(os
, ztest_get_data
);
3685 * Put some objects in there, do a little I/O to them,
3686 * and randomly take a couple of snapshots along the way.
3688 iters
= ztest_random(5);
3689 for (i
= 0; i
< iters
; i
++) {
3690 ztest_dmu_object_alloc_free(zdtmp
, id
);
3691 if (ztest_random(iters
) == 0)
3692 (void) ztest_snapshot_create(name
, i
);
3696 * Verify that we cannot create an existing dataset.
3698 VERIFY3U(EEXIST
, ==,
3699 dmu_objset_create(name
, DMU_OST_OTHER
, 0, NULL
, NULL
));
3702 * Verify that we can hold an objset that is also owned.
3704 VERIFY3U(0, ==, dmu_objset_hold(name
, FTAG
, &os2
));
3705 dmu_objset_rele(os2
, FTAG
);
3708 * Verify that we cannot own an objset that is already owned.
3711 dmu_objset_own(name
, DMU_OST_OTHER
, B_FALSE
, FTAG
, &os2
));
3714 dmu_objset_disown(os
, FTAG
);
3715 ztest_zd_fini(zdtmp
);
3717 (void) rw_unlock(&ztest_name_lock
);
3719 umem_free(zdtmp
, sizeof (ztest_ds_t
));
3723 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3726 ztest_dmu_snapshot_create_destroy(ztest_ds_t
*zd
, uint64_t id
)
3728 (void) rw_rdlock(&ztest_name_lock
);
3729 (void) ztest_snapshot_destroy(zd
->zd_name
, id
);
3730 (void) ztest_snapshot_create(zd
->zd_name
, id
);
3731 (void) rw_unlock(&ztest_name_lock
);
3735 * Cleanup non-standard snapshots and clones.
3738 ztest_dsl_dataset_cleanup(char *osname
, uint64_t id
)
3747 snap1name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
3748 clone1name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
3749 snap2name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
3750 clone2name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
3751 snap3name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
3753 (void) snprintf(snap1name
, ZFS_MAX_DATASET_NAME_LEN
,
3754 "%s@s1_%llu", osname
, (u_longlong_t
)id
);
3755 (void) snprintf(clone1name
, ZFS_MAX_DATASET_NAME_LEN
,
3756 "%s/c1_%llu", osname
, (u_longlong_t
)id
);
3757 (void) snprintf(snap2name
, ZFS_MAX_DATASET_NAME_LEN
,
3758 "%s@s2_%llu", clone1name
, (u_longlong_t
)id
);
3759 (void) snprintf(clone2name
, ZFS_MAX_DATASET_NAME_LEN
,
3760 "%s/c2_%llu", osname
, (u_longlong_t
)id
);
3761 (void) snprintf(snap3name
, ZFS_MAX_DATASET_NAME_LEN
,
3762 "%s@s3_%llu", clone1name
, (u_longlong_t
)id
);
3764 error
= dsl_destroy_head(clone2name
);
3765 if (error
&& error
!= ENOENT
)
3766 fatal(0, "dsl_destroy_head(%s) = %d", clone2name
, error
);
3767 error
= dsl_destroy_snapshot(snap3name
, B_FALSE
);
3768 if (error
&& error
!= ENOENT
)
3769 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap3name
, error
);
3770 error
= dsl_destroy_snapshot(snap2name
, B_FALSE
);
3771 if (error
&& error
!= ENOENT
)
3772 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap2name
, error
);
3773 error
= dsl_destroy_head(clone1name
);
3774 if (error
&& error
!= ENOENT
)
3775 fatal(0, "dsl_destroy_head(%s) = %d", clone1name
, error
);
3776 error
= dsl_destroy_snapshot(snap1name
, B_FALSE
);
3777 if (error
&& error
!= ENOENT
)
3778 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap1name
, error
);
3780 umem_free(snap1name
, ZFS_MAX_DATASET_NAME_LEN
);
3781 umem_free(clone1name
, ZFS_MAX_DATASET_NAME_LEN
);
3782 umem_free(snap2name
, ZFS_MAX_DATASET_NAME_LEN
);
3783 umem_free(clone2name
, ZFS_MAX_DATASET_NAME_LEN
);
3784 umem_free(snap3name
, ZFS_MAX_DATASET_NAME_LEN
);
3788 * Verify dsl_dataset_promote handles EBUSY
3791 ztest_dsl_dataset_promote_busy(ztest_ds_t
*zd
, uint64_t id
)
3799 char *osname
= zd
->zd_name
;
3802 snap1name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
3803 clone1name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
3804 snap2name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
3805 clone2name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
3806 snap3name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
3808 (void) rw_rdlock(&ztest_name_lock
);
3810 ztest_dsl_dataset_cleanup(osname
, id
);
3812 (void) snprintf(snap1name
, ZFS_MAX_DATASET_NAME_LEN
,
3813 "%s@s1_%llu", osname
, (u_longlong_t
)id
);
3814 (void) snprintf(clone1name
, ZFS_MAX_DATASET_NAME_LEN
,
3815 "%s/c1_%llu", osname
, (u_longlong_t
)id
);
3816 (void) snprintf(snap2name
, ZFS_MAX_DATASET_NAME_LEN
,
3817 "%s@s2_%llu", clone1name
, (u_longlong_t
)id
);
3818 (void) snprintf(clone2name
, ZFS_MAX_DATASET_NAME_LEN
,
3819 "%s/c2_%llu", osname
, (u_longlong_t
)id
);
3820 (void) snprintf(snap3name
, ZFS_MAX_DATASET_NAME_LEN
,
3821 "%s@s3_%llu", clone1name
, (u_longlong_t
)id
);
3823 error
= dmu_objset_snapshot_one(osname
, strchr(snap1name
, '@') + 1);
3824 if (error
&& error
!= EEXIST
) {
3825 if (error
== ENOSPC
) {
3826 ztest_record_enospc(FTAG
);
3829 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name
, error
);
3832 error
= dmu_objset_clone(clone1name
, snap1name
);
3834 if (error
== ENOSPC
) {
3835 ztest_record_enospc(FTAG
);
3838 fatal(0, "dmu_objset_create(%s) = %d", clone1name
, error
);
3841 error
= dmu_objset_snapshot_one(clone1name
, strchr(snap2name
, '@') + 1);
3842 if (error
&& error
!= EEXIST
) {
3843 if (error
== ENOSPC
) {
3844 ztest_record_enospc(FTAG
);
3847 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name
, error
);
3850 error
= dmu_objset_snapshot_one(clone1name
, strchr(snap3name
, '@') + 1);
3851 if (error
&& error
!= EEXIST
) {
3852 if (error
== ENOSPC
) {
3853 ztest_record_enospc(FTAG
);
3856 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name
, error
);
3859 error
= dmu_objset_clone(clone2name
, snap3name
);
3861 if (error
== ENOSPC
) {
3862 ztest_record_enospc(FTAG
);
3865 fatal(0, "dmu_objset_create(%s) = %d", clone2name
, error
);
3868 error
= dmu_objset_own(snap2name
, DMU_OST_ANY
, B_TRUE
, FTAG
, &os
);
3870 fatal(0, "dmu_objset_own(%s) = %d", snap2name
, error
);
3871 error
= dsl_dataset_promote(clone2name
, NULL
);
3872 if (error
== ENOSPC
) {
3873 dmu_objset_disown(os
, FTAG
);
3874 ztest_record_enospc(FTAG
);
3878 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name
,
3880 dmu_objset_disown(os
, FTAG
);
3883 ztest_dsl_dataset_cleanup(osname
, id
);
3885 (void) rw_unlock(&ztest_name_lock
);
3887 umem_free(snap1name
, ZFS_MAX_DATASET_NAME_LEN
);
3888 umem_free(clone1name
, ZFS_MAX_DATASET_NAME_LEN
);
3889 umem_free(snap2name
, ZFS_MAX_DATASET_NAME_LEN
);
3890 umem_free(clone2name
, ZFS_MAX_DATASET_NAME_LEN
);
3891 umem_free(snap3name
, ZFS_MAX_DATASET_NAME_LEN
);
3894 #undef OD_ARRAY_SIZE
3895 #define OD_ARRAY_SIZE 4
3898 * Verify that dmu_object_{alloc,free} work as expected.
3901 ztest_dmu_object_alloc_free(ztest_ds_t
*zd
, uint64_t id
)
3908 size
= sizeof (ztest_od_t
) * OD_ARRAY_SIZE
;
3909 od
= umem_alloc(size
, UMEM_NOFAIL
);
3910 batchsize
= OD_ARRAY_SIZE
;
3912 for (b
= 0; b
< batchsize
; b
++)
3913 ztest_od_init(od
+ b
, id
, FTAG
, b
, DMU_OT_UINT64_OTHER
,
3917 * Destroy the previous batch of objects, create a new batch,
3918 * and do some I/O on the new objects.
3920 if (ztest_object_init(zd
, od
, size
, B_TRUE
) != 0)
3923 while (ztest_random(4 * batchsize
) != 0)
3924 ztest_io(zd
, od
[ztest_random(batchsize
)].od_object
,
3925 ztest_random(ZTEST_RANGE_LOCKS
) << SPA_MAXBLOCKSHIFT
);
3927 umem_free(od
, size
);
3930 #undef OD_ARRAY_SIZE
3931 #define OD_ARRAY_SIZE 2
3934 * Verify that dmu_{read,write} work as expected.
3937 ztest_dmu_read_write(ztest_ds_t
*zd
, uint64_t id
)
3942 objset_t
*os
= zd
->zd_os
;
3943 size
= sizeof (ztest_od_t
) * OD_ARRAY_SIZE
;
3944 od
= umem_alloc(size
, UMEM_NOFAIL
);
3946 int i
, freeit
, error
;
3948 bufwad_t
*packbuf
, *bigbuf
, *pack
, *bigH
, *bigT
;
3949 uint64_t packobj
, packoff
, packsize
, bigobj
, bigoff
, bigsize
;
3950 uint64_t chunksize
= (1000 + ztest_random(1000)) * sizeof (uint64_t);
3951 uint64_t regions
= 997;
3952 uint64_t stride
= 123456789ULL;
3953 uint64_t width
= 40;
3954 int free_percent
= 5;
3957 * This test uses two objects, packobj and bigobj, that are always
3958 * updated together (i.e. in the same tx) so that their contents are
3959 * in sync and can be compared. Their contents relate to each other
3960 * in a simple way: packobj is a dense array of 'bufwad' structures,
3961 * while bigobj is a sparse array of the same bufwads. Specifically,
3962 * for any index n, there are three bufwads that should be identical:
3964 * packobj, at offset n * sizeof (bufwad_t)
3965 * bigobj, at the head of the nth chunk
3966 * bigobj, at the tail of the nth chunk
3968 * The chunk size is arbitrary. It doesn't have to be a power of two,
3969 * and it doesn't have any relation to the object blocksize.
3970 * The only requirement is that it can hold at least two bufwads.
3972 * Normally, we write the bufwad to each of these locations.
3973 * However, free_percent of the time we instead write zeroes to
3974 * packobj and perform a dmu_free_range() on bigobj. By comparing
3975 * bigobj to packobj, we can verify that the DMU is correctly
3976 * tracking which parts of an object are allocated and free,
3977 * and that the contents of the allocated blocks are correct.
3981 * Read the directory info. If it's the first time, set things up.
3983 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, 0, 0, chunksize
);
3984 ztest_od_init(od
+ 1, id
, FTAG
, 1, DMU_OT_UINT64_OTHER
, 0, 0,
3987 if (ztest_object_init(zd
, od
, size
, B_FALSE
) != 0) {
3988 umem_free(od
, size
);
3992 bigobj
= od
[0].od_object
;
3993 packobj
= od
[1].od_object
;
3994 chunksize
= od
[0].od_gen
;
3995 ASSERT(chunksize
== od
[1].od_gen
);
3998 * Prefetch a random chunk of the big object.
3999 * Our aim here is to get some async reads in flight
4000 * for blocks that we may free below; the DMU should
4001 * handle this race correctly.
4003 n
= ztest_random(regions
) * stride
+ ztest_random(width
);
4004 s
= 1 + ztest_random(2 * width
- 1);
4005 dmu_prefetch(os
, bigobj
, 0, n
* chunksize
, s
* chunksize
,
4006 ZIO_PRIORITY_SYNC_READ
);
4009 * Pick a random index and compute the offsets into packobj and bigobj.
4011 n
= ztest_random(regions
) * stride
+ ztest_random(width
);
4012 s
= 1 + ztest_random(width
- 1);
4014 packoff
= n
* sizeof (bufwad_t
);
4015 packsize
= s
* sizeof (bufwad_t
);
4017 bigoff
= n
* chunksize
;
4018 bigsize
= s
* chunksize
;
4020 packbuf
= umem_alloc(packsize
, UMEM_NOFAIL
);
4021 bigbuf
= umem_alloc(bigsize
, UMEM_NOFAIL
);
4024 * free_percent of the time, free a range of bigobj rather than
4027 freeit
= (ztest_random(100) < free_percent
);
4030 * Read the current contents of our objects.
4032 error
= dmu_read(os
, packobj
, packoff
, packsize
, packbuf
,
4035 error
= dmu_read(os
, bigobj
, bigoff
, bigsize
, bigbuf
,
4040 * Get a tx for the mods to both packobj and bigobj.
4042 tx
= dmu_tx_create(os
);
4044 dmu_tx_hold_write(tx
, packobj
, packoff
, packsize
);
4047 dmu_tx_hold_free(tx
, bigobj
, bigoff
, bigsize
);
4049 dmu_tx_hold_write(tx
, bigobj
, bigoff
, bigsize
);
4051 /* This accounts for setting the checksum/compression. */
4052 dmu_tx_hold_bonus(tx
, bigobj
);
4054 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
4056 umem_free(packbuf
, packsize
);
4057 umem_free(bigbuf
, bigsize
);
4058 umem_free(od
, size
);
4062 enum zio_checksum cksum
;
4064 cksum
= (enum zio_checksum
)
4065 ztest_random_dsl_prop(ZFS_PROP_CHECKSUM
);
4066 } while (cksum
>= ZIO_CHECKSUM_LEGACY_FUNCTIONS
);
4067 dmu_object_set_checksum(os
, bigobj
, cksum
, tx
);
4069 enum zio_compress comp
;
4071 comp
= (enum zio_compress
)
4072 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION
);
4073 } while (comp
>= ZIO_COMPRESS_LEGACY_FUNCTIONS
);
4074 dmu_object_set_compress(os
, bigobj
, comp
, tx
);
4077 * For each index from n to n + s, verify that the existing bufwad
4078 * in packobj matches the bufwads at the head and tail of the
4079 * corresponding chunk in bigobj. Then update all three bufwads
4080 * with the new values we want to write out.
4082 for (i
= 0; i
< s
; i
++) {
4084 pack
= (bufwad_t
*)((char *)packbuf
+ i
* sizeof (bufwad_t
));
4086 bigH
= (bufwad_t
*)((char *)bigbuf
+ i
* chunksize
);
4088 bigT
= (bufwad_t
*)((char *)bigH
+ chunksize
) - 1;
4090 ASSERT((uintptr_t)bigH
- (uintptr_t)bigbuf
< bigsize
);
4091 ASSERT((uintptr_t)bigT
- (uintptr_t)bigbuf
< bigsize
);
4093 if (pack
->bw_txg
> txg
)
4094 fatal(0, "future leak: got %llx, open txg is %llx",
4097 if (pack
->bw_data
!= 0 && pack
->bw_index
!= n
+ i
)
4098 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4099 pack
->bw_index
, n
, i
);
4101 if (bcmp(pack
, bigH
, sizeof (bufwad_t
)) != 0)
4102 fatal(0, "pack/bigH mismatch in %p/%p", pack
, bigH
);
4104 if (bcmp(pack
, bigT
, sizeof (bufwad_t
)) != 0)
4105 fatal(0, "pack/bigT mismatch in %p/%p", pack
, bigT
);
4108 bzero(pack
, sizeof (bufwad_t
));
4110 pack
->bw_index
= n
+ i
;
4112 pack
->bw_data
= 1 + ztest_random(-2ULL);
4119 * We've verified all the old bufwads, and made new ones.
4120 * Now write them out.
4122 dmu_write(os
, packobj
, packoff
, packsize
, packbuf
, tx
);
4125 if (ztest_opts
.zo_verbose
>= 7) {
4126 (void) printf("freeing offset %llx size %llx"
4128 (u_longlong_t
)bigoff
,
4129 (u_longlong_t
)bigsize
,
4132 VERIFY(0 == dmu_free_range(os
, bigobj
, bigoff
, bigsize
, tx
));
4134 if (ztest_opts
.zo_verbose
>= 7) {
4135 (void) printf("writing offset %llx size %llx"
4137 (u_longlong_t
)bigoff
,
4138 (u_longlong_t
)bigsize
,
4141 dmu_write(os
, bigobj
, bigoff
, bigsize
, bigbuf
, tx
);
4147 * Sanity check the stuff we just wrote.
4150 void *packcheck
= umem_alloc(packsize
, UMEM_NOFAIL
);
4151 void *bigcheck
= umem_alloc(bigsize
, UMEM_NOFAIL
);
4153 VERIFY(0 == dmu_read(os
, packobj
, packoff
,
4154 packsize
, packcheck
, DMU_READ_PREFETCH
));
4155 VERIFY(0 == dmu_read(os
, bigobj
, bigoff
,
4156 bigsize
, bigcheck
, DMU_READ_PREFETCH
));
4158 ASSERT(bcmp(packbuf
, packcheck
, packsize
) == 0);
4159 ASSERT(bcmp(bigbuf
, bigcheck
, bigsize
) == 0);
4161 umem_free(packcheck
, packsize
);
4162 umem_free(bigcheck
, bigsize
);
4165 umem_free(packbuf
, packsize
);
4166 umem_free(bigbuf
, bigsize
);
4167 umem_free(od
, size
);
4171 compare_and_update_pbbufs(uint64_t s
, bufwad_t
*packbuf
, bufwad_t
*bigbuf
,
4172 uint64_t bigsize
, uint64_t n
, uint64_t chunksize
, uint64_t txg
)
4180 * For each index from n to n + s, verify that the existing bufwad
4181 * in packobj matches the bufwads at the head and tail of the
4182 * corresponding chunk in bigobj. Then update all three bufwads
4183 * with the new values we want to write out.
4185 for (i
= 0; i
< s
; i
++) {
4187 pack
= (bufwad_t
*)((char *)packbuf
+ i
* sizeof (bufwad_t
));
4189 bigH
= (bufwad_t
*)((char *)bigbuf
+ i
* chunksize
);
4191 bigT
= (bufwad_t
*)((char *)bigH
+ chunksize
) - 1;
4193 ASSERT((uintptr_t)bigH
- (uintptr_t)bigbuf
< bigsize
);
4194 ASSERT((uintptr_t)bigT
- (uintptr_t)bigbuf
< bigsize
);
4196 if (pack
->bw_txg
> txg
)
4197 fatal(0, "future leak: got %llx, open txg is %llx",
4200 if (pack
->bw_data
!= 0 && pack
->bw_index
!= n
+ i
)
4201 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4202 pack
->bw_index
, n
, i
);
4204 if (bcmp(pack
, bigH
, sizeof (bufwad_t
)) != 0)
4205 fatal(0, "pack/bigH mismatch in %p/%p", pack
, bigH
);
4207 if (bcmp(pack
, bigT
, sizeof (bufwad_t
)) != 0)
4208 fatal(0, "pack/bigT mismatch in %p/%p", pack
, bigT
);
4210 pack
->bw_index
= n
+ i
;
4212 pack
->bw_data
= 1 + ztest_random(-2ULL);
4219 #undef OD_ARRAY_SIZE
4220 #define OD_ARRAY_SIZE 2
4223 ztest_dmu_read_write_zcopy(ztest_ds_t
*zd
, uint64_t id
)
4225 objset_t
*os
= zd
->zd_os
;
4232 bufwad_t
*packbuf
, *bigbuf
;
4233 uint64_t packobj
, packoff
, packsize
, bigobj
, bigoff
, bigsize
;
4234 uint64_t blocksize
= ztest_random_blocksize();
4235 uint64_t chunksize
= blocksize
;
4236 uint64_t regions
= 997;
4237 uint64_t stride
= 123456789ULL;
4239 dmu_buf_t
*bonus_db
;
4240 arc_buf_t
**bigbuf_arcbufs
;
4241 dmu_object_info_t doi
;
4243 size
= sizeof (ztest_od_t
) * OD_ARRAY_SIZE
;
4244 od
= umem_alloc(size
, UMEM_NOFAIL
);
4247 * This test uses two objects, packobj and bigobj, that are always
4248 * updated together (i.e. in the same tx) so that their contents are
4249 * in sync and can be compared. Their contents relate to each other
4250 * in a simple way: packobj is a dense array of 'bufwad' structures,
4251 * while bigobj is a sparse array of the same bufwads. Specifically,
4252 * for any index n, there are three bufwads that should be identical:
4254 * packobj, at offset n * sizeof (bufwad_t)
4255 * bigobj, at the head of the nth chunk
4256 * bigobj, at the tail of the nth chunk
4258 * The chunk size is set equal to bigobj block size so that
4259 * dmu_assign_arcbuf() can be tested for object updates.
4263 * Read the directory info. If it's the first time, set things up.
4265 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, blocksize
, 0, 0);
4266 ztest_od_init(od
+ 1, id
, FTAG
, 1, DMU_OT_UINT64_OTHER
, 0, 0,
4270 if (ztest_object_init(zd
, od
, size
, B_FALSE
) != 0) {
4271 umem_free(od
, size
);
4275 bigobj
= od
[0].od_object
;
4276 packobj
= od
[1].od_object
;
4277 blocksize
= od
[0].od_blocksize
;
4278 chunksize
= blocksize
;
4279 ASSERT(chunksize
== od
[1].od_gen
);
4281 VERIFY(dmu_object_info(os
, bigobj
, &doi
) == 0);
4282 VERIFY(ISP2(doi
.doi_data_block_size
));
4283 VERIFY(chunksize
== doi
.doi_data_block_size
);
4284 VERIFY(chunksize
>= 2 * sizeof (bufwad_t
));
4287 * Pick a random index and compute the offsets into packobj and bigobj.
4289 n
= ztest_random(regions
) * stride
+ ztest_random(width
);
4290 s
= 1 + ztest_random(width
- 1);
4292 packoff
= n
* sizeof (bufwad_t
);
4293 packsize
= s
* sizeof (bufwad_t
);
4295 bigoff
= n
* chunksize
;
4296 bigsize
= s
* chunksize
;
4298 packbuf
= umem_zalloc(packsize
, UMEM_NOFAIL
);
4299 bigbuf
= umem_zalloc(bigsize
, UMEM_NOFAIL
);
4301 VERIFY3U(0, ==, dmu_bonus_hold(os
, bigobj
, FTAG
, &bonus_db
));
4303 bigbuf_arcbufs
= umem_zalloc(2 * s
* sizeof (arc_buf_t
*), UMEM_NOFAIL
);
4306 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
4307 * Iteration 1 test zcopy to already referenced dbufs.
4308 * Iteration 2 test zcopy to dirty dbuf in the same txg.
4309 * Iteration 3 test zcopy to dbuf dirty in previous txg.
4310 * Iteration 4 test zcopy when dbuf is no longer dirty.
4311 * Iteration 5 test zcopy when it can't be done.
4312 * Iteration 6 one more zcopy write.
4314 for (i
= 0; i
< 7; i
++) {
4319 * In iteration 5 (i == 5) use arcbufs
4320 * that don't match bigobj blksz to test
4321 * dmu_assign_arcbuf() when it can't directly
4322 * assign an arcbuf to a dbuf.
4324 for (j
= 0; j
< s
; j
++) {
4325 if (i
!= 5 || chunksize
< (SPA_MINBLOCKSIZE
* 2)) {
4327 dmu_request_arcbuf(bonus_db
, chunksize
);
4329 bigbuf_arcbufs
[2 * j
] =
4330 dmu_request_arcbuf(bonus_db
, chunksize
/ 2);
4331 bigbuf_arcbufs
[2 * j
+ 1] =
4332 dmu_request_arcbuf(bonus_db
, chunksize
/ 2);
4337 * Get a tx for the mods to both packobj and bigobj.
4339 tx
= dmu_tx_create(os
);
4341 dmu_tx_hold_write(tx
, packobj
, packoff
, packsize
);
4342 dmu_tx_hold_write(tx
, bigobj
, bigoff
, bigsize
);
4344 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
4346 umem_free(packbuf
, packsize
);
4347 umem_free(bigbuf
, bigsize
);
4348 for (j
= 0; j
< s
; j
++) {
4350 chunksize
< (SPA_MINBLOCKSIZE
* 2)) {
4351 dmu_return_arcbuf(bigbuf_arcbufs
[j
]);
4354 bigbuf_arcbufs
[2 * j
]);
4356 bigbuf_arcbufs
[2 * j
+ 1]);
4359 umem_free(bigbuf_arcbufs
, 2 * s
* sizeof (arc_buf_t
*));
4360 umem_free(od
, size
);
4361 dmu_buf_rele(bonus_db
, FTAG
);
4366 * 50% of the time don't read objects in the 1st iteration to
4367 * test dmu_assign_arcbuf() for the case when there're no
4368 * existing dbufs for the specified offsets.
4370 if (i
!= 0 || ztest_random(2) != 0) {
4371 error
= dmu_read(os
, packobj
, packoff
,
4372 packsize
, packbuf
, DMU_READ_PREFETCH
);
4374 error
= dmu_read(os
, bigobj
, bigoff
, bigsize
,
4375 bigbuf
, DMU_READ_PREFETCH
);
4378 compare_and_update_pbbufs(s
, packbuf
, bigbuf
, bigsize
,
4382 * We've verified all the old bufwads, and made new ones.
4383 * Now write them out.
4385 dmu_write(os
, packobj
, packoff
, packsize
, packbuf
, tx
);
4386 if (ztest_opts
.zo_verbose
>= 7) {
4387 (void) printf("writing offset %llx size %llx"
4389 (u_longlong_t
)bigoff
,
4390 (u_longlong_t
)bigsize
,
4393 for (off
= bigoff
, j
= 0; j
< s
; j
++, off
+= chunksize
) {
4395 if (i
!= 5 || chunksize
< (SPA_MINBLOCKSIZE
* 2)) {
4396 bcopy((caddr_t
)bigbuf
+ (off
- bigoff
),
4397 bigbuf_arcbufs
[j
]->b_data
, chunksize
);
4399 bcopy((caddr_t
)bigbuf
+ (off
- bigoff
),
4400 bigbuf_arcbufs
[2 * j
]->b_data
,
4402 bcopy((caddr_t
)bigbuf
+ (off
- bigoff
) +
4404 bigbuf_arcbufs
[2 * j
+ 1]->b_data
,
4409 VERIFY(dmu_buf_hold(os
, bigobj
, off
,
4410 FTAG
, &dbt
, DMU_READ_NO_PREFETCH
) == 0);
4412 if (i
!= 5 || chunksize
< (SPA_MINBLOCKSIZE
* 2)) {
4413 dmu_assign_arcbuf(bonus_db
, off
,
4414 bigbuf_arcbufs
[j
], tx
);
4416 dmu_assign_arcbuf(bonus_db
, off
,
4417 bigbuf_arcbufs
[2 * j
], tx
);
4418 dmu_assign_arcbuf(bonus_db
,
4419 off
+ chunksize
/ 2,
4420 bigbuf_arcbufs
[2 * j
+ 1], tx
);
4423 dmu_buf_rele(dbt
, FTAG
);
4429 * Sanity check the stuff we just wrote.
4432 void *packcheck
= umem_alloc(packsize
, UMEM_NOFAIL
);
4433 void *bigcheck
= umem_alloc(bigsize
, UMEM_NOFAIL
);
4435 VERIFY(0 == dmu_read(os
, packobj
, packoff
,
4436 packsize
, packcheck
, DMU_READ_PREFETCH
));
4437 VERIFY(0 == dmu_read(os
, bigobj
, bigoff
,
4438 bigsize
, bigcheck
, DMU_READ_PREFETCH
));
4440 ASSERT(bcmp(packbuf
, packcheck
, packsize
) == 0);
4441 ASSERT(bcmp(bigbuf
, bigcheck
, bigsize
) == 0);
4443 umem_free(packcheck
, packsize
);
4444 umem_free(bigcheck
, bigsize
);
4447 txg_wait_open(dmu_objset_pool(os
), 0);
4448 } else if (i
== 3) {
4449 txg_wait_synced(dmu_objset_pool(os
), 0);
4453 dmu_buf_rele(bonus_db
, FTAG
);
4454 umem_free(packbuf
, packsize
);
4455 umem_free(bigbuf
, bigsize
);
4456 umem_free(bigbuf_arcbufs
, 2 * s
* sizeof (arc_buf_t
*));
4457 umem_free(od
, size
);
4462 ztest_dmu_write_parallel(ztest_ds_t
*zd
, uint64_t id
)
4466 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
4467 uint64_t offset
= (1ULL << (ztest_random(20) + 43)) +
4468 (ztest_random(ZTEST_RANGE_LOCKS
) << SPA_MAXBLOCKSHIFT
);
4471 * Have multiple threads write to large offsets in an object
4472 * to verify that parallel writes to an object -- even to the
4473 * same blocks within the object -- doesn't cause any trouble.
4475 ztest_od_init(od
, ID_PARALLEL
, FTAG
, 0, DMU_OT_UINT64_OTHER
, 0, 0, 0);
4477 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
), B_FALSE
) != 0)
4480 while (ztest_random(10) != 0)
4481 ztest_io(zd
, od
->od_object
, offset
);
4483 umem_free(od
, sizeof (ztest_od_t
));
4487 ztest_dmu_prealloc(ztest_ds_t
*zd
, uint64_t id
)
4490 uint64_t offset
= (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT
)) +
4491 (ztest_random(ZTEST_RANGE_LOCKS
) << SPA_MAXBLOCKSHIFT
);
4492 uint64_t count
= ztest_random(20) + 1;
4493 uint64_t blocksize
= ztest_random_blocksize();
4496 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
4498 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, blocksize
, 0, 0);
4500 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
),
4501 !ztest_random(2)) != 0) {
4502 umem_free(od
, sizeof (ztest_od_t
));
4506 if (ztest_truncate(zd
, od
->od_object
, offset
, count
* blocksize
) != 0) {
4507 umem_free(od
, sizeof (ztest_od_t
));
4511 ztest_prealloc(zd
, od
->od_object
, offset
, count
* blocksize
);
4513 data
= umem_zalloc(blocksize
, UMEM_NOFAIL
);
4515 while (ztest_random(count
) != 0) {
4516 uint64_t randoff
= offset
+ (ztest_random(count
) * blocksize
);
4517 if (ztest_write(zd
, od
->od_object
, randoff
, blocksize
,
4520 while (ztest_random(4) != 0)
4521 ztest_io(zd
, od
->od_object
, randoff
);
4524 umem_free(data
, blocksize
);
4525 umem_free(od
, sizeof (ztest_od_t
));
4529 * Verify that zap_{create,destroy,add,remove,update} work as expected.
4531 #define ZTEST_ZAP_MIN_INTS 1
4532 #define ZTEST_ZAP_MAX_INTS 4
4533 #define ZTEST_ZAP_MAX_PROPS 1000
4536 ztest_zap(ztest_ds_t
*zd
, uint64_t id
)
4538 objset_t
*os
= zd
->zd_os
;
4541 uint64_t txg
, last_txg
;
4542 uint64_t value
[ZTEST_ZAP_MAX_INTS
];
4543 uint64_t zl_ints
, zl_intsize
, prop
;
4546 char propname
[100], txgname
[100];
4548 char *hc
[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
4550 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
4551 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_ZAP_OTHER
, 0, 0, 0);
4553 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
),
4554 !ztest_random(2)) != 0)
4557 object
= od
->od_object
;
4560 * Generate a known hash collision, and verify that
4561 * we can lookup and remove both entries.
4563 tx
= dmu_tx_create(os
);
4564 dmu_tx_hold_zap(tx
, object
, B_TRUE
, NULL
);
4565 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
4568 for (i
= 0; i
< 2; i
++) {
4570 VERIFY3U(0, ==, zap_add(os
, object
, hc
[i
], sizeof (uint64_t),
4573 for (i
= 0; i
< 2; i
++) {
4574 VERIFY3U(EEXIST
, ==, zap_add(os
, object
, hc
[i
],
4575 sizeof (uint64_t), 1, &value
[i
], tx
));
4577 zap_length(os
, object
, hc
[i
], &zl_intsize
, &zl_ints
));
4578 ASSERT3U(zl_intsize
, ==, sizeof (uint64_t));
4579 ASSERT3U(zl_ints
, ==, 1);
4581 for (i
= 0; i
< 2; i
++) {
4582 VERIFY3U(0, ==, zap_remove(os
, object
, hc
[i
], tx
));
4587 * Generate a buch of random entries.
4589 ints
= MAX(ZTEST_ZAP_MIN_INTS
, object
% ZTEST_ZAP_MAX_INTS
);
4591 prop
= ztest_random(ZTEST_ZAP_MAX_PROPS
);
4592 (void) sprintf(propname
, "prop_%llu", (u_longlong_t
)prop
);
4593 (void) sprintf(txgname
, "txg_%llu", (u_longlong_t
)prop
);
4594 bzero(value
, sizeof (value
));
4598 * If these zap entries already exist, validate their contents.
4600 error
= zap_length(os
, object
, txgname
, &zl_intsize
, &zl_ints
);
4602 ASSERT3U(zl_intsize
, ==, sizeof (uint64_t));
4603 ASSERT3U(zl_ints
, ==, 1);
4605 VERIFY(zap_lookup(os
, object
, txgname
, zl_intsize
,
4606 zl_ints
, &last_txg
) == 0);
4608 VERIFY(zap_length(os
, object
, propname
, &zl_intsize
,
4611 ASSERT3U(zl_intsize
, ==, sizeof (uint64_t));
4612 ASSERT3U(zl_ints
, ==, ints
);
4614 VERIFY(zap_lookup(os
, object
, propname
, zl_intsize
,
4615 zl_ints
, value
) == 0);
4617 for (i
= 0; i
< ints
; i
++) {
4618 ASSERT3U(value
[i
], ==, last_txg
+ object
+ i
);
4621 ASSERT3U(error
, ==, ENOENT
);
4625 * Atomically update two entries in our zap object.
4626 * The first is named txg_%llu, and contains the txg
4627 * in which the property was last updated. The second
4628 * is named prop_%llu, and the nth element of its value
4629 * should be txg + object + n.
4631 tx
= dmu_tx_create(os
);
4632 dmu_tx_hold_zap(tx
, object
, B_TRUE
, NULL
);
4633 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
4638 fatal(0, "zap future leak: old %llu new %llu", last_txg
, txg
);
4640 for (i
= 0; i
< ints
; i
++)
4641 value
[i
] = txg
+ object
+ i
;
4643 VERIFY3U(0, ==, zap_update(os
, object
, txgname
, sizeof (uint64_t),
4645 VERIFY3U(0, ==, zap_update(os
, object
, propname
, sizeof (uint64_t),
4651 * Remove a random pair of entries.
4653 prop
= ztest_random(ZTEST_ZAP_MAX_PROPS
);
4654 (void) sprintf(propname
, "prop_%llu", (u_longlong_t
)prop
);
4655 (void) sprintf(txgname
, "txg_%llu", (u_longlong_t
)prop
);
4657 error
= zap_length(os
, object
, txgname
, &zl_intsize
, &zl_ints
);
4659 if (error
== ENOENT
)
4664 tx
= dmu_tx_create(os
);
4665 dmu_tx_hold_zap(tx
, object
, B_TRUE
, NULL
);
4666 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
4669 VERIFY3U(0, ==, zap_remove(os
, object
, txgname
, tx
));
4670 VERIFY3U(0, ==, zap_remove(os
, object
, propname
, tx
));
4673 umem_free(od
, sizeof (ztest_od_t
));
4677 * Testcase to test the upgrading of a microzap to fatzap.
4680 ztest_fzap(ztest_ds_t
*zd
, uint64_t id
)
4682 objset_t
*os
= zd
->zd_os
;
4684 uint64_t object
, txg
;
4687 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
4688 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_ZAP_OTHER
, 0, 0, 0);
4690 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
),
4691 !ztest_random(2)) != 0)
4693 object
= od
->od_object
;
4696 * Add entries to this ZAP and make sure it spills over
4697 * and gets upgraded to a fatzap. Also, since we are adding
4698 * 2050 entries we should see ptrtbl growth and leaf-block split.
4700 for (i
= 0; i
< 2050; i
++) {
4701 char name
[ZFS_MAX_DATASET_NAME_LEN
];
4706 (void) snprintf(name
, sizeof (name
), "fzap-%llu-%llu",
4707 (u_longlong_t
)id
, (u_longlong_t
)value
);
4709 tx
= dmu_tx_create(os
);
4710 dmu_tx_hold_zap(tx
, object
, B_TRUE
, name
);
4711 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
4714 error
= zap_add(os
, object
, name
, sizeof (uint64_t), 1,
4716 ASSERT(error
== 0 || error
== EEXIST
);
4720 umem_free(od
, sizeof (ztest_od_t
));
4725 ztest_zap_parallel(ztest_ds_t
*zd
, uint64_t id
)
4727 objset_t
*os
= zd
->zd_os
;
4729 uint64_t txg
, object
, count
, wsize
, wc
, zl_wsize
, zl_wc
;
4731 int i
, namelen
, error
;
4732 int micro
= ztest_random(2);
4733 char name
[20], string_value
[20];
4736 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
4737 ztest_od_init(od
, ID_PARALLEL
, FTAG
, micro
, DMU_OT_ZAP_OTHER
, 0, 0, 0);
4739 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
), B_FALSE
) != 0) {
4740 umem_free(od
, sizeof (ztest_od_t
));
4744 object
= od
->od_object
;
4747 * Generate a random name of the form 'xxx.....' where each
4748 * x is a random printable character and the dots are dots.
4749 * There are 94 such characters, and the name length goes from
4750 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
4752 namelen
= ztest_random(sizeof (name
) - 5) + 5 + 1;
4754 for (i
= 0; i
< 3; i
++)
4755 name
[i
] = '!' + ztest_random('~' - '!' + 1);
4756 for (; i
< namelen
- 1; i
++)
4760 if ((namelen
& 1) || micro
) {
4761 wsize
= sizeof (txg
);
4767 data
= string_value
;
4771 VERIFY0(zap_count(os
, object
, &count
));
4772 ASSERT(count
!= -1ULL);
4775 * Select an operation: length, lookup, add, update, remove.
4777 i
= ztest_random(5);
4780 tx
= dmu_tx_create(os
);
4781 dmu_tx_hold_zap(tx
, object
, B_TRUE
, NULL
);
4782 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
4784 umem_free(od
, sizeof (ztest_od_t
));
4787 bcopy(name
, string_value
, namelen
);
4791 bzero(string_value
, namelen
);
4797 error
= zap_length(os
, object
, name
, &zl_wsize
, &zl_wc
);
4799 ASSERT3U(wsize
, ==, zl_wsize
);
4800 ASSERT3U(wc
, ==, zl_wc
);
4802 ASSERT3U(error
, ==, ENOENT
);
4807 error
= zap_lookup(os
, object
, name
, wsize
, wc
, data
);
4809 if (data
== string_value
&&
4810 bcmp(name
, data
, namelen
) != 0)
4811 fatal(0, "name '%s' != val '%s' len %d",
4812 name
, data
, namelen
);
4814 ASSERT3U(error
, ==, ENOENT
);
4819 error
= zap_add(os
, object
, name
, wsize
, wc
, data
, tx
);
4820 ASSERT(error
== 0 || error
== EEXIST
);
4824 VERIFY(zap_update(os
, object
, name
, wsize
, wc
, data
, tx
) == 0);
4828 error
= zap_remove(os
, object
, name
, tx
);
4829 ASSERT(error
== 0 || error
== ENOENT
);
4836 umem_free(od
, sizeof (ztest_od_t
));
4840 * Commit callback data.
4842 typedef struct ztest_cb_data
{
4843 list_node_t zcd_node
;
4845 int zcd_expected_err
;
4846 boolean_t zcd_added
;
4847 boolean_t zcd_called
;
4851 /* This is the actual commit callback function */
4853 ztest_commit_callback(void *arg
, int error
)
4855 ztest_cb_data_t
*data
= arg
;
4856 uint64_t synced_txg
;
4858 VERIFY(data
!= NULL
);
4859 VERIFY3S(data
->zcd_expected_err
, ==, error
);
4860 VERIFY(!data
->zcd_called
);
4862 synced_txg
= spa_last_synced_txg(data
->zcd_spa
);
4863 if (data
->zcd_txg
> synced_txg
)
4864 fatal(0, "commit callback of txg %" PRIu64
" called prematurely"
4865 ", last synced txg = %" PRIu64
"\n", data
->zcd_txg
,
4868 data
->zcd_called
= B_TRUE
;
4870 if (error
== ECANCELED
) {
4871 ASSERT0(data
->zcd_txg
);
4872 ASSERT(!data
->zcd_added
);
4875 * The private callback data should be destroyed here, but
4876 * since we are going to check the zcd_called field after
4877 * dmu_tx_abort(), we will destroy it there.
4882 ASSERT(data
->zcd_added
);
4883 ASSERT3U(data
->zcd_txg
, !=, 0);
4885 (void) mutex_enter(&zcl
.zcl_callbacks_lock
);
4887 /* See if this cb was called more quickly */
4888 if ((synced_txg
- data
->zcd_txg
) < zc_min_txg_delay
)
4889 zc_min_txg_delay
= synced_txg
- data
->zcd_txg
;
4891 /* Remove our callback from the list */
4892 list_remove(&zcl
.zcl_callbacks
, data
);
4894 (void) mutex_exit(&zcl
.zcl_callbacks_lock
);
4896 umem_free(data
, sizeof (ztest_cb_data_t
));
4899 /* Allocate and initialize callback data structure */
4900 static ztest_cb_data_t
*
4901 ztest_create_cb_data(objset_t
*os
, uint64_t txg
)
4903 ztest_cb_data_t
*cb_data
;
4905 cb_data
= umem_zalloc(sizeof (ztest_cb_data_t
), UMEM_NOFAIL
);
4907 cb_data
->zcd_txg
= txg
;
4908 cb_data
->zcd_spa
= dmu_objset_spa(os
);
4909 list_link_init(&cb_data
->zcd_node
);
4915 * Commit callback test.
4918 ztest_dmu_commit_callbacks(ztest_ds_t
*zd
, uint64_t id
)
4920 objset_t
*os
= zd
->zd_os
;
4923 ztest_cb_data_t
*cb_data
[3], *tmp_cb
;
4924 uint64_t old_txg
, txg
;
4927 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
4928 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, 0, 0, 0);
4930 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
), B_FALSE
) != 0) {
4931 umem_free(od
, sizeof (ztest_od_t
));
4935 tx
= dmu_tx_create(os
);
4937 cb_data
[0] = ztest_create_cb_data(os
, 0);
4938 dmu_tx_callback_register(tx
, ztest_commit_callback
, cb_data
[0]);
4940 dmu_tx_hold_write(tx
, od
->od_object
, 0, sizeof (uint64_t));
4942 /* Every once in a while, abort the transaction on purpose */
4943 if (ztest_random(100) == 0)
4947 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
4949 txg
= error
? 0 : dmu_tx_get_txg(tx
);
4951 cb_data
[0]->zcd_txg
= txg
;
4952 cb_data
[1] = ztest_create_cb_data(os
, txg
);
4953 dmu_tx_callback_register(tx
, ztest_commit_callback
, cb_data
[1]);
4957 * It's not a strict requirement to call the registered
4958 * callbacks from inside dmu_tx_abort(), but that's what
4959 * it's supposed to happen in the current implementation
4960 * so we will check for that.
4962 for (i
= 0; i
< 2; i
++) {
4963 cb_data
[i
]->zcd_expected_err
= ECANCELED
;
4964 VERIFY(!cb_data
[i
]->zcd_called
);
4969 for (i
= 0; i
< 2; i
++) {
4970 VERIFY(cb_data
[i
]->zcd_called
);
4971 umem_free(cb_data
[i
], sizeof (ztest_cb_data_t
));
4974 umem_free(od
, sizeof (ztest_od_t
));
4978 cb_data
[2] = ztest_create_cb_data(os
, txg
);
4979 dmu_tx_callback_register(tx
, ztest_commit_callback
, cb_data
[2]);
4982 * Read existing data to make sure there isn't a future leak.
4984 VERIFY(0 == dmu_read(os
, od
->od_object
, 0, sizeof (uint64_t),
4985 &old_txg
, DMU_READ_PREFETCH
));
4988 fatal(0, "future leak: got %" PRIu64
", open txg is %" PRIu64
,
4991 dmu_write(os
, od
->od_object
, 0, sizeof (uint64_t), &txg
, tx
);
4993 (void) mutex_enter(&zcl
.zcl_callbacks_lock
);
4996 * Since commit callbacks don't have any ordering requirement and since
4997 * it is theoretically possible for a commit callback to be called
4998 * after an arbitrary amount of time has elapsed since its txg has been
4999 * synced, it is difficult to reliably determine whether a commit
5000 * callback hasn't been called due to high load or due to a flawed
5003 * In practice, we will assume that if after a certain number of txgs a
5004 * commit callback hasn't been called, then most likely there's an
5005 * implementation bug..
5007 tmp_cb
= list_head(&zcl
.zcl_callbacks
);
5008 if (tmp_cb
!= NULL
&&
5009 tmp_cb
->zcd_txg
+ ZTEST_COMMIT_CB_THRESH
< txg
) {
5010 fatal(0, "Commit callback threshold exceeded, oldest txg: %"
5011 PRIu64
", open txg: %" PRIu64
"\n", tmp_cb
->zcd_txg
, txg
);
5015 * Let's find the place to insert our callbacks.
5017 * Even though the list is ordered by txg, it is possible for the
5018 * insertion point to not be the end because our txg may already be
5019 * quiescing at this point and other callbacks in the open txg
5020 * (from other objsets) may have sneaked in.
5022 tmp_cb
= list_tail(&zcl
.zcl_callbacks
);
5023 while (tmp_cb
!= NULL
&& tmp_cb
->zcd_txg
> txg
)
5024 tmp_cb
= list_prev(&zcl
.zcl_callbacks
, tmp_cb
);
5026 /* Add the 3 callbacks to the list */
5027 for (i
= 0; i
< 3; i
++) {
5029 list_insert_head(&zcl
.zcl_callbacks
, cb_data
[i
]);
5031 list_insert_after(&zcl
.zcl_callbacks
, tmp_cb
,
5034 cb_data
[i
]->zcd_added
= B_TRUE
;
5035 VERIFY(!cb_data
[i
]->zcd_called
);
5037 tmp_cb
= cb_data
[i
];
5042 (void) mutex_exit(&zcl
.zcl_callbacks_lock
);
5046 umem_free(od
, sizeof (ztest_od_t
));
5050 * Visit each object in the dataset. Verify that its properties
5051 * are consistent what was stored in the block tag when it was created,
5052 * and that its unused bonus buffer space has not been overwritten.
5056 ztest_verify_dnode_bt(ztest_ds_t
*zd
, uint64_t id
)
5058 objset_t
*os
= zd
->zd_os
;
5062 for (obj
= 0; err
== 0; err
= dmu_object_next(os
, &obj
, FALSE
, 0)) {
5063 ztest_block_tag_t
*bt
= NULL
;
5064 dmu_object_info_t doi
;
5067 if (dmu_bonus_hold(os
, obj
, FTAG
, &db
) != 0)
5070 dmu_object_info_from_db(db
, &doi
);
5071 if (doi
.doi_bonus_size
>= sizeof (*bt
))
5072 bt
= ztest_bt_bonus(db
);
5074 if (bt
&& bt
->bt_magic
== BT_MAGIC
) {
5075 ztest_bt_verify(bt
, os
, obj
, doi
.doi_dnodesize
,
5076 bt
->bt_offset
, bt
->bt_gen
, bt
->bt_txg
,
5078 ztest_verify_unused_bonus(db
, bt
, obj
, os
, bt
->bt_gen
);
5081 dmu_buf_rele(db
, FTAG
);
5087 ztest_dsl_prop_get_set(ztest_ds_t
*zd
, uint64_t id
)
5089 zfs_prop_t proplist
[] = {
5091 ZFS_PROP_COMPRESSION
,
5097 (void) rw_rdlock(&ztest_name_lock
);
5099 for (p
= 0; p
< sizeof (proplist
) / sizeof (proplist
[0]); p
++)
5100 (void) ztest_dsl_prop_set_uint64(zd
->zd_name
, proplist
[p
],
5101 ztest_random_dsl_prop(proplist
[p
]), (int)ztest_random(2));
5103 VERIFY0(ztest_dsl_prop_set_uint64(zd
->zd_name
, ZFS_PROP_RECORDSIZE
,
5104 ztest_random_blocksize(), (int)ztest_random(2)));
5106 (void) rw_unlock(&ztest_name_lock
);
5111 ztest_spa_prop_get_set(ztest_ds_t
*zd
, uint64_t id
)
5113 nvlist_t
*props
= NULL
;
5115 (void) rw_rdlock(&ztest_name_lock
);
5117 (void) ztest_spa_prop_set_uint64(ZPOOL_PROP_DEDUPDITTO
,
5118 ZIO_DEDUPDITTO_MIN
+ ztest_random(ZIO_DEDUPDITTO_MIN
));
5120 VERIFY0(spa_prop_get(ztest_spa
, &props
));
5122 if (ztest_opts
.zo_verbose
>= 6)
5123 dump_nvlist(props
, 4);
5127 (void) rw_unlock(&ztest_name_lock
);
5131 user_release_one(const char *snapname
, const char *holdname
)
5133 nvlist_t
*snaps
, *holds
;
5136 snaps
= fnvlist_alloc();
5137 holds
= fnvlist_alloc();
5138 fnvlist_add_boolean(holds
, holdname
);
5139 fnvlist_add_nvlist(snaps
, snapname
, holds
);
5140 fnvlist_free(holds
);
5141 error
= dsl_dataset_user_release(snaps
, NULL
);
5142 fnvlist_free(snaps
);
5147 * Test snapshot hold/release and deferred destroy.
5150 ztest_dmu_snapshot_hold(ztest_ds_t
*zd
, uint64_t id
)
5153 objset_t
*os
= zd
->zd_os
;
5157 char clonename
[100];
5159 char osname
[ZFS_MAX_DATASET_NAME_LEN
];
5162 (void) rw_rdlock(&ztest_name_lock
);
5164 dmu_objset_name(os
, osname
);
5166 (void) snprintf(snapname
, sizeof (snapname
), "sh1_%llu",
5168 (void) snprintf(fullname
, sizeof (fullname
), "%s@%s", osname
, snapname
);
5169 (void) snprintf(clonename
, sizeof (clonename
),
5170 "%s/ch1_%llu", osname
, (u_longlong_t
)id
);
5171 (void) snprintf(tag
, sizeof (tag
), "tag_%llu", (u_longlong_t
)id
);
5174 * Clean up from any previous run.
5176 error
= dsl_destroy_head(clonename
);
5177 if (error
!= ENOENT
)
5179 error
= user_release_one(fullname
, tag
);
5180 if (error
!= ESRCH
&& error
!= ENOENT
)
5182 error
= dsl_destroy_snapshot(fullname
, B_FALSE
);
5183 if (error
!= ENOENT
)
5187 * Create snapshot, clone it, mark snap for deferred destroy,
5188 * destroy clone, verify snap was also destroyed.
5190 error
= dmu_objset_snapshot_one(osname
, snapname
);
5192 if (error
== ENOSPC
) {
5193 ztest_record_enospc("dmu_objset_snapshot");
5196 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname
, error
);
5199 error
= dmu_objset_clone(clonename
, fullname
);
5201 if (error
== ENOSPC
) {
5202 ztest_record_enospc("dmu_objset_clone");
5205 fatal(0, "dmu_objset_clone(%s) = %d", clonename
, error
);
5208 error
= dsl_destroy_snapshot(fullname
, B_TRUE
);
5210 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5214 error
= dsl_destroy_head(clonename
);
5216 fatal(0, "dsl_destroy_head(%s) = %d", clonename
, error
);
5218 error
= dmu_objset_hold(fullname
, FTAG
, &origin
);
5219 if (error
!= ENOENT
)
5220 fatal(0, "dmu_objset_hold(%s) = %d", fullname
, error
);
5223 * Create snapshot, add temporary hold, verify that we can't
5224 * destroy a held snapshot, mark for deferred destroy,
5225 * release hold, verify snapshot was destroyed.
5227 error
= dmu_objset_snapshot_one(osname
, snapname
);
5229 if (error
== ENOSPC
) {
5230 ztest_record_enospc("dmu_objset_snapshot");
5233 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname
, error
);
5236 holds
= fnvlist_alloc();
5237 fnvlist_add_string(holds
, fullname
, tag
);
5238 error
= dsl_dataset_user_hold(holds
, 0, NULL
);
5239 fnvlist_free(holds
);
5241 if (error
== ENOSPC
) {
5242 ztest_record_enospc("dsl_dataset_user_hold");
5245 fatal(0, "dsl_dataset_user_hold(%s, %s) = %u",
5246 fullname
, tag
, error
);
5249 error
= dsl_destroy_snapshot(fullname
, B_FALSE
);
5250 if (error
!= EBUSY
) {
5251 fatal(0, "dsl_destroy_snapshot(%s, B_FALSE) = %d",
5255 error
= dsl_destroy_snapshot(fullname
, B_TRUE
);
5257 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5261 error
= user_release_one(fullname
, tag
);
5263 fatal(0, "user_release_one(%s, %s) = %d", fullname
, tag
, error
);
5265 VERIFY3U(dmu_objset_hold(fullname
, FTAG
, &origin
), ==, ENOENT
);
5268 (void) rw_unlock(&ztest_name_lock
);
5272 * Inject random faults into the on-disk data.
5276 ztest_fault_inject(ztest_ds_t
*zd
, uint64_t id
)
5278 ztest_shared_t
*zs
= ztest_shared
;
5279 spa_t
*spa
= ztest_spa
;
5283 uint64_t bad
= 0x1990c0ffeedecadeull
;
5288 int bshift
= SPA_MAXBLOCKSHIFT
+ 2;
5294 boolean_t islog
= B_FALSE
;
5296 path0
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
5297 pathrand
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
5299 mutex_enter(&ztest_vdev_lock
);
5300 maxfaults
= MAXFAULTS();
5301 leaves
= MAX(zs
->zs_mirrors
, 1) * ztest_opts
.zo_raidz
;
5302 mirror_save
= zs
->zs_mirrors
;
5303 mutex_exit(&ztest_vdev_lock
);
5305 ASSERT(leaves
>= 1);
5308 * Grab the name lock as reader. There are some operations
5309 * which don't like to have their vdevs changed while
5310 * they are in progress (i.e. spa_change_guid). Those
5311 * operations will have grabbed the name lock as writer.
5313 (void) rw_rdlock(&ztest_name_lock
);
5316 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
5318 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
5320 if (ztest_random(2) == 0) {
5322 * Inject errors on a normal data device or slog device.
5324 top
= ztest_random_vdev_top(spa
, B_TRUE
);
5325 leaf
= ztest_random(leaves
) + zs
->zs_splits
;
5328 * Generate paths to the first leaf in this top-level vdev,
5329 * and to the random leaf we selected. We'll induce transient
5330 * write failures and random online/offline activity on leaf 0,
5331 * and we'll write random garbage to the randomly chosen leaf.
5333 (void) snprintf(path0
, MAXPATHLEN
, ztest_dev_template
,
5334 ztest_opts
.zo_dir
, ztest_opts
.zo_pool
,
5335 top
* leaves
+ zs
->zs_splits
);
5336 (void) snprintf(pathrand
, MAXPATHLEN
, ztest_dev_template
,
5337 ztest_opts
.zo_dir
, ztest_opts
.zo_pool
,
5338 top
* leaves
+ leaf
);
5340 vd0
= vdev_lookup_by_path(spa
->spa_root_vdev
, path0
);
5341 if (vd0
!= NULL
&& vd0
->vdev_top
->vdev_islog
)
5345 * If the top-level vdev needs to be resilvered
5346 * then we only allow faults on the device that is
5349 if (vd0
!= NULL
&& maxfaults
!= 1 &&
5350 (!vdev_resilver_needed(vd0
->vdev_top
, NULL
, NULL
) ||
5351 vd0
->vdev_resilver_txg
!= 0)) {
5353 * Make vd0 explicitly claim to be unreadable,
5354 * or unwriteable, or reach behind its back
5355 * and close the underlying fd. We can do this if
5356 * maxfaults == 0 because we'll fail and reexecute,
5357 * and we can do it if maxfaults >= 2 because we'll
5358 * have enough redundancy. If maxfaults == 1, the
5359 * combination of this with injection of random data
5360 * corruption below exceeds the pool's fault tolerance.
5362 vdev_file_t
*vf
= vd0
->vdev_tsd
;
5364 if (vf
!= NULL
&& ztest_random(3) == 0) {
5365 (void) close(vf
->vf_vnode
->v_fd
);
5366 vf
->vf_vnode
->v_fd
= -1;
5367 } else if (ztest_random(2) == 0) {
5368 vd0
->vdev_cant_read
= B_TRUE
;
5370 vd0
->vdev_cant_write
= B_TRUE
;
5372 guid0
= vd0
->vdev_guid
;
5376 * Inject errors on an l2cache device.
5378 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
5380 if (sav
->sav_count
== 0) {
5381 spa_config_exit(spa
, SCL_STATE
, FTAG
);
5382 (void) rw_unlock(&ztest_name_lock
);
5385 vd0
= sav
->sav_vdevs
[ztest_random(sav
->sav_count
)];
5386 guid0
= vd0
->vdev_guid
;
5387 (void) strcpy(path0
, vd0
->vdev_path
);
5388 (void) strcpy(pathrand
, vd0
->vdev_path
);
5392 maxfaults
= INT_MAX
; /* no limit on cache devices */
5395 spa_config_exit(spa
, SCL_STATE
, FTAG
);
5396 (void) rw_unlock(&ztest_name_lock
);
5399 * If we can tolerate two or more faults, or we're dealing
5400 * with a slog, randomly online/offline vd0.
5402 if ((maxfaults
>= 2 || islog
) && guid0
!= 0) {
5403 if (ztest_random(10) < 6) {
5404 int flags
= (ztest_random(2) == 0 ?
5405 ZFS_OFFLINE_TEMPORARY
: 0);
5408 * We have to grab the zs_name_lock as writer to
5409 * prevent a race between offlining a slog and
5410 * destroying a dataset. Offlining the slog will
5411 * grab a reference on the dataset which may cause
5412 * dsl_destroy_head() to fail with EBUSY thus
5413 * leaving the dataset in an inconsistent state.
5416 (void) rw_wrlock(&ztest_name_lock
);
5418 VERIFY(vdev_offline(spa
, guid0
, flags
) != EBUSY
);
5421 (void) rw_unlock(&ztest_name_lock
);
5424 * Ideally we would like to be able to randomly
5425 * call vdev_[on|off]line without holding locks
5426 * to force unpredictable failures but the side
5427 * effects of vdev_[on|off]line prevent us from
5428 * doing so. We grab the ztest_vdev_lock here to
5429 * prevent a race between injection testing and
5432 mutex_enter(&ztest_vdev_lock
);
5433 (void) vdev_online(spa
, guid0
, 0, NULL
);
5434 mutex_exit(&ztest_vdev_lock
);
5442 * We have at least single-fault tolerance, so inject data corruption.
5444 fd
= open(pathrand
, O_RDWR
);
5446 if (fd
== -1) /* we hit a gap in the device namespace */
5449 fsize
= lseek(fd
, 0, SEEK_END
);
5451 while (--iters
!= 0) {
5453 * The offset must be chosen carefully to ensure that
5454 * we do not inject a given logical block with errors
5455 * on two different leaf devices, because ZFS can not
5456 * tolerate that (if maxfaults==1).
5458 * We divide each leaf into chunks of size
5459 * (# leaves * SPA_MAXBLOCKSIZE * 4). Within each chunk
5460 * there is a series of ranges to which we can inject errors.
5461 * Each range can accept errors on only a single leaf vdev.
5462 * The error injection ranges are separated by ranges
5463 * which we will not inject errors on any device (DMZs).
5464 * Each DMZ must be large enough such that a single block
5465 * can not straddle it, so that a single block can not be
5466 * a target in two different injection ranges (on different
5469 * For example, with 3 leaves, each chunk looks like:
5470 * 0 to 32M: injection range for leaf 0
5471 * 32M to 64M: DMZ - no injection allowed
5472 * 64M to 96M: injection range for leaf 1
5473 * 96M to 128M: DMZ - no injection allowed
5474 * 128M to 160M: injection range for leaf 2
5475 * 160M to 192M: DMZ - no injection allowed
5477 offset
= ztest_random(fsize
/ (leaves
<< bshift
)) *
5478 (leaves
<< bshift
) + (leaf
<< bshift
) +
5479 (ztest_random(1ULL << (bshift
- 1)) & -8ULL);
5482 * Only allow damage to the labels at one end of the vdev.
5484 * If all labels are damaged, the device will be totally
5485 * inaccessible, which will result in loss of data,
5486 * because we also damage (parts of) the other side of
5489 * Additionally, we will always have both an even and an
5490 * odd label, so that we can handle crashes in the
5491 * middle of vdev_config_sync().
5493 if ((leaf
& 1) == 0 && offset
< VDEV_LABEL_START_SIZE
)
5497 * The two end labels are stored at the "end" of the disk, but
5498 * the end of the disk (vdev_psize) is aligned to
5499 * sizeof (vdev_label_t).
5501 uint64_t psize
= P2ALIGN(fsize
, sizeof (vdev_label_t
));
5502 if ((leaf
& 1) == 1 &&
5503 offset
+ sizeof (bad
) > psize
- VDEV_LABEL_END_SIZE
)
5506 mutex_enter(&ztest_vdev_lock
);
5507 if (mirror_save
!= zs
->zs_mirrors
) {
5508 mutex_exit(&ztest_vdev_lock
);
5513 if (pwrite(fd
, &bad
, sizeof (bad
), offset
) != sizeof (bad
))
5514 fatal(1, "can't inject bad word at 0x%llx in %s",
5517 mutex_exit(&ztest_vdev_lock
);
5519 if (ztest_opts
.zo_verbose
>= 7)
5520 (void) printf("injected bad word into %s,"
5521 " offset 0x%llx\n", pathrand
, (u_longlong_t
)offset
);
5526 umem_free(path0
, MAXPATHLEN
);
5527 umem_free(pathrand
, MAXPATHLEN
);
5531 * Verify that DDT repair works as expected.
5534 ztest_ddt_repair(ztest_ds_t
*zd
, uint64_t id
)
5536 ztest_shared_t
*zs
= ztest_shared
;
5537 spa_t
*spa
= ztest_spa
;
5538 objset_t
*os
= zd
->zd_os
;
5540 uint64_t object
, blocksize
, txg
, pattern
, psize
;
5541 enum zio_checksum checksum
= spa_dedup_checksum(spa
);
5546 int copies
= 2 * ZIO_DEDUPDITTO_MIN
;
5549 blocksize
= ztest_random_blocksize();
5550 blocksize
= MIN(blocksize
, 2048); /* because we write so many */
5552 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
5553 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, blocksize
, 0, 0);
5555 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
), B_FALSE
) != 0) {
5556 umem_free(od
, sizeof (ztest_od_t
));
5561 * Take the name lock as writer to prevent anyone else from changing
5562 * the pool and dataset properies we need to maintain during this test.
5564 (void) rw_wrlock(&ztest_name_lock
);
5566 if (ztest_dsl_prop_set_uint64(zd
->zd_name
, ZFS_PROP_DEDUP
, checksum
,
5568 ztest_dsl_prop_set_uint64(zd
->zd_name
, ZFS_PROP_COPIES
, 1,
5570 (void) rw_unlock(&ztest_name_lock
);
5571 umem_free(od
, sizeof (ztest_od_t
));
5575 dmu_objset_stats_t dds
;
5576 dsl_pool_config_enter(dmu_objset_pool(os
), FTAG
);
5577 dmu_objset_fast_stat(os
, &dds
);
5578 dsl_pool_config_exit(dmu_objset_pool(os
), FTAG
);
5580 object
= od
[0].od_object
;
5581 blocksize
= od
[0].od_blocksize
;
5582 pattern
= zs
->zs_guid
^ dds
.dds_guid
;
5584 ASSERT(object
!= 0);
5586 tx
= dmu_tx_create(os
);
5587 dmu_tx_hold_write(tx
, object
, 0, copies
* blocksize
);
5588 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
5590 (void) rw_unlock(&ztest_name_lock
);
5591 umem_free(od
, sizeof (ztest_od_t
));
5596 * Write all the copies of our block.
5598 for (i
= 0; i
< copies
; i
++) {
5599 uint64_t offset
= i
* blocksize
;
5600 int error
= dmu_buf_hold(os
, object
, offset
, FTAG
, &db
,
5601 DMU_READ_NO_PREFETCH
);
5603 fatal(B_FALSE
, "dmu_buf_hold(%p, %llu, %llu) = %u",
5604 os
, (long long)object
, (long long) offset
, error
);
5606 ASSERT(db
->db_offset
== offset
);
5607 ASSERT(db
->db_size
== blocksize
);
5608 ASSERT(ztest_pattern_match(db
->db_data
, db
->db_size
, pattern
) ||
5609 ztest_pattern_match(db
->db_data
, db
->db_size
, 0ULL));
5610 dmu_buf_will_fill(db
, tx
);
5611 ztest_pattern_set(db
->db_data
, db
->db_size
, pattern
);
5612 dmu_buf_rele(db
, FTAG
);
5616 txg_wait_synced(spa_get_dsl(spa
), txg
);
5619 * Find out what block we got.
5621 VERIFY0(dmu_buf_hold(os
, object
, 0, FTAG
, &db
,
5622 DMU_READ_NO_PREFETCH
));
5623 blk
= *((dmu_buf_impl_t
*)db
)->db_blkptr
;
5624 dmu_buf_rele(db
, FTAG
);
5627 * Damage the block. Dedup-ditto will save us when we read it later.
5629 psize
= BP_GET_PSIZE(&blk
);
5630 abd
= abd_alloc_linear(psize
, B_TRUE
);
5631 ztest_pattern_set(abd_to_buf(abd
), psize
, ~pattern
);
5633 (void) zio_wait(zio_rewrite(NULL
, spa
, 0, &blk
,
5634 abd
, psize
, NULL
, NULL
, ZIO_PRIORITY_SYNC_WRITE
,
5635 ZIO_FLAG_CANFAIL
| ZIO_FLAG_INDUCE_DAMAGE
, NULL
));
5639 (void) rw_unlock(&ztest_name_lock
);
5640 umem_free(od
, sizeof (ztest_od_t
));
5648 ztest_scrub(ztest_ds_t
*zd
, uint64_t id
)
5650 spa_t
*spa
= ztest_spa
;
5652 (void) spa_scan(spa
, POOL_SCAN_SCRUB
);
5653 (void) poll(NULL
, 0, 100); /* wait a moment, then force a restart */
5654 (void) spa_scan(spa
, POOL_SCAN_SCRUB
);
5658 * Change the guid for the pool.
5662 ztest_reguid(ztest_ds_t
*zd
, uint64_t id
)
5664 spa_t
*spa
= ztest_spa
;
5665 uint64_t orig
, load
;
5668 if (ztest_opts
.zo_mmp_test
)
5671 orig
= spa_guid(spa
);
5672 load
= spa_load_guid(spa
);
5674 (void) rw_wrlock(&ztest_name_lock
);
5675 error
= spa_change_guid(spa
);
5676 (void) rw_unlock(&ztest_name_lock
);
5681 if (ztest_opts
.zo_verbose
>= 4) {
5682 (void) printf("Changed guid old %llu -> %llu\n",
5683 (u_longlong_t
)orig
, (u_longlong_t
)spa_guid(spa
));
5686 VERIFY3U(orig
, !=, spa_guid(spa
));
5687 VERIFY3U(load
, ==, spa_load_guid(spa
));
5691 * Rename the pool to a different name and then rename it back.
5695 ztest_spa_rename(ztest_ds_t
*zd
, uint64_t id
)
5697 char *oldname
, *newname
;
5700 if (ztest_opts
.zo_mmp_test
)
5703 (void) rw_wrlock(&ztest_name_lock
);
5705 oldname
= ztest_opts
.zo_pool
;
5706 newname
= umem_alloc(strlen(oldname
) + 5, UMEM_NOFAIL
);
5707 (void) strcpy(newname
, oldname
);
5708 (void) strcat(newname
, "_tmp");
5713 VERIFY3U(0, ==, spa_rename(oldname
, newname
));
5716 * Try to open it under the old name, which shouldn't exist
5718 VERIFY3U(ENOENT
, ==, spa_open(oldname
, &spa
, FTAG
));
5721 * Open it under the new name and make sure it's still the same spa_t.
5723 VERIFY3U(0, ==, spa_open(newname
, &spa
, FTAG
));
5725 ASSERT(spa
== ztest_spa
);
5726 spa_close(spa
, FTAG
);
5729 * Rename it back to the original
5731 VERIFY3U(0, ==, spa_rename(newname
, oldname
));
5734 * Make sure it can still be opened
5736 VERIFY3U(0, ==, spa_open(oldname
, &spa
, FTAG
));
5738 ASSERT(spa
== ztest_spa
);
5739 spa_close(spa
, FTAG
);
5741 umem_free(newname
, strlen(newname
) + 1);
5743 (void) rw_unlock(&ztest_name_lock
);
5747 ztest_fletcher(ztest_ds_t
*zd
, uint64_t id
)
5749 hrtime_t end
= gethrtime() + NANOSEC
;
5751 while (gethrtime() <= end
) {
5752 int run_count
= 100;
5754 struct abd
*abd_data
, *abd_meta
;
5759 zio_cksum_t zc_ref_byteswap
;
5761 size
= ztest_random_blocksize();
5763 buf
= umem_alloc(size
, UMEM_NOFAIL
);
5764 abd_data
= abd_alloc(size
, B_FALSE
);
5765 abd_meta
= abd_alloc(size
, B_TRUE
);
5767 for (i
= 0, ptr
= buf
; i
< size
/ sizeof (*ptr
); i
++, ptr
++)
5768 *ptr
= ztest_random(UINT_MAX
);
5770 abd_copy_from_buf_off(abd_data
, buf
, 0, size
);
5771 abd_copy_from_buf_off(abd_meta
, buf
, 0, size
);
5773 VERIFY0(fletcher_4_impl_set("scalar"));
5774 fletcher_4_native(buf
, size
, NULL
, &zc_ref
);
5775 fletcher_4_byteswap(buf
, size
, NULL
, &zc_ref_byteswap
);
5777 VERIFY0(fletcher_4_impl_set("cycle"));
5778 while (run_count
-- > 0) {
5780 zio_cksum_t zc_byteswap
;
5782 fletcher_4_byteswap(buf
, size
, NULL
, &zc_byteswap
);
5783 fletcher_4_native(buf
, size
, NULL
, &zc
);
5785 VERIFY0(bcmp(&zc
, &zc_ref
, sizeof (zc
)));
5786 VERIFY0(bcmp(&zc_byteswap
, &zc_ref_byteswap
,
5787 sizeof (zc_byteswap
)));
5789 /* Test ABD - data */
5790 abd_fletcher_4_byteswap(abd_data
, size
, NULL
,
5792 abd_fletcher_4_native(abd_data
, size
, NULL
, &zc
);
5794 VERIFY0(bcmp(&zc
, &zc_ref
, sizeof (zc
)));
5795 VERIFY0(bcmp(&zc_byteswap
, &zc_ref_byteswap
,
5796 sizeof (zc_byteswap
)));
5798 /* Test ABD - metadata */
5799 abd_fletcher_4_byteswap(abd_meta
, size
, NULL
,
5801 abd_fletcher_4_native(abd_meta
, size
, NULL
, &zc
);
5803 VERIFY0(bcmp(&zc
, &zc_ref
, sizeof (zc
)));
5804 VERIFY0(bcmp(&zc_byteswap
, &zc_ref_byteswap
,
5805 sizeof (zc_byteswap
)));
5809 umem_free(buf
, size
);
5816 ztest_fletcher_incr(ztest_ds_t
*zd
, uint64_t id
)
5823 zio_cksum_t zc_ref_bswap
;
5825 hrtime_t end
= gethrtime() + NANOSEC
;
5827 while (gethrtime() <= end
) {
5828 int run_count
= 100;
5830 size
= ztest_random_blocksize();
5831 buf
= umem_alloc(size
, UMEM_NOFAIL
);
5833 for (i
= 0, ptr
= buf
; i
< size
/ sizeof (*ptr
); i
++, ptr
++)
5834 *ptr
= ztest_random(UINT_MAX
);
5836 VERIFY0(fletcher_4_impl_set("scalar"));
5837 fletcher_4_native(buf
, size
, NULL
, &zc_ref
);
5838 fletcher_4_byteswap(buf
, size
, NULL
, &zc_ref_bswap
);
5840 VERIFY0(fletcher_4_impl_set("cycle"));
5842 while (run_count
-- > 0) {
5844 zio_cksum_t zc_bswap
;
5847 ZIO_SET_CHECKSUM(&zc
, 0, 0, 0, 0);
5848 ZIO_SET_CHECKSUM(&zc_bswap
, 0, 0, 0, 0);
5850 while (pos
< size
) {
5851 size_t inc
= 64 * ztest_random(size
/ 67);
5852 /* sometimes add few bytes to test non-simd */
5853 if (ztest_random(100) < 10)
5854 inc
+= P2ALIGN(ztest_random(64),
5857 if (inc
> (size
- pos
))
5860 fletcher_4_incremental_native(buf
+ pos
, inc
,
5862 fletcher_4_incremental_byteswap(buf
+ pos
, inc
,
5868 VERIFY3U(pos
, ==, size
);
5870 VERIFY(ZIO_CHECKSUM_EQUAL(zc
, zc_ref
));
5871 VERIFY(ZIO_CHECKSUM_EQUAL(zc_bswap
, zc_ref_bswap
));
5874 * verify if incremental on the whole buffer is
5875 * equivalent to non-incremental version
5877 ZIO_SET_CHECKSUM(&zc
, 0, 0, 0, 0);
5878 ZIO_SET_CHECKSUM(&zc_bswap
, 0, 0, 0, 0);
5880 fletcher_4_incremental_native(buf
, size
, &zc
);
5881 fletcher_4_incremental_byteswap(buf
, size
, &zc_bswap
);
5883 VERIFY(ZIO_CHECKSUM_EQUAL(zc
, zc_ref
));
5884 VERIFY(ZIO_CHECKSUM_EQUAL(zc_bswap
, zc_ref_bswap
));
5887 umem_free(buf
, size
);
5892 ztest_check_path(char *path
)
5895 /* return true on success */
5896 return (!stat(path
, &s
));
5900 ztest_get_zdb_bin(char *bin
, int len
)
5904 * Try to use ZDB_PATH and in-tree zdb path. If not successful, just
5905 * let popen to search through PATH.
5907 if ((zdb_path
= getenv("ZDB_PATH"))) {
5908 strlcpy(bin
, zdb_path
, len
); /* In env */
5909 if (!ztest_check_path(bin
)) {
5910 ztest_dump_core
= 0;
5911 fatal(1, "invalid ZDB_PATH '%s'", bin
);
5916 VERIFY(realpath(getexecname(), bin
) != NULL
);
5917 if (strstr(bin
, "/ztest/")) {
5918 strstr(bin
, "/ztest/")[0] = '\0'; /* In-tree */
5919 strcat(bin
, "/zdb/zdb");
5920 if (ztest_check_path(bin
))
5927 * Verify pool integrity by running zdb.
5930 ztest_run_zdb(char *pool
)
5936 const int len
= MAXPATHLEN
+ MAXNAMELEN
+ 20;
5939 bin
= umem_alloc(len
, UMEM_NOFAIL
);
5940 zdb
= umem_alloc(len
, UMEM_NOFAIL
);
5941 zbuf
= umem_alloc(1024, UMEM_NOFAIL
);
5943 ztest_get_zdb_bin(bin
, len
);
5946 "%s -bcc%s%s -G -d -U %s %s",
5948 ztest_opts
.zo_verbose
>= 3 ? "s" : "",
5949 ztest_opts
.zo_verbose
>= 4 ? "v" : "",
5953 if (ztest_opts
.zo_verbose
>= 5)
5954 (void) printf("Executing %s\n", strstr(zdb
, "zdb "));
5956 fp
= popen(zdb
, "r");
5958 while (fgets(zbuf
, 1024, fp
) != NULL
)
5959 if (ztest_opts
.zo_verbose
>= 3)
5960 (void) printf("%s", zbuf
);
5962 status
= pclose(fp
);
5967 ztest_dump_core
= 0;
5968 if (WIFEXITED(status
))
5969 fatal(0, "'%s' exit code %d", zdb
, WEXITSTATUS(status
));
5971 fatal(0, "'%s' died with signal %d", zdb
, WTERMSIG(status
));
5973 umem_free(bin
, len
);
5974 umem_free(zdb
, len
);
5975 umem_free(zbuf
, 1024);
5979 ztest_walk_pool_directory(char *header
)
5983 if (ztest_opts
.zo_verbose
>= 6)
5984 (void) printf("%s\n", header
);
5986 mutex_enter(&spa_namespace_lock
);
5987 while ((spa
= spa_next(spa
)) != NULL
)
5988 if (ztest_opts
.zo_verbose
>= 6)
5989 (void) printf("\t%s\n", spa_name(spa
));
5990 mutex_exit(&spa_namespace_lock
);
5994 ztest_spa_import_export(char *oldname
, char *newname
)
5996 nvlist_t
*config
, *newconfig
;
6001 if (ztest_opts
.zo_verbose
>= 4) {
6002 (void) printf("import/export: old = %s, new = %s\n",
6007 * Clean up from previous runs.
6009 (void) spa_destroy(newname
);
6012 * Get the pool's configuration and guid.
6014 VERIFY3U(0, ==, spa_open(oldname
, &spa
, FTAG
));
6017 * Kick off a scrub to tickle scrub/export races.
6019 if (ztest_random(2) == 0)
6020 (void) spa_scan(spa
, POOL_SCAN_SCRUB
);
6022 pool_guid
= spa_guid(spa
);
6023 spa_close(spa
, FTAG
);
6025 ztest_walk_pool_directory("pools before export");
6030 VERIFY3U(0, ==, spa_export(oldname
, &config
, B_FALSE
, B_FALSE
));
6032 ztest_walk_pool_directory("pools after export");
6037 newconfig
= spa_tryimport(config
);
6038 ASSERT(newconfig
!= NULL
);
6039 nvlist_free(newconfig
);
6042 * Import it under the new name.
6044 error
= spa_import(newname
, config
, NULL
, 0);
6046 dump_nvlist(config
, 0);
6047 fatal(B_FALSE
, "couldn't import pool %s as %s: error %u",
6048 oldname
, newname
, error
);
6051 ztest_walk_pool_directory("pools after import");
6054 * Try to import it again -- should fail with EEXIST.
6056 VERIFY3U(EEXIST
, ==, spa_import(newname
, config
, NULL
, 0));
6059 * Try to import it under a different name -- should fail with EEXIST.
6061 VERIFY3U(EEXIST
, ==, spa_import(oldname
, config
, NULL
, 0));
6064 * Verify that the pool is no longer visible under the old name.
6066 VERIFY3U(ENOENT
, ==, spa_open(oldname
, &spa
, FTAG
));
6069 * Verify that we can open and close the pool using the new name.
6071 VERIFY3U(0, ==, spa_open(newname
, &spa
, FTAG
));
6072 ASSERT(pool_guid
== spa_guid(spa
));
6073 spa_close(spa
, FTAG
);
6075 nvlist_free(config
);
6079 ztest_resume(spa_t
*spa
)
6081 if (spa_suspended(spa
) && ztest_opts
.zo_verbose
>= 6)
6082 (void) printf("resuming from suspended state\n");
6083 spa_vdev_state_enter(spa
, SCL_NONE
);
6084 vdev_clear(spa
, NULL
);
6085 (void) spa_vdev_state_exit(spa
, NULL
, 0);
6086 (void) zio_resume(spa
);
6090 ztest_resume_thread(void *arg
)
6094 while (!ztest_exiting
) {
6095 if (spa_suspended(spa
))
6097 (void) poll(NULL
, 0, 100);
6100 * Periodically change the zfs_compressed_arc_enabled setting.
6102 if (ztest_random(10) == 0)
6103 zfs_compressed_arc_enabled
= ztest_random(2);
6106 * Periodically change the zfs_abd_scatter_enabled setting.
6108 if (ztest_random(10) == 0)
6109 zfs_abd_scatter_enabled
= ztest_random(2);
6121 ztest_deadman_alarm(int sig
)
6123 fatal(0, "failed to complete within %d seconds of deadline", GRACE
);
6128 ztest_execute(int test
, ztest_info_t
*zi
, uint64_t id
)
6130 ztest_ds_t
*zd
= &ztest_ds
[id
% ztest_opts
.zo_datasets
];
6131 ztest_shared_callstate_t
*zc
= ZTEST_GET_SHARED_CALLSTATE(test
);
6132 hrtime_t functime
= gethrtime();
6135 for (i
= 0; i
< zi
->zi_iters
; i
++)
6136 zi
->zi_func(zd
, id
);
6138 functime
= gethrtime() - functime
;
6140 atomic_add_64(&zc
->zc_count
, 1);
6141 atomic_add_64(&zc
->zc_time
, functime
);
6143 if (ztest_opts
.zo_verbose
>= 4)
6144 (void) printf("%6.2f sec in %s\n",
6145 (double)functime
/ NANOSEC
, zi
->zi_funcname
);
6149 ztest_thread(void *arg
)
6152 uint64_t id
= (uintptr_t)arg
;
6153 ztest_shared_t
*zs
= ztest_shared
;
6157 ztest_shared_callstate_t
*zc
;
6159 while ((now
= gethrtime()) < zs
->zs_thread_stop
) {
6161 * See if it's time to force a crash.
6163 if (now
> zs
->zs_thread_kill
)
6167 * If we're getting ENOSPC with some regularity, stop.
6169 if (zs
->zs_enospc_count
> 10)
6173 * Pick a random function to execute.
6175 rand
= ztest_random(ZTEST_FUNCS
);
6176 zi
= &ztest_info
[rand
];
6177 zc
= ZTEST_GET_SHARED_CALLSTATE(rand
);
6178 call_next
= zc
->zc_next
;
6180 if (now
>= call_next
&&
6181 atomic_cas_64(&zc
->zc_next
, call_next
, call_next
+
6182 ztest_random(2 * zi
->zi_interval
[0] + 1)) == call_next
) {
6183 ztest_execute(rand
, zi
, id
);
6193 ztest_dataset_name(char *dsname
, char *pool
, int d
)
6195 (void) snprintf(dsname
, ZFS_MAX_DATASET_NAME_LEN
, "%s/ds_%d", pool
, d
);
6199 ztest_dataset_destroy(int d
)
6201 char name
[ZFS_MAX_DATASET_NAME_LEN
];
6204 ztest_dataset_name(name
, ztest_opts
.zo_pool
, d
);
6206 if (ztest_opts
.zo_verbose
>= 3)
6207 (void) printf("Destroying %s to free up space\n", name
);
6210 * Cleanup any non-standard clones and snapshots. In general,
6211 * ztest thread t operates on dataset (t % zopt_datasets),
6212 * so there may be more than one thing to clean up.
6214 for (t
= d
; t
< ztest_opts
.zo_threads
;
6215 t
+= ztest_opts
.zo_datasets
)
6216 ztest_dsl_dataset_cleanup(name
, t
);
6218 (void) dmu_objset_find(name
, ztest_objset_destroy_cb
, NULL
,
6219 DS_FIND_SNAPSHOTS
| DS_FIND_CHILDREN
);
6223 ztest_dataset_dirobj_verify(ztest_ds_t
*zd
)
6225 uint64_t usedobjs
, dirobjs
, scratch
;
6228 * ZTEST_DIROBJ is the object directory for the entire dataset.
6229 * Therefore, the number of objects in use should equal the
6230 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
6231 * If not, we have an object leak.
6233 * Note that we can only check this in ztest_dataset_open(),
6234 * when the open-context and syncing-context values agree.
6235 * That's because zap_count() returns the open-context value,
6236 * while dmu_objset_space() returns the rootbp fill count.
6238 VERIFY3U(0, ==, zap_count(zd
->zd_os
, ZTEST_DIROBJ
, &dirobjs
));
6239 dmu_objset_space(zd
->zd_os
, &scratch
, &scratch
, &usedobjs
, &scratch
);
6240 ASSERT3U(dirobjs
+ 1, ==, usedobjs
);
6244 ztest_dataset_open(int d
)
6246 ztest_ds_t
*zd
= &ztest_ds
[d
];
6247 uint64_t committed_seq
= ZTEST_GET_SHARED_DS(d
)->zd_seq
;
6250 char name
[ZFS_MAX_DATASET_NAME_LEN
];
6253 ztest_dataset_name(name
, ztest_opts
.zo_pool
, d
);
6255 (void) rw_rdlock(&ztest_name_lock
);
6257 error
= ztest_dataset_create(name
);
6258 if (error
== ENOSPC
) {
6259 (void) rw_unlock(&ztest_name_lock
);
6260 ztest_record_enospc(FTAG
);
6263 ASSERT(error
== 0 || error
== EEXIST
);
6265 VERIFY0(dmu_objset_own(name
, DMU_OST_OTHER
, B_FALSE
, zd
, &os
));
6266 (void) rw_unlock(&ztest_name_lock
);
6268 ztest_zd_init(zd
, ZTEST_GET_SHARED_DS(d
), os
);
6270 zilog
= zd
->zd_zilog
;
6272 if (zilog
->zl_header
->zh_claim_lr_seq
!= 0 &&
6273 zilog
->zl_header
->zh_claim_lr_seq
< committed_seq
)
6274 fatal(0, "missing log records: claimed %llu < committed %llu",
6275 zilog
->zl_header
->zh_claim_lr_seq
, committed_seq
);
6277 ztest_dataset_dirobj_verify(zd
);
6279 zil_replay(os
, zd
, ztest_replay_vector
);
6281 ztest_dataset_dirobj_verify(zd
);
6283 if (ztest_opts
.zo_verbose
>= 6)
6284 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
6286 (u_longlong_t
)zilog
->zl_parse_blk_count
,
6287 (u_longlong_t
)zilog
->zl_parse_lr_count
,
6288 (u_longlong_t
)zilog
->zl_replaying_seq
);
6290 zilog
= zil_open(os
, ztest_get_data
);
6292 if (zilog
->zl_replaying_seq
!= 0 &&
6293 zilog
->zl_replaying_seq
< committed_seq
)
6294 fatal(0, "missing log records: replayed %llu < committed %llu",
6295 zilog
->zl_replaying_seq
, committed_seq
);
6301 ztest_dataset_close(int d
)
6303 ztest_ds_t
*zd
= &ztest_ds
[d
];
6305 zil_close(zd
->zd_zilog
);
6306 dmu_objset_disown(zd
->zd_os
, zd
);
6312 * Kick off threads to run tests on all datasets in parallel.
6315 ztest_run(ztest_shared_t
*zs
)
6320 kthread_t
*resume_thread
;
6325 ztest_exiting
= B_FALSE
;
6328 * Initialize parent/child shared state.
6330 mutex_init(&ztest_vdev_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
6331 VERIFY(rwlock_init(&ztest_name_lock
, USYNC_THREAD
, NULL
) == 0);
6333 zs
->zs_thread_start
= gethrtime();
6334 zs
->zs_thread_stop
=
6335 zs
->zs_thread_start
+ ztest_opts
.zo_passtime
* NANOSEC
;
6336 zs
->zs_thread_stop
= MIN(zs
->zs_thread_stop
, zs
->zs_proc_stop
);
6337 zs
->zs_thread_kill
= zs
->zs_thread_stop
;
6338 if (ztest_random(100) < ztest_opts
.zo_killrate
) {
6339 zs
->zs_thread_kill
-=
6340 ztest_random(ztest_opts
.zo_passtime
* NANOSEC
);
6343 mutex_init(&zcl
.zcl_callbacks_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
6345 list_create(&zcl
.zcl_callbacks
, sizeof (ztest_cb_data_t
),
6346 offsetof(ztest_cb_data_t
, zcd_node
));
6351 kernel_init(FREAD
| FWRITE
);
6352 VERIFY0(spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
));
6353 spa
->spa_debug
= B_TRUE
;
6354 metaslab_preload_limit
= ztest_random(20) + 1;
6357 dmu_objset_stats_t dds
;
6358 VERIFY0(dmu_objset_own(ztest_opts
.zo_pool
,
6359 DMU_OST_ANY
, B_TRUE
, FTAG
, &os
));
6360 dsl_pool_config_enter(dmu_objset_pool(os
), FTAG
);
6361 dmu_objset_fast_stat(os
, &dds
);
6362 dsl_pool_config_exit(dmu_objset_pool(os
), FTAG
);
6363 zs
->zs_guid
= dds
.dds_guid
;
6364 dmu_objset_disown(os
, FTAG
);
6366 spa
->spa_dedup_ditto
= 2 * ZIO_DEDUPDITTO_MIN
;
6369 * We don't expect the pool to suspend unless maxfaults == 0,
6370 * in which case ztest_fault_inject() temporarily takes away
6371 * the only valid replica.
6373 if (MAXFAULTS() == 0)
6374 spa
->spa_failmode
= ZIO_FAILURE_MODE_WAIT
;
6376 spa
->spa_failmode
= ZIO_FAILURE_MODE_PANIC
;
6379 * Create a thread to periodically resume suspended I/O.
6381 VERIFY3P((resume_thread
= zk_thread_create(NULL
, 0,
6382 (thread_func_t
)ztest_resume_thread
, spa
, 0, NULL
, TS_RUN
, 0,
6383 PTHREAD_CREATE_JOINABLE
)), !=, NULL
);
6387 * Set a deadman alarm to abort() if we hang.
6389 signal(SIGALRM
, ztest_deadman_alarm
);
6390 alarm((zs
->zs_thread_stop
- zs
->zs_thread_start
) / NANOSEC
+ GRACE
);
6394 * Verify that we can safely inquire about about any object,
6395 * whether it's allocated or not. To make it interesting,
6396 * we probe a 5-wide window around each power of two.
6397 * This hits all edge cases, including zero and the max.
6399 for (t
= 0; t
< 64; t
++) {
6400 for (d
= -5; d
<= 5; d
++) {
6401 error
= dmu_object_info(spa
->spa_meta_objset
,
6402 (1ULL << t
) + d
, NULL
);
6403 ASSERT(error
== 0 || error
== ENOENT
||
6409 * If we got any ENOSPC errors on the previous run, destroy something.
6411 if (zs
->zs_enospc_count
!= 0) {
6412 int d
= ztest_random(ztest_opts
.zo_datasets
);
6413 ztest_dataset_destroy(d
);
6415 zs
->zs_enospc_count
= 0;
6417 tid
= umem_zalloc(ztest_opts
.zo_threads
* sizeof (kt_did_t
),
6420 if (ztest_opts
.zo_verbose
>= 4)
6421 (void) printf("starting main threads...\n");
6424 * Kick off all the tests that run in parallel.
6426 for (t
= 0; t
< ztest_opts
.zo_threads
; t
++) {
6429 if (t
< ztest_opts
.zo_datasets
&&
6430 ztest_dataset_open(t
) != 0) {
6432 ztest_opts
.zo_threads
* sizeof (kt_did_t
));
6436 VERIFY3P(thread
= zk_thread_create(NULL
, 0,
6437 (thread_func_t
)ztest_thread
,
6438 (void *)(uintptr_t)t
, 0, NULL
, TS_RUN
, 0,
6439 PTHREAD_CREATE_JOINABLE
), !=, NULL
);
6440 tid
[t
] = thread
->t_tid
;
6444 * Wait for all of the tests to complete. We go in reverse order
6445 * so we don't close datasets while threads are still using them.
6447 for (t
= ztest_opts
.zo_threads
- 1; t
>= 0; t
--) {
6448 thread_join(tid
[t
]);
6449 if (t
< ztest_opts
.zo_datasets
)
6450 ztest_dataset_close(t
);
6453 txg_wait_synced(spa_get_dsl(spa
), 0);
6455 zs
->zs_alloc
= metaslab_class_get_alloc(spa_normal_class(spa
));
6456 zs
->zs_space
= metaslab_class_get_space(spa_normal_class(spa
));
6458 umem_free(tid
, ztest_opts
.zo_threads
* sizeof (kt_did_t
));
6460 /* Kill the resume thread */
6461 ztest_exiting
= B_TRUE
;
6462 thread_join(resume_thread
->t_tid
);
6466 * Right before closing the pool, kick off a bunch of async I/O;
6467 * spa_close() should wait for it to complete.
6469 for (object
= 1; object
< 50; object
++) {
6470 dmu_prefetch(spa
->spa_meta_objset
, object
, 0, 0, 1ULL << 20,
6471 ZIO_PRIORITY_SYNC_READ
);
6474 /* Verify that at least one commit cb was called in a timely fashion */
6475 if (zc_cb_counter
>= ZTEST_COMMIT_CB_MIN_REG
)
6476 VERIFY0(zc_min_txg_delay
);
6478 spa_close(spa
, FTAG
);
6481 * Verify that we can loop over all pools.
6483 mutex_enter(&spa_namespace_lock
);
6484 for (spa
= spa_next(NULL
); spa
!= NULL
; spa
= spa_next(spa
))
6485 if (ztest_opts
.zo_verbose
> 3)
6486 (void) printf("spa_next: found %s\n", spa_name(spa
));
6487 mutex_exit(&spa_namespace_lock
);
6490 * Verify that we can export the pool and reimport it under a
6493 if ((ztest_random(2) == 0) && !ztest_opts
.zo_mmp_test
) {
6494 char name
[ZFS_MAX_DATASET_NAME_LEN
];
6495 (void) snprintf(name
, sizeof (name
), "%s_import",
6496 ztest_opts
.zo_pool
);
6497 ztest_spa_import_export(ztest_opts
.zo_pool
, name
);
6498 ztest_spa_import_export(name
, ztest_opts
.zo_pool
);
6503 list_destroy(&zcl
.zcl_callbacks
);
6504 mutex_destroy(&zcl
.zcl_callbacks_lock
);
6505 (void) rwlock_destroy(&ztest_name_lock
);
6506 mutex_destroy(&ztest_vdev_lock
);
6512 ztest_ds_t
*zd
= &ztest_ds
[0];
6516 if (ztest_opts
.zo_verbose
>= 3)
6517 (void) printf("testing spa_freeze()...\n");
6519 kernel_init(FREAD
| FWRITE
);
6520 VERIFY3U(0, ==, spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
));
6521 VERIFY3U(0, ==, ztest_dataset_open(0));
6522 spa
->spa_debug
= B_TRUE
;
6526 * Force the first log block to be transactionally allocated.
6527 * We have to do this before we freeze the pool -- otherwise
6528 * the log chain won't be anchored.
6530 while (BP_IS_HOLE(&zd
->zd_zilog
->zl_header
->zh_log
)) {
6531 ztest_dmu_object_alloc_free(zd
, 0);
6532 zil_commit(zd
->zd_zilog
, 0);
6535 txg_wait_synced(spa_get_dsl(spa
), 0);
6538 * Freeze the pool. This stops spa_sync() from doing anything,
6539 * so that the only way to record changes from now on is the ZIL.
6544 * Because it is hard to predict how much space a write will actually
6545 * require beforehand, we leave ourselves some fudge space to write over
6548 uint64_t capacity
= metaslab_class_get_space(spa_normal_class(spa
)) / 2;
6551 * Run tests that generate log records but don't alter the pool config
6552 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
6553 * We do a txg_wait_synced() after each iteration to force the txg
6554 * to increase well beyond the last synced value in the uberblock.
6555 * The ZIL should be OK with that.
6557 * Run a random number of times less than zo_maxloops and ensure we do
6558 * not run out of space on the pool.
6560 while (ztest_random(10) != 0 &&
6561 numloops
++ < ztest_opts
.zo_maxloops
&&
6562 metaslab_class_get_alloc(spa_normal_class(spa
)) < capacity
) {
6564 ztest_od_init(&od
, 0, FTAG
, 0, DMU_OT_UINT64_OTHER
, 0, 0, 0);
6565 VERIFY0(ztest_object_init(zd
, &od
, sizeof (od
), B_FALSE
));
6566 ztest_io(zd
, od
.od_object
,
6567 ztest_random(ZTEST_RANGE_LOCKS
) << SPA_MAXBLOCKSHIFT
);
6568 txg_wait_synced(spa_get_dsl(spa
), 0);
6572 * Commit all of the changes we just generated.
6574 zil_commit(zd
->zd_zilog
, 0);
6575 txg_wait_synced(spa_get_dsl(spa
), 0);
6578 * Close our dataset and close the pool.
6580 ztest_dataset_close(0);
6581 spa_close(spa
, FTAG
);
6585 * Open and close the pool and dataset to induce log replay.
6587 kernel_init(FREAD
| FWRITE
);
6588 VERIFY3U(0, ==, spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
));
6589 ASSERT(spa_freeze_txg(spa
) == UINT64_MAX
);
6590 VERIFY3U(0, ==, ztest_dataset_open(0));
6591 ztest_dataset_close(0);
6593 spa
->spa_debug
= B_TRUE
;
6595 txg_wait_synced(spa_get_dsl(spa
), 0);
6596 ztest_reguid(NULL
, 0);
6598 spa_close(spa
, FTAG
);
6603 print_time(hrtime_t t
, char *timebuf
)
6605 hrtime_t s
= t
/ NANOSEC
;
6606 hrtime_t m
= s
/ 60;
6607 hrtime_t h
= m
/ 60;
6608 hrtime_t d
= h
/ 24;
6617 (void) sprintf(timebuf
,
6618 "%llud%02lluh%02llum%02llus", d
, h
, m
, s
);
6620 (void) sprintf(timebuf
, "%lluh%02llum%02llus", h
, m
, s
);
6622 (void) sprintf(timebuf
, "%llum%02llus", m
, s
);
6624 (void) sprintf(timebuf
, "%llus", s
);
6628 make_random_props(void)
6632 VERIFY(nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) == 0);
6633 if (ztest_random(2) == 0)
6635 VERIFY(nvlist_add_uint64(props
, "autoreplace", 1) == 0);
6641 * Import a storage pool with the given name.
6644 ztest_import(ztest_shared_t
*zs
)
6646 libzfs_handle_t
*hdl
;
6647 importargs_t args
= { 0 };
6649 nvlist_t
*cfg
= NULL
;
6651 char *searchdirs
[nsearch
];
6652 char *name
= ztest_opts
.zo_pool
;
6653 int flags
= ZFS_IMPORT_MISSING_LOG
;
6656 mutex_init(&ztest_vdev_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
6657 VERIFY(rwlock_init(&ztest_name_lock
, USYNC_THREAD
, NULL
) == 0);
6659 kernel_init(FREAD
| FWRITE
);
6660 hdl
= libzfs_init();
6662 searchdirs
[0] = ztest_opts
.zo_dir
;
6663 args
.paths
= nsearch
;
6664 args
.path
= searchdirs
;
6665 args
.can_be_active
= B_FALSE
;
6667 error
= zpool_tryimport(hdl
, name
, &cfg
, &args
);
6669 (void) fatal(0, "No pools found\n");
6671 VERIFY0(spa_import(name
, cfg
, NULL
, flags
));
6672 VERIFY0(spa_open(name
, &spa
, FTAG
));
6673 zs
->zs_metaslab_sz
=
6674 1ULL << spa
->spa_root_vdev
->vdev_child
[0]->vdev_ms_shift
;
6675 spa_close(spa
, FTAG
);
6680 if (!ztest_opts
.zo_mmp_test
) {
6681 ztest_run_zdb(ztest_opts
.zo_pool
);
6683 ztest_run_zdb(ztest_opts
.zo_pool
);
6686 (void) rwlock_destroy(&ztest_name_lock
);
6687 mutex_destroy(&ztest_vdev_lock
);
6691 * Create a storage pool with the given name and initial vdev size.
6692 * Then test spa_freeze() functionality.
6695 ztest_init(ztest_shared_t
*zs
)
6698 nvlist_t
*nvroot
, *props
;
6701 mutex_init(&ztest_vdev_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
6702 VERIFY(rwlock_init(&ztest_name_lock
, USYNC_THREAD
, NULL
) == 0);
6704 kernel_init(FREAD
| FWRITE
);
6707 * Create the storage pool.
6709 (void) spa_destroy(ztest_opts
.zo_pool
);
6710 ztest_shared
->zs_vdev_next_leaf
= 0;
6712 zs
->zs_mirrors
= ztest_opts
.zo_mirrors
;
6713 nvroot
= make_vdev_root(NULL
, NULL
, NULL
, ztest_opts
.zo_vdev_size
, 0,
6714 0, ztest_opts
.zo_raidz
, zs
->zs_mirrors
, 1);
6715 props
= make_random_props();
6716 for (i
= 0; i
< SPA_FEATURES
; i
++) {
6718 VERIFY3S(-1, !=, asprintf(&buf
, "feature@%s",
6719 spa_feature_table
[i
].fi_uname
));
6720 VERIFY3U(0, ==, nvlist_add_uint64(props
, buf
, 0));
6723 VERIFY3U(0, ==, spa_create(ztest_opts
.zo_pool
, nvroot
, props
, NULL
));
6724 nvlist_free(nvroot
);
6727 VERIFY3U(0, ==, spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
));
6728 zs
->zs_metaslab_sz
=
6729 1ULL << spa
->spa_root_vdev
->vdev_child
[0]->vdev_ms_shift
;
6730 spa_close(spa
, FTAG
);
6734 if (!ztest_opts
.zo_mmp_test
) {
6735 ztest_run_zdb(ztest_opts
.zo_pool
);
6737 ztest_run_zdb(ztest_opts
.zo_pool
);
6740 (void) rwlock_destroy(&ztest_name_lock
);
6741 mutex_destroy(&ztest_vdev_lock
);
6747 static char ztest_name_data
[] = "/tmp/ztest.data.XXXXXX";
6749 ztest_fd_data
= mkstemp(ztest_name_data
);
6750 ASSERT3S(ztest_fd_data
, >=, 0);
6751 (void) unlink(ztest_name_data
);
6755 shared_data_size(ztest_shared_hdr_t
*hdr
)
6759 size
= hdr
->zh_hdr_size
;
6760 size
+= hdr
->zh_opts_size
;
6761 size
+= hdr
->zh_size
;
6762 size
+= hdr
->zh_stats_size
* hdr
->zh_stats_count
;
6763 size
+= hdr
->zh_ds_size
* hdr
->zh_ds_count
;
6772 ztest_shared_hdr_t
*hdr
;
6774 hdr
= (void *)mmap(0, P2ROUNDUP(sizeof (*hdr
), getpagesize()),
6775 PROT_READ
| PROT_WRITE
, MAP_SHARED
, ztest_fd_data
, 0);
6776 ASSERT(hdr
!= MAP_FAILED
);
6778 VERIFY3U(0, ==, ftruncate(ztest_fd_data
, sizeof (ztest_shared_hdr_t
)));
6780 hdr
->zh_hdr_size
= sizeof (ztest_shared_hdr_t
);
6781 hdr
->zh_opts_size
= sizeof (ztest_shared_opts_t
);
6782 hdr
->zh_size
= sizeof (ztest_shared_t
);
6783 hdr
->zh_stats_size
= sizeof (ztest_shared_callstate_t
);
6784 hdr
->zh_stats_count
= ZTEST_FUNCS
;
6785 hdr
->zh_ds_size
= sizeof (ztest_shared_ds_t
);
6786 hdr
->zh_ds_count
= ztest_opts
.zo_datasets
;
6788 size
= shared_data_size(hdr
);
6789 VERIFY3U(0, ==, ftruncate(ztest_fd_data
, size
));
6791 (void) munmap((caddr_t
)hdr
, P2ROUNDUP(sizeof (*hdr
), getpagesize()));
6798 ztest_shared_hdr_t
*hdr
;
6801 hdr
= (void *)mmap(0, P2ROUNDUP(sizeof (*hdr
), getpagesize()),
6802 PROT_READ
, MAP_SHARED
, ztest_fd_data
, 0);
6803 ASSERT(hdr
!= MAP_FAILED
);
6805 size
= shared_data_size(hdr
);
6807 (void) munmap((caddr_t
)hdr
, P2ROUNDUP(sizeof (*hdr
), getpagesize()));
6808 hdr
= ztest_shared_hdr
= (void *)mmap(0, P2ROUNDUP(size
, getpagesize()),
6809 PROT_READ
| PROT_WRITE
, MAP_SHARED
, ztest_fd_data
, 0);
6810 ASSERT(hdr
!= MAP_FAILED
);
6811 buf
= (uint8_t *)hdr
;
6813 offset
= hdr
->zh_hdr_size
;
6814 ztest_shared_opts
= (void *)&buf
[offset
];
6815 offset
+= hdr
->zh_opts_size
;
6816 ztest_shared
= (void *)&buf
[offset
];
6817 offset
+= hdr
->zh_size
;
6818 ztest_shared_callstate
= (void *)&buf
[offset
];
6819 offset
+= hdr
->zh_stats_size
* hdr
->zh_stats_count
;
6820 ztest_shared_ds
= (void *)&buf
[offset
];
6824 exec_child(char *cmd
, char *libpath
, boolean_t ignorekill
, int *statusp
)
6828 char *cmdbuf
= NULL
;
6833 cmdbuf
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
6834 (void) strlcpy(cmdbuf
, getexecname(), MAXPATHLEN
);
6839 fatal(1, "fork failed");
6841 if (pid
== 0) { /* child */
6842 char *emptyargv
[2] = { cmd
, NULL
};
6843 char fd_data_str
[12];
6845 struct rlimit rl
= { 1024, 1024 };
6846 (void) setrlimit(RLIMIT_NOFILE
, &rl
);
6848 (void) close(ztest_fd_rand
);
6849 VERIFY(11 >= snprintf(fd_data_str
, 12, "%d", ztest_fd_data
));
6850 VERIFY(0 == setenv("ZTEST_FD_DATA", fd_data_str
, 1));
6852 (void) enable_extended_FILE_stdio(-1, -1);
6853 if (libpath
!= NULL
)
6854 VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath
, 1));
6855 (void) execv(cmd
, emptyargv
);
6856 ztest_dump_core
= B_FALSE
;
6857 fatal(B_TRUE
, "exec failed: %s", cmd
);
6860 if (cmdbuf
!= NULL
) {
6861 umem_free(cmdbuf
, MAXPATHLEN
);
6865 while (waitpid(pid
, &status
, 0) != pid
)
6867 if (statusp
!= NULL
)
6870 if (WIFEXITED(status
)) {
6871 if (WEXITSTATUS(status
) != 0) {
6872 (void) fprintf(stderr
, "child exited with code %d\n",
6873 WEXITSTATUS(status
));
6877 } else if (WIFSIGNALED(status
)) {
6878 if (!ignorekill
|| WTERMSIG(status
) != SIGKILL
) {
6879 (void) fprintf(stderr
, "child died with signal %d\n",
6885 (void) fprintf(stderr
, "something strange happened to child\n");
6892 ztest_run_init(void)
6896 ztest_shared_t
*zs
= ztest_shared
;
6899 * Blow away any existing copy of zpool.cache
6901 (void) remove(spa_config_path
);
6903 if (ztest_opts
.zo_init
== 0) {
6904 if (ztest_opts
.zo_verbose
>= 1)
6905 (void) printf("Importing pool %s\n",
6906 ztest_opts
.zo_pool
);
6912 * Create and initialize our storage pool.
6914 for (i
= 1; i
<= ztest_opts
.zo_init
; i
++) {
6915 bzero(zs
, sizeof (ztest_shared_t
));
6916 if (ztest_opts
.zo_verbose
>= 3 &&
6917 ztest_opts
.zo_init
!= 1) {
6918 (void) printf("ztest_init(), pass %d\n", i
);
6925 main(int argc
, char **argv
)
6933 ztest_shared_callstate_t
*zc
;
6940 char *fd_data_str
= getenv("ZTEST_FD_DATA");
6941 struct sigaction action
;
6943 (void) setvbuf(stdout
, NULL
, _IOLBF
, 0);
6945 dprintf_setup(&argc
, argv
);
6947 action
.sa_handler
= sig_handler
;
6948 sigemptyset(&action
.sa_mask
);
6949 action
.sa_flags
= 0;
6951 if (sigaction(SIGSEGV
, &action
, NULL
) < 0) {
6952 (void) fprintf(stderr
, "ztest: cannot catch SIGSEGV: %s.\n",
6957 if (sigaction(SIGABRT
, &action
, NULL
) < 0) {
6958 (void) fprintf(stderr
, "ztest: cannot catch SIGABRT: %s.\n",
6963 ztest_fd_rand
= open("/dev/urandom", O_RDONLY
);
6964 ASSERT3S(ztest_fd_rand
, >=, 0);
6967 process_options(argc
, argv
);
6972 bcopy(&ztest_opts
, ztest_shared_opts
,
6973 sizeof (*ztest_shared_opts
));
6975 ztest_fd_data
= atoi(fd_data_str
);
6977 bcopy(ztest_shared_opts
, &ztest_opts
, sizeof (ztest_opts
));
6979 ASSERT3U(ztest_opts
.zo_datasets
, ==, ztest_shared_hdr
->zh_ds_count
);
6981 /* Override location of zpool.cache */
6982 VERIFY(asprintf((char **)&spa_config_path
, "%s/zpool.cache",
6983 ztest_opts
.zo_dir
) != -1);
6985 ztest_ds
= umem_alloc(ztest_opts
.zo_datasets
* sizeof (ztest_ds_t
),
6990 metaslab_gang_bang
= ztest_opts
.zo_metaslab_gang_bang
;
6991 metaslab_df_alloc_threshold
=
6992 zs
->zs_metaslab_df_alloc_threshold
;
7001 hasalt
= (strlen(ztest_opts
.zo_alt_ztest
) != 0);
7003 if (ztest_opts
.zo_verbose
>= 1) {
7004 (void) printf("%llu vdevs, %d datasets, %d threads,"
7005 " %llu seconds...\n",
7006 (u_longlong_t
)ztest_opts
.zo_vdevs
,
7007 ztest_opts
.zo_datasets
,
7008 ztest_opts
.zo_threads
,
7009 (u_longlong_t
)ztest_opts
.zo_time
);
7012 cmd
= umem_alloc(MAXNAMELEN
, UMEM_NOFAIL
);
7013 (void) strlcpy(cmd
, getexecname(), MAXNAMELEN
);
7015 zs
->zs_do_init
= B_TRUE
;
7016 if (strlen(ztest_opts
.zo_alt_ztest
) != 0) {
7017 if (ztest_opts
.zo_verbose
>= 1) {
7018 (void) printf("Executing older ztest for "
7019 "initialization: %s\n", ztest_opts
.zo_alt_ztest
);
7021 VERIFY(!exec_child(ztest_opts
.zo_alt_ztest
,
7022 ztest_opts
.zo_alt_libpath
, B_FALSE
, NULL
));
7024 VERIFY(!exec_child(NULL
, NULL
, B_FALSE
, NULL
));
7026 zs
->zs_do_init
= B_FALSE
;
7028 zs
->zs_proc_start
= gethrtime();
7029 zs
->zs_proc_stop
= zs
->zs_proc_start
+ ztest_opts
.zo_time
* NANOSEC
;
7031 for (f
= 0; f
< ZTEST_FUNCS
; f
++) {
7032 zi
= &ztest_info
[f
];
7033 zc
= ZTEST_GET_SHARED_CALLSTATE(f
);
7034 if (zs
->zs_proc_start
+ zi
->zi_interval
[0] > zs
->zs_proc_stop
)
7035 zc
->zc_next
= UINT64_MAX
;
7037 zc
->zc_next
= zs
->zs_proc_start
+
7038 ztest_random(2 * zi
->zi_interval
[0] + 1);
7042 * Run the tests in a loop. These tests include fault injection
7043 * to verify that self-healing data works, and forced crashes
7044 * to verify that we never lose on-disk consistency.
7046 while (gethrtime() < zs
->zs_proc_stop
) {
7051 * Initialize the workload counters for each function.
7053 for (f
= 0; f
< ZTEST_FUNCS
; f
++) {
7054 zc
= ZTEST_GET_SHARED_CALLSTATE(f
);
7059 /* Set the allocation switch size */
7060 zs
->zs_metaslab_df_alloc_threshold
=
7061 ztest_random(zs
->zs_metaslab_sz
/ 4) + 1;
7063 if (!hasalt
|| ztest_random(2) == 0) {
7064 if (hasalt
&& ztest_opts
.zo_verbose
>= 1) {
7065 (void) printf("Executing newer ztest: %s\n",
7069 killed
= exec_child(cmd
, NULL
, B_TRUE
, &status
);
7071 if (hasalt
&& ztest_opts
.zo_verbose
>= 1) {
7072 (void) printf("Executing older ztest: %s\n",
7073 ztest_opts
.zo_alt_ztest
);
7076 killed
= exec_child(ztest_opts
.zo_alt_ztest
,
7077 ztest_opts
.zo_alt_libpath
, B_TRUE
, &status
);
7084 if (ztest_opts
.zo_verbose
>= 1) {
7085 hrtime_t now
= gethrtime();
7087 now
= MIN(now
, zs
->zs_proc_stop
);
7088 print_time(zs
->zs_proc_stop
- now
, timebuf
);
7089 nicenum(zs
->zs_space
, numbuf
);
7091 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
7092 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
7094 WIFEXITED(status
) ? "Complete" : "SIGKILL",
7095 (u_longlong_t
)zs
->zs_enospc_count
,
7096 100.0 * zs
->zs_alloc
/ zs
->zs_space
,
7098 100.0 * (now
- zs
->zs_proc_start
) /
7099 (ztest_opts
.zo_time
* NANOSEC
), timebuf
);
7102 if (ztest_opts
.zo_verbose
>= 2) {
7103 (void) printf("\nWorkload summary:\n\n");
7104 (void) printf("%7s %9s %s\n",
7105 "Calls", "Time", "Function");
7106 (void) printf("%7s %9s %s\n",
7107 "-----", "----", "--------");
7108 for (f
= 0; f
< ZTEST_FUNCS
; f
++) {
7109 zi
= &ztest_info
[f
];
7110 zc
= ZTEST_GET_SHARED_CALLSTATE(f
);
7111 print_time(zc
->zc_time
, timebuf
);
7112 (void) printf("%7llu %9s %s\n",
7113 (u_longlong_t
)zc
->zc_count
, timebuf
,
7116 (void) printf("\n");
7120 * It's possible that we killed a child during a rename test,
7121 * in which case we'll have a 'ztest_tmp' pool lying around
7122 * instead of 'ztest'. Do a blind rename in case this happened.
7125 if (spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
) == 0) {
7126 spa_close(spa
, FTAG
);
7128 char tmpname
[ZFS_MAX_DATASET_NAME_LEN
];
7130 kernel_init(FREAD
| FWRITE
);
7131 (void) snprintf(tmpname
, sizeof (tmpname
), "%s_tmp",
7132 ztest_opts
.zo_pool
);
7133 (void) spa_rename(tmpname
, ztest_opts
.zo_pool
);
7137 if (!ztest_opts
.zo_mmp_test
)
7138 ztest_run_zdb(ztest_opts
.zo_pool
);
7141 if (ztest_opts
.zo_verbose
>= 1) {
7143 (void) printf("%d runs of older ztest: %s\n", older
,
7144 ztest_opts
.zo_alt_ztest
);
7145 (void) printf("%d runs of newer ztest: %s\n", newer
,
7148 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
7149 kills
, iters
- kills
, (100.0 * kills
) / MAX(1, iters
));
7152 umem_free(cmd
, MAXNAMELEN
);