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