1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy  *
8eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
10eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11eda14cbcSMatt Macy  * and limitations under the License.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy  *
19eda14cbcSMatt Macy  * CDDL HEADER END
20eda14cbcSMatt Macy  */
21eda14cbcSMatt Macy /*
22eda14cbcSMatt Macy  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
237877fdebSMatt Macy  * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
24eda14cbcSMatt Macy  * Copyright (c) 2013 Steven Hartland. All rights reserved.
25eda14cbcSMatt Macy  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
26eda14cbcSMatt Macy  * Copyright 2016 Nexenta Systems, Inc.  All rights reserved.
27eda14cbcSMatt Macy  */
28eda14cbcSMatt Macy 
29eda14cbcSMatt Macy #include <sys/dsl_pool.h>
30eda14cbcSMatt Macy #include <sys/dsl_dataset.h>
31eda14cbcSMatt Macy #include <sys/dsl_prop.h>
32eda14cbcSMatt Macy #include <sys/dsl_dir.h>
33eda14cbcSMatt Macy #include <sys/dsl_synctask.h>
34eda14cbcSMatt Macy #include <sys/dsl_scan.h>
35eda14cbcSMatt Macy #include <sys/dnode.h>
36eda14cbcSMatt Macy #include <sys/dmu_tx.h>
37eda14cbcSMatt Macy #include <sys/dmu_objset.h>
38eda14cbcSMatt Macy #include <sys/arc.h>
39eda14cbcSMatt Macy #include <sys/zap.h>
40eda14cbcSMatt Macy #include <sys/zio.h>
41eda14cbcSMatt Macy #include <sys/zfs_context.h>
42eda14cbcSMatt Macy #include <sys/fs/zfs.h>
43eda14cbcSMatt Macy #include <sys/zfs_znode.h>
44eda14cbcSMatt Macy #include <sys/spa_impl.h>
45eda14cbcSMatt Macy #include <sys/vdev_impl.h>
46eda14cbcSMatt Macy #include <sys/metaslab_impl.h>
47eda14cbcSMatt Macy #include <sys/bptree.h>
48eda14cbcSMatt Macy #include <sys/zfeature.h>
49eda14cbcSMatt Macy #include <sys/zil_impl.h>
50eda14cbcSMatt Macy #include <sys/dsl_userhold.h>
51eda14cbcSMatt Macy #include <sys/trace_zfs.h>
52eda14cbcSMatt Macy #include <sys/mmp.h>
53eda14cbcSMatt Macy 
54eda14cbcSMatt Macy /*
55eda14cbcSMatt Macy  * ZFS Write Throttle
56eda14cbcSMatt Macy  * ------------------
57eda14cbcSMatt Macy  *
58eda14cbcSMatt Macy  * ZFS must limit the rate of incoming writes to the rate at which it is able
59eda14cbcSMatt Macy  * to sync data modifications to the backend storage. Throttling by too much
60eda14cbcSMatt Macy  * creates an artificial limit; throttling by too little can only be sustained
61eda14cbcSMatt Macy  * for short periods and would lead to highly lumpy performance. On a per-pool
62eda14cbcSMatt Macy  * basis, ZFS tracks the amount of modified (dirty) data. As operations change
63eda14cbcSMatt Macy  * data, the amount of dirty data increases; as ZFS syncs out data, the amount
64eda14cbcSMatt Macy  * of dirty data decreases. When the amount of dirty data exceeds a
65eda14cbcSMatt Macy  * predetermined threshold further modifications are blocked until the amount
66eda14cbcSMatt Macy  * of dirty data decreases (as data is synced out).
67eda14cbcSMatt Macy  *
68eda14cbcSMatt Macy  * The limit on dirty data is tunable, and should be adjusted according to
69eda14cbcSMatt Macy  * both the IO capacity and available memory of the system. The larger the
70eda14cbcSMatt Macy  * window, the more ZFS is able to aggregate and amortize metadata (and data)
71eda14cbcSMatt Macy  * changes. However, memory is a limited resource, and allowing for more dirty
72eda14cbcSMatt Macy  * data comes at the cost of keeping other useful data in memory (for example
73eda14cbcSMatt Macy  * ZFS data cached by the ARC).
74eda14cbcSMatt Macy  *
75eda14cbcSMatt Macy  * Implementation
76eda14cbcSMatt Macy  *
77eda14cbcSMatt Macy  * As buffers are modified dsl_pool_willuse_space() increments both the per-
78eda14cbcSMatt Macy  * txg (dp_dirty_pertxg[]) and poolwide (dp_dirty_total) accounting of
79eda14cbcSMatt Macy  * dirty space used; dsl_pool_dirty_space() decrements those values as data
80eda14cbcSMatt Macy  * is synced out from dsl_pool_sync(). While only the poolwide value is
81eda14cbcSMatt Macy  * relevant, the per-txg value is useful for debugging. The tunable
82eda14cbcSMatt Macy  * zfs_dirty_data_max determines the dirty space limit. Once that value is
83eda14cbcSMatt Macy  * exceeded, new writes are halted until space frees up.
84eda14cbcSMatt Macy  *
85eda14cbcSMatt Macy  * The zfs_dirty_data_sync_percent tunable dictates the threshold at which we
86eda14cbcSMatt Macy  * ensure that there is a txg syncing (see the comment in txg.c for a full
87eda14cbcSMatt Macy  * description of transaction group stages).
88eda14cbcSMatt Macy  *
89eda14cbcSMatt Macy  * The IO scheduler uses both the dirty space limit and current amount of
90eda14cbcSMatt Macy  * dirty data as inputs. Those values affect the number of concurrent IOs ZFS
91eda14cbcSMatt Macy  * issues. See the comment in vdev_queue.c for details of the IO scheduler.
92eda14cbcSMatt Macy  *
93eda14cbcSMatt Macy  * The delay is also calculated based on the amount of dirty data.  See the
94eda14cbcSMatt Macy  * comment above dmu_tx_delay() for details.
95eda14cbcSMatt Macy  */
96eda14cbcSMatt Macy 
97eda14cbcSMatt Macy /*
98eda14cbcSMatt Macy  * zfs_dirty_data_max will be set to zfs_dirty_data_max_percent% of all memory,
99eda14cbcSMatt Macy  * capped at zfs_dirty_data_max_max.  It can also be overridden with a module
100eda14cbcSMatt Macy  * parameter.
101eda14cbcSMatt Macy  */
102dbd5678dSMartin Matuska uint64_t zfs_dirty_data_max = 0;
103dbd5678dSMartin Matuska uint64_t zfs_dirty_data_max_max = 0;
104be181ee2SMartin Matuska uint_t zfs_dirty_data_max_percent = 10;
105be181ee2SMartin Matuska uint_t zfs_dirty_data_max_max_percent = 25;
106eda14cbcSMatt Macy 
107eda14cbcSMatt Macy /*
108e3aa18adSMartin Matuska  * The upper limit of TX_WRITE log data.  Write operations are throttled
109e3aa18adSMartin Matuska  * when approaching the limit until log data is cleared out after txg sync.
1103f9d360cSMartin Matuska  * It only counts TX_WRITE log with WR_COPIED or WR_NEED_COPY.
1113f9d360cSMartin Matuska  */
112dbd5678dSMartin Matuska uint64_t zfs_wrlog_data_max = 0;
1133f9d360cSMartin Matuska 
1143f9d360cSMartin Matuska /*
115eda14cbcSMatt Macy  * If there's at least this much dirty data (as a percentage of
116eda14cbcSMatt Macy  * zfs_dirty_data_max), push out a txg.  This should be less than
117eda14cbcSMatt Macy  * zfs_vdev_async_write_active_min_dirty_percent.
118eda14cbcSMatt Macy  */
119be181ee2SMartin Matuska static uint_t zfs_dirty_data_sync_percent = 20;
120eda14cbcSMatt Macy 
121eda14cbcSMatt Macy /*
122eda14cbcSMatt Macy  * Once there is this amount of dirty data, the dmu_tx_delay() will kick in
123eda14cbcSMatt Macy  * and delay each transaction.
124eda14cbcSMatt Macy  * This value should be >= zfs_vdev_async_write_active_max_dirty_percent.
125eda14cbcSMatt Macy  */
126be181ee2SMartin Matuska uint_t zfs_delay_min_dirty_percent = 60;
127eda14cbcSMatt Macy 
128eda14cbcSMatt Macy /*
129eda14cbcSMatt Macy  * This controls how quickly the delay approaches infinity.
130eda14cbcSMatt Macy  * Larger values cause it to delay more for a given amount of dirty data.
131eda14cbcSMatt Macy  * Therefore larger values will cause there to be less dirty data for a
132eda14cbcSMatt Macy  * given throughput.
133eda14cbcSMatt Macy  *
134eda14cbcSMatt Macy  * For the smoothest delay, this value should be about 1 billion divided
135eda14cbcSMatt Macy  * by the maximum number of operations per second.  This will smoothly
136eda14cbcSMatt Macy  * handle between 10x and 1/10th this number.
137eda14cbcSMatt Macy  *
138eda14cbcSMatt Macy  * Note: zfs_delay_scale * zfs_dirty_data_max must be < 2^64, due to the
139eda14cbcSMatt Macy  * multiply in dmu_tx_delay().
140eda14cbcSMatt Macy  */
141dbd5678dSMartin Matuska uint64_t zfs_delay_scale = 1000 * 1000 * 1000 / 2000;
142eda14cbcSMatt Macy 
143eda14cbcSMatt Macy /*
144eda14cbcSMatt Macy  * These tunables determine the behavior of how zil_itxg_clean() is
145eda14cbcSMatt Macy  * called via zil_clean() in the context of spa_sync(). When an itxg
146eda14cbcSMatt Macy  * list needs to be cleaned, TQ_NOSLEEP will be used when dispatching.
147eda14cbcSMatt Macy  * If the dispatch fails, the call to zil_itxg_clean() will occur
148eda14cbcSMatt Macy  * synchronously in the context of spa_sync(), which can negatively
149eda14cbcSMatt Macy  * impact the performance of spa_sync() (e.g. in the case of the itxg
150eda14cbcSMatt Macy  * list having a large number of itxs that needs to be cleaned).
151eda14cbcSMatt Macy  *
152eda14cbcSMatt Macy  * Thus, these tunables can be used to manipulate the behavior of the
153eda14cbcSMatt Macy  * taskq used by zil_clean(); they determine the number of taskq entries
154eda14cbcSMatt Macy  * that are pre-populated when the taskq is first created (via the
155eda14cbcSMatt Macy  * "zfs_zil_clean_taskq_minalloc" tunable) and the maximum number of
156eda14cbcSMatt Macy  * taskq entries that are cached after an on-demand allocation (via the
157eda14cbcSMatt Macy  * "zfs_zil_clean_taskq_maxalloc").
158eda14cbcSMatt Macy  *
159eda14cbcSMatt Macy  * The idea being, we want to try reasonably hard to ensure there will
160eda14cbcSMatt Macy  * already be a taskq entry pre-allocated by the time that it is needed
161eda14cbcSMatt Macy  * by zil_clean(). This way, we can avoid the possibility of an
162eda14cbcSMatt Macy  * on-demand allocation of a new taskq entry from failing, which would
163eda14cbcSMatt Macy  * result in zil_itxg_clean() being called synchronously from zil_clean()
164eda14cbcSMatt Macy  * (which can adversely affect performance of spa_sync()).
165eda14cbcSMatt Macy  *
166eda14cbcSMatt Macy  * Additionally, the number of threads used by the taskq can be
167eda14cbcSMatt Macy  * configured via the "zfs_zil_clean_taskq_nthr_pct" tunable.
168eda14cbcSMatt Macy  */
169e92ffd9bSMartin Matuska static int zfs_zil_clean_taskq_nthr_pct = 100;
170e92ffd9bSMartin Matuska static int zfs_zil_clean_taskq_minalloc = 1024;
171e92ffd9bSMartin Matuska static int zfs_zil_clean_taskq_maxalloc = 1024 * 1024;
172eda14cbcSMatt Macy 
173eda14cbcSMatt Macy int
dsl_pool_open_special_dir(dsl_pool_t * dp,const char * name,dsl_dir_t ** ddp)174eda14cbcSMatt Macy dsl_pool_open_special_dir(dsl_pool_t *dp, const char *name, dsl_dir_t **ddp)
175eda14cbcSMatt Macy {
176eda14cbcSMatt Macy 	uint64_t obj;
177eda14cbcSMatt Macy 	int err;
178eda14cbcSMatt Macy 
179eda14cbcSMatt Macy 	err = zap_lookup(dp->dp_meta_objset,
180eda14cbcSMatt Macy 	    dsl_dir_phys(dp->dp_root_dir)->dd_child_dir_zapobj,
181eda14cbcSMatt Macy 	    name, sizeof (obj), 1, &obj);
182eda14cbcSMatt Macy 	if (err)
183eda14cbcSMatt Macy 		return (err);
184eda14cbcSMatt Macy 
185eda14cbcSMatt Macy 	return (dsl_dir_hold_obj(dp, obj, name, dp, ddp));
186eda14cbcSMatt Macy }
187eda14cbcSMatt Macy 
188eda14cbcSMatt Macy static dsl_pool_t *
dsl_pool_open_impl(spa_t * spa,uint64_t txg)189eda14cbcSMatt Macy dsl_pool_open_impl(spa_t *spa, uint64_t txg)
190eda14cbcSMatt Macy {
191eda14cbcSMatt Macy 	dsl_pool_t *dp;
192eda14cbcSMatt Macy 	blkptr_t *bp = spa_get_rootblkptr(spa);
193eda14cbcSMatt Macy 
194eda14cbcSMatt Macy 	dp = kmem_zalloc(sizeof (dsl_pool_t), KM_SLEEP);
195eda14cbcSMatt Macy 	dp->dp_spa = spa;
196eda14cbcSMatt Macy 	dp->dp_meta_rootbp = *bp;
197eda14cbcSMatt Macy 	rrw_init(&dp->dp_config_rwlock, B_TRUE);
198eda14cbcSMatt Macy 	txg_init(dp, txg);
199eda14cbcSMatt Macy 	mmp_init(spa);
200eda14cbcSMatt Macy 
201eda14cbcSMatt Macy 	txg_list_create(&dp->dp_dirty_datasets, spa,
202eda14cbcSMatt Macy 	    offsetof(dsl_dataset_t, ds_dirty_link));
203eda14cbcSMatt Macy 	txg_list_create(&dp->dp_dirty_zilogs, spa,
204eda14cbcSMatt Macy 	    offsetof(zilog_t, zl_dirty_link));
205eda14cbcSMatt Macy 	txg_list_create(&dp->dp_dirty_dirs, spa,
206eda14cbcSMatt Macy 	    offsetof(dsl_dir_t, dd_dirty_link));
207eda14cbcSMatt Macy 	txg_list_create(&dp->dp_sync_tasks, spa,
208eda14cbcSMatt Macy 	    offsetof(dsl_sync_task_t, dst_node));
209eda14cbcSMatt Macy 	txg_list_create(&dp->dp_early_sync_tasks, spa,
210eda14cbcSMatt Macy 	    offsetof(dsl_sync_task_t, dst_node));
211eda14cbcSMatt Macy 
21214c2e0a0SMartin Matuska 	dp->dp_sync_taskq = spa_sync_tq_create(spa, "dp_sync_taskq");
213eda14cbcSMatt Macy 
214eda14cbcSMatt Macy 	dp->dp_zil_clean_taskq = taskq_create("dp_zil_clean_taskq",
215eda14cbcSMatt Macy 	    zfs_zil_clean_taskq_nthr_pct, minclsyspri,
216eda14cbcSMatt Macy 	    zfs_zil_clean_taskq_minalloc,
217eda14cbcSMatt Macy 	    zfs_zil_clean_taskq_maxalloc,
218eda14cbcSMatt Macy 	    TASKQ_PREPOPULATE | TASKQ_THREADS_CPU_PCT);
219eda14cbcSMatt Macy 
220eda14cbcSMatt Macy 	mutex_init(&dp->dp_lock, NULL, MUTEX_DEFAULT, NULL);
221eda14cbcSMatt Macy 	cv_init(&dp->dp_spaceavail_cv, NULL, CV_DEFAULT, NULL);
222eda14cbcSMatt Macy 
2233f9d360cSMartin Matuska 	aggsum_init(&dp->dp_wrlog_total, 0);
2243f9d360cSMartin Matuska 	for (int i = 0; i < TXG_SIZE; i++) {
2253f9d360cSMartin Matuska 		aggsum_init(&dp->dp_wrlog_pertxg[i], 0);
2263f9d360cSMartin Matuska 	}
2273f9d360cSMartin Matuska 
2287877fdebSMatt Macy 	dp->dp_zrele_taskq = taskq_create("z_zrele", 100, defclsyspri,
2297877fdebSMatt Macy 	    boot_ncpus * 8, INT_MAX, TASKQ_PREPOPULATE | TASKQ_DYNAMIC |
2307877fdebSMatt Macy 	    TASKQ_THREADS_CPU_PCT);
231eda14cbcSMatt Macy 	dp->dp_unlinked_drain_taskq = taskq_create("z_unlinked_drain",
2327877fdebSMatt Macy 	    100, defclsyspri, boot_ncpus, INT_MAX,
2337877fdebSMatt Macy 	    TASKQ_PREPOPULATE | TASKQ_DYNAMIC | TASKQ_THREADS_CPU_PCT);
234eda14cbcSMatt Macy 
235eda14cbcSMatt Macy 	return (dp);
236eda14cbcSMatt Macy }
237eda14cbcSMatt Macy 
238eda14cbcSMatt Macy int
dsl_pool_init(spa_t * spa,uint64_t txg,dsl_pool_t ** dpp)239eda14cbcSMatt Macy dsl_pool_init(spa_t *spa, uint64_t txg, dsl_pool_t **dpp)
240eda14cbcSMatt Macy {
241eda14cbcSMatt Macy 	int err;
242eda14cbcSMatt Macy 	dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
243eda14cbcSMatt Macy 
244eda14cbcSMatt Macy 	/*
245eda14cbcSMatt Macy 	 * Initialize the caller's dsl_pool_t structure before we actually open
246eda14cbcSMatt Macy 	 * the meta objset.  This is done because a self-healing write zio may
247eda14cbcSMatt Macy 	 * be issued as part of dmu_objset_open_impl() and the spa needs its
248eda14cbcSMatt Macy 	 * dsl_pool_t initialized in order to handle the write.
249eda14cbcSMatt Macy 	 */
250eda14cbcSMatt Macy 	*dpp = dp;
251eda14cbcSMatt Macy 
252eda14cbcSMatt Macy 	err = dmu_objset_open_impl(spa, NULL, &dp->dp_meta_rootbp,
253eda14cbcSMatt Macy 	    &dp->dp_meta_objset);
254eda14cbcSMatt Macy 	if (err != 0) {
255eda14cbcSMatt Macy 		dsl_pool_close(dp);
256eda14cbcSMatt Macy 		*dpp = NULL;
257eda14cbcSMatt Macy 	}
258eda14cbcSMatt Macy 
259eda14cbcSMatt Macy 	return (err);
260eda14cbcSMatt Macy }
261eda14cbcSMatt Macy 
262eda14cbcSMatt Macy int
dsl_pool_open(dsl_pool_t * dp)263eda14cbcSMatt Macy dsl_pool_open(dsl_pool_t *dp)
264eda14cbcSMatt Macy {
265eda14cbcSMatt Macy 	int err;
266eda14cbcSMatt Macy 	dsl_dir_t *dd;
267eda14cbcSMatt Macy 	dsl_dataset_t *ds;
268eda14cbcSMatt Macy 	uint64_t obj;
269eda14cbcSMatt Macy 
270eda14cbcSMatt Macy 	rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
271eda14cbcSMatt Macy 	err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
272eda14cbcSMatt Macy 	    DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1,
273eda14cbcSMatt Macy 	    &dp->dp_root_dir_obj);
274eda14cbcSMatt Macy 	if (err)
275eda14cbcSMatt Macy 		goto out;
276eda14cbcSMatt Macy 
277eda14cbcSMatt Macy 	err = dsl_dir_hold_obj(dp, dp->dp_root_dir_obj,
278eda14cbcSMatt Macy 	    NULL, dp, &dp->dp_root_dir);
279eda14cbcSMatt Macy 	if (err)
280eda14cbcSMatt Macy 		goto out;
281eda14cbcSMatt Macy 
282eda14cbcSMatt Macy 	err = dsl_pool_open_special_dir(dp, MOS_DIR_NAME, &dp->dp_mos_dir);
283eda14cbcSMatt Macy 	if (err)
284eda14cbcSMatt Macy 		goto out;
285eda14cbcSMatt Macy 
286eda14cbcSMatt Macy 	if (spa_version(dp->dp_spa) >= SPA_VERSION_ORIGIN) {
287eda14cbcSMatt Macy 		err = dsl_pool_open_special_dir(dp, ORIGIN_DIR_NAME, &dd);
288eda14cbcSMatt Macy 		if (err)
289eda14cbcSMatt Macy 			goto out;
290eda14cbcSMatt Macy 		err = dsl_dataset_hold_obj(dp,
291eda14cbcSMatt Macy 		    dsl_dir_phys(dd)->dd_head_dataset_obj, FTAG, &ds);
292eda14cbcSMatt Macy 		if (err == 0) {
293eda14cbcSMatt Macy 			err = dsl_dataset_hold_obj(dp,
294eda14cbcSMatt Macy 			    dsl_dataset_phys(ds)->ds_prev_snap_obj, dp,
295eda14cbcSMatt Macy 			    &dp->dp_origin_snap);
296eda14cbcSMatt Macy 			dsl_dataset_rele(ds, FTAG);
297eda14cbcSMatt Macy 		}
298eda14cbcSMatt Macy 		dsl_dir_rele(dd, dp);
299eda14cbcSMatt Macy 		if (err)
300eda14cbcSMatt Macy 			goto out;
301eda14cbcSMatt Macy 	}
302eda14cbcSMatt Macy 
303eda14cbcSMatt Macy 	if (spa_version(dp->dp_spa) >= SPA_VERSION_DEADLISTS) {
304eda14cbcSMatt Macy 		err = dsl_pool_open_special_dir(dp, FREE_DIR_NAME,
305eda14cbcSMatt Macy 		    &dp->dp_free_dir);
306eda14cbcSMatt Macy 		if (err)
307eda14cbcSMatt Macy 			goto out;
308eda14cbcSMatt Macy 
309eda14cbcSMatt Macy 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
310eda14cbcSMatt Macy 		    DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj);
311eda14cbcSMatt Macy 		if (err)
312eda14cbcSMatt Macy 			goto out;
313eda14cbcSMatt Macy 		VERIFY0(bpobj_open(&dp->dp_free_bpobj,
314eda14cbcSMatt Macy 		    dp->dp_meta_objset, obj));
315eda14cbcSMatt Macy 	}
316eda14cbcSMatt Macy 
317eda14cbcSMatt Macy 	if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_OBSOLETE_COUNTS)) {
318eda14cbcSMatt Macy 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
319eda14cbcSMatt Macy 		    DMU_POOL_OBSOLETE_BPOBJ, sizeof (uint64_t), 1, &obj);
320eda14cbcSMatt Macy 		if (err == 0) {
321eda14cbcSMatt Macy 			VERIFY0(bpobj_open(&dp->dp_obsolete_bpobj,
322eda14cbcSMatt Macy 			    dp->dp_meta_objset, obj));
323eda14cbcSMatt Macy 		} else if (err == ENOENT) {
324eda14cbcSMatt Macy 			/*
325eda14cbcSMatt Macy 			 * We might not have created the remap bpobj yet.
326eda14cbcSMatt Macy 			 */
327eda14cbcSMatt Macy 		} else {
328eda14cbcSMatt Macy 			goto out;
329eda14cbcSMatt Macy 		}
330eda14cbcSMatt Macy 	}
331eda14cbcSMatt Macy 
332eda14cbcSMatt Macy 	/*
333eda14cbcSMatt Macy 	 * Note: errors ignored, because the these special dirs, used for
334eda14cbcSMatt Macy 	 * space accounting, are only created on demand.
335eda14cbcSMatt Macy 	 */
336eda14cbcSMatt Macy 	(void) dsl_pool_open_special_dir(dp, LEAK_DIR_NAME,
337eda14cbcSMatt Macy 	    &dp->dp_leak_dir);
338eda14cbcSMatt Macy 
339eda14cbcSMatt Macy 	if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_ASYNC_DESTROY)) {
340eda14cbcSMatt Macy 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
341eda14cbcSMatt Macy 		    DMU_POOL_BPTREE_OBJ, sizeof (uint64_t), 1,
342eda14cbcSMatt Macy 		    &dp->dp_bptree_obj);
343eda14cbcSMatt Macy 		if (err != 0)
344eda14cbcSMatt Macy 			goto out;
345eda14cbcSMatt Macy 	}
346eda14cbcSMatt Macy 
347eda14cbcSMatt Macy 	if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_EMPTY_BPOBJ)) {
348eda14cbcSMatt Macy 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
349eda14cbcSMatt Macy 		    DMU_POOL_EMPTY_BPOBJ, sizeof (uint64_t), 1,
350eda14cbcSMatt Macy 		    &dp->dp_empty_bpobj);
351eda14cbcSMatt Macy 		if (err != 0)
352eda14cbcSMatt Macy 			goto out;
353eda14cbcSMatt Macy 	}
354eda14cbcSMatt Macy 
355eda14cbcSMatt Macy 	err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
356eda14cbcSMatt Macy 	    DMU_POOL_TMP_USERREFS, sizeof (uint64_t), 1,
357eda14cbcSMatt Macy 	    &dp->dp_tmp_userrefs_obj);
358eda14cbcSMatt Macy 	if (err == ENOENT)
359eda14cbcSMatt Macy 		err = 0;
360eda14cbcSMatt Macy 	if (err)
361eda14cbcSMatt Macy 		goto out;
362eda14cbcSMatt Macy 
363eda14cbcSMatt Macy 	err = dsl_scan_init(dp, dp->dp_tx.tx_open_txg);
364eda14cbcSMatt Macy 
365eda14cbcSMatt Macy out:
366eda14cbcSMatt Macy 	rrw_exit(&dp->dp_config_rwlock, FTAG);
367eda14cbcSMatt Macy 	return (err);
368eda14cbcSMatt Macy }
369eda14cbcSMatt Macy 
370eda14cbcSMatt Macy void
dsl_pool_close(dsl_pool_t * dp)371eda14cbcSMatt Macy dsl_pool_close(dsl_pool_t *dp)
372eda14cbcSMatt Macy {
373eda14cbcSMatt Macy 	/*
374eda14cbcSMatt Macy 	 * Drop our references from dsl_pool_open().
375eda14cbcSMatt Macy 	 *
376eda14cbcSMatt Macy 	 * Since we held the origin_snap from "syncing" context (which
377eda14cbcSMatt Macy 	 * includes pool-opening context), it actually only got a "ref"
378eda14cbcSMatt Macy 	 * and not a hold, so just drop that here.
379eda14cbcSMatt Macy 	 */
380eda14cbcSMatt Macy 	if (dp->dp_origin_snap != NULL)
381eda14cbcSMatt Macy 		dsl_dataset_rele(dp->dp_origin_snap, dp);
382eda14cbcSMatt Macy 	if (dp->dp_mos_dir != NULL)
383eda14cbcSMatt Macy 		dsl_dir_rele(dp->dp_mos_dir, dp);
384eda14cbcSMatt Macy 	if (dp->dp_free_dir != NULL)
385eda14cbcSMatt Macy 		dsl_dir_rele(dp->dp_free_dir, dp);
386eda14cbcSMatt Macy 	if (dp->dp_leak_dir != NULL)
387eda14cbcSMatt Macy 		dsl_dir_rele(dp->dp_leak_dir, dp);
388eda14cbcSMatt Macy 	if (dp->dp_root_dir != NULL)
389eda14cbcSMatt Macy 		dsl_dir_rele(dp->dp_root_dir, dp);
390eda14cbcSMatt Macy 
391eda14cbcSMatt Macy 	bpobj_close(&dp->dp_free_bpobj);
392eda14cbcSMatt Macy 	bpobj_close(&dp->dp_obsolete_bpobj);
393eda14cbcSMatt Macy 
394eda14cbcSMatt Macy 	/* undo the dmu_objset_open_impl(mos) from dsl_pool_open() */
395eda14cbcSMatt Macy 	if (dp->dp_meta_objset != NULL)
396eda14cbcSMatt Macy 		dmu_objset_evict(dp->dp_meta_objset);
397eda14cbcSMatt Macy 
398eda14cbcSMatt Macy 	txg_list_destroy(&dp->dp_dirty_datasets);
399eda14cbcSMatt Macy 	txg_list_destroy(&dp->dp_dirty_zilogs);
400eda14cbcSMatt Macy 	txg_list_destroy(&dp->dp_sync_tasks);
401eda14cbcSMatt Macy 	txg_list_destroy(&dp->dp_early_sync_tasks);
402eda14cbcSMatt Macy 	txg_list_destroy(&dp->dp_dirty_dirs);
403eda14cbcSMatt Macy 
404eda14cbcSMatt Macy 	taskq_destroy(dp->dp_zil_clean_taskq);
40514c2e0a0SMartin Matuska 	spa_sync_tq_destroy(dp->dp_spa);
406eda14cbcSMatt Macy 
407eda14cbcSMatt Macy 	/*
408eda14cbcSMatt Macy 	 * We can't set retry to TRUE since we're explicitly specifying
409eda14cbcSMatt Macy 	 * a spa to flush. This is good enough; any missed buffers for
410eda14cbcSMatt Macy 	 * this spa won't cause trouble, and they'll eventually fall
411eda14cbcSMatt Macy 	 * out of the ARC just like any other unused buffer.
412eda14cbcSMatt Macy 	 */
413eda14cbcSMatt Macy 	arc_flush(dp->dp_spa, FALSE);
414eda14cbcSMatt Macy 
415eda14cbcSMatt Macy 	mmp_fini(dp->dp_spa);
416eda14cbcSMatt Macy 	txg_fini(dp);
417eda14cbcSMatt Macy 	dsl_scan_fini(dp);
418eda14cbcSMatt Macy 	dmu_buf_user_evict_wait();
419eda14cbcSMatt Macy 
420eda14cbcSMatt Macy 	rrw_destroy(&dp->dp_config_rwlock);
421eda14cbcSMatt Macy 	mutex_destroy(&dp->dp_lock);
422eda14cbcSMatt Macy 	cv_destroy(&dp->dp_spaceavail_cv);
4233f9d360cSMartin Matuska 
4243f9d360cSMartin Matuska 	ASSERT0(aggsum_value(&dp->dp_wrlog_total));
4253f9d360cSMartin Matuska 	aggsum_fini(&dp->dp_wrlog_total);
4263f9d360cSMartin Matuska 	for (int i = 0; i < TXG_SIZE; i++) {
4273f9d360cSMartin Matuska 		ASSERT0(aggsum_value(&dp->dp_wrlog_pertxg[i]));
4283f9d360cSMartin Matuska 		aggsum_fini(&dp->dp_wrlog_pertxg[i]);
4293f9d360cSMartin Matuska 	}
4303f9d360cSMartin Matuska 
431eda14cbcSMatt Macy 	taskq_destroy(dp->dp_unlinked_drain_taskq);
432eda14cbcSMatt Macy 	taskq_destroy(dp->dp_zrele_taskq);
433a0b956f5SMartin Matuska 	if (dp->dp_blkstats != NULL)
434eda14cbcSMatt Macy 		vmem_free(dp->dp_blkstats, sizeof (zfs_all_blkstats_t));
435eda14cbcSMatt Macy 	kmem_free(dp, sizeof (dsl_pool_t));
436eda14cbcSMatt Macy }
437eda14cbcSMatt Macy 
438eda14cbcSMatt Macy void
dsl_pool_create_obsolete_bpobj(dsl_pool_t * dp,dmu_tx_t * tx)439eda14cbcSMatt Macy dsl_pool_create_obsolete_bpobj(dsl_pool_t *dp, dmu_tx_t *tx)
440eda14cbcSMatt Macy {
441eda14cbcSMatt Macy 	uint64_t obj;
442eda14cbcSMatt Macy 	/*
443eda14cbcSMatt Macy 	 * Currently, we only create the obsolete_bpobj where there are
444eda14cbcSMatt Macy 	 * indirect vdevs with referenced mappings.
445eda14cbcSMatt Macy 	 */
446eda14cbcSMatt Macy 	ASSERT(spa_feature_is_active(dp->dp_spa, SPA_FEATURE_DEVICE_REMOVAL));
447eda14cbcSMatt Macy 	/* create and open the obsolete_bpobj */
448eda14cbcSMatt Macy 	obj = bpobj_alloc(dp->dp_meta_objset, SPA_OLD_MAXBLOCKSIZE, tx);
449eda14cbcSMatt Macy 	VERIFY0(bpobj_open(&dp->dp_obsolete_bpobj, dp->dp_meta_objset, obj));
450eda14cbcSMatt Macy 	VERIFY0(zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
451eda14cbcSMatt Macy 	    DMU_POOL_OBSOLETE_BPOBJ, sizeof (uint64_t), 1, &obj, tx));
452eda14cbcSMatt Macy 	spa_feature_incr(dp->dp_spa, SPA_FEATURE_OBSOLETE_COUNTS, tx);
453eda14cbcSMatt Macy }
454eda14cbcSMatt Macy 
455eda14cbcSMatt Macy void
dsl_pool_destroy_obsolete_bpobj(dsl_pool_t * dp,dmu_tx_t * tx)456eda14cbcSMatt Macy dsl_pool_destroy_obsolete_bpobj(dsl_pool_t *dp, dmu_tx_t *tx)
457eda14cbcSMatt Macy {
458eda14cbcSMatt Macy 	spa_feature_decr(dp->dp_spa, SPA_FEATURE_OBSOLETE_COUNTS, tx);
459eda14cbcSMatt Macy 	VERIFY0(zap_remove(dp->dp_meta_objset,
460eda14cbcSMatt Macy 	    DMU_POOL_DIRECTORY_OBJECT,
461eda14cbcSMatt Macy 	    DMU_POOL_OBSOLETE_BPOBJ, tx));
462eda14cbcSMatt Macy 	bpobj_free(dp->dp_meta_objset,
463eda14cbcSMatt Macy 	    dp->dp_obsolete_bpobj.bpo_object, tx);
464eda14cbcSMatt Macy 	bpobj_close(&dp->dp_obsolete_bpobj);
465eda14cbcSMatt Macy }
466eda14cbcSMatt Macy 
467eda14cbcSMatt Macy dsl_pool_t *
dsl_pool_create(spa_t * spa,nvlist_t * zplprops,dsl_crypto_params_t * dcp,uint64_t txg)468e92ffd9bSMartin Matuska dsl_pool_create(spa_t *spa, nvlist_t *zplprops __attribute__((unused)),
469e92ffd9bSMartin Matuska     dsl_crypto_params_t *dcp, uint64_t txg)
470eda14cbcSMatt Macy {
471eda14cbcSMatt Macy 	int err;
472eda14cbcSMatt Macy 	dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
473eda14cbcSMatt Macy 	dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg);
474eda14cbcSMatt Macy #ifdef _KERNEL
475eda14cbcSMatt Macy 	objset_t *os;
476eda14cbcSMatt Macy #else
477eda14cbcSMatt Macy 	objset_t *os __attribute__((unused));
478eda14cbcSMatt Macy #endif
479eda14cbcSMatt Macy 	dsl_dataset_t *ds;
480eda14cbcSMatt Macy 	uint64_t obj;
481eda14cbcSMatt Macy 
482eda14cbcSMatt Macy 	rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
483eda14cbcSMatt Macy 
484eda14cbcSMatt Macy 	/* create and open the MOS (meta-objset) */
485eda14cbcSMatt Macy 	dp->dp_meta_objset = dmu_objset_create_impl(spa,
486eda14cbcSMatt Macy 	    NULL, &dp->dp_meta_rootbp, DMU_OST_META, tx);
487eda14cbcSMatt Macy 	spa->spa_meta_objset = dp->dp_meta_objset;
488eda14cbcSMatt Macy 
489eda14cbcSMatt Macy 	/* create the pool directory */
490eda14cbcSMatt Macy 	err = zap_create_claim(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
491eda14cbcSMatt Macy 	    DMU_OT_OBJECT_DIRECTORY, DMU_OT_NONE, 0, tx);
492eda14cbcSMatt Macy 	ASSERT0(err);
493eda14cbcSMatt Macy 
494eda14cbcSMatt Macy 	/* Initialize scan structures */
495eda14cbcSMatt Macy 	VERIFY0(dsl_scan_init(dp, txg));
496eda14cbcSMatt Macy 
497eda14cbcSMatt Macy 	/* create and open the root dir */
498eda14cbcSMatt Macy 	dp->dp_root_dir_obj = dsl_dir_create_sync(dp, NULL, NULL, tx);
499eda14cbcSMatt Macy 	VERIFY0(dsl_dir_hold_obj(dp, dp->dp_root_dir_obj,
500eda14cbcSMatt Macy 	    NULL, dp, &dp->dp_root_dir));
501eda14cbcSMatt Macy 
502eda14cbcSMatt Macy 	/* create and open the meta-objset dir */
503eda14cbcSMatt Macy 	(void) dsl_dir_create_sync(dp, dp->dp_root_dir, MOS_DIR_NAME, tx);
504eda14cbcSMatt Macy 	VERIFY0(dsl_pool_open_special_dir(dp,
505eda14cbcSMatt Macy 	    MOS_DIR_NAME, &dp->dp_mos_dir));
506eda14cbcSMatt Macy 
507eda14cbcSMatt Macy 	if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
508eda14cbcSMatt Macy 		/* create and open the free dir */
509eda14cbcSMatt Macy 		(void) dsl_dir_create_sync(dp, dp->dp_root_dir,
510eda14cbcSMatt Macy 		    FREE_DIR_NAME, tx);
511eda14cbcSMatt Macy 		VERIFY0(dsl_pool_open_special_dir(dp,
512eda14cbcSMatt Macy 		    FREE_DIR_NAME, &dp->dp_free_dir));
513eda14cbcSMatt Macy 
514eda14cbcSMatt Macy 		/* create and open the free_bplist */
515eda14cbcSMatt Macy 		obj = bpobj_alloc(dp->dp_meta_objset, SPA_OLD_MAXBLOCKSIZE, tx);
516eda14cbcSMatt Macy 		VERIFY(zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
517eda14cbcSMatt Macy 		    DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj, tx) == 0);
518eda14cbcSMatt Macy 		VERIFY0(bpobj_open(&dp->dp_free_bpobj,
519eda14cbcSMatt Macy 		    dp->dp_meta_objset, obj));
520eda14cbcSMatt Macy 	}
521eda14cbcSMatt Macy 
522eda14cbcSMatt Macy 	if (spa_version(spa) >= SPA_VERSION_DSL_SCRUB)
523eda14cbcSMatt Macy 		dsl_pool_create_origin(dp, tx);
524eda14cbcSMatt Macy 
525eda14cbcSMatt Macy 	/*
526eda14cbcSMatt Macy 	 * Some features may be needed when creating the root dataset, so we
527eda14cbcSMatt Macy 	 * create the feature objects here.
528eda14cbcSMatt Macy 	 */
529eda14cbcSMatt Macy 	if (spa_version(spa) >= SPA_VERSION_FEATURES)
530eda14cbcSMatt Macy 		spa_feature_create_zap_objects(spa, tx);
531eda14cbcSMatt Macy 
532eda14cbcSMatt Macy 	if (dcp != NULL && dcp->cp_crypt != ZIO_CRYPT_OFF &&
533eda14cbcSMatt Macy 	    dcp->cp_crypt != ZIO_CRYPT_INHERIT)
534eda14cbcSMatt Macy 		spa_feature_enable(spa, SPA_FEATURE_ENCRYPTION, tx);
535eda14cbcSMatt Macy 
536eda14cbcSMatt Macy 	/* create the root dataset */
537eda14cbcSMatt Macy 	obj = dsl_dataset_create_sync_dd(dp->dp_root_dir, NULL, dcp, 0, tx);
538eda14cbcSMatt Macy 
539eda14cbcSMatt Macy 	/* create the root objset */
540eda14cbcSMatt Macy 	VERIFY0(dsl_dataset_hold_obj_flags(dp, obj,
541eda14cbcSMatt Macy 	    DS_HOLD_FLAG_DECRYPT, FTAG, &ds));
542eda14cbcSMatt Macy 	rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
543eda14cbcSMatt Macy 	os = dmu_objset_create_impl(dp->dp_spa, ds,
544eda14cbcSMatt Macy 	    dsl_dataset_get_blkptr(ds), DMU_OST_ZFS, tx);
545eda14cbcSMatt Macy 	rrw_exit(&ds->ds_bp_rwlock, FTAG);
546eda14cbcSMatt Macy #ifdef _KERNEL
547eda14cbcSMatt Macy 	zfs_create_fs(os, kcred, zplprops, tx);
548eda14cbcSMatt Macy #endif
549eda14cbcSMatt Macy 	dsl_dataset_rele_flags(ds, DS_HOLD_FLAG_DECRYPT, FTAG);
550eda14cbcSMatt Macy 
551eda14cbcSMatt Macy 	dmu_tx_commit(tx);
552eda14cbcSMatt Macy 
553eda14cbcSMatt Macy 	rrw_exit(&dp->dp_config_rwlock, FTAG);
554eda14cbcSMatt Macy 
555eda14cbcSMatt Macy 	return (dp);
556eda14cbcSMatt Macy }
557eda14cbcSMatt Macy 
558eda14cbcSMatt Macy /*
559eda14cbcSMatt Macy  * Account for the meta-objset space in its placeholder dsl_dir.
560eda14cbcSMatt Macy  */
561eda14cbcSMatt Macy void
dsl_pool_mos_diduse_space(dsl_pool_t * dp,int64_t used,int64_t comp,int64_t uncomp)562eda14cbcSMatt Macy dsl_pool_mos_diduse_space(dsl_pool_t *dp,
563eda14cbcSMatt Macy     int64_t used, int64_t comp, int64_t uncomp)
564eda14cbcSMatt Macy {
565eda14cbcSMatt Macy 	ASSERT3U(comp, ==, uncomp); /* it's all metadata */
566eda14cbcSMatt Macy 	mutex_enter(&dp->dp_lock);
567eda14cbcSMatt Macy 	dp->dp_mos_used_delta += used;
568eda14cbcSMatt Macy 	dp->dp_mos_compressed_delta += comp;
569eda14cbcSMatt Macy 	dp->dp_mos_uncompressed_delta += uncomp;
570eda14cbcSMatt Macy 	mutex_exit(&dp->dp_lock);
571eda14cbcSMatt Macy }
572eda14cbcSMatt Macy 
573eda14cbcSMatt Macy static void
dsl_pool_sync_mos(dsl_pool_t * dp,dmu_tx_t * tx)574eda14cbcSMatt Macy dsl_pool_sync_mos(dsl_pool_t *dp, dmu_tx_t *tx)
575eda14cbcSMatt Macy {
576eda14cbcSMatt Macy 	zio_t *zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
577eda14cbcSMatt Macy 	dmu_objset_sync(dp->dp_meta_objset, zio, tx);
578eda14cbcSMatt Macy 	VERIFY0(zio_wait(zio));
5797877fdebSMatt Macy 	dmu_objset_sync_done(dp->dp_meta_objset, tx);
5807877fdebSMatt Macy 	taskq_wait(dp->dp_sync_taskq);
5813ff01b23SMartin Matuska 	multilist_destroy(&dp->dp_meta_objset->os_synced_dnodes);
5827877fdebSMatt Macy 
583eda14cbcSMatt Macy 	dprintf_bp(&dp->dp_meta_rootbp, "meta objset rootbp is %s", "");
584eda14cbcSMatt Macy 	spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
585eda14cbcSMatt Macy }
586eda14cbcSMatt Macy 
587eda14cbcSMatt Macy static void
dsl_pool_dirty_delta(dsl_pool_t * dp,int64_t delta)588eda14cbcSMatt Macy dsl_pool_dirty_delta(dsl_pool_t *dp, int64_t delta)
589eda14cbcSMatt Macy {
590eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&dp->dp_lock));
591eda14cbcSMatt Macy 
592eda14cbcSMatt Macy 	if (delta < 0)
593eda14cbcSMatt Macy 		ASSERT3U(-delta, <=, dp->dp_dirty_total);
594eda14cbcSMatt Macy 
595eda14cbcSMatt Macy 	dp->dp_dirty_total += delta;
596eda14cbcSMatt Macy 
597eda14cbcSMatt Macy 	/*
598eda14cbcSMatt Macy 	 * Note: we signal even when increasing dp_dirty_total.
599eda14cbcSMatt Macy 	 * This ensures forward progress -- each thread wakes the next waiter.
600eda14cbcSMatt Macy 	 */
601eda14cbcSMatt Macy 	if (dp->dp_dirty_total < zfs_dirty_data_max)
602eda14cbcSMatt Macy 		cv_signal(&dp->dp_spaceavail_cv);
603eda14cbcSMatt Macy }
604eda14cbcSMatt Macy 
6053f9d360cSMartin Matuska void
dsl_pool_wrlog_count(dsl_pool_t * dp,int64_t size,uint64_t txg)6063f9d360cSMartin Matuska dsl_pool_wrlog_count(dsl_pool_t *dp, int64_t size, uint64_t txg)
6073f9d360cSMartin Matuska {
6083f9d360cSMartin Matuska 	ASSERT3S(size, >=, 0);
6093f9d360cSMartin Matuska 
6103f9d360cSMartin Matuska 	aggsum_add(&dp->dp_wrlog_pertxg[txg & TXG_MASK], size);
6113f9d360cSMartin Matuska 	aggsum_add(&dp->dp_wrlog_total, size);
6123f9d360cSMartin Matuska 
6133f9d360cSMartin Matuska 	/* Choose a value slightly bigger than min dirty sync bytes */
6143f9d360cSMartin Matuska 	uint64_t sync_min =
615e3aa18adSMartin Matuska 	    zfs_wrlog_data_max * (zfs_dirty_data_sync_percent + 10) / 200;
6163f9d360cSMartin Matuska 	if (aggsum_compare(&dp->dp_wrlog_pertxg[txg & TXG_MASK], sync_min) > 0)
6173f9d360cSMartin Matuska 		txg_kick(dp, txg);
6183f9d360cSMartin Matuska }
6193f9d360cSMartin Matuska 
6203f9d360cSMartin Matuska boolean_t
dsl_pool_need_wrlog_delay(dsl_pool_t * dp)621e3aa18adSMartin Matuska dsl_pool_need_wrlog_delay(dsl_pool_t *dp)
6223f9d360cSMartin Matuska {
623e3aa18adSMartin Matuska 	uint64_t delay_min_bytes =
624e3aa18adSMartin Matuska 	    zfs_wrlog_data_max * zfs_delay_min_dirty_percent / 100;
625e3aa18adSMartin Matuska 
626e3aa18adSMartin Matuska 	return (aggsum_compare(&dp->dp_wrlog_total, delay_min_bytes) > 0);
6273f9d360cSMartin Matuska }
6283f9d360cSMartin Matuska 
6293f9d360cSMartin Matuska static void
dsl_pool_wrlog_clear(dsl_pool_t * dp,uint64_t txg)6303f9d360cSMartin Matuska dsl_pool_wrlog_clear(dsl_pool_t *dp, uint64_t txg)
6313f9d360cSMartin Matuska {
6323f9d360cSMartin Matuska 	int64_t delta;
6333f9d360cSMartin Matuska 	delta = -(int64_t)aggsum_value(&dp->dp_wrlog_pertxg[txg & TXG_MASK]);
6343f9d360cSMartin Matuska 	aggsum_add(&dp->dp_wrlog_pertxg[txg & TXG_MASK], delta);
6353f9d360cSMartin Matuska 	aggsum_add(&dp->dp_wrlog_total, delta);
636e3aa18adSMartin Matuska 	/* Compact per-CPU sums after the big change. */
637e3aa18adSMartin Matuska 	(void) aggsum_value(&dp->dp_wrlog_pertxg[txg & TXG_MASK]);
638e3aa18adSMartin Matuska 	(void) aggsum_value(&dp->dp_wrlog_total);
6393f9d360cSMartin Matuska }
6403f9d360cSMartin Matuska 
641eda14cbcSMatt Macy #ifdef ZFS_DEBUG
642eda14cbcSMatt Macy static boolean_t
dsl_early_sync_task_verify(dsl_pool_t * dp,uint64_t txg)643eda14cbcSMatt Macy dsl_early_sync_task_verify(dsl_pool_t *dp, uint64_t txg)
644eda14cbcSMatt Macy {
645eda14cbcSMatt Macy 	spa_t *spa = dp->dp_spa;
646eda14cbcSMatt Macy 	vdev_t *rvd = spa->spa_root_vdev;
647eda14cbcSMatt Macy 
648eda14cbcSMatt Macy 	for (uint64_t c = 0; c < rvd->vdev_children; c++) {
649eda14cbcSMatt Macy 		vdev_t *vd = rvd->vdev_child[c];
650eda14cbcSMatt Macy 		txg_list_t *tl = &vd->vdev_ms_list;
651eda14cbcSMatt Macy 		metaslab_t *ms;
652eda14cbcSMatt Macy 
653eda14cbcSMatt Macy 		for (ms = txg_list_head(tl, TXG_CLEAN(txg)); ms;
654eda14cbcSMatt Macy 		    ms = txg_list_next(tl, ms, TXG_CLEAN(txg))) {
655eda14cbcSMatt Macy 			VERIFY(range_tree_is_empty(ms->ms_freeing));
656eda14cbcSMatt Macy 			VERIFY(range_tree_is_empty(ms->ms_checkpointing));
657eda14cbcSMatt Macy 		}
658eda14cbcSMatt Macy 	}
659eda14cbcSMatt Macy 
660eda14cbcSMatt Macy 	return (B_TRUE);
661eda14cbcSMatt Macy }
662e92ffd9bSMartin Matuska #else
663e92ffd9bSMartin Matuska #define	dsl_early_sync_task_verify(dp, txg) \
664e92ffd9bSMartin Matuska 	((void) sizeof (dp), (void) sizeof (txg), B_TRUE)
665eda14cbcSMatt Macy #endif
666eda14cbcSMatt Macy 
667eda14cbcSMatt Macy void
dsl_pool_sync(dsl_pool_t * dp,uint64_t txg)668eda14cbcSMatt Macy dsl_pool_sync(dsl_pool_t *dp, uint64_t txg)
669eda14cbcSMatt Macy {
67014c2e0a0SMartin Matuska 	zio_t *rio;	/* root zio for all dirty dataset syncs */
671eda14cbcSMatt Macy 	dmu_tx_t *tx;
672eda14cbcSMatt Macy 	dsl_dir_t *dd;
673eda14cbcSMatt Macy 	dsl_dataset_t *ds;
674eda14cbcSMatt Macy 	objset_t *mos = dp->dp_meta_objset;
675eda14cbcSMatt Macy 	list_t synced_datasets;
676eda14cbcSMatt Macy 
677eda14cbcSMatt Macy 	list_create(&synced_datasets, sizeof (dsl_dataset_t),
678eda14cbcSMatt Macy 	    offsetof(dsl_dataset_t, ds_synced_link));
679eda14cbcSMatt Macy 
680eda14cbcSMatt Macy 	tx = dmu_tx_create_assigned(dp, txg);
681eda14cbcSMatt Macy 
682eda14cbcSMatt Macy 	/*
683eda14cbcSMatt Macy 	 * Run all early sync tasks before writing out any dirty blocks.
684eda14cbcSMatt Macy 	 * For more info on early sync tasks see block comment in
685eda14cbcSMatt Macy 	 * dsl_early_sync_task().
686eda14cbcSMatt Macy 	 */
687eda14cbcSMatt Macy 	if (!txg_list_empty(&dp->dp_early_sync_tasks, txg)) {
688eda14cbcSMatt Macy 		dsl_sync_task_t *dst;
689eda14cbcSMatt Macy 
690eda14cbcSMatt Macy 		ASSERT3U(spa_sync_pass(dp->dp_spa), ==, 1);
691eda14cbcSMatt Macy 		while ((dst =
692eda14cbcSMatt Macy 		    txg_list_remove(&dp->dp_early_sync_tasks, txg)) != NULL) {
693eda14cbcSMatt Macy 			ASSERT(dsl_early_sync_task_verify(dp, txg));
694eda14cbcSMatt Macy 			dsl_sync_task_sync(dst, tx);
695eda14cbcSMatt Macy 		}
696eda14cbcSMatt Macy 		ASSERT(dsl_early_sync_task_verify(dp, txg));
697eda14cbcSMatt Macy 	}
698eda14cbcSMatt Macy 
699eda14cbcSMatt Macy 	/*
70014c2e0a0SMartin Matuska 	 * Write out all dirty blocks of dirty datasets. Note, this could
70114c2e0a0SMartin Matuska 	 * create a very large (+10k) zio tree.
702eda14cbcSMatt Macy 	 */
70314c2e0a0SMartin Matuska 	rio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
704eda14cbcSMatt Macy 	while ((ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) != NULL) {
705eda14cbcSMatt Macy 		/*
706eda14cbcSMatt Macy 		 * We must not sync any non-MOS datasets twice, because
707eda14cbcSMatt Macy 		 * we may have taken a snapshot of them.  However, we
708eda14cbcSMatt Macy 		 * may sync newly-created datasets on pass 2.
709eda14cbcSMatt Macy 		 */
710eda14cbcSMatt Macy 		ASSERT(!list_link_active(&ds->ds_synced_link));
711eda14cbcSMatt Macy 		list_insert_tail(&synced_datasets, ds);
71214c2e0a0SMartin Matuska 		dsl_dataset_sync(ds, rio, tx);
713eda14cbcSMatt Macy 	}
71414c2e0a0SMartin Matuska 	VERIFY0(zio_wait(rio));
715eda14cbcSMatt Macy 
716eda14cbcSMatt Macy 	/*
717eda14cbcSMatt Macy 	 * Update the long range free counter after
718eda14cbcSMatt Macy 	 * we're done syncing user data
719eda14cbcSMatt Macy 	 */
720eda14cbcSMatt Macy 	mutex_enter(&dp->dp_lock);
721eda14cbcSMatt Macy 	ASSERT(spa_sync_pass(dp->dp_spa) == 1 ||
722eda14cbcSMatt Macy 	    dp->dp_long_free_dirty_pertxg[txg & TXG_MASK] == 0);
723eda14cbcSMatt Macy 	dp->dp_long_free_dirty_pertxg[txg & TXG_MASK] = 0;
724eda14cbcSMatt Macy 	mutex_exit(&dp->dp_lock);
725eda14cbcSMatt Macy 
726eda14cbcSMatt Macy 	/*
727eda14cbcSMatt Macy 	 * After the data blocks have been written (ensured by the zio_wait()
728eda14cbcSMatt Macy 	 * above), update the user/group/project space accounting.  This happens
729eda14cbcSMatt Macy 	 * in tasks dispatched to dp_sync_taskq, so wait for them before
730eda14cbcSMatt Macy 	 * continuing.
731eda14cbcSMatt Macy 	 */
732eda14cbcSMatt Macy 	for (ds = list_head(&synced_datasets); ds != NULL;
733eda14cbcSMatt Macy 	    ds = list_next(&synced_datasets, ds)) {
7347877fdebSMatt Macy 		dmu_objset_sync_done(ds->ds_objset, tx);
735eda14cbcSMatt Macy 	}
736eda14cbcSMatt Macy 	taskq_wait(dp->dp_sync_taskq);
737eda14cbcSMatt Macy 
738eda14cbcSMatt Macy 	/*
739eda14cbcSMatt Macy 	 * Sync the datasets again to push out the changes due to
740eda14cbcSMatt Macy 	 * userspace updates.  This must be done before we process the
741eda14cbcSMatt Macy 	 * sync tasks, so that any snapshots will have the correct
742eda14cbcSMatt Macy 	 * user accounting information (and we won't get confused
743eda14cbcSMatt Macy 	 * about which blocks are part of the snapshot).
744eda14cbcSMatt Macy 	 */
74514c2e0a0SMartin Matuska 	rio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
746eda14cbcSMatt Macy 	while ((ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) != NULL) {
747eda14cbcSMatt Macy 		objset_t *os = ds->ds_objset;
748eda14cbcSMatt Macy 
749eda14cbcSMatt Macy 		ASSERT(list_link_active(&ds->ds_synced_link));
750eda14cbcSMatt Macy 		dmu_buf_rele(ds->ds_dbuf, ds);
75114c2e0a0SMartin Matuska 		dsl_dataset_sync(ds, rio, tx);
752eda14cbcSMatt Macy 
753eda14cbcSMatt Macy 		/*
754eda14cbcSMatt Macy 		 * Release any key mappings created by calls to
755eda14cbcSMatt Macy 		 * dsl_dataset_dirty() from the userquota accounting
756eda14cbcSMatt Macy 		 * code paths.
757eda14cbcSMatt Macy 		 */
758eda14cbcSMatt Macy 		if (os->os_encrypted && !os->os_raw_receive &&
759eda14cbcSMatt Macy 		    !os->os_next_write_raw[txg & TXG_MASK]) {
760eda14cbcSMatt Macy 			ASSERT3P(ds->ds_key_mapping, !=, NULL);
761eda14cbcSMatt Macy 			key_mapping_rele(dp->dp_spa, ds->ds_key_mapping, ds);
762eda14cbcSMatt Macy 		}
763eda14cbcSMatt Macy 	}
76414c2e0a0SMartin Matuska 	VERIFY0(zio_wait(rio));
765eda14cbcSMatt Macy 
766eda14cbcSMatt Macy 	/*
767eda14cbcSMatt Macy 	 * Now that the datasets have been completely synced, we can
768eda14cbcSMatt Macy 	 * clean up our in-memory structures accumulated while syncing:
769eda14cbcSMatt Macy 	 *
770eda14cbcSMatt Macy 	 *  - move dead blocks from the pending deadlist and livelists
771eda14cbcSMatt Macy 	 *    to the on-disk versions
772eda14cbcSMatt Macy 	 *  - release hold from dsl_dataset_dirty()
773eda14cbcSMatt Macy 	 *  - release key mapping hold from dsl_dataset_dirty()
774eda14cbcSMatt Macy 	 */
775eda14cbcSMatt Macy 	while ((ds = list_remove_head(&synced_datasets)) != NULL) {
776eda14cbcSMatt Macy 		objset_t *os = ds->ds_objset;
777eda14cbcSMatt Macy 
778eda14cbcSMatt Macy 		if (os->os_encrypted && !os->os_raw_receive &&
779eda14cbcSMatt Macy 		    !os->os_next_write_raw[txg & TXG_MASK]) {
780eda14cbcSMatt Macy 			ASSERT3P(ds->ds_key_mapping, !=, NULL);
781eda14cbcSMatt Macy 			key_mapping_rele(dp->dp_spa, ds->ds_key_mapping, ds);
782eda14cbcSMatt Macy 		}
783eda14cbcSMatt Macy 
784eda14cbcSMatt Macy 		dsl_dataset_sync_done(ds, tx);
7852a58b312SMartin Matuska 		dmu_buf_rele(ds->ds_dbuf, ds);
786eda14cbcSMatt Macy 	}
787eda14cbcSMatt Macy 
788eda14cbcSMatt Macy 	while ((dd = txg_list_remove(&dp->dp_dirty_dirs, txg)) != NULL) {
789eda14cbcSMatt Macy 		dsl_dir_sync(dd, tx);
790eda14cbcSMatt Macy 	}
791eda14cbcSMatt Macy 
792eda14cbcSMatt Macy 	/*
793eda14cbcSMatt Macy 	 * The MOS's space is accounted for in the pool/$MOS
794eda14cbcSMatt Macy 	 * (dp_mos_dir).  We can't modify the mos while we're syncing
795eda14cbcSMatt Macy 	 * it, so we remember the deltas and apply them here.
796eda14cbcSMatt Macy 	 */
797eda14cbcSMatt Macy 	if (dp->dp_mos_used_delta != 0 || dp->dp_mos_compressed_delta != 0 ||
798eda14cbcSMatt Macy 	    dp->dp_mos_uncompressed_delta != 0) {
799eda14cbcSMatt Macy 		dsl_dir_diduse_space(dp->dp_mos_dir, DD_USED_HEAD,
800eda14cbcSMatt Macy 		    dp->dp_mos_used_delta,
801eda14cbcSMatt Macy 		    dp->dp_mos_compressed_delta,
802eda14cbcSMatt Macy 		    dp->dp_mos_uncompressed_delta, tx);
803eda14cbcSMatt Macy 		dp->dp_mos_used_delta = 0;
804eda14cbcSMatt Macy 		dp->dp_mos_compressed_delta = 0;
805eda14cbcSMatt Macy 		dp->dp_mos_uncompressed_delta = 0;
806eda14cbcSMatt Macy 	}
807eda14cbcSMatt Macy 
808eda14cbcSMatt Macy 	if (dmu_objset_is_dirty(mos, txg)) {
809eda14cbcSMatt Macy 		dsl_pool_sync_mos(dp, tx);
810eda14cbcSMatt Macy 	}
811eda14cbcSMatt Macy 
812eda14cbcSMatt Macy 	/*
813eda14cbcSMatt Macy 	 * We have written all of the accounted dirty data, so our
814eda14cbcSMatt Macy 	 * dp_space_towrite should now be zero. However, some seldom-used
815eda14cbcSMatt Macy 	 * code paths do not adhere to this (e.g. dbuf_undirty()). Shore up
816eda14cbcSMatt Macy 	 * the accounting of any dirtied space now.
817eda14cbcSMatt Macy 	 *
818eda14cbcSMatt Macy 	 * Note that, besides any dirty data from datasets, the amount of
819eda14cbcSMatt Macy 	 * dirty data in the MOS is also accounted by the pool. Therefore,
820eda14cbcSMatt Macy 	 * we want to do this cleanup after dsl_pool_sync_mos() so we don't
821eda14cbcSMatt Macy 	 * attempt to update the accounting for the same dirty data twice.
822eda14cbcSMatt Macy 	 * (i.e. at this point we only update the accounting for the space
823eda14cbcSMatt Macy 	 * that we know that we "leaked").
824eda14cbcSMatt Macy 	 */
825eda14cbcSMatt Macy 	dsl_pool_undirty_space(dp, dp->dp_dirty_pertxg[txg & TXG_MASK], txg);
826eda14cbcSMatt Macy 
827eda14cbcSMatt Macy 	/*
828eda14cbcSMatt Macy 	 * If we modify a dataset in the same txg that we want to destroy it,
829eda14cbcSMatt Macy 	 * its dsl_dir's dd_dbuf will be dirty, and thus have a hold on it.
830eda14cbcSMatt Macy 	 * dsl_dir_destroy_check() will fail if there are unexpected holds.
831eda14cbcSMatt Macy 	 * Therefore, we want to sync the MOS (thus syncing the dd_dbuf
832eda14cbcSMatt Macy 	 * and clearing the hold on it) before we process the sync_tasks.
833eda14cbcSMatt Macy 	 * The MOS data dirtied by the sync_tasks will be synced on the next
834eda14cbcSMatt Macy 	 * pass.
835eda14cbcSMatt Macy 	 */
836eda14cbcSMatt Macy 	if (!txg_list_empty(&dp->dp_sync_tasks, txg)) {
837eda14cbcSMatt Macy 		dsl_sync_task_t *dst;
838eda14cbcSMatt Macy 		/*
839eda14cbcSMatt Macy 		 * No more sync tasks should have been added while we
840eda14cbcSMatt Macy 		 * were syncing.
841eda14cbcSMatt Macy 		 */
842eda14cbcSMatt Macy 		ASSERT3U(spa_sync_pass(dp->dp_spa), ==, 1);
843eda14cbcSMatt Macy 		while ((dst = txg_list_remove(&dp->dp_sync_tasks, txg)) != NULL)
844eda14cbcSMatt Macy 			dsl_sync_task_sync(dst, tx);
845eda14cbcSMatt Macy 	}
846eda14cbcSMatt Macy 
847eda14cbcSMatt Macy 	dmu_tx_commit(tx);
848eda14cbcSMatt Macy 
849eda14cbcSMatt Macy 	DTRACE_PROBE2(dsl_pool_sync__done, dsl_pool_t *dp, dp, uint64_t, txg);
850eda14cbcSMatt Macy }
851eda14cbcSMatt Macy 
852eda14cbcSMatt Macy void
dsl_pool_sync_done(dsl_pool_t * dp,uint64_t txg)853eda14cbcSMatt Macy dsl_pool_sync_done(dsl_pool_t *dp, uint64_t txg)
854eda14cbcSMatt Macy {
855eda14cbcSMatt Macy 	zilog_t *zilog;
856eda14cbcSMatt Macy 
857eda14cbcSMatt Macy 	while ((zilog = txg_list_head(&dp->dp_dirty_zilogs, txg))) {
858eda14cbcSMatt Macy 		dsl_dataset_t *ds = dmu_objset_ds(zilog->zl_os);
859eda14cbcSMatt Macy 		/*
860eda14cbcSMatt Macy 		 * We don't remove the zilog from the dp_dirty_zilogs
861eda14cbcSMatt Macy 		 * list until after we've cleaned it. This ensures that
862eda14cbcSMatt Macy 		 * callers of zilog_is_dirty() receive an accurate
863eda14cbcSMatt Macy 		 * answer when they are racing with the spa sync thread.
864eda14cbcSMatt Macy 		 */
865eda14cbcSMatt Macy 		zil_clean(zilog, txg);
866eda14cbcSMatt Macy 		(void) txg_list_remove_this(&dp->dp_dirty_zilogs, zilog, txg);
867eda14cbcSMatt Macy 		ASSERT(!dmu_objset_is_dirty(zilog->zl_os, txg));
868eda14cbcSMatt Macy 		dmu_buf_rele(ds->ds_dbuf, zilog);
869eda14cbcSMatt Macy 	}
8703f9d360cSMartin Matuska 
8713f9d360cSMartin Matuska 	dsl_pool_wrlog_clear(dp, txg);
8723f9d360cSMartin Matuska 
873eda14cbcSMatt Macy 	ASSERT(!dmu_objset_is_dirty(dp->dp_meta_objset, txg));
874eda14cbcSMatt Macy }
875eda14cbcSMatt Macy 
876eda14cbcSMatt Macy /*
877eda14cbcSMatt Macy  * TRUE if the current thread is the tx_sync_thread or if we
878eda14cbcSMatt Macy  * are being called from SPA context during pool initialization.
879eda14cbcSMatt Macy  */
880eda14cbcSMatt Macy int
dsl_pool_sync_context(dsl_pool_t * dp)881eda14cbcSMatt Macy dsl_pool_sync_context(dsl_pool_t *dp)
882eda14cbcSMatt Macy {
883eda14cbcSMatt Macy 	return (curthread == dp->dp_tx.tx_sync_thread ||
884eda14cbcSMatt Macy 	    spa_is_initializing(dp->dp_spa) ||
885eda14cbcSMatt Macy 	    taskq_member(dp->dp_sync_taskq, curthread));
886eda14cbcSMatt Macy }
887eda14cbcSMatt Macy 
888eda14cbcSMatt Macy /*
889eda14cbcSMatt Macy  * This function returns the amount of allocatable space in the pool
890eda14cbcSMatt Macy  * minus whatever space is currently reserved by ZFS for specific
891eda14cbcSMatt Macy  * purposes. Specifically:
892eda14cbcSMatt Macy  *
893eda14cbcSMatt Macy  * 1] Any reserved SLOP space
894eda14cbcSMatt Macy  * 2] Any space used by the checkpoint
895eda14cbcSMatt Macy  * 3] Any space used for deferred frees
896eda14cbcSMatt Macy  *
897eda14cbcSMatt Macy  * The latter 2 are especially important because they are needed to
898eda14cbcSMatt Macy  * rectify the SPA's and DMU's different understanding of how much space
899eda14cbcSMatt Macy  * is used. Now the DMU is aware of that extra space tracked by the SPA
900eda14cbcSMatt Macy  * without having to maintain a separate special dir (e.g similar to
901eda14cbcSMatt Macy  * $MOS, $FREEING, and $LEAKED).
902eda14cbcSMatt Macy  *
903eda14cbcSMatt Macy  * Note: By deferred frees here, we mean the frees that were deferred
904eda14cbcSMatt Macy  * in spa_sync() after sync pass 1 (spa_deferred_bpobj), and not the
905eda14cbcSMatt Macy  * segments placed in ms_defer trees during metaslab_sync_done().
906eda14cbcSMatt Macy  */
907eda14cbcSMatt Macy uint64_t
dsl_pool_adjustedsize(dsl_pool_t * dp,zfs_space_check_t slop_policy)908eda14cbcSMatt Macy dsl_pool_adjustedsize(dsl_pool_t *dp, zfs_space_check_t slop_policy)
909eda14cbcSMatt Macy {
910eda14cbcSMatt Macy 	spa_t *spa = dp->dp_spa;
911eda14cbcSMatt Macy 	uint64_t space, resv, adjustedsize;
912eda14cbcSMatt Macy 	uint64_t spa_deferred_frees =
913eda14cbcSMatt Macy 	    spa->spa_deferred_bpobj.bpo_phys->bpo_bytes;
914eda14cbcSMatt Macy 
915eda14cbcSMatt Macy 	space = spa_get_dspace(spa)
916eda14cbcSMatt Macy 	    - spa_get_checkpoint_space(spa) - spa_deferred_frees;
917eda14cbcSMatt Macy 	resv = spa_get_slop_space(spa);
918eda14cbcSMatt Macy 
919eda14cbcSMatt Macy 	switch (slop_policy) {
920eda14cbcSMatt Macy 	case ZFS_SPACE_CHECK_NORMAL:
921eda14cbcSMatt Macy 		break;
922eda14cbcSMatt Macy 	case ZFS_SPACE_CHECK_RESERVED:
923eda14cbcSMatt Macy 		resv >>= 1;
924eda14cbcSMatt Macy 		break;
925eda14cbcSMatt Macy 	case ZFS_SPACE_CHECK_EXTRA_RESERVED:
926eda14cbcSMatt Macy 		resv >>= 2;
927eda14cbcSMatt Macy 		break;
928eda14cbcSMatt Macy 	case ZFS_SPACE_CHECK_NONE:
929eda14cbcSMatt Macy 		resv = 0;
930eda14cbcSMatt Macy 		break;
931eda14cbcSMatt Macy 	default:
932eda14cbcSMatt Macy 		panic("invalid slop policy value: %d", slop_policy);
933eda14cbcSMatt Macy 		break;
934eda14cbcSMatt Macy 	}
935eda14cbcSMatt Macy 	adjustedsize = (space >= resv) ? (space - resv) : 0;
936eda14cbcSMatt Macy 
937eda14cbcSMatt Macy 	return (adjustedsize);
938eda14cbcSMatt Macy }
939eda14cbcSMatt Macy 
940eda14cbcSMatt Macy uint64_t
dsl_pool_unreserved_space(dsl_pool_t * dp,zfs_space_check_t slop_policy)941eda14cbcSMatt Macy dsl_pool_unreserved_space(dsl_pool_t *dp, zfs_space_check_t slop_policy)
942eda14cbcSMatt Macy {
943eda14cbcSMatt Macy 	uint64_t poolsize = dsl_pool_adjustedsize(dp, slop_policy);
944eda14cbcSMatt Macy 	uint64_t deferred =
945eda14cbcSMatt Macy 	    metaslab_class_get_deferred(spa_normal_class(dp->dp_spa));
946eda14cbcSMatt Macy 	uint64_t quota = (poolsize >= deferred) ? (poolsize - deferred) : 0;
947eda14cbcSMatt Macy 	return (quota);
948eda14cbcSMatt Macy }
949eda14cbcSMatt Macy 
950c03c5b1cSMartin Matuska uint64_t
dsl_pool_deferred_space(dsl_pool_t * dp)951c03c5b1cSMartin Matuska dsl_pool_deferred_space(dsl_pool_t *dp)
952c03c5b1cSMartin Matuska {
953c03c5b1cSMartin Matuska 	return (metaslab_class_get_deferred(spa_normal_class(dp->dp_spa)));
954c03c5b1cSMartin Matuska }
955c03c5b1cSMartin Matuska 
956eda14cbcSMatt Macy boolean_t
dsl_pool_need_dirty_delay(dsl_pool_t * dp)957eda14cbcSMatt Macy dsl_pool_need_dirty_delay(dsl_pool_t *dp)
958eda14cbcSMatt Macy {
959eda14cbcSMatt Macy 	uint64_t delay_min_bytes =
960eda14cbcSMatt Macy 	    zfs_dirty_data_max * zfs_delay_min_dirty_percent / 100;
961eda14cbcSMatt Macy 
96222b267e8SMartin Matuska 	/*
96322b267e8SMartin Matuska 	 * We are not taking the dp_lock here and few other places, since torn
96422b267e8SMartin Matuska 	 * reads are unlikely: on 64-bit systems due to register size and on
96522b267e8SMartin Matuska 	 * 32-bit due to memory constraints.  Pool-wide locks in hot path may
96622b267e8SMartin Matuska 	 * be too expensive, while we do not need a precise result here.
96722b267e8SMartin Matuska 	 */
96822b267e8SMartin Matuska 	return (dp->dp_dirty_total > delay_min_bytes);
969eda14cbcSMatt Macy }
970eda14cbcSMatt Macy 
9717cd22ac4SMartin Matuska static boolean_t
dsl_pool_need_dirty_sync(dsl_pool_t * dp,uint64_t txg)9727cd22ac4SMartin Matuska dsl_pool_need_dirty_sync(dsl_pool_t *dp, uint64_t txg)
9737cd22ac4SMartin Matuska {
9747cd22ac4SMartin Matuska 	uint64_t dirty_min_bytes =
9757cd22ac4SMartin Matuska 	    zfs_dirty_data_max * zfs_dirty_data_sync_percent / 100;
9767cd22ac4SMartin Matuska 	uint64_t dirty = dp->dp_dirty_pertxg[txg & TXG_MASK];
9777cd22ac4SMartin Matuska 
9787cd22ac4SMartin Matuska 	return (dirty > dirty_min_bytes);
9797cd22ac4SMartin Matuska }
9807cd22ac4SMartin Matuska 
981eda14cbcSMatt Macy void
dsl_pool_dirty_space(dsl_pool_t * dp,int64_t space,dmu_tx_t * tx)982eda14cbcSMatt Macy dsl_pool_dirty_space(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx)
983eda14cbcSMatt Macy {
984eda14cbcSMatt Macy 	if (space > 0) {
985eda14cbcSMatt Macy 		mutex_enter(&dp->dp_lock);
986eda14cbcSMatt Macy 		dp->dp_dirty_pertxg[tx->tx_txg & TXG_MASK] += space;
987eda14cbcSMatt Macy 		dsl_pool_dirty_delta(dp, space);
9887cd22ac4SMartin Matuska 		boolean_t needsync = !dmu_tx_is_syncing(tx) &&
9897cd22ac4SMartin Matuska 		    dsl_pool_need_dirty_sync(dp, tx->tx_txg);
990eda14cbcSMatt Macy 		mutex_exit(&dp->dp_lock);
9917cd22ac4SMartin Matuska 
9927cd22ac4SMartin Matuska 		if (needsync)
9937cd22ac4SMartin Matuska 			txg_kick(dp, tx->tx_txg);
994eda14cbcSMatt Macy 	}
995eda14cbcSMatt Macy }
996eda14cbcSMatt Macy 
997eda14cbcSMatt Macy void
dsl_pool_undirty_space(dsl_pool_t * dp,int64_t space,uint64_t txg)998eda14cbcSMatt Macy dsl_pool_undirty_space(dsl_pool_t *dp, int64_t space, uint64_t txg)
999eda14cbcSMatt Macy {
1000eda14cbcSMatt Macy 	ASSERT3S(space, >=, 0);
1001eda14cbcSMatt Macy 	if (space == 0)
1002eda14cbcSMatt Macy 		return;
1003eda14cbcSMatt Macy 
1004eda14cbcSMatt Macy 	mutex_enter(&dp->dp_lock);
1005eda14cbcSMatt Macy 	if (dp->dp_dirty_pertxg[txg & TXG_MASK] < space) {
1006eda14cbcSMatt Macy 		/* XXX writing something we didn't dirty? */
1007eda14cbcSMatt Macy 		space = dp->dp_dirty_pertxg[txg & TXG_MASK];
1008eda14cbcSMatt Macy 	}
1009eda14cbcSMatt Macy 	ASSERT3U(dp->dp_dirty_pertxg[txg & TXG_MASK], >=, space);
1010eda14cbcSMatt Macy 	dp->dp_dirty_pertxg[txg & TXG_MASK] -= space;
1011eda14cbcSMatt Macy 	ASSERT3U(dp->dp_dirty_total, >=, space);
1012eda14cbcSMatt Macy 	dsl_pool_dirty_delta(dp, -space);
1013eda14cbcSMatt Macy 	mutex_exit(&dp->dp_lock);
1014eda14cbcSMatt Macy }
1015eda14cbcSMatt Macy 
1016eda14cbcSMatt Macy static int
upgrade_clones_cb(dsl_pool_t * dp,dsl_dataset_t * hds,void * arg)1017eda14cbcSMatt Macy upgrade_clones_cb(dsl_pool_t *dp, dsl_dataset_t *hds, void *arg)
1018eda14cbcSMatt Macy {
1019eda14cbcSMatt Macy 	dmu_tx_t *tx = arg;
1020eda14cbcSMatt Macy 	dsl_dataset_t *ds, *prev = NULL;
1021eda14cbcSMatt Macy 	int err;
1022eda14cbcSMatt Macy 
1023eda14cbcSMatt Macy 	err = dsl_dataset_hold_obj(dp, hds->ds_object, FTAG, &ds);
1024eda14cbcSMatt Macy 	if (err)
1025eda14cbcSMatt Macy 		return (err);
1026eda14cbcSMatt Macy 
1027eda14cbcSMatt Macy 	while (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
1028eda14cbcSMatt Macy 		err = dsl_dataset_hold_obj(dp,
1029eda14cbcSMatt Macy 		    dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &prev);
1030eda14cbcSMatt Macy 		if (err) {
1031eda14cbcSMatt Macy 			dsl_dataset_rele(ds, FTAG);
1032eda14cbcSMatt Macy 			return (err);
1033eda14cbcSMatt Macy 		}
1034eda14cbcSMatt Macy 
1035eda14cbcSMatt Macy 		if (dsl_dataset_phys(prev)->ds_next_snap_obj != ds->ds_object)
1036eda14cbcSMatt Macy 			break;
1037eda14cbcSMatt Macy 		dsl_dataset_rele(ds, FTAG);
1038eda14cbcSMatt Macy 		ds = prev;
1039eda14cbcSMatt Macy 		prev = NULL;
1040eda14cbcSMatt Macy 	}
1041eda14cbcSMatt Macy 
1042eda14cbcSMatt Macy 	if (prev == NULL) {
1043eda14cbcSMatt Macy 		prev = dp->dp_origin_snap;
1044eda14cbcSMatt Macy 
1045eda14cbcSMatt Macy 		/*
1046eda14cbcSMatt Macy 		 * The $ORIGIN can't have any data, or the accounting
1047eda14cbcSMatt Macy 		 * will be wrong.
1048eda14cbcSMatt Macy 		 */
1049eda14cbcSMatt Macy 		rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
1050783d3ff6SMartin Matuska 		ASSERT0(BP_GET_LOGICAL_BIRTH(&dsl_dataset_phys(prev)->ds_bp));
1051eda14cbcSMatt Macy 		rrw_exit(&ds->ds_bp_rwlock, FTAG);
1052eda14cbcSMatt Macy 
1053eda14cbcSMatt Macy 		/* The origin doesn't get attached to itself */
1054eda14cbcSMatt Macy 		if (ds->ds_object == prev->ds_object) {
1055eda14cbcSMatt Macy 			dsl_dataset_rele(ds, FTAG);
1056eda14cbcSMatt Macy 			return (0);
1057eda14cbcSMatt Macy 		}
1058eda14cbcSMatt Macy 
1059eda14cbcSMatt Macy 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
1060eda14cbcSMatt Macy 		dsl_dataset_phys(ds)->ds_prev_snap_obj = prev->ds_object;
1061eda14cbcSMatt Macy 		dsl_dataset_phys(ds)->ds_prev_snap_txg =
1062eda14cbcSMatt Macy 		    dsl_dataset_phys(prev)->ds_creation_txg;
1063eda14cbcSMatt Macy 
1064eda14cbcSMatt Macy 		dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
1065eda14cbcSMatt Macy 		dsl_dir_phys(ds->ds_dir)->dd_origin_obj = prev->ds_object;
1066eda14cbcSMatt Macy 
1067eda14cbcSMatt Macy 		dmu_buf_will_dirty(prev->ds_dbuf, tx);
1068eda14cbcSMatt Macy 		dsl_dataset_phys(prev)->ds_num_children++;
1069eda14cbcSMatt Macy 
1070eda14cbcSMatt Macy 		if (dsl_dataset_phys(ds)->ds_next_snap_obj == 0) {
1071eda14cbcSMatt Macy 			ASSERT(ds->ds_prev == NULL);
1072eda14cbcSMatt Macy 			VERIFY0(dsl_dataset_hold_obj(dp,
1073eda14cbcSMatt Macy 			    dsl_dataset_phys(ds)->ds_prev_snap_obj,
1074eda14cbcSMatt Macy 			    ds, &ds->ds_prev));
1075eda14cbcSMatt Macy 		}
1076eda14cbcSMatt Macy 	}
1077eda14cbcSMatt Macy 
1078eda14cbcSMatt Macy 	ASSERT3U(dsl_dir_phys(ds->ds_dir)->dd_origin_obj, ==, prev->ds_object);
1079eda14cbcSMatt Macy 	ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_obj, ==, prev->ds_object);
1080eda14cbcSMatt Macy 
1081eda14cbcSMatt Macy 	if (dsl_dataset_phys(prev)->ds_next_clones_obj == 0) {
1082eda14cbcSMatt Macy 		dmu_buf_will_dirty(prev->ds_dbuf, tx);
1083eda14cbcSMatt Macy 		dsl_dataset_phys(prev)->ds_next_clones_obj =
1084eda14cbcSMatt Macy 		    zap_create(dp->dp_meta_objset,
1085eda14cbcSMatt Macy 		    DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
1086eda14cbcSMatt Macy 	}
1087eda14cbcSMatt Macy 	VERIFY0(zap_add_int(dp->dp_meta_objset,
1088eda14cbcSMatt Macy 	    dsl_dataset_phys(prev)->ds_next_clones_obj, ds->ds_object, tx));
1089eda14cbcSMatt Macy 
1090eda14cbcSMatt Macy 	dsl_dataset_rele(ds, FTAG);
1091eda14cbcSMatt Macy 	if (prev != dp->dp_origin_snap)
1092eda14cbcSMatt Macy 		dsl_dataset_rele(prev, FTAG);
1093eda14cbcSMatt Macy 	return (0);
1094eda14cbcSMatt Macy }
1095eda14cbcSMatt Macy 
1096eda14cbcSMatt Macy void
dsl_pool_upgrade_clones(dsl_pool_t * dp,dmu_tx_t * tx)1097eda14cbcSMatt Macy dsl_pool_upgrade_clones(dsl_pool_t *dp, dmu_tx_t *tx)
1098eda14cbcSMatt Macy {
1099eda14cbcSMatt Macy 	ASSERT(dmu_tx_is_syncing(tx));
1100eda14cbcSMatt Macy 	ASSERT(dp->dp_origin_snap != NULL);
1101eda14cbcSMatt Macy 
1102eda14cbcSMatt Macy 	VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj, upgrade_clones_cb,
1103eda14cbcSMatt Macy 	    tx, DS_FIND_CHILDREN | DS_FIND_SERIALIZE));
1104eda14cbcSMatt Macy }
1105eda14cbcSMatt Macy 
1106eda14cbcSMatt Macy static int
upgrade_dir_clones_cb(dsl_pool_t * dp,dsl_dataset_t * ds,void * arg)1107eda14cbcSMatt Macy upgrade_dir_clones_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
1108eda14cbcSMatt Macy {
1109eda14cbcSMatt Macy 	dmu_tx_t *tx = arg;
1110eda14cbcSMatt Macy 	objset_t *mos = dp->dp_meta_objset;
1111eda14cbcSMatt Macy 
1112eda14cbcSMatt Macy 	if (dsl_dir_phys(ds->ds_dir)->dd_origin_obj != 0) {
1113eda14cbcSMatt Macy 		dsl_dataset_t *origin;
1114eda14cbcSMatt Macy 
1115eda14cbcSMatt Macy 		VERIFY0(dsl_dataset_hold_obj(dp,
1116eda14cbcSMatt Macy 		    dsl_dir_phys(ds->ds_dir)->dd_origin_obj, FTAG, &origin));
1117eda14cbcSMatt Macy 
1118eda14cbcSMatt Macy 		if (dsl_dir_phys(origin->ds_dir)->dd_clones == 0) {
1119eda14cbcSMatt Macy 			dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx);
1120eda14cbcSMatt Macy 			dsl_dir_phys(origin->ds_dir)->dd_clones =
1121eda14cbcSMatt Macy 			    zap_create(mos, DMU_OT_DSL_CLONES, DMU_OT_NONE,
1122eda14cbcSMatt Macy 			    0, tx);
1123eda14cbcSMatt Macy 		}
1124eda14cbcSMatt Macy 
1125eda14cbcSMatt Macy 		VERIFY0(zap_add_int(dp->dp_meta_objset,
1126eda14cbcSMatt Macy 		    dsl_dir_phys(origin->ds_dir)->dd_clones,
1127eda14cbcSMatt Macy 		    ds->ds_object, tx));
1128eda14cbcSMatt Macy 
1129eda14cbcSMatt Macy 		dsl_dataset_rele(origin, FTAG);
1130eda14cbcSMatt Macy 	}
1131eda14cbcSMatt Macy 	return (0);
1132eda14cbcSMatt Macy }
1133eda14cbcSMatt Macy 
1134eda14cbcSMatt Macy void
dsl_pool_upgrade_dir_clones(dsl_pool_t * dp,dmu_tx_t * tx)1135eda14cbcSMatt Macy dsl_pool_upgrade_dir_clones(dsl_pool_t *dp, dmu_tx_t *tx)
1136eda14cbcSMatt Macy {
1137eda14cbcSMatt Macy 	uint64_t obj;
1138eda14cbcSMatt Macy 
1139eda14cbcSMatt Macy 	ASSERT(dmu_tx_is_syncing(tx));
1140eda14cbcSMatt Macy 
1141eda14cbcSMatt Macy 	(void) dsl_dir_create_sync(dp, dp->dp_root_dir, FREE_DIR_NAME, tx);
1142eda14cbcSMatt Macy 	VERIFY0(dsl_pool_open_special_dir(dp,
1143eda14cbcSMatt Macy 	    FREE_DIR_NAME, &dp->dp_free_dir));
1144eda14cbcSMatt Macy 
1145eda14cbcSMatt Macy 	/*
1146eda14cbcSMatt Macy 	 * We can't use bpobj_alloc(), because spa_version() still
1147eda14cbcSMatt Macy 	 * returns the old version, and we need a new-version bpobj with
1148eda14cbcSMatt Macy 	 * subobj support.  So call dmu_object_alloc() directly.
1149eda14cbcSMatt Macy 	 */
1150eda14cbcSMatt Macy 	obj = dmu_object_alloc(dp->dp_meta_objset, DMU_OT_BPOBJ,
1151eda14cbcSMatt Macy 	    SPA_OLD_MAXBLOCKSIZE, DMU_OT_BPOBJ_HDR, sizeof (bpobj_phys_t), tx);
1152eda14cbcSMatt Macy 	VERIFY0(zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1153eda14cbcSMatt Macy 	    DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj, tx));
1154eda14cbcSMatt Macy 	VERIFY0(bpobj_open(&dp->dp_free_bpobj, dp->dp_meta_objset, obj));
1155eda14cbcSMatt Macy 
1156eda14cbcSMatt Macy 	VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
1157eda14cbcSMatt Macy 	    upgrade_dir_clones_cb, tx, DS_FIND_CHILDREN | DS_FIND_SERIALIZE));
1158eda14cbcSMatt Macy }
1159eda14cbcSMatt Macy 
1160eda14cbcSMatt Macy void
dsl_pool_create_origin(dsl_pool_t * dp,dmu_tx_t * tx)1161eda14cbcSMatt Macy dsl_pool_create_origin(dsl_pool_t *dp, dmu_tx_t *tx)
1162eda14cbcSMatt Macy {
1163eda14cbcSMatt Macy 	uint64_t dsobj;
1164eda14cbcSMatt Macy 	dsl_dataset_t *ds;
1165eda14cbcSMatt Macy 
1166eda14cbcSMatt Macy 	ASSERT(dmu_tx_is_syncing(tx));
1167eda14cbcSMatt Macy 	ASSERT(dp->dp_origin_snap == NULL);
1168eda14cbcSMatt Macy 	ASSERT(rrw_held(&dp->dp_config_rwlock, RW_WRITER));
1169eda14cbcSMatt Macy 
1170eda14cbcSMatt Macy 	/* create the origin dir, ds, & snap-ds */
1171eda14cbcSMatt Macy 	dsobj = dsl_dataset_create_sync(dp->dp_root_dir, ORIGIN_DIR_NAME,
1172eda14cbcSMatt Macy 	    NULL, 0, kcred, NULL, tx);
1173eda14cbcSMatt Macy 	VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
1174eda14cbcSMatt Macy 	dsl_dataset_snapshot_sync_impl(ds, ORIGIN_DIR_NAME, tx);
1175eda14cbcSMatt Macy 	VERIFY0(dsl_dataset_hold_obj(dp, dsl_dataset_phys(ds)->ds_prev_snap_obj,
1176eda14cbcSMatt Macy 	    dp, &dp->dp_origin_snap));
1177eda14cbcSMatt Macy 	dsl_dataset_rele(ds, FTAG);
1178eda14cbcSMatt Macy }
1179eda14cbcSMatt Macy 
1180eda14cbcSMatt Macy taskq_t *
dsl_pool_zrele_taskq(dsl_pool_t * dp)1181eda14cbcSMatt Macy dsl_pool_zrele_taskq(dsl_pool_t *dp)
1182eda14cbcSMatt Macy {
1183eda14cbcSMatt Macy 	return (dp->dp_zrele_taskq);
1184eda14cbcSMatt Macy }
1185eda14cbcSMatt Macy 
1186eda14cbcSMatt Macy taskq_t *
dsl_pool_unlinked_drain_taskq(dsl_pool_t * dp)1187eda14cbcSMatt Macy dsl_pool_unlinked_drain_taskq(dsl_pool_t *dp)
1188eda14cbcSMatt Macy {
1189eda14cbcSMatt Macy 	return (dp->dp_unlinked_drain_taskq);
1190eda14cbcSMatt Macy }
1191eda14cbcSMatt Macy 
1192eda14cbcSMatt Macy /*
1193eda14cbcSMatt Macy  * Walk through the pool-wide zap object of temporary snapshot user holds
1194eda14cbcSMatt Macy  * and release them.
1195eda14cbcSMatt Macy  */
1196eda14cbcSMatt Macy void
dsl_pool_clean_tmp_userrefs(dsl_pool_t * dp)1197eda14cbcSMatt Macy dsl_pool_clean_tmp_userrefs(dsl_pool_t *dp)
1198eda14cbcSMatt Macy {
1199eda14cbcSMatt Macy 	zap_attribute_t za;
1200eda14cbcSMatt Macy 	zap_cursor_t zc;
1201eda14cbcSMatt Macy 	objset_t *mos = dp->dp_meta_objset;
1202eda14cbcSMatt Macy 	uint64_t zapobj = dp->dp_tmp_userrefs_obj;
1203eda14cbcSMatt Macy 	nvlist_t *holds;
1204eda14cbcSMatt Macy 
1205eda14cbcSMatt Macy 	if (zapobj == 0)
1206eda14cbcSMatt Macy 		return;
1207eda14cbcSMatt Macy 	ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
1208eda14cbcSMatt Macy 
1209eda14cbcSMatt Macy 	holds = fnvlist_alloc();
1210eda14cbcSMatt Macy 
1211eda14cbcSMatt Macy 	for (zap_cursor_init(&zc, mos, zapobj);
1212eda14cbcSMatt Macy 	    zap_cursor_retrieve(&zc, &za) == 0;
1213eda14cbcSMatt Macy 	    zap_cursor_advance(&zc)) {
1214eda14cbcSMatt Macy 		char *htag;
1215eda14cbcSMatt Macy 		nvlist_t *tags;
1216eda14cbcSMatt Macy 
1217eda14cbcSMatt Macy 		htag = strchr(za.za_name, '-');
1218eda14cbcSMatt Macy 		*htag = '\0';
1219eda14cbcSMatt Macy 		++htag;
1220eda14cbcSMatt Macy 		if (nvlist_lookup_nvlist(holds, za.za_name, &tags) != 0) {
1221eda14cbcSMatt Macy 			tags = fnvlist_alloc();
1222eda14cbcSMatt Macy 			fnvlist_add_boolean(tags, htag);
1223eda14cbcSMatt Macy 			fnvlist_add_nvlist(holds, za.za_name, tags);
1224eda14cbcSMatt Macy 			fnvlist_free(tags);
1225eda14cbcSMatt Macy 		} else {
1226eda14cbcSMatt Macy 			fnvlist_add_boolean(tags, htag);
1227eda14cbcSMatt Macy 		}
1228eda14cbcSMatt Macy 	}
1229eda14cbcSMatt Macy 	dsl_dataset_user_release_tmp(dp, holds);
1230eda14cbcSMatt Macy 	fnvlist_free(holds);
1231eda14cbcSMatt Macy 	zap_cursor_fini(&zc);
1232eda14cbcSMatt Macy }
1233eda14cbcSMatt Macy 
1234eda14cbcSMatt Macy /*
1235eda14cbcSMatt Macy  * Create the pool-wide zap object for storing temporary snapshot holds.
1236eda14cbcSMatt Macy  */
1237eda14cbcSMatt Macy static void
dsl_pool_user_hold_create_obj(dsl_pool_t * dp,dmu_tx_t * tx)1238eda14cbcSMatt Macy dsl_pool_user_hold_create_obj(dsl_pool_t *dp, dmu_tx_t *tx)
1239eda14cbcSMatt Macy {
1240eda14cbcSMatt Macy 	objset_t *mos = dp->dp_meta_objset;
1241eda14cbcSMatt Macy 
1242eda14cbcSMatt Macy 	ASSERT(dp->dp_tmp_userrefs_obj == 0);
1243eda14cbcSMatt Macy 	ASSERT(dmu_tx_is_syncing(tx));
1244eda14cbcSMatt Macy 
1245eda14cbcSMatt Macy 	dp->dp_tmp_userrefs_obj = zap_create_link(mos, DMU_OT_USERREFS,
1246eda14cbcSMatt Macy 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_TMP_USERREFS, tx);
1247eda14cbcSMatt Macy }
1248eda14cbcSMatt Macy 
1249eda14cbcSMatt Macy static int
dsl_pool_user_hold_rele_impl(dsl_pool_t * dp,uint64_t dsobj,const char * tag,uint64_t now,dmu_tx_t * tx,boolean_t holding)1250eda14cbcSMatt Macy dsl_pool_user_hold_rele_impl(dsl_pool_t *dp, uint64_t dsobj,
1251eda14cbcSMatt Macy     const char *tag, uint64_t now, dmu_tx_t *tx, boolean_t holding)
1252eda14cbcSMatt Macy {
1253eda14cbcSMatt Macy 	objset_t *mos = dp->dp_meta_objset;
1254eda14cbcSMatt Macy 	uint64_t zapobj = dp->dp_tmp_userrefs_obj;
1255eda14cbcSMatt Macy 	char *name;
1256eda14cbcSMatt Macy 	int error;
1257eda14cbcSMatt Macy 
1258eda14cbcSMatt Macy 	ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
1259eda14cbcSMatt Macy 	ASSERT(dmu_tx_is_syncing(tx));
1260eda14cbcSMatt Macy 
1261eda14cbcSMatt Macy 	/*
1262eda14cbcSMatt Macy 	 * If the pool was created prior to SPA_VERSION_USERREFS, the
1263eda14cbcSMatt Macy 	 * zap object for temporary holds might not exist yet.
1264eda14cbcSMatt Macy 	 */
1265eda14cbcSMatt Macy 	if (zapobj == 0) {
1266eda14cbcSMatt Macy 		if (holding) {
1267eda14cbcSMatt Macy 			dsl_pool_user_hold_create_obj(dp, tx);
1268eda14cbcSMatt Macy 			zapobj = dp->dp_tmp_userrefs_obj;
1269eda14cbcSMatt Macy 		} else {
1270eda14cbcSMatt Macy 			return (SET_ERROR(ENOENT));
1271eda14cbcSMatt Macy 		}
1272eda14cbcSMatt Macy 	}
1273eda14cbcSMatt Macy 
1274eda14cbcSMatt Macy 	name = kmem_asprintf("%llx-%s", (u_longlong_t)dsobj, tag);
1275eda14cbcSMatt Macy 	if (holding)
1276eda14cbcSMatt Macy 		error = zap_add(mos, zapobj, name, 8, 1, &now, tx);
1277eda14cbcSMatt Macy 	else
1278eda14cbcSMatt Macy 		error = zap_remove(mos, zapobj, name, tx);
1279eda14cbcSMatt Macy 	kmem_strfree(name);
1280eda14cbcSMatt Macy 
1281eda14cbcSMatt Macy 	return (error);
1282eda14cbcSMatt Macy }
1283eda14cbcSMatt Macy 
1284eda14cbcSMatt Macy /*
1285eda14cbcSMatt Macy  * Add a temporary hold for the given dataset object and tag.
1286eda14cbcSMatt Macy  */
1287eda14cbcSMatt Macy int
dsl_pool_user_hold(dsl_pool_t * dp,uint64_t dsobj,const char * tag,uint64_t now,dmu_tx_t * tx)1288eda14cbcSMatt Macy dsl_pool_user_hold(dsl_pool_t *dp, uint64_t dsobj, const char *tag,
1289eda14cbcSMatt Macy     uint64_t now, dmu_tx_t *tx)
1290eda14cbcSMatt Macy {
1291eda14cbcSMatt Macy 	return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, now, tx, B_TRUE));
1292eda14cbcSMatt Macy }
1293eda14cbcSMatt Macy 
1294eda14cbcSMatt Macy /*
1295eda14cbcSMatt Macy  * Release a temporary hold for the given dataset object and tag.
1296eda14cbcSMatt Macy  */
1297eda14cbcSMatt Macy int
dsl_pool_user_release(dsl_pool_t * dp,uint64_t dsobj,const char * tag,dmu_tx_t * tx)1298eda14cbcSMatt Macy dsl_pool_user_release(dsl_pool_t *dp, uint64_t dsobj, const char *tag,
1299eda14cbcSMatt Macy     dmu_tx_t *tx)
1300eda14cbcSMatt Macy {
1301eda14cbcSMatt Macy 	return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, 0,
1302eda14cbcSMatt Macy 	    tx, B_FALSE));
1303eda14cbcSMatt Macy }
1304eda14cbcSMatt Macy 
1305eda14cbcSMatt Macy /*
1306eda14cbcSMatt Macy  * DSL Pool Configuration Lock
1307eda14cbcSMatt Macy  *
1308eda14cbcSMatt Macy  * The dp_config_rwlock protects against changes to DSL state (e.g. dataset
1309eda14cbcSMatt Macy  * creation / destruction / rename / property setting).  It must be held for
1310eda14cbcSMatt Macy  * read to hold a dataset or dsl_dir.  I.e. you must call
1311eda14cbcSMatt Macy  * dsl_pool_config_enter() or dsl_pool_hold() before calling
1312eda14cbcSMatt Macy  * dsl_{dataset,dir}_hold{_obj}.  In most circumstances, the dp_config_rwlock
1313eda14cbcSMatt Macy  * must be held continuously until all datasets and dsl_dirs are released.
1314eda14cbcSMatt Macy  *
1315eda14cbcSMatt Macy  * The only exception to this rule is that if a "long hold" is placed on
1316eda14cbcSMatt Macy  * a dataset, then the dp_config_rwlock may be dropped while the dataset
1317eda14cbcSMatt Macy  * is still held.  The long hold will prevent the dataset from being
1318eda14cbcSMatt Macy  * destroyed -- the destroy will fail with EBUSY.  A long hold can be
1319eda14cbcSMatt Macy  * obtained by calling dsl_dataset_long_hold(), or by "owning" a dataset
1320eda14cbcSMatt Macy  * (by calling dsl_{dataset,objset}_{try}own{_obj}).
1321eda14cbcSMatt Macy  *
1322eda14cbcSMatt Macy  * Legitimate long-holders (including owners) should be long-running, cancelable
1323eda14cbcSMatt Macy  * tasks that should cause "zfs destroy" to fail.  This includes DMU
1324eda14cbcSMatt Macy  * consumers (i.e. a ZPL filesystem being mounted or ZVOL being open),
1325eda14cbcSMatt Macy  * "zfs send", and "zfs diff".  There are several other long-holders whose
1326eda14cbcSMatt Macy  * uses are suboptimal (e.g. "zfs promote", and zil_suspend()).
1327eda14cbcSMatt Macy  *
1328eda14cbcSMatt Macy  * The usual formula for long-holding would be:
1329eda14cbcSMatt Macy  * dsl_pool_hold()
1330eda14cbcSMatt Macy  * dsl_dataset_hold()
1331eda14cbcSMatt Macy  * ... perform checks ...
1332eda14cbcSMatt Macy  * dsl_dataset_long_hold()
1333eda14cbcSMatt Macy  * dsl_pool_rele()
1334eda14cbcSMatt Macy  * ... perform long-running task ...
1335eda14cbcSMatt Macy  * dsl_dataset_long_rele()
1336eda14cbcSMatt Macy  * dsl_dataset_rele()
1337eda14cbcSMatt Macy  *
1338eda14cbcSMatt Macy  * Note that when the long hold is released, the dataset is still held but
1339eda14cbcSMatt Macy  * the pool is not held.  The dataset may change arbitrarily during this time
1340eda14cbcSMatt Macy  * (e.g. it could be destroyed).  Therefore you shouldn't do anything to the
1341eda14cbcSMatt Macy  * dataset except release it.
1342eda14cbcSMatt Macy  *
13437877fdebSMatt Macy  * Operations generally fall somewhere into the following taxonomy:
13447877fdebSMatt Macy  *
13457877fdebSMatt Macy  *                              Read-Only             Modifying
13467877fdebSMatt Macy  *
13477877fdebSMatt Macy  *    Dataset Layer / MOS        zfs get             zfs destroy
13487877fdebSMatt Macy  *
13497877fdebSMatt Macy  *     Individual Dataset         read()                write()
13507877fdebSMatt Macy  *
13517877fdebSMatt Macy  *
13527877fdebSMatt Macy  * Dataset Layer Operations
1353eda14cbcSMatt Macy  *
1354eda14cbcSMatt Macy  * Modifying operations should generally use dsl_sync_task().  The synctask
1355eda14cbcSMatt Macy  * infrastructure enforces proper locking strategy with respect to the
1356eda14cbcSMatt Macy  * dp_config_rwlock.  See the comment above dsl_sync_task() for details.
1357eda14cbcSMatt Macy  *
1358eda14cbcSMatt Macy  * Read-only operations will manually hold the pool, then the dataset, obtain
1359eda14cbcSMatt Macy  * information from the dataset, then release the pool and dataset.
1360eda14cbcSMatt Macy  * dmu_objset_{hold,rele}() are convenience routines that also do the pool
1361eda14cbcSMatt Macy  * hold/rele.
13627877fdebSMatt Macy  *
13637877fdebSMatt Macy  *
13647877fdebSMatt Macy  * Operations On Individual Datasets
13657877fdebSMatt Macy  *
13667877fdebSMatt Macy  * Objects _within_ an objset should only be modified by the current 'owner'
13677877fdebSMatt Macy  * of the objset to prevent incorrect concurrent modification. Thus, use
13687877fdebSMatt Macy  * {dmu_objset,dsl_dataset}_own to mark some entity as the current owner,
13697877fdebSMatt Macy  * and fail with EBUSY if there is already an owner. The owner can then
13707877fdebSMatt Macy  * implement its own locking strategy, independent of the dataset layer's
13717877fdebSMatt Macy  * locking infrastructure.
13727877fdebSMatt Macy  * (E.g., the ZPL has its own set of locks to control concurrency. A regular
13737877fdebSMatt Macy  *  vnop will not reach into the dataset layer).
13747877fdebSMatt Macy  *
13757877fdebSMatt Macy  * Ideally, objects would also only be read by the objset’s owner, so that we
13767877fdebSMatt Macy  * don’t observe state mid-modification.
13777877fdebSMatt Macy  * (E.g. the ZPL is creating a new object and linking it into a directory; if
13787877fdebSMatt Macy  * you don’t coordinate with the ZPL to hold ZPL-level locks, you could see an
13797877fdebSMatt Macy  * intermediate state.  The ioctl level violates this but in pretty benign
13807877fdebSMatt Macy  * ways, e.g. reading the zpl props object.)
1381eda14cbcSMatt Macy  */
1382eda14cbcSMatt Macy 
1383eda14cbcSMatt Macy int
dsl_pool_hold(const char * name,const void * tag,dsl_pool_t ** dp)1384a0b956f5SMartin Matuska dsl_pool_hold(const char *name, const void *tag, dsl_pool_t **dp)
1385eda14cbcSMatt Macy {
1386eda14cbcSMatt Macy 	spa_t *spa;
1387eda14cbcSMatt Macy 	int error;
1388eda14cbcSMatt Macy 
1389eda14cbcSMatt Macy 	error = spa_open(name, &spa, tag);
1390eda14cbcSMatt Macy 	if (error == 0) {
1391eda14cbcSMatt Macy 		*dp = spa_get_dsl(spa);
1392eda14cbcSMatt Macy 		dsl_pool_config_enter(*dp, tag);
1393eda14cbcSMatt Macy 	}
1394eda14cbcSMatt Macy 	return (error);
1395eda14cbcSMatt Macy }
1396eda14cbcSMatt Macy 
1397eda14cbcSMatt Macy void
dsl_pool_rele(dsl_pool_t * dp,const void * tag)1398a0b956f5SMartin Matuska dsl_pool_rele(dsl_pool_t *dp, const void *tag)
1399eda14cbcSMatt Macy {
1400eda14cbcSMatt Macy 	dsl_pool_config_exit(dp, tag);
1401eda14cbcSMatt Macy 	spa_close(dp->dp_spa, tag);
1402eda14cbcSMatt Macy }
1403eda14cbcSMatt Macy 
1404eda14cbcSMatt Macy void
dsl_pool_config_enter(dsl_pool_t * dp,const void * tag)1405a0b956f5SMartin Matuska dsl_pool_config_enter(dsl_pool_t *dp, const void *tag)
1406eda14cbcSMatt Macy {
1407eda14cbcSMatt Macy 	/*
1408eda14cbcSMatt Macy 	 * We use a "reentrant" reader-writer lock, but not reentrantly.
1409eda14cbcSMatt Macy 	 *
1410eda14cbcSMatt Macy 	 * The rrwlock can (with the track_all flag) track all reading threads,
1411eda14cbcSMatt Macy 	 * which is very useful for debugging which code path failed to release
1412eda14cbcSMatt Macy 	 * the lock, and for verifying that the *current* thread does hold
1413eda14cbcSMatt Macy 	 * the lock.
1414eda14cbcSMatt Macy 	 *
1415eda14cbcSMatt Macy 	 * (Unlike a rwlock, which knows that N threads hold it for
1416eda14cbcSMatt Macy 	 * read, but not *which* threads, so rw_held(RW_READER) returns TRUE
1417eda14cbcSMatt Macy 	 * if any thread holds it for read, even if this thread doesn't).
1418eda14cbcSMatt Macy 	 */
1419eda14cbcSMatt Macy 	ASSERT(!rrw_held(&dp->dp_config_rwlock, RW_READER));
1420eda14cbcSMatt Macy 	rrw_enter(&dp->dp_config_rwlock, RW_READER, tag);
1421eda14cbcSMatt Macy }
1422eda14cbcSMatt Macy 
1423eda14cbcSMatt Macy void
dsl_pool_config_enter_prio(dsl_pool_t * dp,const void * tag)1424a0b956f5SMartin Matuska dsl_pool_config_enter_prio(dsl_pool_t *dp, const void *tag)
1425eda14cbcSMatt Macy {
1426eda14cbcSMatt Macy 	ASSERT(!rrw_held(&dp->dp_config_rwlock, RW_READER));
1427eda14cbcSMatt Macy 	rrw_enter_read_prio(&dp->dp_config_rwlock, tag);
1428eda14cbcSMatt Macy }
1429eda14cbcSMatt Macy 
1430eda14cbcSMatt Macy void
dsl_pool_config_exit(dsl_pool_t * dp,const void * tag)1431a0b956f5SMartin Matuska dsl_pool_config_exit(dsl_pool_t *dp, const void *tag)
1432eda14cbcSMatt Macy {
1433eda14cbcSMatt Macy 	rrw_exit(&dp->dp_config_rwlock, tag);
1434eda14cbcSMatt Macy }
1435eda14cbcSMatt Macy 
1436eda14cbcSMatt Macy boolean_t
dsl_pool_config_held(dsl_pool_t * dp)1437eda14cbcSMatt Macy dsl_pool_config_held(dsl_pool_t *dp)
1438eda14cbcSMatt Macy {
1439eda14cbcSMatt Macy 	return (RRW_LOCK_HELD(&dp->dp_config_rwlock));
1440eda14cbcSMatt Macy }
1441eda14cbcSMatt Macy 
1442eda14cbcSMatt Macy boolean_t
dsl_pool_config_held_writer(dsl_pool_t * dp)1443eda14cbcSMatt Macy dsl_pool_config_held_writer(dsl_pool_t *dp)
1444eda14cbcSMatt Macy {
1445eda14cbcSMatt Macy 	return (RRW_WRITE_HELD(&dp->dp_config_rwlock));
1446eda14cbcSMatt Macy }
1447eda14cbcSMatt Macy 
1448eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_pool_config_enter);
1449eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_pool_config_exit);
1450eda14cbcSMatt Macy 
1451eda14cbcSMatt Macy /* zfs_dirty_data_max_percent only applied at module load in arc_init(). */
1452be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs, zfs_, dirty_data_max_percent, UINT, ZMOD_RD,
1453eda14cbcSMatt Macy 	"Max percent of RAM allowed to be dirty");
1454eda14cbcSMatt Macy 
1455eda14cbcSMatt Macy /* zfs_dirty_data_max_max_percent only applied at module load in arc_init(). */
1456be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs, zfs_, dirty_data_max_max_percent, UINT, ZMOD_RD,
1457eda14cbcSMatt Macy 	"zfs_dirty_data_max upper bound as % of RAM");
1458eda14cbcSMatt Macy 
1459be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs, zfs_, delay_min_dirty_percent, UINT, ZMOD_RW,
1460eda14cbcSMatt Macy 	"Transaction delay threshold");
1461eda14cbcSMatt Macy 
1462dbd5678dSMartin Matuska ZFS_MODULE_PARAM(zfs, zfs_, dirty_data_max, U64, ZMOD_RW,
1463eda14cbcSMatt Macy 	"Determines the dirty space limit");
1464eda14cbcSMatt Macy 
1465dbd5678dSMartin Matuska ZFS_MODULE_PARAM(zfs, zfs_, wrlog_data_max, U64, ZMOD_RW,
14663f9d360cSMartin Matuska 	"The size limit of write-transaction zil log data");
14673f9d360cSMartin Matuska 
1468eda14cbcSMatt Macy /* zfs_dirty_data_max_max only applied at module load in arc_init(). */
1469dbd5678dSMartin Matuska ZFS_MODULE_PARAM(zfs, zfs_, dirty_data_max_max, U64, ZMOD_RD,
1470eda14cbcSMatt Macy 	"zfs_dirty_data_max upper bound in bytes");
1471eda14cbcSMatt Macy 
1472be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs, zfs_, dirty_data_sync_percent, UINT, ZMOD_RW,
1473eda14cbcSMatt Macy 	"Dirty data txg sync threshold as a percentage of zfs_dirty_data_max");
1474eda14cbcSMatt Macy 
1475dbd5678dSMartin Matuska ZFS_MODULE_PARAM(zfs, zfs_, delay_scale, U64, ZMOD_RW,
1476eda14cbcSMatt Macy 	"How quickly delay approaches infinity");
1477eda14cbcSMatt Macy 
1478eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs_zil, zfs_zil_, clean_taskq_nthr_pct, INT, ZMOD_RW,
1479eda14cbcSMatt Macy 	"Max percent of CPUs that are used per dp_sync_taskq");
1480eda14cbcSMatt Macy 
1481eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs_zil, zfs_zil_, clean_taskq_minalloc, INT, ZMOD_RW,
1482eda14cbcSMatt Macy 	"Number of taskq entries that are pre-populated");
1483eda14cbcSMatt Macy 
1484eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs_zil, zfs_zil_, clean_taskq_maxalloc, INT, ZMOD_RW,
1485eda14cbcSMatt Macy 	"Max number of taskq entries that are cached");
1486