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