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