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, DMU_READ_PREFETCH)); 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, DMU_READ_PREFETCH)); 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, DMU_READ_PREFETCH)); 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, DMU_READ_PREFETCH)); 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 DMU_READ_PREFETCH); 1913 ASSERT3U(error, ==, 0); 1914 error = dmu_read(os, dd.dd_bigobj, bigoff, bigsize, bigbuf, 1915 DMU_READ_PREFETCH); 1916 ASSERT3U(error, ==, 0); 1917 1918 /* 1919 * Get a tx for the mods to both packobj and bigobj. 1920 */ 1921 tx = dmu_tx_create(os); 1922 1923 dmu_tx_hold_write(tx, dd.dd_packobj, packoff, packsize); 1924 1925 if (freeit) 1926 dmu_tx_hold_free(tx, dd.dd_bigobj, bigoff, bigsize); 1927 else 1928 dmu_tx_hold_write(tx, dd.dd_bigobj, bigoff, bigsize); 1929 1930 error = dmu_tx_assign(tx, TXG_WAIT); 1931 1932 if (error) { 1933 ztest_record_enospc("dmu r/w range"); 1934 dmu_tx_abort(tx); 1935 umem_free(packbuf, packsize); 1936 umem_free(bigbuf, bigsize); 1937 return; 1938 } 1939 1940 txg = dmu_tx_get_txg(tx); 1941 1942 /* 1943 * For each index from n to n + s, verify that the existing bufwad 1944 * in packobj matches the bufwads at the head and tail of the 1945 * corresponding chunk in bigobj. Then update all three bufwads 1946 * with the new values we want to write out. 1947 */ 1948 for (i = 0; i < s; i++) { 1949 /* LINTED */ 1950 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t)); 1951 /* LINTED */ 1952 bigH = (bufwad_t *)((char *)bigbuf + i * dd.dd_chunk); 1953 /* LINTED */ 1954 bigT = (bufwad_t *)((char *)bigH + dd.dd_chunk) - 1; 1955 1956 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize); 1957 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize); 1958 1959 if (pack->bw_txg > txg) 1960 fatal(0, "future leak: got %llx, open txg is %llx", 1961 pack->bw_txg, txg); 1962 1963 if (pack->bw_data != 0 && pack->bw_index != n + i) 1964 fatal(0, "wrong index: got %llx, wanted %llx+%llx", 1965 pack->bw_index, n, i); 1966 1967 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0) 1968 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH); 1969 1970 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0) 1971 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT); 1972 1973 if (freeit) { 1974 bzero(pack, sizeof (bufwad_t)); 1975 } else { 1976 pack->bw_index = n + i; 1977 pack->bw_txg = txg; 1978 pack->bw_data = 1 + ztest_random(-2ULL); 1979 } 1980 *bigH = *pack; 1981 *bigT = *pack; 1982 } 1983 1984 /* 1985 * We've verified all the old bufwads, and made new ones. 1986 * Now write them out. 1987 */ 1988 dmu_write(os, dd.dd_packobj, packoff, packsize, packbuf, tx); 1989 1990 if (freeit) { 1991 if (zopt_verbose >= 6) { 1992 (void) printf("freeing offset %llx size %llx" 1993 " txg %llx\n", 1994 (u_longlong_t)bigoff, 1995 (u_longlong_t)bigsize, 1996 (u_longlong_t)txg); 1997 } 1998 VERIFY(0 == dmu_free_range(os, dd.dd_bigobj, bigoff, 1999 bigsize, tx)); 2000 } else { 2001 if (zopt_verbose >= 6) { 2002 (void) printf("writing offset %llx size %llx" 2003 " txg %llx\n", 2004 (u_longlong_t)bigoff, 2005 (u_longlong_t)bigsize, 2006 (u_longlong_t)txg); 2007 } 2008 dmu_write(os, dd.dd_bigobj, bigoff, bigsize, bigbuf, tx); 2009 } 2010 2011 dmu_tx_commit(tx); 2012 2013 /* 2014 * Sanity check the stuff we just wrote. 2015 */ 2016 { 2017 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL); 2018 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL); 2019 2020 VERIFY(0 == dmu_read(os, dd.dd_packobj, packoff, 2021 packsize, packcheck, DMU_READ_PREFETCH)); 2022 VERIFY(0 == dmu_read(os, dd.dd_bigobj, bigoff, 2023 bigsize, bigcheck, DMU_READ_PREFETCH)); 2024 2025 ASSERT(bcmp(packbuf, packcheck, packsize) == 0); 2026 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0); 2027 2028 umem_free(packcheck, packsize); 2029 umem_free(bigcheck, bigsize); 2030 } 2031 2032 umem_free(packbuf, packsize); 2033 umem_free(bigbuf, bigsize); 2034 } 2035 2036 void 2037 compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf, 2038 uint64_t bigsize, uint64_t n, dmu_read_write_dir_t dd, uint64_t txg) 2039 { 2040 uint64_t i; 2041 bufwad_t *pack; 2042 bufwad_t *bigH; 2043 bufwad_t *bigT; 2044 2045 /* 2046 * For each index from n to n + s, verify that the existing bufwad 2047 * in packobj matches the bufwads at the head and tail of the 2048 * corresponding chunk in bigobj. Then update all three bufwads 2049 * with the new values we want to write out. 2050 */ 2051 for (i = 0; i < s; i++) { 2052 /* LINTED */ 2053 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t)); 2054 /* LINTED */ 2055 bigH = (bufwad_t *)((char *)bigbuf + i * dd.dd_chunk); 2056 /* LINTED */ 2057 bigT = (bufwad_t *)((char *)bigH + dd.dd_chunk) - 1; 2058 2059 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize); 2060 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize); 2061 2062 if (pack->bw_txg > txg) 2063 fatal(0, "future leak: got %llx, open txg is %llx", 2064 pack->bw_txg, txg); 2065 2066 if (pack->bw_data != 0 && pack->bw_index != n + i) 2067 fatal(0, "wrong index: got %llx, wanted %llx+%llx", 2068 pack->bw_index, n, i); 2069 2070 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0) 2071 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH); 2072 2073 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0) 2074 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT); 2075 2076 pack->bw_index = n + i; 2077 pack->bw_txg = txg; 2078 pack->bw_data = 1 + ztest_random(-2ULL); 2079 2080 *bigH = *pack; 2081 *bigT = *pack; 2082 } 2083 } 2084 2085 void 2086 ztest_dmu_read_write_zcopy(ztest_args_t *za) 2087 { 2088 objset_t *os = za->za_os; 2089 dmu_read_write_dir_t dd; 2090 dmu_tx_t *tx; 2091 uint64_t i; 2092 int error; 2093 uint64_t n, s, txg; 2094 bufwad_t *packbuf, *bigbuf; 2095 uint64_t packoff, packsize, bigoff, bigsize; 2096 uint64_t regions = 997; 2097 uint64_t stride = 123456789ULL; 2098 uint64_t width = 9; 2099 dmu_buf_t *bonus_db; 2100 arc_buf_t **bigbuf_arcbufs; 2101 dmu_object_info_t *doi = &za->za_doi; 2102 2103 /* 2104 * This test uses two objects, packobj and bigobj, that are always 2105 * updated together (i.e. in the same tx) so that their contents are 2106 * in sync and can be compared. Their contents relate to each other 2107 * in a simple way: packobj is a dense array of 'bufwad' structures, 2108 * while bigobj is a sparse array of the same bufwads. Specifically, 2109 * for any index n, there are three bufwads that should be identical: 2110 * 2111 * packobj, at offset n * sizeof (bufwad_t) 2112 * bigobj, at the head of the nth chunk 2113 * bigobj, at the tail of the nth chunk 2114 * 2115 * The chunk size is set equal to bigobj block size so that 2116 * dmu_assign_arcbuf() can be tested for object updates. 2117 */ 2118 2119 /* 2120 * Read the directory info. If it's the first time, set things up. 2121 */ 2122 VERIFY(0 == dmu_read(os, ZTEST_DIROBJ, za->za_diroff, 2123 sizeof (dd), &dd, DMU_READ_PREFETCH)); 2124 if (dd.dd_chunk == 0) { 2125 ASSERT(dd.dd_packobj == 0); 2126 ASSERT(dd.dd_bigobj == 0); 2127 tx = dmu_tx_create(os); 2128 dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, sizeof (dd)); 2129 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 2130 error = dmu_tx_assign(tx, TXG_WAIT); 2131 if (error) { 2132 ztest_record_enospc("create r/w directory"); 2133 dmu_tx_abort(tx); 2134 return; 2135 } 2136 2137 dd.dd_packobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0, 2138 DMU_OT_NONE, 0, tx); 2139 dd.dd_bigobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0, 2140 DMU_OT_NONE, 0, tx); 2141 ztest_set_random_blocksize(os, dd.dd_packobj, tx); 2142 ztest_set_random_blocksize(os, dd.dd_bigobj, tx); 2143 2144 VERIFY(dmu_object_info(os, dd.dd_bigobj, doi) == 0); 2145 ASSERT(doi->doi_data_block_size >= 2 * sizeof (bufwad_t)); 2146 ASSERT(ISP2(doi->doi_data_block_size)); 2147 dd.dd_chunk = doi->doi_data_block_size; 2148 2149 dmu_write(os, ZTEST_DIROBJ, za->za_diroff, sizeof (dd), &dd, 2150 tx); 2151 dmu_tx_commit(tx); 2152 } else { 2153 VERIFY(dmu_object_info(os, dd.dd_bigobj, doi) == 0); 2154 VERIFY(ISP2(doi->doi_data_block_size)); 2155 VERIFY(dd.dd_chunk == doi->doi_data_block_size); 2156 VERIFY(dd.dd_chunk >= 2 * sizeof (bufwad_t)); 2157 } 2158 2159 /* 2160 * Pick a random index and compute the offsets into packobj and bigobj. 2161 */ 2162 n = ztest_random(regions) * stride + ztest_random(width); 2163 s = 1 + ztest_random(width - 1); 2164 2165 packoff = n * sizeof (bufwad_t); 2166 packsize = s * sizeof (bufwad_t); 2167 2168 bigoff = n * dd.dd_chunk; 2169 bigsize = s * dd.dd_chunk; 2170 2171 packbuf = umem_zalloc(packsize, UMEM_NOFAIL); 2172 bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL); 2173 2174 VERIFY(dmu_bonus_hold(os, dd.dd_bigobj, FTAG, &bonus_db) == 0); 2175 2176 bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL); 2177 2178 /* 2179 * Iteration 0 test zcopy for DB_UNCACHED dbufs. 2180 * Iteration 1 test zcopy to already referenced dbufs. 2181 * Iteration 2 test zcopy to dirty dbuf in the same txg. 2182 * Iteration 3 test zcopy to dbuf dirty in previous txg. 2183 * Iteration 4 test zcopy when dbuf is no longer dirty. 2184 * Iteration 5 test zcopy when it can't be done. 2185 * Iteration 6 one more zcopy write. 2186 */ 2187 for (i = 0; i < 7; i++) { 2188 uint64_t j; 2189 uint64_t off; 2190 2191 /* 2192 * In iteration 5 (i == 5) use arcbufs 2193 * that don't match bigobj blksz to test 2194 * dmu_assign_arcbuf() when it can't directly 2195 * assign an arcbuf to a dbuf. 2196 */ 2197 for (j = 0; j < s; j++) { 2198 if (i != 5) { 2199 bigbuf_arcbufs[j] = 2200 dmu_request_arcbuf(bonus_db, 2201 dd.dd_chunk); 2202 } else { 2203 bigbuf_arcbufs[2 * j] = 2204 dmu_request_arcbuf(bonus_db, 2205 dd.dd_chunk / 2); 2206 bigbuf_arcbufs[2 * j + 1] = 2207 dmu_request_arcbuf(bonus_db, 2208 dd.dd_chunk / 2); 2209 } 2210 } 2211 2212 /* 2213 * Get a tx for the mods to both packobj and bigobj. 2214 */ 2215 tx = dmu_tx_create(os); 2216 2217 dmu_tx_hold_write(tx, dd.dd_packobj, packoff, packsize); 2218 dmu_tx_hold_write(tx, dd.dd_bigobj, bigoff, bigsize); 2219 2220 if (ztest_random(100) == 0) { 2221 error = -1; 2222 } else { 2223 error = dmu_tx_assign(tx, TXG_WAIT); 2224 } 2225 2226 if (error) { 2227 if (error != -1) { 2228 ztest_record_enospc("dmu r/w range"); 2229 } 2230 dmu_tx_abort(tx); 2231 umem_free(packbuf, packsize); 2232 umem_free(bigbuf, bigsize); 2233 for (j = 0; j < s; j++) { 2234 if (i != 5) { 2235 dmu_return_arcbuf(bigbuf_arcbufs[j]); 2236 } else { 2237 dmu_return_arcbuf( 2238 bigbuf_arcbufs[2 * j]); 2239 dmu_return_arcbuf( 2240 bigbuf_arcbufs[2 * j + 1]); 2241 } 2242 } 2243 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *)); 2244 dmu_buf_rele(bonus_db, FTAG); 2245 return; 2246 } 2247 2248 txg = dmu_tx_get_txg(tx); 2249 2250 /* 2251 * 50% of the time don't read objects in the 1st iteration to 2252 * test dmu_assign_arcbuf() for the case when there're no 2253 * existing dbufs for the specified offsets. 2254 */ 2255 if (i != 0 || ztest_random(2) != 0) { 2256 error = dmu_read(os, dd.dd_packobj, packoff, 2257 packsize, packbuf, DMU_READ_PREFETCH); 2258 ASSERT3U(error, ==, 0); 2259 error = dmu_read(os, dd.dd_bigobj, bigoff, bigsize, 2260 bigbuf, DMU_READ_PREFETCH); 2261 ASSERT3U(error, ==, 0); 2262 } 2263 compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize, 2264 n, dd, txg); 2265 2266 /* 2267 * We've verified all the old bufwads, and made new ones. 2268 * Now write them out. 2269 */ 2270 dmu_write(os, dd.dd_packobj, packoff, packsize, packbuf, tx); 2271 if (zopt_verbose >= 6) { 2272 (void) printf("writing offset %llx size %llx" 2273 " txg %llx\n", 2274 (u_longlong_t)bigoff, 2275 (u_longlong_t)bigsize, 2276 (u_longlong_t)txg); 2277 } 2278 for (off = bigoff, j = 0; j < s; j++, off += dd.dd_chunk) { 2279 dmu_buf_t *dbt; 2280 if (i != 5) { 2281 bcopy((caddr_t)bigbuf + (off - bigoff), 2282 bigbuf_arcbufs[j]->b_data, dd.dd_chunk); 2283 } else { 2284 bcopy((caddr_t)bigbuf + (off - bigoff), 2285 bigbuf_arcbufs[2 * j]->b_data, 2286 dd.dd_chunk / 2); 2287 bcopy((caddr_t)bigbuf + (off - bigoff) + 2288 dd.dd_chunk / 2, 2289 bigbuf_arcbufs[2 * j + 1]->b_data, 2290 dd.dd_chunk / 2); 2291 } 2292 2293 if (i == 1) { 2294 VERIFY(dmu_buf_hold(os, dd.dd_bigobj, off, 2295 FTAG, &dbt) == 0); 2296 } 2297 if (i != 5) { 2298 dmu_assign_arcbuf(bonus_db, off, 2299 bigbuf_arcbufs[j], tx); 2300 } else { 2301 dmu_assign_arcbuf(bonus_db, off, 2302 bigbuf_arcbufs[2 * j], tx); 2303 dmu_assign_arcbuf(bonus_db, 2304 off + dd.dd_chunk / 2, 2305 bigbuf_arcbufs[2 * j + 1], tx); 2306 } 2307 if (i == 1) { 2308 dmu_buf_rele(dbt, FTAG); 2309 } 2310 } 2311 dmu_tx_commit(tx); 2312 2313 /* 2314 * Sanity check the stuff we just wrote. 2315 */ 2316 { 2317 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL); 2318 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL); 2319 2320 VERIFY(0 == dmu_read(os, dd.dd_packobj, packoff, 2321 packsize, packcheck, DMU_READ_PREFETCH)); 2322 VERIFY(0 == dmu_read(os, dd.dd_bigobj, bigoff, 2323 bigsize, bigcheck, DMU_READ_PREFETCH)); 2324 2325 ASSERT(bcmp(packbuf, packcheck, packsize) == 0); 2326 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0); 2327 2328 umem_free(packcheck, packsize); 2329 umem_free(bigcheck, bigsize); 2330 } 2331 if (i == 2) { 2332 txg_wait_open(dmu_objset_pool(os), 0); 2333 } else if (i == 3) { 2334 txg_wait_synced(dmu_objset_pool(os), 0); 2335 } 2336 } 2337 2338 dmu_buf_rele(bonus_db, FTAG); 2339 umem_free(packbuf, packsize); 2340 umem_free(bigbuf, bigsize); 2341 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *)); 2342 } 2343 2344 void 2345 ztest_dmu_check_future_leak(ztest_args_t *za) 2346 { 2347 objset_t *os = za->za_os; 2348 dmu_buf_t *db; 2349 ztest_block_tag_t *bt; 2350 dmu_object_info_t *doi = &za->za_doi; 2351 2352 /* 2353 * Make sure that, if there is a write record in the bonus buffer 2354 * of the ZTEST_DIROBJ, that the txg for this record is <= the 2355 * last synced txg of the pool. 2356 */ 2357 VERIFY(dmu_bonus_hold(os, ZTEST_DIROBJ, FTAG, &db) == 0); 2358 za->za_dbuf = db; 2359 VERIFY(dmu_object_info(os, ZTEST_DIROBJ, doi) == 0); 2360 ASSERT3U(doi->doi_bonus_size, >=, sizeof (*bt)); 2361 ASSERT3U(doi->doi_bonus_size, <=, db->db_size); 2362 ASSERT3U(doi->doi_bonus_size % sizeof (*bt), ==, 0); 2363 bt = (void *)((char *)db->db_data + doi->doi_bonus_size - sizeof (*bt)); 2364 if (bt->bt_objset != 0) { 2365 ASSERT3U(bt->bt_objset, ==, dmu_objset_id(os)); 2366 ASSERT3U(bt->bt_object, ==, ZTEST_DIROBJ); 2367 ASSERT3U(bt->bt_offset, ==, -1ULL); 2368 ASSERT3U(bt->bt_txg, <, spa_first_txg(za->za_spa)); 2369 } 2370 dmu_buf_rele(db, FTAG); 2371 za->za_dbuf = NULL; 2372 } 2373 2374 void 2375 ztest_dmu_write_parallel(ztest_args_t *za) 2376 { 2377 objset_t *os = za->za_os; 2378 ztest_block_tag_t *rbt = &za->za_rbt; 2379 ztest_block_tag_t *wbt = &za->za_wbt; 2380 const size_t btsize = sizeof (ztest_block_tag_t); 2381 dmu_buf_t *db; 2382 int b, error; 2383 int bs = ZTEST_DIROBJ_BLOCKSIZE; 2384 int do_free = 0; 2385 uint64_t off, txg, txg_how; 2386 mutex_t *lp; 2387 char osname[MAXNAMELEN]; 2388 char iobuf[SPA_MAXBLOCKSIZE]; 2389 blkptr_t blk = { 0 }; 2390 uint64_t blkoff; 2391 zbookmark_t zb; 2392 dmu_tx_t *tx = dmu_tx_create(os); 2393 dmu_buf_t *bonus_db; 2394 arc_buf_t *abuf = NULL; 2395 2396 dmu_objset_name(os, osname); 2397 2398 /* 2399 * Have multiple threads write to large offsets in ZTEST_DIROBJ 2400 * to verify that having multiple threads writing to the same object 2401 * in parallel doesn't cause any trouble. 2402 */ 2403 if (ztest_random(4) == 0) { 2404 /* 2405 * Do the bonus buffer instead of a regular block. 2406 * We need a lock to serialize resize vs. others, 2407 * so we hash on the objset ID. 2408 */ 2409 b = dmu_objset_id(os) % ZTEST_SYNC_LOCKS; 2410 off = -1ULL; 2411 dmu_tx_hold_bonus(tx, ZTEST_DIROBJ); 2412 } else { 2413 b = ztest_random(ZTEST_SYNC_LOCKS); 2414 off = za->za_diroff_shared + (b << SPA_MAXBLOCKSHIFT); 2415 if (ztest_random(4) == 0) { 2416 do_free = 1; 2417 dmu_tx_hold_free(tx, ZTEST_DIROBJ, off, bs); 2418 } else { 2419 dmu_tx_hold_write(tx, ZTEST_DIROBJ, off, bs); 2420 } 2421 } 2422 2423 if (off != -1ULL && P2PHASE(off, bs) == 0 && !do_free && 2424 ztest_random(8) == 0) { 2425 VERIFY(dmu_bonus_hold(os, ZTEST_DIROBJ, FTAG, &bonus_db) == 0); 2426 abuf = dmu_request_arcbuf(bonus_db, bs); 2427 } 2428 2429 txg_how = ztest_random(2) == 0 ? TXG_WAIT : TXG_NOWAIT; 2430 error = dmu_tx_assign(tx, txg_how); 2431 if (error) { 2432 if (error == ERESTART) { 2433 ASSERT(txg_how == TXG_NOWAIT); 2434 dmu_tx_wait(tx); 2435 } else { 2436 ztest_record_enospc("dmu write parallel"); 2437 } 2438 dmu_tx_abort(tx); 2439 if (abuf != NULL) { 2440 dmu_return_arcbuf(abuf); 2441 dmu_buf_rele(bonus_db, FTAG); 2442 } 2443 return; 2444 } 2445 txg = dmu_tx_get_txg(tx); 2446 2447 lp = &ztest_shared->zs_sync_lock[b]; 2448 (void) mutex_lock(lp); 2449 2450 wbt->bt_objset = dmu_objset_id(os); 2451 wbt->bt_object = ZTEST_DIROBJ; 2452 wbt->bt_offset = off; 2453 wbt->bt_txg = txg; 2454 wbt->bt_thread = za->za_instance; 2455 wbt->bt_seq = ztest_shared->zs_seq[b]++; /* protected by lp */ 2456 2457 /* 2458 * Occasionally, write an all-zero block to test the behavior 2459 * of blocks that compress into holes. 2460 */ 2461 if (off != -1ULL && ztest_random(8) == 0) 2462 bzero(wbt, btsize); 2463 2464 if (off == -1ULL) { 2465 dmu_object_info_t *doi = &za->za_doi; 2466 char *dboff; 2467 2468 VERIFY(dmu_bonus_hold(os, ZTEST_DIROBJ, FTAG, &db) == 0); 2469 za->za_dbuf = db; 2470 dmu_object_info_from_db(db, doi); 2471 ASSERT3U(doi->doi_bonus_size, <=, db->db_size); 2472 ASSERT3U(doi->doi_bonus_size, >=, btsize); 2473 ASSERT3U(doi->doi_bonus_size % btsize, ==, 0); 2474 dboff = (char *)db->db_data + doi->doi_bonus_size - btsize; 2475 bcopy(dboff, rbt, btsize); 2476 if (rbt->bt_objset != 0) { 2477 ASSERT3U(rbt->bt_objset, ==, wbt->bt_objset); 2478 ASSERT3U(rbt->bt_object, ==, wbt->bt_object); 2479 ASSERT3U(rbt->bt_offset, ==, wbt->bt_offset); 2480 ASSERT3U(rbt->bt_txg, <=, wbt->bt_txg); 2481 } 2482 if (ztest_random(10) == 0) { 2483 int newsize = (ztest_random(db->db_size / 2484 btsize) + 1) * btsize; 2485 2486 ASSERT3U(newsize, >=, btsize); 2487 ASSERT3U(newsize, <=, db->db_size); 2488 VERIFY3U(dmu_set_bonus(db, newsize, tx), ==, 0); 2489 dboff = (char *)db->db_data + newsize - btsize; 2490 } 2491 dmu_buf_will_dirty(db, tx); 2492 bcopy(wbt, dboff, btsize); 2493 dmu_buf_rele(db, FTAG); 2494 za->za_dbuf = NULL; 2495 } else if (do_free) { 2496 VERIFY(dmu_free_range(os, ZTEST_DIROBJ, off, bs, tx) == 0); 2497 } else if (abuf == NULL) { 2498 dmu_write(os, ZTEST_DIROBJ, off, btsize, wbt, tx); 2499 } else { 2500 bcopy(wbt, abuf->b_data, btsize); 2501 dmu_assign_arcbuf(bonus_db, off, abuf, tx); 2502 dmu_buf_rele(bonus_db, FTAG); 2503 } 2504 2505 (void) mutex_unlock(lp); 2506 2507 if (ztest_random(1000) == 0) 2508 (void) poll(NULL, 0, 1); /* open dn_notxholds window */ 2509 2510 dmu_tx_commit(tx); 2511 2512 if (ztest_random(10000) == 0) 2513 txg_wait_synced(dmu_objset_pool(os), txg); 2514 2515 if (off == -1ULL || do_free) 2516 return; 2517 2518 if (ztest_random(2) != 0) 2519 return; 2520 2521 /* 2522 * dmu_sync() the block we just wrote. 2523 */ 2524 (void) mutex_lock(lp); 2525 2526 blkoff = P2ALIGN_TYPED(off, bs, uint64_t); 2527 error = dmu_buf_hold(os, ZTEST_DIROBJ, blkoff, FTAG, &db); 2528 za->za_dbuf = db; 2529 if (error) { 2530 (void) mutex_unlock(lp); 2531 return; 2532 } 2533 blkoff = off - blkoff; 2534 error = dmu_sync(NULL, db, &blk, txg, NULL, NULL); 2535 dmu_buf_rele(db, FTAG); 2536 za->za_dbuf = NULL; 2537 2538 if (error) { 2539 (void) mutex_unlock(lp); 2540 return; 2541 } 2542 2543 if (blk.blk_birth == 0) { /* concurrent free */ 2544 (void) mutex_unlock(lp); 2545 return; 2546 } 2547 2548 txg_suspend(dmu_objset_pool(os)); 2549 2550 (void) mutex_unlock(lp); 2551 2552 ASSERT(blk.blk_fill == 1); 2553 ASSERT3U(BP_GET_TYPE(&blk), ==, DMU_OT_UINT64_OTHER); 2554 ASSERT3U(BP_GET_LEVEL(&blk), ==, 0); 2555 ASSERT3U(BP_GET_LSIZE(&blk), ==, bs); 2556 2557 /* 2558 * Read the block that dmu_sync() returned to make sure its contents 2559 * match what we wrote. We do this while still txg_suspend()ed 2560 * to ensure that the block can't be reused before we read it. 2561 */ 2562 zb.zb_objset = dmu_objset_id(os); 2563 zb.zb_object = ZTEST_DIROBJ; 2564 zb.zb_level = 0; 2565 zb.zb_blkid = off / bs; 2566 error = zio_wait(zio_read(NULL, za->za_spa, &blk, iobuf, bs, 2567 NULL, NULL, ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_MUSTSUCCEED, &zb)); 2568 ASSERT3U(error, ==, 0); 2569 2570 txg_resume(dmu_objset_pool(os)); 2571 2572 bcopy(&iobuf[blkoff], rbt, btsize); 2573 2574 if (rbt->bt_objset == 0) /* concurrent free */ 2575 return; 2576 2577 if (wbt->bt_objset == 0) /* all-zero overwrite */ 2578 return; 2579 2580 ASSERT3U(rbt->bt_objset, ==, wbt->bt_objset); 2581 ASSERT3U(rbt->bt_object, ==, wbt->bt_object); 2582 ASSERT3U(rbt->bt_offset, ==, wbt->bt_offset); 2583 2584 /* 2585 * The semantic of dmu_sync() is that we always push the most recent 2586 * version of the data, so in the face of concurrent updates we may 2587 * see a newer version of the block. That's OK. 2588 */ 2589 ASSERT3U(rbt->bt_txg, >=, wbt->bt_txg); 2590 if (rbt->bt_thread == wbt->bt_thread) 2591 ASSERT3U(rbt->bt_seq, ==, wbt->bt_seq); 2592 else 2593 ASSERT3U(rbt->bt_seq, >, wbt->bt_seq); 2594 } 2595 2596 /* 2597 * Verify that zap_{create,destroy,add,remove,update} work as expected. 2598 */ 2599 #define ZTEST_ZAP_MIN_INTS 1 2600 #define ZTEST_ZAP_MAX_INTS 4 2601 #define ZTEST_ZAP_MAX_PROPS 1000 2602 2603 void 2604 ztest_zap(ztest_args_t *za) 2605 { 2606 objset_t *os = za->za_os; 2607 uint64_t object; 2608 uint64_t txg, last_txg; 2609 uint64_t value[ZTEST_ZAP_MAX_INTS]; 2610 uint64_t zl_ints, zl_intsize, prop; 2611 int i, ints; 2612 dmu_tx_t *tx; 2613 char propname[100], txgname[100]; 2614 int error; 2615 char osname[MAXNAMELEN]; 2616 char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" }; 2617 2618 dmu_objset_name(os, osname); 2619 2620 /* 2621 * Create a new object if necessary, and record it in the directory. 2622 */ 2623 VERIFY(0 == dmu_read(os, ZTEST_DIROBJ, za->za_diroff, 2624 sizeof (uint64_t), &object, DMU_READ_PREFETCH)); 2625 2626 if (object == 0) { 2627 tx = dmu_tx_create(os); 2628 dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, 2629 sizeof (uint64_t)); 2630 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, NULL); 2631 error = dmu_tx_assign(tx, TXG_WAIT); 2632 if (error) { 2633 ztest_record_enospc("create zap test obj"); 2634 dmu_tx_abort(tx); 2635 return; 2636 } 2637 object = zap_create(os, DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx); 2638 if (error) { 2639 fatal(0, "zap_create('%s', %llu) = %d", 2640 osname, object, error); 2641 } 2642 ASSERT(object != 0); 2643 dmu_write(os, ZTEST_DIROBJ, za->za_diroff, 2644 sizeof (uint64_t), &object, tx); 2645 /* 2646 * Generate a known hash collision, and verify that 2647 * we can lookup and remove both entries. 2648 */ 2649 for (i = 0; i < 2; i++) { 2650 value[i] = i; 2651 error = zap_add(os, object, hc[i], sizeof (uint64_t), 2652 1, &value[i], tx); 2653 ASSERT3U(error, ==, 0); 2654 } 2655 for (i = 0; i < 2; i++) { 2656 error = zap_add(os, object, hc[i], sizeof (uint64_t), 2657 1, &value[i], tx); 2658 ASSERT3U(error, ==, EEXIST); 2659 error = zap_length(os, object, hc[i], 2660 &zl_intsize, &zl_ints); 2661 ASSERT3U(error, ==, 0); 2662 ASSERT3U(zl_intsize, ==, sizeof (uint64_t)); 2663 ASSERT3U(zl_ints, ==, 1); 2664 } 2665 for (i = 0; i < 2; i++) { 2666 error = zap_remove(os, object, hc[i], tx); 2667 ASSERT3U(error, ==, 0); 2668 } 2669 2670 dmu_tx_commit(tx); 2671 } 2672 2673 ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS); 2674 2675 prop = ztest_random(ZTEST_ZAP_MAX_PROPS); 2676 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop); 2677 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop); 2678 bzero(value, sizeof (value)); 2679 last_txg = 0; 2680 2681 /* 2682 * If these zap entries already exist, validate their contents. 2683 */ 2684 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints); 2685 if (error == 0) { 2686 ASSERT3U(zl_intsize, ==, sizeof (uint64_t)); 2687 ASSERT3U(zl_ints, ==, 1); 2688 2689 VERIFY(zap_lookup(os, object, txgname, zl_intsize, 2690 zl_ints, &last_txg) == 0); 2691 2692 VERIFY(zap_length(os, object, propname, &zl_intsize, 2693 &zl_ints) == 0); 2694 2695 ASSERT3U(zl_intsize, ==, sizeof (uint64_t)); 2696 ASSERT3U(zl_ints, ==, ints); 2697 2698 VERIFY(zap_lookup(os, object, propname, zl_intsize, 2699 zl_ints, value) == 0); 2700 2701 for (i = 0; i < ints; i++) { 2702 ASSERT3U(value[i], ==, last_txg + object + i); 2703 } 2704 } else { 2705 ASSERT3U(error, ==, ENOENT); 2706 } 2707 2708 /* 2709 * Atomically update two entries in our zap object. 2710 * The first is named txg_%llu, and contains the txg 2711 * in which the property was last updated. The second 2712 * is named prop_%llu, and the nth element of its value 2713 * should be txg + object + n. 2714 */ 2715 tx = dmu_tx_create(os); 2716 dmu_tx_hold_zap(tx, object, TRUE, NULL); 2717 error = dmu_tx_assign(tx, TXG_WAIT); 2718 if (error) { 2719 ztest_record_enospc("create zap entry"); 2720 dmu_tx_abort(tx); 2721 return; 2722 } 2723 txg = dmu_tx_get_txg(tx); 2724 2725 if (last_txg > txg) 2726 fatal(0, "zap future leak: old %llu new %llu", last_txg, txg); 2727 2728 for (i = 0; i < ints; i++) 2729 value[i] = txg + object + i; 2730 2731 error = zap_update(os, object, txgname, sizeof (uint64_t), 1, &txg, tx); 2732 if (error) 2733 fatal(0, "zap_update('%s', %llu, '%s') = %d", 2734 osname, object, txgname, error); 2735 2736 error = zap_update(os, object, propname, sizeof (uint64_t), 2737 ints, value, tx); 2738 if (error) 2739 fatal(0, "zap_update('%s', %llu, '%s') = %d", 2740 osname, object, propname, error); 2741 2742 dmu_tx_commit(tx); 2743 2744 /* 2745 * Remove a random pair of entries. 2746 */ 2747 prop = ztest_random(ZTEST_ZAP_MAX_PROPS); 2748 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop); 2749 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop); 2750 2751 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints); 2752 2753 if (error == ENOENT) 2754 return; 2755 2756 ASSERT3U(error, ==, 0); 2757 2758 tx = dmu_tx_create(os); 2759 dmu_tx_hold_zap(tx, object, TRUE, NULL); 2760 error = dmu_tx_assign(tx, TXG_WAIT); 2761 if (error) { 2762 ztest_record_enospc("remove zap entry"); 2763 dmu_tx_abort(tx); 2764 return; 2765 } 2766 error = zap_remove(os, object, txgname, tx); 2767 if (error) 2768 fatal(0, "zap_remove('%s', %llu, '%s') = %d", 2769 osname, object, txgname, error); 2770 2771 error = zap_remove(os, object, propname, tx); 2772 if (error) 2773 fatal(0, "zap_remove('%s', %llu, '%s') = %d", 2774 osname, object, propname, error); 2775 2776 dmu_tx_commit(tx); 2777 2778 /* 2779 * Once in a while, destroy the object. 2780 */ 2781 if (ztest_random(1000) != 0) 2782 return; 2783 2784 tx = dmu_tx_create(os); 2785 dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, sizeof (uint64_t)); 2786 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END); 2787 error = dmu_tx_assign(tx, TXG_WAIT); 2788 if (error) { 2789 ztest_record_enospc("destroy zap object"); 2790 dmu_tx_abort(tx); 2791 return; 2792 } 2793 error = zap_destroy(os, object, tx); 2794 if (error) 2795 fatal(0, "zap_destroy('%s', %llu) = %d", 2796 osname, object, error); 2797 object = 0; 2798 dmu_write(os, ZTEST_DIROBJ, za->za_diroff, sizeof (uint64_t), 2799 &object, tx); 2800 dmu_tx_commit(tx); 2801 } 2802 2803 void 2804 ztest_zap_parallel(ztest_args_t *za) 2805 { 2806 objset_t *os = za->za_os; 2807 uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc; 2808 dmu_tx_t *tx; 2809 int i, namelen, error; 2810 char name[20], string_value[20]; 2811 void *data; 2812 2813 /* 2814 * Generate a random name of the form 'xxx.....' where each 2815 * x is a random printable character and the dots are dots. 2816 * There are 94 such characters, and the name length goes from 2817 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names. 2818 */ 2819 namelen = ztest_random(sizeof (name) - 5) + 5 + 1; 2820 2821 for (i = 0; i < 3; i++) 2822 name[i] = '!' + ztest_random('~' - '!' + 1); 2823 for (; i < namelen - 1; i++) 2824 name[i] = '.'; 2825 name[i] = '\0'; 2826 2827 if (ztest_random(2) == 0) 2828 object = ZTEST_MICROZAP_OBJ; 2829 else 2830 object = ZTEST_FATZAP_OBJ; 2831 2832 if ((namelen & 1) || object == ZTEST_MICROZAP_OBJ) { 2833 wsize = sizeof (txg); 2834 wc = 1; 2835 data = &txg; 2836 } else { 2837 wsize = 1; 2838 wc = namelen; 2839 data = string_value; 2840 } 2841 2842 count = -1ULL; 2843 VERIFY(zap_count(os, object, &count) == 0); 2844 ASSERT(count != -1ULL); 2845 2846 /* 2847 * Select an operation: length, lookup, add, update, remove. 2848 */ 2849 i = ztest_random(5); 2850 2851 if (i >= 2) { 2852 tx = dmu_tx_create(os); 2853 dmu_tx_hold_zap(tx, object, TRUE, NULL); 2854 error = dmu_tx_assign(tx, TXG_WAIT); 2855 if (error) { 2856 ztest_record_enospc("zap parallel"); 2857 dmu_tx_abort(tx); 2858 return; 2859 } 2860 txg = dmu_tx_get_txg(tx); 2861 bcopy(name, string_value, namelen); 2862 } else { 2863 tx = NULL; 2864 txg = 0; 2865 bzero(string_value, namelen); 2866 } 2867 2868 switch (i) { 2869 2870 case 0: 2871 error = zap_length(os, object, name, &zl_wsize, &zl_wc); 2872 if (error == 0) { 2873 ASSERT3U(wsize, ==, zl_wsize); 2874 ASSERT3U(wc, ==, zl_wc); 2875 } else { 2876 ASSERT3U(error, ==, ENOENT); 2877 } 2878 break; 2879 2880 case 1: 2881 error = zap_lookup(os, object, name, wsize, wc, data); 2882 if (error == 0) { 2883 if (data == string_value && 2884 bcmp(name, data, namelen) != 0) 2885 fatal(0, "name '%s' != val '%s' len %d", 2886 name, data, namelen); 2887 } else { 2888 ASSERT3U(error, ==, ENOENT); 2889 } 2890 break; 2891 2892 case 2: 2893 error = zap_add(os, object, name, wsize, wc, data, tx); 2894 ASSERT(error == 0 || error == EEXIST); 2895 break; 2896 2897 case 3: 2898 VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0); 2899 break; 2900 2901 case 4: 2902 error = zap_remove(os, object, name, tx); 2903 ASSERT(error == 0 || error == ENOENT); 2904 break; 2905 } 2906 2907 if (tx != NULL) 2908 dmu_tx_commit(tx); 2909 } 2910 2911 void 2912 ztest_dsl_prop_get_set(ztest_args_t *za) 2913 { 2914 objset_t *os = za->za_os; 2915 int i, inherit; 2916 uint64_t value; 2917 const char *prop, *valname; 2918 char setpoint[MAXPATHLEN]; 2919 char osname[MAXNAMELEN]; 2920 int error; 2921 2922 (void) rw_rdlock(&ztest_shared->zs_name_lock); 2923 2924 dmu_objset_name(os, osname); 2925 2926 for (i = 0; i < 2; i++) { 2927 if (i == 0) { 2928 prop = "checksum"; 2929 value = ztest_random_checksum(); 2930 inherit = (value == ZIO_CHECKSUM_INHERIT); 2931 } else { 2932 prop = "compression"; 2933 value = ztest_random_compress(); 2934 inherit = (value == ZIO_COMPRESS_INHERIT); 2935 } 2936 2937 error = dsl_prop_set(osname, prop, sizeof (value), 2938 !inherit, &value); 2939 2940 if (error == ENOSPC) { 2941 ztest_record_enospc("dsl_prop_set"); 2942 break; 2943 } 2944 2945 ASSERT3U(error, ==, 0); 2946 2947 VERIFY3U(dsl_prop_get(osname, prop, sizeof (value), 2948 1, &value, setpoint), ==, 0); 2949 2950 if (i == 0) 2951 valname = zio_checksum_table[value].ci_name; 2952 else 2953 valname = zio_compress_table[value].ci_name; 2954 2955 if (zopt_verbose >= 6) { 2956 (void) printf("%s %s = %s for '%s'\n", 2957 osname, prop, valname, setpoint); 2958 } 2959 } 2960 2961 (void) rw_unlock(&ztest_shared->zs_name_lock); 2962 } 2963 2964 /* 2965 * Inject random faults into the on-disk data. 2966 */ 2967 void 2968 ztest_fault_inject(ztest_args_t *za) 2969 { 2970 int fd; 2971 uint64_t offset; 2972 uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz; 2973 uint64_t bad = 0x1990c0ffeedecade; 2974 uint64_t top, leaf; 2975 char path0[MAXPATHLEN]; 2976 char pathrand[MAXPATHLEN]; 2977 size_t fsize; 2978 spa_t *spa = za->za_spa; 2979 int bshift = SPA_MAXBLOCKSHIFT + 2; /* don't scrog all labels */ 2980 int iters = 1000; 2981 int maxfaults = zopt_maxfaults; 2982 vdev_t *vd0 = NULL; 2983 uint64_t guid0 = 0; 2984 2985 ASSERT(leaves >= 1); 2986 2987 /* 2988 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd. 2989 */ 2990 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 2991 2992 if (ztest_random(2) == 0) { 2993 /* 2994 * Inject errors on a normal data device. 2995 */ 2996 top = ztest_random(spa->spa_root_vdev->vdev_children); 2997 leaf = ztest_random(leaves); 2998 2999 /* 3000 * Generate paths to the first leaf in this top-level vdev, 3001 * and to the random leaf we selected. We'll induce transient 3002 * write failures and random online/offline activity on leaf 0, 3003 * and we'll write random garbage to the randomly chosen leaf. 3004 */ 3005 (void) snprintf(path0, sizeof (path0), ztest_dev_template, 3006 zopt_dir, zopt_pool, top * leaves + 0); 3007 (void) snprintf(pathrand, sizeof (pathrand), ztest_dev_template, 3008 zopt_dir, zopt_pool, top * leaves + leaf); 3009 3010 vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0); 3011 if (vd0 != NULL && maxfaults != 1) { 3012 /* 3013 * Make vd0 explicitly claim to be unreadable, 3014 * or unwriteable, or reach behind its back 3015 * and close the underlying fd. We can do this if 3016 * maxfaults == 0 because we'll fail and reexecute, 3017 * and we can do it if maxfaults >= 2 because we'll 3018 * have enough redundancy. If maxfaults == 1, the 3019 * combination of this with injection of random data 3020 * corruption below exceeds the pool's fault tolerance. 3021 */ 3022 vdev_file_t *vf = vd0->vdev_tsd; 3023 3024 if (vf != NULL && ztest_random(3) == 0) { 3025 (void) close(vf->vf_vnode->v_fd); 3026 vf->vf_vnode->v_fd = -1; 3027 } else if (ztest_random(2) == 0) { 3028 vd0->vdev_cant_read = B_TRUE; 3029 } else { 3030 vd0->vdev_cant_write = B_TRUE; 3031 } 3032 guid0 = vd0->vdev_guid; 3033 } 3034 } else { 3035 /* 3036 * Inject errors on an l2cache device. 3037 */ 3038 spa_aux_vdev_t *sav = &spa->spa_l2cache; 3039 3040 if (sav->sav_count == 0) { 3041 spa_config_exit(spa, SCL_STATE, FTAG); 3042 return; 3043 } 3044 vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)]; 3045 guid0 = vd0->vdev_guid; 3046 (void) strcpy(path0, vd0->vdev_path); 3047 (void) strcpy(pathrand, vd0->vdev_path); 3048 3049 leaf = 0; 3050 leaves = 1; 3051 maxfaults = INT_MAX; /* no limit on cache devices */ 3052 } 3053 3054 spa_config_exit(spa, SCL_STATE, FTAG); 3055 3056 if (maxfaults == 0) 3057 return; 3058 3059 /* 3060 * If we can tolerate two or more faults, randomly online/offline vd0. 3061 */ 3062 if (maxfaults >= 2 && guid0 != 0) { 3063 if (ztest_random(10) < 6) { 3064 int flags = (ztest_random(2) == 0 ? 3065 ZFS_OFFLINE_TEMPORARY : 0); 3066 VERIFY(vdev_offline(spa, guid0, flags) != EBUSY); 3067 } else { 3068 (void) vdev_online(spa, guid0, 0, NULL); 3069 } 3070 } 3071 3072 /* 3073 * We have at least single-fault tolerance, so inject data corruption. 3074 */ 3075 fd = open(pathrand, O_RDWR); 3076 3077 if (fd == -1) /* we hit a gap in the device namespace */ 3078 return; 3079 3080 fsize = lseek(fd, 0, SEEK_END); 3081 3082 while (--iters != 0) { 3083 offset = ztest_random(fsize / (leaves << bshift)) * 3084 (leaves << bshift) + (leaf << bshift) + 3085 (ztest_random(1ULL << (bshift - 1)) & -8ULL); 3086 3087 if (offset >= fsize) 3088 continue; 3089 3090 if (zopt_verbose >= 6) 3091 (void) printf("injecting bad word into %s," 3092 " offset 0x%llx\n", pathrand, (u_longlong_t)offset); 3093 3094 if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad)) 3095 fatal(1, "can't inject bad word at 0x%llx in %s", 3096 offset, pathrand); 3097 } 3098 3099 (void) close(fd); 3100 } 3101 3102 /* 3103 * Scrub the pool. 3104 */ 3105 void 3106 ztest_scrub(ztest_args_t *za) 3107 { 3108 spa_t *spa = za->za_spa; 3109 3110 (void) spa_scrub(spa, POOL_SCRUB_EVERYTHING); 3111 (void) poll(NULL, 0, 1000); /* wait a second, then force a restart */ 3112 (void) spa_scrub(spa, POOL_SCRUB_EVERYTHING); 3113 } 3114 3115 /* 3116 * Rename the pool to a different name and then rename it back. 3117 */ 3118 void 3119 ztest_spa_rename(ztest_args_t *za) 3120 { 3121 char *oldname, *newname; 3122 int error; 3123 spa_t *spa; 3124 3125 (void) rw_wrlock(&ztest_shared->zs_name_lock); 3126 3127 oldname = za->za_pool; 3128 newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL); 3129 (void) strcpy(newname, oldname); 3130 (void) strcat(newname, "_tmp"); 3131 3132 /* 3133 * Do the rename 3134 */ 3135 error = spa_rename(oldname, newname); 3136 if (error) 3137 fatal(0, "spa_rename('%s', '%s') = %d", oldname, 3138 newname, error); 3139 3140 /* 3141 * Try to open it under the old name, which shouldn't exist 3142 */ 3143 error = spa_open(oldname, &spa, FTAG); 3144 if (error != ENOENT) 3145 fatal(0, "spa_open('%s') = %d", oldname, error); 3146 3147 /* 3148 * Open it under the new name and make sure it's still the same spa_t. 3149 */ 3150 error = spa_open(newname, &spa, FTAG); 3151 if (error != 0) 3152 fatal(0, "spa_open('%s') = %d", newname, error); 3153 3154 ASSERT(spa == za->za_spa); 3155 spa_close(spa, FTAG); 3156 3157 /* 3158 * Rename it back to the original 3159 */ 3160 error = spa_rename(newname, oldname); 3161 if (error) 3162 fatal(0, "spa_rename('%s', '%s') = %d", newname, 3163 oldname, error); 3164 3165 /* 3166 * Make sure it can still be opened 3167 */ 3168 error = spa_open(oldname, &spa, FTAG); 3169 if (error != 0) 3170 fatal(0, "spa_open('%s') = %d", oldname, error); 3171 3172 ASSERT(spa == za->za_spa); 3173 spa_close(spa, FTAG); 3174 3175 umem_free(newname, strlen(newname) + 1); 3176 3177 (void) rw_unlock(&ztest_shared->zs_name_lock); 3178 } 3179 3180 3181 /* 3182 * Completely obliterate one disk. 3183 */ 3184 static void 3185 ztest_obliterate_one_disk(uint64_t vdev) 3186 { 3187 int fd; 3188 char dev_name[MAXPATHLEN], copy_name[MAXPATHLEN]; 3189 size_t fsize; 3190 3191 if (zopt_maxfaults < 2) 3192 return; 3193 3194 (void) sprintf(dev_name, ztest_dev_template, zopt_dir, zopt_pool, vdev); 3195 (void) snprintf(copy_name, MAXPATHLEN, "%s.old", dev_name); 3196 3197 fd = open(dev_name, O_RDWR); 3198 3199 if (fd == -1) 3200 fatal(1, "can't open %s", dev_name); 3201 3202 /* 3203 * Determine the size. 3204 */ 3205 fsize = lseek(fd, 0, SEEK_END); 3206 3207 (void) close(fd); 3208 3209 /* 3210 * Rename the old device to dev_name.old (useful for debugging). 3211 */ 3212 VERIFY(rename(dev_name, copy_name) == 0); 3213 3214 /* 3215 * Create a new one. 3216 */ 3217 VERIFY((fd = open(dev_name, O_RDWR | O_CREAT | O_TRUNC, 0666)) >= 0); 3218 VERIFY(ftruncate(fd, fsize) == 0); 3219 (void) close(fd); 3220 } 3221 3222 static void 3223 ztest_replace_one_disk(spa_t *spa, uint64_t vdev) 3224 { 3225 char dev_name[MAXPATHLEN]; 3226 nvlist_t *root; 3227 int error; 3228 uint64_t guid; 3229 vdev_t *vd; 3230 3231 (void) sprintf(dev_name, ztest_dev_template, zopt_dir, zopt_pool, vdev); 3232 3233 /* 3234 * Build the nvlist describing dev_name. 3235 */ 3236 root = make_vdev_root(dev_name, NULL, 0, 0, 0, 0, 0, 1); 3237 3238 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); 3239 if ((vd = vdev_lookup_by_path(spa->spa_root_vdev, dev_name)) == NULL) 3240 guid = 0; 3241 else 3242 guid = vd->vdev_guid; 3243 spa_config_exit(spa, SCL_VDEV, FTAG); 3244 error = spa_vdev_attach(spa, guid, root, B_TRUE); 3245 if (error != 0 && 3246 error != EBUSY && 3247 error != ENOTSUP && 3248 error != ENODEV && 3249 error != EDOM) 3250 fatal(0, "spa_vdev_attach(in-place) = %d", error); 3251 3252 nvlist_free(root); 3253 } 3254 3255 static void 3256 ztest_verify_blocks(char *pool) 3257 { 3258 int status; 3259 char zdb[MAXPATHLEN + MAXNAMELEN + 20]; 3260 char zbuf[1024]; 3261 char *bin; 3262 char *ztest; 3263 char *isa; 3264 int isalen; 3265 FILE *fp; 3266 3267 (void) realpath(getexecname(), zdb); 3268 3269 /* zdb lives in /usr/sbin, while ztest lives in /usr/bin */ 3270 bin = strstr(zdb, "/usr/bin/"); 3271 ztest = strstr(bin, "/ztest"); 3272 isa = bin + 8; 3273 isalen = ztest - isa; 3274 isa = strdup(isa); 3275 /* LINTED */ 3276 (void) sprintf(bin, 3277 "/usr/sbin%.*s/zdb -bcc%s%s -U /tmp/zpool.cache %s", 3278 isalen, 3279 isa, 3280 zopt_verbose >= 3 ? "s" : "", 3281 zopt_verbose >= 4 ? "v" : "", 3282 pool); 3283 free(isa); 3284 3285 if (zopt_verbose >= 5) 3286 (void) printf("Executing %s\n", strstr(zdb, "zdb ")); 3287 3288 fp = popen(zdb, "r"); 3289 3290 while (fgets(zbuf, sizeof (zbuf), fp) != NULL) 3291 if (zopt_verbose >= 3) 3292 (void) printf("%s", zbuf); 3293 3294 status = pclose(fp); 3295 3296 if (status == 0) 3297 return; 3298 3299 ztest_dump_core = 0; 3300 if (WIFEXITED(status)) 3301 fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status)); 3302 else 3303 fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status)); 3304 } 3305 3306 static void 3307 ztest_walk_pool_directory(char *header) 3308 { 3309 spa_t *spa = NULL; 3310 3311 if (zopt_verbose >= 6) 3312 (void) printf("%s\n", header); 3313 3314 mutex_enter(&spa_namespace_lock); 3315 while ((spa = spa_next(spa)) != NULL) 3316 if (zopt_verbose >= 6) 3317 (void) printf("\t%s\n", spa_name(spa)); 3318 mutex_exit(&spa_namespace_lock); 3319 } 3320 3321 static void 3322 ztest_spa_import_export(char *oldname, char *newname) 3323 { 3324 nvlist_t *config, *newconfig; 3325 uint64_t pool_guid; 3326 spa_t *spa; 3327 int error; 3328 3329 if (zopt_verbose >= 4) { 3330 (void) printf("import/export: old = %s, new = %s\n", 3331 oldname, newname); 3332 } 3333 3334 /* 3335 * Clean up from previous runs. 3336 */ 3337 (void) spa_destroy(newname); 3338 3339 /* 3340 * Get the pool's configuration and guid. 3341 */ 3342 error = spa_open(oldname, &spa, FTAG); 3343 if (error) 3344 fatal(0, "spa_open('%s') = %d", oldname, error); 3345 3346 /* 3347 * Kick off a scrub to tickle scrub/export races. 3348 */ 3349 if (ztest_random(2) == 0) 3350 (void) spa_scrub(spa, POOL_SCRUB_EVERYTHING); 3351 3352 pool_guid = spa_guid(spa); 3353 spa_close(spa, FTAG); 3354 3355 ztest_walk_pool_directory("pools before export"); 3356 3357 /* 3358 * Export it. 3359 */ 3360 error = spa_export(oldname, &config, B_FALSE, B_FALSE); 3361 if (error) 3362 fatal(0, "spa_export('%s') = %d", oldname, error); 3363 3364 ztest_walk_pool_directory("pools after export"); 3365 3366 /* 3367 * Try to import it. 3368 */ 3369 newconfig = spa_tryimport(config); 3370 ASSERT(newconfig != NULL); 3371 nvlist_free(newconfig); 3372 3373 /* 3374 * Import it under the new name. 3375 */ 3376 error = spa_import(newname, config, NULL); 3377 if (error) 3378 fatal(0, "spa_import('%s') = %d", newname, error); 3379 3380 ztest_walk_pool_directory("pools after import"); 3381 3382 /* 3383 * Try to import it again -- should fail with EEXIST. 3384 */ 3385 error = spa_import(newname, config, NULL); 3386 if (error != EEXIST) 3387 fatal(0, "spa_import('%s') twice", newname); 3388 3389 /* 3390 * Try to import it under a different name -- should fail with EEXIST. 3391 */ 3392 error = spa_import(oldname, config, NULL); 3393 if (error != EEXIST) 3394 fatal(0, "spa_import('%s') under multiple names", newname); 3395 3396 /* 3397 * Verify that the pool is no longer visible under the old name. 3398 */ 3399 error = spa_open(oldname, &spa, FTAG); 3400 if (error != ENOENT) 3401 fatal(0, "spa_open('%s') = %d", newname, error); 3402 3403 /* 3404 * Verify that we can open and close the pool using the new name. 3405 */ 3406 error = spa_open(newname, &spa, FTAG); 3407 if (error) 3408 fatal(0, "spa_open('%s') = %d", newname, error); 3409 ASSERT(pool_guid == spa_guid(spa)); 3410 spa_close(spa, FTAG); 3411 3412 nvlist_free(config); 3413 } 3414 3415 static void 3416 ztest_resume(spa_t *spa) 3417 { 3418 if (spa_suspended(spa)) { 3419 spa_vdev_state_enter(spa); 3420 vdev_clear(spa, NULL); 3421 (void) spa_vdev_state_exit(spa, NULL, 0); 3422 (void) zio_resume(spa); 3423 } 3424 } 3425 3426 static void * 3427 ztest_resume_thread(void *arg) 3428 { 3429 spa_t *spa = arg; 3430 3431 while (!ztest_exiting) { 3432 (void) poll(NULL, 0, 1000); 3433 ztest_resume(spa); 3434 } 3435 return (NULL); 3436 } 3437 3438 static void * 3439 ztest_thread(void *arg) 3440 { 3441 ztest_args_t *za = arg; 3442 ztest_shared_t *zs = ztest_shared; 3443 hrtime_t now, functime; 3444 ztest_info_t *zi; 3445 int f, i; 3446 3447 while ((now = gethrtime()) < za->za_stop) { 3448 /* 3449 * See if it's time to force a crash. 3450 */ 3451 if (now > za->za_kill) { 3452 zs->zs_alloc = spa_get_alloc(za->za_spa); 3453 zs->zs_space = spa_get_space(za->za_spa); 3454 (void) kill(getpid(), SIGKILL); 3455 } 3456 3457 /* 3458 * Pick a random function. 3459 */ 3460 f = ztest_random(ZTEST_FUNCS); 3461 zi = &zs->zs_info[f]; 3462 3463 /* 3464 * Decide whether to call it, based on the requested frequency. 3465 */ 3466 if (zi->zi_call_target == 0 || 3467 (double)zi->zi_call_total / zi->zi_call_target > 3468 (double)(now - zs->zs_start_time) / (zopt_time * NANOSEC)) 3469 continue; 3470 3471 atomic_add_64(&zi->zi_calls, 1); 3472 atomic_add_64(&zi->zi_call_total, 1); 3473 3474 za->za_diroff = (za->za_instance * ZTEST_FUNCS + f) * 3475 ZTEST_DIRSIZE; 3476 za->za_diroff_shared = (1ULL << 63); 3477 3478 for (i = 0; i < zi->zi_iters; i++) 3479 zi->zi_func(za); 3480 3481 functime = gethrtime() - now; 3482 3483 atomic_add_64(&zi->zi_call_time, functime); 3484 3485 if (zopt_verbose >= 4) { 3486 Dl_info dli; 3487 (void) dladdr((void *)zi->zi_func, &dli); 3488 (void) printf("%6.2f sec in %s\n", 3489 (double)functime / NANOSEC, dli.dli_sname); 3490 } 3491 3492 /* 3493 * If we're getting ENOSPC with some regularity, stop. 3494 */ 3495 if (zs->zs_enospc_count > 10) 3496 break; 3497 } 3498 3499 return (NULL); 3500 } 3501 3502 /* 3503 * Kick off threads to run tests on all datasets in parallel. 3504 */ 3505 static void 3506 ztest_run(char *pool) 3507 { 3508 int t, d, error; 3509 ztest_shared_t *zs = ztest_shared; 3510 ztest_args_t *za; 3511 spa_t *spa; 3512 char name[100]; 3513 thread_t resume_tid; 3514 3515 ztest_exiting = B_FALSE; 3516 3517 (void) _mutex_init(&zs->zs_vdev_lock, USYNC_THREAD, NULL); 3518 (void) rwlock_init(&zs->zs_name_lock, USYNC_THREAD, NULL); 3519 3520 for (t = 0; t < ZTEST_SYNC_LOCKS; t++) 3521 (void) _mutex_init(&zs->zs_sync_lock[t], USYNC_THREAD, NULL); 3522 3523 /* 3524 * Destroy one disk before we even start. 3525 * It's mirrored, so everything should work just fine. 3526 * This makes us exercise fault handling very early in spa_load(). 3527 */ 3528 ztest_obliterate_one_disk(0); 3529 3530 /* 3531 * Verify that the sum of the sizes of all blocks in the pool 3532 * equals the SPA's allocated space total. 3533 */ 3534 ztest_verify_blocks(pool); 3535 3536 /* 3537 * Kick off a replacement of the disk we just obliterated. 3538 */ 3539 kernel_init(FREAD | FWRITE); 3540 VERIFY(spa_open(pool, &spa, FTAG) == 0); 3541 ztest_replace_one_disk(spa, 0); 3542 if (zopt_verbose >= 5) 3543 show_pool_stats(spa); 3544 spa_close(spa, FTAG); 3545 kernel_fini(); 3546 3547 kernel_init(FREAD | FWRITE); 3548 3549 /* 3550 * Verify that we can export the pool and reimport it under a 3551 * different name. 3552 */ 3553 if (ztest_random(2) == 0) { 3554 (void) snprintf(name, 100, "%s_import", pool); 3555 ztest_spa_import_export(pool, name); 3556 ztest_spa_import_export(name, pool); 3557 } 3558 3559 /* 3560 * Verify that we can loop over all pools. 3561 */ 3562 mutex_enter(&spa_namespace_lock); 3563 for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa)) { 3564 if (zopt_verbose > 3) { 3565 (void) printf("spa_next: found %s\n", spa_name(spa)); 3566 } 3567 } 3568 mutex_exit(&spa_namespace_lock); 3569 3570 /* 3571 * Open our pool. 3572 */ 3573 VERIFY(spa_open(pool, &spa, FTAG) == 0); 3574 3575 /* 3576 * We don't expect the pool to suspend unless maxfaults == 0, 3577 * in which case ztest_fault_inject() temporarily takes away 3578 * the only valid replica. 3579 */ 3580 if (zopt_maxfaults == 0) 3581 spa->spa_failmode = ZIO_FAILURE_MODE_WAIT; 3582 else 3583 spa->spa_failmode = ZIO_FAILURE_MODE_PANIC; 3584 3585 /* 3586 * Create a thread to periodically resume suspended I/O. 3587 */ 3588 VERIFY(thr_create(0, 0, ztest_resume_thread, spa, THR_BOUND, 3589 &resume_tid) == 0); 3590 3591 /* 3592 * Verify that we can safely inquire about about any object, 3593 * whether it's allocated or not. To make it interesting, 3594 * we probe a 5-wide window around each power of two. 3595 * This hits all edge cases, including zero and the max. 3596 */ 3597 for (t = 0; t < 64; t++) { 3598 for (d = -5; d <= 5; d++) { 3599 error = dmu_object_info(spa->spa_meta_objset, 3600 (1ULL << t) + d, NULL); 3601 ASSERT(error == 0 || error == ENOENT || 3602 error == EINVAL); 3603 } 3604 } 3605 3606 /* 3607 * Now kick off all the tests that run in parallel. 3608 */ 3609 zs->zs_enospc_count = 0; 3610 3611 za = umem_zalloc(zopt_threads * sizeof (ztest_args_t), UMEM_NOFAIL); 3612 3613 if (zopt_verbose >= 4) 3614 (void) printf("starting main threads...\n"); 3615 3616 za[0].za_start = gethrtime(); 3617 za[0].za_stop = za[0].za_start + zopt_passtime * NANOSEC; 3618 za[0].za_stop = MIN(za[0].za_stop, zs->zs_stop_time); 3619 za[0].za_kill = za[0].za_stop; 3620 if (ztest_random(100) < zopt_killrate) 3621 za[0].za_kill -= ztest_random(zopt_passtime * NANOSEC); 3622 3623 for (t = 0; t < zopt_threads; t++) { 3624 d = t % zopt_datasets; 3625 3626 (void) strcpy(za[t].za_pool, pool); 3627 za[t].za_os = za[d].za_os; 3628 za[t].za_spa = spa; 3629 za[t].za_zilog = za[d].za_zilog; 3630 za[t].za_instance = t; 3631 za[t].za_random = ztest_random(-1ULL); 3632 za[t].za_start = za[0].za_start; 3633 za[t].za_stop = za[0].za_stop; 3634 za[t].za_kill = za[0].za_kill; 3635 3636 if (t < zopt_datasets) { 3637 int test_future = FALSE; 3638 (void) rw_rdlock(&ztest_shared->zs_name_lock); 3639 (void) snprintf(name, 100, "%s/%s_%d", pool, pool, d); 3640 error = dmu_objset_create(name, DMU_OST_OTHER, NULL, 0, 3641 ztest_create_cb, NULL); 3642 if (error == EEXIST) { 3643 test_future = TRUE; 3644 } else if (error == ENOSPC) { 3645 zs->zs_enospc_count++; 3646 (void) rw_unlock(&ztest_shared->zs_name_lock); 3647 break; 3648 } else if (error != 0) { 3649 fatal(0, "dmu_objset_create(%s) = %d", 3650 name, error); 3651 } 3652 error = dmu_objset_open(name, DMU_OST_OTHER, 3653 DS_MODE_USER, &za[d].za_os); 3654 if (error) 3655 fatal(0, "dmu_objset_open('%s') = %d", 3656 name, error); 3657 (void) rw_unlock(&ztest_shared->zs_name_lock); 3658 if (test_future) 3659 ztest_dmu_check_future_leak(&za[t]); 3660 zil_replay(za[d].za_os, za[d].za_os, 3661 ztest_replay_vector); 3662 za[d].za_zilog = zil_open(za[d].za_os, NULL); 3663 } 3664 3665 VERIFY(thr_create(0, 0, ztest_thread, &za[t], THR_BOUND, 3666 &za[t].za_thread) == 0); 3667 } 3668 3669 while (--t >= 0) { 3670 VERIFY(thr_join(za[t].za_thread, NULL, NULL) == 0); 3671 if (t < zopt_datasets) { 3672 zil_close(za[t].za_zilog); 3673 dmu_objset_close(za[t].za_os); 3674 } 3675 } 3676 3677 if (zopt_verbose >= 3) 3678 show_pool_stats(spa); 3679 3680 txg_wait_synced(spa_get_dsl(spa), 0); 3681 3682 zs->zs_alloc = spa_get_alloc(spa); 3683 zs->zs_space = spa_get_space(spa); 3684 3685 /* 3686 * If we had out-of-space errors, destroy a random objset. 3687 */ 3688 if (zs->zs_enospc_count != 0) { 3689 (void) rw_rdlock(&ztest_shared->zs_name_lock); 3690 d = (int)ztest_random(zopt_datasets); 3691 (void) snprintf(name, 100, "%s/%s_%d", pool, pool, d); 3692 if (zopt_verbose >= 3) 3693 (void) printf("Destroying %s to free up space\n", name); 3694 (void) dmu_objset_find(name, ztest_destroy_cb, &za[d], 3695 DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN); 3696 (void) rw_unlock(&ztest_shared->zs_name_lock); 3697 } 3698 3699 txg_wait_synced(spa_get_dsl(spa), 0); 3700 3701 umem_free(za, zopt_threads * sizeof (ztest_args_t)); 3702 3703 /* Kill the resume thread */ 3704 ztest_exiting = B_TRUE; 3705 VERIFY(thr_join(resume_tid, NULL, NULL) == 0); 3706 ztest_resume(spa); 3707 3708 /* 3709 * Right before closing the pool, kick off a bunch of async I/O; 3710 * spa_close() should wait for it to complete. 3711 */ 3712 for (t = 1; t < 50; t++) 3713 dmu_prefetch(spa->spa_meta_objset, t, 0, 1 << 15); 3714 3715 spa_close(spa, FTAG); 3716 3717 kernel_fini(); 3718 } 3719 3720 void 3721 print_time(hrtime_t t, char *timebuf) 3722 { 3723 hrtime_t s = t / NANOSEC; 3724 hrtime_t m = s / 60; 3725 hrtime_t h = m / 60; 3726 hrtime_t d = h / 24; 3727 3728 s -= m * 60; 3729 m -= h * 60; 3730 h -= d * 24; 3731 3732 timebuf[0] = '\0'; 3733 3734 if (d) 3735 (void) sprintf(timebuf, 3736 "%llud%02lluh%02llum%02llus", d, h, m, s); 3737 else if (h) 3738 (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s); 3739 else if (m) 3740 (void) sprintf(timebuf, "%llum%02llus", m, s); 3741 else 3742 (void) sprintf(timebuf, "%llus", s); 3743 } 3744 3745 /* 3746 * Create a storage pool with the given name and initial vdev size. 3747 * Then create the specified number of datasets in the pool. 3748 */ 3749 static void 3750 ztest_init(char *pool) 3751 { 3752 spa_t *spa; 3753 int error; 3754 nvlist_t *nvroot; 3755 3756 kernel_init(FREAD | FWRITE); 3757 3758 /* 3759 * Create the storage pool. 3760 */ 3761 (void) spa_destroy(pool); 3762 ztest_shared->zs_vdev_primaries = 0; 3763 nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0, 3764 0, zopt_raidz, zopt_mirrors, 1); 3765 error = spa_create(pool, nvroot, NULL, NULL, NULL); 3766 nvlist_free(nvroot); 3767 3768 if (error) 3769 fatal(0, "spa_create() = %d", error); 3770 error = spa_open(pool, &spa, FTAG); 3771 if (error) 3772 fatal(0, "spa_open() = %d", error); 3773 3774 metaslab_sz = 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift; 3775 3776 if (zopt_verbose >= 3) 3777 show_pool_stats(spa); 3778 3779 spa_close(spa, FTAG); 3780 3781 kernel_fini(); 3782 } 3783 3784 int 3785 main(int argc, char **argv) 3786 { 3787 int kills = 0; 3788 int iters = 0; 3789 int i, f; 3790 ztest_shared_t *zs; 3791 ztest_info_t *zi; 3792 char timebuf[100]; 3793 char numbuf[6]; 3794 3795 (void) setvbuf(stdout, NULL, _IOLBF, 0); 3796 3797 /* Override location of zpool.cache */ 3798 spa_config_path = "/tmp/zpool.cache"; 3799 3800 ztest_random_fd = open("/dev/urandom", O_RDONLY); 3801 3802 process_options(argc, argv); 3803 3804 /* 3805 * Blow away any existing copy of zpool.cache 3806 */ 3807 if (zopt_init != 0) 3808 (void) remove("/tmp/zpool.cache"); 3809 3810 zs = ztest_shared = (void *)mmap(0, 3811 P2ROUNDUP(sizeof (ztest_shared_t), getpagesize()), 3812 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0); 3813 3814 if (zopt_verbose >= 1) { 3815 (void) printf("%llu vdevs, %d datasets, %d threads," 3816 " %llu seconds...\n", 3817 (u_longlong_t)zopt_vdevs, zopt_datasets, zopt_threads, 3818 (u_longlong_t)zopt_time); 3819 } 3820 3821 /* 3822 * Create and initialize our storage pool. 3823 */ 3824 for (i = 1; i <= zopt_init; i++) { 3825 bzero(zs, sizeof (ztest_shared_t)); 3826 if (zopt_verbose >= 3 && zopt_init != 1) 3827 (void) printf("ztest_init(), pass %d\n", i); 3828 ztest_init(zopt_pool); 3829 } 3830 3831 /* 3832 * Initialize the call targets for each function. 3833 */ 3834 for (f = 0; f < ZTEST_FUNCS; f++) { 3835 zi = &zs->zs_info[f]; 3836 3837 *zi = ztest_info[f]; 3838 3839 if (*zi->zi_interval == 0) 3840 zi->zi_call_target = UINT64_MAX; 3841 else 3842 zi->zi_call_target = zopt_time / *zi->zi_interval; 3843 } 3844 3845 zs->zs_start_time = gethrtime(); 3846 zs->zs_stop_time = zs->zs_start_time + zopt_time * NANOSEC; 3847 3848 /* 3849 * Run the tests in a loop. These tests include fault injection 3850 * to verify that self-healing data works, and forced crashes 3851 * to verify that we never lose on-disk consistency. 3852 */ 3853 while (gethrtime() < zs->zs_stop_time) { 3854 int status; 3855 pid_t pid; 3856 char *tmp; 3857 3858 /* 3859 * Initialize the workload counters for each function. 3860 */ 3861 for (f = 0; f < ZTEST_FUNCS; f++) { 3862 zi = &zs->zs_info[f]; 3863 zi->zi_calls = 0; 3864 zi->zi_call_time = 0; 3865 } 3866 3867 /* Set the allocation switch size */ 3868 metaslab_df_alloc_threshold = ztest_random(metaslab_sz / 4) + 1; 3869 3870 pid = fork(); 3871 3872 if (pid == -1) 3873 fatal(1, "fork failed"); 3874 3875 if (pid == 0) { /* child */ 3876 struct rlimit rl = { 1024, 1024 }; 3877 (void) setrlimit(RLIMIT_NOFILE, &rl); 3878 (void) enable_extended_FILE_stdio(-1, -1); 3879 ztest_run(zopt_pool); 3880 exit(0); 3881 } 3882 3883 while (waitpid(pid, &status, 0) != pid) 3884 continue; 3885 3886 if (WIFEXITED(status)) { 3887 if (WEXITSTATUS(status) != 0) { 3888 (void) fprintf(stderr, 3889 "child exited with code %d\n", 3890 WEXITSTATUS(status)); 3891 exit(2); 3892 } 3893 } else if (WIFSIGNALED(status)) { 3894 if (WTERMSIG(status) != SIGKILL) { 3895 (void) fprintf(stderr, 3896 "child died with signal %d\n", 3897 WTERMSIG(status)); 3898 exit(3); 3899 } 3900 kills++; 3901 } else { 3902 (void) fprintf(stderr, "something strange happened " 3903 "to child\n"); 3904 exit(4); 3905 } 3906 3907 iters++; 3908 3909 if (zopt_verbose >= 1) { 3910 hrtime_t now = gethrtime(); 3911 3912 now = MIN(now, zs->zs_stop_time); 3913 print_time(zs->zs_stop_time - now, timebuf); 3914 nicenum(zs->zs_space, numbuf); 3915 3916 (void) printf("Pass %3d, %8s, %3llu ENOSPC, " 3917 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n", 3918 iters, 3919 WIFEXITED(status) ? "Complete" : "SIGKILL", 3920 (u_longlong_t)zs->zs_enospc_count, 3921 100.0 * zs->zs_alloc / zs->zs_space, 3922 numbuf, 3923 100.0 * (now - zs->zs_start_time) / 3924 (zopt_time * NANOSEC), timebuf); 3925 } 3926 3927 if (zopt_verbose >= 2) { 3928 (void) printf("\nWorkload summary:\n\n"); 3929 (void) printf("%7s %9s %s\n", 3930 "Calls", "Time", "Function"); 3931 (void) printf("%7s %9s %s\n", 3932 "-----", "----", "--------"); 3933 for (f = 0; f < ZTEST_FUNCS; f++) { 3934 Dl_info dli; 3935 3936 zi = &zs->zs_info[f]; 3937 print_time(zi->zi_call_time, timebuf); 3938 (void) dladdr((void *)zi->zi_func, &dli); 3939 (void) printf("%7llu %9s %s\n", 3940 (u_longlong_t)zi->zi_calls, timebuf, 3941 dli.dli_sname); 3942 } 3943 (void) printf("\n"); 3944 } 3945 3946 /* 3947 * It's possible that we killed a child during a rename test, in 3948 * which case we'll have a 'ztest_tmp' pool lying around instead 3949 * of 'ztest'. Do a blind rename in case this happened. 3950 */ 3951 tmp = umem_alloc(strlen(zopt_pool) + 5, UMEM_NOFAIL); 3952 (void) strcpy(tmp, zopt_pool); 3953 (void) strcat(tmp, "_tmp"); 3954 kernel_init(FREAD | FWRITE); 3955 (void) spa_rename(tmp, zopt_pool); 3956 kernel_fini(); 3957 umem_free(tmp, strlen(tmp) + 1); 3958 } 3959 3960 ztest_verify_blocks(zopt_pool); 3961 3962 if (zopt_verbose >= 1) { 3963 (void) printf("%d killed, %d completed, %.0f%% kill rate\n", 3964 kills, iters - kills, (100.0 * kills) / MAX(1, iters)); 3965 } 3966 3967 return (0); 3968 } 3969