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