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 /* 22*8f63aa46SLin Ling * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23fa9e4066Sahrens * Use is subject to license terms. 24fa9e4066Sahrens */ 25fa9e4066Sahrens 26fa9e4066Sahrens #include <sys/dsl_pool.h> 27fa9e4066Sahrens #include <sys/dsl_dataset.h> 28fa9e4066Sahrens #include <sys/dsl_dir.h> 291d452cf5Sahrens #include <sys/dsl_synctask.h> 30fa9e4066Sahrens #include <sys/dmu_tx.h> 31fa9e4066Sahrens #include <sys/dmu_objset.h> 32fa9e4066Sahrens #include <sys/arc.h> 33fa9e4066Sahrens #include <sys/zap.h> 34c717a561Smaybee #include <sys/zio.h> 35fa9e4066Sahrens #include <sys/zfs_context.h> 36fa9e4066Sahrens #include <sys/fs/zfs.h> 37088f3894Sahrens #include <sys/zfs_znode.h> 38088f3894Sahrens #include <sys/spa_impl.h> 39fa9e4066Sahrens 401ab7f2deSmaybee int zfs_no_write_throttle = 0; 4105715f94SMark Maybee int zfs_write_limit_shift = 3; /* 1/8th of physical memory */ 4205715f94SMark Maybee int zfs_txg_synctime = 5; /* target secs to sync a txg */ 4305715f94SMark Maybee 4405715f94SMark Maybee uint64_t zfs_write_limit_min = 32 << 20; /* min write limit is 32MB */ 4505715f94SMark Maybee uint64_t zfs_write_limit_max = 0; /* max data payload per txg */ 4605715f94SMark Maybee uint64_t zfs_write_limit_inflated = 0; 471ab7f2deSmaybee uint64_t zfs_write_limit_override = 0; 481ab7f2deSmaybee 4905715f94SMark Maybee kmutex_t zfs_write_limit_lock; 5005715f94SMark Maybee 5105715f94SMark Maybee static pgcnt_t old_physmem = 0; 52088f3894Sahrens 53ea8dc4b6Seschrock static int 54088f3894Sahrens dsl_pool_open_special_dir(dsl_pool_t *dp, const char *name, dsl_dir_t **ddp) 55fa9e4066Sahrens { 56fa9e4066Sahrens uint64_t obj; 57fa9e4066Sahrens int err; 58fa9e4066Sahrens 59fa9e4066Sahrens err = zap_lookup(dp->dp_meta_objset, 60fa9e4066Sahrens dp->dp_root_dir->dd_phys->dd_child_dir_zapobj, 61088f3894Sahrens name, sizeof (obj), 1, &obj); 62ea8dc4b6Seschrock if (err) 63ea8dc4b6Seschrock return (err); 64fa9e4066Sahrens 65088f3894Sahrens return (dsl_dir_open_obj(dp, obj, name, dp, ddp)); 66fa9e4066Sahrens } 67fa9e4066Sahrens 68fa9e4066Sahrens static dsl_pool_t * 69fa9e4066Sahrens dsl_pool_open_impl(spa_t *spa, uint64_t txg) 70fa9e4066Sahrens { 71fa9e4066Sahrens dsl_pool_t *dp; 72fa9e4066Sahrens blkptr_t *bp = spa_get_rootblkptr(spa); 73fa9e4066Sahrens 74fa9e4066Sahrens dp = kmem_zalloc(sizeof (dsl_pool_t), KM_SLEEP); 75fa9e4066Sahrens dp->dp_spa = spa; 76fa9e4066Sahrens dp->dp_meta_rootbp = *bp; 775ad82045Snd rw_init(&dp->dp_config_rwlock, NULL, RW_DEFAULT, NULL); 781ab7f2deSmaybee dp->dp_write_limit = zfs_write_limit_min; 79fa9e4066Sahrens txg_init(dp, txg); 80fa9e4066Sahrens 81fa9e4066Sahrens txg_list_create(&dp->dp_dirty_datasets, 82fa9e4066Sahrens offsetof(dsl_dataset_t, ds_dirty_link)); 83fa9e4066Sahrens txg_list_create(&dp->dp_dirty_dirs, 84fa9e4066Sahrens offsetof(dsl_dir_t, dd_dirty_link)); 851d452cf5Sahrens txg_list_create(&dp->dp_sync_tasks, 861d452cf5Sahrens offsetof(dsl_sync_task_group_t, dstg_node)); 873cb34c60Sahrens list_create(&dp->dp_synced_datasets, sizeof (dsl_dataset_t), 88fa9e4066Sahrens offsetof(dsl_dataset_t, ds_synced_link)); 89fa9e4066Sahrens 901ab7f2deSmaybee mutex_init(&dp->dp_lock, NULL, MUTEX_DEFAULT, NULL); 91088f3894Sahrens mutex_init(&dp->dp_scrub_cancel_lock, NULL, MUTEX_DEFAULT, NULL); 921ab7f2deSmaybee 93fa9e4066Sahrens return (dp); 94fa9e4066Sahrens } 95fa9e4066Sahrens 96ea8dc4b6Seschrock int 97ea8dc4b6Seschrock dsl_pool_open(spa_t *spa, uint64_t txg, dsl_pool_t **dpp) 98fa9e4066Sahrens { 99fa9e4066Sahrens int err; 100fa9e4066Sahrens dsl_pool_t *dp = dsl_pool_open_impl(spa, txg); 101088f3894Sahrens dsl_dir_t *dd; 102088f3894Sahrens dsl_dataset_t *ds; 103ea8dc4b6Seschrock objset_impl_t *osi; 104fa9e4066Sahrens 105088f3894Sahrens rw_enter(&dp->dp_config_rwlock, RW_WRITER); 106ea8dc4b6Seschrock err = dmu_objset_open_impl(spa, NULL, &dp->dp_meta_rootbp, &osi); 107ea8dc4b6Seschrock if (err) 108ea8dc4b6Seschrock goto out; 109ea8dc4b6Seschrock dp->dp_meta_objset = &osi->os; 110ea8dc4b6Seschrock 111fa9e4066Sahrens err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 112fa9e4066Sahrens DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, 113fa9e4066Sahrens &dp->dp_root_dir_obj); 114ea8dc4b6Seschrock if (err) 115ea8dc4b6Seschrock goto out; 116ea8dc4b6Seschrock 117ea8dc4b6Seschrock err = dsl_dir_open_obj(dp, dp->dp_root_dir_obj, 118ea8dc4b6Seschrock NULL, dp, &dp->dp_root_dir); 119ea8dc4b6Seschrock if (err) 120ea8dc4b6Seschrock goto out; 121fa9e4066Sahrens 122088f3894Sahrens err = dsl_pool_open_special_dir(dp, MOS_DIR_NAME, &dp->dp_mos_dir); 123ea8dc4b6Seschrock if (err) 124ea8dc4b6Seschrock goto out; 125ea8dc4b6Seschrock 126088f3894Sahrens if (spa_version(spa) >= SPA_VERSION_ORIGIN) { 127088f3894Sahrens err = dsl_pool_open_special_dir(dp, ORIGIN_DIR_NAME, &dd); 128088f3894Sahrens if (err) 129088f3894Sahrens goto out; 130088f3894Sahrens err = dsl_dataset_hold_obj(dp, dd->dd_phys->dd_head_dataset_obj, 131088f3894Sahrens FTAG, &ds); 132*8f63aa46SLin Ling if (err == 0) { 133*8f63aa46SLin Ling err = dsl_dataset_hold_obj(dp, 134*8f63aa46SLin Ling ds->ds_phys->ds_prev_snap_obj, dp, 135*8f63aa46SLin Ling &dp->dp_origin_snap); 136*8f63aa46SLin Ling dsl_dataset_rele(ds, FTAG); 137*8f63aa46SLin Ling } 138*8f63aa46SLin Ling dsl_dir_close(dd, dp); 139088f3894Sahrens if (err) 140088f3894Sahrens goto out; 141088f3894Sahrens } 142088f3894Sahrens 143088f3894Sahrens /* get scrub status */ 144088f3894Sahrens err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 145088f3894Sahrens DMU_POOL_SCRUB_FUNC, sizeof (uint32_t), 1, 146088f3894Sahrens &dp->dp_scrub_func); 147088f3894Sahrens if (err == 0) { 148088f3894Sahrens err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 149088f3894Sahrens DMU_POOL_SCRUB_QUEUE, sizeof (uint64_t), 1, 150088f3894Sahrens &dp->dp_scrub_queue_obj); 151088f3894Sahrens if (err) 152088f3894Sahrens goto out; 153088f3894Sahrens err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 154088f3894Sahrens DMU_POOL_SCRUB_MIN_TXG, sizeof (uint64_t), 1, 155088f3894Sahrens &dp->dp_scrub_min_txg); 156088f3894Sahrens if (err) 157088f3894Sahrens goto out; 158088f3894Sahrens err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 159088f3894Sahrens DMU_POOL_SCRUB_MAX_TXG, sizeof (uint64_t), 1, 160088f3894Sahrens &dp->dp_scrub_max_txg); 161088f3894Sahrens if (err) 162088f3894Sahrens goto out; 163088f3894Sahrens err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 164088f3894Sahrens DMU_POOL_SCRUB_BOOKMARK, sizeof (uint64_t), 4, 165088f3894Sahrens &dp->dp_scrub_bookmark); 166088f3894Sahrens if (err) 167088f3894Sahrens goto out; 168088f3894Sahrens err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 169088f3894Sahrens DMU_POOL_SCRUB_ERRORS, sizeof (uint64_t), 1, 170088f3894Sahrens &spa->spa_scrub_errors); 171088f3894Sahrens if (err) 172088f3894Sahrens goto out; 173088f3894Sahrens if (spa_version(spa) < SPA_VERSION_DSL_SCRUB) { 174088f3894Sahrens /* 175088f3894Sahrens * A new-type scrub was in progress on an old 176088f3894Sahrens * pool. Restart from the beginning, since the 177088f3894Sahrens * old software may have changed the pool in the 178088f3894Sahrens * meantime. 179088f3894Sahrens */ 180088f3894Sahrens dsl_pool_scrub_restart(dp); 181088f3894Sahrens } 182088f3894Sahrens } else { 183088f3894Sahrens /* 184088f3894Sahrens * It's OK if there is no scrub in progress (and if 185088f3894Sahrens * there was an I/O error, ignore it). 186088f3894Sahrens */ 187088f3894Sahrens err = 0; 188088f3894Sahrens } 189088f3894Sahrens 190ea8dc4b6Seschrock out: 191fa9e4066Sahrens rw_exit(&dp->dp_config_rwlock); 192ea8dc4b6Seschrock if (err) 193ea8dc4b6Seschrock dsl_pool_close(dp); 194ea8dc4b6Seschrock else 195ea8dc4b6Seschrock *dpp = dp; 196fa9e4066Sahrens 197ea8dc4b6Seschrock return (err); 198fa9e4066Sahrens } 199fa9e4066Sahrens 200fa9e4066Sahrens void 201fa9e4066Sahrens dsl_pool_close(dsl_pool_t *dp) 202fa9e4066Sahrens { 203088f3894Sahrens /* drop our references from dsl_pool_open() */ 204088f3894Sahrens 205088f3894Sahrens /* 206088f3894Sahrens * Since we held the origin_snap from "syncing" context (which 207088f3894Sahrens * includes pool-opening context), it actually only got a "ref" 208088f3894Sahrens * and not a hold, so just drop that here. 209088f3894Sahrens */ 210088f3894Sahrens if (dp->dp_origin_snap) 211088f3894Sahrens dsl_dataset_drop_ref(dp->dp_origin_snap, dp); 212ea8dc4b6Seschrock if (dp->dp_mos_dir) 213ea8dc4b6Seschrock dsl_dir_close(dp->dp_mos_dir, dp); 214ea8dc4b6Seschrock if (dp->dp_root_dir) 215ea8dc4b6Seschrock dsl_dir_close(dp->dp_root_dir, dp); 216fa9e4066Sahrens 217fa9e4066Sahrens /* undo the dmu_objset_open_impl(mos) from dsl_pool_open() */ 218ea8dc4b6Seschrock if (dp->dp_meta_objset) 219ea8dc4b6Seschrock dmu_objset_evict(NULL, dp->dp_meta_objset->os); 220fa9e4066Sahrens 221fa9e4066Sahrens txg_list_destroy(&dp->dp_dirty_datasets); 222fa9e4066Sahrens txg_list_destroy(&dp->dp_dirty_dirs); 2233cb34c60Sahrens list_destroy(&dp->dp_synced_datasets); 224fa9e4066Sahrens 225874395d5Smaybee arc_flush(dp->dp_spa); 226fa9e4066Sahrens txg_fini(dp); 2275ad82045Snd rw_destroy(&dp->dp_config_rwlock); 2281ab7f2deSmaybee mutex_destroy(&dp->dp_lock); 229088f3894Sahrens mutex_destroy(&dp->dp_scrub_cancel_lock); 23088b7b0f2SMatthew Ahrens if (dp->dp_blkstats) 23188b7b0f2SMatthew Ahrens kmem_free(dp->dp_blkstats, sizeof (zfs_all_blkstats_t)); 232fa9e4066Sahrens kmem_free(dp, sizeof (dsl_pool_t)); 233fa9e4066Sahrens } 234fa9e4066Sahrens 235fa9e4066Sahrens dsl_pool_t * 2360a48a24eStimh dsl_pool_create(spa_t *spa, nvlist_t *zplprops, uint64_t txg) 237fa9e4066Sahrens { 238fa9e4066Sahrens int err; 239fa9e4066Sahrens dsl_pool_t *dp = dsl_pool_open_impl(spa, txg); 240fa9e4066Sahrens dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg); 241088f3894Sahrens objset_impl_t *osip; 242088f3894Sahrens dsl_dataset_t *ds; 243088f3894Sahrens uint64_t dsobj; 244088f3894Sahrens 245088f3894Sahrens /* create and open the MOS (meta-objset) */ 246fa9e4066Sahrens dp->dp_meta_objset = &dmu_objset_create_impl(spa, 247c717a561Smaybee NULL, &dp->dp_meta_rootbp, DMU_OST_META, tx)->os; 248fa9e4066Sahrens 249fa9e4066Sahrens /* create the pool directory */ 250fa9e4066Sahrens err = zap_create_claim(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 251fa9e4066Sahrens DMU_OT_OBJECT_DIRECTORY, DMU_OT_NONE, 0, tx); 252fa9e4066Sahrens ASSERT3U(err, ==, 0); 253fa9e4066Sahrens 254fa9e4066Sahrens /* create and open the root dir */ 255088f3894Sahrens dp->dp_root_dir_obj = dsl_dir_create_sync(dp, NULL, NULL, tx); 256ea8dc4b6Seschrock VERIFY(0 == dsl_dir_open_obj(dp, dp->dp_root_dir_obj, 257ea8dc4b6Seschrock NULL, dp, &dp->dp_root_dir)); 258fa9e4066Sahrens 259fa9e4066Sahrens /* create and open the meta-objset dir */ 260088f3894Sahrens (void) dsl_dir_create_sync(dp, dp->dp_root_dir, MOS_DIR_NAME, tx); 261088f3894Sahrens VERIFY(0 == dsl_pool_open_special_dir(dp, 262088f3894Sahrens MOS_DIR_NAME, &dp->dp_mos_dir)); 263088f3894Sahrens 264088f3894Sahrens if (spa_version(spa) >= SPA_VERSION_DSL_SCRUB) 265088f3894Sahrens dsl_pool_create_origin(dp, tx); 266088f3894Sahrens 267088f3894Sahrens /* create the root dataset */ 268088f3894Sahrens dsobj = dsl_dataset_create_sync_dd(dp->dp_root_dir, NULL, 0, tx); 269088f3894Sahrens 270088f3894Sahrens /* create the root objset */ 271088f3894Sahrens VERIFY(0 == dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds)); 272088f3894Sahrens osip = dmu_objset_create_impl(dp->dp_spa, ds, 273088f3894Sahrens dsl_dataset_get_blkptr(ds), DMU_OST_ZFS, tx); 274088f3894Sahrens #ifdef _KERNEL 2750a48a24eStimh zfs_create_fs(&osip->os, kcred, zplprops, tx); 276088f3894Sahrens #endif 277088f3894Sahrens dsl_dataset_rele(ds, FTAG); 278fa9e4066Sahrens 279fa9e4066Sahrens dmu_tx_commit(tx); 280fa9e4066Sahrens 281fa9e4066Sahrens return (dp); 282fa9e4066Sahrens } 283fa9e4066Sahrens 284fa9e4066Sahrens void 285fa9e4066Sahrens dsl_pool_sync(dsl_pool_t *dp, uint64_t txg) 286fa9e4066Sahrens { 287c717a561Smaybee zio_t *zio; 288fa9e4066Sahrens dmu_tx_t *tx; 289c717a561Smaybee dsl_dir_t *dd; 290c717a561Smaybee dsl_dataset_t *ds; 291c717a561Smaybee dsl_sync_task_group_t *dstg; 292fa9e4066Sahrens objset_impl_t *mosi = dp->dp_meta_objset->os; 29305715f94SMark Maybee hrtime_t start, write_time; 29405715f94SMark Maybee uint64_t data_written; 295c717a561Smaybee int err; 296fa9e4066Sahrens 297fa9e4066Sahrens tx = dmu_tx_create_assigned(dp, txg); 298fa9e4066Sahrens 29905715f94SMark Maybee dp->dp_read_overhead = 0; 300c717a561Smaybee zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 301c717a561Smaybee while (ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) { 302c717a561Smaybee if (!list_link_active(&ds->ds_synced_link)) 3033cb34c60Sahrens list_insert_tail(&dp->dp_synced_datasets, ds); 304af2c4821Smaybee else 305af2c4821Smaybee dmu_buf_rele(ds->ds_dbuf, ds); 306c717a561Smaybee dsl_dataset_sync(ds, zio, tx); 307c717a561Smaybee } 30805715f94SMark Maybee DTRACE_PROBE(pool_sync__1setup); 30905715f94SMark Maybee 31005715f94SMark Maybee start = gethrtime(); 311c717a561Smaybee err = zio_wait(zio); 31205715f94SMark Maybee write_time = gethrtime() - start; 313c717a561Smaybee ASSERT(err == 0); 31405715f94SMark Maybee DTRACE_PROBE(pool_sync__2rootzio); 315c717a561Smaybee 316c717a561Smaybee while (dstg = txg_list_remove(&dp->dp_sync_tasks, txg)) 317c717a561Smaybee dsl_sync_task_group_sync(dstg, tx); 31805715f94SMark Maybee DTRACE_PROBE(pool_sync__3task); 31905715f94SMark Maybee 32005715f94SMark Maybee start = gethrtime(); 321c717a561Smaybee while (dd = txg_list_remove(&dp->dp_dirty_dirs, txg)) 322c717a561Smaybee dsl_dir_sync(dd, tx); 32305715f94SMark Maybee write_time += gethrtime() - start; 324fa9e4066Sahrens 325088f3894Sahrens if (spa_sync_pass(dp->dp_spa) == 1) 326088f3894Sahrens dsl_pool_scrub_sync(dp, tx); 327088f3894Sahrens 32805715f94SMark Maybee start = gethrtime(); 329fa9e4066Sahrens if (list_head(&mosi->os_dirty_dnodes[txg & TXG_MASK]) != NULL || 330fa9e4066Sahrens list_head(&mosi->os_free_dnodes[txg & TXG_MASK]) != NULL) { 331c717a561Smaybee zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 332c717a561Smaybee dmu_objset_sync(mosi, zio, tx); 333c717a561Smaybee err = zio_wait(zio); 334c717a561Smaybee ASSERT(err == 0); 335fa9e4066Sahrens dprintf_bp(&dp->dp_meta_rootbp, "meta objset rootbp is %s", ""); 336fa9e4066Sahrens spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp); 337fa9e4066Sahrens } 33805715f94SMark Maybee write_time += gethrtime() - start; 33905715f94SMark Maybee DTRACE_PROBE2(pool_sync__4io, hrtime_t, write_time, 34005715f94SMark Maybee hrtime_t, dp->dp_read_overhead); 34105715f94SMark Maybee write_time -= dp->dp_read_overhead; 342fa9e4066Sahrens 343fa9e4066Sahrens dmu_tx_commit(tx); 34405715f94SMark Maybee 34505715f94SMark Maybee data_written = dp->dp_space_towrite[txg & TXG_MASK]; 34605715f94SMark Maybee dp->dp_space_towrite[txg & TXG_MASK] = 0; 34705715f94SMark Maybee ASSERT(dp->dp_tempreserved[txg & TXG_MASK] == 0); 34805715f94SMark Maybee 34905715f94SMark Maybee /* 35005715f94SMark Maybee * If the write limit max has not been explicitly set, set it 35105715f94SMark Maybee * to a fraction of available physical memory (default 1/8th). 35205715f94SMark Maybee * Note that we must inflate the limit because the spa 35305715f94SMark Maybee * inflates write sizes to account for data replication. 35405715f94SMark Maybee * Check this each sync phase to catch changing memory size. 35505715f94SMark Maybee */ 35605715f94SMark Maybee if (physmem != old_physmem && zfs_write_limit_shift) { 35705715f94SMark Maybee mutex_enter(&zfs_write_limit_lock); 35805715f94SMark Maybee old_physmem = physmem; 35905715f94SMark Maybee zfs_write_limit_max = ptob(physmem) >> zfs_write_limit_shift; 36005715f94SMark Maybee zfs_write_limit_inflated = MAX(zfs_write_limit_min, 36105715f94SMark Maybee spa_get_asize(dp->dp_spa, zfs_write_limit_max)); 36205715f94SMark Maybee mutex_exit(&zfs_write_limit_lock); 36305715f94SMark Maybee } 36405715f94SMark Maybee 36505715f94SMark Maybee /* 36605715f94SMark Maybee * Attempt to keep the sync time consistent by adjusting the 36705715f94SMark Maybee * amount of write traffic allowed into each transaction group. 36805715f94SMark Maybee * Weight the throughput calculation towards the current value: 36905715f94SMark Maybee * thru = 3/4 old_thru + 1/4 new_thru 37005715f94SMark Maybee */ 37105715f94SMark Maybee ASSERT(zfs_write_limit_min > 0); 37205715f94SMark Maybee if (data_written > zfs_write_limit_min / 8 && write_time > 0) { 37305715f94SMark Maybee uint64_t throughput = (data_written * NANOSEC) / write_time; 37405715f94SMark Maybee if (dp->dp_throughput) 37505715f94SMark Maybee dp->dp_throughput = throughput / 4 + 37605715f94SMark Maybee 3 * dp->dp_throughput / 4; 37705715f94SMark Maybee else 37805715f94SMark Maybee dp->dp_throughput = throughput; 37905715f94SMark Maybee dp->dp_write_limit = MIN(zfs_write_limit_inflated, 38005715f94SMark Maybee MAX(zfs_write_limit_min, 38105715f94SMark Maybee dp->dp_throughput * zfs_txg_synctime)); 38205715f94SMark Maybee } 383fa9e4066Sahrens } 384fa9e4066Sahrens 385fa9e4066Sahrens void 386fa9e4066Sahrens dsl_pool_zil_clean(dsl_pool_t *dp) 387fa9e4066Sahrens { 388fa9e4066Sahrens dsl_dataset_t *ds; 389fa9e4066Sahrens 3903cb34c60Sahrens while (ds = list_head(&dp->dp_synced_datasets)) { 3913cb34c60Sahrens list_remove(&dp->dp_synced_datasets, ds); 392fa9e4066Sahrens ASSERT(ds->ds_user_ptr != NULL); 393fa9e4066Sahrens zil_clean(((objset_impl_t *)ds->ds_user_ptr)->os_zil); 394af2c4821Smaybee dmu_buf_rele(ds->ds_dbuf, ds); 395fa9e4066Sahrens } 396fa9e4066Sahrens } 397fa9e4066Sahrens 398c717a561Smaybee /* 399c717a561Smaybee * TRUE if the current thread is the tx_sync_thread or if we 400c717a561Smaybee * are being called from SPA context during pool initialization. 401c717a561Smaybee */ 402fa9e4066Sahrens int 403fa9e4066Sahrens dsl_pool_sync_context(dsl_pool_t *dp) 404fa9e4066Sahrens { 405fa9e4066Sahrens return (curthread == dp->dp_tx.tx_sync_thread || 406c717a561Smaybee spa_get_dsl(dp->dp_spa) == NULL); 407fa9e4066Sahrens } 408fa9e4066Sahrens 409fa9e4066Sahrens uint64_t 410fa9e4066Sahrens dsl_pool_adjustedsize(dsl_pool_t *dp, boolean_t netfree) 411fa9e4066Sahrens { 412fa9e4066Sahrens uint64_t space, resv; 413fa9e4066Sahrens 414fa9e4066Sahrens /* 41544cd46caSbillm * Reserve about 1.6% (1/64), or at least 32MB, for allocation 416fa9e4066Sahrens * efficiency. 417fa9e4066Sahrens * XXX The intent log is not accounted for, so it must fit 418fa9e4066Sahrens * within this slop. 419fa9e4066Sahrens * 420fa9e4066Sahrens * If we're trying to assess whether it's OK to do a free, 421fa9e4066Sahrens * cut the reservation in half to allow forward progress 422fa9e4066Sahrens * (e.g. make it possible to rm(1) files from a full pool). 423fa9e4066Sahrens */ 42499653d4eSeschrock space = spa_get_dspace(dp->dp_spa); 42544cd46caSbillm resv = MAX(space >> 6, SPA_MINDEVSIZE >> 1); 426fa9e4066Sahrens if (netfree) 427fa9e4066Sahrens resv >>= 1; 428fa9e4066Sahrens 429fa9e4066Sahrens return (space - resv); 430fa9e4066Sahrens } 4311ab7f2deSmaybee 4321ab7f2deSmaybee int 4331ab7f2deSmaybee dsl_pool_tempreserve_space(dsl_pool_t *dp, uint64_t space, dmu_tx_t *tx) 4341ab7f2deSmaybee { 4351ab7f2deSmaybee uint64_t reserved = 0; 4361ab7f2deSmaybee uint64_t write_limit = (zfs_write_limit_override ? 4371ab7f2deSmaybee zfs_write_limit_override : dp->dp_write_limit); 4381ab7f2deSmaybee 4391ab7f2deSmaybee if (zfs_no_write_throttle) { 440c5904d13Seschrock atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], 441c5904d13Seschrock space); 4421ab7f2deSmaybee return (0); 4431ab7f2deSmaybee } 4441ab7f2deSmaybee 4451ab7f2deSmaybee /* 4461ab7f2deSmaybee * Check to see if we have exceeded the maximum allowed IO for 4471ab7f2deSmaybee * this transaction group. We can do this without locks since 4481ab7f2deSmaybee * a little slop here is ok. Note that we do the reserved check 4491ab7f2deSmaybee * with only half the requested reserve: this is because the 4501ab7f2deSmaybee * reserve requests are worst-case, and we really don't want to 4511ab7f2deSmaybee * throttle based off of worst-case estimates. 4521ab7f2deSmaybee */ 4531ab7f2deSmaybee if (write_limit > 0) { 4541ab7f2deSmaybee reserved = dp->dp_space_towrite[tx->tx_txg & TXG_MASK] 4551ab7f2deSmaybee + dp->dp_tempreserved[tx->tx_txg & TXG_MASK] / 2; 4561ab7f2deSmaybee 4571ab7f2deSmaybee if (reserved && reserved > write_limit) 4581ab7f2deSmaybee return (ERESTART); 4591ab7f2deSmaybee } 4601ab7f2deSmaybee 4611ab7f2deSmaybee atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], space); 4621ab7f2deSmaybee 4631ab7f2deSmaybee /* 4641ab7f2deSmaybee * If this transaction group is over 7/8ths capacity, delay 4651ab7f2deSmaybee * the caller 1 clock tick. This will slow down the "fill" 4661ab7f2deSmaybee * rate until the sync process can catch up with us. 4671ab7f2deSmaybee */ 468e8397a2bSgw if (reserved && reserved > (write_limit - (write_limit >> 3))) 4691ab7f2deSmaybee txg_delay(dp, tx->tx_txg, 1); 4701ab7f2deSmaybee 4711ab7f2deSmaybee return (0); 4721ab7f2deSmaybee } 4731ab7f2deSmaybee 4741ab7f2deSmaybee void 4751ab7f2deSmaybee dsl_pool_tempreserve_clear(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx) 4761ab7f2deSmaybee { 4771ab7f2deSmaybee ASSERT(dp->dp_tempreserved[tx->tx_txg & TXG_MASK] >= space); 4781ab7f2deSmaybee atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], -space); 4791ab7f2deSmaybee } 4801ab7f2deSmaybee 4811ab7f2deSmaybee void 4821ab7f2deSmaybee dsl_pool_memory_pressure(dsl_pool_t *dp) 4831ab7f2deSmaybee { 4841ab7f2deSmaybee uint64_t space_inuse = 0; 4851ab7f2deSmaybee int i; 4861ab7f2deSmaybee 4871ab7f2deSmaybee if (dp->dp_write_limit == zfs_write_limit_min) 4881ab7f2deSmaybee return; 4891ab7f2deSmaybee 4901ab7f2deSmaybee for (i = 0; i < TXG_SIZE; i++) { 4911ab7f2deSmaybee space_inuse += dp->dp_space_towrite[i]; 4921ab7f2deSmaybee space_inuse += dp->dp_tempreserved[i]; 4931ab7f2deSmaybee } 4941ab7f2deSmaybee dp->dp_write_limit = MAX(zfs_write_limit_min, 4951ab7f2deSmaybee MIN(dp->dp_write_limit, space_inuse / 4)); 4961ab7f2deSmaybee } 4971ab7f2deSmaybee 4981ab7f2deSmaybee void 4991ab7f2deSmaybee dsl_pool_willuse_space(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx) 5001ab7f2deSmaybee { 5011ab7f2deSmaybee if (space > 0) { 5021ab7f2deSmaybee mutex_enter(&dp->dp_lock); 5031ab7f2deSmaybee dp->dp_space_towrite[tx->tx_txg & TXG_MASK] += space; 5041ab7f2deSmaybee mutex_exit(&dp->dp_lock); 5051ab7f2deSmaybee } 5061ab7f2deSmaybee } 507088f3894Sahrens 508088f3894Sahrens /* ARGSUSED */ 509088f3894Sahrens static int 510088f3894Sahrens upgrade_clones_cb(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg) 511088f3894Sahrens { 512088f3894Sahrens dmu_tx_t *tx = arg; 513088f3894Sahrens dsl_dataset_t *ds, *prev = NULL; 514088f3894Sahrens int err; 515088f3894Sahrens dsl_pool_t *dp = spa_get_dsl(spa); 516088f3894Sahrens 517088f3894Sahrens err = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds); 518088f3894Sahrens if (err) 519088f3894Sahrens return (err); 520088f3894Sahrens 521088f3894Sahrens while (ds->ds_phys->ds_prev_snap_obj != 0) { 522088f3894Sahrens err = dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj, 523088f3894Sahrens FTAG, &prev); 524088f3894Sahrens if (err) { 525088f3894Sahrens dsl_dataset_rele(ds, FTAG); 526088f3894Sahrens return (err); 527088f3894Sahrens } 528088f3894Sahrens 529088f3894Sahrens if (prev->ds_phys->ds_next_snap_obj != ds->ds_object) 530088f3894Sahrens break; 531088f3894Sahrens dsl_dataset_rele(ds, FTAG); 532088f3894Sahrens ds = prev; 533088f3894Sahrens prev = NULL; 534088f3894Sahrens } 535088f3894Sahrens 536088f3894Sahrens if (prev == NULL) { 537088f3894Sahrens prev = dp->dp_origin_snap; 538088f3894Sahrens 539088f3894Sahrens /* 540088f3894Sahrens * The $ORIGIN can't have any data, or the accounting 541088f3894Sahrens * will be wrong. 542088f3894Sahrens */ 543088f3894Sahrens ASSERT(prev->ds_phys->ds_bp.blk_birth == 0); 544088f3894Sahrens 545088f3894Sahrens /* The origin doesn't get attached to itself */ 546088f3894Sahrens if (ds->ds_object == prev->ds_object) { 547088f3894Sahrens dsl_dataset_rele(ds, FTAG); 548088f3894Sahrens return (0); 549088f3894Sahrens } 550088f3894Sahrens 551088f3894Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 552088f3894Sahrens ds->ds_phys->ds_prev_snap_obj = prev->ds_object; 553088f3894Sahrens ds->ds_phys->ds_prev_snap_txg = prev->ds_phys->ds_creation_txg; 554088f3894Sahrens 555088f3894Sahrens dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx); 556088f3894Sahrens ds->ds_dir->dd_phys->dd_origin_obj = prev->ds_object; 557088f3894Sahrens 558088f3894Sahrens dmu_buf_will_dirty(prev->ds_dbuf, tx); 559088f3894Sahrens prev->ds_phys->ds_num_children++; 560088f3894Sahrens 561088f3894Sahrens if (ds->ds_phys->ds_next_snap_obj == 0) { 562088f3894Sahrens ASSERT(ds->ds_prev == NULL); 563088f3894Sahrens VERIFY(0 == dsl_dataset_hold_obj(dp, 564088f3894Sahrens ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev)); 565088f3894Sahrens } 566088f3894Sahrens } 567088f3894Sahrens 568088f3894Sahrens ASSERT(ds->ds_dir->dd_phys->dd_origin_obj == prev->ds_object); 569088f3894Sahrens ASSERT(ds->ds_phys->ds_prev_snap_obj == prev->ds_object); 570088f3894Sahrens 571088f3894Sahrens if (prev->ds_phys->ds_next_clones_obj == 0) { 572088f3894Sahrens prev->ds_phys->ds_next_clones_obj = 573088f3894Sahrens zap_create(dp->dp_meta_objset, 574088f3894Sahrens DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx); 575088f3894Sahrens } 576088f3894Sahrens VERIFY(0 == zap_add_int(dp->dp_meta_objset, 577088f3894Sahrens prev->ds_phys->ds_next_clones_obj, ds->ds_object, tx)); 578088f3894Sahrens 579088f3894Sahrens dsl_dataset_rele(ds, FTAG); 580088f3894Sahrens if (prev != dp->dp_origin_snap) 581088f3894Sahrens dsl_dataset_rele(prev, FTAG); 582088f3894Sahrens return (0); 583088f3894Sahrens } 584088f3894Sahrens 585088f3894Sahrens void 586088f3894Sahrens dsl_pool_upgrade_clones(dsl_pool_t *dp, dmu_tx_t *tx) 587088f3894Sahrens { 588088f3894Sahrens ASSERT(dmu_tx_is_syncing(tx)); 589088f3894Sahrens ASSERT(dp->dp_origin_snap != NULL); 590088f3894Sahrens 591088f3894Sahrens (void) dmu_objset_find_spa(dp->dp_spa, NULL, upgrade_clones_cb, 592088f3894Sahrens tx, DS_FIND_CHILDREN); 593088f3894Sahrens } 594088f3894Sahrens 595088f3894Sahrens void 596088f3894Sahrens dsl_pool_create_origin(dsl_pool_t *dp, dmu_tx_t *tx) 597088f3894Sahrens { 598088f3894Sahrens uint64_t dsobj; 599088f3894Sahrens dsl_dataset_t *ds; 600088f3894Sahrens 601088f3894Sahrens ASSERT(dmu_tx_is_syncing(tx)); 602088f3894Sahrens ASSERT(dp->dp_origin_snap == NULL); 603088f3894Sahrens 604088f3894Sahrens /* create the origin dir, ds, & snap-ds */ 605088f3894Sahrens rw_enter(&dp->dp_config_rwlock, RW_WRITER); 606088f3894Sahrens dsobj = dsl_dataset_create_sync(dp->dp_root_dir, ORIGIN_DIR_NAME, 607088f3894Sahrens NULL, 0, kcred, tx); 608088f3894Sahrens VERIFY(0 == dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds)); 609088f3894Sahrens dsl_dataset_snapshot_sync(ds, ORIGIN_DIR_NAME, kcred, tx); 610088f3894Sahrens VERIFY(0 == dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj, 611088f3894Sahrens dp, &dp->dp_origin_snap)); 612088f3894Sahrens dsl_dataset_rele(ds, FTAG); 613088f3894Sahrens rw_exit(&dp->dp_config_rwlock); 614088f3894Sahrens } 615