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 /* 229412SAleksandr.Guzovskiy@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens #ifndef _SYS_DBUF_H 27789Sahrens #define _SYS_DBUF_H 28789Sahrens 29789Sahrens #include <sys/dmu.h> 30789Sahrens #include <sys/spa.h> 31789Sahrens #include <sys/txg.h> 32789Sahrens #include <sys/zio.h> 33789Sahrens #include <sys/arc.h> 34789Sahrens #include <sys/zfs_context.h> 35789Sahrens #include <sys/refcount.h> 36789Sahrens 37789Sahrens #ifdef __cplusplus 38789Sahrens extern "C" { 39789Sahrens #endif 40789Sahrens 41789Sahrens #define DB_BONUS_BLKID (-1ULL) 423547Smaybee #define IN_DMU_SYNC 2 43789Sahrens 44789Sahrens /* 451544Seschrock * define flags for dbuf_read 46789Sahrens */ 47789Sahrens 482391Smaybee #define DB_RF_MUST_SUCCEED (1 << 0) 49789Sahrens #define DB_RF_CANFAIL (1 << 1) 50789Sahrens #define DB_RF_HAVESTRUCT (1 << 2) 51789Sahrens #define DB_RF_NOPREFETCH (1 << 3) 521544Seschrock #define DB_RF_NEVERWAIT (1 << 4) 532391Smaybee #define DB_RF_CACHED (1 << 5) 54789Sahrens 55789Sahrens /* 567872STim.Haley@Sun.COM * The simplified state transition diagram for dbufs looks like: 57789Sahrens * 58789Sahrens * +----> READ ----+ 59789Sahrens * | | 60789Sahrens * | V 611544Seschrock * (alloc)-->UNCACHED CACHED-->EVICTING-->(free) 627872STim.Haley@Sun.COM * | ^ ^ 637872STim.Haley@Sun.COM * | | | 647872STim.Haley@Sun.COM * +----> FILL ----+ | 657872STim.Haley@Sun.COM * | | 667872STim.Haley@Sun.COM * | | 677872STim.Haley@Sun.COM * +--------> NOFILL -------+ 68789Sahrens */ 69789Sahrens typedef enum dbuf_states { 70789Sahrens DB_UNCACHED, 71789Sahrens DB_FILL, 727872STim.Haley@Sun.COM DB_NOFILL, 73789Sahrens DB_READ, 741544Seschrock DB_CACHED, 751544Seschrock DB_EVICTING 76789Sahrens } dbuf_states_t; 77789Sahrens 78789Sahrens struct dnode; 79789Sahrens struct dmu_tx; 80789Sahrens 81789Sahrens /* 82789Sahrens * level = 0 means the user data 83789Sahrens * level = 1 means the single indirect block 84789Sahrens * etc. 85789Sahrens */ 86789Sahrens 87789Sahrens #define LIST_LINK_INACTIVE(link) \ 88789Sahrens ((link)->list_next == NULL && (link)->list_prev == NULL) 89789Sahrens 903547Smaybee struct dmu_buf_impl; 913547Smaybee 923547Smaybee typedef enum override_states { 933547Smaybee DR_NOT_OVERRIDDEN, 943547Smaybee DR_IN_DMU_SYNC, 953547Smaybee DR_OVERRIDDEN 963547Smaybee } override_states_t; 973547Smaybee 983547Smaybee typedef struct dbuf_dirty_record { 993547Smaybee /* link on our parents dirty list */ 1003547Smaybee list_node_t dr_dirty_node; 1013547Smaybee 1023547Smaybee /* transaction group this data will sync in */ 1033547Smaybee uint64_t dr_txg; 1043547Smaybee 1053547Smaybee /* zio of outstanding write IO */ 1063547Smaybee zio_t *dr_zio; 1073547Smaybee 1083547Smaybee /* pointer back to our dbuf */ 1093547Smaybee struct dmu_buf_impl *dr_dbuf; 1103547Smaybee 1113547Smaybee /* pointer to next dirty record */ 1123547Smaybee struct dbuf_dirty_record *dr_next; 1133547Smaybee 1143547Smaybee /* pointer to parent dirty record */ 1153547Smaybee struct dbuf_dirty_record *dr_parent; 1163547Smaybee 1173547Smaybee union dirty_types { 1183547Smaybee struct dirty_indirect { 1193547Smaybee 1203547Smaybee /* protect access to list */ 1213547Smaybee kmutex_t dr_mtx; 1223547Smaybee 1233547Smaybee /* Our list of dirty children */ 1243547Smaybee list_t dr_children; 1253547Smaybee } di; 1263547Smaybee struct dirty_leaf { 1273547Smaybee 1283547Smaybee /* 1293547Smaybee * dr_data is set when we dirty the buffer 1303547Smaybee * so that we can retain the pointer even if it 1313547Smaybee * gets COW'd in a subsequent transaction group. 1323547Smaybee */ 1333547Smaybee arc_buf_t *dr_data; 1343547Smaybee blkptr_t dr_overridden_by; 1353547Smaybee override_states_t dr_override_state; 136*10922SJeff.Bonwick@Sun.COM uint8_t dr_copies; 1373547Smaybee } dl; 1383547Smaybee } dt; 1393547Smaybee } dbuf_dirty_record_t; 1403547Smaybee 141789Sahrens typedef struct dmu_buf_impl { 142789Sahrens /* 143789Sahrens * The following members are immutable, with the exception of 144789Sahrens * db.db_data, which is protected by db_mtx. 145789Sahrens */ 146789Sahrens 147789Sahrens /* the publicly visible structure */ 148789Sahrens dmu_buf_t db; 149789Sahrens 150789Sahrens /* the objset we belong to */ 15110298SMatthew.Ahrens@Sun.COM struct objset *db_objset; 152789Sahrens 153789Sahrens /* 154789Sahrens * the dnode we belong to (NULL when evicted) 155789Sahrens */ 156789Sahrens struct dnode *db_dnode; 157789Sahrens 158789Sahrens /* 159789Sahrens * our parent buffer; if the dnode points to us directly, 160789Sahrens * db_parent == db_dnode->dn_dbuf 161789Sahrens * only accessed by sync thread ??? 162789Sahrens * (NULL when evicted) 163789Sahrens */ 164789Sahrens struct dmu_buf_impl *db_parent; 165789Sahrens 166789Sahrens /* 167789Sahrens * link for hash table of all dmu_buf_impl_t's 168789Sahrens */ 169789Sahrens struct dmu_buf_impl *db_hash_next; 170789Sahrens 171789Sahrens /* our block number */ 172789Sahrens uint64_t db_blkid; 173789Sahrens 174789Sahrens /* 175789Sahrens * Pointer to the blkptr_t which points to us. May be NULL if we 176789Sahrens * don't have one yet. (NULL when evicted) 177789Sahrens */ 178789Sahrens blkptr_t *db_blkptr; 179789Sahrens 180789Sahrens /* 181789Sahrens * Our indirection level. Data buffers have db_level==0. 182789Sahrens * Indirect buffers which point to data buffers have 183789Sahrens * db_level==1. etc. Buffers which contain dnodes have 184789Sahrens * db_level==0, since the dnodes are stored in a file. 185789Sahrens */ 186789Sahrens uint8_t db_level; 187789Sahrens 188789Sahrens /* db_mtx protects the members below */ 189789Sahrens kmutex_t db_mtx; 190789Sahrens 191789Sahrens /* 192789Sahrens * Current state of the buffer 193789Sahrens */ 194789Sahrens dbuf_states_t db_state; 195789Sahrens 196789Sahrens /* 197789Sahrens * Refcount accessed by dmu_buf_{hold,rele}. 198789Sahrens * If nonzero, the buffer can't be destroyed. 199789Sahrens * Protected by db_mtx. 200789Sahrens */ 201789Sahrens refcount_t db_holds; 202789Sahrens 203789Sahrens /* buffer holding our data */ 204789Sahrens arc_buf_t *db_buf; 205789Sahrens 206789Sahrens kcondvar_t db_changed; 2073547Smaybee dbuf_dirty_record_t *db_data_pending; 2083547Smaybee 2093547Smaybee /* pointer to most recent dirty record for this buffer */ 2103547Smaybee dbuf_dirty_record_t *db_last_dirty; 211789Sahrens 212789Sahrens /* 2133547Smaybee * Our link on the owner dnodes's dn_dbufs list. 2141544Seschrock * Protected by its dn_dbufs_mtx. 215789Sahrens */ 216789Sahrens list_node_t db_link; 217789Sahrens 2183547Smaybee /* Data which is unique to data (leaf) blocks: */ 219789Sahrens 2203547Smaybee /* stuff we store for the user (see dmu_buf_set_user) */ 2213547Smaybee void *db_user_ptr; 2223547Smaybee void **db_user_data_ptr_ptr; 2233547Smaybee dmu_buf_evict_func_t *db_evict_func; 2243547Smaybee 2253547Smaybee uint8_t db_immediate_evict; 2263547Smaybee uint8_t db_freed_in_flight; 2273547Smaybee 2283547Smaybee uint8_t db_dirtycnt; 229789Sahrens } dmu_buf_impl_t; 230789Sahrens 231789Sahrens /* Note: the dbuf hash table is exposed only for the mdb module */ 232789Sahrens #define DBUF_MUTEXES 256 233789Sahrens #define DBUF_HASH_MUTEX(h, idx) (&(h)->hash_mutexes[(idx) & (DBUF_MUTEXES-1)]) 234789Sahrens typedef struct dbuf_hash_table { 235789Sahrens uint64_t hash_table_mask; 236789Sahrens dmu_buf_impl_t **hash_table; 237789Sahrens kmutex_t hash_mutexes[DBUF_MUTEXES]; 238789Sahrens } dbuf_hash_table_t; 239789Sahrens 240789Sahrens 241789Sahrens uint64_t dbuf_whichblock(struct dnode *di, uint64_t offset); 242789Sahrens 243789Sahrens dmu_buf_impl_t *dbuf_create_tlib(struct dnode *dn, char *data); 2444944Smaybee void dbuf_create_bonus(struct dnode *dn); 245789Sahrens 2461544Seschrock dmu_buf_impl_t *dbuf_hold(struct dnode *dn, uint64_t blkid, void *tag); 247789Sahrens dmu_buf_impl_t *dbuf_hold_level(struct dnode *dn, int level, uint64_t blkid, 248789Sahrens void *tag); 249789Sahrens int dbuf_hold_impl(struct dnode *dn, uint8_t level, uint64_t blkid, int create, 250789Sahrens void *tag, dmu_buf_impl_t **dbp); 251789Sahrens 252789Sahrens void dbuf_prefetch(struct dnode *dn, uint64_t blkid); 253789Sahrens 254789Sahrens void dbuf_add_ref(dmu_buf_impl_t *db, void *tag); 255789Sahrens uint64_t dbuf_refcount(dmu_buf_impl_t *db); 256789Sahrens 2571544Seschrock void dbuf_rele(dmu_buf_impl_t *db, void *tag); 258*10922SJeff.Bonwick@Sun.COM void dbuf_rele_and_unlock(dmu_buf_impl_t *db, void *tag); 259789Sahrens 260789Sahrens dmu_buf_impl_t *dbuf_find(struct dnode *dn, uint8_t level, uint64_t blkid); 261789Sahrens 2621544Seschrock int dbuf_read(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags); 263789Sahrens void dbuf_will_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx); 264789Sahrens void dbuf_fill_done(dmu_buf_impl_t *db, dmu_tx_t *tx); 2657872STim.Haley@Sun.COM void dmu_buf_will_not_fill(dmu_buf_t *db, dmu_tx_t *tx); 266789Sahrens void dmu_buf_will_fill(dmu_buf_t *db, dmu_tx_t *tx); 267789Sahrens void dmu_buf_fill_done(dmu_buf_t *db, dmu_tx_t *tx); 2689412SAleksandr.Guzovskiy@Sun.COM void dbuf_assign_arcbuf(dmu_buf_impl_t *db, arc_buf_t *buf, dmu_tx_t *tx); 2693547Smaybee dbuf_dirty_record_t *dbuf_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx); 270789Sahrens 2711544Seschrock void dbuf_clear(dmu_buf_impl_t *db); 272789Sahrens void dbuf_evict(dmu_buf_impl_t *db); 273789Sahrens 274789Sahrens void dbuf_setdirty(dmu_buf_impl_t *db, dmu_tx_t *tx); 2753547Smaybee void dbuf_unoverride(dbuf_dirty_record_t *dr); 2763547Smaybee void dbuf_sync_list(list_t *list, dmu_tx_t *tx); 277789Sahrens 2786992Smaybee void dbuf_free_range(struct dnode *dn, uint64_t start, uint64_t end, 279789Sahrens struct dmu_tx *); 280789Sahrens 281789Sahrens void dbuf_new_size(dmu_buf_impl_t *db, int size, dmu_tx_t *tx); 282789Sahrens 283789Sahrens void dbuf_init(void); 284789Sahrens void dbuf_fini(void); 285789Sahrens 2867237Sek110237 #define DBUF_IS_METADATA(db) \ 2877237Sek110237 ((db)->db_level > 0 || dmu_ot[(db)->db_dnode->dn_type].ot_metadata) 2887237Sek110237 2897237Sek110237 #define DBUF_GET_BUFC_TYPE(db) \ 2907237Sek110237 (DBUF_IS_METADATA(db) ? ARC_BUFC_METADATA : ARC_BUFC_DATA) 2917237Sek110237 2927237Sek110237 #define DBUF_IS_CACHEABLE(db) \ 2937237Sek110237 ((db)->db_objset->os_primary_cache == ZFS_CACHE_ALL || \ 2947237Sek110237 (DBUF_IS_METADATA(db) && \ 2957237Sek110237 ((db)->db_objset->os_primary_cache == ZFS_CACHE_METADATA))) 2967237Sek110237 2977237Sek110237 #define DBUF_IS_L2CACHEABLE(db) \ 2987237Sek110237 ((db)->db_objset->os_secondary_cache == ZFS_CACHE_ALL || \ 2997237Sek110237 (DBUF_IS_METADATA(db) && \ 3007237Sek110237 ((db)->db_objset->os_secondary_cache == ZFS_CACHE_METADATA))) 3013290Sjohansen 302789Sahrens #ifdef ZFS_DEBUG 303789Sahrens 304789Sahrens /* 305789Sahrens * There should be a ## between the string literal and fmt, to make it 306896Smaybee * clear that we're joining two strings together, but gcc does not 307896Smaybee * support that preprocessor token. 308789Sahrens */ 309789Sahrens #define dprintf_dbuf(dbuf, fmt, ...) do { \ 310789Sahrens if (zfs_flags & ZFS_DEBUG_DPRINTF) { \ 311789Sahrens char __db_buf[32]; \ 312789Sahrens uint64_t __db_obj = (dbuf)->db.db_object; \ 313789Sahrens if (__db_obj == DMU_META_DNODE_OBJECT) \ 314789Sahrens (void) strcpy(__db_buf, "mdn"); \ 315789Sahrens else \ 316789Sahrens (void) snprintf(__db_buf, sizeof (__db_buf), "%lld", \ 317789Sahrens (u_longlong_t)__db_obj); \ 318789Sahrens dprintf_ds((dbuf)->db_objset->os_dsl_dataset, \ 319789Sahrens "obj=%s lvl=%u blkid=%lld " fmt, \ 320789Sahrens __db_buf, (dbuf)->db_level, \ 321789Sahrens (u_longlong_t)(dbuf)->db_blkid, __VA_ARGS__); \ 322789Sahrens } \ 323789Sahrens _NOTE(CONSTCOND) } while (0) 324789Sahrens 325789Sahrens #define dprintf_dbuf_bp(db, bp, fmt, ...) do { \ 326896Smaybee if (zfs_flags & ZFS_DEBUG_DPRINTF) { \ 3273700Sek110237 char *__blkbuf = kmem_alloc(BP_SPRINTF_LEN, KM_SLEEP); \ 328*10922SJeff.Bonwick@Sun.COM sprintf_blkptr(__blkbuf, bp); \ 329789Sahrens dprintf_dbuf(db, fmt " %s\n", __VA_ARGS__, __blkbuf); \ 3303700Sek110237 kmem_free(__blkbuf, BP_SPRINTF_LEN); \ 3313700Sek110237 } \ 332789Sahrens _NOTE(CONSTCOND) } while (0) 333789Sahrens 334873Sek110237 #define DBUF_VERIFY(db) dbuf_verify(db) 335873Sek110237 336789Sahrens #else 337789Sahrens 338789Sahrens #define dprintf_dbuf(db, fmt, ...) 339789Sahrens #define dprintf_dbuf_bp(db, bp, fmt, ...) 340873Sek110237 #define DBUF_VERIFY(db) 341789Sahrens 342789Sahrens #endif 343789Sahrens 344789Sahrens 345789Sahrens #ifdef __cplusplus 346789Sahrens } 347789Sahrens #endif 348789Sahrens 349789Sahrens #endif /* _SYS_DBUF_H */ 350