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