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