xref: /illumos-gate/usr/src/uts/common/fs/zfs/sys/txg.h (revision 084fd14f)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
51ab7f2deSmaybee  * Common Development and Distribution License (the "License").
61ab7f2deSmaybee  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
22495807d7SMatthew Ahrens  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
25ce636f8bSMatthew Ahrens /*
26b7b2590dSMatthew Ahrens  * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
27ce636f8bSMatthew Ahrens  */
28fa9e4066Sahrens 
29fa9e4066Sahrens #ifndef _SYS_TXG_H
30fa9e4066Sahrens #define	_SYS_TXG_H
31fa9e4066Sahrens 
32fa9e4066Sahrens #include <sys/spa.h>
33fa9e4066Sahrens #include <sys/zfs_context.h>
34fa9e4066Sahrens 
35fa9e4066Sahrens #ifdef	__cplusplus
36fa9e4066Sahrens extern "C" {
37fa9e4066Sahrens #endif
38fa9e4066Sahrens 
39fa9e4066Sahrens #define	TXG_CONCURRENT_STATES	3	/* open, quiescing, syncing	*/
40fa9e4066Sahrens #define	TXG_SIZE		4		/* next power of 2	*/
41fa9e4066Sahrens #define	TXG_MASK		(TXG_SIZE - 1)	/* mask for size	*/
42*084fd14fSBrian Behlendorf #define	TXG_INITIAL		TXG_SIZE	/* initial txg		*/
43fa9e4066Sahrens #define	TXG_IDX			(txg & TXG_MASK)
44fa9e4066Sahrens 
45468c413aSTim Haley /* Number of txgs worth of frees we defer adding to in-core spacemaps */
46468c413aSTim Haley #define	TXG_DEFER_SIZE		2
47468c413aSTim Haley 
48fa9e4066Sahrens typedef struct tx_cpu tx_cpu_t;
49fa9e4066Sahrens 
50fa9e4066Sahrens typedef struct txg_handle {
51fa9e4066Sahrens 	tx_cpu_t	*th_cpu;
52fa9e4066Sahrens 	uint64_t	th_txg;
53fa9e4066Sahrens } txg_handle_t;
54fa9e4066Sahrens 
55fa9e4066Sahrens typedef struct txg_node {
56fa9e4066Sahrens 	struct txg_node	*tn_next[TXG_SIZE];
57fa9e4066Sahrens 	uint8_t		tn_member[TXG_SIZE];
58fa9e4066Sahrens } txg_node_t;
59fa9e4066Sahrens 
60fa9e4066Sahrens typedef struct txg_list {
61fa9e4066Sahrens 	kmutex_t	tl_lock;
62fa9e4066Sahrens 	size_t		tl_offset;
63b7b2590dSMatthew Ahrens 	spa_t		*tl_spa;
64fa9e4066Sahrens 	txg_node_t	*tl_head[TXG_SIZE];
65fa9e4066Sahrens } txg_list_t;
66fa9e4066Sahrens 
67fa9e4066Sahrens struct dsl_pool;
68fa9e4066Sahrens 
69fa9e4066Sahrens extern void txg_init(struct dsl_pool *dp, uint64_t txg);
70fa9e4066Sahrens extern void txg_fini(struct dsl_pool *dp);
71fa9e4066Sahrens extern void txg_sync_start(struct dsl_pool *dp);
72fa9e4066Sahrens extern void txg_sync_stop(struct dsl_pool *dp);
73fa9e4066Sahrens extern uint64_t txg_hold_open(struct dsl_pool *dp, txg_handle_t *txghp);
74fa9e4066Sahrens extern void txg_rele_to_quiesce(txg_handle_t *txghp);
75fa9e4066Sahrens extern void txg_rele_to_sync(txg_handle_t *txghp);
76d20e665cSRicardo M. Correia extern void txg_register_callbacks(txg_handle_t *txghp, list_t *tx_callbacks);
77fa9e4066Sahrens 
780689f76cSAdam Leventhal extern void txg_delay(struct dsl_pool *dp, uint64_t txg, hrtime_t delta,
790689f76cSAdam Leventhal     hrtime_t resolution);
8069962b56SMatthew Ahrens extern void txg_kick(struct dsl_pool *dp);
811ab7f2deSmaybee 
82fa9e4066Sahrens /*
83fa9e4066Sahrens  * Wait until the given transaction group has finished syncing.
84fa9e4066Sahrens  * Try to make this happen as soon as possible (eg. kick off any
85fa9e4066Sahrens  * necessary syncs immediately).  If txg==0, wait for the currently open
86fa9e4066Sahrens  * txg to finish syncing.
87fa9e4066Sahrens  */
88fa9e4066Sahrens extern void txg_wait_synced(struct dsl_pool *dp, uint64_t txg);
89fa9e4066Sahrens 
90d0cb1fb9SDon Brady /*
91d0cb1fb9SDon Brady  * Wait as above. Returns true if the thread was signaled while waiting.
92d0cb1fb9SDon Brady  */
93d0cb1fb9SDon Brady extern boolean_t txg_wait_synced_sig(struct dsl_pool *dp, uint64_t txg);
94d0cb1fb9SDon Brady 
95fa9e4066Sahrens /*
96fa9e4066Sahrens  * Wait until the given transaction group, or one after it, is
97fa9e4066Sahrens  * the open transaction group.  Try to make this happen as soon
98*084fd14fSBrian Behlendorf  * as possible (eg. kick off any necessary syncs immediately) when
99*084fd14fSBrian Behlendorf  * should_quiesce is set.  If txg == 0, wait for the next open txg.
100fa9e4066Sahrens  */
101*084fd14fSBrian Behlendorf extern void txg_wait_open(struct dsl_pool *dp, uint64_t txg,
102*084fd14fSBrian Behlendorf     boolean_t should_quiesce);
103fa9e4066Sahrens 
104fa9e4066Sahrens /*
105fa9e4066Sahrens  * Returns TRUE if we are "backed up" waiting for the syncing
106fa9e4066Sahrens  * transaction to complete; otherwise returns FALSE.
107fa9e4066Sahrens  */
108088f3894Sahrens extern boolean_t txg_stalled(struct dsl_pool *dp);
109088f3894Sahrens 
110088f3894Sahrens /* returns TRUE if someone is waiting for the next txg to sync */
111088f3894Sahrens extern boolean_t txg_sync_waiting(struct dsl_pool *dp);
112fa9e4066Sahrens 
113b7b2590dSMatthew Ahrens extern void txg_verify(spa_t *spa, uint64_t txg);
114b7b2590dSMatthew Ahrens 
115fa9e4066Sahrens /*
116fa9e4066Sahrens  * Per-txg object lists.
117fa9e4066Sahrens  */
118fa9e4066Sahrens 
119fa9e4066Sahrens #define	TXG_CLEAN(txg)	((txg) - 1)
120fa9e4066Sahrens 
121b7b2590dSMatthew Ahrens extern void txg_list_create(txg_list_t *tl, spa_t *spa, size_t offset);
122fa9e4066Sahrens extern void txg_list_destroy(txg_list_t *tl);
123ce636f8bSMatthew Ahrens extern boolean_t txg_list_empty(txg_list_t *tl, uint64_t txg);
12473527f44SAlex Reece extern boolean_t txg_all_lists_empty(txg_list_t *tl);
1253b2aab18SMatthew Ahrens extern boolean_t txg_list_add(txg_list_t *tl, void *p, uint64_t txg);
1263b2aab18SMatthew Ahrens extern boolean_t txg_list_add_tail(txg_list_t *tl, void *p, uint64_t txg);
127fa9e4066Sahrens extern void *txg_list_remove(txg_list_t *tl, uint64_t txg);
128fa9e4066Sahrens extern void *txg_list_remove_this(txg_list_t *tl, void *p, uint64_t txg);
1293b2aab18SMatthew Ahrens extern boolean_t txg_list_member(txg_list_t *tl, void *p, uint64_t txg);
130fa9e4066Sahrens extern void *txg_list_head(txg_list_t *tl, uint64_t txg);
131fa9e4066Sahrens extern void *txg_list_next(txg_list_t *tl, void *p, uint64_t txg);
132fa9e4066Sahrens 
133fa9e4066Sahrens #ifdef	__cplusplus
134fa9e4066Sahrens }
135fa9e4066Sahrens #endif
136fa9e4066Sahrens 
137fa9e4066Sahrens #endif	/* _SYS_TXG_H */
138