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