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.
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.
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]
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
29 #pragma ident "%Z%%M% %I% %E% SMI"
31 #include <sys/zfs_context.h>
36 #include <sys/refcount.h>
37 #include <sys/dmu_zfetch.h>
46 #define DNODE_MUST_BE_ALLOCATED 1
47 #define DNODE_MUST_BE_FREE 2
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 */
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 #define DN_MAX_OBJECT (1ULL << DN_MAX_OBJECT_SHIFT)
68 #define DNODES_PER_BLOCK_SHIFT (DNODE_BLOCK_SHIFT - DNODE_SHIFT)
69 #define DNODES_PER_BLOCK (1ULL << DNODES_PER_BLOCK_SHIFT)
70 #define DNODES_PER_LEVEL_SHIFT (DN_MAX_INDBLKSHIFT - SPA_BLKPTRSHIFT)
72 /* The +2 here is a cheesy way to round up */
73 #define DN_MAX_LEVELS (2 + ((DN_MAX_OFFSET_SHIFT - SPA_MINBLOCKSHIFT) / \
74 (DN_MIN_INDBLKSHIFT - SPA_BLKPTRSHIFT)))
76 #define DN_BONUS(dnp) ((void*)((dnp)->dn_bonus + \
77 (((dnp)->dn_nblkptr - 1) * sizeof (blkptr_t))))
79 #define DN_USED_BYTES(dnp) (((dnp)->dn_flags & DNODE_FLAG_USED_BYTES) ? \
80 (dnp)->dn_used : (dnp)->dn_used << SPA_MINBLOCKSHIFT)
82 #define EPB(blkshift, typeshift) (1 << (blkshift - typeshift))
88 enum dnode_dirtycontext {
94 /* Is dn_used in bytes? if not, it's in multiples of SPA_MINBLOCKSIZE */
95 #define DNODE_FLAG_USED_BYTES (1<<0)
97 typedef struct dnode_phys {
98 uint8_t dn_type; /* dmu_object_type_t */
99 uint8_t dn_indblkshift; /* ln2(indirect block size) */
100 uint8_t dn_nlevels; /* 1=dn_blkptr->data blocks */
101 uint8_t dn_nblkptr; /* length of dn_blkptr */
102 uint8_t dn_bonustype; /* type of data in bonus buffer */
103 uint8_t dn_checksum; /* ZIO_CHECKSUM type */
104 uint8_t dn_compress; /* ZIO_COMPRESS type */
105 uint8_t dn_flags; /* DNODE_FLAG_* */
106 uint16_t dn_datablkszsec; /* data block size in 512b sectors */
107 uint16_t dn_bonuslen; /* length of dn_bonus */
110 /* accounting is protected by dn_dirty_mtx */
111 uint64_t dn_maxblkid; /* largest allocated block ID */
112 uint64_t dn_used; /* bytes (or sectors) of disk space */
116 blkptr_t dn_blkptr[1];
117 uint8_t dn_bonus[DN_MAX_BONUSLEN];
120 typedef struct dnode {
122 * dn_struct_rwlock protects the structure of the dnode,
123 * including the number of levels of indirection (dn_nlevels),
124 * dn_maxblkid, and dn_next_*
126 krwlock_t dn_struct_rwlock;
129 * Our link on dataset's dd_dnodes list.
130 * Protected by dd_accounting_mtx.
135 struct objset_impl *dn_objset;
137 struct dmu_buf_impl *dn_dbuf;
138 dnode_phys_t *dn_phys; /* pointer into dn->dn_dbuf->db.db_data */
141 * Copies of stuff in dn_phys. They're valid in the open
142 * context (eg. even before the dnode is first synced).
143 * Where necessary, these are protected by dn_struct_rwlock.
145 dmu_object_type_t dn_type; /* object type */
146 uint16_t dn_bonuslen; /* bonus length */
147 uint8_t dn_bonustype; /* bonus type */
148 uint8_t dn_nblkptr; /* number of blkptrs (immutable) */
149 uint8_t dn_checksum; /* ZIO_CHECKSUM type */
150 uint8_t dn_compress; /* ZIO_COMPRESS type */
152 uint8_t dn_indblkshift;
153 uint8_t dn_datablkshift; /* zero if blksz not power of 2! */
154 uint16_t dn_datablkszsec; /* in 512b sectors */
155 uint32_t dn_datablksz; /* in bytes */
156 uint64_t dn_maxblkid;
157 uint8_t dn_next_nlevels[TXG_SIZE];
158 uint8_t dn_next_indblkshift[TXG_SIZE];
159 uint32_t dn_next_blksz[TXG_SIZE]; /* next block size in bytes */
161 /* protected by os_lock: */
162 list_node_t dn_dirty_link[TXG_SIZE]; /* next on dataset's dirty */
164 /* protected by dn_mtx: */
166 list_t dn_dirty_records[TXG_SIZE];
167 avl_tree_t dn_ranges[TXG_SIZE];
168 uint64_t dn_allocated_txg;
169 uint64_t dn_free_txg;
170 uint64_t dn_assigned_txg;
171 kcondvar_t dn_notxholds;
172 enum dnode_dirtycontext dn_dirtyctx;
173 uint8_t *dn_dirtyctx_firstset; /* dbg: contents meaningless */
175 /* protected by own devices */
176 refcount_t dn_tx_holds;
179 kmutex_t dn_dbufs_mtx;
180 list_t dn_dbufs; /* linked list of descendent dbuf_t's */
181 struct dmu_buf_impl *dn_bonus; /* bonus buffer dbuf */
183 /* parent IO for current sync write */
186 /* holds prefetch structure */
187 struct zfetch dn_zfetch;
190 typedef struct free_range {
196 dnode_t *dnode_special_open(struct objset_impl *dd, dnode_phys_t *dnp,
198 void dnode_special_close(dnode_t *dn);
200 int dnode_hold(struct objset_impl *dd, uint64_t object,
201 void *ref, dnode_t **dnp);
202 int dnode_hold_impl(struct objset_impl *dd, uint64_t object, int flag,
203 void *ref, dnode_t **dnp);
204 void dnode_add_ref(dnode_t *dn, void *ref);
205 void dnode_rele(dnode_t *dn, void *ref);
206 void dnode_setdirty(dnode_t *dn, dmu_tx_t *tx);
207 void dnode_sync(dnode_t *dn, dmu_tx_t *tx);
208 void dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs,
209 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx);
210 void dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize,
211 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx);
212 void dnode_free(dnode_t *dn, dmu_tx_t *tx);
213 void dnode_byteswap(dnode_phys_t *dnp);
214 void dnode_buf_byteswap(void *buf, size_t size);
215 void dnode_verify(dnode_t *dn);
216 int dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx);
217 uint64_t dnode_current_max_length(dnode_t *dn);
218 void dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx);
219 void dnode_clear_range(dnode_t *dn, uint64_t blkid,
220 uint64_t nblks, dmu_tx_t *tx);
221 void dnode_diduse_space(dnode_t *dn, int64_t space);
222 void dnode_willuse_space(dnode_t *dn, int64_t space, dmu_tx_t *tx);
223 void dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx);
224 uint64_t dnode_block_freed(dnode_t *dn, uint64_t blkid);
225 void dnode_init(void);
226 void dnode_fini(void);
227 int dnode_next_offset(dnode_t *dn, boolean_t hole, uint64_t *off, int minlvl,
228 uint64_t blkfill, uint64_t txg);
229 int dnode_evict_dbufs(dnode_t *dn, int try);
234 * There should be a ## between the string literal and fmt, to make it
235 * clear that we're joining two strings together, but that piece of shit
236 * gcc doesn't support that preprocessor token.
238 #define dprintf_dnode(dn, fmt, ...) do { \
239 if (zfs_flags & ZFS_DEBUG_DPRINTF) { \
241 uint64_t __db_obj = (dn)->dn_object; \
242 if (__db_obj == DMU_META_DNODE_OBJECT) \
243 (void) strcpy(__db_buf, "mdn"); \
245 (void) snprintf(__db_buf, sizeof (__db_buf), "%lld", \
246 (u_longlong_t)__db_obj);\
247 dprintf_ds((dn)->dn_objset->os_dsl_dataset, "obj=%s " fmt, \
248 __db_buf, __VA_ARGS__); \
250 _NOTE(CONSTCOND) } while (0)
252 #define DNODE_VERIFY(dn) dnode_verify(dn)
253 #define FREE_VERIFY(db, start, end, tx) free_verify(db, start, end, tx)
257 #define dprintf_dnode(db, fmt, ...)
258 #define DNODE_VERIFY(dn)
259 #define FREE_VERIFY(db, start, end, tx)
267 #endif /* _SYS_DNODE_H */