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