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