xref: /illumos-gate/usr/src/uts/common/fs/zfs/sys/dmu_tx.h (revision 622200ad88c6c6382403a01985a94e22484baac6)
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef	_SYS_DMU_TX_H
27 #define	_SYS_DMU_TX_H
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #include <sys/inttypes.h>
32 #include <sys/dmu.h>
33 #include <sys/txg.h>
34 #include <sys/refcount.h>
35 
36 #ifdef	__cplusplus
37 extern "C" {
38 #endif
39 
40 struct dmu_buf_impl;
41 struct dnode_link;
42 struct dsl_pool;
43 struct dnode;
44 struct dsl_dir;
45 
46 struct dmu_tx {
47 	/*
48 	 * No synchronization is needed because a tx can only be handled
49 	 * by one thread.
50 	 */
51 	list_t tx_holds; /* list of dmu_tx_hold_t */
52 	objset_t *tx_objset;
53 	struct dsl_dir *tx_dir;
54 	struct dsl_pool *tx_pool;
55 	uint64_t tx_txg;
56 	uint64_t tx_lastsnap_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 	int tx_err;
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 struct dmu_tx_hold {
83 	list_node_t dth_node;
84 	struct dnode *dth_dnode;
85 	enum dmu_tx_hold_type dth_type;
86 	uint64_t dth_arg1;
87 	uint64_t dth_arg2;
88 	/* XXX track what the actual estimates were for this hold */
89 } dmu_tx_hold_t;
90 
91 
92 /*
93  * These routines are defined in dmu.h, and are called by the user.
94  */
95 dmu_tx_t *dmu_tx_create(objset_t *dd);
96 int dmu_tx_assign(dmu_tx_t *tx, uint64_t txg_how);
97 void dmu_tx_commit(dmu_tx_t *tx);
98 void dmu_tx_abort(dmu_tx_t *tx);
99 uint64_t dmu_tx_get_txg(dmu_tx_t *tx);
100 
101 /*
102  * These routines are defined in dmu_spa.h, and are called by the SPA.
103  */
104 extern dmu_tx_t *dmu_tx_create_assigned(struct dsl_pool *dp, uint64_t txg);
105 
106 /*
107  * These routines are only called by the DMU.
108  */
109 dmu_tx_t *dmu_tx_create_ds(dsl_dir_t *dd);
110 int dmu_tx_is_syncing(dmu_tx_t *tx);
111 int dmu_tx_private_ok(dmu_tx_t *tx);
112 void dmu_tx_add_new_object(dmu_tx_t *tx, objset_t *os, uint64_t object);
113 void dmu_tx_willuse_space(dmu_tx_t *tx, int64_t delta);
114 void dmu_tx_dirty_buf(dmu_tx_t *tx, struct dmu_buf_impl *db);
115 int dmu_tx_holds(dmu_tx_t *tx, uint64_t object);
116 void dmu_tx_hold_space(dmu_tx_t *tx, uint64_t space);
117 
118 #ifdef ZFS_DEBUG
119 
120 extern int dmu_use_tx_debug_bufs;
121 
122 #define	dprintf_tx(tx, fmt, ...) \
123 	if (dmu_use_tx_debug_bufs) \
124 	do { \
125 	char *__bufp; \
126 	int __len; \
127 	if (tx->tx_debug_buf == NULL) { \
128 		__bufp = kmem_zalloc(4096, KM_SLEEP); \
129 		tx->tx_debug_buf = __bufp; \
130 		tx->tx_debug_len = __len = 4096; \
131 	} else { \
132 		__len = tx->tx_debug_len; \
133 		__bufp = &tx->tx_debug_buf[4096-__len]; \
134 	} \
135 	tx->tx_debug_len -= snprintf(__bufp, __len, fmt, __VA_ARGS__); \
136 _NOTE(CONSTCOND) } while (0); \
137 	else dprintf(fmt, __VA_ARGS__)
138 
139 #define	DMU_TX_DIRTY_BUF(tx, db)	dmu_tx_dirty_buf(tx, db)
140 
141 #else
142 
143 #define	dprintf_tx(tx, fmt, ...)
144 #define	DMU_TX_DIRTY_BUF(tx, db)
145 
146 #endif
147 
148 #ifdef	__cplusplus
149 }
150 #endif
151 
152 #endif	/* _SYS_DMU_TX_H */
153