xref: /onnv-gate/usr/src/uts/common/fs/zfs/sys/dbuf.h (revision 3290)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51544Seschrock  * Common Development and Distribution License (the "License").
61544Seschrock  * 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 /*
221544Seschrock  * 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 /*
471544Seschrock  * define flags for dbuf_read
48789Sahrens  */
49789Sahrens 
502391Smaybee #define	DB_RF_MUST_SUCCEED	(1 << 0)
51789Sahrens #define	DB_RF_CANFAIL		(1 << 1)
52789Sahrens #define	DB_RF_HAVESTRUCT	(1 << 2)
53789Sahrens #define	DB_RF_NOPREFETCH	(1 << 3)
541544Seschrock #define	DB_RF_NEVERWAIT		(1 << 4)
552391Smaybee #define	DB_RF_CACHED		(1 << 5)
56789Sahrens 
57789Sahrens /*
58789Sahrens  * The state transition diagram for dbufs looks like:
59789Sahrens  *
60789Sahrens  *		+----> READ ----+
61789Sahrens  *		|		|
62789Sahrens  *		|		V
631544Seschrock  *  (alloc)-->UNCACHED	     CACHED-->EVICTING-->(free)
64789Sahrens  *		|		^
65789Sahrens  *		|		|
66789Sahrens  *		+----> FILL ----+
67789Sahrens  */
68789Sahrens typedef enum dbuf_states {
69789Sahrens 	DB_UNCACHED,
70789Sahrens 	DB_FILL,
71789Sahrens 	DB_READ,
721544Seschrock 	DB_CACHED,
731544Seschrock 	DB_EVICTING
74789Sahrens } dbuf_states_t;
75789Sahrens 
76789Sahrens struct objset_impl;
77789Sahrens struct dnode;
78789Sahrens struct dmu_tx;
79789Sahrens 
80789Sahrens /*
81789Sahrens  * level = 0 means the user data
82789Sahrens  * level = 1 means the single indirect block
83789Sahrens  * etc.
84789Sahrens  */
85789Sahrens 
86789Sahrens #define	LIST_LINK_INACTIVE(link) \
87789Sahrens 	((link)->list_next == NULL && (link)->list_prev == NULL)
88789Sahrens 
89789Sahrens typedef struct dmu_buf_impl {
90789Sahrens 	/*
91789Sahrens 	 * The following members are immutable, with the exception of
92789Sahrens 	 * db.db_data, which is protected by db_mtx.
93789Sahrens 	 */
94789Sahrens 
95789Sahrens 	/* the publicly visible structure */
96789Sahrens 	dmu_buf_t db;
97789Sahrens 
98789Sahrens 	/* the objset we belong to */
99789Sahrens 	struct objset_impl *db_objset;
100789Sahrens 
101789Sahrens 	/*
102789Sahrens 	 * the dnode we belong to (NULL when evicted)
103789Sahrens 	 */
104789Sahrens 	struct dnode *db_dnode;
105789Sahrens 
106789Sahrens 	/*
107789Sahrens 	 * our parent buffer; if the dnode points to us directly,
108789Sahrens 	 * db_parent == db_dnode->dn_dbuf
109789Sahrens 	 * only accessed by sync thread ???
110789Sahrens 	 * (NULL when evicted)
111789Sahrens 	 */
112789Sahrens 	struct dmu_buf_impl *db_parent;
113789Sahrens 
114789Sahrens 	/*
115789Sahrens 	 * link for hash table of all dmu_buf_impl_t's
116789Sahrens 	 */
117789Sahrens 	struct dmu_buf_impl *db_hash_next;
118789Sahrens 
119789Sahrens 	/* our block number */
120789Sahrens 	uint64_t db_blkid;
121789Sahrens 
122789Sahrens 	/*
123789Sahrens 	 * Pointer to the blkptr_t which points to us. May be NULL if we
124789Sahrens 	 * don't have one yet. (NULL when evicted)
125789Sahrens 	 */
126789Sahrens 	blkptr_t *db_blkptr;
127789Sahrens 
128789Sahrens 	/*
129789Sahrens 	 * Our indirection level.  Data buffers have db_level==0.
130789Sahrens 	 * Indirect buffers which point to data buffers have
131789Sahrens 	 * db_level==1. etc.  Buffers which contain dnodes have
132789Sahrens 	 * db_level==0, since the dnodes are stored in a file.
133789Sahrens 	 */
134789Sahrens 	uint8_t db_level;
135789Sahrens 
136789Sahrens 	/* db_mtx protects the members below */
137789Sahrens 	kmutex_t db_mtx;
138789Sahrens 
139789Sahrens 	/*
140789Sahrens 	 * Current state of the buffer
141789Sahrens 	 */
142789Sahrens 	dbuf_states_t db_state;
143789Sahrens 
144789Sahrens 	/*
145789Sahrens 	 * Refcount accessed by dmu_buf_{hold,rele}.
146789Sahrens 	 * If nonzero, the buffer can't be destroyed.
147789Sahrens 	 * Protected by db_mtx.
148789Sahrens 	 */
149789Sahrens 	refcount_t db_holds;
150789Sahrens 
151789Sahrens 	/* buffer holding our data */
152789Sahrens 	arc_buf_t *db_buf;
153789Sahrens 
154789Sahrens 	kcondvar_t db_changed;
155789Sahrens 	arc_buf_t *db_data_pending;
156789Sahrens 
157789Sahrens 	/*
158789Sahrens 	 * Last time (transaction group) this buffer was dirtied.
159789Sahrens 	 */
160789Sahrens 	uint64_t db_dirtied;
161789Sahrens 
162789Sahrens 	/*
1631544Seschrock 	 * If db_dnode != NULL, our link on the owner dnodes's dn_dbufs list.
1641544Seschrock 	 * Protected by its dn_dbufs_mtx.
165789Sahrens 	 */
166789Sahrens 	list_node_t db_link;
167789Sahrens 
168789Sahrens 	/* Our link on dn_dirty_dbufs[txg] */
169789Sahrens 	list_node_t db_dirty_node[TXG_SIZE];
170789Sahrens 	uint8_t db_dirtycnt;
171789Sahrens 
172789Sahrens 	/*
173789Sahrens 	 * Data which is unique to data (leaf) blocks:
174789Sahrens 	 */
175789Sahrens 	struct {
176789Sahrens 		/* stuff we store for the user (see dmu_buf_set_user) */
177789Sahrens 		void *db_user_ptr;
178789Sahrens 		void **db_user_data_ptr_ptr;
179789Sahrens 		dmu_buf_evict_func_t *db_evict_func;
180789Sahrens 		uint8_t db_immediate_evict;
181789Sahrens 		uint8_t db_freed_in_flight;
182789Sahrens 
183789Sahrens 		/*
184789Sahrens 		 * db_data_old[txg&TXG_MASK] is set when we
185789Sahrens 		 * dirty the buffer, so that we can retain the
186789Sahrens 		 * pointer even if it gets COW'd in a subsequent
187789Sahrens 		 * transaction group.
188789Sahrens 		 *
189789Sahrens 		 * If the buffer is dirty in any txg, it can't
190789Sahrens 		 * be destroyed.
191789Sahrens 		 */
192789Sahrens 		/*
193789Sahrens 		 * XXX Protected by db_mtx and dn_dirty_mtx.
194789Sahrens 		 * db_mtx must be held to read db_dirty[], and
195789Sahrens 		 * both db_mtx and dn_dirty_mtx must be held to
196789Sahrens 		 * modify (dirty or clean). db_mtx must be held
197789Sahrens 		 * before dn_dirty_mtx.
198789Sahrens 		 */
1993093Sahrens 		arc_buf_t *db_data_old[TXG_SIZE];
200789Sahrens 		blkptr_t *db_overridden_by[TXG_SIZE];
201789Sahrens 	} db_d;
202789Sahrens } dmu_buf_impl_t;
203789Sahrens 
204789Sahrens /* Note: the dbuf hash table is exposed only for the mdb module */
205789Sahrens #define	DBUF_MUTEXES 256
206789Sahrens #define	DBUF_HASH_MUTEX(h, idx) (&(h)->hash_mutexes[(idx) & (DBUF_MUTEXES-1)])
207789Sahrens typedef struct dbuf_hash_table {
208789Sahrens 	uint64_t hash_table_mask;
209789Sahrens 	dmu_buf_impl_t **hash_table;
210789Sahrens 	kmutex_t hash_mutexes[DBUF_MUTEXES];
211789Sahrens } dbuf_hash_table_t;
212789Sahrens 
213789Sahrens 
214789Sahrens uint64_t dbuf_whichblock(struct dnode *di, uint64_t offset);
215789Sahrens 
216789Sahrens dmu_buf_impl_t *dbuf_create_tlib(struct dnode *dn, char *data);
2171544Seschrock dmu_buf_impl_t *dbuf_create_bonus(struct dnode *dn);
218789Sahrens 
2191544Seschrock dmu_buf_impl_t *dbuf_hold(struct dnode *dn, uint64_t blkid, void *tag);
220789Sahrens dmu_buf_impl_t *dbuf_hold_level(struct dnode *dn, int level, uint64_t blkid,
221789Sahrens     void *tag);
222789Sahrens int dbuf_hold_impl(struct dnode *dn, uint8_t level, uint64_t blkid, int create,
223789Sahrens     void *tag, dmu_buf_impl_t **dbp);
224789Sahrens 
225789Sahrens void dbuf_prefetch(struct dnode *dn, uint64_t blkid);
226789Sahrens 
227789Sahrens void dbuf_add_ref(dmu_buf_impl_t *db, void *tag);
228789Sahrens uint64_t dbuf_refcount(dmu_buf_impl_t *db);
229789Sahrens 
2301544Seschrock void dbuf_rele(dmu_buf_impl_t *db, void *tag);
231789Sahrens 
232789Sahrens dmu_buf_impl_t *dbuf_find(struct dnode *dn, uint8_t level, uint64_t blkid);
233789Sahrens 
2341544Seschrock int dbuf_read(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags);
235789Sahrens void dbuf_will_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx);
2361544Seschrock void dmu_buf_will_fill(dmu_buf_t *db, dmu_tx_t *tx);
237789Sahrens void dbuf_fill_done(dmu_buf_impl_t *db, dmu_tx_t *tx);
238789Sahrens void dmu_buf_will_fill(dmu_buf_t *db, dmu_tx_t *tx);
239789Sahrens void dmu_buf_fill_done(dmu_buf_t *db, dmu_tx_t *tx);
240789Sahrens void dbuf_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx);
241789Sahrens 
2421544Seschrock void dbuf_clear(dmu_buf_impl_t *db);
243789Sahrens void dbuf_evict(dmu_buf_impl_t *db);
244789Sahrens 
245789Sahrens void dbuf_setdirty(dmu_buf_impl_t *db, dmu_tx_t *tx);
246789Sahrens void dbuf_sync(dmu_buf_impl_t *db, zio_t *zio, dmu_tx_t *tx);
247789Sahrens void dbuf_unoverride(dmu_buf_impl_t *db, uint64_t txg);
248789Sahrens 
249789Sahrens void dbuf_free_range(struct dnode *dn, uint64_t blkid, uint64_t nblks,
250789Sahrens     struct dmu_tx *);
251789Sahrens 
252789Sahrens void dbuf_new_size(dmu_buf_impl_t *db, int size, dmu_tx_t *tx);
253789Sahrens 
254789Sahrens void dbuf_init(void);
255789Sahrens void dbuf_fini(void);
256789Sahrens 
257*3290Sjohansen #define	DBUF_GET_BUFC_TYPE(db)					\
258*3290Sjohansen 	((((db)->db_level > 0) ||				\
259*3290Sjohansen 	    (dmu_ot[(db)->db_dnode->dn_type].ot_metadata)) ?	\
260*3290Sjohansen 	    ARC_BUFC_METADATA : ARC_BUFC_DATA);
261*3290Sjohansen 
262789Sahrens #ifdef ZFS_DEBUG
263789Sahrens 
264789Sahrens /*
265789Sahrens  * There should be a ## between the string literal and fmt, to make it
266896Smaybee  * clear that we're joining two strings together, but gcc does not
267896Smaybee  * support that preprocessor token.
268789Sahrens  */
269789Sahrens #define	dprintf_dbuf(dbuf, fmt, ...) do { \
270789Sahrens 	if (zfs_flags & ZFS_DEBUG_DPRINTF) { \
271789Sahrens 	char __db_buf[32]; \
272789Sahrens 	uint64_t __db_obj = (dbuf)->db.db_object; \
273789Sahrens 	if (__db_obj == DMU_META_DNODE_OBJECT) \
274789Sahrens 		(void) strcpy(__db_buf, "mdn"); \
275789Sahrens 	else \
276789Sahrens 		(void) snprintf(__db_buf, sizeof (__db_buf), "%lld", \
277789Sahrens 		    (u_longlong_t)__db_obj); \
278789Sahrens 	dprintf_ds((dbuf)->db_objset->os_dsl_dataset, \
279789Sahrens 	    "obj=%s lvl=%u blkid=%lld " fmt, \
280789Sahrens 	    __db_buf, (dbuf)->db_level, \
281789Sahrens 	    (u_longlong_t)(dbuf)->db_blkid, __VA_ARGS__); \
282789Sahrens 	} \
283789Sahrens _NOTE(CONSTCOND) } while (0)
284789Sahrens 
285789Sahrens #define	dprintf_dbuf_bp(db, bp, fmt, ...) do {			\
286896Smaybee 	if (zfs_flags & ZFS_DEBUG_DPRINTF) {			\
287896Smaybee 	char __blkbuf[BP_SPRINTF_LEN];				\
288896Smaybee 	sprintf_blkptr(__blkbuf, BP_SPRINTF_LEN, bp);		\
289789Sahrens 	dprintf_dbuf(db, fmt " %s\n", __VA_ARGS__, __blkbuf);	\
290789Sahrens 	} \
291789Sahrens _NOTE(CONSTCOND) } while (0)
292789Sahrens 
293873Sek110237 #define	DBUF_VERIFY(db)	dbuf_verify(db)
294873Sek110237 
295789Sahrens #else
296789Sahrens 
297789Sahrens #define	dprintf_dbuf(db, fmt, ...)
298789Sahrens #define	dprintf_dbuf_bp(db, bp, fmt, ...)
299873Sek110237 #define	DBUF_VERIFY(db)
300789Sahrens 
301789Sahrens #endif
302789Sahrens 
303789Sahrens 
304789Sahrens #ifdef	__cplusplus
305789Sahrens }
306789Sahrens #endif
307789Sahrens 
308789Sahrens #endif /* _SYS_DBUF_H */
309