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