xref: /illumos-gate/usr/src/uts/common/fs/zfs/sys/dnode.h (revision 9c9dc39aa72ac40bb2558d54adfa596d217135d9)
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_DNODE_H
28 #define	_SYS_DNODE_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include <sys/zfs_context.h>
33 #include <sys/avl.h>
34 #include <sys/spa.h>
35 #include <sys/txg.h>
36 #include <sys/refcount.h>
37 #include <sys/dmu_zfetch.h>
38 
39 #ifdef	__cplusplus
40 extern "C" {
41 #endif
42 
43 /*
44  * Flags.
45  */
46 #define	DNODE_MUST_BE_ALLOCATED	1
47 #define	DNODE_MUST_BE_FREE	2
48 
49 /*
50  * Fixed constants.
51  */
52 #define	DNODE_SHIFT		9	/* 512 bytes */
53 #define	DN_MIN_INDBLKSHIFT	10	/* 1k */
54 #define	DN_MAX_INDBLKSHIFT	14	/* 16k */
55 #define	DNODE_BLOCK_SHIFT	14	/* 16k */
56 #define	DNODE_CORE_SIZE		64	/* 64 bytes for dnode sans blkptrs */
57 #define	DN_MAX_OBJECT_SHIFT	48	/* 256 trillion (zfs_fid_t limit) */
58 #define	DN_MAX_OFFSET_SHIFT	64	/* 2^64 bytes in a dnode */
59 
60 /*
61  * Derived constants.
62  */
63 #define	DNODE_SIZE	(1 << DNODE_SHIFT)
64 #define	DN_MAX_NBLKPTR	((DNODE_SIZE - DNODE_CORE_SIZE) >> SPA_BLKPTRSHIFT)
65 #define	DN_MAX_BONUSLEN	(DNODE_SIZE - DNODE_CORE_SIZE - (1 << SPA_BLKPTRSHIFT))
66 
67 #define	DNODES_PER_BLOCK_SHIFT	(DNODE_BLOCK_SHIFT - DNODE_SHIFT)
68 #define	DNODES_PER_BLOCK	(1ULL << DNODES_PER_BLOCK_SHIFT)
69 #define	DNODES_PER_LEVEL_SHIFT	(DN_MAX_INDBLKSHIFT - SPA_BLKPTRSHIFT)
70 
71 #define	DN_META_DNODE_LEVELS	\
72 	(1 + (DN_MAX_OBJECT_SHIFT - DNODE_SHIFT + SPA_BLKPTRSHIFT -	\
73 	DNODES_PER_BLOCK_SHIFT) / DNODES_PER_LEVEL_SHIFT)
74 
75 /* The +2 here is a cheesy way to round up */
76 #define	DN_MAX_LEVELS	(2 + ((DN_MAX_OFFSET_SHIFT - SPA_MINBLOCKSHIFT) / \
77 	(DN_MIN_INDBLKSHIFT - SPA_BLKPTRSHIFT)))
78 
79 #define	DN_MAX_OBJECT		\
80 	((uint64_t)DN_MAX_NBLKPTR << (DNODES_PER_BLOCK_SHIFT +	\
81 	(DN_META_DNODE_LEVELS - 1) * DNODES_PER_LEVEL_SHIFT))
82 
83 #define	DN_BONUS(dnp)	((void*)((dnp)->dn_bonus + \
84 	(((dnp)->dn_nblkptr - 1) * sizeof (blkptr_t))))
85 
86 #define	EPB(blkshift, typeshift)	(1 << (blkshift - typeshift))
87 
88 struct dmu_buf_impl;
89 struct objset_impl;
90 struct zio;
91 
92 enum dnode_dirtycontext {
93 	DN_UNDIRTIED,
94 	DN_DIRTY_OPEN,
95 	DN_DIRTY_SYNC
96 };
97 
98 typedef struct dnode_phys {
99 	uint8_t dn_type;		/* dmu_object_type_t */
100 	uint8_t dn_indblkshift;		/* ln2(indirect block size) */
101 	uint8_t dn_nlevels;		/* 1=dn_blkptr->data blocks */
102 	uint8_t dn_nblkptr;		/* length of dn_blkptr */
103 	uint8_t dn_bonustype;		/* type of data in bonus buffer */
104 	uint8_t	dn_checksum;		/* ZIO_CHECKSUM type */
105 	uint8_t	dn_compress;		/* ZIO_COMPRESS type */
106 	uint8_t dn_pad1[1];
107 	uint16_t dn_datablkszsec;	/* data block size in 512b sectors */
108 	uint16_t dn_bonuslen;		/* length of dn_bonus */
109 	uint8_t dn_pad2[4];
110 
111 	/* accounting is protected by dn_dirty_mtx */
112 	uint64_t dn_maxblkid;		/* largest allocated block ID */
113 	uint64_t dn_secphys;		/* 512b sectors of disk space used */
114 
115 	uint64_t dn_pad3[4];
116 
117 	blkptr_t dn_blkptr[1];
118 	uint8_t dn_bonus[DN_MAX_BONUSLEN];
119 } dnode_phys_t;
120 
121 typedef struct dnode {
122 	/*
123 	 * lock ordering:
124 	 *
125 	 * db_mtx > dn_dirty_mtx
126 	 * 	dbuf_syncdone
127 	 *
128 	 * dn_struct_rwlock/r > dn_dirty_mtx
129 	 * 	dmu_object_info
130 	 *
131 	 * dn_struct_rwlock/r > db_mtx > dn_dirty_mtx
132 	 * 	dbuf_dirty
133 	 * 	dbuf_setdirty
134 	 *
135 	 * dn_struct_rwlock/w > db_mtx > dn_mtx
136 	 * 	dnode_increase_indirection -> dbuf_find
137 	 * 	dbuf_hold_impl
138 	 * 	dnode_set_bonus
139 	 *
140 	 * dn_struct_rwlock/w > dn_mtx
141 	 * 	dnode_increase_indirection
142 	 *
143 	 * dn_dirty_mtx > dn_mtx
144 	 * 	dnode_buf_pageout
145 	 *
146 	 * db_mtx > dn_mtx
147 	 * 	dbuf_create
148 	 */
149 
150 	/*
151 	 * dn_struct_rwlock protects the structure of the dnode.
152 	 * In particular, it protects the number of levels of indirection.
153 	 */
154 	krwlock_t dn_struct_rwlock;
155 
156 	/*
157 	 * Our link on dataset's dd_dnodes list.
158 	 * Protected by dd_accounting_mtx.
159 	 */
160 	list_node_t dn_link;
161 
162 	/* immutable: */
163 	struct objset_impl *dn_objset;
164 	uint64_t dn_object;
165 	struct dmu_buf_impl *dn_dbuf;
166 	dnode_phys_t *dn_phys; /* pointer into dn->dn_dbuf->db.db_data */
167 
168 	/*
169 	 * Copies of stuff in dn_phys.  They're valid here even before
170 	 * the dnode is first synced.
171 	 */
172 	dmu_object_type_t dn_type;	/* object type (immutable) */
173 	uint8_t dn_bonustype;		/* bonus type (immutable) */
174 	uint16_t dn_bonuslen;		/* bonus length (immutable) */
175 	uint8_t dn_nblkptr;		/* number of blkptrs (immutable) */
176 	uint8_t dn_datablkshift;	/* zero if blksz not power of 2! */
177 	uint32_t dn_datablksz;		/* in bytes */
178 	uint16_t dn_datablkszsec;	/* in 512b sectors */
179 
180 	uint8_t dn_checksum;		/* ZIO_CHECKSUM type */
181 	uint8_t dn_compress;		/* ZIO_COMPRESS type */
182 
183 	/*
184 	 * The following are kept up-to-date in the *open* context, the syncing
185 	 * context should only pay attention to the dn_next_* values.
186 	 */
187 	uint8_t dn_nlevels;
188 	uint8_t dn_indblkshift;
189 
190 	uint8_t dn_next_nlevels[TXG_SIZE];
191 	uint8_t dn_next_indblkshift[TXG_SIZE];
192 
193 	/* protected by os_lock: */
194 	uint32_t dn_dirtyblksz[TXG_SIZE];	/* dirty block size in bytes */
195 	list_node_t dn_dirty_link[TXG_SIZE];	/* next on dataset's dirty */
196 
197 	/* protected by dn_mtx: */
198 	kmutex_t dn_mtx;
199 	list_t dn_dirty_dbufs[TXG_SIZE];
200 	uint64_t dn_maxblkid;
201 	avl_tree_t dn_ranges[TXG_SIZE];
202 	uint64_t dn_allocated_txg;
203 	uint64_t dn_free_txg;
204 	uint64_t dn_assigned_txg;
205 	struct dmu_tx *dn_assigned_tx;		/* if only one tx cares */
206 	kcondvar_t dn_notxholds;
207 	enum dnode_dirtycontext dn_dirtyctx;
208 	uint8_t *dn_dirtyctx_firstset;		/* dbg: contents meaningless */
209 
210 	/* protected by own devices */
211 	refcount_t dn_tx_holds;
212 	refcount_t dn_holds;
213 
214 	kmutex_t dn_dbufs_mtx;
215 	list_t dn_dbufs;		/* linked list of descendent dbuf_t's */
216 	kcondvar_t dn_evicted;		/* a child dbuf has been evicted */
217 
218 	/*
219 	 * Performance hack: whenever we have a hold on the bonus buffer of a
220 	 * ZAP object, we will also have a hold on db0.  This will keep the
221 	 * meta-data for a micro-zap object cached as long as the znode for the
222 	 * object is in the znode cache.
223 	 */
224 	struct dmu_buf_impl *dn_db0;
225 
226 	/* holds prefetch structure */
227 	struct zfetch	dn_zfetch;
228 } dnode_t;
229 
230 typedef struct free_range {
231 	avl_node_t fr_node;
232 	uint64_t fr_blkid;
233 	uint64_t fr_nblks;
234 } free_range_t;
235 
236 dnode_t *dnode_special_open(struct objset_impl *dd, dnode_phys_t *dnp,
237     uint64_t object);
238 void dnode_special_close(dnode_t *dn);
239 
240 dnode_t *dnode_hold(struct objset_impl *dd, uint64_t object, void *ref);
241 dnode_t *dnode_hold_impl(struct objset_impl *dd, uint64_t object, int flag,
242     void *ref);
243 void dnode_add_ref(dnode_t *dn, void *ref);
244 void dnode_rele(dnode_t *dn, void *ref);
245 void dnode_setdirty(dnode_t *dn, dmu_tx_t *tx);
246 int dnode_sync(dnode_t *dn, int level, struct zio *zio, dmu_tx_t *tx);
247 void dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs,
248     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx);
249 void dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize,
250     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx);
251 void dnode_free(dnode_t *dn, dmu_tx_t *tx);
252 void dnode_byteswap(dnode_phys_t *dnp);
253 void dnode_buf_byteswap(void *buf, size_t size);
254 void dnode_verify(dnode_t *dn);
255 int dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx);
256 uint64_t dnode_current_max_length(dnode_t *dn);
257 uint64_t dnode_max_nonzero_offset(dnode_t *dn);
258 void dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx);
259 void dnode_clear_range(dnode_t *dn, uint64_t blkid,
260     uint64_t nblks, dmu_tx_t *tx);
261 void dnode_diduse_space(dnode_t *dn, int64_t space);
262 void dnode_willuse_space(dnode_t *dn, int64_t space, dmu_tx_t *tx);
263 void dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx);
264 uint64_t dnode_block_freed(dnode_t *dn, uint64_t blkid);
265 void dnode_init(void);
266 void dnode_fini(void);
267 int dnode_next_offset(dnode_t *dn, boolean_t hole, uint64_t *off, int minlvl,
268     uint64_t blkfill);
269 
270 #ifdef ZFS_DEBUG
271 
272 /*
273  * There should be a ## between the string literal and fmt, to make it
274  * clear that we're joining two strings together, but that piece of shit
275  * gcc doesn't support that preprocessor token.
276  */
277 #define	dprintf_dnode(dn, fmt, ...) do { \
278 	if (zfs_flags & ZFS_DEBUG_DPRINTF) { \
279 	char __db_buf[32]; \
280 	uint64_t __db_obj = (dn)->dn_object; \
281 	if (__db_obj == DMU_META_DNODE_OBJECT) \
282 		(void) strcpy(__db_buf, "mdn"); \
283 	else \
284 		(void) snprintf(__db_buf, sizeof (__db_buf), "%lld", \
285 		    (u_longlong_t)__db_obj);\
286 	dprintf_ds((dn)->dn_objset->os_dsl_dataset, "obj=%s " fmt, \
287 	    __db_buf, __VA_ARGS__); \
288 	} \
289 _NOTE(CONSTCOND) } while (0)
290 
291 #define	DNODE_VERIFY(dn)		dnode_verify(dn)
292 #define	FREE_VERIFY(db, start, end, tx)	free_verify(db, start, end, tx)
293 
294 #else
295 
296 #define	dprintf_dnode(db, fmt, ...)
297 #define	DNODE_VERIFY(dn)
298 #define	FREE_VERIFY(db, start, end, tx)
299 
300 #endif
301 
302 #ifdef	__cplusplus
303 }
304 #endif
305 
306 #endif	/* _SYS_DNODE_H */
307