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 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* 27 * The objective of this program is to provide a DMU/ZAP/SPA stress test 28 * that runs entirely in userland, is easy to use, and easy to extend. 29 * 30 * The overall design of the ztest program is as follows: 31 * 32 * (1) For each major functional area (e.g. adding vdevs to a pool, 33 * creating and destroying datasets, reading and writing objects, etc) 34 * we have a simple routine to test that functionality. These 35 * individual routines do not have to do anything "stressful". 36 * 37 * (2) We turn these simple functionality tests into a stress test by 38 * running them all in parallel, with as many threads as desired, 39 * and spread across as many datasets, objects, and vdevs as desired. 40 * 41 * (3) While all this is happening, we inject faults into the pool to 42 * verify that self-healing data really works. 43 * 44 * (4) Every time we open a dataset, we change its checksum and compression 45 * functions. Thus even individual objects vary from block to block 46 * in which checksum they use and whether they're compressed. 47 * 48 * (5) To verify that we never lose on-disk consistency after a crash, 49 * we run the entire test in a child of the main process. 50 * At random times, the child self-immolates with a SIGKILL. 51 * This is the software equivalent of pulling the power cord. 52 * The parent then runs the test again, using the existing 53 * storage pool, as many times as desired. 54 * 55 * (6) To verify that we don't have future leaks or temporal incursions, 56 * many of the functional tests record the transaction group number 57 * as part of their data. When reading old data, they verify that 58 * the transaction group number is less than the current, open txg. 59 * If you add a new test, please do this if applicable. 60 * 61 * When run with no arguments, ztest runs for about five minutes and 62 * produces no output if successful. To get a little bit of information, 63 * specify -V. To get more information, specify -VV, and so on. 64 * 65 * To turn this into an overnight stress test, use -T to specify run time. 66 * 67 * You can ask more more vdevs [-v], datasets [-d], or threads [-t] 68 * to increase the pool capacity, fanout, and overall stress level. 69 * 70 * The -N(okill) option will suppress kills, so each child runs to completion. 71 * This can be useful when you're trying to distinguish temporal incursions 72 * from plain old race conditions. 73 */ 74 75 #include <sys/zfs_context.h> 76 #include <sys/spa.h> 77 #include <sys/dmu.h> 78 #include <sys/txg.h> 79 #include <sys/dbuf.h> 80 #include <sys/zap.h> 81 #include <sys/dmu_objset.h> 82 #include <sys/poll.h> 83 #include <sys/stat.h> 84 #include <sys/time.h> 85 #include <sys/wait.h> 86 #include <sys/mman.h> 87 #include <sys/resource.h> 88 #include <sys/zio.h> 89 #include <sys/zio_checksum.h> 90 #include <sys/zio_compress.h> 91 #include <sys/zil.h> 92 #include <sys/vdev_impl.h> 93 #include <sys/vdev_file.h> 94 #include <sys/spa_impl.h> 95 #include <sys/metaslab_impl.h> 96 #include <sys/dsl_prop.h> 97 #include <sys/dsl_dataset.h> 98 #include <sys/refcount.h> 99 #include <stdio.h> 100 #include <stdio_ext.h> 101 #include <stdlib.h> 102 #include <unistd.h> 103 #include <signal.h> 104 #include <umem.h> 105 #include <dlfcn.h> 106 #include <ctype.h> 107 #include <math.h> 108 #include <sys/fs/zfs.h> 109 110 static char cmdname[] = "ztest"; 111 static char *zopt_pool = cmdname; 112 113 static uint64_t zopt_vdevs = 5; 114 static uint64_t zopt_vdevtime; 115 static int zopt_ashift = SPA_MINBLOCKSHIFT; 116 static int zopt_mirrors = 2; 117 static int zopt_raidz = 4; 118 static int zopt_raidz_parity = 1; 119 static size_t zopt_vdev_size = SPA_MINDEVSIZE; 120 static int zopt_datasets = 7; 121 static int zopt_threads = 23; 122 static uint64_t zopt_passtime = 60; /* 60 seconds */ 123 static uint64_t zopt_killrate = 70; /* 70% kill rate */ 124 static int zopt_verbose = 0; 125 static int zopt_init = 1; 126 static char *zopt_dir = "/tmp"; 127 static uint64_t zopt_time = 300; /* 5 minutes */ 128 static int zopt_maxfaults; 129 130 typedef struct ztest_block_tag { 131 uint64_t bt_objset; 132 uint64_t bt_object; 133 uint64_t bt_offset; 134 uint64_t bt_txg; 135 uint64_t bt_thread; 136 uint64_t bt_seq; 137 } ztest_block_tag_t; 138 139 typedef struct ztest_args { 140 char za_pool[MAXNAMELEN]; 141 spa_t *za_spa; 142 objset_t *za_os; 143 zilog_t *za_zilog; 144 thread_t za_thread; 145 uint64_t za_instance; 146 uint64_t za_random; 147 uint64_t za_diroff; 148 uint64_t za_diroff_shared; 149 uint64_t za_zil_seq; 150 hrtime_t za_start; 151 hrtime_t za_stop; 152 hrtime_t za_kill; 153 /* 154 * Thread-local variables can go here to aid debugging. 155 */ 156 ztest_block_tag_t za_rbt; 157 ztest_block_tag_t za_wbt; 158 dmu_object_info_t za_doi; 159 dmu_buf_t *za_dbuf; 160 } ztest_args_t; 161 162 typedef void ztest_func_t(ztest_args_t *); 163 164 /* 165 * Note: these aren't static because we want dladdr() to work. 166 */ 167 ztest_func_t ztest_dmu_read_write; 168 ztest_func_t ztest_dmu_read_write_zcopy; 169 ztest_func_t ztest_dmu_write_parallel; 170 ztest_func_t ztest_dmu_object_alloc_free; 171 ztest_func_t ztest_dmu_commit_callbacks; 172 ztest_func_t ztest_zap; 173 ztest_func_t ztest_fzap; 174 ztest_func_t ztest_zap_parallel; 175 ztest_func_t ztest_traverse; 176 ztest_func_t ztest_dsl_prop_get_set; 177 ztest_func_t ztest_dmu_objset_create_destroy; 178 ztest_func_t ztest_dmu_snapshot_create_destroy; 179 ztest_func_t ztest_dsl_dataset_promote_busy; 180 ztest_func_t ztest_spa_create_destroy; 181 ztest_func_t ztest_fault_inject; 182 ztest_func_t ztest_spa_rename; 183 ztest_func_t ztest_vdev_attach_detach; 184 ztest_func_t ztest_vdev_LUN_growth; 185 ztest_func_t ztest_vdev_add_remove; 186 ztest_func_t ztest_vdev_aux_add_remove; 187 ztest_func_t ztest_scrub; 188 189 typedef struct ztest_info { 190 ztest_func_t *zi_func; /* test function */ 191 uint64_t zi_iters; /* iterations per execution */ 192 uint64_t *zi_interval; /* execute every <interval> seconds */ 193 uint64_t zi_calls; /* per-pass count */ 194 uint64_t zi_call_time; /* per-pass time */ 195 uint64_t zi_call_total; /* cumulative total */ 196 uint64_t zi_call_target; /* target cumulative total */ 197 } ztest_info_t; 198 199 uint64_t zopt_always = 0; /* all the time */ 200 uint64_t zopt_often = 1; /* every second */ 201 uint64_t zopt_sometimes = 10; /* every 10 seconds */ 202 uint64_t zopt_rarely = 60; /* every 60 seconds */ 203 204 ztest_info_t ztest_info[] = { 205 { ztest_dmu_read_write, 1, &zopt_always }, 206 { ztest_dmu_read_write_zcopy, 1, &zopt_always }, 207 { ztest_dmu_write_parallel, 30, &zopt_always }, 208 { ztest_dmu_object_alloc_free, 1, &zopt_always }, 209 { ztest_dmu_commit_callbacks, 10, &zopt_always }, 210 { ztest_zap, 30, &zopt_always }, 211 { ztest_fzap, 30, &zopt_always }, 212 { ztest_zap_parallel, 100, &zopt_always }, 213 { ztest_dsl_prop_get_set, 1, &zopt_sometimes }, 214 { ztest_dmu_objset_create_destroy, 1, &zopt_sometimes }, 215 { ztest_dmu_snapshot_create_destroy, 1, &zopt_sometimes }, 216 { ztest_spa_create_destroy, 1, &zopt_sometimes }, 217 { ztest_fault_inject, 1, &zopt_sometimes }, 218 { ztest_spa_rename, 1, &zopt_rarely }, 219 { ztest_vdev_attach_detach, 1, &zopt_rarely }, 220 { ztest_vdev_LUN_growth, 1, &zopt_rarely }, 221 { ztest_dsl_dataset_promote_busy, 1, &zopt_rarely }, 222 { ztest_vdev_add_remove, 1, &zopt_vdevtime }, 223 { ztest_vdev_aux_add_remove, 1, &zopt_vdevtime }, 224 { ztest_scrub, 1, &zopt_vdevtime }, 225 }; 226 227 #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t)) 228 229 #define ZTEST_SYNC_LOCKS 16 230 231 /* 232 * The following struct is used to hold a list of uncalled commit callbacks. 233 * 234 * The callbacks are ordered by txg number. 235 */ 236 typedef struct ztest_cb_list { 237 mutex_t zcl_callbacks_lock; 238 list_t zcl_callbacks; 239 } ztest_cb_list_t; 240 241 /* 242 * Stuff we need to share writably between parent and child. 243 */ 244 typedef struct ztest_shared { 245 mutex_t zs_vdev_lock; 246 rwlock_t zs_name_lock; 247 uint64_t zs_vdev_next_leaf; 248 uint64_t zs_vdev_aux; 249 uint64_t zs_enospc_count; 250 hrtime_t zs_start_time; 251 hrtime_t zs_stop_time; 252 uint64_t zs_alloc; 253 uint64_t zs_space; 254 ztest_info_t zs_info[ZTEST_FUNCS]; 255 mutex_t zs_sync_lock[ZTEST_SYNC_LOCKS]; 256 uint64_t zs_seq[ZTEST_SYNC_LOCKS]; 257 } ztest_shared_t; 258 259 static char ztest_dev_template[] = "%s/%s.%llua"; 260 static char ztest_aux_template[] = "%s/%s.%s.%llu"; 261 static ztest_shared_t *ztest_shared; 262 263 static int ztest_random_fd; 264 static int ztest_dump_core = 1; 265 266 static uint64_t metaslab_sz; 267 static boolean_t ztest_exiting; 268 269 /* Global commit callback list */ 270 static ztest_cb_list_t zcl; 271 272 extern uint64_t metaslab_gang_bang; 273 extern uint64_t metaslab_df_alloc_threshold; 274 275 #define ZTEST_DIROBJ 1 276 #define ZTEST_MICROZAP_OBJ 2 277 #define ZTEST_FATZAP_OBJ 3 278 279 #define ZTEST_DIROBJ_BLOCKSIZE (1 << 10) 280 #define ZTEST_DIRSIZE 256 281 282 static void usage(boolean_t) __NORETURN; 283 284 /* 285 * These libumem hooks provide a reasonable set of defaults for the allocator's 286 * debugging facilities. 287 */ 288 const char * 289 _umem_debug_init() 290 { 291 return ("default,verbose"); /* $UMEM_DEBUG setting */ 292 } 293 294 const char * 295 _umem_logging_init(void) 296 { 297 return ("fail,contents"); /* $UMEM_LOGGING setting */ 298 } 299 300 #define FATAL_MSG_SZ 1024 301 302 char *fatal_msg; 303 304 static void 305 fatal(int do_perror, char *message, ...) 306 { 307 va_list args; 308 int save_errno = errno; 309 char buf[FATAL_MSG_SZ]; 310 311 (void) fflush(stdout); 312 313 va_start(args, message); 314 (void) sprintf(buf, "ztest: "); 315 /* LINTED */ 316 (void) vsprintf(buf + strlen(buf), message, args); 317 va_end(args); 318 if (do_perror) { 319 (void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf), 320 ": %s", strerror(save_errno)); 321 } 322 (void) fprintf(stderr, "%s\n", buf); 323 fatal_msg = buf; /* to ease debugging */ 324 if (ztest_dump_core) 325 abort(); 326 exit(3); 327 } 328 329 static int 330 str2shift(const char *buf) 331 { 332 const char *ends = "BKMGTPEZ"; 333 int i; 334 335 if (buf[0] == '\0') 336 return (0); 337 for (i = 0; i < strlen(ends); i++) { 338 if (toupper(buf[0]) == ends[i]) 339 break; 340 } 341 if (i == strlen(ends)) { 342 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", 343 buf); 344 usage(B_FALSE); 345 } 346 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) { 347 return (10*i); 348 } 349 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf); 350 usage(B_FALSE); 351 /* NOTREACHED */ 352 } 353 354 static uint64_t 355 nicenumtoull(const char *buf) 356 { 357 char *end; 358 uint64_t val; 359 360 val = strtoull(buf, &end, 0); 361 if (end == buf) { 362 (void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf); 363 usage(B_FALSE); 364 } else if (end[0] == '.') { 365 double fval = strtod(buf, &end); 366 fval *= pow(2, str2shift(end)); 367 if (fval > UINT64_MAX) { 368 (void) fprintf(stderr, "ztest: value too large: %s\n", 369 buf); 370 usage(B_FALSE); 371 } 372 val = (uint64_t)fval; 373 } else { 374 int shift = str2shift(end); 375 if (shift >= 64 || (val << shift) >> shift != val) { 376 (void) fprintf(stderr, "ztest: value too large: %s\n", 377 buf); 378 usage(B_FALSE); 379 } 380 val <<= shift; 381 } 382 return (val); 383 } 384 385 static void 386 usage(boolean_t requested) 387 { 388 char nice_vdev_size[10]; 389 char nice_gang_bang[10]; 390 FILE *fp = requested ? stdout : stderr; 391 392 nicenum(zopt_vdev_size, nice_vdev_size); 393 nicenum(metaslab_gang_bang, nice_gang_bang); 394 395 (void) fprintf(fp, "Usage: %s\n" 396 "\t[-v vdevs (default: %llu)]\n" 397 "\t[-s size_of_each_vdev (default: %s)]\n" 398 "\t[-a alignment_shift (default: %d) (use 0 for random)]\n" 399 "\t[-m mirror_copies (default: %d)]\n" 400 "\t[-r raidz_disks (default: %d)]\n" 401 "\t[-R raidz_parity (default: %d)]\n" 402 "\t[-d datasets (default: %d)]\n" 403 "\t[-t threads (default: %d)]\n" 404 "\t[-g gang_block_threshold (default: %s)]\n" 405 "\t[-i initialize pool i times (default: %d)]\n" 406 "\t[-k kill percentage (default: %llu%%)]\n" 407 "\t[-p pool_name (default: %s)]\n" 408 "\t[-f file directory for vdev files (default: %s)]\n" 409 "\t[-V(erbose)] (use multiple times for ever more blather)\n" 410 "\t[-E(xisting)] (use existing pool instead of creating new one)\n" 411 "\t[-T time] total run time (default: %llu sec)\n" 412 "\t[-P passtime] time per pass (default: %llu sec)\n" 413 "\t[-h] (print help)\n" 414 "", 415 cmdname, 416 (u_longlong_t)zopt_vdevs, /* -v */ 417 nice_vdev_size, /* -s */ 418 zopt_ashift, /* -a */ 419 zopt_mirrors, /* -m */ 420 zopt_raidz, /* -r */ 421 zopt_raidz_parity, /* -R */ 422 zopt_datasets, /* -d */ 423 zopt_threads, /* -t */ 424 nice_gang_bang, /* -g */ 425 zopt_init, /* -i */ 426 (u_longlong_t)zopt_killrate, /* -k */ 427 zopt_pool, /* -p */ 428 zopt_dir, /* -f */ 429 (u_longlong_t)zopt_time, /* -T */ 430 (u_longlong_t)zopt_passtime); /* -P */ 431 exit(requested ? 0 : 1); 432 } 433 434 static uint64_t 435 ztest_random(uint64_t range) 436 { 437 uint64_t r; 438 439 if (range == 0) 440 return (0); 441 442 if (read(ztest_random_fd, &r, sizeof (r)) != sizeof (r)) 443 fatal(1, "short read from /dev/urandom"); 444 445 return (r % range); 446 } 447 448 /* ARGSUSED */ 449 static void 450 ztest_record_enospc(char *s) 451 { 452 ztest_shared->zs_enospc_count++; 453 } 454 455 static void 456 process_options(int argc, char **argv) 457 { 458 int opt; 459 uint64_t value; 460 461 /* By default, test gang blocks for blocks 32K and greater */ 462 metaslab_gang_bang = 32 << 10; 463 464 while ((opt = getopt(argc, argv, 465 "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:h")) != EOF) { 466 value = 0; 467 switch (opt) { 468 case 'v': 469 case 's': 470 case 'a': 471 case 'm': 472 case 'r': 473 case 'R': 474 case 'd': 475 case 't': 476 case 'g': 477 case 'i': 478 case 'k': 479 case 'T': 480 case 'P': 481 value = nicenumtoull(optarg); 482 } 483 switch (opt) { 484 case 'v': 485 zopt_vdevs = value; 486 break; 487 case 's': 488 zopt_vdev_size = MAX(SPA_MINDEVSIZE, value); 489 break; 490 case 'a': 491 zopt_ashift = value; 492 break; 493 case 'm': 494 zopt_mirrors = value; 495 break; 496 case 'r': 497 zopt_raidz = MAX(1, value); 498 break; 499 case 'R': 500 zopt_raidz_parity = MIN(MAX(value, 1), 3); 501 break; 502 case 'd': 503 zopt_datasets = MAX(1, value); 504 break; 505 case 't': 506 zopt_threads = MAX(1, value); 507 break; 508 case 'g': 509 metaslab_gang_bang = MAX(SPA_MINBLOCKSIZE << 1, value); 510 break; 511 case 'i': 512 zopt_init = value; 513 break; 514 case 'k': 515 zopt_killrate = value; 516 break; 517 case 'p': 518 zopt_pool = strdup(optarg); 519 break; 520 case 'f': 521 zopt_dir = strdup(optarg); 522 break; 523 case 'V': 524 zopt_verbose++; 525 break; 526 case 'E': 527 zopt_init = 0; 528 break; 529 case 'T': 530 zopt_time = value; 531 break; 532 case 'P': 533 zopt_passtime = MAX(1, value); 534 break; 535 case 'h': 536 usage(B_TRUE); 537 break; 538 case '?': 539 default: 540 usage(B_FALSE); 541 break; 542 } 543 } 544 545 zopt_raidz_parity = MIN(zopt_raidz_parity, zopt_raidz - 1); 546 547 zopt_vdevtime = (zopt_vdevs > 0 ? zopt_time / zopt_vdevs : UINT64_MAX); 548 zopt_maxfaults = MAX(zopt_mirrors, 1) * (zopt_raidz_parity + 1) - 1; 549 } 550 551 static uint64_t 552 ztest_get_ashift(void) 553 { 554 if (zopt_ashift == 0) 555 return (SPA_MINBLOCKSHIFT + ztest_random(3)); 556 return (zopt_ashift); 557 } 558 559 static nvlist_t * 560 make_vdev_file(char *path, char *aux, size_t size, uint64_t ashift) 561 { 562 char pathbuf[MAXPATHLEN]; 563 uint64_t vdev; 564 nvlist_t *file; 565 566 if (ashift == 0) 567 ashift = ztest_get_ashift(); 568 569 if (path == NULL) { 570 path = pathbuf; 571 572 if (aux != NULL) { 573 vdev = ztest_shared->zs_vdev_aux; 574 (void) sprintf(path, ztest_aux_template, 575 zopt_dir, zopt_pool, aux, vdev); 576 } else { 577 vdev = ztest_shared->zs_vdev_next_leaf++; 578 (void) sprintf(path, ztest_dev_template, 579 zopt_dir, zopt_pool, vdev); 580 } 581 } 582 583 if (size != 0) { 584 int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666); 585 if (fd == -1) 586 fatal(1, "can't open %s", path); 587 if (ftruncate(fd, size) != 0) 588 fatal(1, "can't ftruncate %s", path); 589 (void) close(fd); 590 } 591 592 VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0); 593 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0); 594 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0); 595 VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0); 596 597 return (file); 598 } 599 600 static nvlist_t * 601 make_vdev_raidz(char *path, char *aux, size_t size, uint64_t ashift, int r) 602 { 603 nvlist_t *raidz, **child; 604 int c; 605 606 if (r < 2) 607 return (make_vdev_file(path, aux, size, ashift)); 608 child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL); 609 610 for (c = 0; c < r; c++) 611 child[c] = make_vdev_file(path, aux, size, ashift); 612 613 VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0); 614 VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE, 615 VDEV_TYPE_RAIDZ) == 0); 616 VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY, 617 zopt_raidz_parity) == 0); 618 VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN, 619 child, r) == 0); 620 621 for (c = 0; c < r; c++) 622 nvlist_free(child[c]); 623 624 umem_free(child, r * sizeof (nvlist_t *)); 625 626 return (raidz); 627 } 628 629 static nvlist_t * 630 make_vdev_mirror(char *path, char *aux, size_t size, uint64_t ashift, 631 int r, int m) 632 { 633 nvlist_t *mirror, **child; 634 int c; 635 636 if (m < 1) 637 return (make_vdev_raidz(path, aux, size, ashift, r)); 638 639 child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL); 640 641 for (c = 0; c < m; c++) 642 child[c] = make_vdev_raidz(path, aux, size, ashift, r); 643 644 VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0); 645 VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE, 646 VDEV_TYPE_MIRROR) == 0); 647 VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN, 648 child, m) == 0); 649 650 for (c = 0; c < m; c++) 651 nvlist_free(child[c]); 652 653 umem_free(child, m * sizeof (nvlist_t *)); 654 655 return (mirror); 656 } 657 658 static nvlist_t * 659 make_vdev_root(char *path, char *aux, size_t size, uint64_t ashift, 660 int log, int r, int m, int t) 661 { 662 nvlist_t *root, **child; 663 int c; 664 665 ASSERT(t > 0); 666 667 child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL); 668 669 for (c = 0; c < t; c++) { 670 child[c] = make_vdev_mirror(path, aux, size, ashift, r, m); 671 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 672 log) == 0); 673 } 674 675 VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0); 676 VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0); 677 VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN, 678 child, t) == 0); 679 680 for (c = 0; c < t; c++) 681 nvlist_free(child[c]); 682 683 umem_free(child, t * sizeof (nvlist_t *)); 684 685 return (root); 686 } 687 688 static void 689 ztest_set_random_blocksize(objset_t *os, uint64_t object, dmu_tx_t *tx) 690 { 691 int bs = SPA_MINBLOCKSHIFT + 692 ztest_random(SPA_MAXBLOCKSHIFT - SPA_MINBLOCKSHIFT + 1); 693 int ibs = DN_MIN_INDBLKSHIFT + 694 ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1); 695 int error; 696 697 error = dmu_object_set_blocksize(os, object, 1ULL << bs, ibs, tx); 698 if (error) { 699 char osname[300]; 700 dmu_objset_name(os, osname); 701 fatal(0, "dmu_object_set_blocksize('%s', %llu, %d, %d) = %d", 702 osname, object, 1 << bs, ibs, error); 703 } 704 } 705 706 static uint8_t 707 ztest_random_checksum(void) 708 { 709 uint8_t checksum; 710 711 do { 712 checksum = ztest_random(ZIO_CHECKSUM_FUNCTIONS); 713 } while (zio_checksum_table[checksum].ci_zbt); 714 715 if (checksum == ZIO_CHECKSUM_OFF) 716 checksum = ZIO_CHECKSUM_ON; 717 718 return (checksum); 719 } 720 721 static uint8_t 722 ztest_random_compress(void) 723 { 724 return ((uint8_t)ztest_random(ZIO_COMPRESS_FUNCTIONS)); 725 } 726 727 static int 728 ztest_replay_create(objset_t *os, lr_create_t *lr, boolean_t byteswap) 729 { 730 dmu_tx_t *tx; 731 int error; 732 733 if (byteswap) 734 byteswap_uint64_array(lr, sizeof (*lr)); 735 736 tx = dmu_tx_create(os); 737 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 738 error = dmu_tx_assign(tx, TXG_WAIT); 739 if (error) { 740 dmu_tx_abort(tx); 741 return (error); 742 } 743 744 error = dmu_object_claim(os, lr->lr_doid, lr->lr_mode, 0, 745 DMU_OT_NONE, 0, tx); 746 ASSERT3U(error, ==, 0); 747 dmu_tx_commit(tx); 748 749 if (zopt_verbose >= 5) { 750 char osname[MAXNAMELEN]; 751 dmu_objset_name(os, osname); 752 (void) printf("replay create of %s object %llu" 753 " in txg %llu = %d\n", 754 osname, (u_longlong_t)lr->lr_doid, 755 (u_longlong_t)dmu_tx_get_txg(tx), error); 756 } 757 758 return (error); 759 } 760 761 static int 762 ztest_replay_remove(objset_t *os, lr_remove_t *lr, boolean_t byteswap) 763 { 764 dmu_tx_t *tx; 765 int error; 766 767 if (byteswap) 768 byteswap_uint64_array(lr, sizeof (*lr)); 769 770 tx = dmu_tx_create(os); 771 dmu_tx_hold_free(tx, lr->lr_doid, 0, DMU_OBJECT_END); 772 error = dmu_tx_assign(tx, TXG_WAIT); 773 if (error) { 774 dmu_tx_abort(tx); 775 return (error); 776 } 777 778 error = dmu_object_free(os, lr->lr_doid, tx); 779 dmu_tx_commit(tx); 780 781 return (error); 782 } 783 784 zil_replay_func_t *ztest_replay_vector[TX_MAX_TYPE] = { 785 NULL, /* 0 no such transaction type */ 786 ztest_replay_create, /* TX_CREATE */ 787 NULL, /* TX_MKDIR */ 788 NULL, /* TX_MKXATTR */ 789 NULL, /* TX_SYMLINK */ 790 ztest_replay_remove, /* TX_REMOVE */ 791 NULL, /* TX_RMDIR */ 792 NULL, /* TX_LINK */ 793 NULL, /* TX_RENAME */ 794 NULL, /* TX_WRITE */ 795 NULL, /* TX_TRUNCATE */ 796 NULL, /* TX_SETATTR */ 797 NULL, /* TX_ACL */ 798 }; 799 800 /* 801 * Verify that we can't destroy an active pool, create an existing pool, 802 * or create a pool with a bad vdev spec. 803 */ 804 void 805 ztest_spa_create_destroy(ztest_args_t *za) 806 { 807 int error; 808 spa_t *spa; 809 nvlist_t *nvroot; 810 811 /* 812 * Attempt to create using a bad file. 813 */ 814 nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1); 815 error = spa_create("ztest_bad_file", nvroot, NULL, NULL, NULL); 816 nvlist_free(nvroot); 817 if (error != ENOENT) 818 fatal(0, "spa_create(bad_file) = %d", error); 819 820 /* 821 * Attempt to create using a bad mirror. 822 */ 823 nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 2, 1); 824 error = spa_create("ztest_bad_mirror", nvroot, NULL, NULL, NULL); 825 nvlist_free(nvroot); 826 if (error != ENOENT) 827 fatal(0, "spa_create(bad_mirror) = %d", error); 828 829 /* 830 * Attempt to create an existing pool. It shouldn't matter 831 * what's in the nvroot; we should fail with EEXIST. 832 */ 833 (void) rw_rdlock(&ztest_shared->zs_name_lock); 834 nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1); 835 error = spa_create(za->za_pool, nvroot, NULL, NULL, NULL); 836 nvlist_free(nvroot); 837 if (error != EEXIST) 838 fatal(0, "spa_create(whatever) = %d", error); 839 840 error = spa_open(za->za_pool, &spa, FTAG); 841 if (error) 842 fatal(0, "spa_open() = %d", error); 843 844 error = spa_destroy(za->za_pool); 845 if (error != EBUSY) 846 fatal(0, "spa_destroy() = %d", error); 847 848 spa_close(spa, FTAG); 849 (void) rw_unlock(&ztest_shared->zs_name_lock); 850 } 851 852 static vdev_t * 853 vdev_lookup_by_path(vdev_t *vd, const char *path) 854 { 855 vdev_t *mvd; 856 857 if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0) 858 return (vd); 859 860 for (int c = 0; c < vd->vdev_children; c++) 861 if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) != 862 NULL) 863 return (mvd); 864 865 return (NULL); 866 } 867 868 /* 869 * Find the first available hole which can be used as a top-level. 870 */ 871 int 872 find_vdev_hole(spa_t *spa) 873 { 874 vdev_t *rvd = spa->spa_root_vdev; 875 int c; 876 877 ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV); 878 879 for (c = 0; c < rvd->vdev_children; c++) { 880 vdev_t *cvd = rvd->vdev_child[c]; 881 882 if (cvd->vdev_ishole) 883 break; 884 } 885 return (c); 886 } 887 888 /* 889 * Verify that vdev_add() works as expected. 890 */ 891 void 892 ztest_vdev_add_remove(ztest_args_t *za) 893 { 894 spa_t *spa = za->za_spa; 895 uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz; 896 uint64_t guid; 897 nvlist_t *nvroot; 898 int error; 899 900 (void) mutex_lock(&ztest_shared->zs_vdev_lock); 901 902 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); 903 904 ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves; 905 906 /* 907 * If we have slogs then remove them 1/4 of the time. 908 */ 909 if (spa_has_slogs(spa) && ztest_random(4) == 0) { 910 /* 911 * Grab the guid from the head of the log class rotor. 912 */ 913 guid = spa->spa_log_class->mc_rotor->mg_vd->vdev_guid; 914 915 spa_config_exit(spa, SCL_VDEV, FTAG); 916 917 /* 918 * We have to grab the zs_name_lock as writer to 919 * prevent a race between removing a slog (dmu_objset_find) 920 * and destroying a dataset. Removing the slog will 921 * grab a reference on the dataset which may cause 922 * dmu_objset_destroy() to fail with EBUSY thus 923 * leaving the dataset in an inconsistent state. 924 */ 925 (void) rw_wrlock(&ztest_shared->zs_name_lock); 926 error = spa_vdev_remove(spa, guid, B_FALSE); 927 (void) rw_unlock(&ztest_shared->zs_name_lock); 928 929 if (error && error != EEXIST) 930 fatal(0, "spa_vdev_remove() = %d", error); 931 } else { 932 spa_config_exit(spa, SCL_VDEV, FTAG); 933 934 /* 935 * Make 1/4 of the devices be log devices. 936 */ 937 nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0, 938 ztest_random(4) == 0, zopt_raidz, zopt_mirrors, 1); 939 940 error = spa_vdev_add(spa, nvroot); 941 nvlist_free(nvroot); 942 943 if (error == ENOSPC) 944 ztest_record_enospc("spa_vdev_add"); 945 else if (error != 0) 946 fatal(0, "spa_vdev_add() = %d", error); 947 } 948 949 (void) mutex_unlock(&ztest_shared->zs_vdev_lock); 950 } 951 952 /* 953 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected. 954 */ 955 void 956 ztest_vdev_aux_add_remove(ztest_args_t *za) 957 { 958 spa_t *spa = za->za_spa; 959 vdev_t *rvd = spa->spa_root_vdev; 960 spa_aux_vdev_t *sav; 961 char *aux; 962 uint64_t guid = 0; 963 int error; 964 965 if (ztest_random(2) == 0) { 966 sav = &spa->spa_spares; 967 aux = ZPOOL_CONFIG_SPARES; 968 } else { 969 sav = &spa->spa_l2cache; 970 aux = ZPOOL_CONFIG_L2CACHE; 971 } 972 973 (void) mutex_lock(&ztest_shared->zs_vdev_lock); 974 975 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); 976 977 if (sav->sav_count != 0 && ztest_random(4) == 0) { 978 /* 979 * Pick a random device to remove. 980 */ 981 guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid; 982 } else { 983 /* 984 * Find an unused device we can add. 985 */ 986 ztest_shared->zs_vdev_aux = 0; 987 for (;;) { 988 char path[MAXPATHLEN]; 989 int c; 990 (void) sprintf(path, ztest_aux_template, zopt_dir, 991 zopt_pool, aux, ztest_shared->zs_vdev_aux); 992 for (c = 0; c < sav->sav_count; c++) 993 if (strcmp(sav->sav_vdevs[c]->vdev_path, 994 path) == 0) 995 break; 996 if (c == sav->sav_count && 997 vdev_lookup_by_path(rvd, path) == NULL) 998 break; 999 ztest_shared->zs_vdev_aux++; 1000 } 1001 } 1002 1003 spa_config_exit(spa, SCL_VDEV, FTAG); 1004 1005 if (guid == 0) { 1006 /* 1007 * Add a new device. 1008 */ 1009 nvlist_t *nvroot = make_vdev_root(NULL, aux, 1010 (zopt_vdev_size * 5) / 4, 0, 0, 0, 0, 1); 1011 error = spa_vdev_add(spa, nvroot); 1012 if (error != 0) 1013 fatal(0, "spa_vdev_add(%p) = %d", nvroot, error); 1014 nvlist_free(nvroot); 1015 } else { 1016 /* 1017 * Remove an existing device. Sometimes, dirty its 1018 * vdev state first to make sure we handle removal 1019 * of devices that have pending state changes. 1020 */ 1021 if (ztest_random(2) == 0) 1022 (void) vdev_online(spa, guid, 0, NULL); 1023 1024 error = spa_vdev_remove(spa, guid, B_FALSE); 1025 if (error != 0 && error != EBUSY) 1026 fatal(0, "spa_vdev_remove(%llu) = %d", guid, error); 1027 } 1028 1029 (void) mutex_unlock(&ztest_shared->zs_vdev_lock); 1030 } 1031 1032 /* 1033 * Verify that we can attach and detach devices. 1034 */ 1035 void 1036 ztest_vdev_attach_detach(ztest_args_t *za) 1037 { 1038 spa_t *spa = za->za_spa; 1039 spa_aux_vdev_t *sav = &spa->spa_spares; 1040 vdev_t *rvd = spa->spa_root_vdev; 1041 vdev_t *oldvd, *newvd, *pvd; 1042 nvlist_t *root; 1043 uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz; 1044 uint64_t leaf, top; 1045 uint64_t ashift = ztest_get_ashift(); 1046 uint64_t oldguid, pguid; 1047 size_t oldsize, newsize; 1048 char oldpath[MAXPATHLEN], newpath[MAXPATHLEN]; 1049 int replacing; 1050 int oldvd_has_siblings = B_FALSE; 1051 int newvd_is_spare = B_FALSE; 1052 int oldvd_is_log; 1053 int error, expected_error; 1054 1055 (void) mutex_lock(&ztest_shared->zs_vdev_lock); 1056 1057 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); 1058 1059 /* 1060 * Decide whether to do an attach or a replace. 1061 */ 1062 replacing = ztest_random(2); 1063 1064 /* 1065 * Pick a random top-level vdev. 1066 */ 1067 top = ztest_random(rvd->vdev_children); 1068 1069 /* 1070 * Pick a random leaf within it. 1071 */ 1072 leaf = ztest_random(leaves); 1073 1074 /* 1075 * Locate this vdev. 1076 */ 1077 oldvd = rvd->vdev_child[top]; 1078 if (zopt_mirrors >= 1) { 1079 ASSERT(oldvd->vdev_ops == &vdev_mirror_ops); 1080 ASSERT(oldvd->vdev_children >= zopt_mirrors); 1081 oldvd = oldvd->vdev_child[leaf / zopt_raidz]; 1082 } 1083 if (zopt_raidz > 1) { 1084 ASSERT(oldvd->vdev_ops == &vdev_raidz_ops); 1085 ASSERT(oldvd->vdev_children == zopt_raidz); 1086 oldvd = oldvd->vdev_child[leaf % zopt_raidz]; 1087 } 1088 1089 /* 1090 * If we're already doing an attach or replace, oldvd may be a 1091 * mirror vdev -- in which case, pick a random child. 1092 */ 1093 while (oldvd->vdev_children != 0) { 1094 oldvd_has_siblings = B_TRUE; 1095 ASSERT(oldvd->vdev_children >= 2); 1096 oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)]; 1097 } 1098 1099 oldguid = oldvd->vdev_guid; 1100 oldsize = vdev_get_min_asize(oldvd); 1101 oldvd_is_log = oldvd->vdev_top->vdev_islog; 1102 (void) strcpy(oldpath, oldvd->vdev_path); 1103 pvd = oldvd->vdev_parent; 1104 pguid = pvd->vdev_guid; 1105 1106 /* 1107 * If oldvd has siblings, then half of the time, detach it. 1108 */ 1109 if (oldvd_has_siblings && ztest_random(2) == 0) { 1110 spa_config_exit(spa, SCL_VDEV, FTAG); 1111 error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE); 1112 if (error != 0 && error != ENODEV && error != EBUSY && 1113 error != ENOTSUP) 1114 fatal(0, "detach (%s) returned %d", oldpath, error); 1115 (void) mutex_unlock(&ztest_shared->zs_vdev_lock); 1116 return; 1117 } 1118 1119 /* 1120 * For the new vdev, choose with equal probability between the two 1121 * standard paths (ending in either 'a' or 'b') or a random hot spare. 1122 */ 1123 if (sav->sav_count != 0 && ztest_random(3) == 0) { 1124 newvd = sav->sav_vdevs[ztest_random(sav->sav_count)]; 1125 newvd_is_spare = B_TRUE; 1126 (void) strcpy(newpath, newvd->vdev_path); 1127 } else { 1128 (void) snprintf(newpath, sizeof (newpath), ztest_dev_template, 1129 zopt_dir, zopt_pool, top * leaves + leaf); 1130 if (ztest_random(2) == 0) 1131 newpath[strlen(newpath) - 1] = 'b'; 1132 newvd = vdev_lookup_by_path(rvd, newpath); 1133 } 1134 1135 if (newvd) { 1136 newsize = vdev_get_min_asize(newvd); 1137 } else { 1138 /* 1139 * Make newsize a little bigger or smaller than oldsize. 1140 * If it's smaller, the attach should fail. 1141 * If it's larger, and we're doing a replace, 1142 * we should get dynamic LUN growth when we're done. 1143 */ 1144 newsize = 10 * oldsize / (9 + ztest_random(3)); 1145 } 1146 1147 /* 1148 * If pvd is not a mirror or root, the attach should fail with ENOTSUP, 1149 * unless it's a replace; in that case any non-replacing parent is OK. 1150 * 1151 * If newvd is already part of the pool, it should fail with EBUSY. 1152 * 1153 * If newvd is too small, it should fail with EOVERFLOW. 1154 */ 1155 if (pvd->vdev_ops != &vdev_mirror_ops && 1156 pvd->vdev_ops != &vdev_root_ops && (!replacing || 1157 pvd->vdev_ops == &vdev_replacing_ops || 1158 pvd->vdev_ops == &vdev_spare_ops)) 1159 expected_error = ENOTSUP; 1160 else if (newvd_is_spare && (!replacing || oldvd_is_log)) 1161 expected_error = ENOTSUP; 1162 else if (newvd == oldvd) 1163 expected_error = replacing ? 0 : EBUSY; 1164 else if (vdev_lookup_by_path(rvd, newpath) != NULL) 1165 expected_error = EBUSY; 1166 else if (newsize < oldsize) 1167 expected_error = EOVERFLOW; 1168 else if (ashift > oldvd->vdev_top->vdev_ashift) 1169 expected_error = EDOM; 1170 else 1171 expected_error = 0; 1172 1173 spa_config_exit(spa, SCL_VDEV, FTAG); 1174 1175 /* 1176 * Build the nvlist describing newpath. 1177 */ 1178 root = make_vdev_root(newpath, NULL, newvd == NULL ? newsize : 0, 1179 ashift, 0, 0, 0, 1); 1180 1181 error = spa_vdev_attach(spa, oldguid, root, replacing); 1182 1183 nvlist_free(root); 1184 1185 /* 1186 * If our parent was the replacing vdev, but the replace completed, 1187 * then instead of failing with ENOTSUP we may either succeed, 1188 * fail with ENODEV, or fail with EOVERFLOW. 1189 */ 1190 if (expected_error == ENOTSUP && 1191 (error == 0 || error == ENODEV || error == EOVERFLOW)) 1192 expected_error = error; 1193 1194 /* 1195 * If someone grew the LUN, the replacement may be too small. 1196 */ 1197 if (error == EOVERFLOW || error == EBUSY) 1198 expected_error = error; 1199 1200 /* XXX workaround 6690467 */ 1201 if (error != expected_error && expected_error != EBUSY) { 1202 fatal(0, "attach (%s %llu, %s %llu, %d) " 1203 "returned %d, expected %d", 1204 oldpath, (longlong_t)oldsize, newpath, 1205 (longlong_t)newsize, replacing, error, expected_error); 1206 } 1207 1208 (void) mutex_unlock(&ztest_shared->zs_vdev_lock); 1209 } 1210 1211 /* 1212 * Callback function which expands the physical size of the vdev. 1213 */ 1214 vdev_t * 1215 grow_vdev(vdev_t *vd, void *arg) 1216 { 1217 spa_t *spa = vd->vdev_spa; 1218 size_t *newsize = arg; 1219 size_t fsize; 1220 int fd; 1221 1222 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE); 1223 ASSERT(vd->vdev_ops->vdev_op_leaf); 1224 1225 if ((fd = open(vd->vdev_path, O_RDWR)) == -1) 1226 return (vd); 1227 1228 fsize = lseek(fd, 0, SEEK_END); 1229 (void) ftruncate(fd, *newsize); 1230 1231 if (zopt_verbose >= 6) { 1232 (void) printf("%s grew from %lu to %lu bytes\n", 1233 vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize); 1234 } 1235 (void) close(fd); 1236 return (NULL); 1237 } 1238 1239 /* 1240 * Callback function which expands a given vdev by calling vdev_online(). 1241 */ 1242 /* ARGSUSED */ 1243 vdev_t * 1244 online_vdev(vdev_t *vd, void *arg) 1245 { 1246 spa_t *spa = vd->vdev_spa; 1247 vdev_t *tvd = vd->vdev_top; 1248 vdev_t *pvd = vd->vdev_parent; 1249 uint64_t guid = vd->vdev_guid; 1250 1251 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE); 1252 ASSERT(vd->vdev_ops->vdev_op_leaf); 1253 1254 /* Calling vdev_online will initialize the new metaslabs */ 1255 spa_config_exit(spa, SCL_STATE, spa); 1256 (void) vdev_online(spa, guid, ZFS_ONLINE_EXPAND, NULL); 1257 spa_config_enter(spa, SCL_STATE, spa, RW_READER); 1258 1259 /* 1260 * Since we dropped the lock we need to ensure that we're 1261 * still talking to the original vdev. It's possible this 1262 * vdev may have been detached/replaced while we were 1263 * trying to online it. 1264 */ 1265 if (vd != vdev_lookup_by_guid(tvd, guid) || vd->vdev_parent != pvd) { 1266 if (zopt_verbose >= 6) { 1267 (void) printf("vdev %p has disappeared, was " 1268 "guid %llu\n", (void *)vd, (u_longlong_t)guid); 1269 } 1270 return (vd); 1271 } 1272 return (NULL); 1273 } 1274 1275 /* 1276 * Traverse the vdev tree calling the supplied function. 1277 * We continue to walk the tree until we either have walked all 1278 * children or we receive a non-NULL return from the callback. 1279 * If a NULL callback is passed, then we just return back the first 1280 * leaf vdev we encounter. 1281 */ 1282 vdev_t * 1283 vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg) 1284 { 1285 if (vd->vdev_ops->vdev_op_leaf) { 1286 if (func == NULL) 1287 return (vd); 1288 else 1289 return (func(vd, arg)); 1290 } 1291 1292 for (uint_t c = 0; c < vd->vdev_children; c++) { 1293 vdev_t *cvd = vd->vdev_child[c]; 1294 if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL) 1295 return (cvd); 1296 } 1297 return (NULL); 1298 } 1299 1300 /* 1301 * Verify that dynamic LUN growth works as expected. 1302 */ 1303 void 1304 ztest_vdev_LUN_growth(ztest_args_t *za) 1305 { 1306 spa_t *spa = za->za_spa; 1307 vdev_t *vd, *tvd = NULL; 1308 size_t psize, newsize; 1309 uint64_t spa_newsize, spa_cursize, ms_count; 1310 1311 (void) mutex_lock(&ztest_shared->zs_vdev_lock); 1312 mutex_enter(&spa_namespace_lock); 1313 spa_config_enter(spa, SCL_STATE, spa, RW_READER); 1314 1315 while (tvd == NULL || tvd->vdev_islog) { 1316 uint64_t vdev; 1317 1318 vdev = ztest_random(spa->spa_root_vdev->vdev_children); 1319 tvd = spa->spa_root_vdev->vdev_child[vdev]; 1320 } 1321 1322 /* 1323 * Determine the size of the first leaf vdev associated with 1324 * our top-level device. 1325 */ 1326 vd = vdev_walk_tree(tvd, NULL, NULL); 1327 ASSERT3P(vd, !=, NULL); 1328 ASSERT(vd->vdev_ops->vdev_op_leaf); 1329 1330 psize = vd->vdev_psize; 1331 1332 /* 1333 * We only try to expand the vdev if it's less than 4x its 1334 * original size and it has a valid psize. 1335 */ 1336 if (psize == 0 || psize >= 4 * zopt_vdev_size) { 1337 spa_config_exit(spa, SCL_STATE, spa); 1338 mutex_exit(&spa_namespace_lock); 1339 (void) mutex_unlock(&ztest_shared->zs_vdev_lock); 1340 return; 1341 } 1342 ASSERT(psize > 0); 1343 newsize = psize + psize / 8; 1344 ASSERT3U(newsize, >, psize); 1345 1346 if (zopt_verbose >= 6) { 1347 (void) printf("Expanding vdev %s from %lu to %lu\n", 1348 vd->vdev_path, (ulong_t)psize, (ulong_t)newsize); 1349 } 1350 1351 spa_cursize = spa_get_space(spa); 1352 ms_count = tvd->vdev_ms_count; 1353 1354 /* 1355 * Growing the vdev is a two step process: 1356 * 1). expand the physical size (i.e. relabel) 1357 * 2). online the vdev to create the new metaslabs 1358 */ 1359 if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL || 1360 vdev_walk_tree(tvd, online_vdev, NULL) != NULL || 1361 tvd->vdev_state != VDEV_STATE_HEALTHY) { 1362 if (zopt_verbose >= 5) { 1363 (void) printf("Could not expand LUN because " 1364 "some vdevs were not healthy\n"); 1365 } 1366 (void) spa_config_exit(spa, SCL_STATE, spa); 1367 mutex_exit(&spa_namespace_lock); 1368 (void) mutex_unlock(&ztest_shared->zs_vdev_lock); 1369 return; 1370 } 1371 1372 (void) spa_config_exit(spa, SCL_STATE, spa); 1373 mutex_exit(&spa_namespace_lock); 1374 1375 /* 1376 * Expanding the LUN will update the config asynchronously, 1377 * thus we must wait for the async thread to complete any 1378 * pending tasks before proceeding. 1379 */ 1380 mutex_enter(&spa->spa_async_lock); 1381 while (spa->spa_async_thread != NULL || spa->spa_async_tasks) 1382 cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 1383 mutex_exit(&spa->spa_async_lock); 1384 1385 spa_config_enter(spa, SCL_STATE, spa, RW_READER); 1386 spa_newsize = spa_get_space(spa); 1387 1388 /* 1389 * Make sure we were able to grow the pool. 1390 */ 1391 if (ms_count >= tvd->vdev_ms_count || 1392 spa_cursize >= spa_newsize) { 1393 (void) printf("Top-level vdev metaslab count: " 1394 "before %llu, after %llu\n", 1395 (u_longlong_t)ms_count, 1396 (u_longlong_t)tvd->vdev_ms_count); 1397 fatal(0, "LUN expansion failed: before %llu, " 1398 "after %llu\n", spa_cursize, spa_newsize); 1399 } else if (zopt_verbose >= 5) { 1400 char oldnumbuf[6], newnumbuf[6]; 1401 1402 nicenum(spa_cursize, oldnumbuf); 1403 nicenum(spa_newsize, newnumbuf); 1404 (void) printf("%s grew from %s to %s\n", 1405 spa->spa_name, oldnumbuf, newnumbuf); 1406 } 1407 spa_config_exit(spa, SCL_STATE, spa); 1408 (void) mutex_unlock(&ztest_shared->zs_vdev_lock); 1409 } 1410 1411 /* ARGSUSED */ 1412 static void 1413 ztest_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) 1414 { 1415 /* 1416 * Create the directory object. 1417 */ 1418 VERIFY(dmu_object_claim(os, ZTEST_DIROBJ, 1419 DMU_OT_UINT64_OTHER, ZTEST_DIROBJ_BLOCKSIZE, 1420 DMU_OT_UINT64_OTHER, 5 * sizeof (ztest_block_tag_t), tx) == 0); 1421 1422 VERIFY(zap_create_claim(os, ZTEST_MICROZAP_OBJ, 1423 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0); 1424 1425 VERIFY(zap_create_claim(os, ZTEST_FATZAP_OBJ, 1426 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0); 1427 } 1428 1429 static int 1430 ztest_destroy_cb(char *name, void *arg) 1431 { 1432 ztest_args_t *za = arg; 1433 objset_t *os; 1434 dmu_object_info_t *doi = &za->za_doi; 1435 int error; 1436 1437 /* 1438 * Verify that the dataset contains a directory object. 1439 */ 1440 error = dmu_objset_hold(name, FTAG, &os); 1441 ASSERT3U(error, ==, 0); 1442 error = dmu_object_info(os, ZTEST_DIROBJ, doi); 1443 if (error != ENOENT) { 1444 /* We could have crashed in the middle of destroying it */ 1445 ASSERT3U(error, ==, 0); 1446 ASSERT3U(doi->doi_type, ==, DMU_OT_UINT64_OTHER); 1447 ASSERT3S(doi->doi_physical_blks, >=, 0); 1448 } 1449 dmu_objset_rele(os, FTAG); 1450 1451 /* 1452 * Destroy the dataset. 1453 */ 1454 error = dmu_objset_destroy(name, B_FALSE); 1455 if (error) { 1456 (void) dmu_objset_hold(name, FTAG, &os); 1457 fatal(0, "dmu_objset_destroy(os=%p) = %d\n", os, error); 1458 } 1459 return (0); 1460 } 1461 1462 /* 1463 * Verify that dmu_objset_{create,destroy,open,close} work as expected. 1464 */ 1465 static uint64_t 1466 ztest_log_create(zilog_t *zilog, dmu_tx_t *tx, uint64_t object, int mode) 1467 { 1468 itx_t *itx; 1469 lr_create_t *lr; 1470 size_t namesize; 1471 char name[24]; 1472 1473 (void) sprintf(name, "ZOBJ_%llu", (u_longlong_t)object); 1474 namesize = strlen(name) + 1; 1475 1476 itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize + 1477 ztest_random(ZIL_MAX_BLKSZ)); 1478 lr = (lr_create_t *)&itx->itx_lr; 1479 bzero(lr + 1, lr->lr_common.lrc_reclen - sizeof (*lr)); 1480 lr->lr_doid = object; 1481 lr->lr_foid = 0; 1482 lr->lr_mode = mode; 1483 lr->lr_uid = 0; 1484 lr->lr_gid = 0; 1485 lr->lr_gen = dmu_tx_get_txg(tx); 1486 lr->lr_crtime[0] = time(NULL); 1487 lr->lr_crtime[1] = 0; 1488 lr->lr_rdev = 0; 1489 bcopy(name, (char *)(lr + 1), namesize); 1490 1491 return (zil_itx_assign(zilog, itx, tx)); 1492 } 1493 1494 void 1495 ztest_dmu_objset_create_destroy(ztest_args_t *za) 1496 { 1497 int error; 1498 objset_t *os, *os2; 1499 char name[100]; 1500 zilog_t *zilog; 1501 uint64_t seq; 1502 uint64_t objects; 1503 1504 (void) rw_rdlock(&ztest_shared->zs_name_lock); 1505 (void) snprintf(name, 100, "%s/%s_temp_%llu", za->za_pool, za->za_pool, 1506 (u_longlong_t)za->za_instance); 1507 1508 /* 1509 * If this dataset exists from a previous run, process its replay log 1510 * half of the time. If we don't replay it, then dmu_objset_destroy() 1511 * (invoked from ztest_destroy_cb() below) should just throw it away. 1512 */ 1513 if (ztest_random(2) == 0 && 1514 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) { 1515 zil_replay(os, os, ztest_replay_vector); 1516 dmu_objset_disown(os, FTAG); 1517 } 1518 1519 /* 1520 * There may be an old instance of the dataset we're about to 1521 * create lying around from a previous run. If so, destroy it 1522 * and all of its snapshots. 1523 */ 1524 (void) dmu_objset_find(name, ztest_destroy_cb, za, 1525 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS); 1526 1527 /* 1528 * Verify that the destroyed dataset is no longer in the namespace. 1529 */ 1530 error = dmu_objset_hold(name, FTAG, &os); 1531 if (error != ENOENT) 1532 fatal(1, "dmu_objset_open(%s) found destroyed dataset %p", 1533 name, os); 1534 1535 /* 1536 * Verify that we can create a new dataset. 1537 */ 1538 error = dmu_objset_create(name, DMU_OST_OTHER, 0, 1539 ztest_create_cb, NULL); 1540 if (error) { 1541 if (error == ENOSPC) { 1542 ztest_record_enospc("dmu_objset_create"); 1543 (void) rw_unlock(&ztest_shared->zs_name_lock); 1544 return; 1545 } 1546 fatal(0, "dmu_objset_create(%s) = %d", name, error); 1547 } 1548 1549 error = dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os); 1550 if (error) { 1551 fatal(0, "dmu_objset_open(%s) = %d", name, error); 1552 } 1553 1554 /* 1555 * Open the intent log for it. 1556 */ 1557 zilog = zil_open(os, NULL); 1558 1559 /* 1560 * Put a random number of objects in there. 1561 */ 1562 objects = ztest_random(20); 1563 seq = 0; 1564 while (objects-- != 0) { 1565 uint64_t object; 1566 dmu_tx_t *tx = dmu_tx_create(os); 1567 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, sizeof (name)); 1568 error = dmu_tx_assign(tx, TXG_WAIT); 1569 if (error) { 1570 dmu_tx_abort(tx); 1571 } else { 1572 object = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0, 1573 DMU_OT_NONE, 0, tx); 1574 ztest_set_random_blocksize(os, object, tx); 1575 seq = ztest_log_create(zilog, tx, object, 1576 DMU_OT_UINT64_OTHER); 1577 dmu_write(os, object, 0, sizeof (name), name, tx); 1578 dmu_tx_commit(tx); 1579 } 1580 if (ztest_random(5) == 0) { 1581 zil_commit(zilog, seq, object); 1582 } 1583 if (ztest_random(100) == 0) { 1584 error = zil_suspend(zilog); 1585 if (error == 0) { 1586 zil_resume(zilog); 1587 } 1588 } 1589 } 1590 1591 /* 1592 * Verify that we cannot create an existing dataset. 1593 */ 1594 error = dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL); 1595 if (error != EEXIST) 1596 fatal(0, "created existing dataset, error = %d", error); 1597 1598 /* 1599 * Verify that we can hold an objset that is also owned. 1600 */ 1601 error = dmu_objset_hold(name, FTAG, &os2); 1602 if (error) 1603 fatal(0, "dmu_objset_open('%s') = %d", name, error); 1604 dmu_objset_rele(os2, FTAG); 1605 1606 /* 1607 * Verify that we can not own an objset that is already owned. 1608 */ 1609 error = dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2); 1610 if (error != EBUSY) 1611 fatal(0, "dmu_objset_open('%s') = %d, expected EBUSY", 1612 name, error); 1613 1614 zil_close(zilog); 1615 dmu_objset_disown(os, FTAG); 1616 1617 error = dmu_objset_destroy(name, B_FALSE); 1618 if (error) 1619 fatal(0, "dmu_objset_destroy(%s) = %d", name, error); 1620 1621 (void) rw_unlock(&ztest_shared->zs_name_lock); 1622 } 1623 1624 /* 1625 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected. 1626 */ 1627 void 1628 ztest_dmu_snapshot_create_destroy(ztest_args_t *za) 1629 { 1630 int error; 1631 objset_t *os = za->za_os; 1632 char snapname[100]; 1633 char osname[MAXNAMELEN]; 1634 1635 (void) rw_rdlock(&ztest_shared->zs_name_lock); 1636 dmu_objset_name(os, osname); 1637 (void) snprintf(snapname, 100, "%s@%llu", osname, 1638 (u_longlong_t)za->za_instance); 1639 1640 error = dmu_objset_destroy(snapname, B_FALSE); 1641 if (error != 0 && error != ENOENT) 1642 fatal(0, "dmu_objset_destroy() = %d", error); 1643 error = dmu_objset_snapshot(osname, strchr(snapname, '@')+1, 1644 NULL, FALSE); 1645 if (error == ENOSPC) 1646 ztest_record_enospc("dmu_take_snapshot"); 1647 else if (error != 0 && error != EEXIST) 1648 fatal(0, "dmu_take_snapshot() = %d", error); 1649 (void) rw_unlock(&ztest_shared->zs_name_lock); 1650 } 1651 1652 /* 1653 * Cleanup non-standard snapshots and clones. 1654 */ 1655 void 1656 ztest_dsl_dataset_cleanup(char *osname, uint64_t curval) 1657 { 1658 char snap1name[100]; 1659 char clone1name[100]; 1660 char snap2name[100]; 1661 char clone2name[100]; 1662 char snap3name[100]; 1663 int error; 1664 1665 (void) snprintf(snap1name, 100, "%s@s1_%llu", osname, curval); 1666 (void) snprintf(clone1name, 100, "%s/c1_%llu", osname, curval); 1667 (void) snprintf(snap2name, 100, "%s@s2_%llu", clone1name, curval); 1668 (void) snprintf(clone2name, 100, "%s/c2_%llu", osname, curval); 1669 (void) snprintf(snap3name, 100, "%s@s3_%llu", clone1name, curval); 1670 1671 error = dmu_objset_destroy(clone2name, B_FALSE); 1672 if (error && error != ENOENT) 1673 fatal(0, "dmu_objset_destroy(%s) = %d", clone2name, error); 1674 error = dmu_objset_destroy(snap3name, B_FALSE); 1675 if (error && error != ENOENT) 1676 fatal(0, "dmu_objset_destroy(%s) = %d", snap3name, error); 1677 error = dmu_objset_destroy(snap2name, B_FALSE); 1678 if (error && error != ENOENT) 1679 fatal(0, "dmu_objset_destroy(%s) = %d", snap2name, error); 1680 error = dmu_objset_destroy(clone1name, B_FALSE); 1681 if (error && error != ENOENT) 1682 fatal(0, "dmu_objset_destroy(%s) = %d", clone1name, error); 1683 error = dmu_objset_destroy(snap1name, B_FALSE); 1684 if (error && error != ENOENT) 1685 fatal(0, "dmu_objset_destroy(%s) = %d", snap1name, error); 1686 } 1687 1688 /* 1689 * Verify dsl_dataset_promote handles EBUSY 1690 */ 1691 void 1692 ztest_dsl_dataset_promote_busy(ztest_args_t *za) 1693 { 1694 int error; 1695 objset_t *os = za->za_os; 1696 objset_t *clone; 1697 dsl_dataset_t *ds; 1698 char snap1name[100]; 1699 char clone1name[100]; 1700 char snap2name[100]; 1701 char clone2name[100]; 1702 char snap3name[100]; 1703 char osname[MAXNAMELEN]; 1704 uint64_t curval = za->za_instance; 1705 1706 (void) rw_rdlock(&ztest_shared->zs_name_lock); 1707 1708 dmu_objset_name(os, osname); 1709 ztest_dsl_dataset_cleanup(osname, curval); 1710 1711 (void) snprintf(snap1name, 100, "%s@s1_%llu", osname, curval); 1712 (void) snprintf(clone1name, 100, "%s/c1_%llu", osname, curval); 1713 (void) snprintf(snap2name, 100, "%s@s2_%llu", clone1name, curval); 1714 (void) snprintf(clone2name, 100, "%s/c2_%llu", osname, curval); 1715 (void) snprintf(snap3name, 100, "%s@s3_%llu", clone1name, curval); 1716 1717 error = dmu_objset_snapshot(osname, strchr(snap1name, '@')+1, 1718 NULL, FALSE); 1719 if (error && error != EEXIST) { 1720 if (error == ENOSPC) { 1721 ztest_record_enospc("dmu_take_snapshot"); 1722 goto out; 1723 } 1724 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error); 1725 } 1726 1727 error = dmu_objset_hold(snap1name, FTAG, &clone); 1728 if (error) 1729 fatal(0, "dmu_open_snapshot(%s) = %d", snap1name, error); 1730 1731 error = dmu_objset_clone(clone1name, dmu_objset_ds(clone), 0); 1732 dmu_objset_rele(clone, FTAG); 1733 if (error) { 1734 if (error == ENOSPC) { 1735 ztest_record_enospc("dmu_objset_create"); 1736 goto out; 1737 } 1738 fatal(0, "dmu_objset_create(%s) = %d", clone1name, error); 1739 } 1740 1741 error = dmu_objset_snapshot(clone1name, strchr(snap2name, '@')+1, 1742 NULL, FALSE); 1743 if (error && error != EEXIST) { 1744 if (error == ENOSPC) { 1745 ztest_record_enospc("dmu_take_snapshot"); 1746 goto out; 1747 } 1748 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error); 1749 } 1750 1751 error = dmu_objset_snapshot(clone1name, strchr(snap3name, '@')+1, 1752 NULL, FALSE); 1753 if (error && error != EEXIST) { 1754 if (error == ENOSPC) { 1755 ztest_record_enospc("dmu_take_snapshot"); 1756 goto out; 1757 } 1758 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error); 1759 } 1760 1761 error = dmu_objset_hold(snap3name, FTAG, &clone); 1762 if (error) 1763 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error); 1764 1765 error = dmu_objset_clone(clone2name, dmu_objset_ds(clone), 0); 1766 dmu_objset_rele(clone, FTAG); 1767 if (error) { 1768 if (error == ENOSPC) { 1769 ztest_record_enospc("dmu_objset_create"); 1770 goto out; 1771 } 1772 fatal(0, "dmu_objset_create(%s) = %d", clone2name, error); 1773 } 1774 1775 error = dsl_dataset_own(snap1name, B_FALSE, FTAG, &ds); 1776 if (error) 1777 fatal(0, "dsl_dataset_own(%s) = %d", snap1name, error); 1778 error = dsl_dataset_promote(clone2name, NULL); 1779 if (error != EBUSY) 1780 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name, 1781 error); 1782 dsl_dataset_disown(ds, FTAG); 1783 1784 out: 1785 ztest_dsl_dataset_cleanup(osname, curval); 1786 1787 (void) rw_unlock(&ztest_shared->zs_name_lock); 1788 } 1789 1790 /* 1791 * Verify that dmu_object_{alloc,free} work as expected. 1792 */ 1793 void 1794 ztest_dmu_object_alloc_free(ztest_args_t *za) 1795 { 1796 objset_t *os = za->za_os; 1797 dmu_buf_t *db; 1798 dmu_tx_t *tx; 1799 uint64_t batchobj, object, batchsize, endoff, temp; 1800 int b, c, error, bonuslen; 1801 dmu_object_info_t *doi = &za->za_doi; 1802 char osname[MAXNAMELEN]; 1803 1804 dmu_objset_name(os, osname); 1805 1806 endoff = -8ULL; 1807 batchsize = 2; 1808 1809 /* 1810 * Create a batch object if necessary, and record it in the directory. 1811 */ 1812 VERIFY3U(0, ==, dmu_read(os, ZTEST_DIROBJ, za->za_diroff, 1813 sizeof (uint64_t), &batchobj, DMU_READ_PREFETCH)); 1814 if (batchobj == 0) { 1815 tx = dmu_tx_create(os); 1816 dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, 1817 sizeof (uint64_t)); 1818 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 1819 error = dmu_tx_assign(tx, TXG_WAIT); 1820 if (error) { 1821 ztest_record_enospc("create a batch object"); 1822 dmu_tx_abort(tx); 1823 return; 1824 } 1825 batchobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0, 1826 DMU_OT_NONE, 0, tx); 1827 ztest_set_random_blocksize(os, batchobj, tx); 1828 dmu_write(os, ZTEST_DIROBJ, za->za_diroff, 1829 sizeof (uint64_t), &batchobj, tx); 1830 dmu_tx_commit(tx); 1831 } 1832 1833 /* 1834 * Destroy the previous batch of objects. 1835 */ 1836 for (b = 0; b < batchsize; b++) { 1837 VERIFY3U(0, ==, dmu_read(os, batchobj, b * sizeof (uint64_t), 1838 sizeof (uint64_t), &object, DMU_READ_PREFETCH)); 1839 if (object == 0) 1840 continue; 1841 /* 1842 * Read and validate contents. 1843 * We expect the nth byte of the bonus buffer to be n. 1844 */ 1845 VERIFY(0 == dmu_bonus_hold(os, object, FTAG, &db)); 1846 za->za_dbuf = db; 1847 1848 dmu_object_info_from_db(db, doi); 1849 ASSERT(doi->doi_type == DMU_OT_UINT64_OTHER); 1850 ASSERT(doi->doi_bonus_type == DMU_OT_PLAIN_OTHER); 1851 ASSERT3S(doi->doi_physical_blks, >=, 0); 1852 1853 bonuslen = doi->doi_bonus_size; 1854 1855 for (c = 0; c < bonuslen; c++) { 1856 if (((uint8_t *)db->db_data)[c] != 1857 (uint8_t)(c + bonuslen)) { 1858 fatal(0, 1859 "bad bonus: %s, obj %llu, off %d: %u != %u", 1860 osname, object, c, 1861 ((uint8_t *)db->db_data)[c], 1862 (uint8_t)(c + bonuslen)); 1863 } 1864 } 1865 1866 dmu_buf_rele(db, FTAG); 1867 za->za_dbuf = NULL; 1868 1869 /* 1870 * We expect the word at endoff to be our object number. 1871 */ 1872 VERIFY(0 == dmu_read(os, object, endoff, 1873 sizeof (uint64_t), &temp, DMU_READ_PREFETCH)); 1874 1875 if (temp != object) { 1876 fatal(0, "bad data in %s, got %llu, expected %llu", 1877 osname, temp, object); 1878 } 1879 1880 /* 1881 * Destroy old object and clear batch entry. 1882 */ 1883 tx = dmu_tx_create(os); 1884 dmu_tx_hold_write(tx, batchobj, 1885 b * sizeof (uint64_t), sizeof (uint64_t)); 1886 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END); 1887 error = dmu_tx_assign(tx, TXG_WAIT); 1888 if (error) { 1889 ztest_record_enospc("free object"); 1890 dmu_tx_abort(tx); 1891 return; 1892 } 1893 error = dmu_object_free(os, object, tx); 1894 if (error) { 1895 fatal(0, "dmu_object_free('%s', %llu) = %d", 1896 osname, object, error); 1897 } 1898 object = 0; 1899 1900 dmu_object_set_checksum(os, batchobj, 1901 ztest_random_checksum(), tx); 1902 dmu_object_set_compress(os, batchobj, 1903 ztest_random_compress(), tx); 1904 1905 dmu_write(os, batchobj, b * sizeof (uint64_t), 1906 sizeof (uint64_t), &object, tx); 1907 1908 dmu_tx_commit(tx); 1909 } 1910 1911 /* 1912 * Before creating the new batch of objects, generate a bunch of churn. 1913 */ 1914 for (b = ztest_random(100); b > 0; b--) { 1915 tx = dmu_tx_create(os); 1916 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 1917 error = dmu_tx_assign(tx, TXG_WAIT); 1918 if (error) { 1919 ztest_record_enospc("churn objects"); 1920 dmu_tx_abort(tx); 1921 return; 1922 } 1923 object = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0, 1924 DMU_OT_NONE, 0, tx); 1925 ztest_set_random_blocksize(os, object, tx); 1926 error = dmu_object_free(os, object, tx); 1927 if (error) { 1928 fatal(0, "dmu_object_free('%s', %llu) = %d", 1929 osname, object, error); 1930 } 1931 dmu_tx_commit(tx); 1932 } 1933 1934 /* 1935 * Create a new batch of objects with randomly chosen 1936 * blocksizes and record them in the batch directory. 1937 */ 1938 for (b = 0; b < batchsize; b++) { 1939 uint32_t va_blksize; 1940 u_longlong_t va_nblocks; 1941 1942 tx = dmu_tx_create(os); 1943 dmu_tx_hold_write(tx, batchobj, b * sizeof (uint64_t), 1944 sizeof (uint64_t)); 1945 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 1946 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, endoff, 1947 sizeof (uint64_t)); 1948 error = dmu_tx_assign(tx, TXG_WAIT); 1949 if (error) { 1950 ztest_record_enospc("create batchobj"); 1951 dmu_tx_abort(tx); 1952 return; 1953 } 1954 bonuslen = (int)ztest_random(dmu_bonus_max()) + 1; 1955 1956 object = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0, 1957 DMU_OT_PLAIN_OTHER, bonuslen, tx); 1958 1959 ztest_set_random_blocksize(os, object, tx); 1960 1961 dmu_object_set_checksum(os, object, 1962 ztest_random_checksum(), tx); 1963 dmu_object_set_compress(os, object, 1964 ztest_random_compress(), tx); 1965 1966 dmu_write(os, batchobj, b * sizeof (uint64_t), 1967 sizeof (uint64_t), &object, tx); 1968 1969 /* 1970 * Write to both the bonus buffer and the regular data. 1971 */ 1972 VERIFY(dmu_bonus_hold(os, object, FTAG, &db) == 0); 1973 za->za_dbuf = db; 1974 ASSERT3U(bonuslen, <=, db->db_size); 1975 1976 dmu_object_size_from_db(db, &va_blksize, &va_nblocks); 1977 ASSERT3S(va_nblocks, >=, 0); 1978 1979 dmu_buf_will_dirty(db, tx); 1980 1981 /* 1982 * See comments above regarding the contents of 1983 * the bonus buffer and the word at endoff. 1984 */ 1985 for (c = 0; c < bonuslen; c++) 1986 ((uint8_t *)db->db_data)[c] = (uint8_t)(c + bonuslen); 1987 1988 dmu_buf_rele(db, FTAG); 1989 za->za_dbuf = NULL; 1990 1991 /* 1992 * Write to a large offset to increase indirection. 1993 */ 1994 dmu_write(os, object, endoff, sizeof (uint64_t), &object, tx); 1995 1996 dmu_tx_commit(tx); 1997 } 1998 } 1999 2000 /* 2001 * Verify that dmu_{read,write} work as expected. 2002 */ 2003 typedef struct bufwad { 2004 uint64_t bw_index; 2005 uint64_t bw_txg; 2006 uint64_t bw_data; 2007 } bufwad_t; 2008 2009 typedef struct dmu_read_write_dir { 2010 uint64_t dd_packobj; 2011 uint64_t dd_bigobj; 2012 uint64_t dd_chunk; 2013 } dmu_read_write_dir_t; 2014 2015 void 2016 ztest_dmu_read_write(ztest_args_t *za) 2017 { 2018 objset_t *os = za->za_os; 2019 dmu_read_write_dir_t dd; 2020 dmu_tx_t *tx; 2021 int i, freeit, error; 2022 uint64_t n, s, txg; 2023 bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT; 2024 uint64_t packoff, packsize, bigoff, bigsize; 2025 uint64_t regions = 997; 2026 uint64_t stride = 123456789ULL; 2027 uint64_t width = 40; 2028 int free_percent = 5; 2029 2030 /* 2031 * This test uses two objects, packobj and bigobj, that are always 2032 * updated together (i.e. in the same tx) so that their contents are 2033 * in sync and can be compared. Their contents relate to each other 2034 * in a simple way: packobj is a dense array of 'bufwad' structures, 2035 * while bigobj is a sparse array of the same bufwads. Specifically, 2036 * for any index n, there are three bufwads that should be identical: 2037 * 2038 * packobj, at offset n * sizeof (bufwad_t) 2039 * bigobj, at the head of the nth chunk 2040 * bigobj, at the tail of the nth chunk 2041 * 2042 * The chunk size is arbitrary. It doesn't have to be a power of two, 2043 * and it doesn't have any relation to the object blocksize. 2044 * The only requirement is that it can hold at least two bufwads. 2045 * 2046 * Normally, we write the bufwad to each of these locations. 2047 * However, free_percent of the time we instead write zeroes to 2048 * packobj and perform a dmu_free_range() on bigobj. By comparing 2049 * bigobj to packobj, we can verify that the DMU is correctly 2050 * tracking which parts of an object are allocated and free, 2051 * and that the contents of the allocated blocks are correct. 2052 */ 2053 2054 /* 2055 * Read the directory info. If it's the first time, set things up. 2056 */ 2057 VERIFY(0 == dmu_read(os, ZTEST_DIROBJ, za->za_diroff, 2058 sizeof (dd), &dd, DMU_READ_PREFETCH)); 2059 if (dd.dd_chunk == 0) { 2060 ASSERT(dd.dd_packobj == 0); 2061 ASSERT(dd.dd_bigobj == 0); 2062 tx = dmu_tx_create(os); 2063 dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, sizeof (dd)); 2064 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 2065 error = dmu_tx_assign(tx, TXG_WAIT); 2066 if (error) { 2067 ztest_record_enospc("create r/w directory"); 2068 dmu_tx_abort(tx); 2069 return; 2070 } 2071 2072 dd.dd_packobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0, 2073 DMU_OT_NONE, 0, tx); 2074 dd.dd_bigobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0, 2075 DMU_OT_NONE, 0, tx); 2076 dd.dd_chunk = (1000 + ztest_random(1000)) * sizeof (uint64_t); 2077 2078 ztest_set_random_blocksize(os, dd.dd_packobj, tx); 2079 ztest_set_random_blocksize(os, dd.dd_bigobj, tx); 2080 2081 dmu_write(os, ZTEST_DIROBJ, za->za_diroff, sizeof (dd), &dd, 2082 tx); 2083 dmu_tx_commit(tx); 2084 } 2085 2086 /* 2087 * Prefetch a random chunk of the big object. 2088 * Our aim here is to get some async reads in flight 2089 * for blocks that we may free below; the DMU should 2090 * handle this race correctly. 2091 */ 2092 n = ztest_random(regions) * stride + ztest_random(width); 2093 s = 1 + ztest_random(2 * width - 1); 2094 dmu_prefetch(os, dd.dd_bigobj, n * dd.dd_chunk, s * dd.dd_chunk); 2095 2096 /* 2097 * Pick a random index and compute the offsets into packobj and bigobj. 2098 */ 2099 n = ztest_random(regions) * stride + ztest_random(width); 2100 s = 1 + ztest_random(width - 1); 2101 2102 packoff = n * sizeof (bufwad_t); 2103 packsize = s * sizeof (bufwad_t); 2104 2105 bigoff = n * dd.dd_chunk; 2106 bigsize = s * dd.dd_chunk; 2107 2108 packbuf = umem_alloc(packsize, UMEM_NOFAIL); 2109 bigbuf = umem_alloc(bigsize, UMEM_NOFAIL); 2110 2111 /* 2112 * free_percent of the time, free a range of bigobj rather than 2113 * overwriting it. 2114 */ 2115 freeit = (ztest_random(100) < free_percent); 2116 2117 /* 2118 * Read the current contents of our objects. 2119 */ 2120 error = dmu_read(os, dd.dd_packobj, packoff, packsize, packbuf, 2121 DMU_READ_PREFETCH); 2122 ASSERT3U(error, ==, 0); 2123 error = dmu_read(os, dd.dd_bigobj, bigoff, bigsize, bigbuf, 2124 DMU_READ_PREFETCH); 2125 ASSERT3U(error, ==, 0); 2126 2127 /* 2128 * Get a tx for the mods to both packobj and bigobj. 2129 */ 2130 tx = dmu_tx_create(os); 2131 2132 dmu_tx_hold_write(tx, dd.dd_packobj, packoff, packsize); 2133 2134 if (freeit) 2135 dmu_tx_hold_free(tx, dd.dd_bigobj, bigoff, bigsize); 2136 else 2137 dmu_tx_hold_write(tx, dd.dd_bigobj, bigoff, bigsize); 2138 2139 error = dmu_tx_assign(tx, TXG_WAIT); 2140 2141 if (error) { 2142 ztest_record_enospc("dmu r/w range"); 2143 dmu_tx_abort(tx); 2144 umem_free(packbuf, packsize); 2145 umem_free(bigbuf, bigsize); 2146 return; 2147 } 2148 2149 txg = dmu_tx_get_txg(tx); 2150 2151 /* 2152 * For each index from n to n + s, verify that the existing bufwad 2153 * in packobj matches the bufwads at the head and tail of the 2154 * corresponding chunk in bigobj. Then update all three bufwads 2155 * with the new values we want to write out. 2156 */ 2157 for (i = 0; i < s; i++) { 2158 /* LINTED */ 2159 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t)); 2160 /* LINTED */ 2161 bigH = (bufwad_t *)((char *)bigbuf + i * dd.dd_chunk); 2162 /* LINTED */ 2163 bigT = (bufwad_t *)((char *)bigH + dd.dd_chunk) - 1; 2164 2165 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize); 2166 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize); 2167 2168 if (pack->bw_txg > txg) 2169 fatal(0, "future leak: got %llx, open txg is %llx", 2170 pack->bw_txg, txg); 2171 2172 if (pack->bw_data != 0 && pack->bw_index != n + i) 2173 fatal(0, "wrong index: got %llx, wanted %llx+%llx", 2174 pack->bw_index, n, i); 2175 2176 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0) 2177 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH); 2178 2179 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0) 2180 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT); 2181 2182 if (freeit) { 2183 bzero(pack, sizeof (bufwad_t)); 2184 } else { 2185 pack->bw_index = n + i; 2186 pack->bw_txg = txg; 2187 pack->bw_data = 1 + ztest_random(-2ULL); 2188 } 2189 *bigH = *pack; 2190 *bigT = *pack; 2191 } 2192 2193 /* 2194 * We've verified all the old bufwads, and made new ones. 2195 * Now write them out. 2196 */ 2197 dmu_write(os, dd.dd_packobj, packoff, packsize, packbuf, tx); 2198 2199 if (freeit) { 2200 if (zopt_verbose >= 6) { 2201 (void) printf("freeing offset %llx size %llx" 2202 " txg %llx\n", 2203 (u_longlong_t)bigoff, 2204 (u_longlong_t)bigsize, 2205 (u_longlong_t)txg); 2206 } 2207 VERIFY(0 == dmu_free_range(os, dd.dd_bigobj, bigoff, 2208 bigsize, tx)); 2209 } else { 2210 if (zopt_verbose >= 6) { 2211 (void) printf("writing offset %llx size %llx" 2212 " txg %llx\n", 2213 (u_longlong_t)bigoff, 2214 (u_longlong_t)bigsize, 2215 (u_longlong_t)txg); 2216 } 2217 dmu_write(os, dd.dd_bigobj, bigoff, bigsize, bigbuf, tx); 2218 } 2219 2220 dmu_tx_commit(tx); 2221 2222 /* 2223 * Sanity check the stuff we just wrote. 2224 */ 2225 { 2226 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL); 2227 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL); 2228 2229 VERIFY(0 == dmu_read(os, dd.dd_packobj, packoff, 2230 packsize, packcheck, DMU_READ_PREFETCH)); 2231 VERIFY(0 == dmu_read(os, dd.dd_bigobj, bigoff, 2232 bigsize, bigcheck, DMU_READ_PREFETCH)); 2233 2234 ASSERT(bcmp(packbuf, packcheck, packsize) == 0); 2235 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0); 2236 2237 umem_free(packcheck, packsize); 2238 umem_free(bigcheck, bigsize); 2239 } 2240 2241 umem_free(packbuf, packsize); 2242 umem_free(bigbuf, bigsize); 2243 } 2244 2245 void 2246 compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf, 2247 uint64_t bigsize, uint64_t n, dmu_read_write_dir_t dd, uint64_t txg) 2248 { 2249 uint64_t i; 2250 bufwad_t *pack; 2251 bufwad_t *bigH; 2252 bufwad_t *bigT; 2253 2254 /* 2255 * For each index from n to n + s, verify that the existing bufwad 2256 * in packobj matches the bufwads at the head and tail of the 2257 * corresponding chunk in bigobj. Then update all three bufwads 2258 * with the new values we want to write out. 2259 */ 2260 for (i = 0; i < s; i++) { 2261 /* LINTED */ 2262 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t)); 2263 /* LINTED */ 2264 bigH = (bufwad_t *)((char *)bigbuf + i * dd.dd_chunk); 2265 /* LINTED */ 2266 bigT = (bufwad_t *)((char *)bigH + dd.dd_chunk) - 1; 2267 2268 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize); 2269 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize); 2270 2271 if (pack->bw_txg > txg) 2272 fatal(0, "future leak: got %llx, open txg is %llx", 2273 pack->bw_txg, txg); 2274 2275 if (pack->bw_data != 0 && pack->bw_index != n + i) 2276 fatal(0, "wrong index: got %llx, wanted %llx+%llx", 2277 pack->bw_index, n, i); 2278 2279 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0) 2280 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH); 2281 2282 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0) 2283 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT); 2284 2285 pack->bw_index = n + i; 2286 pack->bw_txg = txg; 2287 pack->bw_data = 1 + ztest_random(-2ULL); 2288 2289 *bigH = *pack; 2290 *bigT = *pack; 2291 } 2292 } 2293 2294 void 2295 ztest_dmu_read_write_zcopy(ztest_args_t *za) 2296 { 2297 objset_t *os = za->za_os; 2298 dmu_read_write_dir_t dd; 2299 dmu_tx_t *tx; 2300 uint64_t i; 2301 int error; 2302 uint64_t n, s, txg; 2303 bufwad_t *packbuf, *bigbuf; 2304 uint64_t packoff, packsize, bigoff, bigsize; 2305 uint64_t regions = 997; 2306 uint64_t stride = 123456789ULL; 2307 uint64_t width = 9; 2308 dmu_buf_t *bonus_db; 2309 arc_buf_t **bigbuf_arcbufs; 2310 dmu_object_info_t *doi = &za->za_doi; 2311 2312 /* 2313 * This test uses two objects, packobj and bigobj, that are always 2314 * updated together (i.e. in the same tx) so that their contents are 2315 * in sync and can be compared. Their contents relate to each other 2316 * in a simple way: packobj is a dense array of 'bufwad' structures, 2317 * while bigobj is a sparse array of the same bufwads. Specifically, 2318 * for any index n, there are three bufwads that should be identical: 2319 * 2320 * packobj, at offset n * sizeof (bufwad_t) 2321 * bigobj, at the head of the nth chunk 2322 * bigobj, at the tail of the nth chunk 2323 * 2324 * The chunk size is set equal to bigobj block size so that 2325 * dmu_assign_arcbuf() can be tested for object updates. 2326 */ 2327 2328 /* 2329 * Read the directory info. If it's the first time, set things up. 2330 */ 2331 VERIFY(0 == dmu_read(os, ZTEST_DIROBJ, za->za_diroff, 2332 sizeof (dd), &dd, DMU_READ_PREFETCH)); 2333 if (dd.dd_chunk == 0) { 2334 ASSERT(dd.dd_packobj == 0); 2335 ASSERT(dd.dd_bigobj == 0); 2336 tx = dmu_tx_create(os); 2337 dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, sizeof (dd)); 2338 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 2339 error = dmu_tx_assign(tx, TXG_WAIT); 2340 if (error) { 2341 ztest_record_enospc("create r/w directory"); 2342 dmu_tx_abort(tx); 2343 return; 2344 } 2345 2346 dd.dd_packobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0, 2347 DMU_OT_NONE, 0, tx); 2348 dd.dd_bigobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0, 2349 DMU_OT_NONE, 0, tx); 2350 ztest_set_random_blocksize(os, dd.dd_packobj, tx); 2351 ztest_set_random_blocksize(os, dd.dd_bigobj, tx); 2352 2353 VERIFY(dmu_object_info(os, dd.dd_bigobj, doi) == 0); 2354 ASSERT(doi->doi_data_block_size >= 2 * sizeof (bufwad_t)); 2355 ASSERT(ISP2(doi->doi_data_block_size)); 2356 dd.dd_chunk = doi->doi_data_block_size; 2357 2358 dmu_write(os, ZTEST_DIROBJ, za->za_diroff, sizeof (dd), &dd, 2359 tx); 2360 dmu_tx_commit(tx); 2361 } else { 2362 VERIFY(dmu_object_info(os, dd.dd_bigobj, doi) == 0); 2363 VERIFY(ISP2(doi->doi_data_block_size)); 2364 VERIFY(dd.dd_chunk == doi->doi_data_block_size); 2365 VERIFY(dd.dd_chunk >= 2 * sizeof (bufwad_t)); 2366 } 2367 2368 /* 2369 * Pick a random index and compute the offsets into packobj and bigobj. 2370 */ 2371 n = ztest_random(regions) * stride + ztest_random(width); 2372 s = 1 + ztest_random(width - 1); 2373 2374 packoff = n * sizeof (bufwad_t); 2375 packsize = s * sizeof (bufwad_t); 2376 2377 bigoff = n * dd.dd_chunk; 2378 bigsize = s * dd.dd_chunk; 2379 2380 packbuf = umem_zalloc(packsize, UMEM_NOFAIL); 2381 bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL); 2382 2383 VERIFY(dmu_bonus_hold(os, dd.dd_bigobj, FTAG, &bonus_db) == 0); 2384 2385 bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL); 2386 2387 /* 2388 * Iteration 0 test zcopy for DB_UNCACHED dbufs. 2389 * Iteration 1 test zcopy to already referenced dbufs. 2390 * Iteration 2 test zcopy to dirty dbuf in the same txg. 2391 * Iteration 3 test zcopy to dbuf dirty in previous txg. 2392 * Iteration 4 test zcopy when dbuf is no longer dirty. 2393 * Iteration 5 test zcopy when it can't be done. 2394 * Iteration 6 one more zcopy write. 2395 */ 2396 for (i = 0; i < 7; i++) { 2397 uint64_t j; 2398 uint64_t off; 2399 2400 /* 2401 * In iteration 5 (i == 5) use arcbufs 2402 * that don't match bigobj blksz to test 2403 * dmu_assign_arcbuf() when it can't directly 2404 * assign an arcbuf to a dbuf. 2405 */ 2406 for (j = 0; j < s; j++) { 2407 if (i != 5) { 2408 bigbuf_arcbufs[j] = 2409 dmu_request_arcbuf(bonus_db, 2410 dd.dd_chunk); 2411 } else { 2412 bigbuf_arcbufs[2 * j] = 2413 dmu_request_arcbuf(bonus_db, 2414 dd.dd_chunk / 2); 2415 bigbuf_arcbufs[2 * j + 1] = 2416 dmu_request_arcbuf(bonus_db, 2417 dd.dd_chunk / 2); 2418 } 2419 } 2420 2421 /* 2422 * Get a tx for the mods to both packobj and bigobj. 2423 */ 2424 tx = dmu_tx_create(os); 2425 2426 dmu_tx_hold_write(tx, dd.dd_packobj, packoff, packsize); 2427 dmu_tx_hold_write(tx, dd.dd_bigobj, bigoff, bigsize); 2428 2429 if (ztest_random(100) == 0) { 2430 error = -1; 2431 } else { 2432 error = dmu_tx_assign(tx, TXG_WAIT); 2433 } 2434 2435 if (error) { 2436 if (error != -1) { 2437 ztest_record_enospc("dmu r/w range"); 2438 } 2439 dmu_tx_abort(tx); 2440 umem_free(packbuf, packsize); 2441 umem_free(bigbuf, bigsize); 2442 for (j = 0; j < s; j++) { 2443 if (i != 5) { 2444 dmu_return_arcbuf(bigbuf_arcbufs[j]); 2445 } else { 2446 dmu_return_arcbuf( 2447 bigbuf_arcbufs[2 * j]); 2448 dmu_return_arcbuf( 2449 bigbuf_arcbufs[2 * j + 1]); 2450 } 2451 } 2452 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *)); 2453 dmu_buf_rele(bonus_db, FTAG); 2454 return; 2455 } 2456 2457 txg = dmu_tx_get_txg(tx); 2458 2459 /* 2460 * 50% of the time don't read objects in the 1st iteration to 2461 * test dmu_assign_arcbuf() for the case when there're no 2462 * existing dbufs for the specified offsets. 2463 */ 2464 if (i != 0 || ztest_random(2) != 0) { 2465 error = dmu_read(os, dd.dd_packobj, packoff, 2466 packsize, packbuf, DMU_READ_PREFETCH); 2467 ASSERT3U(error, ==, 0); 2468 error = dmu_read(os, dd.dd_bigobj, bigoff, bigsize, 2469 bigbuf, DMU_READ_PREFETCH); 2470 ASSERT3U(error, ==, 0); 2471 } 2472 compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize, 2473 n, dd, txg); 2474 2475 /* 2476 * We've verified all the old bufwads, and made new ones. 2477 * Now write them out. 2478 */ 2479 dmu_write(os, dd.dd_packobj, packoff, packsize, packbuf, tx); 2480 if (zopt_verbose >= 6) { 2481 (void) printf("writing offset %llx size %llx" 2482 " txg %llx\n", 2483 (u_longlong_t)bigoff, 2484 (u_longlong_t)bigsize, 2485 (u_longlong_t)txg); 2486 } 2487 for (off = bigoff, j = 0; j < s; j++, off += dd.dd_chunk) { 2488 dmu_buf_t *dbt; 2489 if (i != 5) { 2490 bcopy((caddr_t)bigbuf + (off - bigoff), 2491 bigbuf_arcbufs[j]->b_data, dd.dd_chunk); 2492 } else { 2493 bcopy((caddr_t)bigbuf + (off - bigoff), 2494 bigbuf_arcbufs[2 * j]->b_data, 2495 dd.dd_chunk / 2); 2496 bcopy((caddr_t)bigbuf + (off - bigoff) + 2497 dd.dd_chunk / 2, 2498 bigbuf_arcbufs[2 * j + 1]->b_data, 2499 dd.dd_chunk / 2); 2500 } 2501 2502 if (i == 1) { 2503 VERIFY(dmu_buf_hold(os, dd.dd_bigobj, off, 2504 FTAG, &dbt) == 0); 2505 } 2506 if (i != 5) { 2507 dmu_assign_arcbuf(bonus_db, off, 2508 bigbuf_arcbufs[j], tx); 2509 } else { 2510 dmu_assign_arcbuf(bonus_db, off, 2511 bigbuf_arcbufs[2 * j], tx); 2512 dmu_assign_arcbuf(bonus_db, 2513 off + dd.dd_chunk / 2, 2514 bigbuf_arcbufs[2 * j + 1], tx); 2515 } 2516 if (i == 1) { 2517 dmu_buf_rele(dbt, FTAG); 2518 } 2519 } 2520 dmu_tx_commit(tx); 2521 2522 /* 2523 * Sanity check the stuff we just wrote. 2524 */ 2525 { 2526 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL); 2527 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL); 2528 2529 VERIFY(0 == dmu_read(os, dd.dd_packobj, packoff, 2530 packsize, packcheck, DMU_READ_PREFETCH)); 2531 VERIFY(0 == dmu_read(os, dd.dd_bigobj, bigoff, 2532 bigsize, bigcheck, DMU_READ_PREFETCH)); 2533 2534 ASSERT(bcmp(packbuf, packcheck, packsize) == 0); 2535 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0); 2536 2537 umem_free(packcheck, packsize); 2538 umem_free(bigcheck, bigsize); 2539 } 2540 if (i == 2) { 2541 txg_wait_open(dmu_objset_pool(os), 0); 2542 } else if (i == 3) { 2543 txg_wait_synced(dmu_objset_pool(os), 0); 2544 } 2545 } 2546 2547 dmu_buf_rele(bonus_db, FTAG); 2548 umem_free(packbuf, packsize); 2549 umem_free(bigbuf, bigsize); 2550 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *)); 2551 } 2552 2553 void 2554 ztest_dmu_check_future_leak(ztest_args_t *za) 2555 { 2556 objset_t *os = za->za_os; 2557 dmu_buf_t *db; 2558 ztest_block_tag_t *bt; 2559 dmu_object_info_t *doi = &za->za_doi; 2560 2561 /* 2562 * Make sure that, if there is a write record in the bonus buffer 2563 * of the ZTEST_DIROBJ, that the txg for this record is <= the 2564 * last synced txg of the pool. 2565 */ 2566 VERIFY(dmu_bonus_hold(os, ZTEST_DIROBJ, FTAG, &db) == 0); 2567 za->za_dbuf = db; 2568 VERIFY(dmu_object_info(os, ZTEST_DIROBJ, doi) == 0); 2569 ASSERT3U(doi->doi_bonus_size, >=, sizeof (*bt)); 2570 ASSERT3U(doi->doi_bonus_size, <=, db->db_size); 2571 ASSERT3U(doi->doi_bonus_size % sizeof (*bt), ==, 0); 2572 bt = (void *)((char *)db->db_data + doi->doi_bonus_size - sizeof (*bt)); 2573 if (bt->bt_objset != 0) { 2574 ASSERT3U(bt->bt_objset, ==, dmu_objset_id(os)); 2575 ASSERT3U(bt->bt_object, ==, ZTEST_DIROBJ); 2576 ASSERT3U(bt->bt_offset, ==, -1ULL); 2577 ASSERT3U(bt->bt_txg, <, spa_first_txg(za->za_spa)); 2578 } 2579 dmu_buf_rele(db, FTAG); 2580 za->za_dbuf = NULL; 2581 } 2582 2583 void 2584 ztest_dmu_write_parallel(ztest_args_t *za) 2585 { 2586 objset_t *os = za->za_os; 2587 ztest_block_tag_t *rbt = &za->za_rbt; 2588 ztest_block_tag_t *wbt = &za->za_wbt; 2589 const size_t btsize = sizeof (ztest_block_tag_t); 2590 dmu_buf_t *db; 2591 int b, error; 2592 int bs = ZTEST_DIROBJ_BLOCKSIZE; 2593 int do_free = 0; 2594 uint64_t off, txg, txg_how; 2595 mutex_t *lp; 2596 char osname[MAXNAMELEN]; 2597 char iobuf[SPA_MAXBLOCKSIZE]; 2598 blkptr_t blk = { 0 }; 2599 uint64_t blkoff; 2600 zbookmark_t zb; 2601 dmu_tx_t *tx = dmu_tx_create(os); 2602 dmu_buf_t *bonus_db; 2603 arc_buf_t *abuf = NULL; 2604 2605 dmu_objset_name(os, osname); 2606 2607 /* 2608 * Have multiple threads write to large offsets in ZTEST_DIROBJ 2609 * to verify that having multiple threads writing to the same object 2610 * in parallel doesn't cause any trouble. 2611 */ 2612 if (ztest_random(4) == 0) { 2613 /* 2614 * Do the bonus buffer instead of a regular block. 2615 * We need a lock to serialize resize vs. others, 2616 * so we hash on the objset ID. 2617 */ 2618 b = dmu_objset_id(os) % ZTEST_SYNC_LOCKS; 2619 off = -1ULL; 2620 dmu_tx_hold_bonus(tx, ZTEST_DIROBJ); 2621 } else { 2622 b = ztest_random(ZTEST_SYNC_LOCKS); 2623 off = za->za_diroff_shared + (b << SPA_MAXBLOCKSHIFT); 2624 if (ztest_random(4) == 0) { 2625 do_free = 1; 2626 dmu_tx_hold_free(tx, ZTEST_DIROBJ, off, bs); 2627 } else { 2628 dmu_tx_hold_write(tx, ZTEST_DIROBJ, off, bs); 2629 } 2630 } 2631 2632 if (off != -1ULL && P2PHASE(off, bs) == 0 && !do_free && 2633 ztest_random(8) == 0) { 2634 VERIFY(dmu_bonus_hold(os, ZTEST_DIROBJ, FTAG, &bonus_db) == 0); 2635 abuf = dmu_request_arcbuf(bonus_db, bs); 2636 } 2637 2638 txg_how = ztest_random(2) == 0 ? TXG_WAIT : TXG_NOWAIT; 2639 error = dmu_tx_assign(tx, txg_how); 2640 if (error) { 2641 if (error == ERESTART) { 2642 ASSERT(txg_how == TXG_NOWAIT); 2643 dmu_tx_wait(tx); 2644 } else { 2645 ztest_record_enospc("dmu write parallel"); 2646 } 2647 dmu_tx_abort(tx); 2648 if (abuf != NULL) { 2649 dmu_return_arcbuf(abuf); 2650 dmu_buf_rele(bonus_db, FTAG); 2651 } 2652 return; 2653 } 2654 txg = dmu_tx_get_txg(tx); 2655 2656 lp = &ztest_shared->zs_sync_lock[b]; 2657 (void) mutex_lock(lp); 2658 2659 wbt->bt_objset = dmu_objset_id(os); 2660 wbt->bt_object = ZTEST_DIROBJ; 2661 wbt->bt_offset = off; 2662 wbt->bt_txg = txg; 2663 wbt->bt_thread = za->za_instance; 2664 wbt->bt_seq = ztest_shared->zs_seq[b]++; /* protected by lp */ 2665 2666 /* 2667 * Occasionally, write an all-zero block to test the behavior 2668 * of blocks that compress into holes. 2669 */ 2670 if (off != -1ULL && ztest_random(8) == 0) 2671 bzero(wbt, btsize); 2672 2673 if (off == -1ULL) { 2674 dmu_object_info_t *doi = &za->za_doi; 2675 char *dboff; 2676 2677 VERIFY(dmu_bonus_hold(os, ZTEST_DIROBJ, FTAG, &db) == 0); 2678 za->za_dbuf = db; 2679 dmu_object_info_from_db(db, doi); 2680 ASSERT3U(doi->doi_bonus_size, <=, db->db_size); 2681 ASSERT3U(doi->doi_bonus_size, >=, btsize); 2682 ASSERT3U(doi->doi_bonus_size % btsize, ==, 0); 2683 dboff = (char *)db->db_data + doi->doi_bonus_size - btsize; 2684 bcopy(dboff, rbt, btsize); 2685 if (rbt->bt_objset != 0) { 2686 ASSERT3U(rbt->bt_objset, ==, wbt->bt_objset); 2687 ASSERT3U(rbt->bt_object, ==, wbt->bt_object); 2688 ASSERT3U(rbt->bt_offset, ==, wbt->bt_offset); 2689 ASSERT3U(rbt->bt_txg, <=, wbt->bt_txg); 2690 } 2691 if (ztest_random(10) == 0) { 2692 int newsize = (ztest_random(db->db_size / 2693 btsize) + 1) * btsize; 2694 2695 ASSERT3U(newsize, >=, btsize); 2696 ASSERT3U(newsize, <=, db->db_size); 2697 VERIFY3U(dmu_set_bonus(db, newsize, tx), ==, 0); 2698 dboff = (char *)db->db_data + newsize - btsize; 2699 } 2700 dmu_buf_will_dirty(db, tx); 2701 bcopy(wbt, dboff, btsize); 2702 dmu_buf_rele(db, FTAG); 2703 za->za_dbuf = NULL; 2704 } else if (do_free) { 2705 VERIFY(dmu_free_range(os, ZTEST_DIROBJ, off, bs, tx) == 0); 2706 } else if (abuf == NULL) { 2707 dmu_write(os, ZTEST_DIROBJ, off, btsize, wbt, tx); 2708 } else { 2709 bcopy(wbt, abuf->b_data, btsize); 2710 dmu_assign_arcbuf(bonus_db, off, abuf, tx); 2711 dmu_buf_rele(bonus_db, FTAG); 2712 } 2713 2714 (void) mutex_unlock(lp); 2715 2716 if (ztest_random(1000) == 0) 2717 (void) poll(NULL, 0, 1); /* open dn_notxholds window */ 2718 2719 dmu_tx_commit(tx); 2720 2721 if (ztest_random(10000) == 0) 2722 txg_wait_synced(dmu_objset_pool(os), txg); 2723 2724 if (off == -1ULL || do_free) 2725 return; 2726 2727 if (ztest_random(2) != 0) 2728 return; 2729 2730 /* 2731 * dmu_sync() the block we just wrote. 2732 */ 2733 (void) mutex_lock(lp); 2734 2735 blkoff = P2ALIGN_TYPED(off, bs, uint64_t); 2736 error = dmu_buf_hold(os, ZTEST_DIROBJ, blkoff, FTAG, &db); 2737 za->za_dbuf = db; 2738 if (error) { 2739 (void) mutex_unlock(lp); 2740 return; 2741 } 2742 blkoff = off - blkoff; 2743 error = dmu_sync(NULL, db, &blk, txg, NULL, NULL); 2744 dmu_buf_rele(db, FTAG); 2745 za->za_dbuf = NULL; 2746 2747 if (error) { 2748 (void) mutex_unlock(lp); 2749 return; 2750 } 2751 2752 if (blk.blk_birth == 0) { /* concurrent free */ 2753 (void) mutex_unlock(lp); 2754 return; 2755 } 2756 2757 txg_suspend(dmu_objset_pool(os)); 2758 2759 (void) mutex_unlock(lp); 2760 2761 ASSERT(blk.blk_fill == 1); 2762 ASSERT3U(BP_GET_TYPE(&blk), ==, DMU_OT_UINT64_OTHER); 2763 ASSERT3U(BP_GET_LEVEL(&blk), ==, 0); 2764 ASSERT3U(BP_GET_LSIZE(&blk), ==, bs); 2765 2766 /* 2767 * Read the block that dmu_sync() returned to make sure its contents 2768 * match what we wrote. We do this while still txg_suspend()ed 2769 * to ensure that the block can't be reused before we read it. 2770 */ 2771 zb.zb_objset = dmu_objset_id(os); 2772 zb.zb_object = ZTEST_DIROBJ; 2773 zb.zb_level = 0; 2774 zb.zb_blkid = off / bs; 2775 error = zio_wait(zio_read(NULL, za->za_spa, &blk, iobuf, bs, 2776 NULL, NULL, ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_MUSTSUCCEED, &zb)); 2777 ASSERT3U(error, ==, 0); 2778 2779 txg_resume(dmu_objset_pool(os)); 2780 2781 bcopy(&iobuf[blkoff], rbt, btsize); 2782 2783 if (rbt->bt_objset == 0) /* concurrent free */ 2784 return; 2785 2786 if (wbt->bt_objset == 0) /* all-zero overwrite */ 2787 return; 2788 2789 ASSERT3U(rbt->bt_objset, ==, wbt->bt_objset); 2790 ASSERT3U(rbt->bt_object, ==, wbt->bt_object); 2791 ASSERT3U(rbt->bt_offset, ==, wbt->bt_offset); 2792 2793 /* 2794 * The semantic of dmu_sync() is that we always push the most recent 2795 * version of the data, so in the face of concurrent updates we may 2796 * see a newer version of the block. That's OK. 2797 */ 2798 ASSERT3U(rbt->bt_txg, >=, wbt->bt_txg); 2799 if (rbt->bt_thread == wbt->bt_thread) 2800 ASSERT3U(rbt->bt_seq, ==, wbt->bt_seq); 2801 else 2802 ASSERT3U(rbt->bt_seq, >, wbt->bt_seq); 2803 } 2804 2805 /* 2806 * Verify that zap_{create,destroy,add,remove,update} work as expected. 2807 */ 2808 #define ZTEST_ZAP_MIN_INTS 1 2809 #define ZTEST_ZAP_MAX_INTS 4 2810 #define ZTEST_ZAP_MAX_PROPS 1000 2811 2812 void 2813 ztest_zap(ztest_args_t *za) 2814 { 2815 objset_t *os = za->za_os; 2816 uint64_t object; 2817 uint64_t txg, last_txg; 2818 uint64_t value[ZTEST_ZAP_MAX_INTS]; 2819 uint64_t zl_ints, zl_intsize, prop; 2820 int i, ints; 2821 dmu_tx_t *tx; 2822 char propname[100], txgname[100]; 2823 int error; 2824 char osname[MAXNAMELEN]; 2825 char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" }; 2826 2827 dmu_objset_name(os, osname); 2828 2829 /* 2830 * Create a new object if necessary, and record it in the directory. 2831 */ 2832 VERIFY(0 == dmu_read(os, ZTEST_DIROBJ, za->za_diroff, 2833 sizeof (uint64_t), &object, DMU_READ_PREFETCH)); 2834 2835 if (object == 0) { 2836 tx = dmu_tx_create(os); 2837 dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, 2838 sizeof (uint64_t)); 2839 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, NULL); 2840 error = dmu_tx_assign(tx, TXG_WAIT); 2841 if (error) { 2842 ztest_record_enospc("create zap test obj"); 2843 dmu_tx_abort(tx); 2844 return; 2845 } 2846 object = zap_create(os, DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx); 2847 if (error) { 2848 fatal(0, "zap_create('%s', %llu) = %d", 2849 osname, object, error); 2850 } 2851 ASSERT(object != 0); 2852 dmu_write(os, ZTEST_DIROBJ, za->za_diroff, 2853 sizeof (uint64_t), &object, tx); 2854 /* 2855 * Generate a known hash collision, and verify that 2856 * we can lookup and remove both entries. 2857 */ 2858 for (i = 0; i < 2; i++) { 2859 value[i] = i; 2860 error = zap_add(os, object, hc[i], sizeof (uint64_t), 2861 1, &value[i], tx); 2862 ASSERT3U(error, ==, 0); 2863 } 2864 for (i = 0; i < 2; i++) { 2865 error = zap_add(os, object, hc[i], sizeof (uint64_t), 2866 1, &value[i], tx); 2867 ASSERT3U(error, ==, EEXIST); 2868 error = zap_length(os, object, hc[i], 2869 &zl_intsize, &zl_ints); 2870 ASSERT3U(error, ==, 0); 2871 ASSERT3U(zl_intsize, ==, sizeof (uint64_t)); 2872 ASSERT3U(zl_ints, ==, 1); 2873 } 2874 for (i = 0; i < 2; i++) { 2875 error = zap_remove(os, object, hc[i], tx); 2876 ASSERT3U(error, ==, 0); 2877 } 2878 2879 dmu_tx_commit(tx); 2880 } 2881 2882 ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS); 2883 2884 prop = ztest_random(ZTEST_ZAP_MAX_PROPS); 2885 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop); 2886 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop); 2887 bzero(value, sizeof (value)); 2888 last_txg = 0; 2889 2890 /* 2891 * If these zap entries already exist, validate their contents. 2892 */ 2893 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints); 2894 if (error == 0) { 2895 ASSERT3U(zl_intsize, ==, sizeof (uint64_t)); 2896 ASSERT3U(zl_ints, ==, 1); 2897 2898 VERIFY(zap_lookup(os, object, txgname, zl_intsize, 2899 zl_ints, &last_txg) == 0); 2900 2901 VERIFY(zap_length(os, object, propname, &zl_intsize, 2902 &zl_ints) == 0); 2903 2904 ASSERT3U(zl_intsize, ==, sizeof (uint64_t)); 2905 ASSERT3U(zl_ints, ==, ints); 2906 2907 VERIFY(zap_lookup(os, object, propname, zl_intsize, 2908 zl_ints, value) == 0); 2909 2910 for (i = 0; i < ints; i++) { 2911 ASSERT3U(value[i], ==, last_txg + object + i); 2912 } 2913 } else { 2914 ASSERT3U(error, ==, ENOENT); 2915 } 2916 2917 /* 2918 * Atomically update two entries in our zap object. 2919 * The first is named txg_%llu, and contains the txg 2920 * in which the property was last updated. The second 2921 * is named prop_%llu, and the nth element of its value 2922 * should be txg + object + n. 2923 */ 2924 tx = dmu_tx_create(os); 2925 dmu_tx_hold_zap(tx, object, TRUE, NULL); 2926 error = dmu_tx_assign(tx, TXG_WAIT); 2927 if (error) { 2928 ztest_record_enospc("create zap entry"); 2929 dmu_tx_abort(tx); 2930 return; 2931 } 2932 txg = dmu_tx_get_txg(tx); 2933 2934 if (last_txg > txg) 2935 fatal(0, "zap future leak: old %llu new %llu", last_txg, txg); 2936 2937 for (i = 0; i < ints; i++) 2938 value[i] = txg + object + i; 2939 2940 error = zap_update(os, object, txgname, sizeof (uint64_t), 1, &txg, tx); 2941 if (error) 2942 fatal(0, "zap_update('%s', %llu, '%s') = %d", 2943 osname, object, txgname, error); 2944 2945 error = zap_update(os, object, propname, sizeof (uint64_t), 2946 ints, value, tx); 2947 if (error) 2948 fatal(0, "zap_update('%s', %llu, '%s') = %d", 2949 osname, object, propname, error); 2950 2951 dmu_tx_commit(tx); 2952 2953 /* 2954 * Remove a random pair of entries. 2955 */ 2956 prop = ztest_random(ZTEST_ZAP_MAX_PROPS); 2957 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop); 2958 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop); 2959 2960 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints); 2961 2962 if (error == ENOENT) 2963 return; 2964 2965 ASSERT3U(error, ==, 0); 2966 2967 tx = dmu_tx_create(os); 2968 dmu_tx_hold_zap(tx, object, TRUE, NULL); 2969 error = dmu_tx_assign(tx, TXG_WAIT); 2970 if (error) { 2971 ztest_record_enospc("remove zap entry"); 2972 dmu_tx_abort(tx); 2973 return; 2974 } 2975 error = zap_remove(os, object, txgname, tx); 2976 if (error) 2977 fatal(0, "zap_remove('%s', %llu, '%s') = %d", 2978 osname, object, txgname, error); 2979 2980 error = zap_remove(os, object, propname, tx); 2981 if (error) 2982 fatal(0, "zap_remove('%s', %llu, '%s') = %d", 2983 osname, object, propname, error); 2984 2985 dmu_tx_commit(tx); 2986 2987 /* 2988 * Once in a while, destroy the object. 2989 */ 2990 if (ztest_random(1000) != 0) 2991 return; 2992 2993 tx = dmu_tx_create(os); 2994 dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, sizeof (uint64_t)); 2995 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END); 2996 error = dmu_tx_assign(tx, TXG_WAIT); 2997 if (error) { 2998 ztest_record_enospc("destroy zap object"); 2999 dmu_tx_abort(tx); 3000 return; 3001 } 3002 error = zap_destroy(os, object, tx); 3003 if (error) 3004 fatal(0, "zap_destroy('%s', %llu) = %d", 3005 osname, object, error); 3006 object = 0; 3007 dmu_write(os, ZTEST_DIROBJ, za->za_diroff, sizeof (uint64_t), 3008 &object, tx); 3009 dmu_tx_commit(tx); 3010 } 3011 3012 /* 3013 * Testcase to test the upgrading of a microzap to fatzap. 3014 */ 3015 void 3016 ztest_fzap(ztest_args_t *za) 3017 { 3018 objset_t *os = za->za_os; 3019 uint64_t object; 3020 uint64_t value; 3021 dmu_tx_t *tx; 3022 int i, error; 3023 char osname[MAXNAMELEN]; 3024 char *name = "aaa"; 3025 char entname[MAXNAMELEN]; 3026 3027 dmu_objset_name(os, osname); 3028 3029 /* 3030 * Create a new object if necessary, and record it in the directory. 3031 */ 3032 VERIFY(0 == dmu_read(os, ZTEST_DIROBJ, za->za_diroff, 3033 sizeof (uint64_t), &object, DMU_READ_PREFETCH)); 3034 3035 if (object == 0) { 3036 tx = dmu_tx_create(os); 3037 dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, 3038 sizeof (uint64_t)); 3039 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, NULL); 3040 error = dmu_tx_assign(tx, TXG_WAIT); 3041 if (error) { 3042 ztest_record_enospc("create zap test obj"); 3043 dmu_tx_abort(tx); 3044 return; 3045 } 3046 object = zap_create(os, DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx); 3047 if (error) { 3048 fatal(0, "zap_create('%s', %llu) = %d", 3049 osname, object, error); 3050 } 3051 ASSERT(object != 0); 3052 dmu_write(os, ZTEST_DIROBJ, za->za_diroff, 3053 sizeof (uint64_t), &object, tx); 3054 dmu_tx_commit(tx); 3055 } 3056 3057 /* 3058 * Add entries to this ZAP amd make sure it spills over 3059 * and gets upgraded to a fatzap. Also, since we are adding 3060 * 2050 entries we should see ptrtbl growth and leaf-block 3061 * split. 3062 */ 3063 for (i = 0; i < 2050; i++) { 3064 (void) snprintf(entname, sizeof (entname), "%s-%d", name, i); 3065 value = i; 3066 3067 tx = dmu_tx_create(os); 3068 dmu_tx_hold_zap(tx, object, TRUE, entname); 3069 error = dmu_tx_assign(tx, TXG_WAIT); 3070 3071 if (error) { 3072 ztest_record_enospc("create zap entry"); 3073 dmu_tx_abort(tx); 3074 return; 3075 } 3076 error = zap_add(os, object, entname, sizeof (uint64_t), 3077 1, &value, tx); 3078 3079 ASSERT(error == 0 || error == EEXIST); 3080 dmu_tx_commit(tx); 3081 } 3082 3083 /* 3084 * Once in a while, destroy the object. 3085 */ 3086 if (ztest_random(1000) != 0) 3087 return; 3088 3089 tx = dmu_tx_create(os); 3090 dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, sizeof (uint64_t)); 3091 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END); 3092 error = dmu_tx_assign(tx, TXG_WAIT); 3093 if (error) { 3094 ztest_record_enospc("destroy zap object"); 3095 dmu_tx_abort(tx); 3096 return; 3097 } 3098 error = zap_destroy(os, object, tx); 3099 if (error) 3100 fatal(0, "zap_destroy('%s', %llu) = %d", 3101 osname, object, error); 3102 object = 0; 3103 dmu_write(os, ZTEST_DIROBJ, za->za_diroff, sizeof (uint64_t), 3104 &object, tx); 3105 dmu_tx_commit(tx); 3106 } 3107 3108 void 3109 ztest_zap_parallel(ztest_args_t *za) 3110 { 3111 objset_t *os = za->za_os; 3112 uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc; 3113 dmu_tx_t *tx; 3114 int i, namelen, error; 3115 char name[20], string_value[20]; 3116 void *data; 3117 3118 /* 3119 * Generate a random name of the form 'xxx.....' where each 3120 * x is a random printable character and the dots are dots. 3121 * There are 94 such characters, and the name length goes from 3122 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names. 3123 */ 3124 namelen = ztest_random(sizeof (name) - 5) + 5 + 1; 3125 3126 for (i = 0; i < 3; i++) 3127 name[i] = '!' + ztest_random('~' - '!' + 1); 3128 for (; i < namelen - 1; i++) 3129 name[i] = '.'; 3130 name[i] = '\0'; 3131 3132 if (ztest_random(2) == 0) 3133 object = ZTEST_MICROZAP_OBJ; 3134 else 3135 object = ZTEST_FATZAP_OBJ; 3136 3137 if ((namelen & 1) || object == ZTEST_MICROZAP_OBJ) { 3138 wsize = sizeof (txg); 3139 wc = 1; 3140 data = &txg; 3141 } else { 3142 wsize = 1; 3143 wc = namelen; 3144 data = string_value; 3145 } 3146 3147 count = -1ULL; 3148 VERIFY(zap_count(os, object, &count) == 0); 3149 ASSERT(count != -1ULL); 3150 3151 /* 3152 * Select an operation: length, lookup, add, update, remove. 3153 */ 3154 i = ztest_random(5); 3155 3156 if (i >= 2) { 3157 tx = dmu_tx_create(os); 3158 dmu_tx_hold_zap(tx, object, TRUE, NULL); 3159 error = dmu_tx_assign(tx, TXG_WAIT); 3160 if (error) { 3161 ztest_record_enospc("zap parallel"); 3162 dmu_tx_abort(tx); 3163 return; 3164 } 3165 txg = dmu_tx_get_txg(tx); 3166 bcopy(name, string_value, namelen); 3167 } else { 3168 tx = NULL; 3169 txg = 0; 3170 bzero(string_value, namelen); 3171 } 3172 3173 switch (i) { 3174 3175 case 0: 3176 error = zap_length(os, object, name, &zl_wsize, &zl_wc); 3177 if (error == 0) { 3178 ASSERT3U(wsize, ==, zl_wsize); 3179 ASSERT3U(wc, ==, zl_wc); 3180 } else { 3181 ASSERT3U(error, ==, ENOENT); 3182 } 3183 break; 3184 3185 case 1: 3186 error = zap_lookup(os, object, name, wsize, wc, data); 3187 if (error == 0) { 3188 if (data == string_value && 3189 bcmp(name, data, namelen) != 0) 3190 fatal(0, "name '%s' != val '%s' len %d", 3191 name, data, namelen); 3192 } else { 3193 ASSERT3U(error, ==, ENOENT); 3194 } 3195 break; 3196 3197 case 2: 3198 error = zap_add(os, object, name, wsize, wc, data, tx); 3199 ASSERT(error == 0 || error == EEXIST); 3200 break; 3201 3202 case 3: 3203 VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0); 3204 break; 3205 3206 case 4: 3207 error = zap_remove(os, object, name, tx); 3208 ASSERT(error == 0 || error == ENOENT); 3209 break; 3210 } 3211 3212 if (tx != NULL) 3213 dmu_tx_commit(tx); 3214 } 3215 3216 /* 3217 * Commit callback data. 3218 */ 3219 typedef struct ztest_cb_data { 3220 list_node_t zcd_node; 3221 uint64_t zcd_txg; 3222 int zcd_expected_err; 3223 boolean_t zcd_added; 3224 boolean_t zcd_called; 3225 spa_t *zcd_spa; 3226 } ztest_cb_data_t; 3227 3228 /* This is the actual commit callback function */ 3229 static void 3230 ztest_commit_callback(void *arg, int error) 3231 { 3232 ztest_cb_data_t *data = arg; 3233 uint64_t synced_txg; 3234 3235 VERIFY(data != NULL); 3236 VERIFY3S(data->zcd_expected_err, ==, error); 3237 VERIFY(!data->zcd_called); 3238 3239 synced_txg = spa_last_synced_txg(data->zcd_spa); 3240 if (data->zcd_txg > synced_txg) 3241 fatal(0, "commit callback of txg %" PRIu64 " called prematurely" 3242 ", last synced txg = %" PRIu64 "\n", data->zcd_txg, 3243 synced_txg); 3244 3245 data->zcd_called = B_TRUE; 3246 3247 if (error == ECANCELED) { 3248 ASSERT3U(data->zcd_txg, ==, 0); 3249 ASSERT(!data->zcd_added); 3250 3251 /* 3252 * The private callback data should be destroyed here, but 3253 * since we are going to check the zcd_called field after 3254 * dmu_tx_abort(), we will destroy it there. 3255 */ 3256 return; 3257 } 3258 3259 /* Was this callback added to the global callback list? */ 3260 if (!data->zcd_added) 3261 goto out; 3262 3263 ASSERT3U(data->zcd_txg, !=, 0); 3264 3265 /* Remove our callback from the list */ 3266 (void) mutex_lock(&zcl.zcl_callbacks_lock); 3267 list_remove(&zcl.zcl_callbacks, data); 3268 (void) mutex_unlock(&zcl.zcl_callbacks_lock); 3269 3270 out: 3271 umem_free(data, sizeof (ztest_cb_data_t)); 3272 } 3273 3274 /* Allocate and initialize callback data structure */ 3275 static ztest_cb_data_t * 3276 ztest_create_cb_data(objset_t *os, uint64_t txg) 3277 { 3278 ztest_cb_data_t *cb_data; 3279 3280 cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL); 3281 3282 cb_data->zcd_txg = txg; 3283 cb_data->zcd_spa = dmu_objset_spa(os); 3284 3285 return (cb_data); 3286 } 3287 3288 /* 3289 * If a number of txgs equal to this threshold have been created after a commit 3290 * callback has been registered but not called, then we assume there is an 3291 * implementation bug. 3292 */ 3293 #define ZTEST_COMMIT_CALLBACK_THRESH (TXG_CONCURRENT_STATES + 2) 3294 3295 /* 3296 * Commit callback test. 3297 */ 3298 void 3299 ztest_dmu_commit_callbacks(ztest_args_t *za) 3300 { 3301 objset_t *os = za->za_os; 3302 dmu_tx_t *tx; 3303 ztest_cb_data_t *cb_data[3], *tmp_cb; 3304 uint64_t old_txg, txg; 3305 int i, error; 3306 3307 tx = dmu_tx_create(os); 3308 3309 cb_data[0] = ztest_create_cb_data(os, 0); 3310 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]); 3311 3312 dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, sizeof (uint64_t)); 3313 3314 /* Every once in a while, abort the transaction on purpose */ 3315 if (ztest_random(100) == 0) 3316 error = -1; 3317 3318 if (!error) 3319 error = dmu_tx_assign(tx, TXG_NOWAIT); 3320 3321 txg = error ? 0 : dmu_tx_get_txg(tx); 3322 3323 cb_data[0]->zcd_txg = txg; 3324 cb_data[1] = ztest_create_cb_data(os, txg); 3325 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]); 3326 3327 if (error) { 3328 /* 3329 * It's not a strict requirement to call the registered 3330 * callbacks from inside dmu_tx_abort(), but that's what 3331 * it's supposed to happen in the current implementation 3332 * so we will check for that. 3333 */ 3334 for (i = 0; i < 2; i++) { 3335 cb_data[i]->zcd_expected_err = ECANCELED; 3336 VERIFY(!cb_data[i]->zcd_called); 3337 } 3338 3339 dmu_tx_abort(tx); 3340 3341 for (i = 0; i < 2; i++) { 3342 VERIFY(cb_data[i]->zcd_called); 3343 umem_free(cb_data[i], sizeof (ztest_cb_data_t)); 3344 } 3345 3346 return; 3347 } 3348 3349 cb_data[2] = ztest_create_cb_data(os, txg); 3350 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]); 3351 3352 /* 3353 * Read existing data to make sure there isn't a future leak. 3354 */ 3355 VERIFY(0 == dmu_read(os, ZTEST_DIROBJ, za->za_diroff, sizeof (uint64_t), 3356 &old_txg, DMU_READ_PREFETCH)); 3357 3358 if (old_txg > txg) 3359 fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64, 3360 old_txg, txg); 3361 3362 dmu_write(os, ZTEST_DIROBJ, za->za_diroff, sizeof (uint64_t), &txg, tx); 3363 3364 (void) mutex_lock(&zcl.zcl_callbacks_lock); 3365 3366 /* 3367 * Since commit callbacks don't have any ordering requirement and since 3368 * it is theoretically possible for a commit callback to be called 3369 * after an arbitrary amount of time has elapsed since its txg has been 3370 * synced, it is difficult to reliably determine whether a commit 3371 * callback hasn't been called due to high load or due to a flawed 3372 * implementation. 3373 * 3374 * In practice, we will assume that if after a certain number of txgs a 3375 * commit callback hasn't been called, then most likely there's an 3376 * implementation bug.. 3377 */ 3378 tmp_cb = list_head(&zcl.zcl_callbacks); 3379 if (tmp_cb != NULL && 3380 tmp_cb->zcd_txg > txg - ZTEST_COMMIT_CALLBACK_THRESH) { 3381 fatal(0, "Commit callback threshold exceeded, oldest txg: %" 3382 PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg); 3383 } 3384 3385 /* 3386 * Let's find the place to insert our callbacks. 3387 * 3388 * Even though the list is ordered by txg, it is possible for the 3389 * insertion point to not be the end because our txg may already be 3390 * quiescing at this point and other callbacks in the open txg 3391 * (from other objsets) may have sneaked in. 3392 */ 3393 tmp_cb = list_tail(&zcl.zcl_callbacks); 3394 while (tmp_cb != NULL && tmp_cb->zcd_txg > txg) 3395 tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb); 3396 3397 /* Add the 3 callbacks to the list */ 3398 for (i = 0; i < 3; i++) { 3399 if (tmp_cb == NULL) 3400 list_insert_head(&zcl.zcl_callbacks, cb_data[i]); 3401 else 3402 list_insert_after(&zcl.zcl_callbacks, tmp_cb, 3403 cb_data[i]); 3404 3405 cb_data[i]->zcd_added = B_TRUE; 3406 VERIFY(!cb_data[i]->zcd_called); 3407 3408 tmp_cb = cb_data[i]; 3409 } 3410 3411 (void) mutex_unlock(&zcl.zcl_callbacks_lock); 3412 3413 dmu_tx_commit(tx); 3414 } 3415 3416 void 3417 ztest_dsl_prop_get_set(ztest_args_t *za) 3418 { 3419 objset_t *os = za->za_os; 3420 int i, inherit; 3421 uint64_t value; 3422 const char *prop, *valname; 3423 char setpoint[MAXPATHLEN]; 3424 char osname[MAXNAMELEN]; 3425 int error; 3426 3427 (void) rw_rdlock(&ztest_shared->zs_name_lock); 3428 3429 dmu_objset_name(os, osname); 3430 3431 for (i = 0; i < 2; i++) { 3432 if (i == 0) { 3433 prop = "checksum"; 3434 value = ztest_random_checksum(); 3435 inherit = (value == ZIO_CHECKSUM_INHERIT); 3436 } else { 3437 prop = "compression"; 3438 value = ztest_random_compress(); 3439 inherit = (value == ZIO_COMPRESS_INHERIT); 3440 } 3441 3442 error = dsl_prop_set(osname, prop, sizeof (value), 3443 !inherit, &value); 3444 3445 if (error == ENOSPC) { 3446 ztest_record_enospc("dsl_prop_set"); 3447 break; 3448 } 3449 3450 ASSERT3U(error, ==, 0); 3451 3452 VERIFY3U(dsl_prop_get(osname, prop, sizeof (value), 3453 1, &value, setpoint), ==, 0); 3454 3455 if (i == 0) 3456 valname = zio_checksum_table[value].ci_name; 3457 else 3458 valname = zio_compress_table[value].ci_name; 3459 3460 if (zopt_verbose >= 6) { 3461 (void) printf("%s %s = %s for '%s'\n", 3462 osname, prop, valname, setpoint); 3463 } 3464 } 3465 3466 (void) rw_unlock(&ztest_shared->zs_name_lock); 3467 } 3468 3469 /* 3470 * Inject random faults into the on-disk data. 3471 */ 3472 void 3473 ztest_fault_inject(ztest_args_t *za) 3474 { 3475 int fd; 3476 uint64_t offset; 3477 uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz; 3478 uint64_t bad = 0x1990c0ffeedecade; 3479 uint64_t top, leaf; 3480 char path0[MAXPATHLEN]; 3481 char pathrand[MAXPATHLEN]; 3482 size_t fsize; 3483 spa_t *spa = za->za_spa; 3484 int bshift = SPA_MAXBLOCKSHIFT + 2; /* don't scrog all labels */ 3485 int iters = 1000; 3486 int maxfaults = zopt_maxfaults; 3487 vdev_t *vd0 = NULL; 3488 uint64_t guid0 = 0; 3489 3490 ASSERT(leaves >= 1); 3491 3492 /* 3493 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd. 3494 */ 3495 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 3496 3497 if (ztest_random(2) == 0) { 3498 /* 3499 * Inject errors on a normal data device. 3500 */ 3501 top = ztest_random(spa->spa_root_vdev->vdev_children); 3502 leaf = ztest_random(leaves); 3503 3504 /* 3505 * Generate paths to the first leaf in this top-level vdev, 3506 * and to the random leaf we selected. We'll induce transient 3507 * write failures and random online/offline activity on leaf 0, 3508 * and we'll write random garbage to the randomly chosen leaf. 3509 */ 3510 (void) snprintf(path0, sizeof (path0), ztest_dev_template, 3511 zopt_dir, zopt_pool, top * leaves + 0); 3512 (void) snprintf(pathrand, sizeof (pathrand), ztest_dev_template, 3513 zopt_dir, zopt_pool, top * leaves + leaf); 3514 3515 vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0); 3516 if (vd0 != NULL && maxfaults != 1) { 3517 /* 3518 * Make vd0 explicitly claim to be unreadable, 3519 * or unwriteable, or reach behind its back 3520 * and close the underlying fd. We can do this if 3521 * maxfaults == 0 because we'll fail and reexecute, 3522 * and we can do it if maxfaults >= 2 because we'll 3523 * have enough redundancy. If maxfaults == 1, the 3524 * combination of this with injection of random data 3525 * corruption below exceeds the pool's fault tolerance. 3526 */ 3527 vdev_file_t *vf = vd0->vdev_tsd; 3528 3529 if (vf != NULL && ztest_random(3) == 0) { 3530 (void) close(vf->vf_vnode->v_fd); 3531 vf->vf_vnode->v_fd = -1; 3532 } else if (ztest_random(2) == 0) { 3533 vd0->vdev_cant_read = B_TRUE; 3534 } else { 3535 vd0->vdev_cant_write = B_TRUE; 3536 } 3537 guid0 = vd0->vdev_guid; 3538 } 3539 } else { 3540 /* 3541 * Inject errors on an l2cache device. 3542 */ 3543 spa_aux_vdev_t *sav = &spa->spa_l2cache; 3544 3545 if (sav->sav_count == 0) { 3546 spa_config_exit(spa, SCL_STATE, FTAG); 3547 return; 3548 } 3549 vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)]; 3550 guid0 = vd0->vdev_guid; 3551 (void) strcpy(path0, vd0->vdev_path); 3552 (void) strcpy(pathrand, vd0->vdev_path); 3553 3554 leaf = 0; 3555 leaves = 1; 3556 maxfaults = INT_MAX; /* no limit on cache devices */ 3557 } 3558 3559 spa_config_exit(spa, SCL_STATE, FTAG); 3560 3561 if (maxfaults == 0) 3562 return; 3563 3564 /* 3565 * If we can tolerate two or more faults, randomly online/offline vd0. 3566 */ 3567 if (maxfaults >= 2 && guid0 != 0) { 3568 if (ztest_random(10) < 6) { 3569 int flags = (ztest_random(2) == 0 ? 3570 ZFS_OFFLINE_TEMPORARY : 0); 3571 VERIFY(vdev_offline(spa, guid0, flags) != EBUSY); 3572 } else { 3573 (void) vdev_online(spa, guid0, 0, NULL); 3574 } 3575 } 3576 3577 /* 3578 * We have at least single-fault tolerance, so inject data corruption. 3579 */ 3580 fd = open(pathrand, O_RDWR); 3581 3582 if (fd == -1) /* we hit a gap in the device namespace */ 3583 return; 3584 3585 fsize = lseek(fd, 0, SEEK_END); 3586 3587 while (--iters != 0) { 3588 offset = ztest_random(fsize / (leaves << bshift)) * 3589 (leaves << bshift) + (leaf << bshift) + 3590 (ztest_random(1ULL << (bshift - 1)) & -8ULL); 3591 3592 if (offset >= fsize) 3593 continue; 3594 3595 if (zopt_verbose >= 6) 3596 (void) printf("injecting bad word into %s," 3597 " offset 0x%llx\n", pathrand, (u_longlong_t)offset); 3598 3599 if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad)) 3600 fatal(1, "can't inject bad word at 0x%llx in %s", 3601 offset, pathrand); 3602 } 3603 3604 (void) close(fd); 3605 } 3606 3607 /* 3608 * Scrub the pool. 3609 */ 3610 void 3611 ztest_scrub(ztest_args_t *za) 3612 { 3613 spa_t *spa = za->za_spa; 3614 3615 (void) spa_scrub(spa, POOL_SCRUB_EVERYTHING); 3616 (void) poll(NULL, 0, 1000); /* wait a second, then force a restart */ 3617 (void) spa_scrub(spa, POOL_SCRUB_EVERYTHING); 3618 } 3619 3620 /* 3621 * Rename the pool to a different name and then rename it back. 3622 */ 3623 void 3624 ztest_spa_rename(ztest_args_t *za) 3625 { 3626 char *oldname, *newname; 3627 int error; 3628 spa_t *spa; 3629 3630 (void) rw_wrlock(&ztest_shared->zs_name_lock); 3631 3632 oldname = za->za_pool; 3633 newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL); 3634 (void) strcpy(newname, oldname); 3635 (void) strcat(newname, "_tmp"); 3636 3637 /* 3638 * Do the rename 3639 */ 3640 error = spa_rename(oldname, newname); 3641 if (error) 3642 fatal(0, "spa_rename('%s', '%s') = %d", oldname, 3643 newname, error); 3644 3645 /* 3646 * Try to open it under the old name, which shouldn't exist 3647 */ 3648 error = spa_open(oldname, &spa, FTAG); 3649 if (error != ENOENT) 3650 fatal(0, "spa_open('%s') = %d", oldname, error); 3651 3652 /* 3653 * Open it under the new name and make sure it's still the same spa_t. 3654 */ 3655 error = spa_open(newname, &spa, FTAG); 3656 if (error != 0) 3657 fatal(0, "spa_open('%s') = %d", newname, error); 3658 3659 ASSERT(spa == za->za_spa); 3660 spa_close(spa, FTAG); 3661 3662 /* 3663 * Rename it back to the original 3664 */ 3665 error = spa_rename(newname, oldname); 3666 if (error) 3667 fatal(0, "spa_rename('%s', '%s') = %d", newname, 3668 oldname, error); 3669 3670 /* 3671 * Make sure it can still be opened 3672 */ 3673 error = spa_open(oldname, &spa, FTAG); 3674 if (error != 0) 3675 fatal(0, "spa_open('%s') = %d", oldname, error); 3676 3677 ASSERT(spa == za->za_spa); 3678 spa_close(spa, FTAG); 3679 3680 umem_free(newname, strlen(newname) + 1); 3681 3682 (void) rw_unlock(&ztest_shared->zs_name_lock); 3683 } 3684 3685 3686 /* 3687 * Completely obliterate one disk. 3688 */ 3689 static void 3690 ztest_obliterate_one_disk(uint64_t vdev) 3691 { 3692 int fd; 3693 char dev_name[MAXPATHLEN], copy_name[MAXPATHLEN]; 3694 size_t fsize; 3695 3696 if (zopt_maxfaults < 2) 3697 return; 3698 3699 (void) sprintf(dev_name, ztest_dev_template, zopt_dir, zopt_pool, vdev); 3700 (void) snprintf(copy_name, MAXPATHLEN, "%s.old", dev_name); 3701 3702 fd = open(dev_name, O_RDWR); 3703 3704 if (fd == -1) 3705 fatal(1, "can't open %s", dev_name); 3706 3707 /* 3708 * Determine the size. 3709 */ 3710 fsize = lseek(fd, 0, SEEK_END); 3711 3712 (void) close(fd); 3713 3714 /* 3715 * Rename the old device to dev_name.old (useful for debugging). 3716 */ 3717 VERIFY(rename(dev_name, copy_name) == 0); 3718 3719 /* 3720 * Create a new one. 3721 */ 3722 VERIFY((fd = open(dev_name, O_RDWR | O_CREAT | O_TRUNC, 0666)) >= 0); 3723 VERIFY(ftruncate(fd, fsize) == 0); 3724 (void) close(fd); 3725 } 3726 3727 static void 3728 ztest_replace_one_disk(spa_t *spa, uint64_t vdev) 3729 { 3730 char dev_name[MAXPATHLEN]; 3731 nvlist_t *root; 3732 int error; 3733 uint64_t guid; 3734 vdev_t *vd; 3735 3736 (void) sprintf(dev_name, ztest_dev_template, zopt_dir, zopt_pool, vdev); 3737 3738 /* 3739 * Build the nvlist describing dev_name. 3740 */ 3741 root = make_vdev_root(dev_name, NULL, 0, 0, 0, 0, 0, 1); 3742 3743 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); 3744 if ((vd = vdev_lookup_by_path(spa->spa_root_vdev, dev_name)) == NULL) 3745 guid = 0; 3746 else 3747 guid = vd->vdev_guid; 3748 spa_config_exit(spa, SCL_VDEV, FTAG); 3749 error = spa_vdev_attach(spa, guid, root, B_TRUE); 3750 if (error != 0 && 3751 error != EBUSY && 3752 error != ENOTSUP && 3753 error != ENODEV && 3754 error != EDOM) 3755 fatal(0, "spa_vdev_attach(in-place) = %d", error); 3756 3757 nvlist_free(root); 3758 } 3759 3760 static void 3761 ztest_verify_blocks(char *pool) 3762 { 3763 int status; 3764 char zdb[MAXPATHLEN + MAXNAMELEN + 20]; 3765 char zbuf[1024]; 3766 char *bin; 3767 char *ztest; 3768 char *isa; 3769 int isalen; 3770 FILE *fp; 3771 3772 (void) realpath(getexecname(), zdb); 3773 3774 /* zdb lives in /usr/sbin, while ztest lives in /usr/bin */ 3775 bin = strstr(zdb, "/usr/bin/"); 3776 ztest = strstr(bin, "/ztest"); 3777 isa = bin + 8; 3778 isalen = ztest - isa; 3779 isa = strdup(isa); 3780 /* LINTED */ 3781 (void) sprintf(bin, 3782 "/usr/sbin%.*s/zdb -bcc%s%s -U /tmp/zpool.cache %s", 3783 isalen, 3784 isa, 3785 zopt_verbose >= 3 ? "s" : "", 3786 zopt_verbose >= 4 ? "v" : "", 3787 pool); 3788 free(isa); 3789 3790 if (zopt_verbose >= 5) 3791 (void) printf("Executing %s\n", strstr(zdb, "zdb ")); 3792 3793 fp = popen(zdb, "r"); 3794 3795 while (fgets(zbuf, sizeof (zbuf), fp) != NULL) 3796 if (zopt_verbose >= 3) 3797 (void) printf("%s", zbuf); 3798 3799 status = pclose(fp); 3800 3801 if (status == 0) 3802 return; 3803 3804 ztest_dump_core = 0; 3805 if (WIFEXITED(status)) 3806 fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status)); 3807 else 3808 fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status)); 3809 } 3810 3811 static void 3812 ztest_walk_pool_directory(char *header) 3813 { 3814 spa_t *spa = NULL; 3815 3816 if (zopt_verbose >= 6) 3817 (void) printf("%s\n", header); 3818 3819 mutex_enter(&spa_namespace_lock); 3820 while ((spa = spa_next(spa)) != NULL) 3821 if (zopt_verbose >= 6) 3822 (void) printf("\t%s\n", spa_name(spa)); 3823 mutex_exit(&spa_namespace_lock); 3824 } 3825 3826 static void 3827 ztest_spa_import_export(char *oldname, char *newname) 3828 { 3829 nvlist_t *config, *newconfig; 3830 uint64_t pool_guid; 3831 spa_t *spa; 3832 int error; 3833 3834 if (zopt_verbose >= 4) { 3835 (void) printf("import/export: old = %s, new = %s\n", 3836 oldname, newname); 3837 } 3838 3839 /* 3840 * Clean up from previous runs. 3841 */ 3842 (void) spa_destroy(newname); 3843 3844 /* 3845 * Get the pool's configuration and guid. 3846 */ 3847 error = spa_open(oldname, &spa, FTAG); 3848 if (error) 3849 fatal(0, "spa_open('%s') = %d", oldname, error); 3850 3851 /* 3852 * Kick off a scrub to tickle scrub/export races. 3853 */ 3854 if (ztest_random(2) == 0) 3855 (void) spa_scrub(spa, POOL_SCRUB_EVERYTHING); 3856 3857 pool_guid = spa_guid(spa); 3858 spa_close(spa, FTAG); 3859 3860 ztest_walk_pool_directory("pools before export"); 3861 3862 /* 3863 * Export it. 3864 */ 3865 error = spa_export(oldname, &config, B_FALSE, B_FALSE); 3866 if (error) 3867 fatal(0, "spa_export('%s') = %d", oldname, error); 3868 3869 ztest_walk_pool_directory("pools after export"); 3870 3871 /* 3872 * Try to import it. 3873 */ 3874 newconfig = spa_tryimport(config); 3875 ASSERT(newconfig != NULL); 3876 nvlist_free(newconfig); 3877 3878 /* 3879 * Import it under the new name. 3880 */ 3881 error = spa_import(newname, config, NULL); 3882 if (error) 3883 fatal(0, "spa_import('%s') = %d", newname, error); 3884 3885 ztest_walk_pool_directory("pools after import"); 3886 3887 /* 3888 * Try to import it again -- should fail with EEXIST. 3889 */ 3890 error = spa_import(newname, config, NULL); 3891 if (error != EEXIST) 3892 fatal(0, "spa_import('%s') twice", newname); 3893 3894 /* 3895 * Try to import it under a different name -- should fail with EEXIST. 3896 */ 3897 error = spa_import(oldname, config, NULL); 3898 if (error != EEXIST) 3899 fatal(0, "spa_import('%s') under multiple names", newname); 3900 3901 /* 3902 * Verify that the pool is no longer visible under the old name. 3903 */ 3904 error = spa_open(oldname, &spa, FTAG); 3905 if (error != ENOENT) 3906 fatal(0, "spa_open('%s') = %d", newname, error); 3907 3908 /* 3909 * Verify that we can open and close the pool using the new name. 3910 */ 3911 error = spa_open(newname, &spa, FTAG); 3912 if (error) 3913 fatal(0, "spa_open('%s') = %d", newname, error); 3914 ASSERT(pool_guid == spa_guid(spa)); 3915 spa_close(spa, FTAG); 3916 3917 nvlist_free(config); 3918 } 3919 3920 static void 3921 ztest_resume(spa_t *spa) 3922 { 3923 if (spa_suspended(spa)) { 3924 spa_vdev_state_enter(spa); 3925 vdev_clear(spa, NULL); 3926 (void) spa_vdev_state_exit(spa, NULL, 0); 3927 (void) zio_resume(spa); 3928 } 3929 } 3930 3931 static void * 3932 ztest_resume_thread(void *arg) 3933 { 3934 spa_t *spa = arg; 3935 3936 while (!ztest_exiting) { 3937 (void) poll(NULL, 0, 1000); 3938 ztest_resume(spa); 3939 } 3940 return (NULL); 3941 } 3942 3943 static void * 3944 ztest_thread(void *arg) 3945 { 3946 ztest_args_t *za = arg; 3947 ztest_shared_t *zs = ztest_shared; 3948 hrtime_t now, functime; 3949 ztest_info_t *zi; 3950 int f, i; 3951 3952 while ((now = gethrtime()) < za->za_stop) { 3953 /* 3954 * See if it's time to force a crash. 3955 */ 3956 if (now > za->za_kill) { 3957 zs->zs_alloc = spa_get_alloc(za->za_spa); 3958 zs->zs_space = spa_get_space(za->za_spa); 3959 (void) kill(getpid(), SIGKILL); 3960 } 3961 3962 /* 3963 * Pick a random function. 3964 */ 3965 f = ztest_random(ZTEST_FUNCS); 3966 zi = &zs->zs_info[f]; 3967 3968 /* 3969 * Decide whether to call it, based on the requested frequency. 3970 */ 3971 if (zi->zi_call_target == 0 || 3972 (double)zi->zi_call_total / zi->zi_call_target > 3973 (double)(now - zs->zs_start_time) / (zopt_time * NANOSEC)) 3974 continue; 3975 3976 atomic_add_64(&zi->zi_calls, 1); 3977 atomic_add_64(&zi->zi_call_total, 1); 3978 3979 za->za_diroff = (za->za_instance * ZTEST_FUNCS + f) * 3980 ZTEST_DIRSIZE; 3981 za->za_diroff_shared = (1ULL << 63); 3982 3983 for (i = 0; i < zi->zi_iters; i++) 3984 zi->zi_func(za); 3985 3986 functime = gethrtime() - now; 3987 3988 atomic_add_64(&zi->zi_call_time, functime); 3989 3990 if (zopt_verbose >= 4) { 3991 Dl_info dli; 3992 (void) dladdr((void *)zi->zi_func, &dli); 3993 (void) printf("%6.2f sec in %s\n", 3994 (double)functime / NANOSEC, dli.dli_sname); 3995 } 3996 3997 /* 3998 * If we're getting ENOSPC with some regularity, stop. 3999 */ 4000 if (zs->zs_enospc_count > 10) 4001 break; 4002 } 4003 4004 return (NULL); 4005 } 4006 4007 /* 4008 * Kick off threads to run tests on all datasets in parallel. 4009 */ 4010 static void 4011 ztest_run(char *pool) 4012 { 4013 int t, d, error; 4014 ztest_shared_t *zs = ztest_shared; 4015 ztest_args_t *za; 4016 spa_t *spa; 4017 char name[100]; 4018 thread_t resume_tid; 4019 4020 ztest_exiting = B_FALSE; 4021 4022 (void) _mutex_init(&zs->zs_vdev_lock, USYNC_THREAD, NULL); 4023 (void) rwlock_init(&zs->zs_name_lock, USYNC_THREAD, NULL); 4024 4025 (void) _mutex_init(&zcl.zcl_callbacks_lock, USYNC_THREAD, 4026 NULL); 4027 4028 list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t), 4029 offsetof(ztest_cb_data_t, zcd_node)); 4030 4031 for (t = 0; t < ZTEST_SYNC_LOCKS; t++) 4032 (void) _mutex_init(&zs->zs_sync_lock[t], USYNC_THREAD, NULL); 4033 4034 /* 4035 * Destroy one disk before we even start. 4036 * It's mirrored, so everything should work just fine. 4037 * This makes us exercise fault handling very early in spa_load(). 4038 */ 4039 ztest_obliterate_one_disk(0); 4040 4041 /* 4042 * Verify that the sum of the sizes of all blocks in the pool 4043 * equals the SPA's allocated space total. 4044 */ 4045 ztest_verify_blocks(pool); 4046 4047 /* 4048 * Kick off a replacement of the disk we just obliterated. 4049 */ 4050 kernel_init(FREAD | FWRITE); 4051 VERIFY(spa_open(pool, &spa, FTAG) == 0); 4052 ztest_replace_one_disk(spa, 0); 4053 if (zopt_verbose >= 5) 4054 show_pool_stats(spa); 4055 spa_close(spa, FTAG); 4056 kernel_fini(); 4057 4058 kernel_init(FREAD | FWRITE); 4059 4060 /* 4061 * Verify that we can export the pool and reimport it under a 4062 * different name. 4063 */ 4064 if (ztest_random(2) == 0) { 4065 (void) snprintf(name, 100, "%s_import", pool); 4066 ztest_spa_import_export(pool, name); 4067 ztest_spa_import_export(name, pool); 4068 } 4069 4070 /* 4071 * Verify that we can loop over all pools. 4072 */ 4073 mutex_enter(&spa_namespace_lock); 4074 for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa)) { 4075 if (zopt_verbose > 3) { 4076 (void) printf("spa_next: found %s\n", spa_name(spa)); 4077 } 4078 } 4079 mutex_exit(&spa_namespace_lock); 4080 4081 /* 4082 * Open our pool. 4083 */ 4084 VERIFY(spa_open(pool, &spa, FTAG) == 0); 4085 4086 /* 4087 * We don't expect the pool to suspend unless maxfaults == 0, 4088 * in which case ztest_fault_inject() temporarily takes away 4089 * the only valid replica. 4090 */ 4091 if (zopt_maxfaults == 0) 4092 spa->spa_failmode = ZIO_FAILURE_MODE_WAIT; 4093 else 4094 spa->spa_failmode = ZIO_FAILURE_MODE_PANIC; 4095 4096 /* 4097 * Create a thread to periodically resume suspended I/O. 4098 */ 4099 VERIFY(thr_create(0, 0, ztest_resume_thread, spa, THR_BOUND, 4100 &resume_tid) == 0); 4101 4102 /* 4103 * Verify that we can safely inquire about about any object, 4104 * whether it's allocated or not. To make it interesting, 4105 * we probe a 5-wide window around each power of two. 4106 * This hits all edge cases, including zero and the max. 4107 */ 4108 for (t = 0; t < 64; t++) { 4109 for (d = -5; d <= 5; d++) { 4110 error = dmu_object_info(spa->spa_meta_objset, 4111 (1ULL << t) + d, NULL); 4112 ASSERT(error == 0 || error == ENOENT || 4113 error == EINVAL); 4114 } 4115 } 4116 4117 /* 4118 * Now kick off all the tests that run in parallel. 4119 */ 4120 zs->zs_enospc_count = 0; 4121 4122 za = umem_zalloc(zopt_threads * sizeof (ztest_args_t), UMEM_NOFAIL); 4123 4124 if (zopt_verbose >= 4) 4125 (void) printf("starting main threads...\n"); 4126 4127 za[0].za_start = gethrtime(); 4128 za[0].za_stop = za[0].za_start + zopt_passtime * NANOSEC; 4129 za[0].za_stop = MIN(za[0].za_stop, zs->zs_stop_time); 4130 za[0].za_kill = za[0].za_stop; 4131 if (ztest_random(100) < zopt_killrate) 4132 za[0].za_kill -= ztest_random(zopt_passtime * NANOSEC); 4133 4134 for (t = 0; t < zopt_threads; t++) { 4135 d = t % zopt_datasets; 4136 4137 (void) strcpy(za[t].za_pool, pool); 4138 za[t].za_os = za[d].za_os; 4139 za[t].za_spa = spa; 4140 za[t].za_zilog = za[d].za_zilog; 4141 za[t].za_instance = t; 4142 za[t].za_random = ztest_random(-1ULL); 4143 za[t].za_start = za[0].za_start; 4144 za[t].za_stop = za[0].za_stop; 4145 za[t].za_kill = za[0].za_kill; 4146 4147 if (t < zopt_datasets) { 4148 int test_future = FALSE; 4149 (void) rw_rdlock(&ztest_shared->zs_name_lock); 4150 (void) snprintf(name, 100, "%s/%s_%d", pool, pool, d); 4151 error = dmu_objset_create(name, DMU_OST_OTHER, 0, 4152 ztest_create_cb, NULL); 4153 if (error == EEXIST) { 4154 test_future = TRUE; 4155 } else if (error == ENOSPC) { 4156 zs->zs_enospc_count++; 4157 (void) rw_unlock(&ztest_shared->zs_name_lock); 4158 break; 4159 } else if (error != 0) { 4160 fatal(0, "dmu_objset_create(%s) = %d", 4161 name, error); 4162 } 4163 error = dmu_objset_hold(name, FTAG, &za[d].za_os); 4164 if (error) 4165 fatal(0, "dmu_objset_open('%s') = %d", 4166 name, error); 4167 (void) rw_unlock(&ztest_shared->zs_name_lock); 4168 if (test_future) 4169 ztest_dmu_check_future_leak(&za[t]); 4170 zil_replay(za[d].za_os, za[d].za_os, 4171 ztest_replay_vector); 4172 za[d].za_zilog = zil_open(za[d].za_os, NULL); 4173 } 4174 4175 VERIFY(thr_create(0, 0, ztest_thread, &za[t], THR_BOUND, 4176 &za[t].za_thread) == 0); 4177 } 4178 4179 while (--t >= 0) { 4180 VERIFY(thr_join(za[t].za_thread, NULL, NULL) == 0); 4181 if (t < zopt_datasets) { 4182 zil_close(za[t].za_zilog); 4183 dmu_objset_rele(za[t].za_os, FTAG); 4184 } 4185 } 4186 4187 if (zopt_verbose >= 3) 4188 show_pool_stats(spa); 4189 4190 txg_wait_synced(spa_get_dsl(spa), 0); 4191 4192 zs->zs_alloc = spa_get_alloc(spa); 4193 zs->zs_space = spa_get_space(spa); 4194 4195 /* 4196 * If we had out-of-space errors, destroy a random objset. 4197 */ 4198 if (zs->zs_enospc_count != 0) { 4199 (void) rw_rdlock(&ztest_shared->zs_name_lock); 4200 d = (int)ztest_random(zopt_datasets); 4201 (void) snprintf(name, 100, "%s/%s_%d", pool, pool, d); 4202 if (zopt_verbose >= 3) 4203 (void) printf("Destroying %s to free up space\n", name); 4204 4205 /* Cleanup any non-standard clones and snapshots */ 4206 ztest_dsl_dataset_cleanup(name, za[d].za_instance); 4207 4208 (void) dmu_objset_find(name, ztest_destroy_cb, &za[d], 4209 DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN); 4210 (void) rw_unlock(&ztest_shared->zs_name_lock); 4211 } 4212 4213 txg_wait_synced(spa_get_dsl(spa), 0); 4214 4215 umem_free(za, zopt_threads * sizeof (ztest_args_t)); 4216 4217 /* Kill the resume thread */ 4218 ztest_exiting = B_TRUE; 4219 VERIFY(thr_join(resume_tid, NULL, NULL) == 0); 4220 ztest_resume(spa); 4221 4222 /* 4223 * Right before closing the pool, kick off a bunch of async I/O; 4224 * spa_close() should wait for it to complete. 4225 */ 4226 for (t = 1; t < 50; t++) 4227 dmu_prefetch(spa->spa_meta_objset, t, 0, 1 << 15); 4228 4229 spa_close(spa, FTAG); 4230 4231 kernel_fini(); 4232 4233 list_destroy(&zcl.zcl_callbacks); 4234 4235 (void) _mutex_destroy(&zcl.zcl_callbacks_lock); 4236 4237 (void) rwlock_destroy(&zs->zs_name_lock); 4238 (void) _mutex_destroy(&zs->zs_vdev_lock); 4239 } 4240 4241 void 4242 print_time(hrtime_t t, char *timebuf) 4243 { 4244 hrtime_t s = t / NANOSEC; 4245 hrtime_t m = s / 60; 4246 hrtime_t h = m / 60; 4247 hrtime_t d = h / 24; 4248 4249 s -= m * 60; 4250 m -= h * 60; 4251 h -= d * 24; 4252 4253 timebuf[0] = '\0'; 4254 4255 if (d) 4256 (void) sprintf(timebuf, 4257 "%llud%02lluh%02llum%02llus", d, h, m, s); 4258 else if (h) 4259 (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s); 4260 else if (m) 4261 (void) sprintf(timebuf, "%llum%02llus", m, s); 4262 else 4263 (void) sprintf(timebuf, "%llus", s); 4264 } 4265 4266 /* 4267 * Create a storage pool with the given name and initial vdev size. 4268 * Then create the specified number of datasets in the pool. 4269 */ 4270 static void 4271 ztest_init(char *pool) 4272 { 4273 spa_t *spa; 4274 int error; 4275 nvlist_t *nvroot; 4276 4277 kernel_init(FREAD | FWRITE); 4278 4279 /* 4280 * Create the storage pool. 4281 */ 4282 (void) spa_destroy(pool); 4283 ztest_shared->zs_vdev_next_leaf = 0; 4284 nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0, 4285 0, zopt_raidz, zopt_mirrors, 1); 4286 error = spa_create(pool, nvroot, NULL, NULL, NULL); 4287 nvlist_free(nvroot); 4288 4289 if (error) 4290 fatal(0, "spa_create() = %d", error); 4291 error = spa_open(pool, &spa, FTAG); 4292 if (error) 4293 fatal(0, "spa_open() = %d", error); 4294 4295 metaslab_sz = 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift; 4296 4297 if (zopt_verbose >= 3) 4298 show_pool_stats(spa); 4299 4300 spa_close(spa, FTAG); 4301 4302 kernel_fini(); 4303 } 4304 4305 int 4306 main(int argc, char **argv) 4307 { 4308 int kills = 0; 4309 int iters = 0; 4310 int i, f; 4311 ztest_shared_t *zs; 4312 ztest_info_t *zi; 4313 char timebuf[100]; 4314 char numbuf[6]; 4315 4316 (void) setvbuf(stdout, NULL, _IOLBF, 0); 4317 4318 /* Override location of zpool.cache */ 4319 spa_config_path = "/tmp/zpool.cache"; 4320 4321 ztest_random_fd = open("/dev/urandom", O_RDONLY); 4322 4323 process_options(argc, argv); 4324 4325 /* 4326 * Blow away any existing copy of zpool.cache 4327 */ 4328 if (zopt_init != 0) 4329 (void) remove("/tmp/zpool.cache"); 4330 4331 zs = ztest_shared = (void *)mmap(0, 4332 P2ROUNDUP(sizeof (ztest_shared_t), getpagesize()), 4333 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0); 4334 4335 if (zopt_verbose >= 1) { 4336 (void) printf("%llu vdevs, %d datasets, %d threads," 4337 " %llu seconds...\n", 4338 (u_longlong_t)zopt_vdevs, zopt_datasets, zopt_threads, 4339 (u_longlong_t)zopt_time); 4340 } 4341 4342 /* 4343 * Create and initialize our storage pool. 4344 */ 4345 for (i = 1; i <= zopt_init; i++) { 4346 bzero(zs, sizeof (ztest_shared_t)); 4347 if (zopt_verbose >= 3 && zopt_init != 1) 4348 (void) printf("ztest_init(), pass %d\n", i); 4349 ztest_init(zopt_pool); 4350 } 4351 4352 /* 4353 * Initialize the call targets for each function. 4354 */ 4355 for (f = 0; f < ZTEST_FUNCS; f++) { 4356 zi = &zs->zs_info[f]; 4357 4358 *zi = ztest_info[f]; 4359 4360 if (*zi->zi_interval == 0) 4361 zi->zi_call_target = UINT64_MAX; 4362 else 4363 zi->zi_call_target = zopt_time / *zi->zi_interval; 4364 } 4365 4366 zs->zs_start_time = gethrtime(); 4367 zs->zs_stop_time = zs->zs_start_time + zopt_time * NANOSEC; 4368 4369 /* 4370 * Run the tests in a loop. These tests include fault injection 4371 * to verify that self-healing data works, and forced crashes 4372 * to verify that we never lose on-disk consistency. 4373 */ 4374 while (gethrtime() < zs->zs_stop_time) { 4375 int status; 4376 pid_t pid; 4377 char *tmp; 4378 4379 /* 4380 * Initialize the workload counters for each function. 4381 */ 4382 for (f = 0; f < ZTEST_FUNCS; f++) { 4383 zi = &zs->zs_info[f]; 4384 zi->zi_calls = 0; 4385 zi->zi_call_time = 0; 4386 } 4387 4388 /* Set the allocation switch size */ 4389 metaslab_df_alloc_threshold = ztest_random(metaslab_sz / 4) + 1; 4390 4391 pid = fork(); 4392 4393 if (pid == -1) 4394 fatal(1, "fork failed"); 4395 4396 if (pid == 0) { /* child */ 4397 struct rlimit rl = { 1024, 1024 }; 4398 (void) setrlimit(RLIMIT_NOFILE, &rl); 4399 (void) enable_extended_FILE_stdio(-1, -1); 4400 ztest_run(zopt_pool); 4401 exit(0); 4402 } 4403 4404 while (waitpid(pid, &status, 0) != pid) 4405 continue; 4406 4407 if (WIFEXITED(status)) { 4408 if (WEXITSTATUS(status) != 0) { 4409 (void) fprintf(stderr, 4410 "child exited with code %d\n", 4411 WEXITSTATUS(status)); 4412 exit(2); 4413 } 4414 } else if (WIFSIGNALED(status)) { 4415 if (WTERMSIG(status) != SIGKILL) { 4416 (void) fprintf(stderr, 4417 "child died with signal %d\n", 4418 WTERMSIG(status)); 4419 exit(3); 4420 } 4421 kills++; 4422 } else { 4423 (void) fprintf(stderr, "something strange happened " 4424 "to child\n"); 4425 exit(4); 4426 } 4427 4428 iters++; 4429 4430 if (zopt_verbose >= 1) { 4431 hrtime_t now = gethrtime(); 4432 4433 now = MIN(now, zs->zs_stop_time); 4434 print_time(zs->zs_stop_time - now, timebuf); 4435 nicenum(zs->zs_space, numbuf); 4436 4437 (void) printf("Pass %3d, %8s, %3llu ENOSPC, " 4438 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n", 4439 iters, 4440 WIFEXITED(status) ? "Complete" : "SIGKILL", 4441 (u_longlong_t)zs->zs_enospc_count, 4442 100.0 * zs->zs_alloc / zs->zs_space, 4443 numbuf, 4444 100.0 * (now - zs->zs_start_time) / 4445 (zopt_time * NANOSEC), timebuf); 4446 } 4447 4448 if (zopt_verbose >= 2) { 4449 (void) printf("\nWorkload summary:\n\n"); 4450 (void) printf("%7s %9s %s\n", 4451 "Calls", "Time", "Function"); 4452 (void) printf("%7s %9s %s\n", 4453 "-----", "----", "--------"); 4454 for (f = 0; f < ZTEST_FUNCS; f++) { 4455 Dl_info dli; 4456 4457 zi = &zs->zs_info[f]; 4458 print_time(zi->zi_call_time, timebuf); 4459 (void) dladdr((void *)zi->zi_func, &dli); 4460 (void) printf("%7llu %9s %s\n", 4461 (u_longlong_t)zi->zi_calls, timebuf, 4462 dli.dli_sname); 4463 } 4464 (void) printf("\n"); 4465 } 4466 4467 /* 4468 * It's possible that we killed a child during a rename test, in 4469 * which case we'll have a 'ztest_tmp' pool lying around instead 4470 * of 'ztest'. Do a blind rename in case this happened. 4471 */ 4472 tmp = umem_alloc(strlen(zopt_pool) + 5, UMEM_NOFAIL); 4473 (void) strcpy(tmp, zopt_pool); 4474 (void) strcat(tmp, "_tmp"); 4475 kernel_init(FREAD | FWRITE); 4476 (void) spa_rename(tmp, zopt_pool); 4477 kernel_fini(); 4478 umem_free(tmp, strlen(tmp) + 1); 4479 } 4480 4481 ztest_verify_blocks(zopt_pool); 4482 4483 if (zopt_verbose >= 1) { 4484 (void) printf("%d killed, %d completed, %.0f%% kill rate\n", 4485 kills, iters - kills, (100.0 * kills) / MAX(1, iters)); 4486 } 4487 4488 return (0); 4489 } 4490