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