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_write_parallel, 30, &zopt_always }, 207 { ztest_dmu_object_alloc_free, 1, &zopt_always }, 208 { ztest_dmu_commit_callbacks, 10, &zopt_always }, 209 { ztest_zap, 30, &zopt_always }, 210 { ztest_fzap, 30, &zopt_always }, 211 { ztest_zap_parallel, 100, &zopt_always }, 212 { ztest_dmu_read_write_zcopy, 1, &zopt_sometimes }, 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 uint64_t guid = vd->vdev_guid; 1249 uint64_t generation = spa->spa_config_generation + 1; 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 (generation != spa->spa_config_generation) { 1266 if (zopt_verbose >= 5) { 1267 (void) printf("vdev configuration has changed, " 1268 "guid %llu, state %llu, expected gen %llu, " 1269 "got gen %llu\n", (u_longlong_t)guid, 1270 (u_longlong_t)tvd->vdev_state, 1271 (u_longlong_t)generation, 1272 (u_longlong_t)spa->spa_config_generation); 1273 } 1274 return (vd); 1275 } 1276 return (NULL); 1277 } 1278 1279 /* 1280 * Traverse the vdev tree calling the supplied function. 1281 * We continue to walk the tree until we either have walked all 1282 * children or we receive a non-NULL return from the callback. 1283 * If a NULL callback is passed, then we just return back the first 1284 * leaf vdev we encounter. 1285 */ 1286 vdev_t * 1287 vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg) 1288 { 1289 if (vd->vdev_ops->vdev_op_leaf) { 1290 if (func == NULL) 1291 return (vd); 1292 else 1293 return (func(vd, arg)); 1294 } 1295 1296 for (uint_t c = 0; c < vd->vdev_children; c++) { 1297 vdev_t *cvd = vd->vdev_child[c]; 1298 if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL) 1299 return (cvd); 1300 } 1301 return (NULL); 1302 } 1303 1304 /* 1305 * Verify that dynamic LUN growth works as expected. 1306 */ 1307 void 1308 ztest_vdev_LUN_growth(ztest_args_t *za) 1309 { 1310 spa_t *spa = za->za_spa; 1311 vdev_t *vd, *tvd = NULL; 1312 size_t psize, newsize; 1313 uint64_t spa_newsize, spa_cursize, ms_count; 1314 1315 (void) mutex_lock(&ztest_shared->zs_vdev_lock); 1316 spa_config_enter(spa, SCL_STATE, spa, RW_READER); 1317 1318 while (tvd == NULL || tvd->vdev_islog) { 1319 uint64_t vdev; 1320 1321 vdev = ztest_random(spa->spa_root_vdev->vdev_children); 1322 tvd = spa->spa_root_vdev->vdev_child[vdev]; 1323 } 1324 1325 /* 1326 * Determine the size of the first leaf vdev associated with 1327 * our top-level device. 1328 */ 1329 vd = vdev_walk_tree(tvd, NULL, NULL); 1330 ASSERT3P(vd, !=, NULL); 1331 ASSERT(vd->vdev_ops->vdev_op_leaf); 1332 1333 psize = vd->vdev_psize; 1334 1335 /* 1336 * We only try to expand the vdev if it's healthy, less than 4x its 1337 * original size, and it has a valid psize. 1338 */ 1339 if (tvd->vdev_state != VDEV_STATE_HEALTHY || 1340 psize == 0 || psize >= 4 * zopt_vdev_size) { 1341 spa_config_exit(spa, SCL_STATE, spa); 1342 (void) mutex_unlock(&ztest_shared->zs_vdev_lock); 1343 return; 1344 } 1345 ASSERT(psize > 0); 1346 newsize = psize + psize / 8; 1347 ASSERT3U(newsize, >, psize); 1348 1349 if (zopt_verbose >= 6) { 1350 (void) printf("Expanding vdev %s from %lu to %lu\n", 1351 vd->vdev_path, (ulong_t)psize, (ulong_t)newsize); 1352 } 1353 1354 spa_cursize = spa_get_space(spa); 1355 ms_count = tvd->vdev_ms_count; 1356 1357 /* 1358 * Growing the vdev is a two step process: 1359 * 1). expand the physical size (i.e. relabel) 1360 * 2). online the vdev to create the new metaslabs 1361 */ 1362 if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL || 1363 vdev_walk_tree(tvd, online_vdev, NULL) != NULL || 1364 tvd->vdev_state != VDEV_STATE_HEALTHY) { 1365 if (zopt_verbose >= 5) { 1366 (void) printf("Could not expand LUN because " 1367 "the vdev configuration changed.\n"); 1368 } 1369 (void) spa_config_exit(spa, SCL_STATE, spa); 1370 (void) mutex_unlock(&ztest_shared->zs_vdev_lock); 1371 return; 1372 } 1373 1374 (void) spa_config_exit(spa, SCL_STATE, spa); 1375 1376 /* 1377 * Expanding the LUN will update the config asynchronously, 1378 * thus we must wait for the async thread to complete any 1379 * pending tasks before proceeding. 1380 */ 1381 mutex_enter(&spa->spa_async_lock); 1382 while (spa->spa_async_thread != NULL || spa->spa_async_tasks) 1383 cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 1384 mutex_exit(&spa->spa_async_lock); 1385 1386 spa_config_enter(spa, SCL_STATE, spa, RW_READER); 1387 spa_newsize = spa_get_space(spa); 1388 1389 /* 1390 * Make sure we were able to grow the pool. 1391 */ 1392 if (ms_count >= tvd->vdev_ms_count || 1393 spa_cursize >= spa_newsize) { 1394 (void) printf("Top-level vdev metaslab count: " 1395 "before %llu, after %llu\n", 1396 (u_longlong_t)ms_count, 1397 (u_longlong_t)tvd->vdev_ms_count); 1398 fatal(0, "LUN expansion failed: before %llu, " 1399 "after %llu\n", spa_cursize, spa_newsize); 1400 } else if (zopt_verbose >= 5) { 1401 char oldnumbuf[6], newnumbuf[6]; 1402 1403 nicenum(spa_cursize, oldnumbuf); 1404 nicenum(spa_newsize, newnumbuf); 1405 (void) printf("%s grew from %s to %s\n", 1406 spa->spa_name, oldnumbuf, newnumbuf); 1407 } 1408 spa_config_exit(spa, SCL_STATE, spa); 1409 (void) mutex_unlock(&ztest_shared->zs_vdev_lock); 1410 } 1411 1412 /* ARGSUSED */ 1413 static void 1414 ztest_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) 1415 { 1416 /* 1417 * Create the directory object. 1418 */ 1419 VERIFY(dmu_object_claim(os, ZTEST_DIROBJ, 1420 DMU_OT_UINT64_OTHER, ZTEST_DIROBJ_BLOCKSIZE, 1421 DMU_OT_UINT64_OTHER, 5 * sizeof (ztest_block_tag_t), tx) == 0); 1422 1423 VERIFY(zap_create_claim(os, ZTEST_MICROZAP_OBJ, 1424 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0); 1425 1426 VERIFY(zap_create_claim(os, ZTEST_FATZAP_OBJ, 1427 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0); 1428 } 1429 1430 static int 1431 ztest_destroy_cb(char *name, void *arg) 1432 { 1433 ztest_args_t *za = arg; 1434 objset_t *os; 1435 dmu_object_info_t *doi = &za->za_doi; 1436 int error; 1437 1438 /* 1439 * Verify that the dataset contains a directory object. 1440 */ 1441 error = dmu_objset_hold(name, FTAG, &os); 1442 ASSERT3U(error, ==, 0); 1443 error = dmu_object_info(os, ZTEST_DIROBJ, doi); 1444 if (error != ENOENT) { 1445 /* We could have crashed in the middle of destroying it */ 1446 ASSERT3U(error, ==, 0); 1447 ASSERT3U(doi->doi_type, ==, DMU_OT_UINT64_OTHER); 1448 ASSERT3S(doi->doi_physical_blks, >=, 0); 1449 } 1450 dmu_objset_rele(os, FTAG); 1451 1452 /* 1453 * Destroy the dataset. 1454 */ 1455 error = dmu_objset_destroy(name, B_FALSE); 1456 if (error) { 1457 (void) dmu_objset_hold(name, FTAG, &os); 1458 fatal(0, "dmu_objset_destroy(os=%p) = %d\n", os, error); 1459 } 1460 return (0); 1461 } 1462 1463 /* 1464 * Verify that dmu_objset_{create,destroy,open,close} work as expected. 1465 */ 1466 static uint64_t 1467 ztest_log_create(zilog_t *zilog, dmu_tx_t *tx, uint64_t object, int mode) 1468 { 1469 itx_t *itx; 1470 lr_create_t *lr; 1471 size_t namesize; 1472 char name[24]; 1473 1474 (void) sprintf(name, "ZOBJ_%llu", (u_longlong_t)object); 1475 namesize = strlen(name) + 1; 1476 1477 itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize + 1478 ztest_random(ZIL_MAX_BLKSZ)); 1479 lr = (lr_create_t *)&itx->itx_lr; 1480 bzero(lr + 1, lr->lr_common.lrc_reclen - sizeof (*lr)); 1481 lr->lr_doid = object; 1482 lr->lr_foid = 0; 1483 lr->lr_mode = mode; 1484 lr->lr_uid = 0; 1485 lr->lr_gid = 0; 1486 lr->lr_gen = dmu_tx_get_txg(tx); 1487 lr->lr_crtime[0] = time(NULL); 1488 lr->lr_crtime[1] = 0; 1489 lr->lr_rdev = 0; 1490 bcopy(name, (char *)(lr + 1), namesize); 1491 1492 return (zil_itx_assign(zilog, itx, tx)); 1493 } 1494 1495 void 1496 ztest_dmu_objset_create_destroy(ztest_args_t *za) 1497 { 1498 int error; 1499 objset_t *os, *os2; 1500 char name[100]; 1501 zilog_t *zilog; 1502 uint64_t seq; 1503 uint64_t objects; 1504 1505 (void) rw_rdlock(&ztest_shared->zs_name_lock); 1506 (void) snprintf(name, 100, "%s/%s_temp_%llu", za->za_pool, za->za_pool, 1507 (u_longlong_t)za->za_instance); 1508 1509 /* 1510 * If this dataset exists from a previous run, process its replay log 1511 * half of the time. If we don't replay it, then dmu_objset_destroy() 1512 * (invoked from ztest_destroy_cb() below) should just throw it away. 1513 */ 1514 if (ztest_random(2) == 0 && 1515 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) { 1516 zil_replay(os, os, ztest_replay_vector); 1517 dmu_objset_disown(os, FTAG); 1518 } 1519 1520 /* 1521 * There may be an old instance of the dataset we're about to 1522 * create lying around from a previous run. If so, destroy it 1523 * and all of its snapshots. 1524 */ 1525 (void) dmu_objset_find(name, ztest_destroy_cb, za, 1526 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS); 1527 1528 /* 1529 * Verify that the destroyed dataset is no longer in the namespace. 1530 */ 1531 error = dmu_objset_hold(name, FTAG, &os); 1532 if (error != ENOENT) 1533 fatal(1, "dmu_objset_open(%s) found destroyed dataset %p", 1534 name, os); 1535 1536 /* 1537 * Verify that we can create a new dataset. 1538 */ 1539 error = dmu_objset_create(name, DMU_OST_OTHER, 0, 1540 ztest_create_cb, NULL); 1541 if (error) { 1542 if (error == ENOSPC) { 1543 ztest_record_enospc("dmu_objset_create"); 1544 (void) rw_unlock(&ztest_shared->zs_name_lock); 1545 return; 1546 } 1547 fatal(0, "dmu_objset_create(%s) = %d", name, error); 1548 } 1549 1550 error = dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os); 1551 if (error) { 1552 fatal(0, "dmu_objset_open(%s) = %d", name, error); 1553 } 1554 1555 /* 1556 * Open the intent log for it. 1557 */ 1558 zilog = zil_open(os, NULL); 1559 1560 /* 1561 * Put a random number of objects in there. 1562 */ 1563 objects = ztest_random(20); 1564 seq = 0; 1565 while (objects-- != 0) { 1566 uint64_t object; 1567 dmu_tx_t *tx = dmu_tx_create(os); 1568 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, sizeof (name)); 1569 error = dmu_tx_assign(tx, TXG_WAIT); 1570 if (error) { 1571 dmu_tx_abort(tx); 1572 } else { 1573 object = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0, 1574 DMU_OT_NONE, 0, tx); 1575 ztest_set_random_blocksize(os, object, tx); 1576 seq = ztest_log_create(zilog, tx, object, 1577 DMU_OT_UINT64_OTHER); 1578 dmu_write(os, object, 0, sizeof (name), name, tx); 1579 dmu_tx_commit(tx); 1580 } 1581 if (ztest_random(5) == 0) { 1582 zil_commit(zilog, seq, object); 1583 } 1584 if (ztest_random(100) == 0) { 1585 error = zil_suspend(zilog); 1586 if (error == 0) { 1587 zil_resume(zilog); 1588 } 1589 } 1590 } 1591 1592 /* 1593 * Verify that we cannot create an existing dataset. 1594 */ 1595 error = dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL); 1596 if (error != EEXIST) 1597 fatal(0, "created existing dataset, error = %d", error); 1598 1599 /* 1600 * Verify that we can hold an objset that is also owned. 1601 */ 1602 error = dmu_objset_hold(name, FTAG, &os2); 1603 if (error) 1604 fatal(0, "dmu_objset_open('%s') = %d", name, error); 1605 dmu_objset_rele(os2, FTAG); 1606 1607 /* 1608 * Verify that we can not own an objset that is already owned. 1609 */ 1610 error = dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2); 1611 if (error != EBUSY) 1612 fatal(0, "dmu_objset_open('%s') = %d, expected EBUSY", 1613 name, error); 1614 1615 zil_close(zilog); 1616 dmu_objset_disown(os, FTAG); 1617 1618 error = dmu_objset_destroy(name, B_FALSE); 1619 if (error) 1620 fatal(0, "dmu_objset_destroy(%s) = %d", name, error); 1621 1622 (void) rw_unlock(&ztest_shared->zs_name_lock); 1623 } 1624 1625 /* 1626 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected. 1627 */ 1628 void 1629 ztest_dmu_snapshot_create_destroy(ztest_args_t *za) 1630 { 1631 int error; 1632 objset_t *os = za->za_os; 1633 char snapname[100]; 1634 char osname[MAXNAMELEN]; 1635 1636 (void) rw_rdlock(&ztest_shared->zs_name_lock); 1637 dmu_objset_name(os, osname); 1638 (void) snprintf(snapname, 100, "%s@%llu", osname, 1639 (u_longlong_t)za->za_instance); 1640 1641 error = dmu_objset_destroy(snapname, B_FALSE); 1642 if (error != 0 && error != ENOENT) 1643 fatal(0, "dmu_objset_destroy() = %d", error); 1644 error = dmu_objset_snapshot(osname, strchr(snapname, '@')+1, 1645 NULL, FALSE); 1646 if (error == ENOSPC) 1647 ztest_record_enospc("dmu_take_snapshot"); 1648 else if (error != 0 && error != EEXIST) 1649 fatal(0, "dmu_take_snapshot() = %d", error); 1650 (void) rw_unlock(&ztest_shared->zs_name_lock); 1651 } 1652 1653 /* 1654 * Cleanup non-standard snapshots and clones. 1655 */ 1656 void 1657 ztest_dsl_dataset_cleanup(char *osname, uint64_t curval) 1658 { 1659 char snap1name[100]; 1660 char clone1name[100]; 1661 char snap2name[100]; 1662 char clone2name[100]; 1663 char snap3name[100]; 1664 int error; 1665 1666 (void) snprintf(snap1name, 100, "%s@s1_%llu", osname, curval); 1667 (void) snprintf(clone1name, 100, "%s/c1_%llu", osname, curval); 1668 (void) snprintf(snap2name, 100, "%s@s2_%llu", clone1name, curval); 1669 (void) snprintf(clone2name, 100, "%s/c2_%llu", osname, curval); 1670 (void) snprintf(snap3name, 100, "%s@s3_%llu", clone1name, curval); 1671 1672 error = dmu_objset_destroy(clone2name, B_FALSE); 1673 if (error && error != ENOENT) 1674 fatal(0, "dmu_objset_destroy(%s) = %d", clone2name, error); 1675 error = dmu_objset_destroy(snap3name, B_FALSE); 1676 if (error && error != ENOENT) 1677 fatal(0, "dmu_objset_destroy(%s) = %d", snap3name, error); 1678 error = dmu_objset_destroy(snap2name, B_FALSE); 1679 if (error && error != ENOENT) 1680 fatal(0, "dmu_objset_destroy(%s) = %d", snap2name, error); 1681 error = dmu_objset_destroy(clone1name, B_FALSE); 1682 if (error && error != ENOENT) 1683 fatal(0, "dmu_objset_destroy(%s) = %d", clone1name, error); 1684 error = dmu_objset_destroy(snap1name, B_FALSE); 1685 if (error && error != ENOENT) 1686 fatal(0, "dmu_objset_destroy(%s) = %d", snap1name, error); 1687 } 1688 1689 /* 1690 * Verify dsl_dataset_promote handles EBUSY 1691 */ 1692 void 1693 ztest_dsl_dataset_promote_busy(ztest_args_t *za) 1694 { 1695 int error; 1696 objset_t *os = za->za_os; 1697 objset_t *clone; 1698 dsl_dataset_t *ds; 1699 char snap1name[100]; 1700 char clone1name[100]; 1701 char snap2name[100]; 1702 char clone2name[100]; 1703 char snap3name[100]; 1704 char osname[MAXNAMELEN]; 1705 uint64_t curval = za->za_instance; 1706 1707 (void) rw_rdlock(&ztest_shared->zs_name_lock); 1708 1709 dmu_objset_name(os, osname); 1710 ztest_dsl_dataset_cleanup(osname, curval); 1711 1712 (void) snprintf(snap1name, 100, "%s@s1_%llu", osname, curval); 1713 (void) snprintf(clone1name, 100, "%s/c1_%llu", osname, curval); 1714 (void) snprintf(snap2name, 100, "%s@s2_%llu", clone1name, curval); 1715 (void) snprintf(clone2name, 100, "%s/c2_%llu", osname, curval); 1716 (void) snprintf(snap3name, 100, "%s@s3_%llu", clone1name, curval); 1717 1718 error = dmu_objset_snapshot(osname, strchr(snap1name, '@')+1, 1719 NULL, FALSE); 1720 if (error && error != EEXIST) { 1721 if (error == ENOSPC) { 1722 ztest_record_enospc("dmu_take_snapshot"); 1723 goto out; 1724 } 1725 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error); 1726 } 1727 1728 error = dmu_objset_hold(snap1name, FTAG, &clone); 1729 if (error) 1730 fatal(0, "dmu_open_snapshot(%s) = %d", snap1name, error); 1731 1732 error = dmu_objset_clone(clone1name, dmu_objset_ds(clone), 0); 1733 dmu_objset_rele(clone, FTAG); 1734 if (error) { 1735 if (error == ENOSPC) { 1736 ztest_record_enospc("dmu_objset_create"); 1737 goto out; 1738 } 1739 fatal(0, "dmu_objset_create(%s) = %d", clone1name, error); 1740 } 1741 1742 error = dmu_objset_snapshot(clone1name, strchr(snap2name, '@')+1, 1743 NULL, FALSE); 1744 if (error && error != EEXIST) { 1745 if (error == ENOSPC) { 1746 ztest_record_enospc("dmu_take_snapshot"); 1747 goto out; 1748 } 1749 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error); 1750 } 1751 1752 error = dmu_objset_snapshot(clone1name, strchr(snap3name, '@')+1, 1753 NULL, FALSE); 1754 if (error && error != EEXIST) { 1755 if (error == ENOSPC) { 1756 ztest_record_enospc("dmu_take_snapshot"); 1757 goto out; 1758 } 1759 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error); 1760 } 1761 1762 error = dmu_objset_hold(snap3name, FTAG, &clone); 1763 if (error) 1764 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error); 1765 1766 error = dmu_objset_clone(clone2name, dmu_objset_ds(clone), 0); 1767 dmu_objset_rele(clone, FTAG); 1768 if (error) { 1769 if (error == ENOSPC) { 1770 ztest_record_enospc("dmu_objset_create"); 1771 goto out; 1772 } 1773 fatal(0, "dmu_objset_create(%s) = %d", clone2name, error); 1774 } 1775 1776 error = dsl_dataset_own(snap1name, B_FALSE, FTAG, &ds); 1777 if (error) 1778 fatal(0, "dsl_dataset_own(%s) = %d", snap1name, error); 1779 error = dsl_dataset_promote(clone2name, NULL); 1780 if (error != EBUSY) 1781 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name, 1782 error); 1783 dsl_dataset_disown(ds, FTAG); 1784 1785 out: 1786 ztest_dsl_dataset_cleanup(osname, curval); 1787 1788 (void) rw_unlock(&ztest_shared->zs_name_lock); 1789 } 1790 1791 /* 1792 * Verify that dmu_object_{alloc,free} work as expected. 1793 */ 1794 void 1795 ztest_dmu_object_alloc_free(ztest_args_t *za) 1796 { 1797 objset_t *os = za->za_os; 1798 dmu_buf_t *db; 1799 dmu_tx_t *tx; 1800 uint64_t batchobj, object, batchsize, endoff, temp; 1801 int b, c, error, bonuslen; 1802 dmu_object_info_t *doi = &za->za_doi; 1803 char osname[MAXNAMELEN]; 1804 1805 dmu_objset_name(os, osname); 1806 1807 endoff = -8ULL; 1808 batchsize = 2; 1809 1810 /* 1811 * Create a batch object if necessary, and record it in the directory. 1812 */ 1813 VERIFY3U(0, ==, dmu_read(os, ZTEST_DIROBJ, za->za_diroff, 1814 sizeof (uint64_t), &batchobj, DMU_READ_PREFETCH)); 1815 if (batchobj == 0) { 1816 tx = dmu_tx_create(os); 1817 dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, 1818 sizeof (uint64_t)); 1819 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 1820 error = dmu_tx_assign(tx, TXG_WAIT); 1821 if (error) { 1822 ztest_record_enospc("create a batch object"); 1823 dmu_tx_abort(tx); 1824 return; 1825 } 1826 batchobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0, 1827 DMU_OT_NONE, 0, tx); 1828 ztest_set_random_blocksize(os, batchobj, tx); 1829 dmu_write(os, ZTEST_DIROBJ, za->za_diroff, 1830 sizeof (uint64_t), &batchobj, tx); 1831 dmu_tx_commit(tx); 1832 } 1833 1834 /* 1835 * Destroy the previous batch of objects. 1836 */ 1837 for (b = 0; b < batchsize; b++) { 1838 VERIFY3U(0, ==, dmu_read(os, batchobj, b * sizeof (uint64_t), 1839 sizeof (uint64_t), &object, DMU_READ_PREFETCH)); 1840 if (object == 0) 1841 continue; 1842 /* 1843 * Read and validate contents. 1844 * We expect the nth byte of the bonus buffer to be n. 1845 */ 1846 VERIFY(0 == dmu_bonus_hold(os, object, FTAG, &db)); 1847 za->za_dbuf = db; 1848 1849 dmu_object_info_from_db(db, doi); 1850 ASSERT(doi->doi_type == DMU_OT_UINT64_OTHER); 1851 ASSERT(doi->doi_bonus_type == DMU_OT_PLAIN_OTHER); 1852 ASSERT3S(doi->doi_physical_blks, >=, 0); 1853 1854 bonuslen = doi->doi_bonus_size; 1855 1856 for (c = 0; c < bonuslen; c++) { 1857 if (((uint8_t *)db->db_data)[c] != 1858 (uint8_t)(c + bonuslen)) { 1859 fatal(0, 1860 "bad bonus: %s, obj %llu, off %d: %u != %u", 1861 osname, object, c, 1862 ((uint8_t *)db->db_data)[c], 1863 (uint8_t)(c + bonuslen)); 1864 } 1865 } 1866 1867 dmu_buf_rele(db, FTAG); 1868 za->za_dbuf = NULL; 1869 1870 /* 1871 * We expect the word at endoff to be our object number. 1872 */ 1873 VERIFY(0 == dmu_read(os, object, endoff, 1874 sizeof (uint64_t), &temp, DMU_READ_PREFETCH)); 1875 1876 if (temp != object) { 1877 fatal(0, "bad data in %s, got %llu, expected %llu", 1878 osname, temp, object); 1879 } 1880 1881 /* 1882 * Destroy old object and clear batch entry. 1883 */ 1884 tx = dmu_tx_create(os); 1885 dmu_tx_hold_write(tx, batchobj, 1886 b * sizeof (uint64_t), sizeof (uint64_t)); 1887 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END); 1888 error = dmu_tx_assign(tx, TXG_WAIT); 1889 if (error) { 1890 ztest_record_enospc("free object"); 1891 dmu_tx_abort(tx); 1892 return; 1893 } 1894 error = dmu_object_free(os, object, tx); 1895 if (error) { 1896 fatal(0, "dmu_object_free('%s', %llu) = %d", 1897 osname, object, error); 1898 } 1899 object = 0; 1900 1901 dmu_object_set_checksum(os, batchobj, 1902 ztest_random_checksum(), tx); 1903 dmu_object_set_compress(os, batchobj, 1904 ztest_random_compress(), tx); 1905 1906 dmu_write(os, batchobj, b * sizeof (uint64_t), 1907 sizeof (uint64_t), &object, tx); 1908 1909 dmu_tx_commit(tx); 1910 } 1911 1912 /* 1913 * Before creating the new batch of objects, generate a bunch of churn. 1914 */ 1915 for (b = ztest_random(100); b > 0; b--) { 1916 tx = dmu_tx_create(os); 1917 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 1918 error = dmu_tx_assign(tx, TXG_WAIT); 1919 if (error) { 1920 ztest_record_enospc("churn objects"); 1921 dmu_tx_abort(tx); 1922 return; 1923 } 1924 object = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0, 1925 DMU_OT_NONE, 0, tx); 1926 ztest_set_random_blocksize(os, object, tx); 1927 error = dmu_object_free(os, object, tx); 1928 if (error) { 1929 fatal(0, "dmu_object_free('%s', %llu) = %d", 1930 osname, object, error); 1931 } 1932 dmu_tx_commit(tx); 1933 } 1934 1935 /* 1936 * Create a new batch of objects with randomly chosen 1937 * blocksizes and record them in the batch directory. 1938 */ 1939 for (b = 0; b < batchsize; b++) { 1940 uint32_t va_blksize; 1941 u_longlong_t va_nblocks; 1942 1943 tx = dmu_tx_create(os); 1944 dmu_tx_hold_write(tx, batchobj, b * sizeof (uint64_t), 1945 sizeof (uint64_t)); 1946 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 1947 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, endoff, 1948 sizeof (uint64_t)); 1949 error = dmu_tx_assign(tx, TXG_WAIT); 1950 if (error) { 1951 ztest_record_enospc("create batchobj"); 1952 dmu_tx_abort(tx); 1953 return; 1954 } 1955 bonuslen = (int)ztest_random(dmu_bonus_max()) + 1; 1956 1957 object = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0, 1958 DMU_OT_PLAIN_OTHER, bonuslen, tx); 1959 1960 ztest_set_random_blocksize(os, object, tx); 1961 1962 dmu_object_set_checksum(os, object, 1963 ztest_random_checksum(), tx); 1964 dmu_object_set_compress(os, object, 1965 ztest_random_compress(), tx); 1966 1967 dmu_write(os, batchobj, b * sizeof (uint64_t), 1968 sizeof (uint64_t), &object, tx); 1969 1970 /* 1971 * Write to both the bonus buffer and the regular data. 1972 */ 1973 VERIFY(dmu_bonus_hold(os, object, FTAG, &db) == 0); 1974 za->za_dbuf = db; 1975 ASSERT3U(bonuslen, <=, db->db_size); 1976 1977 dmu_object_size_from_db(db, &va_blksize, &va_nblocks); 1978 ASSERT3S(va_nblocks, >=, 0); 1979 1980 dmu_buf_will_dirty(db, tx); 1981 1982 /* 1983 * See comments above regarding the contents of 1984 * the bonus buffer and the word at endoff. 1985 */ 1986 for (c = 0; c < bonuslen; c++) 1987 ((uint8_t *)db->db_data)[c] = (uint8_t)(c + bonuslen); 1988 1989 dmu_buf_rele(db, FTAG); 1990 za->za_dbuf = NULL; 1991 1992 /* 1993 * Write to a large offset to increase indirection. 1994 */ 1995 dmu_write(os, object, endoff, sizeof (uint64_t), &object, tx); 1996 1997 dmu_tx_commit(tx); 1998 } 1999 } 2000 2001 /* 2002 * Verify that dmu_{read,write} work as expected. 2003 */ 2004 typedef struct bufwad { 2005 uint64_t bw_index; 2006 uint64_t bw_txg; 2007 uint64_t bw_data; 2008 } bufwad_t; 2009 2010 typedef struct dmu_read_write_dir { 2011 uint64_t dd_packobj; 2012 uint64_t dd_bigobj; 2013 uint64_t dd_chunk; 2014 } dmu_read_write_dir_t; 2015 2016 void 2017 ztest_dmu_read_write(ztest_args_t *za) 2018 { 2019 objset_t *os = za->za_os; 2020 dmu_read_write_dir_t dd; 2021 dmu_tx_t *tx; 2022 int i, freeit, error; 2023 uint64_t n, s, txg; 2024 bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT; 2025 uint64_t packoff, packsize, bigoff, bigsize; 2026 uint64_t regions = 997; 2027 uint64_t stride = 123456789ULL; 2028 uint64_t width = 40; 2029 int free_percent = 5; 2030 2031 /* 2032 * This test uses two objects, packobj and bigobj, that are always 2033 * updated together (i.e. in the same tx) so that their contents are 2034 * in sync and can be compared. Their contents relate to each other 2035 * in a simple way: packobj is a dense array of 'bufwad' structures, 2036 * while bigobj is a sparse array of the same bufwads. Specifically, 2037 * for any index n, there are three bufwads that should be identical: 2038 * 2039 * packobj, at offset n * sizeof (bufwad_t) 2040 * bigobj, at the head of the nth chunk 2041 * bigobj, at the tail of the nth chunk 2042 * 2043 * The chunk size is arbitrary. It doesn't have to be a power of two, 2044 * and it doesn't have any relation to the object blocksize. 2045 * The only requirement is that it can hold at least two bufwads. 2046 * 2047 * Normally, we write the bufwad to each of these locations. 2048 * However, free_percent of the time we instead write zeroes to 2049 * packobj and perform a dmu_free_range() on bigobj. By comparing 2050 * bigobj to packobj, we can verify that the DMU is correctly 2051 * tracking which parts of an object are allocated and free, 2052 * and that the contents of the allocated blocks are correct. 2053 */ 2054 2055 /* 2056 * Read the directory info. If it's the first time, set things up. 2057 */ 2058 VERIFY(0 == dmu_read(os, ZTEST_DIROBJ, za->za_diroff, 2059 sizeof (dd), &dd, DMU_READ_PREFETCH)); 2060 if (dd.dd_chunk == 0) { 2061 ASSERT(dd.dd_packobj == 0); 2062 ASSERT(dd.dd_bigobj == 0); 2063 tx = dmu_tx_create(os); 2064 dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, sizeof (dd)); 2065 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 2066 error = dmu_tx_assign(tx, TXG_WAIT); 2067 if (error) { 2068 ztest_record_enospc("create r/w directory"); 2069 dmu_tx_abort(tx); 2070 return; 2071 } 2072 2073 dd.dd_packobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0, 2074 DMU_OT_NONE, 0, tx); 2075 dd.dd_bigobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0, 2076 DMU_OT_NONE, 0, tx); 2077 dd.dd_chunk = (1000 + ztest_random(1000)) * sizeof (uint64_t); 2078 2079 ztest_set_random_blocksize(os, dd.dd_packobj, tx); 2080 ztest_set_random_blocksize(os, dd.dd_bigobj, tx); 2081 2082 dmu_write(os, ZTEST_DIROBJ, za->za_diroff, sizeof (dd), &dd, 2083 tx); 2084 dmu_tx_commit(tx); 2085 } 2086 2087 /* 2088 * Prefetch a random chunk of the big object. 2089 * Our aim here is to get some async reads in flight 2090 * for blocks that we may free below; the DMU should 2091 * handle this race correctly. 2092 */ 2093 n = ztest_random(regions) * stride + ztest_random(width); 2094 s = 1 + ztest_random(2 * width - 1); 2095 dmu_prefetch(os, dd.dd_bigobj, n * dd.dd_chunk, s * dd.dd_chunk); 2096 2097 /* 2098 * Pick a random index and compute the offsets into packobj and bigobj. 2099 */ 2100 n = ztest_random(regions) * stride + ztest_random(width); 2101 s = 1 + ztest_random(width - 1); 2102 2103 packoff = n * sizeof (bufwad_t); 2104 packsize = s * sizeof (bufwad_t); 2105 2106 bigoff = n * dd.dd_chunk; 2107 bigsize = s * dd.dd_chunk; 2108 2109 packbuf = umem_alloc(packsize, UMEM_NOFAIL); 2110 bigbuf = umem_alloc(bigsize, UMEM_NOFAIL); 2111 2112 /* 2113 * free_percent of the time, free a range of bigobj rather than 2114 * overwriting it. 2115 */ 2116 freeit = (ztest_random(100) < free_percent); 2117 2118 /* 2119 * Read the current contents of our objects. 2120 */ 2121 error = dmu_read(os, dd.dd_packobj, packoff, packsize, packbuf, 2122 DMU_READ_PREFETCH); 2123 ASSERT3U(error, ==, 0); 2124 error = dmu_read(os, dd.dd_bigobj, bigoff, bigsize, bigbuf, 2125 DMU_READ_PREFETCH); 2126 ASSERT3U(error, ==, 0); 2127 2128 /* 2129 * Get a tx for the mods to both packobj and bigobj. 2130 */ 2131 tx = dmu_tx_create(os); 2132 2133 dmu_tx_hold_write(tx, dd.dd_packobj, packoff, packsize); 2134 2135 if (freeit) 2136 dmu_tx_hold_free(tx, dd.dd_bigobj, bigoff, bigsize); 2137 else 2138 dmu_tx_hold_write(tx, dd.dd_bigobj, bigoff, bigsize); 2139 2140 error = dmu_tx_assign(tx, TXG_WAIT); 2141 2142 if (error) { 2143 ztest_record_enospc("dmu r/w range"); 2144 dmu_tx_abort(tx); 2145 umem_free(packbuf, packsize); 2146 umem_free(bigbuf, bigsize); 2147 return; 2148 } 2149 2150 txg = dmu_tx_get_txg(tx); 2151 2152 /* 2153 * For each index from n to n + s, verify that the existing bufwad 2154 * in packobj matches the bufwads at the head and tail of the 2155 * corresponding chunk in bigobj. Then update all three bufwads 2156 * with the new values we want to write out. 2157 */ 2158 for (i = 0; i < s; i++) { 2159 /* LINTED */ 2160 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t)); 2161 /* LINTED */ 2162 bigH = (bufwad_t *)((char *)bigbuf + i * dd.dd_chunk); 2163 /* LINTED */ 2164 bigT = (bufwad_t *)((char *)bigH + dd.dd_chunk) - 1; 2165 2166 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize); 2167 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize); 2168 2169 if (pack->bw_txg > txg) 2170 fatal(0, "future leak: got %llx, open txg is %llx", 2171 pack->bw_txg, txg); 2172 2173 if (pack->bw_data != 0 && pack->bw_index != n + i) 2174 fatal(0, "wrong index: got %llx, wanted %llx+%llx", 2175 pack->bw_index, n, i); 2176 2177 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0) 2178 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH); 2179 2180 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0) 2181 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT); 2182 2183 if (freeit) { 2184 bzero(pack, sizeof (bufwad_t)); 2185 } else { 2186 pack->bw_index = n + i; 2187 pack->bw_txg = txg; 2188 pack->bw_data = 1 + ztest_random(-2ULL); 2189 } 2190 *bigH = *pack; 2191 *bigT = *pack; 2192 } 2193 2194 /* 2195 * We've verified all the old bufwads, and made new ones. 2196 * Now write them out. 2197 */ 2198 dmu_write(os, dd.dd_packobj, packoff, packsize, packbuf, tx); 2199 2200 if (freeit) { 2201 if (zopt_verbose >= 6) { 2202 (void) printf("freeing offset %llx size %llx" 2203 " txg %llx\n", 2204 (u_longlong_t)bigoff, 2205 (u_longlong_t)bigsize, 2206 (u_longlong_t)txg); 2207 } 2208 VERIFY(0 == dmu_free_range(os, dd.dd_bigobj, bigoff, 2209 bigsize, tx)); 2210 } else { 2211 if (zopt_verbose >= 6) { 2212 (void) printf("writing offset %llx size %llx" 2213 " txg %llx\n", 2214 (u_longlong_t)bigoff, 2215 (u_longlong_t)bigsize, 2216 (u_longlong_t)txg); 2217 } 2218 dmu_write(os, dd.dd_bigobj, bigoff, bigsize, bigbuf, tx); 2219 } 2220 2221 dmu_tx_commit(tx); 2222 2223 /* 2224 * Sanity check the stuff we just wrote. 2225 */ 2226 { 2227 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL); 2228 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL); 2229 2230 VERIFY(0 == dmu_read(os, dd.dd_packobj, packoff, 2231 packsize, packcheck, DMU_READ_PREFETCH)); 2232 VERIFY(0 == dmu_read(os, dd.dd_bigobj, bigoff, 2233 bigsize, bigcheck, DMU_READ_PREFETCH)); 2234 2235 ASSERT(bcmp(packbuf, packcheck, packsize) == 0); 2236 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0); 2237 2238 umem_free(packcheck, packsize); 2239 umem_free(bigcheck, bigsize); 2240 } 2241 2242 umem_free(packbuf, packsize); 2243 umem_free(bigbuf, bigsize); 2244 } 2245 2246 void 2247 compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf, 2248 uint64_t bigsize, uint64_t n, dmu_read_write_dir_t dd, uint64_t txg) 2249 { 2250 uint64_t i; 2251 bufwad_t *pack; 2252 bufwad_t *bigH; 2253 bufwad_t *bigT; 2254 2255 /* 2256 * For each index from n to n + s, verify that the existing bufwad 2257 * in packobj matches the bufwads at the head and tail of the 2258 * corresponding chunk in bigobj. Then update all three bufwads 2259 * with the new values we want to write out. 2260 */ 2261 for (i = 0; i < s; i++) { 2262 /* LINTED */ 2263 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t)); 2264 /* LINTED */ 2265 bigH = (bufwad_t *)((char *)bigbuf + i * dd.dd_chunk); 2266 /* LINTED */ 2267 bigT = (bufwad_t *)((char *)bigH + dd.dd_chunk) - 1; 2268 2269 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize); 2270 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize); 2271 2272 if (pack->bw_txg > txg) 2273 fatal(0, "future leak: got %llx, open txg is %llx", 2274 pack->bw_txg, txg); 2275 2276 if (pack->bw_data != 0 && pack->bw_index != n + i) 2277 fatal(0, "wrong index: got %llx, wanted %llx+%llx", 2278 pack->bw_index, n, i); 2279 2280 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0) 2281 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH); 2282 2283 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0) 2284 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT); 2285 2286 pack->bw_index = n + i; 2287 pack->bw_txg = txg; 2288 pack->bw_data = 1 + ztest_random(-2ULL); 2289 2290 *bigH = *pack; 2291 *bigT = *pack; 2292 } 2293 } 2294 2295 void 2296 ztest_dmu_read_write_zcopy(ztest_args_t *za) 2297 { 2298 objset_t *os = za->za_os; 2299 dmu_read_write_dir_t dd; 2300 dmu_tx_t *tx; 2301 uint64_t i; 2302 int error; 2303 uint64_t n, s, txg; 2304 bufwad_t *packbuf, *bigbuf; 2305 uint64_t packoff, packsize, bigoff, bigsize; 2306 uint64_t regions = 997; 2307 uint64_t stride = 123456789ULL; 2308 uint64_t width = 9; 2309 dmu_buf_t *bonus_db; 2310 arc_buf_t **bigbuf_arcbufs; 2311 dmu_object_info_t *doi = &za->za_doi; 2312 2313 /* 2314 * This test uses two objects, packobj and bigobj, that are always 2315 * updated together (i.e. in the same tx) so that their contents are 2316 * in sync and can be compared. Their contents relate to each other 2317 * in a simple way: packobj is a dense array of 'bufwad' structures, 2318 * while bigobj is a sparse array of the same bufwads. Specifically, 2319 * for any index n, there are three bufwads that should be identical: 2320 * 2321 * packobj, at offset n * sizeof (bufwad_t) 2322 * bigobj, at the head of the nth chunk 2323 * bigobj, at the tail of the nth chunk 2324 * 2325 * The chunk size is set equal to bigobj block size so that 2326 * dmu_assign_arcbuf() can be tested for object updates. 2327 */ 2328 2329 /* 2330 * Read the directory info. If it's the first time, set things up. 2331 */ 2332 VERIFY(0 == dmu_read(os, ZTEST_DIROBJ, za->za_diroff, 2333 sizeof (dd), &dd, DMU_READ_PREFETCH)); 2334 if (dd.dd_chunk == 0) { 2335 ASSERT(dd.dd_packobj == 0); 2336 ASSERT(dd.dd_bigobj == 0); 2337 tx = dmu_tx_create(os); 2338 dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, sizeof (dd)); 2339 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 2340 error = dmu_tx_assign(tx, TXG_WAIT); 2341 if (error) { 2342 ztest_record_enospc("create r/w directory"); 2343 dmu_tx_abort(tx); 2344 return; 2345 } 2346 2347 dd.dd_packobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0, 2348 DMU_OT_NONE, 0, tx); 2349 dd.dd_bigobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0, 2350 DMU_OT_NONE, 0, tx); 2351 ztest_set_random_blocksize(os, dd.dd_packobj, tx); 2352 ztest_set_random_blocksize(os, dd.dd_bigobj, tx); 2353 2354 VERIFY(dmu_object_info(os, dd.dd_bigobj, doi) == 0); 2355 ASSERT(doi->doi_data_block_size >= 2 * sizeof (bufwad_t)); 2356 ASSERT(ISP2(doi->doi_data_block_size)); 2357 dd.dd_chunk = doi->doi_data_block_size; 2358 2359 dmu_write(os, ZTEST_DIROBJ, za->za_diroff, sizeof (dd), &dd, 2360 tx); 2361 dmu_tx_commit(tx); 2362 } else { 2363 VERIFY(dmu_object_info(os, dd.dd_bigobj, doi) == 0); 2364 VERIFY(ISP2(doi->doi_data_block_size)); 2365 VERIFY(dd.dd_chunk == doi->doi_data_block_size); 2366 VERIFY(dd.dd_chunk >= 2 * sizeof (bufwad_t)); 2367 } 2368 2369 /* 2370 * Pick a random index and compute the offsets into packobj and bigobj. 2371 */ 2372 n = ztest_random(regions) * stride + ztest_random(width); 2373 s = 1 + ztest_random(width - 1); 2374 2375 packoff = n * sizeof (bufwad_t); 2376 packsize = s * sizeof (bufwad_t); 2377 2378 bigoff = n * dd.dd_chunk; 2379 bigsize = s * dd.dd_chunk; 2380 2381 packbuf = umem_zalloc(packsize, UMEM_NOFAIL); 2382 bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL); 2383 2384 VERIFY(dmu_bonus_hold(os, dd.dd_bigobj, FTAG, &bonus_db) == 0); 2385 2386 bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL); 2387 2388 /* 2389 * Iteration 0 test zcopy for DB_UNCACHED dbufs. 2390 * Iteration 1 test zcopy to already referenced dbufs. 2391 * Iteration 2 test zcopy to dirty dbuf in the same txg. 2392 * Iteration 3 test zcopy to dbuf dirty in previous txg. 2393 * Iteration 4 test zcopy when dbuf is no longer dirty. 2394 * Iteration 5 test zcopy when it can't be done. 2395 * Iteration 6 one more zcopy write. 2396 */ 2397 for (i = 0; i < 7; i++) { 2398 uint64_t j; 2399 uint64_t off; 2400 2401 /* 2402 * In iteration 5 (i == 5) use arcbufs 2403 * that don't match bigobj blksz to test 2404 * dmu_assign_arcbuf() when it can't directly 2405 * assign an arcbuf to a dbuf. 2406 */ 2407 for (j = 0; j < s; j++) { 2408 if (i != 5) { 2409 bigbuf_arcbufs[j] = 2410 dmu_request_arcbuf(bonus_db, 2411 dd.dd_chunk); 2412 } else { 2413 bigbuf_arcbufs[2 * j] = 2414 dmu_request_arcbuf(bonus_db, 2415 dd.dd_chunk / 2); 2416 bigbuf_arcbufs[2 * j + 1] = 2417 dmu_request_arcbuf(bonus_db, 2418 dd.dd_chunk / 2); 2419 } 2420 } 2421 2422 /* 2423 * Get a tx for the mods to both packobj and bigobj. 2424 */ 2425 tx = dmu_tx_create(os); 2426 2427 dmu_tx_hold_write(tx, dd.dd_packobj, packoff, packsize); 2428 dmu_tx_hold_write(tx, dd.dd_bigobj, bigoff, bigsize); 2429 2430 if (ztest_random(100) == 0) { 2431 error = -1; 2432 } else { 2433 error = dmu_tx_assign(tx, TXG_WAIT); 2434 } 2435 2436 if (error) { 2437 if (error != -1) { 2438 ztest_record_enospc("dmu r/w range"); 2439 } 2440 dmu_tx_abort(tx); 2441 umem_free(packbuf, packsize); 2442 umem_free(bigbuf, bigsize); 2443 for (j = 0; j < s; j++) { 2444 if (i != 5) { 2445 dmu_return_arcbuf(bigbuf_arcbufs[j]); 2446 } else { 2447 dmu_return_arcbuf( 2448 bigbuf_arcbufs[2 * j]); 2449 dmu_return_arcbuf( 2450 bigbuf_arcbufs[2 * j + 1]); 2451 } 2452 } 2453 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *)); 2454 dmu_buf_rele(bonus_db, FTAG); 2455 return; 2456 } 2457 2458 txg = dmu_tx_get_txg(tx); 2459 2460 /* 2461 * 50% of the time don't read objects in the 1st iteration to 2462 * test dmu_assign_arcbuf() for the case when there're no 2463 * existing dbufs for the specified offsets. 2464 */ 2465 if (i != 0 || ztest_random(2) != 0) { 2466 error = dmu_read(os, dd.dd_packobj, packoff, 2467 packsize, packbuf, DMU_READ_PREFETCH); 2468 ASSERT3U(error, ==, 0); 2469 error = dmu_read(os, dd.dd_bigobj, bigoff, bigsize, 2470 bigbuf, DMU_READ_PREFETCH); 2471 ASSERT3U(error, ==, 0); 2472 } 2473 compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize, 2474 n, dd, txg); 2475 2476 /* 2477 * We've verified all the old bufwads, and made new ones. 2478 * Now write them out. 2479 */ 2480 dmu_write(os, dd.dd_packobj, packoff, packsize, packbuf, tx); 2481 if (zopt_verbose >= 6) { 2482 (void) printf("writing offset %llx size %llx" 2483 " txg %llx\n", 2484 (u_longlong_t)bigoff, 2485 (u_longlong_t)bigsize, 2486 (u_longlong_t)txg); 2487 } 2488 for (off = bigoff, j = 0; j < s; j++, off += dd.dd_chunk) { 2489 dmu_buf_t *dbt; 2490 if (i != 5) { 2491 bcopy((caddr_t)bigbuf + (off - bigoff), 2492 bigbuf_arcbufs[j]->b_data, dd.dd_chunk); 2493 } else { 2494 bcopy((caddr_t)bigbuf + (off - bigoff), 2495 bigbuf_arcbufs[2 * j]->b_data, 2496 dd.dd_chunk / 2); 2497 bcopy((caddr_t)bigbuf + (off - bigoff) + 2498 dd.dd_chunk / 2, 2499 bigbuf_arcbufs[2 * j + 1]->b_data, 2500 dd.dd_chunk / 2); 2501 } 2502 2503 if (i == 1) { 2504 VERIFY(dmu_buf_hold(os, dd.dd_bigobj, off, 2505 FTAG, &dbt) == 0); 2506 } 2507 if (i != 5) { 2508 dmu_assign_arcbuf(bonus_db, off, 2509 bigbuf_arcbufs[j], tx); 2510 } else { 2511 dmu_assign_arcbuf(bonus_db, off, 2512 bigbuf_arcbufs[2 * j], tx); 2513 dmu_assign_arcbuf(bonus_db, 2514 off + dd.dd_chunk / 2, 2515 bigbuf_arcbufs[2 * j + 1], tx); 2516 } 2517 if (i == 1) { 2518 dmu_buf_rele(dbt, FTAG); 2519 } 2520 } 2521 dmu_tx_commit(tx); 2522 2523 /* 2524 * Sanity check the stuff we just wrote. 2525 */ 2526 { 2527 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL); 2528 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL); 2529 2530 VERIFY(0 == dmu_read(os, dd.dd_packobj, packoff, 2531 packsize, packcheck, DMU_READ_PREFETCH)); 2532 VERIFY(0 == dmu_read(os, dd.dd_bigobj, bigoff, 2533 bigsize, bigcheck, DMU_READ_PREFETCH)); 2534 2535 ASSERT(bcmp(packbuf, packcheck, packsize) == 0); 2536 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0); 2537 2538 umem_free(packcheck, packsize); 2539 umem_free(bigcheck, bigsize); 2540 } 2541 if (i == 2) { 2542 txg_wait_open(dmu_objset_pool(os), 0); 2543 } else if (i == 3) { 2544 txg_wait_synced(dmu_objset_pool(os), 0); 2545 } 2546 } 2547 2548 dmu_buf_rele(bonus_db, FTAG); 2549 umem_free(packbuf, packsize); 2550 umem_free(bigbuf, bigsize); 2551 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *)); 2552 } 2553 2554 void 2555 ztest_dmu_check_future_leak(ztest_args_t *za) 2556 { 2557 objset_t *os = za->za_os; 2558 dmu_buf_t *db; 2559 ztest_block_tag_t *bt; 2560 dmu_object_info_t *doi = &za->za_doi; 2561 2562 /* 2563 * Make sure that, if there is a write record in the bonus buffer 2564 * of the ZTEST_DIROBJ, that the txg for this record is <= the 2565 * last synced txg of the pool. 2566 */ 2567 VERIFY(dmu_bonus_hold(os, ZTEST_DIROBJ, FTAG, &db) == 0); 2568 za->za_dbuf = db; 2569 VERIFY(dmu_object_info(os, ZTEST_DIROBJ, doi) == 0); 2570 ASSERT3U(doi->doi_bonus_size, >=, sizeof (*bt)); 2571 ASSERT3U(doi->doi_bonus_size, <=, db->db_size); 2572 ASSERT3U(doi->doi_bonus_size % sizeof (*bt), ==, 0); 2573 bt = (void *)((char *)db->db_data + doi->doi_bonus_size - sizeof (*bt)); 2574 if (bt->bt_objset != 0) { 2575 ASSERT3U(bt->bt_objset, ==, dmu_objset_id(os)); 2576 ASSERT3U(bt->bt_object, ==, ZTEST_DIROBJ); 2577 ASSERT3U(bt->bt_offset, ==, -1ULL); 2578 ASSERT3U(bt->bt_txg, <, spa_first_txg(za->za_spa)); 2579 } 2580 dmu_buf_rele(db, FTAG); 2581 za->za_dbuf = NULL; 2582 } 2583 2584 void 2585 ztest_dmu_write_parallel(ztest_args_t *za) 2586 { 2587 objset_t *os = za->za_os; 2588 ztest_block_tag_t *rbt = &za->za_rbt; 2589 ztest_block_tag_t *wbt = &za->za_wbt; 2590 const size_t btsize = sizeof (ztest_block_tag_t); 2591 dmu_buf_t *db; 2592 int b, error; 2593 int bs = ZTEST_DIROBJ_BLOCKSIZE; 2594 int do_free = 0; 2595 uint64_t off, txg, txg_how; 2596 mutex_t *lp; 2597 char osname[MAXNAMELEN]; 2598 char iobuf[SPA_MAXBLOCKSIZE]; 2599 blkptr_t blk = { 0 }; 2600 uint64_t blkoff; 2601 zbookmark_t zb; 2602 dmu_tx_t *tx = dmu_tx_create(os); 2603 dmu_buf_t *bonus_db; 2604 arc_buf_t *abuf = NULL; 2605 2606 dmu_objset_name(os, osname); 2607 2608 /* 2609 * Have multiple threads write to large offsets in ZTEST_DIROBJ 2610 * to verify that having multiple threads writing to the same object 2611 * in parallel doesn't cause any trouble. 2612 */ 2613 if (ztest_random(4) == 0) { 2614 /* 2615 * Do the bonus buffer instead of a regular block. 2616 * We need a lock to serialize resize vs. others, 2617 * so we hash on the objset ID. 2618 */ 2619 b = dmu_objset_id(os) % ZTEST_SYNC_LOCKS; 2620 off = -1ULL; 2621 dmu_tx_hold_bonus(tx, ZTEST_DIROBJ); 2622 } else { 2623 b = ztest_random(ZTEST_SYNC_LOCKS); 2624 off = za->za_diroff_shared + (b << SPA_MAXBLOCKSHIFT); 2625 if (ztest_random(4) == 0) { 2626 do_free = 1; 2627 dmu_tx_hold_free(tx, ZTEST_DIROBJ, off, bs); 2628 } else { 2629 dmu_tx_hold_write(tx, ZTEST_DIROBJ, off, bs); 2630 } 2631 } 2632 2633 if (off != -1ULL && P2PHASE(off, bs) == 0 && !do_free && 2634 ztest_random(8) == 0) { 2635 VERIFY(dmu_bonus_hold(os, ZTEST_DIROBJ, FTAG, &bonus_db) == 0); 2636 abuf = dmu_request_arcbuf(bonus_db, bs); 2637 } 2638 2639 txg_how = ztest_random(2) == 0 ? TXG_WAIT : TXG_NOWAIT; 2640 error = dmu_tx_assign(tx, txg_how); 2641 if (error) { 2642 if (error == ERESTART) { 2643 ASSERT(txg_how == TXG_NOWAIT); 2644 dmu_tx_wait(tx); 2645 } else { 2646 ztest_record_enospc("dmu write parallel"); 2647 } 2648 dmu_tx_abort(tx); 2649 if (abuf != NULL) { 2650 dmu_return_arcbuf(abuf); 2651 dmu_buf_rele(bonus_db, FTAG); 2652 } 2653 return; 2654 } 2655 txg = dmu_tx_get_txg(tx); 2656 2657 lp = &ztest_shared->zs_sync_lock[b]; 2658 (void) mutex_lock(lp); 2659 2660 wbt->bt_objset = dmu_objset_id(os); 2661 wbt->bt_object = ZTEST_DIROBJ; 2662 wbt->bt_offset = off; 2663 wbt->bt_txg = txg; 2664 wbt->bt_thread = za->za_instance; 2665 wbt->bt_seq = ztest_shared->zs_seq[b]++; /* protected by lp */ 2666 2667 /* 2668 * Occasionally, write an all-zero block to test the behavior 2669 * of blocks that compress into holes. 2670 */ 2671 if (off != -1ULL && ztest_random(8) == 0) 2672 bzero(wbt, btsize); 2673 2674 if (off == -1ULL) { 2675 dmu_object_info_t *doi = &za->za_doi; 2676 char *dboff; 2677 2678 VERIFY(dmu_bonus_hold(os, ZTEST_DIROBJ, FTAG, &db) == 0); 2679 za->za_dbuf = db; 2680 dmu_object_info_from_db(db, doi); 2681 ASSERT3U(doi->doi_bonus_size, <=, db->db_size); 2682 ASSERT3U(doi->doi_bonus_size, >=, btsize); 2683 ASSERT3U(doi->doi_bonus_size % btsize, ==, 0); 2684 dboff = (char *)db->db_data + doi->doi_bonus_size - btsize; 2685 bcopy(dboff, rbt, btsize); 2686 if (rbt->bt_objset != 0) { 2687 ASSERT3U(rbt->bt_objset, ==, wbt->bt_objset); 2688 ASSERT3U(rbt->bt_object, ==, wbt->bt_object); 2689 ASSERT3U(rbt->bt_offset, ==, wbt->bt_offset); 2690 ASSERT3U(rbt->bt_txg, <=, wbt->bt_txg); 2691 } 2692 if (ztest_random(10) == 0) { 2693 int newsize = (ztest_random(db->db_size / 2694 btsize) + 1) * btsize; 2695 2696 ASSERT3U(newsize, >=, btsize); 2697 ASSERT3U(newsize, <=, db->db_size); 2698 VERIFY3U(dmu_set_bonus(db, newsize, tx), ==, 0); 2699 dboff = (char *)db->db_data + newsize - btsize; 2700 } 2701 dmu_buf_will_dirty(db, tx); 2702 bcopy(wbt, dboff, btsize); 2703 dmu_buf_rele(db, FTAG); 2704 za->za_dbuf = NULL; 2705 } else if (do_free) { 2706 VERIFY(dmu_free_range(os, ZTEST_DIROBJ, off, bs, tx) == 0); 2707 } else if (abuf == NULL) { 2708 dmu_write(os, ZTEST_DIROBJ, off, btsize, wbt, tx); 2709 } else { 2710 bcopy(wbt, abuf->b_data, btsize); 2711 dmu_assign_arcbuf(bonus_db, off, abuf, tx); 2712 dmu_buf_rele(bonus_db, FTAG); 2713 } 2714 2715 (void) mutex_unlock(lp); 2716 2717 if (ztest_random(1000) == 0) 2718 (void) poll(NULL, 0, 1); /* open dn_notxholds window */ 2719 2720 dmu_tx_commit(tx); 2721 2722 if (ztest_random(10000) == 0) 2723 txg_wait_synced(dmu_objset_pool(os), txg); 2724 2725 if (off == -1ULL || do_free) 2726 return; 2727 2728 if (ztest_random(2) != 0) 2729 return; 2730 2731 /* 2732 * dmu_sync() the block we just wrote. 2733 */ 2734 (void) mutex_lock(lp); 2735 2736 blkoff = P2ALIGN_TYPED(off, bs, uint64_t); 2737 error = dmu_buf_hold(os, ZTEST_DIROBJ, blkoff, FTAG, &db); 2738 za->za_dbuf = db; 2739 if (error) { 2740 (void) mutex_unlock(lp); 2741 return; 2742 } 2743 blkoff = off - blkoff; 2744 error = dmu_sync(NULL, db, &blk, txg, NULL, NULL); 2745 dmu_buf_rele(db, FTAG); 2746 za->za_dbuf = NULL; 2747 2748 if (error) { 2749 (void) mutex_unlock(lp); 2750 return; 2751 } 2752 2753 if (blk.blk_birth == 0) { /* concurrent free */ 2754 (void) mutex_unlock(lp); 2755 return; 2756 } 2757 2758 txg_suspend(dmu_objset_pool(os)); 2759 2760 (void) mutex_unlock(lp); 2761 2762 ASSERT(blk.blk_fill == 1); 2763 ASSERT3U(BP_GET_TYPE(&blk), ==, DMU_OT_UINT64_OTHER); 2764 ASSERT3U(BP_GET_LEVEL(&blk), ==, 0); 2765 ASSERT3U(BP_GET_LSIZE(&blk), ==, bs); 2766 2767 /* 2768 * Read the block that dmu_sync() returned to make sure its contents 2769 * match what we wrote. We do this while still txg_suspend()ed 2770 * to ensure that the block can't be reused before we read it. 2771 */ 2772 zb.zb_objset = dmu_objset_id(os); 2773 zb.zb_object = ZTEST_DIROBJ; 2774 zb.zb_level = 0; 2775 zb.zb_blkid = off / bs; 2776 error = zio_wait(zio_read(NULL, za->za_spa, &blk, iobuf, bs, 2777 NULL, NULL, ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_MUSTSUCCEED, &zb)); 2778 ASSERT3U(error, ==, 0); 2779 2780 txg_resume(dmu_objset_pool(os)); 2781 2782 bcopy(&iobuf[blkoff], rbt, btsize); 2783 2784 if (rbt->bt_objset == 0) /* concurrent free */ 2785 return; 2786 2787 if (wbt->bt_objset == 0) /* all-zero overwrite */ 2788 return; 2789 2790 ASSERT3U(rbt->bt_objset, ==, wbt->bt_objset); 2791 ASSERT3U(rbt->bt_object, ==, wbt->bt_object); 2792 ASSERT3U(rbt->bt_offset, ==, wbt->bt_offset); 2793 2794 /* 2795 * The semantic of dmu_sync() is that we always push the most recent 2796 * version of the data, so in the face of concurrent updates we may 2797 * see a newer version of the block. That's OK. 2798 */ 2799 ASSERT3U(rbt->bt_txg, >=, wbt->bt_txg); 2800 if (rbt->bt_thread == wbt->bt_thread) 2801 ASSERT3U(rbt->bt_seq, ==, wbt->bt_seq); 2802 else 2803 ASSERT3U(rbt->bt_seq, >, wbt->bt_seq); 2804 } 2805 2806 /* 2807 * Verify that zap_{create,destroy,add,remove,update} work as expected. 2808 */ 2809 #define ZTEST_ZAP_MIN_INTS 1 2810 #define ZTEST_ZAP_MAX_INTS 4 2811 #define ZTEST_ZAP_MAX_PROPS 1000 2812 2813 void 2814 ztest_zap(ztest_args_t *za) 2815 { 2816 objset_t *os = za->za_os; 2817 uint64_t object; 2818 uint64_t txg, last_txg; 2819 uint64_t value[ZTEST_ZAP_MAX_INTS]; 2820 uint64_t zl_ints, zl_intsize, prop; 2821 int i, ints; 2822 dmu_tx_t *tx; 2823 char propname[100], txgname[100]; 2824 int error; 2825 char osname[MAXNAMELEN]; 2826 char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" }; 2827 2828 dmu_objset_name(os, osname); 2829 2830 /* 2831 * Create a new object if necessary, and record it in the directory. 2832 */ 2833 VERIFY(0 == dmu_read(os, ZTEST_DIROBJ, za->za_diroff, 2834 sizeof (uint64_t), &object, DMU_READ_PREFETCH)); 2835 2836 if (object == 0) { 2837 tx = dmu_tx_create(os); 2838 dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, 2839 sizeof (uint64_t)); 2840 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, NULL); 2841 error = dmu_tx_assign(tx, TXG_WAIT); 2842 if (error) { 2843 ztest_record_enospc("create zap test obj"); 2844 dmu_tx_abort(tx); 2845 return; 2846 } 2847 object = zap_create(os, DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx); 2848 if (error) { 2849 fatal(0, "zap_create('%s', %llu) = %d", 2850 osname, object, error); 2851 } 2852 ASSERT(object != 0); 2853 dmu_write(os, ZTEST_DIROBJ, za->za_diroff, 2854 sizeof (uint64_t), &object, tx); 2855 /* 2856 * Generate a known hash collision, and verify that 2857 * we can lookup and remove both entries. 2858 */ 2859 for (i = 0; i < 2; i++) { 2860 value[i] = i; 2861 error = zap_add(os, object, hc[i], sizeof (uint64_t), 2862 1, &value[i], tx); 2863 ASSERT3U(error, ==, 0); 2864 } 2865 for (i = 0; i < 2; i++) { 2866 error = zap_add(os, object, hc[i], sizeof (uint64_t), 2867 1, &value[i], tx); 2868 ASSERT3U(error, ==, EEXIST); 2869 error = zap_length(os, object, hc[i], 2870 &zl_intsize, &zl_ints); 2871 ASSERT3U(error, ==, 0); 2872 ASSERT3U(zl_intsize, ==, sizeof (uint64_t)); 2873 ASSERT3U(zl_ints, ==, 1); 2874 } 2875 for (i = 0; i < 2; i++) { 2876 error = zap_remove(os, object, hc[i], tx); 2877 ASSERT3U(error, ==, 0); 2878 } 2879 2880 dmu_tx_commit(tx); 2881 } 2882 2883 ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS); 2884 2885 prop = ztest_random(ZTEST_ZAP_MAX_PROPS); 2886 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop); 2887 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop); 2888 bzero(value, sizeof (value)); 2889 last_txg = 0; 2890 2891 /* 2892 * If these zap entries already exist, validate their contents. 2893 */ 2894 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints); 2895 if (error == 0) { 2896 ASSERT3U(zl_intsize, ==, sizeof (uint64_t)); 2897 ASSERT3U(zl_ints, ==, 1); 2898 2899 VERIFY(zap_lookup(os, object, txgname, zl_intsize, 2900 zl_ints, &last_txg) == 0); 2901 2902 VERIFY(zap_length(os, object, propname, &zl_intsize, 2903 &zl_ints) == 0); 2904 2905 ASSERT3U(zl_intsize, ==, sizeof (uint64_t)); 2906 ASSERT3U(zl_ints, ==, ints); 2907 2908 VERIFY(zap_lookup(os, object, propname, zl_intsize, 2909 zl_ints, value) == 0); 2910 2911 for (i = 0; i < ints; i++) { 2912 ASSERT3U(value[i], ==, last_txg + object + i); 2913 } 2914 } else { 2915 ASSERT3U(error, ==, ENOENT); 2916 } 2917 2918 /* 2919 * Atomically update two entries in our zap object. 2920 * The first is named txg_%llu, and contains the txg 2921 * in which the property was last updated. The second 2922 * is named prop_%llu, and the nth element of its value 2923 * should be txg + object + n. 2924 */ 2925 tx = dmu_tx_create(os); 2926 dmu_tx_hold_zap(tx, object, TRUE, NULL); 2927 error = dmu_tx_assign(tx, TXG_WAIT); 2928 if (error) { 2929 ztest_record_enospc("create zap entry"); 2930 dmu_tx_abort(tx); 2931 return; 2932 } 2933 txg = dmu_tx_get_txg(tx); 2934 2935 if (last_txg > txg) 2936 fatal(0, "zap future leak: old %llu new %llu", last_txg, txg); 2937 2938 for (i = 0; i < ints; i++) 2939 value[i] = txg + object + i; 2940 2941 error = zap_update(os, object, txgname, sizeof (uint64_t), 1, &txg, tx); 2942 if (error) 2943 fatal(0, "zap_update('%s', %llu, '%s') = %d", 2944 osname, object, txgname, error); 2945 2946 error = zap_update(os, object, propname, sizeof (uint64_t), 2947 ints, value, tx); 2948 if (error) 2949 fatal(0, "zap_update('%s', %llu, '%s') = %d", 2950 osname, object, propname, error); 2951 2952 dmu_tx_commit(tx); 2953 2954 /* 2955 * Remove a random pair of entries. 2956 */ 2957 prop = ztest_random(ZTEST_ZAP_MAX_PROPS); 2958 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop); 2959 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop); 2960 2961 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints); 2962 2963 if (error == ENOENT) 2964 return; 2965 2966 ASSERT3U(error, ==, 0); 2967 2968 tx = dmu_tx_create(os); 2969 dmu_tx_hold_zap(tx, object, TRUE, NULL); 2970 error = dmu_tx_assign(tx, TXG_WAIT); 2971 if (error) { 2972 ztest_record_enospc("remove zap entry"); 2973 dmu_tx_abort(tx); 2974 return; 2975 } 2976 error = zap_remove(os, object, txgname, tx); 2977 if (error) 2978 fatal(0, "zap_remove('%s', %llu, '%s') = %d", 2979 osname, object, txgname, error); 2980 2981 error = zap_remove(os, object, propname, tx); 2982 if (error) 2983 fatal(0, "zap_remove('%s', %llu, '%s') = %d", 2984 osname, object, propname, error); 2985 2986 dmu_tx_commit(tx); 2987 2988 /* 2989 * Once in a while, destroy the object. 2990 */ 2991 if (ztest_random(1000) != 0) 2992 return; 2993 2994 tx = dmu_tx_create(os); 2995 dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, sizeof (uint64_t)); 2996 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END); 2997 error = dmu_tx_assign(tx, TXG_WAIT); 2998 if (error) { 2999 ztest_record_enospc("destroy zap object"); 3000 dmu_tx_abort(tx); 3001 return; 3002 } 3003 error = zap_destroy(os, object, tx); 3004 if (error) 3005 fatal(0, "zap_destroy('%s', %llu) = %d", 3006 osname, object, error); 3007 object = 0; 3008 dmu_write(os, ZTEST_DIROBJ, za->za_diroff, sizeof (uint64_t), 3009 &object, tx); 3010 dmu_tx_commit(tx); 3011 } 3012 3013 /* 3014 * Testcase to test the upgrading of a microzap to fatzap. 3015 */ 3016 void 3017 ztest_fzap(ztest_args_t *za) 3018 { 3019 objset_t *os = za->za_os; 3020 uint64_t object; 3021 uint64_t value; 3022 dmu_tx_t *tx; 3023 int i, error; 3024 char osname[MAXNAMELEN]; 3025 char *name = "aaa"; 3026 char entname[MAXNAMELEN]; 3027 3028 dmu_objset_name(os, osname); 3029 3030 /* 3031 * Create a new object if necessary, and record it in the directory. 3032 */ 3033 VERIFY(0 == dmu_read(os, ZTEST_DIROBJ, za->za_diroff, 3034 sizeof (uint64_t), &object, DMU_READ_PREFETCH)); 3035 3036 if (object == 0) { 3037 tx = dmu_tx_create(os); 3038 dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, 3039 sizeof (uint64_t)); 3040 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, NULL); 3041 error = dmu_tx_assign(tx, TXG_WAIT); 3042 if (error) { 3043 ztest_record_enospc("create zap test obj"); 3044 dmu_tx_abort(tx); 3045 return; 3046 } 3047 object = zap_create(os, DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx); 3048 if (error) { 3049 fatal(0, "zap_create('%s', %llu) = %d", 3050 osname, object, error); 3051 } 3052 ASSERT(object != 0); 3053 dmu_write(os, ZTEST_DIROBJ, za->za_diroff, 3054 sizeof (uint64_t), &object, tx); 3055 dmu_tx_commit(tx); 3056 } 3057 3058 /* 3059 * Add entries to this ZAP amd make sure it spills over 3060 * and gets upgraded to a fatzap. Also, since we are adding 3061 * 2050 entries we should see ptrtbl growth and leaf-block 3062 * split. 3063 */ 3064 for (i = 0; i < 2050; i++) { 3065 (void) snprintf(entname, sizeof (entname), "%s-%d", name, i); 3066 value = i; 3067 3068 tx = dmu_tx_create(os); 3069 dmu_tx_hold_zap(tx, object, TRUE, entname); 3070 error = dmu_tx_assign(tx, TXG_WAIT); 3071 3072 if (error) { 3073 ztest_record_enospc("create zap entry"); 3074 dmu_tx_abort(tx); 3075 return; 3076 } 3077 error = zap_add(os, object, entname, sizeof (uint64_t), 3078 1, &value, tx); 3079 3080 ASSERT(error == 0 || error == EEXIST); 3081 dmu_tx_commit(tx); 3082 } 3083 3084 /* 3085 * Once in a while, destroy the object. 3086 */ 3087 if (ztest_random(1000) != 0) 3088 return; 3089 3090 tx = dmu_tx_create(os); 3091 dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, sizeof (uint64_t)); 3092 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END); 3093 error = dmu_tx_assign(tx, TXG_WAIT); 3094 if (error) { 3095 ztest_record_enospc("destroy zap object"); 3096 dmu_tx_abort(tx); 3097 return; 3098 } 3099 error = zap_destroy(os, object, tx); 3100 if (error) 3101 fatal(0, "zap_destroy('%s', %llu) = %d", 3102 osname, object, error); 3103 object = 0; 3104 dmu_write(os, ZTEST_DIROBJ, za->za_diroff, sizeof (uint64_t), 3105 &object, tx); 3106 dmu_tx_commit(tx); 3107 } 3108 3109 void 3110 ztest_zap_parallel(ztest_args_t *za) 3111 { 3112 objset_t *os = za->za_os; 3113 uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc; 3114 dmu_tx_t *tx; 3115 int i, namelen, error; 3116 char name[20], string_value[20]; 3117 void *data; 3118 3119 /* 3120 * Generate a random name of the form 'xxx.....' where each 3121 * x is a random printable character and the dots are dots. 3122 * There are 94 such characters, and the name length goes from 3123 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names. 3124 */ 3125 namelen = ztest_random(sizeof (name) - 5) + 5 + 1; 3126 3127 for (i = 0; i < 3; i++) 3128 name[i] = '!' + ztest_random('~' - '!' + 1); 3129 for (; i < namelen - 1; i++) 3130 name[i] = '.'; 3131 name[i] = '\0'; 3132 3133 if (ztest_random(2) == 0) 3134 object = ZTEST_MICROZAP_OBJ; 3135 else 3136 object = ZTEST_FATZAP_OBJ; 3137 3138 if ((namelen & 1) || object == ZTEST_MICROZAP_OBJ) { 3139 wsize = sizeof (txg); 3140 wc = 1; 3141 data = &txg; 3142 } else { 3143 wsize = 1; 3144 wc = namelen; 3145 data = string_value; 3146 } 3147 3148 count = -1ULL; 3149 VERIFY(zap_count(os, object, &count) == 0); 3150 ASSERT(count != -1ULL); 3151 3152 /* 3153 * Select an operation: length, lookup, add, update, remove. 3154 */ 3155 i = ztest_random(5); 3156 3157 if (i >= 2) { 3158 tx = dmu_tx_create(os); 3159 dmu_tx_hold_zap(tx, object, TRUE, NULL); 3160 error = dmu_tx_assign(tx, TXG_WAIT); 3161 if (error) { 3162 ztest_record_enospc("zap parallel"); 3163 dmu_tx_abort(tx); 3164 return; 3165 } 3166 txg = dmu_tx_get_txg(tx); 3167 bcopy(name, string_value, namelen); 3168 } else { 3169 tx = NULL; 3170 txg = 0; 3171 bzero(string_value, namelen); 3172 } 3173 3174 switch (i) { 3175 3176 case 0: 3177 error = zap_length(os, object, name, &zl_wsize, &zl_wc); 3178 if (error == 0) { 3179 ASSERT3U(wsize, ==, zl_wsize); 3180 ASSERT3U(wc, ==, zl_wc); 3181 } else { 3182 ASSERT3U(error, ==, ENOENT); 3183 } 3184 break; 3185 3186 case 1: 3187 error = zap_lookup(os, object, name, wsize, wc, data); 3188 if (error == 0) { 3189 if (data == string_value && 3190 bcmp(name, data, namelen) != 0) 3191 fatal(0, "name '%s' != val '%s' len %d", 3192 name, data, namelen); 3193 } else { 3194 ASSERT3U(error, ==, ENOENT); 3195 } 3196 break; 3197 3198 case 2: 3199 error = zap_add(os, object, name, wsize, wc, data, tx); 3200 ASSERT(error == 0 || error == EEXIST); 3201 break; 3202 3203 case 3: 3204 VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0); 3205 break; 3206 3207 case 4: 3208 error = zap_remove(os, object, name, tx); 3209 ASSERT(error == 0 || error == ENOENT); 3210 break; 3211 } 3212 3213 if (tx != NULL) 3214 dmu_tx_commit(tx); 3215 } 3216 3217 /* 3218 * Commit callback data. 3219 */ 3220 typedef struct ztest_cb_data { 3221 list_node_t zcd_node; 3222 uint64_t zcd_txg; 3223 int zcd_expected_err; 3224 boolean_t zcd_added; 3225 boolean_t zcd_called; 3226 spa_t *zcd_spa; 3227 } ztest_cb_data_t; 3228 3229 /* This is the actual commit callback function */ 3230 static void 3231 ztest_commit_callback(void *arg, int error) 3232 { 3233 ztest_cb_data_t *data = arg; 3234 uint64_t synced_txg; 3235 3236 VERIFY(data != NULL); 3237 VERIFY3S(data->zcd_expected_err, ==, error); 3238 VERIFY(!data->zcd_called); 3239 3240 synced_txg = spa_last_synced_txg(data->zcd_spa); 3241 if (data->zcd_txg > synced_txg) 3242 fatal(0, "commit callback of txg %" PRIu64 " called prematurely" 3243 ", last synced txg = %" PRIu64 "\n", data->zcd_txg, 3244 synced_txg); 3245 3246 data->zcd_called = B_TRUE; 3247 3248 if (error == ECANCELED) { 3249 ASSERT3U(data->zcd_txg, ==, 0); 3250 ASSERT(!data->zcd_added); 3251 3252 /* 3253 * The private callback data should be destroyed here, but 3254 * since we are going to check the zcd_called field after 3255 * dmu_tx_abort(), we will destroy it there. 3256 */ 3257 return; 3258 } 3259 3260 /* Was this callback added to the global callback list? */ 3261 if (!data->zcd_added) 3262 goto out; 3263 3264 ASSERT3U(data->zcd_txg, !=, 0); 3265 3266 /* Remove our callback from the list */ 3267 (void) mutex_lock(&zcl.zcl_callbacks_lock); 3268 list_remove(&zcl.zcl_callbacks, data); 3269 (void) mutex_unlock(&zcl.zcl_callbacks_lock); 3270 3271 out: 3272 umem_free(data, sizeof (ztest_cb_data_t)); 3273 } 3274 3275 /* Allocate and initialize callback data structure */ 3276 static ztest_cb_data_t * 3277 ztest_create_cb_data(objset_t *os, uint64_t txg) 3278 { 3279 ztest_cb_data_t *cb_data; 3280 3281 cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL); 3282 3283 cb_data->zcd_txg = txg; 3284 cb_data->zcd_spa = dmu_objset_spa(os); 3285 3286 return (cb_data); 3287 } 3288 3289 /* 3290 * If a number of txgs equal to this threshold have been created after a commit 3291 * callback has been registered but not called, then we assume there is an 3292 * implementation bug. 3293 */ 3294 #define ZTEST_COMMIT_CALLBACK_THRESH (TXG_CONCURRENT_STATES + 2) 3295 3296 /* 3297 * Commit callback test. 3298 */ 3299 void 3300 ztest_dmu_commit_callbacks(ztest_args_t *za) 3301 { 3302 objset_t *os = za->za_os; 3303 dmu_tx_t *tx; 3304 ztest_cb_data_t *cb_data[3], *tmp_cb; 3305 uint64_t old_txg, txg; 3306 int i, error; 3307 3308 tx = dmu_tx_create(os); 3309 3310 cb_data[0] = ztest_create_cb_data(os, 0); 3311 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]); 3312 3313 dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, sizeof (uint64_t)); 3314 3315 /* Every once in a while, abort the transaction on purpose */ 3316 if (ztest_random(100) == 0) 3317 error = -1; 3318 3319 if (!error) 3320 error = dmu_tx_assign(tx, TXG_NOWAIT); 3321 3322 txg = error ? 0 : dmu_tx_get_txg(tx); 3323 3324 cb_data[0]->zcd_txg = txg; 3325 cb_data[1] = ztest_create_cb_data(os, txg); 3326 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]); 3327 3328 if (error) { 3329 /* 3330 * It's not a strict requirement to call the registered 3331 * callbacks from inside dmu_tx_abort(), but that's what 3332 * it's supposed to happen in the current implementation 3333 * so we will check for that. 3334 */ 3335 for (i = 0; i < 2; i++) { 3336 cb_data[i]->zcd_expected_err = ECANCELED; 3337 VERIFY(!cb_data[i]->zcd_called); 3338 } 3339 3340 dmu_tx_abort(tx); 3341 3342 for (i = 0; i < 2; i++) { 3343 VERIFY(cb_data[i]->zcd_called); 3344 umem_free(cb_data[i], sizeof (ztest_cb_data_t)); 3345 } 3346 3347 return; 3348 } 3349 3350 cb_data[2] = ztest_create_cb_data(os, txg); 3351 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]); 3352 3353 /* 3354 * Read existing data to make sure there isn't a future leak. 3355 */ 3356 VERIFY(0 == dmu_read(os, ZTEST_DIROBJ, za->za_diroff, sizeof (uint64_t), 3357 &old_txg, DMU_READ_PREFETCH)); 3358 3359 if (old_txg > txg) 3360 fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64, 3361 old_txg, txg); 3362 3363 dmu_write(os, ZTEST_DIROBJ, za->za_diroff, sizeof (uint64_t), &txg, tx); 3364 3365 (void) mutex_lock(&zcl.zcl_callbacks_lock); 3366 3367 /* 3368 * Since commit callbacks don't have any ordering requirement and since 3369 * it is theoretically possible for a commit callback to be called 3370 * after an arbitrary amount of time has elapsed since its txg has been 3371 * synced, it is difficult to reliably determine whether a commit 3372 * callback hasn't been called due to high load or due to a flawed 3373 * implementation. 3374 * 3375 * In practice, we will assume that if after a certain number of txgs a 3376 * commit callback hasn't been called, then most likely there's an 3377 * implementation bug.. 3378 */ 3379 tmp_cb = list_head(&zcl.zcl_callbacks); 3380 if (tmp_cb != NULL && 3381 tmp_cb->zcd_txg > txg - ZTEST_COMMIT_CALLBACK_THRESH) { 3382 fatal(0, "Commit callback threshold exceeded, oldest txg: %" 3383 PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg); 3384 } 3385 3386 /* 3387 * Let's find the place to insert our callbacks. 3388 * 3389 * Even though the list is ordered by txg, it is possible for the 3390 * insertion point to not be the end because our txg may already be 3391 * quiescing at this point and other callbacks in the open txg 3392 * (from other objsets) may have sneaked in. 3393 */ 3394 tmp_cb = list_tail(&zcl.zcl_callbacks); 3395 while (tmp_cb != NULL && tmp_cb->zcd_txg > txg) 3396 tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb); 3397 3398 /* Add the 3 callbacks to the list */ 3399 for (i = 0; i < 3; i++) { 3400 if (tmp_cb == NULL) 3401 list_insert_head(&zcl.zcl_callbacks, cb_data[i]); 3402 else 3403 list_insert_after(&zcl.zcl_callbacks, tmp_cb, 3404 cb_data[i]); 3405 3406 cb_data[i]->zcd_added = B_TRUE; 3407 VERIFY(!cb_data[i]->zcd_called); 3408 3409 tmp_cb = cb_data[i]; 3410 } 3411 3412 (void) mutex_unlock(&zcl.zcl_callbacks_lock); 3413 3414 dmu_tx_commit(tx); 3415 } 3416 3417 void 3418 ztest_dsl_prop_get_set(ztest_args_t *za) 3419 { 3420 objset_t *os = za->za_os; 3421 int i, inherit; 3422 uint64_t value; 3423 const char *prop, *valname; 3424 char setpoint[MAXPATHLEN]; 3425 char osname[MAXNAMELEN]; 3426 int error; 3427 3428 (void) rw_rdlock(&ztest_shared->zs_name_lock); 3429 3430 dmu_objset_name(os, osname); 3431 3432 for (i = 0; i < 2; i++) { 3433 if (i == 0) { 3434 prop = "checksum"; 3435 value = ztest_random_checksum(); 3436 inherit = (value == ZIO_CHECKSUM_INHERIT); 3437 } else { 3438 prop = "compression"; 3439 value = ztest_random_compress(); 3440 inherit = (value == ZIO_COMPRESS_INHERIT); 3441 } 3442 3443 error = dsl_prop_set(osname, prop, sizeof (value), 3444 !inherit, &value); 3445 3446 if (error == ENOSPC) { 3447 ztest_record_enospc("dsl_prop_set"); 3448 break; 3449 } 3450 3451 ASSERT3U(error, ==, 0); 3452 3453 VERIFY3U(dsl_prop_get(osname, prop, sizeof (value), 3454 1, &value, setpoint), ==, 0); 3455 3456 if (i == 0) 3457 valname = zio_checksum_table[value].ci_name; 3458 else 3459 valname = zio_compress_table[value].ci_name; 3460 3461 if (zopt_verbose >= 6) { 3462 (void) printf("%s %s = %s for '%s'\n", 3463 osname, prop, valname, setpoint); 3464 } 3465 } 3466 3467 (void) rw_unlock(&ztest_shared->zs_name_lock); 3468 } 3469 3470 /* 3471 * Inject random faults into the on-disk data. 3472 */ 3473 void 3474 ztest_fault_inject(ztest_args_t *za) 3475 { 3476 int fd; 3477 uint64_t offset; 3478 uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz; 3479 uint64_t bad = 0x1990c0ffeedecade; 3480 uint64_t top, leaf; 3481 char path0[MAXPATHLEN]; 3482 char pathrand[MAXPATHLEN]; 3483 size_t fsize; 3484 spa_t *spa = za->za_spa; 3485 int bshift = SPA_MAXBLOCKSHIFT + 2; /* don't scrog all labels */ 3486 int iters = 1000; 3487 int maxfaults = zopt_maxfaults; 3488 vdev_t *vd0 = NULL; 3489 uint64_t guid0 = 0; 3490 boolean_t islog = B_FALSE; 3491 3492 ASSERT(leaves >= 1); 3493 3494 /* 3495 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd. 3496 */ 3497 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 3498 3499 if (ztest_random(2) == 0) { 3500 /* 3501 * Inject errors on a normal data device. 3502 */ 3503 top = ztest_random(spa->spa_root_vdev->vdev_children); 3504 leaf = ztest_random(leaves); 3505 3506 /* 3507 * Generate paths to the first leaf in this top-level vdev, 3508 * and to the random leaf we selected. We'll induce transient 3509 * write failures and random online/offline activity on leaf 0, 3510 * and we'll write random garbage to the randomly chosen leaf. 3511 */ 3512 (void) snprintf(path0, sizeof (path0), ztest_dev_template, 3513 zopt_dir, zopt_pool, top * leaves + 0); 3514 (void) snprintf(pathrand, sizeof (pathrand), ztest_dev_template, 3515 zopt_dir, zopt_pool, top * leaves + leaf); 3516 3517 vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0); 3518 if (vd0 != NULL && vd0->vdev_top->vdev_islog) 3519 islog = B_TRUE; 3520 3521 if (vd0 != NULL && maxfaults != 1) { 3522 /* 3523 * Make vd0 explicitly claim to be unreadable, 3524 * or unwriteable, or reach behind its back 3525 * and close the underlying fd. We can do this if 3526 * maxfaults == 0 because we'll fail and reexecute, 3527 * and we can do it if maxfaults >= 2 because we'll 3528 * have enough redundancy. If maxfaults == 1, the 3529 * combination of this with injection of random data 3530 * corruption below exceeds the pool's fault tolerance. 3531 */ 3532 vdev_file_t *vf = vd0->vdev_tsd; 3533 3534 if (vf != NULL && ztest_random(3) == 0) { 3535 (void) close(vf->vf_vnode->v_fd); 3536 vf->vf_vnode->v_fd = -1; 3537 } else if (ztest_random(2) == 0) { 3538 vd0->vdev_cant_read = B_TRUE; 3539 } else { 3540 vd0->vdev_cant_write = B_TRUE; 3541 } 3542 guid0 = vd0->vdev_guid; 3543 } 3544 } else { 3545 /* 3546 * Inject errors on an l2cache device. 3547 */ 3548 spa_aux_vdev_t *sav = &spa->spa_l2cache; 3549 3550 if (sav->sav_count == 0) { 3551 spa_config_exit(spa, SCL_STATE, FTAG); 3552 return; 3553 } 3554 vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)]; 3555 guid0 = vd0->vdev_guid; 3556 (void) strcpy(path0, vd0->vdev_path); 3557 (void) strcpy(pathrand, vd0->vdev_path); 3558 3559 leaf = 0; 3560 leaves = 1; 3561 maxfaults = INT_MAX; /* no limit on cache devices */ 3562 } 3563 3564 spa_config_exit(spa, SCL_STATE, FTAG); 3565 3566 /* 3567 * If we can tolerate two or more faults, or we're dealing 3568 * with a slog, randomly online/offline vd0. 3569 */ 3570 if ((maxfaults >= 2 || islog) && guid0 != 0) { 3571 if (ztest_random(10) < 6) { 3572 int flags = (ztest_random(2) == 0 ? 3573 ZFS_OFFLINE_TEMPORARY : 0); 3574 3575 /* 3576 * We have to grab the zs_name_lock as writer to 3577 * prevent a race between offlining a slog and 3578 * destroying a dataset. Offlining the slog will 3579 * grab a reference on the dataset which may cause 3580 * dmu_objset_destroy() to fail with EBUSY thus 3581 * leaving the dataset in an inconsistent state. 3582 */ 3583 if (islog) 3584 (void) rw_wrlock(&ztest_shared->zs_name_lock); 3585 3586 VERIFY(vdev_offline(spa, guid0, flags) != EBUSY); 3587 3588 if (islog) 3589 (void) rw_unlock(&ztest_shared->zs_name_lock); 3590 } else { 3591 (void) vdev_online(spa, guid0, 0, NULL); 3592 } 3593 } 3594 3595 if (maxfaults == 0) 3596 return; 3597 3598 /* 3599 * We have at least single-fault tolerance, so inject data corruption. 3600 */ 3601 fd = open(pathrand, O_RDWR); 3602 3603 if (fd == -1) /* we hit a gap in the device namespace */ 3604 return; 3605 3606 fsize = lseek(fd, 0, SEEK_END); 3607 3608 while (--iters != 0) { 3609 offset = ztest_random(fsize / (leaves << bshift)) * 3610 (leaves << bshift) + (leaf << bshift) + 3611 (ztest_random(1ULL << (bshift - 1)) & -8ULL); 3612 3613 if (offset >= fsize) 3614 continue; 3615 3616 if (zopt_verbose >= 6) 3617 (void) printf("injecting bad word into %s," 3618 " offset 0x%llx\n", pathrand, (u_longlong_t)offset); 3619 3620 if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad)) 3621 fatal(1, "can't inject bad word at 0x%llx in %s", 3622 offset, pathrand); 3623 } 3624 3625 (void) close(fd); 3626 } 3627 3628 /* 3629 * Scrub the pool. 3630 */ 3631 void 3632 ztest_scrub(ztest_args_t *za) 3633 { 3634 spa_t *spa = za->za_spa; 3635 3636 (void) spa_scrub(spa, POOL_SCRUB_EVERYTHING); 3637 (void) poll(NULL, 0, 1000); /* wait a second, then force a restart */ 3638 (void) spa_scrub(spa, POOL_SCRUB_EVERYTHING); 3639 } 3640 3641 /* 3642 * Rename the pool to a different name and then rename it back. 3643 */ 3644 void 3645 ztest_spa_rename(ztest_args_t *za) 3646 { 3647 char *oldname, *newname; 3648 int error; 3649 spa_t *spa; 3650 3651 (void) rw_wrlock(&ztest_shared->zs_name_lock); 3652 3653 oldname = za->za_pool; 3654 newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL); 3655 (void) strcpy(newname, oldname); 3656 (void) strcat(newname, "_tmp"); 3657 3658 /* 3659 * Do the rename 3660 */ 3661 error = spa_rename(oldname, newname); 3662 if (error) 3663 fatal(0, "spa_rename('%s', '%s') = %d", oldname, 3664 newname, error); 3665 3666 /* 3667 * Try to open it under the old name, which shouldn't exist 3668 */ 3669 error = spa_open(oldname, &spa, FTAG); 3670 if (error != ENOENT) 3671 fatal(0, "spa_open('%s') = %d", oldname, error); 3672 3673 /* 3674 * Open it under the new name and make sure it's still the same spa_t. 3675 */ 3676 error = spa_open(newname, &spa, FTAG); 3677 if (error != 0) 3678 fatal(0, "spa_open('%s') = %d", newname, error); 3679 3680 ASSERT(spa == za->za_spa); 3681 spa_close(spa, FTAG); 3682 3683 /* 3684 * Rename it back to the original 3685 */ 3686 error = spa_rename(newname, oldname); 3687 if (error) 3688 fatal(0, "spa_rename('%s', '%s') = %d", newname, 3689 oldname, error); 3690 3691 /* 3692 * Make sure it can still be opened 3693 */ 3694 error = spa_open(oldname, &spa, FTAG); 3695 if (error != 0) 3696 fatal(0, "spa_open('%s') = %d", oldname, error); 3697 3698 ASSERT(spa == za->za_spa); 3699 spa_close(spa, FTAG); 3700 3701 umem_free(newname, strlen(newname) + 1); 3702 3703 (void) rw_unlock(&ztest_shared->zs_name_lock); 3704 } 3705 3706 3707 /* 3708 * Completely obliterate one disk. 3709 */ 3710 static void 3711 ztest_obliterate_one_disk(uint64_t vdev) 3712 { 3713 int fd; 3714 char dev_name[MAXPATHLEN], copy_name[MAXPATHLEN]; 3715 size_t fsize; 3716 3717 if (zopt_maxfaults < 2) 3718 return; 3719 3720 (void) sprintf(dev_name, ztest_dev_template, zopt_dir, zopt_pool, vdev); 3721 (void) snprintf(copy_name, MAXPATHLEN, "%s.old", dev_name); 3722 3723 fd = open(dev_name, O_RDWR); 3724 3725 if (fd == -1) 3726 fatal(1, "can't open %s", dev_name); 3727 3728 /* 3729 * Determine the size. 3730 */ 3731 fsize = lseek(fd, 0, SEEK_END); 3732 3733 (void) close(fd); 3734 3735 /* 3736 * Rename the old device to dev_name.old (useful for debugging). 3737 */ 3738 VERIFY(rename(dev_name, copy_name) == 0); 3739 3740 /* 3741 * Create a new one. 3742 */ 3743 VERIFY((fd = open(dev_name, O_RDWR | O_CREAT | O_TRUNC, 0666)) >= 0); 3744 VERIFY(ftruncate(fd, fsize) == 0); 3745 (void) close(fd); 3746 } 3747 3748 static void 3749 ztest_replace_one_disk(spa_t *spa, uint64_t vdev) 3750 { 3751 char dev_name[MAXPATHLEN]; 3752 nvlist_t *root; 3753 int error; 3754 uint64_t guid; 3755 vdev_t *vd; 3756 3757 (void) sprintf(dev_name, ztest_dev_template, zopt_dir, zopt_pool, vdev); 3758 3759 /* 3760 * Build the nvlist describing dev_name. 3761 */ 3762 root = make_vdev_root(dev_name, NULL, 0, 0, 0, 0, 0, 1); 3763 3764 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); 3765 if ((vd = vdev_lookup_by_path(spa->spa_root_vdev, dev_name)) == NULL) 3766 guid = 0; 3767 else 3768 guid = vd->vdev_guid; 3769 spa_config_exit(spa, SCL_VDEV, FTAG); 3770 error = spa_vdev_attach(spa, guid, root, B_TRUE); 3771 if (error != 0 && 3772 error != EBUSY && 3773 error != ENOTSUP && 3774 error != ENODEV && 3775 error != EDOM) 3776 fatal(0, "spa_vdev_attach(in-place) = %d", error); 3777 3778 nvlist_free(root); 3779 } 3780 3781 static void 3782 ztest_verify_blocks(char *pool) 3783 { 3784 int status; 3785 char zdb[MAXPATHLEN + MAXNAMELEN + 20]; 3786 char zbuf[1024]; 3787 char *bin; 3788 char *ztest; 3789 char *isa; 3790 int isalen; 3791 FILE *fp; 3792 3793 (void) realpath(getexecname(), zdb); 3794 3795 /* zdb lives in /usr/sbin, while ztest lives in /usr/bin */ 3796 bin = strstr(zdb, "/usr/bin/"); 3797 ztest = strstr(bin, "/ztest"); 3798 isa = bin + 8; 3799 isalen = ztest - isa; 3800 isa = strdup(isa); 3801 /* LINTED */ 3802 (void) sprintf(bin, 3803 "/usr/sbin%.*s/zdb -bcc%s%s -U /tmp/zpool.cache %s", 3804 isalen, 3805 isa, 3806 zopt_verbose >= 3 ? "s" : "", 3807 zopt_verbose >= 4 ? "v" : "", 3808 pool); 3809 free(isa); 3810 3811 if (zopt_verbose >= 5) 3812 (void) printf("Executing %s\n", strstr(zdb, "zdb ")); 3813 3814 fp = popen(zdb, "r"); 3815 3816 while (fgets(zbuf, sizeof (zbuf), fp) != NULL) 3817 if (zopt_verbose >= 3) 3818 (void) printf("%s", zbuf); 3819 3820 status = pclose(fp); 3821 3822 if (status == 0) 3823 return; 3824 3825 ztest_dump_core = 0; 3826 if (WIFEXITED(status)) 3827 fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status)); 3828 else 3829 fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status)); 3830 } 3831 3832 static void 3833 ztest_walk_pool_directory(char *header) 3834 { 3835 spa_t *spa = NULL; 3836 3837 if (zopt_verbose >= 6) 3838 (void) printf("%s\n", header); 3839 3840 mutex_enter(&spa_namespace_lock); 3841 while ((spa = spa_next(spa)) != NULL) 3842 if (zopt_verbose >= 6) 3843 (void) printf("\t%s\n", spa_name(spa)); 3844 mutex_exit(&spa_namespace_lock); 3845 } 3846 3847 static void 3848 ztest_spa_import_export(char *oldname, char *newname) 3849 { 3850 nvlist_t *config, *newconfig; 3851 uint64_t pool_guid; 3852 spa_t *spa; 3853 int error; 3854 3855 if (zopt_verbose >= 4) { 3856 (void) printf("import/export: old = %s, new = %s\n", 3857 oldname, newname); 3858 } 3859 3860 /* 3861 * Clean up from previous runs. 3862 */ 3863 (void) spa_destroy(newname); 3864 3865 /* 3866 * Get the pool's configuration and guid. 3867 */ 3868 error = spa_open(oldname, &spa, FTAG); 3869 if (error) 3870 fatal(0, "spa_open('%s') = %d", oldname, error); 3871 3872 /* 3873 * Kick off a scrub to tickle scrub/export races. 3874 */ 3875 if (ztest_random(2) == 0) 3876 (void) spa_scrub(spa, POOL_SCRUB_EVERYTHING); 3877 3878 pool_guid = spa_guid(spa); 3879 spa_close(spa, FTAG); 3880 3881 ztest_walk_pool_directory("pools before export"); 3882 3883 /* 3884 * Export it. 3885 */ 3886 error = spa_export(oldname, &config, B_FALSE, B_FALSE); 3887 if (error) 3888 fatal(0, "spa_export('%s') = %d", oldname, error); 3889 3890 ztest_walk_pool_directory("pools after export"); 3891 3892 /* 3893 * Try to import it. 3894 */ 3895 newconfig = spa_tryimport(config); 3896 ASSERT(newconfig != NULL); 3897 nvlist_free(newconfig); 3898 3899 /* 3900 * Import it under the new name. 3901 */ 3902 error = spa_import(newname, config, NULL); 3903 if (error) 3904 fatal(0, "spa_import('%s') = %d", newname, error); 3905 3906 ztest_walk_pool_directory("pools after import"); 3907 3908 /* 3909 * Try to import it again -- should fail with EEXIST. 3910 */ 3911 error = spa_import(newname, config, NULL); 3912 if (error != EEXIST) 3913 fatal(0, "spa_import('%s') twice", newname); 3914 3915 /* 3916 * Try to import it under a different name -- should fail with EEXIST. 3917 */ 3918 error = spa_import(oldname, config, NULL); 3919 if (error != EEXIST) 3920 fatal(0, "spa_import('%s') under multiple names", newname); 3921 3922 /* 3923 * Verify that the pool is no longer visible under the old name. 3924 */ 3925 error = spa_open(oldname, &spa, FTAG); 3926 if (error != ENOENT) 3927 fatal(0, "spa_open('%s') = %d", newname, error); 3928 3929 /* 3930 * Verify that we can open and close the pool using the new name. 3931 */ 3932 error = spa_open(newname, &spa, FTAG); 3933 if (error) 3934 fatal(0, "spa_open('%s') = %d", newname, error); 3935 ASSERT(pool_guid == spa_guid(spa)); 3936 spa_close(spa, FTAG); 3937 3938 nvlist_free(config); 3939 } 3940 3941 static void 3942 ztest_resume(spa_t *spa) 3943 { 3944 if (spa_suspended(spa)) { 3945 spa_vdev_state_enter(spa, SCL_NONE); 3946 vdev_clear(spa, NULL); 3947 (void) spa_vdev_state_exit(spa, NULL, 0); 3948 (void) zio_resume(spa); 3949 } 3950 } 3951 3952 static void * 3953 ztest_resume_thread(void *arg) 3954 { 3955 spa_t *spa = arg; 3956 3957 while (!ztest_exiting) { 3958 (void) poll(NULL, 0, 1000); 3959 ztest_resume(spa); 3960 } 3961 return (NULL); 3962 } 3963 3964 static void * 3965 ztest_thread(void *arg) 3966 { 3967 ztest_args_t *za = arg; 3968 ztest_shared_t *zs = ztest_shared; 3969 hrtime_t now, functime; 3970 ztest_info_t *zi; 3971 int f, i; 3972 3973 while ((now = gethrtime()) < za->za_stop) { 3974 /* 3975 * See if it's time to force a crash. 3976 */ 3977 if (now > za->za_kill) { 3978 zs->zs_alloc = spa_get_alloc(za->za_spa); 3979 zs->zs_space = spa_get_space(za->za_spa); 3980 (void) kill(getpid(), SIGKILL); 3981 } 3982 3983 /* 3984 * Pick a random function. 3985 */ 3986 f = ztest_random(ZTEST_FUNCS); 3987 zi = &zs->zs_info[f]; 3988 3989 /* 3990 * Decide whether to call it, based on the requested frequency. 3991 */ 3992 if (zi->zi_call_target == 0 || 3993 (double)zi->zi_call_total / zi->zi_call_target > 3994 (double)(now - zs->zs_start_time) / (zopt_time * NANOSEC)) 3995 continue; 3996 3997 atomic_add_64(&zi->zi_calls, 1); 3998 atomic_add_64(&zi->zi_call_total, 1); 3999 4000 za->za_diroff = (za->za_instance * ZTEST_FUNCS + f) * 4001 ZTEST_DIRSIZE; 4002 za->za_diroff_shared = (1ULL << 63); 4003 4004 for (i = 0; i < zi->zi_iters; i++) 4005 zi->zi_func(za); 4006 4007 functime = gethrtime() - now; 4008 4009 atomic_add_64(&zi->zi_call_time, functime); 4010 4011 if (zopt_verbose >= 4) { 4012 Dl_info dli; 4013 (void) dladdr((void *)zi->zi_func, &dli); 4014 (void) printf("%6.2f sec in %s\n", 4015 (double)functime / NANOSEC, dli.dli_sname); 4016 } 4017 4018 /* 4019 * If we're getting ENOSPC with some regularity, stop. 4020 */ 4021 if (zs->zs_enospc_count > 10) 4022 break; 4023 } 4024 4025 return (NULL); 4026 } 4027 4028 /* 4029 * Kick off threads to run tests on all datasets in parallel. 4030 */ 4031 static void 4032 ztest_run(char *pool) 4033 { 4034 int t, d, error; 4035 ztest_shared_t *zs = ztest_shared; 4036 ztest_args_t *za; 4037 spa_t *spa; 4038 char name[100]; 4039 thread_t resume_tid; 4040 4041 ztest_exiting = B_FALSE; 4042 4043 (void) _mutex_init(&zs->zs_vdev_lock, USYNC_THREAD, NULL); 4044 (void) rwlock_init(&zs->zs_name_lock, USYNC_THREAD, NULL); 4045 4046 (void) _mutex_init(&zcl.zcl_callbacks_lock, USYNC_THREAD, 4047 NULL); 4048 4049 list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t), 4050 offsetof(ztest_cb_data_t, zcd_node)); 4051 4052 for (t = 0; t < ZTEST_SYNC_LOCKS; t++) 4053 (void) _mutex_init(&zs->zs_sync_lock[t], USYNC_THREAD, NULL); 4054 4055 /* 4056 * Destroy one disk before we even start. 4057 * It's mirrored, so everything should work just fine. 4058 * This makes us exercise fault handling very early in spa_load(). 4059 */ 4060 ztest_obliterate_one_disk(0); 4061 4062 /* 4063 * Verify that the sum of the sizes of all blocks in the pool 4064 * equals the SPA's allocated space total. 4065 */ 4066 ztest_verify_blocks(pool); 4067 4068 /* 4069 * Kick off a replacement of the disk we just obliterated. 4070 */ 4071 kernel_init(FREAD | FWRITE); 4072 VERIFY(spa_open(pool, &spa, FTAG) == 0); 4073 ztest_replace_one_disk(spa, 0); 4074 if (zopt_verbose >= 5) 4075 show_pool_stats(spa); 4076 spa_close(spa, FTAG); 4077 kernel_fini(); 4078 4079 kernel_init(FREAD | FWRITE); 4080 4081 /* 4082 * Verify that we can export the pool and reimport it under a 4083 * different name. 4084 */ 4085 if (ztest_random(2) == 0) { 4086 (void) snprintf(name, 100, "%s_import", pool); 4087 ztest_spa_import_export(pool, name); 4088 ztest_spa_import_export(name, pool); 4089 } 4090 4091 /* 4092 * Verify that we can loop over all pools. 4093 */ 4094 mutex_enter(&spa_namespace_lock); 4095 for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa)) { 4096 if (zopt_verbose > 3) { 4097 (void) printf("spa_next: found %s\n", spa_name(spa)); 4098 } 4099 } 4100 mutex_exit(&spa_namespace_lock); 4101 4102 /* 4103 * Open our pool. 4104 */ 4105 VERIFY(spa_open(pool, &spa, FTAG) == 0); 4106 4107 /* 4108 * We don't expect the pool to suspend unless maxfaults == 0, 4109 * in which case ztest_fault_inject() temporarily takes away 4110 * the only valid replica. 4111 */ 4112 if (zopt_maxfaults == 0) 4113 spa->spa_failmode = ZIO_FAILURE_MODE_WAIT; 4114 else 4115 spa->spa_failmode = ZIO_FAILURE_MODE_PANIC; 4116 4117 /* 4118 * Create a thread to periodically resume suspended I/O. 4119 */ 4120 VERIFY(thr_create(0, 0, ztest_resume_thread, spa, THR_BOUND, 4121 &resume_tid) == 0); 4122 4123 /* 4124 * Verify that we can safely inquire about about any object, 4125 * whether it's allocated or not. To make it interesting, 4126 * we probe a 5-wide window around each power of two. 4127 * This hits all edge cases, including zero and the max. 4128 */ 4129 for (t = 0; t < 64; t++) { 4130 for (d = -5; d <= 5; d++) { 4131 error = dmu_object_info(spa->spa_meta_objset, 4132 (1ULL << t) + d, NULL); 4133 ASSERT(error == 0 || error == ENOENT || 4134 error == EINVAL); 4135 } 4136 } 4137 4138 /* 4139 * Now kick off all the tests that run in parallel. 4140 */ 4141 zs->zs_enospc_count = 0; 4142 4143 za = umem_zalloc(zopt_threads * sizeof (ztest_args_t), UMEM_NOFAIL); 4144 4145 if (zopt_verbose >= 4) 4146 (void) printf("starting main threads...\n"); 4147 4148 za[0].za_start = gethrtime(); 4149 za[0].za_stop = za[0].za_start + zopt_passtime * NANOSEC; 4150 za[0].za_stop = MIN(za[0].za_stop, zs->zs_stop_time); 4151 za[0].za_kill = za[0].za_stop; 4152 if (ztest_random(100) < zopt_killrate) 4153 za[0].za_kill -= ztest_random(zopt_passtime * NANOSEC); 4154 4155 for (t = 0; t < zopt_threads; t++) { 4156 d = t % zopt_datasets; 4157 4158 (void) strcpy(za[t].za_pool, pool); 4159 za[t].za_os = za[d].za_os; 4160 za[t].za_spa = spa; 4161 za[t].za_zilog = za[d].za_zilog; 4162 za[t].za_instance = t; 4163 za[t].za_random = ztest_random(-1ULL); 4164 za[t].za_start = za[0].za_start; 4165 za[t].za_stop = za[0].za_stop; 4166 za[t].za_kill = za[0].za_kill; 4167 4168 if (t < zopt_datasets) { 4169 int test_future = FALSE; 4170 (void) rw_rdlock(&ztest_shared->zs_name_lock); 4171 (void) snprintf(name, 100, "%s/%s_%d", pool, pool, d); 4172 error = dmu_objset_create(name, DMU_OST_OTHER, 0, 4173 ztest_create_cb, NULL); 4174 if (error == EEXIST) { 4175 test_future = TRUE; 4176 } else if (error == ENOSPC) { 4177 zs->zs_enospc_count++; 4178 (void) rw_unlock(&ztest_shared->zs_name_lock); 4179 break; 4180 } else if (error != 0) { 4181 fatal(0, "dmu_objset_create(%s) = %d", 4182 name, error); 4183 } 4184 error = dmu_objset_hold(name, FTAG, &za[d].za_os); 4185 if (error) 4186 fatal(0, "dmu_objset_open('%s') = %d", 4187 name, error); 4188 (void) rw_unlock(&ztest_shared->zs_name_lock); 4189 if (test_future) 4190 ztest_dmu_check_future_leak(&za[t]); 4191 zil_replay(za[d].za_os, za[d].za_os, 4192 ztest_replay_vector); 4193 za[d].za_zilog = zil_open(za[d].za_os, NULL); 4194 } 4195 4196 VERIFY(thr_create(0, 0, ztest_thread, &za[t], THR_BOUND, 4197 &za[t].za_thread) == 0); 4198 } 4199 4200 while (--t >= 0) { 4201 VERIFY(thr_join(za[t].za_thread, NULL, NULL) == 0); 4202 if (t < zopt_datasets) { 4203 zil_close(za[t].za_zilog); 4204 dmu_objset_rele(za[t].za_os, FTAG); 4205 } 4206 } 4207 4208 if (zopt_verbose >= 3) 4209 show_pool_stats(spa); 4210 4211 txg_wait_synced(spa_get_dsl(spa), 0); 4212 4213 zs->zs_alloc = spa_get_alloc(spa); 4214 zs->zs_space = spa_get_space(spa); 4215 4216 /* 4217 * If we had out-of-space errors, destroy a random objset. 4218 */ 4219 if (zs->zs_enospc_count != 0) { 4220 (void) rw_rdlock(&ztest_shared->zs_name_lock); 4221 d = (int)ztest_random(zopt_datasets); 4222 (void) snprintf(name, 100, "%s/%s_%d", pool, pool, d); 4223 if (zopt_verbose >= 3) 4224 (void) printf("Destroying %s to free up space\n", name); 4225 4226 /* Cleanup any non-standard clones and snapshots */ 4227 ztest_dsl_dataset_cleanup(name, za[d].za_instance); 4228 4229 (void) dmu_objset_find(name, ztest_destroy_cb, &za[d], 4230 DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN); 4231 (void) rw_unlock(&ztest_shared->zs_name_lock); 4232 } 4233 4234 txg_wait_synced(spa_get_dsl(spa), 0); 4235 4236 umem_free(za, zopt_threads * sizeof (ztest_args_t)); 4237 4238 /* Kill the resume thread */ 4239 ztest_exiting = B_TRUE; 4240 VERIFY(thr_join(resume_tid, NULL, NULL) == 0); 4241 ztest_resume(spa); 4242 4243 /* 4244 * Right before closing the pool, kick off a bunch of async I/O; 4245 * spa_close() should wait for it to complete. 4246 */ 4247 for (t = 1; t < 50; t++) 4248 dmu_prefetch(spa->spa_meta_objset, t, 0, 1 << 15); 4249 4250 spa_close(spa, FTAG); 4251 4252 kernel_fini(); 4253 4254 list_destroy(&zcl.zcl_callbacks); 4255 4256 (void) _mutex_destroy(&zcl.zcl_callbacks_lock); 4257 4258 (void) rwlock_destroy(&zs->zs_name_lock); 4259 (void) _mutex_destroy(&zs->zs_vdev_lock); 4260 } 4261 4262 void 4263 print_time(hrtime_t t, char *timebuf) 4264 { 4265 hrtime_t s = t / NANOSEC; 4266 hrtime_t m = s / 60; 4267 hrtime_t h = m / 60; 4268 hrtime_t d = h / 24; 4269 4270 s -= m * 60; 4271 m -= h * 60; 4272 h -= d * 24; 4273 4274 timebuf[0] = '\0'; 4275 4276 if (d) 4277 (void) sprintf(timebuf, 4278 "%llud%02lluh%02llum%02llus", d, h, m, s); 4279 else if (h) 4280 (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s); 4281 else if (m) 4282 (void) sprintf(timebuf, "%llum%02llus", m, s); 4283 else 4284 (void) sprintf(timebuf, "%llus", s); 4285 } 4286 4287 /* 4288 * Create a storage pool with the given name and initial vdev size. 4289 * Then create the specified number of datasets in the pool. 4290 */ 4291 static void 4292 ztest_init(char *pool) 4293 { 4294 spa_t *spa; 4295 int error; 4296 nvlist_t *nvroot; 4297 4298 kernel_init(FREAD | FWRITE); 4299 4300 /* 4301 * Create the storage pool. 4302 */ 4303 (void) spa_destroy(pool); 4304 ztest_shared->zs_vdev_next_leaf = 0; 4305 nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0, 4306 0, zopt_raidz, zopt_mirrors, 1); 4307 error = spa_create(pool, nvroot, NULL, NULL, NULL); 4308 nvlist_free(nvroot); 4309 4310 if (error) 4311 fatal(0, "spa_create() = %d", error); 4312 error = spa_open(pool, &spa, FTAG); 4313 if (error) 4314 fatal(0, "spa_open() = %d", error); 4315 4316 metaslab_sz = 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift; 4317 4318 if (zopt_verbose >= 3) 4319 show_pool_stats(spa); 4320 4321 spa_close(spa, FTAG); 4322 4323 kernel_fini(); 4324 } 4325 4326 int 4327 main(int argc, char **argv) 4328 { 4329 int kills = 0; 4330 int iters = 0; 4331 int i, f; 4332 ztest_shared_t *zs; 4333 ztest_info_t *zi; 4334 char timebuf[100]; 4335 char numbuf[6]; 4336 4337 (void) setvbuf(stdout, NULL, _IOLBF, 0); 4338 4339 /* Override location of zpool.cache */ 4340 spa_config_path = "/tmp/zpool.cache"; 4341 4342 ztest_random_fd = open("/dev/urandom", O_RDONLY); 4343 4344 process_options(argc, argv); 4345 4346 /* 4347 * Blow away any existing copy of zpool.cache 4348 */ 4349 if (zopt_init != 0) 4350 (void) remove("/tmp/zpool.cache"); 4351 4352 zs = ztest_shared = (void *)mmap(0, 4353 P2ROUNDUP(sizeof (ztest_shared_t), getpagesize()), 4354 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0); 4355 4356 if (zopt_verbose >= 1) { 4357 (void) printf("%llu vdevs, %d datasets, %d threads," 4358 " %llu seconds...\n", 4359 (u_longlong_t)zopt_vdevs, zopt_datasets, zopt_threads, 4360 (u_longlong_t)zopt_time); 4361 } 4362 4363 /* 4364 * Create and initialize our storage pool. 4365 */ 4366 for (i = 1; i <= zopt_init; i++) { 4367 bzero(zs, sizeof (ztest_shared_t)); 4368 if (zopt_verbose >= 3 && zopt_init != 1) 4369 (void) printf("ztest_init(), pass %d\n", i); 4370 ztest_init(zopt_pool); 4371 } 4372 4373 /* 4374 * Initialize the call targets for each function. 4375 */ 4376 for (f = 0; f < ZTEST_FUNCS; f++) { 4377 zi = &zs->zs_info[f]; 4378 4379 *zi = ztest_info[f]; 4380 4381 if (*zi->zi_interval == 0) 4382 zi->zi_call_target = UINT64_MAX; 4383 else 4384 zi->zi_call_target = zopt_time / *zi->zi_interval; 4385 } 4386 4387 zs->zs_start_time = gethrtime(); 4388 zs->zs_stop_time = zs->zs_start_time + zopt_time * NANOSEC; 4389 4390 /* 4391 * Run the tests in a loop. These tests include fault injection 4392 * to verify that self-healing data works, and forced crashes 4393 * to verify that we never lose on-disk consistency. 4394 */ 4395 while (gethrtime() < zs->zs_stop_time) { 4396 int status; 4397 pid_t pid; 4398 char *tmp; 4399 4400 /* 4401 * Initialize the workload counters for each function. 4402 */ 4403 for (f = 0; f < ZTEST_FUNCS; f++) { 4404 zi = &zs->zs_info[f]; 4405 zi->zi_calls = 0; 4406 zi->zi_call_time = 0; 4407 } 4408 4409 /* Set the allocation switch size */ 4410 metaslab_df_alloc_threshold = ztest_random(metaslab_sz / 4) + 1; 4411 4412 pid = fork(); 4413 4414 if (pid == -1) 4415 fatal(1, "fork failed"); 4416 4417 if (pid == 0) { /* child */ 4418 struct rlimit rl = { 1024, 1024 }; 4419 (void) setrlimit(RLIMIT_NOFILE, &rl); 4420 (void) enable_extended_FILE_stdio(-1, -1); 4421 ztest_run(zopt_pool); 4422 exit(0); 4423 } 4424 4425 while (waitpid(pid, &status, 0) != pid) 4426 continue; 4427 4428 if (WIFEXITED(status)) { 4429 if (WEXITSTATUS(status) != 0) { 4430 (void) fprintf(stderr, 4431 "child exited with code %d\n", 4432 WEXITSTATUS(status)); 4433 exit(2); 4434 } 4435 } else if (WIFSIGNALED(status)) { 4436 if (WTERMSIG(status) != SIGKILL) { 4437 (void) fprintf(stderr, 4438 "child died with signal %d\n", 4439 WTERMSIG(status)); 4440 exit(3); 4441 } 4442 kills++; 4443 } else { 4444 (void) fprintf(stderr, "something strange happened " 4445 "to child\n"); 4446 exit(4); 4447 } 4448 4449 iters++; 4450 4451 if (zopt_verbose >= 1) { 4452 hrtime_t now = gethrtime(); 4453 4454 now = MIN(now, zs->zs_stop_time); 4455 print_time(zs->zs_stop_time - now, timebuf); 4456 nicenum(zs->zs_space, numbuf); 4457 4458 (void) printf("Pass %3d, %8s, %3llu ENOSPC, " 4459 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n", 4460 iters, 4461 WIFEXITED(status) ? "Complete" : "SIGKILL", 4462 (u_longlong_t)zs->zs_enospc_count, 4463 100.0 * zs->zs_alloc / zs->zs_space, 4464 numbuf, 4465 100.0 * (now - zs->zs_start_time) / 4466 (zopt_time * NANOSEC), timebuf); 4467 } 4468 4469 if (zopt_verbose >= 2) { 4470 (void) printf("\nWorkload summary:\n\n"); 4471 (void) printf("%7s %9s %s\n", 4472 "Calls", "Time", "Function"); 4473 (void) printf("%7s %9s %s\n", 4474 "-----", "----", "--------"); 4475 for (f = 0; f < ZTEST_FUNCS; f++) { 4476 Dl_info dli; 4477 4478 zi = &zs->zs_info[f]; 4479 print_time(zi->zi_call_time, timebuf); 4480 (void) dladdr((void *)zi->zi_func, &dli); 4481 (void) printf("%7llu %9s %s\n", 4482 (u_longlong_t)zi->zi_calls, timebuf, 4483 dli.dli_sname); 4484 } 4485 (void) printf("\n"); 4486 } 4487 4488 /* 4489 * It's possible that we killed a child during a rename test, in 4490 * which case we'll have a 'ztest_tmp' pool lying around instead 4491 * of 'ztest'. Do a blind rename in case this happened. 4492 */ 4493 tmp = umem_alloc(strlen(zopt_pool) + 5, UMEM_NOFAIL); 4494 (void) strcpy(tmp, zopt_pool); 4495 (void) strcat(tmp, "_tmp"); 4496 kernel_init(FREAD | FWRITE); 4497 (void) spa_rename(tmp, zopt_pool); 4498 kernel_fini(); 4499 umem_free(tmp, strlen(tmp) + 1); 4500 } 4501 4502 ztest_verify_blocks(zopt_pool); 4503 4504 if (zopt_verbose >= 1) { 4505 (void) printf("%d killed, %d completed, %.0f%% kill rate\n", 4506 kills, iters - kills, (100.0 * kills) / MAX(1, iters)); 4507 } 4508 4509 return (0); 4510 } 4511