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