xref: /illumos-gate/usr/src/uts/common/fs/zfs/sys/zil_impl.h (revision cab3a55e)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5d80c45e0Sbonwick  * Common Development and Distribution License (the "License").
6d80c45e0Sbonwick  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
2255da60b9SMark J Musante  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
231271e4b1SPrakash Surya  * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
24c3d26abcSMatthew Ahrens  * Copyright (c) 2014 Integros [integros.com]
25fa9e4066Sahrens  */
26fa9e4066Sahrens 
2755da60b9SMark J Musante /* Portions Copyright 2010 Robert Milkowski */
2855da60b9SMark J Musante 
29fa9e4066Sahrens #ifndef	_SYS_ZIL_IMPL_H
30fa9e4066Sahrens #define	_SYS_ZIL_IMPL_H
31fa9e4066Sahrens 
32fa9e4066Sahrens #include <sys/zil.h>
33fa9e4066Sahrens #include <sys/dmu_objset.h>
34fa9e4066Sahrens 
35fa9e4066Sahrens #ifdef	__cplusplus
36fa9e4066Sahrens extern "C" {
37fa9e4066Sahrens #endif
38fa9e4066Sahrens 
39fa9e4066Sahrens /*
4094ddd090SPrakash Surya  * Possbile states for a given lwb structure.
4194ddd090SPrakash Surya  *
4294ddd090SPrakash Surya  * An lwb will start out in the "closed" state, and then transition to
4394ddd090SPrakash Surya  * the "opened" state via a call to zil_lwb_write_open(). When
4494ddd090SPrakash Surya  * transitioning from "closed" to "opened" the zilog's "zl_issuer_lock"
4594ddd090SPrakash Surya  * must be held.
4694ddd090SPrakash Surya  *
4794ddd090SPrakash Surya  * After the lwb is "opened", it can transition into the "issued" state
4894ddd090SPrakash Surya  * via zil_lwb_write_issue(). Again, the zilog's "zl_issuer_lock" must
4994ddd090SPrakash Surya  * be held when making this transition.
5094ddd090SPrakash Surya  *
51*cab3a55eSPrakash Surya  * After the lwb's write zio completes, it transitions into the "write
52*cab3a55eSPrakash Surya  * done" state via zil_lwb_write_done(); and then into the "flush done"
53*cab3a55eSPrakash Surya  * state via zil_lwb_flush_vdevs_done(). When transitioning from
54*cab3a55eSPrakash Surya  * "issued" to "write done", and then from "write done" to "flush done",
55*cab3a55eSPrakash Surya  * the zilog's "zl_lock" must be held, *not* the "zl_issuer_lock".
5694ddd090SPrakash Surya  *
5794ddd090SPrakash Surya  * The zilog's "zl_issuer_lock" can become heavily contended in certain
5894ddd090SPrakash Surya  * workloads, so we specifically avoid acquiring that lock when
5994ddd090SPrakash Surya  * transitioning an lwb from "issued" to "done". This allows us to avoid
6094ddd090SPrakash Surya  * having to acquire the "zl_issuer_lock" for each lwb ZIO completion,
6194ddd090SPrakash Surya  * which would have added more lock contention on an already heavily
6294ddd090SPrakash Surya  * contended lock.
6394ddd090SPrakash Surya  *
6494ddd090SPrakash Surya  * Additionally, correctness when reading an lwb's state is often
6594ddd090SPrakash Surya  * acheived by exploiting the fact that these state transitions occur in
6694ddd090SPrakash Surya  * this specific order; i.e. "closed" to "opened" to "issued" to "done".
6794ddd090SPrakash Surya  *
6894ddd090SPrakash Surya  * Thus, if an lwb is in the "closed" or "opened" state, holding the
6994ddd090SPrakash Surya  * "zl_issuer_lock" will prevent a concurrent thread from transitioning
7094ddd090SPrakash Surya  * that lwb to the "issued" state. Likewise, if an lwb is already in the
7194ddd090SPrakash Surya  * "issued" state, holding the "zl_lock" will prevent a concurrent
72*cab3a55eSPrakash Surya  * thread from transitioning that lwb to the "write done" state.
731271e4b1SPrakash Surya  */
741271e4b1SPrakash Surya typedef enum {
751271e4b1SPrakash Surya     LWB_STATE_CLOSED,
761271e4b1SPrakash Surya     LWB_STATE_OPENED,
771271e4b1SPrakash Surya     LWB_STATE_ISSUED,
78*cab3a55eSPrakash Surya     LWB_STATE_WRITE_DONE,
79*cab3a55eSPrakash Surya     LWB_STATE_FLUSH_DONE,
801271e4b1SPrakash Surya     LWB_NUM_STATES
811271e4b1SPrakash Surya } lwb_state_t;
821271e4b1SPrakash Surya 
831271e4b1SPrakash Surya /*
841271e4b1SPrakash Surya  * Log write block (lwb)
851271e4b1SPrakash Surya  *
861271e4b1SPrakash Surya  * Prior to an lwb being issued to disk via zil_lwb_write_issue(), it
87cf07d3daSPrakash Surya  * will be protected by the zilog's "zl_issuer_lock". Basically, prior
881271e4b1SPrakash Surya  * to it being issued, it will only be accessed by the thread that's
89cf07d3daSPrakash Surya  * holding the "zl_issuer_lock". After the lwb is issued, the zilog's
901271e4b1SPrakash Surya  * "zl_lock" is used to protect the lwb against concurrent access.
91fa9e4066Sahrens  */
92fa9e4066Sahrens typedef struct lwb {
93fa9e4066Sahrens 	zilog_t		*lwb_zilog;	/* back pointer to log struct */
94fa9e4066Sahrens 	blkptr_t	lwb_blk;	/* on disk address of this log blk */
95c5ee4681SAlexander Motin 	boolean_t	lwb_slog;	/* lwb_blk is on SLOG device */
96fa9e4066Sahrens 	int		lwb_nused;	/* # used bytes in buffer */
97fa9e4066Sahrens 	int		lwb_sz;		/* size of block and buffer */
981271e4b1SPrakash Surya 	lwb_state_t	lwb_state;	/* the state of this lwb */
99fa9e4066Sahrens 	char		*lwb_buf;	/* log write buffer */
1001271e4b1SPrakash Surya 	zio_t		*lwb_write_zio;	/* zio for the lwb buffer */
1011271e4b1SPrakash Surya 	zio_t		*lwb_root_zio;	/* root zio for lwb write and flushes */
102b24ab676SJeff Bonwick 	dmu_tx_t	*lwb_tx;	/* tx for log block allocation */
103fa9e4066Sahrens 	uint64_t	lwb_max_txg;	/* highest txg in this lwb */
104fa9e4066Sahrens 	list_node_t	lwb_node;	/* zilog->zl_lwb_list linkage */
1051271e4b1SPrakash Surya 	list_t		lwb_waiters;	/* list of zil_commit_waiter's */
1061271e4b1SPrakash Surya 	avl_tree_t	lwb_vdev_tree;	/* vdevs to flush after lwb write */
1071271e4b1SPrakash Surya 	kmutex_t	lwb_vdev_lock;	/* protects lwb_vdev_tree */
1081271e4b1SPrakash Surya 	hrtime_t	lwb_issued_timestamp; /* when was the lwb issued? */
109fa9e4066Sahrens } lwb_t;
110fa9e4066Sahrens 
1111271e4b1SPrakash Surya /*
1121271e4b1SPrakash Surya  * ZIL commit waiter.
1131271e4b1SPrakash Surya  *
1141271e4b1SPrakash Surya  * This structure is allocated each time zil_commit() is called, and is
1151271e4b1SPrakash Surya  * used by zil_commit() to communicate with other parts of the ZIL, such
1161271e4b1SPrakash Surya  * that zil_commit() can know when it safe for it return. For more
1171271e4b1SPrakash Surya  * details, see the comment above zil_commit().
1181271e4b1SPrakash Surya  *
1191271e4b1SPrakash Surya  * The "zcw_lock" field is used to protect the commit waiter against
1201271e4b1SPrakash Surya  * concurrent access. This lock is often acquired while already holding
121cf07d3daSPrakash Surya  * the zilog's "zl_issuer_lock" or "zl_lock"; see the functions
1221271e4b1SPrakash Surya  * zil_process_commit_list() and zil_lwb_flush_vdevs_done() as examples
1231271e4b1SPrakash Surya  * of this. Thus, one must be careful not to acquire the
124cf07d3daSPrakash Surya  * "zl_issuer_lock" or "zl_lock" when already holding the "zcw_lock";
1251271e4b1SPrakash Surya  * e.g. see the zil_commit_waiter_timeout() function.
1261271e4b1SPrakash Surya  */
1271271e4b1SPrakash Surya typedef struct zil_commit_waiter {
1281271e4b1SPrakash Surya 	kcondvar_t	zcw_cv;		/* signalled when "done" */
1291271e4b1SPrakash Surya 	kmutex_t	zcw_lock;	/* protects fields of this struct */
1301271e4b1SPrakash Surya 	list_node_t	zcw_node;	/* linkage in lwb_t:lwb_waiter list */
1311271e4b1SPrakash Surya 	lwb_t		*zcw_lwb;	/* back pointer to lwb when linked */
1321271e4b1SPrakash Surya 	boolean_t	zcw_done;	/* B_TRUE when "done", else B_FALSE */
1331271e4b1SPrakash Surya 	int		zcw_zio_error;	/* contains the zio io_error value */
1341271e4b1SPrakash Surya } zil_commit_waiter_t;
1351271e4b1SPrakash Surya 
1365002558fSNeil Perrin /*
1375002558fSNeil Perrin  * Intent log transaction lists
1385002558fSNeil Perrin  */
1395002558fSNeil Perrin typedef struct itxs {
1405002558fSNeil Perrin 	list_t		i_sync_list;	/* list of synchronous itxs */
1415002558fSNeil Perrin 	avl_tree_t	i_async_tree;	/* tree of foids for async itxs */
1425002558fSNeil Perrin } itxs_t;
1435002558fSNeil Perrin 
1445002558fSNeil Perrin typedef struct itxg {
1455002558fSNeil Perrin 	kmutex_t	itxg_lock;	/* lock for this structure */
1465002558fSNeil Perrin 	uint64_t	itxg_txg;	/* txg for this chain */
1475002558fSNeil Perrin 	itxs_t		*itxg_itxs;	/* sync and async itxs */
1485002558fSNeil Perrin } itxg_t;
1495002558fSNeil Perrin 
1505002558fSNeil Perrin /* for async nodes we build up an AVL tree of lists of async itxs per file */
1515002558fSNeil Perrin typedef struct itx_async_node {
1525002558fSNeil Perrin 	uint64_t	ia_foid;	/* file object id */
1535002558fSNeil Perrin 	list_t		ia_list;	/* list of async itxs for this foid */
1545002558fSNeil Perrin 	avl_node_t	ia_node;	/* AVL tree linkage */
1555002558fSNeil Perrin } itx_async_node_t;
1565002558fSNeil Perrin 
157fa9e4066Sahrens /*
15817f17c2dSbonwick  * Vdev flushing: during a zil_commit(), we build up an AVL tree of the vdevs
15917f17c2dSbonwick  * we've touched so we know which ones need a write cache flush at the end.
160fa9e4066Sahrens  */
16117f17c2dSbonwick typedef struct zil_vdev_node {
16217f17c2dSbonwick 	uint64_t	zv_vdev;	/* vdev to be flushed */
16317f17c2dSbonwick 	avl_node_t	zv_node;	/* AVL tree linkage */
16417f17c2dSbonwick } zil_vdev_node_t;
165fa9e4066Sahrens 
1666e1f5caaSNeil Perrin #define	ZIL_PREV_BLKS 16
1676e1f5caaSNeil Perrin 
168fa9e4066Sahrens /*
169fa9e4066Sahrens  * Stable storage intent log management structure.  One per dataset.
170fa9e4066Sahrens  */
171fa9e4066Sahrens struct zilog {
172fa9e4066Sahrens 	kmutex_t	zl_lock;	/* protects most zilog_t fields */
173fa9e4066Sahrens 	struct dsl_pool	*zl_dmu_pool;	/* DSL pool */
174fa9e4066Sahrens 	spa_t		*zl_spa;	/* handle for read/write log */
175d80c45e0Sbonwick 	const zil_header_t *zl_header;	/* log header buffer */
176fa9e4066Sahrens 	objset_t	*zl_os;		/* object set we're logging */
177fa9e4066Sahrens 	zil_get_data_t	*zl_get_data;	/* callback to get object content */
1781271e4b1SPrakash Surya 	lwb_t		*zl_last_lwb_opened; /* most recent lwb opened */
1791271e4b1SPrakash Surya 	hrtime_t	zl_last_lwb_latency; /* zio latency of last lwb done */
180b24ab676SJeff Bonwick 	uint64_t	zl_lr_seq;	/* on-disk log record sequence number */
181b24ab676SJeff Bonwick 	uint64_t	zl_commit_lr_seq; /* last committed on-disk lr seq */
182fa9e4066Sahrens 	uint64_t	zl_destroy_txg;	/* txg of last zil_destroy() */
1831209a471SNeil Perrin 	uint64_t	zl_replayed_seq[TXG_SIZE]; /* last replayed rec seq */
1841209a471SNeil Perrin 	uint64_t	zl_replaying_seq; /* current replay seq number */
185fa9e4066Sahrens 	uint32_t	zl_suspend;	/* log suspend count */
186d80c45e0Sbonwick 	kcondvar_t	zl_cv_suspend;	/* log suspend completion */
187d80c45e0Sbonwick 	uint8_t		zl_suspending;	/* log is currently suspending */
188d80c45e0Sbonwick 	uint8_t		zl_keep_first;	/* keep first log block in destroy */
1891209a471SNeil Perrin 	uint8_t		zl_replay;	/* replaying records while set */
190fa9e4066Sahrens 	uint8_t		zl_stop_sync;	/* for debugging */
191cf07d3daSPrakash Surya 	kmutex_t	zl_issuer_lock;	/* single writer, per ZIL, at a time */
192e09fa4daSNeil Perrin 	uint8_t		zl_logbias;	/* latency or throughput */
19355da60b9SMark J Musante 	uint8_t		zl_sync;	/* synchronous or asynchronous */
194b24ab676SJeff Bonwick 	int		zl_parse_error;	/* last zil_parse() error */
195b24ab676SJeff Bonwick 	uint64_t	zl_parse_blk_seq; /* highest blk seq on last parse */
196b24ab676SJeff Bonwick 	uint64_t	zl_parse_lr_seq; /* highest lr seq on last parse */
197b24ab676SJeff Bonwick 	uint64_t	zl_parse_blk_count; /* number of blocks parsed */
198b24ab676SJeff Bonwick 	uint64_t	zl_parse_lr_count; /* number of log records parsed */
1995002558fSNeil Perrin 	itxg_t		zl_itxg[TXG_SIZE]; /* intent log txg chains */
2005002558fSNeil Perrin 	list_t		zl_itx_commit_list; /* itx list to be committed */
20122ac5be4Sperrin 	uint64_t	zl_cur_used;	/* current commit log size used */
202fa9e4066Sahrens 	list_t		zl_lwb_list;	/* in-flight log write list */
203b24ab676SJeff Bonwick 	avl_tree_t	zl_bp_tree;	/* track bps during log parse */
20467bd71c6Sperrin 	clock_t		zl_replay_time;	/* lbolt of when replay started */
20567bd71c6Sperrin 	uint64_t	zl_replay_blks;	/* number of log blocks replayed */
206b24ab676SJeff Bonwick 	zil_header_t	zl_old_header;	/* debugging aid */
2076e1f5caaSNeil Perrin 	uint_t		zl_prev_blks[ZIL_PREV_BLKS]; /* size - sector rounded */
2086e1f5caaSNeil Perrin 	uint_t		zl_prev_rotor;	/* rotor for zl_prev[] */
209ce636f8bSMatthew Ahrens 	txg_node_t	zl_dirty_link;	/* protected by dp_dirty_zilogs list */
2101271e4b1SPrakash Surya 	uint64_t	zl_dirty_max_txg; /* highest txg used to dirty zilog */
211fa9e4066Sahrens };
212fa9e4066Sahrens 
213b24ab676SJeff Bonwick typedef struct zil_bp_node {
214fa9e4066Sahrens 	dva_t		zn_dva;
215fa9e4066Sahrens 	avl_node_t	zn_node;
216b24ab676SJeff Bonwick } zil_bp_node_t;
217fa9e4066Sahrens 
218c5ee4681SAlexander Motin /*
219c5ee4681SAlexander Motin  * Maximum amount of write data that can be put into single log block.
220c5ee4681SAlexander Motin  */
221b5152584SMatthew Ahrens #define	ZIL_MAX_LOG_DATA (SPA_OLD_MAXBLOCKSIZE - sizeof (zil_chain_t) - \
222510b6c0eSNeil Perrin     sizeof (lr_write_t))
223510b6c0eSNeil Perrin 
224c5ee4681SAlexander Motin /*
225c5ee4681SAlexander Motin  * Maximum amount of log space we agree to waste to reduce number of
226c5ee4681SAlexander Motin  * WR_NEED_COPY chunks to reduce zl_get_data() overhead (~12%).
227c5ee4681SAlexander Motin  */
228c5ee4681SAlexander Motin #define	ZIL_MAX_WASTE_SPACE (ZIL_MAX_LOG_DATA / 8)
229c5ee4681SAlexander Motin 
230c5ee4681SAlexander Motin /*
231c5ee4681SAlexander Motin  * Maximum amount of write data for WR_COPIED.  Fall back to WR_NEED_COPY
232c5ee4681SAlexander Motin  * as more space efficient if we can't fit at least two log records into
233c5ee4681SAlexander Motin  * maximum sized log block.
234c5ee4681SAlexander Motin  */
235c5ee4681SAlexander Motin #define	ZIL_MAX_COPIED_DATA ((SPA_OLD_MAXBLOCKSIZE - \
236c5ee4681SAlexander Motin     sizeof (zil_chain_t)) / 2 - sizeof (lr_write_t))
237c5ee4681SAlexander Motin 
238fa9e4066Sahrens #ifdef	__cplusplus
239fa9e4066Sahrens }
240fa9e4066Sahrens #endif
241fa9e4066Sahrens 
242fa9e4066Sahrens #endif	/* _SYS_ZIL_IMPL_H */
243