]>
Commit | Line | Data |
---|---|---|
34dc7c2f BB |
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 | /* | |
428870ff | 22 | * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. |
9bd274dd | 23 | * Copyright (c) 2012, 2014 by Delphix. All rights reserved. |
34dc7c2f BB |
24 | */ |
25 | ||
26 | #ifndef _SYS_DNODE_H | |
27 | #define _SYS_DNODE_H | |
28 | ||
34dc7c2f BB |
29 | #include <sys/zfs_context.h> |
30 | #include <sys/avl.h> | |
31 | #include <sys/spa.h> | |
32 | #include <sys/txg.h> | |
33 | #include <sys/zio.h> | |
34 | #include <sys/refcount.h> | |
35 | #include <sys/dmu_zfetch.h> | |
572e2857 | 36 | #include <sys/zrlock.h> |
34dc7c2f BB |
37 | |
38 | #ifdef __cplusplus | |
39 | extern "C" { | |
40 | #endif | |
41 | ||
42 | /* | |
b128c09f | 43 | * dnode_hold() flags. |
34dc7c2f BB |
44 | */ |
45 | #define DNODE_MUST_BE_ALLOCATED 1 | |
46 | #define DNODE_MUST_BE_FREE 2 | |
47 | ||
b128c09f BB |
48 | /* |
49 | * dnode_next_offset() flags. | |
50 | */ | |
51 | #define DNODE_FIND_HOLE 1 | |
52 | #define DNODE_FIND_BACKWARDS 2 | |
53 | #define DNODE_FIND_HAVELOCK 4 | |
54 | ||
34dc7c2f BB |
55 | /* |
56 | * Fixed constants. | |
57 | */ | |
58 | #define DNODE_SHIFT 9 /* 512 bytes */ | |
59 | #define DN_MIN_INDBLKSHIFT 10 /* 1k */ | |
60 | #define DN_MAX_INDBLKSHIFT 14 /* 16k */ | |
61 | #define DNODE_BLOCK_SHIFT 14 /* 16k */ | |
62 | #define DNODE_CORE_SIZE 64 /* 64 bytes for dnode sans blkptrs */ | |
63 | #define DN_MAX_OBJECT_SHIFT 48 /* 256 trillion (zfs_fid_t limit) */ | |
64 | #define DN_MAX_OFFSET_SHIFT 64 /* 2^64 bytes in a dnode */ | |
65 | ||
428870ff BB |
66 | /* |
67 | * dnode id flags | |
68 | * | |
69 | * Note: a file will never ever have its | |
70 | * ids moved from bonus->spill | |
71 | * and only in a crypto environment would it be on spill | |
72 | */ | |
73 | #define DN_ID_CHKED_BONUS 0x1 | |
74 | #define DN_ID_CHKED_SPILL 0x2 | |
75 | #define DN_ID_OLD_EXIST 0x4 | |
76 | #define DN_ID_NEW_EXIST 0x8 | |
77 | ||
34dc7c2f BB |
78 | /* |
79 | * Derived constants. | |
80 | */ | |
81 | #define DNODE_SIZE (1 << DNODE_SHIFT) | |
82 | #define DN_MAX_NBLKPTR ((DNODE_SIZE - DNODE_CORE_SIZE) >> SPA_BLKPTRSHIFT) | |
83 | #define DN_MAX_BONUSLEN (DNODE_SIZE - DNODE_CORE_SIZE - (1 << SPA_BLKPTRSHIFT)) | |
84 | #define DN_MAX_OBJECT (1ULL << DN_MAX_OBJECT_SHIFT) | |
85 | #define DN_ZERO_BONUSLEN (DN_MAX_BONUSLEN + 1) | |
428870ff | 86 | #define DN_KILL_SPILLBLK (1) |
34dc7c2f BB |
87 | |
88 | #define DNODES_PER_BLOCK_SHIFT (DNODE_BLOCK_SHIFT - DNODE_SHIFT) | |
89 | #define DNODES_PER_BLOCK (1ULL << DNODES_PER_BLOCK_SHIFT) | |
90 | #define DNODES_PER_LEVEL_SHIFT (DN_MAX_INDBLKSHIFT - SPA_BLKPTRSHIFT) | |
428870ff | 91 | #define DNODES_PER_LEVEL (1ULL << DNODES_PER_LEVEL_SHIFT) |
34dc7c2f BB |
92 | |
93 | /* The +2 here is a cheesy way to round up */ | |
94 | #define DN_MAX_LEVELS (2 + ((DN_MAX_OFFSET_SHIFT - SPA_MINBLOCKSHIFT) / \ | |
95 | (DN_MIN_INDBLKSHIFT - SPA_BLKPTRSHIFT))) | |
96 | ||
97 | #define DN_BONUS(dnp) ((void*)((dnp)->dn_bonus + \ | |
98 | (((dnp)->dn_nblkptr - 1) * sizeof (blkptr_t)))) | |
99 | ||
100 | #define DN_USED_BYTES(dnp) (((dnp)->dn_flags & DNODE_FLAG_USED_BYTES) ? \ | |
101 | (dnp)->dn_used : (dnp)->dn_used << SPA_MINBLOCKSHIFT) | |
102 | ||
103 | #define EPB(blkshift, typeshift) (1 << (blkshift - typeshift)) | |
104 | ||
105 | struct dmu_buf_impl; | |
428870ff | 106 | struct objset; |
34dc7c2f BB |
107 | struct zio; |
108 | ||
109 | enum dnode_dirtycontext { | |
110 | DN_UNDIRTIED, | |
111 | DN_DIRTY_OPEN, | |
112 | DN_DIRTY_SYNC | |
113 | }; | |
114 | ||
115 | /* Is dn_used in bytes? if not, it's in multiples of SPA_MINBLOCKSIZE */ | |
9babb374 BB |
116 | #define DNODE_FLAG_USED_BYTES (1<<0) |
117 | #define DNODE_FLAG_USERUSED_ACCOUNTED (1<<1) | |
34dc7c2f | 118 | |
428870ff BB |
119 | /* Does dnode have a SA spill blkptr in bonus? */ |
120 | #define DNODE_FLAG_SPILL_BLKPTR (1<<2) | |
121 | ||
34dc7c2f BB |
122 | typedef struct dnode_phys { |
123 | uint8_t dn_type; /* dmu_object_type_t */ | |
124 | uint8_t dn_indblkshift; /* ln2(indirect block size) */ | |
125 | uint8_t dn_nlevels; /* 1=dn_blkptr->data blocks */ | |
126 | uint8_t dn_nblkptr; /* length of dn_blkptr */ | |
127 | uint8_t dn_bonustype; /* type of data in bonus buffer */ | |
128 | uint8_t dn_checksum; /* ZIO_CHECKSUM type */ | |
129 | uint8_t dn_compress; /* ZIO_COMPRESS type */ | |
130 | uint8_t dn_flags; /* DNODE_FLAG_* */ | |
131 | uint16_t dn_datablkszsec; /* data block size in 512b sectors */ | |
132 | uint16_t dn_bonuslen; /* length of dn_bonus */ | |
133 | uint8_t dn_pad2[4]; | |
134 | ||
135 | /* accounting is protected by dn_dirty_mtx */ | |
136 | uint64_t dn_maxblkid; /* largest allocated block ID */ | |
137 | uint64_t dn_used; /* bytes (or sectors) of disk space */ | |
138 | ||
139 | uint64_t dn_pad3[4]; | |
140 | ||
aca19e06 JE |
141 | /* |
142 | * The tail region is 448 bytes, and there are three ways to | |
143 | * look at it. | |
144 | * | |
145 | * 0 64 128 192 256 320 384 448 (offset) | |
146 | * +---------------+---------------+---------------+-------+ | |
147 | * | dn_blkptr[0] | dn_blkptr[1] | dn_blkptr[2] | / | | |
148 | * +---------------+---------------+---------------+-------+ | |
149 | * | dn_blkptr[0] | dn_bonus[0..319] | | |
150 | * +---------------+-----------------------+---------------+ | |
151 | * | dn_blkptr[0] | / | dn_spill | | |
152 | * +---------------+-----------------------+---------------+ | |
153 | */ | |
154 | union { | |
155 | blkptr_t dn_blkptr[1+DN_MAX_BONUSLEN/sizeof (blkptr_t)]; | |
156 | struct { | |
157 | blkptr_t __dn_ignore1; | |
158 | uint8_t dn_bonus[DN_MAX_BONUSLEN]; | |
159 | }; | |
160 | struct { | |
161 | blkptr_t __dn_ignore2; | |
162 | uint8_t __dn_ignore3[DN_MAX_BONUSLEN-sizeof (blkptr_t)]; | |
163 | blkptr_t dn_spill; | |
164 | }; | |
165 | }; | |
34dc7c2f BB |
166 | } dnode_phys_t; |
167 | ||
168 | typedef struct dnode { | |
169 | /* | |
d3cc8b15 WA |
170 | * Protects the structure of the dnode, including the number of levels |
171 | * of indirection (dn_nlevels), dn_maxblkid, and dn_next_* | |
34dc7c2f BB |
172 | */ |
173 | krwlock_t dn_struct_rwlock; | |
174 | ||
9babb374 | 175 | /* Our link on dn_objset->os_dnodes list; protected by os_lock. */ |
34dc7c2f BB |
176 | list_node_t dn_link; |
177 | ||
178 | /* immutable: */ | |
428870ff | 179 | struct objset *dn_objset; |
34dc7c2f BB |
180 | uint64_t dn_object; |
181 | struct dmu_buf_impl *dn_dbuf; | |
572e2857 | 182 | struct dnode_handle *dn_handle; |
34dc7c2f BB |
183 | dnode_phys_t *dn_phys; /* pointer into dn->dn_dbuf->db.db_data */ |
184 | ||
185 | /* | |
186 | * Copies of stuff in dn_phys. They're valid in the open | |
187 | * context (eg. even before the dnode is first synced). | |
188 | * Where necessary, these are protected by dn_struct_rwlock. | |
189 | */ | |
190 | dmu_object_type_t dn_type; /* object type */ | |
191 | uint16_t dn_bonuslen; /* bonus length */ | |
192 | uint8_t dn_bonustype; /* bonus type */ | |
193 | uint8_t dn_nblkptr; /* number of blkptrs (immutable) */ | |
194 | uint8_t dn_checksum; /* ZIO_CHECKSUM type */ | |
195 | uint8_t dn_compress; /* ZIO_COMPRESS type */ | |
196 | uint8_t dn_nlevels; | |
197 | uint8_t dn_indblkshift; | |
198 | uint8_t dn_datablkshift; /* zero if blksz not power of 2! */ | |
572e2857 | 199 | uint8_t dn_moved; /* Has this dnode been moved? */ |
34dc7c2f BB |
200 | uint16_t dn_datablkszsec; /* in 512b sectors */ |
201 | uint32_t dn_datablksz; /* in bytes */ | |
202 | uint64_t dn_maxblkid; | |
fa86b5db | 203 | uint8_t dn_next_type[TXG_SIZE]; |
d164b209 | 204 | uint8_t dn_next_nblkptr[TXG_SIZE]; |
34dc7c2f BB |
205 | uint8_t dn_next_nlevels[TXG_SIZE]; |
206 | uint8_t dn_next_indblkshift[TXG_SIZE]; | |
428870ff BB |
207 | uint8_t dn_next_bonustype[TXG_SIZE]; |
208 | uint8_t dn_rm_spillblk[TXG_SIZE]; /* for removing spill blk */ | |
34dc7c2f BB |
209 | uint16_t dn_next_bonuslen[TXG_SIZE]; |
210 | uint32_t dn_next_blksz[TXG_SIZE]; /* next block size in bytes */ | |
211 | ||
572e2857 BB |
212 | /* protected by dn_dbufs_mtx; declared here to fill 32-bit hole */ |
213 | uint32_t dn_dbufs_count; /* count of dn_dbufs */ | |
b663a23d MA |
214 | /* There are no level-0 blocks of this blkid or higher in dn_dbufs */ |
215 | uint64_t dn_unlisted_l0_blkid; | |
572e2857 | 216 | |
34dc7c2f BB |
217 | /* protected by os_lock: */ |
218 | list_node_t dn_dirty_link[TXG_SIZE]; /* next on dataset's dirty */ | |
219 | ||
220 | /* protected by dn_mtx: */ | |
221 | kmutex_t dn_mtx; | |
222 | list_t dn_dirty_records[TXG_SIZE]; | |
9bd274dd | 223 | struct range_tree *dn_free_ranges[TXG_SIZE]; |
34dc7c2f BB |
224 | uint64_t dn_allocated_txg; |
225 | uint64_t dn_free_txg; | |
226 | uint64_t dn_assigned_txg; | |
227 | kcondvar_t dn_notxholds; | |
228 | enum dnode_dirtycontext dn_dirtyctx; | |
229 | uint8_t *dn_dirtyctx_firstset; /* dbg: contents meaningless */ | |
230 | ||
231 | /* protected by own devices */ | |
232 | refcount_t dn_tx_holds; | |
233 | refcount_t dn_holds; | |
234 | ||
235 | kmutex_t dn_dbufs_mtx; | |
8951cb8d | 236 | avl_tree_t dn_dbufs; /* descendent dbufs */ |
572e2857 BB |
237 | |
238 | /* protected by dn_struct_rwlock */ | |
34dc7c2f | 239 | struct dmu_buf_impl *dn_bonus; /* bonus buffer dbuf */ |
572e2857 | 240 | |
428870ff | 241 | boolean_t dn_have_spill; /* have spill or are spilling */ |
34dc7c2f BB |
242 | |
243 | /* parent IO for current sync write */ | |
244 | zio_t *dn_zio; | |
245 | ||
9babb374 | 246 | /* used in syncing context */ |
428870ff BB |
247 | uint64_t dn_oldused; /* old phys used bytes */ |
248 | uint64_t dn_oldflags; /* old phys dn_flags */ | |
249 | uint64_t dn_olduid, dn_oldgid; | |
250 | uint64_t dn_newuid, dn_newgid; | |
251 | int dn_id_flags; | |
9babb374 | 252 | |
34dc7c2f BB |
253 | /* holds prefetch structure */ |
254 | struct zfetch dn_zfetch; | |
255 | } dnode_t; | |
256 | ||
572e2857 BB |
257 | /* |
258 | * Adds a level of indirection between the dbuf and the dnode to avoid | |
259 | * iterating descendent dbufs in dnode_move(). Handles are not allocated | |
260 | * individually, but as an array of child dnodes in dnode_hold_impl(). | |
261 | */ | |
262 | typedef struct dnode_handle { | |
263 | /* Protects dnh_dnode from modification by dnode_move(). */ | |
264 | zrlock_t dnh_zrlock; | |
265 | dnode_t *dnh_dnode; | |
266 | } dnode_handle_t; | |
267 | ||
268 | typedef struct dnode_children { | |
269 | size_t dnc_count; /* number of children */ | |
270 | dnode_handle_t dnc_children[1]; /* sized dynamically */ | |
271 | } dnode_children_t; | |
272 | ||
34dc7c2f BB |
273 | typedef struct free_range { |
274 | avl_node_t fr_node; | |
275 | uint64_t fr_blkid; | |
276 | uint64_t fr_nblks; | |
277 | } free_range_t; | |
278 | ||
428870ff | 279 | dnode_t *dnode_special_open(struct objset *dd, dnode_phys_t *dnp, |
572e2857 BB |
280 | uint64_t object, dnode_handle_t *dnh); |
281 | void dnode_special_close(dnode_handle_t *dnh); | |
34dc7c2f BB |
282 | |
283 | void dnode_setbonuslen(dnode_t *dn, int newsize, dmu_tx_t *tx); | |
428870ff BB |
284 | void dnode_setbonus_type(dnode_t *dn, dmu_object_type_t, dmu_tx_t *tx); |
285 | void dnode_rm_spill(dnode_t *dn, dmu_tx_t *tx); | |
286 | ||
287 | int dnode_hold(struct objset *dd, uint64_t object, | |
34dc7c2f | 288 | void *ref, dnode_t **dnp); |
428870ff | 289 | int dnode_hold_impl(struct objset *dd, uint64_t object, int flag, |
34dc7c2f BB |
290 | void *ref, dnode_t **dnp); |
291 | boolean_t dnode_add_ref(dnode_t *dn, void *ref); | |
292 | void dnode_rele(dnode_t *dn, void *ref); | |
4c7b7eed | 293 | void dnode_rele_and_unlock(dnode_t *dn, void *tag); |
34dc7c2f BB |
294 | void dnode_setdirty(dnode_t *dn, dmu_tx_t *tx); |
295 | void dnode_sync(dnode_t *dn, dmu_tx_t *tx); | |
296 | void dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs, | |
297 | dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx); | |
298 | void dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, | |
299 | dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx); | |
300 | void dnode_free(dnode_t *dn, dmu_tx_t *tx); | |
301 | void dnode_byteswap(dnode_phys_t *dnp); | |
302 | void dnode_buf_byteswap(void *buf, size_t size); | |
303 | void dnode_verify(dnode_t *dn); | |
304 | int dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx); | |
34dc7c2f | 305 | void dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx); |
34dc7c2f BB |
306 | void dnode_diduse_space(dnode_t *dn, int64_t space); |
307 | void dnode_willuse_space(dnode_t *dn, int64_t space, dmu_tx_t *tx); | |
b128c09f | 308 | void dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx, boolean_t); |
34dc7c2f BB |
309 | uint64_t dnode_block_freed(dnode_t *dn, uint64_t blkid); |
310 | void dnode_init(void); | |
311 | void dnode_fini(void); | |
b128c09f BB |
312 | int dnode_next_offset(dnode_t *dn, int flags, uint64_t *off, |
313 | int minlvl, uint64_t blkfill, uint64_t txg); | |
34dc7c2f | 314 | void dnode_evict_dbufs(dnode_t *dn); |
4c7b7eed | 315 | void dnode_evict_bonus(dnode_t *dn); |
34dc7c2f BB |
316 | |
317 | #ifdef ZFS_DEBUG | |
318 | ||
319 | /* | |
320 | * There should be a ## between the string literal and fmt, to make it | |
321 | * clear that we're joining two strings together, but that piece of shit | |
322 | * gcc doesn't support that preprocessor token. | |
323 | */ | |
324 | #define dprintf_dnode(dn, fmt, ...) do { \ | |
325 | if (zfs_flags & ZFS_DEBUG_DPRINTF) { \ | |
326 | char __db_buf[32]; \ | |
327 | uint64_t __db_obj = (dn)->dn_object; \ | |
328 | if (__db_obj == DMU_META_DNODE_OBJECT) \ | |
329 | (void) strcpy(__db_buf, "mdn"); \ | |
330 | else \ | |
331 | (void) snprintf(__db_buf, sizeof (__db_buf), "%lld", \ | |
332 | (u_longlong_t)__db_obj);\ | |
333 | dprintf_ds((dn)->dn_objset->os_dsl_dataset, "obj=%s " fmt, \ | |
334 | __db_buf, __VA_ARGS__); \ | |
335 | } \ | |
336 | _NOTE(CONSTCOND) } while (0) | |
337 | ||
338 | #define DNODE_VERIFY(dn) dnode_verify(dn) | |
339 | #define FREE_VERIFY(db, start, end, tx) free_verify(db, start, end, tx) | |
340 | ||
341 | #else | |
342 | ||
343 | #define dprintf_dnode(db, fmt, ...) | |
344 | #define DNODE_VERIFY(dn) | |
345 | #define FREE_VERIFY(db, start, end, tx) | |
346 | ||
347 | #endif | |
348 | ||
349 | #ifdef __cplusplus | |
350 | } | |
351 | #endif | |
352 | ||
353 | #endif /* _SYS_DNODE_H */ |