1fa9e406ahrens/*
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 */
21fa9e406ahrens/*
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 */
29fa9e406ahrens
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>
54fa9e406ahrens
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;
1391ab7f2dmaybee
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)
172fa9e406ahrens{
173fa9e406ahrens	uint64_t obj;
174fa9e406ahrens	int err;
175fa9e406ahrens
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);
181fa9e406ahrens
1823b2aab1Matthew Ahrens	return (dsl_dir_hold_obj(dp, obj, name, dp, ddp));
183fa9e406ahrens}
184fa9e406ahrens
185fa9e406ahrensstatic dsl_pool_t *
186fa9e406ahrensdsl_pool_open_impl(spa_t *spa, uint64_t txg)
187fa9e406ahrens{
188fa9e406ahrens	dsl_pool_t *dp;
189fa9e406ahrens	blkptr_t *bp = spa_get_rootblkptr(spa);
190fa9e406ahrens
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);
197fa9e406ahrens
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));
208fa9e406ahrens
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,
217216d772Prakash Surya	    TASKQ_PREPOPULATE | TASKQ_THREADS_CPU_PCT);
218216d772Prakash Surya
2191ab7f2dmaybee	mutex_init(&dp->dp_lock, NULL, MUTEX_DEFAULT, NULL);
22069962b5Matthew Ahrens	cv_init(&dp->dp_spaceavail_cv, NULL, CV_DEFAULT, NULL);
2211ab7f2dmaybee
2229d3574bNeil Perrin	dp->dp_vnrele_taskq = taskq_create("zfs_vn_rele_taskq", 1, minclsyspri,
2239d3574bNeil Perrin	    1, 4, 0);
2249d3574bNeil Perrin
225fa9e406ahrens	return (dp);
226fa9e406ahrens}
227fa9e406ahrens
228ea8dc4beschrockint
229ad135b5Christopher Sidendsl_pool_init(spa_t *spa, uint64_t txg, dsl_pool_t **dpp)
230fa9e406ahrens{
231fa9e406ahrens	int err;
232fa9e406ahrens	dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
233ad135b5Christopher Siden
234ad135b5Christopher Siden	err = dmu_objset_open_impl(spa, NULL, &dp->dp_meta_rootbp,
235ad135b5Christopher Siden	    &dp->dp_meta_objset);
236ad135b5Christopher Siden	if (err != 0)
237ad135b5Christopher Siden		dsl_pool_close(dp);
238ad135b5Christopher Siden	else
239ad135b5Christopher Siden		*dpp = dp;
240ad135b5Christopher Siden
241ad135b5Christopher Siden	return (err);
242ad135b5Christopher Siden}
243ad135b5Christopher Siden
244ad135b5Christopher Sidenint
245ad135b5Christopher Sidendsl_pool_open(dsl_pool_t *dp)
246ad135b5Christopher Siden{
247ad135b5Christopher Siden	int err;
248088f389ahrens	dsl_dir_t *dd;
249088f389ahrens	dsl_dataset_t *ds;
250cde58dbMatthew Ahrens	uint64_t obj;
251fa9e406ahrens
2523b2aab1Matthew Ahrens	rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
253fa9e406ahrens	err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
254fa9e406ahrens	    DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1,
255fa9e406ahrens	    &dp->dp_root_dir_obj);
256ea8dc4beschrock	if (err)
257ea8dc4beschrock		goto out;
258ea8dc4beschrock
2593b2aab1Matthew Ahrens	err = dsl_dir_hold_obj(dp, dp->dp_root_dir_obj,
260ea8dc4beschrock	    NULL, dp, &dp->dp_root_dir);
261ea8dc4beschrock	if (err)
262ea8dc4beschrock		goto out;
263fa9e406ahrens
264088f389ahrens	err = dsl_pool_open_special_dir(dp, MOS_DIR_NAME, &dp->dp_mos_dir);
265ea8dc4beschrock	if (err)
266ea8dc4beschrock		goto out;
267ea8dc4beschrock
268ad135b5Christopher Siden	if (spa_version(dp->dp_spa) >= SPA_VERSION_ORIGIN) {
269088f389ahrens		err = dsl_pool_open_special_dir(dp, ORIGIN_DIR_NAME, &dd);
270088f389ahrens		if (err)
271088f389ahrens			goto out;
272c137962Justin T. Gibbs		err = dsl_dataset_hold_obj(dp,
273c137962Justin T. Gibbs		    dsl_dir_phys(dd)->dd_head_dataset_obj, FTAG, &ds);
2748f63aa4Lin Ling		if (err == 0) {
2758f63aa4Lin Ling			err = dsl_dataset_hold_obj(dp,
276c137962Justin T. Gibbs			    dsl_dataset_phys(ds)->ds_prev_snap_obj, dp,
2778f63aa4Lin Ling			    &dp->dp_origin_snap);
2788f63aa4Lin Ling			dsl_dataset_rele(ds, FTAG);
2798f63aa4Lin Ling		}
2803b2aab1Matthew Ahrens		dsl_dir_rele(dd, dp);
281088f389ahrens		if (err)
282088f389ahrens			goto out;
283088f389ahrens	}
284088f389ahrens
285ad135b5Christopher Siden	if (spa_version(dp->dp_spa) >= SPA_VERSION_DEADLISTS) {
286cde58dbMatthew Ahrens		err = dsl_pool_open_special_dir(dp, FREE_DIR_NAME,
287cde58dbMatthew Ahrens		    &dp->dp_free_dir);
288cde58dbMatthew Ahrens		if (err)
289cde58dbMatthew Ahrens			goto out;
290cde58dbMatthew Ahrens
291cde58dbMatthew Ahrens		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
292cde58dbMatthew Ahrens		    DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj);
293cde58dbMatthew Ahrens		if (err)
294cde58dbMatthew Ahrens			goto out;
2953b2aab1Matthew Ahrens		VERIFY0(bpobj_open(&dp->dp_free_bpobj,
296cde58dbMatthew Ahrens		    dp->dp_meta_objset, obj));
297cde58dbMatthew Ahrens	}
298cde58dbMatthew Ahrens
2995cabbc6Prashanth Sreenivasa	if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_OBSOLETE_COUNTS)) {
3005cabbc6Prashanth Sreenivasa		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
3015cabbc6Prashanth Sreenivasa		    DMU_POOL_OBSOLETE_BPOBJ, sizeof (uint64_t), 1, &obj);
3025cabbc6Prashanth Sreenivasa		if (err == 0) {
3035cabbc6Prashanth Sreenivasa			VERIFY0(bpobj_open(&dp->dp_obsolete_bpobj,
3045cabbc6Prashanth Sreenivasa			    dp->dp_meta_objset, obj));
3055cabbc6Prashanth Sreenivasa		} else if (err == ENOENT) {
3065cabbc6Prashanth Sreenivasa			/*
3075cabbc6Prashanth Sreenivasa			 * We might not have created the remap bpobj yet.
3085cabbc6Prashanth Sreenivasa			 */
3095cabbc6Prashanth Sreenivasa			err = 0;
3105cabbc6Prashanth Sreenivasa		} else {
3115cabbc6Prashanth Sreenivasa			goto out;
3125cabbc6Prashanth Sreenivasa		}
3135cabbc6Prashanth Sreenivasa	}
3145cabbc6Prashanth Sreenivasa
3157fd05acMatthew Ahrens	/*
3165cabbc6Prashanth Sreenivasa	 * Note: errors ignored, because the these special dirs, used for
3175cabbc6Prashanth Sreenivasa	 * space accounting, are only created on demand.
3187fd05acMatthew Ahrens	 */
3197fd05acMatthew Ahrens	(void) dsl_pool_open_special_dir(dp, LEAK_DIR_NAME,
3207fd05acMatthew Ahrens	    &dp->dp_leak_dir);
3217fd05acMatthew Ahrens
3222acef22Matthew Ahrens	if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_ASYNC_DESTROY)) {
323ad135b5Christopher Siden		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
324ad135b5Christopher Siden		    DMU_POOL_BPTREE_OBJ, sizeof (uint64_t), 1,
325ad135b5Christopher Siden		    &dp->dp_bptree_obj);
326ad135b5Christopher Siden		if (err != 0)
327ad135b5Christopher Siden			goto out;
328ad135b5Christopher Siden	}
329ad135b5Christopher Siden
3302acef22Matthew Ahrens	if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_EMPTY_BPOBJ)) {
331f174573Matthew Ahrens		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
332f174573Matthew Ahrens		    DMU_POOL_EMPTY_BPOBJ, sizeof (uint64_t), 1,
333f174573Matthew Ahrens		    &dp->dp_empty_bpobj);
334f174573Matthew Ahrens		if (err != 0)
335f174573Matthew Ahrens			goto out;
336f174573Matthew Ahrens	}
337f174573Matthew Ahrens
338ca45db4Chris Kirby	err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
339ca45db4Chris Kirby	    DMU_POOL_TMP_USERREFS, sizeof (uint64_t), 1,
340ca45db4Chris Kirby	    &dp->dp_tmp_userrefs_obj);
341ca45db4Chris Kirby	if (err == ENOENT)
342ca45db4Chris Kirby		err = 0;
343ca45db4Chris Kirby	if (err)
344ca45db4Chris Kirby		goto out;
345ca45db4Chris Kirby
346ad135b5Christopher Siden	err = dsl_scan_init(dp, dp->dp_tx.tx_open_txg);
347088f389ahrens
348ea8dc4beschrockout:
3493b2aab1Matthew Ahrens	rrw_exit(&dp->dp_config_rwlock, FTAG);
350ea8dc4beschrock	return (err);
351fa9e406ahrens}
352fa9e406ahrens
353fa9e406ahrensvoid
354fa9e406ahrensdsl_pool_close(dsl_pool_t *dp)
355fa9e406ahrens{
356088f389ahrens	/*
35769962b5Matthew Ahrens	 * Drop our references from dsl_pool_open().
35869962b5Matthew Ahrens	 *
359088f389ahrens	 * Since we held the origin_snap from "syncing" context (which
360088f389ahrens	 * includes pool-opening context), it actually only got a "ref"
361088f389ahrens	 * and not a hold, so just drop that here.
362088f389ahrens	 */
3635cabbc6Prashanth Sreenivasa	if (dp->dp_origin_snap != NULL)
3643b2aab1Matthew Ahrens		dsl_dataset_rele(dp->dp_origin_snap, dp);
3655cabbc6Prashanth Sreenivasa	if (dp->dp_mos_dir != NULL)
3663b2aab1Matthew Ahrens		dsl_dir_rele(dp->dp_mos_dir, dp);
3675cabbc6Prashanth Sreenivasa	if (dp->dp_free_dir != NULL)
3683b2aab1Matthew Ahrens		dsl_dir_rele(dp->dp_free_dir, dp);
3695cabbc6Prashanth Sreenivasa	if (dp->dp_leak_dir != NULL)
3707fd05acMatthew Ahrens		dsl_dir_rele(dp->dp_leak_dir, dp);
3715cabbc6Prashanth Sreenivasa	if (dp->dp_root_dir != NULL)
3723b2aab1Matthew Ahrens		dsl_dir_rele(dp->dp_root_dir, dp);
373fa9e406ahrens
374cde58dbMatthew Ahrens	bpobj_close(&dp->dp_free_bpobj);
3755cabbc6Prashanth Sreenivasa	bpobj_close(&dp->dp_obsolete_bpobj);
376cde58dbMatthew Ahrens
377fa9e406ahrens	/* undo the dmu_objset_open_impl(mos) from dsl_pool_open() */
3785cabbc6Prashanth Sreenivasa	if (dp->dp_meta_objset != NULL)
379503ad85Matthew Ahrens		dmu_objset_evict(dp->dp_meta_objset);
380fa9e406ahrens
381fa9e406ahrens	txg_list_destroy(&dp->dp_dirty_datasets);
382ce636f8Matthew Ahrens	txg_list_destroy(&dp->dp_dirty_zilogs);
38354a9111Chris Kirby	txg_list_destroy(&dp->dp_sync_tasks);
3848671400Serapheim Dimitropoulos	txg_list_destroy(&dp->dp_early_sync_tasks);
385fa9e406ahrens	txg_list_destroy(&dp->dp_dirty_dirs);
386fa9e406ahrens
387216d772Prakash Surya	taskq_destroy(dp->dp_zil_clean_taskq);
38894c2d0eMatthew Ahrens	taskq_destroy(dp->dp_sync_taskq);
38994c2d0eMatthew Ahrens
390244781fPrakash Surya	/*
391244781fPrakash Surya	 * We can't set retry to TRUE since we're explicitly specifying
392244781fPrakash Surya	 * a spa to flush. This is good enough; any missed buffers for
393244781fPrakash Surya	 * this spa won't cause trouble, and they'll eventually fall
394244781fPrakash Surya	 * out of the ARC just like any other unused buffer.
395244781fPrakash Surya	 */
396244781fPrakash Surya	arc_flush(dp->dp_spa, FALSE);
397244781fPrakash Surya
398e0f1c0aOlaf Faaland	mmp_fini(dp->dp_spa);
399fa9e406ahrens	txg_fini(dp);
4003f9d6adLin Ling	dsl_scan_fini(dp);
401bc9014eJustin Gibbs	dmu_buf_user_evict_wait();
402bc9014eJustin Gibbs
4033b2aab1Matthew Ahrens	rrw_destroy(&dp->dp_config_rwlock);
4041ab7f2dmaybee	mutex_destroy(&dp->dp_lock);
4059d3574bNeil Perrin	taskq_destroy(dp->dp_vnrele_taskq);
4065cabbc6Prashanth Sreenivasa	if (dp->dp_blkstats != NULL)
40788b7b0fMatthew Ahrens		kmem_free(dp->dp_blkstats, sizeof (zfs_all_blkstats_t));
408fa9e406ahrens	kmem_free(dp, sizeof (dsl_pool_t));
409fa9e406ahrens}
410fa9e406ahrens
4115cabbc6Prashanth Sreenivasavoid
4125cabbc6Prashanth Sreenivasadsl_pool_create_obsolete_bpobj(dsl_pool_t *dp, dmu_tx_t *tx)
4135cabbc6Prashanth Sreenivasa{
4145cabbc6Prashanth Sreenivasa	uint64_t obj;
4155cabbc6Prashanth Sreenivasa	/*
4165cabbc6Prashanth Sreenivasa	 * Currently, we only create the obsolete_bpobj where there are
4175cabbc6Prashanth Sreenivasa	 * indirect vdevs with referenced mappings.
4185cabbc6Prashanth Sreenivasa	 */
4195cabbc6Prashanth Sreenivasa	ASSERT(spa_feature_is_active(dp->dp_spa, SPA_FEATURE_DEVICE_REMOVAL));
4205cabbc6Prashanth Sreenivasa	/* create and open the obsolete_bpobj */
4215cabbc6Prashanth Sreenivasa	obj = bpobj_alloc(dp->dp_meta_objset, SPA_OLD_MAXBLOCKSIZE, tx);
4225cabbc6Prashanth Sreenivasa	VERIFY0(bpobj_open(&dp->dp_obsolete_bpobj, dp->dp_meta_objset, obj));
4235cabbc6Prashanth Sreenivasa	VERIFY0(zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
4245cabbc6Prashanth Sreenivasa	    DMU_POOL_OBSOLETE_BPOBJ, sizeof (uint64_t), 1, &obj, tx));
4255cabbc6Prashanth Sreenivasa	spa_feature_incr(dp->dp_spa, SPA_FEATURE_OBSOLETE_COUNTS, tx);
4265cabbc6Prashanth Sreenivasa}
4275cabbc6Prashanth Sreenivasa
4285cabbc6Prashanth Sreenivasavoid
4295cabbc6Prashanth Sreenivasadsl_pool_destroy_obsolete_bpobj(dsl_pool_t *dp, dmu_tx_t *tx)
4305cabbc6Prashanth Sreenivasa{
4315cabbc6Prashanth Sreenivasa	spa_feature_decr(dp->dp_spa, SPA_FEATURE_OBSOLETE_COUNTS, tx);
4325cabbc6Prashanth Sreenivasa	VERIFY0(zap_remove(dp->dp_meta_objset,
4335cabbc6Prashanth Sreenivasa	    DMU_POOL_DIRECTORY_OBJECT,
4345cabbc6Prashanth Sreenivasa	    DMU_POOL_OBSOLETE_BPOBJ, tx));
4355cabbc6Prashanth Sreenivasa	bpobj_free(dp->dp_meta_objset,
4365cabbc6Prashanth Sreenivasa	    dp->dp_obsolete_bpobj.bpo_object, tx);
4375cabbc6Prashanth Sreenivasa	bpobj_close(&dp->dp_obsolete_bpobj);
4385cabbc6Prashanth Sreenivasa}
4395cabbc6Prashanth Sreenivasa
440fa9e406ahrensdsl_pool_t *
441eb63303Tom Caputidsl_pool_create(spa_t *spa, nvlist_t *zplprops, dsl_crypto_params_t *dcp,
442eb63303Tom Caputi    uint64_t txg)
443fa9e406ahrens{
444fa9e406ahrens	int err;
445fa9e406ahrens	dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
446fa9e406ahrens	dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg);
447088f389ahrens	dsl_dataset_t *ds;
448cde58dbMatthew Ahrens	uint64_t obj;
449088f389ahrens
4503b2aab1Matthew Ahrens	rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
4513b2aab1Matthew Ahrens
452088f389ahrens	/* create and open the MOS (meta-objset) */
453503ad85Matthew Ahrens	dp->dp_meta_objset = dmu_objset_create_impl(spa,
454503ad85Matthew Ahrens	    NULL, &dp->dp_meta_rootbp, DMU_OST_META, tx);
455eb63303Tom Caputi	spa->spa_meta_objset = dp->dp_meta_objset;
456fa9e406ahrens
457fa9e406ahrens	/* create the pool directory */
458fa9e406ahrens	err = zap_create_claim(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
459fa9e406ahrens	    DMU_OT_OBJECT_DIRECTORY, DMU_OT_NONE, 0, tx);
460fb09f5aMadhav Suresh	ASSERT0(err);
461fa9e406ahrens
4623f9d6adLin Ling	/* Initialize scan structures */
4633b2aab1Matthew Ahrens	VERIFY0(dsl_scan_init(dp, txg));
4643f9d6adLin Ling
465fa9e406ahrens	/* create and open the root dir */
466088f389ahrens	dp->dp_root_dir_obj = dsl_dir_create_sync(dp, NULL, NULL, tx);
4673b2aab1Matthew Ahrens	VERIFY0(dsl_dir_hold_obj(dp, dp->dp_root_dir_obj,
468ea8dc4beschrock	    NULL, dp, &dp->dp_root_dir));
469fa9e406ahrens
470fa9e406ahrens	/* create and open the meta-objset dir */
471088f389ahrens	(void) dsl_dir_create_sync(dp, dp->dp_root_dir, MOS_DIR_NAME, tx);
4723b2aab1Matthew Ahrens	VERIFY0(dsl_pool_open_special_dir(dp,
473088f389ahrens	    MOS_DIR_NAME, &dp->dp_mos_dir));
474088f389ahrens
475cde58dbMatthew Ahrens	if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
476cde58dbMatthew Ahrens		/* create and open the free dir */
477cde58dbMatthew Ahrens		(void) dsl_dir_create_sync(dp, dp->dp_root_dir,
478cde58dbMatthew Ahrens		    FREE_DIR_NAME, tx);
4793b2aab1Matthew Ahrens		VERIFY0(dsl_pool_open_special_dir(dp,
480cde58dbMatthew Ahrens		    FREE_DIR_NAME, &dp->dp_free_dir));
481cde58dbMatthew Ahrens
482cde58dbMatthew Ahrens		/* create and open the free_bplist */
483b515258Matthew Ahrens		obj = bpobj_alloc(dp->dp_meta_objset, SPA_OLD_MAXBLOCKSIZE, tx);
484cde58dbMatthew Ahrens		VERIFY(zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
485cde58dbMatthew Ahrens		    DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj, tx) == 0);
4863b2aab1Matthew Ahrens		VERIFY0(bpobj_open(&dp->dp_free_bpobj,
487cde58dbMatthew Ahrens		    dp->dp_meta_objset, obj));
488cde58dbMatthew Ahrens	}
489cde58dbMatthew Ahrens
490088f389ahrens	if (spa_version(spa) >= SPA_VERSION_DSL_SCRUB)
491088f389ahrens		dsl_pool_create_origin(dp, tx);
492088f389ahrens
493eb63303Tom Caputi	/*
494eb63303Tom Caputi	 * Some features may be needed when creating the root dataset, so we
495eb63303Tom Caputi	 * create the feature objects here.
496eb63303Tom Caputi	 */
497eb63303Tom Caputi	if (spa_version(spa) >= SPA_VERSION_FEATURES)
498eb63303Tom Caputi		spa_feature_create_zap_objects(spa, tx);
499eb63303Tom Caputi
500eb63303Tom Caputi	if (dcp != NULL && dcp->cp_crypt != ZIO_CRYPT_OFF &&
501eb63303Tom Caputi	    dcp->cp_crypt != ZIO_CRYPT_INHERIT)
502eb63303Tom Caputi		spa_feature_enable(spa, SPA_FEATURE_ENCRYPTION, tx);
503eb63303Tom Caputi
504088f389ahrens	/* create the root dataset */
505eb63303Tom Caputi	obj = dsl_dataset_create_sync_dd(dp->dp_root_dir, NULL, dcp, 0, tx);
506088f389ahrens
507088f389ahrens	/* create the root objset */
508eb63303Tom Caputi	VERIFY0(dsl_dataset_hold_obj_flags(dp, obj,
509eb63303Tom Caputi	    DS_HOLD_FLAG_DECRYPT, FTAG, &ds));
510088f389ahrens#ifdef _KERNEL
511b852c2fToomas Soome	{
512b852c2fToomas Soome		objset_t *os;
513b852c2fToomas Soome		rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
514b852c2fToomas Soome		os = dmu_objset_create_impl(dp->dp_spa, ds,
515b852c2fToomas Soome		    dsl_dataset_get_blkptr(ds), DMU_OST_ZFS, tx);
516b852c2fToomas Soome		rrw_exit(&ds->ds_bp_rwlock, FTAG);
517b852c2fToomas Soome		zfs_create_fs(os, kcred, zplprops, tx);
518b852c2fToomas Soome	}
519088f389ahrens#endif
520eb63303Tom Caputi	dsl_dataset_rele_flags(ds, DS_HOLD_FLAG_DECRYPT, FTAG);
521fa9e406ahrens
522fa9e406ahrens	dmu_tx_commit(tx);
523fa9e406ahrens
5243b2aab1Matthew Ahrens	rrw_exit(&dp->dp_config_rwlock, FTAG);
5253b2aab1Matthew Ahrens
526fa9e406ahrens	return (dp);
527fa9e406ahrens}
528fa9e406ahrens
529ce636f8Matthew Ahrens/*
530ce636f8Matthew Ahrens * Account for the meta-objset space in its placeholder dsl_dir.
531ce636f8Matthew Ahrens */
532ce636f8Matthew Ahrensvoid
533ce636f8Matthew Ahrensdsl_pool_mos_diduse_space(dsl_pool_t *dp,
534ce636f8Matthew Ahrens    int64_t used, int64_t comp, int64_t uncomp)
535ce636f8Matthew Ahrens{
536ce636f8Matthew Ahrens	ASSERT3U(comp, ==, uncomp); /* it's all metadata */
537ce636f8Matthew Ahrens	mutex_enter(&dp->dp_lock);
538ce636f8Matthew Ahrens	dp->dp_mos_used_delta += used;
539ce636f8Matthew Ahrens	dp->dp_mos_compressed_delta += comp;
540ce636f8Matthew Ahrens	dp->dp_mos_uncompressed_delta += uncomp;
541ce636f8Matthew Ahrens	mutex_exit(&dp->dp_lock);
542ce636f8Matthew Ahrens}
543ce636f8Matthew Ahrens
54469962b5Matthew Ahrensstatic void
54569962b5Matthew Ahrensdsl_pool_sync_mos(dsl_pool_t *dp, dmu_tx_t *tx)
54669962b5Matthew Ahrens{
54769962b5Matthew Ahrens	zio_t *zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
54869962b5Matthew Ahrens	dmu_objset_sync(dp->dp_meta_objset, zio, tx);
54969962b5Matthew Ahrens	VERIFY0(zio_wait(zio));
55069962b5Matthew Ahrens	dprintf_bp(&dp->dp_meta_rootbp, "meta objset rootbp is %s", "");
55169962b5Matthew Ahrens	spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
55269962b5Matthew Ahrens}
55369962b5Matthew Ahrens
55469962b5Matthew Ahrensstatic void
55569962b5Matthew Ahrensdsl_pool_dirty_delta(dsl_pool_t *dp, int64_t delta)
55669962b5Matthew Ahrens{
55769962b5Matthew Ahrens	ASSERT(MUTEX_HELD(&dp->dp_lock));
55869962b5Matthew Ahrens
55969962b5Matthew Ahrens	if (delta < 0)
56069962b5Matthew Ahrens		ASSERT3U(-delta, <=, dp->dp_dirty_total);
56169962b5Matthew Ahrens
56269962b5Matthew Ahrens	dp->dp_dirty_total += delta;
56369962b5Matthew Ahrens
56469962b5Matthew Ahrens	/*
56569962b5Matthew Ahrens	 * Note: we signal even when increasing dp_dirty_total.
56669962b5Matthew Ahrens	 * This ensures forward progress -- each thread wakes the next waiter.
56769962b5Matthew Ahrens	 */
568313ae1eAndriy Gapon	if (dp->dp_dirty_total < zfs_dirty_data_max)
56969962b5Matthew Ahrens		cv_signal(&dp->dp_spaceavail_cv);
57069962b5Matthew Ahrens}
57169962b5Matthew Ahrens
5728671400Serapheim Dimitropoulosstatic boolean_t
5738671400Serapheim Dimitropoulosdsl_early_sync_task_verify(dsl_pool_t *dp, uint64_t txg)
5748671400Serapheim Dimitropoulos{
5758671400Serapheim Dimitropoulos	spa_t *spa = dp->dp_spa;
5768671400Serapheim Dimitropoulos	vdev_t *rvd = spa->spa_root_vdev;
5778671400Serapheim Dimitropoulos
5788671400Serapheim Dimitropoulos	for (uint64_t c = 0; c < rvd->vdev_children; c++) {
5798671400Serapheim Dimitropoulos		vdev_t *vd = rvd->vdev_child[c];
5808671400Serapheim Dimitropoulos		txg_list_t *tl = &vd->vdev_ms_list;
5818671400Serapheim Dimitropoulos		metaslab_t *ms;
5828671400Serapheim Dimitropoulos
5838671400Serapheim Dimitropoulos		for (ms = txg_list_head(tl, TXG_CLEAN(txg)); ms;
5848671400Serapheim Dimitropoulos		    ms = txg_list_next(tl, ms, TXG_CLEAN(txg))) {
5858671400Serapheim Dimitropoulos			VERIFY(range_tree_is_empty(ms->ms_freeing));
5868671400Serapheim Dimitropoulos			VERIFY(range_tree_is_empty(ms->ms_checkpointing));
5878671400Serapheim Dimitropoulos		}
5888671400Serapheim Dimitropoulos	}
5898671400Serapheim Dimitropoulos
5908671400Serapheim Dimitropoulos	return (B_TRUE);
5918671400Serapheim Dimitropoulos}
5928671400Serapheim Dimitropoulos
593fa9e406ahrensvoid
594fa9e406ahrensdsl_pool_sync(dsl_pool_t *dp, uint64_t txg)
595fa9e406ahrens{
596c717a56maybee	zio_t *zio;
597fa9e406ahrens	dmu_tx_t *tx;
598c717a56maybee	dsl_dir_t *dd;
599c717a56maybee	dsl_dataset_t *ds;
600503ad85Matthew Ahrens	objset_t *mos = dp->dp_meta_objset;
601ce636f8Matthew Ahrens	list_t synced_datasets;
602ce636f8Matthew Ahrens
603ce636f8Matthew Ahrens	list_create(&synced_datasets, sizeof (dsl_dataset_t),
604ce636f8Matthew Ahrens	    offsetof(dsl_dataset_t, ds_synced_link));
605fa9e406ahrens
606fa9e406ahrens	tx = dmu_tx_create_assigned(dp, txg);
607fa9e406ahrens
60869962b5Matthew Ahrens	/*
6098671400Serapheim Dimitropoulos	 * Run all early sync tasks before writing out any dirty blocks.
6108671400Serapheim Dimitropoulos	 * For more info on early sync tasks see block comment in
6118671400Serapheim Dimitropoulos	 * dsl_early_sync_task().
6128671400Serapheim Dimitropoulos	 */
6138671400Serapheim Dimitropoulos	if (!txg_list_empty(&dp->dp_early_sync_tasks, txg)) {
6148671400Serapheim Dimitropoulos		dsl_sync_task_t *dst;
6158671400Serapheim Dimitropoulos
6168671400Serapheim Dimitropoulos		ASSERT3U(spa_sync_pass(dp->dp_spa), ==, 1);
6178671400Serapheim Dimitropoulos		while ((dst =
6188671400Serapheim Dimitropoulos		    txg_list_remove(&dp->dp_early_sync_tasks, txg)) != NULL) {
6198671400Serapheim Dimitropoulos			ASSERT(dsl_early_sync_task_verify(dp, txg));
6208671400Serapheim Dimitropoulos			dsl_sync_task_sync(dst, tx);
6218671400Serapheim Dimitropoulos		}
6228671400Serapheim Dimitropoulos		ASSERT(dsl_early_sync_task_verify(dp, txg));
6238671400Serapheim Dimitropoulos	}
6248671400Serapheim Dimitropoulos
6258671400Serapheim Dimitropoulos	/*
62669962b5Matthew Ahrens	 * Write out all dirty blocks of dirty datasets.
62769962b5Matthew Ahrens	 */
628c717a56maybee	zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
62969962b5Matthew Ahrens	while ((ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) != NULL) {
6301484342Matthew Ahrens		/*
6311484342Matthew Ahrens		 * We must not sync any non-MOS datasets twice, because
6321484342Matthew Ahrens		 * we may have taken a snapshot of them.  However, we
6331484342Matthew Ahrens		 * may sync newly-created datasets on pass 2.
6341484342Matthew Ahrens		 */
6351484342Matthew Ahrens		ASSERT(!list_link_active(&ds->ds_synced_link));
636ce636f8Matthew Ahrens		list_insert_tail(&synced_datasets, ds);
637c717a56maybee		dsl_dataset_sync(ds, zio, tx);
638c717a56maybee	}
63969962b5Matthew Ahrens	VERIFY0(zio_wait(zio));
6401484342Matthew Ahrens
64169962b5Matthew Ahrens	/*
64269962b5Matthew Ahrens	 * We have written all of the accounted dirty data, so our
64369962b5Matthew Ahrens	 * dp_space_towrite should now be zero.  However, some seldom-used
64469962b5Matthew Ahrens	 * code paths do not adhere to this (e.g. dbuf_undirty(), also
64569962b5Matthew Ahrens	 * rounding error in dbuf_write_physdone).
64669962b5Matthew Ahrens	 * Shore up the accounting of any dirtied space now.
64769962b5Matthew Ahrens	 */
64869962b5Matthew Ahrens	dsl_pool_undirty_space(dp, dp->dp_dirty_pertxg[txg & TXG_MASK], txg);
649c717a56maybee
650ce636f8Matthew Ahrens	/*
651ff5177eAlek Pinchuk	 * Update the long range free counter after
652ff5177eAlek Pinchuk	 * we're done syncing user data
653ff5177eAlek Pinchuk	 */
654ff5177eAlek Pinchuk	mutex_enter(&dp->dp_lock);
655ff5177eAlek Pinchuk	ASSERT(spa_sync_pass(dp->dp_spa) == 1 ||
656ff5177eAlek Pinchuk	    dp->dp_long_free_dirty_pertxg[txg & TXG_MASK] == 0);
657ff5177eAlek Pinchuk	dp->dp_long_free_dirty_pertxg[txg & TXG_MASK] = 0;
658ff5177eAlek Pinchuk	mutex_exit(&dp->dp_lock);
659ff5177eAlek Pinchuk
660ff5177eAlek Pinchuk	/*
661ce636f8Matthew Ahrens	 * After the data blocks have been written (ensured by the zio_wait()
662f67950bNasf-Fan	 * above), update the user/group/project space accounting.  This happens
66394c2d0eMatthew Ahrens	 * in tasks dispatched to dp_sync_taskq, so wait for them before
66494c2d0eMatthew Ahrens	 * continuing.
665ce636f8Matthew Ahrens	 */
66669962b5Matthew Ahrens	for (ds = list_head(&synced_datasets); ds != NULL;
66769962b5Matthew Ahrens	    ds = list_next(&synced_datasets, ds)) {
6680a586ceMark Shellenbaum		dmu_objset_do_userquota_updates(ds->ds_objset, tx);
66969962b5Matthew Ahrens	}
67094c2d0eMatthew Ahrens	taskq_wait(dp->dp_sync_taskq);
6711484342Matthew Ahrens
6721484342Matthew Ahrens	/*
6731484342Matthew Ahrens	 * Sync the datasets again to push out the changes due to
6743f9d6adLin Ling	 * userspace updates.  This must be done before we process the
675ce636f8Matthew Ahrens	 * sync tasks, so that any snapshots will have the correct
676ce636f8Matthew Ahrens	 * user accounting information (and we won't get confused
677ce636f8Matthew Ahrens	 * about which blocks are part of the snapshot).
6781484342Matthew Ahrens	 */
6791484342Matthew Ahrens	zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
68069962b5Matthew Ahrens	while ((ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) != NULL) {
681eb63303Tom Caputi		objset_t *os = ds->ds_objset;
682eb63303Tom Caputi
6831484342Matthew Ahrens		ASSERT(list_link_active(&ds->ds_synced_link));
6841484342Matthew Ahrens		dmu_buf_rele(ds->ds_dbuf, ds);
6851484342Matthew Ahrens		dsl_dataset_sync(ds, zio, tx);
686eb63303Tom Caputi
687eb63303Tom Caputi		/*
688eb63303Tom Caputi		 * Release any key mappings created by calls to
689eb63303Tom Caputi		 * dsl_dataset_dirty() from the userquota accounting
690eb63303Tom Caputi		 * code paths.
691eb63303Tom Caputi		 */
692eb63303Tom Caputi		if (os->os_encrypted && !os->os_raw_receive &&
693eb63303Tom Caputi		    !os->os_next_write_raw[txg & TXG_MASK]) {
694eb63303Tom Caputi			ASSERT3P(ds->ds_key_mapping, !=, NULL);
695eb63303Tom Caputi			key_mapping_rele(dp->dp_spa, ds->ds_key_mapping, ds);
696eb63303Tom Caputi		}
6971484342Matthew Ahrens	}
69869962b5Matthew Ahrens	VERIFY0(zio_wait(zio));
6991484342Matthew Ahrens
700b24ab67Jeff Bonwick	/*
701ce636f8Matthew Ahrens	 * Now that the datasets have been completely synced, we can
702ce636f8Matthew Ahrens	 * clean up our in-memory structures accum