1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51544Seschrock * Common Development and Distribution License (the "License"). 61544Seschrock * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 21789Sahrens /* 2212432SMark.Shellenbaum@Sun.COM * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23789Sahrens */ 24789Sahrens 25789Sahrens #ifndef _SYS_DNODE_H 26789Sahrens #define _SYS_DNODE_H 27789Sahrens 28789Sahrens #include <sys/zfs_context.h> 29789Sahrens #include <sys/avl.h> 30789Sahrens #include <sys/spa.h> 31789Sahrens #include <sys/txg.h> 323547Smaybee #include <sys/zio.h> 33789Sahrens #include <sys/refcount.h> 34789Sahrens #include <sys/dmu_zfetch.h> 35*12684STom.Erickson@Sun.COM #include <sys/zrlock.h> 36789Sahrens 37789Sahrens #ifdef __cplusplus 38789Sahrens extern "C" { 39789Sahrens #endif 40789Sahrens 41789Sahrens /* 426992Smaybee * dnode_hold() flags. 43789Sahrens */ 44789Sahrens #define DNODE_MUST_BE_ALLOCATED 1 45789Sahrens #define DNODE_MUST_BE_FREE 2 46789Sahrens 47789Sahrens /* 486992Smaybee * dnode_next_offset() flags. 496992Smaybee */ 506992Smaybee #define DNODE_FIND_HOLE 1 516992Smaybee #define DNODE_FIND_BACKWARDS 2 526992Smaybee #define DNODE_FIND_HAVELOCK 4 536992Smaybee 546992Smaybee /* 55789Sahrens * Fixed constants. 56789Sahrens */ 57789Sahrens #define DNODE_SHIFT 9 /* 512 bytes */ 58789Sahrens #define DN_MIN_INDBLKSHIFT 10 /* 1k */ 59789Sahrens #define DN_MAX_INDBLKSHIFT 14 /* 16k */ 60789Sahrens #define DNODE_BLOCK_SHIFT 14 /* 16k */ 61789Sahrens #define DNODE_CORE_SIZE 64 /* 64 bytes for dnode sans blkptrs */ 62789Sahrens #define DN_MAX_OBJECT_SHIFT 48 /* 256 trillion (zfs_fid_t limit) */ 63789Sahrens #define DN_MAX_OFFSET_SHIFT 64 /* 2^64 bytes in a dnode */ 64789Sahrens 65789Sahrens /* 6611935SMark.Shellenbaum@Sun.COM * dnode id flags 6711935SMark.Shellenbaum@Sun.COM * 6811935SMark.Shellenbaum@Sun.COM * Note: a file will never ever have its 6911935SMark.Shellenbaum@Sun.COM * ids moved from bonus->spill 7011935SMark.Shellenbaum@Sun.COM * and only in a crypto environment would it be on spill 7111935SMark.Shellenbaum@Sun.COM */ 7211935SMark.Shellenbaum@Sun.COM #define DN_ID_CHKED_BONUS 0x1 7311935SMark.Shellenbaum@Sun.COM #define DN_ID_CHKED_SPILL 0x2 7411935SMark.Shellenbaum@Sun.COM #define DN_ID_OLD_EXIST 0x4 7511935SMark.Shellenbaum@Sun.COM #define DN_ID_NEW_EXIST 0x8 7611935SMark.Shellenbaum@Sun.COM 7711935SMark.Shellenbaum@Sun.COM /* 78789Sahrens * Derived constants. 79789Sahrens */ 80789Sahrens #define DNODE_SIZE (1 << DNODE_SHIFT) 81789Sahrens #define DN_MAX_NBLKPTR ((DNODE_SIZE - DNODE_CORE_SIZE) >> SPA_BLKPTRSHIFT) 82789Sahrens #define DN_MAX_BONUSLEN (DNODE_SIZE - DNODE_CORE_SIZE - (1 << SPA_BLKPTRSHIFT)) 831544Seschrock #define DN_MAX_OBJECT (1ULL << DN_MAX_OBJECT_SHIFT) 844944Smaybee #define DN_ZERO_BONUSLEN (DN_MAX_BONUSLEN + 1) 8511935SMark.Shellenbaum@Sun.COM #define DN_KILL_SPILLBLK (1) 86789Sahrens 87789Sahrens #define DNODES_PER_BLOCK_SHIFT (DNODE_BLOCK_SHIFT - DNODE_SHIFT) 88789Sahrens #define DNODES_PER_BLOCK (1ULL << DNODES_PER_BLOCK_SHIFT) 89789Sahrens #define DNODES_PER_LEVEL_SHIFT (DN_MAX_INDBLKSHIFT - SPA_BLKPTRSHIFT) 9011449SEric.Taylor@Sun.COM #define DNODES_PER_LEVEL (1ULL << DNODES_PER_LEVEL_SHIFT) 91789Sahrens 92789Sahrens /* The +2 here is a cheesy way to round up */ 93789Sahrens #define DN_MAX_LEVELS (2 + ((DN_MAX_OFFSET_SHIFT - SPA_MINBLOCKSHIFT) / \ 94789Sahrens (DN_MIN_INDBLKSHIFT - SPA_BLKPTRSHIFT))) 95789Sahrens 96789Sahrens #define DN_BONUS(dnp) ((void*)((dnp)->dn_bonus + \ 97789Sahrens (((dnp)->dn_nblkptr - 1) * sizeof (blkptr_t)))) 98789Sahrens 992082Seschrock #define DN_USED_BYTES(dnp) (((dnp)->dn_flags & DNODE_FLAG_USED_BYTES) ? \ 1002082Seschrock (dnp)->dn_used : (dnp)->dn_used << SPA_MINBLOCKSHIFT) 1012082Seschrock 102789Sahrens #define EPB(blkshift, typeshift) (1 << (blkshift - typeshift)) 103789Sahrens 104789Sahrens struct dmu_buf_impl; 10510298SMatthew.Ahrens@Sun.COM struct objset; 106789Sahrens struct zio; 107789Sahrens 108789Sahrens enum dnode_dirtycontext { 109789Sahrens DN_UNDIRTIED, 110789Sahrens DN_DIRTY_OPEN, 111789Sahrens DN_DIRTY_SYNC 112789Sahrens }; 113789Sahrens 1142082Seschrock /* Is dn_used in bytes? if not, it's in multiples of SPA_MINBLOCKSIZE */ 1159396SMatthew.Ahrens@Sun.COM #define DNODE_FLAG_USED_BYTES (1<<0) 1169396SMatthew.Ahrens@Sun.COM #define DNODE_FLAG_USERUSED_ACCOUNTED (1<<1) 1172082Seschrock 11811935SMark.Shellenbaum@Sun.COM /* Does dnode have a SA spill blkptr in bonus? */ 11911935SMark.Shellenbaum@Sun.COM #define DNODE_FLAG_SPILL_BLKPTR (1<<2) 12011935SMark.Shellenbaum@Sun.COM 121789Sahrens typedef struct dnode_phys { 122789Sahrens uint8_t dn_type; /* dmu_object_type_t */ 123789Sahrens uint8_t dn_indblkshift; /* ln2(indirect block size) */ 124789Sahrens uint8_t dn_nlevels; /* 1=dn_blkptr->data blocks */ 125789Sahrens uint8_t dn_nblkptr; /* length of dn_blkptr */ 126789Sahrens uint8_t dn_bonustype; /* type of data in bonus buffer */ 127789Sahrens uint8_t dn_checksum; /* ZIO_CHECKSUM type */ 128789Sahrens uint8_t dn_compress; /* ZIO_COMPRESS type */ 1292082Seschrock uint8_t dn_flags; /* DNODE_FLAG_* */ 130789Sahrens uint16_t dn_datablkszsec; /* data block size in 512b sectors */ 131789Sahrens uint16_t dn_bonuslen; /* length of dn_bonus */ 132789Sahrens uint8_t dn_pad2[4]; 133789Sahrens 134789Sahrens /* accounting is protected by dn_dirty_mtx */ 135789Sahrens uint64_t dn_maxblkid; /* largest allocated block ID */ 1362082Seschrock uint64_t dn_used; /* bytes (or sectors) of disk space */ 137789Sahrens 138789Sahrens uint64_t dn_pad3[4]; 139789Sahrens 140789Sahrens blkptr_t dn_blkptr[1]; 14111935SMark.Shellenbaum@Sun.COM uint8_t dn_bonus[DN_MAX_BONUSLEN - sizeof (blkptr_t)]; 14211935SMark.Shellenbaum@Sun.COM blkptr_t dn_spill; 143789Sahrens } dnode_phys_t; 144789Sahrens 145789Sahrens typedef struct dnode { 146789Sahrens /* 1471596Sahrens * dn_struct_rwlock protects the structure of the dnode, 1481596Sahrens * including the number of levels of indirection (dn_nlevels), 1491596Sahrens * dn_maxblkid, and dn_next_* 150789Sahrens */ 151789Sahrens krwlock_t dn_struct_rwlock; 152789Sahrens 1539396SMatthew.Ahrens@Sun.COM /* Our link on dn_objset->os_dnodes list; protected by os_lock. */ 154789Sahrens list_node_t dn_link; 155789Sahrens 156789Sahrens /* immutable: */ 15710298SMatthew.Ahrens@Sun.COM struct objset *dn_objset; 158789Sahrens uint64_t dn_object; 159789Sahrens struct dmu_buf_impl *dn_dbuf; 160*12684STom.Erickson@Sun.COM struct dnode_handle *dn_handle; 161789Sahrens dnode_phys_t *dn_phys; /* pointer into dn->dn_dbuf->db.db_data */ 162789Sahrens 163789Sahrens /* 1641596Sahrens * Copies of stuff in dn_phys. They're valid in the open 1651596Sahrens * context (eg. even before the dnode is first synced). 1661596Sahrens * Where necessary, these are protected by dn_struct_rwlock. 167789Sahrens */ 1681596Sahrens dmu_object_type_t dn_type; /* object type */ 1691596Sahrens uint16_t dn_bonuslen; /* bonus length */ 1701596Sahrens uint8_t dn_bonustype; /* bonus type */ 171789Sahrens uint8_t dn_nblkptr; /* number of blkptrs (immutable) */ 172789Sahrens uint8_t dn_checksum; /* ZIO_CHECKSUM type */ 173789Sahrens uint8_t dn_compress; /* ZIO_COMPRESS type */ 174789Sahrens uint8_t dn_nlevels; 175789Sahrens uint8_t dn_indblkshift; 1761596Sahrens uint8_t dn_datablkshift; /* zero if blksz not power of 2! */ 177*12684STom.Erickson@Sun.COM uint8_t dn_moved; /* Has this dnode been moved? */ 1781596Sahrens uint16_t dn_datablkszsec; /* in 512b sectors */ 1791596Sahrens uint32_t dn_datablksz; /* in bytes */ 1801596Sahrens uint64_t dn_maxblkid; 1818644SMark.Maybee@Sun.COM uint8_t dn_next_nblkptr[TXG_SIZE]; 182789Sahrens uint8_t dn_next_nlevels[TXG_SIZE]; 183789Sahrens uint8_t dn_next_indblkshift[TXG_SIZE]; 18411935SMark.Shellenbaum@Sun.COM uint8_t dn_next_bonustype[TXG_SIZE]; 18511935SMark.Shellenbaum@Sun.COM uint8_t dn_rm_spillblk[TXG_SIZE]; /* for removing spill blk */ 1864944Smaybee uint16_t dn_next_bonuslen[TXG_SIZE]; 1871596Sahrens uint32_t dn_next_blksz[TXG_SIZE]; /* next block size in bytes */ 188789Sahrens 189*12684STom.Erickson@Sun.COM /* protected by dn_dbufs_mtx; declared here to fill 32-bit hole */ 190*12684STom.Erickson@Sun.COM uint32_t dn_dbufs_count; /* count of dn_dbufs */ 191*12684STom.Erickson@Sun.COM 192789Sahrens /* protected by os_lock: */ 193789Sahrens list_node_t dn_dirty_link[TXG_SIZE]; /* next on dataset's dirty */ 194789Sahrens 195789Sahrens /* protected by dn_mtx: */ 196789Sahrens kmutex_t dn_mtx; 1973547Smaybee list_t dn_dirty_records[TXG_SIZE]; 198789Sahrens avl_tree_t dn_ranges[TXG_SIZE]; 199789Sahrens uint64_t dn_allocated_txg; 200789Sahrens uint64_t dn_free_txg; 201789Sahrens uint64_t dn_assigned_txg; 202789Sahrens kcondvar_t dn_notxholds; 203789Sahrens enum dnode_dirtycontext dn_dirtyctx; 204789Sahrens uint8_t *dn_dirtyctx_firstset; /* dbg: contents meaningless */ 205789Sahrens 206789Sahrens /* protected by own devices */ 207789Sahrens refcount_t dn_tx_holds; 208789Sahrens refcount_t dn_holds; 209789Sahrens 210789Sahrens kmutex_t dn_dbufs_mtx; 211*12684STom.Erickson@Sun.COM list_t dn_dbufs; /* descendent dbufs */ 212*12684STom.Erickson@Sun.COM 213*12684STom.Erickson@Sun.COM /* protected by dn_struct_rwlock */ 2141544Seschrock struct dmu_buf_impl *dn_bonus; /* bonus buffer dbuf */ 215*12684STom.Erickson@Sun.COM 21611935SMark.Shellenbaum@Sun.COM boolean_t dn_have_spill; /* have spill or are spilling */ 217789Sahrens 2183547Smaybee /* parent IO for current sync write */ 2193547Smaybee zio_t *dn_zio; 2203547Smaybee 2219396SMatthew.Ahrens@Sun.COM /* used in syncing context */ 22211935SMark.Shellenbaum@Sun.COM uint64_t dn_oldused; /* old phys used bytes */ 22311935SMark.Shellenbaum@Sun.COM uint64_t dn_oldflags; /* old phys dn_flags */ 22411935SMark.Shellenbaum@Sun.COM uint64_t dn_olduid, dn_oldgid; 22511935SMark.Shellenbaum@Sun.COM uint64_t dn_newuid, dn_newgid; 22611935SMark.Shellenbaum@Sun.COM int dn_id_flags; 2279396SMatthew.Ahrens@Sun.COM 228789Sahrens /* holds prefetch structure */ 229789Sahrens struct zfetch dn_zfetch; 230789Sahrens } dnode_t; 231789Sahrens 232*12684STom.Erickson@Sun.COM /* 233*12684STom.Erickson@Sun.COM * Adds a level of indirection between the dbuf and the dnode to avoid 234*12684STom.Erickson@Sun.COM * iterating descendent dbufs in dnode_move(). Handles are not allocated 235*12684STom.Erickson@Sun.COM * individually, but as an array of child dnodes in dnode_hold_impl(). 236*12684STom.Erickson@Sun.COM */ 237*12684STom.Erickson@Sun.COM typedef struct dnode_handle { 238*12684STom.Erickson@Sun.COM /* Protects dnh_dnode from modification by dnode_move(). */ 239*12684STom.Erickson@Sun.COM zrlock_t dnh_zrlock; 240*12684STom.Erickson@Sun.COM dnode_t *dnh_dnode; 241*12684STom.Erickson@Sun.COM } dnode_handle_t; 242*12684STom.Erickson@Sun.COM 243*12684STom.Erickson@Sun.COM typedef struct dnode_children { 244*12684STom.Erickson@Sun.COM size_t dnc_count; /* number of children */ 245*12684STom.Erickson@Sun.COM dnode_handle_t dnc_children[1]; /* sized dynamically */ 246*12684STom.Erickson@Sun.COM } dnode_children_t; 247*12684STom.Erickson@Sun.COM 248789Sahrens typedef struct free_range { 249789Sahrens avl_node_t fr_node; 250789Sahrens uint64_t fr_blkid; 251789Sahrens uint64_t fr_nblks; 252789Sahrens } free_range_t; 253789Sahrens 25410298SMatthew.Ahrens@Sun.COM dnode_t *dnode_special_open(struct objset *dd, dnode_phys_t *dnp, 255*12684STom.Erickson@Sun.COM uint64_t object, dnode_handle_t *dnh); 256*12684STom.Erickson@Sun.COM void dnode_special_close(dnode_handle_t *dnh); 257789Sahrens 2584944Smaybee void dnode_setbonuslen(dnode_t *dn, int newsize, dmu_tx_t *tx); 25911935SMark.Shellenbaum@Sun.COM void dnode_setbonus_type(dnode_t *dn, dmu_object_type_t, dmu_tx_t *tx); 26011935SMark.Shellenbaum@Sun.COM void dnode_rm_spill(dnode_t *dn, dmu_tx_t *tx); 26111935SMark.Shellenbaum@Sun.COM 26210298SMatthew.Ahrens@Sun.COM int dnode_hold(struct objset *dd, uint64_t object, 2631544Seschrock void *ref, dnode_t **dnp); 26410298SMatthew.Ahrens@Sun.COM int dnode_hold_impl(struct objset *dd, uint64_t object, int flag, 2651544Seschrock void *ref, dnode_t **dnp); 2664944Smaybee boolean_t dnode_add_ref(dnode_t *dn, void *ref); 267789Sahrens void dnode_rele(dnode_t *dn, void *ref); 268789Sahrens void dnode_setdirty(dnode_t *dn, dmu_tx_t *tx); 2693547Smaybee void dnode_sync(dnode_t *dn, dmu_tx_t *tx); 270789Sahrens void dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs, 271789Sahrens dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx); 272789Sahrens void dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, 273789Sahrens dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx); 274789Sahrens void dnode_free(dnode_t *dn, dmu_tx_t *tx); 275789Sahrens void dnode_byteswap(dnode_phys_t *dnp); 276789Sahrens void dnode_buf_byteswap(void *buf, size_t size); 277789Sahrens void dnode_verify(dnode_t *dn); 278789Sahrens int dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx); 279789Sahrens uint64_t dnode_current_max_length(dnode_t *dn); 280789Sahrens void dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx); 281789Sahrens void dnode_clear_range(dnode_t *dn, uint64_t blkid, 282789Sahrens uint64_t nblks, dmu_tx_t *tx); 283789Sahrens void dnode_diduse_space(dnode_t *dn, int64_t space); 284789Sahrens void dnode_willuse_space(dnode_t *dn, int64_t space, dmu_tx_t *tx); 2857332SJonathan.Adams@Sun.COM void dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx, boolean_t); 286789Sahrens uint64_t dnode_block_freed(dnode_t *dn, uint64_t blkid); 287789Sahrens void dnode_init(void); 288789Sahrens void dnode_fini(void); 2896992Smaybee int dnode_next_offset(dnode_t *dn, int flags, uint64_t *off, 2906992Smaybee int minlvl, uint64_t blkfill, uint64_t txg); 2914944Smaybee void dnode_evict_dbufs(dnode_t *dn); 292789Sahrens 293789Sahrens #ifdef ZFS_DEBUG 294789Sahrens 295789Sahrens /* 296789Sahrens * There should be a ## between the string literal and fmt, to make it 297789Sahrens * clear that we're joining two strings together, but that piece of shit 298789Sahrens * gcc doesn't support that preprocessor token. 299789Sahrens */ 300789Sahrens #define dprintf_dnode(dn, fmt, ...) do { \ 301789Sahrens if (zfs_flags & ZFS_DEBUG_DPRINTF) { \ 302789Sahrens char __db_buf[32]; \ 303789Sahrens uint64_t __db_obj = (dn)->dn_object; \ 304789Sahrens if (__db_obj == DMU_META_DNODE_OBJECT) \ 305789Sahrens (void) strcpy(__db_buf, "mdn"); \ 306789Sahrens else \ 307789Sahrens (void) snprintf(__db_buf, sizeof (__db_buf), "%lld", \ 308789Sahrens (u_longlong_t)__db_obj);\ 309789Sahrens dprintf_ds((dn)->dn_objset->os_dsl_dataset, "obj=%s " fmt, \ 310789Sahrens __db_buf, __VA_ARGS__); \ 311789Sahrens } \ 312789Sahrens _NOTE(CONSTCOND) } while (0) 313789Sahrens 314873Sek110237 #define DNODE_VERIFY(dn) dnode_verify(dn) 315873Sek110237 #define FREE_VERIFY(db, start, end, tx) free_verify(db, start, end, tx) 316873Sek110237 317789Sahrens #else 318789Sahrens 319789Sahrens #define dprintf_dnode(db, fmt, ...) 320873Sek110237 #define DNODE_VERIFY(dn) 321873Sek110237 #define FREE_VERIFY(db, start, end, tx) 322789Sahrens 323789Sahrens #endif 324789Sahrens 325789Sahrens #ifdef __cplusplus 326789Sahrens } 327789Sahrens #endif 328789Sahrens 329789Sahrens #endif /* _SYS_DNODE_H */ 330