dnode.h revision 307292
1338414Semaste/* 2260684Skaiw * CDDL HEADER START 3338414Semaste * 4260684Skaiw * The contents of this file are subject to the terms of the 5260684Skaiw * Common Development and Distribution License (the "License"). 6260684Skaiw * You may not use this file except in compliance with the License. 7260684Skaiw * 8260684Skaiw * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9260684Skaiw * or http://www.opensolaris.org/os/licensing. 10260684Skaiw * See the License for the specific language governing permissions 11260684Skaiw * and limitations under the License. 12260684Skaiw * 13260684Skaiw * When distributing Covered Code, include this CDDL HEADER in each 14260684Skaiw * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15260684Skaiw * If applicable, add the following below this CDDL HEADER, with the 16260684Skaiw * fields enclosed by brackets "[]" replaced with your own identifying 17260684Skaiw * information: Portions Copyright [yyyy] [name of copyright owner] 18260684Skaiw * 19260684Skaiw * CDDL HEADER END 20260684Skaiw */ 21260684Skaiw/* 22260684Skaiw * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23260684Skaiw * Copyright (c) 2012, 2016 by Delphix. All rights reserved. 24260684Skaiw * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved. 25260684Skaiw */ 26260684Skaiw 27260684Skaiw#ifndef _SYS_DNODE_H 28260684Skaiw#define _SYS_DNODE_H 29260684Skaiw 30260684Skaiw#include <sys/zfs_context.h> 31260684Skaiw#include <sys/avl.h> 32260684Skaiw#include <sys/spa.h> 33260684Skaiw#include <sys/txg.h> 34260684Skaiw#include <sys/zio.h> 35260684Skaiw#include <sys/refcount.h> 36260684Skaiw#include <sys/dmu_zfetch.h> 37260684Skaiw#include <sys/zrlock.h> 38260684Skaiw 39260684Skaiw#ifdef __cplusplus 40260684Skaiwextern "C" { 41260684Skaiw#endif 42260684Skaiw 43260684Skaiw/* 44260684Skaiw * dnode_hold() flags. 45260684Skaiw */ 46260684Skaiw#define DNODE_MUST_BE_ALLOCATED 1 47260684Skaiw#define DNODE_MUST_BE_FREE 2 48260684Skaiw 49260684Skaiw/* 50260684Skaiw * dnode_next_offset() flags. 51260684Skaiw */ 52260684Skaiw#define DNODE_FIND_HOLE 1 53260684Skaiw#define DNODE_FIND_BACKWARDS 2 54260684Skaiw#define DNODE_FIND_HAVELOCK 4 55260684Skaiw 56260684Skaiw/* 57260684Skaiw * Fixed constants. 58260684Skaiw */ 59260684Skaiw#define DNODE_SHIFT 9 /* 512 bytes */ 60260684Skaiw#define DN_MIN_INDBLKSHIFT 12 /* 4k */ 61260684Skaiw#define DN_MAX_INDBLKSHIFT 17 /* 128k */ 62260684Skaiw#define DNODE_BLOCK_SHIFT 14 /* 16k */ 63260684Skaiw#define DNODE_CORE_SIZE 64 /* 64 bytes for dnode sans blkptrs */ 64260684Skaiw#define DN_MAX_OBJECT_SHIFT 48 /* 256 trillion (zfs_fid_t limit) */ 65260684Skaiw#define DN_MAX_OFFSET_SHIFT 64 /* 2^64 bytes in a dnode */ 66260684Skaiw 67260684Skaiw/* 68260684Skaiw * dnode id flags 69260684Skaiw * 70260684Skaiw * Note: a file will never ever have its 71260684Skaiw * ids moved from bonus->spill 72260684Skaiw * and only in a crypto environment would it be on spill 73260684Skaiw */ 74260684Skaiw#define DN_ID_CHKED_BONUS 0x1 75260684Skaiw#define DN_ID_CHKED_SPILL 0x2 76260684Skaiw#define DN_ID_OLD_EXIST 0x4 77260684Skaiw#define DN_ID_NEW_EXIST 0x8 78260684Skaiw 79260684Skaiw/* 80260684Skaiw * Derived constants. 81260684Skaiw */ 82260684Skaiw#define DNODE_SIZE (1 << DNODE_SHIFT) 83260684Skaiw#define DN_MAX_NBLKPTR ((DNODE_SIZE - DNODE_CORE_SIZE) >> SPA_BLKPTRSHIFT) 84260684Skaiw#define DN_MAX_BONUSLEN (DNODE_SIZE - DNODE_CORE_SIZE - (1 << SPA_BLKPTRSHIFT)) 85260684Skaiw#define DN_MAX_OBJECT (1ULL << DN_MAX_OBJECT_SHIFT) 86260684Skaiw#define DN_ZERO_BONUSLEN (DN_MAX_BONUSLEN + 1) 87260684Skaiw#define DN_KILL_SPILLBLK (1) 88260684Skaiw 89260684Skaiw#define DNODES_PER_BLOCK_SHIFT (DNODE_BLOCK_SHIFT - DNODE_SHIFT) 90260684Skaiw#define DNODES_PER_BLOCK (1ULL << DNODES_PER_BLOCK_SHIFT) 91260684Skaiw 92260684Skaiw/* 93260684Skaiw * This is inaccurate if the indblkshift of the particular object is not the 94260684Skaiw * max. But it's only used by userland to calculate the zvol reservation. 95260684Skaiw */ 96260684Skaiw#define DNODES_PER_LEVEL_SHIFT (DN_MAX_INDBLKSHIFT - SPA_BLKPTRSHIFT) 97260684Skaiw#define DNODES_PER_LEVEL (1ULL << DNODES_PER_LEVEL_SHIFT) 98260684Skaiw 99260684Skaiw/* The +2 here is a cheesy way to round up */ 100260684Skaiw#define DN_MAX_LEVELS (2 + ((DN_MAX_OFFSET_SHIFT - SPA_MINBLOCKSHIFT) / \ 101260684Skaiw (DN_MIN_INDBLKSHIFT - SPA_BLKPTRSHIFT))) 102260684Skaiw 103260684Skaiw#define DN_BONUS(dnp) ((void*)((dnp)->dn_bonus + \ 104260684Skaiw (((dnp)->dn_nblkptr - 1) * sizeof (blkptr_t)))) 105260684Skaiw 106260684Skaiw#define DN_USED_BYTES(dnp) (((dnp)->dn_flags & DNODE_FLAG_USED_BYTES) ? \ 107260684Skaiw (dnp)->dn_used : (dnp)->dn_used << SPA_MINBLOCKSHIFT) 108260684Skaiw 109260684Skaiw#define EPB(blkshift, typeshift) (1 << (blkshift - typeshift)) 110260684Skaiw 111260684Skaiwstruct dmu_buf_impl; 112260684Skaiwstruct objset; 113260684Skaiwstruct zio; 114260684Skaiw 115260684Skaiwenum dnode_dirtycontext { 116260684Skaiw DN_UNDIRTIED, 117260684Skaiw DN_DIRTY_OPEN, 118260684Skaiw DN_DIRTY_SYNC 119260684Skaiw}; 120260684Skaiw 121260684Skaiw/* Is dn_used in bytes? if not, it's in multiples of SPA_MINBLOCKSIZE */ 122260684Skaiw#define DNODE_FLAG_USED_BYTES (1<<0) 123260684Skaiw#define DNODE_FLAG_USERUSED_ACCOUNTED (1<<1) 124260684Skaiw 125260684Skaiw/* Does dnode have a SA spill blkptr in bonus? */ 126260684Skaiw#define DNODE_FLAG_SPILL_BLKPTR (1<<2) 127260684Skaiw 128260684Skaiwtypedef struct dnode_phys { 129260684Skaiw uint8_t dn_type; /* dmu_object_type_t */ 130260684Skaiw uint8_t dn_indblkshift; /* ln2(indirect block size) */ 131260684Skaiw uint8_t dn_nlevels; /* 1=dn_blkptr->data blocks */ 132260684Skaiw uint8_t dn_nblkptr; /* length of dn_blkptr */ 133260684Skaiw uint8_t dn_bonustype; /* type of data in bonus buffer */ 134260684Skaiw uint8_t dn_checksum; /* ZIO_CHECKSUM type */ 135260684Skaiw uint8_t dn_compress; /* ZIO_COMPRESS type */ 136260684Skaiw uint8_t dn_flags; /* DNODE_FLAG_* */ 137260684Skaiw uint16_t dn_datablkszsec; /* data block size in 512b sectors */ 138260684Skaiw uint16_t dn_bonuslen; /* length of dn_bonus */ 139260684Skaiw uint8_t dn_pad2[4]; 140260684Skaiw 141260684Skaiw /* accounting is protected by dn_dirty_mtx */ 142260684Skaiw uint64_t dn_maxblkid; /* largest allocated block ID */ 143260684Skaiw uint64_t dn_used; /* bytes (or sectors) of disk space */ 144260684Skaiw 145260684Skaiw uint64_t dn_pad3[4]; 146260684Skaiw 147260684Skaiw blkptr_t dn_blkptr[1]; 148260684Skaiw uint8_t dn_bonus[DN_MAX_BONUSLEN - sizeof (blkptr_t)]; 149260684Skaiw blkptr_t dn_spill; 150260684Skaiw} dnode_phys_t; 151260684Skaiw 152260684Skaiwstruct dnode { 153260684Skaiw /* 154260684Skaiw * Protects the structure of the dnode, including the number of levels 155260684Skaiw * of indirection (dn_nlevels), dn_maxblkid, and dn_next_* 156260684Skaiw */ 157260684Skaiw krwlock_t dn_struct_rwlock; 158260684Skaiw 159260684Skaiw /* Our link on dn_objset->os_dnodes list; protected by os_lock. */ 160260684Skaiw list_node_t dn_link; 161260684Skaiw 162260684Skaiw /* immutable: */ 163260684Skaiw struct objset *dn_objset; 164260684Skaiw uint64_t dn_object; 165260684Skaiw struct dmu_buf_impl *dn_dbuf; 166 struct dnode_handle *dn_handle; 167 dnode_phys_t *dn_phys; /* pointer into dn->dn_dbuf->db.db_data */ 168 169 /* 170 * Copies of stuff in dn_phys. They're valid in the open 171 * context (eg. even before the dnode is first synced). 172 * Where necessary, these are protected by dn_struct_rwlock. 173 */ 174 dmu_object_type_t dn_type; /* object type */ 175 uint16_t dn_bonuslen; /* bonus length */ 176 uint8_t dn_bonustype; /* bonus type */ 177 uint8_t dn_nblkptr; /* number of blkptrs (immutable) */ 178 uint8_t dn_checksum; /* ZIO_CHECKSUM type */ 179 uint8_t dn_compress; /* ZIO_COMPRESS type */ 180 uint8_t dn_nlevels; 181 uint8_t dn_indblkshift; 182 uint8_t dn_datablkshift; /* zero if blksz not power of 2! */ 183 uint8_t dn_moved; /* Has this dnode been moved? */ 184 uint16_t dn_datablkszsec; /* in 512b sectors */ 185 uint32_t dn_datablksz; /* in bytes */ 186 uint64_t dn_maxblkid; 187 uint8_t dn_next_type[TXG_SIZE]; 188 uint8_t dn_next_nblkptr[TXG_SIZE]; 189 uint8_t dn_next_nlevels[TXG_SIZE]; 190 uint8_t dn_next_indblkshift[TXG_SIZE]; 191 uint8_t dn_next_bonustype[TXG_SIZE]; 192 uint8_t dn_rm_spillblk[TXG_SIZE]; /* for removing spill blk */ 193 uint16_t dn_next_bonuslen[TXG_SIZE]; 194 uint32_t dn_next_blksz[TXG_SIZE]; /* next block size in bytes */ 195 196 /* protected by dn_dbufs_mtx; declared here to fill 32-bit hole */ 197 uint32_t dn_dbufs_count; /* count of dn_dbufs */ 198 /* There are no level-0 blocks of this blkid or higher in dn_dbufs */ 199 uint64_t dn_unlisted_l0_blkid; 200 201 /* protected by os_lock: */ 202 list_node_t dn_dirty_link[TXG_SIZE]; /* next on dataset's dirty */ 203 204 /* protected by dn_mtx: */ 205 kmutex_t dn_mtx; 206 list_t dn_dirty_records[TXG_SIZE]; 207 struct range_tree *dn_free_ranges[TXG_SIZE]; 208 uint64_t dn_allocated_txg; 209 uint64_t dn_free_txg; 210 uint64_t dn_assigned_txg; 211 kcondvar_t dn_notxholds; 212 enum dnode_dirtycontext dn_dirtyctx; 213 uint8_t *dn_dirtyctx_firstset; /* dbg: contents meaningless */ 214 215 /* protected by own devices */ 216 refcount_t dn_tx_holds; 217 refcount_t dn_holds; 218 219 kmutex_t dn_dbufs_mtx; 220 /* 221 * Descendent dbufs, ordered by dbuf_compare. Note that dn_dbufs 222 * can contain multiple dbufs of the same (level, blkid) when a 223 * dbuf is marked DB_EVICTING without being removed from 224 * dn_dbufs. To maintain the avl invariant that there cannot be 225 * duplicate entries, we order the dbufs by an arbitrary value - 226 * their address in memory. This means that dn_dbufs cannot be used to 227 * directly look up a dbuf. Instead, callers must use avl_walk, have 228 * a reference to the dbuf, or look up a non-existant node with 229 * db_state = DB_SEARCH (see dbuf_free_range for an example). 230 */ 231 avl_tree_t dn_dbufs; 232 233 /* protected by dn_struct_rwlock */ 234 struct dmu_buf_impl *dn_bonus; /* bonus buffer dbuf */ 235 236 boolean_t dn_have_spill; /* have spill or are spilling */ 237 238 /* parent IO for current sync write */ 239 zio_t *dn_zio; 240 241 /* used in syncing context */ 242 uint64_t dn_oldused; /* old phys used bytes */ 243 uint64_t dn_oldflags; /* old phys dn_flags */ 244 uint64_t dn_olduid, dn_oldgid; 245 uint64_t dn_newuid, dn_newgid; 246 int dn_id_flags; 247 248 /* holds prefetch structure */ 249 struct zfetch dn_zfetch; 250}; 251 252/* 253 * Adds a level of indirection between the dbuf and the dnode to avoid 254 * iterating descendent dbufs in dnode_move(). Handles are not allocated 255 * individually, but as an array of child dnodes in dnode_hold_impl(). 256 */ 257typedef struct dnode_handle { 258 /* Protects dnh_dnode from modification by dnode_move(). */ 259 zrlock_t dnh_zrlock; 260 dnode_t *dnh_dnode; 261} dnode_handle_t; 262 263typedef struct dnode_children { 264 dmu_buf_user_t dnc_dbu; /* User evict data */ 265 size_t dnc_count; /* number of children */ 266 dnode_handle_t dnc_children[]; /* sized dynamically */ 267} dnode_children_t; 268 269typedef struct free_range { 270 avl_node_t fr_node; 271 uint64_t fr_blkid; 272 uint64_t fr_nblks; 273} free_range_t; 274 275void dnode_special_open(struct objset *dd, dnode_phys_t *dnp, 276 uint64_t object, dnode_handle_t *dnh); 277void dnode_special_close(dnode_handle_t *dnh); 278 279void dnode_setbonuslen(dnode_t *dn, int newsize, dmu_tx_t *tx); 280void dnode_setbonus_type(dnode_t *dn, dmu_object_type_t, dmu_tx_t *tx); 281void dnode_rm_spill(dnode_t *dn, dmu_tx_t *tx); 282 283int dnode_hold(struct objset *dd, uint64_t object, 284 void *ref, dnode_t **dnp); 285int dnode_hold_impl(struct objset *dd, uint64_t object, int flag, 286 void *ref, dnode_t **dnp); 287boolean_t dnode_add_ref(dnode_t *dn, void *ref); 288void dnode_rele(dnode_t *dn, void *ref); 289void dnode_rele_and_unlock(dnode_t *dn, void *tag); 290void dnode_setdirty(dnode_t *dn, dmu_tx_t *tx); 291void dnode_sync(dnode_t *dn, dmu_tx_t *tx); 292void dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs, 293 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx); 294void dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, 295 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx); 296void dnode_free(dnode_t *dn, dmu_tx_t *tx); 297void dnode_byteswap(dnode_phys_t *dnp); 298void dnode_buf_byteswap(void *buf, size_t size); 299void dnode_verify(dnode_t *dn); 300int dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx); 301void dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx); 302void dnode_diduse_space(dnode_t *dn, int64_t space); 303void dnode_willuse_space(dnode_t *dn, int64_t space, dmu_tx_t *tx); 304void dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx, boolean_t); 305uint64_t dnode_block_freed(dnode_t *dn, uint64_t blkid); 306void dnode_init(void); 307void dnode_fini(void); 308int dnode_next_offset(dnode_t *dn, int flags, uint64_t *off, 309 int minlvl, uint64_t blkfill, uint64_t txg); 310void dnode_evict_dbufs(dnode_t *dn); 311void dnode_evict_bonus(dnode_t *dn); 312 313#define DNODE_IS_CACHEABLE(_dn) \ 314 ((_dn)->dn_objset->os_primary_cache == ZFS_CACHE_ALL || \ 315 (DMU_OT_IS_METADATA((_dn)->dn_type) && \ 316 (_dn)->dn_objset->os_primary_cache == ZFS_CACHE_METADATA)) 317 318#define DNODE_META_IS_CACHEABLE(_dn) \ 319 ((_dn)->dn_objset->os_primary_cache == ZFS_CACHE_ALL || \ 320 (_dn)->dn_objset->os_primary_cache == ZFS_CACHE_METADATA) 321 322#ifdef ZFS_DEBUG 323 324/* 325 * There should be a ## between the string literal and fmt, to make it 326 * clear that we're joining two strings together, but that piece of shit 327 * gcc doesn't support that preprocessor token. 328 */ 329#define dprintf_dnode(dn, fmt, ...) do { \ 330 if (zfs_flags & ZFS_DEBUG_DPRINTF) { \ 331 char __db_buf[32]; \ 332 uint64_t __db_obj = (dn)->dn_object; \ 333 if (__db_obj == DMU_META_DNODE_OBJECT) \ 334 (void) strcpy(__db_buf, "mdn"); \ 335 else \ 336 (void) snprintf(__db_buf, sizeof (__db_buf), "%lld", \ 337 (u_longlong_t)__db_obj);\ 338 dprintf_ds((dn)->dn_objset->os_dsl_dataset, "obj=%s " fmt, \ 339 __db_buf, __VA_ARGS__); \ 340 } \ 341_NOTE(CONSTCOND) } while (0) 342 343#define DNODE_VERIFY(dn) dnode_verify(dn) 344#define FREE_VERIFY(db, start, end, tx) free_verify(db, start, end, tx) 345 346#else 347 348#define dprintf_dnode(db, fmt, ...) 349#define DNODE_VERIFY(dn) 350#define FREE_VERIFY(db, start, end, tx) 351 352#endif 353 354#ifdef __cplusplus 355} 356#endif 357 358#endif /* _SYS_DNODE_H */ 359