1*22ce4affSfengbojiang /* 2*22ce4affSfengbojiang * CDDL HEADER START 3*22ce4affSfengbojiang * 4*22ce4affSfengbojiang * The contents of this file are subject to the terms of the 5*22ce4affSfengbojiang * Common Development and Distribution License (the "License"). 6*22ce4affSfengbojiang * You may not use this file except in compliance with the License. 7*22ce4affSfengbojiang * 8*22ce4affSfengbojiang * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*22ce4affSfengbojiang * or http://www.opensolaris.org/os/licensing. 10*22ce4affSfengbojiang * See the License for the specific language governing permissions 11*22ce4affSfengbojiang * and limitations under the License. 12*22ce4affSfengbojiang * 13*22ce4affSfengbojiang * When distributing Covered Code, include this CDDL HEADER in each 14*22ce4affSfengbojiang * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*22ce4affSfengbojiang * If applicable, add the following below this CDDL HEADER, with the 16*22ce4affSfengbojiang * fields enclosed by brackets "[]" replaced with your own identifying 17*22ce4affSfengbojiang * information: Portions Copyright [yyyy] [name of copyright owner] 18*22ce4affSfengbojiang * 19*22ce4affSfengbojiang * CDDL HEADER END 20*22ce4affSfengbojiang */ 21*22ce4affSfengbojiang /* 22*22ce4affSfengbojiang * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23*22ce4affSfengbojiang * Copyright (c) 2012, 2018 by Delphix. All rights reserved. 24*22ce4affSfengbojiang * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved. 25*22ce4affSfengbojiang */ 26*22ce4affSfengbojiang 27*22ce4affSfengbojiang #ifndef _SYS_DNODE_H 28*22ce4affSfengbojiang #define _SYS_DNODE_H 29*22ce4affSfengbojiang 30*22ce4affSfengbojiang #include <sys/zfs_context.h> 31*22ce4affSfengbojiang #include <sys/avl.h> 32*22ce4affSfengbojiang #include <sys/spa.h> 33*22ce4affSfengbojiang #include <sys/txg.h> 34*22ce4affSfengbojiang #include <sys/zio.h> 35*22ce4affSfengbojiang #include <sys/zfs_refcount.h> 36*22ce4affSfengbojiang #include <sys/dmu_zfetch.h> 37*22ce4affSfengbojiang #include <sys/zrlock.h> 38*22ce4affSfengbojiang #include <sys/multilist.h> 39*22ce4affSfengbojiang 40*22ce4affSfengbojiang #ifdef __cplusplus 41*22ce4affSfengbojiang extern "C" { 42*22ce4affSfengbojiang #endif 43*22ce4affSfengbojiang 44*22ce4affSfengbojiang /* 45*22ce4affSfengbojiang * dnode_hold() flags. 46*22ce4affSfengbojiang */ 47*22ce4affSfengbojiang #define DNODE_MUST_BE_ALLOCATED 1 48*22ce4affSfengbojiang #define DNODE_MUST_BE_FREE 2 49*22ce4affSfengbojiang #define DNODE_DRY_RUN 4 50*22ce4affSfengbojiang 51*22ce4affSfengbojiang /* 52*22ce4affSfengbojiang * dnode_next_offset() flags. 53*22ce4affSfengbojiang */ 54*22ce4affSfengbojiang #define DNODE_FIND_HOLE 1 55*22ce4affSfengbojiang #define DNODE_FIND_BACKWARDS 2 56*22ce4affSfengbojiang #define DNODE_FIND_HAVELOCK 4 57*22ce4affSfengbojiang 58*22ce4affSfengbojiang /* 59*22ce4affSfengbojiang * Fixed constants. 60*22ce4affSfengbojiang */ 61*22ce4affSfengbojiang #define DNODE_SHIFT 9 /* 512 bytes */ 62*22ce4affSfengbojiang #define DN_MIN_INDBLKSHIFT 12 /* 4k */ 63*22ce4affSfengbojiang /* 64*22ce4affSfengbojiang * If we ever increase this value beyond 20, we need to revisit all logic that 65*22ce4affSfengbojiang * does x << level * ebps to handle overflow. With a 1M indirect block size, 66*22ce4affSfengbojiang * 4 levels of indirect blocks would not be able to guarantee addressing an 67*22ce4affSfengbojiang * entire object, so 5 levels will be used, but 5 * (20 - 7) = 65. 68*22ce4affSfengbojiang */ 69*22ce4affSfengbojiang #define DN_MAX_INDBLKSHIFT 17 /* 128k */ 70*22ce4affSfengbojiang #define DNODE_BLOCK_SHIFT 14 /* 16k */ 71*22ce4affSfengbojiang #define DNODE_CORE_SIZE 64 /* 64 bytes for dnode sans blkptrs */ 72*22ce4affSfengbojiang #define DN_MAX_OBJECT_SHIFT 48 /* 256 trillion (zfs_fid_t limit) */ 73*22ce4affSfengbojiang #define DN_MAX_OFFSET_SHIFT 64 /* 2^64 bytes in a dnode */ 74*22ce4affSfengbojiang 75*22ce4affSfengbojiang /* 76*22ce4affSfengbojiang * dnode id flags 77*22ce4affSfengbojiang * 78*22ce4affSfengbojiang * Note: a file will never ever have its ids moved from bonus->spill 79*22ce4affSfengbojiang */ 80*22ce4affSfengbojiang #define DN_ID_CHKED_BONUS 0x1 81*22ce4affSfengbojiang #define DN_ID_CHKED_SPILL 0x2 82*22ce4affSfengbojiang #define DN_ID_OLD_EXIST 0x4 83*22ce4affSfengbojiang #define DN_ID_NEW_EXIST 0x8 84*22ce4affSfengbojiang 85*22ce4affSfengbojiang /* 86*22ce4affSfengbojiang * Derived constants. 87*22ce4affSfengbojiang */ 88*22ce4affSfengbojiang #define DNODE_MIN_SIZE (1 << DNODE_SHIFT) 89*22ce4affSfengbojiang #define DNODE_MAX_SIZE (1 << DNODE_BLOCK_SHIFT) 90*22ce4affSfengbojiang #define DNODE_BLOCK_SIZE (1 << DNODE_BLOCK_SHIFT) 91*22ce4affSfengbojiang #define DNODE_MIN_SLOTS (DNODE_MIN_SIZE >> DNODE_SHIFT) 92*22ce4affSfengbojiang #define DNODE_MAX_SLOTS (DNODE_MAX_SIZE >> DNODE_SHIFT) 93*22ce4affSfengbojiang #define DN_BONUS_SIZE(dnsize) ((dnsize) - DNODE_CORE_SIZE - \ 94*22ce4affSfengbojiang (1 << SPA_BLKPTRSHIFT)) 95*22ce4affSfengbojiang #define DN_SLOTS_TO_BONUSLEN(slots) DN_BONUS_SIZE((slots) << DNODE_SHIFT) 96*22ce4affSfengbojiang #define DN_OLD_MAX_BONUSLEN (DN_BONUS_SIZE(DNODE_MIN_SIZE)) 97*22ce4affSfengbojiang #define DN_MAX_NBLKPTR ((DNODE_MIN_SIZE - DNODE_CORE_SIZE) >> SPA_BLKPTRSHIFT) 98*22ce4affSfengbojiang #define DN_MAX_OBJECT (1ULL << DN_MAX_OBJECT_SHIFT) 99*22ce4affSfengbojiang #define DN_ZERO_BONUSLEN (DN_BONUS_SIZE(DNODE_MAX_SIZE) + 1) 100*22ce4affSfengbojiang #define DN_KILL_SPILLBLK (1) 101*22ce4affSfengbojiang 102*22ce4affSfengbojiang #define DN_SLOT_UNINIT ((void *)NULL) /* Uninitialized */ 103*22ce4affSfengbojiang #define DN_SLOT_FREE ((void *)1UL) /* Free slot */ 104*22ce4affSfengbojiang #define DN_SLOT_ALLOCATED ((void *)2UL) /* Allocated slot */ 105*22ce4affSfengbojiang #define DN_SLOT_INTERIOR ((void *)3UL) /* Interior allocated slot */ 106*22ce4affSfengbojiang #define DN_SLOT_IS_PTR(dn) ((void *)dn > DN_SLOT_INTERIOR) 107*22ce4affSfengbojiang #define DN_SLOT_IS_VALID(dn) ((void *)dn != NULL) 108*22ce4affSfengbojiang 109*22ce4affSfengbojiang #define DNODES_PER_BLOCK_SHIFT (DNODE_BLOCK_SHIFT - DNODE_SHIFT) 110*22ce4affSfengbojiang #define DNODES_PER_BLOCK (1ULL << DNODES_PER_BLOCK_SHIFT) 111*22ce4affSfengbojiang 112*22ce4affSfengbojiang /* 113*22ce4affSfengbojiang * This is inaccurate if the indblkshift of the particular object is not the 114*22ce4affSfengbojiang * max. But it's only used by userland to calculate the zvol reservation. 115*22ce4affSfengbojiang */ 116*22ce4affSfengbojiang #define DNODES_PER_LEVEL_SHIFT (DN_MAX_INDBLKSHIFT - SPA_BLKPTRSHIFT) 117*22ce4affSfengbojiang #define DNODES_PER_LEVEL (1ULL << DNODES_PER_LEVEL_SHIFT) 118*22ce4affSfengbojiang 119*22ce4affSfengbojiang #define DN_MAX_LEVELS (DIV_ROUND_UP(DN_MAX_OFFSET_SHIFT - SPA_MINBLOCKSHIFT, \ 120*22ce4affSfengbojiang DN_MIN_INDBLKSHIFT - SPA_BLKPTRSHIFT) + 1) 121*22ce4affSfengbojiang 122*22ce4affSfengbojiang #define DN_BONUS(dnp) ((void*)((dnp)->dn_bonus + \ 123*22ce4affSfengbojiang (((dnp)->dn_nblkptr - 1) * sizeof (blkptr_t)))) 124*22ce4affSfengbojiang #define DN_MAX_BONUS_LEN(dnp) \ 125*22ce4affSfengbojiang ((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) ? \ 126*22ce4affSfengbojiang (uint8_t *)DN_SPILL_BLKPTR(dnp) - (uint8_t *)DN_BONUS(dnp) : \ 127*22ce4affSfengbojiang (uint8_t *)(dnp + (dnp->dn_extra_slots + 1)) - (uint8_t *)DN_BONUS(dnp)) 128*22ce4affSfengbojiang 129*22ce4affSfengbojiang #define DN_USED_BYTES(dnp) (((dnp)->dn_flags & DNODE_FLAG_USED_BYTES) ? \ 130*22ce4affSfengbojiang (dnp)->dn_used : (dnp)->dn_used << SPA_MINBLOCKSHIFT) 131*22ce4affSfengbojiang 132*22ce4affSfengbojiang #define EPB(blkshift, typeshift) (1 << (blkshift - typeshift)) 133*22ce4affSfengbojiang 134*22ce4affSfengbojiang struct dmu_buf_impl; 135*22ce4affSfengbojiang struct objset; 136*22ce4affSfengbojiang struct zio; 137*22ce4affSfengbojiang 138*22ce4affSfengbojiang enum dnode_dirtycontext { 139*22ce4affSfengbojiang DN_UNDIRTIED, 140*22ce4affSfengbojiang DN_DIRTY_OPEN, 141*22ce4affSfengbojiang DN_DIRTY_SYNC 142*22ce4affSfengbojiang }; 143*22ce4affSfengbojiang 144*22ce4affSfengbojiang /* Is dn_used in bytes? if not, it's in multiples of SPA_MINBLOCKSIZE */ 145*22ce4affSfengbojiang #define DNODE_FLAG_USED_BYTES (1 << 0) 146*22ce4affSfengbojiang #define DNODE_FLAG_USERUSED_ACCOUNTED (1 << 1) 147*22ce4affSfengbojiang 148*22ce4affSfengbojiang /* Does dnode have a SA spill blkptr in bonus? */ 149*22ce4affSfengbojiang #define DNODE_FLAG_SPILL_BLKPTR (1 << 2) 150*22ce4affSfengbojiang 151*22ce4affSfengbojiang /* User/Group/Project dnode accounting */ 152*22ce4affSfengbojiang #define DNODE_FLAG_USEROBJUSED_ACCOUNTED (1 << 3) 153*22ce4affSfengbojiang 154*22ce4affSfengbojiang /* 155*22ce4affSfengbojiang * This mask defines the set of flags which are "portable", meaning 156*22ce4affSfengbojiang * that they can be preserved when doing a raw encrypted zfs send. 157*22ce4affSfengbojiang * Flags included in this mask will be protected by AAD when the block 158*22ce4affSfengbojiang * of dnodes is encrypted. 159*22ce4affSfengbojiang */ 160*22ce4affSfengbojiang #define DNODE_CRYPT_PORTABLE_FLAGS_MASK (DNODE_FLAG_SPILL_BLKPTR) 161*22ce4affSfengbojiang 162*22ce4affSfengbojiang /* 163*22ce4affSfengbojiang * VARIABLE-LENGTH (LARGE) DNODES 164*22ce4affSfengbojiang * 165*22ce4affSfengbojiang * The motivation for variable-length dnodes is to eliminate the overhead 166*22ce4affSfengbojiang * associated with using spill blocks. Spill blocks are used to store 167*22ce4affSfengbojiang * system attribute data (i.e. file metadata) that does not fit in the 168*22ce4affSfengbojiang * dnode's bonus buffer. By allowing a larger bonus buffer area the use of 169*22ce4affSfengbojiang * a spill block can be avoided. Spill blocks potentially incur an 170*22ce4affSfengbojiang * additional read I/O for every dnode in a dnode block. As a worst case 171*22ce4affSfengbojiang * example, reading 32 dnodes from a 16k dnode block and all of the spill 172*22ce4affSfengbojiang * blocks could issue 33 separate reads. Now suppose those dnodes have size 173*22ce4affSfengbojiang * 1024 and therefore don't need spill blocks. Then the worst case number 174*22ce4affSfengbojiang * of blocks read is reduced to from 33 to two--one per dnode block. 175*22ce4affSfengbojiang * 176*22ce4affSfengbojiang * ZFS-on-Linux systems that make heavy use of extended attributes benefit 177*22ce4affSfengbojiang * from this feature. In particular, ZFS-on-Linux supports the xattr=sa 178*22ce4affSfengbojiang * dataset property which allows file extended attribute data to be stored 179*22ce4affSfengbojiang * in the dnode bonus buffer as an alternative to the traditional 180*22ce4affSfengbojiang * directory-based format. Workloads such as SELinux and the Lustre 181*22ce4affSfengbojiang * distributed filesystem often store enough xattr data to force spill 182*22ce4affSfengbojiang * blocks when xattr=sa is in effect. Large dnodes may therefore provide a 183*22ce4affSfengbojiang * performance benefit to such systems. Other use cases that benefit from 184*22ce4affSfengbojiang * this feature include files with large ACLs and symbolic links with long 185*22ce4affSfengbojiang * target names. 186*22ce4affSfengbojiang * 187*22ce4affSfengbojiang * The size of a dnode may be a multiple of 512 bytes up to the size of a 188*22ce4affSfengbojiang * dnode block (currently 16384 bytes). The dn_extra_slots field of the 189*22ce4affSfengbojiang * on-disk dnode_phys_t structure describes the size of the physical dnode 190*22ce4affSfengbojiang * on disk. The field represents how many "extra" dnode_phys_t slots a 191*22ce4affSfengbojiang * dnode consumes in its dnode block. This convention results in a value of 192*22ce4affSfengbojiang * 0 for 512 byte dnodes which preserves on-disk format compatibility with 193*22ce4affSfengbojiang * older software which doesn't support large dnodes. 194*22ce4affSfengbojiang * 195*22ce4affSfengbojiang * Similarly, the in-memory dnode_t structure has a dn_num_slots field 196*22ce4affSfengbojiang * to represent the total number of dnode_phys_t slots consumed on disk. 197*22ce4affSfengbojiang * Thus dn->dn_num_slots is 1 greater than the corresponding 198*22ce4affSfengbojiang * dnp->dn_extra_slots. This difference in convention was adopted 199*22ce4affSfengbojiang * because, unlike on-disk structures, backward compatibility is not a 200*22ce4affSfengbojiang * concern for in-memory objects, so we used a more natural way to 201*22ce4affSfengbojiang * represent size for a dnode_t. 202*22ce4affSfengbojiang * 203*22ce4affSfengbojiang * The default size for newly created dnodes is determined by the value of 204*22ce4affSfengbojiang * the "dnodesize" dataset property. By default the property is set to 205*22ce4affSfengbojiang * "legacy" which is compatible with older software. Setting the property 206*22ce4affSfengbojiang * to "auto" will allow the filesystem to choose the most suitable dnode 207*22ce4affSfengbojiang * size. Currently this just sets the default dnode size to 1k, but future 208*22ce4affSfengbojiang * code improvements could dynamically choose a size based on observed 209*22ce4affSfengbojiang * workload patterns. Dnodes of varying sizes can coexist within the same 210*22ce4affSfengbojiang * dataset and even within the same dnode block. 211*22ce4affSfengbojiang */ 212*22ce4affSfengbojiang 213*22ce4affSfengbojiang typedef struct dnode_phys { 214*22ce4affSfengbojiang uint8_t dn_type; /* dmu_object_type_t */ 215*22ce4affSfengbojiang uint8_t dn_indblkshift; /* ln2(indirect block size) */ 216*22ce4affSfengbojiang uint8_t dn_nlevels; /* 1=dn_blkptr->data blocks */ 217*22ce4affSfengbojiang uint8_t dn_nblkptr; /* length of dn_blkptr */ 218*22ce4affSfengbojiang uint8_t dn_bonustype; /* type of data in bonus buffer */ 219*22ce4affSfengbojiang uint8_t dn_checksum; /* ZIO_CHECKSUM type */ 220*22ce4affSfengbojiang uint8_t dn_compress; /* ZIO_COMPRESS type */ 221*22ce4affSfengbojiang uint8_t dn_flags; /* DNODE_FLAG_* */ 222*22ce4affSfengbojiang uint16_t dn_datablkszsec; /* data block size in 512b sectors */ 223*22ce4affSfengbojiang uint16_t dn_bonuslen; /* length of dn_bonus */ 224*22ce4affSfengbojiang uint8_t dn_extra_slots; /* # of subsequent slots consumed */ 225*22ce4affSfengbojiang uint8_t dn_pad2[3]; 226*22ce4affSfengbojiang 227*22ce4affSfengbojiang /* accounting is protected by dn_dirty_mtx */ 228*22ce4affSfengbojiang uint64_t dn_maxblkid; /* largest allocated block ID */ 229*22ce4affSfengbojiang uint64_t dn_used; /* bytes (or sectors) of disk space */ 230*22ce4affSfengbojiang 231*22ce4affSfengbojiang /* 232*22ce4affSfengbojiang * Both dn_pad2 and dn_pad3 are protected by the block's MAC. This 233*22ce4affSfengbojiang * allows us to protect any fields that might be added here in the 234*22ce4affSfengbojiang * future. In either case, developers will want to check 235*22ce4affSfengbojiang * zio_crypt_init_uios_dnode() to ensure the new field is being 236*22ce4affSfengbojiang * protected properly. 237*22ce4affSfengbojiang */ 238*22ce4affSfengbojiang uint64_t dn_pad3[4]; 239*22ce4affSfengbojiang 240*22ce4affSfengbojiang /* 241*22ce4affSfengbojiang * The tail region is 448 bytes for a 512 byte dnode, and 242*22ce4affSfengbojiang * correspondingly larger for larger dnode sizes. The spill 243*22ce4affSfengbojiang * block pointer, when present, is always at the end of the tail 244*22ce4affSfengbojiang * region. There are three ways this space may be used, using 245*22ce4affSfengbojiang * a 512 byte dnode for this diagram: 246*22ce4affSfengbojiang * 247*22ce4affSfengbojiang * 0 64 128 192 256 320 384 448 (offset) 248*22ce4affSfengbojiang * +---------------+---------------+---------------+-------+ 249*22ce4affSfengbojiang * | dn_blkptr[0] | dn_blkptr[1] | dn_blkptr[2] | / | 250*22ce4affSfengbojiang * +---------------+---------------+---------------+-------+ 251*22ce4affSfengbojiang * | dn_blkptr[0] | dn_bonus[0..319] | 252*22ce4affSfengbojiang * +---------------+-----------------------+---------------+ 253*22ce4affSfengbojiang * | dn_blkptr[0] | dn_bonus[0..191] | dn_spill | 254*22ce4affSfengbojiang * +---------------+-----------------------+---------------+ 255*22ce4affSfengbojiang */ 256*22ce4affSfengbojiang union { 257*22ce4affSfengbojiang blkptr_t dn_blkptr[1+DN_OLD_MAX_BONUSLEN/sizeof (blkptr_t)]; 258*22ce4affSfengbojiang struct { 259*22ce4affSfengbojiang blkptr_t __dn_ignore1; 260*22ce4affSfengbojiang uint8_t dn_bonus[DN_OLD_MAX_BONUSLEN]; 261*22ce4affSfengbojiang }; 262*22ce4affSfengbojiang struct { 263*22ce4affSfengbojiang blkptr_t __dn_ignore2; 264*22ce4affSfengbojiang uint8_t __dn_ignore3[DN_OLD_MAX_BONUSLEN - 265*22ce4affSfengbojiang sizeof (blkptr_t)]; 266*22ce4affSfengbojiang blkptr_t dn_spill; 267*22ce4affSfengbojiang }; 268*22ce4affSfengbojiang }; 269*22ce4affSfengbojiang } dnode_phys_t; 270*22ce4affSfengbojiang 271*22ce4affSfengbojiang #define DN_SPILL_BLKPTR(dnp) ((blkptr_t *)((char *)(dnp) + \ 272*22ce4affSfengbojiang (((dnp)->dn_extra_slots + 1) << DNODE_SHIFT) - (1 << SPA_BLKPTRSHIFT))) 273*22ce4affSfengbojiang 274*22ce4affSfengbojiang struct dnode { 275*22ce4affSfengbojiang /* 276*22ce4affSfengbojiang * Protects the structure of the dnode, including the number of levels 277*22ce4affSfengbojiang * of indirection (dn_nlevels), dn_maxblkid, and dn_next_* 278*22ce4affSfengbojiang */ 279*22ce4affSfengbojiang krwlock_t dn_struct_rwlock; 280*22ce4affSfengbojiang 281*22ce4affSfengbojiang /* Our link on dn_objset->os_dnodes list; protected by os_lock. */ 282*22ce4affSfengbojiang list_node_t dn_link; 283*22ce4affSfengbojiang 284*22ce4affSfengbojiang /* immutable: */ 285*22ce4affSfengbojiang struct objset *dn_objset; 286*22ce4affSfengbojiang uint64_t dn_object; 287*22ce4affSfengbojiang struct dmu_buf_impl *dn_dbuf; 288*22ce4affSfengbojiang struct dnode_handle *dn_handle; 289*22ce4affSfengbojiang dnode_phys_t *dn_phys; /* pointer into dn->dn_dbuf->db.db_data */ 290*22ce4affSfengbojiang 291*22ce4affSfengbojiang /* 292*22ce4affSfengbojiang * Copies of stuff in dn_phys. They're valid in the open 293*22ce4affSfengbojiang * context (eg. even before the dnode is first synced). 294*22ce4affSfengbojiang * Where necessary, these are protected by dn_struct_rwlock. 295*22ce4affSfengbojiang */ 296*22ce4affSfengbojiang dmu_object_type_t dn_type; /* object type */ 297*22ce4affSfengbojiang uint16_t dn_bonuslen; /* bonus length */ 298*22ce4affSfengbojiang uint8_t dn_bonustype; /* bonus type */ 299*22ce4affSfengbojiang uint8_t dn_nblkptr; /* number of blkptrs (immutable) */ 300*22ce4affSfengbojiang uint8_t dn_checksum; /* ZIO_CHECKSUM type */ 301*22ce4affSfengbojiang uint8_t dn_compress; /* ZIO_COMPRESS type */ 302*22ce4affSfengbojiang uint8_t dn_nlevels; 303*22ce4affSfengbojiang uint8_t dn_indblkshift; 304*22ce4affSfengbojiang uint8_t dn_datablkshift; /* zero if blksz not power of 2! */ 305*22ce4affSfengbojiang uint8_t dn_moved; /* Has this dnode been moved? */ 306*22ce4affSfengbojiang uint16_t dn_datablkszsec; /* in 512b sectors */ 307*22ce4affSfengbojiang uint32_t dn_datablksz; /* in bytes */ 308*22ce4affSfengbojiang uint64_t dn_maxblkid; 309*22ce4affSfengbojiang uint8_t dn_next_type[TXG_SIZE]; 310*22ce4affSfengbojiang uint8_t dn_num_slots; /* metadnode slots consumed on disk */ 311*22ce4affSfengbojiang uint8_t dn_next_nblkptr[TXG_SIZE]; 312*22ce4affSfengbojiang uint8_t dn_next_nlevels[TXG_SIZE]; 313*22ce4affSfengbojiang uint8_t dn_next_indblkshift[TXG_SIZE]; 314*22ce4affSfengbojiang uint8_t dn_next_bonustype[TXG_SIZE]; 315*22ce4affSfengbojiang uint8_t dn_rm_spillblk[TXG_SIZE]; /* for removing spill blk */ 316*22ce4affSfengbojiang uint16_t dn_next_bonuslen[TXG_SIZE]; 317*22ce4affSfengbojiang uint32_t dn_next_blksz[TXG_SIZE]; /* next block size in bytes */ 318*22ce4affSfengbojiang uint64_t dn_next_maxblkid[TXG_SIZE]; /* next maxblkid in bytes */ 319*22ce4affSfengbojiang 320*22ce4affSfengbojiang /* protected by dn_dbufs_mtx; declared here to fill 32-bit hole */ 321*22ce4affSfengbojiang uint32_t dn_dbufs_count; /* count of dn_dbufs */ 322*22ce4affSfengbojiang 323*22ce4affSfengbojiang /* protected by os_lock: */ 324*22ce4affSfengbojiang multilist_node_t dn_dirty_link[TXG_SIZE]; /* next on dataset's dirty */ 325*22ce4affSfengbojiang 326*22ce4affSfengbojiang /* protected by dn_mtx: */ 327*22ce4affSfengbojiang kmutex_t dn_mtx; 328*22ce4affSfengbojiang list_t dn_dirty_records[TXG_SIZE]; 329*22ce4affSfengbojiang struct range_tree *dn_free_ranges[TXG_SIZE]; 330*22ce4affSfengbojiang uint64_t dn_allocated_txg; 331*22ce4affSfengbojiang uint64_t dn_free_txg; 332*22ce4affSfengbojiang uint64_t dn_assigned_txg; 333*22ce4affSfengbojiang uint64_t dn_dirty_txg; /* txg dnode was last dirtied */ 334*22ce4affSfengbojiang kcondvar_t dn_notxholds; 335*22ce4affSfengbojiang kcondvar_t dn_nodnholds; 336*22ce4affSfengbojiang enum dnode_dirtycontext dn_dirtyctx; 337*22ce4affSfengbojiang void *dn_dirtyctx_firstset; /* dbg: contents meaningless */ 338*22ce4affSfengbojiang 339*22ce4affSfengbojiang /* protected by own devices */ 340*22ce4affSfengbojiang zfs_refcount_t dn_tx_holds; 341*22ce4affSfengbojiang zfs_refcount_t dn_holds; 342*22ce4affSfengbojiang 343*22ce4affSfengbojiang kmutex_t dn_dbufs_mtx; 344*22ce4affSfengbojiang /* 345*22ce4affSfengbojiang * Descendent dbufs, ordered by dbuf_compare. Note that dn_dbufs 346*22ce4affSfengbojiang * can contain multiple dbufs of the same (level, blkid) when a 347*22ce4affSfengbojiang * dbuf is marked DB_EVICTING without being removed from 348*22ce4affSfengbojiang * dn_dbufs. To maintain the avl invariant that there cannot be 349*22ce4affSfengbojiang * duplicate entries, we order the dbufs by an arbitrary value - 350*22ce4affSfengbojiang * their address in memory. This means that dn_dbufs cannot be used to 351*22ce4affSfengbojiang * directly look up a dbuf. Instead, callers must use avl_walk, have 352*22ce4affSfengbojiang * a reference to the dbuf, or look up a non-existent node with 353*22ce4affSfengbojiang * db_state = DB_SEARCH (see dbuf_free_range for an example). 354*22ce4affSfengbojiang */ 355*22ce4affSfengbojiang avl_tree_t dn_dbufs; 356*22ce4affSfengbojiang 357*22ce4affSfengbojiang /* protected by dn_struct_rwlock */ 358*22ce4affSfengbojiang struct dmu_buf_impl *dn_bonus; /* bonus buffer dbuf */ 359*22ce4affSfengbojiang 360*22ce4affSfengbojiang boolean_t dn_have_spill; /* have spill or are spilling */ 361*22ce4affSfengbojiang 362*22ce4affSfengbojiang /* parent IO for current sync write */ 363*22ce4affSfengbojiang zio_t *dn_zio; 364*22ce4affSfengbojiang 365*22ce4affSfengbojiang /* used in syncing context */ 366*22ce4affSfengbojiang uint64_t dn_oldused; /* old phys used bytes */ 367*22ce4affSfengbojiang uint64_t dn_oldflags; /* old phys dn_flags */ 368*22ce4affSfengbojiang uint64_t dn_olduid, dn_oldgid, dn_oldprojid; 369*22ce4affSfengbojiang uint64_t dn_newuid, dn_newgid, dn_newprojid; 370*22ce4affSfengbojiang int dn_id_flags; 371*22ce4affSfengbojiang 372*22ce4affSfengbojiang /* holds prefetch structure */ 373*22ce4affSfengbojiang struct zfetch dn_zfetch; 374*22ce4affSfengbojiang }; 375*22ce4affSfengbojiang 376*22ce4affSfengbojiang /* 377*22ce4affSfengbojiang * Since AVL already has embedded element counter, use dn_dbufs_count 378*22ce4affSfengbojiang * only for dbufs not counted there (bonus buffers) and just add them. 379*22ce4affSfengbojiang */ 380*22ce4affSfengbojiang #define DN_DBUFS_COUNT(dn) ((dn)->dn_dbufs_count + \ 381*22ce4affSfengbojiang avl_numnodes(&(dn)->dn_dbufs)) 382*22ce4affSfengbojiang 383*22ce4affSfengbojiang /* 384*22ce4affSfengbojiang * We use this (otherwise unused) bit to indicate if the value of 385*22ce4affSfengbojiang * dn_next_maxblkid[txgoff] is valid to use in dnode_sync(). 386*22ce4affSfengbojiang */ 387*22ce4affSfengbojiang #define DMU_NEXT_MAXBLKID_SET (1ULL << 63) 388*22ce4affSfengbojiang 389*22ce4affSfengbojiang /* 390*22ce4affSfengbojiang * Adds a level of indirection between the dbuf and the dnode to avoid 391*22ce4affSfengbojiang * iterating descendent dbufs in dnode_move(). Handles are not allocated 392*22ce4affSfengbojiang * individually, but as an array of child dnodes in dnode_hold_impl(). 393*22ce4affSfengbojiang */ 394*22ce4affSfengbojiang typedef struct dnode_handle { 395*22ce4affSfengbojiang /* Protects dnh_dnode from modification by dnode_move(). */ 396*22ce4affSfengbojiang zrlock_t dnh_zrlock; 397*22ce4affSfengbojiang dnode_t *dnh_dnode; 398*22ce4affSfengbojiang } dnode_handle_t; 399*22ce4affSfengbojiang 400*22ce4affSfengbojiang typedef struct dnode_children { 401*22ce4affSfengbojiang dmu_buf_user_t dnc_dbu; /* User evict data */ 402*22ce4affSfengbojiang size_t dnc_count; /* number of children */ 403*22ce4affSfengbojiang dnode_handle_t dnc_children[]; /* sized dynamically */ 404*22ce4affSfengbojiang } dnode_children_t; 405*22ce4affSfengbojiang 406*22ce4affSfengbojiang typedef struct free_range { 407*22ce4affSfengbojiang avl_node_t fr_node; 408*22ce4affSfengbojiang uint64_t fr_blkid; 409*22ce4affSfengbojiang uint64_t fr_nblks; 410*22ce4affSfengbojiang } free_range_t; 411*22ce4affSfengbojiang 412*22ce4affSfengbojiang void dnode_special_open(struct objset *dd, dnode_phys_t *dnp, 413*22ce4affSfengbojiang uint64_t object, dnode_handle_t *dnh); 414*22ce4affSfengbojiang void dnode_special_close(dnode_handle_t *dnh); 415*22ce4affSfengbojiang 416*22ce4affSfengbojiang void dnode_setbonuslen(dnode_t *dn, int newsize, dmu_tx_t *tx); 417*22ce4affSfengbojiang void dnode_setbonus_type(dnode_t *dn, dmu_object_type_t, dmu_tx_t *tx); 418*22ce4affSfengbojiang void dnode_rm_spill(dnode_t *dn, dmu_tx_t *tx); 419*22ce4affSfengbojiang 420*22ce4affSfengbojiang int dnode_hold(struct objset *dd, uint64_t object, 421*22ce4affSfengbojiang void *ref, dnode_t **dnp); 422*22ce4affSfengbojiang int dnode_hold_impl(struct objset *dd, uint64_t object, int flag, int dn_slots, 423*22ce4affSfengbojiang void *ref, dnode_t **dnp); 424*22ce4affSfengbojiang boolean_t dnode_add_ref(dnode_t *dn, void *ref); 425*22ce4affSfengbojiang void dnode_rele(dnode_t *dn, void *ref); 426*22ce4affSfengbojiang void dnode_rele_and_unlock(dnode_t *dn, void *tag, boolean_t evicting); 427*22ce4affSfengbojiang int dnode_try_claim(objset_t *os, uint64_t object, int slots); 428*22ce4affSfengbojiang void dnode_setdirty(dnode_t *dn, dmu_tx_t *tx); 429*22ce4affSfengbojiang void dnode_set_dirtyctx(dnode_t *dn, dmu_tx_t *tx, void *tag); 430*22ce4affSfengbojiang void dnode_sync(dnode_t *dn, dmu_tx_t *tx); 431*22ce4affSfengbojiang void dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs, 432*22ce4affSfengbojiang dmu_object_type_t bonustype, int bonuslen, int dn_slots, dmu_tx_t *tx); 433*22ce4affSfengbojiang void dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, 434*22ce4affSfengbojiang dmu_object_type_t bonustype, int bonuslen, int dn_slots, 435*22ce4affSfengbojiang boolean_t keep_spill, dmu_tx_t *tx); 436*22ce4affSfengbojiang void dnode_free(dnode_t *dn, dmu_tx_t *tx); 437*22ce4affSfengbojiang void dnode_byteswap(dnode_phys_t *dnp); 438*22ce4affSfengbojiang void dnode_buf_byteswap(void *buf, size_t size); 439*22ce4affSfengbojiang void dnode_verify(dnode_t *dn); 440*22ce4affSfengbojiang int dnode_set_nlevels(dnode_t *dn, int nlevels, dmu_tx_t *tx); 441*22ce4affSfengbojiang int dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx); 442*22ce4affSfengbojiang void dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx); 443*22ce4affSfengbojiang void dnode_diduse_space(dnode_t *dn, int64_t space); 444*22ce4affSfengbojiang void dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx, 445*22ce4affSfengbojiang boolean_t have_read, boolean_t force); 446*22ce4affSfengbojiang uint64_t dnode_block_freed(dnode_t *dn, uint64_t blkid); 447*22ce4affSfengbojiang void dnode_init(void); 448*22ce4affSfengbojiang void dnode_fini(void); 449*22ce4affSfengbojiang int dnode_next_offset(dnode_t *dn, int flags, uint64_t *off, 450*22ce4affSfengbojiang int minlvl, uint64_t blkfill, uint64_t txg); 451*22ce4affSfengbojiang void dnode_evict_dbufs(dnode_t *dn); 452*22ce4affSfengbojiang void dnode_evict_bonus(dnode_t *dn); 453*22ce4affSfengbojiang void dnode_free_interior_slots(dnode_t *dn); 454*22ce4affSfengbojiang 455*22ce4affSfengbojiang #define DNODE_IS_DIRTY(_dn) \ 456*22ce4affSfengbojiang ((_dn)->dn_dirty_txg >= spa_syncing_txg((_dn)->dn_objset->os_spa)) 457*22ce4affSfengbojiang 458*22ce4affSfengbojiang #define DNODE_IS_CACHEABLE(_dn) \ 459*22ce4affSfengbojiang ((_dn)->dn_objset->os_primary_cache == ZFS_CACHE_ALL || \ 460*22ce4affSfengbojiang (DMU_OT_IS_METADATA((_dn)->dn_type) && \ 461*22ce4affSfengbojiang (_dn)->dn_objset->os_primary_cache == ZFS_CACHE_METADATA)) 462*22ce4affSfengbojiang 463*22ce4affSfengbojiang #define DNODE_META_IS_CACHEABLE(_dn) \ 464*22ce4affSfengbojiang ((_dn)->dn_objset->os_primary_cache == ZFS_CACHE_ALL || \ 465*22ce4affSfengbojiang (_dn)->dn_objset->os_primary_cache == ZFS_CACHE_METADATA) 466*22ce4affSfengbojiang 467*22ce4affSfengbojiang /* 468*22ce4affSfengbojiang * Used for dnodestats kstat. 469*22ce4affSfengbojiang */ 470*22ce4affSfengbojiang typedef struct dnode_stats { 471*22ce4affSfengbojiang /* 472*22ce4affSfengbojiang * Number of failed attempts to hold a meta dnode dbuf. 473*22ce4affSfengbojiang */ 474*22ce4affSfengbojiang kstat_named_t dnode_hold_dbuf_hold; 475*22ce4affSfengbojiang /* 476*22ce4affSfengbojiang * Number of failed attempts to read a meta dnode dbuf. 477*22ce4affSfengbojiang */ 478*22ce4affSfengbojiang kstat_named_t dnode_hold_dbuf_read; 479*22ce4affSfengbojiang /* 480*22ce4affSfengbojiang * Number of times dnode_hold(..., DNODE_MUST_BE_ALLOCATED) was able 481*22ce4affSfengbojiang * to hold the requested object number which was allocated. This is 482*22ce4affSfengbojiang * the common case when looking up any allocated object number. 483*22ce4affSfengbojiang */ 484*22ce4affSfengbojiang kstat_named_t dnode_hold_alloc_hits; 485*22ce4affSfengbojiang /* 486*22ce4affSfengbojiang * Number of times dnode_hold(..., DNODE_MUST_BE_ALLOCATED) was not 487*22ce4affSfengbojiang * able to hold the request object number because it was not allocated. 488*22ce4affSfengbojiang */ 489*22ce4affSfengbojiang kstat_named_t dnode_hold_alloc_misses; 490*22ce4affSfengbojiang /* 491*22ce4affSfengbojiang * Number of times dnode_hold(..., DNODE_MUST_BE_ALLOCATED) was not 492*22ce4affSfengbojiang * able to hold the request object number because the object number 493*22ce4affSfengbojiang * refers to an interior large dnode slot. 494*22ce4affSfengbojiang */ 495*22ce4affSfengbojiang kstat_named_t dnode_hold_alloc_interior; 496*22ce4affSfengbojiang /* 497*22ce4affSfengbojiang * Number of times dnode_hold(..., DNODE_MUST_BE_ALLOCATED) needed 498*22ce4affSfengbojiang * to retry acquiring slot zrl locks due to contention. 499*22ce4affSfengbojiang */ 500*22ce4affSfengbojiang kstat_named_t dnode_hold_alloc_lock_retry; 501*22ce4affSfengbojiang /* 502*22ce4affSfengbojiang * Number of times dnode_hold(..., DNODE_MUST_BE_ALLOCATED) did not 503*22ce4affSfengbojiang * need to create the dnode because another thread did so after 504*22ce4affSfengbojiang * dropping the read lock but before acquiring the write lock. 505*22ce4affSfengbojiang */ 506*22ce4affSfengbojiang kstat_named_t dnode_hold_alloc_lock_misses; 507*22ce4affSfengbojiang /* 508*22ce4affSfengbojiang * Number of times dnode_hold(..., DNODE_MUST_BE_ALLOCATED) found 509*22ce4affSfengbojiang * a free dnode instantiated by dnode_create() but not yet allocated 510*22ce4affSfengbojiang * by dnode_allocate(). 511*22ce4affSfengbojiang */ 512*22ce4affSfengbojiang kstat_named_t dnode_hold_alloc_type_none; 513*22ce4affSfengbojiang /* 514*22ce4affSfengbojiang * Number of times dnode_hold(..., DNODE_MUST_BE_FREE) was able 515*22ce4affSfengbojiang * to hold the requested range of free dnode slots. 516*22ce4affSfengbojiang */ 517*22ce4affSfengbojiang kstat_named_t dnode_hold_free_hits; 518*22ce4affSfengbojiang /* 519*22ce4affSfengbojiang * Number of times dnode_hold(..., DNODE_MUST_BE_FREE) was not 520*22ce4affSfengbojiang * able to hold the requested range of free dnode slots because 521*22ce4affSfengbojiang * at least one slot was allocated. 522*22ce4affSfengbojiang */ 523*22ce4affSfengbojiang kstat_named_t dnode_hold_free_misses; 524*22ce4affSfengbojiang /* 525*22ce4affSfengbojiang * Number of times dnode_hold(..., DNODE_MUST_BE_FREE) was not 526*22ce4affSfengbojiang * able to hold the requested range of free dnode slots because 527*22ce4affSfengbojiang * after acquiring the zrl lock at least one slot was allocated. 528*22ce4affSfengbojiang */ 529*22ce4affSfengbojiang kstat_named_t dnode_hold_free_lock_misses; 530*22ce4affSfengbojiang /* 531*22ce4affSfengbojiang * Number of times dnode_hold(..., DNODE_MUST_BE_FREE) needed 532*22ce4affSfengbojiang * to retry acquiring slot zrl locks due to contention. 533*22ce4affSfengbojiang */ 534*22ce4affSfengbojiang kstat_named_t dnode_hold_free_lock_retry; 535*22ce4affSfengbojiang /* 536*22ce4affSfengbojiang * Number of times dnode_hold(..., DNODE_MUST_BE_FREE) requested 537*22ce4affSfengbojiang * a range of dnode slots which were held by another thread. 538*22ce4affSfengbojiang */ 539*22ce4affSfengbojiang kstat_named_t dnode_hold_free_refcount; 540*22ce4affSfengbojiang /* 541*22ce4affSfengbojiang * Number of times dnode_hold(..., DNODE_MUST_BE_FREE) requested 542*22ce4affSfengbojiang * a range of dnode slots which would overflow the dnode_phys_t. 543*22ce4affSfengbojiang */ 544*22ce4affSfengbojiang kstat_named_t dnode_hold_free_overflow; 545*22ce4affSfengbojiang /* 546*22ce4affSfengbojiang * Number of times dnode_free_interior_slots() needed to retry 547*22ce4affSfengbojiang * acquiring a slot zrl lock due to contention. 548*22ce4affSfengbojiang */ 549*22ce4affSfengbojiang kstat_named_t dnode_free_interior_lock_retry; 550*22ce4affSfengbojiang /* 551*22ce4affSfengbojiang * Number of new dnodes allocated by dnode_allocate(). 552*22ce4affSfengbojiang */ 553*22ce4affSfengbojiang kstat_named_t dnode_allocate; 554*22ce4affSfengbojiang /* 555*22ce4affSfengbojiang * Number of dnodes re-allocated by dnode_reallocate(). 556*22ce4affSfengbojiang */ 557*22ce4affSfengbojiang kstat_named_t dnode_reallocate; 558*22ce4affSfengbojiang /* 559*22ce4affSfengbojiang * Number of meta dnode dbufs evicted. 560*22ce4affSfengbojiang */ 561*22ce4affSfengbojiang kstat_named_t dnode_buf_evict; 562*22ce4affSfengbojiang /* 563*22ce4affSfengbojiang * Number of times dmu_object_alloc*() reached the end of the existing 564*22ce4affSfengbojiang * object ID chunk and advanced to a new one. 565*22ce4affSfengbojiang */ 566*22ce4affSfengbojiang kstat_named_t dnode_alloc_next_chunk; 567*22ce4affSfengbojiang /* 568*22ce4affSfengbojiang * Number of times multiple threads attempted to allocate a dnode 569*22ce4affSfengbojiang * from the same block of free dnodes. 570*22ce4affSfengbojiang */ 571*22ce4affSfengbojiang kstat_named_t dnode_alloc_race; 572*22ce4affSfengbojiang /* 573*22ce4affSfengbojiang * Number of times dmu_object_alloc*() was forced to advance to the 574*22ce4affSfengbojiang * next meta dnode dbuf due to an error from dmu_object_next(). 575*22ce4affSfengbojiang */ 576*22ce4affSfengbojiang kstat_named_t dnode_alloc_next_block; 577*22ce4affSfengbojiang /* 578*22ce4affSfengbojiang * Statistics for tracking dnodes which have been moved. 579*22ce4affSfengbojiang */ 580*22ce4affSfengbojiang kstat_named_t dnode_move_invalid; 581*22ce4affSfengbojiang kstat_named_t dnode_move_recheck1; 582*22ce4affSfengbojiang kstat_named_t dnode_move_recheck2; 583*22ce4affSfengbojiang kstat_named_t dnode_move_special; 584*22ce4affSfengbojiang kstat_named_t dnode_move_handle; 585*22ce4affSfengbojiang kstat_named_t dnode_move_rwlock; 586*22ce4affSfengbojiang kstat_named_t dnode_move_active; 587*22ce4affSfengbojiang } dnode_stats_t; 588*22ce4affSfengbojiang 589*22ce4affSfengbojiang extern dnode_stats_t dnode_stats; 590*22ce4affSfengbojiang 591*22ce4affSfengbojiang #define DNODE_STAT_INCR(stat, val) \ 592*22ce4affSfengbojiang atomic_add_64(&dnode_stats.stat.value.ui64, (val)); 593*22ce4affSfengbojiang #define DNODE_STAT_BUMP(stat) \ 594*22ce4affSfengbojiang DNODE_STAT_INCR(stat, 1); 595*22ce4affSfengbojiang 596*22ce4affSfengbojiang #ifdef ZFS_DEBUG 597*22ce4affSfengbojiang 598*22ce4affSfengbojiang #define dprintf_dnode(dn, fmt, ...) do { \ 599*22ce4affSfengbojiang if (zfs_flags & ZFS_DEBUG_DPRINTF) { \ 600*22ce4affSfengbojiang char __db_buf[32]; \ 601*22ce4affSfengbojiang uint64_t __db_obj = (dn)->dn_object; \ 602*22ce4affSfengbojiang if (__db_obj == DMU_META_DNODE_OBJECT) \ 603*22ce4affSfengbojiang (void) strcpy(__db_buf, "mdn"); \ 604*22ce4affSfengbojiang else \ 605*22ce4affSfengbojiang (void) snprintf(__db_buf, sizeof (__db_buf), "%lld", \ 606*22ce4affSfengbojiang (u_longlong_t)__db_obj);\ 607*22ce4affSfengbojiang dprintf_ds((dn)->dn_objset->os_dsl_dataset, "obj=%s " fmt, \ 608*22ce4affSfengbojiang __db_buf, __VA_ARGS__); \ 609*22ce4affSfengbojiang } \ 610*22ce4affSfengbojiang _NOTE(CONSTCOND) } while (0) 611*22ce4affSfengbojiang 612*22ce4affSfengbojiang #define DNODE_VERIFY(dn) dnode_verify(dn) 613*22ce4affSfengbojiang #define FREE_VERIFY(db, start, end, tx) free_verify(db, start, end, tx) 614*22ce4affSfengbojiang 615*22ce4affSfengbojiang #else 616*22ce4affSfengbojiang 617*22ce4affSfengbojiang #define dprintf_dnode(db, fmt, ...) 618*22ce4affSfengbojiang #define DNODE_VERIFY(dn) 619*22ce4affSfengbojiang #define FREE_VERIFY(db, start, end, tx) 620*22ce4affSfengbojiang 621*22ce4affSfengbojiang #endif 622*22ce4affSfengbojiang 623*22ce4affSfengbojiang #ifdef __cplusplus 624*22ce4affSfengbojiang } 625*22ce4affSfengbojiang #endif 626*22ce4affSfengbojiang 627*22ce4affSfengbojiang #endif /* _SYS_DNODE_H */ 628