1 // SPDX-License-Identifier: CDDL-1.0 2 /* 3 * CDDL HEADER START 4 * 5 * The contents of this file are subject to the terms of the 6 * Common Development and Distribution License (the "License"). 7 * You may not use this file except in compliance with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or https://opensource.org/licenses/CDDL-1.0. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24 * Copyright (c) 2012, 2018 by Delphix. All rights reserved. 25 */ 26 27 /* Portions Copyright 2010 Robert Milkowski */ 28 29 #ifndef _SYS_ZIL_IMPL_H 30 #define _SYS_ZIL_IMPL_H 31 32 #include <sys/zil.h> 33 #include <sys/dmu_objset.h> 34 35 #ifdef __cplusplus 36 extern "C" { 37 #endif 38 39 /* 40 * Possible states for a given lwb structure. 41 * 42 * An lwb will start out in the "new" state, and transition to the "opened" 43 * state via a call to zil_lwb_write_open() on first itx assignment. When 44 * transitioning from "new" to "opened" the zilog's "zl_issuer_lock" must be 45 * held. 46 * 47 * After the lwb is "opened", it can be assigned number of itxs and transition 48 * into the "closed" state via zil_lwb_write_close() when full or on timeout. 49 * When transitioning from "opened" to "closed" the zilog's "zl_issuer_lock" 50 * must be held. New lwb allocation also takes "zl_lock" to protect the list. 51 * 52 * After the lwb is "closed", it can transition into the "ready" state via 53 * zil_lwb_write_issue(). "zl_lock" must be held when making this transition. 54 * Since it is done by the same thread, "zl_issuer_lock" is not needed. 55 * 56 * When lwb in "ready" state receives its block pointer, it can transition to 57 * "issued". "zl_lock" must be held when making this transition. 58 * 59 * After the lwb's write zio completes, it transitions into the "write 60 * done" state via zil_lwb_write_done(); and then into the "flush done" 61 * state via zil_lwb_flush_vdevs_done(). When transitioning from 62 * "issued" to "write done", and then from "write done" to "flush done", 63 * the zilog's "zl_lock" must be held, *not* the "zl_issuer_lock". 64 * 65 * The zilog's "zl_issuer_lock" can become heavily contended in certain 66 * workloads, so we specifically avoid acquiring that lock when 67 * transitioning an lwb from "issued" to "done". This allows us to avoid 68 * having to acquire the "zl_issuer_lock" for each lwb ZIO completion, 69 * which would have added more lock contention on an already heavily 70 * contended lock. 71 * 72 * Additionally, correctness when reading an lwb's state is often 73 * achieved by exploiting the fact that these state transitions occur in 74 * this specific order; i.e. "new" to "opened" to "closed" to "ready" to 75 * "issued" to "write_done" and finally "flush_done". 76 * 77 * Thus, if an lwb is in the "new" or "opened" state, holding the 78 * "zl_issuer_lock" will prevent a concurrent thread from transitioning 79 * that lwb to the "closed" state. Likewise, if an lwb is already in the 80 * "ready" state, holding the "zl_lock" will prevent a concurrent thread 81 * from transitioning that lwb to the "issued" state. 82 */ 83 typedef enum { 84 LWB_STATE_NEW, 85 LWB_STATE_OPENED, 86 LWB_STATE_CLOSED, 87 LWB_STATE_READY, 88 LWB_STATE_ISSUED, 89 LWB_STATE_WRITE_DONE, 90 LWB_STATE_FLUSH_DONE, 91 LWB_NUM_STATES 92 } lwb_state_t; 93 94 /* 95 * Log write block (lwb) 96 * 97 * Prior to an lwb being issued to disk via zil_lwb_write_issue(), it 98 * will be protected by the zilog's "zl_issuer_lock". Basically, prior 99 * to it being issued, it will only be accessed by the thread that's 100 * holding the "zl_issuer_lock". After the lwb is issued, the zilog's 101 * "zl_lock" is used to protect the lwb against concurrent access. 102 */ 103 typedef struct lwb { 104 zilog_t *lwb_zilog; /* back pointer to log struct */ 105 blkptr_t lwb_blk; /* on disk address of this log blk */ 106 boolean_t lwb_slim; /* log block has slim format */ 107 boolean_t lwb_slog; /* lwb_blk is on SLOG device */ 108 int lwb_error; /* log block allocation error */ 109 int lwb_nmax; /* max bytes in the buffer */ 110 int lwb_nused; /* # used bytes in buffer */ 111 int lwb_nfilled; /* # filled bytes in buffer */ 112 int lwb_sz; /* size of block and buffer */ 113 lwb_state_t lwb_state; /* the state of this lwb */ 114 char *lwb_buf; /* log write buffer */ 115 zio_t *lwb_child_zio; /* parent zio for children */ 116 zio_t *lwb_write_zio; /* zio for the lwb buffer */ 117 zio_t *lwb_root_zio; /* root zio for lwb write and flushes */ 118 hrtime_t lwb_issued_timestamp; /* when was the lwb issued? */ 119 uint64_t lwb_issued_txg; /* the txg when the write is issued */ 120 uint64_t lwb_alloc_txg; /* the txg when lwb_blk is allocated */ 121 uint64_t lwb_max_txg; /* highest txg in this lwb */ 122 list_node_t lwb_node; /* zilog->zl_lwb_list linkage */ 123 list_node_t lwb_issue_node; /* linkage of lwbs ready for issue */ 124 list_t lwb_itxs; /* list of itx's */ 125 list_t lwb_waiters; /* list of zil_commit_waiter's */ 126 avl_tree_t lwb_vdev_tree; /* vdevs to flush after lwb write */ 127 kmutex_t lwb_vdev_lock; /* protects lwb_vdev_tree */ 128 } lwb_t; 129 130 /* 131 * ZIL commit waiter. 132 * 133 * This structure is allocated each time zil_commit() is called, and is 134 * used by zil_commit() to communicate with other parts of the ZIL, such 135 * that zil_commit() can know when it safe for it return. For more 136 * details, see the comment above zil_commit(). 137 * 138 * The "zcw_lock" field is used to protect the commit waiter against 139 * concurrent access. This lock is often acquired while already holding 140 * the zilog's "zl_issuer_lock" or "zl_lock"; see the functions 141 * zil_process_commit_list() and zil_lwb_flush_vdevs_done() as examples 142 * of this. Thus, one must be careful not to acquire the 143 * "zl_issuer_lock" or "zl_lock" when already holding the "zcw_lock"; 144 * e.g. see the zil_commit_waiter_timeout() function. 145 */ 146 typedef struct zil_commit_waiter { 147 kcondvar_t zcw_cv; /* signalled when "done" */ 148 kmutex_t zcw_lock; /* protects fields of this struct */ 149 list_node_t zcw_node; /* linkage in lwb_t:lwb_waiter list */ 150 lwb_t *zcw_lwb; /* back pointer to lwb when linked */ 151 boolean_t zcw_done; /* B_TRUE when "done", else B_FALSE */ 152 int zcw_zio_error; /* contains the zio io_error value */ 153 } zil_commit_waiter_t; 154 155 /* 156 * Intent log transaction lists 157 */ 158 typedef struct itxs { 159 list_t i_sync_list; /* list of synchronous itxs */ 160 avl_tree_t i_async_tree; /* tree of foids for async itxs */ 161 } itxs_t; 162 163 typedef struct itxg { 164 kmutex_t itxg_lock; /* lock for this structure */ 165 uint64_t itxg_txg; /* txg for this chain */ 166 itxs_t *itxg_itxs; /* sync and async itxs */ 167 } itxg_t; 168 169 /* for async nodes we build up an AVL tree of lists of async itxs per file */ 170 typedef struct itx_async_node { 171 uint64_t ia_foid; /* file object id */ 172 list_t ia_list; /* list of async itxs for this foid */ 173 avl_node_t ia_node; /* AVL tree linkage */ 174 } itx_async_node_t; 175 176 /* 177 * Vdev flushing: during a zil_commit(), we build up an AVL tree of the vdevs 178 * we've touched so we know which ones need a write cache flush at the end. 179 */ 180 typedef struct zil_vdev_node { 181 uint64_t zv_vdev; /* vdev to be flushed */ 182 avl_node_t zv_node; /* AVL tree linkage */ 183 } zil_vdev_node_t; 184 185 #define ZIL_BURSTS 8 186 187 /* 188 * Stable storage intent log management structure. One per dataset. 189 */ 190 struct zilog { 191 kmutex_t zl_lock; /* protects most zilog_t fields */ 192 struct dsl_pool *zl_dmu_pool; /* DSL pool */ 193 spa_t *zl_spa; /* handle for read/write log */ 194 const zil_header_t *zl_header; /* log header buffer */ 195 objset_t *zl_os; /* object set we're logging */ 196 zil_get_data_t *zl_get_data; /* callback to get object content */ 197 lwb_t *zl_last_lwb_opened; /* most recent lwb opened */ 198 hrtime_t zl_last_lwb_latency; /* zio latency of last lwb done */ 199 uint64_t zl_lr_seq; /* on-disk log record sequence number */ 200 uint64_t zl_commit_lr_seq; /* last committed on-disk lr seq */ 201 uint64_t zl_destroy_txg; /* txg of last zil_destroy() */ 202 uint64_t zl_replayed_seq[TXG_SIZE]; /* last replayed rec seq */ 203 uint64_t zl_replaying_seq; /* current replay seq number */ 204 uint32_t zl_suspend; /* log suspend count */ 205 kcondvar_t zl_cv_suspend; /* log suspend completion */ 206 uint8_t zl_suspending; /* log is currently suspending */ 207 uint8_t zl_keep_first; /* keep first log block in destroy */ 208 uint8_t zl_replay; /* replaying records while set */ 209 uint8_t zl_stop_sync; /* for debugging */ 210 kmutex_t zl_issuer_lock; /* single writer, per ZIL, at a time */ 211 uint8_t zl_logbias; /* latency or throughput */ 212 uint8_t zl_sync; /* synchronous or asynchronous */ 213 int zl_parse_error; /* last zil_parse() error */ 214 uint64_t zl_parse_blk_seq; /* highest blk seq on last parse */ 215 uint64_t zl_parse_lr_seq; /* highest lr seq on last parse */ 216 uint64_t zl_parse_blk_count; /* number of blocks parsed */ 217 uint64_t zl_parse_lr_count; /* number of log records parsed */ 218 itxg_t zl_itxg[TXG_SIZE]; /* intent log txg chains */ 219 list_t zl_itx_commit_list; /* itx list to be committed */ 220 uint64_t zl_cur_size; /* current burst full size */ 221 uint64_t zl_cur_left; /* current burst remaining size */ 222 uint64_t zl_cur_max; /* biggest record in current burst */ 223 list_t zl_lwb_list; /* in-flight log write list */ 224 avl_tree_t zl_bp_tree; /* track bps during log parse */ 225 clock_t zl_replay_time; /* lbolt of when replay started */ 226 uint64_t zl_replay_blks; /* number of log blocks replayed */ 227 zil_header_t zl_old_header; /* debugging aid */ 228 uint_t zl_parallel; /* workload is multi-threaded */ 229 uint_t zl_prev_rotor; /* rotor for zl_prev[] */ 230 uint_t zl_prev_opt[ZIL_BURSTS]; /* optimal block size */ 231 uint_t zl_prev_min[ZIL_BURSTS]; /* minimal first block size */ 232 txg_node_t zl_dirty_link; /* protected by dp_dirty_zilogs list */ 233 uint64_t zl_dirty_max_txg; /* highest txg used to dirty zilog */ 234 235 kmutex_t zl_lwb_io_lock; /* protect following members */ 236 uint64_t zl_lwb_inflight[TXG_SIZE]; /* io issued, but not done */ 237 kcondvar_t zl_lwb_io_cv; /* signal when the flush is done */ 238 uint64_t zl_lwb_max_issued_txg; /* max txg when lwb io issued */ 239 240 /* 241 * Max block size for this ZIL. Note that this can not be changed 242 * while the ZIL is in use because consumers (ZPL/zvol) need to take 243 * this into account when deciding between WR_COPIED and WR_NEED_COPY 244 * (see zil_max_copied_data()). 245 */ 246 uint64_t zl_max_block_size; 247 248 /* Pointer for per dataset zil sums */ 249 zil_sums_t *zl_sums; 250 }; 251 252 typedef struct zil_bp_node { 253 dva_t zn_dva; 254 avl_node_t zn_node; 255 } zil_bp_node_t; 256 257 #ifdef __cplusplus 258 } 259 #endif 260 261 #endif /* _SYS_ZIL_IMPL_H */ 262