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 /* 2212296SLin.Ling@Sun.COM * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23789Sahrens */ 24789Sahrens 25789Sahrens #ifndef _SYS_DBUF_H 26789Sahrens #define _SYS_DBUF_H 27789Sahrens 28789Sahrens #include <sys/dmu.h> 29789Sahrens #include <sys/spa.h> 30789Sahrens #include <sys/txg.h> 31789Sahrens #include <sys/zio.h> 32789Sahrens #include <sys/arc.h> 33789Sahrens #include <sys/zfs_context.h> 34789Sahrens #include <sys/refcount.h> 35*12684STom.Erickson@Sun.COM #include <sys/zrlock.h> 36789Sahrens 37789Sahrens #ifdef __cplusplus 38789Sahrens extern "C" { 39789Sahrens #endif 40789Sahrens 413547Smaybee #define IN_DMU_SYNC 2 42789Sahrens 43789Sahrens /* 441544Seschrock * define flags for dbuf_read 45789Sahrens */ 46789Sahrens 472391Smaybee #define DB_RF_MUST_SUCCEED (1 << 0) 48789Sahrens #define DB_RF_CANFAIL (1 << 1) 49789Sahrens #define DB_RF_HAVESTRUCT (1 << 2) 50789Sahrens #define DB_RF_NOPREFETCH (1 << 3) 511544Seschrock #define DB_RF_NEVERWAIT (1 << 4) 522391Smaybee #define DB_RF_CACHED (1 << 5) 53789Sahrens 54789Sahrens /* 557872STim.Haley@Sun.COM * The simplified state transition diagram for dbufs looks like: 56789Sahrens * 57789Sahrens * +----> READ ----+ 58789Sahrens * | | 59789Sahrens * | V 601544Seschrock * (alloc)-->UNCACHED CACHED-->EVICTING-->(free) 617872STim.Haley@Sun.COM * | ^ ^ 627872STim.Haley@Sun.COM * | | | 637872STim.Haley@Sun.COM * +----> FILL ----+ | 647872STim.Haley@Sun.COM * | | 657872STim.Haley@Sun.COM * | | 667872STim.Haley@Sun.COM * +--------> NOFILL -------+ 67789Sahrens */ 68789Sahrens typedef enum dbuf_states { 69789Sahrens DB_UNCACHED, 70789Sahrens DB_FILL, 717872STim.Haley@Sun.COM DB_NOFILL, 72789Sahrens DB_READ, 731544Seschrock DB_CACHED, 741544Seschrock DB_EVICTING 75789Sahrens } dbuf_states_t; 76789Sahrens 77789Sahrens struct dnode; 78789Sahrens struct dmu_tx; 79789Sahrens 80789Sahrens /* 81789Sahrens * level = 0 means the user data 82789Sahrens * level = 1 means the single indirect block 83789Sahrens * etc. 84789Sahrens */ 85789Sahrens 863547Smaybee struct dmu_buf_impl; 873547Smaybee 883547Smaybee typedef enum override_states { 893547Smaybee DR_NOT_OVERRIDDEN, 903547Smaybee DR_IN_DMU_SYNC, 913547Smaybee DR_OVERRIDDEN 923547Smaybee } override_states_t; 933547Smaybee 943547Smaybee typedef struct dbuf_dirty_record { 953547Smaybee /* link on our parents dirty list */ 963547Smaybee list_node_t dr_dirty_node; 973547Smaybee 983547Smaybee /* transaction group this data will sync in */ 993547Smaybee uint64_t dr_txg; 1003547Smaybee 1013547Smaybee /* zio of outstanding write IO */ 1023547Smaybee zio_t *dr_zio; 1033547Smaybee 1043547Smaybee /* pointer back to our dbuf */ 1053547Smaybee struct dmu_buf_impl *dr_dbuf; 1063547Smaybee 1073547Smaybee /* pointer to next dirty record */ 1083547Smaybee struct dbuf_dirty_record *dr_next; 1093547Smaybee 1103547Smaybee /* pointer to parent dirty record */ 1113547Smaybee struct dbuf_dirty_record *dr_parent; 1123547Smaybee 1133547Smaybee union dirty_types { 1143547Smaybee struct dirty_indirect { 1153547Smaybee 1163547Smaybee /* protect access to list */ 1173547Smaybee kmutex_t dr_mtx; 1183547Smaybee 1193547Smaybee /* Our list of dirty children */ 1203547Smaybee list_t dr_children; 1213547Smaybee } di; 1223547Smaybee struct dirty_leaf { 1233547Smaybee 1243547Smaybee /* 1253547Smaybee * dr_data is set when we dirty the buffer 1263547Smaybee * so that we can retain the pointer even if it 1273547Smaybee * gets COW'd in a subsequent transaction group. 1283547Smaybee */ 1293547Smaybee arc_buf_t *dr_data; 1303547Smaybee blkptr_t dr_overridden_by; 1313547Smaybee override_states_t dr_override_state; 13210922SJeff.Bonwick@Sun.COM uint8_t dr_copies; 1333547Smaybee } dl; 1343547Smaybee } dt; 1353547Smaybee } dbuf_dirty_record_t; 1363547Smaybee 137789Sahrens typedef struct dmu_buf_impl { 138789Sahrens /* 139789Sahrens * The following members are immutable, with the exception of 140789Sahrens * db.db_data, which is protected by db_mtx. 141789Sahrens */ 142789Sahrens 143789Sahrens /* the publicly visible structure */ 144789Sahrens dmu_buf_t db; 145789Sahrens 146789Sahrens /* the objset we belong to */ 14710298SMatthew.Ahrens@Sun.COM struct objset *db_objset; 148789Sahrens 149789Sahrens /* 150*12684STom.Erickson@Sun.COM * handle to safely access the dnode we belong to (NULL when evicted) 151789Sahrens */ 152*12684STom.Erickson@Sun.COM struct dnode_handle *db_dnode_handle; 153789Sahrens 154789Sahrens /* 155789Sahrens * our parent buffer; if the dnode points to us directly, 156*12684STom.Erickson@Sun.COM * db_parent == db_dnode_handle->dnh_dnode->dn_dbuf 157789Sahrens * only accessed by sync thread ??? 158789Sahrens * (NULL when evicted) 159*12684STom.Erickson@Sun.COM * May change from NULL to non-NULL under the protection of db_mtx 160*12684STom.Erickson@Sun.COM * (see dbuf_check_blkptr()) 161789Sahrens */ 162789Sahrens struct dmu_buf_impl *db_parent; 163789Sahrens 164789Sahrens /* 165789Sahrens * link for hash table of all dmu_buf_impl_t's 166789Sahrens */ 167789Sahrens struct dmu_buf_impl *db_hash_next; 168789Sahrens 169789Sahrens /* our block number */ 170789Sahrens uint64_t db_blkid; 171789Sahrens 172789Sahrens /* 173789Sahrens * Pointer to the blkptr_t which points to us. May be NULL if we 174789Sahrens * don't have one yet. (NULL when evicted) 175789Sahrens */ 176789Sahrens blkptr_t *db_blkptr; 177789Sahrens 178789Sahrens /* 179789Sahrens * Our indirection level. Data buffers have db_level==0. 180789Sahrens * Indirect buffers which point to data buffers have 181789Sahrens * db_level==1. etc. Buffers which contain dnodes have 182789Sahrens * db_level==0, since the dnodes are stored in a file. 183789Sahrens */ 184789Sahrens uint8_t db_level; 185789Sahrens 186789Sahrens /* db_mtx protects the members below */ 187789Sahrens kmutex_t db_mtx; 188789Sahrens 189789Sahrens /* 190789Sahrens * Current state of the buffer 191789Sahrens */ 192789Sahrens dbuf_states_t db_state; 193789Sahrens 194789Sahrens /* 195789Sahrens * Refcount accessed by dmu_buf_{hold,rele}. 196789Sahrens * If nonzero, the buffer can't be destroyed. 197789Sahrens * Protected by db_mtx. 198789Sahrens */ 199789Sahrens refcount_t db_holds; 200789Sahrens 201789Sahrens /* buffer holding our data */ 202789Sahrens arc_buf_t *db_buf; 203789Sahrens 204789Sahrens kcondvar_t db_changed; 2053547Smaybee dbuf_dirty_record_t *db_data_pending; 2063547Smaybee 2073547Smaybee /* pointer to most recent dirty record for this buffer */ 2083547Smaybee dbuf_dirty_record_t *db_last_dirty; 209789Sahrens 210789Sahrens /* 2113547Smaybee * Our link on the owner dnodes's dn_dbufs list. 2121544Seschrock * Protected by its dn_dbufs_mtx. 213789Sahrens */ 214789Sahrens list_node_t db_link; 215789Sahrens 2163547Smaybee /* Data which is unique to data (leaf) blocks: */ 217789Sahrens 2183547Smaybee /* stuff we store for the user (see dmu_buf_set_user) */ 2193547Smaybee void *db_user_ptr; 2203547Smaybee void **db_user_data_ptr_ptr; 2213547Smaybee dmu_buf_evict_func_t *db_evict_func; 2223547Smaybee 2233547Smaybee uint8_t db_immediate_evict; 2243547Smaybee uint8_t db_freed_in_flight; 2253547Smaybee 2263547Smaybee uint8_t db_dirtycnt; 227789Sahrens } dmu_buf_impl_t; 228789Sahrens 229789Sahrens /* Note: the dbuf hash table is exposed only for the mdb module */ 230789Sahrens #define DBUF_MUTEXES 256 231789Sahrens #define DBUF_HASH_MUTEX(h, idx) (&(h)->hash_mutexes[(idx) & (DBUF_MUTEXES-1)]) 232789Sahrens typedef struct dbuf_hash_table { 233789Sahrens uint64_t hash_table_mask; 234789Sahrens dmu_buf_impl_t **hash_table; 235789Sahrens kmutex_t hash_mutexes[DBUF_MUTEXES]; 236789Sahrens } dbuf_hash_table_t; 237789Sahrens 238789Sahrens 239789Sahrens uint64_t dbuf_whichblock(struct dnode *di, uint64_t offset); 240789Sahrens 241789Sahrens dmu_buf_impl_t *dbuf_create_tlib(struct dnode *dn, char *data); 2424944Smaybee void dbuf_create_bonus(struct dnode *dn); 24311935SMark.Shellenbaum@Sun.COM int dbuf_spill_set_blksz(dmu_buf_t *db, uint64_t blksz, dmu_tx_t *tx); 24411935SMark.Shellenbaum@Sun.COM void dbuf_spill_hold(struct dnode *dn, dmu_buf_impl_t **dbp, void *tag); 24511935SMark.Shellenbaum@Sun.COM 24611935SMark.Shellenbaum@Sun.COM void dbuf_rm_spill(struct dnode *dn, dmu_tx_t *tx); 247789Sahrens 2481544Seschrock dmu_buf_impl_t *dbuf_hold(struct dnode *dn, uint64_t blkid, void *tag); 249789Sahrens dmu_buf_impl_t *dbuf_hold_level(struct dnode *dn, int level, uint64_t blkid, 250789Sahrens void *tag); 251789Sahrens int dbuf_hold_impl(struct dnode *dn, uint8_t level, uint64_t blkid, int create, 252789Sahrens void *tag, dmu_buf_impl_t **dbp); 253789Sahrens 254789Sahrens void dbuf_prefetch(struct dnode *dn, uint64_t blkid); 255789Sahrens 256789Sahrens void dbuf_add_ref(dmu_buf_impl_t *db, void *tag); 257789Sahrens uint64_t dbuf_refcount(dmu_buf_impl_t *db); 258789Sahrens 2591544Seschrock void dbuf_rele(dmu_buf_impl_t *db, void *tag); 26010922SJeff.Bonwick@Sun.COM void dbuf_rele_and_unlock(dmu_buf_impl_t *db, void *tag); 261789Sahrens 262789Sahrens dmu_buf_impl_t *dbuf_find(struct dnode *dn, uint8_t level, uint64_t blkid); 263789Sahrens 2641544Seschrock int dbuf_read(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags); 265789Sahrens void dbuf_will_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx); 266789Sahrens void dbuf_fill_done(dmu_buf_impl_t *db, dmu_tx_t *tx); 2677872STim.Haley@Sun.COM void dmu_buf_will_not_fill(dmu_buf_t *db, dmu_tx_t *tx); 268789Sahrens void dmu_buf_will_fill(dmu_buf_t *db, dmu_tx_t *tx); 269789Sahrens void dmu_buf_fill_done(dmu_buf_t *db, dmu_tx_t *tx); 2709412SAleksandr.Guzovskiy@Sun.COM void dbuf_assign_arcbuf(dmu_buf_impl_t *db, arc_buf_t *buf, dmu_tx_t *tx); 2713547Smaybee dbuf_dirty_record_t *dbuf_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx); 27211539SChunli.Zhang@Sun.COM arc_buf_t *dbuf_loan_arcbuf(dmu_buf_impl_t *db); 273789Sahrens 2741544Seschrock void dbuf_clear(dmu_buf_impl_t *db); 275789Sahrens void dbuf_evict(dmu_buf_impl_t *db); 276789Sahrens 277789Sahrens void dbuf_setdirty(dmu_buf_impl_t *db, dmu_tx_t *tx); 2783547Smaybee void dbuf_unoverride(dbuf_dirty_record_t *dr); 2793547Smaybee void dbuf_sync_list(list_t *list, dmu_tx_t *tx); 28012296SLin.Ling@Sun.COM void dbuf_release_bp(dmu_buf_impl_t *db); 281789Sahrens 2826992Smaybee void dbuf_free_range(struct dnode *dn, uint64_t start, uint64_t end, 283789Sahrens struct dmu_tx *); 284789Sahrens 285789Sahrens void dbuf_new_size(dmu_buf_impl_t *db, int size, dmu_tx_t *tx); 286789Sahrens 287*12684STom.Erickson@Sun.COM #define DB_DNODE(_db) ((_db)->db_dnode_handle->dnh_dnode) 288*12684STom.Erickson@Sun.COM #define DB_DNODE_LOCK(_db) ((_db)->db_dnode_handle->dnh_zrlock) 289*12684STom.Erickson@Sun.COM #define DB_DNODE_ENTER(_db) (zrl_add(&DB_DNODE_LOCK(_db))) 290*12684STom.Erickson@Sun.COM #define DB_DNODE_EXIT(_db) (zrl_remove(&DB_DNODE_LOCK(_db))) 291*12684STom.Erickson@Sun.COM #define DB_DNODE_HELD(_db) (!zrl_is_zero(&DB_DNODE_LOCK(_db))) 292*12684STom.Erickson@Sun.COM #define DB_GET_SPA(_spa_p, _db) { \ 293*12684STom.Erickson@Sun.COM dnode_t *__dn; \ 294*12684STom.Erickson@Sun.COM DB_DNODE_ENTER(_db); \ 295*12684STom.Erickson@Sun.COM __dn = DB_DNODE(_db); \ 296*12684STom.Erickson@Sun.COM *(_spa_p) = __dn->dn_objset->os_spa; \ 297*12684STom.Erickson@Sun.COM DB_DNODE_EXIT(_db); \ 298*12684STom.Erickson@Sun.COM } 299*12684STom.Erickson@Sun.COM #define DB_GET_OBJSET(_os_p, _db) { \ 300*12684STom.Erickson@Sun.COM dnode_t *__dn; \ 301*12684STom.Erickson@Sun.COM DB_DNODE_ENTER(_db); \ 302*12684STom.Erickson@Sun.COM __dn = DB_DNODE(_db); \ 303*12684STom.Erickson@Sun.COM *(_os_p) = __dn->dn_objset; \ 304*12684STom.Erickson@Sun.COM DB_DNODE_EXIT(_db); \ 305*12684STom.Erickson@Sun.COM } 306*12684STom.Erickson@Sun.COM 307789Sahrens void dbuf_init(void); 308789Sahrens void dbuf_fini(void); 309789Sahrens 310*12684STom.Erickson@Sun.COM boolean_t dbuf_is_metadata(dmu_buf_impl_t *db); 3117237Sek110237 312*12684STom.Erickson@Sun.COM #define DBUF_IS_METADATA(_db) \ 313*12684STom.Erickson@Sun.COM (dbuf_is_metadata(_db)) 314*12684STom.Erickson@Sun.COM 315*12684STom.Erickson@Sun.COM #define DBUF_GET_BUFC_TYPE(_db) \ 316*12684STom.Erickson@Sun.COM (DBUF_IS_METADATA(_db) ? ARC_BUFC_METADATA : ARC_BUFC_DATA) 3177237Sek110237 318*12684STom.Erickson@Sun.COM #define DBUF_IS_CACHEABLE(_db) \ 319*12684STom.Erickson@Sun.COM ((_db)->db_objset->os_primary_cache == ZFS_CACHE_ALL || \ 320*12684STom.Erickson@Sun.COM (DBUF_IS_METADATA(_db) && \ 321*12684STom.Erickson@Sun.COM ((_db)->db_objset->os_primary_cache == ZFS_CACHE_METADATA))) 3227237Sek110237 323*12684STom.Erickson@Sun.COM #define DBUF_IS_L2CACHEABLE(_db) \ 324*12684STom.Erickson@Sun.COM ((_db)->db_objset->os_secondary_cache == ZFS_CACHE_ALL || \ 325*12684STom.Erickson@Sun.COM (DBUF_IS_METADATA(_db) && \ 326*12684STom.Erickson@Sun.COM ((_db)->db_objset->os_secondary_cache == ZFS_CACHE_METADATA))) 3273290Sjohansen 328789Sahrens #ifdef ZFS_DEBUG 329789Sahrens 330789Sahrens /* 331789Sahrens * There should be a ## between the string literal and fmt, to make it 332896Smaybee * clear that we're joining two strings together, but gcc does not 333896Smaybee * support that preprocessor token. 334789Sahrens */ 335789Sahrens #define dprintf_dbuf(dbuf, fmt, ...) do { \ 336789Sahrens if (zfs_flags & ZFS_DEBUG_DPRINTF) { \ 337789Sahrens char __db_buf[32]; \ 338789Sahrens uint64_t __db_obj = (dbuf)->db.db_object; \ 339789Sahrens if (__db_obj == DMU_META_DNODE_OBJECT) \ 340789Sahrens (void) strcpy(__db_buf, "mdn"); \ 341789Sahrens else \ 342789Sahrens (void) snprintf(__db_buf, sizeof (__db_buf), "%lld", \ 343789Sahrens (u_longlong_t)__db_obj); \ 344789Sahrens dprintf_ds((dbuf)->db_objset->os_dsl_dataset, \ 345789Sahrens "obj=%s lvl=%u blkid=%lld " fmt, \ 346789Sahrens __db_buf, (dbuf)->db_level, \ 347789Sahrens (u_longlong_t)(dbuf)->db_blkid, __VA_ARGS__); \ 348789Sahrens } \ 349789Sahrens _NOTE(CONSTCOND) } while (0) 350789Sahrens 351789Sahrens #define dprintf_dbuf_bp(db, bp, fmt, ...) do { \ 352896Smaybee if (zfs_flags & ZFS_DEBUG_DPRINTF) { \ 3533700Sek110237 char *__blkbuf = kmem_alloc(BP_SPRINTF_LEN, KM_SLEEP); \ 35410922SJeff.Bonwick@Sun.COM sprintf_blkptr(__blkbuf, bp); \ 355789Sahrens dprintf_dbuf(db, fmt " %s\n", __VA_ARGS__, __blkbuf); \ 3563700Sek110237 kmem_free(__blkbuf, BP_SPRINTF_LEN); \ 357*12684STom.Erickson@Sun.COM } \ 358789Sahrens _NOTE(CONSTCOND) } while (0) 359789Sahrens 360873Sek110237 #define DBUF_VERIFY(db) dbuf_verify(db) 361873Sek110237 362789Sahrens #else 363789Sahrens 364789Sahrens #define dprintf_dbuf(db, fmt, ...) 365789Sahrens #define dprintf_dbuf_bp(db, bp, fmt, ...) 366873Sek110237 #define DBUF_VERIFY(db) 367789Sahrens 368789Sahrens #endif 369789Sahrens 370789Sahrens 371789Sahrens #ifdef __cplusplus 372789Sahrens } 373789Sahrens #endif 374789Sahrens 375789Sahrens #endif /* _SYS_DBUF_H */ 376