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