xref: /freebsd/sys/contrib/openzfs/include/sys/zil_impl.h (revision 53a2e2635ab2d17bed1de7b4e0d782dd23ceb6ea)
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" and
45  * LWB's "lwb_lock" must be 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 enum {
104 	LWB_FLAG_SLIM =		(1<<0),	/* log block has slim format */
105 	LWB_FLAG_SLOG =		(1<<1),	/* lwb_blk is on SLOG device */
106 	LWB_FLAG_CRASHED =	(1<<2),	/* lwb is on the crash list */
107 } lwb_flag_t;
108 
109 typedef struct lwb {
110 	zilog_t		*lwb_zilog;	/* back pointer to log struct */
111 	blkptr_t	lwb_blk;	/* on disk address of this log blk */
112 	lwb_flag_t	lwb_flags;	/* extra info about this lwb */
113 	int		lwb_error;	/* log block allocation error */
114 	int		lwb_nmax;	/* max bytes in the buffer */
115 	int		lwb_nused;	/* # used bytes in buffer */
116 	int		lwb_nfilled;	/* # filled bytes in buffer */
117 	int		lwb_sz;		/* size of block and buffer */
118 	int		lwb_min_sz;	/* min size for range allocation */
119 	lwb_state_t	lwb_state;	/* the state of this lwb */
120 	char		*lwb_buf;	/* log write buffer */
121 	zio_t		*lwb_child_zio;	/* parent zio for children */
122 	zio_t		*lwb_write_zio;	/* zio for the lwb buffer */
123 	zio_t		*lwb_root_zio;	/* root zio for lwb write and flushes */
124 	hrtime_t	lwb_issued_timestamp; /* when was the lwb issued? */
125 	uint64_t	lwb_issued_txg;	/* the txg when the write is issued */
126 	uint64_t	lwb_alloc_txg;	/* the txg when lwb_blk is allocated */
127 	uint64_t	lwb_max_txg;	/* highest txg in this lwb */
128 	list_node_t	lwb_node;	/* zilog->zl_lwb_list linkage */
129 	list_node_t	lwb_issue_node;	/* linkage of lwbs ready for issue */
130 	list_t		lwb_itxs;	/* list of itx's */
131 	list_t		lwb_waiters;	/* list of zil_commit_waiter's */
132 	avl_tree_t	lwb_vdev_tree;	/* vdevs to flush after lwb write */
133 	kmutex_t	lwb_lock;	/* protects lwb_vdev_tree and size */
134 } lwb_t;
135 
136 /*
137  * ZIL commit waiter.
138  *
139  * This structure is allocated each time zil_commit() is called, and is
140  * used by zil_commit() to communicate with other parts of the ZIL, such
141  * that zil_commit() can know when it safe for it return. For more
142  * details, see the comment above zil_commit().
143  *
144  * The "zcw_lock" field is used to protect the commit waiter against
145  * concurrent access. This lock is often acquired while already holding
146  * the zilog's "zl_issuer_lock" or "zl_lock"; see the functions
147  * zil_process_commit_list() and zil_lwb_flush_vdevs_done() as examples
148  * of this. Thus, one must be careful not to acquire the
149  * "zl_issuer_lock" or "zl_lock" when already holding the "zcw_lock";
150  * e.g. see the zil_commit_waiter_timeout() function.
151  */
152 typedef struct zil_commit_waiter {
153 	kcondvar_t	zcw_cv;		/* signalled when "done" */
154 	kmutex_t	zcw_lock;	/* protects fields of this struct */
155 	list_node_t	zcw_node;	/* linkage in lwb_t:lwb_waiter list */
156 	lwb_t		*zcw_lwb;	/* back pointer to lwb when linked */
157 	boolean_t	zcw_done;	/* B_TRUE when "done", else B_FALSE */
158 	int		zcw_error;	/* result to return from zil_commit() */
159 } zil_commit_waiter_t;
160 
161 /*
162  * Intent log transaction lists
163  */
164 typedef struct itxs {
165 	list_t		i_sync_list;	/* list of synchronous itxs */
166 	avl_tree_t	i_async_tree;	/* tree of foids for async itxs */
167 } itxs_t;
168 
169 typedef struct itxg {
170 	kmutex_t	itxg_lock;	/* lock for this structure */
171 	uint64_t	itxg_txg;	/* txg for this chain */
172 	itxs_t		*itxg_itxs;	/* sync and async itxs */
173 } itxg_t;
174 
175 /* for async nodes we build up an AVL tree of lists of async itxs per file */
176 typedef struct itx_async_node {
177 	uint64_t	ia_foid;	/* file object id */
178 	list_t		ia_list;	/* list of async itxs for this foid */
179 	avl_node_t	ia_node;	/* AVL tree linkage */
180 } itx_async_node_t;
181 
182 /*
183  * Vdev flushing: during a zil_commit(), we build up an AVL tree of the vdevs
184  * we've touched so we know which ones need a write cache flush at the end.
185  */
186 typedef struct zil_vdev_node {
187 	uint64_t	zv_vdev;	/* vdev to be flushed */
188 	avl_node_t	zv_node;	/* AVL tree linkage */
189 } zil_vdev_node_t;
190 
191 #define	ZIL_BURSTS 8
192 
193 /*
194  * Stable storage intent log management structure.  One per dataset.
195  */
196 struct zilog {
197 	kmutex_t	zl_lock;	/* protects most zilog_t fields */
198 	struct dsl_pool	*zl_dmu_pool;	/* DSL pool */
199 	spa_t		*zl_spa;	/* handle for read/write log */
200 	const zil_header_t *zl_header;	/* log header buffer */
201 	objset_t	*zl_os;		/* object set we're logging */
202 	zil_get_data_t	*zl_get_data;	/* callback to get object content */
203 	lwb_t		*zl_last_lwb_opened; /* most recent lwb opened */
204 	hrtime_t	zl_last_lwb_latency; /* zio latency of last lwb done */
205 	uint64_t	zl_lr_seq;	/* on-disk log record sequence number */
206 	uint64_t	zl_commit_lr_seq; /* last committed on-disk lr seq */
207 	uint64_t	zl_destroy_txg;	/* txg of last zil_destroy() */
208 	uint64_t	zl_replayed_seq[TXG_SIZE]; /* last replayed rec seq */
209 	uint64_t	zl_replaying_seq; /* current replay seq number */
210 	uint32_t	zl_suspend;	/* log suspend count */
211 	kcondvar_t	zl_cv_suspend;	/* log suspend completion */
212 	uint8_t		zl_suspending;	/* log is currently suspending */
213 	uint8_t		zl_keep_first;	/* keep first log block in destroy */
214 	uint8_t		zl_replay;	/* replaying records while set */
215 	uint8_t		zl_stop_sync;	/* for debugging */
216 	kmutex_t	zl_issuer_lock;	/* single writer, per ZIL, at a time */
217 	uint8_t		zl_logbias;	/* latency or throughput */
218 	uint8_t		zl_sync;	/* synchronous or asynchronous */
219 	int		zl_parse_error;	/* last zil_parse() error */
220 	uint64_t	zl_parse_blk_seq; /* highest blk seq on last parse */
221 	uint64_t	zl_parse_lr_seq; /* highest lr seq on last parse */
222 	uint64_t	zl_parse_blk_count; /* number of blocks parsed */
223 	uint64_t	zl_parse_lr_count; /* number of log records parsed */
224 	itxg_t		zl_itxg[TXG_SIZE]; /* intent log txg chains */
225 	list_t		zl_itx_commit_list; /* itx list to be committed */
226 	uint64_t	zl_cur_size;	/* current burst full size */
227 	uint64_t	zl_cur_left;	/* current burst remaining size */
228 	uint64_t	zl_cur_max;	/* biggest record in current burst */
229 	list_t		zl_lwb_list;	/* in-flight log write list */
230 	list_t		zl_lwb_crash_list; /* log writes in-flight at crash */
231 	avl_tree_t	zl_bp_tree;	/* track bps during log parse */
232 	clock_t		zl_replay_time;	/* lbolt of when replay started */
233 	uint64_t	zl_replay_blks;	/* number of log blocks replayed */
234 	zil_header_t	zl_old_header;	/* debugging aid */
235 	uint_t		zl_parallel;	/* workload is multi-threaded */
236 	uint_t		zl_prev_rotor;	/* rotor for zl_prev[] */
237 	uint_t		zl_prev_opt[ZIL_BURSTS]; /* optimal block size */
238 	uint_t		zl_prev_min[ZIL_BURSTS]; /* minimal first block size */
239 	txg_node_t	zl_dirty_link;	/* protected by dp_dirty_zilogs list */
240 	uint64_t	zl_dirty_max_txg; /* highest txg used to dirty zilog */
241 
242 	kmutex_t	zl_lwb_io_lock; /* protect following members */
243 	uint64_t	zl_lwb_inflight[TXG_SIZE]; /* io issued, but not done */
244 	kcondvar_t	zl_lwb_io_cv;	/* signal when the flush is done */
245 	uint64_t	zl_lwb_max_issued_txg; /* max txg when lwb io issued */
246 
247 	/*
248 	 * Max block size for this ZIL.  Note that this can not be changed
249 	 * while the ZIL is in use because consumers (ZPL/zvol) need to take
250 	 * this into account when deciding between WR_COPIED and WR_NEED_COPY
251 	 * (see zil_max_copied_data()).
252 	 */
253 	uint64_t	zl_max_block_size;
254 
255 	/* After crash, txg to restart zil */
256 	uint64_t	zl_restart_txg;
257 
258 	/* Pointer for per dataset zil sums */
259 	zil_sums_t *zl_sums;
260 };
261 
262 typedef struct zil_bp_node {
263 	dva_t		zn_dva;
264 	avl_node_t	zn_node;
265 } zil_bp_node_t;
266 
267 #ifdef	__cplusplus
268 }
269 #endif
270 
271 #endif	/* _SYS_ZIL_IMPL_H */
272