OpenZFS 7004 - dmu_tx_hold_zap() does dnode_hold() 7x on same object
[zfs.git] / include / sys / dnode.h
blob50e3399157e7e08562e9825f574566a98e063971
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
24 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27 #ifndef _SYS_DNODE_H
28 #define _SYS_DNODE_H
30 #include <sys/zfs_context.h>
31 #include <sys/avl.h>
32 #include <sys/spa.h>
33 #include <sys/txg.h>
34 #include <sys/zio.h>
35 #include <sys/refcount.h>
36 #include <sys/dmu_zfetch.h>
37 #include <sys/zrlock.h>
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
44 * dnode_hold() flags.
46 #define DNODE_MUST_BE_ALLOCATED 1
47 #define DNODE_MUST_BE_FREE 2
50 * dnode_next_offset() flags.
52 #define DNODE_FIND_HOLE 1
53 #define DNODE_FIND_BACKWARDS 2
54 #define DNODE_FIND_HAVELOCK 4
57 * Fixed constants.
59 #define DNODE_SHIFT 9 /* 512 bytes */
60 #define DN_MIN_INDBLKSHIFT 12 /* 4k */
62 * If we ever increase this value beyond 20, we need to revisit all logic that
63 * does x << level * ebps to handle overflow. With a 1M indirect block size,
64 * 4 levels of indirect blocks would not be able to guarantee addressing an
65 * entire object, so 5 levels will be used, but 5 * (20 - 7) = 65.
67 #define DN_MAX_INDBLKSHIFT 14 /* 16k */
68 #define DNODE_BLOCK_SHIFT 14 /* 16k */
69 #define DNODE_CORE_SIZE 64 /* 64 bytes for dnode sans blkptrs */
70 #define DN_MAX_OBJECT_SHIFT 48 /* 256 trillion (zfs_fid_t limit) */
71 #define DN_MAX_OFFSET_SHIFT 64 /* 2^64 bytes in a dnode */
74 * dnode id flags
76 * Note: a file will never ever have its
77 * ids moved from bonus->spill
78 * and only in a crypto environment would it be on spill
80 #define DN_ID_CHKED_BONUS 0x1
81 #define DN_ID_CHKED_SPILL 0x2
82 #define DN_ID_OLD_EXIST 0x4
83 #define DN_ID_NEW_EXIST 0x8
86 * Derived constants.
88 #define DNODE_MIN_SIZE (1 << DNODE_SHIFT)
89 #define DNODE_MAX_SIZE (1 << DNODE_BLOCK_SHIFT)
90 #define DNODE_BLOCK_SIZE (1 << DNODE_BLOCK_SHIFT)
91 #define DNODE_MIN_SLOTS (DNODE_MIN_SIZE >> DNODE_SHIFT)
92 #define DNODE_MAX_SLOTS (DNODE_MAX_SIZE >> DNODE_SHIFT)
93 #define DN_BONUS_SIZE(dnsize) ((dnsize) - DNODE_CORE_SIZE - \
94 (1 << SPA_BLKPTRSHIFT))
95 #define DN_SLOTS_TO_BONUSLEN(slots) DN_BONUS_SIZE((slots) << DNODE_SHIFT)
96 #define DN_OLD_MAX_BONUSLEN (DN_BONUS_SIZE(DNODE_MIN_SIZE))
97 #define DN_MAX_NBLKPTR ((DNODE_MIN_SIZE - DNODE_CORE_SIZE) >> SPA_BLKPTRSHIFT)
98 #define DN_MAX_OBJECT (1ULL << DN_MAX_OBJECT_SHIFT)
99 #define DN_ZERO_BONUSLEN (DN_BONUS_SIZE(DNODE_MAX_SIZE) + 1)
100 #define DN_KILL_SPILLBLK (1)
102 #define DNODES_PER_BLOCK_SHIFT (DNODE_BLOCK_SHIFT - DNODE_SHIFT)
103 #define DNODES_PER_BLOCK (1ULL << DNODES_PER_BLOCK_SHIFT)
104 #define DNODES_PER_LEVEL_SHIFT (DN_MAX_INDBLKSHIFT - SPA_BLKPTRSHIFT)
105 #define DNODES_PER_LEVEL (1ULL << DNODES_PER_LEVEL_SHIFT)
107 /* The +2 here is a cheesy way to round up */
108 #define DN_MAX_LEVELS (2 + ((DN_MAX_OFFSET_SHIFT - SPA_MINBLOCKSHIFT) / \
109 (DN_MIN_INDBLKSHIFT - SPA_BLKPTRSHIFT)))
111 #define DN_BONUS(dnp) ((void*)((dnp)->dn_bonus + \
112 (((dnp)->dn_nblkptr - 1) * sizeof (blkptr_t))))
114 #define DN_USED_BYTES(dnp) (((dnp)->dn_flags & DNODE_FLAG_USED_BYTES) ? \
115 (dnp)->dn_used : (dnp)->dn_used << SPA_MINBLOCKSHIFT)
117 #define EPB(blkshift, typeshift) (1 << (blkshift - typeshift))
119 struct dmu_buf_impl;
120 struct objset;
121 struct zio;
123 enum dnode_dirtycontext {
124 DN_UNDIRTIED,
125 DN_DIRTY_OPEN,
126 DN_DIRTY_SYNC
129 /* Is dn_used in bytes? if not, it's in multiples of SPA_MINBLOCKSIZE */
130 #define DNODE_FLAG_USED_BYTES (1<<0)
131 #define DNODE_FLAG_USERUSED_ACCOUNTED (1<<1)
133 /* Does dnode have a SA spill blkptr in bonus? */
134 #define DNODE_FLAG_SPILL_BLKPTR (1<<2)
136 typedef struct dnode_phys {
137 uint8_t dn_type; /* dmu_object_type_t */
138 uint8_t dn_indblkshift; /* ln2(indirect block size) */
139 uint8_t dn_nlevels; /* 1=dn_blkptr->data blocks */
140 uint8_t dn_nblkptr; /* length of dn_blkptr */
141 uint8_t dn_bonustype; /* type of data in bonus buffer */
142 uint8_t dn_checksum; /* ZIO_CHECKSUM type */
143 uint8_t dn_compress; /* ZIO_COMPRESS type */
144 uint8_t dn_flags; /* DNODE_FLAG_* */
145 uint16_t dn_datablkszsec; /* data block size in 512b sectors */
146 uint16_t dn_bonuslen; /* length of dn_bonus */
147 uint8_t dn_extra_slots; /* # of subsequent slots consumed */
148 uint8_t dn_pad2[3];
150 /* accounting is protected by dn_dirty_mtx */
151 uint64_t dn_maxblkid; /* largest allocated block ID */
152 uint64_t dn_used; /* bytes (or sectors) of disk space */
154 uint64_t dn_pad3[4];
157 * The tail region is 448 bytes for a 512 byte dnode, and
158 * correspondingly larger for larger dnode sizes. The spill
159 * block pointer, when present, is always at the end of the tail
160 * region. There are three ways this space may be used, using
161 * a 512 byte dnode for this diagram:
163 * 0 64 128 192 256 320 384 448 (offset)
164 * +---------------+---------------+---------------+-------+
165 * | dn_blkptr[0] | dn_blkptr[1] | dn_blkptr[2] | / |
166 * +---------------+---------------+---------------+-------+
167 * | dn_blkptr[0] | dn_bonus[0..319] |
168 * +---------------+-----------------------+---------------+
169 * | dn_blkptr[0] | dn_bonus[0..191] | dn_spill |
170 * +---------------+-----------------------+---------------+
172 union {
173 blkptr_t dn_blkptr[1+DN_OLD_MAX_BONUSLEN/sizeof (blkptr_t)];
174 struct {
175 blkptr_t __dn_ignore1;
176 uint8_t dn_bonus[DN_OLD_MAX_BONUSLEN];
178 struct {
179 blkptr_t __dn_ignore2;
180 uint8_t __dn_ignore3[DN_OLD_MAX_BONUSLEN -
181 sizeof (blkptr_t)];
182 blkptr_t dn_spill;
185 } dnode_phys_t;
187 #define DN_SPILL_BLKPTR(dnp) (blkptr_t *)((char *)(dnp) + \
188 (((dnp)->dn_extra_slots + 1) << DNODE_SHIFT) - (1 << SPA_BLKPTRSHIFT))
190 struct dnode {
192 * Protects the structure of the dnode, including the number of levels
193 * of indirection (dn_nlevels), dn_maxblkid, and dn_next_*
195 krwlock_t dn_struct_rwlock;
197 /* Our link on dn_objset->os_dnodes list; protected by os_lock. */
198 list_node_t dn_link;
200 /* immutable: */
201 struct objset *dn_objset;
202 uint64_t dn_object;
203 struct dmu_buf_impl *dn_dbuf;
204 struct dnode_handle *dn_handle;
205 dnode_phys_t *dn_phys; /* pointer into dn->dn_dbuf->db.db_data */
208 * Copies of stuff in dn_phys. They're valid in the open
209 * context (eg. even before the dnode is first synced).
210 * Where necessary, these are protected by dn_struct_rwlock.
212 dmu_object_type_t dn_type; /* object type */
213 uint16_t dn_bonuslen; /* bonus length */
214 uint8_t dn_bonustype; /* bonus type */
215 uint8_t dn_nblkptr; /* number of blkptrs (immutable) */
216 uint8_t dn_checksum; /* ZIO_CHECKSUM type */
217 uint8_t dn_compress; /* ZIO_COMPRESS type */
218 uint8_t dn_nlevels;
219 uint8_t dn_indblkshift;
220 uint8_t dn_datablkshift; /* zero if blksz not power of 2! */
221 uint8_t dn_moved; /* Has this dnode been moved? */
222 uint16_t dn_datablkszsec; /* in 512b sectors */
223 uint32_t dn_datablksz; /* in bytes */
224 uint64_t dn_maxblkid;
225 uint8_t dn_next_type[TXG_SIZE];
226 uint8_t dn_num_slots; /* metadnode slots consumed on disk */
227 uint8_t dn_next_nblkptr[TXG_SIZE];
228 uint8_t dn_next_nlevels[TXG_SIZE];
229 uint8_t dn_next_indblkshift[TXG_SIZE];
230 uint8_t dn_next_bonustype[TXG_SIZE];
231 uint8_t dn_rm_spillblk[TXG_SIZE]; /* for removing spill blk */
232 uint16_t dn_next_bonuslen[TXG_SIZE];
233 uint32_t dn_next_blksz[TXG_SIZE]; /* next block size in bytes */
235 /* protected by dn_dbufs_mtx; declared here to fill 32-bit hole */
236 uint32_t dn_dbufs_count; /* count of dn_dbufs */
237 /* There are no level-0 blocks of this blkid or higher in dn_dbufs */
238 uint64_t dn_unlisted_l0_blkid;
240 /* protected by os_lock: */
241 list_node_t dn_dirty_link[TXG_SIZE]; /* next on dataset's dirty */
243 /* protected by dn_mtx: */
244 kmutex_t dn_mtx;
245 list_t dn_dirty_records[TXG_SIZE];
246 struct range_tree *dn_free_ranges[TXG_SIZE];
247 uint64_t dn_allocated_txg;
248 uint64_t dn_free_txg;
249 uint64_t dn_assigned_txg;
250 kcondvar_t dn_notxholds;
251 enum dnode_dirtycontext dn_dirtyctx;
252 uint8_t *dn_dirtyctx_firstset; /* dbg: contents meaningless */
254 /* protected by own devices */
255 refcount_t dn_tx_holds;
256 refcount_t dn_holds;
258 kmutex_t dn_dbufs_mtx;
260 * Descendent dbufs, ordered by dbuf_compare. Note that dn_dbufs
261 * can contain multiple dbufs of the same (level, blkid) when a
262 * dbuf is marked DB_EVICTING without being removed from
263 * dn_dbufs. To maintain the avl invariant that there cannot be
264 * duplicate entries, we order the dbufs by an arbitrary value -
265 * their address in memory. This means that dn_dbufs cannot be used to
266 * directly look up a dbuf. Instead, callers must use avl_walk, have
267 * a reference to the dbuf, or look up a non-existant node with
268 * db_state = DB_SEARCH (see dbuf_free_range for an example).
270 avl_tree_t dn_dbufs;
272 /* protected by dn_struct_rwlock */
273 struct dmu_buf_impl *dn_bonus; /* bonus buffer dbuf */
275 boolean_t dn_have_spill; /* have spill or are spilling */
277 /* parent IO for current sync write */
278 zio_t *dn_zio;
280 /* used in syncing context */
281 uint64_t dn_oldused; /* old phys used bytes */
282 uint64_t dn_oldflags; /* old phys dn_flags */
283 uint64_t dn_olduid, dn_oldgid;
284 uint64_t dn_newuid, dn_newgid;
285 int dn_id_flags;
287 /* holds prefetch structure */
288 struct zfetch dn_zfetch;
292 * Adds a level of indirection between the dbuf and the dnode to avoid
293 * iterating descendent dbufs in dnode_move(). Handles are not allocated
294 * individually, but as an array of child dnodes in dnode_hold_impl().
296 typedef struct dnode_handle {
297 /* Protects dnh_dnode from modification by dnode_move(). */
298 zrlock_t dnh_zrlock;
299 dnode_t *dnh_dnode;
300 } dnode_handle_t;
302 typedef struct dnode_children {
303 dmu_buf_user_t dnc_dbu; /* User evict data */
304 size_t dnc_count; /* number of children */
305 dnode_handle_t dnc_children[]; /* sized dynamically */
306 } dnode_children_t;
308 typedef struct free_range {
309 avl_node_t fr_node;
310 uint64_t fr_blkid;
311 uint64_t fr_nblks;
312 } free_range_t;
314 void dnode_special_open(struct objset *dd, dnode_phys_t *dnp,
315 uint64_t object, dnode_handle_t *dnh);
316 void dnode_special_close(dnode_handle_t *dnh);
318 void dnode_setbonuslen(dnode_t *dn, int newsize, dmu_tx_t *tx);
319 void dnode_setbonus_type(dnode_t *dn, dmu_object_type_t, dmu_tx_t *tx);
320 void dnode_rm_spill(dnode_t *dn, dmu_tx_t *tx);
322 int dnode_hold(struct objset *dd, uint64_t object,
323 void *ref, dnode_t **dnp);
324 int dnode_hold_impl(struct objset *dd, uint64_t object, int flag, int dn_slots,
325 void *ref, dnode_t **dnp);
326 boolean_t dnode_add_ref(dnode_t *dn, void *ref);
327 void dnode_rele(dnode_t *dn, void *ref);
328 void dnode_rele_and_unlock(dnode_t *dn, void *tag);
329 void dnode_setdirty(dnode_t *dn, dmu_tx_t *tx);
330 void dnode_sync(dnode_t *dn, dmu_tx_t *tx);
331 void dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs,
332 dmu_object_type_t bonustype, int bonuslen, int dn_slots, dmu_tx_t *tx);
333 void dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize,
334 dmu_object_type_t bonustype, int bonuslen, int dn_slots, dmu_tx_t *tx);
335 void dnode_free(dnode_t *dn, dmu_tx_t *tx);
336 void dnode_byteswap(dnode_phys_t *dnp);
337 void dnode_buf_byteswap(void *buf, size_t size);
338 void dnode_verify(dnode_t *dn);
339 int dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx);
340 void dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx);
341 void dnode_diduse_space(dnode_t *dn, int64_t space);
342 void dnode_willuse_space(dnode_t *dn, int64_t space, dmu_tx_t *tx);
343 void dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx, boolean_t);
344 uint64_t dnode_block_freed(dnode_t *dn, uint64_t blkid);
345 void dnode_init(void);
346 void dnode_fini(void);
347 int dnode_next_offset(dnode_t *dn, int flags, uint64_t *off,
348 int minlvl, uint64_t blkfill, uint64_t txg);
349 void dnode_evict_dbufs(dnode_t *dn);
350 void dnode_evict_bonus(dnode_t *dn);
352 #ifdef ZFS_DEBUG
355 * There should be a ## between the string literal and fmt, to make it
356 * clear that we're joining two strings together, but that piece of shit
357 * gcc doesn't support that preprocessor token.
359 #define dprintf_dnode(dn, fmt, ...) do { \
360 if (zfs_flags & ZFS_DEBUG_DPRINTF) { \
361 char __db_buf[32]; \
362 uint64_t __db_obj = (dn)->dn_object; \
363 if (__db_obj == DMU_META_DNODE_OBJECT) \
364 (void) strcpy(__db_buf, "mdn"); \
365 else \
366 (void) snprintf(__db_buf, sizeof (__db_buf), "%lld", \
367 (u_longlong_t)__db_obj);\
368 dprintf_ds((dn)->dn_objset->os_dsl_dataset, "obj=%s " fmt, \
369 __db_buf, __VA_ARGS__); \
371 _NOTE(CONSTCOND) } while (0)
373 #define DNODE_VERIFY(dn) dnode_verify(dn)
374 #define FREE_VERIFY(db, start, end, tx) free_verify(db, start, end, tx)
376 #else
378 #define dprintf_dnode(db, fmt, ...)
379 #define DNODE_VERIFY(dn)
380 #define FREE_VERIFY(db, start, end, tx)
382 #endif
384 #ifdef __cplusplus
386 #endif
388 #endif /* _SYS_DNODE_H */