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