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