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