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