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