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 2010 Sun Microsystems, Inc.  All rights reserved.
23 * Use is subject to license terms.
24 */
25/*
26 * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
27 */
28
29#ifndef _SYS_TXG_H
30#define	_SYS_TXG_H
31
32#include <sys/spa.h>
33#include <sys/zfs_context.h>
34
35#ifdef	__cplusplus
36extern "C" {
37#endif
38
39#define	TXG_CONCURRENT_STATES	3	/* open, quiescing, syncing	*/
40#define	TXG_SIZE		4		/* next power of 2	*/
41#define	TXG_MASK		(TXG_SIZE - 1)	/* mask for size	*/
42#define	TXG_INITIAL		TXG_SIZE	/* initial txg		*/
43#define	TXG_IDX			(txg & TXG_MASK)
44
45/* Number of txgs worth of frees we defer adding to in-core spacemaps */
46#define	TXG_DEFER_SIZE		2
47
48typedef struct tx_cpu tx_cpu_t;
49
50typedef struct txg_handle {
51	tx_cpu_t	*th_cpu;
52	uint64_t	th_txg;
53} txg_handle_t;
54
55typedef struct txg_node {
56	struct txg_node	*tn_next[TXG_SIZE];
57	uint8_t		tn_member[TXG_SIZE];
58} txg_node_t;
59
60typedef struct txg_list {
61	kmutex_t	tl_lock;
62	size_t		tl_offset;
63	spa_t		*tl_spa;
64	txg_node_t	*tl_head[TXG_SIZE];
65} txg_list_t;
66
67struct dsl_pool;
68
69extern void txg_init(struct dsl_pool *dp, uint64_t txg);
70extern void txg_fini(struct dsl_pool *dp);
71extern void txg_sync_start(struct dsl_pool *dp);
72extern void txg_sync_stop(struct dsl_pool *dp);
73extern uint64_t txg_hold_open(struct dsl_pool *dp, txg_handle_t *txghp);
74extern void txg_rele_to_quiesce(txg_handle_t *txghp);
75extern void txg_rele_to_sync(txg_handle_t *txghp);
76extern void txg_register_callbacks(txg_handle_t *txghp, list_t *tx_callbacks);
77
78extern void txg_delay(struct dsl_pool *dp, uint64_t txg, hrtime_t delta,
79    hrtime_t resolution);
80extern void txg_kick(struct dsl_pool *dp);
81
82/*
83 * Wait until the given transaction group has finished syncing.
84 * Try to make this happen as soon as possible (eg. kick off any
85 * necessary syncs immediately).  If txg==0, wait for the currently open
86 * txg to finish syncing.
87 */
88extern void txg_wait_synced(struct dsl_pool *dp, uint64_t txg);
89
90/*
91 * Wait as above. Returns true if the thread was signaled while waiting.
92 */
93extern boolean_t txg_wait_synced_sig(struct dsl_pool *dp, uint64_t txg);
94
95/*
96 * Wait until the given transaction group, or one after it, is
97 * the open transaction group.  Try to make this happen as soon
98 * as possible (eg. kick off any necessary syncs immediately) when
99 * should_quiesce is set.  If txg == 0, wait for the next open txg.
100 */
101extern void txg_wait_open(struct dsl_pool *dp, uint64_t txg,
102    boolean_t should_quiesce);
103
104/*
105 * Returns TRUE if we are "backed up" waiting for the syncing
106 * transaction to complete; otherwise returns FALSE.
107 */
108extern boolean_t txg_stalled(struct dsl_pool *dp);
109
110/* returns TRUE if someone is waiting for the next txg to sync */
111extern boolean_t txg_sync_waiting(struct dsl_pool *dp);
112
113extern void txg_verify(spa_t *spa, uint64_t txg);
114
115/*
116 * Per-txg object lists.
117 */
118
119#define	TXG_CLEAN(txg)	((txg) - 1)
120
121extern void txg_list_create(txg_list_t *tl, spa_t *spa, size_t offset);
122extern void txg_list_destroy(txg_list_t *tl);
123extern boolean_t txg_list_empty(txg_list_t *tl, uint64_t txg);
124extern boolean_t txg_all_lists_empty(txg_list_t *tl);
125extern boolean_t txg_list_add(txg_list_t *tl, void *p, uint64_t txg);
126extern boolean_t txg_list_add_tail(txg_list_t *tl, void *p, uint64_t txg);
127extern void *txg_list_remove(txg_list_t *tl, uint64_t txg);
128extern void *txg_list_remove_this(txg_list_t *tl, void *p, uint64_t txg);
129extern boolean_t txg_list_member(txg_list_t *tl, void *p, uint64_t txg);
130extern void *txg_list_head(txg_list_t *tl, uint64_t txg);
131extern void *txg_list_next(txg_list_t *tl, void *p, uint64_t txg);
132
133#ifdef	__cplusplus
134}
135#endif
136
137#endif	/* _SYS_TXG_H */
138