1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5fa9e4066Sahrens * Common Development and Distribution License, Version 1.0 only 6fa9e4066Sahrens * (the "License"). You may not use this file except in compliance 7fa9e4066Sahrens * with the License. 8fa9e4066Sahrens * 9fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 11fa9e4066Sahrens * See the License for the specific language governing permissions 12fa9e4066Sahrens * and limitations under the License. 13fa9e4066Sahrens * 14fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 15fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 17fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 18fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 19fa9e4066Sahrens * 20fa9e4066Sahrens * CDDL HEADER END 21fa9e4066Sahrens */ 22fa9e4066Sahrens /* 23fa9e4066Sahrens * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24fa9e4066Sahrens * Use is subject to license terms. 25fa9e4066Sahrens */ 26fa9e4066Sahrens 27fa9e4066Sahrens #ifndef _SYS_DNODE_H 28fa9e4066Sahrens #define _SYS_DNODE_H 29fa9e4066Sahrens 30fa9e4066Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 31fa9e4066Sahrens 32fa9e4066Sahrens #include <sys/zfs_context.h> 33fa9e4066Sahrens #include <sys/avl.h> 34fa9e4066Sahrens #include <sys/spa.h> 35fa9e4066Sahrens #include <sys/txg.h> 36fa9e4066Sahrens #include <sys/refcount.h> 37fa9e4066Sahrens #include <sys/dmu_zfetch.h> 38fa9e4066Sahrens 39fa9e4066Sahrens #ifdef __cplusplus 40fa9e4066Sahrens extern "C" { 41fa9e4066Sahrens #endif 42fa9e4066Sahrens 43fa9e4066Sahrens /* 44fa9e4066Sahrens * Flags. 45fa9e4066Sahrens */ 46fa9e4066Sahrens #define DNODE_MUST_BE_ALLOCATED 1 47fa9e4066Sahrens #define DNODE_MUST_BE_FREE 2 48fa9e4066Sahrens 49fa9e4066Sahrens /* 50fa9e4066Sahrens * Fixed constants. 51fa9e4066Sahrens */ 52fa9e4066Sahrens #define DNODE_SHIFT 9 /* 512 bytes */ 53fa9e4066Sahrens #define DN_MIN_INDBLKSHIFT 10 /* 1k */ 54fa9e4066Sahrens #define DN_MAX_INDBLKSHIFT 14 /* 16k */ 55fa9e4066Sahrens #define DNODE_BLOCK_SHIFT 14 /* 16k */ 56fa9e4066Sahrens #define DNODE_CORE_SIZE 64 /* 64 bytes for dnode sans blkptrs */ 57fa9e4066Sahrens #define DN_MAX_OBJECT_SHIFT 48 /* 256 trillion (zfs_fid_t limit) */ 58fa9e4066Sahrens #define DN_MAX_OFFSET_SHIFT 64 /* 2^64 bytes in a dnode */ 59fa9e4066Sahrens 60fa9e4066Sahrens /* 61fa9e4066Sahrens * Derived constants. 62fa9e4066Sahrens */ 63fa9e4066Sahrens #define DNODE_SIZE (1 << DNODE_SHIFT) 64fa9e4066Sahrens #define DN_MAX_NBLKPTR ((DNODE_SIZE - DNODE_CORE_SIZE) >> SPA_BLKPTRSHIFT) 65fa9e4066Sahrens #define DN_MAX_BONUSLEN (DNODE_SIZE - DNODE_CORE_SIZE - (1 << SPA_BLKPTRSHIFT)) 66fa9e4066Sahrens 67fa9e4066Sahrens #define DNODES_PER_BLOCK_SHIFT (DNODE_BLOCK_SHIFT - DNODE_SHIFT) 68fa9e4066Sahrens #define DNODES_PER_BLOCK (1ULL << DNODES_PER_BLOCK_SHIFT) 69fa9e4066Sahrens #define DNODES_PER_LEVEL_SHIFT (DN_MAX_INDBLKSHIFT - SPA_BLKPTRSHIFT) 70fa9e4066Sahrens 71fa9e4066Sahrens #define DN_META_DNODE_LEVELS \ 72fa9e4066Sahrens (1 + (DN_MAX_OBJECT_SHIFT - DNODE_SHIFT + SPA_BLKPTRSHIFT - \ 73fa9e4066Sahrens DNODES_PER_BLOCK_SHIFT) / DNODES_PER_LEVEL_SHIFT) 74fa9e4066Sahrens 75fa9e4066Sahrens /* The +2 here is a cheesy way to round up */ 76fa9e4066Sahrens #define DN_MAX_LEVELS (2 + ((DN_MAX_OFFSET_SHIFT - SPA_MINBLOCKSHIFT) / \ 77fa9e4066Sahrens (DN_MIN_INDBLKSHIFT - SPA_BLKPTRSHIFT))) 78fa9e4066Sahrens 79fa9e4066Sahrens #define DN_MAX_OBJECT \ 80fa9e4066Sahrens ((uint64_t)DN_MAX_NBLKPTR << (DNODES_PER_BLOCK_SHIFT + \ 81fa9e4066Sahrens (DN_META_DNODE_LEVELS - 1) * DNODES_PER_LEVEL_SHIFT)) 82fa9e4066Sahrens 83fa9e4066Sahrens #define DN_BONUS(dnp) ((void*)((dnp)->dn_bonus + \ 84fa9e4066Sahrens (((dnp)->dn_nblkptr - 1) * sizeof (blkptr_t)))) 85fa9e4066Sahrens 86fa9e4066Sahrens #define EPB(blkshift, typeshift) (1 << (blkshift - typeshift)) 87fa9e4066Sahrens 88fa9e4066Sahrens struct dmu_buf_impl; 89fa9e4066Sahrens struct objset_impl; 90fa9e4066Sahrens struct zio; 91fa9e4066Sahrens 92fa9e4066Sahrens enum dnode_dirtycontext { 93fa9e4066Sahrens DN_UNDIRTIED, 94fa9e4066Sahrens DN_DIRTY_OPEN, 95fa9e4066Sahrens DN_DIRTY_SYNC 96fa9e4066Sahrens }; 97fa9e4066Sahrens 98fa9e4066Sahrens typedef struct dnode_phys { 99fa9e4066Sahrens uint8_t dn_type; /* dmu_object_type_t */ 100fa9e4066Sahrens uint8_t dn_indblkshift; /* ln2(indirect block size) */ 101fa9e4066Sahrens uint8_t dn_nlevels; /* 1=dn_blkptr->data blocks */ 102fa9e4066Sahrens uint8_t dn_nblkptr; /* length of dn_blkptr */ 103fa9e4066Sahrens uint8_t dn_bonustype; /* type of data in bonus buffer */ 104fa9e4066Sahrens uint8_t dn_checksum; /* ZIO_CHECKSUM type */ 105fa9e4066Sahrens uint8_t dn_compress; /* ZIO_COMPRESS type */ 106fa9e4066Sahrens uint8_t dn_pad1[1]; 107fa9e4066Sahrens uint16_t dn_datablkszsec; /* data block size in 512b sectors */ 108fa9e4066Sahrens uint16_t dn_bonuslen; /* length of dn_bonus */ 109fa9e4066Sahrens uint8_t dn_pad2[4]; 110fa9e4066Sahrens 111fa9e4066Sahrens /* accounting is protected by dn_dirty_mtx */ 112fa9e4066Sahrens uint64_t dn_maxblkid; /* largest allocated block ID */ 113fa9e4066Sahrens uint64_t dn_secphys; /* 512b sectors of disk space used */ 114fa9e4066Sahrens 115fa9e4066Sahrens uint64_t dn_pad3[4]; 116fa9e4066Sahrens 117fa9e4066Sahrens blkptr_t dn_blkptr[1]; 118fa9e4066Sahrens uint8_t dn_bonus[DN_MAX_BONUSLEN]; 119fa9e4066Sahrens } dnode_phys_t; 120fa9e4066Sahrens 121fa9e4066Sahrens typedef struct dnode { 122fa9e4066Sahrens /* 123fa9e4066Sahrens * lock ordering: 124fa9e4066Sahrens * 125fa9e4066Sahrens * db_mtx > dn_dirty_mtx 126fa9e4066Sahrens * dbuf_syncdone 127fa9e4066Sahrens * 128fa9e4066Sahrens * dn_struct_rwlock/r > dn_dirty_mtx 129fa9e4066Sahrens * dmu_object_info 130fa9e4066Sahrens * 131fa9e4066Sahrens * dn_struct_rwlock/r > db_mtx > dn_dirty_mtx 132fa9e4066Sahrens * dbuf_dirty 133fa9e4066Sahrens * dbuf_setdirty 134fa9e4066Sahrens * 135fa9e4066Sahrens * dn_struct_rwlock/w > db_mtx > dn_mtx 136fa9e4066Sahrens * dnode_increase_indirection -> dbuf_find 137fa9e4066Sahrens * dbuf_hold_impl 138fa9e4066Sahrens * dnode_set_bonus 139fa9e4066Sahrens * 140fa9e4066Sahrens * dn_struct_rwlock/w > dn_mtx 141fa9e4066Sahrens * dnode_increase_indirection 142fa9e4066Sahrens * 143fa9e4066Sahrens * dn_dirty_mtx > dn_mtx 144fa9e4066Sahrens * dnode_buf_pageout 145fa9e4066Sahrens * 146fa9e4066Sahrens * db_mtx > dn_mtx 147fa9e4066Sahrens * dbuf_create 148fa9e4066Sahrens */ 149fa9e4066Sahrens 150fa9e4066Sahrens /* 151fa9e4066Sahrens * dn_struct_rwlock protects the structure of the dnode. 152fa9e4066Sahrens * In particular, it protects the number of levels of indirection. 153fa9e4066Sahrens */ 154fa9e4066Sahrens krwlock_t dn_struct_rwlock; 155fa9e4066Sahrens 156fa9e4066Sahrens /* 157fa9e4066Sahrens * Our link on dataset's dd_dnodes list. 158fa9e4066Sahrens * Protected by dd_accounting_mtx. 159fa9e4066Sahrens */ 160fa9e4066Sahrens list_node_t dn_link; 161fa9e4066Sahrens 162fa9e4066Sahrens /* immutable: */ 163fa9e4066Sahrens struct objset_impl *dn_objset; 164fa9e4066Sahrens uint64_t dn_object; 165fa9e4066Sahrens struct dmu_buf_impl *dn_dbuf; 166fa9e4066Sahrens dnode_phys_t *dn_phys; /* pointer into dn->dn_dbuf->db.db_data */ 167fa9e4066Sahrens 168fa9e4066Sahrens /* 169fa9e4066Sahrens * Copies of stuff in dn_phys. They're valid here even before 170fa9e4066Sahrens * the dnode is first synced. 171fa9e4066Sahrens */ 172fa9e4066Sahrens dmu_object_type_t dn_type; /* object type (immutable) */ 173fa9e4066Sahrens uint8_t dn_bonustype; /* bonus type (immutable) */ 174fa9e4066Sahrens uint16_t dn_bonuslen; /* bonus length (immutable) */ 175fa9e4066Sahrens uint8_t dn_nblkptr; /* number of blkptrs (immutable) */ 176fa9e4066Sahrens uint8_t dn_datablkshift; /* zero if blksz not power of 2! */ 177fa9e4066Sahrens uint32_t dn_datablksz; /* in bytes */ 178fa9e4066Sahrens uint16_t dn_datablkszsec; /* in 512b sectors */ 179fa9e4066Sahrens 180fa9e4066Sahrens uint8_t dn_checksum; /* ZIO_CHECKSUM type */ 181fa9e4066Sahrens uint8_t dn_compress; /* ZIO_COMPRESS type */ 182fa9e4066Sahrens 183fa9e4066Sahrens /* 184fa9e4066Sahrens * The following are kept up-to-date in the *open* context, the syncing 185fa9e4066Sahrens * context should only pay attention to the dn_next_* values. 186fa9e4066Sahrens */ 187fa9e4066Sahrens uint8_t dn_nlevels; 188fa9e4066Sahrens uint8_t dn_indblkshift; 189fa9e4066Sahrens 190fa9e4066Sahrens uint8_t dn_next_nlevels[TXG_SIZE]; 191fa9e4066Sahrens uint8_t dn_next_indblkshift[TXG_SIZE]; 192fa9e4066Sahrens 193fa9e4066Sahrens /* protected by os_lock: */ 194fa9e4066Sahrens uint32_t dn_dirtyblksz[TXG_SIZE]; /* dirty block size in bytes */ 195fa9e4066Sahrens list_node_t dn_dirty_link[TXG_SIZE]; /* next on dataset's dirty */ 196fa9e4066Sahrens 197fa9e4066Sahrens /* protected by dn_mtx: */ 198fa9e4066Sahrens kmutex_t dn_mtx; 199fa9e4066Sahrens list_t dn_dirty_dbufs[TXG_SIZE]; 200fa9e4066Sahrens uint64_t dn_maxblkid; 201fa9e4066Sahrens avl_tree_t dn_ranges[TXG_SIZE]; 202fa9e4066Sahrens uint64_t dn_allocated_txg; 203fa9e4066Sahrens uint64_t dn_free_txg; 204fa9e4066Sahrens uint64_t dn_assigned_txg; 205fa9e4066Sahrens struct dmu_tx *dn_assigned_tx; /* if only one tx cares */ 206fa9e4066Sahrens kcondvar_t dn_notxholds; 207fa9e4066Sahrens enum dnode_dirtycontext dn_dirtyctx; 208fa9e4066Sahrens uint8_t *dn_dirtyctx_firstset; /* dbg: contents meaningless */ 209fa9e4066Sahrens 210fa9e4066Sahrens /* protected by own devices */ 211fa9e4066Sahrens refcount_t dn_tx_holds; 212fa9e4066Sahrens refcount_t dn_holds; 213fa9e4066Sahrens 214fa9e4066Sahrens kmutex_t dn_dbufs_mtx; 215fa9e4066Sahrens list_t dn_dbufs; /* linked list of descendent dbuf_t's */ 216fa9e4066Sahrens kcondvar_t dn_evicted; /* a child dbuf has been evicted */ 217fa9e4066Sahrens 218fa9e4066Sahrens /* 219fa9e4066Sahrens * Performance hack: whenever we have a hold on the bonus buffer of a 220fa9e4066Sahrens * ZAP object, we will also have a hold on db0. This will keep the 221fa9e4066Sahrens * meta-data for a micro-zap object cached as long as the znode for the 222fa9e4066Sahrens * object is in the znode cache. 223fa9e4066Sahrens */ 224fa9e4066Sahrens struct dmu_buf_impl *dn_db0; 225fa9e4066Sahrens 226fa9e4066Sahrens /* holds prefetch structure */ 227fa9e4066Sahrens struct zfetch dn_zfetch; 228fa9e4066Sahrens } dnode_t; 229fa9e4066Sahrens 230fa9e4066Sahrens typedef struct free_range { 231fa9e4066Sahrens avl_node_t fr_node; 232fa9e4066Sahrens uint64_t fr_blkid; 233fa9e4066Sahrens uint64_t fr_nblks; 234fa9e4066Sahrens } free_range_t; 235fa9e4066Sahrens 236fa9e4066Sahrens dnode_t *dnode_special_open(struct objset_impl *dd, dnode_phys_t *dnp, 237fa9e4066Sahrens uint64_t object); 238fa9e4066Sahrens void dnode_special_close(dnode_t *dn); 239fa9e4066Sahrens 240fa9e4066Sahrens dnode_t *dnode_hold(struct objset_impl *dd, uint64_t object, void *ref); 241fa9e4066Sahrens dnode_t *dnode_hold_impl(struct objset_impl *dd, uint64_t object, int flag, 242fa9e4066Sahrens void *ref); 243fa9e4066Sahrens void dnode_add_ref(dnode_t *dn, void *ref); 244fa9e4066Sahrens void dnode_rele(dnode_t *dn, void *ref); 245fa9e4066Sahrens void dnode_setdirty(dnode_t *dn, dmu_tx_t *tx); 246fa9e4066Sahrens int dnode_sync(dnode_t *dn, int level, struct zio *zio, dmu_tx_t *tx); 247fa9e4066Sahrens void dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs, 248fa9e4066Sahrens dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx); 249fa9e4066Sahrens void dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, 250fa9e4066Sahrens dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx); 251fa9e4066Sahrens void dnode_free(dnode_t *dn, dmu_tx_t *tx); 252fa9e4066Sahrens void dnode_byteswap(dnode_phys_t *dnp); 253fa9e4066Sahrens void dnode_buf_byteswap(void *buf, size_t size); 254fa9e4066Sahrens void dnode_verify(dnode_t *dn); 255fa9e4066Sahrens int dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx); 256fa9e4066Sahrens uint64_t dnode_current_max_length(dnode_t *dn); 257fa9e4066Sahrens uint64_t dnode_max_nonzero_offset(dnode_t *dn); 258fa9e4066Sahrens void dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx); 259fa9e4066Sahrens void dnode_clear_range(dnode_t *dn, uint64_t blkid, 260fa9e4066Sahrens uint64_t nblks, dmu_tx_t *tx); 261fa9e4066Sahrens void dnode_diduse_space(dnode_t *dn, int64_t space); 262fa9e4066Sahrens void dnode_willuse_space(dnode_t *dn, int64_t space, dmu_tx_t *tx); 263fa9e4066Sahrens void dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx); 264fa9e4066Sahrens uint64_t dnode_block_freed(dnode_t *dn, uint64_t blkid); 265fa9e4066Sahrens void dnode_init(void); 266fa9e4066Sahrens void dnode_fini(void); 267fa9e4066Sahrens int dnode_next_offset(dnode_t *dn, boolean_t hole, uint64_t *off, int minlvl, 268fa9e4066Sahrens uint64_t blkfill); 269fa9e4066Sahrens 270fa9e4066Sahrens #ifdef ZFS_DEBUG 271fa9e4066Sahrens 272fa9e4066Sahrens /* 273fa9e4066Sahrens * There should be a ## between the string literal and fmt, to make it 274fa9e4066Sahrens * clear that we're joining two strings together, but that piece of shit 275fa9e4066Sahrens * gcc doesn't support that preprocessor token. 276fa9e4066Sahrens */ 277fa9e4066Sahrens #define dprintf_dnode(dn, fmt, ...) do { \ 278fa9e4066Sahrens if (zfs_flags & ZFS_DEBUG_DPRINTF) { \ 279fa9e4066Sahrens char __db_buf[32]; \ 280fa9e4066Sahrens uint64_t __db_obj = (dn)->dn_object; \ 281fa9e4066Sahrens if (__db_obj == DMU_META_DNODE_OBJECT) \ 282fa9e4066Sahrens (void) strcpy(__db_buf, "mdn"); \ 283fa9e4066Sahrens else \ 284fa9e4066Sahrens (void) snprintf(__db_buf, sizeof (__db_buf), "%lld", \ 285fa9e4066Sahrens (u_longlong_t)__db_obj);\ 286fa9e4066Sahrens dprintf_ds((dn)->dn_objset->os_dsl_dataset, "obj=%s " fmt, \ 287fa9e4066Sahrens __db_buf, __VA_ARGS__); \ 288fa9e4066Sahrens } \ 289fa9e4066Sahrens _NOTE(CONSTCOND) } while (0) 290fa9e4066Sahrens 291*9c9dc39aSek #define DNODE_VERIFY(dn) dnode_verify(dn) 292*9c9dc39aSek #define FREE_VERIFY(db, start, end, tx) free_verify(db, start, end, tx) 293*9c9dc39aSek 294fa9e4066Sahrens #else 295fa9e4066Sahrens 296fa9e4066Sahrens #define dprintf_dnode(db, fmt, ...) 297*9c9dc39aSek #define DNODE_VERIFY(dn) 298*9c9dc39aSek #define FREE_VERIFY(db, start, end, tx) 299fa9e4066Sahrens 300fa9e4066Sahrens #endif 301fa9e4066Sahrens 302fa9e4066Sahrens #ifdef __cplusplus 303fa9e4066Sahrens } 304fa9e4066Sahrens #endif 305fa9e4066Sahrens 306fa9e4066Sahrens #endif /* _SYS_DNODE_H */ 307