Home | History | Annotate | Line # | Download | only in sys
      1  1.1   haad /*
      2  1.1   haad  * CDDL HEADER START
      3  1.1   haad  *
      4  1.1   haad  * The contents of this file are subject to the terms of the
      5  1.1   haad  * Common Development and Distribution License (the "License").
      6  1.1   haad  * You may not use this file except in compliance with the License.
      7  1.1   haad  *
      8  1.1   haad  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9  1.1   haad  * or http://www.opensolaris.org/os/licensing.
     10  1.1   haad  * See the License for the specific language governing permissions
     11  1.1   haad  * and limitations under the License.
     12  1.1   haad  *
     13  1.1   haad  * When distributing Covered Code, include this CDDL HEADER in each
     14  1.1   haad  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15  1.1   haad  * If applicable, add the following below this CDDL HEADER, with the
     16  1.1   haad  * fields enclosed by brackets "[]" replaced with your own identifying
     17  1.1   haad  * information: Portions Copyright [yyyy] [name of copyright owner]
     18  1.1   haad  *
     19  1.1   haad  * CDDL HEADER END
     20  1.1   haad  */
     21  1.1   haad /*
     22  1.3    chs  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
     23  1.3    chs  * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
     24  1.3    chs  * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
     25  1.3    chs  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
     26  1.1   haad  */
     27  1.1   haad 
     28  1.1   haad #ifndef	_SYS_DBUF_H
     29  1.1   haad #define	_SYS_DBUF_H
     30  1.1   haad 
     31  1.1   haad #include <sys/dmu.h>
     32  1.1   haad #include <sys/spa.h>
     33  1.1   haad #include <sys/txg.h>
     34  1.1   haad #include <sys/zio.h>
     35  1.1   haad #include <sys/arc.h>
     36  1.1   haad #include <sys/zfs_context.h>
     37  1.1   haad #include <sys/refcount.h>
     38  1.3    chs #include <sys/zrlock.h>
     39  1.3    chs #include <sys/multilist.h>
     40  1.1   haad 
     41  1.1   haad #ifdef	__cplusplus
     42  1.1   haad extern "C" {
     43  1.1   haad #endif
     44  1.1   haad 
     45  1.1   haad #define	IN_DMU_SYNC 2
     46  1.1   haad 
     47  1.1   haad /*
     48  1.1   haad  * define flags for dbuf_read
     49  1.1   haad  */
     50  1.1   haad 
     51  1.1   haad #define	DB_RF_MUST_SUCCEED	(1 << 0)
     52  1.1   haad #define	DB_RF_CANFAIL		(1 << 1)
     53  1.1   haad #define	DB_RF_HAVESTRUCT	(1 << 2)
     54  1.1   haad #define	DB_RF_NOPREFETCH	(1 << 3)
     55  1.1   haad #define	DB_RF_NEVERWAIT		(1 << 4)
     56  1.1   haad #define	DB_RF_CACHED		(1 << 5)
     57  1.1   haad 
     58  1.1   haad /*
     59  1.1   haad  * The simplified state transition diagram for dbufs looks like:
     60  1.1   haad  *
     61  1.1   haad  *		+----> READ ----+
     62  1.1   haad  *		|		|
     63  1.1   haad  *		|		V
     64  1.1   haad  *  (alloc)-->UNCACHED	     CACHED-->EVICTING-->(free)
     65  1.1   haad  *		|		^	 ^
     66  1.1   haad  *		|		|	 |
     67  1.1   haad  *		+----> FILL ----+	 |
     68  1.1   haad  *		|			 |
     69  1.1   haad  *		|			 |
     70  1.1   haad  *		+--------> NOFILL -------+
     71  1.3    chs  *
     72  1.3    chs  * DB_SEARCH is an invalid state for a dbuf. It is used by dbuf_free_range
     73  1.3    chs  * to find all dbufs in a range of a dnode and must be less than any other
     74  1.3    chs  * dbuf_states_t (see comment on dn_dbufs in dnode.h).
     75  1.1   haad  */
     76  1.1   haad typedef enum dbuf_states {
     77  1.3    chs 	DB_SEARCH = -1,
     78  1.1   haad 	DB_UNCACHED,
     79  1.1   haad 	DB_FILL,
     80  1.1   haad 	DB_NOFILL,
     81  1.1   haad 	DB_READ,
     82  1.1   haad 	DB_CACHED,
     83  1.1   haad 	DB_EVICTING
     84  1.1   haad } dbuf_states_t;
     85  1.1   haad 
     86  1.1   haad struct dnode;
     87  1.1   haad struct dmu_tx;
     88  1.1   haad 
     89  1.1   haad /*
     90  1.1   haad  * level = 0 means the user data
     91  1.1   haad  * level = 1 means the single indirect block
     92  1.1   haad  * etc.
     93  1.1   haad  */
     94  1.1   haad 
     95  1.1   haad struct dmu_buf_impl;
     96  1.1   haad 
     97  1.1   haad typedef enum override_states {
     98  1.1   haad 	DR_NOT_OVERRIDDEN,
     99  1.1   haad 	DR_IN_DMU_SYNC,
    100  1.1   haad 	DR_OVERRIDDEN
    101  1.1   haad } override_states_t;
    102  1.1   haad 
    103  1.1   haad typedef struct dbuf_dirty_record {
    104  1.1   haad 	/* link on our parents dirty list */
    105  1.1   haad 	list_node_t dr_dirty_node;
    106  1.1   haad 
    107  1.1   haad 	/* transaction group this data will sync in */
    108  1.1   haad 	uint64_t dr_txg;
    109  1.1   haad 
    110  1.1   haad 	/* zio of outstanding write IO */
    111  1.1   haad 	zio_t *dr_zio;
    112  1.1   haad 
    113  1.1   haad 	/* pointer back to our dbuf */
    114  1.1   haad 	struct dmu_buf_impl *dr_dbuf;
    115  1.1   haad 
    116  1.1   haad 	/* pointer to next dirty record */
    117  1.1   haad 	struct dbuf_dirty_record *dr_next;
    118  1.1   haad 
    119  1.1   haad 	/* pointer to parent dirty record */
    120  1.1   haad 	struct dbuf_dirty_record *dr_parent;
    121  1.1   haad 
    122  1.3    chs 	/* How much space was changed to dsl_pool_dirty_space() for this? */
    123  1.3    chs 	unsigned int dr_accounted;
    124  1.3    chs 
    125  1.3    chs 	/* A copy of the bp that points to us */
    126  1.3    chs 	blkptr_t dr_bp_copy;
    127  1.3    chs 
    128  1.1   haad 	union dirty_types {
    129  1.1   haad 		struct dirty_indirect {
    130  1.1   haad 
    131  1.1   haad 			/* protect access to list */
    132  1.1   haad 			kmutex_t dr_mtx;
    133  1.1   haad 
    134  1.1   haad 			/* Our list of dirty children */
    135  1.1   haad 			list_t dr_children;
    136  1.1   haad 		} di;
    137  1.1   haad 		struct dirty_leaf {
    138  1.1   haad 
    139  1.1   haad 			/*
    140  1.1   haad 			 * dr_data is set when we dirty the buffer
    141  1.1   haad 			 * so that we can retain the pointer even if it
    142  1.1   haad 			 * gets COW'd in a subsequent transaction group.
    143  1.1   haad 			 */
    144  1.1   haad 			arc_buf_t *dr_data;
    145  1.1   haad 			blkptr_t dr_overridden_by;
    146  1.1   haad 			override_states_t dr_override_state;
    147  1.2  ozaki 			uint8_t dr_copies;
    148  1.3    chs 			boolean_t dr_nopwrite;
    149  1.1   haad 		} dl;
    150  1.1   haad 	} dt;
    151  1.1   haad } dbuf_dirty_record_t;
    152  1.1   haad 
    153  1.1   haad typedef struct dmu_buf_impl {
    154  1.1   haad 	/*
    155  1.1   haad 	 * The following members are immutable, with the exception of
    156  1.1   haad 	 * db.db_data, which is protected by db_mtx.
    157  1.1   haad 	 */
    158  1.1   haad 
    159  1.1   haad 	/* the publicly visible structure */
    160  1.1   haad 	dmu_buf_t db;
    161  1.1   haad 
    162  1.1   haad 	/* the objset we belong to */
    163  1.2  ozaki 	struct objset *db_objset;
    164  1.1   haad 
    165  1.1   haad 	/*
    166  1.3    chs 	 * handle to safely access the dnode we belong to (NULL when evicted)
    167  1.1   haad 	 */
    168  1.3    chs 	struct dnode_handle *db_dnode_handle;
    169  1.1   haad 
    170  1.1   haad 	/*
    171  1.1   haad 	 * our parent buffer; if the dnode points to us directly,
    172  1.3    chs 	 * db_parent == db_dnode_handle->dnh_dnode->dn_dbuf
    173  1.1   haad 	 * only accessed by sync thread ???
    174  1.1   haad 	 * (NULL when evicted)
    175  1.3    chs 	 * May change from NULL to non-NULL under the protection of db_mtx
    176  1.3    chs 	 * (see dbuf_check_blkptr())
    177  1.1   haad 	 */
    178  1.1   haad 	struct dmu_buf_impl *db_parent;
    179  1.1   haad 
    180  1.1   haad 	/*
    181  1.1   haad 	 * link for hash table of all dmu_buf_impl_t's
    182  1.1   haad 	 */
    183  1.1   haad 	struct dmu_buf_impl *db_hash_next;
    184  1.1   haad 
    185  1.1   haad 	/* our block number */
    186  1.1   haad 	uint64_t db_blkid;
    187  1.1   haad 
    188  1.1   haad 	/*
    189  1.1   haad 	 * Pointer to the blkptr_t which points to us. May be NULL if we
    190  1.1   haad 	 * don't have one yet. (NULL when evicted)
    191  1.1   haad 	 */
    192  1.1   haad 	blkptr_t *db_blkptr;
    193  1.1   haad 
    194  1.1   haad 	/*
    195  1.1   haad 	 * Our indirection level.  Data buffers have db_level==0.
    196  1.1   haad 	 * Indirect buffers which point to data buffers have
    197  1.1   haad 	 * db_level==1. etc.  Buffers which contain dnodes have
    198  1.1   haad 	 * db_level==0, since the dnodes are stored in a file.
    199  1.1   haad 	 */
    200  1.1   haad 	uint8_t db_level;
    201  1.1   haad 
    202  1.1   haad 	/* db_mtx protects the members below */
    203  1.1   haad 	kmutex_t db_mtx;
    204  1.1   haad 
    205  1.1   haad 	/*
    206  1.1   haad 	 * Current state of the buffer
    207  1.1   haad 	 */
    208  1.1   haad 	dbuf_states_t db_state;
    209  1.1   haad 
    210  1.1   haad 	/*
    211  1.1   haad 	 * Refcount accessed by dmu_buf_{hold,rele}.
    212  1.1   haad 	 * If nonzero, the buffer can't be destroyed.
    213  1.1   haad 	 * Protected by db_mtx.
    214  1.1   haad 	 */
    215  1.1   haad 	refcount_t db_holds;
    216  1.1   haad 
    217  1.1   haad 	/* buffer holding our data */
    218  1.1   haad 	arc_buf_t *db_buf;
    219  1.1   haad 
    220  1.1   haad 	kcondvar_t db_changed;
    221  1.1   haad 	dbuf_dirty_record_t *db_data_pending;
    222  1.1   haad 
    223  1.1   haad 	/* pointer to most recent dirty record for this buffer */
    224  1.1   haad 	dbuf_dirty_record_t *db_last_dirty;
    225  1.1   haad 
    226  1.1   haad 	/*
    227  1.1   haad 	 * Our link on the owner dnodes's dn_dbufs list.
    228  1.1   haad 	 * Protected by its dn_dbufs_mtx.
    229  1.1   haad 	 */
    230  1.3    chs 	avl_node_t db_link;
    231  1.3    chs 
    232  1.3    chs 	/*
    233  1.3    chs 	 * Link in dbuf_cache.
    234  1.3    chs 	 */
    235  1.3    chs 	multilist_node_t db_cache_link;
    236  1.1   haad 
    237  1.1   haad 	/* Data which is unique to data (leaf) blocks: */
    238  1.1   haad 
    239  1.3    chs 	/* User callback information. */
    240  1.3    chs 	dmu_buf_user_t *db_user;
    241  1.3    chs 
    242  1.3    chs 	/*
    243  1.3    chs 	 * Evict user data as soon as the dirty and reference
    244  1.3    chs 	 * counts are equal.
    245  1.3    chs 	 */
    246  1.3    chs 	uint8_t db_user_immediate_evict;
    247  1.1   haad 
    248  1.3    chs 	/*
    249  1.3    chs 	 * This block was freed while a read or write was
    250  1.3    chs 	 * active.
    251  1.3    chs 	 */
    252  1.1   haad 	uint8_t db_freed_in_flight;
    253  1.1   haad 
    254  1.3    chs 	/*
    255  1.3    chs 	 * dnode_evict_dbufs() or dnode_evict_bonus() tried to
    256  1.3    chs 	 * evict this dbuf, but couldn't due to outstanding
    257  1.3    chs 	 * references.  Evict once the refcount drops to 0.
    258  1.3    chs 	 */
    259  1.3    chs 	uint8_t db_pending_evict;
    260  1.3    chs 
    261  1.1   haad 	uint8_t db_dirtycnt;
    262  1.1   haad } dmu_buf_impl_t;
    263  1.1   haad 
    264  1.1   haad /* Note: the dbuf hash table is exposed only for the mdb module */
    265  1.1   haad #define	DBUF_MUTEXES 256
    266  1.1   haad #define	DBUF_HASH_MUTEX(h, idx) (&(h)->hash_mutexes[(idx) & (DBUF_MUTEXES-1)])
    267  1.1   haad typedef struct dbuf_hash_table {
    268  1.1   haad 	uint64_t hash_table_mask;
    269  1.1   haad 	dmu_buf_impl_t **hash_table;
    270  1.1   haad 	kmutex_t hash_mutexes[DBUF_MUTEXES];
    271  1.1   haad } dbuf_hash_table_t;
    272  1.1   haad 
    273  1.3    chs uint64_t dbuf_whichblock(struct dnode *di, int64_t level, uint64_t offset);
    274  1.1   haad 
    275  1.1   haad dmu_buf_impl_t *dbuf_create_tlib(struct dnode *dn, char *data);
    276  1.1   haad void dbuf_create_bonus(struct dnode *dn);
    277  1.3    chs int dbuf_spill_set_blksz(dmu_buf_t *db, uint64_t blksz, dmu_tx_t *tx);
    278  1.3    chs void dbuf_spill_hold(struct dnode *dn, dmu_buf_impl_t **dbp, void *tag);
    279  1.3    chs 
    280  1.3    chs void dbuf_rm_spill(struct dnode *dn, dmu_tx_t *tx);
    281  1.1   haad 
    282  1.1   haad dmu_buf_impl_t *dbuf_hold(struct dnode *dn, uint64_t blkid, void *tag);
    283  1.1   haad dmu_buf_impl_t *dbuf_hold_level(struct dnode *dn, int level, uint64_t blkid,
    284  1.1   haad     void *tag);
    285  1.3    chs int dbuf_hold_impl(struct dnode *dn, uint8_t level, uint64_t blkid,
    286  1.3    chs     boolean_t fail_sparse, boolean_t fail_uncached,
    287  1.1   haad     void *tag, dmu_buf_impl_t **dbp);
    288  1.1   haad 
    289  1.3    chs void dbuf_prefetch(struct dnode *dn, int64_t level, uint64_t blkid,
    290  1.3    chs     zio_priority_t prio, arc_flags_t aflags);
    291  1.1   haad 
    292  1.1   haad void dbuf_add_ref(dmu_buf_impl_t *db, void *tag);
    293  1.3    chs boolean_t dbuf_try_add_ref(dmu_buf_t *db, objset_t *os, uint64_t obj,
    294  1.3    chs     uint64_t blkid, void *tag);
    295  1.1   haad uint64_t dbuf_refcount(dmu_buf_impl_t *db);
    296  1.1   haad 
    297  1.1   haad void dbuf_rele(dmu_buf_impl_t *db, void *tag);
    298  1.2  ozaki void dbuf_rele_and_unlock(dmu_buf_impl_t *db, void *tag);
    299  1.1   haad 
    300  1.3    chs dmu_buf_impl_t *dbuf_find(struct objset *os, uint64_t object, uint8_t level,
    301  1.3    chs     uint64_t blkid);
    302  1.1   haad 
    303  1.1   haad int dbuf_read(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags);
    304  1.1   haad void dmu_buf_will_not_fill(dmu_buf_t *db, dmu_tx_t *tx);
    305  1.1   haad void dmu_buf_will_fill(dmu_buf_t *db, dmu_tx_t *tx);
    306  1.1   haad void dmu_buf_fill_done(dmu_buf_t *db, dmu_tx_t *tx);
    307  1.2  ozaki void dbuf_assign_arcbuf(dmu_buf_impl_t *db, arc_buf_t *buf, dmu_tx_t *tx);
    308  1.1   haad dbuf_dirty_record_t *dbuf_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx);
    309  1.2  ozaki arc_buf_t *dbuf_loan_arcbuf(dmu_buf_impl_t *db);
    310  1.3    chs void dmu_buf_write_embedded(dmu_buf_t *dbuf, void *data,
    311  1.3    chs     bp_embedded_type_t etype, enum zio_compress comp,
    312  1.3    chs     int uncompressed_size, int compressed_size, int byteorder, dmu_tx_t *tx);
    313  1.1   haad 
    314  1.3    chs void dbuf_destroy(dmu_buf_impl_t *db);
    315  1.1   haad 
    316  1.1   haad void dbuf_setdirty(dmu_buf_impl_t *db, dmu_tx_t *tx);
    317  1.1   haad void dbuf_unoverride(dbuf_dirty_record_t *dr);
    318  1.3    chs void dbuf_sync_list(list_t *list, int level, dmu_tx_t *tx);
    319  1.3    chs void dbuf_release_bp(dmu_buf_impl_t *db);
    320  1.1   haad 
    321  1.1   haad void dbuf_free_range(struct dnode *dn, uint64_t start, uint64_t end,
    322  1.1   haad     struct dmu_tx *);
    323  1.1   haad 
    324  1.1   haad void dbuf_new_size(dmu_buf_impl_t *db, int size, dmu_tx_t *tx);
    325  1.1   haad 
    326  1.3    chs #define	DB_DNODE(_db)		((_db)->db_dnode_handle->dnh_dnode)
    327  1.3    chs #define	DB_DNODE_LOCK(_db)	((_db)->db_dnode_handle->dnh_zrlock)
    328  1.3    chs #define	DB_DNODE_ENTER(_db)	(zrl_add(&DB_DNODE_LOCK(_db)))
    329  1.3    chs #define	DB_DNODE_EXIT(_db)	(zrl_remove(&DB_DNODE_LOCK(_db)))
    330  1.3    chs #define	DB_DNODE_HELD(_db)	(!zrl_is_zero(&DB_DNODE_LOCK(_db)))
    331  1.3    chs 
    332  1.1   haad void dbuf_init(void);
    333  1.1   haad void dbuf_fini(void);
    334  1.1   haad 
    335  1.3    chs boolean_t dbuf_is_metadata(dmu_buf_impl_t *db);
    336  1.1   haad 
    337  1.3    chs #define	DBUF_GET_BUFC_TYPE(_db)	\
    338  1.3    chs 	(dbuf_is_metadata(_db) ? ARC_BUFC_METADATA : ARC_BUFC_DATA)
    339  1.1   haad 
    340  1.3    chs #define	DBUF_IS_CACHEABLE(_db)						\
    341  1.3    chs 	((_db)->db_objset->os_primary_cache == ZFS_CACHE_ALL ||		\
    342  1.3    chs 	(dbuf_is_metadata(_db) &&					\
    343  1.3    chs 	((_db)->db_objset->os_primary_cache == ZFS_CACHE_METADATA)))
    344  1.3    chs 
    345  1.3    chs #define	DBUF_IS_L2CACHEABLE(_db)					\
    346  1.3    chs 	((_db)->db_objset->os_secondary_cache == ZFS_CACHE_ALL ||	\
    347  1.3    chs 	(dbuf_is_metadata(_db) &&					\
    348  1.3    chs 	((_db)->db_objset->os_secondary_cache == ZFS_CACHE_METADATA)))
    349  1.1   haad 
    350  1.1   haad #ifdef ZFS_DEBUG
    351  1.1   haad 
    352  1.1   haad /*
    353  1.1   haad  * There should be a ## between the string literal and fmt, to make it
    354  1.1   haad  * clear that we're joining two strings together, but gcc does not
    355  1.1   haad  * support that preprocessor token.
    356  1.1   haad  */
    357  1.1   haad #define	dprintf_dbuf(dbuf, fmt, ...) do { \
    358  1.1   haad 	if (zfs_flags & ZFS_DEBUG_DPRINTF) { \
    359  1.1   haad 	char __db_buf[32]; \
    360  1.1   haad 	uint64_t __db_obj = (dbuf)->db.db_object; \
    361  1.1   haad 	if (__db_obj == DMU_META_DNODE_OBJECT) \
    362  1.1   haad 		(void) strcpy(__db_buf, "mdn"); \
    363  1.1   haad 	else \
    364  1.1   haad 		(void) snprintf(__db_buf, sizeof (__db_buf), "%lld", \
    365  1.1   haad 		    (u_longlong_t)__db_obj); \
    366  1.1   haad 	dprintf_ds((dbuf)->db_objset->os_dsl_dataset, \
    367  1.1   haad 	    "obj=%s lvl=%u blkid=%lld " fmt, \
    368  1.1   haad 	    __db_buf, (dbuf)->db_level, \
    369  1.1   haad 	    (u_longlong_t)(dbuf)->db_blkid, __VA_ARGS__); \
    370  1.1   haad 	} \
    371  1.1   haad _NOTE(CONSTCOND) } while (0)
    372  1.1   haad 
    373  1.1   haad #define	dprintf_dbuf_bp(db, bp, fmt, ...) do {			\
    374  1.1   haad 	if (zfs_flags & ZFS_DEBUG_DPRINTF) {			\
    375  1.1   haad 	char *__blkbuf = kmem_alloc(BP_SPRINTF_LEN, KM_SLEEP);	\
    376  1.3    chs 	snprintf_blkptr(__blkbuf, BP_SPRINTF_LEN, bp);		\
    377  1.1   haad 	dprintf_dbuf(db, fmt " %s\n", __VA_ARGS__, __blkbuf);	\
    378  1.1   haad 	kmem_free(__blkbuf, BP_SPRINTF_LEN);			\
    379  1.3    chs 	}							\
    380  1.1   haad _NOTE(CONSTCOND) } while (0)
    381  1.1   haad 
    382  1.1   haad #define	DBUF_VERIFY(db)	dbuf_verify(db)
    383  1.1   haad 
    384  1.1   haad #else
    385  1.1   haad 
    386  1.1   haad #define	dprintf_dbuf(db, fmt, ...)
    387  1.1   haad #define	dprintf_dbuf_bp(db, bp, fmt, ...)
    388  1.1   haad #define	DBUF_VERIFY(db)
    389  1.1   haad 
    390  1.1   haad #endif
    391  1.1   haad 
    392  1.1   haad 
    393  1.1   haad #ifdef	__cplusplus
    394  1.1   haad }
    395  1.1   haad #endif
    396  1.1   haad 
    397  1.1   haad #endif /* _SYS_DBUF_H */
    398