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