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