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