2fa9e406ahrens * CDDL HEADER START
3fa9e406ahrens *
4fa9e406ahrens * The contents of this file are subject to the terms of the
5ea8dc4beschrock * Common Development and Distribution License (the "License").
6ea8dc4beschrock * You may not use this file except in compliance with the License.
7fa9e406ahrens *
8fa9e406ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e406ahrens * or http://www.opensolaris.org/os/licensing.
10fa9e406ahrens * See the License for the specific language governing permissions
11fa9e406ahrens * and limitations under the License.
12fa9e406ahrens *
13fa9e406ahrens * When distributing Covered Code, include this CDDL HEADER in each
14fa9e406ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e406ahrens * If applicable, add the following below this CDDL HEADER, with the
16fa9e406ahrens * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e406ahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e406ahrens *
19fa9e406ahrens * CDDL HEADER END
20fa9e406ahrens */
223f9d6adLin Ling * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23814dcd4Serapheim Dimitropoulos * Copyright (c) 2011, 2019 by Delphix. All rights reserved.
24a7a845eSteven Hartland * Copyright (c) 2013 Steven Hartland. All rights reserved.
25bc9014eJustin Gibbs * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
26c3d26abMatthew Ahrens * Copyright (c) 2014 Integros [integros.com]
27ff5177eAlek Pinchuk * Copyright 2016 Nexenta Systems, Inc.  All rights reserved.
28fa9e406ahrens */
30fa9e406ahrens#include <sys/dsl_pool.h>
31fa9e406ahrens#include <sys/dsl_dataset.h>
323f9d6adLin Ling#include <sys/dsl_prop.h>
33fa9e406ahrens#include <sys/dsl_dir.h>
341d452cfahrens#include <sys/dsl_synctask.h>
353f9d6adLin Ling#include <sys/dsl_scan.h>
363f9d6adLin Ling#include <sys/dnode.h>
37fa9e406ahrens#include <sys/dmu_tx.h>
38fa9e406ahrens#include <sys/dmu_objset.h>
39fa9e406ahrens#include <sys/arc.h>
40fa9e406ahrens#include <sys/zap.h>
41c717a56maybee#include <sys/zio.h>
42fa9e406ahrens#include <sys/zfs_context.h>
43fa9e406ahrens#include <sys/fs/zfs.h>
44088f389ahrens#include <sys/zfs_znode.h>
45088f389ahrens#include <sys/spa_impl.h>
46cde58dbMatthew Ahrens#include <sys/dsl_deadlist.h>
478671400Serapheim Dimitropoulos#include <sys/vdev_impl.h>
488671400Serapheim Dimitropoulos#include <sys/metaslab_impl.h>
49ad135b5Christopher Siden#include <sys/bptree.h>
50ad135b5Christopher Siden#include <sys/zfeature.h>
51ce636f8Matthew Ahrens#include <sys/zil_impl.h>
523b2aab1Matthew Ahrens#include <sys/dsl_userhold.h>
53e0f1c0aOlaf Faaland#include <sys/mmp.h>
5569962b5Matthew Ahrens/*
5669962b5Matthew Ahrens * ZFS Write Throttle
5769962b5Matthew Ahrens * ------------------
5869962b5Matthew Ahrens *
5969962b5Matthew Ahrens * ZFS must limit the rate of incoming writes to the rate at which it is able
6069962b5Matthew Ahrens * to sync data modifications to the backend storage. Throttling by too much
6169962b5Matthew Ahrens * creates an artificial limit; throttling by too little can only be sustained
6269962b5Matthew Ahrens * for short periods and would lead to highly lumpy performance. On a per-pool
6369962b5Matthew Ahrens * basis, ZFS tracks the amount of modified (dirty) data. As operations change
6469962b5Matthew Ahrens * data, the amount of dirty data increases; as ZFS syncs out data, the amount
6569962b5Matthew Ahrens * of dirty data decreases. When the amount of dirty data exceeds a
6669962b5Matthew Ahrens * predetermined threshold further modifications are blocked until the amount
6769962b5Matthew Ahrens * of dirty data decreases (as data is synced out).
6869962b5Matthew Ahrens *
6969962b5Matthew Ahrens * The limit on dirty data is tunable, and should be adjusted according to
7069962b5Matthew Ahrens * both the IO capacity and available memory of the system. The larger the
7169962b5Matthew Ahrens * window, the more ZFS is able to aggregate and amortize metadata (and data)
7269962b5Matthew Ahrens * changes. However, memory is a limited resource, and allowing for more dirty
7369962b5Matthew Ahrens * data comes at the cost of keeping other useful data in memory (for example
7469962b5Matthew Ahrens * ZFS data cached by the ARC).
7569962b5Matthew Ahrens *
7669962b5Matthew Ahrens * Implementation
7769962b5Matthew Ahrens *
7869962b5Matthew Ahrens * As buffers are modified dsl_pool_willuse_space() increments both the per-
7969962b5Matthew Ahrens * txg (dp_dirty_pertxg[]) and poolwide (dp_dirty_total) accounting of
8069962b5Matthew Ahrens * dirty space used; dsl_pool_dirty_space() decrements those values as data
8169962b5Matthew Ahrens * is synced out from dsl_pool_sync(). While only the poolwide value is
8269962b5Matthew Ahrens * relevant, the per-txg value is useful for debugging. The tunable
8369962b5Matthew Ahrens * zfs_dirty_data_max determines the dirty space limit. Once that value is
8469962b5Matthew Ahrens * exceeded, new writes are halted until space frees up.
8569962b5Matthew Ahrens *
8669962b5Matthew Ahrens * The zfs_dirty_data_sync tunable dictates the threshold at which we
8769962b5Matthew Ahrens * ensure that there is a txg syncing (see the comment in txg.c for a full
8869962b5Matthew Ahrens * description of transaction group stages).
8969962b5Matthew Ahrens *
9069962b5Matthew Ahrens * The IO scheduler uses both the dirty space limit and current amount of
9169962b5Matthew Ahrens * dirty data as inputs. Those values affect the number of concurrent IOs ZFS
9269962b5Matthew Ahrens * issues. See the comment in vdev_queue.c for details of the IO scheduler.
9369962b5Matthew Ahrens *
9469962b5Matthew Ahrens * The delay is also calculated based on the amount of dirty data.  See the
9569962b5Matthew Ahrens * comment above dmu_tx_delay() for details.
9669962b5Matthew Ahrens */
9769962b5Matthew Ahrens
9869962b5Matthew Ahrens/*
9969962b5Matthew Ahrens * zfs_dirty_data_max will be set to zfs_dirty_data_max_percent% of all memory,
10069962b5Matthew Ahrens * capped at zfs_dirty_data_max_max.  It can also be overridden in /etc/system.
10169962b5Matthew Ahrens */
10269962b5Matthew Ahrensuint64_t zfs_dirty_data_max;
10369962b5Matthew Ahrensuint64_t zfs_dirty_data_max_max = 4ULL * 1024 * 1024 * 1024;
10469962b5Matthew Ahrensint zfs_dirty_data_max_percent = 10;
10569962b5Matthew Ahrens
10669962b5Matthew Ahrens/*
1077928f4bMatthew Ahrens * If there's at least this much dirty data (as a percentage of
1087928f4bMatthew Ahrens * zfs_dirty_data_max), push out a txg.  This should be less than
1097928f4bMatthew Ahrens * zfs_vdev_async_write_active_min_dirty_percent.
11069962b5Matthew Ahrens */
1117928f4bMatthew Ahrensuint64_t zfs_dirty_data_sync_pct = 20;
11269962b5Matthew Ahrens
11369962b5Matthew Ahrens/*
11469962b5Matthew Ahrens * Once there is this amount of dirty data, the dmu_tx_delay() will kick in
11569962b5Matthew Ahrens * and delay each transaction.
11669962b5Matthew Ahrens * This value should be >= zfs_vdev_async_write_active_max_dirty_percent.
11769962b5Matthew Ahrens */
11869962b5Matthew Ahrensint zfs_delay_min_dirty_percent = 60;
11969962b5Matthew Ahrens
12069962b5Matthew Ahrens/*
12169962b5Matthew Ahrens * This controls how quickly the delay approaches infinity.
122d85a1e9Matthew Ahrens * Larger values cause it to delay more for a given amount of dirty data.
123d85a1e9Matthew Ahrens * Therefore larger values will cause there to be less dirty data for a
12469962b5Matthew Ahrens * given throughput.
12569962b5Matthew Ahrens *
12669962b5Matthew Ahrens * For the smoothest delay, this value should be about 1 billion divided
12769962b5Matthew Ahrens * by the maximum number of operations per second.  This will smoothly
12869962b5Matthew Ahrens * handle between 10x and 1/10th this number.
12969962b5Matthew Ahrens *
13069962b5Matthew Ahrens * Note: zfs_delay_scale * zfs_dirty_data_max must be < 2^64, due to the
13169962b5Matthew Ahrens * multiply in dmu_tx_delay().
13269962b5Matthew Ahrens */
13369962b5Matthew Ahrensuint64_t zfs_delay_scale = 1000 * 1000 * 1000 / 2000;
13405715f9Mark Maybee
13594c2d0eMatthew Ahrens/*
13694c2d0eMatthew Ahrens * This determines the number of threads used by the dp_sync_taskq.
13794c2d0eMatthew Ahrens */
13894c2d0eMatthew Ahrensint zfs_sync_taskq_batch_pct = 75;
140216d772Prakash Surya/*
141216d772Prakash Surya * These tunables determine the behavior of how zil_itxg_clean() is
142216d772Prakash Surya * called via zil_clean() in the context of spa_sync(). When an itxg
143216d772Prakash Surya * list needs to be cleaned, TQ_NOSLEEP will be used when dispatching.
144216d772Prakash Surya * If the dispatch fails, the call to zil_itxg_clean() will occur
145216d772Prakash Surya * synchronously in the context of spa_sync(), which can negatively
146216d772Prakash Surya * impact the performance of spa_sync() (e.g. in the case of the itxg
147216d772Prakash Surya * list having a large number of itxs that needs to be cleaned).
148216d772Prakash Surya *
149216d772Prakash Surya * Thus, these tunables can be used to manipulate the behavior of the
150216d772Prakash Surya * taskq used by zil_clean(); they determine the number of taskq entries
151216d772Prakash Surya * that are pre-populated when the taskq is first created (via the
152216d772Prakash Surya * "zfs_zil_clean_taskq_minalloc" tunable) and the maximum number of
153216d772Prakash Surya * taskq entries that are cached after an on-demand allocation (via the
154216d772Prakash Surya * "zfs_zil_clean_taskq_maxalloc").
155216d772Prakash Surya *
156216d772Prakash Surya * The idea being, we want to try reasonably hard to ensure there will
157216d772Prakash Surya * already be a taskq entry pre-allocated by the time that it is needed
158216d772Prakash Surya * by zil_clean(). This way, we can avoid the possibility of an
159216d772Prakash Surya * on-demand allocation of a new taskq entry from failing, which would
160216d772Prakash Surya * result in zil_itxg_clean() being called synchronously from zil_clean()
161216d772Prakash Surya * (which can adversely affect performance of spa_sync()).
162216d772Prakash Surya *
163216d772Prakash Surya * Additionally, the number of threads used by the taskq can be
164216d772Prakash Surya * configured via the "zfs_zil_clean_taskq_nthr_pct" tunable.
165216d772Prakash Surya */
166216d772Prakash Suryaint zfs_zil_clean_taskq_nthr_pct = 100;
167216d772Prakash Suryaint zfs_zil_clean_taskq_minalloc = 1024;
168216d772Prakash Suryaint zfs_zil_clean_taskq_maxalloc = 1024 * 1024;
169216d772Prakash Surya
1703f9d6adLin Lingint
171088f389ahrensdsl_pool_open_special_dir(dsl_pool_t *dp, const char *name, dsl_dir_t **ddp)
173fa9e406ahrens	uint64_t obj;
174fa9e406ahrens	int err;
176fa9e406ahrens	err = zap_lookup(dp->dp_meta_objset,
177c137962Justin T. Gibbs	    dsl_dir_phys(dp->dp_root_dir)->dd_child_dir_zapobj,
178088f389ahrens	    name, sizeof (obj), 1, &obj);
179ea8dc4beschrock	if (err)
180ea8dc4beschrock		return (err);
1823b2aab1Matthew Ahrens	return (dsl_dir_hold_obj(dp, obj, name, dp, ddp));
185fa9e406ahrensstatic dsl_pool_t *
186fa9e406ahrensdsl_pool_open_impl(spa_t *spa, uint64_t txg)
188fa9e406ahrens	dsl_pool_t *dp;
189fa9e406ahrens	blkptr_t *bp = spa_get_rootblkptr(spa);
191fa9e406ahrens	dp = kmem_zalloc(sizeof (dsl_pool_t), KM_SLEEP);
192fa9e406ahrens	dp->dp_spa = spa;
193fa9e406ahrens	dp->dp_meta_rootbp = *bp;
1943b2aab1Matthew Ahrens	rrw_init(&dp->dp_config_rwlock, B_TRUE);
195fa9e406ahrens	txg_init(dp, txg);
196e0f1c0aOlaf Faaland	mmp_init(spa);
198b7b2590Matthew Ahrens	txg_list_create(&dp->dp_dirty_datasets, spa,
199fa9e406ahrens	    offsetof(dsl_dataset_t, ds_dirty_link));
200b7b2590Matthew Ahrens	txg_list_create(&dp->dp_dirty_zilogs, spa,
201ce636f8Matthew Ahrens	    offsetof(zilog_t, zl_dirty_link));
202b7b2590Matthew Ahrens	txg_list_create(&dp->dp_dirty_dirs, spa,
203fa9e406ahrens	    offsetof(dsl_dir_t, dd_dirty_link));
204b7b2590Matthew Ahrens	txg_list_create(&dp->dp_sync_tasks, spa,
2053b2aab1Matthew Ahrens	    offsetof(dsl_sync_task_t, dst_node));
2068671400Serapheim Dimitropoulos	txg_list_create(&dp->dp_early_sync_tasks, spa,
2078671400Serapheim Dimitropoulos	    offsetof(dsl_sync_task_t, dst_node));
20994c2d0eMatthew Ahrens	dp->dp_sync_taskq = taskq_create("dp_sync_taskq",
21094c2d0eMatthew Ahrens	    zfs_sync_taskq_batch_pct, minclsyspri, 1, INT_MAX,
21194c2d0eMatthew Ahrens	    TASKQ_THREADS_CPU_PCT);
21294c2d0eMatthew Ahrens
213216d772Prakash Surya	dp->dp_zil_clean_taskq = taskq_create("dp_zil_clean_taskq",
214216d772Prakash Surya	    zfs_zil_clean_taskq_nthr_pct, minclsyspri,
215216d772Prakash Surya	    zfs_zil_clean_taskq_minalloc,
216216d772Prakash Surya	    zfs_zil_clean_taskq_maxalloc,
218216d772Prakash Surya
2191ab7f2dmaybee	mutex_init(&dp->dp_lock, NULL, MUTEX_DEFAULT, NULL);
22069962b5Matthew Ahrens	cv_init(&dp->dp_spaceavail_cv, NULL, CV_DEFAULT, NULL);
2229d3574bNeil Perrin	dp->dp_vnrele_taskq = taskq_create("zfs_vn_rele_taskq", 1, minclsyspri,
2239d3574bNeil Perrin	    1, 4, 0);
224c5832a5Alek Pinchuk	dp->dp_unlinked_drain_taskq = taskq_create("z_unlinked_drain",
225c5832a5Alek Pinchuk	    max_ncpus, minclsyspri, max_ncpus, INT_MAX,
226c5832a5Alek Pinchuk	    TASKQ_PREPOPULATE | TASKQ_DYNAMIC);
2279d3574bNeil Perrin
228fa9e406ahrens	return (dp);
232ad135b5Christopher Sidendsl_pool_init(spa_t *spa, uint64_t txg, dsl_pool_t **dpp)
234fa9e406ahrens	int err;
235fa9e406ahrens	dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
236ad135b5Christopher Siden
237ad135b5Christopher Siden	err = dmu_objset_open_impl(spa, NULL, &dp->dp_meta_rootbp,
238ad135b5Christopher Siden	    &dp->dp_meta_objset);
239ad135b5Christopher Siden	if (err != 0)
240ad135b5Christopher Siden		dsl_pool_close(dp);
241ad135b5Christopher Siden	else
242ad135b5Christopher Siden		*dpp = dp;
243ad135b5Christopher Siden
244ad135b5Christopher Siden	return (err);
245ad135b5Christopher Siden}
246ad135b5Christopher Siden
247ad135b5Christopher Sidenint
248ad135b5Christopher Sidendsl_pool_open(dsl_pool_t *dp)
249ad135b5Christopher Siden{
250ad135b5Christopher Siden	int err;
251088f389ahrens	dsl_dir_t *dd;
252088f389ahrens	dsl_dataset_t *ds;
253cde58dbMatthew Ahrens	uint64_t obj;
2553b2aab1Matthew Ahrens	rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
256fa9e406ahrens	err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
257fa9e406ahrens	    DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1,
258fa9e406ahrens	    &dp->dp_root_dir_obj);
259ea8dc4beschrock	if (err)
260ea8dc4beschrock		goto out;
2623b2aab1Matthew Ahrens	err = dsl_dir_hold_obj(dp, dp->dp_root_dir_obj,
263ea8dc4beschrock	    NULL, dp, &dp->dp_root_dir);
264ea8dc4beschrock	if (err)
265ea8dc4beschrock		goto out;
267088f389ahrens	err = dsl_pool_open_special_dir(dp, MOS_DIR_NAME, &dp->dp_mos_dir);
268ea8dc4beschrock	if (err)
269ea8dc4beschrock		goto out;
271ad135b5Christopher Siden	if (spa_version(dp->dp_spa) >= SPA_VERSION_ORIGIN) {
272088f389ahrens		err = dsl_pool_open_special_dir(dp, ORIGIN_DIR_NAME, &dd);
273088f389ahrens		if (err)
274088f389ahrens			goto out;
275c137962Justin T. Gibbs		err = dsl_dataset_hold_obj(dp,
276c137962Justin T. Gibbs		    dsl_dir_phys(dd)->dd_head_dataset_obj, FTAG, &ds);
2778f63aa4Lin Ling		if (err == 0) {
2788f63aa4Lin Ling			err = dsl_dataset_hold_obj(dp,
279c137962Justin T. Gibbs			    dsl_dataset_phys(ds)->ds_prev_snap_obj, dp,
2808f63aa4Lin Ling			    &dp->dp_origin_snap);
2818f63aa4Lin Ling			dsl_dataset_rele(ds, FTAG);
2828f63aa4Lin Ling		}
2833b2aab1Matthew Ahrens		dsl_dir_rele(dd, dp);
284088f389ahrens		if (err)
285088f389ahrens			goto out;
286088f389ahrens	}
288ad135b5Christopher Siden	if (spa_version(dp->dp_spa) >= SPA_VERSION_DEADLISTS) {
289cde58dbMatthew Ahrens		err = dsl_pool_open_special_dir(dp, FREE_DIR_NAME,
290cde58dbMatthew Ahrens		    &dp->dp_free_dir);
291cde58dbMatthew Ahrens		if (err)
292cde58dbMatthew Ahrens			goto out;
293cde58dbMatthew Ahrens
294cde58dbMatthew Ahrens		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
295cde58dbMatthew Ahrens		    DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj);
296cde58dbMatthew Ahrens		if (err)
297cde58dbMatthew Ahrens			goto out;
2983b2aab1Matthew Ahrens		VERIFY0(bpobj_open(&dp->dp_free_bpobj,
299cde58dbMatthew Ahrens		    dp->dp_meta_objset, obj));
300cde58dbMatthew Ahrens	}
301cde58dbMatthew Ahrens
3025cabbc6Prashanth Sreenivasa	if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_OBSOLETE_COUNTS)) {
3035cabbc6Prashanth Sreenivasa		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
3045cabbc6Prashanth Sreenivasa		    DMU_POOL_OBSOLETE_BPOBJ, sizeof (uint64_t), 1, &obj);
3055cabbc6Prashanth Sreenivasa		if (err == 0) {
3065cabbc6Prashanth Sreenivasa			VERIFY0(bpobj_open(&dp->dp_obsolete_bpobj,
3075cabbc6Prashanth Sreenivasa			    dp->dp_meta_objset, obj));
3085cabbc6Prashanth Sreenivasa		} else if (err == ENOENT) {
3095cabbc6Prashanth Sreenivasa			/*
3105cabbc6Prashanth Sreenivasa			 * We might not have created the remap bpobj yet.
3115cabbc6Prashanth Sreenivasa			 */
3125cabbc6Prashanth Sreenivasa			err = 0;
3135cabbc6Prashanth Sreenivasa		} else {
3145cabbc6Prashanth Sreenivasa			goto out;
3155cabbc6Prashanth Sreenivasa		}
3165cabbc6Prashanth Sreenivasa	}
3175cabbc6Prashanth Sreenivasa
3187fd05acMatthew Ahrens	/*
3195cabbc6Prashanth Sreenivasa	 * Note: errors ignored, because the these special dirs, used for
3205cabbc6Prashanth Sreenivasa	 * space accounting, are only created on demand.
3217fd05acMatthew Ahrens	 */
3227fd05acMatthew Ahrens	(void) dsl_pool_open_special_dir(dp, LEAK_DIR_NAME,
3237fd05acMatthew Ahrens	    &dp->dp_leak_dir);
3247fd05acMatthew Ahrens
3252acef22Matthew Ahrens	if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_ASYNC_DESTROY)) {
326ad135b5Christopher Siden		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
327ad135b5Christopher Siden		    DMU_POOL_BPTREE_OBJ, sizeof (uint64_t), 1,
328ad135b5Christopher Siden		    &dp->dp_bptree_obj);
329ad135b5Christopher Siden		if (err != 0)
330ad135b5Christopher Siden			goto out;
331ad135b5Christopher Siden	}
332ad135b5Christopher Siden
3332acef22Matthew Ahrens	if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_EMPTY_BPOBJ)) {
334f174573Matthew Ahrens		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
335f174573Matthew Ahrens		    DMU_POOL_EMPTY_BPOBJ, sizeof (uint64_t), 1,
336f174573Matthew Ahrens		    &dp->dp_empty_bpobj);
337f174573Matthew Ahrens		if (err != 0)
338f174573Matthew Ahrens			goto out;
339f174573Matthew Ahrens	}
340f174573Matthew Ahrens
341ca45db4Chris Kirby	err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
342ca45db4Chris Kirby	    DMU_POOL_TMP_USERREFS, sizeof (uint64_t), 1,
343ca45db4Chris Kirby	    &dp->dp_tmp_userrefs_obj);
344ca45db4Chris Kirby	if (err == ENOENT)
345ca45db4Chris Kirby		err = 0;
346ca45db4Chris Kirby	if (err)
347ca45db4Chris Kirby		goto out;
348ca45db4Chris Kirby
349ad135b5Christopher Siden	err = dsl_scan_init(dp, dp->dp_tx.tx_open_txg);
3523b2aab1Matthew Ahrens	rrw_exit(&dp->dp_config_rwlock, FTAG);
353ea8dc4beschrock	return (err);
357fa9e406ahrensdsl_pool_close(dsl_pool_t *dp)
359088f389ahrens	/*
36069962b5Matthew Ahrens	 * Drop our references from dsl_pool_open().
36169962b5Matthew Ahrens	 *
362088f389ahrens	 * Since we held the origin_snap from "syncing" context (which
363088f389ahrens	 * includes pool-opening context), it actually only got a "ref"
364088f389ahrens	 * and not a hold, so just drop that here.
365088f389ahrens	 */
3665cabbc6Prashanth Sreenivasa	if (dp->dp_origin_snap != NULL)
3673b2aab1Matthew Ahrens		dsl_dataset_rele(dp->dp_origin_snap, dp);
3685cabbc6Prashanth Sreenivasa	if (dp->dp_mos_dir != NULL)
3693b2aab1Matthew Ahrens		dsl_dir_rele(dp->dp_mos_dir, dp);
3705cabbc6Prashanth Sreenivasa	if (dp->dp_free_dir != NULL)
3713b2aab1Matthew Ahrens		dsl_dir_rele(dp->dp_free_dir, dp);
3725cabbc6Prashanth Sreenivasa	if (dp->dp_leak_dir != NULL)
3737fd05acMatthew Ahrens		dsl_dir_rele(dp->dp_leak_dir, dp);
3745cabbc6Prashanth Sreenivasa	if (dp->dp_root_dir != NULL)
3753b2aab1Matthew Ahrens		dsl_dir_rele(dp->dp_root_dir, dp);
377cde58dbMatthew Ahrens	bpobj_close(&dp->dp_free_bpobj);
3785cabbc6Prashanth Sreenivasa	bpobj_close(&dp->dp_obsolete_bpobj);
379cde58dbMatthew Ahrens
380fa9e406ahrens	/* undo the dmu_objset_open_impl(mos) from dsl_pool_open() */
3815cabbc6Prashanth Sreenivasa	if (dp->dp_meta_objset != NULL)
382503ad85Matthew Ahrens		dmu_objset_evict(dp->dp_meta_objset);
384fa9e406ahrens	txg_list_destroy(&dp->dp_dirty_datasets);
385ce636f8Matthew Ahrens	txg_list_destroy(&dp->dp_dirty_zilogs);
38654a9111Chris Kirby	txg_list_destroy(&dp->dp_sync_tasks);
3878671400Serapheim Dimitropoulos	txg_list_destroy(&dp->dp_early_sync_tasks);
388fa9e406ahrens	txg_list_destroy(&dp->dp_dirty_dirs);
390216d772Prakash Surya	taskq_destroy(dp->dp_zil_clean_taskq);
39194c2d0eMatthew Ahrens	taskq_destroy(dp->dp_sync_taskq);
39294c2d0eMatthew Ahrens
393244781fPrakash Surya	/*
394244781fPrakash Surya	 * We can't set retry to TRUE since we're explicitly specifying
395244781fPrakash Surya	 * a spa to flush. This is good enough; any missed buffers for
396244781fPrakash Surya	 * this spa won't cause trouble, and they'll eventually fall
397244781fPrakash Surya	 * out of the ARC just like any other unused buffer.
398244781fPrakash Surya	 */
399244781fPrakash Surya	arc_flush(dp->dp_spa, FALSE);
400244781fPrakash Surya
401e0f1c0aOlaf Faaland	mmp_fini(dp->dp_spa);
402fa9e406ahrens	txg_fini(dp);
4033f9d6adLin Ling	dsl_scan_fini(dp);
404bc9014eJustin Gibbs	dmu_buf_user_evict_wait();
405bc9014eJustin Gibbs
4063b2aab1Matthew Ahrens	rrw_destroy(&dp->dp_config_rwlock);
4071ab7f2dmaybee	mutex_destroy(&dp->dp_lock);
408c5832a5Alek Pinchuk	taskq_destroy(dp->dp_unlinked_drain_taskq);
4099d3574bNeil Perrin	taskq_destroy(dp->dp_vnrele_taskq);
4105cabbc6Prashanth Sreenivasa	if (dp->dp_blkstats != NULL)
41188b7b0fMatthew Ahrens		kmem_free(dp->dp_blkstats, sizeof (zfs_all_blkstats_t));
412fa9e406ahrens	kmem_free(dp, sizeof (dsl_pool_t));
4155cabbc6Prashanth Sreenivasavoid
4165cabbc6Prashanth Sreenivasadsl_pool_create_obsolete_bpobj(dsl_pool_t *dp, dmu_tx_t *tx)
4175cabbc6Prashanth Sreenivasa{
4185cabbc6Prashanth Sreenivasa	uint64_t obj;
4195cabbc6Prashanth Sreenivasa	/*
4205cabbc6Prashanth Sreenivasa	 * Currently, we only create the obsolete_bpobj where there are
4215cabbc6Prashanth Sreenivasa	 * indirect vdevs with referenced mappings.
4225cabbc6Prashanth Sreenivasa	 */
4235cabbc6Prashanth Sreenivasa	ASSERT(spa_feature_is_active(dp->dp_spa, SPA_FEATURE_DEVICE_REMOVAL));
4245cabbc6Prashanth Sreenivasa	/* create and open the obsolete_bpobj */
4255cabbc6Prashanth Sreenivasa	obj = bpobj_alloc(dp->dp_meta_objset, SPA_OLD_MAXBLOCKSIZE, tx);
4265cabbc6Prashanth Sreenivasa	VERIFY0(bpobj_open(&dp->dp_obsolete_bpobj, dp->dp_meta_objset, obj));
4275cabbc6Prashanth Sreenivasa	VERIFY0(zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
4285cabbc6Prashanth Sreenivasa	    DMU_POOL_OBSOLETE_BPOBJ, sizeof (uint64_t), 1, &obj, tx));
4295cabbc6Prashanth Sreenivasa	spa_feature_incr(dp->dp_spa, SPA_FEATURE_OBSOLETE_COUNTS, tx);
4305cabbc6Prashanth Sreenivasa}
4315cabbc6Prashanth Sreenivasa
4325cabbc6Prashanth Sreenivasavoid
4335cabbc6Prashanth Sreenivasadsl_pool_destroy_obsolete_bpobj(dsl_pool_t *dp, dmu_tx_t *tx)
4345cabbc6Prashanth Sreenivasa{
4355cabbc6Prashanth Sreenivasa	spa_feature_decr(dp->dp_spa, SPA_FEATURE_OBSOLETE_COUNTS, tx);
4365cabbc6Prashanth Sreenivasa	VERIFY0(zap_remove(dp->dp_meta_objset,
4375cabbc6Prashanth Sreenivasa	    DMU_POOL_DIRECTORY_OBJECT,
4385cabbc6Prashanth Sreenivasa	    DMU_POOL_OBSOLETE_BPOBJ, tx));
4395cabbc6Prashanth Sreenivasa	bpobj_free(dp->dp_meta_objset,
4405cabbc6Prashanth Sreenivasa	    dp->dp_obsolete_bpobj.bpo_object, tx);
4415cabbc6Prashanth Sreenivasa	bpobj_close(&dp->dp_obsolete_bpobj);
4425cabbc6Prashanth Sreenivasa}
4435cabbc6Prashanth Sreenivasa
444fa9e406ahrensdsl_pool_t *
445eb63303Tom Caputidsl_pool_create(spa_t *spa, nvlist_t *zplprops, dsl_crypto_params_t *dcp,
446eb63303Tom Caputi    uint64_t txg)
448fa9e406ahrens	int err;
449fa9e406ahrens	dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
450fa9e406ahrens	dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg);
451088f389ahrens	dsl_dataset_t *ds;
452cde58dbMatthew Ahrens	uint64_t obj;
4543b2aab1Matthew Ahrens	rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
4553b2aab1Matthew Ahrens
456088f389ahrens	/* create and open the MOS (meta-objset) */
457503ad85Matthew Ahrens	dp->dp_meta_objset = dmu_objset_create_impl(spa,
458503ad85Matthew Ahrens	    NULL, &dp->dp_meta_rootbp, DMU_OST_META, tx);
459eb63303Tom Caputi	spa->spa_meta_objset = dp->dp_meta_objset;
461fa9e406ahrens	/* create the pool directory */
462fa9e406ahrens	err = zap_create_claim(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
463fa9e406ahrens	    DMU_OT_OBJECT_DIRECTORY, DMU_OT_NONE, 0, tx);
464fb09f5aMadhav Suresh	ASSERT0(err);
4663f9d6adLin Ling	/* Initialize scan structures */
4673b2aab1Matthew Ahrens	VERIFY0(dsl_scan_init(dp, txg));
4683f9d6adLin Ling
469fa9e406ahrens	/* create and open the root dir */
470088f389ahrens	dp->dp_root_dir_obj = dsl_dir_create_sync(dp, NULL, NULL, tx);
4713b2aab1Matthew Ahrens	VERIFY0(dsl_dir_hold_obj(dp, dp->dp_root_dir_obj,
472ea8dc4beschrock	    NULL, dp, &dp->dp_root_dir));
474fa9e406ahrens	/* create and open the meta-objset dir */
475088f389ahrens	(void) dsl_dir_create_sync(dp, dp->dp_root_dir, MOS_DIR_NAME, tx);
4763b2aab1Matthew Ahrens	VERIFY0(dsl_pool_open_special_dir(dp,
477088f389ahrens	    MOS_DIR_NAME, &dp->dp_mos_dir));
479cde58dbMatthew Ahrens	if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
480cde58dbMatthew Ahrens		/* create and open the free dir */
481cde58dbMatthew Ahrens		(void) dsl_dir_create_sync(dp, dp->dp_root_dir,
482cde58dbMatthew Ahrens		    FREE_DIR_NAME, tx);
4833b2aab1Matthew Ahrens		VERIFY0(dsl_pool_open_special_dir(dp,
484cde58dbMatthew Ahrens		    FREE_DIR_NAME, &dp->dp_free_dir));
485cde58dbMatthew Ahrens
486cde58dbMatthew Ahrens		/* create and open the free_bplist */
487b515258Matthew Ahrens		obj = bpobj_alloc(dp->dp_meta_objset, SPA_OLD_MAXBLOCKSIZE, tx);
488cde58dbMatthew Ahrens		VERIFY(zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
489cde58dbMatthew Ahrens		    DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj, tx) == 0);
4903b2aab1Matthew Ahrens		VERIFY0(bpobj_open(&dp->dp_free_bpobj,
491cde58dbMatthew Ahrens		    dp->dp_meta_objset, obj));
492cde58dbMatthew Ahrens	}
493cde58dbMatthew Ahrens
494088f389ahrens	if (spa_version(spa) >= SPA_VERSION_DSL_SCRUB)
495088f389ahrens		dsl_pool_create_origin(dp, tx);
497eb63303Tom Caputi	/*
498eb63303Tom Caputi	 * Some features may be needed when creating the root dataset, so we
499eb63303Tom Caputi	 * create the feature objects here.
500eb63303Tom Caputi	 */
501eb63303Tom Caputi	if (spa_version(spa) >= SPA_VERSION_FEATURES)
502eb63303Tom Caputi		spa_feature_create_zap_objects(spa, tx);
503eb63303Tom Caputi
504eb63303Tom Caputi	if (dcp != NULL && dcp->cp_crypt != ZIO_CRYPT_OFF &&
505eb63303Tom Caputi	    dcp->cp_crypt != ZIO_CRYPT_INHERIT)
506eb63303Tom Caputi		spa_feature_enable(spa, SPA_FEATURE_ENCRYPTION, tx);
507eb63303Tom Caputi
508088f389ahrens	/* create the root dataset */
509eb63303Tom Caputi	obj = dsl_dataset_create_sync_dd(dp->dp_root_dir, NULL, dcp, 0, tx);
511088f389ahrens	/* create the root objset */
512eb63303Tom Caputi	VERIFY0(dsl_dataset_hold_obj_flags(dp, obj,
513eb63303Tom Caputi	    DS_HOLD_FLAG_DECRYPT, FTAG, &ds));
514088f389ahrens#ifdef _KERNEL
515b852c2fToomas Soome	{
516b852c2fToomas Soome		objset_t *os;
517b852c2fToomas Soome		rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
518b852c2fToomas Soome		os = dmu_objset_create_impl(dp->dp_spa, ds,
519b852c2fToomas Soome		    dsl_dataset_get_blkptr(ds), DMU_OST_ZFS, tx);
520b852c2fToomas Soome		rrw_exit(&ds->ds_bp_rwlock, FTAG);
521b852c2fToomas Soome		zfs_create_fs(os, kcred, zplprops, tx);
522b852c2fToomas Soome	}
524eb63303Tom Caputi	dsl_dataset_rele_flags(ds, DS_HOLD_FLAG_DECRYPT, FTAG);
526fa9e406ahrens	dmu_tx_commit(tx);
5283b2aab1Matthew Ahrens	rrw_exit(&dp->dp_config_rwlock, FTAG);
5293b2aab1Matthew Ahrens
530fa9e406ahrens	return (dp);
533ce636f8Matthew Ahrens/*
534ce636f8Matthew Ahrens * Account for the meta-objset space in its placeholder dsl_dir.
535ce636f8Matthew Ahrens */
536ce636f8Matthew Ahrensvoid
537ce636f8Matthew Ahrensdsl_pool_mos_diduse_space(dsl_pool_t *dp,
538ce636f8Matthew Ahrens    int64_t used, int64_t comp, int64_t uncomp)
539ce636f8Matthew Ahrens{
540ce636f8Matthew Ahrens	ASSERT3U(comp, ==, uncomp); /* it's all metadata */
541ce636f8Matthew Ahrens	mutex_enter(&dp->dp_lock);
542ce636f8Matthew Ahrens	dp->dp_mos_used_delta += used;
543ce636f8Matthew Ahrens	dp->dp_mos_compressed_delta += comp;
544ce636f8Matthew Ahrens	dp->dp_mos_uncompressed_delta += uncomp;
545ce636f8Matthew Ahrens	mutex_exit(&dp->dp_lock);
546ce636f8Matthew Ahrens}
547ce636f8Matthew Ahrens
54869962b5Matthew Ahrensstatic void
54969962b5Matthew Ahrensdsl_pool_sync_mos(dsl_pool_t *dp, dmu_tx_t *tx)
55069962b5Matthew Ahrens{
55169962b5Matthew Ahrens	zio_t *zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
55269962b5Matthew Ahrens	dmu_objset_sync(dp->dp_meta_objset, zio, tx);
55369962b5Matthew Ahrens	VERIFY0(zio_wait(zio));
55469962b5Matthew Ahrens	dprintf_bp(&dp->dp_meta_rootbp, "meta objset rootbp is %s", "");
55569962b5Matthew Ahrens	spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
55669962b5Matthew Ahrens}
55769962b5Matthew Ahrens
55869962b5Matthew Ahrensstatic void
55969962b5Matthew Ahrensdsl_pool_dirty_delta(dsl_pool_t *dp, int64_t delta)
56069962b5Matthew Ahrens{
56169962b5Matthew Ahrens	ASSERT(MUTEX_HELD(&dp->dp_lock));
56269962b5Matthew Ahrens
56369962b5Matthew Ahrens	if (delta < 0)
56469962b5Matthew Ahrens		ASSERT3U(-delta, <=, dp->dp_dirty_total);
56569962b5Matthew Ahrens
56669962b5Matthew Ahrens	dp->dp_dirty_total += delta;
56769962b5Matthew Ahrens
56869962b5Matthew Ahrens	/*
56969962b5Matthew Ahrens	 * Note: we signal even when increasing dp_dirty_total.
57069962b5Matthew Ahrens	 * This ensures forward progress -- each thread wakes the next waiter.
57169962b5Matthew Ahrens	 */
572313ae1eAndriy Gapon	if (dp->dp_dirty_total < zfs_dirty_data_max)
57369962b5Matthew Ahrens		cv_signal(&dp->dp_spaceavail_cv);
57469962b5Matthew Ahrens}
57569962b5Matthew Ahrens
5768671400Serapheim Dimitropoulosstatic boolean_t
5778671400Serapheim Dimitropoulosdsl_early_sync_task_verify(dsl_pool_t *dp, uint64_t txg)
5788671400Serapheim Dimitropoulos{
5798671400Serapheim Dimitropoulos	spa_t *spa = dp->dp_spa;
5808671400Serapheim Dimitropoulos	vdev_t *rvd = spa->spa_root_vdev;
5818671400Serapheim Dimitropoulos
5828671400Serapheim Dimitropoulos	for (uint64_t c = 0; c < rvd->vdev_children; c++) {
5838671400Serapheim Dimitropoulos		vdev_t *vd = rvd->vdev_child[c];
5848671400Serapheim Dimitropoulos		txg_list_t *tl = &vd->vdev_ms_list;
5858671400Serapheim Dimitropoulos		metaslab_t *ms;
5868671400Serapheim Dimitropoulos
5878671400Serapheim Dimitropoulos		for (ms = txg_list_head(tl, TXG_CLEAN(txg)); ms;
5888671400Serapheim Dimitropoulos		    ms = txg_list_next(tl, ms, TXG_CLEAN(txg))) {
5898671400Serapheim Dimitropoulos			VERIFY(range_tree_is_empty(ms->ms_freeing));
5908671400Serapheim Dimitropoulos			VERIFY(range_tree_is_empty(ms->ms_checkpointing));
5918671400Serapheim Dimitropoulos		}
5928671400Serapheim Dimitropoulos	}
5938671400Serapheim Dimitropoulos
5948671400Serapheim Dimitropoulos	return (B_TRUE);
5958671400Serapheim Dimitropoulos}
5968671400Serapheim Dimitropoulos
598fa9e406ahrensdsl_pool_sync(dsl_pool_t *dp, uint64_t txg)
600c717a56maybee	zio_t *zio;
601fa9e406ahrens	dmu_tx_t *tx;
602c717a56maybee	dsl_dir_t *dd;
603c717a56maybee	dsl_dataset_t *ds;
604503ad85Matthew Ahrens	objset_t *mos = dp->dp_meta_objset;
605ce636f8Matthew Ahrens	list_t synced_datasets;
606ce636f8Matthew Ahrens
607ce636f8Matthew Ahrens	list_create(&synced_datasets, sizeof (dsl_dataset_t),
608ce636f8Matthew Ahrens	    offsetof(dsl_dataset_t, ds_synced_link));
610fa9e406ahrens	tx = dmu_tx_create_assigned(dp, txg);
61269962b5Matthew Ahrens	/*
6138671400Serapheim Dimitropoulos	 * Run all early sync tasks before writing out any dirty blocks.
6148671400Serapheim Dimitropoulos	 * For more info on early sync tasks see block comment in
6158671400Serapheim Dimitropoulos	 * dsl_early_sync_task().
6168671400Serapheim Dimitropoulos	 */
6178671400Serapheim Dimitropoulos	if (!txg_list_empty(&dp->dp_early_sync_tasks, txg)) {
6188671400Serapheim Dimitropoulos		dsl_sync_task_t *dst;
6198671400Serapheim Dimitropoulos
6208671400Serapheim Dimitropoulos		ASSERT3U(spa_sync_pass(dp->dp_spa), ==, 1);
6218671400Serapheim Dimitropoulos		while ((dst =
6228671400Serapheim Dimitropoulos		    txg_list_remove(&dp->dp_early_sync_tasks, txg)) != NULL) {
6238671400Serapheim Dimitropoulos			ASSERT(dsl_early_sync_task_verify(dp, txg));
6248671400Serapheim Dimitropoulos			dsl_sync_task_sync(dst, tx);
6258671400Serapheim Dimitropoulos		}
6268671400Serapheim Dimitropoulos		ASSERT(dsl_early_sync_task_verify(dp, txg));
6278671400Serapheim Dimitropoulos	}
6288671400Serapheim Dimitropoulos
6298671400Serapheim Dimitropoulos	/*
63069962b5Matthew Ahrens	 * Write out all dirty blocks of dirty datasets.
63169962b5Matthew Ahrens	 */
632c717a56maybee	zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
63369962b5Matthew Ahrens	while ((ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) != NULL) {
6341484342Matthew Ahrens		/*
6351484342Matthew Ahrens		 * We must not sync any non-MOS datasets twice, because
6361484342Matthew Ahrens		 * we may have taken a snapshot of them.  However, we
6371484342Matthew Ahrens		 * may sync newly-created datasets on pass 2.
6381484342Matthew Ahrens		 */
6391484342Matthew Ahrens		ASSERT(!list_link_active(&ds->ds_synced_link));
640ce636f8Matthew Ahrens		list_insert_tail(&synced_datasets, ds);
641c717a56maybee		dsl_dataset_sync(ds, zio, tx);
642c717a56maybee	}
64369962b5Matthew Ahrens	VERIFY0(zio_wait(zio));
6441484342Matthew Ahrens
64569962b5Matthew Ahrens	/*
64669962b5Matthew Ahrens	 * We have written all of the accounted dirty data, so our
64769962b5Matthew Ahrens	 * dp_space_towrite should now be zero.  However, some seldom-used
64869962b5Matthew Ahrens	 * code paths do not adhere to this (e.g. dbuf_undirty(), also
64969962b5Matthew Ahrens	 * rounding error in dbuf_write_physdone).
65069962b5Matthew Ahrens	 * Shore up the accounting of any dirtied space now.
65169962b5Matthew Ahrens	 */
65269962b5Matthew Ahrens	dsl_pool_undirty_space(dp, dp->dp_dirty_pertxg[txg & TXG_MASK], txg);
654ce636f8Matthew Ahrens	/*
655ff5177eAlek Pinchuk	 * Update the long range free counter after
656ff5177eAlek Pinchuk	 * we're done syncing user data
657ff5177eAlek Pinchuk	 */
658ff5177eAlek Pinchuk	mutex_enter(&dp->dp_lock);
659ff5177eAlek Pinchuk	ASSERT(spa_sync_pass(dp->dp_spa) == 1 ||
660ff5177eAlek Pinchuk	    dp->dp_long_free_dirty_pertxg[txg & TXG_MASK] == 0);
661ff5177eAlek Pinchuk	dp->dp_long_free_dirty_pertxg[txg & TXG_MASK] = 0;
662ff5177eAlek Pinchuk	mutex_exit(&dp->dp_lock);
663ff5177eAlek Pinchuk
664ff5177eAlek Pinchuk	/*
665ce636f8Matthew Ahrens	 * After the data blocks have been written (ensured by the zio_wait()
666f67950bNasf-Fan	 * above), update the user/group/project space accounting.  This happens
66794c2d0eMatthew Ahrens	 * in tasks dispatched to dp_sync_taskq, so wait for them before
66894c2d0eMatthew Ahrens	 * continuing.
669ce636f8Matthew Ahrens	 */
67069962b5Matthew Ahrens	for (ds = list_head(&synced_datasets); ds != NULL;
67169962b5Matthew Ahrens	    ds = list_next(&synced_datasets, ds)) {
6720a586ceMark Shellenbaum		dmu_objset_do_userquota_updates(ds->ds_objset, tx);
67369962b5Matthew Ahrens	}
67494c2d0eMatthew Ahrens	taskq_wait(dp->dp_sync_taskq);
6751484342Matthew Ahrens
6761484342Matthew Ahrens	/*
6771484342Matthew Ahrens	 * Sync the datasets again to push out the changes due to
6783f9d6adLin Ling	 * userspace updates.  This must be done before we process the
679ce636f8Matthew Ahrens	 * sync tasks, so that any snapshots will have the correct
680ce636f8Matthew Ahrens	 * user accounting information (and we won't get confused
681ce636f8Matthew Ahrens	 * about which blocks are part of the snapshot).
6821484342Matthew Ahrens	 */
6831484342Matthew Ahrens	zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
68469962b5Matthew Ahrens	while ((ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) != NULL) {
685eb63303Tom Caputi		objset_t *os = ds->ds_objset;
686eb63303Tom Caputi
6871484342Matthew Ahrens		ASSERT(list_link_active(&ds->ds_synced_link));
6881484342Matthew Ahrens		dmu_buf_rele(ds->ds_dbuf, ds);
6891484342Matthew Ahrens		dsl_dataset_sync(ds, zio, tx);
690eb63303Tom Caputi
691eb63303Tom Caputi		/*
692eb63303Tom Caputi		 * Release any key mappings created by calls to
693eb63303Tom Caputi		 * dsl_dataset_dirty() from the userquota accounting
694eb63303Tom Caputi		 * code paths.
695eb63303Tom Caputi		 */
696eb63303Tom Caputi		if (os->os_encrypted && !os->os_raw_receive &&
697eb63303Tom Caputi		    !os->os_next_write_raw[txg & TXG_MASK]) {
698eb63303Tom Caputi			ASSERT3P(ds->ds_key_mapping, !=, NULL);
699eb63303Tom Caputi			key_mapping_rele(dp->dp_spa, ds->ds_key_mapping, ds);