xref: /freebsd/sys/contrib/openzfs/include/sys/dnode.h (revision d411c1d6)
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 
385 /*
386  * Since AVL already has embedded element counter, use dn_dbufs_count
387  * only for dbufs not counted there (bonus buffers) and just add them.
388  */
389 #define	DN_DBUFS_COUNT(dn)	((dn)->dn_dbufs_count + \
390     avl_numnodes(&(dn)->dn_dbufs))
391 
392 /*
393  * We use this (otherwise unused) bit to indicate if the value of
394  * dn_next_maxblkid[txgoff] is valid to use in dnode_sync().
395  */
396 #define	DMU_NEXT_MAXBLKID_SET		(1ULL << 63)
397 
398 /*
399  * Adds a level of indirection between the dbuf and the dnode to avoid
400  * iterating descendent dbufs in dnode_move(). Handles are not allocated
401  * individually, but as an array of child dnodes in dnode_hold_impl().
402  */
403 typedef struct dnode_handle {
404 	/* Protects dnh_dnode from modification by dnode_move(). */
405 	zrlock_t dnh_zrlock;
406 	dnode_t *dnh_dnode;
407 } dnode_handle_t;
408 
409 typedef struct dnode_children {
410 	dmu_buf_user_t dnc_dbu;		/* User evict data */
411 	size_t dnc_count;		/* number of children */
412 	dnode_handle_t dnc_children[];	/* sized dynamically */
413 } dnode_children_t;
414 
415 typedef struct free_range {
416 	avl_node_t fr_node;
417 	uint64_t fr_blkid;
418 	uint64_t fr_nblks;
419 } free_range_t;
420 
421 void dnode_special_open(struct objset *dd, dnode_phys_t *dnp,
422     uint64_t object, dnode_handle_t *dnh);
423 void dnode_special_close(dnode_handle_t *dnh);
424 
425 void dnode_setbonuslen(dnode_t *dn, int newsize, dmu_tx_t *tx);
426 void dnode_setbonus_type(dnode_t *dn, dmu_object_type_t, dmu_tx_t *tx);
427 void dnode_rm_spill(dnode_t *dn, dmu_tx_t *tx);
428 
429 int dnode_hold(struct objset *dd, uint64_t object,
430     const void *ref, dnode_t **dnp);
431 int dnode_hold_impl(struct objset *dd, uint64_t object, int flag, int dn_slots,
432     const void *ref, dnode_t **dnp);
433 boolean_t dnode_add_ref(dnode_t *dn, const void *ref);
434 void dnode_rele(dnode_t *dn, const void *ref);
435 void dnode_rele_and_unlock(dnode_t *dn, const void *tag, boolean_t evicting);
436 int dnode_try_claim(objset_t *os, uint64_t object, int slots);
437 boolean_t dnode_is_dirty(dnode_t *dn);
438 void dnode_setdirty(dnode_t *dn, dmu_tx_t *tx);
439 void dnode_set_dirtyctx(dnode_t *dn, dmu_tx_t *tx, const void *tag);
440 void dnode_sync(dnode_t *dn, dmu_tx_t *tx);
441 void dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs,
442     dmu_object_type_t bonustype, int bonuslen, int dn_slots, dmu_tx_t *tx);
443 void dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize,
444     dmu_object_type_t bonustype, int bonuslen, int dn_slots,
445     boolean_t keep_spill, dmu_tx_t *tx);
446 void dnode_free(dnode_t *dn, dmu_tx_t *tx);
447 void dnode_byteswap(dnode_phys_t *dnp);
448 void dnode_buf_byteswap(void *buf, size_t size);
449 void dnode_verify(dnode_t *dn);
450 int dnode_set_nlevels(dnode_t *dn, int nlevels, dmu_tx_t *tx);
451 int dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx);
452 void dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx);
453 void dnode_diduse_space(dnode_t *dn, int64_t space);
454 void dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx,
455     boolean_t have_read, boolean_t force);
456 uint64_t dnode_block_freed(dnode_t *dn, uint64_t blkid);
457 void dnode_init(void);
458 void dnode_fini(void);
459 int dnode_next_offset(dnode_t *dn, int flags, uint64_t *off,
460     int minlvl, uint64_t blkfill, uint64_t txg);
461 void dnode_evict_dbufs(dnode_t *dn);
462 void dnode_evict_bonus(dnode_t *dn);
463 void dnode_free_interior_slots(dnode_t *dn);
464 
465 #define	DNODE_IS_DIRTY(_dn)						\
466 	((_dn)->dn_dirty_txg >= spa_syncing_txg((_dn)->dn_objset->os_spa))
467 
468 #define	DNODE_LEVEL_IS_CACHEABLE(_dn, _level)				\
469 	((_dn)->dn_objset->os_primary_cache == ZFS_CACHE_ALL ||		\
470 	(((_level) > 0 || DMU_OT_IS_METADATA((_dn)->dn_type)) &&	\
471 	(_dn)->dn_objset->os_primary_cache == ZFS_CACHE_METADATA))
472 
473 /*
474  * Used for dnodestats kstat.
475  */
476 typedef struct dnode_stats {
477 	/*
478 	 * Number of failed attempts to hold a meta dnode dbuf.
479 	 */
480 	kstat_named_t dnode_hold_dbuf_hold;
481 	/*
482 	 * Number of failed attempts to read a meta dnode dbuf.
483 	 */
484 	kstat_named_t dnode_hold_dbuf_read;
485 	/*
486 	 * Number of times dnode_hold(..., DNODE_MUST_BE_ALLOCATED) was able
487 	 * to hold the requested object number which was allocated.  This is
488 	 * the common case when looking up any allocated object number.
489 	 */
490 	kstat_named_t dnode_hold_alloc_hits;
491 	/*
492 	 * Number of times dnode_hold(..., DNODE_MUST_BE_ALLOCATED) was not
493 	 * able to hold the request object number because it was not allocated.
494 	 */
495 	kstat_named_t dnode_hold_alloc_misses;
496 	/*
497 	 * Number of times dnode_hold(..., DNODE_MUST_BE_ALLOCATED) was not
498 	 * able to hold the request object number because the object number
499 	 * refers to an interior large dnode slot.
500 	 */
501 	kstat_named_t dnode_hold_alloc_interior;
502 	/*
503 	 * Number of times dnode_hold(..., DNODE_MUST_BE_ALLOCATED) needed
504 	 * to retry acquiring slot zrl locks due to contention.
505 	 */
506 	kstat_named_t dnode_hold_alloc_lock_retry;
507 	/*
508 	 * Number of times dnode_hold(..., DNODE_MUST_BE_ALLOCATED) did not
509 	 * need to create the dnode because another thread did so after
510 	 * dropping the read lock but before acquiring the write lock.
511 	 */
512 	kstat_named_t dnode_hold_alloc_lock_misses;
513 	/*
514 	 * Number of times dnode_hold(..., DNODE_MUST_BE_ALLOCATED) found
515 	 * a free dnode instantiated by dnode_create() but not yet allocated
516 	 * by dnode_allocate().
517 	 */
518 	kstat_named_t dnode_hold_alloc_type_none;
519 	/*
520 	 * Number of times dnode_hold(..., DNODE_MUST_BE_FREE) was able
521 	 * to hold the requested range of free dnode slots.
522 	 */
523 	kstat_named_t dnode_hold_free_hits;
524 	/*
525 	 * Number of times dnode_hold(..., DNODE_MUST_BE_FREE) was not
526 	 * able to hold the requested range of free dnode slots because
527 	 * at least one slot was allocated.
528 	 */
529 	kstat_named_t dnode_hold_free_misses;
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 	 * after acquiring the zrl lock at least one slot was allocated.
534 	 */
535 	kstat_named_t dnode_hold_free_lock_misses;
536 	/*
537 	 * Number of times dnode_hold(..., DNODE_MUST_BE_FREE) needed
538 	 * to retry acquiring slot zrl locks due to contention.
539 	 */
540 	kstat_named_t dnode_hold_free_lock_retry;
541 	/*
542 	 * Number of times dnode_hold(..., DNODE_MUST_BE_FREE) requested
543 	 * a range of dnode slots which were held by another thread.
544 	 */
545 	kstat_named_t dnode_hold_free_refcount;
546 	/*
547 	 * Number of times dnode_hold(..., DNODE_MUST_BE_FREE) requested
548 	 * a range of dnode slots which would overflow the dnode_phys_t.
549 	 */
550 	kstat_named_t dnode_hold_free_overflow;
551 	/*
552 	 * Number of times dnode_free_interior_slots() needed to retry
553 	 * acquiring a slot zrl lock due to contention.
554 	 */
555 	kstat_named_t dnode_free_interior_lock_retry;
556 	/*
557 	 * Number of new dnodes allocated by dnode_allocate().
558 	 */
559 	kstat_named_t dnode_allocate;
560 	/*
561 	 * Number of dnodes re-allocated by dnode_reallocate().
562 	 */
563 	kstat_named_t dnode_reallocate;
564 	/*
565 	 * Number of meta dnode dbufs evicted.
566 	 */
567 	kstat_named_t dnode_buf_evict;
568 	/*
569 	 * Number of times dmu_object_alloc*() reached the end of the existing
570 	 * object ID chunk and advanced to a new one.
571 	 */
572 	kstat_named_t dnode_alloc_next_chunk;
573 	/*
574 	 * Number of times multiple threads attempted to allocate a dnode
575 	 * from the same block of free dnodes.
576 	 */
577 	kstat_named_t dnode_alloc_race;
578 	/*
579 	 * Number of times dmu_object_alloc*() was forced to advance to the
580 	 * next meta dnode dbuf due to an error from  dmu_object_next().
581 	 */
582 	kstat_named_t dnode_alloc_next_block;
583 	/*
584 	 * Statistics for tracking dnodes which have been moved.
585 	 */
586 	kstat_named_t dnode_move_invalid;
587 	kstat_named_t dnode_move_recheck1;
588 	kstat_named_t dnode_move_recheck2;
589 	kstat_named_t dnode_move_special;
590 	kstat_named_t dnode_move_handle;
591 	kstat_named_t dnode_move_rwlock;
592 	kstat_named_t dnode_move_active;
593 } dnode_stats_t;
594 
595 typedef struct dnode_sums {
596 	wmsum_t dnode_hold_dbuf_hold;
597 	wmsum_t dnode_hold_dbuf_read;
598 	wmsum_t dnode_hold_alloc_hits;
599 	wmsum_t dnode_hold_alloc_misses;
600 	wmsum_t dnode_hold_alloc_interior;
601 	wmsum_t dnode_hold_alloc_lock_retry;
602 	wmsum_t dnode_hold_alloc_lock_misses;
603 	wmsum_t dnode_hold_alloc_type_none;
604 	wmsum_t dnode_hold_free_hits;
605 	wmsum_t dnode_hold_free_misses;
606 	wmsum_t dnode_hold_free_lock_misses;
607 	wmsum_t dnode_hold_free_lock_retry;
608 	wmsum_t dnode_hold_free_refcount;
609 	wmsum_t dnode_hold_free_overflow;
610 	wmsum_t dnode_free_interior_lock_retry;
611 	wmsum_t dnode_allocate;
612 	wmsum_t dnode_reallocate;
613 	wmsum_t dnode_buf_evict;
614 	wmsum_t dnode_alloc_next_chunk;
615 	wmsum_t dnode_alloc_race;
616 	wmsum_t dnode_alloc_next_block;
617 	wmsum_t dnode_move_invalid;
618 	wmsum_t dnode_move_recheck1;
619 	wmsum_t dnode_move_recheck2;
620 	wmsum_t dnode_move_special;
621 	wmsum_t dnode_move_handle;
622 	wmsum_t dnode_move_rwlock;
623 	wmsum_t dnode_move_active;
624 } dnode_sums_t;
625 
626 extern dnode_stats_t dnode_stats;
627 extern dnode_sums_t dnode_sums;
628 
629 #define	DNODE_STAT_INCR(stat, val) \
630     wmsum_add(&dnode_sums.stat, (val))
631 #define	DNODE_STAT_BUMP(stat) \
632     DNODE_STAT_INCR(stat, 1);
633 
634 #ifdef ZFS_DEBUG
635 
636 #define	dprintf_dnode(dn, fmt, ...) do { \
637 	if (zfs_flags & ZFS_DEBUG_DPRINTF) { \
638 	char __db_buf[32]; \
639 	uint64_t __db_obj = (dn)->dn_object; \
640 	if (__db_obj == DMU_META_DNODE_OBJECT) \
641 		(void) strlcpy(__db_buf, "mdn", sizeof (__db_buf));	\
642 	else \
643 		(void) snprintf(__db_buf, sizeof (__db_buf), "%lld", \
644 		    (u_longlong_t)__db_obj);\
645 	dprintf_ds((dn)->dn_objset->os_dsl_dataset, "obj=%s " fmt, \
646 	    __db_buf, __VA_ARGS__); \
647 	} \
648 } while (0)
649 
650 #define	DNODE_VERIFY(dn)		dnode_verify(dn)
651 #define	FREE_VERIFY(db, start, end, tx)	free_verify(db, start, end, tx)
652 
653 #else
654 
655 #define	dprintf_dnode(db, fmt, ...)
656 #define	DNODE_VERIFY(dn)		((void) sizeof ((uintptr_t)(dn)))
657 #define	FREE_VERIFY(db, start, end, tx)
658 
659 #endif
660 
661 #ifdef	__cplusplus
662 }
663 #endif
664 
665 #endif	/* _SYS_DNODE_H */
666