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