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