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