xref: /illumos-gate/usr/src/uts/common/fs/zfs/sys/zil_impl.h (revision cf07d3da)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
24  * Copyright (c) 2014 Integros [integros.com]
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  * Possbile states for a given lwb structure. An lwb will start out in
41  * the "closed" state, and then transition to the "opened" state via a
42  * call to zil_lwb_write_open(). After the lwb is "open", it can
43  * transition into the "issued" state via zil_lwb_write_issue(). After
44  * the lwb's zio completes, and the vdev's are flushed, the lwb will
45  * transition into the "done" state via zil_lwb_write_done(), and the
46  * structure eventually freed.
47  */
48 typedef enum {
49     LWB_STATE_CLOSED,
50     LWB_STATE_OPENED,
51     LWB_STATE_ISSUED,
52     LWB_STATE_DONE,
53     LWB_NUM_STATES
54 } lwb_state_t;
55 
56 /*
57  * Log write block (lwb)
58  *
59  * Prior to an lwb being issued to disk via zil_lwb_write_issue(), it
60  * will be protected by the zilog's "zl_issuer_lock". Basically, prior
61  * to it being issued, it will only be accessed by the thread that's
62  * holding the "zl_issuer_lock". After the lwb is issued, the zilog's
63  * "zl_lock" is used to protect the lwb against concurrent access.
64  */
65 typedef struct lwb {
66 	zilog_t		*lwb_zilog;	/* back pointer to log struct */
67 	blkptr_t	lwb_blk;	/* on disk address of this log blk */
68 	boolean_t	lwb_slog;	/* lwb_blk is on SLOG device */
69 	int		lwb_nused;	/* # used bytes in buffer */
70 	int		lwb_sz;		/* size of block and buffer */
71 	lwb_state_t	lwb_state;	/* the state of this lwb */
72 	char		*lwb_buf;	/* log write buffer */
73 	zio_t		*lwb_write_zio;	/* zio for the lwb buffer */
74 	zio_t		*lwb_root_zio;	/* root zio for lwb write and flushes */
75 	dmu_tx_t	*lwb_tx;	/* tx for log block allocation */
76 	uint64_t	lwb_max_txg;	/* highest txg in this lwb */
77 	list_node_t	lwb_node;	/* zilog->zl_lwb_list linkage */
78 	list_t		lwb_waiters;	/* list of zil_commit_waiter's */
79 	avl_tree_t	lwb_vdev_tree;	/* vdevs to flush after lwb write */
80 	kmutex_t	lwb_vdev_lock;	/* protects lwb_vdev_tree */
81 	hrtime_t	lwb_issued_timestamp; /* when was the lwb issued? */
82 } lwb_t;
83 
84 /*
85  * ZIL commit waiter.
86  *
87  * This structure is allocated each time zil_commit() is called, and is
88  * used by zil_commit() to communicate with other parts of the ZIL, such
89  * that zil_commit() can know when it safe for it return. For more
90  * details, see the comment above zil_commit().
91  *
92  * The "zcw_lock" field is used to protect the commit waiter against
93  * concurrent access. This lock is often acquired while already holding
94  * the zilog's "zl_issuer_lock" or "zl_lock"; see the functions
95  * zil_process_commit_list() and zil_lwb_flush_vdevs_done() as examples
96  * of this. Thus, one must be careful not to acquire the
97  * "zl_issuer_lock" or "zl_lock" when already holding the "zcw_lock";
98  * e.g. see the zil_commit_waiter_timeout() function.
99  */
100 typedef struct zil_commit_waiter {
101 	kcondvar_t	zcw_cv;		/* signalled when "done" */
102 	kmutex_t	zcw_lock;	/* protects fields of this struct */
103 	list_node_t	zcw_node;	/* linkage in lwb_t:lwb_waiter list */
104 	lwb_t		*zcw_lwb;	/* back pointer to lwb when linked */
105 	boolean_t	zcw_done;	/* B_TRUE when "done", else B_FALSE */
106 	int		zcw_zio_error;	/* contains the zio io_error value */
107 } zil_commit_waiter_t;
108 
109 /*
110  * Intent log transaction lists
111  */
112 typedef struct itxs {
113 	list_t		i_sync_list;	/* list of synchronous itxs */
114 	avl_tree_t	i_async_tree;	/* tree of foids for async itxs */
115 } itxs_t;
116 
117 typedef struct itxg {
118 	kmutex_t	itxg_lock;	/* lock for this structure */
119 	uint64_t	itxg_txg;	/* txg for this chain */
120 	itxs_t		*itxg_itxs;	/* sync and async itxs */
121 } itxg_t;
122 
123 /* for async nodes we build up an AVL tree of lists of async itxs per file */
124 typedef struct itx_async_node {
125 	uint64_t	ia_foid;	/* file object id */
126 	list_t		ia_list;	/* list of async itxs for this foid */
127 	avl_node_t	ia_node;	/* AVL tree linkage */
128 } itx_async_node_t;
129 
130 /*
131  * Vdev flushing: during a zil_commit(), we build up an AVL tree of the vdevs
132  * we've touched so we know which ones need a write cache flush at the end.
133  */
134 typedef struct zil_vdev_node {
135 	uint64_t	zv_vdev;	/* vdev to be flushed */
136 	avl_node_t	zv_node;	/* AVL tree linkage */
137 } zil_vdev_node_t;
138 
139 #define	ZIL_PREV_BLKS 16
140 
141 /*
142  * Stable storage intent log management structure.  One per dataset.
143  */
144 struct zilog {
145 	kmutex_t	zl_lock;	/* protects most zilog_t fields */
146 	struct dsl_pool	*zl_dmu_pool;	/* DSL pool */
147 	spa_t		*zl_spa;	/* handle for read/write log */
148 	const zil_header_t *zl_header;	/* log header buffer */
149 	objset_t	*zl_os;		/* object set we're logging */
150 	zil_get_data_t	*zl_get_data;	/* callback to get object content */
151 	lwb_t		*zl_last_lwb_opened; /* most recent lwb opened */
152 	hrtime_t	zl_last_lwb_latency; /* zio latency of last lwb done */
153 	uint64_t	zl_lr_seq;	/* on-disk log record sequence number */
154 	uint64_t	zl_commit_lr_seq; /* last committed on-disk lr seq */
155 	uint64_t	zl_destroy_txg;	/* txg of last zil_destroy() */
156 	uint64_t	zl_replayed_seq[TXG_SIZE]; /* last replayed rec seq */
157 	uint64_t	zl_replaying_seq; /* current replay seq number */
158 	uint32_t	zl_suspend;	/* log suspend count */
159 	kcondvar_t	zl_cv_suspend;	/* log suspend completion */
160 	uint8_t		zl_suspending;	/* log is currently suspending */
161 	uint8_t		zl_keep_first;	/* keep first log block in destroy */
162 	uint8_t		zl_replay;	/* replaying records while set */
163 	uint8_t		zl_stop_sync;	/* for debugging */
164 	kmutex_t	zl_issuer_lock;	/* single writer, per ZIL, at a time */
165 	uint8_t		zl_logbias;	/* latency or throughput */
166 	uint8_t		zl_sync;	/* synchronous or asynchronous */
167 	int		zl_parse_error;	/* last zil_parse() error */
168 	uint64_t	zl_parse_blk_seq; /* highest blk seq on last parse */
169 	uint64_t	zl_parse_lr_seq; /* highest lr seq on last parse */
170 	uint64_t	zl_parse_blk_count; /* number of blocks parsed */
171 	uint64_t	zl_parse_lr_count; /* number of log records parsed */
172 	itxg_t		zl_itxg[TXG_SIZE]; /* intent log txg chains */
173 	list_t		zl_itx_commit_list; /* itx list to be committed */
174 	uint64_t	zl_cur_used;	/* current commit log size used */
175 	list_t		zl_lwb_list;	/* in-flight log write list */
176 	avl_tree_t	zl_bp_tree;	/* track bps during log parse */
177 	clock_t		zl_replay_time;	/* lbolt of when replay started */
178 	uint64_t	zl_replay_blks;	/* number of log blocks replayed */
179 	zil_header_t	zl_old_header;	/* debugging aid */
180 	uint_t		zl_prev_blks[ZIL_PREV_BLKS]; /* size - sector rounded */
181 	uint_t		zl_prev_rotor;	/* rotor for zl_prev[] */
182 	txg_node_t	zl_dirty_link;	/* protected by dp_dirty_zilogs list */
183 	uint64_t	zl_dirty_max_txg; /* highest txg used to dirty zilog */
184 };
185 
186 typedef struct zil_bp_node {
187 	dva_t		zn_dva;
188 	avl_node_t	zn_node;
189 } zil_bp_node_t;
190 
191 /*
192  * Maximum amount of write data that can be put into single log block.
193  */
194 #define	ZIL_MAX_LOG_DATA (SPA_OLD_MAXBLOCKSIZE - sizeof (zil_chain_t) - \
195     sizeof (lr_write_t))
196 
197 /*
198  * Maximum amount of log space we agree to waste to reduce number of
199  * WR_NEED_COPY chunks to reduce zl_get_data() overhead (~12%).
200  */
201 #define	ZIL_MAX_WASTE_SPACE (ZIL_MAX_LOG_DATA / 8)
202 
203 /*
204  * Maximum amount of write data for WR_COPIED.  Fall back to WR_NEED_COPY
205  * as more space efficient if we can't fit at least two log records into
206  * maximum sized log block.
207  */
208 #define	ZIL_MAX_COPIED_DATA ((SPA_OLD_MAXBLOCKSIZE - \
209     sizeof (zil_chain_t)) / 2 - sizeof (lr_write_t))
210 
211 #ifdef	__cplusplus
212 }
213 #endif
214 
215 #endif	/* _SYS_ZIL_IMPL_H */
216