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 /* 221ab7f2deSmaybee * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 23fa9e4066Sahrens * Use is subject to license terms. 24fa9e4066Sahrens */ 25fa9e4066Sahrens 26fa9e4066Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 27fa9e4066Sahrens 28fa9e4066Sahrens #include <sys/dsl_pool.h> 29fa9e4066Sahrens #include <sys/dsl_dataset.h> 30fa9e4066Sahrens #include <sys/dsl_dir.h> 311d452cf5Sahrens #include <sys/dsl_synctask.h> 32fa9e4066Sahrens #include <sys/dmu_tx.h> 33fa9e4066Sahrens #include <sys/dmu_objset.h> 34fa9e4066Sahrens #include <sys/arc.h> 35fa9e4066Sahrens #include <sys/zap.h> 36c717a561Smaybee #include <sys/zio.h> 37fa9e4066Sahrens #include <sys/zfs_context.h> 38fa9e4066Sahrens #include <sys/fs/zfs.h> 39*088f3894Sahrens #include <sys/zfs_znode.h> 40*088f3894Sahrens #include <sys/spa_impl.h> 41fa9e4066Sahrens 421ab7f2deSmaybee int zfs_no_write_throttle = 0; 431ab7f2deSmaybee uint64_t zfs_write_limit_override = 0; 441ab7f2deSmaybee 45*088f3894Sahrens 46ea8dc4b6Seschrock static int 47*088f3894Sahrens dsl_pool_open_special_dir(dsl_pool_t *dp, const char *name, dsl_dir_t **ddp) 48fa9e4066Sahrens { 49fa9e4066Sahrens uint64_t obj; 50fa9e4066Sahrens int err; 51fa9e4066Sahrens 52fa9e4066Sahrens err = zap_lookup(dp->dp_meta_objset, 53fa9e4066Sahrens dp->dp_root_dir->dd_phys->dd_child_dir_zapobj, 54*088f3894Sahrens name, sizeof (obj), 1, &obj); 55ea8dc4b6Seschrock if (err) 56ea8dc4b6Seschrock return (err); 57fa9e4066Sahrens 58*088f3894Sahrens return (dsl_dir_open_obj(dp, obj, name, dp, ddp)); 59fa9e4066Sahrens } 60fa9e4066Sahrens 61fa9e4066Sahrens static dsl_pool_t * 62fa9e4066Sahrens dsl_pool_open_impl(spa_t *spa, uint64_t txg) 63fa9e4066Sahrens { 64fa9e4066Sahrens dsl_pool_t *dp; 65fa9e4066Sahrens blkptr_t *bp = spa_get_rootblkptr(spa); 661ab7f2deSmaybee extern uint64_t zfs_write_limit_min; 67fa9e4066Sahrens 68fa9e4066Sahrens dp = kmem_zalloc(sizeof (dsl_pool_t), KM_SLEEP); 69fa9e4066Sahrens dp->dp_spa = spa; 70fa9e4066Sahrens dp->dp_meta_rootbp = *bp; 715ad82045Snd rw_init(&dp->dp_config_rwlock, NULL, RW_DEFAULT, NULL); 721ab7f2deSmaybee dp->dp_write_limit = zfs_write_limit_min; 73fa9e4066Sahrens txg_init(dp, txg); 74fa9e4066Sahrens 75fa9e4066Sahrens txg_list_create(&dp->dp_dirty_datasets, 76fa9e4066Sahrens offsetof(dsl_dataset_t, ds_dirty_link)); 77fa9e4066Sahrens txg_list_create(&dp->dp_dirty_dirs, 78fa9e4066Sahrens offsetof(dsl_dir_t, dd_dirty_link)); 791d452cf5Sahrens txg_list_create(&dp->dp_sync_tasks, 801d452cf5Sahrens offsetof(dsl_sync_task_group_t, dstg_node)); 813cb34c60Sahrens list_create(&dp->dp_synced_datasets, sizeof (dsl_dataset_t), 82fa9e4066Sahrens offsetof(dsl_dataset_t, ds_synced_link)); 83fa9e4066Sahrens 841ab7f2deSmaybee mutex_init(&dp->dp_lock, NULL, MUTEX_DEFAULT, NULL); 85*088f3894Sahrens mutex_init(&dp->dp_scrub_cancel_lock, NULL, MUTEX_DEFAULT, NULL); 861ab7f2deSmaybee 87fa9e4066Sahrens return (dp); 88fa9e4066Sahrens } 89fa9e4066Sahrens 90ea8dc4b6Seschrock int 91ea8dc4b6Seschrock dsl_pool_open(spa_t *spa, uint64_t txg, dsl_pool_t **dpp) 92fa9e4066Sahrens { 93fa9e4066Sahrens int err; 94fa9e4066Sahrens dsl_pool_t *dp = dsl_pool_open_impl(spa, txg); 95*088f3894Sahrens dsl_dir_t *dd; 96*088f3894Sahrens dsl_dataset_t *ds; 97ea8dc4b6Seschrock objset_impl_t *osi; 98fa9e4066Sahrens 99*088f3894Sahrens rw_enter(&dp->dp_config_rwlock, RW_WRITER); 100ea8dc4b6Seschrock err = dmu_objset_open_impl(spa, NULL, &dp->dp_meta_rootbp, &osi); 101ea8dc4b6Seschrock if (err) 102ea8dc4b6Seschrock goto out; 103ea8dc4b6Seschrock dp->dp_meta_objset = &osi->os; 104ea8dc4b6Seschrock 105fa9e4066Sahrens err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 106fa9e4066Sahrens DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, 107fa9e4066Sahrens &dp->dp_root_dir_obj); 108ea8dc4b6Seschrock if (err) 109ea8dc4b6Seschrock goto out; 110ea8dc4b6Seschrock 111ea8dc4b6Seschrock err = dsl_dir_open_obj(dp, dp->dp_root_dir_obj, 112ea8dc4b6Seschrock NULL, dp, &dp->dp_root_dir); 113ea8dc4b6Seschrock if (err) 114ea8dc4b6Seschrock goto out; 115fa9e4066Sahrens 116*088f3894Sahrens err = dsl_pool_open_special_dir(dp, MOS_DIR_NAME, &dp->dp_mos_dir); 117ea8dc4b6Seschrock if (err) 118ea8dc4b6Seschrock goto out; 119ea8dc4b6Seschrock 120*088f3894Sahrens if (spa_version(spa) >= SPA_VERSION_ORIGIN) { 121*088f3894Sahrens err = dsl_pool_open_special_dir(dp, ORIGIN_DIR_NAME, &dd); 122*088f3894Sahrens if (err) 123*088f3894Sahrens goto out; 124*088f3894Sahrens err = dsl_dataset_hold_obj(dp, dd->dd_phys->dd_head_dataset_obj, 125*088f3894Sahrens FTAG, &ds); 126*088f3894Sahrens if (err) 127*088f3894Sahrens goto out; 128*088f3894Sahrens err = dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj, 129*088f3894Sahrens dp, &dp->dp_origin_snap); 130*088f3894Sahrens if (err) 131*088f3894Sahrens goto out; 132*088f3894Sahrens dsl_dataset_rele(ds, FTAG); 133*088f3894Sahrens dsl_dir_close(dd, dp); 134*088f3894Sahrens } 135*088f3894Sahrens 136*088f3894Sahrens /* get scrub status */ 137*088f3894Sahrens err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 138*088f3894Sahrens DMU_POOL_SCRUB_FUNC, sizeof (uint32_t), 1, 139*088f3894Sahrens &dp->dp_scrub_func); 140*088f3894Sahrens if (err == 0) { 141*088f3894Sahrens err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 142*088f3894Sahrens DMU_POOL_SCRUB_QUEUE, sizeof (uint64_t), 1, 143*088f3894Sahrens &dp->dp_scrub_queue_obj); 144*088f3894Sahrens if (err) 145*088f3894Sahrens goto out; 146*088f3894Sahrens err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 147*088f3894Sahrens DMU_POOL_SCRUB_MIN_TXG, sizeof (uint64_t), 1, 148*088f3894Sahrens &dp->dp_scrub_min_txg); 149*088f3894Sahrens if (err) 150*088f3894Sahrens goto out; 151*088f3894Sahrens err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 152*088f3894Sahrens DMU_POOL_SCRUB_MAX_TXG, sizeof (uint64_t), 1, 153*088f3894Sahrens &dp->dp_scrub_max_txg); 154*088f3894Sahrens if (err) 155*088f3894Sahrens goto out; 156*088f3894Sahrens err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 157*088f3894Sahrens DMU_POOL_SCRUB_BOOKMARK, sizeof (uint64_t), 4, 158*088f3894Sahrens &dp->dp_scrub_bookmark); 159*088f3894Sahrens if (err) 160*088f3894Sahrens goto out; 161*088f3894Sahrens err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 162*088f3894Sahrens DMU_POOL_SCRUB_ERRORS, sizeof (uint64_t), 1, 163*088f3894Sahrens &spa->spa_scrub_errors); 164*088f3894Sahrens if (err) 165*088f3894Sahrens goto out; 166*088f3894Sahrens if (spa_version(spa) < SPA_VERSION_DSL_SCRUB) { 167*088f3894Sahrens /* 168*088f3894Sahrens * A new-type scrub was in progress on an old 169*088f3894Sahrens * pool. Restart from the beginning, since the 170*088f3894Sahrens * old software may have changed the pool in the 171*088f3894Sahrens * meantime. 172*088f3894Sahrens */ 173*088f3894Sahrens dsl_pool_scrub_restart(dp); 174*088f3894Sahrens } 175*088f3894Sahrens } else { 176*088f3894Sahrens /* 177*088f3894Sahrens * It's OK if there is no scrub in progress (and if 178*088f3894Sahrens * there was an I/O error, ignore it). 179*088f3894Sahrens */ 180*088f3894Sahrens err = 0; 181*088f3894Sahrens } 182*088f3894Sahrens 183ea8dc4b6Seschrock out: 184fa9e4066Sahrens rw_exit(&dp->dp_config_rwlock); 185ea8dc4b6Seschrock if (err) 186ea8dc4b6Seschrock dsl_pool_close(dp); 187ea8dc4b6Seschrock else 188ea8dc4b6Seschrock *dpp = dp; 189fa9e4066Sahrens 190ea8dc4b6Seschrock return (err); 191fa9e4066Sahrens } 192fa9e4066Sahrens 193fa9e4066Sahrens void 194fa9e4066Sahrens dsl_pool_close(dsl_pool_t *dp) 195fa9e4066Sahrens { 196*088f3894Sahrens /* drop our references from dsl_pool_open() */ 197*088f3894Sahrens 198*088f3894Sahrens /* 199*088f3894Sahrens * Since we held the origin_snap from "syncing" context (which 200*088f3894Sahrens * includes pool-opening context), it actually only got a "ref" 201*088f3894Sahrens * and not a hold, so just drop that here. 202*088f3894Sahrens */ 203*088f3894Sahrens if (dp->dp_origin_snap) 204*088f3894Sahrens dsl_dataset_drop_ref(dp->dp_origin_snap, dp); 205ea8dc4b6Seschrock if (dp->dp_mos_dir) 206ea8dc4b6Seschrock dsl_dir_close(dp->dp_mos_dir, dp); 207ea8dc4b6Seschrock if (dp->dp_root_dir) 208ea8dc4b6Seschrock dsl_dir_close(dp->dp_root_dir, dp); 209fa9e4066Sahrens 210fa9e4066Sahrens /* undo the dmu_objset_open_impl(mos) from dsl_pool_open() */ 211ea8dc4b6Seschrock if (dp->dp_meta_objset) 212ea8dc4b6Seschrock dmu_objset_evict(NULL, dp->dp_meta_objset->os); 213fa9e4066Sahrens 214fa9e4066Sahrens txg_list_destroy(&dp->dp_dirty_datasets); 215fa9e4066Sahrens txg_list_destroy(&dp->dp_dirty_dirs); 2163cb34c60Sahrens list_destroy(&dp->dp_synced_datasets); 217fa9e4066Sahrens 218874395d5Smaybee arc_flush(dp->dp_spa); 219fa9e4066Sahrens txg_fini(dp); 2205ad82045Snd rw_destroy(&dp->dp_config_rwlock); 2211ab7f2deSmaybee mutex_destroy(&dp->dp_lock); 222*088f3894Sahrens mutex_destroy(&dp->dp_scrub_cancel_lock); 223fa9e4066Sahrens kmem_free(dp, sizeof (dsl_pool_t)); 224fa9e4066Sahrens } 225fa9e4066Sahrens 226fa9e4066Sahrens dsl_pool_t * 227fa9e4066Sahrens dsl_pool_create(spa_t *spa, uint64_t txg) 228fa9e4066Sahrens { 229fa9e4066Sahrens int err; 230fa9e4066Sahrens dsl_pool_t *dp = dsl_pool_open_impl(spa, txg); 231fa9e4066Sahrens dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg); 232*088f3894Sahrens objset_impl_t *osip; 233*088f3894Sahrens dsl_dataset_t *ds; 234*088f3894Sahrens uint64_t dsobj; 235*088f3894Sahrens 236*088f3894Sahrens /* create and open the MOS (meta-objset) */ 237fa9e4066Sahrens dp->dp_meta_objset = &dmu_objset_create_impl(spa, 238c717a561Smaybee NULL, &dp->dp_meta_rootbp, DMU_OST_META, tx)->os; 239fa9e4066Sahrens 240fa9e4066Sahrens /* create the pool directory */ 241fa9e4066Sahrens err = zap_create_claim(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 242fa9e4066Sahrens DMU_OT_OBJECT_DIRECTORY, DMU_OT_NONE, 0, tx); 243fa9e4066Sahrens ASSERT3U(err, ==, 0); 244fa9e4066Sahrens 245fa9e4066Sahrens /* create and open the root dir */ 246*088f3894Sahrens dp->dp_root_dir_obj = dsl_dir_create_sync(dp, NULL, NULL, tx); 247ea8dc4b6Seschrock VERIFY(0 == dsl_dir_open_obj(dp, dp->dp_root_dir_obj, 248ea8dc4b6Seschrock NULL, dp, &dp->dp_root_dir)); 249fa9e4066Sahrens 250fa9e4066Sahrens /* create and open the meta-objset dir */ 251*088f3894Sahrens (void) dsl_dir_create_sync(dp, dp->dp_root_dir, MOS_DIR_NAME, tx); 252*088f3894Sahrens VERIFY(0 == dsl_pool_open_special_dir(dp, 253*088f3894Sahrens MOS_DIR_NAME, &dp->dp_mos_dir)); 254*088f3894Sahrens 255*088f3894Sahrens if (spa_version(spa) >= SPA_VERSION_DSL_SCRUB) 256*088f3894Sahrens dsl_pool_create_origin(dp, tx); 257*088f3894Sahrens 258*088f3894Sahrens /* create the root dataset */ 259*088f3894Sahrens dsobj = dsl_dataset_create_sync_dd(dp->dp_root_dir, NULL, 0, tx); 260*088f3894Sahrens 261*088f3894Sahrens /* create the root objset */ 262*088f3894Sahrens VERIFY(0 == dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds)); 263*088f3894Sahrens osip = dmu_objset_create_impl(dp->dp_spa, ds, 264*088f3894Sahrens dsl_dataset_get_blkptr(ds), DMU_OST_ZFS, tx); 265*088f3894Sahrens #ifdef _KERNEL 266*088f3894Sahrens zfs_create_fs(&osip->os, kcred, NULL, tx); 267*088f3894Sahrens #endif 268*088f3894Sahrens dsl_dataset_rele(ds, FTAG); 269fa9e4066Sahrens 270fa9e4066Sahrens dmu_tx_commit(tx); 271fa9e4066Sahrens 272fa9e4066Sahrens return (dp); 273fa9e4066Sahrens } 274fa9e4066Sahrens 275fa9e4066Sahrens void 276fa9e4066Sahrens dsl_pool_sync(dsl_pool_t *dp, uint64_t txg) 277fa9e4066Sahrens { 278c717a561Smaybee zio_t *zio; 279fa9e4066Sahrens dmu_tx_t *tx; 280c717a561Smaybee dsl_dir_t *dd; 281c717a561Smaybee dsl_dataset_t *ds; 282c717a561Smaybee dsl_sync_task_group_t *dstg; 283fa9e4066Sahrens objset_impl_t *mosi = dp->dp_meta_objset->os; 284c717a561Smaybee int err; 285fa9e4066Sahrens 286fa9e4066Sahrens tx = dmu_tx_create_assigned(dp, txg); 287fa9e4066Sahrens 288c717a561Smaybee zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 289c717a561Smaybee while (ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) { 290c717a561Smaybee if (!list_link_active(&ds->ds_synced_link)) 2913cb34c60Sahrens list_insert_tail(&dp->dp_synced_datasets, ds); 292af2c4821Smaybee else 293af2c4821Smaybee dmu_buf_rele(ds->ds_dbuf, ds); 294c717a561Smaybee dsl_dataset_sync(ds, zio, tx); 295c717a561Smaybee } 296c717a561Smaybee err = zio_wait(zio); 297c717a561Smaybee ASSERT(err == 0); 298c717a561Smaybee 299c717a561Smaybee while (dstg = txg_list_remove(&dp->dp_sync_tasks, txg)) 300c717a561Smaybee dsl_sync_task_group_sync(dstg, tx); 301c717a561Smaybee while (dd = txg_list_remove(&dp->dp_dirty_dirs, txg)) 302c717a561Smaybee dsl_dir_sync(dd, tx); 303fa9e4066Sahrens 304*088f3894Sahrens if (spa_sync_pass(dp->dp_spa) == 1) 305*088f3894Sahrens dsl_pool_scrub_sync(dp, tx); 306*088f3894Sahrens 307fa9e4066Sahrens if (list_head(&mosi->os_dirty_dnodes[txg & TXG_MASK]) != NULL || 308fa9e4066Sahrens list_head(&mosi->os_free_dnodes[txg & TXG_MASK]) != NULL) { 309c717a561Smaybee zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 310c717a561Smaybee dmu_objset_sync(mosi, zio, tx); 311c717a561Smaybee err = zio_wait(zio); 312c717a561Smaybee ASSERT(err == 0); 313fa9e4066Sahrens dprintf_bp(&dp->dp_meta_rootbp, "meta objset rootbp is %s", ""); 314fa9e4066Sahrens spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp); 315fa9e4066Sahrens } 316fa9e4066Sahrens 317fa9e4066Sahrens dmu_tx_commit(tx); 318fa9e4066Sahrens } 319fa9e4066Sahrens 320fa9e4066Sahrens void 321fa9e4066Sahrens dsl_pool_zil_clean(dsl_pool_t *dp) 322fa9e4066Sahrens { 323fa9e4066Sahrens dsl_dataset_t *ds; 324fa9e4066Sahrens 3253cb34c60Sahrens while (ds = list_head(&dp->dp_synced_datasets)) { 3263cb34c60Sahrens list_remove(&dp->dp_synced_datasets, ds); 327fa9e4066Sahrens ASSERT(ds->ds_user_ptr != NULL); 328fa9e4066Sahrens zil_clean(((objset_impl_t *)ds->ds_user_ptr)->os_zil); 329af2c4821Smaybee dmu_buf_rele(ds->ds_dbuf, ds); 330fa9e4066Sahrens } 331fa9e4066Sahrens } 332fa9e4066Sahrens 333c717a561Smaybee /* 334c717a561Smaybee * TRUE if the current thread is the tx_sync_thread or if we 335c717a561Smaybee * are being called from SPA context during pool initialization. 336c717a561Smaybee */ 337fa9e4066Sahrens int 338fa9e4066Sahrens dsl_pool_sync_context(dsl_pool_t *dp) 339fa9e4066Sahrens { 340fa9e4066Sahrens return (curthread == dp->dp_tx.tx_sync_thread || 341c717a561Smaybee spa_get_dsl(dp->dp_spa) == NULL); 342fa9e4066Sahrens } 343fa9e4066Sahrens 344fa9e4066Sahrens uint64_t 345fa9e4066Sahrens dsl_pool_adjustedsize(dsl_pool_t *dp, boolean_t netfree) 346fa9e4066Sahrens { 347fa9e4066Sahrens uint64_t space, resv; 348fa9e4066Sahrens 349fa9e4066Sahrens /* 35044cd46caSbillm * Reserve about 1.6% (1/64), or at least 32MB, for allocation 351fa9e4066Sahrens * efficiency. 352fa9e4066Sahrens * XXX The intent log is not accounted for, so it must fit 353fa9e4066Sahrens * within this slop. 354fa9e4066Sahrens * 355fa9e4066Sahrens * If we're trying to assess whether it's OK to do a free, 356fa9e4066Sahrens * cut the reservation in half to allow forward progress 357fa9e4066Sahrens * (e.g. make it possible to rm(1) files from a full pool). 358fa9e4066Sahrens */ 35999653d4eSeschrock space = spa_get_dspace(dp->dp_spa); 36044cd46caSbillm resv = MAX(space >> 6, SPA_MINDEVSIZE >> 1); 361fa9e4066Sahrens if (netfree) 362fa9e4066Sahrens resv >>= 1; 363fa9e4066Sahrens 364fa9e4066Sahrens return (space - resv); 365fa9e4066Sahrens } 3661ab7f2deSmaybee 3671ab7f2deSmaybee int 3681ab7f2deSmaybee dsl_pool_tempreserve_space(dsl_pool_t *dp, uint64_t space, dmu_tx_t *tx) 3691ab7f2deSmaybee { 3701ab7f2deSmaybee uint64_t reserved = 0; 3711ab7f2deSmaybee uint64_t write_limit = (zfs_write_limit_override ? 3721ab7f2deSmaybee zfs_write_limit_override : dp->dp_write_limit); 3731ab7f2deSmaybee 3741ab7f2deSmaybee if (zfs_no_write_throttle) { 375c5904d13Seschrock atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], 376c5904d13Seschrock space); 3771ab7f2deSmaybee return (0); 3781ab7f2deSmaybee } 3791ab7f2deSmaybee 3801ab7f2deSmaybee /* 3811ab7f2deSmaybee * Check to see if we have exceeded the maximum allowed IO for 3821ab7f2deSmaybee * this transaction group. We can do this without locks since 3831ab7f2deSmaybee * a little slop here is ok. Note that we do the reserved check 3841ab7f2deSmaybee * with only half the requested reserve: this is because the 3851ab7f2deSmaybee * reserve requests are worst-case, and we really don't want to 3861ab7f2deSmaybee * throttle based off of worst-case estimates. 3871ab7f2deSmaybee */ 3881ab7f2deSmaybee if (write_limit > 0) { 3891ab7f2deSmaybee reserved = dp->dp_space_towrite[tx->tx_txg & TXG_MASK] 3901ab7f2deSmaybee + dp->dp_tempreserved[tx->tx_txg & TXG_MASK] / 2; 3911ab7f2deSmaybee 3921ab7f2deSmaybee if (reserved && reserved > write_limit) 3931ab7f2deSmaybee return (ERESTART); 3941ab7f2deSmaybee } 3951ab7f2deSmaybee 3961ab7f2deSmaybee atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], space); 3971ab7f2deSmaybee 3981ab7f2deSmaybee /* 3991ab7f2deSmaybee * If this transaction group is over 7/8ths capacity, delay 4001ab7f2deSmaybee * the caller 1 clock tick. This will slow down the "fill" 4011ab7f2deSmaybee * rate until the sync process can catch up with us. 4021ab7f2deSmaybee */ 403e8397a2bSgw if (reserved && reserved > (write_limit - (write_limit >> 3))) 4041ab7f2deSmaybee txg_delay(dp, tx->tx_txg, 1); 4051ab7f2deSmaybee 4061ab7f2deSmaybee return (0); 4071ab7f2deSmaybee } 4081ab7f2deSmaybee 4091ab7f2deSmaybee void 4101ab7f2deSmaybee dsl_pool_tempreserve_clear(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx) 4111ab7f2deSmaybee { 4121ab7f2deSmaybee ASSERT(dp->dp_tempreserved[tx->tx_txg & TXG_MASK] >= space); 4131ab7f2deSmaybee atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], -space); 4141ab7f2deSmaybee } 4151ab7f2deSmaybee 4161ab7f2deSmaybee void 4171ab7f2deSmaybee dsl_pool_memory_pressure(dsl_pool_t *dp) 4181ab7f2deSmaybee { 4191ab7f2deSmaybee extern uint64_t zfs_write_limit_min; 4201ab7f2deSmaybee uint64_t space_inuse = 0; 4211ab7f2deSmaybee int i; 4221ab7f2deSmaybee 4231ab7f2deSmaybee if (dp->dp_write_limit == zfs_write_limit_min) 4241ab7f2deSmaybee return; 4251ab7f2deSmaybee 4261ab7f2deSmaybee for (i = 0; i < TXG_SIZE; i++) { 4271ab7f2deSmaybee space_inuse += dp->dp_space_towrite[i]; 4281ab7f2deSmaybee space_inuse += dp->dp_tempreserved[i]; 4291ab7f2deSmaybee } 4301ab7f2deSmaybee dp->dp_write_limit = MAX(zfs_write_limit_min, 4311ab7f2deSmaybee MIN(dp->dp_write_limit, space_inuse / 4)); 4321ab7f2deSmaybee } 4331ab7f2deSmaybee 4341ab7f2deSmaybee void 4351ab7f2deSmaybee dsl_pool_willuse_space(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx) 4361ab7f2deSmaybee { 4371ab7f2deSmaybee if (space > 0) { 4381ab7f2deSmaybee mutex_enter(&dp->dp_lock); 4391ab7f2deSmaybee dp->dp_space_towrite[tx->tx_txg & TXG_MASK] += space; 4401ab7f2deSmaybee mutex_exit(&dp->dp_lock); 4411ab7f2deSmaybee } 4421ab7f2deSmaybee } 443*088f3894Sahrens 444*088f3894Sahrens /* ARGSUSED */ 445*088f3894Sahrens static int 446*088f3894Sahrens upgrade_clones_cb(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg) 447*088f3894Sahrens { 448*088f3894Sahrens dmu_tx_t *tx = arg; 449*088f3894Sahrens dsl_dataset_t *ds, *prev = NULL; 450*088f3894Sahrens int err; 451*088f3894Sahrens dsl_pool_t *dp = spa_get_dsl(spa); 452*088f3894Sahrens 453*088f3894Sahrens err = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds); 454*088f3894Sahrens if (err) 455*088f3894Sahrens return (err); 456*088f3894Sahrens 457*088f3894Sahrens while (ds->ds_phys->ds_prev_snap_obj != 0) { 458*088f3894Sahrens err = dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj, 459*088f3894Sahrens FTAG, &prev); 460*088f3894Sahrens if (err) { 461*088f3894Sahrens dsl_dataset_rele(ds, FTAG); 462*088f3894Sahrens return (err); 463*088f3894Sahrens } 464*088f3894Sahrens 465*088f3894Sahrens if (prev->ds_phys->ds_next_snap_obj != ds->ds_object) 466*088f3894Sahrens break; 467*088f3894Sahrens dsl_dataset_rele(ds, FTAG); 468*088f3894Sahrens ds = prev; 469*088f3894Sahrens prev = NULL; 470*088f3894Sahrens } 471*088f3894Sahrens 472*088f3894Sahrens if (prev == NULL) { 473*088f3894Sahrens prev = dp->dp_origin_snap; 474*088f3894Sahrens 475*088f3894Sahrens /* 476*088f3894Sahrens * The $ORIGIN can't have any data, or the accounting 477*088f3894Sahrens * will be wrong. 478*088f3894Sahrens */ 479*088f3894Sahrens ASSERT(prev->ds_phys->ds_bp.blk_birth == 0); 480*088f3894Sahrens 481*088f3894Sahrens /* The origin doesn't get attached to itself */ 482*088f3894Sahrens if (ds->ds_object == prev->ds_object) { 483*088f3894Sahrens dsl_dataset_rele(ds, FTAG); 484*088f3894Sahrens return (0); 485*088f3894Sahrens } 486*088f3894Sahrens 487*088f3894Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 488*088f3894Sahrens ds->ds_phys->ds_prev_snap_obj = prev->ds_object; 489*088f3894Sahrens ds->ds_phys->ds_prev_snap_txg = prev->ds_phys->ds_creation_txg; 490*088f3894Sahrens 491*088f3894Sahrens dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx); 492*088f3894Sahrens ds->ds_dir->dd_phys->dd_origin_obj = prev->ds_object; 493*088f3894Sahrens 494*088f3894Sahrens dmu_buf_will_dirty(prev->ds_dbuf, tx); 495*088f3894Sahrens prev->ds_phys->ds_num_children++; 496*088f3894Sahrens 497*088f3894Sahrens if (ds->ds_phys->ds_next_snap_obj == 0) { 498*088f3894Sahrens ASSERT(ds->ds_prev == NULL); 499*088f3894Sahrens VERIFY(0 == dsl_dataset_hold_obj(dp, 500*088f3894Sahrens ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev)); 501*088f3894Sahrens } 502*088f3894Sahrens } 503*088f3894Sahrens 504*088f3894Sahrens ASSERT(ds->ds_dir->dd_phys->dd_origin_obj == prev->ds_object); 505*088f3894Sahrens ASSERT(ds->ds_phys->ds_prev_snap_obj == prev->ds_object); 506*088f3894Sahrens 507*088f3894Sahrens if (prev->ds_phys->ds_next_clones_obj == 0) { 508*088f3894Sahrens prev->ds_phys->ds_next_clones_obj = 509*088f3894Sahrens zap_create(dp->dp_meta_objset, 510*088f3894Sahrens DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx); 511*088f3894Sahrens } 512*088f3894Sahrens VERIFY(0 == zap_add_int(dp->dp_meta_objset, 513*088f3894Sahrens prev->ds_phys->ds_next_clones_obj, ds->ds_object, tx)); 514*088f3894Sahrens 515*088f3894Sahrens dsl_dataset_rele(ds, FTAG); 516*088f3894Sahrens if (prev != dp->dp_origin_snap) 517*088f3894Sahrens dsl_dataset_rele(prev, FTAG); 518*088f3894Sahrens return (0); 519*088f3894Sahrens } 520*088f3894Sahrens 521*088f3894Sahrens void 522*088f3894Sahrens dsl_pool_upgrade_clones(dsl_pool_t *dp, dmu_tx_t *tx) 523*088f3894Sahrens { 524*088f3894Sahrens ASSERT(dmu_tx_is_syncing(tx)); 525*088f3894Sahrens ASSERT(dp->dp_origin_snap != NULL); 526*088f3894Sahrens 527*088f3894Sahrens (void) dmu_objset_find_spa(dp->dp_spa, NULL, upgrade_clones_cb, 528*088f3894Sahrens tx, DS_FIND_CHILDREN); 529*088f3894Sahrens } 530*088f3894Sahrens 531*088f3894Sahrens void 532*088f3894Sahrens dsl_pool_create_origin(dsl_pool_t *dp, dmu_tx_t *tx) 533*088f3894Sahrens { 534*088f3894Sahrens uint64_t dsobj; 535*088f3894Sahrens dsl_dataset_t *ds; 536*088f3894Sahrens 537*088f3894Sahrens ASSERT(dmu_tx_is_syncing(tx)); 538*088f3894Sahrens ASSERT(dp->dp_origin_snap == NULL); 539*088f3894Sahrens 540*088f3894Sahrens /* create the origin dir, ds, & snap-ds */ 541*088f3894Sahrens rw_enter(&dp->dp_config_rwlock, RW_WRITER); 542*088f3894Sahrens dsobj = dsl_dataset_create_sync(dp->dp_root_dir, ORIGIN_DIR_NAME, 543*088f3894Sahrens NULL, 0, kcred, tx); 544*088f3894Sahrens VERIFY(0 == dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds)); 545*088f3894Sahrens dsl_dataset_snapshot_sync(ds, ORIGIN_DIR_NAME, kcred, tx); 546*088f3894Sahrens VERIFY(0 == dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj, 547*088f3894Sahrens dp, &dp->dp_origin_snap)); 548*088f3894Sahrens dsl_dataset_rele(ds, FTAG); 549*088f3894Sahrens rw_exit(&dp->dp_config_rwlock); 550*088f3894Sahrens } 551