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