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