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 /* 2247cb52daSJeff Bonwick * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23*40a5c998SMatthew Ahrens * Copyright (c) 2011, 2016 by Delphix. All rights reserved. 24e9103aaeSGarrett D'Amore * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 25a7a845e4SSteven Hartland * Copyright (c) 2013 Steven Hartland. All rights reserved. 26c3d26abcSMatthew Ahrens * Copyright (c) 2014 Integros [integros.com] 27fa9e4066Sahrens */ 28fa9e4066Sahrens 29fa9e4066Sahrens /* 30fa9e4066Sahrens * The objective of this program is to provide a DMU/ZAP/SPA stress test 31fa9e4066Sahrens * that runs entirely in userland, is easy to use, and easy to extend. 32fa9e4066Sahrens * 33fa9e4066Sahrens * The overall design of the ztest program is as follows: 34fa9e4066Sahrens * 35fa9e4066Sahrens * (1) For each major functional area (e.g. adding vdevs to a pool, 36fa9e4066Sahrens * creating and destroying datasets, reading and writing objects, etc) 37fa9e4066Sahrens * we have a simple routine to test that functionality. These 38fa9e4066Sahrens * individual routines do not have to do anything "stressful". 39fa9e4066Sahrens * 40fa9e4066Sahrens * (2) We turn these simple functionality tests into a stress test by 41fa9e4066Sahrens * running them all in parallel, with as many threads as desired, 42fa9e4066Sahrens * and spread across as many datasets, objects, and vdevs as desired. 43fa9e4066Sahrens * 44fa9e4066Sahrens * (3) While all this is happening, we inject faults into the pool to 45fa9e4066Sahrens * verify that self-healing data really works. 46fa9e4066Sahrens * 47fa9e4066Sahrens * (4) Every time we open a dataset, we change its checksum and compression 48fa9e4066Sahrens * functions. Thus even individual objects vary from block to block 49fa9e4066Sahrens * in which checksum they use and whether they're compressed. 50fa9e4066Sahrens * 51fa9e4066Sahrens * (5) To verify that we never lose on-disk consistency after a crash, 52fa9e4066Sahrens * we run the entire test in a child of the main process. 53fa9e4066Sahrens * At random times, the child self-immolates with a SIGKILL. 54fa9e4066Sahrens * This is the software equivalent of pulling the power cord. 55fa9e4066Sahrens * The parent then runs the test again, using the existing 565d7b4d43SMatthew Ahrens * storage pool, as many times as desired. If backwards compatibility 57420dfc95SChris Siden * testing is enabled ztest will sometimes run the "older" version 58420dfc95SChris Siden * of ztest after a SIGKILL. 59fa9e4066Sahrens * 60fa9e4066Sahrens * (6) To verify that we don't have future leaks or temporal incursions, 61fa9e4066Sahrens * many of the functional tests record the transaction group number 62fa9e4066Sahrens * as part of their data. When reading old data, they verify that 63fa9e4066Sahrens * the transaction group number is less than the current, open txg. 64fa9e4066Sahrens * If you add a new test, please do this if applicable. 65fa9e4066Sahrens * 66fa9e4066Sahrens * When run with no arguments, ztest runs for about five minutes and 67fa9e4066Sahrens * produces no output if successful. To get a little bit of information, 68fa9e4066Sahrens * specify -V. To get more information, specify -VV, and so on. 69fa9e4066Sahrens * 70fa9e4066Sahrens * To turn this into an overnight stress test, use -T to specify run time. 71fa9e4066Sahrens * 72fa9e4066Sahrens * You can ask more more vdevs [-v], datasets [-d], or threads [-t] 73fa9e4066Sahrens * to increase the pool capacity, fanout, and overall stress level. 74fa9e4066Sahrens * 75420dfc95SChris Siden * Use the -k option to set the desired frequency of kills. 76420dfc95SChris Siden * 77420dfc95SChris Siden * When ztest invokes itself it passes all relevant information through a 78420dfc95SChris Siden * temporary file which is mmap-ed in the child process. This allows shared 79420dfc95SChris Siden * memory to survive the exec syscall. The ztest_shared_hdr_t struct is always 80420dfc95SChris Siden * stored at offset 0 of this file and contains information on the size and 81420dfc95SChris Siden * number of shared structures in the file. The information stored in this file 82420dfc95SChris Siden * must remain backwards compatible with older versions of ztest so that 83420dfc95SChris Siden * ztest can invoke them during backwards compatibility testing (-B). 84fa9e4066Sahrens */ 85fa9e4066Sahrens 86fa9e4066Sahrens #include <sys/zfs_context.h> 87fa9e4066Sahrens #include <sys/spa.h> 88fa9e4066Sahrens #include <sys/dmu.h> 89fa9e4066Sahrens #include <sys/txg.h> 902fdbea25SAleksandr Guzovskiy #include <sys/dbuf.h> 91fa9e4066Sahrens #include <sys/zap.h> 92fa9e4066Sahrens #include <sys/dmu_objset.h> 93fa9e4066Sahrens #include <sys/poll.h> 94fa9e4066Sahrens #include <sys/stat.h> 95fa9e4066Sahrens #include <sys/time.h> 96fa9e4066Sahrens #include <sys/wait.h> 97fa9e4066Sahrens #include <sys/mman.h> 98fa9e4066Sahrens #include <sys/resource.h> 99fa9e4066Sahrens #include <sys/zio.h> 100fa9e4066Sahrens #include <sys/zil.h> 101b24ab676SJeff Bonwick #include <sys/zil_impl.h> 102fa9e4066Sahrens #include <sys/vdev_impl.h> 103e14bb325SJeff Bonwick #include <sys/vdev_file.h> 104fa9e4066Sahrens #include <sys/spa_impl.h> 10588ecc943SGeorge Wilson #include <sys/metaslab_impl.h> 106fa9e4066Sahrens #include <sys/dsl_prop.h> 1074f5064b7SMark J Musante #include <sys/dsl_dataset.h> 1083b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h> 1093f9d6ad7SLin Ling #include <sys/dsl_scan.h> 110cde58dbcSMatthew Ahrens #include <sys/zio_checksum.h> 111fa9e4066Sahrens #include <sys/refcount.h> 112ad135b5dSChristopher Siden #include <sys/zfeature.h> 1133b2aab18SMatthew Ahrens #include <sys/dsl_userhold.h> 114fa9e4066Sahrens #include <stdio.h> 115004388ebScasper #include <stdio_ext.h> 116fa9e4066Sahrens #include <stdlib.h> 117fa9e4066Sahrens #include <unistd.h> 118fa9e4066Sahrens #include <signal.h> 119fa9e4066Sahrens #include <umem.h> 120fa9e4066Sahrens #include <dlfcn.h> 121fa9e4066Sahrens #include <ctype.h> 122fa9e4066Sahrens #include <math.h> 123fa9e4066Sahrens #include <sys/fs/zfs.h> 124b24ab676SJeff Bonwick #include <libnvpair.h> 125fa9e4066Sahrens 126741652b0SEtienne Dechamps static int ztest_fd_data = -1; 127741652b0SEtienne Dechamps static int ztest_fd_rand = -1; 128fa9e4066Sahrens 129420dfc95SChris Siden typedef struct ztest_shared_hdr { 130420dfc95SChris Siden uint64_t zh_hdr_size; 131420dfc95SChris Siden uint64_t zh_opts_size; 132420dfc95SChris Siden uint64_t zh_size; 133420dfc95SChris Siden uint64_t zh_stats_size; 134420dfc95SChris Siden uint64_t zh_stats_count; 135420dfc95SChris Siden uint64_t zh_ds_size; 136420dfc95SChris Siden uint64_t zh_ds_count; 137420dfc95SChris Siden } ztest_shared_hdr_t; 138420dfc95SChris Siden 139420dfc95SChris Siden static ztest_shared_hdr_t *ztest_shared_hdr; 140420dfc95SChris Siden 141420dfc95SChris Siden typedef struct ztest_shared_opts { 142*40a5c998SMatthew Ahrens char zo_pool[ZFS_MAX_DATASET_NAME_LEN]; 143*40a5c998SMatthew Ahrens char zo_dir[ZFS_MAX_DATASET_NAME_LEN]; 144420dfc95SChris Siden char zo_alt_ztest[MAXNAMELEN]; 145420dfc95SChris Siden char zo_alt_libpath[MAXNAMELEN]; 146420dfc95SChris Siden uint64_t zo_vdevs; 147420dfc95SChris Siden uint64_t zo_vdevtime; 148420dfc95SChris Siden size_t zo_vdev_size; 149420dfc95SChris Siden int zo_ashift; 150420dfc95SChris Siden int zo_mirrors; 151420dfc95SChris Siden int zo_raidz; 152420dfc95SChris Siden int zo_raidz_parity; 153420dfc95SChris Siden int zo_datasets; 154420dfc95SChris Siden int zo_threads; 155420dfc95SChris Siden uint64_t zo_passtime; 156420dfc95SChris Siden uint64_t zo_killrate; 157420dfc95SChris Siden int zo_verbose; 158420dfc95SChris Siden int zo_init; 159420dfc95SChris Siden uint64_t zo_time; 160420dfc95SChris Siden uint64_t zo_maxloops; 161420dfc95SChris Siden uint64_t zo_metaslab_gang_bang; 162420dfc95SChris Siden } ztest_shared_opts_t; 163420dfc95SChris Siden 164420dfc95SChris Siden static const ztest_shared_opts_t ztest_opts_defaults = { 165420dfc95SChris Siden .zo_pool = { 'z', 't', 'e', 's', 't', '\0' }, 166420dfc95SChris Siden .zo_dir = { '/', 't', 'm', 'p', '\0' }, 167420dfc95SChris Siden .zo_alt_ztest = { '\0' }, 168420dfc95SChris Siden .zo_alt_libpath = { '\0' }, 169420dfc95SChris Siden .zo_vdevs = 5, 170420dfc95SChris Siden .zo_ashift = SPA_MINBLOCKSHIFT, 171420dfc95SChris Siden .zo_mirrors = 2, 172420dfc95SChris Siden .zo_raidz = 4, 173420dfc95SChris Siden .zo_raidz_parity = 1, 174539eed8aSMatthew Ahrens .zo_vdev_size = SPA_MINDEVSIZE * 2, 175420dfc95SChris Siden .zo_datasets = 7, 176420dfc95SChris Siden .zo_threads = 23, 177420dfc95SChris Siden .zo_passtime = 60, /* 60 seconds */ 178420dfc95SChris Siden .zo_killrate = 70, /* 70% kill rate */ 179420dfc95SChris Siden .zo_verbose = 0, 180420dfc95SChris Siden .zo_init = 1, 181420dfc95SChris Siden .zo_time = 300, /* 5 minutes */ 182420dfc95SChris Siden .zo_maxloops = 50, /* max loops during spa_freeze() */ 183420dfc95SChris Siden .zo_metaslab_gang_bang = 32 << 10 184420dfc95SChris Siden }; 185420dfc95SChris Siden 186420dfc95SChris Siden extern uint64_t metaslab_gang_bang; 187420dfc95SChris Siden extern uint64_t metaslab_df_alloc_threshold; 18869962b56SMatthew Ahrens extern uint64_t zfs_deadman_synctime_ms; 18930beaff4SGeorge Wilson extern int metaslab_preload_limit; 190420dfc95SChris Siden 191420dfc95SChris Siden static ztest_shared_opts_t *ztest_shared_opts; 192420dfc95SChris Siden static ztest_shared_opts_t ztest_opts; 193420dfc95SChris Siden 194420dfc95SChris Siden typedef struct ztest_shared_ds { 195420dfc95SChris Siden uint64_t zd_seq; 196420dfc95SChris Siden } ztest_shared_ds_t; 197420dfc95SChris Siden 198420dfc95SChris Siden static ztest_shared_ds_t *ztest_shared_ds; 199420dfc95SChris Siden #define ZTEST_GET_SHARED_DS(d) (&ztest_shared_ds[d]) 200fa9e4066Sahrens 201b24ab676SJeff Bonwick #define BT_MAGIC 0x123456789abcdefULL 202420dfc95SChris Siden #define MAXFAULTS() \ 203420dfc95SChris Siden (MAX(zs->zs_mirrors, 1) * (ztest_opts.zo_raidz_parity + 1) - 1) 204b24ab676SJeff Bonwick 205b24ab676SJeff Bonwick enum ztest_io_type { 206b24ab676SJeff Bonwick ZTEST_IO_WRITE_TAG, 207b24ab676SJeff Bonwick ZTEST_IO_WRITE_PATTERN, 208b24ab676SJeff Bonwick ZTEST_IO_WRITE_ZEROES, 209b24ab676SJeff Bonwick ZTEST_IO_TRUNCATE, 210b24ab676SJeff Bonwick ZTEST_IO_SETATTR, 21180901aeaSGeorge Wilson ZTEST_IO_REWRITE, 212b24ab676SJeff Bonwick ZTEST_IO_TYPES 213b24ab676SJeff Bonwick }; 214b24ab676SJeff Bonwick 215e05725b1Sbonwick typedef struct ztest_block_tag { 216b24ab676SJeff Bonwick uint64_t bt_magic; 217e05725b1Sbonwick uint64_t bt_objset; 218e05725b1Sbonwick uint64_t bt_object; 219e05725b1Sbonwick uint64_t bt_offset; 220b24ab676SJeff Bonwick uint64_t bt_gen; 221e05725b1Sbonwick uint64_t bt_txg; 222b24ab676SJeff Bonwick uint64_t bt_crtxg; 223e05725b1Sbonwick } ztest_block_tag_t; 224e05725b1Sbonwick 225b24ab676SJeff Bonwick typedef struct bufwad { 226b24ab676SJeff Bonwick uint64_t bw_index; 227b24ab676SJeff Bonwick uint64_t bw_txg; 228b24ab676SJeff Bonwick uint64_t bw_data; 229b24ab676SJeff Bonwick } bufwad_t; 230fa9e4066Sahrens 231fa9e4066Sahrens /* 232b24ab676SJeff Bonwick * XXX -- fix zfs range locks to be generic so we can use them here. 233fa9e4066Sahrens */ 234b24ab676SJeff Bonwick typedef enum { 235b24ab676SJeff Bonwick RL_READER, 236b24ab676SJeff Bonwick RL_WRITER, 237b24ab676SJeff Bonwick RL_APPEND 238b24ab676SJeff Bonwick } rl_type_t; 239b24ab676SJeff Bonwick 240b24ab676SJeff Bonwick typedef struct rll { 241b24ab676SJeff Bonwick void *rll_writer; 242b24ab676SJeff Bonwick int rll_readers; 243b24ab676SJeff Bonwick mutex_t rll_lock; 244b24ab676SJeff Bonwick cond_t rll_cv; 245b24ab676SJeff Bonwick } rll_t; 246b24ab676SJeff Bonwick 247b24ab676SJeff Bonwick typedef struct rl { 248b24ab676SJeff Bonwick uint64_t rl_object; 249b24ab676SJeff Bonwick uint64_t rl_offset; 250b24ab676SJeff Bonwick uint64_t rl_size; 251b24ab676SJeff Bonwick rll_t *rl_lock; 252b24ab676SJeff Bonwick } rl_t; 253b24ab676SJeff Bonwick 254b24ab676SJeff Bonwick #define ZTEST_RANGE_LOCKS 64 255b24ab676SJeff Bonwick #define ZTEST_OBJECT_LOCKS 64 256b24ab676SJeff Bonwick 257b24ab676SJeff Bonwick /* 258b24ab676SJeff Bonwick * Object descriptor. Used as a template for object lookup/create/remove. 259b24ab676SJeff Bonwick */ 260b24ab676SJeff Bonwick typedef struct ztest_od { 261b24ab676SJeff Bonwick uint64_t od_dir; 262b24ab676SJeff Bonwick uint64_t od_object; 263b24ab676SJeff Bonwick dmu_object_type_t od_type; 264b24ab676SJeff Bonwick dmu_object_type_t od_crtype; 265b24ab676SJeff Bonwick uint64_t od_blocksize; 266b24ab676SJeff Bonwick uint64_t od_crblocksize; 267b24ab676SJeff Bonwick uint64_t od_gen; 268b24ab676SJeff Bonwick uint64_t od_crgen; 269*40a5c998SMatthew Ahrens char od_name[ZFS_MAX_DATASET_NAME_LEN]; 270b24ab676SJeff Bonwick } ztest_od_t; 271b24ab676SJeff Bonwick 272b24ab676SJeff Bonwick /* 273b24ab676SJeff Bonwick * Per-dataset state. 274b24ab676SJeff Bonwick */ 275b24ab676SJeff Bonwick typedef struct ztest_ds { 276420dfc95SChris Siden ztest_shared_ds_t *zd_shared; 277b24ab676SJeff Bonwick objset_t *zd_os; 278c9ba2a43SEric Schrock rwlock_t zd_zilog_lock; 279b24ab676SJeff Bonwick zilog_t *zd_zilog; 280b24ab676SJeff Bonwick ztest_od_t *zd_od; /* debugging aid */ 281*40a5c998SMatthew Ahrens char zd_name[ZFS_MAX_DATASET_NAME_LEN]; 282b24ab676SJeff Bonwick mutex_t zd_dirobj_lock; 283b24ab676SJeff Bonwick rll_t zd_object_lock[ZTEST_OBJECT_LOCKS]; 284b24ab676SJeff Bonwick rll_t zd_range_lock[ZTEST_RANGE_LOCKS]; 285b24ab676SJeff Bonwick } ztest_ds_t; 286b24ab676SJeff Bonwick 287b24ab676SJeff Bonwick /* 288b24ab676SJeff Bonwick * Per-iteration state. 289b24ab676SJeff Bonwick */ 290b24ab676SJeff Bonwick typedef void ztest_func_t(ztest_ds_t *zd, uint64_t id); 291fa9e4066Sahrens 292fa9e4066Sahrens typedef struct ztest_info { 293fa9e4066Sahrens ztest_func_t *zi_func; /* test function */ 294e05725b1Sbonwick uint64_t zi_iters; /* iterations per execution */ 295fa9e4066Sahrens uint64_t *zi_interval; /* execute every <interval> seconds */ 296fa9e4066Sahrens } ztest_info_t; 297fa9e4066Sahrens 298420dfc95SChris Siden typedef struct ztest_shared_callstate { 299420dfc95SChris Siden uint64_t zc_count; /* per-pass count */ 300420dfc95SChris Siden uint64_t zc_time; /* per-pass time */ 301420dfc95SChris Siden uint64_t zc_next; /* next time to call this function */ 302420dfc95SChris Siden } ztest_shared_callstate_t; 303420dfc95SChris Siden 304420dfc95SChris Siden static ztest_shared_callstate_t *ztest_shared_callstate; 305420dfc95SChris Siden #define ZTEST_GET_SHARED_CALLSTATE(c) (&ztest_shared_callstate[c]) 306420dfc95SChris Siden 307b24ab676SJeff Bonwick /* 308b24ab676SJeff Bonwick * Note: these aren't static because we want dladdr() to work. 309b24ab676SJeff Bonwick */ 310b24ab676SJeff Bonwick ztest_func_t ztest_dmu_read_write; 311b24ab676SJeff Bonwick ztest_func_t ztest_dmu_write_parallel; 312b24ab676SJeff Bonwick ztest_func_t ztest_dmu_object_alloc_free; 313b24ab676SJeff Bonwick ztest_func_t ztest_dmu_commit_callbacks; 314b24ab676SJeff Bonwick ztest_func_t ztest_zap; 315b24ab676SJeff Bonwick ztest_func_t ztest_zap_parallel; 316b24ab676SJeff Bonwick ztest_func_t ztest_zil_commit; 317c9ba2a43SEric Schrock ztest_func_t ztest_zil_remount; 318b24ab676SJeff Bonwick ztest_func_t ztest_dmu_read_write_zcopy; 319b24ab676SJeff Bonwick ztest_func_t ztest_dmu_objset_create_destroy; 320b24ab676SJeff Bonwick ztest_func_t ztest_dmu_prealloc; 321b24ab676SJeff Bonwick ztest_func_t ztest_fzap; 322b24ab676SJeff Bonwick ztest_func_t ztest_dmu_snapshot_create_destroy; 323b24ab676SJeff Bonwick ztest_func_t ztest_dsl_prop_get_set; 324b24ab676SJeff Bonwick ztest_func_t ztest_spa_prop_get_set; 325b24ab676SJeff Bonwick ztest_func_t ztest_spa_create_destroy; 326b24ab676SJeff Bonwick ztest_func_t ztest_fault_inject; 327b24ab676SJeff Bonwick ztest_func_t ztest_ddt_repair; 328b24ab676SJeff Bonwick ztest_func_t ztest_dmu_snapshot_hold; 329b24ab676SJeff Bonwick ztest_func_t ztest_spa_rename; 330b24ab676SJeff Bonwick ztest_func_t ztest_scrub; 331b24ab676SJeff Bonwick ztest_func_t ztest_dsl_dataset_promote_busy; 332b24ab676SJeff Bonwick ztest_func_t ztest_vdev_attach_detach; 333b24ab676SJeff Bonwick ztest_func_t ztest_vdev_LUN_growth; 334b24ab676SJeff Bonwick ztest_func_t ztest_vdev_add_remove; 335b24ab676SJeff Bonwick ztest_func_t ztest_vdev_aux_add_remove; 3361195e687SMark J Musante ztest_func_t ztest_split_pool; 337e9103aaeSGarrett D'Amore ztest_func_t ztest_reguid; 33825345e46SGeorge Wilson ztest_func_t ztest_spa_upgrade; 339b24ab676SJeff Bonwick 340b24ab676SJeff Bonwick uint64_t zopt_always = 0ULL * NANOSEC; /* all the time */ 341b24ab676SJeff Bonwick uint64_t zopt_incessant = 1ULL * NANOSEC / 10; /* every 1/10 second */ 342b24ab676SJeff Bonwick uint64_t zopt_often = 1ULL * NANOSEC; /* every second */ 343b24ab676SJeff Bonwick uint64_t zopt_sometimes = 10ULL * NANOSEC; /* every 10 seconds */ 344b24ab676SJeff Bonwick uint64_t zopt_rarely = 60ULL * NANOSEC; /* every 60 seconds */ 345fa9e4066Sahrens 346fa9e4066Sahrens ztest_info_t ztest_info[] = { 347e05725b1Sbonwick { ztest_dmu_read_write, 1, &zopt_always }, 348b24ab676SJeff Bonwick { ztest_dmu_write_parallel, 10, &zopt_always }, 349e05725b1Sbonwick { ztest_dmu_object_alloc_free, 1, &zopt_always }, 350b24ab676SJeff Bonwick { ztest_dmu_commit_callbacks, 1, &zopt_always }, 351e05725b1Sbonwick { ztest_zap, 30, &zopt_always }, 352e05725b1Sbonwick { ztest_zap_parallel, 100, &zopt_always }, 3531195e687SMark J Musante { ztest_split_pool, 1, &zopt_always }, 354b24ab676SJeff Bonwick { ztest_zil_commit, 1, &zopt_incessant }, 355c9ba2a43SEric Schrock { ztest_zil_remount, 1, &zopt_sometimes }, 356b24ab676SJeff Bonwick { ztest_dmu_read_write_zcopy, 1, &zopt_often }, 357b24ab676SJeff Bonwick { ztest_dmu_objset_create_destroy, 1, &zopt_often }, 358b24ab676SJeff Bonwick { ztest_dsl_prop_get_set, 1, &zopt_often }, 359b24ab676SJeff Bonwick { ztest_spa_prop_get_set, 1, &zopt_sometimes }, 360b24ab676SJeff Bonwick #if 0 361b24ab676SJeff Bonwick { ztest_dmu_prealloc, 1, &zopt_sometimes }, 362b24ab676SJeff Bonwick #endif 363b24ab676SJeff Bonwick { ztest_fzap, 1, &zopt_sometimes }, 364e14bb325SJeff Bonwick { ztest_dmu_snapshot_create_destroy, 1, &zopt_sometimes }, 365e05725b1Sbonwick { ztest_spa_create_destroy, 1, &zopt_sometimes }, 366e05725b1Sbonwick { ztest_fault_inject, 1, &zopt_sometimes }, 367b24ab676SJeff Bonwick { ztest_ddt_repair, 1, &zopt_sometimes }, 3685c987a37SChris Kirby { ztest_dmu_snapshot_hold, 1, &zopt_sometimes }, 3692c1e2b44SGeorge Wilson { ztest_reguid, 1, &zopt_rarely }, 370e05725b1Sbonwick { ztest_spa_rename, 1, &zopt_rarely }, 371b24ab676SJeff Bonwick { ztest_scrub, 1, &zopt_rarely }, 37225345e46SGeorge Wilson { ztest_spa_upgrade, 1, &zopt_rarely }, 373b24ab676SJeff Bonwick { ztest_dsl_dataset_promote_busy, 1, &zopt_rarely }, 3743b2aab18SMatthew Ahrens { ztest_vdev_attach_detach, 1, &zopt_sometimes }, 375e05725b1Sbonwick { ztest_vdev_LUN_growth, 1, &zopt_rarely }, 376420dfc95SChris Siden { ztest_vdev_add_remove, 1, 377420dfc95SChris Siden &ztest_opts.zo_vdevtime }, 378420dfc95SChris Siden { ztest_vdev_aux_add_remove, 1, 379420dfc95SChris Siden &ztest_opts.zo_vdevtime }, 380fa9e4066Sahrens }; 381fa9e4066Sahrens 382fa9e4066Sahrens #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t)) 383fa9e4066Sahrens 384fa9e4066Sahrens /* 385d20e665cSRicardo M. Correia * The following struct is used to hold a list of uncalled commit callbacks. 386d20e665cSRicardo M. Correia * The callbacks are ordered by txg number. 387d20e665cSRicardo M. Correia */ 388d20e665cSRicardo M. Correia typedef struct ztest_cb_list { 389d20e665cSRicardo M. Correia mutex_t zcl_callbacks_lock; 390d20e665cSRicardo M. Correia list_t zcl_callbacks; 391d20e665cSRicardo M. Correia } ztest_cb_list_t; 392d20e665cSRicardo M. Correia 393d20e665cSRicardo M. Correia /* 394fa9e4066Sahrens * Stuff we need to share writably between parent and child. 395fa9e4066Sahrens */ 396fa9e4066Sahrens typedef struct ztest_shared { 397420dfc95SChris Siden boolean_t zs_do_init; 398b24ab676SJeff Bonwick hrtime_t zs_proc_start; 399b24ab676SJeff Bonwick hrtime_t zs_proc_stop; 400b24ab676SJeff Bonwick hrtime_t zs_thread_start; 401b24ab676SJeff Bonwick hrtime_t zs_thread_stop; 402b24ab676SJeff Bonwick hrtime_t zs_thread_kill; 403b24ab676SJeff Bonwick uint64_t zs_enospc_count; 40488ecc943SGeorge Wilson uint64_t zs_vdev_next_leaf; 405e14bb325SJeff Bonwick uint64_t zs_vdev_aux; 406fa9e4066Sahrens uint64_t zs_alloc; 407fa9e4066Sahrens uint64_t zs_space; 4081195e687SMark J Musante uint64_t zs_splits; 4091195e687SMark J Musante uint64_t zs_mirrors; 410420dfc95SChris Siden uint64_t zs_metaslab_sz; 411420dfc95SChris Siden uint64_t zs_metaslab_df_alloc_threshold; 412420dfc95SChris Siden uint64_t zs_guid; 413fa9e4066Sahrens } ztest_shared_t; 414fa9e4066Sahrens 415b24ab676SJeff Bonwick #define ID_PARALLEL -1ULL 416b24ab676SJeff Bonwick 417fa9e4066Sahrens static char ztest_dev_template[] = "%s/%s.%llua"; 418e14bb325SJeff Bonwick static char ztest_aux_template[] = "%s/%s.%s.%llu"; 419b24ab676SJeff Bonwick ztest_shared_t *ztest_shared; 420fa9e4066Sahrens 421420dfc95SChris Siden static spa_t *ztest_spa = NULL; 422420dfc95SChris Siden static ztest_ds_t *ztest_ds; 423fa9e4066Sahrens 424420dfc95SChris Siden static mutex_t ztest_vdev_lock; 425dfbb9432SGeorge Wilson 426dfbb9432SGeorge Wilson /* 427dfbb9432SGeorge Wilson * The ztest_name_lock protects the pool and dataset namespace used by 428dfbb9432SGeorge Wilson * the individual tests. To modify the namespace, consumers must grab 429dfbb9432SGeorge Wilson * this lock as writer. Grabbing the lock as reader will ensure that the 430dfbb9432SGeorge Wilson * namespace does not change while the lock is held. 431dfbb9432SGeorge Wilson */ 432420dfc95SChris Siden static rwlock_t ztest_name_lock; 433420dfc95SChris Siden 434420dfc95SChris Siden static boolean_t ztest_dump_core = B_TRUE; 435e14bb325SJeff Bonwick static boolean_t ztest_exiting; 4360a4e9518Sgw25295 437d20e665cSRicardo M. Correia /* Global commit callback list */ 438d20e665cSRicardo M. Correia static ztest_cb_list_t zcl; 439d20e665cSRicardo M. Correia 440b24ab676SJeff Bonwick enum ztest_object { 441b24ab676SJeff Bonwick ZTEST_META_DNODE = 0, 442b24ab676SJeff Bonwick ZTEST_DIROBJ, 443b24ab676SJeff Bonwick ZTEST_OBJECTS 444b24ab676SJeff Bonwick }; 445fa9e4066Sahrens 4461ce825d8Sraf static void usage(boolean_t) __NORETURN; 447f1b4288bSvb160487 448fa9e4066Sahrens /* 449fa9e4066Sahrens * These libumem hooks provide a reasonable set of defaults for the allocator's 450fa9e4066Sahrens * debugging facilities. 451fa9e4066Sahrens */ 452fa9e4066Sahrens const char * 453fa9e4066Sahrens _umem_debug_init() 454fa9e4066Sahrens { 455fa9e4066Sahrens return ("default,verbose"); /* $UMEM_DEBUG setting */ 456fa9e4066Sahrens } 457fa9e4066Sahrens 458fa9e4066Sahrens const char * 459fa9e4066Sahrens _umem_logging_init(void) 460fa9e4066Sahrens { 461fa9e4066Sahrens return ("fail,contents"); /* $UMEM_LOGGING setting */ 462fa9e4066Sahrens } 463fa9e4066Sahrens 464fa9e4066Sahrens #define FATAL_MSG_SZ 1024 465fa9e4066Sahrens 466fa9e4066Sahrens char *fatal_msg; 467fa9e4066Sahrens 468fa9e4066Sahrens static void 469fa9e4066Sahrens fatal(int do_perror, char *message, ...) 470fa9e4066Sahrens { 471fa9e4066Sahrens va_list args; 472fa9e4066Sahrens int save_errno = errno; 473fa9e4066Sahrens char buf[FATAL_MSG_SZ]; 474fa9e4066Sahrens 475fa9e4066Sahrens (void) fflush(stdout); 476fa9e4066Sahrens 477fa9e4066Sahrens va_start(args, message); 478fa9e4066Sahrens (void) sprintf(buf, "ztest: "); 479fa9e4066Sahrens /* LINTED */ 480fa9e4066Sahrens (void) vsprintf(buf + strlen(buf), message, args); 481fa9e4066Sahrens va_end(args); 482fa9e4066Sahrens if (do_perror) { 483fa9e4066Sahrens (void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf), 484fa9e4066Sahrens ": %s", strerror(save_errno)); 485fa9e4066Sahrens } 486fa9e4066Sahrens (void) fprintf(stderr, "%s\n", buf); 487fa9e4066Sahrens fatal_msg = buf; /* to ease debugging */ 488fa9e4066Sahrens if (ztest_dump_core) 489fa9e4066Sahrens abort(); 490fa9e4066Sahrens exit(3); 491fa9e4066Sahrens } 492fa9e4066Sahrens 493fa9e4066Sahrens static int 494fa9e4066Sahrens str2shift(const char *buf) 495fa9e4066Sahrens { 496fa9e4066Sahrens const char *ends = "BKMGTPEZ"; 497fa9e4066Sahrens int i; 498fa9e4066Sahrens 499fa9e4066Sahrens if (buf[0] == '\0') 500fa9e4066Sahrens return (0); 501fa9e4066Sahrens for (i = 0; i < strlen(ends); i++) { 502fa9e4066Sahrens if (toupper(buf[0]) == ends[i]) 503fa9e4066Sahrens break; 504fa9e4066Sahrens } 505f1b4288bSvb160487 if (i == strlen(ends)) { 506f1b4288bSvb160487 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", 507f1b4288bSvb160487 buf); 508f1b4288bSvb160487 usage(B_FALSE); 509f1b4288bSvb160487 } 510fa9e4066Sahrens if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) { 511fa9e4066Sahrens return (10*i); 512fa9e4066Sahrens } 513f1b4288bSvb160487 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf); 514f1b4288bSvb160487 usage(B_FALSE); 515f1b4288bSvb160487 /* NOTREACHED */ 516fa9e4066Sahrens } 517fa9e4066Sahrens 518fa9e4066Sahrens static uint64_t 519fa9e4066Sahrens nicenumtoull(const char *buf) 520fa9e4066Sahrens { 521fa9e4066Sahrens char *end; 522fa9e4066Sahrens uint64_t val; 523fa9e4066Sahrens 524fa9e4066Sahrens val = strtoull(buf, &end, 0); 525fa9e4066Sahrens if (end == buf) { 526f1b4288bSvb160487 (void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf); 527f1b4288bSvb160487 usage(B_FALSE); 528fa9e4066Sahrens } else if (end[0] == '.') { 529fa9e4066Sahrens double fval = strtod(buf, &end); 530fa9e4066Sahrens fval *= pow(2, str2shift(end)); 531f1b4288bSvb160487 if (fval > UINT64_MAX) { 532f1b4288bSvb160487 (void) fprintf(stderr, "ztest: value too large: %s\n", 533f1b4288bSvb160487 buf); 534f1b4288bSvb160487 usage(B_FALSE); 535f1b4288bSvb160487 } 536fa9e4066Sahrens val = (uint64_t)fval; 537fa9e4066Sahrens } else { 538fa9e4066Sahrens int shift = str2shift(end); 539f1b4288bSvb160487 if (shift >= 64 || (val << shift) >> shift != val) { 540f1b4288bSvb160487 (void) fprintf(stderr, "ztest: value too large: %s\n", 541f1b4288bSvb160487 buf); 542f1b4288bSvb160487 usage(B_FALSE); 543f1b4288bSvb160487 } 544fa9e4066Sahrens val <<= shift; 545fa9e4066Sahrens } 546fa9e4066Sahrens return (val); 547fa9e4066Sahrens } 548fa9e4066Sahrens 549fa9e4066Sahrens static void 550f1b4288bSvb160487 usage(boolean_t requested) 551fa9e4066Sahrens { 552420dfc95SChris Siden const ztest_shared_opts_t *zo = &ztest_opts_defaults; 553420dfc95SChris Siden 554fa9e4066Sahrens char nice_vdev_size[10]; 555fa9e4066Sahrens char nice_gang_bang[10]; 556f1b4288bSvb160487 FILE *fp = requested ? stdout : stderr; 557fa9e4066Sahrens 558420dfc95SChris Siden nicenum(zo->zo_vdev_size, nice_vdev_size); 559420dfc95SChris Siden nicenum(zo->zo_metaslab_gang_bang, nice_gang_bang); 560fa9e4066Sahrens 561f1b4288bSvb160487 (void) fprintf(fp, "Usage: %s\n" 562fa9e4066Sahrens "\t[-v vdevs (default: %llu)]\n" 563fa9e4066Sahrens "\t[-s size_of_each_vdev (default: %s)]\n" 5642215e990SMark J Musante "\t[-a alignment_shift (default: %d)] use 0 for random\n" 565fa9e4066Sahrens "\t[-m mirror_copies (default: %d)]\n" 566fa9e4066Sahrens "\t[-r raidz_disks (default: %d)]\n" 56799653d4eSeschrock "\t[-R raidz_parity (default: %d)]\n" 568fa9e4066Sahrens "\t[-d datasets (default: %d)]\n" 569fa9e4066Sahrens "\t[-t threads (default: %d)]\n" 570fa9e4066Sahrens "\t[-g gang_block_threshold (default: %s)]\n" 5712215e990SMark J Musante "\t[-i init_count (default: %d)] initialize pool i times\n" 5722215e990SMark J Musante "\t[-k kill_percentage (default: %llu%%)]\n" 573fa9e4066Sahrens "\t[-p pool_name (default: %s)]\n" 5742215e990SMark J Musante "\t[-f dir (default: %s)] file directory for vdev files\n" 5752215e990SMark J Musante "\t[-V] verbose (use multiple times for ever more blather)\n" 5762215e990SMark J Musante "\t[-E] use existing pool instead of creating new one\n" 5772215e990SMark J Musante "\t[-T time (default: %llu sec)] total run time\n" 5782215e990SMark J Musante "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n" 5792215e990SMark J Musante "\t[-P passtime (default: %llu sec)] time per pass\n" 580420dfc95SChris Siden "\t[-B alt_ztest (default: <none>)] alternate ztest path\n" 581f1b4288bSvb160487 "\t[-h] (print help)\n" 582fa9e4066Sahrens "", 583420dfc95SChris Siden zo->zo_pool, 584420dfc95SChris Siden (u_longlong_t)zo->zo_vdevs, /* -v */ 585fa9e4066Sahrens nice_vdev_size, /* -s */ 586420dfc95SChris Siden zo->zo_ashift, /* -a */ 587420dfc95SChris Siden zo->zo_mirrors, /* -m */ 588420dfc95SChris Siden zo->zo_raidz, /* -r */ 589420dfc95SChris Siden zo->zo_raidz_parity, /* -R */ 590420dfc95SChris Siden zo->zo_datasets, /* -d */ 591420dfc95SChris Siden zo->zo_threads, /* -t */ 592fa9e4066Sahrens nice_gang_bang, /* -g */ 593420dfc95SChris Siden zo->zo_init, /* -i */ 594420dfc95SChris Siden (u_longlong_t)zo->zo_killrate, /* -k */ 595420dfc95SChris Siden zo->zo_pool, /* -p */ 596420dfc95SChris Siden zo->zo_dir, /* -f */ 597420dfc95SChris Siden (u_longlong_t)zo->zo_time, /* -T */ 598420dfc95SChris Siden (u_longlong_t)zo->zo_maxloops, /* -F */ 599420dfc95SChris Siden (u_longlong_t)zo->zo_passtime); 600f1b4288bSvb160487 exit(requested ? 0 : 1); 601fa9e4066Sahrens } 602fa9e4066Sahrens 603fa9e4066Sahrens static void 604fa9e4066Sahrens process_options(int argc, char **argv) 605fa9e4066Sahrens { 606420dfc95SChris Siden char *path; 607420dfc95SChris Siden ztest_shared_opts_t *zo = &ztest_opts; 608420dfc95SChris Siden 609fa9e4066Sahrens int opt; 610fa9e4066Sahrens uint64_t value; 611420dfc95SChris Siden char altdir[MAXNAMELEN] = { 0 }; 612fa9e4066Sahrens 613420dfc95SChris Siden bcopy(&ztest_opts_defaults, zo, sizeof (*zo)); 614fa9e4066Sahrens 615fa9e4066Sahrens while ((opt = getopt(argc, argv, 616420dfc95SChris Siden "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:hF:B:")) != EOF) { 617fa9e4066Sahrens value = 0; 618fa9e4066Sahrens switch (opt) { 619fa9e4066Sahrens case 'v': 620fa9e4066Sahrens case 's': 621ecc2d604Sbonwick case 'a': 622fa9e4066Sahrens case 'm': 623fa9e4066Sahrens case 'r': 62499653d4eSeschrock case 'R': 625fa9e4066Sahrens case 'd': 626fa9e4066Sahrens case 't': 627fa9e4066Sahrens case 'g': 628fa9e4066Sahrens case 'i': 629fa9e4066Sahrens case 'k': 630fa9e4066Sahrens case 'T': 631fa9e4066Sahrens case 'P': 6322215e990SMark J Musante case 'F': 633fa9e4066Sahrens value = nicenumtoull(optarg); 634fa9e4066Sahrens } 635fa9e4066Sahrens switch (opt) { 636fa9e4066Sahrens case 'v': 637420dfc95SChris Siden zo->zo_vdevs = value; 638fa9e4066Sahrens break; 639fa9e4066Sahrens case 's': 640420dfc95SChris Siden zo->zo_vdev_size = MAX(SPA_MINDEVSIZE, value); 641fa9e4066Sahrens break; 642ecc2d604Sbonwick case 'a': 643420dfc95SChris Siden zo->zo_ashift = value; 644ecc2d604Sbonwick break; 645fa9e4066Sahrens case 'm': 646420dfc95SChris Siden zo->zo_mirrors = value; 647fa9e4066Sahrens break; 648fa9e4066Sahrens case 'r': 649420dfc95SChris Siden zo->zo_raidz = MAX(1, value); 650fa9e4066Sahrens break; 65199653d4eSeschrock case 'R': 652420dfc95SChris Siden zo->zo_raidz_parity = MIN(MAX(value, 1), 3); 65399653d4eSeschrock break; 654fa9e4066Sahrens case 'd': 655420dfc95SChris Siden zo->zo_datasets = MAX(1, value); 656fa9e4066Sahrens break; 657fa9e4066Sahrens case 't': 658420dfc95SChris Siden zo->zo_threads = MAX(1, value); 659fa9e4066Sahrens break; 660fa9e4066Sahrens case 'g': 661420dfc95SChris Siden zo->zo_metaslab_gang_bang = MAX(SPA_MINBLOCKSIZE << 1, 662420dfc95SChris Siden value); 663fa9e4066Sahrens break; 664fa9e4066Sahrens case 'i': 665420dfc95SChris Siden zo->zo_init = value; 666fa9e4066Sahrens break; 667fa9e4066Sahrens case 'k': 668420dfc95SChris Siden zo->zo_killrate = value; 669fa9e4066Sahrens break; 670fa9e4066Sahrens case 'p': 671420dfc95SChris Siden (void) strlcpy(zo->zo_pool, optarg, 672420dfc95SChris Siden sizeof (zo->zo_pool)); 673fa9e4066Sahrens break; 674fa9e4066Sahrens case 'f': 675420dfc95SChris Siden path = realpath(optarg, NULL); 676420dfc95SChris Siden if (path == NULL) { 677420dfc95SChris Siden (void) fprintf(stderr, "error: %s: %s\n", 678420dfc95SChris Siden optarg, strerror(errno)); 679420dfc95SChris Siden usage(B_FALSE); 680420dfc95SChris Siden } else { 681420dfc95SChris Siden (void) strlcpy(zo->zo_dir, path, 682420dfc95SChris Siden sizeof (zo->zo_dir)); 683420dfc95SChris Siden } 684fa9e4066Sahrens break; 685fa9e4066Sahrens case 'V': 686420dfc95SChris Siden zo->zo_verbose++; 687fa9e4066Sahrens break; 688fa9e4066Sahrens case 'E': 689420dfc95SChris Siden zo->zo_init = 0; 690fa9e4066Sahrens break; 691fa9e4066Sahrens case 'T': 692420dfc95SChris Siden zo->zo_time = value; 693fa9e4066Sahrens break; 694fa9e4066Sahrens case 'P': 695420dfc95SChris Siden zo->zo_passtime = MAX(1, value); 696fa9e4066Sahrens break; 6972215e990SMark J Musante case 'F': 698420dfc95SChris Siden zo->zo_maxloops = MAX(1, value); 699420dfc95SChris Siden break; 700420dfc95SChris Siden case 'B': 701420dfc95SChris Siden (void) strlcpy(altdir, optarg, sizeof (altdir)); 7022215e990SMark J Musante break; 703f1b4288bSvb160487 case 'h': 704f1b4288bSvb160487 usage(B_TRUE); 705f1b4288bSvb160487 break; 706fa9e4066Sahrens case '?': 707fa9e4066Sahrens default: 708f1b4288bSvb160487 usage(B_FALSE); 709fa9e4066Sahrens break; 710fa9e4066Sahrens } 711fa9e4066Sahrens } 712fa9e4066Sahrens 713420dfc95SChris Siden zo->zo_raidz_parity = MIN(zo->zo_raidz_parity, zo->zo_raidz - 1); 71499653d4eSeschrock 715420dfc95SChris Siden zo->zo_vdevtime = 716420dfc95SChris Siden (zo->zo_vdevs > 0 ? zo->zo_time * NANOSEC / zo->zo_vdevs : 717b24ab676SJeff Bonwick UINT64_MAX >> 2); 718420dfc95SChris Siden 719420dfc95SChris Siden if (strlen(altdir) > 0) { 720741652b0SEtienne Dechamps char *cmd; 721741652b0SEtienne Dechamps char *realaltdir; 722420dfc95SChris Siden char *bin; 723420dfc95SChris Siden char *ztest; 724420dfc95SChris Siden char *isa; 725420dfc95SChris Siden int isalen; 726420dfc95SChris Siden 727741652b0SEtienne Dechamps cmd = umem_alloc(MAXPATHLEN, UMEM_NOFAIL); 728741652b0SEtienne Dechamps realaltdir = umem_alloc(MAXPATHLEN, UMEM_NOFAIL); 729741652b0SEtienne Dechamps 730741652b0SEtienne Dechamps VERIFY(NULL != realpath(getexecname(), cmd)); 731420dfc95SChris Siden if (0 != access(altdir, F_OK)) { 732420dfc95SChris Siden ztest_dump_core = B_FALSE; 733420dfc95SChris Siden fatal(B_TRUE, "invalid alternate ztest path: %s", 734420dfc95SChris Siden altdir); 735420dfc95SChris Siden } 736420dfc95SChris Siden VERIFY(NULL != realpath(altdir, realaltdir)); 737420dfc95SChris Siden 738420dfc95SChris Siden /* 739420dfc95SChris Siden * 'cmd' should be of the form "<anything>/usr/bin/<isa>/ztest". 740420dfc95SChris Siden * We want to extract <isa> to determine if we should use 741420dfc95SChris Siden * 32 or 64 bit binaries. 742420dfc95SChris Siden */ 743420dfc95SChris Siden bin = strstr(cmd, "/usr/bin/"); 744420dfc95SChris Siden ztest = strstr(bin, "/ztest"); 745420dfc95SChris Siden isa = bin + 9; 746420dfc95SChris Siden isalen = ztest - isa; 747420dfc95SChris Siden (void) snprintf(zo->zo_alt_ztest, sizeof (zo->zo_alt_ztest), 748420dfc95SChris Siden "%s/usr/bin/%.*s/ztest", realaltdir, isalen, isa); 749420dfc95SChris Siden (void) snprintf(zo->zo_alt_libpath, sizeof (zo->zo_alt_libpath), 750420dfc95SChris Siden "%s/usr/lib/%.*s", realaltdir, isalen, isa); 751420dfc95SChris Siden 752420dfc95SChris Siden if (0 != access(zo->zo_alt_ztest, X_OK)) { 753420dfc95SChris Siden ztest_dump_core = B_FALSE; 754420dfc95SChris Siden fatal(B_TRUE, "invalid alternate ztest: %s", 755420dfc95SChris Siden zo->zo_alt_ztest); 756420dfc95SChris Siden } else if (0 != access(zo->zo_alt_libpath, X_OK)) { 757420dfc95SChris Siden ztest_dump_core = B_FALSE; 758420dfc95SChris Siden fatal(B_TRUE, "invalid alternate lib directory %s", 759420dfc95SChris Siden zo->zo_alt_libpath); 760420dfc95SChris Siden } 761741652b0SEtienne Dechamps 762741652b0SEtienne Dechamps umem_free(cmd, MAXPATHLEN); 763741652b0SEtienne Dechamps umem_free(realaltdir, MAXPATHLEN); 764420dfc95SChris Siden } 765fa9e4066Sahrens } 766fa9e4066Sahrens 767b24ab676SJeff Bonwick static void 768b24ab676SJeff Bonwick ztest_kill(ztest_shared_t *zs) 769b24ab676SJeff Bonwick { 770420dfc95SChris Siden zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(ztest_spa)); 771420dfc95SChris Siden zs->zs_space = metaslab_class_get_space(spa_normal_class(ztest_spa)); 772b4952e17SGeorge Wilson 773b4952e17SGeorge Wilson /* 774b4952e17SGeorge Wilson * Before we kill off ztest, make sure that the config is updated. 775b4952e17SGeorge Wilson * See comment above spa_config_sync(). 776b4952e17SGeorge Wilson */ 777b4952e17SGeorge Wilson mutex_enter(&spa_namespace_lock); 778b4952e17SGeorge Wilson spa_config_sync(ztest_spa, B_FALSE, B_FALSE); 779b4952e17SGeorge Wilson mutex_exit(&spa_namespace_lock); 780b4952e17SGeorge Wilson 781b4952e17SGeorge Wilson zfs_dbgmsg_print(FTAG); 782b24ab676SJeff Bonwick (void) kill(getpid(), SIGKILL); 783b24ab676SJeff Bonwick } 784b24ab676SJeff Bonwick 785b24ab676SJeff Bonwick static uint64_t 786b24ab676SJeff Bonwick ztest_random(uint64_t range) 787b24ab676SJeff Bonwick { 788b24ab676SJeff Bonwick uint64_t r; 789b24ab676SJeff Bonwick 790741652b0SEtienne Dechamps ASSERT3S(ztest_fd_rand, >=, 0); 791741652b0SEtienne Dechamps 792b24ab676SJeff Bonwick if (range == 0) 793b24ab676SJeff Bonwick return (0); 794b24ab676SJeff Bonwick 795741652b0SEtienne Dechamps if (read(ztest_fd_rand, &r, sizeof (r)) != sizeof (r)) 796b24ab676SJeff Bonwick fatal(1, "short read from /dev/urandom"); 797b24ab676SJeff Bonwick 798b24ab676SJeff Bonwick return (r % range); 799b24ab676SJeff Bonwick } 800b24ab676SJeff Bonwick 801b24ab676SJeff Bonwick /* ARGSUSED */ 802b24ab676SJeff Bonwick static void 803b24ab676SJeff Bonwick ztest_record_enospc(const char *s) 804b24ab676SJeff Bonwick { 805b24ab676SJeff Bonwick ztest_shared->zs_enospc_count++; 806b24ab676SJeff Bonwick } 807b24ab676SJeff Bonwick 808ecc2d604Sbonwick static uint64_t 809ecc2d604Sbonwick ztest_get_ashift(void) 810ecc2d604Sbonwick { 811420dfc95SChris Siden if (ztest_opts.zo_ashift == 0) 8122a104a52SAlex Reece return (SPA_MINBLOCKSHIFT + ztest_random(5)); 813420dfc95SChris Siden return (ztest_opts.zo_ashift); 814ecc2d604Sbonwick } 815ecc2d604Sbonwick 816fa9e4066Sahrens static nvlist_t * 81725345e46SGeorge Wilson make_vdev_file(char *path, char *aux, char *pool, size_t size, uint64_t ashift) 818fa9e4066Sahrens { 819e14bb325SJeff Bonwick char pathbuf[MAXPATHLEN]; 820fa9e4066Sahrens uint64_t vdev; 821fa9e4066Sahrens nvlist_t *file; 822fa9e4066Sahrens 823e14bb325SJeff Bonwick if (ashift == 0) 824e14bb325SJeff Bonwick ashift = ztest_get_ashift(); 825e14bb325SJeff Bonwick 826e14bb325SJeff Bonwick if (path == NULL) { 827e14bb325SJeff Bonwick path = pathbuf; 828e14bb325SJeff Bonwick 829e14bb325SJeff Bonwick if (aux != NULL) { 830e14bb325SJeff Bonwick vdev = ztest_shared->zs_vdev_aux; 831420dfc95SChris Siden (void) snprintf(path, sizeof (pathbuf), 832420dfc95SChris Siden ztest_aux_template, ztest_opts.zo_dir, 83325345e46SGeorge Wilson pool == NULL ? ztest_opts.zo_pool : pool, 83425345e46SGeorge Wilson aux, vdev); 835fa9e4066Sahrens } else { 83688ecc943SGeorge Wilson vdev = ztest_shared->zs_vdev_next_leaf++; 837420dfc95SChris Siden (void) snprintf(path, sizeof (pathbuf), 838420dfc95SChris Siden ztest_dev_template, ztest_opts.zo_dir, 83925345e46SGeorge Wilson pool == NULL ? ztest_opts.zo_pool : pool, vdev); 840e14bb325SJeff Bonwick } 841e14bb325SJeff Bonwick } 842fa9e4066Sahrens 843e14bb325SJeff Bonwick if (size != 0) { 844e14bb325SJeff Bonwick int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666); 845fa9e4066Sahrens if (fd == -1) 846e14bb325SJeff Bonwick fatal(1, "can't open %s", path); 847fa9e4066Sahrens if (ftruncate(fd, size) != 0) 848e14bb325SJeff Bonwick fatal(1, "can't ftruncate %s", path); 849fa9e4066Sahrens (void) close(fd); 850fa9e4066Sahrens } 851fa9e4066Sahrens 852fa9e4066Sahrens VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0); 853fa9e4066Sahrens VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0); 854e14bb325SJeff Bonwick VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0); 855ecc2d604Sbonwick VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0); 856fa9e4066Sahrens 857fa9e4066Sahrens return (file); 858fa9e4066Sahrens } 859fa9e4066Sahrens 860fa9e4066Sahrens static nvlist_t * 86125345e46SGeorge Wilson make_vdev_raidz(char *path, char *aux, char *pool, size_t size, 86225345e46SGeorge Wilson uint64_t ashift, int r) 863fa9e4066Sahrens { 864fa9e4066Sahrens nvlist_t *raidz, **child; 865fa9e4066Sahrens int c; 866fa9e4066Sahrens 867fa9e4066Sahrens if (r < 2) 86825345e46SGeorge Wilson return (make_vdev_file(path, aux, pool, size, ashift)); 869fa9e4066Sahrens child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL); 870fa9e4066Sahrens 871fa9e4066Sahrens for (c = 0; c < r; c++) 87225345e46SGeorge Wilson child[c] = make_vdev_file(path, aux, pool, size, ashift); 873fa9e4066Sahrens 874fa9e4066Sahrens VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0); 875fa9e4066Sahrens VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE, 876fa9e4066Sahrens VDEV_TYPE_RAIDZ) == 0); 87799653d4eSeschrock VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY, 878420dfc95SChris Siden ztest_opts.zo_raidz_parity) == 0); 879fa9e4066Sahrens VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN, 880fa9e4066Sahrens child, r) == 0); 881fa9e4066Sahrens 882fa9e4066Sahrens for (c = 0; c < r; c++) 883fa9e4066Sahrens nvlist_free(child[c]); 884fa9e4066Sahrens 885fa9e4066Sahrens umem_free(child, r * sizeof (nvlist_t *)); 886fa9e4066Sahrens 887fa9e4066Sahrens return (raidz); 888fa9e4066Sahrens } 889fa9e4066Sahrens 890fa9e4066Sahrens static nvlist_t * 89125345e46SGeorge Wilson make_vdev_mirror(char *path, char *aux, char *pool, size_t size, 89225345e46SGeorge Wilson uint64_t ashift, int r, int m) 893fa9e4066Sahrens { 894fa9e4066Sahrens nvlist_t *mirror, **child; 895fa9e4066Sahrens int c; 896fa9e4066Sahrens 897fa9e4066Sahrens if (m < 1) 89825345e46SGeorge Wilson return (make_vdev_raidz(path, aux, pool, size, ashift, r)); 899fa9e4066Sahrens 900fa9e4066Sahrens child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL); 901fa9e4066Sahrens 902fa9e4066Sahrens for (c = 0; c < m; c++) 90325345e46SGeorge Wilson child[c] = make_vdev_raidz(path, aux, pool, size, ashift, r); 904fa9e4066Sahrens 905fa9e4066Sahrens VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0); 906fa9e4066Sahrens VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE, 907fa9e4066Sahrens VDEV_TYPE_MIRROR) == 0); 908fa9e4066Sahrens VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN, 909fa9e4066Sahrens child, m) == 0); 910fa9e4066Sahrens 911fa9e4066Sahrens for (c = 0; c < m; c++) 912fa9e4066Sahrens nvlist_free(child[c]); 913fa9e4066Sahrens 914fa9e4066Sahrens umem_free(child, m * sizeof (nvlist_t *)); 915fa9e4066Sahrens 916fa9e4066Sahrens return (mirror); 917fa9e4066Sahrens } 918fa9e4066Sahrens 919fa9e4066Sahrens static nvlist_t * 92025345e46SGeorge Wilson make_vdev_root(char *path, char *aux, char *pool, size_t size, uint64_t ashift, 921e14bb325SJeff Bonwick int log, int r, int m, int t) 922fa9e4066Sahrens { 923fa9e4066Sahrens nvlist_t *root, **child; 924fa9e4066Sahrens int c; 925fa9e4066Sahrens 926fa9e4066Sahrens ASSERT(t > 0); 927fa9e4066Sahrens 928fa9e4066Sahrens child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL); 929fa9e4066Sahrens 93031157203SJeff Bonwick for (c = 0; c < t; c++) { 93125345e46SGeorge Wilson child[c] = make_vdev_mirror(path, aux, pool, size, ashift, 93225345e46SGeorge Wilson r, m); 93331157203SJeff Bonwick VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 93431157203SJeff Bonwick log) == 0); 93531157203SJeff Bonwick } 936fa9e4066Sahrens 937fa9e4066Sahrens VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0); 938fa9e4066Sahrens VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0); 939e14bb325SJeff Bonwick VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN, 940fa9e4066Sahrens child, t) == 0); 941fa9e4066Sahrens 942fa9e4066Sahrens for (c = 0; c < t; c++) 943fa9e4066Sahrens nvlist_free(child[c]); 944fa9e4066Sahrens 945fa9e4066Sahrens umem_free(child, t * sizeof (nvlist_t *)); 946fa9e4066Sahrens 947fa9e4066Sahrens return (root); 948fa9e4066Sahrens } 949fa9e4066Sahrens 95025345e46SGeorge Wilson /* 95125345e46SGeorge Wilson * Find a random spa version. Returns back a random spa version in the 95225345e46SGeorge Wilson * range [initial_version, SPA_VERSION_FEATURES]. 95325345e46SGeorge Wilson */ 95425345e46SGeorge Wilson static uint64_t 95525345e46SGeorge Wilson ztest_random_spa_version(uint64_t initial_version) 95625345e46SGeorge Wilson { 95725345e46SGeorge Wilson uint64_t version = initial_version; 95825345e46SGeorge Wilson 95925345e46SGeorge Wilson if (version <= SPA_VERSION_BEFORE_FEATURES) { 96025345e46SGeorge Wilson version = version + 96125345e46SGeorge Wilson ztest_random(SPA_VERSION_BEFORE_FEATURES - version + 1); 96225345e46SGeorge Wilson } 96325345e46SGeorge Wilson 96425345e46SGeorge Wilson if (version > SPA_VERSION_BEFORE_FEATURES) 96525345e46SGeorge Wilson version = SPA_VERSION_FEATURES; 96625345e46SGeorge Wilson 96725345e46SGeorge Wilson ASSERT(SPA_VERSION_IS_SUPPORTED(version)); 96825345e46SGeorge Wilson return (version); 96925345e46SGeorge Wilson } 97025345e46SGeorge Wilson 971b24ab676SJeff Bonwick static int 972b24ab676SJeff Bonwick ztest_random_blocksize(void) 973fa9e4066Sahrens { 974b5152584SMatthew Ahrens uint64_t block_shift; 975b5152584SMatthew Ahrens /* 976b5152584SMatthew Ahrens * Choose a block size >= the ashift. 977b5152584SMatthew Ahrens * If the SPA supports new MAXBLOCKSIZE, test up to 1MB blocks. 978b5152584SMatthew Ahrens */ 979b5152584SMatthew Ahrens int maxbs = SPA_OLD_MAXBLOCKSHIFT; 980b5152584SMatthew Ahrens if (spa_maxblocksize(ztest_spa) == SPA_MAXBLOCKSIZE) 981b5152584SMatthew Ahrens maxbs = 20; 98281cd5c55SMatthew Ahrens block_shift = ztest_random(maxbs - ztest_spa->spa_max_ashift + 1); 9832a104a52SAlex Reece return (1 << (SPA_MINBLOCKSHIFT + block_shift)); 984fa9e4066Sahrens } 985fa9e4066Sahrens 986b24ab676SJeff Bonwick static int 987b24ab676SJeff Bonwick ztest_random_ibshift(void) 988fa9e4066Sahrens { 989b24ab676SJeff Bonwick return (DN_MIN_INDBLKSHIFT + 990b24ab676SJeff Bonwick ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1)); 991b24ab676SJeff Bonwick } 992b24ab676SJeff Bonwick 993b24ab676SJeff Bonwick static uint64_t 994b24ab676SJeff Bonwick ztest_random_vdev_top(spa_t *spa, boolean_t log_ok) 995b24ab676SJeff Bonwick { 996b24ab676SJeff Bonwick uint64_t top; 997b24ab676SJeff Bonwick vdev_t *rvd = spa->spa_root_vdev; 998b24ab676SJeff Bonwick vdev_t *tvd; 999b24ab676SJeff Bonwick 1000b24ab676SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 1001fa9e4066Sahrens 1002fa9e4066Sahrens do { 1003b24ab676SJeff Bonwick top = ztest_random(rvd->vdev_children); 1004b24ab676SJeff Bonwick tvd = rvd->vdev_child[top]; 1005b24ab676SJeff Bonwick } while (tvd->vdev_ishole || (tvd->vdev_islog && !log_ok) || 1006b24ab676SJeff Bonwick tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL); 1007fa9e4066Sahrens 1008b24ab676SJeff Bonwick return (top); 1009fa9e4066Sahrens } 1010fa9e4066Sahrens 1011b24ab676SJeff Bonwick static uint64_t 1012b24ab676SJeff Bonwick ztest_random_dsl_prop(zfs_prop_t prop) 1013fa9e4066Sahrens { 1014b24ab676SJeff Bonwick uint64_t value; 1015b24ab676SJeff Bonwick 1016b24ab676SJeff Bonwick do { 1017b24ab676SJeff Bonwick value = zfs_prop_random_value(prop, ztest_random(-1ULL)); 1018b24ab676SJeff Bonwick } while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF); 1019b24ab676SJeff Bonwick 1020b24ab676SJeff Bonwick return (value); 1021fa9e4066Sahrens } 1022fa9e4066Sahrens 1023fa9e4066Sahrens static int 1024b24ab676SJeff Bonwick ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value, 1025b24ab676SJeff Bonwick boolean_t inherit) 1026fa9e4066Sahrens { 1027b24ab676SJeff Bonwick const char *propname = zfs_prop_to_name(prop); 1028b24ab676SJeff Bonwick const char *valname; 1029b24ab676SJeff Bonwick char setpoint[MAXPATHLEN]; 1030b24ab676SJeff Bonwick uint64_t curval; 1031fa9e4066Sahrens int error; 1032fa9e4066Sahrens 10333b2aab18SMatthew Ahrens error = dsl_prop_set_int(osname, propname, 10343b2aab18SMatthew Ahrens (inherit ? ZPROP_SRC_NONE : ZPROP_SRC_LOCAL), value); 1035fa9e4066Sahrens 1036b24ab676SJeff Bonwick if (error == ENOSPC) { 1037b24ab676SJeff Bonwick ztest_record_enospc(FTAG); 1038fa9e4066Sahrens return (error); 1039fa9e4066Sahrens } 1040fb09f5aaSMadhav Suresh ASSERT0(error); 1041fa9e4066Sahrens 10423b2aab18SMatthew Ahrens VERIFY0(dsl_prop_get_integer(osname, propname, &curval, setpoint)); 1043b24ab676SJeff Bonwick 1044420dfc95SChris Siden if (ztest_opts.zo_verbose >= 6) { 1045b24ab676SJeff Bonwick VERIFY(zfs_prop_index_to_string(prop, curval, &valname) == 0); 1046b24ab676SJeff Bonwick (void) printf("%s %s = %s at '%s'\n", 1047b24ab676SJeff Bonwick osname, propname, valname, setpoint); 1048fa9e4066Sahrens } 1049fa9e4066Sahrens 1050fa9e4066Sahrens return (error); 1051fa9e4066Sahrens } 1052fa9e4066Sahrens 1053fa9e4066Sahrens static int 1054420dfc95SChris Siden ztest_spa_prop_set_uint64(zpool_prop_t prop, uint64_t value) 1055fa9e4066Sahrens { 1056420dfc95SChris Siden spa_t *spa = ztest_spa; 1057b24ab676SJeff Bonwick nvlist_t *props = NULL; 1058fa9e4066Sahrens int error; 1059fa9e4066Sahrens 1060b24ab676SJeff Bonwick VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0); 1061b24ab676SJeff Bonwick VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0); 1062b24ab676SJeff Bonwick 1063b24ab676SJeff Bonwick error = spa_prop_set(spa, props); 1064b24ab676SJeff Bonwick 1065b24ab676SJeff Bonwick nvlist_free(props); 1066b24ab676SJeff Bonwick 1067b24ab676SJeff Bonwick if (error == ENOSPC) { 1068b24ab676SJeff Bonwick ztest_record_enospc(FTAG); 1069b24ab676SJeff Bonwick return (error); 1070b24ab676SJeff Bonwick } 1071fb09f5aaSMadhav Suresh ASSERT0(error); 1072b24ab676SJeff Bonwick 1073b24ab676SJeff Bonwick return (error); 1074b24ab676SJeff Bonwick } 1075b24ab676SJeff Bonwick 1076b24ab676SJeff Bonwick static void 1077b24ab676SJeff Bonwick ztest_rll_init(rll_t *rll) 1078b24ab676SJeff Bonwick { 1079b24ab676SJeff Bonwick rll->rll_writer = NULL; 1080b24ab676SJeff Bonwick rll->rll_readers = 0; 1081b24ab676SJeff Bonwick VERIFY(_mutex_init(&rll->rll_lock, USYNC_THREAD, NULL) == 0); 1082b24ab676SJeff Bonwick VERIFY(cond_init(&rll->rll_cv, USYNC_THREAD, NULL) == 0); 1083b24ab676SJeff Bonwick } 1084b24ab676SJeff Bonwick 1085b24ab676SJeff Bonwick static void 1086b24ab676SJeff Bonwick ztest_rll_destroy(rll_t *rll) 1087b24ab676SJeff Bonwick { 1088b24ab676SJeff Bonwick ASSERT(rll->rll_writer == NULL); 1089b24ab676SJeff Bonwick ASSERT(rll->rll_readers == 0); 1090b24ab676SJeff Bonwick VERIFY(_mutex_destroy(&rll->rll_lock) == 0); 1091b24ab676SJeff Bonwick VERIFY(cond_destroy(&rll->rll_cv) == 0); 1092b24ab676SJeff Bonwick } 1093b24ab676SJeff Bonwick 1094b24ab676SJeff Bonwick static void 1095b24ab676SJeff Bonwick ztest_rll_lock(rll_t *rll, rl_type_t type) 1096b24ab676SJeff Bonwick { 1097b24ab676SJeff Bonwick VERIFY(mutex_lock(&rll->rll_lock) == 0); 1098b24ab676SJeff Bonwick 1099b24ab676SJeff Bonwick if (type == RL_READER) { 1100b24ab676SJeff Bonwick while (rll->rll_writer != NULL) 1101b24ab676SJeff Bonwick (void) cond_wait(&rll->rll_cv, &rll->rll_lock); 1102b24ab676SJeff Bonwick rll->rll_readers++; 1103b24ab676SJeff Bonwick } else { 1104b24ab676SJeff Bonwick while (rll->rll_writer != NULL || rll->rll_readers) 1105b24ab676SJeff Bonwick (void) cond_wait(&rll->rll_cv, &rll->rll_lock); 1106b24ab676SJeff Bonwick rll->rll_writer = curthread; 1107b24ab676SJeff Bonwick } 1108b24ab676SJeff Bonwick 1109b24ab676SJeff Bonwick VERIFY(mutex_unlock(&rll->rll_lock) == 0); 1110b24ab676SJeff Bonwick } 1111b24ab676SJeff Bonwick 1112b24ab676SJeff Bonwick static void 1113b24ab676SJeff Bonwick ztest_rll_unlock(rll_t *rll) 1114b24ab676SJeff Bonwick { 1115b24ab676SJeff Bonwick VERIFY(mutex_lock(&rll->rll_lock) == 0); 1116b24ab676SJeff Bonwick 1117b24ab676SJeff Bonwick if (rll->rll_writer) { 1118b24ab676SJeff Bonwick ASSERT(rll->rll_readers == 0); 1119b24ab676SJeff Bonwick rll->rll_writer = NULL; 1120b24ab676SJeff Bonwick } else { 1121b24ab676SJeff Bonwick ASSERT(rll->rll_readers != 0); 1122b24ab676SJeff Bonwick ASSERT(rll->rll_writer == NULL); 1123b24ab676SJeff Bonwick rll->rll_readers--; 1124b24ab676SJeff Bonwick } 1125b24ab676SJeff Bonwick 1126b24ab676SJeff Bonwick if (rll->rll_writer == NULL && rll->rll_readers == 0) 1127b24ab676SJeff Bonwick VERIFY(cond_broadcast(&rll->rll_cv) == 0); 1128b24ab676SJeff Bonwick 1129b24ab676SJeff Bonwick VERIFY(mutex_unlock(&rll->rll_lock) == 0); 1130b24ab676SJeff Bonwick } 1131b24ab676SJeff Bonwick 1132b24ab676SJeff Bonwick static void 1133b24ab676SJeff Bonwick ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type) 1134b24ab676SJeff Bonwick { 1135b24ab676SJeff Bonwick rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)]; 1136b24ab676SJeff Bonwick 1137b24ab676SJeff Bonwick ztest_rll_lock(rll, type); 1138b24ab676SJeff Bonwick } 1139b24ab676SJeff Bonwick 1140b24ab676SJeff Bonwick static void 1141b24ab676SJeff Bonwick ztest_object_unlock(ztest_ds_t *zd, uint64_t object) 1142b24ab676SJeff Bonwick { 1143b24ab676SJeff Bonwick rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)]; 1144b24ab676SJeff Bonwick 1145b24ab676SJeff Bonwick ztest_rll_unlock(rll); 1146b24ab676SJeff Bonwick } 1147b24ab676SJeff Bonwick 1148b24ab676SJeff Bonwick static rl_t * 1149b24ab676SJeff Bonwick ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset, 1150b24ab676SJeff Bonwick uint64_t size, rl_type_t type) 1151b24ab676SJeff Bonwick { 1152b24ab676SJeff Bonwick uint64_t hash = object ^ (offset % (ZTEST_RANGE_LOCKS + 1)); 1153b24ab676SJeff Bonwick rll_t *rll = &zd->zd_range_lock[hash & (ZTEST_RANGE_LOCKS - 1)]; 1154b24ab676SJeff Bonwick rl_t *rl; 1155b24ab676SJeff Bonwick 1156b24ab676SJeff Bonwick rl = umem_alloc(sizeof (*rl), UMEM_NOFAIL); 1157b24ab676SJeff Bonwick rl->rl_object = object; 1158b24ab676SJeff Bonwick rl->rl_offset = offset; 1159b24ab676SJeff Bonwick rl->rl_size = size; 1160b24ab676SJeff Bonwick rl->rl_lock = rll; 1161b24ab676SJeff Bonwick 1162b24ab676SJeff Bonwick ztest_rll_lock(rll, type); 1163b24ab676SJeff Bonwick 1164b24ab676SJeff Bonwick return (rl); 1165b24ab676SJeff Bonwick } 1166b24ab676SJeff Bonwick 1167b24ab676SJeff Bonwick static void 1168b24ab676SJeff Bonwick ztest_range_unlock(rl_t *rl) 1169b24ab676SJeff Bonwick { 1170b24ab676SJeff Bonwick rll_t *rll = rl->rl_lock; 1171b24ab676SJeff Bonwick 1172b24ab676SJeff Bonwick ztest_rll_unlock(rll); 1173b24ab676SJeff Bonwick 1174b24ab676SJeff Bonwick umem_free(rl, sizeof (*rl)); 1175b24ab676SJeff Bonwick } 1176b24ab676SJeff Bonwick 1177b24ab676SJeff Bonwick static void 1178420dfc95SChris Siden ztest_zd_init(ztest_ds_t *zd, ztest_shared_ds_t *szd, objset_t *os) 1179b24ab676SJeff Bonwick { 1180b24ab676SJeff Bonwick zd->zd_os = os; 1181b24ab676SJeff Bonwick zd->zd_zilog = dmu_objset_zil(os); 1182420dfc95SChris Siden zd->zd_shared = szd; 1183b24ab676SJeff Bonwick dmu_objset_name(os, zd->zd_name); 1184b24ab676SJeff Bonwick 1185420dfc95SChris Siden if (zd->zd_shared != NULL) 1186420dfc95SChris Siden zd->zd_shared->zd_seq = 0; 1187420dfc95SChris Siden 1188c9ba2a43SEric Schrock VERIFY(rwlock_init(&zd->zd_zilog_lock, USYNC_THREAD, NULL) == 0); 1189b24ab676SJeff Bonwick VERIFY(_mutex_init(&zd->zd_dirobj_lock, USYNC_THREAD, NULL) == 0); 1190b24ab676SJeff Bonwick 1191b24ab676SJeff Bonwick for (int l = 0; l < ZTEST_OBJECT_LOCKS; l++) 1192b24ab676SJeff Bonwick ztest_rll_init(&zd->zd_object_lock[l]); 1193b24ab676SJeff Bonwick 1194b24ab676SJeff Bonwick for (int l = 0; l < ZTEST_RANGE_LOCKS; l++) 1195b24ab676SJeff Bonwick ztest_rll_init(&zd->zd_range_lock[l]); 1196b24ab676SJeff Bonwick } 1197b24ab676SJeff Bonwick 1198b24ab676SJeff Bonwick static void 1199b24ab676SJeff Bonwick ztest_zd_fini(ztest_ds_t *zd) 1200b24ab676SJeff Bonwick { 1201b24ab676SJeff Bonwick VERIFY(_mutex_destroy(&zd->zd_dirobj_lock) == 0); 1202b24ab676SJeff Bonwick 1203b24ab676SJeff Bonwick for (int l = 0; l < ZTEST_OBJECT_LOCKS; l++) 1204b24ab676SJeff Bonwick ztest_rll_destroy(&zd->zd_object_lock[l]); 1205b24ab676SJeff Bonwick 1206b24ab676SJeff Bonwick for (int l = 0; l < ZTEST_RANGE_LOCKS; l++) 1207b24ab676SJeff Bonwick ztest_rll_destroy(&zd->zd_range_lock[l]); 1208b24ab676SJeff Bonwick } 1209b24ab676SJeff Bonwick 1210b24ab676SJeff Bonwick #define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT) 1211b24ab676SJeff Bonwick 1212b24ab676SJeff Bonwick static uint64_t 1213b24ab676SJeff Bonwick ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag) 1214b24ab676SJeff Bonwick { 1215b24ab676SJeff Bonwick uint64_t txg; 1216b24ab676SJeff Bonwick int error; 1217b24ab676SJeff Bonwick 1218b24ab676SJeff Bonwick /* 1219b24ab676SJeff Bonwick * Attempt to assign tx to some transaction group. 1220b24ab676SJeff Bonwick */ 1221b24ab676SJeff Bonwick error = dmu_tx_assign(tx, txg_how); 1222b24ab676SJeff Bonwick if (error) { 1223b24ab676SJeff Bonwick if (error == ERESTART) { 1224b24ab676SJeff Bonwick ASSERT(txg_how == TXG_NOWAIT); 1225b24ab676SJeff Bonwick dmu_tx_wait(tx); 1226b24ab676SJeff Bonwick } else { 1227b24ab676SJeff Bonwick ASSERT3U(error, ==, ENOSPC); 1228b24ab676SJeff Bonwick ztest_record_enospc(tag); 1229b24ab676SJeff Bonwick } 1230b24ab676SJeff Bonwick dmu_tx_abort(tx); 1231b24ab676SJeff Bonwick return (0); 1232b24ab676SJeff Bonwick } 1233b24ab676SJeff Bonwick txg = dmu_tx_get_txg(tx); 1234b24ab676SJeff Bonwick ASSERT(txg != 0); 1235b24ab676SJeff Bonwick return (txg); 1236b24ab676SJeff Bonwick } 1237b24ab676SJeff Bonwick 1238b24ab676SJeff Bonwick static void 1239b24ab676SJeff Bonwick ztest_pattern_set(void *buf, uint64_t size, uint64_t value) 1240b24ab676SJeff Bonwick { 1241b24ab676SJeff Bonwick uint64_t *ip = buf; 1242b24ab676SJeff Bonwick uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size); 1243b24ab676SJeff Bonwick 1244b24ab676SJeff Bonwick while (ip < ip_end) 1245b24ab676SJeff Bonwick *ip++ = value; 1246b24ab676SJeff Bonwick } 1247b24ab676SJeff Bonwick 1248b24ab676SJeff Bonwick static boolean_t 1249b24ab676SJeff Bonwick ztest_pattern_match(void *buf, uint64_t size, uint64_t value) 1250b24ab676SJeff Bonwick { 1251b24ab676SJeff Bonwick uint64_t *ip = buf; 1252b24ab676SJeff Bonwick uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size); 1253b24ab676SJeff Bonwick uint64_t diff = 0; 1254b24ab676SJeff Bonwick 1255b24ab676SJeff Bonwick while (ip < ip_end) 1256b24ab676SJeff Bonwick diff |= (value - *ip++); 1257b24ab676SJeff Bonwick 1258b24ab676SJeff Bonwick return (diff == 0); 1259b24ab676SJeff Bonwick } 1260b24ab676SJeff Bonwick 1261b24ab676SJeff Bonwick static void 1262b24ab676SJeff Bonwick ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object, 1263b24ab676SJeff Bonwick uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg) 1264b24ab676SJeff Bonwick { 1265b24ab676SJeff Bonwick bt->bt_magic = BT_MAGIC; 1266b24ab676SJeff Bonwick bt->bt_objset = dmu_objset_id(os); 1267b24ab676SJeff Bonwick bt->bt_object = object; 1268b24ab676SJeff Bonwick bt->bt_offset = offset; 1269b24ab676SJeff Bonwick bt->bt_gen = gen; 1270b24ab676SJeff Bonwick bt->bt_txg = txg; 1271b24ab676SJeff Bonwick bt->bt_crtxg = crtxg; 1272b24ab676SJeff Bonwick } 1273b24ab676SJeff Bonwick 1274b24ab676SJeff Bonwick static void 1275b24ab676SJeff Bonwick ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object, 1276b24ab676SJeff Bonwick uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg) 1277b24ab676SJeff Bonwick { 12785d7b4d43SMatthew Ahrens ASSERT3U(bt->bt_magic, ==, BT_MAGIC); 12795d7b4d43SMatthew Ahrens ASSERT3U(bt->bt_objset, ==, dmu_objset_id(os)); 12805d7b4d43SMatthew Ahrens ASSERT3U(bt->bt_object, ==, object); 12815d7b4d43SMatthew Ahrens ASSERT3U(bt->bt_offset, ==, offset); 12825d7b4d43SMatthew Ahrens ASSERT3U(bt->bt_gen, <=, gen); 12835d7b4d43SMatthew Ahrens ASSERT3U(bt->bt_txg, <=, txg); 12845d7b4d43SMatthew Ahrens ASSERT3U(bt->bt_crtxg, ==, crtxg); 1285b24ab676SJeff Bonwick } 1286b24ab676SJeff Bonwick 1287b24ab676SJeff Bonwick static ztest_block_tag_t * 1288b24ab676SJeff Bonwick ztest_bt_bonus(dmu_buf_t *db) 1289b24ab676SJeff Bonwick { 1290b24ab676SJeff Bonwick dmu_object_info_t doi; 1291b24ab676SJeff Bonwick ztest_block_tag_t *bt; 1292b24ab676SJeff Bonwick 1293b24ab676SJeff Bonwick dmu_object_info_from_db(db, &doi); 1294b24ab676SJeff Bonwick ASSERT3U(doi.doi_bonus_size, <=, db->db_size); 1295b24ab676SJeff Bonwick ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt)); 1296b24ab676SJeff Bonwick bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt)); 1297b24ab676SJeff Bonwick 1298b24ab676SJeff Bonwick return (bt); 1299b24ab676SJeff Bonwick } 1300b24ab676SJeff Bonwick 1301b24ab676SJeff Bonwick /* 1302b24ab676SJeff Bonwick * ZIL logging ops 1303b24ab676SJeff Bonwick */ 1304b24ab676SJeff Bonwick 1305b24ab676SJeff Bonwick #define lrz_type lr_mode 1306b24ab676SJeff Bonwick #define lrz_blocksize lr_uid 1307b24ab676SJeff Bonwick #define lrz_ibshift lr_gid 1308b24ab676SJeff Bonwick #define lrz_bonustype lr_rdev 1309b24ab676SJeff Bonwick #define lrz_bonuslen lr_crtime[1] 1310b24ab676SJeff Bonwick 13115002558fSNeil Perrin static void 1312b24ab676SJeff Bonwick ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr) 1313b24ab676SJeff Bonwick { 1314b24ab676SJeff Bonwick char *name = (void *)(lr + 1); /* name follows lr */ 1315b24ab676SJeff Bonwick size_t namesize = strlen(name) + 1; 1316b24ab676SJeff Bonwick itx_t *itx; 1317b24ab676SJeff Bonwick 1318b24ab676SJeff Bonwick if (zil_replaying(zd->zd_zilog, tx)) 13195002558fSNeil Perrin return; 1320b24ab676SJeff Bonwick 1321b24ab676SJeff Bonwick itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize); 1322b24ab676SJeff Bonwick bcopy(&lr->lr_common + 1, &itx->itx_lr + 1, 1323b24ab676SJeff Bonwick sizeof (*lr) + namesize - sizeof (lr_t)); 1324b24ab676SJeff Bonwick 13255002558fSNeil Perrin zil_itx_assign(zd->zd_zilog, itx, tx); 1326b24ab676SJeff Bonwick } 1327b24ab676SJeff Bonwick 13285002558fSNeil Perrin static void 13295002558fSNeil Perrin ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr, uint64_t object) 1330b24ab676SJeff Bonwick { 1331b24ab676SJeff Bonwick char *name = (void *)(lr + 1); /* name follows lr */ 1332b24ab676SJeff Bonwick size_t namesize = strlen(name) + 1; 1333b24ab676SJeff Bonwick itx_t *itx; 1334b24ab676SJeff Bonwick 1335b24ab676SJeff Bonwick if (zil_replaying(zd->zd_zilog, tx)) 13365002558fSNeil Perrin return; 1337b24ab676SJeff Bonwick 1338b24ab676SJeff Bonwick itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize); 1339b24ab676SJeff Bonwick bcopy(&lr->lr_common + 1, &itx->itx_lr + 1, 1340b24ab676SJeff Bonwick sizeof (*lr) + namesize - sizeof (lr_t)); 1341b24ab676SJeff Bonwick 134251bd2f97SNeil Perrin itx->itx_oid = object; 13435002558fSNeil Perrin zil_itx_assign(zd->zd_zilog, itx, tx); 1344b24ab676SJeff Bonwick } 1345b24ab676SJeff Bonwick 13465002558fSNeil Perrin static void 1347b24ab676SJeff Bonwick ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr) 1348b24ab676SJeff Bonwick { 1349b24ab676SJeff Bonwick itx_t *itx; 1350b24ab676SJeff Bonwick itx_wr_state_t write_state = ztest_random(WR_NUM_STATES); 1351b24ab676SJeff Bonwick 1352b24ab676SJeff Bonwick if (zil_replaying(zd->zd_zilog, tx)) 13535002558fSNeil Perrin return; 1354b24ab676SJeff Bonwick 1355b24ab676SJeff Bonwick if (lr->lr_length > ZIL_MAX_LOG_DATA) 1356b24ab676SJeff Bonwick write_state = WR_INDIRECT; 1357b24ab676SJeff Bonwick 1358b24ab676SJeff Bonwick itx = zil_itx_create(TX_WRITE, 1359b24ab676SJeff Bonwick sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0)); 1360b24ab676SJeff Bonwick 1361b24ab676SJeff Bonwick if (write_state == WR_COPIED && 1362b24ab676SJeff Bonwick dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length, 1363b24ab676SJeff Bonwick ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) { 1364b24ab676SJeff Bonwick zil_itx_destroy(itx); 1365b24ab676SJeff Bonwick itx = zil_itx_create(TX_WRITE, sizeof (*lr)); 1366b24ab676SJeff Bonwick write_state = WR_NEED_COPY; 1367b24ab676SJeff Bonwick } 1368b24ab676SJeff Bonwick itx->itx_private = zd; 1369b24ab676SJeff Bonwick itx->itx_wr_state = write_state; 1370b24ab676SJeff Bonwick itx->itx_sync = (ztest_random(8) == 0); 1371b24ab676SJeff Bonwick itx->itx_sod += (write_state == WR_NEED_COPY ? lr->lr_length : 0); 1372b24ab676SJeff Bonwick 1373b24ab676SJeff Bonwick bcopy(&lr->lr_common + 1, &itx->itx_lr + 1, 1374b24ab676SJeff Bonwick sizeof (*lr) - sizeof (lr_t)); 1375b24ab676SJeff Bonwick 13765002558fSNeil Perrin zil_itx_assign(zd->zd_zilog, itx, tx); 1377b24ab676SJeff Bonwick } 1378b24ab676SJeff Bonwick 13795002558fSNeil Perrin static void 1380b24ab676SJeff Bonwick ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr) 1381b24ab676SJeff Bonwick { 1382b24ab676SJeff Bonwick itx_t *itx; 1383b24ab676SJeff Bonwick 1384b24ab676SJeff Bonwick if (zil_replaying(zd->zd_zilog, tx)) 13855002558fSNeil Perrin return; 1386b24ab676SJeff Bonwick 1387b24ab676SJeff Bonwick itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr)); 1388b24ab676SJeff Bonwick bcopy(&lr->lr_common + 1, &itx->itx_lr + 1, 1389b24ab676SJeff Bonwick sizeof (*lr) - sizeof (lr_t)); 1390b24ab676SJeff Bonwick 13915002558fSNeil Perrin itx->itx_sync = B_FALSE; 13925002558fSNeil Perrin zil_itx_assign(zd->zd_zilog, itx, tx); 1393b24ab676SJeff Bonwick } 1394b24ab676SJeff Bonwick 13955002558fSNeil Perrin static void 1396b24ab676SJeff Bonwick ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr) 1397b24ab676SJeff Bonwick { 1398b24ab676SJeff Bonwick itx_t *itx; 1399b24ab676SJeff Bonwick 1400b24ab676SJeff Bonwick if (zil_replaying(zd->zd_zilog, tx)) 14015002558fSNeil Perrin return; 1402b24ab676SJeff Bonwick 1403b24ab676SJeff Bonwick itx = zil_itx_create(TX_SETATTR, sizeof (*lr)); 1404b24ab676SJeff Bonwick bcopy(&lr->lr_common + 1, &itx->itx_lr + 1, 1405b24ab676SJeff Bonwick sizeof (*lr) - sizeof (lr_t)); 1406b24ab676SJeff Bonwick 14075002558fSNeil Perrin itx->itx_sync = B_FALSE; 14085002558fSNeil Perrin zil_itx_assign(zd->zd_zilog, itx, tx); 1409b24ab676SJeff Bonwick } 1410b24ab676SJeff Bonwick 1411b24ab676SJeff Bonwick /* 1412b24ab676SJeff Bonwick * ZIL replay ops 1413b24ab676SJeff Bonwick */ 1414b24ab676SJeff Bonwick static int 1415b24ab676SJeff Bonwick ztest_replay_create(ztest_ds_t *zd, lr_create_t *lr, boolean_t byteswap) 1416b24ab676SJeff Bonwick { 1417b24ab676SJeff Bonwick char *name = (void *)(lr + 1); /* name follows lr */ 1418b24ab676SJeff Bonwick objset_t *os = zd->zd_os; 1419b24ab676SJeff Bonwick ztest_block_tag_t *bbt; 1420b24ab676SJeff Bonwick dmu_buf_t *db; 1421b24ab676SJeff Bonwick dmu_tx_t *tx; 1422b24ab676SJeff Bonwick uint64_t txg; 1423b24ab676SJeff Bonwick int error = 0; 1424b24ab676SJeff Bonwick 1425fa9e4066Sahrens if (byteswap) 1426fa9e4066Sahrens byteswap_uint64_array(lr, sizeof (*lr)); 1427fa9e4066Sahrens 1428b24ab676SJeff Bonwick ASSERT(lr->lr_doid == ZTEST_DIROBJ); 1429b24ab676SJeff Bonwick ASSERT(name[0] != '\0'); 1430b24ab676SJeff Bonwick 1431fa9e4066Sahrens tx = dmu_tx_create(os); 1432b24ab676SJeff Bonwick 1433b24ab676SJeff Bonwick dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name); 1434b24ab676SJeff Bonwick 1435b24ab676SJeff Bonwick if (lr->lrz_type == DMU_OT_ZAP_OTHER) { 1436b24ab676SJeff Bonwick dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL); 1437b24ab676SJeff Bonwick } else { 1438b24ab676SJeff Bonwick dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 1439b24ab676SJeff Bonwick } 1440b24ab676SJeff Bonwick 1441b24ab676SJeff Bonwick txg = ztest_tx_assign(tx, TXG_WAIT, FTAG); 1442b24ab676SJeff Bonwick if (txg == 0) 1443b24ab676SJeff Bonwick return (ENOSPC); 1444b24ab676SJeff Bonwick 1445b24ab676SJeff Bonwick ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid); 1446b24ab676SJeff Bonwick 1447b24ab676SJeff Bonwick if (lr->lrz_type == DMU_OT_ZAP_OTHER) { 1448b24ab676SJeff Bonwick if (lr->lr_foid == 0) { 1449b24ab676SJeff Bonwick lr->lr_foid = zap_create(os, 1450b24ab676SJeff Bonwick lr->lrz_type, lr->lrz_bonustype, 1451b24ab676SJeff Bonwick lr->lrz_bonuslen, tx); 1452b24ab676SJeff Bonwick } else { 1453b24ab676SJeff Bonwick error = zap_create_claim(os, lr->lr_foid, 1454b24ab676SJeff Bonwick lr->lrz_type, lr->lrz_bonustype, 1455b24ab676SJeff Bonwick lr->lrz_bonuslen, tx); 1456b24ab676SJeff Bonwick } 1457b24ab676SJeff Bonwick } else { 1458b24ab676SJeff Bonwick if (lr->lr_foid == 0) { 1459b24ab676SJeff Bonwick lr->lr_foid = dmu_object_alloc(os, 1460b24ab676SJeff Bonwick lr->lrz_type, 0, lr->lrz_bonustype, 1461b24ab676SJeff Bonwick lr->lrz_bonuslen, tx); 1462b24ab676SJeff Bonwick } else { 1463b24ab676SJeff Bonwick error = dmu_object_claim(os, lr->lr_foid, 1464b24ab676SJeff Bonwick lr->lrz_type, 0, lr->lrz_bonustype, 1465b24ab676SJeff Bonwick lr->lrz_bonuslen, tx); 1466b24ab676SJeff Bonwick } 1467b24ab676SJeff Bonwick } 1468b24ab676SJeff Bonwick 1469fa9e4066Sahrens if (error) { 1470b24ab676SJeff Bonwick ASSERT3U(error, ==, EEXIST); 1471b24ab676SJeff Bonwick ASSERT(zd->zd_zilog->zl_replay); 1472b24ab676SJeff Bonwick dmu_tx_commit(tx); 1473fa9e4066Sahrens return (error); 1474fa9e4066Sahrens } 1475fa9e4066Sahrens 1476b24ab676SJeff Bonwick ASSERT(lr->lr_foid != 0); 1477b24ab676SJeff Bonwick 1478b24ab676SJeff Bonwick if (lr->lrz_type != DMU_OT_ZAP_OTHER) 1479b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid, 1480b24ab676SJeff Bonwick lr->lrz_blocksize, lr->lrz_ibshift, tx)); 1481b24ab676SJeff Bonwick 1482b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db)); 1483b24ab676SJeff Bonwick bbt = ztest_bt_bonus(db); 1484b24ab676SJeff Bonwick dmu_buf_will_dirty(db, tx); 1485b24ab676SJeff Bonwick ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_gen, txg, txg); 1486b24ab676SJeff Bonwick dmu_buf_rele(db, FTAG); 1487b24ab676SJeff Bonwick 1488b420f3adSRichard Lowe VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1, 1489b24ab676SJeff Bonwick &lr->lr_foid, tx)); 1490b24ab676SJeff Bonwick 1491b24ab676SJeff Bonwick (void) ztest_log_create(zd, tx, lr); 1492b24ab676SJeff Bonwick 1493fa9e4066Sahrens dmu_tx_commit(tx); 1494fa9e4066Sahrens 1495b24ab676SJeff Bonwick return (0); 1496b24ab676SJeff Bonwick } 1497b24ab676SJeff Bonwick 1498b24ab676SJeff Bonwick static int 1499b24ab676SJeff Bonwick ztest_replay_remove(ztest_ds_t *zd, lr_remove_t *lr, boolean_t byteswap) 1500b24ab676SJeff Bonwick { 1501b24ab676SJeff Bonwick char *name = (void *)(lr + 1); /* name follows lr */ 1502b24ab676SJeff Bonwick objset_t *os = zd->zd_os; 1503b24ab676SJeff Bonwick dmu_object_info_t doi; 1504b24ab676SJeff Bonwick dmu_tx_t *tx; 1505b24ab676SJeff Bonwick uint64_t object, txg; 1506b24ab676SJeff Bonwick 1507b24ab676SJeff Bonwick if (byteswap) 1508b24ab676SJeff Bonwick byteswap_uint64_array(lr, sizeof (*lr)); 1509b24ab676SJeff Bonwick 1510b24ab676SJeff Bonwick ASSERT(lr->lr_doid == ZTEST_DIROBJ); 1511b24ab676SJeff Bonwick ASSERT(name[0] != '\0'); 1512b24ab676SJeff Bonwick 1513b420f3adSRichard Lowe VERIFY3U(0, ==, 1514b24ab676SJeff Bonwick zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object)); 1515b24ab676SJeff Bonwick ASSERT(object != 0); 1516b24ab676SJeff Bonwick 1517b24ab676SJeff Bonwick ztest_object_lock(zd, object, RL_WRITER); 1518b24ab676SJeff Bonwick 1519b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_object_info(os, object, &doi)); 1520b24ab676SJeff Bonwick 1521b24ab676SJeff Bonwick tx = dmu_tx_create(os); 1522b24ab676SJeff Bonwick 1523b24ab676SJeff Bonwick dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name); 1524b24ab676SJeff Bonwick dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END); 1525b24ab676SJeff Bonwick 1526b24ab676SJeff Bonwick txg = ztest_tx_assign(tx, TXG_WAIT, FTAG); 1527b24ab676SJeff Bonwick if (txg == 0) { 1528b24ab676SJeff Bonwick ztest_object_unlock(zd, object); 1529b24ab676SJeff Bonwick return (ENOSPC); 1530b24ab676SJeff Bonwick } 1531b24ab676SJeff Bonwick 1532b24ab676SJeff Bonwick if (doi.doi_type == DMU_OT_ZAP_OTHER) { 1533b420f3adSRichard Lowe VERIFY3U(0, ==, zap_destroy(os, object, tx)); 1534b24ab676SJeff Bonwick } else { 1535b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_object_free(os, object, tx)); 1536b24ab676SJeff Bonwick } 1537b24ab676SJeff Bonwick 1538b420f3adSRichard Lowe VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx)); 1539b24ab676SJeff Bonwick 15405002558fSNeil Perrin (void) ztest_log_remove(zd, tx, lr, object); 1541b24ab676SJeff Bonwick 1542b24ab676SJeff Bonwick dmu_tx_commit(tx); 1543b24ab676SJeff Bonwick 1544b24ab676SJeff Bonwick ztest_object_unlock(zd, object); 1545b24ab676SJeff Bonwick 1546b24ab676SJeff Bonwick return (0); 1547b24ab676SJeff Bonwick } 1548b24ab676SJeff Bonwick 1549b24ab676SJeff Bonwick static int 1550b24ab676SJeff Bonwick ztest_replay_write(ztest_ds_t *zd, lr_write_t *lr, boolean_t byteswap) 1551b24ab676SJeff Bonwick { 1552b24ab676SJeff Bonwick objset_t *os = zd->zd_os; 1553b24ab676SJeff Bonwick void *data = lr + 1; /* data follows lr */ 1554b24ab676SJeff Bonwick uint64_t offset, length; 1555b24ab676SJeff Bonwick ztest_block_tag_t *bt = data; 1556b24ab676SJeff Bonwick ztest_block_tag_t *bbt; 1557b24ab676SJeff Bonwick uint64_t gen, txg, lrtxg, crtxg; 1558b24ab676SJeff Bonwick dmu_object_info_t doi; 1559b24ab676SJeff Bonwick dmu_tx_t *tx; 1560b24ab676SJeff Bonwick dmu_buf_t *db; 1561b24ab676SJeff Bonwick arc_buf_t *abuf = NULL; 1562b24ab676SJeff Bonwick rl_t *rl; 1563b24ab676SJeff Bonwick 1564b24ab676SJeff Bonwick if (byteswap) 1565b24ab676SJeff Bonwick byteswap_uint64_array(lr, sizeof (*lr)); 1566b24ab676SJeff Bonwick 1567b24ab676SJeff Bonwick offset = lr->lr_offset; 1568b24ab676SJeff Bonwick length = lr->lr_length; 1569b24ab676SJeff Bonwick 1570b24ab676SJeff Bonwick /* If it's a dmu_sync() block, write the whole block */ 1571b24ab676SJeff Bonwick if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) { 1572b24ab676SJeff Bonwick uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr); 1573b24ab676SJeff Bonwick if (length < blocksize) { 1574b24ab676SJeff Bonwick offset -= offset % blocksize; 1575b24ab676SJeff Bonwick length = blocksize; 1576b24ab676SJeff Bonwick } 1577b24ab676SJeff Bonwick } 1578b24ab676SJeff Bonwick 1579b24ab676SJeff Bonwick if (bt->bt_magic == BSWAP_64(BT_MAGIC)) 1580b24ab676SJeff Bonwick byteswap_uint64_array(bt, sizeof (*bt)); 1581b24ab676SJeff Bonwick 1582b24ab676SJeff Bonwick if (bt->bt_magic != BT_MAGIC) 1583b24ab676SJeff Bonwick bt = NULL; 1584b24ab676SJeff Bonwick 1585b24ab676SJeff Bonwick ztest_object_lock(zd, lr->lr_foid, RL_READER); 1586b24ab676SJeff Bonwick rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER); 1587b24ab676SJeff Bonwick 1588b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db)); 1589b24ab676SJeff Bonwick 1590b24ab676SJeff Bonwick dmu_object_info_from_db(db, &doi); 1591b24ab676SJeff Bonwick 1592b24ab676SJeff Bonwick bbt = ztest_bt_bonus(db); 1593b24ab676SJeff Bonwick ASSERT3U(bbt->bt_magic, ==, BT_MAGIC); 1594b24ab676SJeff Bonwick gen = bbt->bt_gen; 1595b24ab676SJeff Bonwick crtxg = bbt->bt_crtxg; 1596b24ab676SJeff Bonwick lrtxg = lr->lr_common.lrc_txg; 1597b24ab676SJeff Bonwick 1598b24ab676SJeff Bonwick tx = dmu_tx_create(os); 1599b24ab676SJeff Bonwick 1600b24ab676SJeff Bonwick dmu_tx_hold_write(tx, lr->lr_foid, offset, length); 1601b24ab676SJeff Bonwick 1602b24ab676SJeff Bonwick if (ztest_random(8) == 0 && length == doi.doi_data_block_size && 1603b24ab676SJeff Bonwick P2PHASE(offset, length) == 0) 1604b24ab676SJeff Bonwick abuf = dmu_request_arcbuf(db, length); 1605b24ab676SJeff Bonwick 1606b24ab676SJeff Bonwick txg = ztest_tx_assign(tx, TXG_WAIT, FTAG); 1607b24ab676SJeff Bonwick if (txg == 0) { 1608b24ab676SJeff Bonwick if (abuf != NULL) 1609b24ab676SJeff Bonwick dmu_return_arcbuf(abuf); 1610b24ab676SJeff Bonwick dmu_buf_rele(db, FTAG); 1611b24ab676SJeff Bonwick ztest_range_unlock(rl); 1612b24ab676SJeff Bonwick ztest_object_unlock(zd, lr->lr_foid); 1613b24ab676SJeff Bonwick return (ENOSPC); 1614b24ab676SJeff Bonwick } 1615b24ab676SJeff Bonwick 1616b24ab676SJeff Bonwick if (bt != NULL) { 1617b24ab676SJeff Bonwick /* 1618b24ab676SJeff Bonwick * Usually, verify the old data before writing new data -- 1619b24ab676SJeff Bonwick * but not always, because we also want to verify correct 1620b24ab676SJeff Bonwick * behavior when the data was not recently read into cache. 1621b24ab676SJeff Bonwick */ 1622b24ab676SJeff Bonwick ASSERT(offset % doi.doi_data_block_size == 0); 1623b24ab676SJeff Bonwick if (ztest_random(4) != 0) { 1624b24ab676SJeff Bonwick int prefetch = ztest_random(2) ? 1625b24ab676SJeff Bonwick DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH; 1626b24ab676SJeff Bonwick ztest_block_tag_t rbt; 1627b24ab676SJeff Bonwick 1628b24ab676SJeff Bonwick VERIFY(dmu_read(os, lr->lr_foid, offset, 1629b24ab676SJeff Bonwick sizeof (rbt), &rbt, prefetch) == 0); 1630b24ab676SJeff Bonwick if (rbt.bt_magic == BT_MAGIC) { 1631b24ab676SJeff Bonwick ztest_bt_verify(&rbt, os, lr->lr_foid, 1632b24ab676SJeff Bonwick offset, gen, txg, crtxg); 1633b24ab676SJeff Bonwick } 1634b24ab676SJeff Bonwick } 1635b24ab676SJeff Bonwick 1636b24ab676SJeff Bonwick /* 1637b24ab676SJeff Bonwick * Writes can appear to be newer than the bonus buffer because 1638b24ab676SJeff Bonwick * the ztest_get_data() callback does a dmu_read() of the 1639b24ab676SJeff Bonwick * open-context data, which may be different than the data 1640b24ab676SJeff Bonwick * as it was when the write was generated. 1641b24ab676SJeff Bonwick */ 1642b24ab676SJeff Bonwick if (zd->zd_zilog->zl_replay) { 1643b24ab676SJeff Bonwick ztest_bt_verify(bt, os, lr->lr_foid, offset, 1644b24ab676SJeff Bonwick MAX(gen, bt->bt_gen), MAX(txg, lrtxg), 1645b24ab676SJeff Bonwick bt->bt_crtxg); 1646b24ab676SJeff Bonwick } 1647b24ab676SJeff Bonwick 1648b24ab676SJeff Bonwick /* 1649b24ab676SJeff Bonwick * Set the bt's gen/txg to the bonus buffer's gen/txg 1650b24ab676SJeff Bonwick * so that all of the usual ASSERTs will work. 1651b24ab676SJeff Bonwick */ 1652b24ab676SJeff Bonwick ztest_bt_generate(bt, os, lr->lr_foid, offset, gen, txg, crtxg); 1653b24ab676SJeff Bonwick } 1654b24ab676SJeff Bonwick 1655b24ab676SJeff Bonwick if (abuf == NULL) { 1656b24ab676SJeff Bonwick dmu_write(os, lr->lr_foid, offset, length, data, tx); 1657b24ab676SJeff Bonwick } else { 1658b24ab676SJeff Bonwick bcopy(data, abuf->b_data, length); 1659b24ab676SJeff Bonwick dmu_assign_arcbuf(db, offset, abuf, tx); 1660b24ab676SJeff Bonwick } 1661b24ab676SJeff Bonwick 1662b24ab676SJeff Bonwick (void) ztest_log_write(zd, tx, lr); 1663b24ab676SJeff Bonwick 1664b24ab676SJeff Bonwick dmu_buf_rele(db, FTAG); 1665b24ab676SJeff Bonwick 1666b24ab676SJeff Bonwick dmu_tx_commit(tx); 1667b24ab676SJeff Bonwick 1668b24ab676SJeff Bonwick ztest_range_unlock(rl); 1669b24ab676SJeff Bonwick ztest_object_unlock(zd, lr->lr_foid); 1670b24ab676SJeff Bonwick 1671b24ab676SJeff Bonwick return (0); 1672b24ab676SJeff Bonwick } 1673b24ab676SJeff Bonwick 1674b24ab676SJeff Bonwick static int 1675b24ab676SJeff Bonwick ztest_replay_truncate(ztest_ds_t *zd, lr_truncate_t *lr, boolean_t byteswap) 1676b24ab676SJeff Bonwick { 1677b24ab676SJeff Bonwick objset_t *os = zd->zd_os; 1678b24ab676SJeff Bonwick dmu_tx_t *tx; 1679b24ab676SJeff Bonwick uint64_t txg; 1680b24ab676SJeff Bonwick rl_t *rl; 1681b24ab676SJeff Bonwick 1682b24ab676SJeff Bonwick if (byteswap) 1683b24ab676SJeff Bonwick byteswap_uint64_array(lr, sizeof (*lr)); 1684b24ab676SJeff Bonwick 1685b24ab676SJeff Bonwick ztest_object_lock(zd, lr->lr_foid, RL_READER); 1686b24ab676SJeff Bonwick rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length, 1687b24ab676SJeff Bonwick RL_WRITER); 1688b24ab676SJeff Bonwick 1689b24ab676SJeff Bonwick tx = dmu_tx_create(os); 1690b24ab676SJeff Bonwick 1691b24ab676SJeff Bonwick dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length); 1692b24ab676SJeff Bonwick 1693b24ab676SJeff Bonwick txg = ztest_tx_assign(tx, TXG_WAIT, FTAG); 1694b24ab676SJeff Bonwick if (txg == 0) { 1695b24ab676SJeff Bonwick ztest_range_unlock(rl); 1696b24ab676SJeff Bonwick ztest_object_unlock(zd, lr->lr_foid); 1697b24ab676SJeff Bonwick return (ENOSPC); 1698b24ab676SJeff Bonwick } 1699b24ab676SJeff Bonwick 1700b24ab676SJeff Bonwick VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset, 1701b24ab676SJeff Bonwick lr->lr_length, tx) == 0); 1702b24ab676SJeff Bonwick 1703b24ab676SJeff Bonwick (void) ztest_log_truncate(zd, tx, lr); 1704b24ab676SJeff Bonwick 1705b24ab676SJeff Bonwick dmu_tx_commit(tx); 1706b24ab676SJeff Bonwick 1707b24ab676SJeff Bonwick ztest_range_unlock(rl); 1708b24ab676SJeff Bonwick ztest_object_unlock(zd, lr->lr_foid); 1709b24ab676SJeff Bonwick 1710b24ab676SJeff Bonwick return (0); 1711b24ab676SJeff Bonwick } 1712b24ab676SJeff Bonwick 1713b24ab676SJeff Bonwick static int 1714b24ab676SJeff Bonwick ztest_replay_setattr(ztest_ds_t *zd, lr_setattr_t *lr, boolean_t byteswap) 1715b24ab676SJeff Bonwick { 1716b24ab676SJeff Bonwick objset_t *os = zd->zd_os; 1717b24ab676SJeff Bonwick dmu_tx_t *tx; 1718b24ab676SJeff Bonwick dmu_buf_t *db; 1719b24ab676SJeff Bonwick ztest_block_tag_t *bbt; 1720b24ab676SJeff Bonwick uint64_t txg, lrtxg, crtxg; 1721b24ab676SJeff Bonwick 1722b24ab676SJeff Bonwick if (byteswap) 1723b24ab676SJeff Bonwick byteswap_uint64_array(lr, sizeof (*lr)); 1724b24ab676SJeff Bonwick 1725b24ab676SJeff Bonwick ztest_object_lock(zd, lr->lr_foid, RL_WRITER); 1726b24ab676SJeff Bonwick 1727b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db)); 1728b24ab676SJeff Bonwick 1729b24ab676SJeff Bonwick tx = dmu_tx_create(os); 1730b24ab676SJeff Bonwick dmu_tx_hold_bonus(tx, lr->lr_foid); 1731b24ab676SJeff Bonwick 1732b24ab676SJeff Bonwick txg = ztest_tx_assign(tx, TXG_WAIT, FTAG); 1733b24ab676SJeff Bonwick if (txg == 0) { 1734b24ab676SJeff Bonwick dmu_buf_rele(db, FTAG); 1735b24ab676SJeff Bonwick ztest_object_unlock(zd, lr->lr_foid); 1736b24ab676SJeff Bonwick return (ENOSPC); 1737b24ab676SJeff Bonwick } 1738b24ab676SJeff Bonwick 1739b24ab676SJeff Bonwick bbt = ztest_bt_bonus(db); 1740b24ab676SJeff Bonwick ASSERT3U(bbt->bt_magic, ==, BT_MAGIC); 1741b24ab676SJeff Bonwick crtxg = bbt->bt_crtxg; 1742b24ab676SJeff Bonwick lrtxg = lr->lr_common.lrc_txg; 1743b24ab676SJeff Bonwick 1744b24ab676SJeff Bonwick if (zd->zd_zilog->zl_replay) { 1745b24ab676SJeff Bonwick ASSERT(lr->lr_size != 0); 1746b24ab676SJeff Bonwick ASSERT(lr->lr_mode != 0); 1747b24ab676SJeff Bonwick ASSERT(lrtxg != 0); 1748b24ab676SJeff Bonwick } else { 1749b24ab676SJeff Bonwick /* 1750b24ab676SJeff Bonwick * Randomly change the size and increment the generation. 1751b24ab676SJeff Bonwick */ 1752b24ab676SJeff Bonwick lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) * 1753b24ab676SJeff Bonwick sizeof (*bbt); 1754b24ab676SJeff Bonwick lr->lr_mode = bbt->bt_gen + 1; 1755b24ab676SJeff Bonwick ASSERT(lrtxg == 0); 1756b24ab676SJeff Bonwick } 1757b24ab676SJeff Bonwick 1758b24ab676SJeff Bonwick /* 1759b24ab676SJeff Bonwick * Verify that the current bonus buffer is not newer than our txg. 1760b24ab676SJeff Bonwick */ 1761b24ab676SJeff Bonwick ztest_bt_verify(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode, 1762b24ab676SJeff Bonwick MAX(txg, lrtxg), crtxg); 1763b24ab676SJeff Bonwick 1764b24ab676SJeff Bonwick dmu_buf_will_dirty(db, tx); 1765b24ab676SJeff Bonwick 1766b24ab676SJeff Bonwick ASSERT3U(lr->lr_size, >=, sizeof (*bbt)); 1767b24ab676SJeff Bonwick ASSERT3U(lr->lr_size, <=, db->db_size); 1768fb09f5aaSMadhav Suresh VERIFY0(dmu_set_bonus(db, lr->lr_size, tx)); 1769b24ab676SJeff Bonwick bbt = ztest_bt_bonus(db); 1770b24ab676SJeff Bonwick 1771b24ab676SJeff Bonwick ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode, txg, crtxg); 1772b24ab676SJeff Bonwick 1773b24ab676SJeff Bonwick dmu_buf_rele(db, FTAG); 1774b24ab676SJeff Bonwick 1775b24ab676SJeff Bonwick (void) ztest_log_setattr(zd, tx, lr); 1776b24ab676SJeff Bonwick 1777b24ab676SJeff Bonwick dmu_tx_commit(tx); 1778b24ab676SJeff Bonwick 1779b24ab676SJeff Bonwick ztest_object_unlock(zd, lr->lr_foid); 1780b24ab676SJeff Bonwick 1781b24ab676SJeff Bonwick return (0); 1782fa9e4066Sahrens } 1783fa9e4066Sahrens 1784fa9e4066Sahrens zil_replay_func_t *ztest_replay_vector[TX_MAX_TYPE] = { 1785fa9e4066Sahrens NULL, /* 0 no such transaction type */ 1786fa9e4066Sahrens ztest_replay_create, /* TX_CREATE */ 1787fa9e4066Sahrens NULL, /* TX_MKDIR */ 1788fa9e4066Sahrens NULL, /* TX_MKXATTR */ 1789fa9e4066Sahrens NULL, /* TX_SYMLINK */ 1790fa9e4066Sahrens ztest_replay_remove, /* TX_REMOVE */ 1791fa9e4066Sahrens NULL, /* TX_RMDIR */ 1792fa9e4066Sahrens NULL, /* TX_LINK */ 1793fa9e4066Sahrens NULL, /* TX_RENAME */ 1794b24ab676SJeff Bonwick ztest_replay_write, /* TX_WRITE */ 1795b24ab676SJeff Bonwick ztest_replay_truncate, /* TX_TRUNCATE */ 1796b24ab676SJeff Bonwick ztest_replay_setattr, /* TX_SETATTR */ 1797fa9e4066Sahrens NULL, /* TX_ACL */ 1798975c32a0SNeil Perrin NULL, /* TX_CREATE_ACL */ 1799975c32a0SNeil Perrin NULL, /* TX_CREATE_ATTR */ 1800975c32a0SNeil Perrin NULL, /* TX_CREATE_ACL_ATTR */ 1801975c32a0SNeil Perrin NULL, /* TX_MKDIR_ACL */ 1802975c32a0SNeil Perrin NULL, /* TX_MKDIR_ATTR */ 1803975c32a0SNeil Perrin NULL, /* TX_MKDIR_ACL_ATTR */ 1804975c32a0SNeil Perrin NULL, /* TX_WRITE2 */ 1805fa9e4066Sahrens }; 1806fa9e4066Sahrens 1807fa9e4066Sahrens /* 1808b24ab676SJeff Bonwick * ZIL get_data callbacks 1809b24ab676SJeff Bonwick */ 1810b24ab676SJeff Bonwick 1811b24ab676SJeff Bonwick static void 1812b24ab676SJeff Bonwick ztest_get_done(zgd_t *zgd, int error) 1813b24ab676SJeff Bonwick { 1814b24ab676SJeff Bonwick ztest_ds_t *zd = zgd->zgd_private; 1815b24ab676SJeff Bonwick uint64_t object = zgd->zgd_rl->rl_object; 1816b24ab676SJeff Bonwick 1817b24ab676SJeff Bonwick if (zgd->zgd_db) 1818b24ab676SJeff Bonwick dmu_buf_rele(zgd->zgd_db, zgd); 1819b24ab676SJeff Bonwick 1820b24ab676SJeff Bonwick ztest_range_unlock(zgd->zgd_rl); 1821b24ab676SJeff Bonwick ztest_object_unlock(zd, object); 1822b24ab676SJeff Bonwick 1823b24ab676SJeff Bonwick if (error == 0 && zgd->zgd_bp) 1824b24ab676SJeff Bonwick zil_add_block(zgd->zgd_zilog, zgd->zgd_bp); 1825b24ab676SJeff Bonwick 1826b24ab676SJeff Bonwick umem_free(zgd, sizeof (*zgd)); 1827b24ab676SJeff Bonwick } 1828b24ab676SJeff Bonwick 1829b24ab676SJeff Bonwick static int 1830b24ab676SJeff Bonwick ztest_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio) 1831b24ab676SJeff Bonwick { 1832b24ab676SJeff Bonwick ztest_ds_t *zd = arg; 1833b24ab676SJeff Bonwick objset_t *os = zd->zd_os; 1834b24ab676SJeff Bonwick uint64_t object = lr->lr_foid; 1835b24ab676SJeff Bonwick uint64_t offset = lr->lr_offset; 1836b24ab676SJeff Bonwick uint64_t size = lr->lr_length; 1837b24ab676SJeff Bonwick blkptr_t *bp = &lr->lr_blkptr; 1838b24ab676SJeff Bonwick uint64_t txg = lr->lr_common.lrc_txg; 1839b24ab676SJeff Bonwick uint64_t crtxg; 1840b24ab676SJeff Bonwick dmu_object_info_t doi; 1841b24ab676SJeff Bonwick dmu_buf_t *db; 1842b24ab676SJeff Bonwick zgd_t *zgd; 1843b24ab676SJeff Bonwick int error; 1844b24ab676SJeff Bonwick 1845b24ab676SJeff Bonwick ztest_object_lock(zd, object, RL_READER); 1846b24ab676SJeff Bonwick error = dmu_bonus_hold(os, object, FTAG, &db); 1847b24ab676SJeff Bonwick if (error) { 1848b24ab676SJeff Bonwick ztest_object_unlock(zd, object); 1849b24ab676SJeff Bonwick return (error); 1850b24ab676SJeff Bonwick } 1851b24ab676SJeff Bonwick 1852b24ab676SJeff Bonwick crtxg = ztest_bt_bonus(db)->bt_crtxg; 1853b24ab676SJeff Bonwick 1854b24ab676SJeff Bonwick if (crtxg == 0 || crtxg > txg) { 1855b24ab676SJeff Bonwick dmu_buf_rele(db, FTAG); 1856b24ab676SJeff Bonwick ztest_object_unlock(zd, object); 1857b24ab676SJeff Bonwick return (ENOENT); 1858b24ab676SJeff Bonwick } 1859b24ab676SJeff Bonwick 1860b24ab676SJeff Bonwick dmu_object_info_from_db(db, &doi); 1861b24ab676SJeff Bonwick dmu_buf_rele(db, FTAG); 1862b24ab676SJeff Bonwick db = NULL; 1863b24ab676SJeff Bonwick 1864b24ab676SJeff Bonwick zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL); 1865b24ab676SJeff Bonwick zgd->zgd_zilog = zd->zd_zilog; 1866b24ab676SJeff Bonwick zgd->zgd_private = zd; 1867b24ab676SJeff Bonwick 1868b24ab676SJeff Bonwick if (buf != NULL) { /* immediate write */ 1869b24ab676SJeff Bonwick zgd->zgd_rl = ztest_range_lock(zd, object, offset, size, 1870b24ab676SJeff Bonwick RL_READER); 1871b24ab676SJeff Bonwick 1872b24ab676SJeff Bonwick error = dmu_read(os, object, offset, size, buf, 1873b24ab676SJeff Bonwick DMU_READ_NO_PREFETCH); 1874b24ab676SJeff Bonwick ASSERT(error == 0); 1875b24ab676SJeff Bonwick } else { 1876b24ab676SJeff Bonwick size = doi.doi_data_block_size; 1877dfe73b3dSJeff Bonwick if (ISP2(size)) { 1878b24ab676SJeff Bonwick offset = P2ALIGN(offset, size); 1879dfe73b3dSJeff Bonwick } else { 1880dfe73b3dSJeff Bonwick ASSERT(offset < size); 1881dfe73b3dSJeff Bonwick offset = 0; 1882dfe73b3dSJeff Bonwick } 1883b24ab676SJeff Bonwick 1884b24ab676SJeff Bonwick zgd->zgd_rl = ztest_range_lock(zd, object, offset, size, 1885b24ab676SJeff Bonwick RL_READER); 1886b24ab676SJeff Bonwick 188747cb52daSJeff Bonwick error = dmu_buf_hold(os, object, offset, zgd, &db, 188847cb52daSJeff Bonwick DMU_READ_NO_PREFETCH); 1889b24ab676SJeff Bonwick 1890b24ab676SJeff Bonwick if (error == 0) { 189180901aeaSGeorge Wilson blkptr_t *obp = dmu_buf_get_blkptr(db); 189280901aeaSGeorge Wilson if (obp) { 189380901aeaSGeorge Wilson ASSERT(BP_IS_HOLE(bp)); 189480901aeaSGeorge Wilson *bp = *obp; 189580901aeaSGeorge Wilson } 189680901aeaSGeorge Wilson 1897b24ab676SJeff Bonwick zgd->zgd_db = db; 1898b24ab676SJeff Bonwick zgd->zgd_bp = bp; 1899b24ab676SJeff Bonwick 1900b24ab676SJeff Bonwick ASSERT(db->db_offset == offset); 1901b24ab676SJeff Bonwick ASSERT(db->db_size == size); 1902b24ab676SJeff Bonwick 1903b24ab676SJeff Bonwick error = dmu_sync(zio, lr->lr_common.lrc_txg, 1904b24ab676SJeff Bonwick ztest_get_done, zgd); 1905b24ab676SJeff Bonwick 1906b24ab676SJeff Bonwick if (error == 0) 1907b24ab676SJeff Bonwick return (0); 1908b24ab676SJeff Bonwick } 1909b24ab676SJeff Bonwick } 1910b24ab676SJeff Bonwick 1911b24ab676SJeff Bonwick ztest_get_done(zgd, error); 1912b24ab676SJeff Bonwick 1913b24ab676SJeff Bonwick return (error); 1914b24ab676SJeff Bonwick } 1915b24ab676SJeff Bonwick 1916b24ab676SJeff Bonwick static void * 1917b24ab676SJeff Bonwick ztest_lr_alloc(size_t lrsize, char *name) 1918b24ab676SJeff Bonwick { 1919b24ab676SJeff Bonwick char *lr; 1920b24ab676SJeff Bonwick size_t namesize = name ? strlen(name) + 1 : 0; 1921b24ab676SJeff Bonwick 1922b24ab676SJeff Bonwick lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL); 1923b24ab676SJeff Bonwick 1924b24ab676SJeff Bonwick if (name) 1925b24ab676SJeff Bonwick bcopy(name, lr + lrsize, namesize); 1926b24ab676SJeff Bonwick 1927b24ab676SJeff Bonwick return (lr); 1928b24ab676SJeff Bonwick } 1929b24ab676SJeff Bonwick 1930b24ab676SJeff Bonwick void 1931b24ab676SJeff Bonwick ztest_lr_free(void *lr, size_t lrsize, char *name) 1932b24ab676SJeff Bonwick { 1933b24ab676SJeff Bonwick size_t namesize = name ? strlen(name) + 1 : 0; 1934b24ab676SJeff Bonwick 1935b24ab676SJeff Bonwick umem_free(lr, lrsize + namesize); 1936b24ab676SJeff Bonwick } 1937b24ab676SJeff Bonwick 1938b24ab676SJeff Bonwick /* 1939b24ab676SJeff Bonwick * Lookup a bunch of objects. Returns the number of objects not found. 1940b24ab676SJeff Bonwick */ 1941b24ab676SJeff Bonwick static int 1942b24ab676SJeff Bonwick ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count) 1943b24ab676SJeff Bonwick { 1944b24ab676SJeff Bonwick int missing = 0; 1945b24ab676SJeff Bonwick int error; 1946b24ab676SJeff Bonwick 1947b24ab676SJeff Bonwick ASSERT(_mutex_held(&zd->zd_dirobj_lock)); 1948b24ab676SJeff Bonwick 1949b24ab676SJeff Bonwick for (int i = 0; i < count; i++, od++) { 1950b24ab676SJeff Bonwick od->od_object = 0; 1951b24ab676SJeff Bonwick error = zap_lookup(zd->zd_os, od->od_dir, od->od_name, 1952b24ab676SJeff Bonwick sizeof (uint64_t), 1, &od->od_object); 1953b24ab676SJeff Bonwick if (error) { 1954b24ab676SJeff Bonwick ASSERT(error == ENOENT); 1955b24ab676SJeff Bonwick ASSERT(od->od_object == 0); 1956b24ab676SJeff Bonwick missing++; 1957b24ab676SJeff Bonwick } else { 1958b24ab676SJeff Bonwick dmu_buf_t *db; 1959b24ab676SJeff Bonwick ztest_block_tag_t *bbt; 1960b24ab676SJeff Bonwick dmu_object_info_t doi; 1961b24ab676SJeff Bonwick 1962b24ab676SJeff Bonwick ASSERT(od->od_object != 0); 1963b24ab676SJeff Bonwick ASSERT(missing == 0); /* there should be no gaps */ 1964b24ab676SJeff Bonwick 1965b24ab676SJeff Bonwick ztest_object_lock(zd, od->od_object, RL_READER); 1966b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os, 1967b24ab676SJeff Bonwick od->od_object, FTAG, &db)); 1968b24ab676SJeff Bonwick dmu_object_info_from_db(db, &doi); 1969b24ab676SJeff Bonwick bbt = ztest_bt_bonus(db); 1970b24ab676SJeff Bonwick ASSERT3U(bbt->bt_magic, ==, BT_MAGIC); 1971b24ab676SJeff Bonwick od->od_type = doi.doi_type; 1972b24ab676SJeff Bonwick od->od_blocksize = doi.doi_data_block_size; 1973b24ab676SJeff Bonwick od->od_gen = bbt->bt_gen; 1974b24ab676SJeff Bonwick dmu_buf_rele(db, FTAG); 1975b24ab676SJeff Bonwick ztest_object_unlock(zd, od->od_object); 1976b24ab676SJeff Bonwick } 1977b24ab676SJeff Bonwick } 1978b24ab676SJeff Bonwick 1979b24ab676SJeff Bonwick return (missing); 1980b24ab676SJeff Bonwick } 1981b24ab676SJeff Bonwick 1982b24ab676SJeff Bonwick static int 1983b24ab676SJeff Bonwick ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count) 1984b24ab676SJeff Bonwick { 1985b24ab676SJeff Bonwick int missing = 0; 1986b24ab676SJeff Bonwick 1987b24ab676SJeff Bonwick ASSERT(_mutex_held(&zd->zd_dirobj_lock)); 1988b24ab676SJeff Bonwick 1989b24ab676SJeff Bonwick for (int i = 0; i < count; i++, od++) { 1990b24ab676SJeff Bonwick if (missing) { 1991b24ab676SJeff Bonwick od->od_object = 0; 1992b24ab676SJeff Bonwick missing++; 1993b24ab676SJeff Bonwick continue; 1994b24ab676SJeff Bonwick } 1995b24ab676SJeff Bonwick 1996b24ab676SJeff Bonwick lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name); 1997b24ab676SJeff Bonwick 1998b24ab676SJeff Bonwick lr->lr_doid = od->od_dir; 1999b24ab676SJeff Bonwick lr->lr_foid = 0; /* 0 to allocate, > 0 to claim */ 2000b24ab676SJeff Bonwick lr->lrz_type = od->od_crtype; 2001b24ab676SJeff Bonwick lr->lrz_blocksize = od->od_crblocksize; 2002b24ab676SJeff Bonwick lr->lrz_ibshift = ztest_random_ibshift(); 2003b24ab676SJeff Bonwick lr->lrz_bonustype = DMU_OT_UINT64_OTHER; 2004b24ab676SJeff Bonwick lr->lrz_bonuslen = dmu_bonus_max(); 2005b24ab676SJeff Bonwick lr->lr_gen = od->od_crgen; 2006b24ab676SJeff Bonwick lr->lr_crtime[0] = time(NULL); 2007b24ab676SJeff Bonwick 2008b24ab676SJeff Bonwick if (ztest_replay_create(zd, lr, B_FALSE) != 0) { 2009b24ab676SJeff Bonwick ASSERT(missing == 0); 2010b24ab676SJeff Bonwick od->od_object = 0; 2011b24ab676SJeff Bonwick missing++; 2012b24ab676SJeff Bonwick } else { 2013b24ab676SJeff Bonwick od->od_object = lr->lr_foid; 2014b24ab676SJeff Bonwick od->od_type = od->od_crtype; 2015b24ab676SJeff Bonwick od->od_blocksize = od->od_crblocksize; 2016b24ab676SJeff Bonwick od->od_gen = od->od_crgen; 2017b24ab676SJeff Bonwick ASSERT(od->od_object != 0); 2018b24ab676SJeff Bonwick } 2019b24ab676SJeff Bonwick 2020b24ab676SJeff Bonwick ztest_lr_free(lr, sizeof (*lr), od->od_name); 2021b24ab676SJeff Bonwick } 2022b24ab676SJeff Bonwick 2023b24ab676SJeff Bonwick return (missing); 2024b24ab676SJeff Bonwick } 2025b24ab676SJeff Bonwick 2026b24ab676SJeff Bonwick static int 2027b24ab676SJeff Bonwick ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count) 2028b24ab676SJeff Bonwick { 2029b24ab676SJeff Bonwick int missing = 0; 2030b24ab676SJeff Bonwick int error; 2031b24ab676SJeff Bonwick 2032b24ab676SJeff Bonwick ASSERT(_mutex_held(&zd->zd_dirobj_lock)); 2033b24ab676SJeff Bonwick 2034b24ab676SJeff Bonwick od += count - 1; 2035b24ab676SJeff Bonwick 2036b24ab676SJeff Bonwick for (int i = count - 1; i >= 0; i--, od--) { 2037b24ab676SJeff Bonwick if (missing) { 2038b24ab676SJeff Bonwick missing++; 2039b24ab676SJeff Bonwick continue; 2040b24ab676SJeff Bonwick } 2041b24ab676SJeff Bonwick 204280901aeaSGeorge Wilson /* 204380901aeaSGeorge Wilson * No object was found. 204480901aeaSGeorge Wilson */ 2045b24ab676SJeff Bonwick if (od->od_object == 0) 2046b24ab676SJeff Bonwick continue; 2047b24ab676SJeff Bonwick 2048b24ab676SJeff Bonwick lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name); 2049b24ab676SJeff Bonwick 2050b24ab676SJeff Bonwick lr->lr_doid = od->od_dir; 2051b24ab676SJeff Bonwick 2052b24ab676SJeff Bonwick if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) { 2053b24ab676SJeff Bonwick ASSERT3U(error, ==, ENOSPC); 2054b24ab676SJeff Bonwick missing++; 2055b24ab676SJeff Bonwick } else { 2056b24ab676SJeff Bonwick od->od_object = 0; 2057b24ab676SJeff Bonwick } 2058b24ab676SJeff Bonwick ztest_lr_free(lr, sizeof (*lr), od->od_name); 2059b24ab676SJeff Bonwick } 2060b24ab676SJeff Bonwick 2061b24ab676SJeff Bonwick return (missing); 2062b24ab676SJeff Bonwick } 2063b24ab676SJeff Bonwick 2064b24ab676SJeff Bonwick static int 2065b24ab676SJeff Bonwick ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size, 2066b24ab676SJeff Bonwick void *data) 2067b24ab676SJeff Bonwick { 2068b24ab676SJeff Bonwick lr_write_t *lr; 2069b24ab676SJeff Bonwick int error; 2070b24ab676SJeff Bonwick 2071b24ab676SJeff Bonwick lr = ztest_lr_alloc(sizeof (*lr) + size, NULL); 2072b24ab676SJeff Bonwick 2073b24ab676SJeff Bonwick lr->lr_foid = object; 2074b24ab676SJeff Bonwick lr->lr_offset = offset; 2075b24ab676SJeff Bonwick lr->lr_length = size; 2076b24ab676SJeff Bonwick lr->lr_blkoff = 0; 2077b24ab676SJeff Bonwick BP_ZERO(&lr->lr_blkptr); 2078b24ab676SJeff Bonwick 2079b24ab676SJeff Bonwick bcopy(data, lr + 1, size); 2080b24ab676SJeff Bonwick 2081b24ab676SJeff Bonwick error = ztest_replay_write(zd, lr, B_FALSE); 2082b24ab676SJeff Bonwick 2083b24ab676SJeff Bonwick ztest_lr_free(lr, sizeof (*lr) + size, NULL); 2084b24ab676SJeff Bonwick 2085b24ab676SJeff Bonwick return (error); 2086b24ab676SJeff Bonwick } 2087b24ab676SJeff Bonwick 2088b24ab676SJeff Bonwick static int 2089b24ab676SJeff Bonwick ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size) 2090b24ab676SJeff Bonwick { 2091b24ab676SJeff Bonwick lr_truncate_t *lr; 2092b24ab676SJeff Bonwick int error; 2093b24ab676SJeff Bonwick 2094b24ab676SJeff Bonwick lr = ztest_lr_alloc(sizeof (*lr), NULL); 2095b24ab676SJeff Bonwick 2096b24ab676SJeff Bonwick lr->lr_foid = object; 2097b24ab676SJeff Bonwick lr->lr_offset = offset; 2098b24ab676SJeff Bonwick lr->lr_length = size; 2099b24ab676SJeff Bonwick 2100b24ab676SJeff Bonwick error = ztest_replay_truncate(zd, lr, B_FALSE); 2101b24ab676SJeff Bonwick 2102b24ab676SJeff Bonwick ztest_lr_free(lr, sizeof (*lr), NULL); 2103b24ab676SJeff Bonwick 2104b24ab676SJeff Bonwick return (error); 2105b24ab676SJeff Bonwick } 2106b24ab676SJeff Bonwick 2107b24ab676SJeff Bonwick static int 2108b24ab676SJeff Bonwick ztest_setattr(ztest_ds_t *zd, uint64_t object) 2109b24ab676SJeff Bonwick { 2110b24ab676SJeff Bonwick lr_setattr_t *lr; 2111b24ab676SJeff Bonwick int error; 2112b24ab676SJeff Bonwick 2113b24ab676SJeff Bonwick lr = ztest_lr_alloc(sizeof (*lr), NULL); 2114b24ab676SJeff Bonwick 2115b24ab676SJeff Bonwick lr->lr_foid = object; 2116b24ab676SJeff Bonwick lr->lr_size = 0; 2117b24ab676SJeff Bonwick lr->lr_mode = 0; 2118b24ab676SJeff Bonwick 2119b24ab676SJeff Bonwick error = ztest_replay_setattr(zd, lr, B_FALSE); 2120b24ab676SJeff Bonwick 2121b24ab676SJeff Bonwick ztest_lr_free(lr, sizeof (*lr), NULL); 2122b24ab676SJeff Bonwick 2123b24ab676SJeff Bonwick return (error); 2124b24ab676SJeff Bonwick } 2125b24ab676SJeff Bonwick 2126b24ab676SJeff Bonwick static void 2127b24ab676SJeff Bonwick ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size) 2128b24ab676SJeff Bonwick { 2129b24ab676SJeff Bonwick objset_t *os = zd->zd_os; 2130b24ab676SJeff Bonwick dmu_tx_t *tx; 2131b24ab676SJeff Bonwick uint64_t txg; 2132b24ab676SJeff Bonwick rl_t *rl; 2133b24ab676SJeff Bonwick 2134b24ab676SJeff Bonwick txg_wait_synced(dmu_objset_pool(os), 0); 2135b24ab676SJeff Bonwick 2136b24ab676SJeff Bonwick ztest_object_lock(zd, object, RL_READER); 2137b24ab676SJeff Bonwick rl = ztest_range_lock(zd, object, offset, size, RL_WRITER); 2138b24ab676SJeff Bonwick 2139b24ab676SJeff Bonwick tx = dmu_tx_create(os); 2140b24ab676SJeff Bonwick 2141b24ab676SJeff Bonwick dmu_tx_hold_write(tx, object, offset, size); 2142b24ab676SJeff Bonwick 2143b24ab676SJeff Bonwick txg = ztest_tx_assign(tx, TXG_WAIT, FTAG); 2144b24ab676SJeff Bonwick 2145b24ab676SJeff Bonwick if (txg != 0) { 2146b24ab676SJeff Bonwick dmu_prealloc(os, object, offset, size, tx); 2147b24ab676SJeff Bonwick dmu_tx_commit(tx); 2148b24ab676SJeff Bonwick txg_wait_synced(dmu_objset_pool(os), txg); 2149b24ab676SJeff Bonwick } else { 2150b24ab676SJeff Bonwick (void) dmu_free_long_range(os, object, offset, size); 2151b24ab676SJeff Bonwick } 2152b24ab676SJeff Bonwick 2153b24ab676SJeff Bonwick ztest_range_unlock(rl); 2154b24ab676SJeff Bonwick ztest_object_unlock(zd, object); 2155b24ab676SJeff Bonwick } 2156b24ab676SJeff Bonwick 2157b24ab676SJeff Bonwick static void 2158b24ab676SJeff Bonwick ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset) 2159b24ab676SJeff Bonwick { 216080901aeaSGeorge Wilson int err; 2161b24ab676SJeff Bonwick ztest_block_tag_t wbt; 2162b24ab676SJeff Bonwick dmu_object_info_t doi; 2163b24ab676SJeff Bonwick enum ztest_io_type io_type; 2164b24ab676SJeff Bonwick uint64_t blocksize; 2165b24ab676SJeff Bonwick void *data; 2166b24ab676SJeff Bonwick 2167b24ab676SJeff Bonwick VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0); 2168b24ab676SJeff Bonwick blocksize = doi.doi_data_block_size; 2169b24ab676SJeff Bonwick data = umem_alloc(blocksize, UMEM_NOFAIL); 2170b24ab676SJeff Bonwick 2171b24ab676SJeff Bonwick /* 2172b24ab676SJeff Bonwick * Pick an i/o type at random, biased toward writing block tags. 2173b24ab676SJeff Bonwick */ 2174b24ab676SJeff Bonwick io_type = ztest_random(ZTEST_IO_TYPES); 2175b24ab676SJeff Bonwick if (ztest_random(2) == 0) 2176b24ab676SJeff Bonwick io_type = ZTEST_IO_WRITE_TAG; 2177b24ab676SJeff Bonwick 2178c9ba2a43SEric Schrock (void) rw_rdlock(&zd->zd_zilog_lock); 2179c9ba2a43SEric Schrock 2180b24ab676SJeff Bonwick switch (io_type) { 2181b24ab676SJeff Bonwick 2182b24ab676SJeff Bonwick case ZTEST_IO_WRITE_TAG: 2183b24ab676SJeff Bonwick ztest_bt_generate(&wbt, zd->zd_os, object, offset, 0, 0, 0); 2184b24ab676SJeff Bonwick (void) ztest_write(zd, object, offset, sizeof (wbt), &wbt); 2185b24ab676SJeff Bonwick break; 2186b24ab676SJeff Bonwick 2187b24ab676SJeff Bonwick case ZTEST_IO_WRITE_PATTERN: 2188b24ab676SJeff Bonwick (void) memset(data, 'a' + (object + offset) % 5, blocksize); 2189b24ab676SJeff Bonwick if (ztest_random(2) == 0) { 2190b24ab676SJeff Bonwick /* 2191b24ab676SJeff Bonwick * Induce fletcher2 collisions to ensure that 2192b24ab676SJeff Bonwick * zio_ddt_collision() detects and resolves them 2193b24ab676SJeff Bonwick * when using fletcher2-verify for deduplication. 2194b24ab676SJeff Bonwick */ 2195b24ab676SJeff Bonwick ((uint64_t *)data)[0] ^= 1ULL << 63; 2196b24ab676SJeff Bonwick ((uint64_t *)data)[4] ^= 1ULL << 63; 2197b24ab676SJeff Bonwick } 2198b24ab676SJeff Bonwick (void) ztest_write(zd, object, offset, blocksize, data); 2199b24ab676SJeff Bonwick break; 2200b24ab676SJeff Bonwick 2201b24ab676SJeff Bonwick case ZTEST_IO_WRITE_ZEROES: 2202b24ab676SJeff Bonwick bzero(data, blocksize); 2203b24ab676SJeff Bonwick (void) ztest_write(zd, object, offset, blocksize, data); 2204b24ab676SJeff Bonwick break; 2205b24ab676SJeff Bonwick 2206b24ab676SJeff Bonwick case ZTEST_IO_TRUNCATE: 2207b24ab676SJeff Bonwick (void) ztest_truncate(zd, object, offset, blocksize); 2208b24ab676SJeff Bonwick break; 2209b24ab676SJeff Bonwick 2210b24ab676SJeff Bonwick case ZTEST_IO_SETATTR: 2211b24ab676SJeff Bonwick (void) ztest_setattr(zd, object); 2212b24ab676SJeff Bonwick break; 221380901aeaSGeorge Wilson 221480901aeaSGeorge Wilson case ZTEST_IO_REWRITE: 221580901aeaSGeorge Wilson (void) rw_rdlock(&ztest_name_lock); 221680901aeaSGeorge Wilson err = ztest_dsl_prop_set_uint64(zd->zd_name, 221780901aeaSGeorge Wilson ZFS_PROP_CHECKSUM, spa_dedup_checksum(ztest_spa), 221880901aeaSGeorge Wilson B_FALSE); 221980901aeaSGeorge Wilson VERIFY(err == 0 || err == ENOSPC); 222080901aeaSGeorge Wilson err = ztest_dsl_prop_set_uint64(zd->zd_name, 222180901aeaSGeorge Wilson ZFS_PROP_COMPRESSION, 222280901aeaSGeorge Wilson ztest_random_dsl_prop(ZFS_PROP_COMPRESSION), 222380901aeaSGeorge Wilson B_FALSE); 222480901aeaSGeorge Wilson VERIFY(err == 0 || err == ENOSPC); 222580901aeaSGeorge Wilson (void) rw_unlock(&ztest_name_lock); 222680901aeaSGeorge Wilson 222780901aeaSGeorge Wilson VERIFY0(dmu_read(zd->zd_os, object, offset, blocksize, data, 222880901aeaSGeorge Wilson DMU_READ_NO_PREFETCH)); 222980901aeaSGeorge Wilson 223080901aeaSGeorge Wilson (void) ztest_write(zd, object, offset, blocksize, data); 223180901aeaSGeorge Wilson break; 2232b24ab676SJeff Bonwick } 2233b24ab676SJeff Bonwick 2234c9ba2a43SEric Schrock (void) rw_unlock(&zd->zd_zilog_lock); 2235c9ba2a43SEric Schrock 2236b24ab676SJeff Bonwick umem_free(data, blocksize); 2237b24ab676SJeff Bonwick } 2238b24ab676SJeff Bonwick 2239b24ab676SJeff Bonwick /* 2240b24ab676SJeff Bonwick * Initialize an object description template. 2241b24ab676SJeff Bonwick */ 2242b24ab676SJeff Bonwick static void 2243b24ab676SJeff Bonwick ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index, 2244b24ab676SJeff Bonwick dmu_object_type_t type, uint64_t blocksize, uint64_t gen) 2245b24ab676SJeff Bonwick { 2246b24ab676SJeff Bonwick od->od_dir = ZTEST_DIROBJ; 2247b24ab676SJeff Bonwick od->od_object = 0; 2248b24ab676SJeff Bonwick 2249b24ab676SJeff Bonwick od->od_crtype = type; 2250b24ab676SJeff Bonwick od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize(); 2251b24ab676SJeff Bonwick od->od_crgen = gen; 2252b24ab676SJeff Bonwick 2253b24ab676SJeff Bonwick od->od_type = DMU_OT_NONE; 2254b24ab676SJeff Bonwick od->od_blocksize = 0; 2255b24ab676SJeff Bonwick od->od_gen = 0; 2256b24ab676SJeff Bonwick 2257b24ab676SJeff Bonwick (void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]", 2258b24ab676SJeff Bonwick tag, (int64_t)id, index); 2259b24ab676SJeff Bonwick } 2260b24ab676SJeff Bonwick 2261b24ab676SJeff Bonwick /* 2262b24ab676SJeff Bonwick * Lookup or create the objects for a test using the od template. 2263b24ab676SJeff Bonwick * If the objects do not all exist, or if 'remove' is specified, 2264b24ab676SJeff Bonwick * remove any existing objects and create new ones. Otherwise, 2265b24ab676SJeff Bonwick * use the existing objects. 2266b24ab676SJeff Bonwick */ 2267b24ab676SJeff Bonwick static int 2268b24ab676SJeff Bonwick ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove) 2269b24ab676SJeff Bonwick { 2270b24ab676SJeff Bonwick int count = size / sizeof (*od); 2271b24ab676SJeff Bonwick int rv = 0; 2272b24ab676SJeff Bonwick 2273b24ab676SJeff Bonwick VERIFY(mutex_lock(&zd->zd_dirobj_lock) == 0); 2274b24ab676SJeff Bonwick if ((ztest_lookup(zd, od, count) != 0 || remove) && 2275b24ab676SJeff Bonwick (ztest_remove(zd, od, count) != 0 || 2276b24ab676SJeff Bonwick ztest_create(zd, od, count) != 0)) 2277b24ab676SJeff Bonwick rv = -1; 2278b24ab676SJeff Bonwick zd->zd_od = od; 2279b24ab676SJeff Bonwick VERIFY(mutex_unlock(&zd->zd_dirobj_lock) == 0); 2280b24ab676SJeff Bonwick 2281b24ab676SJeff Bonwick return (rv); 2282b24ab676SJeff Bonwick } 2283b24ab676SJeff Bonwick 2284b24ab676SJeff Bonwick /* ARGSUSED */ 2285b24ab676SJeff Bonwick void 2286b24ab676SJeff Bonwick ztest_zil_commit(ztest_ds_t *zd, uint64_t id) 2287b24ab676SJeff Bonwick { 2288b24ab676SJeff Bonwick zilog_t *zilog = zd->zd_zilog; 2289b24ab676SJeff Bonwick 2290c9ba2a43SEric Schrock (void) rw_rdlock(&zd->zd_zilog_lock); 2291c9ba2a43SEric Schrock 22925002558fSNeil Perrin zil_commit(zilog, ztest_random(ZTEST_OBJECTS)); 2293b24ab676SJeff Bonwick 2294b24ab676SJeff Bonwick /* 2295b24ab676SJeff Bonwick * Remember the committed values in zd, which is in parent/child 2296b24ab676SJeff Bonwick * shared memory. If we die, the next iteration of ztest_run() 2297b24ab676SJeff Bonwick * will verify that the log really does contain this record. 2298b24ab676SJeff Bonwick */ 2299b24ab676SJeff Bonwick mutex_enter(&zilog->zl_lock); 2300420dfc95SChris Siden ASSERT(zd->zd_shared != NULL); 2301420dfc95SChris Siden ASSERT3U(zd->zd_shared->zd_seq, <=, zilog->zl_commit_lr_seq); 2302420dfc95SChris Siden zd->zd_shared->zd_seq = zilog->zl_commit_lr_seq; 2303b24ab676SJeff Bonwick mutex_exit(&zilog->zl_lock); 2304c9ba2a43SEric Schrock 2305c9ba2a43SEric Schrock (void) rw_unlock(&zd->zd_zilog_lock); 2306c9ba2a43SEric Schrock } 2307c9ba2a43SEric Schrock 2308c9ba2a43SEric Schrock /* 2309c9ba2a43SEric Schrock * This function is designed to simulate the operations that occur during a 2310c9ba2a43SEric Schrock * mount/unmount operation. We hold the dataset across these operations in an 2311c9ba2a43SEric Schrock * attempt to expose any implicit assumptions about ZIL management. 2312c9ba2a43SEric Schrock */ 2313c9ba2a43SEric Schrock /* ARGSUSED */ 2314c9ba2a43SEric Schrock void 2315c9ba2a43SEric Schrock ztest_zil_remount(ztest_ds_t *zd, uint64_t id) 2316c9ba2a43SEric Schrock { 2317c9ba2a43SEric Schrock objset_t *os = zd->zd_os; 2318c9ba2a43SEric Schrock 231980901aeaSGeorge Wilson /* 232080901aeaSGeorge Wilson * We grab the zd_dirobj_lock to ensure that no other thread is 232180901aeaSGeorge Wilson * updating the zil (i.e. adding in-memory log records) and the 232280901aeaSGeorge Wilson * zd_zilog_lock to block any I/O. 232380901aeaSGeorge Wilson */ 232480901aeaSGeorge Wilson VERIFY0(mutex_lock(&zd->zd_dirobj_lock)); 2325c9ba2a43SEric Schrock (void) rw_wrlock(&zd->zd_zilog_lock); 2326c9ba2a43SEric Schrock 2327c9ba2a43SEric Schrock /* zfsvfs_teardown() */ 2328c9ba2a43SEric Schrock zil_close(zd->zd_zilog); 2329c9ba2a43SEric Schrock 2330c9ba2a43SEric Schrock /* zfsvfs_setup() */ 2331c9ba2a43SEric Schrock VERIFY(zil_open(os, ztest_get_data) == zd->zd_zilog); 2332c9ba2a43SEric Schrock zil_replay(os, zd, ztest_replay_vector); 2333c9ba2a43SEric Schrock 2334c9ba2a43SEric Schrock (void) rw_unlock(&zd->zd_zilog_lock); 2335ce636f8bSMatthew Ahrens VERIFY(mutex_unlock(&zd->zd_dirobj_lock) == 0); 2336b24ab676SJeff Bonwick } 2337b24ab676SJeff Bonwick 2338b24ab676SJeff Bonwick /* 2339fa9e4066Sahrens * Verify that we can't destroy an active pool, create an existing pool, 2340fa9e4066Sahrens * or create a pool with a bad vdev spec. 2341fa9e4066Sahrens */ 2342b24ab676SJeff Bonwick /* ARGSUSED */ 2343fa9e4066Sahrens void 2344b24ab676SJeff Bonwick ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id) 2345fa9e4066Sahrens { 2346420dfc95SChris Siden ztest_shared_opts_t *zo = &ztest_opts; 2347fa9e4066Sahrens spa_t *spa; 2348fa9e4066Sahrens nvlist_t *nvroot; 2349fa9e4066Sahrens 2350fa9e4066Sahrens /* 2351fa9e4066Sahrens * Attempt to create using a bad file. 2352fa9e4066Sahrens */ 235325345e46SGeorge Wilson nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1); 2354b24ab676SJeff Bonwick VERIFY3U(ENOENT, ==, 23554445fffbSMatthew Ahrens spa_create("ztest_bad_file", nvroot, NULL, NULL)); 2356fa9e4066Sahrens nvlist_free(nvroot); 2357fa9e4066Sahrens 2358fa9e4066Sahrens /* 2359fa9e4066Sahrens * Attempt to create using a bad mirror. 2360fa9e4066Sahrens */ 236125345e46SGeorge Wilson nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 2, 1); 2362b24ab676SJeff Bonwick VERIFY3U(ENOENT, ==, 23634445fffbSMatthew Ahrens spa_create("ztest_bad_mirror", nvroot, NULL, NULL)); 2364fa9e4066Sahrens nvlist_free(nvroot); 2365fa9e4066Sahrens 2366fa9e4066Sahrens /* 2367fa9e4066Sahrens * Attempt to create an existing pool. It shouldn't matter 2368fa9e4066Sahrens * what's in the nvroot; we should fail with EEXIST. 2369fa9e4066Sahrens */ 2370420dfc95SChris Siden (void) rw_rdlock(&ztest_name_lock); 237125345e46SGeorge Wilson nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1); 23724445fffbSMatthew Ahrens VERIFY3U(EEXIST, ==, spa_create(zo->zo_pool, nvroot, NULL, NULL)); 2373fa9e4066Sahrens nvlist_free(nvroot); 2374b420f3adSRichard Lowe VERIFY3U(0, ==, spa_open(zo->zo_pool, &spa, FTAG)); 2375420dfc95SChris Siden VERIFY3U(EBUSY, ==, spa_destroy(zo->zo_pool)); 2376fa9e4066Sahrens spa_close(spa, FTAG); 2377b24ab676SJeff Bonwick 2378420dfc95SChris Siden (void) rw_unlock(&ztest_name_lock); 2379fa9e4066Sahrens } 2380fa9e4066Sahrens 238125345e46SGeorge Wilson /* ARGSUSED */ 238225345e46SGeorge Wilson void 238325345e46SGeorge Wilson ztest_spa_upgrade(ztest_ds_t *zd, uint64_t id) 238425345e46SGeorge Wilson { 238525345e46SGeorge Wilson spa_t *spa; 238625345e46SGeorge Wilson uint64_t initial_version = SPA_VERSION_INITIAL; 238725345e46SGeorge Wilson uint64_t version, newversion; 238825345e46SGeorge Wilson nvlist_t *nvroot, *props; 238925345e46SGeorge Wilson char *name; 239025345e46SGeorge Wilson 239125345e46SGeorge Wilson VERIFY0(mutex_lock(&ztest_vdev_lock)); 239225345e46SGeorge Wilson name = kmem_asprintf("%s_upgrade", ztest_opts.zo_pool); 239325345e46SGeorge Wilson 239425345e46SGeorge Wilson /* 239525345e46SGeorge Wilson * Clean up from previous runs. 239625345e46SGeorge Wilson */ 239725345e46SGeorge Wilson (void) spa_destroy(name); 239825345e46SGeorge Wilson 239925345e46SGeorge Wilson nvroot = make_vdev_root(NULL, NULL, name, ztest_opts.zo_vdev_size, 0, 240025345e46SGeorge Wilson 0, ztest_opts.zo_raidz, ztest_opts.zo_mirrors, 1); 240125345e46SGeorge Wilson 240225345e46SGeorge Wilson /* 240325345e46SGeorge Wilson * If we're configuring a RAIDZ device then make sure that the 240425345e46SGeorge Wilson * the initial version is capable of supporting that feature. 240525345e46SGeorge Wilson */ 240625345e46SGeorge Wilson switch (ztest_opts.zo_raidz_parity) { 240725345e46SGeorge Wilson case 0: 240825345e46SGeorge Wilson case 1: 240925345e46SGeorge Wilson initial_version = SPA_VERSION_INITIAL; 241025345e46SGeorge Wilson break; 241125345e46SGeorge Wilson case 2: 241225345e46SGeorge Wilson initial_version = SPA_VERSION_RAIDZ2; 241325345e46SGeorge Wilson break; 241425345e46SGeorge Wilson case 3: 241525345e46SGeorge Wilson initial_version = SPA_VERSION_RAIDZ3; 241625345e46SGeorge Wilson break; 241725345e46SGeorge Wilson } 241825345e46SGeorge Wilson 241925345e46SGeorge Wilson /* 242025345e46SGeorge Wilson * Create a pool with a spa version that can be upgraded. Pick 242125345e46SGeorge Wilson * a value between initial_version and SPA_VERSION_BEFORE_FEATURES. 242225345e46SGeorge Wilson */ 242325345e46SGeorge Wilson do { 242425345e46SGeorge Wilson version = ztest_random_spa_version(initial_version); 242525345e46SGeorge Wilson } while (version > SPA_VERSION_BEFORE_FEATURES); 242625345e46SGeorge Wilson 242725345e46SGeorge Wilson props = fnvlist_alloc(); 242825345e46SGeorge Wilson fnvlist_add_uint64(props, 242925345e46SGeorge Wilson zpool_prop_to_name(ZPOOL_PROP_VERSION), version); 243025345e46SGeorge Wilson VERIFY0(spa_create(name, nvroot, props, NULL)); 243125345e46SGeorge Wilson fnvlist_free(nvroot); 243225345e46SGeorge Wilson fnvlist_free(props); 243325345e46SGeorge Wilson 243425345e46SGeorge Wilson VERIFY0(spa_open(name, &spa, FTAG)); 243525345e46SGeorge Wilson VERIFY3U(spa_version(spa), ==, version); 243625345e46SGeorge Wilson newversion = ztest_random_spa_version(version + 1); 243725345e46SGeorge Wilson 243825345e46SGeorge Wilson if (ztest_opts.zo_verbose >= 4) { 243925345e46SGeorge Wilson (void) printf("upgrading spa version from %llu to %llu\n", 244025345e46SGeorge Wilson (u_longlong_t)version, (u_longlong_t)newversion); 244125345e46SGeorge Wilson } 244225345e46SGeorge Wilson 244325345e46SGeorge Wilson spa_upgrade(spa, newversion); 244425345e46SGeorge Wilson VERIFY3U(spa_version(spa), >, version); 244525345e46SGeorge Wilson VERIFY3U(spa_version(spa), ==, fnvlist_lookup_uint64(spa->spa_config, 244625345e46SGeorge Wilson zpool_prop_to_name(ZPOOL_PROP_VERSION))); 244725345e46SGeorge Wilson spa_close(spa, FTAG); 244825345e46SGeorge Wilson 244925345e46SGeorge Wilson strfree(name); 245025345e46SGeorge Wilson VERIFY0(mutex_unlock(&ztest_vdev_lock)); 245125345e46SGeorge Wilson } 245225345e46SGeorge Wilson 245331157203SJeff Bonwick static vdev_t * 245431157203SJeff Bonwick vdev_lookup_by_path(vdev_t *vd, const char *path) 245531157203SJeff Bonwick { 245631157203SJeff Bonwick vdev_t *mvd; 245731157203SJeff Bonwick 245831157203SJeff Bonwick if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0) 245931157203SJeff Bonwick return (vd); 246031157203SJeff Bonwick 246131157203SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 246231157203SJeff Bonwick if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) != 246331157203SJeff Bonwick NULL) 246431157203SJeff Bonwick return (mvd); 246531157203SJeff Bonwick 246631157203SJeff Bonwick return (NULL); 246731157203SJeff Bonwick } 246831157203SJeff Bonwick 2469fa9e4066Sahrens /* 247088ecc943SGeorge Wilson * Find the first available hole which can be used as a top-level. 247188ecc943SGeorge Wilson */ 247288ecc943SGeorge Wilson int 247388ecc943SGeorge Wilson find_vdev_hole(spa_t *spa) 247488ecc943SGeorge Wilson { 247588ecc943SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 247688ecc943SGeorge Wilson int c; 247788ecc943SGeorge Wilson 247888ecc943SGeorge Wilson ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV); 247988ecc943SGeorge Wilson 248088ecc943SGeorge Wilson for (c = 0; c < rvd->vdev_children; c++) { 248188ecc943SGeorge Wilson vdev_t *cvd = rvd->vdev_child[c]; 248288ecc943SGeorge Wilson 248388ecc943SGeorge Wilson if (cvd->vdev_ishole) 248488ecc943SGeorge Wilson break; 248588ecc943SGeorge Wilson } 248688ecc943SGeorge Wilson return (c); 248788ecc943SGeorge Wilson } 248888ecc943SGeorge Wilson 248988ecc943SGeorge Wilson /* 2490fa9e4066Sahrens * Verify that vdev_add() works as expected. 2491fa9e4066Sahrens */ 2492b24ab676SJeff Bonwick /* ARGSUSED */ 2493fa9e4066Sahrens void 2494b24ab676SJeff Bonwick ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id) 2495fa9e4066Sahrens { 2496b24ab676SJeff Bonwick ztest_shared_t *zs = ztest_shared; 2497420dfc95SChris Siden spa_t *spa = ztest_spa; 24981195e687SMark J Musante uint64_t leaves; 249988ecc943SGeorge Wilson uint64_t guid; 2500fa9e4066Sahrens nvlist_t *nvroot; 2501fa9e4066Sahrens int error; 2502fa9e4066Sahrens 2503420dfc95SChris Siden VERIFY(mutex_lock(&ztest_vdev_lock) == 0); 25043b2aab18SMatthew Ahrens leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * ztest_opts.zo_raidz; 2505fa9e4066Sahrens 2506e14bb325SJeff Bonwick spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); 2507fa9e4066Sahrens 250888ecc943SGeorge Wilson ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves; 2509fa9e4066Sahrens 251088ecc943SGeorge Wilson /* 251188ecc943SGeorge Wilson * If we have slogs then remove them 1/4 of the time. 251288ecc943SGeorge Wilson */ 251388ecc943SGeorge Wilson if (spa_has_slogs(spa) && ztest_random(4) == 0) { 251488ecc943SGeorge Wilson /* 251588ecc943SGeorge Wilson * Grab the guid from the head of the log class rotor. 251688ecc943SGeorge Wilson */ 2517b24ab676SJeff Bonwick guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid; 251888ecc943SGeorge Wilson 251988ecc943SGeorge Wilson spa_config_exit(spa, SCL_VDEV, FTAG); 252088ecc943SGeorge Wilson 252188ecc943SGeorge Wilson /* 252288ecc943SGeorge Wilson * We have to grab the zs_name_lock as writer to 252388ecc943SGeorge Wilson * prevent a race between removing a slog (dmu_objset_find) 252488ecc943SGeorge Wilson * and destroying a dataset. Removing the slog will 252588ecc943SGeorge Wilson * grab a reference on the dataset which may cause 252688ecc943SGeorge Wilson * dmu_objset_destroy() to fail with EBUSY thus 252788ecc943SGeorge Wilson * leaving the dataset in an inconsistent state. 252888ecc943SGeorge Wilson */ 2529420dfc95SChris Siden VERIFY(rw_wrlock(&ztest_name_lock) == 0); 253088ecc943SGeorge Wilson error = spa_vdev_remove(spa, guid, B_FALSE); 2531420dfc95SChris Siden VERIFY(rw_unlock(&ztest_name_lock) == 0); 253288ecc943SGeorge Wilson 253388ecc943SGeorge Wilson if (error && error != EEXIST) 253488ecc943SGeorge Wilson fatal(0, "spa_vdev_remove() = %d", error); 253588ecc943SGeorge Wilson } else { 2536e14bb325SJeff Bonwick spa_config_exit(spa, SCL_VDEV, FTAG); 2537fa9e4066Sahrens 25388654d025Sperrin /* 25398654d025Sperrin * Make 1/4 of the devices be log devices. 25408654d025Sperrin */ 254125345e46SGeorge Wilson nvroot = make_vdev_root(NULL, NULL, NULL, 2542420dfc95SChris Siden ztest_opts.zo_vdev_size, 0, 2543420dfc95SChris Siden ztest_random(4) == 0, ztest_opts.zo_raidz, 2544420dfc95SChris Siden zs->zs_mirrors, 1); 25458654d025Sperrin 2546fa9e4066Sahrens error = spa_vdev_add(spa, nvroot); 2547fa9e4066Sahrens nvlist_free(nvroot); 2548fa9e4066Sahrens 2549fa9e4066Sahrens if (error == ENOSPC) 2550fa9e4066Sahrens ztest_record_enospc("spa_vdev_add"); 2551fa9e4066Sahrens else if (error != 0) 2552fa9e4066Sahrens fatal(0, "spa_vdev_add() = %d", error); 2553e14bb325SJeff Bonwick } 2554fa9e4066Sahrens 2555420dfc95SChris Siden VERIFY(mutex_unlock(&ztest_vdev_lock) == 0); 255688ecc943SGeorge Wilson } 255788ecc943SGeorge Wilson 2558e14bb325SJeff Bonwick /* 2559e14bb325SJeff Bonwick * Verify that adding/removing aux devices (l2arc, hot spare) works as expected. 2560e14bb325SJeff Bonwick */ 2561b24ab676SJeff Bonwick /* ARGSUSED */ 2562e14bb325SJeff Bonwick void 2563b24ab676SJeff Bonwick ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id) 2564e14bb325SJeff Bonwick { 2565b24ab676SJeff Bonwick ztest_shared_t *zs = ztest_shared; 2566420dfc95SChris Siden spa_t *spa = ztest_spa; 256731157203SJeff Bonwick vdev_t *rvd = spa->spa_root_vdev; 2568e14bb325SJeff Bonwick spa_aux_vdev_t *sav; 2569e14bb325SJeff Bonwick char *aux; 2570e14bb325SJeff Bonwick uint64_t guid = 0; 2571e14bb325SJeff Bonwick int error; 2572e14bb325SJeff Bonwick 257331157203SJeff Bonwick if (ztest_random(2) == 0) { 2574e14bb325SJeff Bonwick sav = &spa->spa_spares; 2575e14bb325SJeff Bonwick aux = ZPOOL_CONFIG_SPARES; 2576e14bb325SJeff Bonwick } else { 2577e14bb325SJeff Bonwick sav = &spa->spa_l2cache; 2578e14bb325SJeff Bonwick aux = ZPOOL_CONFIG_L2CACHE; 2579e14bb325SJeff Bonwick } 2580e14bb325SJeff Bonwick 2581420dfc95SChris Siden VERIFY(mutex_lock(&ztest_vdev_lock) == 0); 2582e14bb325SJeff Bonwick 2583e14bb325SJeff Bonwick spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); 2584e14bb325SJeff Bonwick 2585e14bb325SJeff Bonwick if (sav->sav_count != 0 && ztest_random(4) == 0) { 2586e14bb325SJeff Bonwick /* 2587e14bb325SJeff Bonwick * Pick a random device to remove. 2588e14bb325SJeff Bonwick */ 2589e14bb325SJeff Bonwick guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid; 2590e14bb325SJeff Bonwick } else { 2591e14bb325SJeff Bonwick /* 2592e14bb325SJeff Bonwick * Find an unused device we can add. 2593e14bb325SJeff Bonwick */ 2594b24ab676SJeff Bonwick zs->zs_vdev_aux = 0; 2595e14bb325SJeff Bonwick for (;;) { 2596e14bb325SJeff Bonwick char path[MAXPATHLEN]; 2597e14bb325SJeff Bonwick int c; 2598420dfc95SChris Siden (void) snprintf(path, sizeof (path), ztest_aux_template, 2599420dfc95SChris Siden ztest_opts.zo_dir, ztest_opts.zo_pool, aux, 2600420dfc95SChris Siden zs->zs_vdev_aux); 2601e14bb325SJeff Bonwick for (c = 0; c < sav->sav_count; c++) 2602e14bb325SJeff Bonwick if (strcmp(sav->sav_vdevs[c]->vdev_path, 2603e14bb325SJeff Bonwick path) == 0) 2604e14bb325SJeff Bonwick break; 260531157203SJeff Bonwick if (c == sav->sav_count && 260631157203SJeff Bonwick vdev_lookup_by_path(rvd, path) == NULL) 2607e14bb325SJeff Bonwick break; 2608b24ab676SJeff Bonwick zs->zs_vdev_aux++; 2609e14bb325SJeff Bonwick } 2610e14bb325SJeff Bonwick } 2611e14bb325SJeff Bonwick 2612e14bb325SJeff Bonwick spa_config_exit(spa, SCL_VDEV, FTAG); 2613e14bb325SJeff Bonwick 2614e14bb325SJeff Bonwick if (guid == 0) { 2615e14bb325SJeff Bonwick /* 2616e14bb325SJeff Bonwick * Add a new device. 2617e14bb325SJeff Bonwick */ 261825345e46SGeorge Wilson nvlist_t *nvroot = make_vdev_root(NULL, aux, NULL, 2619420dfc95SChris Siden (ztest_opts.zo_vdev_size * 5) / 4, 0, 0, 0, 0, 1); 2620e14bb325SJeff Bonwick error = spa_vdev_add(spa, nvroot); 2621e14bb325SJeff Bonwick if (error != 0) 2622e14bb325SJeff Bonwick fatal(0, "spa_vdev_add(%p) = %d", nvroot, error); 2623e14bb325SJeff Bonwick nvlist_free(nvroot); 2624e14bb325SJeff Bonwick } else { 2625e14bb325SJeff Bonwick /* 2626e14bb325SJeff Bonwick * Remove an existing device. Sometimes, dirty its 2627e14bb325SJeff Bonwick * vdev state first to make sure we handle removal 2628e14bb325SJeff Bonwick * of devices that have pending state changes. 2629e14bb325SJeff Bonwick */ 2630e14bb325SJeff Bonwick if (ztest_random(2) == 0) 2631573ca77eSGeorge Wilson (void) vdev_online(spa, guid, 0, NULL); 2632e14bb325SJeff Bonwick 2633e14bb325SJeff Bonwick error = spa_vdev_remove(spa, guid, B_FALSE); 2634e14bb325SJeff Bonwick if (error != 0 && error != EBUSY) 2635e14bb325SJeff Bonwick fatal(0, "spa_vdev_remove(%llu) = %d", guid, error); 2636e14bb325SJeff Bonwick } 2637e14bb325SJeff Bonwick 2638420dfc95SChris Siden VERIFY(mutex_unlock(&ztest_vdev_lock) == 0); 2639fa9e4066Sahrens } 2640fa9e4066Sahrens 2641fa9e4066Sahrens /* 26421195e687SMark J Musante * split a pool if it has mirror tlvdevs 26431195e687SMark J Musante */ 26441195e687SMark J Musante /* ARGSUSED */ 26451195e687SMark J Musante void 26461195e687SMark J Musante ztest_split_pool(ztest_ds_t *zd, uint64_t id) 26471195e687SMark J Musante { 26481195e687SMark J Musante ztest_shared_t *zs = ztest_shared; 2649420dfc95SChris Siden spa_t *spa = ztest_spa; 26501195e687SMark J Musante vdev_t *rvd = spa->spa_root_vdev; 26511195e687SMark J Musante nvlist_t *tree, **child, *config, *split, **schild; 26521195e687SMark J Musante uint_t c, children, schildren = 0, lastlogid = 0; 26531195e687SMark J Musante int error = 0; 26541195e687SMark J Musante 2655420dfc95SChris Siden VERIFY(mutex_lock(&ztest_vdev_lock) == 0); 26561195e687SMark J Musante 26571195e687SMark J Musante /* ensure we have a useable config; mirrors of raidz aren't supported */ 2658420dfc95SChris Siden if (zs->zs_mirrors < 3 || ztest_opts.zo_raidz > 1) { 2659420dfc95SChris Siden VERIFY(mutex_unlock(&ztest_vdev_lock) == 0); 26601195e687SMark J Musante return; 26611195e687SMark J Musante } 26621195e687SMark J Musante 26631195e687SMark J Musante /* clean up the old pool, if any */ 26641195e687SMark J Musante (void) spa_destroy("splitp"); 26651195e687SMark J Musante 26661195e687SMark J Musante spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); 26671195e687SMark J Musante 26681195e687SMark J Musante /* generate a config from the existing config */ 266998295d61SMark J Musante mutex_enter(&spa->spa_props_lock); 26701195e687SMark J Musante VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE, 26711195e687SMark J Musante &tree) == 0); 267298295d61SMark J Musante mutex_exit(&spa->spa_props_lock); 267398295d61SMark J Musante 26741195e687SMark J Musante VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child, 26751195e687SMark J Musante &children) == 0); 26761195e687SMark J Musante 26771195e687SMark J Musante schild = malloc(rvd->vdev_children * sizeof (nvlist_t *)); 26781195e687SMark J Musante for (c = 0; c < children; c++) { 26791195e687SMark J Musante vdev_t *tvd = rvd->vdev_child[c]; 26801195e687SMark J Musante nvlist_t **mchild; 26811195e687SMark J Musante uint_t mchildren; 26821195e687SMark J Musante 26831195e687SMark J Musante if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) { 26841195e687SMark J Musante VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME, 26851195e687SMark J Musante 0) == 0); 26861195e687SMark J Musante VERIFY(nvlist_add_string(schild[schildren], 26871195e687SMark J Musante ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0); 26881195e687SMark J Musante VERIFY(nvlist_add_uint64(schild[schildren], 26891195e687SMark J Musante ZPOOL_CONFIG_IS_HOLE, 1) == 0); 26901195e687SMark J Musante if (lastlogid == 0) 26911195e687SMark J Musante lastlogid = schildren; 26921195e687SMark J Musante ++schildren; 26931195e687SMark J Musante continue; 26941195e687SMark J Musante } 26951195e687SMark J Musante lastlogid = 0; 26961195e687SMark J Musante VERIFY(nvlist_lookup_nvlist_array(child[c], 26971195e687SMark J Musante ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0); 26981195e687SMark J Musante VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0); 26991195e687SMark J Musante } 27001195e687SMark J Musante 27011195e687SMark J Musante /* OK, create a config that can be used to split */ 27021195e687SMark J Musante VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0); 27031195e687SMark J Musante VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE, 27041195e687SMark J Musante VDEV_TYPE_ROOT) == 0); 27051195e687SMark J Musante VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild, 27061195e687SMark J Musante lastlogid != 0 ? lastlogid : schildren) == 0); 27071195e687SMark J Musante 27081195e687SMark J Musante VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0); 27091195e687SMark J Musante VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0); 27101195e687SMark J Musante 27111195e687SMark J Musante for (c = 0; c < schildren; c++) 27121195e687SMark J Musante nvlist_free(schild[c]); 27131195e687SMark J Musante free(schild); 27141195e687SMark J Musante nvlist_free(split); 27151195e687SMark J Musante 27161195e687SMark J Musante spa_config_exit(spa, SCL_VDEV, FTAG); 27171195e687SMark J Musante 2718420dfc95SChris Siden (void) rw_wrlock(&ztest_name_lock); 27191195e687SMark J Musante error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE); 2720420dfc95SChris Siden (void) rw_unlock(&ztest_name_lock); 27211195e687SMark J Musante 27221195e687SMark J Musante nvlist_free(config); 27231195e687SMark J Musante 27241195e687SMark J Musante if (error == 0) { 27251195e687SMark J Musante (void) printf("successful split - results:\n"); 27261195e687SMark J Musante mutex_enter(&spa_namespace_lock); 27271195e687SMark J Musante show_pool_stats(spa); 27281195e687SMark J Musante show_pool_stats(spa_lookup("splitp")); 27291195e687SMark J Musante mutex_exit(&spa_namespace_lock); 27301195e687SMark J Musante ++zs->zs_splits; 27311195e687SMark J Musante --zs->zs_mirrors; 27321195e687SMark J Musante } 2733420dfc95SChris Siden VERIFY(mutex_unlock(&ztest_vdev_lock) == 0); 27341195e687SMark J Musante 27351195e687SMark J Musante } 27361195e687SMark J Musante 27371195e687SMark J Musante /* 2738fa9e4066Sahrens * Verify that we can attach and detach devices. 2739fa9e4066Sahrens */ 2740b24ab676SJeff Bonwick /* ARGSUSED */ 2741fa9e4066Sahrens void 2742b24ab676SJeff Bonwick ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id) 2743fa9e4066Sahrens { 2744b24ab676SJeff Bonwick ztest_shared_t *zs = ztest_shared; 2745420dfc95SChris Siden spa_t *spa = ztest_spa; 274631157203SJeff Bonwick spa_aux_vdev_t *sav = &spa->spa_spares; 2747fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 2748ea8dc4b6Seschrock vdev_t *oldvd, *newvd, *pvd; 2749e14bb325SJeff Bonwick nvlist_t *root; 27501195e687SMark J Musante uint64_t leaves; 2751fa9e4066Sahrens uint64_t leaf, top; 2752ecc2d604Sbonwick uint64_t ashift = ztest_get_ashift(); 27538ad4d6ddSJeff Bonwick uint64_t oldguid, pguid; 2754b4952e17SGeorge Wilson uint64_t oldsize, newsize; 2755ea8dc4b6Seschrock char oldpath[MAXPATHLEN], newpath[MAXPATHLEN]; 2756fa9e4066Sahrens int replacing; 27579af0a4dfSJeff Bonwick int oldvd_has_siblings = B_FALSE; 275831157203SJeff Bonwick int newvd_is_spare = B_FALSE; 275931157203SJeff Bonwick int oldvd_is_log; 2760fa9e4066Sahrens int error, expected_error; 2761fa9e4066Sahrens 2762420dfc95SChris Siden VERIFY(mutex_lock(&ztest_vdev_lock) == 0); 2763420dfc95SChris Siden leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz; 2764fa9e4066Sahrens 2765e14bb325SJeff Bonwick spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); 2766fa9e4066Sahrens 2767fa9e4066Sahrens /* 2768fa9e4066Sahrens * Decide whether to do an attach or a replace. 2769fa9e4066Sahrens */ 2770fa9e4066Sahrens replacing = ztest_random(2); 2771fa9e4066Sahrens 2772fa9e4066Sahrens /* 2773fa9e4066Sahrens * Pick a random top-level vdev. 2774fa9e4066Sahrens */ 2775b24ab676SJeff Bonwick top = ztest_random_vdev_top(spa, B_TRUE); 2776fa9e4066Sahrens 2777fa9e4066Sahrens /* 2778fa9e4066Sahrens * Pick a random leaf within it. 2779fa9e4066Sahrens */ 2780d41c4376SMark J Musante leaf = ztest_random(leaves); 2781fa9e4066Sahrens 2782fa9e4066Sahrens /* 278331157203SJeff Bonwick * Locate this vdev. 2784fa9e4066Sahrens */ 278531157203SJeff Bonwick oldvd = rvd->vdev_child[top]; 27861195e687SMark J Musante if (zs->zs_mirrors >= 1) { 27878ad4d6ddSJeff Bonwick ASSERT(oldvd->vdev_ops == &vdev_mirror_ops); 27881195e687SMark J Musante ASSERT(oldvd->vdev_children >= zs->zs_mirrors); 2789420dfc95SChris Siden oldvd = oldvd->vdev_child[leaf / ztest_opts.zo_raidz]; 27908ad4d6ddSJeff Bonwick } 2791420dfc95SChris Siden if (ztest_opts.zo_raidz > 1) { 27928ad4d6ddSJeff Bonwick ASSERT(oldvd->vdev_ops == &vdev_raidz_ops); 2793420dfc95SChris Siden ASSERT(oldvd->vdev_children == ztest_opts.zo_raidz); 2794420dfc95SChris Siden oldvd = oldvd->vdev_child[leaf % ztest_opts.zo_raidz]; 27958ad4d6ddSJeff Bonwick } 2796fa9e4066Sahrens 2797fa9e4066Sahrens /* 279831157203SJeff Bonwick * If we're already doing an attach or replace, oldvd may be a 279931157203SJeff Bonwick * mirror vdev -- in which case, pick a random child. 2800fa9e4066Sahrens */ 280131157203SJeff Bonwick while (oldvd->vdev_children != 0) { 28029af0a4dfSJeff Bonwick oldvd_has_siblings = B_TRUE; 28038ad4d6ddSJeff Bonwick ASSERT(oldvd->vdev_children >= 2); 28048ad4d6ddSJeff Bonwick oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)]; 280531157203SJeff Bonwick } 2806fa9e4066Sahrens 280731157203SJeff Bonwick oldguid = oldvd->vdev_guid; 2808573ca77eSGeorge Wilson oldsize = vdev_get_min_asize(oldvd); 280931157203SJeff Bonwick oldvd_is_log = oldvd->vdev_top->vdev_islog; 281031157203SJeff Bonwick (void) strcpy(oldpath, oldvd->vdev_path); 2811ea8dc4b6Seschrock pvd = oldvd->vdev_parent; 28128ad4d6ddSJeff Bonwick pguid = pvd->vdev_guid; 2813fa9e4066Sahrens 2814fa9e4066Sahrens /* 28159af0a4dfSJeff Bonwick * If oldvd has siblings, then half of the time, detach it. 28169af0a4dfSJeff Bonwick */ 28179af0a4dfSJeff Bonwick if (oldvd_has_siblings && ztest_random(2) == 0) { 28189af0a4dfSJeff Bonwick spa_config_exit(spa, SCL_VDEV, FTAG); 28198ad4d6ddSJeff Bonwick error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE); 28208ad4d6ddSJeff Bonwick if (error != 0 && error != ENODEV && error != EBUSY && 28218ad4d6ddSJeff Bonwick error != ENOTSUP) 28228ad4d6ddSJeff Bonwick fatal(0, "detach (%s) returned %d", oldpath, error); 2823420dfc95SChris Siden VERIFY(mutex_unlock(&ztest_vdev_lock) == 0); 28249af0a4dfSJeff Bonwick return; 28259af0a4dfSJeff Bonwick } 28269af0a4dfSJeff Bonwick 28279af0a4dfSJeff Bonwick /* 282831157203SJeff Bonwick * For the new vdev, choose with equal probability between the two 282931157203SJeff Bonwick * standard paths (ending in either 'a' or 'b') or a random hot spare. 283031157203SJeff Bonwick */ 283131157203SJeff Bonwick if (sav->sav_count != 0 && ztest_random(3) == 0) { 283231157203SJeff Bonwick newvd = sav->sav_vdevs[ztest_random(sav->sav_count)]; 283331157203SJeff Bonwick newvd_is_spare = B_TRUE; 283431157203SJeff Bonwick (void) strcpy(newpath, newvd->vdev_path); 283531157203SJeff Bonwick } else { 283631157203SJeff Bonwick (void) snprintf(newpath, sizeof (newpath), ztest_dev_template, 2837420dfc95SChris Siden ztest_opts.zo_dir, ztest_opts.zo_pool, 2838420dfc95SChris Siden top * leaves + leaf); 283931157203SJeff Bonwick if (ztest_random(2) == 0) 284031157203SJeff Bonwick newpath[strlen(newpath) - 1] = 'b'; 284131157203SJeff Bonwick newvd = vdev_lookup_by_path(rvd, newpath); 284231157203SJeff Bonwick } 284331157203SJeff Bonwick 284431157203SJeff Bonwick if (newvd) { 2845573ca77eSGeorge Wilson newsize = vdev_get_min_asize(newvd); 284631157203SJeff Bonwick } else { 284731157203SJeff Bonwick /* 2848ea8dc4b6Seschrock * Make newsize a little bigger or smaller than oldsize. 2849fa9e4066Sahrens * If it's smaller, the attach should fail. 2850fa9e4066Sahrens * If it's larger, and we're doing a replace, 2851fa9e4066Sahrens * we should get dynamic LUN growth when we're done. 2852fa9e4066Sahrens */ 2853ea8dc4b6Seschrock newsize = 10 * oldsize / (9 + ztest_random(3)); 285431157203SJeff Bonwick } 2855fa9e4066Sahrens 2856fa9e4066Sahrens /* 2857fa9e4066Sahrens * If pvd is not a mirror or root, the attach should fail with ENOTSUP, 2858fa9e4066Sahrens * unless it's a replace; in that case any non-replacing parent is OK. 2859fa9e4066Sahrens * 2860ea8dc4b6Seschrock * If newvd is already part of the pool, it should fail with EBUSY. 2861fa9e4066Sahrens * 2862ea8dc4b6Seschrock * If newvd is too small, it should fail with EOVERFLOW. 2863fa9e4066Sahrens */ 286431157203SJeff Bonwick if (pvd->vdev_ops != &vdev_mirror_ops && 286531157203SJeff Bonwick pvd->vdev_ops != &vdev_root_ops && (!replacing || 286631157203SJeff Bonwick pvd->vdev_ops == &vdev_replacing_ops || 286731157203SJeff Bonwick pvd->vdev_ops == &vdev_spare_ops)) 2868fa9e4066Sahrens expected_error = ENOTSUP; 286931157203SJeff Bonwick else if (newvd_is_spare && (!replacing || oldvd_is_log)) 287031157203SJeff Bonwick expected_error = ENOTSUP; 287131157203SJeff Bonwick else if (newvd == oldvd) 287231157203SJeff Bonwick expected_error = replacing ? 0 : EBUSY; 287331157203SJeff Bonwick else if (vdev_lookup_by_path(rvd, newpath) != NULL) 287431157203SJeff Bonwick expected_error = EBUSY; 2875ea8dc4b6Seschrock else if (newsize < oldsize) 2876fa9e4066Sahrens expected_error = EOVERFLOW; 2877ecc2d604Sbonwick else if (ashift > oldvd->vdev_top->vdev_ashift) 2878ecc2d604Sbonwick expected_error = EDOM; 2879fa9e4066Sahrens else 2880fa9e4066Sahrens expected_error = 0; 2881fa9e4066Sahrens 2882e14bb325SJeff Bonwick spa_config_exit(spa, SCL_VDEV, FTAG); 2883fa9e4066Sahrens 2884fa9e4066Sahrens /* 2885ea8dc4b6Seschrock * Build the nvlist describing newpath. 2886fa9e4066Sahrens */ 288725345e46SGeorge Wilson root = make_vdev_root(newpath, NULL, NULL, newvd == NULL ? newsize : 0, 2888e14bb325SJeff Bonwick ashift, 0, 0, 0, 1); 2889fa9e4066Sahrens 289031157203SJeff Bonwick error = spa_vdev_attach(spa, oldguid, root, replacing); 2891fa9e4066Sahrens 2892fa9e4066Sahrens nvlist_free(root); 2893fa9e4066Sahrens 2894fa9e4066Sahrens /* 2895fa9e4066Sahrens * If our parent was the replacing vdev, but the replace completed, 2896fa9e4066Sahrens * then instead of failing with ENOTSUP we may either succeed, 2897fa9e4066Sahrens * fail with ENODEV, or fail with EOVERFLOW. 2898fa9e4066Sahrens */ 2899fa9e4066Sahrens if (expected_error == ENOTSUP && 2900fa9e4066Sahrens (error == 0 || error == ENODEV || error == EOVERFLOW)) 2901fa9e4066Sahrens expected_error = error; 2902fa9e4066Sahrens 2903f0aa80d4Sbonwick /* 2904f0aa80d4Sbonwick * If someone grew the LUN, the replacement may be too small. 2905f0aa80d4Sbonwick */ 2906088f3894Sahrens if (error == EOVERFLOW || error == EBUSY) 2907f0aa80d4Sbonwick expected_error = error; 2908f0aa80d4Sbonwick 2909088f3894Sahrens /* XXX workaround 6690467 */ 2910088f3894Sahrens if (error != expected_error && expected_error != EBUSY) { 2911088f3894Sahrens fatal(0, "attach (%s %llu, %s %llu, %d) " 2912088f3894Sahrens "returned %d, expected %d", 2913b4952e17SGeorge Wilson oldpath, oldsize, newpath, 2914b4952e17SGeorge Wilson newsize, replacing, error, expected_error); 2915fa9e4066Sahrens } 2916fa9e4066Sahrens 2917420dfc95SChris Siden VERIFY(mutex_unlock(&ztest_vdev_lock) == 0); 2918fa9e4066Sahrens } 2919fa9e4066Sahrens 2920fa9e4066Sahrens /* 2921573ca77eSGeorge Wilson * Callback function which expands the physical size of the vdev. 2922573ca77eSGeorge Wilson */ 2923573ca77eSGeorge Wilson vdev_t * 2924573ca77eSGeorge Wilson grow_vdev(vdev_t *vd, void *arg) 2925573ca77eSGeorge Wilson { 2926573ca77eSGeorge Wilson spa_t *spa = vd->vdev_spa; 2927573ca77eSGeorge Wilson size_t *newsize = arg; 2928573ca77eSGeorge Wilson size_t fsize; 2929573ca77eSGeorge Wilson int fd; 2930573ca77eSGeorge Wilson 2931573ca77eSGeorge Wilson ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE); 2932573ca77eSGeorge Wilson ASSERT(vd->vdev_ops->vdev_op_leaf); 2933573ca77eSGeorge Wilson 2934573ca77eSGeorge Wilson if ((fd = open(vd->vdev_path, O_RDWR)) == -1) 2935573ca77eSGeorge Wilson return (vd); 2936573ca77eSGeorge Wilson 2937573ca77eSGeorge Wilson fsize = lseek(fd, 0, SEEK_END); 2938573ca77eSGeorge Wilson (void) ftruncate(fd, *newsize); 2939573ca77eSGeorge Wilson 2940420dfc95SChris Siden if (ztest_opts.zo_verbose >= 6) { 2941573ca77eSGeorge Wilson (void) printf("%s grew from %lu to %lu bytes\n", 2942573ca77eSGeorge Wilson vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize); 2943573ca77eSGeorge Wilson } 2944573ca77eSGeorge Wilson (void) close(fd); 2945573ca77eSGeorge Wilson return (NULL); 2946573ca77eSGeorge Wilson } 2947573ca77eSGeorge Wilson 2948573ca77eSGeorge Wilson /* 2949573ca77eSGeorge Wilson * Callback function which expands a given vdev by calling vdev_online(). 2950573ca77eSGeorge Wilson */ 2951573ca77eSGeorge Wilson /* ARGSUSED */ 2952573ca77eSGeorge Wilson vdev_t * 2953573ca77eSGeorge Wilson online_vdev(vdev_t *vd, void *arg) 2954573ca77eSGeorge Wilson { 2955573ca77eSGeorge Wilson spa_t *spa = vd->vdev_spa; 2956573ca77eSGeorge Wilson vdev_t *tvd = vd->vdev_top; 2957573ca77eSGeorge Wilson uint64_t guid = vd->vdev_guid; 29588f18d1faSGeorge Wilson uint64_t generation = spa->spa_config_generation + 1; 2959095bcd66SGeorge Wilson vdev_state_t newstate = VDEV_STATE_UNKNOWN; 2960095bcd66SGeorge Wilson int error; 2961573ca77eSGeorge Wilson 2962573ca77eSGeorge Wilson ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE); 2963573ca77eSGeorge Wilson ASSERT(vd->vdev_ops->vdev_op_leaf); 2964573ca77eSGeorge Wilson 2965573ca77eSGeorge Wilson /* Calling vdev_online will initialize the new metaslabs */ 2966573ca77eSGeorge Wilson spa_config_exit(spa, SCL_STATE, spa); 2967095bcd66SGeorge Wilson error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate); 2968573ca77eSGeorge Wilson spa_config_enter(spa, SCL_STATE, spa, RW_READER); 2969573ca77eSGeorge Wilson 2970573ca77eSGeorge Wilson /* 2971095bcd66SGeorge Wilson * If vdev_online returned an error or the underlying vdev_open 2972095bcd66SGeorge Wilson * failed then we abort the expand. The only way to know that 2973095bcd66SGeorge Wilson * vdev_open fails is by checking the returned newstate. 2974095bcd66SGeorge Wilson */ 2975095bcd66SGeorge Wilson if (error || newstate != VDEV_STATE_HEALTHY) { 2976420dfc95SChris Siden if (ztest_opts.zo_verbose >= 5) { 2977095bcd66SGeorge Wilson (void) printf("Unable to expand vdev, state %llu, " 2978095bcd66SGeorge Wilson "error %d\n", (u_longlong_t)newstate, error); 2979095bcd66SGeorge Wilson } 2980095bcd66SGeorge Wilson return (vd); 2981095bcd66SGeorge Wilson } 2982095bcd66SGeorge Wilson ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY); 2983095bcd66SGeorge Wilson 2984095bcd66SGeorge Wilson /* 2985573ca77eSGeorge Wilson * Since we dropped the lock we need to ensure that we're 2986573ca77eSGeorge Wilson * still talking to the original vdev. It's possible this 2987573ca77eSGeorge Wilson * vdev may have been detached/replaced while we were 2988573ca77eSGeorge Wilson * trying to online it. 2989573ca77eSGeorge Wilson */ 29908f18d1faSGeorge Wilson if (generation != spa->spa_config_generation) { 2991420dfc95SChris Siden if (ztest_opts.zo_verbose >= 5) { 29928f18d1faSGeorge Wilson (void) printf("vdev configuration has changed, " 29938f18d1faSGeorge Wilson "guid %llu, state %llu, expected gen %llu, " 2994b24ab676SJeff Bonwick "got gen %llu\n", 2995b24ab676SJeff Bonwick (u_longlong_t)guid, 29968f18d1faSGeorge Wilson (u_longlong_t)tvd->vdev_state, 29978f18d1faSGeorge Wilson (u_longlong_t)generation, 29988f18d1faSGeorge Wilson (u_longlong_t)spa->spa_config_generation); 2999573ca77eSGeorge Wilson } 3000573ca77eSGeorge Wilson return (vd); 3001573ca77eSGeorge Wilson } 3002573ca77eSGeorge Wilson return (NULL); 3003573ca77eSGeorge Wilson } 3004573ca77eSGeorge Wilson 3005573ca77eSGeorge Wilson /* 3006573ca77eSGeorge Wilson * Traverse the vdev tree calling the supplied function. 3007573ca77eSGeorge Wilson * We continue to walk the tree until we either have walked all 3008573ca77eSGeorge Wilson * children or we receive a non-NULL return from the callback. 3009573ca77eSGeorge Wilson * If a NULL callback is passed, then we just return back the first 3010573ca77eSGeorge Wilson * leaf vdev we encounter. 3011573ca77eSGeorge Wilson */ 3012573ca77eSGeorge Wilson vdev_t * 3013573ca77eSGeorge Wilson vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg) 3014573ca77eSGeorge Wilson { 3015573ca77eSGeorge Wilson if (vd->vdev_ops->vdev_op_leaf) { 3016573ca77eSGeorge Wilson if (func == NULL) 3017573ca77eSGeorge Wilson return (vd); 3018573ca77eSGeorge Wilson else 3019573ca77eSGeorge Wilson return (func(vd, arg)); 3020573ca77eSGeorge Wilson } 3021573ca77eSGeorge Wilson 3022573ca77eSGeorge Wilson for (uint_t c = 0; c < vd->vdev_children; c++) { 3023573ca77eSGeorge Wilson vdev_t *cvd = vd->vdev_child[c]; 3024573ca77eSGeorge Wilson if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL) 3025573ca77eSGeorge Wilson return (cvd); 3026573ca77eSGeorge Wilson } 3027573ca77eSGeorge Wilson return (NULL); 3028573ca77eSGeorge Wilson } 3029573ca77eSGeorge Wilson 3030573ca77eSGeorge Wilson /* 3031fa9e4066Sahrens * Verify that dynamic LUN growth works as expected. 3032fa9e4066Sahrens */ 3033b24ab676SJeff Bonwick /* ARGSUSED */ 3034fa9e4066Sahrens void 3035b24ab676SJeff Bonwick ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id) 3036fa9e4066Sahrens { 3037420dfc95SChris Siden spa_t *spa = ztest_spa; 3038b24ab676SJeff Bonwick vdev_t *vd, *tvd; 3039b24ab676SJeff Bonwick metaslab_class_t *mc; 3040b24ab676SJeff Bonwick metaslab_group_t *mg; 3041573ca77eSGeorge Wilson size_t psize, newsize; 3042b24ab676SJeff Bonwick uint64_t top; 3043b24ab676SJeff Bonwick uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count; 3044fa9e4066Sahrens 3045420dfc95SChris Siden VERIFY(mutex_lock(&ztest_vdev_lock) == 0); 3046573ca77eSGeorge Wilson spa_config_enter(spa, SCL_STATE, spa, RW_READER); 3047573ca77eSGeorge Wilson 3048b24ab676SJeff Bonwick top = ztest_random_vdev_top(spa, B_TRUE); 3049573ca77eSGeorge Wilson 3050b24ab676SJeff Bonwick tvd = spa->spa_root_vdev->vdev_child[top]; 3051b24ab676SJeff Bonwick mg = tvd->vdev_mg; 3052b24ab676SJeff Bonwick mc = mg->mg_class; 3053b24ab676SJeff Bonwick old_ms_count = tvd->vdev_ms_count; 3054b24ab676SJeff Bonwick old_class_space = metaslab_class_get_space(mc); 3055fa9e4066Sahrens 3056fa9e4066Sahrens /* 3057573ca77eSGeorge Wilson * Determine the size of the first leaf vdev associated with 3058573ca77eSGeorge Wilson * our top-level device. 3059fa9e4066Sahrens */ 3060573ca77eSGeorge Wilson vd = vdev_walk_tree(tvd, NULL, NULL); 3061573ca77eSGeorge Wilson ASSERT3P(vd, !=, NULL); 3062573ca77eSGeorge Wilson ASSERT(vd->vdev_ops->vdev_op_leaf); 3063fa9e4066Sahrens 3064573ca77eSGeorge Wilson psize = vd->vdev_psize; 3065fa9e4066Sahrens 3066fa9e4066Sahrens /* 30678f18d1faSGeorge Wilson * We only try to expand the vdev if it's healthy, less than 4x its 30688f18d1faSGeorge Wilson * original size, and it has a valid psize. 3069fa9e4066Sahrens */ 30708f18d1faSGeorge Wilson if (tvd->vdev_state != VDEV_STATE_HEALTHY || 3071420dfc95SChris Siden psize == 0 || psize >= 4 * ztest_opts.zo_vdev_size) { 3072573ca77eSGeorge Wilson spa_config_exit(spa, SCL_STATE, spa); 3073420dfc95SChris Siden VERIFY(mutex_unlock(&ztest_vdev_lock) == 0); 3074573ca77eSGeorge Wilson return; 3075573ca77eSGeorge Wilson } 3076573ca77eSGeorge Wilson ASSERT(psize > 0); 3077573ca77eSGeorge Wilson newsize = psize + psize / 8; 3078573ca77eSGeorge Wilson ASSERT3U(newsize, >, psize); 3079573ca77eSGeorge Wilson 3080420dfc95SChris Siden if (ztest_opts.zo_verbose >= 6) { 3081b24ab676SJeff Bonwick (void) printf("Expanding LUN %s from %lu to %lu\n", 3082573ca77eSGeorge Wilson vd->vdev_path, (ulong_t)psize, (ulong_t)newsize); 3083fa9e4066Sahrens } 3084fa9e4066Sahrens 3085573ca77eSGeorge Wilson /* 3086573ca77eSGeorge Wilson * Growing the vdev is a two step process: 3087573ca77eSGeorge Wilson * 1). expand the physical size (i.e. relabel) 3088573ca77eSGeorge Wilson * 2). online the vdev to create the new metaslabs 3089573ca77eSGeorge Wilson */ 3090573ca77eSGeorge Wilson if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL || 3091573ca77eSGeorge Wilson vdev_walk_tree(tvd, online_vdev, NULL) != NULL || 3092573ca77eSGeorge Wilson tvd->vdev_state != VDEV_STATE_HEALTHY) { 3093420dfc95SChris Siden if (ztest_opts.zo_verbose >= 5) { 3094573ca77eSGeorge Wilson (void) printf("Could not expand LUN because " 30958f18d1faSGeorge Wilson "the vdev configuration changed.\n"); 3096573ca77eSGeorge Wilson } 3097b24ab676SJeff Bonwick spa_config_exit(spa, SCL_STATE, spa); 3098420dfc95SChris Siden VERIFY(mutex_unlock(&ztest_vdev_lock) == 0); 3099573ca77eSGeorge Wilson return; 3100573ca77eSGeorge Wilson } 3101573ca77eSGeorge Wilson 3102b24ab676SJeff Bonwick spa_config_exit(spa, SCL_STATE, spa); 3103573ca77eSGeorge Wilson 3104573ca77eSGeorge Wilson /* 3105573ca77eSGeorge Wilson * Expanding the LUN will update the config asynchronously, 3106573ca77eSGeorge Wilson * thus we must wait for the async thread to complete any 3107573ca77eSGeorge Wilson * pending tasks before proceeding. 3108573ca77eSGeorge Wilson */ 3109b24ab676SJeff Bonwick for (;;) { 3110b24ab676SJeff Bonwick boolean_t done; 3111573ca77eSGeorge Wilson mutex_enter(&spa->spa_async_lock); 3112b24ab676SJeff Bonwick done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks); 3113573ca77eSGeorge Wilson mutex_exit(&spa->spa_async_lock); 3114b24ab676SJeff Bonwick if (done) 3115b24ab676SJeff Bonwick break; 3116b24ab676SJeff Bonwick txg_wait_synced(spa_get_dsl(spa), 0); 3117b24ab676SJeff Bonwick (void) poll(NULL, 0, 100); 3118b24ab676SJeff Bonwick } 3119573ca77eSGeorge Wilson 3120573ca77eSGeorge Wilson spa_config_enter(spa, SCL_STATE, spa, RW_READER); 3121b24ab676SJeff Bonwick 3122b24ab676SJeff Bonwick tvd = spa->spa_root_vdev->vdev_child[top]; 3123b24ab676SJeff Bonwick new_ms_count = tvd->vdev_ms_count; 3124b24ab676SJeff Bonwick new_class_space = metaslab_class_get_space(mc); 3125b24ab676SJeff Bonwick 3126b24ab676SJeff Bonwick if (tvd->vdev_mg != mg || mg->mg_class != mc) { 3127420dfc95SChris Siden if (ztest_opts.zo_verbose >= 5) { 3128b24ab676SJeff Bonwick (void) printf("Could not verify LUN expansion due to " 3129b24ab676SJeff Bonwick "intervening vdev offline or remove.\n"); 3130b24ab676SJeff Bonwick } 3131b24ab676SJeff Bonwick spa_config_exit(spa, SCL_STATE, spa); 3132420dfc95SChris Siden VERIFY(mutex_unlock(&ztest_vdev_lock) == 0); 3133b24ab676SJeff Bonwick return; 3134b24ab676SJeff Bonwick } 3135b24ab676SJeff Bonwick 3136b24ab676SJeff Bonwick /* 3137b24ab676SJeff Bonwick * Make sure we were able to grow the vdev. 3138b24ab676SJeff Bonwick */ 3139b24ab676SJeff Bonwick if (new_ms_count <= old_ms_count) 3140b24ab676SJeff Bonwick fatal(0, "LUN expansion failed: ms_count %llu <= %llu\n", 3141b24ab676SJeff Bonwick old_ms_count, new_ms_count); 3142573ca77eSGeorge Wilson 3143573ca77eSGeorge Wilson /* 3144573ca77eSGeorge Wilson * Make sure we were able to grow the pool. 3145573ca77eSGeorge Wilson */ 3146b24ab676SJeff Bonwick if (new_class_space <= old_class_space) 3147b24ab676SJeff Bonwick fatal(0, "LUN expansion failed: class_space %llu <= %llu\n", 3148b24ab676SJeff Bonwick old_class_space, new_class_space); 3149b24ab676SJeff Bonwick 3150420dfc95SChris Siden if (ztest_opts.zo_verbose >= 5) { 3151573ca77eSGeorge Wilson char oldnumbuf[6], newnumbuf[6]; 3152573ca77eSGeorge Wilson 3153b24ab676SJeff Bonwick nicenum(old_class_space, oldnumbuf); 3154b24ab676SJeff Bonwick nicenum(new_class_space, newnumbuf); 3155573ca77eSGeorge Wilson (void) printf("%s grew from %s to %s\n", 3156573ca77eSGeorge Wilson spa->spa_name, oldnumbuf, newnumbuf); 3157573ca77eSGeorge Wilson } 3158b24ab676SJeff Bonwick 3159573ca77eSGeorge Wilson spa_config_exit(spa, SCL_STATE, spa); 3160420dfc95SChris Siden VERIFY(mutex_unlock(&ztest_vdev_lock) == 0); 3161b24ab676SJeff Bonwick } 3162b24ab676SJeff Bonwick 3163b24ab676SJeff Bonwick /* 3164b24ab676SJeff Bonwick * Verify that dmu_objset_{create,destroy,open,close} work as expected. 3165b24ab676SJeff Bonwick */ 3166b24ab676SJeff Bonwick /* ARGSUSED */ 3167b24ab676SJeff Bonwick static void 3168b24ab676SJeff Bonwick ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) 3169b24ab676SJeff Bonwick { 3170b24ab676SJeff Bonwick /* 3171b24ab676SJeff Bonwick * Create the objects common to all ztest datasets. 3172b24ab676SJeff Bonwick */ 3173b24ab676SJeff Bonwick VERIFY(zap_create_claim(os, ZTEST_DIROBJ, 3174b24ab676SJeff Bonwick DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0); 3175fa9e4066Sahrens } 3176fa9e4066Sahrens 317755da60b9SMark J Musante static int 317855da60b9SMark J Musante ztest_dataset_create(char *dsname) 317955da60b9SMark J Musante { 318055da60b9SMark J Musante uint64_t zilset = ztest_random(100); 318155da60b9SMark J Musante int err = dmu_objset_create(dsname, DMU_OST_OTHER, 0, 318255da60b9SMark J Musante ztest_objset_create_cb, NULL); 318355da60b9SMark J Musante 318455da60b9SMark J Musante if (err || zilset < 80) 318555da60b9SMark J Musante return (err); 318655da60b9SMark J Musante 3187420dfc95SChris Siden if (ztest_opts.zo_verbose >= 6) 318855da60b9SMark J Musante (void) printf("Setting dataset %s to sync always\n", dsname); 318955da60b9SMark J Musante return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC, 319055da60b9SMark J Musante ZFS_SYNC_ALWAYS, B_FALSE)); 319155da60b9SMark J Musante } 319255da60b9SMark J Musante 3193fa9e4066Sahrens /* ARGSUSED */ 31941d452cf5Sahrens static int 3195fd136879SMatthew Ahrens ztest_objset_destroy_cb(const char *name, void *arg) 3196fa9e4066Sahrens { 3197fa9e4066Sahrens objset_t *os; 3198b24ab676SJeff Bonwick dmu_object_info_t doi; 3199fa9e4066Sahrens int error; 3200fa9e4066Sahrens 3201fa9e4066Sahrens /* 3202fa9e4066Sahrens * Verify that the dataset contains a directory object. 3203fa9e4066Sahrens */ 32043b2aab18SMatthew Ahrens VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_TRUE, FTAG, &os)); 3205b24ab676SJeff Bonwick error = dmu_object_info(os, ZTEST_DIROBJ, &doi); 3206e1930233Sbonwick if (error != ENOENT) { 3207e1930233Sbonwick /* We could have crashed in the middle of destroying it */ 3208fb09f5aaSMadhav Suresh ASSERT0(error); 3209b24ab676SJeff Bonwick ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER); 3210b24ab676SJeff Bonwick ASSERT3S(doi.doi_physical_blocks_512, >=, 0); 3211e1930233Sbonwick } 32123b2aab18SMatthew Ahrens dmu_objset_disown(os, FTAG); 3213fa9e4066Sahrens 3214fa9e4066Sahrens /* 3215fa9e4066Sahrens * Destroy the dataset. 3216fa9e4066Sahrens */ 32173b2aab18SMatthew Ahrens if (strchr(name, '@') != NULL) { 32183b2aab18SMatthew Ahrens VERIFY0(dsl_destroy_snapshot(name, B_FALSE)); 32193b2aab18SMatthew Ahrens } else { 32203b2aab18SMatthew Ahrens VERIFY0(dsl_destroy_head(name)); 32213b2aab18SMatthew Ahrens } 32221d452cf5Sahrens return (0); 3223fa9e4066Sahrens } 3224fa9e4066Sahrens 3225b24ab676SJeff Bonwick static boolean_t 3226b24ab676SJeff Bonwick ztest_snapshot_create(char *osname, uint64_t id) 3227fa9e4066Sahrens { 3228*40a5c998SMatthew Ahrens char snapname[ZFS_MAX_DATASET_NAME_LEN]; 3229b24ab676SJeff Bonwick int error; 3230fa9e4066Sahrens 32313b2aab18SMatthew Ahrens (void) snprintf(snapname, sizeof (snapname), "%llu", (u_longlong_t)id); 3232fa9e4066Sahrens 32333b2aab18SMatthew Ahrens error = dmu_objset_snapshot_one(osname, snapname); 3234b24ab676SJeff Bonwick if (error == ENOSPC) { 3235b24ab676SJeff Bonwick ztest_record_enospc(FTAG); 3236b24ab676SJeff Bonwick return (B_FALSE); 3237b24ab676SJeff Bonwick } 32383b2aab18SMatthew Ahrens if (error != 0 && error != EEXIST) { 32393b2aab18SMatthew Ahrens fatal(0, "ztest_snapshot_create(%s@%s) = %d", osname, 32403b2aab18SMatthew Ahrens snapname, error); 32413b2aab18SMatthew Ahrens } 3242b24ab676SJeff Bonwick return (B_TRUE); 3243fa9e4066Sahrens } 3244fa9e4066Sahrens 3245b24ab676SJeff Bonwick static boolean_t 3246b24ab676SJeff Bonwick ztest_snapshot_destroy(char *osname, uint64_t id) 3247fa9e4066Sahrens { 3248*40a5c998SMatthew Ahrens char snapname[ZFS_MAX_DATASET_NAME_LEN]; 3249b24ab676SJeff Bonwick int error; 3250b24ab676SJeff Bonwick 3251*40a5c998SMatthew Ahrens (void) snprintf(snapname, sizeof (snapname), "%s@%llu", osname, 3252b24ab676SJeff Bonwick (u_longlong_t)id); 3253b24ab676SJeff Bonwick 32543b2aab18SMatthew Ahrens error = dsl_destroy_snapshot(snapname, B_FALSE); 3255b24ab676SJeff Bonwick if (error != 0 && error != ENOENT) 3256b24ab676SJeff Bonwick fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error); 3257b24ab676SJeff Bonwick return (B_TRUE); 3258b24ab676SJeff Bonwick } 3259b24ab676SJeff Bonwick 3260b24ab676SJeff Bonwick /* ARGSUSED */ 3261b24ab676SJeff Bonwick void 3262b24ab676SJeff Bonwick ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id) 3263b24ab676SJeff Bonwick { 3264b24ab676SJeff Bonwick ztest_ds_t zdtmp; 3265b24ab676SJeff Bonwick int iters; 3266fa9e4066Sahrens int error; 3267745cd3c5Smaybee objset_t *os, *os2; 3268*40a5c998SMatthew Ahrens char name[ZFS_MAX_DATASET_NAME_LEN]; 3269fa9e4066Sahrens zilog_t *zilog; 3270fa9e4066Sahrens 3271420dfc95SChris Siden (void) rw_rdlock(&ztest_name_lock); 3272b24ab676SJeff Bonwick 3273*40a5c998SMatthew Ahrens (void) snprintf(name, sizeof (name), "%s/temp_%llu", 3274420dfc95SChris Siden ztest_opts.zo_pool, (u_longlong_t)id); 3275fa9e4066Sahrens 3276fa9e4066Sahrens /* 3277fa9e4066Sahrens * If this dataset exists from a previous run, process its replay log 3278fa9e4066Sahrens * half of the time. If we don't replay it, then dmu_objset_destroy() 3279b24ab676SJeff Bonwick * (invoked from ztest_objset_destroy_cb()) should just throw it away. 3280fa9e4066Sahrens */ 3281fa9e4066Sahrens if (ztest_random(2) == 0 && 3282503ad85cSMatthew Ahrens dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) { 3283420dfc95SChris Siden ztest_zd_init(&zdtmp, NULL, os); 3284b24ab676SJeff Bonwick zil_replay(os, &zdtmp, ztest_replay_vector); 3285b24ab676SJeff Bonwick ztest_zd_fini(&zdtmp); 3286503ad85cSMatthew Ahrens dmu_objset_disown(os, FTAG); 3287fa9e4066Sahrens } 3288fa9e4066Sahrens 3289fa9e4066Sahrens /* 3290fa9e4066Sahrens * There may be an old instance of the dataset we're about to 3291fa9e4066Sahrens * create lying around from a previous run. If so, destroy it 3292fa9e4066Sahrens * and all of its snapshots. 3293fa9e4066Sahrens */ 3294b24ab676SJeff Bonwick (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL, 32950b69c2f0Sahrens DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS); 3296fa9e4066Sahrens 3297fa9e4066Sahrens /* 3298fa9e4066Sahrens * Verify that the destroyed dataset is no longer in the namespace. 3299fa9e4066Sahrens */ 33003b2aab18SMatthew Ahrens VERIFY3U(ENOENT, ==, dmu_objset_own(name, DMU_OST_OTHER, B_TRUE, 33013b2aab18SMatthew Ahrens FTAG, &os)); 3302fa9e4066Sahrens 3303fa9e4066Sahrens /* 3304fa9e4066Sahrens * Verify that we can create a new dataset. 3305fa9e4066Sahrens */ 330655da60b9SMark J Musante error = ztest_dataset_create(name); 3307fa9e4066Sahrens if (error) { 3308fa9e4066Sahrens if (error == ENOSPC) { 3309b24ab676SJeff Bonwick ztest_record_enospc(FTAG); 3310420dfc95SChris Siden (void) rw_unlock(&ztest_name_lock); 3311fa9e4066Sahrens return; 3312fa9e4066Sahrens } 3313fa9e4066Sahrens fatal(0, "dmu_objset_create(%s) = %d", name, error); 3314fa9e4066Sahrens } 3315fa9e4066Sahrens 33163b2aab18SMatthew Ahrens VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os)); 3317b24ab676SJeff Bonwick 3318420dfc95SChris Siden ztest_zd_init(&zdtmp, NULL, os); 3319fa9e4066Sahrens 3320fa9e4066Sahrens /* 3321fa9e4066Sahrens * Open the intent log for it. 3322fa9e4066Sahrens */ 3323b24ab676SJeff Bonwick zilog = zil_open(os, ztest_get_data); 3324fa9e4066Sahrens 3325fa9e4066Sahrens /* 3326b24ab676SJeff Bonwick * Put some objects in there, do a little I/O to them, 3327b24ab676SJeff Bonwick * and randomly take a couple of snapshots along the way. 3328fa9e4066Sahrens */ 3329b24ab676SJeff Bonwick iters = ztest_random(5); 3330b24ab676SJeff Bonwick for (int i = 0; i < iters; i++) { 3331b24ab676SJeff Bonwick ztest_dmu_object_alloc_free(&zdtmp, id); 3332b24ab676SJeff Bonwick if (ztest_random(iters) == 0) 3333b24ab676SJeff Bonwick (void) ztest_snapshot_create(name, i); 3334fa9e4066Sahrens } 3335fa9e4066Sahrens 3336fa9e4066Sahrens /* 3337fa9e4066Sahrens * Verify that we cannot create an existing dataset. 3338fa9e4066Sahrens */ 3339b24ab676SJeff Bonwick VERIFY3U(EEXIST, ==, 3340b24ab676SJeff Bonwick dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL)); 3341fa9e4066Sahrens 3342fa9e4066Sahrens /* 3343503ad85cSMatthew Ahrens * Verify that we can hold an objset that is also owned. 3344fa9e4066Sahrens */ 3345b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2)); 3346503ad85cSMatthew Ahrens dmu_objset_rele(os2, FTAG); 3347503ad85cSMatthew Ahrens 3348503ad85cSMatthew Ahrens /* 3349503ad85cSMatthew Ahrens * Verify that we cannot own an objset that is already owned. 3350503ad85cSMatthew Ahrens */ 3351b24ab676SJeff Bonwick VERIFY3U(EBUSY, ==, 3352b24ab676SJeff Bonwick dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2)); 3353fa9e4066Sahrens 3354fa9e4066Sahrens zil_close(zilog); 3355503ad85cSMatthew Ahrens dmu_objset_disown(os, FTAG); 3356b24ab676SJeff Bonwick ztest_zd_fini(&zdtmp); 3357fa9e4066Sahrens 3358420dfc95SChris Siden (void) rw_unlock(&ztest_name_lock); 3359fa9e4066Sahrens } 3360fa9e4066Sahrens 3361fa9e4066Sahrens /* 3362fa9e4066Sahrens * Verify that dmu_snapshot_{create,destroy,open,close} work as expected. 3363fa9e4066Sahrens */ 3364fa9e4066Sahrens void 3365b24ab676SJeff Bonwick ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id) 3366fa9e4066Sahrens { 3367420dfc95SChris Siden (void) rw_rdlock(&ztest_name_lock); 3368b24ab676SJeff Bonwick (void) ztest_snapshot_destroy(zd->zd_name, id); 3369b24ab676SJeff Bonwick (void) ztest_snapshot_create(zd->zd_name, id); 3370420dfc95SChris Siden (void) rw_unlock(&ztest_name_lock); 3371fa9e4066Sahrens } 3372fa9e4066Sahrens 3373fa9e4066Sahrens /* 3374718d718aSGeorge Wilson * Cleanup non-standard snapshots and clones. 3375718d718aSGeorge Wilson */ 3376718d718aSGeorge Wilson void 3377b24ab676SJeff Bonwick ztest_dsl_dataset_cleanup(char *osname, uint64_t id) 3378718d718aSGeorge Wilson { 3379*40a5c998SMatthew Ahrens char snap1name[ZFS_MAX_DATASET_NAME_LEN]; 3380*40a5c998SMatthew Ahrens char clone1name[ZFS_MAX_DATASET_NAME_LEN]; 3381*40a5c998SMatthew Ahrens char snap2name[ZFS_MAX_DATASET_NAME_LEN]; 3382*40a5c998SMatthew Ahrens char clone2name[ZFS_MAX_DATASET_NAME_LEN]; 3383*40a5c998SMatthew Ahrens char snap3name[ZFS_MAX_DATASET_NAME_LEN]; 3384718d718aSGeorge Wilson int error; 3385718d718aSGeorge Wilson 3386*40a5c998SMatthew Ahrens (void) snprintf(snap1name, sizeof (snap1name), 3387*40a5c998SMatthew Ahrens "%s@s1_%llu", osname, id); 3388*40a5c998SMatthew Ahrens (void) snprintf(clone1name, sizeof (clone1name), 3389*40a5c998SMatthew Ahrens "%s/c1_%llu", osname, id); 3390*40a5c998SMatthew Ahrens (void) snprintf(snap2name, sizeof (snap2name), 3391*40a5c998SMatthew Ahrens "%s@s2_%llu", clone1name, id); 3392*40a5c998SMatthew Ahrens (void) snprintf(clone2name, sizeof (clone2name), 3393*40a5c998SMatthew Ahrens "%s/c2_%llu", osname, id); 3394*40a5c998SMatthew Ahrens (void) snprintf(snap3name, sizeof (snap3name), 3395*40a5c998SMatthew Ahrens "%s@s3_%llu", clone1name, id); 3396718d718aSGeorge Wilson 33973b2aab18SMatthew Ahrens error = dsl_destroy_head(clone2name); 3398718d718aSGeorge Wilson if (error && error != ENOENT) 33993b2aab18SMatthew Ahrens fatal(0, "dsl_destroy_head(%s) = %d", clone2name, error); 34003b2aab18SMatthew Ahrens error = dsl_destroy_snapshot(snap3name, B_FALSE); 3401718d718aSGeorge Wilson if (error && error != ENOENT) 34023b2aab18SMatthew Ahrens fatal(0, "dsl_destroy_snapshot(%s) = %d", snap3name, error); 34033b2aab18SMatthew Ahrens error = dsl_destroy_snapshot(snap2name, B_FALSE); 3404718d718aSGeorge Wilson if (error && error != ENOENT) 34053b2aab18SMatthew Ahrens fatal(0, "dsl_destroy_snapshot(%s) = %d", snap2name, error); 34063b2aab18SMatthew Ahrens error = dsl_destroy_head(clone1name); 3407718d718aSGeorge Wilson if (error && error != ENOENT) 34083b2aab18SMatthew Ahrens fatal(0, "dsl_destroy_head(%s) = %d", clone1name, error); 34093b2aab18SMatthew Ahrens error = dsl_destroy_snapshot(snap1name, B_FALSE); 3410718d718aSGeorge Wilson if (error && error != ENOENT) 34113b2aab18SMatthew Ahrens fatal(0, "dsl_destroy_snapshot(%s) = %d", snap1name, error); 3412718d718aSGeorge Wilson } 3413718d718aSGeorge Wilson 3414718d718aSGeorge Wilson /* 34154f5064b7SMark J Musante * Verify dsl_dataset_promote handles EBUSY 34164f5064b7SMark J Musante */ 34174f5064b7SMark J Musante void 3418b24ab676SJeff Bonwick ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id) 34194f5064b7SMark J Musante { 34203b2aab18SMatthew Ahrens objset_t *os; 3421*40a5c998SMatthew Ahrens char snap1name[ZFS_MAX_DATASET_NAME_LEN]; 3422*40a5c998SMatthew Ahrens char clone1name[ZFS_MAX_DATASET_NAME_LEN]; 3423*40a5c998SMatthew Ahrens char snap2name[ZFS_MAX_DATASET_NAME_LEN]; 3424*40a5c998SMatthew Ahrens char clone2name[ZFS_MAX_DATASET_NAME_LEN]; 3425*40a5c998SMatthew Ahrens char snap3name[ZFS_MAX_DATASET_NAME_LEN]; 3426b24ab676SJeff Bonwick char *osname = zd->zd_name; 3427b24ab676SJeff Bonwick int error; 34284f5064b7SMark J Musante 3429420dfc95SChris Siden (void) rw_rdlock(&ztest_name_lock); 34304f5064b7SMark J Musante 3431b24ab676SJeff Bonwick ztest_dsl_dataset_cleanup(osname, id); 3432718d718aSGeorge Wilson 3433*40a5c998SMatthew Ahrens (void) snprintf(snap1name, sizeof (snap1name), 3434*40a5c998SMatthew Ahrens "%s@s1_%llu", osname, id); 3435*40a5c998SMatthew Ahrens (void) snprintf(clone1name, sizeof (clone1name), 3436*40a5c998SMatthew Ahrens "%s/c1_%llu", osname, id); 3437*40a5c998SMatthew Ahrens (void) snprintf(snap2name, sizeof (snap2name), 3438*40a5c998SMatthew Ahrens "%s@s2_%llu", clone1name, id); 3439*40a5c998SMatthew Ahrens (void) snprintf(clone2name, sizeof (clone2name), 3440*40a5c998SMatthew Ahrens "%s/c2_%llu", osname, id); 3441*40a5c998SMatthew Ahrens (void) snprintf(snap3name, sizeof (snap3name), 3442*40a5c998SMatthew Ahrens "%s@s3_%llu", clone1name, id); 344305312e2cSMark J Musante 34444445fffbSMatthew Ahrens error = dmu_objset_snapshot_one(osname, strchr(snap1name, '@') + 1); 3445b2d22b66SGeorge Wilson if (error && error != EEXIST) { 3446b2d22b66SGeorge Wilson if (error == ENOSPC) { 3447b24ab676SJeff Bonwick ztest_record_enospc(FTAG); 3448b2d22b66SGeorge Wilson goto out; 3449b2d22b66SGeorge Wilson } 3450b2d22b66SGeorge Wilson fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error); 3451b2d22b66SGeorge Wilson } 34524f5064b7SMark J Musante 34533b2aab18SMatthew Ahrens error = dmu_objset_clone(clone1name, snap1name); 3454b2d22b66SGeorge Wilson if (error) { 3455b2d22b66SGeorge Wilson if (error == ENOSPC) { 3456b24ab676SJeff Bonwick ztest_record_enospc(FTAG); 3457b2d22b66SGeorge Wilson goto out; 3458b2d22b66SGeorge Wilson } 3459b2d22b66SGeorge Wilson fatal(0, "dmu_objset_create(%s) = %d", clone1name, error); 3460b2d22b66SGeorge Wilson } 34614f5064b7SMark J Musante 34624445fffbSMatthew Ahrens error = dmu_objset_snapshot_one(clone1name, strchr(snap2name, '@') + 1); 3463b2d22b66SGeorge Wilson if (error && error != EEXIST) { 3464b2d22b66SGeorge Wilson if (error == ENOSPC) { 3465b24ab676SJeff Bonwick ztest_record_enospc(FTAG); 3466b2d22b66SGeorge Wilson goto out; 3467b2d22b66SGeorge Wilson } 3468b2d22b66SGeorge Wilson fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error); 3469b2d22b66SGeorge Wilson } 34704f5064b7SMark J Musante 34714445fffbSMatthew Ahrens error = dmu_objset_snapshot_one(clone1name, strchr(snap3name, '@') + 1); 3472b2d22b66SGeorge Wilson if (error && error != EEXIST) { 3473b2d22b66SGeorge Wilson if (error == ENOSPC) { 3474b24ab676SJeff Bonwick ztest_record_enospc(FTAG); 3475b2d22b66SGeorge Wilson goto out; 3476b2d22b66SGeorge Wilson } 3477b2d22b66SGeorge Wilson fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error); 3478b2d22b66SGeorge Wilson } 34794f5064b7SMark J Musante 34803b2aab18SMatthew Ahrens error = dmu_objset_clone(clone2name, snap3name); 3481b2d22b66SGeorge Wilson if (error) { 3482b2d22b66SGeorge Wilson if (error == ENOSPC) { 3483b24ab676SJeff Bonwick ztest_record_enospc(FTAG); 3484b2d22b66SGeorge Wilson goto out; 3485b2d22b66SGeorge Wilson } 3486b2d22b66SGeorge Wilson fatal(0, "dmu_objset_create(%s) = %d", clone2name, error); 3487b2d22b66SGeorge Wilson } 34884f5064b7SMark J Musante 34893b2aab18SMatthew Ahrens error = dmu_objset_own(snap2name, DMU_OST_ANY, B_TRUE, FTAG, &os); 34904f5064b7SMark J Musante if (error) 34913b2aab18SMatthew Ahrens fatal(0, "dmu_objset_own(%s) = %d", snap2name, error); 3492681d9761SEric Taylor error = dsl_dataset_promote(clone2name, NULL); 34935d7b4d43SMatthew Ahrens if (error == ENOSPC) { 34945d7b4d43SMatthew Ahrens dmu_objset_disown(os, FTAG); 34955d7b4d43SMatthew Ahrens ztest_record_enospc(FTAG); 34965d7b4d43SMatthew Ahrens goto out; 34975d7b4d43SMatthew Ahrens } 34984f5064b7SMark J Musante if (error != EBUSY) 34994f5064b7SMark J Musante fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name, 35004f5064b7SMark J Musante error); 35013b2aab18SMatthew Ahrens dmu_objset_disown(os, FTAG); 35024f5064b7SMark J Musante 3503b2d22b66SGeorge Wilson out: 3504b24ab676SJeff Bonwick ztest_dsl_dataset_cleanup(osname, id); 35054f5064b7SMark J Musante 3506420dfc95SChris Siden (void) rw_unlock(&ztest_name_lock); 35074f5064b7SMark J Musante } 35084f5064b7SMark J Musante 35094f5064b7SMark J Musante /* 3510fa9e4066Sahrens * Verify that dmu_object_{alloc,free} work as expected. 3511fa9e4066Sahrens */ 3512fa9e4066Sahrens void 3513b24ab676SJeff Bonwick ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id) 3514fa9e4066Sahrens { 3515b24ab676SJeff Bonwick ztest_od_t od[4]; 3516b24ab676SJeff Bonwick int batchsize = sizeof (od) / sizeof (od[0]); 3517fa9e4066Sahrens 3518b24ab676SJeff Bonwick for (int b = 0; b < batchsize; b++) 3519b24ab676SJeff Bonwick ztest_od_init(&od[b], id, FTAG, b, DMU_OT_UINT64_OTHER, 0, 0); 3520fa9e4066Sahrens 3521fa9e4066Sahrens /* 3522b24ab676SJeff Bonwick * Destroy the previous batch of objects, create a new batch, 3523b24ab676SJeff Bonwick * and do some I/O on the new objects. 3524fa9e4066Sahrens */ 3525b24ab676SJeff Bonwick if (ztest_object_init(zd, od, sizeof (od), B_TRUE) != 0) 3526fa9e4066Sahrens return; 3527fa9e4066Sahrens 3528b24ab676SJeff Bonwick while (ztest_random(4 * batchsize) != 0) 3529b24ab676SJeff Bonwick ztest_io(zd, od[ztest_random(batchsize)].od_object, 3530b24ab676SJeff Bonwick ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT); 3531fa9e4066Sahrens } 3532fa9e4066Sahrens 3533fa9e4066Sahrens /* 3534fa9e4066Sahrens * Verify that dmu_{read,write} work as expected. 3535fa9e4066Sahrens */ 3536fa9e4066Sahrens void 3537b24ab676SJeff Bonwick ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id) 3538fa9e4066Sahrens { 3539b24ab676SJeff Bonwick objset_t *os = zd->zd_os; 3540b24ab676SJeff Bonwick ztest_od_t od[2]; 3541fa9e4066Sahrens dmu_tx_t *tx; 3542fa9e4066Sahrens int i, freeit, error; 3543fa9e4066Sahrens uint64_t n, s, txg; 3544fa9e4066Sahrens bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT; 3545b24ab676SJeff Bonwick uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize; 3546b24ab676SJeff Bonwick uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t); 3547fa9e4066Sahrens uint64_t regions = 997; 3548fa9e4066Sahrens uint64_t stride = 123456789ULL; 3549fa9e4066Sahrens uint64_t width = 40; 3550fa9e4066Sahrens int free_percent = 5; 3551fa9e4066Sahrens 3552fa9e4066Sahrens /* 3553fa9e4066Sahrens * This test uses two objects, packobj and bigobj, that are always 3554fa9e4066Sahrens * updated together (i.e. in the same tx) so that their contents are 3555fa9e4066Sahrens * in sync and can be compared. Their contents relate to each other 3556fa9e4066Sahrens * in a simple way: packobj is a dense array of 'bufwad' structures, 3557fa9e4066Sahrens * while bigobj is a sparse array of the same bufwads. Specifically, 3558fa9e4066Sahrens * for any index n, there are three bufwads that should be identical: 3559fa9e4066Sahrens * 3560fa9e4066Sahrens * packobj, at offset n * sizeof (bufwad_t) 3561fa9e4066Sahrens * bigobj, at the head of the nth chunk 3562fa9e4066Sahrens * bigobj, at the tail of the nth chunk 3563fa9e4066Sahrens * 3564fa9e4066Sahrens * The chunk size is arbitrary. It doesn't have to be a power of two, 3565fa9e4066Sahrens * and it doesn't have any relation to the object blocksize. 3566fa9e4066Sahrens * The only requirement is that it can hold at least two bufwads. 3567fa9e4066Sahrens * 3568fa9e4066Sahrens * Normally, we write the bufwad to each of these locations. 3569fa9e4066Sahrens * However, free_percent of the time we instead write zeroes to 3570fa9e4066Sahrens * packobj and perform a dmu_free_range() on bigobj. By comparing 3571fa9e4066Sahrens * bigobj to packobj, we can verify that the DMU is correctly 3572fa9e4066Sahrens * tracking which parts of an object are allocated and free, 3573fa9e4066Sahrens * and that the contents of the allocated blocks are correct. 3574fa9e4066Sahrens */ 3575fa9e4066Sahrens 3576fa9e4066Sahrens /* 3577fa9e4066Sahrens * Read the directory info. If it's the first time, set things up. 3578fa9e4066Sahrens */ 3579b24ab676SJeff Bonwick ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, chunksize); 3580b24ab676SJeff Bonwick ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize); 3581b24ab676SJeff Bonwick 3582b24ab676SJeff Bonwick if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0) 3583fa9e4066Sahrens return; 3584fa9e4066Sahrens 3585b24ab676SJeff Bonwick bigobj = od[0].od_object; 3586b24ab676SJeff Bonwick packobj = od[1].od_object; 3587b24ab676SJeff Bonwick chunksize = od[0].od_gen; 3588b24ab676SJeff Bonwick ASSERT(chunksize == od[1].od_gen); 3589fa9e4066Sahrens 3590fa9e4066Sahrens /* 3591fa9e4066Sahrens * Prefetch a random chunk of the big object. 3592fa9e4066Sahrens * Our aim here is to get some async reads in flight 3593fa9e4066Sahrens * for blocks that we may free below; the DMU should 3594fa9e4066Sahrens * handle this race correctly. 3595fa9e4066Sahrens */ 3596fa9e4066Sahrens n = ztest_random(regions) * stride + ztest_random(width); 3597fa9e4066Sahrens s = 1 + ztest_random(2 * width - 1); 3598a2cdcdd2SPaul Dagnelie dmu_prefetch(os, bigobj, 0, n * chunksize, s * chunksize, 3599a2cdcdd2SPaul Dagnelie ZIO_PRIORITY_SYNC_READ); 3600fa9e4066Sahrens 3601fa9e4066Sahrens /* 3602fa9e4066Sahrens * Pick a random index and compute the offsets into packobj and bigobj. 3603fa9e4066Sahrens */ 3604fa9e4066Sahrens n = ztest_random(regions) * stride + ztest_random(width); 3605fa9e4066Sahrens s = 1 + ztest_random(width - 1); 3606fa9e4066Sahrens 3607fa9e4066Sahrens packoff = n * sizeof (bufwad_t); 3608fa9e4066Sahrens packsize = s * sizeof (bufwad_t); 3609fa9e4066Sahrens 3610b24ab676SJeff Bonwick bigoff = n * chunksize; 3611b24ab676SJeff Bonwick bigsize = s * chunksize; 3612fa9e4066Sahrens 3613fa9e4066Sahrens packbuf = umem_alloc(packsize, UMEM_NOFAIL); 3614fa9e4066Sahrens bigbuf = umem_alloc(bigsize, UMEM_NOFAIL); 3615fa9e4066Sahrens 3616fa9e4066Sahrens /* 3617fa9e4066Sahrens * free_percent of the time, free a range of bigobj rather than 3618fa9e4066Sahrens * overwriting it. 3619fa9e4066Sahrens */ 3620fa9e4066Sahrens freeit = (ztest_random(100) < free_percent); 3621fa9e4066Sahrens 3622fa9e4066Sahrens /* 3623fa9e4066Sahrens * Read the current contents of our objects. 3624fa9e4066Sahrens */ 3625b24ab676SJeff Bonwick error = dmu_read(os, packobj, packoff, packsize, packbuf, 36267bfdf011SNeil Perrin DMU_READ_PREFETCH); 3627fb09f5aaSMadhav Suresh ASSERT0(error); 3628b24ab676SJeff Bonwick error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf, 36297bfdf011SNeil Perrin DMU_READ_PREFETCH); 3630fb09f5aaSMadhav Suresh ASSERT0(error); 3631fa9e4066Sahrens 3632fa9e4066Sahrens /* 3633fa9e4066Sahrens * Get a tx for the mods to both packobj and bigobj. 3634fa9e4066Sahrens */ 3635fa9e4066Sahrens tx = dmu_tx_create(os); 3636fa9e4066Sahrens 3637b24ab676SJeff Bonwick dmu_tx_hold_write(tx, packobj, packoff, packsize); 3638fa9e4066Sahrens 3639fa9e4066Sahrens if (freeit) 3640b24ab676SJeff Bonwick dmu_tx_hold_free(tx, bigobj, bigoff, bigsize); 3641fa9e4066Sahrens else 3642b24ab676SJeff Bonwick dmu_tx_hold_write(tx, bigobj, bigoff, bigsize); 3643fa9e4066Sahrens 3644be9000ccSMatthew Ahrens /* This accounts for setting the checksum/compression. */ 3645be9000ccSMatthew Ahrens dmu_tx_hold_bonus(tx, bigobj); 3646be9000ccSMatthew Ahrens 3647b24ab676SJeff Bonwick txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG); 3648b24ab676SJeff Bonwick if (txg == 0) { 3649fa9e4066Sahrens umem_free(packbuf, packsize); 3650fa9e4066Sahrens umem_free(bigbuf, bigsize); 3651fa9e4066Sahrens return; 3652fa9e4066Sahrens } 3653fa9e4066Sahrens 36545d7b4d43SMatthew Ahrens enum zio_checksum cksum; 36555d7b4d43SMatthew Ahrens do { 36565d7b4d43SMatthew Ahrens cksum = (enum zio_checksum) 36575d7b4d43SMatthew Ahrens ztest_random_dsl_prop(ZFS_PROP_CHECKSUM); 36585d7b4d43SMatthew Ahrens } while (cksum >= ZIO_CHECKSUM_LEGACY_FUNCTIONS); 36595d7b4d43SMatthew Ahrens dmu_object_set_checksum(os, bigobj, cksum, tx); 3660b24ab676SJeff Bonwick 36615d7b4d43SMatthew Ahrens enum zio_compress comp; 36625d7b4d43SMatthew Ahrens do { 36635d7b4d43SMatthew Ahrens comp = (enum zio_compress) 36645d7b4d43SMatthew Ahrens ztest_random_dsl_prop(ZFS_PROP_COMPRESSION); 36655d7b4d43SMatthew Ahrens } while (comp >= ZIO_COMPRESS_LEGACY_FUNCTIONS); 36665d7b4d43SMatthew Ahrens dmu_object_set_compress(os, bigobj, comp, tx); 3667fa9e4066Sahrens 3668fa9e4066Sahrens /* 3669fa9e4066Sahrens * For each index from n to n + s, verify that the existing bufwad 3670fa9e4066Sahrens * in packobj matches the bufwads at the head and tail of the 3671fa9e4066Sahrens * corresponding chunk in bigobj. Then update all three bufwads 3672fa9e4066Sahrens * with the new values we want to write out. 3673fa9e4066Sahrens */ 3674fa9e4066Sahrens for (i = 0; i < s; i++) { 3675fa9e4066Sahrens /* LINTED */ 3676fa9e4066Sahrens pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t)); 3677fa9e4066Sahrens /* LINTED */ 3678b24ab676SJeff Bonwick bigH = (bufwad_t *)((char *)bigbuf + i * chunksize); 3679fa9e4066Sahrens /* LINTED */ 3680b24ab676SJeff Bonwick bigT = (bufwad_t *)((char *)bigH + chunksize) - 1; 3681fa9e4066Sahrens 3682fa9e4066Sahrens ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize); 3683fa9e4066Sahrens ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize); 3684fa9e4066Sahrens 3685fa9e4066Sahrens if (pack->bw_txg > txg) 3686fa9e4066Sahrens fatal(0, "future leak: got %llx, open txg is %llx", 3687fa9e4066Sahrens pack->bw_txg, txg); 3688fa9e4066Sahrens 3689fa9e4066Sahrens if (pack->bw_data != 0 && pack->bw_index != n + i) 3690fa9e4066Sahrens fatal(0, "wrong index: got %llx, wanted %llx+%llx", 3691fa9e4066Sahrens pack->bw_index, n, i); 3692fa9e4066Sahrens 3693fa9e4066Sahrens if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0) 3694fa9e4066Sahrens fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH); 3695fa9e4066Sahrens 3696fa9e4066Sahrens if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0) 3697fa9e4066Sahrens fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT); 3698fa9e4066Sahrens 3699fa9e4066Sahrens if (freeit) { 3700fa9e4066Sahrens bzero(pack, sizeof (bufwad_t)); 3701fa9e4066Sahrens } else { 3702fa9e4066Sahrens pack->bw_index = n + i; 3703fa9e4066Sahrens pack->bw_txg = txg; 3704fa9e4066Sahrens pack->bw_data = 1 + ztest_random(-2ULL); 3705fa9e4066Sahrens } 3706fa9e4066Sahrens *bigH = *pack; 3707fa9e4066Sahrens *bigT = *pack; 3708fa9e4066Sahrens } 3709fa9e4066Sahrens 3710fa9e4066Sahrens /* 3711fa9e4066Sahrens * We've verified all the old bufwads, and made new ones. 3712fa9e4066Sahrens * Now write them out. 3713fa9e4066Sahrens */ 3714b24ab676SJeff Bonwick dmu_write(os, packobj, packoff, packsize, packbuf, tx); 3715fa9e4066Sahrens 3716fa9e4066Sahrens if (freeit) { 3717420dfc95SChris Siden if (ztest_opts.zo_verbose >= 7) { 3718fa9e4066Sahrens (void) printf("freeing offset %llx size %llx" 3719fa9e4066Sahrens " txg %llx\n", 3720fa9e4066Sahrens (u_longlong_t)bigoff, 3721fa9e4066Sahrens (u_longlong_t)bigsize, 3722fa9e4066Sahrens (u_longlong_t)txg); 3723fa9e4066Sahrens } 3724b24ab676SJeff Bonwick VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx)); 3725fa9e4066Sahrens } else { 3726420dfc95SChris Siden if (ztest_opts.zo_verbose >= 7) { 3727fa9e4066Sahrens (void) printf("writing offset %llx size %llx" 3728fa9e4066Sahrens " txg %llx\n", 3729fa9e4066Sahrens (u_longlong_t)bigoff, 3730fa9e4066Sahrens (u_longlong_t)bigsize, 3731fa9e4066Sahrens (u_longlong_t)txg); 3732fa9e4066Sahrens } 3733b24ab676SJeff Bonwick dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx); 3734fa9e4066Sahrens } 3735fa9e4066Sahrens 3736fa9e4066Sahrens dmu_tx_commit(tx); 3737fa9e4066Sahrens 3738fa9e4066Sahrens /* 3739fa9e4066Sahrens * Sanity check the stuff we just wrote. 3740fa9e4066Sahrens */ 3741fa9e4066Sahrens { 3742fa9e4066Sahrens void *packcheck = umem_alloc(packsize, UMEM_NOFAIL); 3743fa9e4066Sahrens void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL); 3744fa9e4066Sahrens 3745b24ab676SJeff Bonwick VERIFY(0 == dmu_read(os, packobj, packoff, 37467bfdf011SNeil Perrin packsize, packcheck, DMU_READ_PREFETCH)); 3747b24ab676SJeff Bonwick VERIFY(0 == dmu_read(os, bigobj, bigoff, 37487bfdf011SNeil Perrin bigsize, bigcheck, DMU_READ_PREFETCH)); 3749fa9e4066Sahrens 3750fa9e4066Sahrens ASSERT(bcmp(packbuf, packcheck, packsize) == 0); 3751fa9e4066Sahrens ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0); 3752fa9e4066Sahrens 3753fa9e4066Sahrens umem_free(packcheck, packsize); 3754fa9e4066Sahrens umem_free(bigcheck, bigsize); 3755fa9e4066Sahrens } 3756fa9e4066Sahrens 3757fa9e4066Sahrens umem_free(packbuf, packsize); 3758fa9e4066Sahrens umem_free(bigbuf, bigsize); 3759fa9e4066Sahrens } 3760fa9e4066Sahrens 3761fa9e4066Sahrens void 37622fdbea25SAleksandr Guzovskiy compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf, 3763b24ab676SJeff Bonwick uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg) 37642fdbea25SAleksandr Guzovskiy { 37652fdbea25SAleksandr Guzovskiy uint64_t i; 37662fdbea25SAleksandr Guzovskiy bufwad_t *pack; 37672fdbea25SAleksandr Guzovskiy bufwad_t *bigH; 37682fdbea25SAleksandr Guzovskiy bufwad_t *bigT; 37692fdbea25SAleksandr Guzovskiy 37702fdbea25SAleksandr Guzovskiy /* 37712fdbea25SAleksandr Guzovskiy * For each index from n to n + s, verify that the existing bufwad 37722fdbea25SAleksandr Guzovskiy * in packobj matches the bufwads at the head and tail of the 37732fdbea25SAleksandr Guzovskiy * corresponding chunk in bigobj. Then update all three bufwads 37742fdbea25SAleksandr Guzovskiy * with the new values we want to write out. 37752fdbea25SAleksandr Guzovskiy */ 37762fdbea25SAleksandr Guzovskiy for (i = 0; i < s; i++) { 37772fdbea25SAleksandr Guzovskiy /* LINTED */ 37782fdbea25SAleksandr Guzovskiy pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t)); 37792fdbea25SAleksandr Guzovskiy /* LINTED */ 3780b24ab676SJeff Bonwick bigH = (bufwad_t *)((char *)bigbuf + i * chunksize); 37812fdbea25SAleksandr Guzovskiy /* LINTED */ 3782b24ab676SJeff Bonwick bigT = (bufwad_t *)((char *)bigH + chunksize) - 1; 37832fdbea25SAleksandr Guzovskiy 37842fdbea25SAleksandr Guzovskiy ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize); 37852fdbea25SAleksandr Guzovskiy ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize); 37862fdbea25SAleksandr Guzovskiy 37872fdbea25SAleksandr Guzovskiy if (pack->bw_txg > txg) 37882fdbea25SAleksandr Guzovskiy fatal(0, "future leak: got %llx, open txg is %llx", 37892fdbea25SAleksandr Guzovskiy pack->bw_txg, txg); 37902fdbea25SAleksandr Guzovskiy 37912fdbea25SAleksandr Guzovskiy if (pack->bw_data != 0 && pack->bw_index != n + i) 37922fdbea25SAleksandr Guzovskiy fatal(0, "wrong index: got %llx, wanted %llx+%llx", 37932fdbea25SAleksandr Guzovskiy pack->bw_index, n, i); 37942fdbea25SAleksandr Guzovskiy 37952fdbea25SAleksandr Guzovskiy if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0) 37962fdbea25SAleksandr Guzovskiy fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH); 37972fdbea25SAleksandr Guzovskiy 37982fdbea25SAleksandr Guzovskiy if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0) 37992fdbea25SAleksandr Guzovskiy fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT); 38002fdbea25SAleksandr Guzovskiy 38012fdbea25SAleksandr Guzovskiy pack->bw_index = n + i; 38022fdbea25SAleksandr Guzovskiy pack->bw_txg = txg; 38032fdbea25SAleksandr Guzovskiy pack->bw_data = 1 + ztest_random(-2ULL); 38042fdbea25SAleksandr Guzovskiy 38052fdbea25SAleksandr Guzovskiy *bigH = *pack; 38062fdbea25SAleksandr Guzovskiy *bigT = *pack; 38072fdbea25SAleksandr Guzovskiy } 38082fdbea25SAleksandr Guzovskiy } 38092fdbea25SAleksandr Guzovskiy 38102fdbea25SAleksandr Guzovskiy void 3811b24ab676SJeff Bonwick ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id) 38122fdbea25SAleksandr Guzovskiy { 3813b24ab676SJeff Bonwick objset_t *os = zd->zd_os; 3814b24ab676SJeff Bonwick ztest_od_t od[2]; 38152fdbea25SAleksandr Guzovskiy dmu_tx_t *tx; 38162fdbea25SAleksandr Guzovskiy uint64_t i; 38172fdbea25SAleksandr Guzovskiy int error; 38182fdbea25SAleksandr Guzovskiy uint64_t n, s, txg; 38192fdbea25SAleksandr Guzovskiy bufwad_t *packbuf, *bigbuf; 3820b24ab676SJeff Bonwick uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize; 3821b24ab676SJeff Bonwick uint64_t blocksize = ztest_random_blocksize(); 3822b24ab676SJeff Bonwick uint64_t chunksize = blocksize; 38232fdbea25SAleksandr Guzovskiy uint64_t regions = 997; 38242fdbea25SAleksandr Guzovskiy uint64_t stride = 123456789ULL; 38252fdbea25SAleksandr Guzovskiy uint64_t width = 9; 38262fdbea25SAleksandr Guzovskiy dmu_buf_t *bonus_db; 38272fdbea25SAleksandr Guzovskiy arc_buf_t **bigbuf_arcbufs; 3828b24ab676SJeff Bonwick dmu_object_info_t doi; 38292fdbea25SAleksandr Guzovskiy 38302fdbea25SAleksandr Guzovskiy /* 38312fdbea25SAleksandr Guzovskiy * This test uses two objects, packobj and bigobj, that are always 38322fdbea25SAleksandr Guzovskiy * updated together (i.e. in the same tx) so that their contents are 38332fdbea25SAleksandr Guzovskiy * in sync and can be compared. Their contents relate to each other 38342fdbea25SAleksandr Guzovskiy * in a simple way: packobj is a dense array of 'bufwad' structures, 38352fdbea25SAleksandr Guzovskiy * while bigobj is a sparse array of the same bufwads. Specifically, 38362fdbea25SAleksandr Guzovskiy * for any index n, there are three bufwads that should be identical: 38372fdbea25SAleksandr Guzovskiy * 38382fdbea25SAleksandr Guzovskiy * packobj, at offset n * sizeof (bufwad_t) 38392fdbea25SAleksandr Guzovskiy * bigobj, at the head of the nth chunk 38402fdbea25SAleksandr Guzovskiy * bigobj, at the tail of the nth chunk 38412fdbea25SAleksandr Guzovskiy * 38422fdbea25SAleksandr Guzovskiy * The chunk size is set equal to bigobj block size so that 38432fdbea25SAleksandr Guzovskiy * dmu_assign_arcbuf() can be tested for object updates. 38442fdbea25SAleksandr Guzovskiy */ 38452fdbea25SAleksandr Guzovskiy 38462fdbea25SAleksandr Guzovskiy /* 38472fdbea25SAleksandr Guzovskiy * Read the directory info. If it's the first time, set things up. 38482fdbea25SAleksandr Guzovskiy */ 3849b24ab676SJeff Bonwick ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0); 3850b24ab676SJeff Bonwick ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize); 3851b24ab676SJeff Bonwick 3852b24ab676SJeff Bonwick if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0) 38532fdbea25SAleksandr Guzovskiy return; 38542fdbea25SAleksandr Guzovskiy 3855b24ab676SJeff Bonwick bigobj = od[0].od_object; 3856b24ab676SJeff Bonwick packobj = od[1].od_object; 3857b24ab676SJeff Bonwick blocksize = od[0].od_blocksize; 3858b24ab676SJeff Bonwick chunksize = blocksize; 3859b24ab676SJeff Bonwick ASSERT(chunksize == od[1].od_gen); 38602fdbea25SAleksandr Guzovskiy 3861b24ab676SJeff Bonwick VERIFY(dmu_object_info(os, bigobj, &doi) == 0); 3862b24ab676SJeff Bonwick VERIFY(ISP2(doi.doi_data_block_size)); 3863b24ab676SJeff Bonwick VERIFY(chunksize == doi.doi_data_block_size); 3864b24ab676SJeff Bonwick VERIFY(chunksize >= 2 * sizeof (bufwad_t)); 38652fdbea25SAleksandr Guzovskiy 38662fdbea25SAleksandr Guzovskiy /* 38672fdbea25SAleksandr Guzovskiy * Pick a random index and compute the offsets into packobj and bigobj. 38682fdbea25SAleksandr Guzovskiy */ 38692fdbea25SAleksandr Guzovskiy n = ztest_random(regions) * stride + ztest_random(width); 38702fdbea25SAleksandr Guzovskiy s = 1 + ztest_random(width - 1); 38712fdbea25SAleksandr Guzovskiy 38722fdbea25SAleksandr Guzovskiy packoff = n * sizeof (bufwad_t); 38732fdbea25SAleksandr Guzovskiy packsize = s * sizeof (bufwad_t); 38742fdbea25SAleksandr Guzovskiy 3875b24ab676SJeff Bonwick bigoff = n * chunksize; 3876b24ab676SJeff Bonwick bigsize = s * chunksize; 38772fdbea25SAleksandr Guzovskiy 38782fdbea25SAleksandr Guzovskiy packbuf = umem_zalloc(packsize, UMEM_NOFAIL); 38792fdbea25SAleksandr Guzovskiy bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL); 38802fdbea25SAleksandr Guzovskiy 3881b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db)); 38822fdbea25SAleksandr Guzovskiy 38832fdbea25SAleksandr Guzovskiy bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL); 38842fdbea25SAleksandr Guzovskiy 38852fdbea25SAleksandr Guzovskiy /* 38862fdbea25SAleksandr Guzovskiy * Iteration 0 test zcopy for DB_UNCACHED dbufs. 38872fdbea25SAleksandr Guzovskiy * Iteration 1 test zcopy to already referenced dbufs. 38882fdbea25SAleksandr Guzovskiy * Iteration 2 test zcopy to dirty dbuf in the same txg. 38892fdbea25SAleksandr Guzovskiy * Iteration 3 test zcopy to dbuf dirty in previous txg. 38902fdbea25SAleksandr Guzovskiy * Iteration 4 test zcopy when dbuf is no longer dirty. 38912fdbea25SAleksandr Guzovskiy * Iteration 5 test zcopy when it can't be done. 38922fdbea25SAleksandr Guzovskiy * Iteration 6 one more zcopy write. 38932fdbea25SAleksandr Guzovskiy */ 38942fdbea25SAleksandr Guzovskiy for (i = 0; i < 7; i++) { 38952fdbea25SAleksandr Guzovskiy uint64_t j; 38962fdbea25SAleksandr Guzovskiy uint64_t off; 38972fdbea25SAleksandr Guzovskiy 38982fdbea25SAleksandr Guzovskiy /* 38992fdbea25SAleksandr Guzovskiy * In iteration 5 (i == 5) use arcbufs 39002fdbea25SAleksandr Guzovskiy * that don't match bigobj blksz to test 39012fdbea25SAleksandr Guzovskiy * dmu_assign_arcbuf() when it can't directly 39022fdbea25SAleksandr Guzovskiy * assign an arcbuf to a dbuf. 39032fdbea25SAleksandr Guzovskiy */ 39042fdbea25SAleksandr Guzovskiy for (j = 0; j < s; j++) { 390589c86e32SChris Williamson if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) { 39062fdbea25SAleksandr Guzovskiy bigbuf_arcbufs[j] = 3907b24ab676SJeff Bonwick dmu_request_arcbuf(bonus_db, chunksize); 39082fdbea25SAleksandr Guzovskiy } else { 39092fdbea25SAleksandr Guzovskiy bigbuf_arcbufs[2 * j] = 3910b24ab676SJeff Bonwick dmu_request_arcbuf(bonus_db, chunksize / 2); 39112fdbea25SAleksandr Guzovskiy bigbuf_arcbufs[2 * j + 1] = 3912b24ab676SJeff Bonwick dmu_request_arcbuf(bonus_db, chunksize / 2); 39132fdbea25SAleksandr Guzovskiy } 39142fdbea25SAleksandr Guzovskiy } 39152fdbea25SAleksandr Guzovskiy 39162fdbea25SAleksandr Guzovskiy /* 39172fdbea25SAleksandr Guzovskiy * Get a tx for the mods to both packobj and bigobj. 39182fdbea25SAleksandr Guzovskiy */ 39192fdbea25SAleksandr Guzovskiy tx = dmu_tx_create(os); 39202fdbea25SAleksandr Guzovskiy 3921b24ab676SJeff Bonwick dmu_tx_hold_write(tx, packobj, packoff, packsize); 3922b24ab676SJeff Bonwick dmu_tx_hold_write(tx, bigobj, bigoff, bigsize); 39232fdbea25SAleksandr Guzovskiy 3924b24ab676SJeff Bonwick txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG); 3925b24ab676SJeff Bonwick if (txg == 0) { 39262fdbea25SAleksandr Guzovskiy umem_free(packbuf, packsize); 39272fdbea25SAleksandr Guzovskiy umem_free(bigbuf, bigsize); 39282fdbea25SAleksandr Guzovskiy for (j = 0; j < s; j++) { 392989c86e32SChris Williamson if (i != 5 || 393089c86e32SChris Williamson chunksize < (SPA_MINBLOCKSIZE * 2)) { 39312fdbea25SAleksandr Guzovskiy dmu_return_arcbuf(bigbuf_arcbufs[j]); 39322fdbea25SAleksandr Guzovskiy } else { 39332fdbea25SAleksandr Guzovskiy dmu_return_arcbuf( 39342fdbea25SAleksandr Guzovskiy bigbuf_arcbufs[2 * j]); 39352fdbea25SAleksandr Guzovskiy dmu_return_arcbuf( 39362fdbea25SAleksandr Guzovskiy bigbuf_arcbufs[2 * j + 1]); 39372fdbea25SAleksandr Guzovskiy } 39382fdbea25SAleksandr Guzovskiy } 39392fdbea25SAleksandr Guzovskiy umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *)); 39402fdbea25SAleksandr Guzovskiy dmu_buf_rele(bonus_db, FTAG); 39412fdbea25SAleksandr Guzovskiy return; 39422fdbea25SAleksandr Guzovskiy } 39432fdbea25SAleksandr Guzovskiy 39442fdbea25SAleksandr Guzovskiy /* 39452fdbea25SAleksandr Guzovskiy * 50% of the time don't read objects in the 1st iteration to 39462fdbea25SAleksandr Guzovskiy * test dmu_assign_arcbuf() for the case when there're no 39472fdbea25SAleksandr Guzovskiy * existing dbufs for the specified offsets. 39482fdbea25SAleksandr Guzovskiy */ 39492fdbea25SAleksandr Guzovskiy if (i != 0 || ztest_random(2) != 0) { 3950b24ab676SJeff Bonwick error = dmu_read(os, packobj, packoff, 39517bfdf011SNeil Perrin packsize, packbuf, DMU_READ_PREFETCH); 3952fb09f5aaSMadhav Suresh ASSERT0(error); 3953b24ab676SJeff Bonwick error = dmu_read(os, bigobj, bigoff, bigsize, 39547bfdf011SNeil Perrin bigbuf, DMU_READ_PREFETCH); 3955fb09f5aaSMadhav Suresh ASSERT0(error); 39562fdbea25SAleksandr Guzovskiy } 39572fdbea25SAleksandr Guzovskiy compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize, 3958b24ab676SJeff Bonwick n, chunksize, txg); 39592fdbea25SAleksandr Guzovskiy 39602fdbea25SAleksandr Guzovskiy /* 39612fdbea25SAleksandr Guzovskiy * We've verified all the old bufwads, and made new ones. 39622fdbea25SAleksandr Guzovskiy * Now write them out. 39632fdbea25SAleksandr Guzovskiy */ 3964b24ab676SJeff Bonwick dmu_write(os, packobj, packoff, packsize, packbuf, tx); 3965420dfc95SChris Siden if (ztest_opts.zo_verbose >= 7) { 39662fdbea25SAleksandr Guzovskiy (void) printf("writing offset %llx size %llx" 39672fdbea25SAleksandr Guzovskiy " txg %llx\n", 39682fdbea25SAleksandr Guzovskiy (u_longlong_t)bigoff, 39692fdbea25SAleksandr Guzovskiy (u_longlong_t)bigsize, 39702fdbea25SAleksandr Guzovskiy (u_longlong_t)txg); 39712fdbea25SAleksandr Guzovskiy } 3972b24ab676SJeff Bonwick for (off = bigoff, j = 0; j < s; j++, off += chunksize) { 39732fdbea25SAleksandr Guzovskiy dmu_buf_t *dbt; 397489c86e32SChris Williamson if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) { 39752fdbea25SAleksandr Guzovskiy bcopy((caddr_t)bigbuf + (off - bigoff), 3976b24ab676SJeff Bonwick bigbuf_arcbufs[j]->b_data, chunksize); 39772fdbea25SAleksandr Guzovskiy } else { 39782fdbea25SAleksandr Guzovskiy bcopy((caddr_t)bigbuf + (off - bigoff), 39792fdbea25SAleksandr Guzovskiy bigbuf_arcbufs[2 * j]->b_data, 3980b24ab676SJeff Bonwick chunksize / 2); 39812fdbea25SAleksandr Guzovskiy bcopy((caddr_t)bigbuf + (off - bigoff) + 3982b24ab676SJeff Bonwick chunksize / 2, 39832fdbea25SAleksandr Guzovskiy bigbuf_arcbufs[2 * j + 1]->b_data, 3984b24ab676SJeff Bonwick chunksize / 2); 39852fdbea25SAleksandr Guzovskiy } 39862fdbea25SAleksandr Guzovskiy 39872fdbea25SAleksandr Guzovskiy if (i == 1) { 3988b24ab676SJeff Bonwick VERIFY(dmu_buf_hold(os, bigobj, off, 398947cb52daSJeff Bonwick FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0); 39902fdbea25SAleksandr Guzovskiy } 399189c86e32SChris Williamson if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) { 39922fdbea25SAleksandr Guzovskiy dmu_assign_arcbuf(bonus_db, off, 39932fdbea25SAleksandr Guzovskiy bigbuf_arcbufs[j], tx); 39942fdbea25SAleksandr Guzovskiy } else { 39952fdbea25SAleksandr Guzovskiy dmu_assign_arcbuf(bonus_db, off, 39962fdbea25SAleksandr Guzovskiy bigbuf_arcbufs[2 * j], tx); 39972fdbea25SAleksandr Guzovskiy dmu_assign_arcbuf(bonus_db, 3998b24ab676SJeff Bonwick off + chunksize / 2, 39992fdbea25SAleksandr Guzovskiy bigbuf_arcbufs[2 * j + 1], tx); 40002fdbea25SAleksandr Guzovskiy } 40012fdbea25SAleksandr Guzovskiy if (i == 1) { 40022fdbea25SAleksandr Guzovskiy dmu_buf_rele(dbt, FTAG); 40032fdbea25SAleksandr Guzovskiy } 40042fdbea25SAleksandr Guzovskiy } 40052fdbea25SAleksandr Guzovskiy dmu_tx_commit(tx); 40062fdbea25SAleksandr Guzovskiy 40072fdbea25SAleksandr Guzovskiy /* 40082fdbea25SAleksandr Guzovskiy * Sanity check the stuff we just wrote. 40092fdbea25SAleksandr Guzovskiy */ 40102fdbea25SAleksandr Guzovskiy { 40112fdbea25SAleksandr Guzovskiy void *packcheck = umem_alloc(packsize, UMEM_NOFAIL); 40122fdbea25SAleksandr Guzovskiy void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL); 40132fdbea25SAleksandr Guzovskiy 4014b24ab676SJeff Bonwick VERIFY(0 == dmu_read(os, packobj, packoff, 40157bfdf011SNeil Perrin packsize, packcheck, DMU_READ_PREFETCH)); 4016b24ab676SJeff Bonwick VERIFY(0 == dmu_read(os, bigobj, bigoff, 40177bfdf011SNeil Perrin bigsize, bigcheck, DMU_READ_PREFETCH)); 40182fdbea25SAleksandr Guzovskiy 40192fdbea25SAleksandr Guzovskiy ASSERT(bcmp(packbuf, packcheck, packsize) == 0); 40202fdbea25SAleksandr Guzovskiy ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0); 40212fdbea25SAleksandr Guzovskiy 40222fdbea25SAleksandr Guzovskiy umem_free(packcheck, packsize); 40232fdbea25SAleksandr Guzovskiy umem_free(bigcheck, bigsize); 40242fdbea25SAleksandr Guzovskiy } 40252fdbea25SAleksandr Guzovskiy if (i == 2) { 40262fdbea25SAleksandr Guzovskiy txg_wait_open(dmu_objset_pool(os), 0); 40272fdbea25SAleksandr Guzovskiy } else if (i == 3) { 40282fdbea25SAleksandr Guzovskiy txg_wait_synced(dmu_objset_pool(os), 0); 40292fdbea25SAleksandr Guzovskiy } 40302fdbea25SAleksandr Guzovskiy } 40312fdbea25SAleksandr Guzovskiy 40322fdbea25SAleksandr Guzovskiy dmu_buf_rele(bonus_db, FTAG); 40332fdbea25SAleksandr Guzovskiy umem_free(packbuf, packsize); 40342fdbea25SAleksandr Guzovskiy umem_free(bigbuf, bigsize); 40352fdbea25SAleksandr Guzovskiy umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *)); 40362fdbea25SAleksandr Guzovskiy } 40372fdbea25SAleksandr Guzovskiy 4038b24ab676SJeff Bonwick /* ARGSUSED */ 40392fdbea25SAleksandr Guzovskiy void 4040b24ab676SJeff Bonwick ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id) 40414d31c452Smaybee { 4042b24ab676SJeff Bonwick ztest_od_t od[1]; 4043b24ab676SJeff Bonwick uint64_t offset = (1ULL << (ztest_random(20) + 43)) + 4044b24ab676SJeff Bonwick (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT); 40454d31c452Smaybee 40464d31c452Smaybee /* 4047b24ab676SJeff Bonwick * Have multiple threads write to large offsets in an object 4048b24ab676SJeff Bonwick * to verify that parallel writes to an object -- even to the 4049b24ab676SJeff Bonwick * same blocks within the object -- doesn't cause any trouble. 40504d31c452Smaybee */ 4051b24ab676SJeff Bonwick ztest_od_init(&od[0], ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0); 4052b24ab676SJeff Bonwick 4053b24ab676SJeff Bonwick if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0) 4054b24ab676SJeff Bonwick return; 4055b24ab676SJeff Bonwick 4056b24ab676SJeff Bonwick while (ztest_random(10) != 0) 4057b24ab676SJeff Bonwick ztest_io(zd, od[0].od_object, offset); 40584d31c452Smaybee } 40594d31c452Smaybee 40604d31c452Smaybee void 4061b24ab676SJeff Bonwick ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id) 4062fa9e4066Sahrens { 4063b24ab676SJeff Bonwick ztest_od_t od[1]; 4064b24ab676SJeff Bonwick uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) + 4065b24ab676SJeff Bonwick (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT); 4066b24ab676SJeff Bonwick uint64_t count = ztest_random(20) + 1; 4067b24ab676SJeff Bonwick uint64_t blocksize = ztest_random_blocksize(); 4068b24ab676SJeff Bonwick void *data; 4069fa9e4066Sahrens 4070b24ab676SJeff Bonwick ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0); 4071fa9e4066Sahrens 4072b24ab676SJeff Bonwick if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0) 4073e05725b1Sbonwick return; 4074fa9e4066Sahrens 4075b24ab676SJeff Bonwick if (ztest_truncate(zd, od[0].od_object, offset, count * blocksize) != 0) 4076e05725b1Sbonwick return; 4077e05725b1Sbonwick 4078b24ab676SJeff Bonwick ztest_prealloc(zd, od[0].od_object, offset, count * blocksize); 4079e05725b1Sbonwick 4080b24ab676SJeff Bonwick data = umem_zalloc(blocksize, UMEM_NOFAIL); 4081e05725b1Sbonwick 4082b24ab676SJeff Bonwick while (ztest_random(count) != 0) { 4083b24ab676SJeff Bonwick uint64_t randoff = offset + (ztest_random(count) * blocksize); 4084b24ab676SJeff Bonwick if (ztest_write(zd, od[0].od_object, randoff, blocksize, 4085b24ab676SJeff Bonwick data) != 0) 4086b24ab676SJeff Bonwick break; 4087b24ab676SJeff Bonwick while (ztest_random(4) != 0) 4088b24ab676SJeff Bonwick ztest_io(zd, od[0].od_object, randoff); 40892fdbea25SAleksandr Guzovskiy } 4090fa9e4066Sahrens 4091b24ab676SJeff Bonwick umem_free(data, blocksize); 4092fa9e4066Sahrens } 4093fa9e4066Sahrens 4094fa9e4066Sahrens /* 4095fa9e4066Sahrens * Verify that zap_{create,destroy,add,remove,update} work as expected. 4096fa9e4066Sahrens */ 4097fa9e4066Sahrens #define ZTEST_ZAP_MIN_INTS 1 4098fa9e4066Sahrens #define ZTEST_ZAP_MAX_INTS 4 4099fa9e4066Sahrens #define ZTEST_ZAP_MAX_PROPS 1000 4100fa9e4066Sahrens 4101fa9e4066Sahrens void 4102b24ab676SJeff Bonwick ztest_zap(ztest_ds_t *zd, uint64_t id) 4103fa9e4066Sahrens { 4104b24ab676SJeff Bonwick objset_t *os = zd->zd_os; 4105b24ab676SJeff Bonwick ztest_od_t od[1]; 4106fa9e4066Sahrens uint64_t object; 4107fa9e4066Sahrens uint64_t txg, last_txg; 4108fa9e4066Sahrens uint64_t value[ZTEST_ZAP_MAX_INTS]; 4109fa9e4066Sahrens uint64_t zl_ints, zl_intsize, prop; 4110fa9e4066Sahrens int i, ints; 4111fa9e4066Sahrens dmu_tx_t *tx; 4112fa9e4066Sahrens char propname[100], txgname[100]; 4113fa9e4066Sahrens int error; 4114fa9e4066Sahrens char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" }; 4115fa9e4066Sahrens 4116b24ab676SJeff Bonwick ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0); 4117fa9e4066Sahrens 4118b24ab676SJeff Bonwick if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0) 4119fa9e4066Sahrens return; 4120b24ab676SJeff Bonwick 4121b24ab676SJeff Bonwick object = od[0].od_object; 4122b24ab676SJeff Bonwick 4123fa9e4066Sahrens /* 4124fa9e4066Sahrens * Generate a known hash collision, and verify that 4125fa9e4066Sahrens * we can lookup and remove both entries. 4126fa9e4066Sahrens */ 4127b24ab676SJeff Bonwick tx = dmu_tx_create(os); 4128b24ab676SJeff Bonwick dmu_tx_hold_zap(tx, object, B_TRUE, NULL); 4129b24ab676SJeff Bonwick txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG); 4130b24ab676SJeff Bonwick if (txg == 0) 4131b24ab676SJeff Bonwick return; 4132fa9e4066Sahrens for (i = 0; i < 2; i++) { 4133fa9e4066Sahrens value[i] = i; 4134b420f3adSRichard Lowe VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t), 4135b24ab676SJeff Bonwick 1, &value[i], tx)); 4136fa9e4066Sahrens } 4137fa9e4066Sahrens for (i = 0; i < 2; i++) { 4138b24ab676SJeff Bonwick VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i], 4139b24ab676SJeff Bonwick sizeof (uint64_t), 1, &value[i], tx)); 4140b420f3adSRichard Lowe VERIFY3U(0, ==, 4141b420f3adSRichard Lowe zap_length(os, object, hc[i], &zl_intsize, &zl_ints)); 4142fa9e4066Sahrens ASSERT3U(zl_intsize, ==, sizeof (uint64_t)); 4143fa9e4066Sahrens ASSERT3U(zl_ints, ==, 1); 4144fa9e4066Sahrens } 4145fa9e4066Sahrens for (i = 0; i < 2; i++) { 4146b420f3adSRichard Lowe VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx)); 4147fa9e4066Sahrens } 4148fa9e4066Sahrens dmu_tx_commit(tx); 4149fa9e4066Sahrens 4150b24ab676SJeff Bonwick /* 4151b24ab676SJeff Bonwick * Generate a buch of random entries. 4152b24ab676SJeff Bonwick */ 4153fa9e4066Sahrens ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS); 4154fa9e4066Sahrens 4155fa9e4066Sahrens prop = ztest_random(ZTEST_ZAP_MAX_PROPS); 4156fa9e4066Sahrens (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop); 4157fa9e4066Sahrens (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop); 4158fa9e4066Sahrens bzero(value, sizeof (value)); 4159fa9e4066Sahrens last_txg = 0; 4160fa9e4066Sahrens 4161fa9e4066Sahrens /* 4162fa9e4066Sahrens * If these zap entries already exist, validate their contents. 4163fa9e4066Sahrens */ 4164fa9e4066Sahrens error = zap_length(os, object, txgname, &zl_intsize, &zl_ints); 4165fa9e4066Sahrens if (error == 0) { 4166fa9e4066Sahrens ASSERT3U(zl_intsize, ==, sizeof (uint64_t)); 4167fa9e4066Sahrens ASSERT3U(zl_ints, ==, 1); 4168fa9e4066Sahrens 4169e05725b1Sbonwick VERIFY(zap_lookup(os, object, txgname, zl_intsize, 4170e05725b1Sbonwick zl_ints, &last_txg) == 0); 4171fa9e4066Sahrens 4172e05725b1Sbonwick VERIFY(zap_length(os, object, propname, &zl_intsize, 4173e05725b1Sbonwick &zl_ints) == 0); 4174fa9e4066Sahrens 4175fa9e4066Sahrens ASSERT3U(zl_intsize, ==, sizeof (uint64_t)); 4176fa9e4066Sahrens ASSERT3U(zl_ints, ==, ints); 4177fa9e4066Sahrens 4178e05725b1Sbonwick VERIFY(zap_lookup(os, object, propname, zl_intsize, 4179e05725b1Sbonwick zl_ints, value) == 0); 4180fa9e4066Sahrens 4181fa9e4066Sahrens for (i = 0; i < ints; i++) { 4182fa9e4066Sahrens ASSERT3U(value[i], ==, last_txg + object + i); 4183fa9e4066Sahrens } 4184fa9e4066Sahrens } else { 4185fa9e4066Sahrens ASSERT3U(error, ==, ENOENT); 4186fa9e4066Sahrens } 4187fa9e4066Sahrens 4188fa9e4066Sahrens /* 4189fa9e4066Sahrens * Atomically update two entries in our zap object. 4190fa9e4066Sahrens * The first is named txg_%llu, and contains the txg 4191fa9e4066Sahrens * in which the property was last updated. The second 4192fa9e4066Sahrens * is named prop_%llu, and the nth element of its value 4193fa9e4066Sahrens * should be txg + object + n. 4194fa9e4066Sahrens */ 4195fa9e4066Sahrens tx = dmu_tx_create(os); 4196b24ab676SJeff Bonwick dmu_tx_hold_zap(tx, object, B_TRUE, NULL); 4197b24ab676SJeff Bonwick txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG); 4198b24ab676SJeff Bonwick if (txg == 0) 4199fa9e4066Sahrens return; 4200fa9e4066Sahrens 4201fa9e4066Sahrens if (last_txg > txg) 4202e05725b1Sbonwick fatal(0, "zap future leak: old %llu new %llu", last_txg, txg); 4203fa9e4066Sahrens 4204fa9e4066Sahrens for (i = 0; i < ints; i++) 4205fa9e4066Sahrens value[i] = txg + object + i; 4206fa9e4066Sahrens 4207b420f3adSRichard Lowe VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t), 4208b24ab676SJeff Bonwick 1, &txg, tx)); 4209b420f3adSRichard Lowe VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t), 4210b24ab676SJeff Bonwick ints, value, tx)); 4211fa9e4066Sahrens 4212fa9e4066Sahrens dmu_tx_commit(tx); 4213fa9e4066Sahrens 4214fa9e4066Sahrens /* 4215fa9e4066Sahrens * Remove a random pair of entries. 4216fa9e4066Sahrens */ 4217fa9e4066Sahrens prop = ztest_random(ZTEST_ZAP_MAX_PROPS); 4218fa9e4066Sahrens (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop); 4219fa9e4066Sahrens (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop); 4220fa9e4066Sahrens 4221fa9e4066Sahrens error = zap_length(os, object, txgname, &zl_intsize, &zl_ints); 4222fa9e4066Sahrens 4223fa9e4066Sahrens if (error == ENOENT) 4224e05725b1Sbonwick return; 4225fa9e4066Sahrens 4226fb09f5aaSMadhav Suresh ASSERT0(error); 4227fa9e4066Sahrens 4228fa9e4066Sahrens tx = dmu_tx_create(os); 4229b24ab676SJeff Bonwick dmu_tx_hold_zap(tx, object, B_TRUE, NULL); 4230b24ab676SJeff Bonwick txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG); 4231b24ab676SJeff Bonwick if (txg == 0) 4232fa9e4066Sahrens return; 4233b420f3adSRichard Lowe VERIFY3U(0, ==, zap_remove(os, object, txgname, tx)); 4234b420f3adSRichard Lowe VERIFY3U(0, ==, zap_remove(os, object, propname, tx)); 4235fa9e4066Sahrens dmu_tx_commit(tx); 4236fa9e4066Sahrens } 4237fa9e4066Sahrens 423812a2833aSSanjeev Bagewadi /* 423912a2833aSSanjeev Bagewadi * Testcase to test the upgrading of a microzap to fatzap. 424012a2833aSSanjeev Bagewadi */ 424112a2833aSSanjeev Bagewadi void 4242b24ab676SJeff Bonwick ztest_fzap(ztest_ds_t *zd, uint64_t id) 424312a2833aSSanjeev Bagewadi { 4244b24ab676SJeff Bonwick objset_t *os = zd->zd_os; 4245b24ab676SJeff Bonwick ztest_od_t od[1]; 4246b24ab676SJeff Bonwick uint64_t object, txg; 424712a2833aSSanjeev Bagewadi 4248b24ab676SJeff Bonwick ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0); 424912a2833aSSanjeev Bagewadi 4250b24ab676SJeff Bonwick if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0) 425112a2833aSSanjeev Bagewadi return; 4252b24ab676SJeff Bonwick 4253b24ab676SJeff Bonwick object = od[0].od_object; 425412a2833aSSanjeev Bagewadi 425512a2833aSSanjeev Bagewadi /* 4256b24ab676SJeff Bonwick * Add entries to this ZAP and make sure it spills over 425712a2833aSSanjeev Bagewadi * and gets upgraded to a fatzap. Also, since we are adding 4258b24ab676SJeff Bonwick * 2050 entries we should see ptrtbl growth and leaf-block split. 425912a2833aSSanjeev Bagewadi */ 4260b24ab676SJeff Bonwick for (int i = 0; i < 2050; i++) { 4261*40a5c998SMatthew Ahrens char name[ZFS_MAX_DATASET_NAME_LEN]; 4262b24ab676SJeff Bonwick uint64_t value = i; 4263b24ab676SJeff Bonwick dmu_tx_t *tx; 4264b24ab676SJeff Bonwick int error; 4265b24ab676SJeff Bonwick 4266b24ab676SJeff Bonwick (void) snprintf(name, sizeof (name), "fzap-%llu-%llu", 4267b24ab676SJeff Bonwick id, value); 426812a2833aSSanjeev Bagewadi 426912a2833aSSanjeev Bagewadi tx = dmu_tx_create(os); 4270b24ab676SJeff Bonwick dmu_tx_hold_zap(tx, object, B_TRUE, name); 4271b24ab676SJeff Bonwick txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG); 4272b24ab676SJeff Bonwick if (txg == 0) 427312a2833aSSanjeev Bagewadi return; 4274b24ab676SJeff Bonwick error = zap_add(os, object, name, sizeof (uint64_t), 1, 4275b24ab676SJeff Bonwick &value, tx); 427612a2833aSSanjeev Bagewadi ASSERT(error == 0 || error == EEXIST); 427712a2833aSSanjeev Bagewadi dmu_tx_commit(tx); 427812a2833aSSanjeev Bagewadi } 427912a2833aSSanjeev Bagewadi } 428012a2833aSSanjeev Bagewadi 4281b24ab676SJeff Bonwick /* ARGSUSED */ 4282fa9e4066Sahrens void 4283b24ab676SJeff Bonwick ztest_zap_parallel(ztest_ds_t *zd, uint64_t id) 4284fa9e4066Sahrens { 4285b24ab676SJeff Bonwick objset_t *os = zd->zd_os; 4286b24ab676SJeff Bonwick ztest_od_t od[1]; 4287fa9e4066Sahrens uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc; 4288fa9e4066Sahrens dmu_tx_t *tx; 4289fa9e4066Sahrens int i, namelen, error; 4290b24ab676SJeff Bonwick int micro = ztest_random(2); 4291fa9e4066Sahrens char name[20], string_value[20]; 4292fa9e4066Sahrens void *data; 4293fa9e4066Sahrens 4294b24ab676SJeff Bonwick ztest_od_init(&od[0], ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER, 0, 0); 4295b24ab676SJeff Bonwick 4296b24ab676SJeff Bonwick if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0) 4297b24ab676SJeff Bonwick return; 4298b24ab676SJeff Bonwick 4299b24ab676SJeff Bonwick object = od[0].od_object; 4300b24ab676SJeff Bonwick 4301fa9e4066Sahrens /* 4302fa9e4066Sahrens * Generate a random name of the form 'xxx.....' where each 4303fa9e4066Sahrens * x is a random printable character and the dots are dots. 4304fa9e4066Sahrens * There are 94 such characters, and the name length goes from 4305fa9e4066Sahrens * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names. 4306fa9e4066Sahrens */ 4307fa9e4066Sahrens namelen = ztest_random(sizeof (name) - 5) + 5 + 1; 4308fa9e4066Sahrens 4309fa9e4066Sahrens for (i = 0; i < 3; i++) 4310fa9e4066Sahrens name[i] = '!' + ztest_random('~' - '!' + 1); 4311fa9e4066Sahrens for (; i < namelen - 1; i++) 4312fa9e4066Sahrens name[i] = '.'; 4313fa9e4066Sahrens name[i] = '\0'; 4314fa9e4066Sahrens 4315b24ab676SJeff Bonwick if ((namelen & 1) || micro) { 4316fa9e4066Sahrens wsize = sizeof (txg); 4317fa9e4066Sahrens wc = 1; 4318fa9e4066Sahrens data = &txg; 4319fa9e4066Sahrens } else { 4320fa9e4066Sahrens wsize = 1; 4321fa9e4066Sahrens wc = namelen; 4322fa9e4066Sahrens data = string_value; 4323fa9e4066Sahrens } 4324fa9e4066Sahrens 4325fa9e4066Sahrens count = -1ULL; 43263b2aab18SMatthew Ahrens VERIFY0(zap_count(os, object, &count)); 4327fa9e4066Sahrens ASSERT(count != -1ULL); 4328fa9e4066Sahrens 4329fa9e4066Sahrens /* 4330fa9e4066Sahrens * Select an operation: length, lookup, add, update, remove. 4331fa9e4066Sahrens */ 4332fa9e4066Sahrens i = ztest_random(5); 4333fa9e4066Sahrens 4334fa9e4066Sahrens if (i >= 2) { 4335fa9e4066Sahrens tx = dmu_tx_create(os); 4336b24ab676SJeff Bonwick dmu_tx_hold_zap(tx, object, B_TRUE, NULL); 4337b24ab676SJeff Bonwick txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG); 4338b24ab676SJeff Bonwick if (txg == 0) 4339fa9e4066Sahrens return; 4340fa9e4066Sahrens bcopy(name, string_value, namelen); 4341fa9e4066Sahrens } else { 4342fa9e4066Sahrens tx = NULL; 4343fa9e4066Sahrens txg = 0; 4344fa9e4066Sahrens bzero(string_value, namelen); 4345fa9e4066Sahrens } 4346fa9e4066Sahrens 4347fa9e4066Sahrens switch (i) { 4348fa9e4066Sahrens 4349fa9e4066Sahrens case 0: 4350fa9e4066Sahrens error = zap_length(os, object, name, &zl_wsize, &zl_wc); 4351fa9e4066Sahrens if (error == 0) { 4352fa9e4066Sahrens ASSERT3U(wsize, ==, zl_wsize); 4353fa9e4066Sahrens ASSERT3U(wc, ==, zl_wc); 4354fa9e4066Sahrens } else { 4355fa9e4066Sahrens ASSERT3U(error, ==, ENOENT); 4356fa9e4066Sahrens } 4357fa9e4066Sahrens break; 4358fa9e4066Sahrens 4359fa9e4066Sahrens case 1: 4360fa9e4066Sahrens error = zap_lookup(os, object, name, wsize, wc, data); 4361fa9e4066Sahrens if (error == 0) { 4362fa9e4066Sahrens if (data == string_value && 4363fa9e4066Sahrens bcmp(name, data, namelen) != 0) 4364fa9e4066Sahrens fatal(0, "name '%s' != val '%s' len %d", 4365fa9e4066Sahrens name, data, namelen); 4366fa9e4066Sahrens } else { 4367fa9e4066Sahrens ASSERT3U(error, ==, ENOENT); 4368fa9e4066Sahrens } 4369fa9e4066Sahrens break; 4370fa9e4066Sahrens 4371fa9e4066Sahrens case 2: 4372fa9e4066Sahrens error = zap_add(os, object, name, wsize, wc, data, tx); 4373fa9e4066Sahrens ASSERT(error == 0 || error == EEXIST); 4374fa9e4066Sahrens break; 4375fa9e4066Sahrens 4376fa9e4066Sahrens case 3: 4377e05725b1Sbonwick VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0); 4378fa9e4066Sahrens break; 4379fa9e4066Sahrens 4380fa9e4066Sahrens case 4: 4381fa9e4066Sahrens error = zap_remove(os, object, name, tx); 4382fa9e4066Sahrens ASSERT(error == 0 || error == ENOENT); 4383fa9e4066Sahrens break; 4384fa9e4066Sahrens } 4385fa9e4066Sahrens 4386fa9e4066Sahrens if (tx != NULL) 4387fa9e4066Sahrens dmu_tx_commit(tx); 4388fa9e4066Sahrens } 4389fa9e4066Sahrens 4390d20e665cSRicardo M. Correia /* 4391d20e665cSRicardo M. Correia * Commit callback data. 4392d20e665cSRicardo M. Correia */ 4393d20e665cSRicardo M. Correia typedef struct ztest_cb_data { 4394d20e665cSRicardo M. Correia list_node_t zcd_node; 4395d20e665cSRicardo M. Correia uint64_t zcd_txg; 4396d20e665cSRicardo M. Correia int zcd_expected_err; 4397d20e665cSRicardo M. Correia boolean_t zcd_added; 4398d20e665cSRicardo M. Correia boolean_t zcd_called; 4399d20e665cSRicardo M. Correia spa_t *zcd_spa; 4400d20e665cSRicardo M. Correia } ztest_cb_data_t; 4401d20e665cSRicardo M. Correia 4402d20e665cSRicardo M. Correia /* This is the actual commit callback function */ 4403d20e665cSRicardo M. Correia static void 4404d20e665cSRicardo M. Correia ztest_commit_callback(void *arg, int error) 4405d20e665cSRicardo M. Correia { 4406d20e665cSRicardo M. Correia ztest_cb_data_t *data = arg; 4407d20e665cSRicardo M. Correia uint64_t synced_txg; 4408d20e665cSRicardo M. Correia 4409d20e665cSRicardo M. Correia VERIFY(data != NULL); 4410d20e665cSRicardo M. Correia VERIFY3S(data->zcd_expected_err, ==, error); 4411d20e665cSRicardo M. Correia VERIFY(!data->zcd_called); 4412d20e665cSRicardo M. Correia 4413d20e665cSRicardo M. Correia synced_txg = spa_last_synced_txg(data->zcd_spa); 4414d20e665cSRicardo M. Correia if (data->zcd_txg > synced_txg) 4415d20e665cSRicardo M. Correia fatal(0, "commit callback of txg %" PRIu64 " called prematurely" 4416d20e665cSRicardo M. Correia ", last synced txg = %" PRIu64 "\n", data->zcd_txg, 4417d20e665cSRicardo M. Correia synced_txg); 4418d20e665cSRicardo M. Correia 4419d20e665cSRicardo M. Correia data->zcd_called = B_TRUE; 4420d20e665cSRicardo M. Correia 4421d20e665cSRicardo M. Correia if (error == ECANCELED) { 4422fb09f5aaSMadhav Suresh ASSERT0(data->zcd_txg); 4423d20e665cSRicardo M. Correia ASSERT(!data->zcd_added); 4424d20e665cSRicardo M. Correia 4425d20e665cSRicardo M. Correia /* 4426d20e665cSRicardo M. Correia * The private callback data should be destroyed here, but 4427d20e665cSRicardo M. Correia * since we are going to check the zcd_called field after 4428d20e665cSRicardo M. Correia * dmu_tx_abort(), we will destroy it there. 4429d20e665cSRicardo M. Correia */ 4430d20e665cSRicardo M. Correia return; 4431d20e665cSRicardo M. Correia } 4432d20e665cSRicardo M. Correia 4433d20e665cSRicardo M. Correia /* Was this callback added to the global callback list? */ 4434d20e665cSRicardo M. Correia if (!data->zcd_added) 4435d20e665cSRicardo M. Correia goto out; 4436d20e665cSRicardo M. Correia 4437d20e665cSRicardo M. Correia ASSERT3U(data->zcd_txg, !=, 0); 4438d20e665cSRicardo M. Correia 4439d20e665cSRicardo M. Correia /* Remove our callback from the list */ 4440d20e665cSRicardo M. Correia (void) mutex_lock(&zcl.zcl_callbacks_lock); 4441d20e665cSRicardo M. Correia list_remove(&zcl.zcl_callbacks, data); 4442d20e665cSRicardo M. Correia (void) mutex_unlock(&zcl.zcl_callbacks_lock); 4443d20e665cSRicardo M. Correia 4444d20e665cSRicardo M. Correia out: 4445d20e665cSRicardo M. Correia umem_free(data, sizeof (ztest_cb_data_t)); 4446d20e665cSRicardo M. Correia } 4447d20e665cSRicardo M. Correia 4448d20e665cSRicardo M. Correia /* Allocate and initialize callback data structure */ 4449d20e665cSRicardo M. Correia static ztest_cb_data_t * 4450d20e665cSRicardo M. Correia ztest_create_cb_data(objset_t *os, uint64_t txg) 4451d20e665cSRicardo M. Correia { 4452d20e665cSRicardo M. Correia ztest_cb_data_t *cb_data; 4453d20e665cSRicardo M. Correia 4454d20e665cSRicardo M. Correia cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL); 4455d20e665cSRicardo M. Correia 4456d20e665cSRicardo M. Correia cb_data->zcd_txg = txg; 4457d20e665cSRicardo M. Correia cb_data->zcd_spa = dmu_objset_spa(os); 4458d20e665cSRicardo M. Correia 4459d20e665cSRicardo M. Correia return (cb_data); 4460d20e665cSRicardo M. Correia } 4461d20e665cSRicardo M. Correia 4462d20e665cSRicardo M. Correia /* 4463d20e665cSRicardo M. Correia * If a number of txgs equal to this threshold have been created after a commit 4464d20e665cSRicardo M. Correia * callback has been registered but not called, then we assume there is an 4465d20e665cSRicardo M. Correia * implementation bug. 4466d20e665cSRicardo M. Correia */ 4467d20e665cSRicardo M. Correia #define ZTEST_COMMIT_CALLBACK_THRESH (TXG_CONCURRENT_STATES + 2) 4468d20e665cSRicardo M. Correia 4469d20e665cSRicardo M. Correia /* 4470d20e665cSRicardo M. Correia * Commit callback test. 4471d20e665cSRicardo M. Correia */ 4472d20e665cSRicardo M. Correia void 4473b24ab676SJeff Bonwick ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id) 4474d20e665cSRicardo M. Correia { 4475b24ab676SJeff Bonwick objset_t *os = zd->zd_os; 4476b24ab676SJeff Bonwick ztest_od_t od[1]; 4477d20e665cSRicardo M. Correia dmu_tx_t *tx; 4478d20e665cSRicardo M. Correia ztest_cb_data_t *cb_data[3], *tmp_cb; 4479d20e665cSRicardo M. Correia uint64_t old_txg, txg; 4480d20e665cSRicardo M. Correia int i, error; 4481d20e665cSRicardo M. Correia 4482b24ab676SJeff Bonwick ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0); 4483b24ab676SJeff Bonwick 4484b24ab676SJeff Bonwick if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0) 4485b24ab676SJeff Bonwick return; 4486b24ab676SJeff Bonwick 4487d20e665cSRicardo M. Correia tx = dmu_tx_create(os); 4488d20e665cSRicardo M. Correia 4489d20e665cSRicardo M. Correia cb_data[0] = ztest_create_cb_data(os, 0); 4490d20e665cSRicardo M. Correia dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]); 4491d20e665cSRicardo M. Correia 4492b24ab676SJeff Bonwick dmu_tx_hold_write(tx, od[0].od_object, 0, sizeof (uint64_t)); 4493d20e665cSRicardo M. Correia 4494d20e665cSRicardo M. Correia /* Every once in a while, abort the transaction on purpose */ 4495d20e665cSRicardo M. Correia if (ztest_random(100) == 0) 4496d20e665cSRicardo M. Correia error = -1; 4497d20e665cSRicardo M. Correia 4498d20e665cSRicardo M. Correia if (!error) 4499d20e665cSRicardo M. Correia error = dmu_tx_assign(tx, TXG_NOWAIT); 4500d20e665cSRicardo M. Correia 4501d20e665cSRicardo M. Correia txg = error ? 0 : dmu_tx_get_txg(tx); 4502d20e665cSRicardo M. Correia 4503d20e665cSRicardo M. Correia cb_data[0]->zcd_txg = txg; 4504d20e665cSRicardo M. Correia cb_data[1] = ztest_create_cb_data(os, txg); 4505d20e665cSRicardo M. Correia dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]); 4506d20e665cSRicardo M. Correia 4507d20e665cSRicardo M. Correia if (error) { 4508d20e665cSRicardo M. Correia /* 4509d20e665cSRicardo M. Correia * It's not a strict requirement to call the registered 4510d20e665cSRicardo M. Correia * callbacks from inside dmu_tx_abort(), but that's what 4511d20e665cSRicardo M. Correia * it's supposed to happen in the current implementation 4512d20e665cSRicardo M. Correia * so we will check for that. 4513d20e665cSRicardo M. Correia */ 4514d20e665cSRicardo M. Correia for (i = 0; i < 2; i++) { 4515d20e665cSRicardo M. Correia cb_data[i]->zcd_expected_err = ECANCELED; 4516d20e665cSRicardo M. Correia VERIFY(!cb_data[i]->zcd_called); 4517d20e665cSRicardo M. Correia } 4518d20e665cSRicardo M. Correia 4519d20e665cSRicardo M. Correia dmu_tx_abort(tx); 4520d20e665cSRicardo M. Correia 4521d20e665cSRicardo M. Correia for (i = 0; i < 2; i++) { 4522d20e665cSRicardo M. Correia VERIFY(cb_data[i]->zcd_called); 4523d20e665cSRicardo M. Correia umem_free(cb_data[i], sizeof (ztest_cb_data_t)); 4524d20e665cSRicardo M. Correia } 4525d20e665cSRicardo M. Correia 4526d20e665cSRicardo M. Correia return; 4527d20e665cSRicardo M. Correia } 4528d20e665cSRicardo M. Correia 4529d20e665cSRicardo M. Correia cb_data[2] = ztest_create_cb_data(os, txg); 4530d20e665cSRicardo M. Correia dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]); 4531d20e665cSRicardo M. Correia 4532d20e665cSRicardo M. Correia /* 4533d20e665cSRicardo M. Correia * Read existing data to make sure there isn't a future leak. 4534d20e665cSRicardo M. Correia */ 4535b24ab676SJeff Bonwick VERIFY(0 == dmu_read(os, od[0].od_object, 0, sizeof (uint64_t), 4536d20e665cSRicardo M. Correia &old_txg, DMU_READ_PREFETCH)); 4537d20e665cSRicardo M. Correia 4538d20e665cSRicardo M. Correia if (old_txg > txg) 4539d20e665cSRicardo M. Correia fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64, 4540d20e665cSRicardo M. Correia old_txg, txg); 4541d20e665cSRicardo M. Correia 4542b24ab676SJeff Bonwick dmu_write(os, od[0].od_object, 0, sizeof (uint64_t), &txg, tx); 4543d20e665cSRicardo M. Correia 4544d20e665cSRicardo M. Correia (void) mutex_lock(&zcl.zcl_callbacks_lock); 4545d20e665cSRicardo M. Correia 4546d20e665cSRicardo M. Correia /* 4547d20e665cSRicardo M. Correia * Since commit callbacks don't have any ordering requirement and since 4548d20e665cSRicardo M. Correia * it is theoretically possible for a commit callback to be called 4549d20e665cSRicardo M. Correia * after an arbitrary amount of time has elapsed since its txg has been 4550d20e665cSRicardo M. Correia * synced, it is difficult to reliably determine whether a commit 4551d20e665cSRicardo M. Correia * callback hasn't been called due to high load or due to a flawed 4552d20e665cSRicardo M. Correia * implementation. 4553d20e665cSRicardo M. Correia * 4554d20e665cSRicardo M. Correia * In practice, we will assume that if after a certain number of txgs a 4555d20e665cSRicardo M. Correia * commit callback hasn't been called, then most likely there's an 4556d20e665cSRicardo M. Correia * implementation bug.. 4557d20e665cSRicardo M. Correia */ 4558d20e665cSRicardo M. Correia tmp_cb = list_head(&zcl.zcl_callbacks); 4559d20e665cSRicardo M. Correia if (tmp_cb != NULL && 4560b3d9f2e2SWill Andrews (txg - ZTEST_COMMIT_CALLBACK_THRESH) > tmp_cb->zcd_txg) { 4561d20e665cSRicardo M. Correia fatal(0, "Commit callback threshold exceeded, oldest txg: %" 4562d20e665cSRicardo M. Correia PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg); 4563d20e665cSRicardo M. Correia } 4564d20e665cSRicardo M. Correia 4565d20e665cSRicardo M. Correia /* 4566d20e665cSRicardo M. Correia * Let's find the place to insert our callbacks. 4567d20e665cSRicardo M. Correia * 4568d20e665cSRicardo M. Correia * Even though the list is ordered by txg, it is possible for the 4569d20e665cSRicardo M. Correia * insertion point to not be the end because our txg may already be 4570d20e665cSRicardo M. Correia * quiescing at this point and other callbacks in the open txg 4571d20e665cSRicardo M. Correia * (from other objsets) may have sneaked in. 4572d20e665cSRicardo M. Correia */ 4573d20e665cSRicardo M. Correia tmp_cb = list_tail(&zcl.zcl_callbacks); 4574d20e665cSRicardo M. Correia while (tmp_cb != NULL && tmp_cb->zcd_txg > txg) 4575d20e665cSRicardo M. Correia tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb); 4576d20e665cSRicardo M. Correia 4577d20e665cSRicardo M. Correia /* Add the 3 callbacks to the list */ 4578d20e665cSRicardo M. Correia for (i = 0; i < 3; i++) { 4579d20e665cSRicardo M. Correia if (tmp_cb == NULL) 4580d20e665cSRicardo M. Correia list_insert_head(&zcl.zcl_callbacks, cb_data[i]); 4581d20e665cSRicardo M. Correia else 4582d20e665cSRicardo M. Correia list_insert_after(&zcl.zcl_callbacks, tmp_cb, 4583d20e665cSRicardo M. Correia cb_data[i]); 4584d20e665cSRicardo M. Correia 4585d20e665cSRicardo M. Correia cb_data[i]->zcd_added = B_TRUE; 4586d20e665cSRicardo M. Correia VERIFY(!cb_data[i]->zcd_called); 4587d20e665cSRicardo M. Correia 4588d20e665cSRicardo M. Correia tmp_cb = cb_data[i]; 4589d20e665cSRicardo M. Correia } 4590d20e665cSRicardo M. Correia 4591d20e665cSRicardo M. Correia (void) mutex_unlock(&zcl.zcl_callbacks_lock); 4592d20e665cSRicardo M. Correia 4593d20e665cSRicardo M. Correia dmu_tx_commit(tx); 4594d20e665cSRicardo M. Correia } 4595d20e665cSRicardo M. Correia 4596b24ab676SJeff Bonwick /* ARGSUSED */ 4597fa9e4066Sahrens void 4598b24ab676SJeff Bonwick ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id) 4599fa9e4066Sahrens { 4600b24ab676SJeff Bonwick zfs_prop_t proplist[] = { 4601b24ab676SJeff Bonwick ZFS_PROP_CHECKSUM, 4602b24ab676SJeff Bonwick ZFS_PROP_COMPRESSION, 4603b24ab676SJeff Bonwick ZFS_PROP_COPIES, 4604b24ab676SJeff Bonwick ZFS_PROP_DEDUP 4605b24ab676SJeff Bonwick }; 4606fa9e4066Sahrens 4607420dfc95SChris Siden (void) rw_rdlock(&ztest_name_lock); 4608fa9e4066Sahrens 4609b24ab676SJeff Bonwick for (int p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++) 4610b24ab676SJeff Bonwick (void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p], 4611b24ab676SJeff Bonwick ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2)); 4612fa9e4066Sahrens 4613420dfc95SChris Siden (void) rw_unlock(&ztest_name_lock); 4614fa9e4066Sahrens } 4615fa9e4066Sahrens 4616b24ab676SJeff Bonwick /* ARGSUSED */ 4617b24ab676SJeff Bonwick void 4618b24ab676SJeff Bonwick ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id) 4619b24ab676SJeff Bonwick { 4620b24ab676SJeff Bonwick nvlist_t *props = NULL; 4621ea8dc4b6Seschrock 4622420dfc95SChris Siden (void) rw_rdlock(&ztest_name_lock); 4623ea8dc4b6Seschrock 4624420dfc95SChris Siden (void) ztest_spa_prop_set_uint64(ZPOOL_PROP_DEDUPDITTO, 4625b24ab676SJeff Bonwick ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN)); 4626fa9e4066Sahrens 4627fb09f5aaSMadhav Suresh VERIFY0(spa_prop_get(ztest_spa, &props)); 4628fa9e4066Sahrens 4629420dfc95SChris Siden if (ztest_opts.zo_verbose >= 6) 4630b24ab676SJeff Bonwick dump_nvlist(props, 4); 4631fa9e4066Sahrens 4632b24ab676SJeff Bonwick nvlist_free(props); 4633fa9e4066Sahrens 4634420dfc95SChris Siden (void) rw_unlock(&ztest_name_lock); 4635fa9e4066Sahrens } 4636fa9e4066Sahrens 46373b2aab18SMatthew Ahrens static int 46383b2aab18SMatthew Ahrens user_release_one(const char *snapname, const char *holdname) 46393b2aab18SMatthew Ahrens { 46403b2aab18SMatthew Ahrens nvlist_t *snaps, *holds; 46413b2aab18SMatthew Ahrens int error; 46423b2aab18SMatthew Ahrens 46433b2aab18SMatthew Ahrens snaps = fnvlist_alloc(); 46443b2aab18SMatthew Ahrens holds = fnvlist_alloc(); 46453b2aab18SMatthew Ahrens fnvlist_add_boolean(holds, holdname); 46463b2aab18SMatthew Ahrens fnvlist_add_nvlist(snaps, snapname, holds); 46473b2aab18SMatthew Ahrens fnvlist_free(holds); 46483b2aab18SMatthew Ahrens error = dsl_dataset_user_release(snaps, NULL); 46493b2aab18SMatthew Ahrens fnvlist_free(snaps); 46503b2aab18SMatthew Ahrens return (error); 46513b2aab18SMatthew Ahrens } 46523b2aab18SMatthew Ahrens 4653fa9e4066Sahrens /* 46545c987a37SChris Kirby * Test snapshot hold/release and deferred destroy. 46555c987a37SChris Kirby */ 46565c987a37SChris Kirby void 4657b24ab676SJeff Bonwick ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id) 46585c987a37SChris Kirby { 46595c987a37SChris Kirby int error; 4660b24ab676SJeff Bonwick objset_t *os = zd->zd_os; 46615c987a37SChris Kirby objset_t *origin; 46625c987a37SChris Kirby char snapname[100]; 46635c987a37SChris Kirby char fullname[100]; 46645c987a37SChris Kirby char clonename[100]; 46655c987a37SChris Kirby char tag[100]; 4666*40a5c998SMatthew Ahrens char osname[ZFS_MAX_DATASET_NAME_LEN]; 46673b2aab18SMatthew Ahrens nvlist_t *holds; 46685c987a37SChris Kirby 4669420dfc95SChris Siden (void) rw_rdlock(&ztest_name_lock); 46705c987a37SChris Kirby 46715c987a37SChris Kirby dmu_objset_name(os, osname); 46725c987a37SChris Kirby 46733b2aab18SMatthew Ahrens (void) snprintf(snapname, sizeof (snapname), "sh1_%llu", id); 46743b2aab18SMatthew Ahrens (void) snprintf(fullname, sizeof (fullname), "%s@%s", osname, snapname); 46753b2aab18SMatthew Ahrens (void) snprintf(clonename, sizeof (clonename), 46763b2aab18SMatthew Ahrens "%s/ch1_%llu", osname, id); 46773b2aab18SMatthew Ahrens (void) snprintf(tag, sizeof (tag), "tag_%llu", id); 46785c987a37SChris Kirby 46795c987a37SChris Kirby /* 46805c987a37SChris Kirby * Clean up from any previous run. 46815c987a37SChris Kirby */ 46823b2aab18SMatthew Ahrens error = dsl_destroy_head(clonename); 46833b2aab18SMatthew Ahrens if (error != ENOENT) 46843b2aab18SMatthew Ahrens ASSERT0(error); 46853b2aab18SMatthew Ahrens error = user_release_one(fullname, tag); 46863b2aab18SMatthew Ahrens if (error != ESRCH && error != ENOENT) 46873b2aab18SMatthew Ahrens ASSERT0(error); 46883b2aab18SMatthew Ahrens error = dsl_destroy_snapshot(fullname, B_FALSE); 46893b2aab18SMatthew Ahrens if (error != ENOENT) 46903b2aab18SMatthew Ahrens ASSERT0(error); 46915c987a37SChris Kirby 46925c987a37SChris Kirby /* 46935c987a37SChris Kirby * Create snapshot, clone it, mark snap for deferred destroy, 46945c987a37SChris Kirby * destroy clone, verify snap was also destroyed. 46955c987a37SChris Kirby */ 46964445fffbSMatthew Ahrens error = dmu_objset_snapshot_one(osname, snapname); 46975c987a37SChris Kirby if (error) { 46985c987a37SChris Kirby if (error == ENOSPC) { 46995c987a37SChris Kirby ztest_record_enospc("dmu_objset_snapshot"); 47005c987a37SChris Kirby goto out; 47015c987a37SChris Kirby } 47025c987a37SChris Kirby fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error); 47035c987a37SChris Kirby } 47045c987a37SChris Kirby 47053b2aab18SMatthew Ahrens error = dmu_objset_clone(clonename, fullname); 47065c987a37SChris Kirby if (error) { 47075c987a37SChris Kirby if (error == ENOSPC) { 47085c987a37SChris Kirby ztest_record_enospc("dmu_objset_clone"); 47095c987a37SChris Kirby goto out; 47105c987a37SChris Kirby } 47115c987a37SChris Kirby fatal(0, "dmu_objset_clone(%s) = %d", clonename, error); 47125c987a37SChris Kirby } 47135c987a37SChris Kirby 47143b2aab18SMatthew Ahrens error = dsl_destroy_snapshot(fullname, B_TRUE); 47155c987a37SChris Kirby if (error) { 47163b2aab18SMatthew Ahrens fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d", 47175c987a37SChris Kirby fullname, error); 47185c987a37SChris Kirby } 47195c987a37SChris Kirby 47203b2aab18SMatthew Ahrens error = dsl_destroy_head(clonename); 47215c987a37SChris Kirby if (error) 47223b2aab18SMatthew Ahrens fatal(0, "dsl_destroy_head(%s) = %d", clonename, error); 47235c987a37SChris Kirby 47245c987a37SChris Kirby error = dmu_objset_hold(fullname, FTAG, &origin); 47255c987a37SChris Kirby if (error != ENOENT) 47265c987a37SChris Kirby fatal(0, "dmu_objset_hold(%s) = %d", fullname, error); 47275c987a37SChris Kirby 47285c987a37SChris Kirby /* 47295c987a37SChris Kirby * Create snapshot, add temporary hold, verify that we can't 47305c987a37SChris Kirby * destroy a held snapshot, mark for deferred destroy, 47315c987a37SChris Kirby * release hold, verify snapshot was destroyed. 47325c987a37SChris Kirby */ 47334445fffbSMatthew Ahrens error = dmu_objset_snapshot_one(osname, snapname); 47345c987a37SChris Kirby if (error) { 47355c987a37SChris Kirby if (error == ENOSPC) { 47365c987a37SChris Kirby ztest_record_enospc("dmu_objset_snapshot"); 47375c987a37SChris Kirby goto out; 47385c987a37SChris Kirby } 47395c987a37SChris Kirby fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error); 47405c987a37SChris Kirby } 47415c987a37SChris Kirby 47423b2aab18SMatthew Ahrens holds = fnvlist_alloc(); 47433b2aab18SMatthew Ahrens fnvlist_add_string(holds, fullname, tag); 47443b2aab18SMatthew Ahrens error = dsl_dataset_user_hold(holds, 0, NULL); 47453b2aab18SMatthew Ahrens fnvlist_free(holds); 47463b2aab18SMatthew Ahrens 47475d7b4d43SMatthew Ahrens if (error == ENOSPC) { 47485d7b4d43SMatthew Ahrens ztest_record_enospc("dsl_dataset_user_hold"); 47495d7b4d43SMatthew Ahrens goto out; 47505d7b4d43SMatthew Ahrens } else if (error) { 47515d7b4d43SMatthew Ahrens fatal(0, "dsl_dataset_user_hold(%s, %s) = %u", 47525d7b4d43SMatthew Ahrens fullname, tag, error); 47535d7b4d43SMatthew Ahrens } 47545c987a37SChris Kirby 47553b2aab18SMatthew Ahrens error = dsl_destroy_snapshot(fullname, B_FALSE); 47565c987a37SChris Kirby if (error != EBUSY) { 47573b2aab18SMatthew Ahrens fatal(0, "dsl_destroy_snapshot(%s, B_FALSE) = %d", 47585c987a37SChris Kirby fullname, error); 47595c987a37SChris Kirby } 47605c987a37SChris Kirby 47613b2aab18SMatthew Ahrens error = dsl_destroy_snapshot(fullname, B_TRUE); 47625c987a37SChris Kirby if (error) { 47633b2aab18SMatthew Ahrens fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d", 47645c987a37SChris Kirby fullname, error); 47655c987a37SChris Kirby } 47665c987a37SChris Kirby 47673b2aab18SMatthew Ahrens error = user_release_one(fullname, tag); 47685c987a37SChris Kirby if (error) 4769a7a845e4SSteven Hartland fatal(0, "user_release_one(%s, %s) = %d", fullname, tag, error); 47705c987a37SChris Kirby 47713b2aab18SMatthew Ahrens VERIFY3U(dmu_objset_hold(fullname, FTAG, &origin), ==, ENOENT); 47725c987a37SChris Kirby 47735c987a37SChris Kirby out: 4774420dfc95SChris Siden (void) rw_unlock(&ztest_name_lock); 47755c987a37SChris Kirby } 47765c987a37SChris Kirby 47775c987a37SChris Kirby /* 4778fa9e4066Sahrens * Inject random faults into the on-disk data. 4779fa9e4066Sahrens */ 4780b24ab676SJeff Bonwick /* ARGSUSED */ 4781fa9e4066Sahrens void 4782b24ab676SJeff Bonwick ztest_fault_inject(ztest_ds_t *zd, uint64_t id) 4783fa9e4066Sahrens { 4784b24ab676SJeff Bonwick ztest_shared_t *zs = ztest_shared; 4785420dfc95SChris Siden spa_t *spa = ztest_spa; 4786fa9e4066Sahrens int fd; 4787fa9e4066Sahrens uint64_t offset; 47881195e687SMark J Musante uint64_t leaves; 4789fa9e4066Sahrens uint64_t bad = 0x1990c0ffeedecade; 4790fa9e4066Sahrens uint64_t top, leaf; 4791fa9e4066Sahrens char path0[MAXPATHLEN]; 4792fa9e4066Sahrens char pathrand[MAXPATHLEN]; 4793fa9e4066Sahrens size_t fsize; 4794f9eb9fdfSMatthew Ahrens int bshift = SPA_MAXBLOCKSHIFT + 2; /* don't scrog all labels */ 4795fa9e4066Sahrens int iters = 1000; 47961195e687SMark J Musante int maxfaults; 47971195e687SMark J Musante int mirror_save; 4798e14bb325SJeff Bonwick vdev_t *vd0 = NULL; 4799ea8dc4b6Seschrock uint64_t guid0 = 0; 48008f18d1faSGeorge Wilson boolean_t islog = B_FALSE; 4801ea8dc4b6Seschrock 4802420dfc95SChris Siden VERIFY(mutex_lock(&ztest_vdev_lock) == 0); 48031195e687SMark J Musante maxfaults = MAXFAULTS(); 4804420dfc95SChris Siden leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz; 48051195e687SMark J Musante mirror_save = zs->zs_mirrors; 4806420dfc95SChris Siden VERIFY(mutex_unlock(&ztest_vdev_lock) == 0); 48071195e687SMark J Musante 4808e14bb325SJeff Bonwick ASSERT(leaves >= 1); 4809fa9e4066Sahrens 4810fa9e4066Sahrens /* 48112c1e2b44SGeorge Wilson * Grab the name lock as reader. There are some operations 48122c1e2b44SGeorge Wilson * which don't like to have their vdevs changed while 48132c1e2b44SGeorge Wilson * they are in progress (i.e. spa_change_guid). Those 48142c1e2b44SGeorge Wilson * operations will have grabbed the name lock as writer. 48152c1e2b44SGeorge Wilson */ 48162c1e2b44SGeorge Wilson (void) rw_rdlock(&ztest_name_lock); 48172c1e2b44SGeorge Wilson 48182c1e2b44SGeorge Wilson /* 4819e14bb325SJeff Bonwick * We need SCL_STATE here because we're going to look at vd0->vdev_tsd. 4820fa9e4066Sahrens */ 4821e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 4822e14bb325SJeff Bonwick 4823e14bb325SJeff Bonwick if (ztest_random(2) == 0) { 4824e14bb325SJeff Bonwick /* 4825b24ab676SJeff Bonwick * Inject errors on a normal data device or slog device. 4826e14bb325SJeff Bonwick */ 4827b24ab676SJeff Bonwick top = ztest_random_vdev_top(spa, B_TRUE); 48281195e687SMark J Musante leaf = ztest_random(leaves) + zs->zs_splits; 4829fa9e4066Sahrens 4830fa9e4066Sahrens /* 4831e14bb325SJeff Bonwick * Generate paths to the first leaf in this top-level vdev, 4832fa9e4066Sahrens * and to the random leaf we selected. We'll induce transient 4833e14bb325SJeff Bonwick * write failures and random online/offline activity on leaf 0, 4834fa9e4066Sahrens * and we'll write random garbage to the randomly chosen leaf. 4835fa9e4066Sahrens */ 4836e14bb325SJeff Bonwick (void) snprintf(path0, sizeof (path0), ztest_dev_template, 4837420dfc95SChris Siden ztest_opts.zo_dir, ztest_opts.zo_pool, 4838420dfc95SChris Siden top * leaves + zs->zs_splits); 4839e14bb325SJeff Bonwick (void) snprintf(pathrand, sizeof (pathrand), ztest_dev_template, 4840420dfc95SChris Siden ztest_opts.zo_dir, ztest_opts.zo_pool, 4841420dfc95SChris Siden top * leaves + leaf); 4842e14bb325SJeff Bonwick 4843e14bb325SJeff Bonwick vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0); 48448f18d1faSGeorge Wilson if (vd0 != NULL && vd0->vdev_top->vdev_islog) 48458f18d1faSGeorge Wilson islog = B_TRUE; 48468f18d1faSGeorge Wilson 48472c1e2b44SGeorge Wilson /* 48482c1e2b44SGeorge Wilson * If the top-level vdev needs to be resilvered 48492c1e2b44SGeorge Wilson * then we only allow faults on the device that is 48502c1e2b44SGeorge Wilson * resilvering. 48512c1e2b44SGeorge Wilson */ 48522c1e2b44SGeorge Wilson if (vd0 != NULL && maxfaults != 1 && 48532c1e2b44SGeorge Wilson (!vdev_resilver_needed(vd0->vdev_top, NULL, NULL) || 4854b4952e17SGeorge Wilson vd0->vdev_resilver_txg != 0)) { 4855e14bb325SJeff Bonwick /* 4856e14bb325SJeff Bonwick * Make vd0 explicitly claim to be unreadable, 4857e14bb325SJeff Bonwick * or unwriteable, or reach behind its back 4858e14bb325SJeff Bonwick * and close the underlying fd. We can do this if 4859e14bb325SJeff Bonwick * maxfaults == 0 because we'll fail and reexecute, 4860e14bb325SJeff Bonwick * and we can do it if maxfaults >= 2 because we'll 4861e14bb325SJeff Bonwick * have enough redundancy. If maxfaults == 1, the 4862e14bb325SJeff Bonwick * combination of this with injection of random data 4863e14bb325SJeff Bonwick * corruption below exceeds the pool's fault tolerance. 4864e14bb325SJeff Bonwick */ 4865e14bb325SJeff Bonwick vdev_file_t *vf = vd0->vdev_tsd; 4866e14bb325SJeff Bonwick 4867e14bb325SJeff Bonwick if (vf != NULL && ztest_random(3) == 0) { 4868e14bb325SJeff Bonwick (void) close(vf->vf_vnode->v_fd); 4869e14bb325SJeff Bonwick vf->vf_vnode->v_fd = -1; 4870e14bb325SJeff Bonwick } else if (ztest_random(2) == 0) { 4871e14bb325SJeff Bonwick vd0->vdev_cant_read = B_TRUE; 4872e14bb325SJeff Bonwick } else { 4873e14bb325SJeff Bonwick vd0->vdev_cant_write = B_TRUE; 4874e14bb325SJeff Bonwick } 4875e14bb325SJeff Bonwick guid0 = vd0->vdev_guid; 4876e14bb325SJeff Bonwick } 4877e14bb325SJeff Bonwick } else { 4878e14bb325SJeff Bonwick /* 4879e14bb325SJeff Bonwick * Inject errors on an l2cache device. 4880e14bb325SJeff Bonwick */ 4881e14bb325SJeff Bonwick spa_aux_vdev_t *sav = &spa->spa_l2cache; 4882e14bb325SJeff Bonwick 4883e14bb325SJeff Bonwick if (sav->sav_count == 0) { 4884e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 48852c1e2b44SGeorge Wilson (void) rw_unlock(&ztest_name_lock); 4886e14bb325SJeff Bonwick return; 4887e14bb325SJeff Bonwick } 4888e14bb325SJeff Bonwick vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)]; 4889e14bb325SJeff Bonwick guid0 = vd0->vdev_guid; 4890e14bb325SJeff Bonwick (void) strcpy(path0, vd0->vdev_path); 4891e14bb325SJeff Bonwick (void) strcpy(pathrand, vd0->vdev_path); 4892e14bb325SJeff Bonwick 4893e14bb325SJeff Bonwick leaf = 0; 4894e14bb325SJeff Bonwick leaves = 1; 4895e14bb325SJeff Bonwick maxfaults = INT_MAX; /* no limit on cache devices */ 4896e14bb325SJeff Bonwick } 4897fa9e4066Sahrens 4898e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 48992c1e2b44SGeorge Wilson (void) rw_unlock(&ztest_name_lock); 4900fa9e4066Sahrens 4901ea8dc4b6Seschrock /* 49028f18d1faSGeorge Wilson * If we can tolerate two or more faults, or we're dealing 49038f18d1faSGeorge Wilson * with a slog, randomly online/offline vd0. 4904ea8dc4b6Seschrock */ 49058f18d1faSGeorge Wilson if ((maxfaults >= 2 || islog) && guid0 != 0) { 49068ad4d6ddSJeff Bonwick if (ztest_random(10) < 6) { 49078ad4d6ddSJeff Bonwick int flags = (ztest_random(2) == 0 ? 49088ad4d6ddSJeff Bonwick ZFS_OFFLINE_TEMPORARY : 0); 49098f18d1faSGeorge Wilson 49108f18d1faSGeorge Wilson /* 49118f18d1faSGeorge Wilson * We have to grab the zs_name_lock as writer to 49128f18d1faSGeorge Wilson * prevent a race between offlining a slog and 49138f18d1faSGeorge Wilson * destroying a dataset. Offlining the slog will 49148f18d1faSGeorge Wilson * grab a reference on the dataset which may cause 49158f18d1faSGeorge Wilson * dmu_objset_destroy() to fail with EBUSY thus 49168f18d1faSGeorge Wilson * leaving the dataset in an inconsistent state. 49178f18d1faSGeorge Wilson */ 49188f18d1faSGeorge Wilson if (islog) 4919420dfc95SChris Siden (void) rw_wrlock(&ztest_name_lock); 49208f18d1faSGeorge Wilson 49218ad4d6ddSJeff Bonwick VERIFY(vdev_offline(spa, guid0, flags) != EBUSY); 49228f18d1faSGeorge Wilson 49238f18d1faSGeorge Wilson if (islog) 4924420dfc95SChris Siden (void) rw_unlock(&ztest_name_lock); 49258ad4d6ddSJeff Bonwick } else { 49269253d63dSGeorge Wilson /* 49279253d63dSGeorge Wilson * Ideally we would like to be able to randomly 49289253d63dSGeorge Wilson * call vdev_[on|off]line without holding locks 49299253d63dSGeorge Wilson * to force unpredictable failures but the side 49309253d63dSGeorge Wilson * effects of vdev_[on|off]line prevent us from 49319253d63dSGeorge Wilson * doing so. We grab the ztest_vdev_lock here to 49329253d63dSGeorge Wilson * prevent a race between injection testing and 49339253d63dSGeorge Wilson * aux_vdev removal. 49349253d63dSGeorge Wilson */ 49359253d63dSGeorge Wilson VERIFY(mutex_lock(&ztest_vdev_lock) == 0); 49368ad4d6ddSJeff Bonwick (void) vdev_online(spa, guid0, 0, NULL); 49379253d63dSGeorge Wilson VERIFY(mutex_unlock(&ztest_vdev_lock) == 0); 49388ad4d6ddSJeff Bonwick } 4939fa9e4066Sahrens } 4940fa9e4066Sahrens 49418f18d1faSGeorge Wilson if (maxfaults == 0) 49428f18d1faSGeorge Wilson return; 49438f18d1faSGeorge Wilson 4944fa9e4066Sahrens /* 4945ea8dc4b6Seschrock * We have at least single-fault tolerance, so inject data corruption. 4946fa9e4066Sahrens */ 4947fa9e4066Sahrens fd = open(pathrand, O_RDWR); 4948fa9e4066Sahrens 4949fa9e4066Sahrens if (fd == -1) /* we hit a gap in the device namespace */ 4950fa9e4066Sahrens return; 4951fa9e4066Sahrens 4952fa9e4066Sahrens fsize = lseek(fd, 0, SEEK_END); 4953fa9e4066Sahrens 4954fa9e4066Sahrens while (--iters != 0) { 4955f9eb9fdfSMatthew Ahrens /* 4956f9eb9fdfSMatthew Ahrens * The offset must be chosen carefully to ensure that 4957f9eb9fdfSMatthew Ahrens * we do not inject a given logical block with errors 4958f9eb9fdfSMatthew Ahrens * on two different leaf devices, because ZFS can not 4959f9eb9fdfSMatthew Ahrens * tolerate that (if maxfaults==1). 4960f9eb9fdfSMatthew Ahrens * 4961f9eb9fdfSMatthew Ahrens * We divide each leaf into chunks of size 4962f9eb9fdfSMatthew Ahrens * (# leaves * SPA_MAXBLOCKSIZE * 4). Within each chunk 4963f9eb9fdfSMatthew Ahrens * there is a series of ranges to which we can inject errors. 4964f9eb9fdfSMatthew Ahrens * Each range can accept errors on only a single leaf vdev. 4965f9eb9fdfSMatthew Ahrens * The error injection ranges are separated by ranges 4966f9eb9fdfSMatthew Ahrens * which we will not inject errors on any device (DMZs). 4967f9eb9fdfSMatthew Ahrens * Each DMZ must be large enough such that a single block 4968f9eb9fdfSMatthew Ahrens * can not straddle it, so that a single block can not be 4969f9eb9fdfSMatthew Ahrens * a target in two different injection ranges (on different 4970f9eb9fdfSMatthew Ahrens * leaf vdevs). 4971f9eb9fdfSMatthew Ahrens * 4972f9eb9fdfSMatthew Ahrens * For example, with 3 leaves, each chunk looks like: 4973f9eb9fdfSMatthew Ahrens * 0 to 32M: injection range for leaf 0 4974f9eb9fdfSMatthew Ahrens * 32M to 64M: DMZ - no injection allowed 4975f9eb9fdfSMatthew Ahrens * 64M to 96M: injection range for leaf 1 4976f9eb9fdfSMatthew Ahrens * 96M to 128M: DMZ - no injection allowed 4977f9eb9fdfSMatthew Ahrens * 128M to 160M: injection range for leaf 2 4978f9eb9fdfSMatthew Ahrens * 160M to 192M: DMZ - no injection allowed 4979f9eb9fdfSMatthew Ahrens */ 4980fa9e4066Sahrens offset = ztest_random(fsize / (leaves << bshift)) * 4981fa9e4066Sahrens (leaves << bshift) + (leaf << bshift) + 4982fa9e4066Sahrens (ztest_random(1ULL << (bshift - 1)) & -8ULL); 4983fa9e4066Sahrens 4984fa9e4066Sahrens if (offset >= fsize) 4985fa9e4066Sahrens continue; 4986fa9e4066Sahrens 4987420dfc95SChris Siden VERIFY(mutex_lock(&ztest_vdev_lock) == 0); 49881195e687SMark J Musante if (mirror_save != zs->zs_mirrors) { 4989420dfc95SChris Siden VERIFY(mutex_unlock(&ztest_vdev_lock) == 0); 49901195e687SMark J Musante (void) close(fd); 49911195e687SMark J Musante return; 49921195e687SMark J Musante } 4993fa9e4066Sahrens 4994fa9e4066Sahrens if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad)) 4995fa9e4066Sahrens fatal(1, "can't inject bad word at 0x%llx in %s", 4996fa9e4066Sahrens offset, pathrand); 49971195e687SMark J Musante 4998420dfc95SChris Siden VERIFY(mutex_unlock(&ztest_vdev_lock) == 0); 49991195e687SMark J Musante 5000420dfc95SChris Siden if (ztest_opts.zo_verbose >= 7) 50011195e687SMark J Musante (void) printf("injected bad word into %s," 50021195e687SMark J Musante " offset 0x%llx\n", pathrand, (u_longlong_t)offset); 5003fa9e4066Sahrens } 5004fa9e4066Sahrens 5005fa9e4066Sahrens (void) close(fd); 5006fa9e4066Sahrens } 5007fa9e4066Sahrens 5008fa9e4066Sahrens /* 5009b24ab676SJeff Bonwick * Verify that DDT repair works as expected. 5010fa9e4066Sahrens */ 5011fa9e4066Sahrens void 5012b24ab676SJeff Bonwick ztest_ddt_repair(ztest_ds_t *zd, uint64_t id) 5013fa9e4066Sahrens { 5014b24ab676SJeff Bonwick ztest_shared_t *zs = ztest_shared; 5015420dfc95SChris Siden spa_t *spa = ztest_spa; 5016b24ab676SJeff Bonwick objset_t *os = zd->zd_os; 5017b24ab676SJeff Bonwick ztest_od_t od[1]; 5018b24ab676SJeff Bonwick uint64_t object, blocksize, txg, pattern, psize; 5019b24ab676SJeff Bonwick enum zio_checksum checksum = spa_dedup_checksum(spa); 5020b24ab676SJeff Bonwick dmu_buf_t *db; 5021b24ab676SJeff Bonwick dmu_tx_t *tx; 5022b24ab676SJeff Bonwick void *buf; 5023b24ab676SJeff Bonwick blkptr_t blk; 5024b24ab676SJeff Bonwick int copies = 2 * ZIO_DEDUPDITTO_MIN; 5025b24ab676SJeff Bonwick 5026b24ab676SJeff Bonwick blocksize = ztest_random_blocksize(); 5027b24ab676SJeff Bonwick blocksize = MIN(blocksize, 2048); /* because we write so many */ 5028b24ab676SJeff Bonwick 5029b24ab676SJeff Bonwick ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0); 5030b24ab676SJeff Bonwick 5031b24ab676SJeff Bonwick if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0) 5032b24ab676SJeff Bonwick return; 5033b24ab676SJeff Bonwick 5034b24ab676SJeff Bonwick /* 5035b24ab676SJeff Bonwick * Take the name lock as writer to prevent anyone else from changing 5036b24ab676SJeff Bonwick * the pool and dataset properies we need to maintain during this test. 5037b24ab676SJeff Bonwick */ 5038420dfc95SChris Siden (void) rw_wrlock(&ztest_name_lock); 5039b24ab676SJeff Bonwick 5040b24ab676SJeff Bonwick if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum, 5041b24ab676SJeff Bonwick B_FALSE) != 0 || 5042b24ab676SJeff Bonwick ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1, 5043b24ab676SJeff Bonwick B_FALSE) != 0) { 5044420dfc95SChris Siden (void) rw_unlock(&ztest_name_lock); 5045b24ab676SJeff Bonwick return; 5046b24ab676SJeff Bonwick } 5047b24ab676SJeff Bonwick 5048b24ab676SJeff Bonwick object = od[0].od_object; 5049b24ab676SJeff Bonwick blocksize = od[0].od_blocksize; 5050e9103aaeSGarrett D'Amore pattern = zs->zs_guid ^ dmu_objset_fsid_guid(os); 5051b24ab676SJeff Bonwick 5052b24ab676SJeff Bonwick ASSERT(object != 0); 5053b24ab676SJeff Bonwick 5054b24ab676SJeff Bonwick tx = dmu_tx_create(os); 5055b24ab676SJeff Bonwick dmu_tx_hold_write(tx, object, 0, copies * blocksize); 5056b24ab676SJeff Bonwick txg = ztest_tx_assign(tx, TXG_WAIT, FTAG); 5057b24ab676SJeff Bonwick if (txg == 0) { 5058420dfc95SChris Siden (void) rw_unlock(&ztest_name_lock); 5059b24ab676SJeff Bonwick return; 5060b24ab676SJeff Bonwick } 5061b24ab676SJeff Bonwick 5062b24ab676SJeff Bonwick /* 5063b24ab676SJeff Bonwick * Write all the copies of our block. 5064b24ab676SJeff Bonwick */ 5065b24ab676SJeff Bonwick for (int i = 0; i < copies; i++) { 5066b24ab676SJeff Bonwick uint64_t offset = i * blocksize; 50673b2aab18SMatthew Ahrens int error = dmu_buf_hold(os, object, offset, FTAG, &db, 50683b2aab18SMatthew Ahrens DMU_READ_NO_PREFETCH); 50693b2aab18SMatthew Ahrens if (error != 0) { 50703b2aab18SMatthew Ahrens fatal(B_FALSE, "dmu_buf_hold(%p, %llu, %llu) = %u", 50713b2aab18SMatthew Ahrens os, (long long)object, (long long) offset, error); 50723b2aab18SMatthew Ahrens } 5073b24ab676SJeff Bonwick ASSERT(db->db_offset == offset); 5074b24ab676SJeff Bonwick ASSERT(db->db_size == blocksize); 5075b24ab676SJeff Bonwick ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) || 5076b24ab676SJeff Bonwick ztest_pattern_match(db->db_data, db->db_size, 0ULL)); 5077b24ab676SJeff Bonwick dmu_buf_will_fill(db, tx); 5078b24ab676SJeff Bonwick ztest_pattern_set(db->db_data, db->db_size, pattern); 5079b24ab676SJeff Bonwick dmu_buf_rele(db, FTAG); 5080b24ab676SJeff Bonwick } 5081b24ab676SJeff Bonwick 5082b24ab676SJeff Bonwick dmu_tx_commit(tx); 5083b24ab676SJeff Bonwick txg_wait_synced(spa_get_dsl(spa), txg); 5084b24ab676SJeff Bonwick 5085b24ab676SJeff Bonwick /* 5086b24ab676SJeff Bonwick * Find out what block we got. 5087b24ab676SJeff Bonwick */ 508880901aeaSGeorge Wilson VERIFY0(dmu_buf_hold(os, object, 0, FTAG, &db, 508980901aeaSGeorge Wilson DMU_READ_NO_PREFETCH)); 5090b24ab676SJeff Bonwick blk = *((dmu_buf_impl_t *)db)->db_blkptr; 5091b24ab676SJeff Bonwick dmu_buf_rele(db, FTAG); 5092b24ab676SJeff Bonwick 5093b24ab676SJeff Bonwick /* 5094b24ab676SJeff Bonwick * Damage the block. Dedup-ditto will save us when we read it later. 5095b24ab676SJeff Bonwick */ 5096b24ab676SJeff Bonwick psize = BP_GET_PSIZE(&blk); 5097b24ab676SJeff Bonwick buf = zio_buf_alloc(psize); 5098b24ab676SJeff Bonwick ztest_pattern_set(buf, psize, ~pattern); 5099b24ab676SJeff Bonwick 5100b24ab676SJeff Bonwick (void) zio_wait(zio_rewrite(NULL, spa, 0, &blk, 5101b24ab676SJeff Bonwick buf, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE, 5102b24ab676SJeff Bonwick ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL)); 5103b24ab676SJeff Bonwick 5104b24ab676SJeff Bonwick zio_buf_free(buf, psize); 5105b24ab676SJeff Bonwick 5106420dfc95SChris Siden (void) rw_unlock(&ztest_name_lock); 5107b24ab676SJeff Bonwick } 5108b24ab676SJeff Bonwick 5109b24ab676SJeff Bonwick /* 5110b24ab676SJeff Bonwick * Scrub the pool. 5111b24ab676SJeff Bonwick */ 5112b24ab676SJeff Bonwick /* ARGSUSED */ 5113b24ab676SJeff Bonwick void 5114b24ab676SJeff Bonwick ztest_scrub(ztest_ds_t *zd, uint64_t id) 5115b24ab676SJeff Bonwick { 5116420dfc95SChris Siden spa_t *spa = ztest_spa; 5117fa9e4066Sahrens 51183f9d6ad7SLin Ling (void) spa_scan(spa, POOL_SCAN_SCRUB); 5119b24ab676SJeff Bonwick (void) poll(NULL, 0, 100); /* wait a moment, then force a restart */ 51203f9d6ad7SLin Ling (void) spa_scan(spa, POOL_SCAN_SCRUB); 5121fa9e4066Sahrens } 5122fa9e4066Sahrens 5123fa9e4066Sahrens /* 5124e9103aaeSGarrett D'Amore * Change the guid for the pool. 5125e9103aaeSGarrett D'Amore */ 5126e9103aaeSGarrett D'Amore /* ARGSUSED */ 5127e9103aaeSGarrett D'Amore void 5128e9103aaeSGarrett D'Amore ztest_reguid(ztest_ds_t *zd, uint64_t id) 5129e9103aaeSGarrett D'Amore { 5130420dfc95SChris Siden spa_t *spa = ztest_spa; 5131e9103aaeSGarrett D'Amore uint64_t orig, load; 5132dfbb9432SGeorge Wilson int error; 5133e9103aaeSGarrett D'Amore 5134e9103aaeSGarrett D'Amore orig = spa_guid(spa); 5135e9103aaeSGarrett D'Amore load = spa_load_guid(spa); 5136dfbb9432SGeorge Wilson 5137dfbb9432SGeorge Wilson (void) rw_wrlock(&ztest_name_lock); 5138dfbb9432SGeorge Wilson error = spa_change_guid(spa); 5139dfbb9432SGeorge Wilson (void) rw_unlock(&ztest_name_lock); 5140dfbb9432SGeorge Wilson 5141dfbb9432SGeorge Wilson if (error != 0) 5142e9103aaeSGarrett D'Amore return; 5143e9103aaeSGarrett D'Amore 514425345e46SGeorge Wilson if (ztest_opts.zo_verbose >= 4) { 5145e9103aaeSGarrett D'Amore (void) printf("Changed guid old %llu -> %llu\n", 5146e9103aaeSGarrett D'Amore (u_longlong_t)orig, (u_longlong_t)spa_guid(spa)); 5147e9103aaeSGarrett D'Amore } 5148e9103aaeSGarrett D'Amore 5149e9103aaeSGarrett D'Amore VERIFY3U(orig, !=, spa_guid(spa)); 5150e9103aaeSGarrett D'Amore VERIFY3U(load, ==, spa_load_guid(spa)); 5151e9103aaeSGarrett D'Amore } 5152e9103aaeSGarrett D'Amore 5153e9103aaeSGarrett D'Amore /* 5154fa9e4066Sahrens * Rename the pool to a different name and then rename it back. 5155fa9e4066Sahrens */ 5156b24ab676SJeff Bonwick /* ARGSUSED */ 5157fa9e4066Sahrens void 5158b24ab676SJeff Bonwick ztest_spa_rename(ztest_ds_t *zd, uint64_t id) 5159fa9e4066Sahrens { 5160fa9e4066Sahrens char *oldname, *newname; 5161fa9e4066Sahrens spa_t *spa; 5162fa9e4066Sahrens 5163420dfc95SChris Siden (void) rw_wrlock(&ztest_name_lock); 5164fa9e4066Sahrens 5165420dfc95SChris Siden oldname = ztest_opts.zo_pool; 5166fa9e4066Sahrens newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL); 5167fa9e4066Sahrens (void) strcpy(newname, oldname); 5168fa9e4066Sahrens (void) strcat(newname, "_tmp"); 5169fa9e4066Sahrens 5170fa9e4066Sahrens /* 5171fa9e4066Sahrens * Do the rename 5172fa9e4066Sahrens */ 5173b420f3adSRichard Lowe VERIFY3U(0, ==, spa_rename(oldname, newname)); 5174fa9e4066Sahrens 5175fa9e4066Sahrens /* 5176fa9e4066Sahrens * Try to open it under the old name, which shouldn't exist 5177fa9e4066Sahrens */ 5178b24ab676SJeff Bonwick VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG)); 5179fa9e4066Sahrens 5180fa9e4066Sahrens /* 5181fa9e4066Sahrens * Open it under the new name and make sure it's still the same spa_t. 5182fa9e4066Sahrens */ 5183b420f3adSRichard Lowe VERIFY3U(0, ==, spa_open(newname, &spa, FTAG)); 5184fa9e4066Sahrens 5185420dfc95SChris Siden ASSERT(spa == ztest_spa); 5186fa9e4066Sahrens spa_close(spa, FTAG); 5187fa9e4066Sahrens 5188fa9e4066Sahrens /* 5189fa9e4066Sahrens * Rename it back to the original 5190fa9e4066Sahrens */ 5191b420f3adSRichard Lowe VERIFY3U(0, ==, spa_rename(newname, oldname)); 5192fa9e4066Sahrens 5193fa9e4066Sahrens /* 5194fa9e4066Sahrens * Make sure it can still be opened 5195fa9e4066Sahrens */ 5196b420f3adSRichard Lowe VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG)); 5197fa9e4066Sahrens 5198420dfc95SChris Siden ASSERT(spa == ztest_spa); 5199fa9e4066Sahrens spa_close(spa, FTAG); 5200fa9e4066Sahrens 5201fa9e4066Sahrens umem_free(newname, strlen(newname) + 1); 5202fa9e4066Sahrens 5203420dfc95SChris Siden (void) rw_unlock(&ztest_name_lock); 5204fa9e4066Sahrens } 5205fa9e4066Sahrens 5206fa9e4066Sahrens /* 5207b24ab676SJeff Bonwick * Verify pool integrity by running zdb. 5208fa9e4066Sahrens */ 5209fa9e4066Sahrens static void 5210b24ab676SJeff Bonwick ztest_run_zdb(char *pool) 5211fa9e4066Sahrens { 5212fa9e4066Sahrens int status; 5213fa9e4066Sahrens char zdb[MAXPATHLEN + MAXNAMELEN + 20]; 5214fa9e4066Sahrens char zbuf[1024]; 5215fa9e4066Sahrens char *bin; 52168654d025Sperrin char *ztest; 52178654d025Sperrin char *isa; 52188654d025Sperrin int isalen; 5219fa9e4066Sahrens FILE *fp; 5220fa9e4066Sahrens 5221fa9e4066Sahrens (void) realpath(getexecname(), zdb); 5222fa9e4066Sahrens 5223fa9e4066Sahrens /* zdb lives in /usr/sbin, while ztest lives in /usr/bin */ 5224fa9e4066Sahrens bin = strstr(zdb, "/usr/bin/"); 52258654d025Sperrin ztest = strstr(bin, "/ztest"); 52268654d025Sperrin isa = bin + 8; 52278654d025Sperrin isalen = ztest - isa; 52288654d025Sperrin isa = strdup(isa); 5229fa9e4066Sahrens /* LINTED */ 52303a57275aSck153898 (void) sprintf(bin, 52315d7b4d43SMatthew Ahrens "/usr/sbin%.*s/zdb -bcc%s%s -d -U %s %s", 52328654d025Sperrin isalen, 52338654d025Sperrin isa, 5234420dfc95SChris Siden ztest_opts.zo_verbose >= 3 ? "s" : "", 5235420dfc95SChris Siden ztest_opts.zo_verbose >= 4 ? "v" : "", 5236f0ba89beSJeff Bonwick spa_config_path, 523788b7b0f2SMatthew Ahrens pool); 52388654d025Sperrin free(isa); 5239fa9e4066Sahrens 5240420dfc95SChris Siden if (ztest_opts.zo_verbose >= 5) 5241fa9e4066Sahrens (void) printf("Executing %s\n", strstr(zdb, "zdb ")); 5242fa9e4066Sahrens 5243fa9e4066Sahrens fp = popen(zdb, "r"); 5244fa9e4066Sahrens 5245fa9e4066Sahrens while (fgets(zbuf, sizeof (zbuf), fp) != NULL) 5246420dfc95SChris Siden if (ztest_opts.zo_verbose >= 3) 5247fa9e4066Sahrens (void) printf("%s", zbuf); 5248fa9e4066Sahrens 5249fa9e4066Sahrens status = pclose(fp); 5250fa9e4066Sahrens 5251fa9e4066Sahrens if (status == 0) 5252fa9e4066Sahrens return; 5253fa9e4066Sahrens 5254fa9e4066Sahrens ztest_dump_core = 0; 5255fa9e4066Sahrens if (WIFEXITED(status)) 5256fa9e4066Sahrens fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status)); 5257fa9e4066Sahrens else 5258fa9e4066Sahrens fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status)); 5259fa9e4066Sahrens } 5260fa9e4066Sahrens 5261fa9e4066Sahrens static void 5262fa9e4066Sahrens ztest_walk_pool_directory(char *header) 5263fa9e4066Sahrens { 5264fa9e4066Sahrens spa_t *spa = NULL; 5265fa9e4066Sahrens 5266420dfc95SChris Siden if (ztest_opts.zo_verbose >= 6) 5267fa9e4066Sahrens (void) printf("%s\n", header); 5268fa9e4066Sahrens 5269fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 5270fa9e4066Sahrens while ((spa = spa_next(spa)) != NULL) 5271420dfc95SChris Siden if (ztest_opts.zo_verbose >= 6) 5272fa9e4066Sahrens (void) printf("\t%s\n", spa_name(spa)); 5273fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 5274fa9e4066Sahrens } 5275fa9e4066Sahrens 5276fa9e4066Sahrens static void 5277fa9e4066Sahrens ztest_spa_import_export(char *oldname, char *newname) 5278fa9e4066Sahrens { 52798ad4d6ddSJeff Bonwick nvlist_t *config, *newconfig; 5280fa9e4066Sahrens uint64_t pool_guid; 5281fa9e4066Sahrens spa_t *spa; 52823b2aab18SMatthew Ahrens int error; 5283fa9e4066Sahrens 5284420dfc95SChris Siden if (ztest_opts.zo_verbose >= 4) { 5285fa9e4066Sahrens (void) printf("import/export: old = %s, new = %s\n", 5286fa9e4066Sahrens oldname, newname); 5287fa9e4066Sahrens } 5288fa9e4066Sahrens 5289fa9e4066Sahrens /* 5290fa9e4066Sahrens * Clean up from previous runs. 5291fa9e4066Sahrens */ 5292fa9e4066Sahrens (void) spa_destroy(newname); 5293fa9e4066Sahrens 5294fa9e4066Sahrens /* 5295fa9e4066Sahrens * Get the pool's configuration and guid. 5296fa9e4066Sahrens */ 5297b420f3adSRichard Lowe VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG)); 5298fa9e4066Sahrens 52998ad4d6ddSJeff Bonwick /* 53008ad4d6ddSJeff Bonwick * Kick off a scrub to tickle scrub/export races. 53018ad4d6ddSJeff Bonwick */ 53028ad4d6ddSJeff Bonwick if (ztest_random(2) == 0) 53033f9d6ad7SLin Ling (void) spa_scan(spa, POOL_SCAN_SCRUB); 53048ad4d6ddSJeff Bonwick 5305fa9e4066Sahrens pool_guid = spa_guid(spa); 5306fa9e4066Sahrens spa_close(spa, FTAG); 5307fa9e4066Sahrens 5308fa9e4066Sahrens ztest_walk_pool_directory("pools before export"); 5309fa9e4066Sahrens 5310fa9e4066Sahrens /* 5311fa9e4066Sahrens * Export it. 5312fa9e4066Sahrens */ 5313b420f3adSRichard Lowe VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE)); 5314fa9e4066Sahrens 5315fa9e4066Sahrens ztest_walk_pool_directory("pools after export"); 5316fa9e4066Sahrens 5317fa9e4066Sahrens /* 53188ad4d6ddSJeff Bonwick * Try to import it. 53198ad4d6ddSJeff Bonwick */ 53208ad4d6ddSJeff Bonwick newconfig = spa_tryimport(config); 53218ad4d6ddSJeff Bonwick ASSERT(newconfig != NULL); 53228ad4d6ddSJeff Bonwick nvlist_free(newconfig); 53238ad4d6ddSJeff Bonwick 53248ad4d6ddSJeff Bonwick /* 5325fa9e4066Sahrens * Import it under the new name. 5326fa9e4066Sahrens */ 53273b2aab18SMatthew Ahrens error = spa_import(newname, config, NULL, 0); 53283b2aab18SMatthew Ahrens if (error != 0) { 53293b2aab18SMatthew Ahrens dump_nvlist(config, 0); 53303b2aab18SMatthew Ahrens fatal(B_FALSE, "couldn't import pool %s as %s: error %u", 53313b2aab18SMatthew Ahrens oldname, newname, error); 53323b2aab18SMatthew Ahrens } 5333fa9e4066Sahrens 5334fa9e4066Sahrens ztest_walk_pool_directory("pools after import"); 5335fa9e4066Sahrens 5336fa9e4066Sahrens /* 5337fa9e4066Sahrens * Try to import it again -- should fail with EEXIST. 5338fa9e4066Sahrens */ 53394b964adaSGeorge Wilson VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL, 0)); 5340fa9e4066Sahrens 5341fa9e4066Sahrens /* 5342fa9e4066Sahrens * Try to import it under a different name -- should fail with EEXIST. 5343fa9e4066Sahrens */ 53444b964adaSGeorge Wilson VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL, 0)); 5345fa9e4066Sahrens 5346fa9e4066Sahrens /* 5347fa9e4066Sahrens * Verify that the pool is no longer visible under the old name. 5348fa9e4066Sahrens */ 5349b24ab676SJeff Bonwick VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG)); 5350fa9e4066Sahrens 5351fa9e4066Sahrens /* 5352fa9e4066Sahrens * Verify that we can open and close the pool using the new name. 5353fa9e4066Sahrens */ 5354b420f3adSRichard Lowe VERIFY3U(0, ==, spa_open(newname, &spa, FTAG)); 5355fa9e4066Sahrens ASSERT(pool_guid == spa_guid(spa)); 5356fa9e4066Sahrens spa_close(spa, FTAG); 5357fa9e4066Sahrens 5358fa9e4066Sahrens nvlist_free(config); 5359fa9e4066Sahrens } 5360fa9e4066Sahrens 53618ad4d6ddSJeff Bonwick static void 53628ad4d6ddSJeff Bonwick ztest_resume(spa_t *spa) 53638ad4d6ddSJeff Bonwick { 5364420dfc95SChris Siden if (spa_suspended(spa) && ztest_opts.zo_verbose >= 6) 5365b24ab676SJeff Bonwick (void) printf("resuming from suspended state\n"); 53668f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 53678ad4d6ddSJeff Bonwick vdev_clear(spa, NULL); 53688ad4d6ddSJeff Bonwick (void) spa_vdev_state_exit(spa, NULL, 0); 536954d692b7SGeorge Wilson (void) zio_resume(spa); 53708ad4d6ddSJeff Bonwick } 53718ad4d6ddSJeff Bonwick 53720a4e9518Sgw25295 static void * 53738ad4d6ddSJeff Bonwick ztest_resume_thread(void *arg) 53740a4e9518Sgw25295 { 5375e14bb325SJeff Bonwick spa_t *spa = arg; 53760a4e9518Sgw25295 53770a4e9518Sgw25295 while (!ztest_exiting) { 5378b24ab676SJeff Bonwick if (spa_suspended(spa)) 53798ad4d6ddSJeff Bonwick ztest_resume(spa); 5380b24ab676SJeff Bonwick (void) poll(NULL, 0, 100); 53810a4e9518Sgw25295 } 53820a4e9518Sgw25295 return (NULL); 53830a4e9518Sgw25295 } 53840a4e9518Sgw25295 5385fa9e4066Sahrens static void * 5386b24ab676SJeff Bonwick ztest_deadman_thread(void *arg) 5387fa9e4066Sahrens { 5388b24ab676SJeff Bonwick ztest_shared_t *zs = arg; 53892c1e2b44SGeorge Wilson spa_t *spa = ztest_spa; 53902c1e2b44SGeorge Wilson hrtime_t delta, total = 0; 5391fa9e4066Sahrens 53922c1e2b44SGeorge Wilson for (;;) { 539369962b56SMatthew Ahrens delta = zs->zs_thread_stop - zs->zs_thread_start + 539469962b56SMatthew Ahrens MSEC2NSEC(zfs_deadman_synctime_ms); 5395b24ab676SJeff Bonwick 539669962b56SMatthew Ahrens (void) poll(NULL, 0, (int)NSEC2MSEC(delta)); 5397b24ab676SJeff Bonwick 53982c1e2b44SGeorge Wilson /* 53992c1e2b44SGeorge Wilson * If the pool is suspended then fail immediately. Otherwise, 54002c1e2b44SGeorge Wilson * check to see if the pool is making any progress. If 54012c1e2b44SGeorge Wilson * vdev_deadman() discovers that there hasn't been any recent 54022c1e2b44SGeorge Wilson * I/Os then it will end up aborting the tests. 54032c1e2b44SGeorge Wilson */ 54040713e232SGeorge Wilson if (spa_suspended(spa) || spa->spa_root_vdev == NULL) { 54052c1e2b44SGeorge Wilson fatal(0, "aborting test after %llu seconds because " 54062c1e2b44SGeorge Wilson "pool has transitioned to a suspended state.", 540769962b56SMatthew Ahrens zfs_deadman_synctime_ms / 1000); 5408b24ab676SJeff Bonwick return (NULL); 5409fa9e4066Sahrens } 54102c1e2b44SGeorge Wilson vdev_deadman(spa->spa_root_vdev); 54112c1e2b44SGeorge Wilson 541269962b56SMatthew Ahrens total += zfs_deadman_synctime_ms/1000; 54132c1e2b44SGeorge Wilson (void) printf("ztest has been running for %lld seconds\n", 54142c1e2b44SGeorge Wilson total); 54152c1e2b44SGeorge Wilson } 54162c1e2b44SGeorge Wilson } 5417fa9e4066Sahrens 5418b24ab676SJeff Bonwick static void 5419420dfc95SChris Siden ztest_execute(int test, ztest_info_t *zi, uint64_t id) 5420b24ab676SJeff Bonwick { 5421420dfc95SChris Siden ztest_ds_t *zd = &ztest_ds[id % ztest_opts.zo_datasets]; 5422420dfc95SChris Siden ztest_shared_callstate_t *zc = ZTEST_GET_SHARED_CALLSTATE(test); 5423b24ab676SJeff Bonwick hrtime_t functime = gethrtime(); 5424fa9e4066Sahrens 5425b24ab676SJeff Bonwick for (int i = 0; i < zi->zi_iters; i++) 5426b24ab676SJeff Bonwick zi->zi_func(zd, id); 5427fa9e4066Sahrens 5428b24ab676SJeff Bonwick functime = gethrtime() - functime; 5429fa9e4066Sahrens 5430420dfc95SChris Siden atomic_add_64(&zc->zc_count, 1); 5431420dfc95SChris Siden atomic_add_64(&zc->zc_time, functime); 5432fa9e4066Sahrens 5433420dfc95SChris Siden if (ztest_opts.zo_verbose >= 4) { 5434fa9e4066Sahrens Dl_info dli; 5435fa9e4066Sahrens (void) dladdr((void *)zi->zi_func, &dli); 5436fa9e4066Sahrens (void) printf("%6.2f sec in %s\n", 5437fa9e4066Sahrens (double)functime / NANOSEC, dli.dli_sname); 5438fa9e4066Sahrens } 5439b24ab676SJeff Bonwick } 5440b24ab676SJeff Bonwick 5441b24ab676SJeff Bonwick static void * 5442b24ab676SJeff Bonwick ztest_thread(void *arg) 5443b24ab676SJeff Bonwick { 5444420dfc95SChris Siden int rand; 5445b24ab676SJeff Bonwick uint64_t id = (uintptr_t)arg; 5446b24ab676SJeff Bonwick ztest_shared_t *zs = ztest_shared; 5447b24ab676SJeff Bonwick uint64_t call_next; 5448b24ab676SJeff Bonwick hrtime_t now; 5449b24ab676SJeff Bonwick ztest_info_t *zi; 5450420dfc95SChris Siden ztest_shared_callstate_t *zc; 5451b24ab676SJeff Bonwick 5452b24ab676SJeff Bonwick while ((now = gethrtime()) < zs->zs_thread_stop) { 5453b24ab676SJeff Bonwick /* 5454b24ab676SJeff Bonwick * See if it's time to force a crash. 5455b24ab676SJeff Bonwick */ 5456b24ab676SJeff Bonwick if (now > zs->zs_thread_kill) 5457b24ab676SJeff Bonwick ztest_kill(zs); 5458fa9e4066Sahrens 5459fa9e4066Sahrens /* 5460fa9e4066Sahrens * If we're getting ENOSPC with some regularity, stop. 5461fa9e4066Sahrens */ 5462fa9e4066Sahrens if (zs->zs_enospc_count > 10) 5463fa9e4066Sahrens break; 5464b24ab676SJeff Bonwick 5465b24ab676SJeff Bonwick /* 5466b24ab676SJeff Bonwick * Pick a random function to execute. 5467b24ab676SJeff Bonwick */ 5468420dfc95SChris Siden rand = ztest_random(ZTEST_FUNCS); 5469420dfc95SChris Siden zi = &ztest_info[rand]; 5470420dfc95SChris Siden zc = ZTEST_GET_SHARED_CALLSTATE(rand); 5471420dfc95SChris Siden call_next = zc->zc_next; 5472b24ab676SJeff Bonwick 5473b24ab676SJeff Bonwick if (now >= call_next && 5474420dfc95SChris Siden atomic_cas_64(&zc->zc_next, call_next, call_next + 5475420dfc95SChris Siden ztest_random(2 * zi->zi_interval[0] + 1)) == call_next) { 5476420dfc95SChris Siden ztest_execute(rand, zi, id); 5477420dfc95SChris Siden } 5478fa9e4066Sahrens } 5479fa9e4066Sahrens 5480fa9e4066Sahrens return (NULL); 5481fa9e4066Sahrens } 5482fa9e4066Sahrens 5483b24ab676SJeff Bonwick static void 5484b24ab676SJeff Bonwick ztest_dataset_name(char *dsname, char *pool, int d) 5485b24ab676SJeff Bonwick { 5486*40a5c998SMatthew Ahrens (void) snprintf(dsname, ZFS_MAX_DATASET_NAME_LEN, "%s/ds_%d", pool, d); 5487b24ab676SJeff Bonwick } 5488b24ab676SJeff Bonwick 5489b24ab676SJeff Bonwick static void 5490420dfc95SChris Siden ztest_dataset_destroy(int d) 5491b24ab676SJeff Bonwick { 5492*40a5c998SMatthew Ahrens char name[ZFS_MAX_DATASET_NAME_LEN]; 5493b24ab676SJeff Bonwick 5494420dfc95SChris Siden ztest_dataset_name(name, ztest_opts.zo_pool, d); 5495b24ab676SJeff Bonwick 5496420dfc95SChris Siden if (ztest_opts.zo_verbose >= 3) 5497b24ab676SJeff Bonwick (void) printf("Destroying %s to free up space\n", name); 5498b24ab676SJeff Bonwick 5499b24ab676SJeff Bonwick /* 5500b24ab676SJeff Bonwick * Cleanup any non-standard clones and snapshots. In general, 5501b24ab676SJeff Bonwick * ztest thread t operates on dataset (t % zopt_datasets), 5502b24ab676SJeff Bonwick * so there may be more than one thing to clean up. 5503b24ab676SJeff Bonwick */ 5504420dfc95SChris Siden for (int t = d; t < ztest_opts.zo_threads; 5505420dfc95SChris Siden t += ztest_opts.zo_datasets) { 5506b24ab676SJeff Bonwick ztest_dsl_dataset_cleanup(name, t); 5507420dfc95SChris Siden } 5508b24ab676SJeff Bonwick 5509b24ab676SJeff Bonwick (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL, 5510b24ab676SJeff Bonwick DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN); 5511b24ab676SJeff Bonwick } 5512b24ab676SJeff Bonwick 5513b24ab676SJeff Bonwick static void 5514b24ab676SJeff Bonwick ztest_dataset_dirobj_verify(ztest_ds_t *zd) 5515b24ab676SJeff Bonwick { 5516b24ab676SJeff Bonwick uint64_t usedobjs, dirobjs, scratch; 5517b24ab676SJeff Bonwick 5518b24ab676SJeff Bonwick /* 5519b24ab676SJeff Bonwick * ZTEST_DIROBJ is the object directory for the entire dataset. 5520b24ab676SJeff Bonwick * Therefore, the number of objects in use should equal the 5521b24ab676SJeff Bonwick * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself. 5522b24ab676SJeff Bonwick * If not, we have an object leak. 5523b24ab676SJeff Bonwick * 5524b24ab676SJeff Bonwick * Note that we can only check this in ztest_dataset_open(), 5525b24ab676SJeff Bonwick * when the open-context and syncing-context values agree. 5526b24ab676SJeff Bonwick * That's because zap_count() returns the open-context value, 5527b24ab676SJeff Bonwick * while dmu_objset_space() returns the rootbp fill count. 5528b24ab676SJeff Bonwick */ 5529b420f3adSRichard Lowe VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs)); 5530b24ab676SJeff Bonwick dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch); 5531b24ab676SJeff Bonwick ASSERT3U(dirobjs + 1, ==, usedobjs); 5532b24ab676SJeff Bonwick } 5533b24ab676SJeff Bonwick 5534b24ab676SJeff Bonwick static int 5535420dfc95SChris Siden ztest_dataset_open(int d) 5536b24ab676SJeff Bonwick { 5537420dfc95SChris Siden ztest_ds_t *zd = &ztest_ds[d]; 5538420dfc95SChris Siden uint64_t committed_seq = ZTEST_GET_SHARED_DS(d)->zd_seq; 5539b24ab676SJeff Bonwick objset_t *os; 5540b24ab676SJeff Bonwick zilog_t *zilog; 5541*40a5c998SMatthew Ahrens char name[ZFS_MAX_DATASET_NAME_LEN]; 5542b24ab676SJeff Bonwick int error; 5543b24ab676SJeff Bonwick 5544420dfc95SChris Siden ztest_dataset_name(name, ztest_opts.zo_pool, d); 5545b24ab676SJeff Bonwick 5546420dfc95SChris Siden (void) rw_rdlock(&ztest_name_lock); 5547b24ab676SJeff Bonwick 554855da60b9SMark J Musante error = ztest_dataset_create(name); 5549b24ab676SJeff Bonwick if (error == ENOSPC) { 5550420dfc95SChris Siden (void) rw_unlock(&ztest_name_lock); 5551b24ab676SJeff Bonwick ztest_record_enospc(FTAG); 5552b24ab676SJeff Bonwick return (error); 5553b24ab676SJeff Bonwick } 5554b24ab676SJeff Bonwick ASSERT(error == 0 || error == EEXIST); 5555b24ab676SJeff Bonwick 55563b2aab18SMatthew Ahrens VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, zd, &os)); 5557420dfc95SChris Siden (void) rw_unlock(&ztest_name_lock); 5558b24ab676SJeff Bonwick 5559420dfc95SChris Siden ztest_zd_init(zd, ZTEST_GET_SHARED_DS(d), os); 5560b24ab676SJeff Bonwick 5561b24ab676SJeff Bonwick zilog = zd->zd_zilog; 5562b24ab676SJeff Bonwick 5563b24ab676SJeff Bonwick if (zilog->zl_header->zh_claim_lr_seq != 0 && 5564b24ab676SJeff Bonwick zilog->zl_header->zh_claim_lr_seq < committed_seq) 5565b24ab676SJeff Bonwick fatal(0, "missing log records: claimed %llu < committed %llu", 5566b24ab676SJeff Bonwick zilog->zl_header->zh_claim_lr_seq, committed_seq); 5567b24ab676SJeff Bonwick 5568b24ab676SJeff Bonwick ztest_dataset_dirobj_verify(zd); 5569b24ab676SJeff Bonwick 5570b24ab676SJeff Bonwick zil_replay(os, zd, ztest_replay_vector); 5571b24ab676SJeff Bonwick 5572b24ab676SJeff Bonwick ztest_dataset_dirobj_verify(zd); 5573b24ab676SJeff Bonwick 5574420dfc95SChris Siden if (ztest_opts.zo_verbose >= 6) 5575b24ab676SJeff Bonwick (void) printf("%s replay %llu blocks, %llu records, seq %llu\n", 5576b24ab676SJeff Bonwick zd->zd_name, 5577b24ab676SJeff Bonwick (u_longlong_t)zilog->zl_parse_blk_count, 5578b24ab676SJeff Bonwick (u_longlong_t)zilog->zl_parse_lr_count, 5579b24ab676SJeff Bonwick (u_longlong_t)zilog->zl_replaying_seq); 5580b24ab676SJeff Bonwick 5581b24ab676SJeff Bonwick zilog = zil_open(os, ztest_get_data); 5582b24ab676SJeff Bonwick 5583b24ab676SJeff Bonwick if (zilog->zl_replaying_seq != 0 && 5584b24ab676SJeff Bonwick zilog->zl_replaying_seq < committed_seq) 5585b24ab676SJeff Bonwick fatal(0, "missing log records: replayed %llu < committed %llu", 5586b24ab676SJeff Bonwick zilog->zl_replaying_seq, committed_seq); 5587b24ab676SJeff Bonwick 5588b24ab676SJeff Bonwick return (0); 5589b24ab676SJeff Bonwick } 5590b24ab676SJeff Bonwick 5591b24ab676SJeff Bonwick static void 5592420dfc95SChris Siden ztest_dataset_close(int d) 5593b24ab676SJeff Bonwick { 5594420dfc95SChris Siden ztest_ds_t *zd = &ztest_ds[d]; 5595b24ab676SJeff Bonwick 5596b24ab676SJeff Bonwick zil_close(zd->zd_zilog); 55973b2aab18SMatthew Ahrens dmu_objset_disown(zd->zd_os, zd); 5598b24ab676SJeff Bonwick 5599b24ab676SJeff Bonwick ztest_zd_fini(zd); 5600b24ab676SJeff Bonwick } 5601b24ab676SJeff Bonwick 5602fa9e4066Sahrens /* 5603fa9e4066Sahrens * Kick off threads to run tests on all datasets in parallel. 5604fa9e4066Sahrens */ 5605fa9e4066Sahrens static void 5606b24ab676SJeff Bonwick ztest_run(ztest_shared_t *zs) 5607fa9e4066Sahrens { 5608b24ab676SJeff Bonwick thread_t *tid; 5609fa9e4066Sahrens spa_t *spa; 5610e9103aaeSGarrett D'Amore objset_t *os; 5611e14bb325SJeff Bonwick thread_t resume_tid; 5612b24ab676SJeff Bonwick int error; 5613e14bb325SJeff Bonwick 5614e14bb325SJeff Bonwick ztest_exiting = B_FALSE; 5615fa9e4066Sahrens 5616b24ab676SJeff Bonwick /* 5617b24ab676SJeff Bonwick * Initialize parent/child shared state. 5618b24ab676SJeff Bonwick */ 5619420dfc95SChris Siden VERIFY(_mutex_init(&ztest_vdev_lock, USYNC_THREAD, NULL) == 0); 5620420dfc95SChris Siden VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0); 5621fa9e4066Sahrens 5622b24ab676SJeff Bonwick zs->zs_thread_start = gethrtime(); 5623420dfc95SChris Siden zs->zs_thread_stop = 5624420dfc95SChris Siden zs->zs_thread_start + ztest_opts.zo_passtime * NANOSEC; 5625b24ab676SJeff Bonwick zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop); 5626b24ab676SJeff Bonwick zs->zs_thread_kill = zs->zs_thread_stop; 5627420dfc95SChris Siden if (ztest_random(100) < ztest_opts.zo_killrate) { 5628420dfc95SChris Siden zs->zs_thread_kill -= 5629420dfc95SChris Siden ztest_random(ztest_opts.zo_passtime * NANOSEC); 5630420dfc95SChris Siden } 5631b24ab676SJeff Bonwick 5632b24ab676SJeff Bonwick (void) _mutex_init(&zcl.zcl_callbacks_lock, USYNC_THREAD, NULL); 5633d20e665cSRicardo M. Correia 5634d20e665cSRicardo M. Correia list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t), 5635d20e665cSRicardo M. Correia offsetof(ztest_cb_data_t, zcd_node)); 5636d20e665cSRicardo M. Correia 5637fa9e4066Sahrens /* 5638fa9e4066Sahrens * Open our pool. 5639fa9e4066Sahrens */ 5640b24ab676SJeff Bonwick kernel_init(FREAD | FWRITE); 56413b2aab18SMatthew Ahrens VERIFY0(spa_open(ztest_opts.zo_pool, &spa, FTAG)); 564209c9d376SGeorge Wilson spa->spa_debug = B_TRUE; 564330beaff4SGeorge Wilson metaslab_preload_limit = ztest_random(20) + 1; 5644420dfc95SChris Siden ztest_spa = spa; 5645b24ab676SJeff Bonwick 56463b2aab18SMatthew Ahrens VERIFY0(dmu_objset_own(ztest_opts.zo_pool, 56473b2aab18SMatthew Ahrens DMU_OST_ANY, B_TRUE, FTAG, &os)); 5648e9103aaeSGarrett D'Amore zs->zs_guid = dmu_objset_fsid_guid(os); 56493b2aab18SMatthew Ahrens dmu_objset_disown(os, FTAG); 5650e9103aaeSGarrett D'Amore 5651b24ab676SJeff Bonwick spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN; 5652e14bb325SJeff Bonwick 5653e14bb325SJeff Bonwick /* 56548ad4d6ddSJeff Bonwick * We don't expect the pool to suspend unless maxfaults == 0, 56558ad4d6ddSJeff Bonwick * in which case ztest_fault_inject() temporarily takes away 56568ad4d6ddSJeff Bonwick * the only valid replica. 56578ad4d6ddSJeff Bonwick */ 56581195e687SMark J Musante if (MAXFAULTS() == 0) 56598ad4d6ddSJeff Bonwick spa->spa_failmode = ZIO_FAILURE_MODE_WAIT; 56608ad4d6ddSJeff Bonwick else 56618ad4d6ddSJeff Bonwick spa->spa_failmode = ZIO_FAILURE_MODE_PANIC; 56628ad4d6ddSJeff Bonwick 56638ad4d6ddSJeff Bonwick /* 5664e14bb325SJeff Bonwick * Create a thread to periodically resume suspended I/O. 5665e14bb325SJeff Bonwick */ 56668ad4d6ddSJeff Bonwick VERIFY(thr_create(0, 0, ztest_resume_thread, spa, THR_BOUND, 5667e14bb325SJeff Bonwick &resume_tid) == 0); 5668fa9e4066Sahrens 5669fa9e4066Sahrens /* 5670b24ab676SJeff Bonwick * Create a deadman thread to abort() if we hang. 5671b24ab676SJeff Bonwick */ 5672b24ab676SJeff Bonwick VERIFY(thr_create(0, 0, ztest_deadman_thread, zs, THR_BOUND, 5673b24ab676SJeff Bonwick NULL) == 0); 5674b24ab676SJeff Bonwick 5675b24ab676SJeff Bonwick /* 5676fa9e4066Sahrens * Verify that we can safely inquire about about any object, 5677fa9e4066Sahrens * whether it's allocated or not. To make it interesting, 5678fa9e4066Sahrens * we probe a 5-wide window around each power of two. 5679fa9e4066Sahrens * This hits all edge cases, including zero and the max. 5680fa9e4066Sahrens */ 5681b24ab676SJeff Bonwick for (int t = 0; t < 64; t++) { 5682b24ab676SJeff Bonwick for (int d = -5; d <= 5; d++) { 5683fa9e4066Sahrens error = dmu_object_info(spa->spa_meta_objset, 5684fa9e4066Sahrens (1ULL << t) + d, NULL); 5685ea8dc4b6Seschrock ASSERT(error == 0 || error == ENOENT || 5686ea8dc4b6Seschrock error == EINVAL); 5687fa9e4066Sahrens } 5688fa9e4066Sahrens } 5689fa9e4066Sahrens 5690fa9e4066Sahrens /* 5691b24ab676SJeff Bonwick * If we got any ENOSPC errors on the previous run, destroy something. 5692fa9e4066Sahrens */ 5693b24ab676SJeff Bonwick if (zs->zs_enospc_count != 0) { 5694420dfc95SChris Siden int d = ztest_random(ztest_opts.zo_datasets); 5695420dfc95SChris Siden ztest_dataset_destroy(d); 5696b24ab676SJeff Bonwick } 5697fa9e4066Sahrens zs->zs_enospc_count = 0; 5698fa9e4066Sahrens 5699420dfc95SChris Siden tid = umem_zalloc(ztest_opts.zo_threads * sizeof (thread_t), 5700420dfc95SChris Siden UMEM_NOFAIL); 5701fa9e4066Sahrens 5702420dfc95SChris Siden if (ztest_opts.zo_verbose >= 4) 5703fa9e4066Sahrens (void) printf("starting main threads...\n"); 5704fa9e4066Sahrens 5705b24ab676SJeff Bonwick /* 5706b24ab676SJeff Bonwick * Kick off all the tests that run in parallel. 5707b24ab676SJeff Bonwick */ 5708420dfc95SChris Siden for (int t = 0; t < ztest_opts.zo_threads; t++) { 5709420dfc95SChris Siden if (t < ztest_opts.zo_datasets && 5710420dfc95SChris Siden ztest_dataset_open(t) != 0) 5711b24ab676SJeff Bonwick return; 5712b24ab676SJeff Bonwick VERIFY(thr_create(0, 0, ztest_thread, (void *)(uintptr_t)t, 5713b24ab676SJeff Bonwick THR_BOUND, &tid[t]) == 0); 5714fa9e4066Sahrens } 5715fa9e4066Sahrens 5716fa9e4066Sahrens /* 5717b24ab676SJeff Bonwick * Wait for all of the tests to complete. We go in reverse order 5718b24ab676SJeff Bonwick * so we don't close datasets while threads are still using them. 5719fa9e4066Sahrens */ 5720420dfc95SChris Siden for (int t = ztest_opts.zo_threads - 1; t >= 0; t--) { 5721b24ab676SJeff Bonwick VERIFY(thr_join(tid[t], NULL, NULL) == 0); 5722420dfc95SChris Siden if (t < ztest_opts.zo_datasets) 5723420dfc95SChris Siden ztest_dataset_close(t); 5724fa9e4066Sahrens } 5725fa9e4066Sahrens 5726ea8dc4b6Seschrock txg_wait_synced(spa_get_dsl(spa), 0); 5727fa9e4066Sahrens 5728b24ab676SJeff Bonwick zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa)); 5729b24ab676SJeff Bonwick zs->zs_space = metaslab_class_get_space(spa_normal_class(spa)); 5730b4952e17SGeorge Wilson zfs_dbgmsg_print(FTAG); 5731b24ab676SJeff Bonwick 5732420dfc95SChris Siden umem_free(tid, ztest_opts.zo_threads * sizeof (thread_t)); 5733e14bb325SJeff Bonwick 5734e14bb325SJeff Bonwick /* Kill the resume thread */ 5735e14bb325SJeff Bonwick ztest_exiting = B_TRUE; 5736e14bb325SJeff Bonwick VERIFY(thr_join(resume_tid, NULL, NULL) == 0); 57378ad4d6ddSJeff Bonwick ztest_resume(spa); 5738e14bb325SJeff Bonwick 5739fa9e4066Sahrens /* 5740fa9e4066Sahrens * Right before closing the pool, kick off a bunch of async I/O; 5741fa9e4066Sahrens * spa_close() should wait for it to complete. 5742fa9e4066Sahrens */ 5743a2cdcdd2SPaul Dagnelie for (uint64_t object = 1; object < 50; object++) { 5744a2cdcdd2SPaul Dagnelie dmu_prefetch(spa->spa_meta_objset, object, 0, 0, 1ULL << 20, 5745a2cdcdd2SPaul Dagnelie ZIO_PRIORITY_SYNC_READ); 5746a2cdcdd2SPaul Dagnelie } 5747fa9e4066Sahrens 5748e05725b1Sbonwick spa_close(spa, FTAG); 5749e05725b1Sbonwick 5750b24ab676SJeff Bonwick /* 5751b24ab676SJeff Bonwick * Verify that we can loop over all pools. 5752b24ab676SJeff Bonwick */ 5753b24ab676SJeff Bonwick mutex_enter(&spa_namespace_lock); 5754b24ab676SJeff Bonwick for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa)) 5755420dfc95SChris Siden if (ztest_opts.zo_verbose > 3) 5756b24ab676SJeff Bonwick (void) printf("spa_next: found %s\n", spa_name(spa)); 5757b24ab676SJeff Bonwick mutex_exit(&spa_namespace_lock); 5758b24ab676SJeff Bonwick 5759b24ab676SJeff Bonwick /* 5760b24ab676SJeff Bonwick * Verify that we can export the pool and reimport it under a 5761b24ab676SJeff Bonwick * different name. 5762b24ab676SJeff Bonwick */ 5763b24ab676SJeff Bonwick if (ztest_random(2) == 0) { 5764*40a5c998SMatthew Ahrens char name[ZFS_MAX_DATASET_NAME_LEN]; 5765*40a5c998SMatthew Ahrens (void) snprintf(name, sizeof (name), "%s_import", 5766420dfc95SChris Siden ztest_opts.zo_pool); 5767420dfc95SChris Siden ztest_spa_import_export(ztest_opts.zo_pool, name); 5768420dfc95SChris Siden ztest_spa_import_export(name, ztest_opts.zo_pool); 5769b24ab676SJeff Bonwick } 5770b24ab676SJeff Bonwick 5771b24ab676SJeff Bonwick kernel_fini(); 577226cb54a9SGeorge Wilson 577326cb54a9SGeorge Wilson list_destroy(&zcl.zcl_callbacks); 577426cb54a9SGeorge Wilson 577526cb54a9SGeorge Wilson (void) _mutex_destroy(&zcl.zcl_callbacks_lock); 577626cb54a9SGeorge Wilson 5777420dfc95SChris Siden (void) rwlock_destroy(&ztest_name_lock); 5778420dfc95SChris Siden (void) _mutex_destroy(&ztest_vdev_lock); 5779b24ab676SJeff Bonwick } 5780b24ab676SJeff Bonwick 5781b24ab676SJeff Bonwick static void 5782420dfc95SChris Siden ztest_freeze(void) 5783b24ab676SJeff Bonwick { 5784420dfc95SChris Siden ztest_ds_t *zd = &ztest_ds[0]; 5785b24ab676SJeff Bonwick spa_t *spa; 57862215e990SMark J Musante int numloops = 0; 5787b24ab676SJeff Bonwick 5788420dfc95SChris Siden if (ztest_opts.zo_verbose >= 3) 5789b24ab676SJeff Bonwick (void) printf("testing spa_freeze()...\n"); 5790b24ab676SJeff Bonwick 5791b24ab676SJeff Bonwick kernel_init(FREAD | FWRITE); 5792b420f3adSRichard Lowe VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG)); 5793b420f3adSRichard Lowe VERIFY3U(0, ==, ztest_dataset_open(0)); 579480901aeaSGeorge Wilson spa->spa_debug = B_TRUE; 579580901aeaSGeorge Wilson ztest_spa = spa; 5796b24ab676SJeff Bonwick 5797b24ab676SJeff Bonwick /* 5798b24ab676SJeff Bonwick * Force the first log block to be transactionally allocated. 5799b24ab676SJeff Bonwick * We have to do this before we freeze the pool -- otherwise 5800b24ab676SJeff Bonwick * the log chain won't be anchored. 5801b24ab676SJeff Bonwick */ 5802b24ab676SJeff Bonwick while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) { 5803b24ab676SJeff Bonwick ztest_dmu_object_alloc_free(zd, 0); 58045002558fSNeil Perrin zil_commit(zd->zd_zilog, 0); 5805b24ab676SJeff Bonwick } 5806b24ab676SJeff Bonwick 5807b24ab676SJeff Bonwick txg_wait_synced(spa_get_dsl(spa), 0); 5808b24ab676SJeff Bonwick 5809b24ab676SJeff Bonwick /* 5810b24ab676SJeff Bonwick * Freeze the pool. This stops spa_sync() from doing anything, 5811b24ab676SJeff Bonwick * so that the only way to record changes from now on is the ZIL. 5812b24ab676SJeff Bonwick */ 5813b24ab676SJeff Bonwick spa_freeze(spa); 5814b24ab676SJeff Bonwick 5815b24ab676SJeff Bonwick /* 58162a104a52SAlex Reece * Because it is hard to predict how much space a write will actually 58172a104a52SAlex Reece * require beforehand, we leave ourselves some fudge space to write over 58182a104a52SAlex Reece * capacity. 58192a104a52SAlex Reece */ 58202a104a52SAlex Reece uint64_t capacity = metaslab_class_get_space(spa_normal_class(spa)) / 2; 58212a104a52SAlex Reece 58222a104a52SAlex Reece /* 5823b24ab676SJeff Bonwick * Run tests that generate log records but don't alter the pool config 5824b24ab676SJeff Bonwick * or depend on DSL sync tasks (snapshots, objset create/destroy, etc). 5825b24ab676SJeff Bonwick * We do a txg_wait_synced() after each iteration to force the txg 5826b24ab676SJeff Bonwick * to increase well beyond the last synced value in the uberblock. 5827b24ab676SJeff Bonwick * The ZIL should be OK with that. 58282a104a52SAlex Reece * 58292a104a52SAlex Reece * Run a random number of times less than zo_maxloops and ensure we do 58302a104a52SAlex Reece * not run out of space on the pool. 5831b24ab676SJeff Bonwick */ 5832420dfc95SChris Siden while (ztest_random(10) != 0 && 58332a104a52SAlex Reece numloops++ < ztest_opts.zo_maxloops && 58342a104a52SAlex Reece metaslab_class_get_alloc(spa_normal_class(spa)) < capacity) { 58352a104a52SAlex Reece ztest_od_t od; 58362a104a52SAlex Reece ztest_od_init(&od, 0, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0); 58372a104a52SAlex Reece VERIFY0(ztest_object_init(zd, &od, sizeof (od), B_FALSE)); 58382a104a52SAlex Reece ztest_io(zd, od.od_object, 58392a104a52SAlex Reece ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT); 5840b24ab676SJeff Bonwick txg_wait_synced(spa_get_dsl(spa), 0); 5841b24ab676SJeff Bonwick } 5842b24ab676SJeff Bonwick 5843b24ab676SJeff Bonwick /* 5844b24ab676SJeff Bonwick * Commit all of the changes we just generated. 5845b24ab676SJeff Bonwick */ 58465002558fSNeil Perrin zil_commit(zd->zd_zilog, 0); 5847b24ab676SJeff Bonwick txg_wait_synced(spa_get_dsl(spa), 0); 5848b24ab676SJeff Bonwick 5849b24ab676SJeff Bonwick /* 5850b24ab676SJeff Bonwick * Close our dataset and close the pool. 5851b24ab676SJeff Bonwick */ 5852420dfc95SChris Siden ztest_dataset_close(0); 5853b24ab676SJeff Bonwick spa_close(spa, FTAG); 5854b24ab676SJeff Bonwick kernel_fini(); 5855b24ab676SJeff Bonwick 5856b24ab676SJeff Bonwick /* 5857b24ab676SJeff Bonwick * Open and close the pool and dataset to induce log replay. 5858b24ab676SJeff Bonwick */ 5859b24ab676SJeff Bonwick kernel_init(FREAD | FWRITE); 5860b420f3adSRichard Lowe VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG)); 5861ce636f8bSMatthew Ahrens ASSERT(spa_freeze_txg(spa) == UINT64_MAX); 5862b420f3adSRichard Lowe VERIFY3U(0, ==, ztest_dataset_open(0)); 5863420dfc95SChris Siden ztest_dataset_close(0); 5864dfbb9432SGeorge Wilson 5865dfbb9432SGeorge Wilson spa->spa_debug = B_TRUE; 5866dfbb9432SGeorge Wilson ztest_spa = spa; 5867dfbb9432SGeorge Wilson txg_wait_synced(spa_get_dsl(spa), 0); 5868dfbb9432SGeorge Wilson ztest_reguid(NULL, 0); 5869dfbb9432SGeorge Wilson 5870b24ab676SJeff Bonwick spa_close(spa, FTAG); 5871fa9e4066Sahrens kernel_fini(); 5872fa9e4066Sahrens } 5873fa9e4066Sahrens 5874fa9e4066Sahrens void 5875fa9e4066Sahrens print_time(hrtime_t t, char *timebuf) 5876fa9e4066Sahrens { 5877fa9e4066Sahrens hrtime_t s = t / NANOSEC; 5878fa9e4066Sahrens hrtime_t m = s / 60; 5879fa9e4066Sahrens hrtime_t h = m / 60; 5880fa9e4066Sahrens hrtime_t d = h / 24; 5881fa9e4066Sahrens 5882fa9e4066Sahrens s -= m * 60; 5883fa9e4066Sahrens m -= h * 60; 5884fa9e4066Sahrens h -= d * 24; 5885fa9e4066Sahrens 5886fa9e4066Sahrens timebuf[0] = '\0'; 5887fa9e4066Sahrens 5888fa9e4066Sahrens if (d) 5889fa9e4066Sahrens (void) sprintf(timebuf, 5890fa9e4066Sahrens "%llud%02lluh%02llum%02llus", d, h, m, s); 5891fa9e4066Sahrens else if (h) 5892fa9e4066Sahrens (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s); 5893fa9e4066Sahrens else if (m) 5894fa9e4066Sahrens (void) sprintf(timebuf, "%llum%02llus", m, s); 5895fa9e4066Sahrens else 5896fa9e4066Sahrens (void) sprintf(timebuf, "%llus", s); 5897fa9e4066Sahrens } 5898fa9e4066Sahrens 58991195e687SMark J Musante static nvlist_t * 59001195e687SMark J Musante make_random_props() 59011195e687SMark J Musante { 59021195e687SMark J Musante nvlist_t *props; 59031195e687SMark J Musante 59041195e687SMark J Musante VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0); 5905ad135b5dSChristopher Siden if (ztest_random(2) == 0) 5906ad135b5dSChristopher Siden return (props); 59071195e687SMark J Musante VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0); 59081195e687SMark J Musante 59091195e687SMark J Musante return (props); 59101195e687SMark J Musante } 59111195e687SMark J Musante 5912fa9e4066Sahrens /* 5913fa9e4066Sahrens * Create a storage pool with the given name and initial vdev size. 5914b24ab676SJeff Bonwick * Then test spa_freeze() functionality. 5915fa9e4066Sahrens */ 5916fa9e4066Sahrens static void 5917b24ab676SJeff Bonwick ztest_init(ztest_shared_t *zs) 5918fa9e4066Sahrens { 5919fa9e4066Sahrens spa_t *spa; 59201195e687SMark J Musante nvlist_t *nvroot, *props; 5921fa9e4066Sahrens 5922420dfc95SChris Siden VERIFY(_mutex_init(&ztest_vdev_lock, USYNC_THREAD, NULL) == 0); 5923420dfc95SChris Siden VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0); 5924b24ab676SJeff Bonwick 5925fa9e4066Sahrens kernel_init(FREAD | FWRITE); 5926fa9e4066Sahrens 5927fa9e4066Sahrens /* 5928fa9e4066Sahrens * Create the storage pool. 5929fa9e4066Sahrens */ 5930420dfc95SChris Siden (void) spa_destroy(ztest_opts.zo_pool); 593188ecc943SGeorge Wilson ztest_shared->zs_vdev_next_leaf = 0; 59321195e687SMark J Musante zs->zs_splits = 0; 5933420dfc95SChris Siden zs->zs_mirrors = ztest_opts.zo_mirrors; 593425345e46SGeorge Wilson nvroot = make_vdev_root(NULL, NULL, NULL, ztest_opts.zo_vdev_size, 0, 5935420dfc95SChris Siden 0, ztest_opts.zo_raidz, zs->zs_mirrors, 1); 59361195e687SMark J Musante props = make_random_props(); 5937ad135b5dSChristopher Siden for (int i = 0; i < SPA_FEATURES; i++) { 5938ad135b5dSChristopher Siden char buf[1024]; 5939ad135b5dSChristopher Siden (void) snprintf(buf, sizeof (buf), "feature@%s", 5940ad135b5dSChristopher Siden spa_feature_table[i].fi_uname); 5941b420f3adSRichard Lowe VERIFY3U(0, ==, nvlist_add_uint64(props, buf, 0)); 5942ad135b5dSChristopher Siden } 5943b420f3adSRichard Lowe VERIFY3U(0, ==, spa_create(ztest_opts.zo_pool, nvroot, props, NULL)); 5944fa9e4066Sahrens nvlist_free(nvroot); 59451df447ebSRichard Yao nvlist_free(props); 5946fa9e4066Sahrens 5947b420f3adSRichard Lowe VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG)); 5948420dfc95SChris Siden zs->zs_metaslab_sz = 5949420dfc95SChris Siden 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift; 5950ad135b5dSChristopher Siden 5951fa9e4066Sahrens spa_close(spa, FTAG); 5952fa9e4066Sahrens 5953fa9e4066Sahrens kernel_fini(); 5954b24ab676SJeff Bonwick 5955420dfc95SChris Siden ztest_run_zdb(ztest_opts.zo_pool); 5956b24ab676SJeff Bonwick 5957420dfc95SChris Siden ztest_freeze(); 5958b24ab676SJeff Bonwick 5959420dfc95SChris Siden ztest_run_zdb(ztest_opts.zo_pool); 596026cb54a9SGeorge Wilson 5961420dfc95SChris Siden (void) rwlock_destroy(&ztest_name_lock); 5962420dfc95SChris Siden (void) _mutex_destroy(&ztest_vdev_lock); 5963420dfc95SChris Siden } 5964420dfc95SChris Siden 5965420dfc95SChris Siden static void 5966741652b0SEtienne Dechamps setup_data_fd(void) 5967420dfc95SChris Siden { 5968741652b0SEtienne Dechamps static char ztest_name_data[] = "/tmp/ztest.data.XXXXXX"; 5969420dfc95SChris Siden 5970741652b0SEtienne Dechamps ztest_fd_data = mkstemp(ztest_name_data); 5971741652b0SEtienne Dechamps ASSERT3S(ztest_fd_data, >=, 0); 5972741652b0SEtienne Dechamps (void) unlink(ztest_name_data); 5973420dfc95SChris Siden } 5974420dfc95SChris Siden 5975741652b0SEtienne Dechamps 5976ad135b5dSChristopher Siden static int 5977ad135b5dSChristopher Siden shared_data_size(ztest_shared_hdr_t *hdr) 5978ad135b5dSChristopher Siden { 5979ad135b5dSChristopher Siden int size; 5980ad135b5dSChristopher Siden 5981ad135b5dSChristopher Siden size = hdr->zh_hdr_size; 5982ad135b5dSChristopher Siden size += hdr->zh_opts_size; 5983ad135b5dSChristopher Siden size += hdr->zh_size; 5984ad135b5dSChristopher Siden size += hdr->zh_stats_size * hdr->zh_stats_count; 5985ad135b5dSChristopher Siden size += hdr->zh_ds_size * hdr->zh_ds_count; 5986ad135b5dSChristopher Siden 5987ad135b5dSChristopher Siden return (size); 5988ad135b5dSChristopher Siden } 5989ad135b5dSChristopher Siden 5990420dfc95SChris Siden static void 5991420dfc95SChris Siden setup_hdr(void) 5992420dfc95SChris Siden { 5993ad135b5dSChristopher Siden int size; 5994420dfc95SChris Siden ztest_shared_hdr_t *hdr; 5995420dfc95SChris Siden 5996420dfc95SChris Siden hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()), 5997741652b0SEtienne Dechamps PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0); 5998420dfc95SChris Siden ASSERT(hdr != MAP_FAILED); 5999420dfc95SChris Siden 6000741652b0SEtienne Dechamps VERIFY3U(0, ==, ftruncate(ztest_fd_data, sizeof (ztest_shared_hdr_t))); 6001ad135b5dSChristopher Siden 6002420dfc95SChris Siden hdr->zh_hdr_size = sizeof (ztest_shared_hdr_t); 6003420dfc95SChris Siden hdr->zh_opts_size = sizeof (ztest_shared_opts_t); 6004420dfc95SChris Siden hdr->zh_size = sizeof (ztest_shared_t); 6005420dfc95SChris Siden hdr->zh_stats_size = sizeof (ztest_shared_callstate_t); 6006420dfc95SChris Siden hdr->zh_stats_count = ZTEST_FUNCS; 6007420dfc95SChris Siden hdr->zh_ds_size = sizeof (ztest_shared_ds_t); 6008420dfc95SChris Siden hdr->zh_ds_count = ztest_opts.zo_datasets; 6009420dfc95SChris Siden 6010ad135b5dSChristopher Siden size = shared_data_size(hdr); 6011741652b0SEtienne Dechamps VERIFY3U(0, ==, ftruncate(ztest_fd_data, size)); 6012ad135b5dSChristopher Siden 6013420dfc95SChris Siden (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize())); 6014420dfc95SChris Siden } 6015420dfc95SChris Siden 6016420dfc95SChris Siden static void 6017420dfc95SChris Siden setup_data(void) 6018420dfc95SChris Siden { 6019420dfc95SChris Siden int size, offset; 6020420dfc95SChris Siden ztest_shared_hdr_t *hdr; 6021420dfc95SChris Siden uint8_t *buf; 6022420dfc95SChris Siden 6023420dfc95SChris Siden hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()), 6024741652b0SEtienne Dechamps PROT_READ, MAP_SHARED, ztest_fd_data, 0); 6025420dfc95SChris Siden ASSERT(hdr != MAP_FAILED); 6026420dfc95SChris Siden 6027ad135b5dSChristopher Siden size = shared_data_size(hdr); 6028420dfc95SChris Siden 6029420dfc95SChris Siden (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize())); 6030420dfc95SChris Siden hdr = ztest_shared_hdr = (void *)mmap(0, P2ROUNDUP(size, getpagesize()), 6031741652b0SEtienne Dechamps PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0); 6032420dfc95SChris Siden ASSERT(hdr != MAP_FAILED); 6033420dfc95SChris Siden buf = (uint8_t *)hdr; 6034420dfc95SChris Siden 6035420dfc95SChris Siden offset = hdr->zh_hdr_size; 6036420dfc95SChris Siden ztest_shared_opts = (void *)&buf[offset]; 6037420dfc95SChris Siden offset += hdr->zh_opts_size; 6038420dfc95SChris Siden ztest_shared = (void *)&buf[offset]; 6039420dfc95SChris Siden offset += hdr->zh_size; 6040420dfc95SChris Siden ztest_shared_callstate = (void *)&buf[offset]; 6041420dfc95SChris Siden offset += hdr->zh_stats_size * hdr->zh_stats_count; 6042420dfc95SChris Siden ztest_shared_ds = (void *)&buf[offset]; 6043420dfc95SChris Siden } 6044420dfc95SChris Siden 6045420dfc95SChris Siden static boolean_t 6046420dfc95SChris Siden exec_child(char *cmd, char *libpath, boolean_t ignorekill, int *statusp) 6047420dfc95SChris Siden { 6048420dfc95SChris Siden pid_t pid; 6049420dfc95SChris Siden int status; 6050741652b0SEtienne Dechamps char *cmdbuf = NULL; 6051420dfc95SChris Siden 6052420dfc95SChris Siden pid = fork(); 6053420dfc95SChris Siden 6054420dfc95SChris Siden if (cmd == NULL) { 6055741652b0SEtienne Dechamps cmdbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL); 6056741652b0SEtienne Dechamps (void) strlcpy(cmdbuf, getexecname(), MAXPATHLEN); 6057420dfc95SChris Siden cmd = cmdbuf; 6058420dfc95SChris Siden } 6059420dfc95SChris Siden 6060420dfc95SChris Siden if (pid == -1) 6061420dfc95SChris Siden fatal(1, "fork failed"); 6062420dfc95SChris Siden 6063420dfc95SChris Siden if (pid == 0) { /* child */ 6064420dfc95SChris Siden char *emptyargv[2] = { cmd, NULL }; 6065741652b0SEtienne Dechamps char fd_data_str[12]; 6066420dfc95SChris Siden 6067420dfc95SChris Siden struct rlimit rl = { 1024, 1024 }; 6068420dfc95SChris Siden (void) setrlimit(RLIMIT_NOFILE, &rl); 6069741652b0SEtienne Dechamps 6070741652b0SEtienne Dechamps (void) close(ztest_fd_rand); 6071741652b0SEtienne Dechamps VERIFY3U(11, >=, 6072741652b0SEtienne Dechamps snprintf(fd_data_str, 12, "%d", ztest_fd_data)); 6073741652b0SEtienne Dechamps VERIFY0(setenv("ZTEST_FD_DATA", fd_data_str, 1)); 6074741652b0SEtienne Dechamps 6075420dfc95SChris Siden (void) enable_extended_FILE_stdio(-1, -1); 6076420dfc95SChris Siden if (libpath != NULL) 6077420dfc95SChris Siden VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath, 1)); 6078420dfc95SChris Siden (void) execv(cmd, emptyargv); 6079420dfc95SChris Siden ztest_dump_core = B_FALSE; 6080420dfc95SChris Siden fatal(B_TRUE, "exec failed: %s", cmd); 6081420dfc95SChris Siden } 6082420dfc95SChris Siden 6083741652b0SEtienne Dechamps if (cmdbuf != NULL) { 6084741652b0SEtienne Dechamps umem_free(cmdbuf, MAXPATHLEN); 6085741652b0SEtienne Dechamps cmd = NULL; 6086741652b0SEtienne Dechamps } 6087741652b0SEtienne Dechamps 6088420dfc95SChris Siden while (waitpid(pid, &status, 0) != pid) 6089420dfc95SChris Siden continue; 6090420dfc95SChris Siden if (statusp != NULL) 6091420dfc95SChris Siden *statusp = status; 6092420dfc95SChris Siden 6093420dfc95SChris Siden if (WIFEXITED(status)) { 6094420dfc95SChris Siden if (WEXITSTATUS(status) != 0) { 6095420dfc95SChris Siden (void) fprintf(stderr, "child exited with code %d\n", 6096420dfc95SChris Siden WEXITSTATUS(status)); 6097420dfc95SChris Siden exit(2); 6098420dfc95SChris Siden } 6099420dfc95SChris Siden return (B_FALSE); 6100420dfc95SChris Siden } else if (WIFSIGNALED(status)) { 6101420dfc95SChris Siden if (!ignorekill || WTERMSIG(status) != SIGKILL) { 6102420dfc95SChris Siden (void) fprintf(stderr, "child died with signal %d\n", 6103420dfc95SChris Siden WTERMSIG(status)); 6104420dfc95SChris Siden exit(3); 6105420dfc95SChris Siden } 6106420dfc95SChris Siden return (B_TRUE); 6107420dfc95SChris Siden } else { 6108420dfc95SChris Siden (void) fprintf(stderr, "something strange happened to child\n"); 6109420dfc95SChris Siden exit(4); 6110420dfc95SChris Siden /* NOTREACHED */ 6111420dfc95SChris Siden } 6112420dfc95SChris Siden } 6113420dfc95SChris Siden 6114420dfc95SChris Siden static void 6115420dfc95SChris Siden ztest_run_init(void) 6116420dfc95SChris Siden { 6117420dfc95SChris Siden ztest_shared_t *zs = ztest_shared; 6118420dfc95SChris Siden 6119420dfc95SChris Siden ASSERT(ztest_opts.zo_init != 0); 6120420dfc95SChris Siden 6121420dfc95SChris Siden /* 6122420dfc95SChris Siden * Blow away any existing copy of zpool.cache 6123420dfc95SChris Siden */ 6124420dfc95SChris Siden (void) remove(spa_config_path); 6125420dfc95SChris Siden 6126420dfc95SChris Siden /* 6127420dfc95SChris Siden * Create and initialize our storage pool. 6128420dfc95SChris Siden */ 6129420dfc95SChris Siden for (int i = 1; i <= ztest_opts.zo_init; i++) { 6130420dfc95SChris Siden bzero(zs, sizeof (ztest_shared_t)); 6131420dfc95SChris Siden if (ztest_opts.zo_verbose >= 3 && 6132420dfc95SChris Siden ztest_opts.zo_init != 1) { 6133420dfc95SChris Siden (void) printf("ztest_init(), pass %d\n", i); 6134420dfc95SChris Siden } 6135420dfc95SChris Siden ztest_init(zs); 6136420dfc95SChris Siden } 6137fa9e4066Sahrens } 6138fa9e4066Sahrens 6139fa9e4066Sahrens int 6140fa9e4066Sahrens main(int argc, char **argv) 6141fa9e4066Sahrens { 6142fa9e4066Sahrens int kills = 0; 6143fa9e4066Sahrens int iters = 0; 6144420dfc95SChris Siden int older = 0; 6145420dfc95SChris Siden int newer = 0; 6146fa9e4066Sahrens ztest_shared_t *zs; 6147fa9e4066Sahrens ztest_info_t *zi; 6148420dfc95SChris Siden ztest_shared_callstate_t *zc; 6149fa9e4066Sahrens char timebuf[100]; 6150fa9e4066Sahrens char numbuf[6]; 6151b24ab676SJeff Bonwick spa_t *spa; 6152741652b0SEtienne Dechamps char *cmd; 6153420dfc95SChris Siden boolean_t hasalt; 6154741652b0SEtienne Dechamps char *fd_data_str = getenv("ZTEST_FD_DATA"); 6155fa9e4066Sahrens 6156fa9e4066Sahrens (void) setvbuf(stdout, NULL, _IOLBF, 0); 6157fa9e4066Sahrens 6158cd1c8b85SMatthew Ahrens dprintf_setup(&argc, argv); 615969962b56SMatthew Ahrens zfs_deadman_synctime_ms = 300000; 6160cd1c8b85SMatthew Ahrens 6161741652b0SEtienne Dechamps ztest_fd_rand = open("/dev/urandom", O_RDONLY); 6162741652b0SEtienne Dechamps ASSERT3S(ztest_fd_rand, >=, 0); 6163741652b0SEtienne Dechamps 6164741652b0SEtienne Dechamps if (!fd_data_str) { 6165fa9e4066Sahrens process_options(argc, argv); 6166fa9e4066Sahrens 6167741652b0SEtienne Dechamps setup_data_fd(); 6168420dfc95SChris Siden setup_hdr(); 6169420dfc95SChris Siden setup_data(); 6170420dfc95SChris Siden bcopy(&ztest_opts, ztest_shared_opts, 6171420dfc95SChris Siden sizeof (*ztest_shared_opts)); 6172420dfc95SChris Siden } else { 6173741652b0SEtienne Dechamps ztest_fd_data = atoi(fd_data_str); 6174420dfc95SChris Siden setup_data(); 6175420dfc95SChris Siden bcopy(ztest_shared_opts, &ztest_opts, sizeof (ztest_opts)); 6176420dfc95SChris Siden } 6177420dfc95SChris Siden ASSERT3U(ztest_opts.zo_datasets, ==, ztest_shared_hdr->zh_ds_count); 6178420dfc95SChris Siden 6179f0ba89beSJeff Bonwick /* Override location of zpool.cache */ 6180741652b0SEtienne Dechamps VERIFY3U(asprintf((char **)&spa_config_path, "%s/zpool.cache", 6181741652b0SEtienne Dechamps ztest_opts.zo_dir), !=, -1); 6182f0ba89beSJeff Bonwick 6183420dfc95SChris Siden ztest_ds = umem_alloc(ztest_opts.zo_datasets * sizeof (ztest_ds_t), 6184420dfc95SChris Siden UMEM_NOFAIL); 6185420dfc95SChris Siden zs = ztest_shared; 6186ea8dc4b6Seschrock 6187741652b0SEtienne Dechamps if (fd_data_str) { 6188420dfc95SChris Siden metaslab_gang_bang = ztest_opts.zo_metaslab_gang_bang; 6189420dfc95SChris Siden metaslab_df_alloc_threshold = 6190420dfc95SChris Siden zs->zs_metaslab_df_alloc_threshold; 6191b24ab676SJeff Bonwick 6192420dfc95SChris Siden if (zs->zs_do_init) 6193420dfc95SChris Siden ztest_run_init(); 6194420dfc95SChris Siden else 6195420dfc95SChris Siden ztest_run(zs); 6196420dfc95SChris Siden exit(0); 6197420dfc95SChris Siden } 6198fa9e4066Sahrens 6199420dfc95SChris Siden hasalt = (strlen(ztest_opts.zo_alt_ztest) != 0); 6200420dfc95SChris Siden 6201420dfc95SChris Siden if (ztest_opts.zo_verbose >= 1) { 6202fa9e4066Sahrens (void) printf("%llu vdevs, %d datasets, %d threads," 6203fa9e4066Sahrens " %llu seconds...\n", 6204420dfc95SChris Siden (u_longlong_t)ztest_opts.zo_vdevs, 6205420dfc95SChris Siden ztest_opts.zo_datasets, 6206420dfc95SChris Siden ztest_opts.zo_threads, 6207420dfc95SChris Siden (u_longlong_t)ztest_opts.zo_time); 6208fa9e4066Sahrens } 6209fa9e4066Sahrens 6210741652b0SEtienne Dechamps cmd = umem_alloc(MAXNAMELEN, UMEM_NOFAIL); 6211741652b0SEtienne Dechamps (void) strlcpy(cmd, getexecname(), MAXNAMELEN); 6212fa9e4066Sahrens 6213420dfc95SChris Siden zs->zs_do_init = B_TRUE; 6214420dfc95SChris Siden if (strlen(ztest_opts.zo_alt_ztest) != 0) { 6215420dfc95SChris Siden if (ztest_opts.zo_verbose >= 1) { 6216420dfc95SChris Siden (void) printf("Executing older ztest for " 6217420dfc95SChris Siden "initialization: %s\n", ztest_opts.zo_alt_ztest); 6218420dfc95SChris Siden } 6219420dfc95SChris Siden VERIFY(!exec_child(ztest_opts.zo_alt_ztest, 6220420dfc95SChris Siden ztest_opts.zo_alt_libpath, B_FALSE, NULL)); 6221420dfc95SChris Siden } else { 6222420dfc95SChris Siden VERIFY(!exec_child(NULL, NULL, B_FALSE, NULL)); 6223420dfc95SChris Siden } 6224420dfc95SChris Siden zs->zs_do_init = B_FALSE; 6225420dfc95SChris Siden 6226b24ab676SJeff Bonwick zs->zs_proc_start = gethrtime(); 6227420dfc95SChris Siden zs->zs_proc_stop = zs->zs_proc_start + ztest_opts.zo_time * NANOSEC; 6228b24ab676SJeff Bonwick 6229b24ab676SJeff Bonwick for (int f = 0; f < ZTEST_FUNCS; f++) { 6230420dfc95SChris Siden zi = &ztest_info[f]; 6231420dfc95SChris Siden zc = ZTEST_GET_SHARED_CALLSTATE(f); 6232b24ab676SJeff Bonwick if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop) 6233420dfc95SChris Siden zc->zc_next = UINT64_MAX; 6234fa9e4066Sahrens else 6235420dfc95SChris Siden zc->zc_next = zs->zs_proc_start + 6236b24ab676SJeff Bonwick ztest_random(2 * zi->zi_interval[0] + 1); 6237fa9e4066Sahrens } 6238fa9e4066Sahrens 6239fa9e4066Sahrens /* 6240fa9e4066Sahrens * Run the tests in a loop. These tests include fault injection 6241fa9e4066Sahrens * to verify that self-healing data works, and forced crashes 6242fa9e4066Sahrens * to verify that we never lose on-disk consistency. 6243fa9e4066Sahrens */ 6244b24ab676SJeff Bonwick while (gethrtime() < zs->zs_proc_stop) { 6245fa9e4066Sahrens int status; 6246420dfc95SChris Siden boolean_t killed; 6247fa9e4066Sahrens 6248fa9e4066Sahrens /* 6249fa9e4066Sahrens * Initialize the workload counters for each function. 6250fa9e4066Sahrens */ 6251b24ab676SJeff Bonwick for (int f = 0; f < ZTEST_FUNCS; f++) { 6252420dfc95SChris Siden zc = ZTEST_GET_SHARED_CALLSTATE(f); 6253420dfc95SChris Siden zc->zc_count = 0; 6254420dfc95SChris Siden zc->zc_time = 0; 6255fa9e4066Sahrens } 6256fa9e4066Sahrens 6257d6e555bdSGeorge Wilson /* Set the allocation switch size */ 6258420dfc95SChris Siden zs->zs_metaslab_df_alloc_threshold = 6259420dfc95SChris Siden ztest_random(zs->zs_metaslab_sz / 4) + 1; 6260d6e555bdSGeorge Wilson 6261420dfc95SChris Siden if (!hasalt || ztest_random(2) == 0) { 6262420dfc95SChris Siden if (hasalt && ztest_opts.zo_verbose >= 1) { 6263420dfc95SChris Siden (void) printf("Executing newer ztest: %s\n", 6264420dfc95SChris Siden cmd); 6265fa9e4066Sahrens } 6266420dfc95SChris Siden newer++; 6267420dfc95SChris Siden killed = exec_child(cmd, NULL, B_TRUE, &status); 62685ad82045Snd150628 } else { 6269420dfc95SChris Siden if (hasalt && ztest_opts.zo_verbose >= 1) { 6270420dfc95SChris Siden (void) printf("Executing older ztest: %s\n", 6271420dfc95SChris Siden ztest_opts.zo_alt_ztest); 6272420dfc95SChris Siden } 6273420dfc95SChris Siden older++; 6274420dfc95SChris Siden killed = exec_child(ztest_opts.zo_alt_ztest, 6275420dfc95SChris Siden ztest_opts.zo_alt_libpath, B_TRUE, &status); 6276fa9e4066Sahrens } 6277fa9e4066Sahrens 6278420dfc95SChris Siden if (killed) 6279420dfc95SChris Siden kills++; 6280fa9e4066Sahrens iters++; 6281fa9e4066Sahrens 6282420dfc95SChris Siden if (ztest_opts.zo_verbose >= 1) { 6283fa9e4066Sahrens hrtime_t now = gethrtime(); 6284fa9e4066Sahrens 6285b24ab676SJeff Bonwick now = MIN(now, zs->zs_proc_stop); 6286b24ab676SJeff Bonwick print_time(zs->zs_proc_stop - now, timebuf); 6287fa9e4066Sahrens nicenum(zs->zs_space, numbuf); 6288fa9e4066Sahrens 6289fa9e4066Sahrens (void) printf("Pass %3d, %8s, %3llu ENOSPC, " 6290fa9e4066Sahrens "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n", 6291fa9e4066Sahrens iters, 6292fa9e4066Sahrens WIFEXITED(status) ? "Complete" : "SIGKILL", 6293fa9e4066Sahrens (u_longlong_t)zs->zs_enospc_count, 6294fa9e4066Sahrens 100.0 * zs->zs_alloc / zs->zs_space, 6295fa9e4066Sahrens numbuf, 6296b24ab676SJeff Bonwick 100.0 * (now - zs->zs_proc_start) / 6297420dfc95SChris Siden (ztest_opts.zo_time * NANOSEC), timebuf); 6298fa9e4066Sahrens } 6299fa9e4066Sahrens 6300420dfc95SChris Siden if (ztest_opts.zo_verbose >= 2) { 6301fa9e4066Sahrens (void) printf("\nWorkload summary:\n\n"); 6302fa9e4066Sahrens (void) printf("%7s %9s %s\n", 6303fa9e4066Sahrens "Calls", "Time", "Function"); 6304fa9e4066Sahrens (void) printf("%7s %9s %s\n", 6305fa9e4066Sahrens "-----", "----", "--------"); 6306b24ab676SJeff Bonwick for (int f = 0; f < ZTEST_FUNCS; f++) { 6307fa9e4066Sahrens Dl_info dli; 6308fa9e4066Sahrens 6309420dfc95SChris Siden zi = &ztest_info[f]; 6310420dfc95SChris Siden zc = ZTEST_GET_SHARED_CALLSTATE(f); 6311420dfc95SChris Siden print_time(zc->zc_time, timebuf); 6312fa9e4066Sahrens (void) dladdr((void *)zi->zi_func, &dli); 6313fa9e4066Sahrens (void) printf("%7llu %9s %s\n", 6314420dfc95SChris Siden (u_longlong_t)zc->zc_count, timebuf, 6315fa9e4066Sahrens dli.dli_sname); 6316fa9e4066Sahrens } 6317fa9e4066Sahrens (void) printf("\n"); 6318fa9e4066Sahrens } 6319fa9e4066Sahrens 6320fa9e4066Sahrens /* 6321b24ab676SJeff Bonwick * It's possible that we killed a child during a rename test, 6322b24ab676SJeff Bonwick * in which case we'll have a 'ztest_tmp' pool lying around 6323b24ab676SJeff Bonwick * instead of 'ztest'. Do a blind rename in case this happened. 6324fa9e4066Sahrens */ 6325b24ab676SJeff Bonwick kernel_init(FREAD); 6326420dfc95SChris Siden if (spa_open(ztest_opts.zo_pool, &spa, FTAG) == 0) { 6327b24ab676SJeff Bonwick spa_close(spa, FTAG); 6328b24ab676SJeff Bonwick } else { 6329*40a5c998SMatthew Ahrens char tmpname[ZFS_MAX_DATASET_NAME_LEN]; 6330fa9e4066Sahrens kernel_fini(); 6331b24ab676SJeff Bonwick kernel_init(FREAD | FWRITE); 6332b24ab676SJeff Bonwick (void) snprintf(tmpname, sizeof (tmpname), "%s_tmp", 6333420dfc95SChris Siden ztest_opts.zo_pool); 6334420dfc95SChris Siden (void) spa_rename(tmpname, ztest_opts.zo_pool); 6335fa9e4066Sahrens } 6336b24ab676SJeff Bonwick kernel_fini(); 6337fa9e4066Sahrens 6338420dfc95SChris Siden ztest_run_zdb(ztest_opts.zo_pool); 6339b24ab676SJeff Bonwick } 6340fa9e4066Sahrens 6341420dfc95SChris Siden if (ztest_opts.zo_verbose >= 1) { 6342420dfc95SChris Siden if (hasalt) { 6343420dfc95SChris Siden (void) printf("%d runs of older ztest: %s\n", older, 6344420dfc95SChris Siden ztest_opts.zo_alt_ztest); 6345420dfc95SChris Siden (void) printf("%d runs of newer ztest: %s\n", newer, 6346420dfc95SChris Siden cmd); 6347420dfc95SChris Siden } 6348fa9e4066Sahrens (void) printf("%d killed, %d completed, %.0f%% kill rate\n", 6349fa9e4066Sahrens kills, iters - kills, (100.0 * kills) / MAX(1, iters)); 6350fa9e4066Sahrens } 6351fa9e4066Sahrens 6352741652b0SEtienne Dechamps umem_free(cmd, MAXNAMELEN); 6353741652b0SEtienne Dechamps 6354fa9e4066Sahrens return (0); 6355fa9e4066Sahrens } 6356