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