xref: /freebsd-src/sys/contrib/openzfs/module/zfs/dsl_pool.c (revision 3ff01b231dfa83d518854c63e7c9cd1debd1139e)
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
9eda14cbcSMatt Macy  * or http://www.opensolaris.org/os/licensing.
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  */
102eda14cbcSMatt Macy unsigned long zfs_dirty_data_max = 0;
103eda14cbcSMatt Macy unsigned long zfs_dirty_data_max_max = 0;
104eda14cbcSMatt Macy int zfs_dirty_data_max_percent = 10;
105eda14cbcSMatt Macy int zfs_dirty_data_max_max_percent = 25;
106eda14cbcSMatt Macy 
107eda14cbcSMatt Macy /*
108eda14cbcSMatt Macy  * If there's at least this much dirty data (as a percentage of
109eda14cbcSMatt Macy  * zfs_dirty_data_max), push out a txg.  This should be less than
110eda14cbcSMatt Macy  * zfs_vdev_async_write_active_min_dirty_percent.
111eda14cbcSMatt Macy  */
112eda14cbcSMatt Macy int zfs_dirty_data_sync_percent = 20;
113eda14cbcSMatt Macy 
114eda14cbcSMatt Macy /*
115eda14cbcSMatt Macy  * Once there is this amount of dirty data, the dmu_tx_delay() will kick in
116eda14cbcSMatt Macy  * and delay each transaction.
117eda14cbcSMatt Macy  * This value should be >= zfs_vdev_async_write_active_max_dirty_percent.
118eda14cbcSMatt Macy  */
119eda14cbcSMatt Macy int zfs_delay_min_dirty_percent = 60;
120eda14cbcSMatt Macy 
121eda14cbcSMatt Macy /*
122eda14cbcSMatt Macy  * This controls how quickly the delay approaches infinity.
123eda14cbcSMatt Macy  * Larger values cause it to delay more for a given amount of dirty data.
124eda14cbcSMatt Macy  * Therefore larger values will cause there to be less dirty data for a
125eda14cbcSMatt Macy  * given throughput.
126eda14cbcSMatt Macy  *
127eda14cbcSMatt Macy  * For the smoothest delay, this value should be about 1 billion divided
128eda14cbcSMatt Macy  * by the maximum number of operations per second.  This will smoothly
129eda14cbcSMatt Macy  * handle between 10x and 1/10th this number.
130eda14cbcSMatt Macy  *
131eda14cbcSMatt Macy  * Note: zfs_delay_scale * zfs_dirty_data_max must be < 2^64, due to the
132eda14cbcSMatt Macy  * multiply in dmu_tx_delay().
133eda14cbcSMatt Macy  */
134eda14cbcSMatt Macy unsigned long zfs_delay_scale = 1000 * 1000 * 1000 / 2000;
135eda14cbcSMatt Macy 
136eda14cbcSMatt Macy /*
137eda14cbcSMatt Macy  * This determines the number of threads used by the dp_sync_taskq.
138eda14cbcSMatt Macy  */
139eda14cbcSMatt Macy int zfs_sync_taskq_batch_pct = 75;
140eda14cbcSMatt Macy 
141eda14cbcSMatt Macy /*
142eda14cbcSMatt Macy  * These tunables determine the behavior of how zil_itxg_clean() is
143eda14cbcSMatt Macy  * called via zil_clean() in the context of spa_sync(). When an itxg
144eda14cbcSMatt Macy  * list needs to be cleaned, TQ_NOSLEEP will be used when dispatching.
145eda14cbcSMatt Macy  * If the dispatch fails, the call to zil_itxg_clean() will occur
146eda14cbcSMatt Macy  * synchronously in the context of spa_sync(), which can negatively
147eda14cbcSMatt Macy  * impact the performance of spa_sync() (e.g. in the case of the itxg
148eda14cbcSMatt Macy  * list having a large number of itxs that needs to be cleaned).
149eda14cbcSMatt Macy  *
150eda14cbcSMatt Macy  * Thus, these tunables can be used to manipulate the behavior of the
151eda14cbcSMatt Macy  * taskq used by zil_clean(); they determine the number of taskq entries
152eda14cbcSMatt Macy  * that are pre-populated when the taskq is first created (via the
153eda14cbcSMatt Macy  * "zfs_zil_clean_taskq_minalloc" tunable) and the maximum number of
154eda14cbcSMatt Macy  * taskq entries that are cached after an on-demand allocation (via the
155eda14cbcSMatt Macy  * "zfs_zil_clean_taskq_maxalloc").
156eda14cbcSMatt Macy  *
157eda14cbcSMatt Macy  * The idea being, we want to try reasonably hard to ensure there will
158eda14cbcSMatt Macy  * already be a taskq entry pre-allocated by the time that it is needed
159eda14cbcSMatt Macy  * by zil_clean(). This way, we can avoid the possibility of an
160eda14cbcSMatt Macy  * on-demand allocation of a new taskq entry from failing, which would
161eda14cbcSMatt Macy  * result in zil_itxg_clean() being called synchronously from zil_clean()
162eda14cbcSMatt Macy  * (which can adversely affect performance of spa_sync()).
163eda14cbcSMatt Macy  *
164eda14cbcSMatt Macy  * Additionally, the number of threads used by the taskq can be
165eda14cbcSMatt Macy  * configured via the "zfs_zil_clean_taskq_nthr_pct" tunable.
166eda14cbcSMatt Macy  */
167eda14cbcSMatt Macy int zfs_zil_clean_taskq_nthr_pct = 100;
168eda14cbcSMatt Macy int zfs_zil_clean_taskq_minalloc = 1024;
169eda14cbcSMatt Macy int zfs_zil_clean_taskq_maxalloc = 1024 * 1024;
170eda14cbcSMatt Macy 
171eda14cbcSMatt Macy int
172eda14cbcSMatt Macy dsl_pool_open_special_dir(dsl_pool_t *dp, const char *name, dsl_dir_t **ddp)
173eda14cbcSMatt Macy {
174eda14cbcSMatt Macy 	uint64_t obj;
175eda14cbcSMatt Macy 	int err;
176eda14cbcSMatt Macy 
177eda14cbcSMatt Macy 	err = zap_lookup(dp->dp_meta_objset,
178eda14cbcSMatt Macy 	    dsl_dir_phys(dp->dp_root_dir)->dd_child_dir_zapobj,
179eda14cbcSMatt Macy 	    name, sizeof (obj), 1, &obj);
180eda14cbcSMatt Macy 	if (err)
181eda14cbcSMatt Macy 		return (err);
182eda14cbcSMatt Macy 
183eda14cbcSMatt Macy 	return (dsl_dir_hold_obj(dp, obj, name, dp, ddp));
184eda14cbcSMatt Macy }
185eda14cbcSMatt Macy 
186eda14cbcSMatt Macy static dsl_pool_t *
187eda14cbcSMatt Macy dsl_pool_open_impl(spa_t *spa, uint64_t txg)
188eda14cbcSMatt Macy {
189eda14cbcSMatt Macy 	dsl_pool_t *dp;
190eda14cbcSMatt Macy 	blkptr_t *bp = spa_get_rootblkptr(spa);
191eda14cbcSMatt Macy 
192eda14cbcSMatt Macy 	dp = kmem_zalloc(sizeof (dsl_pool_t), KM_SLEEP);
193eda14cbcSMatt Macy 	dp->dp_spa = spa;
194eda14cbcSMatt Macy 	dp->dp_meta_rootbp = *bp;
195eda14cbcSMatt Macy 	rrw_init(&dp->dp_config_rwlock, B_TRUE);
196eda14cbcSMatt Macy 	txg_init(dp, txg);
197eda14cbcSMatt Macy 	mmp_init(spa);
198eda14cbcSMatt Macy 
199eda14cbcSMatt Macy 	txg_list_create(&dp->dp_dirty_datasets, spa,
200eda14cbcSMatt Macy 	    offsetof(dsl_dataset_t, ds_dirty_link));
201eda14cbcSMatt Macy 	txg_list_create(&dp->dp_dirty_zilogs, spa,
202eda14cbcSMatt Macy 	    offsetof(zilog_t, zl_dirty_link));
203eda14cbcSMatt Macy 	txg_list_create(&dp->dp_dirty_dirs, spa,
204eda14cbcSMatt Macy 	    offsetof(dsl_dir_t, dd_dirty_link));
205eda14cbcSMatt Macy 	txg_list_create(&dp->dp_sync_tasks, spa,
206eda14cbcSMatt Macy 	    offsetof(dsl_sync_task_t, dst_node));
207eda14cbcSMatt Macy 	txg_list_create(&dp->dp_early_sync_tasks, spa,
208eda14cbcSMatt Macy 	    offsetof(dsl_sync_task_t, dst_node));
209eda14cbcSMatt Macy 
210eda14cbcSMatt Macy 	dp->dp_sync_taskq = taskq_create("dp_sync_taskq",
211eda14cbcSMatt Macy 	    zfs_sync_taskq_batch_pct, minclsyspri, 1, INT_MAX,
212eda14cbcSMatt Macy 	    TASKQ_THREADS_CPU_PCT);
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 
2237877fdebSMatt Macy 	dp->dp_zrele_taskq = taskq_create("z_zrele", 100, defclsyspri,
2247877fdebSMatt Macy 	    boot_ncpus * 8, INT_MAX, TASKQ_PREPOPULATE | TASKQ_DYNAMIC |
2257877fdebSMatt Macy 	    TASKQ_THREADS_CPU_PCT);
226eda14cbcSMatt Macy 	dp->dp_unlinked_drain_taskq = taskq_create("z_unlinked_drain",
2277877fdebSMatt Macy 	    100, defclsyspri, boot_ncpus, INT_MAX,
2287877fdebSMatt Macy 	    TASKQ_PREPOPULATE | TASKQ_DYNAMIC | TASKQ_THREADS_CPU_PCT);
229eda14cbcSMatt Macy 
230eda14cbcSMatt Macy 	return (dp);
231eda14cbcSMatt Macy }
232eda14cbcSMatt Macy 
233eda14cbcSMatt Macy int
234eda14cbcSMatt Macy dsl_pool_init(spa_t *spa, uint64_t txg, dsl_pool_t **dpp)
235eda14cbcSMatt Macy {
236eda14cbcSMatt Macy 	int err;
237eda14cbcSMatt Macy 	dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
238eda14cbcSMatt Macy 
239eda14cbcSMatt Macy 	/*
240eda14cbcSMatt Macy 	 * Initialize the caller's dsl_pool_t structure before we actually open
241eda14cbcSMatt Macy 	 * the meta objset.  This is done because a self-healing write zio may
242eda14cbcSMatt Macy 	 * be issued as part of dmu_objset_open_impl() and the spa needs its
243eda14cbcSMatt Macy 	 * dsl_pool_t initialized in order to handle the write.
244eda14cbcSMatt Macy 	 */
245eda14cbcSMatt Macy 	*dpp = dp;
246eda14cbcSMatt Macy 
247eda14cbcSMatt Macy 	err = dmu_objset_open_impl(spa, NULL, &dp->dp_meta_rootbp,
248eda14cbcSMatt Macy 	    &dp->dp_meta_objset);
249eda14cbcSMatt Macy 	if (err != 0) {
250eda14cbcSMatt Macy 		dsl_pool_close(dp);
251eda14cbcSMatt Macy 		*dpp = NULL;
252eda14cbcSMatt Macy 	}
253eda14cbcSMatt Macy 
254eda14cbcSMatt Macy 	return (err);
255eda14cbcSMatt Macy }
256eda14cbcSMatt Macy 
257eda14cbcSMatt Macy int
258eda14cbcSMatt Macy dsl_pool_open(dsl_pool_t *dp)
259eda14cbcSMatt Macy {
260eda14cbcSMatt Macy 	int err;
261eda14cbcSMatt Macy 	dsl_dir_t *dd;
262eda14cbcSMatt Macy 	dsl_dataset_t *ds;
263eda14cbcSMatt Macy 	uint64_t obj;
264eda14cbcSMatt Macy 
265eda14cbcSMatt Macy 	rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
266eda14cbcSMatt Macy 	err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
267eda14cbcSMatt Macy 	    DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1,
268eda14cbcSMatt Macy 	    &dp->dp_root_dir_obj);
269eda14cbcSMatt Macy 	if (err)
270eda14cbcSMatt Macy 		goto out;
271eda14cbcSMatt Macy 
272eda14cbcSMatt Macy 	err = dsl_dir_hold_obj(dp, dp->dp_root_dir_obj,
273eda14cbcSMatt Macy 	    NULL, dp, &dp->dp_root_dir);
274eda14cbcSMatt Macy 	if (err)
275eda14cbcSMatt Macy 		goto out;
276eda14cbcSMatt Macy 
277eda14cbcSMatt Macy 	err = dsl_pool_open_special_dir(dp, MOS_DIR_NAME, &dp->dp_mos_dir);
278eda14cbcSMatt Macy 	if (err)
279eda14cbcSMatt Macy 		goto out;
280eda14cbcSMatt Macy 
281eda14cbcSMatt Macy 	if (spa_version(dp->dp_spa) >= SPA_VERSION_ORIGIN) {
282eda14cbcSMatt Macy 		err = dsl_pool_open_special_dir(dp, ORIGIN_DIR_NAME, &dd);
283eda14cbcSMatt Macy 		if (err)
284eda14cbcSMatt Macy 			goto out;
285eda14cbcSMatt Macy 		err = dsl_dataset_hold_obj(dp,
286eda14cbcSMatt Macy 		    dsl_dir_phys(dd)->dd_head_dataset_obj, FTAG, &ds);
287eda14cbcSMatt Macy 		if (err == 0) {
288eda14cbcSMatt Macy 			err = dsl_dataset_hold_obj(dp,
289eda14cbcSMatt Macy 			    dsl_dataset_phys(ds)->ds_prev_snap_obj, dp,
290eda14cbcSMatt Macy 			    &dp->dp_origin_snap);
291eda14cbcSMatt Macy 			dsl_dataset_rele(ds, FTAG);
292eda14cbcSMatt Macy 		}
293eda14cbcSMatt Macy 		dsl_dir_rele(dd, dp);
294eda14cbcSMatt Macy 		if (err)
295eda14cbcSMatt Macy 			goto out;
296eda14cbcSMatt Macy 	}
297eda14cbcSMatt Macy 
298eda14cbcSMatt Macy 	if (spa_version(dp->dp_spa) >= SPA_VERSION_DEADLISTS) {
299eda14cbcSMatt Macy 		err = dsl_pool_open_special_dir(dp, FREE_DIR_NAME,
300eda14cbcSMatt Macy 		    &dp->dp_free_dir);
301eda14cbcSMatt Macy 		if (err)
302eda14cbcSMatt Macy 			goto out;
303eda14cbcSMatt Macy 
304eda14cbcSMatt Macy 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
305eda14cbcSMatt Macy 		    DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj);
306eda14cbcSMatt Macy 		if (err)
307eda14cbcSMatt Macy 			goto out;
308eda14cbcSMatt Macy 		VERIFY0(bpobj_open(&dp->dp_free_bpobj,
309eda14cbcSMatt Macy 		    dp->dp_meta_objset, obj));
310eda14cbcSMatt Macy 	}
311eda14cbcSMatt Macy 
312eda14cbcSMatt Macy 	if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_OBSOLETE_COUNTS)) {
313eda14cbcSMatt Macy 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
314eda14cbcSMatt Macy 		    DMU_POOL_OBSOLETE_BPOBJ, sizeof (uint64_t), 1, &obj);
315eda14cbcSMatt Macy 		if (err == 0) {
316eda14cbcSMatt Macy 			VERIFY0(bpobj_open(&dp->dp_obsolete_bpobj,
317eda14cbcSMatt Macy 			    dp->dp_meta_objset, obj));
318eda14cbcSMatt Macy 		} else if (err == ENOENT) {
319eda14cbcSMatt Macy 			/*
320eda14cbcSMatt Macy 			 * We might not have created the remap bpobj yet.
321eda14cbcSMatt Macy 			 */
322eda14cbcSMatt Macy 			err = 0;
323eda14cbcSMatt Macy 		} else {
324eda14cbcSMatt Macy 			goto out;
325eda14cbcSMatt Macy 		}
326eda14cbcSMatt Macy 	}
327eda14cbcSMatt Macy 
328eda14cbcSMatt Macy 	/*
329eda14cbcSMatt Macy 	 * Note: errors ignored, because the these special dirs, used for
330eda14cbcSMatt Macy 	 * space accounting, are only created on demand.
331eda14cbcSMatt Macy 	 */
332eda14cbcSMatt Macy 	(void) dsl_pool_open_special_dir(dp, LEAK_DIR_NAME,
333eda14cbcSMatt Macy 	    &dp->dp_leak_dir);
334eda14cbcSMatt Macy 
335eda14cbcSMatt Macy 	if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_ASYNC_DESTROY)) {
336eda14cbcSMatt Macy 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
337eda14cbcSMatt Macy 		    DMU_POOL_BPTREE_OBJ, sizeof (uint64_t), 1,
338eda14cbcSMatt Macy 		    &dp->dp_bptree_obj);
339eda14cbcSMatt Macy 		if (err != 0)
340eda14cbcSMatt Macy 			goto out;
341eda14cbcSMatt Macy 	}
342eda14cbcSMatt Macy 
343eda14cbcSMatt Macy 	if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_EMPTY_BPOBJ)) {
344eda14cbcSMatt Macy 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
345eda14cbcSMatt Macy 		    DMU_POOL_EMPTY_BPOBJ, sizeof (uint64_t), 1,
346eda14cbcSMatt Macy 		    &dp->dp_empty_bpobj);
347eda14cbcSMatt Macy 		if (err != 0)
348eda14cbcSMatt Macy 			goto out;
349eda14cbcSMatt Macy 	}
350eda14cbcSMatt Macy 
351eda14cbcSMatt Macy 	err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
352eda14cbcSMatt Macy 	    DMU_POOL_TMP_USERREFS, sizeof (uint64_t), 1,
353eda14cbcSMatt Macy 	    &dp->dp_tmp_userrefs_obj);
354eda14cbcSMatt Macy 	if (err == ENOENT)
355eda14cbcSMatt Macy 		err = 0;
356eda14cbcSMatt Macy 	if (err)
357eda14cbcSMatt Macy 		goto out;
358eda14cbcSMatt Macy 
359eda14cbcSMatt Macy 	err = dsl_scan_init(dp, dp->dp_tx.tx_open_txg);
360eda14cbcSMatt Macy 
361eda14cbcSMatt Macy out:
362eda14cbcSMatt Macy 	rrw_exit(&dp->dp_config_rwlock, FTAG);
363eda14cbcSMatt Macy 	return (err);
364eda14cbcSMatt Macy }
365eda14cbcSMatt Macy 
366eda14cbcSMatt Macy void
367eda14cbcSMatt Macy dsl_pool_close(dsl_pool_t *dp)
368eda14cbcSMatt Macy {
369eda14cbcSMatt Macy 	/*
370eda14cbcSMatt Macy 	 * Drop our references from dsl_pool_open().
371eda14cbcSMatt Macy 	 *
372eda14cbcSMatt Macy 	 * Since we held the origin_snap from "syncing" context (which
373eda14cbcSMatt Macy 	 * includes pool-opening context), it actually only got a "ref"
374eda14cbcSMatt Macy 	 * and not a hold, so just drop that here.
375eda14cbcSMatt Macy 	 */
376eda14cbcSMatt Macy 	if (dp->dp_origin_snap != NULL)
377eda14cbcSMatt Macy 		dsl_dataset_rele(dp->dp_origin_snap, dp);
378eda14cbcSMatt Macy 	if (dp->dp_mos_dir != NULL)
379eda14cbcSMatt Macy 		dsl_dir_rele(dp->dp_mos_dir, dp);
380eda14cbcSMatt Macy 	if (dp->dp_free_dir != NULL)
381eda14cbcSMatt Macy 		dsl_dir_rele(dp->dp_free_dir, dp);
382eda14cbcSMatt Macy 	if (dp->dp_leak_dir != NULL)
383eda14cbcSMatt Macy 		dsl_dir_rele(dp->dp_leak_dir, dp);
384eda14cbcSMatt Macy 	if (dp->dp_root_dir != NULL)
385eda14cbcSMatt Macy 		dsl_dir_rele(dp->dp_root_dir, dp);
386eda14cbcSMatt Macy 
387eda14cbcSMatt Macy 	bpobj_close(&dp->dp_free_bpobj);
388eda14cbcSMatt Macy 	bpobj_close(&dp->dp_obsolete_bpobj);
389eda14cbcSMatt Macy 
390eda14cbcSMatt Macy 	/* undo the dmu_objset_open_impl(mos) from dsl_pool_open() */
391eda14cbcSMatt Macy 	if (dp->dp_meta_objset != NULL)
392eda14cbcSMatt Macy 		dmu_objset_evict(dp->dp_meta_objset);
393eda14cbcSMatt Macy 
394eda14cbcSMatt Macy 	txg_list_destroy(&dp->dp_dirty_datasets);
395eda14cbcSMatt Macy 	txg_list_destroy(&dp->dp_dirty_zilogs);
396eda14cbcSMatt Macy 	txg_list_destroy(&dp->dp_sync_tasks);
397eda14cbcSMatt Macy 	txg_list_destroy(&dp->dp_early_sync_tasks);
398eda14cbcSMatt Macy 	txg_list_destroy(&dp->dp_dirty_dirs);
399eda14cbcSMatt Macy 
400eda14cbcSMatt Macy 	taskq_destroy(dp->dp_zil_clean_taskq);
401eda14cbcSMatt Macy 	taskq_destroy(dp->dp_sync_taskq);
402eda14cbcSMatt Macy 
403eda14cbcSMatt Macy 	/*
404eda14cbcSMatt Macy 	 * We can't set retry to TRUE since we're explicitly specifying
405eda14cbcSMatt Macy 	 * a spa to flush. This is good enough; any missed buffers for
406eda14cbcSMatt Macy 	 * this spa won't cause trouble, and they'll eventually fall
407eda14cbcSMatt Macy 	 * out of the ARC just like any other unused buffer.
408eda14cbcSMatt Macy 	 */
409eda14cbcSMatt Macy 	arc_flush(dp->dp_spa, FALSE);
410eda14cbcSMatt Macy 
411eda14cbcSMatt Macy 	mmp_fini(dp->dp_spa);
412eda14cbcSMatt Macy 	txg_fini(dp);
413eda14cbcSMatt Macy 	dsl_scan_fini(dp);
414eda14cbcSMatt Macy 	dmu_buf_user_evict_wait();
415eda14cbcSMatt Macy 
416eda14cbcSMatt Macy 	rrw_destroy(&dp->dp_config_rwlock);
417eda14cbcSMatt Macy 	mutex_destroy(&dp->dp_lock);
418eda14cbcSMatt Macy 	cv_destroy(&dp->dp_spaceavail_cv);
419eda14cbcSMatt Macy 	taskq_destroy(dp->dp_unlinked_drain_taskq);
420eda14cbcSMatt Macy 	taskq_destroy(dp->dp_zrele_taskq);
421eda14cbcSMatt Macy 	if (dp->dp_blkstats != NULL) {
422eda14cbcSMatt Macy 		mutex_destroy(&dp->dp_blkstats->zab_lock);
423eda14cbcSMatt Macy 		vmem_free(dp->dp_blkstats, sizeof (zfs_all_blkstats_t));
424eda14cbcSMatt Macy 	}
425eda14cbcSMatt Macy 	kmem_free(dp, sizeof (dsl_pool_t));
426eda14cbcSMatt Macy }
427eda14cbcSMatt Macy 
428eda14cbcSMatt Macy void
429eda14cbcSMatt Macy dsl_pool_create_obsolete_bpobj(dsl_pool_t *dp, dmu_tx_t *tx)
430eda14cbcSMatt Macy {
431eda14cbcSMatt Macy 	uint64_t obj;
432eda14cbcSMatt Macy 	/*
433eda14cbcSMatt Macy 	 * Currently, we only create the obsolete_bpobj where there are
434eda14cbcSMatt Macy 	 * indirect vdevs with referenced mappings.
435eda14cbcSMatt Macy 	 */
436eda14cbcSMatt Macy 	ASSERT(spa_feature_is_active(dp->dp_spa, SPA_FEATURE_DEVICE_REMOVAL));
437eda14cbcSMatt Macy 	/* create and open the obsolete_bpobj */
438eda14cbcSMatt Macy 	obj = bpobj_alloc(dp->dp_meta_objset, SPA_OLD_MAXBLOCKSIZE, tx);
439eda14cbcSMatt Macy 	VERIFY0(bpobj_open(&dp->dp_obsolete_bpobj, dp->dp_meta_objset, obj));
440eda14cbcSMatt Macy 	VERIFY0(zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
441eda14cbcSMatt Macy 	    DMU_POOL_OBSOLETE_BPOBJ, sizeof (uint64_t), 1, &obj, tx));
442eda14cbcSMatt Macy 	spa_feature_incr(dp->dp_spa, SPA_FEATURE_OBSOLETE_COUNTS, tx);
443eda14cbcSMatt Macy }
444eda14cbcSMatt Macy 
445eda14cbcSMatt Macy void
446eda14cbcSMatt Macy dsl_pool_destroy_obsolete_bpobj(dsl_pool_t *dp, dmu_tx_t *tx)
447eda14cbcSMatt Macy {
448eda14cbcSMatt Macy 	spa_feature_decr(dp->dp_spa, SPA_FEATURE_OBSOLETE_COUNTS, tx);
449eda14cbcSMatt Macy 	VERIFY0(zap_remove(dp->dp_meta_objset,
450eda14cbcSMatt Macy 	    DMU_POOL_DIRECTORY_OBJECT,
451eda14cbcSMatt Macy 	    DMU_POOL_OBSOLETE_BPOBJ, tx));
452eda14cbcSMatt Macy 	bpobj_free(dp->dp_meta_objset,
453eda14cbcSMatt Macy 	    dp->dp_obsolete_bpobj.bpo_object, tx);
454eda14cbcSMatt Macy 	bpobj_close(&dp->dp_obsolete_bpobj);
455eda14cbcSMatt Macy }
456eda14cbcSMatt Macy 
457eda14cbcSMatt Macy dsl_pool_t *
458eda14cbcSMatt Macy dsl_pool_create(spa_t *spa, nvlist_t *zplprops, dsl_crypto_params_t *dcp,
459eda14cbcSMatt Macy     uint64_t txg)
460eda14cbcSMatt Macy {
461eda14cbcSMatt Macy 	int err;
462eda14cbcSMatt Macy 	dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
463eda14cbcSMatt Macy 	dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg);
464eda14cbcSMatt Macy #ifdef _KERNEL
465eda14cbcSMatt Macy 	objset_t *os;
466eda14cbcSMatt Macy #else
467eda14cbcSMatt Macy 	objset_t *os __attribute__((unused));
468eda14cbcSMatt Macy #endif
469eda14cbcSMatt Macy 	dsl_dataset_t *ds;
470eda14cbcSMatt Macy 	uint64_t obj;
471eda14cbcSMatt Macy 
472eda14cbcSMatt Macy 	rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
473eda14cbcSMatt Macy 
474eda14cbcSMatt Macy 	/* create and open the MOS (meta-objset) */
475eda14cbcSMatt Macy 	dp->dp_meta_objset = dmu_objset_create_impl(spa,
476eda14cbcSMatt Macy 	    NULL, &dp->dp_meta_rootbp, DMU_OST_META, tx);
477eda14cbcSMatt Macy 	spa->spa_meta_objset = dp->dp_meta_objset;
478eda14cbcSMatt Macy 
479eda14cbcSMatt Macy 	/* create the pool directory */
480eda14cbcSMatt Macy 	err = zap_create_claim(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
481eda14cbcSMatt Macy 	    DMU_OT_OBJECT_DIRECTORY, DMU_OT_NONE, 0, tx);
482eda14cbcSMatt Macy 	ASSERT0(err);
483eda14cbcSMatt Macy 
484eda14cbcSMatt Macy 	/* Initialize scan structures */
485eda14cbcSMatt Macy 	VERIFY0(dsl_scan_init(dp, txg));
486eda14cbcSMatt Macy 
487eda14cbcSMatt Macy 	/* create and open the root dir */
488eda14cbcSMatt Macy 	dp->dp_root_dir_obj = dsl_dir_create_sync(dp, NULL, NULL, tx);
489eda14cbcSMatt Macy 	VERIFY0(dsl_dir_hold_obj(dp, dp->dp_root_dir_obj,
490eda14cbcSMatt Macy 	    NULL, dp, &dp->dp_root_dir));
491eda14cbcSMatt Macy 
492eda14cbcSMatt Macy 	/* create and open the meta-objset dir */
493eda14cbcSMatt Macy 	(void) dsl_dir_create_sync(dp, dp->dp_root_dir, MOS_DIR_NAME, tx);
494eda14cbcSMatt Macy 	VERIFY0(dsl_pool_open_special_dir(dp,
495eda14cbcSMatt Macy 	    MOS_DIR_NAME, &dp->dp_mos_dir));
496eda14cbcSMatt Macy 
497eda14cbcSMatt Macy 	if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
498eda14cbcSMatt Macy 		/* create and open the free dir */
499eda14cbcSMatt Macy 		(void) dsl_dir_create_sync(dp, dp->dp_root_dir,
500eda14cbcSMatt Macy 		    FREE_DIR_NAME, tx);
501eda14cbcSMatt Macy 		VERIFY0(dsl_pool_open_special_dir(dp,
502eda14cbcSMatt Macy 		    FREE_DIR_NAME, &dp->dp_free_dir));
503eda14cbcSMatt Macy 
504eda14cbcSMatt Macy 		/* create and open the free_bplist */
505eda14cbcSMatt Macy 		obj = bpobj_alloc(dp->dp_meta_objset, SPA_OLD_MAXBLOCKSIZE, tx);
506eda14cbcSMatt Macy 		VERIFY(zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
507eda14cbcSMatt Macy 		    DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj, tx) == 0);
508eda14cbcSMatt Macy 		VERIFY0(bpobj_open(&dp->dp_free_bpobj,
509eda14cbcSMatt Macy 		    dp->dp_meta_objset, obj));
510eda14cbcSMatt Macy 	}
511eda14cbcSMatt Macy 
512eda14cbcSMatt Macy 	if (spa_version(spa) >= SPA_VERSION_DSL_SCRUB)
513eda14cbcSMatt Macy 		dsl_pool_create_origin(dp, tx);
514eda14cbcSMatt Macy 
515eda14cbcSMatt Macy 	/*
516eda14cbcSMatt Macy 	 * Some features may be needed when creating the root dataset, so we
517eda14cbcSMatt Macy 	 * create the feature objects here.
518eda14cbcSMatt Macy 	 */
519eda14cbcSMatt Macy 	if (spa_version(spa) >= SPA_VERSION_FEATURES)
520eda14cbcSMatt Macy 		spa_feature_create_zap_objects(spa, tx);
521eda14cbcSMatt Macy 
522eda14cbcSMatt Macy 	if (dcp != NULL && dcp->cp_crypt != ZIO_CRYPT_OFF &&
523eda14cbcSMatt Macy 	    dcp->cp_crypt != ZIO_CRYPT_INHERIT)
524eda14cbcSMatt Macy 		spa_feature_enable(spa, SPA_FEATURE_ENCRYPTION, tx);
525eda14cbcSMatt Macy 
526eda14cbcSMatt Macy 	/* create the root dataset */
527eda14cbcSMatt Macy 	obj = dsl_dataset_create_sync_dd(dp->dp_root_dir, NULL, dcp, 0, tx);
528eda14cbcSMatt Macy 
529eda14cbcSMatt Macy 	/* create the root objset */
530eda14cbcSMatt Macy 	VERIFY0(dsl_dataset_hold_obj_flags(dp, obj,
531eda14cbcSMatt Macy 	    DS_HOLD_FLAG_DECRYPT, FTAG, &ds));
532eda14cbcSMatt Macy 	rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
533eda14cbcSMatt Macy 	os = dmu_objset_create_impl(dp->dp_spa, ds,
534eda14cbcSMatt Macy 	    dsl_dataset_get_blkptr(ds), DMU_OST_ZFS, tx);
535eda14cbcSMatt Macy 	rrw_exit(&ds->ds_bp_rwlock, FTAG);
536eda14cbcSMatt Macy #ifdef _KERNEL
537eda14cbcSMatt Macy 	zfs_create_fs(os, kcred, zplprops, tx);
538eda14cbcSMatt Macy #endif
539eda14cbcSMatt Macy 	dsl_dataset_rele_flags(ds, DS_HOLD_FLAG_DECRYPT, FTAG);
540eda14cbcSMatt Macy 
541eda14cbcSMatt Macy 	dmu_tx_commit(tx);
542eda14cbcSMatt Macy 
543eda14cbcSMatt Macy 	rrw_exit(&dp->dp_config_rwlock, FTAG);
544eda14cbcSMatt Macy 
545eda14cbcSMatt Macy 	return (dp);
546eda14cbcSMatt Macy }
547eda14cbcSMatt Macy 
548eda14cbcSMatt Macy /*
549eda14cbcSMatt Macy  * Account for the meta-objset space in its placeholder dsl_dir.
550eda14cbcSMatt Macy  */
551eda14cbcSMatt Macy void
552eda14cbcSMatt Macy dsl_pool_mos_diduse_space(dsl_pool_t *dp,
553eda14cbcSMatt Macy     int64_t used, int64_t comp, int64_t uncomp)
554eda14cbcSMatt Macy {
555eda14cbcSMatt Macy 	ASSERT3U(comp, ==, uncomp); /* it's all metadata */
556eda14cbcSMatt Macy 	mutex_enter(&dp->dp_lock);
557eda14cbcSMatt Macy 	dp->dp_mos_used_delta += used;
558eda14cbcSMatt Macy 	dp->dp_mos_compressed_delta += comp;
559eda14cbcSMatt Macy 	dp->dp_mos_uncompressed_delta += uncomp;
560eda14cbcSMatt Macy 	mutex_exit(&dp->dp_lock);
561eda14cbcSMatt Macy }
562eda14cbcSMatt Macy 
563eda14cbcSMatt Macy static void
564eda14cbcSMatt Macy dsl_pool_sync_mos(dsl_pool_t *dp, dmu_tx_t *tx)
565eda14cbcSMatt Macy {
566eda14cbcSMatt Macy 	zio_t *zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
567eda14cbcSMatt Macy 	dmu_objset_sync(dp->dp_meta_objset, zio, tx);
568eda14cbcSMatt Macy 	VERIFY0(zio_wait(zio));
5697877fdebSMatt Macy 	dmu_objset_sync_done(dp->dp_meta_objset, tx);
5707877fdebSMatt Macy 	taskq_wait(dp->dp_sync_taskq);
571*3ff01b23SMartin Matuska 	multilist_destroy(&dp->dp_meta_objset->os_synced_dnodes);
5727877fdebSMatt Macy 
573eda14cbcSMatt Macy 	dprintf_bp(&dp->dp_meta_rootbp, "meta objset rootbp is %s", "");
574eda14cbcSMatt Macy 	spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
575eda14cbcSMatt Macy }
576eda14cbcSMatt Macy 
577eda14cbcSMatt Macy static void
578eda14cbcSMatt Macy dsl_pool_dirty_delta(dsl_pool_t *dp, int64_t delta)
579eda14cbcSMatt Macy {
580eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&dp->dp_lock));
581eda14cbcSMatt Macy 
582eda14cbcSMatt Macy 	if (delta < 0)
583eda14cbcSMatt Macy 		ASSERT3U(-delta, <=, dp->dp_dirty_total);
584eda14cbcSMatt Macy 
585eda14cbcSMatt Macy 	dp->dp_dirty_total += delta;
586eda14cbcSMatt Macy 
587eda14cbcSMatt Macy 	/*
588eda14cbcSMatt Macy 	 * Note: we signal even when increasing dp_dirty_total.
589eda14cbcSMatt Macy 	 * This ensures forward progress -- each thread wakes the next waiter.
590eda14cbcSMatt Macy 	 */
591eda14cbcSMatt Macy 	if (dp->dp_dirty_total < zfs_dirty_data_max)
592eda14cbcSMatt Macy 		cv_signal(&dp->dp_spaceavail_cv);
593eda14cbcSMatt Macy }
594eda14cbcSMatt Macy 
595eda14cbcSMatt Macy #ifdef ZFS_DEBUG
596eda14cbcSMatt Macy static boolean_t
597eda14cbcSMatt Macy dsl_early_sync_task_verify(dsl_pool_t *dp, uint64_t txg)
598eda14cbcSMatt Macy {
599eda14cbcSMatt Macy 	spa_t *spa = dp->dp_spa;
600eda14cbcSMatt Macy 	vdev_t *rvd = spa->spa_root_vdev;
601eda14cbcSMatt Macy 
602eda14cbcSMatt Macy 	for (uint64_t c = 0; c < rvd->vdev_children; c++) {
603eda14cbcSMatt Macy 		vdev_t *vd = rvd->vdev_child[c];
604eda14cbcSMatt Macy 		txg_list_t *tl = &vd->vdev_ms_list;
605eda14cbcSMatt Macy 		metaslab_t *ms;
606eda14cbcSMatt Macy 
607eda14cbcSMatt Macy 		for (ms = txg_list_head(tl, TXG_CLEAN(txg)); ms;
608eda14cbcSMatt Macy 		    ms = txg_list_next(tl, ms, TXG_CLEAN(txg))) {
609eda14cbcSMatt Macy 			VERIFY(range_tree_is_empty(ms->ms_freeing));
610eda14cbcSMatt Macy 			VERIFY(range_tree_is_empty(ms->ms_checkpointing));
611eda14cbcSMatt Macy 		}
612eda14cbcSMatt Macy 	}
613eda14cbcSMatt Macy 
614eda14cbcSMatt Macy 	return (B_TRUE);
615eda14cbcSMatt Macy }
616eda14cbcSMatt Macy #endif
617eda14cbcSMatt Macy 
618eda14cbcSMatt Macy void
619eda14cbcSMatt Macy dsl_pool_sync(dsl_pool_t *dp, uint64_t txg)
620eda14cbcSMatt Macy {
621eda14cbcSMatt Macy 	zio_t *zio;
622eda14cbcSMatt Macy 	dmu_tx_t *tx;
623eda14cbcSMatt Macy 	dsl_dir_t *dd;
624eda14cbcSMatt Macy 	dsl_dataset_t *ds;
625eda14cbcSMatt Macy 	objset_t *mos = dp->dp_meta_objset;
626eda14cbcSMatt Macy 	list_t synced_datasets;
627eda14cbcSMatt Macy 
628eda14cbcSMatt Macy 	list_create(&synced_datasets, sizeof (dsl_dataset_t),
629eda14cbcSMatt Macy 	    offsetof(dsl_dataset_t, ds_synced_link));
630eda14cbcSMatt Macy 
631eda14cbcSMatt Macy 	tx = dmu_tx_create_assigned(dp, txg);
632eda14cbcSMatt Macy 
633eda14cbcSMatt Macy 	/*
634eda14cbcSMatt Macy 	 * Run all early sync tasks before writing out any dirty blocks.
635eda14cbcSMatt Macy 	 * For more info on early sync tasks see block comment in
636eda14cbcSMatt Macy 	 * dsl_early_sync_task().
637eda14cbcSMatt Macy 	 */
638eda14cbcSMatt Macy 	if (!txg_list_empty(&dp->dp_early_sync_tasks, txg)) {
639eda14cbcSMatt Macy 		dsl_sync_task_t *dst;
640eda14cbcSMatt Macy 
641eda14cbcSMatt Macy 		ASSERT3U(spa_sync_pass(dp->dp_spa), ==, 1);
642eda14cbcSMatt Macy 		while ((dst =
643eda14cbcSMatt Macy 		    txg_list_remove(&dp->dp_early_sync_tasks, txg)) != NULL) {
644eda14cbcSMatt Macy 			ASSERT(dsl_early_sync_task_verify(dp, txg));
645eda14cbcSMatt Macy 			dsl_sync_task_sync(dst, tx);
646eda14cbcSMatt Macy 		}
647eda14cbcSMatt Macy 		ASSERT(dsl_early_sync_task_verify(dp, txg));
648eda14cbcSMatt Macy 	}
649eda14cbcSMatt Macy 
650eda14cbcSMatt Macy 	/*
651eda14cbcSMatt Macy 	 * Write out all dirty blocks of dirty datasets.
652eda14cbcSMatt Macy 	 */
653eda14cbcSMatt Macy 	zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
654eda14cbcSMatt Macy 	while ((ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) != NULL) {
655eda14cbcSMatt Macy 		/*
656eda14cbcSMatt Macy 		 * We must not sync any non-MOS datasets twice, because
657eda14cbcSMatt Macy 		 * we may have taken a snapshot of them.  However, we
658eda14cbcSMatt Macy 		 * may sync newly-created datasets on pass 2.
659eda14cbcSMatt Macy 		 */
660eda14cbcSMatt Macy 		ASSERT(!list_link_active(&ds->ds_synced_link));
661eda14cbcSMatt Macy 		list_insert_tail(&synced_datasets, ds);
662eda14cbcSMatt Macy 		dsl_dataset_sync(ds, zio, tx);
663eda14cbcSMatt Macy 	}
664eda14cbcSMatt Macy 	VERIFY0(zio_wait(zio));
665eda14cbcSMatt Macy 
666eda14cbcSMatt Macy 	/*
667eda14cbcSMatt Macy 	 * Update the long range free counter after
668eda14cbcSMatt Macy 	 * we're done syncing user data
669eda14cbcSMatt Macy 	 */
670eda14cbcSMatt Macy 	mutex_enter(&dp->dp_lock);
671eda14cbcSMatt Macy 	ASSERT(spa_sync_pass(dp->dp_spa) == 1 ||
672eda14cbcSMatt Macy 	    dp->dp_long_free_dirty_pertxg[txg & TXG_MASK] == 0);
673eda14cbcSMatt Macy 	dp->dp_long_free_dirty_pertxg[txg & TXG_MASK] = 0;
674eda14cbcSMatt Macy 	mutex_exit(&dp->dp_lock);
675eda14cbcSMatt Macy 
676eda14cbcSMatt Macy 	/*
677eda14cbcSMatt Macy 	 * After the data blocks have been written (ensured by the zio_wait()
678eda14cbcSMatt Macy 	 * above), update the user/group/project space accounting.  This happens
679eda14cbcSMatt Macy 	 * in tasks dispatched to dp_sync_taskq, so wait for them before
680eda14cbcSMatt Macy 	 * continuing.
681eda14cbcSMatt Macy 	 */
682eda14cbcSMatt Macy 	for (ds = list_head(&synced_datasets); ds != NULL;
683eda14cbcSMatt Macy 	    ds = list_next(&synced_datasets, ds)) {
6847877fdebSMatt Macy 		dmu_objset_sync_done(ds->ds_objset, tx);
685eda14cbcSMatt Macy 	}
686eda14cbcSMatt Macy 	taskq_wait(dp->dp_sync_taskq);
687eda14cbcSMatt Macy 
688eda14cbcSMatt Macy 	/*
689eda14cbcSMatt Macy 	 * Sync the datasets again to push out the changes due to
690eda14cbcSMatt Macy 	 * userspace updates.  This must be done before we process the
691eda14cbcSMatt Macy 	 * sync tasks, so that any snapshots will have the correct
692eda14cbcSMatt Macy 	 * user accounting information (and we won't get confused
693eda14cbcSMatt Macy 	 * about which blocks are part of the snapshot).
694eda14cbcSMatt Macy 	 */
695eda14cbcSMatt Macy 	zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
696eda14cbcSMatt Macy 	while ((ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) != NULL) {
697eda14cbcSMatt Macy 		objset_t *os = ds->ds_objset;
698eda14cbcSMatt Macy 
699eda14cbcSMatt Macy 		ASSERT(list_link_active(&ds->ds_synced_link));
700eda14cbcSMatt Macy 		dmu_buf_rele(ds->ds_dbuf, ds);
701eda14cbcSMatt Macy 		dsl_dataset_sync(ds, zio, tx);
702eda14cbcSMatt Macy 
703eda14cbcSMatt Macy 		/*
704eda14cbcSMatt Macy 		 * Release any key mappings created by calls to
705eda14cbcSMatt Macy 		 * dsl_dataset_dirty() from the userquota accounting
706eda14cbcSMatt Macy 		 * code paths.
707eda14cbcSMatt Macy 		 */
708eda14cbcSMatt Macy 		if (os->os_encrypted && !os->os_raw_receive &&
709eda14cbcSMatt Macy 		    !os->os_next_write_raw[txg & TXG_MASK]) {
710eda14cbcSMatt Macy 			ASSERT3P(ds->ds_key_mapping, !=, NULL);
711eda14cbcSMatt Macy 			key_mapping_rele(dp->dp_spa, ds->ds_key_mapping, ds);
712eda14cbcSMatt Macy 		}
713eda14cbcSMatt Macy 	}
714eda14cbcSMatt Macy 	VERIFY0(zio_wait(zio));
715eda14cbcSMatt Macy 
716eda14cbcSMatt Macy 	/*
717eda14cbcSMatt Macy 	 * Now that the datasets have been completely synced, we can
718eda14cbcSMatt Macy 	 * clean up our in-memory structures accumulated while syncing:
719eda14cbcSMatt Macy 	 *
720eda14cbcSMatt Macy 	 *  - move dead blocks from the pending deadlist and livelists
721eda14cbcSMatt Macy 	 *    to the on-disk versions
722eda14cbcSMatt Macy 	 *  - release hold from dsl_dataset_dirty()
723eda14cbcSMatt Macy 	 *  - release key mapping hold from dsl_dataset_dirty()
724eda14cbcSMatt Macy 	 */
725eda14cbcSMatt Macy 	while ((ds = list_remove_head(&synced_datasets)) != NULL) {
726eda14cbcSMatt Macy 		objset_t *os = ds->ds_objset;
727eda14cbcSMatt Macy 
728eda14cbcSMatt Macy 		if (os->os_encrypted && !os->os_raw_receive &&
729eda14cbcSMatt Macy 		    !os->os_next_write_raw[txg & TXG_MASK]) {
730eda14cbcSMatt Macy 			ASSERT3P(ds->ds_key_mapping, !=, NULL);
731eda14cbcSMatt Macy 			key_mapping_rele(dp->dp_spa, ds->ds_key_mapping, ds);
732eda14cbcSMatt Macy 		}
733eda14cbcSMatt Macy 
734eda14cbcSMatt Macy 		dsl_dataset_sync_done(ds, tx);
735eda14cbcSMatt Macy 	}
736eda14cbcSMatt Macy 
737eda14cbcSMatt Macy 	while ((dd = txg_list_remove(&dp->dp_dirty_dirs, txg)) != NULL) {
738eda14cbcSMatt Macy 		dsl_dir_sync(dd, tx);
739eda14cbcSMatt Macy 	}
740eda14cbcSMatt Macy 
741eda14cbcSMatt Macy 	/*
742eda14cbcSMatt Macy 	 * The MOS's space is accounted for in the pool/$MOS
743eda14cbcSMatt Macy 	 * (dp_mos_dir).  We can't modify the mos while we're syncing
744eda14cbcSMatt Macy 	 * it, so we remember the deltas and apply them here.
745eda14cbcSMatt Macy 	 */
746eda14cbcSMatt Macy 	if (dp->dp_mos_used_delta != 0 || dp->dp_mos_compressed_delta != 0 ||
747eda14cbcSMatt Macy 	    dp->dp_mos_uncompressed_delta != 0) {
748eda14cbcSMatt Macy 		dsl_dir_diduse_space(dp->dp_mos_dir, DD_USED_HEAD,
749eda14cbcSMatt Macy 		    dp->dp_mos_used_delta,
750eda14cbcSMatt Macy 		    dp->dp_mos_compressed_delta,
751eda14cbcSMatt Macy 		    dp->dp_mos_uncompressed_delta, tx);
752eda14cbcSMatt Macy 		dp->dp_mos_used_delta = 0;
753eda14cbcSMatt Macy 		dp->dp_mos_compressed_delta = 0;
754eda14cbcSMatt Macy 		dp->dp_mos_uncompressed_delta = 0;
755eda14cbcSMatt Macy 	}
756eda14cbcSMatt Macy 
757eda14cbcSMatt Macy 	if (dmu_objset_is_dirty(mos, txg)) {
758eda14cbcSMatt Macy 		dsl_pool_sync_mos(dp, tx);
759eda14cbcSMatt Macy 	}
760eda14cbcSMatt Macy 
761eda14cbcSMatt Macy 	/*
762eda14cbcSMatt Macy 	 * We have written all of the accounted dirty data, so our
763eda14cbcSMatt Macy 	 * dp_space_towrite should now be zero. However, some seldom-used
764eda14cbcSMatt Macy 	 * code paths do not adhere to this (e.g. dbuf_undirty()). Shore up
765eda14cbcSMatt Macy 	 * the accounting of any dirtied space now.
766eda14cbcSMatt Macy 	 *
767eda14cbcSMatt Macy 	 * Note that, besides any dirty data from datasets, the amount of
768eda14cbcSMatt Macy 	 * dirty data in the MOS is also accounted by the pool. Therefore,
769eda14cbcSMatt Macy 	 * we want to do this cleanup after dsl_pool_sync_mos() so we don't
770eda14cbcSMatt Macy 	 * attempt to update the accounting for the same dirty data twice.
771eda14cbcSMatt Macy 	 * (i.e. at this point we only update the accounting for the space
772eda14cbcSMatt Macy 	 * that we know that we "leaked").
773eda14cbcSMatt Macy 	 */
774eda14cbcSMatt Macy 	dsl_pool_undirty_space(dp, dp->dp_dirty_pertxg[txg & TXG_MASK], txg);
775eda14cbcSMatt Macy 
776eda14cbcSMatt Macy 	/*
777eda14cbcSMatt Macy 	 * If we modify a dataset in the same txg that we want to destroy it,
778eda14cbcSMatt Macy 	 * its dsl_dir's dd_dbuf will be dirty, and thus have a hold on it.
779eda14cbcSMatt Macy 	 * dsl_dir_destroy_check() will fail if there are unexpected holds.
780eda14cbcSMatt Macy 	 * Therefore, we want to sync the MOS (thus syncing the dd_dbuf
781eda14cbcSMatt Macy 	 * and clearing the hold on it) before we process the sync_tasks.
782eda14cbcSMatt Macy 	 * The MOS data dirtied by the sync_tasks will be synced on the next
783eda14cbcSMatt Macy 	 * pass.
784eda14cbcSMatt Macy 	 */
785eda14cbcSMatt Macy 	if (!txg_list_empty(&dp->dp_sync_tasks, txg)) {
786eda14cbcSMatt Macy 		dsl_sync_task_t *dst;
787eda14cbcSMatt Macy 		/*
788eda14cbcSMatt Macy 		 * No more sync tasks should have been added while we
789eda14cbcSMatt Macy 		 * were syncing.
790eda14cbcSMatt Macy 		 */
791eda14cbcSMatt Macy 		ASSERT3U(spa_sync_pass(dp->dp_spa), ==, 1);
792eda14cbcSMatt Macy 		while ((dst = txg_list_remove(&dp->dp_sync_tasks, txg)) != NULL)
793eda14cbcSMatt Macy 			dsl_sync_task_sync(dst, tx);
794eda14cbcSMatt Macy 	}
795eda14cbcSMatt Macy 
796eda14cbcSMatt Macy 	dmu_tx_commit(tx);
797eda14cbcSMatt Macy 
798eda14cbcSMatt Macy 	DTRACE_PROBE2(dsl_pool_sync__done, dsl_pool_t *dp, dp, uint64_t, txg);
799eda14cbcSMatt Macy }
800eda14cbcSMatt Macy 
801eda14cbcSMatt Macy void
802eda14cbcSMatt Macy dsl_pool_sync_done(dsl_pool_t *dp, uint64_t txg)
803eda14cbcSMatt Macy {
804eda14cbcSMatt Macy 	zilog_t *zilog;
805eda14cbcSMatt Macy 
806eda14cbcSMatt Macy 	while ((zilog = txg_list_head(&dp->dp_dirty_zilogs, txg))) {
807eda14cbcSMatt Macy 		dsl_dataset_t *ds = dmu_objset_ds(zilog->zl_os);
808eda14cbcSMatt Macy 		/*
809eda14cbcSMatt Macy 		 * We don't remove the zilog from the dp_dirty_zilogs
810eda14cbcSMatt Macy 		 * list until after we've cleaned it. This ensures that
811eda14cbcSMatt Macy 		 * callers of zilog_is_dirty() receive an accurate
812eda14cbcSMatt Macy 		 * answer when they are racing with the spa sync thread.
813eda14cbcSMatt Macy 		 */
814eda14cbcSMatt Macy 		zil_clean(zilog, txg);
815eda14cbcSMatt Macy 		(void) txg_list_remove_this(&dp->dp_dirty_zilogs, zilog, txg);
816eda14cbcSMatt Macy 		ASSERT(!dmu_objset_is_dirty(zilog->zl_os, txg));
817eda14cbcSMatt Macy 		dmu_buf_rele(ds->ds_dbuf, zilog);
818eda14cbcSMatt Macy 	}
819eda14cbcSMatt Macy 	ASSERT(!dmu_objset_is_dirty(dp->dp_meta_objset, txg));
820eda14cbcSMatt Macy }
821eda14cbcSMatt Macy 
822eda14cbcSMatt Macy /*
823eda14cbcSMatt Macy  * TRUE if the current thread is the tx_sync_thread or if we
824eda14cbcSMatt Macy  * are being called from SPA context during pool initialization.
825eda14cbcSMatt Macy  */
826eda14cbcSMatt Macy int
827eda14cbcSMatt Macy dsl_pool_sync_context(dsl_pool_t *dp)
828eda14cbcSMatt Macy {
829eda14cbcSMatt Macy 	return (curthread == dp->dp_tx.tx_sync_thread ||
830eda14cbcSMatt Macy 	    spa_is_initializing(dp->dp_spa) ||
831eda14cbcSMatt Macy 	    taskq_member(dp->dp_sync_taskq, curthread));
832eda14cbcSMatt Macy }
833eda14cbcSMatt Macy 
834eda14cbcSMatt Macy /*
835eda14cbcSMatt Macy  * This function returns the amount of allocatable space in the pool
836eda14cbcSMatt Macy  * minus whatever space is currently reserved by ZFS for specific
837eda14cbcSMatt Macy  * purposes. Specifically:
838eda14cbcSMatt Macy  *
839eda14cbcSMatt Macy  * 1] Any reserved SLOP space
840eda14cbcSMatt Macy  * 2] Any space used by the checkpoint
841eda14cbcSMatt Macy  * 3] Any space used for deferred frees
842eda14cbcSMatt Macy  *
843eda14cbcSMatt Macy  * The latter 2 are especially important because they are needed to
844eda14cbcSMatt Macy  * rectify the SPA's and DMU's different understanding of how much space
845eda14cbcSMatt Macy  * is used. Now the DMU is aware of that extra space tracked by the SPA
846eda14cbcSMatt Macy  * without having to maintain a separate special dir (e.g similar to
847eda14cbcSMatt Macy  * $MOS, $FREEING, and $LEAKED).
848eda14cbcSMatt Macy  *
849eda14cbcSMatt Macy  * Note: By deferred frees here, we mean the frees that were deferred
850eda14cbcSMatt Macy  * in spa_sync() after sync pass 1 (spa_deferred_bpobj), and not the
851eda14cbcSMatt Macy  * segments placed in ms_defer trees during metaslab_sync_done().
852eda14cbcSMatt Macy  */
853eda14cbcSMatt Macy uint64_t
854eda14cbcSMatt Macy dsl_pool_adjustedsize(dsl_pool_t *dp, zfs_space_check_t slop_policy)
855eda14cbcSMatt Macy {
856eda14cbcSMatt Macy 	spa_t *spa = dp->dp_spa;
857eda14cbcSMatt Macy 	uint64_t space, resv, adjustedsize;
858eda14cbcSMatt Macy 	uint64_t spa_deferred_frees =
859eda14cbcSMatt Macy 	    spa->spa_deferred_bpobj.bpo_phys->bpo_bytes;
860eda14cbcSMatt Macy 
861eda14cbcSMatt Macy 	space = spa_get_dspace(spa)
862eda14cbcSMatt Macy 	    - spa_get_checkpoint_space(spa) - spa_deferred_frees;
863eda14cbcSMatt Macy 	resv = spa_get_slop_space(spa);
864eda14cbcSMatt Macy 
865eda14cbcSMatt Macy 	switch (slop_policy) {
866eda14cbcSMatt Macy 	case ZFS_SPACE_CHECK_NORMAL:
867eda14cbcSMatt Macy 		break;
868eda14cbcSMatt Macy 	case ZFS_SPACE_CHECK_RESERVED:
869eda14cbcSMatt Macy 		resv >>= 1;
870eda14cbcSMatt Macy 		break;
871eda14cbcSMatt Macy 	case ZFS_SPACE_CHECK_EXTRA_RESERVED:
872eda14cbcSMatt Macy 		resv >>= 2;
873eda14cbcSMatt Macy 		break;
874eda14cbcSMatt Macy 	case ZFS_SPACE_CHECK_NONE:
875eda14cbcSMatt Macy 		resv = 0;
876eda14cbcSMatt Macy 		break;
877eda14cbcSMatt Macy 	default:
878eda14cbcSMatt Macy 		panic("invalid slop policy value: %d", slop_policy);
879eda14cbcSMatt Macy 		break;
880eda14cbcSMatt Macy 	}
881eda14cbcSMatt Macy 	adjustedsize = (space >= resv) ? (space - resv) : 0;
882eda14cbcSMatt Macy 
883eda14cbcSMatt Macy 	return (adjustedsize);
884eda14cbcSMatt Macy }
885eda14cbcSMatt Macy 
886eda14cbcSMatt Macy uint64_t
887eda14cbcSMatt Macy dsl_pool_unreserved_space(dsl_pool_t *dp, zfs_space_check_t slop_policy)
888eda14cbcSMatt Macy {
889eda14cbcSMatt Macy 	uint64_t poolsize = dsl_pool_adjustedsize(dp, slop_policy);
890eda14cbcSMatt Macy 	uint64_t deferred =
891eda14cbcSMatt Macy 	    metaslab_class_get_deferred(spa_normal_class(dp->dp_spa));
892eda14cbcSMatt Macy 	uint64_t quota = (poolsize >= deferred) ? (poolsize - deferred) : 0;
893eda14cbcSMatt Macy 	return (quota);
894eda14cbcSMatt Macy }
895eda14cbcSMatt Macy 
896eda14cbcSMatt Macy boolean_t
897eda14cbcSMatt Macy dsl_pool_need_dirty_delay(dsl_pool_t *dp)
898eda14cbcSMatt Macy {
899eda14cbcSMatt Macy 	uint64_t delay_min_bytes =
900eda14cbcSMatt Macy 	    zfs_dirty_data_max * zfs_delay_min_dirty_percent / 100;
901eda14cbcSMatt Macy 	uint64_t dirty_min_bytes =
902eda14cbcSMatt Macy 	    zfs_dirty_data_max * zfs_dirty_data_sync_percent / 100;
903eda14cbcSMatt Macy 	uint64_t dirty;
904eda14cbcSMatt Macy 
905eda14cbcSMatt Macy 	mutex_enter(&dp->dp_lock);
906eda14cbcSMatt Macy 	dirty = dp->dp_dirty_total;
907eda14cbcSMatt Macy 	mutex_exit(&dp->dp_lock);
908eda14cbcSMatt Macy 	if (dirty > dirty_min_bytes)
909eda14cbcSMatt Macy 		txg_kick(dp);
910eda14cbcSMatt Macy 	return (dirty > delay_min_bytes);
911eda14cbcSMatt Macy }
912eda14cbcSMatt Macy 
913eda14cbcSMatt Macy void
914eda14cbcSMatt Macy dsl_pool_dirty_space(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx)
915eda14cbcSMatt Macy {
916eda14cbcSMatt Macy 	if (space > 0) {
917eda14cbcSMatt Macy 		mutex_enter(&dp->dp_lock);
918eda14cbcSMatt Macy 		dp->dp_dirty_pertxg[tx->tx_txg & TXG_MASK] += space;
919eda14cbcSMatt Macy 		dsl_pool_dirty_delta(dp, space);
920eda14cbcSMatt Macy 		mutex_exit(&dp->dp_lock);
921eda14cbcSMatt Macy 	}
922eda14cbcSMatt Macy }
923eda14cbcSMatt Macy 
924eda14cbcSMatt Macy void
925eda14cbcSMatt Macy dsl_pool_undirty_space(dsl_pool_t *dp, int64_t space, uint64_t txg)
926eda14cbcSMatt Macy {
927eda14cbcSMatt Macy 	ASSERT3S(space, >=, 0);
928eda14cbcSMatt Macy 	if (space == 0)
929eda14cbcSMatt Macy 		return;
930eda14cbcSMatt Macy 
931eda14cbcSMatt Macy 	mutex_enter(&dp->dp_lock);
932eda14cbcSMatt Macy 	if (dp->dp_dirty_pertxg[txg & TXG_MASK] < space) {
933eda14cbcSMatt Macy 		/* XXX writing something we didn't dirty? */
934eda14cbcSMatt Macy 		space = dp->dp_dirty_pertxg[txg & TXG_MASK];
935eda14cbcSMatt Macy 	}
936eda14cbcSMatt Macy 	ASSERT3U(dp->dp_dirty_pertxg[txg & TXG_MASK], >=, space);
937eda14cbcSMatt Macy 	dp->dp_dirty_pertxg[txg & TXG_MASK] -= space;
938eda14cbcSMatt Macy 	ASSERT3U(dp->dp_dirty_total, >=, space);
939eda14cbcSMatt Macy 	dsl_pool_dirty_delta(dp, -space);
940eda14cbcSMatt Macy 	mutex_exit(&dp->dp_lock);
941eda14cbcSMatt Macy }
942eda14cbcSMatt Macy 
943eda14cbcSMatt Macy /* ARGSUSED */
944eda14cbcSMatt Macy static int
945eda14cbcSMatt Macy upgrade_clones_cb(dsl_pool_t *dp, dsl_dataset_t *hds, void *arg)
946eda14cbcSMatt Macy {
947eda14cbcSMatt Macy 	dmu_tx_t *tx = arg;
948eda14cbcSMatt Macy 	dsl_dataset_t *ds, *prev = NULL;
949eda14cbcSMatt Macy 	int err;
950eda14cbcSMatt Macy 
951eda14cbcSMatt Macy 	err = dsl_dataset_hold_obj(dp, hds->ds_object, FTAG, &ds);
952eda14cbcSMatt Macy 	if (err)
953eda14cbcSMatt Macy 		return (err);
954eda14cbcSMatt Macy 
955eda14cbcSMatt Macy 	while (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
956eda14cbcSMatt Macy 		err = dsl_dataset_hold_obj(dp,
957eda14cbcSMatt Macy 		    dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &prev);
958eda14cbcSMatt Macy 		if (err) {
959eda14cbcSMatt Macy 			dsl_dataset_rele(ds, FTAG);
960eda14cbcSMatt Macy 			return (err);
961eda14cbcSMatt Macy 		}
962eda14cbcSMatt Macy 
963eda14cbcSMatt Macy 		if (dsl_dataset_phys(prev)->ds_next_snap_obj != ds->ds_object)
964eda14cbcSMatt Macy 			break;
965eda14cbcSMatt Macy 		dsl_dataset_rele(ds, FTAG);
966eda14cbcSMatt Macy 		ds = prev;
967eda14cbcSMatt Macy 		prev = NULL;
968eda14cbcSMatt Macy 	}
969eda14cbcSMatt Macy 
970eda14cbcSMatt Macy 	if (prev == NULL) {
971eda14cbcSMatt Macy 		prev = dp->dp_origin_snap;
972eda14cbcSMatt Macy 
973eda14cbcSMatt Macy 		/*
974eda14cbcSMatt Macy 		 * The $ORIGIN can't have any data, or the accounting
975eda14cbcSMatt Macy 		 * will be wrong.
976eda14cbcSMatt Macy 		 */
977eda14cbcSMatt Macy 		rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
978eda14cbcSMatt Macy 		ASSERT0(dsl_dataset_phys(prev)->ds_bp.blk_birth);
979eda14cbcSMatt Macy 		rrw_exit(&ds->ds_bp_rwlock, FTAG);
980eda14cbcSMatt Macy 
981eda14cbcSMatt Macy 		/* The origin doesn't get attached to itself */
982eda14cbcSMatt Macy 		if (ds->ds_object == prev->ds_object) {
983eda14cbcSMatt Macy 			dsl_dataset_rele(ds, FTAG);
984eda14cbcSMatt Macy 			return (0);
985eda14cbcSMatt Macy 		}
986eda14cbcSMatt Macy 
987eda14cbcSMatt Macy 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
988eda14cbcSMatt Macy 		dsl_dataset_phys(ds)->ds_prev_snap_obj = prev->ds_object;
989eda14cbcSMatt Macy 		dsl_dataset_phys(ds)->ds_prev_snap_txg =
990eda14cbcSMatt Macy 		    dsl_dataset_phys(prev)->ds_creation_txg;
991eda14cbcSMatt Macy 
992eda14cbcSMatt Macy 		dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
993eda14cbcSMatt Macy 		dsl_dir_phys(ds->ds_dir)->dd_origin_obj = prev->ds_object;
994eda14cbcSMatt Macy 
995eda14cbcSMatt Macy 		dmu_buf_will_dirty(prev->ds_dbuf, tx);
996eda14cbcSMatt Macy 		dsl_dataset_phys(prev)->ds_num_children++;
997eda14cbcSMatt Macy 
998eda14cbcSMatt Macy 		if (dsl_dataset_phys(ds)->ds_next_snap_obj == 0) {
999eda14cbcSMatt Macy 			ASSERT(ds->ds_prev == NULL);
1000eda14cbcSMatt Macy 			VERIFY0(dsl_dataset_hold_obj(dp,
1001eda14cbcSMatt Macy 			    dsl_dataset_phys(ds)->ds_prev_snap_obj,
1002eda14cbcSMatt Macy 			    ds, &ds->ds_prev));
1003eda14cbcSMatt Macy 		}
1004eda14cbcSMatt Macy 	}
1005eda14cbcSMatt Macy 
1006eda14cbcSMatt Macy 	ASSERT3U(dsl_dir_phys(ds->ds_dir)->dd_origin_obj, ==, prev->ds_object);
1007eda14cbcSMatt Macy 	ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_obj, ==, prev->ds_object);
1008eda14cbcSMatt Macy 
1009eda14cbcSMatt Macy 	if (dsl_dataset_phys(prev)->ds_next_clones_obj == 0) {
1010eda14cbcSMatt Macy 		dmu_buf_will_dirty(prev->ds_dbuf, tx);
1011eda14cbcSMatt Macy 		dsl_dataset_phys(prev)->ds_next_clones_obj =
1012eda14cbcSMatt Macy 		    zap_create(dp->dp_meta_objset,
1013eda14cbcSMatt Macy 		    DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
1014eda14cbcSMatt Macy 	}
1015eda14cbcSMatt Macy 	VERIFY0(zap_add_int(dp->dp_meta_objset,
1016eda14cbcSMatt Macy 	    dsl_dataset_phys(prev)->ds_next_clones_obj, ds->ds_object, tx));
1017eda14cbcSMatt Macy 
1018eda14cbcSMatt Macy 	dsl_dataset_rele(ds, FTAG);
1019eda14cbcSMatt Macy 	if (prev != dp->dp_origin_snap)
1020eda14cbcSMatt Macy 		dsl_dataset_rele(prev, FTAG);
1021eda14cbcSMatt Macy 	return (0);
1022eda14cbcSMatt Macy }
1023eda14cbcSMatt Macy 
1024eda14cbcSMatt Macy void
1025eda14cbcSMatt Macy dsl_pool_upgrade_clones(dsl_pool_t *dp, dmu_tx_t *tx)
1026eda14cbcSMatt Macy {
1027eda14cbcSMatt Macy 	ASSERT(dmu_tx_is_syncing(tx));
1028eda14cbcSMatt Macy 	ASSERT(dp->dp_origin_snap != NULL);
1029eda14cbcSMatt Macy 
1030eda14cbcSMatt Macy 	VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj, upgrade_clones_cb,
1031eda14cbcSMatt Macy 	    tx, DS_FIND_CHILDREN | DS_FIND_SERIALIZE));
1032eda14cbcSMatt Macy }
1033eda14cbcSMatt Macy 
1034eda14cbcSMatt Macy /* ARGSUSED */
1035eda14cbcSMatt Macy static int
1036eda14cbcSMatt Macy upgrade_dir_clones_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
1037eda14cbcSMatt Macy {
1038eda14cbcSMatt Macy 	dmu_tx_t *tx = arg;
1039eda14cbcSMatt Macy 	objset_t *mos = dp->dp_meta_objset;
1040eda14cbcSMatt Macy 
1041eda14cbcSMatt Macy 	if (dsl_dir_phys(ds->ds_dir)->dd_origin_obj != 0) {
1042eda14cbcSMatt Macy 		dsl_dataset_t *origin;
1043eda14cbcSMatt Macy 
1044eda14cbcSMatt Macy 		VERIFY0(dsl_dataset_hold_obj(dp,
1045eda14cbcSMatt Macy 		    dsl_dir_phys(ds->ds_dir)->dd_origin_obj, FTAG, &origin));
1046eda14cbcSMatt Macy 
1047eda14cbcSMatt Macy 		if (dsl_dir_phys(origin->ds_dir)->dd_clones == 0) {
1048eda14cbcSMatt Macy 			dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx);
1049eda14cbcSMatt Macy 			dsl_dir_phys(origin->ds_dir)->dd_clones =
1050eda14cbcSMatt Macy 			    zap_create(mos, DMU_OT_DSL_CLONES, DMU_OT_NONE,
1051eda14cbcSMatt Macy 			    0, tx);
1052eda14cbcSMatt Macy 		}
1053eda14cbcSMatt Macy 
1054eda14cbcSMatt Macy 		VERIFY0(zap_add_int(dp->dp_meta_objset,
1055eda14cbcSMatt Macy 		    dsl_dir_phys(origin->ds_dir)->dd_clones,
1056eda14cbcSMatt Macy 		    ds->ds_object, tx));
1057eda14cbcSMatt Macy 
1058eda14cbcSMatt Macy 		dsl_dataset_rele(origin, FTAG);
1059eda14cbcSMatt Macy 	}
1060eda14cbcSMatt Macy 	return (0);
1061eda14cbcSMatt Macy }
1062eda14cbcSMatt Macy 
1063eda14cbcSMatt Macy void
1064eda14cbcSMatt Macy dsl_pool_upgrade_dir_clones(dsl_pool_t *dp, dmu_tx_t *tx)
1065eda14cbcSMatt Macy {
1066eda14cbcSMatt Macy 	uint64_t obj;
1067eda14cbcSMatt Macy 
1068eda14cbcSMatt Macy 	ASSERT(dmu_tx_is_syncing(tx));
1069eda14cbcSMatt Macy 
1070eda14cbcSMatt Macy 	(void) dsl_dir_create_sync(dp, dp->dp_root_dir, FREE_DIR_NAME, tx);
1071eda14cbcSMatt Macy 	VERIFY0(dsl_pool_open_special_dir(dp,
1072eda14cbcSMatt Macy 	    FREE_DIR_NAME, &dp->dp_free_dir));
1073eda14cbcSMatt Macy 
1074eda14cbcSMatt Macy 	/*
1075eda14cbcSMatt Macy 	 * We can't use bpobj_alloc(), because spa_version() still
1076eda14cbcSMatt Macy 	 * returns the old version, and we need a new-version bpobj with
1077eda14cbcSMatt Macy 	 * subobj support.  So call dmu_object_alloc() directly.
1078eda14cbcSMatt Macy 	 */
1079eda14cbcSMatt Macy 	obj = dmu_object_alloc(dp->dp_meta_objset, DMU_OT_BPOBJ,
1080eda14cbcSMatt Macy 	    SPA_OLD_MAXBLOCKSIZE, DMU_OT_BPOBJ_HDR, sizeof (bpobj_phys_t), tx);
1081eda14cbcSMatt Macy 	VERIFY0(zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1082eda14cbcSMatt Macy 	    DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj, tx));
1083eda14cbcSMatt Macy 	VERIFY0(bpobj_open(&dp->dp_free_bpobj, dp->dp_meta_objset, obj));
1084eda14cbcSMatt Macy 
1085eda14cbcSMatt Macy 	VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
1086eda14cbcSMatt Macy 	    upgrade_dir_clones_cb, tx, DS_FIND_CHILDREN | DS_FIND_SERIALIZE));
1087eda14cbcSMatt Macy }
1088eda14cbcSMatt Macy 
1089eda14cbcSMatt Macy void
1090eda14cbcSMatt Macy dsl_pool_create_origin(dsl_pool_t *dp, dmu_tx_t *tx)
1091eda14cbcSMatt Macy {
1092eda14cbcSMatt Macy 	uint64_t dsobj;
1093eda14cbcSMatt Macy 	dsl_dataset_t *ds;
1094eda14cbcSMatt Macy 
1095eda14cbcSMatt Macy 	ASSERT(dmu_tx_is_syncing(tx));
1096eda14cbcSMatt Macy 	ASSERT(dp->dp_origin_snap == NULL);
1097eda14cbcSMatt Macy 	ASSERT(rrw_held(&dp->dp_config_rwlock, RW_WRITER));
1098eda14cbcSMatt Macy 
1099eda14cbcSMatt Macy 	/* create the origin dir, ds, & snap-ds */
1100eda14cbcSMatt Macy 	dsobj = dsl_dataset_create_sync(dp->dp_root_dir, ORIGIN_DIR_NAME,
1101eda14cbcSMatt Macy 	    NULL, 0, kcred, NULL, tx);
1102eda14cbcSMatt Macy 	VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
1103eda14cbcSMatt Macy 	dsl_dataset_snapshot_sync_impl(ds, ORIGIN_DIR_NAME, tx);
1104eda14cbcSMatt Macy 	VERIFY0(dsl_dataset_hold_obj(dp, dsl_dataset_phys(ds)->ds_prev_snap_obj,
1105eda14cbcSMatt Macy 	    dp, &dp->dp_origin_snap));
1106eda14cbcSMatt Macy 	dsl_dataset_rele(ds, FTAG);
1107eda14cbcSMatt Macy }
1108eda14cbcSMatt Macy 
1109eda14cbcSMatt Macy taskq_t *
1110eda14cbcSMatt Macy dsl_pool_zrele_taskq(dsl_pool_t *dp)
1111eda14cbcSMatt Macy {
1112eda14cbcSMatt Macy 	return (dp->dp_zrele_taskq);
1113eda14cbcSMatt Macy }
1114eda14cbcSMatt Macy 
1115eda14cbcSMatt Macy taskq_t *
1116eda14cbcSMatt Macy dsl_pool_unlinked_drain_taskq(dsl_pool_t *dp)
1117eda14cbcSMatt Macy {
1118eda14cbcSMatt Macy 	return (dp->dp_unlinked_drain_taskq);
1119eda14cbcSMatt Macy }
1120eda14cbcSMatt Macy 
1121eda14cbcSMatt Macy /*
1122eda14cbcSMatt Macy  * Walk through the pool-wide zap object of temporary snapshot user holds
1123eda14cbcSMatt Macy  * and release them.
1124eda14cbcSMatt Macy  */
1125eda14cbcSMatt Macy void
1126eda14cbcSMatt Macy dsl_pool_clean_tmp_userrefs(dsl_pool_t *dp)
1127eda14cbcSMatt Macy {
1128eda14cbcSMatt Macy 	zap_attribute_t za;
1129eda14cbcSMatt Macy 	zap_cursor_t zc;
1130eda14cbcSMatt Macy 	objset_t *mos = dp->dp_meta_objset;
1131eda14cbcSMatt Macy 	uint64_t zapobj = dp->dp_tmp_userrefs_obj;
1132eda14cbcSMatt Macy 	nvlist_t *holds;
1133eda14cbcSMatt Macy 
1134eda14cbcSMatt Macy 	if (zapobj == 0)
1135eda14cbcSMatt Macy 		return;
1136eda14cbcSMatt Macy 	ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
1137eda14cbcSMatt Macy 
1138eda14cbcSMatt Macy 	holds = fnvlist_alloc();
1139eda14cbcSMatt Macy 
1140eda14cbcSMatt Macy 	for (zap_cursor_init(&zc, mos, zapobj);
1141eda14cbcSMatt Macy 	    zap_cursor_retrieve(&zc, &za) == 0;
1142eda14cbcSMatt Macy 	    zap_cursor_advance(&zc)) {
1143eda14cbcSMatt Macy 		char *htag;
1144eda14cbcSMatt Macy 		nvlist_t *tags;
1145eda14cbcSMatt Macy 
1146eda14cbcSMatt Macy 		htag = strchr(za.za_name, '-');
1147eda14cbcSMatt Macy 		*htag = '\0';
1148eda14cbcSMatt Macy 		++htag;
1149eda14cbcSMatt Macy 		if (nvlist_lookup_nvlist(holds, za.za_name, &tags) != 0) {
1150eda14cbcSMatt Macy 			tags = fnvlist_alloc();
1151eda14cbcSMatt Macy 			fnvlist_add_boolean(tags, htag);
1152eda14cbcSMatt Macy 			fnvlist_add_nvlist(holds, za.za_name, tags);
1153eda14cbcSMatt Macy 			fnvlist_free(tags);
1154eda14cbcSMatt Macy 		} else {
1155eda14cbcSMatt Macy 			fnvlist_add_boolean(tags, htag);
1156eda14cbcSMatt Macy 		}
1157eda14cbcSMatt Macy 	}
1158eda14cbcSMatt Macy 	dsl_dataset_user_release_tmp(dp, holds);
1159eda14cbcSMatt Macy 	fnvlist_free(holds);
1160eda14cbcSMatt Macy 	zap_cursor_fini(&zc);
1161eda14cbcSMatt Macy }
1162eda14cbcSMatt Macy 
1163eda14cbcSMatt Macy /*
1164eda14cbcSMatt Macy  * Create the pool-wide zap object for storing temporary snapshot holds.
1165eda14cbcSMatt Macy  */
1166eda14cbcSMatt Macy static void
1167eda14cbcSMatt Macy dsl_pool_user_hold_create_obj(dsl_pool_t *dp, dmu_tx_t *tx)
1168eda14cbcSMatt Macy {
1169eda14cbcSMatt Macy 	objset_t *mos = dp->dp_meta_objset;
1170eda14cbcSMatt Macy 
1171eda14cbcSMatt Macy 	ASSERT(dp->dp_tmp_userrefs_obj == 0);
1172eda14cbcSMatt Macy 	ASSERT(dmu_tx_is_syncing(tx));
1173eda14cbcSMatt Macy 
1174eda14cbcSMatt Macy 	dp->dp_tmp_userrefs_obj = zap_create_link(mos, DMU_OT_USERREFS,
1175eda14cbcSMatt Macy 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_TMP_USERREFS, tx);
1176eda14cbcSMatt Macy }
1177eda14cbcSMatt Macy 
1178eda14cbcSMatt Macy static int
1179eda14cbcSMatt Macy dsl_pool_user_hold_rele_impl(dsl_pool_t *dp, uint64_t dsobj,
1180eda14cbcSMatt Macy     const char *tag, uint64_t now, dmu_tx_t *tx, boolean_t holding)
1181eda14cbcSMatt Macy {
1182eda14cbcSMatt Macy 	objset_t *mos = dp->dp_meta_objset;
1183eda14cbcSMatt Macy 	uint64_t zapobj = dp->dp_tmp_userrefs_obj;
1184eda14cbcSMatt Macy 	char *name;
1185eda14cbcSMatt Macy 	int error;
1186eda14cbcSMatt Macy 
1187eda14cbcSMatt Macy 	ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
1188eda14cbcSMatt Macy 	ASSERT(dmu_tx_is_syncing(tx));
1189eda14cbcSMatt Macy 
1190eda14cbcSMatt Macy 	/*
1191eda14cbcSMatt Macy 	 * If the pool was created prior to SPA_VERSION_USERREFS, the
1192eda14cbcSMatt Macy 	 * zap object for temporary holds might not exist yet.
1193eda14cbcSMatt Macy 	 */
1194eda14cbcSMatt Macy 	if (zapobj == 0) {
1195eda14cbcSMatt Macy 		if (holding) {
1196eda14cbcSMatt Macy 			dsl_pool_user_hold_create_obj(dp, tx);
1197eda14cbcSMatt Macy 			zapobj = dp->dp_tmp_userrefs_obj;
1198eda14cbcSMatt Macy 		} else {
1199eda14cbcSMatt Macy 			return (SET_ERROR(ENOENT));
1200eda14cbcSMatt Macy 		}
1201eda14cbcSMatt Macy 	}
1202eda14cbcSMatt Macy 
1203eda14cbcSMatt Macy 	name = kmem_asprintf("%llx-%s", (u_longlong_t)dsobj, tag);
1204eda14cbcSMatt Macy 	if (holding)
1205eda14cbcSMatt Macy 		error = zap_add(mos, zapobj, name, 8, 1, &now, tx);
1206eda14cbcSMatt Macy 	else
1207eda14cbcSMatt Macy 		error = zap_remove(mos, zapobj, name, tx);
1208eda14cbcSMatt Macy 	kmem_strfree(name);
1209eda14cbcSMatt Macy 
1210eda14cbcSMatt Macy 	return (error);
1211eda14cbcSMatt Macy }
1212eda14cbcSMatt Macy 
1213eda14cbcSMatt Macy /*
1214eda14cbcSMatt Macy  * Add a temporary hold for the given dataset object and tag.
1215eda14cbcSMatt Macy  */
1216eda14cbcSMatt Macy int
1217eda14cbcSMatt Macy dsl_pool_user_hold(dsl_pool_t *dp, uint64_t dsobj, const char *tag,
1218eda14cbcSMatt Macy     uint64_t now, dmu_tx_t *tx)
1219eda14cbcSMatt Macy {
1220eda14cbcSMatt Macy 	return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, now, tx, B_TRUE));
1221eda14cbcSMatt Macy }
1222eda14cbcSMatt Macy 
1223eda14cbcSMatt Macy /*
1224eda14cbcSMatt Macy  * Release a temporary hold for the given dataset object and tag.
1225eda14cbcSMatt Macy  */
1226eda14cbcSMatt Macy int
1227eda14cbcSMatt Macy dsl_pool_user_release(dsl_pool_t *dp, uint64_t dsobj, const char *tag,
1228eda14cbcSMatt Macy     dmu_tx_t *tx)
1229eda14cbcSMatt Macy {
1230eda14cbcSMatt Macy 	return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, 0,
1231eda14cbcSMatt Macy 	    tx, B_FALSE));
1232eda14cbcSMatt Macy }
1233eda14cbcSMatt Macy 
1234eda14cbcSMatt Macy /*
1235eda14cbcSMatt Macy  * DSL Pool Configuration Lock
1236eda14cbcSMatt Macy  *
1237eda14cbcSMatt Macy  * The dp_config_rwlock protects against changes to DSL state (e.g. dataset
1238eda14cbcSMatt Macy  * creation / destruction / rename / property setting).  It must be held for
1239eda14cbcSMatt Macy  * read to hold a dataset or dsl_dir.  I.e. you must call
1240eda14cbcSMatt Macy  * dsl_pool_config_enter() or dsl_pool_hold() before calling
1241eda14cbcSMatt Macy  * dsl_{dataset,dir}_hold{_obj}.  In most circumstances, the dp_config_rwlock
1242eda14cbcSMatt Macy  * must be held continuously until all datasets and dsl_dirs are released.
1243eda14cbcSMatt Macy  *
1244eda14cbcSMatt Macy  * The only exception to this rule is that if a "long hold" is placed on
1245eda14cbcSMatt Macy  * a dataset, then the dp_config_rwlock may be dropped while the dataset
1246eda14cbcSMatt Macy  * is still held.  The long hold will prevent the dataset from being
1247eda14cbcSMatt Macy  * destroyed -- the destroy will fail with EBUSY.  A long hold can be
1248eda14cbcSMatt Macy  * obtained by calling dsl_dataset_long_hold(), or by "owning" a dataset
1249eda14cbcSMatt Macy  * (by calling dsl_{dataset,objset}_{try}own{_obj}).
1250eda14cbcSMatt Macy  *
1251eda14cbcSMatt Macy  * Legitimate long-holders (including owners) should be long-running, cancelable
1252eda14cbcSMatt Macy  * tasks that should cause "zfs destroy" to fail.  This includes DMU
1253eda14cbcSMatt Macy  * consumers (i.e. a ZPL filesystem being mounted or ZVOL being open),
1254eda14cbcSMatt Macy  * "zfs send", and "zfs diff".  There are several other long-holders whose
1255eda14cbcSMatt Macy  * uses are suboptimal (e.g. "zfs promote", and zil_suspend()).
1256eda14cbcSMatt Macy  *
1257eda14cbcSMatt Macy  * The usual formula for long-holding would be:
1258eda14cbcSMatt Macy  * dsl_pool_hold()
1259eda14cbcSMatt Macy  * dsl_dataset_hold()
1260eda14cbcSMatt Macy  * ... perform checks ...
1261eda14cbcSMatt Macy  * dsl_dataset_long_hold()
1262eda14cbcSMatt Macy  * dsl_pool_rele()
1263eda14cbcSMatt Macy  * ... perform long-running task ...
1264eda14cbcSMatt Macy  * dsl_dataset_long_rele()
1265eda14cbcSMatt Macy  * dsl_dataset_rele()
1266eda14cbcSMatt Macy  *
1267eda14cbcSMatt Macy  * Note that when the long hold is released, the dataset is still held but
1268eda14cbcSMatt Macy  * the pool is not held.  The dataset may change arbitrarily during this time
1269eda14cbcSMatt Macy  * (e.g. it could be destroyed).  Therefore you shouldn't do anything to the
1270eda14cbcSMatt Macy  * dataset except release it.
1271eda14cbcSMatt Macy  *
12727877fdebSMatt Macy  * Operations generally fall somewhere into the following taxonomy:
12737877fdebSMatt Macy  *
12747877fdebSMatt Macy  *                              Read-Only             Modifying
12757877fdebSMatt Macy  *
12767877fdebSMatt Macy  *    Dataset Layer / MOS        zfs get             zfs destroy
12777877fdebSMatt Macy  *
12787877fdebSMatt Macy  *     Individual Dataset         read()                write()
12797877fdebSMatt Macy  *
12807877fdebSMatt Macy  *
12817877fdebSMatt Macy  * Dataset Layer Operations
1282eda14cbcSMatt Macy  *
1283eda14cbcSMatt Macy  * Modifying operations should generally use dsl_sync_task().  The synctask
1284eda14cbcSMatt Macy  * infrastructure enforces proper locking strategy with respect to the
1285eda14cbcSMatt Macy  * dp_config_rwlock.  See the comment above dsl_sync_task() for details.
1286eda14cbcSMatt Macy  *
1287eda14cbcSMatt Macy  * Read-only operations will manually hold the pool, then the dataset, obtain
1288eda14cbcSMatt Macy  * information from the dataset, then release the pool and dataset.
1289eda14cbcSMatt Macy  * dmu_objset_{hold,rele}() are convenience routines that also do the pool
1290eda14cbcSMatt Macy  * hold/rele.
12917877fdebSMatt Macy  *
12927877fdebSMatt Macy  *
12937877fdebSMatt Macy  * Operations On Individual Datasets
12947877fdebSMatt Macy  *
12957877fdebSMatt Macy  * Objects _within_ an objset should only be modified by the current 'owner'
12967877fdebSMatt Macy  * of the objset to prevent incorrect concurrent modification. Thus, use
12977877fdebSMatt Macy  * {dmu_objset,dsl_dataset}_own to mark some entity as the current owner,
12987877fdebSMatt Macy  * and fail with EBUSY if there is already an owner. The owner can then
12997877fdebSMatt Macy  * implement its own locking strategy, independent of the dataset layer's
13007877fdebSMatt Macy  * locking infrastructure.
13017877fdebSMatt Macy  * (E.g., the ZPL has its own set of locks to control concurrency. A regular
13027877fdebSMatt Macy  *  vnop will not reach into the dataset layer).
13037877fdebSMatt Macy  *
13047877fdebSMatt Macy  * Ideally, objects would also only be read by the objset’s owner, so that we
13057877fdebSMatt Macy  * don’t observe state mid-modification.
13067877fdebSMatt Macy  * (E.g. the ZPL is creating a new object and linking it into a directory; if
13077877fdebSMatt Macy  * you don’t coordinate with the ZPL to hold ZPL-level locks, you could see an
13087877fdebSMatt Macy  * intermediate state.  The ioctl level violates this but in pretty benign
13097877fdebSMatt Macy  * ways, e.g. reading the zpl props object.)
1310eda14cbcSMatt Macy  */
1311eda14cbcSMatt Macy 
1312eda14cbcSMatt Macy int
1313eda14cbcSMatt Macy dsl_pool_hold(const char *name, void *tag, dsl_pool_t **dp)
1314eda14cbcSMatt Macy {
1315eda14cbcSMatt Macy 	spa_t *spa;
1316eda14cbcSMatt Macy 	int error;
1317eda14cbcSMatt Macy 
1318eda14cbcSMatt Macy 	error = spa_open(name, &spa, tag);
1319eda14cbcSMatt Macy 	if (error == 0) {
1320eda14cbcSMatt Macy 		*dp = spa_get_dsl(spa);
1321eda14cbcSMatt Macy 		dsl_pool_config_enter(*dp, tag);
1322eda14cbcSMatt Macy 	}
1323eda14cbcSMatt Macy 	return (error);
1324eda14cbcSMatt Macy }
1325eda14cbcSMatt Macy 
1326eda14cbcSMatt Macy void
1327eda14cbcSMatt Macy dsl_pool_rele(dsl_pool_t *dp, void *tag)
1328eda14cbcSMatt Macy {
1329eda14cbcSMatt Macy 	dsl_pool_config_exit(dp, tag);
1330eda14cbcSMatt Macy 	spa_close(dp->dp_spa, tag);
1331eda14cbcSMatt Macy }
1332eda14cbcSMatt Macy 
1333eda14cbcSMatt Macy void
1334eda14cbcSMatt Macy dsl_pool_config_enter(dsl_pool_t *dp, void *tag)
1335eda14cbcSMatt Macy {
1336eda14cbcSMatt Macy 	/*
1337eda14cbcSMatt Macy 	 * We use a "reentrant" reader-writer lock, but not reentrantly.
1338eda14cbcSMatt Macy 	 *
1339eda14cbcSMatt Macy 	 * The rrwlock can (with the track_all flag) track all reading threads,
1340eda14cbcSMatt Macy 	 * which is very useful for debugging which code path failed to release
1341eda14cbcSMatt Macy 	 * the lock, and for verifying that the *current* thread does hold
1342eda14cbcSMatt Macy 	 * the lock.
1343eda14cbcSMatt Macy 	 *
1344eda14cbcSMatt Macy 	 * (Unlike a rwlock, which knows that N threads hold it for
1345eda14cbcSMatt Macy 	 * read, but not *which* threads, so rw_held(RW_READER) returns TRUE
1346eda14cbcSMatt Macy 	 * if any thread holds it for read, even if this thread doesn't).
1347eda14cbcSMatt Macy 	 */
1348eda14cbcSMatt Macy 	ASSERT(!rrw_held(&dp->dp_config_rwlock, RW_READER));
1349eda14cbcSMatt Macy 	rrw_enter(&dp->dp_config_rwlock, RW_READER, tag);
1350eda14cbcSMatt Macy }
1351eda14cbcSMatt Macy 
1352eda14cbcSMatt Macy void
1353eda14cbcSMatt Macy dsl_pool_config_enter_prio(dsl_pool_t *dp, void *tag)
1354eda14cbcSMatt Macy {
1355eda14cbcSMatt Macy 	ASSERT(!rrw_held(&dp->dp_config_rwlock, RW_READER));
1356eda14cbcSMatt Macy 	rrw_enter_read_prio(&dp->dp_config_rwlock, tag);
1357eda14cbcSMatt Macy }
1358eda14cbcSMatt Macy 
1359eda14cbcSMatt Macy void
1360eda14cbcSMatt Macy dsl_pool_config_exit(dsl_pool_t *dp, void *tag)
1361eda14cbcSMatt Macy {
1362eda14cbcSMatt Macy 	rrw_exit(&dp->dp_config_rwlock, tag);
1363eda14cbcSMatt Macy }
1364eda14cbcSMatt Macy 
1365eda14cbcSMatt Macy boolean_t
1366eda14cbcSMatt Macy dsl_pool_config_held(dsl_pool_t *dp)
1367eda14cbcSMatt Macy {
1368eda14cbcSMatt Macy 	return (RRW_LOCK_HELD(&dp->dp_config_rwlock));
1369eda14cbcSMatt Macy }
1370eda14cbcSMatt Macy 
1371eda14cbcSMatt Macy boolean_t
1372eda14cbcSMatt Macy dsl_pool_config_held_writer(dsl_pool_t *dp)
1373eda14cbcSMatt Macy {
1374eda14cbcSMatt Macy 	return (RRW_WRITE_HELD(&dp->dp_config_rwlock));
1375eda14cbcSMatt Macy }
1376eda14cbcSMatt Macy 
1377eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_pool_config_enter);
1378eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_pool_config_exit);
1379eda14cbcSMatt Macy 
1380eda14cbcSMatt Macy /* BEGIN CSTYLED */
1381eda14cbcSMatt Macy /* zfs_dirty_data_max_percent only applied at module load in arc_init(). */
1382eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs, zfs_, dirty_data_max_percent, INT, ZMOD_RD,
1383eda14cbcSMatt Macy 	"Max percent of RAM allowed to be dirty");
1384eda14cbcSMatt Macy 
1385eda14cbcSMatt Macy /* zfs_dirty_data_max_max_percent only applied at module load in arc_init(). */
1386eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs, zfs_, dirty_data_max_max_percent, INT, ZMOD_RD,
1387eda14cbcSMatt Macy 	"zfs_dirty_data_max upper bound as % of RAM");
1388eda14cbcSMatt Macy 
1389eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs, zfs_, delay_min_dirty_percent, INT, ZMOD_RW,
1390eda14cbcSMatt Macy 	"Transaction delay threshold");
1391eda14cbcSMatt Macy 
1392eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs, zfs_, dirty_data_max, ULONG, ZMOD_RW,
1393eda14cbcSMatt Macy 	"Determines the dirty space limit");
1394eda14cbcSMatt Macy 
1395eda14cbcSMatt Macy /* zfs_dirty_data_max_max only applied at module load in arc_init(). */
1396eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs, zfs_, dirty_data_max_max, ULONG, ZMOD_RD,
1397eda14cbcSMatt Macy 	"zfs_dirty_data_max upper bound in bytes");
1398eda14cbcSMatt Macy 
1399eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs, zfs_, dirty_data_sync_percent, INT, ZMOD_RW,
1400eda14cbcSMatt Macy 	"Dirty data txg sync threshold as a percentage of zfs_dirty_data_max");
1401eda14cbcSMatt Macy 
1402eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs, zfs_, delay_scale, ULONG, ZMOD_RW,
1403eda14cbcSMatt Macy 	"How quickly delay approaches infinity");
1404eda14cbcSMatt Macy 
1405eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs, zfs_, sync_taskq_batch_pct, INT, ZMOD_RW,
1406eda14cbcSMatt Macy 	"Max percent of CPUs that are used to sync dirty data");
1407eda14cbcSMatt Macy 
1408eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs_zil, zfs_zil_, clean_taskq_nthr_pct, INT, ZMOD_RW,
1409eda14cbcSMatt Macy 	"Max percent of CPUs that are used per dp_sync_taskq");
1410eda14cbcSMatt Macy 
1411eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs_zil, zfs_zil_, clean_taskq_minalloc, INT, ZMOD_RW,
1412eda14cbcSMatt Macy 	"Number of taskq entries that are pre-populated");
1413eda14cbcSMatt Macy 
1414eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs_zil, zfs_zil_, clean_taskq_maxalloc, INT, ZMOD_RW,
1415eda14cbcSMatt Macy 	"Max number of taskq entries that are cached");
1416eda14cbcSMatt Macy /* END CSTYLED */
1417