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