xref: /illumos-gate/usr/src/uts/common/fs/zfs/sys/dmu_tx.h (revision 43d18f1c320355e93c47399bea0b2e022fe06364)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
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 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef	_SYS_DMU_TX_H
28 #define	_SYS_DMU_TX_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include <sys/inttypes.h>
33 #include <sys/dmu.h>
34 #include <sys/txg.h>
35 #include <sys/refcount.h>
36 
37 #ifdef	__cplusplus
38 extern "C" {
39 #endif
40 
41 struct dmu_buf_impl;
42 struct dnode_link;
43 struct dsl_pool;
44 struct dnode;
45 struct dsl_dir;
46 
47 struct dmu_tx {
48 	/*
49 	 * No synchronization is needed because a tx can only be handled
50 	 * by one thread.
51 	 */
52 	list_t tx_holds; /* list of dmu_tx_hold_t */
53 	objset_t *tx_objset;
54 	struct dsl_dir *tx_dir;
55 	struct dsl_pool *tx_pool;
56 	uint64_t tx_txg;
57 	txg_handle_t tx_txgh;
58 	uint64_t tx_space_towrite;
59 	refcount_t tx_space_written;
60 	uint64_t tx_space_tofree;
61 	refcount_t tx_space_freed;
62 	uint64_t tx_space_tooverwrite;
63 	void *tx_tempreserve_cookie;
64 	uint8_t tx_anyobj;
65 	uint8_t tx_privateobj;
66 #ifdef ZFS_DEBUG
67 	char *tx_debug_buf;
68 	int tx_debug_len;
69 #endif
70 };
71 
72 enum dmu_tx_hold_type {
73 	THT_NEWOBJECT,
74 	THT_WRITE,
75 	THT_BONUS,
76 	THT_FREE,
77 	THT_ZAP,
78 	THT_SPACE,
79 	THT_NUMTYPES
80 };
81 
82 typedef void (*dmu_tx_hold_func_t)(dmu_tx_t *tx, struct dnode *dn,
83     uint64_t arg1, uint64_t arg2);
84 
85 
86 typedef struct dmu_tx_hold {
87 	list_node_t dth_node;
88 	struct dnode *dth_dnode;
89 	enum dmu_tx_hold_type dth_type;
90 	dmu_tx_hold_func_t dth_func;
91 	uint64_t dth_arg1;
92 	uint64_t dth_arg2;
93 	/* XXX track what the actual estimates were for this hold */
94 } dmu_tx_hold_t;
95 
96 
97 /*
98  * These routines are defined in dmu.h, and are called by the user.
99  */
100 dmu_tx_t *dmu_tx_create(objset_t *dd);
101 int dmu_tx_assign(dmu_tx_t *tx, uint64_t txg_how);
102 void dmu_tx_commit(dmu_tx_t *tx);
103 void dmu_tx_abort(dmu_tx_t *tx);
104 uint64_t dmu_tx_get_txg(dmu_tx_t *tx);
105 
106 /*
107  * These routines are defined in dmu_spa.h, and are called by the SPA.
108  */
109 extern dmu_tx_t *dmu_tx_create_assigned(struct dsl_pool *dp, uint64_t txg);
110 
111 /*
112  * These routines are only called by the DMU.
113  */
114 dmu_tx_t *dmu_tx_create_ds(dsl_dir_t *dd);
115 int dmu_tx_is_syncing(dmu_tx_t *tx);
116 int dmu_tx_private_ok(dmu_tx_t *tx);
117 void dmu_tx_add_new_object(dmu_tx_t *tx, objset_t *os, uint64_t object);
118 void dmu_tx_willuse_space(dmu_tx_t *tx, int64_t delta);
119 void dmu_tx_dirty_buf(dmu_tx_t *tx, struct dmu_buf_impl *db);
120 int dmu_tx_holds(dmu_tx_t *tx, uint64_t object);
121 void dmu_tx_hold_space(dmu_tx_t *tx, uint64_t space);
122 
123 #ifdef ZFS_DEBUG
124 
125 extern int dmu_use_tx_debug_bufs;
126 
127 #define	dprintf_tx(tx, fmt, ...) \
128 	if (dmu_use_tx_debug_bufs) \
129 	do { \
130 	char *__bufp; \
131 	int __len; \
132 	if (tx->tx_debug_buf == NULL) { \
133 		__bufp = kmem_zalloc(4096, KM_SLEEP); \
134 		tx->tx_debug_buf = __bufp; \
135 		tx->tx_debug_len = __len = 4096; \
136 	} else { \
137 		__len = tx->tx_debug_len; \
138 		__bufp = &tx->tx_debug_buf[4096-__len]; \
139 	} \
140 	tx->tx_debug_len -= snprintf(__bufp, __len, fmt, __VA_ARGS__); \
141 _NOTE(CONSTCOND) } while (0); \
142 	else dprintf(fmt, __VA_ARGS__)
143 
144 #define	DMU_TX_DIRTY_BUF(tx, db)	dmu_tx_dirty_buf(tx, db)
145 
146 #else
147 
148 #define	dprintf_tx(tx, fmt, ...)
149 #define	DMU_TX_DIRTY_BUF(tx, db)
150 
151 #endif
152 
153 #ifdef	__cplusplus
154 }
155 #endif
156 
157 #endif	/* _SYS_DMU_TX_H */
158