1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5441d80aaSlling * Common Development and Distribution License (the "License"). 6441d80aaSlling * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 21fa9e4066Sahrens /* 22893a6d32Sahrens * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23fa9e4066Sahrens * Use is subject to license terms. 24fa9e4066Sahrens */ 25fa9e4066Sahrens 26fa9e4066Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 27fa9e4066Sahrens 28fa9e4066Sahrens /* 29fa9e4066Sahrens * The objective of this program is to provide a DMU/ZAP/SPA stress test 30fa9e4066Sahrens * that runs entirely in userland, is easy to use, and easy to extend. 31fa9e4066Sahrens * 32fa9e4066Sahrens * The overall design of the ztest program is as follows: 33fa9e4066Sahrens * 34fa9e4066Sahrens * (1) For each major functional area (e.g. adding vdevs to a pool, 35fa9e4066Sahrens * creating and destroying datasets, reading and writing objects, etc) 36fa9e4066Sahrens * we have a simple routine to test that functionality. These 37fa9e4066Sahrens * individual routines do not have to do anything "stressful". 38fa9e4066Sahrens * 39fa9e4066Sahrens * (2) We turn these simple functionality tests into a stress test by 40fa9e4066Sahrens * running them all in parallel, with as many threads as desired, 41fa9e4066Sahrens * and spread across as many datasets, objects, and vdevs as desired. 42fa9e4066Sahrens * 43fa9e4066Sahrens * (3) While all this is happening, we inject faults into the pool to 44fa9e4066Sahrens * verify that self-healing data really works. 45fa9e4066Sahrens * 46fa9e4066Sahrens * (4) Every time we open a dataset, we change its checksum and compression 47fa9e4066Sahrens * functions. Thus even individual objects vary from block to block 48fa9e4066Sahrens * in which checksum they use and whether they're compressed. 49fa9e4066Sahrens * 50fa9e4066Sahrens * (5) To verify that we never lose on-disk consistency after a crash, 51fa9e4066Sahrens * we run the entire test in a child of the main process. 52fa9e4066Sahrens * At random times, the child self-immolates with a SIGKILL. 53fa9e4066Sahrens * This is the software equivalent of pulling the power cord. 54fa9e4066Sahrens * The parent then runs the test again, using the existing 55fa9e4066Sahrens * storage pool, as many times as desired. 56fa9e4066Sahrens * 57fa9e4066Sahrens * (6) To verify that we don't have future leaks or temporal incursions, 58fa9e4066Sahrens * many of the functional tests record the transaction group number 59fa9e4066Sahrens * as part of their data. When reading old data, they verify that 60fa9e4066Sahrens * the transaction group number is less than the current, open txg. 61fa9e4066Sahrens * If you add a new test, please do this if applicable. 62fa9e4066Sahrens * 63fa9e4066Sahrens * When run with no arguments, ztest runs for about five minutes and 64fa9e4066Sahrens * produces no output if successful. To get a little bit of information, 65fa9e4066Sahrens * specify -V. To get more information, specify -VV, and so on. 66fa9e4066Sahrens * 67fa9e4066Sahrens * To turn this into an overnight stress test, use -T to specify run time. 68fa9e4066Sahrens * 69fa9e4066Sahrens * You can ask more more vdevs [-v], datasets [-d], or threads [-t] 70fa9e4066Sahrens * to increase the pool capacity, fanout, and overall stress level. 71fa9e4066Sahrens * 72fa9e4066Sahrens * The -N(okill) option will suppress kills, so each child runs to completion. 73fa9e4066Sahrens * This can be useful when you're trying to distinguish temporal incursions 74fa9e4066Sahrens * from plain old race conditions. 75fa9e4066Sahrens */ 76fa9e4066Sahrens 77fa9e4066Sahrens #include <sys/zfs_context.h> 78fa9e4066Sahrens #include <sys/spa.h> 79fa9e4066Sahrens #include <sys/dmu.h> 80fa9e4066Sahrens #include <sys/txg.h> 81fa9e4066Sahrens #include <sys/zap.h> 82fa9e4066Sahrens #include <sys/dmu_traverse.h> 83fa9e4066Sahrens #include <sys/dmu_objset.h> 84fa9e4066Sahrens #include <sys/poll.h> 85fa9e4066Sahrens #include <sys/stat.h> 86fa9e4066Sahrens #include <sys/time.h> 87fa9e4066Sahrens #include <sys/wait.h> 88fa9e4066Sahrens #include <sys/mman.h> 89fa9e4066Sahrens #include <sys/resource.h> 90fa9e4066Sahrens #include <sys/zio.h> 91fa9e4066Sahrens #include <sys/zio_checksum.h> 92fa9e4066Sahrens #include <sys/zio_compress.h> 93fa9e4066Sahrens #include <sys/zil.h> 94fa9e4066Sahrens #include <sys/vdev_impl.h> 95fa9e4066Sahrens #include <sys/spa_impl.h> 96fa9e4066Sahrens #include <sys/dsl_prop.h> 97fa9e4066Sahrens #include <sys/refcount.h> 98fa9e4066Sahrens #include <stdio.h> 99004388ebScasper #include <stdio_ext.h> 100fa9e4066Sahrens #include <stdlib.h> 101fa9e4066Sahrens #include <unistd.h> 102fa9e4066Sahrens #include <signal.h> 103fa9e4066Sahrens #include <umem.h> 104fa9e4066Sahrens #include <dlfcn.h> 105fa9e4066Sahrens #include <ctype.h> 106fa9e4066Sahrens #include <math.h> 107fa9e4066Sahrens #include <sys/fs/zfs.h> 108fa9e4066Sahrens 109fa9e4066Sahrens static char cmdname[] = "ztest"; 110fa9e4066Sahrens static char *zopt_pool = cmdname; 111fa9e4066Sahrens 112fa9e4066Sahrens static uint64_t zopt_vdevs = 5; 113fa9e4066Sahrens static uint64_t zopt_vdevtime; 114ecc2d604Sbonwick static int zopt_ashift = SPA_MINBLOCKSHIFT; 115fa9e4066Sahrens static int zopt_mirrors = 2; 116fa9e4066Sahrens static int zopt_raidz = 4; 11799653d4eSeschrock static int zopt_raidz_parity = 1; 118fa9e4066Sahrens static size_t zopt_vdev_size = SPA_MINDEVSIZE; 119ecc2d604Sbonwick static int zopt_datasets = 7; 120fa9e4066Sahrens static int zopt_threads = 23; 121fa9e4066Sahrens static uint64_t zopt_passtime = 60; /* 60 seconds */ 122fa9e4066Sahrens static uint64_t zopt_killrate = 70; /* 70% kill rate */ 123fa9e4066Sahrens static int zopt_verbose = 0; 124fa9e4066Sahrens static int zopt_init = 1; 125fa9e4066Sahrens static char *zopt_dir = "/tmp"; 126fa9e4066Sahrens static uint64_t zopt_time = 300; /* 5 minutes */ 127fa9e4066Sahrens static int zopt_maxfaults; 128fa9e4066Sahrens 129fa9e4066Sahrens typedef struct ztest_args { 130fa9e4066Sahrens char *za_pool; 131fa9e4066Sahrens objset_t *za_os; 132fa9e4066Sahrens zilog_t *za_zilog; 133fa9e4066Sahrens thread_t za_thread; 134fa9e4066Sahrens uint64_t za_instance; 135fa9e4066Sahrens uint64_t za_random; 136fa9e4066Sahrens uint64_t za_diroff; 137fa9e4066Sahrens uint64_t za_diroff_shared; 138ea8dc4b6Seschrock uint64_t za_zil_seq; 139fa9e4066Sahrens hrtime_t za_start; 140fa9e4066Sahrens hrtime_t za_stop; 141fa9e4066Sahrens hrtime_t za_kill; 142fa9e4066Sahrens traverse_handle_t *za_th; 143fa9e4066Sahrens } ztest_args_t; 144fa9e4066Sahrens 145fa9e4066Sahrens typedef void ztest_func_t(ztest_args_t *); 146fa9e4066Sahrens 147fa9e4066Sahrens /* 148fa9e4066Sahrens * Note: these aren't static because we want dladdr() to work. 149fa9e4066Sahrens */ 150fa9e4066Sahrens ztest_func_t ztest_dmu_read_write; 151fa9e4066Sahrens ztest_func_t ztest_dmu_write_parallel; 152fa9e4066Sahrens ztest_func_t ztest_dmu_object_alloc_free; 153fa9e4066Sahrens ztest_func_t ztest_zap; 154fa9e4066Sahrens ztest_func_t ztest_zap_parallel; 155fa9e4066Sahrens ztest_func_t ztest_traverse; 156fa9e4066Sahrens ztest_func_t ztest_dsl_prop_get_set; 157fa9e4066Sahrens ztest_func_t ztest_dmu_objset_create_destroy; 158fa9e4066Sahrens ztest_func_t ztest_dmu_snapshot_create_destroy; 159fa9e4066Sahrens ztest_func_t ztest_spa_create_destroy; 160fa9e4066Sahrens ztest_func_t ztest_fault_inject; 161fa9e4066Sahrens ztest_func_t ztest_vdev_attach_detach; 162fa9e4066Sahrens ztest_func_t ztest_vdev_LUN_growth; 163fa9e4066Sahrens ztest_func_t ztest_vdev_add_remove; 164fa9e4066Sahrens ztest_func_t ztest_scrub; 165fa9e4066Sahrens ztest_func_t ztest_spa_rename; 166fa9e4066Sahrens 167fa9e4066Sahrens typedef struct ztest_info { 168fa9e4066Sahrens ztest_func_t *zi_func; /* test function */ 169fa9e4066Sahrens uint64_t *zi_interval; /* execute every <interval> seconds */ 170fa9e4066Sahrens uint64_t zi_calls; /* per-pass count */ 171fa9e4066Sahrens uint64_t zi_call_time; /* per-pass time */ 172fa9e4066Sahrens uint64_t zi_call_total; /* cumulative total */ 173fa9e4066Sahrens uint64_t zi_call_target; /* target cumulative total */ 174fa9e4066Sahrens } ztest_info_t; 175fa9e4066Sahrens 176fa9e4066Sahrens uint64_t zopt_always = 0; /* all the time */ 177fa9e4066Sahrens uint64_t zopt_often = 1; /* every second */ 178fa9e4066Sahrens uint64_t zopt_sometimes = 10; /* every 10 seconds */ 179fa9e4066Sahrens uint64_t zopt_rarely = 60; /* every 60 seconds */ 180fa9e4066Sahrens 181fa9e4066Sahrens ztest_info_t ztest_info[] = { 182fa9e4066Sahrens { ztest_dmu_read_write, &zopt_always }, 183fa9e4066Sahrens { ztest_dmu_write_parallel, &zopt_always }, 184fa9e4066Sahrens { ztest_dmu_object_alloc_free, &zopt_always }, 185fa9e4066Sahrens { ztest_zap, &zopt_always }, 186fa9e4066Sahrens { ztest_zap_parallel, &zopt_always }, 187fa9e4066Sahrens { ztest_traverse, &zopt_often }, 188fa9e4066Sahrens { ztest_dsl_prop_get_set, &zopt_sometimes }, 189fa9e4066Sahrens { ztest_dmu_objset_create_destroy, &zopt_sometimes }, 190ea8dc4b6Seschrock { ztest_dmu_snapshot_create_destroy, &zopt_rarely }, 191fa9e4066Sahrens { ztest_spa_create_destroy, &zopt_sometimes }, 192fa9e4066Sahrens { ztest_fault_inject, &zopt_sometimes }, 193fa9e4066Sahrens { ztest_spa_rename, &zopt_rarely }, 194fa9e4066Sahrens { ztest_vdev_attach_detach, &zopt_rarely }, 195fa9e4066Sahrens { ztest_vdev_LUN_growth, &zopt_rarely }, 196fa9e4066Sahrens { ztest_vdev_add_remove, &zopt_vdevtime }, 197fa9e4066Sahrens { ztest_scrub, &zopt_vdevtime }, 198fa9e4066Sahrens }; 199fa9e4066Sahrens 200fa9e4066Sahrens #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t)) 201fa9e4066Sahrens 202fa9e4066Sahrens #define ZTEST_SYNC_LOCKS 16 203fa9e4066Sahrens 204fa9e4066Sahrens /* 205fa9e4066Sahrens * Stuff we need to share writably between parent and child. 206fa9e4066Sahrens */ 207fa9e4066Sahrens typedef struct ztest_shared { 208fa9e4066Sahrens mutex_t zs_vdev_lock; 209fa9e4066Sahrens rwlock_t zs_name_lock; 210fa9e4066Sahrens uint64_t zs_vdev_primaries; 211fa9e4066Sahrens uint64_t zs_enospc_count; 212fa9e4066Sahrens hrtime_t zs_start_time; 213fa9e4066Sahrens hrtime_t zs_stop_time; 214fa9e4066Sahrens uint64_t zs_alloc; 215fa9e4066Sahrens uint64_t zs_space; 2164d31c452Smaybee uint64_t zs_txg; 217fa9e4066Sahrens ztest_info_t zs_info[ZTEST_FUNCS]; 218fa9e4066Sahrens mutex_t zs_sync_lock[ZTEST_SYNC_LOCKS]; 219fa9e4066Sahrens uint64_t zs_seq[ZTEST_SYNC_LOCKS]; 220fa9e4066Sahrens } ztest_shared_t; 221fa9e4066Sahrens 222fa9e4066Sahrens typedef struct ztest_block_tag { 223fa9e4066Sahrens uint64_t bt_objset; 224fa9e4066Sahrens uint64_t bt_object; 225fa9e4066Sahrens uint64_t bt_offset; 226fa9e4066Sahrens uint64_t bt_txg; 227fa9e4066Sahrens uint64_t bt_thread; 228fa9e4066Sahrens uint64_t bt_seq; 229fa9e4066Sahrens } ztest_block_tag_t; 230fa9e4066Sahrens 231fa9e4066Sahrens static char ztest_dev_template[] = "%s/%s.%llua"; 232fa9e4066Sahrens static ztest_shared_t *ztest_shared; 233fa9e4066Sahrens 234fa9e4066Sahrens static int ztest_random_fd; 235fa9e4066Sahrens static int ztest_dump_core = 1; 236fa9e4066Sahrens 237fa9e4066Sahrens extern uint64_t zio_gang_bang; 238d63d470bSgw25295 extern uint16_t zio_zil_fail_shift; 239fa9e4066Sahrens 240fa9e4066Sahrens #define ZTEST_DIROBJ 1 241fa9e4066Sahrens #define ZTEST_MICROZAP_OBJ 2 242fa9e4066Sahrens #define ZTEST_FATZAP_OBJ 3 243fa9e4066Sahrens 244fa9e4066Sahrens #define ZTEST_DIROBJ_BLOCKSIZE (1 << 10) 245fa9e4066Sahrens #define ZTEST_DIRSIZE 256 246fa9e4066Sahrens 2471ce825d8Sraf static void usage(boolean_t) __NORETURN; 248f1b4288bSvb160487 249fa9e4066Sahrens /* 250fa9e4066Sahrens * These libumem hooks provide a reasonable set of defaults for the allocator's 251fa9e4066Sahrens * debugging facilities. 252fa9e4066Sahrens */ 253fa9e4066Sahrens const char * 254fa9e4066Sahrens _umem_debug_init() 255fa9e4066Sahrens { 256fa9e4066Sahrens return ("default,verbose"); /* $UMEM_DEBUG setting */ 257fa9e4066Sahrens } 258fa9e4066Sahrens 259fa9e4066Sahrens const char * 260fa9e4066Sahrens _umem_logging_init(void) 261fa9e4066Sahrens { 262fa9e4066Sahrens return ("fail,contents"); /* $UMEM_LOGGING setting */ 263fa9e4066Sahrens } 264fa9e4066Sahrens 265fa9e4066Sahrens #define FATAL_MSG_SZ 1024 266fa9e4066Sahrens 267fa9e4066Sahrens char *fatal_msg; 268fa9e4066Sahrens 269fa9e4066Sahrens static void 270fa9e4066Sahrens fatal(int do_perror, char *message, ...) 271fa9e4066Sahrens { 272fa9e4066Sahrens va_list args; 273fa9e4066Sahrens int save_errno = errno; 274fa9e4066Sahrens char buf[FATAL_MSG_SZ]; 275fa9e4066Sahrens 276fa9e4066Sahrens (void) fflush(stdout); 277fa9e4066Sahrens 278fa9e4066Sahrens va_start(args, message); 279fa9e4066Sahrens (void) sprintf(buf, "ztest: "); 280fa9e4066Sahrens /* LINTED */ 281fa9e4066Sahrens (void) vsprintf(buf + strlen(buf), message, args); 282fa9e4066Sahrens va_end(args); 283fa9e4066Sahrens if (do_perror) { 284fa9e4066Sahrens (void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf), 285fa9e4066Sahrens ": %s", strerror(save_errno)); 286fa9e4066Sahrens } 287fa9e4066Sahrens (void) fprintf(stderr, "%s\n", buf); 288fa9e4066Sahrens fatal_msg = buf; /* to ease debugging */ 289fa9e4066Sahrens if (ztest_dump_core) 290fa9e4066Sahrens abort(); 291fa9e4066Sahrens exit(3); 292fa9e4066Sahrens } 293fa9e4066Sahrens 294fa9e4066Sahrens static int 295fa9e4066Sahrens str2shift(const char *buf) 296fa9e4066Sahrens { 297fa9e4066Sahrens const char *ends = "BKMGTPEZ"; 298fa9e4066Sahrens int i; 299fa9e4066Sahrens 300fa9e4066Sahrens if (buf[0] == '\0') 301fa9e4066Sahrens return (0); 302fa9e4066Sahrens for (i = 0; i < strlen(ends); i++) { 303fa9e4066Sahrens if (toupper(buf[0]) == ends[i]) 304fa9e4066Sahrens break; 305fa9e4066Sahrens } 306f1b4288bSvb160487 if (i == strlen(ends)) { 307f1b4288bSvb160487 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", 308f1b4288bSvb160487 buf); 309f1b4288bSvb160487 usage(B_FALSE); 310f1b4288bSvb160487 } 311fa9e4066Sahrens if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) { 312fa9e4066Sahrens return (10*i); 313fa9e4066Sahrens } 314f1b4288bSvb160487 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf); 315f1b4288bSvb160487 usage(B_FALSE); 316f1b4288bSvb160487 /* NOTREACHED */ 317fa9e4066Sahrens } 318fa9e4066Sahrens 319fa9e4066Sahrens static uint64_t 320fa9e4066Sahrens nicenumtoull(const char *buf) 321fa9e4066Sahrens { 322fa9e4066Sahrens char *end; 323fa9e4066Sahrens uint64_t val; 324fa9e4066Sahrens 325fa9e4066Sahrens val = strtoull(buf, &end, 0); 326fa9e4066Sahrens if (end == buf) { 327f1b4288bSvb160487 (void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf); 328f1b4288bSvb160487 usage(B_FALSE); 329fa9e4066Sahrens } else if (end[0] == '.') { 330fa9e4066Sahrens double fval = strtod(buf, &end); 331fa9e4066Sahrens fval *= pow(2, str2shift(end)); 332f1b4288bSvb160487 if (fval > UINT64_MAX) { 333f1b4288bSvb160487 (void) fprintf(stderr, "ztest: value too large: %s\n", 334f1b4288bSvb160487 buf); 335f1b4288bSvb160487 usage(B_FALSE); 336f1b4288bSvb160487 } 337fa9e4066Sahrens val = (uint64_t)fval; 338fa9e4066Sahrens } else { 339fa9e4066Sahrens int shift = str2shift(end); 340f1b4288bSvb160487 if (shift >= 64 || (val << shift) >> shift != val) { 341f1b4288bSvb160487 (void) fprintf(stderr, "ztest: value too large: %s\n", 342f1b4288bSvb160487 buf); 343f1b4288bSvb160487 usage(B_FALSE); 344f1b4288bSvb160487 } 345fa9e4066Sahrens val <<= shift; 346fa9e4066Sahrens } 347fa9e4066Sahrens return (val); 348fa9e4066Sahrens } 349fa9e4066Sahrens 350fa9e4066Sahrens static void 351f1b4288bSvb160487 usage(boolean_t requested) 352fa9e4066Sahrens { 353fa9e4066Sahrens char nice_vdev_size[10]; 354fa9e4066Sahrens char nice_gang_bang[10]; 355f1b4288bSvb160487 FILE *fp = requested ? stdout : stderr; 356fa9e4066Sahrens 357fa9e4066Sahrens nicenum(zopt_vdev_size, nice_vdev_size); 358fa9e4066Sahrens nicenum(zio_gang_bang, nice_gang_bang); 359fa9e4066Sahrens 360f1b4288bSvb160487 (void) fprintf(fp, "Usage: %s\n" 361fa9e4066Sahrens "\t[-v vdevs (default: %llu)]\n" 362fa9e4066Sahrens "\t[-s size_of_each_vdev (default: %s)]\n" 363ecc2d604Sbonwick "\t[-a alignment_shift (default: %d) (use 0 for random)]\n" 364fa9e4066Sahrens "\t[-m mirror_copies (default: %d)]\n" 365fa9e4066Sahrens "\t[-r raidz_disks (default: %d)]\n" 36699653d4eSeschrock "\t[-R raidz_parity (default: %d)]\n" 367fa9e4066Sahrens "\t[-d datasets (default: %d)]\n" 368fa9e4066Sahrens "\t[-t threads (default: %d)]\n" 369fa9e4066Sahrens "\t[-g gang_block_threshold (default: %s)]\n" 370fa9e4066Sahrens "\t[-i initialize pool i times (default: %d)]\n" 371fa9e4066Sahrens "\t[-k kill percentage (default: %llu%%)]\n" 372fa9e4066Sahrens "\t[-p pool_name (default: %s)]\n" 373fa9e4066Sahrens "\t[-f file directory for vdev files (default: %s)]\n" 374fa9e4066Sahrens "\t[-V(erbose)] (use multiple times for ever more blather)\n" 375ecc2d604Sbonwick "\t[-E(xisting)] (use existing pool instead of creating new one)\n" 376fa9e4066Sahrens "\t[-T time] total run time (default: %llu sec)\n" 377fa9e4066Sahrens "\t[-P passtime] time per pass (default: %llu sec)\n" 378d63d470bSgw25295 "\t[-z zil failure rate (default: fail every 2^%llu allocs)]\n" 379f1b4288bSvb160487 "\t[-h] (print help)\n" 380fa9e4066Sahrens "", 381fa9e4066Sahrens cmdname, 382fa9e4066Sahrens (u_longlong_t)zopt_vdevs, /* -v */ 383fa9e4066Sahrens nice_vdev_size, /* -s */ 384ecc2d604Sbonwick zopt_ashift, /* -a */ 385fa9e4066Sahrens zopt_mirrors, /* -m */ 386fa9e4066Sahrens zopt_raidz, /* -r */ 38799653d4eSeschrock zopt_raidz_parity, /* -R */ 388ecc2d604Sbonwick zopt_datasets, /* -d */ 389fa9e4066Sahrens zopt_threads, /* -t */ 390fa9e4066Sahrens nice_gang_bang, /* -g */ 391fa9e4066Sahrens zopt_init, /* -i */ 392fa9e4066Sahrens (u_longlong_t)zopt_killrate, /* -k */ 393fa9e4066Sahrens zopt_pool, /* -p */ 394fa9e4066Sahrens zopt_dir, /* -f */ 395fa9e4066Sahrens (u_longlong_t)zopt_time, /* -T */ 396d63d470bSgw25295 (u_longlong_t)zopt_passtime, /* -P */ 397d63d470bSgw25295 (u_longlong_t)zio_zil_fail_shift); /* -z */ 398f1b4288bSvb160487 exit(requested ? 0 : 1); 399fa9e4066Sahrens } 400fa9e4066Sahrens 401fa9e4066Sahrens static uint64_t 402fa9e4066Sahrens ztest_random(uint64_t range) 403fa9e4066Sahrens { 404fa9e4066Sahrens uint64_t r; 405fa9e4066Sahrens 406fa9e4066Sahrens if (range == 0) 407fa9e4066Sahrens return (0); 408fa9e4066Sahrens 409fa9e4066Sahrens if (read(ztest_random_fd, &r, sizeof (r)) != sizeof (r)) 410fa9e4066Sahrens fatal(1, "short read from /dev/urandom"); 411fa9e4066Sahrens 412fa9e4066Sahrens return (r % range); 413fa9e4066Sahrens } 414fa9e4066Sahrens 415fa9e4066Sahrens static void 416fa9e4066Sahrens ztest_record_enospc(char *s) 417fa9e4066Sahrens { 418fa9e4066Sahrens dprintf("ENOSPC doing: %s\n", s ? s : "<unknown>"); 419fa9e4066Sahrens ztest_shared->zs_enospc_count++; 420fa9e4066Sahrens } 421fa9e4066Sahrens 422fa9e4066Sahrens static void 423fa9e4066Sahrens process_options(int argc, char **argv) 424fa9e4066Sahrens { 425fa9e4066Sahrens int opt; 426fa9e4066Sahrens uint64_t value; 427fa9e4066Sahrens 428fa9e4066Sahrens /* By default, test gang blocks for blocks 32K and greater */ 429fa9e4066Sahrens zio_gang_bang = 32 << 10; 430fa9e4066Sahrens 431d63d470bSgw25295 /* Default value, fail every 32nd allocation */ 432d63d470bSgw25295 zio_zil_fail_shift = 5; 433d63d470bSgw25295 434fa9e4066Sahrens while ((opt = getopt(argc, argv, 435f1b4288bSvb160487 "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:z:h")) != EOF) { 436fa9e4066Sahrens value = 0; 437fa9e4066Sahrens switch (opt) { 438fa9e4066Sahrens case 'v': 439fa9e4066Sahrens case 's': 440ecc2d604Sbonwick case 'a': 441fa9e4066Sahrens case 'm': 442fa9e4066Sahrens case 'r': 44399653d4eSeschrock case 'R': 444fa9e4066Sahrens case 'd': 445fa9e4066Sahrens case 't': 446fa9e4066Sahrens case 'g': 447fa9e4066Sahrens case 'i': 448fa9e4066Sahrens case 'k': 449fa9e4066Sahrens case 'T': 450fa9e4066Sahrens case 'P': 451d63d470bSgw25295 case 'z': 452fa9e4066Sahrens value = nicenumtoull(optarg); 453fa9e4066Sahrens } 454fa9e4066Sahrens switch (opt) { 455fa9e4066Sahrens case 'v': 456fa9e4066Sahrens zopt_vdevs = value; 457fa9e4066Sahrens break; 458fa9e4066Sahrens case 's': 459fa9e4066Sahrens zopt_vdev_size = MAX(SPA_MINDEVSIZE, value); 460fa9e4066Sahrens break; 461ecc2d604Sbonwick case 'a': 462ecc2d604Sbonwick zopt_ashift = value; 463ecc2d604Sbonwick break; 464fa9e4066Sahrens case 'm': 465fa9e4066Sahrens zopt_mirrors = value; 466fa9e4066Sahrens break; 467fa9e4066Sahrens case 'r': 468fa9e4066Sahrens zopt_raidz = MAX(1, value); 469fa9e4066Sahrens break; 47099653d4eSeschrock case 'R': 47199653d4eSeschrock zopt_raidz_parity = MIN(MAX(value, 1), 2); 47299653d4eSeschrock break; 473fa9e4066Sahrens case 'd': 474ecc2d604Sbonwick zopt_datasets = MAX(1, value); 475fa9e4066Sahrens break; 476fa9e4066Sahrens case 't': 477fa9e4066Sahrens zopt_threads = MAX(1, value); 478fa9e4066Sahrens break; 479fa9e4066Sahrens case 'g': 480fa9e4066Sahrens zio_gang_bang = MAX(SPA_MINBLOCKSIZE << 1, value); 481fa9e4066Sahrens break; 482fa9e4066Sahrens case 'i': 483fa9e4066Sahrens zopt_init = value; 484fa9e4066Sahrens break; 485fa9e4066Sahrens case 'k': 486fa9e4066Sahrens zopt_killrate = value; 487fa9e4066Sahrens break; 488fa9e4066Sahrens case 'p': 489fa9e4066Sahrens zopt_pool = strdup(optarg); 490fa9e4066Sahrens break; 491fa9e4066Sahrens case 'f': 492fa9e4066Sahrens zopt_dir = strdup(optarg); 493fa9e4066Sahrens break; 494fa9e4066Sahrens case 'V': 495fa9e4066Sahrens zopt_verbose++; 496fa9e4066Sahrens break; 497fa9e4066Sahrens case 'E': 498fa9e4066Sahrens zopt_init = 0; 499fa9e4066Sahrens break; 500fa9e4066Sahrens case 'T': 501fa9e4066Sahrens zopt_time = value; 502fa9e4066Sahrens break; 503fa9e4066Sahrens case 'P': 504fa9e4066Sahrens zopt_passtime = MAX(1, value); 505fa9e4066Sahrens break; 506d63d470bSgw25295 case 'z': 507d63d470bSgw25295 zio_zil_fail_shift = MIN(value, 16); 508d63d470bSgw25295 break; 509f1b4288bSvb160487 case 'h': 510f1b4288bSvb160487 usage(B_TRUE); 511f1b4288bSvb160487 break; 512fa9e4066Sahrens case '?': 513fa9e4066Sahrens default: 514f1b4288bSvb160487 usage(B_FALSE); 515fa9e4066Sahrens break; 516fa9e4066Sahrens } 517fa9e4066Sahrens } 518fa9e4066Sahrens 51999653d4eSeschrock zopt_raidz_parity = MIN(zopt_raidz_parity, zopt_raidz - 1); 52099653d4eSeschrock 521fa9e4066Sahrens zopt_vdevtime = (zopt_vdevs > 0 ? zopt_time / zopt_vdevs : UINT64_MAX); 52299653d4eSeschrock zopt_maxfaults = MAX(zopt_mirrors, 1) * (zopt_raidz_parity + 1) - 1; 523fa9e4066Sahrens } 524fa9e4066Sahrens 525ecc2d604Sbonwick static uint64_t 526ecc2d604Sbonwick ztest_get_ashift(void) 527ecc2d604Sbonwick { 528ecc2d604Sbonwick if (zopt_ashift == 0) 529ecc2d604Sbonwick return (SPA_MINBLOCKSHIFT + ztest_random(3)); 530ecc2d604Sbonwick return (zopt_ashift); 531ecc2d604Sbonwick } 532ecc2d604Sbonwick 533fa9e4066Sahrens static nvlist_t * 534fa9e4066Sahrens make_vdev_file(size_t size) 535fa9e4066Sahrens { 536fa9e4066Sahrens char dev_name[MAXPATHLEN]; 537fa9e4066Sahrens uint64_t vdev; 538ecc2d604Sbonwick uint64_t ashift = ztest_get_ashift(); 539fa9e4066Sahrens int fd; 540fa9e4066Sahrens nvlist_t *file; 541fa9e4066Sahrens 542fa9e4066Sahrens if (size == 0) { 543fa9e4066Sahrens (void) snprintf(dev_name, sizeof (dev_name), "%s", 544fa9e4066Sahrens "/dev/bogus"); 545fa9e4066Sahrens } else { 546fa9e4066Sahrens vdev = ztest_shared->zs_vdev_primaries++; 547fa9e4066Sahrens (void) sprintf(dev_name, ztest_dev_template, 548fa9e4066Sahrens zopt_dir, zopt_pool, vdev); 549fa9e4066Sahrens 550fa9e4066Sahrens fd = open(dev_name, O_RDWR | O_CREAT | O_TRUNC, 0666); 551fa9e4066Sahrens if (fd == -1) 552fa9e4066Sahrens fatal(1, "can't open %s", dev_name); 553fa9e4066Sahrens if (ftruncate(fd, size) != 0) 554fa9e4066Sahrens fatal(1, "can't ftruncate %s", dev_name); 555fa9e4066Sahrens (void) close(fd); 556fa9e4066Sahrens } 557fa9e4066Sahrens 558fa9e4066Sahrens VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0); 559fa9e4066Sahrens VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0); 560fa9e4066Sahrens VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, dev_name) == 0); 561ecc2d604Sbonwick VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0); 562fa9e4066Sahrens 563fa9e4066Sahrens return (file); 564fa9e4066Sahrens } 565fa9e4066Sahrens 566fa9e4066Sahrens static nvlist_t * 567fa9e4066Sahrens make_vdev_raidz(size_t size, int r) 568fa9e4066Sahrens { 569fa9e4066Sahrens nvlist_t *raidz, **child; 570fa9e4066Sahrens int c; 571fa9e4066Sahrens 572fa9e4066Sahrens if (r < 2) 573fa9e4066Sahrens return (make_vdev_file(size)); 574fa9e4066Sahrens 575fa9e4066Sahrens child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL); 576fa9e4066Sahrens 577fa9e4066Sahrens for (c = 0; c < r; c++) 578fa9e4066Sahrens child[c] = make_vdev_file(size); 579fa9e4066Sahrens 580fa9e4066Sahrens VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0); 581fa9e4066Sahrens VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE, 582fa9e4066Sahrens VDEV_TYPE_RAIDZ) == 0); 58399653d4eSeschrock VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY, 58499653d4eSeschrock zopt_raidz_parity) == 0); 585fa9e4066Sahrens VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN, 586fa9e4066Sahrens child, r) == 0); 587fa9e4066Sahrens 588fa9e4066Sahrens for (c = 0; c < r; c++) 589fa9e4066Sahrens nvlist_free(child[c]); 590fa9e4066Sahrens 591fa9e4066Sahrens umem_free(child, r * sizeof (nvlist_t *)); 592fa9e4066Sahrens 593fa9e4066Sahrens return (raidz); 594fa9e4066Sahrens } 595fa9e4066Sahrens 596fa9e4066Sahrens static nvlist_t * 5978654d025Sperrin make_vdev_mirror(size_t size, int log, int r, int m) 598fa9e4066Sahrens { 599fa9e4066Sahrens nvlist_t *mirror, **child; 600fa9e4066Sahrens int c; 601fa9e4066Sahrens 602fa9e4066Sahrens if (m < 1) 603fa9e4066Sahrens return (make_vdev_raidz(size, r)); 604fa9e4066Sahrens 605fa9e4066Sahrens child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL); 606fa9e4066Sahrens 607fa9e4066Sahrens for (c = 0; c < m; c++) 608fa9e4066Sahrens child[c] = make_vdev_raidz(size, r); 609fa9e4066Sahrens 610fa9e4066Sahrens VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0); 611fa9e4066Sahrens VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE, 612fa9e4066Sahrens VDEV_TYPE_MIRROR) == 0); 613fa9e4066Sahrens VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN, 614fa9e4066Sahrens child, m) == 0); 6158654d025Sperrin VERIFY(nvlist_add_uint64(mirror, ZPOOL_CONFIG_IS_LOG, log) == 0); 616fa9e4066Sahrens 617fa9e4066Sahrens for (c = 0; c < m; c++) 618fa9e4066Sahrens nvlist_free(child[c]); 619fa9e4066Sahrens 620fa9e4066Sahrens umem_free(child, m * sizeof (nvlist_t *)); 621fa9e4066Sahrens 622fa9e4066Sahrens return (mirror); 623fa9e4066Sahrens } 624fa9e4066Sahrens 625fa9e4066Sahrens static nvlist_t * 6268654d025Sperrin make_vdev_root(size_t size, int log, int r, int m, int t) 627fa9e4066Sahrens { 628fa9e4066Sahrens nvlist_t *root, **child; 629fa9e4066Sahrens int c; 630fa9e4066Sahrens 631fa9e4066Sahrens ASSERT(t > 0); 632fa9e4066Sahrens 633fa9e4066Sahrens child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL); 634fa9e4066Sahrens 635fa9e4066Sahrens for (c = 0; c < t; c++) 6368654d025Sperrin child[c] = make_vdev_mirror(size, log, r, m); 637fa9e4066Sahrens 638fa9e4066Sahrens VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0); 639fa9e4066Sahrens VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0); 640fa9e4066Sahrens VERIFY(nvlist_add_nvlist_array(root, ZPOOL_CONFIG_CHILDREN, 641fa9e4066Sahrens child, t) == 0); 642fa9e4066Sahrens 643fa9e4066Sahrens for (c = 0; c < t; c++) 644fa9e4066Sahrens nvlist_free(child[c]); 645fa9e4066Sahrens 646fa9e4066Sahrens umem_free(child, t * sizeof (nvlist_t *)); 647fa9e4066Sahrens 648fa9e4066Sahrens return (root); 649fa9e4066Sahrens } 650fa9e4066Sahrens 651fa9e4066Sahrens static void 652fa9e4066Sahrens ztest_set_random_blocksize(objset_t *os, uint64_t object, dmu_tx_t *tx) 653fa9e4066Sahrens { 654fa9e4066Sahrens int bs = SPA_MINBLOCKSHIFT + 655fa9e4066Sahrens ztest_random(SPA_MAXBLOCKSHIFT - SPA_MINBLOCKSHIFT + 1); 656fa9e4066Sahrens int ibs = DN_MIN_INDBLKSHIFT + 657fa9e4066Sahrens ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1); 658fa9e4066Sahrens int error; 659fa9e4066Sahrens 660fa9e4066Sahrens error = dmu_object_set_blocksize(os, object, 1ULL << bs, ibs, tx); 661fa9e4066Sahrens if (error) { 662fa9e4066Sahrens char osname[300]; 663fa9e4066Sahrens dmu_objset_name(os, osname); 664fa9e4066Sahrens fatal(0, "dmu_object_set_blocksize('%s', %llu, %d, %d) = %d", 665fa9e4066Sahrens osname, object, 1 << bs, ibs, error); 666fa9e4066Sahrens } 667fa9e4066Sahrens } 668fa9e4066Sahrens 669fa9e4066Sahrens static uint8_t 670fa9e4066Sahrens ztest_random_checksum(void) 671fa9e4066Sahrens { 672fa9e4066Sahrens uint8_t checksum; 673fa9e4066Sahrens 674fa9e4066Sahrens do { 675fa9e4066Sahrens checksum = ztest_random(ZIO_CHECKSUM_FUNCTIONS); 676fa9e4066Sahrens } while (zio_checksum_table[checksum].ci_zbt); 677fa9e4066Sahrens 678fa9e4066Sahrens if (checksum == ZIO_CHECKSUM_OFF) 679fa9e4066Sahrens checksum = ZIO_CHECKSUM_ON; 680fa9e4066Sahrens 681fa9e4066Sahrens return (checksum); 682fa9e4066Sahrens } 683fa9e4066Sahrens 684fa9e4066Sahrens static uint8_t 685fa9e4066Sahrens ztest_random_compress(void) 686fa9e4066Sahrens { 687fa9e4066Sahrens return ((uint8_t)ztest_random(ZIO_COMPRESS_FUNCTIONS)); 688fa9e4066Sahrens } 689fa9e4066Sahrens 690fa9e4066Sahrens typedef struct ztest_replay { 691fa9e4066Sahrens objset_t *zr_os; 692fa9e4066Sahrens uint64_t zr_assign; 693fa9e4066Sahrens } ztest_replay_t; 694fa9e4066Sahrens 695fa9e4066Sahrens static int 696fa9e4066Sahrens ztest_replay_create(ztest_replay_t *zr, lr_create_t *lr, boolean_t byteswap) 697fa9e4066Sahrens { 698fa9e4066Sahrens objset_t *os = zr->zr_os; 699fa9e4066Sahrens dmu_tx_t *tx; 700fa9e4066Sahrens int error; 701fa9e4066Sahrens 702fa9e4066Sahrens if (byteswap) 703fa9e4066Sahrens byteswap_uint64_array(lr, sizeof (*lr)); 704fa9e4066Sahrens 705fa9e4066Sahrens tx = dmu_tx_create(os); 706fa9e4066Sahrens dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 707fa9e4066Sahrens error = dmu_tx_assign(tx, zr->zr_assign); 708fa9e4066Sahrens if (error) { 709fa9e4066Sahrens dmu_tx_abort(tx); 710fa9e4066Sahrens return (error); 711fa9e4066Sahrens } 712fa9e4066Sahrens 713fa9e4066Sahrens error = dmu_object_claim(os, lr->lr_doid, lr->lr_mode, 0, 714fa9e4066Sahrens DMU_OT_NONE, 0, tx); 71599653d4eSeschrock ASSERT3U(error, ==, 0); 716fa9e4066Sahrens dmu_tx_commit(tx); 717fa9e4066Sahrens 718fa9e4066Sahrens if (zopt_verbose >= 5) { 719fa9e4066Sahrens char osname[MAXNAMELEN]; 720fa9e4066Sahrens dmu_objset_name(os, osname); 721fa9e4066Sahrens (void) printf("replay create of %s object %llu" 722fa9e4066Sahrens " in txg %llu = %d\n", 723fa9e4066Sahrens osname, (u_longlong_t)lr->lr_doid, 724fa9e4066Sahrens (u_longlong_t)zr->zr_assign, error); 725fa9e4066Sahrens } 726fa9e4066Sahrens 727fa9e4066Sahrens return (error); 728fa9e4066Sahrens } 729fa9e4066Sahrens 730fa9e4066Sahrens static int 731fa9e4066Sahrens ztest_replay_remove(ztest_replay_t *zr, lr_remove_t *lr, boolean_t byteswap) 732fa9e4066Sahrens { 733fa9e4066Sahrens objset_t *os = zr->zr_os; 734fa9e4066Sahrens dmu_tx_t *tx; 735fa9e4066Sahrens int error; 736fa9e4066Sahrens 737fa9e4066Sahrens if (byteswap) 738fa9e4066Sahrens byteswap_uint64_array(lr, sizeof (*lr)); 739fa9e4066Sahrens 740fa9e4066Sahrens tx = dmu_tx_create(os); 741fa9e4066Sahrens dmu_tx_hold_free(tx, lr->lr_doid, 0, DMU_OBJECT_END); 742fa9e4066Sahrens error = dmu_tx_assign(tx, zr->zr_assign); 743fa9e4066Sahrens if (error) { 744fa9e4066Sahrens dmu_tx_abort(tx); 745fa9e4066Sahrens return (error); 746fa9e4066Sahrens } 747fa9e4066Sahrens 748fa9e4066Sahrens error = dmu_object_free(os, lr->lr_doid, tx); 749fa9e4066Sahrens dmu_tx_commit(tx); 750fa9e4066Sahrens 751fa9e4066Sahrens return (error); 752fa9e4066Sahrens } 753fa9e4066Sahrens 754fa9e4066Sahrens zil_replay_func_t *ztest_replay_vector[TX_MAX_TYPE] = { 755fa9e4066Sahrens NULL, /* 0 no such transaction type */ 756fa9e4066Sahrens ztest_replay_create, /* TX_CREATE */ 757fa9e4066Sahrens NULL, /* TX_MKDIR */ 758fa9e4066Sahrens NULL, /* TX_MKXATTR */ 759fa9e4066Sahrens NULL, /* TX_SYMLINK */ 760fa9e4066Sahrens ztest_replay_remove, /* TX_REMOVE */ 761fa9e4066Sahrens NULL, /* TX_RMDIR */ 762fa9e4066Sahrens NULL, /* TX_LINK */ 763fa9e4066Sahrens NULL, /* TX_RENAME */ 764fa9e4066Sahrens NULL, /* TX_WRITE */ 765fa9e4066Sahrens NULL, /* TX_TRUNCATE */ 766fa9e4066Sahrens NULL, /* TX_SETATTR */ 767fa9e4066Sahrens NULL, /* TX_ACL */ 768fa9e4066Sahrens }; 769fa9e4066Sahrens 770fa9e4066Sahrens /* 771fa9e4066Sahrens * Verify that we can't destroy an active pool, create an existing pool, 772fa9e4066Sahrens * or create a pool with a bad vdev spec. 773fa9e4066Sahrens */ 774fa9e4066Sahrens void 775fa9e4066Sahrens ztest_spa_create_destroy(ztest_args_t *za) 776fa9e4066Sahrens { 777fa9e4066Sahrens int error; 778fa9e4066Sahrens spa_t *spa; 779fa9e4066Sahrens nvlist_t *nvroot; 780fa9e4066Sahrens 781fa9e4066Sahrens /* 782fa9e4066Sahrens * Attempt to create using a bad file. 783fa9e4066Sahrens */ 7848654d025Sperrin nvroot = make_vdev_root(0, 0, 0, 0, 1); 785228975ccSek110237 error = spa_create("ztest_bad_file", nvroot, NULL, NULL); 786fa9e4066Sahrens nvlist_free(nvroot); 787fa9e4066Sahrens if (error != ENOENT) 788fa9e4066Sahrens fatal(0, "spa_create(bad_file) = %d", error); 789fa9e4066Sahrens 790fa9e4066Sahrens /* 791fa9e4066Sahrens * Attempt to create using a bad mirror. 792fa9e4066Sahrens */ 7938654d025Sperrin nvroot = make_vdev_root(0, 0, 0, 2, 1); 794228975ccSek110237 error = spa_create("ztest_bad_mirror", nvroot, NULL, NULL); 795fa9e4066Sahrens nvlist_free(nvroot); 796fa9e4066Sahrens if (error != ENOENT) 797fa9e4066Sahrens fatal(0, "spa_create(bad_mirror) = %d", error); 798fa9e4066Sahrens 799fa9e4066Sahrens /* 800fa9e4066Sahrens * Attempt to create an existing pool. It shouldn't matter 801fa9e4066Sahrens * what's in the nvroot; we should fail with EEXIST. 802fa9e4066Sahrens */ 803fa9e4066Sahrens (void) rw_rdlock(&ztest_shared->zs_name_lock); 8048654d025Sperrin nvroot = make_vdev_root(0, 0, 0, 0, 1); 805228975ccSek110237 error = spa_create(za->za_pool, nvroot, NULL, NULL); 806fa9e4066Sahrens nvlist_free(nvroot); 807fa9e4066Sahrens if (error != EEXIST) 808fa9e4066Sahrens fatal(0, "spa_create(whatever) = %d", error); 809fa9e4066Sahrens 810fa9e4066Sahrens error = spa_open(za->za_pool, &spa, FTAG); 811fa9e4066Sahrens if (error) 812fa9e4066Sahrens fatal(0, "spa_open() = %d", error); 813fa9e4066Sahrens 814fa9e4066Sahrens error = spa_destroy(za->za_pool); 815fa9e4066Sahrens if (error != EBUSY) 816fa9e4066Sahrens fatal(0, "spa_destroy() = %d", error); 817fa9e4066Sahrens 818fa9e4066Sahrens spa_close(spa, FTAG); 819fa9e4066Sahrens (void) rw_unlock(&ztest_shared->zs_name_lock); 820fa9e4066Sahrens } 821fa9e4066Sahrens 822fa9e4066Sahrens /* 823fa9e4066Sahrens * Verify that vdev_add() works as expected. 824fa9e4066Sahrens */ 825fa9e4066Sahrens void 826fa9e4066Sahrens ztest_vdev_add_remove(ztest_args_t *za) 827fa9e4066Sahrens { 828fa9e4066Sahrens spa_t *spa = dmu_objset_spa(za->za_os); 829fa9e4066Sahrens uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz; 830fa9e4066Sahrens nvlist_t *nvroot; 831fa9e4066Sahrens int error; 832fa9e4066Sahrens 833fa9e4066Sahrens if (zopt_verbose >= 6) 834fa9e4066Sahrens (void) printf("adding vdev\n"); 835fa9e4066Sahrens 836fa9e4066Sahrens (void) mutex_lock(&ztest_shared->zs_vdev_lock); 837fa9e4066Sahrens 838ea8dc4b6Seschrock spa_config_enter(spa, RW_READER, FTAG); 839fa9e4066Sahrens 840fa9e4066Sahrens ztest_shared->zs_vdev_primaries = 841fa9e4066Sahrens spa->spa_root_vdev->vdev_children * leaves; 842fa9e4066Sahrens 843ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 844fa9e4066Sahrens 8458654d025Sperrin /* 8468654d025Sperrin * Make 1/4 of the devices be log devices. 8478654d025Sperrin */ 8488654d025Sperrin nvroot = make_vdev_root(zopt_vdev_size, 8498654d025Sperrin ztest_random(4) == 0, zopt_raidz, zopt_mirrors, 1); 8508654d025Sperrin 851fa9e4066Sahrens error = spa_vdev_add(spa, nvroot); 852fa9e4066Sahrens nvlist_free(nvroot); 853fa9e4066Sahrens 854fa9e4066Sahrens (void) mutex_unlock(&ztest_shared->zs_vdev_lock); 855fa9e4066Sahrens 856fa9e4066Sahrens if (error == ENOSPC) 857fa9e4066Sahrens ztest_record_enospc("spa_vdev_add"); 858fa9e4066Sahrens else if (error != 0) 859fa9e4066Sahrens fatal(0, "spa_vdev_add() = %d", error); 860fa9e4066Sahrens 861fa9e4066Sahrens if (zopt_verbose >= 6) 862fa9e4066Sahrens (void) printf("spa_vdev_add = %d, as expected\n", error); 863fa9e4066Sahrens } 864fa9e4066Sahrens 865ea8dc4b6Seschrock static vdev_t * 866ea8dc4b6Seschrock vdev_lookup_by_path(vdev_t *vd, const char *path) 867ea8dc4b6Seschrock { 868ea8dc4b6Seschrock int c; 869ea8dc4b6Seschrock vdev_t *mvd; 870ea8dc4b6Seschrock 871ea8dc4b6Seschrock if (vd->vdev_path != NULL) { 872ea8dc4b6Seschrock if (vd->vdev_wholedisk == 1) { 873ea8dc4b6Seschrock /* 874ea8dc4b6Seschrock * For whole disks, the internal path has 's0', but the 875ea8dc4b6Seschrock * path passed in by the user doesn't. 876ea8dc4b6Seschrock */ 877ea8dc4b6Seschrock if (strlen(path) == strlen(vd->vdev_path) - 2 && 878ea8dc4b6Seschrock strncmp(path, vd->vdev_path, strlen(path)) == 0) 879ea8dc4b6Seschrock return (vd); 880ea8dc4b6Seschrock } else if (strcmp(path, vd->vdev_path) == 0) { 881ea8dc4b6Seschrock return (vd); 882ea8dc4b6Seschrock } 883ea8dc4b6Seschrock } 884ea8dc4b6Seschrock 885ea8dc4b6Seschrock for (c = 0; c < vd->vdev_children; c++) 886ea8dc4b6Seschrock if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) != 887ea8dc4b6Seschrock NULL) 888ea8dc4b6Seschrock return (mvd); 889ea8dc4b6Seschrock 890ea8dc4b6Seschrock return (NULL); 891ea8dc4b6Seschrock } 892ea8dc4b6Seschrock 893fa9e4066Sahrens /* 894fa9e4066Sahrens * Verify that we can attach and detach devices. 895fa9e4066Sahrens */ 896fa9e4066Sahrens void 897fa9e4066Sahrens ztest_vdev_attach_detach(ztest_args_t *za) 898fa9e4066Sahrens { 899fa9e4066Sahrens spa_t *spa = dmu_objset_spa(za->za_os); 900fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 901ea8dc4b6Seschrock vdev_t *oldvd, *newvd, *pvd; 902fa9e4066Sahrens nvlist_t *root, *file; 903fa9e4066Sahrens uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz; 904fa9e4066Sahrens uint64_t leaf, top; 905ecc2d604Sbonwick uint64_t ashift = ztest_get_ashift(); 906ea8dc4b6Seschrock size_t oldsize, newsize; 907ea8dc4b6Seschrock char oldpath[MAXPATHLEN], newpath[MAXPATHLEN]; 908fa9e4066Sahrens int replacing; 909fa9e4066Sahrens int error, expected_error; 910fa9e4066Sahrens int fd; 911fa9e4066Sahrens 912fa9e4066Sahrens (void) mutex_lock(&ztest_shared->zs_vdev_lock); 913fa9e4066Sahrens 914ea8dc4b6Seschrock spa_config_enter(spa, RW_READER, FTAG); 915fa9e4066Sahrens 916fa9e4066Sahrens /* 917fa9e4066Sahrens * Decide whether to do an attach or a replace. 918fa9e4066Sahrens */ 919fa9e4066Sahrens replacing = ztest_random(2); 920fa9e4066Sahrens 921fa9e4066Sahrens /* 922fa9e4066Sahrens * Pick a random top-level vdev. 923fa9e4066Sahrens */ 924fa9e4066Sahrens top = ztest_random(rvd->vdev_children); 925fa9e4066Sahrens 926fa9e4066Sahrens /* 927fa9e4066Sahrens * Pick a random leaf within it. 928fa9e4066Sahrens */ 929fa9e4066Sahrens leaf = ztest_random(leaves); 930fa9e4066Sahrens 931fa9e4066Sahrens /* 932fa9e4066Sahrens * Generate the path to this leaf. The filename will end with 'a'. 933fa9e4066Sahrens * We'll alternate replacements with a filename that ends with 'b'. 934fa9e4066Sahrens */ 935ea8dc4b6Seschrock (void) snprintf(oldpath, sizeof (oldpath), 936fa9e4066Sahrens ztest_dev_template, zopt_dir, zopt_pool, top * leaves + leaf); 937fa9e4066Sahrens 938ea8dc4b6Seschrock bcopy(oldpath, newpath, MAXPATHLEN); 939fa9e4066Sahrens 940fa9e4066Sahrens /* 941fa9e4066Sahrens * If the 'a' file isn't part of the pool, the 'b' file must be. 942fa9e4066Sahrens */ 943ea8dc4b6Seschrock if (vdev_lookup_by_path(rvd, oldpath) == NULL) 944ea8dc4b6Seschrock oldpath[strlen(oldpath) - 1] = 'b'; 945fa9e4066Sahrens else 946ea8dc4b6Seschrock newpath[strlen(newpath) - 1] = 'b'; 947fa9e4066Sahrens 948fa9e4066Sahrens /* 949ea8dc4b6Seschrock * Now oldpath represents something that's already in the pool, 950ea8dc4b6Seschrock * and newpath is the thing we'll try to attach. 951fa9e4066Sahrens */ 952ea8dc4b6Seschrock oldvd = vdev_lookup_by_path(rvd, oldpath); 953ea8dc4b6Seschrock newvd = vdev_lookup_by_path(rvd, newpath); 954ea8dc4b6Seschrock ASSERT(oldvd != NULL); 955ea8dc4b6Seschrock pvd = oldvd->vdev_parent; 956fa9e4066Sahrens 957fa9e4066Sahrens /* 958ea8dc4b6Seschrock * Make newsize a little bigger or smaller than oldsize. 959fa9e4066Sahrens * If it's smaller, the attach should fail. 960fa9e4066Sahrens * If it's larger, and we're doing a replace, 961fa9e4066Sahrens * we should get dynamic LUN growth when we're done. 962fa9e4066Sahrens */ 963ea8dc4b6Seschrock oldsize = vdev_get_rsize(oldvd); 964ea8dc4b6Seschrock newsize = 10 * oldsize / (9 + ztest_random(3)); 965fa9e4066Sahrens 966fa9e4066Sahrens /* 967fa9e4066Sahrens * If pvd is not a mirror or root, the attach should fail with ENOTSUP, 968fa9e4066Sahrens * unless it's a replace; in that case any non-replacing parent is OK. 969fa9e4066Sahrens * 970ea8dc4b6Seschrock * If newvd is already part of the pool, it should fail with EBUSY. 971fa9e4066Sahrens * 972ea8dc4b6Seschrock * If newvd is too small, it should fail with EOVERFLOW. 973fa9e4066Sahrens */ 974b5989ec7Seschrock if (newvd != NULL) 975b5989ec7Seschrock expected_error = EBUSY; 976b5989ec7Seschrock else if (pvd->vdev_ops != &vdev_mirror_ops && 977fa9e4066Sahrens pvd->vdev_ops != &vdev_root_ops && 978fa9e4066Sahrens (!replacing || pvd->vdev_ops == &vdev_replacing_ops)) 979fa9e4066Sahrens expected_error = ENOTSUP; 980ea8dc4b6Seschrock else if (newsize < oldsize) 981fa9e4066Sahrens expected_error = EOVERFLOW; 982ecc2d604Sbonwick else if (ashift > oldvd->vdev_top->vdev_ashift) 983ecc2d604Sbonwick expected_error = EDOM; 984fa9e4066Sahrens else 985fa9e4066Sahrens expected_error = 0; 986fa9e4066Sahrens 987fa9e4066Sahrens /* 988ea8dc4b6Seschrock * If newvd isn't already part of the pool, create it. 989fa9e4066Sahrens */ 990ea8dc4b6Seschrock if (newvd == NULL) { 991ea8dc4b6Seschrock fd = open(newpath, O_RDWR | O_CREAT | O_TRUNC, 0666); 992fa9e4066Sahrens if (fd == -1) 993ea8dc4b6Seschrock fatal(1, "can't open %s", newpath); 994ea8dc4b6Seschrock if (ftruncate(fd, newsize) != 0) 995ea8dc4b6Seschrock fatal(1, "can't ftruncate %s", newpath); 996fa9e4066Sahrens (void) close(fd); 997fa9e4066Sahrens } 998fa9e4066Sahrens 999ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 1000fa9e4066Sahrens 1001fa9e4066Sahrens /* 1002ea8dc4b6Seschrock * Build the nvlist describing newpath. 1003fa9e4066Sahrens */ 1004fa9e4066Sahrens VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0); 1005fa9e4066Sahrens VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0); 1006ea8dc4b6Seschrock VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, newpath) == 0); 1007ecc2d604Sbonwick VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0); 1008fa9e4066Sahrens 1009fa9e4066Sahrens VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0); 1010fa9e4066Sahrens VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0); 1011fa9e4066Sahrens VERIFY(nvlist_add_nvlist_array(root, ZPOOL_CONFIG_CHILDREN, 1012fa9e4066Sahrens &file, 1) == 0); 1013fa9e4066Sahrens 1014ea8dc4b6Seschrock error = spa_vdev_attach(spa, oldvd->vdev_guid, root, replacing); 1015fa9e4066Sahrens 1016fa9e4066Sahrens nvlist_free(file); 1017fa9e4066Sahrens nvlist_free(root); 1018fa9e4066Sahrens 1019fa9e4066Sahrens /* 1020fa9e4066Sahrens * If our parent was the replacing vdev, but the replace completed, 1021fa9e4066Sahrens * then instead of failing with ENOTSUP we may either succeed, 1022fa9e4066Sahrens * fail with ENODEV, or fail with EOVERFLOW. 1023fa9e4066Sahrens */ 1024fa9e4066Sahrens if (expected_error == ENOTSUP && 1025fa9e4066Sahrens (error == 0 || error == ENODEV || error == EOVERFLOW)) 1026fa9e4066Sahrens expected_error = error; 1027fa9e4066Sahrens 1028f0aa80d4Sbonwick /* 1029f0aa80d4Sbonwick * If someone grew the LUN, the replacement may be too small. 1030f0aa80d4Sbonwick */ 1031f0aa80d4Sbonwick if (error == EOVERFLOW) 1032f0aa80d4Sbonwick expected_error = error; 1033f0aa80d4Sbonwick 1034fa9e4066Sahrens if (error != expected_error) { 1035fa9e4066Sahrens fatal(0, "attach (%s, %s, %d) returned %d, expected %d", 1036ea8dc4b6Seschrock oldpath, newpath, replacing, error, expected_error); 1037fa9e4066Sahrens } 1038fa9e4066Sahrens 1039fa9e4066Sahrens (void) mutex_unlock(&ztest_shared->zs_vdev_lock); 1040fa9e4066Sahrens } 1041fa9e4066Sahrens 1042fa9e4066Sahrens /* 1043fa9e4066Sahrens * Verify that dynamic LUN growth works as expected. 1044fa9e4066Sahrens */ 1045fa9e4066Sahrens /* ARGSUSED */ 1046fa9e4066Sahrens void 1047fa9e4066Sahrens ztest_vdev_LUN_growth(ztest_args_t *za) 1048fa9e4066Sahrens { 1049fa9e4066Sahrens spa_t *spa = dmu_objset_spa(za->za_os); 1050fa9e4066Sahrens char dev_name[MAXPATHLEN]; 1051fa9e4066Sahrens uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz; 1052fa9e4066Sahrens uint64_t vdev; 1053fa9e4066Sahrens size_t fsize; 1054fa9e4066Sahrens int fd; 1055fa9e4066Sahrens 1056fa9e4066Sahrens (void) mutex_lock(&ztest_shared->zs_vdev_lock); 1057fa9e4066Sahrens 1058fa9e4066Sahrens /* 1059fa9e4066Sahrens * Pick a random leaf vdev. 1060fa9e4066Sahrens */ 1061ea8dc4b6Seschrock spa_config_enter(spa, RW_READER, FTAG); 1062fa9e4066Sahrens vdev = ztest_random(spa->spa_root_vdev->vdev_children * leaves); 1063ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 1064fa9e4066Sahrens 1065fa9e4066Sahrens (void) sprintf(dev_name, ztest_dev_template, zopt_dir, zopt_pool, vdev); 1066fa9e4066Sahrens 1067fa9e4066Sahrens if ((fd = open(dev_name, O_RDWR)) != -1) { 1068fa9e4066Sahrens /* 1069fa9e4066Sahrens * Determine the size. 1070fa9e4066Sahrens */ 1071fa9e4066Sahrens fsize = lseek(fd, 0, SEEK_END); 1072fa9e4066Sahrens 1073fa9e4066Sahrens /* 1074fa9e4066Sahrens * If it's less than 2x the original size, grow by around 3%. 1075fa9e4066Sahrens */ 1076fa9e4066Sahrens if (fsize < 2 * zopt_vdev_size) { 1077fa9e4066Sahrens size_t newsize = fsize + ztest_random(fsize / 32); 1078fa9e4066Sahrens (void) ftruncate(fd, newsize); 1079fa9e4066Sahrens if (zopt_verbose >= 6) { 1080fa9e4066Sahrens (void) printf("%s grew from %lu to %lu bytes\n", 1081fa9e4066Sahrens dev_name, (ulong_t)fsize, (ulong_t)newsize); 1082fa9e4066Sahrens } 1083fa9e4066Sahrens } 1084fa9e4066Sahrens (void) close(fd); 1085fa9e4066Sahrens } 1086fa9e4066Sahrens 1087fa9e4066Sahrens (void) mutex_unlock(&ztest_shared->zs_vdev_lock); 1088fa9e4066Sahrens } 1089fa9e4066Sahrens 1090fa9e4066Sahrens /* ARGSUSED */ 1091fa9e4066Sahrens static void 1092ecd6cf80Smarks ztest_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) 1093fa9e4066Sahrens { 1094fa9e4066Sahrens /* 1095fa9e4066Sahrens * Create the directory object. 1096fa9e4066Sahrens */ 1097fa9e4066Sahrens VERIFY(dmu_object_claim(os, ZTEST_DIROBJ, 1098fa9e4066Sahrens DMU_OT_UINT64_OTHER, ZTEST_DIROBJ_BLOCKSIZE, 1099fa9e4066Sahrens DMU_OT_UINT64_OTHER, sizeof (ztest_block_tag_t), tx) == 0); 1100fa9e4066Sahrens 1101fa9e4066Sahrens VERIFY(zap_create_claim(os, ZTEST_MICROZAP_OBJ, 1102fa9e4066Sahrens DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0); 1103fa9e4066Sahrens 1104fa9e4066Sahrens VERIFY(zap_create_claim(os, ZTEST_FATZAP_OBJ, 1105fa9e4066Sahrens DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0); 1106fa9e4066Sahrens } 1107fa9e4066Sahrens 1108fa9e4066Sahrens /* ARGSUSED */ 11091d452cf5Sahrens static int 1110fa9e4066Sahrens ztest_destroy_cb(char *name, void *arg) 1111fa9e4066Sahrens { 1112fa9e4066Sahrens objset_t *os; 1113fa9e4066Sahrens dmu_object_info_t doi; 1114fa9e4066Sahrens int error; 1115fa9e4066Sahrens 1116fa9e4066Sahrens /* 1117fa9e4066Sahrens * Verify that the dataset contains a directory object. 1118fa9e4066Sahrens */ 1119fa9e4066Sahrens error = dmu_objset_open(name, DMU_OST_OTHER, 1120fa9e4066Sahrens DS_MODE_STANDARD | DS_MODE_READONLY, &os); 1121fa9e4066Sahrens ASSERT3U(error, ==, 0); 1122fa9e4066Sahrens error = dmu_object_info(os, ZTEST_DIROBJ, &doi); 1123e1930233Sbonwick if (error != ENOENT) { 1124e1930233Sbonwick /* We could have crashed in the middle of destroying it */ 1125fa9e4066Sahrens ASSERT3U(error, ==, 0); 1126fa9e4066Sahrens ASSERT3U(doi.doi_type, ==, DMU_OT_UINT64_OTHER); 1127fa9e4066Sahrens ASSERT3S(doi.doi_physical_blks, >=, 0); 1128e1930233Sbonwick } 1129fa9e4066Sahrens dmu_objset_close(os); 1130fa9e4066Sahrens 1131fa9e4066Sahrens /* 1132fa9e4066Sahrens * Destroy the dataset. 1133fa9e4066Sahrens */ 1134fa9e4066Sahrens error = dmu_objset_destroy(name); 1135fa9e4066Sahrens ASSERT3U(error, ==, 0); 11361d452cf5Sahrens return (0); 1137fa9e4066Sahrens } 1138fa9e4066Sahrens 1139fa9e4066Sahrens /* 1140fa9e4066Sahrens * Verify that dmu_objset_{create,destroy,open,close} work as expected. 1141fa9e4066Sahrens */ 1142fa9e4066Sahrens static uint64_t 1143fa9e4066Sahrens ztest_log_create(zilog_t *zilog, dmu_tx_t *tx, uint64_t object, int mode) 1144fa9e4066Sahrens { 1145fa9e4066Sahrens itx_t *itx; 1146fa9e4066Sahrens lr_create_t *lr; 1147fa9e4066Sahrens size_t namesize; 1148fa9e4066Sahrens char name[24]; 1149fa9e4066Sahrens 1150fa9e4066Sahrens (void) sprintf(name, "ZOBJ_%llu", (u_longlong_t)object); 1151fa9e4066Sahrens namesize = strlen(name) + 1; 1152fa9e4066Sahrens 1153fa9e4066Sahrens itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize + 1154fa9e4066Sahrens ztest_random(ZIL_MAX_BLKSZ)); 1155fa9e4066Sahrens lr = (lr_create_t *)&itx->itx_lr; 1156fa9e4066Sahrens bzero(lr + 1, lr->lr_common.lrc_reclen - sizeof (*lr)); 1157fa9e4066Sahrens lr->lr_doid = object; 1158fa9e4066Sahrens lr->lr_foid = 0; 1159fa9e4066Sahrens lr->lr_mode = mode; 1160fa9e4066Sahrens lr->lr_uid = 0; 1161fa9e4066Sahrens lr->lr_gid = 0; 1162fa9e4066Sahrens lr->lr_gen = dmu_tx_get_txg(tx); 1163fa9e4066Sahrens lr->lr_crtime[0] = time(NULL); 1164fa9e4066Sahrens lr->lr_crtime[1] = 0; 1165fa9e4066Sahrens lr->lr_rdev = 0; 1166fa9e4066Sahrens bcopy(name, (char *)(lr + 1), namesize); 1167fa9e4066Sahrens 1168fa9e4066Sahrens return (zil_itx_assign(zilog, itx, tx)); 1169fa9e4066Sahrens } 1170fa9e4066Sahrens 1171fa9e4066Sahrens void 1172fa9e4066Sahrens ztest_dmu_objset_create_destroy(ztest_args_t *za) 1173fa9e4066Sahrens { 1174fa9e4066Sahrens int error; 1175fa9e4066Sahrens objset_t *os; 1176fa9e4066Sahrens char name[100]; 1177fa9e4066Sahrens int mode, basemode, expected_error; 1178fa9e4066Sahrens zilog_t *zilog; 1179fa9e4066Sahrens uint64_t seq; 1180fa9e4066Sahrens uint64_t objects; 1181fa9e4066Sahrens ztest_replay_t zr; 1182fa9e4066Sahrens 1183fa9e4066Sahrens (void) rw_rdlock(&ztest_shared->zs_name_lock); 1184fa9e4066Sahrens (void) snprintf(name, 100, "%s/%s_temp_%llu", za->za_pool, za->za_pool, 1185fa9e4066Sahrens (u_longlong_t)za->za_instance); 1186fa9e4066Sahrens 1187fa9e4066Sahrens basemode = DS_MODE_LEVEL(za->za_instance); 1188fa9e4066Sahrens if (basemode == DS_MODE_NONE) 1189fa9e4066Sahrens basemode++; 1190fa9e4066Sahrens 1191fa9e4066Sahrens /* 1192fa9e4066Sahrens * If this dataset exists from a previous run, process its replay log 1193fa9e4066Sahrens * half of the time. If we don't replay it, then dmu_objset_destroy() 1194fa9e4066Sahrens * (invoked from ztest_destroy_cb() below) should just throw it away. 1195fa9e4066Sahrens */ 1196fa9e4066Sahrens if (ztest_random(2) == 0 && 1197fa9e4066Sahrens dmu_objset_open(name, DMU_OST_OTHER, DS_MODE_PRIMARY, &os) == 0) { 1198fa9e4066Sahrens zr.zr_os = os; 1199893a6d32Sahrens zil_replay(os, &zr, &zr.zr_assign, ztest_replay_vector); 1200fa9e4066Sahrens dmu_objset_close(os); 1201fa9e4066Sahrens } 1202fa9e4066Sahrens 1203fa9e4066Sahrens /* 1204fa9e4066Sahrens * There may be an old instance of the dataset we're about to 1205fa9e4066Sahrens * create lying around from a previous run. If so, destroy it 1206fa9e4066Sahrens * and all of its snapshots. 1207fa9e4066Sahrens */ 12080b69c2f0Sahrens (void) dmu_objset_find(name, ztest_destroy_cb, NULL, 12090b69c2f0Sahrens DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS); 1210fa9e4066Sahrens 1211fa9e4066Sahrens /* 1212fa9e4066Sahrens * Verify that the destroyed dataset is no longer in the namespace. 1213fa9e4066Sahrens */ 1214fa9e4066Sahrens error = dmu_objset_open(name, DMU_OST_OTHER, basemode, &os); 1215fa9e4066Sahrens if (error != ENOENT) 1216fa9e4066Sahrens fatal(1, "dmu_objset_open(%s) found destroyed dataset %p", 1217fa9e4066Sahrens name, os); 1218fa9e4066Sahrens 1219fa9e4066Sahrens /* 1220fa9e4066Sahrens * Verify that we can create a new dataset. 1221fa9e4066Sahrens */ 1222fa9e4066Sahrens error = dmu_objset_create(name, DMU_OST_OTHER, NULL, ztest_create_cb, 1223fa9e4066Sahrens NULL); 1224fa9e4066Sahrens if (error) { 1225fa9e4066Sahrens if (error == ENOSPC) { 1226fa9e4066Sahrens ztest_record_enospc("dmu_objset_create"); 1227fa9e4066Sahrens (void) rw_unlock(&ztest_shared->zs_name_lock); 1228fa9e4066Sahrens return; 1229fa9e4066Sahrens } 1230fa9e4066Sahrens fatal(0, "dmu_objset_create(%s) = %d", name, error); 1231fa9e4066Sahrens } 1232fa9e4066Sahrens 1233fa9e4066Sahrens error = dmu_objset_open(name, DMU_OST_OTHER, basemode, &os); 1234fa9e4066Sahrens if (error) { 1235fa9e4066Sahrens fatal(0, "dmu_objset_open(%s) = %d", name, error); 1236fa9e4066Sahrens } 1237fa9e4066Sahrens 1238fa9e4066Sahrens /* 1239fa9e4066Sahrens * Open the intent log for it. 1240fa9e4066Sahrens */ 1241fa9e4066Sahrens zilog = zil_open(os, NULL); 1242fa9e4066Sahrens 1243fa9e4066Sahrens /* 1244fa9e4066Sahrens * Put a random number of objects in there. 1245fa9e4066Sahrens */ 1246d80c45e0Sbonwick objects = ztest_random(20); 1247fa9e4066Sahrens seq = 0; 1248fa9e4066Sahrens while (objects-- != 0) { 1249fa9e4066Sahrens uint64_t object; 1250fa9e4066Sahrens dmu_tx_t *tx = dmu_tx_create(os); 1251fa9e4066Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, sizeof (name)); 1252fa9e4066Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 1253fa9e4066Sahrens if (error) { 1254fa9e4066Sahrens dmu_tx_abort(tx); 1255fa9e4066Sahrens } else { 1256fa9e4066Sahrens object = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0, 1257fa9e4066Sahrens DMU_OT_NONE, 0, tx); 1258fa9e4066Sahrens ztest_set_random_blocksize(os, object, tx); 1259fa9e4066Sahrens seq = ztest_log_create(zilog, tx, object, 1260fa9e4066Sahrens DMU_OT_UINT64_OTHER); 1261fa9e4066Sahrens dmu_write(os, object, 0, sizeof (name), name, tx); 1262fa9e4066Sahrens dmu_tx_commit(tx); 1263fa9e4066Sahrens } 1264fa9e4066Sahrens if (ztest_random(5) == 0) { 1265b19a79ecSperrin zil_commit(zilog, seq, object); 1266fa9e4066Sahrens } 1267d80c45e0Sbonwick if (ztest_random(100) == 0) { 1268fa9e4066Sahrens error = zil_suspend(zilog); 1269fa9e4066Sahrens if (error == 0) { 1270fa9e4066Sahrens zil_resume(zilog); 1271fa9e4066Sahrens } 1272fa9e4066Sahrens } 1273fa9e4066Sahrens } 1274fa9e4066Sahrens 1275fa9e4066Sahrens /* 1276fa9e4066Sahrens * Verify that we cannot create an existing dataset. 1277fa9e4066Sahrens */ 1278fa9e4066Sahrens error = dmu_objset_create(name, DMU_OST_OTHER, NULL, NULL, NULL); 1279fa9e4066Sahrens if (error != EEXIST) 1280fa9e4066Sahrens fatal(0, "created existing dataset, error = %d", error); 1281fa9e4066Sahrens 1282fa9e4066Sahrens /* 1283fa9e4066Sahrens * Verify that multiple dataset opens are allowed, but only when 1284fa9e4066Sahrens * the new access mode is compatible with the base mode. 1285fa9e4066Sahrens * We use a mixture of typed and typeless opens, and when the 1286fa9e4066Sahrens * open succeeds, verify that the discovered type is correct. 1287fa9e4066Sahrens */ 1288fa9e4066Sahrens for (mode = DS_MODE_STANDARD; mode < DS_MODE_LEVELS; mode++) { 1289fa9e4066Sahrens objset_t *os2; 1290fa9e4066Sahrens error = dmu_objset_open(name, DMU_OST_OTHER, mode, &os2); 1291fa9e4066Sahrens expected_error = (basemode + mode < DS_MODE_LEVELS) ? 0 : EBUSY; 1292fa9e4066Sahrens if (error != expected_error) 1293fa9e4066Sahrens fatal(0, "dmu_objset_open('%s') = %d, expected %d", 1294fa9e4066Sahrens name, error, expected_error); 1295fa9e4066Sahrens if (error == 0) 1296fa9e4066Sahrens dmu_objset_close(os2); 1297fa9e4066Sahrens } 1298fa9e4066Sahrens 1299fa9e4066Sahrens zil_close(zilog); 1300fa9e4066Sahrens dmu_objset_close(os); 1301fa9e4066Sahrens 1302fa9e4066Sahrens error = dmu_objset_destroy(name); 1303fa9e4066Sahrens if (error) 1304fa9e4066Sahrens fatal(0, "dmu_objset_destroy(%s) = %d", name, error); 1305fa9e4066Sahrens 1306fa9e4066Sahrens (void) rw_unlock(&ztest_shared->zs_name_lock); 1307fa9e4066Sahrens } 1308fa9e4066Sahrens 1309fa9e4066Sahrens /* 1310fa9e4066Sahrens * Verify that dmu_snapshot_{create,destroy,open,close} work as expected. 1311fa9e4066Sahrens */ 1312fa9e4066Sahrens void 1313fa9e4066Sahrens ztest_dmu_snapshot_create_destroy(ztest_args_t *za) 1314fa9e4066Sahrens { 1315fa9e4066Sahrens int error; 1316fa9e4066Sahrens objset_t *os = za->za_os; 1317fa9e4066Sahrens char snapname[100]; 1318fa9e4066Sahrens char osname[MAXNAMELEN]; 1319fa9e4066Sahrens 1320fa9e4066Sahrens (void) rw_rdlock(&ztest_shared->zs_name_lock); 1321fa9e4066Sahrens dmu_objset_name(os, osname); 1322fa9e4066Sahrens (void) snprintf(snapname, 100, "%s@%llu", osname, 1323fa9e4066Sahrens (u_longlong_t)za->za_instance); 1324fa9e4066Sahrens 1325fa9e4066Sahrens error = dmu_objset_destroy(snapname); 1326fa9e4066Sahrens if (error != 0 && error != ENOENT) 1327fa9e4066Sahrens fatal(0, "dmu_objset_destroy() = %d", error); 13281d452cf5Sahrens error = dmu_objset_snapshot(osname, strchr(snapname, '@')+1, FALSE); 1329fa9e4066Sahrens if (error == ENOSPC) 1330fa9e4066Sahrens ztest_record_enospc("dmu_take_snapshot"); 1331fa9e4066Sahrens else if (error != 0 && error != EEXIST) 1332fa9e4066Sahrens fatal(0, "dmu_take_snapshot() = %d", error); 1333fa9e4066Sahrens (void) rw_unlock(&ztest_shared->zs_name_lock); 1334fa9e4066Sahrens } 1335fa9e4066Sahrens 1336fa9e4066Sahrens #define ZTEST_TRAVERSE_BLOCKS 1000 1337fa9e4066Sahrens 1338fa9e4066Sahrens static int 1339fa9e4066Sahrens ztest_blk_cb(traverse_blk_cache_t *bc, spa_t *spa, void *arg) 1340fa9e4066Sahrens { 1341fa9e4066Sahrens ztest_args_t *za = arg; 1342fa9e4066Sahrens zbookmark_t *zb = &bc->bc_bookmark; 1343fa9e4066Sahrens blkptr_t *bp = &bc->bc_blkptr; 1344fa9e4066Sahrens dnode_phys_t *dnp = bc->bc_dnode; 1345fa9e4066Sahrens traverse_handle_t *th = za->za_th; 1346fa9e4066Sahrens uint64_t size = BP_GET_LSIZE(bp); 1347fa9e4066Sahrens 1348ea8dc4b6Seschrock /* 1349ea8dc4b6Seschrock * Level -1 indicates the objset_phys_t or something in its intent log. 1350ea8dc4b6Seschrock */ 1351ea8dc4b6Seschrock if (zb->zb_level == -1) { 1352ea8dc4b6Seschrock if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) { 1353ea8dc4b6Seschrock ASSERT3U(zb->zb_object, ==, 0); 1354ea8dc4b6Seschrock ASSERT3U(zb->zb_blkid, ==, 0); 1355ea8dc4b6Seschrock ASSERT3U(size, ==, sizeof (objset_phys_t)); 1356ea8dc4b6Seschrock za->za_zil_seq = 0; 1357ea8dc4b6Seschrock } else if (BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG) { 1358ea8dc4b6Seschrock ASSERT3U(zb->zb_object, ==, 0); 1359ea8dc4b6Seschrock ASSERT3U(zb->zb_blkid, >, za->za_zil_seq); 1360ea8dc4b6Seschrock za->za_zil_seq = zb->zb_blkid; 1361ea8dc4b6Seschrock } else { 1362ea8dc4b6Seschrock ASSERT3U(zb->zb_object, !=, 0); /* lr_write_t */ 1363ea8dc4b6Seschrock } 1364ea8dc4b6Seschrock 1365ea8dc4b6Seschrock return (0); 1366ea8dc4b6Seschrock } 1367ea8dc4b6Seschrock 1368fa9e4066Sahrens ASSERT(dnp != NULL); 1369fa9e4066Sahrens 1370fa9e4066Sahrens if (bc->bc_errno) 1371fa9e4066Sahrens return (ERESTART); 1372fa9e4066Sahrens 1373fa9e4066Sahrens /* 1374fa9e4066Sahrens * Once in a while, abort the traverse. We only do this to odd 1375fa9e4066Sahrens * instance numbers to ensure that even ones can run to completion. 1376fa9e4066Sahrens */ 1377fa9e4066Sahrens if ((za->za_instance & 1) && ztest_random(10000) == 0) 1378fa9e4066Sahrens return (EINTR); 1379fa9e4066Sahrens 1380fa9e4066Sahrens if (bp->blk_birth == 0) { 1381fa9e4066Sahrens ASSERT(th->th_advance & ADVANCE_HOLES); 1382fa9e4066Sahrens return (0); 1383fa9e4066Sahrens } 1384fa9e4066Sahrens 1385fa9e4066Sahrens if (zb->zb_level == 0 && !(th->th_advance & ADVANCE_DATA) && 1386fa9e4066Sahrens bc == &th->th_cache[ZB_DN_CACHE][0]) { 1387fa9e4066Sahrens ASSERT(bc->bc_data == NULL); 1388fa9e4066Sahrens return (0); 1389fa9e4066Sahrens } 1390fa9e4066Sahrens 1391fa9e4066Sahrens ASSERT(bc->bc_data != NULL); 1392fa9e4066Sahrens 1393fa9e4066Sahrens /* 1394fa9e4066Sahrens * This is an expensive question, so don't ask it too often. 1395fa9e4066Sahrens */ 1396fa9e4066Sahrens if (((za->za_random ^ th->th_callbacks) & 0xff) == 0) { 1397fa9e4066Sahrens void *xbuf = umem_alloc(size, UMEM_NOFAIL); 1398fa9e4066Sahrens if (arc_tryread(spa, bp, xbuf) == 0) { 1399fa9e4066Sahrens ASSERT(bcmp(bc->bc_data, xbuf, size) == 0); 1400fa9e4066Sahrens } 1401fa9e4066Sahrens umem_free(xbuf, size); 1402fa9e4066Sahrens } 1403fa9e4066Sahrens 1404fa9e4066Sahrens if (zb->zb_level > 0) { 1405fa9e4066Sahrens ASSERT3U(size, ==, 1ULL << dnp->dn_indblkshift); 1406fa9e4066Sahrens return (0); 1407fa9e4066Sahrens } 1408fa9e4066Sahrens 1409fa9e4066Sahrens ASSERT(zb->zb_level == 0); 1410fa9e4066Sahrens ASSERT3U(size, ==, dnp->dn_datablkszsec << DEV_BSHIFT); 1411fa9e4066Sahrens 1412fa9e4066Sahrens return (0); 1413fa9e4066Sahrens } 1414fa9e4066Sahrens 1415fa9e4066Sahrens /* 1416fa9e4066Sahrens * Verify that live pool traversal works. 1417fa9e4066Sahrens */ 1418fa9e4066Sahrens void 1419fa9e4066Sahrens ztest_traverse(ztest_args_t *za) 1420fa9e4066Sahrens { 1421fa9e4066Sahrens spa_t *spa = dmu_objset_spa(za->za_os); 1422fa9e4066Sahrens traverse_handle_t *th = za->za_th; 1423fa9e4066Sahrens int rc, advance; 1424fa9e4066Sahrens uint64_t cbstart, cblimit; 1425fa9e4066Sahrens 1426fa9e4066Sahrens if (th == NULL) { 1427fa9e4066Sahrens advance = 0; 1428fa9e4066Sahrens 1429fa9e4066Sahrens if (ztest_random(2) == 0) 1430fa9e4066Sahrens advance |= ADVANCE_PRE; 1431fa9e4066Sahrens 1432fa9e4066Sahrens if (ztest_random(2) == 0) 1433fa9e4066Sahrens advance |= ADVANCE_PRUNE; 1434fa9e4066Sahrens 1435fa9e4066Sahrens if (ztest_random(2) == 0) 1436fa9e4066Sahrens advance |= ADVANCE_DATA; 1437fa9e4066Sahrens 1438fa9e4066Sahrens if (ztest_random(2) == 0) 1439fa9e4066Sahrens advance |= ADVANCE_HOLES; 1440fa9e4066Sahrens 1441ea8dc4b6Seschrock if (ztest_random(2) == 0) 1442ea8dc4b6Seschrock advance |= ADVANCE_ZIL; 1443ea8dc4b6Seschrock 1444fa9e4066Sahrens th = za->za_th = traverse_init(spa, ztest_blk_cb, za, advance, 1445fa9e4066Sahrens ZIO_FLAG_CANFAIL); 1446fa9e4066Sahrens 1447fa9e4066Sahrens traverse_add_pool(th, 0, -1ULL); 1448fa9e4066Sahrens } 1449fa9e4066Sahrens 1450fa9e4066Sahrens advance = th->th_advance; 1451fa9e4066Sahrens cbstart = th->th_callbacks; 1452fa9e4066Sahrens cblimit = cbstart + ((advance & ADVANCE_DATA) ? 100 : 1000); 1453fa9e4066Sahrens 1454fa9e4066Sahrens while ((rc = traverse_more(th)) == EAGAIN && th->th_callbacks < cblimit) 1455fa9e4066Sahrens continue; 1456fa9e4066Sahrens 1457fa9e4066Sahrens if (zopt_verbose >= 5) 1458fa9e4066Sahrens (void) printf("traverse %s%s%s%s %llu blocks to " 1459ea8dc4b6Seschrock "<%llu, %llu, %lld, %llx>%s\n", 1460fa9e4066Sahrens (advance & ADVANCE_PRE) ? "pre" : "post", 1461fa9e4066Sahrens (advance & ADVANCE_PRUNE) ? "|prune" : "", 1462fa9e4066Sahrens (advance & ADVANCE_DATA) ? "|data" : "", 1463fa9e4066Sahrens (advance & ADVANCE_HOLES) ? "|holes" : "", 1464fa9e4066Sahrens (u_longlong_t)(th->th_callbacks - cbstart), 1465fa9e4066Sahrens (u_longlong_t)th->th_lastcb.zb_objset, 1466fa9e4066Sahrens (u_longlong_t)th->th_lastcb.zb_object, 1467ea8dc4b6Seschrock (u_longlong_t)th->th_lastcb.zb_level, 1468fa9e4066Sahrens (u_longlong_t)th->th_lastcb.zb_blkid, 1469fa9e4066Sahrens rc == 0 ? " [done]" : 1470fa9e4066Sahrens rc == EINTR ? " [aborted]" : 1471fa9e4066Sahrens rc == EAGAIN ? "" : 1472fa9e4066Sahrens strerror(rc)); 1473fa9e4066Sahrens 1474fa9e4066Sahrens if (rc != EAGAIN) { 1475fa9e4066Sahrens if (rc != 0 && rc != EINTR) 1476fa9e4066Sahrens fatal(0, "traverse_more(%p) = %d", th, rc); 1477fa9e4066Sahrens traverse_fini(th); 1478fa9e4066Sahrens za->za_th = NULL; 1479fa9e4066Sahrens } 1480fa9e4066Sahrens } 1481fa9e4066Sahrens 1482fa9e4066Sahrens /* 1483fa9e4066Sahrens * Verify that dmu_object_{alloc,free} work as expected. 1484fa9e4066Sahrens */ 1485fa9e4066Sahrens void 1486fa9e4066Sahrens ztest_dmu_object_alloc_free(ztest_args_t *za) 1487fa9e4066Sahrens { 1488fa9e4066Sahrens objset_t *os = za->za_os; 1489fa9e4066Sahrens dmu_buf_t *db; 1490fa9e4066Sahrens dmu_tx_t *tx; 1491fa9e4066Sahrens uint64_t batchobj, object, batchsize, endoff, temp; 1492fa9e4066Sahrens int b, c, error, bonuslen; 1493fa9e4066Sahrens dmu_object_info_t doi; 1494fa9e4066Sahrens char osname[MAXNAMELEN]; 1495fa9e4066Sahrens 1496fa9e4066Sahrens dmu_objset_name(os, osname); 1497fa9e4066Sahrens 1498fa9e4066Sahrens endoff = -8ULL; 1499fa9e4066Sahrens batchsize = 2; 1500fa9e4066Sahrens 1501fa9e4066Sahrens /* 1502fa9e4066Sahrens * Create a batch object if necessary, and record it in the directory. 1503fa9e4066Sahrens */ 1504ea8dc4b6Seschrock VERIFY(0 == dmu_read(os, ZTEST_DIROBJ, za->za_diroff, 1505ea8dc4b6Seschrock sizeof (uint64_t), &batchobj)); 1506fa9e4066Sahrens if (batchobj == 0) { 1507fa9e4066Sahrens tx = dmu_tx_create(os); 1508fa9e4066Sahrens dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, 1509fa9e4066Sahrens sizeof (uint64_t)); 1510fa9e4066Sahrens dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 1511fa9e4066Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 1512fa9e4066Sahrens if (error) { 1513fa9e4066Sahrens ztest_record_enospc("create a batch object"); 1514fa9e4066Sahrens dmu_tx_abort(tx); 1515fa9e4066Sahrens return; 1516fa9e4066Sahrens } 1517fa9e4066Sahrens batchobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0, 1518fa9e4066Sahrens DMU_OT_NONE, 0, tx); 1519fa9e4066Sahrens ztest_set_random_blocksize(os, batchobj, tx); 1520fa9e4066Sahrens dmu_write(os, ZTEST_DIROBJ, za->za_diroff, 1521fa9e4066Sahrens sizeof (uint64_t), &batchobj, tx); 1522fa9e4066Sahrens dmu_tx_commit(tx); 1523fa9e4066Sahrens } 1524fa9e4066Sahrens 1525fa9e4066Sahrens /* 1526fa9e4066Sahrens * Destroy the previous batch of objects. 1527fa9e4066Sahrens */ 1528fa9e4066Sahrens for (b = 0; b < batchsize; b++) { 1529ea8dc4b6Seschrock VERIFY(0 == dmu_read(os, batchobj, b * sizeof (uint64_t), 1530ea8dc4b6Seschrock sizeof (uint64_t), &object)); 1531fa9e4066Sahrens if (object == 0) 1532fa9e4066Sahrens continue; 1533fa9e4066Sahrens /* 1534fa9e4066Sahrens * Read and validate contents. 1535fa9e4066Sahrens * We expect the nth byte of the bonus buffer to be n. 1536fa9e4066Sahrens */ 1537ea8dc4b6Seschrock VERIFY(0 == dmu_bonus_hold(os, object, FTAG, &db)); 1538fa9e4066Sahrens 1539fa9e4066Sahrens dmu_object_info_from_db(db, &doi); 1540fa9e4066Sahrens ASSERT(doi.doi_type == DMU_OT_UINT64_OTHER); 1541fa9e4066Sahrens ASSERT(doi.doi_bonus_type == DMU_OT_PLAIN_OTHER); 1542fa9e4066Sahrens ASSERT3S(doi.doi_physical_blks, >=, 0); 1543fa9e4066Sahrens 1544fa9e4066Sahrens bonuslen = db->db_size; 1545fa9e4066Sahrens 1546fa9e4066Sahrens for (c = 0; c < bonuslen; c++) { 1547fa9e4066Sahrens if (((uint8_t *)db->db_data)[c] != 1548fa9e4066Sahrens (uint8_t)(c + bonuslen)) { 1549fa9e4066Sahrens fatal(0, 1550fa9e4066Sahrens "bad bonus: %s, obj %llu, off %d: %u != %u", 1551fa9e4066Sahrens osname, object, c, 1552fa9e4066Sahrens ((uint8_t *)db->db_data)[c], 1553fa9e4066Sahrens (uint8_t)(c + bonuslen)); 1554fa9e4066Sahrens } 1555fa9e4066Sahrens } 1556fa9e4066Sahrens 1557ea8dc4b6Seschrock dmu_buf_rele(db, FTAG); 1558fa9e4066Sahrens 1559fa9e4066Sahrens /* 1560fa9e4066Sahrens * We expect the word at endoff to be our object number. 1561fa9e4066Sahrens */ 1562ea8dc4b6Seschrock VERIFY(0 == dmu_read(os, object, endoff, 1563ea8dc4b6Seschrock sizeof (uint64_t), &temp)); 1564fa9e4066Sahrens 1565fa9e4066Sahrens if (temp != object) { 1566fa9e4066Sahrens fatal(0, "bad data in %s, got %llu, expected %llu", 1567fa9e4066Sahrens osname, temp, object); 1568fa9e4066Sahrens } 1569fa9e4066Sahrens 1570fa9e4066Sahrens /* 1571fa9e4066Sahrens * Destroy old object and clear batch entry. 1572fa9e4066Sahrens */ 1573fa9e4066Sahrens tx = dmu_tx_create(os); 1574fa9e4066Sahrens dmu_tx_hold_write(tx, batchobj, 1575fa9e4066Sahrens b * sizeof (uint64_t), sizeof (uint64_t)); 1576fa9e4066Sahrens dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END); 1577fa9e4066Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 1578fa9e4066Sahrens if (error) { 1579fa9e4066Sahrens ztest_record_enospc("free object"); 1580fa9e4066Sahrens dmu_tx_abort(tx); 1581fa9e4066Sahrens return; 1582fa9e4066Sahrens } 1583fa9e4066Sahrens error = dmu_object_free(os, object, tx); 1584fa9e4066Sahrens if (error) { 1585fa9e4066Sahrens fatal(0, "dmu_object_free('%s', %llu) = %d", 1586fa9e4066Sahrens osname, object, error); 1587fa9e4066Sahrens } 1588fa9e4066Sahrens object = 0; 1589fa9e4066Sahrens 1590fa9e4066Sahrens dmu_object_set_checksum(os, batchobj, 1591fa9e4066Sahrens ztest_random_checksum(), tx); 1592fa9e4066Sahrens dmu_object_set_compress(os, batchobj, 1593fa9e4066Sahrens ztest_random_compress(), tx); 1594fa9e4066Sahrens 1595fa9e4066Sahrens dmu_write(os, batchobj, b * sizeof (uint64_t), 1596fa9e4066Sahrens sizeof (uint64_t), &object, tx); 1597fa9e4066Sahrens 1598fa9e4066Sahrens dmu_tx_commit(tx); 1599fa9e4066Sahrens } 1600fa9e4066Sahrens 1601fa9e4066Sahrens /* 1602fa9e4066Sahrens * Before creating the new batch of objects, generate a bunch of churn. 1603fa9e4066Sahrens */ 1604fa9e4066Sahrens for (b = ztest_random(100); b > 0; b--) { 1605fa9e4066Sahrens tx = dmu_tx_create(os); 1606fa9e4066Sahrens dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 1607fa9e4066Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 1608fa9e4066Sahrens if (error) { 1609fa9e4066Sahrens ztest_record_enospc("churn objects"); 1610fa9e4066Sahrens dmu_tx_abort(tx); 1611fa9e4066Sahrens return; 1612fa9e4066Sahrens } 1613fa9e4066Sahrens object = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0, 1614fa9e4066Sahrens DMU_OT_NONE, 0, tx); 1615fa9e4066Sahrens ztest_set_random_blocksize(os, object, tx); 1616fa9e4066Sahrens error = dmu_object_free(os, object, tx); 1617fa9e4066Sahrens if (error) { 1618fa9e4066Sahrens fatal(0, "dmu_object_free('%s', %llu) = %d", 1619fa9e4066Sahrens osname, object, error); 1620fa9e4066Sahrens } 1621fa9e4066Sahrens dmu_tx_commit(tx); 1622fa9e4066Sahrens } 1623fa9e4066Sahrens 1624fa9e4066Sahrens /* 1625fa9e4066Sahrens * Create a new batch of objects with randomly chosen 1626fa9e4066Sahrens * blocksizes and record them in the batch directory. 1627fa9e4066Sahrens */ 1628fa9e4066Sahrens for (b = 0; b < batchsize; b++) { 1629fa9e4066Sahrens uint32_t va_blksize; 1630fa9e4066Sahrens u_longlong_t va_nblocks; 1631fa9e4066Sahrens 1632fa9e4066Sahrens tx = dmu_tx_create(os); 1633fa9e4066Sahrens dmu_tx_hold_write(tx, batchobj, b * sizeof (uint64_t), 1634fa9e4066Sahrens sizeof (uint64_t)); 1635fa9e4066Sahrens dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 1636fa9e4066Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, endoff, 1637fa9e4066Sahrens sizeof (uint64_t)); 1638fa9e4066Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 1639fa9e4066Sahrens if (error) { 1640fa9e4066Sahrens ztest_record_enospc("create batchobj"); 1641fa9e4066Sahrens dmu_tx_abort(tx); 1642fa9e4066Sahrens return; 1643fa9e4066Sahrens } 1644fa9e4066Sahrens bonuslen = (int)ztest_random(dmu_bonus_max()) + 1; 1645fa9e4066Sahrens 1646fa9e4066Sahrens object = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0, 1647fa9e4066Sahrens DMU_OT_PLAIN_OTHER, bonuslen, tx); 1648fa9e4066Sahrens 1649fa9e4066Sahrens ztest_set_random_blocksize(os, object, tx); 1650fa9e4066Sahrens 1651fa9e4066Sahrens dmu_object_set_checksum(os, object, 1652fa9e4066Sahrens ztest_random_checksum(), tx); 1653fa9e4066Sahrens dmu_object_set_compress(os, object, 1654fa9e4066Sahrens ztest_random_compress(), tx); 1655fa9e4066Sahrens 1656fa9e4066Sahrens dmu_write(os, batchobj, b * sizeof (uint64_t), 1657fa9e4066Sahrens sizeof (uint64_t), &object, tx); 1658fa9e4066Sahrens 1659fa9e4066Sahrens /* 1660fa9e4066Sahrens * Write to both the bonus buffer and the regular data. 1661fa9e4066Sahrens */ 1662ea8dc4b6Seschrock VERIFY(0 == dmu_bonus_hold(os, object, FTAG, &db)); 1663fa9e4066Sahrens ASSERT3U(bonuslen, ==, db->db_size); 1664fa9e4066Sahrens 1665fa9e4066Sahrens dmu_object_size_from_db(db, &va_blksize, &va_nblocks); 1666fa9e4066Sahrens ASSERT3S(va_nblocks, >=, 0); 1667fa9e4066Sahrens 1668fa9e4066Sahrens dmu_buf_will_dirty(db, tx); 1669fa9e4066Sahrens 1670fa9e4066Sahrens /* 1671fa9e4066Sahrens * See comments above regarding the contents of 1672fa9e4066Sahrens * the bonus buffer and the word at endoff. 1673fa9e4066Sahrens */ 1674fa9e4066Sahrens for (c = 0; c < db->db_size; c++) 1675fa9e4066Sahrens ((uint8_t *)db->db_data)[c] = (uint8_t)(c + bonuslen); 1676fa9e4066Sahrens 1677ea8dc4b6Seschrock dmu_buf_rele(db, FTAG); 1678fa9e4066Sahrens 1679fa9e4066Sahrens /* 1680fa9e4066Sahrens * Write to a large offset to increase indirection. 1681fa9e4066Sahrens */ 1682fa9e4066Sahrens dmu_write(os, object, endoff, sizeof (uint64_t), &object, tx); 1683fa9e4066Sahrens 1684fa9e4066Sahrens dmu_tx_commit(tx); 1685fa9e4066Sahrens } 1686fa9e4066Sahrens } 1687fa9e4066Sahrens 1688fa9e4066Sahrens /* 1689fa9e4066Sahrens * Verify that dmu_{read,write} work as expected. 1690fa9e4066Sahrens */ 1691fa9e4066Sahrens typedef struct bufwad { 1692fa9e4066Sahrens uint64_t bw_index; 1693fa9e4066Sahrens uint64_t bw_txg; 1694fa9e4066Sahrens uint64_t bw_data; 1695fa9e4066Sahrens } bufwad_t; 1696fa9e4066Sahrens 1697fa9e4066Sahrens typedef struct dmu_read_write_dir { 1698fa9e4066Sahrens uint64_t dd_packobj; 1699fa9e4066Sahrens uint64_t dd_bigobj; 1700fa9e4066Sahrens uint64_t dd_chunk; 1701fa9e4066Sahrens } dmu_read_write_dir_t; 1702fa9e4066Sahrens 1703fa9e4066Sahrens void 1704fa9e4066Sahrens ztest_dmu_read_write(ztest_args_t *za) 1705fa9e4066Sahrens { 1706fa9e4066Sahrens objset_t *os = za->za_os; 1707fa9e4066Sahrens dmu_read_write_dir_t dd; 1708fa9e4066Sahrens dmu_tx_t *tx; 1709fa9e4066Sahrens int i, freeit, error; 1710fa9e4066Sahrens uint64_t n, s, txg; 1711fa9e4066Sahrens bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT; 1712fa9e4066Sahrens uint64_t packoff, packsize, bigoff, bigsize; 1713fa9e4066Sahrens uint64_t regions = 997; 1714fa9e4066Sahrens uint64_t stride = 123456789ULL; 1715fa9e4066Sahrens uint64_t width = 40; 1716fa9e4066Sahrens int free_percent = 5; 1717fa9e4066Sahrens 1718fa9e4066Sahrens /* 1719fa9e4066Sahrens * This test uses two objects, packobj and bigobj, that are always 1720fa9e4066Sahrens * updated together (i.e. in the same tx) so that their contents are 1721fa9e4066Sahrens * in sync and can be compared. Their contents relate to each other 1722fa9e4066Sahrens * in a simple way: packobj is a dense array of 'bufwad' structures, 1723fa9e4066Sahrens * while bigobj is a sparse array of the same bufwads. Specifically, 1724fa9e4066Sahrens * for any index n, there are three bufwads that should be identical: 1725fa9e4066Sahrens * 1726fa9e4066Sahrens * packobj, at offset n * sizeof (bufwad_t) 1727fa9e4066Sahrens * bigobj, at the head of the nth chunk 1728fa9e4066Sahrens * bigobj, at the tail of the nth chunk 1729fa9e4066Sahrens * 1730fa9e4066Sahrens * The chunk size is arbitrary. It doesn't have to be a power of two, 1731fa9e4066Sahrens * and it doesn't have any relation to the object blocksize. 1732fa9e4066Sahrens * The only requirement is that it can hold at least two bufwads. 1733fa9e4066Sahrens * 1734fa9e4066Sahrens * Normally, we write the bufwad to each of these locations. 1735fa9e4066Sahrens * However, free_percent of the time we instead write zeroes to 1736fa9e4066Sahrens * packobj and perform a dmu_free_range() on bigobj. By comparing 1737fa9e4066Sahrens * bigobj to packobj, we can verify that the DMU is correctly 1738fa9e4066Sahrens * tracking which parts of an object are allocated and free, 1739fa9e4066Sahrens * and that the contents of the allocated blocks are correct. 1740fa9e4066Sahrens */ 1741fa9e4066Sahrens 1742fa9e4066Sahrens /* 1743fa9e4066Sahrens * Read the directory info. If it's the first time, set things up. 1744fa9e4066Sahrens */ 1745ea8dc4b6Seschrock VERIFY(0 == dmu_read(os, ZTEST_DIROBJ, za->za_diroff, 1746ea8dc4b6Seschrock sizeof (dd), &dd)); 1747fa9e4066Sahrens if (dd.dd_chunk == 0) { 1748fa9e4066Sahrens ASSERT(dd.dd_packobj == 0); 1749fa9e4066Sahrens ASSERT(dd.dd_bigobj == 0); 1750fa9e4066Sahrens tx = dmu_tx_create(os); 1751fa9e4066Sahrens dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, sizeof (dd)); 1752fa9e4066Sahrens dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 1753fa9e4066Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 1754fa9e4066Sahrens if (error) { 1755fa9e4066Sahrens ztest_record_enospc("create r/w directory"); 1756fa9e4066Sahrens dmu_tx_abort(tx); 1757fa9e4066Sahrens return; 1758fa9e4066Sahrens } 1759fa9e4066Sahrens 1760fa9e4066Sahrens dd.dd_packobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0, 1761fa9e4066Sahrens DMU_OT_NONE, 0, tx); 1762fa9e4066Sahrens dd.dd_bigobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0, 1763fa9e4066Sahrens DMU_OT_NONE, 0, tx); 1764fa9e4066Sahrens dd.dd_chunk = (1000 + ztest_random(1000)) * sizeof (uint64_t); 1765fa9e4066Sahrens 1766fa9e4066Sahrens ztest_set_random_blocksize(os, dd.dd_packobj, tx); 1767fa9e4066Sahrens ztest_set_random_blocksize(os, dd.dd_bigobj, tx); 1768fa9e4066Sahrens 1769fa9e4066Sahrens dmu_write(os, ZTEST_DIROBJ, za->za_diroff, sizeof (dd), &dd, 1770fa9e4066Sahrens tx); 1771fa9e4066Sahrens dmu_tx_commit(tx); 1772fa9e4066Sahrens } 1773fa9e4066Sahrens 1774fa9e4066Sahrens /* 1775fa9e4066Sahrens * Prefetch a random chunk of the big object. 1776fa9e4066Sahrens * Our aim here is to get some async reads in flight 1777fa9e4066Sahrens * for blocks that we may free below; the DMU should 1778fa9e4066Sahrens * handle this race correctly. 1779fa9e4066Sahrens */ 1780fa9e4066Sahrens n = ztest_random(regions) * stride + ztest_random(width); 1781fa9e4066Sahrens s = 1 + ztest_random(2 * width - 1); 1782fa9e4066Sahrens dmu_prefetch(os, dd.dd_bigobj, n * dd.dd_chunk, s * dd.dd_chunk); 1783fa9e4066Sahrens 1784fa9e4066Sahrens /* 1785fa9e4066Sahrens * Pick a random index and compute the offsets into packobj and bigobj. 1786fa9e4066Sahrens */ 1787fa9e4066Sahrens n = ztest_random(regions) * stride + ztest_random(width); 1788fa9e4066Sahrens s = 1 + ztest_random(width - 1); 1789fa9e4066Sahrens 1790fa9e4066Sahrens packoff = n * sizeof (bufwad_t); 1791fa9e4066Sahrens packsize = s * sizeof (bufwad_t); 1792fa9e4066Sahrens 1793fa9e4066Sahrens bigoff = n * dd.dd_chunk; 1794fa9e4066Sahrens bigsize = s * dd.dd_chunk; 1795fa9e4066Sahrens 1796fa9e4066Sahrens packbuf = umem_alloc(packsize, UMEM_NOFAIL); 1797fa9e4066Sahrens bigbuf = umem_alloc(bigsize, UMEM_NOFAIL); 1798fa9e4066Sahrens 1799fa9e4066Sahrens /* 1800fa9e4066Sahrens * free_percent of the time, free a range of bigobj rather than 1801fa9e4066Sahrens * overwriting it. 1802fa9e4066Sahrens */ 1803fa9e4066Sahrens freeit = (ztest_random(100) < free_percent); 1804fa9e4066Sahrens 1805fa9e4066Sahrens /* 1806fa9e4066Sahrens * Read the current contents of our objects. 1807fa9e4066Sahrens */ 1808ea8dc4b6Seschrock error = dmu_read(os, dd.dd_packobj, packoff, packsize, packbuf); 1809ea8dc4b6Seschrock ASSERT3U(error, ==, 0); 1810ea8dc4b6Seschrock error = dmu_read(os, dd.dd_bigobj, bigoff, bigsize, bigbuf); 1811ea8dc4b6Seschrock ASSERT3U(error, ==, 0); 1812fa9e4066Sahrens 1813fa9e4066Sahrens /* 1814fa9e4066Sahrens * Get a tx for the mods to both packobj and bigobj. 1815fa9e4066Sahrens */ 1816fa9e4066Sahrens tx = dmu_tx_create(os); 1817fa9e4066Sahrens 1818fa9e4066Sahrens dmu_tx_hold_write(tx, dd.dd_packobj, packoff, packsize); 1819fa9e4066Sahrens 1820fa9e4066Sahrens if (freeit) 1821fa9e4066Sahrens dmu_tx_hold_free(tx, dd.dd_bigobj, bigoff, bigsize); 1822fa9e4066Sahrens else 1823fa9e4066Sahrens dmu_tx_hold_write(tx, dd.dd_bigobj, bigoff, bigsize); 1824fa9e4066Sahrens 1825fa9e4066Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 1826fa9e4066Sahrens 1827fa9e4066Sahrens if (error) { 1828fa9e4066Sahrens ztest_record_enospc("dmu r/w range"); 1829fa9e4066Sahrens dmu_tx_abort(tx); 1830fa9e4066Sahrens umem_free(packbuf, packsize); 1831fa9e4066Sahrens umem_free(bigbuf, bigsize); 1832fa9e4066Sahrens return; 1833fa9e4066Sahrens } 1834fa9e4066Sahrens 1835fa9e4066Sahrens txg = dmu_tx_get_txg(tx); 1836fa9e4066Sahrens 1837fa9e4066Sahrens /* 1838fa9e4066Sahrens * For each index from n to n + s, verify that the existing bufwad 1839fa9e4066Sahrens * in packobj matches the bufwads at the head and tail of the 1840fa9e4066Sahrens * corresponding chunk in bigobj. Then update all three bufwads 1841fa9e4066Sahrens * with the new values we want to write out. 1842fa9e4066Sahrens */ 1843fa9e4066Sahrens for (i = 0; i < s; i++) { 1844fa9e4066Sahrens /* LINTED */ 1845fa9e4066Sahrens pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t)); 1846fa9e4066Sahrens /* LINTED */ 1847fa9e4066Sahrens bigH = (bufwad_t *)((char *)bigbuf + i * dd.dd_chunk); 1848fa9e4066Sahrens /* LINTED */ 1849fa9e4066Sahrens bigT = (bufwad_t *)((char *)bigH + dd.dd_chunk) - 1; 1850fa9e4066Sahrens 1851fa9e4066Sahrens ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize); 1852fa9e4066Sahrens ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize); 1853fa9e4066Sahrens 1854fa9e4066Sahrens if (pack->bw_txg > txg) 1855fa9e4066Sahrens fatal(0, "future leak: got %llx, open txg is %llx", 1856fa9e4066Sahrens pack->bw_txg, txg); 1857fa9e4066Sahrens 1858fa9e4066Sahrens if (pack->bw_data != 0 && pack->bw_index != n + i) 1859fa9e4066Sahrens fatal(0, "wrong index: got %llx, wanted %llx+%llx", 1860fa9e4066Sahrens pack->bw_index, n, i); 1861fa9e4066Sahrens 1862fa9e4066Sahrens if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0) 1863fa9e4066Sahrens fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH); 1864fa9e4066Sahrens 1865fa9e4066Sahrens if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0) 1866fa9e4066Sahrens fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT); 1867fa9e4066Sahrens 1868fa9e4066Sahrens if (freeit) { 1869fa9e4066Sahrens bzero(pack, sizeof (bufwad_t)); 1870fa9e4066Sahrens } else { 1871fa9e4066Sahrens pack->bw_index = n + i; 1872fa9e4066Sahrens pack->bw_txg = txg; 1873fa9e4066Sahrens pack->bw_data = 1 + ztest_random(-2ULL); 1874fa9e4066Sahrens } 1875fa9e4066Sahrens *bigH = *pack; 1876fa9e4066Sahrens *bigT = *pack; 1877fa9e4066Sahrens } 1878fa9e4066Sahrens 1879fa9e4066Sahrens /* 1880fa9e4066Sahrens * We've verified all the old bufwads, and made new ones. 1881fa9e4066Sahrens * Now write them out. 1882fa9e4066Sahrens */ 1883fa9e4066Sahrens dmu_write(os, dd.dd_packobj, packoff, packsize, packbuf, tx); 1884fa9e4066Sahrens 1885fa9e4066Sahrens if (freeit) { 1886fa9e4066Sahrens if (zopt_verbose >= 6) { 1887fa9e4066Sahrens (void) printf("freeing offset %llx size %llx" 1888fa9e4066Sahrens " txg %llx\n", 1889fa9e4066Sahrens (u_longlong_t)bigoff, 1890fa9e4066Sahrens (u_longlong_t)bigsize, 1891fa9e4066Sahrens (u_longlong_t)txg); 1892fa9e4066Sahrens } 1893ea8dc4b6Seschrock VERIFY(0 == dmu_free_range(os, dd.dd_bigobj, bigoff, 1894ea8dc4b6Seschrock bigsize, tx)); 1895fa9e4066Sahrens } else { 1896fa9e4066Sahrens if (zopt_verbose >= 6) { 1897fa9e4066Sahrens (void) printf("writing offset %llx size %llx" 1898fa9e4066Sahrens " txg %llx\n", 1899fa9e4066Sahrens (u_longlong_t)bigoff, 1900fa9e4066Sahrens (u_longlong_t)bigsize, 1901fa9e4066Sahrens (u_longlong_t)txg); 1902fa9e4066Sahrens } 1903fa9e4066Sahrens dmu_write(os, dd.dd_bigobj, bigoff, bigsize, bigbuf, tx); 1904fa9e4066Sahrens } 1905fa9e4066Sahrens 1906fa9e4066Sahrens dmu_tx_commit(tx); 1907fa9e4066Sahrens 1908fa9e4066Sahrens /* 1909fa9e4066Sahrens * Sanity check the stuff we just wrote. 1910fa9e4066Sahrens */ 1911fa9e4066Sahrens { 1912fa9e4066Sahrens void *packcheck = umem_alloc(packsize, UMEM_NOFAIL); 1913fa9e4066Sahrens void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL); 1914fa9e4066Sahrens 1915ea8dc4b6Seschrock VERIFY(0 == dmu_read(os, dd.dd_packobj, packoff, 1916ea8dc4b6Seschrock packsize, packcheck)); 1917ea8dc4b6Seschrock VERIFY(0 == dmu_read(os, dd.dd_bigobj, bigoff, 1918ea8dc4b6Seschrock bigsize, bigcheck)); 1919fa9e4066Sahrens 1920fa9e4066Sahrens ASSERT(bcmp(packbuf, packcheck, packsize) == 0); 1921fa9e4066Sahrens ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0); 1922fa9e4066Sahrens 1923fa9e4066Sahrens umem_free(packcheck, packsize); 1924fa9e4066Sahrens umem_free(bigcheck, bigsize); 1925fa9e4066Sahrens } 1926fa9e4066Sahrens 1927fa9e4066Sahrens umem_free(packbuf, packsize); 1928fa9e4066Sahrens umem_free(bigbuf, bigsize); 1929fa9e4066Sahrens } 1930fa9e4066Sahrens 1931fa9e4066Sahrens void 19324d31c452Smaybee ztest_dmu_check_future_leak(objset_t *os, uint64_t txg) 19334d31c452Smaybee { 19344d31c452Smaybee dmu_buf_t *db; 19354d31c452Smaybee ztest_block_tag_t rbt; 19364d31c452Smaybee 19374d31c452Smaybee if (zopt_verbose >= 3) { 19384d31c452Smaybee char osname[MAXNAMELEN]; 19394d31c452Smaybee dmu_objset_name(os, osname); 19404d31c452Smaybee (void) printf("checking %s for future leaks in txg %lld...\n", 19414d31c452Smaybee osname, (u_longlong_t)txg); 19424d31c452Smaybee } 19434d31c452Smaybee 19444d31c452Smaybee /* 19454d31c452Smaybee * Make sure that, if there is a write record in the bonus buffer 19464d31c452Smaybee * of the ZTEST_DIROBJ, that the txg for this record is <= the 19474d31c452Smaybee * last synced txg of the pool. 19484d31c452Smaybee */ 19494d31c452Smaybee 19504d31c452Smaybee VERIFY(0 == dmu_bonus_hold(os, ZTEST_DIROBJ, FTAG, &db)); 19514d31c452Smaybee ASSERT3U(db->db_size, ==, sizeof (rbt)); 19524d31c452Smaybee bcopy(db->db_data, &rbt, db->db_size); 19534d31c452Smaybee if (rbt.bt_objset != 0) { 19544d31c452Smaybee ASSERT3U(rbt.bt_objset, ==, dmu_objset_id(os)); 19554d31c452Smaybee ASSERT3U(rbt.bt_object, ==, ZTEST_DIROBJ); 19564d31c452Smaybee ASSERT3U(rbt.bt_offset, ==, -1ULL); 19574d31c452Smaybee if (rbt.bt_txg > txg) { 19584d31c452Smaybee fatal(0, 19594d31c452Smaybee "future leak: got %llx, last synced txg is %llx", 19604d31c452Smaybee rbt.bt_txg, txg); 19614d31c452Smaybee } 19624d31c452Smaybee } 19634d31c452Smaybee dmu_buf_rele(db, FTAG); 19644d31c452Smaybee } 19654d31c452Smaybee 19664d31c452Smaybee void 1967fa9e4066Sahrens ztest_dmu_write_parallel(ztest_args_t *za) 1968fa9e4066Sahrens { 1969fa9e4066Sahrens objset_t *os = za->za_os; 1970fa9e4066Sahrens dmu_tx_t *tx; 1971fa9e4066Sahrens dmu_buf_t *db; 1972fa9e4066Sahrens int i, b, error, do_free, bs; 1973fa9e4066Sahrens uint64_t off, txg_how, txg; 1974fa9e4066Sahrens mutex_t *lp; 1975fa9e4066Sahrens char osname[MAXNAMELEN]; 1976fa9e4066Sahrens char iobuf[SPA_MAXBLOCKSIZE]; 1977fa9e4066Sahrens ztest_block_tag_t rbt, wbt; 1978fa9e4066Sahrens 1979fa9e4066Sahrens dmu_objset_name(os, osname); 1980fa9e4066Sahrens bs = ZTEST_DIROBJ_BLOCKSIZE; 1981fa9e4066Sahrens 1982fa9e4066Sahrens /* 1983fa9e4066Sahrens * Have multiple threads write to large offsets in ZTEST_DIROBJ 1984fa9e4066Sahrens * to verify that having multiple threads writing to the same object 1985fa9e4066Sahrens * in parallel doesn't cause any trouble. 1986fa9e4066Sahrens * Also do parallel writes to the bonus buffer on occasion. 1987fa9e4066Sahrens */ 1988fa9e4066Sahrens for (i = 0; i < 50; i++) { 1989fa9e4066Sahrens b = ztest_random(ZTEST_SYNC_LOCKS); 1990fa9e4066Sahrens lp = &ztest_shared->zs_sync_lock[b]; 1991fa9e4066Sahrens 1992fa9e4066Sahrens do_free = (ztest_random(4) == 0); 1993fa9e4066Sahrens 1994fa9e4066Sahrens off = za->za_diroff_shared + ((uint64_t)b << SPA_MAXBLOCKSHIFT); 1995fa9e4066Sahrens 1996fa9e4066Sahrens if (ztest_random(4) == 0) { 1997fa9e4066Sahrens /* 1998fa9e4066Sahrens * Do the bonus buffer instead of a regular block. 1999fa9e4066Sahrens */ 2000fa9e4066Sahrens do_free = 0; 2001fa9e4066Sahrens off = -1ULL; 2002fa9e4066Sahrens } 2003fa9e4066Sahrens 2004fa9e4066Sahrens tx = dmu_tx_create(os); 2005fa9e4066Sahrens 2006fa9e4066Sahrens if (off == -1ULL) 2007fa9e4066Sahrens dmu_tx_hold_bonus(tx, ZTEST_DIROBJ); 2008fa9e4066Sahrens else if (do_free) 2009fa9e4066Sahrens dmu_tx_hold_free(tx, ZTEST_DIROBJ, off, bs); 2010fa9e4066Sahrens else 2011fa9e4066Sahrens dmu_tx_hold_write(tx, ZTEST_DIROBJ, off, bs); 2012fa9e4066Sahrens 2013fa9e4066Sahrens txg_how = ztest_random(2) == 0 ? TXG_WAIT : TXG_NOWAIT; 2014fa9e4066Sahrens error = dmu_tx_assign(tx, txg_how); 2015fa9e4066Sahrens if (error) { 2016fa9e4066Sahrens if (error == ERESTART) { 2017fa9e4066Sahrens ASSERT(txg_how == TXG_NOWAIT); 20188a2f1b91Sahrens dmu_tx_wait(tx); 20198a2f1b91Sahrens dmu_tx_abort(tx); 2020fa9e4066Sahrens continue; 2021fa9e4066Sahrens } 20228a2f1b91Sahrens dmu_tx_abort(tx); 2023fa9e4066Sahrens ztest_record_enospc("dmu write parallel"); 2024fa9e4066Sahrens return; 2025fa9e4066Sahrens } 2026fa9e4066Sahrens txg = dmu_tx_get_txg(tx); 2027fa9e4066Sahrens 2028fa9e4066Sahrens if (do_free) { 2029fa9e4066Sahrens (void) mutex_lock(lp); 2030ea8dc4b6Seschrock VERIFY(0 == dmu_free_range(os, ZTEST_DIROBJ, off, 2031ea8dc4b6Seschrock bs, tx)); 2032fa9e4066Sahrens (void) mutex_unlock(lp); 2033fa9e4066Sahrens dmu_tx_commit(tx); 2034fa9e4066Sahrens continue; 2035fa9e4066Sahrens } 2036fa9e4066Sahrens 2037fa9e4066Sahrens wbt.bt_objset = dmu_objset_id(os); 2038fa9e4066Sahrens wbt.bt_object = ZTEST_DIROBJ; 2039fa9e4066Sahrens wbt.bt_offset = off; 2040fa9e4066Sahrens wbt.bt_txg = txg; 2041fa9e4066Sahrens wbt.bt_thread = za->za_instance; 2042fa9e4066Sahrens 2043fa9e4066Sahrens if (off == -1ULL) { 2044fa9e4066Sahrens wbt.bt_seq = 0; 2045ea8dc4b6Seschrock VERIFY(0 == dmu_bonus_hold(os, ZTEST_DIROBJ, 2046ea8dc4b6Seschrock FTAG, &db)); 2047fa9e4066Sahrens ASSERT3U(db->db_size, ==, sizeof (wbt)); 2048fa9e4066Sahrens bcopy(db->db_data, &rbt, db->db_size); 2049fa9e4066Sahrens if (rbt.bt_objset != 0) { 2050fa9e4066Sahrens ASSERT3U(rbt.bt_objset, ==, wbt.bt_objset); 2051fa9e4066Sahrens ASSERT3U(rbt.bt_object, ==, wbt.bt_object); 2052fa9e4066Sahrens ASSERT3U(rbt.bt_offset, ==, wbt.bt_offset); 2053fa9e4066Sahrens ASSERT3U(rbt.bt_txg, <=, wbt.bt_txg); 2054fa9e4066Sahrens } 2055fa9e4066Sahrens dmu_buf_will_dirty(db, tx); 2056fa9e4066Sahrens bcopy(&wbt, db->db_data, db->db_size); 2057ea8dc4b6Seschrock dmu_buf_rele(db, FTAG); 2058fa9e4066Sahrens dmu_tx_commit(tx); 2059fa9e4066Sahrens continue; 2060fa9e4066Sahrens } 2061fa9e4066Sahrens 2062fa9e4066Sahrens (void) mutex_lock(lp); 2063fa9e4066Sahrens 2064fa9e4066Sahrens wbt.bt_seq = ztest_shared->zs_seq[b]++; 2065fa9e4066Sahrens 2066fa9e4066Sahrens dmu_write(os, ZTEST_DIROBJ, off, sizeof (wbt), &wbt, tx); 2067fa9e4066Sahrens 2068fa9e4066Sahrens (void) mutex_unlock(lp); 2069fa9e4066Sahrens 2070fa9e4066Sahrens if (ztest_random(100) == 0) 2071fa9e4066Sahrens (void) poll(NULL, 0, 1); /* open dn_notxholds window */ 2072fa9e4066Sahrens 2073fa9e4066Sahrens dmu_tx_commit(tx); 2074fa9e4066Sahrens 2075fa9e4066Sahrens if (ztest_random(1000) == 0) 2076fa9e4066Sahrens txg_wait_synced(dmu_objset_pool(os), txg); 2077fa9e4066Sahrens 2078fa9e4066Sahrens if (ztest_random(2) == 0) { 2079fa9e4066Sahrens blkptr_t blk = { 0 }; 2080fa9e4066Sahrens uint64_t blkoff; 2081ea8dc4b6Seschrock zbookmark_t zb; 2082fa9e4066Sahrens 2083fa9e4066Sahrens (void) mutex_lock(lp); 2084c5c6ffa0Smaybee blkoff = P2ALIGN_TYPED(off, bs, uint64_t); 2085c5c6ffa0Smaybee error = dmu_buf_hold(os, 2086c5c6ffa0Smaybee ZTEST_DIROBJ, blkoff, FTAG, &db); 2087c5c6ffa0Smaybee if (error) { 2088c5c6ffa0Smaybee dprintf("dmu_buf_hold(%s, %d, %llx) = %d\n", 2089c5c6ffa0Smaybee osname, ZTEST_DIROBJ, blkoff, error); 2090c5c6ffa0Smaybee (void) mutex_unlock(lp); 2091c5c6ffa0Smaybee continue; 2092c5c6ffa0Smaybee } 2093c5c6ffa0Smaybee blkoff = off - blkoff; 2094c5c6ffa0Smaybee error = dmu_sync(NULL, db, &blk, txg, NULL, NULL); 2095c5c6ffa0Smaybee dmu_buf_rele(db, FTAG); 2096fa9e4066Sahrens (void) mutex_unlock(lp); 2097fa9e4066Sahrens if (error) { 2098fa9e4066Sahrens dprintf("dmu_sync(%s, %d, %llx) = %d\n", 2099fa9e4066Sahrens osname, ZTEST_DIROBJ, off, error); 2100fa9e4066Sahrens continue; 2101fa9e4066Sahrens } 2102fa9e4066Sahrens 2103fa9e4066Sahrens if (blk.blk_birth == 0) { /* concurrent free */ 2104fa9e4066Sahrens continue; 2105fa9e4066Sahrens } 2106c5c6ffa0Smaybee txg_suspend(dmu_objset_pool(os)); 2107fa9e4066Sahrens 2108fa9e4066Sahrens ASSERT(blk.blk_fill == 1); 2109fa9e4066Sahrens ASSERT3U(BP_GET_TYPE(&blk), ==, DMU_OT_UINT64_OTHER); 2110fa9e4066Sahrens ASSERT3U(BP_GET_LEVEL(&blk), ==, 0); 2111fa9e4066Sahrens ASSERT3U(BP_GET_LSIZE(&blk), ==, bs); 2112fa9e4066Sahrens 2113fa9e4066Sahrens /* 2114fa9e4066Sahrens * Read the block that dmu_sync() returned to 2115fa9e4066Sahrens * make sure its contents match what we wrote. 2116fa9e4066Sahrens * We do this while still txg_suspend()ed to ensure 2117fa9e4066Sahrens * that the block can't be reused before we read it. 2118fa9e4066Sahrens */ 2119ea8dc4b6Seschrock zb.zb_objset = dmu_objset_id(os); 2120ea8dc4b6Seschrock zb.zb_object = ZTEST_DIROBJ; 2121ea8dc4b6Seschrock zb.zb_level = 0; 2122ea8dc4b6Seschrock zb.zb_blkid = off / bs; 2123fa9e4066Sahrens error = zio_wait(zio_read(NULL, dmu_objset_spa(os), 2124fa9e4066Sahrens &blk, iobuf, bs, NULL, NULL, 2125ea8dc4b6Seschrock ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_MUSTSUCCEED, &zb)); 2126fa9e4066Sahrens ASSERT(error == 0); 2127fa9e4066Sahrens 2128fa9e4066Sahrens txg_resume(dmu_objset_pool(os)); 2129fa9e4066Sahrens 2130fa9e4066Sahrens bcopy(&iobuf[blkoff], &rbt, sizeof (rbt)); 2131fa9e4066Sahrens 21320e34b6a7Sbonwick if (rbt.bt_objset == 0) /* concurrent free */ 21330e34b6a7Sbonwick continue; 21340e34b6a7Sbonwick 2135fa9e4066Sahrens ASSERT3U(rbt.bt_objset, ==, wbt.bt_objset); 2136fa9e4066Sahrens ASSERT3U(rbt.bt_object, ==, wbt.bt_object); 2137fa9e4066Sahrens ASSERT3U(rbt.bt_offset, ==, wbt.bt_offset); 2138fa9e4066Sahrens 2139fa9e4066Sahrens /* 2140fa9e4066Sahrens * The semantic of dmu_sync() is that we always 2141fa9e4066Sahrens * push the most recent version of the data, 2142fa9e4066Sahrens * so in the face of concurrent updates we may 2143fa9e4066Sahrens * see a newer version of the block. That's OK. 2144fa9e4066Sahrens */ 2145fa9e4066Sahrens ASSERT3U(rbt.bt_txg, >=, wbt.bt_txg); 2146fa9e4066Sahrens if (rbt.bt_thread == wbt.bt_thread) 2147fa9e4066Sahrens ASSERT3U(rbt.bt_seq, ==, wbt.bt_seq); 2148fa9e4066Sahrens else 2149fa9e4066Sahrens ASSERT3U(rbt.bt_seq, >, wbt.bt_seq); 2150fa9e4066Sahrens } 2151fa9e4066Sahrens } 2152fa9e4066Sahrens } 2153fa9e4066Sahrens 2154fa9e4066Sahrens /* 2155fa9e4066Sahrens * Verify that zap_{create,destroy,add,remove,update} work as expected. 2156fa9e4066Sahrens */ 2157fa9e4066Sahrens #define ZTEST_ZAP_MIN_INTS 1 2158fa9e4066Sahrens #define ZTEST_ZAP_MAX_INTS 4 2159fa9e4066Sahrens #define ZTEST_ZAP_MAX_PROPS 1000 2160fa9e4066Sahrens 2161fa9e4066Sahrens void 2162fa9e4066Sahrens ztest_zap(ztest_args_t *za) 2163fa9e4066Sahrens { 2164fa9e4066Sahrens objset_t *os = za->za_os; 2165fa9e4066Sahrens uint64_t object; 2166fa9e4066Sahrens uint64_t txg, last_txg; 2167fa9e4066Sahrens uint64_t value[ZTEST_ZAP_MAX_INTS]; 2168fa9e4066Sahrens uint64_t zl_ints, zl_intsize, prop; 2169fa9e4066Sahrens int i, ints; 2170fa9e4066Sahrens int iters = 100; 2171fa9e4066Sahrens dmu_tx_t *tx; 2172fa9e4066Sahrens char propname[100], txgname[100]; 2173fa9e4066Sahrens int error; 2174fa9e4066Sahrens char osname[MAXNAMELEN]; 2175fa9e4066Sahrens char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" }; 2176fa9e4066Sahrens 2177fa9e4066Sahrens dmu_objset_name(os, osname); 2178fa9e4066Sahrens 2179fa9e4066Sahrens /* 2180fa9e4066Sahrens * Create a new object if necessary, and record it in the directory. 2181fa9e4066Sahrens */ 2182ea8dc4b6Seschrock VERIFY(0 == dmu_read(os, ZTEST_DIROBJ, za->za_diroff, 2183ea8dc4b6Seschrock sizeof (uint64_t), &object)); 2184fa9e4066Sahrens 2185fa9e4066Sahrens if (object == 0) { 2186fa9e4066Sahrens tx = dmu_tx_create(os); 2187fa9e4066Sahrens dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, 2188fa9e4066Sahrens sizeof (uint64_t)); 2189ea8dc4b6Seschrock dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, NULL); 2190fa9e4066Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 2191fa9e4066Sahrens if (error) { 2192fa9e4066Sahrens ztest_record_enospc("create zap test obj"); 2193fa9e4066Sahrens dmu_tx_abort(tx); 2194fa9e4066Sahrens return; 2195fa9e4066Sahrens } 2196fa9e4066Sahrens object = zap_create(os, DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx); 2197fa9e4066Sahrens if (error) { 2198fa9e4066Sahrens fatal(0, "zap_create('%s', %llu) = %d", 2199fa9e4066Sahrens osname, object, error); 2200fa9e4066Sahrens } 2201fa9e4066Sahrens ASSERT(object != 0); 2202fa9e4066Sahrens dmu_write(os, ZTEST_DIROBJ, za->za_diroff, 2203fa9e4066Sahrens sizeof (uint64_t), &object, tx); 2204fa9e4066Sahrens /* 2205fa9e4066Sahrens * Generate a known hash collision, and verify that 2206fa9e4066Sahrens * we can lookup and remove both entries. 2207fa9e4066Sahrens */ 2208fa9e4066Sahrens for (i = 0; i < 2; i++) { 2209fa9e4066Sahrens value[i] = i; 2210fa9e4066Sahrens error = zap_add(os, object, hc[i], sizeof (uint64_t), 2211fa9e4066Sahrens 1, &value[i], tx); 2212fa9e4066Sahrens ASSERT3U(error, ==, 0); 2213fa9e4066Sahrens } 2214fa9e4066Sahrens for (i = 0; i < 2; i++) { 2215fa9e4066Sahrens error = zap_add(os, object, hc[i], sizeof (uint64_t), 2216fa9e4066Sahrens 1, &value[i], tx); 2217fa9e4066Sahrens ASSERT3U(error, ==, EEXIST); 2218fa9e4066Sahrens error = zap_length(os, object, hc[i], 2219fa9e4066Sahrens &zl_intsize, &zl_ints); 2220fa9e4066Sahrens ASSERT3U(error, ==, 0); 2221fa9e4066Sahrens ASSERT3U(zl_intsize, ==, sizeof (uint64_t)); 2222fa9e4066Sahrens ASSERT3U(zl_ints, ==, 1); 2223fa9e4066Sahrens } 2224fa9e4066Sahrens for (i = 0; i < 2; i++) { 2225fa9e4066Sahrens error = zap_remove(os, object, hc[i], tx); 2226fa9e4066Sahrens ASSERT3U(error, ==, 0); 2227fa9e4066Sahrens } 2228fa9e4066Sahrens 2229fa9e4066Sahrens dmu_tx_commit(tx); 2230fa9e4066Sahrens } 2231fa9e4066Sahrens 2232fa9e4066Sahrens ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS); 2233fa9e4066Sahrens 2234fa9e4066Sahrens while (--iters >= 0) { 2235fa9e4066Sahrens prop = ztest_random(ZTEST_ZAP_MAX_PROPS); 2236fa9e4066Sahrens (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop); 2237fa9e4066Sahrens (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop); 2238fa9e4066Sahrens bzero(value, sizeof (value)); 2239fa9e4066Sahrens last_txg = 0; 2240fa9e4066Sahrens 2241fa9e4066Sahrens /* 2242fa9e4066Sahrens * If these zap entries already exist, validate their contents. 2243fa9e4066Sahrens */ 2244fa9e4066Sahrens error = zap_length(os, object, txgname, &zl_intsize, &zl_ints); 2245fa9e4066Sahrens if (error == 0) { 2246fa9e4066Sahrens ASSERT3U(zl_intsize, ==, sizeof (uint64_t)); 2247fa9e4066Sahrens ASSERT3U(zl_ints, ==, 1); 2248fa9e4066Sahrens 2249fa9e4066Sahrens error = zap_lookup(os, object, txgname, zl_intsize, 2250fa9e4066Sahrens zl_ints, &last_txg); 2251fa9e4066Sahrens 2252fa9e4066Sahrens ASSERT3U(error, ==, 0); 2253fa9e4066Sahrens 2254fa9e4066Sahrens error = zap_length(os, object, propname, &zl_intsize, 2255fa9e4066Sahrens &zl_ints); 2256fa9e4066Sahrens 2257fa9e4066Sahrens ASSERT3U(error, ==, 0); 2258fa9e4066Sahrens ASSERT3U(zl_intsize, ==, sizeof (uint64_t)); 2259fa9e4066Sahrens ASSERT3U(zl_ints, ==, ints); 2260fa9e4066Sahrens 2261fa9e4066Sahrens error = zap_lookup(os, object, propname, zl_intsize, 2262fa9e4066Sahrens zl_ints, value); 2263fa9e4066Sahrens 2264fa9e4066Sahrens ASSERT3U(error, ==, 0); 2265fa9e4066Sahrens 2266fa9e4066Sahrens for (i = 0; i < ints; i++) { 2267fa9e4066Sahrens ASSERT3U(value[i], ==, last_txg + object + i); 2268fa9e4066Sahrens } 2269fa9e4066Sahrens } else { 2270fa9e4066Sahrens ASSERT3U(error, ==, ENOENT); 2271fa9e4066Sahrens } 2272fa9e4066Sahrens 2273fa9e4066Sahrens /* 2274fa9e4066Sahrens * Atomically update two entries in our zap object. 2275fa9e4066Sahrens * The first is named txg_%llu, and contains the txg 2276fa9e4066Sahrens * in which the property was last updated. The second 2277fa9e4066Sahrens * is named prop_%llu, and the nth element of its value 2278fa9e4066Sahrens * should be txg + object + n. 2279fa9e4066Sahrens */ 2280fa9e4066Sahrens tx = dmu_tx_create(os); 2281ea8dc4b6Seschrock dmu_tx_hold_zap(tx, object, TRUE, NULL); 2282fa9e4066Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 2283fa9e4066Sahrens if (error) { 2284fa9e4066Sahrens ztest_record_enospc("create zap entry"); 2285fa9e4066Sahrens dmu_tx_abort(tx); 2286fa9e4066Sahrens return; 2287fa9e4066Sahrens } 2288fa9e4066Sahrens txg = dmu_tx_get_txg(tx); 2289fa9e4066Sahrens 2290fa9e4066Sahrens if (last_txg > txg) 2291fa9e4066Sahrens fatal(0, "zap future leak: old %llu new %llu", 2292fa9e4066Sahrens last_txg, txg); 2293fa9e4066Sahrens 2294fa9e4066Sahrens for (i = 0; i < ints; i++) 2295fa9e4066Sahrens value[i] = txg + object + i; 2296fa9e4066Sahrens 2297fa9e4066Sahrens error = zap_update(os, object, txgname, sizeof (uint64_t), 2298fa9e4066Sahrens 1, &txg, tx); 2299fa9e4066Sahrens if (error) 2300fa9e4066Sahrens fatal(0, "zap_update('%s', %llu, '%s') = %d", 2301fa9e4066Sahrens osname, object, txgname, error); 2302fa9e4066Sahrens 2303fa9e4066Sahrens error = zap_update(os, object, propname, sizeof (uint64_t), 2304fa9e4066Sahrens ints, value, tx); 2305fa9e4066Sahrens if (error) 2306fa9e4066Sahrens fatal(0, "zap_update('%s', %llu, '%s') = %d", 2307fa9e4066Sahrens osname, object, propname, error); 2308fa9e4066Sahrens 2309fa9e4066Sahrens dmu_tx_commit(tx); 2310fa9e4066Sahrens 2311fa9e4066Sahrens /* 2312fa9e4066Sahrens * Remove a random pair of entries. 2313fa9e4066Sahrens */ 2314fa9e4066Sahrens prop = ztest_random(ZTEST_ZAP_MAX_PROPS); 2315fa9e4066Sahrens (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop); 2316fa9e4066Sahrens (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop); 2317fa9e4066Sahrens 2318fa9e4066Sahrens error = zap_length(os, object, txgname, &zl_intsize, &zl_ints); 2319fa9e4066Sahrens 2320fa9e4066Sahrens if (error == ENOENT) 2321fa9e4066Sahrens continue; 2322fa9e4066Sahrens 2323fa9e4066Sahrens ASSERT3U(error, ==, 0); 2324fa9e4066Sahrens 2325fa9e4066Sahrens tx = dmu_tx_create(os); 2326ea8dc4b6Seschrock dmu_tx_hold_zap(tx, object, TRUE, NULL); 2327fa9e4066Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 2328fa9e4066Sahrens if (error) { 2329fa9e4066Sahrens ztest_record_enospc("remove zap entry"); 2330fa9e4066Sahrens dmu_tx_abort(tx); 2331fa9e4066Sahrens return; 2332fa9e4066Sahrens } 2333fa9e4066Sahrens error = zap_remove(os, object, txgname, tx); 2334fa9e4066Sahrens if (error) 2335fa9e4066Sahrens fatal(0, "zap_remove('%s', %llu, '%s') = %d", 2336fa9e4066Sahrens osname, object, txgname, error); 2337fa9e4066Sahrens 2338fa9e4066Sahrens error = zap_remove(os, object, propname, tx); 2339fa9e4066Sahrens if (error) 2340fa9e4066Sahrens fatal(0, "zap_remove('%s', %llu, '%s') = %d", 2341fa9e4066Sahrens osname, object, propname, error); 2342fa9e4066Sahrens 2343fa9e4066Sahrens dmu_tx_commit(tx); 2344fa9e4066Sahrens } 2345fa9e4066Sahrens 2346fa9e4066Sahrens /* 2347fa9e4066Sahrens * Once in a while, destroy the object. 2348fa9e4066Sahrens */ 2349fa9e4066Sahrens if (ztest_random(100) != 0) 2350fa9e4066Sahrens return; 2351fa9e4066Sahrens 2352fa9e4066Sahrens tx = dmu_tx_create(os); 2353fa9e4066Sahrens dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, sizeof (uint64_t)); 2354fa9e4066Sahrens dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END); 2355fa9e4066Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 2356fa9e4066Sahrens if (error) { 2357fa9e4066Sahrens ztest_record_enospc("destroy zap object"); 2358fa9e4066Sahrens dmu_tx_abort(tx); 2359fa9e4066Sahrens return; 2360fa9e4066Sahrens } 2361fa9e4066Sahrens error = zap_destroy(os, object, tx); 2362fa9e4066Sahrens if (error) 2363fa9e4066Sahrens fatal(0, "zap_destroy('%s', %llu) = %d", 2364fa9e4066Sahrens osname, object, error); 2365fa9e4066Sahrens object = 0; 2366fa9e4066Sahrens dmu_write(os, ZTEST_DIROBJ, za->za_diroff, sizeof (uint64_t), 2367fa9e4066Sahrens &object, tx); 2368fa9e4066Sahrens dmu_tx_commit(tx); 2369fa9e4066Sahrens } 2370fa9e4066Sahrens 2371fa9e4066Sahrens void 2372fa9e4066Sahrens ztest_zap_parallel(ztest_args_t *za) 2373fa9e4066Sahrens { 2374fa9e4066Sahrens objset_t *os = za->za_os; 2375fa9e4066Sahrens uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc; 2376fa9e4066Sahrens int iters = 100; 2377fa9e4066Sahrens dmu_tx_t *tx; 2378fa9e4066Sahrens int i, namelen, error; 2379fa9e4066Sahrens char name[20], string_value[20]; 2380fa9e4066Sahrens void *data; 2381fa9e4066Sahrens 2382fa9e4066Sahrens while (--iters >= 0) { 2383fa9e4066Sahrens /* 2384fa9e4066Sahrens * Generate a random name of the form 'xxx.....' where each 2385fa9e4066Sahrens * x is a random printable character and the dots are dots. 2386fa9e4066Sahrens * There are 94 such characters, and the name length goes from 2387fa9e4066Sahrens * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names. 2388fa9e4066Sahrens */ 2389fa9e4066Sahrens namelen = ztest_random(sizeof (name) - 5) + 5 + 1; 2390fa9e4066Sahrens 2391fa9e4066Sahrens for (i = 0; i < 3; i++) 2392fa9e4066Sahrens name[i] = '!' + ztest_random('~' - '!' + 1); 2393fa9e4066Sahrens for (; i < namelen - 1; i++) 2394fa9e4066Sahrens name[i] = '.'; 2395fa9e4066Sahrens name[i] = '\0'; 2396fa9e4066Sahrens 2397fa9e4066Sahrens if (ztest_random(2) == 0) 2398fa9e4066Sahrens object = ZTEST_MICROZAP_OBJ; 2399fa9e4066Sahrens else 2400fa9e4066Sahrens object = ZTEST_FATZAP_OBJ; 2401fa9e4066Sahrens 2402fa9e4066Sahrens if ((namelen & 1) || object == ZTEST_MICROZAP_OBJ) { 2403fa9e4066Sahrens wsize = sizeof (txg); 2404fa9e4066Sahrens wc = 1; 2405fa9e4066Sahrens data = &txg; 2406fa9e4066Sahrens } else { 2407fa9e4066Sahrens wsize = 1; 2408fa9e4066Sahrens wc = namelen; 2409fa9e4066Sahrens data = string_value; 2410fa9e4066Sahrens } 2411fa9e4066Sahrens 2412fa9e4066Sahrens count = -1ULL; 2413fa9e4066Sahrens VERIFY(zap_count(os, object, &count) == 0); 2414fa9e4066Sahrens ASSERT(count != -1ULL); 2415fa9e4066Sahrens 2416fa9e4066Sahrens /* 2417fa9e4066Sahrens * Select an operation: length, lookup, add, update, remove. 2418fa9e4066Sahrens */ 2419fa9e4066Sahrens i = ztest_random(5); 2420fa9e4066Sahrens 2421fa9e4066Sahrens if (i >= 2) { 2422fa9e4066Sahrens tx = dmu_tx_create(os); 2423ea8dc4b6Seschrock dmu_tx_hold_zap(tx, object, TRUE, NULL); 2424fa9e4066Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 2425fa9e4066Sahrens if (error) { 2426fa9e4066Sahrens ztest_record_enospc("zap parallel"); 2427fa9e4066Sahrens dmu_tx_abort(tx); 2428fa9e4066Sahrens return; 2429fa9e4066Sahrens } 2430fa9e4066Sahrens txg = dmu_tx_get_txg(tx); 2431fa9e4066Sahrens bcopy(name, string_value, namelen); 2432fa9e4066Sahrens } else { 2433fa9e4066Sahrens tx = NULL; 2434fa9e4066Sahrens txg = 0; 2435fa9e4066Sahrens bzero(string_value, namelen); 2436fa9e4066Sahrens } 2437fa9e4066Sahrens 2438fa9e4066Sahrens switch (i) { 2439fa9e4066Sahrens 2440fa9e4066Sahrens case 0: 2441fa9e4066Sahrens error = zap_length(os, object, name, &zl_wsize, &zl_wc); 2442fa9e4066Sahrens if (error == 0) { 2443fa9e4066Sahrens ASSERT3U(wsize, ==, zl_wsize); 2444fa9e4066Sahrens ASSERT3U(wc, ==, zl_wc); 2445fa9e4066Sahrens } else { 2446fa9e4066Sahrens ASSERT3U(error, ==, ENOENT); 2447fa9e4066Sahrens } 2448fa9e4066Sahrens break; 2449fa9e4066Sahrens 2450fa9e4066Sahrens case 1: 2451fa9e4066Sahrens error = zap_lookup(os, object, name, wsize, wc, data); 2452fa9e4066Sahrens if (error == 0) { 2453fa9e4066Sahrens if (data == string_value && 2454fa9e4066Sahrens bcmp(name, data, namelen) != 0) 2455fa9e4066Sahrens fatal(0, "name '%s' != val '%s' len %d", 2456fa9e4066Sahrens name, data, namelen); 2457fa9e4066Sahrens } else { 2458fa9e4066Sahrens ASSERT3U(error, ==, ENOENT); 2459fa9e4066Sahrens } 2460fa9e4066Sahrens break; 2461fa9e4066Sahrens 2462fa9e4066Sahrens case 2: 2463fa9e4066Sahrens error = zap_add(os, object, name, wsize, wc, data, tx); 2464fa9e4066Sahrens ASSERT(error == 0 || error == EEXIST); 2465fa9e4066Sahrens break; 2466fa9e4066Sahrens 2467fa9e4066Sahrens case 3: 2468fa9e4066Sahrens VERIFY(zap_update(os, object, name, wsize, wc, 2469fa9e4066Sahrens data, tx) == 0); 2470fa9e4066Sahrens break; 2471fa9e4066Sahrens 2472fa9e4066Sahrens case 4: 2473fa9e4066Sahrens error = zap_remove(os, object, name, tx); 2474fa9e4066Sahrens ASSERT(error == 0 || error == ENOENT); 2475fa9e4066Sahrens break; 2476fa9e4066Sahrens } 2477fa9e4066Sahrens 2478fa9e4066Sahrens if (tx != NULL) 2479fa9e4066Sahrens dmu_tx_commit(tx); 2480fa9e4066Sahrens } 2481fa9e4066Sahrens } 2482fa9e4066Sahrens 2483fa9e4066Sahrens void 2484fa9e4066Sahrens ztest_dsl_prop_get_set(ztest_args_t *za) 2485fa9e4066Sahrens { 2486fa9e4066Sahrens objset_t *os = za->za_os; 2487fa9e4066Sahrens int i, inherit; 2488fa9e4066Sahrens uint64_t value; 2489fa9e4066Sahrens const char *prop, *valname; 2490fa9e4066Sahrens char setpoint[MAXPATHLEN]; 2491fa9e4066Sahrens char osname[MAXNAMELEN]; 2492ea8dc4b6Seschrock int error; 2493fa9e4066Sahrens 2494fa9e4066Sahrens (void) rw_rdlock(&ztest_shared->zs_name_lock); 2495fa9e4066Sahrens 2496fa9e4066Sahrens dmu_objset_name(os, osname); 2497fa9e4066Sahrens 2498fa9e4066Sahrens for (i = 0; i < 2; i++) { 2499fa9e4066Sahrens if (i == 0) { 2500fa9e4066Sahrens prop = "checksum"; 2501fa9e4066Sahrens value = ztest_random_checksum(); 2502fa9e4066Sahrens inherit = (value == ZIO_CHECKSUM_INHERIT); 2503fa9e4066Sahrens } else { 2504fa9e4066Sahrens prop = "compression"; 2505fa9e4066Sahrens value = ztest_random_compress(); 2506fa9e4066Sahrens inherit = (value == ZIO_COMPRESS_INHERIT); 2507fa9e4066Sahrens } 2508fa9e4066Sahrens 2509ea8dc4b6Seschrock error = dsl_prop_set(osname, prop, sizeof (value), 2510ea8dc4b6Seschrock !inherit, &value); 2511ea8dc4b6Seschrock 2512ea8dc4b6Seschrock if (error == ENOSPC) { 2513ea8dc4b6Seschrock ztest_record_enospc("dsl_prop_set"); 2514ea8dc4b6Seschrock break; 2515ea8dc4b6Seschrock } 2516ea8dc4b6Seschrock 2517ea8dc4b6Seschrock ASSERT3U(error, ==, 0); 2518fa9e4066Sahrens 2519fa9e4066Sahrens VERIFY3U(dsl_prop_get(osname, prop, sizeof (value), 2520fa9e4066Sahrens 1, &value, setpoint), ==, 0); 2521fa9e4066Sahrens 2522fa9e4066Sahrens if (i == 0) 2523fa9e4066Sahrens valname = zio_checksum_table[value].ci_name; 2524fa9e4066Sahrens else 2525fa9e4066Sahrens valname = zio_compress_table[value].ci_name; 2526fa9e4066Sahrens 2527fa9e4066Sahrens if (zopt_verbose >= 6) { 2528fa9e4066Sahrens (void) printf("%s %s = %s for '%s'\n", 2529fa9e4066Sahrens osname, prop, valname, setpoint); 2530fa9e4066Sahrens } 2531fa9e4066Sahrens } 2532fa9e4066Sahrens 2533fa9e4066Sahrens (void) rw_unlock(&ztest_shared->zs_name_lock); 2534fa9e4066Sahrens } 2535fa9e4066Sahrens 2536ea8dc4b6Seschrock static void 2537ea8dc4b6Seschrock ztest_error_setup(vdev_t *vd, int mode, int mask, uint64_t arg) 2538ea8dc4b6Seschrock { 2539ea8dc4b6Seschrock int c; 2540ea8dc4b6Seschrock 2541ea8dc4b6Seschrock for (c = 0; c < vd->vdev_children; c++) 2542ea8dc4b6Seschrock ztest_error_setup(vd->vdev_child[c], mode, mask, arg); 2543ea8dc4b6Seschrock 2544ea8dc4b6Seschrock if (vd->vdev_path != NULL) { 2545ea8dc4b6Seschrock vd->vdev_fault_mode = mode; 2546ea8dc4b6Seschrock vd->vdev_fault_mask = mask; 2547ea8dc4b6Seschrock vd->vdev_fault_arg = arg; 2548ea8dc4b6Seschrock } 2549ea8dc4b6Seschrock } 2550ea8dc4b6Seschrock 2551fa9e4066Sahrens /* 2552fa9e4066Sahrens * Inject random faults into the on-disk data. 2553fa9e4066Sahrens */ 2554fa9e4066Sahrens void 2555fa9e4066Sahrens ztest_fault_inject(ztest_args_t *za) 2556fa9e4066Sahrens { 2557fa9e4066Sahrens int fd; 2558fa9e4066Sahrens uint64_t offset; 2559fa9e4066Sahrens uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz; 2560fa9e4066Sahrens uint64_t bad = 0x1990c0ffeedecade; 2561fa9e4066Sahrens uint64_t top, leaf; 2562fa9e4066Sahrens char path0[MAXPATHLEN]; 2563fa9e4066Sahrens char pathrand[MAXPATHLEN]; 2564fa9e4066Sahrens size_t fsize; 2565fa9e4066Sahrens spa_t *spa = dmu_objset_spa(za->za_os); 2566fa9e4066Sahrens int bshift = SPA_MAXBLOCKSHIFT + 2; /* don't scrog all labels */ 2567fa9e4066Sahrens int iters = 1000; 2568ea8dc4b6Seschrock vdev_t *vd0; 2569ea8dc4b6Seschrock uint64_t guid0 = 0; 2570ea8dc4b6Seschrock 2571ea8dc4b6Seschrock /* 2572ea8dc4b6Seschrock * We can't inject faults when we have no fault tolerance. 2573ea8dc4b6Seschrock */ 2574ea8dc4b6Seschrock if (zopt_maxfaults == 0) 2575ea8dc4b6Seschrock return; 2576ea8dc4b6Seschrock 2577ea8dc4b6Seschrock ASSERT(leaves >= 2); 2578fa9e4066Sahrens 2579fa9e4066Sahrens /* 2580fa9e4066Sahrens * Pick a random top-level vdev. 2581fa9e4066Sahrens */ 2582ea8dc4b6Seschrock spa_config_enter(spa, RW_READER, FTAG); 2583fa9e4066Sahrens top = ztest_random(spa->spa_root_vdev->vdev_children); 2584ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 2585fa9e4066Sahrens 2586fa9e4066Sahrens /* 2587fa9e4066Sahrens * Pick a random leaf. 2588fa9e4066Sahrens */ 2589fa9e4066Sahrens leaf = ztest_random(leaves); 2590fa9e4066Sahrens 2591fa9e4066Sahrens /* 2592ea8dc4b6Seschrock * Generate paths to the first two leaves in this top-level vdev, 2593fa9e4066Sahrens * and to the random leaf we selected. We'll induce transient 2594ea8dc4b6Seschrock * I/O errors and random online/offline activity on leaf 0, 2595fa9e4066Sahrens * and we'll write random garbage to the randomly chosen leaf. 2596fa9e4066Sahrens */ 2597fa9e4066Sahrens (void) snprintf(path0, sizeof (path0), 2598fa9e4066Sahrens ztest_dev_template, zopt_dir, zopt_pool, top * leaves + 0); 2599fa9e4066Sahrens (void) snprintf(pathrand, sizeof (pathrand), 2600fa9e4066Sahrens ztest_dev_template, zopt_dir, zopt_pool, top * leaves + leaf); 2601fa9e4066Sahrens 2602ea8dc4b6Seschrock dprintf("damaging %s and %s\n", path0, pathrand); 2603fa9e4066Sahrens 2604ea8dc4b6Seschrock spa_config_enter(spa, RW_READER, FTAG); 2605fa9e4066Sahrens 2606fa9e4066Sahrens /* 2607ea8dc4b6Seschrock * If we can tolerate two or more faults, make vd0 fail randomly. 2608fa9e4066Sahrens */ 2609ea8dc4b6Seschrock vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0); 2610ea8dc4b6Seschrock if (vd0 != NULL && zopt_maxfaults >= 2) { 2611ea8dc4b6Seschrock guid0 = vd0->vdev_guid; 2612ea8dc4b6Seschrock ztest_error_setup(vd0, VDEV_FAULT_COUNT, 2613ea8dc4b6Seschrock (1U << ZIO_TYPE_READ) | (1U << ZIO_TYPE_WRITE), 100); 2614ea8dc4b6Seschrock } 2615ea8dc4b6Seschrock 2616ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 2617ea8dc4b6Seschrock 2618ea8dc4b6Seschrock /* 2619ea8dc4b6Seschrock * If we can tolerate two or more faults, randomly online/offline vd0. 2620ea8dc4b6Seschrock */ 2621ea8dc4b6Seschrock if (zopt_maxfaults >= 2 && guid0 != 0) { 2622fa9e4066Sahrens if (ztest_random(10) < 6) 2623ea8dc4b6Seschrock (void) vdev_offline(spa, guid0, B_TRUE); 2624fa9e4066Sahrens else 26253d7072f8Seschrock (void) vdev_online(spa, guid0, B_FALSE, NULL); 2626fa9e4066Sahrens } 2627fa9e4066Sahrens 2628fa9e4066Sahrens /* 2629ea8dc4b6Seschrock * We have at least single-fault tolerance, so inject data corruption. 2630fa9e4066Sahrens */ 2631fa9e4066Sahrens fd = open(pathrand, O_RDWR); 2632fa9e4066Sahrens 2633fa9e4066Sahrens if (fd == -1) /* we hit a gap in the device namespace */ 2634fa9e4066Sahrens return; 2635fa9e4066Sahrens 2636fa9e4066Sahrens fsize = lseek(fd, 0, SEEK_END); 2637fa9e4066Sahrens 2638fa9e4066Sahrens while (--iters != 0) { 2639fa9e4066Sahrens offset = ztest_random(fsize / (leaves << bshift)) * 2640fa9e4066Sahrens (leaves << bshift) + (leaf << bshift) + 2641fa9e4066Sahrens (ztest_random(1ULL << (bshift - 1)) & -8ULL); 2642fa9e4066Sahrens 2643fa9e4066Sahrens if (offset >= fsize) 2644fa9e4066Sahrens continue; 2645fa9e4066Sahrens 2646fa9e4066Sahrens if (zopt_verbose >= 6) 2647fa9e4066Sahrens (void) printf("injecting bad word into %s," 2648fa9e4066Sahrens " offset 0x%llx\n", pathrand, (u_longlong_t)offset); 2649fa9e4066Sahrens 2650fa9e4066Sahrens if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad)) 2651fa9e4066Sahrens fatal(1, "can't inject bad word at 0x%llx in %s", 2652fa9e4066Sahrens offset, pathrand); 2653fa9e4066Sahrens } 2654fa9e4066Sahrens 2655fa9e4066Sahrens (void) close(fd); 2656fa9e4066Sahrens } 2657fa9e4066Sahrens 2658fa9e4066Sahrens /* 2659fa9e4066Sahrens * Scrub the pool. 2660fa9e4066Sahrens */ 2661fa9e4066Sahrens void 2662fa9e4066Sahrens ztest_scrub(ztest_args_t *za) 2663fa9e4066Sahrens { 2664fa9e4066Sahrens spa_t *spa = dmu_objset_spa(za->za_os); 2665fa9e4066Sahrens 2666*bb8b5132Sek110237 mutex_enter(&spa_namespace_lock); 2667fa9e4066Sahrens (void) spa_scrub(spa, POOL_SCRUB_EVERYTHING, B_FALSE); 2668*bb8b5132Sek110237 mutex_exit(&spa_namespace_lock); 2669fa9e4066Sahrens (void) poll(NULL, 0, 1000); /* wait a second, then force a restart */ 2670*bb8b5132Sek110237 mutex_enter(&spa_namespace_lock); 2671fa9e4066Sahrens (void) spa_scrub(spa, POOL_SCRUB_EVERYTHING, B_FALSE); 2672*bb8b5132Sek110237 mutex_exit(&spa_namespace_lock); 2673fa9e4066Sahrens } 2674fa9e4066Sahrens 2675fa9e4066Sahrens /* 2676fa9e4066Sahrens * Rename the pool to a different name and then rename it back. 2677fa9e4066Sahrens */ 2678fa9e4066Sahrens void 2679fa9e4066Sahrens ztest_spa_rename(ztest_args_t *za) 2680fa9e4066Sahrens { 2681fa9e4066Sahrens char *oldname, *newname; 2682fa9e4066Sahrens int error; 2683fa9e4066Sahrens spa_t *spa; 2684fa9e4066Sahrens 2685fa9e4066Sahrens (void) rw_wrlock(&ztest_shared->zs_name_lock); 2686fa9e4066Sahrens 2687fa9e4066Sahrens oldname = za->za_pool; 2688fa9e4066Sahrens newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL); 2689fa9e4066Sahrens (void) strcpy(newname, oldname); 2690fa9e4066Sahrens (void) strcat(newname, "_tmp"); 2691fa9e4066Sahrens 2692fa9e4066Sahrens /* 2693fa9e4066Sahrens * Do the rename 2694fa9e4066Sahrens */ 2695fa9e4066Sahrens error = spa_rename(oldname, newname); 2696fa9e4066Sahrens if (error) 2697fa9e4066Sahrens fatal(0, "spa_rename('%s', '%s') = %d", oldname, 2698fa9e4066Sahrens newname, error); 2699fa9e4066Sahrens 2700fa9e4066Sahrens /* 2701fa9e4066Sahrens * Try to open it under the old name, which shouldn't exist 2702fa9e4066Sahrens */ 2703fa9e4066Sahrens error = spa_open(oldname, &spa, FTAG); 2704fa9e4066Sahrens if (error != ENOENT) 2705fa9e4066Sahrens fatal(0, "spa_open('%s') = %d", oldname, error); 2706fa9e4066Sahrens 2707fa9e4066Sahrens /* 2708fa9e4066Sahrens * Open it under the new name and make sure it's still the same spa_t. 2709fa9e4066Sahrens */ 2710fa9e4066Sahrens error = spa_open(newname, &spa, FTAG); 2711fa9e4066Sahrens if (error != 0) 2712fa9e4066Sahrens fatal(0, "spa_open('%s') = %d", newname, error); 2713fa9e4066Sahrens 2714fa9e4066Sahrens ASSERT(spa == dmu_objset_spa(za->za_os)); 2715fa9e4066Sahrens spa_close(spa, FTAG); 2716fa9e4066Sahrens 2717fa9e4066Sahrens /* 2718fa9e4066Sahrens * Rename it back to the original 2719fa9e4066Sahrens */ 2720fa9e4066Sahrens error = spa_rename(newname, oldname); 2721fa9e4066Sahrens if (error) 2722fa9e4066Sahrens fatal(0, "spa_rename('%s', '%s') = %d", newname, 2723fa9e4066Sahrens oldname, error); 2724fa9e4066Sahrens 2725fa9e4066Sahrens /* 2726fa9e4066Sahrens * Make sure it can still be opened 2727fa9e4066Sahrens */ 2728fa9e4066Sahrens error = spa_open(oldname, &spa, FTAG); 2729fa9e4066Sahrens if (error != 0) 2730fa9e4066Sahrens fatal(0, "spa_open('%s') = %d", oldname, error); 2731fa9e4066Sahrens 2732fa9e4066Sahrens ASSERT(spa == dmu_objset_spa(za->za_os)); 2733fa9e4066Sahrens spa_close(spa, FTAG); 2734fa9e4066Sahrens 2735fa9e4066Sahrens umem_free(newname, strlen(newname) + 1); 2736fa9e4066Sahrens 2737fa9e4066Sahrens (void) rw_unlock(&ztest_shared->zs_name_lock); 2738fa9e4066Sahrens } 2739fa9e4066Sahrens 2740fa9e4066Sahrens 2741fa9e4066Sahrens /* 2742fa9e4066Sahrens * Completely obliterate one disk. 2743fa9e4066Sahrens */ 2744fa9e4066Sahrens static void 2745fa9e4066Sahrens ztest_obliterate_one_disk(uint64_t vdev) 2746fa9e4066Sahrens { 2747fa9e4066Sahrens int fd; 2748d80c45e0Sbonwick char dev_name[MAXPATHLEN], copy_name[MAXPATHLEN]; 2749fa9e4066Sahrens size_t fsize; 2750fa9e4066Sahrens 2751fa9e4066Sahrens if (zopt_maxfaults < 2) 2752fa9e4066Sahrens return; 2753fa9e4066Sahrens 2754fa9e4066Sahrens (void) sprintf(dev_name, ztest_dev_template, zopt_dir, zopt_pool, vdev); 2755d80c45e0Sbonwick (void) snprintf(copy_name, MAXPATHLEN, "%s.old", dev_name); 2756fa9e4066Sahrens 2757fa9e4066Sahrens fd = open(dev_name, O_RDWR); 2758fa9e4066Sahrens 2759fa9e4066Sahrens if (fd == -1) 2760fa9e4066Sahrens fatal(1, "can't open %s", dev_name); 2761fa9e4066Sahrens 2762fa9e4066Sahrens /* 2763fa9e4066Sahrens * Determine the size. 2764fa9e4066Sahrens */ 2765fa9e4066Sahrens fsize = lseek(fd, 0, SEEK_END); 2766d80c45e0Sbonwick 2767fa9e4066Sahrens (void) close(fd); 2768fa9e4066Sahrens 2769fa9e4066Sahrens /* 2770d80c45e0Sbonwick * Rename the old device to dev_name.old (useful for debugging). 2771fa9e4066Sahrens */ 2772d80c45e0Sbonwick VERIFY(rename(dev_name, copy_name) == 0); 2773fa9e4066Sahrens 2774fa9e4066Sahrens /* 2775fa9e4066Sahrens * Create a new one. 2776fa9e4066Sahrens */ 2777fa9e4066Sahrens VERIFY((fd = open(dev_name, O_RDWR | O_CREAT | O_TRUNC, 0666)) >= 0); 2778fa9e4066Sahrens VERIFY(ftruncate(fd, fsize) == 0); 2779fa9e4066Sahrens (void) close(fd); 2780fa9e4066Sahrens } 2781fa9e4066Sahrens 2782fa9e4066Sahrens static void 2783fa9e4066Sahrens ztest_replace_one_disk(spa_t *spa, uint64_t vdev) 2784fa9e4066Sahrens { 2785fa9e4066Sahrens char dev_name[MAXPATHLEN]; 2786fa9e4066Sahrens nvlist_t *file, *root; 2787fa9e4066Sahrens int error; 2788ea8dc4b6Seschrock uint64_t guid; 2789ecc2d604Sbonwick uint64_t ashift = ztest_get_ashift(); 2790ea8dc4b6Seschrock vdev_t *vd; 2791fa9e4066Sahrens 2792fa9e4066Sahrens (void) sprintf(dev_name, ztest_dev_template, zopt_dir, zopt_pool, vdev); 2793fa9e4066Sahrens 2794fa9e4066Sahrens /* 2795fa9e4066Sahrens * Build the nvlist describing dev_name. 2796fa9e4066Sahrens */ 2797fa9e4066Sahrens VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0); 2798fa9e4066Sahrens VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0); 2799fa9e4066Sahrens VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, dev_name) == 0); 2800ecc2d604Sbonwick VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0); 2801fa9e4066Sahrens 2802fa9e4066Sahrens VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0); 2803fa9e4066Sahrens VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0); 2804fa9e4066Sahrens VERIFY(nvlist_add_nvlist_array(root, ZPOOL_CONFIG_CHILDREN, 2805fa9e4066Sahrens &file, 1) == 0); 2806fa9e4066Sahrens 2807ea8dc4b6Seschrock spa_config_enter(spa, RW_READER, FTAG); 2808ea8dc4b6Seschrock if ((vd = vdev_lookup_by_path(spa->spa_root_vdev, dev_name)) == NULL) 2809ea8dc4b6Seschrock guid = 0; 2810ea8dc4b6Seschrock else 2811ea8dc4b6Seschrock guid = vd->vdev_guid; 2812ea8dc4b6Seschrock spa_config_exit(spa, FTAG); 2813ea8dc4b6Seschrock error = spa_vdev_attach(spa, guid, root, B_TRUE); 2814ecc2d604Sbonwick if (error != 0 && 2815ecc2d604Sbonwick error != EBUSY && 2816ecc2d604Sbonwick error != ENOTSUP && 2817ecc2d604Sbonwick error != ENODEV && 2818ecc2d604Sbonwick error != EDOM) 2819fa9e4066Sahrens fatal(0, "spa_vdev_attach(in-place) = %d", error); 2820fa9e4066Sahrens 2821fa9e4066Sahrens nvlist_free(file); 2822fa9e4066Sahrens nvlist_free(root); 2823fa9e4066Sahrens } 2824fa9e4066Sahrens 2825fa9e4066Sahrens static void 2826fa9e4066Sahrens ztest_verify_blocks(char *pool) 2827fa9e4066Sahrens { 2828fa9e4066Sahrens int status; 2829fa9e4066Sahrens char zdb[MAXPATHLEN + MAXNAMELEN + 20]; 2830fa9e4066Sahrens char zbuf[1024]; 2831fa9e4066Sahrens char *bin; 28328654d025Sperrin char *ztest; 28338654d025Sperrin char *isa; 28348654d025Sperrin int isalen; 2835fa9e4066Sahrens FILE *fp; 2836fa9e4066Sahrens 2837fa9e4066Sahrens (void) realpath(getexecname(), zdb); 2838fa9e4066Sahrens 2839fa9e4066Sahrens /* zdb lives in /usr/sbin, while ztest lives in /usr/bin */ 2840fa9e4066Sahrens bin = strstr(zdb, "/usr/bin/"); 28418654d025Sperrin ztest = strstr(bin, "/ztest"); 28428654d025Sperrin isa = bin + 8; 28438654d025Sperrin isalen = ztest - isa; 28448654d025Sperrin isa = strdup(isa); 2845fa9e4066Sahrens /* LINTED */ 28468654d025Sperrin (void) sprintf(bin, "/usr/sbin%.*s/zdb -bc%s%s -U -O %s %s", 28478654d025Sperrin isalen, 28488654d025Sperrin isa, 2849fa9e4066Sahrens zopt_verbose >= 3 ? "s" : "", 2850fa9e4066Sahrens zopt_verbose >= 4 ? "v" : "", 2851fa9e4066Sahrens ztest_random(2) == 0 ? "pre" : "post", pool); 28528654d025Sperrin free(isa); 2853fa9e4066Sahrens 2854fa9e4066Sahrens if (zopt_verbose >= 5) 2855fa9e4066Sahrens (void) printf("Executing %s\n", strstr(zdb, "zdb ")); 2856fa9e4066Sahrens 2857fa9e4066Sahrens fp = popen(zdb, "r"); 2858fa9e4066Sahrens 2859fa9e4066Sahrens while (fgets(zbuf, sizeof (zbuf), fp) != NULL) 2860fa9e4066Sahrens if (zopt_verbose >= 3) 2861fa9e4066Sahrens (void) printf("%s", zbuf); 2862fa9e4066Sahrens 2863fa9e4066Sahrens status = pclose(fp); 2864fa9e4066Sahrens 2865fa9e4066Sahrens if (status == 0) 2866fa9e4066Sahrens return; 2867fa9e4066Sahrens 2868fa9e4066Sahrens ztest_dump_core = 0; 2869fa9e4066Sahrens if (WIFEXITED(status)) 2870fa9e4066Sahrens fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status)); 2871fa9e4066Sahrens else 2872fa9e4066Sahrens fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status)); 2873fa9e4066Sahrens } 2874fa9e4066Sahrens 2875fa9e4066Sahrens static void 2876fa9e4066Sahrens ztest_walk_pool_directory(char *header) 2877fa9e4066Sahrens { 2878fa9e4066Sahrens spa_t *spa = NULL; 2879fa9e4066Sahrens 2880fa9e4066Sahrens if (zopt_verbose >= 6) 2881fa9e4066Sahrens (void) printf("%s\n", header); 2882fa9e4066Sahrens 2883fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 2884fa9e4066Sahrens while ((spa = spa_next(spa)) != NULL) 2885fa9e4066Sahrens if (zopt_verbose >= 6) 2886fa9e4066Sahrens (void) printf("\t%s\n", spa_name(spa)); 2887fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2888fa9e4066Sahrens } 2889fa9e4066Sahrens 2890fa9e4066Sahrens static void 2891fa9e4066Sahrens ztest_spa_import_export(char *oldname, char *newname) 2892fa9e4066Sahrens { 2893fa9e4066Sahrens nvlist_t *config; 2894fa9e4066Sahrens uint64_t pool_guid; 2895fa9e4066Sahrens spa_t *spa; 2896fa9e4066Sahrens int error; 2897fa9e4066Sahrens 2898fa9e4066Sahrens if (zopt_verbose >= 4) { 2899fa9e4066Sahrens (void) printf("import/export: old = %s, new = %s\n", 2900fa9e4066Sahrens oldname, newname); 2901fa9e4066Sahrens } 2902fa9e4066Sahrens 2903fa9e4066Sahrens /* 2904fa9e4066Sahrens * Clean up from previous runs. 2905fa9e4066Sahrens */ 2906fa9e4066Sahrens (void) spa_destroy(newname); 2907fa9e4066Sahrens 2908fa9e4066Sahrens /* 2909fa9e4066Sahrens * Get the pool's configuration and guid. 2910fa9e4066Sahrens */ 2911fa9e4066Sahrens error = spa_open(oldname, &spa, FTAG); 2912fa9e4066Sahrens if (error) 2913fa9e4066Sahrens fatal(0, "spa_open('%s') = %d", oldname, error); 2914fa9e4066Sahrens 2915fa9e4066Sahrens pool_guid = spa_guid(spa); 2916fa9e4066Sahrens spa_close(spa, FTAG); 2917fa9e4066Sahrens 2918fa9e4066Sahrens ztest_walk_pool_directory("pools before export"); 2919fa9e4066Sahrens 2920fa9e4066Sahrens /* 2921fa9e4066Sahrens * Export it. 2922fa9e4066Sahrens */ 292344cd46caSbillm error = spa_export(oldname, &config); 2924fa9e4066Sahrens if (error) 2925fa9e4066Sahrens fatal(0, "spa_export('%s') = %d", oldname, error); 2926fa9e4066Sahrens 2927fa9e4066Sahrens ztest_walk_pool_directory("pools after export"); 2928fa9e4066Sahrens 2929fa9e4066Sahrens /* 2930fa9e4066Sahrens * Import it under the new name. 2931fa9e4066Sahrens */ 2932fa9e4066Sahrens error = spa_import(newname, config, NULL); 2933fa9e4066Sahrens if (error) 2934fa9e4066Sahrens fatal(0, "spa_import('%s') = %d", newname, error); 2935fa9e4066Sahrens 2936fa9e4066Sahrens ztest_walk_pool_directory("pools after import"); 2937fa9e4066Sahrens 2938fa9e4066Sahrens /* 2939fa9e4066Sahrens * Try to import it again -- should fail with EEXIST. 2940fa9e4066Sahrens */ 2941fa9e4066Sahrens error = spa_import(newname, config, NULL); 2942fa9e4066Sahrens if (error != EEXIST) 2943fa9e4066Sahrens fatal(0, "spa_import('%s') twice", newname); 2944fa9e4066Sahrens 2945fa9e4066Sahrens /* 2946fa9e4066Sahrens * Try to import it under a different name -- should fail with EEXIST. 2947fa9e4066Sahrens */ 2948fa9e4066Sahrens error = spa_import(oldname, config, NULL); 2949fa9e4066Sahrens if (error != EEXIST) 2950fa9e4066Sahrens fatal(0, "spa_import('%s') under multiple names", newname); 2951fa9e4066Sahrens 2952fa9e4066Sahrens /* 2953fa9e4066Sahrens * Verify that the pool is no longer visible under the old name. 2954fa9e4066Sahrens */ 2955fa9e4066Sahrens error = spa_open(oldname, &spa, FTAG); 2956fa9e4066Sahrens if (error != ENOENT) 2957fa9e4066Sahrens fatal(0, "spa_open('%s') = %d", newname, error); 2958fa9e4066Sahrens 2959fa9e4066Sahrens /* 2960fa9e4066Sahrens * Verify that we can open and close the pool using the new name. 2961fa9e4066Sahrens */ 2962fa9e4066Sahrens error = spa_open(newname, &spa, FTAG); 2963fa9e4066Sahrens if (error) 2964fa9e4066Sahrens fatal(0, "spa_open('%s') = %d", newname, error); 2965fa9e4066Sahrens ASSERT(pool_guid == spa_guid(spa)); 2966fa9e4066Sahrens spa_close(spa, FTAG); 2967fa9e4066Sahrens 2968fa9e4066Sahrens nvlist_free(config); 2969fa9e4066Sahrens } 2970fa9e4066Sahrens 2971fa9e4066Sahrens static void * 2972fa9e4066Sahrens ztest_thread(void *arg) 2973fa9e4066Sahrens { 2974fa9e4066Sahrens ztest_args_t *za = arg; 2975fa9e4066Sahrens ztest_shared_t *zs = ztest_shared; 2976fa9e4066Sahrens hrtime_t now, functime; 2977fa9e4066Sahrens ztest_info_t *zi; 2978fa9e4066Sahrens int f; 2979fa9e4066Sahrens 2980fa9e4066Sahrens while ((now = gethrtime()) < za->za_stop) { 2981fa9e4066Sahrens /* 2982fa9e4066Sahrens * See if it's time to force a crash. 2983fa9e4066Sahrens */ 2984fa9e4066Sahrens if (now > za->za_kill) { 29854d31c452Smaybee dmu_tx_t *tx; 29864d31c452Smaybee uint64_t txg; 29874d31c452Smaybee 29884d31c452Smaybee mutex_enter(&spa_namespace_lock); 29894d31c452Smaybee tx = dmu_tx_create(za->za_os); 29904d31c452Smaybee VERIFY(0 == dmu_tx_assign(tx, TXG_NOWAIT)); 29914d31c452Smaybee txg = dmu_tx_get_txg(tx); 29924d31c452Smaybee dmu_tx_commit(tx); 29934d31c452Smaybee zs->zs_txg = txg; 29944d31c452Smaybee if (zopt_verbose >= 3) 29954d31c452Smaybee (void) printf( 29964d31c452Smaybee "killing process after txg %lld\n", 29974d31c452Smaybee (u_longlong_t)txg); 29984d31c452Smaybee txg_wait_synced(dmu_objset_pool(za->za_os), txg); 2999fa9e4066Sahrens zs->zs_alloc = spa_get_alloc(dmu_objset_spa(za->za_os)); 3000fa9e4066Sahrens zs->zs_space = spa_get_space(dmu_objset_spa(za->za_os)); 3001fa9e4066Sahrens (void) kill(getpid(), SIGKILL); 3002fa9e4066Sahrens } 3003fa9e4066Sahrens 3004fa9e4066Sahrens /* 3005fa9e4066Sahrens * Pick a random function. 3006fa9e4066Sahrens */ 3007fa9e4066Sahrens f = ztest_random(ZTEST_FUNCS); 3008fa9e4066Sahrens zi = &zs->zs_info[f]; 3009fa9e4066Sahrens 3010fa9e4066Sahrens /* 3011fa9e4066Sahrens * Decide whether to call it, based on the requested frequency. 3012fa9e4066Sahrens */ 3013fa9e4066Sahrens if (zi->zi_call_target == 0 || 3014fa9e4066Sahrens (double)zi->zi_call_total / zi->zi_call_target > 3015fa9e4066Sahrens (double)(now - zs->zs_start_time) / (zopt_time * NANOSEC)) 3016fa9e4066Sahrens continue; 3017fa9e4066Sahrens 3018fa9e4066Sahrens atomic_add_64(&zi->zi_calls, 1); 3019fa9e4066Sahrens atomic_add_64(&zi->zi_call_total, 1); 3020fa9e4066Sahrens 3021fa9e4066Sahrens za->za_diroff = (za->za_instance * ZTEST_FUNCS + f) * 3022fa9e4066Sahrens ZTEST_DIRSIZE; 3023fa9e4066Sahrens za->za_diroff_shared = (1ULL << 63); 3024fa9e4066Sahrens 3025fa9e4066Sahrens ztest_dmu_write_parallel(za); 3026fa9e4066Sahrens 3027fa9e4066Sahrens zi->zi_func(za); 3028fa9e4066Sahrens 3029fa9e4066Sahrens functime = gethrtime() - now; 3030fa9e4066Sahrens 3031fa9e4066Sahrens atomic_add_64(&zi->zi_call_time, functime); 3032fa9e4066Sahrens 3033fa9e4066Sahrens if (zopt_verbose >= 4) { 3034fa9e4066Sahrens Dl_info dli; 3035fa9e4066Sahrens (void) dladdr((void *)zi->zi_func, &dli); 3036fa9e4066Sahrens (void) printf("%6.2f sec in %s\n", 3037fa9e4066Sahrens (double)functime / NANOSEC, dli.dli_sname); 3038fa9e4066Sahrens } 3039fa9e4066Sahrens 3040fa9e4066Sahrens /* 3041fa9e4066Sahrens * If we're getting ENOSPC with some regularity, stop. 3042fa9e4066Sahrens */ 3043fa9e4066Sahrens if (zs->zs_enospc_count > 10) 3044fa9e4066Sahrens break; 3045fa9e4066Sahrens } 3046fa9e4066Sahrens 3047fa9e4066Sahrens return (NULL); 3048fa9e4066Sahrens } 3049fa9e4066Sahrens 3050fa9e4066Sahrens /* 3051fa9e4066Sahrens * Kick off threads to run tests on all datasets in parallel. 3052fa9e4066Sahrens */ 3053fa9e4066Sahrens static void 3054fa9e4066Sahrens ztest_run(char *pool) 3055fa9e4066Sahrens { 3056fa9e4066Sahrens int t, d, error; 3057fa9e4066Sahrens ztest_shared_t *zs = ztest_shared; 3058fa9e4066Sahrens ztest_args_t *za; 3059fa9e4066Sahrens spa_t *spa; 3060fa9e4066Sahrens char name[100]; 3061fa9e4066Sahrens 3062fa9e4066Sahrens (void) _mutex_init(&zs->zs_vdev_lock, USYNC_THREAD, NULL); 3063fa9e4066Sahrens (void) rwlock_init(&zs->zs_name_lock, USYNC_THREAD, NULL); 3064fa9e4066Sahrens 3065fa9e4066Sahrens for (t = 0; t < ZTEST_SYNC_LOCKS; t++) 3066fa9e4066Sahrens (void) _mutex_init(&zs->zs_sync_lock[t], USYNC_THREAD, NULL); 3067fa9e4066Sahrens 3068fa9e4066Sahrens /* 3069fa9e4066Sahrens * Destroy one disk before we even start. 3070fa9e4066Sahrens * It's mirrored, so everything should work just fine. 3071fa9e4066Sahrens * This makes us exercise fault handling very early in spa_load(). 3072fa9e4066Sahrens */ 3073fa9e4066Sahrens ztest_obliterate_one_disk(0); 3074fa9e4066Sahrens 3075fa9e4066Sahrens /* 3076fa9e4066Sahrens * Verify that the sum of the sizes of all blocks in the pool 3077fa9e4066Sahrens * equals the SPA's allocated space total. 3078fa9e4066Sahrens */ 3079fa9e4066Sahrens ztest_verify_blocks(pool); 3080fa9e4066Sahrens 3081fa9e4066Sahrens /* 3082fa9e4066Sahrens * Kick off a replacement of the disk we just obliterated. 3083fa9e4066Sahrens */ 3084fa9e4066Sahrens kernel_init(FREAD | FWRITE); 3085fa9e4066Sahrens error = spa_open(pool, &spa, FTAG); 3086fa9e4066Sahrens if (error) 3087fa9e4066Sahrens fatal(0, "spa_open(%s) = %d", pool, error); 3088fa9e4066Sahrens ztest_replace_one_disk(spa, 0); 3089fa9e4066Sahrens if (zopt_verbose >= 5) 3090fa9e4066Sahrens show_pool_stats(spa); 3091fa9e4066Sahrens spa_close(spa, FTAG); 3092fa9e4066Sahrens kernel_fini(); 3093fa9e4066Sahrens 3094fa9e4066Sahrens kernel_init(FREAD | FWRITE); 3095fa9e4066Sahrens 3096fa9e4066Sahrens /* 3097fa9e4066Sahrens * Verify that we can export the pool and reimport it under a 3098fa9e4066Sahrens * different name. 3099fa9e4066Sahrens */ 31000e34b6a7Sbonwick if (ztest_random(2) == 0) { 3101fa9e4066Sahrens (void) snprintf(name, 100, "%s_import", pool); 3102fa9e4066Sahrens ztest_spa_import_export(pool, name); 3103fa9e4066Sahrens ztest_spa_import_export(name, pool); 31040e34b6a7Sbonwick } 3105fa9e4066Sahrens 3106fa9e4066Sahrens /* 3107fa9e4066Sahrens * Verify that we can loop over all pools. 3108fa9e4066Sahrens */ 3109fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 3110fa9e4066Sahrens for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa)) { 3111fa9e4066Sahrens if (zopt_verbose > 3) { 3112fa9e4066Sahrens (void) printf("spa_next: found %s\n", spa_name(spa)); 3113fa9e4066Sahrens } 3114fa9e4066Sahrens } 3115fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3116fa9e4066Sahrens 3117fa9e4066Sahrens /* 3118fa9e4066Sahrens * Open our pool. 3119fa9e4066Sahrens */ 3120fa9e4066Sahrens error = spa_open(pool, &spa, FTAG); 3121fa9e4066Sahrens if (error) 3122fa9e4066Sahrens fatal(0, "spa_open() = %d", error); 3123fa9e4066Sahrens 3124fa9e4066Sahrens /* 3125fa9e4066Sahrens * Verify that we can safely inquire about about any object, 3126fa9e4066Sahrens * whether it's allocated or not. To make it interesting, 3127fa9e4066Sahrens * we probe a 5-wide window around each power of two. 3128fa9e4066Sahrens * This hits all edge cases, including zero and the max. 3129fa9e4066Sahrens */ 3130fa9e4066Sahrens for (t = 0; t < 64; t++) { 3131fa9e4066Sahrens for (d = -5; d <= 5; d++) { 3132fa9e4066Sahrens error = dmu_object_info(spa->spa_meta_objset, 3133fa9e4066Sahrens (1ULL << t) + d, NULL); 3134ea8dc4b6Seschrock ASSERT(error == 0 || error == ENOENT || 3135ea8dc4b6Seschrock error == EINVAL); 3136fa9e4066Sahrens } 3137fa9e4066Sahrens } 3138fa9e4066Sahrens 3139fa9e4066Sahrens /* 3140fa9e4066Sahrens * Now kick off all the tests that run in parallel. 3141fa9e4066Sahrens */ 3142fa9e4066Sahrens zs->zs_enospc_count = 0; 3143fa9e4066Sahrens 3144fa9e4066Sahrens za = umem_zalloc(zopt_threads * sizeof (ztest_args_t), UMEM_NOFAIL); 3145fa9e4066Sahrens 3146fa9e4066Sahrens if (zopt_verbose >= 4) 3147fa9e4066Sahrens (void) printf("starting main threads...\n"); 3148fa9e4066Sahrens 3149fa9e4066Sahrens za[0].za_start = gethrtime(); 3150fa9e4066Sahrens za[0].za_stop = za[0].za_start + zopt_passtime * NANOSEC; 3151fa9e4066Sahrens za[0].za_stop = MIN(za[0].za_stop, zs->zs_stop_time); 3152fa9e4066Sahrens za[0].za_kill = za[0].za_stop; 3153fa9e4066Sahrens if (ztest_random(100) < zopt_killrate) 3154fa9e4066Sahrens za[0].za_kill -= ztest_random(zopt_passtime * NANOSEC); 3155fa9e4066Sahrens 3156fa9e4066Sahrens for (t = 0; t < zopt_threads; t++) { 3157ecc2d604Sbonwick d = t % zopt_datasets; 3158ecc2d604Sbonwick if (t < zopt_datasets) { 3159fa9e4066Sahrens ztest_replay_t zr; 31604d31c452Smaybee int test_future = FALSE; 3161fa9e4066Sahrens (void) rw_rdlock(&ztest_shared->zs_name_lock); 3162fa9e4066Sahrens (void) snprintf(name, 100, "%s/%s_%d", pool, pool, d); 3163fa9e4066Sahrens error = dmu_objset_create(name, DMU_OST_OTHER, NULL, 3164fa9e4066Sahrens ztest_create_cb, NULL); 31654d31c452Smaybee if (error == EEXIST) { 31664d31c452Smaybee test_future = TRUE; 31674d31c452Smaybee } else if (error != 0) { 3168fa9e4066Sahrens if (error == ENOSPC) { 3169fa9e4066Sahrens zs->zs_enospc_count++; 3170fa9e4066Sahrens (void) rw_unlock( 3171fa9e4066Sahrens &ztest_shared->zs_name_lock); 3172fa9e4066Sahrens break; 3173fa9e4066Sahrens } 3174fa9e4066Sahrens fatal(0, "dmu_objset_create(%s) = %d", 3175fa9e4066Sahrens name, error); 3176fa9e4066Sahrens } 3177fa9e4066Sahrens error = dmu_objset_open(name, DMU_OST_OTHER, 3178fa9e4066Sahrens DS_MODE_STANDARD, &za[d].za_os); 3179fa9e4066Sahrens if (error) 3180fa9e4066Sahrens fatal(0, "dmu_objset_open('%s') = %d", 3181fa9e4066Sahrens name, error); 3182fa9e4066Sahrens (void) rw_unlock(&ztest_shared->zs_name_lock); 31834d31c452Smaybee if (test_future && ztest_shared->zs_txg > 0) 31844d31c452Smaybee ztest_dmu_check_future_leak(za[d].za_os, 31854d31c452Smaybee ztest_shared->zs_txg); 3186fa9e4066Sahrens zr.zr_os = za[d].za_os; 3187fa9e4066Sahrens zil_replay(zr.zr_os, &zr, &zr.zr_assign, 3188893a6d32Sahrens ztest_replay_vector); 3189fa9e4066Sahrens za[d].za_zilog = zil_open(za[d].za_os, NULL); 3190fa9e4066Sahrens } 3191fa9e4066Sahrens za[t].za_pool = spa_strdup(pool); 3192fa9e4066Sahrens za[t].za_os = za[d].za_os; 3193fa9e4066Sahrens za[t].za_zilog = za[d].za_zilog; 3194fa9e4066Sahrens za[t].za_instance = t; 3195fa9e4066Sahrens za[t].za_random = ztest_random(-1ULL); 3196fa9e4066Sahrens za[t].za_start = za[0].za_start; 3197fa9e4066Sahrens za[t].za_stop = za[0].za_stop; 3198fa9e4066Sahrens za[t].za_kill = za[0].za_kill; 3199fa9e4066Sahrens 3200fa9e4066Sahrens error = thr_create(0, 0, ztest_thread, &za[t], THR_BOUND, 3201fa9e4066Sahrens &za[t].za_thread); 3202fa9e4066Sahrens if (error) 3203fa9e4066Sahrens fatal(0, "can't create thread %d: error %d", 3204fa9e4066Sahrens t, error); 3205fa9e4066Sahrens } 32064d31c452Smaybee ztest_shared->zs_txg = 0; 3207fa9e4066Sahrens 3208fa9e4066Sahrens while (--t >= 0) { 3209fa9e4066Sahrens error = thr_join(za[t].za_thread, NULL, NULL); 3210fa9e4066Sahrens if (error) 3211fa9e4066Sahrens fatal(0, "thr_join(%d) = %d", t, error); 3212fa9e4066Sahrens if (za[t].za_th) 3213fa9e4066Sahrens traverse_fini(za[t].za_th); 3214ecc2d604Sbonwick if (t < zopt_datasets) { 3215fa9e4066Sahrens zil_close(za[t].za_zilog); 3216fa9e4066Sahrens dmu_objset_close(za[t].za_os); 3217fa9e4066Sahrens } 3218fa9e4066Sahrens spa_strfree(za[t].za_pool); 3219fa9e4066Sahrens } 3220fa9e4066Sahrens 3221fa9e4066Sahrens umem_free(za, zopt_threads * sizeof (ztest_args_t)); 3222fa9e4066Sahrens 3223fa9e4066Sahrens if (zopt_verbose >= 3) 3224fa9e4066Sahrens show_pool_stats(spa); 3225fa9e4066Sahrens 3226fa9e4066Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 3227fa9e4066Sahrens 3228fa9e4066Sahrens zs->zs_alloc = spa_get_alloc(spa); 3229fa9e4066Sahrens zs->zs_space = spa_get_space(spa); 3230fa9e4066Sahrens 3231fa9e4066Sahrens /* 3232fa9e4066Sahrens * Did we have out-of-space errors? If so, destroy a random objset. 3233fa9e4066Sahrens */ 3234fa9e4066Sahrens if (zs->zs_enospc_count != 0) { 3235fa9e4066Sahrens (void) rw_rdlock(&ztest_shared->zs_name_lock); 3236fa9e4066Sahrens (void) snprintf(name, 100, "%s/%s_%d", pool, pool, 3237ecc2d604Sbonwick (int)ztest_random(zopt_datasets)); 3238fa9e4066Sahrens if (zopt_verbose >= 3) 3239fa9e4066Sahrens (void) printf("Destroying %s to free up space\n", name); 32401d452cf5Sahrens (void) dmu_objset_find(name, ztest_destroy_cb, NULL, 32410b69c2f0Sahrens DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN); 3242fa9e4066Sahrens (void) rw_unlock(&ztest_shared->zs_name_lock); 3243fa9e4066Sahrens } 3244fa9e4066Sahrens 3245ea8dc4b6Seschrock txg_wait_synced(spa_get_dsl(spa), 0); 3246fa9e4066Sahrens 3247fa9e4066Sahrens /* 3248fa9e4066Sahrens * Right before closing the pool, kick off a bunch of async I/O; 3249fa9e4066Sahrens * spa_close() should wait for it to complete. 3250fa9e4066Sahrens */ 3251fa9e4066Sahrens for (t = 1; t < 50; t++) 3252fa9e4066Sahrens dmu_prefetch(spa->spa_meta_objset, t, 0, 1 << 15); 3253fa9e4066Sahrens 3254fa9e4066Sahrens spa_close(spa, FTAG); 3255fa9e4066Sahrens 3256fa9e4066Sahrens kernel_fini(); 3257fa9e4066Sahrens } 3258fa9e4066Sahrens 3259fa9e4066Sahrens void 3260fa9e4066Sahrens print_time(hrtime_t t, char *timebuf) 3261fa9e4066Sahrens { 3262fa9e4066Sahrens hrtime_t s = t / NANOSEC; 3263fa9e4066Sahrens hrtime_t m = s / 60; 3264fa9e4066Sahrens hrtime_t h = m / 60; 3265fa9e4066Sahrens hrtime_t d = h / 24; 3266fa9e4066Sahrens 3267fa9e4066Sahrens s -= m * 60; 3268fa9e4066Sahrens m -= h * 60; 3269fa9e4066Sahrens h -= d * 24; 3270fa9e4066Sahrens 3271fa9e4066Sahrens timebuf[0] = '\0'; 3272fa9e4066Sahrens 3273fa9e4066Sahrens if (d) 3274fa9e4066Sahrens (void) sprintf(timebuf, 3275fa9e4066Sahrens "%llud%02lluh%02llum%02llus", d, h, m, s); 3276fa9e4066Sahrens else if (h) 3277fa9e4066Sahrens (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s); 3278fa9e4066Sahrens else if (m) 3279fa9e4066Sahrens (void) sprintf(timebuf, "%llum%02llus", m, s); 3280fa9e4066Sahrens else 3281fa9e4066Sahrens (void) sprintf(timebuf, "%llus", s); 3282fa9e4066Sahrens } 3283fa9e4066Sahrens 3284fa9e4066Sahrens /* 3285fa9e4066Sahrens * Create a storage pool with the given name and initial vdev size. 3286fa9e4066Sahrens * Then create the specified number of datasets in the pool. 3287fa9e4066Sahrens */ 3288fa9e4066Sahrens static void 3289fa9e4066Sahrens ztest_init(char *pool) 3290fa9e4066Sahrens { 3291fa9e4066Sahrens spa_t *spa; 3292fa9e4066Sahrens int error; 3293fa9e4066Sahrens nvlist_t *nvroot; 3294fa9e4066Sahrens 3295fa9e4066Sahrens kernel_init(FREAD | FWRITE); 3296fa9e4066Sahrens 3297fa9e4066Sahrens /* 3298fa9e4066Sahrens * Create the storage pool. 3299fa9e4066Sahrens */ 3300fa9e4066Sahrens (void) spa_destroy(pool); 3301fa9e4066Sahrens ztest_shared->zs_vdev_primaries = 0; 33028654d025Sperrin nvroot = make_vdev_root(zopt_vdev_size, 0, zopt_raidz, zopt_mirrors, 1); 3303228975ccSek110237 error = spa_create(pool, nvroot, NULL, NULL); 3304fa9e4066Sahrens nvlist_free(nvroot); 3305fa9e4066Sahrens 3306fa9e4066Sahrens if (error) 3307fa9e4066Sahrens fatal(0, "spa_create() = %d", error); 3308fa9e4066Sahrens error = spa_open(pool, &spa, FTAG); 3309fa9e4066Sahrens if (error) 3310fa9e4066Sahrens fatal(0, "spa_open() = %d", error); 3311fa9e4066Sahrens 3312fa9e4066Sahrens if (zopt_verbose >= 3) 3313fa9e4066Sahrens show_pool_stats(spa); 3314fa9e4066Sahrens 3315fa9e4066Sahrens spa_close(spa, FTAG); 3316fa9e4066Sahrens 3317fa9e4066Sahrens kernel_fini(); 3318fa9e4066Sahrens } 3319fa9e4066Sahrens 3320fa9e4066Sahrens int 3321fa9e4066Sahrens main(int argc, char **argv) 3322fa9e4066Sahrens { 3323fa9e4066Sahrens int kills = 0; 3324fa9e4066Sahrens int iters = 0; 3325fa9e4066Sahrens int i, f; 3326fa9e4066Sahrens ztest_shared_t *zs; 3327fa9e4066Sahrens ztest_info_t *zi; 3328fa9e4066Sahrens char timebuf[100]; 3329fa9e4066Sahrens char numbuf[6]; 3330fa9e4066Sahrens 3331fa9e4066Sahrens (void) setvbuf(stdout, NULL, _IOLBF, 0); 3332fa9e4066Sahrens 3333fa9e4066Sahrens /* Override location of zpool.cache */ 3334fa9e4066Sahrens spa_config_dir = "/tmp"; 3335fa9e4066Sahrens 3336fa9e4066Sahrens ztest_random_fd = open("/dev/urandom", O_RDONLY); 3337fa9e4066Sahrens 3338fa9e4066Sahrens process_options(argc, argv); 3339fa9e4066Sahrens 3340fa9e4066Sahrens argc -= optind; 3341fa9e4066Sahrens argv += optind; 3342fa9e4066Sahrens 3343fa9e4066Sahrens dprintf_setup(&argc, argv); 3344fa9e4066Sahrens 3345ea8dc4b6Seschrock /* 3346ea8dc4b6Seschrock * Blow away any existing copy of zpool.cache 3347ea8dc4b6Seschrock */ 3348ea8dc4b6Seschrock if (zopt_init != 0) 3349ea8dc4b6Seschrock (void) remove("/tmp/zpool.cache"); 3350ea8dc4b6Seschrock 3351fa9e4066Sahrens zs = ztest_shared = (void *)mmap(0, 3352fa9e4066Sahrens P2ROUNDUP(sizeof (ztest_shared_t), getpagesize()), 3353fa9e4066Sahrens PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0); 3354fa9e4066Sahrens 3355fa9e4066Sahrens if (zopt_verbose >= 1) { 3356fa9e4066Sahrens (void) printf("%llu vdevs, %d datasets, %d threads," 3357fa9e4066Sahrens " %llu seconds...\n", 3358ecc2d604Sbonwick (u_longlong_t)zopt_vdevs, zopt_datasets, zopt_threads, 3359fa9e4066Sahrens (u_longlong_t)zopt_time); 3360fa9e4066Sahrens } 3361fa9e4066Sahrens 3362fa9e4066Sahrens /* 3363fa9e4066Sahrens * Create and initialize our storage pool. 3364fa9e4066Sahrens */ 3365fa9e4066Sahrens for (i = 1; i <= zopt_init; i++) { 3366fa9e4066Sahrens bzero(zs, sizeof (ztest_shared_t)); 3367fa9e4066Sahrens if (zopt_verbose >= 3 && zopt_init != 1) 3368fa9e4066Sahrens (void) printf("ztest_init(), pass %d\n", i); 3369fa9e4066Sahrens ztest_init(zopt_pool); 3370fa9e4066Sahrens } 3371fa9e4066Sahrens 3372fa9e4066Sahrens /* 3373fa9e4066Sahrens * Initialize the call targets for each function. 3374fa9e4066Sahrens */ 3375fa9e4066Sahrens for (f = 0; f < ZTEST_FUNCS; f++) { 3376fa9e4066Sahrens zi = &zs->zs_info[f]; 3377fa9e4066Sahrens 3378fa9e4066Sahrens *zi = ztest_info[f]; 3379fa9e4066Sahrens 3380fa9e4066Sahrens if (*zi->zi_interval == 0) 3381fa9e4066Sahrens zi->zi_call_target = UINT64_MAX; 3382fa9e4066Sahrens else 3383fa9e4066Sahrens zi->zi_call_target = zopt_time / *zi->zi_interval; 3384fa9e4066Sahrens } 3385fa9e4066Sahrens 3386fa9e4066Sahrens zs->zs_start_time = gethrtime(); 3387fa9e4066Sahrens zs->zs_stop_time = zs->zs_start_time + zopt_time * NANOSEC; 3388fa9e4066Sahrens 3389fa9e4066Sahrens /* 3390fa9e4066Sahrens * Run the tests in a loop. These tests include fault injection 3391fa9e4066Sahrens * to verify that self-healing data works, and forced crashes 3392fa9e4066Sahrens * to verify that we never lose on-disk consistency. 3393fa9e4066Sahrens */ 3394fa9e4066Sahrens while (gethrtime() < zs->zs_stop_time) { 3395fa9e4066Sahrens int status; 3396fa9e4066Sahrens pid_t pid; 3397fa9e4066Sahrens char *tmp; 3398fa9e4066Sahrens 3399fa9e4066Sahrens /* 3400fa9e4066Sahrens * Initialize the workload counters for each function. 3401fa9e4066Sahrens */ 3402fa9e4066Sahrens for (f = 0; f < ZTEST_FUNCS; f++) { 3403fa9e4066Sahrens zi = &zs->zs_info[f]; 3404fa9e4066Sahrens zi->zi_calls = 0; 3405fa9e4066Sahrens zi->zi_call_time = 0; 3406fa9e4066Sahrens } 3407fa9e4066Sahrens 3408fa9e4066Sahrens pid = fork(); 3409fa9e4066Sahrens 3410fa9e4066Sahrens if (pid == -1) 3411fa9e4066Sahrens fatal(1, "fork failed"); 3412fa9e4066Sahrens 3413fa9e4066Sahrens if (pid == 0) { /* child */ 3414fa9e4066Sahrens struct rlimit rl = { 1024, 1024 }; 3415fa9e4066Sahrens (void) setrlimit(RLIMIT_NOFILE, &rl); 3416004388ebScasper (void) enable_extended_FILE_stdio(-1, -1); 3417fa9e4066Sahrens ztest_run(zopt_pool); 3418fa9e4066Sahrens exit(0); 3419fa9e4066Sahrens } 3420fa9e4066Sahrens 34215ad82045Snd150628 while (waitpid(pid, &status, 0) != pid) 3422fa9e4066Sahrens continue; 3423fa9e4066Sahrens 3424fa9e4066Sahrens if (WIFEXITED(status)) { 3425fa9e4066Sahrens if (WEXITSTATUS(status) != 0) { 3426fa9e4066Sahrens (void) fprintf(stderr, 3427fa9e4066Sahrens "child exited with code %d\n", 3428fa9e4066Sahrens WEXITSTATUS(status)); 3429fa9e4066Sahrens exit(2); 3430fa9e4066Sahrens } 34315ad82045Snd150628 } else if (WIFSIGNALED(status)) { 3432fa9e4066Sahrens if (WTERMSIG(status) != SIGKILL) { 3433fa9e4066Sahrens (void) fprintf(stderr, 3434fa9e4066Sahrens "child died with signal %d\n", 3435fa9e4066Sahrens WTERMSIG(status)); 3436fa9e4066Sahrens exit(3); 3437fa9e4066Sahrens } 3438fa9e4066Sahrens kills++; 34395ad82045Snd150628 } else { 34405ad82045Snd150628 (void) fprintf(stderr, "something strange happened " 34415ad82045Snd150628 "to child\n"); 34425ad82045Snd150628 exit(4); 3443fa9e4066Sahrens } 3444fa9e4066Sahrens 3445fa9e4066Sahrens iters++; 3446fa9e4066Sahrens 3447fa9e4066Sahrens if (zopt_verbose >= 1) { 3448fa9e4066Sahrens hrtime_t now = gethrtime(); 3449fa9e4066Sahrens 3450fa9e4066Sahrens now = MIN(now, zs->zs_stop_time); 3451fa9e4066Sahrens print_time(zs->zs_stop_time - now, timebuf); 3452fa9e4066Sahrens nicenum(zs->zs_space, numbuf); 3453fa9e4066Sahrens 3454fa9e4066Sahrens (void) printf("Pass %3d, %8s, %3llu ENOSPC, " 3455fa9e4066Sahrens "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n", 3456fa9e4066Sahrens iters, 3457fa9e4066Sahrens WIFEXITED(status) ? "Complete" : "SIGKILL", 3458fa9e4066Sahrens (u_longlong_t)zs->zs_enospc_count, 3459fa9e4066Sahrens 100.0 * zs->zs_alloc / zs->zs_space, 3460fa9e4066Sahrens numbuf, 3461fa9e4066Sahrens 100.0 * (now - zs->zs_start_time) / 3462fa9e4066Sahrens (zopt_time * NANOSEC), timebuf); 3463fa9e4066Sahrens } 3464fa9e4066Sahrens 3465fa9e4066Sahrens if (zopt_verbose >= 2) { 3466fa9e4066Sahrens (void) printf("\nWorkload summary:\n\n"); 3467fa9e4066Sahrens (void) printf("%7s %9s %s\n", 3468fa9e4066Sahrens "Calls", "Time", "Function"); 3469fa9e4066Sahrens (void) printf("%7s %9s %s\n", 3470fa9e4066Sahrens "-----", "----", "--------"); 3471fa9e4066Sahrens for (f = 0; f < ZTEST_FUNCS; f++) { 3472fa9e4066Sahrens Dl_info dli; 3473fa9e4066Sahrens 3474fa9e4066Sahrens zi = &zs->zs_info[f]; 3475fa9e4066Sahrens print_time(zi->zi_call_time, timebuf); 3476fa9e4066Sahrens (void) dladdr((void *)zi->zi_func, &dli); 3477fa9e4066Sahrens (void) printf("%7llu %9s %s\n", 3478fa9e4066Sahrens (u_longlong_t)zi->zi_calls, timebuf, 3479fa9e4066Sahrens dli.dli_sname); 3480fa9e4066Sahrens } 3481fa9e4066Sahrens (void) printf("\n"); 3482fa9e4066Sahrens } 3483fa9e4066Sahrens 3484fa9e4066Sahrens /* 3485fa9e4066Sahrens * It's possible that we killed a child during a rename test, in 3486fa9e4066Sahrens * which case we'll have a 'ztest_tmp' pool lying around instead 3487fa9e4066Sahrens * of 'ztest'. Do a blind rename in case this happened. 3488fa9e4066Sahrens */ 3489fa9e4066Sahrens tmp = umem_alloc(strlen(zopt_pool) + 5, UMEM_NOFAIL); 3490fa9e4066Sahrens (void) strcpy(tmp, zopt_pool); 3491fa9e4066Sahrens (void) strcat(tmp, "_tmp"); 3492fa9e4066Sahrens kernel_init(FREAD | FWRITE); 3493fa9e4066Sahrens (void) spa_rename(tmp, zopt_pool); 3494fa9e4066Sahrens kernel_fini(); 3495fa9e4066Sahrens umem_free(tmp, strlen(tmp) + 1); 3496fa9e4066Sahrens } 3497fa9e4066Sahrens 3498fa9e4066Sahrens ztest_verify_blocks(zopt_pool); 3499fa9e4066Sahrens 3500fa9e4066Sahrens if (zopt_verbose >= 1) { 3501fa9e4066Sahrens (void) printf("%d killed, %d completed, %.0f%% kill rate\n", 3502fa9e4066Sahrens kills, iters - kills, (100.0 * kills) / MAX(1, iters)); 3503fa9e4066Sahrens } 3504fa9e4066Sahrens 3505fa9e4066Sahrens return (0); 3506fa9e4066Sahrens } 3507