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