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