xref: /freebsd/sys/contrib/openzfs/include/sys/dnode.h (revision ce4dcb97ca433b2a2f03fbae957dae0ff16f6f51)
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 https://opensource.org/licenses/CDDL-1.0.
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 /*
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.
25  */
26 
27 #ifndef	_SYS_DNODE_H
28 #define	_SYS_DNODE_H
29 
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/zfs_refcount.h>
36 #include <sys/dmu_zfetch.h>
37 #include <sys/zrlock.h>
38 #include <sys/multilist.h>
39 #include <sys/wmsum.h>
40 
41 #ifdef	__cplusplus
42 extern "C" {
43 #endif
44 
45 /*
46  * dnode_hold() flags.
47  */
48 #define	DNODE_MUST_BE_ALLOCATED	1
49 #define	DNODE_MUST_BE_FREE	2
50 #define	DNODE_DRY_RUN		4
51 
52 /*
53  * dnode_next_offset() flags.
54  */
55 #define	DNODE_FIND_HOLE		1
56 #define	DNODE_FIND_BACKWARDS	2
57 #define	DNODE_FIND_HAVELOCK	4
58 
59 /*
60  * Fixed constants.
61  */
62 #define	DNODE_SHIFT		9	/* 512 bytes */
63 #define	DN_MIN_INDBLKSHIFT	12	/* 4k */
64 /*
65  * If we ever increase this value beyond 20, we need to revisit all logic that
66  * does x << level * ebps to handle overflow.  With a 1M indirect block size,
67  * 4 levels of indirect blocks would not be able to guarantee addressing an
68  * entire object, so 5 levels will be used, but 5 * (20 - 7) = 65.
69  */
70 #define	DN_MAX_INDBLKSHIFT	17	/* 128k */
71 #define	DNODE_BLOCK_SHIFT	14	/* 16k */
72 #define	DNODE_CORE_SIZE		64	/* 64 bytes for dnode sans blkptrs */
73 #define	DN_MAX_OBJECT_SHIFT	48	/* 256 trillion (zfs_fid_t limit) */
74 #define	DN_MAX_OFFSET_SHIFT	64	/* 2^64 bytes in a dnode */
75 
76 /*
77  * dnode id flags
78  *
79  * Note: a file will never ever have its ids moved from bonus->spill
80  */
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
85 
86 /*
87  * Derived constants.
88  */
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)
102 
103 #define	DN_SLOT_UNINIT		((void *)NULL)	/* Uninitialized */
104 #define	DN_SLOT_FREE		((void *)1UL)	/* Free slot */
105 #define	DN_SLOT_ALLOCATED	((void *)2UL)	/* Allocated slot */
106 #define	DN_SLOT_INTERIOR	((void *)3UL)	/* Interior allocated slot */
107 #define	DN_SLOT_IS_PTR(dn)	((void *)dn > DN_SLOT_INTERIOR)
108 #define	DN_SLOT_IS_VALID(dn)	((void *)dn != NULL)
109 
110 #define	DNODES_PER_BLOCK_SHIFT	(DNODE_BLOCK_SHIFT - DNODE_SHIFT)
111 #define	DNODES_PER_BLOCK	(1ULL << DNODES_PER_BLOCK_SHIFT)
112 
113 /*
114  * This is inaccurate if the indblkshift of the particular object is not the
115  * max.  But it's only used by userland to calculate the zvol reservation.
116  */
117 #define	DNODES_PER_LEVEL_SHIFT	(DN_MAX_INDBLKSHIFT - SPA_BLKPTRSHIFT)
118 #define	DNODES_PER_LEVEL	(1ULL << DNODES_PER_LEVEL_SHIFT)
119 
120 #define	DN_MAX_LEVELS	(DIV_ROUND_UP(DN_MAX_OFFSET_SHIFT - SPA_MINBLOCKSHIFT, \
121 	DN_MIN_INDBLKSHIFT - SPA_BLKPTRSHIFT) + 1)
122 
123 /*
124  * Use the flexible array instead of the fixed length one dn_bonus
125  * to address memcpy/memmove fortify error
126  */
127 #define	DN_BONUS(dnp)	((void*)((dnp)->dn_bonus_flexible + \
128 	(((dnp)->dn_nblkptr - 1) * sizeof (blkptr_t))))
129 #define	DN_MAX_BONUS_LEN(dnp) \
130 	((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) ? \
131 	(uint8_t *)DN_SPILL_BLKPTR(dnp) - (uint8_t *)DN_BONUS(dnp) : \
132 	(uint8_t *)(dnp + (dnp->dn_extra_slots + 1)) - (uint8_t *)DN_BONUS(dnp))
133 
134 #define	DN_USED_BYTES(dnp) (((dnp)->dn_flags & DNODE_FLAG_USED_BYTES) ? \
135 	(dnp)->dn_used : (dnp)->dn_used << SPA_MINBLOCKSHIFT)
136 
137 #define	EPB(blkshift, typeshift)	(1 << (blkshift - typeshift))
138 
139 struct dmu_buf_impl;
140 struct objset;
141 struct zio;
142 
143 enum dnode_dirtycontext {
144 	DN_UNDIRTIED,
145 	DN_DIRTY_OPEN,
146 	DN_DIRTY_SYNC
147 };
148 
149 /* Is dn_used in bytes?  if not, it's in multiples of SPA_MINBLOCKSIZE */
150 #define	DNODE_FLAG_USED_BYTES			(1 << 0)
151 #define	DNODE_FLAG_USERUSED_ACCOUNTED		(1 << 1)
152 
153 /* Does dnode have a SA spill blkptr in bonus? */
154 #define	DNODE_FLAG_SPILL_BLKPTR			(1 << 2)
155 
156 /* User/Group/Project dnode accounting */
157 #define	DNODE_FLAG_USEROBJUSED_ACCOUNTED	(1 << 3)
158 
159 /*
160  * This mask defines the set of flags which are "portable", meaning
161  * that they can be preserved when doing a raw encrypted zfs send.
162  * Flags included in this mask will be protected by AAD when the block
163  * of dnodes is encrypted.
164  */
165 #define	DNODE_CRYPT_PORTABLE_FLAGS_MASK		(DNODE_FLAG_SPILL_BLKPTR)
166 
167 /*
168  * VARIABLE-LENGTH (LARGE) DNODES
169  *
170  * The motivation for variable-length dnodes is to eliminate the overhead
171  * associated with using spill blocks.  Spill blocks are used to store
172  * system attribute data (i.e. file metadata) that does not fit in the
173  * dnode's bonus buffer. By allowing a larger bonus buffer area the use of
174  * a spill block can be avoided.  Spill blocks potentially incur an
175  * additional read I/O for every dnode in a dnode block. As a worst case
176  * example, reading 32 dnodes from a 16k dnode block and all of the spill
177  * blocks could issue 33 separate reads. Now suppose those dnodes have size
178  * 1024 and therefore don't need spill blocks. Then the worst case number
179  * of blocks read is reduced from 33 to two--one per dnode block.
180  *
181  * ZFS-on-Linux systems that make heavy use of extended attributes benefit
182  * from this feature. In particular, ZFS-on-Linux supports the xattr=sa
183  * dataset property which allows file extended attribute data to be stored
184  * in the dnode bonus buffer as an alternative to the traditional
185  * directory-based format. Workloads such as SELinux and the Lustre
186  * distributed filesystem often store enough xattr data to force spill
187  * blocks when xattr=sa is in effect. Large dnodes may therefore provide a
188  * performance benefit to such systems. Other use cases that benefit from
189  * this feature include files with large ACLs and symbolic links with long
190  * target names.
191  *
192  * The size of a dnode may be a multiple of 512 bytes up to the size of a
193  * dnode block (currently 16384 bytes). The dn_extra_slots field of the
194  * on-disk dnode_phys_t structure describes the size of the physical dnode
195  * on disk. The field represents how many "extra" dnode_phys_t slots a
196  * dnode consumes in its dnode block. This convention results in a value of
197  * 0 for 512 byte dnodes which preserves on-disk format compatibility with
198  * older software which doesn't support large dnodes.
199  *
200  * Similarly, the in-memory dnode_t structure has a dn_num_slots field
201  * to represent the total number of dnode_phys_t slots consumed on disk.
202  * Thus dn->dn_num_slots is 1 greater than the corresponding
203  * dnp->dn_extra_slots. This difference in convention was adopted
204  * because, unlike on-disk structures, backward compatibility is not a
205  * concern for in-memory objects, so we used a more natural way to
206  * represent size for a dnode_t.
207  *
208  * The default size for newly created dnodes is determined by the value of
209  * the "dnodesize" dataset property. By default the property is set to
210  * "legacy" which is compatible with older software. Setting the property
211  * to "auto" will allow the filesystem to choose the most suitable dnode
212  * size. Currently this just sets the default dnode size to 1k, but future
213  * code improvements could dynamically choose a size based on observed
214  * workload patterns. Dnodes of varying sizes can coexist within the same
215  * dataset and even within the same dnode block.
216  */
217 
218 typedef struct dnode_phys {
219 	uint8_t dn_type;		/* dmu_object_type_t */
220 	uint8_t dn_indblkshift;		/* ln2(indirect block size) */
221 	uint8_t dn_nlevels;		/* 1=dn_blkptr->data blocks */
222 	uint8_t dn_nblkptr;		/* length of dn_blkptr */
223 	uint8_t dn_bonustype;		/* type of data in bonus buffer */
224 	uint8_t	dn_checksum;		/* ZIO_CHECKSUM type */
225 	uint8_t	dn_compress;		/* ZIO_COMPRESS type */
226 	uint8_t dn_flags;		/* DNODE_FLAG_* */
227 	uint16_t dn_datablkszsec;	/* data block size in 512b sectors */
228 	uint16_t dn_bonuslen;		/* length of dn_bonus */
229 	uint8_t dn_extra_slots;		/* # of subsequent slots consumed */
230 	uint8_t dn_pad2[3];
231 
232 	/* accounting is protected by dn_dirty_mtx */
233 	uint64_t dn_maxblkid;		/* largest allocated block ID */
234 	uint64_t dn_used;		/* bytes (or sectors) of disk space */
235 
236 	/*
237 	 * Both dn_pad2 and dn_pad3 are protected by the block's MAC. This
238 	 * allows us to protect any fields that might be added here in the
239 	 * future. In either case, developers will want to check
240 	 * zio_crypt_init_uios_dnode() and zio_crypt_do_dnode_hmac_updates()
241 	 * to ensure the new field is being protected and updated properly.
242 	 */
243 	uint64_t dn_pad3[4];
244 
245 	/*
246 	 * The tail region is 448 bytes for a 512 byte dnode, and
247 	 * correspondingly larger for larger dnode sizes. The spill
248 	 * block pointer, when present, is always at the end of the tail
249 	 * region. There are three ways this space may be used, using
250 	 * a 512 byte dnode for this diagram:
251 	 *
252 	 * 0       64      128     192     256     320     384     448 (offset)
253 	 * +---------------+---------------+---------------+-------+
254 	 * | dn_blkptr[0]  | dn_blkptr[1]  | dn_blkptr[2]  | /     |
255 	 * +---------------+---------------+---------------+-------+
256 	 * | dn_blkptr[0]  | dn_bonus[0..319]                      |
257 	 * +---------------+-----------------------+---------------+
258 	 * | dn_blkptr[0]  | dn_bonus[0..191]      | dn_spill      |
259 	 * +---------------+-----------------------+---------------+
260 	 */
261 	union {
262 		blkptr_t dn_blkptr[1+DN_OLD_MAX_BONUSLEN/sizeof (blkptr_t)];
263 		struct {
264 			blkptr_t __dn_ignore1;
265 			uint8_t dn_bonus[DN_OLD_MAX_BONUSLEN];
266 		};
267 		struct {
268 			blkptr_t __dn_ignore2;
269 			uint8_t __dn_ignore3[DN_OLD_MAX_BONUSLEN -
270 			    sizeof (blkptr_t)];
271 			blkptr_t dn_spill;
272 		};
273 		struct {
274 			blkptr_t __dn_ignore4;
275 			uint8_t dn_bonus_flexible[];
276 		};
277 	};
278 } dnode_phys_t;
279 
280 #define	DN_SPILL_BLKPTR(dnp)	((blkptr_t *)((char *)(dnp) + \
281 	(((dnp)->dn_extra_slots + 1) << DNODE_SHIFT) - (1 << SPA_BLKPTRSHIFT)))
282 
283 struct dnode {
284 	/*
285 	 * Protects the structure of the dnode, including the number of levels
286 	 * of indirection (dn_nlevels), dn_maxblkid, and dn_next_*
287 	 */
288 	krwlock_t dn_struct_rwlock;
289 
290 	/* Our link on dn_objset->os_dnodes list; protected by os_lock.  */
291 	list_node_t dn_link;
292 
293 	/* immutable: */
294 	struct objset *dn_objset;
295 	uint64_t dn_object;
296 	struct dmu_buf_impl *dn_dbuf;
297 	struct dnode_handle *dn_handle;
298 	dnode_phys_t *dn_phys; /* pointer into dn->dn_dbuf->db.db_data */
299 
300 	/*
301 	 * Copies of stuff in dn_phys.  They're valid in the open
302 	 * context (eg. even before the dnode is first synced).
303 	 * Where necessary, these are protected by dn_struct_rwlock.
304 	 */
305 	dmu_object_type_t dn_type;	/* object type */
306 	uint16_t dn_bonuslen;		/* bonus length */
307 	uint8_t dn_bonustype;		/* bonus type */
308 	uint8_t dn_nblkptr;		/* number of blkptrs (immutable) */
309 	uint8_t dn_checksum;		/* ZIO_CHECKSUM type */
310 	uint8_t dn_compress;		/* ZIO_COMPRESS type */
311 	uint8_t dn_nlevels;
312 	uint8_t dn_indblkshift;
313 	uint8_t dn_datablkshift;	/* zero if blksz not power of 2! */
314 	uint8_t dn_moved;		/* Has this dnode been moved? */
315 	uint16_t dn_datablkszsec;	/* in 512b sectors */
316 	uint32_t dn_datablksz;		/* in bytes */
317 	uint64_t dn_maxblkid;
318 	uint8_t dn_next_type[TXG_SIZE];
319 	uint8_t dn_num_slots;		/* metadnode slots consumed on disk */
320 	uint8_t dn_next_nblkptr[TXG_SIZE];
321 	uint8_t dn_next_nlevels[TXG_SIZE];
322 	uint8_t dn_next_indblkshift[TXG_SIZE];
323 	uint8_t dn_next_bonustype[TXG_SIZE];
324 	uint8_t dn_rm_spillblk[TXG_SIZE];	/* for removing spill blk */
325 	uint16_t dn_next_bonuslen[TXG_SIZE];
326 	uint32_t dn_next_blksz[TXG_SIZE];	/* next block size in bytes */
327 	uint64_t dn_next_maxblkid[TXG_SIZE];	/* next maxblkid in bytes */
328 
329 	/* protected by dn_dbufs_mtx; declared here to fill 32-bit hole */
330 	uint32_t dn_dbufs_count;	/* count of dn_dbufs */
331 
332 	/* protected by os_lock: */
333 	multilist_node_t dn_dirty_link[TXG_SIZE]; /* next on dataset's dirty */
334 
335 	/* protected by dn_mtx: */
336 	kmutex_t dn_mtx;
337 	list_t dn_dirty_records[TXG_SIZE];
338 	struct range_tree *dn_free_ranges[TXG_SIZE];
339 	uint64_t dn_allocated_txg;
340 	uint64_t dn_free_txg;
341 	uint64_t dn_assigned_txg;
342 	uint64_t dn_dirty_txg;			/* txg dnode was last dirtied */
343 	kcondvar_t dn_notxholds;
344 	kcondvar_t dn_nodnholds;
345 	enum dnode_dirtycontext dn_dirtyctx;
346 	const void *dn_dirtyctx_firstset;	/* dbg: contents meaningless */
347 
348 	/* protected by own devices */
349 	zfs_refcount_t dn_tx_holds;
350 	zfs_refcount_t dn_holds;
351 
352 	kmutex_t dn_dbufs_mtx;
353 	/*
354 	 * Descendent dbufs, ordered by dbuf_compare. Note that dn_dbufs
355 	 * can contain multiple dbufs of the same (level, blkid) when a
356 	 * dbuf is marked DB_EVICTING without being removed from
357 	 * dn_dbufs. To maintain the avl invariant that there cannot be
358 	 * duplicate entries, we order the dbufs by an arbitrary value -
359 	 * their address in memory. This means that dn_dbufs cannot be used to
360 	 * directly look up a dbuf. Instead, callers must use avl_walk, have
361 	 * a reference to the dbuf, or look up a non-existent node with
362 	 * db_state = DB_SEARCH (see dbuf_free_range for an example).
363 	 */
364 	avl_tree_t dn_dbufs;
365 
366 	/* protected by dn_struct_rwlock */
367 	struct dmu_buf_impl *dn_bonus;	/* bonus buffer dbuf */
368 
369 	boolean_t dn_have_spill;	/* have spill or are spilling */
370 
371 	/* parent IO for current sync write */
372 	zio_t *dn_zio;
373 
374 	/* used in syncing context */
375 	uint64_t dn_oldused;	/* old phys used bytes */
376 	uint64_t dn_oldflags;	/* old phys dn_flags */
377 	uint64_t dn_olduid, dn_oldgid, dn_oldprojid;
378 	uint64_t dn_newuid, dn_newgid, dn_newprojid;
379 	int dn_id_flags;
380 
381 	/* holds prefetch structure */
382 	struct zfetch	dn_zfetch;
383 
384 	/* Not in dn_phys, but should be. set it after taking a hold */
385 	dmu_object_type_t dn_storage_type;	/* type for storage class */
386 };
387 
388 /*
389  * Since AVL already has embedded element counter, use dn_dbufs_count
390  * only for dbufs not counted there (bonus buffers) and just add them.
391  */
392 #define	DN_DBUFS_COUNT(dn)	((dn)->dn_dbufs_count + \
393     avl_numnodes(&(dn)->dn_dbufs))
394 
395 /*
396  * We use this (otherwise unused) bit to indicate if the value of
397  * dn_next_maxblkid[txgoff] is valid to use in dnode_sync().
398  */
399 #define	DMU_NEXT_MAXBLKID_SET		(1ULL << 63)
400 
401 /*
402  * Adds a level of indirection between the dbuf and the dnode to avoid
403  * iterating descendent dbufs in dnode_move(). Handles are not allocated
404  * individually, but as an array of child dnodes in dnode_hold_impl().
405  */
406 typedef struct dnode_handle {
407 	/* Protects dnh_dnode from modification by dnode_move(). */
408 	zrlock_t dnh_zrlock;
409 	dnode_t *dnh_dnode;
410 } dnode_handle_t;
411 
412 typedef struct dnode_children {
413 	dmu_buf_user_t dnc_dbu;		/* User evict data */
414 	size_t dnc_count;		/* number of children */
415 	dnode_handle_t dnc_children[];	/* sized dynamically */
416 } dnode_children_t;
417 
418 typedef struct free_range {
419 	avl_node_t fr_node;
420 	uint64_t fr_blkid;
421 	uint64_t fr_nblks;
422 } free_range_t;
423 
424 void dnode_special_open(struct objset *dd, dnode_phys_t *dnp,
425     uint64_t object, dnode_handle_t *dnh);
426 void dnode_special_close(dnode_handle_t *dnh);
427 
428 void dnode_setbonuslen(dnode_t *dn, int newsize, dmu_tx_t *tx);
429 void dnode_setbonus_type(dnode_t *dn, dmu_object_type_t, dmu_tx_t *tx);
430 void dnode_rm_spill(dnode_t *dn, dmu_tx_t *tx);
431 
432 int dnode_hold(struct objset *dd, uint64_t object,
433     const void *ref, dnode_t **dnp);
434 int dnode_hold_impl(struct objset *dd, uint64_t object, int flag, int dn_slots,
435     const void *ref, dnode_t **dnp);
436 boolean_t dnode_add_ref(dnode_t *dn, const void *ref);
437 void dnode_rele(dnode_t *dn, const void *ref);
438 void dnode_rele_and_unlock(dnode_t *dn, const void *tag, boolean_t evicting);
439 int dnode_try_claim(objset_t *os, uint64_t object, int slots);
440 boolean_t dnode_is_dirty(dnode_t *dn);
441 void dnode_setdirty(dnode_t *dn, dmu_tx_t *tx);
442 void dnode_set_dirtyctx(dnode_t *dn, dmu_tx_t *tx, const void *tag);
443 void dnode_sync(dnode_t *dn, dmu_tx_t *tx);
444 void dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs,
445     dmu_object_type_t bonustype, int bonuslen, int dn_slots, dmu_tx_t *tx);
446 void dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize,
447     dmu_object_type_t bonustype, int bonuslen, int dn_slots,
448     boolean_t keep_spill, dmu_tx_t *tx);
449 void dnode_free(dnode_t *dn, dmu_tx_t *tx);
450 void dnode_byteswap(dnode_phys_t *dnp);
451 void dnode_buf_byteswap(void *buf, size_t size);
452 void dnode_verify(dnode_t *dn);
453 int dnode_set_nlevels(dnode_t *dn, int nlevels, dmu_tx_t *tx);
454 int dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx);
455 void dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx);
456 void dnode_diduse_space(dnode_t *dn, int64_t space);
457 void dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx,
458     boolean_t have_read, boolean_t force);
459 uint64_t dnode_block_freed(dnode_t *dn, uint64_t blkid);
460 void dnode_init(void);
461 void dnode_fini(void);
462 int dnode_next_offset(dnode_t *dn, int flags, uint64_t *off,
463     int minlvl, uint64_t blkfill, uint64_t txg);
464 void dnode_evict_dbufs(dnode_t *dn);
465 void dnode_evict_bonus(dnode_t *dn);
466 void dnode_free_interior_slots(dnode_t *dn);
467 
468 void dnode_set_storage_type(dnode_t *dn, dmu_object_type_t type);
469 
470 #define	DNODE_IS_DIRTY(_dn)						\
471 	((_dn)->dn_dirty_txg >= spa_syncing_txg((_dn)->dn_objset->os_spa))
472 
473 #define	DNODE_LEVEL_IS_CACHEABLE(_dn, _level)				\
474 	((_dn)->dn_objset->os_primary_cache == ZFS_CACHE_ALL ||		\
475 	(((_level) > 0 || DMU_OT_IS_METADATA((_dn)->dn_type)) &&	\
476 	(_dn)->dn_objset->os_primary_cache == ZFS_CACHE_METADATA))
477 
478 /*
479  * Used for dnodestats kstat.
480  */
481 typedef struct dnode_stats {
482 	/*
483 	 * Number of failed attempts to hold a meta dnode dbuf.
484 	 */
485 	kstat_named_t dnode_hold_dbuf_hold;
486 	/*
487 	 * Number of failed attempts to read a meta dnode dbuf.
488 	 */
489 	kstat_named_t dnode_hold_dbuf_read;
490 	/*
491 	 * Number of times dnode_hold(..., DNODE_MUST_BE_ALLOCATED) was able
492 	 * to hold the requested object number which was allocated.  This is
493 	 * the common case when looking up any allocated object number.
494 	 */
495 	kstat_named_t dnode_hold_alloc_hits;
496 	/*
497 	 * Number of times dnode_hold(..., DNODE_MUST_BE_ALLOCATED) was not
498 	 * able to hold the request object number because it was not allocated.
499 	 */
500 	kstat_named_t dnode_hold_alloc_misses;
501 	/*
502 	 * Number of times dnode_hold(..., DNODE_MUST_BE_ALLOCATED) was not
503 	 * able to hold the request object number because the object number
504 	 * refers to an interior large dnode slot.
505 	 */
506 	kstat_named_t dnode_hold_alloc_interior;
507 	/*
508 	 * Number of times dnode_hold(..., DNODE_MUST_BE_ALLOCATED) needed
509 	 * to retry acquiring slot zrl locks due to contention.
510 	 */
511 	kstat_named_t dnode_hold_alloc_lock_retry;
512 	/*
513 	 * Number of times dnode_hold(..., DNODE_MUST_BE_ALLOCATED) did not
514 	 * need to create the dnode because another thread did so after
515 	 * dropping the read lock but before acquiring the write lock.
516 	 */
517 	kstat_named_t dnode_hold_alloc_lock_misses;
518 	/*
519 	 * Number of times dnode_hold(..., DNODE_MUST_BE_ALLOCATED) found
520 	 * a free dnode instantiated by dnode_create() but not yet allocated
521 	 * by dnode_allocate().
522 	 */
523 	kstat_named_t dnode_hold_alloc_type_none;
524 	/*
525 	 * Number of times dnode_hold(..., DNODE_MUST_BE_FREE) was able
526 	 * to hold the requested range of free dnode slots.
527 	 */
528 	kstat_named_t dnode_hold_free_hits;
529 	/*
530 	 * Number of times dnode_hold(..., DNODE_MUST_BE_FREE) was not
531 	 * able to hold the requested range of free dnode slots because
532 	 * at least one slot was allocated.
533 	 */
534 	kstat_named_t dnode_hold_free_misses;
535 	/*
536 	 * Number of times dnode_hold(..., DNODE_MUST_BE_FREE) was not
537 	 * able to hold the requested range of free dnode slots because
538 	 * after acquiring the zrl lock at least one slot was allocated.
539 	 */
540 	kstat_named_t dnode_hold_free_lock_misses;
541 	/*
542 	 * Number of times dnode_hold(..., DNODE_MUST_BE_FREE) needed
543 	 * to retry acquiring slot zrl locks due to contention.
544 	 */
545 	kstat_named_t dnode_hold_free_lock_retry;
546 	/*
547 	 * Number of times dnode_hold(..., DNODE_MUST_BE_FREE) requested
548 	 * a range of dnode slots which were held by another thread.
549 	 */
550 	kstat_named_t dnode_hold_free_refcount;
551 	/*
552 	 * Number of times dnode_hold(..., DNODE_MUST_BE_FREE) requested
553 	 * a range of dnode slots which would overflow the dnode_phys_t.
554 	 */
555 	kstat_named_t dnode_hold_free_overflow;
556 	/*
557 	 * Number of times dnode_free_interior_slots() needed to retry
558 	 * acquiring a slot zrl lock due to contention.
559 	 */
560 	kstat_named_t dnode_free_interior_lock_retry;
561 	/*
562 	 * Number of new dnodes allocated by dnode_allocate().
563 	 */
564 	kstat_named_t dnode_allocate;
565 	/*
566 	 * Number of dnodes re-allocated by dnode_reallocate().
567 	 */
568 	kstat_named_t dnode_reallocate;
569 	/*
570 	 * Number of meta dnode dbufs evicted.
571 	 */
572 	kstat_named_t dnode_buf_evict;
573 	/*
574 	 * Number of times dmu_object_alloc*() reached the end of the existing
575 	 * object ID chunk and advanced to a new one.
576 	 */
577 	kstat_named_t dnode_alloc_next_chunk;
578 	/*
579 	 * Number of times multiple threads attempted to allocate a dnode
580 	 * from the same block of free dnodes.
581 	 */
582 	kstat_named_t dnode_alloc_race;
583 	/*
584 	 * Number of times dmu_object_alloc*() was forced to advance to the
585 	 * next meta dnode dbuf due to an error from  dmu_object_next().
586 	 */
587 	kstat_named_t dnode_alloc_next_block;
588 	/*
589 	 * Statistics for tracking dnodes which have been moved.
590 	 */
591 	kstat_named_t dnode_move_invalid;
592 	kstat_named_t dnode_move_recheck1;
593 	kstat_named_t dnode_move_recheck2;
594 	kstat_named_t dnode_move_special;
595 	kstat_named_t dnode_move_handle;
596 	kstat_named_t dnode_move_rwlock;
597 	kstat_named_t dnode_move_active;
598 } dnode_stats_t;
599 
600 typedef struct dnode_sums {
601 	wmsum_t dnode_hold_dbuf_hold;
602 	wmsum_t dnode_hold_dbuf_read;
603 	wmsum_t dnode_hold_alloc_hits;
604 	wmsum_t dnode_hold_alloc_misses;
605 	wmsum_t dnode_hold_alloc_interior;
606 	wmsum_t dnode_hold_alloc_lock_retry;
607 	wmsum_t dnode_hold_alloc_lock_misses;
608 	wmsum_t dnode_hold_alloc_type_none;
609 	wmsum_t dnode_hold_free_hits;
610 	wmsum_t dnode_hold_free_misses;
611 	wmsum_t dnode_hold_free_lock_misses;
612 	wmsum_t dnode_hold_free_lock_retry;
613 	wmsum_t dnode_hold_free_refcount;
614 	wmsum_t dnode_hold_free_overflow;
615 	wmsum_t dnode_free_interior_lock_retry;
616 	wmsum_t dnode_allocate;
617 	wmsum_t dnode_reallocate;
618 	wmsum_t dnode_buf_evict;
619 	wmsum_t dnode_alloc_next_chunk;
620 	wmsum_t dnode_alloc_race;
621 	wmsum_t dnode_alloc_next_block;
622 	wmsum_t dnode_move_invalid;
623 	wmsum_t dnode_move_recheck1;
624 	wmsum_t dnode_move_recheck2;
625 	wmsum_t dnode_move_special;
626 	wmsum_t dnode_move_handle;
627 	wmsum_t dnode_move_rwlock;
628 	wmsum_t dnode_move_active;
629 } dnode_sums_t;
630 
631 extern dnode_stats_t dnode_stats;
632 extern dnode_sums_t dnode_sums;
633 
634 #define	DNODE_STAT_INCR(stat, val) \
635     wmsum_add(&dnode_sums.stat, (val))
636 #define	DNODE_STAT_BUMP(stat) \
637     DNODE_STAT_INCR(stat, 1);
638 
639 #ifdef ZFS_DEBUG
640 
641 #define	dprintf_dnode(dn, fmt, ...) do { \
642 	if (zfs_flags & ZFS_DEBUG_DPRINTF) { \
643 	char __db_buf[32]; \
644 	uint64_t __db_obj = (dn)->dn_object; \
645 	if (__db_obj == DMU_META_DNODE_OBJECT) \
646 		(void) strlcpy(__db_buf, "mdn", sizeof (__db_buf));	\
647 	else \
648 		(void) snprintf(__db_buf, sizeof (__db_buf), "%lld", \
649 		    (u_longlong_t)__db_obj);\
650 	dprintf_ds((dn)->dn_objset->os_dsl_dataset, "obj=%s " fmt, \
651 	    __db_buf, __VA_ARGS__); \
652 	} \
653 } while (0)
654 
655 #define	DNODE_VERIFY(dn)		dnode_verify(dn)
656 #define	FREE_VERIFY(db, start, end, tx)	free_verify(db, start, end, tx)
657 
658 #else
659 
660 #define	dprintf_dnode(db, fmt, ...)
661 #define	DNODE_VERIFY(dn)		((void) sizeof ((uintptr_t)(dn)))
662 #define	FREE_VERIFY(db, start, end, tx)
663 
664 #endif
665 
666 #ifdef	__cplusplus
667 }
668 #endif
669 
670 #endif	/* _SYS_DNODE_H */
671