1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* 27 * The objective of this program is to provide a DMU/ZAP/SPA stress test 28 * that runs entirely in userland, is easy to use, and easy to extend. 29 * 30 * The overall design of the ztest program is as follows: 31 * 32 * (1) For each major functional area (e.g. adding vdevs to a pool, 33 * creating and destroying datasets, reading and writing objects, etc) 34 * we have a simple routine to test that functionality. These 35 * individual routines do not have to do anything "stressful". 36 * 37 * (2) We turn these simple functionality tests into a stress test by 38 * running them all in parallel, with as many threads as desired, 39 * and spread across as many datasets, objects, and vdevs as desired. 40 * 41 * (3) While all this is happening, we inject faults into the pool to 42 * verify that self-healing data really works. 43 * 44 * (4) Every time we open a dataset, we change its checksum and compression 45 * functions. Thus even individual objects vary from block to block 46 * in which checksum they use and whether they're compressed. 47 * 48 * (5) To verify that we never lose on-disk consistency after a crash, 49 * we run the entire test in a child of the main process. 50 * At random times, the child self-immolates with a SIGKILL. 51 * This is the software equivalent of pulling the power cord. 52 * The parent then runs the test again, using the existing 53 * storage pool, as many times as desired. 54 * 55 * (6) To verify that we don't have future leaks or temporal incursions, 56 * many of the functional tests record the transaction group number 57 * as part of their data. When reading old data, they verify that 58 * the transaction group number is less than the current, open txg. 59 * If you add a new test, please do this if applicable. 60 * 61 * When run with no arguments, ztest runs for about five minutes and 62 * produces no output if successful. To get a little bit of information, 63 * specify -V. To get more information, specify -VV, and so on. 64 * 65 * To turn this into an overnight stress test, use -T to specify run time. 66 * 67 * You can ask more more vdevs [-v], datasets [-d], or threads [-t] 68 * to increase the pool capacity, fanout, and overall stress level. 69 * 70 * The -N(okill) option will suppress kills, so each child runs to completion. 71 * This can be useful when you're trying to distinguish temporal incursions 72 * from plain old race conditions. 73 */ 74 75 #include <sys/zfs_context.h> 76 #include <sys/spa.h> 77 #include <sys/dmu.h> 78 #include <sys/txg.h> 79 #include <sys/dbuf.h> 80 #include <sys/zap.h> 81 #include <sys/dmu_objset.h> 82 #include <sys/poll.h> 83 #include <sys/stat.h> 84 #include <sys/time.h> 85 #include <sys/wait.h> 86 #include <sys/mman.h> 87 #include <sys/resource.h> 88 #include <sys/zio.h> 89 #include <sys/zil.h> 90 #include <sys/zil_impl.h> 91 #include <sys/vdev_impl.h> 92 #include <sys/vdev_file.h> 93 #include <sys/spa_impl.h> 94 #include <sys/metaslab_impl.h> 95 #include <sys/dsl_prop.h> 96 #include <sys/dsl_dataset.h> 97 #include <sys/refcount.h> 98 #include <stdio.h> 99 #include <stdio_ext.h> 100 #include <stdlib.h> 101 #include <unistd.h> 102 #include <signal.h> 103 #include <umem.h> 104 #include <dlfcn.h> 105 #include <ctype.h> 106 #include <math.h> 107 #include <sys/fs/zfs.h> 108 #include <libnvpair.h> 109 110 static char cmdname[] = "ztest"; 111 static char *zopt_pool = cmdname; 112 113 static uint64_t zopt_vdevs = 5; 114 static uint64_t zopt_vdevtime; 115 static int zopt_ashift = SPA_MINBLOCKSHIFT; 116 static int zopt_mirrors = 2; 117 static int zopt_raidz = 4; 118 static int zopt_raidz_parity = 1; 119 static size_t zopt_vdev_size = SPA_MINDEVSIZE; 120 static int zopt_datasets = 7; 121 static int zopt_threads = 23; 122 static uint64_t zopt_passtime = 60; /* 60 seconds */ 123 static uint64_t zopt_killrate = 70; /* 70% kill rate */ 124 static int zopt_verbose = 0; 125 static int zopt_init = 1; 126 static char *zopt_dir = "/tmp"; 127 static uint64_t zopt_time = 300; /* 5 minutes */ 128 static int zopt_maxfaults; 129 130 #define BT_MAGIC 0x123456789abcdefULL 131 132 enum ztest_io_type { 133 ZTEST_IO_WRITE_TAG, 134 ZTEST_IO_WRITE_PATTERN, 135 ZTEST_IO_WRITE_ZEROES, 136 ZTEST_IO_TRUNCATE, 137 ZTEST_IO_SETATTR, 138 ZTEST_IO_TYPES 139 }; 140 141 typedef struct ztest_block_tag { 142 uint64_t bt_magic; 143 uint64_t bt_objset; 144 uint64_t bt_object; 145 uint64_t bt_offset; 146 uint64_t bt_gen; 147 uint64_t bt_txg; 148 uint64_t bt_crtxg; 149 } ztest_block_tag_t; 150 151 typedef struct bufwad { 152 uint64_t bw_index; 153 uint64_t bw_txg; 154 uint64_t bw_data; 155 } bufwad_t; 156 157 /* 158 * XXX -- fix zfs range locks to be generic so we can use them here. 159 */ 160 typedef enum { 161 RL_READER, 162 RL_WRITER, 163 RL_APPEND 164 } rl_type_t; 165 166 typedef struct rll { 167 void *rll_writer; 168 int rll_readers; 169 mutex_t rll_lock; 170 cond_t rll_cv; 171 } rll_t; 172 173 typedef struct rl { 174 uint64_t rl_object; 175 uint64_t rl_offset; 176 uint64_t rl_size; 177 rll_t *rl_lock; 178 } rl_t; 179 180 #define ZTEST_RANGE_LOCKS 64 181 #define ZTEST_OBJECT_LOCKS 64 182 183 /* 184 * Object descriptor. Used as a template for object lookup/create/remove. 185 */ 186 typedef struct ztest_od { 187 uint64_t od_dir; 188 uint64_t od_object; 189 dmu_object_type_t od_type; 190 dmu_object_type_t od_crtype; 191 uint64_t od_blocksize; 192 uint64_t od_crblocksize; 193 uint64_t od_gen; 194 uint64_t od_crgen; 195 char od_name[MAXNAMELEN]; 196 } ztest_od_t; 197 198 /* 199 * Per-dataset state. 200 */ 201 typedef struct ztest_ds { 202 objset_t *zd_os; 203 zilog_t *zd_zilog; 204 uint64_t zd_seq; 205 ztest_od_t *zd_od; /* debugging aid */ 206 char zd_name[MAXNAMELEN]; 207 mutex_t zd_dirobj_lock; 208 rll_t zd_object_lock[ZTEST_OBJECT_LOCKS]; 209 rll_t zd_range_lock[ZTEST_RANGE_LOCKS]; 210 } ztest_ds_t; 211 212 /* 213 * Per-iteration state. 214 */ 215 typedef void ztest_func_t(ztest_ds_t *zd, uint64_t id); 216 217 typedef struct ztest_info { 218 ztest_func_t *zi_func; /* test function */ 219 uint64_t zi_iters; /* iterations per execution */ 220 uint64_t *zi_interval; /* execute every <interval> seconds */ 221 uint64_t zi_call_count; /* per-pass count */ 222 uint64_t zi_call_time; /* per-pass time */ 223 uint64_t zi_call_next; /* next time to call this function */ 224 } ztest_info_t; 225 226 /* 227 * Note: these aren't static because we want dladdr() to work. 228 */ 229 ztest_func_t ztest_dmu_read_write; 230 ztest_func_t ztest_dmu_write_parallel; 231 ztest_func_t ztest_dmu_object_alloc_free; 232 ztest_func_t ztest_dmu_commit_callbacks; 233 ztest_func_t ztest_zap; 234 ztest_func_t ztest_zap_parallel; 235 ztest_func_t ztest_zil_commit; 236 ztest_func_t ztest_dmu_read_write_zcopy; 237 ztest_func_t ztest_dmu_objset_create_destroy; 238 ztest_func_t ztest_dmu_prealloc; 239 ztest_func_t ztest_fzap; 240 ztest_func_t ztest_dmu_snapshot_create_destroy; 241 ztest_func_t ztest_dsl_prop_get_set; 242 ztest_func_t ztest_spa_prop_get_set; 243 ztest_func_t ztest_spa_create_destroy; 244 ztest_func_t ztest_fault_inject; 245 ztest_func_t ztest_ddt_repair; 246 ztest_func_t ztest_dmu_snapshot_hold; 247 ztest_func_t ztest_spa_rename; 248 ztest_func_t ztest_scrub; 249 ztest_func_t ztest_dsl_dataset_promote_busy; 250 ztest_func_t ztest_vdev_attach_detach; 251 ztest_func_t ztest_vdev_LUN_growth; 252 ztest_func_t ztest_vdev_add_remove; 253 ztest_func_t ztest_vdev_aux_add_remove; 254 255 uint64_t zopt_always = 0ULL * NANOSEC; /* all the time */ 256 uint64_t zopt_incessant = 1ULL * NANOSEC / 10; /* every 1/10 second */ 257 uint64_t zopt_often = 1ULL * NANOSEC; /* every second */ 258 uint64_t zopt_sometimes = 10ULL * NANOSEC; /* every 10 seconds */ 259 uint64_t zopt_rarely = 60ULL * NANOSEC; /* every 60 seconds */ 260 261 ztest_info_t ztest_info[] = { 262 { ztest_dmu_read_write, 1, &zopt_always }, 263 { ztest_dmu_write_parallel, 10, &zopt_always }, 264 { ztest_dmu_object_alloc_free, 1, &zopt_always }, 265 { ztest_dmu_commit_callbacks, 1, &zopt_always }, 266 { ztest_zap, 30, &zopt_always }, 267 { ztest_zap_parallel, 100, &zopt_always }, 268 { ztest_zil_commit, 1, &zopt_incessant }, 269 { ztest_dmu_read_write_zcopy, 1, &zopt_often }, 270 { ztest_dmu_objset_create_destroy, 1, &zopt_often }, 271 { ztest_dsl_prop_get_set, 1, &zopt_often }, 272 { ztest_spa_prop_get_set, 1, &zopt_sometimes }, 273 #if 0 274 { ztest_dmu_prealloc, 1, &zopt_sometimes }, 275 #endif 276 { ztest_fzap, 1, &zopt_sometimes }, 277 { ztest_dmu_snapshot_create_destroy, 1, &zopt_sometimes }, 278 { ztest_spa_create_destroy, 1, &zopt_sometimes }, 279 { ztest_fault_inject, 1, &zopt_sometimes }, 280 { ztest_ddt_repair, 1, &zopt_sometimes }, 281 { ztest_dmu_snapshot_hold, 1, &zopt_sometimes }, 282 { ztest_spa_rename, 1, &zopt_rarely }, 283 { ztest_scrub, 1, &zopt_rarely }, 284 { ztest_dsl_dataset_promote_busy, 1, &zopt_rarely }, 285 { ztest_vdev_attach_detach, 1, &zopt_rarely }, 286 { ztest_vdev_LUN_growth, 1, &zopt_rarely }, 287 { ztest_vdev_add_remove, 1, &zopt_vdevtime }, 288 { ztest_vdev_aux_add_remove, 1, &zopt_vdevtime }, 289 }; 290 291 #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t)) 292 293 /* 294 * The following struct is used to hold a list of uncalled commit callbacks. 295 * The callbacks are ordered by txg number. 296 */ 297 typedef struct ztest_cb_list { 298 mutex_t zcl_callbacks_lock; 299 list_t zcl_callbacks; 300 } ztest_cb_list_t; 301 302 /* 303 * Stuff we need to share writably between parent and child. 304 */ 305 typedef struct ztest_shared { 306 char *zs_pool; 307 spa_t *zs_spa; 308 hrtime_t zs_proc_start; 309 hrtime_t zs_proc_stop; 310 hrtime_t zs_thread_start; 311 hrtime_t zs_thread_stop; 312 hrtime_t zs_thread_kill; 313 uint64_t zs_enospc_count; 314 uint64_t zs_vdev_next_leaf; 315 uint64_t zs_vdev_aux; 316 uint64_t zs_alloc; 317 uint64_t zs_space; 318 mutex_t zs_vdev_lock; 319 rwlock_t zs_name_lock; 320 ztest_info_t zs_info[ZTEST_FUNCS]; 321 ztest_ds_t zs_zd[]; 322 } ztest_shared_t; 323 324 #define ID_PARALLEL -1ULL 325 326 static char ztest_dev_template[] = "%s/%s.%llua"; 327 static char ztest_aux_template[] = "%s/%s.%s.%llu"; 328 ztest_shared_t *ztest_shared; 329 uint64_t *ztest_seq; 330 331 static int ztest_random_fd; 332 static int ztest_dump_core = 1; 333 334 static boolean_t ztest_exiting; 335 336 /* Global commit callback list */ 337 static ztest_cb_list_t zcl; 338 339 extern uint64_t metaslab_gang_bang; 340 extern uint64_t metaslab_df_alloc_threshold; 341 static uint64_t metaslab_sz; 342 343 enum ztest_object { 344 ZTEST_META_DNODE = 0, 345 ZTEST_DIROBJ, 346 ZTEST_OBJECTS 347 }; 348 349 static void usage(boolean_t) __NORETURN; 350 351 /* 352 * These libumem hooks provide a reasonable set of defaults for the allocator's 353 * debugging facilities. 354 */ 355 const char * 356 _umem_debug_init() 357 { 358 return ("default,verbose"); /* $UMEM_DEBUG setting */ 359 } 360 361 const char * 362 _umem_logging_init(void) 363 { 364 return ("fail,contents"); /* $UMEM_LOGGING setting */ 365 } 366 367 #define FATAL_MSG_SZ 1024 368 369 char *fatal_msg; 370 371 static void 372 fatal(int do_perror, char *message, ...) 373 { 374 va_list args; 375 int save_errno = errno; 376 char buf[FATAL_MSG_SZ]; 377 378 (void) fflush(stdout); 379 380 va_start(args, message); 381 (void) sprintf(buf, "ztest: "); 382 /* LINTED */ 383 (void) vsprintf(buf + strlen(buf), message, args); 384 va_end(args); 385 if (do_perror) { 386 (void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf), 387 ": %s", strerror(save_errno)); 388 } 389 (void) fprintf(stderr, "%s\n", buf); 390 fatal_msg = buf; /* to ease debugging */ 391 if (ztest_dump_core) 392 abort(); 393 exit(3); 394 } 395 396 static int 397 str2shift(const char *buf) 398 { 399 const char *ends = "BKMGTPEZ"; 400 int i; 401 402 if (buf[0] == '\0') 403 return (0); 404 for (i = 0; i < strlen(ends); i++) { 405 if (toupper(buf[0]) == ends[i]) 406 break; 407 } 408 if (i == strlen(ends)) { 409 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", 410 buf); 411 usage(B_FALSE); 412 } 413 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) { 414 return (10*i); 415 } 416 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf); 417 usage(B_FALSE); 418 /* NOTREACHED */ 419 } 420 421 static uint64_t 422 nicenumtoull(const char *buf) 423 { 424 char *end; 425 uint64_t val; 426 427 val = strtoull(buf, &end, 0); 428 if (end == buf) { 429 (void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf); 430 usage(B_FALSE); 431 } else if (end[0] == '.') { 432 double fval = strtod(buf, &end); 433 fval *= pow(2, str2shift(end)); 434 if (fval > UINT64_MAX) { 435 (void) fprintf(stderr, "ztest: value too large: %s\n", 436 buf); 437 usage(B_FALSE); 438 } 439 val = (uint64_t)fval; 440 } else { 441 int shift = str2shift(end); 442 if (shift >= 64 || (val << shift) >> shift != val) { 443 (void) fprintf(stderr, "ztest: value too large: %s\n", 444 buf); 445 usage(B_FALSE); 446 } 447 val <<= shift; 448 } 449 return (val); 450 } 451 452 static void 453 usage(boolean_t requested) 454 { 455 char nice_vdev_size[10]; 456 char nice_gang_bang[10]; 457 FILE *fp = requested ? stdout : stderr; 458 459 nicenum(zopt_vdev_size, nice_vdev_size); 460 nicenum(metaslab_gang_bang, nice_gang_bang); 461 462 (void) fprintf(fp, "Usage: %s\n" 463 "\t[-v vdevs (default: %llu)]\n" 464 "\t[-s size_of_each_vdev (default: %s)]\n" 465 "\t[-a alignment_shift (default: %d) (use 0 for random)]\n" 466 "\t[-m mirror_copies (default: %d)]\n" 467 "\t[-r raidz_disks (default: %d)]\n" 468 "\t[-R raidz_parity (default: %d)]\n" 469 "\t[-d datasets (default: %d)]\n" 470 "\t[-t threads (default: %d)]\n" 471 "\t[-g gang_block_threshold (default: %s)]\n" 472 "\t[-i initialize pool i times (default: %d)]\n" 473 "\t[-k kill percentage (default: %llu%%)]\n" 474 "\t[-p pool_name (default: %s)]\n" 475 "\t[-f file directory for vdev files (default: %s)]\n" 476 "\t[-V(erbose)] (use multiple times for ever more blather)\n" 477 "\t[-E(xisting)] (use existing pool instead of creating new one)\n" 478 "\t[-T time] total run time (default: %llu sec)\n" 479 "\t[-P passtime] time per pass (default: %llu sec)\n" 480 "\t[-h] (print help)\n" 481 "", 482 cmdname, 483 (u_longlong_t)zopt_vdevs, /* -v */ 484 nice_vdev_size, /* -s */ 485 zopt_ashift, /* -a */ 486 zopt_mirrors, /* -m */ 487 zopt_raidz, /* -r */ 488 zopt_raidz_parity, /* -R */ 489 zopt_datasets, /* -d */ 490 zopt_threads, /* -t */ 491 nice_gang_bang, /* -g */ 492 zopt_init, /* -i */ 493 (u_longlong_t)zopt_killrate, /* -k */ 494 zopt_pool, /* -p */ 495 zopt_dir, /* -f */ 496 (u_longlong_t)zopt_time, /* -T */ 497 (u_longlong_t)zopt_passtime); /* -P */ 498 exit(requested ? 0 : 1); 499 } 500 501 static void 502 process_options(int argc, char **argv) 503 { 504 int opt; 505 uint64_t value; 506 507 /* By default, test gang blocks for blocks 32K and greater */ 508 metaslab_gang_bang = 32 << 10; 509 510 while ((opt = getopt(argc, argv, 511 "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:h")) != EOF) { 512 value = 0; 513 switch (opt) { 514 case 'v': 515 case 's': 516 case 'a': 517 case 'm': 518 case 'r': 519 case 'R': 520 case 'd': 521 case 't': 522 case 'g': 523 case 'i': 524 case 'k': 525 case 'T': 526 case 'P': 527 value = nicenumtoull(optarg); 528 } 529 switch (opt) { 530 case 'v': 531 zopt_vdevs = value; 532 break; 533 case 's': 534 zopt_vdev_size = MAX(SPA_MINDEVSIZE, value); 535 break; 536 case 'a': 537 zopt_ashift = value; 538 break; 539 case 'm': 540 zopt_mirrors = value; 541 break; 542 case 'r': 543 zopt_raidz = MAX(1, value); 544 break; 545 case 'R': 546 zopt_raidz_parity = MIN(MAX(value, 1), 3); 547 break; 548 case 'd': 549 zopt_datasets = MAX(1, value); 550 break; 551 case 't': 552 zopt_threads = MAX(1, value); 553 break; 554 case 'g': 555 metaslab_gang_bang = MAX(SPA_MINBLOCKSIZE << 1, value); 556 break; 557 case 'i': 558 zopt_init = value; 559 break; 560 case 'k': 561 zopt_killrate = value; 562 break; 563 case 'p': 564 zopt_pool = strdup(optarg); 565 break; 566 case 'f': 567 zopt_dir = strdup(optarg); 568 break; 569 case 'V': 570 zopt_verbose++; 571 break; 572 case 'E': 573 zopt_init = 0; 574 break; 575 case 'T': 576 zopt_time = value; 577 break; 578 case 'P': 579 zopt_passtime = MAX(1, value); 580 break; 581 case 'h': 582 usage(B_TRUE); 583 break; 584 case '?': 585 default: 586 usage(B_FALSE); 587 break; 588 } 589 } 590 591 zopt_raidz_parity = MIN(zopt_raidz_parity, zopt_raidz - 1); 592 593 zopt_vdevtime = (zopt_vdevs > 0 ? zopt_time * NANOSEC / zopt_vdevs : 594 UINT64_MAX >> 2); 595 zopt_maxfaults = MAX(zopt_mirrors, 1) * (zopt_raidz_parity + 1) - 1; 596 } 597 598 static void 599 ztest_kill(ztest_shared_t *zs) 600 { 601 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(zs->zs_spa)); 602 zs->zs_space = metaslab_class_get_space(spa_normal_class(zs->zs_spa)); 603 (void) kill(getpid(), SIGKILL); 604 } 605 606 static uint64_t 607 ztest_random(uint64_t range) 608 { 609 uint64_t r; 610 611 if (range == 0) 612 return (0); 613 614 if (read(ztest_random_fd, &r, sizeof (r)) != sizeof (r)) 615 fatal(1, "short read from /dev/urandom"); 616 617 return (r % range); 618 } 619 620 /* ARGSUSED */ 621 static void 622 ztest_record_enospc(const char *s) 623 { 624 ztest_shared->zs_enospc_count++; 625 } 626 627 static uint64_t 628 ztest_get_ashift(void) 629 { 630 if (zopt_ashift == 0) 631 return (SPA_MINBLOCKSHIFT + ztest_random(3)); 632 return (zopt_ashift); 633 } 634 635 static nvlist_t * 636 make_vdev_file(char *path, char *aux, size_t size, uint64_t ashift) 637 { 638 char pathbuf[MAXPATHLEN]; 639 uint64_t vdev; 640 nvlist_t *file; 641 642 if (ashift == 0) 643 ashift = ztest_get_ashift(); 644 645 if (path == NULL) { 646 path = pathbuf; 647 648 if (aux != NULL) { 649 vdev = ztest_shared->zs_vdev_aux; 650 (void) sprintf(path, ztest_aux_template, 651 zopt_dir, zopt_pool, aux, vdev); 652 } else { 653 vdev = ztest_shared->zs_vdev_next_leaf++; 654 (void) sprintf(path, ztest_dev_template, 655 zopt_dir, zopt_pool, vdev); 656 } 657 } 658 659 if (size != 0) { 660 int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666); 661 if (fd == -1) 662 fatal(1, "can't open %s", path); 663 if (ftruncate(fd, size) != 0) 664 fatal(1, "can't ftruncate %s", path); 665 (void) close(fd); 666 } 667 668 VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0); 669 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0); 670 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0); 671 VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0); 672 673 return (file); 674 } 675 676 static nvlist_t * 677 make_vdev_raidz(char *path, char *aux, size_t size, uint64_t ashift, int r) 678 { 679 nvlist_t *raidz, **child; 680 int c; 681 682 if (r < 2) 683 return (make_vdev_file(path, aux, size, ashift)); 684 child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL); 685 686 for (c = 0; c < r; c++) 687 child[c] = make_vdev_file(path, aux, size, ashift); 688 689 VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0); 690 VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE, 691 VDEV_TYPE_RAIDZ) == 0); 692 VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY, 693 zopt_raidz_parity) == 0); 694 VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN, 695 child, r) == 0); 696 697 for (c = 0; c < r; c++) 698 nvlist_free(child[c]); 699 700 umem_free(child, r * sizeof (nvlist_t *)); 701 702 return (raidz); 703 } 704 705 static nvlist_t * 706 make_vdev_mirror(char *path, char *aux, size_t size, uint64_t ashift, 707 int r, int m) 708 { 709 nvlist_t *mirror, **child; 710 int c; 711 712 if (m < 1) 713 return (make_vdev_raidz(path, aux, size, ashift, r)); 714 715 child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL); 716 717 for (c = 0; c < m; c++) 718 child[c] = make_vdev_raidz(path, aux, size, ashift, r); 719 720 VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0); 721 VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE, 722 VDEV_TYPE_MIRROR) == 0); 723 VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN, 724 child, m) == 0); 725 726 for (c = 0; c < m; c++) 727 nvlist_free(child[c]); 728 729 umem_free(child, m * sizeof (nvlist_t *)); 730 731 return (mirror); 732 } 733 734 static nvlist_t * 735 make_vdev_root(char *path, char *aux, size_t size, uint64_t ashift, 736 int log, int r, int m, int t) 737 { 738 nvlist_t *root, **child; 739 int c; 740 741 ASSERT(t > 0); 742 743 child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL); 744 745 for (c = 0; c < t; c++) { 746 child[c] = make_vdev_mirror(path, aux, size, ashift, r, m); 747 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 748 log) == 0); 749 } 750 751 VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0); 752 VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0); 753 VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN, 754 child, t) == 0); 755 756 for (c = 0; c < t; c++) 757 nvlist_free(child[c]); 758 759 umem_free(child, t * sizeof (nvlist_t *)); 760 761 return (root); 762 } 763 764 static int 765 ztest_random_blocksize(void) 766 { 767 return (1 << (SPA_MINBLOCKSHIFT + 768 ztest_random(SPA_MAXBLOCKSHIFT - SPA_MINBLOCKSHIFT + 1))); 769 } 770 771 static int 772 ztest_random_ibshift(void) 773 { 774 return (DN_MIN_INDBLKSHIFT + 775 ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1)); 776 } 777 778 static uint64_t 779 ztest_random_vdev_top(spa_t *spa, boolean_t log_ok) 780 { 781 uint64_t top; 782 vdev_t *rvd = spa->spa_root_vdev; 783 vdev_t *tvd; 784 785 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 786 787 do { 788 top = ztest_random(rvd->vdev_children); 789 tvd = rvd->vdev_child[top]; 790 } while (tvd->vdev_ishole || (tvd->vdev_islog && !log_ok) || 791 tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL); 792 793 return (top); 794 } 795 796 static uint64_t 797 ztest_random_dsl_prop(zfs_prop_t prop) 798 { 799 uint64_t value; 800 801 do { 802 value = zfs_prop_random_value(prop, ztest_random(-1ULL)); 803 } while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF); 804 805 return (value); 806 } 807 808 static int 809 ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value, 810 boolean_t inherit) 811 { 812 const char *propname = zfs_prop_to_name(prop); 813 const char *valname; 814 char setpoint[MAXPATHLEN]; 815 uint64_t curval; 816 int error; 817 818 error = dsl_prop_set(osname, propname, sizeof (value), 819 inherit ? 0 : 1, &value); 820 821 if (error == ENOSPC) { 822 ztest_record_enospc(FTAG); 823 return (error); 824 } 825 ASSERT3U(error, ==, 0); 826 827 VERIFY3U(dsl_prop_get(osname, propname, sizeof (curval), 828 1, &curval, setpoint), ==, 0); 829 830 if (zopt_verbose >= 6) { 831 VERIFY(zfs_prop_index_to_string(prop, curval, &valname) == 0); 832 (void) printf("%s %s = %s at '%s'\n", 833 osname, propname, valname, setpoint); 834 } 835 836 return (error); 837 } 838 839 #if 0 840 static int 841 ztest_spa_prop_set_uint64(ztest_shared_t *zs, zpool_prop_t prop, uint64_t value) 842 { 843 spa_t *spa = zs->zs_spa; 844 nvlist_t *props = NULL; 845 int error; 846 847 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0); 848 VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0); 849 850 error = spa_prop_set(spa, props); 851 852 nvlist_free(props); 853 854 if (error == ENOSPC) { 855 ztest_record_enospc(FTAG); 856 return (error); 857 } 858 ASSERT3U(error, ==, 0); 859 860 return (error); 861 } 862 #endif 863 864 static void 865 ztest_rll_init(rll_t *rll) 866 { 867 rll->rll_writer = NULL; 868 rll->rll_readers = 0; 869 VERIFY(_mutex_init(&rll->rll_lock, USYNC_THREAD, NULL) == 0); 870 VERIFY(cond_init(&rll->rll_cv, USYNC_THREAD, NULL) == 0); 871 } 872 873 static void 874 ztest_rll_destroy(rll_t *rll) 875 { 876 ASSERT(rll->rll_writer == NULL); 877 ASSERT(rll->rll_readers == 0); 878 VERIFY(_mutex_destroy(&rll->rll_lock) == 0); 879 VERIFY(cond_destroy(&rll->rll_cv) == 0); 880 } 881 882 static void 883 ztest_rll_lock(rll_t *rll, rl_type_t type) 884 { 885 VERIFY(mutex_lock(&rll->rll_lock) == 0); 886 887 if (type == RL_READER) { 888 while (rll->rll_writer != NULL) 889 (void) cond_wait(&rll->rll_cv, &rll->rll_lock); 890 rll->rll_readers++; 891 } else { 892 while (rll->rll_writer != NULL || rll->rll_readers) 893 (void) cond_wait(&rll->rll_cv, &rll->rll_lock); 894 rll->rll_writer = curthread; 895 } 896 897 VERIFY(mutex_unlock(&rll->rll_lock) == 0); 898 } 899 900 static void 901 ztest_rll_unlock(rll_t *rll) 902 { 903 VERIFY(mutex_lock(&rll->rll_lock) == 0); 904 905 if (rll->rll_writer) { 906 ASSERT(rll->rll_readers == 0); 907 rll->rll_writer = NULL; 908 } else { 909 ASSERT(rll->rll_readers != 0); 910 ASSERT(rll->rll_writer == NULL); 911 rll->rll_readers--; 912 } 913 914 if (rll->rll_writer == NULL && rll->rll_readers == 0) 915 VERIFY(cond_broadcast(&rll->rll_cv) == 0); 916 917 VERIFY(mutex_unlock(&rll->rll_lock) == 0); 918 } 919 920 static void 921 ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type) 922 { 923 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)]; 924 925 ztest_rll_lock(rll, type); 926 } 927 928 static void 929 ztest_object_unlock(ztest_ds_t *zd, uint64_t object) 930 { 931 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)]; 932 933 ztest_rll_unlock(rll); 934 } 935 936 static rl_t * 937 ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset, 938 uint64_t size, rl_type_t type) 939 { 940 uint64_t hash = object ^ (offset % (ZTEST_RANGE_LOCKS + 1)); 941 rll_t *rll = &zd->zd_range_lock[hash & (ZTEST_RANGE_LOCKS - 1)]; 942 rl_t *rl; 943 944 rl = umem_alloc(sizeof (*rl), UMEM_NOFAIL); 945 rl->rl_object = object; 946 rl->rl_offset = offset; 947 rl->rl_size = size; 948 rl->rl_lock = rll; 949 950 ztest_rll_lock(rll, type); 951 952 return (rl); 953 } 954 955 static void 956 ztest_range_unlock(rl_t *rl) 957 { 958 rll_t *rll = rl->rl_lock; 959 960 ztest_rll_unlock(rll); 961 962 umem_free(rl, sizeof (*rl)); 963 } 964 965 static void 966 ztest_zd_init(ztest_ds_t *zd, objset_t *os) 967 { 968 zd->zd_os = os; 969 zd->zd_zilog = dmu_objset_zil(os); 970 zd->zd_seq = 0; 971 dmu_objset_name(os, zd->zd_name); 972 973 VERIFY(_mutex_init(&zd->zd_dirobj_lock, USYNC_THREAD, NULL) == 0); 974 975 for (int l = 0; l < ZTEST_OBJECT_LOCKS; l++) 976 ztest_rll_init(&zd->zd_object_lock[l]); 977 978 for (int l = 0; l < ZTEST_RANGE_LOCKS; l++) 979 ztest_rll_init(&zd->zd_range_lock[l]); 980 } 981 982 static void 983 ztest_zd_fini(ztest_ds_t *zd) 984 { 985 VERIFY(_mutex_destroy(&zd->zd_dirobj_lock) == 0); 986 987 for (int l = 0; l < ZTEST_OBJECT_LOCKS; l++) 988 ztest_rll_destroy(&zd->zd_object_lock[l]); 989 990 for (int l = 0; l < ZTEST_RANGE_LOCKS; l++) 991 ztest_rll_destroy(&zd->zd_range_lock[l]); 992 } 993 994 #define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT) 995 996 static uint64_t 997 ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag) 998 { 999 uint64_t txg; 1000 int error; 1001 1002 /* 1003 * Attempt to assign tx to some transaction group. 1004 */ 1005 error = dmu_tx_assign(tx, txg_how); 1006 if (error) { 1007 if (error == ERESTART) { 1008 ASSERT(txg_how == TXG_NOWAIT); 1009 dmu_tx_wait(tx); 1010 } else { 1011 ASSERT3U(error, ==, ENOSPC); 1012 ztest_record_enospc(tag); 1013 } 1014 dmu_tx_abort(tx); 1015 return (0); 1016 } 1017 txg = dmu_tx_get_txg(tx); 1018 ASSERT(txg != 0); 1019 return (txg); 1020 } 1021 1022 static void 1023 ztest_pattern_set(void *buf, uint64_t size, uint64_t value) 1024 { 1025 uint64_t *ip = buf; 1026 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size); 1027 1028 while (ip < ip_end) 1029 *ip++ = value; 1030 } 1031 1032 static boolean_t 1033 ztest_pattern_match(void *buf, uint64_t size, uint64_t value) 1034 { 1035 uint64_t *ip = buf; 1036 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size); 1037 uint64_t diff = 0; 1038 1039 while (ip < ip_end) 1040 diff |= (value - *ip++); 1041 1042 return (diff == 0); 1043 } 1044 1045 static void 1046 ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object, 1047 uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg) 1048 { 1049 bt->bt_magic = BT_MAGIC; 1050 bt->bt_objset = dmu_objset_id(os); 1051 bt->bt_object = object; 1052 bt->bt_offset = offset; 1053 bt->bt_gen = gen; 1054 bt->bt_txg = txg; 1055 bt->bt_crtxg = crtxg; 1056 } 1057 1058 static void 1059 ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object, 1060 uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg) 1061 { 1062 ASSERT(bt->bt_magic == BT_MAGIC); 1063 ASSERT(bt->bt_objset == dmu_objset_id(os)); 1064 ASSERT(bt->bt_object == object); 1065 ASSERT(bt->bt_offset == offset); 1066 ASSERT(bt->bt_gen <= gen); 1067 ASSERT(bt->bt_txg <= txg); 1068 ASSERT(bt->bt_crtxg == crtxg); 1069 } 1070 1071 static ztest_block_tag_t * 1072 ztest_bt_bonus(dmu_buf_t *db) 1073 { 1074 dmu_object_info_t doi; 1075 ztest_block_tag_t *bt; 1076 1077 dmu_object_info_from_db(db, &doi); 1078 ASSERT3U(doi.doi_bonus_size, <=, db->db_size); 1079 ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt)); 1080 bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt)); 1081 1082 return (bt); 1083 } 1084 1085 /* 1086 * ZIL logging ops 1087 */ 1088 1089 #define lrz_type lr_mode 1090 #define lrz_blocksize lr_uid 1091 #define lrz_ibshift lr_gid 1092 #define lrz_bonustype lr_rdev 1093 #define lrz_bonuslen lr_crtime[1] 1094 1095 static uint64_t 1096 ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr) 1097 { 1098 char *name = (void *)(lr + 1); /* name follows lr */ 1099 size_t namesize = strlen(name) + 1; 1100 itx_t *itx; 1101 1102 if (zil_replaying(zd->zd_zilog, tx)) 1103 return (0); 1104 1105 itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize); 1106 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1, 1107 sizeof (*lr) + namesize - sizeof (lr_t)); 1108 1109 return (zil_itx_assign(zd->zd_zilog, itx, tx)); 1110 } 1111 1112 static uint64_t 1113 ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr) 1114 { 1115 char *name = (void *)(lr + 1); /* name follows lr */ 1116 size_t namesize = strlen(name) + 1; 1117 itx_t *itx; 1118 1119 if (zil_replaying(zd->zd_zilog, tx)) 1120 return (0); 1121 1122 itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize); 1123 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1, 1124 sizeof (*lr) + namesize - sizeof (lr_t)); 1125 1126 return (zil_itx_assign(zd->zd_zilog, itx, tx)); 1127 } 1128 1129 static uint64_t 1130 ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr) 1131 { 1132 itx_t *itx; 1133 itx_wr_state_t write_state = ztest_random(WR_NUM_STATES); 1134 1135 if (zil_replaying(zd->zd_zilog, tx)) 1136 return (0); 1137 1138 if (lr->lr_length > ZIL_MAX_LOG_DATA) 1139 write_state = WR_INDIRECT; 1140 1141 itx = zil_itx_create(TX_WRITE, 1142 sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0)); 1143 1144 if (write_state == WR_COPIED && 1145 dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length, 1146 ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) { 1147 zil_itx_destroy(itx); 1148 itx = zil_itx_create(TX_WRITE, sizeof (*lr)); 1149 write_state = WR_NEED_COPY; 1150 } 1151 itx->itx_private = zd; 1152 itx->itx_wr_state = write_state; 1153 itx->itx_sync = (ztest_random(8) == 0); 1154 itx->itx_sod += (write_state == WR_NEED_COPY ? lr->lr_length : 0); 1155 1156 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1, 1157 sizeof (*lr) - sizeof (lr_t)); 1158 1159 return (zil_itx_assign(zd->zd_zilog, itx, tx)); 1160 } 1161 1162 static uint64_t 1163 ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr) 1164 { 1165 itx_t *itx; 1166 1167 if (zil_replaying(zd->zd_zilog, tx)) 1168 return (0); 1169 1170 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr)); 1171 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1, 1172 sizeof (*lr) - sizeof (lr_t)); 1173 1174 return (zil_itx_assign(zd->zd_zilog, itx, tx)); 1175 } 1176 1177 static uint64_t 1178 ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr) 1179 { 1180 itx_t *itx; 1181 1182 if (zil_replaying(zd->zd_zilog, tx)) 1183 return (0); 1184 1185 itx = zil_itx_create(TX_SETATTR, sizeof (*lr)); 1186 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1, 1187 sizeof (*lr) - sizeof (lr_t)); 1188 1189 return (zil_itx_assign(zd->zd_zilog, itx, tx)); 1190 } 1191 1192 /* 1193 * ZIL replay ops 1194 */ 1195 static int 1196 ztest_replay_create(ztest_ds_t *zd, lr_create_t *lr, boolean_t byteswap) 1197 { 1198 char *name = (void *)(lr + 1); /* name follows lr */ 1199 objset_t *os = zd->zd_os; 1200 ztest_block_tag_t *bbt; 1201 dmu_buf_t *db; 1202 dmu_tx_t *tx; 1203 uint64_t txg; 1204 int error = 0; 1205 1206 if (byteswap) 1207 byteswap_uint64_array(lr, sizeof (*lr)); 1208 1209 ASSERT(lr->lr_doid == ZTEST_DIROBJ); 1210 ASSERT(name[0] != '\0'); 1211 1212 tx = dmu_tx_create(os); 1213 1214 dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name); 1215 1216 if (lr->lrz_type == DMU_OT_ZAP_OTHER) { 1217 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL); 1218 } else { 1219 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 1220 } 1221 1222 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG); 1223 if (txg == 0) 1224 return (ENOSPC); 1225 1226 ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid); 1227 1228 if (lr->lrz_type == DMU_OT_ZAP_OTHER) { 1229 if (lr->lr_foid == 0) { 1230 lr->lr_foid = zap_create(os, 1231 lr->lrz_type, lr->lrz_bonustype, 1232 lr->lrz_bonuslen, tx); 1233 } else { 1234 error = zap_create_claim(os, lr->lr_foid, 1235 lr->lrz_type, lr->lrz_bonustype, 1236 lr->lrz_bonuslen, tx); 1237 } 1238 } else { 1239 if (lr->lr_foid == 0) { 1240 lr->lr_foid = dmu_object_alloc(os, 1241 lr->lrz_type, 0, lr->lrz_bonustype, 1242 lr->lrz_bonuslen, tx); 1243 } else { 1244 error = dmu_object_claim(os, lr->lr_foid, 1245 lr->lrz_type, 0, lr->lrz_bonustype, 1246 lr->lrz_bonuslen, tx); 1247 } 1248 } 1249 1250 if (error) { 1251 ASSERT3U(error, ==, EEXIST); 1252 ASSERT(zd->zd_zilog->zl_replay); 1253 dmu_tx_commit(tx); 1254 return (error); 1255 } 1256 1257 ASSERT(lr->lr_foid != 0); 1258 1259 if (lr->lrz_type != DMU_OT_ZAP_OTHER) 1260 VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid, 1261 lr->lrz_blocksize, lr->lrz_ibshift, tx)); 1262 1263 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db)); 1264 bbt = ztest_bt_bonus(db); 1265 dmu_buf_will_dirty(db, tx); 1266 ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_gen, txg, txg); 1267 dmu_buf_rele(db, FTAG); 1268 1269 VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1, 1270 &lr->lr_foid, tx)); 1271 1272 (void) ztest_log_create(zd, tx, lr); 1273 1274 dmu_tx_commit(tx); 1275 1276 return (0); 1277 } 1278 1279 static int 1280 ztest_replay_remove(ztest_ds_t *zd, lr_remove_t *lr, boolean_t byteswap) 1281 { 1282 char *name = (void *)(lr + 1); /* name follows lr */ 1283 objset_t *os = zd->zd_os; 1284 dmu_object_info_t doi; 1285 dmu_tx_t *tx; 1286 uint64_t object, txg; 1287 1288 if (byteswap) 1289 byteswap_uint64_array(lr, sizeof (*lr)); 1290 1291 ASSERT(lr->lr_doid == ZTEST_DIROBJ); 1292 ASSERT(name[0] != '\0'); 1293 1294 VERIFY3U(0, ==, 1295 zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object)); 1296 ASSERT(object != 0); 1297 1298 ztest_object_lock(zd, object, RL_WRITER); 1299 1300 VERIFY3U(0, ==, dmu_object_info(os, object, &doi)); 1301 1302 tx = dmu_tx_create(os); 1303 1304 dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name); 1305 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END); 1306 1307 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG); 1308 if (txg == 0) { 1309 ztest_object_unlock(zd, object); 1310 return (ENOSPC); 1311 } 1312 1313 if (doi.doi_type == DMU_OT_ZAP_OTHER) { 1314 VERIFY3U(0, ==, zap_destroy(os, object, tx)); 1315 } else { 1316 VERIFY3U(0, ==, dmu_object_free(os, object, tx)); 1317 } 1318 1319 VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx)); 1320 1321 (void) ztest_log_remove(zd, tx, lr); 1322 1323 dmu_tx_commit(tx); 1324 1325 ztest_object_unlock(zd, object); 1326 1327 return (0); 1328 } 1329 1330 static int 1331 ztest_replay_write(ztest_ds_t *zd, lr_write_t *lr, boolean_t byteswap) 1332 { 1333 objset_t *os = zd->zd_os; 1334 void *data = lr + 1; /* data follows lr */ 1335 uint64_t offset, length; 1336 ztest_block_tag_t *bt = data; 1337 ztest_block_tag_t *bbt; 1338 uint64_t gen, txg, lrtxg, crtxg; 1339 dmu_object_info_t doi; 1340 dmu_tx_t *tx; 1341 dmu_buf_t *db; 1342 arc_buf_t *abuf = NULL; 1343 rl_t *rl; 1344 1345 if (byteswap) 1346 byteswap_uint64_array(lr, sizeof (*lr)); 1347 1348 offset = lr->lr_offset; 1349 length = lr->lr_length; 1350 1351 /* If it's a dmu_sync() block, write the whole block */ 1352 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) { 1353 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr); 1354 if (length < blocksize) { 1355 offset -= offset % blocksize; 1356 length = blocksize; 1357 } 1358 } 1359 1360 if (bt->bt_magic == BSWAP_64(BT_MAGIC)) 1361 byteswap_uint64_array(bt, sizeof (*bt)); 1362 1363 if (bt->bt_magic != BT_MAGIC) 1364 bt = NULL; 1365 1366 ztest_object_lock(zd, lr->lr_foid, RL_READER); 1367 rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER); 1368 1369 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db)); 1370 1371 dmu_object_info_from_db(db, &doi); 1372 1373 bbt = ztest_bt_bonus(db); 1374 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC); 1375 gen = bbt->bt_gen; 1376 crtxg = bbt->bt_crtxg; 1377 lrtxg = lr->lr_common.lrc_txg; 1378 1379 tx = dmu_tx_create(os); 1380 1381 dmu_tx_hold_write(tx, lr->lr_foid, offset, length); 1382 1383 if (ztest_random(8) == 0 && length == doi.doi_data_block_size && 1384 P2PHASE(offset, length) == 0) 1385 abuf = dmu_request_arcbuf(db, length); 1386 1387 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG); 1388 if (txg == 0) { 1389 if (abuf != NULL) 1390 dmu_return_arcbuf(abuf); 1391 dmu_buf_rele(db, FTAG); 1392 ztest_range_unlock(rl); 1393 ztest_object_unlock(zd, lr->lr_foid); 1394 return (ENOSPC); 1395 } 1396 1397 if (bt != NULL) { 1398 /* 1399 * Usually, verify the old data before writing new data -- 1400 * but not always, because we also want to verify correct 1401 * behavior when the data was not recently read into cache. 1402 */ 1403 ASSERT(offset % doi.doi_data_block_size == 0); 1404 if (ztest_random(4) != 0) { 1405 int prefetch = ztest_random(2) ? 1406 DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH; 1407 ztest_block_tag_t rbt; 1408 1409 VERIFY(dmu_read(os, lr->lr_foid, offset, 1410 sizeof (rbt), &rbt, prefetch) == 0); 1411 if (rbt.bt_magic == BT_MAGIC) { 1412 ztest_bt_verify(&rbt, os, lr->lr_foid, 1413 offset, gen, txg, crtxg); 1414 } 1415 } 1416 1417 /* 1418 * Writes can appear to be newer than the bonus buffer because 1419 * the ztest_get_data() callback does a dmu_read() of the 1420 * open-context data, which may be different than the data 1421 * as it was when the write was generated. 1422 */ 1423 if (zd->zd_zilog->zl_replay) { 1424 ztest_bt_verify(bt, os, lr->lr_foid, offset, 1425 MAX(gen, bt->bt_gen), MAX(txg, lrtxg), 1426 bt->bt_crtxg); 1427 } 1428 1429 /* 1430 * Set the bt's gen/txg to the bonus buffer's gen/txg 1431 * so that all of the usual ASSERTs will work. 1432 */ 1433 ztest_bt_generate(bt, os, lr->lr_foid, offset, gen, txg, crtxg); 1434 } 1435 1436 if (abuf == NULL) { 1437 dmu_write(os, lr->lr_foid, offset, length, data, tx); 1438 } else { 1439 bcopy(data, abuf->b_data, length); 1440 dmu_assign_arcbuf(db, offset, abuf, tx); 1441 } 1442 1443 (void) ztest_log_write(zd, tx, lr); 1444 1445 dmu_buf_rele(db, FTAG); 1446 1447 dmu_tx_commit(tx); 1448 1449 ztest_range_unlock(rl); 1450 ztest_object_unlock(zd, lr->lr_foid); 1451 1452 return (0); 1453 } 1454 1455 static int 1456 ztest_replay_truncate(ztest_ds_t *zd, lr_truncate_t *lr, boolean_t byteswap) 1457 { 1458 objset_t *os = zd->zd_os; 1459 dmu_tx_t *tx; 1460 uint64_t txg; 1461 rl_t *rl; 1462 1463 if (byteswap) 1464 byteswap_uint64_array(lr, sizeof (*lr)); 1465 1466 ztest_object_lock(zd, lr->lr_foid, RL_READER); 1467 rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length, 1468 RL_WRITER); 1469 1470 tx = dmu_tx_create(os); 1471 1472 dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length); 1473 1474 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG); 1475 if (txg == 0) { 1476 ztest_range_unlock(rl); 1477 ztest_object_unlock(zd, lr->lr_foid); 1478 return (ENOSPC); 1479 } 1480 1481 VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset, 1482 lr->lr_length, tx) == 0); 1483 1484 (void) ztest_log_truncate(zd, tx, lr); 1485 1486 dmu_tx_commit(tx); 1487 1488 ztest_range_unlock(rl); 1489 ztest_object_unlock(zd, lr->lr_foid); 1490 1491 return (0); 1492 } 1493 1494 static int 1495 ztest_replay_setattr(ztest_ds_t *zd, lr_setattr_t *lr, boolean_t byteswap) 1496 { 1497 objset_t *os = zd->zd_os; 1498 dmu_tx_t *tx; 1499 dmu_buf_t *db; 1500 ztest_block_tag_t *bbt; 1501 uint64_t txg, lrtxg, crtxg; 1502 1503 if (byteswap) 1504 byteswap_uint64_array(lr, sizeof (*lr)); 1505 1506 ztest_object_lock(zd, lr->lr_foid, RL_WRITER); 1507 1508 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db)); 1509 1510 tx = dmu_tx_create(os); 1511 dmu_tx_hold_bonus(tx, lr->lr_foid); 1512 1513 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG); 1514 if (txg == 0) { 1515 dmu_buf_rele(db, FTAG); 1516 ztest_object_unlock(zd, lr->lr_foid); 1517 return (ENOSPC); 1518 } 1519 1520 bbt = ztest_bt_bonus(db); 1521 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC); 1522 crtxg = bbt->bt_crtxg; 1523 lrtxg = lr->lr_common.lrc_txg; 1524 1525 if (zd->zd_zilog->zl_replay) { 1526 ASSERT(lr->lr_size != 0); 1527 ASSERT(lr->lr_mode != 0); 1528 ASSERT(lrtxg != 0); 1529 } else { 1530 /* 1531 * Randomly change the size and increment the generation. 1532 */ 1533 lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) * 1534 sizeof (*bbt); 1535 lr->lr_mode = bbt->bt_gen + 1; 1536 ASSERT(lrtxg == 0); 1537 } 1538 1539 /* 1540 * Verify that the current bonus buffer is not newer than our txg. 1541 */ 1542 ztest_bt_verify(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode, 1543 MAX(txg, lrtxg), crtxg); 1544 1545 dmu_buf_will_dirty(db, tx); 1546 1547 ASSERT3U(lr->lr_size, >=, sizeof (*bbt)); 1548 ASSERT3U(lr->lr_size, <=, db->db_size); 1549 VERIFY3U(dmu_set_bonus(db, lr->lr_size, tx), ==, 0); 1550 bbt = ztest_bt_bonus(db); 1551 1552 ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode, txg, crtxg); 1553 1554 dmu_buf_rele(db, FTAG); 1555 1556 (void) ztest_log_setattr(zd, tx, lr); 1557 1558 dmu_tx_commit(tx); 1559 1560 ztest_object_unlock(zd, lr->lr_foid); 1561 1562 return (0); 1563 } 1564 1565 zil_replay_func_t *ztest_replay_vector[TX_MAX_TYPE] = { 1566 NULL, /* 0 no such transaction type */ 1567 ztest_replay_create, /* TX_CREATE */ 1568 NULL, /* TX_MKDIR */ 1569 NULL, /* TX_MKXATTR */ 1570 NULL, /* TX_SYMLINK */ 1571 ztest_replay_remove, /* TX_REMOVE */ 1572 NULL, /* TX_RMDIR */ 1573 NULL, /* TX_LINK */ 1574 NULL, /* TX_RENAME */ 1575 ztest_replay_write, /* TX_WRITE */ 1576 ztest_replay_truncate, /* TX_TRUNCATE */ 1577 ztest_replay_setattr, /* TX_SETATTR */ 1578 NULL, /* TX_ACL */ 1579 NULL, /* TX_CREATE_ACL */ 1580 NULL, /* TX_CREATE_ATTR */ 1581 NULL, /* TX_CREATE_ACL_ATTR */ 1582 NULL, /* TX_MKDIR_ACL */ 1583 NULL, /* TX_MKDIR_ATTR */ 1584 NULL, /* TX_MKDIR_ACL_ATTR */ 1585 NULL, /* TX_WRITE2 */ 1586 }; 1587 1588 /* 1589 * ZIL get_data callbacks 1590 */ 1591 1592 static void 1593 ztest_get_done(zgd_t *zgd, int error) 1594 { 1595 ztest_ds_t *zd = zgd->zgd_private; 1596 uint64_t object = zgd->zgd_rl->rl_object; 1597 1598 if (zgd->zgd_db) 1599 dmu_buf_rele(zgd->zgd_db, zgd); 1600 1601 ztest_range_unlock(zgd->zgd_rl); 1602 ztest_object_unlock(zd, object); 1603 1604 if (error == 0 && zgd->zgd_bp) 1605 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp); 1606 1607 umem_free(zgd, sizeof (*zgd)); 1608 } 1609 1610 static int 1611 ztest_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio) 1612 { 1613 ztest_ds_t *zd = arg; 1614 objset_t *os = zd->zd_os; 1615 uint64_t object = lr->lr_foid; 1616 uint64_t offset = lr->lr_offset; 1617 uint64_t size = lr->lr_length; 1618 blkptr_t *bp = &lr->lr_blkptr; 1619 uint64_t txg = lr->lr_common.lrc_txg; 1620 uint64_t crtxg; 1621 dmu_object_info_t doi; 1622 dmu_buf_t *db; 1623 zgd_t *zgd; 1624 int error; 1625 1626 ztest_object_lock(zd, object, RL_READER); 1627 error = dmu_bonus_hold(os, object, FTAG, &db); 1628 if (error) { 1629 ztest_object_unlock(zd, object); 1630 return (error); 1631 } 1632 1633 crtxg = ztest_bt_bonus(db)->bt_crtxg; 1634 1635 if (crtxg == 0 || crtxg > txg) { 1636 dmu_buf_rele(db, FTAG); 1637 ztest_object_unlock(zd, object); 1638 return (ENOENT); 1639 } 1640 1641 dmu_object_info_from_db(db, &doi); 1642 dmu_buf_rele(db, FTAG); 1643 db = NULL; 1644 1645 zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL); 1646 zgd->zgd_zilog = zd->zd_zilog; 1647 zgd->zgd_private = zd; 1648 1649 if (buf != NULL) { /* immediate write */ 1650 zgd->zgd_rl = ztest_range_lock(zd, object, offset, size, 1651 RL_READER); 1652 1653 error = dmu_read(os, object, offset, size, buf, 1654 DMU_READ_NO_PREFETCH); 1655 ASSERT(error == 0); 1656 } else { 1657 size = doi.doi_data_block_size; 1658 if (ISP2(size)) 1659 offset = P2ALIGN(offset, size); 1660 1661 zgd->zgd_rl = ztest_range_lock(zd, object, offset, size, 1662 RL_READER); 1663 1664 error = dmu_buf_hold(os, object, offset, zgd, &db); 1665 1666 if (error == 0) { 1667 zgd->zgd_db = db; 1668 zgd->zgd_bp = bp; 1669 1670 ASSERT(db->db_offset == offset); 1671 ASSERT(db->db_size == size); 1672 1673 error = dmu_sync(zio, lr->lr_common.lrc_txg, 1674 ztest_get_done, zgd); 1675 1676 if (error == 0) 1677 return (0); 1678 } 1679 } 1680 1681 ztest_get_done(zgd, error); 1682 1683 return (error); 1684 } 1685 1686 static void * 1687 ztest_lr_alloc(size_t lrsize, char *name) 1688 { 1689 char *lr; 1690 size_t namesize = name ? strlen(name) + 1 : 0; 1691 1692 lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL); 1693 1694 if (name) 1695 bcopy(name, lr + lrsize, namesize); 1696 1697 return (lr); 1698 } 1699 1700 void 1701 ztest_lr_free(void *lr, size_t lrsize, char *name) 1702 { 1703 size_t namesize = name ? strlen(name) + 1 : 0; 1704 1705 umem_free(lr, lrsize + namesize); 1706 } 1707 1708 /* 1709 * Lookup a bunch of objects. Returns the number of objects not found. 1710 */ 1711 static int 1712 ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count) 1713 { 1714 int missing = 0; 1715 int error; 1716 1717 ASSERT(_mutex_held(&zd->zd_dirobj_lock)); 1718 1719 for (int i = 0; i < count; i++, od++) { 1720 od->od_object = 0; 1721 error = zap_lookup(zd->zd_os, od->od_dir, od->od_name, 1722 sizeof (uint64_t), 1, &od->od_object); 1723 if (error) { 1724 ASSERT(error == ENOENT); 1725 ASSERT(od->od_object == 0); 1726 missing++; 1727 } else { 1728 dmu_buf_t *db; 1729 ztest_block_tag_t *bbt; 1730 dmu_object_info_t doi; 1731 1732 ASSERT(od->od_object != 0); 1733 ASSERT(missing == 0); /* there should be no gaps */ 1734 1735 ztest_object_lock(zd, od->od_object, RL_READER); 1736 VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os, 1737 od->od_object, FTAG, &db)); 1738 dmu_object_info_from_db(db, &doi); 1739 bbt = ztest_bt_bonus(db); 1740 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC); 1741 od->od_type = doi.doi_type; 1742 od->od_blocksize = doi.doi_data_block_size; 1743 od->od_gen = bbt->bt_gen; 1744 dmu_buf_rele(db, FTAG); 1745 ztest_object_unlock(zd, od->od_object); 1746 } 1747 } 1748 1749 return (missing); 1750 } 1751 1752 static int 1753 ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count) 1754 { 1755 int missing = 0; 1756 1757 ASSERT(_mutex_held(&zd->zd_dirobj_lock)); 1758 1759 for (int i = 0; i < count; i++, od++) { 1760 if (missing) { 1761 od->od_object = 0; 1762 missing++; 1763 continue; 1764 } 1765 1766 lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name); 1767 1768 lr->lr_doid = od->od_dir; 1769 lr->lr_foid = 0; /* 0 to allocate, > 0 to claim */ 1770 lr->lrz_type = od->od_crtype; 1771 lr->lrz_blocksize = od->od_crblocksize; 1772 lr->lrz_ibshift = ztest_random_ibshift(); 1773 lr->lrz_bonustype = DMU_OT_UINT64_OTHER; 1774 lr->lrz_bonuslen = dmu_bonus_max(); 1775 lr->lr_gen = od->od_crgen; 1776 lr->lr_crtime[0] = time(NULL); 1777 1778 if (ztest_replay_create(zd, lr, B_FALSE) != 0) { 1779 ASSERT(missing == 0); 1780 od->od_object = 0; 1781 missing++; 1782 } else { 1783 od->od_object = lr->lr_foid; 1784 od->od_type = od->od_crtype; 1785 od->od_blocksize = od->od_crblocksize; 1786 od->od_gen = od->od_crgen; 1787 ASSERT(od->od_object != 0); 1788 } 1789 1790 ztest_lr_free(lr, sizeof (*lr), od->od_name); 1791 } 1792 1793 return (missing); 1794 } 1795 1796 static int 1797 ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count) 1798 { 1799 int missing = 0; 1800 int error; 1801 1802 ASSERT(_mutex_held(&zd->zd_dirobj_lock)); 1803 1804 od += count - 1; 1805 1806 for (int i = count - 1; i >= 0; i--, od--) { 1807 if (missing) { 1808 missing++; 1809 continue; 1810 } 1811 1812 if (od->od_object == 0) 1813 continue; 1814 1815 lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name); 1816 1817 lr->lr_doid = od->od_dir; 1818 1819 if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) { 1820 ASSERT3U(error, ==, ENOSPC); 1821 missing++; 1822 } else { 1823 od->od_object = 0; 1824 } 1825 ztest_lr_free(lr, sizeof (*lr), od->od_name); 1826 } 1827 1828 return (missing); 1829 } 1830 1831 static int 1832 ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size, 1833 void *data) 1834 { 1835 lr_write_t *lr; 1836 int error; 1837 1838 lr = ztest_lr_alloc(sizeof (*lr) + size, NULL); 1839 1840 lr->lr_foid = object; 1841 lr->lr_offset = offset; 1842 lr->lr_length = size; 1843 lr->lr_blkoff = 0; 1844 BP_ZERO(&lr->lr_blkptr); 1845 1846 bcopy(data, lr + 1, size); 1847 1848 error = ztest_replay_write(zd, lr, B_FALSE); 1849 1850 ztest_lr_free(lr, sizeof (*lr) + size, NULL); 1851 1852 return (error); 1853 } 1854 1855 static int 1856 ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size) 1857 { 1858 lr_truncate_t *lr; 1859 int error; 1860 1861 lr = ztest_lr_alloc(sizeof (*lr), NULL); 1862 1863 lr->lr_foid = object; 1864 lr->lr_offset = offset; 1865 lr->lr_length = size; 1866 1867 error = ztest_replay_truncate(zd, lr, B_FALSE); 1868 1869 ztest_lr_free(lr, sizeof (*lr), NULL); 1870 1871 return (error); 1872 } 1873 1874 static int 1875 ztest_setattr(ztest_ds_t *zd, uint64_t object) 1876 { 1877 lr_setattr_t *lr; 1878 int error; 1879 1880 lr = ztest_lr_alloc(sizeof (*lr), NULL); 1881 1882 lr->lr_foid = object; 1883 lr->lr_size = 0; 1884 lr->lr_mode = 0; 1885 1886 error = ztest_replay_setattr(zd, lr, B_FALSE); 1887 1888 ztest_lr_free(lr, sizeof (*lr), NULL); 1889 1890 return (error); 1891 } 1892 1893 static void 1894 ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size) 1895 { 1896 objset_t *os = zd->zd_os; 1897 dmu_tx_t *tx; 1898 uint64_t txg; 1899 rl_t *rl; 1900 1901 txg_wait_synced(dmu_objset_pool(os), 0); 1902 1903 ztest_object_lock(zd, object, RL_READER); 1904 rl = ztest_range_lock(zd, object, offset, size, RL_WRITER); 1905 1906 tx = dmu_tx_create(os); 1907 1908 dmu_tx_hold_write(tx, object, offset, size); 1909 1910 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG); 1911 1912 if (txg != 0) { 1913 dmu_prealloc(os, object, offset, size, tx); 1914 dmu_tx_commit(tx); 1915 txg_wait_synced(dmu_objset_pool(os), txg); 1916 } else { 1917 (void) dmu_free_long_range(os, object, offset, size); 1918 } 1919 1920 ztest_range_unlock(rl); 1921 ztest_object_unlock(zd, object); 1922 } 1923 1924 static void 1925 ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset) 1926 { 1927 ztest_block_tag_t wbt; 1928 dmu_object_info_t doi; 1929 enum ztest_io_type io_type; 1930 uint64_t blocksize; 1931 void *data; 1932 1933 VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0); 1934 blocksize = doi.doi_data_block_size; 1935 data = umem_alloc(blocksize, UMEM_NOFAIL); 1936 1937 /* 1938 * Pick an i/o type at random, biased toward writing block tags. 1939 */ 1940 io_type = ztest_random(ZTEST_IO_TYPES); 1941 if (ztest_random(2) == 0) 1942 io_type = ZTEST_IO_WRITE_TAG; 1943 1944 switch (io_type) { 1945 1946 case ZTEST_IO_WRITE_TAG: 1947 ztest_bt_generate(&wbt, zd->zd_os, object, offset, 0, 0, 0); 1948 (void) ztest_write(zd, object, offset, sizeof (wbt), &wbt); 1949 break; 1950 1951 case ZTEST_IO_WRITE_PATTERN: 1952 (void) memset(data, 'a' + (object + offset) % 5, blocksize); 1953 if (ztest_random(2) == 0) { 1954 /* 1955 * Induce fletcher2 collisions to ensure that 1956 * zio_ddt_collision() detects and resolves them 1957 * when using fletcher2-verify for deduplication. 1958 */ 1959 ((uint64_t *)data)[0] ^= 1ULL << 63; 1960 ((uint64_t *)data)[4] ^= 1ULL << 63; 1961 } 1962 (void) ztest_write(zd, object, offset, blocksize, data); 1963 break; 1964 1965 case ZTEST_IO_WRITE_ZEROES: 1966 bzero(data, blocksize); 1967 (void) ztest_write(zd, object, offset, blocksize, data); 1968 break; 1969 1970 case ZTEST_IO_TRUNCATE: 1971 (void) ztest_truncate(zd, object, offset, blocksize); 1972 break; 1973 1974 case ZTEST_IO_SETATTR: 1975 (void) ztest_setattr(zd, object); 1976 break; 1977 } 1978 1979 umem_free(data, blocksize); 1980 } 1981 1982 /* 1983 * Initialize an object description template. 1984 */ 1985 static void 1986 ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index, 1987 dmu_object_type_t type, uint64_t blocksize, uint64_t gen) 1988 { 1989 od->od_dir = ZTEST_DIROBJ; 1990 od->od_object = 0; 1991 1992 od->od_crtype = type; 1993 od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize(); 1994 od->od_crgen = gen; 1995 1996 od->od_type = DMU_OT_NONE; 1997 od->od_blocksize = 0; 1998 od->od_gen = 0; 1999 2000 (void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]", 2001 tag, (int64_t)id, index); 2002 } 2003 2004 /* 2005 * Lookup or create the objects for a test using the od template. 2006 * If the objects do not all exist, or if 'remove' is specified, 2007 * remove any existing objects and create new ones. Otherwise, 2008 * use the existing objects. 2009 */ 2010 static int 2011 ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove) 2012 { 2013 int count = size / sizeof (*od); 2014 int rv = 0; 2015 2016 VERIFY(mutex_lock(&zd->zd_dirobj_lock) == 0); 2017 if ((ztest_lookup(zd, od, count) != 0 || remove) && 2018 (ztest_remove(zd, od, count) != 0 || 2019 ztest_create(zd, od, count) != 0)) 2020 rv = -1; 2021 zd->zd_od = od; 2022 VERIFY(mutex_unlock(&zd->zd_dirobj_lock) == 0); 2023 2024 return (rv); 2025 } 2026 2027 /* ARGSUSED */ 2028 void 2029 ztest_zil_commit(ztest_ds_t *zd, uint64_t id) 2030 { 2031 zilog_t *zilog = zd->zd_zilog; 2032 2033 zil_commit(zilog, UINT64_MAX, ztest_random(ZTEST_OBJECTS)); 2034 2035 /* 2036 * Remember the committed values in zd, which is in parent/child 2037 * shared memory. If we die, the next iteration of ztest_run() 2038 * will verify that the log really does contain this record. 2039 */ 2040 mutex_enter(&zilog->zl_lock); 2041 ASSERT(zd->zd_seq <= zilog->zl_commit_lr_seq); 2042 zd->zd_seq = zilog->zl_commit_lr_seq; 2043 mutex_exit(&zilog->zl_lock); 2044 } 2045 2046 /* 2047 * Verify that we can't destroy an active pool, create an existing pool, 2048 * or create a pool with a bad vdev spec. 2049 */ 2050 /* ARGSUSED */ 2051 void 2052 ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id) 2053 { 2054 ztest_shared_t *zs = ztest_shared; 2055 spa_t *spa; 2056 nvlist_t *nvroot; 2057 2058 /* 2059 * Attempt to create using a bad file. 2060 */ 2061 nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1); 2062 VERIFY3U(ENOENT, ==, 2063 spa_create("ztest_bad_file", nvroot, NULL, NULL, NULL)); 2064 nvlist_free(nvroot); 2065 2066 /* 2067 * Attempt to create using a bad mirror. 2068 */ 2069 nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 2, 1); 2070 VERIFY3U(ENOENT, ==, 2071 spa_create("ztest_bad_mirror", nvroot, NULL, NULL, NULL)); 2072 nvlist_free(nvroot); 2073 2074 /* 2075 * Attempt to create an existing pool. It shouldn't matter 2076 * what's in the nvroot; we should fail with EEXIST. 2077 */ 2078 (void) rw_rdlock(&zs->zs_name_lock); 2079 nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1); 2080 VERIFY3U(EEXIST, ==, spa_create(zs->zs_pool, nvroot, NULL, NULL, NULL)); 2081 nvlist_free(nvroot); 2082 VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG)); 2083 VERIFY3U(EBUSY, ==, spa_destroy(zs->zs_pool)); 2084 spa_close(spa, FTAG); 2085 2086 (void) rw_unlock(&zs->zs_name_lock); 2087 } 2088 2089 static vdev_t * 2090 vdev_lookup_by_path(vdev_t *vd, const char *path) 2091 { 2092 vdev_t *mvd; 2093 2094 if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0) 2095 return (vd); 2096 2097 for (int c = 0; c < vd->vdev_children; c++) 2098 if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) != 2099 NULL) 2100 return (mvd); 2101 2102 return (NULL); 2103 } 2104 2105 /* 2106 * Find the first available hole which can be used as a top-level. 2107 */ 2108 int 2109 find_vdev_hole(spa_t *spa) 2110 { 2111 vdev_t *rvd = spa->spa_root_vdev; 2112 int c; 2113 2114 ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV); 2115 2116 for (c = 0; c < rvd->vdev_children; c++) { 2117 vdev_t *cvd = rvd->vdev_child[c]; 2118 2119 if (cvd->vdev_ishole) 2120 break; 2121 } 2122 return (c); 2123 } 2124 2125 /* 2126 * Verify that vdev_add() works as expected. 2127 */ 2128 /* ARGSUSED */ 2129 void 2130 ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id) 2131 { 2132 ztest_shared_t *zs = ztest_shared; 2133 spa_t *spa = zs->zs_spa; 2134 uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz; 2135 uint64_t guid; 2136 nvlist_t *nvroot; 2137 int error; 2138 2139 VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0); 2140 2141 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); 2142 2143 ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves; 2144 2145 /* 2146 * If we have slogs then remove them 1/4 of the time. 2147 */ 2148 if (spa_has_slogs(spa) && ztest_random(4) == 0) { 2149 /* 2150 * Grab the guid from the head of the log class rotor. 2151 */ 2152 guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid; 2153 2154 spa_config_exit(spa, SCL_VDEV, FTAG); 2155 2156 /* 2157 * We have to grab the zs_name_lock as writer to 2158 * prevent a race between removing a slog (dmu_objset_find) 2159 * and destroying a dataset. Removing the slog will 2160 * grab a reference on the dataset which may cause 2161 * dmu_objset_destroy() to fail with EBUSY thus 2162 * leaving the dataset in an inconsistent state. 2163 */ 2164 VERIFY(rw_wrlock(&ztest_shared->zs_name_lock) == 0); 2165 error = spa_vdev_remove(spa, guid, B_FALSE); 2166 VERIFY(rw_unlock(&ztest_shared->zs_name_lock) == 0); 2167 2168 if (error && error != EEXIST) 2169 fatal(0, "spa_vdev_remove() = %d", error); 2170 } else { 2171 spa_config_exit(spa, SCL_VDEV, FTAG); 2172 2173 /* 2174 * Make 1/4 of the devices be log devices. 2175 */ 2176 nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0, 2177 ztest_random(4) == 0, zopt_raidz, zopt_mirrors, 1); 2178 2179 error = spa_vdev_add(spa, nvroot); 2180 nvlist_free(nvroot); 2181 2182 if (error == ENOSPC) 2183 ztest_record_enospc("spa_vdev_add"); 2184 else if (error != 0) 2185 fatal(0, "spa_vdev_add() = %d", error); 2186 } 2187 2188 VERIFY(mutex_unlock(&ztest_shared->zs_vdev_lock) == 0); 2189 } 2190 2191 /* 2192 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected. 2193 */ 2194 /* ARGSUSED */ 2195 void 2196 ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id) 2197 { 2198 ztest_shared_t *zs = ztest_shared; 2199 spa_t *spa = zs->zs_spa; 2200 vdev_t *rvd = spa->spa_root_vdev; 2201 spa_aux_vdev_t *sav; 2202 char *aux; 2203 uint64_t guid = 0; 2204 int error; 2205 2206 if (ztest_random(2) == 0) { 2207 sav = &spa->spa_spares; 2208 aux = ZPOOL_CONFIG_SPARES; 2209 } else { 2210 sav = &spa->spa_l2cache; 2211 aux = ZPOOL_CONFIG_L2CACHE; 2212 } 2213 2214 VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0); 2215 2216 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); 2217 2218 if (sav->sav_count != 0 && ztest_random(4) == 0) { 2219 /* 2220 * Pick a random device to remove. 2221 */ 2222 guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid; 2223 } else { 2224 /* 2225 * Find an unused device we can add. 2226 */ 2227 zs->zs_vdev_aux = 0; 2228 for (;;) { 2229 char path[MAXPATHLEN]; 2230 int c; 2231 (void) sprintf(path, ztest_aux_template, zopt_dir, 2232 zopt_pool, aux, zs->zs_vdev_aux); 2233 for (c = 0; c < sav->sav_count; c++) 2234 if (strcmp(sav->sav_vdevs[c]->vdev_path, 2235 path) == 0) 2236 break; 2237 if (c == sav->sav_count && 2238 vdev_lookup_by_path(rvd, path) == NULL) 2239 break; 2240 zs->zs_vdev_aux++; 2241 } 2242 } 2243 2244 spa_config_exit(spa, SCL_VDEV, FTAG); 2245 2246 if (guid == 0) { 2247 /* 2248 * Add a new device. 2249 */ 2250 nvlist_t *nvroot = make_vdev_root(NULL, aux, 2251 (zopt_vdev_size * 5) / 4, 0, 0, 0, 0, 1); 2252 error = spa_vdev_add(spa, nvroot); 2253 if (error != 0) 2254 fatal(0, "spa_vdev_add(%p) = %d", nvroot, error); 2255 nvlist_free(nvroot); 2256 } else { 2257 /* 2258 * Remove an existing device. Sometimes, dirty its 2259 * vdev state first to make sure we handle removal 2260 * of devices that have pending state changes. 2261 */ 2262 if (ztest_random(2) == 0) 2263 (void) vdev_online(spa, guid, 0, NULL); 2264 2265 error = spa_vdev_remove(spa, guid, B_FALSE); 2266 if (error != 0 && error != EBUSY) 2267 fatal(0, "spa_vdev_remove(%llu) = %d", guid, error); 2268 } 2269 2270 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0); 2271 } 2272 2273 /* 2274 * Verify that we can attach and detach devices. 2275 */ 2276 /* ARGSUSED */ 2277 void 2278 ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id) 2279 { 2280 ztest_shared_t *zs = ztest_shared; 2281 spa_t *spa = zs->zs_spa; 2282 spa_aux_vdev_t *sav = &spa->spa_spares; 2283 vdev_t *rvd = spa->spa_root_vdev; 2284 vdev_t *oldvd, *newvd, *pvd; 2285 nvlist_t *root; 2286 uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz; 2287 uint64_t leaf, top; 2288 uint64_t ashift = ztest_get_ashift(); 2289 uint64_t oldguid, pguid; 2290 size_t oldsize, newsize; 2291 char oldpath[MAXPATHLEN], newpath[MAXPATHLEN]; 2292 int replacing; 2293 int oldvd_has_siblings = B_FALSE; 2294 int newvd_is_spare = B_FALSE; 2295 int oldvd_is_log; 2296 int error, expected_error; 2297 2298 VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0); 2299 2300 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); 2301 2302 /* 2303 * Decide whether to do an attach or a replace. 2304 */ 2305 replacing = ztest_random(2); 2306 2307 /* 2308 * Pick a random top-level vdev. 2309 */ 2310 top = ztest_random_vdev_top(spa, B_TRUE); 2311 2312 /* 2313 * Pick a random leaf within it. 2314 */ 2315 leaf = ztest_random(leaves); 2316 2317 /* 2318 * Locate this vdev. 2319 */ 2320 oldvd = rvd->vdev_child[top]; 2321 if (zopt_mirrors >= 1) { 2322 ASSERT(oldvd->vdev_ops == &vdev_mirror_ops); 2323 ASSERT(oldvd->vdev_children >= zopt_mirrors); 2324 oldvd = oldvd->vdev_child[leaf / zopt_raidz]; 2325 } 2326 if (zopt_raidz > 1) { 2327 ASSERT(oldvd->vdev_ops == &vdev_raidz_ops); 2328 ASSERT(oldvd->vdev_children == zopt_raidz); 2329 oldvd = oldvd->vdev_child[leaf % zopt_raidz]; 2330 } 2331 2332 /* 2333 * If we're already doing an attach or replace, oldvd may be a 2334 * mirror vdev -- in which case, pick a random child. 2335 */ 2336 while (oldvd->vdev_children != 0) { 2337 oldvd_has_siblings = B_TRUE; 2338 ASSERT(oldvd->vdev_children >= 2); 2339 oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)]; 2340 } 2341 2342 oldguid = oldvd->vdev_guid; 2343 oldsize = vdev_get_min_asize(oldvd); 2344 oldvd_is_log = oldvd->vdev_top->vdev_islog; 2345 (void) strcpy(oldpath, oldvd->vdev_path); 2346 pvd = oldvd->vdev_parent; 2347 pguid = pvd->vdev_guid; 2348 2349 /* 2350 * If oldvd has siblings, then half of the time, detach it. 2351 */ 2352 if (oldvd_has_siblings && ztest_random(2) == 0) { 2353 spa_config_exit(spa, SCL_VDEV, FTAG); 2354 error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE); 2355 if (error != 0 && error != ENODEV && error != EBUSY && 2356 error != ENOTSUP) 2357 fatal(0, "detach (%s) returned %d", oldpath, error); 2358 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0); 2359 return; 2360 } 2361 2362 /* 2363 * For the new vdev, choose with equal probability between the two 2364 * standard paths (ending in either 'a' or 'b') or a random hot spare. 2365 */ 2366 if (sav->sav_count != 0 && ztest_random(3) == 0) { 2367 newvd = sav->sav_vdevs[ztest_random(sav->sav_count)]; 2368 newvd_is_spare = B_TRUE; 2369 (void) strcpy(newpath, newvd->vdev_path); 2370 } else { 2371 (void) snprintf(newpath, sizeof (newpath), ztest_dev_template, 2372 zopt_dir, zopt_pool, top * leaves + leaf); 2373 if (ztest_random(2) == 0) 2374 newpath[strlen(newpath) - 1] = 'b'; 2375 newvd = vdev_lookup_by_path(rvd, newpath); 2376 } 2377 2378 if (newvd) { 2379 newsize = vdev_get_min_asize(newvd); 2380 } else { 2381 /* 2382 * Make newsize a little bigger or smaller than oldsize. 2383 * If it's smaller, the attach should fail. 2384 * If it's larger, and we're doing a replace, 2385 * we should get dynamic LUN growth when we're done. 2386 */ 2387 newsize = 10 * oldsize / (9 + ztest_random(3)); 2388 } 2389 2390 /* 2391 * If pvd is not a mirror or root, the attach should fail with ENOTSUP, 2392 * unless it's a replace; in that case any non-replacing parent is OK. 2393 * 2394 * If newvd is already part of the pool, it should fail with EBUSY. 2395 * 2396 * If newvd is too small, it should fail with EOVERFLOW. 2397 */ 2398 if (pvd->vdev_ops != &vdev_mirror_ops && 2399 pvd->vdev_ops != &vdev_root_ops && (!replacing || 2400 pvd->vdev_ops == &vdev_replacing_ops || 2401 pvd->vdev_ops == &vdev_spare_ops)) 2402 expected_error = ENOTSUP; 2403 else if (newvd_is_spare && (!replacing || oldvd_is_log)) 2404 expected_error = ENOTSUP; 2405 else if (newvd == oldvd) 2406 expected_error = replacing ? 0 : EBUSY; 2407 else if (vdev_lookup_by_path(rvd, newpath) != NULL) 2408 expected_error = EBUSY; 2409 else if (newsize < oldsize) 2410 expected_error = EOVERFLOW; 2411 else if (ashift > oldvd->vdev_top->vdev_ashift) 2412 expected_error = EDOM; 2413 else 2414 expected_error = 0; 2415 2416 spa_config_exit(spa, SCL_VDEV, FTAG); 2417 2418 /* 2419 * Build the nvlist describing newpath. 2420 */ 2421 root = make_vdev_root(newpath, NULL, newvd == NULL ? newsize : 0, 2422 ashift, 0, 0, 0, 1); 2423 2424 error = spa_vdev_attach(spa, oldguid, root, replacing); 2425 2426 nvlist_free(root); 2427 2428 /* 2429 * If our parent was the replacing vdev, but the replace completed, 2430 * then instead of failing with ENOTSUP we may either succeed, 2431 * fail with ENODEV, or fail with EOVERFLOW. 2432 */ 2433 if (expected_error == ENOTSUP && 2434 (error == 0 || error == ENODEV || error == EOVERFLOW)) 2435 expected_error = error; 2436 2437 /* 2438 * If someone grew the LUN, the replacement may be too small. 2439 */ 2440 if (error == EOVERFLOW || error == EBUSY) 2441 expected_error = error; 2442 2443 /* XXX workaround 6690467 */ 2444 if (error != expected_error && expected_error != EBUSY) { 2445 fatal(0, "attach (%s %llu, %s %llu, %d) " 2446 "returned %d, expected %d", 2447 oldpath, (longlong_t)oldsize, newpath, 2448 (longlong_t)newsize, replacing, error, expected_error); 2449 } 2450 2451 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0); 2452 } 2453 2454 /* 2455 * Callback function which expands the physical size of the vdev. 2456 */ 2457 vdev_t * 2458 grow_vdev(vdev_t *vd, void *arg) 2459 { 2460 spa_t *spa = vd->vdev_spa; 2461 size_t *newsize = arg; 2462 size_t fsize; 2463 int fd; 2464 2465 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE); 2466 ASSERT(vd->vdev_ops->vdev_op_leaf); 2467 2468 if ((fd = open(vd->vdev_path, O_RDWR)) == -1) 2469 return (vd); 2470 2471 fsize = lseek(fd, 0, SEEK_END); 2472 (void) ftruncate(fd, *newsize); 2473 2474 if (zopt_verbose >= 6) { 2475 (void) printf("%s grew from %lu to %lu bytes\n", 2476 vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize); 2477 } 2478 (void) close(fd); 2479 return (NULL); 2480 } 2481 2482 /* 2483 * Callback function which expands a given vdev by calling vdev_online(). 2484 */ 2485 /* ARGSUSED */ 2486 vdev_t * 2487 online_vdev(vdev_t *vd, void *arg) 2488 { 2489 spa_t *spa = vd->vdev_spa; 2490 vdev_t *tvd = vd->vdev_top; 2491 uint64_t guid = vd->vdev_guid; 2492 uint64_t generation = spa->spa_config_generation + 1; 2493 vdev_state_t newstate = VDEV_STATE_UNKNOWN; 2494 int error; 2495 2496 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE); 2497 ASSERT(vd->vdev_ops->vdev_op_leaf); 2498 2499 /* Calling vdev_online will initialize the new metaslabs */ 2500 spa_config_exit(spa, SCL_STATE, spa); 2501 error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate); 2502 spa_config_enter(spa, SCL_STATE, spa, RW_READER); 2503 2504 /* 2505 * If vdev_online returned an error or the underlying vdev_open 2506 * failed then we abort the expand. The only way to know that 2507 * vdev_open fails is by checking the returned newstate. 2508 */ 2509 if (error || newstate != VDEV_STATE_HEALTHY) { 2510 if (zopt_verbose >= 5) { 2511 (void) printf("Unable to expand vdev, state %llu, " 2512 "error %d\n", (u_longlong_t)newstate, error); 2513 } 2514 return (vd); 2515 } 2516 ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY); 2517 2518 /* 2519 * Since we dropped the lock we need to ensure that we're 2520 * still talking to the original vdev. It's possible this 2521 * vdev may have been detached/replaced while we were 2522 * trying to online it. 2523 */ 2524 if (generation != spa->spa_config_generation) { 2525 if (zopt_verbose >= 5) { 2526 (void) printf("vdev configuration has changed, " 2527 "guid %llu, state %llu, expected gen %llu, " 2528 "got gen %llu\n", 2529 (u_longlong_t)guid, 2530 (u_longlong_t)tvd->vdev_state, 2531 (u_longlong_t)generation, 2532 (u_longlong_t)spa->spa_config_generation); 2533 } 2534 return (vd); 2535 } 2536 return (NULL); 2537 } 2538 2539 /* 2540 * Traverse the vdev tree calling the supplied function. 2541 * We continue to walk the tree until we either have walked all 2542 * children or we receive a non-NULL return from the callback. 2543 * If a NULL callback is passed, then we just return back the first 2544 * leaf vdev we encounter. 2545 */ 2546 vdev_t * 2547 vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg) 2548 { 2549 if (vd->vdev_ops->vdev_op_leaf) { 2550 if (func == NULL) 2551 return (vd); 2552 else 2553 return (func(vd, arg)); 2554 } 2555 2556 for (uint_t c = 0; c < vd->vdev_children; c++) { 2557 vdev_t *cvd = vd->vdev_child[c]; 2558 if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL) 2559 return (cvd); 2560 } 2561 return (NULL); 2562 } 2563 2564 /* 2565 * Verify that dynamic LUN growth works as expected. 2566 */ 2567 /* ARGSUSED */ 2568 void 2569 ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id) 2570 { 2571 ztest_shared_t *zs = ztest_shared; 2572 spa_t *spa = zs->zs_spa; 2573 vdev_t *vd, *tvd; 2574 metaslab_class_t *mc; 2575 metaslab_group_t *mg; 2576 size_t psize, newsize; 2577 uint64_t top; 2578 uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count; 2579 2580 VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0); 2581 spa_config_enter(spa, SCL_STATE, spa, RW_READER); 2582 2583 top = ztest_random_vdev_top(spa, B_TRUE); 2584 2585 tvd = spa->spa_root_vdev->vdev_child[top]; 2586 mg = tvd->vdev_mg; 2587 mc = mg->mg_class; 2588 old_ms_count = tvd->vdev_ms_count; 2589 old_class_space = metaslab_class_get_space(mc); 2590 2591 /* 2592 * Determine the size of the first leaf vdev associated with 2593 * our top-level device. 2594 */ 2595 vd = vdev_walk_tree(tvd, NULL, NULL); 2596 ASSERT3P(vd, !=, NULL); 2597 ASSERT(vd->vdev_ops->vdev_op_leaf); 2598 2599 psize = vd->vdev_psize; 2600 2601 /* 2602 * We only try to expand the vdev if it's healthy, less than 4x its 2603 * original size, and it has a valid psize. 2604 */ 2605 if (tvd->vdev_state != VDEV_STATE_HEALTHY || 2606 psize == 0 || psize >= 4 * zopt_vdev_size) { 2607 spa_config_exit(spa, SCL_STATE, spa); 2608 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0); 2609 return; 2610 } 2611 ASSERT(psize > 0); 2612 newsize = psize + psize / 8; 2613 ASSERT3U(newsize, >, psize); 2614 2615 if (zopt_verbose >= 6) { 2616 (void) printf("Expanding LUN %s from %lu to %lu\n", 2617 vd->vdev_path, (ulong_t)psize, (ulong_t)newsize); 2618 } 2619 2620 /* 2621 * Growing the vdev is a two step process: 2622 * 1). expand the physical size (i.e. relabel) 2623 * 2). online the vdev to create the new metaslabs 2624 */ 2625 if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL || 2626 vdev_walk_tree(tvd, online_vdev, NULL) != NULL || 2627 tvd->vdev_state != VDEV_STATE_HEALTHY) { 2628 if (zopt_verbose >= 5) { 2629 (void) printf("Could not expand LUN because " 2630 "the vdev configuration changed.\n"); 2631 } 2632 spa_config_exit(spa, SCL_STATE, spa); 2633 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0); 2634 return; 2635 } 2636 2637 spa_config_exit(spa, SCL_STATE, spa); 2638 2639 /* 2640 * Expanding the LUN will update the config asynchronously, 2641 * thus we must wait for the async thread to complete any 2642 * pending tasks before proceeding. 2643 */ 2644 for (;;) { 2645 boolean_t done; 2646 mutex_enter(&spa->spa_async_lock); 2647 done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks); 2648 mutex_exit(&spa->spa_async_lock); 2649 if (done) 2650 break; 2651 txg_wait_synced(spa_get_dsl(spa), 0); 2652 (void) poll(NULL, 0, 100); 2653 } 2654 2655 spa_config_enter(spa, SCL_STATE, spa, RW_READER); 2656 2657 tvd = spa->spa_root_vdev->vdev_child[top]; 2658 new_ms_count = tvd->vdev_ms_count; 2659 new_class_space = metaslab_class_get_space(mc); 2660 2661 if (tvd->vdev_mg != mg || mg->mg_class != mc) { 2662 if (zopt_verbose >= 5) { 2663 (void) printf("Could not verify LUN expansion due to " 2664 "intervening vdev offline or remove.\n"); 2665 } 2666 spa_config_exit(spa, SCL_STATE, spa); 2667 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0); 2668 return; 2669 } 2670 2671 /* 2672 * Make sure we were able to grow the vdev. 2673 */ 2674 if (new_ms_count <= old_ms_count) 2675 fatal(0, "LUN expansion failed: ms_count %llu <= %llu\n", 2676 old_ms_count, new_ms_count); 2677 2678 /* 2679 * Make sure we were able to grow the pool. 2680 */ 2681 if (new_class_space <= old_class_space) 2682 fatal(0, "LUN expansion failed: class_space %llu <= %llu\n", 2683 old_class_space, new_class_space); 2684 2685 if (zopt_verbose >= 5) { 2686 char oldnumbuf[6], newnumbuf[6]; 2687 2688 nicenum(old_class_space, oldnumbuf); 2689 nicenum(new_class_space, newnumbuf); 2690 (void) printf("%s grew from %s to %s\n", 2691 spa->spa_name, oldnumbuf, newnumbuf); 2692 } 2693 2694 spa_config_exit(spa, SCL_STATE, spa); 2695 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0); 2696 } 2697 2698 /* 2699 * Verify that dmu_objset_{create,destroy,open,close} work as expected. 2700 */ 2701 /* ARGSUSED */ 2702 static void 2703 ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) 2704 { 2705 /* 2706 * Create the objects common to all ztest datasets. 2707 */ 2708 VERIFY(zap_create_claim(os, ZTEST_DIROBJ, 2709 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0); 2710 } 2711 2712 /* ARGSUSED */ 2713 static int 2714 ztest_objset_destroy_cb(char *name, void *arg) 2715 { 2716 objset_t *os; 2717 dmu_object_info_t doi; 2718 int error; 2719 2720 /* 2721 * Verify that the dataset contains a directory object. 2722 */ 2723 VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os)); 2724 error = dmu_object_info(os, ZTEST_DIROBJ, &doi); 2725 if (error != ENOENT) { 2726 /* We could have crashed in the middle of destroying it */ 2727 ASSERT3U(error, ==, 0); 2728 ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER); 2729 ASSERT3S(doi.doi_physical_blocks_512, >=, 0); 2730 } 2731 dmu_objset_rele(os, FTAG); 2732 2733 /* 2734 * Destroy the dataset. 2735 */ 2736 VERIFY3U(0, ==, dmu_objset_destroy(name, B_FALSE)); 2737 return (0); 2738 } 2739 2740 static boolean_t 2741 ztest_snapshot_create(char *osname, uint64_t id) 2742 { 2743 char snapname[MAXNAMELEN]; 2744 int error; 2745 2746 (void) snprintf(snapname, MAXNAMELEN, "%s@%llu", osname, 2747 (u_longlong_t)id); 2748 2749 error = dmu_objset_snapshot(osname, strchr(snapname, '@') + 1, 2750 NULL, B_FALSE); 2751 if (error == ENOSPC) { 2752 ztest_record_enospc(FTAG); 2753 return (B_FALSE); 2754 } 2755 if (error != 0 && error != EEXIST) 2756 fatal(0, "ztest_snapshot_create(%s) = %d", snapname, error); 2757 return (B_TRUE); 2758 } 2759 2760 static boolean_t 2761 ztest_snapshot_destroy(char *osname, uint64_t id) 2762 { 2763 char snapname[MAXNAMELEN]; 2764 int error; 2765 2766 (void) snprintf(snapname, MAXNAMELEN, "%s@%llu", osname, 2767 (u_longlong_t)id); 2768 2769 error = dmu_objset_destroy(snapname, B_FALSE); 2770 if (error != 0 && error != ENOENT) 2771 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error); 2772 return (B_TRUE); 2773 } 2774 2775 /* ARGSUSED */ 2776 void 2777 ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id) 2778 { 2779 ztest_shared_t *zs = ztest_shared; 2780 ztest_ds_t zdtmp; 2781 int iters; 2782 int error; 2783 objset_t *os, *os2; 2784 char name[MAXNAMELEN]; 2785 zilog_t *zilog; 2786 2787 (void) rw_rdlock(&zs->zs_name_lock); 2788 2789 (void) snprintf(name, MAXNAMELEN, "%s/temp_%llu", 2790 zs->zs_pool, (u_longlong_t)id); 2791 2792 /* 2793 * If this dataset exists from a previous run, process its replay log 2794 * half of the time. If we don't replay it, then dmu_objset_destroy() 2795 * (invoked from ztest_objset_destroy_cb()) should just throw it away. 2796 */ 2797 if (ztest_random(2) == 0 && 2798 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) { 2799 ztest_zd_init(&zdtmp, os); 2800 zil_replay(os, &zdtmp, ztest_replay_vector); 2801 ztest_zd_fini(&zdtmp); 2802 dmu_objset_disown(os, FTAG); 2803 } 2804 2805 /* 2806 * There may be an old instance of the dataset we're about to 2807 * create lying around from a previous run. If so, destroy it 2808 * and all of its snapshots. 2809 */ 2810 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL, 2811 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS); 2812 2813 /* 2814 * Verify that the destroyed dataset is no longer in the namespace. 2815 */ 2816 VERIFY3U(ENOENT, ==, dmu_objset_hold(name, FTAG, &os)); 2817 2818 /* 2819 * Verify that we can create a new dataset. 2820 */ 2821 error = dmu_objset_create(name, DMU_OST_OTHER, 0, 2822 ztest_objset_create_cb, NULL); 2823 if (error) { 2824 if (error == ENOSPC) { 2825 ztest_record_enospc(FTAG); 2826 (void) rw_unlock(&zs->zs_name_lock); 2827 return; 2828 } 2829 fatal(0, "dmu_objset_create(%s) = %d", name, error); 2830 } 2831 2832 VERIFY3U(0, ==, 2833 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os)); 2834 2835 ztest_zd_init(&zdtmp, os); 2836 2837 /* 2838 * Open the intent log for it. 2839 */ 2840 zilog = zil_open(os, ztest_get_data); 2841 2842 /* 2843 * Put some objects in there, do a little I/O to them, 2844 * and randomly take a couple of snapshots along the way. 2845 */ 2846 iters = ztest_random(5); 2847 for (int i = 0; i < iters; i++) { 2848 ztest_dmu_object_alloc_free(&zdtmp, id); 2849 if (ztest_random(iters) == 0) 2850 (void) ztest_snapshot_create(name, i); 2851 } 2852 2853 /* 2854 * Verify that we cannot create an existing dataset. 2855 */ 2856 VERIFY3U(EEXIST, ==, 2857 dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL)); 2858 2859 /* 2860 * Verify that we can hold an objset that is also owned. 2861 */ 2862 VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2)); 2863 dmu_objset_rele(os2, FTAG); 2864 2865 /* 2866 * Verify that we cannot own an objset that is already owned. 2867 */ 2868 VERIFY3U(EBUSY, ==, 2869 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2)); 2870 2871 zil_close(zilog); 2872 dmu_objset_disown(os, FTAG); 2873 ztest_zd_fini(&zdtmp); 2874 2875 (void) rw_unlock(&zs->zs_name_lock); 2876 } 2877 2878 /* 2879 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected. 2880 */ 2881 void 2882 ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id) 2883 { 2884 ztest_shared_t *zs = ztest_shared; 2885 2886 (void) rw_rdlock(&zs->zs_name_lock); 2887 (void) ztest_snapshot_destroy(zd->zd_name, id); 2888 (void) ztest_snapshot_create(zd->zd_name, id); 2889 (void) rw_unlock(&zs->zs_name_lock); 2890 } 2891 2892 /* 2893 * Cleanup non-standard snapshots and clones. 2894 */ 2895 void 2896 ztest_dsl_dataset_cleanup(char *osname, uint64_t id) 2897 { 2898 char snap1name[MAXNAMELEN]; 2899 char clone1name[MAXNAMELEN]; 2900 char snap2name[MAXNAMELEN]; 2901 char clone2name[MAXNAMELEN]; 2902 char snap3name[MAXNAMELEN]; 2903 int error; 2904 2905 (void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu", osname, id); 2906 (void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu", osname, id); 2907 (void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu", clone1name, id); 2908 (void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu", osname, id); 2909 (void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu", clone1name, id); 2910 2911 error = dmu_objset_destroy(clone2name, B_FALSE); 2912 if (error && error != ENOENT) 2913 fatal(0, "dmu_objset_destroy(%s) = %d", clone2name, error); 2914 error = dmu_objset_destroy(snap3name, B_FALSE); 2915 if (error && error != ENOENT) 2916 fatal(0, "dmu_objset_destroy(%s) = %d", snap3name, error); 2917 error = dmu_objset_destroy(snap2name, B_FALSE); 2918 if (error && error != ENOENT) 2919 fatal(0, "dmu_objset_destroy(%s) = %d", snap2name, error); 2920 error = dmu_objset_destroy(clone1name, B_FALSE); 2921 if (error && error != ENOENT) 2922 fatal(0, "dmu_objset_destroy(%s) = %d", clone1name, error); 2923 error = dmu_objset_destroy(snap1name, B_FALSE); 2924 if (error && error != ENOENT) 2925 fatal(0, "dmu_objset_destroy(%s) = %d", snap1name, error); 2926 } 2927 2928 /* 2929 * Verify dsl_dataset_promote handles EBUSY 2930 */ 2931 void 2932 ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id) 2933 { 2934 ztest_shared_t *zs = ztest_shared; 2935 objset_t *clone; 2936 dsl_dataset_t *ds; 2937 char snap1name[MAXNAMELEN]; 2938 char clone1name[MAXNAMELEN]; 2939 char snap2name[MAXNAMELEN]; 2940 char clone2name[MAXNAMELEN]; 2941 char snap3name[MAXNAMELEN]; 2942 char *osname = zd->zd_name; 2943 int error; 2944 2945 (void) rw_rdlock(&zs->zs_name_lock); 2946 2947 ztest_dsl_dataset_cleanup(osname, id); 2948 2949 (void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu", osname, id); 2950 (void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu", osname, id); 2951 (void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu", clone1name, id); 2952 (void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu", osname, id); 2953 (void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu", clone1name, id); 2954 2955 error = dmu_objset_snapshot(osname, strchr(snap1name, '@')+1, 2956 NULL, B_FALSE); 2957 if (error && error != EEXIST) { 2958 if (error == ENOSPC) { 2959 ztest_record_enospc(FTAG); 2960 goto out; 2961 } 2962 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error); 2963 } 2964 2965 error = dmu_objset_hold(snap1name, FTAG, &clone); 2966 if (error) 2967 fatal(0, "dmu_open_snapshot(%s) = %d", snap1name, error); 2968 2969 error = dmu_objset_clone(clone1name, dmu_objset_ds(clone), 0); 2970 dmu_objset_rele(clone, FTAG); 2971 if (error) { 2972 if (error == ENOSPC) { 2973 ztest_record_enospc(FTAG); 2974 goto out; 2975 } 2976 fatal(0, "dmu_objset_create(%s) = %d", clone1name, error); 2977 } 2978 2979 error = dmu_objset_snapshot(clone1name, strchr(snap2name, '@')+1, 2980 NULL, B_FALSE); 2981 if (error && error != EEXIST) { 2982 if (error == ENOSPC) { 2983 ztest_record_enospc(FTAG); 2984 goto out; 2985 } 2986 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error); 2987 } 2988 2989 error = dmu_objset_snapshot(clone1name, strchr(snap3name, '@')+1, 2990 NULL, B_FALSE); 2991 if (error && error != EEXIST) { 2992 if (error == ENOSPC) { 2993 ztest_record_enospc(FTAG); 2994 goto out; 2995 } 2996 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error); 2997 } 2998 2999 error = dmu_objset_hold(snap3name, FTAG, &clone); 3000 if (error) 3001 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error); 3002 3003 error = dmu_objset_clone(clone2name, dmu_objset_ds(clone), 0); 3004 dmu_objset_rele(clone, FTAG); 3005 if (error) { 3006 if (error == ENOSPC) { 3007 ztest_record_enospc(FTAG); 3008 goto out; 3009 } 3010 fatal(0, "dmu_objset_create(%s) = %d", clone2name, error); 3011 } 3012 3013 error = dsl_dataset_own(snap1name, B_FALSE, FTAG, &ds); 3014 if (error) 3015 fatal(0, "dsl_dataset_own(%s) = %d", snap1name, error); 3016 error = dsl_dataset_promote(clone2name, NULL); 3017 if (error != EBUSY) 3018 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name, 3019 error); 3020 dsl_dataset_disown(ds, FTAG); 3021 3022 out: 3023 ztest_dsl_dataset_cleanup(osname, id); 3024 3025 (void) rw_unlock(&zs->zs_name_lock); 3026 } 3027 3028 /* 3029 * Verify that dmu_object_{alloc,free} work as expected. 3030 */ 3031 void 3032 ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id) 3033 { 3034 ztest_od_t od[4]; 3035 int batchsize = sizeof (od) / sizeof (od[0]); 3036 3037 for (int b = 0; b < batchsize; b++) 3038 ztest_od_init(&od[b], id, FTAG, b, DMU_OT_UINT64_OTHER, 0, 0); 3039 3040 /* 3041 * Destroy the previous batch of objects, create a new batch, 3042 * and do some I/O on the new objects. 3043 */ 3044 if (ztest_object_init(zd, od, sizeof (od), B_TRUE) != 0) 3045 return; 3046 3047 while (ztest_random(4 * batchsize) != 0) 3048 ztest_io(zd, od[ztest_random(batchsize)].od_object, 3049 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT); 3050 } 3051 3052 /* 3053 * Verify that dmu_{read,write} work as expected. 3054 */ 3055 void 3056 ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id) 3057 { 3058 objset_t *os = zd->zd_os; 3059 ztest_od_t od[2]; 3060 dmu_tx_t *tx; 3061 int i, freeit, error; 3062 uint64_t n, s, txg; 3063 bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT; 3064 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize; 3065 uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t); 3066 uint64_t regions = 997; 3067 uint64_t stride = 123456789ULL; 3068 uint64_t width = 40; 3069 int free_percent = 5; 3070 3071 /* 3072 * This test uses two objects, packobj and bigobj, that are always 3073 * updated together (i.e. in the same tx) so that their contents are 3074 * in sync and can be compared. Their contents relate to each other 3075 * in a simple way: packobj is a dense array of 'bufwad' structures, 3076 * while bigobj is a sparse array of the same bufwads. Specifically, 3077 * for any index n, there are three bufwads that should be identical: 3078 * 3079 * packobj, at offset n * sizeof (bufwad_t) 3080 * bigobj, at the head of the nth chunk 3081 * bigobj, at the tail of the nth chunk 3082 * 3083 * The chunk size is arbitrary. It doesn't have to be a power of two, 3084 * and it doesn't have any relation to the object blocksize. 3085 * The only requirement is that it can hold at least two bufwads. 3086 * 3087 * Normally, we write the bufwad to each of these locations. 3088 * However, free_percent of the time we instead write zeroes to 3089 * packobj and perform a dmu_free_range() on bigobj. By comparing 3090 * bigobj to packobj, we can verify that the DMU is correctly 3091 * tracking which parts of an object are allocated and free, 3092 * and that the contents of the allocated blocks are correct. 3093 */ 3094 3095 /* 3096 * Read the directory info. If it's the first time, set things up. 3097 */ 3098 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, chunksize); 3099 ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize); 3100 3101 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0) 3102 return; 3103 3104 bigobj = od[0].od_object; 3105 packobj = od[1].od_object; 3106 chunksize = od[0].od_gen; 3107 ASSERT(chunksize == od[1].od_gen); 3108 3109 /* 3110 * Prefetch a random chunk of the big object. 3111 * Our aim here is to get some async reads in flight 3112 * for blocks that we may free below; the DMU should 3113 * handle this race correctly. 3114 */ 3115 n = ztest_random(regions) * stride + ztest_random(width); 3116 s = 1 + ztest_random(2 * width - 1); 3117 dmu_prefetch(os, bigobj, n * chunksize, s * chunksize); 3118 3119 /* 3120 * Pick a random index and compute the offsets into packobj and bigobj. 3121 */ 3122 n = ztest_random(regions) * stride + ztest_random(width); 3123 s = 1 + ztest_random(width - 1); 3124 3125 packoff = n * sizeof (bufwad_t); 3126 packsize = s * sizeof (bufwad_t); 3127 3128 bigoff = n * chunksize; 3129 bigsize = s * chunksize; 3130 3131 packbuf = umem_alloc(packsize, UMEM_NOFAIL); 3132 bigbuf = umem_alloc(bigsize, UMEM_NOFAIL); 3133 3134 /* 3135 * free_percent of the time, free a range of bigobj rather than 3136 * overwriting it. 3137 */ 3138 freeit = (ztest_random(100) < free_percent); 3139 3140 /* 3141 * Read the current contents of our objects. 3142 */ 3143 error = dmu_read(os, packobj, packoff, packsize, packbuf, 3144 DMU_READ_PREFETCH); 3145 ASSERT3U(error, ==, 0); 3146 error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf, 3147 DMU_READ_PREFETCH); 3148 ASSERT3U(error, ==, 0); 3149 3150 /* 3151 * Get a tx for the mods to both packobj and bigobj. 3152 */ 3153 tx = dmu_tx_create(os); 3154 3155 dmu_tx_hold_write(tx, packobj, packoff, packsize); 3156 3157 if (freeit) 3158 dmu_tx_hold_free(tx, bigobj, bigoff, bigsize); 3159 else 3160 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize); 3161 3162 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG); 3163 if (txg == 0) { 3164 umem_free(packbuf, packsize); 3165 umem_free(bigbuf, bigsize); 3166 return; 3167 } 3168 3169 dmu_object_set_checksum(os, bigobj, 3170 (enum zio_checksum)ztest_random_dsl_prop(ZFS_PROP_CHECKSUM), tx); 3171 3172 dmu_object_set_compress(os, bigobj, 3173 (enum zio_compress)ztest_random_dsl_prop(ZFS_PROP_COMPRESSION), tx); 3174 3175 /* 3176 * For each index from n to n + s, verify that the existing bufwad 3177 * in packobj matches the bufwads at the head and tail of the 3178 * corresponding chunk in bigobj. Then update all three bufwads 3179 * with the new values we want to write out. 3180 */ 3181 for (i = 0; i < s; i++) { 3182 /* LINTED */ 3183 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t)); 3184 /* LINTED */ 3185 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize); 3186 /* LINTED */ 3187 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1; 3188 3189 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize); 3190 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize); 3191 3192 if (pack->bw_txg > txg) 3193 fatal(0, "future leak: got %llx, open txg is %llx", 3194 pack->bw_txg, txg); 3195 3196 if (pack->bw_data != 0 && pack->bw_index != n + i) 3197 fatal(0, "wrong index: got %llx, wanted %llx+%llx", 3198 pack->bw_index, n, i); 3199 3200 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0) 3201 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH); 3202 3203 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0) 3204 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT); 3205 3206 if (freeit) { 3207 bzero(pack, sizeof (bufwad_t)); 3208 } else { 3209 pack->bw_index = n + i; 3210 pack->bw_txg = txg; 3211 pack->bw_data = 1 + ztest_random(-2ULL); 3212 } 3213 *bigH = *pack; 3214 *bigT = *pack; 3215 } 3216 3217 /* 3218 * We've verified all the old bufwads, and made new ones. 3219 * Now write them out. 3220 */ 3221 dmu_write(os, packobj, packoff, packsize, packbuf, tx); 3222 3223 if (freeit) { 3224 if (zopt_verbose >= 7) { 3225 (void) printf("freeing offset %llx size %llx" 3226 " txg %llx\n", 3227 (u_longlong_t)bigoff, 3228 (u_longlong_t)bigsize, 3229 (u_longlong_t)txg); 3230 } 3231 VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx)); 3232 } else { 3233 if (zopt_verbose >= 7) { 3234 (void) printf("writing offset %llx size %llx" 3235 " txg %llx\n", 3236 (u_longlong_t)bigoff, 3237 (u_longlong_t)bigsize, 3238 (u_longlong_t)txg); 3239 } 3240 dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx); 3241 } 3242 3243 dmu_tx_commit(tx); 3244 3245 /* 3246 * Sanity check the stuff we just wrote. 3247 */ 3248 { 3249 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL); 3250 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL); 3251 3252 VERIFY(0 == dmu_read(os, packobj, packoff, 3253 packsize, packcheck, DMU_READ_PREFETCH)); 3254 VERIFY(0 == dmu_read(os, bigobj, bigoff, 3255 bigsize, bigcheck, DMU_READ_PREFETCH)); 3256 3257 ASSERT(bcmp(packbuf, packcheck, packsize) == 0); 3258 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0); 3259 3260 umem_free(packcheck, packsize); 3261 umem_free(bigcheck, bigsize); 3262 } 3263 3264 umem_free(packbuf, packsize); 3265 umem_free(bigbuf, bigsize); 3266 } 3267 3268 void 3269 compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf, 3270 uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg) 3271 { 3272 uint64_t i; 3273 bufwad_t *pack; 3274 bufwad_t *bigH; 3275 bufwad_t *bigT; 3276 3277 /* 3278 * For each index from n to n + s, verify that the existing bufwad 3279 * in packobj matches the bufwads at the head and tail of the 3280 * corresponding chunk in bigobj. Then update all three bufwads 3281 * with the new values we want to write out. 3282 */ 3283 for (i = 0; i < s; i++) { 3284 /* LINTED */ 3285 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t)); 3286 /* LINTED */ 3287 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize); 3288 /* LINTED */ 3289 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1; 3290 3291 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize); 3292 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize); 3293 3294 if (pack->bw_txg > txg) 3295 fatal(0, "future leak: got %llx, open txg is %llx", 3296 pack->bw_txg, txg); 3297 3298 if (pack->bw_data != 0 && pack->bw_index != n + i) 3299 fatal(0, "wrong index: got %llx, wanted %llx+%llx", 3300 pack->bw_index, n, i); 3301 3302 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0) 3303 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH); 3304 3305 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0) 3306 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT); 3307 3308 pack->bw_index = n + i; 3309 pack->bw_txg = txg; 3310 pack->bw_data = 1 + ztest_random(-2ULL); 3311 3312 *bigH = *pack; 3313 *bigT = *pack; 3314 } 3315 } 3316 3317 void 3318 ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id) 3319 { 3320 objset_t *os = zd->zd_os; 3321 ztest_od_t od[2]; 3322 dmu_tx_t *tx; 3323 uint64_t i; 3324 int error; 3325 uint64_t n, s, txg; 3326 bufwad_t *packbuf, *bigbuf; 3327 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize; 3328 uint64_t blocksize = ztest_random_blocksize(); 3329 uint64_t chunksize = blocksize; 3330 uint64_t regions = 997; 3331 uint64_t stride = 123456789ULL; 3332 uint64_t width = 9; 3333 dmu_buf_t *bonus_db; 3334 arc_buf_t **bigbuf_arcbufs; 3335 dmu_object_info_t doi; 3336 3337 /* 3338 * This test uses two objects, packobj and bigobj, that are always 3339 * updated together (i.e. in the same tx) so that their contents are 3340 * in sync and can be compared. Their contents relate to each other 3341 * in a simple way: packobj is a dense array of 'bufwad' structures, 3342 * while bigobj is a sparse array of the same bufwads. Specifically, 3343 * for any index n, there are three bufwads that should be identical: 3344 * 3345 * packobj, at offset n * sizeof (bufwad_t) 3346 * bigobj, at the head of the nth chunk 3347 * bigobj, at the tail of the nth chunk 3348 * 3349 * The chunk size is set equal to bigobj block size so that 3350 * dmu_assign_arcbuf() can be tested for object updates. 3351 */ 3352 3353 /* 3354 * Read the directory info. If it's the first time, set things up. 3355 */ 3356 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0); 3357 ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize); 3358 3359 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0) 3360 return; 3361 3362 bigobj = od[0].od_object; 3363 packobj = od[1].od_object; 3364 blocksize = od[0].od_blocksize; 3365 chunksize = blocksize; 3366 ASSERT(chunksize == od[1].od_gen); 3367 3368 VERIFY(dmu_object_info(os, bigobj, &doi) == 0); 3369 VERIFY(ISP2(doi.doi_data_block_size)); 3370 VERIFY(chunksize == doi.doi_data_block_size); 3371 VERIFY(chunksize >= 2 * sizeof (bufwad_t)); 3372 3373 /* 3374 * Pick a random index and compute the offsets into packobj and bigobj. 3375 */ 3376 n = ztest_random(regions) * stride + ztest_random(width); 3377 s = 1 + ztest_random(width - 1); 3378 3379 packoff = n * sizeof (bufwad_t); 3380 packsize = s * sizeof (bufwad_t); 3381 3382 bigoff = n * chunksize; 3383 bigsize = s * chunksize; 3384 3385 packbuf = umem_zalloc(packsize, UMEM_NOFAIL); 3386 bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL); 3387 3388 VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db)); 3389 3390 bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL); 3391 3392 /* 3393 * Iteration 0 test zcopy for DB_UNCACHED dbufs. 3394 * Iteration 1 test zcopy to already referenced dbufs. 3395 * Iteration 2 test zcopy to dirty dbuf in the same txg. 3396 * Iteration 3 test zcopy to dbuf dirty in previous txg. 3397 * Iteration 4 test zcopy when dbuf is no longer dirty. 3398 * Iteration 5 test zcopy when it can't be done. 3399 * Iteration 6 one more zcopy write. 3400 */ 3401 for (i = 0; i < 7; i++) { 3402 uint64_t j; 3403 uint64_t off; 3404 3405 /* 3406 * In iteration 5 (i == 5) use arcbufs 3407 * that don't match bigobj blksz to test 3408 * dmu_assign_arcbuf() when it can't directly 3409 * assign an arcbuf to a dbuf. 3410 */ 3411 for (j = 0; j < s; j++) { 3412 if (i != 5) { 3413 bigbuf_arcbufs[j] = 3414 dmu_request_arcbuf(bonus_db, chunksize); 3415 } else { 3416 bigbuf_arcbufs[2 * j] = 3417 dmu_request_arcbuf(bonus_db, chunksize / 2); 3418 bigbuf_arcbufs[2 * j + 1] = 3419 dmu_request_arcbuf(bonus_db, chunksize / 2); 3420 } 3421 } 3422 3423 /* 3424 * Get a tx for the mods to both packobj and bigobj. 3425 */ 3426 tx = dmu_tx_create(os); 3427 3428 dmu_tx_hold_write(tx, packobj, packoff, packsize); 3429 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize); 3430 3431 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG); 3432 if (txg == 0) { 3433 umem_free(packbuf, packsize); 3434 umem_free(bigbuf, bigsize); 3435 for (j = 0; j < s; j++) { 3436 if (i != 5) { 3437 dmu_return_arcbuf(bigbuf_arcbufs[j]); 3438 } else { 3439 dmu_return_arcbuf( 3440 bigbuf_arcbufs[2 * j]); 3441 dmu_return_arcbuf( 3442 bigbuf_arcbufs[2 * j + 1]); 3443 } 3444 } 3445 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *)); 3446 dmu_buf_rele(bonus_db, FTAG); 3447 return; 3448 } 3449 3450 /* 3451 * 50% of the time don't read objects in the 1st iteration to 3452 * test dmu_assign_arcbuf() for the case when there're no 3453 * existing dbufs for the specified offsets. 3454 */ 3455 if (i != 0 || ztest_random(2) != 0) { 3456 error = dmu_read(os, packobj, packoff, 3457 packsize, packbuf, DMU_READ_PREFETCH); 3458 ASSERT3U(error, ==, 0); 3459 error = dmu_read(os, bigobj, bigoff, bigsize, 3460 bigbuf, DMU_READ_PREFETCH); 3461 ASSERT3U(error, ==, 0); 3462 } 3463 compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize, 3464 n, chunksize, txg); 3465 3466 /* 3467 * We've verified all the old bufwads, and made new ones. 3468 * Now write them out. 3469 */ 3470 dmu_write(os, packobj, packoff, packsize, packbuf, tx); 3471 if (zopt_verbose >= 7) { 3472 (void) printf("writing offset %llx size %llx" 3473 " txg %llx\n", 3474 (u_longlong_t)bigoff, 3475 (u_longlong_t)bigsize, 3476 (u_longlong_t)txg); 3477 } 3478 for (off = bigoff, j = 0; j < s; j++, off += chunksize) { 3479 dmu_buf_t *dbt; 3480 if (i != 5) { 3481 bcopy((caddr_t)bigbuf + (off - bigoff), 3482 bigbuf_arcbufs[j]->b_data, chunksize); 3483 } else { 3484 bcopy((caddr_t)bigbuf + (off - bigoff), 3485 bigbuf_arcbufs[2 * j]->b_data, 3486 chunksize / 2); 3487 bcopy((caddr_t)bigbuf + (off - bigoff) + 3488 chunksize / 2, 3489 bigbuf_arcbufs[2 * j + 1]->b_data, 3490 chunksize / 2); 3491 } 3492 3493 if (i == 1) { 3494 VERIFY(dmu_buf_hold(os, bigobj, off, 3495 FTAG, &dbt) == 0); 3496 } 3497 if (i != 5) { 3498 dmu_assign_arcbuf(bonus_db, off, 3499 bigbuf_arcbufs[j], tx); 3500 } else { 3501 dmu_assign_arcbuf(bonus_db, off, 3502 bigbuf_arcbufs[2 * j], tx); 3503 dmu_assign_arcbuf(bonus_db, 3504 off + chunksize / 2, 3505 bigbuf_arcbufs[2 * j + 1], tx); 3506 } 3507 if (i == 1) { 3508 dmu_buf_rele(dbt, FTAG); 3509 } 3510 } 3511 dmu_tx_commit(tx); 3512 3513 /* 3514 * Sanity check the stuff we just wrote. 3515 */ 3516 { 3517 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL); 3518 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL); 3519 3520 VERIFY(0 == dmu_read(os, packobj, packoff, 3521 packsize, packcheck, DMU_READ_PREFETCH)); 3522 VERIFY(0 == dmu_read(os, bigobj, bigoff, 3523 bigsize, bigcheck, DMU_READ_PREFETCH)); 3524 3525 ASSERT(bcmp(packbuf, packcheck, packsize) == 0); 3526 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0); 3527 3528 umem_free(packcheck, packsize); 3529 umem_free(bigcheck, bigsize); 3530 } 3531 if (i == 2) { 3532 txg_wait_open(dmu_objset_pool(os), 0); 3533 } else if (i == 3) { 3534 txg_wait_synced(dmu_objset_pool(os), 0); 3535 } 3536 } 3537 3538 dmu_buf_rele(bonus_db, FTAG); 3539 umem_free(packbuf, packsize); 3540 umem_free(bigbuf, bigsize); 3541 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *)); 3542 } 3543 3544 /* ARGSUSED */ 3545 void 3546 ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id) 3547 { 3548 ztest_od_t od[1]; 3549 uint64_t offset = (1ULL << (ztest_random(20) + 43)) + 3550 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT); 3551 3552 /* 3553 * Have multiple threads write to large offsets in an object 3554 * to verify that parallel writes to an object -- even to the 3555 * same blocks within the object -- doesn't cause any trouble. 3556 */ 3557 ztest_od_init(&od[0], ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0); 3558 3559 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0) 3560 return; 3561 3562 while (ztest_random(10) != 0) 3563 ztest_io(zd, od[0].od_object, offset); 3564 } 3565 3566 void 3567 ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id) 3568 { 3569 ztest_od_t od[1]; 3570 uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) + 3571 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT); 3572 uint64_t count = ztest_random(20) + 1; 3573 uint64_t blocksize = ztest_random_blocksize(); 3574 void *data; 3575 3576 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0); 3577 3578 if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0) 3579 return; 3580 3581 if (ztest_truncate(zd, od[0].od_object, offset, count * blocksize) != 0) 3582 return; 3583 3584 ztest_prealloc(zd, od[0].od_object, offset, count * blocksize); 3585 3586 data = umem_zalloc(blocksize, UMEM_NOFAIL); 3587 3588 while (ztest_random(count) != 0) { 3589 uint64_t randoff = offset + (ztest_random(count) * blocksize); 3590 if (ztest_write(zd, od[0].od_object, randoff, blocksize, 3591 data) != 0) 3592 break; 3593 while (ztest_random(4) != 0) 3594 ztest_io(zd, od[0].od_object, randoff); 3595 } 3596 3597 umem_free(data, blocksize); 3598 } 3599 3600 /* 3601 * Verify that zap_{create,destroy,add,remove,update} work as expected. 3602 */ 3603 #define ZTEST_ZAP_MIN_INTS 1 3604 #define ZTEST_ZAP_MAX_INTS 4 3605 #define ZTEST_ZAP_MAX_PROPS 1000 3606 3607 void 3608 ztest_zap(ztest_ds_t *zd, uint64_t id) 3609 { 3610 objset_t *os = zd->zd_os; 3611 ztest_od_t od[1]; 3612 uint64_t object; 3613 uint64_t txg, last_txg; 3614 uint64_t value[ZTEST_ZAP_MAX_INTS]; 3615 uint64_t zl_ints, zl_intsize, prop; 3616 int i, ints; 3617 dmu_tx_t *tx; 3618 char propname[100], txgname[100]; 3619 int error; 3620 char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" }; 3621 3622 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0); 3623 3624 if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0) 3625 return; 3626 3627 object = od[0].od_object; 3628 3629 /* 3630 * Generate a known hash collision, and verify that 3631 * we can lookup and remove both entries. 3632 */ 3633 tx = dmu_tx_create(os); 3634 dmu_tx_hold_zap(tx, object, B_TRUE, NULL); 3635 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG); 3636 if (txg == 0) 3637 return; 3638 for (i = 0; i < 2; i++) { 3639 value[i] = i; 3640 VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t), 3641 1, &value[i], tx)); 3642 } 3643 for (i = 0; i < 2; i++) { 3644 VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i], 3645 sizeof (uint64_t), 1, &value[i], tx)); 3646 VERIFY3U(0, ==, 3647 zap_length(os, object, hc[i], &zl_intsize, &zl_ints)); 3648 ASSERT3U(zl_intsize, ==, sizeof (uint64_t)); 3649 ASSERT3U(zl_ints, ==, 1); 3650 } 3651 for (i = 0; i < 2; i++) { 3652 VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx)); 3653 } 3654 dmu_tx_commit(tx); 3655 3656 /* 3657 * Generate a buch of random entries. 3658 */ 3659 ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS); 3660 3661 prop = ztest_random(ZTEST_ZAP_MAX_PROPS); 3662 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop); 3663 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop); 3664 bzero(value, sizeof (value)); 3665 last_txg = 0; 3666 3667 /* 3668 * If these zap entries already exist, validate their contents. 3669 */ 3670 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints); 3671 if (error == 0) { 3672 ASSERT3U(zl_intsize, ==, sizeof (uint64_t)); 3673 ASSERT3U(zl_ints, ==, 1); 3674 3675 VERIFY(zap_lookup(os, object, txgname, zl_intsize, 3676 zl_ints, &last_txg) == 0); 3677 3678 VERIFY(zap_length(os, object, propname, &zl_intsize, 3679 &zl_ints) == 0); 3680 3681 ASSERT3U(zl_intsize, ==, sizeof (uint64_t)); 3682 ASSERT3U(zl_ints, ==, ints); 3683 3684 VERIFY(zap_lookup(os, object, propname, zl_intsize, 3685 zl_ints, value) == 0); 3686 3687 for (i = 0; i < ints; i++) { 3688 ASSERT3U(value[i], ==, last_txg + object + i); 3689 } 3690 } else { 3691 ASSERT3U(error, ==, ENOENT); 3692 } 3693 3694 /* 3695 * Atomically update two entries in our zap object. 3696 * The first is named txg_%llu, and contains the txg 3697 * in which the property was last updated. The second 3698 * is named prop_%llu, and the nth element of its value 3699 * should be txg + object + n. 3700 */ 3701 tx = dmu_tx_create(os); 3702 dmu_tx_hold_zap(tx, object, B_TRUE, NULL); 3703 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG); 3704 if (txg == 0) 3705 return; 3706 3707 if (last_txg > txg) 3708 fatal(0, "zap future leak: old %llu new %llu", last_txg, txg); 3709 3710 for (i = 0; i < ints; i++) 3711 value[i] = txg + object + i; 3712 3713 VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t), 3714 1, &txg, tx)); 3715 VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t), 3716 ints, value, tx)); 3717 3718 dmu_tx_commit(tx); 3719 3720 /* 3721 * Remove a random pair of entries. 3722 */ 3723 prop = ztest_random(ZTEST_ZAP_MAX_PROPS); 3724 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop); 3725 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop); 3726 3727 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints); 3728 3729 if (error == ENOENT) 3730 return; 3731 3732 ASSERT3U(error, ==, 0); 3733 3734 tx = dmu_tx_create(os); 3735 dmu_tx_hold_zap(tx, object, B_TRUE, NULL); 3736 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG); 3737 if (txg == 0) 3738 return; 3739 VERIFY3U(0, ==, zap_remove(os, object, txgname, tx)); 3740 VERIFY3U(0, ==, zap_remove(os, object, propname, tx)); 3741 dmu_tx_commit(tx); 3742 } 3743 3744 /* 3745 * Testcase to test the upgrading of a microzap to fatzap. 3746 */ 3747 void 3748 ztest_fzap(ztest_ds_t *zd, uint64_t id) 3749 { 3750 objset_t *os = zd->zd_os; 3751 ztest_od_t od[1]; 3752 uint64_t object, txg; 3753 3754 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0); 3755 3756 if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0) 3757 return; 3758 3759 object = od[0].od_object; 3760 3761 /* 3762 * Add entries to this ZAP and make sure it spills over 3763 * and gets upgraded to a fatzap. Also, since we are adding 3764 * 2050 entries we should see ptrtbl growth and leaf-block split. 3765 */ 3766 for (int i = 0; i < 2050; i++) { 3767 char name[MAXNAMELEN]; 3768 uint64_t value = i; 3769 dmu_tx_t *tx; 3770 int error; 3771 3772 (void) snprintf(name, sizeof (name), "fzap-%llu-%llu", 3773 id, value); 3774 3775 tx = dmu_tx_create(os); 3776 dmu_tx_hold_zap(tx, object, B_TRUE, name); 3777 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG); 3778 if (txg == 0) 3779 return; 3780 error = zap_add(os, object, name, sizeof (uint64_t), 1, 3781 &value, tx); 3782 ASSERT(error == 0 || error == EEXIST); 3783 dmu_tx_commit(tx); 3784 } 3785 } 3786 3787 /* ARGSUSED */ 3788 void 3789 ztest_zap_parallel(ztest_ds_t *zd, uint64_t id) 3790 { 3791 objset_t *os = zd->zd_os; 3792 ztest_od_t od[1]; 3793 uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc; 3794 dmu_tx_t *tx; 3795 int i, namelen, error; 3796 int micro = ztest_random(2); 3797 char name[20], string_value[20]; 3798 void *data; 3799 3800 ztest_od_init(&od[0], ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER, 0, 0); 3801 3802 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0) 3803 return; 3804 3805 object = od[0].od_object; 3806 3807 /* 3808 * Generate a random name of the form 'xxx.....' where each 3809 * x is a random printable character and the dots are dots. 3810 * There are 94 such characters, and the name length goes from 3811 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names. 3812 */ 3813 namelen = ztest_random(sizeof (name) - 5) + 5 + 1; 3814 3815 for (i = 0; i < 3; i++) 3816 name[i] = '!' + ztest_random('~' - '!' + 1); 3817 for (; i < namelen - 1; i++) 3818 name[i] = '.'; 3819 name[i] = '\0'; 3820 3821 if ((namelen & 1) || micro) { 3822 wsize = sizeof (txg); 3823 wc = 1; 3824 data = &txg; 3825 } else { 3826 wsize = 1; 3827 wc = namelen; 3828 data = string_value; 3829 } 3830 3831 count = -1ULL; 3832 VERIFY(zap_count(os, object, &count) == 0); 3833 ASSERT(count != -1ULL); 3834 3835 /* 3836 * Select an operation: length, lookup, add, update, remove. 3837 */ 3838 i = ztest_random(5); 3839 3840 if (i >= 2) { 3841 tx = dmu_tx_create(os); 3842 dmu_tx_hold_zap(tx, object, B_TRUE, NULL); 3843 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG); 3844 if (txg == 0) 3845 return; 3846 bcopy(name, string_value, namelen); 3847 } else { 3848 tx = NULL; 3849 txg = 0; 3850 bzero(string_value, namelen); 3851 } 3852 3853 switch (i) { 3854 3855 case 0: 3856 error = zap_length(os, object, name, &zl_wsize, &zl_wc); 3857 if (error == 0) { 3858 ASSERT3U(wsize, ==, zl_wsize); 3859 ASSERT3U(wc, ==, zl_wc); 3860 } else { 3861 ASSERT3U(error, ==, ENOENT); 3862 } 3863 break; 3864 3865 case 1: 3866 error = zap_lookup(os, object, name, wsize, wc, data); 3867 if (error == 0) { 3868 if (data == string_value && 3869 bcmp(name, data, namelen) != 0) 3870 fatal(0, "name '%s' != val '%s' len %d", 3871 name, data, namelen); 3872 } else { 3873 ASSERT3U(error, ==, ENOENT); 3874 } 3875 break; 3876 3877 case 2: 3878 error = zap_add(os, object, name, wsize, wc, data, tx); 3879 ASSERT(error == 0 || error == EEXIST); 3880 break; 3881 3882 case 3: 3883 VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0); 3884 break; 3885 3886 case 4: 3887 error = zap_remove(os, object, name, tx); 3888 ASSERT(error == 0 || error == ENOENT); 3889 break; 3890 } 3891 3892 if (tx != NULL) 3893 dmu_tx_commit(tx); 3894 } 3895 3896 /* 3897 * Commit callback data. 3898 */ 3899 typedef struct ztest_cb_data { 3900 list_node_t zcd_node; 3901 uint64_t zcd_txg; 3902 int zcd_expected_err; 3903 boolean_t zcd_added; 3904 boolean_t zcd_called; 3905 spa_t *zcd_spa; 3906 } ztest_cb_data_t; 3907 3908 /* This is the actual commit callback function */ 3909 static void 3910 ztest_commit_callback(void *arg, int error) 3911 { 3912 ztest_cb_data_t *data = arg; 3913 uint64_t synced_txg; 3914 3915 VERIFY(data != NULL); 3916 VERIFY3S(data->zcd_expected_err, ==, error); 3917 VERIFY(!data->zcd_called); 3918 3919 synced_txg = spa_last_synced_txg(data->zcd_spa); 3920 if (data->zcd_txg > synced_txg) 3921 fatal(0, "commit callback of txg %" PRIu64 " called prematurely" 3922 ", last synced txg = %" PRIu64 "\n", data->zcd_txg, 3923 synced_txg); 3924 3925 data->zcd_called = B_TRUE; 3926 3927 if (error == ECANCELED) { 3928 ASSERT3U(data->zcd_txg, ==, 0); 3929 ASSERT(!data->zcd_added); 3930 3931 /* 3932 * The private callback data should be destroyed here, but 3933 * since we are going to check the zcd_called field after 3934 * dmu_tx_abort(), we will destroy it there. 3935 */ 3936 return; 3937 } 3938 3939 /* Was this callback added to the global callback list? */ 3940 if (!data->zcd_added) 3941 goto out; 3942 3943 ASSERT3U(data->zcd_txg, !=, 0); 3944 3945 /* Remove our callback from the list */ 3946 (void) mutex_lock(&zcl.zcl_callbacks_lock); 3947 list_remove(&zcl.zcl_callbacks, data); 3948 (void) mutex_unlock(&zcl.zcl_callbacks_lock); 3949 3950 out: 3951 umem_free(data, sizeof (ztest_cb_data_t)); 3952 } 3953 3954 /* Allocate and initialize callback data structure */ 3955 static ztest_cb_data_t * 3956 ztest_create_cb_data(objset_t *os, uint64_t txg) 3957 { 3958 ztest_cb_data_t *cb_data; 3959 3960 cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL); 3961 3962 cb_data->zcd_txg = txg; 3963 cb_data->zcd_spa = dmu_objset_spa(os); 3964 3965 return (cb_data); 3966 } 3967 3968 /* 3969 * If a number of txgs equal to this threshold have been created after a commit 3970 * callback has been registered but not called, then we assume there is an 3971 * implementation bug. 3972 */ 3973 #define ZTEST_COMMIT_CALLBACK_THRESH (TXG_CONCURRENT_STATES + 2) 3974 3975 /* 3976 * Commit callback test. 3977 */ 3978 void 3979 ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id) 3980 { 3981 objset_t *os = zd->zd_os; 3982 ztest_od_t od[1]; 3983 dmu_tx_t *tx; 3984 ztest_cb_data_t *cb_data[3], *tmp_cb; 3985 uint64_t old_txg, txg; 3986 int i, error; 3987 3988 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0); 3989 3990 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0) 3991 return; 3992 3993 tx = dmu_tx_create(os); 3994 3995 cb_data[0] = ztest_create_cb_data(os, 0); 3996 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]); 3997 3998 dmu_tx_hold_write(tx, od[0].od_object, 0, sizeof (uint64_t)); 3999 4000 /* Every once in a while, abort the transaction on purpose */ 4001 if (ztest_random(100) == 0) 4002 error = -1; 4003 4004 if (!error) 4005 error = dmu_tx_assign(tx, TXG_NOWAIT); 4006 4007 txg = error ? 0 : dmu_tx_get_txg(tx); 4008 4009 cb_data[0]->zcd_txg = txg; 4010 cb_data[1] = ztest_create_cb_data(os, txg); 4011 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]); 4012 4013 if (error) { 4014 /* 4015 * It's not a strict requirement to call the registered 4016 * callbacks from inside dmu_tx_abort(), but that's what 4017 * it's supposed to happen in the current implementation 4018 * so we will check for that. 4019 */ 4020 for (i = 0; i < 2; i++) { 4021 cb_data[i]->zcd_expected_err = ECANCELED; 4022 VERIFY(!cb_data[i]->zcd_called); 4023 } 4024 4025 dmu_tx_abort(tx); 4026 4027 for (i = 0; i < 2; i++) { 4028 VERIFY(cb_data[i]->zcd_called); 4029 umem_free(cb_data[i], sizeof (ztest_cb_data_t)); 4030 } 4031 4032 return; 4033 } 4034 4035 cb_data[2] = ztest_create_cb_data(os, txg); 4036 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]); 4037 4038 /* 4039 * Read existing data to make sure there isn't a future leak. 4040 */ 4041 VERIFY(0 == dmu_read(os, od[0].od_object, 0, sizeof (uint64_t), 4042 &old_txg, DMU_READ_PREFETCH)); 4043 4044 if (old_txg > txg) 4045 fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64, 4046 old_txg, txg); 4047 4048 dmu_write(os, od[0].od_object, 0, sizeof (uint64_t), &txg, tx); 4049 4050 (void) mutex_lock(&zcl.zcl_callbacks_lock); 4051 4052 /* 4053 * Since commit callbacks don't have any ordering requirement and since 4054 * it is theoretically possible for a commit callback to be called 4055 * after an arbitrary amount of time has elapsed since its txg has been 4056 * synced, it is difficult to reliably determine whether a commit 4057 * callback hasn't been called due to high load or due to a flawed 4058 * implementation. 4059 * 4060 * In practice, we will assume that if after a certain number of txgs a 4061 * commit callback hasn't been called, then most likely there's an 4062 * implementation bug.. 4063 */ 4064 tmp_cb = list_head(&zcl.zcl_callbacks); 4065 if (tmp_cb != NULL && 4066 tmp_cb->zcd_txg > txg - ZTEST_COMMIT_CALLBACK_THRESH) { 4067 fatal(0, "Commit callback threshold exceeded, oldest txg: %" 4068 PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg); 4069 } 4070 4071 /* 4072 * Let's find the place to insert our callbacks. 4073 * 4074 * Even though the list is ordered by txg, it is possible for the 4075 * insertion point to not be the end because our txg may already be 4076 * quiescing at this point and other callbacks in the open txg 4077 * (from other objsets) may have sneaked in. 4078 */ 4079 tmp_cb = list_tail(&zcl.zcl_callbacks); 4080 while (tmp_cb != NULL && tmp_cb->zcd_txg > txg) 4081 tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb); 4082 4083 /* Add the 3 callbacks to the list */ 4084 for (i = 0; i < 3; i++) { 4085 if (tmp_cb == NULL) 4086 list_insert_head(&zcl.zcl_callbacks, cb_data[i]); 4087 else 4088 list_insert_after(&zcl.zcl_callbacks, tmp_cb, 4089 cb_data[i]); 4090 4091 cb_data[i]->zcd_added = B_TRUE; 4092 VERIFY(!cb_data[i]->zcd_called); 4093 4094 tmp_cb = cb_data[i]; 4095 } 4096 4097 (void) mutex_unlock(&zcl.zcl_callbacks_lock); 4098 4099 dmu_tx_commit(tx); 4100 } 4101 4102 /* ARGSUSED */ 4103 void 4104 ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id) 4105 { 4106 zfs_prop_t proplist[] = { 4107 ZFS_PROP_CHECKSUM, 4108 ZFS_PROP_COMPRESSION, 4109 ZFS_PROP_COPIES, 4110 ZFS_PROP_DEDUP 4111 }; 4112 ztest_shared_t *zs = ztest_shared; 4113 4114 (void) rw_rdlock(&zs->zs_name_lock); 4115 4116 for (int p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++) 4117 (void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p], 4118 ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2)); 4119 4120 (void) rw_unlock(&zs->zs_name_lock); 4121 } 4122 4123 /* ARGSUSED */ 4124 void 4125 ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id) 4126 { 4127 ztest_shared_t *zs = ztest_shared; 4128 nvlist_t *props = NULL; 4129 4130 (void) rw_rdlock(&zs->zs_name_lock); 4131 4132 #if 0 4133 (void) ztest_spa_prop_set_uint64(zs, ZPOOL_PROP_DEDUPDITTO, 4134 ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN)); 4135 #endif 4136 4137 VERIFY3U(spa_prop_get(zs->zs_spa, &props), ==, 0); 4138 4139 if (zopt_verbose >= 6) 4140 dump_nvlist(props, 4); 4141 4142 nvlist_free(props); 4143 4144 (void) rw_unlock(&zs->zs_name_lock); 4145 } 4146 4147 /* 4148 * Test snapshot hold/release and deferred destroy. 4149 */ 4150 void 4151 ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id) 4152 { 4153 int error; 4154 objset_t *os = zd->zd_os; 4155 objset_t *origin; 4156 char snapname[100]; 4157 char fullname[100]; 4158 char clonename[100]; 4159 char tag[100]; 4160 char osname[MAXNAMELEN]; 4161 4162 (void) rw_rdlock(&ztest_shared->zs_name_lock); 4163 4164 dmu_objset_name(os, osname); 4165 4166 (void) snprintf(snapname, 100, "sh1_%llu", id); 4167 (void) snprintf(fullname, 100, "%s@%s", osname, snapname); 4168 (void) snprintf(clonename, 100, "%s/ch1_%llu", osname, id); 4169 (void) snprintf(tag, 100, "%tag_%llu", id); 4170 4171 /* 4172 * Clean up from any previous run. 4173 */ 4174 (void) dmu_objset_destroy(clonename, B_FALSE); 4175 (void) dsl_dataset_user_release(osname, snapname, tag, B_FALSE); 4176 (void) dmu_objset_destroy(fullname, B_FALSE); 4177 4178 /* 4179 * Create snapshot, clone it, mark snap for deferred destroy, 4180 * destroy clone, verify snap was also destroyed. 4181 */ 4182 error = dmu_objset_snapshot(osname, snapname, NULL, FALSE); 4183 if (error) { 4184 if (error == ENOSPC) { 4185 ztest_record_enospc("dmu_objset_snapshot"); 4186 goto out; 4187 } 4188 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error); 4189 } 4190 4191 error = dmu_objset_hold(fullname, FTAG, &origin); 4192 if (error) 4193 fatal(0, "dmu_objset_hold(%s) = %d", fullname, error); 4194 4195 error = dmu_objset_clone(clonename, dmu_objset_ds(origin), 0); 4196 dmu_objset_rele(origin, FTAG); 4197 if (error) { 4198 if (error == ENOSPC) { 4199 ztest_record_enospc("dmu_objset_clone"); 4200 goto out; 4201 } 4202 fatal(0, "dmu_objset_clone(%s) = %d", clonename, error); 4203 } 4204 4205 error = dmu_objset_destroy(fullname, B_TRUE); 4206 if (error) { 4207 fatal(0, "dmu_objset_destroy(%s, B_TRUE) = %d", 4208 fullname, error); 4209 } 4210 4211 error = dmu_objset_destroy(clonename, B_FALSE); 4212 if (error) 4213 fatal(0, "dmu_objset_destroy(%s) = %d", clonename, error); 4214 4215 error = dmu_objset_hold(fullname, FTAG, &origin); 4216 if (error != ENOENT) 4217 fatal(0, "dmu_objset_hold(%s) = %d", fullname, error); 4218 4219 /* 4220 * Create snapshot, add temporary hold, verify that we can't 4221 * destroy a held snapshot, mark for deferred destroy, 4222 * release hold, verify snapshot was destroyed. 4223 */ 4224 error = dmu_objset_snapshot(osname, snapname, NULL, FALSE); 4225 if (error) { 4226 if (error == ENOSPC) { 4227 ztest_record_enospc("dmu_objset_snapshot"); 4228 goto out; 4229 } 4230 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error); 4231 } 4232 4233 error = dsl_dataset_user_hold(osname, snapname, tag, B_FALSE, B_TRUE); 4234 if (error) 4235 fatal(0, "dsl_dataset_user_hold(%s)", fullname, tag); 4236 4237 error = dmu_objset_destroy(fullname, B_FALSE); 4238 if (error != EBUSY) { 4239 fatal(0, "dmu_objset_destroy(%s, B_FALSE) = %d", 4240 fullname, error); 4241 } 4242 4243 error = dmu_objset_destroy(fullname, B_TRUE); 4244 if (error) { 4245 fatal(0, "dmu_objset_destroy(%s, B_TRUE) = %d", 4246 fullname, error); 4247 } 4248 4249 error = dsl_dataset_user_release(osname, snapname, tag, B_FALSE); 4250 if (error) 4251 fatal(0, "dsl_dataset_user_release(%s)", fullname, tag); 4252 4253 VERIFY(dmu_objset_hold(fullname, FTAG, &origin) == ENOENT); 4254 4255 out: 4256 (void) rw_unlock(&ztest_shared->zs_name_lock); 4257 } 4258 4259 /* 4260 * Inject random faults into the on-disk data. 4261 */ 4262 /* ARGSUSED */ 4263 void 4264 ztest_fault_inject(ztest_ds_t *zd, uint64_t id) 4265 { 4266 ztest_shared_t *zs = ztest_shared; 4267 spa_t *spa = zs->zs_spa; 4268 int fd; 4269 uint64_t offset; 4270 uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz; 4271 uint64_t bad = 0x1990c0ffeedecade; 4272 uint64_t top, leaf; 4273 char path0[MAXPATHLEN]; 4274 char pathrand[MAXPATHLEN]; 4275 size_t fsize; 4276 int bshift = SPA_MAXBLOCKSHIFT + 2; /* don't scrog all labels */ 4277 int iters = 1000; 4278 int maxfaults = zopt_maxfaults; 4279 vdev_t *vd0 = NULL; 4280 uint64_t guid0 = 0; 4281 boolean_t islog = B_FALSE; 4282 4283 ASSERT(leaves >= 1); 4284 4285 /* 4286 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd. 4287 */ 4288 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 4289 4290 if (ztest_random(2) == 0) { 4291 /* 4292 * Inject errors on a normal data device or slog device. 4293 */ 4294 top = ztest_random_vdev_top(spa, B_TRUE); 4295 leaf = ztest_random(leaves); 4296 4297 /* 4298 * Generate paths to the first leaf in this top-level vdev, 4299 * and to the random leaf we selected. We'll induce transient 4300 * write failures and random online/offline activity on leaf 0, 4301 * and we'll write random garbage to the randomly chosen leaf. 4302 */ 4303 (void) snprintf(path0, sizeof (path0), ztest_dev_template, 4304 zopt_dir, zopt_pool, top * leaves + 0); 4305 (void) snprintf(pathrand, sizeof (pathrand), ztest_dev_template, 4306 zopt_dir, zopt_pool, top * leaves + leaf); 4307 4308 vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0); 4309 if (vd0 != NULL && vd0->vdev_top->vdev_islog) 4310 islog = B_TRUE; 4311 4312 if (vd0 != NULL && maxfaults != 1) { 4313 /* 4314 * Make vd0 explicitly claim to be unreadable, 4315 * or unwriteable, or reach behind its back 4316 * and close the underlying fd. We can do this if 4317 * maxfaults == 0 because we'll fail and reexecute, 4318 * and we can do it if maxfaults >= 2 because we'll 4319 * have enough redundancy. If maxfaults == 1, the 4320 * combination of this with injection of random data 4321 * corruption below exceeds the pool's fault tolerance. 4322 */ 4323 vdev_file_t *vf = vd0->vdev_tsd; 4324 4325 if (vf != NULL && ztest_random(3) == 0) { 4326 (void) close(vf->vf_vnode->v_fd); 4327 vf->vf_vnode->v_fd = -1; 4328 } else if (ztest_random(2) == 0) { 4329 vd0->vdev_cant_read = B_TRUE; 4330 } else { 4331 vd0->vdev_cant_write = B_TRUE; 4332 } 4333 guid0 = vd0->vdev_guid; 4334 } 4335 } else { 4336 /* 4337 * Inject errors on an l2cache device. 4338 */ 4339 spa_aux_vdev_t *sav = &spa->spa_l2cache; 4340 4341 if (sav->sav_count == 0) { 4342 spa_config_exit(spa, SCL_STATE, FTAG); 4343 return; 4344 } 4345 vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)]; 4346 guid0 = vd0->vdev_guid; 4347 (void) strcpy(path0, vd0->vdev_path); 4348 (void) strcpy(pathrand, vd0->vdev_path); 4349 4350 leaf = 0; 4351 leaves = 1; 4352 maxfaults = INT_MAX; /* no limit on cache devices */ 4353 } 4354 4355 spa_config_exit(spa, SCL_STATE, FTAG); 4356 4357 /* 4358 * If we can tolerate two or more faults, or we're dealing 4359 * with a slog, randomly online/offline vd0. 4360 */ 4361 if ((maxfaults >= 2 || islog) && guid0 != 0) { 4362 if (ztest_random(10) < 6) { 4363 int flags = (ztest_random(2) == 0 ? 4364 ZFS_OFFLINE_TEMPORARY : 0); 4365 4366 /* 4367 * We have to grab the zs_name_lock as writer to 4368 * prevent a race between offlining a slog and 4369 * destroying a dataset. Offlining the slog will 4370 * grab a reference on the dataset which may cause 4371 * dmu_objset_destroy() to fail with EBUSY thus 4372 * leaving the dataset in an inconsistent state. 4373 */ 4374 if (islog) 4375 (void) rw_wrlock(&ztest_shared->zs_name_lock); 4376 4377 VERIFY(vdev_offline(spa, guid0, flags) != EBUSY); 4378 4379 if (islog) 4380 (void) rw_unlock(&ztest_shared->zs_name_lock); 4381 } else { 4382 (void) vdev_online(spa, guid0, 0, NULL); 4383 } 4384 } 4385 4386 if (maxfaults == 0) 4387 return; 4388 4389 /* 4390 * We have at least single-fault tolerance, so inject data corruption. 4391 */ 4392 fd = open(pathrand, O_RDWR); 4393 4394 if (fd == -1) /* we hit a gap in the device namespace */ 4395 return; 4396 4397 fsize = lseek(fd, 0, SEEK_END); 4398 4399 while (--iters != 0) { 4400 offset = ztest_random(fsize / (leaves << bshift)) * 4401 (leaves << bshift) + (leaf << bshift) + 4402 (ztest_random(1ULL << (bshift - 1)) & -8ULL); 4403 4404 if (offset >= fsize) 4405 continue; 4406 4407 if (zopt_verbose >= 7) 4408 (void) printf("injecting bad word into %s," 4409 " offset 0x%llx\n", pathrand, (u_longlong_t)offset); 4410 4411 if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad)) 4412 fatal(1, "can't inject bad word at 0x%llx in %s", 4413 offset, pathrand); 4414 } 4415 4416 (void) close(fd); 4417 } 4418 4419 /* 4420 * Verify that DDT repair works as expected. 4421 */ 4422 void 4423 ztest_ddt_repair(ztest_ds_t *zd, uint64_t id) 4424 { 4425 ztest_shared_t *zs = ztest_shared; 4426 spa_t *spa = zs->zs_spa; 4427 objset_t *os = zd->zd_os; 4428 ztest_od_t od[1]; 4429 uint64_t object, blocksize, txg, pattern, psize; 4430 enum zio_checksum checksum = spa_dedup_checksum(spa); 4431 dmu_buf_t *db; 4432 dmu_tx_t *tx; 4433 void *buf; 4434 blkptr_t blk; 4435 int copies = 2 * ZIO_DEDUPDITTO_MIN; 4436 4437 blocksize = ztest_random_blocksize(); 4438 blocksize = MIN(blocksize, 2048); /* because we write so many */ 4439 4440 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0); 4441 4442 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0) 4443 return; 4444 4445 /* 4446 * Take the name lock as writer to prevent anyone else from changing 4447 * the pool and dataset properies we need to maintain during this test. 4448 */ 4449 (void) rw_wrlock(&zs->zs_name_lock); 4450 4451 if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum, 4452 B_FALSE) != 0 || 4453 ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1, 4454 B_FALSE) != 0) { 4455 (void) rw_unlock(&zs->zs_name_lock); 4456 return; 4457 } 4458 4459 object = od[0].od_object; 4460 blocksize = od[0].od_blocksize; 4461 pattern = spa_guid(spa) ^ dmu_objset_fsid_guid(os); 4462 4463 ASSERT(object != 0); 4464 4465 tx = dmu_tx_create(os); 4466 dmu_tx_hold_write(tx, object, 0, copies * blocksize); 4467 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG); 4468 if (txg == 0) { 4469 (void) rw_unlock(&zs->zs_name_lock); 4470 return; 4471 } 4472 4473 /* 4474 * Write all the copies of our block. 4475 */ 4476 for (int i = 0; i < copies; i++) { 4477 uint64_t offset = i * blocksize; 4478 VERIFY(dmu_buf_hold(os, object, offset, FTAG, &db) == 0); 4479 ASSERT(db->db_offset == offset); 4480 ASSERT(db->db_size == blocksize); 4481 ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) || 4482 ztest_pattern_match(db->db_data, db->db_size, 0ULL)); 4483 dmu_buf_will_fill(db, tx); 4484 ztest_pattern_set(db->db_data, db->db_size, pattern); 4485 dmu_buf_rele(db, FTAG); 4486 } 4487 4488 dmu_tx_commit(tx); 4489 txg_wait_synced(spa_get_dsl(spa), txg); 4490 4491 /* 4492 * Find out what block we got. 4493 */ 4494 VERIFY(dmu_buf_hold(os, object, 0, FTAG, &db) == 0); 4495 blk = *((dmu_buf_impl_t *)db)->db_blkptr; 4496 dmu_buf_rele(db, FTAG); 4497 4498 /* 4499 * Damage the block. Dedup-ditto will save us when we read it later. 4500 */ 4501 psize = BP_GET_PSIZE(&blk); 4502 buf = zio_buf_alloc(psize); 4503 ztest_pattern_set(buf, psize, ~pattern); 4504 4505 (void) zio_wait(zio_rewrite(NULL, spa, 0, &blk, 4506 buf, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE, 4507 ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL)); 4508 4509 zio_buf_free(buf, psize); 4510 4511 (void) rw_unlock(&zs->zs_name_lock); 4512 } 4513 4514 /* 4515 * Scrub the pool. 4516 */ 4517 /* ARGSUSED */ 4518 void 4519 ztest_scrub(ztest_ds_t *zd, uint64_t id) 4520 { 4521 ztest_shared_t *zs = ztest_shared; 4522 spa_t *spa = zs->zs_spa; 4523 4524 (void) spa_scrub(spa, POOL_SCRUB_EVERYTHING); 4525 (void) poll(NULL, 0, 100); /* wait a moment, then force a restart */ 4526 (void) spa_scrub(spa, POOL_SCRUB_EVERYTHING); 4527 } 4528 4529 /* 4530 * Rename the pool to a different name and then rename it back. 4531 */ 4532 /* ARGSUSED */ 4533 void 4534 ztest_spa_rename(ztest_ds_t *zd, uint64_t id) 4535 { 4536 ztest_shared_t *zs = ztest_shared; 4537 char *oldname, *newname; 4538 spa_t *spa; 4539 4540 (void) rw_wrlock(&zs->zs_name_lock); 4541 4542 oldname = zs->zs_pool; 4543 newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL); 4544 (void) strcpy(newname, oldname); 4545 (void) strcat(newname, "_tmp"); 4546 4547 /* 4548 * Do the rename 4549 */ 4550 VERIFY3U(0, ==, spa_rename(oldname, newname)); 4551 4552 /* 4553 * Try to open it under the old name, which shouldn't exist 4554 */ 4555 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG)); 4556 4557 /* 4558 * Open it under the new name and make sure it's still the same spa_t. 4559 */ 4560 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG)); 4561 4562 ASSERT(spa == zs->zs_spa); 4563 spa_close(spa, FTAG); 4564 4565 /* 4566 * Rename it back to the original 4567 */ 4568 VERIFY3U(0, ==, spa_rename(newname, oldname)); 4569 4570 /* 4571 * Make sure it can still be opened 4572 */ 4573 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG)); 4574 4575 ASSERT(spa == zs->zs_spa); 4576 spa_close(spa, FTAG); 4577 4578 umem_free(newname, strlen(newname) + 1); 4579 4580 (void) rw_unlock(&zs->zs_name_lock); 4581 } 4582 4583 /* 4584 * Verify pool integrity by running zdb. 4585 */ 4586 static void 4587 ztest_run_zdb(char *pool) 4588 { 4589 int status; 4590 char zdb[MAXPATHLEN + MAXNAMELEN + 20]; 4591 char zbuf[1024]; 4592 char *bin; 4593 char *ztest; 4594 char *isa; 4595 int isalen; 4596 FILE *fp; 4597 4598 (void) realpath(getexecname(), zdb); 4599 4600 /* zdb lives in /usr/sbin, while ztest lives in /usr/bin */ 4601 bin = strstr(zdb, "/usr/bin/"); 4602 ztest = strstr(bin, "/ztest"); 4603 isa = bin + 8; 4604 isalen = ztest - isa; 4605 isa = strdup(isa); 4606 /* LINTED */ 4607 (void) sprintf(bin, 4608 "/usr/sbin%.*s/zdb -bcc%s%s -U /tmp/zpool.cache %s", 4609 isalen, 4610 isa, 4611 zopt_verbose >= 3 ? "s" : "", 4612 zopt_verbose >= 4 ? "v" : "", 4613 pool); 4614 free(isa); 4615 4616 if (zopt_verbose >= 5) 4617 (void) printf("Executing %s\n", strstr(zdb, "zdb ")); 4618 4619 fp = popen(zdb, "r"); 4620 4621 while (fgets(zbuf, sizeof (zbuf), fp) != NULL) 4622 if (zopt_verbose >= 3) 4623 (void) printf("%s", zbuf); 4624 4625 status = pclose(fp); 4626 4627 if (status == 0) 4628 return; 4629 4630 ztest_dump_core = 0; 4631 if (WIFEXITED(status)) 4632 fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status)); 4633 else 4634 fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status)); 4635 } 4636 4637 static void 4638 ztest_walk_pool_directory(char *header) 4639 { 4640 spa_t *spa = NULL; 4641 4642 if (zopt_verbose >= 6) 4643 (void) printf("%s\n", header); 4644 4645 mutex_enter(&spa_namespace_lock); 4646 while ((spa = spa_next(spa)) != NULL) 4647 if (zopt_verbose >= 6) 4648 (void) printf("\t%s\n", spa_name(spa)); 4649 mutex_exit(&spa_namespace_lock); 4650 } 4651 4652 static void 4653 ztest_spa_import_export(char *oldname, char *newname) 4654 { 4655 nvlist_t *config, *newconfig; 4656 uint64_t pool_guid; 4657 spa_t *spa; 4658 4659 if (zopt_verbose >= 4) { 4660 (void) printf("import/export: old = %s, new = %s\n", 4661 oldname, newname); 4662 } 4663 4664 /* 4665 * Clean up from previous runs. 4666 */ 4667 (void) spa_destroy(newname); 4668 4669 /* 4670 * Get the pool's configuration and guid. 4671 */ 4672 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG)); 4673 4674 /* 4675 * Kick off a scrub to tickle scrub/export races. 4676 */ 4677 if (ztest_random(2) == 0) 4678 (void) spa_scrub(spa, POOL_SCRUB_EVERYTHING); 4679 4680 pool_guid = spa_guid(spa); 4681 spa_close(spa, FTAG); 4682 4683 ztest_walk_pool_directory("pools before export"); 4684 4685 /* 4686 * Export it. 4687 */ 4688 VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE)); 4689 4690 ztest_walk_pool_directory("pools after export"); 4691 4692 /* 4693 * Try to import it. 4694 */ 4695 newconfig = spa_tryimport(config); 4696 ASSERT(newconfig != NULL); 4697 nvlist_free(newconfig); 4698 4699 /* 4700 * Import it under the new name. 4701 */ 4702 VERIFY3U(0, ==, spa_import(newname, config, NULL)); 4703 4704 ztest_walk_pool_directory("pools after import"); 4705 4706 /* 4707 * Try to import it again -- should fail with EEXIST. 4708 */ 4709 VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL)); 4710 4711 /* 4712 * Try to import it under a different name -- should fail with EEXIST. 4713 */ 4714 VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL)); 4715 4716 /* 4717 * Verify that the pool is no longer visible under the old name. 4718 */ 4719 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG)); 4720 4721 /* 4722 * Verify that we can open and close the pool using the new name. 4723 */ 4724 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG)); 4725 ASSERT(pool_guid == spa_guid(spa)); 4726 spa_close(spa, FTAG); 4727 4728 nvlist_free(config); 4729 } 4730 4731 static void 4732 ztest_resume(spa_t *spa) 4733 { 4734 if (spa_suspended(spa) && zopt_verbose >= 6) 4735 (void) printf("resuming from suspended state\n"); 4736 spa_vdev_state_enter(spa, SCL_NONE); 4737 vdev_clear(spa, NULL); 4738 (void) spa_vdev_state_exit(spa, NULL, 0); 4739 (void) zio_resume(spa); 4740 } 4741 4742 static void * 4743 ztest_resume_thread(void *arg) 4744 { 4745 spa_t *spa = arg; 4746 4747 while (!ztest_exiting) { 4748 if (spa_suspended(spa)) 4749 ztest_resume(spa); 4750 (void) poll(NULL, 0, 100); 4751 } 4752 return (NULL); 4753 } 4754 4755 static void * 4756 ztest_deadman_thread(void *arg) 4757 { 4758 ztest_shared_t *zs = arg; 4759 int grace = 300; 4760 hrtime_t delta; 4761 4762 delta = (zs->zs_thread_stop - zs->zs_thread_start) / NANOSEC + grace; 4763 4764 (void) poll(NULL, 0, (int)(1000 * delta)); 4765 4766 fatal(0, "failed to complete within %d seconds of deadline", grace); 4767 4768 return (NULL); 4769 } 4770 4771 static void 4772 ztest_execute(ztest_info_t *zi, uint64_t id) 4773 { 4774 ztest_shared_t *zs = ztest_shared; 4775 ztest_ds_t *zd = &zs->zs_zd[id % zopt_datasets]; 4776 hrtime_t functime = gethrtime(); 4777 4778 for (int i = 0; i < zi->zi_iters; i++) 4779 zi->zi_func(zd, id); 4780 4781 functime = gethrtime() - functime; 4782 4783 atomic_add_64(&zi->zi_call_count, 1); 4784 atomic_add_64(&zi->zi_call_time, functime); 4785 4786 if (zopt_verbose >= 4) { 4787 Dl_info dli; 4788 (void) dladdr((void *)zi->zi_func, &dli); 4789 (void) printf("%6.2f sec in %s\n", 4790 (double)functime / NANOSEC, dli.dli_sname); 4791 } 4792 } 4793 4794 static void * 4795 ztest_thread(void *arg) 4796 { 4797 uint64_t id = (uintptr_t)arg; 4798 ztest_shared_t *zs = ztest_shared; 4799 uint64_t call_next; 4800 hrtime_t now; 4801 ztest_info_t *zi; 4802 4803 while ((now = gethrtime()) < zs->zs_thread_stop) { 4804 /* 4805 * See if it's time to force a crash. 4806 */ 4807 if (now > zs->zs_thread_kill) 4808 ztest_kill(zs); 4809 4810 /* 4811 * If we're getting ENOSPC with some regularity, stop. 4812 */ 4813 if (zs->zs_enospc_count > 10) 4814 break; 4815 4816 /* 4817 * Pick a random function to execute. 4818 */ 4819 zi = &zs->zs_info[ztest_random(ZTEST_FUNCS)]; 4820 call_next = zi->zi_call_next; 4821 4822 if (now >= call_next && 4823 atomic_cas_64(&zi->zi_call_next, call_next, call_next + 4824 ztest_random(2 * zi->zi_interval[0] + 1)) == call_next) 4825 ztest_execute(zi, id); 4826 } 4827 4828 return (NULL); 4829 } 4830 4831 static void 4832 ztest_dataset_name(char *dsname, char *pool, int d) 4833 { 4834 (void) snprintf(dsname, MAXNAMELEN, "%s/ds_%d", pool, d); 4835 } 4836 4837 static void 4838 ztest_dataset_destroy(ztest_shared_t *zs, int d) 4839 { 4840 char name[MAXNAMELEN]; 4841 4842 ztest_dataset_name(name, zs->zs_pool, d); 4843 4844 if (zopt_verbose >= 3) 4845 (void) printf("Destroying %s to free up space\n", name); 4846 4847 /* 4848 * Cleanup any non-standard clones and snapshots. In general, 4849 * ztest thread t operates on dataset (t % zopt_datasets), 4850 * so there may be more than one thing to clean up. 4851 */ 4852 for (int t = d; t < zopt_threads; t += zopt_datasets) 4853 ztest_dsl_dataset_cleanup(name, t); 4854 4855 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL, 4856 DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN); 4857 } 4858 4859 static void 4860 ztest_dataset_dirobj_verify(ztest_ds_t *zd) 4861 { 4862 uint64_t usedobjs, dirobjs, scratch; 4863 4864 /* 4865 * ZTEST_DIROBJ is the object directory for the entire dataset. 4866 * Therefore, the number of objects in use should equal the 4867 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself. 4868 * If not, we have an object leak. 4869 * 4870 * Note that we can only check this in ztest_dataset_open(), 4871 * when the open-context and syncing-context values agree. 4872 * That's because zap_count() returns the open-context value, 4873 * while dmu_objset_space() returns the rootbp fill count. 4874 */ 4875 VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs)); 4876 dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch); 4877 ASSERT3U(dirobjs + 1, ==, usedobjs); 4878 } 4879 4880 static int 4881 ztest_dataset_open(ztest_shared_t *zs, int d) 4882 { 4883 ztest_ds_t *zd = &zs->zs_zd[d]; 4884 uint64_t committed_seq = zd->zd_seq; 4885 objset_t *os; 4886 zilog_t *zilog; 4887 char name[MAXNAMELEN]; 4888 int error; 4889 4890 ztest_dataset_name(name, zs->zs_pool, d); 4891 4892 (void) rw_rdlock(&zs->zs_name_lock); 4893 4894 error = dmu_objset_create(name, DMU_OST_OTHER, 0, 4895 ztest_objset_create_cb, NULL); 4896 if (error == ENOSPC) { 4897 (void) rw_unlock(&zs->zs_name_lock); 4898 ztest_record_enospc(FTAG); 4899 return (error); 4900 } 4901 ASSERT(error == 0 || error == EEXIST); 4902 4903 VERIFY3U(dmu_objset_hold(name, zd, &os), ==, 0); 4904 (void) rw_unlock(&zs->zs_name_lock); 4905 4906 ztest_zd_init(zd, os); 4907 4908 zilog = zd->zd_zilog; 4909 4910 if (zilog->zl_header->zh_claim_lr_seq != 0 && 4911 zilog->zl_header->zh_claim_lr_seq < committed_seq) 4912 fatal(0, "missing log records: claimed %llu < committed %llu", 4913 zilog->zl_header->zh_claim_lr_seq, committed_seq); 4914 4915 ztest_dataset_dirobj_verify(zd); 4916 4917 zil_replay(os, zd, ztest_replay_vector); 4918 4919 ztest_dataset_dirobj_verify(zd); 4920 4921 if (zopt_verbose >= 6) 4922 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n", 4923 zd->zd_name, 4924 (u_longlong_t)zilog->zl_parse_blk_count, 4925 (u_longlong_t)zilog->zl_parse_lr_count, 4926 (u_longlong_t)zilog->zl_replaying_seq); 4927 4928 zilog = zil_open(os, ztest_get_data); 4929 4930 if (zilog->zl_replaying_seq != 0 && 4931 zilog->zl_replaying_seq < committed_seq) 4932 fatal(0, "missing log records: replayed %llu < committed %llu", 4933 zilog->zl_replaying_seq, committed_seq); 4934 4935 return (0); 4936 } 4937 4938 static void 4939 ztest_dataset_close(ztest_shared_t *zs, int d) 4940 { 4941 ztest_ds_t *zd = &zs->zs_zd[d]; 4942 4943 zil_close(zd->zd_zilog); 4944 dmu_objset_rele(zd->zd_os, zd); 4945 4946 ztest_zd_fini(zd); 4947 } 4948 4949 /* 4950 * Kick off threads to run tests on all datasets in parallel. 4951 */ 4952 static void 4953 ztest_run(ztest_shared_t *zs) 4954 { 4955 thread_t *tid; 4956 spa_t *spa; 4957 thread_t resume_tid; 4958 int error; 4959 4960 ztest_exiting = B_FALSE; 4961 4962 /* 4963 * Initialize parent/child shared state. 4964 */ 4965 VERIFY(_mutex_init(&zs->zs_vdev_lock, USYNC_THREAD, NULL) == 0); 4966 VERIFY(rwlock_init(&zs->zs_name_lock, USYNC_THREAD, NULL) == 0); 4967 4968 zs->zs_thread_start = gethrtime(); 4969 zs->zs_thread_stop = zs->zs_thread_start + zopt_passtime * NANOSEC; 4970 zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop); 4971 zs->zs_thread_kill = zs->zs_thread_stop; 4972 if (ztest_random(100) < zopt_killrate) 4973 zs->zs_thread_kill -= ztest_random(zopt_passtime * NANOSEC); 4974 4975 (void) _mutex_init(&zcl.zcl_callbacks_lock, USYNC_THREAD, NULL); 4976 4977 list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t), 4978 offsetof(ztest_cb_data_t, zcd_node)); 4979 4980 /* 4981 * Open our pool. 4982 */ 4983 kernel_init(FREAD | FWRITE); 4984 VERIFY(spa_open(zs->zs_pool, &spa, FTAG) == 0); 4985 zs->zs_spa = spa; 4986 4987 spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN; 4988 4989 /* 4990 * We don't expect the pool to suspend unless maxfaults == 0, 4991 * in which case ztest_fault_inject() temporarily takes away 4992 * the only valid replica. 4993 */ 4994 if (zopt_maxfaults == 0) 4995 spa->spa_failmode = ZIO_FAILURE_MODE_WAIT; 4996 else 4997 spa->spa_failmode = ZIO_FAILURE_MODE_PANIC; 4998 4999 /* 5000 * Create a thread to periodically resume suspended I/O. 5001 */ 5002 VERIFY(thr_create(0, 0, ztest_resume_thread, spa, THR_BOUND, 5003 &resume_tid) == 0); 5004 5005 /* 5006 * Create a deadman thread to abort() if we hang. 5007 */ 5008 VERIFY(thr_create(0, 0, ztest_deadman_thread, zs, THR_BOUND, 5009 NULL) == 0); 5010 5011 /* 5012 * Verify that we can safely inquire about about any object, 5013 * whether it's allocated or not. To make it interesting, 5014 * we probe a 5-wide window around each power of two. 5015 * This hits all edge cases, including zero and the max. 5016 */ 5017 for (int t = 0; t < 64; t++) { 5018 for (int d = -5; d <= 5; d++) { 5019 error = dmu_object_info(spa->spa_meta_objset, 5020 (1ULL << t) + d, NULL); 5021 ASSERT(error == 0 || error == ENOENT || 5022 error == EINVAL); 5023 } 5024 } 5025 5026 /* 5027 * If we got any ENOSPC errors on the previous run, destroy something. 5028 */ 5029 if (zs->zs_enospc_count != 0) { 5030 int d = ztest_random(zopt_datasets); 5031 ztest_dataset_destroy(zs, d); 5032 } 5033 zs->zs_enospc_count = 0; 5034 5035 tid = umem_zalloc(zopt_threads * sizeof (thread_t), UMEM_NOFAIL); 5036 5037 if (zopt_verbose >= 4) 5038 (void) printf("starting main threads...\n"); 5039 5040 /* 5041 * Kick off all the tests that run in parallel. 5042 */ 5043 for (int t = 0; t < zopt_threads; t++) { 5044 if (t < zopt_datasets && ztest_dataset_open(zs, t) != 0) 5045 return; 5046 VERIFY(thr_create(0, 0, ztest_thread, (void *)(uintptr_t)t, 5047 THR_BOUND, &tid[t]) == 0); 5048 } 5049 5050 /* 5051 * Wait for all of the tests to complete. We go in reverse order 5052 * so we don't close datasets while threads are still using them. 5053 */ 5054 for (int t = zopt_threads - 1; t >= 0; t--) { 5055 VERIFY(thr_join(tid[t], NULL, NULL) == 0); 5056 if (t < zopt_datasets) 5057 ztest_dataset_close(zs, t); 5058 } 5059 5060 txg_wait_synced(spa_get_dsl(spa), 0); 5061 5062 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa)); 5063 zs->zs_space = metaslab_class_get_space(spa_normal_class(spa)); 5064 5065 umem_free(tid, zopt_threads * sizeof (thread_t)); 5066 5067 /* Kill the resume thread */ 5068 ztest_exiting = B_TRUE; 5069 VERIFY(thr_join(resume_tid, NULL, NULL) == 0); 5070 ztest_resume(spa); 5071 5072 /* 5073 * Right before closing the pool, kick off a bunch of async I/O; 5074 * spa_close() should wait for it to complete. 5075 */ 5076 for (uint64_t object = 1; object < 50; object++) 5077 dmu_prefetch(spa->spa_meta_objset, object, 0, 1ULL << 20); 5078 5079 spa_close(spa, FTAG); 5080 5081 /* 5082 * Verify that we can loop over all pools. 5083 */ 5084 mutex_enter(&spa_namespace_lock); 5085 for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa)) 5086 if (zopt_verbose > 3) 5087 (void) printf("spa_next: found %s\n", spa_name(spa)); 5088 mutex_exit(&spa_namespace_lock); 5089 5090 /* 5091 * Verify that we can export the pool and reimport it under a 5092 * different name. 5093 */ 5094 if (ztest_random(2) == 0) { 5095 char name[MAXNAMELEN]; 5096 (void) snprintf(name, MAXNAMELEN, "%s_import", zs->zs_pool); 5097 ztest_spa_import_export(zs->zs_pool, name); 5098 ztest_spa_import_export(name, zs->zs_pool); 5099 } 5100 5101 kernel_fini(); 5102 } 5103 5104 static void 5105 ztest_freeze(ztest_shared_t *zs) 5106 { 5107 ztest_ds_t *zd = &zs->zs_zd[0]; 5108 spa_t *spa; 5109 5110 if (zopt_verbose >= 3) 5111 (void) printf("testing spa_freeze()...\n"); 5112 5113 kernel_init(FREAD | FWRITE); 5114 VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG)); 5115 VERIFY3U(0, ==, ztest_dataset_open(zs, 0)); 5116 5117 /* 5118 * Force the first log block to be transactionally allocated. 5119 * We have to do this before we freeze the pool -- otherwise 5120 * the log chain won't be anchored. 5121 */ 5122 while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) { 5123 ztest_dmu_object_alloc_free(zd, 0); 5124 zil_commit(zd->zd_zilog, UINT64_MAX, 0); 5125 } 5126 5127 txg_wait_synced(spa_get_dsl(spa), 0); 5128 5129 /* 5130 * Freeze the pool. This stops spa_sync() from doing anything, 5131 * so that the only way to record changes from now on is the ZIL. 5132 */ 5133 spa_freeze(spa); 5134 5135 /* 5136 * Run tests that generate log records but don't alter the pool config 5137 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc). 5138 * We do a txg_wait_synced() after each iteration to force the txg 5139 * to increase well beyond the last synced value in the uberblock. 5140 * The ZIL should be OK with that. 5141 */ 5142 while (ztest_random(20) != 0) { 5143 ztest_dmu_write_parallel(zd, 0); 5144 ztest_dmu_object_alloc_free(zd, 0); 5145 txg_wait_synced(spa_get_dsl(spa), 0); 5146 } 5147 5148 /* 5149 * Commit all of the changes we just generated. 5150 */ 5151 zil_commit(zd->zd_zilog, UINT64_MAX, 0); 5152 txg_wait_synced(spa_get_dsl(spa), 0); 5153 5154 /* 5155 * Close our dataset and close the pool. 5156 */ 5157 ztest_dataset_close(zs, 0); 5158 spa_close(spa, FTAG); 5159 kernel_fini(); 5160 5161 /* 5162 * Open and close the pool and dataset to induce log replay. 5163 */ 5164 kernel_init(FREAD | FWRITE); 5165 VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG)); 5166 VERIFY3U(0, ==, ztest_dataset_open(zs, 0)); 5167 ztest_dataset_close(zs, 0); 5168 spa_close(spa, FTAG); 5169 kernel_fini(); 5170 5171 list_destroy(&zcl.zcl_callbacks); 5172 5173 (void) _mutex_destroy(&zcl.zcl_callbacks_lock); 5174 5175 (void) rwlock_destroy(&zs->zs_name_lock); 5176 (void) _mutex_destroy(&zs->zs_vdev_lock); 5177 } 5178 5179 void 5180 print_time(hrtime_t t, char *timebuf) 5181 { 5182 hrtime_t s = t / NANOSEC; 5183 hrtime_t m = s / 60; 5184 hrtime_t h = m / 60; 5185 hrtime_t d = h / 24; 5186 5187 s -= m * 60; 5188 m -= h * 60; 5189 h -= d * 24; 5190 5191 timebuf[0] = '\0'; 5192 5193 if (d) 5194 (void) sprintf(timebuf, 5195 "%llud%02lluh%02llum%02llus", d, h, m, s); 5196 else if (h) 5197 (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s); 5198 else if (m) 5199 (void) sprintf(timebuf, "%llum%02llus", m, s); 5200 else 5201 (void) sprintf(timebuf, "%llus", s); 5202 } 5203 5204 /* 5205 * Create a storage pool with the given name and initial vdev size. 5206 * Then test spa_freeze() functionality. 5207 */ 5208 static void 5209 ztest_init(ztest_shared_t *zs) 5210 { 5211 spa_t *spa; 5212 nvlist_t *nvroot; 5213 5214 VERIFY(_mutex_init(&zs->zs_vdev_lock, USYNC_THREAD, NULL) == 0); 5215 VERIFY(rwlock_init(&zs->zs_name_lock, USYNC_THREAD, NULL) == 0); 5216 5217 kernel_init(FREAD | FWRITE); 5218 5219 /* 5220 * Create the storage pool. 5221 */ 5222 (void) spa_destroy(zs->zs_pool); 5223 ztest_shared->zs_vdev_next_leaf = 0; 5224 nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0, 5225 0, zopt_raidz, zopt_mirrors, 1); 5226 VERIFY3U(0, ==, spa_create(zs->zs_pool, nvroot, NULL, NULL, NULL)); 5227 nvlist_free(nvroot); 5228 5229 VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG)); 5230 metaslab_sz = 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift; 5231 spa_close(spa, FTAG); 5232 5233 kernel_fini(); 5234 5235 ztest_run_zdb(zs->zs_pool); 5236 5237 ztest_freeze(zs); 5238 5239 ztest_run_zdb(zs->zs_pool); 5240 } 5241 5242 int 5243 main(int argc, char **argv) 5244 { 5245 int kills = 0; 5246 int iters = 0; 5247 ztest_shared_t *zs; 5248 size_t shared_size; 5249 ztest_info_t *zi; 5250 char timebuf[100]; 5251 char numbuf[6]; 5252 spa_t *spa; 5253 5254 (void) setvbuf(stdout, NULL, _IOLBF, 0); 5255 5256 /* Override location of zpool.cache */ 5257 spa_config_path = "/tmp/zpool.cache"; 5258 5259 ztest_random_fd = open("/dev/urandom", O_RDONLY); 5260 5261 process_options(argc, argv); 5262 5263 /* 5264 * Blow away any existing copy of zpool.cache 5265 */ 5266 if (zopt_init != 0) 5267 (void) remove("/tmp/zpool.cache"); 5268 5269 shared_size = sizeof (*zs) + zopt_datasets * sizeof (ztest_ds_t); 5270 5271 zs = ztest_shared = (void *)mmap(0, 5272 P2ROUNDUP(shared_size, getpagesize()), 5273 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0); 5274 5275 if (zopt_verbose >= 1) { 5276 (void) printf("%llu vdevs, %d datasets, %d threads," 5277 " %llu seconds...\n", 5278 (u_longlong_t)zopt_vdevs, zopt_datasets, zopt_threads, 5279 (u_longlong_t)zopt_time); 5280 } 5281 5282 /* 5283 * Create and initialize our storage pool. 5284 */ 5285 for (int i = 1; i <= zopt_init; i++) { 5286 bzero(zs, sizeof (ztest_shared_t)); 5287 if (zopt_verbose >= 3 && zopt_init != 1) 5288 (void) printf("ztest_init(), pass %d\n", i); 5289 zs->zs_pool = zopt_pool; 5290 ztest_init(zs); 5291 } 5292 5293 zs->zs_pool = zopt_pool; 5294 zs->zs_proc_start = gethrtime(); 5295 zs->zs_proc_stop = zs->zs_proc_start + zopt_time * NANOSEC; 5296 5297 for (int f = 0; f < ZTEST_FUNCS; f++) { 5298 zi = &zs->zs_info[f]; 5299 *zi = ztest_info[f]; 5300 if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop) 5301 zi->zi_call_next = UINT64_MAX; 5302 else 5303 zi->zi_call_next = zs->zs_proc_start + 5304 ztest_random(2 * zi->zi_interval[0] + 1); 5305 } 5306 5307 /* 5308 * Run the tests in a loop. These tests include fault injection 5309 * to verify that self-healing data works, and forced crashes 5310 * to verify that we never lose on-disk consistency. 5311 */ 5312 while (gethrtime() < zs->zs_proc_stop) { 5313 int status; 5314 pid_t pid; 5315 5316 /* 5317 * Initialize the workload counters for each function. 5318 */ 5319 for (int f = 0; f < ZTEST_FUNCS; f++) { 5320 zi = &zs->zs_info[f]; 5321 zi->zi_call_count = 0; 5322 zi->zi_call_time = 0; 5323 } 5324 5325 /* Set the allocation switch size */ 5326 metaslab_df_alloc_threshold = ztest_random(metaslab_sz / 4) + 1; 5327 5328 pid = fork(); 5329 5330 if (pid == -1) 5331 fatal(1, "fork failed"); 5332 5333 if (pid == 0) { /* child */ 5334 struct rlimit rl = { 1024, 1024 }; 5335 (void) setrlimit(RLIMIT_NOFILE, &rl); 5336 (void) enable_extended_FILE_stdio(-1, -1); 5337 ztest_run(zs); 5338 exit(0); 5339 } 5340 5341 while (waitpid(pid, &status, 0) != pid) 5342 continue; 5343 5344 if (WIFEXITED(status)) { 5345 if (WEXITSTATUS(status) != 0) { 5346 (void) fprintf(stderr, 5347 "child exited with code %d\n", 5348 WEXITSTATUS(status)); 5349 exit(2); 5350 } 5351 } else if (WIFSIGNALED(status)) { 5352 if (WTERMSIG(status) != SIGKILL) { 5353 (void) fprintf(stderr, 5354 "child died with signal %d\n", 5355 WTERMSIG(status)); 5356 exit(3); 5357 } 5358 kills++; 5359 } else { 5360 (void) fprintf(stderr, "something strange happened " 5361 "to child\n"); 5362 exit(4); 5363 } 5364 5365 iters++; 5366 5367 if (zopt_verbose >= 1) { 5368 hrtime_t now = gethrtime(); 5369 5370 now = MIN(now, zs->zs_proc_stop); 5371 print_time(zs->zs_proc_stop - now, timebuf); 5372 nicenum(zs->zs_space, numbuf); 5373 5374 (void) printf("Pass %3d, %8s, %3llu ENOSPC, " 5375 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n", 5376 iters, 5377 WIFEXITED(status) ? "Complete" : "SIGKILL", 5378 (u_longlong_t)zs->zs_enospc_count, 5379 100.0 * zs->zs_alloc / zs->zs_space, 5380 numbuf, 5381 100.0 * (now - zs->zs_proc_start) / 5382 (zopt_time * NANOSEC), timebuf); 5383 } 5384 5385 if (zopt_verbose >= 2) { 5386 (void) printf("\nWorkload summary:\n\n"); 5387 (void) printf("%7s %9s %s\n", 5388 "Calls", "Time", "Function"); 5389 (void) printf("%7s %9s %s\n", 5390 "-----", "----", "--------"); 5391 for (int f = 0; f < ZTEST_FUNCS; f++) { 5392 Dl_info dli; 5393 5394 zi = &zs->zs_info[f]; 5395 print_time(zi->zi_call_time, timebuf); 5396 (void) dladdr((void *)zi->zi_func, &dli); 5397 (void) printf("%7llu %9s %s\n", 5398 (u_longlong_t)zi->zi_call_count, timebuf, 5399 dli.dli_sname); 5400 } 5401 (void) printf("\n"); 5402 } 5403 5404 /* 5405 * It's possible that we killed a child during a rename test, 5406 * in which case we'll have a 'ztest_tmp' pool lying around 5407 * instead of 'ztest'. Do a blind rename in case this happened. 5408 */ 5409 kernel_init(FREAD); 5410 if (spa_open(zopt_pool, &spa, FTAG) == 0) { 5411 spa_close(spa, FTAG); 5412 } else { 5413 char tmpname[MAXNAMELEN]; 5414 kernel_fini(); 5415 kernel_init(FREAD | FWRITE); 5416 (void) snprintf(tmpname, sizeof (tmpname), "%s_tmp", 5417 zopt_pool); 5418 (void) spa_rename(tmpname, zopt_pool); 5419 } 5420 kernel_fini(); 5421 5422 ztest_run_zdb(zopt_pool); 5423 } 5424 5425 if (zopt_verbose >= 1) { 5426 (void) printf("%d killed, %d completed, %.0f%% kill rate\n", 5427 kills, iters - kills, (100.0 * kills) / MAX(1, iters)); 5428 } 5429 5430 return (0); 5431 } 5432