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