xref: /illumos-gate/usr/src/uts/common/fs/zfs/dsl_pool.c (revision ad135b5d644628e791c3188a6ecbd9c257961ef8)
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.
23*ad135b5dSChristopher 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>
43*ad135b5dSChristopher Siden #include <sys/bptree.h>
44*ad135b5dSChristopher Siden #include <sys/zfeature.h>
45fa9e4066Sahrens 
461ab7f2deSmaybee int zfs_no_write_throttle = 0;
4705715f94SMark Maybee int zfs_write_limit_shift = 3;			/* 1/8th of physical memory */
4844ecc532SGeorge Wilson int zfs_txg_synctime_ms = 1000;		/* target millisecs to sync a txg */
4905715f94SMark Maybee 
5005715f94SMark Maybee uint64_t zfs_write_limit_min = 32 << 20;	/* min write limit is 32MB */
5105715f94SMark Maybee uint64_t zfs_write_limit_max = 0;		/* max data payload per txg */
5205715f94SMark Maybee uint64_t zfs_write_limit_inflated = 0;
531ab7f2deSmaybee uint64_t zfs_write_limit_override = 0;
541ab7f2deSmaybee 
5505715f94SMark Maybee kmutex_t zfs_write_limit_lock;
5605715f94SMark Maybee 
5705715f94SMark Maybee static pgcnt_t old_physmem = 0;
58088f3894Sahrens 
593f9d6ad7SLin Ling int
60088f3894Sahrens dsl_pool_open_special_dir(dsl_pool_t *dp, const char *name, dsl_dir_t **ddp)
61fa9e4066Sahrens {
62fa9e4066Sahrens 	uint64_t obj;
63fa9e4066Sahrens 	int err;
64fa9e4066Sahrens 
65fa9e4066Sahrens 	err = zap_lookup(dp->dp_meta_objset,
66fa9e4066Sahrens 	    dp->dp_root_dir->dd_phys->dd_child_dir_zapobj,
67088f3894Sahrens 	    name, sizeof (obj), 1, &obj);
68ea8dc4b6Seschrock 	if (err)
69ea8dc4b6Seschrock 		return (err);
70fa9e4066Sahrens 
71088f3894Sahrens 	return (dsl_dir_open_obj(dp, obj, name, dp, ddp));
72fa9e4066Sahrens }
73fa9e4066Sahrens 
74fa9e4066Sahrens static dsl_pool_t *
75fa9e4066Sahrens dsl_pool_open_impl(spa_t *spa, uint64_t txg)
76fa9e4066Sahrens {
77fa9e4066Sahrens 	dsl_pool_t *dp;
78fa9e4066Sahrens 	blkptr_t *bp = spa_get_rootblkptr(spa);
79fa9e4066Sahrens 
80fa9e4066Sahrens 	dp = kmem_zalloc(sizeof (dsl_pool_t), KM_SLEEP);
81fa9e4066Sahrens 	dp->dp_spa = spa;
82fa9e4066Sahrens 	dp->dp_meta_rootbp = *bp;
835ad82045Snd 	rw_init(&dp->dp_config_rwlock, NULL, RW_DEFAULT, NULL);
841ab7f2deSmaybee 	dp->dp_write_limit = zfs_write_limit_min;
85fa9e4066Sahrens 	txg_init(dp, txg);
86fa9e4066Sahrens 
87fa9e4066Sahrens 	txg_list_create(&dp->dp_dirty_datasets,
88fa9e4066Sahrens 	    offsetof(dsl_dataset_t, ds_dirty_link));
89fa9e4066Sahrens 	txg_list_create(&dp->dp_dirty_dirs,
90fa9e4066Sahrens 	    offsetof(dsl_dir_t, dd_dirty_link));
911d452cf5Sahrens 	txg_list_create(&dp->dp_sync_tasks,
921d452cf5Sahrens 	    offsetof(dsl_sync_task_group_t, dstg_node));
933cb34c60Sahrens 	list_create(&dp->dp_synced_datasets, sizeof (dsl_dataset_t),
94fa9e4066Sahrens 	    offsetof(dsl_dataset_t, ds_synced_link));
95fa9e4066Sahrens 
961ab7f2deSmaybee 	mutex_init(&dp->dp_lock, NULL, MUTEX_DEFAULT, NULL);
971ab7f2deSmaybee 
989d3574bfSNeil Perrin 	dp->dp_vnrele_taskq = taskq_create("zfs_vn_rele_taskq", 1, minclsyspri,
999d3574bfSNeil Perrin 	    1, 4, 0);
1009d3574bfSNeil Perrin 
101fa9e4066Sahrens 	return (dp);
102fa9e4066Sahrens }
103fa9e4066Sahrens 
104ea8dc4b6Seschrock int
105*ad135b5dSChristopher Siden dsl_pool_init(spa_t *spa, uint64_t txg, dsl_pool_t **dpp)
106fa9e4066Sahrens {
107fa9e4066Sahrens 	int err;
108fa9e4066Sahrens 	dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
109*ad135b5dSChristopher Siden 
110*ad135b5dSChristopher Siden 	err = dmu_objset_open_impl(spa, NULL, &dp->dp_meta_rootbp,
111*ad135b5dSChristopher Siden 	    &dp->dp_meta_objset);
112*ad135b5dSChristopher Siden 	if (err != 0)
113*ad135b5dSChristopher Siden 		dsl_pool_close(dp);
114*ad135b5dSChristopher Siden 	else
115*ad135b5dSChristopher Siden 		*dpp = dp;
116*ad135b5dSChristopher Siden 
117*ad135b5dSChristopher Siden 	return (err);
118*ad135b5dSChristopher Siden }
119*ad135b5dSChristopher Siden 
120*ad135b5dSChristopher Siden int
121*ad135b5dSChristopher Siden dsl_pool_open(dsl_pool_t *dp)
122*ad135b5dSChristopher Siden {
123*ad135b5dSChristopher Siden 	int err;
124088f3894Sahrens 	dsl_dir_t *dd;
125088f3894Sahrens 	dsl_dataset_t *ds;
126cde58dbcSMatthew Ahrens 	uint64_t obj;
127fa9e4066Sahrens 
128*ad135b5dSChristopher Siden 	ASSERT(!dmu_objset_is_dirty_anywhere(dp->dp_meta_objset));
129ea8dc4b6Seschrock 
130*ad135b5dSChristopher Siden 	rw_enter(&dp->dp_config_rwlock, RW_WRITER);
131fa9e4066Sahrens 	err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
132fa9e4066Sahrens 	    DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1,
133fa9e4066Sahrens 	    &dp->dp_root_dir_obj);
134ea8dc4b6Seschrock 	if (err)
135ea8dc4b6Seschrock 		goto out;
136ea8dc4b6Seschrock 
137ea8dc4b6Seschrock 	err = dsl_dir_open_obj(dp, dp->dp_root_dir_obj,
138ea8dc4b6Seschrock 	    NULL, dp, &dp->dp_root_dir);
139ea8dc4b6Seschrock 	if (err)
140ea8dc4b6Seschrock 		goto out;
141fa9e4066Sahrens 
142088f3894Sahrens 	err = dsl_pool_open_special_dir(dp, MOS_DIR_NAME, &dp->dp_mos_dir);
143ea8dc4b6Seschrock 	if (err)
144ea8dc4b6Seschrock 		goto out;
145ea8dc4b6Seschrock 
146*ad135b5dSChristopher Siden 	if (spa_version(dp->dp_spa) >= SPA_VERSION_ORIGIN) {
147088f3894Sahrens 		err = dsl_pool_open_special_dir(dp, ORIGIN_DIR_NAME, &dd);
148088f3894Sahrens 		if (err)
149088f3894Sahrens 			goto out;
150088f3894Sahrens 		err = dsl_dataset_hold_obj(dp, dd->dd_phys->dd_head_dataset_obj,
151088f3894Sahrens 		    FTAG, &ds);
1528f63aa46SLin Ling 		if (err == 0) {
1538f63aa46SLin Ling 			err = dsl_dataset_hold_obj(dp,
1548f63aa46SLin Ling 			    ds->ds_phys->ds_prev_snap_obj, dp,
1558f63aa46SLin Ling 			    &dp->dp_origin_snap);
1568f63aa46SLin Ling 			dsl_dataset_rele(ds, FTAG);
1578f63aa46SLin Ling 		}
1588f63aa46SLin Ling 		dsl_dir_close(dd, dp);
159088f3894Sahrens 		if (err)
160088f3894Sahrens 			goto out;
161088f3894Sahrens 	}
162088f3894Sahrens 
163*ad135b5dSChristopher Siden 	if (spa_version(dp->dp_spa) >= SPA_VERSION_DEADLISTS) {
164cde58dbcSMatthew Ahrens 		err = dsl_pool_open_special_dir(dp, FREE_DIR_NAME,
165cde58dbcSMatthew Ahrens 		    &dp->dp_free_dir);
166cde58dbcSMatthew Ahrens 		if (err)
167cde58dbcSMatthew Ahrens 			goto out;
168cde58dbcSMatthew Ahrens 
169cde58dbcSMatthew Ahrens 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
170cde58dbcSMatthew Ahrens 		    DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj);
171cde58dbcSMatthew Ahrens 		if (err)
172cde58dbcSMatthew Ahrens 			goto out;
173cde58dbcSMatthew Ahrens 		VERIFY3U(0, ==, bpobj_open(&dp->dp_free_bpobj,
174cde58dbcSMatthew Ahrens 		    dp->dp_meta_objset, obj));
175cde58dbcSMatthew Ahrens 	}
176cde58dbcSMatthew Ahrens 
177*ad135b5dSChristopher Siden 	if (spa_feature_is_active(dp->dp_spa,
178*ad135b5dSChristopher Siden 	    &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY])) {
179*ad135b5dSChristopher Siden 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
180*ad135b5dSChristopher Siden 		    DMU_POOL_BPTREE_OBJ, sizeof (uint64_t), 1,
181*ad135b5dSChristopher Siden 		    &dp->dp_bptree_obj);
182*ad135b5dSChristopher Siden 		if (err != 0)
183*ad135b5dSChristopher Siden 			goto out;
184*ad135b5dSChristopher Siden 	}
185*ad135b5dSChristopher Siden 
186ca45db41SChris Kirby 	err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
187ca45db41SChris Kirby 	    DMU_POOL_TMP_USERREFS, sizeof (uint64_t), 1,
188ca45db41SChris Kirby 	    &dp->dp_tmp_userrefs_obj);
189ca45db41SChris Kirby 	if (err == ENOENT)
190ca45db41SChris Kirby 		err = 0;
191ca45db41SChris Kirby 	if (err)
192ca45db41SChris Kirby 		goto out;
193ca45db41SChris Kirby 
194*ad135b5dSChristopher Siden 	err = dsl_scan_init(dp, dp->dp_tx.tx_open_txg);
195088f3894Sahrens 
196ea8dc4b6Seschrock out:
197fa9e4066Sahrens 	rw_exit(&dp->dp_config_rwlock);
198ea8dc4b6Seschrock 	return (err);
199fa9e4066Sahrens }
200fa9e4066Sahrens 
201fa9e4066Sahrens void
202fa9e4066Sahrens dsl_pool_close(dsl_pool_t *dp)
203fa9e4066Sahrens {
204088f3894Sahrens 	/* drop our references from dsl_pool_open() */
205088f3894Sahrens 
206088f3894Sahrens 	/*
207088f3894Sahrens 	 * Since we held the origin_snap from "syncing" context (which
208088f3894Sahrens 	 * includes pool-opening context), it actually only got a "ref"
209088f3894Sahrens 	 * and not a hold, so just drop that here.
210088f3894Sahrens 	 */
211088f3894Sahrens 	if (dp->dp_origin_snap)
212088f3894Sahrens 		dsl_dataset_drop_ref(dp->dp_origin_snap, dp);
213ea8dc4b6Seschrock 	if (dp->dp_mos_dir)
214ea8dc4b6Seschrock 		dsl_dir_close(dp->dp_mos_dir, dp);
215cde58dbcSMatthew Ahrens 	if (dp->dp_free_dir)
216cde58dbcSMatthew Ahrens 		dsl_dir_close(dp->dp_free_dir, dp);
217ea8dc4b6Seschrock 	if (dp->dp_root_dir)
218ea8dc4b6Seschrock 		dsl_dir_close(dp->dp_root_dir, dp);
219fa9e4066Sahrens 
220cde58dbcSMatthew Ahrens 	bpobj_close(&dp->dp_free_bpobj);
221cde58dbcSMatthew Ahrens 
222fa9e4066Sahrens 	/* undo the dmu_objset_open_impl(mos) from dsl_pool_open() */
223ea8dc4b6Seschrock 	if (dp->dp_meta_objset)
224503ad85cSMatthew Ahrens 		dmu_objset_evict(dp->dp_meta_objset);
225fa9e4066Sahrens 
226fa9e4066Sahrens 	txg_list_destroy(&dp->dp_dirty_datasets);
22754a91118SChris Kirby 	txg_list_destroy(&dp->dp_sync_tasks);
228fa9e4066Sahrens 	txg_list_destroy(&dp->dp_dirty_dirs);
2293cb34c60Sahrens 	list_destroy(&dp->dp_synced_datasets);
230fa9e4066Sahrens 
231874395d5Smaybee 	arc_flush(dp->dp_spa);
232fa9e4066Sahrens 	txg_fini(dp);
2333f9d6ad7SLin Ling 	dsl_scan_fini(dp);
2345ad82045Snd 	rw_destroy(&dp->dp_config_rwlock);
2351ab7f2deSmaybee 	mutex_destroy(&dp->dp_lock);
2369d3574bfSNeil Perrin 	taskq_destroy(dp->dp_vnrele_taskq);
23788b7b0f2SMatthew Ahrens 	if (dp->dp_blkstats)
23888b7b0f2SMatthew Ahrens 		kmem_free(dp->dp_blkstats, sizeof (zfs_all_blkstats_t));
239fa9e4066Sahrens 	kmem_free(dp, sizeof (dsl_pool_t));
240fa9e4066Sahrens }
241fa9e4066Sahrens 
242fa9e4066Sahrens dsl_pool_t *
2430a48a24eStimh dsl_pool_create(spa_t *spa, nvlist_t *zplprops, uint64_t txg)
244fa9e4066Sahrens {
245fa9e4066Sahrens 	int err;
246fa9e4066Sahrens 	dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
247fa9e4066Sahrens 	dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg);
248503ad85cSMatthew Ahrens 	objset_t *os;
249088f3894Sahrens 	dsl_dataset_t *ds;
250cde58dbcSMatthew Ahrens 	uint64_t obj;
251088f3894Sahrens 
252088f3894Sahrens 	/* create and open the MOS (meta-objset) */
253503ad85cSMatthew Ahrens 	dp->dp_meta_objset = dmu_objset_create_impl(spa,
254503ad85cSMatthew Ahrens 	    NULL, &dp->dp_meta_rootbp, DMU_OST_META, tx);
255fa9e4066Sahrens 
256fa9e4066Sahrens 	/* create the pool directory */
257fa9e4066Sahrens 	err = zap_create_claim(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
258fa9e4066Sahrens 	    DMU_OT_OBJECT_DIRECTORY, DMU_OT_NONE, 0, tx);
259fa9e4066Sahrens 	ASSERT3U(err, ==, 0);
260fa9e4066Sahrens 
2613f9d6ad7SLin Ling 	/* Initialize scan structures */
2623f9d6ad7SLin Ling 	VERIFY3U(0, ==, dsl_scan_init(dp, txg));
2633f9d6ad7SLin Ling 
264fa9e4066Sahrens 	/* create and open the root dir */
265088f3894Sahrens 	dp->dp_root_dir_obj = dsl_dir_create_sync(dp, NULL, NULL, tx);
266ea8dc4b6Seschrock 	VERIFY(0 == dsl_dir_open_obj(dp, dp->dp_root_dir_obj,
267ea8dc4b6Seschrock 	    NULL, dp, &dp->dp_root_dir));
268fa9e4066Sahrens 
269fa9e4066Sahrens 	/* create and open the meta-objset dir */
270088f3894Sahrens 	(void) dsl_dir_create_sync(dp, dp->dp_root_dir, MOS_DIR_NAME, tx);
271088f3894Sahrens 	VERIFY(0 == dsl_pool_open_special_dir(dp,
272088f3894Sahrens 	    MOS_DIR_NAME, &dp->dp_mos_dir));
273088f3894Sahrens 
274cde58dbcSMatthew Ahrens 	if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
275cde58dbcSMatthew Ahrens 		/* create and open the free dir */
276cde58dbcSMatthew Ahrens 		(void) dsl_dir_create_sync(dp, dp->dp_root_dir,
277cde58dbcSMatthew Ahrens 		    FREE_DIR_NAME, tx);
278cde58dbcSMatthew Ahrens 		VERIFY(0 == dsl_pool_open_special_dir(dp,
279cde58dbcSMatthew Ahrens 		    FREE_DIR_NAME, &dp->dp_free_dir));
280cde58dbcSMatthew Ahrens 
281cde58dbcSMatthew Ahrens 		/* create and open the free_bplist */
282cde58dbcSMatthew Ahrens 		obj = bpobj_alloc(dp->dp_meta_objset, SPA_MAXBLOCKSIZE, tx);
283cde58dbcSMatthew Ahrens 		VERIFY(zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
284cde58dbcSMatthew Ahrens 		    DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj, tx) == 0);
285cde58dbcSMatthew Ahrens 		VERIFY3U(0, ==, bpobj_open(&dp->dp_free_bpobj,
286cde58dbcSMatthew Ahrens 		    dp->dp_meta_objset, obj));
287cde58dbcSMatthew Ahrens 	}
288cde58dbcSMatthew Ahrens 
289088f3894Sahrens 	if (spa_version(spa) >= SPA_VERSION_DSL_SCRUB)
290088f3894Sahrens 		dsl_pool_create_origin(dp, tx);
291088f3894Sahrens 
292088f3894Sahrens 	/* create the root dataset */
293cde58dbcSMatthew Ahrens 	obj = dsl_dataset_create_sync_dd(dp->dp_root_dir, NULL, 0, tx);
294088f3894Sahrens 
295088f3894Sahrens 	/* create the root objset */
296cde58dbcSMatthew Ahrens 	VERIFY(0 == dsl_dataset_hold_obj(dp, obj, FTAG, &ds));
297503ad85cSMatthew Ahrens 	os = dmu_objset_create_impl(dp->dp_spa, ds,
298088f3894Sahrens 	    dsl_dataset_get_blkptr(ds), DMU_OST_ZFS, tx);
299088f3894Sahrens #ifdef _KERNEL
300503ad85cSMatthew Ahrens 	zfs_create_fs(os, kcred, zplprops, tx);
301088f3894Sahrens #endif
302088f3894Sahrens 	dsl_dataset_rele(ds, FTAG);
303fa9e4066Sahrens 
304fa9e4066Sahrens 	dmu_tx_commit(tx);
305fa9e4066Sahrens 
306fa9e4066Sahrens 	return (dp);
307fa9e4066Sahrens }
308fa9e4066Sahrens 
309cde58dbcSMatthew Ahrens static int
310cde58dbcSMatthew Ahrens deadlist_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
311cde58dbcSMatthew Ahrens {
312cde58dbcSMatthew Ahrens 	dsl_deadlist_t *dl = arg;
31319b94df9SMatthew Ahrens 	dsl_pool_t *dp = dmu_objset_pool(dl->dl_os);
31419b94df9SMatthew Ahrens 	rw_enter(&dp->dp_config_rwlock, RW_READER);
315cde58dbcSMatthew Ahrens 	dsl_deadlist_insert(dl, bp, tx);
31619b94df9SMatthew Ahrens 	rw_exit(&dp->dp_config_rwlock);
317cde58dbcSMatthew Ahrens 	return (0);
318cde58dbcSMatthew Ahrens }
319cde58dbcSMatthew Ahrens 
320fa9e4066Sahrens void
321fa9e4066Sahrens dsl_pool_sync(dsl_pool_t *dp, uint64_t txg)
322fa9e4066Sahrens {
323c717a561Smaybee 	zio_t *zio;
324fa9e4066Sahrens 	dmu_tx_t *tx;
325c717a561Smaybee 	dsl_dir_t *dd;
326c717a561Smaybee 	dsl_dataset_t *ds;
327c717a561Smaybee 	dsl_sync_task_group_t *dstg;
328503ad85cSMatthew Ahrens 	objset_t *mos = dp->dp_meta_objset;
32905715f94SMark Maybee 	hrtime_t start, write_time;
33005715f94SMark Maybee 	uint64_t data_written;
331c717a561Smaybee 	int err;
332fa9e4066Sahrens 
3333f9d6ad7SLin Ling 	/*
3343f9d6ad7SLin Ling 	 * We need to copy dp_space_towrite() before doing
3353f9d6ad7SLin Ling 	 * dsl_sync_task_group_sync(), because
3363f9d6ad7SLin Ling 	 * dsl_dataset_snapshot_reserve_space() will increase
3373f9d6ad7SLin Ling 	 * dp_space_towrite but not actually write anything.
3383f9d6ad7SLin Ling 	 */
3393f9d6ad7SLin Ling 	data_written = dp->dp_space_towrite[txg & TXG_MASK];
3403f9d6ad7SLin Ling 
341fa9e4066Sahrens 	tx = dmu_tx_create_assigned(dp, txg);
342fa9e4066Sahrens 
34305715f94SMark Maybee 	dp->dp_read_overhead = 0;
3440fd90d51SMark Maybee 	start = gethrtime();
34514843421SMatthew Ahrens 
346c717a561Smaybee 	zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
347c717a561Smaybee 	while (ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) {
34814843421SMatthew Ahrens 		/*
34914843421SMatthew Ahrens 		 * We must not sync any non-MOS datasets twice, because
35014843421SMatthew Ahrens 		 * we may have taken a snapshot of them.  However, we
35114843421SMatthew Ahrens 		 * may sync newly-created datasets on pass 2.
35214843421SMatthew Ahrens 		 */
35314843421SMatthew Ahrens 		ASSERT(!list_link_active(&ds->ds_synced_link));
35414843421SMatthew Ahrens 		list_insert_tail(&dp->dp_synced_datasets, ds);
355c717a561Smaybee 		dsl_dataset_sync(ds, zio, tx);
356c717a561Smaybee 	}
35705715f94SMark Maybee 	DTRACE_PROBE(pool_sync__1setup);
358c717a561Smaybee 	err = zio_wait(zio);
35914843421SMatthew Ahrens 
36005715f94SMark Maybee 	write_time = gethrtime() - start;
361c717a561Smaybee 	ASSERT(err == 0);
36205715f94SMark Maybee 	DTRACE_PROBE(pool_sync__2rootzio);
363c717a561Smaybee 
36414843421SMatthew Ahrens 	for (ds = list_head(&dp->dp_synced_datasets); ds;
36514843421SMatthew Ahrens 	    ds = list_next(&dp->dp_synced_datasets, ds))
3660a586ceaSMark Shellenbaum 		dmu_objset_do_userquota_updates(ds->ds_objset, tx);
36714843421SMatthew Ahrens 
36814843421SMatthew Ahrens 	/*
36914843421SMatthew Ahrens 	 * Sync the datasets again to push out the changes due to
3703f9d6ad7SLin Ling 	 * userspace updates.  This must be done before we process the
37114843421SMatthew Ahrens 	 * sync tasks, because that could cause a snapshot of a dataset
37214843421SMatthew Ahrens 	 * whose ds_bp will be rewritten when we do this 2nd sync.
37314843421SMatthew Ahrens 	 */
37414843421SMatthew Ahrens 	zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
37514843421SMatthew Ahrens 	while (ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) {
37614843421SMatthew Ahrens 		ASSERT(list_link_active(&ds->ds_synced_link));
37714843421SMatthew Ahrens 		dmu_buf_rele(ds->ds_dbuf, ds);
37814843421SMatthew Ahrens 		dsl_dataset_sync(ds, zio, tx);
37914843421SMatthew Ahrens 	}
38014843421SMatthew Ahrens 	err = zio_wait(zio);
38114843421SMatthew Ahrens 
382b24ab676SJeff Bonwick 	/*
383cde58dbcSMatthew Ahrens 	 * Move dead blocks from the pending deadlist to the on-disk
384cde58dbcSMatthew Ahrens 	 * deadlist.
385b24ab676SJeff Bonwick 	 */
386b24ab676SJeff Bonwick 	for (ds = list_head(&dp->dp_synced_datasets); ds;
387cde58dbcSMatthew Ahrens 	    ds = list_next(&dp->dp_synced_datasets, ds)) {
388cde58dbcSMatthew Ahrens 		bplist_iterate(&ds->ds_pending_deadlist,
389cde58dbcSMatthew Ahrens 		    deadlist_enqueue_cb, &ds->ds_deadlist, tx);
390cde58dbcSMatthew Ahrens 	}
391b24ab676SJeff Bonwick 
39214843421SMatthew Ahrens 	while (dstg = txg_list_remove(&dp->dp_sync_tasks, txg)) {
39314843421SMatthew Ahrens 		/*
39414843421SMatthew Ahrens 		 * No more sync tasks should have been added while we
39514843421SMatthew Ahrens 		 * were syncing.
39614843421SMatthew Ahrens 		 */
39714843421SMatthew Ahrens 		ASSERT(spa_sync_pass(dp->dp_spa) == 1);
398c717a561Smaybee 		dsl_sync_task_group_sync(dstg, tx);
39914843421SMatthew Ahrens 	}
40005715f94SMark Maybee 	DTRACE_PROBE(pool_sync__3task);
40105715f94SMark Maybee 
40205715f94SMark Maybee 	start = gethrtime();
403c717a561Smaybee 	while (dd = txg_list_remove(&dp->dp_dirty_dirs, txg))
404c717a561Smaybee 		dsl_dir_sync(dd, tx);
40505715f94SMark Maybee 	write_time += gethrtime() - start;
406fa9e4066Sahrens 
40705715f94SMark Maybee 	start = gethrtime();
408503ad85cSMatthew Ahrens 	if (list_head(&mos->os_dirty_dnodes[txg & TXG_MASK]) != NULL ||
409503ad85cSMatthew Ahrens 	    list_head(&mos->os_free_dnodes[txg & TXG_MASK]) != NULL) {
410c717a561Smaybee 		zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
411503ad85cSMatthew Ahrens 		dmu_objset_sync(mos, zio, tx);
412c717a561Smaybee 		err = zio_wait(zio);
413c717a561Smaybee 		ASSERT(err == 0);
414fa9e4066Sahrens 		dprintf_bp(&dp->dp_meta_rootbp, "meta objset rootbp is %s", "");
415fa9e4066Sahrens 		spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
416fa9e4066Sahrens 	}
41705715f94SMark Maybee 	write_time += gethrtime() - start;
41805715f94SMark Maybee 	DTRACE_PROBE2(pool_sync__4io, hrtime_t, write_time,
41905715f94SMark Maybee 	    hrtime_t, dp->dp_read_overhead);
42005715f94SMark Maybee 	write_time -= dp->dp_read_overhead;
421fa9e4066Sahrens 
422fa9e4066Sahrens 	dmu_tx_commit(tx);
42305715f94SMark Maybee 
42405715f94SMark Maybee 	dp->dp_space_towrite[txg & TXG_MASK] = 0;
42505715f94SMark Maybee 	ASSERT(dp->dp_tempreserved[txg & TXG_MASK] == 0);
42605715f94SMark Maybee 
42705715f94SMark Maybee 	/*
42805715f94SMark Maybee 	 * If the write limit max has not been explicitly set, set it
42905715f94SMark Maybee 	 * to a fraction of available physical memory (default 1/8th).
43005715f94SMark Maybee 	 * Note that we must inflate the limit because the spa
43105715f94SMark Maybee 	 * inflates write sizes to account for data replication.
43205715f94SMark Maybee 	 * Check this each sync phase to catch changing memory size.
43305715f94SMark Maybee 	 */
43405715f94SMark Maybee 	if (physmem != old_physmem && zfs_write_limit_shift) {
43505715f94SMark Maybee 		mutex_enter(&zfs_write_limit_lock);
43605715f94SMark Maybee 		old_physmem = physmem;
43705715f94SMark Maybee 		zfs_write_limit_max = ptob(physmem) >> zfs_write_limit_shift;
43805715f94SMark Maybee 		zfs_write_limit_inflated = MAX(zfs_write_limit_min,
43905715f94SMark Maybee 		    spa_get_asize(dp->dp_spa, zfs_write_limit_max));
44005715f94SMark Maybee 		mutex_exit(&zfs_write_limit_lock);
44105715f94SMark Maybee 	}
44205715f94SMark Maybee 
44305715f94SMark Maybee 	/*
44405715f94SMark Maybee 	 * Attempt to keep the sync time consistent by adjusting the
44505715f94SMark Maybee 	 * amount of write traffic allowed into each transaction group.
44605715f94SMark Maybee 	 * Weight the throughput calculation towards the current value:
44705715f94SMark Maybee 	 * 	thru = 3/4 old_thru + 1/4 new_thru
448fb5dd802SLin Ling 	 *
449fb5dd802SLin Ling 	 * Note: write_time is in nanosecs, so write_time/MICROSEC
450fb5dd802SLin Ling 	 * yields millisecs
45105715f94SMark Maybee 	 */
45205715f94SMark Maybee 	ASSERT(zfs_write_limit_min > 0);
453fb5dd802SLin Ling 	if (data_written > zfs_write_limit_min / 8 && write_time > MICROSEC) {
454fb5dd802SLin Ling 		uint64_t throughput = data_written / (write_time / MICROSEC);
455fb5dd802SLin Ling 
45605715f94SMark Maybee 		if (dp->dp_throughput)
45705715f94SMark Maybee 			dp->dp_throughput = throughput / 4 +
45805715f94SMark Maybee 			    3 * dp->dp_throughput / 4;
45905715f94SMark Maybee 		else
46005715f94SMark Maybee 			dp->dp_throughput = throughput;
46105715f94SMark Maybee 		dp->dp_write_limit = MIN(zfs_write_limit_inflated,
46205715f94SMark Maybee 		    MAX(zfs_write_limit_min,
463fb5dd802SLin Ling 		    dp->dp_throughput * zfs_txg_synctime_ms));
46405715f94SMark Maybee 	}
465fa9e4066Sahrens }
466fa9e4066Sahrens 
467fa9e4066Sahrens void
468b24ab676SJeff Bonwick dsl_pool_sync_done(dsl_pool_t *dp, uint64_t txg)
469fa9e4066Sahrens {
470fa9e4066Sahrens 	dsl_dataset_t *ds;
471b24ab676SJeff Bonwick 	objset_t *os;
472fa9e4066Sahrens 
4733cb34c60Sahrens 	while (ds = list_head(&dp->dp_synced_datasets)) {
4743cb34c60Sahrens 		list_remove(&dp->dp_synced_datasets, ds);
475b24ab676SJeff Bonwick 		os = ds->ds_objset;
4765002558fSNeil Perrin 		zil_clean(os->os_zil, txg);
477b24ab676SJeff Bonwick 		ASSERT(!dmu_objset_is_dirty(os, txg));
478af2c4821Smaybee 		dmu_buf_rele(ds->ds_dbuf, ds);
479fa9e4066Sahrens 	}
480b24ab676SJeff Bonwick 	ASSERT(!dmu_objset_is_dirty(dp->dp_meta_objset, txg));
481fa9e4066Sahrens }
482fa9e4066Sahrens 
483c717a561Smaybee /*
484c717a561Smaybee  * TRUE if the current thread is the tx_sync_thread or if we
485c717a561Smaybee  * are being called from SPA context during pool initialization.
486c717a561Smaybee  */
487fa9e4066Sahrens int
488fa9e4066Sahrens dsl_pool_sync_context(dsl_pool_t *dp)
489fa9e4066Sahrens {
490fa9e4066Sahrens 	return (curthread == dp->dp_tx.tx_sync_thread ||
491*ad135b5dSChristopher Siden 	    spa_is_initializing(dp->dp_spa));
492fa9e4066Sahrens }
493fa9e4066Sahrens 
494fa9e4066Sahrens uint64_t
495fa9e4066Sahrens dsl_pool_adjustedsize(dsl_pool_t *dp, boolean_t netfree)
496fa9e4066Sahrens {
497fa9e4066Sahrens 	uint64_t space, resv;
498fa9e4066Sahrens 
499fa9e4066Sahrens 	/*
50044cd46caSbillm 	 * Reserve about 1.6% (1/64), or at least 32MB, for allocation
501fa9e4066Sahrens 	 * efficiency.
502fa9e4066Sahrens 	 * XXX The intent log is not accounted for, so it must fit
503fa9e4066Sahrens 	 * within this slop.
504fa9e4066Sahrens 	 *
505fa9e4066Sahrens 	 * If we're trying to assess whether it's OK to do a free,
506fa9e4066Sahrens 	 * cut the reservation in half to allow forward progress
507fa9e4066Sahrens 	 * (e.g. make it possible to rm(1) files from a full pool).
508fa9e4066Sahrens 	 */
509485bbbf5SGeorge Wilson 	space = spa_get_dspace(dp->dp_spa);
51044cd46caSbillm 	resv = MAX(space >> 6, SPA_MINDEVSIZE >> 1);
511fa9e4066Sahrens 	if (netfree)
512fa9e4066Sahrens 		resv >>= 1;
513fa9e4066Sahrens 
514fa9e4066Sahrens 	return (space - resv);
515fa9e4066Sahrens }
5161ab7f2deSmaybee 
5171ab7f2deSmaybee int
5181ab7f2deSmaybee dsl_pool_tempreserve_space(dsl_pool_t *dp, uint64_t space, dmu_tx_t *tx)
5191ab7f2deSmaybee {
5201ab7f2deSmaybee 	uint64_t reserved = 0;
5211ab7f2deSmaybee 	uint64_t write_limit = (zfs_write_limit_override ?
5221ab7f2deSmaybee 	    zfs_write_limit_override : dp->dp_write_limit);
5231ab7f2deSmaybee 
5241ab7f2deSmaybee 	if (zfs_no_write_throttle) {
525c5904d13Seschrock 		atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK],
526c5904d13Seschrock 		    space);
5271ab7f2deSmaybee 		return (0);
5281ab7f2deSmaybee 	}
5291ab7f2deSmaybee 
5301ab7f2deSmaybee 	/*
5311ab7f2deSmaybee 	 * Check to see if we have exceeded the maximum allowed IO for
5321ab7f2deSmaybee 	 * this transaction group.  We can do this without locks since
5331ab7f2deSmaybee 	 * a little slop here is ok.  Note that we do the reserved check
5341ab7f2deSmaybee 	 * with only half the requested reserve: this is because the
5351ab7f2deSmaybee 	 * reserve requests are worst-case, and we really don't want to
5361ab7f2deSmaybee 	 * throttle based off of worst-case estimates.
5371ab7f2deSmaybee 	 */
5381ab7f2deSmaybee 	if (write_limit > 0) {
5391ab7f2deSmaybee 		reserved = dp->dp_space_towrite[tx->tx_txg & TXG_MASK]
5401ab7f2deSmaybee 		    + dp->dp_tempreserved[tx->tx_txg & TXG_MASK] / 2;
5411ab7f2deSmaybee 
5421ab7f2deSmaybee 		if (reserved && reserved > write_limit)
5431ab7f2deSmaybee 			return (ERESTART);
5441ab7f2deSmaybee 	}
5451ab7f2deSmaybee 
5461ab7f2deSmaybee 	atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], space);
5471ab7f2deSmaybee 
5481ab7f2deSmaybee 	/*
5491ab7f2deSmaybee 	 * If this transaction group is over 7/8ths capacity, delay
5501ab7f2deSmaybee 	 * the caller 1 clock tick.  This will slow down the "fill"
5511ab7f2deSmaybee 	 * rate until the sync process can catch up with us.
5521ab7f2deSmaybee 	 */
553e8397a2bSgw 	if (reserved && reserved > (write_limit - (write_limit >> 3)))
5541ab7f2deSmaybee 		txg_delay(dp, tx->tx_txg, 1);
5551ab7f2deSmaybee 
5561ab7f2deSmaybee 	return (0);
5571ab7f2deSmaybee }
5581ab7f2deSmaybee 
5591ab7f2deSmaybee void
5601ab7f2deSmaybee dsl_pool_tempreserve_clear(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx)
5611ab7f2deSmaybee {
5621ab7f2deSmaybee 	ASSERT(dp->dp_tempreserved[tx->tx_txg & TXG_MASK] >= space);
5631ab7f2deSmaybee 	atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], -space);
5641ab7f2deSmaybee }
5651ab7f2deSmaybee 
5661ab7f2deSmaybee void
5671ab7f2deSmaybee dsl_pool_memory_pressure(dsl_pool_t *dp)
5681ab7f2deSmaybee {
5691ab7f2deSmaybee 	uint64_t space_inuse = 0;
5701ab7f2deSmaybee 	int i;
5711ab7f2deSmaybee 
5721ab7f2deSmaybee 	if (dp->dp_write_limit == zfs_write_limit_min)
5731ab7f2deSmaybee 		return;
5741ab7f2deSmaybee 
5751ab7f2deSmaybee 	for (i = 0; i < TXG_SIZE; i++) {
5761ab7f2deSmaybee 		space_inuse += dp->dp_space_towrite[i];
5771ab7f2deSmaybee 		space_inuse += dp->dp_tempreserved[i];
5781ab7f2deSmaybee 	}
5791ab7f2deSmaybee 	dp->dp_write_limit = MAX(zfs_write_limit_min,
5801ab7f2deSmaybee 	    MIN(dp->dp_write_limit, space_inuse / 4));
5811ab7f2deSmaybee }
5821ab7f2deSmaybee 
5831ab7f2deSmaybee void
5841ab7f2deSmaybee dsl_pool_willuse_space(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx)
5851ab7f2deSmaybee {
5861ab7f2deSmaybee 	if (space > 0) {
5871ab7f2deSmaybee 		mutex_enter(&dp->dp_lock);
5881ab7f2deSmaybee 		dp->dp_space_towrite[tx->tx_txg & TXG_MASK] += space;
5891ab7f2deSmaybee 		mutex_exit(&dp->dp_lock);
5901ab7f2deSmaybee 	}
5911ab7f2deSmaybee }
592088f3894Sahrens 
593088f3894Sahrens /* ARGSUSED */
594088f3894Sahrens static int
595088f3894Sahrens upgrade_clones_cb(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
596088f3894Sahrens {
597088f3894Sahrens 	dmu_tx_t *tx = arg;
598088f3894Sahrens 	dsl_dataset_t *ds, *prev = NULL;
599088f3894Sahrens 	int err;
600088f3894Sahrens 	dsl_pool_t *dp = spa_get_dsl(spa);
601088f3894Sahrens 
602088f3894Sahrens 	err = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds);
603088f3894Sahrens 	if (err)
604088f3894Sahrens 		return (err);
605088f3894Sahrens 
606088f3894Sahrens 	while (ds->ds_phys->ds_prev_snap_obj != 0) {
607088f3894Sahrens 		err = dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
608088f3894Sahrens 		    FTAG, &prev);
609088f3894Sahrens 		if (err) {
610088f3894Sahrens 			dsl_dataset_rele(ds, FTAG);
611088f3894Sahrens 			return (err);
612088f3894Sahrens 		}
613088f3894Sahrens 
614088f3894Sahrens 		if (prev->ds_phys->ds_next_snap_obj != ds->ds_object)
615088f3894Sahrens 			break;
616088f3894Sahrens 		dsl_dataset_rele(ds, FTAG);
617088f3894Sahrens 		ds = prev;
618088f3894Sahrens 		prev = NULL;
619088f3894Sahrens 	}
620088f3894Sahrens 
621088f3894Sahrens 	if (prev == NULL) {
622088f3894Sahrens 		prev = dp->dp_origin_snap;
623088f3894Sahrens 
624088f3894Sahrens 		/*
625088f3894Sahrens 		 * The $ORIGIN can't have any data, or the accounting
626088f3894Sahrens 		 * will be wrong.
627088f3894Sahrens 		 */
628088f3894Sahrens 		ASSERT(prev->ds_phys->ds_bp.blk_birth == 0);
629088f3894Sahrens 
630088f3894Sahrens 		/* The origin doesn't get attached to itself */
631088f3894Sahrens 		if (ds->ds_object == prev->ds_object) {
632088f3894Sahrens 			dsl_dataset_rele(ds, FTAG);
633088f3894Sahrens 			return (0);
634088f3894Sahrens 		}
635088f3894Sahrens 
636088f3894Sahrens 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
637088f3894Sahrens 		ds->ds_phys->ds_prev_snap_obj = prev->ds_object;
638088f3894Sahrens 		ds->ds_phys->ds_prev_snap_txg = prev->ds_phys->ds_creation_txg;
639088f3894Sahrens 
640088f3894Sahrens 		dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
641088f3894Sahrens 		ds->ds_dir->dd_phys->dd_origin_obj = prev->ds_object;
642088f3894Sahrens 
643088f3894Sahrens 		dmu_buf_will_dirty(prev->ds_dbuf, tx);
644088f3894Sahrens 		prev->ds_phys->ds_num_children++;
645088f3894Sahrens 
646088f3894Sahrens 		if (ds->ds_phys->ds_next_snap_obj == 0) {
647088f3894Sahrens 			ASSERT(ds->ds_prev == NULL);
648088f3894Sahrens 			VERIFY(0 == dsl_dataset_hold_obj(dp,
649088f3894Sahrens 			    ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev));
650088f3894Sahrens 		}
651088f3894Sahrens 	}
652088f3894Sahrens 
653088f3894Sahrens 	ASSERT(ds->ds_dir->dd_phys->dd_origin_obj == prev->ds_object);
654088f3894Sahrens 	ASSERT(ds->ds_phys->ds_prev_snap_obj == prev->ds_object);
655088f3894Sahrens 
656088f3894Sahrens 	if (prev->ds_phys->ds_next_clones_obj == 0) {
657c33e334fSMatthew Ahrens 		dmu_buf_will_dirty(prev->ds_dbuf, tx);
658088f3894Sahrens 		prev->ds_phys->ds_next_clones_obj =
659088f3894Sahrens 		    zap_create(dp->dp_meta_objset,
660088f3894Sahrens 		    DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
661088f3894Sahrens 	}
662088f3894Sahrens 	VERIFY(0 == zap_add_int(dp->dp_meta_objset,
663088f3894Sahrens 	    prev->ds_phys->ds_next_clones_obj, ds->ds_object, tx));
664088f3894Sahrens 
665088f3894Sahrens 	dsl_dataset_rele(ds, FTAG);
666088f3894Sahrens 	if (prev != dp->dp_origin_snap)
667088f3894Sahrens 		dsl_dataset_rele(prev, FTAG);
668088f3894Sahrens 	return (0);
669088f3894Sahrens }
670088f3894Sahrens 
671088f3894Sahrens void
672088f3894Sahrens dsl_pool_upgrade_clones(dsl_pool_t *dp, dmu_tx_t *tx)
673088f3894Sahrens {
674088f3894Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
675088f3894Sahrens 	ASSERT(dp->dp_origin_snap != NULL);
676088f3894Sahrens 
677c33e334fSMatthew Ahrens 	VERIFY3U(0, ==, dmu_objset_find_spa(dp->dp_spa, NULL, upgrade_clones_cb,
678c33e334fSMatthew Ahrens 	    tx, DS_FIND_CHILDREN));
679088f3894Sahrens }
680088f3894Sahrens 
681cde58dbcSMatthew Ahrens /* ARGSUSED */
682cde58dbcSMatthew Ahrens static int
683cde58dbcSMatthew Ahrens upgrade_dir_clones_cb(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
684cde58dbcSMatthew Ahrens {
685cde58dbcSMatthew Ahrens 	dmu_tx_t *tx = arg;
686cde58dbcSMatthew Ahrens 	dsl_dataset_t *ds;
687cde58dbcSMatthew Ahrens 	dsl_pool_t *dp = spa_get_dsl(spa);
688cde58dbcSMatthew Ahrens 	objset_t *mos = dp->dp_meta_objset;
689cde58dbcSMatthew Ahrens 
690cde58dbcSMatthew Ahrens 	VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
691cde58dbcSMatthew Ahrens 
692cde58dbcSMatthew Ahrens 	if (ds->ds_dir->dd_phys->dd_origin_obj) {
693cde58dbcSMatthew Ahrens 		dsl_dataset_t *origin;
694cde58dbcSMatthew Ahrens 
695cde58dbcSMatthew Ahrens 		VERIFY3U(0, ==, dsl_dataset_hold_obj(dp,
696cde58dbcSMatthew Ahrens 		    ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &origin));
697cde58dbcSMatthew Ahrens 
698cde58dbcSMatthew Ahrens 		if (origin->ds_dir->dd_phys->dd_clones == 0) {
699cde58dbcSMatthew Ahrens 			dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx);
700cde58dbcSMatthew Ahrens 			origin->ds_dir->dd_phys->dd_clones = zap_create(mos,
701cde58dbcSMatthew Ahrens 			    DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
702cde58dbcSMatthew Ahrens 		}
703cde58dbcSMatthew Ahrens 
704cde58dbcSMatthew Ahrens 		VERIFY3U(0, ==, zap_add_int(dp->dp_meta_objset,
705cde58dbcSMatthew Ahrens 		    origin->ds_dir->dd_phys->dd_clones, dsobj, tx));
706cde58dbcSMatthew Ahrens 
707cde58dbcSMatthew Ahrens 		dsl_dataset_rele(origin, FTAG);
708cde58dbcSMatthew Ahrens 	}
709cde58dbcSMatthew Ahrens 
710cde58dbcSMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
711cde58dbcSMatthew Ahrens 	return (0);
712cde58dbcSMatthew Ahrens }
713cde58dbcSMatthew Ahrens 
714cde58dbcSMatthew Ahrens void
715cde58dbcSMatthew Ahrens dsl_pool_upgrade_dir_clones(dsl_pool_t *dp, dmu_tx_t *tx)
716cde58dbcSMatthew Ahrens {
717cde58dbcSMatthew Ahrens 	ASSERT(dmu_tx_is_syncing(tx));
718cde58dbcSMatthew Ahrens 	uint64_t obj;
719cde58dbcSMatthew Ahrens 
720cde58dbcSMatthew Ahrens 	(void) dsl_dir_create_sync(dp, dp->dp_root_dir, FREE_DIR_NAME, tx);
721cde58dbcSMatthew Ahrens 	VERIFY(0 == dsl_pool_open_special_dir(dp,
722cde58dbcSMatthew Ahrens 	    FREE_DIR_NAME, &dp->dp_free_dir));
723cde58dbcSMatthew Ahrens 
724cde58dbcSMatthew Ahrens 	/*
725cde58dbcSMatthew Ahrens 	 * We can't use bpobj_alloc(), because spa_version() still
726cde58dbcSMatthew Ahrens 	 * returns the old version, and we need a new-version bpobj with
727cde58dbcSMatthew Ahrens 	 * subobj support.  So call dmu_object_alloc() directly.
728cde58dbcSMatthew Ahrens 	 */
729cde58dbcSMatthew Ahrens 	obj = dmu_object_alloc(dp->dp_meta_objset, DMU_OT_BPOBJ,
730cde58dbcSMatthew Ahrens 	    SPA_MAXBLOCKSIZE, DMU_OT_BPOBJ_HDR, sizeof (bpobj_phys_t), tx);
731cde58dbcSMatthew Ahrens 	VERIFY3U(0, ==, zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
732cde58dbcSMatthew Ahrens 	    DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj, tx));
733cde58dbcSMatthew Ahrens 	VERIFY3U(0, ==, bpobj_open(&dp->dp_free_bpobj,
734cde58dbcSMatthew Ahrens 	    dp->dp_meta_objset, obj));
735cde58dbcSMatthew Ahrens 
736cde58dbcSMatthew Ahrens 	VERIFY3U(0, ==, dmu_objset_find_spa(dp->dp_spa, NULL,
737cde58dbcSMatthew Ahrens 	    upgrade_dir_clones_cb, tx, DS_FIND_CHILDREN));
738cde58dbcSMatthew Ahrens }
739cde58dbcSMatthew Ahrens 
740088f3894Sahrens void
741088f3894Sahrens dsl_pool_create_origin(dsl_pool_t *dp, dmu_tx_t *tx)
742088f3894Sahrens {
743088f3894Sahrens 	uint64_t dsobj;
744088f3894Sahrens 	dsl_dataset_t *ds;
745088f3894Sahrens 
746088f3894Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
747088f3894Sahrens 	ASSERT(dp->dp_origin_snap == NULL);
748088f3894Sahrens 
749088f3894Sahrens 	/* create the origin dir, ds, & snap-ds */
750088f3894Sahrens 	rw_enter(&dp->dp_config_rwlock, RW_WRITER);
751088f3894Sahrens 	dsobj = dsl_dataset_create_sync(dp->dp_root_dir, ORIGIN_DIR_NAME,
752088f3894Sahrens 	    NULL, 0, kcred, tx);
753088f3894Sahrens 	VERIFY(0 == dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
7543f9d6ad7SLin Ling 	dsl_dataset_snapshot_sync(ds, ORIGIN_DIR_NAME, tx);
755088f3894Sahrens 	VERIFY(0 == dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
756088f3894Sahrens 	    dp, &dp->dp_origin_snap));
757088f3894Sahrens 	dsl_dataset_rele(ds, FTAG);
758088f3894Sahrens 	rw_exit(&dp->dp_config_rwlock);
759088f3894Sahrens }
7609d3574bfSNeil Perrin 
7619d3574bfSNeil Perrin taskq_t *
7629d3574bfSNeil Perrin dsl_pool_vnrele_taskq(dsl_pool_t *dp)
7639d3574bfSNeil Perrin {
7649d3574bfSNeil Perrin 	return (dp->dp_vnrele_taskq);
7659d3574bfSNeil Perrin }
766ca45db41SChris Kirby 
767ca45db41SChris Kirby /*
768ca45db41SChris Kirby  * Walk through the pool-wide zap object of temporary snapshot user holds
769ca45db41SChris Kirby  * and release them.
770ca45db41SChris Kirby  */
771ca45db41SChris Kirby void
772ca45db41SChris Kirby dsl_pool_clean_tmp_userrefs(dsl_pool_t *dp)
773ca45db41SChris Kirby {
774ca45db41SChris Kirby 	zap_attribute_t za;
775ca45db41SChris Kirby 	zap_cursor_t zc;
776ca45db41SChris Kirby 	objset_t *mos = dp->dp_meta_objset;
777ca45db41SChris Kirby 	uint64_t zapobj = dp->dp_tmp_userrefs_obj;
778ca45db41SChris Kirby 
779ca45db41SChris Kirby 	if (zapobj == 0)
780ca45db41SChris Kirby 		return;
781ca45db41SChris Kirby 	ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
782ca45db41SChris Kirby 
783ca45db41SChris Kirby 	for (zap_cursor_init(&zc, mos, zapobj);
784ca45db41SChris Kirby 	    zap_cursor_retrieve(&zc, &za) == 0;
785ca45db41SChris Kirby 	    zap_cursor_advance(&zc)) {
786ca45db41SChris Kirby 		char *htag;
787ca45db41SChris Kirby 		uint64_t dsobj;
788ca45db41SChris Kirby 
789ca45db41SChris Kirby 		htag = strchr(za.za_name, '-');
790ca45db41SChris Kirby 		*htag = '\0';
791ca45db41SChris Kirby 		++htag;
792ca45db41SChris Kirby 		dsobj = strtonum(za.za_name, NULL);
793a7f53a56SChris Kirby 		(void) dsl_dataset_user_release_tmp(dp, dsobj, htag, B_FALSE);
794ca45db41SChris Kirby 	}
795ca45db41SChris Kirby 	zap_cursor_fini(&zc);
796ca45db41SChris Kirby }
797ca45db41SChris Kirby 
798ca45db41SChris Kirby /*
799ca45db41SChris Kirby  * Create the pool-wide zap object for storing temporary snapshot holds.
800ca45db41SChris Kirby  */
801ca45db41SChris Kirby void
802ca45db41SChris Kirby dsl_pool_user_hold_create_obj(dsl_pool_t *dp, dmu_tx_t *tx)
803ca45db41SChris Kirby {
804ca45db41SChris Kirby 	objset_t *mos = dp->dp_meta_objset;
805ca45db41SChris Kirby 
806ca45db41SChris Kirby 	ASSERT(dp->dp_tmp_userrefs_obj == 0);
807ca45db41SChris Kirby 	ASSERT(dmu_tx_is_syncing(tx));
808ca45db41SChris Kirby 
809*ad135b5dSChristopher Siden 	dp->dp_tmp_userrefs_obj = zap_create_link(mos, DMU_OT_USERREFS,
810*ad135b5dSChristopher Siden 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_TMP_USERREFS, tx);
811ca45db41SChris Kirby }
812ca45db41SChris Kirby 
813ca45db41SChris Kirby static int
814ca45db41SChris Kirby dsl_pool_user_hold_rele_impl(dsl_pool_t *dp, uint64_t dsobj,
81515508ac0SChris Kirby     const char *tag, uint64_t *now, dmu_tx_t *tx, boolean_t holding)
816ca45db41SChris Kirby {
817ca45db41SChris Kirby 	objset_t *mos = dp->dp_meta_objset;
818ca45db41SChris Kirby 	uint64_t zapobj = dp->dp_tmp_userrefs_obj;
819ca45db41SChris Kirby 	char *name;
820ca45db41SChris Kirby 	int error;
821ca45db41SChris Kirby 
822ca45db41SChris Kirby 	ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
823ca45db41SChris Kirby 	ASSERT(dmu_tx_is_syncing(tx));
824ca45db41SChris Kirby 
825ca45db41SChris Kirby 	/*
826ca45db41SChris Kirby 	 * If the pool was created prior to SPA_VERSION_USERREFS, the
827ca45db41SChris Kirby 	 * zap object for temporary holds might not exist yet.
828ca45db41SChris Kirby 	 */
829ca45db41SChris Kirby 	if (zapobj == 0) {
830ca45db41SChris Kirby 		if (holding) {
831ca45db41SChris Kirby 			dsl_pool_user_hold_create_obj(dp, tx);
832ca45db41SChris Kirby 			zapobj = dp->dp_tmp_userrefs_obj;
833ca45db41SChris Kirby 		} else {
834ca45db41SChris Kirby 			return (ENOENT);
835ca45db41SChris Kirby 		}
836ca45db41SChris Kirby 	}
837ca45db41SChris Kirby 
838ca45db41SChris Kirby 	name = kmem_asprintf("%llx-%s", (u_longlong_t)dsobj, tag);
839ca45db41SChris Kirby 	if (holding)
84015508ac0SChris Kirby 		error = zap_add(mos, zapobj, name, 8, 1, now, tx);
841ca45db41SChris Kirby 	else
842ca45db41SChris Kirby 		error = zap_remove(mos, zapobj, name, tx);
843ca45db41SChris Kirby 	strfree(name);
844ca45db41SChris Kirby 
845ca45db41SChris Kirby 	return (error);
846ca45db41SChris Kirby }
847ca45db41SChris Kirby 
848ca45db41SChris Kirby /*
849ca45db41SChris Kirby  * Add a temporary hold for the given dataset object and tag.
850ca45db41SChris Kirby  */
851ca45db41SChris Kirby int
852ca45db41SChris Kirby dsl_pool_user_hold(dsl_pool_t *dp, uint64_t dsobj, const char *tag,
85315508ac0SChris Kirby     uint64_t *now, dmu_tx_t *tx)
854ca45db41SChris Kirby {
85515508ac0SChris Kirby 	return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, now, tx, B_TRUE));
856ca45db41SChris Kirby }
857ca45db41SChris Kirby 
858ca45db41SChris Kirby /*
859ca45db41SChris Kirby  * Release a temporary hold for the given dataset object and tag.
860ca45db41SChris Kirby  */
861ca45db41SChris Kirby int
862ca45db41SChris Kirby dsl_pool_user_release(dsl_pool_t *dp, uint64_t dsobj, const char *tag,
863ca45db41SChris Kirby     dmu_tx_t *tx)
864ca45db41SChris Kirby {
865ca45db41SChris Kirby 	return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, NULL,
866ca45db41SChris Kirby 	    tx, B_FALSE));
867ca45db41SChris Kirby }
868