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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
24 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
30 #include <sys/zfs_context.h>
35 #include <sys/refcount.h>
36 #include <sys/dmu_zfetch.h>
37 #include <sys/zrlock.h>
38 #include <sys/multilist.h>
47 #define DNODE_MUST_BE_ALLOCATED 1
48 #define DNODE_MUST_BE_FREE 2
51 * dnode_next_offset() flags.
53 #define DNODE_FIND_HOLE 1
54 #define DNODE_FIND_BACKWARDS 2
55 #define DNODE_FIND_HAVELOCK 4
60 #define DNODE_SHIFT 9 /* 512 bytes */
61 #define DN_MIN_INDBLKSHIFT 12 /* 4k */
63 * If we ever increase this value beyond 20, we need to revisit all logic that
64 * does x << level * ebps to handle overflow. With a 1M indirect block size,
65 * 4 levels of indirect blocks would not be able to guarantee addressing an
66 * entire object, so 5 levels will be used, but 5 * (20 - 7) = 65.
68 #define DN_MAX_INDBLKSHIFT 17 /* 128k */
69 #define DNODE_BLOCK_SHIFT 14 /* 16k */
70 #define DNODE_CORE_SIZE 64 /* 64 bytes for dnode sans blkptrs */
71 #define DN_MAX_OBJECT_SHIFT 48 /* 256 trillion (zfs_fid_t limit) */
72 #define DN_MAX_OFFSET_SHIFT 64 /* 2^64 bytes in a dnode */
77 * Note: a file will never ever have its
78 * ids moved from bonus->spill
79 * and only in a crypto environment would it be on spill
81 #define DN_ID_CHKED_BONUS 0x1
82 #define DN_ID_CHKED_SPILL 0x2
83 #define DN_ID_OLD_EXIST 0x4
84 #define DN_ID_NEW_EXIST 0x8
89 #define DNODE_MIN_SIZE (1 << DNODE_SHIFT)
90 #define DNODE_MAX_SIZE (1 << DNODE_BLOCK_SHIFT)
91 #define DNODE_BLOCK_SIZE (1 << DNODE_BLOCK_SHIFT)
92 #define DNODE_MIN_SLOTS (DNODE_MIN_SIZE >> DNODE_SHIFT)
93 #define DNODE_MAX_SLOTS (DNODE_MAX_SIZE >> DNODE_SHIFT)
94 #define DN_BONUS_SIZE(dnsize) ((dnsize) - DNODE_CORE_SIZE - \
95 (1 << SPA_BLKPTRSHIFT))
96 #define DN_SLOTS_TO_BONUSLEN(slots) DN_BONUS_SIZE((slots) << DNODE_SHIFT)
97 #define DN_OLD_MAX_BONUSLEN (DN_BONUS_SIZE(DNODE_MIN_SIZE))
98 #define DN_MAX_NBLKPTR ((DNODE_MIN_SIZE - DNODE_CORE_SIZE) >> SPA_BLKPTRSHIFT)
99 #define DN_MAX_OBJECT (1ULL << DN_MAX_OBJECT_SHIFT)
100 #define DN_ZERO_BONUSLEN (DN_BONUS_SIZE(DNODE_MAX_SIZE) + 1)
101 #define DN_KILL_SPILLBLK (1)
103 #define DNODES_PER_BLOCK_SHIFT (DNODE_BLOCK_SHIFT - DNODE_SHIFT)
104 #define DNODES_PER_BLOCK (1ULL << DNODES_PER_BLOCK_SHIFT)
107 * This is inaccurate if the indblkshift of the particular object is not the
108 * max. But it's only used by userland to calculate the zvol reservation.
110 #define DNODES_PER_LEVEL_SHIFT (DN_MAX_INDBLKSHIFT - SPA_BLKPTRSHIFT)
111 #define DNODES_PER_LEVEL (1ULL << DNODES_PER_LEVEL_SHIFT)
113 /* The +2 here is a cheesy way to round up */
114 #define DN_MAX_LEVELS (2 + ((DN_MAX_OFFSET_SHIFT - SPA_MINBLOCKSHIFT) / \
115 (DN_MIN_INDBLKSHIFT - SPA_BLKPTRSHIFT)))
117 #define DN_BONUS(dnp) ((void*)((dnp)->dn_bonus + \
118 (((dnp)->dn_nblkptr - 1) * sizeof (blkptr_t))))
119 #define DN_MAX_BONUS_LEN(dnp) \
120 ((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) ? \
121 (uint8_t *)DN_SPILL_BLKPTR(dnp) - (uint8_t *)DN_BONUS(dnp) : \
122 (uint8_t *)(dnp + (dnp->dn_extra_slots + 1)) - (uint8_t *)DN_BONUS(dnp))
124 #define DN_USED_BYTES(dnp) (((dnp)->dn_flags & DNODE_FLAG_USED_BYTES) ? \
125 (dnp)->dn_used : (dnp)->dn_used << SPA_MINBLOCKSHIFT)
127 #define EPB(blkshift, typeshift) (1 << (blkshift - typeshift))
133 enum dnode_dirtycontext {
139 /* Is dn_used in bytes? if not, it's in multiples of SPA_MINBLOCKSIZE */
140 #define DNODE_FLAG_USED_BYTES (1<<0)
141 #define DNODE_FLAG_USERUSED_ACCOUNTED (1<<1)
143 /* Does dnode have a SA spill blkptr in bonus? */
144 #define DNODE_FLAG_SPILL_BLKPTR (1<<2)
146 typedef struct dnode_phys {
147 uint8_t dn_type; /* dmu_object_type_t */
148 uint8_t dn_indblkshift; /* ln2(indirect block size) */
149 uint8_t dn_nlevels; /* 1=dn_blkptr->data blocks */
150 uint8_t dn_nblkptr; /* length of dn_blkptr */
151 uint8_t dn_bonustype; /* type of data in bonus buffer */
152 uint8_t dn_checksum; /* ZIO_CHECKSUM type */
153 uint8_t dn_compress; /* ZIO_COMPRESS type */
154 uint8_t dn_flags; /* DNODE_FLAG_* */
155 uint16_t dn_datablkszsec; /* data block size in 512b sectors */
156 uint16_t dn_bonuslen; /* length of dn_bonus */
157 uint8_t dn_extra_slots; /* # of subsequent slots consumed */
160 /* accounting is protected by dn_dirty_mtx */
161 uint64_t dn_maxblkid; /* largest allocated block ID */
162 uint64_t dn_used; /* bytes (or sectors) of disk space */
165 * Both dn_pad2 and dn_pad3 are protected by the block's MAC. This
166 * allows us to protect any fields that might be added here in the
167 * future. In either case, developers will want to check
168 * zio_crypt_init_uios_dnode() to ensure the new field is being
169 * protected properly.
173 * The tail region is 448 bytes for a 512 byte dnode, and
174 * correspondingly larger for larger dnode sizes. The spill
175 * block pointer, when present, is always at the end of the tail
176 * region. There are three ways this space may be used, using
177 * a 512 byte dnode for this diagram:
179 * 0 64 128 192 256 320 384 448 (offset)
180 * +---------------+---------------+---------------+-------+
181 * | dn_blkptr[0] | dn_blkptr[1] | dn_blkptr[2] | / |
182 * +---------------+---------------+---------------+-------+
183 * | dn_blkptr[0] | dn_bonus[0..319] |
184 * +---------------+-----------------------+---------------+
185 * | dn_blkptr[0] | dn_bonus[0..191] | dn_spill |
186 * +---------------+-----------------------+---------------+
189 blkptr_t dn_blkptr[1+DN_OLD_MAX_BONUSLEN/sizeof (blkptr_t)];
191 blkptr_t __dn_ignore1;
192 uint8_t dn_bonus[DN_OLD_MAX_BONUSLEN];
195 blkptr_t __dn_ignore2;
196 uint8_t __dn_ignore3[DN_OLD_MAX_BONUSLEN -
203 #define DN_SPILL_BLKPTR(dnp) (blkptr_t *)((char *)(dnp) + \
204 (((dnp)->dn_extra_slots + 1) << DNODE_SHIFT) - (1 << SPA_BLKPTRSHIFT))
208 * Protects the structure of the dnode, including the number of levels
209 * of indirection (dn_nlevels), dn_maxblkid, and dn_next_*
211 krwlock_t dn_struct_rwlock;
213 /* Our link on dn_objset->os_dnodes list; protected by os_lock. */
217 struct objset *dn_objset;
219 struct dmu_buf_impl *dn_dbuf;
220 struct dnode_handle *dn_handle;
221 dnode_phys_t *dn_phys; /* pointer into dn->dn_dbuf->db.db_data */
224 * Copies of stuff in dn_phys. They're valid in the open
225 * context (eg. even before the dnode is first synced).
226 * Where necessary, these are protected by dn_struct_rwlock.
228 dmu_object_type_t dn_type; /* object type */
229 uint16_t dn_bonuslen; /* bonus length */
230 uint8_t dn_bonustype; /* bonus type */
231 uint8_t dn_nblkptr; /* number of blkptrs (immutable) */
232 uint8_t dn_checksum; /* ZIO_CHECKSUM type */
233 uint8_t dn_compress; /* ZIO_COMPRESS type */
235 uint8_t dn_indblkshift;
236 uint8_t dn_datablkshift; /* zero if blksz not power of 2! */
237 uint8_t dn_moved; /* Has this dnode been moved? */
238 uint16_t dn_datablkszsec; /* in 512b sectors */
239 uint32_t dn_datablksz; /* in bytes */
240 uint64_t dn_maxblkid;
241 uint8_t dn_next_type[TXG_SIZE];
242 uint8_t dn_num_slots; /* metadnode slots consumed on disk */
243 uint8_t dn_next_nblkptr[TXG_SIZE];
244 uint8_t dn_next_nlevels[TXG_SIZE];
245 uint8_t dn_next_indblkshift[TXG_SIZE];
246 uint8_t dn_next_bonustype[TXG_SIZE];
247 uint8_t dn_rm_spillblk[TXG_SIZE]; /* for removing spill blk */
248 uint16_t dn_next_bonuslen[TXG_SIZE];
249 uint32_t dn_next_blksz[TXG_SIZE]; /* next block size in bytes */
251 /* protected by dn_dbufs_mtx; declared here to fill 32-bit hole */
252 uint32_t dn_dbufs_count; /* count of dn_dbufs */
254 /* protected by os_lock: */
255 multilist_node_t dn_dirty_link[TXG_SIZE]; /* next on dataset's dirty */
257 /* protected by dn_mtx: */
259 list_t dn_dirty_records[TXG_SIZE];
260 struct range_tree *dn_free_ranges[TXG_SIZE];
261 uint64_t dn_allocated_txg;
262 uint64_t dn_free_txg;
263 uint64_t dn_assigned_txg;
264 kcondvar_t dn_notxholds;
265 enum dnode_dirtycontext dn_dirtyctx;
266 uint8_t *dn_dirtyctx_firstset; /* dbg: contents meaningless */
268 /* protected by own devices */
269 refcount_t dn_tx_holds;
272 kmutex_t dn_dbufs_mtx;
274 * Descendent dbufs, ordered by dbuf_compare. Note that dn_dbufs
275 * can contain multiple dbufs of the same (level, blkid) when a
276 * dbuf is marked DB_EVICTING without being removed from
277 * dn_dbufs. To maintain the avl invariant that there cannot be
278 * duplicate entries, we order the dbufs by an arbitrary value -
279 * their address in memory. This means that dn_dbufs cannot be used to
280 * directly look up a dbuf. Instead, callers must use avl_walk, have
281 * a reference to the dbuf, or look up a non-existant node with
282 * db_state = DB_SEARCH (see dbuf_free_range for an example).
286 /* protected by dn_struct_rwlock */
287 struct dmu_buf_impl *dn_bonus; /* bonus buffer dbuf */
289 boolean_t dn_have_spill; /* have spill or are spilling */
291 /* parent IO for current sync write */
294 /* used in syncing context */
295 uint64_t dn_oldused; /* old phys used bytes */
296 uint64_t dn_oldflags; /* old phys dn_flags */
297 uint64_t dn_olduid, dn_oldgid;
298 uint64_t dn_newuid, dn_newgid;
301 /* holds prefetch structure */
302 struct zfetch dn_zfetch;
306 * Adds a level of indirection between the dbuf and the dnode to avoid
307 * iterating descendent dbufs in dnode_move(). Handles are not allocated
308 * individually, but as an array of child dnodes in dnode_hold_impl().
310 typedef struct dnode_handle {
311 /* Protects dnh_dnode from modification by dnode_move(). */
316 typedef struct dnode_children {
317 dmu_buf_user_t dnc_dbu; /* User evict data */
318 size_t dnc_count; /* number of children */
319 dnode_handle_t dnc_children[]; /* sized dynamically */
322 typedef struct free_range {
328 void dnode_special_open(struct objset *dd, dnode_phys_t *dnp,
329 uint64_t object, dnode_handle_t *dnh);
330 void dnode_special_close(dnode_handle_t *dnh);
332 void dnode_setbonuslen(dnode_t *dn, int newsize, dmu_tx_t *tx);
333 void dnode_setbonus_type(dnode_t *dn, dmu_object_type_t, dmu_tx_t *tx);
334 void dnode_rm_spill(dnode_t *dn, dmu_tx_t *tx);
336 int dnode_hold(struct objset *dd, uint64_t object,
337 void *ref, dnode_t **dnp);
338 int dnode_hold_impl(struct objset *dd, uint64_t object, int flag, int dn_slots,
339 void *ref, dnode_t **dnp);
340 boolean_t dnode_add_ref(dnode_t *dn, void *ref);
341 void dnode_rele(dnode_t *dn, void *ref);
342 void dnode_rele_and_unlock(dnode_t *dn, void *tag, boolean_t evicting);
343 void dnode_setdirty(dnode_t *dn, dmu_tx_t *tx);
344 void dnode_sync(dnode_t *dn, dmu_tx_t *tx);
345 void dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs,
346 dmu_object_type_t bonustype, int bonuslen, int dn_slots, dmu_tx_t *tx);
347 void dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize,
348 dmu_object_type_t bonustype, int bonuslen, int dn_slots, dmu_tx_t *tx);
349 void dnode_free(dnode_t *dn, dmu_tx_t *tx);
350 void dnode_byteswap(dnode_phys_t *dnp);
351 void dnode_buf_byteswap(void *buf, size_t size);
352 void dnode_verify(dnode_t *dn);
353 int dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx);
354 void dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx);
355 void dnode_diduse_space(dnode_t *dn, int64_t space);
356 void dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx, boolean_t);
357 uint64_t dnode_block_freed(dnode_t *dn, uint64_t blkid);
358 void dnode_init(void);
359 void dnode_fini(void);
360 int dnode_next_offset(dnode_t *dn, int flags, uint64_t *off,
361 int minlvl, uint64_t blkfill, uint64_t txg);
362 void dnode_evict_dbufs(dnode_t *dn);
363 void dnode_evict_bonus(dnode_t *dn);
364 boolean_t dnode_needs_remap(const dnode_t *dn);
366 #define DNODE_IS_CACHEABLE(_dn) \
367 ((_dn)->dn_objset->os_primary_cache == ZFS_CACHE_ALL || \
368 (DMU_OT_IS_METADATA((_dn)->dn_type) && \
369 (_dn)->dn_objset->os_primary_cache == ZFS_CACHE_METADATA))
371 #define DNODE_META_IS_CACHEABLE(_dn) \
372 ((_dn)->dn_objset->os_primary_cache == ZFS_CACHE_ALL || \
373 (_dn)->dn_objset->os_primary_cache == ZFS_CACHE_METADATA)
378 * There should be a ## between the string literal and fmt, to make it
379 * clear that we're joining two strings together, but that piece of shit
380 * gcc doesn't support that preprocessor token.
382 #define dprintf_dnode(dn, fmt, ...) do { \
383 if (zfs_flags & ZFS_DEBUG_DPRINTF) { \
385 uint64_t __db_obj = (dn)->dn_object; \
386 if (__db_obj == DMU_META_DNODE_OBJECT) \
387 (void) strcpy(__db_buf, "mdn"); \
389 (void) snprintf(__db_buf, sizeof (__db_buf), "%lld", \
390 (u_longlong_t)__db_obj);\
391 dprintf_ds((dn)->dn_objset->os_dsl_dataset, "obj=%s " fmt, \
392 __db_buf, __VA_ARGS__); \
394 _NOTE(CONSTCOND) } while (0)
396 #define DNODE_VERIFY(dn) dnode_verify(dn)
397 #define FREE_VERIFY(db, start, end, tx) free_verify(db, start, end, tx)
401 #define dprintf_dnode(db, fmt, ...)
402 #define DNODE_VERIFY(dn)
403 #define FREE_VERIFY(db, start, end, tx)
411 #endif /* _SYS_DNODE_H */