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