xref: /freebsd/sys/contrib/openzfs/include/sys/txg.h (revision b1c1ee4429fcca8f69873a8be66184e68e1b19d7)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 /*
27  * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
28  * Copyright (c) 2025, Klara, Inc.
29  */
30 
31 #ifndef _SYS_TXG_H
32 #define	_SYS_TXG_H
33 
34 #include <sys/spa.h>
35 #include <sys/zfs_context.h>
36 
37 #ifdef	__cplusplus
38 extern "C" {
39 #endif
40 
41 #define	TXG_CONCURRENT_STATES	3	/* open, quiescing, syncing	*/
42 #define	TXG_SIZE		4		/* next power of 2	*/
43 #define	TXG_MASK		(TXG_SIZE - 1)	/* mask for size	*/
44 #define	TXG_INITIAL		TXG_SIZE	/* initial txg 		*/
45 #define	TXG_IDX			(txg & TXG_MASK)
46 #define	TXG_UNKNOWN		0
47 
48 /* Number of txgs worth of frees we defer adding to in-core spacemaps */
49 #define	TXG_DEFER_SIZE		2
50 
51 typedef struct tx_cpu tx_cpu_t;
52 
53 typedef struct txg_handle {
54 	tx_cpu_t	*th_cpu;
55 	uint64_t	th_txg;
56 } txg_handle_t;
57 
58 typedef struct txg_node {
59 	struct txg_node	*tn_next[TXG_SIZE];
60 	uint8_t		tn_member[TXG_SIZE];
61 } txg_node_t;
62 
63 typedef struct txg_list {
64 	kmutex_t	tl_lock;
65 	size_t		tl_offset;
66 	spa_t		*tl_spa;
67 	txg_node_t	*tl_head[TXG_SIZE];
68 } txg_list_t;
69 
70 /*
71  * Wait flags for txg_wait_synced_flags(). By default (TXG_WAIT_NONE), it will
72  * wait until the wanted txg is reached, or block forever. Additional flags
73  * indicate other conditions that the caller is interested in, that will cause
74  * the wait to break and return an error code describing the condition.
75  */
76 typedef enum {
77 	/* No special flags. Guaranteed to block forever or return 0 */
78 	TXG_WAIT_NONE   = 0,
79 
80 	/* If a signal arrives while waiting, abort and return EINTR */
81 	TXG_WAIT_SIGNAL = (1 << 0),
82 
83 	/* If the pool suspends while waiting, abort and return ESHUTDOWN. */
84 	TXG_WAIT_SUSPEND = (1 << 1),
85 } txg_wait_flag_t;
86 
87 struct dsl_pool;
88 
89 extern void txg_init(struct dsl_pool *dp, uint64_t txg);
90 extern void txg_fini(struct dsl_pool *dp);
91 extern void txg_sync_start(struct dsl_pool *dp);
92 extern void txg_sync_stop(struct dsl_pool *dp);
93 extern uint64_t txg_hold_open(struct dsl_pool *dp, txg_handle_t *txghp);
94 extern void txg_rele_to_quiesce(txg_handle_t *txghp);
95 extern void txg_rele_to_sync(txg_handle_t *txghp);
96 extern void txg_register_callbacks(txg_handle_t *txghp, list_t *tx_callbacks);
97 
98 extern void txg_delay(struct dsl_pool *dp, uint64_t txg, hrtime_t delta,
99     hrtime_t resolution);
100 extern void txg_kick(struct dsl_pool *dp, uint64_t txg);
101 
102 /*
103  * Wait until the given transaction group has finished syncing.
104  * Try to make this happen as soon as possible (eg. kick off any
105  * necessary syncs immediately).  If txg==0, wait for the currently open
106  * txg to finish syncing.
107  * See txg_wait_flag_t above for a description of how the flags affect the wait.
108  */
109 extern int txg_wait_synced_flags(struct dsl_pool *dp, uint64_t txg,
110     txg_wait_flag_t flags);
111 
112 /*
113  * Traditional form of txg_wait_synced_flags, waits forever.
114  * Shorthand for VERIFY0(txg_wait_synced_flags(dp, TXG_WAIT_NONE))
115  */
116 extern void txg_wait_synced(struct dsl_pool *dp, uint64_t txg);
117 
118 /*
119  * Wake all threads waiting in txg_wait_synced_flags() so they can reevaluate.
120  */
121 extern void txg_wait_kick(struct dsl_pool *dp);
122 
123 /*
124  * Wait until the given transaction group, or one after it, is
125  * the open transaction group.  Try to make this happen as soon
126  * as possible (eg. kick off any necessary syncs immediately) when
127  * should_quiesce is set.  If txg == 0, wait for the next open txg.
128  */
129 extern void txg_wait_open(struct dsl_pool *dp, uint64_t txg,
130     boolean_t should_quiesce);
131 
132 /*
133  * Returns TRUE if we are "backed up" waiting for the syncing
134  * transaction to complete; otherwise returns FALSE.
135  */
136 extern boolean_t txg_stalled(struct dsl_pool *dp);
137 
138 /* returns TRUE if someone is waiting for the next txg to sync */
139 extern boolean_t txg_sync_waiting(struct dsl_pool *dp);
140 
141 extern void txg_verify(spa_t *spa, uint64_t txg);
142 
143 /*
144  * Wait for pending commit callbacks of already-synced transactions to finish
145  * processing.
146  */
147 extern void txg_wait_callbacks(struct dsl_pool *dp);
148 
149 /*
150  * Per-txg object lists.
151  */
152 
153 #define	TXG_CLEAN(txg)	((txg) - 1)
154 
155 extern void txg_list_create(txg_list_t *tl, spa_t *spa, size_t offset);
156 extern void txg_list_destroy(txg_list_t *tl);
157 extern boolean_t txg_list_empty(txg_list_t *tl, uint64_t txg);
158 extern boolean_t txg_all_lists_empty(txg_list_t *tl);
159 extern boolean_t txg_list_add(txg_list_t *tl, void *p, uint64_t txg);
160 extern boolean_t txg_list_add_tail(txg_list_t *tl, void *p, uint64_t txg);
161 extern void *txg_list_remove(txg_list_t *tl, uint64_t txg);
162 extern void *txg_list_remove_this(txg_list_t *tl, void *p, uint64_t txg);
163 extern boolean_t txg_list_member(txg_list_t *tl, void *p, uint64_t txg);
164 extern void *txg_list_head(txg_list_t *tl, uint64_t txg);
165 extern void *txg_list_next(txg_list_t *tl, void *p, uint64_t txg);
166 
167 /* Global tuning */
168 extern uint_t zfs_txg_timeout;
169 
170 
171 #ifdef ZFS_DEBUG
172 #define	TXG_VERIFY(spa, txg)		txg_verify(spa, txg)
173 #else
174 #define	TXG_VERIFY(spa, txg)
175 #endif
176 
177 #ifdef	__cplusplus
178 }
179 #endif
180 
181 #endif	/* _SYS_TXG_H */
182