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 /*
2212296SLin.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>
2712296SLin.Ling@Sun.COM #include <sys/dsl_prop.h>
28789Sahrens #include <sys/dsl_dir.h>
292199Sahrens #include <sys/dsl_synctask.h>
3012296SLin.Ling@Sun.COM #include <sys/dsl_scan.h>
3112296SLin.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>
4112470SMatthew.Ahrens@Sun.COM #include <sys/dsl_deadlist.h>
42789Sahrens
436245Smaybee int zfs_no_write_throttle = 0;
447468SMark.Maybee@Sun.COM int zfs_write_limit_shift = 3; /* 1/8th of physical memory */
4512586SGeorge.Wilson@Sun.COM int zfs_txg_synctime_ms = 1000; /* target millisecs to sync a txg */
467468SMark.Maybee@Sun.COM
477468SMark.Maybee@Sun.COM uint64_t zfs_write_limit_min = 32 << 20; /* min write limit is 32MB */
487468SMark.Maybee@Sun.COM uint64_t zfs_write_limit_max = 0; /* max data payload per txg */
497468SMark.Maybee@Sun.COM uint64_t zfs_write_limit_inflated = 0;
506245Smaybee uint64_t zfs_write_limit_override = 0;
516245Smaybee
527468SMark.Maybee@Sun.COM kmutex_t zfs_write_limit_lock;
537468SMark.Maybee@Sun.COM
547468SMark.Maybee@Sun.COM static pgcnt_t old_physmem = 0;
557046Sahrens
5612296SLin.Ling@Sun.COM int
dsl_pool_open_special_dir(dsl_pool_t * dp,const char * name,dsl_dir_t ** ddp)577046Sahrens dsl_pool_open_special_dir(dsl_pool_t *dp, const char *name, dsl_dir_t **ddp)
58789Sahrens {
59789Sahrens uint64_t obj;
60789Sahrens int err;
61789Sahrens
62789Sahrens err = zap_lookup(dp->dp_meta_objset,
63789Sahrens dp->dp_root_dir->dd_phys->dd_child_dir_zapobj,
647046Sahrens name, sizeof (obj), 1, &obj);
651544Seschrock if (err)
661544Seschrock return (err);
67789Sahrens
687046Sahrens return (dsl_dir_open_obj(dp, obj, name, dp, ddp));
69789Sahrens }
70789Sahrens
71789Sahrens static dsl_pool_t *
dsl_pool_open_impl(spa_t * spa,uint64_t txg)72789Sahrens dsl_pool_open_impl(spa_t *spa, uint64_t txg)
73789Sahrens {
74789Sahrens dsl_pool_t *dp;
75789Sahrens blkptr_t *bp = spa_get_rootblkptr(spa);
76789Sahrens
77789Sahrens dp = kmem_zalloc(sizeof (dsl_pool_t), KM_SLEEP);
78789Sahrens dp->dp_spa = spa;
79789Sahrens dp->dp_meta_rootbp = *bp;
802856Snd150628 rw_init(&dp->dp_config_rwlock, NULL, RW_DEFAULT, NULL);
816245Smaybee dp->dp_write_limit = zfs_write_limit_min;
82789Sahrens txg_init(dp, txg);
83789Sahrens
84789Sahrens txg_list_create(&dp->dp_dirty_datasets,
85789Sahrens offsetof(dsl_dataset_t, ds_dirty_link));
86789Sahrens txg_list_create(&dp->dp_dirty_dirs,
87789Sahrens offsetof(dsl_dir_t, dd_dirty_link));
882199Sahrens txg_list_create(&dp->dp_sync_tasks,
892199Sahrens offsetof(dsl_sync_task_group_t, dstg_node));
905367Sahrens list_create(&dp->dp_synced_datasets, sizeof (dsl_dataset_t),
91789Sahrens offsetof(dsl_dataset_t, ds_synced_link));
92789Sahrens
936245Smaybee mutex_init(&dp->dp_lock, NULL, MUTEX_DEFAULT, NULL);
946245Smaybee
959321SNeil.Perrin@Sun.COM dp->dp_vnrele_taskq = taskq_create("zfs_vn_rele_taskq", 1, minclsyspri,
969321SNeil.Perrin@Sun.COM 1, 4, 0);
979321SNeil.Perrin@Sun.COM
98789Sahrens return (dp);
99789Sahrens }
100789Sahrens
1011544Seschrock int
dsl_pool_open(spa_t * spa,uint64_t txg,dsl_pool_t ** dpp)1021544Seschrock dsl_pool_open(spa_t *spa, uint64_t txg, dsl_pool_t **dpp)
103789Sahrens {
104789Sahrens int err;
105789Sahrens dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
1067046Sahrens dsl_dir_t *dd;
1077046Sahrens dsl_dataset_t *ds;
10812470SMatthew.Ahrens@Sun.COM uint64_t obj;
109789Sahrens
1107046Sahrens rw_enter(&dp->dp_config_rwlock, RW_WRITER);
11110298SMatthew.Ahrens@Sun.COM err = dmu_objset_open_impl(spa, NULL, &dp->dp_meta_rootbp,
11210298SMatthew.Ahrens@Sun.COM &dp->dp_meta_objset);
1131544Seschrock if (err)
1141544Seschrock goto out;
1151544Seschrock
116789Sahrens err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
117789Sahrens DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1,
118789Sahrens &dp->dp_root_dir_obj);
1191544Seschrock if (err)
1201544Seschrock goto out;
1211544Seschrock
1221544Seschrock err = dsl_dir_open_obj(dp, dp->dp_root_dir_obj,
1231544Seschrock NULL, dp, &dp->dp_root_dir);
1241544Seschrock if (err)
1251544Seschrock goto out;
126789Sahrens
1277046Sahrens err = dsl_pool_open_special_dir(dp, MOS_DIR_NAME, &dp->dp_mos_dir);
1281544Seschrock if (err)
1291544Seschrock goto out;
1301544Seschrock
1317046Sahrens if (spa_version(spa) >= SPA_VERSION_ORIGIN) {
1327046Sahrens err = dsl_pool_open_special_dir(dp, ORIGIN_DIR_NAME, &dd);
1337046Sahrens if (err)
1347046Sahrens goto out;
1357046Sahrens err = dsl_dataset_hold_obj(dp, dd->dd_phys->dd_head_dataset_obj,
1367046Sahrens FTAG, &ds);
1379008SLin.Ling@Sun.COM if (err == 0) {
1389008SLin.Ling@Sun.COM err = dsl_dataset_hold_obj(dp,
1399008SLin.Ling@Sun.COM ds->ds_phys->ds_prev_snap_obj, dp,
1409008SLin.Ling@Sun.COM &dp->dp_origin_snap);
1419008SLin.Ling@Sun.COM dsl_dataset_rele(ds, FTAG);
1429008SLin.Ling@Sun.COM }
1439008SLin.Ling@Sun.COM dsl_dir_close(dd, dp);
1447046Sahrens if (err)
1457046Sahrens goto out;
1467046Sahrens }
1477046Sahrens
14812470SMatthew.Ahrens@Sun.COM if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
14912470SMatthew.Ahrens@Sun.COM err = dsl_pool_open_special_dir(dp, FREE_DIR_NAME,
15012470SMatthew.Ahrens@Sun.COM &dp->dp_free_dir);
15112470SMatthew.Ahrens@Sun.COM if (err)
15212470SMatthew.Ahrens@Sun.COM goto out;
15312470SMatthew.Ahrens@Sun.COM
15412470SMatthew.Ahrens@Sun.COM err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
15512470SMatthew.Ahrens@Sun.COM DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj);
15612470SMatthew.Ahrens@Sun.COM if (err)
15712470SMatthew.Ahrens@Sun.COM goto out;
15812470SMatthew.Ahrens@Sun.COM VERIFY3U(0, ==, bpobj_open(&dp->dp_free_bpobj,
15912470SMatthew.Ahrens@Sun.COM dp->dp_meta_objset, obj));
16012470SMatthew.Ahrens@Sun.COM }
16112470SMatthew.Ahrens@Sun.COM
16210342Schris.kirby@sun.com err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
16310342Schris.kirby@sun.com DMU_POOL_TMP_USERREFS, sizeof (uint64_t), 1,
16410342Schris.kirby@sun.com &dp->dp_tmp_userrefs_obj);
16510342Schris.kirby@sun.com if (err == ENOENT)
16610342Schris.kirby@sun.com err = 0;
16710342Schris.kirby@sun.com if (err)
16810342Schris.kirby@sun.com goto out;
16910342Schris.kirby@sun.com
17012296SLin.Ling@Sun.COM err = dsl_scan_init(dp, txg);
1717046Sahrens
1721544Seschrock out:
173789Sahrens rw_exit(&dp->dp_config_rwlock);
1741544Seschrock if (err)
1751544Seschrock dsl_pool_close(dp);
1761544Seschrock else
1771544Seschrock *dpp = dp;
178789Sahrens
1791544Seschrock return (err);
180789Sahrens }
181789Sahrens
182789Sahrens void
dsl_pool_close(dsl_pool_t * dp)183789Sahrens dsl_pool_close(dsl_pool_t *dp)
184789Sahrens {
1857046Sahrens /* drop our references from dsl_pool_open() */
1867046Sahrens
1877046Sahrens /*
1887046Sahrens * Since we held the origin_snap from "syncing" context (which
1897046Sahrens * includes pool-opening context), it actually only got a "ref"
1907046Sahrens * and not a hold, so just drop that here.
1917046Sahrens */
1927046Sahrens if (dp->dp_origin_snap)
1937046Sahrens dsl_dataset_drop_ref(dp->dp_origin_snap, dp);
1941544Seschrock if (dp->dp_mos_dir)
1951544Seschrock dsl_dir_close(dp->dp_mos_dir, dp);
19612470SMatthew.Ahrens@Sun.COM if (dp->dp_free_dir)
19712470SMatthew.Ahrens@Sun.COM dsl_dir_close(dp->dp_free_dir, dp);
1981544Seschrock if (dp->dp_root_dir)
1991544Seschrock dsl_dir_close(dp->dp_root_dir, dp);
200789Sahrens
20112470SMatthew.Ahrens@Sun.COM bpobj_close(&dp->dp_free_bpobj);
20212470SMatthew.Ahrens@Sun.COM
203789Sahrens /* undo the dmu_objset_open_impl(mos) from dsl_pool_open() */
2041544Seschrock if (dp->dp_meta_objset)
20510298SMatthew.Ahrens@Sun.COM dmu_objset_evict(dp->dp_meta_objset);
206789Sahrens
207789Sahrens txg_list_destroy(&dp->dp_dirty_datasets);
20811814SChris.Kirby@sun.com txg_list_destroy(&dp->dp_sync_tasks);
209789Sahrens txg_list_destroy(&dp->dp_dirty_dirs);
2105367Sahrens list_destroy(&dp->dp_synced_datasets);
211789Sahrens
2125642Smaybee arc_flush(dp->dp_spa);
213789Sahrens txg_fini(dp);
21412296SLin.Ling@Sun.COM dsl_scan_fini(dp);
2152856Snd150628 rw_destroy(&dp->dp_config_rwlock);
2166245Smaybee mutex_destroy(&dp->dp_lock);
2179321SNeil.Perrin@Sun.COM taskq_destroy(dp->dp_vnrele_taskq);
2187837SMatthew.Ahrens@Sun.COM if (dp->dp_blkstats)
2197837SMatthew.Ahrens@Sun.COM kmem_free(dp->dp_blkstats, sizeof (zfs_all_blkstats_t));
220789Sahrens kmem_free(dp, sizeof (dsl_pool_t));
221789Sahrens }
222789Sahrens
223789Sahrens dsl_pool_t *
dsl_pool_create(spa_t * spa,nvlist_t * zplprops,uint64_t txg)2247184Stimh dsl_pool_create(spa_t *spa, nvlist_t *zplprops, uint64_t txg)
225789Sahrens {
226789Sahrens int err;
227789Sahrens dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
228789Sahrens dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg);
22910298SMatthew.Ahrens@Sun.COM objset_t *os;
2307046Sahrens dsl_dataset_t *ds;
23112470SMatthew.Ahrens@Sun.COM uint64_t obj;
2327046Sahrens
2337046Sahrens /* create and open the MOS (meta-objset) */
23410298SMatthew.Ahrens@Sun.COM dp->dp_meta_objset = dmu_objset_create_impl(spa,
23510298SMatthew.Ahrens@Sun.COM NULL, &dp->dp_meta_rootbp, DMU_OST_META, tx);
236789Sahrens
237789Sahrens /* create the pool directory */
238789Sahrens err = zap_create_claim(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
239789Sahrens DMU_OT_OBJECT_DIRECTORY, DMU_OT_NONE, 0, tx);
240789Sahrens ASSERT3U(err, ==, 0);
241789Sahrens
24212296SLin.Ling@Sun.COM /* Initialize scan structures */
24312296SLin.Ling@Sun.COM VERIFY3U(0, ==, dsl_scan_init(dp, txg));
24412296SLin.Ling@Sun.COM
245789Sahrens /* create and open the root dir */
2467046Sahrens dp->dp_root_dir_obj = dsl_dir_create_sync(dp, NULL, NULL, tx);
2471544Seschrock VERIFY(0 == dsl_dir_open_obj(dp, dp->dp_root_dir_obj,
2481544Seschrock NULL, dp, &dp->dp_root_dir));
249789Sahrens
250789Sahrens /* create and open the meta-objset dir */
2517046Sahrens (void) dsl_dir_create_sync(dp, dp->dp_root_dir, MOS_DIR_NAME, tx);
2527046Sahrens VERIFY(0 == dsl_pool_open_special_dir(dp,
2537046Sahrens MOS_DIR_NAME, &dp->dp_mos_dir));
2547046Sahrens
25512470SMatthew.Ahrens@Sun.COM if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
25612470SMatthew.Ahrens@Sun.COM /* create and open the free dir */
25712470SMatthew.Ahrens@Sun.COM (void) dsl_dir_create_sync(dp, dp->dp_root_dir,
25812470SMatthew.Ahrens@Sun.COM FREE_DIR_NAME, tx);
25912470SMatthew.Ahrens@Sun.COM VERIFY(0 == dsl_pool_open_special_dir(dp,
26012470SMatthew.Ahrens@Sun.COM FREE_DIR_NAME, &dp->dp_free_dir));
26112470SMatthew.Ahrens@Sun.COM
26212470SMatthew.Ahrens@Sun.COM /* create and open the free_bplist */
26312470SMatthew.Ahrens@Sun.COM obj = bpobj_alloc(dp->dp_meta_objset, SPA_MAXBLOCKSIZE, tx);
26412470SMatthew.Ahrens@Sun.COM VERIFY(zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
26512470SMatthew.Ahrens@Sun.COM DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj, tx) == 0);
26612470SMatthew.Ahrens@Sun.COM VERIFY3U(0, ==, bpobj_open(&dp->dp_free_bpobj,
26712470SMatthew.Ahrens@Sun.COM dp->dp_meta_objset, obj));
26812470SMatthew.Ahrens@Sun.COM }
26912470SMatthew.Ahrens@Sun.COM
2707046Sahrens if (spa_version(spa) >= SPA_VERSION_DSL_SCRUB)
2717046Sahrens dsl_pool_create_origin(dp, tx);
2727046Sahrens
2737046Sahrens /* create the root dataset */
27412470SMatthew.Ahrens@Sun.COM obj = dsl_dataset_create_sync_dd(dp->dp_root_dir, NULL, 0, tx);
2757046Sahrens
2767046Sahrens /* create the root objset */
27712470SMatthew.Ahrens@Sun.COM VERIFY(0 == dsl_dataset_hold_obj(dp, obj, FTAG, &ds));
27810298SMatthew.Ahrens@Sun.COM os = dmu_objset_create_impl(dp->dp_spa, ds,
2797046Sahrens dsl_dataset_get_blkptr(ds), DMU_OST_ZFS, tx);
2807046Sahrens #ifdef _KERNEL
28110298SMatthew.Ahrens@Sun.COM zfs_create_fs(os, kcred, zplprops, tx);
2827046Sahrens #endif
2837046Sahrens dsl_dataset_rele(ds, FTAG);
284789Sahrens
285789Sahrens dmu_tx_commit(tx);
286789Sahrens
287789Sahrens return (dp);
288789Sahrens }
289789Sahrens
29012470SMatthew.Ahrens@Sun.COM static int
deadlist_enqueue_cb(void * arg,const blkptr_t * bp,dmu_tx_t * tx)29112470SMatthew.Ahrens@Sun.COM deadlist_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
29212470SMatthew.Ahrens@Sun.COM {
29312470SMatthew.Ahrens@Sun.COM dsl_deadlist_t *dl = arg;
29412470SMatthew.Ahrens@Sun.COM dsl_deadlist_insert(dl, bp, tx);
29512470SMatthew.Ahrens@Sun.COM return (0);
29612470SMatthew.Ahrens@Sun.COM }
29712470SMatthew.Ahrens@Sun.COM
298789Sahrens void
dsl_pool_sync(dsl_pool_t * dp,uint64_t txg)299789Sahrens dsl_pool_sync(dsl_pool_t *dp, uint64_t txg)
300789Sahrens {
3013547Smaybee zio_t *zio;
302789Sahrens dmu_tx_t *tx;
3033547Smaybee dsl_dir_t *dd;
3043547Smaybee dsl_dataset_t *ds;
3053547Smaybee dsl_sync_task_group_t *dstg;
30610298SMatthew.Ahrens@Sun.COM objset_t *mos = dp->dp_meta_objset;
3077468SMark.Maybee@Sun.COM hrtime_t start, write_time;
3087468SMark.Maybee@Sun.COM uint64_t data_written;
3093547Smaybee int err;
310789Sahrens
31112296SLin.Ling@Sun.COM /*
31212296SLin.Ling@Sun.COM * We need to copy dp_space_towrite() before doing
31312296SLin.Ling@Sun.COM * dsl_sync_task_group_sync(), because
31412296SLin.Ling@Sun.COM * dsl_dataset_snapshot_reserve_space() will increase
31512296SLin.Ling@Sun.COM * dp_space_towrite but not actually write anything.
31612296SLin.Ling@Sun.COM */
31712296SLin.Ling@Sun.COM data_written = dp->dp_space_towrite[txg & TXG_MASK];
31812296SLin.Ling@Sun.COM
319789Sahrens tx = dmu_tx_create_assigned(dp, txg);
320789Sahrens
3217468SMark.Maybee@Sun.COM dp->dp_read_overhead = 0;
3229366SMark.Maybee@Sun.COM start = gethrtime();
3239396SMatthew.Ahrens@Sun.COM
3243547Smaybee zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
3253547Smaybee while (ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) {
3269396SMatthew.Ahrens@Sun.COM /*
3279396SMatthew.Ahrens@Sun.COM * We must not sync any non-MOS datasets twice, because
3289396SMatthew.Ahrens@Sun.COM * we may have taken a snapshot of them. However, we
3299396SMatthew.Ahrens@Sun.COM * may sync newly-created datasets on pass 2.
3309396SMatthew.Ahrens@Sun.COM */
3319396SMatthew.Ahrens@Sun.COM ASSERT(!list_link_active(&ds->ds_synced_link));
3329396SMatthew.Ahrens@Sun.COM list_insert_tail(&dp->dp_synced_datasets, ds);
3333547Smaybee dsl_dataset_sync(ds, zio, tx);
3343547Smaybee }
3357468SMark.Maybee@Sun.COM DTRACE_PROBE(pool_sync__1setup);
3369396SMatthew.Ahrens@Sun.COM err = zio_wait(zio);
3377468SMark.Maybee@Sun.COM
3387468SMark.Maybee@Sun.COM write_time = gethrtime() - start;
3393547Smaybee ASSERT(err == 0);
3407468SMark.Maybee@Sun.COM DTRACE_PROBE(pool_sync__2rootzio);
341789Sahrens
3429396SMatthew.Ahrens@Sun.COM for (ds = list_head(&dp->dp_synced_datasets); ds;
3439396SMatthew.Ahrens@Sun.COM ds = list_next(&dp->dp_synced_datasets, ds))
34411935SMark.Shellenbaum@Sun.COM dmu_objset_do_userquota_updates(ds->ds_objset, tx);
3459396SMatthew.Ahrens@Sun.COM
3469396SMatthew.Ahrens@Sun.COM /*
3479396SMatthew.Ahrens@Sun.COM * Sync the datasets again to push out the changes due to
34812296SLin.Ling@Sun.COM * userspace updates. This must be done before we process the
3499396SMatthew.Ahrens@Sun.COM * sync tasks, because that could cause a snapshot of a dataset
3509396SMatthew.Ahrens@Sun.COM * whose ds_bp will be rewritten when we do this 2nd sync.
3519396SMatthew.Ahrens@Sun.COM */
3529396SMatthew.Ahrens@Sun.COM zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
3539396SMatthew.Ahrens@Sun.COM while (ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) {
3549396SMatthew.Ahrens@Sun.COM ASSERT(list_link_active(&ds->ds_synced_link));
3559396SMatthew.Ahrens@Sun.COM dmu_buf_rele(ds->ds_dbuf, ds);
3569396SMatthew.Ahrens@Sun.COM dsl_dataset_sync(ds, zio, tx);
3579396SMatthew.Ahrens@Sun.COM }
3589396SMatthew.Ahrens@Sun.COM err = zio_wait(zio);
3599396SMatthew.Ahrens@Sun.COM
36010922SJeff.Bonwick@Sun.COM /*
36112470SMatthew.Ahrens@Sun.COM * Move dead blocks from the pending deadlist to the on-disk
36212470SMatthew.Ahrens@Sun.COM * deadlist.
36310922SJeff.Bonwick@Sun.COM */
36410922SJeff.Bonwick@Sun.COM for (ds = list_head(&dp->dp_synced_datasets); ds;
36512470SMatthew.Ahrens@Sun.COM ds = list_next(&dp->dp_synced_datasets, ds)) {
36612470SMatthew.Ahrens@Sun.COM bplist_iterate(&ds->ds_pending_deadlist,
36712470SMatthew.Ahrens@Sun.COM deadlist_enqueue_cb, &ds->ds_deadlist, tx);
36812470SMatthew.Ahrens@Sun.COM }
36910922SJeff.Bonwick@Sun.COM
3709396SMatthew.Ahrens@Sun.COM while (dstg = txg_list_remove(&dp->dp_sync_tasks, txg)) {
3719396SMatthew.Ahrens@Sun.COM /*
3729396SMatthew.Ahrens@Sun.COM * No more sync tasks should have been added while we
3739396SMatthew.Ahrens@Sun.COM * were syncing.
3749396SMatthew.Ahrens@Sun.COM */
3759396SMatthew.Ahrens@Sun.COM ASSERT(spa_sync_pass(dp->dp_spa) == 1);
3763547Smaybee dsl_sync_task_group_sync(dstg, tx);
3779396SMatthew.Ahrens@Sun.COM }
3787468SMark.Maybee@Sun.COM DTRACE_PROBE(pool_sync__3task);
3797468SMark.Maybee@Sun.COM
3807468SMark.Maybee@Sun.COM start = gethrtime();
3813547Smaybee while (dd = txg_list_remove(&dp->dp_dirty_dirs, txg))
3823547Smaybee dsl_dir_sync(dd, tx);
3837468SMark.Maybee@Sun.COM write_time += gethrtime() - start;
384789Sahrens
3857468SMark.Maybee@Sun.COM start = gethrtime();
38610298SMatthew.Ahrens@Sun.COM if (list_head(&mos->os_dirty_dnodes[txg & TXG_MASK]) != NULL ||
38710298SMatthew.Ahrens@Sun.COM list_head(&mos->os_free_dnodes[txg & TXG_MASK]) != NULL) {
3883547Smaybee zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
38910298SMatthew.Ahrens@Sun.COM dmu_objset_sync(mos, zio, tx);
3903547Smaybee err = zio_wait(zio);
3913547Smaybee ASSERT(err == 0);
392789Sahrens dprintf_bp(&dp->dp_meta_rootbp, "meta objset rootbp is %s", "");
393789Sahrens spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
394789Sahrens }
3957468SMark.Maybee@Sun.COM write_time += gethrtime() - start;
3967468SMark.Maybee@Sun.COM DTRACE_PROBE2(pool_sync__4io, hrtime_t, write_time,
3977468SMark.Maybee@Sun.COM hrtime_t, dp->dp_read_overhead);
3987468SMark.Maybee@Sun.COM write_time -= dp->dp_read_overhead;
399789Sahrens
400789Sahrens dmu_tx_commit(tx);
4017468SMark.Maybee@Sun.COM
4027468SMark.Maybee@Sun.COM dp->dp_space_towrite[txg & TXG_MASK] = 0;
4037468SMark.Maybee@Sun.COM ASSERT(dp->dp_tempreserved[txg & TXG_MASK] == 0);
4047468SMark.Maybee@Sun.COM
4057468SMark.Maybee@Sun.COM /*
4067468SMark.Maybee@Sun.COM * If the write limit max has not been explicitly set, set it
4077468SMark.Maybee@Sun.COM * to a fraction of available physical memory (default 1/8th).
4087468SMark.Maybee@Sun.COM * Note that we must inflate the limit because the spa
4097468SMark.Maybee@Sun.COM * inflates write sizes to account for data replication.
4107468SMark.Maybee@Sun.COM * Check this each sync phase to catch changing memory size.
4117468SMark.Maybee@Sun.COM */
4127468SMark.Maybee@Sun.COM if (physmem != old_physmem && zfs_write_limit_shift) {
4137468SMark.Maybee@Sun.COM mutex_enter(&zfs_write_limit_lock);
4147468SMark.Maybee@Sun.COM old_physmem = physmem;
4157468SMark.Maybee@Sun.COM zfs_write_limit_max = ptob(physmem) >> zfs_write_limit_shift;
4167468SMark.Maybee@Sun.COM zfs_write_limit_inflated = MAX(zfs_write_limit_min,
4177468SMark.Maybee@Sun.COM spa_get_asize(dp->dp_spa, zfs_write_limit_max));
4187468SMark.Maybee@Sun.COM mutex_exit(&zfs_write_limit_lock);
4197468SMark.Maybee@Sun.COM }
4207468SMark.Maybee@Sun.COM
4217468SMark.Maybee@Sun.COM /*
4227468SMark.Maybee@Sun.COM * Attempt to keep the sync time consistent by adjusting the
4237468SMark.Maybee@Sun.COM * amount of write traffic allowed into each transaction group.
4247468SMark.Maybee@Sun.COM * Weight the throughput calculation towards the current value:
4257468SMark.Maybee@Sun.COM * thru = 3/4 old_thru + 1/4 new_thru
42611614SLin.Ling@Sun.COM *
42711614SLin.Ling@Sun.COM * Note: write_time is in nanosecs, so write_time/MICROSEC
42811614SLin.Ling@Sun.COM * yields millisecs
4297468SMark.Maybee@Sun.COM */
4307468SMark.Maybee@Sun.COM ASSERT(zfs_write_limit_min > 0);
43111614SLin.Ling@Sun.COM if (data_written > zfs_write_limit_min / 8 && write_time > MICROSEC) {
43211614SLin.Ling@Sun.COM uint64_t throughput = data_written / (write_time / MICROSEC);
43311614SLin.Ling@Sun.COM
4347468SMark.Maybee@Sun.COM if (dp->dp_throughput)
4357468SMark.Maybee@Sun.COM dp->dp_throughput = throughput / 4 +
4367468SMark.Maybee@Sun.COM 3 * dp->dp_throughput / 4;
4377468SMark.Maybee@Sun.COM else
4387468SMark.Maybee@Sun.COM dp->dp_throughput = throughput;
4397468SMark.Maybee@Sun.COM dp->dp_write_limit = MIN(zfs_write_limit_inflated,
4407468SMark.Maybee@Sun.COM MAX(zfs_write_limit_min,
44111614SLin.Ling@Sun.COM dp->dp_throughput * zfs_txg_synctime_ms));
4427468SMark.Maybee@Sun.COM }
443789Sahrens }
444789Sahrens
445789Sahrens void
dsl_pool_sync_done(dsl_pool_t * dp,uint64_t txg)44610922SJeff.Bonwick@Sun.COM dsl_pool_sync_done(dsl_pool_t *dp, uint64_t txg)
447789Sahrens {
448789Sahrens dsl_dataset_t *ds;
44910922SJeff.Bonwick@Sun.COM objset_t *os;
450789Sahrens
4515367Sahrens while (ds = list_head(&dp->dp_synced_datasets)) {
4525367Sahrens list_remove(&dp->dp_synced_datasets, ds);
45310922SJeff.Bonwick@Sun.COM os = ds->ds_objset;
45412699SNeil.Perrin@Sun.COM zil_clean(os->os_zil, txg);
45510922SJeff.Bonwick@Sun.COM ASSERT(!dmu_objset_is_dirty(os, txg));
4563897Smaybee dmu_buf_rele(ds->ds_dbuf, ds);
457789Sahrens }
45810922SJeff.Bonwick@Sun.COM ASSERT(!dmu_objset_is_dirty(dp->dp_meta_objset, txg));
459789Sahrens }
460789Sahrens
4613547Smaybee /*
4623547Smaybee * TRUE if the current thread is the tx_sync_thread or if we
4633547Smaybee * are being called from SPA context during pool initialization.
4643547Smaybee */
465789Sahrens int
dsl_pool_sync_context(dsl_pool_t * dp)466789Sahrens dsl_pool_sync_context(dsl_pool_t *dp)
467789Sahrens {
468789Sahrens return (curthread == dp->dp_tx.tx_sync_thread ||
4693547Smaybee spa_get_dsl(dp->dp_spa) == NULL);
470789Sahrens }
471789Sahrens
472789Sahrens uint64_t
dsl_pool_adjustedsize(dsl_pool_t * dp,boolean_t netfree)473789Sahrens dsl_pool_adjustedsize(dsl_pool_t *dp, boolean_t netfree)
474789Sahrens {
475789Sahrens uint64_t space, resv;
476789Sahrens
477789Sahrens /*
4781775Sbillm * Reserve about 1.6% (1/64), or at least 32MB, for allocation
479789Sahrens * efficiency.
480789Sahrens * XXX The intent log is not accounted for, so it must fit
481789Sahrens * within this slop.
482789Sahrens *
483789Sahrens * If we're trying to assess whether it's OK to do a free,
484789Sahrens * cut the reservation in half to allow forward progress
485789Sahrens * (e.g. make it possible to rm(1) files from a full pool).
486789Sahrens */
48710956SGeorge.Wilson@Sun.COM space = spa_get_dspace(dp->dp_spa);
4881775Sbillm resv = MAX(space >> 6, SPA_MINDEVSIZE >> 1);
489789Sahrens if (netfree)
490789Sahrens resv >>= 1;
491789Sahrens
492789Sahrens return (space - resv);
493789Sahrens }
4946245Smaybee
4956245Smaybee int
dsl_pool_tempreserve_space(dsl_pool_t * dp,uint64_t space,dmu_tx_t * tx)4966245Smaybee dsl_pool_tempreserve_space(dsl_pool_t *dp, uint64_t space, dmu_tx_t *tx)
4976245Smaybee {
4986245Smaybee uint64_t reserved = 0;
4996245Smaybee uint64_t write_limit = (zfs_write_limit_override ?
5006245Smaybee zfs_write_limit_override : dp->dp_write_limit);
5016245Smaybee
5026245Smaybee if (zfs_no_write_throttle) {
5036643Seschrock atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK],
5046643Seschrock space);
5056245Smaybee return (0);
5066245Smaybee }
5076245Smaybee
5086245Smaybee /*
5096245Smaybee * Check to see if we have exceeded the maximum allowed IO for
5106245Smaybee * this transaction group. We can do this without locks since
5116245Smaybee * a little slop here is ok. Note that we do the reserved check
5126245Smaybee * with only half the requested reserve: this is because the
5136245Smaybee * reserve requests are worst-case, and we really don't want to
5146245Smaybee * throttle based off of worst-case estimates.
5156245Smaybee */
5166245Smaybee if (write_limit > 0) {
5176245Smaybee reserved = dp->dp_space_towrite[tx->tx_txg & TXG_MASK]
5186245Smaybee + dp->dp_tempreserved[tx->tx_txg & TXG_MASK] / 2;
5196245Smaybee
5206245Smaybee if (reserved && reserved > write_limit)
5216245Smaybee return (ERESTART);
5226245Smaybee }
5236245Smaybee
5246245Smaybee atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], space);
5256245Smaybee
5266245Smaybee /*
5276245Smaybee * If this transaction group is over 7/8ths capacity, delay
5286245Smaybee * the caller 1 clock tick. This will slow down the "fill"
5296245Smaybee * rate until the sync process can catch up with us.
5306245Smaybee */
5316740Sgw25295 if (reserved && reserved > (write_limit - (write_limit >> 3)))
5326245Smaybee txg_delay(dp, tx->tx_txg, 1);
5336245Smaybee
5346245Smaybee return (0);
5356245Smaybee }
5366245Smaybee
5376245Smaybee void
dsl_pool_tempreserve_clear(dsl_pool_t * dp,int64_t space,dmu_tx_t * tx)5386245Smaybee dsl_pool_tempreserve_clear(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx)
5396245Smaybee {
5406245Smaybee ASSERT(dp->dp_tempreserved[tx->tx_txg & TXG_MASK] >= space);
5416245Smaybee atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], -space);
5426245Smaybee }
5436245Smaybee
5446245Smaybee void
dsl_pool_memory_pressure(dsl_pool_t * dp)5456245Smaybee dsl_pool_memory_pressure(dsl_pool_t *dp)
5466245Smaybee {
5476245Smaybee uint64_t space_inuse = 0;
5486245Smaybee int i;
5496245Smaybee
5506245Smaybee if (dp->dp_write_limit == zfs_write_limit_min)
5516245Smaybee return;
5526245Smaybee
5536245Smaybee for (i = 0; i < TXG_SIZE; i++) {
5546245Smaybee space_inuse += dp->dp_space_towrite[i];
5556245Smaybee space_inuse += dp->dp_tempreserved[i];
5566245Smaybee }
5576245Smaybee dp->dp_write_limit = MAX(zfs_write_limit_min,
5586245Smaybee MIN(dp->dp_write_limit, space_inuse / 4));
5596245Smaybee }
5606245Smaybee
5616245Smaybee void
dsl_pool_willuse_space(dsl_pool_t * dp,int64_t space,dmu_tx_t * tx)5626245Smaybee dsl_pool_willuse_space(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx)
5636245Smaybee {
5646245Smaybee if (space > 0) {
5656245Smaybee mutex_enter(&dp->dp_lock);
5666245Smaybee dp->dp_space_towrite[tx->tx_txg & TXG_MASK] += space;
5676245Smaybee mutex_exit(&dp->dp_lock);
5686245Smaybee }
5696245Smaybee }
5707046Sahrens
5717046Sahrens /* ARGSUSED */
5727046Sahrens static int
upgrade_clones_cb(spa_t * spa,uint64_t dsobj,const char * dsname,void * arg)5737046Sahrens upgrade_clones_cb(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
5747046Sahrens {
5757046Sahrens dmu_tx_t *tx = arg;
5767046Sahrens dsl_dataset_t *ds, *prev = NULL;
5777046Sahrens int err;
5787046Sahrens dsl_pool_t *dp = spa_get_dsl(spa);
5797046Sahrens
5807046Sahrens err = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds);
5817046Sahrens if (err)
5827046Sahrens return (err);
5837046Sahrens
5847046Sahrens while (ds->ds_phys->ds_prev_snap_obj != 0) {
5857046Sahrens err = dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
5867046Sahrens FTAG, &prev);
5877046Sahrens if (err) {
5887046Sahrens dsl_dataset_rele(ds, FTAG);
5897046Sahrens return (err);
5907046Sahrens }
5917046Sahrens
5927046Sahrens if (prev->ds_phys->ds_next_snap_obj != ds->ds_object)
5937046Sahrens break;
5947046Sahrens dsl_dataset_rele(ds, FTAG);
5957046Sahrens ds = prev;
5967046Sahrens prev = NULL;
5977046Sahrens }
5987046Sahrens
5997046Sahrens if (prev == NULL) {
6007046Sahrens prev = dp->dp_origin_snap;
6017046Sahrens
6027046Sahrens /*
6037046Sahrens * The $ORIGIN can't have any data, or the accounting
6047046Sahrens * will be wrong.
6057046Sahrens */
6067046Sahrens ASSERT(prev->ds_phys->ds_bp.blk_birth == 0);
6077046Sahrens
6087046Sahrens /* The origin doesn't get attached to itself */
6097046Sahrens if (ds->ds_object == prev->ds_object) {
6107046Sahrens dsl_dataset_rele(ds, FTAG);
6117046Sahrens return (0);
6127046Sahrens }
6137046Sahrens
6147046Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx);
6157046Sahrens ds->ds_phys->ds_prev_snap_obj = prev->ds_object;
6167046Sahrens ds->ds_phys->ds_prev_snap_txg = prev->ds_phys->ds_creation_txg;
6177046Sahrens
6187046Sahrens dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
6197046Sahrens ds->ds_dir->dd_phys->dd_origin_obj = prev->ds_object;
6207046Sahrens
6217046Sahrens dmu_buf_will_dirty(prev->ds_dbuf, tx);
6227046Sahrens prev->ds_phys->ds_num_children++;
6237046Sahrens
6247046Sahrens if (ds->ds_phys->ds_next_snap_obj == 0) {
6257046Sahrens ASSERT(ds->ds_prev == NULL);
6267046Sahrens VERIFY(0 == dsl_dataset_hold_obj(dp,
6277046Sahrens ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev));
6287046Sahrens }
6297046Sahrens }
6307046Sahrens
6317046Sahrens ASSERT(ds->ds_dir->dd_phys->dd_origin_obj == prev->ds_object);
6327046Sahrens ASSERT(ds->ds_phys->ds_prev_snap_obj == prev->ds_object);
6337046Sahrens
6347046Sahrens if (prev->ds_phys->ds_next_clones_obj == 0) {
63510801SMatthew.Ahrens@Sun.COM dmu_buf_will_dirty(prev->ds_dbuf, tx);
6367046Sahrens prev->ds_phys->ds_next_clones_obj =
6377046Sahrens zap_create(dp->dp_meta_objset,
6387046Sahrens DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
6397046Sahrens }
6407046Sahrens VERIFY(0 == zap_add_int(dp->dp_meta_objset,
6417046Sahrens prev->ds_phys->ds_next_clones_obj, ds->ds_object, tx));
6427046Sahrens
6437046Sahrens dsl_dataset_rele(ds, FTAG);
6447046Sahrens if (prev != dp->dp_origin_snap)
6457046Sahrens dsl_dataset_rele(prev, FTAG);
6467046Sahrens return (0);
6477046Sahrens }
6487046Sahrens
6497046Sahrens void
dsl_pool_upgrade_clones(dsl_pool_t * dp,dmu_tx_t * tx)6507046Sahrens dsl_pool_upgrade_clones(dsl_pool_t *dp, dmu_tx_t *tx)
6517046Sahrens {
6527046Sahrens ASSERT(dmu_tx_is_syncing(tx));
6537046Sahrens ASSERT(dp->dp_origin_snap != NULL);
6547046Sahrens
65510801SMatthew.Ahrens@Sun.COM VERIFY3U(0, ==, dmu_objset_find_spa(dp->dp_spa, NULL, upgrade_clones_cb,
65610801SMatthew.Ahrens@Sun.COM tx, DS_FIND_CHILDREN));
6577046Sahrens }
6587046Sahrens
65912470SMatthew.Ahrens@Sun.COM /* ARGSUSED */
66012470SMatthew.Ahrens@Sun.COM static int
upgrade_dir_clones_cb(spa_t * spa,uint64_t dsobj,const char * dsname,void * arg)66112470SMatthew.Ahrens@Sun.COM upgrade_dir_clones_cb(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
66212470SMatthew.Ahrens@Sun.COM {
66312470SMatthew.Ahrens@Sun.COM dmu_tx_t *tx = arg;
66412470SMatthew.Ahrens@Sun.COM dsl_dataset_t *ds;
66512470SMatthew.Ahrens@Sun.COM dsl_pool_t *dp = spa_get_dsl(spa);
66612470SMatthew.Ahrens@Sun.COM objset_t *mos = dp->dp_meta_objset;
66712470SMatthew.Ahrens@Sun.COM
66812470SMatthew.Ahrens@Sun.COM VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
66912470SMatthew.Ahrens@Sun.COM
67012470SMatthew.Ahrens@Sun.COM if (ds->ds_dir->dd_phys->dd_origin_obj) {
67112470SMatthew.Ahrens@Sun.COM dsl_dataset_t *origin;
67212470SMatthew.Ahrens@Sun.COM
67312470SMatthew.Ahrens@Sun.COM VERIFY3U(0, ==, dsl_dataset_hold_obj(dp,
67412470SMatthew.Ahrens@Sun.COM ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &origin));
67512470SMatthew.Ahrens@Sun.COM
67612470SMatthew.Ahrens@Sun.COM if (origin->ds_dir->dd_phys->dd_clones == 0) {
67712470SMatthew.Ahrens@Sun.COM dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx);
67812470SMatthew.Ahrens@Sun.COM origin->ds_dir->dd_phys->dd_clones = zap_create(mos,
67912470SMatthew.Ahrens@Sun.COM DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
68012470SMatthew.Ahrens@Sun.COM }
68112470SMatthew.Ahrens@Sun.COM
68212470SMatthew.Ahrens@Sun.COM VERIFY3U(0, ==, zap_add_int(dp->dp_meta_objset,
68312470SMatthew.Ahrens@Sun.COM origin->ds_dir->dd_phys->dd_clones, dsobj, tx));
68412470SMatthew.Ahrens@Sun.COM
68512470SMatthew.Ahrens@Sun.COM dsl_dataset_rele(origin, FTAG);
68612470SMatthew.Ahrens@Sun.COM }
68712470SMatthew.Ahrens@Sun.COM
68812470SMatthew.Ahrens@Sun.COM dsl_dataset_rele(ds, FTAG);
68912470SMatthew.Ahrens@Sun.COM return (0);
69012470SMatthew.Ahrens@Sun.COM }
69112470SMatthew.Ahrens@Sun.COM
69212470SMatthew.Ahrens@Sun.COM void
dsl_pool_upgrade_dir_clones(dsl_pool_t * dp,dmu_tx_t * tx)69312470SMatthew.Ahrens@Sun.COM dsl_pool_upgrade_dir_clones(dsl_pool_t *dp, dmu_tx_t *tx)
69412470SMatthew.Ahrens@Sun.COM {
69512470SMatthew.Ahrens@Sun.COM ASSERT(dmu_tx_is_syncing(tx));
69612470SMatthew.Ahrens@Sun.COM uint64_t obj;
69712470SMatthew.Ahrens@Sun.COM
69812470SMatthew.Ahrens@Sun.COM (void) dsl_dir_create_sync(dp, dp->dp_root_dir, FREE_DIR_NAME, tx);
69912470SMatthew.Ahrens@Sun.COM VERIFY(0 == dsl_pool_open_special_dir(dp,
70012470SMatthew.Ahrens@Sun.COM FREE_DIR_NAME, &dp->dp_free_dir));
70112470SMatthew.Ahrens@Sun.COM
70212470SMatthew.Ahrens@Sun.COM /*
70312470SMatthew.Ahrens@Sun.COM * We can't use bpobj_alloc(), because spa_version() still
70412470SMatthew.Ahrens@Sun.COM * returns the old version, and we need a new-version bpobj with
70512470SMatthew.Ahrens@Sun.COM * subobj support. So call dmu_object_alloc() directly.
70612470SMatthew.Ahrens@Sun.COM */
70712470SMatthew.Ahrens@Sun.COM obj = dmu_object_alloc(dp->dp_meta_objset, DMU_OT_BPOBJ,
70812470SMatthew.Ahrens@Sun.COM SPA_MAXBLOCKSIZE, DMU_OT_BPOBJ_HDR, sizeof (bpobj_phys_t), tx);
70912470SMatthew.Ahrens@Sun.COM VERIFY3U(0, ==, zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
71012470SMatthew.Ahrens@Sun.COM DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj, tx));
71112470SMatthew.Ahrens@Sun.COM VERIFY3U(0, ==, bpobj_open(&dp->dp_free_bpobj,
71212470SMatthew.Ahrens@Sun.COM dp->dp_meta_objset, obj));
71312470SMatthew.Ahrens@Sun.COM
71412470SMatthew.Ahrens@Sun.COM VERIFY3U(0, ==, dmu_objset_find_spa(dp->dp_spa, NULL,
71512470SMatthew.Ahrens@Sun.COM upgrade_dir_clones_cb, tx, DS_FIND_CHILDREN));
71612470SMatthew.Ahrens@Sun.COM }
71712470SMatthew.Ahrens@Sun.COM
7187046Sahrens void
dsl_pool_create_origin(dsl_pool_t * dp,dmu_tx_t * tx)7197046Sahrens dsl_pool_create_origin(dsl_pool_t *dp, dmu_tx_t *tx)
7207046Sahrens {
7217046Sahrens uint64_t dsobj;
7227046Sahrens dsl_dataset_t *ds;
7237046Sahrens
7247046Sahrens ASSERT(dmu_tx_is_syncing(tx));
7257046Sahrens ASSERT(dp->dp_origin_snap == NULL);
7267046Sahrens
7277046Sahrens /* create the origin dir, ds, & snap-ds */
7287046Sahrens rw_enter(&dp->dp_config_rwlock, RW_WRITER);
7297046Sahrens dsobj = dsl_dataset_create_sync(dp->dp_root_dir, ORIGIN_DIR_NAME,
7307046Sahrens NULL, 0, kcred, tx);
7317046Sahrens VERIFY(0 == dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
73212296SLin.Ling@Sun.COM dsl_dataset_snapshot_sync(ds, ORIGIN_DIR_NAME, tx);
7337046Sahrens VERIFY(0 == dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
7347046Sahrens dp, &dp->dp_origin_snap));
7357046Sahrens dsl_dataset_rele(ds, FTAG);
7367046Sahrens rw_exit(&dp->dp_config_rwlock);
7377046Sahrens }
7389321SNeil.Perrin@Sun.COM
7399321SNeil.Perrin@Sun.COM taskq_t *
dsl_pool_vnrele_taskq(dsl_pool_t * dp)7409321SNeil.Perrin@Sun.COM dsl_pool_vnrele_taskq(dsl_pool_t *dp)
7419321SNeil.Perrin@Sun.COM {
7429321SNeil.Perrin@Sun.COM return (dp->dp_vnrele_taskq);
7439321SNeil.Perrin@Sun.COM }
74410342Schris.kirby@sun.com
74510342Schris.kirby@sun.com /*
74610342Schris.kirby@sun.com * Walk through the pool-wide zap object of temporary snapshot user holds
74710342Schris.kirby@sun.com * and release them.
74810342Schris.kirby@sun.com */
74910342Schris.kirby@sun.com void
dsl_pool_clean_tmp_userrefs(dsl_pool_t * dp)75010342Schris.kirby@sun.com dsl_pool_clean_tmp_userrefs(dsl_pool_t *dp)
75110342Schris.kirby@sun.com {
75210342Schris.kirby@sun.com zap_attribute_t za;
75310342Schris.kirby@sun.com zap_cursor_t zc;
75410342Schris.kirby@sun.com objset_t *mos = dp->dp_meta_objset;
75510342Schris.kirby@sun.com uint64_t zapobj = dp->dp_tmp_userrefs_obj;
75610342Schris.kirby@sun.com
75710342Schris.kirby@sun.com if (zapobj == 0)
75810342Schris.kirby@sun.com return;
75910342Schris.kirby@sun.com ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
76010342Schris.kirby@sun.com
76110342Schris.kirby@sun.com for (zap_cursor_init(&zc, mos, zapobj);
76210342Schris.kirby@sun.com zap_cursor_retrieve(&zc, &za) == 0;
76310342Schris.kirby@sun.com zap_cursor_advance(&zc)) {
76410342Schris.kirby@sun.com char *htag;
76510342Schris.kirby@sun.com uint64_t dsobj;
76610342Schris.kirby@sun.com
76710342Schris.kirby@sun.com htag = strchr(za.za_name, '-');
76810342Schris.kirby@sun.com *htag = '\0';
76910342Schris.kirby@sun.com ++htag;
77010342Schris.kirby@sun.com dsobj = strtonum(za.za_name, NULL);
771*12786SChris.Kirby@oracle.com (void) dsl_dataset_user_release_tmp(dp, dsobj, htag, B_FALSE);
77210342Schris.kirby@sun.com }
77310342Schris.kirby@sun.com zap_cursor_fini(&zc);
77410342Schris.kirby@sun.com }
77510342Schris.kirby@sun.com
77610342Schris.kirby@sun.com /*
77710342Schris.kirby@sun.com * Create the pool-wide zap object for storing temporary snapshot holds.
77810342Schris.kirby@sun.com */
77910342Schris.kirby@sun.com void
dsl_pool_user_hold_create_obj(dsl_pool_t * dp,dmu_tx_t * tx)78010342Schris.kirby@sun.com dsl_pool_user_hold_create_obj(dsl_pool_t *dp, dmu_tx_t *tx)
78110342Schris.kirby@sun.com {
78210342Schris.kirby@sun.com objset_t *mos = dp->dp_meta_objset;
78310342Schris.kirby@sun.com
78410342Schris.kirby@sun.com ASSERT(dp->dp_tmp_userrefs_obj == 0);
78510342Schris.kirby@sun.com ASSERT(dmu_tx_is_syncing(tx));
78610342Schris.kirby@sun.com
78710342Schris.kirby@sun.com dp->dp_tmp_userrefs_obj = zap_create(mos, DMU_OT_USERREFS,
78810342Schris.kirby@sun.com DMU_OT_NONE, 0, tx);
78910342Schris.kirby@sun.com
79010342Schris.kirby@sun.com VERIFY(zap_add(mos, DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_TMP_USERREFS,
79110342Schris.kirby@sun.com sizeof (uint64_t), 1, &dp->dp_tmp_userrefs_obj, tx) == 0);
79210342Schris.kirby@sun.com }
79310342Schris.kirby@sun.com
79410342Schris.kirby@sun.com static int
dsl_pool_user_hold_rele_impl(dsl_pool_t * dp,uint64_t dsobj,const char * tag,uint64_t * now,dmu_tx_t * tx,boolean_t holding)79510342Schris.kirby@sun.com dsl_pool_user_hold_rele_impl(dsl_pool_t *dp, uint64_t dsobj,
79610951SChris.Kirby@sun.com const char *tag, uint64_t *now, dmu_tx_t *tx, boolean_t holding)
79710342Schris.kirby@sun.com {
79810342Schris.kirby@sun.com objset_t *mos = dp->dp_meta_objset;
79910342Schris.kirby@sun.com uint64_t zapobj = dp->dp_tmp_userrefs_obj;
80010342Schris.kirby@sun.com char *name;
80110342Schris.kirby@sun.com int error;
80210342Schris.kirby@sun.com
80310342Schris.kirby@sun.com ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
80410342Schris.kirby@sun.com ASSERT(dmu_tx_is_syncing(tx));
80510342Schris.kirby@sun.com
80610342Schris.kirby@sun.com /*
80710342Schris.kirby@sun.com * If the pool was created prior to SPA_VERSION_USERREFS, the
80810342Schris.kirby@sun.com * zap object for temporary holds might not exist yet.
80910342Schris.kirby@sun.com */
81010342Schris.kirby@sun.com if (zapobj == 0) {
81110342Schris.kirby@sun.com if (holding) {
81210342Schris.kirby@sun.com dsl_pool_user_hold_create_obj(dp, tx);
81310342Schris.kirby@sun.com zapobj = dp->dp_tmp_userrefs_obj;
81410342Schris.kirby@sun.com } else {
81510342Schris.kirby@sun.com return (ENOENT);
81610342Schris.kirby@sun.com }
81710342Schris.kirby@sun.com }
81810342Schris.kirby@sun.com
81910342Schris.kirby@sun.com name = kmem_asprintf("%llx-%s", (u_longlong_t)dsobj, tag);
82010342Schris.kirby@sun.com if (holding)
82110951SChris.Kirby@sun.com error = zap_add(mos, zapobj, name, 8, 1, now, tx);
82210342Schris.kirby@sun.com else
82310342Schris.kirby@sun.com error = zap_remove(mos, zapobj, name, tx);
82410342Schris.kirby@sun.com strfree(name);
82510342Schris.kirby@sun.com
82610342Schris.kirby@sun.com return (error);
82710342Schris.kirby@sun.com }
82810342Schris.kirby@sun.com
82910342Schris.kirby@sun.com /*
83010342Schris.kirby@sun.com * Add a temporary hold for the given dataset object and tag.
83110342Schris.kirby@sun.com */
83210342Schris.kirby@sun.com int
dsl_pool_user_hold(dsl_pool_t * dp,uint64_t dsobj,const char * tag,uint64_t * now,dmu_tx_t * tx)83310342Schris.kirby@sun.com dsl_pool_user_hold(dsl_pool_t *dp, uint64_t dsobj, const char *tag,
83410951SChris.Kirby@sun.com uint64_t *now, dmu_tx_t *tx)
83510342Schris.kirby@sun.com {
83610951SChris.Kirby@sun.com return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, now, tx, B_TRUE));
83710342Schris.kirby@sun.com }
83810342Schris.kirby@sun.com
83910342Schris.kirby@sun.com /*
84010342Schris.kirby@sun.com * Release a temporary hold for the given dataset object and tag.
84110342Schris.kirby@sun.com */
84210342Schris.kirby@sun.com int
dsl_pool_user_release(dsl_pool_t * dp,uint64_t dsobj,const char * tag,dmu_tx_t * tx)84310342Schris.kirby@sun.com dsl_pool_user_release(dsl_pool_t *dp, uint64_t dsobj, const char *tag,
84410342Schris.kirby@sun.com dmu_tx_t *tx)
84510342Schris.kirby@sun.com {
84610342Schris.kirby@sun.com return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, NULL,
84710342Schris.kirby@sun.com tx, B_FALSE));
84810342Schris.kirby@sun.com }
849