1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5ea8dc4b6Seschrock * Common Development and Distribution License (the "License"). 6ea8dc4b6Seschrock * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 21fa9e4066Sahrens /* 2228d97a71SMark Shellenbaum * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23fa9e4066Sahrens */ 24fa9e4066Sahrens 25fa9e4066Sahrens #ifndef _SYS_DNODE_H 26fa9e4066Sahrens #define _SYS_DNODE_H 27fa9e4066Sahrens 28fa9e4066Sahrens #include <sys/zfs_context.h> 29fa9e4066Sahrens #include <sys/avl.h> 30fa9e4066Sahrens #include <sys/spa.h> 31fa9e4066Sahrens #include <sys/txg.h> 32c717a561Smaybee #include <sys/zio.h> 33fa9e4066Sahrens #include <sys/refcount.h> 34fa9e4066Sahrens #include <sys/dmu_zfetch.h> 35*744947dcSTom Erickson #include <sys/zrlock.h> 36fa9e4066Sahrens 37fa9e4066Sahrens #ifdef __cplusplus 38fa9e4066Sahrens extern "C" { 39fa9e4066Sahrens #endif 40fa9e4066Sahrens 41fa9e4066Sahrens /* 42cdb0ab79Smaybee * dnode_hold() flags. 43fa9e4066Sahrens */ 44fa9e4066Sahrens #define DNODE_MUST_BE_ALLOCATED 1 45fa9e4066Sahrens #define DNODE_MUST_BE_FREE 2 46fa9e4066Sahrens 47cdb0ab79Smaybee /* 48cdb0ab79Smaybee * dnode_next_offset() flags. 49cdb0ab79Smaybee */ 50cdb0ab79Smaybee #define DNODE_FIND_HOLE 1 51cdb0ab79Smaybee #define DNODE_FIND_BACKWARDS 2 52cdb0ab79Smaybee #define DNODE_FIND_HAVELOCK 4 53cdb0ab79Smaybee 54fa9e4066Sahrens /* 55fa9e4066Sahrens * Fixed constants. 56fa9e4066Sahrens */ 57fa9e4066Sahrens #define DNODE_SHIFT 9 /* 512 bytes */ 58fa9e4066Sahrens #define DN_MIN_INDBLKSHIFT 10 /* 1k */ 59fa9e4066Sahrens #define DN_MAX_INDBLKSHIFT 14 /* 16k */ 60fa9e4066Sahrens #define DNODE_BLOCK_SHIFT 14 /* 16k */ 61fa9e4066Sahrens #define DNODE_CORE_SIZE 64 /* 64 bytes for dnode sans blkptrs */ 62fa9e4066Sahrens #define DN_MAX_OBJECT_SHIFT 48 /* 256 trillion (zfs_fid_t limit) */ 63fa9e4066Sahrens #define DN_MAX_OFFSET_SHIFT 64 /* 2^64 bytes in a dnode */ 64fa9e4066Sahrens 650a586ceaSMark Shellenbaum /* 660a586ceaSMark Shellenbaum * dnode id flags 670a586ceaSMark Shellenbaum * 680a586ceaSMark Shellenbaum * Note: a file will never ever have its 690a586ceaSMark Shellenbaum * ids moved from bonus->spill 700a586ceaSMark Shellenbaum * and only in a crypto environment would it be on spill 710a586ceaSMark Shellenbaum */ 720a586ceaSMark Shellenbaum #define DN_ID_CHKED_BONUS 0x1 730a586ceaSMark Shellenbaum #define DN_ID_CHKED_SPILL 0x2 740a586ceaSMark Shellenbaum #define DN_ID_OLD_EXIST 0x4 750a586ceaSMark Shellenbaum #define DN_ID_NEW_EXIST 0x8 760a586ceaSMark Shellenbaum 77fa9e4066Sahrens /* 78fa9e4066Sahrens * Derived constants. 79fa9e4066Sahrens */ 80fa9e4066Sahrens #define DNODE_SIZE (1 << DNODE_SHIFT) 81fa9e4066Sahrens #define DN_MAX_NBLKPTR ((DNODE_SIZE - DNODE_CORE_SIZE) >> SPA_BLKPTRSHIFT) 82fa9e4066Sahrens #define DN_MAX_BONUSLEN (DNODE_SIZE - DNODE_CORE_SIZE - (1 << SPA_BLKPTRSHIFT)) 83ea8dc4b6Seschrock #define DN_MAX_OBJECT (1ULL << DN_MAX_OBJECT_SHIFT) 841934e92fSmaybee #define DN_ZERO_BONUSLEN (DN_MAX_BONUSLEN + 1) 850a586ceaSMark Shellenbaum #define DN_KILL_SPILLBLK (1) 86fa9e4066Sahrens 87fa9e4066Sahrens #define DNODES_PER_BLOCK_SHIFT (DNODE_BLOCK_SHIFT - DNODE_SHIFT) 88fa9e4066Sahrens #define DNODES_PER_BLOCK (1ULL << DNODES_PER_BLOCK_SHIFT) 89fa9e4066Sahrens #define DNODES_PER_LEVEL_SHIFT (DN_MAX_INDBLKSHIFT - SPA_BLKPTRSHIFT) 90c1449561SEric Taylor #define DNODES_PER_LEVEL (1ULL << DNODES_PER_LEVEL_SHIFT) 91fa9e4066Sahrens 92fa9e4066Sahrens /* The +2 here is a cheesy way to round up */ 93fa9e4066Sahrens #define DN_MAX_LEVELS (2 + ((DN_MAX_OFFSET_SHIFT - SPA_MINBLOCKSHIFT) / \ 94fa9e4066Sahrens (DN_MIN_INDBLKSHIFT - SPA_BLKPTRSHIFT))) 95fa9e4066Sahrens 96fa9e4066Sahrens #define DN_BONUS(dnp) ((void*)((dnp)->dn_bonus + \ 97fa9e4066Sahrens (((dnp)->dn_nblkptr - 1) * sizeof (blkptr_t)))) 98fa9e4066Sahrens 9999653d4eSeschrock #define DN_USED_BYTES(dnp) (((dnp)->dn_flags & DNODE_FLAG_USED_BYTES) ? \ 10099653d4eSeschrock (dnp)->dn_used : (dnp)->dn_used << SPA_MINBLOCKSHIFT) 10199653d4eSeschrock 102fa9e4066Sahrens #define EPB(blkshift, typeshift) (1 << (blkshift - typeshift)) 103fa9e4066Sahrens 104fa9e4066Sahrens struct dmu_buf_impl; 105503ad85cSMatthew Ahrens struct objset; 106fa9e4066Sahrens struct zio; 107fa9e4066Sahrens 108fa9e4066Sahrens enum dnode_dirtycontext { 109fa9e4066Sahrens DN_UNDIRTIED, 110fa9e4066Sahrens DN_DIRTY_OPEN, 111fa9e4066Sahrens DN_DIRTY_SYNC 112fa9e4066Sahrens }; 113fa9e4066Sahrens 11499653d4eSeschrock /* Is dn_used in bytes? if not, it's in multiples of SPA_MINBLOCKSIZE */ 11514843421SMatthew Ahrens #define DNODE_FLAG_USED_BYTES (1<<0) 11614843421SMatthew Ahrens #define DNODE_FLAG_USERUSED_ACCOUNTED (1<<1) 11799653d4eSeschrock 1180a586ceaSMark Shellenbaum /* Does dnode have a SA spill blkptr in bonus? */ 1190a586ceaSMark Shellenbaum #define DNODE_FLAG_SPILL_BLKPTR (1<<2) 1200a586ceaSMark Shellenbaum 121fa9e4066Sahrens typedef struct dnode_phys { 122fa9e4066Sahrens uint8_t dn_type; /* dmu_object_type_t */ 123fa9e4066Sahrens uint8_t dn_indblkshift; /* ln2(indirect block size) */ 124fa9e4066Sahrens uint8_t dn_nlevels; /* 1=dn_blkptr->data blocks */ 125fa9e4066Sahrens uint8_t dn_nblkptr; /* length of dn_blkptr */ 126fa9e4066Sahrens uint8_t dn_bonustype; /* type of data in bonus buffer */ 127fa9e4066Sahrens uint8_t dn_checksum; /* ZIO_CHECKSUM type */ 128fa9e4066Sahrens uint8_t dn_compress; /* ZIO_COMPRESS type */ 12999653d4eSeschrock uint8_t dn_flags; /* DNODE_FLAG_* */ 130fa9e4066Sahrens uint16_t dn_datablkszsec; /* data block size in 512b sectors */ 131fa9e4066Sahrens uint16_t dn_bonuslen; /* length of dn_bonus */ 132fa9e4066Sahrens uint8_t dn_pad2[4]; 133fa9e4066Sahrens 134fa9e4066Sahrens /* accounting is protected by dn_dirty_mtx */ 135fa9e4066Sahrens uint64_t dn_maxblkid; /* largest allocated block ID */ 13699653d4eSeschrock uint64_t dn_used; /* bytes (or sectors) of disk space */ 137fa9e4066Sahrens 138fa9e4066Sahrens uint64_t dn_pad3[4]; 139fa9e4066Sahrens 140fa9e4066Sahrens blkptr_t dn_blkptr[1]; 1410a586ceaSMark Shellenbaum uint8_t dn_bonus[DN_MAX_BONUSLEN - sizeof (blkptr_t)]; 1420a586ceaSMark Shellenbaum blkptr_t dn_spill; 143fa9e4066Sahrens } dnode_phys_t; 144fa9e4066Sahrens 145fa9e4066Sahrens typedef struct dnode { 146fa9e4066Sahrens /* 147c543ec06Sahrens * dn_struct_rwlock protects the structure of the dnode, 148c543ec06Sahrens * including the number of levels of indirection (dn_nlevels), 149c543ec06Sahrens * dn_maxblkid, and dn_next_* 150fa9e4066Sahrens */ 151fa9e4066Sahrens krwlock_t dn_struct_rwlock; 152fa9e4066Sahrens 15314843421SMatthew Ahrens /* Our link on dn_objset->os_dnodes list; protected by os_lock. */ 154fa9e4066Sahrens list_node_t dn_link; 155fa9e4066Sahrens 156fa9e4066Sahrens /* immutable: */ 157503ad85cSMatthew Ahrens struct objset *dn_objset; 158fa9e4066Sahrens uint64_t dn_object; 159fa9e4066Sahrens struct dmu_buf_impl *dn_dbuf; 160*744947dcSTom Erickson struct dnode_handle *dn_handle; 161fa9e4066Sahrens dnode_phys_t *dn_phys; /* pointer into dn->dn_dbuf->db.db_data */ 162fa9e4066Sahrens 163fa9e4066Sahrens /* 164c543ec06Sahrens * Copies of stuff in dn_phys. They're valid in the open 165c543ec06Sahrens * context (eg. even before the dnode is first synced). 166c543ec06Sahrens * Where necessary, these are protected by dn_struct_rwlock. 167fa9e4066Sahrens */ 168c543ec06Sahrens dmu_object_type_t dn_type; /* object type */ 169c543ec06Sahrens uint16_t dn_bonuslen; /* bonus length */ 170c543ec06Sahrens uint8_t dn_bonustype; /* bonus type */ 171fa9e4066Sahrens uint8_t dn_nblkptr; /* number of blkptrs (immutable) */ 172fa9e4066Sahrens uint8_t dn_checksum; /* ZIO_CHECKSUM type */ 173fa9e4066Sahrens uint8_t dn_compress; /* ZIO_COMPRESS type */ 174fa9e4066Sahrens uint8_t dn_nlevels; 175fa9e4066Sahrens uint8_t dn_indblkshift; 176c543ec06Sahrens uint8_t dn_datablkshift; /* zero if blksz not power of 2! */ 177*744947dcSTom Erickson uint8_t dn_moved; /* Has this dnode been moved? */ 178c543ec06Sahrens uint16_t dn_datablkszsec; /* in 512b sectors */ 179c543ec06Sahrens uint32_t dn_datablksz; /* in bytes */ 180c543ec06Sahrens uint64_t dn_maxblkid; 181da03de99SMark Maybee uint8_t dn_next_nblkptr[TXG_SIZE]; 182fa9e4066Sahrens uint8_t dn_next_nlevels[TXG_SIZE]; 183fa9e4066Sahrens uint8_t dn_next_indblkshift[TXG_SIZE]; 1840a586ceaSMark Shellenbaum uint8_t dn_next_bonustype[TXG_SIZE]; 1850a586ceaSMark Shellenbaum uint8_t dn_rm_spillblk[TXG_SIZE]; /* for removing spill blk */ 1861934e92fSmaybee uint16_t dn_next_bonuslen[TXG_SIZE]; 187c543ec06Sahrens uint32_t dn_next_blksz[TXG_SIZE]; /* next block size in bytes */ 188fa9e4066Sahrens 189*744947dcSTom Erickson /* protected by dn_dbufs_mtx; declared here to fill 32-bit hole */ 190*744947dcSTom Erickson uint32_t dn_dbufs_count; /* count of dn_dbufs */ 191*744947dcSTom Erickson 192fa9e4066Sahrens /* protected by os_lock: */ 193fa9e4066Sahrens list_node_t dn_dirty_link[TXG_SIZE]; /* next on dataset's dirty */ 194fa9e4066Sahrens 195fa9e4066Sahrens /* protected by dn_mtx: */ 196fa9e4066Sahrens kmutex_t dn_mtx; 197c717a561Smaybee list_t dn_dirty_records[TXG_SIZE]; 198fa9e4066Sahrens avl_tree_t dn_ranges[TXG_SIZE]; 199fa9e4066Sahrens uint64_t dn_allocated_txg; 200fa9e4066Sahrens uint64_t dn_free_txg; 201fa9e4066Sahrens uint64_t dn_assigned_txg; 202fa9e4066Sahrens kcondvar_t dn_notxholds; 203fa9e4066Sahrens enum dnode_dirtycontext dn_dirtyctx; 204fa9e4066Sahrens uint8_t *dn_dirtyctx_firstset; /* dbg: contents meaningless */ 205fa9e4066Sahrens 206fa9e4066Sahrens /* protected by own devices */ 207fa9e4066Sahrens refcount_t dn_tx_holds; 208fa9e4066Sahrens refcount_t dn_holds; 209fa9e4066Sahrens 210fa9e4066Sahrens kmutex_t dn_dbufs_mtx; 211*744947dcSTom Erickson list_t dn_dbufs; /* descendent dbufs */ 212*744947dcSTom Erickson 213*744947dcSTom Erickson /* protected by dn_struct_rwlock */ 214ea8dc4b6Seschrock struct dmu_buf_impl *dn_bonus; /* bonus buffer dbuf */ 215*744947dcSTom Erickson 2160a586ceaSMark Shellenbaum boolean_t dn_have_spill; /* have spill or are spilling */ 217fa9e4066Sahrens 218c717a561Smaybee /* parent IO for current sync write */ 219c717a561Smaybee zio_t *dn_zio; 220c717a561Smaybee 22114843421SMatthew Ahrens /* used in syncing context */ 2220a586ceaSMark Shellenbaum uint64_t dn_oldused; /* old phys used bytes */ 2230a586ceaSMark Shellenbaum uint64_t dn_oldflags; /* old phys dn_flags */ 2240a586ceaSMark Shellenbaum uint64_t dn_olduid, dn_oldgid; 2250a586ceaSMark Shellenbaum uint64_t dn_newuid, dn_newgid; 2260a586ceaSMark Shellenbaum int dn_id_flags; 22714843421SMatthew Ahrens 228fa9e4066Sahrens /* holds prefetch structure */ 229fa9e4066Sahrens struct zfetch dn_zfetch; 230fa9e4066Sahrens } dnode_t; 231fa9e4066Sahrens 232*744947dcSTom Erickson /* 233*744947dcSTom Erickson * Adds a level of indirection between the dbuf and the dnode to avoid 234*744947dcSTom Erickson * iterating descendent dbufs in dnode_move(). Handles are not allocated 235*744947dcSTom Erickson * individually, but as an array of child dnodes in dnode_hold_impl(). 236*744947dcSTom Erickson */ 237*744947dcSTom Erickson typedef struct dnode_handle { 238*744947dcSTom Erickson /* Protects dnh_dnode from modification by dnode_move(). */ 239*744947dcSTom Erickson zrlock_t dnh_zrlock; 240*744947dcSTom Erickson dnode_t *dnh_dnode; 241*744947dcSTom Erickson } dnode_handle_t; 242*744947dcSTom Erickson 243*744947dcSTom Erickson typedef struct dnode_children { 244*744947dcSTom Erickson size_t dnc_count; /* number of children */ 245*744947dcSTom Erickson dnode_handle_t dnc_children[1]; /* sized dynamically */ 246*744947dcSTom Erickson } dnode_children_t; 247*744947dcSTom Erickson 248fa9e4066Sahrens typedef struct free_range { 249fa9e4066Sahrens avl_node_t fr_node; 250fa9e4066Sahrens uint64_t fr_blkid; 251fa9e4066Sahrens uint64_t fr_nblks; 252fa9e4066Sahrens } free_range_t; 253fa9e4066Sahrens 254503ad85cSMatthew Ahrens dnode_t *dnode_special_open(struct objset *dd, dnode_phys_t *dnp, 255*744947dcSTom Erickson uint64_t object, dnode_handle_t *dnh); 256*744947dcSTom Erickson void dnode_special_close(dnode_handle_t *dnh); 257fa9e4066Sahrens 2581934e92fSmaybee void dnode_setbonuslen(dnode_t *dn, int newsize, dmu_tx_t *tx); 2590a586ceaSMark Shellenbaum void dnode_setbonus_type(dnode_t *dn, dmu_object_type_t, dmu_tx_t *tx); 2600a586ceaSMark Shellenbaum void dnode_rm_spill(dnode_t *dn, dmu_tx_t *tx); 2610a586ceaSMark Shellenbaum 262503ad85cSMatthew Ahrens int dnode_hold(struct objset *dd, uint64_t object, 263ea8dc4b6Seschrock void *ref, dnode_t **dnp); 264503ad85cSMatthew Ahrens int dnode_hold_impl(struct objset *dd, uint64_t object, int flag, 265ea8dc4b6Seschrock void *ref, dnode_t **dnp); 2661934e92fSmaybee boolean_t dnode_add_ref(dnode_t *dn, void *ref); 267fa9e4066Sahrens void dnode_rele(dnode_t *dn, void *ref); 268fa9e4066Sahrens void dnode_setdirty(dnode_t *dn, dmu_tx_t *tx); 269c717a561Smaybee void dnode_sync(dnode_t *dn, dmu_tx_t *tx); 270fa9e4066Sahrens void dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs, 271fa9e4066Sahrens dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx); 272fa9e4066Sahrens void dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, 273fa9e4066Sahrens dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx); 274fa9e4066Sahrens void dnode_free(dnode_t *dn, dmu_tx_t *tx); 275fa9e4066Sahrens void dnode_byteswap(dnode_phys_t *dnp); 276fa9e4066Sahrens void dnode_buf_byteswap(void *buf, size_t size); 277fa9e4066Sahrens void dnode_verify(dnode_t *dn); 278fa9e4066Sahrens int dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx); 279fa9e4066Sahrens uint64_t dnode_current_max_length(dnode_t *dn); 280fa9e4066Sahrens void dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx); 281fa9e4066Sahrens void dnode_clear_range(dnode_t *dn, uint64_t blkid, 282fa9e4066Sahrens uint64_t nblks, dmu_tx_t *tx); 283fa9e4066Sahrens void dnode_diduse_space(dnode_t *dn, int64_t space); 284fa9e4066Sahrens void dnode_willuse_space(dnode_t *dn, int64_t space, dmu_tx_t *tx); 2858346f03fSJonathan W Adams void dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx, boolean_t); 286fa9e4066Sahrens uint64_t dnode_block_freed(dnode_t *dn, uint64_t blkid); 287fa9e4066Sahrens void dnode_init(void); 288fa9e4066Sahrens void dnode_fini(void); 289cdb0ab79Smaybee int dnode_next_offset(dnode_t *dn, int flags, uint64_t *off, 290cdb0ab79Smaybee int minlvl, uint64_t blkfill, uint64_t txg); 2911934e92fSmaybee void dnode_evict_dbufs(dnode_t *dn); 292fa9e4066Sahrens 293fa9e4066Sahrens #ifdef ZFS_DEBUG 294fa9e4066Sahrens 295fa9e4066Sahrens /* 296fa9e4066Sahrens * There should be a ## between the string literal and fmt, to make it 297fa9e4066Sahrens * clear that we're joining two strings together, but that piece of shit 298fa9e4066Sahrens * gcc doesn't support that preprocessor token. 299fa9e4066Sahrens */ 300fa9e4066Sahrens #define dprintf_dnode(dn, fmt, ...) do { \ 301fa9e4066Sahrens if (zfs_flags & ZFS_DEBUG_DPRINTF) { \ 302fa9e4066Sahrens char __db_buf[32]; \ 303fa9e4066Sahrens uint64_t __db_obj = (dn)->dn_object; \ 304fa9e4066Sahrens if (__db_obj == DMU_META_DNODE_OBJECT) \ 305fa9e4066Sahrens (void) strcpy(__db_buf, "mdn"); \ 306fa9e4066Sahrens else \ 307fa9e4066Sahrens (void) snprintf(__db_buf, sizeof (__db_buf), "%lld", \ 308fa9e4066Sahrens (u_longlong_t)__db_obj);\ 309fa9e4066Sahrens dprintf_ds((dn)->dn_objset->os_dsl_dataset, "obj=%s " fmt, \ 310fa9e4066Sahrens __db_buf, __VA_ARGS__); \ 311fa9e4066Sahrens } \ 312fa9e4066Sahrens _NOTE(CONSTCOND) } while (0) 313fa9e4066Sahrens 3149c9dc39aSek #define DNODE_VERIFY(dn) dnode_verify(dn) 3159c9dc39aSek #define FREE_VERIFY(db, start, end, tx) free_verify(db, start, end, tx) 3169c9dc39aSek 317fa9e4066Sahrens #else 318fa9e4066Sahrens 319fa9e4066Sahrens #define dprintf_dnode(db, fmt, ...) 3209c9dc39aSek #define DNODE_VERIFY(dn) 3219c9dc39aSek #define FREE_VERIFY(db, start, end, tx) 322fa9e4066Sahrens 323fa9e4066Sahrens #endif 324fa9e4066Sahrens 325fa9e4066Sahrens #ifdef __cplusplus 326fa9e4066Sahrens } 327fa9e4066Sahrens #endif 328fa9e4066Sahrens 329fa9e4066Sahrens #endif /* _SYS_DNODE_H */ 330