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