1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51544Seschrock * Common Development and Distribution License (the "License"). 61544Seschrock * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 21789Sahrens /* 229008SLin.Ling@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens #include <sys/dsl_pool.h> 27789Sahrens #include <sys/dsl_dataset.h> 28789Sahrens #include <sys/dsl_dir.h> 292199Sahrens #include <sys/dsl_synctask.h> 30789Sahrens #include <sys/dmu_tx.h> 31789Sahrens #include <sys/dmu_objset.h> 32789Sahrens #include <sys/arc.h> 33789Sahrens #include <sys/zap.h> 343547Smaybee #include <sys/zio.h> 35789Sahrens #include <sys/zfs_context.h> 36789Sahrens #include <sys/fs/zfs.h> 377046Sahrens #include <sys/zfs_znode.h> 387046Sahrens #include <sys/spa_impl.h> 39789Sahrens 406245Smaybee int zfs_no_write_throttle = 0; 417468SMark.Maybee@Sun.COM int zfs_write_limit_shift = 3; /* 1/8th of physical memory */ 427468SMark.Maybee@Sun.COM int zfs_txg_synctime = 5; /* target secs to sync a txg */ 437468SMark.Maybee@Sun.COM 447468SMark.Maybee@Sun.COM uint64_t zfs_write_limit_min = 32 << 20; /* min write limit is 32MB */ 457468SMark.Maybee@Sun.COM uint64_t zfs_write_limit_max = 0; /* max data payload per txg */ 467468SMark.Maybee@Sun.COM uint64_t zfs_write_limit_inflated = 0; 476245Smaybee uint64_t zfs_write_limit_override = 0; 486245Smaybee 497468SMark.Maybee@Sun.COM kmutex_t zfs_write_limit_lock; 507468SMark.Maybee@Sun.COM 517468SMark.Maybee@Sun.COM static pgcnt_t old_physmem = 0; 527046Sahrens 531544Seschrock static int 547046Sahrens dsl_pool_open_special_dir(dsl_pool_t *dp, const char *name, dsl_dir_t **ddp) 55789Sahrens { 56789Sahrens uint64_t obj; 57789Sahrens int err; 58789Sahrens 59789Sahrens err = zap_lookup(dp->dp_meta_objset, 60789Sahrens dp->dp_root_dir->dd_phys->dd_child_dir_zapobj, 617046Sahrens name, sizeof (obj), 1, &obj); 621544Seschrock if (err) 631544Seschrock return (err); 64789Sahrens 657046Sahrens return (dsl_dir_open_obj(dp, obj, name, dp, ddp)); 66789Sahrens } 67789Sahrens 68789Sahrens static dsl_pool_t * 69789Sahrens dsl_pool_open_impl(spa_t *spa, uint64_t txg) 70789Sahrens { 71789Sahrens dsl_pool_t *dp; 72789Sahrens blkptr_t *bp = spa_get_rootblkptr(spa); 73789Sahrens 74789Sahrens dp = kmem_zalloc(sizeof (dsl_pool_t), KM_SLEEP); 75789Sahrens dp->dp_spa = spa; 76789Sahrens dp->dp_meta_rootbp = *bp; 772856Snd150628 rw_init(&dp->dp_config_rwlock, NULL, RW_DEFAULT, NULL); 786245Smaybee dp->dp_write_limit = zfs_write_limit_min; 79789Sahrens txg_init(dp, txg); 80789Sahrens 81789Sahrens txg_list_create(&dp->dp_dirty_datasets, 82789Sahrens offsetof(dsl_dataset_t, ds_dirty_link)); 83789Sahrens txg_list_create(&dp->dp_dirty_dirs, 84789Sahrens offsetof(dsl_dir_t, dd_dirty_link)); 852199Sahrens txg_list_create(&dp->dp_sync_tasks, 862199Sahrens offsetof(dsl_sync_task_group_t, dstg_node)); 875367Sahrens list_create(&dp->dp_synced_datasets, sizeof (dsl_dataset_t), 88789Sahrens offsetof(dsl_dataset_t, ds_synced_link)); 89789Sahrens 906245Smaybee mutex_init(&dp->dp_lock, NULL, MUTEX_DEFAULT, NULL); 917046Sahrens mutex_init(&dp->dp_scrub_cancel_lock, NULL, MUTEX_DEFAULT, NULL); 926245Smaybee 939321SNeil.Perrin@Sun.COM dp->dp_vnrele_taskq = taskq_create("zfs_vn_rele_taskq", 1, minclsyspri, 949321SNeil.Perrin@Sun.COM 1, 4, 0); 959321SNeil.Perrin@Sun.COM 96789Sahrens return (dp); 97789Sahrens } 98789Sahrens 991544Seschrock int 1001544Seschrock dsl_pool_open(spa_t *spa, uint64_t txg, dsl_pool_t **dpp) 101789Sahrens { 102789Sahrens int err; 103789Sahrens dsl_pool_t *dp = dsl_pool_open_impl(spa, txg); 1047046Sahrens dsl_dir_t *dd; 1057046Sahrens dsl_dataset_t *ds; 106789Sahrens 1077046Sahrens rw_enter(&dp->dp_config_rwlock, RW_WRITER); 108*10298SMatthew.Ahrens@Sun.COM err = dmu_objset_open_impl(spa, NULL, &dp->dp_meta_rootbp, 109*10298SMatthew.Ahrens@Sun.COM &dp->dp_meta_objset); 1101544Seschrock if (err) 1111544Seschrock goto out; 1121544Seschrock 113789Sahrens err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 114789Sahrens DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, 115789Sahrens &dp->dp_root_dir_obj); 1161544Seschrock if (err) 1171544Seschrock goto out; 1181544Seschrock 1191544Seschrock err = dsl_dir_open_obj(dp, dp->dp_root_dir_obj, 1201544Seschrock NULL, dp, &dp->dp_root_dir); 1211544Seschrock if (err) 1221544Seschrock goto out; 123789Sahrens 1247046Sahrens err = dsl_pool_open_special_dir(dp, MOS_DIR_NAME, &dp->dp_mos_dir); 1251544Seschrock if (err) 1261544Seschrock goto out; 1271544Seschrock 1287046Sahrens if (spa_version(spa) >= SPA_VERSION_ORIGIN) { 1297046Sahrens err = dsl_pool_open_special_dir(dp, ORIGIN_DIR_NAME, &dd); 1307046Sahrens if (err) 1317046Sahrens goto out; 1327046Sahrens err = dsl_dataset_hold_obj(dp, dd->dd_phys->dd_head_dataset_obj, 1337046Sahrens FTAG, &ds); 1349008SLin.Ling@Sun.COM if (err == 0) { 1359008SLin.Ling@Sun.COM err = dsl_dataset_hold_obj(dp, 1369008SLin.Ling@Sun.COM ds->ds_phys->ds_prev_snap_obj, dp, 1379008SLin.Ling@Sun.COM &dp->dp_origin_snap); 1389008SLin.Ling@Sun.COM dsl_dataset_rele(ds, FTAG); 1399008SLin.Ling@Sun.COM } 1409008SLin.Ling@Sun.COM dsl_dir_close(dd, dp); 1417046Sahrens if (err) 1427046Sahrens goto out; 1437046Sahrens } 1447046Sahrens 1457046Sahrens /* get scrub status */ 1467046Sahrens err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 1477046Sahrens DMU_POOL_SCRUB_FUNC, sizeof (uint32_t), 1, 1487046Sahrens &dp->dp_scrub_func); 1497046Sahrens if (err == 0) { 1507046Sahrens err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 1517046Sahrens DMU_POOL_SCRUB_QUEUE, sizeof (uint64_t), 1, 1527046Sahrens &dp->dp_scrub_queue_obj); 1537046Sahrens if (err) 1547046Sahrens goto out; 1557046Sahrens err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 1567046Sahrens DMU_POOL_SCRUB_MIN_TXG, sizeof (uint64_t), 1, 1577046Sahrens &dp->dp_scrub_min_txg); 1587046Sahrens if (err) 1597046Sahrens goto out; 1607046Sahrens err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 1617046Sahrens DMU_POOL_SCRUB_MAX_TXG, sizeof (uint64_t), 1, 1627046Sahrens &dp->dp_scrub_max_txg); 1637046Sahrens if (err) 1647046Sahrens goto out; 1657046Sahrens err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 1667046Sahrens DMU_POOL_SCRUB_BOOKMARK, sizeof (uint64_t), 4, 1677046Sahrens &dp->dp_scrub_bookmark); 1687046Sahrens if (err) 1697046Sahrens goto out; 1707046Sahrens err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 1717046Sahrens DMU_POOL_SCRUB_ERRORS, sizeof (uint64_t), 1, 1727046Sahrens &spa->spa_scrub_errors); 1737046Sahrens if (err) 1747046Sahrens goto out; 1757046Sahrens if (spa_version(spa) < SPA_VERSION_DSL_SCRUB) { 1767046Sahrens /* 1777046Sahrens * A new-type scrub was in progress on an old 1787046Sahrens * pool. Restart from the beginning, since the 1797046Sahrens * old software may have changed the pool in the 1807046Sahrens * meantime. 1817046Sahrens */ 1827046Sahrens dsl_pool_scrub_restart(dp); 1837046Sahrens } 1847046Sahrens } else { 1857046Sahrens /* 1867046Sahrens * It's OK if there is no scrub in progress (and if 1877046Sahrens * there was an I/O error, ignore it). 1887046Sahrens */ 1897046Sahrens err = 0; 1907046Sahrens } 1917046Sahrens 1921544Seschrock out: 193789Sahrens rw_exit(&dp->dp_config_rwlock); 1941544Seschrock if (err) 1951544Seschrock dsl_pool_close(dp); 1961544Seschrock else 1971544Seschrock *dpp = dp; 198789Sahrens 1991544Seschrock return (err); 200789Sahrens } 201789Sahrens 202789Sahrens void 203789Sahrens dsl_pool_close(dsl_pool_t *dp) 204789Sahrens { 2057046Sahrens /* drop our references from dsl_pool_open() */ 2067046Sahrens 2077046Sahrens /* 2087046Sahrens * Since we held the origin_snap from "syncing" context (which 2097046Sahrens * includes pool-opening context), it actually only got a "ref" 2107046Sahrens * and not a hold, so just drop that here. 2117046Sahrens */ 2127046Sahrens if (dp->dp_origin_snap) 2137046Sahrens dsl_dataset_drop_ref(dp->dp_origin_snap, dp); 2141544Seschrock if (dp->dp_mos_dir) 2151544Seschrock dsl_dir_close(dp->dp_mos_dir, dp); 2161544Seschrock if (dp->dp_root_dir) 2171544Seschrock dsl_dir_close(dp->dp_root_dir, dp); 218789Sahrens 219789Sahrens /* undo the dmu_objset_open_impl(mos) from dsl_pool_open() */ 2201544Seschrock if (dp->dp_meta_objset) 221*10298SMatthew.Ahrens@Sun.COM dmu_objset_evict(dp->dp_meta_objset); 222789Sahrens 223789Sahrens txg_list_destroy(&dp->dp_dirty_datasets); 224789Sahrens txg_list_destroy(&dp->dp_dirty_dirs); 2255367Sahrens list_destroy(&dp->dp_synced_datasets); 226789Sahrens 2275642Smaybee arc_flush(dp->dp_spa); 228789Sahrens txg_fini(dp); 2292856Snd150628 rw_destroy(&dp->dp_config_rwlock); 2306245Smaybee mutex_destroy(&dp->dp_lock); 2317046Sahrens mutex_destroy(&dp->dp_scrub_cancel_lock); 2329321SNeil.Perrin@Sun.COM taskq_destroy(dp->dp_vnrele_taskq); 2337837SMatthew.Ahrens@Sun.COM if (dp->dp_blkstats) 2347837SMatthew.Ahrens@Sun.COM kmem_free(dp->dp_blkstats, sizeof (zfs_all_blkstats_t)); 235789Sahrens kmem_free(dp, sizeof (dsl_pool_t)); 236789Sahrens } 237789Sahrens 238789Sahrens dsl_pool_t * 2397184Stimh dsl_pool_create(spa_t *spa, nvlist_t *zplprops, uint64_t txg) 240789Sahrens { 241789Sahrens int err; 242789Sahrens dsl_pool_t *dp = dsl_pool_open_impl(spa, txg); 243789Sahrens dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg); 244*10298SMatthew.Ahrens@Sun.COM objset_t *os; 2457046Sahrens dsl_dataset_t *ds; 2467046Sahrens uint64_t dsobj; 2477046Sahrens 2487046Sahrens /* create and open the MOS (meta-objset) */ 249*10298SMatthew.Ahrens@Sun.COM dp->dp_meta_objset = dmu_objset_create_impl(spa, 250*10298SMatthew.Ahrens@Sun.COM NULL, &dp->dp_meta_rootbp, DMU_OST_META, tx); 251789Sahrens 252789Sahrens /* create the pool directory */ 253789Sahrens err = zap_create_claim(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 254789Sahrens DMU_OT_OBJECT_DIRECTORY, DMU_OT_NONE, 0, tx); 255789Sahrens ASSERT3U(err, ==, 0); 256789Sahrens 257789Sahrens /* create and open the root dir */ 2587046Sahrens dp->dp_root_dir_obj = dsl_dir_create_sync(dp, NULL, NULL, tx); 2591544Seschrock VERIFY(0 == dsl_dir_open_obj(dp, dp->dp_root_dir_obj, 2601544Seschrock NULL, dp, &dp->dp_root_dir)); 261789Sahrens 262789Sahrens /* create and open the meta-objset dir */ 2637046Sahrens (void) dsl_dir_create_sync(dp, dp->dp_root_dir, MOS_DIR_NAME, tx); 2647046Sahrens VERIFY(0 == dsl_pool_open_special_dir(dp, 2657046Sahrens MOS_DIR_NAME, &dp->dp_mos_dir)); 2667046Sahrens 2677046Sahrens if (spa_version(spa) >= SPA_VERSION_DSL_SCRUB) 2687046Sahrens dsl_pool_create_origin(dp, tx); 2697046Sahrens 2707046Sahrens /* create the root dataset */ 2717046Sahrens dsobj = dsl_dataset_create_sync_dd(dp->dp_root_dir, NULL, 0, tx); 2727046Sahrens 2737046Sahrens /* create the root objset */ 2747046Sahrens VERIFY(0 == dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds)); 275*10298SMatthew.Ahrens@Sun.COM os = dmu_objset_create_impl(dp->dp_spa, ds, 2767046Sahrens dsl_dataset_get_blkptr(ds), DMU_OST_ZFS, tx); 2777046Sahrens #ifdef _KERNEL 278*10298SMatthew.Ahrens@Sun.COM zfs_create_fs(os, kcred, zplprops, tx); 2797046Sahrens #endif 2807046Sahrens dsl_dataset_rele(ds, FTAG); 281789Sahrens 282789Sahrens dmu_tx_commit(tx); 283789Sahrens 284789Sahrens return (dp); 285789Sahrens } 286789Sahrens 287789Sahrens void 288789Sahrens dsl_pool_sync(dsl_pool_t *dp, uint64_t txg) 289789Sahrens { 2903547Smaybee zio_t *zio; 291789Sahrens dmu_tx_t *tx; 2923547Smaybee dsl_dir_t *dd; 2933547Smaybee dsl_dataset_t *ds; 2943547Smaybee dsl_sync_task_group_t *dstg; 295*10298SMatthew.Ahrens@Sun.COM objset_t *mos = dp->dp_meta_objset; 2967468SMark.Maybee@Sun.COM hrtime_t start, write_time; 2977468SMark.Maybee@Sun.COM uint64_t data_written; 2983547Smaybee int err; 299789Sahrens 300789Sahrens tx = dmu_tx_create_assigned(dp, txg); 301789Sahrens 3027468SMark.Maybee@Sun.COM dp->dp_read_overhead = 0; 3039366SMark.Maybee@Sun.COM start = gethrtime(); 3049396SMatthew.Ahrens@Sun.COM 3053547Smaybee zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 3063547Smaybee while (ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) { 3079396SMatthew.Ahrens@Sun.COM /* 3089396SMatthew.Ahrens@Sun.COM * We must not sync any non-MOS datasets twice, because 3099396SMatthew.Ahrens@Sun.COM * we may have taken a snapshot of them. However, we 3109396SMatthew.Ahrens@Sun.COM * may sync newly-created datasets on pass 2. 3119396SMatthew.Ahrens@Sun.COM */ 3129396SMatthew.Ahrens@Sun.COM ASSERT(!list_link_active(&ds->ds_synced_link)); 3139396SMatthew.Ahrens@Sun.COM list_insert_tail(&dp->dp_synced_datasets, ds); 3143547Smaybee dsl_dataset_sync(ds, zio, tx); 3153547Smaybee } 3167468SMark.Maybee@Sun.COM DTRACE_PROBE(pool_sync__1setup); 3179396SMatthew.Ahrens@Sun.COM err = zio_wait(zio); 3187468SMark.Maybee@Sun.COM 3197468SMark.Maybee@Sun.COM write_time = gethrtime() - start; 3203547Smaybee ASSERT(err == 0); 3217468SMark.Maybee@Sun.COM DTRACE_PROBE(pool_sync__2rootzio); 322789Sahrens 3239396SMatthew.Ahrens@Sun.COM for (ds = list_head(&dp->dp_synced_datasets); ds; 3249396SMatthew.Ahrens@Sun.COM ds = list_next(&dp->dp_synced_datasets, ds)) 325*10298SMatthew.Ahrens@Sun.COM dmu_objset_do_userquota_callbacks(ds->ds_objset, tx); 3269396SMatthew.Ahrens@Sun.COM 3279396SMatthew.Ahrens@Sun.COM /* 3289396SMatthew.Ahrens@Sun.COM * Sync the datasets again to push out the changes due to 3299396SMatthew.Ahrens@Sun.COM * userquota updates. This must be done before we process the 3309396SMatthew.Ahrens@Sun.COM * sync tasks, because that could cause a snapshot of a dataset 3319396SMatthew.Ahrens@Sun.COM * whose ds_bp will be rewritten when we do this 2nd sync. 3329396SMatthew.Ahrens@Sun.COM */ 3339396SMatthew.Ahrens@Sun.COM zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 3349396SMatthew.Ahrens@Sun.COM while (ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) { 3359396SMatthew.Ahrens@Sun.COM ASSERT(list_link_active(&ds->ds_synced_link)); 3369396SMatthew.Ahrens@Sun.COM dmu_buf_rele(ds->ds_dbuf, ds); 3379396SMatthew.Ahrens@Sun.COM dsl_dataset_sync(ds, zio, tx); 3389396SMatthew.Ahrens@Sun.COM } 3399396SMatthew.Ahrens@Sun.COM err = zio_wait(zio); 3409396SMatthew.Ahrens@Sun.COM 3419396SMatthew.Ahrens@Sun.COM while (dstg = txg_list_remove(&dp->dp_sync_tasks, txg)) { 3429396SMatthew.Ahrens@Sun.COM /* 3439396SMatthew.Ahrens@Sun.COM * No more sync tasks should have been added while we 3449396SMatthew.Ahrens@Sun.COM * were syncing. 3459396SMatthew.Ahrens@Sun.COM */ 3469396SMatthew.Ahrens@Sun.COM ASSERT(spa_sync_pass(dp->dp_spa) == 1); 3473547Smaybee dsl_sync_task_group_sync(dstg, tx); 3489396SMatthew.Ahrens@Sun.COM } 3497468SMark.Maybee@Sun.COM DTRACE_PROBE(pool_sync__3task); 3507468SMark.Maybee@Sun.COM 3517468SMark.Maybee@Sun.COM start = gethrtime(); 3523547Smaybee while (dd = txg_list_remove(&dp->dp_dirty_dirs, txg)) 3533547Smaybee dsl_dir_sync(dd, tx); 3547468SMark.Maybee@Sun.COM write_time += gethrtime() - start; 355789Sahrens 3567046Sahrens if (spa_sync_pass(dp->dp_spa) == 1) 3577046Sahrens dsl_pool_scrub_sync(dp, tx); 3587046Sahrens 3597468SMark.Maybee@Sun.COM start = gethrtime(); 360*10298SMatthew.Ahrens@Sun.COM if (list_head(&mos->os_dirty_dnodes[txg & TXG_MASK]) != NULL || 361*10298SMatthew.Ahrens@Sun.COM list_head(&mos->os_free_dnodes[txg & TXG_MASK]) != NULL) { 3623547Smaybee zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 363*10298SMatthew.Ahrens@Sun.COM dmu_objset_sync(mos, zio, tx); 3643547Smaybee err = zio_wait(zio); 3653547Smaybee ASSERT(err == 0); 366789Sahrens dprintf_bp(&dp->dp_meta_rootbp, "meta objset rootbp is %s", ""); 367789Sahrens spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp); 368789Sahrens } 3697468SMark.Maybee@Sun.COM write_time += gethrtime() - start; 3707468SMark.Maybee@Sun.COM DTRACE_PROBE2(pool_sync__4io, hrtime_t, write_time, 3717468SMark.Maybee@Sun.COM hrtime_t, dp->dp_read_overhead); 3727468SMark.Maybee@Sun.COM write_time -= dp->dp_read_overhead; 373789Sahrens 374789Sahrens dmu_tx_commit(tx); 3757468SMark.Maybee@Sun.COM 3767468SMark.Maybee@Sun.COM data_written = dp->dp_space_towrite[txg & TXG_MASK]; 3777468SMark.Maybee@Sun.COM dp->dp_space_towrite[txg & TXG_MASK] = 0; 3787468SMark.Maybee@Sun.COM ASSERT(dp->dp_tempreserved[txg & TXG_MASK] == 0); 3797468SMark.Maybee@Sun.COM 3807468SMark.Maybee@Sun.COM /* 3817468SMark.Maybee@Sun.COM * If the write limit max has not been explicitly set, set it 3827468SMark.Maybee@Sun.COM * to a fraction of available physical memory (default 1/8th). 3837468SMark.Maybee@Sun.COM * Note that we must inflate the limit because the spa 3847468SMark.Maybee@Sun.COM * inflates write sizes to account for data replication. 3857468SMark.Maybee@Sun.COM * Check this each sync phase to catch changing memory size. 3867468SMark.Maybee@Sun.COM */ 3877468SMark.Maybee@Sun.COM if (physmem != old_physmem && zfs_write_limit_shift) { 3887468SMark.Maybee@Sun.COM mutex_enter(&zfs_write_limit_lock); 3897468SMark.Maybee@Sun.COM old_physmem = physmem; 3907468SMark.Maybee@Sun.COM zfs_write_limit_max = ptob(physmem) >> zfs_write_limit_shift; 3917468SMark.Maybee@Sun.COM zfs_write_limit_inflated = MAX(zfs_write_limit_min, 3927468SMark.Maybee@Sun.COM spa_get_asize(dp->dp_spa, zfs_write_limit_max)); 3937468SMark.Maybee@Sun.COM mutex_exit(&zfs_write_limit_lock); 3947468SMark.Maybee@Sun.COM } 3957468SMark.Maybee@Sun.COM 3967468SMark.Maybee@Sun.COM /* 3977468SMark.Maybee@Sun.COM * Attempt to keep the sync time consistent by adjusting the 3987468SMark.Maybee@Sun.COM * amount of write traffic allowed into each transaction group. 3997468SMark.Maybee@Sun.COM * Weight the throughput calculation towards the current value: 4007468SMark.Maybee@Sun.COM * thru = 3/4 old_thru + 1/4 new_thru 4017468SMark.Maybee@Sun.COM */ 4027468SMark.Maybee@Sun.COM ASSERT(zfs_write_limit_min > 0); 4037468SMark.Maybee@Sun.COM if (data_written > zfs_write_limit_min / 8 && write_time > 0) { 4047468SMark.Maybee@Sun.COM uint64_t throughput = (data_written * NANOSEC) / write_time; 4057468SMark.Maybee@Sun.COM if (dp->dp_throughput) 4067468SMark.Maybee@Sun.COM dp->dp_throughput = throughput / 4 + 4077468SMark.Maybee@Sun.COM 3 * dp->dp_throughput / 4; 4087468SMark.Maybee@Sun.COM else 4097468SMark.Maybee@Sun.COM dp->dp_throughput = throughput; 4107468SMark.Maybee@Sun.COM dp->dp_write_limit = MIN(zfs_write_limit_inflated, 4117468SMark.Maybee@Sun.COM MAX(zfs_write_limit_min, 4127468SMark.Maybee@Sun.COM dp->dp_throughput * zfs_txg_synctime)); 4137468SMark.Maybee@Sun.COM } 414789Sahrens } 415789Sahrens 416789Sahrens void 417789Sahrens dsl_pool_zil_clean(dsl_pool_t *dp) 418789Sahrens { 419789Sahrens dsl_dataset_t *ds; 420789Sahrens 4215367Sahrens while (ds = list_head(&dp->dp_synced_datasets)) { 4225367Sahrens list_remove(&dp->dp_synced_datasets, ds); 423*10298SMatthew.Ahrens@Sun.COM ASSERT(ds->ds_objset != NULL); 424*10298SMatthew.Ahrens@Sun.COM zil_clean(ds->ds_objset->os_zil); 4253897Smaybee dmu_buf_rele(ds->ds_dbuf, ds); 426789Sahrens } 427789Sahrens } 428789Sahrens 4293547Smaybee /* 4303547Smaybee * TRUE if the current thread is the tx_sync_thread or if we 4313547Smaybee * are being called from SPA context during pool initialization. 4323547Smaybee */ 433789Sahrens int 434789Sahrens dsl_pool_sync_context(dsl_pool_t *dp) 435789Sahrens { 436789Sahrens return (curthread == dp->dp_tx.tx_sync_thread || 4373547Smaybee spa_get_dsl(dp->dp_spa) == NULL); 438789Sahrens } 439789Sahrens 440789Sahrens uint64_t 441789Sahrens dsl_pool_adjustedsize(dsl_pool_t *dp, boolean_t netfree) 442789Sahrens { 443789Sahrens uint64_t space, resv; 444789Sahrens 445789Sahrens /* 4461775Sbillm * Reserve about 1.6% (1/64), or at least 32MB, for allocation 447789Sahrens * efficiency. 448789Sahrens * XXX The intent log is not accounted for, so it must fit 449789Sahrens * within this slop. 450789Sahrens * 451789Sahrens * If we're trying to assess whether it's OK to do a free, 452789Sahrens * cut the reservation in half to allow forward progress 453789Sahrens * (e.g. make it possible to rm(1) files from a full pool). 454789Sahrens */ 4552082Seschrock space = spa_get_dspace(dp->dp_spa); 4561775Sbillm resv = MAX(space >> 6, SPA_MINDEVSIZE >> 1); 457789Sahrens if (netfree) 458789Sahrens resv >>= 1; 459789Sahrens 460789Sahrens return (space - resv); 461789Sahrens } 4626245Smaybee 4636245Smaybee int 4646245Smaybee dsl_pool_tempreserve_space(dsl_pool_t *dp, uint64_t space, dmu_tx_t *tx) 4656245Smaybee { 4666245Smaybee uint64_t reserved = 0; 4676245Smaybee uint64_t write_limit = (zfs_write_limit_override ? 4686245Smaybee zfs_write_limit_override : dp->dp_write_limit); 4696245Smaybee 4706245Smaybee if (zfs_no_write_throttle) { 4716643Seschrock atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], 4726643Seschrock space); 4736245Smaybee return (0); 4746245Smaybee } 4756245Smaybee 4766245Smaybee /* 4776245Smaybee * Check to see if we have exceeded the maximum allowed IO for 4786245Smaybee * this transaction group. We can do this without locks since 4796245Smaybee * a little slop here is ok. Note that we do the reserved check 4806245Smaybee * with only half the requested reserve: this is because the 4816245Smaybee * reserve requests are worst-case, and we really don't want to 4826245Smaybee * throttle based off of worst-case estimates. 4836245Smaybee */ 4846245Smaybee if (write_limit > 0) { 4856245Smaybee reserved = dp->dp_space_towrite[tx->tx_txg & TXG_MASK] 4866245Smaybee + dp->dp_tempreserved[tx->tx_txg & TXG_MASK] / 2; 4876245Smaybee 4886245Smaybee if (reserved && reserved > write_limit) 4896245Smaybee return (ERESTART); 4906245Smaybee } 4916245Smaybee 4926245Smaybee atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], space); 4936245Smaybee 4946245Smaybee /* 4956245Smaybee * If this transaction group is over 7/8ths capacity, delay 4966245Smaybee * the caller 1 clock tick. This will slow down the "fill" 4976245Smaybee * rate until the sync process can catch up with us. 4986245Smaybee */ 4996740Sgw25295 if (reserved && reserved > (write_limit - (write_limit >> 3))) 5006245Smaybee txg_delay(dp, tx->tx_txg, 1); 5016245Smaybee 5026245Smaybee return (0); 5036245Smaybee } 5046245Smaybee 5056245Smaybee void 5066245Smaybee dsl_pool_tempreserve_clear(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx) 5076245Smaybee { 5086245Smaybee ASSERT(dp->dp_tempreserved[tx->tx_txg & TXG_MASK] >= space); 5096245Smaybee atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], -space); 5106245Smaybee } 5116245Smaybee 5126245Smaybee void 5136245Smaybee dsl_pool_memory_pressure(dsl_pool_t *dp) 5146245Smaybee { 5156245Smaybee uint64_t space_inuse = 0; 5166245Smaybee int i; 5176245Smaybee 5186245Smaybee if (dp->dp_write_limit == zfs_write_limit_min) 5196245Smaybee return; 5206245Smaybee 5216245Smaybee for (i = 0; i < TXG_SIZE; i++) { 5226245Smaybee space_inuse += dp->dp_space_towrite[i]; 5236245Smaybee space_inuse += dp->dp_tempreserved[i]; 5246245Smaybee } 5256245Smaybee dp->dp_write_limit = MAX(zfs_write_limit_min, 5266245Smaybee MIN(dp->dp_write_limit, space_inuse / 4)); 5276245Smaybee } 5286245Smaybee 5296245Smaybee void 5306245Smaybee dsl_pool_willuse_space(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx) 5316245Smaybee { 5326245Smaybee if (space > 0) { 5336245Smaybee mutex_enter(&dp->dp_lock); 5346245Smaybee dp->dp_space_towrite[tx->tx_txg & TXG_MASK] += space; 5356245Smaybee mutex_exit(&dp->dp_lock); 5366245Smaybee } 5376245Smaybee } 5387046Sahrens 5397046Sahrens /* ARGSUSED */ 5407046Sahrens static int 5417046Sahrens upgrade_clones_cb(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg) 5427046Sahrens { 5437046Sahrens dmu_tx_t *tx = arg; 5447046Sahrens dsl_dataset_t *ds, *prev = NULL; 5457046Sahrens int err; 5467046Sahrens dsl_pool_t *dp = spa_get_dsl(spa); 5477046Sahrens 5487046Sahrens err = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds); 5497046Sahrens if (err) 5507046Sahrens return (err); 5517046Sahrens 5527046Sahrens while (ds->ds_phys->ds_prev_snap_obj != 0) { 5537046Sahrens err = dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj, 5547046Sahrens FTAG, &prev); 5557046Sahrens if (err) { 5567046Sahrens dsl_dataset_rele(ds, FTAG); 5577046Sahrens return (err); 5587046Sahrens } 5597046Sahrens 5607046Sahrens if (prev->ds_phys->ds_next_snap_obj != ds->ds_object) 5617046Sahrens break; 5627046Sahrens dsl_dataset_rele(ds, FTAG); 5637046Sahrens ds = prev; 5647046Sahrens prev = NULL; 5657046Sahrens } 5667046Sahrens 5677046Sahrens if (prev == NULL) { 5687046Sahrens prev = dp->dp_origin_snap; 5697046Sahrens 5707046Sahrens /* 5717046Sahrens * The $ORIGIN can't have any data, or the accounting 5727046Sahrens * will be wrong. 5737046Sahrens */ 5747046Sahrens ASSERT(prev->ds_phys->ds_bp.blk_birth == 0); 5757046Sahrens 5767046Sahrens /* The origin doesn't get attached to itself */ 5777046Sahrens if (ds->ds_object == prev->ds_object) { 5787046Sahrens dsl_dataset_rele(ds, FTAG); 5797046Sahrens return (0); 5807046Sahrens } 5817046Sahrens 5827046Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 5837046Sahrens ds->ds_phys->ds_prev_snap_obj = prev->ds_object; 5847046Sahrens ds->ds_phys->ds_prev_snap_txg = prev->ds_phys->ds_creation_txg; 5857046Sahrens 5867046Sahrens dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx); 5877046Sahrens ds->ds_dir->dd_phys->dd_origin_obj = prev->ds_object; 5887046Sahrens 5897046Sahrens dmu_buf_will_dirty(prev->ds_dbuf, tx); 5907046Sahrens prev->ds_phys->ds_num_children++; 5917046Sahrens 5927046Sahrens if (ds->ds_phys->ds_next_snap_obj == 0) { 5937046Sahrens ASSERT(ds->ds_prev == NULL); 5947046Sahrens VERIFY(0 == dsl_dataset_hold_obj(dp, 5957046Sahrens ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev)); 5967046Sahrens } 5977046Sahrens } 5987046Sahrens 5997046Sahrens ASSERT(ds->ds_dir->dd_phys->dd_origin_obj == prev->ds_object); 6007046Sahrens ASSERT(ds->ds_phys->ds_prev_snap_obj == prev->ds_object); 6017046Sahrens 6027046Sahrens if (prev->ds_phys->ds_next_clones_obj == 0) { 6037046Sahrens prev->ds_phys->ds_next_clones_obj = 6047046Sahrens zap_create(dp->dp_meta_objset, 6057046Sahrens DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx); 6067046Sahrens } 6077046Sahrens VERIFY(0 == zap_add_int(dp->dp_meta_objset, 6087046Sahrens prev->ds_phys->ds_next_clones_obj, ds->ds_object, tx)); 6097046Sahrens 6107046Sahrens dsl_dataset_rele(ds, FTAG); 6117046Sahrens if (prev != dp->dp_origin_snap) 6127046Sahrens dsl_dataset_rele(prev, FTAG); 6137046Sahrens return (0); 6147046Sahrens } 6157046Sahrens 6167046Sahrens void 6177046Sahrens dsl_pool_upgrade_clones(dsl_pool_t *dp, dmu_tx_t *tx) 6187046Sahrens { 6197046Sahrens ASSERT(dmu_tx_is_syncing(tx)); 6207046Sahrens ASSERT(dp->dp_origin_snap != NULL); 6217046Sahrens 6227046Sahrens (void) dmu_objset_find_spa(dp->dp_spa, NULL, upgrade_clones_cb, 6237046Sahrens tx, DS_FIND_CHILDREN); 6247046Sahrens } 6257046Sahrens 6267046Sahrens void 6277046Sahrens dsl_pool_create_origin(dsl_pool_t *dp, dmu_tx_t *tx) 6287046Sahrens { 6297046Sahrens uint64_t dsobj; 6307046Sahrens dsl_dataset_t *ds; 6317046Sahrens 6327046Sahrens ASSERT(dmu_tx_is_syncing(tx)); 6337046Sahrens ASSERT(dp->dp_origin_snap == NULL); 6347046Sahrens 6357046Sahrens /* create the origin dir, ds, & snap-ds */ 6367046Sahrens rw_enter(&dp->dp_config_rwlock, RW_WRITER); 6377046Sahrens dsobj = dsl_dataset_create_sync(dp->dp_root_dir, ORIGIN_DIR_NAME, 6387046Sahrens NULL, 0, kcred, tx); 6397046Sahrens VERIFY(0 == dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds)); 6407046Sahrens dsl_dataset_snapshot_sync(ds, ORIGIN_DIR_NAME, kcred, tx); 6417046Sahrens VERIFY(0 == dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj, 6427046Sahrens dp, &dp->dp_origin_snap)); 6437046Sahrens dsl_dataset_rele(ds, FTAG); 6447046Sahrens rw_exit(&dp->dp_config_rwlock); 6457046Sahrens } 6469321SNeil.Perrin@Sun.COM 6479321SNeil.Perrin@Sun.COM taskq_t * 6489321SNeil.Perrin@Sun.COM dsl_pool_vnrele_taskq(dsl_pool_t *dp) 6499321SNeil.Perrin@Sun.COM { 6509321SNeil.Perrin@Sun.COM return (dp->dp_vnrele_taskq); 6519321SNeil.Perrin@Sun.COM } 652