1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5ea8dc4b6Seschrock * Common Development and Distribution License (the "License"). 6ea8dc4b6Seschrock * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 21fa9e4066Sahrens /* 223f9d6ad7SLin Ling * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23ad135b5dSChristopher Siden * Copyright (c) 2012 by Delphix. All rights reserved. 24fa9e4066Sahrens */ 25fa9e4066Sahrens 26fa9e4066Sahrens #include <sys/dsl_pool.h> 27fa9e4066Sahrens #include <sys/dsl_dataset.h> 283f9d6ad7SLin Ling #include <sys/dsl_prop.h> 29fa9e4066Sahrens #include <sys/dsl_dir.h> 301d452cf5Sahrens #include <sys/dsl_synctask.h> 313f9d6ad7SLin Ling #include <sys/dsl_scan.h> 323f9d6ad7SLin Ling #include <sys/dnode.h> 33fa9e4066Sahrens #include <sys/dmu_tx.h> 34fa9e4066Sahrens #include <sys/dmu_objset.h> 35fa9e4066Sahrens #include <sys/arc.h> 36fa9e4066Sahrens #include <sys/zap.h> 37c717a561Smaybee #include <sys/zio.h> 38fa9e4066Sahrens #include <sys/zfs_context.h> 39fa9e4066Sahrens #include <sys/fs/zfs.h> 40088f3894Sahrens #include <sys/zfs_znode.h> 41088f3894Sahrens #include <sys/spa_impl.h> 42cde58dbcSMatthew Ahrens #include <sys/dsl_deadlist.h> 43ad135b5dSChristopher Siden #include <sys/bptree.h> 44ad135b5dSChristopher Siden #include <sys/zfeature.h> 45ce636f8bSMatthew Ahrens #include <sys/zil_impl.h> 463b2aab18SMatthew Ahrens #include <sys/dsl_userhold.h> 47fa9e4066Sahrens 481ab7f2deSmaybee int zfs_no_write_throttle = 0; 4905715f94SMark Maybee int zfs_write_limit_shift = 3; /* 1/8th of physical memory */ 5044ecc532SGeorge Wilson int zfs_txg_synctime_ms = 1000; /* target millisecs to sync a txg */ 5105715f94SMark Maybee 5205715f94SMark Maybee uint64_t zfs_write_limit_min = 32 << 20; /* min write limit is 32MB */ 5305715f94SMark Maybee uint64_t zfs_write_limit_max = 0; /* max data payload per txg */ 5405715f94SMark Maybee uint64_t zfs_write_limit_inflated = 0; 551ab7f2deSmaybee uint64_t zfs_write_limit_override = 0; 561ab7f2deSmaybee 5705715f94SMark Maybee kmutex_t zfs_write_limit_lock; 5805715f94SMark Maybee 5905715f94SMark Maybee static pgcnt_t old_physmem = 0; 60088f3894Sahrens 61*0689f76cSAdam Leventhal hrtime_t zfs_throttle_delay = MSEC2NSEC(10); 62*0689f76cSAdam Leventhal hrtime_t zfs_throttle_resolution = MSEC2NSEC(10); 63*0689f76cSAdam Leventhal 643f9d6ad7SLin Ling int 65088f3894Sahrens dsl_pool_open_special_dir(dsl_pool_t *dp, const char *name, dsl_dir_t **ddp) 66fa9e4066Sahrens { 67fa9e4066Sahrens uint64_t obj; 68fa9e4066Sahrens int err; 69fa9e4066Sahrens 70fa9e4066Sahrens err = zap_lookup(dp->dp_meta_objset, 71fa9e4066Sahrens dp->dp_root_dir->dd_phys->dd_child_dir_zapobj, 72088f3894Sahrens name, sizeof (obj), 1, &obj); 73ea8dc4b6Seschrock if (err) 74ea8dc4b6Seschrock return (err); 75fa9e4066Sahrens 763b2aab18SMatthew Ahrens return (dsl_dir_hold_obj(dp, obj, name, dp, ddp)); 77fa9e4066Sahrens } 78fa9e4066Sahrens 79fa9e4066Sahrens static dsl_pool_t * 80fa9e4066Sahrens dsl_pool_open_impl(spa_t *spa, uint64_t txg) 81fa9e4066Sahrens { 82fa9e4066Sahrens dsl_pool_t *dp; 83fa9e4066Sahrens blkptr_t *bp = spa_get_rootblkptr(spa); 84fa9e4066Sahrens 85fa9e4066Sahrens dp = kmem_zalloc(sizeof (dsl_pool_t), KM_SLEEP); 86fa9e4066Sahrens dp->dp_spa = spa; 87fa9e4066Sahrens dp->dp_meta_rootbp = *bp; 883b2aab18SMatthew Ahrens rrw_init(&dp->dp_config_rwlock, B_TRUE); 891ab7f2deSmaybee dp->dp_write_limit = zfs_write_limit_min; 90fa9e4066Sahrens txg_init(dp, txg); 91fa9e4066Sahrens 92fa9e4066Sahrens txg_list_create(&dp->dp_dirty_datasets, 93fa9e4066Sahrens offsetof(dsl_dataset_t, ds_dirty_link)); 94ce636f8bSMatthew Ahrens txg_list_create(&dp->dp_dirty_zilogs, 95ce636f8bSMatthew Ahrens offsetof(zilog_t, zl_dirty_link)); 96fa9e4066Sahrens txg_list_create(&dp->dp_dirty_dirs, 97fa9e4066Sahrens offsetof(dsl_dir_t, dd_dirty_link)); 981d452cf5Sahrens txg_list_create(&dp->dp_sync_tasks, 993b2aab18SMatthew Ahrens offsetof(dsl_sync_task_t, dst_node)); 100fa9e4066Sahrens 1011ab7f2deSmaybee mutex_init(&dp->dp_lock, NULL, MUTEX_DEFAULT, NULL); 1021ab7f2deSmaybee 1039d3574bfSNeil Perrin dp->dp_vnrele_taskq = taskq_create("zfs_vn_rele_taskq", 1, minclsyspri, 1049d3574bfSNeil Perrin 1, 4, 0); 1059d3574bfSNeil Perrin 106fa9e4066Sahrens return (dp); 107fa9e4066Sahrens } 108fa9e4066Sahrens 109ea8dc4b6Seschrock int 110ad135b5dSChristopher Siden dsl_pool_init(spa_t *spa, uint64_t txg, dsl_pool_t **dpp) 111fa9e4066Sahrens { 112fa9e4066Sahrens int err; 113fa9e4066Sahrens dsl_pool_t *dp = dsl_pool_open_impl(spa, txg); 114ad135b5dSChristopher Siden 115ad135b5dSChristopher Siden err = dmu_objset_open_impl(spa, NULL, &dp->dp_meta_rootbp, 116ad135b5dSChristopher Siden &dp->dp_meta_objset); 117ad135b5dSChristopher Siden if (err != 0) 118ad135b5dSChristopher Siden dsl_pool_close(dp); 119ad135b5dSChristopher Siden else 120ad135b5dSChristopher Siden *dpp = dp; 121ad135b5dSChristopher Siden 122ad135b5dSChristopher Siden return (err); 123ad135b5dSChristopher Siden } 124ad135b5dSChristopher Siden 125ad135b5dSChristopher Siden int 126ad135b5dSChristopher Siden dsl_pool_open(dsl_pool_t *dp) 127ad135b5dSChristopher Siden { 128ad135b5dSChristopher Siden int err; 129088f3894Sahrens dsl_dir_t *dd; 130088f3894Sahrens dsl_dataset_t *ds; 131cde58dbcSMatthew Ahrens uint64_t obj; 132fa9e4066Sahrens 1333b2aab18SMatthew Ahrens rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG); 134fa9e4066Sahrens err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 135fa9e4066Sahrens DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, 136fa9e4066Sahrens &dp->dp_root_dir_obj); 137ea8dc4b6Seschrock if (err) 138ea8dc4b6Seschrock goto out; 139ea8dc4b6Seschrock 1403b2aab18SMatthew Ahrens err = dsl_dir_hold_obj(dp, dp->dp_root_dir_obj, 141ea8dc4b6Seschrock NULL, dp, &dp->dp_root_dir); 142ea8dc4b6Seschrock if (err) 143ea8dc4b6Seschrock goto out; 144fa9e4066Sahrens 145088f3894Sahrens err = dsl_pool_open_special_dir(dp, MOS_DIR_NAME, &dp->dp_mos_dir); 146ea8dc4b6Seschrock if (err) 147ea8dc4b6Seschrock goto out; 148ea8dc4b6Seschrock 149ad135b5dSChristopher Siden if (spa_version(dp->dp_spa) >= SPA_VERSION_ORIGIN) { 150088f3894Sahrens err = dsl_pool_open_special_dir(dp, ORIGIN_DIR_NAME, &dd); 151088f3894Sahrens if (err) 152088f3894Sahrens goto out; 153088f3894Sahrens err = dsl_dataset_hold_obj(dp, dd->dd_phys->dd_head_dataset_obj, 154088f3894Sahrens FTAG, &ds); 1558f63aa46SLin Ling if (err == 0) { 1568f63aa46SLin Ling err = dsl_dataset_hold_obj(dp, 1578f63aa46SLin Ling ds->ds_phys->ds_prev_snap_obj, dp, 1588f63aa46SLin Ling &dp->dp_origin_snap); 1598f63aa46SLin Ling dsl_dataset_rele(ds, FTAG); 1608f63aa46SLin Ling } 1613b2aab18SMatthew Ahrens dsl_dir_rele(dd, dp); 162088f3894Sahrens if (err) 163088f3894Sahrens goto out; 164088f3894Sahrens } 165088f3894Sahrens 166ad135b5dSChristopher Siden if (spa_version(dp->dp_spa) >= SPA_VERSION_DEADLISTS) { 167cde58dbcSMatthew Ahrens err = dsl_pool_open_special_dir(dp, FREE_DIR_NAME, 168cde58dbcSMatthew Ahrens &dp->dp_free_dir); 169cde58dbcSMatthew Ahrens if (err) 170cde58dbcSMatthew Ahrens goto out; 171cde58dbcSMatthew Ahrens 172cde58dbcSMatthew Ahrens err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 173cde58dbcSMatthew Ahrens DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj); 174cde58dbcSMatthew Ahrens if (err) 175cde58dbcSMatthew Ahrens goto out; 1763b2aab18SMatthew Ahrens VERIFY0(bpobj_open(&dp->dp_free_bpobj, 177cde58dbcSMatthew Ahrens dp->dp_meta_objset, obj)); 178cde58dbcSMatthew Ahrens } 179cde58dbcSMatthew Ahrens 180ad135b5dSChristopher Siden if (spa_feature_is_active(dp->dp_spa, 181ad135b5dSChristopher Siden &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY])) { 182ad135b5dSChristopher Siden err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 183ad135b5dSChristopher Siden DMU_POOL_BPTREE_OBJ, sizeof (uint64_t), 1, 184ad135b5dSChristopher Siden &dp->dp_bptree_obj); 185ad135b5dSChristopher Siden if (err != 0) 186ad135b5dSChristopher Siden goto out; 187ad135b5dSChristopher Siden } 188ad135b5dSChristopher Siden 189f1745736SMatthew Ahrens if (spa_feature_is_active(dp->dp_spa, 190f1745736SMatthew Ahrens &spa_feature_table[SPA_FEATURE_EMPTY_BPOBJ])) { 191f1745736SMatthew Ahrens err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 192f1745736SMatthew Ahrens DMU_POOL_EMPTY_BPOBJ, sizeof (uint64_t), 1, 193f1745736SMatthew Ahrens &dp->dp_empty_bpobj); 194f1745736SMatthew Ahrens if (err != 0) 195f1745736SMatthew Ahrens goto out; 196f1745736SMatthew Ahrens } 197f1745736SMatthew Ahrens 198ca45db41SChris Kirby err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 199ca45db41SChris Kirby DMU_POOL_TMP_USERREFS, sizeof (uint64_t), 1, 200ca45db41SChris Kirby &dp->dp_tmp_userrefs_obj); 201ca45db41SChris Kirby if (err == ENOENT) 202ca45db41SChris Kirby err = 0; 203ca45db41SChris Kirby if (err) 204ca45db41SChris Kirby goto out; 205ca45db41SChris Kirby 206ad135b5dSChristopher Siden err = dsl_scan_init(dp, dp->dp_tx.tx_open_txg); 207088f3894Sahrens 208ea8dc4b6Seschrock out: 2093b2aab18SMatthew Ahrens rrw_exit(&dp->dp_config_rwlock, FTAG); 210ea8dc4b6Seschrock return (err); 211fa9e4066Sahrens } 212fa9e4066Sahrens 213fa9e4066Sahrens void 214fa9e4066Sahrens dsl_pool_close(dsl_pool_t *dp) 215fa9e4066Sahrens { 216088f3894Sahrens /* drop our references from dsl_pool_open() */ 217088f3894Sahrens 218088f3894Sahrens /* 219088f3894Sahrens * Since we held the origin_snap from "syncing" context (which 220088f3894Sahrens * includes pool-opening context), it actually only got a "ref" 221088f3894Sahrens * and not a hold, so just drop that here. 222088f3894Sahrens */ 223088f3894Sahrens if (dp->dp_origin_snap) 2243b2aab18SMatthew Ahrens dsl_dataset_rele(dp->dp_origin_snap, dp); 225ea8dc4b6Seschrock if (dp->dp_mos_dir) 2263b2aab18SMatthew Ahrens dsl_dir_rele(dp->dp_mos_dir, dp); 227cde58dbcSMatthew Ahrens if (dp->dp_free_dir) 2283b2aab18SMatthew Ahrens dsl_dir_rele(dp->dp_free_dir, dp); 229ea8dc4b6Seschrock if (dp->dp_root_dir) 2303b2aab18SMatthew Ahrens dsl_dir_rele(dp->dp_root_dir, dp); 231fa9e4066Sahrens 232cde58dbcSMatthew Ahrens bpobj_close(&dp->dp_free_bpobj); 233cde58dbcSMatthew Ahrens 234fa9e4066Sahrens /* undo the dmu_objset_open_impl(mos) from dsl_pool_open() */ 235ea8dc4b6Seschrock if (dp->dp_meta_objset) 236503ad85cSMatthew Ahrens dmu_objset_evict(dp->dp_meta_objset); 237fa9e4066Sahrens 238fa9e4066Sahrens txg_list_destroy(&dp->dp_dirty_datasets); 239ce636f8bSMatthew Ahrens txg_list_destroy(&dp->dp_dirty_zilogs); 24054a91118SChris Kirby txg_list_destroy(&dp->dp_sync_tasks); 241fa9e4066Sahrens txg_list_destroy(&dp->dp_dirty_dirs); 242fa9e4066Sahrens 243874395d5Smaybee arc_flush(dp->dp_spa); 244fa9e4066Sahrens txg_fini(dp); 2453f9d6ad7SLin Ling dsl_scan_fini(dp); 2463b2aab18SMatthew Ahrens rrw_destroy(&dp->dp_config_rwlock); 2471ab7f2deSmaybee mutex_destroy(&dp->dp_lock); 2489d3574bfSNeil Perrin taskq_destroy(dp->dp_vnrele_taskq); 24988b7b0f2SMatthew Ahrens if (dp->dp_blkstats) 25088b7b0f2SMatthew Ahrens kmem_free(dp->dp_blkstats, sizeof (zfs_all_blkstats_t)); 251fa9e4066Sahrens kmem_free(dp, sizeof (dsl_pool_t)); 252fa9e4066Sahrens } 253fa9e4066Sahrens 254fa9e4066Sahrens dsl_pool_t * 2550a48a24eStimh dsl_pool_create(spa_t *spa, nvlist_t *zplprops, uint64_t txg) 256fa9e4066Sahrens { 257fa9e4066Sahrens int err; 258fa9e4066Sahrens dsl_pool_t *dp = dsl_pool_open_impl(spa, txg); 259fa9e4066Sahrens dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg); 260503ad85cSMatthew Ahrens objset_t *os; 261088f3894Sahrens dsl_dataset_t *ds; 262cde58dbcSMatthew Ahrens uint64_t obj; 263088f3894Sahrens 2643b2aab18SMatthew Ahrens rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG); 2653b2aab18SMatthew Ahrens 266088f3894Sahrens /* create and open the MOS (meta-objset) */ 267503ad85cSMatthew Ahrens dp->dp_meta_objset = dmu_objset_create_impl(spa, 268503ad85cSMatthew Ahrens NULL, &dp->dp_meta_rootbp, DMU_OST_META, tx); 269fa9e4066Sahrens 270fa9e4066Sahrens /* create the pool directory */ 271fa9e4066Sahrens err = zap_create_claim(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 272fa9e4066Sahrens DMU_OT_OBJECT_DIRECTORY, DMU_OT_NONE, 0, tx); 273fb09f5aaSMadhav Suresh ASSERT0(err); 274fa9e4066Sahrens 2753f9d6ad7SLin Ling /* Initialize scan structures */ 2763b2aab18SMatthew Ahrens VERIFY0(dsl_scan_init(dp, txg)); 2773f9d6ad7SLin Ling 278fa9e4066Sahrens /* create and open the root dir */ 279088f3894Sahrens dp->dp_root_dir_obj = dsl_dir_create_sync(dp, NULL, NULL, tx); 2803b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold_obj(dp, dp->dp_root_dir_obj, 281ea8dc4b6Seschrock NULL, dp, &dp->dp_root_dir)); 282fa9e4066Sahrens 283fa9e4066Sahrens /* create and open the meta-objset dir */ 284088f3894Sahrens (void) dsl_dir_create_sync(dp, dp->dp_root_dir, MOS_DIR_NAME, tx); 2853b2aab18SMatthew Ahrens VERIFY0(dsl_pool_open_special_dir(dp, 286088f3894Sahrens MOS_DIR_NAME, &dp->dp_mos_dir)); 287088f3894Sahrens 288cde58dbcSMatthew Ahrens if (spa_version(spa) >= SPA_VERSION_DEADLISTS) { 289cde58dbcSMatthew Ahrens /* create and open the free dir */ 290cde58dbcSMatthew Ahrens (void) dsl_dir_create_sync(dp, dp->dp_root_dir, 291cde58dbcSMatthew Ahrens FREE_DIR_NAME, tx); 2923b2aab18SMatthew Ahrens VERIFY0(dsl_pool_open_special_dir(dp, 293cde58dbcSMatthew Ahrens FREE_DIR_NAME, &dp->dp_free_dir)); 294cde58dbcSMatthew Ahrens 295cde58dbcSMatthew Ahrens /* create and open the free_bplist */ 296cde58dbcSMatthew Ahrens obj = bpobj_alloc(dp->dp_meta_objset, SPA_MAXBLOCKSIZE, tx); 297cde58dbcSMatthew Ahrens VERIFY(zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 298cde58dbcSMatthew Ahrens DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj, tx) == 0); 2993b2aab18SMatthew Ahrens VERIFY0(bpobj_open(&dp->dp_free_bpobj, 300cde58dbcSMatthew Ahrens dp->dp_meta_objset, obj)); 301cde58dbcSMatthew Ahrens } 302cde58dbcSMatthew Ahrens 303088f3894Sahrens if (spa_version(spa) >= SPA_VERSION_DSL_SCRUB) 304088f3894Sahrens dsl_pool_create_origin(dp, tx); 305088f3894Sahrens 306088f3894Sahrens /* create the root dataset */ 307cde58dbcSMatthew Ahrens obj = dsl_dataset_create_sync_dd(dp->dp_root_dir, NULL, 0, tx); 308088f3894Sahrens 309088f3894Sahrens /* create the root objset */ 3103b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, obj, FTAG, &ds)); 311503ad85cSMatthew Ahrens os = dmu_objset_create_impl(dp->dp_spa, ds, 312088f3894Sahrens dsl_dataset_get_blkptr(ds), DMU_OST_ZFS, tx); 313088f3894Sahrens #ifdef _KERNEL 314503ad85cSMatthew Ahrens zfs_create_fs(os, kcred, zplprops, tx); 315088f3894Sahrens #endif 316088f3894Sahrens dsl_dataset_rele(ds, FTAG); 317fa9e4066Sahrens 318fa9e4066Sahrens dmu_tx_commit(tx); 319fa9e4066Sahrens 3203b2aab18SMatthew Ahrens rrw_exit(&dp->dp_config_rwlock, FTAG); 3213b2aab18SMatthew Ahrens 322fa9e4066Sahrens return (dp); 323fa9e4066Sahrens } 324fa9e4066Sahrens 325ce636f8bSMatthew Ahrens /* 326ce636f8bSMatthew Ahrens * Account for the meta-objset space in its placeholder dsl_dir. 327ce636f8bSMatthew Ahrens */ 328ce636f8bSMatthew Ahrens void 329ce636f8bSMatthew Ahrens dsl_pool_mos_diduse_space(dsl_pool_t *dp, 330ce636f8bSMatthew Ahrens int64_t used, int64_t comp, int64_t uncomp) 331ce636f8bSMatthew Ahrens { 332ce636f8bSMatthew Ahrens ASSERT3U(comp, ==, uncomp); /* it's all metadata */ 333ce636f8bSMatthew Ahrens mutex_enter(&dp->dp_lock); 334ce636f8bSMatthew Ahrens dp->dp_mos_used_delta += used; 335ce636f8bSMatthew Ahrens dp->dp_mos_compressed_delta += comp; 336ce636f8bSMatthew Ahrens dp->dp_mos_uncompressed_delta += uncomp; 337ce636f8bSMatthew Ahrens mutex_exit(&dp->dp_lock); 338ce636f8bSMatthew Ahrens } 339ce636f8bSMatthew Ahrens 340cde58dbcSMatthew Ahrens static int 341cde58dbcSMatthew Ahrens deadlist_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 342cde58dbcSMatthew Ahrens { 343cde58dbcSMatthew Ahrens dsl_deadlist_t *dl = arg; 344cde58dbcSMatthew Ahrens dsl_deadlist_insert(dl, bp, tx); 345cde58dbcSMatthew Ahrens return (0); 346cde58dbcSMatthew Ahrens } 347cde58dbcSMatthew Ahrens 348fa9e4066Sahrens void 349fa9e4066Sahrens dsl_pool_sync(dsl_pool_t *dp, uint64_t txg) 350fa9e4066Sahrens { 351c717a561Smaybee zio_t *zio; 352fa9e4066Sahrens dmu_tx_t *tx; 353c717a561Smaybee dsl_dir_t *dd; 354c717a561Smaybee dsl_dataset_t *ds; 355503ad85cSMatthew Ahrens objset_t *mos = dp->dp_meta_objset; 35605715f94SMark Maybee hrtime_t start, write_time; 35705715f94SMark Maybee uint64_t data_written; 358c717a561Smaybee int err; 359ce636f8bSMatthew Ahrens list_t synced_datasets; 360ce636f8bSMatthew Ahrens 361ce636f8bSMatthew Ahrens list_create(&synced_datasets, sizeof (dsl_dataset_t), 362ce636f8bSMatthew Ahrens offsetof(dsl_dataset_t, ds_synced_link)); 363fa9e4066Sahrens 3643f9d6ad7SLin Ling /* 3653f9d6ad7SLin Ling * We need to copy dp_space_towrite() before doing 3663b2aab18SMatthew Ahrens * dsl_sync_task_sync(), because 3673f9d6ad7SLin Ling * dsl_dataset_snapshot_reserve_space() will increase 3683f9d6ad7SLin Ling * dp_space_towrite but not actually write anything. 3693f9d6ad7SLin Ling */ 3703f9d6ad7SLin Ling data_written = dp->dp_space_towrite[txg & TXG_MASK]; 3713f9d6ad7SLin Ling 372fa9e4066Sahrens tx = dmu_tx_create_assigned(dp, txg); 373fa9e4066Sahrens 37405715f94SMark Maybee dp->dp_read_overhead = 0; 3750fd90d51SMark Maybee start = gethrtime(); 37614843421SMatthew Ahrens 377c717a561Smaybee zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 378c717a561Smaybee while (ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) { 37914843421SMatthew Ahrens /* 38014843421SMatthew Ahrens * We must not sync any non-MOS datasets twice, because 38114843421SMatthew Ahrens * we may have taken a snapshot of them. However, we 38214843421SMatthew Ahrens * may sync newly-created datasets on pass 2. 38314843421SMatthew Ahrens */ 38414843421SMatthew Ahrens ASSERT(!list_link_active(&ds->ds_synced_link)); 385ce636f8bSMatthew Ahrens list_insert_tail(&synced_datasets, ds); 386c717a561Smaybee dsl_dataset_sync(ds, zio, tx); 387c717a561Smaybee } 38805715f94SMark Maybee DTRACE_PROBE(pool_sync__1setup); 389c717a561Smaybee err = zio_wait(zio); 39014843421SMatthew Ahrens 39105715f94SMark Maybee write_time = gethrtime() - start; 392c717a561Smaybee ASSERT(err == 0); 39305715f94SMark Maybee DTRACE_PROBE(pool_sync__2rootzio); 394c717a561Smaybee 395ce636f8bSMatthew Ahrens /* 396ce636f8bSMatthew Ahrens * After the data blocks have been written (ensured by the zio_wait() 397ce636f8bSMatthew Ahrens * above), update the user/group space accounting. 398ce636f8bSMatthew Ahrens */ 399ce636f8bSMatthew Ahrens for (ds = list_head(&synced_datasets); ds; 400ce636f8bSMatthew Ahrens ds = list_next(&synced_datasets, ds)) 4010a586ceaSMark Shellenbaum dmu_objset_do_userquota_updates(ds->ds_objset, tx); 40214843421SMatthew Ahrens 40314843421SMatthew Ahrens /* 40414843421SMatthew Ahrens * Sync the datasets again to push out the changes due to 4053f9d6ad7SLin Ling * userspace updates. This must be done before we process the 406ce636f8bSMatthew Ahrens * sync tasks, so that any snapshots will have the correct 407ce636f8bSMatthew Ahrens * user accounting information (and we won't get confused 408ce636f8bSMatthew Ahrens * about which blocks are part of the snapshot). 40914843421SMatthew Ahrens */ 41014843421SMatthew Ahrens zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 41114843421SMatthew Ahrens while (ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) { 41214843421SMatthew Ahrens ASSERT(list_link_active(&ds->ds_synced_link)); 41314843421SMatthew Ahrens dmu_buf_rele(ds->ds_dbuf, ds); 41414843421SMatthew Ahrens dsl_dataset_sync(ds, zio, tx); 41514843421SMatthew Ahrens } 41614843421SMatthew Ahrens err = zio_wait(zio); 41714843421SMatthew Ahrens 418b24ab676SJeff Bonwick /* 419ce636f8bSMatthew Ahrens * Now that the datasets have been completely synced, we can 420ce636f8bSMatthew Ahrens * clean up our in-memory structures accumulated while syncing: 421ce636f8bSMatthew Ahrens * 422ce636f8bSMatthew Ahrens * - move dead blocks from the pending deadlist to the on-disk deadlist 423ce636f8bSMatthew Ahrens * - release hold from dsl_dataset_dirty() 424b24ab676SJeff Bonwick */ 425ce636f8bSMatthew Ahrens while (ds = list_remove_head(&synced_datasets)) { 426ce636f8bSMatthew Ahrens objset_t *os = ds->ds_objset; 427cde58dbcSMatthew Ahrens bplist_iterate(&ds->ds_pending_deadlist, 428cde58dbcSMatthew Ahrens deadlist_enqueue_cb, &ds->ds_deadlist, tx); 429ce636f8bSMatthew Ahrens ASSERT(!dmu_objset_is_dirty(os, txg)); 430ce636f8bSMatthew Ahrens dmu_buf_rele(ds->ds_dbuf, ds); 431cde58dbcSMatthew Ahrens } 432b24ab676SJeff Bonwick 43305715f94SMark Maybee start = gethrtime(); 434c717a561Smaybee while (dd = txg_list_remove(&dp->dp_dirty_dirs, txg)) 435c717a561Smaybee dsl_dir_sync(dd, tx); 43605715f94SMark Maybee write_time += gethrtime() - start; 437fa9e4066Sahrens 438ce636f8bSMatthew Ahrens /* 439ce636f8bSMatthew Ahrens * The MOS's space is accounted for in the pool/$MOS 440ce636f8bSMatthew Ahrens * (dp_mos_dir). We can't modify the mos while we're syncing 441ce636f8bSMatthew Ahrens * it, so we remember the deltas and apply them here. 442ce636f8bSMatthew Ahrens */ 443ce636f8bSMatthew Ahrens if (dp->dp_mos_used_delta != 0 || dp->dp_mos_compressed_delta != 0 || 444ce636f8bSMatthew Ahrens dp->dp_mos_uncompressed_delta != 0) { 445ce636f8bSMatthew Ahrens dsl_dir_diduse_space(dp->dp_mos_dir, DD_USED_HEAD, 446ce636f8bSMatthew Ahrens dp->dp_mos_used_delta, 447ce636f8bSMatthew Ahrens dp->dp_mos_compressed_delta, 448ce636f8bSMatthew Ahrens dp->dp_mos_uncompressed_delta, tx); 449ce636f8bSMatthew Ahrens dp->dp_mos_used_delta = 0; 450ce636f8bSMatthew Ahrens dp->dp_mos_compressed_delta = 0; 451ce636f8bSMatthew Ahrens dp->dp_mos_uncompressed_delta = 0; 452ce636f8bSMatthew Ahrens } 453ce636f8bSMatthew Ahrens 45405715f94SMark Maybee start = gethrtime(); 455503ad85cSMatthew Ahrens if (list_head(&mos->os_dirty_dnodes[txg & TXG_MASK]) != NULL || 456503ad85cSMatthew Ahrens list_head(&mos->os_free_dnodes[txg & TXG_MASK]) != NULL) { 457c717a561Smaybee zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 458503ad85cSMatthew Ahrens dmu_objset_sync(mos, zio, tx); 459c717a561Smaybee err = zio_wait(zio); 460c717a561Smaybee ASSERT(err == 0); 461fa9e4066Sahrens dprintf_bp(&dp->dp_meta_rootbp, "meta objset rootbp is %s", ""); 462fa9e4066Sahrens spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp); 463fa9e4066Sahrens } 46405715f94SMark Maybee write_time += gethrtime() - start; 46505715f94SMark Maybee DTRACE_PROBE2(pool_sync__4io, hrtime_t, write_time, 46605715f94SMark Maybee hrtime_t, dp->dp_read_overhead); 46705715f94SMark Maybee write_time -= dp->dp_read_overhead; 468fa9e4066Sahrens 469ce636f8bSMatthew Ahrens /* 470ce636f8bSMatthew Ahrens * If we modify a dataset in the same txg that we want to destroy it, 471ce636f8bSMatthew Ahrens * its dsl_dir's dd_dbuf will be dirty, and thus have a hold on it. 472ce636f8bSMatthew Ahrens * dsl_dir_destroy_check() will fail if there are unexpected holds. 473ce636f8bSMatthew Ahrens * Therefore, we want to sync the MOS (thus syncing the dd_dbuf 474ce636f8bSMatthew Ahrens * and clearing the hold on it) before we process the sync_tasks. 475ce636f8bSMatthew Ahrens * The MOS data dirtied by the sync_tasks will be synced on the next 476ce636f8bSMatthew Ahrens * pass. 477ce636f8bSMatthew Ahrens */ 478ce636f8bSMatthew Ahrens DTRACE_PROBE(pool_sync__3task); 479ce636f8bSMatthew Ahrens if (!txg_list_empty(&dp->dp_sync_tasks, txg)) { 4803b2aab18SMatthew Ahrens dsl_sync_task_t *dst; 481ce636f8bSMatthew Ahrens /* 482ce636f8bSMatthew Ahrens * No more sync tasks should have been added while we 483ce636f8bSMatthew Ahrens * were syncing. 484ce636f8bSMatthew Ahrens */ 485ce636f8bSMatthew Ahrens ASSERT(spa_sync_pass(dp->dp_spa) == 1); 4863b2aab18SMatthew Ahrens while (dst = txg_list_remove(&dp->dp_sync_tasks, txg)) 4873b2aab18SMatthew Ahrens dsl_sync_task_sync(dst, tx); 488ce636f8bSMatthew Ahrens } 489ce636f8bSMatthew Ahrens 490fa9e4066Sahrens dmu_tx_commit(tx); 49105715f94SMark Maybee 49205715f94SMark Maybee dp->dp_space_towrite[txg & TXG_MASK] = 0; 49305715f94SMark Maybee ASSERT(dp->dp_tempreserved[txg & TXG_MASK] == 0); 49405715f94SMark Maybee 49505715f94SMark Maybee /* 49605715f94SMark Maybee * If the write limit max has not been explicitly set, set it 49705715f94SMark Maybee * to a fraction of available physical memory (default 1/8th). 49805715f94SMark Maybee * Note that we must inflate the limit because the spa 49905715f94SMark Maybee * inflates write sizes to account for data replication. 50005715f94SMark Maybee * Check this each sync phase to catch changing memory size. 50105715f94SMark Maybee */ 50205715f94SMark Maybee if (physmem != old_physmem && zfs_write_limit_shift) { 50305715f94SMark Maybee mutex_enter(&zfs_write_limit_lock); 50405715f94SMark Maybee old_physmem = physmem; 50505715f94SMark Maybee zfs_write_limit_max = ptob(physmem) >> zfs_write_limit_shift; 50605715f94SMark Maybee zfs_write_limit_inflated = MAX(zfs_write_limit_min, 50705715f94SMark Maybee spa_get_asize(dp->dp_spa, zfs_write_limit_max)); 50805715f94SMark Maybee mutex_exit(&zfs_write_limit_lock); 50905715f94SMark Maybee } 51005715f94SMark Maybee 51105715f94SMark Maybee /* 51205715f94SMark Maybee * Attempt to keep the sync time consistent by adjusting the 51305715f94SMark Maybee * amount of write traffic allowed into each transaction group. 51405715f94SMark Maybee * Weight the throughput calculation towards the current value: 51505715f94SMark Maybee * thru = 3/4 old_thru + 1/4 new_thru 516fb5dd802SLin Ling * 517*0689f76cSAdam Leventhal * Note: write_time is in nanosecs while dp_throughput is expressed in 518*0689f76cSAdam Leventhal * bytes per millisecond. 51905715f94SMark Maybee */ 52005715f94SMark Maybee ASSERT(zfs_write_limit_min > 0); 521*0689f76cSAdam Leventhal if (data_written > zfs_write_limit_min / 8 && 522*0689f76cSAdam Leventhal write_time > MSEC2NSEC(1)) { 523*0689f76cSAdam Leventhal uint64_t throughput = data_written / NSEC2MSEC(write_time); 524fb5dd802SLin Ling 52505715f94SMark Maybee if (dp->dp_throughput) 52605715f94SMark Maybee dp->dp_throughput = throughput / 4 + 52705715f94SMark Maybee 3 * dp->dp_throughput / 4; 52805715f94SMark Maybee else 52905715f94SMark Maybee dp->dp_throughput = throughput; 53005715f94SMark Maybee dp->dp_write_limit = MIN(zfs_write_limit_inflated, 53105715f94SMark Maybee MAX(zfs_write_limit_min, 532fb5dd802SLin Ling dp->dp_throughput * zfs_txg_synctime_ms)); 53305715f94SMark Maybee } 534fa9e4066Sahrens } 535fa9e4066Sahrens 536fa9e4066Sahrens void 537b24ab676SJeff Bonwick dsl_pool_sync_done(dsl_pool_t *dp, uint64_t txg) 538fa9e4066Sahrens { 539ce636f8bSMatthew Ahrens zilog_t *zilog; 540fa9e4066Sahrens dsl_dataset_t *ds; 541fa9e4066Sahrens 542ce636f8bSMatthew Ahrens while (zilog = txg_list_remove(&dp->dp_dirty_zilogs, txg)) { 543ce636f8bSMatthew Ahrens ds = dmu_objset_ds(zilog->zl_os); 544ce636f8bSMatthew Ahrens zil_clean(zilog, txg); 545ce636f8bSMatthew Ahrens ASSERT(!dmu_objset_is_dirty(zilog->zl_os, txg)); 546ce636f8bSMatthew Ahrens dmu_buf_rele(ds->ds_dbuf, zilog); 547fa9e4066Sahrens } 548b24ab676SJeff Bonwick ASSERT(!dmu_objset_is_dirty(dp->dp_meta_objset, txg)); 549fa9e4066Sahrens } 550fa9e4066Sahrens 551c717a561Smaybee /* 552c717a561Smaybee * TRUE if the current thread is the tx_sync_thread or if we 553c717a561Smaybee * are being called from SPA context during pool initialization. 554c717a561Smaybee */ 555fa9e4066Sahrens int 556fa9e4066Sahrens dsl_pool_sync_context(dsl_pool_t *dp) 557fa9e4066Sahrens { 558fa9e4066Sahrens return (curthread == dp->dp_tx.tx_sync_thread || 559ad135b5dSChristopher Siden spa_is_initializing(dp->dp_spa)); 560fa9e4066Sahrens } 561fa9e4066Sahrens 562fa9e4066Sahrens uint64_t 563fa9e4066Sahrens dsl_pool_adjustedsize(dsl_pool_t *dp, boolean_t netfree) 564fa9e4066Sahrens { 565fa9e4066Sahrens uint64_t space, resv; 566fa9e4066Sahrens 567fa9e4066Sahrens /* 56844cd46caSbillm * Reserve about 1.6% (1/64), or at least 32MB, for allocation 569fa9e4066Sahrens * efficiency. 570fa9e4066Sahrens * XXX The intent log is not accounted for, so it must fit 571fa9e4066Sahrens * within this slop. 572fa9e4066Sahrens * 573fa9e4066Sahrens * If we're trying to assess whether it's OK to do a free, 574fa9e4066Sahrens * cut the reservation in half to allow forward progress 575fa9e4066Sahrens * (e.g. make it possible to rm(1) files from a full pool). 576fa9e4066Sahrens */ 577485bbbf5SGeorge Wilson space = spa_get_dspace(dp->dp_spa); 57844cd46caSbillm resv = MAX(space >> 6, SPA_MINDEVSIZE >> 1); 579fa9e4066Sahrens if (netfree) 580fa9e4066Sahrens resv >>= 1; 581fa9e4066Sahrens 582fa9e4066Sahrens return (space - resv); 583fa9e4066Sahrens } 5841ab7f2deSmaybee 5851ab7f2deSmaybee int 5861ab7f2deSmaybee dsl_pool_tempreserve_space(dsl_pool_t *dp, uint64_t space, dmu_tx_t *tx) 5871ab7f2deSmaybee { 5881ab7f2deSmaybee uint64_t reserved = 0; 5891ab7f2deSmaybee uint64_t write_limit = (zfs_write_limit_override ? 5901ab7f2deSmaybee zfs_write_limit_override : dp->dp_write_limit); 5911ab7f2deSmaybee 5921ab7f2deSmaybee if (zfs_no_write_throttle) { 593c5904d13Seschrock atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], 594c5904d13Seschrock space); 5951ab7f2deSmaybee return (0); 5961ab7f2deSmaybee } 5971ab7f2deSmaybee 5981ab7f2deSmaybee /* 5991ab7f2deSmaybee * Check to see if we have exceeded the maximum allowed IO for 6001ab7f2deSmaybee * this transaction group. We can do this without locks since 6011ab7f2deSmaybee * a little slop here is ok. Note that we do the reserved check 6021ab7f2deSmaybee * with only half the requested reserve: this is because the 6031ab7f2deSmaybee * reserve requests are worst-case, and we really don't want to 6041ab7f2deSmaybee * throttle based off of worst-case estimates. 6051ab7f2deSmaybee */ 6061ab7f2deSmaybee if (write_limit > 0) { 6071ab7f2deSmaybee reserved = dp->dp_space_towrite[tx->tx_txg & TXG_MASK] 6081ab7f2deSmaybee + dp->dp_tempreserved[tx->tx_txg & TXG_MASK] / 2; 6091ab7f2deSmaybee 6101ab7f2deSmaybee if (reserved && reserved > write_limit) 6111ab7f2deSmaybee return (ERESTART); 6121ab7f2deSmaybee } 6131ab7f2deSmaybee 6141ab7f2deSmaybee atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], space); 6151ab7f2deSmaybee 6161ab7f2deSmaybee /* 6171ab7f2deSmaybee * If this transaction group is over 7/8ths capacity, delay 6181ab7f2deSmaybee * the caller 1 clock tick. This will slow down the "fill" 6191ab7f2deSmaybee * rate until the sync process can catch up with us. 6201ab7f2deSmaybee */ 621*0689f76cSAdam Leventhal if (reserved && reserved > (write_limit - (write_limit >> 3))) { 622*0689f76cSAdam Leventhal txg_delay(dp, tx->tx_txg, zfs_throttle_delay, 623*0689f76cSAdam Leventhal zfs_throttle_resolution); 624*0689f76cSAdam Leventhal } 6251ab7f2deSmaybee 6261ab7f2deSmaybee return (0); 6271ab7f2deSmaybee } 6281ab7f2deSmaybee 6291ab7f2deSmaybee void 6301ab7f2deSmaybee dsl_pool_tempreserve_clear(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx) 6311ab7f2deSmaybee { 6321ab7f2deSmaybee ASSERT(dp->dp_tempreserved[tx->tx_txg & TXG_MASK] >= space); 6331ab7f2deSmaybee atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], -space); 6341ab7f2deSmaybee } 6351ab7f2deSmaybee 6361ab7f2deSmaybee void 6371ab7f2deSmaybee dsl_pool_memory_pressure(dsl_pool_t *dp) 6381ab7f2deSmaybee { 6391ab7f2deSmaybee uint64_t space_inuse = 0; 6401ab7f2deSmaybee int i; 6411ab7f2deSmaybee 6421ab7f2deSmaybee if (dp->dp_write_limit == zfs_write_limit_min) 6431ab7f2deSmaybee return; 6441ab7f2deSmaybee 6451ab7f2deSmaybee for (i = 0; i < TXG_SIZE; i++) { 6461ab7f2deSmaybee space_inuse += dp->dp_space_towrite[i]; 6471ab7f2deSmaybee space_inuse += dp->dp_tempreserved[i]; 6481ab7f2deSmaybee } 6491ab7f2deSmaybee dp->dp_write_limit = MAX(zfs_write_limit_min, 6501ab7f2deSmaybee MIN(dp->dp_write_limit, space_inuse / 4)); 6511ab7f2deSmaybee } 6521ab7f2deSmaybee 6531ab7f2deSmaybee void 6541ab7f2deSmaybee dsl_pool_willuse_space(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx) 6551ab7f2deSmaybee { 6561ab7f2deSmaybee if (space > 0) { 6571ab7f2deSmaybee mutex_enter(&dp->dp_lock); 6581ab7f2deSmaybee dp->dp_space_towrite[tx->tx_txg & TXG_MASK] += space; 6591ab7f2deSmaybee mutex_exit(&dp->dp_lock); 6601ab7f2deSmaybee } 6611ab7f2deSmaybee } 662088f3894Sahrens 663088f3894Sahrens /* ARGSUSED */ 664088f3894Sahrens static int 6653b2aab18SMatthew Ahrens upgrade_clones_cb(dsl_pool_t *dp, dsl_dataset_t *hds, void *arg) 666088f3894Sahrens { 667088f3894Sahrens dmu_tx_t *tx = arg; 668088f3894Sahrens dsl_dataset_t *ds, *prev = NULL; 669088f3894Sahrens int err; 670088f3894Sahrens 6713b2aab18SMatthew Ahrens err = dsl_dataset_hold_obj(dp, hds->ds_object, FTAG, &ds); 672088f3894Sahrens if (err) 673088f3894Sahrens return (err); 674088f3894Sahrens 675088f3894Sahrens while (ds->ds_phys->ds_prev_snap_obj != 0) { 676088f3894Sahrens err = dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj, 677088f3894Sahrens FTAG, &prev); 678088f3894Sahrens if (err) { 679088f3894Sahrens dsl_dataset_rele(ds, FTAG); 680088f3894Sahrens return (err); 681088f3894Sahrens } 682088f3894Sahrens 683088f3894Sahrens if (prev->ds_phys->ds_next_snap_obj != ds->ds_object) 684088f3894Sahrens break; 685088f3894Sahrens dsl_dataset_rele(ds, FTAG); 686088f3894Sahrens ds = prev; 687088f3894Sahrens prev = NULL; 688088f3894Sahrens } 689088f3894Sahrens 690088f3894Sahrens if (prev == NULL) { 691088f3894Sahrens prev = dp->dp_origin_snap; 692088f3894Sahrens 693088f3894Sahrens /* 694088f3894Sahrens * The $ORIGIN can't have any data, or the accounting 695088f3894Sahrens * will be wrong. 696088f3894Sahrens */ 6973b2aab18SMatthew Ahrens ASSERT0(prev->ds_phys->ds_bp.blk_birth); 698088f3894Sahrens 699088f3894Sahrens /* The origin doesn't get attached to itself */ 700088f3894Sahrens if (ds->ds_object == prev->ds_object) { 701088f3894Sahrens dsl_dataset_rele(ds, FTAG); 702088f3894Sahrens return (0); 703088f3894Sahrens } 704088f3894Sahrens 705088f3894Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 706088f3894Sahrens ds->ds_phys->ds_prev_snap_obj = prev->ds_object; 707088f3894Sahrens ds->ds_phys->ds_prev_snap_txg = prev->ds_phys->ds_creation_txg; 708088f3894Sahrens 709088f3894Sahrens dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx); 710088f3894Sahrens ds->ds_dir->dd_phys->dd_origin_obj = prev->ds_object; 711088f3894Sahrens 712088f3894Sahrens dmu_buf_will_dirty(prev->ds_dbuf, tx); 713088f3894Sahrens prev->ds_phys->ds_num_children++; 714088f3894Sahrens 715088f3894Sahrens if (ds->ds_phys->ds_next_snap_obj == 0) { 716088f3894Sahrens ASSERT(ds->ds_prev == NULL); 7173b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, 718088f3894Sahrens ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev)); 719088f3894Sahrens } 720088f3894Sahrens } 721088f3894Sahrens 7223b2aab18SMatthew Ahrens ASSERT3U(ds->ds_dir->dd_phys->dd_origin_obj, ==, prev->ds_object); 7233b2aab18SMatthew Ahrens ASSERT3U(ds->ds_phys->ds_prev_snap_obj, ==, prev->ds_object); 724088f3894Sahrens 725088f3894Sahrens if (prev->ds_phys->ds_next_clones_obj == 0) { 726c33e334fSMatthew Ahrens dmu_buf_will_dirty(prev->ds_dbuf, tx); 727088f3894Sahrens prev->ds_phys->ds_next_clones_obj = 728088f3894Sahrens zap_create(dp->dp_meta_objset, 729088f3894Sahrens DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx); 730088f3894Sahrens } 7313b2aab18SMatthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset, 732088f3894Sahrens prev->ds_phys->ds_next_clones_obj, ds->ds_object, tx)); 733088f3894Sahrens 734088f3894Sahrens dsl_dataset_rele(ds, FTAG); 735088f3894Sahrens if (prev != dp->dp_origin_snap) 736088f3894Sahrens dsl_dataset_rele(prev, FTAG); 737088f3894Sahrens return (0); 738088f3894Sahrens } 739088f3894Sahrens 740088f3894Sahrens void 741088f3894Sahrens dsl_pool_upgrade_clones(dsl_pool_t *dp, dmu_tx_t *tx) 742088f3894Sahrens { 743088f3894Sahrens ASSERT(dmu_tx_is_syncing(tx)); 744088f3894Sahrens ASSERT(dp->dp_origin_snap != NULL); 745088f3894Sahrens 7463b2aab18SMatthew Ahrens VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj, upgrade_clones_cb, 747c33e334fSMatthew Ahrens tx, DS_FIND_CHILDREN)); 748088f3894Sahrens } 749088f3894Sahrens 750cde58dbcSMatthew Ahrens /* ARGSUSED */ 751cde58dbcSMatthew Ahrens static int 7523b2aab18SMatthew Ahrens upgrade_dir_clones_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg) 753cde58dbcSMatthew Ahrens { 754cde58dbcSMatthew Ahrens dmu_tx_t *tx = arg; 755cde58dbcSMatthew Ahrens objset_t *mos = dp->dp_meta_objset; 756cde58dbcSMatthew Ahrens 7573b2aab18SMatthew Ahrens if (ds->ds_dir->dd_phys->dd_origin_obj != 0) { 758cde58dbcSMatthew Ahrens dsl_dataset_t *origin; 759cde58dbcSMatthew Ahrens 7603b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, 761cde58dbcSMatthew Ahrens ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &origin)); 762cde58dbcSMatthew Ahrens 763cde58dbcSMatthew Ahrens if (origin->ds_dir->dd_phys->dd_clones == 0) { 764cde58dbcSMatthew Ahrens dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx); 765cde58dbcSMatthew Ahrens origin->ds_dir->dd_phys->dd_clones = zap_create(mos, 766cde58dbcSMatthew Ahrens DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx); 767cde58dbcSMatthew Ahrens } 768cde58dbcSMatthew Ahrens 7693b2aab18SMatthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset, 7703b2aab18SMatthew Ahrens origin->ds_dir->dd_phys->dd_clones, ds->ds_object, tx)); 771cde58dbcSMatthew Ahrens 772cde58dbcSMatthew Ahrens dsl_dataset_rele(origin, FTAG); 773cde58dbcSMatthew Ahrens } 774cde58dbcSMatthew Ahrens return (0); 775cde58dbcSMatthew Ahrens } 776cde58dbcSMatthew Ahrens 777cde58dbcSMatthew Ahrens void 778cde58dbcSMatthew Ahrens dsl_pool_upgrade_dir_clones(dsl_pool_t *dp, dmu_tx_t *tx) 779cde58dbcSMatthew Ahrens { 780cde58dbcSMatthew Ahrens ASSERT(dmu_tx_is_syncing(tx)); 781cde58dbcSMatthew Ahrens uint64_t obj; 782cde58dbcSMatthew Ahrens 783cde58dbcSMatthew Ahrens (void) dsl_dir_create_sync(dp, dp->dp_root_dir, FREE_DIR_NAME, tx); 7843b2aab18SMatthew Ahrens VERIFY0(dsl_pool_open_special_dir(dp, 785cde58dbcSMatthew Ahrens FREE_DIR_NAME, &dp->dp_free_dir)); 786cde58dbcSMatthew Ahrens 787cde58dbcSMatthew Ahrens /* 788cde58dbcSMatthew Ahrens * We can't use bpobj_alloc(), because spa_version() still 789cde58dbcSMatthew Ahrens * returns the old version, and we need a new-version bpobj with 790cde58dbcSMatthew Ahrens * subobj support. So call dmu_object_alloc() directly. 791cde58dbcSMatthew Ahrens */ 792cde58dbcSMatthew Ahrens obj = dmu_object_alloc(dp->dp_meta_objset, DMU_OT_BPOBJ, 793cde58dbcSMatthew Ahrens SPA_MAXBLOCKSIZE, DMU_OT_BPOBJ_HDR, sizeof (bpobj_phys_t), tx); 7943b2aab18SMatthew Ahrens VERIFY0(zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 795cde58dbcSMatthew Ahrens DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj, tx)); 7963b2aab18SMatthew Ahrens VERIFY0(bpobj_open(&dp->dp_free_bpobj, dp->dp_meta_objset, obj)); 797cde58dbcSMatthew Ahrens 7983b2aab18SMatthew Ahrens VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj, 799cde58dbcSMatthew Ahrens upgrade_dir_clones_cb, tx, DS_FIND_CHILDREN)); 800cde58dbcSMatthew Ahrens } 801cde58dbcSMatthew Ahrens 802088f3894Sahrens void 803088f3894Sahrens dsl_pool_create_origin(dsl_pool_t *dp, dmu_tx_t *tx) 804088f3894Sahrens { 805088f3894Sahrens uint64_t dsobj; 806088f3894Sahrens dsl_dataset_t *ds; 807088f3894Sahrens 808088f3894Sahrens ASSERT(dmu_tx_is_syncing(tx)); 809088f3894Sahrens ASSERT(dp->dp_origin_snap == NULL); 8103b2aab18SMatthew Ahrens ASSERT(rrw_held(&dp->dp_config_rwlock, RW_WRITER)); 811088f3894Sahrens 812088f3894Sahrens /* create the origin dir, ds, & snap-ds */ 813088f3894Sahrens dsobj = dsl_dataset_create_sync(dp->dp_root_dir, ORIGIN_DIR_NAME, 814088f3894Sahrens NULL, 0, kcred, tx); 8153b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds)); 8163b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync_impl(ds, ORIGIN_DIR_NAME, tx); 8173b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj, 818088f3894Sahrens dp, &dp->dp_origin_snap)); 819088f3894Sahrens dsl_dataset_rele(ds, FTAG); 820088f3894Sahrens } 8219d3574bfSNeil Perrin 8229d3574bfSNeil Perrin taskq_t * 8239d3574bfSNeil Perrin dsl_pool_vnrele_taskq(dsl_pool_t *dp) 8249d3574bfSNeil Perrin { 8259d3574bfSNeil Perrin return (dp->dp_vnrele_taskq); 8269d3574bfSNeil Perrin } 827ca45db41SChris Kirby 828ca45db41SChris Kirby /* 829ca45db41SChris Kirby * Walk through the pool-wide zap object of temporary snapshot user holds 830ca45db41SChris Kirby * and release them. 831ca45db41SChris Kirby */ 832ca45db41SChris Kirby void 833ca45db41SChris Kirby dsl_pool_clean_tmp_userrefs(dsl_pool_t *dp) 834ca45db41SChris Kirby { 835ca45db41SChris Kirby zap_attribute_t za; 836ca45db41SChris Kirby zap_cursor_t zc; 837ca45db41SChris Kirby objset_t *mos = dp->dp_meta_objset; 838ca45db41SChris Kirby uint64_t zapobj = dp->dp_tmp_userrefs_obj; 839ca45db41SChris Kirby 840ca45db41SChris Kirby if (zapobj == 0) 841ca45db41SChris Kirby return; 842ca45db41SChris Kirby ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS); 843ca45db41SChris Kirby 844ca45db41SChris Kirby for (zap_cursor_init(&zc, mos, zapobj); 845ca45db41SChris Kirby zap_cursor_retrieve(&zc, &za) == 0; 846ca45db41SChris Kirby zap_cursor_advance(&zc)) { 847ca45db41SChris Kirby char *htag; 848ca45db41SChris Kirby uint64_t dsobj; 849ca45db41SChris Kirby 850ca45db41SChris Kirby htag = strchr(za.za_name, '-'); 851ca45db41SChris Kirby *htag = '\0'; 852ca45db41SChris Kirby ++htag; 853ca45db41SChris Kirby dsobj = strtonum(za.za_name, NULL); 8543b2aab18SMatthew Ahrens dsl_dataset_user_release_tmp(dp, dsobj, htag); 855ca45db41SChris Kirby } 856ca45db41SChris Kirby zap_cursor_fini(&zc); 857ca45db41SChris Kirby } 858ca45db41SChris Kirby 859ca45db41SChris Kirby /* 860ca45db41SChris Kirby * Create the pool-wide zap object for storing temporary snapshot holds. 861ca45db41SChris Kirby */ 862ca45db41SChris Kirby void 863ca45db41SChris Kirby dsl_pool_user_hold_create_obj(dsl_pool_t *dp, dmu_tx_t *tx) 864ca45db41SChris Kirby { 865ca45db41SChris Kirby objset_t *mos = dp->dp_meta_objset; 866ca45db41SChris Kirby 867ca45db41SChris Kirby ASSERT(dp->dp_tmp_userrefs_obj == 0); 868ca45db41SChris Kirby ASSERT(dmu_tx_is_syncing(tx)); 869ca45db41SChris Kirby 870ad135b5dSChristopher Siden dp->dp_tmp_userrefs_obj = zap_create_link(mos, DMU_OT_USERREFS, 871ad135b5dSChristopher Siden DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_TMP_USERREFS, tx); 872ca45db41SChris Kirby } 873ca45db41SChris Kirby 874ca45db41SChris Kirby static int 875ca45db41SChris Kirby dsl_pool_user_hold_rele_impl(dsl_pool_t *dp, uint64_t dsobj, 8763b2aab18SMatthew Ahrens const char *tag, uint64_t now, dmu_tx_t *tx, boolean_t holding) 877ca45db41SChris Kirby { 878ca45db41SChris Kirby objset_t *mos = dp->dp_meta_objset; 879ca45db41SChris Kirby uint64_t zapobj = dp->dp_tmp_userrefs_obj; 880ca45db41SChris Kirby char *name; 881ca45db41SChris Kirby int error; 882ca45db41SChris Kirby 883ca45db41SChris Kirby ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS); 884ca45db41SChris Kirby ASSERT(dmu_tx_is_syncing(tx)); 885ca45db41SChris Kirby 886ca45db41SChris Kirby /* 887ca45db41SChris Kirby * If the pool was created prior to SPA_VERSION_USERREFS, the 888ca45db41SChris Kirby * zap object for temporary holds might not exist yet. 889ca45db41SChris Kirby */ 890ca45db41SChris Kirby if (zapobj == 0) { 891ca45db41SChris Kirby if (holding) { 892ca45db41SChris Kirby dsl_pool_user_hold_create_obj(dp, tx); 893ca45db41SChris Kirby zapobj = dp->dp_tmp_userrefs_obj; 894ca45db41SChris Kirby } else { 895ca45db41SChris Kirby return (ENOENT); 896ca45db41SChris Kirby } 897ca45db41SChris Kirby } 898ca45db41SChris Kirby 899ca45db41SChris Kirby name = kmem_asprintf("%llx-%s", (u_longlong_t)dsobj, tag); 900ca45db41SChris Kirby if (holding) 9013b2aab18SMatthew Ahrens error = zap_add(mos, zapobj, name, 8, 1, &now, tx); 902ca45db41SChris Kirby else 903ca45db41SChris Kirby error = zap_remove(mos, zapobj, name, tx); 904ca45db41SChris Kirby strfree(name); 905ca45db41SChris Kirby 906ca45db41SChris Kirby return (error); 907ca45db41SChris Kirby } 908ca45db41SChris Kirby 909ca45db41SChris Kirby /* 910ca45db41SChris Kirby * Add a temporary hold for the given dataset object and tag. 911ca45db41SChris Kirby */ 912ca45db41SChris Kirby int 913ca45db41SChris Kirby dsl_pool_user_hold(dsl_pool_t *dp, uint64_t dsobj, const char *tag, 9143b2aab18SMatthew Ahrens uint64_t now, dmu_tx_t *tx) 915ca45db41SChris Kirby { 91615508ac0SChris Kirby return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, now, tx, B_TRUE)); 917ca45db41SChris Kirby } 918ca45db41SChris Kirby 919ca45db41SChris Kirby /* 920ca45db41SChris Kirby * Release a temporary hold for the given dataset object and tag. 921ca45db41SChris Kirby */ 922ca45db41SChris Kirby int 923ca45db41SChris Kirby dsl_pool_user_release(dsl_pool_t *dp, uint64_t dsobj, const char *tag, 924ca45db41SChris Kirby dmu_tx_t *tx) 925ca45db41SChris Kirby { 926ca45db41SChris Kirby return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, NULL, 927ca45db41SChris Kirby tx, B_FALSE)); 928ca45db41SChris Kirby } 9293b2aab18SMatthew Ahrens 9303b2aab18SMatthew Ahrens /* 9313b2aab18SMatthew Ahrens * DSL Pool Configuration Lock 9323b2aab18SMatthew Ahrens * 9333b2aab18SMatthew Ahrens * The dp_config_rwlock protects against changes to DSL state (e.g. dataset 9343b2aab18SMatthew Ahrens * creation / destruction / rename / property setting). It must be held for 9353b2aab18SMatthew Ahrens * read to hold a dataset or dsl_dir. I.e. you must call 9363b2aab18SMatthew Ahrens * dsl_pool_config_enter() or dsl_pool_hold() before calling 9373b2aab18SMatthew Ahrens * dsl_{dataset,dir}_hold{_obj}. In most circumstances, the dp_config_rwlock 9383b2aab18SMatthew Ahrens * must be held continuously until all datasets and dsl_dirs are released. 9393b2aab18SMatthew Ahrens * 9403b2aab18SMatthew Ahrens * The only exception to this rule is that if a "long hold" is placed on 9413b2aab18SMatthew Ahrens * a dataset, then the dp_config_rwlock may be dropped while the dataset 9423b2aab18SMatthew Ahrens * is still held. The long hold will prevent the dataset from being 9433b2aab18SMatthew Ahrens * destroyed -- the destroy will fail with EBUSY. A long hold can be 9443b2aab18SMatthew Ahrens * obtained by calling dsl_dataset_long_hold(), or by "owning" a dataset 9453b2aab18SMatthew Ahrens * (by calling dsl_{dataset,objset}_{try}own{_obj}). 9463b2aab18SMatthew Ahrens * 9473b2aab18SMatthew Ahrens * Legitimate long-holders (including owners) should be long-running, cancelable 9483b2aab18SMatthew Ahrens * tasks that should cause "zfs destroy" to fail. This includes DMU 9493b2aab18SMatthew Ahrens * consumers (i.e. a ZPL filesystem being mounted or ZVOL being open), 9503b2aab18SMatthew Ahrens * "zfs send", and "zfs diff". There are several other long-holders whose 9513b2aab18SMatthew Ahrens * uses are suboptimal (e.g. "zfs promote", and zil_suspend()). 9523b2aab18SMatthew Ahrens * 9533b2aab18SMatthew Ahrens * The usual formula for long-holding would be: 9543b2aab18SMatthew Ahrens * dsl_pool_hold() 9553b2aab18SMatthew Ahrens * dsl_dataset_hold() 9563b2aab18SMatthew Ahrens * ... perform checks ... 9573b2aab18SMatthew Ahrens * dsl_dataset_long_hold() 9583b2aab18SMatthew Ahrens * dsl_pool_rele() 9593b2aab18SMatthew Ahrens * ... perform long-running task ... 9603b2aab18SMatthew Ahrens * dsl_dataset_long_rele() 9613b2aab18SMatthew Ahrens * dsl_dataset_rele() 9623b2aab18SMatthew Ahrens * 9633b2aab18SMatthew Ahrens * Note that when the long hold is released, the dataset is still held but 9643b2aab18SMatthew Ahrens * the pool is not held. The dataset may change arbitrarily during this time 9653b2aab18SMatthew Ahrens * (e.g. it could be destroyed). Therefore you shouldn't do anything to the 9663b2aab18SMatthew Ahrens * dataset except release it. 9673b2aab18SMatthew Ahrens * 9683b2aab18SMatthew Ahrens * User-initiated operations (e.g. ioctls, zfs_ioc_*()) are either read-only 9693b2aab18SMatthew Ahrens * or modifying operations. 9703b2aab18SMatthew Ahrens * 9713b2aab18SMatthew Ahrens * Modifying operations should generally use dsl_sync_task(). The synctask 9723b2aab18SMatthew Ahrens * infrastructure enforces proper locking strategy with respect to the 9733b2aab18SMatthew Ahrens * dp_config_rwlock. See the comment above dsl_sync_task() for details. 9743b2aab18SMatthew Ahrens * 9753b2aab18SMatthew Ahrens * Read-only operations will manually hold the pool, then the dataset, obtain 9763b2aab18SMatthew Ahrens * information from the dataset, then release the pool and dataset. 9773b2aab18SMatthew Ahrens * dmu_objset_{hold,rele}() are convenience routines that also do the pool 9783b2aab18SMatthew Ahrens * hold/rele. 9793b2aab18SMatthew Ahrens */ 9803b2aab18SMatthew Ahrens 9813b2aab18SMatthew Ahrens int 9823b2aab18SMatthew Ahrens dsl_pool_hold(const char *name, void *tag, dsl_pool_t **dp) 9833b2aab18SMatthew Ahrens { 9843b2aab18SMatthew Ahrens spa_t *spa; 9853b2aab18SMatthew Ahrens int error; 9863b2aab18SMatthew Ahrens 9873b2aab18SMatthew Ahrens error = spa_open(name, &spa, tag); 9883b2aab18SMatthew Ahrens if (error == 0) { 9893b2aab18SMatthew Ahrens *dp = spa_get_dsl(spa); 9903b2aab18SMatthew Ahrens dsl_pool_config_enter(*dp, tag); 9913b2aab18SMatthew Ahrens } 9923b2aab18SMatthew Ahrens return (error); 9933b2aab18SMatthew Ahrens } 9943b2aab18SMatthew Ahrens 9953b2aab18SMatthew Ahrens void 9963b2aab18SMatthew Ahrens dsl_pool_rele(dsl_pool_t *dp, void *tag) 9973b2aab18SMatthew Ahrens { 9983b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, tag); 9993b2aab18SMatthew Ahrens spa_close(dp->dp_spa, tag); 10003b2aab18SMatthew Ahrens } 10013b2aab18SMatthew Ahrens 10023b2aab18SMatthew Ahrens void 10033b2aab18SMatthew Ahrens dsl_pool_config_enter(dsl_pool_t *dp, void *tag) 10043b2aab18SMatthew Ahrens { 10053b2aab18SMatthew Ahrens /* 10063b2aab18SMatthew Ahrens * We use a "reentrant" reader-writer lock, but not reentrantly. 10073b2aab18SMatthew Ahrens * 10083b2aab18SMatthew Ahrens * The rrwlock can (with the track_all flag) track all reading threads, 10093b2aab18SMatthew Ahrens * which is very useful for debugging which code path failed to release 10103b2aab18SMatthew Ahrens * the lock, and for verifying that the *current* thread does hold 10113b2aab18SMatthew Ahrens * the lock. 10123b2aab18SMatthew Ahrens * 10133b2aab18SMatthew Ahrens * (Unlike a rwlock, which knows that N threads hold it for 10143b2aab18SMatthew Ahrens * read, but not *which* threads, so rw_held(RW_READER) returns TRUE 10153b2aab18SMatthew Ahrens * if any thread holds it for read, even if this thread doesn't). 10163b2aab18SMatthew Ahrens */ 10173b2aab18SMatthew Ahrens ASSERT(!rrw_held(&dp->dp_config_rwlock, RW_READER)); 10183b2aab18SMatthew Ahrens rrw_enter(&dp->dp_config_rwlock, RW_READER, tag); 10193b2aab18SMatthew Ahrens } 10203b2aab18SMatthew Ahrens 10213b2aab18SMatthew Ahrens void 10223b2aab18SMatthew Ahrens dsl_pool_config_exit(dsl_pool_t *dp, void *tag) 10233b2aab18SMatthew Ahrens { 10243b2aab18SMatthew Ahrens rrw_exit(&dp->dp_config_rwlock, tag); 10253b2aab18SMatthew Ahrens } 10263b2aab18SMatthew Ahrens 10273b2aab18SMatthew Ahrens boolean_t 10283b2aab18SMatthew Ahrens dsl_pool_config_held(dsl_pool_t *dp) 10293b2aab18SMatthew Ahrens { 10303b2aab18SMatthew Ahrens return (RRW_LOCK_HELD(&dp->dp_config_rwlock)); 10313b2aab18SMatthew Ahrens } 1032