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