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