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