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 * Portions Copyright 2011 Martin Matuska
15 * Copyright (c) 2012, 2019 by Delphix. All rights reserved.
16 * Copyright (c) 2025, Klara, Inc.
17 */
18
19 #include <sys/zfs_context.h>
20 #include <sys/txg_impl.h>
21 #include <sys/dmu_impl.h>
22 #include <sys/spa_impl.h>
23 #include <sys/dmu_tx.h>
24 #include <sys/dsl_pool.h>
25 #include <sys/dsl_scan.h>
26 #include <sys/zil.h>
27 #include <sys/callb.h>
28 #include <sys/trace_zfs.h>
29
30 /*
31 * ZFS Transaction Groups
32 * ----------------------
33 *
34 * ZFS transaction groups are, as the name implies, groups of transactions
35 * that act on persistent state. ZFS asserts consistency at the granularity of
36 * these transaction groups. Each successive transaction group (txg) is
37 * assigned a 64-bit consecutive identifier. There are three active
38 * transaction group states: open, quiescing, or syncing. At any given time,
39 * there may be an active txg associated with each state; each active txg may
40 * either be processing, or blocked waiting to enter the next state. There may
41 * be up to three active txgs, and there is always a txg in the open state
42 * (though it may be blocked waiting to enter the quiescing state). In broad
43 * strokes, transactions -- operations that change in-memory structures -- are
44 * accepted into the txg in the open state, and are completed while the txg is
45 * in the open or quiescing states. The accumulated changes are written to
46 * disk in the syncing state.
47 *
48 * Open
49 *
50 * When a new txg becomes active, it first enters the open state. New
51 * transactions -- updates to in-memory structures -- are assigned to the
52 * currently open txg. There is always a txg in the open state so that ZFS can
53 * accept new changes (though the txg may refuse new changes if it has hit
54 * some limit). ZFS advances the open txg to the next state for a variety of
55 * reasons such as it hitting a time or size threshold, or the execution of an
56 * administrative action that must be completed in the syncing state.
57 *
58 * Quiescing
59 *
60 * After a txg exits the open state, it enters the quiescing state. The
61 * quiescing state is intended to provide a buffer between accepting new
62 * transactions in the open state and writing them out to stable storage in
63 * the syncing state. While quiescing, transactions can continue their
64 * operation without delaying either of the other states. Typically, a txg is
65 * in the quiescing state very briefly since the operations are bounded by
66 * software latencies rather than, say, slower I/O latencies. After all
67 * transactions complete, the txg is ready to enter the next state.
68 *
69 * Syncing
70 *
71 * In the syncing state, the in-memory state built up during the open and (to
72 * a lesser degree) the quiescing states is written to stable storage. The
73 * process of writing out modified data can, in turn modify more data. For
74 * example when we write new blocks, we need to allocate space for them; those
75 * allocations modify metadata (space maps)... which themselves must be
76 * written to stable storage. During the sync state, ZFS iterates, writing out
77 * data until it converges and all in-memory changes have been written out.
78 * The first such pass is the largest as it encompasses all the modified user
79 * data (as opposed to filesystem metadata). Subsequent passes typically have
80 * far less data to write as they consist exclusively of filesystem metadata.
81 *
82 * To ensure convergence, after a certain number of passes ZFS begins
83 * overwriting locations on stable storage that had been allocated earlier in
84 * the syncing state (and subsequently freed). ZFS usually allocates new
85 * blocks to optimize for large, continuous, writes. For the syncing state to
86 * converge however it must complete a pass where no new blocks are allocated
87 * since each allocation requires a modification of persistent metadata.
88 * Further, to hasten convergence, after a prescribed number of passes, ZFS
89 * also defers frees, and stops compressing.
90 *
91 * In addition to writing out user data, we must also execute synctasks during
92 * the syncing context. A synctask is the mechanism by which some
93 * administrative activities work such as creating and destroying snapshots or
94 * datasets. Note that when a synctask is initiated it enters the open txg,
95 * and ZFS then pushes that txg as quickly as possible to completion of the
96 * syncing state in order to reduce the latency of the administrative
97 * activity. To complete the syncing state, ZFS writes out a new uberblock,
98 * the root of the tree of blocks that comprise all state stored on the ZFS
99 * pool. Finally, if there is a quiesced txg waiting, we signal that it can
100 * now transition to the syncing state.
101 */
102
103 static __attribute__((noreturn)) void txg_sync_thread(void *arg);
104 static __attribute__((noreturn)) void txg_quiesce_thread(void *arg);
105
106 uint_t zfs_txg_timeout = 5; /* max seconds worth of delta per txg */
107
108 /*
109 * Prepare the txg subsystem.
110 */
111 void
txg_init(dsl_pool_t * dp,uint64_t txg)112 txg_init(dsl_pool_t *dp, uint64_t txg)
113 {
114 tx_state_t *tx = &dp->dp_tx;
115 int c;
116 memset(tx, 0, sizeof (tx_state_t));
117
118 tx->tx_cpu = vmem_zalloc(max_ncpus * sizeof (tx_cpu_t), KM_SLEEP);
119
120 for (c = 0; c < max_ncpus; c++) {
121 int i;
122
123 mutex_init(&tx->tx_cpu[c].tc_lock, NULL, MUTEX_DEFAULT, NULL);
124 mutex_init(&tx->tx_cpu[c].tc_open_lock, NULL, MUTEX_NOLOCKDEP,
125 NULL);
126 for (i = 0; i < TXG_SIZE; i++) {
127 cv_init(&tx->tx_cpu[c].tc_cv[i], NULL, CV_DEFAULT,
128 NULL);
129 list_create(&tx->tx_cpu[c].tc_callbacks[i],
130 sizeof (dmu_tx_callback_t),
131 offsetof(dmu_tx_callback_t, dcb_node));
132 }
133 }
134
135 mutex_init(&tx->tx_sync_lock, NULL, MUTEX_DEFAULT, NULL);
136
137 cv_init(&tx->tx_sync_more_cv, NULL, CV_DEFAULT, NULL);
138 cv_init(&tx->tx_sync_done_cv, NULL, CV_DEFAULT, NULL);
139 cv_init(&tx->tx_quiesce_more_cv, NULL, CV_DEFAULT, NULL);
140 cv_init(&tx->tx_quiesce_done_cv, NULL, CV_DEFAULT, NULL);
141 cv_init(&tx->tx_exit_cv, NULL, CV_DEFAULT, NULL);
142
143 tx->tx_open_txg = txg;
144 }
145
146 /*
147 * Close down the txg subsystem.
148 */
149 void
txg_fini(dsl_pool_t * dp)150 txg_fini(dsl_pool_t *dp)
151 {
152 tx_state_t *tx = &dp->dp_tx;
153 int c;
154
155 ASSERT0(tx->tx_threads);
156
157 mutex_destroy(&tx->tx_sync_lock);
158
159 cv_destroy(&tx->tx_sync_more_cv);
160 cv_destroy(&tx->tx_sync_done_cv);
161 cv_destroy(&tx->tx_quiesce_more_cv);
162 cv_destroy(&tx->tx_quiesce_done_cv);
163 cv_destroy(&tx->tx_exit_cv);
164
165 for (c = 0; c < max_ncpus; c++) {
166 int i;
167
168 mutex_destroy(&tx->tx_cpu[c].tc_open_lock);
169 mutex_destroy(&tx->tx_cpu[c].tc_lock);
170 for (i = 0; i < TXG_SIZE; i++) {
171 cv_destroy(&tx->tx_cpu[c].tc_cv[i]);
172 list_destroy(&tx->tx_cpu[c].tc_callbacks[i]);
173 }
174 }
175
176 if (tx->tx_commit_cb_taskq != NULL)
177 taskq_destroy(tx->tx_commit_cb_taskq);
178
179 vmem_free(tx->tx_cpu, max_ncpus * sizeof (tx_cpu_t));
180
181 memset(tx, 0, sizeof (tx_state_t));
182 }
183
184 /*
185 * Start syncing transaction groups.
186 */
187 void
txg_sync_start(dsl_pool_t * dp)188 txg_sync_start(dsl_pool_t *dp)
189 {
190 tx_state_t *tx = &dp->dp_tx;
191
192 mutex_enter(&tx->tx_sync_lock);
193
194 dprintf("pool %p\n", dp);
195
196 ASSERT0(tx->tx_threads);
197
198 tx->tx_threads = 2;
199
200 tx->tx_quiesce_thread = thread_create(NULL, 0, txg_quiesce_thread,
201 dp, 0, &p0, TS_RUN, defclsyspri);
202
203 /*
204 * The sync thread can need a larger-than-default stack size on
205 * 32-bit x86. This is due in part to nested pools and
206 * scrub_visitbp() recursion.
207 */
208 tx->tx_sync_thread = thread_create(NULL, 0, txg_sync_thread,
209 dp, 0, &p0, TS_RUN, defclsyspri);
210
211 mutex_exit(&tx->tx_sync_lock);
212 }
213
214 static void
txg_thread_enter(tx_state_t * tx,callb_cpr_t * cpr)215 txg_thread_enter(tx_state_t *tx, callb_cpr_t *cpr)
216 {
217 CALLB_CPR_INIT(cpr, &tx->tx_sync_lock, callb_generic_cpr, FTAG);
218 mutex_enter(&tx->tx_sync_lock);
219 }
220
221 static void
txg_thread_exit(tx_state_t * tx,callb_cpr_t * cpr,kthread_t ** tpp)222 txg_thread_exit(tx_state_t *tx, callb_cpr_t *cpr, kthread_t **tpp)
223 {
224 ASSERT(*tpp != NULL);
225 *tpp = NULL;
226 tx->tx_threads--;
227 cv_broadcast(&tx->tx_exit_cv);
228 CALLB_CPR_EXIT(cpr); /* drops &tx->tx_sync_lock */
229 thread_exit();
230 }
231
232 static void
txg_thread_wait(tx_state_t * tx,callb_cpr_t * cpr,kcondvar_t * cv,clock_t time)233 txg_thread_wait(tx_state_t *tx, callb_cpr_t *cpr, kcondvar_t *cv, clock_t time)
234 {
235 CALLB_CPR_SAFE_BEGIN(cpr);
236
237 if (time) {
238 (void) cv_timedwait_idle(cv, &tx->tx_sync_lock,
239 ddi_get_lbolt() + time);
240 } else {
241 cv_wait_idle(cv, &tx->tx_sync_lock);
242 }
243
244 CALLB_CPR_SAFE_END(cpr, &tx->tx_sync_lock);
245 }
246
247 /*
248 * Stop syncing transaction groups.
249 */
250 void
txg_sync_stop(dsl_pool_t * dp)251 txg_sync_stop(dsl_pool_t *dp)
252 {
253 tx_state_t *tx = &dp->dp_tx;
254
255 dprintf("pool %p\n", dp);
256 /*
257 * Finish off any work in progress.
258 */
259 ASSERT3U(tx->tx_threads, ==, 2);
260
261 /*
262 * We need to ensure that we've vacated the deferred metaslab trees.
263 */
264 txg_wait_synced(dp, tx->tx_open_txg + TXG_DEFER_SIZE);
265
266 /*
267 * Wake all sync threads and wait for them to die.
268 */
269 mutex_enter(&tx->tx_sync_lock);
270
271 ASSERT3U(tx->tx_threads, ==, 2);
272
273 tx->tx_exiting = 1;
274
275 cv_broadcast(&tx->tx_quiesce_more_cv);
276 cv_broadcast(&tx->tx_quiesce_done_cv);
277 cv_broadcast(&tx->tx_sync_more_cv);
278
279 while (tx->tx_threads != 0)
280 cv_wait(&tx->tx_exit_cv, &tx->tx_sync_lock);
281
282 tx->tx_exiting = 0;
283
284 mutex_exit(&tx->tx_sync_lock);
285 }
286
287 /*
288 * Get a handle on the currently open txg and keep it open.
289 *
290 * The txg is guaranteed to stay open until txg_rele_to_quiesce() is called for
291 * the handle. Once txg_rele_to_quiesce() has been called, the txg stays
292 * in quiescing state until txg_rele_to_sync() is called for the handle.
293 *
294 * It is guaranteed that subsequent calls return monotonically increasing
295 * txgs for the same dsl_pool_t. Of course this is not strong monotonicity,
296 * because the same txg can be returned multiple times in a row. This
297 * guarantee holds both for subsequent calls from one thread and for multiple
298 * threads. For example, it is impossible to observe the following sequence
299 * of events:
300 *
301 * Thread 1 Thread 2
302 *
303 * 1 <- txg_hold_open(P, ...)
304 * 2 <- txg_hold_open(P, ...)
305 * 1 <- txg_hold_open(P, ...)
306 *
307 */
308 uint64_t
txg_hold_open(dsl_pool_t * dp,txg_handle_t * th)309 txg_hold_open(dsl_pool_t *dp, txg_handle_t *th)
310 {
311 tx_state_t *tx = &dp->dp_tx;
312 tx_cpu_t *tc;
313 uint64_t txg;
314
315 /*
316 * It appears the processor id is simply used as a "random"
317 * number to index into the array, and there isn't any other
318 * significance to the chosen tx_cpu. Because.. Why not use
319 * the current cpu to index into the array?
320 */
321 tc = &tx->tx_cpu[CPU_SEQID_UNSTABLE];
322
323 mutex_enter(&tc->tc_open_lock);
324 txg = tx->tx_open_txg;
325
326 mutex_enter(&tc->tc_lock);
327 tc->tc_count[txg & TXG_MASK]++;
328 mutex_exit(&tc->tc_lock);
329
330 th->th_cpu = tc;
331 th->th_txg = txg;
332
333 return (txg);
334 }
335
336 void
txg_rele_to_quiesce(txg_handle_t * th)337 txg_rele_to_quiesce(txg_handle_t *th)
338 {
339 tx_cpu_t *tc = th->th_cpu;
340
341 ASSERT(!MUTEX_HELD(&tc->tc_lock));
342 mutex_exit(&tc->tc_open_lock);
343 }
344
345 void
txg_register_callbacks(txg_handle_t * th,list_t * tx_callbacks)346 txg_register_callbacks(txg_handle_t *th, list_t *tx_callbacks)
347 {
348 tx_cpu_t *tc = th->th_cpu;
349 int g = th->th_txg & TXG_MASK;
350
351 mutex_enter(&tc->tc_lock);
352 list_move_tail(&tc->tc_callbacks[g], tx_callbacks);
353 mutex_exit(&tc->tc_lock);
354 }
355
356 void
txg_rele_to_sync(txg_handle_t * th)357 txg_rele_to_sync(txg_handle_t *th)
358 {
359 tx_cpu_t *tc = th->th_cpu;
360 int g = th->th_txg & TXG_MASK;
361
362 mutex_enter(&tc->tc_lock);
363 ASSERT(tc->tc_count[g] != 0);
364 if (--tc->tc_count[g] == 0)
365 cv_broadcast(&tc->tc_cv[g]);
366 mutex_exit(&tc->tc_lock);
367
368 th->th_cpu = NULL; /* defensive */
369 }
370
371 /*
372 * Blocks until all transactions in the group are committed.
373 *
374 * On return, the transaction group has reached a stable state in which it can
375 * then be passed off to the syncing context.
376 */
377 static void
txg_quiesce(dsl_pool_t * dp,uint64_t txg)378 txg_quiesce(dsl_pool_t *dp, uint64_t txg)
379 {
380 tx_state_t *tx = &dp->dp_tx;
381 uint64_t tx_open_time;
382 int g = txg & TXG_MASK;
383 int c;
384
385 /*
386 * Grab all tc_open_locks so nobody else can get into this txg.
387 */
388 for (c = 0; c < max_ncpus; c++)
389 mutex_enter(&tx->tx_cpu[c].tc_open_lock);
390
391 ASSERT(txg == tx->tx_open_txg);
392 tx->tx_open_txg++;
393 tx->tx_open_time = tx_open_time = gethrtime();
394
395 DTRACE_PROBE2(txg__quiescing, dsl_pool_t *, dp, uint64_t, txg);
396 DTRACE_PROBE2(txg__opened, dsl_pool_t *, dp, uint64_t, tx->tx_open_txg);
397
398 /*
399 * Now that we've incremented tx_open_txg, we can let threads
400 * enter the next transaction group.
401 */
402 for (c = 0; c < max_ncpus; c++)
403 mutex_exit(&tx->tx_cpu[c].tc_open_lock);
404
405 spa_txg_history_set(dp->dp_spa, txg, TXG_STATE_OPEN, tx_open_time);
406 spa_txg_history_add(dp->dp_spa, txg + 1, tx_open_time);
407
408 /*
409 * Quiesce the transaction group by waiting for everyone to
410 * call txg_rele_to_sync() for their open transaction handles.
411 */
412 for (c = 0; c < max_ncpus; c++) {
413 tx_cpu_t *tc = &tx->tx_cpu[c];
414 mutex_enter(&tc->tc_lock);
415 while (tc->tc_count[g] != 0)
416 cv_wait(&tc->tc_cv[g], &tc->tc_lock);
417 mutex_exit(&tc->tc_lock);
418 }
419
420 spa_txg_history_set(dp->dp_spa, txg, TXG_STATE_QUIESCED, gethrtime());
421 }
422
423 static void
txg_do_callbacks(void * cb_list)424 txg_do_callbacks(void *cb_list)
425 {
426 dmu_tx_do_callbacks(cb_list, 0);
427
428 list_destroy(cb_list);
429
430 kmem_free(cb_list, sizeof (list_t));
431 }
432
433 /*
434 * Dispatch the commit callbacks registered on this txg to worker threads.
435 *
436 * If no callbacks are registered for a given TXG, nothing happens.
437 * This function creates a taskq for the associated pool, if needed.
438 */
439 static void
txg_dispatch_callbacks(dsl_pool_t * dp,uint64_t txg)440 txg_dispatch_callbacks(dsl_pool_t *dp, uint64_t txg)
441 {
442 int c;
443 tx_state_t *tx = &dp->dp_tx;
444 list_t *cb_list;
445
446 for (c = 0; c < max_ncpus; c++) {
447 tx_cpu_t *tc = &tx->tx_cpu[c];
448 /*
449 * No need to lock tx_cpu_t at this point, since this can
450 * only be called once a txg has been synced.
451 */
452
453 int g = txg & TXG_MASK;
454
455 if (list_is_empty(&tc->tc_callbacks[g]))
456 continue;
457
458 if (tx->tx_commit_cb_taskq == NULL) {
459 /*
460 * Commit callback taskq hasn't been created yet.
461 */
462 tx->tx_commit_cb_taskq = taskq_create("tx_commit_cb",
463 100, defclsyspri, boot_ncpus, boot_ncpus * 2,
464 TASKQ_PREPOPULATE | TASKQ_DYNAMIC |
465 TASKQ_THREADS_CPU_PCT);
466 }
467
468 cb_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
469 list_create(cb_list, sizeof (dmu_tx_callback_t),
470 offsetof(dmu_tx_callback_t, dcb_node));
471
472 list_move_tail(cb_list, &tc->tc_callbacks[g]);
473
474 (void) taskq_dispatch(tx->tx_commit_cb_taskq,
475 txg_do_callbacks, cb_list, TQ_SLEEP);
476 }
477 }
478
479 /*
480 * Wait for pending commit callbacks of already-synced transactions to finish
481 * processing.
482 * Calling this function from within a commit callback will deadlock.
483 */
484 void
txg_wait_callbacks(dsl_pool_t * dp)485 txg_wait_callbacks(dsl_pool_t *dp)
486 {
487 tx_state_t *tx = &dp->dp_tx;
488
489 if (tx->tx_commit_cb_taskq != NULL)
490 taskq_wait_outstanding(tx->tx_commit_cb_taskq, 0);
491 }
492
493 static boolean_t
txg_is_quiescing(dsl_pool_t * dp)494 txg_is_quiescing(dsl_pool_t *dp)
495 {
496 tx_state_t *tx = &dp->dp_tx;
497 ASSERT(MUTEX_HELD(&tx->tx_sync_lock));
498 return (tx->tx_quiescing_txg != 0);
499 }
500
501 static boolean_t
txg_has_quiesced_to_sync(dsl_pool_t * dp)502 txg_has_quiesced_to_sync(dsl_pool_t *dp)
503 {
504 tx_state_t *tx = &dp->dp_tx;
505 ASSERT(MUTEX_HELD(&tx->tx_sync_lock));
506 return (tx->tx_quiesced_txg != 0);
507 }
508
509 static __attribute__((noreturn)) void
txg_sync_thread(void * arg)510 txg_sync_thread(void *arg)
511 {
512 dsl_pool_t *dp = arg;
513 spa_t *spa = dp->dp_spa;
514 tx_state_t *tx = &dp->dp_tx;
515 callb_cpr_t cpr;
516 clock_t start, delta;
517
518 (void) spl_fstrans_mark();
519 txg_thread_enter(tx, &cpr);
520
521 start = delta = 0;
522 for (;;) {
523 clock_t timeout = zfs_txg_timeout * hz;
524 clock_t timer;
525 uint64_t txg;
526
527 /*
528 * We sync when we're scanning or condensing, there's someone
529 * waiting on us, or the quiesce thread has handed off a txg to
530 * us, or we have reached our timeout.
531 */
532 timer = (delta >= timeout ? 0 : timeout - delta);
533 while (!dsl_scan_active(dp->dp_scan) &&
534 !spa_log_flushall_active(spa) &&
535 !tx->tx_exiting && timer > 0 &&
536 tx->tx_synced_txg >= tx->tx_sync_txg_waiting &&
537 !txg_has_quiesced_to_sync(dp)) {
538 dprintf("waiting; tx_synced=%llu waiting=%llu dp=%p\n",
539 (u_longlong_t)tx->tx_synced_txg,
540 (u_longlong_t)tx->tx_sync_txg_waiting, dp);
541 txg_thread_wait(tx, &cpr, &tx->tx_sync_more_cv, timer);
542 delta = ddi_get_lbolt() - start;
543 timer = (delta > timeout ? 0 : timeout - delta);
544 }
545
546 /*
547 * When we're suspended, nothing should be changing and for
548 * MMP we don't want to bump anything that would make it
549 * harder to detect if another host is changing it when
550 * resuming after a MMP suspend.
551 */
552 if (spa_suspended(spa))
553 continue;
554
555 /*
556 * Wait until the quiesce thread hands off a txg to us,
557 * prompting it to do so if necessary.
558 */
559 while (!tx->tx_exiting && !txg_has_quiesced_to_sync(dp)) {
560 if (txg_is_quiescing(dp)) {
561 txg_thread_wait(tx, &cpr,
562 &tx->tx_quiesce_done_cv, 0);
563 continue;
564 }
565 if (tx->tx_quiesce_txg_waiting < tx->tx_open_txg+1)
566 tx->tx_quiesce_txg_waiting = tx->tx_open_txg+1;
567 cv_broadcast(&tx->tx_quiesce_more_cv);
568 txg_thread_wait(tx, &cpr, &tx->tx_quiesce_done_cv, 0);
569 }
570
571 if (tx->tx_exiting)
572 txg_thread_exit(tx, &cpr, &tx->tx_sync_thread);
573
574 /*
575 * Consume the quiesced txg which has been handed off to
576 * us. This may cause the quiescing thread to now be
577 * able to quiesce another txg, so we must signal it.
578 */
579 ASSERT(tx->tx_quiesced_txg != 0);
580 txg = tx->tx_quiesced_txg;
581 tx->tx_quiesced_txg = 0;
582 tx->tx_syncing_txg = txg;
583 DTRACE_PROBE2(txg__syncing, dsl_pool_t *, dp, uint64_t, txg);
584 cv_broadcast(&tx->tx_quiesce_more_cv);
585
586 dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
587 (u_longlong_t)txg, (u_longlong_t)tx->tx_quiesce_txg_waiting,
588 (u_longlong_t)tx->tx_sync_txg_waiting);
589 mutex_exit(&tx->tx_sync_lock);
590
591 txg_stat_t *ts = spa_txg_history_init_io(spa, txg, dp);
592 start = ddi_get_lbolt();
593 spa_sync(spa, txg);
594 delta = ddi_get_lbolt() - start;
595 spa_txg_history_fini_io(spa, ts);
596
597 mutex_enter(&tx->tx_sync_lock);
598 tx->tx_synced_txg = txg;
599 tx->tx_syncing_txg = 0;
600 DTRACE_PROBE2(txg__synced, dsl_pool_t *, dp, uint64_t, txg);
601 cv_broadcast(&tx->tx_sync_done_cv);
602
603 /*
604 * Dispatch commit callbacks to worker threads.
605 */
606 txg_dispatch_callbacks(dp, txg);
607 }
608 }
609
610 static __attribute__((noreturn)) void
txg_quiesce_thread(void * arg)611 txg_quiesce_thread(void *arg)
612 {
613 dsl_pool_t *dp = arg;
614 tx_state_t *tx = &dp->dp_tx;
615 callb_cpr_t cpr;
616
617 txg_thread_enter(tx, &cpr);
618
619 for (;;) {
620 uint64_t txg;
621
622 /*
623 * We quiesce when there's someone waiting on us.
624 * However, we can only have one txg in "quiescing" or
625 * "quiesced, waiting to sync" state. So we wait until
626 * the "quiesced, waiting to sync" txg has been consumed
627 * by the sync thread.
628 */
629 while (!tx->tx_exiting &&
630 (tx->tx_open_txg >= tx->tx_quiesce_txg_waiting ||
631 txg_has_quiesced_to_sync(dp)))
632 txg_thread_wait(tx, &cpr, &tx->tx_quiesce_more_cv, 0);
633
634 if (tx->tx_exiting)
635 txg_thread_exit(tx, &cpr, &tx->tx_quiesce_thread);
636
637 txg = tx->tx_open_txg;
638 dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
639 (u_longlong_t)txg,
640 (u_longlong_t)tx->tx_quiesce_txg_waiting,
641 (u_longlong_t)tx->tx_sync_txg_waiting);
642 tx->tx_quiescing_txg = txg;
643
644 mutex_exit(&tx->tx_sync_lock);
645 txg_quiesce(dp, txg);
646 mutex_enter(&tx->tx_sync_lock);
647
648 /*
649 * Hand this txg off to the sync thread.
650 */
651 dprintf("quiesce done, handing off txg %llu\n",
652 (u_longlong_t)txg);
653 tx->tx_quiescing_txg = 0;
654 tx->tx_quiesced_txg = txg;
655 DTRACE_PROBE2(txg__quiesced, dsl_pool_t *, dp, uint64_t, txg);
656 cv_broadcast(&tx->tx_sync_more_cv);
657 cv_broadcast(&tx->tx_quiesce_done_cv);
658 }
659 }
660
661 /*
662 * Delay this thread by delay nanoseconds if we are still in the open
663 * transaction group and there is already a waiting txg quiescing or quiesced.
664 * Abort the delay if this txg stalls or enters the quiescing state.
665 */
666 void
txg_delay(dsl_pool_t * dp,uint64_t txg,hrtime_t delay,hrtime_t resolution)667 txg_delay(dsl_pool_t *dp, uint64_t txg, hrtime_t delay, hrtime_t resolution)
668 {
669 tx_state_t *tx = &dp->dp_tx;
670 hrtime_t start = gethrtime();
671
672 /* don't delay if this txg could transition to quiescing immediately */
673 if (tx->tx_open_txg > txg ||
674 tx->tx_syncing_txg == txg-1 || tx->tx_synced_txg == txg-1)
675 return;
676
677 mutex_enter(&tx->tx_sync_lock);
678 if (tx->tx_open_txg > txg || tx->tx_synced_txg == txg-1) {
679 mutex_exit(&tx->tx_sync_lock);
680 return;
681 }
682
683 while (gethrtime() - start < delay &&
684 tx->tx_syncing_txg < txg-1 && !txg_stalled(dp)) {
685 (void) cv_timedwait_hires(&tx->tx_quiesce_more_cv,
686 &tx->tx_sync_lock, delay, resolution, 0);
687 }
688
689 DMU_TX_STAT_BUMP(dmu_tx_delay);
690
691 mutex_exit(&tx->tx_sync_lock);
692 }
693
694 int
txg_wait_synced_flags(dsl_pool_t * dp,uint64_t txg,txg_wait_flag_t flags)695 txg_wait_synced_flags(dsl_pool_t *dp, uint64_t txg, txg_wait_flag_t flags)
696 {
697 int error = 0;
698 tx_state_t *tx = &dp->dp_tx;
699
700 ASSERT0(flags & ~(TXG_WAIT_SIGNAL | TXG_WAIT_SUSPEND));
701 ASSERT(!dsl_pool_config_held(dp));
702
703 mutex_enter(&tx->tx_sync_lock);
704 ASSERT3U(tx->tx_threads, ==, 2);
705 if (txg == 0)
706 txg = tx->tx_open_txg + TXG_DEFER_SIZE;
707 if (tx->tx_sync_txg_waiting < txg)
708 tx->tx_sync_txg_waiting = txg;
709 dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
710 (u_longlong_t)txg, (u_longlong_t)tx->tx_quiesce_txg_waiting,
711 (u_longlong_t)tx->tx_sync_txg_waiting);
712
713 /*
714 * Keep pushing util the pool gets to the wanted txg. If something
715 * else interesting happens, we'll set an error and break out.
716 */
717 while (tx->tx_synced_txg < txg) {
718 if ((flags & TXG_WAIT_SUSPEND) && spa_suspended(dp->dp_spa)) {
719 /*
720 * Pool suspended and the caller does not want to
721 * block; inform them immediately.
722 */
723 error = SET_ERROR(ESHUTDOWN);
724 break;
725 }
726
727 dprintf("broadcasting sync more "
728 "tx_synced=%llu waiting=%llu dp=%px\n",
729 (u_longlong_t)tx->tx_synced_txg,
730 (u_longlong_t)tx->tx_sync_txg_waiting, dp);
731 cv_broadcast(&tx->tx_sync_more_cv);
732
733 if (flags & TXG_WAIT_SIGNAL) {
734 /*
735 * Condition wait here but stop if the thread receives a
736 * signal. The caller may call txg_wait_synced*() again
737 * to resume waiting for this txg.
738 */
739 if (cv_wait_io_sig(&tx->tx_sync_done_cv,
740 &tx->tx_sync_lock) == 0) {
741 error = SET_ERROR(EINTR);
742 break;
743 }
744 } else {
745 /* Uninterruptable wait, until the condvar fires */
746 cv_wait_io(&tx->tx_sync_done_cv, &tx->tx_sync_lock);
747 }
748 }
749
750 mutex_exit(&tx->tx_sync_lock);
751 return (error);
752 }
753
754 void
txg_wait_synced(dsl_pool_t * dp,uint64_t txg)755 txg_wait_synced(dsl_pool_t *dp, uint64_t txg)
756 {
757 VERIFY0(txg_wait_synced_flags(dp, txg, TXG_WAIT_NONE));
758 }
759
760 void
txg_wait_kick(dsl_pool_t * dp)761 txg_wait_kick(dsl_pool_t *dp)
762 {
763 tx_state_t *tx = &dp->dp_tx;
764 mutex_enter(&tx->tx_sync_lock);
765 cv_broadcast(&tx->tx_sync_done_cv);
766 mutex_exit(&tx->tx_sync_lock);
767 }
768
769 /*
770 * Wait for the specified open transaction group. Set should_quiesce
771 * when the current open txg should be quiesced immediately.
772 */
773 void
txg_wait_open(dsl_pool_t * dp,uint64_t txg,boolean_t should_quiesce)774 txg_wait_open(dsl_pool_t *dp, uint64_t txg, boolean_t should_quiesce)
775 {
776 tx_state_t *tx = &dp->dp_tx;
777
778 ASSERT(!dsl_pool_config_held(dp));
779
780 mutex_enter(&tx->tx_sync_lock);
781 ASSERT3U(tx->tx_threads, ==, 2);
782 if (txg == 0)
783 txg = tx->tx_open_txg + 1;
784 if (tx->tx_quiesce_txg_waiting < txg && should_quiesce)
785 tx->tx_quiesce_txg_waiting = txg;
786 dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
787 (u_longlong_t)txg, (u_longlong_t)tx->tx_quiesce_txg_waiting,
788 (u_longlong_t)tx->tx_sync_txg_waiting);
789 while (tx->tx_open_txg < txg) {
790 cv_broadcast(&tx->tx_quiesce_more_cv);
791 /*
792 * Callers setting should_quiesce will use cv_wait_io() and
793 * be accounted for as iowait time. Otherwise, the caller is
794 * understood to be idle and cv_wait_sig() is used to prevent
795 * incorrectly inflating the system load average.
796 */
797 if (should_quiesce == B_TRUE) {
798 cv_wait_io(&tx->tx_quiesce_done_cv, &tx->tx_sync_lock);
799 } else {
800 cv_wait_idle(&tx->tx_quiesce_done_cv,
801 &tx->tx_sync_lock);
802 }
803 }
804 mutex_exit(&tx->tx_sync_lock);
805 }
806
807 /*
808 * Pass in the txg number that should be synced.
809 */
810 void
txg_kick(dsl_pool_t * dp,uint64_t txg)811 txg_kick(dsl_pool_t *dp, uint64_t txg)
812 {
813 tx_state_t *tx = &dp->dp_tx;
814
815 ASSERT(!dsl_pool_config_held(dp));
816
817 if (tx->tx_sync_txg_waiting >= txg)
818 return;
819
820 mutex_enter(&tx->tx_sync_lock);
821 if (tx->tx_sync_txg_waiting < txg) {
822 tx->tx_sync_txg_waiting = txg;
823 cv_broadcast(&tx->tx_sync_more_cv);
824 }
825 mutex_exit(&tx->tx_sync_lock);
826 }
827
828 boolean_t
txg_stalled(dsl_pool_t * dp)829 txg_stalled(dsl_pool_t *dp)
830 {
831 tx_state_t *tx = &dp->dp_tx;
832 return (tx->tx_quiesce_txg_waiting > tx->tx_open_txg);
833 }
834
835 boolean_t
txg_sync_waiting(dsl_pool_t * dp)836 txg_sync_waiting(dsl_pool_t *dp)
837 {
838 tx_state_t *tx = &dp->dp_tx;
839
840 return (tx->tx_syncing_txg <= tx->tx_sync_txg_waiting ||
841 tx->tx_quiesced_txg != 0);
842 }
843
844 /*
845 * Verify that this txg is active (open, quiescing, syncing). Non-active
846 * txg's should not be manipulated.
847 */
848 #ifdef ZFS_DEBUG
849 void
txg_verify(spa_t * spa,uint64_t txg)850 txg_verify(spa_t *spa, uint64_t txg)
851 {
852 dsl_pool_t *dp __maybe_unused = spa_get_dsl(spa);
853 if (txg <= TXG_INITIAL || txg == ZILTEST_TXG)
854 return;
855 ASSERT3U(txg, <=, dp->dp_tx.tx_open_txg);
856 ASSERT3U(txg, >=, dp->dp_tx.tx_synced_txg);
857 ASSERT3U(txg, >=, dp->dp_tx.tx_open_txg - TXG_CONCURRENT_STATES);
858 }
859 #endif
860
861 /*
862 * Per-txg object lists.
863 */
864 void
txg_list_create(txg_list_t * tl,spa_t * spa,size_t offset)865 txg_list_create(txg_list_t *tl, spa_t *spa, size_t offset)
866 {
867 int t;
868
869 mutex_init(&tl->tl_lock, NULL, MUTEX_DEFAULT, NULL);
870
871 tl->tl_offset = offset;
872 tl->tl_spa = spa;
873
874 for (t = 0; t < TXG_SIZE; t++)
875 tl->tl_head[t] = NULL;
876 }
877
878 static boolean_t
txg_list_empty_impl(txg_list_t * tl,uint64_t txg)879 txg_list_empty_impl(txg_list_t *tl, uint64_t txg)
880 {
881 ASSERT(MUTEX_HELD(&tl->tl_lock));
882 TXG_VERIFY(tl->tl_spa, txg);
883 return (tl->tl_head[txg & TXG_MASK] == NULL);
884 }
885
886 boolean_t
txg_list_empty(txg_list_t * tl,uint64_t txg)887 txg_list_empty(txg_list_t *tl, uint64_t txg)
888 {
889 mutex_enter(&tl->tl_lock);
890 boolean_t ret = txg_list_empty_impl(tl, txg);
891 mutex_exit(&tl->tl_lock);
892
893 return (ret);
894 }
895
896 void
txg_list_destroy(txg_list_t * tl)897 txg_list_destroy(txg_list_t *tl)
898 {
899 int t;
900
901 mutex_enter(&tl->tl_lock);
902 for (t = 0; t < TXG_SIZE; t++)
903 ASSERT(txg_list_empty_impl(tl, t));
904 mutex_exit(&tl->tl_lock);
905
906 mutex_destroy(&tl->tl_lock);
907 }
908
909 /*
910 * Returns true if all txg lists are empty.
911 *
912 * Warning: this is inherently racy (an item could be added immediately
913 * after this function returns).
914 */
915 boolean_t
txg_all_lists_empty(txg_list_t * tl)916 txg_all_lists_empty(txg_list_t *tl)
917 {
918 boolean_t res = B_TRUE;
919 for (int i = 0; i < TXG_SIZE; i++)
920 res &= (tl->tl_head[i] == NULL);
921 return (res);
922 }
923
924 /*
925 * Add an entry to the list (unless it's already on the list).
926 * Returns B_TRUE if it was actually added.
927 */
928 boolean_t
txg_list_add(txg_list_t * tl,void * p,uint64_t txg)929 txg_list_add(txg_list_t *tl, void *p, uint64_t txg)
930 {
931 int t = txg & TXG_MASK;
932 txg_node_t *tn = (txg_node_t *)((char *)p + tl->tl_offset);
933 boolean_t add;
934
935 TXG_VERIFY(tl->tl_spa, txg);
936 mutex_enter(&tl->tl_lock);
937 add = (tn->tn_member[t] == 0);
938 if (add) {
939 tn->tn_member[t] = 1;
940 tn->tn_next[t] = tl->tl_head[t];
941 tl->tl_head[t] = tn;
942 }
943 mutex_exit(&tl->tl_lock);
944
945 return (add);
946 }
947
948 /*
949 * Add an entry to the end of the list, unless it's already on the list.
950 * (walks list to find end)
951 * Returns B_TRUE if it was actually added.
952 */
953 boolean_t
txg_list_add_tail(txg_list_t * tl,void * p,uint64_t txg)954 txg_list_add_tail(txg_list_t *tl, void *p, uint64_t txg)
955 {
956 int t = txg & TXG_MASK;
957 txg_node_t *tn = (txg_node_t *)((char *)p + tl->tl_offset);
958 boolean_t add;
959
960 TXG_VERIFY(tl->tl_spa, txg);
961 mutex_enter(&tl->tl_lock);
962 add = (tn->tn_member[t] == 0);
963 if (add) {
964 txg_node_t **tp;
965
966 for (tp = &tl->tl_head[t]; *tp != NULL; tp = &(*tp)->tn_next[t])
967 continue;
968
969 tn->tn_member[t] = 1;
970 tn->tn_next[t] = NULL;
971 *tp = tn;
972 }
973 mutex_exit(&tl->tl_lock);
974
975 return (add);
976 }
977
978 /*
979 * Remove the head of the list and return it.
980 */
981 void *
txg_list_remove(txg_list_t * tl,uint64_t txg)982 txg_list_remove(txg_list_t *tl, uint64_t txg)
983 {
984 int t = txg & TXG_MASK;
985 txg_node_t *tn;
986 void *p = NULL;
987
988 TXG_VERIFY(tl->tl_spa, txg);
989 mutex_enter(&tl->tl_lock);
990 if ((tn = tl->tl_head[t]) != NULL) {
991 ASSERT(tn->tn_member[t]);
992 ASSERT(tn->tn_next[t] == NULL || tn->tn_next[t]->tn_member[t]);
993 p = (char *)tn - tl->tl_offset;
994 tl->tl_head[t] = tn->tn_next[t];
995 tn->tn_next[t] = NULL;
996 tn->tn_member[t] = 0;
997 }
998 mutex_exit(&tl->tl_lock);
999
1000 return (p);
1001 }
1002
1003 /*
1004 * Remove a specific item from the list and return it.
1005 */
1006 void *
txg_list_remove_this(txg_list_t * tl,void * p,uint64_t txg)1007 txg_list_remove_this(txg_list_t *tl, void *p, uint64_t txg)
1008 {
1009 int t = txg & TXG_MASK;
1010 txg_node_t *tn, **tp;
1011
1012 TXG_VERIFY(tl->tl_spa, txg);
1013 mutex_enter(&tl->tl_lock);
1014
1015 for (tp = &tl->tl_head[t]; (tn = *tp) != NULL; tp = &tn->tn_next[t]) {
1016 if ((char *)tn - tl->tl_offset == p) {
1017 *tp = tn->tn_next[t];
1018 tn->tn_next[t] = NULL;
1019 tn->tn_member[t] = 0;
1020 mutex_exit(&tl->tl_lock);
1021 return (p);
1022 }
1023 }
1024
1025 mutex_exit(&tl->tl_lock);
1026
1027 return (NULL);
1028 }
1029
1030 boolean_t
txg_list_member(txg_list_t * tl,void * p,uint64_t txg)1031 txg_list_member(txg_list_t *tl, void *p, uint64_t txg)
1032 {
1033 int t = txg & TXG_MASK;
1034 txg_node_t *tn = (txg_node_t *)((char *)p + tl->tl_offset);
1035
1036 TXG_VERIFY(tl->tl_spa, txg);
1037 return (tn->tn_member[t] != 0);
1038 }
1039
1040 /*
1041 * Walk a txg list
1042 */
1043 void *
txg_list_head(txg_list_t * tl,uint64_t txg)1044 txg_list_head(txg_list_t *tl, uint64_t txg)
1045 {
1046 int t = txg & TXG_MASK;
1047 txg_node_t *tn;
1048
1049 mutex_enter(&tl->tl_lock);
1050 tn = tl->tl_head[t];
1051 mutex_exit(&tl->tl_lock);
1052
1053 TXG_VERIFY(tl->tl_spa, txg);
1054 return (tn == NULL ? NULL : (char *)tn - tl->tl_offset);
1055 }
1056
1057 void *
txg_list_next(txg_list_t * tl,void * p,uint64_t txg)1058 txg_list_next(txg_list_t *tl, void *p, uint64_t txg)
1059 {
1060 int t = txg & TXG_MASK;
1061 txg_node_t *tn = (txg_node_t *)((char *)p + tl->tl_offset);
1062
1063 TXG_VERIFY(tl->tl_spa, txg);
1064
1065 mutex_enter(&tl->tl_lock);
1066 tn = tn->tn_next[t];
1067 mutex_exit(&tl->tl_lock);
1068
1069 return (tn == NULL ? NULL : (char *)tn - tl->tl_offset);
1070 }
1071
1072 EXPORT_SYMBOL(txg_init);
1073 EXPORT_SYMBOL(txg_fini);
1074 EXPORT_SYMBOL(txg_sync_start);
1075 EXPORT_SYMBOL(txg_sync_stop);
1076 EXPORT_SYMBOL(txg_hold_open);
1077 EXPORT_SYMBOL(txg_rele_to_quiesce);
1078 EXPORT_SYMBOL(txg_rele_to_sync);
1079 EXPORT_SYMBOL(txg_register_callbacks);
1080 EXPORT_SYMBOL(txg_delay);
1081 EXPORT_SYMBOL(txg_wait_synced);
1082 EXPORT_SYMBOL(txg_wait_open);
1083 EXPORT_SYMBOL(txg_wait_callbacks);
1084 EXPORT_SYMBOL(txg_stalled);
1085 EXPORT_SYMBOL(txg_sync_waiting);
1086
1087 ZFS_MODULE_PARAM(zfs_txg, zfs_txg_, timeout, UINT, ZMOD_RW,
1088 "Max seconds worth of delta per txg");
1089