xref: /illumos-gate/usr/src/uts/common/fs/zfs/dsl_pool.c (revision fb09f5aad449c97fe309678f3f604982b563a96f)
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 /*
223f9d6ad7SLin Ling  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23ad135b5dSChristopher Siden  * Copyright (c) 2012 by Delphix. All rights reserved.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens #include <sys/dsl_pool.h>
27fa9e4066Sahrens #include <sys/dsl_dataset.h>
283f9d6ad7SLin Ling #include <sys/dsl_prop.h>
29fa9e4066Sahrens #include <sys/dsl_dir.h>
301d452cf5Sahrens #include <sys/dsl_synctask.h>
313f9d6ad7SLin Ling #include <sys/dsl_scan.h>
323f9d6ad7SLin Ling #include <sys/dnode.h>
33fa9e4066Sahrens #include <sys/dmu_tx.h>
34fa9e4066Sahrens #include <sys/dmu_objset.h>
35fa9e4066Sahrens #include <sys/arc.h>
36fa9e4066Sahrens #include <sys/zap.h>
37c717a561Smaybee #include <sys/zio.h>
38fa9e4066Sahrens #include <sys/zfs_context.h>
39fa9e4066Sahrens #include <sys/fs/zfs.h>
40088f3894Sahrens #include <sys/zfs_znode.h>
41088f3894Sahrens #include <sys/spa_impl.h>
42cde58dbcSMatthew Ahrens #include <sys/dsl_deadlist.h>
43ad135b5dSChristopher Siden #include <sys/bptree.h>
44ad135b5dSChristopher Siden #include <sys/zfeature.h>
45ce636f8bSMatthew Ahrens #include <sys/zil_impl.h>
46fa9e4066Sahrens 
471ab7f2deSmaybee int zfs_no_write_throttle = 0;
4805715f94SMark Maybee int zfs_write_limit_shift = 3;			/* 1/8th of physical memory */
4944ecc532SGeorge Wilson int zfs_txg_synctime_ms = 1000;		/* target millisecs to sync a txg */
5005715f94SMark Maybee 
5105715f94SMark Maybee uint64_t zfs_write_limit_min = 32 << 20;	/* min write limit is 32MB */
5205715f94SMark Maybee uint64_t zfs_write_limit_max = 0;		/* max data payload per txg */
5305715f94SMark Maybee uint64_t zfs_write_limit_inflated = 0;
541ab7f2deSmaybee uint64_t zfs_write_limit_override = 0;
551ab7f2deSmaybee 
5605715f94SMark Maybee kmutex_t zfs_write_limit_lock;
5705715f94SMark Maybee 
5805715f94SMark Maybee static pgcnt_t old_physmem = 0;
59088f3894Sahrens 
603f9d6ad7SLin Ling int
61088f3894Sahrens dsl_pool_open_special_dir(dsl_pool_t *dp, const char *name, dsl_dir_t **ddp)
62fa9e4066Sahrens {
63fa9e4066Sahrens 	uint64_t obj;
64fa9e4066Sahrens 	int err;
65fa9e4066Sahrens 
66fa9e4066Sahrens 	err = zap_lookup(dp->dp_meta_objset,
67fa9e4066Sahrens 	    dp->dp_root_dir->dd_phys->dd_child_dir_zapobj,
68088f3894Sahrens 	    name, sizeof (obj), 1, &obj);
69ea8dc4b6Seschrock 	if (err)
70ea8dc4b6Seschrock 		return (err);
71fa9e4066Sahrens 
72088f3894Sahrens 	return (dsl_dir_open_obj(dp, obj, name, dp, ddp));
73fa9e4066Sahrens }
74fa9e4066Sahrens 
75fa9e4066Sahrens static dsl_pool_t *
76fa9e4066Sahrens dsl_pool_open_impl(spa_t *spa, uint64_t txg)
77fa9e4066Sahrens {
78fa9e4066Sahrens 	dsl_pool_t *dp;
79fa9e4066Sahrens 	blkptr_t *bp = spa_get_rootblkptr(spa);
80fa9e4066Sahrens 
81fa9e4066Sahrens 	dp = kmem_zalloc(sizeof (dsl_pool_t), KM_SLEEP);
82fa9e4066Sahrens 	dp->dp_spa = spa;
83fa9e4066Sahrens 	dp->dp_meta_rootbp = *bp;
845ad82045Snd 	rw_init(&dp->dp_config_rwlock, NULL, RW_DEFAULT, NULL);
851ab7f2deSmaybee 	dp->dp_write_limit = zfs_write_limit_min;
86fa9e4066Sahrens 	txg_init(dp, txg);
87fa9e4066Sahrens 
88fa9e4066Sahrens 	txg_list_create(&dp->dp_dirty_datasets,
89fa9e4066Sahrens 	    offsetof(dsl_dataset_t, ds_dirty_link));
90ce636f8bSMatthew Ahrens 	txg_list_create(&dp->dp_dirty_zilogs,
91ce636f8bSMatthew Ahrens 	    offsetof(zilog_t, zl_dirty_link));
92fa9e4066Sahrens 	txg_list_create(&dp->dp_dirty_dirs,
93fa9e4066Sahrens 	    offsetof(dsl_dir_t, dd_dirty_link));
941d452cf5Sahrens 	txg_list_create(&dp->dp_sync_tasks,
951d452cf5Sahrens 	    offsetof(dsl_sync_task_group_t, dstg_node));
96fa9e4066Sahrens 
971ab7f2deSmaybee 	mutex_init(&dp->dp_lock, NULL, MUTEX_DEFAULT, NULL);
981ab7f2deSmaybee 
999d3574bfSNeil Perrin 	dp->dp_vnrele_taskq = taskq_create("zfs_vn_rele_taskq", 1, minclsyspri,
1009d3574bfSNeil Perrin 	    1, 4, 0);
1019d3574bfSNeil Perrin 
102fa9e4066Sahrens 	return (dp);
103fa9e4066Sahrens }
104fa9e4066Sahrens 
105ea8dc4b6Seschrock int
106ad135b5dSChristopher Siden dsl_pool_init(spa_t *spa, uint64_t txg, dsl_pool_t **dpp)
107fa9e4066Sahrens {
108fa9e4066Sahrens 	int err;
109fa9e4066Sahrens 	dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
110ad135b5dSChristopher Siden 
111ad135b5dSChristopher Siden 	err = dmu_objset_open_impl(spa, NULL, &dp->dp_meta_rootbp,
112ad135b5dSChristopher Siden 	    &dp->dp_meta_objset);
113ad135b5dSChristopher Siden 	if (err != 0)
114ad135b5dSChristopher Siden 		dsl_pool_close(dp);
115ad135b5dSChristopher Siden 	else
116ad135b5dSChristopher Siden 		*dpp = dp;
117ad135b5dSChristopher Siden 
118ad135b5dSChristopher Siden 	return (err);
119ad135b5dSChristopher Siden }
120ad135b5dSChristopher Siden 
121ad135b5dSChristopher Siden int
122ad135b5dSChristopher Siden dsl_pool_open(dsl_pool_t *dp)
123ad135b5dSChristopher Siden {
124ad135b5dSChristopher Siden 	int err;
125088f3894Sahrens 	dsl_dir_t *dd;
126088f3894Sahrens 	dsl_dataset_t *ds;
127cde58dbcSMatthew Ahrens 	uint64_t obj;
128fa9e4066Sahrens 
129ad135b5dSChristopher Siden 	rw_enter(&dp->dp_config_rwlock, RW_WRITER);
130fa9e4066Sahrens 	err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
131fa9e4066Sahrens 	    DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1,
132fa9e4066Sahrens 	    &dp->dp_root_dir_obj);
133ea8dc4b6Seschrock 	if (err)
134ea8dc4b6Seschrock 		goto out;
135ea8dc4b6Seschrock 
136ea8dc4b6Seschrock 	err = dsl_dir_open_obj(dp, dp->dp_root_dir_obj,
137ea8dc4b6Seschrock 	    NULL, dp, &dp->dp_root_dir);
138ea8dc4b6Seschrock 	if (err)
139ea8dc4b6Seschrock 		goto out;
140fa9e4066Sahrens 
141088f3894Sahrens 	err = dsl_pool_open_special_dir(dp, MOS_DIR_NAME, &dp->dp_mos_dir);
142ea8dc4b6Seschrock 	if (err)
143ea8dc4b6Seschrock 		goto out;
144ea8dc4b6Seschrock 
145ad135b5dSChristopher Siden 	if (spa_version(dp->dp_spa) >= SPA_VERSION_ORIGIN) {
146088f3894Sahrens 		err = dsl_pool_open_special_dir(dp, ORIGIN_DIR_NAME, &dd);
147088f3894Sahrens 		if (err)
148088f3894Sahrens 			goto out;
149088f3894Sahrens 		err = dsl_dataset_hold_obj(dp, dd->dd_phys->dd_head_dataset_obj,
150088f3894Sahrens 		    FTAG, &ds);
1518f63aa46SLin Ling 		if (err == 0) {
1528f63aa46SLin Ling 			err = dsl_dataset_hold_obj(dp,
1538f63aa46SLin Ling 			    ds->ds_phys->ds_prev_snap_obj, dp,
1548f63aa46SLin Ling 			    &dp->dp_origin_snap);
1558f63aa46SLin Ling 			dsl_dataset_rele(ds, FTAG);
1568f63aa46SLin Ling 		}
1578f63aa46SLin Ling 		dsl_dir_close(dd, dp);
158088f3894Sahrens 		if (err)
159088f3894Sahrens 			goto out;
160088f3894Sahrens 	}
161088f3894Sahrens 
162ad135b5dSChristopher Siden 	if (spa_version(dp->dp_spa) >= SPA_VERSION_DEADLISTS) {
163cde58dbcSMatthew Ahrens 		err = dsl_pool_open_special_dir(dp, FREE_DIR_NAME,
164cde58dbcSMatthew Ahrens 		    &dp->dp_free_dir);
165cde58dbcSMatthew Ahrens 		if (err)
166cde58dbcSMatthew Ahrens 			goto out;
167cde58dbcSMatthew Ahrens 
168cde58dbcSMatthew Ahrens 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
169cde58dbcSMatthew Ahrens 		    DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj);
170cde58dbcSMatthew Ahrens 		if (err)
171cde58dbcSMatthew Ahrens 			goto out;
172b420f3adSRichard Lowe 		VERIFY3U(0, ==, bpobj_open(&dp->dp_free_bpobj,
173cde58dbcSMatthew Ahrens 		    dp->dp_meta_objset, obj));
174cde58dbcSMatthew Ahrens 	}
175cde58dbcSMatthew Ahrens 
176ad135b5dSChristopher Siden 	if (spa_feature_is_active(dp->dp_spa,
177ad135b5dSChristopher Siden 	    &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY])) {
178ad135b5dSChristopher Siden 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
179ad135b5dSChristopher Siden 		    DMU_POOL_BPTREE_OBJ, sizeof (uint64_t), 1,
180ad135b5dSChristopher Siden 		    &dp->dp_bptree_obj);
181ad135b5dSChristopher Siden 		if (err != 0)
182ad135b5dSChristopher Siden 			goto out;
183ad135b5dSChristopher Siden 	}
184ad135b5dSChristopher Siden 
185f1745736SMatthew Ahrens 	if (spa_feature_is_active(dp->dp_spa,
186f1745736SMatthew Ahrens 	    &spa_feature_table[SPA_FEATURE_EMPTY_BPOBJ])) {
187f1745736SMatthew Ahrens 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
188f1745736SMatthew Ahrens 		    DMU_POOL_EMPTY_BPOBJ, sizeof (uint64_t), 1,
189f1745736SMatthew Ahrens 		    &dp->dp_empty_bpobj);
190f1745736SMatthew Ahrens 		if (err != 0)
191f1745736SMatthew Ahrens 			goto out;
192f1745736SMatthew Ahrens 	}
193f1745736SMatthew Ahrens 
194ca45db41SChris Kirby 	err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
195ca45db41SChris Kirby 	    DMU_POOL_TMP_USERREFS, sizeof (uint64_t), 1,
196ca45db41SChris Kirby 	    &dp->dp_tmp_userrefs_obj);
197ca45db41SChris Kirby 	if (err == ENOENT)
198ca45db41SChris Kirby 		err = 0;
199ca45db41SChris Kirby 	if (err)
200ca45db41SChris Kirby 		goto out;
201ca45db41SChris Kirby 
202ad135b5dSChristopher Siden 	err = dsl_scan_init(dp, dp->dp_tx.tx_open_txg);
203088f3894Sahrens 
204ea8dc4b6Seschrock out:
205fa9e4066Sahrens 	rw_exit(&dp->dp_config_rwlock);
206ea8dc4b6Seschrock 	return (err);
207fa9e4066Sahrens }
208fa9e4066Sahrens 
209fa9e4066Sahrens void
210fa9e4066Sahrens dsl_pool_close(dsl_pool_t *dp)
211fa9e4066Sahrens {
212088f3894Sahrens 	/* drop our references from dsl_pool_open() */
213088f3894Sahrens 
214088f3894Sahrens 	/*
215088f3894Sahrens 	 * Since we held the origin_snap from "syncing" context (which
216088f3894Sahrens 	 * includes pool-opening context), it actually only got a "ref"
217088f3894Sahrens 	 * and not a hold, so just drop that here.
218088f3894Sahrens 	 */
219088f3894Sahrens 	if (dp->dp_origin_snap)
220088f3894Sahrens 		dsl_dataset_drop_ref(dp->dp_origin_snap, dp);
221ea8dc4b6Seschrock 	if (dp->dp_mos_dir)
222ea8dc4b6Seschrock 		dsl_dir_close(dp->dp_mos_dir, dp);
223cde58dbcSMatthew Ahrens 	if (dp->dp_free_dir)
224cde58dbcSMatthew Ahrens 		dsl_dir_close(dp->dp_free_dir, dp);
225ea8dc4b6Seschrock 	if (dp->dp_root_dir)
226ea8dc4b6Seschrock 		dsl_dir_close(dp->dp_root_dir, dp);
227fa9e4066Sahrens 
228cde58dbcSMatthew Ahrens 	bpobj_close(&dp->dp_free_bpobj);
229cde58dbcSMatthew Ahrens 
230fa9e4066Sahrens 	/* undo the dmu_objset_open_impl(mos) from dsl_pool_open() */
231ea8dc4b6Seschrock 	if (dp->dp_meta_objset)
232503ad85cSMatthew Ahrens 		dmu_objset_evict(dp->dp_meta_objset);
233fa9e4066Sahrens 
234fa9e4066Sahrens 	txg_list_destroy(&dp->dp_dirty_datasets);
235ce636f8bSMatthew Ahrens 	txg_list_destroy(&dp->dp_dirty_zilogs);
23654a91118SChris Kirby 	txg_list_destroy(&dp->dp_sync_tasks);
237fa9e4066Sahrens 	txg_list_destroy(&dp->dp_dirty_dirs);
238fa9e4066Sahrens 
239874395d5Smaybee 	arc_flush(dp->dp_spa);
240fa9e4066Sahrens 	txg_fini(dp);
2413f9d6ad7SLin Ling 	dsl_scan_fini(dp);
2425ad82045Snd 	rw_destroy(&dp->dp_config_rwlock);
2431ab7f2deSmaybee 	mutex_destroy(&dp->dp_lock);
2449d3574bfSNeil Perrin 	taskq_destroy(dp->dp_vnrele_taskq);
24588b7b0f2SMatthew Ahrens 	if (dp->dp_blkstats)
24688b7b0f2SMatthew Ahrens 		kmem_free(dp->dp_blkstats, sizeof (zfs_all_blkstats_t));
247fa9e4066Sahrens 	kmem_free(dp, sizeof (dsl_pool_t));
248fa9e4066Sahrens }
249fa9e4066Sahrens 
250fa9e4066Sahrens dsl_pool_t *
2510a48a24eStimh dsl_pool_create(spa_t *spa, nvlist_t *zplprops, uint64_t txg)
252fa9e4066Sahrens {
253fa9e4066Sahrens 	int err;
254fa9e4066Sahrens 	dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
255fa9e4066Sahrens 	dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg);
256503ad85cSMatthew Ahrens 	objset_t *os;
257088f3894Sahrens 	dsl_dataset_t *ds;
258cde58dbcSMatthew Ahrens 	uint64_t obj;
259088f3894Sahrens 
260088f3894Sahrens 	/* create and open the MOS (meta-objset) */
261503ad85cSMatthew Ahrens 	dp->dp_meta_objset = dmu_objset_create_impl(spa,
262503ad85cSMatthew Ahrens 	    NULL, &dp->dp_meta_rootbp, DMU_OST_META, tx);
263fa9e4066Sahrens 
264fa9e4066Sahrens 	/* create the pool directory */
265fa9e4066Sahrens 	err = zap_create_claim(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
266fa9e4066Sahrens 	    DMU_OT_OBJECT_DIRECTORY, DMU_OT_NONE, 0, tx);
267*fb09f5aaSMadhav Suresh 	ASSERT0(err);
268fa9e4066Sahrens 
2693f9d6ad7SLin Ling 	/* Initialize scan structures */
270b420f3adSRichard Lowe 	VERIFY3U(0, ==, dsl_scan_init(dp, txg));
2713f9d6ad7SLin Ling 
272fa9e4066Sahrens 	/* create and open the root dir */
273088f3894Sahrens 	dp->dp_root_dir_obj = dsl_dir_create_sync(dp, NULL, NULL, tx);
274ea8dc4b6Seschrock 	VERIFY(0 == dsl_dir_open_obj(dp, dp->dp_root_dir_obj,
275ea8dc4b6Seschrock 	    NULL, dp, &dp->dp_root_dir));
276fa9e4066Sahrens 
277fa9e4066Sahrens 	/* create and open the meta-objset dir */
278088f3894Sahrens 	(void) dsl_dir_create_sync(dp, dp->dp_root_dir, MOS_DIR_NAME, tx);
279088f3894Sahrens 	VERIFY(0 == dsl_pool_open_special_dir(dp,
280088f3894Sahrens 	    MOS_DIR_NAME, &dp->dp_mos_dir));
281088f3894Sahrens 
282cde58dbcSMatthew Ahrens 	if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
283cde58dbcSMatthew Ahrens 		/* create and open the free dir */
284cde58dbcSMatthew Ahrens 		(void) dsl_dir_create_sync(dp, dp->dp_root_dir,
285cde58dbcSMatthew Ahrens 		    FREE_DIR_NAME, tx);
286cde58dbcSMatthew Ahrens 		VERIFY(0 == dsl_pool_open_special_dir(dp,
287cde58dbcSMatthew Ahrens 		    FREE_DIR_NAME, &dp->dp_free_dir));
288cde58dbcSMatthew Ahrens 
289cde58dbcSMatthew Ahrens 		/* create and open the free_bplist */
290cde58dbcSMatthew Ahrens 		obj = bpobj_alloc(dp->dp_meta_objset, SPA_MAXBLOCKSIZE, tx);
291cde58dbcSMatthew Ahrens 		VERIFY(zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
292cde58dbcSMatthew Ahrens 		    DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj, tx) == 0);
293b420f3adSRichard Lowe 		VERIFY3U(0, ==, bpobj_open(&dp->dp_free_bpobj,
294cde58dbcSMatthew Ahrens 		    dp->dp_meta_objset, obj));
295cde58dbcSMatthew Ahrens 	}
296cde58dbcSMatthew Ahrens 
297088f3894Sahrens 	if (spa_version(spa) >= SPA_VERSION_DSL_SCRUB)
298088f3894Sahrens 		dsl_pool_create_origin(dp, tx);
299088f3894Sahrens 
300088f3894Sahrens 	/* create the root dataset */
301cde58dbcSMatthew Ahrens 	obj = dsl_dataset_create_sync_dd(dp->dp_root_dir, NULL, 0, tx);
302088f3894Sahrens 
303088f3894Sahrens 	/* create the root objset */
304cde58dbcSMatthew Ahrens 	VERIFY(0 == dsl_dataset_hold_obj(dp, obj, FTAG, &ds));
305503ad85cSMatthew Ahrens 	os = dmu_objset_create_impl(dp->dp_spa, ds,
306088f3894Sahrens 	    dsl_dataset_get_blkptr(ds), DMU_OST_ZFS, tx);
307088f3894Sahrens #ifdef _KERNEL
308503ad85cSMatthew Ahrens 	zfs_create_fs(os, kcred, zplprops, tx);
309088f3894Sahrens #endif
310088f3894Sahrens 	dsl_dataset_rele(ds, FTAG);
311fa9e4066Sahrens 
312fa9e4066Sahrens 	dmu_tx_commit(tx);
313fa9e4066Sahrens 
314fa9e4066Sahrens 	return (dp);
315fa9e4066Sahrens }
316fa9e4066Sahrens 
317ce636f8bSMatthew Ahrens /*
318ce636f8bSMatthew Ahrens  * Account for the meta-objset space in its placeholder dsl_dir.
319ce636f8bSMatthew Ahrens  */
320ce636f8bSMatthew Ahrens void
321ce636f8bSMatthew Ahrens dsl_pool_mos_diduse_space(dsl_pool_t *dp,
322ce636f8bSMatthew Ahrens     int64_t used, int64_t comp, int64_t uncomp)
323ce636f8bSMatthew Ahrens {
324ce636f8bSMatthew Ahrens 	ASSERT3U(comp, ==, uncomp); /* it's all metadata */
325ce636f8bSMatthew Ahrens 	mutex_enter(&dp->dp_lock);
326ce636f8bSMatthew Ahrens 	dp->dp_mos_used_delta += used;
327ce636f8bSMatthew Ahrens 	dp->dp_mos_compressed_delta += comp;
328ce636f8bSMatthew Ahrens 	dp->dp_mos_uncompressed_delta += uncomp;
329ce636f8bSMatthew Ahrens 	mutex_exit(&dp->dp_lock);
330ce636f8bSMatthew Ahrens }
331ce636f8bSMatthew Ahrens 
332cde58dbcSMatthew Ahrens static int
333cde58dbcSMatthew Ahrens deadlist_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
334cde58dbcSMatthew Ahrens {
335cde58dbcSMatthew Ahrens 	dsl_deadlist_t *dl = arg;
33619b94df9SMatthew Ahrens 	dsl_pool_t *dp = dmu_objset_pool(dl->dl_os);
33719b94df9SMatthew Ahrens 	rw_enter(&dp->dp_config_rwlock, RW_READER);
338cde58dbcSMatthew Ahrens 	dsl_deadlist_insert(dl, bp, tx);
33919b94df9SMatthew Ahrens 	rw_exit(&dp->dp_config_rwlock);
340cde58dbcSMatthew Ahrens 	return (0);
341cde58dbcSMatthew Ahrens }
342cde58dbcSMatthew Ahrens 
343fa9e4066Sahrens void
344fa9e4066Sahrens dsl_pool_sync(dsl_pool_t *dp, uint64_t txg)
345fa9e4066Sahrens {
346c717a561Smaybee 	zio_t *zio;
347fa9e4066Sahrens 	dmu_tx_t *tx;
348c717a561Smaybee 	dsl_dir_t *dd;
349c717a561Smaybee 	dsl_dataset_t *ds;
350503ad85cSMatthew Ahrens 	objset_t *mos = dp->dp_meta_objset;
35105715f94SMark Maybee 	hrtime_t start, write_time;
35205715f94SMark Maybee 	uint64_t data_written;
353c717a561Smaybee 	int err;
354ce636f8bSMatthew Ahrens 	list_t synced_datasets;
355ce636f8bSMatthew Ahrens 
356ce636f8bSMatthew Ahrens 	list_create(&synced_datasets, sizeof (dsl_dataset_t),
357ce636f8bSMatthew Ahrens 	    offsetof(dsl_dataset_t, ds_synced_link));
358fa9e4066Sahrens 
3593f9d6ad7SLin Ling 	/*
3603f9d6ad7SLin Ling 	 * We need to copy dp_space_towrite() before doing
3613f9d6ad7SLin Ling 	 * dsl_sync_task_group_sync(), because
3623f9d6ad7SLin Ling 	 * dsl_dataset_snapshot_reserve_space() will increase
3633f9d6ad7SLin Ling 	 * dp_space_towrite but not actually write anything.
3643f9d6ad7SLin Ling 	 */
3653f9d6ad7SLin Ling 	data_written = dp->dp_space_towrite[txg & TXG_MASK];
3663f9d6ad7SLin Ling 
367fa9e4066Sahrens 	tx = dmu_tx_create_assigned(dp, txg);
368fa9e4066Sahrens 
36905715f94SMark Maybee 	dp->dp_read_overhead = 0;
3700fd90d51SMark Maybee 	start = gethrtime();
37114843421SMatthew Ahrens 
372c717a561Smaybee 	zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
373c717a561Smaybee 	while (ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) {
37414843421SMatthew Ahrens 		/*
37514843421SMatthew Ahrens 		 * We must not sync any non-MOS datasets twice, because
37614843421SMatthew Ahrens 		 * we may have taken a snapshot of them.  However, we
37714843421SMatthew Ahrens 		 * may sync newly-created datasets on pass 2.
37814843421SMatthew Ahrens 		 */
37914843421SMatthew Ahrens 		ASSERT(!list_link_active(&ds->ds_synced_link));
380ce636f8bSMatthew Ahrens 		list_insert_tail(&synced_datasets, ds);
381c717a561Smaybee 		dsl_dataset_sync(ds, zio, tx);
382c717a561Smaybee 	}
38305715f94SMark Maybee 	DTRACE_PROBE(pool_sync__1setup);
384c717a561Smaybee 	err = zio_wait(zio);
38514843421SMatthew Ahrens 
38605715f94SMark Maybee 	write_time = gethrtime() - start;
387c717a561Smaybee 	ASSERT(err == 0);
38805715f94SMark Maybee 	DTRACE_PROBE(pool_sync__2rootzio);
389c717a561Smaybee 
390ce636f8bSMatthew Ahrens 	/*
391ce636f8bSMatthew Ahrens 	 * After the data blocks have been written (ensured by the zio_wait()
392ce636f8bSMatthew Ahrens 	 * above), update the user/group space accounting.
393ce636f8bSMatthew Ahrens 	 */
394ce636f8bSMatthew Ahrens 	for (ds = list_head(&synced_datasets); ds;
395ce636f8bSMatthew Ahrens 	    ds = list_next(&synced_datasets, ds))
3960a586ceaSMark Shellenbaum 		dmu_objset_do_userquota_updates(ds->ds_objset, tx);
39714843421SMatthew Ahrens 
39814843421SMatthew Ahrens 	/*
39914843421SMatthew Ahrens 	 * Sync the datasets again to push out the changes due to
4003f9d6ad7SLin Ling 	 * userspace updates.  This must be done before we process the
401ce636f8bSMatthew Ahrens 	 * sync tasks, so that any snapshots will have the correct
402ce636f8bSMatthew Ahrens 	 * user accounting information (and we won't get confused
403ce636f8bSMatthew Ahrens 	 * about which blocks are part of the snapshot).
40414843421SMatthew Ahrens 	 */
40514843421SMatthew Ahrens 	zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
40614843421SMatthew Ahrens 	while (ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) {
40714843421SMatthew Ahrens 		ASSERT(list_link_active(&ds->ds_synced_link));
40814843421SMatthew Ahrens 		dmu_buf_rele(ds->ds_dbuf, ds);
40914843421SMatthew Ahrens 		dsl_dataset_sync(ds, zio, tx);
41014843421SMatthew Ahrens 	}
41114843421SMatthew Ahrens 	err = zio_wait(zio);
41214843421SMatthew Ahrens 
413b24ab676SJeff Bonwick 	/*
414ce636f8bSMatthew Ahrens 	 * Now that the datasets have been completely synced, we can
415ce636f8bSMatthew Ahrens 	 * clean up our in-memory structures accumulated while syncing:
416ce636f8bSMatthew Ahrens 	 *
417ce636f8bSMatthew Ahrens 	 *  - move dead blocks from the pending deadlist to the on-disk deadlist
418ce636f8bSMatthew Ahrens 	 *  - clean up zil records
419ce636f8bSMatthew Ahrens 	 *  - release hold from dsl_dataset_dirty()
420b24ab676SJeff Bonwick 	 */
421ce636f8bSMatthew Ahrens 	while (ds = list_remove_head(&synced_datasets)) {
422ce636f8bSMatthew Ahrens 		objset_t *os = ds->ds_objset;
423cde58dbcSMatthew Ahrens 		bplist_iterate(&ds->ds_pending_deadlist,
424cde58dbcSMatthew Ahrens 		    deadlist_enqueue_cb, &ds->ds_deadlist, tx);
425ce636f8bSMatthew Ahrens 		ASSERT(!dmu_objset_is_dirty(os, txg));
426ce636f8bSMatthew Ahrens 		dmu_buf_rele(ds->ds_dbuf, ds);
427cde58dbcSMatthew Ahrens 	}
428b24ab676SJeff Bonwick 
42905715f94SMark Maybee 	start = gethrtime();
430c717a561Smaybee 	while (dd = txg_list_remove(&dp->dp_dirty_dirs, txg))
431c717a561Smaybee 		dsl_dir_sync(dd, tx);
43205715f94SMark Maybee 	write_time += gethrtime() - start;
433fa9e4066Sahrens 
434ce636f8bSMatthew Ahrens 	/*
435ce636f8bSMatthew Ahrens 	 * The MOS's space is accounted for in the pool/$MOS
436ce636f8bSMatthew Ahrens 	 * (dp_mos_dir).  We can't modify the mos while we're syncing
437ce636f8bSMatthew Ahrens 	 * it, so we remember the deltas and apply them here.
438ce636f8bSMatthew Ahrens 	 */
439ce636f8bSMatthew Ahrens 	if (dp->dp_mos_used_delta != 0 || dp->dp_mos_compressed_delta != 0 ||
440ce636f8bSMatthew Ahrens 	    dp->dp_mos_uncompressed_delta != 0) {
441ce636f8bSMatthew Ahrens 		dsl_dir_diduse_space(dp->dp_mos_dir, DD_USED_HEAD,
442ce636f8bSMatthew Ahrens 		    dp->dp_mos_used_delta,
443ce636f8bSMatthew Ahrens 		    dp->dp_mos_compressed_delta,
444ce636f8bSMatthew Ahrens 		    dp->dp_mos_uncompressed_delta, tx);
445ce636f8bSMatthew Ahrens 		dp->dp_mos_used_delta = 0;
446ce636f8bSMatthew Ahrens 		dp->dp_mos_compressed_delta = 0;
447ce636f8bSMatthew Ahrens 		dp->dp_mos_uncompressed_delta = 0;
448ce636f8bSMatthew Ahrens 	}
449ce636f8bSMatthew Ahrens 
45005715f94SMark Maybee 	start = gethrtime();
451503ad85cSMatthew Ahrens 	if (list_head(&mos->os_dirty_dnodes[txg & TXG_MASK]) != NULL ||
452503ad85cSMatthew Ahrens 	    list_head(&mos->os_free_dnodes[txg & TXG_MASK]) != NULL) {
453c717a561Smaybee 		zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
454503ad85cSMatthew Ahrens 		dmu_objset_sync(mos, zio, tx);
455c717a561Smaybee 		err = zio_wait(zio);
456c717a561Smaybee 		ASSERT(err == 0);
457fa9e4066Sahrens 		dprintf_bp(&dp->dp_meta_rootbp, "meta objset rootbp is %s", "");
458fa9e4066Sahrens 		spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
459fa9e4066Sahrens 	}
46005715f94SMark Maybee 	write_time += gethrtime() - start;
46105715f94SMark Maybee 	DTRACE_PROBE2(pool_sync__4io, hrtime_t, write_time,
46205715f94SMark Maybee 	    hrtime_t, dp->dp_read_overhead);
46305715f94SMark Maybee 	write_time -= dp->dp_read_overhead;
464fa9e4066Sahrens 
465ce636f8bSMatthew Ahrens 	/*
466ce636f8bSMatthew Ahrens 	 * If we modify a dataset in the same txg that we want to destroy it,
467ce636f8bSMatthew Ahrens 	 * its dsl_dir's dd_dbuf will be dirty, and thus have a hold on it.
468ce636f8bSMatthew Ahrens 	 * dsl_dir_destroy_check() will fail if there are unexpected holds.
469ce636f8bSMatthew Ahrens 	 * Therefore, we want to sync the MOS (thus syncing the dd_dbuf
470ce636f8bSMatthew Ahrens 	 * and clearing the hold on it) before we process the sync_tasks.
471ce636f8bSMatthew Ahrens 	 * The MOS data dirtied by the sync_tasks will be synced on the next
472ce636f8bSMatthew Ahrens 	 * pass.
473ce636f8bSMatthew Ahrens 	 */
474ce636f8bSMatthew Ahrens 	DTRACE_PROBE(pool_sync__3task);
475ce636f8bSMatthew Ahrens 	if (!txg_list_empty(&dp->dp_sync_tasks, txg)) {
476ce636f8bSMatthew Ahrens 		dsl_sync_task_group_t *dstg;
477ce636f8bSMatthew Ahrens 		/*
478ce636f8bSMatthew Ahrens 		 * No more sync tasks should have been added while we
479ce636f8bSMatthew Ahrens 		 * were syncing.
480ce636f8bSMatthew Ahrens 		 */
481ce636f8bSMatthew Ahrens 		ASSERT(spa_sync_pass(dp->dp_spa) == 1);
482ce636f8bSMatthew Ahrens 		while (dstg = txg_list_remove(&dp->dp_sync_tasks, txg))
483ce636f8bSMatthew Ahrens 			dsl_sync_task_group_sync(dstg, tx);
484ce636f8bSMatthew Ahrens 	}
485ce636f8bSMatthew Ahrens 
486fa9e4066Sahrens 	dmu_tx_commit(tx);
48705715f94SMark Maybee 
48805715f94SMark Maybee 	dp->dp_space_towrite[txg & TXG_MASK] = 0;
48905715f94SMark Maybee 	ASSERT(dp->dp_tempreserved[txg & TXG_MASK] == 0);
49005715f94SMark Maybee 
49105715f94SMark Maybee 	/*
49205715f94SMark Maybee 	 * If the write limit max has not been explicitly set, set it
49305715f94SMark Maybee 	 * to a fraction of available physical memory (default 1/8th).
49405715f94SMark Maybee 	 * Note that we must inflate the limit because the spa
49505715f94SMark Maybee 	 * inflates write sizes to account for data replication.
49605715f94SMark Maybee 	 * Check this each sync phase to catch changing memory size.
49705715f94SMark Maybee 	 */
49805715f94SMark Maybee 	if (physmem != old_physmem && zfs_write_limit_shift) {
49905715f94SMark Maybee 		mutex_enter(&zfs_write_limit_lock);
50005715f94SMark Maybee 		old_physmem = physmem;
50105715f94SMark Maybee 		zfs_write_limit_max = ptob(physmem) >> zfs_write_limit_shift;
50205715f94SMark Maybee 		zfs_write_limit_inflated = MAX(zfs_write_limit_min,
50305715f94SMark Maybee 		    spa_get_asize(dp->dp_spa, zfs_write_limit_max));
50405715f94SMark Maybee 		mutex_exit(&zfs_write_limit_lock);
50505715f94SMark Maybee 	}
50605715f94SMark Maybee 
50705715f94SMark Maybee 	/*
50805715f94SMark Maybee 	 * Attempt to keep the sync time consistent by adjusting the
50905715f94SMark Maybee 	 * amount of write traffic allowed into each transaction group.
51005715f94SMark Maybee 	 * Weight the throughput calculation towards the current value:
51105715f94SMark Maybee 	 * 	thru = 3/4 old_thru + 1/4 new_thru
512fb5dd802SLin Ling 	 *
513fb5dd802SLin Ling 	 * Note: write_time is in nanosecs, so write_time/MICROSEC
514fb5dd802SLin Ling 	 * yields millisecs
51505715f94SMark Maybee 	 */
51605715f94SMark Maybee 	ASSERT(zfs_write_limit_min > 0);
517fb5dd802SLin Ling 	if (data_written > zfs_write_limit_min / 8 && write_time > MICROSEC) {
518fb5dd802SLin Ling 		uint64_t throughput = data_written / (write_time / MICROSEC);
519fb5dd802SLin Ling 
52005715f94SMark Maybee 		if (dp->dp_throughput)
52105715f94SMark Maybee 			dp->dp_throughput = throughput / 4 +
52205715f94SMark Maybee 			    3 * dp->dp_throughput / 4;
52305715f94SMark Maybee 		else
52405715f94SMark Maybee 			dp->dp_throughput = throughput;
52505715f94SMark Maybee 		dp->dp_write_limit = MIN(zfs_write_limit_inflated,
52605715f94SMark Maybee 		    MAX(zfs_write_limit_min,
527fb5dd802SLin Ling 		    dp->dp_throughput * zfs_txg_synctime_ms));
52805715f94SMark Maybee 	}
529fa9e4066Sahrens }
530fa9e4066Sahrens 
531fa9e4066Sahrens void
532b24ab676SJeff Bonwick dsl_pool_sync_done(dsl_pool_t *dp, uint64_t txg)
533fa9e4066Sahrens {
534ce636f8bSMatthew Ahrens 	zilog_t *zilog;
535fa9e4066Sahrens 	dsl_dataset_t *ds;
536fa9e4066Sahrens 
537ce636f8bSMatthew Ahrens 	while (zilog = txg_list_remove(&dp->dp_dirty_zilogs, txg)) {
538ce636f8bSMatthew Ahrens 		ds = dmu_objset_ds(zilog->zl_os);
539ce636f8bSMatthew Ahrens 		zil_clean(zilog, txg);
540ce636f8bSMatthew Ahrens 		ASSERT(!dmu_objset_is_dirty(zilog->zl_os, txg));
541ce636f8bSMatthew Ahrens 		dmu_buf_rele(ds->ds_dbuf, zilog);
542fa9e4066Sahrens 	}
543b24ab676SJeff Bonwick 	ASSERT(!dmu_objset_is_dirty(dp->dp_meta_objset, txg));
544fa9e4066Sahrens }
545fa9e4066Sahrens 
546c717a561Smaybee /*
547c717a561Smaybee  * TRUE if the current thread is the tx_sync_thread or if we
548c717a561Smaybee  * are being called from SPA context during pool initialization.
549c717a561Smaybee  */
550fa9e4066Sahrens int
551fa9e4066Sahrens dsl_pool_sync_context(dsl_pool_t *dp)
552fa9e4066Sahrens {
553fa9e4066Sahrens 	return (curthread == dp->dp_tx.tx_sync_thread ||
554ad135b5dSChristopher Siden 	    spa_is_initializing(dp->dp_spa));
555fa9e4066Sahrens }
556fa9e4066Sahrens 
557fa9e4066Sahrens uint64_t
558fa9e4066Sahrens dsl_pool_adjustedsize(dsl_pool_t *dp, boolean_t netfree)
559fa9e4066Sahrens {
560fa9e4066Sahrens 	uint64_t space, resv;
561fa9e4066Sahrens 
562fa9e4066Sahrens 	/*
56344cd46caSbillm 	 * Reserve about 1.6% (1/64), or at least 32MB, for allocation
564fa9e4066Sahrens 	 * efficiency.
565fa9e4066Sahrens 	 * XXX The intent log is not accounted for, so it must fit
566fa9e4066Sahrens 	 * within this slop.
567fa9e4066Sahrens 	 *
568fa9e4066Sahrens 	 * If we're trying to assess whether it's OK to do a free,
569fa9e4066Sahrens 	 * cut the reservation in half to allow forward progress
570fa9e4066Sahrens 	 * (e.g. make it possible to rm(1) files from a full pool).
571fa9e4066Sahrens 	 */
572485bbbf5SGeorge Wilson 	space = spa_get_dspace(dp->dp_spa);
57344cd46caSbillm 	resv = MAX(space >> 6, SPA_MINDEVSIZE >> 1);
574fa9e4066Sahrens 	if (netfree)
575fa9e4066Sahrens 		resv >>= 1;
576fa9e4066Sahrens 
577fa9e4066Sahrens 	return (space - resv);
578fa9e4066Sahrens }
5791ab7f2deSmaybee 
5801ab7f2deSmaybee int
5811ab7f2deSmaybee dsl_pool_tempreserve_space(dsl_pool_t *dp, uint64_t space, dmu_tx_t *tx)
5821ab7f2deSmaybee {
5831ab7f2deSmaybee 	uint64_t reserved = 0;
5841ab7f2deSmaybee 	uint64_t write_limit = (zfs_write_limit_override ?
5851ab7f2deSmaybee 	    zfs_write_limit_override : dp->dp_write_limit);
5861ab7f2deSmaybee 
5871ab7f2deSmaybee 	if (zfs_no_write_throttle) {
588c5904d13Seschrock 		atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK],
589c5904d13Seschrock 		    space);
5901ab7f2deSmaybee 		return (0);
5911ab7f2deSmaybee 	}
5921ab7f2deSmaybee 
5931ab7f2deSmaybee 	/*
5941ab7f2deSmaybee 	 * Check to see if we have exceeded the maximum allowed IO for
5951ab7f2deSmaybee 	 * this transaction group.  We can do this without locks since
5961ab7f2deSmaybee 	 * a little slop here is ok.  Note that we do the reserved check
5971ab7f2deSmaybee 	 * with only half the requested reserve: this is because the
5981ab7f2deSmaybee 	 * reserve requests are worst-case, and we really don't want to
5991ab7f2deSmaybee 	 * throttle based off of worst-case estimates.
6001ab7f2deSmaybee 	 */
6011ab7f2deSmaybee 	if (write_limit > 0) {
6021ab7f2deSmaybee 		reserved = dp->dp_space_towrite[tx->tx_txg & TXG_MASK]
6031ab7f2deSmaybee 		    + dp->dp_tempreserved[tx->tx_txg & TXG_MASK] / 2;
6041ab7f2deSmaybee 
6051ab7f2deSmaybee 		if (reserved && reserved > write_limit)
6061ab7f2deSmaybee 			return (ERESTART);
6071ab7f2deSmaybee 	}
6081ab7f2deSmaybee 
6091ab7f2deSmaybee 	atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], space);
6101ab7f2deSmaybee 
6111ab7f2deSmaybee 	/*
6121ab7f2deSmaybee 	 * If this transaction group is over 7/8ths capacity, delay
6131ab7f2deSmaybee 	 * the caller 1 clock tick.  This will slow down the "fill"
6141ab7f2deSmaybee 	 * rate until the sync process can catch up with us.
6151ab7f2deSmaybee 	 */
616e8397a2bSgw 	if (reserved && reserved > (write_limit - (write_limit >> 3)))
6171ab7f2deSmaybee 		txg_delay(dp, tx->tx_txg, 1);
6181ab7f2deSmaybee 
6191ab7f2deSmaybee 	return (0);
6201ab7f2deSmaybee }
6211ab7f2deSmaybee 
6221ab7f2deSmaybee void
6231ab7f2deSmaybee dsl_pool_tempreserve_clear(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx)
6241ab7f2deSmaybee {
6251ab7f2deSmaybee 	ASSERT(dp->dp_tempreserved[tx->tx_txg & TXG_MASK] >= space);
6261ab7f2deSmaybee 	atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], -space);
6271ab7f2deSmaybee }
6281ab7f2deSmaybee 
6291ab7f2deSmaybee void
6301ab7f2deSmaybee dsl_pool_memory_pressure(dsl_pool_t *dp)
6311ab7f2deSmaybee {
6321ab7f2deSmaybee 	uint64_t space_inuse = 0;
6331ab7f2deSmaybee 	int i;
6341ab7f2deSmaybee 
6351ab7f2deSmaybee 	if (dp->dp_write_limit == zfs_write_limit_min)
6361ab7f2deSmaybee 		return;
6371ab7f2deSmaybee 
6381ab7f2deSmaybee 	for (i = 0; i < TXG_SIZE; i++) {
6391ab7f2deSmaybee 		space_inuse += dp->dp_space_towrite[i];
6401ab7f2deSmaybee 		space_inuse += dp->dp_tempreserved[i];
6411ab7f2deSmaybee 	}
6421ab7f2deSmaybee 	dp->dp_write_limit = MAX(zfs_write_limit_min,
6431ab7f2deSmaybee 	    MIN(dp->dp_write_limit, space_inuse / 4));
6441ab7f2deSmaybee }
6451ab7f2deSmaybee 
6461ab7f2deSmaybee void
6471ab7f2deSmaybee dsl_pool_willuse_space(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx)
6481ab7f2deSmaybee {
6491ab7f2deSmaybee 	if (space > 0) {
6501ab7f2deSmaybee 		mutex_enter(&dp->dp_lock);
6511ab7f2deSmaybee 		dp->dp_space_towrite[tx->tx_txg & TXG_MASK] += space;
6521ab7f2deSmaybee 		mutex_exit(&dp->dp_lock);
6531ab7f2deSmaybee 	}
6541ab7f2deSmaybee }
655088f3894Sahrens 
656088f3894Sahrens /* ARGSUSED */
657088f3894Sahrens static int
658088f3894Sahrens upgrade_clones_cb(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
659088f3894Sahrens {
660088f3894Sahrens 	dmu_tx_t *tx = arg;
661088f3894Sahrens 	dsl_dataset_t *ds, *prev = NULL;
662088f3894Sahrens 	int err;
663088f3894Sahrens 	dsl_pool_t *dp = spa_get_dsl(spa);
664088f3894Sahrens 
665088f3894Sahrens 	err = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds);
666088f3894Sahrens 	if (err)
667088f3894Sahrens 		return (err);
668088f3894Sahrens 
669088f3894Sahrens 	while (ds->ds_phys->ds_prev_snap_obj != 0) {
670088f3894Sahrens 		err = dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
671088f3894Sahrens 		    FTAG, &prev);
672088f3894Sahrens 		if (err) {
673088f3894Sahrens 			dsl_dataset_rele(ds, FTAG);
674088f3894Sahrens 			return (err);
675088f3894Sahrens 		}
676088f3894Sahrens 
677088f3894Sahrens 		if (prev->ds_phys->ds_next_snap_obj != ds->ds_object)
678088f3894Sahrens 			break;
679088f3894Sahrens 		dsl_dataset_rele(ds, FTAG);
680088f3894Sahrens 		ds = prev;
681088f3894Sahrens 		prev = NULL;
682088f3894Sahrens 	}
683088f3894Sahrens 
684088f3894Sahrens 	if (prev == NULL) {
685088f3894Sahrens 		prev = dp->dp_origin_snap;
686088f3894Sahrens 
687088f3894Sahrens 		/*
688088f3894Sahrens 		 * The $ORIGIN can't have any data, or the accounting
689088f3894Sahrens 		 * will be wrong.
690088f3894Sahrens 		 */
691088f3894Sahrens 		ASSERT(prev->ds_phys->ds_bp.blk_birth == 0);
692088f3894Sahrens 
693088f3894Sahrens 		/* The origin doesn't get attached to itself */
694088f3894Sahrens 		if (ds->ds_object == prev->ds_object) {
695088f3894Sahrens 			dsl_dataset_rele(ds, FTAG);
696088f3894Sahrens 			return (0);
697088f3894Sahrens 		}
698088f3894Sahrens 
699088f3894Sahrens 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
700088f3894Sahrens 		ds->ds_phys->ds_prev_snap_obj = prev->ds_object;
701088f3894Sahrens 		ds->ds_phys->ds_prev_snap_txg = prev->ds_phys->ds_creation_txg;
702088f3894Sahrens 
703088f3894Sahrens 		dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
704088f3894Sahrens 		ds->ds_dir->dd_phys->dd_origin_obj = prev->ds_object;
705088f3894Sahrens 
706088f3894Sahrens 		dmu_buf_will_dirty(prev->ds_dbuf, tx);
707088f3894Sahrens 		prev->ds_phys->ds_num_children++;
708088f3894Sahrens 
709088f3894Sahrens 		if (ds->ds_phys->ds_next_snap_obj == 0) {
710088f3894Sahrens 			ASSERT(ds->ds_prev == NULL);
711088f3894Sahrens 			VERIFY(0 == dsl_dataset_hold_obj(dp,
712088f3894Sahrens 			    ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev));
713088f3894Sahrens 		}
714088f3894Sahrens 	}
715088f3894Sahrens 
716088f3894Sahrens 	ASSERT(ds->ds_dir->dd_phys->dd_origin_obj == prev->ds_object);
717088f3894Sahrens 	ASSERT(ds->ds_phys->ds_prev_snap_obj == prev->ds_object);
718088f3894Sahrens 
719088f3894Sahrens 	if (prev->ds_phys->ds_next_clones_obj == 0) {
720c33e334fSMatthew Ahrens 		dmu_buf_will_dirty(prev->ds_dbuf, tx);
721088f3894Sahrens 		prev->ds_phys->ds_next_clones_obj =
722088f3894Sahrens 		    zap_create(dp->dp_meta_objset,
723088f3894Sahrens 		    DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
724088f3894Sahrens 	}
725088f3894Sahrens 	VERIFY(0 == zap_add_int(dp->dp_meta_objset,
726088f3894Sahrens 	    prev->ds_phys->ds_next_clones_obj, ds->ds_object, tx));
727088f3894Sahrens 
728088f3894Sahrens 	dsl_dataset_rele(ds, FTAG);
729088f3894Sahrens 	if (prev != dp->dp_origin_snap)
730088f3894Sahrens 		dsl_dataset_rele(prev, FTAG);
731088f3894Sahrens 	return (0);
732088f3894Sahrens }
733088f3894Sahrens 
734088f3894Sahrens void
735088f3894Sahrens dsl_pool_upgrade_clones(dsl_pool_t *dp, dmu_tx_t *tx)
736088f3894Sahrens {
737088f3894Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
738088f3894Sahrens 	ASSERT(dp->dp_origin_snap != NULL);
739088f3894Sahrens 
740b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_objset_find_spa(dp->dp_spa, NULL, upgrade_clones_cb,
741c33e334fSMatthew Ahrens 	    tx, DS_FIND_CHILDREN));
742088f3894Sahrens }
743088f3894Sahrens 
744cde58dbcSMatthew Ahrens /* ARGSUSED */
745cde58dbcSMatthew Ahrens static int
746cde58dbcSMatthew Ahrens upgrade_dir_clones_cb(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
747cde58dbcSMatthew Ahrens {
748cde58dbcSMatthew Ahrens 	dmu_tx_t *tx = arg;
749cde58dbcSMatthew Ahrens 	dsl_dataset_t *ds;
750cde58dbcSMatthew Ahrens 	dsl_pool_t *dp = spa_get_dsl(spa);
751cde58dbcSMatthew Ahrens 	objset_t *mos = dp->dp_meta_objset;
752cde58dbcSMatthew Ahrens 
753b420f3adSRichard Lowe 	VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
754cde58dbcSMatthew Ahrens 
755cde58dbcSMatthew Ahrens 	if (ds->ds_dir->dd_phys->dd_origin_obj) {
756cde58dbcSMatthew Ahrens 		dsl_dataset_t *origin;
757cde58dbcSMatthew Ahrens 
758b420f3adSRichard Lowe 		VERIFY3U(0, ==, dsl_dataset_hold_obj(dp,
759cde58dbcSMatthew Ahrens 		    ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &origin));
760cde58dbcSMatthew Ahrens 
761cde58dbcSMatthew Ahrens 		if (origin->ds_dir->dd_phys->dd_clones == 0) {
762cde58dbcSMatthew Ahrens 			dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx);
763cde58dbcSMatthew Ahrens 			origin->ds_dir->dd_phys->dd_clones = zap_create(mos,
764cde58dbcSMatthew Ahrens 			    DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
765cde58dbcSMatthew Ahrens 		}
766cde58dbcSMatthew Ahrens 
767b420f3adSRichard Lowe 		VERIFY3U(0, ==, zap_add_int(dp->dp_meta_objset,
768cde58dbcSMatthew Ahrens 		    origin->ds_dir->dd_phys->dd_clones, dsobj, tx));
769cde58dbcSMatthew Ahrens 
770cde58dbcSMatthew Ahrens 		dsl_dataset_rele(origin, FTAG);
771cde58dbcSMatthew Ahrens 	}
772cde58dbcSMatthew Ahrens 
773cde58dbcSMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
774cde58dbcSMatthew Ahrens 	return (0);
775cde58dbcSMatthew Ahrens }
776cde58dbcSMatthew Ahrens 
777cde58dbcSMatthew Ahrens void
778cde58dbcSMatthew Ahrens dsl_pool_upgrade_dir_clones(dsl_pool_t *dp, dmu_tx_t *tx)
779cde58dbcSMatthew Ahrens {
780cde58dbcSMatthew Ahrens 	ASSERT(dmu_tx_is_syncing(tx));
781cde58dbcSMatthew Ahrens 	uint64_t obj;
782cde58dbcSMatthew Ahrens 
783cde58dbcSMatthew Ahrens 	(void) dsl_dir_create_sync(dp, dp->dp_root_dir, FREE_DIR_NAME, tx);
784cde58dbcSMatthew Ahrens 	VERIFY(0 == dsl_pool_open_special_dir(dp,
785cde58dbcSMatthew Ahrens 	    FREE_DIR_NAME, &dp->dp_free_dir));
786cde58dbcSMatthew Ahrens 
787cde58dbcSMatthew Ahrens 	/*
788cde58dbcSMatthew Ahrens 	 * We can't use bpobj_alloc(), because spa_version() still
789cde58dbcSMatthew Ahrens 	 * returns the old version, and we need a new-version bpobj with
790cde58dbcSMatthew Ahrens 	 * subobj support.  So call dmu_object_alloc() directly.
791cde58dbcSMatthew Ahrens 	 */
792cde58dbcSMatthew Ahrens 	obj = dmu_object_alloc(dp->dp_meta_objset, DMU_OT_BPOBJ,
793cde58dbcSMatthew Ahrens 	    SPA_MAXBLOCKSIZE, DMU_OT_BPOBJ_HDR, sizeof (bpobj_phys_t), tx);
794b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
795cde58dbcSMatthew Ahrens 	    DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj, tx));
796b420f3adSRichard Lowe 	VERIFY3U(0, ==, bpobj_open(&dp->dp_free_bpobj,
797cde58dbcSMatthew Ahrens 	    dp->dp_meta_objset, obj));
798cde58dbcSMatthew Ahrens 
799b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_objset_find_spa(dp->dp_spa, NULL,
800cde58dbcSMatthew Ahrens 	    upgrade_dir_clones_cb, tx, DS_FIND_CHILDREN));
801cde58dbcSMatthew Ahrens }
802cde58dbcSMatthew Ahrens 
803088f3894Sahrens void
804088f3894Sahrens dsl_pool_create_origin(dsl_pool_t *dp, dmu_tx_t *tx)
805088f3894Sahrens {
806088f3894Sahrens 	uint64_t dsobj;
807088f3894Sahrens 	dsl_dataset_t *ds;
808088f3894Sahrens 
809088f3894Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
810088f3894Sahrens 	ASSERT(dp->dp_origin_snap == NULL);
811088f3894Sahrens 
812088f3894Sahrens 	/* create the origin dir, ds, & snap-ds */
813088f3894Sahrens 	rw_enter(&dp->dp_config_rwlock, RW_WRITER);
814088f3894Sahrens 	dsobj = dsl_dataset_create_sync(dp->dp_root_dir, ORIGIN_DIR_NAME,
815088f3894Sahrens 	    NULL, 0, kcred, tx);
816088f3894Sahrens 	VERIFY(0 == dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
8173f9d6ad7SLin Ling 	dsl_dataset_snapshot_sync(ds, ORIGIN_DIR_NAME, tx);
818088f3894Sahrens 	VERIFY(0 == dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
819088f3894Sahrens 	    dp, &dp->dp_origin_snap));
820088f3894Sahrens 	dsl_dataset_rele(ds, FTAG);
821088f3894Sahrens 	rw_exit(&dp->dp_config_rwlock);
822088f3894Sahrens }
8239d3574bfSNeil Perrin 
8249d3574bfSNeil Perrin taskq_t *
8259d3574bfSNeil Perrin dsl_pool_vnrele_taskq(dsl_pool_t *dp)
8269d3574bfSNeil Perrin {
8279d3574bfSNeil Perrin 	return (dp->dp_vnrele_taskq);
8289d3574bfSNeil Perrin }
829ca45db41SChris Kirby 
830ca45db41SChris Kirby /*
831ca45db41SChris Kirby  * Walk through the pool-wide zap object of temporary snapshot user holds
832ca45db41SChris Kirby  * and release them.
833ca45db41SChris Kirby  */
834ca45db41SChris Kirby void
835ca45db41SChris Kirby dsl_pool_clean_tmp_userrefs(dsl_pool_t *dp)
836ca45db41SChris Kirby {
837ca45db41SChris Kirby 	zap_attribute_t za;
838ca45db41SChris Kirby 	zap_cursor_t zc;
839ca45db41SChris Kirby 	objset_t *mos = dp->dp_meta_objset;
840ca45db41SChris Kirby 	uint64_t zapobj = dp->dp_tmp_userrefs_obj;
841ca45db41SChris Kirby 
842ca45db41SChris Kirby 	if (zapobj == 0)
843ca45db41SChris Kirby 		return;
844ca45db41SChris Kirby 	ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
845ca45db41SChris Kirby 
846ca45db41SChris Kirby 	for (zap_cursor_init(&zc, mos, zapobj);
847ca45db41SChris Kirby 	    zap_cursor_retrieve(&zc, &za) == 0;
848ca45db41SChris Kirby 	    zap_cursor_advance(&zc)) {
849ca45db41SChris Kirby 		char *htag;
850ca45db41SChris Kirby 		uint64_t dsobj;
851ca45db41SChris Kirby 
852ca45db41SChris Kirby 		htag = strchr(za.za_name, '-');
853ca45db41SChris Kirby 		*htag = '\0';
854ca45db41SChris Kirby 		++htag;
855ca45db41SChris Kirby 		dsobj = strtonum(za.za_name, NULL);
856a7f53a56SChris Kirby 		(void) dsl_dataset_user_release_tmp(dp, dsobj, htag, B_FALSE);
857ca45db41SChris Kirby 	}
858ca45db41SChris Kirby 	zap_cursor_fini(&zc);
859ca45db41SChris Kirby }
860ca45db41SChris Kirby 
861ca45db41SChris Kirby /*
862ca45db41SChris Kirby  * Create the pool-wide zap object for storing temporary snapshot holds.
863ca45db41SChris Kirby  */
864ca45db41SChris Kirby void
865ca45db41SChris Kirby dsl_pool_user_hold_create_obj(dsl_pool_t *dp, dmu_tx_t *tx)
866ca45db41SChris Kirby {
867ca45db41SChris Kirby 	objset_t *mos = dp->dp_meta_objset;
868ca45db41SChris Kirby 
869ca45db41SChris Kirby 	ASSERT(dp->dp_tmp_userrefs_obj == 0);
870ca45db41SChris Kirby 	ASSERT(dmu_tx_is_syncing(tx));
871ca45db41SChris Kirby 
872ad135b5dSChristopher Siden 	dp->dp_tmp_userrefs_obj = zap_create_link(mos, DMU_OT_USERREFS,
873ad135b5dSChristopher Siden 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_TMP_USERREFS, tx);
874ca45db41SChris Kirby }
875ca45db41SChris Kirby 
876ca45db41SChris Kirby static int
877ca45db41SChris Kirby dsl_pool_user_hold_rele_impl(dsl_pool_t *dp, uint64_t dsobj,
87815508ac0SChris Kirby     const char *tag, uint64_t *now, dmu_tx_t *tx, boolean_t holding)
879ca45db41SChris Kirby {
880ca45db41SChris Kirby 	objset_t *mos = dp->dp_meta_objset;
881ca45db41SChris Kirby 	uint64_t zapobj = dp->dp_tmp_userrefs_obj;
882ca45db41SChris Kirby 	char *name;
883ca45db41SChris Kirby 	int error;
884ca45db41SChris Kirby 
885ca45db41SChris Kirby 	ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
886ca45db41SChris Kirby 	ASSERT(dmu_tx_is_syncing(tx));
887ca45db41SChris Kirby 
888ca45db41SChris Kirby 	/*
889ca45db41SChris Kirby 	 * If the pool was created prior to SPA_VERSION_USERREFS, the
890ca45db41SChris Kirby 	 * zap object for temporary holds might not exist yet.
891ca45db41SChris Kirby 	 */
892ca45db41SChris Kirby 	if (zapobj == 0) {
893ca45db41SChris Kirby 		if (holding) {
894ca45db41SChris Kirby 			dsl_pool_user_hold_create_obj(dp, tx);
895ca45db41SChris Kirby 			zapobj = dp->dp_tmp_userrefs_obj;
896ca45db41SChris Kirby 		} else {
897ca45db41SChris Kirby 			return (ENOENT);
898ca45db41SChris Kirby 		}
899ca45db41SChris Kirby 	}
900ca45db41SChris Kirby 
901ca45db41SChris Kirby 	name = kmem_asprintf("%llx-%s", (u_longlong_t)dsobj, tag);
902ca45db41SChris Kirby 	if (holding)
90315508ac0SChris Kirby 		error = zap_add(mos, zapobj, name, 8, 1, now, tx);
904ca45db41SChris Kirby 	else
905ca45db41SChris Kirby 		error = zap_remove(mos, zapobj, name, tx);
906ca45db41SChris Kirby 	strfree(name);
907ca45db41SChris Kirby 
908ca45db41SChris Kirby 	return (error);
909ca45db41SChris Kirby }
910ca45db41SChris Kirby 
911ca45db41SChris Kirby /*
912ca45db41SChris Kirby  * Add a temporary hold for the given dataset object and tag.
913ca45db41SChris Kirby  */
914ca45db41SChris Kirby int
915ca45db41SChris Kirby dsl_pool_user_hold(dsl_pool_t *dp, uint64_t dsobj, const char *tag,
91615508ac0SChris Kirby     uint64_t *now, dmu_tx_t *tx)
917ca45db41SChris Kirby {
91815508ac0SChris Kirby 	return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, now, tx, B_TRUE));
919ca45db41SChris Kirby }
920ca45db41SChris Kirby 
921ca45db41SChris Kirby /*
922ca45db41SChris Kirby  * Release a temporary hold for the given dataset object and tag.
923ca45db41SChris Kirby  */
924ca45db41SChris Kirby int
925ca45db41SChris Kirby dsl_pool_user_release(dsl_pool_t *dp, uint64_t dsobj, const char *tag,
926ca45db41SChris Kirby     dmu_tx_t *tx)
927ca45db41SChris Kirby {
928ca45db41SChris Kirby 	return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, NULL,
929ca45db41SChris Kirby 	    tx, B_FALSE));
930ca45db41SChris Kirby }
931