xref: /illumos-gate/usr/src/uts/common/fs/zfs/dsl_pool.c (revision 3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
22*3f9d6ad7SLin Ling  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23fa9e4066Sahrens  */
24fa9e4066Sahrens 
25fa9e4066Sahrens #include <sys/dsl_pool.h>
26fa9e4066Sahrens #include <sys/dsl_dataset.h>
27*3f9d6ad7SLin Ling #include <sys/dsl_prop.h>
28fa9e4066Sahrens #include <sys/dsl_dir.h>
291d452cf5Sahrens #include <sys/dsl_synctask.h>
30*3f9d6ad7SLin Ling #include <sys/dsl_scan.h>
31*3f9d6ad7SLin Ling #include <sys/dnode.h>
32fa9e4066Sahrens #include <sys/dmu_tx.h>
33fa9e4066Sahrens #include <sys/dmu_objset.h>
34fa9e4066Sahrens #include <sys/arc.h>
35fa9e4066Sahrens #include <sys/zap.h>
36c717a561Smaybee #include <sys/zio.h>
37fa9e4066Sahrens #include <sys/zfs_context.h>
38fa9e4066Sahrens #include <sys/fs/zfs.h>
39088f3894Sahrens #include <sys/zfs_znode.h>
40088f3894Sahrens #include <sys/spa_impl.h>
41fa9e4066Sahrens 
421ab7f2deSmaybee int zfs_no_write_throttle = 0;
4305715f94SMark Maybee int zfs_write_limit_shift = 3;			/* 1/8th of physical memory */
44bf0ec83aSLin Ling int zfs_txg_synctime_ms = 5000;		/* target millisecs to sync a txg */
4505715f94SMark Maybee 
4605715f94SMark Maybee uint64_t zfs_write_limit_min = 32 << 20;	/* min write limit is 32MB */
4705715f94SMark Maybee uint64_t zfs_write_limit_max = 0;		/* max data payload per txg */
4805715f94SMark Maybee uint64_t zfs_write_limit_inflated = 0;
491ab7f2deSmaybee uint64_t zfs_write_limit_override = 0;
501ab7f2deSmaybee 
5105715f94SMark Maybee kmutex_t zfs_write_limit_lock;
5205715f94SMark Maybee 
5305715f94SMark Maybee static pgcnt_t old_physmem = 0;
54088f3894Sahrens 
55*3f9d6ad7SLin Ling int
56088f3894Sahrens dsl_pool_open_special_dir(dsl_pool_t *dp, const char *name, dsl_dir_t **ddp)
57fa9e4066Sahrens {
58fa9e4066Sahrens 	uint64_t obj;
59fa9e4066Sahrens 	int err;
60fa9e4066Sahrens 
61fa9e4066Sahrens 	err = zap_lookup(dp->dp_meta_objset,
62fa9e4066Sahrens 	    dp->dp_root_dir->dd_phys->dd_child_dir_zapobj,
63088f3894Sahrens 	    name, sizeof (obj), 1, &obj);
64ea8dc4b6Seschrock 	if (err)
65ea8dc4b6Seschrock 		return (err);
66fa9e4066Sahrens 
67088f3894Sahrens 	return (dsl_dir_open_obj(dp, obj, name, dp, ddp));
68fa9e4066Sahrens }
69fa9e4066Sahrens 
70fa9e4066Sahrens static dsl_pool_t *
71fa9e4066Sahrens dsl_pool_open_impl(spa_t *spa, uint64_t txg)
72fa9e4066Sahrens {
73fa9e4066Sahrens 	dsl_pool_t *dp;
74fa9e4066Sahrens 	blkptr_t *bp = spa_get_rootblkptr(spa);
75fa9e4066Sahrens 
76fa9e4066Sahrens 	dp = kmem_zalloc(sizeof (dsl_pool_t), KM_SLEEP);
77fa9e4066Sahrens 	dp->dp_spa = spa;
78fa9e4066Sahrens 	dp->dp_meta_rootbp = *bp;
795ad82045Snd 	rw_init(&dp->dp_config_rwlock, NULL, RW_DEFAULT, NULL);
801ab7f2deSmaybee 	dp->dp_write_limit = zfs_write_limit_min;
81fa9e4066Sahrens 	txg_init(dp, txg);
82fa9e4066Sahrens 
83fa9e4066Sahrens 	txg_list_create(&dp->dp_dirty_datasets,
84fa9e4066Sahrens 	    offsetof(dsl_dataset_t, ds_dirty_link));
85fa9e4066Sahrens 	txg_list_create(&dp->dp_dirty_dirs,
86fa9e4066Sahrens 	    offsetof(dsl_dir_t, dd_dirty_link));
871d452cf5Sahrens 	txg_list_create(&dp->dp_sync_tasks,
881d452cf5Sahrens 	    offsetof(dsl_sync_task_group_t, dstg_node));
893cb34c60Sahrens 	list_create(&dp->dp_synced_datasets, sizeof (dsl_dataset_t),
90fa9e4066Sahrens 	    offsetof(dsl_dataset_t, ds_synced_link));
91fa9e4066Sahrens 
921ab7f2deSmaybee 	mutex_init(&dp->dp_lock, NULL, MUTEX_DEFAULT, NULL);
931ab7f2deSmaybee 
949d3574bfSNeil Perrin 	dp->dp_vnrele_taskq = taskq_create("zfs_vn_rele_taskq", 1, minclsyspri,
959d3574bfSNeil Perrin 	    1, 4, 0);
969d3574bfSNeil Perrin 
97fa9e4066Sahrens 	return (dp);
98fa9e4066Sahrens }
99fa9e4066Sahrens 
100ea8dc4b6Seschrock int
101ea8dc4b6Seschrock dsl_pool_open(spa_t *spa, uint64_t txg, dsl_pool_t **dpp)
102fa9e4066Sahrens {
103fa9e4066Sahrens 	int err;
104fa9e4066Sahrens 	dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
105088f3894Sahrens 	dsl_dir_t *dd;
106088f3894Sahrens 	dsl_dataset_t *ds;
107fa9e4066Sahrens 
108088f3894Sahrens 	rw_enter(&dp->dp_config_rwlock, RW_WRITER);
109503ad85cSMatthew Ahrens 	err = dmu_objset_open_impl(spa, NULL, &dp->dp_meta_rootbp,
110503ad85cSMatthew Ahrens 	    &dp->dp_meta_objset);
111ea8dc4b6Seschrock 	if (err)
112ea8dc4b6Seschrock 		goto out;
113ea8dc4b6Seschrock 
114fa9e4066Sahrens 	err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
115fa9e4066Sahrens 	    DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1,
116fa9e4066Sahrens 	    &dp->dp_root_dir_obj);
117ea8dc4b6Seschrock 	if (err)
118ea8dc4b6Seschrock 		goto out;
119ea8dc4b6Seschrock 
120ea8dc4b6Seschrock 	err = dsl_dir_open_obj(dp, dp->dp_root_dir_obj,
121ea8dc4b6Seschrock 	    NULL, dp, &dp->dp_root_dir);
122ea8dc4b6Seschrock 	if (err)
123ea8dc4b6Seschrock 		goto out;
124fa9e4066Sahrens 
125088f3894Sahrens 	err = dsl_pool_open_special_dir(dp, MOS_DIR_NAME, &dp->dp_mos_dir);
126ea8dc4b6Seschrock 	if (err)
127ea8dc4b6Seschrock 		goto out;
128ea8dc4b6Seschrock 
129088f3894Sahrens 	if (spa_version(spa) >= SPA_VERSION_ORIGIN) {
130088f3894Sahrens 		err = dsl_pool_open_special_dir(dp, ORIGIN_DIR_NAME, &dd);
131088f3894Sahrens 		if (err)
132088f3894Sahrens 			goto out;
133088f3894Sahrens 		err = dsl_dataset_hold_obj(dp, dd->dd_phys->dd_head_dataset_obj,
134088f3894Sahrens 		    FTAG, &ds);
1358f63aa46SLin Ling 		if (err == 0) {
1368f63aa46SLin Ling 			err = dsl_dataset_hold_obj(dp,
1378f63aa46SLin Ling 			    ds->ds_phys->ds_prev_snap_obj, dp,
1388f63aa46SLin Ling 			    &dp->dp_origin_snap);
1398f63aa46SLin Ling 			dsl_dataset_rele(ds, FTAG);
1408f63aa46SLin Ling 		}
1418f63aa46SLin Ling 		dsl_dir_close(dd, dp);
142088f3894Sahrens 		if (err)
143088f3894Sahrens 			goto out;
144088f3894Sahrens 	}
145088f3894Sahrens 
146ca45db41SChris Kirby 	err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
147ca45db41SChris Kirby 	    DMU_POOL_TMP_USERREFS, sizeof (uint64_t), 1,
148ca45db41SChris Kirby 	    &dp->dp_tmp_userrefs_obj);
149ca45db41SChris Kirby 	if (err == ENOENT)
150ca45db41SChris Kirby 		err = 0;
151ca45db41SChris Kirby 	if (err)
152ca45db41SChris Kirby 		goto out;
153ca45db41SChris Kirby 
154*3f9d6ad7SLin Ling 	err = dsl_scan_init(dp, txg);
155088f3894Sahrens 
156ea8dc4b6Seschrock out:
157fa9e4066Sahrens 	rw_exit(&dp->dp_config_rwlock);
158ea8dc4b6Seschrock 	if (err)
159ea8dc4b6Seschrock 		dsl_pool_close(dp);
160ea8dc4b6Seschrock 	else
161ea8dc4b6Seschrock 		*dpp = dp;
162fa9e4066Sahrens 
163ea8dc4b6Seschrock 	return (err);
164fa9e4066Sahrens }
165fa9e4066Sahrens 
166fa9e4066Sahrens void
167fa9e4066Sahrens dsl_pool_close(dsl_pool_t *dp)
168fa9e4066Sahrens {
169088f3894Sahrens 	/* drop our references from dsl_pool_open() */
170088f3894Sahrens 
171088f3894Sahrens 	/*
172088f3894Sahrens 	 * Since we held the origin_snap from "syncing" context (which
173088f3894Sahrens 	 * includes pool-opening context), it actually only got a "ref"
174088f3894Sahrens 	 * and not a hold, so just drop that here.
175088f3894Sahrens 	 */
176088f3894Sahrens 	if (dp->dp_origin_snap)
177088f3894Sahrens 		dsl_dataset_drop_ref(dp->dp_origin_snap, dp);
178ea8dc4b6Seschrock 	if (dp->dp_mos_dir)
179ea8dc4b6Seschrock 		dsl_dir_close(dp->dp_mos_dir, dp);
180ea8dc4b6Seschrock 	if (dp->dp_root_dir)
181ea8dc4b6Seschrock 		dsl_dir_close(dp->dp_root_dir, dp);
182fa9e4066Sahrens 
183fa9e4066Sahrens 	/* undo the dmu_objset_open_impl(mos) from dsl_pool_open() */
184ea8dc4b6Seschrock 	if (dp->dp_meta_objset)
185503ad85cSMatthew Ahrens 		dmu_objset_evict(dp->dp_meta_objset);
186fa9e4066Sahrens 
187fa9e4066Sahrens 	txg_list_destroy(&dp->dp_dirty_datasets);
18854a91118SChris Kirby 	txg_list_destroy(&dp->dp_sync_tasks);
189fa9e4066Sahrens 	txg_list_destroy(&dp->dp_dirty_dirs);
1903cb34c60Sahrens 	list_destroy(&dp->dp_synced_datasets);
191fa9e4066Sahrens 
192874395d5Smaybee 	arc_flush(dp->dp_spa);
193fa9e4066Sahrens 	txg_fini(dp);
194*3f9d6ad7SLin Ling 	dsl_scan_fini(dp);
1955ad82045Snd 	rw_destroy(&dp->dp_config_rwlock);
1961ab7f2deSmaybee 	mutex_destroy(&dp->dp_lock);
1979d3574bfSNeil Perrin 	taskq_destroy(dp->dp_vnrele_taskq);
19888b7b0f2SMatthew Ahrens 	if (dp->dp_blkstats)
19988b7b0f2SMatthew Ahrens 		kmem_free(dp->dp_blkstats, sizeof (zfs_all_blkstats_t));
200fa9e4066Sahrens 	kmem_free(dp, sizeof (dsl_pool_t));
201fa9e4066Sahrens }
202fa9e4066Sahrens 
203fa9e4066Sahrens dsl_pool_t *
2040a48a24eStimh dsl_pool_create(spa_t *spa, nvlist_t *zplprops, uint64_t txg)
205fa9e4066Sahrens {
206fa9e4066Sahrens 	int err;
207fa9e4066Sahrens 	dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
208fa9e4066Sahrens 	dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg);
209503ad85cSMatthew Ahrens 	objset_t *os;
210088f3894Sahrens 	dsl_dataset_t *ds;
211088f3894Sahrens 	uint64_t dsobj;
212088f3894Sahrens 
213088f3894Sahrens 	/* create and open the MOS (meta-objset) */
214503ad85cSMatthew Ahrens 	dp->dp_meta_objset = dmu_objset_create_impl(spa,
215503ad85cSMatthew Ahrens 	    NULL, &dp->dp_meta_rootbp, DMU_OST_META, tx);
216fa9e4066Sahrens 
217fa9e4066Sahrens 	/* create the pool directory */
218fa9e4066Sahrens 	err = zap_create_claim(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
219fa9e4066Sahrens 	    DMU_OT_OBJECT_DIRECTORY, DMU_OT_NONE, 0, tx);
220fa9e4066Sahrens 	ASSERT3U(err, ==, 0);
221fa9e4066Sahrens 
222*3f9d6ad7SLin Ling 	/* Initialize scan structures */
223*3f9d6ad7SLin Ling 	VERIFY3U(0, ==, dsl_scan_init(dp, txg));
224*3f9d6ad7SLin Ling 
225fa9e4066Sahrens 	/* create and open the root dir */
226088f3894Sahrens 	dp->dp_root_dir_obj = dsl_dir_create_sync(dp, NULL, NULL, tx);
227ea8dc4b6Seschrock 	VERIFY(0 == dsl_dir_open_obj(dp, dp->dp_root_dir_obj,
228ea8dc4b6Seschrock 	    NULL, dp, &dp->dp_root_dir));
229fa9e4066Sahrens 
230fa9e4066Sahrens 	/* create and open the meta-objset dir */
231088f3894Sahrens 	(void) dsl_dir_create_sync(dp, dp->dp_root_dir, MOS_DIR_NAME, tx);
232088f3894Sahrens 	VERIFY(0 == dsl_pool_open_special_dir(dp,
233088f3894Sahrens 	    MOS_DIR_NAME, &dp->dp_mos_dir));
234088f3894Sahrens 
235088f3894Sahrens 	if (spa_version(spa) >= SPA_VERSION_DSL_SCRUB)
236088f3894Sahrens 		dsl_pool_create_origin(dp, tx);
237088f3894Sahrens 
238088f3894Sahrens 	/* create the root dataset */
239088f3894Sahrens 	dsobj = dsl_dataset_create_sync_dd(dp->dp_root_dir, NULL, 0, tx);
240088f3894Sahrens 
241088f3894Sahrens 	/* create the root objset */
242088f3894Sahrens 	VERIFY(0 == dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
243503ad85cSMatthew Ahrens 	os = dmu_objset_create_impl(dp->dp_spa, ds,
244088f3894Sahrens 	    dsl_dataset_get_blkptr(ds), DMU_OST_ZFS, tx);
245088f3894Sahrens #ifdef _KERNEL
246503ad85cSMatthew Ahrens 	zfs_create_fs(os, kcred, zplprops, tx);
247088f3894Sahrens #endif
248088f3894Sahrens 	dsl_dataset_rele(ds, FTAG);
249fa9e4066Sahrens 
250fa9e4066Sahrens 	dmu_tx_commit(tx);
251fa9e4066Sahrens 
252fa9e4066Sahrens 	return (dp);
253fa9e4066Sahrens }
254fa9e4066Sahrens 
255fa9e4066Sahrens void
256fa9e4066Sahrens dsl_pool_sync(dsl_pool_t *dp, uint64_t txg)
257fa9e4066Sahrens {
258c717a561Smaybee 	zio_t *zio;
259fa9e4066Sahrens 	dmu_tx_t *tx;
260c717a561Smaybee 	dsl_dir_t *dd;
261c717a561Smaybee 	dsl_dataset_t *ds;
262c717a561Smaybee 	dsl_sync_task_group_t *dstg;
263503ad85cSMatthew Ahrens 	objset_t *mos = dp->dp_meta_objset;
26405715f94SMark Maybee 	hrtime_t start, write_time;
26505715f94SMark Maybee 	uint64_t data_written;
266c717a561Smaybee 	int err;
267fa9e4066Sahrens 
268*3f9d6ad7SLin Ling 	/*
269*3f9d6ad7SLin Ling 	 * We need to copy dp_space_towrite() before doing
270*3f9d6ad7SLin Ling 	 * dsl_sync_task_group_sync(), because
271*3f9d6ad7SLin Ling 	 * dsl_dataset_snapshot_reserve_space() will increase
272*3f9d6ad7SLin Ling 	 * dp_space_towrite but not actually write anything.
273*3f9d6ad7SLin Ling 	 */
274*3f9d6ad7SLin Ling 	data_written = dp->dp_space_towrite[txg & TXG_MASK];
275*3f9d6ad7SLin Ling 
276fa9e4066Sahrens 	tx = dmu_tx_create_assigned(dp, txg);
277fa9e4066Sahrens 
27805715f94SMark Maybee 	dp->dp_read_overhead = 0;
2790fd90d51SMark Maybee 	start = gethrtime();
28014843421SMatthew Ahrens 
281c717a561Smaybee 	zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
282c717a561Smaybee 	while (ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) {
28314843421SMatthew Ahrens 		/*
28414843421SMatthew Ahrens 		 * We must not sync any non-MOS datasets twice, because
28514843421SMatthew Ahrens 		 * we may have taken a snapshot of them.  However, we
28614843421SMatthew Ahrens 		 * may sync newly-created datasets on pass 2.
28714843421SMatthew Ahrens 		 */
28814843421SMatthew Ahrens 		ASSERT(!list_link_active(&ds->ds_synced_link));
28914843421SMatthew Ahrens 		list_insert_tail(&dp->dp_synced_datasets, ds);
290c717a561Smaybee 		dsl_dataset_sync(ds, zio, tx);
291c717a561Smaybee 	}
29205715f94SMark Maybee 	DTRACE_PROBE(pool_sync__1setup);
293c717a561Smaybee 	err = zio_wait(zio);
29414843421SMatthew Ahrens 
29505715f94SMark Maybee 	write_time = gethrtime() - start;
296c717a561Smaybee 	ASSERT(err == 0);
29705715f94SMark Maybee 	DTRACE_PROBE(pool_sync__2rootzio);
298c717a561Smaybee 
29914843421SMatthew Ahrens 	for (ds = list_head(&dp->dp_synced_datasets); ds;
30014843421SMatthew Ahrens 	    ds = list_next(&dp->dp_synced_datasets, ds))
3010a586ceaSMark Shellenbaum 		dmu_objset_do_userquota_updates(ds->ds_objset, tx);
30214843421SMatthew Ahrens 
30314843421SMatthew Ahrens 	/*
30414843421SMatthew Ahrens 	 * Sync the datasets again to push out the changes due to
305*3f9d6ad7SLin Ling 	 * userspace updates.  This must be done before we process the
30614843421SMatthew Ahrens 	 * sync tasks, because that could cause a snapshot of a dataset
30714843421SMatthew Ahrens 	 * whose ds_bp will be rewritten when we do this 2nd sync.
30814843421SMatthew Ahrens 	 */
30914843421SMatthew Ahrens 	zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
31014843421SMatthew Ahrens 	while (ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) {
31114843421SMatthew Ahrens 		ASSERT(list_link_active(&ds->ds_synced_link));
31214843421SMatthew Ahrens 		dmu_buf_rele(ds->ds_dbuf, ds);
31314843421SMatthew Ahrens 		dsl_dataset_sync(ds, zio, tx);
31414843421SMatthew Ahrens 	}
31514843421SMatthew Ahrens 	err = zio_wait(zio);
31614843421SMatthew Ahrens 
317b24ab676SJeff Bonwick 	/*
318b24ab676SJeff Bonwick 	 * If anything was added to a deadlist during a zio done callback,
319b24ab676SJeff Bonwick 	 * it had to be put on the deferred queue.  Enqueue it for real now.
320b24ab676SJeff Bonwick 	 */
321b24ab676SJeff Bonwick 	for (ds = list_head(&dp->dp_synced_datasets); ds;
322b24ab676SJeff Bonwick 	    ds = list_next(&dp->dp_synced_datasets, ds))
323b24ab676SJeff Bonwick 		bplist_sync(&ds->ds_deadlist,
324b24ab676SJeff Bonwick 		    bplist_enqueue_cb, &ds->ds_deadlist, tx);
325b24ab676SJeff Bonwick 
32614843421SMatthew Ahrens 	while (dstg = txg_list_remove(&dp->dp_sync_tasks, txg)) {
32714843421SMatthew Ahrens 		/*
32814843421SMatthew Ahrens 		 * No more sync tasks should have been added while we
32914843421SMatthew Ahrens 		 * were syncing.
33014843421SMatthew Ahrens 		 */
33114843421SMatthew Ahrens 		ASSERT(spa_sync_pass(dp->dp_spa) == 1);
332c717a561Smaybee 		dsl_sync_task_group_sync(dstg, tx);
33314843421SMatthew Ahrens 	}
33405715f94SMark Maybee 	DTRACE_PROBE(pool_sync__3task);
33505715f94SMark Maybee 
33605715f94SMark Maybee 	start = gethrtime();
337c717a561Smaybee 	while (dd = txg_list_remove(&dp->dp_dirty_dirs, txg))
338c717a561Smaybee 		dsl_dir_sync(dd, tx);
33905715f94SMark Maybee 	write_time += gethrtime() - start;
340fa9e4066Sahrens 
34105715f94SMark Maybee 	start = gethrtime();
342503ad85cSMatthew Ahrens 	if (list_head(&mos->os_dirty_dnodes[txg & TXG_MASK]) != NULL ||
343503ad85cSMatthew Ahrens 	    list_head(&mos->os_free_dnodes[txg & TXG_MASK]) != NULL) {
344c717a561Smaybee 		zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
345503ad85cSMatthew Ahrens 		dmu_objset_sync(mos, zio, tx);
346c717a561Smaybee 		err = zio_wait(zio);
347c717a561Smaybee 		ASSERT(err == 0);
348fa9e4066Sahrens 		dprintf_bp(&dp->dp_meta_rootbp, "meta objset rootbp is %s", "");
349fa9e4066Sahrens 		spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
350fa9e4066Sahrens 	}
35105715f94SMark Maybee 	write_time += gethrtime() - start;
35205715f94SMark Maybee 	DTRACE_PROBE2(pool_sync__4io, hrtime_t, write_time,
35305715f94SMark Maybee 	    hrtime_t, dp->dp_read_overhead);
35405715f94SMark Maybee 	write_time -= dp->dp_read_overhead;
355fa9e4066Sahrens 
356fa9e4066Sahrens 	dmu_tx_commit(tx);
35705715f94SMark Maybee 
35805715f94SMark Maybee 	dp->dp_space_towrite[txg & TXG_MASK] = 0;
35905715f94SMark Maybee 	ASSERT(dp->dp_tempreserved[txg & TXG_MASK] == 0);
36005715f94SMark Maybee 
36105715f94SMark Maybee 	/*
36205715f94SMark Maybee 	 * If the write limit max has not been explicitly set, set it
36305715f94SMark Maybee 	 * to a fraction of available physical memory (default 1/8th).
36405715f94SMark Maybee 	 * Note that we must inflate the limit because the spa
36505715f94SMark Maybee 	 * inflates write sizes to account for data replication.
36605715f94SMark Maybee 	 * Check this each sync phase to catch changing memory size.
36705715f94SMark Maybee 	 */
36805715f94SMark Maybee 	if (physmem != old_physmem && zfs_write_limit_shift) {
36905715f94SMark Maybee 		mutex_enter(&zfs_write_limit_lock);
37005715f94SMark Maybee 		old_physmem = physmem;
37105715f94SMark Maybee 		zfs_write_limit_max = ptob(physmem) >> zfs_write_limit_shift;
37205715f94SMark Maybee 		zfs_write_limit_inflated = MAX(zfs_write_limit_min,
37305715f94SMark Maybee 		    spa_get_asize(dp->dp_spa, zfs_write_limit_max));
37405715f94SMark Maybee 		mutex_exit(&zfs_write_limit_lock);
37505715f94SMark Maybee 	}
37605715f94SMark Maybee 
37705715f94SMark Maybee 	/*
37805715f94SMark Maybee 	 * Attempt to keep the sync time consistent by adjusting the
37905715f94SMark Maybee 	 * amount of write traffic allowed into each transaction group.
38005715f94SMark Maybee 	 * Weight the throughput calculation towards the current value:
38105715f94SMark Maybee 	 * 	thru = 3/4 old_thru + 1/4 new_thru
382fb5dd802SLin Ling 	 *
383fb5dd802SLin Ling 	 * Note: write_time is in nanosecs, so write_time/MICROSEC
384fb5dd802SLin Ling 	 * yields millisecs
38505715f94SMark Maybee 	 */
38605715f94SMark Maybee 	ASSERT(zfs_write_limit_min > 0);
387fb5dd802SLin Ling 	if (data_written > zfs_write_limit_min / 8 && write_time > MICROSEC) {
388fb5dd802SLin Ling 		uint64_t throughput = data_written / (write_time / MICROSEC);
389fb5dd802SLin Ling 
39005715f94SMark Maybee 		if (dp->dp_throughput)
39105715f94SMark Maybee 			dp->dp_throughput = throughput / 4 +
39205715f94SMark Maybee 			    3 * dp->dp_throughput / 4;
39305715f94SMark Maybee 		else
39405715f94SMark Maybee 			dp->dp_throughput = throughput;
39505715f94SMark Maybee 		dp->dp_write_limit = MIN(zfs_write_limit_inflated,
39605715f94SMark Maybee 		    MAX(zfs_write_limit_min,
397fb5dd802SLin Ling 		    dp->dp_throughput * zfs_txg_synctime_ms));
39805715f94SMark Maybee 	}
399fa9e4066Sahrens }
400fa9e4066Sahrens 
401fa9e4066Sahrens void
402b24ab676SJeff Bonwick dsl_pool_sync_done(dsl_pool_t *dp, uint64_t txg)
403fa9e4066Sahrens {
404fa9e4066Sahrens 	dsl_dataset_t *ds;
405b24ab676SJeff Bonwick 	objset_t *os;
406fa9e4066Sahrens 
4073cb34c60Sahrens 	while (ds = list_head(&dp->dp_synced_datasets)) {
4083cb34c60Sahrens 		list_remove(&dp->dp_synced_datasets, ds);
409b24ab676SJeff Bonwick 		os = ds->ds_objset;
410b24ab676SJeff Bonwick 		zil_clean(os->os_zil);
411b24ab676SJeff Bonwick 		ASSERT(!dmu_objset_is_dirty(os, txg));
412af2c4821Smaybee 		dmu_buf_rele(ds->ds_dbuf, ds);
413fa9e4066Sahrens 	}
414b24ab676SJeff Bonwick 	ASSERT(!dmu_objset_is_dirty(dp->dp_meta_objset, txg));
415fa9e4066Sahrens }
416fa9e4066Sahrens 
417c717a561Smaybee /*
418c717a561Smaybee  * TRUE if the current thread is the tx_sync_thread or if we
419c717a561Smaybee  * are being called from SPA context during pool initialization.
420c717a561Smaybee  */
421fa9e4066Sahrens int
422fa9e4066Sahrens dsl_pool_sync_context(dsl_pool_t *dp)
423fa9e4066Sahrens {
424fa9e4066Sahrens 	return (curthread == dp->dp_tx.tx_sync_thread ||
425c717a561Smaybee 	    spa_get_dsl(dp->dp_spa) == NULL);
426fa9e4066Sahrens }
427fa9e4066Sahrens 
428fa9e4066Sahrens uint64_t
429fa9e4066Sahrens dsl_pool_adjustedsize(dsl_pool_t *dp, boolean_t netfree)
430fa9e4066Sahrens {
431fa9e4066Sahrens 	uint64_t space, resv;
432fa9e4066Sahrens 
433fa9e4066Sahrens 	/*
43444cd46caSbillm 	 * Reserve about 1.6% (1/64), or at least 32MB, for allocation
435fa9e4066Sahrens 	 * efficiency.
436fa9e4066Sahrens 	 * XXX The intent log is not accounted for, so it must fit
437fa9e4066Sahrens 	 * within this slop.
438fa9e4066Sahrens 	 *
439fa9e4066Sahrens 	 * If we're trying to assess whether it's OK to do a free,
440fa9e4066Sahrens 	 * cut the reservation in half to allow forward progress
441fa9e4066Sahrens 	 * (e.g. make it possible to rm(1) files from a full pool).
442fa9e4066Sahrens 	 */
443485bbbf5SGeorge Wilson 	space = spa_get_dspace(dp->dp_spa);
44444cd46caSbillm 	resv = MAX(space >> 6, SPA_MINDEVSIZE >> 1);
445fa9e4066Sahrens 	if (netfree)
446fa9e4066Sahrens 		resv >>= 1;
447fa9e4066Sahrens 
448fa9e4066Sahrens 	return (space - resv);
449fa9e4066Sahrens }
4501ab7f2deSmaybee 
4511ab7f2deSmaybee int
4521ab7f2deSmaybee dsl_pool_tempreserve_space(dsl_pool_t *dp, uint64_t space, dmu_tx_t *tx)
4531ab7f2deSmaybee {
4541ab7f2deSmaybee 	uint64_t reserved = 0;
4551ab7f2deSmaybee 	uint64_t write_limit = (zfs_write_limit_override ?
4561ab7f2deSmaybee 	    zfs_write_limit_override : dp->dp_write_limit);
4571ab7f2deSmaybee 
4581ab7f2deSmaybee 	if (zfs_no_write_throttle) {
459c5904d13Seschrock 		atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK],
460c5904d13Seschrock 		    space);
4611ab7f2deSmaybee 		return (0);
4621ab7f2deSmaybee 	}
4631ab7f2deSmaybee 
4641ab7f2deSmaybee 	/*
4651ab7f2deSmaybee 	 * Check to see if we have exceeded the maximum allowed IO for
4661ab7f2deSmaybee 	 * this transaction group.  We can do this without locks since
4671ab7f2deSmaybee 	 * a little slop here is ok.  Note that we do the reserved check
4681ab7f2deSmaybee 	 * with only half the requested reserve: this is because the
4691ab7f2deSmaybee 	 * reserve requests are worst-case, and we really don't want to
4701ab7f2deSmaybee 	 * throttle based off of worst-case estimates.
4711ab7f2deSmaybee 	 */
4721ab7f2deSmaybee 	if (write_limit > 0) {
4731ab7f2deSmaybee 		reserved = dp->dp_space_towrite[tx->tx_txg & TXG_MASK]
4741ab7f2deSmaybee 		    + dp->dp_tempreserved[tx->tx_txg & TXG_MASK] / 2;
4751ab7f2deSmaybee 
4761ab7f2deSmaybee 		if (reserved && reserved > write_limit)
4771ab7f2deSmaybee 			return (ERESTART);
4781ab7f2deSmaybee 	}
4791ab7f2deSmaybee 
4801ab7f2deSmaybee 	atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], space);
4811ab7f2deSmaybee 
4821ab7f2deSmaybee 	/*
4831ab7f2deSmaybee 	 * If this transaction group is over 7/8ths capacity, delay
4841ab7f2deSmaybee 	 * the caller 1 clock tick.  This will slow down the "fill"
4851ab7f2deSmaybee 	 * rate until the sync process can catch up with us.
4861ab7f2deSmaybee 	 */
487e8397a2bSgw 	if (reserved && reserved > (write_limit - (write_limit >> 3)))
4881ab7f2deSmaybee 		txg_delay(dp, tx->tx_txg, 1);
4891ab7f2deSmaybee 
4901ab7f2deSmaybee 	return (0);
4911ab7f2deSmaybee }
4921ab7f2deSmaybee 
4931ab7f2deSmaybee void
4941ab7f2deSmaybee dsl_pool_tempreserve_clear(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx)
4951ab7f2deSmaybee {
4961ab7f2deSmaybee 	ASSERT(dp->dp_tempreserved[tx->tx_txg & TXG_MASK] >= space);
4971ab7f2deSmaybee 	atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], -space);
4981ab7f2deSmaybee }
4991ab7f2deSmaybee 
5001ab7f2deSmaybee void
5011ab7f2deSmaybee dsl_pool_memory_pressure(dsl_pool_t *dp)
5021ab7f2deSmaybee {
5031ab7f2deSmaybee 	uint64_t space_inuse = 0;
5041ab7f2deSmaybee 	int i;
5051ab7f2deSmaybee 
5061ab7f2deSmaybee 	if (dp->dp_write_limit == zfs_write_limit_min)
5071ab7f2deSmaybee 		return;
5081ab7f2deSmaybee 
5091ab7f2deSmaybee 	for (i = 0; i < TXG_SIZE; i++) {
5101ab7f2deSmaybee 		space_inuse += dp->dp_space_towrite[i];
5111ab7f2deSmaybee 		space_inuse += dp->dp_tempreserved[i];
5121ab7f2deSmaybee 	}
5131ab7f2deSmaybee 	dp->dp_write_limit = MAX(zfs_write_limit_min,
5141ab7f2deSmaybee 	    MIN(dp->dp_write_limit, space_inuse / 4));
5151ab7f2deSmaybee }
5161ab7f2deSmaybee 
5171ab7f2deSmaybee void
5181ab7f2deSmaybee dsl_pool_willuse_space(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx)
5191ab7f2deSmaybee {
5201ab7f2deSmaybee 	if (space > 0) {
5211ab7f2deSmaybee 		mutex_enter(&dp->dp_lock);
5221ab7f2deSmaybee 		dp->dp_space_towrite[tx->tx_txg & TXG_MASK] += space;
5231ab7f2deSmaybee 		mutex_exit(&dp->dp_lock);
5241ab7f2deSmaybee 	}
5251ab7f2deSmaybee }
526088f3894Sahrens 
527088f3894Sahrens /* ARGSUSED */
528088f3894Sahrens static int
529088f3894Sahrens upgrade_clones_cb(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
530088f3894Sahrens {
531088f3894Sahrens 	dmu_tx_t *tx = arg;
532088f3894Sahrens 	dsl_dataset_t *ds, *prev = NULL;
533088f3894Sahrens 	int err;
534088f3894Sahrens 	dsl_pool_t *dp = spa_get_dsl(spa);
535088f3894Sahrens 
536088f3894Sahrens 	err = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds);
537088f3894Sahrens 	if (err)
538088f3894Sahrens 		return (err);
539088f3894Sahrens 
540088f3894Sahrens 	while (ds->ds_phys->ds_prev_snap_obj != 0) {
541088f3894Sahrens 		err = dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
542088f3894Sahrens 		    FTAG, &prev);
543088f3894Sahrens 		if (err) {
544088f3894Sahrens 			dsl_dataset_rele(ds, FTAG);
545088f3894Sahrens 			return (err);
546088f3894Sahrens 		}
547088f3894Sahrens 
548088f3894Sahrens 		if (prev->ds_phys->ds_next_snap_obj != ds->ds_object)
549088f3894Sahrens 			break;
550088f3894Sahrens 		dsl_dataset_rele(ds, FTAG);
551088f3894Sahrens 		ds = prev;
552088f3894Sahrens 		prev = NULL;
553088f3894Sahrens 	}
554088f3894Sahrens 
555088f3894Sahrens 	if (prev == NULL) {
556088f3894Sahrens 		prev = dp->dp_origin_snap;
557088f3894Sahrens 
558088f3894Sahrens 		/*
559088f3894Sahrens 		 * The $ORIGIN can't have any data, or the accounting
560088f3894Sahrens 		 * will be wrong.
561088f3894Sahrens 		 */
562088f3894Sahrens 		ASSERT(prev->ds_phys->ds_bp.blk_birth == 0);
563088f3894Sahrens 
564088f3894Sahrens 		/* The origin doesn't get attached to itself */
565088f3894Sahrens 		if (ds->ds_object == prev->ds_object) {
566088f3894Sahrens 			dsl_dataset_rele(ds, FTAG);
567088f3894Sahrens 			return (0);
568088f3894Sahrens 		}
569088f3894Sahrens 
570088f3894Sahrens 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
571088f3894Sahrens 		ds->ds_phys->ds_prev_snap_obj = prev->ds_object;
572088f3894Sahrens 		ds->ds_phys->ds_prev_snap_txg = prev->ds_phys->ds_creation_txg;
573088f3894Sahrens 
574088f3894Sahrens 		dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
575088f3894Sahrens 		ds->ds_dir->dd_phys->dd_origin_obj = prev->ds_object;
576088f3894Sahrens 
577088f3894Sahrens 		dmu_buf_will_dirty(prev->ds_dbuf, tx);
578088f3894Sahrens 		prev->ds_phys->ds_num_children++;
579088f3894Sahrens 
580088f3894Sahrens 		if (ds->ds_phys->ds_next_snap_obj == 0) {
581088f3894Sahrens 			ASSERT(ds->ds_prev == NULL);
582088f3894Sahrens 			VERIFY(0 == dsl_dataset_hold_obj(dp,
583088f3894Sahrens 			    ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev));
584088f3894Sahrens 		}
585088f3894Sahrens 	}
586088f3894Sahrens 
587088f3894Sahrens 	ASSERT(ds->ds_dir->dd_phys->dd_origin_obj == prev->ds_object);
588088f3894Sahrens 	ASSERT(ds->ds_phys->ds_prev_snap_obj == prev->ds_object);
589088f3894Sahrens 
590088f3894Sahrens 	if (prev->ds_phys->ds_next_clones_obj == 0) {
591c33e334fSMatthew Ahrens 		dmu_buf_will_dirty(prev->ds_dbuf, tx);
592088f3894Sahrens 		prev->ds_phys->ds_next_clones_obj =
593088f3894Sahrens 		    zap_create(dp->dp_meta_objset,
594088f3894Sahrens 		    DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
595088f3894Sahrens 	}
596088f3894Sahrens 	VERIFY(0 == zap_add_int(dp->dp_meta_objset,
597088f3894Sahrens 	    prev->ds_phys->ds_next_clones_obj, ds->ds_object, tx));
598088f3894Sahrens 
599088f3894Sahrens 	dsl_dataset_rele(ds, FTAG);
600088f3894Sahrens 	if (prev != dp->dp_origin_snap)
601088f3894Sahrens 		dsl_dataset_rele(prev, FTAG);
602088f3894Sahrens 	return (0);
603088f3894Sahrens }
604088f3894Sahrens 
605088f3894Sahrens void
606088f3894Sahrens dsl_pool_upgrade_clones(dsl_pool_t *dp, dmu_tx_t *tx)
607088f3894Sahrens {
608088f3894Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
609088f3894Sahrens 	ASSERT(dp->dp_origin_snap != NULL);
610088f3894Sahrens 
611c33e334fSMatthew Ahrens 	VERIFY3U(0, ==, dmu_objset_find_spa(dp->dp_spa, NULL, upgrade_clones_cb,
612c33e334fSMatthew Ahrens 	    tx, DS_FIND_CHILDREN));
613088f3894Sahrens }
614088f3894Sahrens 
615088f3894Sahrens void
616088f3894Sahrens dsl_pool_create_origin(dsl_pool_t *dp, dmu_tx_t *tx)
617088f3894Sahrens {
618088f3894Sahrens 	uint64_t dsobj;
619088f3894Sahrens 	dsl_dataset_t *ds;
620088f3894Sahrens 
621088f3894Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
622088f3894Sahrens 	ASSERT(dp->dp_origin_snap == NULL);
623088f3894Sahrens 
624088f3894Sahrens 	/* create the origin dir, ds, & snap-ds */
625088f3894Sahrens 	rw_enter(&dp->dp_config_rwlock, RW_WRITER);
626088f3894Sahrens 	dsobj = dsl_dataset_create_sync(dp->dp_root_dir, ORIGIN_DIR_NAME,
627088f3894Sahrens 	    NULL, 0, kcred, tx);
628088f3894Sahrens 	VERIFY(0 == dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
629*3f9d6ad7SLin Ling 	dsl_dataset_snapshot_sync(ds, ORIGIN_DIR_NAME, tx);
630088f3894Sahrens 	VERIFY(0 == dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
631088f3894Sahrens 	    dp, &dp->dp_origin_snap));
632088f3894Sahrens 	dsl_dataset_rele(ds, FTAG);
633088f3894Sahrens 	rw_exit(&dp->dp_config_rwlock);
634088f3894Sahrens }
6359d3574bfSNeil Perrin 
6369d3574bfSNeil Perrin taskq_t *
6379d3574bfSNeil Perrin dsl_pool_vnrele_taskq(dsl_pool_t *dp)
6389d3574bfSNeil Perrin {
6399d3574bfSNeil Perrin 	return (dp->dp_vnrele_taskq);
6409d3574bfSNeil Perrin }
641ca45db41SChris Kirby 
642ca45db41SChris Kirby /*
643ca45db41SChris Kirby  * Walk through the pool-wide zap object of temporary snapshot user holds
644ca45db41SChris Kirby  * and release them.
645ca45db41SChris Kirby  */
646ca45db41SChris Kirby void
647ca45db41SChris Kirby dsl_pool_clean_tmp_userrefs(dsl_pool_t *dp)
648ca45db41SChris Kirby {
649ca45db41SChris Kirby 	zap_attribute_t za;
650ca45db41SChris Kirby 	zap_cursor_t zc;
651ca45db41SChris Kirby 	objset_t *mos = dp->dp_meta_objset;
652ca45db41SChris Kirby 	uint64_t zapobj = dp->dp_tmp_userrefs_obj;
653ca45db41SChris Kirby 
654ca45db41SChris Kirby 	if (zapobj == 0)
655ca45db41SChris Kirby 		return;
656ca45db41SChris Kirby 	ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
657ca45db41SChris Kirby 
658ca45db41SChris Kirby 	for (zap_cursor_init(&zc, mos, zapobj);
659ca45db41SChris Kirby 	    zap_cursor_retrieve(&zc, &za) == 0;
660ca45db41SChris Kirby 	    zap_cursor_advance(&zc)) {
661ca45db41SChris Kirby 		char *htag;
662ca45db41SChris Kirby 		uint64_t dsobj;
663ca45db41SChris Kirby 
664ca45db41SChris Kirby 		htag = strchr(za.za_name, '-');
665ca45db41SChris Kirby 		*htag = '\0';
666ca45db41SChris Kirby 		++htag;
667ca45db41SChris Kirby 		dsobj = strtonum(za.za_name, NULL);
668ca45db41SChris Kirby 		(void) dsl_dataset_user_release_tmp(dp, dsobj, htag);
669ca45db41SChris Kirby 	}
670ca45db41SChris Kirby 	zap_cursor_fini(&zc);
671ca45db41SChris Kirby }
672ca45db41SChris Kirby 
673ca45db41SChris Kirby /*
674ca45db41SChris Kirby  * Create the pool-wide zap object for storing temporary snapshot holds.
675ca45db41SChris Kirby  */
676ca45db41SChris Kirby void
677ca45db41SChris Kirby dsl_pool_user_hold_create_obj(dsl_pool_t *dp, dmu_tx_t *tx)
678ca45db41SChris Kirby {
679ca45db41SChris Kirby 	objset_t *mos = dp->dp_meta_objset;
680ca45db41SChris Kirby 
681ca45db41SChris Kirby 	ASSERT(dp->dp_tmp_userrefs_obj == 0);
682ca45db41SChris Kirby 	ASSERT(dmu_tx_is_syncing(tx));
683ca45db41SChris Kirby 
684ca45db41SChris Kirby 	dp->dp_tmp_userrefs_obj = zap_create(mos, DMU_OT_USERREFS,
685ca45db41SChris Kirby 	    DMU_OT_NONE, 0, tx);
686ca45db41SChris Kirby 
687ca45db41SChris Kirby 	VERIFY(zap_add(mos, DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_TMP_USERREFS,
688ca45db41SChris Kirby 	    sizeof (uint64_t), 1, &dp->dp_tmp_userrefs_obj, tx) == 0);
689ca45db41SChris Kirby }
690ca45db41SChris Kirby 
691ca45db41SChris Kirby static int
692ca45db41SChris Kirby dsl_pool_user_hold_rele_impl(dsl_pool_t *dp, uint64_t dsobj,
69315508ac0SChris Kirby     const char *tag, uint64_t *now, dmu_tx_t *tx, boolean_t holding)
694ca45db41SChris Kirby {
695ca45db41SChris Kirby 	objset_t *mos = dp->dp_meta_objset;
696ca45db41SChris Kirby 	uint64_t zapobj = dp->dp_tmp_userrefs_obj;
697ca45db41SChris Kirby 	char *name;
698ca45db41SChris Kirby 	int error;
699ca45db41SChris Kirby 
700ca45db41SChris Kirby 	ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
701ca45db41SChris Kirby 	ASSERT(dmu_tx_is_syncing(tx));
702ca45db41SChris Kirby 
703ca45db41SChris Kirby 	/*
704ca45db41SChris Kirby 	 * If the pool was created prior to SPA_VERSION_USERREFS, the
705ca45db41SChris Kirby 	 * zap object for temporary holds might not exist yet.
706ca45db41SChris Kirby 	 */
707ca45db41SChris Kirby 	if (zapobj == 0) {
708ca45db41SChris Kirby 		if (holding) {
709ca45db41SChris Kirby 			dsl_pool_user_hold_create_obj(dp, tx);
710ca45db41SChris Kirby 			zapobj = dp->dp_tmp_userrefs_obj;
711ca45db41SChris Kirby 		} else {
712ca45db41SChris Kirby 			return (ENOENT);
713ca45db41SChris Kirby 		}
714ca45db41SChris Kirby 	}
715ca45db41SChris Kirby 
716ca45db41SChris Kirby 	name = kmem_asprintf("%llx-%s", (u_longlong_t)dsobj, tag);
717ca45db41SChris Kirby 	if (holding)
71815508ac0SChris Kirby 		error = zap_add(mos, zapobj, name, 8, 1, now, tx);
719ca45db41SChris Kirby 	else
720ca45db41SChris Kirby 		error = zap_remove(mos, zapobj, name, tx);
721ca45db41SChris Kirby 	strfree(name);
722ca45db41SChris Kirby 
723ca45db41SChris Kirby 	return (error);
724ca45db41SChris Kirby }
725ca45db41SChris Kirby 
726ca45db41SChris Kirby /*
727ca45db41SChris Kirby  * Add a temporary hold for the given dataset object and tag.
728ca45db41SChris Kirby  */
729ca45db41SChris Kirby int
730ca45db41SChris Kirby dsl_pool_user_hold(dsl_pool_t *dp, uint64_t dsobj, const char *tag,
73115508ac0SChris Kirby     uint64_t *now, dmu_tx_t *tx)
732ca45db41SChris Kirby {
73315508ac0SChris Kirby 	return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, now, tx, B_TRUE));
734ca45db41SChris Kirby }
735ca45db41SChris Kirby 
736ca45db41SChris Kirby /*
737ca45db41SChris Kirby  * Release a temporary hold for the given dataset object and tag.
738ca45db41SChris Kirby  */
739ca45db41SChris Kirby int
740ca45db41SChris Kirby dsl_pool_user_release(dsl_pool_t *dp, uint64_t dsobj, const char *tag,
741ca45db41SChris Kirby     dmu_tx_t *tx)
742ca45db41SChris Kirby {
743ca45db41SChris Kirby 	return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, NULL,
744ca45db41SChris Kirby 	    tx, B_FALSE));
745ca45db41SChris Kirby }
746