xref: /onnv-gate/usr/src/uts/common/fs/zfs/sys/dbuf.h (revision 1544)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
5*1544Seschrock  * Common Development and Distribution License (the "License").
6*1544Seschrock  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
22*1544Seschrock  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens #ifndef	_SYS_DBUF_H
27789Sahrens #define	_SYS_DBUF_H
28789Sahrens 
29789Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
30789Sahrens 
31789Sahrens #include <sys/dmu.h>
32789Sahrens #include <sys/spa.h>
33789Sahrens #include <sys/txg.h>
34789Sahrens #include <sys/zio.h>
35789Sahrens #include <sys/arc.h>
36789Sahrens #include <sys/zfs_context.h>
37789Sahrens #include <sys/refcount.h>
38789Sahrens 
39789Sahrens #ifdef	__cplusplus
40789Sahrens extern "C" {
41789Sahrens #endif
42789Sahrens 
43789Sahrens #define	DB_BONUS_BLKID (-1ULL)
44789Sahrens #define	IN_DMU_SYNC ((blkptr_t *)-1)
45789Sahrens 
46789Sahrens /*
47*1544Seschrock  * define flags for dbuf_read
48789Sahrens  */
49789Sahrens 
50789Sahrens #define	DB_RF_MUST_SUCCEED	0
51789Sahrens #define	DB_RF_CANFAIL		(1 << 1)
52789Sahrens #define	DB_RF_HAVESTRUCT	(1 << 2)
53789Sahrens #define	DB_RF_NOPREFETCH	(1 << 3)
54*1544Seschrock #define	DB_RF_NEVERWAIT		(1 << 4)
55789Sahrens 
56789Sahrens /*
57789Sahrens  * The state transition diagram for dbufs looks like:
58789Sahrens  *
59789Sahrens  *		+----> READ ----+
60789Sahrens  *		|		|
61789Sahrens  *		|		V
62*1544Seschrock  *  (alloc)-->UNCACHED	     CACHED-->EVICTING-->(free)
63789Sahrens  *		|		^
64789Sahrens  *		|		|
65789Sahrens  *		+----> FILL ----+
66789Sahrens  */
67789Sahrens typedef enum dbuf_states {
68789Sahrens 	DB_UNCACHED,
69789Sahrens 	DB_FILL,
70789Sahrens 	DB_READ,
71*1544Seschrock 	DB_CACHED,
72*1544Seschrock 	DB_EVICTING
73789Sahrens } dbuf_states_t;
74789Sahrens 
75789Sahrens struct objset_impl;
76789Sahrens struct dnode;
77789Sahrens struct dmu_tx;
78789Sahrens 
79789Sahrens /*
80789Sahrens  * level = 0 means the user data
81789Sahrens  * level = 1 means the single indirect block
82789Sahrens  * etc.
83789Sahrens  */
84789Sahrens 
85789Sahrens #define	LIST_LINK_INACTIVE(link) \
86789Sahrens 	((link)->list_next == NULL && (link)->list_prev == NULL)
87789Sahrens 
88789Sahrens typedef struct dmu_buf_impl {
89789Sahrens 	/*
90789Sahrens 	 * The following members are immutable, with the exception of
91789Sahrens 	 * db.db_data, which is protected by db_mtx.
92789Sahrens 	 */
93789Sahrens 
94789Sahrens 	/* the publicly visible structure */
95789Sahrens 	dmu_buf_t db;
96789Sahrens 
97789Sahrens 	/* the objset we belong to */
98789Sahrens 	struct objset_impl *db_objset;
99789Sahrens 
100789Sahrens 	/*
101789Sahrens 	 * the dnode we belong to (NULL when evicted)
102789Sahrens 	 */
103789Sahrens 	struct dnode *db_dnode;
104789Sahrens 
105789Sahrens 	/*
106789Sahrens 	 * our parent buffer; if the dnode points to us directly,
107789Sahrens 	 * db_parent == db_dnode->dn_dbuf
108789Sahrens 	 * only accessed by sync thread ???
109789Sahrens 	 * (NULL when evicted)
110789Sahrens 	 */
111789Sahrens 	struct dmu_buf_impl *db_parent;
112789Sahrens 
113789Sahrens 	/*
114789Sahrens 	 * link for hash table of all dmu_buf_impl_t's
115789Sahrens 	 */
116789Sahrens 	struct dmu_buf_impl *db_hash_next;
117789Sahrens 
118789Sahrens 	/* our block number */
119789Sahrens 	uint64_t db_blkid;
120789Sahrens 
121789Sahrens 	/*
122789Sahrens 	 * Pointer to the blkptr_t which points to us. May be NULL if we
123789Sahrens 	 * don't have one yet. (NULL when evicted)
124789Sahrens 	 */
125789Sahrens 	blkptr_t *db_blkptr;
126789Sahrens 
127789Sahrens 	/*
128789Sahrens 	 * Our indirection level.  Data buffers have db_level==0.
129789Sahrens 	 * Indirect buffers which point to data buffers have
130789Sahrens 	 * db_level==1. etc.  Buffers which contain dnodes have
131789Sahrens 	 * db_level==0, since the dnodes are stored in a file.
132789Sahrens 	 */
133789Sahrens 	uint8_t db_level;
134789Sahrens 
135789Sahrens 	/* db_mtx protects the members below */
136789Sahrens 	kmutex_t db_mtx;
137789Sahrens 
138789Sahrens 	/*
139789Sahrens 	 * Current state of the buffer
140789Sahrens 	 */
141789Sahrens 	dbuf_states_t db_state;
142789Sahrens 
143789Sahrens 	/*
144789Sahrens 	 * Refcount accessed by dmu_buf_{hold,rele}.
145789Sahrens 	 * If nonzero, the buffer can't be destroyed.
146789Sahrens 	 * Protected by db_mtx.
147789Sahrens 	 */
148789Sahrens 	refcount_t db_holds;
149789Sahrens 
150789Sahrens 	/* buffer holding our data */
151789Sahrens 	arc_buf_t *db_buf;
152789Sahrens 
153789Sahrens 	kcondvar_t db_changed;
154789Sahrens 	arc_buf_t *db_data_pending;
155789Sahrens 
156789Sahrens 	/*
157789Sahrens 	 * Last time (transaction group) this buffer was dirtied.
158789Sahrens 	 */
159789Sahrens 	uint64_t db_dirtied;
160789Sahrens 
161789Sahrens 	/*
162*1544Seschrock 	 * If db_dnode != NULL, our link on the owner dnodes's dn_dbufs list.
163*1544Seschrock 	 * Protected by its dn_dbufs_mtx.
164789Sahrens 	 */
165789Sahrens 	list_node_t db_link;
166789Sahrens 
167789Sahrens 	/* Our link on dn_dirty_dbufs[txg] */
168789Sahrens 	list_node_t db_dirty_node[TXG_SIZE];
169789Sahrens 	uint8_t db_dirtycnt;
170789Sahrens 
171789Sahrens 	/*
172789Sahrens 	 * Data which is unique to data (leaf) blocks:
173789Sahrens 	 */
174789Sahrens 	struct {
175789Sahrens 		/* stuff we store for the user (see dmu_buf_set_user) */
176789Sahrens 		void *db_user_ptr;
177789Sahrens 		void **db_user_data_ptr_ptr;
178789Sahrens 		dmu_buf_evict_func_t *db_evict_func;
179789Sahrens 		uint8_t db_immediate_evict;
180789Sahrens 		uint8_t db_freed_in_flight;
181789Sahrens 
182789Sahrens 		/*
183789Sahrens 		 * db_data_old[txg&TXG_MASK] is set when we
184789Sahrens 		 * dirty the buffer, so that we can retain the
185789Sahrens 		 * pointer even if it gets COW'd in a subsequent
186789Sahrens 		 * transaction group.
187789Sahrens 		 *
188789Sahrens 		 * If the buffer is dirty in any txg, it can't
189789Sahrens 		 * be destroyed.
190789Sahrens 		 */
191789Sahrens 		/*
192789Sahrens 		 * XXX Protected by db_mtx and dn_dirty_mtx.
193789Sahrens 		 * db_mtx must be held to read db_dirty[], and
194789Sahrens 		 * both db_mtx and dn_dirty_mtx must be held to
195789Sahrens 		 * modify (dirty or clean). db_mtx must be held
196789Sahrens 		 * before dn_dirty_mtx.
197789Sahrens 		 */
198*1544Seschrock 		void *db_data_old[TXG_SIZE];
199789Sahrens 		blkptr_t *db_overridden_by[TXG_SIZE];
200789Sahrens 	} db_d;
201789Sahrens } dmu_buf_impl_t;
202789Sahrens 
203789Sahrens /* Note: the dbuf hash table is exposed only for the mdb module */
204789Sahrens #define	DBUF_MUTEXES 256
205789Sahrens #define	DBUF_HASH_MUTEX(h, idx) (&(h)->hash_mutexes[(idx) & (DBUF_MUTEXES-1)])
206789Sahrens typedef struct dbuf_hash_table {
207789Sahrens 	uint64_t hash_table_mask;
208789Sahrens 	dmu_buf_impl_t **hash_table;
209789Sahrens 	kmutex_t hash_mutexes[DBUF_MUTEXES];
210789Sahrens } dbuf_hash_table_t;
211789Sahrens 
212789Sahrens 
213789Sahrens uint64_t dbuf_whichblock(struct dnode *di, uint64_t offset);
214789Sahrens 
215789Sahrens dmu_buf_impl_t *dbuf_create_tlib(struct dnode *dn, char *data);
216*1544Seschrock dmu_buf_impl_t *dbuf_create_bonus(struct dnode *dn);
217789Sahrens 
218*1544Seschrock dmu_buf_impl_t *dbuf_hold(struct dnode *dn, uint64_t blkid, void *tag);
219789Sahrens dmu_buf_impl_t *dbuf_hold_level(struct dnode *dn, int level, uint64_t blkid,
220789Sahrens     void *tag);
221789Sahrens int dbuf_hold_impl(struct dnode *dn, uint8_t level, uint64_t blkid, int create,
222789Sahrens     void *tag, dmu_buf_impl_t **dbp);
223789Sahrens 
224789Sahrens void dbuf_prefetch(struct dnode *dn, uint64_t blkid);
225789Sahrens 
226789Sahrens void dbuf_add_ref(dmu_buf_impl_t *db, void *tag);
227789Sahrens uint64_t dbuf_refcount(dmu_buf_impl_t *db);
228789Sahrens 
229*1544Seschrock void dbuf_rele(dmu_buf_impl_t *db, void *tag);
230789Sahrens 
231789Sahrens dmu_buf_impl_t *dbuf_find(struct dnode *dn, uint8_t level, uint64_t blkid);
232789Sahrens 
233*1544Seschrock int dbuf_read(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags);
234789Sahrens void dbuf_will_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx);
235*1544Seschrock void dmu_buf_will_fill(dmu_buf_t *db, dmu_tx_t *tx);
236789Sahrens void dbuf_fill_done(dmu_buf_impl_t *db, dmu_tx_t *tx);
237789Sahrens void dmu_buf_will_fill(dmu_buf_t *db, dmu_tx_t *tx);
238789Sahrens void dmu_buf_fill_done(dmu_buf_t *db, dmu_tx_t *tx);
239789Sahrens void dbuf_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx);
240789Sahrens 
241*1544Seschrock void dbuf_clear(dmu_buf_impl_t *db);
242789Sahrens void dbuf_evict(dmu_buf_impl_t *db);
243789Sahrens 
244789Sahrens void dbuf_setdirty(dmu_buf_impl_t *db, dmu_tx_t *tx);
245789Sahrens void dbuf_sync(dmu_buf_impl_t *db, zio_t *zio, dmu_tx_t *tx);
246789Sahrens void dbuf_unoverride(dmu_buf_impl_t *db, uint64_t txg);
247789Sahrens 
248789Sahrens void dbuf_free_range(struct dnode *dn, uint64_t blkid, uint64_t nblks,
249789Sahrens     struct dmu_tx *);
250789Sahrens 
251789Sahrens void dbuf_new_size(dmu_buf_impl_t *db, int size, dmu_tx_t *tx);
252789Sahrens 
253789Sahrens void dbuf_init(void);
254789Sahrens void dbuf_fini(void);
255789Sahrens 
256789Sahrens #ifdef ZFS_DEBUG
257789Sahrens 
258789Sahrens /*
259789Sahrens  * There should be a ## between the string literal and fmt, to make it
260896Smaybee  * clear that we're joining two strings together, but gcc does not
261896Smaybee  * support that preprocessor token.
262789Sahrens  */
263789Sahrens #define	dprintf_dbuf(dbuf, fmt, ...) do { \
264789Sahrens 	if (zfs_flags & ZFS_DEBUG_DPRINTF) { \
265789Sahrens 	char __db_buf[32]; \
266789Sahrens 	uint64_t __db_obj = (dbuf)->db.db_object; \
267789Sahrens 	if (__db_obj == DMU_META_DNODE_OBJECT) \
268789Sahrens 		(void) strcpy(__db_buf, "mdn"); \
269789Sahrens 	else \
270789Sahrens 		(void) snprintf(__db_buf, sizeof (__db_buf), "%lld", \
271789Sahrens 		    (u_longlong_t)__db_obj); \
272789Sahrens 	dprintf_ds((dbuf)->db_objset->os_dsl_dataset, \
273789Sahrens 	    "obj=%s lvl=%u blkid=%lld " fmt, \
274789Sahrens 	    __db_buf, (dbuf)->db_level, \
275789Sahrens 	    (u_longlong_t)(dbuf)->db_blkid, __VA_ARGS__); \
276789Sahrens 	} \
277789Sahrens _NOTE(CONSTCOND) } while (0)
278789Sahrens 
279789Sahrens #define	dprintf_dbuf_bp(db, bp, fmt, ...) do {			\
280896Smaybee 	if (zfs_flags & ZFS_DEBUG_DPRINTF) {			\
281896Smaybee 	char __blkbuf[BP_SPRINTF_LEN];				\
282896Smaybee 	sprintf_blkptr(__blkbuf, BP_SPRINTF_LEN, bp);		\
283789Sahrens 	dprintf_dbuf(db, fmt " %s\n", __VA_ARGS__, __blkbuf);	\
284789Sahrens 	} \
285789Sahrens _NOTE(CONSTCOND) } while (0)
286789Sahrens 
287873Sek110237 #define	DBUF_VERIFY(db)	dbuf_verify(db)
288873Sek110237 
289789Sahrens #else
290789Sahrens 
291789Sahrens #define	dprintf_dbuf(db, fmt, ...)
292789Sahrens #define	dprintf_dbuf_bp(db, bp, fmt, ...)
293873Sek110237 #define	DBUF_VERIFY(db)
294789Sahrens 
295789Sahrens #endif
296789Sahrens 
297789Sahrens 
298789Sahrens #ifdef	__cplusplus
299789Sahrens }
300789Sahrens #endif
301789Sahrens 
302789Sahrens #endif /* _SYS_DBUF_H */
303