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 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25
26 /*
27 * The objective of this program is to provide a DMU/ZAP/SPA stress test
28 * that runs entirely in userland, is easy to use, and easy to extend.
29 *
30 * The overall design of the ztest program is as follows:
31 *
32 * (1) For each major functional area (e.g. adding vdevs to a pool,
33 * creating and destroying datasets, reading and writing objects, etc)
34 * we have a simple routine to test that functionality. These
35 * individual routines do not have to do anything "stressful".
36 *
37 * (2) We turn these simple functionality tests into a stress test by
38 * running them all in parallel, with as many threads as desired,
39 * and spread across as many datasets, objects, and vdevs as desired.
40 *
41 * (3) While all this is happening, we inject faults into the pool to
42 * verify that self-healing data really works.
43 *
44 * (4) Every time we open a dataset, we change its checksum and compression
45 * functions. Thus even individual objects vary from block to block
46 * in which checksum they use and whether they're compressed.
47 *
48 * (5) To verify that we never lose on-disk consistency after a crash,
49 * we run the entire test in a child of the main process.
50 * At random times, the child self-immolates with a SIGKILL.
51 * This is the software equivalent of pulling the power cord.
52 * The parent then runs the test again, using the existing
53 * storage pool, as many times as desired.
54 *
55 * (6) To verify that we don't have future leaks or temporal incursions,
56 * many of the functional tests record the transaction group number
57 * as part of their data. When reading old data, they verify that
58 * the transaction group number is less than the current, open txg.
59 * If you add a new test, please do this if applicable.
60 *
61 * When run with no arguments, ztest runs for about five minutes and
62 * produces no output if successful. To get a little bit of information,
63 * specify -V. To get more information, specify -VV, and so on.
64 *
65 * To turn this into an overnight stress test, use -T to specify run time.
66 *
67 * You can ask more more vdevs [-v], datasets [-d], or threads [-t]
68 * to increase the pool capacity, fanout, and overall stress level.
69 *
70 * The -N(okill) option will suppress kills, so each child runs to completion.
71 * This can be useful when you're trying to distinguish temporal incursions
72 * from plain old race conditions.
73 */
74
75 #include <sys/zfs_context.h>
76 #include <sys/spa.h>
77 #include <sys/dmu.h>
78 #include <sys/txg.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/zio_checksum.h>
89 #include <sys/zio_compress.h>
90 #include <sys/zio_crypt.h>
91 #include <sys/zil.h>
92 #include <sys/vdev_impl.h>
93 #include <sys/vdev_file.h>
94 #include <sys/spa_impl.h>
95 #include <sys/dsl_prop.h>
96 #include <sys/refcount.h>
97 #include <stdio.h>
98 #include <stdio_ext.h>
99 #include <stdlib.h>
100 #include <unistd.h>
101 #include <signal.h>
102 #include <umem.h>
103 #include <dlfcn.h>
104 #include <ctype.h>
105 #include <math.h>
106 #include <sys/fs/zfs.h>
107
108 static char cmdname[] = "ztest";
109 static char *zopt_pool = cmdname;
110
111 static uint64_t zopt_vdevs = 5;
112 static uint64_t zopt_vdevtime;
113 static int zopt_ashift = SPA_MINBLOCKSHIFT;
114 static int zopt_mirrors = 2;
115 static int zopt_raidz = 4;
116 static int zopt_raidz_parity = 1;
117 static size_t zopt_vdev_size = SPA_MINDEVSIZE;
118 static int zopt_datasets = 7;
119 static int zopt_threads = 23;
120 static uint64_t zopt_passtime = 60; /* 60 seconds */
121 static uint64_t zopt_killrate = 70; /* 70% kill rate */
122 static int zopt_verbose = 0;
123 static int zopt_init = 1;
124 static char *zopt_dir = "/tmp";
125 static uint64_t zopt_time = 300; /* 5 minutes */
126 static int zopt_maxfaults;
127
128 typedef struct ztest_block_tag {
129 uint64_t bt_objset;
130 uint64_t bt_object;
131 uint64_t bt_offset;
132 uint64_t bt_txg;
133 uint64_t bt_thread;
134 uint64_t bt_seq;
135 } ztest_block_tag_t;
136
137 typedef struct ztest_args {
138 char za_pool[MAXNAMELEN];
139 spa_t *za_spa;
140 objset_t *za_os;
141 zilog_t *za_zilog;
142 thread_t za_thread;
143 uint64_t za_instance;
144 uint64_t za_random;
145 uint64_t za_diroff;
146 uint64_t za_diroff_shared;
147 uint64_t za_zil_seq;
148 hrtime_t za_start;
149 hrtime_t za_stop;
150 hrtime_t za_kill;
151 /*
152 * Thread-local variables can go here to aid debugging.
153 */
154 ztest_block_tag_t za_rbt;
155 ztest_block_tag_t za_wbt;
156 dmu_object_info_t za_doi;
157 dmu_buf_t *za_dbuf;
158 } ztest_args_t;
159
160 typedef void ztest_func_t(ztest_args_t *);
161
162 /*
163 * Note: these aren't static because we want dladdr() to work.
164 */
165 ztest_func_t ztest_dmu_read_write;
166 ztest_func_t ztest_dmu_write_parallel;
167 ztest_func_t ztest_dmu_object_alloc_free;
168 ztest_func_t ztest_zap;
169 ztest_func_t ztest_zap_parallel;
170 ztest_func_t ztest_traverse;
171 ztest_func_t ztest_dsl_prop_get_set;
172 ztest_func_t ztest_dmu_objset_create_destroy;
173 ztest_func_t ztest_dmu_snapshot_create_destroy;
174 ztest_func_t ztest_spa_create_destroy;
175 ztest_func_t ztest_fault_inject;
176 ztest_func_t ztest_spa_rename;
177 ztest_func_t ztest_vdev_attach_detach;
178 ztest_func_t ztest_vdev_LUN_growth;
179 ztest_func_t ztest_vdev_add_remove;
180 ztest_func_t ztest_vdev_aux_add_remove;
181 ztest_func_t ztest_scrub;
182
183 typedef struct ztest_info {
184 ztest_func_t *zi_func; /* test function */
185 uint64_t zi_iters; /* iterations per execution */
186 uint64_t *zi_interval; /* execute every <interval> seconds */
187 uint64_t zi_calls; /* per-pass count */
188 uint64_t zi_call_time; /* per-pass time */
189 uint64_t zi_call_total; /* cumulative total */
190 uint64_t zi_call_target; /* target cumulative total */
191 } ztest_info_t;
192
193 uint64_t zopt_always = 0; /* all the time */
194 uint64_t zopt_often = 1; /* every second */
195 uint64_t zopt_sometimes = 10; /* every 10 seconds */
196 uint64_t zopt_rarely = 60; /* every 60 seconds */
197
198 ztest_info_t ztest_info[] = {
199 { ztest_dmu_read_write, 1, &zopt_always },
200 { ztest_dmu_write_parallel, 30, &zopt_always },
201 { ztest_dmu_object_alloc_free, 1, &zopt_always },
202 { ztest_zap, 30, &zopt_always },
203 { ztest_zap_parallel, 100, &zopt_always },
204 { ztest_dsl_prop_get_set, 1, &zopt_sometimes },
205 { ztest_dmu_objset_create_destroy, 1, &zopt_sometimes },
206 { ztest_dmu_snapshot_create_destroy, 1, &zopt_sometimes },
207 { ztest_spa_create_destroy, 1, &zopt_sometimes },
208 { ztest_fault_inject, 1, &zopt_sometimes },
209 { ztest_spa_rename, 1, &zopt_rarely },
210 { ztest_vdev_attach_detach, 1, &zopt_rarely },
211 { ztest_vdev_LUN_growth, 1, &zopt_rarely },
212 { ztest_vdev_add_remove, 1, &zopt_vdevtime },
213 { ztest_vdev_aux_add_remove, 1, &zopt_vdevtime },
214 { ztest_scrub, 1, &zopt_vdevtime },
215 };
216
217 #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
218
219 #define ZTEST_SYNC_LOCKS 16
220
221 /*
222 * Stuff we need to share writably between parent and child.
223 */
224 typedef struct ztest_shared {
225 mutex_t zs_vdev_lock;
226 rwlock_t zs_name_lock;
227 uint64_t zs_vdev_primaries;
228 uint64_t zs_vdev_aux;
229 uint64_t zs_enospc_count;
230 hrtime_t zs_start_time;
231 hrtime_t zs_stop_time;
232 uint64_t zs_alloc;
233 uint64_t zs_space;
234 ztest_info_t zs_info[ZTEST_FUNCS];
235 mutex_t zs_sync_lock[ZTEST_SYNC_LOCKS];
236 uint64_t zs_seq[ZTEST_SYNC_LOCKS];
237 } ztest_shared_t;
238
239 static char ztest_dev_template[] = "%s/%s.%llua";
240 static char ztest_aux_template[] = "%s/%s.%s.%llu";
241 static ztest_shared_t *ztest_shared;
242
243 static int ztest_random_fd;
244 static int ztest_dump_core = 1;
245
246 static boolean_t ztest_exiting;
247
248 extern uint64_t metaslab_gang_bang;
249
250 #define ZTEST_DIROBJ 1
251 #define ZTEST_MICROZAP_OBJ 2
252 #define ZTEST_FATZAP_OBJ 3
253
254 #define ZTEST_DIROBJ_BLOCKSIZE (1 << 10)
255 #define ZTEST_DIRSIZE 256
256
257 static void usage(boolean_t) __NORETURN;
258
259 /*
260 * These libumem hooks provide a reasonable set of defaults for the allocator's
261 * debugging facilities.
262 */
263 const char *
264 _umem_debug_init()
265 {
266 return ("default,verbose"); /* $UMEM_DEBUG setting */
267 }
268
269 const char *
270 _umem_logging_init(void)
271 {
272 return ("fail,contents"); /* $UMEM_LOGGING setting */
273 }
274
275 #define FATAL_MSG_SZ 1024
276
277 char *fatal_msg;
278
279 static void
280 fatal(int do_perror, char *message, ...)
281 {
282 va_list args;
283 int save_errno = errno;
284 char buf[FATAL_MSG_SZ];
285
286 (void) fflush(stdout);
287
288 va_start(args, message);
289 (void) sprintf(buf, "ztest: ");
290 /* LINTED */
291 (void) vsprintf(buf + strlen(buf), message, args);
292 va_end(args);
293 if (do_perror) {
294 (void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
295 ": %s", strerror(save_errno));
296 }
297 (void) fprintf(stderr, "%s\n", buf);
298 fatal_msg = buf; /* to ease debugging */
299 if (ztest_dump_core)
300 abort();
301 exit(3);
302 }
303
304 static int
305 str2shift(const char *buf)
306 {
307 const char *ends = "BKMGTPEZ";
308 int i;
309
310 if (buf[0] == '\0')
311 return (0);
312 for (i = 0; i < strlen(ends); i++) {
313 if (toupper(buf[0]) == ends[i])
314 break;
315 }
316 if (i == strlen(ends)) {
317 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
318 buf);
319 usage(B_FALSE);
320 }
321 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
322 return (10*i);
323 }
324 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
325 usage(B_FALSE);
326 /* NOTREACHED */
327 }
328
329 static uint64_t
330 nicenumtoull(const char *buf)
331 {
332 char *end;
333 uint64_t val;
334
335 val = strtoull(buf, &end, 0);
336 if (end == buf) {
337 (void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
338 usage(B_FALSE);
339 } else if (end[0] == '.') {
340 double fval = strtod(buf, &end);
341 fval *= pow(2, str2shift(end));
342 if (fval > UINT64_MAX) {
343 (void) fprintf(stderr, "ztest: value too large: %s\n",
344 buf);
345 usage(B_FALSE);
346 }
347 val = (uint64_t)fval;
348 } else {
349 int shift = str2shift(end);
350 if (shift >= 64 || (val << shift) >> shift != val) {
351 (void) fprintf(stderr, "ztest: value too large: %s\n",
352 buf);
353 usage(B_FALSE);
354 }
355 val <<= shift;
356 }
357 return (val);
358 }
359
360 static void
361 usage(boolean_t requested)
362 {
363 char nice_vdev_size[10];
364 char nice_gang_bang[10];
365 FILE *fp = requested ? stdout : stderr;
366
367 nicenum(zopt_vdev_size, nice_vdev_size);
368 nicenum(metaslab_gang_bang, nice_gang_bang);
369
370 (void) fprintf(fp, "Usage: %s\n"
371 "\t[-v vdevs (default: %llu)]\n"
372 "\t[-s size_of_each_vdev (default: %s)]\n"
373 "\t[-a alignment_shift (default: %d) (use 0 for random)]\n"
374 "\t[-m mirror_copies (default: %d)]\n"
375 "\t[-r raidz_disks (default: %d)]\n"
376 "\t[-R raidz_parity (default: %d)]\n"
377 "\t[-d datasets (default: %d)]\n"
378 "\t[-t threads (default: %d)]\n"
379 "\t[-g gang_block_threshold (default: %s)]\n"
380 "\t[-i initialize pool i times (default: %d)]\n"
381 "\t[-k kill percentage (default: %llu%%)]\n"
382 "\t[-p pool_name (default: %s)]\n"
383 "\t[-f file directory for vdev files (default: %s)]\n"
384 "\t[-V(erbose)] (use multiple times for ever more blather)\n"
385 "\t[-E(xisting)] (use existing pool instead of creating new one)\n"
386 "\t[-T time] total run time (default: %llu sec)\n"
387 "\t[-P passtime] time per pass (default: %llu sec)\n"
388 "\t[-h] (print help)\n"
389 "",
390 cmdname,
391 (u_longlong_t)zopt_vdevs, /* -v */
392 nice_vdev_size, /* -s */
393 zopt_ashift, /* -a */
394 zopt_mirrors, /* -m */
395 zopt_raidz, /* -r */
396 zopt_raidz_parity, /* -R */
397 zopt_datasets, /* -d */
398 zopt_threads, /* -t */
399 nice_gang_bang, /* -g */
400 zopt_init, /* -i */
401 (u_longlong_t)zopt_killrate, /* -k */
402 zopt_pool, /* -p */
403 zopt_dir, /* -f */
404 (u_longlong_t)zopt_time, /* -T */
405 (u_longlong_t)zopt_passtime); /* -P */
406 exit(requested ? 0 : 1);
407 }
408
409 static uint64_t
410 ztest_random(uint64_t range)
411 {
412 uint64_t r;
413
414 if (range == 0)
415 return (0);
416
417 if (read(ztest_random_fd, &r, sizeof (r)) != sizeof (r))
418 fatal(1, "short read from /dev/urandom");
419
420 return (r % range);
421 }
422
423 /* ARGSUSED */
424 static void
425 ztest_record_enospc(char *s)
426 {
427 ztest_shared->zs_enospc_count++;
428 }
429
430 static void
431 process_options(int argc, char **argv)
432 {
433 int opt;
434 uint64_t value;
435
436 /* By default, test gang blocks for blocks 32K and greater */
437 metaslab_gang_bang = 32 << 10;
438
439 while ((opt = getopt(argc, argv,
440 "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:h")) != EOF) {
441 value = 0;
442 switch (opt) {
443 case 'v':
444 case 's':
445 case 'a':
446 case 'm':
447 case 'r':
448 case 'R':
449 case 'd':
450 case 't':
451 case 'g':
452 case 'i':
453 case 'k':
454 case 'T':
455 case 'P':
456 value = nicenumtoull(optarg);
457 }
458 switch (opt) {
459 case 'v':
460 zopt_vdevs = value;
461 break;
462 case 's':
463 zopt_vdev_size = MAX(SPA_MINDEVSIZE, value);
464 break;
465 case 'a':
466 zopt_ashift = value;
467 break;
468 case 'm':
469 zopt_mirrors = value;
470 break;
471 case 'r':
472 zopt_raidz = MAX(1, value);
473 break;
474 case 'R':
475 zopt_raidz_parity = MIN(MAX(value, 1), 2);
476 break;
477 case 'd':
478 zopt_datasets = MAX(1, value);
479 break;
480 case 't':
481 zopt_threads = MAX(1, value);
482 break;
483 case 'g':
484 metaslab_gang_bang = MAX(SPA_MINBLOCKSIZE << 1, value);
485 break;
486 case 'i':
487 zopt_init = value;
488 break;
489 case 'k':
490 zopt_killrate = value;
491 break;
492 case 'p':
493 zopt_pool = strdup(optarg);
494 break;
495 case 'f':
496 zopt_dir = strdup(optarg);
497 break;
498 case 'V':
499 zopt_verbose++;
500 break;
501 case 'E':
502 zopt_init = 0;
503 break;
504 case 'T':
505 zopt_time = value;
506 break;
507 case 'P':
508 zopt_passtime = MAX(1, value);
509 break;
510 case 'h':
511 usage(B_TRUE);
512 break;
513 case '?':
514 default:
515 usage(B_FALSE);
516 break;
517 }
518 }
519
520 zopt_raidz_parity = MIN(zopt_raidz_parity, zopt_raidz - 1);
521
522 zopt_vdevtime = (zopt_vdevs > 0 ? zopt_time / zopt_vdevs : UINT64_MAX);
523 zopt_maxfaults = MAX(zopt_mirrors, 1) * (zopt_raidz_parity + 1) - 1;
524 }
525
526 static uint64_t
527 ztest_get_ashift(void)
528 {
529 if (zopt_ashift == 0)
530 return (SPA_MINBLOCKSHIFT + ztest_random(3));
531 return (zopt_ashift);
532 }
533
534 static nvlist_t *
535 make_vdev_file(char *path, char *aux, size_t size, uint64_t ashift)
536 {
537 char pathbuf[MAXPATHLEN];
538 uint64_t vdev;
539 nvlist_t *file;
540
541 if (ashift == 0)
542 ashift = ztest_get_ashift();
543
544 if (path == NULL) {
545 path = pathbuf;
546
547 if (aux != NULL) {
548 vdev = ztest_shared->zs_vdev_aux;
549 (void) sprintf(path, ztest_aux_template,
550 zopt_dir, zopt_pool, aux, vdev);
551 } else {
552 vdev = ztest_shared->zs_vdev_primaries++;
553 (void) sprintf(path, ztest_dev_template,
554 zopt_dir, zopt_pool, vdev);
555 }
556 }
557
558 if (size != 0) {
559 int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
560 if (fd == -1)
561 fatal(1, "can't open %s", path);
562 if (ftruncate(fd, size) != 0)
563 fatal(1, "can't ftruncate %s", path);
564 (void) close(fd);
565 }
566
567 VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
568 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
569 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
570 VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
571
572 return (file);
573 }
574
575 static nvlist_t *
576 make_vdev_raidz(char *path, char *aux, size_t size, uint64_t ashift, int r)
577 {
578 nvlist_t *raidz, **child;
579 int c;
580
581 if (r < 2)
582 return (make_vdev_file(path, aux, size, ashift));
583 child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
584
585 for (c = 0; c < r; c++)
586 child[c] = make_vdev_file(path, aux, size, ashift);
587
588 VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
589 VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
590 VDEV_TYPE_RAIDZ) == 0);
591 VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
592 zopt_raidz_parity) == 0);
593 VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
594 child, r) == 0);
595
596 for (c = 0; c < r; c++)
597 nvlist_free(child[c]);
598
599 umem_free(child, r * sizeof (nvlist_t *));
600
601 return (raidz);
602 }
603
604 static nvlist_t *
605 make_vdev_mirror(char *path, char *aux, size_t size, uint64_t ashift,
606 int r, int m)
607 {
608 nvlist_t *mirror, **child;
609 int c;
610
611 if (m < 1)
612 return (make_vdev_raidz(path, aux, size, ashift, r));
613
614 child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
615
616 for (c = 0; c < m; c++)
617 child[c] = make_vdev_raidz(path, aux, size, ashift, r);
618
619 VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
620 VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
621 VDEV_TYPE_MIRROR) == 0);
622 VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
623 child, m) == 0);
624
625 for (c = 0; c < m; c++)
626 nvlist_free(child[c]);
627
628 umem_free(child, m * sizeof (nvlist_t *));
629
630 return (mirror);
631 }
632
633 static nvlist_t *
634 make_vdev_root(char *path, char *aux, size_t size, uint64_t ashift,
635 int log, int r, int m, int t)
636 {
637 nvlist_t *root, **child;
638 int c;
639
640 ASSERT(t > 0);
641
642 child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
643
644 for (c = 0; c < t; c++) {
645 child[c] = make_vdev_mirror(path, aux, size, ashift, r, m);
646 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
647 log) == 0);
648 }
649
650 VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
651 VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
652 VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
653 child, t) == 0);
654
655 for (c = 0; c < t; c++)
656 nvlist_free(child[c]);
657
658 umem_free(child, t * sizeof (nvlist_t *));
659
660 return (root);
661 }
662
663 static void
664 ztest_set_random_blocksize(objset_t *os, uint64_t object, dmu_tx_t *tx)
665 {
666 int bs = SPA_MINBLOCKSHIFT +
667 ztest_random(SPA_MAXBLOCKSHIFT - SPA_MINBLOCKSHIFT + 1);
668 int ibs = DN_MIN_INDBLKSHIFT +
669 ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1);
670 int error;
671
672 error = dmu_object_set_blocksize(os, object, 1ULL << bs, ibs, tx);
673 if (error) {
674 char osname[300];
675 dmu_objset_name(os, osname);
676 fatal(0, "dmu_object_set_blocksize('%s', %llu, %d, %d) = %d",
677 osname, object, 1 << bs, ibs, error);
678 }
679 }
680
681 static uint8_t
682 ztest_random_checksum(void)
683 {
684 uint8_t checksum;
685
686 do {
687 checksum = ztest_random(ZIO_CHECKSUM_FUNCTIONS);
688 } while (zio_checksum_table[checksum].ci_zbt);
689
690 if (checksum == ZIO_CHECKSUM_OFF)
691 checksum = ZIO_CHECKSUM_ON;
692
693 return (checksum);
694 }
695
696 static uint8_t
697 ztest_random_compress(void)
698 {
699 return ((uint8_t)ztest_random(ZIO_COMPRESS_FUNCTIONS));
700 }
701
702 static uint8_t
703 ztest_random_crypt(void)
704 {
705 return ((uint8_t)ztest_random(ZIO_CRYPT_FUNCTIONS));
706 }
707
708 static int
709 ztest_replay_create(objset_t *os, lr_create_t *lr, boolean_t byteswap)
710 {
711 dmu_tx_t *tx;
712 int error;
713
714 if (byteswap)
715 byteswap_uint64_array(lr, sizeof (*lr));
716
717 tx = dmu_tx_create(os);
718 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
719 error = dmu_tx_assign(tx, TXG_WAIT);
720 if (error) {
721 dmu_tx_abort(tx);
722 return (error);
723 }
724
725 error = dmu_object_claim(os, lr->lr_doid, lr->lr_mode, 0,
726 DMU_OT_NONE, 0, tx);
727 ASSERT3U(error, ==, 0);
728 dmu_tx_commit(tx);
729
730 if (zopt_verbose >= 5) {
731 char osname[MAXNAMELEN];
732 dmu_objset_name(os, osname);
733 (void) printf("replay create of %s object %llu"
734 " in txg %llu = %d\n",
735 osname, (u_longlong_t)lr->lr_doid,
736 (u_longlong_t)dmu_tx_get_txg(tx), error);
737 }
738
739 return (error);
740 }
741
742 static int
743 ztest_replay_remove(objset_t *os, lr_remove_t *lr, boolean_t byteswap)
744 {
745 dmu_tx_t *tx;
746 int error;
747
748 if (byteswap)
749 byteswap_uint64_array(lr, sizeof (*lr));
750
751 tx = dmu_tx_create(os);
752 dmu_tx_hold_free(tx, lr->lr_doid, 0, DMU_OBJECT_END);
753 error = dmu_tx_assign(tx, TXG_WAIT);
754 if (error) {
755 dmu_tx_abort(tx);
756 return (error);
757 }
758
759 error = dmu_object_free(os, lr->lr_doid, tx);
760 dmu_tx_commit(tx);
761
762 return (error);
763 }
764
765 zil_replay_func_t *ztest_replay_vector[TX_MAX_TYPE] = {
766 NULL, /* 0 no such transaction type */
767 ztest_replay_create, /* TX_CREATE */
768 NULL, /* TX_MKDIR */
769 NULL, /* TX_MKXATTR */
770 NULL, /* TX_SYMLINK */
771 ztest_replay_remove, /* TX_REMOVE */
772 NULL, /* TX_RMDIR */
773 NULL, /* TX_LINK */
774 NULL, /* TX_RENAME */
775 NULL, /* TX_WRITE */
776 NULL, /* TX_TRUNCATE */
777 NULL, /* TX_SETATTR */
778 NULL, /* TX_ACL */
779 };
780
781 /*
782 * Verify that we can't destroy an active pool, create an existing pool,
783 * or create a pool with a bad vdev spec.
784 */
785 void
786 ztest_spa_create_destroy(ztest_args_t *za)
787 {
788 int error;
789 spa_t *spa;
790 nvlist_t *nvroot;
791
792 /*
793 * Attempt to create using a bad file.
794 */
795 nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
796 error = spa_create("ztest_bad_file", nvroot, NULL, NULL, NULL);
797 nvlist_free(nvroot);
798 if (error != ENOENT)
799 fatal(0, "spa_create(bad_file) = %d", error);
800
801 /*
802 * Attempt to create using a bad mirror.
803 */
804 nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 2, 1);
805 error = spa_create("ztest_bad_mirror", nvroot, NULL, NULL, NULL);
806 nvlist_free(nvroot);
807 if (error != ENOENT)
808 fatal(0, "spa_create(bad_mirror) = %d", error);
809
810 /*
811 * Attempt to create an existing pool. It shouldn't matter
812 * what's in the nvroot; we should fail with EEXIST.
813 */
814 (void) rw_rdlock(&ztest_shared->zs_name_lock);
815 nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
816 error = spa_create(za->za_pool, nvroot, NULL, NULL, NULL);
817 nvlist_free(nvroot);
818 if (error != EEXIST)
819 fatal(0, "spa_create(whatever) = %d", error);
820
821 error = spa_open(za->za_pool, &spa, FTAG);
822 if (error)
823 fatal(0, "spa_open() = %d", error);
824
825 error = spa_destroy(za->za_pool);
826 if (error != EBUSY)
827 fatal(0, "spa_destroy() = %d", error);
828
829 spa_close(spa, FTAG);
830 (void) rw_unlock(&ztest_shared->zs_name_lock);
831 }
832
833 static vdev_t *
834 vdev_lookup_by_path(vdev_t *vd, const char *path)
835 {
836 vdev_t *mvd;
837
838 if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
839 return (vd);
840
841 for (int c = 0; c < vd->vdev_children; c++)
842 if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
843 NULL)
844 return (mvd);
845
846 return (NULL);
847 }
848
849 /*
850 * Verify that vdev_add() works as expected.
851 */
852 void
853 ztest_vdev_add_remove(ztest_args_t *za)
854 {
855 spa_t *spa = za->za_spa;
856 uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz;
857 nvlist_t *nvroot;
858 int error;
859
860 (void) mutex_lock(&ztest_shared->zs_vdev_lock);
861
862 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
863
864 ztest_shared->zs_vdev_primaries =
865 spa->spa_root_vdev->vdev_children * leaves;
866
867 spa_config_exit(spa, SCL_VDEV, FTAG);
868
869 /*
870 * Make 1/4 of the devices be log devices.
871 */
872 nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0,
873 ztest_random(4) == 0, zopt_raidz, zopt_mirrors, 1);
874
875 error = spa_vdev_add(spa, nvroot);
876 nvlist_free(nvroot);
877
878 (void) mutex_unlock(&ztest_shared->zs_vdev_lock);
879
880 if (error == ENOSPC)
881 ztest_record_enospc("spa_vdev_add");
882 else if (error != 0)
883 fatal(0, "spa_vdev_add() = %d", error);
884 }
885
886 /*
887 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
888 */
889 void
890 ztest_vdev_aux_add_remove(ztest_args_t *za)
891 {
892 spa_t *spa = za->za_spa;
893 vdev_t *rvd = spa->spa_root_vdev;
894 spa_aux_vdev_t *sav;
895 char *aux;
896 uint64_t guid = 0;
897 int error;
898
899 if (ztest_random(2) == 0) {
900 sav = &spa->spa_spares;
901 aux = ZPOOL_CONFIG_SPARES;
902 } else {
903 sav = &spa->spa_l2cache;
904 aux = ZPOOL_CONFIG_L2CACHE;
905 }
906
907 (void) mutex_lock(&ztest_shared->zs_vdev_lock);
908
909 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
910
911 if (sav->sav_count != 0 && ztest_random(4) == 0) {
912 /*
913 * Pick a random device to remove.
914 */
915 guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
916 } else {
917 /*
918 * Find an unused device we can add.
919 */
920 ztest_shared->zs_vdev_aux = 0;
921 for (;;) {
922 char path[MAXPATHLEN];
923 int c;
924 (void) sprintf(path, ztest_aux_template, zopt_dir,
925 zopt_pool, aux, ztest_shared->zs_vdev_aux);
926 for (c = 0; c < sav->sav_count; c++)
927 if (strcmp(sav->sav_vdevs[c]->vdev_path,
928 path) == 0)
929 break;
930 if (c == sav->sav_count &&
931 vdev_lookup_by_path(rvd, path) == NULL)
932 break;
933 ztest_shared->zs_vdev_aux++;
934 }
935 }
936
937 spa_config_exit(spa, SCL_VDEV, FTAG);
938
939 if (guid == 0) {
940 /*
941 * Add a new device.
942 */
943 nvlist_t *nvroot = make_vdev_root(NULL, aux,
944 (zopt_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
945 error = spa_vdev_add(spa, nvroot);
946 if (error != 0)
947 fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
948 nvlist_free(nvroot);
949 } else {
950 /*
951 * Remove an existing device. Sometimes, dirty its
952 * vdev state first to make sure we handle removal
953 * of devices that have pending state changes.
954 */
955 if (ztest_random(2) == 0)
956 (void) vdev_online(spa, guid, B_FALSE, NULL);
957
958 error = spa_vdev_remove(spa, guid, B_FALSE);
959 if (error != 0 && error != EBUSY)
960 fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
961 }
962
963 (void) mutex_unlock(&ztest_shared->zs_vdev_lock);
964 }
965
966 /*
967 * Verify that we can attach and detach devices.
968 */
969 void
970 ztest_vdev_attach_detach(ztest_args_t *za)
971 {
972 spa_t *spa = za->za_spa;
973 spa_aux_vdev_t *sav = &spa->spa_spares;
974 vdev_t *rvd = spa->spa_root_vdev;
975 vdev_t *oldvd, *newvd, *pvd;
976 nvlist_t *root;
977 uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz;
978 uint64_t leaf, top;
979 uint64_t ashift = ztest_get_ashift();
980 uint64_t oldguid, pguid;
981 size_t oldsize, newsize;
982 char oldpath[MAXPATHLEN], newpath[MAXPATHLEN];
983 int replacing;
984 int oldvd_has_siblings = B_FALSE;
985 int newvd_is_spare = B_FALSE;
986 int oldvd_is_log;
987 int error, expected_error;
988
989 (void) mutex_lock(&ztest_shared->zs_vdev_lock);
990
991 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
992
993 /*
994 * Decide whether to do an attach or a replace.
995 */
996 replacing = ztest_random(2);
997
998 /*
999 * Pick a random top-level vdev.
1000 */
1001 top = ztest_random(rvd->vdev_children);
1002
1003 /*
1004 * Pick a random leaf within it.
1005 */
1006 leaf = ztest_random(leaves);
1007
1008 /*
1009 * Locate this vdev.
1010 */
1011 oldvd = rvd->vdev_child[top];
1012 if (zopt_mirrors >= 1) {
1013 ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
1014 ASSERT(oldvd->vdev_children >= zopt_mirrors);
1015 oldvd = oldvd->vdev_child[leaf / zopt_raidz];
1016 }
1017 if (zopt_raidz > 1) {
1018 ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
1019 ASSERT(oldvd->vdev_children == zopt_raidz);
1020 oldvd = oldvd->vdev_child[leaf % zopt_raidz];
1021 }
1022
1023 /*
1024 * If we're already doing an attach or replace, oldvd may be a
1025 * mirror vdev -- in which case, pick a random child.
1026 */
1027 while (oldvd->vdev_children != 0) {
1028 oldvd_has_siblings = B_TRUE;
1029 ASSERT(oldvd->vdev_children >= 2);
1030 oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
1031 }
1032
1033 oldguid = oldvd->vdev_guid;
1034 oldsize = vdev_get_rsize(oldvd);
1035 oldvd_is_log = oldvd->vdev_top->vdev_islog;
1036 (void) strcpy(oldpath, oldvd->vdev_path);
1037 pvd = oldvd->vdev_parent;
1038 pguid = pvd->vdev_guid;
1039
1040 /*
1041 * If oldvd has siblings, then half of the time, detach it.
1042 */
1043 if (oldvd_has_siblings && ztest_random(2) == 0) {
1044 spa_config_exit(spa, SCL_VDEV, FTAG);
1045 error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
1046 if (error != 0 && error != ENODEV && error != EBUSY &&
1047 error != ENOTSUP)
1048 fatal(0, "detach (%s) returned %d", oldpath, error);
1049 (void) mutex_unlock(&ztest_shared->zs_vdev_lock);
1050 return;
1051 }
1052
1053 /*
1054 * For the new vdev, choose with equal probability between the two
1055 * standard paths (ending in either 'a' or 'b') or a random hot spare.
1056 */
1057 if (sav->sav_count != 0 && ztest_random(3) == 0) {
1058 newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
1059 newvd_is_spare = B_TRUE;
1060 (void) strcpy(newpath, newvd->vdev_path);
1061 } else {
1062 (void) snprintf(newpath, sizeof (newpath), ztest_dev_template,
1063 zopt_dir, zopt_pool, top * leaves + leaf);
1064 if (ztest_random(2) == 0)
1065 newpath[strlen(newpath) - 1] = 'b';
1066 newvd = vdev_lookup_by_path(rvd, newpath);
1067 }
1068
1069 if (newvd) {
1070 newsize = vdev_get_rsize(newvd);
1071 } else {
1072 /*
1073 * Make newsize a little bigger or smaller than oldsize.
1074 * If it's smaller, the attach should fail.
1075 * If it's larger, and we're doing a replace,
1076 * we should get dynamic LUN growth when we're done.
1077 */
1078 newsize = 10 * oldsize / (9 + ztest_random(3));
1079 }
1080
1081 /*
1082 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
1083 * unless it's a replace; in that case any non-replacing parent is OK.
1084 *
1085 * If newvd is already part of the pool, it should fail with EBUSY.
1086 *
1087 * If newvd is too small, it should fail with EOVERFLOW.
1088 */
1089 if (pvd->vdev_ops != &vdev_mirror_ops &&
1090 pvd->vdev_ops != &vdev_root_ops && (!replacing ||
1091 pvd->vdev_ops == &vdev_replacing_ops ||
1092 pvd->vdev_ops == &vdev_spare_ops))
1093 expected_error = ENOTSUP;
1094 else if (newvd_is_spare && (!replacing || oldvd_is_log))
1095 expected_error = ENOTSUP;
1096 else if (newvd == oldvd)
1097 expected_error = replacing ? 0 : EBUSY;
1098 else if (vdev_lookup_by_path(rvd, newpath) != NULL)
1099 expected_error = EBUSY;
1100 else if (newsize < oldsize)
1101 expected_error = EOVERFLOW;
1102 else if (ashift > oldvd->vdev_top->vdev_ashift)
1103 expected_error = EDOM;
1104 else
1105 expected_error = 0;
1106
1107 spa_config_exit(spa, SCL_VDEV, FTAG);
1108
1109 /*
1110 * Build the nvlist describing newpath.
1111 */
1112 root = make_vdev_root(newpath, NULL, newvd == NULL ? newsize : 0,
1113 ashift, 0, 0, 0, 1);
1114
1115 error = spa_vdev_attach(spa, oldguid, root, replacing);
1116
1117 nvlist_free(root);
1118
1119 /*
1120 * If our parent was the replacing vdev, but the replace completed,
1121 * then instead of failing with ENOTSUP we may either succeed,
1122 * fail with ENODEV, or fail with EOVERFLOW.
1123 */
1124 if (expected_error == ENOTSUP &&
1125 (error == 0 || error == ENODEV || error == EOVERFLOW))
1126 expected_error = error;
1127
1128 /*
1129 * If someone grew the LUN, the replacement may be too small.
1130 */
1131 if (error == EOVERFLOW || error == EBUSY)
1132 expected_error = error;
1133
1134 /* XXX workaround 6690467 */
1135 if (error != expected_error && expected_error != EBUSY) {
1136 fatal(0, "attach (%s %llu, %s %llu, %d) "
1137 "returned %d, expected %d",
1138 oldpath, (longlong_t)oldsize, newpath,
1139 (longlong_t)newsize, replacing, error, expected_error);
1140 }
1141
1142 (void) mutex_unlock(&ztest_shared->zs_vdev_lock);
1143 }
1144
1145 /*
1146 * Verify that dynamic LUN growth works as expected.
1147 */
1148 void
1149 ztest_vdev_LUN_growth(ztest_args_t *za)
1150 {
1151 spa_t *spa = za->za_spa;
1152 char dev_name[MAXPATHLEN];
1153 uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz;
1154 uint64_t vdev;
1155 size_t fsize;
1156 int fd;
1157
1158 (void) mutex_lock(&ztest_shared->zs_vdev_lock);
1159
1160 /*
1161 * Pick a random leaf vdev.
1162 */
1163 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
1164 vdev = ztest_random(spa->spa_root_vdev->vdev_children * leaves);
1165 spa_config_exit(spa, SCL_VDEV, FTAG);
1166
1167 (void) sprintf(dev_name, ztest_dev_template, zopt_dir, zopt_pool, vdev);
1168
1169 if ((fd = open(dev_name, O_RDWR)) != -1) {
1170 /*
1171 * Determine the size.
1172 */
1173 fsize = lseek(fd, 0, SEEK_END);
1174
1175 /*
1176 * If it's less than 2x the original size, grow by around 3%.
1177 */
1178 if (fsize < 2 * zopt_vdev_size) {
1179 size_t newsize = fsize + ztest_random(fsize / 32);
1180 (void) ftruncate(fd, newsize);
1181 if (zopt_verbose >= 6) {
1182 (void) printf("%s grew from %lu to %lu bytes\n",
1183 dev_name, (ulong_t)fsize, (ulong_t)newsize);
1184 }
1185 }
1186 (void) close(fd);
1187 }
1188
1189 (void) mutex_unlock(&ztest_shared->zs_vdev_lock);
1190 }
1191
1192 /* ARGSUSED */
1193 static void
1194 ztest_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
1195 {
1196 /*
1197 * Create the directory object.
1198 */
1199 VERIFY(dmu_object_claim(os, ZTEST_DIROBJ,
1200 DMU_OT_UINT64_OTHER, ZTEST_DIROBJ_BLOCKSIZE,
1201 DMU_OT_UINT64_OTHER, 5 * sizeof (ztest_block_tag_t), tx) == 0);
1202
1203 VERIFY(zap_create_claim(os, ZTEST_MICROZAP_OBJ,
1204 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
1205
1206 VERIFY(zap_create_claim(os, ZTEST_FATZAP_OBJ,
1207 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
1208 }
1209
1210 static int
1211 ztest_destroy_cb(char *name, void *arg)
1212 {
1213 ztest_args_t *za = arg;
1214 objset_t *os;
1215 dmu_object_info_t *doi = &za->za_doi;
1216 int error;
1217
1218 /*
1219 * Verify that the dataset contains a directory object.
1220 */
1221 error = dmu_objset_open(name, DMU_OST_OTHER,
1222 DS_MODE_USER | DS_MODE_READONLY, &os);
1223 ASSERT3U(error, ==, 0);
1224 error = dmu_object_info(os, ZTEST_DIROBJ, doi);
1225 if (error != ENOENT) {
1226 /* We could have crashed in the middle of destroying it */
1227 ASSERT3U(error, ==, 0);
1228 ASSERT3U(doi->doi_type, ==, DMU_OT_UINT64_OTHER);
1229 ASSERT3S(doi->doi_physical_blks, >=, 0);
1230 }
1231 dmu_objset_close(os);
1232
1233 /*
1234 * Destroy the dataset.
1235 */
1236 error = dmu_objset_destroy(name);
1237 if (error) {
1238 (void) dmu_objset_open(name, DMU_OST_OTHER,
1239 DS_MODE_USER | DS_MODE_READONLY, &os);
1240 fatal(0, "dmu_objset_destroy(os=%p) = %d\n", &os, error);
1241 }
1242 return (0);
1243 }
1244
1245 /*
1246 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
1247 */
1248 static uint64_t
1249 ztest_log_create(zilog_t *zilog, dmu_tx_t *tx, uint64_t object, int mode)
1250 {
1251 itx_t *itx;
1252 lr_create_t *lr;
1253 size_t namesize;
1254 char name[24];
1255
1256 (void) sprintf(name, "ZOBJ_%llu", (u_longlong_t)object);
1257 namesize = strlen(name) + 1;
1258
1259 itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize +
1260 ztest_random(ZIL_MAX_BLKSZ));
1261 lr = (lr_create_t *)&itx->itx_lr;
1262 bzero(lr + 1, lr->lr_common.lrc_reclen - sizeof (*lr));
1263 lr->lr_doid = object;
1264 lr->lr_foid = 0;
1265 lr->lr_mode = mode;
1266 lr->lr_uid = 0;
1267 lr->lr_gid = 0;
1268 lr->lr_gen = dmu_tx_get_txg(tx);
1269 lr->lr_crtime[0] = time(NULL);
1270 lr->lr_crtime[1] = 0;
1271 lr->lr_rdev = 0;
1272 bcopy(name, (char *)(lr + 1), namesize);
1273
1274 return (zil_itx_assign(zilog, itx, tx));
1275 }
1276
1277 void
1278 ztest_dmu_objset_create_destroy(ztest_args_t *za)
1279 {
1280 int error;
1281 objset_t *os, *os2;
1282 char name[100];
1283 int basemode, expected_error;
1284 zilog_t *zilog;
1285 uint64_t seq;
1286 uint64_t objects;
1287
1288 (void) rw_rdlock(&ztest_shared->zs_name_lock);
1289 (void) snprintf(name, 100, "%s/%s_temp_%llu", za->za_pool, za->za_pool,
1290 (u_longlong_t)za->za_instance);
1291
1292 basemode = DS_MODE_TYPE(za->za_instance);
1293 if (basemode != DS_MODE_USER && basemode != DS_MODE_OWNER)
1294 basemode = DS_MODE_USER;
1295
1296 /*
1297 * If this dataset exists from a previous run, process its replay log
1298 * half of the time. If we don't replay it, then dmu_objset_destroy()
1299 * (invoked from ztest_destroy_cb() below) should just throw it away.
1300 */
1301 if (ztest_random(2) == 0 &&
1302 dmu_objset_open(name, DMU_OST_OTHER, DS_MODE_OWNER, &os) == 0) {
1303 zil_replay(os, os, ztest_replay_vector);
1304 dmu_objset_close(os);
1305 }
1306
1307 /*
1308 * There may be an old instance of the dataset we're about to
1309 * create lying around from a previous run. If so, destroy it
1310 * and all of its snapshots.
1311 */
1312 (void) dmu_objset_find(name, ztest_destroy_cb, za,
1313 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
1314
1315 /*
1316 * Verify that the destroyed dataset is no longer in the namespace.
1317 */
1318 error = dmu_objset_open(name, DMU_OST_OTHER, basemode, &os);
1319 if (error != ENOENT)
1320 fatal(1, "dmu_objset_open(%s) found destroyed dataset %p",
1321 name, os);
1322
1323 /*
1324 * Verify that we can create a new dataset.
1325 */
1326 error = dmu_objset_create(name, DMU_OST_OTHER, NULL, 0,
1327 ztest_create_cb, NULL);
1328 if (error) {
1329 if (error == ENOSPC) {
1330 ztest_record_enospc("dmu_objset_create");
1331 (void) rw_unlock(&ztest_shared->zs_name_lock);
1332 return;
1333 }
1334 fatal(0, "dmu_objset_create(%s) = %d", name, error);
1335 }
1336
1337 error = dmu_objset_open(name, DMU_OST_OTHER, basemode, &os);
1338 if (error) {
1339 fatal(0, "dmu_objset_open(%s) = %d", name, error);
1340 }
1341
1342 /*
1343 * Open the intent log for it.
1344 */
1345 zilog = zil_open(os, NULL);
1346
1347 /*
1348 * Put a random number of objects in there.
1349 */
1350 objects = ztest_random(20);
1351 seq = 0;
1352 while (objects-- != 0) {
1353 uint64_t object;
1354 dmu_tx_t *tx = dmu_tx_create(os);
1355 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, sizeof (name));
1356 error = dmu_tx_assign(tx, TXG_WAIT);
1357 if (error) {
1358 dmu_tx_abort(tx);
1359 } else {
1360 object = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
1361 DMU_OT_NONE, 0, tx);
1362 ztest_set_random_blocksize(os, object, tx);
1363 seq = ztest_log_create(zilog, tx, object,
1364 DMU_OT_UINT64_OTHER);
1365 dmu_write(os, object, 0, sizeof (name), name, tx);
1366 dmu_tx_commit(tx);
1367 }
1368 if (ztest_random(5) == 0) {
1369 zil_commit(zilog, seq, object);
1370 }
1371 if (ztest_random(100) == 0) {
1372 error = zil_suspend(zilog);
1373 if (error == 0) {
1374 zil_resume(zilog);
1375 }
1376 }
1377 }
1378
1379 /*
1380 * Verify that we cannot create an existing dataset.
1381 */
1382 error = dmu_objset_create(name, DMU_OST_OTHER, NULL, 0, NULL, NULL);
1383 if (error != EEXIST)
1384 fatal(0, "created existing dataset, error = %d", error);
1385
1386 /*
1387 * Verify that multiple dataset holds are allowed, but only when
1388 * the new access mode is compatible with the base mode.
1389 */
1390 if (basemode == DS_MODE_OWNER) {
1391 error = dmu_objset_open(name, DMU_OST_OTHER, DS_MODE_USER,
1392 &os2);
1393 if (error)
1394 fatal(0, "dmu_objset_open('%s') = %d", name, error);
1395 else
1396 dmu_objset_close(os2);
1397 }
1398 error = dmu_objset_open(name, DMU_OST_OTHER, DS_MODE_OWNER, &os2);
1399 expected_error = (basemode == DS_MODE_OWNER) ? EBUSY : 0;
1400 if (error != expected_error)
1401 fatal(0, "dmu_objset_open('%s') = %d, expected %d",
1402 name, error, expected_error);
1403 if (error == 0)
1404 dmu_objset_close(os2);
1405
1406 zil_close(zilog);
1407 dmu_objset_close(os);
1408
1409 error = dmu_objset_destroy(name);
1410 if (error)
1411 fatal(0, "dmu_objset_destroy(%s) = %d", name, error);
1412
1413 (void) rw_unlock(&ztest_shared->zs_name_lock);
1414 }
1415
1416 /*
1417 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
1418 */
1419 void
1420 ztest_dmu_snapshot_create_destroy(ztest_args_t *za)
1421 {
1422 int error;
1423 objset_t *os = za->za_os;
1424 char snapname[100];
1425 char osname[MAXNAMELEN];
1426
1427 (void) rw_rdlock(&ztest_shared->zs_name_lock);
1428 dmu_objset_name(os, osname);
1429 (void) snprintf(snapname, 100, "%s@%llu", osname,
1430 (u_longlong_t)za->za_instance);
1431
1432 error = dmu_objset_destroy(snapname);
1433 if (error != 0 && error != ENOENT)
1434 fatal(0, "dmu_objset_destroy() = %d", error);
1435 error = dmu_objset_snapshot(osname, strchr(snapname, '@')+1, FALSE);
1436 if (error == ENOSPC)
1437 ztest_record_enospc("dmu_take_snapshot");
1438 else if (error != 0 && error != EEXIST)
1439 fatal(0, "dmu_take_snapshot() = %d", error);
1440 (void) rw_unlock(&ztest_shared->zs_name_lock);
1441 }
1442
1443 /*
1444 * Verify that dmu_object_{alloc,free} work as expected.
1445 */
1446 void
1447 ztest_dmu_object_alloc_free(ztest_args_t *za)
1448 {
1449 objset_t *os = za->za_os;
1450 dmu_buf_t *db;
1451 dmu_tx_t *tx;
1452 uint64_t batchobj, object, batchsize, endoff, temp;
1453 int b, c, error, bonuslen;
1454 dmu_object_info_t *doi = &za->za_doi;
1455 char osname[MAXNAMELEN];
1456
1457 dmu_objset_name(os, osname);
1458
1459 endoff = -8ULL;
1460 batchsize = 2;
1461
1462 /*
1463 * Create a batch object if necessary, and record it in the directory.
1464 */
1465 VERIFY3U(0, ==, dmu_read(os, ZTEST_DIROBJ, za->za_diroff,
1466 sizeof (uint64_t), &batchobj));
1467 if (batchobj == 0) {
1468 tx = dmu_tx_create(os);
1469 dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff,
1470 sizeof (uint64_t));
1471 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1472 error = dmu_tx_assign(tx, TXG_WAIT);
1473 if (error) {
1474 ztest_record_enospc("create a batch object");
1475 dmu_tx_abort(tx);
1476 return;
1477 }
1478 batchobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
1479 DMU_OT_NONE, 0, tx);
1480 ztest_set_random_blocksize(os, batchobj, tx);
1481 dmu_write(os, ZTEST_DIROBJ, za->za_diroff,
1482 sizeof (uint64_t), &batchobj, tx);
1483 dmu_tx_commit(tx);
1484 }
1485
1486 /*
1487 * Destroy the previous batch of objects.
1488 */
1489 for (b = 0; b < batchsize; b++) {
1490 VERIFY3U(0, ==, dmu_read(os, batchobj, b * sizeof (uint64_t),
1491 sizeof (uint64_t), &object));
1492 if (object == 0)
1493 continue;
1494 /*
1495 * Read and validate contents.
1496 * We expect the nth byte of the bonus buffer to be n.
1497 */
1498 VERIFY(0 == dmu_bonus_hold(os, object, FTAG, &db));
1499 za->za_dbuf = db;
1500
1501 dmu_object_info_from_db(db, doi);
1502 ASSERT(doi->doi_type == DMU_OT_UINT64_OTHER);
1503 ASSERT(doi->doi_bonus_type == DMU_OT_PLAIN_OTHER);
1504 ASSERT3S(doi->doi_physical_blks, >=, 0);
1505
1506 bonuslen = doi->doi_bonus_size;
1507
1508 for (c = 0; c < bonuslen; c++) {
1509 if (((uint8_t *)db->db_data)[c] !=
1510 (uint8_t)(c + bonuslen)) {
1511 fatal(0,
1512 "bad bonus: %s, obj %llu, off %d: %u != %u",
1513 osname, object, c,
1514 ((uint8_t *)db->db_data)[c],
1515 (uint8_t)(c + bonuslen));
1516 }
1517 }
1518
1519 dmu_buf_rele(db, FTAG);
1520 za->za_dbuf = NULL;
1521
1522 /*
1523 * We expect the word at endoff to be our object number.
1524 */
1525 VERIFY(0 == dmu_read(os, object, endoff,
1526 sizeof (uint64_t), &temp));
1527
1528 if (temp != object) {
1529 fatal(0, "bad data in %s, got %llu, expected %llu",
1530 osname, temp, object);
1531 }
1532
1533 /*
1534 * Destroy old object and clear batch entry.
1535 */
1536 tx = dmu_tx_create(os);
1537 dmu_tx_hold_write(tx, batchobj,
1538 b * sizeof (uint64_t), sizeof (uint64_t));
1539 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1540 error = dmu_tx_assign(tx, TXG_WAIT);
1541 if (error) {
1542 ztest_record_enospc("free object");
1543 dmu_tx_abort(tx);
1544 return;
1545 }
1546 error = dmu_object_free(os, object, tx);
1547 if (error) {
1548 fatal(0, "dmu_object_free('%s', %llu) = %d",
1549 osname, object, error);
1550 }
1551 object = 0;
1552
1553 dmu_object_set_checksum(os, batchobj,
1554 ztest_random_checksum(), tx);
1555 dmu_object_set_compress(os, batchobj,
1556 ztest_random_compress(), tx);
1557 dmu_object_set_crypt(os, batchobj,
1558 ztest_random_crypt(), tx);
1559
1560 dmu_write(os, batchobj, b * sizeof (uint64_t),
1561 sizeof (uint64_t), &object, tx);
1562
1563 dmu_tx_commit(tx);
1564 }
1565
1566 /*
1567 * Before creating the new batch of objects, generate a bunch of churn.
1568 */
1569 for (b = ztest_random(100); b > 0; b--) {
1570 tx = dmu_tx_create(os);
1571 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1572 error = dmu_tx_assign(tx, TXG_WAIT);
1573 if (error) {
1574 ztest_record_enospc("churn objects");
1575 dmu_tx_abort(tx);
1576 return;
1577 }
1578 object = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
1579 DMU_OT_NONE, 0, tx);
1580 ztest_set_random_blocksize(os, object, tx);
1581 error = dmu_object_free(os, object, tx);
1582 if (error) {
1583 fatal(0, "dmu_object_free('%s', %llu) = %d",
1584 osname, object, error);
1585 }
1586 dmu_tx_commit(tx);
1587 }
1588
1589 /*
1590 * Create a new batch of objects with randomly chosen
1591 * blocksizes and record them in the batch directory.
1592 */
1593 for (b = 0; b < batchsize; b++) {
1594 uint32_t va_blksize;
1595 u_longlong_t va_nblocks;
1596
1597 tx = dmu_tx_create(os);
1598 dmu_tx_hold_write(tx, batchobj, b * sizeof (uint64_t),
1599 sizeof (uint64_t));
1600 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1601 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, endoff,
1602 sizeof (uint64_t));
1603 error = dmu_tx_assign(tx, TXG_WAIT);
1604 if (error) {
1605 ztest_record_enospc("create batchobj");
1606 dmu_tx_abort(tx);
1607 return;
1608 }
1609 bonuslen = (int)ztest_random(dmu_bonus_max()) + 1;
1610
1611 object = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
1612 DMU_OT_PLAIN_OTHER, bonuslen, tx);
1613
1614 ztest_set_random_blocksize(os, object, tx);
1615
1616 dmu_object_set_checksum(os, object,
1617 ztest_random_checksum(), tx);
1618 dmu_object_set_compress(os, object,
1619 ztest_random_compress(), tx);
1620 dmu_object_set_crypt(os, object,
1621 ztest_random_crypt(), tx);
1622
1623 dmu_write(os, batchobj, b * sizeof (uint64_t),
1624 sizeof (uint64_t), &object, tx);
1625
1626 /*
1627 * Write to both the bonus buffer and the regular data.
1628 */
1629 VERIFY(dmu_bonus_hold(os, object, FTAG, &db) == 0);
1630 za->za_dbuf = db;
1631 ASSERT3U(bonuslen, <=, db->db_size);
1632
1633 dmu_object_size_from_db(db, &va_blksize, &va_nblocks);
1634 ASSERT3S(va_nblocks, >=, 0);
1635
1636 dmu_buf_will_dirty(db, tx);
1637
1638 /*
1639 * See comments above regarding the contents of
1640 * the bonus buffer and the word at endoff.
1641 */
1642 for (c = 0; c < bonuslen; c++)
1643 ((uint8_t *)db->db_data)[c] = (uint8_t)(c + bonuslen);
1644
1645 dmu_buf_rele(db, FTAG);
1646 za->za_dbuf = NULL;
1647
1648 /*
1649 * Write to a large offset to increase indirection.
1650 */
1651 dmu_write(os, object, endoff, sizeof (uint64_t), &object, tx);
1652
1653 dmu_tx_commit(tx);
1654 }
1655 }
1656
1657 /*
1658 * Verify that dmu_{read,write} work as expected.
1659 */
1660 typedef struct bufwad {
1661 uint64_t bw_index;
1662 uint64_t bw_txg;
1663 uint64_t bw_data;
1664 } bufwad_t;
1665
1666 typedef struct dmu_read_write_dir {
1667 uint64_t dd_packobj;
1668 uint64_t dd_bigobj;
1669 uint64_t dd_chunk;
1670 } dmu_read_write_dir_t;
1671
1672 void
1673 ztest_dmu_read_write(ztest_args_t *za)
1674 {
1675 objset_t *os = za->za_os;
1676 dmu_read_write_dir_t dd;
1677 dmu_tx_t *tx;
1678 int i, freeit, error;
1679 uint64_t n, s, txg;
1680 bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
1681 uint64_t packoff, packsize, bigoff, bigsize;
1682 uint64_t regions = 997;
1683 uint64_t stride = 123456789ULL;
1684 uint64_t width = 40;
1685 int free_percent = 5;
1686
1687 /*
1688 * This test uses two objects, packobj and bigobj, that are always
1689 * updated together (i.e. in the same tx) so that their contents are
1690 * in sync and can be compared. Their contents relate to each other
1691 * in a simple way: packobj is a dense array of 'bufwad' structures,
1692 * while bigobj is a sparse array of the same bufwads. Specifically,
1693 * for any index n, there are three bufwads that should be identical:
1694 *
1695 * packobj, at offset n * sizeof (bufwad_t)
1696 * bigobj, at the head of the nth chunk
1697 * bigobj, at the tail of the nth chunk
1698 *
1699 * The chunk size is arbitrary. It doesn't have to be a power of two,
1700 * and it doesn't have any relation to the object blocksize.
1701 * The only requirement is that it can hold at least two bufwads.
1702 *
1703 * Normally, we write the bufwad to each of these locations.
1704 * However, free_percent of the time we instead write zeroes to
1705 * packobj and perform a dmu_free_range() on bigobj. By comparing
1706 * bigobj to packobj, we can verify that the DMU is correctly
1707 * tracking which parts of an object are allocated and free,
1708 * and that the contents of the allocated blocks are correct.
1709 */
1710
1711 /*
1712 * Read the directory info. If it's the first time, set things up.
1713 */
1714 VERIFY(0 == dmu_read(os, ZTEST_DIROBJ, za->za_diroff,
1715 sizeof (dd), &dd));
1716 if (dd.dd_chunk == 0) {
1717 ASSERT(dd.dd_packobj == 0);
1718 ASSERT(dd.dd_bigobj == 0);
1719 tx = dmu_tx_create(os);
1720 dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, sizeof (dd));
1721 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1722 error = dmu_tx_assign(tx, TXG_WAIT);
1723 if (error) {
1724 ztest_record_enospc("create r/w directory");
1725 dmu_tx_abort(tx);
1726 return;
1727 }
1728
1729 dd.dd_packobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
1730 DMU_OT_NONE, 0, tx);
1731 dd.dd_bigobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
1732 DMU_OT_NONE, 0, tx);
1733 dd.dd_chunk = (1000 + ztest_random(1000)) * sizeof (uint64_t);
1734
1735 ztest_set_random_blocksize(os, dd.dd_packobj, tx);
1736 ztest_set_random_blocksize(os, dd.dd_bigobj, tx);
1737
1738 dmu_write(os, ZTEST_DIROBJ, za->za_diroff, sizeof (dd), &dd,
1739 tx);
1740 dmu_tx_commit(tx);
1741 }
1742
1743 /*
1744 * Prefetch a random chunk of the big object.
1745 * Our aim here is to get some async reads in flight
1746 * for blocks that we may free below; the DMU should
1747 * handle this race correctly.
1748 */
1749 n = ztest_random(regions) * stride + ztest_random(width);
1750 s = 1 + ztest_random(2 * width - 1);
1751 dmu_prefetch(os, dd.dd_bigobj, n * dd.dd_chunk, s * dd.dd_chunk);
1752
1753 /*
1754 * Pick a random index and compute the offsets into packobj and bigobj.
1755 */
1756 n = ztest_random(regions) * stride + ztest_random(width);
1757 s = 1 + ztest_random(width - 1);
1758
1759 packoff = n * sizeof (bufwad_t);
1760 packsize = s * sizeof (bufwad_t);
1761
1762 bigoff = n * dd.dd_chunk;
1763 bigsize = s * dd.dd_chunk;
1764
1765 packbuf = umem_alloc(packsize, UMEM_NOFAIL);
1766 bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
1767
1768 /*
1769 * free_percent of the time, free a range of bigobj rather than
1770 * overwriting it.
1771 */
1772 freeit = (ztest_random(100) < free_percent);
1773
1774 /*
1775 * Read the current contents of our objects.
1776 */
1777 error = dmu_read(os, dd.dd_packobj, packoff, packsize, packbuf);
1778 ASSERT3U(error, ==, 0);
1779 error = dmu_read(os, dd.dd_bigobj, bigoff, bigsize, bigbuf);
1780 ASSERT3U(error, ==, 0);
1781
1782 /*
1783 * Get a tx for the mods to both packobj and bigobj.
1784 */
1785 tx = dmu_tx_create(os);
1786
1787 dmu_tx_hold_write(tx, dd.dd_packobj, packoff, packsize);
1788
1789 if (freeit)
1790 dmu_tx_hold_free(tx, dd.dd_bigobj, bigoff, bigsize);
1791 else
1792 dmu_tx_hold_write(tx, dd.dd_bigobj, bigoff, bigsize);
1793
1794 error = dmu_tx_assign(tx, TXG_WAIT);
1795
1796 if (error) {
1797 ztest_record_enospc("dmu r/w range");
1798 dmu_tx_abort(tx);
1799 umem_free(packbuf, packsize);
1800 umem_free(bigbuf, bigsize);
1801 return;
1802 }
1803
1804 txg = dmu_tx_get_txg(tx);
1805
1806 /*
1807 * For each index from n to n + s, verify that the existing bufwad
1808 * in packobj matches the bufwads at the head and tail of the
1809 * corresponding chunk in bigobj. Then update all three bufwads
1810 * with the new values we want to write out.
1811 */
1812 for (i = 0; i < s; i++) {
1813 /* LINTED */
1814 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
1815 /* LINTED */
1816 bigH = (bufwad_t *)((char *)bigbuf + i * dd.dd_chunk);
1817 /* LINTED */
1818 bigT = (bufwad_t *)((char *)bigH + dd.dd_chunk) - 1;
1819
1820 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
1821 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
1822
1823 if (pack->bw_txg > txg)
1824 fatal(0, "future leak: got %llx, open txg is %llx",
1825 pack->bw_txg, txg);
1826
1827 if (pack->bw_data != 0 && pack->bw_index != n + i)
1828 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
1829 pack->bw_index, n, i);
1830
1831 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
1832 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
1833
1834 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
1835 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
1836
1837 if (freeit) {
1838 bzero(pack, sizeof (bufwad_t));
1839 } else {
1840 pack->bw_index = n + i;
1841 pack->bw_txg = txg;
1842 pack->bw_data = 1 + ztest_random(-2ULL);
1843 }
1844 *bigH = *pack;
1845 *bigT = *pack;
1846 }
1847
1848 /*
1849 * We've verified all the old bufwads, and made new ones.
1850 * Now write them out.
1851 */
1852 dmu_write(os, dd.dd_packobj, packoff, packsize, packbuf, tx);
1853
1854 if (freeit) {
1855 if (zopt_verbose >= 6) {
1856 (void) printf("freeing offset %llx size %llx"
1857 " txg %llx\n",
1858 (u_longlong_t)bigoff,
1859 (u_longlong_t)bigsize,
1860 (u_longlong_t)txg);
1861 }
1862 VERIFY(0 == dmu_free_range(os, dd.dd_bigobj, bigoff,
1863 bigsize, tx));
1864 } else {
1865 if (zopt_verbose >= 6) {
1866 (void) printf("writing offset %llx size %llx"
1867 " txg %llx\n",
1868 (u_longlong_t)bigoff,
1869 (u_longlong_t)bigsize,
1870 (u_longlong_t)txg);
1871 }
1872 dmu_write(os, dd.dd_bigobj, bigoff, bigsize, bigbuf, tx);
1873 }
1874
1875 dmu_tx_commit(tx);
1876
1877 /*
1878 * Sanity check the stuff we just wrote.
1879 */
1880 {
1881 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
1882 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
1883
1884 VERIFY(0 == dmu_read(os, dd.dd_packobj, packoff,
1885 packsize, packcheck));
1886 VERIFY(0 == dmu_read(os, dd.dd_bigobj, bigoff,
1887 bigsize, bigcheck));
1888
1889 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
1890 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
1891
1892 umem_free(packcheck, packsize);
1893 umem_free(bigcheck, bigsize);
1894 }
1895
1896 umem_free(packbuf, packsize);
1897 umem_free(bigbuf, bigsize);
1898 }
1899
1900 void
1901 ztest_dmu_check_future_leak(ztest_args_t *za)
1902 {
1903 objset_t *os = za->za_os;
1904 dmu_buf_t *db;
1905 ztest_block_tag_t *bt;
1906 dmu_object_info_t *doi = &za->za_doi;
1907
1908 /*
1909 * Make sure that, if there is a write record in the bonus buffer
1910 * of the ZTEST_DIROBJ, that the txg for this record is <= the
1911 * last synced txg of the pool.
1912 */
1913 VERIFY(dmu_bonus_hold(os, ZTEST_DIROBJ, FTAG, &db) == 0);
1914 za->za_dbuf = db;
1915 VERIFY(dmu_object_info(os, ZTEST_DIROBJ, doi) == 0);
1916 ASSERT3U(doi->doi_bonus_size, >=, sizeof (*bt));
1917 ASSERT3U(doi->doi_bonus_size, <=, db->db_size);
1918 ASSERT3U(doi->doi_bonus_size % sizeof (*bt), ==, 0);
1919 bt = (void *)((char *)db->db_data + doi->doi_bonus_size - sizeof (*bt));
1920 if (bt->bt_objset != 0) {
1921 ASSERT3U(bt->bt_objset, ==, dmu_objset_id(os));
1922 ASSERT3U(bt->bt_object, ==, ZTEST_DIROBJ);
1923 ASSERT3U(bt->bt_offset, ==, -1ULL);
1924 ASSERT3U(bt->bt_txg, <, spa_first_txg(za->za_spa));
1925 }
1926 dmu_buf_rele(db, FTAG);
1927 za->za_dbuf = NULL;
1928 }
1929
1930 void
1931 ztest_dmu_write_parallel(ztest_args_t *za)
1932 {
1933 objset_t *os = za->za_os;
1934 ztest_block_tag_t *rbt = &za->za_rbt;
1935 ztest_block_tag_t *wbt = &za->za_wbt;
1936 const size_t btsize = sizeof (ztest_block_tag_t);
1937 dmu_buf_t *db;
1938 int b, error;
1939 int bs = ZTEST_DIROBJ_BLOCKSIZE;
1940 int do_free = 0;
1941 uint64_t off, txg, txg_how;
1942 mutex_t *lp;
1943 char osname[MAXNAMELEN];
1944 char iobuf[SPA_MAXBLOCKSIZE];
1945 blkptr_t blk = { 0 };
1946 uint64_t blkoff;
1947 zbookmark_t zb;
1948 dmu_tx_t *tx = dmu_tx_create(os);
1949
1950 dmu_objset_name(os, osname);
1951
1952 /*
1953 * Have multiple threads write to large offsets in ZTEST_DIROBJ
1954 * to verify that having multiple threads writing to the same object
1955 * in parallel doesn't cause any trouble.
1956 */
1957 if (ztest_random(4) == 0) {
1958 /*
1959 * Do the bonus buffer instead of a regular block.
1960 * We need a lock to serialize resize vs. others,
1961 * so we hash on the objset ID.
1962 */
1963 b = dmu_objset_id(os) % ZTEST_SYNC_LOCKS;
1964 off = -1ULL;
1965 dmu_tx_hold_bonus(tx, ZTEST_DIROBJ);
1966 } else {
1967 b = ztest_random(ZTEST_SYNC_LOCKS);
1968 off = za->za_diroff_shared + (b << SPA_MAXBLOCKSHIFT);
1969 if (ztest_random(4) == 0) {
1970 do_free = 1;
1971 dmu_tx_hold_free(tx, ZTEST_DIROBJ, off, bs);
1972 } else {
1973 dmu_tx_hold_write(tx, ZTEST_DIROBJ, off, bs);
1974 }
1975 }
1976
1977 txg_how = ztest_random(2) == 0 ? TXG_WAIT : TXG_NOWAIT;
1978 error = dmu_tx_assign(tx, txg_how);
1979 if (error) {
1980 if (error == ERESTART) {
1981 ASSERT(txg_how == TXG_NOWAIT);
1982 dmu_tx_wait(tx);
1983 } else {
1984 ztest_record_enospc("dmu write parallel");
1985 }
1986 dmu_tx_abort(tx);
1987 return;
1988 }
1989 txg = dmu_tx_get_txg(tx);
1990
1991 lp = &ztest_shared->zs_sync_lock[b];
1992 (void) mutex_lock(lp);
1993
1994 wbt->bt_objset = dmu_objset_id(os);
1995 wbt->bt_object = ZTEST_DIROBJ;
1996 wbt->bt_offset = off;
1997 wbt->bt_txg = txg;
1998 wbt->bt_thread = za->za_instance;
1999 wbt->bt_seq = ztest_shared->zs_seq[b]++; /* protected by lp */
2000
2001 /*
2002 * Occasionally, write an all-zero block to test the behavior
2003 * of blocks that compress into holes.
2004 */
2005 if (off != -1ULL && ztest_random(8) == 0)
2006 bzero(wbt, btsize);
2007
2008 if (off == -1ULL) {
2009 dmu_object_info_t *doi = &za->za_doi;
2010 char *dboff;
2011
2012 VERIFY(dmu_bonus_hold(os, ZTEST_DIROBJ, FTAG, &db) == 0);
2013 za->za_dbuf = db;
2014 dmu_object_info_from_db(db, doi);
2015 ASSERT3U(doi->doi_bonus_size, <=, db->db_size);
2016 ASSERT3U(doi->doi_bonus_size, >=, btsize);
2017 ASSERT3U(doi->doi_bonus_size % btsize, ==, 0);
2018 dboff = (char *)db->db_data + doi->doi_bonus_size - btsize;
2019 bcopy(dboff, rbt, btsize);
2020 if (rbt->bt_objset != 0) {
2021 ASSERT3U(rbt->bt_objset, ==, wbt->bt_objset);
2022 ASSERT3U(rbt->bt_object, ==, wbt->bt_object);
2023 ASSERT3U(rbt->bt_offset, ==, wbt->bt_offset);
2024 ASSERT3U(rbt->bt_txg, <=, wbt->bt_txg);
2025 }
2026 if (ztest_random(10) == 0) {
2027 int newsize = (ztest_random(db->db_size /
2028 btsize) + 1) * btsize;
2029
2030 ASSERT3U(newsize, >=, btsize);
2031 ASSERT3U(newsize, <=, db->db_size);
2032 VERIFY3U(dmu_set_bonus(db, newsize, tx), ==, 0);
2033 dboff = (char *)db->db_data + newsize - btsize;
2034 }
2035 dmu_buf_will_dirty(db, tx);
2036 bcopy(wbt, dboff, btsize);
2037 dmu_buf_rele(db, FTAG);
2038 za->za_dbuf = NULL;
2039 } else if (do_free) {
2040 VERIFY(dmu_free_range(os, ZTEST_DIROBJ, off, bs, tx) == 0);
2041 } else {
2042 dmu_write(os, ZTEST_DIROBJ, off, btsize, wbt, tx);
2043 }
2044
2045 (void) mutex_unlock(lp);
2046
2047 if (ztest_random(1000) == 0)
2048 (void) poll(NULL, 0, 1); /* open dn_notxholds window */
2049
2050 dmu_tx_commit(tx);
2051
2052 if (ztest_random(10000) == 0)
2053 txg_wait_synced(dmu_objset_pool(os), txg);
2054
2055 if (off == -1ULL || do_free)
2056 return;
2057
2058 if (ztest_random(2) != 0)
2059 return;
2060
2061 /*
2062 * dmu_sync() the block we just wrote.
2063 */
2064 (void) mutex_lock(lp);
2065
2066 blkoff = P2ALIGN_TYPED(off, bs, uint64_t);
2067 error = dmu_buf_hold(os, ZTEST_DIROBJ, blkoff, FTAG, &db);
2068 za->za_dbuf = db;
2069 if (error) {
2070 (void) mutex_unlock(lp);
2071 return;
2072 }
2073 blkoff = off - blkoff;
2074 error = dmu_sync(NULL, db, &blk, txg, NULL, NULL);
2075 dmu_buf_rele(db, FTAG);
2076 za->za_dbuf = NULL;
2077
2078 (void) mutex_unlock(lp);
2079
2080 if (error)
2081 return;
2082
2083 if (blk.blk_birth == 0) /* concurrent free */
2084 return;
2085
2086 txg_suspend(dmu_objset_pool(os));
2087
2088 ASSERT(blk.blk_fill == 1);
2089 ASSERT3U(BP_GET_TYPE(&blk), ==, DMU_OT_UINT64_OTHER);
2090 ASSERT3U(BP_GET_LEVEL(&blk), ==, 0);
2091 ASSERT3U(BP_GET_LSIZE(&blk), ==, bs);
2092
2093 /*
2094 * Read the block that dmu_sync() returned to make sure its contents
2095 * match what we wrote. We do this while still txg_suspend()ed
2096 * to ensure that the block can't be reused before we read it.
2097 */
2098 zb.zb_objset = dmu_objset_id(os);
2099 zb.zb_object = ZTEST_DIROBJ;
2100 zb.zb_level = 0;
2101 zb.zb_blkid = off / bs;
2102 error = zio_wait(zio_read(NULL, za->za_spa, &blk, iobuf, bs,
2103 NULL, NULL, ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_MUSTSUCCEED, &zb));
2104 ASSERT3U(error, ==, 0);
2105
2106 txg_resume(dmu_objset_pool(os));
2107
2108 bcopy(&iobuf[blkoff], rbt, btsize);
2109
2110 if (rbt->bt_objset == 0) /* concurrent free */
2111 return;
2112
2113 if (wbt->bt_objset == 0) /* all-zero overwrite */
2114 return;
2115
2116 ASSERT3U(rbt->bt_objset, ==, wbt->bt_objset);
2117 ASSERT3U(rbt->bt_object, ==, wbt->bt_object);
2118 ASSERT3U(rbt->bt_offset, ==, wbt->bt_offset);
2119
2120 /*
2121 * The semantic of dmu_sync() is that we always push the most recent
2122 * version of the data, so in the face of concurrent updates we may
2123 * see a newer version of the block. That's OK.
2124 */
2125 ASSERT3U(rbt->bt_txg, >=, wbt->bt_txg);
2126 if (rbt->bt_thread == wbt->bt_thread)
2127 ASSERT3U(rbt->bt_seq, ==, wbt->bt_seq);
2128 else
2129 ASSERT3U(rbt->bt_seq, >, wbt->bt_seq);
2130 }
2131
2132 /*
2133 * Verify that zap_{create,destroy,add,remove,update} work as expected.
2134 */
2135 #define ZTEST_ZAP_MIN_INTS 1
2136 #define ZTEST_ZAP_MAX_INTS 4
2137 #define ZTEST_ZAP_MAX_PROPS 1000
2138
2139 void
2140 ztest_zap(ztest_args_t *za)
2141 {
2142 objset_t *os = za->za_os;
2143 uint64_t object;
2144 uint64_t txg, last_txg;
2145 uint64_t value[ZTEST_ZAP_MAX_INTS];
2146 uint64_t zl_ints, zl_intsize, prop;
2147 int i, ints;
2148 dmu_tx_t *tx;
2149 char propname[100], txgname[100];
2150 int error;
2151 char osname[MAXNAMELEN];
2152 char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
2153
2154 dmu_objset_name(os, osname);
2155
2156 /*
2157 * Create a new object if necessary, and record it in the directory.
2158 */
2159 VERIFY(0 == dmu_read(os, ZTEST_DIROBJ, za->za_diroff,
2160 sizeof (uint64_t), &object));
2161
2162 if (object == 0) {
2163 tx = dmu_tx_create(os);
2164 dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff,
2165 sizeof (uint64_t));
2166 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, NULL);
2167 error = dmu_tx_assign(tx, TXG_WAIT);
2168 if (error) {
2169 ztest_record_enospc("create zap test obj");
2170 dmu_tx_abort(tx);
2171 return;
2172 }
2173 object = zap_create(os, DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx);
2174 if (error) {
2175 fatal(0, "zap_create('%s', %llu) = %d",
2176 osname, object, error);
2177 }
2178 ASSERT(object != 0);
2179 dmu_write(os, ZTEST_DIROBJ, za->za_diroff,
2180 sizeof (uint64_t), &object, tx);
2181 /*
2182 * Generate a known hash collision, and verify that
2183 * we can lookup and remove both entries.
2184 */
2185 for (i = 0; i < 2; i++) {
2186 value[i] = i;
2187 error = zap_add(os, object, hc[i], sizeof (uint64_t),
2188 1, &value[i], tx);
2189 ASSERT3U(error, ==, 0);
2190 }
2191 for (i = 0; i < 2; i++) {
2192 error = zap_add(os, object, hc[i], sizeof (uint64_t),
2193 1, &value[i], tx);
2194 ASSERT3U(error, ==, EEXIST);
2195 error = zap_length(os, object, hc[i],
2196 &zl_intsize, &zl_ints);
2197 ASSERT3U(error, ==, 0);
2198 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
2199 ASSERT3U(zl_ints, ==, 1);
2200 }
2201 for (i = 0; i < 2; i++) {
2202 error = zap_remove(os, object, hc[i], tx);
2203 ASSERT3U(error, ==, 0);
2204 }
2205
2206 dmu_tx_commit(tx);
2207 }
2208
2209 ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
2210
2211 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
2212 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
2213 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
2214 bzero(value, sizeof (value));
2215 last_txg = 0;
2216
2217 /*
2218 * If these zap entries already exist, validate their contents.
2219 */
2220 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
2221 if (error == 0) {
2222 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
2223 ASSERT3U(zl_ints, ==, 1);
2224
2225 VERIFY(zap_lookup(os, object, txgname, zl_intsize,
2226 zl_ints, &last_txg) == 0);
2227
2228 VERIFY(zap_length(os, object, propname, &zl_intsize,
2229 &zl_ints) == 0);
2230
2231 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
2232 ASSERT3U(zl_ints, ==, ints);
2233
2234 VERIFY(zap_lookup(os, object, propname, zl_intsize,
2235 zl_ints, value) == 0);
2236
2237 for (i = 0; i < ints; i++) {
2238 ASSERT3U(value[i], ==, last_txg + object + i);
2239 }
2240 } else {
2241 ASSERT3U(error, ==, ENOENT);
2242 }
2243
2244 /*
2245 * Atomically update two entries in our zap object.
2246 * The first is named txg_%llu, and contains the txg
2247 * in which the property was last updated. The second
2248 * is named prop_%llu, and the nth element of its value
2249 * should be txg + object + n.
2250 */
2251 tx = dmu_tx_create(os);
2252 dmu_tx_hold_zap(tx, object, TRUE, NULL);
2253 error = dmu_tx_assign(tx, TXG_WAIT);
2254 if (error) {
2255 ztest_record_enospc("create zap entry");
2256 dmu_tx_abort(tx);
2257 return;
2258 }
2259 txg = dmu_tx_get_txg(tx);
2260
2261 if (last_txg > txg)
2262 fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
2263
2264 for (i = 0; i < ints; i++)
2265 value[i] = txg + object + i;
2266
2267 error = zap_update(os, object, txgname, sizeof (uint64_t), 1, &txg, tx);
2268 if (error)
2269 fatal(0, "zap_update('%s', %llu, '%s') = %d",
2270 osname, object, txgname, error);
2271
2272 error = zap_update(os, object, propname, sizeof (uint64_t),
2273 ints, value, tx);
2274 if (error)
2275 fatal(0, "zap_update('%s', %llu, '%s') = %d",
2276 osname, object, propname, error);
2277
2278 dmu_tx_commit(tx);
2279
2280 /*
2281 * Remove a random pair of entries.
2282 */
2283 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
2284 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
2285 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
2286
2287 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
2288
2289 if (error == ENOENT)
2290 return;
2291
2292 ASSERT3U(error, ==, 0);
2293
2294 tx = dmu_tx_create(os);
2295 dmu_tx_hold_zap(tx, object, TRUE, NULL);
2296 error = dmu_tx_assign(tx, TXG_WAIT);
2297 if (error) {
2298 ztest_record_enospc("remove zap entry");
2299 dmu_tx_abort(tx);
2300 return;
2301 }
2302 error = zap_remove(os, object, txgname, tx);
2303 if (error)
2304 fatal(0, "zap_remove('%s', %llu, '%s') = %d",
2305 osname, object, txgname, error);
2306
2307 error = zap_remove(os, object, propname, tx);
2308 if (error)
2309 fatal(0, "zap_remove('%s', %llu, '%s') = %d",
2310 osname, object, propname, error);
2311
2312 dmu_tx_commit(tx);
2313
2314 /*
2315 * Once in a while, destroy the object.
2316 */
2317 if (ztest_random(1000) != 0)
2318 return;
2319
2320 tx = dmu_tx_create(os);
2321 dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, sizeof (uint64_t));
2322 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
2323 error = dmu_tx_assign(tx, TXG_WAIT);
2324 if (error) {
2325 ztest_record_enospc("destroy zap object");
2326 dmu_tx_abort(tx);
2327 return;
2328 }
2329 error = zap_destroy(os, object, tx);
2330 if (error)
2331 fatal(0, "zap_destroy('%s', %llu) = %d",
2332 osname, object, error);
2333 object = 0;
2334 dmu_write(os, ZTEST_DIROBJ, za->za_diroff, sizeof (uint64_t),
2335 &object, tx);
2336 dmu_tx_commit(tx);
2337 }
2338
2339 void
2340 ztest_zap_parallel(ztest_args_t *za)
2341 {
2342 objset_t *os = za->za_os;
2343 uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
2344 dmu_tx_t *tx;
2345 int i, namelen, error;
2346 char name[20], string_value[20];
2347 void *data;
2348
2349 /*
2350 * Generate a random name of the form 'xxx.....' where each
2351 * x is a random printable character and the dots are dots.
2352 * There are 94 such characters, and the name length goes from
2353 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
2354 */
2355 namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
2356
2357 for (i = 0; i < 3; i++)
2358 name[i] = '!' + ztest_random('~' - '!' + 1);
2359 for (; i < namelen - 1; i++)
2360 name[i] = '.';
2361 name[i] = '\0';
2362
2363 if (ztest_random(2) == 0)
2364 object = ZTEST_MICROZAP_OBJ;
2365 else
2366 object = ZTEST_FATZAP_OBJ;
2367
2368 if ((namelen & 1) || object == ZTEST_MICROZAP_OBJ) {
2369 wsize = sizeof (txg);
2370 wc = 1;
2371 data = &txg;
2372 } else {
2373 wsize = 1;
2374 wc = namelen;
2375 data = string_value;
2376 }
2377
2378 count = -1ULL;
2379 VERIFY(zap_count(os, object, &count) == 0);
2380 ASSERT(count != -1ULL);
2381
2382 /*
2383 * Select an operation: length, lookup, add, update, remove.
2384 */
2385 i = ztest_random(5);
2386
2387 if (i >= 2) {
2388 tx = dmu_tx_create(os);
2389 dmu_tx_hold_zap(tx, object, TRUE, NULL);
2390 error = dmu_tx_assign(tx, TXG_WAIT);
2391 if (error) {
2392 ztest_record_enospc("zap parallel");
2393 dmu_tx_abort(tx);
2394 return;
2395 }
2396 txg = dmu_tx_get_txg(tx);
2397 bcopy(name, string_value, namelen);
2398 } else {
2399 tx = NULL;
2400 txg = 0;
2401 bzero(string_value, namelen);
2402 }
2403
2404 switch (i) {
2405
2406 case 0:
2407 error = zap_length(os, object, name, &zl_wsize, &zl_wc);
2408 if (error == 0) {
2409 ASSERT3U(wsize, ==, zl_wsize);
2410 ASSERT3U(wc, ==, zl_wc);
2411 } else {
2412 ASSERT3U(error, ==, ENOENT);
2413 }
2414 break;
2415
2416 case 1:
2417 error = zap_lookup(os, object, name, wsize, wc, data);
2418 if (error == 0) {
2419 if (data == string_value &&
2420 bcmp(name, data, namelen) != 0)
2421 fatal(0, "name '%s' != val '%s' len %d",
2422 name, data, namelen);
2423 } else {
2424 ASSERT3U(error, ==, ENOENT);
2425 }
2426 break;
2427
2428 case 2:
2429 error = zap_add(os, object, name, wsize, wc, data, tx);
2430 ASSERT(error == 0 || error == EEXIST);
2431 break;
2432
2433 case 3:
2434 VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
2435 break;
2436
2437 case 4:
2438 error = zap_remove(os, object, name, tx);
2439 ASSERT(error == 0 || error == ENOENT);
2440 break;
2441 }
2442
2443 if (tx != NULL)
2444 dmu_tx_commit(tx);
2445 }
2446
2447 void
2448 ztest_dsl_prop_get_set(ztest_args_t *za)
2449 {
2450 objset_t *os = za->za_os;
2451 int i, inherit;
2452 uint64_t value;
2453 const char *prop, *valname;
2454 char setpoint[MAXPATHLEN];
2455 char osname[MAXNAMELEN];
2456 int error;
2457
2458 (void) rw_rdlock(&ztest_shared->zs_name_lock);
2459
2460 dmu_objset_name(os, osname);
2461
2462 for (i = 0; i < 3; i++) {
2463 if (i == 0) {
2464 prop = "checksum";
2465 value = ztest_random_checksum();
2466 inherit = (value == ZIO_CHECKSUM_INHERIT);
2467 } else if (i == 1) {
2468 prop = "compression";
2469 value = ztest_random_compress();
2470 inherit = (value == ZIO_COMPRESS_INHERIT);
2471 } else {
2472 prop = "crypt";
2473 value = ztest_random_crypt();
2474 inherit = (value == ZIO_CRYPT_INHERIT);
2475 }
2476
2477 error = dsl_prop_set(osname, prop, sizeof (value),
2478 !inherit, &value);
2479
2480 if (error == ENOSPC) {
2481 ztest_record_enospc("dsl_prop_set");
2482 break;
2483 }
2484
2485 ASSERT3U(error, ==, 0);
2486
2487 VERIFY3U(dsl_prop_get(osname, prop, sizeof (value),
2488 1, &value, setpoint), ==, 0);
2489
2490 if (i == 0) {
2491 valname = zio_checksum_table[value].ci_name;
2492 } else if (i == 1) {
2493 valname = zio_compress_table[value].ci_name;
2494 } else {
2495 valname = zio_crypt_table[value].ci_name;
2496 }
2497
2498
2499 if (zopt_verbose >= 6) {
2500 (void) printf("%s %s = %s for '%s'\n",
2501 osname, prop, valname, setpoint);
2502 }
2503 }
2504
2505 (void) rw_unlock(&ztest_shared->zs_name_lock);
2506 }
2507
2508 /*
2509 * Inject random faults into the on-disk data.
2510 */
2511 void
2512 ztest_fault_inject(ztest_args_t *za)
2513 {
2514 int fd;
2515 uint64_t offset;
2516 uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz;
2517 uint64_t bad = 0x1990c0ffeedecade;
2518 uint64_t top, leaf;
2519 char path0[MAXPATHLEN];
2520 char pathrand[MAXPATHLEN];
2521 size_t fsize;
2522 spa_t *spa = za->za_spa;
2523 int bshift = SPA_MAXBLOCKSHIFT + 2; /* don't scrog all labels */
2524 int iters = 1000;
2525 int maxfaults = zopt_maxfaults;
2526 vdev_t *vd0 = NULL;
2527 uint64_t guid0 = 0;
2528
2529 ASSERT(leaves >= 1);
2530
2531 /*
2532 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
2533 */
2534 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
2535
2536 if (ztest_random(2) == 0) {
2537 /*
2538 * Inject errors on a normal data device.
2539 */
2540 top = ztest_random(spa->spa_root_vdev->vdev_children);
2541 leaf = ztest_random(leaves);
2542
2543 /*
2544 * Generate paths to the first leaf in this top-level vdev,
2545 * and to the random leaf we selected. We'll induce transient
2546 * write failures and random online/offline activity on leaf 0,
2547 * and we'll write random garbage to the randomly chosen leaf.
2548 */
2549 (void) snprintf(path0, sizeof (path0), ztest_dev_template,
2550 zopt_dir, zopt_pool, top * leaves + 0);
2551 (void) snprintf(pathrand, sizeof (pathrand), ztest_dev_template,
2552 zopt_dir, zopt_pool, top * leaves + leaf);
2553
2554 vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
2555 if (vd0 != NULL && maxfaults != 1) {
2556 /*
2557 * Make vd0 explicitly claim to be unreadable,
2558 * or unwriteable, or reach behind its back
2559 * and close the underlying fd. We can do this if
2560 * maxfaults == 0 because we'll fail and reexecute,
2561 * and we can do it if maxfaults >= 2 because we'll
2562 * have enough redundancy. If maxfaults == 1, the
2563 * combination of this with injection of random data
2564 * corruption below exceeds the pool's fault tolerance.
2565 */
2566 vdev_file_t *vf = vd0->vdev_tsd;
2567
2568 if (vf != NULL && ztest_random(3) == 0) {
2569 (void) close(vf->vf_vnode->v_fd);
2570 vf->vf_vnode->v_fd = -1;
2571 } else if (ztest_random(2) == 0) {
2572 vd0->vdev_cant_read = B_TRUE;
2573 } else {
2574 vd0->vdev_cant_write = B_TRUE;
2575 }
2576 guid0 = vd0->vdev_guid;
2577 }
2578 } else {
2579 /*
2580 * Inject errors on an l2cache device.
2581 */
2582 spa_aux_vdev_t *sav = &spa->spa_l2cache;
2583
2584 if (sav->sav_count == 0) {
2585 spa_config_exit(spa, SCL_STATE, FTAG);
2586 return;
2587 }
2588 vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
2589 guid0 = vd0->vdev_guid;
2590 (void) strcpy(path0, vd0->vdev_path);
2591 (void) strcpy(pathrand, vd0->vdev_path);
2592
2593 leaf = 0;
2594 leaves = 1;
2595 maxfaults = INT_MAX; /* no limit on cache devices */
2596 }
2597
2598 spa_config_exit(spa, SCL_STATE, FTAG);
2599
2600 if (maxfaults == 0)
2601 return;
2602
2603 /*
2604 * If we can tolerate two or more faults, randomly online/offline vd0.
2605 */
2606 if (maxfaults >= 2 && guid0 != 0) {
2607 if (ztest_random(10) < 6) {
2608 int flags = (ztest_random(2) == 0 ?
2609 ZFS_OFFLINE_TEMPORARY : 0);
2610 VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
2611 } else {
2612 (void) vdev_online(spa, guid0, 0, NULL);
2613 }
2614 }
2615
2616 /*
2617 * We have at least single-fault tolerance, so inject data corruption.
2618 */
2619 fd = open(pathrand, O_RDWR);
2620
2621 if (fd == -1) /* we hit a gap in the device namespace */
2622 return;
2623
2624 fsize = lseek(fd, 0, SEEK_END);
2625
2626 while (--iters != 0) {
2627 offset = ztest_random(fsize / (leaves << bshift)) *
2628 (leaves << bshift) + (leaf << bshift) +
2629 (ztest_random(1ULL << (bshift - 1)) & -8ULL);
2630
2631 if (offset >= fsize)
2632 continue;
2633
2634 if (zopt_verbose >= 6)
2635 (void) printf("injecting bad word into %s,"
2636 " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
2637
2638 if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
2639 fatal(1, "can't inject bad word at 0x%llx in %s",
2640 offset, pathrand);
2641 }
2642
2643 (void) close(fd);
2644 }
2645
2646 /*
2647 * Scrub the pool.
2648 */
2649 void
2650 ztest_scrub(ztest_args_t *za)
2651 {
2652 spa_t *spa = za->za_spa;
2653
2654 (void) spa_scrub(spa, POOL_SCRUB_EVERYTHING);
2655 (void) poll(NULL, 0, 1000); /* wait a second, then force a restart */
2656 (void) spa_scrub(spa, POOL_SCRUB_EVERYTHING);
2657 }
2658
2659 /*
2660 * Rename the pool to a different name and then rename it back.
2661 */
2662 void
2663 ztest_spa_rename(ztest_args_t *za)
2664 {
2665 char *oldname, *newname;
2666 int error;
2667 spa_t *spa;
2668
2669 (void) rw_wrlock(&ztest_shared->zs_name_lock);
2670
2671 oldname = za->za_pool;
2672 newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
2673 (void) strcpy(newname, oldname);
2674 (void) strcat(newname, "_tmp");
2675
2676 /*
2677 * Do the rename
2678 */
2679 error = spa_rename(oldname, newname);
2680 if (error)
2681 fatal(0, "spa_rename('%s', '%s') = %d", oldname,
2682 newname, error);
2683
2684 /*
2685 * Try to open it under the old name, which shouldn't exist
2686 */
2687 error = spa_open(oldname, &spa, FTAG);
2688 if (error != ENOENT)
2689 fatal(0, "spa_open('%s') = %d", oldname, error);
2690
2691 /*
2692 * Open it under the new name and make sure it's still the same spa_t.
2693 */
2694 error = spa_open(newname, &spa, FTAG);
2695 if (error != 0)
2696 fatal(0, "spa_open('%s') = %d", newname, error);
2697
2698 ASSERT(spa == za->za_spa);
2699 spa_close(spa, FTAG);
2700
2701 /*
2702 * Rename it back to the original
2703 */
2704 error = spa_rename(newname, oldname);
2705 if (error)
2706 fatal(0, "spa_rename('%s', '%s') = %d", newname,
2707 oldname, error);
2708
2709 /*
2710 * Make sure it can still be opened
2711 */
2712 error = spa_open(oldname, &spa, FTAG);
2713 if (error != 0)
2714 fatal(0, "spa_open('%s') = %d", oldname, error);
2715
2716 ASSERT(spa == za->za_spa);
2717 spa_close(spa, FTAG);
2718
2719 umem_free(newname, strlen(newname) + 1);
2720
2721 (void) rw_unlock(&ztest_shared->zs_name_lock);
2722 }
2723
2724
2725 /*
2726 * Completely obliterate one disk.
2727 */
2728 static void
2729 ztest_obliterate_one_disk(uint64_t vdev)
2730 {
2731 int fd;
2732 char dev_name[MAXPATHLEN], copy_name[MAXPATHLEN];
2733 size_t fsize;
2734
2735 if (zopt_maxfaults < 2)
2736 return;
2737
2738 (void) sprintf(dev_name, ztest_dev_template, zopt_dir, zopt_pool, vdev);
2739 (void) snprintf(copy_name, MAXPATHLEN, "%s.old", dev_name);
2740
2741 fd = open(dev_name, O_RDWR);
2742
2743 if (fd == -1)
2744 fatal(1, "can't open %s", dev_name);
2745
2746 /*
2747 * Determine the size.
2748 */
2749 fsize = lseek(fd, 0, SEEK_END);
2750
2751 (void) close(fd);
2752
2753 /*
2754 * Rename the old device to dev_name.old (useful for debugging).
2755 */
2756 VERIFY(rename(dev_name, copy_name) == 0);
2757
2758 /*
2759 * Create a new one.
2760 */
2761 VERIFY((fd = open(dev_name, O_RDWR | O_CREAT | O_TRUNC, 0666)) >= 0);
2762 VERIFY(ftruncate(fd, fsize) == 0);
2763 (void) close(fd);
2764 }
2765
2766 static void
2767 ztest_replace_one_disk(spa_t *spa, uint64_t vdev)
2768 {
2769 char dev_name[MAXPATHLEN];
2770 nvlist_t *root;
2771 int error;
2772 uint64_t guid;
2773 vdev_t *vd;
2774
2775 (void) sprintf(dev_name, ztest_dev_template, zopt_dir, zopt_pool, vdev);
2776
2777 /*
2778 * Build the nvlist describing dev_name.
2779 */
2780 root = make_vdev_root(dev_name, NULL, 0, 0, 0, 0, 0, 1);
2781
2782 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2783 if ((vd = vdev_lookup_by_path(spa->spa_root_vdev, dev_name)) == NULL)
2784 guid = 0;
2785 else
2786 guid = vd->vdev_guid;
2787 spa_config_exit(spa, SCL_VDEV, FTAG);
2788 error = spa_vdev_attach(spa, guid, root, B_TRUE);
2789 if (error != 0 &&
2790 error != EBUSY &&
2791 error != ENOTSUP &&
2792 error != ENODEV &&
2793 error != EDOM)
2794 fatal(0, "spa_vdev_attach(in-place) = %d", error);
2795
2796 nvlist_free(root);
2797 }
2798
2799 static void
2800 ztest_verify_blocks(char *pool)
2801 {
2802 int status;
2803 char zdb[MAXPATHLEN + MAXNAMELEN + 20];
2804 char zbuf[1024];
2805 char *bin;
2806 char *ztest;
2807 char *isa;
2808 int isalen;
2809 FILE *fp;
2810
2811 (void) realpath(getexecname(), zdb);
2812
2813 /* zdb lives in /usr/sbin, while ztest lives in /usr/bin */
2814 bin = strstr(zdb, "/usr/bin/");
2815 ztest = strstr(bin, "/ztest");
2816 isa = bin + 8;
2817 isalen = ztest - isa;
2818 isa = strdup(isa);
2819 /* LINTED */
2820 (void) sprintf(bin,
2821 "/usr/sbin%.*s/zdb -bc%s%s -U /tmp/zpool.cache %s",
2822 isalen,
2823 isa,
2824 zopt_verbose >= 3 ? "s" : "",
2825 zopt_verbose >= 4 ? "v" : "",
2826 pool);
2827 free(isa);
2828
2829 if (zopt_verbose >= 5)
2830 (void) printf("Executing %s\n", strstr(zdb, "zdb "));
2831
2832 fp = popen(zdb, "r");
2833
2834 while (fgets(zbuf, sizeof (zbuf), fp) != NULL)
2835 if (zopt_verbose >= 3)
2836 (void) printf("%s", zbuf);
2837
2838 status = pclose(fp);
2839
2840 if (status == 0)
2841 return;
2842
2843 ztest_dump_core = 0;
2844 if (WIFEXITED(status))
2845 fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
2846 else
2847 fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
2848 }
2849
2850 static void
2851 ztest_walk_pool_directory(char *header)
2852 {
2853 spa_t *spa = NULL;
2854
2855 if (zopt_verbose >= 6)
2856 (void) printf("%s\n", header);
2857
2858 mutex_enter(&spa_namespace_lock);
2859 while ((spa = spa_next(spa)) != NULL)
2860 if (zopt_verbose >= 6)
2861 (void) printf("\t%s\n", spa_name(spa));
2862 mutex_exit(&spa_namespace_lock);
2863 }
2864
2865 static void
2866 ztest_spa_import_export(char *oldname, char *newname)
2867 {
2868 nvlist_t *config, *newconfig;
2869 uint64_t pool_guid;
2870 spa_t *spa;
2871 int error;
2872
2873 if (zopt_verbose >= 4) {
2874 (void) printf("import/export: old = %s, new = %s\n",
2875 oldname, newname);
2876 }
2877
2878 /*
2879 * Clean up from previous runs.
2880 */
2881 (void) spa_destroy(newname);
2882
2883 /*
2884 * Get the pool's configuration and guid.
2885 */
2886 error = spa_open(oldname, &spa, FTAG);
2887 if (error)
2888 fatal(0, "spa_open('%s') = %d", oldname, error);
2889
2890 /*
2891 * Kick off a scrub to tickle scrub/export races.
2892 */
2893 if (ztest_random(2) == 0)
2894 (void) spa_scrub(spa, POOL_SCRUB_EVERYTHING);
2895
2896 pool_guid = spa_guid(spa);
2897 spa_close(spa, FTAG);
2898
2899 ztest_walk_pool_directory("pools before export");
2900
2901 /*
2902 * Export it.
2903 */
2904 error = spa_export(oldname, &config, B_FALSE, B_FALSE);
2905 if (error)
2906 fatal(0, "spa_export('%s') = %d", oldname, error);
2907
2908 ztest_walk_pool_directory("pools after export");
2909
2910 /*
2911 * Try to import it.
2912 */
2913 newconfig = spa_tryimport(config);
2914 ASSERT(newconfig != NULL);
2915 nvlist_free(newconfig);
2916
2917 /*
2918 * Import it under the new name.
2919 */
2920 error = spa_import(newname, config, NULL);
2921 if (error)
2922 fatal(0, "spa_import('%s') = %d", newname, error);
2923
2924 ztest_walk_pool_directory("pools after import");
2925
2926 /*
2927 * Try to import it again -- should fail with EEXIST.
2928 */
2929 error = spa_import(newname, config, NULL);
2930 if (error != EEXIST)
2931 fatal(0, "spa_import('%s') twice", newname);
2932
2933 /*
2934 * Try to import it under a different name -- should fail with EEXIST.
2935 */
2936 error = spa_import(oldname, config, NULL);
2937 if (error != EEXIST)
2938 fatal(0, "spa_import('%s') under multiple names", newname);
2939
2940 /*
2941 * Verify that the pool is no longer visible under the old name.
2942 */
2943 error = spa_open(oldname, &spa, FTAG);
2944 if (error != ENOENT)
2945 fatal(0, "spa_open('%s') = %d", newname, error);
2946
2947 /*
2948 * Verify that we can open and close the pool using the new name.
2949 */
2950 error = spa_open(newname, &spa, FTAG);
2951 if (error)
2952 fatal(0, "spa_open('%s') = %d", newname, error);
2953 ASSERT(pool_guid == spa_guid(spa));
2954 spa_close(spa, FTAG);
2955
2956 nvlist_free(config);
2957 }
2958
2959 static void
2960 ztest_resume(spa_t *spa)
2961 {
2962 if (spa_suspended(spa)) {
2963 spa_vdev_state_enter(spa);
2964 vdev_clear(spa, NULL);
2965 (void) spa_vdev_state_exit(spa, NULL, 0);
2966 zio_resume(spa);
2967 }
2968 }
2969
2970 static void *
2971 ztest_resume_thread(void *arg)
2972 {
2973 spa_t *spa = arg;
2974
2975 while (!ztest_exiting) {
2976 (void) poll(NULL, 0, 1000);
2977 ztest_resume(spa);
2978 }
2979 return (NULL);
2980 }
2981
2982 static void *
2983 ztest_thread(void *arg)
2984 {
2985 ztest_args_t *za = arg;
2986 ztest_shared_t *zs = ztest_shared;
2987 hrtime_t now, functime;
2988 ztest_info_t *zi;
2989 int f, i;
2990
2991 while ((now = gethrtime()) < za->za_stop) {
2992 /*
2993 * See if it's time to force a crash.
2994 */
2995 if (now > za->za_kill) {
2996 zs->zs_alloc = spa_get_alloc(za->za_spa);
2997 zs->zs_space = spa_get_space(za->za_spa);
2998 (void) kill(getpid(), SIGKILL);
2999 }
3000
3001 /*
3002 * Pick a random function.
3003 */
3004 f = ztest_random(ZTEST_FUNCS);
3005 zi = &zs->zs_info[f];
3006
3007 /*
3008 * Decide whether to call it, based on the requested frequency.
3009 */
3010 if (zi->zi_call_target == 0 ||
3011 (double)zi->zi_call_total / zi->zi_call_target >
3012 (double)(now - zs->zs_start_time) / (zopt_time * NANOSEC))
3013 continue;
3014
3015 atomic_add_64(&zi->zi_calls, 1);
3016 atomic_add_64(&zi->zi_call_total, 1);
3017
3018 za->za_diroff = (za->za_instance * ZTEST_FUNCS + f) *
3019 ZTEST_DIRSIZE;
3020 za->za_diroff_shared = (1ULL << 63);
3021
3022 for (i = 0; i < zi->zi_iters; i++)
3023 zi->zi_func(za);
3024
3025 functime = gethrtime() - now;
3026
3027 atomic_add_64(&zi->zi_call_time, functime);
3028
3029 if (zopt_verbose >= 4) {
3030 Dl_info dli;
3031 (void) dladdr((void *)zi->zi_func, &dli);
3032 (void) printf("%6.2f sec in %s\n",
3033 (double)functime / NANOSEC, dli.dli_sname);
3034 }
3035
3036 /*
3037 * If we're getting ENOSPC with some regularity, stop.
3038 */
3039 if (zs->zs_enospc_count > 10)
3040 break;
3041 }
3042
3043 return (NULL);
3044 }
3045
3046 /*
3047 * Kick off threads to run tests on all datasets in parallel.
3048 */
3049 static void
3050 ztest_run(char *pool)
3051 {
3052 int t, d, error;
3053 ztest_shared_t *zs = ztest_shared;
3054 ztest_args_t *za;
3055 spa_t *spa;
3056 char name[100];
3057 thread_t resume_tid;
3058
3059 ztest_exiting = B_FALSE;
3060
3061 (void) _mutex_init(&zs->zs_vdev_lock, USYNC_THREAD, NULL);
3062 (void) rwlock_init(&zs->zs_name_lock, USYNC_THREAD, NULL);
3063
3064 for (t = 0; t < ZTEST_SYNC_LOCKS; t++)
3065 (void) _mutex_init(&zs->zs_sync_lock[t], USYNC_THREAD, NULL);
3066
3067 /*
3068 * Destroy one disk before we even start.
3069 * It's mirrored, so everything should work just fine.
3070 * This makes us exercise fault handling very early in spa_load().
3071 */
3072 ztest_obliterate_one_disk(0);
3073
3074 /*
3075 * Verify that the sum of the sizes of all blocks in the pool
3076 * equals the SPA's allocated space total.
3077 */
3078 ztest_verify_blocks(pool);
3079
3080 /*
3081 * Kick off a replacement of the disk we just obliterated.
3082 */
3083 kernel_init(FREAD | FWRITE);
3084 VERIFY(spa_open(pool, &spa, FTAG) == 0);
3085 ztest_replace_one_disk(spa, 0);
3086 if (zopt_verbose >= 5)
3087 show_pool_stats(spa);
3088 spa_close(spa, FTAG);
3089 kernel_fini();
3090
3091 kernel_init(FREAD | FWRITE);
3092
3093 /*
3094 * Verify that we can export the pool and reimport it under a
3095 * different name.
3096 */
3097 if (ztest_random(2) == 0) {
3098 (void) snprintf(name, 100, "%s_import", pool);
3099 ztest_spa_import_export(pool, name);
3100 ztest_spa_import_export(name, pool);
3101 }
3102
3103 /*
3104 * Verify that we can loop over all pools.
3105 */
3106 mutex_enter(&spa_namespace_lock);
3107 for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa)) {
3108 if (zopt_verbose > 3) {
3109 (void) printf("spa_next: found %s\n", spa_name(spa));
3110 }
3111 }
3112 mutex_exit(&spa_namespace_lock);
3113
3114 /*
3115 * Open our pool.
3116 */
3117 VERIFY(spa_open(pool, &spa, FTAG) == 0);
3118
3119 /*
3120 * We don't expect the pool to suspend unless maxfaults == 0,
3121 * in which case ztest_fault_inject() temporarily takes away
3122 * the only valid replica.
3123 */
3124 if (zopt_maxfaults == 0)
3125 spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
3126 else
3127 spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
3128
3129 /*
3130 * Create a thread to periodically resume suspended I/O.
3131 */
3132 VERIFY(thr_create(0, 0, ztest_resume_thread, spa, THR_BOUND,
3133 &resume_tid) == 0);
3134
3135 /*
3136 * Verify that we can safely inquire about about any object,
3137 * whether it's allocated or not. To make it interesting,
3138 * we probe a 5-wide window around each power of two.
3139 * This hits all edge cases, including zero and the max.
3140 */
3141 for (t = 0; t < 64; t++) {
3142 for (d = -5; d <= 5; d++) {
3143 error = dmu_object_info(spa->spa_meta_objset,
3144 (1ULL << t) + d, NULL);
3145 ASSERT(error == 0 || error == ENOENT ||
3146 error == EINVAL);
3147 }
3148 }
3149
3150 /*
3151 * Now kick off all the tests that run in parallel.
3152 */
3153 zs->zs_enospc_count = 0;
3154
3155 za = umem_zalloc(zopt_threads * sizeof (ztest_args_t), UMEM_NOFAIL);
3156
3157 if (zopt_verbose >= 4)
3158 (void) printf("starting main threads...\n");
3159
3160 za[0].za_start = gethrtime();
3161 za[0].za_stop = za[0].za_start + zopt_passtime * NANOSEC;
3162 za[0].za_stop = MIN(za[0].za_stop, zs->zs_stop_time);
3163 za[0].za_kill = za[0].za_stop;
3164 if (ztest_random(100) < zopt_killrate)
3165 za[0].za_kill -= ztest_random(zopt_passtime * NANOSEC);
3166
3167 for (t = 0; t < zopt_threads; t++) {
3168 d = t % zopt_datasets;
3169
3170 (void) strcpy(za[t].za_pool, pool);
3171 za[t].za_os = za[d].za_os;
3172 za[t].za_spa = spa;
3173 za[t].za_zilog = za[d].za_zilog;
3174 za[t].za_instance = t;
3175 za[t].za_random = ztest_random(-1ULL);
3176 za[t].za_start = za[0].za_start;
3177 za[t].za_stop = za[0].za_stop;
3178 za[t].za_kill = za[0].za_kill;
3179
3180 if (t < zopt_datasets) {
3181 int test_future = FALSE;
3182 (void) rw_rdlock(&ztest_shared->zs_name_lock);
3183 (void) snprintf(name, 100, "%s/%s_%d", pool, pool, d);
3184 error = dmu_objset_create(name, DMU_OST_OTHER, NULL, 0,
3185 ztest_create_cb, NULL);
3186 if (error == EEXIST) {
3187 test_future = TRUE;
3188 } else if (error == ENOSPC) {
3189 zs->zs_enospc_count++;
3190 (void) rw_unlock(&ztest_shared->zs_name_lock);
3191 break;
3192 } else if (error != 0) {
3193 fatal(0, "dmu_objset_create(%s) = %d",
3194 name, error);
3195 }
3196 error = dmu_objset_open(name, DMU_OST_OTHER,
3197 DS_MODE_USER, &za[d].za_os);
3198 if (error)
3199 fatal(0, "dmu_objset_open('%s') = %d",
3200 name, error);
3201 (void) rw_unlock(&ztest_shared->zs_name_lock);
3202 if (test_future)
3203 ztest_dmu_check_future_leak(&za[t]);
3204 zil_replay(za[d].za_os, za[d].za_os,
3205 ztest_replay_vector);
3206 za[d].za_zilog = zil_open(za[d].za_os, NULL);
3207 }
3208
3209 VERIFY(thr_create(0, 0, ztest_thread, &za[t], THR_BOUND,
3210 &za[t].za_thread) == 0);
3211 }
3212
3213 while (--t >= 0) {
3214 VERIFY(thr_join(za[t].za_thread, NULL, NULL) == 0);
3215 if (t < zopt_datasets) {
3216 zil_close(za[t].za_zilog);
3217 dmu_objset_close(za[t].za_os);
3218 }
3219 }
3220
3221 if (zopt_verbose >= 3)
3222 show_pool_stats(spa);
3223
3224 txg_wait_synced(spa_get_dsl(spa), 0);
3225
3226 zs->zs_alloc = spa_get_alloc(spa);
3227 zs->zs_space = spa_get_space(spa);
3228
3229 /*
3230 * If we had out-of-space errors, destroy a random objset.
3231 */
3232 if (zs->zs_enospc_count != 0) {
3233 (void) rw_rdlock(&ztest_shared->zs_name_lock);
3234 d = (int)ztest_random(zopt_datasets);
3235 (void) snprintf(name, 100, "%s/%s_%d", pool, pool, d);
3236 if (zopt_verbose >= 3)
3237 (void) printf("Destroying %s to free up space\n", name);
3238 (void) dmu_objset_find(name, ztest_destroy_cb, &za[d],
3239 DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
3240 (void) rw_unlock(&ztest_shared->zs_name_lock);
3241 }
3242
3243 txg_wait_synced(spa_get_dsl(spa), 0);
3244
3245 umem_free(za, zopt_threads * sizeof (ztest_args_t));
3246
3247 /* Kill the resume thread */
3248 ztest_exiting = B_TRUE;
3249 VERIFY(thr_join(resume_tid, NULL, NULL) == 0);
3250 ztest_resume(spa);
3251
3252 /*
3253 * Right before closing the pool, kick off a bunch of async I/O;
3254 * spa_close() should wait for it to complete.
3255 */
3256 for (t = 1; t < 50; t++)
3257 dmu_prefetch(spa->spa_meta_objset, t, 0, 1 << 15);
3258
3259 spa_close(spa, FTAG);
3260
3261 kernel_fini();
3262 }
3263
3264 void
3265 print_time(hrtime_t t, char *timebuf)
3266 {
3267 hrtime_t s = t / NANOSEC;
3268 hrtime_t m = s / 60;
3269 hrtime_t h = m / 60;
3270 hrtime_t d = h / 24;
3271
3272 s -= m * 60;
3273 m -= h * 60;
3274 h -= d * 24;
3275
3276 timebuf[0] = '\0';
3277
3278 if (d)
3279 (void) sprintf(timebuf,
3280 "%llud%02lluh%02llum%02llus", d, h, m, s);
3281 else if (h)
3282 (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
3283 else if (m)
3284 (void) sprintf(timebuf, "%llum%02llus", m, s);
3285 else
3286 (void) sprintf(timebuf, "%llus", s);
3287 }
3288
3289 /*
3290 * Create a storage pool with the given name and initial vdev size.
3291 * Then create the specified number of datasets in the pool.
3292 */
3293 static void
3294 ztest_init(char *pool)
3295 {
3296 spa_t *spa;
3297 int error;
3298 nvlist_t *nvroot;
3299
3300 kernel_init(FREAD | FWRITE);
3301
3302 /*
3303 * Create the storage pool.
3304 */
3305 (void) spa_destroy(pool);
3306 ztest_shared->zs_vdev_primaries = 0;
3307 nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0,
3308 0, zopt_raidz, zopt_mirrors, 1);
3309 error = spa_create(pool, nvroot, NULL, NULL, NULL);
3310 nvlist_free(nvroot);
3311
3312 if (error)
3313 fatal(0, "spa_create() = %d", error);
3314 error = spa_open(pool, &spa, FTAG);
3315 if (error)
3316 fatal(0, "spa_open() = %d", error);
3317
3318 if (zopt_verbose >= 3)
3319 show_pool_stats(spa);
3320
3321 spa_close(spa, FTAG);
3322
3323 kernel_fini();
3324 }
3325
3326 int
3327 main(int argc, char **argv)
3328 {
3329 int kills = 0;
3330 int iters = 0;
3331 int i, f;
3332 ztest_shared_t *zs;
3333 ztest_info_t *zi;
3334 char timebuf[100];
3335 char numbuf[6];
3336
3337 (void) setvbuf(stdout, NULL, _IOLBF, 0);
3338
3339 /* Override location of zpool.cache */
3340 spa_config_path = "/tmp/zpool.cache";
3341
3342 ztest_random_fd = open("/dev/urandom", O_RDONLY);
3343
3344 process_options(argc, argv);
3345
3346 /*
3347 * Blow away any existing copy of zpool.cache
3348 */
3349 if (zopt_init != 0)
3350 (void) remove("/tmp/zpool.cache");
3351
3352 zs = ztest_shared = (void *)mmap(0,
3353 P2ROUNDUP(sizeof (ztest_shared_t), getpagesize()),
3354 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
3355
3356 if (zopt_verbose >= 1) {
3357 (void) printf("%llu vdevs, %d datasets, %d threads,"
3358 " %llu seconds...\n",
3359 (u_longlong_t)zopt_vdevs, zopt_datasets, zopt_threads,
3360 (u_longlong_t)zopt_time);
3361 }
3362
3363 /*
3364 * Create and initialize our storage pool.
3365 */
3366 for (i = 1; i <= zopt_init; i++) {
3367 bzero(zs, sizeof (ztest_shared_t));
3368 if (zopt_verbose >= 3 && zopt_init != 1)
3369 (void) printf("ztest_init(), pass %d\n", i);
3370 ztest_init(zopt_pool);
3371 }
3372
3373 /*
3374 * Initialize the call targets for each function.
3375 */
3376 for (f = 0; f < ZTEST_FUNCS; f++) {
3377 zi = &zs->zs_info[f];
3378
3379 *zi = ztest_info[f];
3380
3381 if (*zi->zi_interval == 0)
3382 zi->zi_call_target = UINT64_MAX;
3383 else
3384 zi->zi_call_target = zopt_time / *zi->zi_interval;
3385 }
3386
3387 zs->zs_start_time = gethrtime();
3388 zs->zs_stop_time = zs->zs_start_time + zopt_time * NANOSEC;
3389
3390 /*
3391 * Run the tests in a loop. These tests include fault injection
3392 * to verify that self-healing data works, and forced crashes
3393 * to verify that we never lose on-disk consistency.
3394 */
3395 while (gethrtime() < zs->zs_stop_time) {
3396 int status;
3397 pid_t pid;
3398 char *tmp;
3399
3400 /*
3401 * Initialize the workload counters for each function.
3402 */
3403 for (f = 0; f < ZTEST_FUNCS; f++) {
3404 zi = &zs->zs_info[f];
3405 zi->zi_calls = 0;
3406 zi->zi_call_time = 0;
3407 }
3408
3409 pid = fork();
3410
3411 if (pid == -1)
3412 fatal(1, "fork failed");
3413
3414 if (pid == 0) { /* child */
3415 struct rlimit rl = { 1024, 1024 };
3416 (void) setrlimit(RLIMIT_NOFILE, &rl);
3417 (void) enable_extended_FILE_stdio(-1, -1);
3418 ztest_run(zopt_pool);
3419 exit(0);
3420 }
3421
3422 while (waitpid(pid, &status, 0) != pid)
3423 continue;
3424
3425 if (WIFEXITED(status)) {
3426 if (WEXITSTATUS(status) != 0) {
3427 (void) fprintf(stderr,
3428 "child exited with code %d\n",
3429 WEXITSTATUS(status));
3430 exit(2);
3431 }
3432 } else if (WIFSIGNALED(status)) {
3433 if (WTERMSIG(status) != SIGKILL) {
3434 (void) fprintf(stderr,
3435 "child died with signal %d\n",
3436 WTERMSIG(status));
3437 exit(3);
3438 }
3439 kills++;
3440 } else {
3441 (void) fprintf(stderr, "something strange happened "
3442 "to child\n");
3443 exit(4);
3444 }
3445
3446 iters++;
3447
3448 if (zopt_verbose >= 1) {
3449 hrtime_t now = gethrtime();
3450
3451 now = MIN(now, zs->zs_stop_time);
3452 print_time(zs->zs_stop_time - now, timebuf);
3453 nicenum(zs->zs_space, numbuf);
3454
3455 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
3456 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
3457 iters,
3458 WIFEXITED(status) ? "Complete" : "SIGKILL",
3459 (u_longlong_t)zs->zs_enospc_count,
3460 100.0 * zs->zs_alloc / zs->zs_space,
3461 numbuf,
3462 100.0 * (now - zs->zs_start_time) /
3463 (zopt_time * NANOSEC), timebuf);
3464 }
3465
3466 if (zopt_verbose >= 2) {
3467 (void) printf("\nWorkload summary:\n\n");
3468 (void) printf("%7s %9s %s\n",
3469 "Calls", "Time", "Function");
3470 (void) printf("%7s %9s %s\n",
3471 "-----", "----", "--------");
3472 for (f = 0; f < ZTEST_FUNCS; f++) {
3473 Dl_info dli;
3474
3475 zi = &zs->zs_info[f];
3476 print_time(zi->zi_call_time, timebuf);
3477 (void) dladdr((void *)zi->zi_func, &dli);
3478 (void) printf("%7llu %9s %s\n",
3479 (u_longlong_t)zi->zi_calls, timebuf,
3480 dli.dli_sname);
3481 }
3482 (void) printf("\n");
3483 }
3484
3485 /*
3486 * It's possible that we killed a child during a rename test, in
3487 * which case we'll have a 'ztest_tmp' pool lying around instead
3488 * of 'ztest'. Do a blind rename in case this happened.
3489 */
3490 tmp = umem_alloc(strlen(zopt_pool) + 5, UMEM_NOFAIL);
3491 (void) strcpy(tmp, zopt_pool);
3492 (void) strcat(tmp, "_tmp");
3493 kernel_init(FREAD | FWRITE);
3494 (void) spa_rename(tmp, zopt_pool);
3495 kernel_fini();
3496 umem_free(tmp, strlen(tmp) + 1);
3497 }
3498
3499 ztest_verify_blocks(zopt_pool);
3500
3501 if (zopt_verbose >= 1) {
3502 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
3503 kills, iters - kills, (100.0 * kills) / MAX(1, iters));
3504 }
3505
3506 return (0);
3507 }