xref: /onnv-gate/usr/src/uts/common/fs/zfs/dsl_pool.c (revision 11935:538c866aaac6)
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 /*
2211614SLin.Ling@Sun.COM  * Copyright 2010 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 */
4211182SLin.Ling@Sun.COM int zfs_txg_synctime_ms = 5000;		/* target millisecs 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);
10810298SMatthew.Ahrens@Sun.COM 	err = dmu_objset_open_impl(spa, NULL, &dp->dp_meta_rootbp,
10910298SMatthew.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 
14510342Schris.kirby@sun.com 	err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
14610342Schris.kirby@sun.com 	    DMU_POOL_TMP_USERREFS, sizeof (uint64_t), 1,
14710342Schris.kirby@sun.com 	    &dp->dp_tmp_userrefs_obj);
14810342Schris.kirby@sun.com 	if (err == ENOENT)
14910342Schris.kirby@sun.com 		err = 0;
15010342Schris.kirby@sun.com 	if (err)
15110342Schris.kirby@sun.com 		goto out;
15210342Schris.kirby@sun.com 
1537046Sahrens 	/* get scrub status */
1547046Sahrens 	err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1557046Sahrens 	    DMU_POOL_SCRUB_FUNC, sizeof (uint32_t), 1,
1567046Sahrens 	    &dp->dp_scrub_func);
1577046Sahrens 	if (err == 0) {
1587046Sahrens 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1597046Sahrens 		    DMU_POOL_SCRUB_QUEUE, sizeof (uint64_t), 1,
1607046Sahrens 		    &dp->dp_scrub_queue_obj);
1617046Sahrens 		if (err)
1627046Sahrens 			goto out;
1637046Sahrens 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1647046Sahrens 		    DMU_POOL_SCRUB_MIN_TXG, sizeof (uint64_t), 1,
1657046Sahrens 		    &dp->dp_scrub_min_txg);
1667046Sahrens 		if (err)
1677046Sahrens 			goto out;
1687046Sahrens 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1697046Sahrens 		    DMU_POOL_SCRUB_MAX_TXG, sizeof (uint64_t), 1,
1707046Sahrens 		    &dp->dp_scrub_max_txg);
1717046Sahrens 		if (err)
1727046Sahrens 			goto out;
1737046Sahrens 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
17411125SJeff.Bonwick@Sun.COM 		    DMU_POOL_SCRUB_BOOKMARK, sizeof (uint64_t),
17511125SJeff.Bonwick@Sun.COM 		    sizeof (dp->dp_scrub_bookmark) / sizeof (uint64_t),
1767046Sahrens 		    &dp->dp_scrub_bookmark);
1777046Sahrens 		if (err)
1787046Sahrens 			goto out;
1797046Sahrens 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
18011125SJeff.Bonwick@Sun.COM 		    DMU_POOL_SCRUB_DDT_BOOKMARK, sizeof (uint64_t),
18111125SJeff.Bonwick@Sun.COM 		    sizeof (dp->dp_scrub_ddt_bookmark) / sizeof (uint64_t),
18211125SJeff.Bonwick@Sun.COM 		    &dp->dp_scrub_ddt_bookmark);
18311125SJeff.Bonwick@Sun.COM 		if (err && err != ENOENT)
18411125SJeff.Bonwick@Sun.COM 			goto out;
18511125SJeff.Bonwick@Sun.COM 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
18611125SJeff.Bonwick@Sun.COM 		    DMU_POOL_SCRUB_DDT_CLASS_MAX, sizeof (uint64_t), 1,
18711125SJeff.Bonwick@Sun.COM 		    &dp->dp_scrub_ddt_class_max);
18811125SJeff.Bonwick@Sun.COM 		if (err && err != ENOENT)
18911125SJeff.Bonwick@Sun.COM 			goto out;
19011125SJeff.Bonwick@Sun.COM 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1917046Sahrens 		    DMU_POOL_SCRUB_ERRORS, sizeof (uint64_t), 1,
1927046Sahrens 		    &spa->spa_scrub_errors);
1937046Sahrens 		if (err)
1947046Sahrens 			goto out;
1957046Sahrens 		if (spa_version(spa) < SPA_VERSION_DSL_SCRUB) {
1967046Sahrens 			/*
1977046Sahrens 			 * A new-type scrub was in progress on an old
1987046Sahrens 			 * pool.  Restart from the beginning, since the
1997046Sahrens 			 * old software may have changed the pool in the
2007046Sahrens 			 * meantime.
2017046Sahrens 			 */
2027046Sahrens 			dsl_pool_scrub_restart(dp);
2037046Sahrens 		}
2047046Sahrens 	} else {
2057046Sahrens 		/*
2067046Sahrens 		 * It's OK if there is no scrub in progress (and if
2077046Sahrens 		 * there was an I/O error, ignore it).
2087046Sahrens 		 */
2097046Sahrens 		err = 0;
2107046Sahrens 	}
2117046Sahrens 
2121544Seschrock out:
213789Sahrens 	rw_exit(&dp->dp_config_rwlock);
2141544Seschrock 	if (err)
2151544Seschrock 		dsl_pool_close(dp);
2161544Seschrock 	else
2171544Seschrock 		*dpp = dp;
218789Sahrens 
2191544Seschrock 	return (err);
220789Sahrens }
221789Sahrens 
222789Sahrens void
223789Sahrens dsl_pool_close(dsl_pool_t *dp)
224789Sahrens {
2257046Sahrens 	/* drop our references from dsl_pool_open() */
2267046Sahrens 
2277046Sahrens 	/*
2287046Sahrens 	 * Since we held the origin_snap from "syncing" context (which
2297046Sahrens 	 * includes pool-opening context), it actually only got a "ref"
2307046Sahrens 	 * and not a hold, so just drop that here.
2317046Sahrens 	 */
2327046Sahrens 	if (dp->dp_origin_snap)
2337046Sahrens 		dsl_dataset_drop_ref(dp->dp_origin_snap, dp);
2341544Seschrock 	if (dp->dp_mos_dir)
2351544Seschrock 		dsl_dir_close(dp->dp_mos_dir, dp);
2361544Seschrock 	if (dp->dp_root_dir)
2371544Seschrock 		dsl_dir_close(dp->dp_root_dir, dp);
238789Sahrens 
239789Sahrens 	/* undo the dmu_objset_open_impl(mos) from dsl_pool_open() */
2401544Seschrock 	if (dp->dp_meta_objset)
24110298SMatthew.Ahrens@Sun.COM 		dmu_objset_evict(dp->dp_meta_objset);
242789Sahrens 
243789Sahrens 	txg_list_destroy(&dp->dp_dirty_datasets);
24411814SChris.Kirby@sun.com 	txg_list_destroy(&dp->dp_sync_tasks);
245789Sahrens 	txg_list_destroy(&dp->dp_dirty_dirs);
2465367Sahrens 	list_destroy(&dp->dp_synced_datasets);
247789Sahrens 
2485642Smaybee 	arc_flush(dp->dp_spa);
249789Sahrens 	txg_fini(dp);
2502856Snd150628 	rw_destroy(&dp->dp_config_rwlock);
2516245Smaybee 	mutex_destroy(&dp->dp_lock);
2527046Sahrens 	mutex_destroy(&dp->dp_scrub_cancel_lock);
2539321SNeil.Perrin@Sun.COM 	taskq_destroy(dp->dp_vnrele_taskq);
2547837SMatthew.Ahrens@Sun.COM 	if (dp->dp_blkstats)
2557837SMatthew.Ahrens@Sun.COM 		kmem_free(dp->dp_blkstats, sizeof (zfs_all_blkstats_t));
256789Sahrens 	kmem_free(dp, sizeof (dsl_pool_t));
257789Sahrens }
258789Sahrens 
259789Sahrens dsl_pool_t *
2607184Stimh dsl_pool_create(spa_t *spa, nvlist_t *zplprops, uint64_t txg)
261789Sahrens {
262789Sahrens 	int err;
263789Sahrens 	dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
264789Sahrens 	dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg);
26510298SMatthew.Ahrens@Sun.COM 	objset_t *os;
2667046Sahrens 	dsl_dataset_t *ds;
2677046Sahrens 	uint64_t dsobj;
2687046Sahrens 
2697046Sahrens 	/* create and open the MOS (meta-objset) */
27010298SMatthew.Ahrens@Sun.COM 	dp->dp_meta_objset = dmu_objset_create_impl(spa,
27110298SMatthew.Ahrens@Sun.COM 	    NULL, &dp->dp_meta_rootbp, DMU_OST_META, tx);
272789Sahrens 
273789Sahrens 	/* create the pool directory */
274789Sahrens 	err = zap_create_claim(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
275789Sahrens 	    DMU_OT_OBJECT_DIRECTORY, DMU_OT_NONE, 0, tx);
276789Sahrens 	ASSERT3U(err, ==, 0);
277789Sahrens 
278789Sahrens 	/* create and open the root dir */
2797046Sahrens 	dp->dp_root_dir_obj = dsl_dir_create_sync(dp, NULL, NULL, tx);
2801544Seschrock 	VERIFY(0 == dsl_dir_open_obj(dp, dp->dp_root_dir_obj,
2811544Seschrock 	    NULL, dp, &dp->dp_root_dir));
282789Sahrens 
283789Sahrens 	/* create and open the meta-objset dir */
2847046Sahrens 	(void) dsl_dir_create_sync(dp, dp->dp_root_dir, MOS_DIR_NAME, tx);
2857046Sahrens 	VERIFY(0 == dsl_pool_open_special_dir(dp,
2867046Sahrens 	    MOS_DIR_NAME, &dp->dp_mos_dir));
2877046Sahrens 
2887046Sahrens 	if (spa_version(spa) >= SPA_VERSION_DSL_SCRUB)
2897046Sahrens 		dsl_pool_create_origin(dp, tx);
2907046Sahrens 
2917046Sahrens 	/* create the root dataset */
2927046Sahrens 	dsobj = dsl_dataset_create_sync_dd(dp->dp_root_dir, NULL, 0, tx);
2937046Sahrens 
2947046Sahrens 	/* create the root objset */
2957046Sahrens 	VERIFY(0 == dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
29610298SMatthew.Ahrens@Sun.COM 	os = dmu_objset_create_impl(dp->dp_spa, ds,
2977046Sahrens 	    dsl_dataset_get_blkptr(ds), DMU_OST_ZFS, tx);
2987046Sahrens #ifdef _KERNEL
29910298SMatthew.Ahrens@Sun.COM 	zfs_create_fs(os, kcred, zplprops, tx);
3007046Sahrens #endif
3017046Sahrens 	dsl_dataset_rele(ds, FTAG);
302789Sahrens 
303789Sahrens 	dmu_tx_commit(tx);
304789Sahrens 
305789Sahrens 	return (dp);
306789Sahrens }
307789Sahrens 
308789Sahrens void
309789Sahrens dsl_pool_sync(dsl_pool_t *dp, uint64_t txg)
310789Sahrens {
3113547Smaybee 	zio_t *zio;
312789Sahrens 	dmu_tx_t *tx;
3133547Smaybee 	dsl_dir_t *dd;
3143547Smaybee 	dsl_dataset_t *ds;
3153547Smaybee 	dsl_sync_task_group_t *dstg;
31610298SMatthew.Ahrens@Sun.COM 	objset_t *mos = dp->dp_meta_objset;
3177468SMark.Maybee@Sun.COM 	hrtime_t start, write_time;
3187468SMark.Maybee@Sun.COM 	uint64_t data_written;
3193547Smaybee 	int err;
320789Sahrens 
321789Sahrens 	tx = dmu_tx_create_assigned(dp, txg);
322789Sahrens 
3237468SMark.Maybee@Sun.COM 	dp->dp_read_overhead = 0;
3249366SMark.Maybee@Sun.COM 	start = gethrtime();
3259396SMatthew.Ahrens@Sun.COM 
3263547Smaybee 	zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
3273547Smaybee 	while (ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) {
3289396SMatthew.Ahrens@Sun.COM 		/*
3299396SMatthew.Ahrens@Sun.COM 		 * We must not sync any non-MOS datasets twice, because
3309396SMatthew.Ahrens@Sun.COM 		 * we may have taken a snapshot of them.  However, we
3319396SMatthew.Ahrens@Sun.COM 		 * may sync newly-created datasets on pass 2.
3329396SMatthew.Ahrens@Sun.COM 		 */
3339396SMatthew.Ahrens@Sun.COM 		ASSERT(!list_link_active(&ds->ds_synced_link));
3349396SMatthew.Ahrens@Sun.COM 		list_insert_tail(&dp->dp_synced_datasets, ds);
3353547Smaybee 		dsl_dataset_sync(ds, zio, tx);
3363547Smaybee 	}
3377468SMark.Maybee@Sun.COM 	DTRACE_PROBE(pool_sync__1setup);
3389396SMatthew.Ahrens@Sun.COM 	err = zio_wait(zio);
3397468SMark.Maybee@Sun.COM 
3407468SMark.Maybee@Sun.COM 	write_time = gethrtime() - start;
3413547Smaybee 	ASSERT(err == 0);
3427468SMark.Maybee@Sun.COM 	DTRACE_PROBE(pool_sync__2rootzio);
343789Sahrens 
3449396SMatthew.Ahrens@Sun.COM 	for (ds = list_head(&dp->dp_synced_datasets); ds;
3459396SMatthew.Ahrens@Sun.COM 	    ds = list_next(&dp->dp_synced_datasets, ds))
346*11935SMark.Shellenbaum@Sun.COM 		dmu_objset_do_userquota_updates(ds->ds_objset, tx);
3479396SMatthew.Ahrens@Sun.COM 
3489396SMatthew.Ahrens@Sun.COM 	/*
3499396SMatthew.Ahrens@Sun.COM 	 * Sync the datasets again to push out the changes due to
3509396SMatthew.Ahrens@Sun.COM 	 * userquota updates.  This must be done before we process the
3519396SMatthew.Ahrens@Sun.COM 	 * sync tasks, because that could cause a snapshot of a dataset
3529396SMatthew.Ahrens@Sun.COM 	 * whose ds_bp will be rewritten when we do this 2nd sync.
3539396SMatthew.Ahrens@Sun.COM 	 */
3549396SMatthew.Ahrens@Sun.COM 	zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
3559396SMatthew.Ahrens@Sun.COM 	while (ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) {
3569396SMatthew.Ahrens@Sun.COM 		ASSERT(list_link_active(&ds->ds_synced_link));
3579396SMatthew.Ahrens@Sun.COM 		dmu_buf_rele(ds->ds_dbuf, ds);
3589396SMatthew.Ahrens@Sun.COM 		dsl_dataset_sync(ds, zio, tx);
3599396SMatthew.Ahrens@Sun.COM 	}
3609396SMatthew.Ahrens@Sun.COM 	err = zio_wait(zio);
3619396SMatthew.Ahrens@Sun.COM 
36210922SJeff.Bonwick@Sun.COM 	/*
36310922SJeff.Bonwick@Sun.COM 	 * If anything was added to a deadlist during a zio done callback,
36410922SJeff.Bonwick@Sun.COM 	 * it had to be put on the deferred queue.  Enqueue it for real now.
36510922SJeff.Bonwick@Sun.COM 	 */
36610922SJeff.Bonwick@Sun.COM 	for (ds = list_head(&dp->dp_synced_datasets); ds;
36710922SJeff.Bonwick@Sun.COM 	    ds = list_next(&dp->dp_synced_datasets, ds))
36810922SJeff.Bonwick@Sun.COM 		bplist_sync(&ds->ds_deadlist,
36910922SJeff.Bonwick@Sun.COM 		    bplist_enqueue_cb, &ds->ds_deadlist, tx);
37010922SJeff.Bonwick@Sun.COM 
3719396SMatthew.Ahrens@Sun.COM 	while (dstg = txg_list_remove(&dp->dp_sync_tasks, txg)) {
3729396SMatthew.Ahrens@Sun.COM 		/*
3739396SMatthew.Ahrens@Sun.COM 		 * No more sync tasks should have been added while we
3749396SMatthew.Ahrens@Sun.COM 		 * were syncing.
3759396SMatthew.Ahrens@Sun.COM 		 */
3769396SMatthew.Ahrens@Sun.COM 		ASSERT(spa_sync_pass(dp->dp_spa) == 1);
3773547Smaybee 		dsl_sync_task_group_sync(dstg, tx);
3789396SMatthew.Ahrens@Sun.COM 	}
3797468SMark.Maybee@Sun.COM 	DTRACE_PROBE(pool_sync__3task);
3807468SMark.Maybee@Sun.COM 
3817468SMark.Maybee@Sun.COM 	start = gethrtime();
3823547Smaybee 	while (dd = txg_list_remove(&dp->dp_dirty_dirs, txg))
3833547Smaybee 		dsl_dir_sync(dd, tx);
3847468SMark.Maybee@Sun.COM 	write_time += gethrtime() - start;
385789Sahrens 
38611147SGeorge.Wilson@Sun.COM 	if (spa_sync_pass(dp->dp_spa) == 1) {
38711147SGeorge.Wilson@Sun.COM 		dp->dp_scrub_prefetch_zio_root = zio_root(dp->dp_spa, NULL,
38811147SGeorge.Wilson@Sun.COM 		    NULL, ZIO_FLAG_CANFAIL);
3897046Sahrens 		dsl_pool_scrub_sync(dp, tx);
39011147SGeorge.Wilson@Sun.COM 		(void) zio_wait(dp->dp_scrub_prefetch_zio_root);
39111147SGeorge.Wilson@Sun.COM 	}
3927046Sahrens 
3937468SMark.Maybee@Sun.COM 	start = gethrtime();
39410298SMatthew.Ahrens@Sun.COM 	if (list_head(&mos->os_dirty_dnodes[txg & TXG_MASK]) != NULL ||
39510298SMatthew.Ahrens@Sun.COM 	    list_head(&mos->os_free_dnodes[txg & TXG_MASK]) != NULL) {
3963547Smaybee 		zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
39710298SMatthew.Ahrens@Sun.COM 		dmu_objset_sync(mos, zio, tx);
3983547Smaybee 		err = zio_wait(zio);
3993547Smaybee 		ASSERT(err == 0);
400789Sahrens 		dprintf_bp(&dp->dp_meta_rootbp, "meta objset rootbp is %s", "");
401789Sahrens 		spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
402789Sahrens 	}
4037468SMark.Maybee@Sun.COM 	write_time += gethrtime() - start;
4047468SMark.Maybee@Sun.COM 	DTRACE_PROBE2(pool_sync__4io, hrtime_t, write_time,
4057468SMark.Maybee@Sun.COM 	    hrtime_t, dp->dp_read_overhead);
4067468SMark.Maybee@Sun.COM 	write_time -= dp->dp_read_overhead;
407789Sahrens 
408789Sahrens 	dmu_tx_commit(tx);
4097468SMark.Maybee@Sun.COM 
4107468SMark.Maybee@Sun.COM 	data_written = dp->dp_space_towrite[txg & TXG_MASK];
4117468SMark.Maybee@Sun.COM 	dp->dp_space_towrite[txg & TXG_MASK] = 0;
4127468SMark.Maybee@Sun.COM 	ASSERT(dp->dp_tempreserved[txg & TXG_MASK] == 0);
4137468SMark.Maybee@Sun.COM 
4147468SMark.Maybee@Sun.COM 	/*
4157468SMark.Maybee@Sun.COM 	 * If the write limit max has not been explicitly set, set it
4167468SMark.Maybee@Sun.COM 	 * to a fraction of available physical memory (default 1/8th).
4177468SMark.Maybee@Sun.COM 	 * Note that we must inflate the limit because the spa
4187468SMark.Maybee@Sun.COM 	 * inflates write sizes to account for data replication.
4197468SMark.Maybee@Sun.COM 	 * Check this each sync phase to catch changing memory size.
4207468SMark.Maybee@Sun.COM 	 */
4217468SMark.Maybee@Sun.COM 	if (physmem != old_physmem && zfs_write_limit_shift) {
4227468SMark.Maybee@Sun.COM 		mutex_enter(&zfs_write_limit_lock);
4237468SMark.Maybee@Sun.COM 		old_physmem = physmem;
4247468SMark.Maybee@Sun.COM 		zfs_write_limit_max = ptob(physmem) >> zfs_write_limit_shift;
4257468SMark.Maybee@Sun.COM 		zfs_write_limit_inflated = MAX(zfs_write_limit_min,
4267468SMark.Maybee@Sun.COM 		    spa_get_asize(dp->dp_spa, zfs_write_limit_max));
4277468SMark.Maybee@Sun.COM 		mutex_exit(&zfs_write_limit_lock);
4287468SMark.Maybee@Sun.COM 	}
4297468SMark.Maybee@Sun.COM 
4307468SMark.Maybee@Sun.COM 	/*
4317468SMark.Maybee@Sun.COM 	 * Attempt to keep the sync time consistent by adjusting the
4327468SMark.Maybee@Sun.COM 	 * amount of write traffic allowed into each transaction group.
4337468SMark.Maybee@Sun.COM 	 * Weight the throughput calculation towards the current value:
4347468SMark.Maybee@Sun.COM 	 * 	thru = 3/4 old_thru + 1/4 new_thru
43511614SLin.Ling@Sun.COM 	 *
43611614SLin.Ling@Sun.COM 	 * Note: write_time is in nanosecs, so write_time/MICROSEC
43711614SLin.Ling@Sun.COM 	 * yields millisecs
4387468SMark.Maybee@Sun.COM 	 */
4397468SMark.Maybee@Sun.COM 	ASSERT(zfs_write_limit_min > 0);
44011614SLin.Ling@Sun.COM 	if (data_written > zfs_write_limit_min / 8 && write_time > MICROSEC) {
44111614SLin.Ling@Sun.COM 		uint64_t throughput = data_written / (write_time / MICROSEC);
44211614SLin.Ling@Sun.COM 
4437468SMark.Maybee@Sun.COM 		if (dp->dp_throughput)
4447468SMark.Maybee@Sun.COM 			dp->dp_throughput = throughput / 4 +
4457468SMark.Maybee@Sun.COM 			    3 * dp->dp_throughput / 4;
4467468SMark.Maybee@Sun.COM 		else
4477468SMark.Maybee@Sun.COM 			dp->dp_throughput = throughput;
4487468SMark.Maybee@Sun.COM 		dp->dp_write_limit = MIN(zfs_write_limit_inflated,
4497468SMark.Maybee@Sun.COM 		    MAX(zfs_write_limit_min,
45011614SLin.Ling@Sun.COM 		    dp->dp_throughput * zfs_txg_synctime_ms));
4517468SMark.Maybee@Sun.COM 	}
452789Sahrens }
453789Sahrens 
454789Sahrens void
45510922SJeff.Bonwick@Sun.COM dsl_pool_sync_done(dsl_pool_t *dp, uint64_t txg)
456789Sahrens {
457789Sahrens 	dsl_dataset_t *ds;
45810922SJeff.Bonwick@Sun.COM 	objset_t *os;
459789Sahrens 
4605367Sahrens 	while (ds = list_head(&dp->dp_synced_datasets)) {
4615367Sahrens 		list_remove(&dp->dp_synced_datasets, ds);
46210922SJeff.Bonwick@Sun.COM 		os = ds->ds_objset;
46310922SJeff.Bonwick@Sun.COM 		zil_clean(os->os_zil);
46410922SJeff.Bonwick@Sun.COM 		ASSERT(!dmu_objset_is_dirty(os, txg));
4653897Smaybee 		dmu_buf_rele(ds->ds_dbuf, ds);
466789Sahrens 	}
46710922SJeff.Bonwick@Sun.COM 	ASSERT(!dmu_objset_is_dirty(dp->dp_meta_objset, txg));
468789Sahrens }
469789Sahrens 
4703547Smaybee /*
4713547Smaybee  * TRUE if the current thread is the tx_sync_thread or if we
4723547Smaybee  * are being called from SPA context during pool initialization.
4733547Smaybee  */
474789Sahrens int
475789Sahrens dsl_pool_sync_context(dsl_pool_t *dp)
476789Sahrens {
477789Sahrens 	return (curthread == dp->dp_tx.tx_sync_thread ||
4783547Smaybee 	    spa_get_dsl(dp->dp_spa) == NULL);
479789Sahrens }
480789Sahrens 
481789Sahrens uint64_t
482789Sahrens dsl_pool_adjustedsize(dsl_pool_t *dp, boolean_t netfree)
483789Sahrens {
484789Sahrens 	uint64_t space, resv;
485789Sahrens 
486789Sahrens 	/*
4871775Sbillm 	 * Reserve about 1.6% (1/64), or at least 32MB, for allocation
488789Sahrens 	 * efficiency.
489789Sahrens 	 * XXX The intent log is not accounted for, so it must fit
490789Sahrens 	 * within this slop.
491789Sahrens 	 *
492789Sahrens 	 * If we're trying to assess whether it's OK to do a free,
493789Sahrens 	 * cut the reservation in half to allow forward progress
494789Sahrens 	 * (e.g. make it possible to rm(1) files from a full pool).
495789Sahrens 	 */
49610956SGeorge.Wilson@Sun.COM 	space = spa_get_dspace(dp->dp_spa);
4971775Sbillm 	resv = MAX(space >> 6, SPA_MINDEVSIZE >> 1);
498789Sahrens 	if (netfree)
499789Sahrens 		resv >>= 1;
500789Sahrens 
501789Sahrens 	return (space - resv);
502789Sahrens }
5036245Smaybee 
5046245Smaybee int
5056245Smaybee dsl_pool_tempreserve_space(dsl_pool_t *dp, uint64_t space, dmu_tx_t *tx)
5066245Smaybee {
5076245Smaybee 	uint64_t reserved = 0;
5086245Smaybee 	uint64_t write_limit = (zfs_write_limit_override ?
5096245Smaybee 	    zfs_write_limit_override : dp->dp_write_limit);
5106245Smaybee 
5116245Smaybee 	if (zfs_no_write_throttle) {
5126643Seschrock 		atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK],
5136643Seschrock 		    space);
5146245Smaybee 		return (0);
5156245Smaybee 	}
5166245Smaybee 
5176245Smaybee 	/*
5186245Smaybee 	 * Check to see if we have exceeded the maximum allowed IO for
5196245Smaybee 	 * this transaction group.  We can do this without locks since
5206245Smaybee 	 * a little slop here is ok.  Note that we do the reserved check
5216245Smaybee 	 * with only half the requested reserve: this is because the
5226245Smaybee 	 * reserve requests are worst-case, and we really don't want to
5236245Smaybee 	 * throttle based off of worst-case estimates.
5246245Smaybee 	 */
5256245Smaybee 	if (write_limit > 0) {
5266245Smaybee 		reserved = dp->dp_space_towrite[tx->tx_txg & TXG_MASK]
5276245Smaybee 		    + dp->dp_tempreserved[tx->tx_txg & TXG_MASK] / 2;
5286245Smaybee 
5296245Smaybee 		if (reserved && reserved > write_limit)
5306245Smaybee 			return (ERESTART);
5316245Smaybee 	}
5326245Smaybee 
5336245Smaybee 	atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], space);
5346245Smaybee 
5356245Smaybee 	/*
5366245Smaybee 	 * If this transaction group is over 7/8ths capacity, delay
5376245Smaybee 	 * the caller 1 clock tick.  This will slow down the "fill"
5386245Smaybee 	 * rate until the sync process can catch up with us.
5396245Smaybee 	 */
5406740Sgw25295 	if (reserved && reserved > (write_limit - (write_limit >> 3)))
5416245Smaybee 		txg_delay(dp, tx->tx_txg, 1);
5426245Smaybee 
5436245Smaybee 	return (0);
5446245Smaybee }
5456245Smaybee 
5466245Smaybee void
5476245Smaybee dsl_pool_tempreserve_clear(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx)
5486245Smaybee {
5496245Smaybee 	ASSERT(dp->dp_tempreserved[tx->tx_txg & TXG_MASK] >= space);
5506245Smaybee 	atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], -space);
5516245Smaybee }
5526245Smaybee 
5536245Smaybee void
5546245Smaybee dsl_pool_memory_pressure(dsl_pool_t *dp)
5556245Smaybee {
5566245Smaybee 	uint64_t space_inuse = 0;
5576245Smaybee 	int i;
5586245Smaybee 
5596245Smaybee 	if (dp->dp_write_limit == zfs_write_limit_min)
5606245Smaybee 		return;
5616245Smaybee 
5626245Smaybee 	for (i = 0; i < TXG_SIZE; i++) {
5636245Smaybee 		space_inuse += dp->dp_space_towrite[i];
5646245Smaybee 		space_inuse += dp->dp_tempreserved[i];
5656245Smaybee 	}
5666245Smaybee 	dp->dp_write_limit = MAX(zfs_write_limit_min,
5676245Smaybee 	    MIN(dp->dp_write_limit, space_inuse / 4));
5686245Smaybee }
5696245Smaybee 
5706245Smaybee void
5716245Smaybee dsl_pool_willuse_space(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx)
5726245Smaybee {
5736245Smaybee 	if (space > 0) {
5746245Smaybee 		mutex_enter(&dp->dp_lock);
5756245Smaybee 		dp->dp_space_towrite[tx->tx_txg & TXG_MASK] += space;
5766245Smaybee 		mutex_exit(&dp->dp_lock);
5776245Smaybee 	}
5786245Smaybee }
5797046Sahrens 
5807046Sahrens /* ARGSUSED */
5817046Sahrens static int
5827046Sahrens upgrade_clones_cb(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
5837046Sahrens {
5847046Sahrens 	dmu_tx_t *tx = arg;
5857046Sahrens 	dsl_dataset_t *ds, *prev = NULL;
5867046Sahrens 	int err;
5877046Sahrens 	dsl_pool_t *dp = spa_get_dsl(spa);
5887046Sahrens 
5897046Sahrens 	err = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds);
5907046Sahrens 	if (err)
5917046Sahrens 		return (err);
5927046Sahrens 
5937046Sahrens 	while (ds->ds_phys->ds_prev_snap_obj != 0) {
5947046Sahrens 		err = dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
5957046Sahrens 		    FTAG, &prev);
5967046Sahrens 		if (err) {
5977046Sahrens 			dsl_dataset_rele(ds, FTAG);
5987046Sahrens 			return (err);
5997046Sahrens 		}
6007046Sahrens 
6017046Sahrens 		if (prev->ds_phys->ds_next_snap_obj != ds->ds_object)
6027046Sahrens 			break;
6037046Sahrens 		dsl_dataset_rele(ds, FTAG);
6047046Sahrens 		ds = prev;
6057046Sahrens 		prev = NULL;
6067046Sahrens 	}
6077046Sahrens 
6087046Sahrens 	if (prev == NULL) {
6097046Sahrens 		prev = dp->dp_origin_snap;
6107046Sahrens 
6117046Sahrens 		/*
6127046Sahrens 		 * The $ORIGIN can't have any data, or the accounting
6137046Sahrens 		 * will be wrong.
6147046Sahrens 		 */
6157046Sahrens 		ASSERT(prev->ds_phys->ds_bp.blk_birth == 0);
6167046Sahrens 
6177046Sahrens 		/* The origin doesn't get attached to itself */
6187046Sahrens 		if (ds->ds_object == prev->ds_object) {
6197046Sahrens 			dsl_dataset_rele(ds, FTAG);
6207046Sahrens 			return (0);
6217046Sahrens 		}
6227046Sahrens 
6237046Sahrens 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
6247046Sahrens 		ds->ds_phys->ds_prev_snap_obj = prev->ds_object;
6257046Sahrens 		ds->ds_phys->ds_prev_snap_txg = prev->ds_phys->ds_creation_txg;
6267046Sahrens 
6277046Sahrens 		dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
6287046Sahrens 		ds->ds_dir->dd_phys->dd_origin_obj = prev->ds_object;
6297046Sahrens 
6307046Sahrens 		dmu_buf_will_dirty(prev->ds_dbuf, tx);
6317046Sahrens 		prev->ds_phys->ds_num_children++;
6327046Sahrens 
6337046Sahrens 		if (ds->ds_phys->ds_next_snap_obj == 0) {
6347046Sahrens 			ASSERT(ds->ds_prev == NULL);
6357046Sahrens 			VERIFY(0 == dsl_dataset_hold_obj(dp,
6367046Sahrens 			    ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev));
6377046Sahrens 		}
6387046Sahrens 	}
6397046Sahrens 
6407046Sahrens 	ASSERT(ds->ds_dir->dd_phys->dd_origin_obj == prev->ds_object);
6417046Sahrens 	ASSERT(ds->ds_phys->ds_prev_snap_obj == prev->ds_object);
6427046Sahrens 
6437046Sahrens 	if (prev->ds_phys->ds_next_clones_obj == 0) {
64410801SMatthew.Ahrens@Sun.COM 		dmu_buf_will_dirty(prev->ds_dbuf, tx);
6457046Sahrens 		prev->ds_phys->ds_next_clones_obj =
6467046Sahrens 		    zap_create(dp->dp_meta_objset,
6477046Sahrens 		    DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
6487046Sahrens 	}
6497046Sahrens 	VERIFY(0 == zap_add_int(dp->dp_meta_objset,
6507046Sahrens 	    prev->ds_phys->ds_next_clones_obj, ds->ds_object, tx));
6517046Sahrens 
6527046Sahrens 	dsl_dataset_rele(ds, FTAG);
6537046Sahrens 	if (prev != dp->dp_origin_snap)
6547046Sahrens 		dsl_dataset_rele(prev, FTAG);
6557046Sahrens 	return (0);
6567046Sahrens }
6577046Sahrens 
6587046Sahrens void
6597046Sahrens dsl_pool_upgrade_clones(dsl_pool_t *dp, dmu_tx_t *tx)
6607046Sahrens {
6617046Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
6627046Sahrens 	ASSERT(dp->dp_origin_snap != NULL);
6637046Sahrens 
66410801SMatthew.Ahrens@Sun.COM 	VERIFY3U(0, ==, dmu_objset_find_spa(dp->dp_spa, NULL, upgrade_clones_cb,
66510801SMatthew.Ahrens@Sun.COM 	    tx, DS_FIND_CHILDREN));
6667046Sahrens }
6677046Sahrens 
6687046Sahrens void
6697046Sahrens dsl_pool_create_origin(dsl_pool_t *dp, dmu_tx_t *tx)
6707046Sahrens {
6717046Sahrens 	uint64_t dsobj;
6727046Sahrens 	dsl_dataset_t *ds;
6737046Sahrens 
6747046Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
6757046Sahrens 	ASSERT(dp->dp_origin_snap == NULL);
6767046Sahrens 
6777046Sahrens 	/* create the origin dir, ds, & snap-ds */
6787046Sahrens 	rw_enter(&dp->dp_config_rwlock, RW_WRITER);
6797046Sahrens 	dsobj = dsl_dataset_create_sync(dp->dp_root_dir, ORIGIN_DIR_NAME,
6807046Sahrens 	    NULL, 0, kcred, tx);
6817046Sahrens 	VERIFY(0 == dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
6827046Sahrens 	dsl_dataset_snapshot_sync(ds, ORIGIN_DIR_NAME, kcred, tx);
6837046Sahrens 	VERIFY(0 == dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
6847046Sahrens 	    dp, &dp->dp_origin_snap));
6857046Sahrens 	dsl_dataset_rele(ds, FTAG);
6867046Sahrens 	rw_exit(&dp->dp_config_rwlock);
6877046Sahrens }
6889321SNeil.Perrin@Sun.COM 
6899321SNeil.Perrin@Sun.COM taskq_t *
6909321SNeil.Perrin@Sun.COM dsl_pool_vnrele_taskq(dsl_pool_t *dp)
6919321SNeil.Perrin@Sun.COM {
6929321SNeil.Perrin@Sun.COM 	return (dp->dp_vnrele_taskq);
6939321SNeil.Perrin@Sun.COM }
69410342Schris.kirby@sun.com 
69510342Schris.kirby@sun.com /*
69610342Schris.kirby@sun.com  * Walk through the pool-wide zap object of temporary snapshot user holds
69710342Schris.kirby@sun.com  * and release them.
69810342Schris.kirby@sun.com  */
69910342Schris.kirby@sun.com void
70010342Schris.kirby@sun.com dsl_pool_clean_tmp_userrefs(dsl_pool_t *dp)
70110342Schris.kirby@sun.com {
70210342Schris.kirby@sun.com 	zap_attribute_t za;
70310342Schris.kirby@sun.com 	zap_cursor_t zc;
70410342Schris.kirby@sun.com 	objset_t *mos = dp->dp_meta_objset;
70510342Schris.kirby@sun.com 	uint64_t zapobj = dp->dp_tmp_userrefs_obj;
70610342Schris.kirby@sun.com 
70710342Schris.kirby@sun.com 	if (zapobj == 0)
70810342Schris.kirby@sun.com 		return;
70910342Schris.kirby@sun.com 	ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
71010342Schris.kirby@sun.com 
71110342Schris.kirby@sun.com 	for (zap_cursor_init(&zc, mos, zapobj);
71210342Schris.kirby@sun.com 	    zap_cursor_retrieve(&zc, &za) == 0;
71310342Schris.kirby@sun.com 	    zap_cursor_advance(&zc)) {
71410342Schris.kirby@sun.com 		char *htag;
71510342Schris.kirby@sun.com 		uint64_t dsobj;
71610342Schris.kirby@sun.com 
71710342Schris.kirby@sun.com 		htag = strchr(za.za_name, '-');
71810342Schris.kirby@sun.com 		*htag = '\0';
71910342Schris.kirby@sun.com 		++htag;
72010342Schris.kirby@sun.com 		dsobj = strtonum(za.za_name, NULL);
72110342Schris.kirby@sun.com 		(void) dsl_dataset_user_release_tmp(dp, dsobj, htag);
72210342Schris.kirby@sun.com 	}
72310342Schris.kirby@sun.com 	zap_cursor_fini(&zc);
72410342Schris.kirby@sun.com }
72510342Schris.kirby@sun.com 
72610342Schris.kirby@sun.com /*
72710342Schris.kirby@sun.com  * Create the pool-wide zap object for storing temporary snapshot holds.
72810342Schris.kirby@sun.com  */
72910342Schris.kirby@sun.com void
73010342Schris.kirby@sun.com dsl_pool_user_hold_create_obj(dsl_pool_t *dp, dmu_tx_t *tx)
73110342Schris.kirby@sun.com {
73210342Schris.kirby@sun.com 	objset_t *mos = dp->dp_meta_objset;
73310342Schris.kirby@sun.com 
73410342Schris.kirby@sun.com 	ASSERT(dp->dp_tmp_userrefs_obj == 0);
73510342Schris.kirby@sun.com 	ASSERT(dmu_tx_is_syncing(tx));
73610342Schris.kirby@sun.com 
73710342Schris.kirby@sun.com 	dp->dp_tmp_userrefs_obj = zap_create(mos, DMU_OT_USERREFS,
73810342Schris.kirby@sun.com 	    DMU_OT_NONE, 0, tx);
73910342Schris.kirby@sun.com 
74010342Schris.kirby@sun.com 	VERIFY(zap_add(mos, DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_TMP_USERREFS,
74110342Schris.kirby@sun.com 	    sizeof (uint64_t), 1, &dp->dp_tmp_userrefs_obj, tx) == 0);
74210342Schris.kirby@sun.com }
74310342Schris.kirby@sun.com 
74410342Schris.kirby@sun.com static int
74510342Schris.kirby@sun.com dsl_pool_user_hold_rele_impl(dsl_pool_t *dp, uint64_t dsobj,
74610951SChris.Kirby@sun.com     const char *tag, uint64_t *now, dmu_tx_t *tx, boolean_t holding)
74710342Schris.kirby@sun.com {
74810342Schris.kirby@sun.com 	objset_t *mos = dp->dp_meta_objset;
74910342Schris.kirby@sun.com 	uint64_t zapobj = dp->dp_tmp_userrefs_obj;
75010342Schris.kirby@sun.com 	char *name;
75110342Schris.kirby@sun.com 	int error;
75210342Schris.kirby@sun.com 
75310342Schris.kirby@sun.com 	ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
75410342Schris.kirby@sun.com 	ASSERT(dmu_tx_is_syncing(tx));
75510342Schris.kirby@sun.com 
75610342Schris.kirby@sun.com 	/*
75710342Schris.kirby@sun.com 	 * If the pool was created prior to SPA_VERSION_USERREFS, the
75810342Schris.kirby@sun.com 	 * zap object for temporary holds might not exist yet.
75910342Schris.kirby@sun.com 	 */
76010342Schris.kirby@sun.com 	if (zapobj == 0) {
76110342Schris.kirby@sun.com 		if (holding) {
76210342Schris.kirby@sun.com 			dsl_pool_user_hold_create_obj(dp, tx);
76310342Schris.kirby@sun.com 			zapobj = dp->dp_tmp_userrefs_obj;
76410342Schris.kirby@sun.com 		} else {
76510342Schris.kirby@sun.com 			return (ENOENT);
76610342Schris.kirby@sun.com 		}
76710342Schris.kirby@sun.com 	}
76810342Schris.kirby@sun.com 
76910342Schris.kirby@sun.com 	name = kmem_asprintf("%llx-%s", (u_longlong_t)dsobj, tag);
77010342Schris.kirby@sun.com 	if (holding)
77110951SChris.Kirby@sun.com 		error = zap_add(mos, zapobj, name, 8, 1, now, tx);
77210342Schris.kirby@sun.com 	else
77310342Schris.kirby@sun.com 		error = zap_remove(mos, zapobj, name, tx);
77410342Schris.kirby@sun.com 	strfree(name);
77510342Schris.kirby@sun.com 
77610342Schris.kirby@sun.com 	return (error);
77710342Schris.kirby@sun.com }
77810342Schris.kirby@sun.com 
77910342Schris.kirby@sun.com /*
78010342Schris.kirby@sun.com  * Add a temporary hold for the given dataset object and tag.
78110342Schris.kirby@sun.com  */
78210342Schris.kirby@sun.com int
78310342Schris.kirby@sun.com dsl_pool_user_hold(dsl_pool_t *dp, uint64_t dsobj, const char *tag,
78410951SChris.Kirby@sun.com     uint64_t *now, dmu_tx_t *tx)
78510342Schris.kirby@sun.com {
78610951SChris.Kirby@sun.com 	return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, now, tx, B_TRUE));
78710342Schris.kirby@sun.com }
78810342Schris.kirby@sun.com 
78910342Schris.kirby@sun.com /*
79010342Schris.kirby@sun.com  * Release a temporary hold for the given dataset object and tag.
79110342Schris.kirby@sun.com  */
79210342Schris.kirby@sun.com int
79310342Schris.kirby@sun.com dsl_pool_user_release(dsl_pool_t *dp, uint64_t dsobj, const char *tag,
79410342Schris.kirby@sun.com     dmu_tx_t *tx)
79510342Schris.kirby@sun.com {
79610342Schris.kirby@sun.com 	return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, NULL,
79710342Schris.kirby@sun.com 	    tx, B_FALSE));
79810342Schris.kirby@sun.com }
799