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