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