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