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 * Portions Copyright 2011 Martin Matuska <mm@FreeBSD.org>
24 * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
27 #include <sys/zfs_context.h>
28 #include <sys/txg_impl.h>
29 #include <sys/dmu_impl.h>
30 #include <sys/dmu_tx.h>
31 #include <sys/dsl_pool.h>
32 #include <sys/dsl_scan.h>
34 #include <sys/callb.h>
37 * ZFS Transaction Groups
38 * ----------------------
40 * ZFS transaction groups are, as the name implies, groups of transactions
41 * that act on persistent state. ZFS asserts consistency at the granularity of
42 * these transaction groups. Each successive transaction group (txg) is
43 * assigned a 64-bit consecutive identifier. There are three active
44 * transaction group states: open, quiescing, or syncing. At any given time,
45 * there may be an active txg associated with each state; each active txg may
46 * either be processing, or blocked waiting to enter the next state. There may
47 * be up to three active txgs, and there is always a txg in the open state
48 * (though it may be blocked waiting to enter the quiescing state). In broad
49 * strokes, transactions -- operations that change in-memory structures -- are
50 * accepted into the txg in the open state, and are completed while the txg is
51 * in the open or quiescing states. The accumulated changes are written to
52 * disk in the syncing state.
56 * When a new txg becomes active, it first enters the open state. New
57 * transactions -- updates to in-memory structures -- are assigned to the
58 * currently open txg. There is always a txg in the open state so that ZFS can
59 * accept new changes (though the txg may refuse new changes if it has hit
60 * some limit). ZFS advances the open txg to the next state for a variety of
61 * reasons such as it hitting a time or size threshold, or the execution of an
62 * administrative action that must be completed in the syncing state.
66 * After a txg exits the open state, it enters the quiescing state. The
67 * quiescing state is intended to provide a buffer between accepting new
68 * transactions in the open state and writing them out to stable storage in
69 * the syncing state. While quiescing, transactions can continue their
70 * operation without delaying either of the other states. Typically, a txg is
71 * in the quiescing state very briefly since the operations are bounded by
72 * software latencies rather than, say, slower I/O latencies. After all
73 * transactions complete, the txg is ready to enter the next state.
77 * In the syncing state, the in-memory state built up during the open and (to
78 * a lesser degree) the quiescing states is written to stable storage. The
79 * process of writing out modified data can, in turn modify more data. For
80 * example when we write new blocks, we need to allocate space for them; those
81 * allocations modify metadata (space maps)... which themselves must be
82 * written to stable storage. During the sync state, ZFS iterates, writing out
83 * data until it converges and all in-memory changes have been written out.
84 * The first such pass is the largest as it encompasses all the modified user
85 * data (as opposed to filesystem metadata). Subsequent passes typically have
86 * far less data to write as they consist exclusively of filesystem metadata.
88 * To ensure convergence, after a certain number of passes ZFS begins
89 * overwriting locations on stable storage that had been allocated earlier in
90 * the syncing state (and subsequently freed). ZFS usually allocates new
91 * blocks to optimize for large, continuous, writes. For the syncing state to
92 * converge however it must complete a pass where no new blocks are allocated
93 * since each allocation requires a modification of persistent metadata.
94 * Further, to hasten convergence, after a prescribed number of passes, ZFS
95 * also defers frees, and stops compressing.
97 * In addition to writing out user data, we must also execute synctasks during
98 * the syncing context. A synctask is the mechanism by which some
99 * administrative activities work such as creating and destroying snapshots or
100 * datasets. Note that when a synctask is initiated it enters the open txg,
101 * and ZFS then pushes that txg as quickly as possible to completion of the
102 * syncing state in order to reduce the latency of the administrative
103 * activity. To complete the syncing state, ZFS writes out a new uberblock,
104 * the root of the tree of blocks that comprise all state stored on the ZFS
105 * pool. Finally, if there is a quiesced txg waiting, we signal that it can
106 * now transition to the syncing state.
109 static void txg_sync_thread(void *arg);
110 static void txg_quiesce_thread(void *arg);
112 int zfs_txg_timeout = 5; /* max seconds worth of delta per txg */
114 SYSCTL_DECL(_vfs_zfs);
115 SYSCTL_NODE(_vfs_zfs, OID_AUTO, txg, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
117 SYSCTL_INT(_vfs_zfs_txg, OID_AUTO, timeout, CTLFLAG_RWTUN, &zfs_txg_timeout, 0,
118 "Maximum seconds worth of delta per txg");
121 * Prepare the txg subsystem.
124 txg_init(dsl_pool_t *dp, uint64_t txg)
126 tx_state_t *tx = &dp->dp_tx;
128 bzero(tx, sizeof (tx_state_t));
130 tx->tx_cpu = kmem_zalloc(max_ncpus * sizeof (tx_cpu_t), KM_SLEEP);
132 for (c = 0; c < max_ncpus; c++) {
135 mutex_init(&tx->tx_cpu[c].tc_lock, NULL, MUTEX_DEFAULT, NULL);
136 mutex_init(&tx->tx_cpu[c].tc_open_lock, NULL, MUTEX_DEFAULT,
138 for (i = 0; i < TXG_SIZE; i++) {
139 cv_init(&tx->tx_cpu[c].tc_cv[i], NULL, CV_DEFAULT,
141 list_create(&tx->tx_cpu[c].tc_callbacks[i],
142 sizeof (dmu_tx_callback_t),
143 offsetof(dmu_tx_callback_t, dcb_node));
147 mutex_init(&tx->tx_sync_lock, NULL, MUTEX_DEFAULT, NULL);
149 cv_init(&tx->tx_sync_more_cv, NULL, CV_DEFAULT, NULL);
150 cv_init(&tx->tx_sync_done_cv, NULL, CV_DEFAULT, NULL);
151 cv_init(&tx->tx_quiesce_more_cv, NULL, CV_DEFAULT, NULL);
152 cv_init(&tx->tx_quiesce_done_cv, NULL, CV_DEFAULT, NULL);
153 cv_init(&tx->tx_exit_cv, NULL, CV_DEFAULT, NULL);
155 tx->tx_open_txg = txg;
159 * Close down the txg subsystem.
162 txg_fini(dsl_pool_t *dp)
164 tx_state_t *tx = &dp->dp_tx;
167 ASSERT0(tx->tx_threads);
169 mutex_destroy(&tx->tx_sync_lock);
171 cv_destroy(&tx->tx_sync_more_cv);
172 cv_destroy(&tx->tx_sync_done_cv);
173 cv_destroy(&tx->tx_quiesce_more_cv);
174 cv_destroy(&tx->tx_quiesce_done_cv);
175 cv_destroy(&tx->tx_exit_cv);
177 for (c = 0; c < max_ncpus; c++) {
180 mutex_destroy(&tx->tx_cpu[c].tc_open_lock);
181 mutex_destroy(&tx->tx_cpu[c].tc_lock);
182 for (i = 0; i < TXG_SIZE; i++) {
183 cv_destroy(&tx->tx_cpu[c].tc_cv[i]);
184 list_destroy(&tx->tx_cpu[c].tc_callbacks[i]);
188 if (tx->tx_commit_cb_taskq != NULL)
189 taskq_destroy(tx->tx_commit_cb_taskq);
191 kmem_free(tx->tx_cpu, max_ncpus * sizeof (tx_cpu_t));
193 bzero(tx, sizeof (tx_state_t));
197 * Start syncing transaction groups.
200 txg_sync_start(dsl_pool_t *dp)
202 tx_state_t *tx = &dp->dp_tx;
204 mutex_enter(&tx->tx_sync_lock);
206 dprintf("pool %p\n", dp);
208 ASSERT0(tx->tx_threads);
212 tx->tx_quiesce_thread = thread_create(NULL, 0, txg_quiesce_thread,
213 dp, 0, spa_proc(dp->dp_spa), TS_RUN, minclsyspri);
216 * The sync thread can need a larger-than-default stack size on
217 * 32-bit x86. This is due in part to nested pools and
218 * scrub_visitbp() recursion.
220 tx->tx_sync_thread = thread_create(NULL, 32<<10, txg_sync_thread,
221 dp, 0, spa_proc(dp->dp_spa), TS_RUN, minclsyspri);
223 mutex_exit(&tx->tx_sync_lock);
227 txg_thread_enter(tx_state_t *tx, callb_cpr_t *cpr)
229 CALLB_CPR_INIT(cpr, &tx->tx_sync_lock, callb_generic_cpr, FTAG);
230 mutex_enter(&tx->tx_sync_lock);
234 txg_thread_exit(tx_state_t *tx, callb_cpr_t *cpr, kthread_t **tpp)
236 ASSERT(*tpp != NULL);
239 cv_broadcast(&tx->tx_exit_cv);
240 CALLB_CPR_EXIT(cpr); /* drops &tx->tx_sync_lock */
245 txg_thread_wait(tx_state_t *tx, callb_cpr_t *cpr, kcondvar_t *cv, clock_t time)
247 CALLB_CPR_SAFE_BEGIN(cpr);
250 (void) cv_timedwait(cv, &tx->tx_sync_lock, time);
252 cv_wait(cv, &tx->tx_sync_lock);
254 CALLB_CPR_SAFE_END(cpr, &tx->tx_sync_lock);
258 * Stop syncing transaction groups.
261 txg_sync_stop(dsl_pool_t *dp)
263 tx_state_t *tx = &dp->dp_tx;
265 dprintf("pool %p\n", dp);
267 * Finish off any work in progress.
269 ASSERT3U(tx->tx_threads, ==, 2);
272 * We need to ensure that we've vacated the deferred space_maps.
274 txg_wait_synced(dp, tx->tx_open_txg + TXG_DEFER_SIZE);
277 * Wake all sync threads and wait for them to die.
279 mutex_enter(&tx->tx_sync_lock);
281 ASSERT3U(tx->tx_threads, ==, 2);
285 cv_broadcast(&tx->tx_quiesce_more_cv);
286 cv_broadcast(&tx->tx_quiesce_done_cv);
287 cv_broadcast(&tx->tx_sync_more_cv);
289 while (tx->tx_threads != 0)
290 cv_wait(&tx->tx_exit_cv, &tx->tx_sync_lock);
294 mutex_exit(&tx->tx_sync_lock);
298 txg_hold_open(dsl_pool_t *dp, txg_handle_t *th)
300 tx_state_t *tx = &dp->dp_tx;
301 tx_cpu_t *tc = &tx->tx_cpu[CPU_SEQID];
304 mutex_enter(&tc->tc_open_lock);
305 txg = tx->tx_open_txg;
307 mutex_enter(&tc->tc_lock);
308 tc->tc_count[txg & TXG_MASK]++;
309 mutex_exit(&tc->tc_lock);
318 txg_rele_to_quiesce(txg_handle_t *th)
320 tx_cpu_t *tc = th->th_cpu;
322 ASSERT(!MUTEX_HELD(&tc->tc_lock));
323 mutex_exit(&tc->tc_open_lock);
327 txg_register_callbacks(txg_handle_t *th, list_t *tx_callbacks)
329 tx_cpu_t *tc = th->th_cpu;
330 int g = th->th_txg & TXG_MASK;
332 mutex_enter(&tc->tc_lock);
333 list_move_tail(&tc->tc_callbacks[g], tx_callbacks);
334 mutex_exit(&tc->tc_lock);
338 txg_rele_to_sync(txg_handle_t *th)
340 tx_cpu_t *tc = th->th_cpu;
341 int g = th->th_txg & TXG_MASK;
343 mutex_enter(&tc->tc_lock);
344 ASSERT(tc->tc_count[g] != 0);
345 if (--tc->tc_count[g] == 0)
346 cv_broadcast(&tc->tc_cv[g]);
347 mutex_exit(&tc->tc_lock);
349 th->th_cpu = NULL; /* defensive */
353 * Blocks until all transactions in the group are committed.
355 * On return, the transaction group has reached a stable state in which it can
356 * then be passed off to the syncing context.
358 static __noinline void
359 txg_quiesce(dsl_pool_t *dp, uint64_t txg)
361 tx_state_t *tx = &dp->dp_tx;
362 int g = txg & TXG_MASK;
366 * Grab all tc_open_locks so nobody else can get into this txg.
368 for (c = 0; c < max_ncpus; c++)
369 mutex_enter(&tx->tx_cpu[c].tc_open_lock);
371 ASSERT(txg == tx->tx_open_txg);
373 tx->tx_open_time = gethrtime();
375 DTRACE_PROBE2(txg__quiescing, dsl_pool_t *, dp, uint64_t, txg);
376 DTRACE_PROBE2(txg__opened, dsl_pool_t *, dp, uint64_t, tx->tx_open_txg);
379 * Now that we've incremented tx_open_txg, we can let threads
380 * enter the next transaction group.
382 for (c = 0; c < max_ncpus; c++)
383 mutex_exit(&tx->tx_cpu[c].tc_open_lock);
386 * Quiesce the transaction group by waiting for everyone to txg_exit().
388 for (c = 0; c < max_ncpus; c++) {
389 tx_cpu_t *tc = &tx->tx_cpu[c];
390 mutex_enter(&tc->tc_lock);
391 while (tc->tc_count[g] != 0)
392 cv_wait(&tc->tc_cv[g], &tc->tc_lock);
393 mutex_exit(&tc->tc_lock);
398 txg_do_callbacks(void *arg)
400 list_t *cb_list = arg;
402 dmu_tx_do_callbacks(cb_list, 0);
404 list_destroy(cb_list);
406 kmem_free(cb_list, sizeof (list_t));
410 * Dispatch the commit callbacks registered on this txg to worker threads.
412 * If no callbacks are registered for a given TXG, nothing happens.
413 * This function creates a taskq for the associated pool, if needed.
416 txg_dispatch_callbacks(dsl_pool_t *dp, uint64_t txg)
419 tx_state_t *tx = &dp->dp_tx;
422 for (c = 0; c < max_ncpus; c++) {
423 tx_cpu_t *tc = &tx->tx_cpu[c];
425 * No need to lock tx_cpu_t at this point, since this can
426 * only be called once a txg has been synced.
429 int g = txg & TXG_MASK;
431 if (list_is_empty(&tc->tc_callbacks[g]))
434 if (tx->tx_commit_cb_taskq == NULL) {
436 * Commit callback taskq hasn't been created yet.
438 tx->tx_commit_cb_taskq = taskq_create("tx_commit_cb",
439 max_ncpus, minclsyspri, max_ncpus, max_ncpus * 2,
443 cb_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
444 list_create(cb_list, sizeof (dmu_tx_callback_t),
445 offsetof(dmu_tx_callback_t, dcb_node));
447 list_move_tail(cb_list, &tc->tc_callbacks[g]);
449 (void) taskq_dispatch(tx->tx_commit_cb_taskq, (task_func_t *)
450 txg_do_callbacks, cb_list, TQ_SLEEP);
455 txg_is_syncing(dsl_pool_t *dp)
457 tx_state_t *tx = &dp->dp_tx;
458 ASSERT(MUTEX_HELD(&tx->tx_sync_lock));
459 return (tx->tx_syncing_txg != 0);
463 txg_is_quiescing(dsl_pool_t *dp)
465 tx_state_t *tx = &dp->dp_tx;
466 ASSERT(MUTEX_HELD(&tx->tx_sync_lock));
467 return (tx->tx_quiescing_txg != 0);
471 txg_has_quiesced_to_sync(dsl_pool_t *dp)
473 tx_state_t *tx = &dp->dp_tx;
474 ASSERT(MUTEX_HELD(&tx->tx_sync_lock));
475 return (tx->tx_quiesced_txg != 0);
479 txg_sync_thread(void *arg)
481 dsl_pool_t *dp = arg;
482 spa_t *spa = dp->dp_spa;
483 tx_state_t *tx = &dp->dp_tx;
485 uint64_t start, delta;
487 txg_thread_enter(tx, &cpr);
491 uint64_t timeout = zfs_txg_timeout * hz;
494 uint64_t dirty_min_bytes =
495 zfs_dirty_data_max * zfs_dirty_data_sync_pct / 100;
498 * We sync when we're scanning, there's someone waiting
499 * on us, or the quiesce thread has handed off a txg to
500 * us, or we have reached our timeout.
502 timer = (delta >= timeout ? 0 : timeout - delta);
503 while (!dsl_scan_active(dp->dp_scan) &&
504 !tx->tx_exiting && timer > 0 &&
505 tx->tx_synced_txg >= tx->tx_sync_txg_waiting &&
506 !txg_has_quiesced_to_sync(dp) &&
507 dp->dp_dirty_total < dirty_min_bytes) {
508 dprintf("waiting; tx_synced=%llu waiting=%llu dp=%p\n",
509 tx->tx_synced_txg, tx->tx_sync_txg_waiting, dp);
510 txg_thread_wait(tx, &cpr, &tx->tx_sync_more_cv, timer);
511 delta = ddi_get_lbolt() - start;
512 timer = (delta > timeout ? 0 : timeout - delta);
516 * Wait until the quiesce thread hands off a txg to us,
517 * prompting it to do so if necessary.
519 while (!tx->tx_exiting && !txg_has_quiesced_to_sync(dp)) {
520 if (tx->tx_quiesce_txg_waiting < tx->tx_open_txg+1)
521 tx->tx_quiesce_txg_waiting = tx->tx_open_txg+1;
522 cv_broadcast(&tx->tx_quiesce_more_cv);
523 txg_thread_wait(tx, &cpr, &tx->tx_quiesce_done_cv, 0);
527 txg_thread_exit(tx, &cpr, &tx->tx_sync_thread);
530 * Consume the quiesced txg which has been handed off to
531 * us. This may cause the quiescing thread to now be
532 * able to quiesce another txg, so we must signal it.
534 ASSERT(tx->tx_quiesced_txg != 0);
535 txg = tx->tx_quiesced_txg;
536 tx->tx_quiesced_txg = 0;
537 tx->tx_syncing_txg = txg;
538 DTRACE_PROBE2(txg__syncing, dsl_pool_t *, dp, uint64_t, txg);
539 cv_broadcast(&tx->tx_quiesce_more_cv);
541 dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
542 txg, tx->tx_quiesce_txg_waiting, tx->tx_sync_txg_waiting);
543 mutex_exit(&tx->tx_sync_lock);
545 start = ddi_get_lbolt();
547 delta = ddi_get_lbolt() - start;
549 mutex_enter(&tx->tx_sync_lock);
550 tx->tx_synced_txg = txg;
551 tx->tx_syncing_txg = 0;
552 DTRACE_PROBE2(txg__synced, dsl_pool_t *, dp, uint64_t, txg);
553 cv_broadcast(&tx->tx_sync_done_cv);
556 * Dispatch commit callbacks to worker threads.
558 txg_dispatch_callbacks(dp, txg);
563 txg_quiesce_thread(void *arg)
565 dsl_pool_t *dp = arg;
566 tx_state_t *tx = &dp->dp_tx;
569 txg_thread_enter(tx, &cpr);
575 * We quiesce when there's someone waiting on us.
576 * However, we can only have one txg in "quiescing" or
577 * "quiesced, waiting to sync" state. So we wait until
578 * the "quiesced, waiting to sync" txg has been consumed
579 * by the sync thread.
581 while (!tx->tx_exiting &&
582 (tx->tx_open_txg >= tx->tx_quiesce_txg_waiting ||
583 txg_has_quiesced_to_sync(dp)))
584 txg_thread_wait(tx, &cpr, &tx->tx_quiesce_more_cv, 0);
587 txg_thread_exit(tx, &cpr, &tx->tx_quiesce_thread);
589 txg = tx->tx_open_txg;
590 dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
591 txg, tx->tx_quiesce_txg_waiting,
592 tx->tx_sync_txg_waiting);
593 tx->tx_quiescing_txg = txg;
595 mutex_exit(&tx->tx_sync_lock);
596 txg_quiesce(dp, txg);
597 mutex_enter(&tx->tx_sync_lock);
600 * Hand this txg off to the sync thread.
602 dprintf("quiesce done, handing off txg %llu\n", txg);
603 tx->tx_quiescing_txg = 0;
604 tx->tx_quiesced_txg = txg;
605 DTRACE_PROBE2(txg__quiesced, dsl_pool_t *, dp, uint64_t, txg);
606 cv_broadcast(&tx->tx_sync_more_cv);
607 cv_broadcast(&tx->tx_quiesce_done_cv);
612 * Delay this thread by delay nanoseconds if we are still in the open
613 * transaction group and there is already a waiting txg quiesing or quiesced.
614 * Abort the delay if this txg stalls or enters the quiesing state.
617 txg_delay(dsl_pool_t *dp, uint64_t txg, hrtime_t delay, hrtime_t resolution)
619 tx_state_t *tx = &dp->dp_tx;
620 hrtime_t start = gethrtime();
622 /* don't delay if this txg could transition to quiescing immediately */
623 if (tx->tx_open_txg > txg ||
624 tx->tx_syncing_txg == txg-1 || tx->tx_synced_txg == txg-1)
627 mutex_enter(&tx->tx_sync_lock);
628 if (tx->tx_open_txg > txg || tx->tx_synced_txg == txg-1) {
629 mutex_exit(&tx->tx_sync_lock);
633 while (gethrtime() - start < delay &&
634 tx->tx_syncing_txg < txg-1 && !txg_stalled(dp)) {
635 (void) cv_timedwait_hires(&tx->tx_quiesce_more_cv,
636 &tx->tx_sync_lock, delay, resolution, 0);
639 mutex_exit(&tx->tx_sync_lock);
643 txg_wait_synced_impl(dsl_pool_t *dp, uint64_t txg, boolean_t wait_sig)
645 tx_state_t *tx = &dp->dp_tx;
647 ASSERT(!dsl_pool_config_held(dp));
649 mutex_enter(&tx->tx_sync_lock);
650 ASSERT3U(tx->tx_threads, ==, 2);
652 txg = tx->tx_open_txg + TXG_DEFER_SIZE;
653 if (tx->tx_sync_txg_waiting < txg)
654 tx->tx_sync_txg_waiting = txg;
655 dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
656 txg, tx->tx_quiesce_txg_waiting, tx->tx_sync_txg_waiting);
657 while (tx->tx_synced_txg < txg) {
658 dprintf("broadcasting sync more "
659 "tx_synced=%llu waiting=%llu dp=%p\n",
660 tx->tx_synced_txg, tx->tx_sync_txg_waiting, dp);
661 cv_broadcast(&tx->tx_sync_more_cv);
664 * Condition wait here but stop if the thread receives a
665 * signal. The caller may call txg_wait_synced*() again
666 * to resume waiting for this txg.
670 * FreeBSD returns EINTR or ERESTART if there is
671 * a pending signal, zero if the conditional variable
672 * is signaled. illumos returns zero in the former case
673 * and >0 in the latter.
675 if (cv_wait_sig(&tx->tx_sync_done_cv,
676 &tx->tx_sync_lock) != 0) {
678 if (cv_wait_sig(&tx->tx_sync_done_cv,
679 &tx->tx_sync_lock) == 0) {
682 mutex_exit(&tx->tx_sync_lock);
686 cv_wait(&tx->tx_sync_done_cv, &tx->tx_sync_lock);
689 mutex_exit(&tx->tx_sync_lock);
694 txg_wait_synced(dsl_pool_t *dp, uint64_t txg)
696 VERIFY0(txg_wait_synced_impl(dp, txg, B_FALSE));
700 * Similar to a txg_wait_synced but it can be interrupted from a signal.
701 * Returns B_TRUE if the thread was signaled while waiting.
704 txg_wait_synced_sig(dsl_pool_t *dp, uint64_t txg)
706 return (txg_wait_synced_impl(dp, txg, B_TRUE));
710 txg_wait_open(dsl_pool_t *dp, uint64_t txg)
712 tx_state_t *tx = &dp->dp_tx;
714 ASSERT(!dsl_pool_config_held(dp));
716 mutex_enter(&tx->tx_sync_lock);
717 ASSERT3U(tx->tx_threads, ==, 2);
719 txg = tx->tx_open_txg + 1;
720 if (tx->tx_quiesce_txg_waiting < txg)
721 tx->tx_quiesce_txg_waiting = txg;
722 dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
723 txg, tx->tx_quiesce_txg_waiting, tx->tx_sync_txg_waiting);
724 while (tx->tx_open_txg < txg) {
725 cv_broadcast(&tx->tx_quiesce_more_cv);
726 cv_wait(&tx->tx_quiesce_done_cv, &tx->tx_sync_lock);
728 mutex_exit(&tx->tx_sync_lock);
732 * If there isn't a txg syncing or in the pipeline, push another txg through
733 * the pipeline by queiscing the open txg.
736 txg_kick(dsl_pool_t *dp)
738 tx_state_t *tx = &dp->dp_tx;
740 ASSERT(!dsl_pool_config_held(dp));
742 mutex_enter(&tx->tx_sync_lock);
743 if (!txg_is_syncing(dp) &&
744 !txg_is_quiescing(dp) &&
745 tx->tx_quiesce_txg_waiting <= tx->tx_open_txg &&
746 tx->tx_sync_txg_waiting <= tx->tx_synced_txg &&
747 tx->tx_quiesced_txg <= tx->tx_synced_txg) {
748 tx->tx_quiesce_txg_waiting = tx->tx_open_txg + 1;
749 cv_broadcast(&tx->tx_quiesce_more_cv);
751 mutex_exit(&tx->tx_sync_lock);
755 txg_stalled(dsl_pool_t *dp)
757 tx_state_t *tx = &dp->dp_tx;
758 return (tx->tx_quiesce_txg_waiting > tx->tx_open_txg);
762 txg_sync_waiting(dsl_pool_t *dp)
764 tx_state_t *tx = &dp->dp_tx;
766 return (tx->tx_syncing_txg <= tx->tx_sync_txg_waiting ||
767 tx->tx_quiesced_txg != 0);
771 * Verify that this txg is active (open, quiescing, syncing). Non-active
772 * txg's should not be manipulated.
775 txg_verify(spa_t *spa, uint64_t txg)
777 dsl_pool_t *dp = spa_get_dsl(spa);
778 if (txg <= TXG_INITIAL || txg == ZILTEST_TXG)
780 ASSERT3U(txg, <=, dp->dp_tx.tx_open_txg);
781 ASSERT3U(txg, >=, dp->dp_tx.tx_synced_txg);
782 ASSERT3U(txg, >=, dp->dp_tx.tx_open_txg - TXG_CONCURRENT_STATES);
786 * Per-txg object lists.
789 txg_list_create(txg_list_t *tl, spa_t *spa, size_t offset)
793 mutex_init(&tl->tl_lock, NULL, MUTEX_DEFAULT, NULL);
795 tl->tl_offset = offset;
798 for (t = 0; t < TXG_SIZE; t++)
799 tl->tl_head[t] = NULL;
803 txg_list_destroy(txg_list_t *tl)
807 for (t = 0; t < TXG_SIZE; t++)
808 ASSERT(txg_list_empty(tl, t));
810 mutex_destroy(&tl->tl_lock);
814 txg_list_empty(txg_list_t *tl, uint64_t txg)
816 txg_verify(tl->tl_spa, txg);
817 return (tl->tl_head[txg & TXG_MASK] == NULL);
821 * Returns true if all txg lists are empty.
823 * Warning: this is inherently racy (an item could be added immediately
824 * after this function returns). We don't bother with the lock because
825 * it wouldn't change the semantics.
828 txg_all_lists_empty(txg_list_t *tl)
830 for (int i = 0; i < TXG_SIZE; i++) {
831 if (!txg_list_empty(tl, i)) {
839 * Add an entry to the list (unless it's already on the list).
840 * Returns B_TRUE if it was actually added.
843 txg_list_add(txg_list_t *tl, void *p, uint64_t txg)
845 int t = txg & TXG_MASK;
846 txg_node_t *tn = (txg_node_t *)((char *)p + tl->tl_offset);
849 txg_verify(tl->tl_spa, txg);
850 mutex_enter(&tl->tl_lock);
851 add = (tn->tn_member[t] == 0);
853 tn->tn_member[t] = 1;
854 tn->tn_next[t] = tl->tl_head[t];
857 mutex_exit(&tl->tl_lock);
863 * Add an entry to the end of the list, unless it's already on the list.
864 * (walks list to find end)
865 * Returns B_TRUE if it was actually added.
868 txg_list_add_tail(txg_list_t *tl, void *p, uint64_t txg)
870 int t = txg & TXG_MASK;
871 txg_node_t *tn = (txg_node_t *)((char *)p + tl->tl_offset);
874 txg_verify(tl->tl_spa, txg);
875 mutex_enter(&tl->tl_lock);
876 add = (tn->tn_member[t] == 0);
880 for (tp = &tl->tl_head[t]; *tp != NULL; tp = &(*tp)->tn_next[t])
883 tn->tn_member[t] = 1;
884 tn->tn_next[t] = NULL;
887 mutex_exit(&tl->tl_lock);
893 * Remove the head of the list and return it.
896 txg_list_remove(txg_list_t *tl, uint64_t txg)
898 int t = txg & TXG_MASK;
902 txg_verify(tl->tl_spa, txg);
903 mutex_enter(&tl->tl_lock);
904 if ((tn = tl->tl_head[t]) != NULL) {
905 ASSERT(tn->tn_member[t]);
906 ASSERT(tn->tn_next[t] == NULL || tn->tn_next[t]->tn_member[t]);
907 p = (char *)tn - tl->tl_offset;
908 tl->tl_head[t] = tn->tn_next[t];
909 tn->tn_next[t] = NULL;
910 tn->tn_member[t] = 0;
912 mutex_exit(&tl->tl_lock);
918 * Remove a specific item from the list and return it.
921 txg_list_remove_this(txg_list_t *tl, void *p, uint64_t txg)
923 int t = txg & TXG_MASK;
924 txg_node_t *tn, **tp;
926 txg_verify(tl->tl_spa, txg);
927 mutex_enter(&tl->tl_lock);
929 for (tp = &tl->tl_head[t]; (tn = *tp) != NULL; tp = &tn->tn_next[t]) {
930 if ((char *)tn - tl->tl_offset == p) {
931 *tp = tn->tn_next[t];
932 tn->tn_next[t] = NULL;
933 tn->tn_member[t] = 0;
934 mutex_exit(&tl->tl_lock);
939 mutex_exit(&tl->tl_lock);
945 txg_list_member(txg_list_t *tl, void *p, uint64_t txg)
947 int t = txg & TXG_MASK;
948 txg_node_t *tn = (txg_node_t *)((char *)p + tl->tl_offset);
950 txg_verify(tl->tl_spa, txg);
951 return (tn->tn_member[t] != 0);
955 * Walk a txg list -- only safe if you know it's not changing.
958 txg_list_head(txg_list_t *tl, uint64_t txg)
960 int t = txg & TXG_MASK;
961 txg_node_t *tn = tl->tl_head[t];
963 txg_verify(tl->tl_spa, txg);
964 return (tn == NULL ? NULL : (char *)tn - tl->tl_offset);
968 txg_list_next(txg_list_t *tl, void *p, uint64_t txg)
970 int t = txg & TXG_MASK;
971 txg_node_t *tn = (txg_node_t *)((char *)p + tl->tl_offset);
973 txg_verify(tl->tl_spa, txg);
976 return (tn == NULL ? NULL : (char *)tn - tl->tl_offset);