xref: /illumos-gate/usr/src/uts/common/fs/zfs/sys/dnode.h (revision eb633035c80613ec93d62f90482837adaaf21a0a)
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
49fa9e4066Sahrens 
50cdb0ab79Smaybee /*
51cdb0ab79Smaybee  * dnode_next_offset() flags.
52cdb0ab79Smaybee  */
53cdb0ab79Smaybee #define	DNODE_FIND_HOLE		1
54cdb0ab79Smaybee #define	DNODE_FIND_BACKWARDS	2
55cdb0ab79Smaybee #define	DNODE_FIND_HAVELOCK	4
56cdb0ab79Smaybee 
57fa9e4066Sahrens /*
58fa9e4066Sahrens  * Fixed constants.
59fa9e4066Sahrens  */
60fa9e4066Sahrens #define	DNODE_SHIFT		9	/* 512 bytes */
61e94f268aSMatthew Ahrens #define	DN_MIN_INDBLKSHIFT	12	/* 4k */
627de35a3eSPaul Dagnelie /*
637de35a3eSPaul Dagnelie  * If we ever increase this value beyond 20, we need to revisit all logic that
647de35a3eSPaul Dagnelie  * does x << level * ebps to handle overflow.  With a 1M indirect block size,
657de35a3eSPaul Dagnelie  * 4 levels of indirect blocks would not be able to guarantee addressing an
667de35a3eSPaul Dagnelie  * entire object, so 5 levels will be used, but 5 * (20 - 7) = 65.
677de35a3eSPaul Dagnelie  */
684b5c8e93SMatthew Ahrens #define	DN_MAX_INDBLKSHIFT	17	/* 128k */
69fa9e4066Sahrens #define	DNODE_BLOCK_SHIFT	14	/* 16k */
70fa9e4066Sahrens #define	DNODE_CORE_SIZE		64	/* 64 bytes for dnode sans blkptrs */
71fa9e4066Sahrens #define	DN_MAX_OBJECT_SHIFT	48	/* 256 trillion (zfs_fid_t limit) */
72fa9e4066Sahrens #define	DN_MAX_OFFSET_SHIFT	64	/* 2^64 bytes in a dnode */
73fa9e4066Sahrens 
740a586ceaSMark Shellenbaum /*
750a586ceaSMark Shellenbaum  * dnode id flags
760a586ceaSMark Shellenbaum  *
77*eb633035STom Caputi  * Note: a file will never ever have its ids moved from bonus->spill
780a586ceaSMark Shellenbaum  */
790a586ceaSMark Shellenbaum #define	DN_ID_CHKED_BONUS	0x1
800a586ceaSMark Shellenbaum #define	DN_ID_CHKED_SPILL	0x2
810a586ceaSMark Shellenbaum #define	DN_ID_OLD_EXIST		0x4
820a586ceaSMark Shellenbaum #define	DN_ID_NEW_EXIST		0x8
830a586ceaSMark Shellenbaum 
84fa9e4066Sahrens /*
85fa9e4066Sahrens  * Derived constants.
86fa9e4066Sahrens  */
8754811da5SToomas Soome #define	DNODE_MIN_SIZE		(1 << DNODE_SHIFT)
8854811da5SToomas Soome #define	DNODE_MAX_SIZE		(1 << DNODE_BLOCK_SHIFT)
8954811da5SToomas Soome #define	DNODE_BLOCK_SIZE	(1 << DNODE_BLOCK_SHIFT)
9054811da5SToomas Soome #define	DNODE_MIN_SLOTS		(DNODE_MIN_SIZE >> DNODE_SHIFT)
9154811da5SToomas Soome #define	DNODE_MAX_SLOTS		(DNODE_MAX_SIZE >> DNODE_SHIFT)
9254811da5SToomas Soome #define	DN_BONUS_SIZE(dnsize)	((dnsize) - DNODE_CORE_SIZE - \
9354811da5SToomas Soome 	(1 << SPA_BLKPTRSHIFT))
9454811da5SToomas Soome #define	DN_SLOTS_TO_BONUSLEN(slots)	DN_BONUS_SIZE((slots) << DNODE_SHIFT)
9554811da5SToomas Soome #define	DN_OLD_MAX_BONUSLEN	(DN_BONUS_SIZE(DNODE_MIN_SIZE))
9654811da5SToomas Soome #define	DN_MAX_NBLKPTR	((DNODE_MIN_SIZE - DNODE_CORE_SIZE) >> SPA_BLKPTRSHIFT)
9754811da5SToomas Soome #define	DN_MAX_OBJECT		(1ULL << DN_MAX_OBJECT_SHIFT)
9854811da5SToomas Soome #define	DN_ZERO_BONUSLEN	(DN_BONUS_SIZE(DNODE_MAX_SIZE) + 1)
9954811da5SToomas Soome #define	DN_KILL_SPILLBLK	(1)
10054811da5SToomas Soome 
10154811da5SToomas Soome #define	DN_SLOT_UNINIT		((void *)NULL)	/* Uninitialized */
10254811da5SToomas Soome #define	DN_SLOT_FREE		((void *)1UL)	/* Free slot */
10354811da5SToomas Soome #define	DN_SLOT_ALLOCATED	((void *)2UL)	/* Allocated slot */
10454811da5SToomas Soome #define	DN_SLOT_INTERIOR	((void *)3UL)	/* Interior allocated slot */
10554811da5SToomas Soome #define	DN_SLOT_IS_PTR(dn)	((void *)dn > DN_SLOT_INTERIOR)
10654811da5SToomas Soome #define	DN_SLOT_IS_VALID(dn)	((void *)dn != NULL)
107fa9e4066Sahrens 
108fa9e4066Sahrens #define	DNODES_PER_BLOCK_SHIFT	(DNODE_BLOCK_SHIFT - DNODE_SHIFT)
109fa9e4066Sahrens #define	DNODES_PER_BLOCK	(1ULL << DNODES_PER_BLOCK_SHIFT)
1104b5c8e93SMatthew Ahrens 
1114b5c8e93SMatthew Ahrens /*
1124b5c8e93SMatthew Ahrens  * This is inaccurate if the indblkshift of the particular object is not the
1134b5c8e93SMatthew Ahrens  * max.  But it's only used by userland to calculate the zvol reservation.
1144b5c8e93SMatthew Ahrens  */
115fa9e4066Sahrens #define	DNODES_PER_LEVEL_SHIFT	(DN_MAX_INDBLKSHIFT - SPA_BLKPTRSHIFT)
116c1449561SEric Taylor #define	DNODES_PER_LEVEL	(1ULL << DNODES_PER_LEVEL_SHIFT)
117fa9e4066Sahrens 
118fa9e4066Sahrens /* The +2 here is a cheesy way to round up */
119fa9e4066Sahrens #define	DN_MAX_LEVELS	(2 + ((DN_MAX_OFFSET_SHIFT - SPA_MINBLOCKSHIFT) / \
120fa9e4066Sahrens 	(DN_MIN_INDBLKSHIFT - SPA_BLKPTRSHIFT)))
121fa9e4066Sahrens 
122fa9e4066Sahrens #define	DN_BONUS(dnp)	((void*)((dnp)->dn_bonus + \
123fa9e4066Sahrens 	(((dnp)->dn_nblkptr - 1) * sizeof (blkptr_t))))
12454811da5SToomas Soome #define	DN_MAX_BONUS_LEN(dnp) \
12554811da5SToomas Soome 	((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) ? \
12654811da5SToomas Soome 	(uint8_t *)DN_SPILL_BLKPTR(dnp) - (uint8_t *)DN_BONUS(dnp) : \
12754811da5SToomas Soome 	(uint8_t *)(dnp + (dnp->dn_extra_slots + 1)) - (uint8_t *)DN_BONUS(dnp))
128fa9e4066Sahrens 
12999653d4eSeschrock #define	DN_USED_BYTES(dnp) (((dnp)->dn_flags & DNODE_FLAG_USED_BYTES) ? \
13099653d4eSeschrock 	(dnp)->dn_used : (dnp)->dn_used << SPA_MINBLOCKSHIFT)
13199653d4eSeschrock 
132fa9e4066Sahrens #define	EPB(blkshift, typeshift)	(1 << (blkshift - typeshift))
133fa9e4066Sahrens 
134fa9e4066Sahrens struct dmu_buf_impl;
135503ad85cSMatthew Ahrens struct objset;
136fa9e4066Sahrens struct zio;
137fa9e4066Sahrens 
138fa9e4066Sahrens enum dnode_dirtycontext {
139fa9e4066Sahrens 	DN_UNDIRTIED,
140fa9e4066Sahrens 	DN_DIRTY_OPEN,
141fa9e4066Sahrens 	DN_DIRTY_SYNC
142fa9e4066Sahrens };
143fa9e4066Sahrens 
14499653d4eSeschrock /* Is dn_used in bytes?  if not, it's in multiples of SPA_MINBLOCKSIZE */
14514843421SMatthew Ahrens #define	DNODE_FLAG_USED_BYTES		(1<<0)
14614843421SMatthew Ahrens #define	DNODE_FLAG_USERUSED_ACCOUNTED	(1<<1)
14799653d4eSeschrock 
1480a586ceaSMark Shellenbaum /* Does dnode have a SA spill blkptr in bonus? */
1490a586ceaSMark Shellenbaum #define	DNODE_FLAG_SPILL_BLKPTR	(1<<2)
1500a586ceaSMark Shellenbaum 
15154811da5SToomas Soome /*
15254811da5SToomas Soome  * VARIABLE-LENGTH (LARGE) DNODES
15354811da5SToomas Soome  *
15454811da5SToomas Soome  * The motivation for variable-length dnodes is to eliminate the overhead
15554811da5SToomas Soome  * associated with using spill blocks.  Spill blocks are used to store
15654811da5SToomas Soome  * system attribute data (i.e. file metadata) that does not fit in the
15754811da5SToomas Soome  * dnode's bonus buffer. By allowing a larger bonus buffer area the use of
15854811da5SToomas Soome  * a spill block can be avoided.  Spill blocks potentially incur an
15954811da5SToomas Soome  * additional read I/O for every dnode in a dnode block. As a worst case
16054811da5SToomas Soome  * example, reading 32 dnodes from a 16k dnode block and all of the spill
16154811da5SToomas Soome  * blocks could issue 33 separate reads. Now suppose those dnodes have size
16254811da5SToomas Soome  * 1024 and therefore don't need spill blocks. Then the worst case number
16354811da5SToomas Soome  * of blocks read is reduced to from 33 to two--one per dnode block.
16454811da5SToomas Soome  *
16554811da5SToomas Soome  * ZFS-on-Linux systems that make heavy use of extended attributes benefit
16654811da5SToomas Soome  * from this feature. In particular, ZFS-on-Linux supports the xattr=sa
16754811da5SToomas Soome  * dataset property which allows file extended attribute data to be stored
16854811da5SToomas Soome  * in the dnode bonus buffer as an alternative to the traditional
16954811da5SToomas Soome  * directory-based format. Workloads such as SELinux and the Lustre
17054811da5SToomas Soome  * distributed filesystem often store enough xattr data to force spill
17154811da5SToomas Soome  * blocks when xattr=sa is in effect. Large dnodes may therefore provide a
17254811da5SToomas Soome  * performance benefit to such systems. Other use cases that benefit from
17354811da5SToomas Soome  * this feature include files with large ACLs and symbolic links with long
17454811da5SToomas Soome  * target names.
17554811da5SToomas Soome  *
17654811da5SToomas Soome  * The size of a dnode may be a multiple of 512 bytes up to the size of a
17754811da5SToomas Soome  * dnode block (currently 16384 bytes). The dn_extra_slots field of the
17854811da5SToomas Soome  * on-disk dnode_phys_t structure describes the size of the physical dnode
17954811da5SToomas Soome  * on disk. The field represents how many "extra" dnode_phys_t slots a
18054811da5SToomas Soome  * dnode consumes in its dnode block. This convention results in a value of
18154811da5SToomas Soome  * 0 for 512 byte dnodes which preserves on-disk format compatibility with
18254811da5SToomas Soome  * older software which doesn't support large dnodes.
18354811da5SToomas Soome  *
18454811da5SToomas Soome  * Similarly, the in-memory dnode_t structure has a dn_num_slots field
18554811da5SToomas Soome  * to represent the total number of dnode_phys_t slots consumed on disk.
18654811da5SToomas Soome  * Thus dn->dn_num_slots is 1 greater than the corresponding
18754811da5SToomas Soome  * dnp->dn_extra_slots. This difference in convention was adopted
18854811da5SToomas Soome  * because, unlike on-disk structures, backward compatibility is not a
18954811da5SToomas Soome  * concern for in-memory objects, so we used a more natural way to
19054811da5SToomas Soome  * represent size for a dnode_t.
19154811da5SToomas Soome  *
19254811da5SToomas Soome  * The default size for newly created dnodes is determined by the value of
19354811da5SToomas Soome  * the "dnodesize" dataset property. By default the property is set to
19454811da5SToomas Soome  * "legacy" which is compatible with older software. Setting the property
19554811da5SToomas Soome  * to "auto" will allow the filesystem to choose the most suitable dnode
19654811da5SToomas Soome  * size. Currently this just sets the default dnode size to 1k, but future
19754811da5SToomas Soome  * code improvements could dynamically choose a size based on observed
19854811da5SToomas Soome  * workload patterns. Dnodes of varying sizes can coexist within the same
19954811da5SToomas Soome  * dataset and even within the same dnode block.
20054811da5SToomas Soome  */
20154811da5SToomas Soome 
202*eb633035STom Caputi #define	DNODE_CRYPT_PORTABLE_FLAGS_MASK		(DNODE_FLAG_SPILL_BLKPTR)
203*eb633035STom Caputi 
204fa9e4066Sahrens typedef struct dnode_phys {
205fa9e4066Sahrens 	uint8_t dn_type;		/* dmu_object_type_t */
206fa9e4066Sahrens 	uint8_t dn_indblkshift;		/* ln2(indirect block size) */
207fa9e4066Sahrens 	uint8_t dn_nlevels;		/* 1=dn_blkptr->data blocks */
208fa9e4066Sahrens 	uint8_t dn_nblkptr;		/* length of dn_blkptr */
209fa9e4066Sahrens 	uint8_t dn_bonustype;		/* type of data in bonus buffer */
210fa9e4066Sahrens 	uint8_t	dn_checksum;		/* ZIO_CHECKSUM type */
211fa9e4066Sahrens 	uint8_t	dn_compress;		/* ZIO_COMPRESS type */
21299653d4eSeschrock 	uint8_t dn_flags;		/* DNODE_FLAG_* */
213fa9e4066Sahrens 	uint16_t dn_datablkszsec;	/* data block size in 512b sectors */
214fa9e4066Sahrens 	uint16_t dn_bonuslen;		/* length of dn_bonus */
21554811da5SToomas Soome 	uint8_t dn_extra_slots;		/* # of subsequent slots consumed */
21654811da5SToomas Soome 	uint8_t dn_pad2[3];
217fa9e4066Sahrens 
218fa9e4066Sahrens 	/* accounting is protected by dn_dirty_mtx */
219fa9e4066Sahrens 	uint64_t dn_maxblkid;		/* largest allocated block ID */
22099653d4eSeschrock 	uint64_t dn_used;		/* bytes (or sectors) of disk space */
221fa9e4066Sahrens 
222*eb633035STom Caputi 	/*
223*eb633035STom Caputi 	 * Both dn_pad2 and dn_pad3 are protected by the block's MAC. This
224*eb633035STom Caputi 	 * allows us to protect any fields that might be added here in the
225*eb633035STom Caputi 	 * future. In either case, developers will want to check
226*eb633035STom Caputi 	 * zio_crypt_init_uios_dnode() to ensure the new field is being
227*eb633035STom Caputi 	 * protected properly.
228*eb633035STom Caputi 	 */
229fa9e4066Sahrens 	uint64_t dn_pad3[4];
23054811da5SToomas Soome 	union {
23154811da5SToomas Soome 		blkptr_t dn_blkptr[1+DN_OLD_MAX_BONUSLEN/sizeof (blkptr_t)];
23254811da5SToomas Soome 		struct {
23354811da5SToomas Soome 			blkptr_t __dn_ignore1;
23454811da5SToomas Soome 			uint8_t dn_bonus[DN_OLD_MAX_BONUSLEN];
23554811da5SToomas Soome 		};
23654811da5SToomas Soome 		struct {
23754811da5SToomas Soome 			blkptr_t __dn_ignore2;
23854811da5SToomas Soome 			uint8_t __dn_ignore3[DN_OLD_MAX_BONUSLEN -
23954811da5SToomas Soome 			    sizeof (blkptr_t)];
24054811da5SToomas Soome 			blkptr_t dn_spill;
24154811da5SToomas Soome 		};
24254811da5SToomas Soome 	};
243fa9e4066Sahrens } dnode_phys_t;
244fa9e4066Sahrens 
245*eb633035STom Caputi #define	DN_SPILL_BLKPTR(dnp)	((blkptr_t *)((char *)(dnp) +		\
246*eb633035STom Caputi 	(((dnp)->dn_extra_slots + 1) << DNODE_SHIFT) - (1 << SPA_BLKPTRSHIFT)))
24754811da5SToomas Soome 
24879d72832SMatthew Ahrens struct dnode {
249fa9e4066Sahrens 	/*
250f7170741SWill Andrews 	 * Protects the structure of the dnode, including the number of levels
251f7170741SWill Andrews 	 * of indirection (dn_nlevels), dn_maxblkid, and dn_next_*
252fa9e4066Sahrens 	 */
253fa9e4066Sahrens 	krwlock_t dn_struct_rwlock;
254fa9e4066Sahrens 
25514843421SMatthew Ahrens 	/* Our link on dn_objset->os_dnodes list; protected by os_lock.  */
256fa9e4066Sahrens 	list_node_t dn_link;
257fa9e4066Sahrens 
258fa9e4066Sahrens 	/* immutable: */
259503ad85cSMatthew Ahrens 	struct objset *dn_objset;
260fa9e4066Sahrens 	uint64_t dn_object;
261fa9e4066Sahrens 	struct dmu_buf_impl *dn_dbuf;
262744947dcSTom Erickson 	struct dnode_handle *dn_handle;
263fa9e4066Sahrens 	dnode_phys_t *dn_phys; /* pointer into dn->dn_dbuf->db.db_data */
264fa9e4066Sahrens 
265fa9e4066Sahrens 	/*
266c543ec06Sahrens 	 * Copies of stuff in dn_phys.  They're valid in the open
267c543ec06Sahrens 	 * context (eg. even before the dnode is first synced).
268c543ec06Sahrens 	 * Where necessary, these are protected by dn_struct_rwlock.
269fa9e4066Sahrens 	 */
270c543ec06Sahrens 	dmu_object_type_t dn_type;	/* object type */
271c543ec06Sahrens 	uint16_t dn_bonuslen;		/* bonus length */
272c543ec06Sahrens 	uint8_t dn_bonustype;		/* bonus type */
273fa9e4066Sahrens 	uint8_t dn_nblkptr;		/* number of blkptrs (immutable) */
274fa9e4066Sahrens 	uint8_t dn_checksum;		/* ZIO_CHECKSUM type */
275fa9e4066Sahrens 	uint8_t dn_compress;		/* ZIO_COMPRESS type */
276fa9e4066Sahrens 	uint8_t dn_nlevels;
277fa9e4066Sahrens 	uint8_t dn_indblkshift;
278c543ec06Sahrens 	uint8_t dn_datablkshift;	/* zero if blksz not power of 2! */
279744947dcSTom Erickson 	uint8_t dn_moved;		/* Has this dnode been moved? */
280c543ec06Sahrens 	uint16_t dn_datablkszsec;	/* in 512b sectors */
281c543ec06Sahrens 	uint32_t dn_datablksz;		/* in bytes */
282c543ec06Sahrens 	uint64_t dn_maxblkid;
2832acef22dSMatthew Ahrens 	uint8_t dn_next_type[TXG_SIZE];
28454811da5SToomas Soome 	uint8_t dn_num_slots;		/* metadnode slots consumed on disk */
285da03de99SMark Maybee 	uint8_t dn_next_nblkptr[TXG_SIZE];
286fa9e4066Sahrens 	uint8_t dn_next_nlevels[TXG_SIZE];
287fa9e4066Sahrens 	uint8_t dn_next_indblkshift[TXG_SIZE];
2880a586ceaSMark Shellenbaum 	uint8_t dn_next_bonustype[TXG_SIZE];
2890a586ceaSMark Shellenbaum 	uint8_t dn_rm_spillblk[TXG_SIZE];	/* for removing spill blk */
2901934e92fSmaybee 	uint16_t dn_next_bonuslen[TXG_SIZE];
291c543ec06Sahrens 	uint32_t dn_next_blksz[TXG_SIZE];	/* next block size in bytes */
292*eb633035STom Caputi 	uint64_t dn_next_maxblkid[TXG_SIZE];	/* next maxblkid in bytes */
293fa9e4066Sahrens 
294744947dcSTom Erickson 	/* protected by dn_dbufs_mtx; declared here to fill 32-bit hole */
295744947dcSTom Erickson 	uint32_t dn_dbufs_count;	/* count of dn_dbufs */
296744947dcSTom Erickson 
297fa9e4066Sahrens 	/* protected by os_lock: */
29894c2d0ebSMatthew Ahrens 	multilist_node_t dn_dirty_link[TXG_SIZE]; /* next on dataset's dirty */
299fa9e4066Sahrens 
300fa9e4066Sahrens 	/* protected by dn_mtx: */
301fa9e4066Sahrens 	kmutex_t dn_mtx;
302c717a561Smaybee 	list_t dn_dirty_records[TXG_SIZE];
303bf16b11eSMatthew Ahrens 	struct range_tree *dn_free_ranges[TXG_SIZE];
304fa9e4066Sahrens 	uint64_t dn_allocated_txg;
305fa9e4066Sahrens 	uint64_t dn_free_txg;
306fa9e4066Sahrens 	uint64_t dn_assigned_txg;
307aa02ea01STom Caputi 	uint64_t dn_dirty_txg;			/* txg dnode was last dirtied */
308fa9e4066Sahrens 	kcondvar_t dn_notxholds;
309fa9e4066Sahrens 	enum dnode_dirtycontext dn_dirtyctx;
310fa9e4066Sahrens 	uint8_t *dn_dirtyctx_firstset;		/* dbg: contents meaningless */
311fa9e4066Sahrens 
312fa9e4066Sahrens 	/* protected by own devices */
313e914ace2STim Schumacher 	zfs_refcount_t dn_tx_holds;
314e914ace2STim Schumacher 	zfs_refcount_t dn_holds;
315fa9e4066Sahrens 
316fa9e4066Sahrens 	kmutex_t dn_dbufs_mtx;
31786bb58aeSAlex Reece 	/*
31886bb58aeSAlex Reece 	 * Descendent dbufs, ordered by dbuf_compare. Note that dn_dbufs
31986bb58aeSAlex Reece 	 * can contain multiple dbufs of the same (level, blkid) when a
32086bb58aeSAlex Reece 	 * dbuf is marked DB_EVICTING without being removed from
32186bb58aeSAlex Reece 	 * dn_dbufs. To maintain the avl invariant that there cannot be
32286bb58aeSAlex Reece 	 * duplicate entries, we order the dbufs by an arbitrary value -
32386bb58aeSAlex Reece 	 * their address in memory. This means that dn_dbufs cannot be used to
32486bb58aeSAlex Reece 	 * directly look up a dbuf. Instead, callers must use avl_walk, have
32586bb58aeSAlex Reece 	 * a reference to the dbuf, or look up a non-existant node with
32686bb58aeSAlex Reece 	 * db_state = DB_SEARCH (see dbuf_free_range for an example).
32786bb58aeSAlex Reece 	 */
32886bb58aeSAlex Reece 	avl_tree_t dn_dbufs;
329744947dcSTom Erickson 
330744947dcSTom Erickson 	/* protected by dn_struct_rwlock */
331ea8dc4b6Seschrock 	struct dmu_buf_impl *dn_bonus;	/* bonus buffer dbuf */
332744947dcSTom Erickson 
3330a586ceaSMark Shellenbaum 	boolean_t dn_have_spill;	/* have spill or are spilling */
334fa9e4066Sahrens 
335c717a561Smaybee 	/* parent IO for current sync write */
336c717a561Smaybee 	zio_t *dn_zio;
337c717a561Smaybee 
33814843421SMatthew Ahrens 	/* used in syncing context */
3390a586ceaSMark Shellenbaum 	uint64_t dn_oldused;	/* old phys used bytes */
3400a586ceaSMark Shellenbaum 	uint64_t dn_oldflags;	/* old phys dn_flags */
3410a586ceaSMark Shellenbaum 	uint64_t dn_olduid, dn_oldgid;
3420a586ceaSMark Shellenbaum 	uint64_t dn_newuid, dn_newgid;
3430a586ceaSMark Shellenbaum 	int dn_id_flags;
34414843421SMatthew Ahrens 
345fa9e4066Sahrens 	/* holds prefetch structure */
346fa9e4066Sahrens 	struct zfetch	dn_zfetch;
34779d72832SMatthew Ahrens };
348fa9e4066Sahrens 
349*eb633035STom Caputi /*
350*eb633035STom Caputi  * We use this (otherwise unused) bit to indicate if the value of
351*eb633035STom Caputi  * dn_next_maxblkid[txgoff] is valid to use in dnode_sync().
352*eb633035STom Caputi  */
353*eb633035STom Caputi #define	DMU_NEXT_MAXBLKID_SET		(1ULL << 63)
354*eb633035STom Caputi 
355744947dcSTom Erickson /*
356744947dcSTom Erickson  * Adds a level of indirection between the dbuf and the dnode to avoid
357744947dcSTom Erickson  * iterating descendent dbufs in dnode_move(). Handles are not allocated
358744947dcSTom Erickson  * individually, but as an array of child dnodes in dnode_hold_impl().
359744947dcSTom Erickson  */
360744947dcSTom Erickson typedef struct dnode_handle {
361744947dcSTom Erickson 	/* Protects dnh_dnode from modification by dnode_move(). */
362744947dcSTom Erickson 	zrlock_t dnh_zrlock;
363744947dcSTom Erickson 	dnode_t *dnh_dnode;
364744947dcSTom Erickson } dnode_handle_t;
365744947dcSTom Erickson 
366744947dcSTom Erickson typedef struct dnode_children {
367bc9014e6SJustin Gibbs 	dmu_buf_user_t dnc_dbu;		/* User evict data */
368744947dcSTom Erickson 	size_t dnc_count;		/* number of children */
3697f18da4cSJustin T. Gibbs 	dnode_handle_t dnc_children[];	/* sized dynamically */
370744947dcSTom Erickson } dnode_children_t;
371744947dcSTom Erickson 
372fa9e4066Sahrens typedef struct free_range {
373fa9e4066Sahrens 	avl_node_t fr_node;
374fa9e4066Sahrens 	uint64_t fr_blkid;
375fa9e4066Sahrens 	uint64_t fr_nblks;
376fa9e4066Sahrens } free_range_t;
377fa9e4066Sahrens 
378bc9014e6SJustin Gibbs void dnode_special_open(struct objset *dd, dnode_phys_t *dnp,
379744947dcSTom Erickson     uint64_t object, dnode_handle_t *dnh);
380744947dcSTom Erickson void dnode_special_close(dnode_handle_t *dnh);
381fa9e4066Sahrens 
3821934e92fSmaybee void dnode_setbonuslen(dnode_t *dn, int newsize, dmu_tx_t *tx);
3830a586ceaSMark Shellenbaum void dnode_setbonus_type(dnode_t *dn, dmu_object_type_t, dmu_tx_t *tx);
3840a586ceaSMark Shellenbaum void dnode_rm_spill(dnode_t *dn, dmu_tx_t *tx);
3850a586ceaSMark Shellenbaum 
386503ad85cSMatthew Ahrens int dnode_hold(struct objset *dd, uint64_t object,
387ea8dc4b6Seschrock     void *ref, dnode_t **dnp);
38854811da5SToomas Soome int dnode_hold_impl(struct objset *dd, uint64_t object, int flag, int dn_slots,
389ea8dc4b6Seschrock     void *ref, dnode_t **dnp);
3901934e92fSmaybee boolean_t dnode_add_ref(dnode_t *dn, void *ref);
391fa9e4066Sahrens void dnode_rele(dnode_t *dn, void *ref);
392c2919acbSMatthew Ahrens void dnode_rele_and_unlock(dnode_t *dn, void *tag, boolean_t evicting);
393fa9e4066Sahrens void dnode_setdirty(dnode_t *dn, dmu_tx_t *tx);
394c717a561Smaybee void dnode_sync(dnode_t *dn, dmu_tx_t *tx);
395fa9e4066Sahrens void dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs,
39654811da5SToomas Soome     dmu_object_type_t bonustype, int bonuslen, int dn_slots, dmu_tx_t *tx);
397fa9e4066Sahrens void dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize,
398*eb633035STom Caputi     dmu_object_type_t bonustype, int bonuslen, int dn_slots,
399*eb633035STom Caputi     boolean_t keep_spill, dmu_tx_t *tx);
400fa9e4066Sahrens void dnode_free(dnode_t *dn, dmu_tx_t *tx);
401fa9e4066Sahrens void dnode_byteswap(dnode_phys_t *dnp);
402fa9e4066Sahrens void dnode_buf_byteswap(void *buf, size_t size);
403fa9e4066Sahrens void dnode_verify(dnode_t *dn);
404*eb633035STom Caputi int dnode_set_nlevels(dnode_t *dn, int nlevels, dmu_tx_t *tx);
405fa9e4066Sahrens int dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx);
406fa9e4066Sahrens void dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx);
407fa9e4066Sahrens void dnode_diduse_space(dnode_t *dn, int64_t space);
408*eb633035STom Caputi void dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx,
409*eb633035STom Caputi     boolean_t have_read, boolean_t force);
410fa9e4066Sahrens uint64_t dnode_block_freed(dnode_t *dn, uint64_t blkid);
411fa9e4066Sahrens void dnode_init(void);
412fa9e4066Sahrens void dnode_fini(void);
413cdb0ab79Smaybee int dnode_next_offset(dnode_t *dn, int flags, uint64_t *off,
414cdb0ab79Smaybee     int minlvl, uint64_t blkfill, uint64_t txg);
4151934e92fSmaybee void dnode_evict_dbufs(dnode_t *dn);
416cd485b49SJustin T. Gibbs void dnode_evict_bonus(dnode_t *dn);
41754811da5SToomas Soome void dnode_free_interior_slots(dnode_t *dn);
4185cabbc6bSPrashanth Sreenivasa boolean_t dnode_needs_remap(const dnode_t *dn);
419fa9e4066Sahrens 
420aa02ea01STom Caputi #define	DNODE_IS_DIRTY(_dn)						\
421aa02ea01STom Caputi 	((_dn)->dn_dirty_txg >= spa_syncing_txg((_dn)->dn_objset->os_spa))
422aa02ea01STom Caputi 
423cb92f413SAlexander Motin #define	DNODE_IS_CACHEABLE(_dn)						\
424cb92f413SAlexander Motin 	((_dn)->dn_objset->os_primary_cache == ZFS_CACHE_ALL ||		\
425cb92f413SAlexander Motin 	(DMU_OT_IS_METADATA((_dn)->dn_type) &&				\
426cb92f413SAlexander Motin 	(_dn)->dn_objset->os_primary_cache == ZFS_CACHE_METADATA))
427cb92f413SAlexander Motin 
428cb92f413SAlexander Motin #define	DNODE_META_IS_CACHEABLE(_dn)					\
429cb92f413SAlexander Motin 	((_dn)->dn_objset->os_primary_cache == ZFS_CACHE_ALL ||		\
430cb92f413SAlexander Motin 	(_dn)->dn_objset->os_primary_cache == ZFS_CACHE_METADATA)
431cb92f413SAlexander Motin 
43254811da5SToomas Soome /*
43354811da5SToomas Soome  * Used for dnodestats kstat.
43454811da5SToomas Soome  */
43554811da5SToomas Soome typedef struct dnode_stats {
43654811da5SToomas Soome 	/*
43754811da5SToomas Soome 	 * Number of failed attempts to hold a meta dnode dbuf.
43854811da5SToomas Soome 	 */
43954811da5SToomas Soome 	kstat_named_t dnode_hold_dbuf_hold;
44054811da5SToomas Soome 	/*
44154811da5SToomas Soome 	 * Number of failed attempts to read a meta dnode dbuf.
44254811da5SToomas Soome 	 */
44354811da5SToomas Soome 	kstat_named_t dnode_hold_dbuf_read;
44454811da5SToomas Soome 	/*
44554811da5SToomas Soome 	 * Number of times dnode_hold(..., DNODE_MUST_BE_ALLOCATED) was able
44654811da5SToomas Soome 	 * to hold the requested object number which was allocated.  This is
44754811da5SToomas Soome 	 * the common case when looking up any allocated object number.
44854811da5SToomas Soome 	 */
44954811da5SToomas Soome 	kstat_named_t dnode_hold_alloc_hits;
45054811da5SToomas Soome 	/*
45154811da5SToomas Soome 	 * Number of times dnode_hold(..., DNODE_MUST_BE_ALLOCATED) was not
45254811da5SToomas Soome 	 * able to hold the request object number because it was not allocated.
45354811da5SToomas Soome 	 */
45454811da5SToomas Soome 	kstat_named_t dnode_hold_alloc_misses;
45554811da5SToomas Soome 	/*
45654811da5SToomas Soome 	 * Number of times dnode_hold(..., DNODE_MUST_BE_ALLOCATED) was not
45754811da5SToomas Soome 	 * able to hold the request object number because the object number
45854811da5SToomas Soome 	 * refers to an interior large dnode slot.
45954811da5SToomas Soome 	 */
46054811da5SToomas Soome 	kstat_named_t dnode_hold_alloc_interior;
46154811da5SToomas Soome 	/*
46254811da5SToomas Soome 	 * Number of times dnode_hold(..., DNODE_MUST_BE_ALLOCATED) needed
46354811da5SToomas Soome 	 * to retry acquiring slot zrl locks due to contention.
46454811da5SToomas Soome 	 */
46554811da5SToomas Soome 	kstat_named_t dnode_hold_alloc_lock_retry;
46654811da5SToomas Soome 	/*
46754811da5SToomas Soome 	 * Number of times dnode_hold(..., DNODE_MUST_BE_ALLOCATED) did not
46854811da5SToomas Soome 	 * need to create the dnode because another thread did so after
46954811da5SToomas Soome 	 * dropping the read lock but before acquiring the write lock.
47054811da5SToomas Soome 	 */
47154811da5SToomas Soome 	kstat_named_t dnode_hold_alloc_lock_misses;
47254811da5SToomas Soome 	/*
47354811da5SToomas Soome 	 * Number of times dnode_hold(..., DNODE_MUST_BE_ALLOCATED) found
47454811da5SToomas Soome 	 * a free dnode instantiated by dnode_create() but not yet allocated
47554811da5SToomas Soome 	 * by dnode_allocate().
47654811da5SToomas Soome 	 */
47754811da5SToomas Soome 	kstat_named_t dnode_hold_alloc_type_none;
47854811da5SToomas Soome 	/*
47954811da5SToomas Soome 	 * Number of times dnode_hold(..., DNODE_MUST_BE_FREE) was able
48054811da5SToomas Soome 	 * to hold the requested range of free dnode slots.
48154811da5SToomas Soome 	 */
48254811da5SToomas Soome 	kstat_named_t dnode_hold_free_hits;
48354811da5SToomas Soome 	/*
48454811da5SToomas Soome 	 * Number of times dnode_hold(..., DNODE_MUST_BE_FREE) was not
48554811da5SToomas Soome 	 * able to hold the requested range of free dnode slots because
48654811da5SToomas Soome 	 * at least one slot was allocated.
48754811da5SToomas Soome 	 */
48854811da5SToomas Soome 	kstat_named_t dnode_hold_free_misses;
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 	 * after acquiring the zrl lock at least one slot was allocated.
49354811da5SToomas Soome 	 */
49454811da5SToomas Soome 	kstat_named_t dnode_hold_free_lock_misses;
49554811da5SToomas Soome 	/*
49654811da5SToomas Soome 	 * Number of times dnode_hold(..., DNODE_MUST_BE_FREE) needed
49754811da5SToomas Soome 	 * to retry acquiring slot zrl locks due to contention.
49854811da5SToomas Soome 	 */
49954811da5SToomas Soome 	kstat_named_t dnode_hold_free_lock_retry;
50054811da5SToomas Soome 	/*
50154811da5SToomas Soome 	 * Number of times dnode_hold(..., DNODE_MUST_BE_FREE) requested
50254811da5SToomas Soome 	 * a range of dnode slots which were held by another thread.
50354811da5SToomas Soome 	 */
50454811da5SToomas Soome 	kstat_named_t dnode_hold_free_refcount;
50554811da5SToomas Soome 	/*
50654811da5SToomas Soome 	 * Number of times dnode_hold(..., DNODE_MUST_BE_FREE) requested
50754811da5SToomas Soome 	 * a range of dnode slots which would overflow the dnode_phys_t.
50854811da5SToomas Soome 	 */
50954811da5SToomas Soome 	kstat_named_t dnode_hold_free_overflow;
51054811da5SToomas Soome 	/*
51154811da5SToomas Soome 	 * Number of times a dnode_hold(...) was attempted on a dnode
51254811da5SToomas Soome 	 * which had already been unlinked in an earlier txg.
51354811da5SToomas Soome 	 */
51454811da5SToomas Soome 	kstat_named_t dnode_hold_free_txg;
51554811da5SToomas Soome 	/*
51654811da5SToomas Soome 	 * Number of times dnode_free_interior_slots() needed to retry
51754811da5SToomas Soome 	 * acquiring a slot zrl lock due to contention.
51854811da5SToomas Soome 	 */
51954811da5SToomas Soome 	kstat_named_t dnode_free_interior_lock_retry;
52054811da5SToomas Soome 	/*
52154811da5SToomas Soome 	 * Number of new dnodes allocated by dnode_allocate().
52254811da5SToomas Soome 	 */
52354811da5SToomas Soome 	kstat_named_t dnode_allocate;
52454811da5SToomas Soome 	/*
52554811da5SToomas Soome 	 * Number of dnodes re-allocated by dnode_reallocate().
52654811da5SToomas Soome 	 */
52754811da5SToomas Soome 	kstat_named_t dnode_reallocate;
52854811da5SToomas Soome 	/*
52954811da5SToomas Soome 	 * Number of meta dnode dbufs evicted.
53054811da5SToomas Soome 	 */
53154811da5SToomas Soome 	kstat_named_t dnode_buf_evict;
53254811da5SToomas Soome 	/*
53354811da5SToomas Soome 	 * Number of times dmu_object_alloc*() reached the end of the existing
53454811da5SToomas Soome 	 * object ID chunk and advanced to a new one.
53554811da5SToomas Soome 	 */
53654811da5SToomas Soome 	kstat_named_t dnode_alloc_next_chunk;
53754811da5SToomas Soome 	/*
53854811da5SToomas Soome 	 * Number of times multiple threads attempted to allocate a dnode
53954811da5SToomas Soome 	 * from the same block of free dnodes.
54054811da5SToomas Soome 	 */
54154811da5SToomas Soome 	kstat_named_t dnode_alloc_race;
54254811da5SToomas Soome 	/*
54354811da5SToomas Soome 	 * Number of times dmu_object_alloc*() was forced to advance to the
54454811da5SToomas Soome 	 * next meta dnode dbuf due to an error from  dmu_object_next().
54554811da5SToomas Soome 	 */
54654811da5SToomas Soome 	kstat_named_t dnode_alloc_next_block;
54754811da5SToomas Soome 	/*
54854811da5SToomas Soome 	 * Statistics for tracking dnodes which have been moved.
54954811da5SToomas Soome 	 */
55054811da5SToomas Soome 	kstat_named_t dnode_move_invalid;
55154811da5SToomas Soome 	kstat_named_t dnode_move_recheck1;
55254811da5SToomas Soome 	kstat_named_t dnode_move_recheck2;
55354811da5SToomas Soome 	kstat_named_t dnode_move_special;
55454811da5SToomas Soome 	kstat_named_t dnode_move_handle;
55554811da5SToomas Soome 	kstat_named_t dnode_move_rwlock;
55654811da5SToomas Soome 	kstat_named_t dnode_move_active;
55754811da5SToomas Soome } dnode_stats_t;
55854811da5SToomas Soome 
55954811da5SToomas Soome extern dnode_stats_t dnode_stats;
56054811da5SToomas Soome 
56154811da5SToomas Soome #define	DNODE_STAT_INCR(stat, val) \
56254811da5SToomas Soome     atomic_add_64(&dnode_stats.stat.value.ui64, (val));
56354811da5SToomas Soome #define	DNODE_STAT_BUMP(stat) \
56454811da5SToomas Soome     DNODE_STAT_INCR(stat, 1);
56554811da5SToomas Soome 
566fa9e4066Sahrens #ifdef ZFS_DEBUG
567fa9e4066Sahrens 
568fa9e4066Sahrens /*
569fa9e4066Sahrens  * There should be a ## between the string literal and fmt, to make it
570fa9e4066Sahrens  * clear that we're joining two strings together, but that piece of shit
571fa9e4066Sahrens  * gcc doesn't support that preprocessor token.
572fa9e4066Sahrens  */
573fa9e4066Sahrens #define	dprintf_dnode(dn, fmt, ...) do { \
574fa9e4066Sahrens 	if (zfs_flags & ZFS_DEBUG_DPRINTF) { \
575fa9e4066Sahrens 	char __db_buf[32]; \
576fa9e4066Sahrens 	uint64_t __db_obj = (dn)->dn_object; \
577fa9e4066Sahrens 	if (__db_obj == DMU_META_DNODE_OBJECT) \
578fa9e4066Sahrens 		(void) strcpy(__db_buf, "mdn"); \
579fa9e4066Sahrens 	else \
580fa9e4066Sahrens 		(void) snprintf(__db_buf, sizeof (__db_buf), "%lld", \
581fa9e4066Sahrens 		    (u_longlong_t)__db_obj);\
582fa9e4066Sahrens 	dprintf_ds((dn)->dn_objset->os_dsl_dataset, "obj=%s " fmt, \
583fa9e4066Sahrens 	    __db_buf, __VA_ARGS__); \
584fa9e4066Sahrens 	} \
585fa9e4066Sahrens _NOTE(CONSTCOND) } while (0)
586fa9e4066Sahrens 
5879c9dc39aSek #define	DNODE_VERIFY(dn)		dnode_verify(dn)
5889c9dc39aSek #define	FREE_VERIFY(db, start, end, tx)	free_verify(db, start, end, tx)
5899c9dc39aSek 
590fa9e4066Sahrens #else
591fa9e4066Sahrens 
592fa9e4066Sahrens #define	dprintf_dnode(db, fmt, ...)
5939c9dc39aSek #define	DNODE_VERIFY(dn)
5949c9dc39aSek #define	FREE_VERIFY(db, start, end, tx)
595fa9e4066Sahrens 
596fa9e4066Sahrens #endif
597fa9e4066Sahrens 
598fa9e4066Sahrens #ifdef	__cplusplus
599fa9e4066Sahrens }
600fa9e4066Sahrens #endif
601fa9e4066Sahrens 
602fa9e4066Sahrens #endif	/* _SYS_DNODE_H */
603