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 /* 22*12296SLin.Ling@Sun.COM * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23789Sahrens */ 24789Sahrens 25789Sahrens #include <sys/dsl_pool.h> 26789Sahrens #include <sys/dsl_dataset.h> 27*12296SLin.Ling@Sun.COM #include <sys/dsl_prop.h> 28789Sahrens #include <sys/dsl_dir.h> 292199Sahrens #include <sys/dsl_synctask.h> 30*12296SLin.Ling@Sun.COM #include <sys/dsl_scan.h> 31*12296SLin.Ling@Sun.COM #include <sys/dnode.h> 32789Sahrens #include <sys/dmu_tx.h> 33789Sahrens #include <sys/dmu_objset.h> 34789Sahrens #include <sys/arc.h> 35789Sahrens #include <sys/zap.h> 363547Smaybee #include <sys/zio.h> 37789Sahrens #include <sys/zfs_context.h> 38789Sahrens #include <sys/fs/zfs.h> 397046Sahrens #include <sys/zfs_znode.h> 407046Sahrens #include <sys/spa_impl.h> 41789Sahrens 426245Smaybee int zfs_no_write_throttle = 0; 437468SMark.Maybee@Sun.COM int zfs_write_limit_shift = 3; /* 1/8th of physical memory */ 4411182SLin.Ling@Sun.COM int zfs_txg_synctime_ms = 5000; /* target millisecs to sync a txg */ 457468SMark.Maybee@Sun.COM 467468SMark.Maybee@Sun.COM uint64_t zfs_write_limit_min = 32 << 20; /* min write limit is 32MB */ 477468SMark.Maybee@Sun.COM uint64_t zfs_write_limit_max = 0; /* max data payload per txg */ 487468SMark.Maybee@Sun.COM uint64_t zfs_write_limit_inflated = 0; 496245Smaybee uint64_t zfs_write_limit_override = 0; 506245Smaybee 517468SMark.Maybee@Sun.COM kmutex_t zfs_write_limit_lock; 527468SMark.Maybee@Sun.COM 537468SMark.Maybee@Sun.COM static pgcnt_t old_physmem = 0; 547046Sahrens 55*12296SLin.Ling@Sun.COM int 567046Sahrens dsl_pool_open_special_dir(dsl_pool_t *dp, const char *name, dsl_dir_t **ddp) 57789Sahrens { 58789Sahrens uint64_t obj; 59789Sahrens int err; 60789Sahrens 61789Sahrens err = zap_lookup(dp->dp_meta_objset, 62789Sahrens dp->dp_root_dir->dd_phys->dd_child_dir_zapobj, 637046Sahrens name, sizeof (obj), 1, &obj); 641544Seschrock if (err) 651544Seschrock return (err); 66789Sahrens 677046Sahrens return (dsl_dir_open_obj(dp, obj, name, dp, ddp)); 68789Sahrens } 69789Sahrens 70789Sahrens static dsl_pool_t * 71789Sahrens dsl_pool_open_impl(spa_t *spa, uint64_t txg) 72789Sahrens { 73789Sahrens dsl_pool_t *dp; 74789Sahrens blkptr_t *bp = spa_get_rootblkptr(spa); 75789Sahrens 76789Sahrens dp = kmem_zalloc(sizeof (dsl_pool_t), KM_SLEEP); 77789Sahrens dp->dp_spa = spa; 78789Sahrens dp->dp_meta_rootbp = *bp; 792856Snd150628 rw_init(&dp->dp_config_rwlock, NULL, RW_DEFAULT, NULL); 806245Smaybee dp->dp_write_limit = zfs_write_limit_min; 81789Sahrens txg_init(dp, txg); 82789Sahrens 83789Sahrens txg_list_create(&dp->dp_dirty_datasets, 84789Sahrens offsetof(dsl_dataset_t, ds_dirty_link)); 85789Sahrens txg_list_create(&dp->dp_dirty_dirs, 86789Sahrens offsetof(dsl_dir_t, dd_dirty_link)); 872199Sahrens txg_list_create(&dp->dp_sync_tasks, 882199Sahrens offsetof(dsl_sync_task_group_t, dstg_node)); 895367Sahrens list_create(&dp->dp_synced_datasets, sizeof (dsl_dataset_t), 90789Sahrens offsetof(dsl_dataset_t, ds_synced_link)); 91789Sahrens 926245Smaybee mutex_init(&dp->dp_lock, NULL, MUTEX_DEFAULT, NULL); 936245Smaybee 949321SNeil.Perrin@Sun.COM dp->dp_vnrele_taskq = taskq_create("zfs_vn_rele_taskq", 1, minclsyspri, 959321SNeil.Perrin@Sun.COM 1, 4, 0); 969321SNeil.Perrin@Sun.COM 97789Sahrens return (dp); 98789Sahrens } 99789Sahrens 1001544Seschrock int 1011544Seschrock dsl_pool_open(spa_t *spa, uint64_t txg, dsl_pool_t **dpp) 102789Sahrens { 103789Sahrens int err; 104789Sahrens dsl_pool_t *dp = dsl_pool_open_impl(spa, txg); 1057046Sahrens dsl_dir_t *dd; 1067046Sahrens dsl_dataset_t *ds; 107789Sahrens 1087046Sahrens rw_enter(&dp->dp_config_rwlock, RW_WRITER); 10910298SMatthew.Ahrens@Sun.COM err = dmu_objset_open_impl(spa, NULL, &dp->dp_meta_rootbp, 11010298SMatthew.Ahrens@Sun.COM &dp->dp_meta_objset); 1111544Seschrock if (err) 1121544Seschrock goto out; 1131544Seschrock 114789Sahrens err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 115789Sahrens DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, 116789Sahrens &dp->dp_root_dir_obj); 1171544Seschrock if (err) 1181544Seschrock goto out; 1191544Seschrock 1201544Seschrock err = dsl_dir_open_obj(dp, dp->dp_root_dir_obj, 1211544Seschrock NULL, dp, &dp->dp_root_dir); 1221544Seschrock if (err) 1231544Seschrock goto out; 124789Sahrens 1257046Sahrens err = dsl_pool_open_special_dir(dp, MOS_DIR_NAME, &dp->dp_mos_dir); 1261544Seschrock if (err) 1271544Seschrock goto out; 1281544Seschrock 1297046Sahrens if (spa_version(spa) >= SPA_VERSION_ORIGIN) { 1307046Sahrens err = dsl_pool_open_special_dir(dp, ORIGIN_DIR_NAME, &dd); 1317046Sahrens if (err) 1327046Sahrens goto out; 1337046Sahrens err = dsl_dataset_hold_obj(dp, dd->dd_phys->dd_head_dataset_obj, 1347046Sahrens FTAG, &ds); 1359008SLin.Ling@Sun.COM if (err == 0) { 1369008SLin.Ling@Sun.COM err = dsl_dataset_hold_obj(dp, 1379008SLin.Ling@Sun.COM ds->ds_phys->ds_prev_snap_obj, dp, 1389008SLin.Ling@Sun.COM &dp->dp_origin_snap); 1399008SLin.Ling@Sun.COM dsl_dataset_rele(ds, FTAG); 1409008SLin.Ling@Sun.COM } 1419008SLin.Ling@Sun.COM dsl_dir_close(dd, dp); 1427046Sahrens if (err) 1437046Sahrens goto out; 1447046Sahrens } 1457046Sahrens 14610342Schris.kirby@sun.com err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 14710342Schris.kirby@sun.com DMU_POOL_TMP_USERREFS, sizeof (uint64_t), 1, 14810342Schris.kirby@sun.com &dp->dp_tmp_userrefs_obj); 14910342Schris.kirby@sun.com if (err == ENOENT) 15010342Schris.kirby@sun.com err = 0; 15110342Schris.kirby@sun.com if (err) 15210342Schris.kirby@sun.com goto out; 15310342Schris.kirby@sun.com 154*12296SLin.Ling@Sun.COM err = dsl_scan_init(dp, txg); 1557046Sahrens 1561544Seschrock out: 157789Sahrens rw_exit(&dp->dp_config_rwlock); 1581544Seschrock if (err) 1591544Seschrock dsl_pool_close(dp); 1601544Seschrock else 1611544Seschrock *dpp = dp; 162789Sahrens 1631544Seschrock return (err); 164789Sahrens } 165789Sahrens 166789Sahrens void 167789Sahrens dsl_pool_close(dsl_pool_t *dp) 168789Sahrens { 1697046Sahrens /* drop our references from dsl_pool_open() */ 1707046Sahrens 1717046Sahrens /* 1727046Sahrens * Since we held the origin_snap from "syncing" context (which 1737046Sahrens * includes pool-opening context), it actually only got a "ref" 1747046Sahrens * and not a hold, so just drop that here. 1757046Sahrens */ 1767046Sahrens if (dp->dp_origin_snap) 1777046Sahrens dsl_dataset_drop_ref(dp->dp_origin_snap, dp); 1781544Seschrock if (dp->dp_mos_dir) 1791544Seschrock dsl_dir_close(dp->dp_mos_dir, dp); 1801544Seschrock if (dp->dp_root_dir) 1811544Seschrock dsl_dir_close(dp->dp_root_dir, dp); 182789Sahrens 183789Sahrens /* undo the dmu_objset_open_impl(mos) from dsl_pool_open() */ 1841544Seschrock if (dp->dp_meta_objset) 18510298SMatthew.Ahrens@Sun.COM dmu_objset_evict(dp->dp_meta_objset); 186789Sahrens 187789Sahrens txg_list_destroy(&dp->dp_dirty_datasets); 18811814SChris.Kirby@sun.com txg_list_destroy(&dp->dp_sync_tasks); 189789Sahrens txg_list_destroy(&dp->dp_dirty_dirs); 1905367Sahrens list_destroy(&dp->dp_synced_datasets); 191789Sahrens 1925642Smaybee arc_flush(dp->dp_spa); 193789Sahrens txg_fini(dp); 194*12296SLin.Ling@Sun.COM dsl_scan_fini(dp); 1952856Snd150628 rw_destroy(&dp->dp_config_rwlock); 1966245Smaybee mutex_destroy(&dp->dp_lock); 1979321SNeil.Perrin@Sun.COM taskq_destroy(dp->dp_vnrele_taskq); 1987837SMatthew.Ahrens@Sun.COM if (dp->dp_blkstats) 1997837SMatthew.Ahrens@Sun.COM kmem_free(dp->dp_blkstats, sizeof (zfs_all_blkstats_t)); 200789Sahrens kmem_free(dp, sizeof (dsl_pool_t)); 201789Sahrens } 202789Sahrens 203789Sahrens dsl_pool_t * 2047184Stimh dsl_pool_create(spa_t *spa, nvlist_t *zplprops, uint64_t txg) 205789Sahrens { 206789Sahrens int err; 207789Sahrens dsl_pool_t *dp = dsl_pool_open_impl(spa, txg); 208789Sahrens dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg); 20910298SMatthew.Ahrens@Sun.COM objset_t *os; 2107046Sahrens dsl_dataset_t *ds; 2117046Sahrens uint64_t dsobj; 2127046Sahrens 2137046Sahrens /* create and open the MOS (meta-objset) */ 21410298SMatthew.Ahrens@Sun.COM dp->dp_meta_objset = dmu_objset_create_impl(spa, 21510298SMatthew.Ahrens@Sun.COM NULL, &dp->dp_meta_rootbp, DMU_OST_META, tx); 216789Sahrens 217789Sahrens /* create the pool directory */ 218789Sahrens err = zap_create_claim(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 219789Sahrens DMU_OT_OBJECT_DIRECTORY, DMU_OT_NONE, 0, tx); 220789Sahrens ASSERT3U(err, ==, 0); 221789Sahrens 222*12296SLin.Ling@Sun.COM /* Initialize scan structures */ 223*12296SLin.Ling@Sun.COM VERIFY3U(0, ==, dsl_scan_init(dp, txg)); 224*12296SLin.Ling@Sun.COM 225789Sahrens /* create and open the root dir */ 2267046Sahrens dp->dp_root_dir_obj = dsl_dir_create_sync(dp, NULL, NULL, tx); 2271544Seschrock VERIFY(0 == dsl_dir_open_obj(dp, dp->dp_root_dir_obj, 2281544Seschrock NULL, dp, &dp->dp_root_dir)); 229789Sahrens 230789Sahrens /* create and open the meta-objset dir */ 2317046Sahrens (void) dsl_dir_create_sync(dp, dp->dp_root_dir, MOS_DIR_NAME, tx); 2327046Sahrens VERIFY(0 == dsl_pool_open_special_dir(dp, 2337046Sahrens MOS_DIR_NAME, &dp->dp_mos_dir)); 2347046Sahrens 2357046Sahrens if (spa_version(spa) >= SPA_VERSION_DSL_SCRUB) 2367046Sahrens dsl_pool_create_origin(dp, tx); 2377046Sahrens 2387046Sahrens /* create the root dataset */ 2397046Sahrens dsobj = dsl_dataset_create_sync_dd(dp->dp_root_dir, NULL, 0, tx); 2407046Sahrens 2417046Sahrens /* create the root objset */ 2427046Sahrens VERIFY(0 == dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds)); 24310298SMatthew.Ahrens@Sun.COM os = dmu_objset_create_impl(dp->dp_spa, ds, 2447046Sahrens dsl_dataset_get_blkptr(ds), DMU_OST_ZFS, tx); 2457046Sahrens #ifdef _KERNEL 24610298SMatthew.Ahrens@Sun.COM zfs_create_fs(os, kcred, zplprops, tx); 2477046Sahrens #endif 2487046Sahrens dsl_dataset_rele(ds, FTAG); 249789Sahrens 250789Sahrens dmu_tx_commit(tx); 251789Sahrens 252789Sahrens return (dp); 253789Sahrens } 254789Sahrens 255789Sahrens void 256789Sahrens dsl_pool_sync(dsl_pool_t *dp, uint64_t txg) 257789Sahrens { 2583547Smaybee zio_t *zio; 259789Sahrens dmu_tx_t *tx; 2603547Smaybee dsl_dir_t *dd; 2613547Smaybee dsl_dataset_t *ds; 2623547Smaybee dsl_sync_task_group_t *dstg; 26310298SMatthew.Ahrens@Sun.COM objset_t *mos = dp->dp_meta_objset; 2647468SMark.Maybee@Sun.COM hrtime_t start, write_time; 2657468SMark.Maybee@Sun.COM uint64_t data_written; 2663547Smaybee int err; 267789Sahrens 268*12296SLin.Ling@Sun.COM /* 269*12296SLin.Ling@Sun.COM * We need to copy dp_space_towrite() before doing 270*12296SLin.Ling@Sun.COM * dsl_sync_task_group_sync(), because 271*12296SLin.Ling@Sun.COM * dsl_dataset_snapshot_reserve_space() will increase 272*12296SLin.Ling@Sun.COM * dp_space_towrite but not actually write anything. 273*12296SLin.Ling@Sun.COM */ 274*12296SLin.Ling@Sun.COM data_written = dp->dp_space_towrite[txg & TXG_MASK]; 275*12296SLin.Ling@Sun.COM 276789Sahrens tx = dmu_tx_create_assigned(dp, txg); 277789Sahrens 2787468SMark.Maybee@Sun.COM dp->dp_read_overhead = 0; 2799366SMark.Maybee@Sun.COM start = gethrtime(); 2809396SMatthew.Ahrens@Sun.COM 2813547Smaybee zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 2823547Smaybee while (ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) { 2839396SMatthew.Ahrens@Sun.COM /* 2849396SMatthew.Ahrens@Sun.COM * We must not sync any non-MOS datasets twice, because 2859396SMatthew.Ahrens@Sun.COM * we may have taken a snapshot of them. However, we 2869396SMatthew.Ahrens@Sun.COM * may sync newly-created datasets on pass 2. 2879396SMatthew.Ahrens@Sun.COM */ 2889396SMatthew.Ahrens@Sun.COM ASSERT(!list_link_active(&ds->ds_synced_link)); 2899396SMatthew.Ahrens@Sun.COM list_insert_tail(&dp->dp_synced_datasets, ds); 2903547Smaybee dsl_dataset_sync(ds, zio, tx); 2913547Smaybee } 2927468SMark.Maybee@Sun.COM DTRACE_PROBE(pool_sync__1setup); 2939396SMatthew.Ahrens@Sun.COM err = zio_wait(zio); 2947468SMark.Maybee@Sun.COM 2957468SMark.Maybee@Sun.COM write_time = gethrtime() - start; 2963547Smaybee ASSERT(err == 0); 2977468SMark.Maybee@Sun.COM DTRACE_PROBE(pool_sync__2rootzio); 298789Sahrens 2999396SMatthew.Ahrens@Sun.COM for (ds = list_head(&dp->dp_synced_datasets); ds; 3009396SMatthew.Ahrens@Sun.COM ds = list_next(&dp->dp_synced_datasets, ds)) 30111935SMark.Shellenbaum@Sun.COM dmu_objset_do_userquota_updates(ds->ds_objset, tx); 3029396SMatthew.Ahrens@Sun.COM 3039396SMatthew.Ahrens@Sun.COM /* 3049396SMatthew.Ahrens@Sun.COM * Sync the datasets again to push out the changes due to 305*12296SLin.Ling@Sun.COM * userspace updates. This must be done before we process the 3069396SMatthew.Ahrens@Sun.COM * sync tasks, because that could cause a snapshot of a dataset 3079396SMatthew.Ahrens@Sun.COM * whose ds_bp will be rewritten when we do this 2nd sync. 3089396SMatthew.Ahrens@Sun.COM */ 3099396SMatthew.Ahrens@Sun.COM zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 3109396SMatthew.Ahrens@Sun.COM while (ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) { 3119396SMatthew.Ahrens@Sun.COM ASSERT(list_link_active(&ds->ds_synced_link)); 3129396SMatthew.Ahrens@Sun.COM dmu_buf_rele(ds->ds_dbuf, ds); 3139396SMatthew.Ahrens@Sun.COM dsl_dataset_sync(ds, zio, tx); 3149396SMatthew.Ahrens@Sun.COM } 3159396SMatthew.Ahrens@Sun.COM err = zio_wait(zio); 3169396SMatthew.Ahrens@Sun.COM 31710922SJeff.Bonwick@Sun.COM /* 31810922SJeff.Bonwick@Sun.COM * If anything was added to a deadlist during a zio done callback, 31910922SJeff.Bonwick@Sun.COM * it had to be put on the deferred queue. Enqueue it for real now. 32010922SJeff.Bonwick@Sun.COM */ 32110922SJeff.Bonwick@Sun.COM for (ds = list_head(&dp->dp_synced_datasets); ds; 32210922SJeff.Bonwick@Sun.COM ds = list_next(&dp->dp_synced_datasets, ds)) 32310922SJeff.Bonwick@Sun.COM bplist_sync(&ds->ds_deadlist, 32410922SJeff.Bonwick@Sun.COM bplist_enqueue_cb, &ds->ds_deadlist, tx); 32510922SJeff.Bonwick@Sun.COM 3269396SMatthew.Ahrens@Sun.COM while (dstg = txg_list_remove(&dp->dp_sync_tasks, txg)) { 3279396SMatthew.Ahrens@Sun.COM /* 3289396SMatthew.Ahrens@Sun.COM * No more sync tasks should have been added while we 3299396SMatthew.Ahrens@Sun.COM * were syncing. 3309396SMatthew.Ahrens@Sun.COM */ 3319396SMatthew.Ahrens@Sun.COM ASSERT(spa_sync_pass(dp->dp_spa) == 1); 3323547Smaybee dsl_sync_task_group_sync(dstg, tx); 3339396SMatthew.Ahrens@Sun.COM } 3347468SMark.Maybee@Sun.COM DTRACE_PROBE(pool_sync__3task); 3357468SMark.Maybee@Sun.COM 3367468SMark.Maybee@Sun.COM start = gethrtime(); 3373547Smaybee while (dd = txg_list_remove(&dp->dp_dirty_dirs, txg)) 3383547Smaybee dsl_dir_sync(dd, tx); 3397468SMark.Maybee@Sun.COM write_time += gethrtime() - start; 340789Sahrens 3417468SMark.Maybee@Sun.COM start = gethrtime(); 34210298SMatthew.Ahrens@Sun.COM if (list_head(&mos->os_dirty_dnodes[txg & TXG_MASK]) != NULL || 34310298SMatthew.Ahrens@Sun.COM list_head(&mos->os_free_dnodes[txg & TXG_MASK]) != NULL) { 3443547Smaybee zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 34510298SMatthew.Ahrens@Sun.COM dmu_objset_sync(mos, zio, tx); 3463547Smaybee err = zio_wait(zio); 3473547Smaybee ASSERT(err == 0); 348789Sahrens dprintf_bp(&dp->dp_meta_rootbp, "meta objset rootbp is %s", ""); 349789Sahrens spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp); 350789Sahrens } 3517468SMark.Maybee@Sun.COM write_time += gethrtime() - start; 3527468SMark.Maybee@Sun.COM DTRACE_PROBE2(pool_sync__4io, hrtime_t, write_time, 3537468SMark.Maybee@Sun.COM hrtime_t, dp->dp_read_overhead); 3547468SMark.Maybee@Sun.COM write_time -= dp->dp_read_overhead; 355789Sahrens 356789Sahrens dmu_tx_commit(tx); 3577468SMark.Maybee@Sun.COM 3587468SMark.Maybee@Sun.COM dp->dp_space_towrite[txg & TXG_MASK] = 0; 3597468SMark.Maybee@Sun.COM ASSERT(dp->dp_tempreserved[txg & TXG_MASK] == 0); 3607468SMark.Maybee@Sun.COM 3617468SMark.Maybee@Sun.COM /* 3627468SMark.Maybee@Sun.COM * If the write limit max has not been explicitly set, set it 3637468SMark.Maybee@Sun.COM * to a fraction of available physical memory (default 1/8th). 3647468SMark.Maybee@Sun.COM * Note that we must inflate the limit because the spa 3657468SMark.Maybee@Sun.COM * inflates write sizes to account for data replication. 3667468SMark.Maybee@Sun.COM * Check this each sync phase to catch changing memory size. 3677468SMark.Maybee@Sun.COM */ 3687468SMark.Maybee@Sun.COM if (physmem != old_physmem && zfs_write_limit_shift) { 3697468SMark.Maybee@Sun.COM mutex_enter(&zfs_write_limit_lock); 3707468SMark.Maybee@Sun.COM old_physmem = physmem; 3717468SMark.Maybee@Sun.COM zfs_write_limit_max = ptob(physmem) >> zfs_write_limit_shift; 3727468SMark.Maybee@Sun.COM zfs_write_limit_inflated = MAX(zfs_write_limit_min, 3737468SMark.Maybee@Sun.COM spa_get_asize(dp->dp_spa, zfs_write_limit_max)); 3747468SMark.Maybee@Sun.COM mutex_exit(&zfs_write_limit_lock); 3757468SMark.Maybee@Sun.COM } 3767468SMark.Maybee@Sun.COM 3777468SMark.Maybee@Sun.COM /* 3787468SMark.Maybee@Sun.COM * Attempt to keep the sync time consistent by adjusting the 3797468SMark.Maybee@Sun.COM * amount of write traffic allowed into each transaction group. 3807468SMark.Maybee@Sun.COM * Weight the throughput calculation towards the current value: 3817468SMark.Maybee@Sun.COM * thru = 3/4 old_thru + 1/4 new_thru 38211614SLin.Ling@Sun.COM * 38311614SLin.Ling@Sun.COM * Note: write_time is in nanosecs, so write_time/MICROSEC 38411614SLin.Ling@Sun.COM * yields millisecs 3857468SMark.Maybee@Sun.COM */ 3867468SMark.Maybee@Sun.COM ASSERT(zfs_write_limit_min > 0); 38711614SLin.Ling@Sun.COM if (data_written > zfs_write_limit_min / 8 && write_time > MICROSEC) { 38811614SLin.Ling@Sun.COM uint64_t throughput = data_written / (write_time / MICROSEC); 38911614SLin.Ling@Sun.COM 3907468SMark.Maybee@Sun.COM if (dp->dp_throughput) 3917468SMark.Maybee@Sun.COM dp->dp_throughput = throughput / 4 + 3927468SMark.Maybee@Sun.COM 3 * dp->dp_throughput / 4; 3937468SMark.Maybee@Sun.COM else 3947468SMark.Maybee@Sun.COM dp->dp_throughput = throughput; 3957468SMark.Maybee@Sun.COM dp->dp_write_limit = MIN(zfs_write_limit_inflated, 3967468SMark.Maybee@Sun.COM MAX(zfs_write_limit_min, 39711614SLin.Ling@Sun.COM dp->dp_throughput * zfs_txg_synctime_ms)); 3987468SMark.Maybee@Sun.COM } 399789Sahrens } 400789Sahrens 401789Sahrens void 40210922SJeff.Bonwick@Sun.COM dsl_pool_sync_done(dsl_pool_t *dp, uint64_t txg) 403789Sahrens { 404789Sahrens dsl_dataset_t *ds; 40510922SJeff.Bonwick@Sun.COM objset_t *os; 406789Sahrens 4075367Sahrens while (ds = list_head(&dp->dp_synced_datasets)) { 4085367Sahrens list_remove(&dp->dp_synced_datasets, ds); 40910922SJeff.Bonwick@Sun.COM os = ds->ds_objset; 41010922SJeff.Bonwick@Sun.COM zil_clean(os->os_zil); 41110922SJeff.Bonwick@Sun.COM ASSERT(!dmu_objset_is_dirty(os, txg)); 4123897Smaybee dmu_buf_rele(ds->ds_dbuf, ds); 413789Sahrens } 41410922SJeff.Bonwick@Sun.COM ASSERT(!dmu_objset_is_dirty(dp->dp_meta_objset, txg)); 415789Sahrens } 416789Sahrens 4173547Smaybee /* 4183547Smaybee * TRUE if the current thread is the tx_sync_thread or if we 4193547Smaybee * are being called from SPA context during pool initialization. 4203547Smaybee */ 421789Sahrens int 422789Sahrens dsl_pool_sync_context(dsl_pool_t *dp) 423789Sahrens { 424789Sahrens return (curthread == dp->dp_tx.tx_sync_thread || 4253547Smaybee spa_get_dsl(dp->dp_spa) == NULL); 426789Sahrens } 427789Sahrens 428789Sahrens uint64_t 429789Sahrens dsl_pool_adjustedsize(dsl_pool_t *dp, boolean_t netfree) 430789Sahrens { 431789Sahrens uint64_t space, resv; 432789Sahrens 433789Sahrens /* 4341775Sbillm * Reserve about 1.6% (1/64), or at least 32MB, for allocation 435789Sahrens * efficiency. 436789Sahrens * XXX The intent log is not accounted for, so it must fit 437789Sahrens * within this slop. 438789Sahrens * 439789Sahrens * If we're trying to assess whether it's OK to do a free, 440789Sahrens * cut the reservation in half to allow forward progress 441789Sahrens * (e.g. make it possible to rm(1) files from a full pool). 442789Sahrens */ 44310956SGeorge.Wilson@Sun.COM space = spa_get_dspace(dp->dp_spa); 4441775Sbillm resv = MAX(space >> 6, SPA_MINDEVSIZE >> 1); 445789Sahrens if (netfree) 446789Sahrens resv >>= 1; 447789Sahrens 448789Sahrens return (space - resv); 449789Sahrens } 4506245Smaybee 4516245Smaybee int 4526245Smaybee dsl_pool_tempreserve_space(dsl_pool_t *dp, uint64_t space, dmu_tx_t *tx) 4536245Smaybee { 4546245Smaybee uint64_t reserved = 0; 4556245Smaybee uint64_t write_limit = (zfs_write_limit_override ? 4566245Smaybee zfs_write_limit_override : dp->dp_write_limit); 4576245Smaybee 4586245Smaybee if (zfs_no_write_throttle) { 4596643Seschrock atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], 4606643Seschrock space); 4616245Smaybee return (0); 4626245Smaybee } 4636245Smaybee 4646245Smaybee /* 4656245Smaybee * Check to see if we have exceeded the maximum allowed IO for 4666245Smaybee * this transaction group. We can do this without locks since 4676245Smaybee * a little slop here is ok. Note that we do the reserved check 4686245Smaybee * with only half the requested reserve: this is because the 4696245Smaybee * reserve requests are worst-case, and we really don't want to 4706245Smaybee * throttle based off of worst-case estimates. 4716245Smaybee */ 4726245Smaybee if (write_limit > 0) { 4736245Smaybee reserved = dp->dp_space_towrite[tx->tx_txg & TXG_MASK] 4746245Smaybee + dp->dp_tempreserved[tx->tx_txg & TXG_MASK] / 2; 4756245Smaybee 4766245Smaybee if (reserved && reserved > write_limit) 4776245Smaybee return (ERESTART); 4786245Smaybee } 4796245Smaybee 4806245Smaybee atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], space); 4816245Smaybee 4826245Smaybee /* 4836245Smaybee * If this transaction group is over 7/8ths capacity, delay 4846245Smaybee * the caller 1 clock tick. This will slow down the "fill" 4856245Smaybee * rate until the sync process can catch up with us. 4866245Smaybee */ 4876740Sgw25295 if (reserved && reserved > (write_limit - (write_limit >> 3))) 4886245Smaybee txg_delay(dp, tx->tx_txg, 1); 4896245Smaybee 4906245Smaybee return (0); 4916245Smaybee } 4926245Smaybee 4936245Smaybee void 4946245Smaybee dsl_pool_tempreserve_clear(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx) 4956245Smaybee { 4966245Smaybee ASSERT(dp->dp_tempreserved[tx->tx_txg & TXG_MASK] >= space); 4976245Smaybee atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], -space); 4986245Smaybee } 4996245Smaybee 5006245Smaybee void 5016245Smaybee dsl_pool_memory_pressure(dsl_pool_t *dp) 5026245Smaybee { 5036245Smaybee uint64_t space_inuse = 0; 5046245Smaybee int i; 5056245Smaybee 5066245Smaybee if (dp->dp_write_limit == zfs_write_limit_min) 5076245Smaybee return; 5086245Smaybee 5096245Smaybee for (i = 0; i < TXG_SIZE; i++) { 5106245Smaybee space_inuse += dp->dp_space_towrite[i]; 5116245Smaybee space_inuse += dp->dp_tempreserved[i]; 5126245Smaybee } 5136245Smaybee dp->dp_write_limit = MAX(zfs_write_limit_min, 5146245Smaybee MIN(dp->dp_write_limit, space_inuse / 4)); 5156245Smaybee } 5166245Smaybee 5176245Smaybee void 5186245Smaybee dsl_pool_willuse_space(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx) 5196245Smaybee { 5206245Smaybee if (space > 0) { 5216245Smaybee mutex_enter(&dp->dp_lock); 5226245Smaybee dp->dp_space_towrite[tx->tx_txg & TXG_MASK] += space; 5236245Smaybee mutex_exit(&dp->dp_lock); 5246245Smaybee } 5256245Smaybee } 5267046Sahrens 5277046Sahrens /* ARGSUSED */ 5287046Sahrens static int 5297046Sahrens upgrade_clones_cb(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg) 5307046Sahrens { 5317046Sahrens dmu_tx_t *tx = arg; 5327046Sahrens dsl_dataset_t *ds, *prev = NULL; 5337046Sahrens int err; 5347046Sahrens dsl_pool_t *dp = spa_get_dsl(spa); 5357046Sahrens 5367046Sahrens err = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds); 5377046Sahrens if (err) 5387046Sahrens return (err); 5397046Sahrens 5407046Sahrens while (ds->ds_phys->ds_prev_snap_obj != 0) { 5417046Sahrens err = dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj, 5427046Sahrens FTAG, &prev); 5437046Sahrens if (err) { 5447046Sahrens dsl_dataset_rele(ds, FTAG); 5457046Sahrens return (err); 5467046Sahrens } 5477046Sahrens 5487046Sahrens if (prev->ds_phys->ds_next_snap_obj != ds->ds_object) 5497046Sahrens break; 5507046Sahrens dsl_dataset_rele(ds, FTAG); 5517046Sahrens ds = prev; 5527046Sahrens prev = NULL; 5537046Sahrens } 5547046Sahrens 5557046Sahrens if (prev == NULL) { 5567046Sahrens prev = dp->dp_origin_snap; 5577046Sahrens 5587046Sahrens /* 5597046Sahrens * The $ORIGIN can't have any data, or the accounting 5607046Sahrens * will be wrong. 5617046Sahrens */ 5627046Sahrens ASSERT(prev->ds_phys->ds_bp.blk_birth == 0); 5637046Sahrens 5647046Sahrens /* The origin doesn't get attached to itself */ 5657046Sahrens if (ds->ds_object == prev->ds_object) { 5667046Sahrens dsl_dataset_rele(ds, FTAG); 5677046Sahrens return (0); 5687046Sahrens } 5697046Sahrens 5707046Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 5717046Sahrens ds->ds_phys->ds_prev_snap_obj = prev->ds_object; 5727046Sahrens ds->ds_phys->ds_prev_snap_txg = prev->ds_phys->ds_creation_txg; 5737046Sahrens 5747046Sahrens dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx); 5757046Sahrens ds->ds_dir->dd_phys->dd_origin_obj = prev->ds_object; 5767046Sahrens 5777046Sahrens dmu_buf_will_dirty(prev->ds_dbuf, tx); 5787046Sahrens prev->ds_phys->ds_num_children++; 5797046Sahrens 5807046Sahrens if (ds->ds_phys->ds_next_snap_obj == 0) { 5817046Sahrens ASSERT(ds->ds_prev == NULL); 5827046Sahrens VERIFY(0 == dsl_dataset_hold_obj(dp, 5837046Sahrens ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev)); 5847046Sahrens } 5857046Sahrens } 5867046Sahrens 5877046Sahrens ASSERT(ds->ds_dir->dd_phys->dd_origin_obj == prev->ds_object); 5887046Sahrens ASSERT(ds->ds_phys->ds_prev_snap_obj == prev->ds_object); 5897046Sahrens 5907046Sahrens if (prev->ds_phys->ds_next_clones_obj == 0) { 59110801SMatthew.Ahrens@Sun.COM dmu_buf_will_dirty(prev->ds_dbuf, tx); 5927046Sahrens prev->ds_phys->ds_next_clones_obj = 5937046Sahrens zap_create(dp->dp_meta_objset, 5947046Sahrens DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx); 5957046Sahrens } 5967046Sahrens VERIFY(0 == zap_add_int(dp->dp_meta_objset, 5977046Sahrens prev->ds_phys->ds_next_clones_obj, ds->ds_object, tx)); 5987046Sahrens 5997046Sahrens dsl_dataset_rele(ds, FTAG); 6007046Sahrens if (prev != dp->dp_origin_snap) 6017046Sahrens dsl_dataset_rele(prev, FTAG); 6027046Sahrens return (0); 6037046Sahrens } 6047046Sahrens 6057046Sahrens void 6067046Sahrens dsl_pool_upgrade_clones(dsl_pool_t *dp, dmu_tx_t *tx) 6077046Sahrens { 6087046Sahrens ASSERT(dmu_tx_is_syncing(tx)); 6097046Sahrens ASSERT(dp->dp_origin_snap != NULL); 6107046Sahrens 61110801SMatthew.Ahrens@Sun.COM VERIFY3U(0, ==, dmu_objset_find_spa(dp->dp_spa, NULL, upgrade_clones_cb, 61210801SMatthew.Ahrens@Sun.COM tx, DS_FIND_CHILDREN)); 6137046Sahrens } 6147046Sahrens 6157046Sahrens void 6167046Sahrens dsl_pool_create_origin(dsl_pool_t *dp, dmu_tx_t *tx) 6177046Sahrens { 6187046Sahrens uint64_t dsobj; 6197046Sahrens dsl_dataset_t *ds; 6207046Sahrens 6217046Sahrens ASSERT(dmu_tx_is_syncing(tx)); 6227046Sahrens ASSERT(dp->dp_origin_snap == NULL); 6237046Sahrens 6247046Sahrens /* create the origin dir, ds, & snap-ds */ 6257046Sahrens rw_enter(&dp->dp_config_rwlock, RW_WRITER); 6267046Sahrens dsobj = dsl_dataset_create_sync(dp->dp_root_dir, ORIGIN_DIR_NAME, 6277046Sahrens NULL, 0, kcred, tx); 6287046Sahrens VERIFY(0 == dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds)); 629*12296SLin.Ling@Sun.COM dsl_dataset_snapshot_sync(ds, ORIGIN_DIR_NAME, tx); 6307046Sahrens VERIFY(0 == dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj, 6317046Sahrens dp, &dp->dp_origin_snap)); 6327046Sahrens dsl_dataset_rele(ds, FTAG); 6337046Sahrens rw_exit(&dp->dp_config_rwlock); 6347046Sahrens } 6359321SNeil.Perrin@Sun.COM 6369321SNeil.Perrin@Sun.COM taskq_t * 6379321SNeil.Perrin@Sun.COM dsl_pool_vnrele_taskq(dsl_pool_t *dp) 6389321SNeil.Perrin@Sun.COM { 6399321SNeil.Perrin@Sun.COM return (dp->dp_vnrele_taskq); 6409321SNeil.Perrin@Sun.COM } 64110342Schris.kirby@sun.com 64210342Schris.kirby@sun.com /* 64310342Schris.kirby@sun.com * Walk through the pool-wide zap object of temporary snapshot user holds 64410342Schris.kirby@sun.com * and release them. 64510342Schris.kirby@sun.com */ 64610342Schris.kirby@sun.com void 64710342Schris.kirby@sun.com dsl_pool_clean_tmp_userrefs(dsl_pool_t *dp) 64810342Schris.kirby@sun.com { 64910342Schris.kirby@sun.com zap_attribute_t za; 65010342Schris.kirby@sun.com zap_cursor_t zc; 65110342Schris.kirby@sun.com objset_t *mos = dp->dp_meta_objset; 65210342Schris.kirby@sun.com uint64_t zapobj = dp->dp_tmp_userrefs_obj; 65310342Schris.kirby@sun.com 65410342Schris.kirby@sun.com if (zapobj == 0) 65510342Schris.kirby@sun.com return; 65610342Schris.kirby@sun.com ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS); 65710342Schris.kirby@sun.com 65810342Schris.kirby@sun.com for (zap_cursor_init(&zc, mos, zapobj); 65910342Schris.kirby@sun.com zap_cursor_retrieve(&zc, &za) == 0; 66010342Schris.kirby@sun.com zap_cursor_advance(&zc)) { 66110342Schris.kirby@sun.com char *htag; 66210342Schris.kirby@sun.com uint64_t dsobj; 66310342Schris.kirby@sun.com 66410342Schris.kirby@sun.com htag = strchr(za.za_name, '-'); 66510342Schris.kirby@sun.com *htag = '\0'; 66610342Schris.kirby@sun.com ++htag; 66710342Schris.kirby@sun.com dsobj = strtonum(za.za_name, NULL); 66810342Schris.kirby@sun.com (void) dsl_dataset_user_release_tmp(dp, dsobj, htag); 66910342Schris.kirby@sun.com } 67010342Schris.kirby@sun.com zap_cursor_fini(&zc); 67110342Schris.kirby@sun.com } 67210342Schris.kirby@sun.com 67310342Schris.kirby@sun.com /* 67410342Schris.kirby@sun.com * Create the pool-wide zap object for storing temporary snapshot holds. 67510342Schris.kirby@sun.com */ 67610342Schris.kirby@sun.com void 67710342Schris.kirby@sun.com dsl_pool_user_hold_create_obj(dsl_pool_t *dp, dmu_tx_t *tx) 67810342Schris.kirby@sun.com { 67910342Schris.kirby@sun.com objset_t *mos = dp->dp_meta_objset; 68010342Schris.kirby@sun.com 68110342Schris.kirby@sun.com ASSERT(dp->dp_tmp_userrefs_obj == 0); 68210342Schris.kirby@sun.com ASSERT(dmu_tx_is_syncing(tx)); 68310342Schris.kirby@sun.com 68410342Schris.kirby@sun.com dp->dp_tmp_userrefs_obj = zap_create(mos, DMU_OT_USERREFS, 68510342Schris.kirby@sun.com DMU_OT_NONE, 0, tx); 68610342Schris.kirby@sun.com 68710342Schris.kirby@sun.com VERIFY(zap_add(mos, DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_TMP_USERREFS, 68810342Schris.kirby@sun.com sizeof (uint64_t), 1, &dp->dp_tmp_userrefs_obj, tx) == 0); 68910342Schris.kirby@sun.com } 69010342Schris.kirby@sun.com 69110342Schris.kirby@sun.com static int 69210342Schris.kirby@sun.com dsl_pool_user_hold_rele_impl(dsl_pool_t *dp, uint64_t dsobj, 69310951SChris.Kirby@sun.com const char *tag, uint64_t *now, dmu_tx_t *tx, boolean_t holding) 69410342Schris.kirby@sun.com { 69510342Schris.kirby@sun.com objset_t *mos = dp->dp_meta_objset; 69610342Schris.kirby@sun.com uint64_t zapobj = dp->dp_tmp_userrefs_obj; 69710342Schris.kirby@sun.com char *name; 69810342Schris.kirby@sun.com int error; 69910342Schris.kirby@sun.com 70010342Schris.kirby@sun.com ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS); 70110342Schris.kirby@sun.com ASSERT(dmu_tx_is_syncing(tx)); 70210342Schris.kirby@sun.com 70310342Schris.kirby@sun.com /* 70410342Schris.kirby@sun.com * If the pool was created prior to SPA_VERSION_USERREFS, the 70510342Schris.kirby@sun.com * zap object for temporary holds might not exist yet. 70610342Schris.kirby@sun.com */ 70710342Schris.kirby@sun.com if (zapobj == 0) { 70810342Schris.kirby@sun.com if (holding) { 70910342Schris.kirby@sun.com dsl_pool_user_hold_create_obj(dp, tx); 71010342Schris.kirby@sun.com zapobj = dp->dp_tmp_userrefs_obj; 71110342Schris.kirby@sun.com } else { 71210342Schris.kirby@sun.com return (ENOENT); 71310342Schris.kirby@sun.com } 71410342Schris.kirby@sun.com } 71510342Schris.kirby@sun.com 71610342Schris.kirby@sun.com name = kmem_asprintf("%llx-%s", (u_longlong_t)dsobj, tag); 71710342Schris.kirby@sun.com if (holding) 71810951SChris.Kirby@sun.com error = zap_add(mos, zapobj, name, 8, 1, now, tx); 71910342Schris.kirby@sun.com else 72010342Schris.kirby@sun.com error = zap_remove(mos, zapobj, name, tx); 72110342Schris.kirby@sun.com strfree(name); 72210342Schris.kirby@sun.com 72310342Schris.kirby@sun.com return (error); 72410342Schris.kirby@sun.com } 72510342Schris.kirby@sun.com 72610342Schris.kirby@sun.com /* 72710342Schris.kirby@sun.com * Add a temporary hold for the given dataset object and tag. 72810342Schris.kirby@sun.com */ 72910342Schris.kirby@sun.com int 73010342Schris.kirby@sun.com dsl_pool_user_hold(dsl_pool_t *dp, uint64_t dsobj, const char *tag, 73110951SChris.Kirby@sun.com uint64_t *now, dmu_tx_t *tx) 73210342Schris.kirby@sun.com { 73310951SChris.Kirby@sun.com return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, now, tx, B_TRUE)); 73410342Schris.kirby@sun.com } 73510342Schris.kirby@sun.com 73610342Schris.kirby@sun.com /* 73710342Schris.kirby@sun.com * Release a temporary hold for the given dataset object and tag. 73810342Schris.kirby@sun.com */ 73910342Schris.kirby@sun.com int 74010342Schris.kirby@sun.com dsl_pool_user_release(dsl_pool_t *dp, uint64_t dsobj, const char *tag, 74110342Schris.kirby@sun.com dmu_tx_t *tx) 74210342Schris.kirby@sun.com { 74310342Schris.kirby@sun.com return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, NULL, 74410342Schris.kirby@sun.com tx, B_FALSE)); 74510342Schris.kirby@sun.com } 746