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