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