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