]> git.proxmox.com Git - mirror_zfs.git/blame - cmd/ztest/ztest.c
Implement zfs_ioc_recv_new() for OpenZFS 2605
[mirror_zfs.git] / cmd / ztest / ztest.c
CommitLineData
34dc7c2f
BB
1/*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21/*
428870ff 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
c3520e7f 23 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
3541dc6d 24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
95fd54a1 25 * Copyright (c) 2013 Steven Hartland. All rights reserved.
34dc7c2f
BB
26 */
27
34dc7c2f
BB
28/*
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.
31 *
32 * The overall design of the ztest program is as follows:
33 *
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".
38 *
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.
42 *
43 * (3) While all this is happening, we inject faults into the pool to
44 * verify that self-healing data really works.
45 *
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.
49 *
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
9b67f605 55 * storage pool, as many times as desired. If backwards compatibility
c242c188
CS
56 * testing is enabled ztest will sometimes run the "older" version
57 * of ztest after a SIGKILL.
34dc7c2f
BB
58 *
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.
64 *
1e33ac1e
BB
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.
67 *
34dc7c2f
BB
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.
71 *
72 * To turn this into an overnight stress test, use -T to specify run time.
73 *
74 * You can ask more more vdevs [-v], datasets [-d], or threads [-t]
75 * to increase the pool capacity, fanout, and overall stress level.
76 *
c242c188
CS
77 * Use the -k option to set the desired frequency of kills.
78 *
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).
34dc7c2f
BB
86 */
87
88#include <sys/zfs_context.h>
89#include <sys/spa.h>
90#include <sys/dmu.h>
91#include <sys/txg.h>
9babb374 92#include <sys/dbuf.h>
34dc7c2f 93#include <sys/zap.h>
34dc7c2f
BB
94#include <sys/dmu_objset.h>
95#include <sys/poll.h>
96#include <sys/stat.h>
97#include <sys/time.h>
98#include <sys/wait.h>
99#include <sys/mman.h>
100#include <sys/resource.h>
101#include <sys/zio.h>
34dc7c2f 102#include <sys/zil.h>
428870ff 103#include <sys/zil_impl.h>
e3a07cd0 104#include <sys/zfs_rlock.h>
34dc7c2f 105#include <sys/vdev_impl.h>
b128c09f 106#include <sys/vdev_file.h>
34dc7c2f 107#include <sys/spa_impl.h>
428870ff 108#include <sys/metaslab_impl.h>
34dc7c2f 109#include <sys/dsl_prop.h>
9babb374 110#include <sys/dsl_dataset.h>
13fe0198 111#include <sys/dsl_destroy.h>
428870ff
BB
112#include <sys/dsl_scan.h>
113#include <sys/zio_checksum.h>
34dc7c2f 114#include <sys/refcount.h>
9ae529ec 115#include <sys/zfeature.h>
13fe0198 116#include <sys/dsl_userhold.h>
34dc7c2f
BB
117#include <stdio.h>
118#include <stdio_ext.h>
119#include <stdlib.h>
120#include <unistd.h>
121#include <signal.h>
122#include <umem.h>
34dc7c2f
BB
123#include <ctype.h>
124#include <math.h>
125#include <sys/fs/zfs.h>
1eeb4562 126#include <zfs_fletcher.h>
428870ff 127#include <libnvpair.h>
967798d0 128#ifdef __GLIBC__
e82cdc3a
NB
129#include <execinfo.h> /* for backtrace() */
130#endif
34dc7c2f 131
9d81146b
ED
132static int ztest_fd_data = -1;
133static int ztest_fd_rand = -1;
c242c188
CS
134
135typedef struct ztest_shared_hdr {
136 uint64_t zh_hdr_size;
137 uint64_t zh_opts_size;
138 uint64_t zh_size;
139 uint64_t zh_stats_size;
140 uint64_t zh_stats_count;
141 uint64_t zh_ds_size;
142 uint64_t zh_ds_count;
143} ztest_shared_hdr_t;
144
145static ztest_shared_hdr_t *ztest_shared_hdr;
146
147typedef struct ztest_shared_opts {
148 char zo_pool[MAXNAMELEN];
149 char zo_dir[MAXNAMELEN];
150 char zo_alt_ztest[MAXNAMELEN];
151 char zo_alt_libpath[MAXNAMELEN];
152 uint64_t zo_vdevs;
153 uint64_t zo_vdevtime;
154 size_t zo_vdev_size;
155 int zo_ashift;
156 int zo_mirrors;
157 int zo_raidz;
158 int zo_raidz_parity;
159 int zo_datasets;
160 int zo_threads;
161 uint64_t zo_passtime;
162 uint64_t zo_killrate;
163 int zo_verbose;
164 int zo_init;
165 uint64_t zo_time;
166 uint64_t zo_maxloops;
167 uint64_t zo_metaslab_gang_bang;
168} ztest_shared_opts_t;
169
170static const ztest_shared_opts_t ztest_opts_defaults = {
171 .zo_pool = { 'z', 't', 'e', 's', 't', '\0' },
172 .zo_dir = { '/', 't', 'm', 'p', '\0' },
173 .zo_alt_ztest = { '\0' },
174 .zo_alt_libpath = { '\0' },
175 .zo_vdevs = 5,
176 .zo_ashift = SPA_MINBLOCKSHIFT,
177 .zo_mirrors = 2,
178 .zo_raidz = 4,
179 .zo_raidz_parity = 1,
4d6369c1 180 .zo_vdev_size = SPA_MINDEVSIZE * 2,
c242c188
CS
181 .zo_datasets = 7,
182 .zo_threads = 23,
183 .zo_passtime = 60, /* 60 seconds */
184 .zo_killrate = 70, /* 70% kill rate */
185 .zo_verbose = 0,
186 .zo_init = 1,
187 .zo_time = 300, /* 5 minutes */
188 .zo_maxloops = 50, /* max loops during spa_freeze() */
189 .zo_metaslab_gang_bang = 32 << 10
190};
191
192extern uint64_t metaslab_gang_bang;
193extern uint64_t metaslab_df_alloc_threshold;
080b3100 194extern int metaslab_preload_limit;
c242c188
CS
195
196static ztest_shared_opts_t *ztest_shared_opts;
197static ztest_shared_opts_t ztest_opts;
198
199typedef struct ztest_shared_ds {
200 uint64_t zd_seq;
201} ztest_shared_ds_t;
202
203static ztest_shared_ds_t *ztest_shared_ds;
204#define ZTEST_GET_SHARED_DS(d) (&ztest_shared_ds[d])
428870ff
BB
205
206#define BT_MAGIC 0x123456789abcdefULL
c242c188
CS
207#define MAXFAULTS() \
208 (MAX(zs->zs_mirrors, 1) * (ztest_opts.zo_raidz_parity + 1) - 1)
428870ff
BB
209
210enum ztest_io_type {
211 ZTEST_IO_WRITE_TAG,
212 ZTEST_IO_WRITE_PATTERN,
213 ZTEST_IO_WRITE_ZEROES,
214 ZTEST_IO_TRUNCATE,
215 ZTEST_IO_SETATTR,
03c6040b 216 ZTEST_IO_REWRITE,
428870ff
BB
217 ZTEST_IO_TYPES
218};
34dc7c2f
BB
219
220typedef struct ztest_block_tag {
428870ff 221 uint64_t bt_magic;
34dc7c2f
BB
222 uint64_t bt_objset;
223 uint64_t bt_object;
50c957f7 224 uint64_t bt_dnodesize;
34dc7c2f 225 uint64_t bt_offset;
428870ff 226 uint64_t bt_gen;
34dc7c2f 227 uint64_t bt_txg;
428870ff 228 uint64_t bt_crtxg;
34dc7c2f
BB
229} ztest_block_tag_t;
230
428870ff
BB
231typedef struct bufwad {
232 uint64_t bw_index;
233 uint64_t bw_txg;
234 uint64_t bw_data;
235} bufwad_t;
236
428870ff
BB
237typedef struct rll {
238 void *rll_writer;
239 int rll_readers;
1e33ac1e
BB
240 kmutex_t rll_lock;
241 kcondvar_t rll_cv;
428870ff
BB
242} rll_t;
243
e3a07cd0
BP
244typedef struct zll {
245 list_t z_list;
246 kmutex_t z_lock;
247} zll_t;
428870ff
BB
248
249#define ZTEST_RANGE_LOCKS 64
250#define ZTEST_OBJECT_LOCKS 64
251
252/*
253 * Object descriptor. Used as a template for object lookup/create/remove.
254 */
255typedef struct ztest_od {
256 uint64_t od_dir;
257 uint64_t od_object;
258 dmu_object_type_t od_type;
259 dmu_object_type_t od_crtype;
260 uint64_t od_blocksize;
261 uint64_t od_crblocksize;
50c957f7 262 uint64_t od_crdnodesize;
428870ff
BB
263 uint64_t od_gen;
264 uint64_t od_crgen;
265 char od_name[MAXNAMELEN];
266} ztest_od_t;
34dc7c2f 267
428870ff
BB
268/*
269 * Per-dataset state.
270 */
271typedef struct ztest_ds {
c242c188 272 ztest_shared_ds_t *zd_shared;
428870ff 273 objset_t *zd_os;
7809eb8b 274 rwlock_t zd_zilog_lock;
428870ff 275 zilog_t *zd_zilog;
428870ff
BB
276 ztest_od_t *zd_od; /* debugging aid */
277 char zd_name[MAXNAMELEN];
1e33ac1e 278 kmutex_t zd_dirobj_lock;
428870ff 279 rll_t zd_object_lock[ZTEST_OBJECT_LOCKS];
e3a07cd0 280 zll_t zd_range_lock[ZTEST_RANGE_LOCKS];
428870ff
BB
281} ztest_ds_t;
282
283/*
284 * Per-iteration state.
285 */
286typedef void ztest_func_t(ztest_ds_t *zd, uint64_t id);
287
288typedef struct ztest_info {
289 ztest_func_t *zi_func; /* test function */
290 uint64_t zi_iters; /* iterations per execution */
291 uint64_t *zi_interval; /* execute every <interval> seconds */
fdc5d982 292 const char *zi_funcname; /* name of test function */
428870ff 293} ztest_info_t;
34dc7c2f 294
c242c188
CS
295typedef struct ztest_shared_callstate {
296 uint64_t zc_count; /* per-pass count */
297 uint64_t zc_time; /* per-pass time */
298 uint64_t zc_next; /* next time to call this function */
299} ztest_shared_callstate_t;
300
301static ztest_shared_callstate_t *ztest_shared_callstate;
302#define ZTEST_GET_SHARED_CALLSTATE(c) (&ztest_shared_callstate[c])
303
34dc7c2f
BB
304ztest_func_t ztest_dmu_read_write;
305ztest_func_t ztest_dmu_write_parallel;
306ztest_func_t ztest_dmu_object_alloc_free;
428870ff 307ztest_func_t ztest_dmu_commit_callbacks;
34dc7c2f
BB
308ztest_func_t ztest_zap;
309ztest_func_t ztest_zap_parallel;
428870ff 310ztest_func_t ztest_zil_commit;
3e31d2b0 311ztest_func_t ztest_zil_remount;
428870ff 312ztest_func_t ztest_dmu_read_write_zcopy;
34dc7c2f 313ztest_func_t ztest_dmu_objset_create_destroy;
428870ff
BB
314ztest_func_t ztest_dmu_prealloc;
315ztest_func_t ztest_fzap;
34dc7c2f 316ztest_func_t ztest_dmu_snapshot_create_destroy;
428870ff
BB
317ztest_func_t ztest_dsl_prop_get_set;
318ztest_func_t ztest_spa_prop_get_set;
34dc7c2f
BB
319ztest_func_t ztest_spa_create_destroy;
320ztest_func_t ztest_fault_inject;
428870ff
BB
321ztest_func_t ztest_ddt_repair;
322ztest_func_t ztest_dmu_snapshot_hold;
b128c09f 323ztest_func_t ztest_spa_rename;
428870ff
BB
324ztest_func_t ztest_scrub;
325ztest_func_t ztest_dsl_dataset_promote_busy;
34dc7c2f
BB
326ztest_func_t ztest_vdev_attach_detach;
327ztest_func_t ztest_vdev_LUN_growth;
328ztest_func_t ztest_vdev_add_remove;
b128c09f 329ztest_func_t ztest_vdev_aux_add_remove;
428870ff 330ztest_func_t ztest_split_pool;
3541dc6d 331ztest_func_t ztest_reguid;
ea0b2538 332ztest_func_t ztest_spa_upgrade;
1eeb4562 333ztest_func_t ztest_fletcher;
50c957f7 334ztest_func_t ztest_verify_dnode_bt;
34dc7c2f 335
428870ff
BB
336uint64_t zopt_always = 0ULL * NANOSEC; /* all the time */
337uint64_t zopt_incessant = 1ULL * NANOSEC / 10; /* every 1/10 second */
338uint64_t zopt_often = 1ULL * NANOSEC; /* every second */
339uint64_t zopt_sometimes = 10ULL * NANOSEC; /* every 10 seconds */
340uint64_t zopt_rarely = 60ULL * NANOSEC; /* every 60 seconds */
34dc7c2f 341
fdc5d982
TC
342#define ZTI_INIT(func, iters, interval) \
343 { .zi_func = (func), \
344 .zi_iters = (iters), \
345 .zi_interval = (interval), \
346 .zi_funcname = # func }
347
34dc7c2f 348ztest_info_t ztest_info[] = {
fdc5d982
TC
349 ZTI_INIT(ztest_dmu_read_write, 1, &zopt_always),
350 ZTI_INIT(ztest_dmu_write_parallel, 10, &zopt_always),
351 ZTI_INIT(ztest_dmu_object_alloc_free, 1, &zopt_always),
352 ZTI_INIT(ztest_dmu_commit_callbacks, 1, &zopt_always),
353 ZTI_INIT(ztest_zap, 30, &zopt_always),
354 ZTI_INIT(ztest_zap_parallel, 100, &zopt_always),
355 ZTI_INIT(ztest_split_pool, 1, &zopt_always),
356 ZTI_INIT(ztest_zil_commit, 1, &zopt_incessant),
357 ZTI_INIT(ztest_zil_remount, 1, &zopt_sometimes),
358 ZTI_INIT(ztest_dmu_read_write_zcopy, 1, &zopt_often),
359 ZTI_INIT(ztest_dmu_objset_create_destroy, 1, &zopt_often),
360 ZTI_INIT(ztest_dsl_prop_get_set, 1, &zopt_often),
361 ZTI_INIT(ztest_spa_prop_get_set, 1, &zopt_sometimes),
428870ff 362#if 0
fdc5d982 363 ZTI_INIT(ztest_dmu_prealloc, 1, &zopt_sometimes),
428870ff 364#endif
fdc5d982
TC
365 ZTI_INIT(ztest_fzap, 1, &zopt_sometimes),
366 ZTI_INIT(ztest_dmu_snapshot_create_destroy, 1, &zopt_sometimes),
367 ZTI_INIT(ztest_spa_create_destroy, 1, &zopt_sometimes),
368 ZTI_INIT(ztest_fault_inject, 1, &zopt_sometimes),
369 ZTI_INIT(ztest_ddt_repair, 1, &zopt_sometimes),
370 ZTI_INIT(ztest_dmu_snapshot_hold, 1, &zopt_sometimes),
371 ZTI_INIT(ztest_reguid, 1, &zopt_rarely),
372 ZTI_INIT(ztest_spa_rename, 1, &zopt_rarely),
373 ZTI_INIT(ztest_scrub, 1, &zopt_rarely),
374 ZTI_INIT(ztest_spa_upgrade, 1, &zopt_rarely),
375 ZTI_INIT(ztest_dsl_dataset_promote_busy, 1, &zopt_rarely),
376 ZTI_INIT(ztest_vdev_attach_detach, 1, &zopt_sometimes),
377 ZTI_INIT(ztest_vdev_LUN_growth, 1, &zopt_rarely),
378 ZTI_INIT(ztest_vdev_add_remove, 1, &ztest_opts.zo_vdevtime),
379 ZTI_INIT(ztest_vdev_aux_add_remove, 1, &ztest_opts.zo_vdevtime),
1eeb4562 380 ZTI_INIT(ztest_fletcher, 1, &zopt_rarely),
50c957f7 381 ZTI_INIT(ztest_verify_dnode_bt, 1, &zopt_sometimes),
34dc7c2f
BB
382};
383
384#define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
385
428870ff
BB
386/*
387 * The following struct is used to hold a list of uncalled commit callbacks.
388 * The callbacks are ordered by txg number.
389 */
390typedef struct ztest_cb_list {
1e33ac1e
BB
391 kmutex_t zcl_callbacks_lock;
392 list_t zcl_callbacks;
428870ff 393} ztest_cb_list_t;
34dc7c2f
BB
394
395/*
396 * Stuff we need to share writably between parent and child.
397 */
398typedef struct ztest_shared {
c242c188 399 boolean_t zs_do_init;
428870ff
BB
400 hrtime_t zs_proc_start;
401 hrtime_t zs_proc_stop;
402 hrtime_t zs_thread_start;
403 hrtime_t zs_thread_stop;
404 hrtime_t zs_thread_kill;
34dc7c2f 405 uint64_t zs_enospc_count;
428870ff
BB
406 uint64_t zs_vdev_next_leaf;
407 uint64_t zs_vdev_aux;
34dc7c2f
BB
408 uint64_t zs_alloc;
409 uint64_t zs_space;
428870ff
BB
410 uint64_t zs_splits;
411 uint64_t zs_mirrors;
c242c188
CS
412 uint64_t zs_metaslab_sz;
413 uint64_t zs_metaslab_df_alloc_threshold;
414 uint64_t zs_guid;
34dc7c2f
BB
415} ztest_shared_t;
416
428870ff
BB
417#define ID_PARALLEL -1ULL
418
34dc7c2f 419static char ztest_dev_template[] = "%s/%s.%llua";
b128c09f 420static char ztest_aux_template[] = "%s/%s.%s.%llu";
428870ff 421ztest_shared_t *ztest_shared;
34dc7c2f 422
c242c188
CS
423static spa_t *ztest_spa = NULL;
424static ztest_ds_t *ztest_ds;
425
426static kmutex_t ztest_vdev_lock;
3bc7e0fb
GW
427
428/*
429 * The ztest_name_lock protects the pool and dataset namespace used by
430 * the individual tests. To modify the namespace, consumers must grab
431 * this lock as writer. Grabbing the lock as reader will ensure that the
432 * namespace does not change while the lock is held.
433 */
7809eb8b 434static rwlock_t ztest_name_lock;
34dc7c2f 435
c242c188 436static boolean_t ztest_dump_core = B_TRUE;
b128c09f 437static boolean_t ztest_exiting;
34dc7c2f 438
428870ff
BB
439/* Global commit callback list */
440static ztest_cb_list_t zcl;
090ff092
RC
441/* Commit cb delay */
442static uint64_t zc_min_txg_delay = UINT64_MAX;
443static int zc_cb_counter = 0;
444
445/*
446 * Minimum number of commit callbacks that need to be registered for us to check
447 * whether the minimum txg delay is acceptable.
448 */
449#define ZTEST_COMMIT_CB_MIN_REG 100
450
451/*
452 * If a number of txgs equal to this threshold have been created after a commit
453 * callback has been registered but not called, then we assume there is an
454 * implementation bug.
455 */
456#define ZTEST_COMMIT_CB_THRESH (TXG_CONCURRENT_STATES + 1000)
428870ff 457
34dc7c2f 458extern uint64_t metaslab_gang_bang;
9babb374 459extern uint64_t metaslab_df_alloc_threshold;
34dc7c2f 460
428870ff
BB
461enum ztest_object {
462 ZTEST_META_DNODE = 0,
463 ZTEST_DIROBJ,
464 ZTEST_OBJECTS
465};
34dc7c2f
BB
466
467static void usage(boolean_t) __NORETURN;
468
469/*
470 * These libumem hooks provide a reasonable set of defaults for the allocator's
471 * debugging facilities.
472 */
473const char *
0bc8fd78 474_umem_debug_init(void)
34dc7c2f
BB
475{
476 return ("default,verbose"); /* $UMEM_DEBUG setting */
477}
478
479const char *
480_umem_logging_init(void)
481{
482 return ("fail,contents"); /* $UMEM_LOGGING setting */
483}
484
e82cdc3a
NB
485#define BACKTRACE_SZ 100
486
487static void sig_handler(int signo)
488{
489 struct sigaction action;
967798d0 490#ifdef __GLIBC__ /* backtrace() is a GNU extension */
e82cdc3a
NB
491 int nptrs;
492 void *buffer[BACKTRACE_SZ];
493
494 nptrs = backtrace(buffer, BACKTRACE_SZ);
495 backtrace_symbols_fd(buffer, nptrs, STDERR_FILENO);
496#endif
497
498 /*
499 * Restore default action and re-raise signal so SIGSEGV and
500 * SIGABRT can trigger a core dump.
501 */
502 action.sa_handler = SIG_DFL;
503 sigemptyset(&action.sa_mask);
504 action.sa_flags = 0;
505 (void) sigaction(signo, &action, NULL);
506 raise(signo);
507}
508
34dc7c2f
BB
509#define FATAL_MSG_SZ 1024
510
511char *fatal_msg;
512
513static void
514fatal(int do_perror, char *message, ...)
515{
516 va_list args;
517 int save_errno = errno;
40b84e7a 518 char *buf;
34dc7c2f
BB
519
520 (void) fflush(stdout);
40b84e7a 521 buf = umem_alloc(FATAL_MSG_SZ, UMEM_NOFAIL);
34dc7c2f
BB
522
523 va_start(args, message);
524 (void) sprintf(buf, "ztest: ");
525 /* LINTED */
526 (void) vsprintf(buf + strlen(buf), message, args);
527 va_end(args);
528 if (do_perror) {
529 (void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
530 ": %s", strerror(save_errno));
531 }
532 (void) fprintf(stderr, "%s\n", buf);
533 fatal_msg = buf; /* to ease debugging */
534 if (ztest_dump_core)
535 abort();
536 exit(3);
537}
538
539static int
540str2shift(const char *buf)
541{
542 const char *ends = "BKMGTPEZ";
543 int i;
544
545 if (buf[0] == '\0')
546 return (0);
547 for (i = 0; i < strlen(ends); i++) {
548 if (toupper(buf[0]) == ends[i])
549 break;
550 }
551 if (i == strlen(ends)) {
552 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
553 buf);
554 usage(B_FALSE);
555 }
556 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
557 return (10*i);
558 }
559 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
560 usage(B_FALSE);
561 /* NOTREACHED */
562}
563
564static uint64_t
565nicenumtoull(const char *buf)
566{
567 char *end;
568 uint64_t val;
569
570 val = strtoull(buf, &end, 0);
571 if (end == buf) {
572 (void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
573 usage(B_FALSE);
574 } else if (end[0] == '.') {
575 double fval = strtod(buf, &end);
576 fval *= pow(2, str2shift(end));
577 if (fval > UINT64_MAX) {
578 (void) fprintf(stderr, "ztest: value too large: %s\n",
579 buf);
580 usage(B_FALSE);
581 }
582 val = (uint64_t)fval;
583 } else {
584 int shift = str2shift(end);
585 if (shift >= 64 || (val << shift) >> shift != val) {
586 (void) fprintf(stderr, "ztest: value too large: %s\n",
587 buf);
588 usage(B_FALSE);
589 }
590 val <<= shift;
591 }
592 return (val);
593}
594
595static void
596usage(boolean_t requested)
597{
c242c188
CS
598 const ztest_shared_opts_t *zo = &ztest_opts_defaults;
599
34dc7c2f
BB
600 char nice_vdev_size[10];
601 char nice_gang_bang[10];
602 FILE *fp = requested ? stdout : stderr;
603
c242c188
CS
604 nicenum(zo->zo_vdev_size, nice_vdev_size);
605 nicenum(zo->zo_metaslab_gang_bang, nice_gang_bang);
34dc7c2f
BB
606
607 (void) fprintf(fp, "Usage: %s\n"
608 "\t[-v vdevs (default: %llu)]\n"
609 "\t[-s size_of_each_vdev (default: %s)]\n"
428870ff 610 "\t[-a alignment_shift (default: %d)] use 0 for random\n"
34dc7c2f
BB
611 "\t[-m mirror_copies (default: %d)]\n"
612 "\t[-r raidz_disks (default: %d)]\n"
613 "\t[-R raidz_parity (default: %d)]\n"
614 "\t[-d datasets (default: %d)]\n"
615 "\t[-t threads (default: %d)]\n"
616 "\t[-g gang_block_threshold (default: %s)]\n"
428870ff
BB
617 "\t[-i init_count (default: %d)] initialize pool i times\n"
618 "\t[-k kill_percentage (default: %llu%%)]\n"
34dc7c2f 619 "\t[-p pool_name (default: %s)]\n"
428870ff
BB
620 "\t[-f dir (default: %s)] file directory for vdev files\n"
621 "\t[-V] verbose (use multiple times for ever more blather)\n"
622 "\t[-E] use existing pool instead of creating new one\n"
623 "\t[-T time (default: %llu sec)] total run time\n"
624 "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
625 "\t[-P passtime (default: %llu sec)] time per pass\n"
c242c188 626 "\t[-B alt_ztest (default: <none>)] alternate ztest path\n"
34dc7c2f
BB
627 "\t[-h] (print help)\n"
628 "",
c242c188
CS
629 zo->zo_pool,
630 (u_longlong_t)zo->zo_vdevs, /* -v */
34dc7c2f 631 nice_vdev_size, /* -s */
c242c188
CS
632 zo->zo_ashift, /* -a */
633 zo->zo_mirrors, /* -m */
634 zo->zo_raidz, /* -r */
635 zo->zo_raidz_parity, /* -R */
636 zo->zo_datasets, /* -d */
637 zo->zo_threads, /* -t */
34dc7c2f 638 nice_gang_bang, /* -g */
c242c188
CS
639 zo->zo_init, /* -i */
640 (u_longlong_t)zo->zo_killrate, /* -k */
641 zo->zo_pool, /* -p */
642 zo->zo_dir, /* -f */
643 (u_longlong_t)zo->zo_time, /* -T */
644 (u_longlong_t)zo->zo_maxloops, /* -F */
645 (u_longlong_t)zo->zo_passtime);
34dc7c2f
BB
646 exit(requested ? 0 : 1);
647}
648
34dc7c2f
BB
649static void
650process_options(int argc, char **argv)
651{
c242c188
CS
652 char *path;
653 ztest_shared_opts_t *zo = &ztest_opts;
654
34dc7c2f
BB
655 int opt;
656 uint64_t value;
c242c188 657 char altdir[MAXNAMELEN] = { 0 };
34dc7c2f 658
c242c188 659 bcopy(&ztest_opts_defaults, zo, sizeof (*zo));
34dc7c2f 660
34dc7c2f 661 while ((opt = getopt(argc, argv,
c242c188 662 "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:hF:B:")) != EOF) {
34dc7c2f
BB
663 value = 0;
664 switch (opt) {
665 case 'v':
666 case 's':
667 case 'a':
668 case 'm':
669 case 'r':
670 case 'R':
671 case 'd':
672 case 't':
673 case 'g':
674 case 'i':
675 case 'k':
676 case 'T':
677 case 'P':
428870ff 678 case 'F':
34dc7c2f
BB
679 value = nicenumtoull(optarg);
680 }
681 switch (opt) {
682 case 'v':
c242c188 683 zo->zo_vdevs = value;
34dc7c2f
BB
684 break;
685 case 's':
c242c188 686 zo->zo_vdev_size = MAX(SPA_MINDEVSIZE, value);
34dc7c2f
BB
687 break;
688 case 'a':
c242c188 689 zo->zo_ashift = value;
34dc7c2f
BB
690 break;
691 case 'm':
c242c188 692 zo->zo_mirrors = value;
34dc7c2f
BB
693 break;
694 case 'r':
c242c188 695 zo->zo_raidz = MAX(1, value);
34dc7c2f
BB
696 break;
697 case 'R':
c242c188 698 zo->zo_raidz_parity = MIN(MAX(value, 1), 3);
34dc7c2f
BB
699 break;
700 case 'd':
c242c188 701 zo->zo_datasets = MAX(1, value);
34dc7c2f
BB
702 break;
703 case 't':
c242c188 704 zo->zo_threads = MAX(1, value);
34dc7c2f
BB
705 break;
706 case 'g':
c242c188
CS
707 zo->zo_metaslab_gang_bang = MAX(SPA_MINBLOCKSIZE << 1,
708 value);
34dc7c2f
BB
709 break;
710 case 'i':
c242c188 711 zo->zo_init = value;
34dc7c2f
BB
712 break;
713 case 'k':
c242c188 714 zo->zo_killrate = value;
34dc7c2f
BB
715 break;
716 case 'p':
c242c188
CS
717 (void) strlcpy(zo->zo_pool, optarg,
718 sizeof (zo->zo_pool));
34dc7c2f
BB
719 break;
720 case 'f':
c242c188
CS
721 path = realpath(optarg, NULL);
722 if (path == NULL) {
723 (void) fprintf(stderr, "error: %s: %s\n",
724 optarg, strerror(errno));
725 usage(B_FALSE);
726 } else {
727 (void) strlcpy(zo->zo_dir, path,
728 sizeof (zo->zo_dir));
729 }
34dc7c2f
BB
730 break;
731 case 'V':
c242c188 732 zo->zo_verbose++;
34dc7c2f
BB
733 break;
734 case 'E':
c242c188 735 zo->zo_init = 0;
34dc7c2f
BB
736 break;
737 case 'T':
c242c188 738 zo->zo_time = value;
34dc7c2f
BB
739 break;
740 case 'P':
c242c188 741 zo->zo_passtime = MAX(1, value);
34dc7c2f 742 break;
428870ff 743 case 'F':
c242c188
CS
744 zo->zo_maxloops = MAX(1, value);
745 break;
746 case 'B':
747 (void) strlcpy(altdir, optarg, sizeof (altdir));
428870ff 748 break;
34dc7c2f
BB
749 case 'h':
750 usage(B_TRUE);
751 break;
752 case '?':
753 default:
754 usage(B_FALSE);
755 break;
756 }
757 }
758
c242c188 759 zo->zo_raidz_parity = MIN(zo->zo_raidz_parity, zo->zo_raidz - 1);
34dc7c2f 760
c242c188
CS
761 zo->zo_vdevtime =
762 (zo->zo_vdevs > 0 ? zo->zo_time * NANOSEC / zo->zo_vdevs :
428870ff 763 UINT64_MAX >> 2);
c242c188
CS
764
765 if (strlen(altdir) > 0) {
ae380cfa
BB
766 char *cmd;
767 char *realaltdir;
c242c188
CS
768 char *bin;
769 char *ztest;
770 char *isa;
771 int isalen;
772
ae380cfa
BB
773 cmd = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
774 realaltdir = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
775
5be98cfe 776 VERIFY(NULL != realpath(getexecname(), cmd));
c242c188
CS
777 if (0 != access(altdir, F_OK)) {
778 ztest_dump_core = B_FALSE;
779 fatal(B_TRUE, "invalid alternate ztest path: %s",
780 altdir);
781 }
782 VERIFY(NULL != realpath(altdir, realaltdir));
783
784 /*
785 * 'cmd' should be of the form "<anything>/usr/bin/<isa>/ztest".
786 * We want to extract <isa> to determine if we should use
787 * 32 or 64 bit binaries.
788 */
789 bin = strstr(cmd, "/usr/bin/");
790 ztest = strstr(bin, "/ztest");
791 isa = bin + 9;
792 isalen = ztest - isa;
793 (void) snprintf(zo->zo_alt_ztest, sizeof (zo->zo_alt_ztest),
794 "%s/usr/bin/%.*s/ztest", realaltdir, isalen, isa);
795 (void) snprintf(zo->zo_alt_libpath, sizeof (zo->zo_alt_libpath),
796 "%s/usr/lib/%.*s", realaltdir, isalen, isa);
797
798 if (0 != access(zo->zo_alt_ztest, X_OK)) {
799 ztest_dump_core = B_FALSE;
800 fatal(B_TRUE, "invalid alternate ztest: %s",
801 zo->zo_alt_ztest);
802 } else if (0 != access(zo->zo_alt_libpath, X_OK)) {
803 ztest_dump_core = B_FALSE;
804 fatal(B_TRUE, "invalid alternate lib directory %s",
805 zo->zo_alt_libpath);
806 }
ae380cfa
BB
807
808 umem_free(cmd, MAXPATHLEN);
809 umem_free(realaltdir, MAXPATHLEN);
c242c188 810 }
428870ff
BB
811}
812
813static void
814ztest_kill(ztest_shared_t *zs)
815{
c242c188
CS
816 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(ztest_spa));
817 zs->zs_space = metaslab_class_get_space(spa_normal_class(ztest_spa));
5d1f7fb6
GW
818
819 /*
820 * Before we kill off ztest, make sure that the config is updated.
821 * See comment above spa_config_sync().
822 */
823 mutex_enter(&spa_namespace_lock);
824 spa_config_sync(ztest_spa, B_FALSE, B_FALSE);
825 mutex_exit(&spa_namespace_lock);
826
428870ff
BB
827 (void) kill(getpid(), SIGKILL);
828}
829
830static uint64_t
831ztest_random(uint64_t range)
832{
833 uint64_t r;
834
9d81146b
ED
835 ASSERT3S(ztest_fd_rand, >=, 0);
836
428870ff
BB
837 if (range == 0)
838 return (0);
839
9d81146b 840 if (read(ztest_fd_rand, &r, sizeof (r)) != sizeof (r))
428870ff
BB
841 fatal(1, "short read from /dev/urandom");
842
843 return (r % range);
844}
845
846/* ARGSUSED */
847static void
848ztest_record_enospc(const char *s)
849{
850 ztest_shared->zs_enospc_count++;
34dc7c2f
BB
851}
852
853static uint64_t
854ztest_get_ashift(void)
855{
c242c188 856 if (ztest_opts.zo_ashift == 0)
b02fe35d 857 return (SPA_MINBLOCKSHIFT + ztest_random(5));
c242c188 858 return (ztest_opts.zo_ashift);
34dc7c2f
BB
859}
860
861static nvlist_t *
ea0b2538 862make_vdev_file(char *path, char *aux, char *pool, size_t size, uint64_t ashift)
34dc7c2f 863{
40b84e7a 864 char *pathbuf;
34dc7c2f 865 uint64_t vdev;
34dc7c2f
BB
866 nvlist_t *file;
867
40b84e7a
BB
868 pathbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
869
b128c09f
BB
870 if (ashift == 0)
871 ashift = ztest_get_ashift();
872
873 if (path == NULL) {
874 path = pathbuf;
875
876 if (aux != NULL) {
877 vdev = ztest_shared->zs_vdev_aux;
c242c188
CS
878 (void) snprintf(path, MAXPATHLEN,
879 ztest_aux_template, ztest_opts.zo_dir,
ea0b2538
GW
880 pool == NULL ? ztest_opts.zo_pool : pool,
881 aux, vdev);
b128c09f 882 } else {
428870ff 883 vdev = ztest_shared->zs_vdev_next_leaf++;
c242c188
CS
884 (void) snprintf(path, MAXPATHLEN,
885 ztest_dev_template, ztest_opts.zo_dir,
ea0b2538 886 pool == NULL ? ztest_opts.zo_pool : pool, vdev);
b128c09f
BB
887 }
888 }
34dc7c2f 889
b128c09f
BB
890 if (size != 0) {
891 int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
34dc7c2f 892 if (fd == -1)
b128c09f 893 fatal(1, "can't open %s", path);
34dc7c2f 894 if (ftruncate(fd, size) != 0)
b128c09f 895 fatal(1, "can't ftruncate %s", path);
34dc7c2f
BB
896 (void) close(fd);
897 }
898
899 VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
900 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
b128c09f 901 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
34dc7c2f 902 VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
40b84e7a 903 umem_free(pathbuf, MAXPATHLEN);
34dc7c2f
BB
904
905 return (file);
906}
907
908static nvlist_t *
ea0b2538
GW
909make_vdev_raidz(char *path, char *aux, char *pool, size_t size,
910 uint64_t ashift, int r)
34dc7c2f
BB
911{
912 nvlist_t *raidz, **child;
913 int c;
914
915 if (r < 2)
ea0b2538 916 return (make_vdev_file(path, aux, pool, size, ashift));
34dc7c2f
BB
917 child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
918
919 for (c = 0; c < r; c++)
ea0b2538 920 child[c] = make_vdev_file(path, aux, pool, size, ashift);
34dc7c2f
BB
921
922 VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
923 VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
924 VDEV_TYPE_RAIDZ) == 0);
925 VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
c242c188 926 ztest_opts.zo_raidz_parity) == 0);
34dc7c2f
BB
927 VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
928 child, r) == 0);
929
930 for (c = 0; c < r; c++)
931 nvlist_free(child[c]);
932
933 umem_free(child, r * sizeof (nvlist_t *));
934
935 return (raidz);
936}
937
938static nvlist_t *
ea0b2538
GW
939make_vdev_mirror(char *path, char *aux, char *pool, size_t size,
940 uint64_t ashift, int r, int m)
34dc7c2f
BB
941{
942 nvlist_t *mirror, **child;
943 int c;
944
945 if (m < 1)
ea0b2538 946 return (make_vdev_raidz(path, aux, pool, size, ashift, r));
34dc7c2f
BB
947
948 child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
949
950 for (c = 0; c < m; c++)
ea0b2538 951 child[c] = make_vdev_raidz(path, aux, pool, size, ashift, r);
34dc7c2f
BB
952
953 VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
954 VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
955 VDEV_TYPE_MIRROR) == 0);
956 VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
957 child, m) == 0);
34dc7c2f
BB
958
959 for (c = 0; c < m; c++)
960 nvlist_free(child[c]);
961
962 umem_free(child, m * sizeof (nvlist_t *));
963
964 return (mirror);
965}
966
967static nvlist_t *
ea0b2538
GW
968make_vdev_root(char *path, char *aux, char *pool, size_t size, uint64_t ashift,
969 int log, int r, int m, int t)
34dc7c2f
BB
970{
971 nvlist_t *root, **child;
972 int c;
973
974 ASSERT(t > 0);
975
976 child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
977
b128c09f 978 for (c = 0; c < t; c++) {
ea0b2538
GW
979 child[c] = make_vdev_mirror(path, aux, pool, size, ashift,
980 r, m);
b128c09f
BB
981 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
982 log) == 0);
983 }
34dc7c2f
BB
984
985 VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
986 VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
b128c09f 987 VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
34dc7c2f
BB
988 child, t) == 0);
989
990 for (c = 0; c < t; c++)
991 nvlist_free(child[c]);
992
993 umem_free(child, t * sizeof (nvlist_t *));
994
995 return (root);
996}
997
ea0b2538
GW
998/*
999 * Find a random spa version. Returns back a random spa version in the
1000 * range [initial_version, SPA_VERSION_FEATURES].
1001 */
1002static uint64_t
1003ztest_random_spa_version(uint64_t initial_version)
1004{
1005 uint64_t version = initial_version;
1006
1007 if (version <= SPA_VERSION_BEFORE_FEATURES) {
1008 version = version +
1009 ztest_random(SPA_VERSION_BEFORE_FEATURES - version + 1);
1010 }
1011
1012 if (version > SPA_VERSION_BEFORE_FEATURES)
1013 version = SPA_VERSION_FEATURES;
1014
1015 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
1016 return (version);
1017}
1018
428870ff
BB
1019static int
1020ztest_random_blocksize(void)
34dc7c2f 1021{
f1512ee6
MA
1022 /*
1023 * Choose a block size >= the ashift.
1024 * If the SPA supports new MAXBLOCKSIZE, test up to 1MB blocks.
1025 */
1026 int maxbs = SPA_OLD_MAXBLOCKSHIFT;
1027 if (spa_maxblocksize(ztest_spa) == SPA_MAXBLOCKSIZE)
1028 maxbs = 20;
c3520e7f
MA
1029 uint64_t block_shift =
1030 ztest_random(maxbs - ztest_spa->spa_max_ashift + 1);
b02fe35d 1031 return (1 << (SPA_MINBLOCKSHIFT + block_shift));
428870ff 1032}
34dc7c2f 1033
50c957f7
NB
1034static int
1035ztest_random_dnodesize(void)
1036{
1037 int slots;
1038 int max_slots = spa_maxdnodesize(ztest_spa) >> DNODE_SHIFT;
1039
1040 if (max_slots == DNODE_MIN_SLOTS)
1041 return (DNODE_MIN_SIZE);
1042
1043 /*
1044 * Weight the random distribution more heavily toward smaller
1045 * dnode sizes since that is more likely to reflect real-world
1046 * usage.
1047 */
1048 ASSERT3U(max_slots, >, 4);
1049 switch (ztest_random(10)) {
1050 case 0:
1051 slots = 5 + ztest_random(max_slots - 4);
1052 break;
1053 case 1 ... 4:
1054 slots = 2 + ztest_random(3);
1055 break;
1056 default:
1057 slots = 1;
1058 break;
1059 }
1060
1061 return (slots << DNODE_SHIFT);
1062}
1063
428870ff
BB
1064static int
1065ztest_random_ibshift(void)
1066{
1067 return (DN_MIN_INDBLKSHIFT +
1068 ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1));
34dc7c2f
BB
1069}
1070
428870ff
BB
1071static uint64_t
1072ztest_random_vdev_top(spa_t *spa, boolean_t log_ok)
34dc7c2f 1073{
428870ff
BB
1074 uint64_t top;
1075 vdev_t *rvd = spa->spa_root_vdev;
1076 vdev_t *tvd;
34dc7c2f 1077
428870ff 1078 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
34dc7c2f 1079
428870ff
BB
1080 do {
1081 top = ztest_random(rvd->vdev_children);
1082 tvd = rvd->vdev_child[top];
1083 } while (tvd->vdev_ishole || (tvd->vdev_islog && !log_ok) ||
1084 tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL);
34dc7c2f 1085
428870ff 1086 return (top);
34dc7c2f
BB
1087}
1088
428870ff
BB
1089static uint64_t
1090ztest_random_dsl_prop(zfs_prop_t prop)
34dc7c2f 1091{
428870ff
BB
1092 uint64_t value;
1093
1094 do {
1095 value = zfs_prop_random_value(prop, ztest_random(-1ULL));
1096 } while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF);
1097
1098 return (value);
34dc7c2f
BB
1099}
1100
34dc7c2f 1101static int
428870ff
BB
1102ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value,
1103 boolean_t inherit)
34dc7c2f 1104{
428870ff
BB
1105 const char *propname = zfs_prop_to_name(prop);
1106 const char *valname;
40b84e7a 1107 char *setpoint;
428870ff 1108 uint64_t curval;
34dc7c2f
BB
1109 int error;
1110
13fe0198
MA
1111 error = dsl_prop_set_int(osname, propname,
1112 (inherit ? ZPROP_SRC_NONE : ZPROP_SRC_LOCAL), value);
34dc7c2f 1113
428870ff
BB
1114 if (error == ENOSPC) {
1115 ztest_record_enospc(FTAG);
34dc7c2f
BB
1116 return (error);
1117 }
c99c9001 1118 ASSERT0(error);
34dc7c2f 1119
40b84e7a 1120 setpoint = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
13fe0198 1121 VERIFY0(dsl_prop_get_integer(osname, propname, &curval, setpoint));
428870ff 1122
c242c188 1123 if (ztest_opts.zo_verbose >= 6) {
6bec4351
TC
1124 int err;
1125
1126 err = zfs_prop_index_to_string(prop, curval, &valname);
1127 if (err)
1128 (void) printf("%s %s = %llu at '%s'\n",
1129 osname, propname, (unsigned long long)curval,
1130 setpoint);
1131 else
1132 (void) printf("%s %s = %s at '%s'\n",
1133 osname, propname, valname, setpoint);
34dc7c2f 1134 }
40b84e7a 1135 umem_free(setpoint, MAXPATHLEN);
34dc7c2f
BB
1136
1137 return (error);
1138}
1139
1140static int
c242c188 1141ztest_spa_prop_set_uint64(zpool_prop_t prop, uint64_t value)
34dc7c2f 1142{
c242c188 1143 spa_t *spa = ztest_spa;
428870ff 1144 nvlist_t *props = NULL;
34dc7c2f
BB
1145 int error;
1146
428870ff
BB
1147 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
1148 VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0);
34dc7c2f 1149
428870ff
BB
1150 error = spa_prop_set(spa, props);
1151
1152 nvlist_free(props);
1153
1154 if (error == ENOSPC) {
1155 ztest_record_enospc(FTAG);
34dc7c2f
BB
1156 return (error);
1157 }
c99c9001 1158 ASSERT0(error);
34dc7c2f
BB
1159
1160 return (error);
1161}
1162
e3a07cd0
BP
1163
1164/*
1165 * Object and range lock mechanics
1166 */
1167typedef struct {
1168 list_node_t z_lnode;
1169 refcount_t z_refcnt;
1170 uint64_t z_object;
1171 zfs_rlock_t z_range_lock;
1172} ztest_znode_t;
1173
1174typedef struct {
1175 rl_t *z_rl;
1176 ztest_znode_t *z_ztznode;
1177} ztest_zrl_t;
1178
1179static ztest_znode_t *
1180ztest_znode_init(uint64_t object)
1181{
1182 ztest_znode_t *zp = umem_alloc(sizeof (*zp), UMEM_NOFAIL);
1183
1184 list_link_init(&zp->z_lnode);
1185 refcount_create(&zp->z_refcnt);
1186 zp->z_object = object;
1187 zfs_rlock_init(&zp->z_range_lock);
1188
1189 return (zp);
1190}
1191
1192static void
1193ztest_znode_fini(ztest_znode_t *zp)
1194{
1195 ASSERT(refcount_is_zero(&zp->z_refcnt));
1196 zfs_rlock_destroy(&zp->z_range_lock);
1197 zp->z_object = 0;
1198 refcount_destroy(&zp->z_refcnt);
1199 list_link_init(&zp->z_lnode);
1200 umem_free(zp, sizeof (*zp));
1201}
1202
1203static void
1204ztest_zll_init(zll_t *zll)
1205{
1206 mutex_init(&zll->z_lock, NULL, MUTEX_DEFAULT, NULL);
1207 list_create(&zll->z_list, sizeof (ztest_znode_t),
1208 offsetof(ztest_znode_t, z_lnode));
1209}
1210
1211static void
1212ztest_zll_destroy(zll_t *zll)
1213{
1214 list_destroy(&zll->z_list);
1215 mutex_destroy(&zll->z_lock);
1216}
1217
1218#define RL_TAG "range_lock"
1219static ztest_znode_t *
1220ztest_znode_get(ztest_ds_t *zd, uint64_t object)
1221{
1222 zll_t *zll = &zd->zd_range_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1223 ztest_znode_t *zp = NULL;
1224 mutex_enter(&zll->z_lock);
1225 for (zp = list_head(&zll->z_list); (zp);
1226 zp = list_next(&zll->z_list, zp)) {
1227 if (zp->z_object == object) {
1228 refcount_add(&zp->z_refcnt, RL_TAG);
1229 break;
1230 }
1231 }
1232 if (zp == NULL) {
1233 zp = ztest_znode_init(object);
1234 refcount_add(&zp->z_refcnt, RL_TAG);
1235 list_insert_head(&zll->z_list, zp);
1236 }
1237 mutex_exit(&zll->z_lock);
1238 return (zp);
1239}
1240
1241static void
1242ztest_znode_put(ztest_ds_t *zd, ztest_znode_t *zp)
1243{
1244 zll_t *zll = NULL;
1245 ASSERT3U(zp->z_object, !=, 0);
1246 zll = &zd->zd_range_lock[zp->z_object & (ZTEST_OBJECT_LOCKS - 1)];
1247 mutex_enter(&zll->z_lock);
1248 refcount_remove(&zp->z_refcnt, RL_TAG);
1249 if (refcount_is_zero(&zp->z_refcnt)) {
1250 list_remove(&zll->z_list, zp);
1251 ztest_znode_fini(zp);
1252 }
1253 mutex_exit(&zll->z_lock);
1254}
1255
1256
428870ff
BB
1257static void
1258ztest_rll_init(rll_t *rll)
1259{
1260 rll->rll_writer = NULL;
1261 rll->rll_readers = 0;
1e33ac1e
BB
1262 mutex_init(&rll->rll_lock, NULL, MUTEX_DEFAULT, NULL);
1263 cv_init(&rll->rll_cv, NULL, CV_DEFAULT, NULL);
428870ff 1264}
34dc7c2f 1265
428870ff
BB
1266static void
1267ztest_rll_destroy(rll_t *rll)
34dc7c2f 1268{
428870ff
BB
1269 ASSERT(rll->rll_writer == NULL);
1270 ASSERT(rll->rll_readers == 0);
1e33ac1e
BB
1271 mutex_destroy(&rll->rll_lock);
1272 cv_destroy(&rll->rll_cv);
428870ff 1273}
34dc7c2f 1274
428870ff
BB
1275static void
1276ztest_rll_lock(rll_t *rll, rl_type_t type)
1277{
1e33ac1e 1278 mutex_enter(&rll->rll_lock);
34dc7c2f 1279
428870ff
BB
1280 if (type == RL_READER) {
1281 while (rll->rll_writer != NULL)
1e33ac1e 1282 (void) cv_wait(&rll->rll_cv, &rll->rll_lock);
428870ff
BB
1283 rll->rll_readers++;
1284 } else {
1285 while (rll->rll_writer != NULL || rll->rll_readers)
1e33ac1e 1286 (void) cv_wait(&rll->rll_cv, &rll->rll_lock);
428870ff
BB
1287 rll->rll_writer = curthread;
1288 }
34dc7c2f 1289
1e33ac1e 1290 mutex_exit(&rll->rll_lock);
428870ff 1291}
34dc7c2f 1292
428870ff
BB
1293static void
1294ztest_rll_unlock(rll_t *rll)
1295{
1e33ac1e 1296 mutex_enter(&rll->rll_lock);
34dc7c2f 1297
428870ff
BB
1298 if (rll->rll_writer) {
1299 ASSERT(rll->rll_readers == 0);
1300 rll->rll_writer = NULL;
1301 } else {
1302 ASSERT(rll->rll_readers != 0);
1303 ASSERT(rll->rll_writer == NULL);
1304 rll->rll_readers--;
1305 }
34dc7c2f 1306
428870ff 1307 if (rll->rll_writer == NULL && rll->rll_readers == 0)
1e33ac1e 1308 cv_broadcast(&rll->rll_cv);
428870ff 1309
1e33ac1e 1310 mutex_exit(&rll->rll_lock);
34dc7c2f
BB
1311}
1312
428870ff
BB
1313static void
1314ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type)
b128c09f 1315{
428870ff 1316 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
b128c09f 1317
428870ff
BB
1318 ztest_rll_lock(rll, type);
1319}
b128c09f 1320
428870ff
BB
1321static void
1322ztest_object_unlock(ztest_ds_t *zd, uint64_t object)
1323{
1324 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
b128c09f 1325
428870ff 1326 ztest_rll_unlock(rll);
b128c09f
BB
1327}
1328
e3a07cd0
BP
1329static ztest_zrl_t *
1330ztest_zrl_init(rl_t *rl, ztest_znode_t *zp)
34dc7c2f 1331{
e3a07cd0
BP
1332 ztest_zrl_t *zrl = umem_alloc(sizeof (*zrl), UMEM_NOFAIL);
1333 zrl->z_rl = rl;
1334 zrl->z_ztznode = zp;
1335 return (zrl);
428870ff 1336}
34dc7c2f 1337
428870ff 1338static void
e3a07cd0 1339ztest_zrl_fini(ztest_zrl_t *zrl)
428870ff 1340{
e3a07cd0
BP
1341 umem_free(zrl, sizeof (*zrl));
1342}
34dc7c2f 1343
e3a07cd0
BP
1344static ztest_zrl_t *
1345ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset,
1346 uint64_t size, rl_type_t type)
1347{
1348 ztest_znode_t *zp = ztest_znode_get(zd, object);
1349 rl_t *rl = zfs_range_lock(&zp->z_range_lock, offset,
1350 size, type);
1351 return (ztest_zrl_init(rl, zp));
1352}
34dc7c2f 1353
e3a07cd0
BP
1354static void
1355ztest_range_unlock(ztest_ds_t *zd, ztest_zrl_t *zrl)
1356{
1357 zfs_range_unlock(zrl->z_rl);
1358 ztest_znode_put(zd, zrl->z_ztznode);
1359 ztest_zrl_fini(zrl);
428870ff 1360}
34dc7c2f 1361
428870ff 1362static void
c242c188 1363ztest_zd_init(ztest_ds_t *zd, ztest_shared_ds_t *szd, objset_t *os)
428870ff
BB
1364{
1365 zd->zd_os = os;
1366 zd->zd_zilog = dmu_objset_zil(os);
c242c188 1367 zd->zd_shared = szd;
428870ff 1368 dmu_objset_name(os, zd->zd_name);
d6320ddb 1369 int l;
428870ff 1370
c242c188
CS
1371 if (zd->zd_shared != NULL)
1372 zd->zd_shared->zd_seq = 0;
1373
7809eb8b 1374 VERIFY(rwlock_init(&zd->zd_zilog_lock, USYNC_THREAD, NULL) == 0);
1e33ac1e 1375 mutex_init(&zd->zd_dirobj_lock, NULL, MUTEX_DEFAULT, NULL);
34dc7c2f 1376
d6320ddb 1377 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
428870ff 1378 ztest_rll_init(&zd->zd_object_lock[l]);
34dc7c2f 1379
d6320ddb 1380 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
e3a07cd0 1381 ztest_zll_init(&zd->zd_range_lock[l]);
34dc7c2f
BB
1382}
1383
428870ff
BB
1384static void
1385ztest_zd_fini(ztest_ds_t *zd)
34dc7c2f 1386{
d6320ddb
BB
1387 int l;
1388
1e33ac1e 1389 mutex_destroy(&zd->zd_dirobj_lock);
7809eb8b 1390 (void) rwlock_destroy(&zd->zd_zilog_lock);
34dc7c2f 1391
d6320ddb 1392 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
428870ff 1393 ztest_rll_destroy(&zd->zd_object_lock[l]);
b128c09f 1394
d6320ddb 1395 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
e3a07cd0 1396 ztest_zll_destroy(&zd->zd_range_lock[l]);
428870ff 1397}
b128c09f 1398
428870ff 1399#define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
b128c09f 1400
428870ff
BB
1401static uint64_t
1402ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag)
1403{
1404 uint64_t txg;
1405 int error;
1406
1407 /*
1408 * Attempt to assign tx to some transaction group.
1409 */
1410 error = dmu_tx_assign(tx, txg_how);
1411 if (error) {
1412 if (error == ERESTART) {
1413 ASSERT(txg_how == TXG_NOWAIT);
1414 dmu_tx_wait(tx);
1415 } else {
1416 ASSERT3U(error, ==, ENOSPC);
1417 ztest_record_enospc(tag);
1418 }
1419 dmu_tx_abort(tx);
1420 return (0);
1421 }
1422 txg = dmu_tx_get_txg(tx);
1423 ASSERT(txg != 0);
1424 return (txg);
1425}
1426
1427static void
1428ztest_pattern_set(void *buf, uint64_t size, uint64_t value)
1429{
1430 uint64_t *ip = buf;
1431 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1432
1433 while (ip < ip_end)
1434 *ip++ = value;
1435}
1436
1fde1e37 1437#ifndef NDEBUG
428870ff
BB
1438static boolean_t
1439ztest_pattern_match(void *buf, uint64_t size, uint64_t value)
1440{
1441 uint64_t *ip = buf;
1442 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1443 uint64_t diff = 0;
1444
1445 while (ip < ip_end)
1446 diff |= (value - *ip++);
1447
1448 return (diff == 0);
1449}
1fde1e37 1450#endif
428870ff
BB
1451
1452static void
1453ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
50c957f7
NB
1454 uint64_t dnodesize, uint64_t offset, uint64_t gen, uint64_t txg,
1455 uint64_t crtxg)
428870ff
BB
1456{
1457 bt->bt_magic = BT_MAGIC;
1458 bt->bt_objset = dmu_objset_id(os);
1459 bt->bt_object = object;
50c957f7 1460 bt->bt_dnodesize = dnodesize;
428870ff
BB
1461 bt->bt_offset = offset;
1462 bt->bt_gen = gen;
1463 bt->bt_txg = txg;
1464 bt->bt_crtxg = crtxg;
1465}
1466
1467static void
1468ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
50c957f7
NB
1469 uint64_t dnodesize, uint64_t offset, uint64_t gen, uint64_t txg,
1470 uint64_t crtxg)
428870ff 1471{
9b67f605
MA
1472 ASSERT3U(bt->bt_magic, ==, BT_MAGIC);
1473 ASSERT3U(bt->bt_objset, ==, dmu_objset_id(os));
1474 ASSERT3U(bt->bt_object, ==, object);
50c957f7 1475 ASSERT3U(bt->bt_dnodesize, ==, dnodesize);
9b67f605
MA
1476 ASSERT3U(bt->bt_offset, ==, offset);
1477 ASSERT3U(bt->bt_gen, <=, gen);
1478 ASSERT3U(bt->bt_txg, <=, txg);
1479 ASSERT3U(bt->bt_crtxg, ==, crtxg);
428870ff
BB
1480}
1481
1482static ztest_block_tag_t *
1483ztest_bt_bonus(dmu_buf_t *db)
1484{
1485 dmu_object_info_t doi;
1486 ztest_block_tag_t *bt;
1487
1488 dmu_object_info_from_db(db, &doi);
1489 ASSERT3U(doi.doi_bonus_size, <=, db->db_size);
1490 ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt));
1491 bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt));
1492
1493 return (bt);
1494}
1495
50c957f7
NB
1496/*
1497 * Generate a token to fill up unused bonus buffer space. Try to make
1498 * it unique to the object, generation, and offset to verify that data
1499 * is not getting overwritten by data from other dnodes.
1500 */
1501#define ZTEST_BONUS_FILL_TOKEN(obj, ds, gen, offset) \
1502 (((ds) << 48) | ((gen) << 32) | ((obj) << 8) | (offset))
1503
1504/*
1505 * Fill up the unused bonus buffer region before the block tag with a
1506 * verifiable pattern. Filling the whole bonus area with non-zero data
1507 * helps ensure that all dnode traversal code properly skips the
1508 * interior regions of large dnodes.
1509 */
1510void
1511ztest_fill_unused_bonus(dmu_buf_t *db, void *end, uint64_t obj,
1512 objset_t *os, uint64_t gen)
1513{
1514 uint64_t *bonusp;
1515
1516 ASSERT(IS_P2ALIGNED((char *)end - (char *)db->db_data, 8));
1517
1518 for (bonusp = db->db_data; bonusp < (uint64_t *)end; bonusp++) {
1519 uint64_t token = ZTEST_BONUS_FILL_TOKEN(obj, dmu_objset_id(os),
1520 gen, bonusp - (uint64_t *)db->db_data);
1521 *bonusp = token;
1522 }
1523}
1524
1525/*
1526 * Verify that the unused area of a bonus buffer is filled with the
1527 * expected tokens.
1528 */
1529void
1530ztest_verify_unused_bonus(dmu_buf_t *db, void *end, uint64_t obj,
1531 objset_t *os, uint64_t gen)
1532{
1533 uint64_t *bonusp;
1534
1535 for (bonusp = db->db_data; bonusp < (uint64_t *)end; bonusp++) {
1536 uint64_t token = ZTEST_BONUS_FILL_TOKEN(obj, dmu_objset_id(os),
1537 gen, bonusp - (uint64_t *)db->db_data);
1538 VERIFY3U(*bonusp, ==, token);
1539 }
1540}
1541
428870ff
BB
1542/*
1543 * ZIL logging ops
1544 */
1545
1546#define lrz_type lr_mode
1547#define lrz_blocksize lr_uid
1548#define lrz_ibshift lr_gid
1549#define lrz_bonustype lr_rdev
50c957f7 1550#define lrz_dnodesize lr_crtime[1]
428870ff 1551
572e2857 1552static void
428870ff
BB
1553ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr)
1554{
1555 char *name = (void *)(lr + 1); /* name follows lr */
1556 size_t namesize = strlen(name) + 1;
1557 itx_t *itx;
1558
1559 if (zil_replaying(zd->zd_zilog, tx))
572e2857 1560 return;
428870ff
BB
1561
1562 itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize);
1563 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1564 sizeof (*lr) + namesize - sizeof (lr_t));
1565
572e2857 1566 zil_itx_assign(zd->zd_zilog, itx, tx);
428870ff
BB
1567}
1568
572e2857
BB
1569static void
1570ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr, uint64_t object)
428870ff
BB
1571{
1572 char *name = (void *)(lr + 1); /* name follows lr */
1573 size_t namesize = strlen(name) + 1;
1574 itx_t *itx;
1575
1576 if (zil_replaying(zd->zd_zilog, tx))
572e2857 1577 return;
428870ff
BB
1578
1579 itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize);
1580 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1581 sizeof (*lr) + namesize - sizeof (lr_t));
1582
572e2857
BB
1583 itx->itx_oid = object;
1584 zil_itx_assign(zd->zd_zilog, itx, tx);
428870ff
BB
1585}
1586
572e2857 1587static void
428870ff
BB
1588ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr)
1589{
1590 itx_t *itx;
1591 itx_wr_state_t write_state = ztest_random(WR_NUM_STATES);
1592
1593 if (zil_replaying(zd->zd_zilog, tx))
572e2857 1594 return;
428870ff
BB
1595
1596 if (lr->lr_length > ZIL_MAX_LOG_DATA)
1597 write_state = WR_INDIRECT;
1598
1599 itx = zil_itx_create(TX_WRITE,
1600 sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0));
1601
1602 if (write_state == WR_COPIED &&
1603 dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length,
1604 ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) {
1605 zil_itx_destroy(itx);
1606 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1607 write_state = WR_NEED_COPY;
1608 }
1609 itx->itx_private = zd;
1610 itx->itx_wr_state = write_state;
1611 itx->itx_sync = (ztest_random(8) == 0);
1612 itx->itx_sod += (write_state == WR_NEED_COPY ? lr->lr_length : 0);
1613
1614 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1615 sizeof (*lr) - sizeof (lr_t));
1616
572e2857 1617 zil_itx_assign(zd->zd_zilog, itx, tx);
428870ff
BB
1618}
1619
572e2857 1620static void
428870ff
BB
1621ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr)
1622{
1623 itx_t *itx;
1624
1625 if (zil_replaying(zd->zd_zilog, tx))
572e2857 1626 return;
428870ff
BB
1627
1628 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1629 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1630 sizeof (*lr) - sizeof (lr_t));
1631
572e2857
BB
1632 itx->itx_sync = B_FALSE;
1633 zil_itx_assign(zd->zd_zilog, itx, tx);
428870ff
BB
1634}
1635
572e2857 1636static void
428870ff
BB
1637ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr)
1638{
1639 itx_t *itx;
1640
1641 if (zil_replaying(zd->zd_zilog, tx))
572e2857 1642 return;
428870ff
BB
1643
1644 itx = zil_itx_create(TX_SETATTR, sizeof (*lr));
1645 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1646 sizeof (*lr) - sizeof (lr_t));
1647
572e2857
BB
1648 itx->itx_sync = B_FALSE;
1649 zil_itx_assign(zd->zd_zilog, itx, tx);
428870ff
BB
1650}
1651
1652/*
1653 * ZIL replay ops
1654 */
1655static int
1656ztest_replay_create(ztest_ds_t *zd, lr_create_t *lr, boolean_t byteswap)
1657{
1658 char *name = (void *)(lr + 1); /* name follows lr */
1659 objset_t *os = zd->zd_os;
1660 ztest_block_tag_t *bbt;
1661 dmu_buf_t *db;
1662 dmu_tx_t *tx;
1663 uint64_t txg;
1664 int error = 0;
50c957f7 1665 int bonuslen;
428870ff
BB
1666
1667 if (byteswap)
1668 byteswap_uint64_array(lr, sizeof (*lr));
1669
1670 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1671 ASSERT(name[0] != '\0');
1672
1673 tx = dmu_tx_create(os);
1674
1675 dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name);
1676
1677 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1678 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1679 } else {
1680 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1681 }
1682
1683 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1684 if (txg == 0)
1685 return (ENOSPC);
1686
1687 ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid);
50c957f7 1688 bonuslen = DN_BONUS_SIZE(lr->lrz_dnodesize);
428870ff
BB
1689
1690 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1691 if (lr->lr_foid == 0) {
50c957f7 1692 lr->lr_foid = zap_create_dnsize(os,
428870ff 1693 lr->lrz_type, lr->lrz_bonustype,
50c957f7 1694 bonuslen, lr->lrz_dnodesize, tx);
428870ff 1695 } else {
50c957f7 1696 error = zap_create_claim_dnsize(os, lr->lr_foid,
428870ff 1697 lr->lrz_type, lr->lrz_bonustype,
50c957f7 1698 bonuslen, lr->lrz_dnodesize, tx);
428870ff
BB
1699 }
1700 } else {
1701 if (lr->lr_foid == 0) {
50c957f7 1702 lr->lr_foid = dmu_object_alloc_dnsize(os,
428870ff 1703 lr->lrz_type, 0, lr->lrz_bonustype,
50c957f7 1704 bonuslen, lr->lrz_dnodesize, tx);
428870ff 1705 } else {
50c957f7 1706 error = dmu_object_claim_dnsize(os, lr->lr_foid,
428870ff 1707 lr->lrz_type, 0, lr->lrz_bonustype,
50c957f7 1708 bonuslen, lr->lrz_dnodesize, tx);
428870ff
BB
1709 }
1710 }
1711
1712 if (error) {
1713 ASSERT3U(error, ==, EEXIST);
1714 ASSERT(zd->zd_zilog->zl_replay);
1715 dmu_tx_commit(tx);
1716 return (error);
1717 }
1718
1719 ASSERT(lr->lr_foid != 0);
1720
1721 if (lr->lrz_type != DMU_OT_ZAP_OTHER)
1722 VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid,
1723 lr->lrz_blocksize, lr->lrz_ibshift, tx));
1724
1725 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1726 bbt = ztest_bt_bonus(db);
1727 dmu_buf_will_dirty(db, tx);
50c957f7
NB
1728 ztest_bt_generate(bbt, os, lr->lr_foid, lr->lrz_dnodesize, -1ULL,
1729 lr->lr_gen, txg, txg);
1730 ztest_fill_unused_bonus(db, bbt, lr->lr_foid, os, lr->lr_gen);
428870ff
BB
1731 dmu_buf_rele(db, FTAG);
1732
1733 VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1,
1734 &lr->lr_foid, tx));
1735
1736 (void) ztest_log_create(zd, tx, lr);
1737
1738 dmu_tx_commit(tx);
1739
1740 return (0);
1741}
1742
1743static int
1744ztest_replay_remove(ztest_ds_t *zd, lr_remove_t *lr, boolean_t byteswap)
1745{
1746 char *name = (void *)(lr + 1); /* name follows lr */
1747 objset_t *os = zd->zd_os;
1748 dmu_object_info_t doi;
1749 dmu_tx_t *tx;
1750 uint64_t object, txg;
1751
1752 if (byteswap)
1753 byteswap_uint64_array(lr, sizeof (*lr));
1754
1755 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1756 ASSERT(name[0] != '\0');
1757
1758 VERIFY3U(0, ==,
1759 zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object));
1760 ASSERT(object != 0);
1761
1762 ztest_object_lock(zd, object, RL_WRITER);
1763
1764 VERIFY3U(0, ==, dmu_object_info(os, object, &doi));
1765
1766 tx = dmu_tx_create(os);
1767
1768 dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name);
1769 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1770
1771 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1772 if (txg == 0) {
1773 ztest_object_unlock(zd, object);
1774 return (ENOSPC);
1775 }
1776
1777 if (doi.doi_type == DMU_OT_ZAP_OTHER) {
1778 VERIFY3U(0, ==, zap_destroy(os, object, tx));
1779 } else {
1780 VERIFY3U(0, ==, dmu_object_free(os, object, tx));
1781 }
1782
1783 VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx));
1784
572e2857 1785 (void) ztest_log_remove(zd, tx, lr, object);
428870ff
BB
1786
1787 dmu_tx_commit(tx);
1788
1789 ztest_object_unlock(zd, object);
1790
1791 return (0);
1792}
1793
1794static int
1795ztest_replay_write(ztest_ds_t *zd, lr_write_t *lr, boolean_t byteswap)
1796{
1797 objset_t *os = zd->zd_os;
1798 void *data = lr + 1; /* data follows lr */
1799 uint64_t offset, length;
1800 ztest_block_tag_t *bt = data;
1801 ztest_block_tag_t *bbt;
1802 uint64_t gen, txg, lrtxg, crtxg;
1803 dmu_object_info_t doi;
1804 dmu_tx_t *tx;
1805 dmu_buf_t *db;
1806 arc_buf_t *abuf = NULL;
e3a07cd0 1807 ztest_zrl_t *rl;
428870ff
BB
1808
1809 if (byteswap)
1810 byteswap_uint64_array(lr, sizeof (*lr));
1811
1812 offset = lr->lr_offset;
1813 length = lr->lr_length;
1814
1815 /* If it's a dmu_sync() block, write the whole block */
1816 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
1817 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
1818 if (length < blocksize) {
1819 offset -= offset % blocksize;
1820 length = blocksize;
1821 }
1822 }
1823
1824 if (bt->bt_magic == BSWAP_64(BT_MAGIC))
1825 byteswap_uint64_array(bt, sizeof (*bt));
1826
1827 if (bt->bt_magic != BT_MAGIC)
1828 bt = NULL;
1829
1830 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1831 rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER);
1832
1833 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1834
1835 dmu_object_info_from_db(db, &doi);
1836
1837 bbt = ztest_bt_bonus(db);
1838 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1839 gen = bbt->bt_gen;
1840 crtxg = bbt->bt_crtxg;
1841 lrtxg = lr->lr_common.lrc_txg;
1842
1843 tx = dmu_tx_create(os);
1844
1845 dmu_tx_hold_write(tx, lr->lr_foid, offset, length);
1846
1847 if (ztest_random(8) == 0 && length == doi.doi_data_block_size &&
1848 P2PHASE(offset, length) == 0)
1849 abuf = dmu_request_arcbuf(db, length);
1850
1851 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1852 if (txg == 0) {
1853 if (abuf != NULL)
1854 dmu_return_arcbuf(abuf);
1855 dmu_buf_rele(db, FTAG);
e3a07cd0 1856 ztest_range_unlock(zd, rl);
428870ff
BB
1857 ztest_object_unlock(zd, lr->lr_foid);
1858 return (ENOSPC);
1859 }
1860
1861 if (bt != NULL) {
1862 /*
1863 * Usually, verify the old data before writing new data --
1864 * but not always, because we also want to verify correct
1865 * behavior when the data was not recently read into cache.
1866 */
1867 ASSERT(offset % doi.doi_data_block_size == 0);
1868 if (ztest_random(4) != 0) {
1869 int prefetch = ztest_random(2) ?
1870 DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
1871 ztest_block_tag_t rbt;
1872
1873 VERIFY(dmu_read(os, lr->lr_foid, offset,
1874 sizeof (rbt), &rbt, prefetch) == 0);
1875 if (rbt.bt_magic == BT_MAGIC) {
50c957f7 1876 ztest_bt_verify(&rbt, os, lr->lr_foid, 0,
428870ff
BB
1877 offset, gen, txg, crtxg);
1878 }
1879 }
1880
1881 /*
1882 * Writes can appear to be newer than the bonus buffer because
1883 * the ztest_get_data() callback does a dmu_read() of the
1884 * open-context data, which may be different than the data
1885 * as it was when the write was generated.
1886 */
1887 if (zd->zd_zilog->zl_replay) {
50c957f7 1888 ztest_bt_verify(bt, os, lr->lr_foid, 0, offset,
428870ff
BB
1889 MAX(gen, bt->bt_gen), MAX(txg, lrtxg),
1890 bt->bt_crtxg);
1891 }
1892
1893 /*
1894 * Set the bt's gen/txg to the bonus buffer's gen/txg
1895 * so that all of the usual ASSERTs will work.
1896 */
50c957f7
NB
1897 ztest_bt_generate(bt, os, lr->lr_foid, 0, offset, gen, txg,
1898 crtxg);
428870ff
BB
1899 }
1900
1901 if (abuf == NULL) {
1902 dmu_write(os, lr->lr_foid, offset, length, data, tx);
1903 } else {
1904 bcopy(data, abuf->b_data, length);
1905 dmu_assign_arcbuf(db, offset, abuf, tx);
1906 }
1907
1908 (void) ztest_log_write(zd, tx, lr);
1909
1910 dmu_buf_rele(db, FTAG);
1911
1912 dmu_tx_commit(tx);
1913
e3a07cd0 1914 ztest_range_unlock(zd, rl);
428870ff
BB
1915 ztest_object_unlock(zd, lr->lr_foid);
1916
1917 return (0);
1918}
1919
1920static int
1921ztest_replay_truncate(ztest_ds_t *zd, lr_truncate_t *lr, boolean_t byteswap)
1922{
1923 objset_t *os = zd->zd_os;
1924 dmu_tx_t *tx;
1925 uint64_t txg;
e3a07cd0 1926 ztest_zrl_t *rl;
428870ff
BB
1927
1928 if (byteswap)
1929 byteswap_uint64_array(lr, sizeof (*lr));
1930
1931 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1932 rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length,
1933 RL_WRITER);
1934
1935 tx = dmu_tx_create(os);
1936
1937 dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length);
1938
1939 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1940 if (txg == 0) {
e3a07cd0 1941 ztest_range_unlock(zd, rl);
428870ff
BB
1942 ztest_object_unlock(zd, lr->lr_foid);
1943 return (ENOSPC);
1944 }
1945
1946 VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset,
1947 lr->lr_length, tx) == 0);
1948
1949 (void) ztest_log_truncate(zd, tx, lr);
1950
1951 dmu_tx_commit(tx);
1952
e3a07cd0 1953 ztest_range_unlock(zd, rl);
428870ff
BB
1954 ztest_object_unlock(zd, lr->lr_foid);
1955
1956 return (0);
1957}
1958
1959static int
1960ztest_replay_setattr(ztest_ds_t *zd, lr_setattr_t *lr, boolean_t byteswap)
1961{
1962 objset_t *os = zd->zd_os;
1963 dmu_tx_t *tx;
1964 dmu_buf_t *db;
1965 ztest_block_tag_t *bbt;
50c957f7 1966 uint64_t txg, lrtxg, crtxg, dnodesize;
428870ff
BB
1967
1968 if (byteswap)
1969 byteswap_uint64_array(lr, sizeof (*lr));
1970
1971 ztest_object_lock(zd, lr->lr_foid, RL_WRITER);
1972
1973 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1974
1975 tx = dmu_tx_create(os);
1976 dmu_tx_hold_bonus(tx, lr->lr_foid);
1977
1978 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1979 if (txg == 0) {
1980 dmu_buf_rele(db, FTAG);
1981 ztest_object_unlock(zd, lr->lr_foid);
1982 return (ENOSPC);
1983 }
1984
1985 bbt = ztest_bt_bonus(db);
1986 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1987 crtxg = bbt->bt_crtxg;
1988 lrtxg = lr->lr_common.lrc_txg;
50c957f7 1989 dnodesize = bbt->bt_dnodesize;
428870ff
BB
1990
1991 if (zd->zd_zilog->zl_replay) {
1992 ASSERT(lr->lr_size != 0);
1993 ASSERT(lr->lr_mode != 0);
1994 ASSERT(lrtxg != 0);
1995 } else {
1996 /*
1997 * Randomly change the size and increment the generation.
1998 */
1999 lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) *
2000 sizeof (*bbt);
2001 lr->lr_mode = bbt->bt_gen + 1;
2002 ASSERT(lrtxg == 0);
2003 }
2004
2005 /*
2006 * Verify that the current bonus buffer is not newer than our txg.
2007 */
50c957f7 2008 ztest_bt_verify(bbt, os, lr->lr_foid, dnodesize, -1ULL, lr->lr_mode,
428870ff
BB
2009 MAX(txg, lrtxg), crtxg);
2010
2011 dmu_buf_will_dirty(db, tx);
2012
2013 ASSERT3U(lr->lr_size, >=, sizeof (*bbt));
2014 ASSERT3U(lr->lr_size, <=, db->db_size);
c99c9001 2015 VERIFY0(dmu_set_bonus(db, lr->lr_size, tx));
428870ff
BB
2016 bbt = ztest_bt_bonus(db);
2017
50c957f7
NB
2018 ztest_bt_generate(bbt, os, lr->lr_foid, dnodesize, -1ULL, lr->lr_mode,
2019 txg, crtxg);
2020 ztest_fill_unused_bonus(db, bbt, lr->lr_foid, os, bbt->bt_gen);
428870ff
BB
2021 dmu_buf_rele(db, FTAG);
2022
2023 (void) ztest_log_setattr(zd, tx, lr);
2024
2025 dmu_tx_commit(tx);
2026
2027 ztest_object_unlock(zd, lr->lr_foid);
2028
2029 return (0);
2030}
2031
b01615d5 2032zil_replay_func_t ztest_replay_vector[TX_MAX_TYPE] = {
0bc8fd78 2033 NULL, /* 0 no such transaction type */
b01615d5 2034 (zil_replay_func_t)ztest_replay_create, /* TX_CREATE */
0bc8fd78
BB
2035 NULL, /* TX_MKDIR */
2036 NULL, /* TX_MKXATTR */
2037 NULL, /* TX_SYMLINK */
b01615d5 2038 (zil_replay_func_t)ztest_replay_remove, /* TX_REMOVE */
0bc8fd78
BB
2039 NULL, /* TX_RMDIR */
2040 NULL, /* TX_LINK */
2041 NULL, /* TX_RENAME */
b01615d5
RY
2042 (zil_replay_func_t)ztest_replay_write, /* TX_WRITE */
2043 (zil_replay_func_t)ztest_replay_truncate, /* TX_TRUNCATE */
2044 (zil_replay_func_t)ztest_replay_setattr, /* TX_SETATTR */
0bc8fd78
BB
2045 NULL, /* TX_ACL */
2046 NULL, /* TX_CREATE_ACL */
2047 NULL, /* TX_CREATE_ATTR */
2048 NULL, /* TX_CREATE_ACL_ATTR */
2049 NULL, /* TX_MKDIR_ACL */
2050 NULL, /* TX_MKDIR_ATTR */
2051 NULL, /* TX_MKDIR_ACL_ATTR */
2052 NULL, /* TX_WRITE2 */
428870ff
BB
2053};
2054
2055/*
2056 * ZIL get_data callbacks
2057 */
e3a07cd0
BP
2058typedef struct ztest_zgd_private {
2059 ztest_ds_t *z_zd;
2060 ztest_zrl_t *z_rl;
2061 uint64_t z_object;
2062} ztest_zgd_private_t;
428870ff
BB
2063
2064static void
2065ztest_get_done(zgd_t *zgd, int error)
2066{
e3a07cd0
BP
2067 ztest_zgd_private_t *zzp = zgd->zgd_private;
2068 ztest_ds_t *zd = zzp->z_zd;
2069 uint64_t object = zzp->z_object;
428870ff
BB
2070
2071 if (zgd->zgd_db)
2072 dmu_buf_rele(zgd->zgd_db, zgd);
2073
e3a07cd0 2074 ztest_range_unlock(zd, zzp->z_rl);
428870ff
BB
2075 ztest_object_unlock(zd, object);
2076
2077 if (error == 0 && zgd->zgd_bp)
2078 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
2079
2080 umem_free(zgd, sizeof (*zgd));
e3a07cd0 2081 umem_free(zzp, sizeof (*zzp));
428870ff
BB
2082}
2083
2084static int
2085ztest_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
2086{
2087 ztest_ds_t *zd = arg;
2088 objset_t *os = zd->zd_os;
2089 uint64_t object = lr->lr_foid;
2090 uint64_t offset = lr->lr_offset;
2091 uint64_t size = lr->lr_length;
2092 blkptr_t *bp = &lr->lr_blkptr;
2093 uint64_t txg = lr->lr_common.lrc_txg;
2094 uint64_t crtxg;
2095 dmu_object_info_t doi;
2096 dmu_buf_t *db;
2097 zgd_t *zgd;
2098 int error;
e3a07cd0 2099 ztest_zgd_private_t *zgd_private;
428870ff
BB
2100
2101 ztest_object_lock(zd, object, RL_READER);
2102 error = dmu_bonus_hold(os, object, FTAG, &db);
2103 if (error) {
2104 ztest_object_unlock(zd, object);
2105 return (error);
2106 }
2107
2108 crtxg = ztest_bt_bonus(db)->bt_crtxg;
2109
2110 if (crtxg == 0 || crtxg > txg) {
2111 dmu_buf_rele(db, FTAG);
2112 ztest_object_unlock(zd, object);
2113 return (ENOENT);
2114 }
2115
2116 dmu_object_info_from_db(db, &doi);
2117 dmu_buf_rele(db, FTAG);
2118 db = NULL;
2119
2120 zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL);
2121 zgd->zgd_zilog = zd->zd_zilog;
e3a07cd0
BP
2122 zgd_private = umem_zalloc(sizeof (ztest_zgd_private_t), UMEM_NOFAIL);
2123 zgd_private->z_zd = zd;
2124 zgd_private->z_object = object;
2125 zgd->zgd_private = zgd_private;
428870ff
BB
2126
2127 if (buf != NULL) { /* immediate write */
e3a07cd0 2128 zgd_private->z_rl = ztest_range_lock(zd, object, offset, size,
428870ff
BB
2129 RL_READER);
2130
2131 error = dmu_read(os, object, offset, size, buf,
2132 DMU_READ_NO_PREFETCH);
2133 ASSERT(error == 0);
2134 } else {
2135 size = doi.doi_data_block_size;
2136 if (ISP2(size)) {
2137 offset = P2ALIGN(offset, size);
2138 } else {
2139 ASSERT(offset < size);
2140 offset = 0;
2141 }
2142
e3a07cd0 2143 zgd_private->z_rl = ztest_range_lock(zd, object, offset, size,
428870ff
BB
2144 RL_READER);
2145
2146 error = dmu_buf_hold(os, object, offset, zgd, &db,
2147 DMU_READ_NO_PREFETCH);
2148
2149 if (error == 0) {
03c6040b
GW
2150 blkptr_t *obp = dmu_buf_get_blkptr(db);
2151 if (obp) {
2152 ASSERT(BP_IS_HOLE(bp));
2153 *bp = *obp;
2154 }
2155
428870ff
BB
2156 zgd->zgd_db = db;
2157 zgd->zgd_bp = bp;
2158
2159 ASSERT(db->db_offset == offset);
2160 ASSERT(db->db_size == size);
2161
2162 error = dmu_sync(zio, lr->lr_common.lrc_txg,
2163 ztest_get_done, zgd);
2164
2165 if (error == 0)
2166 return (0);
2167 }
2168 }
2169
2170 ztest_get_done(zgd, error);
2171
2172 return (error);
2173}
2174
2175static void *
2176ztest_lr_alloc(size_t lrsize, char *name)
2177{
2178 char *lr;
2179 size_t namesize = name ? strlen(name) + 1 : 0;
2180
2181 lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
2182
2183 if (name)
2184 bcopy(name, lr + lrsize, namesize);
2185
2186 return (lr);
2187}
2188
2189void
2190ztest_lr_free(void *lr, size_t lrsize, char *name)
2191{
2192 size_t namesize = name ? strlen(name) + 1 : 0;
2193
2194 umem_free(lr, lrsize + namesize);
2195}
2196
2197/*
2198 * Lookup a bunch of objects. Returns the number of objects not found.
2199 */
2200static int
2201ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
2202{
2203 int missing = 0;
2204 int error;
d6320ddb 2205 int i;
428870ff 2206
1e33ac1e 2207 ASSERT(mutex_held(&zd->zd_dirobj_lock));
428870ff 2208
d6320ddb 2209 for (i = 0; i < count; i++, od++) {
428870ff
BB
2210 od->od_object = 0;
2211 error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
2212 sizeof (uint64_t), 1, &od->od_object);
2213 if (error) {
2214 ASSERT(error == ENOENT);
2215 ASSERT(od->od_object == 0);
2216 missing++;
2217 } else {
2218 dmu_buf_t *db;
2219 ztest_block_tag_t *bbt;
2220 dmu_object_info_t doi;
2221
2222 ASSERT(od->od_object != 0);
2223 ASSERT(missing == 0); /* there should be no gaps */
2224
2225 ztest_object_lock(zd, od->od_object, RL_READER);
2226 VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
2227 od->od_object, FTAG, &db));
2228 dmu_object_info_from_db(db, &doi);
2229 bbt = ztest_bt_bonus(db);
2230 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
2231 od->od_type = doi.doi_type;
2232 od->od_blocksize = doi.doi_data_block_size;
2233 od->od_gen = bbt->bt_gen;
2234 dmu_buf_rele(db, FTAG);
2235 ztest_object_unlock(zd, od->od_object);
2236 }
2237 }
2238
2239 return (missing);
2240}
2241
2242static int
2243ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
2244{
2245 int missing = 0;
d6320ddb 2246 int i;
428870ff 2247
1e33ac1e 2248 ASSERT(mutex_held(&zd->zd_dirobj_lock));
428870ff 2249
d6320ddb 2250 for (i = 0; i < count; i++, od++) {
428870ff
BB
2251 if (missing) {
2252 od->od_object = 0;
2253 missing++;
2254 continue;
2255 }
2256
2257 lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2258
2259 lr->lr_doid = od->od_dir;
2260 lr->lr_foid = 0; /* 0 to allocate, > 0 to claim */
2261 lr->lrz_type = od->od_crtype;
2262 lr->lrz_blocksize = od->od_crblocksize;
2263 lr->lrz_ibshift = ztest_random_ibshift();
2264 lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
50c957f7 2265 lr->lrz_dnodesize = od->od_crdnodesize;
428870ff
BB
2266 lr->lr_gen = od->od_crgen;
2267 lr->lr_crtime[0] = time(NULL);
2268
2269 if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
2270 ASSERT(missing == 0);
2271 od->od_object = 0;
2272 missing++;
2273 } else {
2274 od->od_object = lr->lr_foid;
2275 od->od_type = od->od_crtype;
2276 od->od_blocksize = od->od_crblocksize;
2277 od->od_gen = od->od_crgen;
2278 ASSERT(od->od_object != 0);
2279 }
2280
2281 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2282 }
2283
2284 return (missing);
2285}
2286
2287static int
2288ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
2289{
2290 int missing = 0;
2291 int error;
d6320ddb 2292 int i;
428870ff 2293
1e33ac1e 2294 ASSERT(mutex_held(&zd->zd_dirobj_lock));
428870ff
BB
2295
2296 od += count - 1;
2297
d6320ddb 2298 for (i = count - 1; i >= 0; i--, od--) {
428870ff
BB
2299 if (missing) {
2300 missing++;
2301 continue;
2302 }
2303
03c6040b
GW
2304 /*
2305 * No object was found.
2306 */
428870ff
BB
2307 if (od->od_object == 0)
2308 continue;
2309
2310 lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2311
2312 lr->lr_doid = od->od_dir;
2313
2314 if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
2315 ASSERT3U(error, ==, ENOSPC);
2316 missing++;
2317 } else {
2318 od->od_object = 0;
2319 }
2320 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2321 }
2322
2323 return (missing);
2324}
2325
2326static int
2327ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
2328 void *data)
2329{
2330 lr_write_t *lr;
2331 int error;
2332
2333 lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
2334
2335 lr->lr_foid = object;
2336 lr->lr_offset = offset;
2337 lr->lr_length = size;
2338 lr->lr_blkoff = 0;
2339 BP_ZERO(&lr->lr_blkptr);
2340
2341 bcopy(data, lr + 1, size);
2342
2343 error = ztest_replay_write(zd, lr, B_FALSE);
2344
2345 ztest_lr_free(lr, sizeof (*lr) + size, NULL);
2346
2347 return (error);
2348}
2349
2350static int
2351ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2352{
2353 lr_truncate_t *lr;
2354 int error;
2355
2356 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2357
2358 lr->lr_foid = object;
2359 lr->lr_offset = offset;
2360 lr->lr_length = size;
2361
2362 error = ztest_replay_truncate(zd, lr, B_FALSE);
2363
2364 ztest_lr_free(lr, sizeof (*lr), NULL);
2365
2366 return (error);
2367}
2368
2369static int
2370ztest_setattr(ztest_ds_t *zd, uint64_t object)
2371{
2372 lr_setattr_t *lr;
2373 int error;
2374
2375 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2376
2377 lr->lr_foid = object;
2378 lr->lr_size = 0;
2379 lr->lr_mode = 0;
2380
2381 error = ztest_replay_setattr(zd, lr, B_FALSE);
2382
2383 ztest_lr_free(lr, sizeof (*lr), NULL);
2384
2385 return (error);
2386}
2387
2388static void
2389ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2390{
2391 objset_t *os = zd->zd_os;
2392 dmu_tx_t *tx;
2393 uint64_t txg;
e3a07cd0 2394 ztest_zrl_t *rl;
428870ff
BB
2395
2396 txg_wait_synced(dmu_objset_pool(os), 0);
2397
2398 ztest_object_lock(zd, object, RL_READER);
2399 rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
2400
2401 tx = dmu_tx_create(os);
2402
2403 dmu_tx_hold_write(tx, object, offset, size);
2404
2405 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
2406
2407 if (txg != 0) {
2408 dmu_prealloc(os, object, offset, size, tx);
2409 dmu_tx_commit(tx);
2410 txg_wait_synced(dmu_objset_pool(os), txg);
2411 } else {
2412 (void) dmu_free_long_range(os, object, offset, size);
2413 }
2414
e3a07cd0 2415 ztest_range_unlock(zd, rl);
428870ff
BB
2416 ztest_object_unlock(zd, object);
2417}
2418
2419static void
2420ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
2421{
03c6040b 2422 int err;
428870ff
BB
2423 ztest_block_tag_t wbt;
2424 dmu_object_info_t doi;
2425 enum ztest_io_type io_type;
2426 uint64_t blocksize;
2427 void *data;
2428
2429 VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
2430 blocksize = doi.doi_data_block_size;
2431 data = umem_alloc(blocksize, UMEM_NOFAIL);
2432
2433 /*
2434 * Pick an i/o type at random, biased toward writing block tags.
2435 */
2436 io_type = ztest_random(ZTEST_IO_TYPES);
2437 if (ztest_random(2) == 0)
2438 io_type = ZTEST_IO_WRITE_TAG;
2439
7809eb8b 2440 (void) rw_rdlock(&zd->zd_zilog_lock);
3e31d2b0 2441
428870ff
BB
2442 switch (io_type) {
2443
2444 case ZTEST_IO_WRITE_TAG:
50c957f7
NB
2445 ztest_bt_generate(&wbt, zd->zd_os, object, doi.doi_dnodesize,
2446 offset, 0, 0, 0);
428870ff
BB
2447 (void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
2448 break;
2449
2450 case ZTEST_IO_WRITE_PATTERN:
2451 (void) memset(data, 'a' + (object + offset) % 5, blocksize);
2452 if (ztest_random(2) == 0) {
2453 /*
2454 * Induce fletcher2 collisions to ensure that
2455 * zio_ddt_collision() detects and resolves them
2456 * when using fletcher2-verify for deduplication.
2457 */
2458 ((uint64_t *)data)[0] ^= 1ULL << 63;
2459 ((uint64_t *)data)[4] ^= 1ULL << 63;
2460 }
2461 (void) ztest_write(zd, object, offset, blocksize, data);
2462 break;
2463
2464 case ZTEST_IO_WRITE_ZEROES:
2465 bzero(data, blocksize);
2466 (void) ztest_write(zd, object, offset, blocksize, data);
2467 break;
2468
2469 case ZTEST_IO_TRUNCATE:
2470 (void) ztest_truncate(zd, object, offset, blocksize);
2471 break;
2472
2473 case ZTEST_IO_SETATTR:
2474 (void) ztest_setattr(zd, object);
2475 break;
e75c13c3
BB
2476 default:
2477 break;
03c6040b
GW
2478
2479 case ZTEST_IO_REWRITE:
7809eb8b 2480 (void) rw_rdlock(&ztest_name_lock);
03c6040b
GW
2481 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2482 ZFS_PROP_CHECKSUM, spa_dedup_checksum(ztest_spa),
2483 B_FALSE);
2484 VERIFY(err == 0 || err == ENOSPC);
2485 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2486 ZFS_PROP_COMPRESSION,
2487 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION),
2488 B_FALSE);
2489 VERIFY(err == 0 || err == ENOSPC);
7809eb8b 2490 (void) rw_unlock(&ztest_name_lock);
03c6040b
GW
2491
2492 VERIFY0(dmu_read(zd->zd_os, object, offset, blocksize, data,
2493 DMU_READ_NO_PREFETCH));
2494
2495 (void) ztest_write(zd, object, offset, blocksize, data);
2496 break;
428870ff
BB
2497 }
2498
7809eb8b 2499 (void) rw_unlock(&zd->zd_zilog_lock);
3e31d2b0 2500
428870ff
BB
2501 umem_free(data, blocksize);
2502}
2503
2504/*
2505 * Initialize an object description template.
2506 */
2507static void
2508ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
50c957f7
NB
2509 dmu_object_type_t type, uint64_t blocksize, uint64_t dnodesize,
2510 uint64_t gen)
428870ff
BB
2511{
2512 od->od_dir = ZTEST_DIROBJ;
2513 od->od_object = 0;
2514
2515 od->od_crtype = type;
2516 od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
50c957f7 2517 od->od_crdnodesize = dnodesize ? dnodesize : ztest_random_dnodesize();
428870ff
BB
2518 od->od_crgen = gen;
2519
2520 od->od_type = DMU_OT_NONE;
2521 od->od_blocksize = 0;
2522 od->od_gen = 0;
2523
2524 (void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
b8864a23 2525 tag, (longlong_t)id, (u_longlong_t)index);
428870ff
BB
2526}
2527
2528/*
2529 * Lookup or create the objects for a test using the od template.
2530 * If the objects do not all exist, or if 'remove' is specified,
2531 * remove any existing objects and create new ones. Otherwise,
2532 * use the existing objects.
2533 */
2534static int
2535ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
2536{
2537 int count = size / sizeof (*od);
2538 int rv = 0;
2539
1e33ac1e 2540 mutex_enter(&zd->zd_dirobj_lock);
428870ff
BB
2541 if ((ztest_lookup(zd, od, count) != 0 || remove) &&
2542 (ztest_remove(zd, od, count) != 0 ||
2543 ztest_create(zd, od, count) != 0))
2544 rv = -1;
2545 zd->zd_od = od;
1e33ac1e 2546 mutex_exit(&zd->zd_dirobj_lock);
428870ff
BB
2547
2548 return (rv);
2549}
2550
2551/* ARGSUSED */
2552void
2553ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
2554{
2555 zilog_t *zilog = zd->zd_zilog;
2556
7809eb8b 2557 (void) rw_rdlock(&zd->zd_zilog_lock);
3e31d2b0 2558
572e2857 2559 zil_commit(zilog, ztest_random(ZTEST_OBJECTS));
428870ff
BB
2560
2561 /*
2562 * Remember the committed values in zd, which is in parent/child
2563 * shared memory. If we die, the next iteration of ztest_run()
2564 * will verify that the log really does contain this record.
2565 */
2566 mutex_enter(&zilog->zl_lock);
c242c188
CS
2567 ASSERT(zd->zd_shared != NULL);
2568 ASSERT3U(zd->zd_shared->zd_seq, <=, zilog->zl_commit_lr_seq);
2569 zd->zd_shared->zd_seq = zilog->zl_commit_lr_seq;
428870ff 2570 mutex_exit(&zilog->zl_lock);
3e31d2b0 2571
7809eb8b 2572 (void) rw_unlock(&zd->zd_zilog_lock);
3e31d2b0
ES
2573}
2574
2575/*
2576 * This function is designed to simulate the operations that occur during a
2577 * mount/unmount operation. We hold the dataset across these operations in an
2578 * attempt to expose any implicit assumptions about ZIL management.
2579 */
2580/* ARGSUSED */
2581void
2582ztest_zil_remount(ztest_ds_t *zd, uint64_t id)
2583{
2584 objset_t *os = zd->zd_os;
2585
03c6040b
GW
2586 /*
2587 * We grab the zd_dirobj_lock to ensure that no other thread is
2588 * updating the zil (i.e. adding in-memory log records) and the
2589 * zd_zilog_lock to block any I/O.
2590 */
29809a6c 2591 mutex_enter(&zd->zd_dirobj_lock);
7809eb8b 2592 (void) rw_wrlock(&zd->zd_zilog_lock);
3e31d2b0 2593
86f35f34 2594 /* zfs_sb_teardown() */
3e31d2b0
ES
2595 zil_close(zd->zd_zilog);
2596
2597 /* zfsvfs_setup() */
2598 VERIFY(zil_open(os, ztest_get_data) == zd->zd_zilog);
2599 zil_replay(os, zd, ztest_replay_vector);
2600
7809eb8b 2601 (void) rw_unlock(&zd->zd_zilog_lock);
29809a6c 2602 mutex_exit(&zd->zd_dirobj_lock);
428870ff
BB
2603}
2604
2605/*
2606 * Verify that we can't destroy an active pool, create an existing pool,
2607 * or create a pool with a bad vdev spec.
2608 */
2609/* ARGSUSED */
2610void
2611ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2612{
c242c188 2613 ztest_shared_opts_t *zo = &ztest_opts;
428870ff
BB
2614 spa_t *spa;
2615 nvlist_t *nvroot;
2616
2617 /*
2618 * Attempt to create using a bad file.
2619 */
ea0b2538 2620 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
428870ff 2621 VERIFY3U(ENOENT, ==,
6f1ffb06 2622 spa_create("ztest_bad_file", nvroot, NULL, NULL));
428870ff
BB
2623 nvlist_free(nvroot);
2624
2625 /*
2626 * Attempt to create using a bad mirror.
2627 */
ea0b2538 2628 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 2, 1);
428870ff 2629 VERIFY3U(ENOENT, ==,
6f1ffb06 2630 spa_create("ztest_bad_mirror", nvroot, NULL, NULL));
428870ff
BB
2631 nvlist_free(nvroot);
2632
2633 /*
2634 * Attempt to create an existing pool. It shouldn't matter
2635 * what's in the nvroot; we should fail with EEXIST.
2636 */
7809eb8b 2637 (void) rw_rdlock(&ztest_name_lock);
ea0b2538 2638 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
6f1ffb06 2639 VERIFY3U(EEXIST, ==, spa_create(zo->zo_pool, nvroot, NULL, NULL));
428870ff 2640 nvlist_free(nvroot);
c242c188
CS
2641 VERIFY3U(0, ==, spa_open(zo->zo_pool, &spa, FTAG));
2642 VERIFY3U(EBUSY, ==, spa_destroy(zo->zo_pool));
428870ff
BB
2643 spa_close(spa, FTAG);
2644
7809eb8b 2645 (void) rw_unlock(&ztest_name_lock);
428870ff
BB
2646}
2647
ea0b2538
GW
2648/* ARGSUSED */
2649void
2650ztest_spa_upgrade(ztest_ds_t *zd, uint64_t id)
2651{
2652 spa_t *spa;
2653 uint64_t initial_version = SPA_VERSION_INITIAL;
2654 uint64_t version, newversion;
2655 nvlist_t *nvroot, *props;
2656 char *name;
2657
2658 mutex_enter(&ztest_vdev_lock);
2659 name = kmem_asprintf("%s_upgrade", ztest_opts.zo_pool);
2660
2661 /*
2662 * Clean up from previous runs.
2663 */
2664 (void) spa_destroy(name);
2665
2666 nvroot = make_vdev_root(NULL, NULL, name, ztest_opts.zo_vdev_size, 0,
2667 0, ztest_opts.zo_raidz, ztest_opts.zo_mirrors, 1);
2668
2669 /*
2670 * If we're configuring a RAIDZ device then make sure that the
2671 * the initial version is capable of supporting that feature.
2672 */
2673 switch (ztest_opts.zo_raidz_parity) {
2674 case 0:
2675 case 1:
2676 initial_version = SPA_VERSION_INITIAL;
2677 break;
2678 case 2:
2679 initial_version = SPA_VERSION_RAIDZ2;
2680 break;
2681 case 3:
2682 initial_version = SPA_VERSION_RAIDZ3;
2683 break;
2684 }
2685
2686 /*
2687 * Create a pool with a spa version that can be upgraded. Pick
2688 * a value between initial_version and SPA_VERSION_BEFORE_FEATURES.
2689 */
2690 do {
2691 version = ztest_random_spa_version(initial_version);
2692 } while (version > SPA_VERSION_BEFORE_FEATURES);
2693
2694 props = fnvlist_alloc();
2695 fnvlist_add_uint64(props,
2696 zpool_prop_to_name(ZPOOL_PROP_VERSION), version);
6f1ffb06 2697 VERIFY3S(spa_create(name, nvroot, props, NULL), ==, 0);
ea0b2538
GW
2698 fnvlist_free(nvroot);
2699 fnvlist_free(props);
2700
2701 VERIFY3S(spa_open(name, &spa, FTAG), ==, 0);
2702 VERIFY3U(spa_version(spa), ==, version);
2703 newversion = ztest_random_spa_version(version + 1);
2704
2705 if (ztest_opts.zo_verbose >= 4) {
2706 (void) printf("upgrading spa version from %llu to %llu\n",
2707 (u_longlong_t)version, (u_longlong_t)newversion);
2708 }
2709
2710 spa_upgrade(spa, newversion);
2711 VERIFY3U(spa_version(spa), >, version);
2712 VERIFY3U(spa_version(spa), ==, fnvlist_lookup_uint64(spa->spa_config,
2713 zpool_prop_to_name(ZPOOL_PROP_VERSION)));
2714 spa_close(spa, FTAG);
2715
2716 strfree(name);
2717 mutex_exit(&ztest_vdev_lock);
2718}
2719
428870ff
BB
2720static vdev_t *
2721vdev_lookup_by_path(vdev_t *vd, const char *path)
2722{
2723 vdev_t *mvd;
d6320ddb 2724 int c;
428870ff
BB
2725
2726 if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
2727 return (vd);
2728
d6320ddb 2729 for (c = 0; c < vd->vdev_children; c++)
428870ff
BB
2730 if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
2731 NULL)
2732 return (mvd);
2733
2734 return (NULL);
2735}
2736
2737/*
2738 * Find the first available hole which can be used as a top-level.
2739 */
2740int
2741find_vdev_hole(spa_t *spa)
2742{
2743 vdev_t *rvd = spa->spa_root_vdev;
2744 int c;
2745
2746 ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
2747
2748 for (c = 0; c < rvd->vdev_children; c++) {
2749 vdev_t *cvd = rvd->vdev_child[c];
2750
2751 if (cvd->vdev_ishole)
2752 break;
2753 }
2754 return (c);
2755}
2756
2757/*
2758 * Verify that vdev_add() works as expected.
2759 */
2760/* ARGSUSED */
2761void
2762ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2763{
2764 ztest_shared_t *zs = ztest_shared;
c242c188 2765 spa_t *spa = ztest_spa;
428870ff
BB
2766 uint64_t leaves;
2767 uint64_t guid;
2768 nvlist_t *nvroot;
2769 int error;
2770
c242c188 2771 mutex_enter(&ztest_vdev_lock);
13fe0198 2772 leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * ztest_opts.zo_raidz;
428870ff
BB
2773
2774 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2775
2776 ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2777
2778 /*
2779 * If we have slogs then remove them 1/4 of the time.
2780 */
2781 if (spa_has_slogs(spa) && ztest_random(4) == 0) {
2782 /*
2783 * Grab the guid from the head of the log class rotor.
2784 */
2785 guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid;
2786
2787 spa_config_exit(spa, SCL_VDEV, FTAG);
2788
2789 /*
2790 * We have to grab the zs_name_lock as writer to
2791 * prevent a race between removing a slog (dmu_objset_find)
2792 * and destroying a dataset. Removing the slog will
2793 * grab a reference on the dataset which may cause
13fe0198 2794 * dsl_destroy_head() to fail with EBUSY thus
428870ff
BB
2795 * leaving the dataset in an inconsistent state.
2796 */
7809eb8b 2797 rw_wrlock(&ztest_name_lock);
428870ff 2798 error = spa_vdev_remove(spa, guid, B_FALSE);
7809eb8b 2799 rw_unlock(&ztest_name_lock);
428870ff
BB
2800
2801 if (error && error != EEXIST)
2802 fatal(0, "spa_vdev_remove() = %d", error);
2803 } else {
2804 spa_config_exit(spa, SCL_VDEV, FTAG);
2805
2806 /*
2807 * Make 1/4 of the devices be log devices.
2808 */
ea0b2538 2809 nvroot = make_vdev_root(NULL, NULL, NULL,
c242c188
CS
2810 ztest_opts.zo_vdev_size, 0,
2811 ztest_random(4) == 0, ztest_opts.zo_raidz,
2812 zs->zs_mirrors, 1);
428870ff
BB
2813
2814 error = spa_vdev_add(spa, nvroot);
2815 nvlist_free(nvroot);
2816
2817 if (error == ENOSPC)
2818 ztest_record_enospc("spa_vdev_add");
2819 else if (error != 0)
2820 fatal(0, "spa_vdev_add() = %d", error);
2821 }
2822
c242c188 2823 mutex_exit(&ztest_vdev_lock);
428870ff
BB
2824}
2825
2826/*
2827 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
2828 */
2829/* ARGSUSED */
2830void
2831ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
2832{
2833 ztest_shared_t *zs = ztest_shared;
c242c188 2834 spa_t *spa = ztest_spa;
428870ff
BB
2835 vdev_t *rvd = spa->spa_root_vdev;
2836 spa_aux_vdev_t *sav;
2837 char *aux;
40b84e7a 2838 char *path;
428870ff
BB
2839 uint64_t guid = 0;
2840 int error;
2841
40b84e7a
BB
2842 path = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2843
428870ff
BB
2844 if (ztest_random(2) == 0) {
2845 sav = &spa->spa_spares;
2846 aux = ZPOOL_CONFIG_SPARES;
2847 } else {
2848 sav = &spa->spa_l2cache;
2849 aux = ZPOOL_CONFIG_L2CACHE;
2850 }
2851
c242c188 2852 mutex_enter(&ztest_vdev_lock);
428870ff
BB
2853
2854 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2855
2856 if (sav->sav_count != 0 && ztest_random(4) == 0) {
b128c09f
BB
2857 /*
2858 * Pick a random device to remove.
2859 */
2860 guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
2861 } else {
2862 /*
2863 * Find an unused device we can add.
2864 */
428870ff 2865 zs->zs_vdev_aux = 0;
b128c09f 2866 for (;;) {
b128c09f 2867 int c;
3db3ff4a 2868 (void) snprintf(path, MAXPATHLEN, ztest_aux_template,
c242c188
CS
2869 ztest_opts.zo_dir, ztest_opts.zo_pool, aux,
2870 zs->zs_vdev_aux);
b128c09f
BB
2871 for (c = 0; c < sav->sav_count; c++)
2872 if (strcmp(sav->sav_vdevs[c]->vdev_path,
2873 path) == 0)
2874 break;
2875 if (c == sav->sav_count &&
2876 vdev_lookup_by_path(rvd, path) == NULL)
2877 break;
428870ff 2878 zs->zs_vdev_aux++;
34dc7c2f
BB
2879 }
2880 }
2881
b128c09f 2882 spa_config_exit(spa, SCL_VDEV, FTAG);
34dc7c2f 2883
b128c09f
BB
2884 if (guid == 0) {
2885 /*
2886 * Add a new device.
2887 */
ea0b2538 2888 nvlist_t *nvroot = make_vdev_root(NULL, aux, NULL,
c242c188 2889 (ztest_opts.zo_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
b128c09f
BB
2890 error = spa_vdev_add(spa, nvroot);
2891 if (error != 0)
2892 fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
2893 nvlist_free(nvroot);
2894 } else {
2895 /*
2896 * Remove an existing device. Sometimes, dirty its
2897 * vdev state first to make sure we handle removal
2898 * of devices that have pending state changes.
2899 */
2900 if (ztest_random(2) == 0)
9babb374 2901 (void) vdev_online(spa, guid, 0, NULL);
b128c09f
BB
2902
2903 error = spa_vdev_remove(spa, guid, B_FALSE);
2904 if (error != 0 && error != EBUSY)
2905 fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
2906 }
2907
c242c188 2908 mutex_exit(&ztest_vdev_lock);
40b84e7a
BB
2909
2910 umem_free(path, MAXPATHLEN);
428870ff
BB
2911}
2912
2913/*
2914 * split a pool if it has mirror tlvdevs
2915 */
2916/* ARGSUSED */
2917void
2918ztest_split_pool(ztest_ds_t *zd, uint64_t id)
2919{
2920 ztest_shared_t *zs = ztest_shared;
c242c188 2921 spa_t *spa = ztest_spa;
428870ff
BB
2922 vdev_t *rvd = spa->spa_root_vdev;
2923 nvlist_t *tree, **child, *config, *split, **schild;
2924 uint_t c, children, schildren = 0, lastlogid = 0;
2925 int error = 0;
2926
c242c188 2927 mutex_enter(&ztest_vdev_lock);
428870ff
BB
2928
2929 /* ensure we have a useable config; mirrors of raidz aren't supported */
c242c188
CS
2930 if (zs->zs_mirrors < 3 || ztest_opts.zo_raidz > 1) {
2931 mutex_exit(&ztest_vdev_lock);
428870ff
BB
2932 return;
2933 }
2934
2935 /* clean up the old pool, if any */
2936 (void) spa_destroy("splitp");
2937
2938 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2939
2940 /* generate a config from the existing config */
2941 mutex_enter(&spa->spa_props_lock);
2942 VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
2943 &tree) == 0);
2944 mutex_exit(&spa->spa_props_lock);
2945
2946 VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2947 &children) == 0);
2948
2949 schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
2950 for (c = 0; c < children; c++) {
2951 vdev_t *tvd = rvd->vdev_child[c];
2952 nvlist_t **mchild;
2953 uint_t mchildren;
2954
2955 if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
2956 VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
2957 0) == 0);
2958 VERIFY(nvlist_add_string(schild[schildren],
2959 ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
2960 VERIFY(nvlist_add_uint64(schild[schildren],
2961 ZPOOL_CONFIG_IS_HOLE, 1) == 0);
2962 if (lastlogid == 0)
2963 lastlogid = schildren;
2964 ++schildren;
2965 continue;
2966 }
2967 lastlogid = 0;
2968 VERIFY(nvlist_lookup_nvlist_array(child[c],
2969 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2970 VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
2971 }
2972
2973 /* OK, create a config that can be used to split */
2974 VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
2975 VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
2976 VDEV_TYPE_ROOT) == 0);
2977 VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
2978 lastlogid != 0 ? lastlogid : schildren) == 0);
2979
2980 VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
2981 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
2982
2983 for (c = 0; c < schildren; c++)
2984 nvlist_free(schild[c]);
2985 free(schild);
2986 nvlist_free(split);
2987
2988 spa_config_exit(spa, SCL_VDEV, FTAG);
2989
7809eb8b 2990 (void) rw_wrlock(&ztest_name_lock);
428870ff 2991 error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
7809eb8b 2992 (void) rw_unlock(&ztest_name_lock);
428870ff
BB
2993
2994 nvlist_free(config);
2995
2996 if (error == 0) {
2997 (void) printf("successful split - results:\n");
2998 mutex_enter(&spa_namespace_lock);
2999 show_pool_stats(spa);
3000 show_pool_stats(spa_lookup("splitp"));
3001 mutex_exit(&spa_namespace_lock);
3002 ++zs->zs_splits;
3003 --zs->zs_mirrors;
3004 }
c242c188 3005 mutex_exit(&ztest_vdev_lock);
428870ff 3006
34dc7c2f
BB
3007}
3008
3009/*
3010 * Verify that we can attach and detach devices.
3011 */
428870ff 3012/* ARGSUSED */
34dc7c2f 3013void
428870ff 3014ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
34dc7c2f 3015{
428870ff 3016 ztest_shared_t *zs = ztest_shared;
c242c188 3017 spa_t *spa = ztest_spa;
b128c09f 3018 spa_aux_vdev_t *sav = &spa->spa_spares;
34dc7c2f
BB
3019 vdev_t *rvd = spa->spa_root_vdev;
3020 vdev_t *oldvd, *newvd, *pvd;
b128c09f 3021 nvlist_t *root;
428870ff 3022 uint64_t leaves;
34dc7c2f
BB
3023 uint64_t leaf, top;
3024 uint64_t ashift = ztest_get_ashift();
fb5f0bc8 3025 uint64_t oldguid, pguid;
5d1f7fb6 3026 uint64_t oldsize, newsize;
40b84e7a 3027 char *oldpath, *newpath;
34dc7c2f 3028 int replacing;
b128c09f
BB
3029 int oldvd_has_siblings = B_FALSE;
3030 int newvd_is_spare = B_FALSE;
3031 int oldvd_is_log;
34dc7c2f 3032 int error, expected_error;
34dc7c2f 3033
40b84e7a
BB
3034 oldpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
3035 newpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
3036
c242c188
CS
3037 mutex_enter(&ztest_vdev_lock);
3038 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
34dc7c2f 3039
b128c09f 3040 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
34dc7c2f
BB
3041
3042 /*
3043 * Decide whether to do an attach or a replace.
3044 */
3045 replacing = ztest_random(2);
3046
3047 /*
3048 * Pick a random top-level vdev.
3049 */
428870ff 3050 top = ztest_random_vdev_top(spa, B_TRUE);
34dc7c2f
BB
3051
3052 /*
3053 * Pick a random leaf within it.
3054 */
3055 leaf = ztest_random(leaves);
3056
3057 /*
b128c09f 3058 * Locate this vdev.
34dc7c2f 3059 */
b128c09f 3060 oldvd = rvd->vdev_child[top];
428870ff 3061 if (zs->zs_mirrors >= 1) {
fb5f0bc8 3062 ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
428870ff 3063 ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
c242c188 3064 oldvd = oldvd->vdev_child[leaf / ztest_opts.zo_raidz];
fb5f0bc8 3065 }
c242c188 3066 if (ztest_opts.zo_raidz > 1) {
fb5f0bc8 3067 ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
c242c188
CS
3068 ASSERT(oldvd->vdev_children == ztest_opts.zo_raidz);
3069 oldvd = oldvd->vdev_child[leaf % ztest_opts.zo_raidz];
fb5f0bc8 3070 }
34dc7c2f
BB
3071
3072 /*
b128c09f
BB
3073 * If we're already doing an attach or replace, oldvd may be a
3074 * mirror vdev -- in which case, pick a random child.
34dc7c2f 3075 */
b128c09f
BB
3076 while (oldvd->vdev_children != 0) {
3077 oldvd_has_siblings = B_TRUE;
fb5f0bc8
BB
3078 ASSERT(oldvd->vdev_children >= 2);
3079 oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
b128c09f
BB
3080 }
3081
3082 oldguid = oldvd->vdev_guid;
9babb374 3083 oldsize = vdev_get_min_asize(oldvd);
b128c09f
BB
3084 oldvd_is_log = oldvd->vdev_top->vdev_islog;
3085 (void) strcpy(oldpath, oldvd->vdev_path);
3086 pvd = oldvd->vdev_parent;
fb5f0bc8 3087 pguid = pvd->vdev_guid;
34dc7c2f
BB
3088
3089 /*
b128c09f 3090 * If oldvd has siblings, then half of the time, detach it.
34dc7c2f 3091 */
b128c09f
BB
3092 if (oldvd_has_siblings && ztest_random(2) == 0) {
3093 spa_config_exit(spa, SCL_VDEV, FTAG);
fb5f0bc8
BB
3094 error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
3095 if (error != 0 && error != ENODEV && error != EBUSY &&
3096 error != ENOTSUP)
3097 fatal(0, "detach (%s) returned %d", oldpath, error);
40b84e7a 3098 goto out;
b128c09f 3099 }
34dc7c2f
BB
3100
3101 /*
b128c09f
BB
3102 * For the new vdev, choose with equal probability between the two
3103 * standard paths (ending in either 'a' or 'b') or a random hot spare.
34dc7c2f 3104 */
b128c09f
BB
3105 if (sav->sav_count != 0 && ztest_random(3) == 0) {
3106 newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
3107 newvd_is_spare = B_TRUE;
3108 (void) strcpy(newpath, newvd->vdev_path);
3109 } else {
6aec1cd5 3110 (void) snprintf(newpath, MAXPATHLEN, ztest_dev_template,
c242c188
CS
3111 ztest_opts.zo_dir, ztest_opts.zo_pool,
3112 top * leaves + leaf);
b128c09f
BB
3113 if (ztest_random(2) == 0)
3114 newpath[strlen(newpath) - 1] = 'b';
3115 newvd = vdev_lookup_by_path(rvd, newpath);
3116 }
3117
3118 if (newvd) {
9babb374 3119 newsize = vdev_get_min_asize(newvd);
b128c09f
BB
3120 } else {
3121 /*
3122 * Make newsize a little bigger or smaller than oldsize.
3123 * If it's smaller, the attach should fail.
3124 * If it's larger, and we're doing a replace,
3125 * we should get dynamic LUN growth when we're done.
3126 */
3127 newsize = 10 * oldsize / (9 + ztest_random(3));
3128 }
34dc7c2f
BB
3129
3130 /*
3131 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
3132 * unless it's a replace; in that case any non-replacing parent is OK.
3133 *
3134 * If newvd is already part of the pool, it should fail with EBUSY.
3135 *
3136 * If newvd is too small, it should fail with EOVERFLOW.
3137 */
b128c09f
BB
3138 if (pvd->vdev_ops != &vdev_mirror_ops &&
3139 pvd->vdev_ops != &vdev_root_ops && (!replacing ||
3140 pvd->vdev_ops == &vdev_replacing_ops ||
3141 pvd->vdev_ops == &vdev_spare_ops))
34dc7c2f 3142 expected_error = ENOTSUP;
b128c09f
BB
3143 else if (newvd_is_spare && (!replacing || oldvd_is_log))
3144 expected_error = ENOTSUP;
3145 else if (newvd == oldvd)
3146 expected_error = replacing ? 0 : EBUSY;
3147 else if (vdev_lookup_by_path(rvd, newpath) != NULL)
3148 expected_error = EBUSY;
34dc7c2f
BB
3149 else if (newsize < oldsize)
3150 expected_error = EOVERFLOW;
3151 else if (ashift > oldvd->vdev_top->vdev_ashift)
3152 expected_error = EDOM;
3153 else
3154 expected_error = 0;
3155
b128c09f 3156 spa_config_exit(spa, SCL_VDEV, FTAG);
34dc7c2f
BB
3157
3158 /*
3159 * Build the nvlist describing newpath.
3160 */
ea0b2538 3161 root = make_vdev_root(newpath, NULL, NULL, newvd == NULL ? newsize : 0,
b128c09f 3162 ashift, 0, 0, 0, 1);
34dc7c2f 3163
b128c09f 3164 error = spa_vdev_attach(spa, oldguid, root, replacing);
34dc7c2f 3165
34dc7c2f
BB
3166 nvlist_free(root);
3167
3168 /*
3169 * If our parent was the replacing vdev, but the replace completed,
3170 * then instead of failing with ENOTSUP we may either succeed,
3171 * fail with ENODEV, or fail with EOVERFLOW.
3172 */
3173 if (expected_error == ENOTSUP &&
3174 (error == 0 || error == ENODEV || error == EOVERFLOW))
3175 expected_error = error;
3176
3177 /*
3178 * If someone grew the LUN, the replacement may be too small.
3179 */
b128c09f 3180 if (error == EOVERFLOW || error == EBUSY)
34dc7c2f
BB
3181 expected_error = error;
3182
b128c09f
BB
3183 /* XXX workaround 6690467 */
3184 if (error != expected_error && expected_error != EBUSY) {
3185 fatal(0, "attach (%s %llu, %s %llu, %d) "
3186 "returned %d, expected %d",
5d1f7fb6
GW
3187 oldpath, oldsize, newpath,
3188 newsize, replacing, error, expected_error);
34dc7c2f 3189 }
40b84e7a 3190out:
c242c188 3191 mutex_exit(&ztest_vdev_lock);
40b84e7a
BB
3192
3193 umem_free(oldpath, MAXPATHLEN);
3194 umem_free(newpath, MAXPATHLEN);
34dc7c2f
BB
3195}
3196
9babb374
BB
3197/*
3198 * Callback function which expands the physical size of the vdev.
3199 */
3200vdev_t *
3201grow_vdev(vdev_t *vd, void *arg)
3202{
1fde1e37 3203 ASSERTV(spa_t *spa = vd->vdev_spa);
9babb374
BB
3204 size_t *newsize = arg;
3205 size_t fsize;
3206 int fd;
3207
3208 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3209 ASSERT(vd->vdev_ops->vdev_op_leaf);
3210
3211 if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
3212 return (vd);
3213
3214 fsize = lseek(fd, 0, SEEK_END);
0e5b68e0 3215 VERIFY(ftruncate(fd, *newsize) == 0);
9babb374 3216
c242c188 3217 if (ztest_opts.zo_verbose >= 6) {
9babb374
BB
3218 (void) printf("%s grew from %lu to %lu bytes\n",
3219 vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
3220 }
3221 (void) close(fd);
3222 return (NULL);
3223}
3224
3225/*
3226 * Callback function which expands a given vdev by calling vdev_online().
3227 */
3228/* ARGSUSED */
3229vdev_t *
3230online_vdev(vdev_t *vd, void *arg)
3231{
3232 spa_t *spa = vd->vdev_spa;
3233 vdev_t *tvd = vd->vdev_top;
9babb374 3234 uint64_t guid = vd->vdev_guid;
428870ff
BB
3235 uint64_t generation = spa->spa_config_generation + 1;
3236 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
3237 int error;
9babb374
BB
3238
3239 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3240 ASSERT(vd->vdev_ops->vdev_op_leaf);
3241
3242 /* Calling vdev_online will initialize the new metaslabs */
3243 spa_config_exit(spa, SCL_STATE, spa);
428870ff 3244 error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
9babb374
BB
3245 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3246
428870ff
BB
3247 /*
3248 * If vdev_online returned an error or the underlying vdev_open
3249 * failed then we abort the expand. The only way to know that
3250 * vdev_open fails is by checking the returned newstate.
3251 */
3252 if (error || newstate != VDEV_STATE_HEALTHY) {
c242c188 3253 if (ztest_opts.zo_verbose >= 5) {
428870ff
BB
3254 (void) printf("Unable to expand vdev, state %llu, "
3255 "error %d\n", (u_longlong_t)newstate, error);
3256 }
3257 return (vd);
3258 }
3259 ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
3260
9babb374
BB
3261 /*
3262 * Since we dropped the lock we need to ensure that we're
3263 * still talking to the original vdev. It's possible this
3264 * vdev may have been detached/replaced while we were
3265 * trying to online it.
3266 */
428870ff 3267 if (generation != spa->spa_config_generation) {
c242c188 3268 if (ztest_opts.zo_verbose >= 5) {
428870ff
BB
3269 (void) printf("vdev configuration has changed, "
3270 "guid %llu, state %llu, expected gen %llu, "
3271 "got gen %llu\n",
3272 (u_longlong_t)guid,
3273 (u_longlong_t)tvd->vdev_state,
3274 (u_longlong_t)generation,
3275 (u_longlong_t)spa->spa_config_generation);
9babb374
BB
3276 }
3277 return (vd);
3278 }
3279 return (NULL);
3280}
3281
3282/*
3283 * Traverse the vdev tree calling the supplied function.
3284 * We continue to walk the tree until we either have walked all
3285 * children or we receive a non-NULL return from the callback.
3286 * If a NULL callback is passed, then we just return back the first
3287 * leaf vdev we encounter.
3288 */
3289vdev_t *
3290vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
3291{
d6320ddb
BB
3292 uint_t c;
3293
9babb374
BB
3294 if (vd->vdev_ops->vdev_op_leaf) {
3295 if (func == NULL)
3296 return (vd);
3297 else
3298 return (func(vd, arg));
3299 }
3300
d6320ddb 3301 for (c = 0; c < vd->vdev_children; c++) {
9babb374
BB
3302 vdev_t *cvd = vd->vdev_child[c];
3303 if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
3304 return (cvd);
3305 }
3306 return (NULL);
3307}
3308
34dc7c2f
BB
3309/*
3310 * Verify that dynamic LUN growth works as expected.
3311 */
428870ff 3312/* ARGSUSED */
34dc7c2f 3313void
428870ff 3314ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
34dc7c2f 3315{
c242c188 3316 spa_t *spa = ztest_spa;
428870ff
BB
3317 vdev_t *vd, *tvd;
3318 metaslab_class_t *mc;
3319 metaslab_group_t *mg;
9babb374 3320 size_t psize, newsize;
428870ff
BB
3321 uint64_t top;
3322 uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
34dc7c2f 3323
c242c188 3324 mutex_enter(&ztest_vdev_lock);
9babb374
BB
3325 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3326
428870ff 3327 top = ztest_random_vdev_top(spa, B_TRUE);
9babb374 3328
428870ff
BB
3329 tvd = spa->spa_root_vdev->vdev_child[top];
3330 mg = tvd->vdev_mg;
3331 mc = mg->mg_class;
3332 old_ms_count = tvd->vdev_ms_count;
3333 old_class_space = metaslab_class_get_space(mc);
34dc7c2f
BB
3334
3335 /*
9babb374
BB
3336 * Determine the size of the first leaf vdev associated with
3337 * our top-level device.
34dc7c2f 3338 */
9babb374
BB
3339 vd = vdev_walk_tree(tvd, NULL, NULL);
3340 ASSERT3P(vd, !=, NULL);
3341 ASSERT(vd->vdev_ops->vdev_op_leaf);
34dc7c2f 3342
9babb374 3343 psize = vd->vdev_psize;
34dc7c2f 3344
9babb374 3345 /*
428870ff
BB
3346 * We only try to expand the vdev if it's healthy, less than 4x its
3347 * original size, and it has a valid psize.
9babb374 3348 */
428870ff 3349 if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
c242c188 3350 psize == 0 || psize >= 4 * ztest_opts.zo_vdev_size) {
9babb374 3351 spa_config_exit(spa, SCL_STATE, spa);
c242c188 3352 mutex_exit(&ztest_vdev_lock);
9babb374
BB
3353 return;
3354 }
3355 ASSERT(psize > 0);
3356 newsize = psize + psize / 8;
3357 ASSERT3U(newsize, >, psize);
34dc7c2f 3358
c242c188 3359 if (ztest_opts.zo_verbose >= 6) {
428870ff 3360 (void) printf("Expanding LUN %s from %lu to %lu\n",
9babb374
BB
3361 vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
3362 }
3363
9babb374
BB
3364 /*
3365 * Growing the vdev is a two step process:
3366 * 1). expand the physical size (i.e. relabel)
3367 * 2). online the vdev to create the new metaslabs
3368 */
3369 if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
3370 vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
3371 tvd->vdev_state != VDEV_STATE_HEALTHY) {
c242c188 3372 if (ztest_opts.zo_verbose >= 5) {
9babb374 3373 (void) printf("Could not expand LUN because "
428870ff 3374 "the vdev configuration changed.\n");
34dc7c2f 3375 }
428870ff 3376 spa_config_exit(spa, SCL_STATE, spa);
c242c188 3377 mutex_exit(&ztest_vdev_lock);
9babb374 3378 return;
34dc7c2f
BB
3379 }
3380
428870ff 3381 spa_config_exit(spa, SCL_STATE, spa);
9babb374
BB
3382
3383 /*
3384 * Expanding the LUN will update the config asynchronously,
3385 * thus we must wait for the async thread to complete any
3386 * pending tasks before proceeding.
3387 */
428870ff
BB
3388 for (;;) {
3389 boolean_t done;
3390 mutex_enter(&spa->spa_async_lock);
3391 done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
3392 mutex_exit(&spa->spa_async_lock);
3393 if (done)
3394 break;
3395 txg_wait_synced(spa_get_dsl(spa), 0);
3396 (void) poll(NULL, 0, 100);
3397 }
9babb374
BB
3398
3399 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
428870ff
BB
3400
3401 tvd = spa->spa_root_vdev->vdev_child[top];
3402 new_ms_count = tvd->vdev_ms_count;
3403 new_class_space = metaslab_class_get_space(mc);
3404
3405 if (tvd->vdev_mg != mg || mg->mg_class != mc) {
c242c188 3406 if (ztest_opts.zo_verbose >= 5) {
428870ff
BB
3407 (void) printf("Could not verify LUN expansion due to "
3408 "intervening vdev offline or remove.\n");
3409 }
3410 spa_config_exit(spa, SCL_STATE, spa);
c242c188 3411 mutex_exit(&ztest_vdev_lock);
428870ff
BB
3412 return;
3413 }
3414
3415 /*
3416 * Make sure we were able to grow the vdev.
3417 */
3418 if (new_ms_count <= old_ms_count)
3419 fatal(0, "LUN expansion failed: ms_count %llu <= %llu\n",
3420 old_ms_count, new_ms_count);
9babb374
BB
3421
3422 /*
3423 * Make sure we were able to grow the pool.
3424 */
428870ff
BB
3425 if (new_class_space <= old_class_space)
3426 fatal(0, "LUN expansion failed: class_space %llu <= %llu\n",
3427 old_class_space, new_class_space);
3428
c242c188 3429 if (ztest_opts.zo_verbose >= 5) {
9babb374
BB
3430 char oldnumbuf[6], newnumbuf[6];
3431
428870ff
BB
3432 nicenum(old_class_space, oldnumbuf);
3433 nicenum(new_class_space, newnumbuf);
9babb374
BB
3434 (void) printf("%s grew from %s to %s\n",
3435 spa->spa_name, oldnumbuf, newnumbuf);
3436 }
428870ff 3437
9babb374 3438 spa_config_exit(spa, SCL_STATE, spa);
c242c188 3439 mutex_exit(&ztest_vdev_lock);
34dc7c2f
BB
3440}
3441
428870ff
BB
3442/*
3443 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
3444 */
34dc7c2f
BB
3445/* ARGSUSED */
3446static void
428870ff 3447ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
34dc7c2f
BB
3448{
3449 /*
428870ff 3450 * Create the objects common to all ztest datasets.
34dc7c2f 3451 */
428870ff 3452 VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
34dc7c2f 3453 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
428870ff 3454}
34dc7c2f 3455
428870ff
BB
3456static int
3457ztest_dataset_create(char *dsname)
3458{
3459 uint64_t zilset = ztest_random(100);
3460 int err = dmu_objset_create(dsname, DMU_OST_OTHER, 0,
3461 ztest_objset_create_cb, NULL);
3462
3463 if (err || zilset < 80)
3464 return (err);
3465
c242c188 3466 if (ztest_opts.zo_verbose >= 5)
b815ff9a 3467 (void) printf("Setting dataset %s to sync always\n", dsname);
428870ff
BB
3468 return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC,
3469 ZFS_SYNC_ALWAYS, B_FALSE));
34dc7c2f
BB
3470}
3471
428870ff 3472/* ARGSUSED */
34dc7c2f 3473static int
428870ff 3474ztest_objset_destroy_cb(const char *name, void *arg)
34dc7c2f 3475{
34dc7c2f 3476 objset_t *os;
428870ff 3477 dmu_object_info_t doi;
34dc7c2f
BB
3478 int error;
3479
3480 /*
3481 * Verify that the dataset contains a directory object.
3482 */
13fe0198 3483 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_TRUE, FTAG, &os));
428870ff 3484 error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
34dc7c2f
BB
3485 if (error != ENOENT) {
3486 /* We could have crashed in the middle of destroying it */
c99c9001 3487 ASSERT0(error);
428870ff
BB
3488 ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
3489 ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
34dc7c2f 3490 }
13fe0198 3491 dmu_objset_disown(os, FTAG);
34dc7c2f
BB
3492
3493 /*
3494 * Destroy the dataset.
3495 */
13fe0198
MA
3496 if (strchr(name, '@') != NULL) {
3497 VERIFY0(dsl_destroy_snapshot(name, B_FALSE));
3498 } else {
3499 VERIFY0(dsl_destroy_head(name));
3500 }
34dc7c2f
BB
3501 return (0);
3502}
3503
428870ff
BB
3504static boolean_t
3505ztest_snapshot_create(char *osname, uint64_t id)
34dc7c2f 3506{
428870ff
BB
3507 char snapname[MAXNAMELEN];
3508 int error;
3509
13fe0198 3510 (void) snprintf(snapname, sizeof (snapname), "%llu", (u_longlong_t)id);
428870ff 3511
13fe0198 3512 error = dmu_objset_snapshot_one(osname, snapname);
428870ff
BB
3513 if (error == ENOSPC) {
3514 ztest_record_enospc(FTAG);
3515 return (B_FALSE);
3516 }
13fe0198
MA
3517 if (error != 0 && error != EEXIST) {
3518 fatal(0, "ztest_snapshot_create(%s@%s) = %d", osname,
3519 snapname, error);
3520 }
428870ff
BB
3521 return (B_TRUE);
3522}
3523
3524static boolean_t
3525ztest_snapshot_destroy(char *osname, uint64_t id)
3526{
3527 char snapname[MAXNAMELEN];
3528 int error;
3529
3530 (void) snprintf(snapname, MAXNAMELEN, "%s@%llu", osname,
3531 (u_longlong_t)id);
3532
13fe0198 3533 error = dsl_destroy_snapshot(snapname, B_FALSE);
428870ff
BB
3534 if (error != 0 && error != ENOENT)
3535 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
3536 return (B_TRUE);
34dc7c2f
BB
3537}
3538
428870ff 3539/* ARGSUSED */
34dc7c2f 3540void
428870ff 3541ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
34dc7c2f 3542{
40b84e7a 3543 ztest_ds_t *zdtmp;
428870ff 3544 int iters;
34dc7c2f 3545 int error;
b128c09f 3546 objset_t *os, *os2;
40b84e7a 3547 char *name;
34dc7c2f 3548 zilog_t *zilog;
d6320ddb 3549 int i;
34dc7c2f 3550
40b84e7a
BB
3551 zdtmp = umem_alloc(sizeof (ztest_ds_t), UMEM_NOFAIL);
3552 name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3553
7809eb8b 3554 (void) rw_rdlock(&ztest_name_lock);
34dc7c2f 3555
428870ff 3556 (void) snprintf(name, MAXNAMELEN, "%s/temp_%llu",
c242c188 3557 ztest_opts.zo_pool, (u_longlong_t)id);
34dc7c2f
BB
3558
3559 /*
3560 * If this dataset exists from a previous run, process its replay log
13fe0198 3561 * half of the time. If we don't replay it, then dsl_destroy_head()
428870ff 3562 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
34dc7c2f
BB
3563 */
3564 if (ztest_random(2) == 0 &&
428870ff 3565 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) {
c242c188 3566 ztest_zd_init(zdtmp, NULL, os);
40b84e7a
BB
3567 zil_replay(os, zdtmp, ztest_replay_vector);
3568 ztest_zd_fini(zdtmp);
428870ff 3569 dmu_objset_disown(os, FTAG);
34dc7c2f
BB
3570 }
3571
3572 /*
3573 * There may be an old instance of the dataset we're about to
3574 * create lying around from a previous run. If so, destroy it
3575 * and all of its snapshots.
3576 */
428870ff 3577 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
34dc7c2f
BB
3578 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
3579
3580 /*
3581 * Verify that the destroyed dataset is no longer in the namespace.
3582 */
13fe0198
MA
3583 VERIFY3U(ENOENT, ==, dmu_objset_own(name, DMU_OST_OTHER, B_TRUE,
3584 FTAG, &os));
34dc7c2f
BB
3585
3586 /*
3587 * Verify that we can create a new dataset.
3588 */
428870ff 3589 error = ztest_dataset_create(name);
34dc7c2f
BB
3590 if (error) {
3591 if (error == ENOSPC) {
428870ff 3592 ztest_record_enospc(FTAG);
40b84e7a 3593 goto out;
34dc7c2f
BB
3594 }
3595 fatal(0, "dmu_objset_create(%s) = %d", name, error);
3596 }
3597
13fe0198 3598 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os));
428870ff 3599
c242c188 3600 ztest_zd_init(zdtmp, NULL, os);
34dc7c2f
BB
3601
3602 /*
3603 * Open the intent log for it.
3604 */
428870ff 3605 zilog = zil_open(os, ztest_get_data);
34dc7c2f
BB
3606
3607 /*
428870ff
BB
3608 * Put some objects in there, do a little I/O to them,
3609 * and randomly take a couple of snapshots along the way.
34dc7c2f 3610 */
428870ff 3611 iters = ztest_random(5);
d6320ddb 3612 for (i = 0; i < iters; i++) {
40b84e7a 3613 ztest_dmu_object_alloc_free(zdtmp, id);
428870ff
BB
3614 if (ztest_random(iters) == 0)
3615 (void) ztest_snapshot_create(name, i);
34dc7c2f
BB
3616 }
3617
3618 /*
3619 * Verify that we cannot create an existing dataset.
3620 */
428870ff
BB
3621 VERIFY3U(EEXIST, ==,
3622 dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL));
34dc7c2f
BB
3623
3624 /*
428870ff 3625 * Verify that we can hold an objset that is also owned.
b128c09f 3626 */
428870ff
BB
3627 VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
3628 dmu_objset_rele(os2, FTAG);
34dc7c2f 3629
428870ff
BB
3630 /*
3631 * Verify that we cannot own an objset that is already owned.
3632 */
3633 VERIFY3U(EBUSY, ==,
3634 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2));
34dc7c2f 3635
428870ff
BB
3636 zil_close(zilog);
3637 dmu_objset_disown(os, FTAG);
40b84e7a
BB
3638 ztest_zd_fini(zdtmp);
3639out:
7809eb8b 3640 (void) rw_unlock(&ztest_name_lock);
40b84e7a
BB
3641
3642 umem_free(name, MAXNAMELEN);
3643 umem_free(zdtmp, sizeof (ztest_ds_t));
34dc7c2f
BB
3644}
3645
3646/*
3647 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3648 */
3649void
428870ff 3650ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
34dc7c2f 3651{
7809eb8b 3652 (void) rw_rdlock(&ztest_name_lock);
428870ff
BB
3653 (void) ztest_snapshot_destroy(zd->zd_name, id);
3654 (void) ztest_snapshot_create(zd->zd_name, id);
7809eb8b 3655 (void) rw_unlock(&ztest_name_lock);
34dc7c2f
BB
3656}
3657
9babb374
BB
3658/*
3659 * Cleanup non-standard snapshots and clones.
3660 */
3661void
428870ff 3662ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
9babb374 3663{
40b84e7a
BB
3664 char *snap1name;
3665 char *clone1name;
3666 char *snap2name;
3667 char *clone2name;
3668 char *snap3name;
9babb374
BB
3669 int error;
3670
40b84e7a
BB
3671 snap1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3672 clone1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3673 snap2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3674 clone2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3675 snap3name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3676
b8864a23
BB
3677 (void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu",
3678 osname, (u_longlong_t)id);
3679 (void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu",
3680 osname, (u_longlong_t)id);
3681 (void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu",
3682 clone1name, (u_longlong_t)id);
3683 (void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu",
3684 osname, (u_longlong_t)id);
3685 (void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu",
3686 clone1name, (u_longlong_t)id);
9babb374 3687
13fe0198 3688 error = dsl_destroy_head(clone2name);
9babb374 3689 if (error && error != ENOENT)
13fe0198
MA
3690 fatal(0, "dsl_destroy_head(%s) = %d", clone2name, error);
3691 error = dsl_destroy_snapshot(snap3name, B_FALSE);
9babb374 3692 if (error && error != ENOENT)
13fe0198
MA
3693 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap3name, error);
3694 error = dsl_destroy_snapshot(snap2name, B_FALSE);
9babb374 3695 if (error && error != ENOENT)
13fe0198
MA
3696 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap2name, error);
3697 error = dsl_destroy_head(clone1name);
9babb374 3698 if (error && error != ENOENT)
13fe0198
MA
3699 fatal(0, "dsl_destroy_head(%s) = %d", clone1name, error);
3700 error = dsl_destroy_snapshot(snap1name, B_FALSE);
9babb374 3701 if (error && error != ENOENT)
13fe0198 3702 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap1name, error);
40b84e7a
BB
3703
3704 umem_free(snap1name, MAXNAMELEN);
3705 umem_free(clone1name, MAXNAMELEN);
3706 umem_free(snap2name, MAXNAMELEN);
3707 umem_free(clone2name, MAXNAMELEN);
3708 umem_free(snap3name, MAXNAMELEN);
9babb374
BB
3709}
3710
3711/*
3712 * Verify dsl_dataset_promote handles EBUSY
3713 */
3714void
428870ff 3715ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
9babb374 3716{
13fe0198 3717 objset_t *os;
40b84e7a
BB
3718 char *snap1name;
3719 char *clone1name;
3720 char *snap2name;
3721 char *clone2name;
3722 char *snap3name;
428870ff
BB
3723 char *osname = zd->zd_name;
3724 int error;
9babb374 3725
40b84e7a
BB
3726 snap1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3727 clone1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3728 snap2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3729 clone2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3730 snap3name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3731
7809eb8b 3732 (void) rw_rdlock(&ztest_name_lock);
9babb374 3733
428870ff 3734 ztest_dsl_dataset_cleanup(osname, id);
9babb374 3735
b8864a23
BB
3736 (void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu",
3737 osname, (u_longlong_t)id);
3738 (void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu",
3739 osname, (u_longlong_t)id);
3740 (void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu",
3741 clone1name, (u_longlong_t)id);
3742 (void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu",
3743 osname, (u_longlong_t)id);
3744 (void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu",
3745 clone1name, (u_longlong_t)id);
9babb374 3746
6f1ffb06 3747 error = dmu_objset_snapshot_one(osname, strchr(snap1name, '@') + 1);
9babb374
BB
3748 if (error && error != EEXIST) {
3749 if (error == ENOSPC) {
428870ff 3750 ztest_record_enospc(FTAG);
9babb374
BB
3751 goto out;
3752 }
3753 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
3754 }
3755
13fe0198 3756 error = dmu_objset_clone(clone1name, snap1name);
9babb374
BB
3757 if (error) {
3758 if (error == ENOSPC) {
428870ff 3759 ztest_record_enospc(FTAG);
9babb374
BB
3760 goto out;
3761 }
3762 fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
3763 }
3764
6f1ffb06 3765 error = dmu_objset_snapshot_one(clone1name, strchr(snap2name, '@') + 1);
428870ff
BB
3766 if (error && error != EEXIST) {
3767 if (error == ENOSPC) {
3768 ztest_record_enospc(FTAG);
3769 goto out;
34dc7c2f 3770 }
428870ff
BB
3771 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
3772 }
34dc7c2f 3773
6f1ffb06 3774 error = dmu_objset_snapshot_one(clone1name, strchr(snap3name, '@') + 1);
428870ff
BB
3775 if (error && error != EEXIST) {
3776 if (error == ENOSPC) {
3777 ztest_record_enospc(FTAG);
3778 goto out;
3779 }
3780 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
3781 }
34dc7c2f 3782
13fe0198 3783 error = dmu_objset_clone(clone2name, snap3name);
428870ff
BB
3784 if (error) {
3785 if (error == ENOSPC) {
3786 ztest_record_enospc(FTAG);
3787 goto out;
3788 }
3789 fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
3790 }
34dc7c2f 3791
13fe0198 3792 error = dmu_objset_own(snap2name, DMU_OST_ANY, B_TRUE, FTAG, &os);
428870ff 3793 if (error)
13fe0198 3794 fatal(0, "dmu_objset_own(%s) = %d", snap2name, error);
428870ff 3795 error = dsl_dataset_promote(clone2name, NULL);
9b67f605
MA
3796 if (error == ENOSPC) {
3797 dmu_objset_disown(os, FTAG);
3798 ztest_record_enospc(FTAG);
3799 goto out;
3800 }
428870ff
BB
3801 if (error != EBUSY)
3802 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
3803 error);
13fe0198 3804 dmu_objset_disown(os, FTAG);
34dc7c2f 3805
428870ff
BB
3806out:
3807 ztest_dsl_dataset_cleanup(osname, id);
34dc7c2f 3808
7809eb8b 3809 (void) rw_unlock(&ztest_name_lock);
40b84e7a
BB
3810
3811 umem_free(snap1name, MAXNAMELEN);
3812 umem_free(clone1name, MAXNAMELEN);
3813 umem_free(snap2name, MAXNAMELEN);
3814 umem_free(clone2name, MAXNAMELEN);
3815 umem_free(snap3name, MAXNAMELEN);
428870ff 3816}
34dc7c2f 3817
40b84e7a 3818#undef OD_ARRAY_SIZE
d1d7e268 3819#define OD_ARRAY_SIZE 4
40b84e7a 3820
428870ff
BB
3821/*
3822 * Verify that dmu_object_{alloc,free} work as expected.
3823 */
3824void
3825ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
3826{
40b84e7a
BB
3827 ztest_od_t *od;
3828 int batchsize;
3829 int size;
d6320ddb 3830 int b;
34dc7c2f 3831
d1d7e268 3832 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
40b84e7a
BB
3833 od = umem_alloc(size, UMEM_NOFAIL);
3834 batchsize = OD_ARRAY_SIZE;
3835
d6320ddb 3836 for (b = 0; b < batchsize; b++)
50c957f7
NB
3837 ztest_od_init(od + b, id, FTAG, b, DMU_OT_UINT64_OTHER,
3838 0, 0, 0);
34dc7c2f 3839
428870ff
BB
3840 /*
3841 * Destroy the previous batch of objects, create a new batch,
3842 * and do some I/O on the new objects.
3843 */
40b84e7a 3844 if (ztest_object_init(zd, od, size, B_TRUE) != 0)
428870ff 3845 return;
34dc7c2f 3846
428870ff
BB
3847 while (ztest_random(4 * batchsize) != 0)
3848 ztest_io(zd, od[ztest_random(batchsize)].od_object,
3849 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
40b84e7a
BB
3850
3851 umem_free(od, size);
34dc7c2f
BB
3852}
3853
40b84e7a 3854#undef OD_ARRAY_SIZE
d1d7e268 3855#define OD_ARRAY_SIZE 2
40b84e7a 3856
34dc7c2f
BB
3857/*
3858 * Verify that dmu_{read,write} work as expected.
3859 */
34dc7c2f 3860void
428870ff 3861ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
34dc7c2f 3862{
40b84e7a
BB
3863 int size;
3864 ztest_od_t *od;
3865
428870ff 3866 objset_t *os = zd->zd_os;
d1d7e268 3867 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
40b84e7a 3868 od = umem_alloc(size, UMEM_NOFAIL);
34dc7c2f
BB
3869 dmu_tx_t *tx;
3870 int i, freeit, error;
3871 uint64_t n, s, txg;
3872 bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
428870ff
BB
3873 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3874 uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
34dc7c2f
BB
3875 uint64_t regions = 997;
3876 uint64_t stride = 123456789ULL;
3877 uint64_t width = 40;
3878 int free_percent = 5;
3879
3880 /*
3881 * This test uses two objects, packobj and bigobj, that are always
3882 * updated together (i.e. in the same tx) so that their contents are
3883 * in sync and can be compared. Their contents relate to each other
3884 * in a simple way: packobj is a dense array of 'bufwad' structures,
3885 * while bigobj is a sparse array of the same bufwads. Specifically,
3886 * for any index n, there are three bufwads that should be identical:
3887 *
3888 * packobj, at offset n * sizeof (bufwad_t)
3889 * bigobj, at the head of the nth chunk
3890 * bigobj, at the tail of the nth chunk
3891 *
3892 * The chunk size is arbitrary. It doesn't have to be a power of two,
3893 * and it doesn't have any relation to the object blocksize.
3894 * The only requirement is that it can hold at least two bufwads.
3895 *
3896 * Normally, we write the bufwad to each of these locations.
3897 * However, free_percent of the time we instead write zeroes to
3898 * packobj and perform a dmu_free_range() on bigobj. By comparing
3899 * bigobj to packobj, we can verify that the DMU is correctly
3900 * tracking which parts of an object are allocated and free,
3901 * and that the contents of the allocated blocks are correct.
3902 */
3903
3904 /*
3905 * Read the directory info. If it's the first time, set things up.
3906 */
50c957f7
NB
3907 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, chunksize);
3908 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, 0,
3909 chunksize);
34dc7c2f 3910
40b84e7a
BB
3911 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
3912 umem_free(od, size);
428870ff 3913 return;
40b84e7a 3914 }
34dc7c2f 3915
428870ff
BB
3916 bigobj = od[0].od_object;
3917 packobj = od[1].od_object;
3918 chunksize = od[0].od_gen;
3919 ASSERT(chunksize == od[1].od_gen);
34dc7c2f
BB
3920
3921 /*
3922 * Prefetch a random chunk of the big object.
3923 * Our aim here is to get some async reads in flight
3924 * for blocks that we may free below; the DMU should
3925 * handle this race correctly.
3926 */
3927 n = ztest_random(regions) * stride + ztest_random(width);
3928 s = 1 + ztest_random(2 * width - 1);
fcff0f35
PD
3929 dmu_prefetch(os, bigobj, 0, n * chunksize, s * chunksize,
3930 ZIO_PRIORITY_SYNC_READ);
34dc7c2f
BB
3931
3932 /*
3933 * Pick a random index and compute the offsets into packobj and bigobj.
3934 */
3935 n = ztest_random(regions) * stride + ztest_random(width);
3936 s = 1 + ztest_random(width - 1);
3937
3938 packoff = n * sizeof (bufwad_t);
3939 packsize = s * sizeof (bufwad_t);
3940
428870ff
BB
3941 bigoff = n * chunksize;
3942 bigsize = s * chunksize;
34dc7c2f
BB
3943
3944 packbuf = umem_alloc(packsize, UMEM_NOFAIL);
3945 bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
3946
3947 /*
3948 * free_percent of the time, free a range of bigobj rather than
3949 * overwriting it.
3950 */
3951 freeit = (ztest_random(100) < free_percent);
3952
3953 /*
3954 * Read the current contents of our objects.
3955 */
428870ff 3956 error = dmu_read(os, packobj, packoff, packsize, packbuf,
9babb374 3957 DMU_READ_PREFETCH);
c99c9001 3958 ASSERT0(error);
428870ff 3959 error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
9babb374 3960 DMU_READ_PREFETCH);
c99c9001 3961 ASSERT0(error);
34dc7c2f
BB
3962
3963 /*
3964 * Get a tx for the mods to both packobj and bigobj.
3965 */
3966 tx = dmu_tx_create(os);
3967
428870ff 3968 dmu_tx_hold_write(tx, packobj, packoff, packsize);
34dc7c2f
BB
3969
3970 if (freeit)
428870ff 3971 dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
34dc7c2f 3972 else
428870ff 3973 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
34dc7c2f 3974
383fc4a9
MA
3975 /* This accounts for setting the checksum/compression. */
3976 dmu_tx_hold_bonus(tx, bigobj);
3977
428870ff
BB
3978 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3979 if (txg == 0) {
34dc7c2f
BB
3980 umem_free(packbuf, packsize);
3981 umem_free(bigbuf, bigsize);
40b84e7a 3982 umem_free(od, size);
34dc7c2f
BB
3983 return;
3984 }
3985
9b67f605
MA
3986 enum zio_checksum cksum;
3987 do {
3988 cksum = (enum zio_checksum)
3989 ztest_random_dsl_prop(ZFS_PROP_CHECKSUM);
3990 } while (cksum >= ZIO_CHECKSUM_LEGACY_FUNCTIONS);
3991 dmu_object_set_checksum(os, bigobj, cksum, tx);
428870ff 3992
9b67f605
MA
3993 enum zio_compress comp;
3994 do {
3995 comp = (enum zio_compress)
3996 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION);
3997 } while (comp >= ZIO_COMPRESS_LEGACY_FUNCTIONS);
3998 dmu_object_set_compress(os, bigobj, comp, tx);
34dc7c2f
BB
3999
4000 /*
4001 * For each index from n to n + s, verify that the existing bufwad
4002 * in packobj matches the bufwads at the head and tail of the
4003 * corresponding chunk in bigobj. Then update all three bufwads
4004 * with the new values we want to write out.
4005 */
4006 for (i = 0; i < s; i++) {
4007 /* LINTED */
4008 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
4009 /* LINTED */
428870ff 4010 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
34dc7c2f 4011 /* LINTED */
428870ff 4012 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
34dc7c2f
BB
4013
4014 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
4015 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
4016
4017 if (pack->bw_txg > txg)
4018 fatal(0, "future leak: got %llx, open txg is %llx",
4019 pack->bw_txg, txg);
4020
4021 if (pack->bw_data != 0 && pack->bw_index != n + i)
4022 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4023 pack->bw_index, n, i);
4024
4025 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
4026 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
4027
4028 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
4029 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
4030
4031 if (freeit) {
4032 bzero(pack, sizeof (bufwad_t));
4033 } else {
4034 pack->bw_index = n + i;
4035 pack->bw_txg = txg;
4036 pack->bw_data = 1 + ztest_random(-2ULL);
4037 }
4038 *bigH = *pack;
4039 *bigT = *pack;
4040 }
4041
4042 /*
4043 * We've verified all the old bufwads, and made new ones.
4044 * Now write them out.
4045 */
428870ff 4046 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
34dc7c2f
BB
4047
4048 if (freeit) {
c242c188 4049 if (ztest_opts.zo_verbose >= 7) {
34dc7c2f
BB
4050 (void) printf("freeing offset %llx size %llx"
4051 " txg %llx\n",
4052 (u_longlong_t)bigoff,
4053 (u_longlong_t)bigsize,
4054 (u_longlong_t)txg);
4055 }
428870ff 4056 VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
34dc7c2f 4057 } else {
c242c188 4058 if (ztest_opts.zo_verbose >= 7) {
34dc7c2f
BB
4059 (void) printf("writing offset %llx size %llx"
4060 " txg %llx\n",
4061 (u_longlong_t)bigoff,
4062 (u_longlong_t)bigsize,
4063 (u_longlong_t)txg);
4064 }
428870ff 4065 dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
34dc7c2f
BB
4066 }
4067
4068 dmu_tx_commit(tx);
4069
4070 /*
4071 * Sanity check the stuff we just wrote.
4072 */
4073 {
4074 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
4075 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
4076
428870ff 4077 VERIFY(0 == dmu_read(os, packobj, packoff,
9babb374 4078 packsize, packcheck, DMU_READ_PREFETCH));
428870ff 4079 VERIFY(0 == dmu_read(os, bigobj, bigoff,
9babb374 4080 bigsize, bigcheck, DMU_READ_PREFETCH));
34dc7c2f
BB
4081
4082 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
4083 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
4084
4085 umem_free(packcheck, packsize);
4086 umem_free(bigcheck, bigsize);
4087 }
4088
4089 umem_free(packbuf, packsize);
4090 umem_free(bigbuf, bigsize);
40b84e7a 4091 umem_free(od, size);
34dc7c2f
BB
4092}
4093
9babb374
BB
4094void
4095compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
428870ff 4096 uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
9babb374
BB
4097{
4098 uint64_t i;
4099 bufwad_t *pack;
4100 bufwad_t *bigH;
4101 bufwad_t *bigT;
4102
4103 /*
4104 * For each index from n to n + s, verify that the existing bufwad
4105 * in packobj matches the bufwads at the head and tail of the
4106 * corresponding chunk in bigobj. Then update all three bufwads
4107 * with the new values we want to write out.
4108 */
4109 for (i = 0; i < s; i++) {
4110 /* LINTED */
4111 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
4112 /* LINTED */
428870ff 4113 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
9babb374 4114 /* LINTED */
428870ff 4115 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
9babb374
BB
4116
4117 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
4118 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
4119
4120 if (pack->bw_txg > txg)
4121 fatal(0, "future leak: got %llx, open txg is %llx",
4122 pack->bw_txg, txg);
4123
4124 if (pack->bw_data != 0 && pack->bw_index != n + i)
4125 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4126 pack->bw_index, n, i);
4127
4128 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
4129 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
4130
4131 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
4132 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
4133
4134 pack->bw_index = n + i;
4135 pack->bw_txg = txg;
4136 pack->bw_data = 1 + ztest_random(-2ULL);
4137
4138 *bigH = *pack;
4139 *bigT = *pack;
4140 }
4141}
4142
40b84e7a 4143#undef OD_ARRAY_SIZE
d1d7e268 4144#define OD_ARRAY_SIZE 2
40b84e7a 4145
9babb374 4146void
428870ff 4147ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
9babb374 4148{
428870ff 4149 objset_t *os = zd->zd_os;
40b84e7a 4150 ztest_od_t *od;
9babb374
BB
4151 dmu_tx_t *tx;
4152 uint64_t i;
4153 int error;
40b84e7a 4154 int size;
9babb374
BB
4155 uint64_t n, s, txg;
4156 bufwad_t *packbuf, *bigbuf;
428870ff
BB
4157 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
4158 uint64_t blocksize = ztest_random_blocksize();
4159 uint64_t chunksize = blocksize;
9babb374
BB
4160 uint64_t regions = 997;
4161 uint64_t stride = 123456789ULL;
4162 uint64_t width = 9;
4163 dmu_buf_t *bonus_db;
4164 arc_buf_t **bigbuf_arcbufs;
428870ff 4165 dmu_object_info_t doi;
9babb374 4166
d1d7e268 4167 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
40b84e7a
BB
4168 od = umem_alloc(size, UMEM_NOFAIL);
4169
9babb374
BB
4170 /*
4171 * This test uses two objects, packobj and bigobj, that are always
4172 * updated together (i.e. in the same tx) so that their contents are
4173 * in sync and can be compared. Their contents relate to each other
4174 * in a simple way: packobj is a dense array of 'bufwad' structures,
4175 * while bigobj is a sparse array of the same bufwads. Specifically,
4176 * for any index n, there are three bufwads that should be identical:
4177 *
4178 * packobj, at offset n * sizeof (bufwad_t)
4179 * bigobj, at the head of the nth chunk
4180 * bigobj, at the tail of the nth chunk
4181 *
4182 * The chunk size is set equal to bigobj block size so that
4183 * dmu_assign_arcbuf() can be tested for object updates.
4184 */
4185
4186 /*
4187 * Read the directory info. If it's the first time, set things up.
4188 */
50c957f7
NB
4189 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0, 0);
4190 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, 0,
4191 chunksize);
40b84e7a 4192
9babb374 4193
40b84e7a
BB
4194 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
4195 umem_free(od, size);
428870ff 4196 return;
40b84e7a 4197 }
9babb374 4198
428870ff
BB
4199 bigobj = od[0].od_object;
4200 packobj = od[1].od_object;
4201 blocksize = od[0].od_blocksize;
4202 chunksize = blocksize;
4203 ASSERT(chunksize == od[1].od_gen);
9babb374 4204
428870ff
BB
4205 VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
4206 VERIFY(ISP2(doi.doi_data_block_size));
4207 VERIFY(chunksize == doi.doi_data_block_size);
4208 VERIFY(chunksize >= 2 * sizeof (bufwad_t));
9babb374
BB
4209
4210 /*
4211 * Pick a random index and compute the offsets into packobj and bigobj.
4212 */
4213 n = ztest_random(regions) * stride + ztest_random(width);
4214 s = 1 + ztest_random(width - 1);
4215
4216 packoff = n * sizeof (bufwad_t);
4217 packsize = s * sizeof (bufwad_t);
4218
428870ff
BB
4219 bigoff = n * chunksize;
4220 bigsize = s * chunksize;
9babb374
BB
4221
4222 packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
4223 bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
4224
428870ff 4225 VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
9babb374
BB
4226
4227 bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
4228
4229 /*
4230 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
4231 * Iteration 1 test zcopy to already referenced dbufs.
4232 * Iteration 2 test zcopy to dirty dbuf in the same txg.
4233 * Iteration 3 test zcopy to dbuf dirty in previous txg.
4234 * Iteration 4 test zcopy when dbuf is no longer dirty.
4235 * Iteration 5 test zcopy when it can't be done.
4236 * Iteration 6 one more zcopy write.
4237 */
4238 for (i = 0; i < 7; i++) {
4239 uint64_t j;
4240 uint64_t off;
4241
4242 /*
4243 * In iteration 5 (i == 5) use arcbufs
4244 * that don't match bigobj blksz to test
4245 * dmu_assign_arcbuf() when it can't directly
4246 * assign an arcbuf to a dbuf.
4247 */
4248 for (j = 0; j < s; j++) {
b9541d6b 4249 if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
9babb374 4250 bigbuf_arcbufs[j] =
428870ff 4251 dmu_request_arcbuf(bonus_db, chunksize);
9babb374
BB
4252 } else {
4253 bigbuf_arcbufs[2 * j] =
428870ff 4254 dmu_request_arcbuf(bonus_db, chunksize / 2);
9babb374 4255 bigbuf_arcbufs[2 * j + 1] =
428870ff 4256 dmu_request_arcbuf(bonus_db, chunksize / 2);
9babb374
BB
4257 }
4258 }
4259
4260 /*
4261 * Get a tx for the mods to both packobj and bigobj.
4262 */
4263 tx = dmu_tx_create(os);
4264
428870ff
BB
4265 dmu_tx_hold_write(tx, packobj, packoff, packsize);
4266 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
9babb374 4267
428870ff
BB
4268 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4269 if (txg == 0) {
9babb374
BB
4270 umem_free(packbuf, packsize);
4271 umem_free(bigbuf, bigsize);
4272 for (j = 0; j < s; j++) {
b9541d6b
CW
4273 if (i != 5 ||
4274 chunksize < (SPA_MINBLOCKSIZE * 2)) {
9babb374
BB
4275 dmu_return_arcbuf(bigbuf_arcbufs[j]);
4276 } else {
4277 dmu_return_arcbuf(
4278 bigbuf_arcbufs[2 * j]);
4279 dmu_return_arcbuf(
4280 bigbuf_arcbufs[2 * j + 1]);
4281 }
4282 }
4283 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
40b84e7a 4284 umem_free(od, size);
9babb374
BB
4285 dmu_buf_rele(bonus_db, FTAG);
4286 return;
4287 }
4288
9babb374
BB
4289 /*
4290 * 50% of the time don't read objects in the 1st iteration to
4291 * test dmu_assign_arcbuf() for the case when there're no
4292 * existing dbufs for the specified offsets.
4293 */
4294 if (i != 0 || ztest_random(2) != 0) {
428870ff 4295 error = dmu_read(os, packobj, packoff,
9babb374 4296 packsize, packbuf, DMU_READ_PREFETCH);
c99c9001 4297 ASSERT0(error);
428870ff 4298 error = dmu_read(os, bigobj, bigoff, bigsize,
9babb374 4299 bigbuf, DMU_READ_PREFETCH);
c99c9001 4300 ASSERT0(error);
9babb374
BB
4301 }
4302 compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
428870ff 4303 n, chunksize, txg);
9babb374
BB
4304
4305 /*
4306 * We've verified all the old bufwads, and made new ones.
4307 * Now write them out.
4308 */
428870ff 4309 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
c242c188 4310 if (ztest_opts.zo_verbose >= 7) {
9babb374
BB
4311 (void) printf("writing offset %llx size %llx"
4312 " txg %llx\n",
4313 (u_longlong_t)bigoff,
4314 (u_longlong_t)bigsize,
4315 (u_longlong_t)txg);
4316 }
428870ff 4317 for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
9babb374 4318 dmu_buf_t *dbt;
b9541d6b 4319 if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
9babb374 4320 bcopy((caddr_t)bigbuf + (off - bigoff),
428870ff 4321 bigbuf_arcbufs[j]->b_data, chunksize);
9babb374
BB
4322 } else {
4323 bcopy((caddr_t)bigbuf + (off - bigoff),
4324 bigbuf_arcbufs[2 * j]->b_data,
428870ff 4325 chunksize / 2);
9babb374 4326 bcopy((caddr_t)bigbuf + (off - bigoff) +
428870ff 4327 chunksize / 2,
9babb374 4328 bigbuf_arcbufs[2 * j + 1]->b_data,
428870ff 4329 chunksize / 2);
9babb374
BB
4330 }
4331
4332 if (i == 1) {
428870ff
BB
4333 VERIFY(dmu_buf_hold(os, bigobj, off,
4334 FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0);
9babb374 4335 }
b9541d6b 4336 if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
9babb374
BB
4337 dmu_assign_arcbuf(bonus_db, off,
4338 bigbuf_arcbufs[j], tx);
4339 } else {
4340 dmu_assign_arcbuf(bonus_db, off,
4341 bigbuf_arcbufs[2 * j], tx);
4342 dmu_assign_arcbuf(bonus_db,
428870ff 4343 off + chunksize / 2,
9babb374
BB
4344 bigbuf_arcbufs[2 * j + 1], tx);
4345 }
4346 if (i == 1) {
4347 dmu_buf_rele(dbt, FTAG);
4348 }
4349 }
4350 dmu_tx_commit(tx);
4351
4352 /*
4353 * Sanity check the stuff we just wrote.
4354 */
4355 {
4356 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
4357 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
4358
428870ff 4359 VERIFY(0 == dmu_read(os, packobj, packoff,
9babb374 4360 packsize, packcheck, DMU_READ_PREFETCH));
428870ff 4361 VERIFY(0 == dmu_read(os, bigobj, bigoff,
9babb374
BB
4362 bigsize, bigcheck, DMU_READ_PREFETCH));
4363
4364 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
4365 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
4366
4367 umem_free(packcheck, packsize);
4368 umem_free(bigcheck, bigsize);
4369 }
4370 if (i == 2) {
4371 txg_wait_open(dmu_objset_pool(os), 0);
4372 } else if (i == 3) {
4373 txg_wait_synced(dmu_objset_pool(os), 0);
4374 }
4375 }
4376
4377 dmu_buf_rele(bonus_db, FTAG);
4378 umem_free(packbuf, packsize);
4379 umem_free(bigbuf, bigsize);
4380 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
40b84e7a 4381 umem_free(od, size);
9babb374
BB
4382}
4383
428870ff 4384/* ARGSUSED */
34dc7c2f 4385void
428870ff 4386ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
34dc7c2f 4387{
40b84e7a
BB
4388 ztest_od_t *od;
4389
d1d7e268 4390 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
428870ff
BB
4391 uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
4392 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
34dc7c2f
BB
4393
4394 /*
428870ff
BB
4395 * Have multiple threads write to large offsets in an object
4396 * to verify that parallel writes to an object -- even to the
4397 * same blocks within the object -- doesn't cause any trouble.
34dc7c2f 4398 */
50c957f7 4399 ztest_od_init(od, ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
9babb374 4400
40b84e7a 4401 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0)
34dc7c2f 4402 return;
34dc7c2f 4403
428870ff 4404 while (ztest_random(10) != 0)
40b84e7a
BB
4405 ztest_io(zd, od->od_object, offset);
4406
d1d7e268 4407 umem_free(od, sizeof (ztest_od_t));
428870ff 4408}
34dc7c2f 4409
428870ff
BB
4410void
4411ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
4412{
40b84e7a 4413 ztest_od_t *od;
428870ff
BB
4414 uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
4415 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4416 uint64_t count = ztest_random(20) + 1;
4417 uint64_t blocksize = ztest_random_blocksize();
4418 void *data;
34dc7c2f 4419
d1d7e268 4420 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
34dc7c2f 4421
50c957f7 4422 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0, 0);
40b84e7a 4423
d1d7e268
MK
4424 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4425 !ztest_random(2)) != 0) {
4426 umem_free(od, sizeof (ztest_od_t));
34dc7c2f 4427 return;
40b84e7a 4428 }
34dc7c2f 4429
40b84e7a 4430 if (ztest_truncate(zd, od->od_object, offset, count * blocksize) != 0) {
d1d7e268 4431 umem_free(od, sizeof (ztest_od_t));
34dc7c2f 4432 return;
40b84e7a 4433 }
34dc7c2f 4434
40b84e7a 4435 ztest_prealloc(zd, od->od_object, offset, count * blocksize);
34dc7c2f 4436
428870ff 4437 data = umem_zalloc(blocksize, UMEM_NOFAIL);
34dc7c2f 4438
428870ff
BB
4439 while (ztest_random(count) != 0) {
4440 uint64_t randoff = offset + (ztest_random(count) * blocksize);
40b84e7a 4441 if (ztest_write(zd, od->od_object, randoff, blocksize,
428870ff
BB
4442 data) != 0)
4443 break;
4444 while (ztest_random(4) != 0)
40b84e7a 4445 ztest_io(zd, od->od_object, randoff);
9babb374 4446 }
34dc7c2f 4447
428870ff 4448 umem_free(data, blocksize);
d1d7e268 4449 umem_free(od, sizeof (ztest_od_t));
34dc7c2f
BB
4450}
4451
4452/*
4453 * Verify that zap_{create,destroy,add,remove,update} work as expected.
4454 */
4455#define ZTEST_ZAP_MIN_INTS 1
4456#define ZTEST_ZAP_MAX_INTS 4
4457#define ZTEST_ZAP_MAX_PROPS 1000
4458
4459void
428870ff 4460ztest_zap(ztest_ds_t *zd, uint64_t id)
34dc7c2f 4461{
428870ff 4462 objset_t *os = zd->zd_os;
40b84e7a 4463 ztest_od_t *od;
34dc7c2f
BB
4464 uint64_t object;
4465 uint64_t txg, last_txg;
4466 uint64_t value[ZTEST_ZAP_MAX_INTS];
4467 uint64_t zl_ints, zl_intsize, prop;
4468 int i, ints;
4469 dmu_tx_t *tx;
4470 char propname[100], txgname[100];
4471 int error;
34dc7c2f
BB
4472 char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
4473
d1d7e268 4474 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
50c957f7 4475 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0, 0);
34dc7c2f 4476
40b84e7a
BB
4477 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4478 !ztest_random(2)) != 0)
4479 goto out;
34dc7c2f 4480
40b84e7a 4481 object = od->od_object;
34dc7c2f 4482
428870ff
BB
4483 /*
4484 * Generate a known hash collision, and verify that
4485 * we can lookup and remove both entries.
4486 */
4487 tx = dmu_tx_create(os);
4488 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4489 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4490 if (txg == 0)
40b84e7a 4491 goto out;
428870ff
BB
4492 for (i = 0; i < 2; i++) {
4493 value[i] = i;
4494 VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
4495 1, &value[i], tx));
4496 }
4497 for (i = 0; i < 2; i++) {
4498 VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
4499 sizeof (uint64_t), 1, &value[i], tx));
4500 VERIFY3U(0, ==,
4501 zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
4502 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4503 ASSERT3U(zl_ints, ==, 1);
4504 }
4505 for (i = 0; i < 2; i++) {
4506 VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
34dc7c2f 4507 }
428870ff 4508 dmu_tx_commit(tx);
34dc7c2f 4509
428870ff
BB
4510 /*
4511 * Generate a buch of random entries.
4512 */
34dc7c2f
BB
4513 ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
4514
4515 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4516 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4517 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4518 bzero(value, sizeof (value));
4519 last_txg = 0;
4520
4521 /*
4522 * If these zap entries already exist, validate their contents.
4523 */
4524 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4525 if (error == 0) {
4526 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4527 ASSERT3U(zl_ints, ==, 1);
4528
4529 VERIFY(zap_lookup(os, object, txgname, zl_intsize,
4530 zl_ints, &last_txg) == 0);
4531
4532 VERIFY(zap_length(os, object, propname, &zl_intsize,
4533 &zl_ints) == 0);
4534
4535 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4536 ASSERT3U(zl_ints, ==, ints);
4537
4538 VERIFY(zap_lookup(os, object, propname, zl_intsize,
4539 zl_ints, value) == 0);
4540
4541 for (i = 0; i < ints; i++) {
4542 ASSERT3U(value[i], ==, last_txg + object + i);
4543 }
4544 } else {
4545 ASSERT3U(error, ==, ENOENT);
4546 }
4547
4548 /*
4549 * Atomically update two entries in our zap object.
4550 * The first is named txg_%llu, and contains the txg
4551 * in which the property was last updated. The second
4552 * is named prop_%llu, and the nth element of its value
4553 * should be txg + object + n.
4554 */
4555 tx = dmu_tx_create(os);
428870ff
BB
4556 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4557 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4558 if (txg == 0)
40b84e7a 4559 goto out;
34dc7c2f
BB
4560
4561 if (last_txg > txg)
4562 fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
4563
4564 for (i = 0; i < ints; i++)
4565 value[i] = txg + object + i;
4566
428870ff
BB
4567 VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
4568 1, &txg, tx));
4569 VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
4570 ints, value, tx));
34dc7c2f
BB
4571
4572 dmu_tx_commit(tx);
4573
4574 /*
4575 * Remove a random pair of entries.
4576 */
4577 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4578 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4579 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4580
4581 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4582
4583 if (error == ENOENT)
40b84e7a 4584 goto out;
34dc7c2f 4585
c99c9001 4586 ASSERT0(error);
34dc7c2f
BB
4587
4588 tx = dmu_tx_create(os);
428870ff
BB
4589 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4590 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4591 if (txg == 0)
40b84e7a 4592 goto out;
428870ff
BB
4593 VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
4594 VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
4595 dmu_tx_commit(tx);
40b84e7a 4596out:
d1d7e268 4597 umem_free(od, sizeof (ztest_od_t));
428870ff 4598}
34dc7c2f 4599
428870ff
BB
4600/*
4601 * Testcase to test the upgrading of a microzap to fatzap.
4602 */
4603void
4604ztest_fzap(ztest_ds_t *zd, uint64_t id)
4605{
4606 objset_t *os = zd->zd_os;
40b84e7a 4607 ztest_od_t *od;
428870ff 4608 uint64_t object, txg;
d6320ddb 4609 int i;
34dc7c2f 4610
d1d7e268 4611 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
50c957f7 4612 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0, 0);
428870ff 4613
40b84e7a
BB
4614 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4615 !ztest_random(2)) != 0)
4616 goto out;
4617 object = od->od_object;
34dc7c2f
BB
4618
4619 /*
428870ff
BB
4620 * Add entries to this ZAP and make sure it spills over
4621 * and gets upgraded to a fatzap. Also, since we are adding
4622 * 2050 entries we should see ptrtbl growth and leaf-block split.
34dc7c2f 4623 */
d6320ddb 4624 for (i = 0; i < 2050; i++) {
428870ff
BB
4625 char name[MAXNAMELEN];
4626 uint64_t value = i;
4627 dmu_tx_t *tx;
4628 int error;
34dc7c2f 4629
428870ff 4630 (void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
b8864a23 4631 (u_longlong_t)id, (u_longlong_t)value);
428870ff
BB
4632
4633 tx = dmu_tx_create(os);
4634 dmu_tx_hold_zap(tx, object, B_TRUE, name);
4635 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4636 if (txg == 0)
40b84e7a 4637 goto out;
428870ff
BB
4638 error = zap_add(os, object, name, sizeof (uint64_t), 1,
4639 &value, tx);
4640 ASSERT(error == 0 || error == EEXIST);
4641 dmu_tx_commit(tx);
34dc7c2f 4642 }
40b84e7a 4643out:
d1d7e268 4644 umem_free(od, sizeof (ztest_od_t));
34dc7c2f
BB
4645}
4646
428870ff 4647/* ARGSUSED */
34dc7c2f 4648void
428870ff 4649ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
34dc7c2f 4650{
428870ff 4651 objset_t *os = zd->zd_os;
40b84e7a 4652 ztest_od_t *od;
34dc7c2f
BB
4653 uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
4654 dmu_tx_t *tx;
4655 int i, namelen, error;
428870ff 4656 int micro = ztest_random(2);
34dc7c2f
BB
4657 char name[20], string_value[20];
4658 void *data;
4659
d1d7e268 4660 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
50c957f7 4661 ztest_od_init(od, ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER, 0, 0, 0);
428870ff 4662
40b84e7a 4663 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
d1d7e268 4664 umem_free(od, sizeof (ztest_od_t));
428870ff 4665 return;
40b84e7a 4666 }
428870ff 4667
40b84e7a 4668 object = od->od_object;
428870ff 4669
34dc7c2f
BB
4670 /*
4671 * Generate a random name of the form 'xxx.....' where each
4672 * x is a random printable character and the dots are dots.
4673 * There are 94 such characters, and the name length goes from
4674 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
4675 */
4676 namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
4677
4678 for (i = 0; i < 3; i++)
4679 name[i] = '!' + ztest_random('~' - '!' + 1);
4680 for (; i < namelen - 1; i++)
4681 name[i] = '.';
4682 name[i] = '\0';
4683
428870ff 4684 if ((namelen & 1) || micro) {
34dc7c2f
BB
4685 wsize = sizeof (txg);
4686 wc = 1;
4687 data = &txg;
4688 } else {
4689 wsize = 1;
4690 wc = namelen;
4691 data = string_value;
4692 }
4693
4694 count = -1ULL;
13fe0198 4695 VERIFY0(zap_count(os, object, &count));
34dc7c2f
BB
4696 ASSERT(count != -1ULL);
4697
4698 /*
4699 * Select an operation: length, lookup, add, update, remove.
4700 */
4701 i = ztest_random(5);
4702
4703 if (i >= 2) {
4704 tx = dmu_tx_create(os);
428870ff
BB
4705 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4706 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4707 if (txg == 0)
34dc7c2f 4708 return;
34dc7c2f
BB
4709 bcopy(name, string_value, namelen);
4710 } else {
4711 tx = NULL;
4712 txg = 0;
4713 bzero(string_value, namelen);
4714 }
4715
4716 switch (i) {
4717
4718 case 0:
4719 error = zap_length(os, object, name, &zl_wsize, &zl_wc);
4720 if (error == 0) {
4721 ASSERT3U(wsize, ==, zl_wsize);
4722 ASSERT3U(wc, ==, zl_wc);
4723 } else {
4724 ASSERT3U(error, ==, ENOENT);
4725 }
4726 break;
4727
4728 case 1:
4729 error = zap_lookup(os, object, name, wsize, wc, data);
4730 if (error == 0) {
4731 if (data == string_value &&
4732 bcmp(name, data, namelen) != 0)
4733 fatal(0, "name '%s' != val '%s' len %d",
4734 name, data, namelen);
4735 } else {
4736 ASSERT3U(error, ==, ENOENT);
4737 }
4738 break;
4739
4740 case 2:
4741 error = zap_add(os, object, name, wsize, wc, data, tx);
4742 ASSERT(error == 0 || error == EEXIST);
4743 break;
4744
4745 case 3:
4746 VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
4747 break;
4748
4749 case 4:
4750 error = zap_remove(os, object, name, tx);
4751 ASSERT(error == 0 || error == ENOENT);
4752 break;
4753 }
4754
4755 if (tx != NULL)
4756 dmu_tx_commit(tx);
40b84e7a 4757
d1d7e268 4758 umem_free(od, sizeof (ztest_od_t));
34dc7c2f
BB
4759}
4760
428870ff
BB
4761/*
4762 * Commit callback data.
4763 */
4764typedef struct ztest_cb_data {
4765 list_node_t zcd_node;
4766 uint64_t zcd_txg;
4767 int zcd_expected_err;
4768 boolean_t zcd_added;
4769 boolean_t zcd_called;
4770 spa_t *zcd_spa;
4771} ztest_cb_data_t;
4772
4773/* This is the actual commit callback function */
4774static void
4775ztest_commit_callback(void *arg, int error)
4776{
4777 ztest_cb_data_t *data = arg;
4778 uint64_t synced_txg;
4779
4780 VERIFY(data != NULL);
4781 VERIFY3S(data->zcd_expected_err, ==, error);
4782 VERIFY(!data->zcd_called);
4783
4784 synced_txg = spa_last_synced_txg(data->zcd_spa);
4785 if (data->zcd_txg > synced_txg)
4786 fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
4787 ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
4788 synced_txg);
4789
4790 data->zcd_called = B_TRUE;
4791
4792 if (error == ECANCELED) {
c99c9001 4793 ASSERT0(data->zcd_txg);
428870ff
BB
4794 ASSERT(!data->zcd_added);
4795
4796 /*
4797 * The private callback data should be destroyed here, but
4798 * since we are going to check the zcd_called field after
4799 * dmu_tx_abort(), we will destroy it there.
4800 */
4801 return;
4802 }
4803
090ff092 4804 ASSERT(data->zcd_added);
428870ff
BB
4805 ASSERT3U(data->zcd_txg, !=, 0);
4806
1e33ac1e 4807 (void) mutex_enter(&zcl.zcl_callbacks_lock);
090ff092
RC
4808
4809 /* See if this cb was called more quickly */
4810 if ((synced_txg - data->zcd_txg) < zc_min_txg_delay)
4811 zc_min_txg_delay = synced_txg - data->zcd_txg;
4812
4813 /* Remove our callback from the list */
428870ff 4814 list_remove(&zcl.zcl_callbacks, data);
090ff092 4815
1e33ac1e 4816 (void) mutex_exit(&zcl.zcl_callbacks_lock);
428870ff 4817
428870ff
BB
4818 umem_free(data, sizeof (ztest_cb_data_t));
4819}
4820
4821/* Allocate and initialize callback data structure */
4822static ztest_cb_data_t *
4823ztest_create_cb_data(objset_t *os, uint64_t txg)
4824{
4825 ztest_cb_data_t *cb_data;
4826
4827 cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
4828
4829 cb_data->zcd_txg = txg;
4830 cb_data->zcd_spa = dmu_objset_spa(os);
1e33ac1e 4831 list_link_init(&cb_data->zcd_node);
428870ff
BB
4832
4833 return (cb_data);
4834}
4835
428870ff
BB
4836/*
4837 * Commit callback test.
4838 */
34dc7c2f 4839void
428870ff
BB
4840ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
4841{
4842 objset_t *os = zd->zd_os;
40b84e7a 4843 ztest_od_t *od;
428870ff
BB
4844 dmu_tx_t *tx;
4845 ztest_cb_data_t *cb_data[3], *tmp_cb;
4846 uint64_t old_txg, txg;
090ff092 4847 int i, error = 0;
428870ff 4848
d1d7e268 4849 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
50c957f7 4850 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
428870ff 4851
40b84e7a 4852 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
d1d7e268 4853 umem_free(od, sizeof (ztest_od_t));
428870ff 4854 return;
40b84e7a 4855 }
428870ff
BB
4856
4857 tx = dmu_tx_create(os);
4858
4859 cb_data[0] = ztest_create_cb_data(os, 0);
4860 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
4861
40b84e7a 4862 dmu_tx_hold_write(tx, od->od_object, 0, sizeof (uint64_t));
428870ff
BB
4863
4864 /* Every once in a while, abort the transaction on purpose */
4865 if (ztest_random(100) == 0)
4866 error = -1;
4867
4868 if (!error)
4869 error = dmu_tx_assign(tx, TXG_NOWAIT);
4870
4871 txg = error ? 0 : dmu_tx_get_txg(tx);
4872
4873 cb_data[0]->zcd_txg = txg;
4874 cb_data[1] = ztest_create_cb_data(os, txg);
4875 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
4876
4877 if (error) {
4878 /*
4879 * It's not a strict requirement to call the registered
4880 * callbacks from inside dmu_tx_abort(), but that's what
4881 * it's supposed to happen in the current implementation
4882 * so we will check for that.
4883 */
4884 for (i = 0; i < 2; i++) {
4885 cb_data[i]->zcd_expected_err = ECANCELED;
4886 VERIFY(!cb_data[i]->zcd_called);
4887 }
4888
4889 dmu_tx_abort(tx);
4890
4891 for (i = 0; i < 2; i++) {
4892 VERIFY(cb_data[i]->zcd_called);
4893 umem_free(cb_data[i], sizeof (ztest_cb_data_t));
4894 }
4895
d1d7e268 4896 umem_free(od, sizeof (ztest_od_t));
428870ff
BB
4897 return;
4898 }
4899
4900 cb_data[2] = ztest_create_cb_data(os, txg);
4901 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
4902
4903 /*
4904 * Read existing data to make sure there isn't a future leak.
4905 */
40b84e7a 4906 VERIFY(0 == dmu_read(os, od->od_object, 0, sizeof (uint64_t),
428870ff
BB
4907 &old_txg, DMU_READ_PREFETCH));
4908
4909 if (old_txg > txg)
4910 fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
4911 old_txg, txg);
4912
40b84e7a 4913 dmu_write(os, od->od_object, 0, sizeof (uint64_t), &txg, tx);
428870ff 4914
1e33ac1e 4915 (void) mutex_enter(&zcl.zcl_callbacks_lock);
428870ff
BB
4916
4917 /*
4918 * Since commit callbacks don't have any ordering requirement and since
4919 * it is theoretically possible for a commit callback to be called
4920 * after an arbitrary amount of time has elapsed since its txg has been
4921 * synced, it is difficult to reliably determine whether a commit
4922 * callback hasn't been called due to high load or due to a flawed
4923 * implementation.
4924 *
4925 * In practice, we will assume that if after a certain number of txgs a
4926 * commit callback hasn't been called, then most likely there's an
4927 * implementation bug..
4928 */
4929 tmp_cb = list_head(&zcl.zcl_callbacks);
4930 if (tmp_cb != NULL &&
090ff092 4931 tmp_cb->zcd_txg + ZTEST_COMMIT_CB_THRESH < txg) {
428870ff
BB
4932 fatal(0, "Commit callback threshold exceeded, oldest txg: %"
4933 PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
4934 }
4935
4936 /*
4937 * Let's find the place to insert our callbacks.
4938 *
4939 * Even though the list is ordered by txg, it is possible for the
4940 * insertion point to not be the end because our txg may already be
4941 * quiescing at this point and other callbacks in the open txg
4942 * (from other objsets) may have sneaked in.
4943 */
4944 tmp_cb = list_tail(&zcl.zcl_callbacks);
4945 while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
4946 tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
4947
4948 /* Add the 3 callbacks to the list */
4949 for (i = 0; i < 3; i++) {
4950 if (tmp_cb == NULL)
4951 list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
4952 else
4953 list_insert_after(&zcl.zcl_callbacks, tmp_cb,
4954 cb_data[i]);
4955
4956 cb_data[i]->zcd_added = B_TRUE;
4957 VERIFY(!cb_data[i]->zcd_called);
4958
4959 tmp_cb = cb_data[i];
4960 }
4961
090ff092
RC
4962 zc_cb_counter += 3;
4963
1e33ac1e 4964 (void) mutex_exit(&zcl.zcl_callbacks_lock);
428870ff
BB
4965
4966 dmu_tx_commit(tx);
40b84e7a 4967
d1d7e268 4968 umem_free(od, sizeof (ztest_od_t));
428870ff
BB
4969}
4970
50c957f7
NB
4971/*
4972 * Visit each object in the dataset. Verify that its properties
4973 * are consistent what was stored in the block tag when it was created,
4974 * and that its unused bonus buffer space has not been overwritten.
4975 */
4976void
4977ztest_verify_dnode_bt(ztest_ds_t *zd, uint64_t id)
4978{
4979 objset_t *os = zd->zd_os;
4980 uint64_t obj;
4981 int err = 0;
4982
4983 for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
4984 ztest_block_tag_t *bt = NULL;
4985 dmu_object_info_t doi;
4986 dmu_buf_t *db;
4987
4988 if (dmu_bonus_hold(os, obj, FTAG, &db) != 0)
4989 continue;
4990
4991 dmu_object_info_from_db(db, &doi);
4992 if (doi.doi_bonus_size >= sizeof (*bt))
4993 bt = ztest_bt_bonus(db);
4994
4995 if (bt && bt->bt_magic == BT_MAGIC) {
4996 ztest_bt_verify(bt, os, obj, doi.doi_dnodesize,
4997 bt->bt_offset, bt->bt_gen, bt->bt_txg,
4998 bt->bt_crtxg);
4999 ztest_verify_unused_bonus(db, bt, obj, os, bt->bt_gen);
5000 }
5001
5002 dmu_buf_rele(db, FTAG);
5003 }
5004}
5005
428870ff
BB
5006/* ARGSUSED */
5007void
5008ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
5009{
5010 zfs_prop_t proplist[] = {
5011 ZFS_PROP_CHECKSUM,
5012 ZFS_PROP_COMPRESSION,
5013 ZFS_PROP_COPIES,
5014 ZFS_PROP_DEDUP
5015 };
d6320ddb 5016 int p;
428870ff 5017
7809eb8b 5018 (void) rw_rdlock(&ztest_name_lock);
428870ff 5019
d6320ddb 5020 for (p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
428870ff
BB
5021 (void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
5022 ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
5023
76d52067
BB
5024 VERIFY0(ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_RECORDSIZE,
5025 ztest_random_blocksize(), (int)ztest_random(2)));
5026
7809eb8b 5027 (void) rw_unlock(&ztest_name_lock);
428870ff
BB
5028}
5029
5030/* ARGSUSED */
5031void
5032ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
5033{
428870ff
BB
5034 nvlist_t *props = NULL;
5035
7809eb8b 5036 (void) rw_rdlock(&ztest_name_lock);
428870ff 5037
c242c188 5038 (void) ztest_spa_prop_set_uint64(ZPOOL_PROP_DEDUPDITTO,
428870ff
BB
5039 ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
5040
c99c9001 5041 VERIFY0(spa_prop_get(ztest_spa, &props));
428870ff 5042
c242c188 5043 if (ztest_opts.zo_verbose >= 6)
428870ff
BB
5044 dump_nvlist(props, 4);
5045
5046 nvlist_free(props);
5047
7809eb8b 5048 (void) rw_unlock(&ztest_name_lock);
428870ff
BB
5049}
5050
13fe0198
MA
5051static int
5052user_release_one(const char *snapname, const char *holdname)
5053{
5054 nvlist_t *snaps, *holds;
5055 int error;
5056
5057 snaps = fnvlist_alloc();
5058 holds = fnvlist_alloc();
5059 fnvlist_add_boolean(holds, holdname);
5060 fnvlist_add_nvlist(snaps, snapname, holds);
5061 fnvlist_free(holds);
5062 error = dsl_dataset_user_release(snaps, NULL);
5063 fnvlist_free(snaps);
5064 return (error);
5065}
5066
428870ff
BB
5067/*
5068 * Test snapshot hold/release and deferred destroy.
5069 */
5070void
5071ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
34dc7c2f 5072{
34dc7c2f 5073 int error;
428870ff
BB
5074 objset_t *os = zd->zd_os;
5075 objset_t *origin;
5076 char snapname[100];
5077 char fullname[100];
5078 char clonename[100];
5079 char tag[100];
5080 char osname[MAXNAMELEN];
13fe0198 5081 nvlist_t *holds;
34dc7c2f 5082
7809eb8b 5083 (void) rw_rdlock(&ztest_name_lock);
34dc7c2f
BB
5084
5085 dmu_objset_name(os, osname);
5086
d1d7e268
MK
5087 (void) snprintf(snapname, sizeof (snapname), "sh1_%llu",
5088 (u_longlong_t)id);
13fe0198
MA
5089 (void) snprintf(fullname, sizeof (fullname), "%s@%s", osname, snapname);
5090 (void) snprintf(clonename, sizeof (clonename),
d1d7e268
MK
5091 "%s/ch1_%llu", osname, (u_longlong_t)id);
5092 (void) snprintf(tag, sizeof (tag), "tag_%llu", (u_longlong_t)id);
428870ff
BB
5093
5094 /*
5095 * Clean up from any previous run.
5096 */
13fe0198
MA
5097 error = dsl_destroy_head(clonename);
5098 if (error != ENOENT)
5099 ASSERT0(error);
5100 error = user_release_one(fullname, tag);
5101 if (error != ESRCH && error != ENOENT)
5102 ASSERT0(error);
5103 error = dsl_destroy_snapshot(fullname, B_FALSE);
5104 if (error != ENOENT)
5105 ASSERT0(error);
428870ff
BB
5106
5107 /*
5108 * Create snapshot, clone it, mark snap for deferred destroy,
5109 * destroy clone, verify snap was also destroyed.
5110 */
6f1ffb06 5111 error = dmu_objset_snapshot_one(osname, snapname);
428870ff
BB
5112 if (error) {
5113 if (error == ENOSPC) {
5114 ztest_record_enospc("dmu_objset_snapshot");
5115 goto out;
34dc7c2f 5116 }
428870ff
BB
5117 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
5118 }
34dc7c2f 5119
13fe0198 5120 error = dmu_objset_clone(clonename, fullname);
428870ff 5121 if (error) {
34dc7c2f 5122 if (error == ENOSPC) {
428870ff
BB
5123 ztest_record_enospc("dmu_objset_clone");
5124 goto out;
34dc7c2f 5125 }
428870ff
BB
5126 fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
5127 }
34dc7c2f 5128
13fe0198 5129 error = dsl_destroy_snapshot(fullname, B_TRUE);
428870ff 5130 if (error) {
13fe0198 5131 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
428870ff
BB
5132 fullname, error);
5133 }
34dc7c2f 5134
13fe0198 5135 error = dsl_destroy_head(clonename);
428870ff 5136 if (error)
13fe0198 5137 fatal(0, "dsl_destroy_head(%s) = %d", clonename, error);
34dc7c2f 5138
428870ff
BB
5139 error = dmu_objset_hold(fullname, FTAG, &origin);
5140 if (error != ENOENT)
5141 fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
34dc7c2f 5142
428870ff
BB
5143 /*
5144 * Create snapshot, add temporary hold, verify that we can't
5145 * destroy a held snapshot, mark for deferred destroy,
5146 * release hold, verify snapshot was destroyed.
5147 */
6f1ffb06 5148 error = dmu_objset_snapshot_one(osname, snapname);
428870ff
BB
5149 if (error) {
5150 if (error == ENOSPC) {
5151 ztest_record_enospc("dmu_objset_snapshot");
5152 goto out;
34dc7c2f 5153 }
428870ff
BB
5154 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
5155 }
5156
13fe0198
MA
5157 holds = fnvlist_alloc();
5158 fnvlist_add_string(holds, fullname, tag);
5159 error = dsl_dataset_user_hold(holds, 0, NULL);
5160 fnvlist_free(holds);
5161
9b67f605
MA
5162 if (error == ENOSPC) {
5163 ztest_record_enospc("dsl_dataset_user_hold");
5164 goto out;
5165 } else if (error) {
5166 fatal(0, "dsl_dataset_user_hold(%s, %s) = %u",
5167 fullname, tag, error);
5168 }
428870ff 5169
13fe0198 5170 error = dsl_destroy_snapshot(fullname, B_FALSE);
428870ff 5171 if (error != EBUSY) {
13fe0198 5172 fatal(0, "dsl_destroy_snapshot(%s, B_FALSE) = %d",
428870ff
BB
5173 fullname, error);
5174 }
5175
13fe0198 5176 error = dsl_destroy_snapshot(fullname, B_TRUE);
428870ff 5177 if (error) {
13fe0198 5178 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
428870ff 5179 fullname, error);
34dc7c2f
BB
5180 }
5181
13fe0198 5182 error = user_release_one(fullname, tag);
428870ff 5183 if (error)
95fd54a1 5184 fatal(0, "user_release_one(%s, %s) = %d", fullname, tag, error);
428870ff 5185
13fe0198 5186 VERIFY3U(dmu_objset_hold(fullname, FTAG, &origin), ==, ENOENT);
428870ff
BB
5187
5188out:
7809eb8b 5189 (void) rw_unlock(&ztest_name_lock);
34dc7c2f
BB
5190}
5191
34dc7c2f
BB
5192/*
5193 * Inject random faults into the on-disk data.
5194 */
428870ff 5195/* ARGSUSED */
34dc7c2f 5196void
428870ff 5197ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
34dc7c2f 5198{
428870ff 5199 ztest_shared_t *zs = ztest_shared;
c242c188 5200 spa_t *spa = ztest_spa;
34dc7c2f
BB
5201 int fd;
5202 uint64_t offset;
428870ff 5203 uint64_t leaves;
c5b3a7bb 5204 uint64_t bad = 0x1990c0ffeedecadeull;
34dc7c2f 5205 uint64_t top, leaf;
40b84e7a
BB
5206 char *path0;
5207 char *pathrand;
34dc7c2f 5208 size_t fsize;
91d88843 5209 int bshift = SPA_MAXBLOCKSHIFT + 2; /* don't scrog all labels */
34dc7c2f 5210 int iters = 1000;
428870ff
BB
5211 int maxfaults;
5212 int mirror_save;
b128c09f 5213 vdev_t *vd0 = NULL;
34dc7c2f 5214 uint64_t guid0 = 0;
428870ff
BB
5215 boolean_t islog = B_FALSE;
5216
40b84e7a
BB
5217 path0 = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
5218 pathrand = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
5219
c242c188 5220 mutex_enter(&ztest_vdev_lock);
428870ff 5221 maxfaults = MAXFAULTS();
c242c188 5222 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
428870ff 5223 mirror_save = zs->zs_mirrors;
c242c188 5224 mutex_exit(&ztest_vdev_lock);
34dc7c2f 5225
b128c09f 5226 ASSERT(leaves >= 1);
34dc7c2f 5227
621dd7bb
GW
5228 /*
5229 * Grab the name lock as reader. There are some operations
5230 * which don't like to have their vdevs changed while
5231 * they are in progress (i.e. spa_change_guid). Those
5232 * operations will have grabbed the name lock as writer.
5233 */
7809eb8b 5234 (void) rw_rdlock(&ztest_name_lock);
621dd7bb 5235
34dc7c2f 5236 /*
b128c09f 5237 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
34dc7c2f 5238 */
b128c09f 5239 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
34dc7c2f 5240
b128c09f
BB
5241 if (ztest_random(2) == 0) {
5242 /*
428870ff 5243 * Inject errors on a normal data device or slog device.
b128c09f 5244 */
428870ff
BB
5245 top = ztest_random_vdev_top(spa, B_TRUE);
5246 leaf = ztest_random(leaves) + zs->zs_splits;
34dc7c2f 5247
b128c09f
BB
5248 /*
5249 * Generate paths to the first leaf in this top-level vdev,
5250 * and to the random leaf we selected. We'll induce transient
5251 * write failures and random online/offline activity on leaf 0,
5252 * and we'll write random garbage to the randomly chosen leaf.
5253 */
6aec1cd5 5254 (void) snprintf(path0, MAXPATHLEN, ztest_dev_template,
c242c188
CS
5255 ztest_opts.zo_dir, ztest_opts.zo_pool,
5256 top * leaves + zs->zs_splits);
6aec1cd5 5257 (void) snprintf(pathrand, MAXPATHLEN, ztest_dev_template,
c242c188
CS
5258 ztest_opts.zo_dir, ztest_opts.zo_pool,
5259 top * leaves + leaf);
34dc7c2f 5260
b128c09f 5261 vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
428870ff
BB
5262 if (vd0 != NULL && vd0->vdev_top->vdev_islog)
5263 islog = B_TRUE;
5264
621dd7bb
GW
5265 /*
5266 * If the top-level vdev needs to be resilvered
5267 * then we only allow faults on the device that is
5268 * resilvering.
5269 */
5270 if (vd0 != NULL && maxfaults != 1 &&
5271 (!vdev_resilver_needed(vd0->vdev_top, NULL, NULL) ||
5d1f7fb6 5272 vd0->vdev_resilver_txg != 0)) {
b128c09f
BB
5273 /*
5274 * Make vd0 explicitly claim to be unreadable,
5275 * or unwriteable, or reach behind its back
5276 * and close the underlying fd. We can do this if
5277 * maxfaults == 0 because we'll fail and reexecute,
5278 * and we can do it if maxfaults >= 2 because we'll
5279 * have enough redundancy. If maxfaults == 1, the
5280 * combination of this with injection of random data
5281 * corruption below exceeds the pool's fault tolerance.
5282 */
5283 vdev_file_t *vf = vd0->vdev_tsd;
5284
5285 if (vf != NULL && ztest_random(3) == 0) {
5286 (void) close(vf->vf_vnode->v_fd);
5287 vf->vf_vnode->v_fd = -1;
5288 } else if (ztest_random(2) == 0) {
5289 vd0->vdev_cant_read = B_TRUE;
5290 } else {
5291 vd0->vdev_cant_write = B_TRUE;
5292 }
5293 guid0 = vd0->vdev_guid;
5294 }
5295 } else {
5296 /*
5297 * Inject errors on an l2cache device.
5298 */
5299 spa_aux_vdev_t *sav = &spa->spa_l2cache;
34dc7c2f 5300
b128c09f
BB
5301 if (sav->sav_count == 0) {
5302 spa_config_exit(spa, SCL_STATE, FTAG);
7809eb8b 5303 (void) rw_unlock(&ztest_name_lock);
40b84e7a 5304 goto out;
b128c09f
BB
5305 }
5306 vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
34dc7c2f 5307 guid0 = vd0->vdev_guid;
b128c09f
BB
5308 (void) strcpy(path0, vd0->vdev_path);
5309 (void) strcpy(pathrand, vd0->vdev_path);
5310
5311 leaf = 0;
5312 leaves = 1;
5313 maxfaults = INT_MAX; /* no limit on cache devices */
34dc7c2f
BB
5314 }
5315
b128c09f 5316 spa_config_exit(spa, SCL_STATE, FTAG);
7809eb8b 5317 (void) rw_unlock(&ztest_name_lock);
b128c09f 5318
34dc7c2f 5319 /*
428870ff
BB
5320 * If we can tolerate two or more faults, or we're dealing
5321 * with a slog, randomly online/offline vd0.
34dc7c2f 5322 */
428870ff 5323 if ((maxfaults >= 2 || islog) && guid0 != 0) {
fb5f0bc8
BB
5324 if (ztest_random(10) < 6) {
5325 int flags = (ztest_random(2) == 0 ?
5326 ZFS_OFFLINE_TEMPORARY : 0);
428870ff
BB
5327
5328 /*
5329 * We have to grab the zs_name_lock as writer to
5330 * prevent a race between offlining a slog and
5331 * destroying a dataset. Offlining the slog will
5332 * grab a reference on the dataset which may cause
13fe0198 5333 * dsl_destroy_head() to fail with EBUSY thus
428870ff
BB
5334 * leaving the dataset in an inconsistent state.
5335 */
5336 if (islog)
7809eb8b 5337 (void) rw_wrlock(&ztest_name_lock);
428870ff 5338
fb5f0bc8 5339 VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
428870ff
BB
5340
5341 if (islog)
7809eb8b 5342 (void) rw_unlock(&ztest_name_lock);
fb5f0bc8 5343 } else {
1eb5bfa3
GW
5344 /*
5345 * Ideally we would like to be able to randomly
5346 * call vdev_[on|off]line without holding locks
5347 * to force unpredictable failures but the side
5348 * effects of vdev_[on|off]line prevent us from
5349 * doing so. We grab the ztest_vdev_lock here to
5350 * prevent a race between injection testing and
5351 * aux_vdev removal.
5352 */
5353 mutex_enter(&ztest_vdev_lock);
fb5f0bc8 5354 (void) vdev_online(spa, guid0, 0, NULL);
1eb5bfa3 5355 mutex_exit(&ztest_vdev_lock);
fb5f0bc8 5356 }
34dc7c2f
BB
5357 }
5358
428870ff 5359 if (maxfaults == 0)
40b84e7a 5360 goto out;
428870ff 5361
34dc7c2f
BB
5362 /*
5363 * We have at least single-fault tolerance, so inject data corruption.
5364 */
5365 fd = open(pathrand, O_RDWR);
5366
5367 if (fd == -1) /* we hit a gap in the device namespace */
40b84e7a 5368 goto out;
34dc7c2f
BB
5369
5370 fsize = lseek(fd, 0, SEEK_END);
5371
5372 while (--iters != 0) {
91d88843
MA
5373 /*
5374 * The offset must be chosen carefully to ensure that
5375 * we do not inject a given logical block with errors
5376 * on two different leaf devices, because ZFS can not
5377 * tolerate that (if maxfaults==1).
5378 *
5379 * We divide each leaf into chunks of size
5380 * (# leaves * SPA_MAXBLOCKSIZE * 4). Within each chunk
5381 * there is a series of ranges to which we can inject errors.
5382 * Each range can accept errors on only a single leaf vdev.
5383 * The error injection ranges are separated by ranges
5384 * which we will not inject errors on any device (DMZs).
5385 * Each DMZ must be large enough such that a single block
5386 * can not straddle it, so that a single block can not be
5387 * a target in two different injection ranges (on different
5388 * leaf vdevs).
5389 *
5390 * For example, with 3 leaves, each chunk looks like:
5391 * 0 to 32M: injection range for leaf 0
5392 * 32M to 64M: DMZ - no injection allowed
5393 * 64M to 96M: injection range for leaf 1
5394 * 96M to 128M: DMZ - no injection allowed
5395 * 128M to 160M: injection range for leaf 2
5396 * 160M to 192M: DMZ - no injection allowed
5397 */
34dc7c2f
BB
5398 offset = ztest_random(fsize / (leaves << bshift)) *
5399 (leaves << bshift) + (leaf << bshift) +
5400 (ztest_random(1ULL << (bshift - 1)) & -8ULL);
5401
5402 if (offset >= fsize)
5403 continue;
5404
c242c188 5405 mutex_enter(&ztest_vdev_lock);
428870ff 5406 if (mirror_save != zs->zs_mirrors) {
c242c188 5407 mutex_exit(&ztest_vdev_lock);
428870ff 5408 (void) close(fd);
40b84e7a 5409 goto out;
428870ff 5410 }
34dc7c2f
BB
5411
5412 if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
5413 fatal(1, "can't inject bad word at 0x%llx in %s",
5414 offset, pathrand);
428870ff 5415
c242c188 5416 mutex_exit(&ztest_vdev_lock);
428870ff 5417
c242c188 5418 if (ztest_opts.zo_verbose >= 7)
428870ff
BB
5419 (void) printf("injected bad word into %s,"
5420 " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
34dc7c2f
BB
5421 }
5422
5423 (void) close(fd);
40b84e7a
BB
5424out:
5425 umem_free(path0, MAXPATHLEN);
5426 umem_free(pathrand, MAXPATHLEN);
34dc7c2f
BB
5427}
5428
5429/*
428870ff 5430 * Verify that DDT repair works as expected.
34dc7c2f
BB
5431 */
5432void
428870ff 5433ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
34dc7c2f 5434{
428870ff 5435 ztest_shared_t *zs = ztest_shared;
c242c188 5436 spa_t *spa = ztest_spa;
428870ff 5437 objset_t *os = zd->zd_os;
40b84e7a 5438 ztest_od_t *od;
428870ff
BB
5439 uint64_t object, blocksize, txg, pattern, psize;
5440 enum zio_checksum checksum = spa_dedup_checksum(spa);
5441 dmu_buf_t *db;
5442 dmu_tx_t *tx;
5443 void *buf;
5444 blkptr_t blk;
5445 int copies = 2 * ZIO_DEDUPDITTO_MIN;
d6320ddb 5446 int i;
34dc7c2f 5447
428870ff
BB
5448 blocksize = ztest_random_blocksize();
5449 blocksize = MIN(blocksize, 2048); /* because we write so many */
34dc7c2f 5450
d1d7e268 5451 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
50c957f7 5452 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0, 0);
34dc7c2f 5453
40b84e7a 5454 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
d1d7e268 5455 umem_free(od, sizeof (ztest_od_t));
428870ff 5456 return;
40b84e7a 5457 }
34dc7c2f
BB
5458
5459 /*
428870ff
BB
5460 * Take the name lock as writer to prevent anyone else from changing
5461 * the pool and dataset properies we need to maintain during this test.
34dc7c2f 5462 */
7809eb8b 5463 (void) rw_wrlock(&ztest_name_lock);
34dc7c2f 5464
428870ff
BB
5465 if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
5466 B_FALSE) != 0 ||
5467 ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
5468 B_FALSE) != 0) {
7809eb8b 5469 (void) rw_unlock(&ztest_name_lock);
d1d7e268 5470 umem_free(od, sizeof (ztest_od_t));
428870ff
BB
5471 return;
5472 }
5473
5474 object = od[0].od_object;
5475 blocksize = od[0].od_blocksize;
3541dc6d 5476 pattern = zs->zs_guid ^ dmu_objset_fsid_guid(os);
428870ff
BB
5477
5478 ASSERT(object != 0);
5479
5480 tx = dmu_tx_create(os);
5481 dmu_tx_hold_write(tx, object, 0, copies * blocksize);
5482 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
5483 if (txg == 0) {
7809eb8b 5484 (void) rw_unlock(&ztest_name_lock);
d1d7e268 5485 umem_free(od, sizeof (ztest_od_t));
428870ff
BB
5486 return;
5487 }
34dc7c2f
BB
5488
5489 /*
428870ff 5490 * Write all the copies of our block.
34dc7c2f 5491 */
d6320ddb 5492 for (i = 0; i < copies; i++) {
428870ff 5493 uint64_t offset = i * blocksize;
13fe0198
MA
5494 int error = dmu_buf_hold(os, object, offset, FTAG, &db,
5495 DMU_READ_NO_PREFETCH);
5496 if (error != 0) {
5497 fatal(B_FALSE, "dmu_buf_hold(%p, %llu, %llu) = %u",
5498 os, (long long)object, (long long) offset, error);
5499 }
428870ff
BB
5500 ASSERT(db->db_offset == offset);
5501 ASSERT(db->db_size == blocksize);
5502 ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
5503 ztest_pattern_match(db->db_data, db->db_size, 0ULL));
5504 dmu_buf_will_fill(db, tx);
5505 ztest_pattern_set(db->db_data, db->db_size, pattern);
5506 dmu_buf_rele(db, FTAG);
5507 }
34dc7c2f 5508
428870ff
BB
5509 dmu_tx_commit(tx);
5510 txg_wait_synced(spa_get_dsl(spa), txg);
34dc7c2f
BB
5511
5512 /*
428870ff 5513 * Find out what block we got.
34dc7c2f 5514 */
03c6040b
GW
5515 VERIFY0(dmu_buf_hold(os, object, 0, FTAG, &db,
5516 DMU_READ_NO_PREFETCH));
428870ff
BB
5517 blk = *((dmu_buf_impl_t *)db)->db_blkptr;
5518 dmu_buf_rele(db, FTAG);
34dc7c2f
BB
5519
5520 /*
428870ff 5521 * Damage the block. Dedup-ditto will save us when we read it later.
34dc7c2f 5522 */
428870ff
BB
5523 psize = BP_GET_PSIZE(&blk);
5524 buf = zio_buf_alloc(psize);
5525 ztest_pattern_set(buf, psize, ~pattern);
34dc7c2f 5526
428870ff
BB
5527 (void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
5528 buf, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
5529 ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
34dc7c2f 5530
428870ff 5531 zio_buf_free(buf, psize);
34dc7c2f 5532
7809eb8b 5533 (void) rw_unlock(&ztest_name_lock);
d1d7e268 5534 umem_free(od, sizeof (ztest_od_t));
34dc7c2f
BB
5535}
5536
34dc7c2f 5537/*
428870ff 5538 * Scrub the pool.
34dc7c2f 5539 */
428870ff
BB
5540/* ARGSUSED */
5541void
5542ztest_scrub(ztest_ds_t *zd, uint64_t id)
34dc7c2f 5543{
c242c188 5544 spa_t *spa = ztest_spa;
34dc7c2f 5545
428870ff
BB
5546 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5547 (void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
5548 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5549}
34dc7c2f 5550
3541dc6d
GA
5551/*
5552 * Change the guid for the pool.
5553 */
5554/* ARGSUSED */
5555void
5556ztest_reguid(ztest_ds_t *zd, uint64_t id)
5557{
c242c188 5558 spa_t *spa = ztest_spa;
3541dc6d 5559 uint64_t orig, load;
3bc7e0fb 5560 int error;
3541dc6d
GA
5561
5562 orig = spa_guid(spa);
5563 load = spa_load_guid(spa);
3bc7e0fb 5564
7809eb8b 5565 (void) rw_wrlock(&ztest_name_lock);
3bc7e0fb 5566 error = spa_change_guid(spa);
7809eb8b 5567 (void) rw_unlock(&ztest_name_lock);
3bc7e0fb
GW
5568
5569 if (error != 0)
3541dc6d
GA
5570 return;
5571
ea0b2538 5572 if (ztest_opts.zo_verbose >= 4) {
3541dc6d
GA
5573 (void) printf("Changed guid old %llu -> %llu\n",
5574 (u_longlong_t)orig, (u_longlong_t)spa_guid(spa));
5575 }
5576
5577 VERIFY3U(orig, !=, spa_guid(spa));
5578 VERIFY3U(load, ==, spa_load_guid(spa));
5579}
5580
428870ff
BB
5581/*
5582 * Rename the pool to a different name and then rename it back.
5583 */
5584/* ARGSUSED */
5585void
5586ztest_spa_rename(ztest_ds_t *zd, uint64_t id)
5587{
428870ff
BB
5588 char *oldname, *newname;
5589 spa_t *spa;
34dc7c2f 5590
7809eb8b 5591 (void) rw_wrlock(&ztest_name_lock);
34dc7c2f 5592
c242c188 5593 oldname = ztest_opts.zo_pool;
428870ff
BB
5594 newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
5595 (void) strcpy(newname, oldname);
5596 (void) strcat(newname, "_tmp");
34dc7c2f
BB
5597
5598 /*
428870ff 5599 * Do the rename
34dc7c2f 5600 */
428870ff 5601 VERIFY3U(0, ==, spa_rename(oldname, newname));
34dc7c2f
BB
5602
5603 /*
428870ff 5604 * Try to open it under the old name, which shouldn't exist
34dc7c2f 5605 */
428870ff 5606 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
34dc7c2f
BB
5607
5608 /*
428870ff
BB
5609 * Open it under the new name and make sure it's still the same spa_t.
5610 */
5611 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
34dc7c2f 5612
c242c188 5613 ASSERT(spa == ztest_spa);
428870ff 5614 spa_close(spa, FTAG);
34dc7c2f
BB
5615
5616 /*
428870ff 5617 * Rename it back to the original
34dc7c2f 5618 */
428870ff 5619 VERIFY3U(0, ==, spa_rename(newname, oldname));
34dc7c2f 5620
428870ff
BB
5621 /*
5622 * Make sure it can still be opened
5623 */
5624 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
34dc7c2f 5625
c242c188 5626 ASSERT(spa == ztest_spa);
428870ff
BB
5627 spa_close(spa, FTAG);
5628
5629 umem_free(newname, strlen(newname) + 1);
5630
7809eb8b 5631 (void) rw_unlock(&ztest_name_lock);
34dc7c2f
BB
5632}
5633
1eeb4562
JX
5634void
5635ztest_fletcher(ztest_ds_t *zd, uint64_t id)
5636{
5637 hrtime_t end = gethrtime() + NANOSEC;
5638
5639 while (gethrtime() <= end) {
5640 int run_count = 100;
5641 void *buf;
5642 uint32_t size;
5643 int *ptr;
5644 int i;
5645 zio_cksum_t zc_ref;
5646 zio_cksum_t zc_ref_byteswap;
5647
5648 size = ztest_random_blocksize();
5649 buf = umem_alloc(size, UMEM_NOFAIL);
5650
5651 for (i = 0, ptr = buf; i < size / sizeof (*ptr); i++, ptr++)
5652 *ptr = ztest_random(UINT_MAX);
5653
5654 VERIFY0(fletcher_4_impl_set("scalar"));
5655 fletcher_4_native(buf, size, &zc_ref);
5656 fletcher_4_byteswap(buf, size, &zc_ref_byteswap);
5657
5658 VERIFY0(fletcher_4_impl_set("cycle"));
5659 while (run_count-- > 0) {
5660 zio_cksum_t zc;
5661 zio_cksum_t zc_byteswap;
5662
5663 fletcher_4_byteswap(buf, size, &zc_byteswap);
5664 fletcher_4_native(buf, size, &zc);
5665
5666 VERIFY0(bcmp(&zc, &zc_ref, sizeof (zc)));
5667 VERIFY0(bcmp(&zc_byteswap, &zc_ref_byteswap,
5668 sizeof (zc_byteswap)));
5669 }
5670
5671 umem_free(buf, size);
5672 }
5673}
5674
90aa094d
CC
5675static int
5676ztest_check_path(char *path)
5677{
5678 struct stat s;
5679 /* return true on success */
5680 return (!stat(path, &s));
5681}
5682
5683static void
5684ztest_get_zdb_bin(char *bin, int len)
5685{
5686 char *zdb_path;
5687 /*
5688 * Try to use ZDB_PATH and in-tree zdb path. If not successful, just
5689 * let popen to search through PATH.
5690 */
5691 if ((zdb_path = getenv("ZDB_PATH"))) {
5692 strlcpy(bin, zdb_path, len); /* In env */
5693 if (!ztest_check_path(bin)) {
5694 ztest_dump_core = 0;
5695 fatal(1, "invalid ZDB_PATH '%s'", bin);
5696 }
5697 return;
5698 }
5699
5700 VERIFY(realpath(getexecname(), bin) != NULL);
5701 if (strstr(bin, "/ztest/")) {
5702 strstr(bin, "/ztest/")[0] = '\0'; /* In-tree */
5703 strcat(bin, "/zdb/zdb");
5704 if (ztest_check_path(bin))
5705 return;
5706 }
5707 strcpy(bin, "zdb");
5708}
5709
428870ff
BB
5710/*
5711 * Verify pool integrity by running zdb.
5712 */
34dc7c2f 5713static void
428870ff 5714ztest_run_zdb(char *pool)
34dc7c2f
BB
5715{
5716 int status;
34dc7c2f 5717 char *bin;
0e8d1b2d
BB
5718 char *zdb;
5719 char *zbuf;
90aa094d 5720 const int len = MAXPATHLEN + MAXNAMELEN + 20;
34dc7c2f
BB
5721 FILE *fp;
5722
90aa094d
CC
5723 bin = umem_alloc(len, UMEM_NOFAIL);
5724 zdb = umem_alloc(len, UMEM_NOFAIL);
0e8d1b2d 5725 zbuf = umem_alloc(1024, UMEM_NOFAIL);
34dc7c2f 5726
90aa094d 5727 ztest_get_zdb_bin(bin, len);
0e8d1b2d
BB
5728
5729 (void) sprintf(zdb,
9b67f605 5730 "%s -bcc%s%s -d -U %s %s",
0e8d1b2d 5731 bin,
c242c188
CS
5732 ztest_opts.zo_verbose >= 3 ? "s" : "",
5733 ztest_opts.zo_verbose >= 4 ? "v" : "",
428870ff 5734 spa_config_path,
b128c09f 5735 pool);
34dc7c2f 5736
c242c188 5737 if (ztest_opts.zo_verbose >= 5)
34dc7c2f
BB
5738 (void) printf("Executing %s\n", strstr(zdb, "zdb "));
5739
5740 fp = popen(zdb, "r");
5741
6aec1cd5 5742 while (fgets(zbuf, 1024, fp) != NULL)
c242c188 5743 if (ztest_opts.zo_verbose >= 3)
34dc7c2f
BB
5744 (void) printf("%s", zbuf);
5745
5746 status = pclose(fp);
5747
5748 if (status == 0)
0e8d1b2d 5749 goto out;
34dc7c2f
BB
5750
5751 ztest_dump_core = 0;
5752 if (WIFEXITED(status))
5753 fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
5754 else
5755 fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
0e8d1b2d 5756out:
90aa094d
CC
5757 umem_free(bin, len);
5758 umem_free(zdb, len);
0e8d1b2d 5759 umem_free(zbuf, 1024);
34dc7c2f
BB
5760}
5761
5762static void
5763ztest_walk_pool_directory(char *header)
5764{
5765 spa_t *spa = NULL;
5766
c242c188 5767 if (ztest_opts.zo_verbose >= 6)
34dc7c2f
BB
5768 (void) printf("%s\n", header);
5769
5770 mutex_enter(&spa_namespace_lock);
5771 while ((spa = spa_next(spa)) != NULL)
c242c188 5772 if (ztest_opts.zo_verbose >= 6)
34dc7c2f
BB
5773 (void) printf("\t%s\n", spa_name(spa));
5774 mutex_exit(&spa_namespace_lock);
5775}
5776
5777static void
5778ztest_spa_import_export(char *oldname, char *newname)
5779{
fb5f0bc8 5780 nvlist_t *config, *newconfig;
34dc7c2f
BB
5781 uint64_t pool_guid;
5782 spa_t *spa;
13fe0198 5783 int error;
34dc7c2f 5784
c242c188 5785 if (ztest_opts.zo_verbose >= 4) {
34dc7c2f
BB
5786 (void) printf("import/export: old = %s, new = %s\n",
5787 oldname, newname);
5788 }
5789
5790 /*
5791 * Clean up from previous runs.
5792 */
5793 (void) spa_destroy(newname);
5794
5795 /*
5796 * Get the pool's configuration and guid.
5797 */
428870ff 5798 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
34dc7c2f 5799
fb5f0bc8
BB
5800 /*
5801 * Kick off a scrub to tickle scrub/export races.
5802 */
5803 if (ztest_random(2) == 0)
428870ff 5804 (void) spa_scan(spa, POOL_SCAN_SCRUB);
fb5f0bc8 5805
34dc7c2f
BB
5806 pool_guid = spa_guid(spa);
5807 spa_close(spa, FTAG);
5808
5809 ztest_walk_pool_directory("pools before export");
5810
5811 /*
5812 * Export it.
5813 */
428870ff 5814 VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
34dc7c2f
BB
5815
5816 ztest_walk_pool_directory("pools after export");
5817
fb5f0bc8
BB
5818 /*
5819 * Try to import it.
5820 */
5821 newconfig = spa_tryimport(config);
5822 ASSERT(newconfig != NULL);
5823 nvlist_free(newconfig);
5824
34dc7c2f
BB
5825 /*
5826 * Import it under the new name.
5827 */
13fe0198
MA
5828 error = spa_import(newname, config, NULL, 0);
5829 if (error != 0) {
5830 dump_nvlist(config, 0);
5831 fatal(B_FALSE, "couldn't import pool %s as %s: error %u",
5832 oldname, newname, error);
5833 }
34dc7c2f
BB
5834
5835 ztest_walk_pool_directory("pools after import");
5836
5837 /*
5838 * Try to import it again -- should fail with EEXIST.
5839 */
572e2857 5840 VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL, 0));
34dc7c2f
BB
5841
5842 /*
5843 * Try to import it under a different name -- should fail with EEXIST.
5844 */
572e2857 5845 VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL, 0));
34dc7c2f
BB
5846
5847 /*
5848 * Verify that the pool is no longer visible under the old name.
5849 */
428870ff 5850 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
34dc7c2f
BB
5851
5852 /*
5853 * Verify that we can open and close the pool using the new name.
5854 */
428870ff 5855 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
34dc7c2f
BB
5856 ASSERT(pool_guid == spa_guid(spa));
5857 spa_close(spa, FTAG);
5858
5859 nvlist_free(config);
5860}
5861
fb5f0bc8
BB
5862static void
5863ztest_resume(spa_t *spa)
5864{
c242c188 5865 if (spa_suspended(spa) && ztest_opts.zo_verbose >= 6)
428870ff
BB
5866 (void) printf("resuming from suspended state\n");
5867 spa_vdev_state_enter(spa, SCL_NONE);
5868 vdev_clear(spa, NULL);
5869 (void) spa_vdev_state_exit(spa, NULL, 0);
5870 (void) zio_resume(spa);
fb5f0bc8
BB
5871}
5872
34dc7c2f 5873static void *
fb5f0bc8 5874ztest_resume_thread(void *arg)
34dc7c2f 5875{
b128c09f 5876 spa_t *spa = arg;
34dc7c2f
BB
5877
5878 while (!ztest_exiting) {
428870ff
BB
5879 if (spa_suspended(spa))
5880 ztest_resume(spa);
5881 (void) poll(NULL, 0, 100);
34dc7c2f 5882 }
34dc7c2f 5883
1e33ac1e 5884 thread_exit();
428870ff 5885
1e33ac1e
BB
5886 return (NULL);
5887}
428870ff 5888
d1d7e268 5889#define GRACE 300
428870ff 5890
26099167 5891#if 0
1e33ac1e
BB
5892static void
5893ztest_deadman_alarm(int sig)
5894{
5895 fatal(0, "failed to complete within %d seconds of deadline", GRACE);
428870ff 5896}
26099167 5897#endif
428870ff
BB
5898
5899static void
c242c188 5900ztest_execute(int test, ztest_info_t *zi, uint64_t id)
428870ff 5901{
c242c188
CS
5902 ztest_ds_t *zd = &ztest_ds[id % ztest_opts.zo_datasets];
5903 ztest_shared_callstate_t *zc = ZTEST_GET_SHARED_CALLSTATE(test);
428870ff 5904 hrtime_t functime = gethrtime();
d6320ddb 5905 int i;
428870ff 5906
d6320ddb 5907 for (i = 0; i < zi->zi_iters; i++)
428870ff
BB
5908 zi->zi_func(zd, id);
5909
5910 functime = gethrtime() - functime;
5911
c242c188
CS
5912 atomic_add_64(&zc->zc_count, 1);
5913 atomic_add_64(&zc->zc_time, functime);
428870ff 5914
fdc5d982 5915 if (ztest_opts.zo_verbose >= 4)
428870ff 5916 (void) printf("%6.2f sec in %s\n",
fdc5d982 5917 (double)functime / NANOSEC, zi->zi_funcname);
428870ff
BB
5918}
5919
34dc7c2f
BB
5920static void *
5921ztest_thread(void *arg)
5922{
c242c188 5923 int rand;
428870ff 5924 uint64_t id = (uintptr_t)arg;
34dc7c2f 5925 ztest_shared_t *zs = ztest_shared;
428870ff
BB
5926 uint64_t call_next;
5927 hrtime_t now;
34dc7c2f 5928 ztest_info_t *zi;
c242c188 5929 ztest_shared_callstate_t *zc;
34dc7c2f 5930
428870ff 5931 while ((now = gethrtime()) < zs->zs_thread_stop) {
34dc7c2f
BB
5932 /*
5933 * See if it's time to force a crash.
5934 */
428870ff
BB
5935 if (now > zs->zs_thread_kill)
5936 ztest_kill(zs);
34dc7c2f
BB
5937
5938 /*
428870ff 5939 * If we're getting ENOSPC with some regularity, stop.
34dc7c2f 5940 */
428870ff
BB
5941 if (zs->zs_enospc_count > 10)
5942 break;
34dc7c2f
BB
5943
5944 /*
428870ff 5945 * Pick a random function to execute.
34dc7c2f 5946 */
c242c188
CS
5947 rand = ztest_random(ZTEST_FUNCS);
5948 zi = &ztest_info[rand];
5949 zc = ZTEST_GET_SHARED_CALLSTATE(rand);
5950 call_next = zc->zc_next;
34dc7c2f 5951
428870ff 5952 if (now >= call_next &&
c242c188
CS
5953 atomic_cas_64(&zc->zc_next, call_next, call_next +
5954 ztest_random(2 * zi->zi_interval[0] + 1)) == call_next) {
5955 ztest_execute(rand, zi, id);
5956 }
428870ff 5957 }
34dc7c2f 5958
1e33ac1e
BB
5959 thread_exit();
5960
428870ff
BB
5961 return (NULL);
5962}
34dc7c2f 5963
428870ff
BB
5964static void
5965ztest_dataset_name(char *dsname, char *pool, int d)
5966{
5967 (void) snprintf(dsname, MAXNAMELEN, "%s/ds_%d", pool, d);
5968}
34dc7c2f 5969
428870ff 5970static void
c242c188 5971ztest_dataset_destroy(int d)
428870ff
BB
5972{
5973 char name[MAXNAMELEN];
d6320ddb 5974 int t;
34dc7c2f 5975
c242c188 5976 ztest_dataset_name(name, ztest_opts.zo_pool, d);
34dc7c2f 5977
c242c188 5978 if (ztest_opts.zo_verbose >= 3)
428870ff 5979 (void) printf("Destroying %s to free up space\n", name);
34dc7c2f 5980
428870ff
BB
5981 /*
5982 * Cleanup any non-standard clones and snapshots. In general,
5983 * ztest thread t operates on dataset (t % zopt_datasets),
5984 * so there may be more than one thing to clean up.
5985 */
c242c188
CS
5986 for (t = d; t < ztest_opts.zo_threads;
5987 t += ztest_opts.zo_datasets)
428870ff
BB
5988 ztest_dsl_dataset_cleanup(name, t);
5989
5990 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
5991 DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
5992}
5993
5994static void
5995ztest_dataset_dirobj_verify(ztest_ds_t *zd)
5996{
5997 uint64_t usedobjs, dirobjs, scratch;
5998
5999 /*
6000 * ZTEST_DIROBJ is the object directory for the entire dataset.
6001 * Therefore, the number of objects in use should equal the
6002 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
6003 * If not, we have an object leak.
6004 *
6005 * Note that we can only check this in ztest_dataset_open(),
6006 * when the open-context and syncing-context values agree.
6007 * That's because zap_count() returns the open-context value,
6008 * while dmu_objset_space() returns the rootbp fill count.
6009 */
6010 VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
6011 dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
6012 ASSERT3U(dirobjs + 1, ==, usedobjs);
6013}
6014
6015static int
c242c188 6016ztest_dataset_open(int d)
428870ff 6017{
c242c188
CS
6018 ztest_ds_t *zd = &ztest_ds[d];
6019 uint64_t committed_seq = ZTEST_GET_SHARED_DS(d)->zd_seq;
428870ff
BB
6020 objset_t *os;
6021 zilog_t *zilog;
6022 char name[MAXNAMELEN];
6023 int error;
6024
c242c188 6025 ztest_dataset_name(name, ztest_opts.zo_pool, d);
428870ff 6026
7809eb8b 6027 (void) rw_rdlock(&ztest_name_lock);
428870ff
BB
6028
6029 error = ztest_dataset_create(name);
6030 if (error == ENOSPC) {
7809eb8b 6031 (void) rw_unlock(&ztest_name_lock);
428870ff
BB
6032 ztest_record_enospc(FTAG);
6033 return (error);
34dc7c2f 6034 }
428870ff 6035 ASSERT(error == 0 || error == EEXIST);
34dc7c2f 6036
13fe0198 6037 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, zd, &os));
7809eb8b 6038 (void) rw_unlock(&ztest_name_lock);
428870ff 6039
c242c188 6040 ztest_zd_init(zd, ZTEST_GET_SHARED_DS(d), os);
428870ff
BB
6041
6042 zilog = zd->zd_zilog;
6043
6044 if (zilog->zl_header->zh_claim_lr_seq != 0 &&
6045 zilog->zl_header->zh_claim_lr_seq < committed_seq)
6046 fatal(0, "missing log records: claimed %llu < committed %llu",
6047 zilog->zl_header->zh_claim_lr_seq, committed_seq);
6048
6049 ztest_dataset_dirobj_verify(zd);
6050
6051 zil_replay(os, zd, ztest_replay_vector);
6052
6053 ztest_dataset_dirobj_verify(zd);
6054
c242c188 6055 if (ztest_opts.zo_verbose >= 6)
428870ff
BB
6056 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
6057 zd->zd_name,
6058 (u_longlong_t)zilog->zl_parse_blk_count,
6059 (u_longlong_t)zilog->zl_parse_lr_count,
6060 (u_longlong_t)zilog->zl_replaying_seq);
6061
6062 zilog = zil_open(os, ztest_get_data);
6063
6064 if (zilog->zl_replaying_seq != 0 &&
6065 zilog->zl_replaying_seq < committed_seq)
6066 fatal(0, "missing log records: replayed %llu < committed %llu",
6067 zilog->zl_replaying_seq, committed_seq);
6068
6069 return (0);
6070}
6071
6072static void
c242c188 6073ztest_dataset_close(int d)
428870ff 6074{
c242c188 6075 ztest_ds_t *zd = &ztest_ds[d];
428870ff
BB
6076
6077 zil_close(zd->zd_zilog);
13fe0198 6078 dmu_objset_disown(zd->zd_os, zd);
428870ff
BB
6079
6080 ztest_zd_fini(zd);
34dc7c2f
BB
6081}
6082
6083/*
6084 * Kick off threads to run tests on all datasets in parallel.
6085 */
6086static void
428870ff 6087ztest_run(ztest_shared_t *zs)
34dc7c2f 6088{
1e33ac1e 6089 kt_did_t *tid;
34dc7c2f 6090 spa_t *spa;
3541dc6d 6091 objset_t *os;
1e33ac1e
BB
6092 kthread_t *resume_thread;
6093 uint64_t object;
428870ff 6094 int error;
d6320ddb 6095 int t, d;
b128c09f
BB
6096
6097 ztest_exiting = B_FALSE;
34dc7c2f 6098
34dc7c2f 6099 /*
428870ff 6100 * Initialize parent/child shared state.
34dc7c2f 6101 */
c242c188 6102 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
7809eb8b 6103 VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0);
34dc7c2f 6104
428870ff 6105 zs->zs_thread_start = gethrtime();
c242c188
CS
6106 zs->zs_thread_stop =
6107 zs->zs_thread_start + ztest_opts.zo_passtime * NANOSEC;
428870ff
BB
6108 zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
6109 zs->zs_thread_kill = zs->zs_thread_stop;
c242c188
CS
6110 if (ztest_random(100) < ztest_opts.zo_killrate) {
6111 zs->zs_thread_kill -=
6112 ztest_random(ztest_opts.zo_passtime * NANOSEC);
6113 }
34dc7c2f 6114
1e33ac1e 6115 mutex_init(&zcl.zcl_callbacks_lock, NULL, MUTEX_DEFAULT, NULL);
34dc7c2f 6116
428870ff
BB
6117 list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
6118 offsetof(ztest_cb_data_t, zcd_node));
34dc7c2f
BB
6119
6120 /*
b128c09f 6121 * Open our pool.
34dc7c2f 6122 */
428870ff 6123 kernel_init(FREAD | FWRITE);
13fe0198 6124 VERIFY0(spa_open(ztest_opts.zo_pool, &spa, FTAG));
6d974228 6125 spa->spa_debug = B_TRUE;
080b3100 6126 metaslab_preload_limit = ztest_random(20) + 1;
c242c188 6127 ztest_spa = spa;
428870ff 6128
13fe0198
MA
6129 VERIFY0(dmu_objset_own(ztest_opts.zo_pool,
6130 DMU_OST_ANY, B_TRUE, FTAG, &os));
3541dc6d 6131 zs->zs_guid = dmu_objset_fsid_guid(os);
13fe0198 6132 dmu_objset_disown(os, FTAG);
3541dc6d 6133
428870ff 6134 spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
34dc7c2f 6135
fb5f0bc8
BB
6136 /*
6137 * We don't expect the pool to suspend unless maxfaults == 0,
6138 * in which case ztest_fault_inject() temporarily takes away
6139 * the only valid replica.
6140 */
428870ff 6141 if (MAXFAULTS() == 0)
fb5f0bc8
BB
6142 spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
6143 else
6144 spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
6145
34dc7c2f 6146 /*
b128c09f 6147 * Create a thread to periodically resume suspended I/O.
34dc7c2f 6148 */
0aebd4f9
ED
6149 VERIFY3P((resume_thread = zk_thread_create(NULL, 0,
6150 (thread_func_t)ztest_resume_thread, spa, TS_RUN, NULL, 0, 0,
6151 PTHREAD_CREATE_JOINABLE)), !=, NULL);
34dc7c2f 6152
26099167 6153#if 0
428870ff 6154 /*
1e33ac1e 6155 * Set a deadman alarm to abort() if we hang.
428870ff 6156 */
1e33ac1e
BB
6157 signal(SIGALRM, ztest_deadman_alarm);
6158 alarm((zs->zs_thread_stop - zs->zs_thread_start) / NANOSEC + GRACE);
26099167 6159#endif
428870ff 6160
34dc7c2f
BB
6161 /*
6162 * Verify that we can safely inquire about about any object,
6163 * whether it's allocated or not. To make it interesting,
6164 * we probe a 5-wide window around each power of two.
6165 * This hits all edge cases, including zero and the max.
6166 */
d6320ddb
BB
6167 for (t = 0; t < 64; t++) {
6168 for (d = -5; d <= 5; d++) {
34dc7c2f
BB
6169 error = dmu_object_info(spa->spa_meta_objset,
6170 (1ULL << t) + d, NULL);
6171 ASSERT(error == 0 || error == ENOENT ||
6172 error == EINVAL);
6173 }
6174 }
6175
6176 /*
428870ff 6177 * If we got any ENOSPC errors on the previous run, destroy something.
34dc7c2f 6178 */
428870ff 6179 if (zs->zs_enospc_count != 0) {
c242c188
CS
6180 int d = ztest_random(ztest_opts.zo_datasets);
6181 ztest_dataset_destroy(d);
428870ff 6182 }
34dc7c2f
BB
6183 zs->zs_enospc_count = 0;
6184
c242c188
CS
6185 tid = umem_zalloc(ztest_opts.zo_threads * sizeof (kt_did_t),
6186 UMEM_NOFAIL);
34dc7c2f 6187
c242c188 6188 if (ztest_opts.zo_verbose >= 4)
34dc7c2f
BB
6189 (void) printf("starting main threads...\n");
6190
428870ff
BB
6191 /*
6192 * Kick off all the tests that run in parallel.
6193 */
c242c188 6194 for (t = 0; t < ztest_opts.zo_threads; t++) {
1e33ac1e
BB
6195 kthread_t *thread;
6196
c242c188
CS
6197 if (t < ztest_opts.zo_datasets &&
6198 ztest_dataset_open(t) != 0)
428870ff 6199 return;
1e33ac1e 6200
0aebd4f9
ED
6201 VERIFY3P(thread = zk_thread_create(NULL, 0,
6202 (thread_func_t)ztest_thread,
6203 (void *)(uintptr_t)t, TS_RUN, NULL, 0, 0,
6204 PTHREAD_CREATE_JOINABLE), !=, NULL);
1e33ac1e 6205 tid[t] = thread->t_tid;
34dc7c2f
BB
6206 }
6207
428870ff
BB
6208 /*
6209 * Wait for all of the tests to complete. We go in reverse order
6210 * so we don't close datasets while threads are still using them.
6211 */
c242c188 6212 for (t = ztest_opts.zo_threads - 1; t >= 0; t--) {
1e33ac1e 6213 thread_join(tid[t]);
c242c188
CS
6214 if (t < ztest_opts.zo_datasets)
6215 ztest_dataset_close(t);
34dc7c2f
BB
6216 }
6217
34dc7c2f
BB
6218 txg_wait_synced(spa_get_dsl(spa), 0);
6219
428870ff
BB
6220 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
6221 zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
6222
c242c188 6223 umem_free(tid, ztest_opts.zo_threads * sizeof (kt_did_t));
428870ff
BB
6224
6225 /* Kill the resume thread */
6226 ztest_exiting = B_TRUE;
1e33ac1e 6227 thread_join(resume_thread->t_tid);
428870ff 6228 ztest_resume(spa);
34dc7c2f
BB
6229
6230 /*
428870ff
BB
6231 * Right before closing the pool, kick off a bunch of async I/O;
6232 * spa_close() should wait for it to complete.
34dc7c2f 6233 */
fcff0f35
PD
6234 for (object = 1; object < 50; object++) {
6235 dmu_prefetch(spa->spa_meta_objset, object, 0, 0, 1ULL << 20,
6236 ZIO_PRIORITY_SYNC_READ);
6237 }
428870ff 6238
090ff092
RC
6239 /* Verify that at least one commit cb was called in a timely fashion */
6240 if (zc_cb_counter >= ZTEST_COMMIT_CB_MIN_REG)
c99c9001 6241 VERIFY0(zc_min_txg_delay);
090ff092 6242
428870ff
BB
6243 spa_close(spa, FTAG);
6244
6245 /*
6246 * Verify that we can loop over all pools.
6247 */
6248 mutex_enter(&spa_namespace_lock);
6249 for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
c242c188 6250 if (ztest_opts.zo_verbose > 3)
428870ff
BB
6251 (void) printf("spa_next: found %s\n", spa_name(spa));
6252 mutex_exit(&spa_namespace_lock);
6253
6254 /*
6255 * Verify that we can export the pool and reimport it under a
6256 * different name.
6257 */
6258 if (ztest_random(2) == 0) {
6259 char name[MAXNAMELEN];
c242c188
CS
6260 (void) snprintf(name, MAXNAMELEN, "%s_import",
6261 ztest_opts.zo_pool);
6262 ztest_spa_import_export(ztest_opts.zo_pool, name);
6263 ztest_spa_import_export(name, ztest_opts.zo_pool);
428870ff
BB
6264 }
6265
6266 kernel_fini();
572e2857
BB
6267
6268 list_destroy(&zcl.zcl_callbacks);
0e8d1b2d 6269 mutex_destroy(&zcl.zcl_callbacks_lock);
7809eb8b 6270 (void) rwlock_destroy(&ztest_name_lock);
c242c188 6271 mutex_destroy(&ztest_vdev_lock);
428870ff
BB
6272}
6273
6274static void
c242c188 6275ztest_freeze(void)
428870ff 6276{
c242c188 6277 ztest_ds_t *zd = &ztest_ds[0];
428870ff
BB
6278 spa_t *spa;
6279 int numloops = 0;
6280
c242c188 6281 if (ztest_opts.zo_verbose >= 3)
428870ff 6282 (void) printf("testing spa_freeze()...\n");
9babb374 6283
428870ff 6284 kernel_init(FREAD | FWRITE);
c242c188
CS
6285 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6286 VERIFY3U(0, ==, ztest_dataset_open(0));
03c6040b
GW
6287 spa->spa_debug = B_TRUE;
6288 ztest_spa = spa;
9babb374 6289
428870ff
BB
6290 /*
6291 * Force the first log block to be transactionally allocated.
6292 * We have to do this before we freeze the pool -- otherwise
6293 * the log chain won't be anchored.
6294 */
6295 while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
6296 ztest_dmu_object_alloc_free(zd, 0);
572e2857 6297 zil_commit(zd->zd_zilog, 0);
34dc7c2f
BB
6298 }
6299
6300 txg_wait_synced(spa_get_dsl(spa), 0);
6301
428870ff
BB
6302 /*
6303 * Freeze the pool. This stops spa_sync() from doing anything,
6304 * so that the only way to record changes from now on is the ZIL.
6305 */
6306 spa_freeze(spa);
b128c09f 6307
b02fe35d
AR
6308 /*
6309 * Because it is hard to predict how much space a write will actually
6310 * require beforehand, we leave ourselves some fudge space to write over
6311 * capacity.
6312 */
6313 uint64_t capacity = metaslab_class_get_space(spa_normal_class(spa)) / 2;
6314
428870ff
BB
6315 /*
6316 * Run tests that generate log records but don't alter the pool config
6317 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
6318 * We do a txg_wait_synced() after each iteration to force the txg
6319 * to increase well beyond the last synced value in the uberblock.
6320 * The ZIL should be OK with that.
b02fe35d
AR
6321 *
6322 * Run a random number of times less than zo_maxloops and ensure we do
6323 * not run out of space on the pool.
428870ff 6324 */
c242c188 6325 while (ztest_random(10) != 0 &&
b02fe35d
AR
6326 numloops++ < ztest_opts.zo_maxloops &&
6327 metaslab_class_get_alloc(spa_normal_class(spa)) < capacity) {
6328 ztest_od_t od;
50c957f7 6329 ztest_od_init(&od, 0, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
b02fe35d
AR
6330 VERIFY0(ztest_object_init(zd, &od, sizeof (od), B_FALSE));
6331 ztest_io(zd, od.od_object,
6332 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
428870ff
BB
6333 txg_wait_synced(spa_get_dsl(spa), 0);
6334 }
b128c09f 6335
34dc7c2f 6336 /*
428870ff 6337 * Commit all of the changes we just generated.
34dc7c2f 6338 */
572e2857 6339 zil_commit(zd->zd_zilog, 0);
428870ff 6340 txg_wait_synced(spa_get_dsl(spa), 0);
34dc7c2f 6341
428870ff
BB
6342 /*
6343 * Close our dataset and close the pool.
6344 */
c242c188 6345 ztest_dataset_close(0);
34dc7c2f 6346 spa_close(spa, FTAG);
428870ff 6347 kernel_fini();
34dc7c2f 6348
428870ff
BB
6349 /*
6350 * Open and close the pool and dataset to induce log replay.
6351 */
6352 kernel_init(FREAD | FWRITE);
c242c188 6353 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
29809a6c 6354 ASSERT(spa_freeze_txg(spa) == UINT64_MAX);
c242c188
CS
6355 VERIFY3U(0, ==, ztest_dataset_open(0));
6356 ztest_dataset_close(0);
3bc7e0fb
GW
6357
6358 spa->spa_debug = B_TRUE;
6359 ztest_spa = spa;
6360 txg_wait_synced(spa_get_dsl(spa), 0);
6361 ztest_reguid(NULL, 0);
6362
428870ff 6363 spa_close(spa, FTAG);
34dc7c2f
BB
6364 kernel_fini();
6365}
6366
6367void
6368print_time(hrtime_t t, char *timebuf)
6369{
6370 hrtime_t s = t / NANOSEC;
6371 hrtime_t m = s / 60;
6372 hrtime_t h = m / 60;
6373 hrtime_t d = h / 24;
6374
6375 s -= m * 60;
6376 m -= h * 60;
6377 h -= d * 24;
6378
6379 timebuf[0] = '\0';
6380
6381 if (d)
6382 (void) sprintf(timebuf,
6383 "%llud%02lluh%02llum%02llus", d, h, m, s);
6384 else if (h)
6385 (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
6386 else if (m)
6387 (void) sprintf(timebuf, "%llum%02llus", m, s);
6388 else
6389 (void) sprintf(timebuf, "%llus", s);
6390}
6391
428870ff 6392static nvlist_t *
0bc8fd78 6393make_random_props(void)
428870ff
BB
6394{
6395 nvlist_t *props;
6396
428870ff 6397 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
9ae529ec
CS
6398 if (ztest_random(2) == 0)
6399 return (props);
428870ff
BB
6400 VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
6401
428870ff
BB
6402 return (props);
6403}
6404
34dc7c2f
BB
6405/*
6406 * Create a storage pool with the given name and initial vdev size.
428870ff 6407 * Then test spa_freeze() functionality.
34dc7c2f
BB
6408 */
6409static void
428870ff 6410ztest_init(ztest_shared_t *zs)
34dc7c2f
BB
6411{
6412 spa_t *spa;
428870ff 6413 nvlist_t *nvroot, *props;
9ae529ec 6414 int i;
428870ff 6415
c242c188 6416 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
7809eb8b 6417 VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0);
34dc7c2f
BB
6418
6419 kernel_init(FREAD | FWRITE);
6420
6421 /*
6422 * Create the storage pool.
6423 */
c242c188 6424 (void) spa_destroy(ztest_opts.zo_pool);
428870ff
BB
6425 ztest_shared->zs_vdev_next_leaf = 0;
6426 zs->zs_splits = 0;
c242c188 6427 zs->zs_mirrors = ztest_opts.zo_mirrors;
ea0b2538 6428 nvroot = make_vdev_root(NULL, NULL, NULL, ztest_opts.zo_vdev_size, 0,
c242c188 6429 0, ztest_opts.zo_raidz, zs->zs_mirrors, 1);
428870ff 6430 props = make_random_props();
9ae529ec
CS
6431 for (i = 0; i < SPA_FEATURES; i++) {
6432 char *buf;
6433 VERIFY3S(-1, !=, asprintf(&buf, "feature@%s",
6434 spa_feature_table[i].fi_uname));
6435 VERIFY3U(0, ==, nvlist_add_uint64(props, buf, 0));
6436 free(buf);
6437 }
6f1ffb06 6438 VERIFY3U(0, ==, spa_create(ztest_opts.zo_pool, nvroot, props, NULL));
34dc7c2f 6439 nvlist_free(nvroot);
85802aa4 6440 nvlist_free(props);
34dc7c2f 6441
c242c188
CS
6442 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6443 zs->zs_metaslab_sz =
6444 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
34dc7c2f
BB
6445 spa_close(spa, FTAG);
6446
6447 kernel_fini();
428870ff 6448
c242c188
CS
6449 ztest_run_zdb(ztest_opts.zo_pool);
6450
6451 ztest_freeze();
6452
6453 ztest_run_zdb(ztest_opts.zo_pool);
6454
7809eb8b 6455 (void) rwlock_destroy(&ztest_name_lock);
c242c188
CS
6456 mutex_destroy(&ztest_vdev_lock);
6457}
6458
6459static void
9d81146b 6460setup_data_fd(void)
c242c188 6461{
facbbe43
BB
6462 static char ztest_name_data[] = "/tmp/ztest.data.XXXXXX";
6463
6464 ztest_fd_data = mkstemp(ztest_name_data);
9d81146b 6465 ASSERT3S(ztest_fd_data, >=, 0);
facbbe43 6466 (void) unlink(ztest_name_data);
c242c188
CS
6467}
6468
22257dc0
CS
6469static int
6470shared_data_size(ztest_shared_hdr_t *hdr)
6471{
6472 int size;
6473
6474 size = hdr->zh_hdr_size;
6475 size += hdr->zh_opts_size;
6476 size += hdr->zh_size;
6477 size += hdr->zh_stats_size * hdr->zh_stats_count;
6478 size += hdr->zh_ds_size * hdr->zh_ds_count;
6479
6480 return (size);
6481}
6482
c242c188
CS
6483static void
6484setup_hdr(void)
6485{
22257dc0 6486 int size;
c242c188
CS
6487 ztest_shared_hdr_t *hdr;
6488
6489 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
9d81146b 6490 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
799402d8 6491 ASSERT(hdr != MAP_FAILED);
c242c188 6492
9d81146b 6493 VERIFY3U(0, ==, ftruncate(ztest_fd_data, sizeof (ztest_shared_hdr_t)));
22257dc0 6494
c242c188
CS
6495 hdr->zh_hdr_size = sizeof (ztest_shared_hdr_t);
6496 hdr->zh_opts_size = sizeof (ztest_shared_opts_t);
6497 hdr->zh_size = sizeof (ztest_shared_t);
6498 hdr->zh_stats_size = sizeof (ztest_shared_callstate_t);
6499 hdr->zh_stats_count = ZTEST_FUNCS;
6500 hdr->zh_ds_size = sizeof (ztest_shared_ds_t);
6501 hdr->zh_ds_count = ztest_opts.zo_datasets;
6502
22257dc0 6503 size = shared_data_size(hdr);
9d81146b 6504 VERIFY3U(0, ==, ftruncate(ztest_fd_data, size));
22257dc0 6505
c242c188
CS
6506 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6507}
6508
6509static void
6510setup_data(void)
6511{
6512 int size, offset;
6513 ztest_shared_hdr_t *hdr;
6514 uint8_t *buf;
6515
6516 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
9d81146b 6517 PROT_READ, MAP_SHARED, ztest_fd_data, 0);
799402d8 6518 ASSERT(hdr != MAP_FAILED);
c242c188 6519
22257dc0 6520 size = shared_data_size(hdr);
c242c188
CS
6521
6522 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6523 hdr = ztest_shared_hdr = (void *)mmap(0, P2ROUNDUP(size, getpagesize()),
9d81146b 6524 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
799402d8 6525 ASSERT(hdr != MAP_FAILED);
c242c188
CS
6526 buf = (uint8_t *)hdr;
6527
6528 offset = hdr->zh_hdr_size;
6529 ztest_shared_opts = (void *)&buf[offset];
6530 offset += hdr->zh_opts_size;
6531 ztest_shared = (void *)&buf[offset];
6532 offset += hdr->zh_size;
6533 ztest_shared_callstate = (void *)&buf[offset];
6534 offset += hdr->zh_stats_size * hdr->zh_stats_count;
6535 ztest_shared_ds = (void *)&buf[offset];
6536}
6537
6538static boolean_t
6539exec_child(char *cmd, char *libpath, boolean_t ignorekill, int *statusp)
6540{
6541 pid_t pid;
6542 int status;
483106eb 6543 char *cmdbuf = NULL;
c242c188
CS
6544
6545 pid = fork();
6546
6547 if (cmd == NULL) {
483106eb
BB
6548 cmdbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
6549 (void) strlcpy(cmdbuf, getexecname(), MAXPATHLEN);
c242c188
CS
6550 cmd = cmdbuf;
6551 }
6552
6553 if (pid == -1)
6554 fatal(1, "fork failed");
6555
6556 if (pid == 0) { /* child */
6557 char *emptyargv[2] = { cmd, NULL };
9d81146b 6558 char fd_data_str[12];
c242c188
CS
6559
6560 struct rlimit rl = { 1024, 1024 };
6561 (void) setrlimit(RLIMIT_NOFILE, &rl);
9d81146b
ED
6562
6563 (void) close(ztest_fd_rand);
6564 VERIFY(11 >= snprintf(fd_data_str, 12, "%d", ztest_fd_data));
6565 VERIFY(0 == setenv("ZTEST_FD_DATA", fd_data_str, 1));
6566
c242c188
CS
6567 (void) enable_extended_FILE_stdio(-1, -1);
6568 if (libpath != NULL)
6569 VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath, 1));
6570 (void) execv(cmd, emptyargv);
6571 ztest_dump_core = B_FALSE;
6572 fatal(B_TRUE, "exec failed: %s", cmd);
6573 }
6574
483106eb
BB
6575 if (cmdbuf != NULL) {
6576 umem_free(cmdbuf, MAXPATHLEN);
6577 cmd = NULL;
6578 }
6579
c242c188
CS
6580 while (waitpid(pid, &status, 0) != pid)
6581 continue;
6582 if (statusp != NULL)
6583 *statusp = status;
6584
6585 if (WIFEXITED(status)) {
6586 if (WEXITSTATUS(status) != 0) {
6587 (void) fprintf(stderr, "child exited with code %d\n",
6588 WEXITSTATUS(status));
6589 exit(2);
6590 }
6591 return (B_FALSE);
6592 } else if (WIFSIGNALED(status)) {
6593 if (!ignorekill || WTERMSIG(status) != SIGKILL) {
6594 (void) fprintf(stderr, "child died with signal %d\n",
6595 WTERMSIG(status));
6596 exit(3);
6597 }
6598 return (B_TRUE);
6599 } else {
6600 (void) fprintf(stderr, "something strange happened to child\n");
6601 exit(4);
6602 /* NOTREACHED */
6603 }
6604}
6605
6606static void
6607ztest_run_init(void)
6608{
6609 int i;
428870ff 6610
c242c188 6611 ztest_shared_t *zs = ztest_shared;
428870ff 6612
c242c188 6613 ASSERT(ztest_opts.zo_init != 0);
572e2857 6614
c242c188
CS
6615 /*
6616 * Blow away any existing copy of zpool.cache
6617 */
6618 (void) remove(spa_config_path);
6619
6620 /*
6621 * Create and initialize our storage pool.
6622 */
6623 for (i = 1; i <= ztest_opts.zo_init; i++) {
6624 bzero(zs, sizeof (ztest_shared_t));
6625 if (ztest_opts.zo_verbose >= 3 &&
6626 ztest_opts.zo_init != 1) {
6627 (void) printf("ztest_init(), pass %d\n", i);
6628 }
6629 ztest_init(zs);
6630 }
34dc7c2f
BB
6631}
6632
6633int
6634main(int argc, char **argv)
6635{
6636 int kills = 0;
6637 int iters = 0;
c242c188
CS
6638 int older = 0;
6639 int newer = 0;
34dc7c2f
BB
6640 ztest_shared_t *zs;
6641 ztest_info_t *zi;
c242c188 6642 ztest_shared_callstate_t *zc;
34dc7c2f
BB
6643 char timebuf[100];
6644 char numbuf[6];
428870ff 6645 spa_t *spa;
483106eb 6646 char *cmd;
c242c188
CS
6647 boolean_t hasalt;
6648 int f;
9d81146b 6649 char *fd_data_str = getenv("ZTEST_FD_DATA");
e82cdc3a 6650 struct sigaction action;
34dc7c2f
BB
6651
6652 (void) setvbuf(stdout, NULL, _IOLBF, 0);
6653
498877ba
MA
6654 dprintf_setup(&argc, argv);
6655
e82cdc3a
NB
6656 action.sa_handler = sig_handler;
6657 sigemptyset(&action.sa_mask);
6658 action.sa_flags = 0;
6659
6660 if (sigaction(SIGSEGV, &action, NULL) < 0) {
6661 (void) fprintf(stderr, "ztest: cannot catch SIGSEGV: %s.\n",
6662 strerror(errno));
6663 exit(EXIT_FAILURE);
6664 }
6665
6666 if (sigaction(SIGABRT, &action, NULL) < 0) {
6667 (void) fprintf(stderr, "ztest: cannot catch SIGABRT: %s.\n",
6668 strerror(errno));
6669 exit(EXIT_FAILURE);
6670 }
6671
9d81146b
ED
6672 ztest_fd_rand = open("/dev/urandom", O_RDONLY);
6673 ASSERT3S(ztest_fd_rand, >=, 0);
6674
6675 if (!fd_data_str) {
c242c188 6676 process_options(argc, argv);
34dc7c2f 6677
9d81146b 6678 setup_data_fd();
c242c188
CS
6679 setup_hdr();
6680 setup_data();
6681 bcopy(&ztest_opts, ztest_shared_opts,
6682 sizeof (*ztest_shared_opts));
6683 } else {
9d81146b 6684 ztest_fd_data = atoi(fd_data_str);
c242c188
CS
6685 setup_data();
6686 bcopy(ztest_shared_opts, &ztest_opts, sizeof (ztest_opts));
6687 }
6688 ASSERT3U(ztest_opts.zo_datasets, ==, ztest_shared_hdr->zh_ds_count);
34dc7c2f 6689
428870ff 6690 /* Override location of zpool.cache */
5be98cfe
BB
6691 VERIFY(asprintf((char **)&spa_config_path, "%s/zpool.cache",
6692 ztest_opts.zo_dir) != -1);
9d81146b 6693
c242c188
CS
6694 ztest_ds = umem_alloc(ztest_opts.zo_datasets * sizeof (ztest_ds_t),
6695 UMEM_NOFAIL);
6696 zs = ztest_shared;
6697
9d81146b 6698 if (fd_data_str) {
c242c188
CS
6699 metaslab_gang_bang = ztest_opts.zo_metaslab_gang_bang;
6700 metaslab_df_alloc_threshold =
6701 zs->zs_metaslab_df_alloc_threshold;
6702
6703 if (zs->zs_do_init)
6704 ztest_run_init();
6705 else
6706 ztest_run(zs);
6707 exit(0);
6708 }
34dc7c2f 6709
c242c188 6710 hasalt = (strlen(ztest_opts.zo_alt_ztest) != 0);
34dc7c2f 6711
c242c188 6712 if (ztest_opts.zo_verbose >= 1) {
34dc7c2f
BB
6713 (void) printf("%llu vdevs, %d datasets, %d threads,"
6714 " %llu seconds...\n",
c242c188
CS
6715 (u_longlong_t)ztest_opts.zo_vdevs,
6716 ztest_opts.zo_datasets,
6717 ztest_opts.zo_threads,
6718 (u_longlong_t)ztest_opts.zo_time);
34dc7c2f
BB
6719 }
6720
483106eb
BB
6721 cmd = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
6722 (void) strlcpy(cmd, getexecname(), MAXNAMELEN);
c242c188
CS
6723
6724 zs->zs_do_init = B_TRUE;
6725 if (strlen(ztest_opts.zo_alt_ztest) != 0) {
6726 if (ztest_opts.zo_verbose >= 1) {
6727 (void) printf("Executing older ztest for "
6728 "initialization: %s\n", ztest_opts.zo_alt_ztest);
6729 }
6730 VERIFY(!exec_child(ztest_opts.zo_alt_ztest,
6731 ztest_opts.zo_alt_libpath, B_FALSE, NULL));
6732 } else {
6733 VERIFY(!exec_child(NULL, NULL, B_FALSE, NULL));
34dc7c2f 6734 }
c242c188 6735 zs->zs_do_init = B_FALSE;
34dc7c2f 6736
428870ff 6737 zs->zs_proc_start = gethrtime();
c242c188 6738 zs->zs_proc_stop = zs->zs_proc_start + ztest_opts.zo_time * NANOSEC;
34dc7c2f 6739
d6320ddb 6740 for (f = 0; f < ZTEST_FUNCS; f++) {
c242c188
CS
6741 zi = &ztest_info[f];
6742 zc = ZTEST_GET_SHARED_CALLSTATE(f);
428870ff 6743 if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
c242c188 6744 zc->zc_next = UINT64_MAX;
34dc7c2f 6745 else
c242c188 6746 zc->zc_next = zs->zs_proc_start +
428870ff 6747 ztest_random(2 * zi->zi_interval[0] + 1);
34dc7c2f
BB
6748 }
6749
34dc7c2f
BB
6750 /*
6751 * Run the tests in a loop. These tests include fault injection
6752 * to verify that self-healing data works, and forced crashes
6753 * to verify that we never lose on-disk consistency.
6754 */
428870ff 6755 while (gethrtime() < zs->zs_proc_stop) {
34dc7c2f 6756 int status;
c242c188 6757 boolean_t killed;
34dc7c2f
BB
6758
6759 /*
6760 * Initialize the workload counters for each function.
6761 */
d6320ddb 6762 for (f = 0; f < ZTEST_FUNCS; f++) {
c242c188
CS
6763 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6764 zc->zc_count = 0;
6765 zc->zc_time = 0;
34dc7c2f
BB
6766 }
6767
9babb374 6768 /* Set the allocation switch size */
c242c188
CS
6769 zs->zs_metaslab_df_alloc_threshold =
6770 ztest_random(zs->zs_metaslab_sz / 4) + 1;
9babb374 6771
c242c188
CS
6772 if (!hasalt || ztest_random(2) == 0) {
6773 if (hasalt && ztest_opts.zo_verbose >= 1) {
6774 (void) printf("Executing newer ztest: %s\n",
6775 cmd);
34dc7c2f 6776 }
c242c188
CS
6777 newer++;
6778 killed = exec_child(cmd, NULL, B_TRUE, &status);
34dc7c2f 6779 } else {
c242c188
CS
6780 if (hasalt && ztest_opts.zo_verbose >= 1) {
6781 (void) printf("Executing older ztest: %s\n",
6782 ztest_opts.zo_alt_ztest);
6783 }
6784 older++;
6785 killed = exec_child(ztest_opts.zo_alt_ztest,
6786 ztest_opts.zo_alt_libpath, B_TRUE, &status);
34dc7c2f
BB
6787 }
6788
c242c188
CS
6789 if (killed)
6790 kills++;
34dc7c2f
BB
6791 iters++;
6792
c242c188 6793 if (ztest_opts.zo_verbose >= 1) {
34dc7c2f
BB
6794 hrtime_t now = gethrtime();
6795
428870ff
BB
6796 now = MIN(now, zs->zs_proc_stop);
6797 print_time(zs->zs_proc_stop - now, timebuf);
34dc7c2f
BB
6798 nicenum(zs->zs_space, numbuf);
6799
6800 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
6801 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
6802 iters,
6803 WIFEXITED(status) ? "Complete" : "SIGKILL",
6804 (u_longlong_t)zs->zs_enospc_count,
6805 100.0 * zs->zs_alloc / zs->zs_space,
6806 numbuf,
428870ff 6807 100.0 * (now - zs->zs_proc_start) /
c242c188 6808 (ztest_opts.zo_time * NANOSEC), timebuf);
34dc7c2f
BB
6809 }
6810
c242c188 6811 if (ztest_opts.zo_verbose >= 2) {
34dc7c2f
BB
6812 (void) printf("\nWorkload summary:\n\n");
6813 (void) printf("%7s %9s %s\n",
6814 "Calls", "Time", "Function");
6815 (void) printf("%7s %9s %s\n",
6816 "-----", "----", "--------");
d6320ddb 6817 for (f = 0; f < ZTEST_FUNCS; f++) {
c242c188
CS
6818 zi = &ztest_info[f];
6819 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6820 print_time(zc->zc_time, timebuf);
34dc7c2f 6821 (void) printf("%7llu %9s %s\n",
c242c188 6822 (u_longlong_t)zc->zc_count, timebuf,
fdc5d982 6823 zi->zi_funcname);
34dc7c2f
BB
6824 }
6825 (void) printf("\n");
6826 }
6827
6828 /*
428870ff
BB
6829 * It's possible that we killed a child during a rename test,
6830 * in which case we'll have a 'ztest_tmp' pool lying around
6831 * instead of 'ztest'. Do a blind rename in case this happened.
34dc7c2f 6832 */
428870ff 6833 kernel_init(FREAD);
c242c188 6834 if (spa_open(ztest_opts.zo_pool, &spa, FTAG) == 0) {
428870ff
BB
6835 spa_close(spa, FTAG);
6836 } else {
6837 char tmpname[MAXNAMELEN];
6838 kernel_fini();
6839 kernel_init(FREAD | FWRITE);
6840 (void) snprintf(tmpname, sizeof (tmpname), "%s_tmp",
c242c188
CS
6841 ztest_opts.zo_pool);
6842 (void) spa_rename(tmpname, ztest_opts.zo_pool);
428870ff 6843 }
34dc7c2f 6844 kernel_fini();
34dc7c2f 6845
c242c188 6846 ztest_run_zdb(ztest_opts.zo_pool);
428870ff 6847 }
34dc7c2f 6848
c242c188
CS
6849 if (ztest_opts.zo_verbose >= 1) {
6850 if (hasalt) {
6851 (void) printf("%d runs of older ztest: %s\n", older,
6852 ztest_opts.zo_alt_ztest);
6853 (void) printf("%d runs of newer ztest: %s\n", newer,
6854 cmd);
6855 }
34dc7c2f
BB
6856 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
6857 kills, iters - kills, (100.0 * kills) / MAX(1, iters));
6858 }
6859
483106eb
BB
6860 umem_free(cmd, MAXNAMELEN);
6861
34dc7c2f
BB
6862 return (0);
6863}