xref: /illumos-gate/usr/src/uts/common/fs/zfs/dsl_pool.c (revision b7b2590dd9f11b12a0b4878db3886068cce176af)
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.
2394c2d0ebSMatthew Ahrens  * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
24a7a845e4SSteven Hartland  * Copyright (c) 2013 Steven Hartland. All rights reserved.
25bc9014e6SJustin Gibbs  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
26c3d26abcSMatthew Ahrens  * Copyright (c) 2014 Integros [integros.com]
27ff5177eeSAlek Pinchuk  * Copyright 2016 Nexenta Systems, Inc.  All rights reserved.
28fa9e4066Sahrens  */
29fa9e4066Sahrens 
30fa9e4066Sahrens #include <sys/dsl_pool.h>
31fa9e4066Sahrens #include <sys/dsl_dataset.h>
323f9d6ad7SLin Ling #include <sys/dsl_prop.h>
33fa9e4066Sahrens #include <sys/dsl_dir.h>
341d452cf5Sahrens #include <sys/dsl_synctask.h>
353f9d6ad7SLin Ling #include <sys/dsl_scan.h>
363f9d6ad7SLin Ling #include <sys/dnode.h>
37fa9e4066Sahrens #include <sys/dmu_tx.h>
38fa9e4066Sahrens #include <sys/dmu_objset.h>
39fa9e4066Sahrens #include <sys/arc.h>
40fa9e4066Sahrens #include <sys/zap.h>
41c717a561Smaybee #include <sys/zio.h>
42fa9e4066Sahrens #include <sys/zfs_context.h>
43fa9e4066Sahrens #include <sys/fs/zfs.h>
44088f3894Sahrens #include <sys/zfs_znode.h>
45088f3894Sahrens #include <sys/spa_impl.h>
46cde58dbcSMatthew Ahrens #include <sys/dsl_deadlist.h>
47ad135b5dSChristopher Siden #include <sys/bptree.h>
48ad135b5dSChristopher Siden #include <sys/zfeature.h>
49ce636f8bSMatthew Ahrens #include <sys/zil_impl.h>
503b2aab18SMatthew Ahrens #include <sys/dsl_userhold.h>
51fa9e4066Sahrens 
5269962b56SMatthew Ahrens /*
5369962b56SMatthew Ahrens  * ZFS Write Throttle
5469962b56SMatthew Ahrens  * ------------------
5569962b56SMatthew Ahrens  *
5669962b56SMatthew Ahrens  * ZFS must limit the rate of incoming writes to the rate at which it is able
5769962b56SMatthew Ahrens  * to sync data modifications to the backend storage. Throttling by too much
5869962b56SMatthew Ahrens  * creates an artificial limit; throttling by too little can only be sustained
5969962b56SMatthew Ahrens  * for short periods and would lead to highly lumpy performance. On a per-pool
6069962b56SMatthew Ahrens  * basis, ZFS tracks the amount of modified (dirty) data. As operations change
6169962b56SMatthew Ahrens  * data, the amount of dirty data increases; as ZFS syncs out data, the amount
6269962b56SMatthew Ahrens  * of dirty data decreases. When the amount of dirty data exceeds a
6369962b56SMatthew Ahrens  * predetermined threshold further modifications are blocked until the amount
6469962b56SMatthew Ahrens  * of dirty data decreases (as data is synced out).
6569962b56SMatthew Ahrens  *
6669962b56SMatthew Ahrens  * The limit on dirty data is tunable, and should be adjusted according to
6769962b56SMatthew Ahrens  * both the IO capacity and available memory of the system. The larger the
6869962b56SMatthew Ahrens  * window, the more ZFS is able to aggregate and amortize metadata (and data)
6969962b56SMatthew Ahrens  * changes. However, memory is a limited resource, and allowing for more dirty
7069962b56SMatthew Ahrens  * data comes at the cost of keeping other useful data in memory (for example
7169962b56SMatthew Ahrens  * ZFS data cached by the ARC).
7269962b56SMatthew Ahrens  *
7369962b56SMatthew Ahrens  * Implementation
7469962b56SMatthew Ahrens  *
7569962b56SMatthew Ahrens  * As buffers are modified dsl_pool_willuse_space() increments both the per-
7669962b56SMatthew Ahrens  * txg (dp_dirty_pertxg[]) and poolwide (dp_dirty_total) accounting of
7769962b56SMatthew Ahrens  * dirty space used; dsl_pool_dirty_space() decrements those values as data
7869962b56SMatthew Ahrens  * is synced out from dsl_pool_sync(). While only the poolwide value is
7969962b56SMatthew Ahrens  * relevant, the per-txg value is useful for debugging. The tunable
8069962b56SMatthew Ahrens  * zfs_dirty_data_max determines the dirty space limit. Once that value is
8169962b56SMatthew Ahrens  * exceeded, new writes are halted until space frees up.
8269962b56SMatthew Ahrens  *
8369962b56SMatthew Ahrens  * The zfs_dirty_data_sync tunable dictates the threshold at which we
8469962b56SMatthew Ahrens  * ensure that there is a txg syncing (see the comment in txg.c for a full
8569962b56SMatthew Ahrens  * description of transaction group stages).
8669962b56SMatthew Ahrens  *
8769962b56SMatthew Ahrens  * The IO scheduler uses both the dirty space limit and current amount of
8869962b56SMatthew Ahrens  * dirty data as inputs. Those values affect the number of concurrent IOs ZFS
8969962b56SMatthew Ahrens  * issues. See the comment in vdev_queue.c for details of the IO scheduler.
9069962b56SMatthew Ahrens  *
9169962b56SMatthew Ahrens  * The delay is also calculated based on the amount of dirty data.  See the
9269962b56SMatthew Ahrens  * comment above dmu_tx_delay() for details.
9369962b56SMatthew Ahrens  */
9469962b56SMatthew Ahrens 
9569962b56SMatthew Ahrens /*
9669962b56SMatthew Ahrens  * zfs_dirty_data_max will be set to zfs_dirty_data_max_percent% of all memory,
9769962b56SMatthew Ahrens  * capped at zfs_dirty_data_max_max.  It can also be overridden in /etc/system.
9869962b56SMatthew Ahrens  */
9969962b56SMatthew Ahrens uint64_t zfs_dirty_data_max;
10069962b56SMatthew Ahrens uint64_t zfs_dirty_data_max_max = 4ULL * 1024 * 1024 * 1024;
10169962b56SMatthew Ahrens int zfs_dirty_data_max_percent = 10;
10269962b56SMatthew Ahrens 
10369962b56SMatthew Ahrens /*
10469962b56SMatthew Ahrens  * If there is at least this much dirty data, push out a txg.
10569962b56SMatthew Ahrens  */
10669962b56SMatthew Ahrens uint64_t zfs_dirty_data_sync = 64 * 1024 * 1024;
10769962b56SMatthew Ahrens 
10869962b56SMatthew Ahrens /*
10969962b56SMatthew Ahrens  * Once there is this amount of dirty data, the dmu_tx_delay() will kick in
11069962b56SMatthew Ahrens  * and delay each transaction.
11169962b56SMatthew Ahrens  * This value should be >= zfs_vdev_async_write_active_max_dirty_percent.
11269962b56SMatthew Ahrens  */
11369962b56SMatthew Ahrens int zfs_delay_min_dirty_percent = 60;
11469962b56SMatthew Ahrens 
11569962b56SMatthew Ahrens /*
11669962b56SMatthew Ahrens  * This controls how quickly the delay approaches infinity.
117d85a1e96SMatthew Ahrens  * Larger values cause it to delay more for a given amount of dirty data.
118d85a1e96SMatthew Ahrens  * Therefore larger values will cause there to be less dirty data for a
11969962b56SMatthew Ahrens  * given throughput.
12069962b56SMatthew Ahrens  *
12169962b56SMatthew Ahrens  * For the smoothest delay, this value should be about 1 billion divided
12269962b56SMatthew Ahrens  * by the maximum number of operations per second.  This will smoothly
12369962b56SMatthew Ahrens  * handle between 10x and 1/10th this number.
12469962b56SMatthew Ahrens  *
12569962b56SMatthew Ahrens  * Note: zfs_delay_scale * zfs_dirty_data_max must be < 2^64, due to the
12669962b56SMatthew Ahrens  * multiply in dmu_tx_delay().
12769962b56SMatthew Ahrens  */
12869962b56SMatthew Ahrens uint64_t zfs_delay_scale = 1000 * 1000 * 1000 / 2000;
12905715f94SMark Maybee 
13094c2d0ebSMatthew Ahrens /*
13194c2d0ebSMatthew Ahrens  * This determines the number of threads used by the dp_sync_taskq.
13294c2d0ebSMatthew Ahrens  */
13394c2d0ebSMatthew Ahrens int zfs_sync_taskq_batch_pct = 75;
1341ab7f2deSmaybee 
1353f9d6ad7SLin Ling int
136088f3894Sahrens dsl_pool_open_special_dir(dsl_pool_t *dp, const char *name, dsl_dir_t **ddp)
137fa9e4066Sahrens {
138fa9e4066Sahrens 	uint64_t obj;
139fa9e4066Sahrens 	int err;
140fa9e4066Sahrens 
141fa9e4066Sahrens 	err = zap_lookup(dp->dp_meta_objset,
142c1379625SJustin T. Gibbs 	    dsl_dir_phys(dp->dp_root_dir)->dd_child_dir_zapobj,
143088f3894Sahrens 	    name, sizeof (obj), 1, &obj);
144ea8dc4b6Seschrock 	if (err)
145ea8dc4b6Seschrock 		return (err);
146fa9e4066Sahrens 
1473b2aab18SMatthew Ahrens 	return (dsl_dir_hold_obj(dp, obj, name, dp, ddp));
148fa9e4066Sahrens }
149fa9e4066Sahrens 
150fa9e4066Sahrens static dsl_pool_t *
151fa9e4066Sahrens dsl_pool_open_impl(spa_t *spa, uint64_t txg)
152fa9e4066Sahrens {
153fa9e4066Sahrens 	dsl_pool_t *dp;
154fa9e4066Sahrens 	blkptr_t *bp = spa_get_rootblkptr(spa);
155fa9e4066Sahrens 
156fa9e4066Sahrens 	dp = kmem_zalloc(sizeof (dsl_pool_t), KM_SLEEP);
157fa9e4066Sahrens 	dp->dp_spa = spa;
158fa9e4066Sahrens 	dp->dp_meta_rootbp = *bp;
1593b2aab18SMatthew Ahrens 	rrw_init(&dp->dp_config_rwlock, B_TRUE);
160fa9e4066Sahrens 	txg_init(dp, txg);
161fa9e4066Sahrens 
162*b7b2590dSMatthew Ahrens 	txg_list_create(&dp->dp_dirty_datasets, spa,
163fa9e4066Sahrens 	    offsetof(dsl_dataset_t, ds_dirty_link));
164*b7b2590dSMatthew Ahrens 	txg_list_create(&dp->dp_dirty_zilogs, spa,
165ce636f8bSMatthew Ahrens 	    offsetof(zilog_t, zl_dirty_link));
166*b7b2590dSMatthew Ahrens 	txg_list_create(&dp->dp_dirty_dirs, spa,
167fa9e4066Sahrens 	    offsetof(dsl_dir_t, dd_dirty_link));
168*b7b2590dSMatthew Ahrens 	txg_list_create(&dp->dp_sync_tasks, spa,
1693b2aab18SMatthew Ahrens 	    offsetof(dsl_sync_task_t, dst_node));
170fa9e4066Sahrens 
17194c2d0ebSMatthew Ahrens 	dp->dp_sync_taskq = taskq_create("dp_sync_taskq",
17294c2d0ebSMatthew Ahrens 	    zfs_sync_taskq_batch_pct, minclsyspri, 1, INT_MAX,
17394c2d0ebSMatthew Ahrens 	    TASKQ_THREADS_CPU_PCT);
17494c2d0ebSMatthew Ahrens 
1751ab7f2deSmaybee 	mutex_init(&dp->dp_lock, NULL, MUTEX_DEFAULT, NULL);
17669962b56SMatthew Ahrens 	cv_init(&dp->dp_spaceavail_cv, NULL, CV_DEFAULT, NULL);
1771ab7f2deSmaybee 
1789d3574bfSNeil Perrin 	dp->dp_vnrele_taskq = taskq_create("zfs_vn_rele_taskq", 1, minclsyspri,
1799d3574bfSNeil Perrin 	    1, 4, 0);
1809d3574bfSNeil Perrin 
181fa9e4066Sahrens 	return (dp);
182fa9e4066Sahrens }
183fa9e4066Sahrens 
184ea8dc4b6Seschrock int
185ad135b5dSChristopher Siden dsl_pool_init(spa_t *spa, uint64_t txg, dsl_pool_t **dpp)
186fa9e4066Sahrens {
187fa9e4066Sahrens 	int err;
188fa9e4066Sahrens 	dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
189ad135b5dSChristopher Siden 
190ad135b5dSChristopher Siden 	err = dmu_objset_open_impl(spa, NULL, &dp->dp_meta_rootbp,
191ad135b5dSChristopher Siden 	    &dp->dp_meta_objset);
192ad135b5dSChristopher Siden 	if (err != 0)
193ad135b5dSChristopher Siden 		dsl_pool_close(dp);
194ad135b5dSChristopher Siden 	else
195ad135b5dSChristopher Siden 		*dpp = dp;
196ad135b5dSChristopher Siden 
197ad135b5dSChristopher Siden 	return (err);
198ad135b5dSChristopher Siden }
199ad135b5dSChristopher Siden 
200ad135b5dSChristopher Siden int
201ad135b5dSChristopher Siden dsl_pool_open(dsl_pool_t *dp)
202ad135b5dSChristopher Siden {
203ad135b5dSChristopher Siden 	int err;
204088f3894Sahrens 	dsl_dir_t *dd;
205088f3894Sahrens 	dsl_dataset_t *ds;
206cde58dbcSMatthew Ahrens 	uint64_t obj;
207fa9e4066Sahrens 
2083b2aab18SMatthew Ahrens 	rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
209fa9e4066Sahrens 	err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
210fa9e4066Sahrens 	    DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1,
211fa9e4066Sahrens 	    &dp->dp_root_dir_obj);
212ea8dc4b6Seschrock 	if (err)
213ea8dc4b6Seschrock 		goto out;
214ea8dc4b6Seschrock 
2153b2aab18SMatthew Ahrens 	err = dsl_dir_hold_obj(dp, dp->dp_root_dir_obj,
216ea8dc4b6Seschrock 	    NULL, dp, &dp->dp_root_dir);
217ea8dc4b6Seschrock 	if (err)
218ea8dc4b6Seschrock 		goto out;
219fa9e4066Sahrens 
220088f3894Sahrens 	err = dsl_pool_open_special_dir(dp, MOS_DIR_NAME, &dp->dp_mos_dir);
221ea8dc4b6Seschrock 	if (err)
222ea8dc4b6Seschrock 		goto out;
223ea8dc4b6Seschrock 
224ad135b5dSChristopher Siden 	if (spa_version(dp->dp_spa) >= SPA_VERSION_ORIGIN) {
225088f3894Sahrens 		err = dsl_pool_open_special_dir(dp, ORIGIN_DIR_NAME, &dd);
226088f3894Sahrens 		if (err)
227088f3894Sahrens 			goto out;
228c1379625SJustin T. Gibbs 		err = dsl_dataset_hold_obj(dp,
229c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_head_dataset_obj, FTAG, &ds);
2308f63aa46SLin Ling 		if (err == 0) {
2318f63aa46SLin Ling 			err = dsl_dataset_hold_obj(dp,
232c1379625SJustin T. Gibbs 			    dsl_dataset_phys(ds)->ds_prev_snap_obj, dp,
2338f63aa46SLin Ling 			    &dp->dp_origin_snap);
2348f63aa46SLin Ling 			dsl_dataset_rele(ds, FTAG);
2358f63aa46SLin Ling 		}
2363b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, dp);
237088f3894Sahrens 		if (err)
238088f3894Sahrens 			goto out;
239088f3894Sahrens 	}
240088f3894Sahrens 
241ad135b5dSChristopher Siden 	if (spa_version(dp->dp_spa) >= SPA_VERSION_DEADLISTS) {
242cde58dbcSMatthew Ahrens 		err = dsl_pool_open_special_dir(dp, FREE_DIR_NAME,
243cde58dbcSMatthew Ahrens 		    &dp->dp_free_dir);
244cde58dbcSMatthew Ahrens 		if (err)
245cde58dbcSMatthew Ahrens 			goto out;
246cde58dbcSMatthew Ahrens 
247cde58dbcSMatthew Ahrens 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
248cde58dbcSMatthew Ahrens 		    DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj);
249cde58dbcSMatthew Ahrens 		if (err)
250cde58dbcSMatthew Ahrens 			goto out;
2513b2aab18SMatthew Ahrens 		VERIFY0(bpobj_open(&dp->dp_free_bpobj,
252cde58dbcSMatthew Ahrens 		    dp->dp_meta_objset, obj));
253cde58dbcSMatthew Ahrens 	}
254cde58dbcSMatthew Ahrens 
2557fd05ac4SMatthew Ahrens 	/*
2567fd05ac4SMatthew Ahrens 	 * Note: errors ignored, because the leak dir will not exist if we
2577fd05ac4SMatthew Ahrens 	 * have not encountered a leak yet.
2587fd05ac4SMatthew Ahrens 	 */
2597fd05ac4SMatthew Ahrens 	(void) dsl_pool_open_special_dir(dp, LEAK_DIR_NAME,
2607fd05ac4SMatthew Ahrens 	    &dp->dp_leak_dir);
2617fd05ac4SMatthew Ahrens 
2622acef22dSMatthew Ahrens 	if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_ASYNC_DESTROY)) {
263ad135b5dSChristopher Siden 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
264ad135b5dSChristopher Siden 		    DMU_POOL_BPTREE_OBJ, sizeof (uint64_t), 1,
265ad135b5dSChristopher Siden 		    &dp->dp_bptree_obj);
266ad135b5dSChristopher Siden 		if (err != 0)
267ad135b5dSChristopher Siden 			goto out;
268ad135b5dSChristopher Siden 	}
269ad135b5dSChristopher Siden 
2702acef22dSMatthew Ahrens 	if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_EMPTY_BPOBJ)) {
271f1745736SMatthew Ahrens 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
272f1745736SMatthew Ahrens 		    DMU_POOL_EMPTY_BPOBJ, sizeof (uint64_t), 1,
273f1745736SMatthew Ahrens 		    &dp->dp_empty_bpobj);
274f1745736SMatthew Ahrens 		if (err != 0)
275f1745736SMatthew Ahrens 			goto out;
276f1745736SMatthew Ahrens 	}
277f1745736SMatthew Ahrens 
278ca45db41SChris Kirby 	err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
279ca45db41SChris Kirby 	    DMU_POOL_TMP_USERREFS, sizeof (uint64_t), 1,
280ca45db41SChris Kirby 	    &dp->dp_tmp_userrefs_obj);
281ca45db41SChris Kirby 	if (err == ENOENT)
282ca45db41SChris Kirby 		err = 0;
283ca45db41SChris Kirby 	if (err)
284ca45db41SChris Kirby 		goto out;
285ca45db41SChris Kirby 
286ad135b5dSChristopher Siden 	err = dsl_scan_init(dp, dp->dp_tx.tx_open_txg);
287088f3894Sahrens 
288ea8dc4b6Seschrock out:
2893b2aab18SMatthew Ahrens 	rrw_exit(&dp->dp_config_rwlock, FTAG);
290ea8dc4b6Seschrock 	return (err);
291fa9e4066Sahrens }
292fa9e4066Sahrens 
293fa9e4066Sahrens void
294fa9e4066Sahrens dsl_pool_close(dsl_pool_t *dp)
295fa9e4066Sahrens {
296088f3894Sahrens 	/*
29769962b56SMatthew Ahrens 	 * Drop our references from dsl_pool_open().
29869962b56SMatthew Ahrens 	 *
299088f3894Sahrens 	 * Since we held the origin_snap from "syncing" context (which
300088f3894Sahrens 	 * includes pool-opening context), it actually only got a "ref"
301088f3894Sahrens 	 * and not a hold, so just drop that here.
302088f3894Sahrens 	 */
303088f3894Sahrens 	if (dp->dp_origin_snap)
3043b2aab18SMatthew Ahrens 		dsl_dataset_rele(dp->dp_origin_snap, dp);
305ea8dc4b6Seschrock 	if (dp->dp_mos_dir)
3063b2aab18SMatthew Ahrens 		dsl_dir_rele(dp->dp_mos_dir, dp);
307cde58dbcSMatthew Ahrens 	if (dp->dp_free_dir)
3083b2aab18SMatthew Ahrens 		dsl_dir_rele(dp->dp_free_dir, dp);
3097fd05ac4SMatthew Ahrens 	if (dp->dp_leak_dir)
3107fd05ac4SMatthew Ahrens 		dsl_dir_rele(dp->dp_leak_dir, dp);
311ea8dc4b6Seschrock 	if (dp->dp_root_dir)
3123b2aab18SMatthew Ahrens 		dsl_dir_rele(dp->dp_root_dir, dp);
313fa9e4066Sahrens 
314cde58dbcSMatthew Ahrens 	bpobj_close(&dp->dp_free_bpobj);
315cde58dbcSMatthew Ahrens 
316fa9e4066Sahrens 	/* undo the dmu_objset_open_impl(mos) from dsl_pool_open() */
317ea8dc4b6Seschrock 	if (dp->dp_meta_objset)
318503ad85cSMatthew Ahrens 		dmu_objset_evict(dp->dp_meta_objset);
319fa9e4066Sahrens 
320fa9e4066Sahrens 	txg_list_destroy(&dp->dp_dirty_datasets);
321ce636f8bSMatthew Ahrens 	txg_list_destroy(&dp->dp_dirty_zilogs);
32254a91118SChris Kirby 	txg_list_destroy(&dp->dp_sync_tasks);
323fa9e4066Sahrens 	txg_list_destroy(&dp->dp_dirty_dirs);
324fa9e4066Sahrens 
32594c2d0ebSMatthew Ahrens 	taskq_destroy(dp->dp_sync_taskq);
32694c2d0ebSMatthew Ahrens 
327244781f1SPrakash Surya 	/*
328244781f1SPrakash Surya 	 * We can't set retry to TRUE since we're explicitly specifying
329244781f1SPrakash Surya 	 * a spa to flush. This is good enough; any missed buffers for
330244781f1SPrakash Surya 	 * this spa won't cause trouble, and they'll eventually fall
331244781f1SPrakash Surya 	 * out of the ARC just like any other unused buffer.
332244781f1SPrakash Surya 	 */
333244781f1SPrakash Surya 	arc_flush(dp->dp_spa, FALSE);
334244781f1SPrakash Surya 
335fa9e4066Sahrens 	txg_fini(dp);
3363f9d6ad7SLin Ling 	dsl_scan_fini(dp);
337bc9014e6SJustin Gibbs 	dmu_buf_user_evict_wait();
338bc9014e6SJustin Gibbs 
3393b2aab18SMatthew Ahrens 	rrw_destroy(&dp->dp_config_rwlock);
3401ab7f2deSmaybee 	mutex_destroy(&dp->dp_lock);
3419d3574bfSNeil Perrin 	taskq_destroy(dp->dp_vnrele_taskq);
34288b7b0f2SMatthew Ahrens 	if (dp->dp_blkstats)
34388b7b0f2SMatthew Ahrens 		kmem_free(dp->dp_blkstats, sizeof (zfs_all_blkstats_t));
344fa9e4066Sahrens 	kmem_free(dp, sizeof (dsl_pool_t));
345fa9e4066Sahrens }
346fa9e4066Sahrens 
347fa9e4066Sahrens dsl_pool_t *
3480a48a24eStimh dsl_pool_create(spa_t *spa, nvlist_t *zplprops, uint64_t txg)
349fa9e4066Sahrens {
350fa9e4066Sahrens 	int err;
351fa9e4066Sahrens 	dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
352fa9e4066Sahrens 	dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg);
353503ad85cSMatthew Ahrens 	objset_t *os;
354088f3894Sahrens 	dsl_dataset_t *ds;
355cde58dbcSMatthew Ahrens 	uint64_t obj;
356088f3894Sahrens 
3573b2aab18SMatthew Ahrens 	rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
3583b2aab18SMatthew Ahrens 
359088f3894Sahrens 	/* create and open the MOS (meta-objset) */
360503ad85cSMatthew Ahrens 	dp->dp_meta_objset = dmu_objset_create_impl(spa,
361503ad85cSMatthew Ahrens 	    NULL, &dp->dp_meta_rootbp, DMU_OST_META, tx);
362fa9e4066Sahrens 
363fa9e4066Sahrens 	/* create the pool directory */
364fa9e4066Sahrens 	err = zap_create_claim(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
365fa9e4066Sahrens 	    DMU_OT_OBJECT_DIRECTORY, DMU_OT_NONE, 0, tx);
366fb09f5aaSMadhav Suresh 	ASSERT0(err);
367fa9e4066Sahrens 
3683f9d6ad7SLin Ling 	/* Initialize scan structures */
3693b2aab18SMatthew Ahrens 	VERIFY0(dsl_scan_init(dp, txg));
3703f9d6ad7SLin Ling 
371fa9e4066Sahrens 	/* create and open the root dir */
372088f3894Sahrens 	dp->dp_root_dir_obj = dsl_dir_create_sync(dp, NULL, NULL, tx);
3733b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold_obj(dp, dp->dp_root_dir_obj,
374ea8dc4b6Seschrock 	    NULL, dp, &dp->dp_root_dir));
375fa9e4066Sahrens 
376fa9e4066Sahrens 	/* create and open the meta-objset dir */
377088f3894Sahrens 	(void) dsl_dir_create_sync(dp, dp->dp_root_dir, MOS_DIR_NAME, tx);
3783b2aab18SMatthew Ahrens 	VERIFY0(dsl_pool_open_special_dir(dp,
379088f3894Sahrens 	    MOS_DIR_NAME, &dp->dp_mos_dir));
380088f3894Sahrens 
381cde58dbcSMatthew Ahrens 	if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
382cde58dbcSMatthew Ahrens 		/* create and open the free dir */
383cde58dbcSMatthew Ahrens 		(void) dsl_dir_create_sync(dp, dp->dp_root_dir,
384cde58dbcSMatthew Ahrens 		    FREE_DIR_NAME, tx);
3853b2aab18SMatthew Ahrens 		VERIFY0(dsl_pool_open_special_dir(dp,
386cde58dbcSMatthew Ahrens 		    FREE_DIR_NAME, &dp->dp_free_dir));
387cde58dbcSMatthew Ahrens 
388cde58dbcSMatthew Ahrens 		/* create and open the free_bplist */
389b5152584SMatthew Ahrens 		obj = bpobj_alloc(dp->dp_meta_objset, SPA_OLD_MAXBLOCKSIZE, tx);
390cde58dbcSMatthew Ahrens 		VERIFY(zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
391cde58dbcSMatthew Ahrens 		    DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj, tx) == 0);
3923b2aab18SMatthew Ahrens 		VERIFY0(bpobj_open(&dp->dp_free_bpobj,
393cde58dbcSMatthew Ahrens 		    dp->dp_meta_objset, obj));
394cde58dbcSMatthew Ahrens 	}
395cde58dbcSMatthew Ahrens 
396088f3894Sahrens 	if (spa_version(spa) >= SPA_VERSION_DSL_SCRUB)
397088f3894Sahrens 		dsl_pool_create_origin(dp, tx);
398088f3894Sahrens 
399088f3894Sahrens 	/* create the root dataset */
400cde58dbcSMatthew Ahrens 	obj = dsl_dataset_create_sync_dd(dp->dp_root_dir, NULL, 0, tx);
401088f3894Sahrens 
402088f3894Sahrens 	/* create the root objset */
4033b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(dp, obj, FTAG, &ds));
404c166b69dSPaul Dagnelie 	rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
405503ad85cSMatthew Ahrens 	os = dmu_objset_create_impl(dp->dp_spa, ds,
406088f3894Sahrens 	    dsl_dataset_get_blkptr(ds), DMU_OST_ZFS, tx);
407c166b69dSPaul Dagnelie 	rrw_exit(&ds->ds_bp_rwlock, FTAG);
408088f3894Sahrens #ifdef _KERNEL
409503ad85cSMatthew Ahrens 	zfs_create_fs(os, kcred, zplprops, tx);
410088f3894Sahrens #endif
411088f3894Sahrens 	dsl_dataset_rele(ds, FTAG);
412fa9e4066Sahrens 
413fa9e4066Sahrens 	dmu_tx_commit(tx);
414fa9e4066Sahrens 
4153b2aab18SMatthew Ahrens 	rrw_exit(&dp->dp_config_rwlock, FTAG);
4163b2aab18SMatthew Ahrens 
417fa9e4066Sahrens 	return (dp);
418fa9e4066Sahrens }
419fa9e4066Sahrens 
420ce636f8bSMatthew Ahrens /*
421ce636f8bSMatthew Ahrens  * Account for the meta-objset space in its placeholder dsl_dir.
422ce636f8bSMatthew Ahrens  */
423ce636f8bSMatthew Ahrens void
424ce636f8bSMatthew Ahrens dsl_pool_mos_diduse_space(dsl_pool_t *dp,
425ce636f8bSMatthew Ahrens     int64_t used, int64_t comp, int64_t uncomp)
426ce636f8bSMatthew Ahrens {
427ce636f8bSMatthew Ahrens 	ASSERT3U(comp, ==, uncomp); /* it's all metadata */
428ce636f8bSMatthew Ahrens 	mutex_enter(&dp->dp_lock);
429ce636f8bSMatthew Ahrens 	dp->dp_mos_used_delta += used;
430ce636f8bSMatthew Ahrens 	dp->dp_mos_compressed_delta += comp;
431ce636f8bSMatthew Ahrens 	dp->dp_mos_uncompressed_delta += uncomp;
432ce636f8bSMatthew Ahrens 	mutex_exit(&dp->dp_lock);
433ce636f8bSMatthew Ahrens }
434ce636f8bSMatthew Ahrens 
43569962b56SMatthew Ahrens static void
43669962b56SMatthew Ahrens dsl_pool_sync_mos(dsl_pool_t *dp, dmu_tx_t *tx)
43769962b56SMatthew Ahrens {
43869962b56SMatthew Ahrens 	zio_t *zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
43969962b56SMatthew Ahrens 	dmu_objset_sync(dp->dp_meta_objset, zio, tx);
44069962b56SMatthew Ahrens 	VERIFY0(zio_wait(zio));
44169962b56SMatthew Ahrens 	dprintf_bp(&dp->dp_meta_rootbp, "meta objset rootbp is %s", "");
44269962b56SMatthew Ahrens 	spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
44369962b56SMatthew Ahrens }
44469962b56SMatthew Ahrens 
44569962b56SMatthew Ahrens static void
44669962b56SMatthew Ahrens dsl_pool_dirty_delta(dsl_pool_t *dp, int64_t delta)
44769962b56SMatthew Ahrens {
44869962b56SMatthew Ahrens 	ASSERT(MUTEX_HELD(&dp->dp_lock));
44969962b56SMatthew Ahrens 
45069962b56SMatthew Ahrens 	if (delta < 0)
45169962b56SMatthew Ahrens 		ASSERT3U(-delta, <=, dp->dp_dirty_total);
45269962b56SMatthew Ahrens 
45369962b56SMatthew Ahrens 	dp->dp_dirty_total += delta;
45469962b56SMatthew Ahrens 
45569962b56SMatthew Ahrens 	/*
45669962b56SMatthew Ahrens 	 * Note: we signal even when increasing dp_dirty_total.
45769962b56SMatthew Ahrens 	 * This ensures forward progress -- each thread wakes the next waiter.
45869962b56SMatthew Ahrens 	 */
459313ae1e1SAndriy Gapon 	if (dp->dp_dirty_total < zfs_dirty_data_max)
46069962b56SMatthew Ahrens 		cv_signal(&dp->dp_spaceavail_cv);
46169962b56SMatthew Ahrens }
46269962b56SMatthew Ahrens 
463fa9e4066Sahrens void
464fa9e4066Sahrens dsl_pool_sync(dsl_pool_t *dp, uint64_t txg)
465fa9e4066Sahrens {
466c717a561Smaybee 	zio_t *zio;
467fa9e4066Sahrens 	dmu_tx_t *tx;
468c717a561Smaybee 	dsl_dir_t *dd;
469c717a561Smaybee 	dsl_dataset_t *ds;
470503ad85cSMatthew Ahrens 	objset_t *mos = dp->dp_meta_objset;
471ce636f8bSMatthew Ahrens 	list_t synced_datasets;
472ce636f8bSMatthew Ahrens 
473ce636f8bSMatthew Ahrens 	list_create(&synced_datasets, sizeof (dsl_dataset_t),
474ce636f8bSMatthew Ahrens 	    offsetof(dsl_dataset_t, ds_synced_link));
475fa9e4066Sahrens 
476fa9e4066Sahrens 	tx = dmu_tx_create_assigned(dp, txg);
477fa9e4066Sahrens 
47869962b56SMatthew Ahrens 	/*
47969962b56SMatthew Ahrens 	 * Write out all dirty blocks of dirty datasets.
48069962b56SMatthew Ahrens 	 */
481c717a561Smaybee 	zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
48269962b56SMatthew Ahrens 	while ((ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) != NULL) {
48314843421SMatthew Ahrens 		/*
48414843421SMatthew Ahrens 		 * We must not sync any non-MOS datasets twice, because
48514843421SMatthew Ahrens 		 * we may have taken a snapshot of them.  However, we
48614843421SMatthew Ahrens 		 * may sync newly-created datasets on pass 2.
48714843421SMatthew Ahrens 		 */
48814843421SMatthew Ahrens 		ASSERT(!list_link_active(&ds->ds_synced_link));
489ce636f8bSMatthew Ahrens 		list_insert_tail(&synced_datasets, ds);
490c717a561Smaybee 		dsl_dataset_sync(ds, zio, tx);
491c717a561Smaybee 	}
49269962b56SMatthew Ahrens 	VERIFY0(zio_wait(zio));
49314843421SMatthew Ahrens 
49469962b56SMatthew Ahrens 	/*
49569962b56SMatthew Ahrens 	 * We have written all of the accounted dirty data, so our
49669962b56SMatthew Ahrens 	 * dp_space_towrite should now be zero.  However, some seldom-used
49769962b56SMatthew Ahrens 	 * code paths do not adhere to this (e.g. dbuf_undirty(), also
49869962b56SMatthew Ahrens 	 * rounding error in dbuf_write_physdone).
49969962b56SMatthew Ahrens 	 * Shore up the accounting of any dirtied space now.
50069962b56SMatthew Ahrens 	 */
50169962b56SMatthew Ahrens 	dsl_pool_undirty_space(dp, dp->dp_dirty_pertxg[txg & TXG_MASK], txg);
502c717a561Smaybee 
503ff5177eeSAlek Pinchuk 	/*
504ff5177eeSAlek Pinchuk 	 * Update the long range free counter after
505ff5177eeSAlek Pinchuk 	 * we're done syncing user data
506ff5177eeSAlek Pinchuk 	 */
507ff5177eeSAlek Pinchuk 	mutex_enter(&dp->dp_lock);
508ff5177eeSAlek Pinchuk 	ASSERT(spa_sync_pass(dp->dp_spa) == 1 ||
509ff5177eeSAlek Pinchuk 	    dp->dp_long_free_dirty_pertxg[txg & TXG_MASK] == 0);
510ff5177eeSAlek Pinchuk 	dp->dp_long_free_dirty_pertxg[txg & TXG_MASK] = 0;
511ff5177eeSAlek Pinchuk 	mutex_exit(&dp->dp_lock);
512ff5177eeSAlek Pinchuk 
513ce636f8bSMatthew Ahrens 	/*
514ce636f8bSMatthew Ahrens 	 * After the data blocks have been written (ensured by the zio_wait()
51594c2d0ebSMatthew Ahrens 	 * above), update the user/group space accounting.  This happens
51694c2d0ebSMatthew Ahrens 	 * in tasks dispatched to dp_sync_taskq, so wait for them before
51794c2d0ebSMatthew Ahrens 	 * continuing.
518ce636f8bSMatthew Ahrens 	 */
51969962b56SMatthew Ahrens 	for (ds = list_head(&synced_datasets); ds != NULL;
52069962b56SMatthew Ahrens 	    ds = list_next(&synced_datasets, ds)) {
5210a586ceaSMark Shellenbaum 		dmu_objset_do_userquota_updates(ds->ds_objset, tx);
52269962b56SMatthew Ahrens 	}
52394c2d0ebSMatthew Ahrens 	taskq_wait(dp->dp_sync_taskq);
52414843421SMatthew Ahrens 
52514843421SMatthew Ahrens 	/*
52614843421SMatthew Ahrens 	 * Sync the datasets again to push out the changes due to
5273f9d6ad7SLin Ling 	 * userspace updates.  This must be done before we process the
528ce636f8bSMatthew Ahrens 	 * sync tasks, so that any snapshots will have the correct
529ce636f8bSMatthew Ahrens 	 * user accounting information (and we won't get confused
530ce636f8bSMatthew Ahrens 	 * about which blocks are part of the snapshot).
53114843421SMatthew Ahrens 	 */
53214843421SMatthew Ahrens 	zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
53369962b56SMatthew Ahrens 	while ((ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) != NULL) {
53414843421SMatthew Ahrens 		ASSERT(list_link_active(&ds->ds_synced_link));
53514843421SMatthew Ahrens 		dmu_buf_rele(ds->ds_dbuf, ds);
53614843421SMatthew Ahrens 		dsl_dataset_sync(ds, zio, tx);
53714843421SMatthew Ahrens 	}
53869962b56SMatthew Ahrens 	VERIFY0(zio_wait(zio));
53914843421SMatthew Ahrens 
540b24ab676SJeff Bonwick 	/*
541ce636f8bSMatthew Ahrens 	 * Now that the datasets have been completely synced, we can
542ce636f8bSMatthew Ahrens 	 * clean up our in-memory structures accumulated while syncing:
543ce636f8bSMatthew Ahrens 	 *
544ce636f8bSMatthew Ahrens 	 *  - move dead blocks from the pending deadlist to the on-disk deadlist
545ce636f8bSMatthew Ahrens 	 *  - release hold from dsl_dataset_dirty()
546b24ab676SJeff Bonwick 	 */
54769962b56SMatthew Ahrens 	while ((ds = list_remove_head(&synced_datasets)) != NULL) {
548bfaed0b9SAndriy Gapon 		dsl_dataset_sync_done(ds, tx);
549cde58dbcSMatthew Ahrens 	}
55069962b56SMatthew Ahrens 	while ((dd = txg_list_remove(&dp->dp_dirty_dirs, txg)) != NULL) {
551c717a561Smaybee 		dsl_dir_sync(dd, tx);
55269962b56SMatthew Ahrens 	}
553fa9e4066Sahrens 
554ce636f8bSMatthew Ahrens 	/*
555ce636f8bSMatthew Ahrens 	 * The MOS's space is accounted for in the pool/$MOS
556ce636f8bSMatthew Ahrens 	 * (dp_mos_dir).  We can't modify the mos while we're syncing
557ce636f8bSMatthew Ahrens 	 * it, so we remember the deltas and apply them here.
558ce636f8bSMatthew Ahrens 	 */
559ce636f8bSMatthew Ahrens 	if (dp->dp_mos_used_delta != 0 || dp->dp_mos_compressed_delta != 0 ||
560ce636f8bSMatthew Ahrens 	    dp->dp_mos_uncompressed_delta != 0) {
561ce636f8bSMatthew Ahrens 		dsl_dir_diduse_space(dp->dp_mos_dir, DD_USED_HEAD,
562ce636f8bSMatthew Ahrens 		    dp->dp_mos_used_delta,
563ce636f8bSMatthew Ahrens 		    dp->dp_mos_compressed_delta,
564ce636f8bSMatthew Ahrens 		    dp->dp_mos_uncompressed_delta, tx);
565ce636f8bSMatthew Ahrens 		dp->dp_mos_used_delta = 0;
566ce636f8bSMatthew Ahrens 		dp->dp_mos_compressed_delta = 0;
567ce636f8bSMatthew Ahrens 		dp->dp_mos_uncompressed_delta = 0;
568ce636f8bSMatthew Ahrens 	}
569ce636f8bSMatthew Ahrens 
57094c2d0ebSMatthew Ahrens 	if (!multilist_is_empty(mos->os_dirty_dnodes[txg & TXG_MASK])) {
57169962b56SMatthew Ahrens 		dsl_pool_sync_mos(dp, tx);
572fa9e4066Sahrens 	}
573fa9e4066Sahrens 
574ce636f8bSMatthew Ahrens 	/*
575ce636f8bSMatthew Ahrens 	 * If we modify a dataset in the same txg that we want to destroy it,
576ce636f8bSMatthew Ahrens 	 * its dsl_dir's dd_dbuf will be dirty, and thus have a hold on it.
577ce636f8bSMatthew Ahrens 	 * dsl_dir_destroy_check() will fail if there are unexpected holds.
578ce636f8bSMatthew Ahrens 	 * Therefore, we want to sync the MOS (thus syncing the dd_dbuf
579ce636f8bSMatthew Ahrens 	 * and clearing the hold on it) before we process the sync_tasks.
580ce636f8bSMatthew Ahrens 	 * The MOS data dirtied by the sync_tasks will be synced on the next
581ce636f8bSMatthew Ahrens 	 * pass.
582ce636f8bSMatthew Ahrens 	 */
583ce636f8bSMatthew Ahrens 	if (!txg_list_empty(&dp->dp_sync_tasks, txg)) {
5843b2aab18SMatthew Ahrens 		dsl_sync_task_t *dst;
585ce636f8bSMatthew Ahrens 		/*
586ce636f8bSMatthew Ahrens 		 * No more sync tasks should have been added while we
587ce636f8bSMatthew Ahrens 		 * were syncing.
588ce636f8bSMatthew Ahrens 		 */
58969962b56SMatthew Ahrens 		ASSERT3U(spa_sync_pass(dp->dp_spa), ==, 1);
59069962b56SMatthew Ahrens 		while ((dst = txg_list_remove(&dp->dp_sync_tasks, txg)) != NULL)
5913b2aab18SMatthew Ahrens 			dsl_sync_task_sync(dst, tx);
592ce636f8bSMatthew Ahrens 	}
593ce636f8bSMatthew Ahrens 
594fa9e4066Sahrens 	dmu_tx_commit(tx);
59505715f94SMark Maybee 
59669962b56SMatthew Ahrens 	DTRACE_PROBE2(dsl_pool_sync__done, dsl_pool_t *dp, dp, uint64_t, txg);
597fa9e4066Sahrens }
598fa9e4066Sahrens 
599fa9e4066Sahrens void
600b24ab676SJeff Bonwick dsl_pool_sync_done(dsl_pool_t *dp, uint64_t txg)
601fa9e4066Sahrens {
602ce636f8bSMatthew Ahrens 	zilog_t *zilog;
603fa9e4066Sahrens 
60443297f97SGeorge Wilson 	while (zilog = txg_list_head(&dp->dp_dirty_zilogs, txg)) {
60569962b56SMatthew Ahrens 		dsl_dataset_t *ds = dmu_objset_ds(zilog->zl_os);
60643297f97SGeorge Wilson 		/*
60743297f97SGeorge Wilson 		 * We don't remove the zilog from the dp_dirty_zilogs
60843297f97SGeorge Wilson 		 * list until after we've cleaned it. This ensures that
60943297f97SGeorge Wilson 		 * callers of zilog_is_dirty() receive an accurate
61043297f97SGeorge Wilson 		 * answer when they are racing with the spa sync thread.
61143297f97SGeorge Wilson 		 */
612ce636f8bSMatthew Ahrens 		zil_clean(zilog, txg);
61343297f97SGeorge Wilson 		(void) txg_list_remove_this(&dp->dp_dirty_zilogs, zilog, txg);
614ce636f8bSMatthew Ahrens 		ASSERT(!dmu_objset_is_dirty(zilog->zl_os, txg));
615ce636f8bSMatthew Ahrens 		dmu_buf_rele(ds->ds_dbuf, zilog);
616fa9e4066Sahrens 	}
617b24ab676SJeff Bonwick 	ASSERT(!dmu_objset_is_dirty(dp->dp_meta_objset, txg));
618fa9e4066Sahrens }
619fa9e4066Sahrens 
620c717a561Smaybee /*
621c717a561Smaybee  * TRUE if the current thread is the tx_sync_thread or if we
622c717a561Smaybee  * are being called from SPA context during pool initialization.
623c717a561Smaybee  */
624fa9e4066Sahrens int
625fa9e4066Sahrens dsl_pool_sync_context(dsl_pool_t *dp)
626fa9e4066Sahrens {
627fa9e4066Sahrens 	return (curthread == dp->dp_tx.tx_sync_thread ||
62894c2d0ebSMatthew Ahrens 	    spa_is_initializing(dp->dp_spa) ||
62994c2d0ebSMatthew Ahrens 	    taskq_member(dp->dp_sync_taskq, curthread));
630fa9e4066Sahrens }
631fa9e4066Sahrens 
632fa9e4066Sahrens uint64_t
633fa9e4066Sahrens dsl_pool_adjustedsize(dsl_pool_t *dp, boolean_t netfree)
634fa9e4066Sahrens {
635fa9e4066Sahrens 	uint64_t space, resv;
636fa9e4066Sahrens 
637fa9e4066Sahrens 	/*
638fa9e4066Sahrens 	 * If we're trying to assess whether it's OK to do a free,
639fa9e4066Sahrens 	 * cut the reservation in half to allow forward progress
640fa9e4066Sahrens 	 * (e.g. make it possible to rm(1) files from a full pool).
641fa9e4066Sahrens 	 */
642485bbbf5SGeorge Wilson 	space = spa_get_dspace(dp->dp_spa);
643c39f2c8cSChristopher Siden 	resv = spa_get_slop_space(dp->dp_spa);
644fa9e4066Sahrens 	if (netfree)
645fa9e4066Sahrens 		resv >>= 1;
646fa9e4066Sahrens 
647fa9e4066Sahrens 	return (space - resv);
648fa9e4066Sahrens }
6491ab7f2deSmaybee 
65069962b56SMatthew Ahrens boolean_t
65169962b56SMatthew Ahrens dsl_pool_need_dirty_delay(dsl_pool_t *dp)
6521ab7f2deSmaybee {
65369962b56SMatthew Ahrens 	uint64_t delay_min_bytes =
65469962b56SMatthew Ahrens 	    zfs_dirty_data_max * zfs_delay_min_dirty_percent / 100;
65569962b56SMatthew Ahrens 	boolean_t rv;
6561ab7f2deSmaybee 
65769962b56SMatthew Ahrens 	mutex_enter(&dp->dp_lock);
65869962b56SMatthew Ahrens 	if (dp->dp_dirty_total > zfs_dirty_data_sync)
65969962b56SMatthew Ahrens 		txg_kick(dp);
66069962b56SMatthew Ahrens 	rv = (dp->dp_dirty_total > delay_min_bytes);
66169962b56SMatthew Ahrens 	mutex_exit(&dp->dp_lock);
66269962b56SMatthew Ahrens 	return (rv);
6631ab7f2deSmaybee }
6641ab7f2deSmaybee 
6651ab7f2deSmaybee void
66669962b56SMatthew Ahrens dsl_pool_dirty_space(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx)
6671ab7f2deSmaybee {
66869962b56SMatthew Ahrens 	if (space > 0) {
66969962b56SMatthew Ahrens 		mutex_enter(&dp->dp_lock);
67069962b56SMatthew Ahrens 		dp->dp_dirty_pertxg[tx->tx_txg & TXG_MASK] += space;
67169962b56SMatthew Ahrens 		dsl_pool_dirty_delta(dp, space);
67269962b56SMatthew Ahrens 		mutex_exit(&dp->dp_lock);
6731ab7f2deSmaybee 	}
6741ab7f2deSmaybee }
6751ab7f2deSmaybee 
6761ab7f2deSmaybee void
67769962b56SMatthew Ahrens dsl_pool_undirty_space(dsl_pool_t *dp, int64_t space, uint64_t txg)
6781ab7f2deSmaybee {
67969962b56SMatthew Ahrens 	ASSERT3S(space, >=, 0);
68069962b56SMatthew Ahrens 	if (space == 0)
68169962b56SMatthew Ahrens 		return;
68269962b56SMatthew Ahrens 	mutex_enter(&dp->dp_lock);
68369962b56SMatthew Ahrens 	if (dp->dp_dirty_pertxg[txg & TXG_MASK] < space) {
68469962b56SMatthew Ahrens 		/* XXX writing something we didn't dirty? */
68569962b56SMatthew Ahrens 		space = dp->dp_dirty_pertxg[txg & TXG_MASK];
6861ab7f2deSmaybee 	}
68769962b56SMatthew Ahrens 	ASSERT3U(dp->dp_dirty_pertxg[txg & TXG_MASK], >=, space);
68869962b56SMatthew Ahrens 	dp->dp_dirty_pertxg[txg & TXG_MASK] -= space;
68969962b56SMatthew Ahrens 	ASSERT3U(dp->dp_dirty_total, >=, space);
69069962b56SMatthew Ahrens 	dsl_pool_dirty_delta(dp, -space);
69169962b56SMatthew Ahrens 	mutex_exit(&dp->dp_lock);
6921ab7f2deSmaybee }
693088f3894Sahrens 
694088f3894Sahrens /* ARGSUSED */
695088f3894Sahrens static int
6963b2aab18SMatthew Ahrens upgrade_clones_cb(dsl_pool_t *dp, dsl_dataset_t *hds, void *arg)
697088f3894Sahrens {
698088f3894Sahrens 	dmu_tx_t *tx = arg;
699088f3894Sahrens 	dsl_dataset_t *ds, *prev = NULL;
700088f3894Sahrens 	int err;
701088f3894Sahrens 
7023b2aab18SMatthew Ahrens 	err = dsl_dataset_hold_obj(dp, hds->ds_object, FTAG, &ds);
703088f3894Sahrens 	if (err)
704088f3894Sahrens 		return (err);
705088f3894Sahrens 
706c1379625SJustin T. Gibbs 	while (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
707c1379625SJustin T. Gibbs 		err = dsl_dataset_hold_obj(dp,
708c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &prev);
709088f3894Sahrens 		if (err) {
710088f3894Sahrens 			dsl_dataset_rele(ds, FTAG);
711088f3894Sahrens 			return (err);
712088f3894Sahrens 		}
713088f3894Sahrens 
714c1379625SJustin T. Gibbs 		if (dsl_dataset_phys(prev)->ds_next_snap_obj != ds->ds_object)
715088f3894Sahrens 			break;
716088f3894Sahrens 		dsl_dataset_rele(ds, FTAG);
717088f3894Sahrens 		ds = prev;
718088f3894Sahrens 		prev = NULL;
719088f3894Sahrens 	}
720088f3894Sahrens 
721088f3894Sahrens 	if (prev == NULL) {
722088f3894Sahrens 		prev = dp->dp_origin_snap;
723088f3894Sahrens 
724088f3894Sahrens 		/*
725088f3894Sahrens 		 * The $ORIGIN can't have any data, or the accounting
726088f3894Sahrens 		 * will be wrong.
727088f3894Sahrens 		 */
728c166b69dSPaul Dagnelie 		rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
729c1379625SJustin T. Gibbs 		ASSERT0(dsl_dataset_phys(prev)->ds_bp.blk_birth);
730c166b69dSPaul Dagnelie 		rrw_exit(&ds->ds_bp_rwlock, FTAG);
731088f3894Sahrens 
732088f3894Sahrens 		/* The origin doesn't get attached to itself */
733088f3894Sahrens 		if (ds->ds_object == prev->ds_object) {
734088f3894Sahrens 			dsl_dataset_rele(ds, FTAG);
735088f3894Sahrens 			return (0);
736088f3894Sahrens 		}
737088f3894Sahrens 
738088f3894Sahrens 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
739c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds)->ds_prev_snap_obj = prev->ds_object;
740c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds)->ds_prev_snap_txg =
741c1379625SJustin T. Gibbs 		    dsl_dataset_phys(prev)->ds_creation_txg;
742088f3894Sahrens 
743088f3894Sahrens 		dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
744c1379625SJustin T. Gibbs 		dsl_dir_phys(ds->ds_dir)->dd_origin_obj = prev->ds_object;
745088f3894Sahrens 
746088f3894Sahrens 		dmu_buf_will_dirty(prev->ds_dbuf, tx);
747c1379625SJustin T. Gibbs 		dsl_dataset_phys(prev)->ds_num_children++;
748088f3894Sahrens 
749c1379625SJustin T. Gibbs 		if (dsl_dataset_phys(ds)->ds_next_snap_obj == 0) {
750088f3894Sahrens 			ASSERT(ds->ds_prev == NULL);
7513b2aab18SMatthew Ahrens 			VERIFY0(dsl_dataset_hold_obj(dp,
752c1379625SJustin T. Gibbs 			    dsl_dataset_phys(ds)->ds_prev_snap_obj,
753c1379625SJustin T. Gibbs 			    ds, &ds->ds_prev));
754088f3894Sahrens 		}
755088f3894Sahrens 	}
756088f3894Sahrens 
757c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dir_phys(ds->ds_dir)->dd_origin_obj, ==, prev->ds_object);
758c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_obj, ==, prev->ds_object);
759088f3894Sahrens 
760c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(prev)->ds_next_clones_obj == 0) {
761c33e334fSMatthew Ahrens 		dmu_buf_will_dirty(prev->ds_dbuf, tx);
762c1379625SJustin T. Gibbs 		dsl_dataset_phys(prev)->ds_next_clones_obj =
763088f3894Sahrens 		    zap_create(dp->dp_meta_objset,
764088f3894Sahrens 		    DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
765088f3894Sahrens 	}
7663b2aab18SMatthew Ahrens 	VERIFY0(zap_add_int(dp->dp_meta_objset,
767c1379625SJustin T. Gibbs 	    dsl_dataset_phys(prev)->ds_next_clones_obj, ds->ds_object, tx));
768088f3894Sahrens 
769088f3894Sahrens 	dsl_dataset_rele(ds, FTAG);
770088f3894Sahrens 	if (prev != dp->dp_origin_snap)
771088f3894Sahrens 		dsl_dataset_rele(prev, FTAG);
772088f3894Sahrens 	return (0);
773088f3894Sahrens }
774088f3894Sahrens 
775088f3894Sahrens void
776088f3894Sahrens dsl_pool_upgrade_clones(dsl_pool_t *dp, dmu_tx_t *tx)
777088f3894Sahrens {
778088f3894Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
779088f3894Sahrens 	ASSERT(dp->dp_origin_snap != NULL);
780088f3894Sahrens 
7813b2aab18SMatthew Ahrens 	VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj, upgrade_clones_cb,
78212380e1eSArne Jansen 	    tx, DS_FIND_CHILDREN | DS_FIND_SERIALIZE));
783088f3894Sahrens }
784088f3894Sahrens 
785cde58dbcSMatthew Ahrens /* ARGSUSED */
786cde58dbcSMatthew Ahrens static int
7873b2aab18SMatthew Ahrens upgrade_dir_clones_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
788cde58dbcSMatthew Ahrens {
789cde58dbcSMatthew Ahrens 	dmu_tx_t *tx = arg;
790cde58dbcSMatthew Ahrens 	objset_t *mos = dp->dp_meta_objset;
791cde58dbcSMatthew Ahrens 
792c1379625SJustin T. Gibbs 	if (dsl_dir_phys(ds->ds_dir)->dd_origin_obj != 0) {
793cde58dbcSMatthew Ahrens 		dsl_dataset_t *origin;
794cde58dbcSMatthew Ahrens 
7953b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold_obj(dp,
796c1379625SJustin T. Gibbs 		    dsl_dir_phys(ds->ds_dir)->dd_origin_obj, FTAG, &origin));
797cde58dbcSMatthew Ahrens 
798c1379625SJustin T. Gibbs 		if (dsl_dir_phys(origin->ds_dir)->dd_clones == 0) {
799cde58dbcSMatthew Ahrens 			dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx);
800c1379625SJustin T. Gibbs 			dsl_dir_phys(origin->ds_dir)->dd_clones =
801c1379625SJustin T. Gibbs 			    zap_create(mos, DMU_OT_DSL_CLONES, DMU_OT_NONE,
802c1379625SJustin T. Gibbs 			    0, tx);
803cde58dbcSMatthew Ahrens 		}
804cde58dbcSMatthew Ahrens 
8053b2aab18SMatthew Ahrens 		VERIFY0(zap_add_int(dp->dp_meta_objset,
806c1379625SJustin T. Gibbs 		    dsl_dir_phys(origin->ds_dir)->dd_clones,
807c1379625SJustin T. Gibbs 		    ds->ds_object, tx));
808cde58dbcSMatthew Ahrens 
809cde58dbcSMatthew Ahrens 		dsl_dataset_rele(origin, FTAG);
810cde58dbcSMatthew Ahrens 	}
811cde58dbcSMatthew Ahrens 	return (0);
812cde58dbcSMatthew Ahrens }
813cde58dbcSMatthew Ahrens 
814cde58dbcSMatthew Ahrens void
815cde58dbcSMatthew Ahrens dsl_pool_upgrade_dir_clones(dsl_pool_t *dp, dmu_tx_t *tx)
816cde58dbcSMatthew Ahrens {
817cde58dbcSMatthew Ahrens 	ASSERT(dmu_tx_is_syncing(tx));
818cde58dbcSMatthew Ahrens 	uint64_t obj;
819cde58dbcSMatthew Ahrens 
820cde58dbcSMatthew Ahrens 	(void) dsl_dir_create_sync(dp, dp->dp_root_dir, FREE_DIR_NAME, tx);
8213b2aab18SMatthew Ahrens 	VERIFY0(dsl_pool_open_special_dir(dp,
822cde58dbcSMatthew Ahrens 	    FREE_DIR_NAME, &dp->dp_free_dir));
823cde58dbcSMatthew Ahrens 
824cde58dbcSMatthew Ahrens 	/*
825cde58dbcSMatthew Ahrens 	 * We can't use bpobj_alloc(), because spa_version() still
826cde58dbcSMatthew Ahrens 	 * returns the old version, and we need a new-version bpobj with
827cde58dbcSMatthew Ahrens 	 * subobj support.  So call dmu_object_alloc() directly.
828cde58dbcSMatthew Ahrens 	 */
829cde58dbcSMatthew Ahrens 	obj = dmu_object_alloc(dp->dp_meta_objset, DMU_OT_BPOBJ,
830b5152584SMatthew Ahrens 	    SPA_OLD_MAXBLOCKSIZE, DMU_OT_BPOBJ_HDR, sizeof (bpobj_phys_t), tx);
8313b2aab18SMatthew Ahrens 	VERIFY0(zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
832cde58dbcSMatthew Ahrens 	    DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj, tx));
8333b2aab18SMatthew Ahrens 	VERIFY0(bpobj_open(&dp->dp_free_bpobj, dp->dp_meta_objset, obj));
834cde58dbcSMatthew Ahrens 
8353b2aab18SMatthew Ahrens 	VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
83612380e1eSArne Jansen 	    upgrade_dir_clones_cb, tx, DS_FIND_CHILDREN | DS_FIND_SERIALIZE));
837cde58dbcSMatthew Ahrens }
838cde58dbcSMatthew Ahrens 
839088f3894Sahrens void
840088f3894Sahrens dsl_pool_create_origin(dsl_pool_t *dp, dmu_tx_t *tx)
841088f3894Sahrens {
842088f3894Sahrens 	uint64_t dsobj;
843088f3894Sahrens 	dsl_dataset_t *ds;
844088f3894Sahrens 
845088f3894Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
846088f3894Sahrens 	ASSERT(dp->dp_origin_snap == NULL);
8473b2aab18SMatthew Ahrens 	ASSERT(rrw_held(&dp->dp_config_rwlock, RW_WRITER));
848088f3894Sahrens 
849088f3894Sahrens 	/* create the origin dir, ds, & snap-ds */
850088f3894Sahrens 	dsobj = dsl_dataset_create_sync(dp->dp_root_dir, ORIGIN_DIR_NAME,
851088f3894Sahrens 	    NULL, 0, kcred, tx);
8523b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
8533b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_sync_impl(ds, ORIGIN_DIR_NAME, tx);
854c1379625SJustin T. Gibbs 	VERIFY0(dsl_dataset_hold_obj(dp, dsl_dataset_phys(ds)->ds_prev_snap_obj,
855088f3894Sahrens 	    dp, &dp->dp_origin_snap));
856088f3894Sahrens 	dsl_dataset_rele(ds, FTAG);
857088f3894Sahrens }
8589d3574bfSNeil Perrin 
8599d3574bfSNeil Perrin taskq_t *
8609d3574bfSNeil Perrin dsl_pool_vnrele_taskq(dsl_pool_t *dp)
8619d3574bfSNeil Perrin {
8629d3574bfSNeil Perrin 	return (dp->dp_vnrele_taskq);
8639d3574bfSNeil Perrin }
864ca45db41SChris Kirby 
865ca45db41SChris Kirby /*
866ca45db41SChris Kirby  * Walk through the pool-wide zap object of temporary snapshot user holds
867ca45db41SChris Kirby  * and release them.
868ca45db41SChris Kirby  */
869ca45db41SChris Kirby void
870ca45db41SChris Kirby dsl_pool_clean_tmp_userrefs(dsl_pool_t *dp)
871ca45db41SChris Kirby {
872ca45db41SChris Kirby 	zap_attribute_t za;
873ca45db41SChris Kirby 	zap_cursor_t zc;
874ca45db41SChris Kirby 	objset_t *mos = dp->dp_meta_objset;
875ca45db41SChris Kirby 	uint64_t zapobj = dp->dp_tmp_userrefs_obj;
876a7a845e4SSteven Hartland 	nvlist_t *holds;
877ca45db41SChris Kirby 
878ca45db41SChris Kirby 	if (zapobj == 0)
879ca45db41SChris Kirby 		return;
880ca45db41SChris Kirby 	ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
881ca45db41SChris Kirby 
882a7a845e4SSteven Hartland 	holds = fnvlist_alloc();
883a7a845e4SSteven Hartland 
884ca45db41SChris Kirby 	for (zap_cursor_init(&zc, mos, zapobj);
885ca45db41SChris Kirby 	    zap_cursor_retrieve(&zc, &za) == 0;
886ca45db41SChris Kirby 	    zap_cursor_advance(&zc)) {
887ca45db41SChris Kirby 		char *htag;
888a7a845e4SSteven Hartland 		nvlist_t *tags;
889ca45db41SChris Kirby 
890ca45db41SChris Kirby 		htag = strchr(za.za_name, '-');
891ca45db41SChris Kirby 		*htag = '\0';
892ca45db41SChris Kirby 		++htag;
893a7a845e4SSteven Hartland 		if (nvlist_lookup_nvlist(holds, za.za_name, &tags) != 0) {
894a7a845e4SSteven Hartland 			tags = fnvlist_alloc();
895a7a845e4SSteven Hartland 			fnvlist_add_boolean(tags, htag);
896a7a845e4SSteven Hartland 			fnvlist_add_nvlist(holds, za.za_name, tags);
897a7a845e4SSteven Hartland 			fnvlist_free(tags);
898a7a845e4SSteven Hartland 		} else {
899a7a845e4SSteven Hartland 			fnvlist_add_boolean(tags, htag);
900a7a845e4SSteven Hartland 		}
901ca45db41SChris Kirby 	}
902a7a845e4SSteven Hartland 	dsl_dataset_user_release_tmp(dp, holds);
903a7a845e4SSteven Hartland 	fnvlist_free(holds);
904ca45db41SChris Kirby 	zap_cursor_fini(&zc);
905ca45db41SChris Kirby }
906ca45db41SChris Kirby 
907ca45db41SChris Kirby /*
908ca45db41SChris Kirby  * Create the pool-wide zap object for storing temporary snapshot holds.
909ca45db41SChris Kirby  */
910ca45db41SChris Kirby void
911ca45db41SChris Kirby dsl_pool_user_hold_create_obj(dsl_pool_t *dp, dmu_tx_t *tx)
912ca45db41SChris Kirby {
913ca45db41SChris Kirby 	objset_t *mos = dp->dp_meta_objset;
914ca45db41SChris Kirby 
915ca45db41SChris Kirby 	ASSERT(dp->dp_tmp_userrefs_obj == 0);
916ca45db41SChris Kirby 	ASSERT(dmu_tx_is_syncing(tx));
917ca45db41SChris Kirby 
918ad135b5dSChristopher Siden 	dp->dp_tmp_userrefs_obj = zap_create_link(mos, DMU_OT_USERREFS,
919ad135b5dSChristopher Siden 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_TMP_USERREFS, tx);
920ca45db41SChris Kirby }
921ca45db41SChris Kirby 
922ca45db41SChris Kirby static int
923ca45db41SChris Kirby dsl_pool_user_hold_rele_impl(dsl_pool_t *dp, uint64_t dsobj,
9243b2aab18SMatthew Ahrens     const char *tag, uint64_t now, dmu_tx_t *tx, boolean_t holding)
925ca45db41SChris Kirby {
926ca45db41SChris Kirby 	objset_t *mos = dp->dp_meta_objset;
927ca45db41SChris Kirby 	uint64_t zapobj = dp->dp_tmp_userrefs_obj;
928ca45db41SChris Kirby 	char *name;
929ca45db41SChris Kirby 	int error;
930ca45db41SChris Kirby 
931ca45db41SChris Kirby 	ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
932ca45db41SChris Kirby 	ASSERT(dmu_tx_is_syncing(tx));
933ca45db41SChris Kirby 
934ca45db41SChris Kirby 	/*
935ca45db41SChris Kirby 	 * If the pool was created prior to SPA_VERSION_USERREFS, the
936ca45db41SChris Kirby 	 * zap object for temporary holds might not exist yet.
937ca45db41SChris Kirby 	 */
938ca45db41SChris Kirby 	if (zapobj == 0) {
939ca45db41SChris Kirby 		if (holding) {
940ca45db41SChris Kirby 			dsl_pool_user_hold_create_obj(dp, tx);
941ca45db41SChris Kirby 			zapobj = dp->dp_tmp_userrefs_obj;
942ca45db41SChris Kirby 		} else {
943be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOENT));
944ca45db41SChris Kirby 		}
945ca45db41SChris Kirby 	}
946ca45db41SChris Kirby 
947ca45db41SChris Kirby 	name = kmem_asprintf("%llx-%s", (u_longlong_t)dsobj, tag);
948ca45db41SChris Kirby 	if (holding)
9493b2aab18SMatthew Ahrens 		error = zap_add(mos, zapobj, name, 8, 1, &now, tx);
950ca45db41SChris Kirby 	else
951ca45db41SChris Kirby 		error = zap_remove(mos, zapobj, name, tx);
952ca45db41SChris Kirby 	strfree(name);
953ca45db41SChris Kirby 
954ca45db41SChris Kirby 	return (error);
955ca45db41SChris Kirby }
956ca45db41SChris Kirby 
957ca45db41SChris Kirby /*
958ca45db41SChris Kirby  * Add a temporary hold for the given dataset object and tag.
959ca45db41SChris Kirby  */
960ca45db41SChris Kirby int
961ca45db41SChris Kirby dsl_pool_user_hold(dsl_pool_t *dp, uint64_t dsobj, const char *tag,
9623b2aab18SMatthew Ahrens     uint64_t now, dmu_tx_t *tx)
963ca45db41SChris Kirby {
96415508ac0SChris Kirby 	return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, now, tx, B_TRUE));
965ca45db41SChris Kirby }
966ca45db41SChris Kirby 
967ca45db41SChris Kirby /*
968ca45db41SChris Kirby  * Release a temporary hold for the given dataset object and tag.
969ca45db41SChris Kirby  */
970ca45db41SChris Kirby int
971ca45db41SChris Kirby dsl_pool_user_release(dsl_pool_t *dp, uint64_t dsobj, const char *tag,
972ca45db41SChris Kirby     dmu_tx_t *tx)
973ca45db41SChris Kirby {
974ca45db41SChris Kirby 	return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, NULL,
975ca45db41SChris Kirby 	    tx, B_FALSE));
976ca45db41SChris Kirby }
9773b2aab18SMatthew Ahrens 
9783b2aab18SMatthew Ahrens /*
9793b2aab18SMatthew Ahrens  * DSL Pool Configuration Lock
9803b2aab18SMatthew Ahrens  *
9813b2aab18SMatthew Ahrens  * The dp_config_rwlock protects against changes to DSL state (e.g. dataset
9823b2aab18SMatthew Ahrens  * creation / destruction / rename / property setting).  It must be held for
9833b2aab18SMatthew Ahrens  * read to hold a dataset or dsl_dir.  I.e. you must call
9843b2aab18SMatthew Ahrens  * dsl_pool_config_enter() or dsl_pool_hold() before calling
9853b2aab18SMatthew Ahrens  * dsl_{dataset,dir}_hold{_obj}.  In most circumstances, the dp_config_rwlock
9863b2aab18SMatthew Ahrens  * must be held continuously until all datasets and dsl_dirs are released.
9873b2aab18SMatthew Ahrens  *
9883b2aab18SMatthew Ahrens  * The only exception to this rule is that if a "long hold" is placed on
9893b2aab18SMatthew Ahrens  * a dataset, then the dp_config_rwlock may be dropped while the dataset
9903b2aab18SMatthew Ahrens  * is still held.  The long hold will prevent the dataset from being
9913b2aab18SMatthew Ahrens  * destroyed -- the destroy will fail with EBUSY.  A long hold can be
9923b2aab18SMatthew Ahrens  * obtained by calling dsl_dataset_long_hold(), or by "owning" a dataset
9933b2aab18SMatthew Ahrens  * (by calling dsl_{dataset,objset}_{try}own{_obj}).
9943b2aab18SMatthew Ahrens  *
9953b2aab18SMatthew Ahrens  * Legitimate long-holders (including owners) should be long-running, cancelable
9963b2aab18SMatthew Ahrens  * tasks that should cause "zfs destroy" to fail.  This includes DMU
9973b2aab18SMatthew Ahrens  * consumers (i.e. a ZPL filesystem being mounted or ZVOL being open),
9983b2aab18SMatthew Ahrens  * "zfs send", and "zfs diff".  There are several other long-holders whose
9993b2aab18SMatthew Ahrens  * uses are suboptimal (e.g. "zfs promote", and zil_suspend()).
10003b2aab18SMatthew Ahrens  *
10013b2aab18SMatthew Ahrens  * The usual formula for long-holding would be:
10023b2aab18SMatthew Ahrens  * dsl_pool_hold()
10033b2aab18SMatthew Ahrens  * dsl_dataset_hold()
10043b2aab18SMatthew Ahrens  * ... perform checks ...
10053b2aab18SMatthew Ahrens  * dsl_dataset_long_hold()
10063b2aab18SMatthew Ahrens  * dsl_pool_rele()
10073b2aab18SMatthew Ahrens  * ... perform long-running task ...
10083b2aab18SMatthew Ahrens  * dsl_dataset_long_rele()
10093b2aab18SMatthew Ahrens  * dsl_dataset_rele()
10103b2aab18SMatthew Ahrens  *
10113b2aab18SMatthew Ahrens  * Note that when the long hold is released, the dataset is still held but
10123b2aab18SMatthew Ahrens  * the pool is not held.  The dataset may change arbitrarily during this time
10133b2aab18SMatthew Ahrens  * (e.g. it could be destroyed).  Therefore you shouldn't do anything to the
10143b2aab18SMatthew Ahrens  * dataset except release it.
10153b2aab18SMatthew Ahrens  *
10163b2aab18SMatthew Ahrens  * User-initiated operations (e.g. ioctls, zfs_ioc_*()) are either read-only
10173b2aab18SMatthew Ahrens  * or modifying operations.
10183b2aab18SMatthew Ahrens  *
10193b2aab18SMatthew Ahrens  * Modifying operations should generally use dsl_sync_task().  The synctask
10203b2aab18SMatthew Ahrens  * infrastructure enforces proper locking strategy with respect to the
10213b2aab18SMatthew Ahrens  * dp_config_rwlock.  See the comment above dsl_sync_task() for details.
10223b2aab18SMatthew Ahrens  *
10233b2aab18SMatthew Ahrens  * Read-only operations will manually hold the pool, then the dataset, obtain
10243b2aab18SMatthew Ahrens  * information from the dataset, then release the pool and dataset.
10253b2aab18SMatthew Ahrens  * dmu_objset_{hold,rele}() are convenience routines that also do the pool
10263b2aab18SMatthew Ahrens  * hold/rele.
10273b2aab18SMatthew Ahrens  */
10283b2aab18SMatthew Ahrens 
10293b2aab18SMatthew Ahrens int
10303b2aab18SMatthew Ahrens dsl_pool_hold(const char *name, void *tag, dsl_pool_t **dp)
10313b2aab18SMatthew Ahrens {
10323b2aab18SMatthew Ahrens 	spa_t *spa;
10333b2aab18SMatthew Ahrens 	int error;
10343b2aab18SMatthew Ahrens 
10353b2aab18SMatthew Ahrens 	error = spa_open(name, &spa, tag);
10363b2aab18SMatthew Ahrens 	if (error == 0) {
10373b2aab18SMatthew Ahrens 		*dp = spa_get_dsl(spa);
10383b2aab18SMatthew Ahrens 		dsl_pool_config_enter(*dp, tag);
10393b2aab18SMatthew Ahrens 	}
10403b2aab18SMatthew Ahrens 	return (error);
10413b2aab18SMatthew Ahrens }
10423b2aab18SMatthew Ahrens 
10433b2aab18SMatthew Ahrens void
10443b2aab18SMatthew Ahrens dsl_pool_rele(dsl_pool_t *dp, void *tag)
10453b2aab18SMatthew Ahrens {
10463b2aab18SMatthew Ahrens 	dsl_pool_config_exit(dp, tag);
10473b2aab18SMatthew Ahrens 	spa_close(dp->dp_spa, tag);
10483b2aab18SMatthew Ahrens }
10493b2aab18SMatthew Ahrens 
10503b2aab18SMatthew Ahrens void
10513b2aab18SMatthew Ahrens dsl_pool_config_enter(dsl_pool_t *dp, void *tag)
10523b2aab18SMatthew Ahrens {
10533b2aab18SMatthew Ahrens 	/*
10543b2aab18SMatthew Ahrens 	 * We use a "reentrant" reader-writer lock, but not reentrantly.
10553b2aab18SMatthew Ahrens 	 *
10563b2aab18SMatthew Ahrens 	 * The rrwlock can (with the track_all flag) track all reading threads,
10573b2aab18SMatthew Ahrens 	 * which is very useful for debugging which code path failed to release
10583b2aab18SMatthew Ahrens 	 * the lock, and for verifying that the *current* thread does hold
10593b2aab18SMatthew Ahrens 	 * the lock.
10603b2aab18SMatthew Ahrens 	 *
10613b2aab18SMatthew Ahrens 	 * (Unlike a rwlock, which knows that N threads hold it for
10623b2aab18SMatthew Ahrens 	 * read, but not *which* threads, so rw_held(RW_READER) returns TRUE
10633b2aab18SMatthew Ahrens 	 * if any thread holds it for read, even if this thread doesn't).
10643b2aab18SMatthew Ahrens 	 */
10653b2aab18SMatthew Ahrens 	ASSERT(!rrw_held(&dp->dp_config_rwlock, RW_READER));
10663b2aab18SMatthew Ahrens 	rrw_enter(&dp->dp_config_rwlock, RW_READER, tag);
10673b2aab18SMatthew Ahrens }
10683b2aab18SMatthew Ahrens 
10691d3f896fSArne Jansen void
10701d3f896fSArne Jansen dsl_pool_config_enter_prio(dsl_pool_t *dp, void *tag)
10711d3f896fSArne Jansen {
10721d3f896fSArne Jansen 	ASSERT(!rrw_held(&dp->dp_config_rwlock, RW_READER));
10731d3f896fSArne Jansen 	rrw_enter_read_prio(&dp->dp_config_rwlock, tag);
10741d3f896fSArne Jansen }
10751d3f896fSArne Jansen 
10763b2aab18SMatthew Ahrens void
10773b2aab18SMatthew Ahrens dsl_pool_config_exit(dsl_pool_t *dp, void *tag)
10783b2aab18SMatthew Ahrens {
10793b2aab18SMatthew Ahrens 	rrw_exit(&dp->dp_config_rwlock, tag);
10803b2aab18SMatthew Ahrens }
10813b2aab18SMatthew Ahrens 
10823b2aab18SMatthew Ahrens boolean_t
10833b2aab18SMatthew Ahrens dsl_pool_config_held(dsl_pool_t *dp)
10843b2aab18SMatthew Ahrens {
10853b2aab18SMatthew Ahrens 	return (RRW_LOCK_HELD(&dp->dp_config_rwlock));
10863b2aab18SMatthew Ahrens }
108712380e1eSArne Jansen 
108812380e1eSArne Jansen boolean_t
108912380e1eSArne Jansen dsl_pool_config_held_writer(dsl_pool_t *dp)
109012380e1eSArne Jansen {
109112380e1eSArne Jansen 	return (RRW_WRITE_HELD(&dp->dp_config_rwlock));
109212380e1eSArne Jansen }
1093