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