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