1eda14cbcSMatt Macy /* 2eda14cbcSMatt Macy * CDDL HEADER START 3eda14cbcSMatt Macy * 4eda14cbcSMatt Macy * The contents of this file are subject to the terms of the 5eda14cbcSMatt Macy * Common Development and Distribution License (the "License"). 6eda14cbcSMatt Macy * You may not use this file except in compliance with the License. 7eda14cbcSMatt Macy * 8eda14cbcSMatt Macy * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9271171e0SMartin Matuska * or https://opensource.org/licenses/CDDL-1.0. 10eda14cbcSMatt Macy * See the License for the specific language governing permissions 11eda14cbcSMatt Macy * and limitations under the License. 12eda14cbcSMatt Macy * 13eda14cbcSMatt Macy * When distributing Covered Code, include this CDDL HEADER in each 14eda14cbcSMatt Macy * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15eda14cbcSMatt Macy * If applicable, add the following below this CDDL HEADER, with the 16eda14cbcSMatt Macy * fields enclosed by brackets "[]" replaced with your own identifying 17eda14cbcSMatt Macy * information: Portions Copyright [yyyy] [name of copyright owner] 18eda14cbcSMatt Macy * 19eda14cbcSMatt Macy * CDDL HEADER END 20eda14cbcSMatt Macy */ 21eda14cbcSMatt Macy /* 22eda14cbcSMatt Macy * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 237877fdebSMatt Macy * Copyright (c) 2011, 2020 by Delphix. All rights reserved. 24eda14cbcSMatt Macy * Copyright (c) 2013 Steven Hartland. All rights reserved. 25eda14cbcSMatt Macy * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved. 26eda14cbcSMatt Macy * Copyright 2016 Nexenta Systems, Inc. All rights reserved. 27eda14cbcSMatt Macy */ 28eda14cbcSMatt Macy 29eda14cbcSMatt Macy #include <sys/dsl_pool.h> 30eda14cbcSMatt Macy #include <sys/dsl_dataset.h> 31eda14cbcSMatt Macy #include <sys/dsl_prop.h> 32eda14cbcSMatt Macy #include <sys/dsl_dir.h> 33eda14cbcSMatt Macy #include <sys/dsl_synctask.h> 34eda14cbcSMatt Macy #include <sys/dsl_scan.h> 35eda14cbcSMatt Macy #include <sys/dnode.h> 36eda14cbcSMatt Macy #include <sys/dmu_tx.h> 37eda14cbcSMatt Macy #include <sys/dmu_objset.h> 38eda14cbcSMatt Macy #include <sys/arc.h> 39eda14cbcSMatt Macy #include <sys/zap.h> 40eda14cbcSMatt Macy #include <sys/zio.h> 41eda14cbcSMatt Macy #include <sys/zfs_context.h> 42eda14cbcSMatt Macy #include <sys/fs/zfs.h> 43eda14cbcSMatt Macy #include <sys/zfs_znode.h> 44eda14cbcSMatt Macy #include <sys/spa_impl.h> 45eda14cbcSMatt Macy #include <sys/vdev_impl.h> 46eda14cbcSMatt Macy #include <sys/metaslab_impl.h> 47eda14cbcSMatt Macy #include <sys/bptree.h> 48eda14cbcSMatt Macy #include <sys/zfeature.h> 49eda14cbcSMatt Macy #include <sys/zil_impl.h> 50eda14cbcSMatt Macy #include <sys/dsl_userhold.h> 51eda14cbcSMatt Macy #include <sys/trace_zfs.h> 52eda14cbcSMatt Macy #include <sys/mmp.h> 53eda14cbcSMatt Macy 54eda14cbcSMatt Macy /* 55eda14cbcSMatt Macy * ZFS Write Throttle 56eda14cbcSMatt Macy * ------------------ 57eda14cbcSMatt Macy * 58eda14cbcSMatt Macy * ZFS must limit the rate of incoming writes to the rate at which it is able 59eda14cbcSMatt Macy * to sync data modifications to the backend storage. Throttling by too much 60eda14cbcSMatt Macy * creates an artificial limit; throttling by too little can only be sustained 61eda14cbcSMatt Macy * for short periods and would lead to highly lumpy performance. On a per-pool 62eda14cbcSMatt Macy * basis, ZFS tracks the amount of modified (dirty) data. As operations change 63eda14cbcSMatt Macy * data, the amount of dirty data increases; as ZFS syncs out data, the amount 64eda14cbcSMatt Macy * of dirty data decreases. When the amount of dirty data exceeds a 65eda14cbcSMatt Macy * predetermined threshold further modifications are blocked until the amount 66eda14cbcSMatt Macy * of dirty data decreases (as data is synced out). 67eda14cbcSMatt Macy * 68eda14cbcSMatt Macy * The limit on dirty data is tunable, and should be adjusted according to 69eda14cbcSMatt Macy * both the IO capacity and available memory of the system. The larger the 70eda14cbcSMatt Macy * window, the more ZFS is able to aggregate and amortize metadata (and data) 71eda14cbcSMatt Macy * changes. However, memory is a limited resource, and allowing for more dirty 72eda14cbcSMatt Macy * data comes at the cost of keeping other useful data in memory (for example 73eda14cbcSMatt Macy * ZFS data cached by the ARC). 74eda14cbcSMatt Macy * 75eda14cbcSMatt Macy * Implementation 76eda14cbcSMatt Macy * 77eda14cbcSMatt Macy * As buffers are modified dsl_pool_willuse_space() increments both the per- 78eda14cbcSMatt Macy * txg (dp_dirty_pertxg[]) and poolwide (dp_dirty_total) accounting of 79eda14cbcSMatt Macy * dirty space used; dsl_pool_dirty_space() decrements those values as data 80eda14cbcSMatt Macy * is synced out from dsl_pool_sync(). While only the poolwide value is 81eda14cbcSMatt Macy * relevant, the per-txg value is useful for debugging. The tunable 82eda14cbcSMatt Macy * zfs_dirty_data_max determines the dirty space limit. Once that value is 83eda14cbcSMatt Macy * exceeded, new writes are halted until space frees up. 84eda14cbcSMatt Macy * 85eda14cbcSMatt Macy * The zfs_dirty_data_sync_percent tunable dictates the threshold at which we 86eda14cbcSMatt Macy * ensure that there is a txg syncing (see the comment in txg.c for a full 87eda14cbcSMatt Macy * description of transaction group stages). 88eda14cbcSMatt Macy * 89eda14cbcSMatt Macy * The IO scheduler uses both the dirty space limit and current amount of 90eda14cbcSMatt Macy * dirty data as inputs. Those values affect the number of concurrent IOs ZFS 91eda14cbcSMatt Macy * issues. See the comment in vdev_queue.c for details of the IO scheduler. 92eda14cbcSMatt Macy * 93eda14cbcSMatt Macy * The delay is also calculated based on the amount of dirty data. See the 94eda14cbcSMatt Macy * comment above dmu_tx_delay() for details. 95eda14cbcSMatt Macy */ 96eda14cbcSMatt Macy 97eda14cbcSMatt Macy /* 98eda14cbcSMatt Macy * zfs_dirty_data_max will be set to zfs_dirty_data_max_percent% of all memory, 99eda14cbcSMatt Macy * capped at zfs_dirty_data_max_max. It can also be overridden with a module 100eda14cbcSMatt Macy * parameter. 101eda14cbcSMatt Macy */ 102dbd5678dSMartin Matuska uint64_t zfs_dirty_data_max = 0; 103dbd5678dSMartin Matuska uint64_t zfs_dirty_data_max_max = 0; 104be181ee2SMartin Matuska uint_t zfs_dirty_data_max_percent = 10; 105be181ee2SMartin Matuska uint_t zfs_dirty_data_max_max_percent = 25; 106eda14cbcSMatt Macy 107eda14cbcSMatt Macy /* 108e3aa18adSMartin Matuska * The upper limit of TX_WRITE log data. Write operations are throttled 109e3aa18adSMartin Matuska * when approaching the limit until log data is cleared out after txg sync. 1103f9d360cSMartin Matuska * It only counts TX_WRITE log with WR_COPIED or WR_NEED_COPY. 1113f9d360cSMartin Matuska */ 112dbd5678dSMartin Matuska uint64_t zfs_wrlog_data_max = 0; 1133f9d360cSMartin Matuska 1143f9d360cSMartin Matuska /* 115eda14cbcSMatt Macy * If there's at least this much dirty data (as a percentage of 116eda14cbcSMatt Macy * zfs_dirty_data_max), push out a txg. This should be less than 117eda14cbcSMatt Macy * zfs_vdev_async_write_active_min_dirty_percent. 118eda14cbcSMatt Macy */ 119be181ee2SMartin Matuska static uint_t zfs_dirty_data_sync_percent = 20; 120eda14cbcSMatt Macy 121eda14cbcSMatt Macy /* 122eda14cbcSMatt Macy * Once there is this amount of dirty data, the dmu_tx_delay() will kick in 123eda14cbcSMatt Macy * and delay each transaction. 124eda14cbcSMatt Macy * This value should be >= zfs_vdev_async_write_active_max_dirty_percent. 125eda14cbcSMatt Macy */ 126be181ee2SMartin Matuska uint_t zfs_delay_min_dirty_percent = 60; 127eda14cbcSMatt Macy 128eda14cbcSMatt Macy /* 129eda14cbcSMatt Macy * This controls how quickly the delay approaches infinity. 130eda14cbcSMatt Macy * Larger values cause it to delay more for a given amount of dirty data. 131eda14cbcSMatt Macy * Therefore larger values will cause there to be less dirty data for a 132eda14cbcSMatt Macy * given throughput. 133eda14cbcSMatt Macy * 134eda14cbcSMatt Macy * For the smoothest delay, this value should be about 1 billion divided 135eda14cbcSMatt Macy * by the maximum number of operations per second. This will smoothly 136eda14cbcSMatt Macy * handle between 10x and 1/10th this number. 137eda14cbcSMatt Macy * 138eda14cbcSMatt Macy * Note: zfs_delay_scale * zfs_dirty_data_max must be < 2^64, due to the 139eda14cbcSMatt Macy * multiply in dmu_tx_delay(). 140eda14cbcSMatt Macy */ 141dbd5678dSMartin Matuska uint64_t zfs_delay_scale = 1000 * 1000 * 1000 / 2000; 142eda14cbcSMatt Macy 143eda14cbcSMatt Macy /* 144eda14cbcSMatt Macy * This determines the number of threads used by the dp_sync_taskq. 145eda14cbcSMatt Macy */ 146e92ffd9bSMartin Matuska static int zfs_sync_taskq_batch_pct = 75; 147eda14cbcSMatt Macy 148eda14cbcSMatt Macy /* 149eda14cbcSMatt Macy * These tunables determine the behavior of how zil_itxg_clean() is 150eda14cbcSMatt Macy * called via zil_clean() in the context of spa_sync(). When an itxg 151eda14cbcSMatt Macy * list needs to be cleaned, TQ_NOSLEEP will be used when dispatching. 152eda14cbcSMatt Macy * If the dispatch fails, the call to zil_itxg_clean() will occur 153eda14cbcSMatt Macy * synchronously in the context of spa_sync(), which can negatively 154eda14cbcSMatt Macy * impact the performance of spa_sync() (e.g. in the case of the itxg 155eda14cbcSMatt Macy * list having a large number of itxs that needs to be cleaned). 156eda14cbcSMatt Macy * 157eda14cbcSMatt Macy * Thus, these tunables can be used to manipulate the behavior of the 158eda14cbcSMatt Macy * taskq used by zil_clean(); they determine the number of taskq entries 159eda14cbcSMatt Macy * that are pre-populated when the taskq is first created (via the 160eda14cbcSMatt Macy * "zfs_zil_clean_taskq_minalloc" tunable) and the maximum number of 161eda14cbcSMatt Macy * taskq entries that are cached after an on-demand allocation (via the 162eda14cbcSMatt Macy * "zfs_zil_clean_taskq_maxalloc"). 163eda14cbcSMatt Macy * 164eda14cbcSMatt Macy * The idea being, we want to try reasonably hard to ensure there will 165eda14cbcSMatt Macy * already be a taskq entry pre-allocated by the time that it is needed 166eda14cbcSMatt Macy * by zil_clean(). This way, we can avoid the possibility of an 167eda14cbcSMatt Macy * on-demand allocation of a new taskq entry from failing, which would 168eda14cbcSMatt Macy * result in zil_itxg_clean() being called synchronously from zil_clean() 169eda14cbcSMatt Macy * (which can adversely affect performance of spa_sync()). 170eda14cbcSMatt Macy * 171eda14cbcSMatt Macy * Additionally, the number of threads used by the taskq can be 172eda14cbcSMatt Macy * configured via the "zfs_zil_clean_taskq_nthr_pct" tunable. 173eda14cbcSMatt Macy */ 174e92ffd9bSMartin Matuska static int zfs_zil_clean_taskq_nthr_pct = 100; 175e92ffd9bSMartin Matuska static int zfs_zil_clean_taskq_minalloc = 1024; 176e92ffd9bSMartin Matuska static int zfs_zil_clean_taskq_maxalloc = 1024 * 1024; 177eda14cbcSMatt Macy 178eda14cbcSMatt Macy int 179eda14cbcSMatt Macy dsl_pool_open_special_dir(dsl_pool_t *dp, const char *name, dsl_dir_t **ddp) 180eda14cbcSMatt Macy { 181eda14cbcSMatt Macy uint64_t obj; 182eda14cbcSMatt Macy int err; 183eda14cbcSMatt Macy 184eda14cbcSMatt Macy err = zap_lookup(dp->dp_meta_objset, 185eda14cbcSMatt Macy dsl_dir_phys(dp->dp_root_dir)->dd_child_dir_zapobj, 186eda14cbcSMatt Macy name, sizeof (obj), 1, &obj); 187eda14cbcSMatt Macy if (err) 188eda14cbcSMatt Macy return (err); 189eda14cbcSMatt Macy 190eda14cbcSMatt Macy return (dsl_dir_hold_obj(dp, obj, name, dp, ddp)); 191eda14cbcSMatt Macy } 192eda14cbcSMatt Macy 193eda14cbcSMatt Macy static dsl_pool_t * 194eda14cbcSMatt Macy dsl_pool_open_impl(spa_t *spa, uint64_t txg) 195eda14cbcSMatt Macy { 196eda14cbcSMatt Macy dsl_pool_t *dp; 197eda14cbcSMatt Macy blkptr_t *bp = spa_get_rootblkptr(spa); 198eda14cbcSMatt Macy 199eda14cbcSMatt Macy dp = kmem_zalloc(sizeof (dsl_pool_t), KM_SLEEP); 200eda14cbcSMatt Macy dp->dp_spa = spa; 201eda14cbcSMatt Macy dp->dp_meta_rootbp = *bp; 202eda14cbcSMatt Macy rrw_init(&dp->dp_config_rwlock, B_TRUE); 203eda14cbcSMatt Macy txg_init(dp, txg); 204eda14cbcSMatt Macy mmp_init(spa); 205eda14cbcSMatt Macy 206eda14cbcSMatt Macy txg_list_create(&dp->dp_dirty_datasets, spa, 207eda14cbcSMatt Macy offsetof(dsl_dataset_t, ds_dirty_link)); 208eda14cbcSMatt Macy txg_list_create(&dp->dp_dirty_zilogs, spa, 209eda14cbcSMatt Macy offsetof(zilog_t, zl_dirty_link)); 210eda14cbcSMatt Macy txg_list_create(&dp->dp_dirty_dirs, spa, 211eda14cbcSMatt Macy offsetof(dsl_dir_t, dd_dirty_link)); 212eda14cbcSMatt Macy txg_list_create(&dp->dp_sync_tasks, spa, 213eda14cbcSMatt Macy offsetof(dsl_sync_task_t, dst_node)); 214eda14cbcSMatt Macy txg_list_create(&dp->dp_early_sync_tasks, spa, 215eda14cbcSMatt Macy offsetof(dsl_sync_task_t, dst_node)); 216eda14cbcSMatt Macy 217eda14cbcSMatt Macy dp->dp_sync_taskq = taskq_create("dp_sync_taskq", 218eda14cbcSMatt Macy zfs_sync_taskq_batch_pct, minclsyspri, 1, INT_MAX, 219eda14cbcSMatt Macy TASKQ_THREADS_CPU_PCT); 220eda14cbcSMatt Macy 221eda14cbcSMatt Macy dp->dp_zil_clean_taskq = taskq_create("dp_zil_clean_taskq", 222eda14cbcSMatt Macy zfs_zil_clean_taskq_nthr_pct, minclsyspri, 223eda14cbcSMatt Macy zfs_zil_clean_taskq_minalloc, 224eda14cbcSMatt Macy zfs_zil_clean_taskq_maxalloc, 225eda14cbcSMatt Macy TASKQ_PREPOPULATE | TASKQ_THREADS_CPU_PCT); 226eda14cbcSMatt Macy 227eda14cbcSMatt Macy mutex_init(&dp->dp_lock, NULL, MUTEX_DEFAULT, NULL); 228eda14cbcSMatt Macy cv_init(&dp->dp_spaceavail_cv, NULL, CV_DEFAULT, NULL); 229eda14cbcSMatt Macy 2303f9d360cSMartin Matuska aggsum_init(&dp->dp_wrlog_total, 0); 2313f9d360cSMartin Matuska for (int i = 0; i < TXG_SIZE; i++) { 2323f9d360cSMartin Matuska aggsum_init(&dp->dp_wrlog_pertxg[i], 0); 2333f9d360cSMartin Matuska } 2343f9d360cSMartin Matuska 2357877fdebSMatt Macy dp->dp_zrele_taskq = taskq_create("z_zrele", 100, defclsyspri, 2367877fdebSMatt Macy boot_ncpus * 8, INT_MAX, TASKQ_PREPOPULATE | TASKQ_DYNAMIC | 2377877fdebSMatt Macy TASKQ_THREADS_CPU_PCT); 238eda14cbcSMatt Macy dp->dp_unlinked_drain_taskq = taskq_create("z_unlinked_drain", 2397877fdebSMatt Macy 100, defclsyspri, boot_ncpus, INT_MAX, 2407877fdebSMatt Macy TASKQ_PREPOPULATE | TASKQ_DYNAMIC | TASKQ_THREADS_CPU_PCT); 241eda14cbcSMatt Macy 242eda14cbcSMatt Macy return (dp); 243eda14cbcSMatt Macy } 244eda14cbcSMatt Macy 245eda14cbcSMatt Macy int 246eda14cbcSMatt Macy dsl_pool_init(spa_t *spa, uint64_t txg, dsl_pool_t **dpp) 247eda14cbcSMatt Macy { 248eda14cbcSMatt Macy int err; 249eda14cbcSMatt Macy dsl_pool_t *dp = dsl_pool_open_impl(spa, txg); 250eda14cbcSMatt Macy 251eda14cbcSMatt Macy /* 252eda14cbcSMatt Macy * Initialize the caller's dsl_pool_t structure before we actually open 253eda14cbcSMatt Macy * the meta objset. This is done because a self-healing write zio may 254eda14cbcSMatt Macy * be issued as part of dmu_objset_open_impl() and the spa needs its 255eda14cbcSMatt Macy * dsl_pool_t initialized in order to handle the write. 256eda14cbcSMatt Macy */ 257eda14cbcSMatt Macy *dpp = dp; 258eda14cbcSMatt Macy 259eda14cbcSMatt Macy err = dmu_objset_open_impl(spa, NULL, &dp->dp_meta_rootbp, 260eda14cbcSMatt Macy &dp->dp_meta_objset); 261eda14cbcSMatt Macy if (err != 0) { 262eda14cbcSMatt Macy dsl_pool_close(dp); 263eda14cbcSMatt Macy *dpp = NULL; 264eda14cbcSMatt Macy } 265eda14cbcSMatt Macy 266eda14cbcSMatt Macy return (err); 267eda14cbcSMatt Macy } 268eda14cbcSMatt Macy 269eda14cbcSMatt Macy int 270eda14cbcSMatt Macy dsl_pool_open(dsl_pool_t *dp) 271eda14cbcSMatt Macy { 272eda14cbcSMatt Macy int err; 273eda14cbcSMatt Macy dsl_dir_t *dd; 274eda14cbcSMatt Macy dsl_dataset_t *ds; 275eda14cbcSMatt Macy uint64_t obj; 276eda14cbcSMatt Macy 277eda14cbcSMatt Macy rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG); 278eda14cbcSMatt Macy err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 279eda14cbcSMatt Macy DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, 280eda14cbcSMatt Macy &dp->dp_root_dir_obj); 281eda14cbcSMatt Macy if (err) 282eda14cbcSMatt Macy goto out; 283eda14cbcSMatt Macy 284eda14cbcSMatt Macy err = dsl_dir_hold_obj(dp, dp->dp_root_dir_obj, 285eda14cbcSMatt Macy NULL, dp, &dp->dp_root_dir); 286eda14cbcSMatt Macy if (err) 287eda14cbcSMatt Macy goto out; 288eda14cbcSMatt Macy 289eda14cbcSMatt Macy err = dsl_pool_open_special_dir(dp, MOS_DIR_NAME, &dp->dp_mos_dir); 290eda14cbcSMatt Macy if (err) 291eda14cbcSMatt Macy goto out; 292eda14cbcSMatt Macy 293eda14cbcSMatt Macy if (spa_version(dp->dp_spa) >= SPA_VERSION_ORIGIN) { 294eda14cbcSMatt Macy err = dsl_pool_open_special_dir(dp, ORIGIN_DIR_NAME, &dd); 295eda14cbcSMatt Macy if (err) 296eda14cbcSMatt Macy goto out; 297eda14cbcSMatt Macy err = dsl_dataset_hold_obj(dp, 298eda14cbcSMatt Macy dsl_dir_phys(dd)->dd_head_dataset_obj, FTAG, &ds); 299eda14cbcSMatt Macy if (err == 0) { 300eda14cbcSMatt Macy err = dsl_dataset_hold_obj(dp, 301eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_prev_snap_obj, dp, 302eda14cbcSMatt Macy &dp->dp_origin_snap); 303eda14cbcSMatt Macy dsl_dataset_rele(ds, FTAG); 304eda14cbcSMatt Macy } 305eda14cbcSMatt Macy dsl_dir_rele(dd, dp); 306eda14cbcSMatt Macy if (err) 307eda14cbcSMatt Macy goto out; 308eda14cbcSMatt Macy } 309eda14cbcSMatt Macy 310eda14cbcSMatt Macy if (spa_version(dp->dp_spa) >= SPA_VERSION_DEADLISTS) { 311eda14cbcSMatt Macy err = dsl_pool_open_special_dir(dp, FREE_DIR_NAME, 312eda14cbcSMatt Macy &dp->dp_free_dir); 313eda14cbcSMatt Macy if (err) 314eda14cbcSMatt Macy goto out; 315eda14cbcSMatt Macy 316eda14cbcSMatt Macy err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 317eda14cbcSMatt Macy DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj); 318eda14cbcSMatt Macy if (err) 319eda14cbcSMatt Macy goto out; 320eda14cbcSMatt Macy VERIFY0(bpobj_open(&dp->dp_free_bpobj, 321eda14cbcSMatt Macy dp->dp_meta_objset, obj)); 322eda14cbcSMatt Macy } 323eda14cbcSMatt Macy 324eda14cbcSMatt Macy if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_OBSOLETE_COUNTS)) { 325eda14cbcSMatt Macy err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 326eda14cbcSMatt Macy DMU_POOL_OBSOLETE_BPOBJ, sizeof (uint64_t), 1, &obj); 327eda14cbcSMatt Macy if (err == 0) { 328eda14cbcSMatt Macy VERIFY0(bpobj_open(&dp->dp_obsolete_bpobj, 329eda14cbcSMatt Macy dp->dp_meta_objset, obj)); 330eda14cbcSMatt Macy } else if (err == ENOENT) { 331eda14cbcSMatt Macy /* 332eda14cbcSMatt Macy * We might not have created the remap bpobj yet. 333eda14cbcSMatt Macy */ 334eda14cbcSMatt Macy } else { 335eda14cbcSMatt Macy goto out; 336eda14cbcSMatt Macy } 337eda14cbcSMatt Macy } 338eda14cbcSMatt Macy 339eda14cbcSMatt Macy /* 340eda14cbcSMatt Macy * Note: errors ignored, because the these special dirs, used for 341eda14cbcSMatt Macy * space accounting, are only created on demand. 342eda14cbcSMatt Macy */ 343eda14cbcSMatt Macy (void) dsl_pool_open_special_dir(dp, LEAK_DIR_NAME, 344eda14cbcSMatt Macy &dp->dp_leak_dir); 345eda14cbcSMatt Macy 346eda14cbcSMatt Macy if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_ASYNC_DESTROY)) { 347eda14cbcSMatt Macy err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 348eda14cbcSMatt Macy DMU_POOL_BPTREE_OBJ, sizeof (uint64_t), 1, 349eda14cbcSMatt Macy &dp->dp_bptree_obj); 350eda14cbcSMatt Macy if (err != 0) 351eda14cbcSMatt Macy goto out; 352eda14cbcSMatt Macy } 353eda14cbcSMatt Macy 354eda14cbcSMatt Macy if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_EMPTY_BPOBJ)) { 355eda14cbcSMatt Macy err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 356eda14cbcSMatt Macy DMU_POOL_EMPTY_BPOBJ, sizeof (uint64_t), 1, 357eda14cbcSMatt Macy &dp->dp_empty_bpobj); 358eda14cbcSMatt Macy if (err != 0) 359eda14cbcSMatt Macy goto out; 360eda14cbcSMatt Macy } 361eda14cbcSMatt Macy 362eda14cbcSMatt Macy err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 363eda14cbcSMatt Macy DMU_POOL_TMP_USERREFS, sizeof (uint64_t), 1, 364eda14cbcSMatt Macy &dp->dp_tmp_userrefs_obj); 365eda14cbcSMatt Macy if (err == ENOENT) 366eda14cbcSMatt Macy err = 0; 367eda14cbcSMatt Macy if (err) 368eda14cbcSMatt Macy goto out; 369eda14cbcSMatt Macy 370eda14cbcSMatt Macy err = dsl_scan_init(dp, dp->dp_tx.tx_open_txg); 371eda14cbcSMatt Macy 372eda14cbcSMatt Macy out: 373eda14cbcSMatt Macy rrw_exit(&dp->dp_config_rwlock, FTAG); 374eda14cbcSMatt Macy return (err); 375eda14cbcSMatt Macy } 376eda14cbcSMatt Macy 377eda14cbcSMatt Macy void 378eda14cbcSMatt Macy dsl_pool_close(dsl_pool_t *dp) 379eda14cbcSMatt Macy { 380eda14cbcSMatt Macy /* 381eda14cbcSMatt Macy * Drop our references from dsl_pool_open(). 382eda14cbcSMatt Macy * 383eda14cbcSMatt Macy * Since we held the origin_snap from "syncing" context (which 384eda14cbcSMatt Macy * includes pool-opening context), it actually only got a "ref" 385eda14cbcSMatt Macy * and not a hold, so just drop that here. 386eda14cbcSMatt Macy */ 387eda14cbcSMatt Macy if (dp->dp_origin_snap != NULL) 388eda14cbcSMatt Macy dsl_dataset_rele(dp->dp_origin_snap, dp); 389eda14cbcSMatt Macy if (dp->dp_mos_dir != NULL) 390eda14cbcSMatt Macy dsl_dir_rele(dp->dp_mos_dir, dp); 391eda14cbcSMatt Macy if (dp->dp_free_dir != NULL) 392eda14cbcSMatt Macy dsl_dir_rele(dp->dp_free_dir, dp); 393eda14cbcSMatt Macy if (dp->dp_leak_dir != NULL) 394eda14cbcSMatt Macy dsl_dir_rele(dp->dp_leak_dir, dp); 395eda14cbcSMatt Macy if (dp->dp_root_dir != NULL) 396eda14cbcSMatt Macy dsl_dir_rele(dp->dp_root_dir, dp); 397eda14cbcSMatt Macy 398eda14cbcSMatt Macy bpobj_close(&dp->dp_free_bpobj); 399eda14cbcSMatt Macy bpobj_close(&dp->dp_obsolete_bpobj); 400eda14cbcSMatt Macy 401eda14cbcSMatt Macy /* undo the dmu_objset_open_impl(mos) from dsl_pool_open() */ 402eda14cbcSMatt Macy if (dp->dp_meta_objset != NULL) 403eda14cbcSMatt Macy dmu_objset_evict(dp->dp_meta_objset); 404eda14cbcSMatt Macy 405eda14cbcSMatt Macy txg_list_destroy(&dp->dp_dirty_datasets); 406eda14cbcSMatt Macy txg_list_destroy(&dp->dp_dirty_zilogs); 407eda14cbcSMatt Macy txg_list_destroy(&dp->dp_sync_tasks); 408eda14cbcSMatt Macy txg_list_destroy(&dp->dp_early_sync_tasks); 409eda14cbcSMatt Macy txg_list_destroy(&dp->dp_dirty_dirs); 410eda14cbcSMatt Macy 411eda14cbcSMatt Macy taskq_destroy(dp->dp_zil_clean_taskq); 412eda14cbcSMatt Macy taskq_destroy(dp->dp_sync_taskq); 413eda14cbcSMatt Macy 414eda14cbcSMatt Macy /* 415eda14cbcSMatt Macy * We can't set retry to TRUE since we're explicitly specifying 416eda14cbcSMatt Macy * a spa to flush. This is good enough; any missed buffers for 417eda14cbcSMatt Macy * this spa won't cause trouble, and they'll eventually fall 418eda14cbcSMatt Macy * out of the ARC just like any other unused buffer. 419eda14cbcSMatt Macy */ 420eda14cbcSMatt Macy arc_flush(dp->dp_spa, FALSE); 421eda14cbcSMatt Macy 422eda14cbcSMatt Macy mmp_fini(dp->dp_spa); 423eda14cbcSMatt Macy txg_fini(dp); 424eda14cbcSMatt Macy dsl_scan_fini(dp); 425eda14cbcSMatt Macy dmu_buf_user_evict_wait(); 426eda14cbcSMatt Macy 427eda14cbcSMatt Macy rrw_destroy(&dp->dp_config_rwlock); 428eda14cbcSMatt Macy mutex_destroy(&dp->dp_lock); 429eda14cbcSMatt Macy cv_destroy(&dp->dp_spaceavail_cv); 4303f9d360cSMartin Matuska 4313f9d360cSMartin Matuska ASSERT0(aggsum_value(&dp->dp_wrlog_total)); 4323f9d360cSMartin Matuska aggsum_fini(&dp->dp_wrlog_total); 4333f9d360cSMartin Matuska for (int i = 0; i < TXG_SIZE; i++) { 4343f9d360cSMartin Matuska ASSERT0(aggsum_value(&dp->dp_wrlog_pertxg[i])); 4353f9d360cSMartin Matuska aggsum_fini(&dp->dp_wrlog_pertxg[i]); 4363f9d360cSMartin Matuska } 4373f9d360cSMartin Matuska 438eda14cbcSMatt Macy taskq_destroy(dp->dp_unlinked_drain_taskq); 439eda14cbcSMatt Macy taskq_destroy(dp->dp_zrele_taskq); 440a0b956f5SMartin Matuska if (dp->dp_blkstats != NULL) 441eda14cbcSMatt Macy vmem_free(dp->dp_blkstats, sizeof (zfs_all_blkstats_t)); 442eda14cbcSMatt Macy kmem_free(dp, sizeof (dsl_pool_t)); 443eda14cbcSMatt Macy } 444eda14cbcSMatt Macy 445eda14cbcSMatt Macy void 446eda14cbcSMatt Macy dsl_pool_create_obsolete_bpobj(dsl_pool_t *dp, dmu_tx_t *tx) 447eda14cbcSMatt Macy { 448eda14cbcSMatt Macy uint64_t obj; 449eda14cbcSMatt Macy /* 450eda14cbcSMatt Macy * Currently, we only create the obsolete_bpobj where there are 451eda14cbcSMatt Macy * indirect vdevs with referenced mappings. 452eda14cbcSMatt Macy */ 453eda14cbcSMatt Macy ASSERT(spa_feature_is_active(dp->dp_spa, SPA_FEATURE_DEVICE_REMOVAL)); 454eda14cbcSMatt Macy /* create and open the obsolete_bpobj */ 455eda14cbcSMatt Macy obj = bpobj_alloc(dp->dp_meta_objset, SPA_OLD_MAXBLOCKSIZE, tx); 456eda14cbcSMatt Macy VERIFY0(bpobj_open(&dp->dp_obsolete_bpobj, dp->dp_meta_objset, obj)); 457eda14cbcSMatt Macy VERIFY0(zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 458eda14cbcSMatt Macy DMU_POOL_OBSOLETE_BPOBJ, sizeof (uint64_t), 1, &obj, tx)); 459eda14cbcSMatt Macy spa_feature_incr(dp->dp_spa, SPA_FEATURE_OBSOLETE_COUNTS, tx); 460eda14cbcSMatt Macy } 461eda14cbcSMatt Macy 462eda14cbcSMatt Macy void 463eda14cbcSMatt Macy dsl_pool_destroy_obsolete_bpobj(dsl_pool_t *dp, dmu_tx_t *tx) 464eda14cbcSMatt Macy { 465eda14cbcSMatt Macy spa_feature_decr(dp->dp_spa, SPA_FEATURE_OBSOLETE_COUNTS, tx); 466eda14cbcSMatt Macy VERIFY0(zap_remove(dp->dp_meta_objset, 467eda14cbcSMatt Macy DMU_POOL_DIRECTORY_OBJECT, 468eda14cbcSMatt Macy DMU_POOL_OBSOLETE_BPOBJ, tx)); 469eda14cbcSMatt Macy bpobj_free(dp->dp_meta_objset, 470eda14cbcSMatt Macy dp->dp_obsolete_bpobj.bpo_object, tx); 471eda14cbcSMatt Macy bpobj_close(&dp->dp_obsolete_bpobj); 472eda14cbcSMatt Macy } 473eda14cbcSMatt Macy 474eda14cbcSMatt Macy dsl_pool_t * 475e92ffd9bSMartin Matuska dsl_pool_create(spa_t *spa, nvlist_t *zplprops __attribute__((unused)), 476e92ffd9bSMartin Matuska dsl_crypto_params_t *dcp, uint64_t txg) 477eda14cbcSMatt Macy { 478eda14cbcSMatt Macy int err; 479eda14cbcSMatt Macy dsl_pool_t *dp = dsl_pool_open_impl(spa, txg); 480eda14cbcSMatt Macy dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg); 481eda14cbcSMatt Macy #ifdef _KERNEL 482eda14cbcSMatt Macy objset_t *os; 483eda14cbcSMatt Macy #else 484eda14cbcSMatt Macy objset_t *os __attribute__((unused)); 485eda14cbcSMatt Macy #endif 486eda14cbcSMatt Macy dsl_dataset_t *ds; 487eda14cbcSMatt Macy uint64_t obj; 488eda14cbcSMatt Macy 489eda14cbcSMatt Macy rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG); 490eda14cbcSMatt Macy 491eda14cbcSMatt Macy /* create and open the MOS (meta-objset) */ 492eda14cbcSMatt Macy dp->dp_meta_objset = dmu_objset_create_impl(spa, 493eda14cbcSMatt Macy NULL, &dp->dp_meta_rootbp, DMU_OST_META, tx); 494eda14cbcSMatt Macy spa->spa_meta_objset = dp->dp_meta_objset; 495eda14cbcSMatt Macy 496eda14cbcSMatt Macy /* create the pool directory */ 497eda14cbcSMatt Macy err = zap_create_claim(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 498eda14cbcSMatt Macy DMU_OT_OBJECT_DIRECTORY, DMU_OT_NONE, 0, tx); 499eda14cbcSMatt Macy ASSERT0(err); 500eda14cbcSMatt Macy 501eda14cbcSMatt Macy /* Initialize scan structures */ 502eda14cbcSMatt Macy VERIFY0(dsl_scan_init(dp, txg)); 503eda14cbcSMatt Macy 504eda14cbcSMatt Macy /* create and open the root dir */ 505eda14cbcSMatt Macy dp->dp_root_dir_obj = dsl_dir_create_sync(dp, NULL, NULL, tx); 506eda14cbcSMatt Macy VERIFY0(dsl_dir_hold_obj(dp, dp->dp_root_dir_obj, 507eda14cbcSMatt Macy NULL, dp, &dp->dp_root_dir)); 508eda14cbcSMatt Macy 509eda14cbcSMatt Macy /* create and open the meta-objset dir */ 510eda14cbcSMatt Macy (void) dsl_dir_create_sync(dp, dp->dp_root_dir, MOS_DIR_NAME, tx); 511eda14cbcSMatt Macy VERIFY0(dsl_pool_open_special_dir(dp, 512eda14cbcSMatt Macy MOS_DIR_NAME, &dp->dp_mos_dir)); 513eda14cbcSMatt Macy 514eda14cbcSMatt Macy if (spa_version(spa) >= SPA_VERSION_DEADLISTS) { 515eda14cbcSMatt Macy /* create and open the free dir */ 516eda14cbcSMatt Macy (void) dsl_dir_create_sync(dp, dp->dp_root_dir, 517eda14cbcSMatt Macy FREE_DIR_NAME, tx); 518eda14cbcSMatt Macy VERIFY0(dsl_pool_open_special_dir(dp, 519eda14cbcSMatt Macy FREE_DIR_NAME, &dp->dp_free_dir)); 520eda14cbcSMatt Macy 521eda14cbcSMatt Macy /* create and open the free_bplist */ 522eda14cbcSMatt Macy obj = bpobj_alloc(dp->dp_meta_objset, SPA_OLD_MAXBLOCKSIZE, tx); 523eda14cbcSMatt Macy VERIFY(zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 524eda14cbcSMatt Macy DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj, tx) == 0); 525eda14cbcSMatt Macy VERIFY0(bpobj_open(&dp->dp_free_bpobj, 526eda14cbcSMatt Macy dp->dp_meta_objset, obj)); 527eda14cbcSMatt Macy } 528eda14cbcSMatt Macy 529eda14cbcSMatt Macy if (spa_version(spa) >= SPA_VERSION_DSL_SCRUB) 530eda14cbcSMatt Macy dsl_pool_create_origin(dp, tx); 531eda14cbcSMatt Macy 532eda14cbcSMatt Macy /* 533eda14cbcSMatt Macy * Some features may be needed when creating the root dataset, so we 534eda14cbcSMatt Macy * create the feature objects here. 535eda14cbcSMatt Macy */ 536eda14cbcSMatt Macy if (spa_version(spa) >= SPA_VERSION_FEATURES) 537eda14cbcSMatt Macy spa_feature_create_zap_objects(spa, tx); 538eda14cbcSMatt Macy 539eda14cbcSMatt Macy if (dcp != NULL && dcp->cp_crypt != ZIO_CRYPT_OFF && 540eda14cbcSMatt Macy dcp->cp_crypt != ZIO_CRYPT_INHERIT) 541eda14cbcSMatt Macy spa_feature_enable(spa, SPA_FEATURE_ENCRYPTION, tx); 542eda14cbcSMatt Macy 543eda14cbcSMatt Macy /* create the root dataset */ 544eda14cbcSMatt Macy obj = dsl_dataset_create_sync_dd(dp->dp_root_dir, NULL, dcp, 0, tx); 545eda14cbcSMatt Macy 546eda14cbcSMatt Macy /* create the root objset */ 547eda14cbcSMatt Macy VERIFY0(dsl_dataset_hold_obj_flags(dp, obj, 548eda14cbcSMatt Macy DS_HOLD_FLAG_DECRYPT, FTAG, &ds)); 549eda14cbcSMatt Macy rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG); 550eda14cbcSMatt Macy os = dmu_objset_create_impl(dp->dp_spa, ds, 551eda14cbcSMatt Macy dsl_dataset_get_blkptr(ds), DMU_OST_ZFS, tx); 552eda14cbcSMatt Macy rrw_exit(&ds->ds_bp_rwlock, FTAG); 553eda14cbcSMatt Macy #ifdef _KERNEL 554eda14cbcSMatt Macy zfs_create_fs(os, kcred, zplprops, tx); 555eda14cbcSMatt Macy #endif 556eda14cbcSMatt Macy dsl_dataset_rele_flags(ds, DS_HOLD_FLAG_DECRYPT, FTAG); 557eda14cbcSMatt Macy 558eda14cbcSMatt Macy dmu_tx_commit(tx); 559eda14cbcSMatt Macy 560eda14cbcSMatt Macy rrw_exit(&dp->dp_config_rwlock, FTAG); 561eda14cbcSMatt Macy 562eda14cbcSMatt Macy return (dp); 563eda14cbcSMatt Macy } 564eda14cbcSMatt Macy 565eda14cbcSMatt Macy /* 566eda14cbcSMatt Macy * Account for the meta-objset space in its placeholder dsl_dir. 567eda14cbcSMatt Macy */ 568eda14cbcSMatt Macy void 569eda14cbcSMatt Macy dsl_pool_mos_diduse_space(dsl_pool_t *dp, 570eda14cbcSMatt Macy int64_t used, int64_t comp, int64_t uncomp) 571eda14cbcSMatt Macy { 572eda14cbcSMatt Macy ASSERT3U(comp, ==, uncomp); /* it's all metadata */ 573eda14cbcSMatt Macy mutex_enter(&dp->dp_lock); 574eda14cbcSMatt Macy dp->dp_mos_used_delta += used; 575eda14cbcSMatt Macy dp->dp_mos_compressed_delta += comp; 576eda14cbcSMatt Macy dp->dp_mos_uncompressed_delta += uncomp; 577eda14cbcSMatt Macy mutex_exit(&dp->dp_lock); 578eda14cbcSMatt Macy } 579eda14cbcSMatt Macy 580eda14cbcSMatt Macy static void 581eda14cbcSMatt Macy dsl_pool_sync_mos(dsl_pool_t *dp, dmu_tx_t *tx) 582eda14cbcSMatt Macy { 583eda14cbcSMatt Macy zio_t *zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 584eda14cbcSMatt Macy dmu_objset_sync(dp->dp_meta_objset, zio, tx); 585eda14cbcSMatt Macy VERIFY0(zio_wait(zio)); 5867877fdebSMatt Macy dmu_objset_sync_done(dp->dp_meta_objset, tx); 5877877fdebSMatt Macy taskq_wait(dp->dp_sync_taskq); 5883ff01b23SMartin Matuska multilist_destroy(&dp->dp_meta_objset->os_synced_dnodes); 5897877fdebSMatt Macy 590eda14cbcSMatt Macy dprintf_bp(&dp->dp_meta_rootbp, "meta objset rootbp is %s", ""); 591eda14cbcSMatt Macy spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp); 592eda14cbcSMatt Macy } 593eda14cbcSMatt Macy 594eda14cbcSMatt Macy static void 595eda14cbcSMatt Macy dsl_pool_dirty_delta(dsl_pool_t *dp, int64_t delta) 596eda14cbcSMatt Macy { 597eda14cbcSMatt Macy ASSERT(MUTEX_HELD(&dp->dp_lock)); 598eda14cbcSMatt Macy 599eda14cbcSMatt Macy if (delta < 0) 600eda14cbcSMatt Macy ASSERT3U(-delta, <=, dp->dp_dirty_total); 601eda14cbcSMatt Macy 602eda14cbcSMatt Macy dp->dp_dirty_total += delta; 603eda14cbcSMatt Macy 604eda14cbcSMatt Macy /* 605eda14cbcSMatt Macy * Note: we signal even when increasing dp_dirty_total. 606eda14cbcSMatt Macy * This ensures forward progress -- each thread wakes the next waiter. 607eda14cbcSMatt Macy */ 608eda14cbcSMatt Macy if (dp->dp_dirty_total < zfs_dirty_data_max) 609eda14cbcSMatt Macy cv_signal(&dp->dp_spaceavail_cv); 610eda14cbcSMatt Macy } 611eda14cbcSMatt Macy 6123f9d360cSMartin Matuska void 6133f9d360cSMartin Matuska dsl_pool_wrlog_count(dsl_pool_t *dp, int64_t size, uint64_t txg) 6143f9d360cSMartin Matuska { 6153f9d360cSMartin Matuska ASSERT3S(size, >=, 0); 6163f9d360cSMartin Matuska 6173f9d360cSMartin Matuska aggsum_add(&dp->dp_wrlog_pertxg[txg & TXG_MASK], size); 6183f9d360cSMartin Matuska aggsum_add(&dp->dp_wrlog_total, size); 6193f9d360cSMartin Matuska 6203f9d360cSMartin Matuska /* Choose a value slightly bigger than min dirty sync bytes */ 6213f9d360cSMartin Matuska uint64_t sync_min = 622e3aa18adSMartin Matuska zfs_wrlog_data_max * (zfs_dirty_data_sync_percent + 10) / 200; 6233f9d360cSMartin Matuska if (aggsum_compare(&dp->dp_wrlog_pertxg[txg & TXG_MASK], sync_min) > 0) 6243f9d360cSMartin Matuska txg_kick(dp, txg); 6253f9d360cSMartin Matuska } 6263f9d360cSMartin Matuska 6273f9d360cSMartin Matuska boolean_t 628e3aa18adSMartin Matuska dsl_pool_need_wrlog_delay(dsl_pool_t *dp) 6293f9d360cSMartin Matuska { 630e3aa18adSMartin Matuska uint64_t delay_min_bytes = 631e3aa18adSMartin Matuska zfs_wrlog_data_max * zfs_delay_min_dirty_percent / 100; 632e3aa18adSMartin Matuska 633e3aa18adSMartin Matuska return (aggsum_compare(&dp->dp_wrlog_total, delay_min_bytes) > 0); 6343f9d360cSMartin Matuska } 6353f9d360cSMartin Matuska 6363f9d360cSMartin Matuska static void 6373f9d360cSMartin Matuska dsl_pool_wrlog_clear(dsl_pool_t *dp, uint64_t txg) 6383f9d360cSMartin Matuska { 6393f9d360cSMartin Matuska int64_t delta; 6403f9d360cSMartin Matuska delta = -(int64_t)aggsum_value(&dp->dp_wrlog_pertxg[txg & TXG_MASK]); 6413f9d360cSMartin Matuska aggsum_add(&dp->dp_wrlog_pertxg[txg & TXG_MASK], delta); 6423f9d360cSMartin Matuska aggsum_add(&dp->dp_wrlog_total, delta); 643e3aa18adSMartin Matuska /* Compact per-CPU sums after the big change. */ 644e3aa18adSMartin Matuska (void) aggsum_value(&dp->dp_wrlog_pertxg[txg & TXG_MASK]); 645e3aa18adSMartin Matuska (void) aggsum_value(&dp->dp_wrlog_total); 6463f9d360cSMartin Matuska } 6473f9d360cSMartin Matuska 648eda14cbcSMatt Macy #ifdef ZFS_DEBUG 649eda14cbcSMatt Macy static boolean_t 650eda14cbcSMatt Macy dsl_early_sync_task_verify(dsl_pool_t *dp, uint64_t txg) 651eda14cbcSMatt Macy { 652eda14cbcSMatt Macy spa_t *spa = dp->dp_spa; 653eda14cbcSMatt Macy vdev_t *rvd = spa->spa_root_vdev; 654eda14cbcSMatt Macy 655eda14cbcSMatt Macy for (uint64_t c = 0; c < rvd->vdev_children; c++) { 656eda14cbcSMatt Macy vdev_t *vd = rvd->vdev_child[c]; 657eda14cbcSMatt Macy txg_list_t *tl = &vd->vdev_ms_list; 658eda14cbcSMatt Macy metaslab_t *ms; 659eda14cbcSMatt Macy 660eda14cbcSMatt Macy for (ms = txg_list_head(tl, TXG_CLEAN(txg)); ms; 661eda14cbcSMatt Macy ms = txg_list_next(tl, ms, TXG_CLEAN(txg))) { 662eda14cbcSMatt Macy VERIFY(range_tree_is_empty(ms->ms_freeing)); 663eda14cbcSMatt Macy VERIFY(range_tree_is_empty(ms->ms_checkpointing)); 664eda14cbcSMatt Macy } 665eda14cbcSMatt Macy } 666eda14cbcSMatt Macy 667eda14cbcSMatt Macy return (B_TRUE); 668eda14cbcSMatt Macy } 669e92ffd9bSMartin Matuska #else 670e92ffd9bSMartin Matuska #define dsl_early_sync_task_verify(dp, txg) \ 671e92ffd9bSMartin Matuska ((void) sizeof (dp), (void) sizeof (txg), B_TRUE) 672eda14cbcSMatt Macy #endif 673eda14cbcSMatt Macy 674eda14cbcSMatt Macy void 675eda14cbcSMatt Macy dsl_pool_sync(dsl_pool_t *dp, uint64_t txg) 676eda14cbcSMatt Macy { 677eda14cbcSMatt Macy zio_t *zio; 678eda14cbcSMatt Macy dmu_tx_t *tx; 679eda14cbcSMatt Macy dsl_dir_t *dd; 680eda14cbcSMatt Macy dsl_dataset_t *ds; 681eda14cbcSMatt Macy objset_t *mos = dp->dp_meta_objset; 682eda14cbcSMatt Macy list_t synced_datasets; 683eda14cbcSMatt Macy 684eda14cbcSMatt Macy list_create(&synced_datasets, sizeof (dsl_dataset_t), 685eda14cbcSMatt Macy offsetof(dsl_dataset_t, ds_synced_link)); 686eda14cbcSMatt Macy 687eda14cbcSMatt Macy tx = dmu_tx_create_assigned(dp, txg); 688eda14cbcSMatt Macy 689eda14cbcSMatt Macy /* 690eda14cbcSMatt Macy * Run all early sync tasks before writing out any dirty blocks. 691eda14cbcSMatt Macy * For more info on early sync tasks see block comment in 692eda14cbcSMatt Macy * dsl_early_sync_task(). 693eda14cbcSMatt Macy */ 694eda14cbcSMatt Macy if (!txg_list_empty(&dp->dp_early_sync_tasks, txg)) { 695eda14cbcSMatt Macy dsl_sync_task_t *dst; 696eda14cbcSMatt Macy 697eda14cbcSMatt Macy ASSERT3U(spa_sync_pass(dp->dp_spa), ==, 1); 698eda14cbcSMatt Macy while ((dst = 699eda14cbcSMatt Macy txg_list_remove(&dp->dp_early_sync_tasks, txg)) != NULL) { 700eda14cbcSMatt Macy ASSERT(dsl_early_sync_task_verify(dp, txg)); 701eda14cbcSMatt Macy dsl_sync_task_sync(dst, tx); 702eda14cbcSMatt Macy } 703eda14cbcSMatt Macy ASSERT(dsl_early_sync_task_verify(dp, txg)); 704eda14cbcSMatt Macy } 705eda14cbcSMatt Macy 706eda14cbcSMatt Macy /* 707eda14cbcSMatt Macy * Write out all dirty blocks of dirty datasets. 708eda14cbcSMatt Macy */ 709eda14cbcSMatt Macy zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 710eda14cbcSMatt Macy while ((ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) != NULL) { 711eda14cbcSMatt Macy /* 712eda14cbcSMatt Macy * We must not sync any non-MOS datasets twice, because 713eda14cbcSMatt Macy * we may have taken a snapshot of them. However, we 714eda14cbcSMatt Macy * may sync newly-created datasets on pass 2. 715eda14cbcSMatt Macy */ 716eda14cbcSMatt Macy ASSERT(!list_link_active(&ds->ds_synced_link)); 717eda14cbcSMatt Macy list_insert_tail(&synced_datasets, ds); 718eda14cbcSMatt Macy dsl_dataset_sync(ds, zio, tx); 719eda14cbcSMatt Macy } 720eda14cbcSMatt Macy VERIFY0(zio_wait(zio)); 721eda14cbcSMatt Macy 722eda14cbcSMatt Macy /* 723eda14cbcSMatt Macy * Update the long range free counter after 724eda14cbcSMatt Macy * we're done syncing user data 725eda14cbcSMatt Macy */ 726eda14cbcSMatt Macy mutex_enter(&dp->dp_lock); 727eda14cbcSMatt Macy ASSERT(spa_sync_pass(dp->dp_spa) == 1 || 728eda14cbcSMatt Macy dp->dp_long_free_dirty_pertxg[txg & TXG_MASK] == 0); 729eda14cbcSMatt Macy dp->dp_long_free_dirty_pertxg[txg & TXG_MASK] = 0; 730eda14cbcSMatt Macy mutex_exit(&dp->dp_lock); 731eda14cbcSMatt Macy 732eda14cbcSMatt Macy /* 733eda14cbcSMatt Macy * After the data blocks have been written (ensured by the zio_wait() 734eda14cbcSMatt Macy * above), update the user/group/project space accounting. This happens 735eda14cbcSMatt Macy * in tasks dispatched to dp_sync_taskq, so wait for them before 736eda14cbcSMatt Macy * continuing. 737eda14cbcSMatt Macy */ 738eda14cbcSMatt Macy for (ds = list_head(&synced_datasets); ds != NULL; 739eda14cbcSMatt Macy ds = list_next(&synced_datasets, ds)) { 7407877fdebSMatt Macy dmu_objset_sync_done(ds->ds_objset, tx); 741eda14cbcSMatt Macy } 742eda14cbcSMatt Macy taskq_wait(dp->dp_sync_taskq); 743eda14cbcSMatt Macy 744eda14cbcSMatt Macy /* 745eda14cbcSMatt Macy * Sync the datasets again to push out the changes due to 746eda14cbcSMatt Macy * userspace updates. This must be done before we process the 747eda14cbcSMatt Macy * sync tasks, so that any snapshots will have the correct 748eda14cbcSMatt Macy * user accounting information (and we won't get confused 749eda14cbcSMatt Macy * about which blocks are part of the snapshot). 750eda14cbcSMatt Macy */ 751eda14cbcSMatt Macy zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 752eda14cbcSMatt Macy while ((ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) != NULL) { 753eda14cbcSMatt Macy objset_t *os = ds->ds_objset; 754eda14cbcSMatt Macy 755eda14cbcSMatt Macy ASSERT(list_link_active(&ds->ds_synced_link)); 756eda14cbcSMatt Macy dmu_buf_rele(ds->ds_dbuf, ds); 757eda14cbcSMatt Macy dsl_dataset_sync(ds, zio, tx); 758eda14cbcSMatt Macy 759eda14cbcSMatt Macy /* 760eda14cbcSMatt Macy * Release any key mappings created by calls to 761eda14cbcSMatt Macy * dsl_dataset_dirty() from the userquota accounting 762eda14cbcSMatt Macy * code paths. 763eda14cbcSMatt Macy */ 764eda14cbcSMatt Macy if (os->os_encrypted && !os->os_raw_receive && 765eda14cbcSMatt Macy !os->os_next_write_raw[txg & TXG_MASK]) { 766eda14cbcSMatt Macy ASSERT3P(ds->ds_key_mapping, !=, NULL); 767eda14cbcSMatt Macy key_mapping_rele(dp->dp_spa, ds->ds_key_mapping, ds); 768eda14cbcSMatt Macy } 769eda14cbcSMatt Macy } 770eda14cbcSMatt Macy VERIFY0(zio_wait(zio)); 771eda14cbcSMatt Macy 772eda14cbcSMatt Macy /* 773eda14cbcSMatt Macy * Now that the datasets have been completely synced, we can 774eda14cbcSMatt Macy * clean up our in-memory structures accumulated while syncing: 775eda14cbcSMatt Macy * 776eda14cbcSMatt Macy * - move dead blocks from the pending deadlist and livelists 777eda14cbcSMatt Macy * to the on-disk versions 778eda14cbcSMatt Macy * - release hold from dsl_dataset_dirty() 779eda14cbcSMatt Macy * - release key mapping hold from dsl_dataset_dirty() 780eda14cbcSMatt Macy */ 781eda14cbcSMatt Macy while ((ds = list_remove_head(&synced_datasets)) != NULL) { 782eda14cbcSMatt Macy objset_t *os = ds->ds_objset; 783eda14cbcSMatt Macy 784eda14cbcSMatt Macy if (os->os_encrypted && !os->os_raw_receive && 785eda14cbcSMatt Macy !os->os_next_write_raw[txg & TXG_MASK]) { 786eda14cbcSMatt Macy ASSERT3P(ds->ds_key_mapping, !=, NULL); 787eda14cbcSMatt Macy key_mapping_rele(dp->dp_spa, ds->ds_key_mapping, ds); 788eda14cbcSMatt Macy } 789eda14cbcSMatt Macy 790eda14cbcSMatt Macy dsl_dataset_sync_done(ds, tx); 791*2a58b312SMartin Matuska dmu_buf_rele(ds->ds_dbuf, ds); 792eda14cbcSMatt Macy } 793eda14cbcSMatt Macy 794eda14cbcSMatt Macy while ((dd = txg_list_remove(&dp->dp_dirty_dirs, txg)) != NULL) { 795eda14cbcSMatt Macy dsl_dir_sync(dd, tx); 796eda14cbcSMatt Macy } 797eda14cbcSMatt Macy 798eda14cbcSMatt Macy /* 799eda14cbcSMatt Macy * The MOS's space is accounted for in the pool/$MOS 800eda14cbcSMatt Macy * (dp_mos_dir). We can't modify the mos while we're syncing 801eda14cbcSMatt Macy * it, so we remember the deltas and apply them here. 802eda14cbcSMatt Macy */ 803eda14cbcSMatt Macy if (dp->dp_mos_used_delta != 0 || dp->dp_mos_compressed_delta != 0 || 804eda14cbcSMatt Macy dp->dp_mos_uncompressed_delta != 0) { 805eda14cbcSMatt Macy dsl_dir_diduse_space(dp->dp_mos_dir, DD_USED_HEAD, 806eda14cbcSMatt Macy dp->dp_mos_used_delta, 807eda14cbcSMatt Macy dp->dp_mos_compressed_delta, 808eda14cbcSMatt Macy dp->dp_mos_uncompressed_delta, tx); 809eda14cbcSMatt Macy dp->dp_mos_used_delta = 0; 810eda14cbcSMatt Macy dp->dp_mos_compressed_delta = 0; 811eda14cbcSMatt Macy dp->dp_mos_uncompressed_delta = 0; 812eda14cbcSMatt Macy } 813eda14cbcSMatt Macy 814eda14cbcSMatt Macy if (dmu_objset_is_dirty(mos, txg)) { 815eda14cbcSMatt Macy dsl_pool_sync_mos(dp, tx); 816eda14cbcSMatt Macy } 817eda14cbcSMatt Macy 818eda14cbcSMatt Macy /* 819eda14cbcSMatt Macy * We have written all of the accounted dirty data, so our 820eda14cbcSMatt Macy * dp_space_towrite should now be zero. However, some seldom-used 821eda14cbcSMatt Macy * code paths do not adhere to this (e.g. dbuf_undirty()). Shore up 822eda14cbcSMatt Macy * the accounting of any dirtied space now. 823eda14cbcSMatt Macy * 824eda14cbcSMatt Macy * Note that, besides any dirty data from datasets, the amount of 825eda14cbcSMatt Macy * dirty data in the MOS is also accounted by the pool. Therefore, 826eda14cbcSMatt Macy * we want to do this cleanup after dsl_pool_sync_mos() so we don't 827eda14cbcSMatt Macy * attempt to update the accounting for the same dirty data twice. 828eda14cbcSMatt Macy * (i.e. at this point we only update the accounting for the space 829eda14cbcSMatt Macy * that we know that we "leaked"). 830eda14cbcSMatt Macy */ 831eda14cbcSMatt Macy dsl_pool_undirty_space(dp, dp->dp_dirty_pertxg[txg & TXG_MASK], txg); 832eda14cbcSMatt Macy 833eda14cbcSMatt Macy /* 834eda14cbcSMatt Macy * If we modify a dataset in the same txg that we want to destroy it, 835eda14cbcSMatt Macy * its dsl_dir's dd_dbuf will be dirty, and thus have a hold on it. 836eda14cbcSMatt Macy * dsl_dir_destroy_check() will fail if there are unexpected holds. 837eda14cbcSMatt Macy * Therefore, we want to sync the MOS (thus syncing the dd_dbuf 838eda14cbcSMatt Macy * and clearing the hold on it) before we process the sync_tasks. 839eda14cbcSMatt Macy * The MOS data dirtied by the sync_tasks will be synced on the next 840eda14cbcSMatt Macy * pass. 841eda14cbcSMatt Macy */ 842eda14cbcSMatt Macy if (!txg_list_empty(&dp->dp_sync_tasks, txg)) { 843eda14cbcSMatt Macy dsl_sync_task_t *dst; 844eda14cbcSMatt Macy /* 845eda14cbcSMatt Macy * No more sync tasks should have been added while we 846eda14cbcSMatt Macy * were syncing. 847eda14cbcSMatt Macy */ 848eda14cbcSMatt Macy ASSERT3U(spa_sync_pass(dp->dp_spa), ==, 1); 849eda14cbcSMatt Macy while ((dst = txg_list_remove(&dp->dp_sync_tasks, txg)) != NULL) 850eda14cbcSMatt Macy dsl_sync_task_sync(dst, tx); 851eda14cbcSMatt Macy } 852eda14cbcSMatt Macy 853eda14cbcSMatt Macy dmu_tx_commit(tx); 854eda14cbcSMatt Macy 855eda14cbcSMatt Macy DTRACE_PROBE2(dsl_pool_sync__done, dsl_pool_t *dp, dp, uint64_t, txg); 856eda14cbcSMatt Macy } 857eda14cbcSMatt Macy 858eda14cbcSMatt Macy void 859eda14cbcSMatt Macy dsl_pool_sync_done(dsl_pool_t *dp, uint64_t txg) 860eda14cbcSMatt Macy { 861eda14cbcSMatt Macy zilog_t *zilog; 862eda14cbcSMatt Macy 863eda14cbcSMatt Macy while ((zilog = txg_list_head(&dp->dp_dirty_zilogs, txg))) { 864eda14cbcSMatt Macy dsl_dataset_t *ds = dmu_objset_ds(zilog->zl_os); 865eda14cbcSMatt Macy /* 866eda14cbcSMatt Macy * We don't remove the zilog from the dp_dirty_zilogs 867eda14cbcSMatt Macy * list until after we've cleaned it. This ensures that 868eda14cbcSMatt Macy * callers of zilog_is_dirty() receive an accurate 869eda14cbcSMatt Macy * answer when they are racing with the spa sync thread. 870eda14cbcSMatt Macy */ 871eda14cbcSMatt Macy zil_clean(zilog, txg); 872eda14cbcSMatt Macy (void) txg_list_remove_this(&dp->dp_dirty_zilogs, zilog, txg); 873eda14cbcSMatt Macy ASSERT(!dmu_objset_is_dirty(zilog->zl_os, txg)); 874eda14cbcSMatt Macy dmu_buf_rele(ds->ds_dbuf, zilog); 875eda14cbcSMatt Macy } 8763f9d360cSMartin Matuska 8773f9d360cSMartin Matuska dsl_pool_wrlog_clear(dp, txg); 8783f9d360cSMartin Matuska 879eda14cbcSMatt Macy ASSERT(!dmu_objset_is_dirty(dp->dp_meta_objset, txg)); 880eda14cbcSMatt Macy } 881eda14cbcSMatt Macy 882eda14cbcSMatt Macy /* 883eda14cbcSMatt Macy * TRUE if the current thread is the tx_sync_thread or if we 884eda14cbcSMatt Macy * are being called from SPA context during pool initialization. 885eda14cbcSMatt Macy */ 886eda14cbcSMatt Macy int 887eda14cbcSMatt Macy dsl_pool_sync_context(dsl_pool_t *dp) 888eda14cbcSMatt Macy { 889eda14cbcSMatt Macy return (curthread == dp->dp_tx.tx_sync_thread || 890eda14cbcSMatt Macy spa_is_initializing(dp->dp_spa) || 891eda14cbcSMatt Macy taskq_member(dp->dp_sync_taskq, curthread)); 892eda14cbcSMatt Macy } 893eda14cbcSMatt Macy 894eda14cbcSMatt Macy /* 895eda14cbcSMatt Macy * This function returns the amount of allocatable space in the pool 896eda14cbcSMatt Macy * minus whatever space is currently reserved by ZFS for specific 897eda14cbcSMatt Macy * purposes. Specifically: 898eda14cbcSMatt Macy * 899eda14cbcSMatt Macy * 1] Any reserved SLOP space 900eda14cbcSMatt Macy * 2] Any space used by the checkpoint 901eda14cbcSMatt Macy * 3] Any space used for deferred frees 902eda14cbcSMatt Macy * 903eda14cbcSMatt Macy * The latter 2 are especially important because they are needed to 904eda14cbcSMatt Macy * rectify the SPA's and DMU's different understanding of how much space 905eda14cbcSMatt Macy * is used. Now the DMU is aware of that extra space tracked by the SPA 906eda14cbcSMatt Macy * without having to maintain a separate special dir (e.g similar to 907eda14cbcSMatt Macy * $MOS, $FREEING, and $LEAKED). 908eda14cbcSMatt Macy * 909eda14cbcSMatt Macy * Note: By deferred frees here, we mean the frees that were deferred 910eda14cbcSMatt Macy * in spa_sync() after sync pass 1 (spa_deferred_bpobj), and not the 911eda14cbcSMatt Macy * segments placed in ms_defer trees during metaslab_sync_done(). 912eda14cbcSMatt Macy */ 913eda14cbcSMatt Macy uint64_t 914eda14cbcSMatt Macy dsl_pool_adjustedsize(dsl_pool_t *dp, zfs_space_check_t slop_policy) 915eda14cbcSMatt Macy { 916eda14cbcSMatt Macy spa_t *spa = dp->dp_spa; 917eda14cbcSMatt Macy uint64_t space, resv, adjustedsize; 918eda14cbcSMatt Macy uint64_t spa_deferred_frees = 919eda14cbcSMatt Macy spa->spa_deferred_bpobj.bpo_phys->bpo_bytes; 920eda14cbcSMatt Macy 921eda14cbcSMatt Macy space = spa_get_dspace(spa) 922eda14cbcSMatt Macy - spa_get_checkpoint_space(spa) - spa_deferred_frees; 923eda14cbcSMatt Macy resv = spa_get_slop_space(spa); 924eda14cbcSMatt Macy 925eda14cbcSMatt Macy switch (slop_policy) { 926eda14cbcSMatt Macy case ZFS_SPACE_CHECK_NORMAL: 927eda14cbcSMatt Macy break; 928eda14cbcSMatt Macy case ZFS_SPACE_CHECK_RESERVED: 929eda14cbcSMatt Macy resv >>= 1; 930eda14cbcSMatt Macy break; 931eda14cbcSMatt Macy case ZFS_SPACE_CHECK_EXTRA_RESERVED: 932eda14cbcSMatt Macy resv >>= 2; 933eda14cbcSMatt Macy break; 934eda14cbcSMatt Macy case ZFS_SPACE_CHECK_NONE: 935eda14cbcSMatt Macy resv = 0; 936eda14cbcSMatt Macy break; 937eda14cbcSMatt Macy default: 938eda14cbcSMatt Macy panic("invalid slop policy value: %d", slop_policy); 939eda14cbcSMatt Macy break; 940eda14cbcSMatt Macy } 941eda14cbcSMatt Macy adjustedsize = (space >= resv) ? (space - resv) : 0; 942eda14cbcSMatt Macy 943eda14cbcSMatt Macy return (adjustedsize); 944eda14cbcSMatt Macy } 945eda14cbcSMatt Macy 946eda14cbcSMatt Macy uint64_t 947eda14cbcSMatt Macy dsl_pool_unreserved_space(dsl_pool_t *dp, zfs_space_check_t slop_policy) 948eda14cbcSMatt Macy { 949eda14cbcSMatt Macy uint64_t poolsize = dsl_pool_adjustedsize(dp, slop_policy); 950eda14cbcSMatt Macy uint64_t deferred = 951eda14cbcSMatt Macy metaslab_class_get_deferred(spa_normal_class(dp->dp_spa)); 952eda14cbcSMatt Macy uint64_t quota = (poolsize >= deferred) ? (poolsize - deferred) : 0; 953eda14cbcSMatt Macy return (quota); 954eda14cbcSMatt Macy } 955eda14cbcSMatt Macy 956c03c5b1cSMartin Matuska uint64_t 957c03c5b1cSMartin Matuska dsl_pool_deferred_space(dsl_pool_t *dp) 958c03c5b1cSMartin Matuska { 959c03c5b1cSMartin Matuska return (metaslab_class_get_deferred(spa_normal_class(dp->dp_spa))); 960c03c5b1cSMartin Matuska } 961c03c5b1cSMartin Matuska 962eda14cbcSMatt Macy boolean_t 963eda14cbcSMatt Macy dsl_pool_need_dirty_delay(dsl_pool_t *dp) 964eda14cbcSMatt Macy { 965eda14cbcSMatt Macy uint64_t delay_min_bytes = 966eda14cbcSMatt Macy zfs_dirty_data_max * zfs_delay_min_dirty_percent / 100; 967eda14cbcSMatt Macy 968eda14cbcSMatt Macy mutex_enter(&dp->dp_lock); 9697cd22ac4SMartin Matuska uint64_t dirty = dp->dp_dirty_total; 970eda14cbcSMatt Macy mutex_exit(&dp->dp_lock); 9717cd22ac4SMartin Matuska 972eda14cbcSMatt Macy return (dirty > delay_min_bytes); 973eda14cbcSMatt Macy } 974eda14cbcSMatt Macy 9757cd22ac4SMartin Matuska static boolean_t 9767cd22ac4SMartin Matuska dsl_pool_need_dirty_sync(dsl_pool_t *dp, uint64_t txg) 9777cd22ac4SMartin Matuska { 9787cd22ac4SMartin Matuska ASSERT(MUTEX_HELD(&dp->dp_lock)); 9797cd22ac4SMartin Matuska 9807cd22ac4SMartin Matuska uint64_t dirty_min_bytes = 9817cd22ac4SMartin Matuska zfs_dirty_data_max * zfs_dirty_data_sync_percent / 100; 9827cd22ac4SMartin Matuska uint64_t dirty = dp->dp_dirty_pertxg[txg & TXG_MASK]; 9837cd22ac4SMartin Matuska 9847cd22ac4SMartin Matuska return (dirty > dirty_min_bytes); 9857cd22ac4SMartin Matuska } 9867cd22ac4SMartin Matuska 987eda14cbcSMatt Macy void 988eda14cbcSMatt Macy dsl_pool_dirty_space(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx) 989eda14cbcSMatt Macy { 990eda14cbcSMatt Macy if (space > 0) { 991eda14cbcSMatt Macy mutex_enter(&dp->dp_lock); 992eda14cbcSMatt Macy dp->dp_dirty_pertxg[tx->tx_txg & TXG_MASK] += space; 993eda14cbcSMatt Macy dsl_pool_dirty_delta(dp, space); 9947cd22ac4SMartin Matuska boolean_t needsync = !dmu_tx_is_syncing(tx) && 9957cd22ac4SMartin Matuska dsl_pool_need_dirty_sync(dp, tx->tx_txg); 996eda14cbcSMatt Macy mutex_exit(&dp->dp_lock); 9977cd22ac4SMartin Matuska 9987cd22ac4SMartin Matuska if (needsync) 9997cd22ac4SMartin Matuska txg_kick(dp, tx->tx_txg); 1000eda14cbcSMatt Macy } 1001eda14cbcSMatt Macy } 1002eda14cbcSMatt Macy 1003eda14cbcSMatt Macy void 1004eda14cbcSMatt Macy dsl_pool_undirty_space(dsl_pool_t *dp, int64_t space, uint64_t txg) 1005eda14cbcSMatt Macy { 1006eda14cbcSMatt Macy ASSERT3S(space, >=, 0); 1007eda14cbcSMatt Macy if (space == 0) 1008eda14cbcSMatt Macy return; 1009eda14cbcSMatt Macy 1010eda14cbcSMatt Macy mutex_enter(&dp->dp_lock); 1011eda14cbcSMatt Macy if (dp->dp_dirty_pertxg[txg & TXG_MASK] < space) { 1012eda14cbcSMatt Macy /* XXX writing something we didn't dirty? */ 1013eda14cbcSMatt Macy space = dp->dp_dirty_pertxg[txg & TXG_MASK]; 1014eda14cbcSMatt Macy } 1015eda14cbcSMatt Macy ASSERT3U(dp->dp_dirty_pertxg[txg & TXG_MASK], >=, space); 1016eda14cbcSMatt Macy dp->dp_dirty_pertxg[txg & TXG_MASK] -= space; 1017eda14cbcSMatt Macy ASSERT3U(dp->dp_dirty_total, >=, space); 1018eda14cbcSMatt Macy dsl_pool_dirty_delta(dp, -space); 1019eda14cbcSMatt Macy mutex_exit(&dp->dp_lock); 1020eda14cbcSMatt Macy } 1021eda14cbcSMatt Macy 1022eda14cbcSMatt Macy static int 1023eda14cbcSMatt Macy upgrade_clones_cb(dsl_pool_t *dp, dsl_dataset_t *hds, void *arg) 1024eda14cbcSMatt Macy { 1025eda14cbcSMatt Macy dmu_tx_t *tx = arg; 1026eda14cbcSMatt Macy dsl_dataset_t *ds, *prev = NULL; 1027eda14cbcSMatt Macy int err; 1028eda14cbcSMatt Macy 1029eda14cbcSMatt Macy err = dsl_dataset_hold_obj(dp, hds->ds_object, FTAG, &ds); 1030eda14cbcSMatt Macy if (err) 1031eda14cbcSMatt Macy return (err); 1032eda14cbcSMatt Macy 1033eda14cbcSMatt Macy while (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) { 1034eda14cbcSMatt Macy err = dsl_dataset_hold_obj(dp, 1035eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &prev); 1036eda14cbcSMatt Macy if (err) { 1037eda14cbcSMatt Macy dsl_dataset_rele(ds, FTAG); 1038eda14cbcSMatt Macy return (err); 1039eda14cbcSMatt Macy } 1040eda14cbcSMatt Macy 1041eda14cbcSMatt Macy if (dsl_dataset_phys(prev)->ds_next_snap_obj != ds->ds_object) 1042eda14cbcSMatt Macy break; 1043eda14cbcSMatt Macy dsl_dataset_rele(ds, FTAG); 1044eda14cbcSMatt Macy ds = prev; 1045eda14cbcSMatt Macy prev = NULL; 1046eda14cbcSMatt Macy } 1047eda14cbcSMatt Macy 1048eda14cbcSMatt Macy if (prev == NULL) { 1049eda14cbcSMatt Macy prev = dp->dp_origin_snap; 1050eda14cbcSMatt Macy 1051eda14cbcSMatt Macy /* 1052eda14cbcSMatt Macy * The $ORIGIN can't have any data, or the accounting 1053eda14cbcSMatt Macy * will be wrong. 1054eda14cbcSMatt Macy */ 1055eda14cbcSMatt Macy rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG); 1056eda14cbcSMatt Macy ASSERT0(dsl_dataset_phys(prev)->ds_bp.blk_birth); 1057eda14cbcSMatt Macy rrw_exit(&ds->ds_bp_rwlock, FTAG); 1058eda14cbcSMatt Macy 1059eda14cbcSMatt Macy /* The origin doesn't get attached to itself */ 1060eda14cbcSMatt Macy if (ds->ds_object == prev->ds_object) { 1061eda14cbcSMatt Macy dsl_dataset_rele(ds, FTAG); 1062eda14cbcSMatt Macy return (0); 1063eda14cbcSMatt Macy } 1064eda14cbcSMatt Macy 1065eda14cbcSMatt Macy dmu_buf_will_dirty(ds->ds_dbuf, tx); 1066eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_prev_snap_obj = prev->ds_object; 1067eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_prev_snap_txg = 1068eda14cbcSMatt Macy dsl_dataset_phys(prev)->ds_creation_txg; 1069eda14cbcSMatt Macy 1070eda14cbcSMatt Macy dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx); 1071eda14cbcSMatt Macy dsl_dir_phys(ds->ds_dir)->dd_origin_obj = prev->ds_object; 1072eda14cbcSMatt Macy 1073eda14cbcSMatt Macy dmu_buf_will_dirty(prev->ds_dbuf, tx); 1074eda14cbcSMatt Macy dsl_dataset_phys(prev)->ds_num_children++; 1075eda14cbcSMatt Macy 1076eda14cbcSMatt Macy if (dsl_dataset_phys(ds)->ds_next_snap_obj == 0) { 1077eda14cbcSMatt Macy ASSERT(ds->ds_prev == NULL); 1078eda14cbcSMatt Macy VERIFY0(dsl_dataset_hold_obj(dp, 1079eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_prev_snap_obj, 1080eda14cbcSMatt Macy ds, &ds->ds_prev)); 1081eda14cbcSMatt Macy } 1082eda14cbcSMatt Macy } 1083eda14cbcSMatt Macy 1084eda14cbcSMatt Macy ASSERT3U(dsl_dir_phys(ds->ds_dir)->dd_origin_obj, ==, prev->ds_object); 1085eda14cbcSMatt Macy ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_obj, ==, prev->ds_object); 1086eda14cbcSMatt Macy 1087eda14cbcSMatt Macy if (dsl_dataset_phys(prev)->ds_next_clones_obj == 0) { 1088eda14cbcSMatt Macy dmu_buf_will_dirty(prev->ds_dbuf, tx); 1089eda14cbcSMatt Macy dsl_dataset_phys(prev)->ds_next_clones_obj = 1090eda14cbcSMatt Macy zap_create(dp->dp_meta_objset, 1091eda14cbcSMatt Macy DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx); 1092eda14cbcSMatt Macy } 1093eda14cbcSMatt Macy VERIFY0(zap_add_int(dp->dp_meta_objset, 1094eda14cbcSMatt Macy dsl_dataset_phys(prev)->ds_next_clones_obj, ds->ds_object, tx)); 1095eda14cbcSMatt Macy 1096eda14cbcSMatt Macy dsl_dataset_rele(ds, FTAG); 1097eda14cbcSMatt Macy if (prev != dp->dp_origin_snap) 1098eda14cbcSMatt Macy dsl_dataset_rele(prev, FTAG); 1099eda14cbcSMatt Macy return (0); 1100eda14cbcSMatt Macy } 1101eda14cbcSMatt Macy 1102eda14cbcSMatt Macy void 1103eda14cbcSMatt Macy dsl_pool_upgrade_clones(dsl_pool_t *dp, dmu_tx_t *tx) 1104eda14cbcSMatt Macy { 1105eda14cbcSMatt Macy ASSERT(dmu_tx_is_syncing(tx)); 1106eda14cbcSMatt Macy ASSERT(dp->dp_origin_snap != NULL); 1107eda14cbcSMatt Macy 1108eda14cbcSMatt Macy VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj, upgrade_clones_cb, 1109eda14cbcSMatt Macy tx, DS_FIND_CHILDREN | DS_FIND_SERIALIZE)); 1110eda14cbcSMatt Macy } 1111eda14cbcSMatt Macy 1112eda14cbcSMatt Macy static int 1113eda14cbcSMatt Macy upgrade_dir_clones_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg) 1114eda14cbcSMatt Macy { 1115eda14cbcSMatt Macy dmu_tx_t *tx = arg; 1116eda14cbcSMatt Macy objset_t *mos = dp->dp_meta_objset; 1117eda14cbcSMatt Macy 1118eda14cbcSMatt Macy if (dsl_dir_phys(ds->ds_dir)->dd_origin_obj != 0) { 1119eda14cbcSMatt Macy dsl_dataset_t *origin; 1120eda14cbcSMatt Macy 1121eda14cbcSMatt Macy VERIFY0(dsl_dataset_hold_obj(dp, 1122eda14cbcSMatt Macy dsl_dir_phys(ds->ds_dir)->dd_origin_obj, FTAG, &origin)); 1123eda14cbcSMatt Macy 1124eda14cbcSMatt Macy if (dsl_dir_phys(origin->ds_dir)->dd_clones == 0) { 1125eda14cbcSMatt Macy dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx); 1126eda14cbcSMatt Macy dsl_dir_phys(origin->ds_dir)->dd_clones = 1127eda14cbcSMatt Macy zap_create(mos, DMU_OT_DSL_CLONES, DMU_OT_NONE, 1128eda14cbcSMatt Macy 0, tx); 1129eda14cbcSMatt Macy } 1130eda14cbcSMatt Macy 1131eda14cbcSMatt Macy VERIFY0(zap_add_int(dp->dp_meta_objset, 1132eda14cbcSMatt Macy dsl_dir_phys(origin->ds_dir)->dd_clones, 1133eda14cbcSMatt Macy ds->ds_object, tx)); 1134eda14cbcSMatt Macy 1135eda14cbcSMatt Macy dsl_dataset_rele(origin, FTAG); 1136eda14cbcSMatt Macy } 1137eda14cbcSMatt Macy return (0); 1138eda14cbcSMatt Macy } 1139eda14cbcSMatt Macy 1140eda14cbcSMatt Macy void 1141eda14cbcSMatt Macy dsl_pool_upgrade_dir_clones(dsl_pool_t *dp, dmu_tx_t *tx) 1142eda14cbcSMatt Macy { 1143eda14cbcSMatt Macy uint64_t obj; 1144eda14cbcSMatt Macy 1145eda14cbcSMatt Macy ASSERT(dmu_tx_is_syncing(tx)); 1146eda14cbcSMatt Macy 1147eda14cbcSMatt Macy (void) dsl_dir_create_sync(dp, dp->dp_root_dir, FREE_DIR_NAME, tx); 1148eda14cbcSMatt Macy VERIFY0(dsl_pool_open_special_dir(dp, 1149eda14cbcSMatt Macy FREE_DIR_NAME, &dp->dp_free_dir)); 1150eda14cbcSMatt Macy 1151eda14cbcSMatt Macy /* 1152eda14cbcSMatt Macy * We can't use bpobj_alloc(), because spa_version() still 1153eda14cbcSMatt Macy * returns the old version, and we need a new-version bpobj with 1154eda14cbcSMatt Macy * subobj support. So call dmu_object_alloc() directly. 1155eda14cbcSMatt Macy */ 1156eda14cbcSMatt Macy obj = dmu_object_alloc(dp->dp_meta_objset, DMU_OT_BPOBJ, 1157eda14cbcSMatt Macy SPA_OLD_MAXBLOCKSIZE, DMU_OT_BPOBJ_HDR, sizeof (bpobj_phys_t), tx); 1158eda14cbcSMatt Macy VERIFY0(zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 1159eda14cbcSMatt Macy DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj, tx)); 1160eda14cbcSMatt Macy VERIFY0(bpobj_open(&dp->dp_free_bpobj, dp->dp_meta_objset, obj)); 1161eda14cbcSMatt Macy 1162eda14cbcSMatt Macy VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj, 1163eda14cbcSMatt Macy upgrade_dir_clones_cb, tx, DS_FIND_CHILDREN | DS_FIND_SERIALIZE)); 1164eda14cbcSMatt Macy } 1165eda14cbcSMatt Macy 1166eda14cbcSMatt Macy void 1167eda14cbcSMatt Macy dsl_pool_create_origin(dsl_pool_t *dp, dmu_tx_t *tx) 1168eda14cbcSMatt Macy { 1169eda14cbcSMatt Macy uint64_t dsobj; 1170eda14cbcSMatt Macy dsl_dataset_t *ds; 1171eda14cbcSMatt Macy 1172eda14cbcSMatt Macy ASSERT(dmu_tx_is_syncing(tx)); 1173eda14cbcSMatt Macy ASSERT(dp->dp_origin_snap == NULL); 1174eda14cbcSMatt Macy ASSERT(rrw_held(&dp->dp_config_rwlock, RW_WRITER)); 1175eda14cbcSMatt Macy 1176eda14cbcSMatt Macy /* create the origin dir, ds, & snap-ds */ 1177eda14cbcSMatt Macy dsobj = dsl_dataset_create_sync(dp->dp_root_dir, ORIGIN_DIR_NAME, 1178eda14cbcSMatt Macy NULL, 0, kcred, NULL, tx); 1179eda14cbcSMatt Macy VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds)); 1180eda14cbcSMatt Macy dsl_dataset_snapshot_sync_impl(ds, ORIGIN_DIR_NAME, tx); 1181eda14cbcSMatt Macy VERIFY0(dsl_dataset_hold_obj(dp, dsl_dataset_phys(ds)->ds_prev_snap_obj, 1182eda14cbcSMatt Macy dp, &dp->dp_origin_snap)); 1183eda14cbcSMatt Macy dsl_dataset_rele(ds, FTAG); 1184eda14cbcSMatt Macy } 1185eda14cbcSMatt Macy 1186eda14cbcSMatt Macy taskq_t * 1187eda14cbcSMatt Macy dsl_pool_zrele_taskq(dsl_pool_t *dp) 1188eda14cbcSMatt Macy { 1189eda14cbcSMatt Macy return (dp->dp_zrele_taskq); 1190eda14cbcSMatt Macy } 1191eda14cbcSMatt Macy 1192eda14cbcSMatt Macy taskq_t * 1193eda14cbcSMatt Macy dsl_pool_unlinked_drain_taskq(dsl_pool_t *dp) 1194eda14cbcSMatt Macy { 1195eda14cbcSMatt Macy return (dp->dp_unlinked_drain_taskq); 1196eda14cbcSMatt Macy } 1197eda14cbcSMatt Macy 1198eda14cbcSMatt Macy /* 1199eda14cbcSMatt Macy * Walk through the pool-wide zap object of temporary snapshot user holds 1200eda14cbcSMatt Macy * and release them. 1201eda14cbcSMatt Macy */ 1202eda14cbcSMatt Macy void 1203eda14cbcSMatt Macy dsl_pool_clean_tmp_userrefs(dsl_pool_t *dp) 1204eda14cbcSMatt Macy { 1205eda14cbcSMatt Macy zap_attribute_t za; 1206eda14cbcSMatt Macy zap_cursor_t zc; 1207eda14cbcSMatt Macy objset_t *mos = dp->dp_meta_objset; 1208eda14cbcSMatt Macy uint64_t zapobj = dp->dp_tmp_userrefs_obj; 1209eda14cbcSMatt Macy nvlist_t *holds; 1210eda14cbcSMatt Macy 1211eda14cbcSMatt Macy if (zapobj == 0) 1212eda14cbcSMatt Macy return; 1213eda14cbcSMatt Macy ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS); 1214eda14cbcSMatt Macy 1215eda14cbcSMatt Macy holds = fnvlist_alloc(); 1216eda14cbcSMatt Macy 1217eda14cbcSMatt Macy for (zap_cursor_init(&zc, mos, zapobj); 1218eda14cbcSMatt Macy zap_cursor_retrieve(&zc, &za) == 0; 1219eda14cbcSMatt Macy zap_cursor_advance(&zc)) { 1220eda14cbcSMatt Macy char *htag; 1221eda14cbcSMatt Macy nvlist_t *tags; 1222eda14cbcSMatt Macy 1223eda14cbcSMatt Macy htag = strchr(za.za_name, '-'); 1224eda14cbcSMatt Macy *htag = '\0'; 1225eda14cbcSMatt Macy ++htag; 1226eda14cbcSMatt Macy if (nvlist_lookup_nvlist(holds, za.za_name, &tags) != 0) { 1227eda14cbcSMatt Macy tags = fnvlist_alloc(); 1228eda14cbcSMatt Macy fnvlist_add_boolean(tags, htag); 1229eda14cbcSMatt Macy fnvlist_add_nvlist(holds, za.za_name, tags); 1230eda14cbcSMatt Macy fnvlist_free(tags); 1231eda14cbcSMatt Macy } else { 1232eda14cbcSMatt Macy fnvlist_add_boolean(tags, htag); 1233eda14cbcSMatt Macy } 1234eda14cbcSMatt Macy } 1235eda14cbcSMatt Macy dsl_dataset_user_release_tmp(dp, holds); 1236eda14cbcSMatt Macy fnvlist_free(holds); 1237eda14cbcSMatt Macy zap_cursor_fini(&zc); 1238eda14cbcSMatt Macy } 1239eda14cbcSMatt Macy 1240eda14cbcSMatt Macy /* 1241eda14cbcSMatt Macy * Create the pool-wide zap object for storing temporary snapshot holds. 1242eda14cbcSMatt Macy */ 1243eda14cbcSMatt Macy static void 1244eda14cbcSMatt Macy dsl_pool_user_hold_create_obj(dsl_pool_t *dp, dmu_tx_t *tx) 1245eda14cbcSMatt Macy { 1246eda14cbcSMatt Macy objset_t *mos = dp->dp_meta_objset; 1247eda14cbcSMatt Macy 1248eda14cbcSMatt Macy ASSERT(dp->dp_tmp_userrefs_obj == 0); 1249eda14cbcSMatt Macy ASSERT(dmu_tx_is_syncing(tx)); 1250eda14cbcSMatt Macy 1251eda14cbcSMatt Macy dp->dp_tmp_userrefs_obj = zap_create_link(mos, DMU_OT_USERREFS, 1252eda14cbcSMatt Macy DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_TMP_USERREFS, tx); 1253eda14cbcSMatt Macy } 1254eda14cbcSMatt Macy 1255eda14cbcSMatt Macy static int 1256eda14cbcSMatt Macy dsl_pool_user_hold_rele_impl(dsl_pool_t *dp, uint64_t dsobj, 1257eda14cbcSMatt Macy const char *tag, uint64_t now, dmu_tx_t *tx, boolean_t holding) 1258eda14cbcSMatt Macy { 1259eda14cbcSMatt Macy objset_t *mos = dp->dp_meta_objset; 1260eda14cbcSMatt Macy uint64_t zapobj = dp->dp_tmp_userrefs_obj; 1261eda14cbcSMatt Macy char *name; 1262eda14cbcSMatt Macy int error; 1263eda14cbcSMatt Macy 1264eda14cbcSMatt Macy ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS); 1265eda14cbcSMatt Macy ASSERT(dmu_tx_is_syncing(tx)); 1266eda14cbcSMatt Macy 1267eda14cbcSMatt Macy /* 1268eda14cbcSMatt Macy * If the pool was created prior to SPA_VERSION_USERREFS, the 1269eda14cbcSMatt Macy * zap object for temporary holds might not exist yet. 1270eda14cbcSMatt Macy */ 1271eda14cbcSMatt Macy if (zapobj == 0) { 1272eda14cbcSMatt Macy if (holding) { 1273eda14cbcSMatt Macy dsl_pool_user_hold_create_obj(dp, tx); 1274eda14cbcSMatt Macy zapobj = dp->dp_tmp_userrefs_obj; 1275eda14cbcSMatt Macy } else { 1276eda14cbcSMatt Macy return (SET_ERROR(ENOENT)); 1277eda14cbcSMatt Macy } 1278eda14cbcSMatt Macy } 1279eda14cbcSMatt Macy 1280eda14cbcSMatt Macy name = kmem_asprintf("%llx-%s", (u_longlong_t)dsobj, tag); 1281eda14cbcSMatt Macy if (holding) 1282eda14cbcSMatt Macy error = zap_add(mos, zapobj, name, 8, 1, &now, tx); 1283eda14cbcSMatt Macy else 1284eda14cbcSMatt Macy error = zap_remove(mos, zapobj, name, tx); 1285eda14cbcSMatt Macy kmem_strfree(name); 1286eda14cbcSMatt Macy 1287eda14cbcSMatt Macy return (error); 1288eda14cbcSMatt Macy } 1289eda14cbcSMatt Macy 1290eda14cbcSMatt Macy /* 1291eda14cbcSMatt Macy * Add a temporary hold for the given dataset object and tag. 1292eda14cbcSMatt Macy */ 1293eda14cbcSMatt Macy int 1294eda14cbcSMatt Macy dsl_pool_user_hold(dsl_pool_t *dp, uint64_t dsobj, const char *tag, 1295eda14cbcSMatt Macy uint64_t now, dmu_tx_t *tx) 1296eda14cbcSMatt Macy { 1297eda14cbcSMatt Macy return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, now, tx, B_TRUE)); 1298eda14cbcSMatt Macy } 1299eda14cbcSMatt Macy 1300eda14cbcSMatt Macy /* 1301eda14cbcSMatt Macy * Release a temporary hold for the given dataset object and tag. 1302eda14cbcSMatt Macy */ 1303eda14cbcSMatt Macy int 1304eda14cbcSMatt Macy dsl_pool_user_release(dsl_pool_t *dp, uint64_t dsobj, const char *tag, 1305eda14cbcSMatt Macy dmu_tx_t *tx) 1306eda14cbcSMatt Macy { 1307eda14cbcSMatt Macy return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, 0, 1308eda14cbcSMatt Macy tx, B_FALSE)); 1309eda14cbcSMatt Macy } 1310eda14cbcSMatt Macy 1311eda14cbcSMatt Macy /* 1312eda14cbcSMatt Macy * DSL Pool Configuration Lock 1313eda14cbcSMatt Macy * 1314eda14cbcSMatt Macy * The dp_config_rwlock protects against changes to DSL state (e.g. dataset 1315eda14cbcSMatt Macy * creation / destruction / rename / property setting). It must be held for 1316eda14cbcSMatt Macy * read to hold a dataset or dsl_dir. I.e. you must call 1317eda14cbcSMatt Macy * dsl_pool_config_enter() or dsl_pool_hold() before calling 1318eda14cbcSMatt Macy * dsl_{dataset,dir}_hold{_obj}. In most circumstances, the dp_config_rwlock 1319eda14cbcSMatt Macy * must be held continuously until all datasets and dsl_dirs are released. 1320eda14cbcSMatt Macy * 1321eda14cbcSMatt Macy * The only exception to this rule is that if a "long hold" is placed on 1322eda14cbcSMatt Macy * a dataset, then the dp_config_rwlock may be dropped while the dataset 1323eda14cbcSMatt Macy * is still held. The long hold will prevent the dataset from being 1324eda14cbcSMatt Macy * destroyed -- the destroy will fail with EBUSY. A long hold can be 1325eda14cbcSMatt Macy * obtained by calling dsl_dataset_long_hold(), or by "owning" a dataset 1326eda14cbcSMatt Macy * (by calling dsl_{dataset,objset}_{try}own{_obj}). 1327eda14cbcSMatt Macy * 1328eda14cbcSMatt Macy * Legitimate long-holders (including owners) should be long-running, cancelable 1329eda14cbcSMatt Macy * tasks that should cause "zfs destroy" to fail. This includes DMU 1330eda14cbcSMatt Macy * consumers (i.e. a ZPL filesystem being mounted or ZVOL being open), 1331eda14cbcSMatt Macy * "zfs send", and "zfs diff". There are several other long-holders whose 1332eda14cbcSMatt Macy * uses are suboptimal (e.g. "zfs promote", and zil_suspend()). 1333eda14cbcSMatt Macy * 1334eda14cbcSMatt Macy * The usual formula for long-holding would be: 1335eda14cbcSMatt Macy * dsl_pool_hold() 1336eda14cbcSMatt Macy * dsl_dataset_hold() 1337eda14cbcSMatt Macy * ... perform checks ... 1338eda14cbcSMatt Macy * dsl_dataset_long_hold() 1339eda14cbcSMatt Macy * dsl_pool_rele() 1340eda14cbcSMatt Macy * ... perform long-running task ... 1341eda14cbcSMatt Macy * dsl_dataset_long_rele() 1342eda14cbcSMatt Macy * dsl_dataset_rele() 1343eda14cbcSMatt Macy * 1344eda14cbcSMatt Macy * Note that when the long hold is released, the dataset is still held but 1345eda14cbcSMatt Macy * the pool is not held. The dataset may change arbitrarily during this time 1346eda14cbcSMatt Macy * (e.g. it could be destroyed). Therefore you shouldn't do anything to the 1347eda14cbcSMatt Macy * dataset except release it. 1348eda14cbcSMatt Macy * 13497877fdebSMatt Macy * Operations generally fall somewhere into the following taxonomy: 13507877fdebSMatt Macy * 13517877fdebSMatt Macy * Read-Only Modifying 13527877fdebSMatt Macy * 13537877fdebSMatt Macy * Dataset Layer / MOS zfs get zfs destroy 13547877fdebSMatt Macy * 13557877fdebSMatt Macy * Individual Dataset read() write() 13567877fdebSMatt Macy * 13577877fdebSMatt Macy * 13587877fdebSMatt Macy * Dataset Layer Operations 1359eda14cbcSMatt Macy * 1360eda14cbcSMatt Macy * Modifying operations should generally use dsl_sync_task(). The synctask 1361eda14cbcSMatt Macy * infrastructure enforces proper locking strategy with respect to the 1362eda14cbcSMatt Macy * dp_config_rwlock. See the comment above dsl_sync_task() for details. 1363eda14cbcSMatt Macy * 1364eda14cbcSMatt Macy * Read-only operations will manually hold the pool, then the dataset, obtain 1365eda14cbcSMatt Macy * information from the dataset, then release the pool and dataset. 1366eda14cbcSMatt Macy * dmu_objset_{hold,rele}() are convenience routines that also do the pool 1367eda14cbcSMatt Macy * hold/rele. 13687877fdebSMatt Macy * 13697877fdebSMatt Macy * 13707877fdebSMatt Macy * Operations On Individual Datasets 13717877fdebSMatt Macy * 13727877fdebSMatt Macy * Objects _within_ an objset should only be modified by the current 'owner' 13737877fdebSMatt Macy * of the objset to prevent incorrect concurrent modification. Thus, use 13747877fdebSMatt Macy * {dmu_objset,dsl_dataset}_own to mark some entity as the current owner, 13757877fdebSMatt Macy * and fail with EBUSY if there is already an owner. The owner can then 13767877fdebSMatt Macy * implement its own locking strategy, independent of the dataset layer's 13777877fdebSMatt Macy * locking infrastructure. 13787877fdebSMatt Macy * (E.g., the ZPL has its own set of locks to control concurrency. A regular 13797877fdebSMatt Macy * vnop will not reach into the dataset layer). 13807877fdebSMatt Macy * 13817877fdebSMatt Macy * Ideally, objects would also only be read by the objset’s owner, so that we 13827877fdebSMatt Macy * don’t observe state mid-modification. 13837877fdebSMatt Macy * (E.g. the ZPL is creating a new object and linking it into a directory; if 13847877fdebSMatt Macy * you don’t coordinate with the ZPL to hold ZPL-level locks, you could see an 13857877fdebSMatt Macy * intermediate state. The ioctl level violates this but in pretty benign 13867877fdebSMatt Macy * ways, e.g. reading the zpl props object.) 1387eda14cbcSMatt Macy */ 1388eda14cbcSMatt Macy 1389eda14cbcSMatt Macy int 1390a0b956f5SMartin Matuska dsl_pool_hold(const char *name, const void *tag, dsl_pool_t **dp) 1391eda14cbcSMatt Macy { 1392eda14cbcSMatt Macy spa_t *spa; 1393eda14cbcSMatt Macy int error; 1394eda14cbcSMatt Macy 1395eda14cbcSMatt Macy error = spa_open(name, &spa, tag); 1396eda14cbcSMatt Macy if (error == 0) { 1397eda14cbcSMatt Macy *dp = spa_get_dsl(spa); 1398eda14cbcSMatt Macy dsl_pool_config_enter(*dp, tag); 1399eda14cbcSMatt Macy } 1400eda14cbcSMatt Macy return (error); 1401eda14cbcSMatt Macy } 1402eda14cbcSMatt Macy 1403eda14cbcSMatt Macy void 1404a0b956f5SMartin Matuska dsl_pool_rele(dsl_pool_t *dp, const void *tag) 1405eda14cbcSMatt Macy { 1406eda14cbcSMatt Macy dsl_pool_config_exit(dp, tag); 1407eda14cbcSMatt Macy spa_close(dp->dp_spa, tag); 1408eda14cbcSMatt Macy } 1409eda14cbcSMatt Macy 1410eda14cbcSMatt Macy void 1411a0b956f5SMartin Matuska dsl_pool_config_enter(dsl_pool_t *dp, const void *tag) 1412eda14cbcSMatt Macy { 1413eda14cbcSMatt Macy /* 1414eda14cbcSMatt Macy * We use a "reentrant" reader-writer lock, but not reentrantly. 1415eda14cbcSMatt Macy * 1416eda14cbcSMatt Macy * The rrwlock can (with the track_all flag) track all reading threads, 1417eda14cbcSMatt Macy * which is very useful for debugging which code path failed to release 1418eda14cbcSMatt Macy * the lock, and for verifying that the *current* thread does hold 1419eda14cbcSMatt Macy * the lock. 1420eda14cbcSMatt Macy * 1421eda14cbcSMatt Macy * (Unlike a rwlock, which knows that N threads hold it for 1422eda14cbcSMatt Macy * read, but not *which* threads, so rw_held(RW_READER) returns TRUE 1423eda14cbcSMatt Macy * if any thread holds it for read, even if this thread doesn't). 1424eda14cbcSMatt Macy */ 1425eda14cbcSMatt Macy ASSERT(!rrw_held(&dp->dp_config_rwlock, RW_READER)); 1426eda14cbcSMatt Macy rrw_enter(&dp->dp_config_rwlock, RW_READER, tag); 1427eda14cbcSMatt Macy } 1428eda14cbcSMatt Macy 1429eda14cbcSMatt Macy void 1430a0b956f5SMartin Matuska dsl_pool_config_enter_prio(dsl_pool_t *dp, const void *tag) 1431eda14cbcSMatt Macy { 1432eda14cbcSMatt Macy ASSERT(!rrw_held(&dp->dp_config_rwlock, RW_READER)); 1433eda14cbcSMatt Macy rrw_enter_read_prio(&dp->dp_config_rwlock, tag); 1434eda14cbcSMatt Macy } 1435eda14cbcSMatt Macy 1436eda14cbcSMatt Macy void 1437a0b956f5SMartin Matuska dsl_pool_config_exit(dsl_pool_t *dp, const void *tag) 1438eda14cbcSMatt Macy { 1439eda14cbcSMatt Macy rrw_exit(&dp->dp_config_rwlock, tag); 1440eda14cbcSMatt Macy } 1441eda14cbcSMatt Macy 1442eda14cbcSMatt Macy boolean_t 1443eda14cbcSMatt Macy dsl_pool_config_held(dsl_pool_t *dp) 1444eda14cbcSMatt Macy { 1445eda14cbcSMatt Macy return (RRW_LOCK_HELD(&dp->dp_config_rwlock)); 1446eda14cbcSMatt Macy } 1447eda14cbcSMatt Macy 1448eda14cbcSMatt Macy boolean_t 1449eda14cbcSMatt Macy dsl_pool_config_held_writer(dsl_pool_t *dp) 1450eda14cbcSMatt Macy { 1451eda14cbcSMatt Macy return (RRW_WRITE_HELD(&dp->dp_config_rwlock)); 1452eda14cbcSMatt Macy } 1453eda14cbcSMatt Macy 1454eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_pool_config_enter); 1455eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_pool_config_exit); 1456eda14cbcSMatt Macy 1457eda14cbcSMatt Macy /* zfs_dirty_data_max_percent only applied at module load in arc_init(). */ 1458be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs, zfs_, dirty_data_max_percent, UINT, ZMOD_RD, 1459eda14cbcSMatt Macy "Max percent of RAM allowed to be dirty"); 1460eda14cbcSMatt Macy 1461eda14cbcSMatt Macy /* zfs_dirty_data_max_max_percent only applied at module load in arc_init(). */ 1462be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs, zfs_, dirty_data_max_max_percent, UINT, ZMOD_RD, 1463eda14cbcSMatt Macy "zfs_dirty_data_max upper bound as % of RAM"); 1464eda14cbcSMatt Macy 1465be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs, zfs_, delay_min_dirty_percent, UINT, ZMOD_RW, 1466eda14cbcSMatt Macy "Transaction delay threshold"); 1467eda14cbcSMatt Macy 1468dbd5678dSMartin Matuska ZFS_MODULE_PARAM(zfs, zfs_, dirty_data_max, U64, ZMOD_RW, 1469eda14cbcSMatt Macy "Determines the dirty space limit"); 1470eda14cbcSMatt Macy 1471dbd5678dSMartin Matuska ZFS_MODULE_PARAM(zfs, zfs_, wrlog_data_max, U64, ZMOD_RW, 14723f9d360cSMartin Matuska "The size limit of write-transaction zil log data"); 14733f9d360cSMartin Matuska 1474eda14cbcSMatt Macy /* zfs_dirty_data_max_max only applied at module load in arc_init(). */ 1475dbd5678dSMartin Matuska ZFS_MODULE_PARAM(zfs, zfs_, dirty_data_max_max, U64, ZMOD_RD, 1476eda14cbcSMatt Macy "zfs_dirty_data_max upper bound in bytes"); 1477eda14cbcSMatt Macy 1478be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs, zfs_, dirty_data_sync_percent, UINT, ZMOD_RW, 1479eda14cbcSMatt Macy "Dirty data txg sync threshold as a percentage of zfs_dirty_data_max"); 1480eda14cbcSMatt Macy 1481dbd5678dSMartin Matuska ZFS_MODULE_PARAM(zfs, zfs_, delay_scale, U64, ZMOD_RW, 1482eda14cbcSMatt Macy "How quickly delay approaches infinity"); 1483eda14cbcSMatt Macy 1484eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs, zfs_, sync_taskq_batch_pct, INT, ZMOD_RW, 1485eda14cbcSMatt Macy "Max percent of CPUs that are used to sync dirty data"); 1486eda14cbcSMatt Macy 1487eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs_zil, zfs_zil_, clean_taskq_nthr_pct, INT, ZMOD_RW, 1488eda14cbcSMatt Macy "Max percent of CPUs that are used per dp_sync_taskq"); 1489eda14cbcSMatt Macy 1490eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs_zil, zfs_zil_, clean_taskq_minalloc, INT, ZMOD_RW, 1491eda14cbcSMatt Macy "Number of taskq entries that are pre-populated"); 1492eda14cbcSMatt Macy 1493eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs_zil, zfs_zil_, clean_taskq_maxalloc, INT, ZMOD_RW, 1494eda14cbcSMatt Macy "Max number of taskq entries that are cached"); 1495