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