xref: /freebsd/sys/contrib/openzfs/include/sys/dmu_tx.h (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
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, 2016 by Delphix. All rights reserved.
28  */
29 
30 #ifndef	_SYS_DMU_TX_H
31 #define	_SYS_DMU_TX_H
32 
33 #include <sys/inttypes.h>
34 #include <sys/dmu.h>
35 #include <sys/txg.h>
36 #include <sys/zfs_refcount.h>
37 
38 #ifdef	__cplusplus
39 extern "C" {
40 #endif
41 
42 struct dmu_buf_impl;
43 struct dmu_tx_hold;
44 struct dnode_link;
45 struct dsl_pool;
46 struct dnode;
47 struct dsl_dir;
48 
49 struct dmu_tx {
50 	/*
51 	 * No synchronization is needed because a tx can only be handled
52 	 * by one thread.
53 	 */
54 	list_t tx_holds; /* list of dmu_tx_hold_t */
55 	objset_t *tx_objset;
56 	struct dsl_dir *tx_dir;
57 	struct dsl_pool *tx_pool;
58 	uint64_t tx_txg;
59 	uint64_t tx_lastsnap_txg;
60 	uint64_t tx_lasttried_txg;
61 	txg_handle_t tx_txgh;
62 	void *tx_tempreserve_cookie;
63 	struct dmu_tx_hold *tx_needassign_txh;
64 
65 	/* list of dmu_tx_callback_t on this dmu_tx */
66 	list_t tx_callbacks;
67 
68 	/* placeholder for syncing context, doesn't need specific holds */
69 	boolean_t tx_anyobj;
70 
71 	/* transaction is marked as being a "net free" of space */
72 	boolean_t tx_netfree;
73 
74 	/* time this transaction was created */
75 	hrtime_t tx_start;
76 
77 	/* need to wait for sufficient dirty space */
78 	boolean_t tx_wait_dirty;
79 
80 	/* has this transaction already been delayed? */
81 	boolean_t tx_dirty_delayed;
82 
83 	int tx_err;
84 };
85 
86 enum dmu_tx_hold_type {
87 	THT_NEWOBJECT,
88 	THT_WRITE,
89 	THT_BONUS,
90 	THT_FREE,
91 	THT_ZAP,
92 	THT_SPACE,
93 	THT_SPILL,
94 	THT_CLONE,
95 	THT_APPEND,
96 	THT_NUMTYPES
97 };
98 
99 typedef struct dmu_tx_hold {
100 	dmu_tx_t *txh_tx;
101 	list_node_t txh_node;
102 	struct dnode *txh_dnode;
103 	zfs_refcount_t txh_space_towrite;
104 	zfs_refcount_t txh_memory_tohold;
105 	enum dmu_tx_hold_type txh_type;
106 	uint64_t txh_arg1;
107 	uint64_t txh_arg2;
108 } dmu_tx_hold_t;
109 
110 typedef struct dmu_tx_callback {
111 	list_node_t		dcb_node;    /* linked to tx_callbacks list */
112 	dmu_tx_callback_func_t	*dcb_func;   /* caller function pointer */
113 	void			*dcb_data;   /* caller private data */
114 } dmu_tx_callback_t;
115 
116 /*
117  * Used for dmu tx kstat.
118  */
119 typedef struct dmu_tx_stats {
120 	kstat_named_t dmu_tx_assigned;
121 	kstat_named_t dmu_tx_delay;
122 	kstat_named_t dmu_tx_error;
123 	kstat_named_t dmu_tx_suspended;
124 	kstat_named_t dmu_tx_group;
125 	kstat_named_t dmu_tx_memory_reserve;
126 	kstat_named_t dmu_tx_memory_reclaim;
127 	kstat_named_t dmu_tx_dirty_throttle;
128 	kstat_named_t dmu_tx_dirty_delay;
129 	kstat_named_t dmu_tx_dirty_over_max;
130 	kstat_named_t dmu_tx_dirty_frees_delay;
131 	kstat_named_t dmu_tx_wrlog_delay;
132 	kstat_named_t dmu_tx_quota;
133 } dmu_tx_stats_t;
134 
135 extern dmu_tx_stats_t dmu_tx_stats;
136 
137 #define	DMU_TX_STAT_INCR(stat, val) \
138     atomic_add_64(&dmu_tx_stats.stat.value.ui64, (val));
139 #define	DMU_TX_STAT_BUMP(stat) \
140     DMU_TX_STAT_INCR(stat, 1);
141 
142 /*
143  * These routines are defined in dmu.h, and are called by the user.
144  */
145 dmu_tx_t *dmu_tx_create(objset_t *dd);
146 int dmu_tx_assign(dmu_tx_t *tx, uint64_t txg_how);
147 void dmu_tx_commit(dmu_tx_t *tx);
148 void dmu_tx_abort(dmu_tx_t *tx);
149 uint64_t dmu_tx_get_txg(dmu_tx_t *tx);
150 struct dsl_pool *dmu_tx_pool(dmu_tx_t *tx);
151 void dmu_tx_wait(dmu_tx_t *tx);
152 
153 /*
154  * These routines are defined in dmu_spa.h, and are called by the SPA.
155  */
156 extern dmu_tx_t *dmu_tx_create_assigned(struct dsl_pool *dp, uint64_t txg);
157 
158 /*
159  * These routines are only called by the DMU.
160  */
161 dmu_tx_t *dmu_tx_create_dd(dsl_dir_t *dd);
162 int dmu_tx_is_syncing(dmu_tx_t *tx);
163 int dmu_tx_private_ok(dmu_tx_t *tx);
164 void dmu_tx_add_new_object(dmu_tx_t *tx, dnode_t *dn);
165 void dmu_tx_dirty_buf(dmu_tx_t *tx, struct dmu_buf_impl *db);
166 void dmu_tx_hold_space(dmu_tx_t *tx, uint64_t space);
167 
168 #ifdef ZFS_DEBUG
169 #define	DMU_TX_DIRTY_BUF(tx, db)	dmu_tx_dirty_buf(tx, db)
170 #else
171 #define	DMU_TX_DIRTY_BUF(tx, db)
172 #endif
173 
174 void dmu_tx_init(void);
175 void dmu_tx_fini(void);
176 
177 #ifdef	__cplusplus
178 }
179 #endif
180 
181 #endif	/* _SYS_DMU_TX_H */
182