4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
24 * Copyright (c) 2013 Steven Hartland. All rights reserved.
25 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
26 * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
29 #include <sys/dsl_pool.h>
30 #include <sys/dsl_dataset.h>
31 #include <sys/dsl_prop.h>
32 #include <sys/dsl_dir.h>
33 #include <sys/dsl_synctask.h>
34 #include <sys/dsl_scan.h>
35 #include <sys/dnode.h>
36 #include <sys/dmu_tx.h>
37 #include <sys/dmu_objset.h>
41 #include <sys/zfs_context.h>
42 #include <sys/fs/zfs.h>
43 #include <sys/zfs_znode.h>
44 #include <sys/spa_impl.h>
45 #include <sys/dsl_deadlist.h>
46 #include <sys/bptree.h>
47 #include <sys/zfeature.h>
48 #include <sys/zil_impl.h>
49 #include <sys/dsl_userhold.h>
50 #include <sys/trace_txg.h>
56 * ZFS must limit the rate of incoming writes to the rate at which it is able
57 * to sync data modifications to the backend storage. Throttling by too much
58 * creates an artificial limit; throttling by too little can only be sustained
59 * for short periods and would lead to highly lumpy performance. On a per-pool
60 * basis, ZFS tracks the amount of modified (dirty) data. As operations change
61 * data, the amount of dirty data increases; as ZFS syncs out data, the amount
62 * of dirty data decreases. When the amount of dirty data exceeds a
63 * predetermined threshold further modifications are blocked until the amount
64 * of dirty data decreases (as data is synced out).
66 * The limit on dirty data is tunable, and should be adjusted according to
67 * both the IO capacity and available memory of the system. The larger the
68 * window, the more ZFS is able to aggregate and amortize metadata (and data)
69 * changes. However, memory is a limited resource, and allowing for more dirty
70 * data comes at the cost of keeping other useful data in memory (for example
71 * ZFS data cached by the ARC).
75 * As buffers are modified dsl_pool_willuse_space() increments both the per-
76 * txg (dp_dirty_pertxg[]) and poolwide (dp_dirty_total) accounting of
77 * dirty space used; dsl_pool_dirty_space() decrements those values as data
78 * is synced out from dsl_pool_sync(). While only the poolwide value is
79 * relevant, the per-txg value is useful for debugging. The tunable
80 * zfs_dirty_data_max determines the dirty space limit. Once that value is
81 * exceeded, new writes are halted until space frees up.
83 * The zfs_dirty_data_sync tunable dictates the threshold at which we
84 * ensure that there is a txg syncing (see the comment in txg.c for a full
85 * description of transaction group stages).
87 * The IO scheduler uses both the dirty space limit and current amount of
88 * dirty data as inputs. Those values affect the number of concurrent IOs ZFS
89 * issues. See the comment in vdev_queue.c for details of the IO scheduler.
91 * The delay is also calculated based on the amount of dirty data. See the
92 * comment above dmu_tx_delay() for details.
96 * zfs_dirty_data_max will be set to zfs_dirty_data_max_percent% of all memory,
97 * capped at zfs_dirty_data_max_max. It can also be overridden with a module
100 unsigned long zfs_dirty_data_max
= 0;
101 unsigned long zfs_dirty_data_max_max
= 0;
102 int zfs_dirty_data_max_percent
= 10;
103 int zfs_dirty_data_max_max_percent
= 25;
106 * If there is at least this much dirty data, push out a txg.
108 unsigned long zfs_dirty_data_sync
= 64 * 1024 * 1024;
111 * Once there is this amount of dirty data, the dmu_tx_delay() will kick in
112 * and delay each transaction.
113 * This value should be >= zfs_vdev_async_write_active_max_dirty_percent.
115 int zfs_delay_min_dirty_percent
= 60;
118 * This controls how quickly the delay approaches infinity.
119 * Larger values cause it to delay more for a given amount of dirty data.
120 * Therefore larger values will cause there to be less dirty data for a
123 * For the smoothest delay, this value should be about 1 billion divided
124 * by the maximum number of operations per second. This will smoothly
125 * handle between 10x and 1/10th this number.
127 * Note: zfs_delay_scale * zfs_dirty_data_max must be < 2^64, due to the
128 * multiply in dmu_tx_delay().
130 unsigned long zfs_delay_scale
= 1000 * 1000 * 1000 / 2000;
132 hrtime_t zfs_throttle_delay
= MSEC2NSEC(10);
133 hrtime_t zfs_throttle_resolution
= MSEC2NSEC(10);
136 * This determines the number of threads used by the dp_sync_taskq.
138 int zfs_sync_taskq_batch_pct
= 75;
141 dsl_pool_open_special_dir(dsl_pool_t
*dp
, const char *name
, dsl_dir_t
**ddp
)
146 err
= zap_lookup(dp
->dp_meta_objset
,
147 dsl_dir_phys(dp
->dp_root_dir
)->dd_child_dir_zapobj
,
148 name
, sizeof (obj
), 1, &obj
);
152 return (dsl_dir_hold_obj(dp
, obj
, name
, dp
, ddp
));
156 dsl_pool_open_impl(spa_t
*spa
, uint64_t txg
)
159 blkptr_t
*bp
= spa_get_rootblkptr(spa
);
161 dp
= kmem_zalloc(sizeof (dsl_pool_t
), KM_SLEEP
);
163 dp
->dp_meta_rootbp
= *bp
;
164 rrw_init(&dp
->dp_config_rwlock
, B_TRUE
);
167 txg_list_create(&dp
->dp_dirty_datasets
,
168 offsetof(dsl_dataset_t
, ds_dirty_link
));
169 txg_list_create(&dp
->dp_dirty_zilogs
,
170 offsetof(zilog_t
, zl_dirty_link
));
171 txg_list_create(&dp
->dp_dirty_dirs
,
172 offsetof(dsl_dir_t
, dd_dirty_link
));
173 txg_list_create(&dp
->dp_sync_tasks
,
174 offsetof(dsl_sync_task_t
, dst_node
));
176 dp
->dp_sync_taskq
= taskq_create("dp_sync_taskq",
177 zfs_sync_taskq_batch_pct
, minclsyspri
, 1, INT_MAX
,
178 TASKQ_THREADS_CPU_PCT
);
180 mutex_init(&dp
->dp_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
181 cv_init(&dp
->dp_spaceavail_cv
, NULL
, CV_DEFAULT
, NULL
);
183 dp
->dp_iput_taskq
= taskq_create("z_iput", max_ncpus
, defclsyspri
,
184 max_ncpus
* 8, INT_MAX
, TASKQ_PREPOPULATE
| TASKQ_DYNAMIC
);
190 dsl_pool_init(spa_t
*spa
, uint64_t txg
, dsl_pool_t
**dpp
)
193 dsl_pool_t
*dp
= dsl_pool_open_impl(spa
, txg
);
196 * Initialize the caller's dsl_pool_t structure before we actually open
197 * the meta objset. This is done because a self-healing write zio may
198 * be issued as part of dmu_objset_open_impl() and the spa needs its
199 * dsl_pool_t initialized in order to handle the write.
203 err
= dmu_objset_open_impl(spa
, NULL
, &dp
->dp_meta_rootbp
,
204 &dp
->dp_meta_objset
);
214 dsl_pool_open(dsl_pool_t
*dp
)
221 rrw_enter(&dp
->dp_config_rwlock
, RW_WRITER
, FTAG
);
222 err
= zap_lookup(dp
->dp_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
223 DMU_POOL_ROOT_DATASET
, sizeof (uint64_t), 1,
224 &dp
->dp_root_dir_obj
);
228 err
= dsl_dir_hold_obj(dp
, dp
->dp_root_dir_obj
,
229 NULL
, dp
, &dp
->dp_root_dir
);
233 err
= dsl_pool_open_special_dir(dp
, MOS_DIR_NAME
, &dp
->dp_mos_dir
);
237 if (spa_version(dp
->dp_spa
) >= SPA_VERSION_ORIGIN
) {
238 err
= dsl_pool_open_special_dir(dp
, ORIGIN_DIR_NAME
, &dd
);
241 err
= dsl_dataset_hold_obj(dp
,
242 dsl_dir_phys(dd
)->dd_head_dataset_obj
, FTAG
, &ds
);
244 err
= dsl_dataset_hold_obj(dp
,
245 dsl_dataset_phys(ds
)->ds_prev_snap_obj
, dp
,
246 &dp
->dp_origin_snap
);
247 dsl_dataset_rele(ds
, FTAG
);
249 dsl_dir_rele(dd
, dp
);
254 if (spa_version(dp
->dp_spa
) >= SPA_VERSION_DEADLISTS
) {
255 err
= dsl_pool_open_special_dir(dp
, FREE_DIR_NAME
,
260 err
= zap_lookup(dp
->dp_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
261 DMU_POOL_FREE_BPOBJ
, sizeof (uint64_t), 1, &obj
);
264 VERIFY0(bpobj_open(&dp
->dp_free_bpobj
,
265 dp
->dp_meta_objset
, obj
));
269 * Note: errors ignored, because the leak dir will not exist if we
270 * have not encountered a leak yet.
272 (void) dsl_pool_open_special_dir(dp
, LEAK_DIR_NAME
,
275 if (spa_feature_is_active(dp
->dp_spa
, SPA_FEATURE_ASYNC_DESTROY
)) {
276 err
= zap_lookup(dp
->dp_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
277 DMU_POOL_BPTREE_OBJ
, sizeof (uint64_t), 1,
283 if (spa_feature_is_active(dp
->dp_spa
, SPA_FEATURE_EMPTY_BPOBJ
)) {
284 err
= zap_lookup(dp
->dp_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
285 DMU_POOL_EMPTY_BPOBJ
, sizeof (uint64_t), 1,
286 &dp
->dp_empty_bpobj
);
291 err
= zap_lookup(dp
->dp_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
292 DMU_POOL_TMP_USERREFS
, sizeof (uint64_t), 1,
293 &dp
->dp_tmp_userrefs_obj
);
299 err
= dsl_scan_init(dp
, dp
->dp_tx
.tx_open_txg
);
302 rrw_exit(&dp
->dp_config_rwlock
, FTAG
);
307 dsl_pool_close(dsl_pool_t
*dp
)
310 * Drop our references from dsl_pool_open().
312 * Since we held the origin_snap from "syncing" context (which
313 * includes pool-opening context), it actually only got a "ref"
314 * and not a hold, so just drop that here.
316 if (dp
->dp_origin_snap
)
317 dsl_dataset_rele(dp
->dp_origin_snap
, dp
);
319 dsl_dir_rele(dp
->dp_mos_dir
, dp
);
321 dsl_dir_rele(dp
->dp_free_dir
, dp
);
323 dsl_dir_rele(dp
->dp_leak_dir
, dp
);
325 dsl_dir_rele(dp
->dp_root_dir
, dp
);
327 bpobj_close(&dp
->dp_free_bpobj
);
329 /* undo the dmu_objset_open_impl(mos) from dsl_pool_open() */
330 if (dp
->dp_meta_objset
)
331 dmu_objset_evict(dp
->dp_meta_objset
);
333 txg_list_destroy(&dp
->dp_dirty_datasets
);
334 txg_list_destroy(&dp
->dp_dirty_zilogs
);
335 txg_list_destroy(&dp
->dp_sync_tasks
);
336 txg_list_destroy(&dp
->dp_dirty_dirs
);
338 taskq_destroy(dp
->dp_sync_taskq
);
341 * We can't set retry to TRUE since we're explicitly specifying
342 * a spa to flush. This is good enough; any missed buffers for
343 * this spa won't cause trouble, and they'll eventually fall
344 * out of the ARC just like any other unused buffer.
346 arc_flush(dp
->dp_spa
, FALSE
);
350 dmu_buf_user_evict_wait();
352 rrw_destroy(&dp
->dp_config_rwlock
);
353 mutex_destroy(&dp
->dp_lock
);
354 taskq_destroy(dp
->dp_iput_taskq
);
356 vmem_free(dp
->dp_blkstats
, sizeof (zfs_all_blkstats_t
));
357 kmem_free(dp
, sizeof (dsl_pool_t
));
361 dsl_pool_create(spa_t
*spa
, nvlist_t
*zplprops
, uint64_t txg
)
364 dsl_pool_t
*dp
= dsl_pool_open_impl(spa
, txg
);
365 dmu_tx_t
*tx
= dmu_tx_create_assigned(dp
, txg
);
370 rrw_enter(&dp
->dp_config_rwlock
, RW_WRITER
, FTAG
);
372 /* create and open the MOS (meta-objset) */
373 dp
->dp_meta_objset
= dmu_objset_create_impl(spa
,
374 NULL
, &dp
->dp_meta_rootbp
, DMU_OST_META
, tx
);
376 /* create the pool directory */
377 err
= zap_create_claim(dp
->dp_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
378 DMU_OT_OBJECT_DIRECTORY
, DMU_OT_NONE
, 0, tx
);
381 /* Initialize scan structures */
382 VERIFY0(dsl_scan_init(dp
, txg
));
384 /* create and open the root dir */
385 dp
->dp_root_dir_obj
= dsl_dir_create_sync(dp
, NULL
, NULL
, tx
);
386 VERIFY0(dsl_dir_hold_obj(dp
, dp
->dp_root_dir_obj
,
387 NULL
, dp
, &dp
->dp_root_dir
));
389 /* create and open the meta-objset dir */
390 (void) dsl_dir_create_sync(dp
, dp
->dp_root_dir
, MOS_DIR_NAME
, tx
);
391 VERIFY0(dsl_pool_open_special_dir(dp
,
392 MOS_DIR_NAME
, &dp
->dp_mos_dir
));
394 if (spa_version(spa
) >= SPA_VERSION_DEADLISTS
) {
395 /* create and open the free dir */
396 (void) dsl_dir_create_sync(dp
, dp
->dp_root_dir
,
398 VERIFY0(dsl_pool_open_special_dir(dp
,
399 FREE_DIR_NAME
, &dp
->dp_free_dir
));
401 /* create and open the free_bplist */
402 obj
= bpobj_alloc(dp
->dp_meta_objset
, SPA_OLD_MAXBLOCKSIZE
, tx
);
403 VERIFY(zap_add(dp
->dp_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
404 DMU_POOL_FREE_BPOBJ
, sizeof (uint64_t), 1, &obj
, tx
) == 0);
405 VERIFY0(bpobj_open(&dp
->dp_free_bpobj
,
406 dp
->dp_meta_objset
, obj
));
409 if (spa_version(spa
) >= SPA_VERSION_DSL_SCRUB
)
410 dsl_pool_create_origin(dp
, tx
);
412 /* create the root dataset */
413 obj
= dsl_dataset_create_sync_dd(dp
->dp_root_dir
, NULL
, 0, tx
);
415 /* create the root objset */
416 VERIFY0(dsl_dataset_hold_obj(dp
, obj
, FTAG
, &ds
));
417 rrw_enter(&ds
->ds_bp_rwlock
, RW_READER
, FTAG
);
418 VERIFY(NULL
!= (os
= dmu_objset_create_impl(dp
->dp_spa
, ds
,
419 dsl_dataset_get_blkptr(ds
), DMU_OST_ZFS
, tx
)));
420 rrw_exit(&ds
->ds_bp_rwlock
, FTAG
);
422 zfs_create_fs(os
, kcred
, zplprops
, tx
);
424 dsl_dataset_rele(ds
, FTAG
);
428 rrw_exit(&dp
->dp_config_rwlock
, FTAG
);
434 * Account for the meta-objset space in its placeholder dsl_dir.
437 dsl_pool_mos_diduse_space(dsl_pool_t
*dp
,
438 int64_t used
, int64_t comp
, int64_t uncomp
)
440 ASSERT3U(comp
, ==, uncomp
); /* it's all metadata */
441 mutex_enter(&dp
->dp_lock
);
442 dp
->dp_mos_used_delta
+= used
;
443 dp
->dp_mos_compressed_delta
+= comp
;
444 dp
->dp_mos_uncompressed_delta
+= uncomp
;
445 mutex_exit(&dp
->dp_lock
);
449 dsl_pool_sync_mos(dsl_pool_t
*dp
, dmu_tx_t
*tx
)
451 zio_t
*zio
= zio_root(dp
->dp_spa
, NULL
, NULL
, ZIO_FLAG_MUSTSUCCEED
);
452 dmu_objset_sync(dp
->dp_meta_objset
, zio
, tx
);
453 VERIFY0(zio_wait(zio
));
454 dprintf_bp(&dp
->dp_meta_rootbp
, "meta objset rootbp is %s", "");
455 spa_set_rootblkptr(dp
->dp_spa
, &dp
->dp_meta_rootbp
);
459 dsl_pool_dirty_delta(dsl_pool_t
*dp
, int64_t delta
)
461 ASSERT(MUTEX_HELD(&dp
->dp_lock
));
464 ASSERT3U(-delta
, <=, dp
->dp_dirty_total
);
466 dp
->dp_dirty_total
+= delta
;
469 * Note: we signal even when increasing dp_dirty_total.
470 * This ensures forward progress -- each thread wakes the next waiter.
472 if (dp
->dp_dirty_total
<= zfs_dirty_data_max
)
473 cv_signal(&dp
->dp_spaceavail_cv
);
477 dsl_pool_sync(dsl_pool_t
*dp
, uint64_t txg
)
483 objset_t
*mos
= dp
->dp_meta_objset
;
484 list_t synced_datasets
;
486 list_create(&synced_datasets
, sizeof (dsl_dataset_t
),
487 offsetof(dsl_dataset_t
, ds_synced_link
));
489 tx
= dmu_tx_create_assigned(dp
, txg
);
492 * Write out all dirty blocks of dirty datasets.
494 zio
= zio_root(dp
->dp_spa
, NULL
, NULL
, ZIO_FLAG_MUSTSUCCEED
);
495 while ((ds
= txg_list_remove(&dp
->dp_dirty_datasets
, txg
)) != NULL
) {
497 * We must not sync any non-MOS datasets twice, because
498 * we may have taken a snapshot of them. However, we
499 * may sync newly-created datasets on pass 2.
501 ASSERT(!list_link_active(&ds
->ds_synced_link
));
502 list_insert_tail(&synced_datasets
, ds
);
503 dsl_dataset_sync(ds
, zio
, tx
);
505 VERIFY0(zio_wait(zio
));
508 * We have written all of the accounted dirty data, so our
509 * dp_space_towrite should now be zero. However, some seldom-used
510 * code paths do not adhere to this (e.g. dbuf_undirty(), also
511 * rounding error in dbuf_write_physdone).
512 * Shore up the accounting of any dirtied space now.
514 dsl_pool_undirty_space(dp
, dp
->dp_dirty_pertxg
[txg
& TXG_MASK
], txg
);
517 * Update the long range free counter after
518 * we're done syncing user data
520 mutex_enter(&dp
->dp_lock
);
521 ASSERT(spa_sync_pass(dp
->dp_spa
) == 1 ||
522 dp
->dp_long_free_dirty_pertxg
[txg
& TXG_MASK
] == 0);
523 dp
->dp_long_free_dirty_pertxg
[txg
& TXG_MASK
] = 0;
524 mutex_exit(&dp
->dp_lock
);
527 * After the data blocks have been written (ensured by the zio_wait()
528 * above), update the user/group space accounting. This happens
529 * in tasks dispatched to dp_sync_taskq, so wait for them before
532 for (ds
= list_head(&synced_datasets
); ds
!= NULL
;
533 ds
= list_next(&synced_datasets
, ds
)) {
534 dmu_objset_do_userquota_updates(ds
->ds_objset
, tx
);
536 taskq_wait(dp
->dp_sync_taskq
);
539 * Sync the datasets again to push out the changes due to
540 * userspace updates. This must be done before we process the
541 * sync tasks, so that any snapshots will have the correct
542 * user accounting information (and we won't get confused
543 * about which blocks are part of the snapshot).
545 zio
= zio_root(dp
->dp_spa
, NULL
, NULL
, ZIO_FLAG_MUSTSUCCEED
);
546 while ((ds
= txg_list_remove(&dp
->dp_dirty_datasets
, txg
)) != NULL
) {
547 ASSERT(list_link_active(&ds
->ds_synced_link
));
548 dmu_buf_rele(ds
->ds_dbuf
, ds
);
549 dsl_dataset_sync(ds
, zio
, tx
);
551 VERIFY0(zio_wait(zio
));
554 * Now that the datasets have been completely synced, we can
555 * clean up our in-memory structures accumulated while syncing:
557 * - move dead blocks from the pending deadlist to the on-disk deadlist
558 * - release hold from dsl_dataset_dirty()
560 while ((ds
= list_remove_head(&synced_datasets
)) != NULL
) {
561 dsl_dataset_sync_done(ds
, tx
);
564 while ((dd
= txg_list_remove(&dp
->dp_dirty_dirs
, txg
)) != NULL
) {
565 dsl_dir_sync(dd
, tx
);
569 * The MOS's space is accounted for in the pool/$MOS
570 * (dp_mos_dir). We can't modify the mos while we're syncing
571 * it, so we remember the deltas and apply them here.
573 if (dp
->dp_mos_used_delta
!= 0 || dp
->dp_mos_compressed_delta
!= 0 ||
574 dp
->dp_mos_uncompressed_delta
!= 0) {
575 dsl_dir_diduse_space(dp
->dp_mos_dir
, DD_USED_HEAD
,
576 dp
->dp_mos_used_delta
,
577 dp
->dp_mos_compressed_delta
,
578 dp
->dp_mos_uncompressed_delta
, tx
);
579 dp
->dp_mos_used_delta
= 0;
580 dp
->dp_mos_compressed_delta
= 0;
581 dp
->dp_mos_uncompressed_delta
= 0;
584 if (!multilist_is_empty(mos
->os_dirty_dnodes
[txg
& TXG_MASK
])) {
585 dsl_pool_sync_mos(dp
, tx
);
589 * If we modify a dataset in the same txg that we want to destroy it,
590 * its dsl_dir's dd_dbuf will be dirty, and thus have a hold on it.
591 * dsl_dir_destroy_check() will fail if there are unexpected holds.
592 * Therefore, we want to sync the MOS (thus syncing the dd_dbuf
593 * and clearing the hold on it) before we process the sync_tasks.
594 * The MOS data dirtied by the sync_tasks will be synced on the next
597 if (!txg_list_empty(&dp
->dp_sync_tasks
, txg
)) {
598 dsl_sync_task_t
*dst
;
600 * No more sync tasks should have been added while we
603 ASSERT3U(spa_sync_pass(dp
->dp_spa
), ==, 1);
604 while ((dst
= txg_list_remove(&dp
->dp_sync_tasks
, txg
)) != NULL
)
605 dsl_sync_task_sync(dst
, tx
);
610 DTRACE_PROBE2(dsl_pool_sync__done
, dsl_pool_t
*dp
, dp
, uint64_t, txg
);
614 dsl_pool_sync_done(dsl_pool_t
*dp
, uint64_t txg
)
618 while ((zilog
= txg_list_remove(&dp
->dp_dirty_zilogs
, txg
))) {
619 dsl_dataset_t
*ds
= dmu_objset_ds(zilog
->zl_os
);
620 zil_clean(zilog
, txg
);
621 ASSERT(!dmu_objset_is_dirty(zilog
->zl_os
, txg
));
622 dmu_buf_rele(ds
->ds_dbuf
, zilog
);
624 ASSERT(!dmu_objset_is_dirty(dp
->dp_meta_objset
, txg
));
628 * TRUE if the current thread is the tx_sync_thread or if we
629 * are being called from SPA context during pool initialization.
632 dsl_pool_sync_context(dsl_pool_t
*dp
)
634 return (curthread
== dp
->dp_tx
.tx_sync_thread
||
635 spa_is_initializing(dp
->dp_spa
) ||
636 taskq_member(dp
->dp_sync_taskq
, curthread
));
640 dsl_pool_adjustedsize(dsl_pool_t
*dp
, boolean_t netfree
)
642 uint64_t space
, resv
;
645 * If we're trying to assess whether it's OK to do a free,
646 * cut the reservation in half to allow forward progress
647 * (e.g. make it possible to rm(1) files from a full pool).
649 space
= spa_get_dspace(dp
->dp_spa
);
650 resv
= spa_get_slop_space(dp
->dp_spa
);
654 return (space
- resv
);
658 dsl_pool_need_dirty_delay(dsl_pool_t
*dp
)
660 uint64_t delay_min_bytes
=
661 zfs_dirty_data_max
* zfs_delay_min_dirty_percent
/ 100;
664 mutex_enter(&dp
->dp_lock
);
665 if (dp
->dp_dirty_total
> zfs_dirty_data_sync
)
667 rv
= (dp
->dp_dirty_total
> delay_min_bytes
);
668 mutex_exit(&dp
->dp_lock
);
673 dsl_pool_dirty_space(dsl_pool_t
*dp
, int64_t space
, dmu_tx_t
*tx
)
676 mutex_enter(&dp
->dp_lock
);
677 dp
->dp_dirty_pertxg
[tx
->tx_txg
& TXG_MASK
] += space
;
678 dsl_pool_dirty_delta(dp
, space
);
679 mutex_exit(&dp
->dp_lock
);
684 dsl_pool_undirty_space(dsl_pool_t
*dp
, int64_t space
, uint64_t txg
)
686 ASSERT3S(space
, >=, 0);
690 mutex_enter(&dp
->dp_lock
);
691 if (dp
->dp_dirty_pertxg
[txg
& TXG_MASK
] < space
) {
692 /* XXX writing something we didn't dirty? */
693 space
= dp
->dp_dirty_pertxg
[txg
& TXG_MASK
];
695 ASSERT3U(dp
->dp_dirty_pertxg
[txg
& TXG_MASK
], >=, space
);
696 dp
->dp_dirty_pertxg
[txg
& TXG_MASK
] -= space
;
697 ASSERT3U(dp
->dp_dirty_total
, >=, space
);
698 dsl_pool_dirty_delta(dp
, -space
);
699 mutex_exit(&dp
->dp_lock
);
704 upgrade_clones_cb(dsl_pool_t
*dp
, dsl_dataset_t
*hds
, void *arg
)
707 dsl_dataset_t
*ds
, *prev
= NULL
;
710 err
= dsl_dataset_hold_obj(dp
, hds
->ds_object
, FTAG
, &ds
);
714 while (dsl_dataset_phys(ds
)->ds_prev_snap_obj
!= 0) {
715 err
= dsl_dataset_hold_obj(dp
,
716 dsl_dataset_phys(ds
)->ds_prev_snap_obj
, FTAG
, &prev
);
718 dsl_dataset_rele(ds
, FTAG
);
722 if (dsl_dataset_phys(prev
)->ds_next_snap_obj
!= ds
->ds_object
)
724 dsl_dataset_rele(ds
, FTAG
);
730 prev
= dp
->dp_origin_snap
;
733 * The $ORIGIN can't have any data, or the accounting
736 rrw_enter(&ds
->ds_bp_rwlock
, RW_READER
, FTAG
);
737 ASSERT0(dsl_dataset_phys(prev
)->ds_bp
.blk_birth
);
738 rrw_exit(&ds
->ds_bp_rwlock
, FTAG
);
740 /* The origin doesn't get attached to itself */
741 if (ds
->ds_object
== prev
->ds_object
) {
742 dsl_dataset_rele(ds
, FTAG
);
746 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
747 dsl_dataset_phys(ds
)->ds_prev_snap_obj
= prev
->ds_object
;
748 dsl_dataset_phys(ds
)->ds_prev_snap_txg
=
749 dsl_dataset_phys(prev
)->ds_creation_txg
;
751 dmu_buf_will_dirty(ds
->ds_dir
->dd_dbuf
, tx
);
752 dsl_dir_phys(ds
->ds_dir
)->dd_origin_obj
= prev
->ds_object
;
754 dmu_buf_will_dirty(prev
->ds_dbuf
, tx
);
755 dsl_dataset_phys(prev
)->ds_num_children
++;
757 if (dsl_dataset_phys(ds
)->ds_next_snap_obj
== 0) {
758 ASSERT(ds
->ds_prev
== NULL
);
759 VERIFY0(dsl_dataset_hold_obj(dp
,
760 dsl_dataset_phys(ds
)->ds_prev_snap_obj
,
765 ASSERT3U(dsl_dir_phys(ds
->ds_dir
)->dd_origin_obj
, ==, prev
->ds_object
);
766 ASSERT3U(dsl_dataset_phys(ds
)->ds_prev_snap_obj
, ==, prev
->ds_object
);
768 if (dsl_dataset_phys(prev
)->ds_next_clones_obj
== 0) {
769 dmu_buf_will_dirty(prev
->ds_dbuf
, tx
);
770 dsl_dataset_phys(prev
)->ds_next_clones_obj
=
771 zap_create(dp
->dp_meta_objset
,
772 DMU_OT_NEXT_CLONES
, DMU_OT_NONE
, 0, tx
);
774 VERIFY0(zap_add_int(dp
->dp_meta_objset
,
775 dsl_dataset_phys(prev
)->ds_next_clones_obj
, ds
->ds_object
, tx
));
777 dsl_dataset_rele(ds
, FTAG
);
778 if (prev
!= dp
->dp_origin_snap
)
779 dsl_dataset_rele(prev
, FTAG
);
784 dsl_pool_upgrade_clones(dsl_pool_t
*dp
, dmu_tx_t
*tx
)
786 ASSERT(dmu_tx_is_syncing(tx
));
787 ASSERT(dp
->dp_origin_snap
!= NULL
);
789 VERIFY0(dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
, upgrade_clones_cb
,
790 tx
, DS_FIND_CHILDREN
| DS_FIND_SERIALIZE
));
795 upgrade_dir_clones_cb(dsl_pool_t
*dp
, dsl_dataset_t
*ds
, void *arg
)
798 objset_t
*mos
= dp
->dp_meta_objset
;
800 if (dsl_dir_phys(ds
->ds_dir
)->dd_origin_obj
!= 0) {
801 dsl_dataset_t
*origin
;
803 VERIFY0(dsl_dataset_hold_obj(dp
,
804 dsl_dir_phys(ds
->ds_dir
)->dd_origin_obj
, FTAG
, &origin
));
806 if (dsl_dir_phys(origin
->ds_dir
)->dd_clones
== 0) {
807 dmu_buf_will_dirty(origin
->ds_dir
->dd_dbuf
, tx
);
808 dsl_dir_phys(origin
->ds_dir
)->dd_clones
=
809 zap_create(mos
, DMU_OT_DSL_CLONES
, DMU_OT_NONE
,
813 VERIFY0(zap_add_int(dp
->dp_meta_objset
,
814 dsl_dir_phys(origin
->ds_dir
)->dd_clones
,
817 dsl_dataset_rele(origin
, FTAG
);
823 dsl_pool_upgrade_dir_clones(dsl_pool_t
*dp
, dmu_tx_t
*tx
)
827 ASSERT(dmu_tx_is_syncing(tx
));
829 (void) dsl_dir_create_sync(dp
, dp
->dp_root_dir
, FREE_DIR_NAME
, tx
);
830 VERIFY0(dsl_pool_open_special_dir(dp
,
831 FREE_DIR_NAME
, &dp
->dp_free_dir
));
834 * We can't use bpobj_alloc(), because spa_version() still
835 * returns the old version, and we need a new-version bpobj with
836 * subobj support. So call dmu_object_alloc() directly.
838 obj
= dmu_object_alloc(dp
->dp_meta_objset
, DMU_OT_BPOBJ
,
839 SPA_OLD_MAXBLOCKSIZE
, DMU_OT_BPOBJ_HDR
, sizeof (bpobj_phys_t
), tx
);
840 VERIFY0(zap_add(dp
->dp_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
841 DMU_POOL_FREE_BPOBJ
, sizeof (uint64_t), 1, &obj
, tx
));
842 VERIFY0(bpobj_open(&dp
->dp_free_bpobj
, dp
->dp_meta_objset
, obj
));
844 VERIFY0(dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
845 upgrade_dir_clones_cb
, tx
, DS_FIND_CHILDREN
| DS_FIND_SERIALIZE
));
849 dsl_pool_create_origin(dsl_pool_t
*dp
, dmu_tx_t
*tx
)
854 ASSERT(dmu_tx_is_syncing(tx
));
855 ASSERT(dp
->dp_origin_snap
== NULL
);
856 ASSERT(rrw_held(&dp
->dp_config_rwlock
, RW_WRITER
));
858 /* create the origin dir, ds, & snap-ds */
859 dsobj
= dsl_dataset_create_sync(dp
->dp_root_dir
, ORIGIN_DIR_NAME
,
861 VERIFY0(dsl_dataset_hold_obj(dp
, dsobj
, FTAG
, &ds
));
862 dsl_dataset_snapshot_sync_impl(ds
, ORIGIN_DIR_NAME
, tx
);
863 VERIFY0(dsl_dataset_hold_obj(dp
, dsl_dataset_phys(ds
)->ds_prev_snap_obj
,
864 dp
, &dp
->dp_origin_snap
));
865 dsl_dataset_rele(ds
, FTAG
);
869 dsl_pool_iput_taskq(dsl_pool_t
*dp
)
871 return (dp
->dp_iput_taskq
);
875 * Walk through the pool-wide zap object of temporary snapshot user holds
879 dsl_pool_clean_tmp_userrefs(dsl_pool_t
*dp
)
883 objset_t
*mos
= dp
->dp_meta_objset
;
884 uint64_t zapobj
= dp
->dp_tmp_userrefs_obj
;
889 ASSERT(spa_version(dp
->dp_spa
) >= SPA_VERSION_USERREFS
);
891 holds
= fnvlist_alloc();
893 for (zap_cursor_init(&zc
, mos
, zapobj
);
894 zap_cursor_retrieve(&zc
, &za
) == 0;
895 zap_cursor_advance(&zc
)) {
899 htag
= strchr(za
.za_name
, '-');
902 if (nvlist_lookup_nvlist(holds
, za
.za_name
, &tags
) != 0) {
903 tags
= fnvlist_alloc();
904 fnvlist_add_boolean(tags
, htag
);
905 fnvlist_add_nvlist(holds
, za
.za_name
, tags
);
908 fnvlist_add_boolean(tags
, htag
);
911 dsl_dataset_user_release_tmp(dp
, holds
);
913 zap_cursor_fini(&zc
);
917 * Create the pool-wide zap object for storing temporary snapshot holds.
920 dsl_pool_user_hold_create_obj(dsl_pool_t
*dp
, dmu_tx_t
*tx
)
922 objset_t
*mos
= dp
->dp_meta_objset
;
924 ASSERT(dp
->dp_tmp_userrefs_obj
== 0);
925 ASSERT(dmu_tx_is_syncing(tx
));
927 dp
->dp_tmp_userrefs_obj
= zap_create_link(mos
, DMU_OT_USERREFS
,
928 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_TMP_USERREFS
, tx
);
932 dsl_pool_user_hold_rele_impl(dsl_pool_t
*dp
, uint64_t dsobj
,
933 const char *tag
, uint64_t now
, dmu_tx_t
*tx
, boolean_t holding
)
935 objset_t
*mos
= dp
->dp_meta_objset
;
936 uint64_t zapobj
= dp
->dp_tmp_userrefs_obj
;
940 ASSERT(spa_version(dp
->dp_spa
) >= SPA_VERSION_USERREFS
);
941 ASSERT(dmu_tx_is_syncing(tx
));
944 * If the pool was created prior to SPA_VERSION_USERREFS, the
945 * zap object for temporary holds might not exist yet.
949 dsl_pool_user_hold_create_obj(dp
, tx
);
950 zapobj
= dp
->dp_tmp_userrefs_obj
;
952 return (SET_ERROR(ENOENT
));
956 name
= kmem_asprintf("%llx-%s", (u_longlong_t
)dsobj
, tag
);
958 error
= zap_add(mos
, zapobj
, name
, 8, 1, &now
, tx
);
960 error
= zap_remove(mos
, zapobj
, name
, tx
);
967 * Add a temporary hold for the given dataset object and tag.
970 dsl_pool_user_hold(dsl_pool_t
*dp
, uint64_t dsobj
, const char *tag
,
971 uint64_t now
, dmu_tx_t
*tx
)
973 return (dsl_pool_user_hold_rele_impl(dp
, dsobj
, tag
, now
, tx
, B_TRUE
));
977 * Release a temporary hold for the given dataset object and tag.
980 dsl_pool_user_release(dsl_pool_t
*dp
, uint64_t dsobj
, const char *tag
,
983 return (dsl_pool_user_hold_rele_impl(dp
, dsobj
, tag
, 0,
988 * DSL Pool Configuration Lock
990 * The dp_config_rwlock protects against changes to DSL state (e.g. dataset
991 * creation / destruction / rename / property setting). It must be held for
992 * read to hold a dataset or dsl_dir. I.e. you must call
993 * dsl_pool_config_enter() or dsl_pool_hold() before calling
994 * dsl_{dataset,dir}_hold{_obj}. In most circumstances, the dp_config_rwlock
995 * must be held continuously until all datasets and dsl_dirs are released.
997 * The only exception to this rule is that if a "long hold" is placed on
998 * a dataset, then the dp_config_rwlock may be dropped while the dataset
999 * is still held. The long hold will prevent the dataset from being
1000 * destroyed -- the destroy will fail with EBUSY. A long hold can be
1001 * obtained by calling dsl_dataset_long_hold(), or by "owning" a dataset
1002 * (by calling dsl_{dataset,objset}_{try}own{_obj}).
1004 * Legitimate long-holders (including owners) should be long-running, cancelable
1005 * tasks that should cause "zfs destroy" to fail. This includes DMU
1006 * consumers (i.e. a ZPL filesystem being mounted or ZVOL being open),
1007 * "zfs send", and "zfs diff". There are several other long-holders whose
1008 * uses are suboptimal (e.g. "zfs promote", and zil_suspend()).
1010 * The usual formula for long-holding would be:
1012 * dsl_dataset_hold()
1013 * ... perform checks ...
1014 * dsl_dataset_long_hold()
1016 * ... perform long-running task ...
1017 * dsl_dataset_long_rele()
1018 * dsl_dataset_rele()
1020 * Note that when the long hold is released, the dataset is still held but
1021 * the pool is not held. The dataset may change arbitrarily during this time
1022 * (e.g. it could be destroyed). Therefore you shouldn't do anything to the
1023 * dataset except release it.
1025 * User-initiated operations (e.g. ioctls, zfs_ioc_*()) are either read-only
1026 * or modifying operations.
1028 * Modifying operations should generally use dsl_sync_task(). The synctask
1029 * infrastructure enforces proper locking strategy with respect to the
1030 * dp_config_rwlock. See the comment above dsl_sync_task() for details.
1032 * Read-only operations will manually hold the pool, then the dataset, obtain
1033 * information from the dataset, then release the pool and dataset.
1034 * dmu_objset_{hold,rele}() are convenience routines that also do the pool
1039 dsl_pool_hold(const char *name
, void *tag
, dsl_pool_t
**dp
)
1044 error
= spa_open(name
, &spa
, tag
);
1046 *dp
= spa_get_dsl(spa
);
1047 dsl_pool_config_enter(*dp
, tag
);
1053 dsl_pool_rele(dsl_pool_t
*dp
, void *tag
)
1055 dsl_pool_config_exit(dp
, tag
);
1056 spa_close(dp
->dp_spa
, tag
);
1060 dsl_pool_config_enter(dsl_pool_t
*dp
, void *tag
)
1063 * We use a "reentrant" reader-writer lock, but not reentrantly.
1065 * The rrwlock can (with the track_all flag) track all reading threads,
1066 * which is very useful for debugging which code path failed to release
1067 * the lock, and for verifying that the *current* thread does hold
1070 * (Unlike a rwlock, which knows that N threads hold it for
1071 * read, but not *which* threads, so rw_held(RW_READER) returns TRUE
1072 * if any thread holds it for read, even if this thread doesn't).
1074 ASSERT(!rrw_held(&dp
->dp_config_rwlock
, RW_READER
));
1075 rrw_enter(&dp
->dp_config_rwlock
, RW_READER
, tag
);
1079 dsl_pool_config_enter_prio(dsl_pool_t
*dp
, void *tag
)
1081 ASSERT(!rrw_held(&dp
->dp_config_rwlock
, RW_READER
));
1082 rrw_enter_read_prio(&dp
->dp_config_rwlock
, tag
);
1086 dsl_pool_config_exit(dsl_pool_t
*dp
, void *tag
)
1088 rrw_exit(&dp
->dp_config_rwlock
, tag
);
1092 dsl_pool_config_held(dsl_pool_t
*dp
)
1094 return (RRW_LOCK_HELD(&dp
->dp_config_rwlock
));
1098 dsl_pool_config_held_writer(dsl_pool_t
*dp
)
1100 return (RRW_WRITE_HELD(&dp
->dp_config_rwlock
));
1103 #if defined(_KERNEL) && defined(HAVE_SPL)
1104 EXPORT_SYMBOL(dsl_pool_config_enter
);
1105 EXPORT_SYMBOL(dsl_pool_config_exit
);
1108 /* zfs_dirty_data_max_percent only applied at module load in arc_init(). */
1109 module_param(zfs_dirty_data_max_percent
, int, 0444);
1110 MODULE_PARM_DESC(zfs_dirty_data_max_percent
, "percent of ram can be dirty");
1112 /* zfs_dirty_data_max_max_percent only applied at module load in arc_init(). */
1113 module_param(zfs_dirty_data_max_max_percent
, int, 0444);
1114 MODULE_PARM_DESC(zfs_dirty_data_max_max_percent
,
1115 "zfs_dirty_data_max upper bound as % of RAM");
1117 module_param(zfs_delay_min_dirty_percent
, int, 0644);
1118 MODULE_PARM_DESC(zfs_delay_min_dirty_percent
, "transaction delay threshold");
1120 module_param(zfs_dirty_data_max
, ulong
, 0644);
1121 MODULE_PARM_DESC(zfs_dirty_data_max
, "determines the dirty space limit");
1123 /* zfs_dirty_data_max_max only applied at module load in arc_init(). */
1124 module_param(zfs_dirty_data_max_max
, ulong
, 0444);
1125 MODULE_PARM_DESC(zfs_dirty_data_max_max
,
1126 "zfs_dirty_data_max upper bound in bytes");
1128 module_param(zfs_dirty_data_sync
, ulong
, 0644);
1129 MODULE_PARM_DESC(zfs_dirty_data_sync
, "sync txg when this much dirty data");
1131 module_param(zfs_delay_scale
, ulong
, 0644);
1132 MODULE_PARM_DESC(zfs_delay_scale
, "how quickly delay approaches infinity");
1134 module_param(zfs_sync_taskq_batch_pct
, int, 0644);
1135 MODULE_PARM_DESC(zfs_sync_taskq_batch_pct
,
1136 "max percent of CPUs that are used to sync dirty data");