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