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