xref: /illumos-gate/usr/src/uts/common/fs/zfs/sys/dnode.h (revision b390f3a9)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
2228d97a71SMark Shellenbaum  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23c2919acbSMatthew Ahrens  * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
24bc9014e6SJustin Gibbs  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
25fa9e4066Sahrens  */
26fa9e4066Sahrens 
27fa9e4066Sahrens #ifndef	_SYS_DNODE_H
28fa9e4066Sahrens #define	_SYS_DNODE_H
29fa9e4066Sahrens 
30fa9e4066Sahrens #include <sys/zfs_context.h>
31fa9e4066Sahrens #include <sys/avl.h>
32fa9e4066Sahrens #include <sys/spa.h>
33fa9e4066Sahrens #include <sys/txg.h>
34c717a561Smaybee #include <sys/zio.h>
35fa9e4066Sahrens #include <sys/refcount.h>
36fa9e4066Sahrens #include <sys/dmu_zfetch.h>
37744947dcSTom Erickson #include <sys/zrlock.h>
3894c2d0ebSMatthew Ahrens #include <sys/multilist.h>
39fa9e4066Sahrens 
40fa9e4066Sahrens #ifdef	__cplusplus
41fa9e4066Sahrens extern "C" {
42fa9e4066Sahrens #endif
43fa9e4066Sahrens 
44fa9e4066Sahrens /*
45cdb0ab79Smaybee  * dnode_hold() flags.
46fa9e4066Sahrens  */
47fa9e4066Sahrens #define	DNODE_MUST_BE_ALLOCATED	1
48fa9e4066Sahrens #define	DNODE_MUST_BE_FREE	2
49d8849d7dSChunwei Chen #define	DNODE_DRY_RUN		4
50fa9e4066Sahrens 
51cdb0ab79Smaybee /*
52cdb0ab79Smaybee  * dnode_next_offset() flags.
53cdb0ab79Smaybee  */
54cdb0ab79Smaybee #define	DNODE_FIND_HOLE		1
55cdb0ab79Smaybee #define	DNODE_FIND_BACKWARDS	2
56cdb0ab79Smaybee #define	DNODE_FIND_HAVELOCK	4
57cdb0ab79Smaybee 
58fa9e4066Sahrens /*
59fa9e4066Sahrens  * Fixed constants.
60fa9e4066Sahrens  */
61fa9e4066Sahrens #define	DNODE_SHIFT		9	/* 512 bytes */
62e94f268aSMatthew Ahrens #define	DN_MIN_INDBLKSHIFT	12	/* 4k */
637de35a3eSPaul Dagnelie /*
647de35a3eSPaul Dagnelie  * If we ever increase this value beyond 20, we need to revisit all logic that
657de35a3eSPaul Dagnelie  * does x << level * ebps to handle overflow.  With a 1M indirect block size,
667de35a3eSPaul Dagnelie  * 4 levels of indirect blocks would not be able to guarantee addressing an
677de35a3eSPaul Dagnelie  * entire object, so 5 levels will be used, but 5 * (20 - 7) = 65.
687de35a3eSPaul Dagnelie  */
694b5c8e93SMatthew Ahrens #define	DN_MAX_INDBLKSHIFT	17	/* 128k */
70fa9e4066Sahrens #define	DNODE_BLOCK_SHIFT	14	/* 16k */
71fa9e4066Sahrens #define	DNODE_CORE_SIZE		64	/* 64 bytes for dnode sans blkptrs */
72fa9e4066Sahrens #define	DN_MAX_OBJECT_SHIFT	48	/* 256 trillion (zfs_fid_t limit) */
73fa9e4066Sahrens #define	DN_MAX_OFFSET_SHIFT	64	/* 2^64 bytes in a dnode */
74fa9e4066Sahrens 
750a586ceaSMark Shellenbaum /*
760a586ceaSMark Shellenbaum  * dnode id flags
770a586ceaSMark Shellenbaum  *
78eb633035STom Caputi  * Note: a file will never ever have its ids moved from bonus->spill
790a586ceaSMark Shellenbaum  */
800a586ceaSMark Shellenbaum #define	DN_ID_CHKED_BONUS	0x1
810a586ceaSMark Shellenbaum #define	DN_ID_CHKED_SPILL	0x2
820a586ceaSMark Shellenbaum #define	DN_ID_OLD_EXIST		0x4
830a586ceaSMark Shellenbaum #define	DN_ID_NEW_EXIST		0x8
840a586ceaSMark Shellenbaum 
85fa9e4066Sahrens /*
86fa9e4066Sahrens  * Derived constants.
87fa9e4066Sahrens  */
8854811da5SToomas Soome #define	DNODE_MIN_SIZE		(1 << DNODE_SHIFT)
8954811da5SToomas Soome #define	DNODE_MAX_SIZE		(1 << DNODE_BLOCK_SHIFT)
9054811da5SToomas Soome #define	DNODE_BLOCK_SIZE	(1 << DNODE_BLOCK_SHIFT)
9154811da5SToomas Soome #define	DNODE_MIN_SLOTS		(DNODE_MIN_SIZE >> DNODE_SHIFT)
9254811da5SToomas Soome #define	DNODE_MAX_SLOTS		(DNODE_MAX_SIZE >> DNODE_SHIFT)
9354811da5SToomas Soome #define	DN_BONUS_SIZE(dnsize)	((dnsize) - DNODE_CORE_SIZE - \
9454811da5SToomas Soome 	(1 << SPA_BLKPTRSHIFT))
9554811da5SToomas Soome #define	DN_SLOTS_TO_BONUSLEN(slots)	DN_BONUS_SIZE((slots) << DNODE_SHIFT)
9654811da5SToomas Soome #define	DN_OLD_MAX_BONUSLEN	(DN_BONUS_SIZE(DNODE_MIN_SIZE))
9754811da5SToomas Soome #define	DN_MAX_NBLKPTR	((DNODE_MIN_SIZE - DNODE_CORE_SIZE) >> SPA_BLKPTRSHIFT)
9854811da5SToomas Soome #define	DN_MAX_OBJECT		(1ULL << DN_MAX_OBJECT_SHIFT)
9954811da5SToomas Soome #define	DN_ZERO_BONUSLEN	(DN_BONUS_SIZE(DNODE_MAX_SIZE) + 1)
10054811da5SToomas Soome #define	DN_KILL_SPILLBLK	(1)
10154811da5SToomas Soome 
10254811da5SToomas Soome #define	DN_SLOT_UNINIT		((void *)NULL)	/* Uninitialized */
10354811da5SToomas Soome #define	DN_SLOT_FREE		((void *)1UL)	/* Free slot */
10454811da5SToomas Soome #define	DN_SLOT_ALLOCATED	((void *)2UL)	/* Allocated slot */
10554811da5SToomas Soome #define	DN_SLOT_INTERIOR	((void *)3UL)	/* Interior allocated slot */
10654811da5SToomas Soome #define	DN_SLOT_IS_PTR(dn)	((void *)dn > DN_SLOT_INTERIOR)
10754811da5SToomas Soome #define	DN_SLOT_IS_VALID(dn)	((void *)dn != NULL)
108fa9e4066Sahrens 
109fa9e4066Sahrens #define	DNODES_PER_BLOCK_SHIFT	(DNODE_BLOCK_SHIFT - DNODE_SHIFT)
110fa9e4066Sahrens #define	DNODES_PER_BLOCK	(1ULL << DNODES_PER_BLOCK_SHIFT)
1114b5c8e93SMatthew Ahrens 
1124b5c8e93SMatthew Ahrens /*
1134b5c8e93SMatthew Ahrens  * This is inaccurate if the indblkshift of the particular object is not the
1144b5c8e93SMatthew Ahrens  * max.  But it's only used by userland to calculate the zvol reservation.
1154b5c8e93SMatthew Ahrens  */
116fa9e4066Sahrens #define	DNODES_PER_LEVEL_SHIFT	(DN_MAX_INDBLKSHIFT - SPA_BLKPTRSHIFT)
117c1449561SEric Taylor #define	DNODES_PER_LEVEL	(1ULL << DNODES_PER_LEVEL_SHIFT)
118fa9e4066Sahrens 
119fa9e4066Sahrens /* The +2 here is a cheesy way to round up */
120fa9e4066Sahrens #define	DN_MAX_LEVELS	(2 + ((DN_MAX_OFFSET_SHIFT - SPA_MINBLOCKSHIFT) / \
121fa9e4066Sahrens 	(DN_MIN_INDBLKSHIFT - SPA_BLKPTRSHIFT)))
122fa9e4066Sahrens 
123fa9e4066Sahrens #define	DN_BONUS(dnp)	((void*)((dnp)->dn_bonus + \
124fa9e4066Sahrens 	(((dnp)->dn_nblkptr - 1) * sizeof (blkptr_t))))
12554811da5SToomas Soome #define	DN_MAX_BONUS_LEN(dnp) \
12654811da5SToomas Soome 	((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) ? \
12754811da5SToomas Soome 	(uint8_t *)DN_SPILL_BLKPTR(dnp) - (uint8_t *)DN_BONUS(dnp) : \
12854811da5SToomas Soome 	(uint8_t *)(dnp + (dnp->dn_extra_slots + 1)) - (uint8_t *)DN_BONUS(dnp))
129fa9e4066Sahrens 
13099653d4eSeschrock #define	DN_USED_BYTES(dnp) (((dnp)->dn_flags & DNODE_FLAG_USED_BYTES) ? \
13199653d4eSeschrock 	(dnp)->dn_used : (dnp)->dn_used << SPA_MINBLOCKSHIFT)
13299653d4eSeschrock 
133fa9e4066Sahrens #define	EPB(blkshift, typeshift)	(1 << (blkshift - typeshift))
134fa9e4066Sahrens 
135fa9e4066Sahrens struct dmu_buf_impl;
136503ad85cSMatthew Ahrens struct objset;
137fa9e4066Sahrens struct zio;
138fa9e4066Sahrens 
139fa9e4066Sahrens enum dnode_dirtycontext {
140fa9e4066Sahrens 	DN_UNDIRTIED,
141fa9e4066Sahrens 	DN_DIRTY_OPEN,
142fa9e4066Sahrens 	DN_DIRTY_SYNC
143fa9e4066Sahrens };
144fa9e4066Sahrens 
14599653d4eSeschrock /* Is dn_used in bytes?  if not, it's in multiples of SPA_MINBLOCKSIZE */
146f67950b2SNasf-Fan #define	DNODE_FLAG_USED_BYTES			(1 << 0)
147f67950b2SNasf-Fan #define	DNODE_FLAG_USERUSED_ACCOUNTED		(1 << 1)
14899653d4eSeschrock 
1490a586ceaSMark Shellenbaum /* Does dnode have a SA spill blkptr in bonus? */
150f67950b2SNasf-Fan #define	DNODE_FLAG_SPILL_BLKPTR			(1 << 2)
151f67950b2SNasf-Fan 
152f67950b2SNasf-Fan /* User/Group/Project dnode accounting */
153f67950b2SNasf-Fan #define	DNODE_FLAG_USEROBJUSED_ACCOUNTED	(1 << 3)
1540a586ceaSMark Shellenbaum 
15554811da5SToomas Soome /*
15654811da5SToomas Soome  * VARIABLE-LENGTH (LARGE) DNODES
15754811da5SToomas Soome  *
15854811da5SToomas Soome  * The motivation for variable-length dnodes is to eliminate the overhead
15954811da5SToomas Soome  * associated with using spill blocks.  Spill blocks are used to store
16054811da5SToomas Soome  * system attribute data (i.e. file metadata) that does not fit in the
16154811da5SToomas Soome  * dnode's bonus buffer. By allowing a larger bonus buffer area the use of
16254811da5SToomas Soome  * a spill block can be avoided.  Spill blocks potentially incur an
16354811da5SToomas Soome  * additional read I/O for every dnode in a dnode block. As a worst case
16454811da5SToomas Soome  * example, reading 32 dnodes from a 16k dnode block and all of the spill
16554811da5SToomas Soome  * blocks could issue 33 separate reads. Now suppose those dnodes have size
16654811da5SToomas Soome  * 1024 and therefore don't need spill blocks. Then the worst case number
16754811da5SToomas Soome  * of blocks read is reduced to from 33 to two--one per dnode block.
16854811da5SToomas Soome  *
16954811da5SToomas Soome  * ZFS-on-Linux systems that make heavy use of extended attributes benefit
17054811da5SToomas Soome  * from this feature. In particular, ZFS-on-Linux supports the xattr=sa
17154811da5SToomas Soome  * dataset property which allows file extended attribute data to be stored
17254811da5SToomas Soome  * in the dnode bonus buffer as an alternative to the traditional
17354811da5SToomas Soome  * directory-based format. Workloads such as SELinux and the Lustre
17454811da5SToomas Soome  * distributed filesystem often store enough xattr data to force spill
17554811da5SToomas Soome  * blocks when xattr=sa is in effect. Large dnodes may therefore provide a
17654811da5SToomas Soome  * performance benefit to such systems. Other use cases that benefit from
17754811da5SToomas Soome  * this feature include files with large ACLs and symbolic links with long
17854811da5SToomas Soome  * target names.
17954811da5SToomas Soome  *
18054811da5SToomas Soome  * The size of a dnode may be a multiple of 512 bytes up to the size of a
18154811da5SToomas Soome  * dnode block (currently 16384 bytes). The dn_extra_slots field of the
18254811da5SToomas Soome  * on-disk dnode_phys_t structure describes the size of the physical dnode
18354811da5SToomas Soome  * on disk. The field represents how many "extra" dnode_phys_t slots a
18454811da5SToomas Soome  * dnode consumes in its dnode block. This convention results in a value of
18554811da5SToomas Soome  * 0 for 512 byte dnodes which preserves on-disk format compatibility with
18654811da5SToomas Soome  * older software which doesn't support large dnodes.
18754811da5SToomas Soome  *
18854811da5SToomas Soome  * Similarly, the in-memory dnode_t structure has a dn_num_slots field
18954811da5SToomas Soome  * to represent the total number of dnode_phys_t slots consumed on disk.
19054811da5SToomas Soome  * Thus dn->dn_num_slots is 1 greater than the corresponding
19154811da5SToomas Soome  * dnp->dn_extra_slots. This difference in convention was adopted
19254811da5SToomas Soome  * because, unlike on-disk structures, backward compatibility is not a
19354811da5SToomas Soome  * concern for in-memory objects, so we used a more natural way to
19454811da5SToomas Soome  * represent size for a dnode_t.
19554811da5SToomas Soome  *
19654811da5SToomas Soome  * The default size for newly created dnodes is determined by the value of
19754811da5SToomas Soome  * the "dnodesize" dataset property. By default the property is set to
19854811da5SToomas Soome  * "legacy" which is compatible with older software. Setting the property
19954811da5SToomas Soome  * to "auto" will allow the filesystem to choose the most suitable dnode
20054811da5SToomas Soome  * size. Currently this just sets the default dnode size to 1k, but future
20154811da5SToomas Soome  * code improvements could dynamically choose a size based on observed
20254811da5SToomas Soome  * workload patterns. Dnodes of varying sizes can coexist within the same
20354811da5SToomas Soome  * dataset and even within the same dnode block.
20454811da5SToomas Soome  */
20554811da5SToomas Soome 
206eb633035STom Caputi #define	DNODE_CRYPT_PORTABLE_FLAGS_MASK		(DNODE_FLAG_SPILL_BLKPTR)
207eb633035STom Caputi 
208fa9e4066Sahrens typedef struct dnode_phys {
209fa9e4066Sahrens 	uint8_t dn_type;		/* dmu_object_type_t */
210fa9e4066Sahrens 	uint8_t dn_indblkshift;		/* ln2(indirect block size) */
211fa9e4066Sahrens 	uint8_t dn_nlevels;		/* 1=dn_blkptr->data blocks */
212fa9e4066Sahrens 	uint8_t dn_nblkptr;		/* length of dn_blkptr */
213fa9e4066Sahrens 	uint8_t dn_bonustype;		/* type of data in bonus buffer */
214fa9e4066Sahrens 	uint8_t	dn_checksum;		/* ZIO_CHECKSUM type */
215fa9e4066Sahrens 	uint8_t	dn_compress;		/* ZIO_COMPRESS type */
21699653d4eSeschrock 	uint8_t dn_flags;		/* DNODE_FLAG_* */
217fa9e4066Sahrens 	uint16_t dn_datablkszsec;	/* data block size in 512b sectors */
218fa9e4066Sahrens 	uint16_t dn_bonuslen;		/* length of dn_bonus */
21954811da5SToomas Soome 	uint8_t dn_extra_slots;		/* # of subsequent slots consumed */
22054811da5SToomas Soome 	uint8_t dn_pad2[3];
221fa9e4066Sahrens 
222fa9e4066Sahrens 	/* accounting is protected by dn_dirty_mtx */
223fa9e4066Sahrens 	uint64_t dn_maxblkid;		/* largest allocated block ID */
22499653d4eSeschrock 	uint64_t dn_used;		/* bytes (or sectors) of disk space */
225fa9e4066Sahrens 
226eb633035STom Caputi 	/*
227eb633035STom Caputi 	 * Both dn_pad2 and dn_pad3 are protected by the block's MAC. This
228eb633035STom Caputi 	 * allows us to protect any fields that might be added here in the
229eb633035STom Caputi 	 * future. In either case, developers will want to check
230eb633035STom Caputi 	 * zio_crypt_init_uios_dnode() to ensure the new field is being
231eb633035STom Caputi 	 * protected properly.
232eb633035STom Caputi 	 */
233fa9e4066Sahrens 	uint64_t dn_pad3[4];
23454811da5SToomas Soome 	union {
23554811da5SToomas Soome 		blkptr_t dn_blkptr[1+DN_OLD_MAX_BONUSLEN/sizeof (blkptr_t)];
23654811da5SToomas Soome 		struct {
23754811da5SToomas Soome 			blkptr_t __dn_ignore1;
23854811da5SToomas Soome 			uint8_t dn_bonus[DN_OLD_MAX_BONUSLEN];
23954811da5SToomas Soome 		};
24054811da5SToomas Soome 		struct {
24154811da5SToomas Soome 			blkptr_t __dn_ignore2;
24254811da5SToomas Soome 			uint8_t __dn_ignore3[DN_OLD_MAX_BONUSLEN -
24354811da5SToomas Soome 			    sizeof (blkptr_t)];
24454811da5SToomas Soome 			blkptr_t dn_spill;
24554811da5SToomas Soome 		};
24654811da5SToomas Soome 	};
247fa9e4066Sahrens } dnode_phys_t;
248fa9e4066Sahrens 
249eb633035STom Caputi #define	DN_SPILL_BLKPTR(dnp)	((blkptr_t *)((char *)(dnp) +		\
250eb633035STom Caputi 	(((dnp)->dn_extra_slots + 1) << DNODE_SHIFT) - (1 << SPA_BLKPTRSHIFT)))
25154811da5SToomas Soome 
25279d72832SMatthew Ahrens struct dnode {
253fa9e4066Sahrens 	/*
254f7170741SWill Andrews 	 * Protects the structure of the dnode, including the number of levels
255f7170741SWill Andrews 	 * of indirection (dn_nlevels), dn_maxblkid, and dn_next_*
256fa9e4066Sahrens 	 */
257fa9e4066Sahrens 	krwlock_t dn_struct_rwlock;
258fa9e4066Sahrens 
25914843421SMatthew Ahrens 	/* Our link on dn_objset->os_dnodes list; protected by os_lock.  */
260fa9e4066Sahrens 	list_node_t dn_link;
261fa9e4066Sahrens 
262fa9e4066Sahrens 	/* immutable: */
263503ad85cSMatthew Ahrens 	struct objset *dn_objset;
264fa9e4066Sahrens 	uint64_t dn_object;
265fa9e4066Sahrens 	struct dmu_buf_impl *dn_dbuf;
266744947dcSTom Erickson 	struct dnode_handle *dn_handle;
267fa9e4066Sahrens 	dnode_phys_t *dn_phys; /* pointer into dn->dn_dbuf->db.db_data */
268fa9e4066Sahrens 
269fa9e4066Sahrens 	/*
270c543ec06Sahrens 	 * Copies of stuff in dn_phys.  They're valid in the open
271c543ec06Sahrens 	 * context (eg. even before the dnode is first synced).
272c543ec06Sahrens 	 * Where necessary, these are protected by dn_struct_rwlock.
273fa9e4066Sahrens 	 */
274c543ec06Sahrens 	dmu_object_type_t dn_type;	/* object type */
275c543ec06Sahrens 	uint16_t dn_bonuslen;		/* bonus length */
276c543ec06Sahrens 	uint8_t dn_bonustype;		/* bonus type */
277fa9e4066Sahrens 	uint8_t dn_nblkptr;		/* number of blkptrs (immutable) */
278fa9e4066Sahrens 	uint8_t dn_checksum;		/* ZIO_CHECKSUM type */
279fa9e4066Sahrens 	uint8_t dn_compress;		/* ZIO_COMPRESS type */
280fa9e4066Sahrens 	uint8_t dn_nlevels;
281fa9e4066Sahrens 	uint8_t dn_indblkshift;
282c543ec06Sahrens 	uint8_t dn_datablkshift;	/* zero if blksz not power of 2! */
283744947dcSTom Erickson 	uint8_t dn_moved;		/* Has this dnode been moved? */
284c543ec06Sahrens 	uint16_t dn_datablkszsec;	/* in 512b sectors */
285c543ec06Sahrens 	uint32_t dn_datablksz;		/* in bytes */
286c543ec06Sahrens 	uint64_t dn_maxblkid;
2872acef22dSMatthew Ahrens 	uint8_t dn_next_type[TXG_SIZE];
28854811da5SToomas Soome 	uint8_t dn_num_slots;		/* metadnode slots consumed on disk */
289da03de99SMark Maybee 	uint8_t dn_next_nblkptr[TXG_SIZE];
290fa9e4066Sahrens 	uint8_t dn_next_nlevels[TXG_SIZE];
291fa9e4066Sahrens 	uint8_t dn_next_indblkshift[TXG_SIZE];
2920a586ceaSMark Shellenbaum 	uint8_t dn_next_bonustype[TXG_SIZE];
2930a586ceaSMark Shellenbaum 	uint8_t dn_rm_spillblk[TXG_SIZE];	/* for removing spill blk */
2941934e92fSmaybee 	uint16_t dn_next_bonuslen[TXG_SIZE];
295c543ec06Sahrens 	uint32_t dn_next_blksz[TXG_SIZE];	/* next block size in bytes */
296eb633035STom Caputi 	uint64_t dn_next_maxblkid[TXG_SIZE];	/* next maxblkid in bytes */
297fa9e4066Sahrens 
298744947dcSTom Erickson 	/* protected by dn_dbufs_mtx; declared here to fill 32-bit hole */
299744947dcSTom Erickson 	uint32_t dn_dbufs_count;	/* count of dn_dbufs */
300744947dcSTom Erickson 
301fa9e4066Sahrens 	/* protected by os_lock: */
30294c2d0ebSMatthew Ahrens 	multilist_node_t dn_dirty_link[TXG_SIZE]; /* next on dataset's dirty */
303fa9e4066Sahrens 
304fa9e4066Sahrens 	/* protected by dn_mtx: */
305fa9e4066Sahrens 	kmutex_t dn_mtx;
306c717a561Smaybee 	list_t dn_dirty_records[TXG_SIZE];
307bf16b11eSMatthew Ahrens 	struct range_tree *dn_free_ranges[TXG_SIZE];
308fa9e4066Sahrens 	uint64_t dn_allocated_txg;
309fa9e4066Sahrens 	uint64_t dn_free_txg;
310fa9e4066Sahrens 	uint64_t dn_assigned_txg;
311aa02ea01STom Caputi 	uint64_t dn_dirty_txg;			/* txg dnode was last dirtied */
312fa9e4066Sahrens 	kcondvar_t dn_notxholds;
313*b390f3a9SJohn Poduska 	kcondvar_t dn_nodnholds;
314fa9e4066Sahrens 	enum dnode_dirtycontext dn_dirtyctx;
315fa9e4066Sahrens 	uint8_t *dn_dirtyctx_firstset;		/* dbg: contents meaningless */
316fa9e4066Sahrens 
317fa9e4066Sahrens 	/* protected by own devices */
318e914ace2STim Schumacher 	zfs_refcount_t dn_tx_holds;
319e914ace2STim Schumacher 	zfs_refcount_t dn_holds;
320fa9e4066Sahrens 
321fa9e4066Sahrens 	kmutex_t dn_dbufs_mtx;
32286bb58aeSAlex Reece 	/*
32386bb58aeSAlex Reece 	 * Descendent dbufs, ordered by dbuf_compare. Note that dn_dbufs
32486bb58aeSAlex Reece 	 * can contain multiple dbufs of the same (level, blkid) when a
32586bb58aeSAlex Reece 	 * dbuf is marked DB_EVICTING without being removed from
32686bb58aeSAlex Reece 	 * dn_dbufs. To maintain the avl invariant that there cannot be
32786bb58aeSAlex Reece 	 * duplicate entries, we order the dbufs by an arbitrary value -
32886bb58aeSAlex Reece 	 * their address in memory. This means that dn_dbufs cannot be used to
32986bb58aeSAlex Reece 	 * directly look up a dbuf. Instead, callers must use avl_walk, have
33086bb58aeSAlex Reece 	 * a reference to the dbuf, or look up a non-existant node with
33186bb58aeSAlex Reece 	 * db_state = DB_SEARCH (see dbuf_free_range for an example).
33286bb58aeSAlex Reece 	 */
33386bb58aeSAlex Reece 	avl_tree_t dn_dbufs;
334744947dcSTom Erickson 
335744947dcSTom Erickson 	/* protected by dn_struct_rwlock */
336ea8dc4b6Seschrock 	struct dmu_buf_impl *dn_bonus;	/* bonus buffer dbuf */
337744947dcSTom Erickson 
3380a586ceaSMark Shellenbaum 	boolean_t dn_have_spill;	/* have spill or are spilling */
339fa9e4066Sahrens 
340c717a561Smaybee 	/* parent IO for current sync write */
341c717a561Smaybee 	zio_t *dn_zio;
342c717a561Smaybee 
34314843421SMatthew Ahrens 	/* used in syncing context */
3440a586ceaSMark Shellenbaum 	uint64_t dn_oldused;	/* old phys used bytes */
3450a586ceaSMark Shellenbaum 	uint64_t dn_oldflags;	/* old phys dn_flags */
346f67950b2SNasf-Fan 	uint64_t dn_olduid, dn_oldgid, dn_oldprojid;
347f67950b2SNasf-Fan 	uint64_t dn_newuid, dn_newgid, dn_newprojid;
3480a586ceaSMark Shellenbaum 	int dn_id_flags;
34914843421SMatthew Ahrens 
350fa9e4066Sahrens 	/* holds prefetch structure */
351fa9e4066Sahrens 	struct zfetch	dn_zfetch;
35279d72832SMatthew Ahrens };
353fa9e4066Sahrens 
354eb633035STom Caputi /*
355eb633035STom Caputi  * We use this (otherwise unused) bit to indicate if the value of
356eb633035STom Caputi  * dn_next_maxblkid[txgoff] is valid to use in dnode_sync().
357eb633035STom Caputi  */
358eb633035STom Caputi #define	DMU_NEXT_MAXBLKID_SET		(1ULL << 63)
359eb633035STom Caputi 
360744947dcSTom Erickson /*
361744947dcSTom Erickson  * Adds a level of indirection between the dbuf and the dnode to avoid
362744947dcSTom Erickson  * iterating descendent dbufs in dnode_move(). Handles are not allocated
363744947dcSTom Erickson  * individually, but as an array of child dnodes in dnode_hold_impl().
364744947dcSTom Erickson  */
365744947dcSTom Erickson typedef struct dnode_handle {
366744947dcSTom Erickson 	/* Protects dnh_dnode from modification by dnode_move(). */
367744947dcSTom Erickson 	zrlock_t dnh_zrlock;
368744947dcSTom Erickson 	dnode_t *dnh_dnode;
369744947dcSTom Erickson } dnode_handle_t;
370744947dcSTom Erickson 
371744947dcSTom Erickson typedef struct dnode_children {
372bc9014e6SJustin Gibbs 	dmu_buf_user_t dnc_dbu;		/* User evict data */
373744947dcSTom Erickson 	size_t dnc_count;		/* number of children */
3747f18da4cSJustin T. Gibbs 	dnode_handle_t dnc_children[];	/* sized dynamically */
375744947dcSTom Erickson } dnode_children_t;
376744947dcSTom Erickson 
377fa9e4066Sahrens typedef struct free_range {
378fa9e4066Sahrens 	avl_node_t fr_node;
379fa9e4066Sahrens 	uint64_t fr_blkid;
380fa9e4066Sahrens 	uint64_t fr_nblks;
381fa9e4066Sahrens } free_range_t;
382fa9e4066Sahrens 
383bc9014e6SJustin Gibbs void dnode_special_open(struct objset *dd, dnode_phys_t *dnp,
384744947dcSTom Erickson     uint64_t object, dnode_handle_t *dnh);
385744947dcSTom Erickson void dnode_special_close(dnode_handle_t *dnh);
386fa9e4066Sahrens 
3871934e92fSmaybee void dnode_setbonuslen(dnode_t *dn, int newsize, dmu_tx_t *tx);
3880a586ceaSMark Shellenbaum void dnode_setbonus_type(dnode_t *dn, dmu_object_type_t, dmu_tx_t *tx);
3890a586ceaSMark Shellenbaum void dnode_rm_spill(dnode_t *dn, dmu_tx_t *tx);
3900a586ceaSMark Shellenbaum 
391503ad85cSMatthew Ahrens int dnode_hold(struct objset *dd, uint64_t object,
392ea8dc4b6Seschrock     void *ref, dnode_t **dnp);
39354811da5SToomas Soome int dnode_hold_impl(struct objset *dd, uint64_t object, int flag, int dn_slots,
394ea8dc4b6Seschrock     void *ref, dnode_t **dnp);
3951934e92fSmaybee boolean_t dnode_add_ref(dnode_t *dn, void *ref);
396fa9e4066Sahrens void dnode_rele(dnode_t *dn, void *ref);
397c2919acbSMatthew Ahrens void dnode_rele_and_unlock(dnode_t *dn, void *tag, boolean_t evicting);
398d8849d7dSChunwei Chen int dnode_try_claim(objset_t *os, uint64_t object, int slots);
399fa9e4066Sahrens void dnode_setdirty(dnode_t *dn, dmu_tx_t *tx);
400c717a561Smaybee void dnode_sync(dnode_t *dn, dmu_tx_t *tx);
401fa9e4066Sahrens void dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs,
40254811da5SToomas Soome     dmu_object_type_t bonustype, int bonuslen, int dn_slots, dmu_tx_t *tx);
403fa9e4066Sahrens void dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize,
404eb633035STom Caputi     dmu_object_type_t bonustype, int bonuslen, int dn_slots,
405eb633035STom Caputi     boolean_t keep_spill, dmu_tx_t *tx);
406fa9e4066Sahrens void dnode_free(dnode_t *dn, dmu_tx_t *tx);
407fa9e4066Sahrens void dnode_byteswap(dnode_phys_t *dnp);
408fa9e4066Sahrens void dnode_buf_byteswap(void *buf, size_t size);
409fa9e4066Sahrens void dnode_verify(dnode_t *dn);
410eb633035STom Caputi int dnode_set_nlevels(dnode_t *dn, int nlevels, dmu_tx_t *tx);
411fa9e4066Sahrens int dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx);
412fa9e4066Sahrens void dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx);
413fa9e4066Sahrens void dnode_diduse_space(dnode_t *dn, int64_t space);
414eb633035STom Caputi void dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx,
415eb633035STom Caputi     boolean_t have_read, boolean_t force);
416fa9e4066Sahrens uint64_t dnode_block_freed(dnode_t *dn, uint64_t blkid);
417fa9e4066Sahrens void dnode_init(void);
418fa9e4066Sahrens void dnode_fini(void);
419cdb0ab79Smaybee int dnode_next_offset(dnode_t *dn, int flags, uint64_t *off,
420cdb0ab79Smaybee     int minlvl, uint64_t blkfill, uint64_t txg);
4211934e92fSmaybee void dnode_evict_dbufs(dnode_t *dn);
422cd485b49SJustin T. Gibbs void dnode_evict_bonus(dnode_t *dn);
42354811da5SToomas Soome void dnode_free_interior_slots(dnode_t *dn);
4245cabbc6bSPrashanth Sreenivasa boolean_t dnode_needs_remap(const dnode_t *dn);
425fa9e4066Sahrens 
426aa02ea01STom Caputi #define	DNODE_IS_DIRTY(_dn)						\
427aa02ea01STom Caputi 	((_dn)->dn_dirty_txg >= spa_syncing_txg((_dn)->dn_objset->os_spa))
428aa02ea01STom Caputi 
429cb92f413SAlexander Motin #define	DNODE_IS_CACHEABLE(_dn)						\
430cb92f413SAlexander Motin 	((_dn)->dn_objset->os_primary_cache == ZFS_CACHE_ALL ||		\
431cb92f413SAlexander Motin 	(DMU_OT_IS_METADATA((_dn)->dn_type) &&				\
432cb92f413SAlexander Motin 	(_dn)->dn_objset->os_primary_cache == ZFS_CACHE_METADATA))
433cb92f413SAlexander Motin 
434cb92f413SAlexander Motin #define	DNODE_META_IS_CACHEABLE(_dn)					\
435cb92f413SAlexander Motin 	((_dn)->dn_objset->os_primary_cache == ZFS_CACHE_ALL ||		\
436cb92f413SAlexander Motin 	(_dn)->dn_objset->os_primary_cache == ZFS_CACHE_METADATA)
437cb92f413SAlexander Motin 
43854811da5SToomas Soome /*
43954811da5SToomas Soome  * Used for dnodestats kstat.
44054811da5SToomas Soome  */
44154811da5SToomas Soome typedef struct dnode_stats {
44254811da5SToomas Soome 	/*
44354811da5SToomas Soome 	 * Number of failed attempts to hold a meta dnode dbuf.
44454811da5SToomas Soome 	 */
44554811da5SToomas Soome 	kstat_named_t dnode_hold_dbuf_hold;
44654811da5SToomas Soome 	/*
44754811da5SToomas Soome 	 * Number of failed attempts to read a meta dnode dbuf.
44854811da5SToomas Soome 	 */
44954811da5SToomas Soome 	kstat_named_t dnode_hold_dbuf_read;
45054811da5SToomas Soome 	/*
45154811da5SToomas Soome 	 * Number of times dnode_hold(..., DNODE_MUST_BE_ALLOCATED) was able
45254811da5SToomas Soome 	 * to hold the requested object number which was allocated.  This is
45354811da5SToomas Soome 	 * the common case when looking up any allocated object number.
45454811da5SToomas Soome 	 */
45554811da5SToomas Soome 	kstat_named_t dnode_hold_alloc_hits;
45654811da5SToomas Soome 	/*
45754811da5SToomas Soome 	 * Number of times dnode_hold(..., DNODE_MUST_BE_ALLOCATED) was not
45854811da5SToomas Soome 	 * able to hold the request object number because it was not allocated.
45954811da5SToomas Soome 	 */
46054811da5SToomas Soome 	kstat_named_t dnode_hold_alloc_misses;
46154811da5SToomas Soome 	/*
46254811da5SToomas Soome 	 * Number of times dnode_hold(..., DNODE_MUST_BE_ALLOCATED) was not
46354811da5SToomas Soome 	 * able to hold the request object number because the object number
46454811da5SToomas Soome 	 * refers to an interior large dnode slot.
46554811da5SToomas Soome 	 */
46654811da5SToomas Soome 	kstat_named_t dnode_hold_alloc_interior;
46754811da5SToomas Soome 	/*
46854811da5SToomas Soome 	 * Number of times dnode_hold(..., DNODE_MUST_BE_ALLOCATED) needed
46954811da5SToomas Soome 	 * to retry acquiring slot zrl locks due to contention.
47054811da5SToomas Soome 	 */
47154811da5SToomas Soome 	kstat_named_t dnode_hold_alloc_lock_retry;
47254811da5SToomas Soome 	/*
47354811da5SToomas Soome 	 * Number of times dnode_hold(..., DNODE_MUST_BE_ALLOCATED) did not
47454811da5SToomas Soome 	 * need to create the dnode because another thread did so after
47554811da5SToomas Soome 	 * dropping the read lock but before acquiring the write lock.
47654811da5SToomas Soome 	 */
47754811da5SToomas Soome 	kstat_named_t dnode_hold_alloc_lock_misses;
47854811da5SToomas Soome 	/*
47954811da5SToomas Soome 	 * Number of times dnode_hold(..., DNODE_MUST_BE_ALLOCATED) found
48054811da5SToomas Soome 	 * a free dnode instantiated by dnode_create() but not yet allocated
48154811da5SToomas Soome 	 * by dnode_allocate().
48254811da5SToomas Soome 	 */
48354811da5SToomas Soome 	kstat_named_t dnode_hold_alloc_type_none;
48454811da5SToomas Soome 	/*
48554811da5SToomas Soome 	 * Number of times dnode_hold(..., DNODE_MUST_BE_FREE) was able
48654811da5SToomas Soome 	 * to hold the requested range of free dnode slots.
48754811da5SToomas Soome 	 */
48854811da5SToomas Soome 	kstat_named_t dnode_hold_free_hits;
48954811da5SToomas Soome 	/*
49054811da5SToomas Soome 	 * Number of times dnode_hold(..., DNODE_MUST_BE_FREE) was not
49154811da5SToomas Soome 	 * able to hold the requested range of free dnode slots because
49254811da5SToomas Soome 	 * at least one slot was allocated.
49354811da5SToomas Soome 	 */
49454811da5SToomas Soome 	kstat_named_t dnode_hold_free_misses;
49554811da5SToomas Soome 	/*
49654811da5SToomas Soome 	 * Number of times dnode_hold(..., DNODE_MUST_BE_FREE) was not
49754811da5SToomas Soome 	 * able to hold the requested range of free dnode slots because
49854811da5SToomas Soome 	 * after acquiring the zrl lock at least one slot was allocated.
49954811da5SToomas Soome 	 */
50054811da5SToomas Soome 	kstat_named_t dnode_hold_free_lock_misses;
50154811da5SToomas Soome 	/*
50254811da5SToomas Soome 	 * Number of times dnode_hold(..., DNODE_MUST_BE_FREE) needed
50354811da5SToomas Soome 	 * to retry acquiring slot zrl locks due to contention.
50454811da5SToomas Soome 	 */
50554811da5SToomas Soome 	kstat_named_t dnode_hold_free_lock_retry;
50654811da5SToomas Soome 	/*
50754811da5SToomas Soome 	 * Number of times dnode_hold(..., DNODE_MUST_BE_FREE) requested
50854811da5SToomas Soome 	 * a range of dnode slots which were held by another thread.
50954811da5SToomas Soome 	 */
51054811da5SToomas Soome 	kstat_named_t dnode_hold_free_refcount;
51154811da5SToomas Soome 	/*
51254811da5SToomas Soome 	 * Number of times dnode_hold(..., DNODE_MUST_BE_FREE) requested
51354811da5SToomas Soome 	 * a range of dnode slots which would overflow the dnode_phys_t.
51454811da5SToomas Soome 	 */
51554811da5SToomas Soome 	kstat_named_t dnode_hold_free_overflow;
51654811da5SToomas Soome 	/*
51754811da5SToomas Soome 	 * Number of times dnode_free_interior_slots() needed to retry
51854811da5SToomas Soome 	 * acquiring a slot zrl lock due to contention.
51954811da5SToomas Soome 	 */
52054811da5SToomas Soome 	kstat_named_t dnode_free_interior_lock_retry;
52154811da5SToomas Soome 	/*
52254811da5SToomas Soome 	 * Number of new dnodes allocated by dnode_allocate().
52354811da5SToomas Soome 	 */
52454811da5SToomas Soome 	kstat_named_t dnode_allocate;
52554811da5SToomas Soome 	/*
52654811da5SToomas Soome 	 * Number of dnodes re-allocated by dnode_reallocate().
52754811da5SToomas Soome 	 */
52854811da5SToomas Soome 	kstat_named_t dnode_reallocate;
52954811da5SToomas Soome 	/*
53054811da5SToomas Soome 	 * Number of meta dnode dbufs evicted.
53154811da5SToomas Soome 	 */
53254811da5SToomas Soome 	kstat_named_t dnode_buf_evict;
53354811da5SToomas Soome 	/*
53454811da5SToomas Soome 	 * Number of times dmu_object_alloc*() reached the end of the existing
53554811da5SToomas Soome 	 * object ID chunk and advanced to a new one.
53654811da5SToomas Soome 	 */
53754811da5SToomas Soome 	kstat_named_t dnode_alloc_next_chunk;
53854811da5SToomas Soome 	/*
53954811da5SToomas Soome 	 * Number of times multiple threads attempted to allocate a dnode
54054811da5SToomas Soome 	 * from the same block of free dnodes.
54154811da5SToomas Soome 	 */
54254811da5SToomas Soome 	kstat_named_t dnode_alloc_race;
54354811da5SToomas Soome 	/*
54454811da5SToomas Soome 	 * Number of times dmu_object_alloc*() was forced to advance to the
54554811da5SToomas Soome 	 * next meta dnode dbuf due to an error from  dmu_object_next().
54654811da5SToomas Soome 	 */
54754811da5SToomas Soome 	kstat_named_t dnode_alloc_next_block;
54854811da5SToomas Soome 	/*
54954811da5SToomas Soome 	 * Statistics for tracking dnodes which have been moved.
55054811da5SToomas Soome 	 */
55154811da5SToomas Soome 	kstat_named_t dnode_move_invalid;
55254811da5SToomas Soome 	kstat_named_t dnode_move_recheck1;
55354811da5SToomas Soome 	kstat_named_t dnode_move_recheck2;
55454811da5SToomas Soome 	kstat_named_t dnode_move_special;
55554811da5SToomas Soome 	kstat_named_t dnode_move_handle;
55654811da5SToomas Soome 	kstat_named_t dnode_move_rwlock;
55754811da5SToomas Soome 	kstat_named_t dnode_move_active;
55854811da5SToomas Soome } dnode_stats_t;
55954811da5SToomas Soome 
56054811da5SToomas Soome extern dnode_stats_t dnode_stats;
56154811da5SToomas Soome 
56254811da5SToomas Soome #define	DNODE_STAT_INCR(stat, val) \
56354811da5SToomas Soome     atomic_add_64(&dnode_stats.stat.value.ui64, (val));
56454811da5SToomas Soome #define	DNODE_STAT_BUMP(stat) \
56554811da5SToomas Soome     DNODE_STAT_INCR(stat, 1);
56654811da5SToomas Soome 
567fa9e4066Sahrens #ifdef ZFS_DEBUG
568fa9e4066Sahrens 
569fa9e4066Sahrens /*
570fa9e4066Sahrens  * There should be a ## between the string literal and fmt, to make it
571fa9e4066Sahrens  * clear that we're joining two strings together, but that piece of shit
572fa9e4066Sahrens  * gcc doesn't support that preprocessor token.
573fa9e4066Sahrens  */
574fa9e4066Sahrens #define	dprintf_dnode(dn, fmt, ...) do { \
575fa9e4066Sahrens 	if (zfs_flags & ZFS_DEBUG_DPRINTF) { \
576fa9e4066Sahrens 	char __db_buf[32]; \
577fa9e4066Sahrens 	uint64_t __db_obj = (dn)->dn_object; \
578fa9e4066Sahrens 	if (__db_obj == DMU_META_DNODE_OBJECT) \
579fa9e4066Sahrens 		(void) strcpy(__db_buf, "mdn"); \
580fa9e4066Sahrens 	else \
581fa9e4066Sahrens 		(void) snprintf(__db_buf, sizeof (__db_buf), "%lld", \
582fa9e4066Sahrens 		    (u_longlong_t)__db_obj);\
583fa9e4066Sahrens 	dprintf_ds((dn)->dn_objset->os_dsl_dataset, "obj=%s " fmt, \
584fa9e4066Sahrens 	    __db_buf, __VA_ARGS__); \
585fa9e4066Sahrens 	} \
586fa9e4066Sahrens _NOTE(CONSTCOND) } while (0)
587fa9e4066Sahrens 
5889c9dc39aSek #define	DNODE_VERIFY(dn)		dnode_verify(dn)
5899c9dc39aSek #define	FREE_VERIFY(db, start, end, tx)	free_verify(db, start, end, tx)
5909c9dc39aSek 
591fa9e4066Sahrens #else
592fa9e4066Sahrens 
593fa9e4066Sahrens #define	dprintf_dnode(db, fmt, ...)
5949c9dc39aSek #define	DNODE_VERIFY(dn)
5959c9dc39aSek #define	FREE_VERIFY(db, start, end, tx)
596fa9e4066Sahrens 
597fa9e4066Sahrens #endif
598fa9e4066Sahrens 
599fa9e4066Sahrens #ifdef	__cplusplus
600fa9e4066Sahrens }
601fa9e4066Sahrens #endif
602fa9e4066Sahrens 
603fa9e4066Sahrens #endif	/* _SYS_DNODE_H */
604