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