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