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