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