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