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