xref: /titanic_44/usr/src/uts/common/fs/zfs/sys/dbuf.h (revision 06e1a7147edd272b7296f208141627a5b1191731)
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_DBUF_H
27 #define	_SYS_DBUF_H
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #include <sys/dmu.h>
32 #include <sys/spa.h>
33 #include <sys/txg.h>
34 #include <sys/zio.h>
35 #include <sys/arc.h>
36 #include <sys/zfs_context.h>
37 #include <sys/refcount.h>
38 
39 #ifdef	__cplusplus
40 extern "C" {
41 #endif
42 
43 #define	DB_BONUS_BLKID (-1ULL)
44 #define	IN_DMU_SYNC ((blkptr_t *)-1)
45 
46 /*
47  * define flags for dbuf_read
48  */
49 
50 #define	DB_RF_MUST_SUCCEED	(1 << 0)
51 #define	DB_RF_CANFAIL		(1 << 1)
52 #define	DB_RF_HAVESTRUCT	(1 << 2)
53 #define	DB_RF_NOPREFETCH	(1 << 3)
54 #define	DB_RF_NEVERWAIT		(1 << 4)
55 #define	DB_RF_CACHED		(1 << 5)
56 
57 /*
58  * The state transition diagram for dbufs looks like:
59  *
60  *		+----> READ ----+
61  *		|		|
62  *		|		V
63  *  (alloc)-->UNCACHED	     CACHED-->EVICTING-->(free)
64  *		|		^
65  *		|		|
66  *		+----> FILL ----+
67  */
68 typedef enum dbuf_states {
69 	DB_UNCACHED,
70 	DB_FILL,
71 	DB_READ,
72 	DB_CACHED,
73 	DB_EVICTING
74 } dbuf_states_t;
75 
76 struct objset_impl;
77 struct dnode;
78 struct dmu_tx;
79 
80 /*
81  * level = 0 means the user data
82  * level = 1 means the single indirect block
83  * etc.
84  */
85 
86 #define	LIST_LINK_INACTIVE(link) \
87 	((link)->list_next == NULL && (link)->list_prev == NULL)
88 
89 typedef struct dmu_buf_impl {
90 	/*
91 	 * The following members are immutable, with the exception of
92 	 * db.db_data, which is protected by db_mtx.
93 	 */
94 
95 	/* the publicly visible structure */
96 	dmu_buf_t db;
97 
98 	/* the objset we belong to */
99 	struct objset_impl *db_objset;
100 
101 	/*
102 	 * the dnode we belong to (NULL when evicted)
103 	 */
104 	struct dnode *db_dnode;
105 
106 	/*
107 	 * our parent buffer; if the dnode points to us directly,
108 	 * db_parent == db_dnode->dn_dbuf
109 	 * only accessed by sync thread ???
110 	 * (NULL when evicted)
111 	 */
112 	struct dmu_buf_impl *db_parent;
113 
114 	/*
115 	 * link for hash table of all dmu_buf_impl_t's
116 	 */
117 	struct dmu_buf_impl *db_hash_next;
118 
119 	/* our block number */
120 	uint64_t db_blkid;
121 
122 	/*
123 	 * Pointer to the blkptr_t which points to us. May be NULL if we
124 	 * don't have one yet. (NULL when evicted)
125 	 */
126 	blkptr_t *db_blkptr;
127 
128 	/*
129 	 * Our indirection level.  Data buffers have db_level==0.
130 	 * Indirect buffers which point to data buffers have
131 	 * db_level==1. etc.  Buffers which contain dnodes have
132 	 * db_level==0, since the dnodes are stored in a file.
133 	 */
134 	uint8_t db_level;
135 
136 	/* db_mtx protects the members below */
137 	kmutex_t db_mtx;
138 
139 	/*
140 	 * Current state of the buffer
141 	 */
142 	dbuf_states_t db_state;
143 
144 	/*
145 	 * Refcount accessed by dmu_buf_{hold,rele}.
146 	 * If nonzero, the buffer can't be destroyed.
147 	 * Protected by db_mtx.
148 	 */
149 	refcount_t db_holds;
150 
151 	/* buffer holding our data */
152 	arc_buf_t *db_buf;
153 
154 	kcondvar_t db_changed;
155 	arc_buf_t *db_data_pending;
156 
157 	/*
158 	 * Last time (transaction group) this buffer was dirtied.
159 	 */
160 	uint64_t db_dirtied;
161 
162 	/*
163 	 * If db_dnode != NULL, our link on the owner dnodes's dn_dbufs list.
164 	 * Protected by its dn_dbufs_mtx.
165 	 */
166 	list_node_t db_link;
167 
168 	/* Our link on dn_dirty_dbufs[txg] */
169 	list_node_t db_dirty_node[TXG_SIZE];
170 	uint8_t db_dirtycnt;
171 
172 	/*
173 	 * Data which is unique to data (leaf) blocks:
174 	 */
175 	struct {
176 		/* stuff we store for the user (see dmu_buf_set_user) */
177 		void *db_user_ptr;
178 		void **db_user_data_ptr_ptr;
179 		dmu_buf_evict_func_t *db_evict_func;
180 		uint8_t db_immediate_evict;
181 		uint8_t db_freed_in_flight;
182 
183 		/*
184 		 * db_data_old[txg&TXG_MASK] is set when we
185 		 * dirty the buffer, so that we can retain the
186 		 * pointer even if it gets COW'd in a subsequent
187 		 * transaction group.
188 		 *
189 		 * If the buffer is dirty in any txg, it can't
190 		 * be destroyed.
191 		 */
192 		/*
193 		 * XXX Protected by db_mtx and dn_dirty_mtx.
194 		 * db_mtx must be held to read db_dirty[], and
195 		 * both db_mtx and dn_dirty_mtx must be held to
196 		 * modify (dirty or clean). db_mtx must be held
197 		 * before dn_dirty_mtx.
198 		 */
199 		void *db_data_old[TXG_SIZE];
200 		blkptr_t *db_overridden_by[TXG_SIZE];
201 	} db_d;
202 } dmu_buf_impl_t;
203 
204 /* Note: the dbuf hash table is exposed only for the mdb module */
205 #define	DBUF_MUTEXES 256
206 #define	DBUF_HASH_MUTEX(h, idx) (&(h)->hash_mutexes[(idx) & (DBUF_MUTEXES-1)])
207 typedef struct dbuf_hash_table {
208 	uint64_t hash_table_mask;
209 	dmu_buf_impl_t **hash_table;
210 	kmutex_t hash_mutexes[DBUF_MUTEXES];
211 } dbuf_hash_table_t;
212 
213 
214 uint64_t dbuf_whichblock(struct dnode *di, uint64_t offset);
215 
216 dmu_buf_impl_t *dbuf_create_tlib(struct dnode *dn, char *data);
217 dmu_buf_impl_t *dbuf_create_bonus(struct dnode *dn);
218 
219 dmu_buf_impl_t *dbuf_hold(struct dnode *dn, uint64_t blkid, void *tag);
220 dmu_buf_impl_t *dbuf_hold_level(struct dnode *dn, int level, uint64_t blkid,
221     void *tag);
222 int dbuf_hold_impl(struct dnode *dn, uint8_t level, uint64_t blkid, int create,
223     void *tag, dmu_buf_impl_t **dbp);
224 
225 void dbuf_prefetch(struct dnode *dn, uint64_t blkid);
226 
227 void dbuf_add_ref(dmu_buf_impl_t *db, void *tag);
228 uint64_t dbuf_refcount(dmu_buf_impl_t *db);
229 
230 void dbuf_rele(dmu_buf_impl_t *db, void *tag);
231 
232 dmu_buf_impl_t *dbuf_find(struct dnode *dn, uint8_t level, uint64_t blkid);
233 
234 int dbuf_read(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags);
235 void dbuf_will_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx);
236 void dmu_buf_will_fill(dmu_buf_t *db, dmu_tx_t *tx);
237 void dbuf_fill_done(dmu_buf_impl_t *db, dmu_tx_t *tx);
238 void dmu_buf_will_fill(dmu_buf_t *db, dmu_tx_t *tx);
239 void dmu_buf_fill_done(dmu_buf_t *db, dmu_tx_t *tx);
240 void dbuf_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx);
241 
242 void dbuf_clear(dmu_buf_impl_t *db);
243 void dbuf_evict(dmu_buf_impl_t *db);
244 
245 void dbuf_setdirty(dmu_buf_impl_t *db, dmu_tx_t *tx);
246 void dbuf_sync(dmu_buf_impl_t *db, zio_t *zio, dmu_tx_t *tx);
247 void dbuf_unoverride(dmu_buf_impl_t *db, uint64_t txg);
248 
249 void dbuf_free_range(struct dnode *dn, uint64_t blkid, uint64_t nblks,
250     struct dmu_tx *);
251 
252 void dbuf_new_size(dmu_buf_impl_t *db, int size, dmu_tx_t *tx);
253 
254 void dbuf_init(void);
255 void dbuf_fini(void);
256 
257 #ifdef ZFS_DEBUG
258 
259 /*
260  * There should be a ## between the string literal and fmt, to make it
261  * clear that we're joining two strings together, but gcc does not
262  * support that preprocessor token.
263  */
264 #define	dprintf_dbuf(dbuf, fmt, ...) do { \
265 	if (zfs_flags & ZFS_DEBUG_DPRINTF) { \
266 	char __db_buf[32]; \
267 	uint64_t __db_obj = (dbuf)->db.db_object; \
268 	if (__db_obj == DMU_META_DNODE_OBJECT) \
269 		(void) strcpy(__db_buf, "mdn"); \
270 	else \
271 		(void) snprintf(__db_buf, sizeof (__db_buf), "%lld", \
272 		    (u_longlong_t)__db_obj); \
273 	dprintf_ds((dbuf)->db_objset->os_dsl_dataset, \
274 	    "obj=%s lvl=%u blkid=%lld " fmt, \
275 	    __db_buf, (dbuf)->db_level, \
276 	    (u_longlong_t)(dbuf)->db_blkid, __VA_ARGS__); \
277 	} \
278 _NOTE(CONSTCOND) } while (0)
279 
280 #define	dprintf_dbuf_bp(db, bp, fmt, ...) do {			\
281 	if (zfs_flags & ZFS_DEBUG_DPRINTF) {			\
282 	char __blkbuf[BP_SPRINTF_LEN];				\
283 	sprintf_blkptr(__blkbuf, BP_SPRINTF_LEN, bp);		\
284 	dprintf_dbuf(db, fmt " %s\n", __VA_ARGS__, __blkbuf);	\
285 	} \
286 _NOTE(CONSTCOND) } while (0)
287 
288 #define	DBUF_VERIFY(db)	dbuf_verify(db)
289 
290 #else
291 
292 #define	dprintf_dbuf(db, fmt, ...)
293 #define	dprintf_dbuf_bp(db, bp, fmt, ...)
294 #define	DBUF_VERIFY(db)
295 
296 #endif
297 
298 
299 #ifdef	__cplusplus
300 }
301 #endif
302 
303 #endif /* _SYS_DBUF_H */
304