xref: /freebsd/sys/contrib/openzfs/module/zfs/zil.c (revision a0b956f5ac5e0941f9e74e24c1c53e05ad061a38)
1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy  *
8eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9eda14cbcSMatt Macy  * or http://www.opensolaris.org/os/licensing.
10eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11eda14cbcSMatt Macy  * and limitations under the License.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy  *
19eda14cbcSMatt Macy  * CDDL HEADER END
20eda14cbcSMatt Macy  */
21eda14cbcSMatt Macy /*
22eda14cbcSMatt Macy  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23eda14cbcSMatt Macy  * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
24eda14cbcSMatt Macy  * Copyright (c) 2014 Integros [integros.com]
25eda14cbcSMatt Macy  * Copyright (c) 2018 Datto Inc.
26eda14cbcSMatt Macy  */
27eda14cbcSMatt Macy 
28eda14cbcSMatt Macy /* Portions Copyright 2010 Robert Milkowski */
29eda14cbcSMatt Macy 
30eda14cbcSMatt Macy #include <sys/zfs_context.h>
31eda14cbcSMatt Macy #include <sys/spa.h>
32eda14cbcSMatt Macy #include <sys/spa_impl.h>
33eda14cbcSMatt Macy #include <sys/dmu.h>
34eda14cbcSMatt Macy #include <sys/zap.h>
35eda14cbcSMatt Macy #include <sys/arc.h>
36eda14cbcSMatt Macy #include <sys/stat.h>
37eda14cbcSMatt Macy #include <sys/zil.h>
38eda14cbcSMatt Macy #include <sys/zil_impl.h>
39eda14cbcSMatt Macy #include <sys/dsl_dataset.h>
40eda14cbcSMatt Macy #include <sys/vdev_impl.h>
41eda14cbcSMatt Macy #include <sys/dmu_tx.h>
42eda14cbcSMatt Macy #include <sys/dsl_pool.h>
43eda14cbcSMatt Macy #include <sys/metaslab.h>
44eda14cbcSMatt Macy #include <sys/trace_zfs.h>
45eda14cbcSMatt Macy #include <sys/abd.h>
46eda14cbcSMatt Macy 
47eda14cbcSMatt Macy /*
48eda14cbcSMatt Macy  * The ZFS Intent Log (ZIL) saves "transaction records" (itxs) of system
49eda14cbcSMatt Macy  * calls that change the file system. Each itx has enough information to
50eda14cbcSMatt Macy  * be able to replay them after a system crash, power loss, or
51eda14cbcSMatt Macy  * equivalent failure mode. These are stored in memory until either:
52eda14cbcSMatt Macy  *
53eda14cbcSMatt Macy  *   1. they are committed to the pool by the DMU transaction group
54eda14cbcSMatt Macy  *      (txg), at which point they can be discarded; or
55eda14cbcSMatt Macy  *   2. they are committed to the on-disk ZIL for the dataset being
56eda14cbcSMatt Macy  *      modified (e.g. due to an fsync, O_DSYNC, or other synchronous
57eda14cbcSMatt Macy  *      requirement).
58eda14cbcSMatt Macy  *
59eda14cbcSMatt Macy  * In the event of a crash or power loss, the itxs contained by each
60eda14cbcSMatt Macy  * dataset's on-disk ZIL will be replayed when that dataset is first
61eda14cbcSMatt Macy  * instantiated (e.g. if the dataset is a normal filesystem, when it is
62eda14cbcSMatt Macy  * first mounted).
63eda14cbcSMatt Macy  *
64eda14cbcSMatt Macy  * As hinted at above, there is one ZIL per dataset (both the in-memory
65eda14cbcSMatt Macy  * representation, and the on-disk representation). The on-disk format
66eda14cbcSMatt Macy  * consists of 3 parts:
67eda14cbcSMatt Macy  *
68eda14cbcSMatt Macy  * 	- a single, per-dataset, ZIL header; which points to a chain of
69eda14cbcSMatt Macy  * 	- zero or more ZIL blocks; each of which contains
70eda14cbcSMatt Macy  * 	- zero or more ZIL records
71eda14cbcSMatt Macy  *
72eda14cbcSMatt Macy  * A ZIL record holds the information necessary to replay a single
73eda14cbcSMatt Macy  * system call transaction. A ZIL block can hold many ZIL records, and
74eda14cbcSMatt Macy  * the blocks are chained together, similarly to a singly linked list.
75eda14cbcSMatt Macy  *
76eda14cbcSMatt Macy  * Each ZIL block contains a block pointer (blkptr_t) to the next ZIL
77eda14cbcSMatt Macy  * block in the chain, and the ZIL header points to the first block in
78eda14cbcSMatt Macy  * the chain.
79eda14cbcSMatt Macy  *
80eda14cbcSMatt Macy  * Note, there is not a fixed place in the pool to hold these ZIL
81eda14cbcSMatt Macy  * blocks; they are dynamically allocated and freed as needed from the
82eda14cbcSMatt Macy  * blocks available on the pool, though they can be preferentially
83eda14cbcSMatt Macy  * allocated from a dedicated "log" vdev.
84eda14cbcSMatt Macy  */
85eda14cbcSMatt Macy 
86eda14cbcSMatt Macy /*
87eda14cbcSMatt Macy  * This controls the amount of time that a ZIL block (lwb) will remain
88eda14cbcSMatt Macy  * "open" when it isn't "full", and it has a thread waiting for it to be
89eda14cbcSMatt Macy  * committed to stable storage. Please refer to the zil_commit_waiter()
90eda14cbcSMatt Macy  * function (and the comments within it) for more details.
91eda14cbcSMatt Macy  */
92e92ffd9bSMartin Matuska static int zfs_commit_timeout_pct = 5;
93eda14cbcSMatt Macy 
94eda14cbcSMatt Macy /*
95eda14cbcSMatt Macy  * See zil.h for more information about these fields.
96eda14cbcSMatt Macy  */
97e92ffd9bSMartin Matuska static zil_stats_t zil_stats = {
98eda14cbcSMatt Macy 	{ "zil_commit_count",			KSTAT_DATA_UINT64 },
99eda14cbcSMatt Macy 	{ "zil_commit_writer_count",		KSTAT_DATA_UINT64 },
100eda14cbcSMatt Macy 	{ "zil_itx_count",			KSTAT_DATA_UINT64 },
101eda14cbcSMatt Macy 	{ "zil_itx_indirect_count",		KSTAT_DATA_UINT64 },
102eda14cbcSMatt Macy 	{ "zil_itx_indirect_bytes",		KSTAT_DATA_UINT64 },
103eda14cbcSMatt Macy 	{ "zil_itx_copied_count",		KSTAT_DATA_UINT64 },
104eda14cbcSMatt Macy 	{ "zil_itx_copied_bytes",		KSTAT_DATA_UINT64 },
105eda14cbcSMatt Macy 	{ "zil_itx_needcopy_count",		KSTAT_DATA_UINT64 },
106eda14cbcSMatt Macy 	{ "zil_itx_needcopy_bytes",		KSTAT_DATA_UINT64 },
107eda14cbcSMatt Macy 	{ "zil_itx_metaslab_normal_count",	KSTAT_DATA_UINT64 },
108eda14cbcSMatt Macy 	{ "zil_itx_metaslab_normal_bytes",	KSTAT_DATA_UINT64 },
109eda14cbcSMatt Macy 	{ "zil_itx_metaslab_slog_count",	KSTAT_DATA_UINT64 },
110eda14cbcSMatt Macy 	{ "zil_itx_metaslab_slog_bytes",	KSTAT_DATA_UINT64 },
111eda14cbcSMatt Macy };
112eda14cbcSMatt Macy 
113eda14cbcSMatt Macy static kstat_t *zil_ksp;
114eda14cbcSMatt Macy 
115eda14cbcSMatt Macy /*
116eda14cbcSMatt Macy  * Disable intent logging replay.  This global ZIL switch affects all pools.
117eda14cbcSMatt Macy  */
118eda14cbcSMatt Macy int zil_replay_disable = 0;
119eda14cbcSMatt Macy 
120eda14cbcSMatt Macy /*
121eda14cbcSMatt Macy  * Disable the DKIOCFLUSHWRITECACHE commands that are normally sent to
122eda14cbcSMatt Macy  * the disk(s) by the ZIL after an LWB write has completed. Setting this
123eda14cbcSMatt Macy  * will cause ZIL corruption on power loss if a volatile out-of-order
124eda14cbcSMatt Macy  * write cache is enabled.
125eda14cbcSMatt Macy  */
126e92ffd9bSMartin Matuska static int zil_nocacheflush = 0;
127eda14cbcSMatt Macy 
128eda14cbcSMatt Macy /*
129eda14cbcSMatt Macy  * Limit SLOG write size per commit executed with synchronous priority.
130eda14cbcSMatt Macy  * Any writes above that will be executed with lower (asynchronous) priority
131eda14cbcSMatt Macy  * to limit potential SLOG device abuse by single active ZIL writer.
132eda14cbcSMatt Macy  */
133e92ffd9bSMartin Matuska static unsigned long zil_slog_bulk = 768 * 1024;
134eda14cbcSMatt Macy 
135eda14cbcSMatt Macy static kmem_cache_t *zil_lwb_cache;
136eda14cbcSMatt Macy static kmem_cache_t *zil_zcw_cache;
137eda14cbcSMatt Macy 
138eda14cbcSMatt Macy #define	LWB_EMPTY(lwb) ((BP_GET_LSIZE(&lwb->lwb_blk) - \
139eda14cbcSMatt Macy     sizeof (zil_chain_t)) == (lwb->lwb_sz - lwb->lwb_nused))
140eda14cbcSMatt Macy 
141eda14cbcSMatt Macy static int
142eda14cbcSMatt Macy zil_bp_compare(const void *x1, const void *x2)
143eda14cbcSMatt Macy {
144eda14cbcSMatt Macy 	const dva_t *dva1 = &((zil_bp_node_t *)x1)->zn_dva;
145eda14cbcSMatt Macy 	const dva_t *dva2 = &((zil_bp_node_t *)x2)->zn_dva;
146eda14cbcSMatt Macy 
147eda14cbcSMatt Macy 	int cmp = TREE_CMP(DVA_GET_VDEV(dva1), DVA_GET_VDEV(dva2));
148eda14cbcSMatt Macy 	if (likely(cmp))
149eda14cbcSMatt Macy 		return (cmp);
150eda14cbcSMatt Macy 
151eda14cbcSMatt Macy 	return (TREE_CMP(DVA_GET_OFFSET(dva1), DVA_GET_OFFSET(dva2)));
152eda14cbcSMatt Macy }
153eda14cbcSMatt Macy 
154eda14cbcSMatt Macy static void
155eda14cbcSMatt Macy zil_bp_tree_init(zilog_t *zilog)
156eda14cbcSMatt Macy {
157eda14cbcSMatt Macy 	avl_create(&zilog->zl_bp_tree, zil_bp_compare,
158eda14cbcSMatt Macy 	    sizeof (zil_bp_node_t), offsetof(zil_bp_node_t, zn_node));
159eda14cbcSMatt Macy }
160eda14cbcSMatt Macy 
161eda14cbcSMatt Macy static void
162eda14cbcSMatt Macy zil_bp_tree_fini(zilog_t *zilog)
163eda14cbcSMatt Macy {
164eda14cbcSMatt Macy 	avl_tree_t *t = &zilog->zl_bp_tree;
165eda14cbcSMatt Macy 	zil_bp_node_t *zn;
166eda14cbcSMatt Macy 	void *cookie = NULL;
167eda14cbcSMatt Macy 
168eda14cbcSMatt Macy 	while ((zn = avl_destroy_nodes(t, &cookie)) != NULL)
169eda14cbcSMatt Macy 		kmem_free(zn, sizeof (zil_bp_node_t));
170eda14cbcSMatt Macy 
171eda14cbcSMatt Macy 	avl_destroy(t);
172eda14cbcSMatt Macy }
173eda14cbcSMatt Macy 
174eda14cbcSMatt Macy int
175eda14cbcSMatt Macy zil_bp_tree_add(zilog_t *zilog, const blkptr_t *bp)
176eda14cbcSMatt Macy {
177eda14cbcSMatt Macy 	avl_tree_t *t = &zilog->zl_bp_tree;
178eda14cbcSMatt Macy 	const dva_t *dva;
179eda14cbcSMatt Macy 	zil_bp_node_t *zn;
180eda14cbcSMatt Macy 	avl_index_t where;
181eda14cbcSMatt Macy 
182eda14cbcSMatt Macy 	if (BP_IS_EMBEDDED(bp))
183eda14cbcSMatt Macy 		return (0);
184eda14cbcSMatt Macy 
185eda14cbcSMatt Macy 	dva = BP_IDENTITY(bp);
186eda14cbcSMatt Macy 
187eda14cbcSMatt Macy 	if (avl_find(t, dva, &where) != NULL)
188eda14cbcSMatt Macy 		return (SET_ERROR(EEXIST));
189eda14cbcSMatt Macy 
190eda14cbcSMatt Macy 	zn = kmem_alloc(sizeof (zil_bp_node_t), KM_SLEEP);
191eda14cbcSMatt Macy 	zn->zn_dva = *dva;
192eda14cbcSMatt Macy 	avl_insert(t, zn, where);
193eda14cbcSMatt Macy 
194eda14cbcSMatt Macy 	return (0);
195eda14cbcSMatt Macy }
196eda14cbcSMatt Macy 
197eda14cbcSMatt Macy static zil_header_t *
198eda14cbcSMatt Macy zil_header_in_syncing_context(zilog_t *zilog)
199eda14cbcSMatt Macy {
200eda14cbcSMatt Macy 	return ((zil_header_t *)zilog->zl_header);
201eda14cbcSMatt Macy }
202eda14cbcSMatt Macy 
203eda14cbcSMatt Macy static void
204eda14cbcSMatt Macy zil_init_log_chain(zilog_t *zilog, blkptr_t *bp)
205eda14cbcSMatt Macy {
206eda14cbcSMatt Macy 	zio_cksum_t *zc = &bp->blk_cksum;
207eda14cbcSMatt Macy 
20833b8c039SMartin Matuska 	(void) random_get_pseudo_bytes((void *)&zc->zc_word[ZIL_ZC_GUID_0],
20933b8c039SMartin Matuska 	    sizeof (zc->zc_word[ZIL_ZC_GUID_0]));
21033b8c039SMartin Matuska 	(void) random_get_pseudo_bytes((void *)&zc->zc_word[ZIL_ZC_GUID_1],
21133b8c039SMartin Matuska 	    sizeof (zc->zc_word[ZIL_ZC_GUID_1]));
212eda14cbcSMatt Macy 	zc->zc_word[ZIL_ZC_OBJSET] = dmu_objset_id(zilog->zl_os);
213eda14cbcSMatt Macy 	zc->zc_word[ZIL_ZC_SEQ] = 1ULL;
214eda14cbcSMatt Macy }
215eda14cbcSMatt Macy 
216eda14cbcSMatt Macy /*
217eda14cbcSMatt Macy  * Read a log block and make sure it's valid.
218eda14cbcSMatt Macy  */
219eda14cbcSMatt Macy static int
220eda14cbcSMatt Macy zil_read_log_block(zilog_t *zilog, boolean_t decrypt, const blkptr_t *bp,
221eda14cbcSMatt Macy     blkptr_t *nbp, void *dst, char **end)
222eda14cbcSMatt Macy {
223eda14cbcSMatt Macy 	enum zio_flag zio_flags = ZIO_FLAG_CANFAIL;
224eda14cbcSMatt Macy 	arc_flags_t aflags = ARC_FLAG_WAIT;
225eda14cbcSMatt Macy 	arc_buf_t *abuf = NULL;
226eda14cbcSMatt Macy 	zbookmark_phys_t zb;
227eda14cbcSMatt Macy 	int error;
228eda14cbcSMatt Macy 
229eda14cbcSMatt Macy 	if (zilog->zl_header->zh_claim_txg == 0)
230eda14cbcSMatt Macy 		zio_flags |= ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB;
231eda14cbcSMatt Macy 
232eda14cbcSMatt Macy 	if (!(zilog->zl_header->zh_flags & ZIL_CLAIM_LR_SEQ_VALID))
233eda14cbcSMatt Macy 		zio_flags |= ZIO_FLAG_SPECULATIVE;
234eda14cbcSMatt Macy 
235eda14cbcSMatt Macy 	if (!decrypt)
236eda14cbcSMatt Macy 		zio_flags |= ZIO_FLAG_RAW;
237eda14cbcSMatt Macy 
238eda14cbcSMatt Macy 	SET_BOOKMARK(&zb, bp->blk_cksum.zc_word[ZIL_ZC_OBJSET],
239eda14cbcSMatt Macy 	    ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, bp->blk_cksum.zc_word[ZIL_ZC_SEQ]);
240eda14cbcSMatt Macy 
241eda14cbcSMatt Macy 	error = arc_read(NULL, zilog->zl_spa, bp, arc_getbuf_func,
242eda14cbcSMatt Macy 	    &abuf, ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb);
243eda14cbcSMatt Macy 
244eda14cbcSMatt Macy 	if (error == 0) {
245eda14cbcSMatt Macy 		zio_cksum_t cksum = bp->blk_cksum;
246eda14cbcSMatt Macy 
247eda14cbcSMatt Macy 		/*
248eda14cbcSMatt Macy 		 * Validate the checksummed log block.
249eda14cbcSMatt Macy 		 *
250eda14cbcSMatt Macy 		 * Sequence numbers should be... sequential.  The checksum
251eda14cbcSMatt Macy 		 * verifier for the next block should be bp's checksum plus 1.
252eda14cbcSMatt Macy 		 *
253eda14cbcSMatt Macy 		 * Also check the log chain linkage and size used.
254eda14cbcSMatt Macy 		 */
255eda14cbcSMatt Macy 		cksum.zc_word[ZIL_ZC_SEQ]++;
256eda14cbcSMatt Macy 
257eda14cbcSMatt Macy 		if (BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_ZILOG2) {
258eda14cbcSMatt Macy 			zil_chain_t *zilc = abuf->b_data;
259eda14cbcSMatt Macy 			char *lr = (char *)(zilc + 1);
260eda14cbcSMatt Macy 			uint64_t len = zilc->zc_nused - sizeof (zil_chain_t);
261eda14cbcSMatt Macy 
262da5137abSMartin Matuska 			if (memcmp(&cksum, &zilc->zc_next_blk.blk_cksum,
263eda14cbcSMatt Macy 			    sizeof (cksum)) || BP_IS_HOLE(&zilc->zc_next_blk)) {
264eda14cbcSMatt Macy 				error = SET_ERROR(ECKSUM);
265eda14cbcSMatt Macy 			} else {
266eda14cbcSMatt Macy 				ASSERT3U(len, <=, SPA_OLD_MAXBLOCKSIZE);
267da5137abSMartin Matuska 				memcpy(dst, lr, len);
268eda14cbcSMatt Macy 				*end = (char *)dst + len;
269eda14cbcSMatt Macy 				*nbp = zilc->zc_next_blk;
270eda14cbcSMatt Macy 			}
271eda14cbcSMatt Macy 		} else {
272eda14cbcSMatt Macy 			char *lr = abuf->b_data;
273eda14cbcSMatt Macy 			uint64_t size = BP_GET_LSIZE(bp);
274eda14cbcSMatt Macy 			zil_chain_t *zilc = (zil_chain_t *)(lr + size) - 1;
275eda14cbcSMatt Macy 
276da5137abSMartin Matuska 			if (memcmp(&cksum, &zilc->zc_next_blk.blk_cksum,
277eda14cbcSMatt Macy 			    sizeof (cksum)) || BP_IS_HOLE(&zilc->zc_next_blk) ||
278eda14cbcSMatt Macy 			    (zilc->zc_nused > (size - sizeof (*zilc)))) {
279eda14cbcSMatt Macy 				error = SET_ERROR(ECKSUM);
280eda14cbcSMatt Macy 			} else {
281eda14cbcSMatt Macy 				ASSERT3U(zilc->zc_nused, <=,
282eda14cbcSMatt Macy 				    SPA_OLD_MAXBLOCKSIZE);
283da5137abSMartin Matuska 				memcpy(dst, lr, zilc->zc_nused);
284eda14cbcSMatt Macy 				*end = (char *)dst + zilc->zc_nused;
285eda14cbcSMatt Macy 				*nbp = zilc->zc_next_blk;
286eda14cbcSMatt Macy 			}
287eda14cbcSMatt Macy 		}
288eda14cbcSMatt Macy 
289eda14cbcSMatt Macy 		arc_buf_destroy(abuf, &abuf);
290eda14cbcSMatt Macy 	}
291eda14cbcSMatt Macy 
292eda14cbcSMatt Macy 	return (error);
293eda14cbcSMatt Macy }
294eda14cbcSMatt Macy 
295eda14cbcSMatt Macy /*
296eda14cbcSMatt Macy  * Read a TX_WRITE log data block.
297eda14cbcSMatt Macy  */
298eda14cbcSMatt Macy static int
299eda14cbcSMatt Macy zil_read_log_data(zilog_t *zilog, const lr_write_t *lr, void *wbuf)
300eda14cbcSMatt Macy {
301eda14cbcSMatt Macy 	enum zio_flag zio_flags = ZIO_FLAG_CANFAIL;
302eda14cbcSMatt Macy 	const blkptr_t *bp = &lr->lr_blkptr;
303eda14cbcSMatt Macy 	arc_flags_t aflags = ARC_FLAG_WAIT;
304eda14cbcSMatt Macy 	arc_buf_t *abuf = NULL;
305eda14cbcSMatt Macy 	zbookmark_phys_t zb;
306eda14cbcSMatt Macy 	int error;
307eda14cbcSMatt Macy 
308eda14cbcSMatt Macy 	if (BP_IS_HOLE(bp)) {
309eda14cbcSMatt Macy 		if (wbuf != NULL)
310da5137abSMartin Matuska 			memset(wbuf, 0, MAX(BP_GET_LSIZE(bp), lr->lr_length));
311eda14cbcSMatt Macy 		return (0);
312eda14cbcSMatt Macy 	}
313eda14cbcSMatt Macy 
314eda14cbcSMatt Macy 	if (zilog->zl_header->zh_claim_txg == 0)
315eda14cbcSMatt Macy 		zio_flags |= ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB;
316eda14cbcSMatt Macy 
317eda14cbcSMatt Macy 	/*
318eda14cbcSMatt Macy 	 * If we are not using the resulting data, we are just checking that
319eda14cbcSMatt Macy 	 * it hasn't been corrupted so we don't need to waste CPU time
320eda14cbcSMatt Macy 	 * decompressing and decrypting it.
321eda14cbcSMatt Macy 	 */
322eda14cbcSMatt Macy 	if (wbuf == NULL)
323eda14cbcSMatt Macy 		zio_flags |= ZIO_FLAG_RAW;
324eda14cbcSMatt Macy 
325eda14cbcSMatt Macy 	SET_BOOKMARK(&zb, dmu_objset_id(zilog->zl_os), lr->lr_foid,
326eda14cbcSMatt Macy 	    ZB_ZIL_LEVEL, lr->lr_offset / BP_GET_LSIZE(bp));
327eda14cbcSMatt Macy 
328eda14cbcSMatt Macy 	error = arc_read(NULL, zilog->zl_spa, bp, arc_getbuf_func, &abuf,
329eda14cbcSMatt Macy 	    ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb);
330eda14cbcSMatt Macy 
331eda14cbcSMatt Macy 	if (error == 0) {
332eda14cbcSMatt Macy 		if (wbuf != NULL)
333da5137abSMartin Matuska 			memcpy(wbuf, abuf->b_data, arc_buf_size(abuf));
334eda14cbcSMatt Macy 		arc_buf_destroy(abuf, &abuf);
335eda14cbcSMatt Macy 	}
336eda14cbcSMatt Macy 
337eda14cbcSMatt Macy 	return (error);
338eda14cbcSMatt Macy }
339eda14cbcSMatt Macy 
340eda14cbcSMatt Macy /*
341eda14cbcSMatt Macy  * Parse the intent log, and call parse_func for each valid record within.
342eda14cbcSMatt Macy  */
343eda14cbcSMatt Macy int
344eda14cbcSMatt Macy zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func,
345eda14cbcSMatt Macy     zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg,
346eda14cbcSMatt Macy     boolean_t decrypt)
347eda14cbcSMatt Macy {
348eda14cbcSMatt Macy 	const zil_header_t *zh = zilog->zl_header;
349eda14cbcSMatt Macy 	boolean_t claimed = !!zh->zh_claim_txg;
350eda14cbcSMatt Macy 	uint64_t claim_blk_seq = claimed ? zh->zh_claim_blk_seq : UINT64_MAX;
351eda14cbcSMatt Macy 	uint64_t claim_lr_seq = claimed ? zh->zh_claim_lr_seq : UINT64_MAX;
352eda14cbcSMatt Macy 	uint64_t max_blk_seq = 0;
353eda14cbcSMatt Macy 	uint64_t max_lr_seq = 0;
354eda14cbcSMatt Macy 	uint64_t blk_count = 0;
355eda14cbcSMatt Macy 	uint64_t lr_count = 0;
356da5137abSMartin Matuska 	blkptr_t blk, next_blk = {{{{0}}}};
357eda14cbcSMatt Macy 	char *lrbuf, *lrp;
358eda14cbcSMatt Macy 	int error = 0;
359eda14cbcSMatt Macy 
360eda14cbcSMatt Macy 	/*
361eda14cbcSMatt Macy 	 * Old logs didn't record the maximum zh_claim_lr_seq.
362eda14cbcSMatt Macy 	 */
363eda14cbcSMatt Macy 	if (!(zh->zh_flags & ZIL_CLAIM_LR_SEQ_VALID))
364eda14cbcSMatt Macy 		claim_lr_seq = UINT64_MAX;
365eda14cbcSMatt Macy 
366eda14cbcSMatt Macy 	/*
367eda14cbcSMatt Macy 	 * Starting at the block pointed to by zh_log we read the log chain.
368eda14cbcSMatt Macy 	 * For each block in the chain we strongly check that block to
369eda14cbcSMatt Macy 	 * ensure its validity.  We stop when an invalid block is found.
370eda14cbcSMatt Macy 	 * For each block pointer in the chain we call parse_blk_func().
371eda14cbcSMatt Macy 	 * For each record in each valid block we call parse_lr_func().
372eda14cbcSMatt Macy 	 * If the log has been claimed, stop if we encounter a sequence
373eda14cbcSMatt Macy 	 * number greater than the highest claimed sequence number.
374eda14cbcSMatt Macy 	 */
375eda14cbcSMatt Macy 	lrbuf = zio_buf_alloc(SPA_OLD_MAXBLOCKSIZE);
376eda14cbcSMatt Macy 	zil_bp_tree_init(zilog);
377eda14cbcSMatt Macy 
378eda14cbcSMatt Macy 	for (blk = zh->zh_log; !BP_IS_HOLE(&blk); blk = next_blk) {
379eda14cbcSMatt Macy 		uint64_t blk_seq = blk.blk_cksum.zc_word[ZIL_ZC_SEQ];
380eda14cbcSMatt Macy 		int reclen;
381eda14cbcSMatt Macy 		char *end = NULL;
382eda14cbcSMatt Macy 
383eda14cbcSMatt Macy 		if (blk_seq > claim_blk_seq)
384eda14cbcSMatt Macy 			break;
385eda14cbcSMatt Macy 
386eda14cbcSMatt Macy 		error = parse_blk_func(zilog, &blk, arg, txg);
387eda14cbcSMatt Macy 		if (error != 0)
388eda14cbcSMatt Macy 			break;
389eda14cbcSMatt Macy 		ASSERT3U(max_blk_seq, <, blk_seq);
390eda14cbcSMatt Macy 		max_blk_seq = blk_seq;
391eda14cbcSMatt Macy 		blk_count++;
392eda14cbcSMatt Macy 
393eda14cbcSMatt Macy 		if (max_lr_seq == claim_lr_seq && max_blk_seq == claim_blk_seq)
394eda14cbcSMatt Macy 			break;
395eda14cbcSMatt Macy 
396eda14cbcSMatt Macy 		error = zil_read_log_block(zilog, decrypt, &blk, &next_blk,
397eda14cbcSMatt Macy 		    lrbuf, &end);
398eda14cbcSMatt Macy 		if (error != 0)
399eda14cbcSMatt Macy 			break;
400eda14cbcSMatt Macy 
401eda14cbcSMatt Macy 		for (lrp = lrbuf; lrp < end; lrp += reclen) {
402eda14cbcSMatt Macy 			lr_t *lr = (lr_t *)lrp;
403eda14cbcSMatt Macy 			reclen = lr->lrc_reclen;
404eda14cbcSMatt Macy 			ASSERT3U(reclen, >=, sizeof (lr_t));
405eda14cbcSMatt Macy 			if (lr->lrc_seq > claim_lr_seq)
406eda14cbcSMatt Macy 				goto done;
407eda14cbcSMatt Macy 
408eda14cbcSMatt Macy 			error = parse_lr_func(zilog, lr, arg, txg);
409eda14cbcSMatt Macy 			if (error != 0)
410eda14cbcSMatt Macy 				goto done;
411eda14cbcSMatt Macy 			ASSERT3U(max_lr_seq, <, lr->lrc_seq);
412eda14cbcSMatt Macy 			max_lr_seq = lr->lrc_seq;
413eda14cbcSMatt Macy 			lr_count++;
414eda14cbcSMatt Macy 		}
415eda14cbcSMatt Macy 	}
416eda14cbcSMatt Macy done:
417eda14cbcSMatt Macy 	zilog->zl_parse_error = error;
418eda14cbcSMatt Macy 	zilog->zl_parse_blk_seq = max_blk_seq;
419eda14cbcSMatt Macy 	zilog->zl_parse_lr_seq = max_lr_seq;
420eda14cbcSMatt Macy 	zilog->zl_parse_blk_count = blk_count;
421eda14cbcSMatt Macy 	zilog->zl_parse_lr_count = lr_count;
422eda14cbcSMatt Macy 
423eda14cbcSMatt Macy 	ASSERT(!claimed || !(zh->zh_flags & ZIL_CLAIM_LR_SEQ_VALID) ||
424eda14cbcSMatt Macy 	    (max_blk_seq == claim_blk_seq && max_lr_seq == claim_lr_seq) ||
425eda14cbcSMatt Macy 	    (decrypt && error == EIO));
426eda14cbcSMatt Macy 
427eda14cbcSMatt Macy 	zil_bp_tree_fini(zilog);
428eda14cbcSMatt Macy 	zio_buf_free(lrbuf, SPA_OLD_MAXBLOCKSIZE);
429eda14cbcSMatt Macy 
430eda14cbcSMatt Macy 	return (error);
431eda14cbcSMatt Macy }
432eda14cbcSMatt Macy 
433eda14cbcSMatt Macy static int
434180f8225SMatt Macy zil_clear_log_block(zilog_t *zilog, const blkptr_t *bp, void *tx,
435180f8225SMatt Macy     uint64_t first_txg)
436eda14cbcSMatt Macy {
437e92ffd9bSMartin Matuska 	(void) tx;
438eda14cbcSMatt Macy 	ASSERT(!BP_IS_HOLE(bp));
439eda14cbcSMatt Macy 
440eda14cbcSMatt Macy 	/*
441eda14cbcSMatt Macy 	 * As we call this function from the context of a rewind to a
442eda14cbcSMatt Macy 	 * checkpoint, each ZIL block whose txg is later than the txg
443eda14cbcSMatt Macy 	 * that we rewind to is invalid. Thus, we return -1 so
444eda14cbcSMatt Macy 	 * zil_parse() doesn't attempt to read it.
445eda14cbcSMatt Macy 	 */
446eda14cbcSMatt Macy 	if (bp->blk_birth >= first_txg)
447eda14cbcSMatt Macy 		return (-1);
448eda14cbcSMatt Macy 
449eda14cbcSMatt Macy 	if (zil_bp_tree_add(zilog, bp) != 0)
450eda14cbcSMatt Macy 		return (0);
451eda14cbcSMatt Macy 
452eda14cbcSMatt Macy 	zio_free(zilog->zl_spa, first_txg, bp);
453eda14cbcSMatt Macy 	return (0);
454eda14cbcSMatt Macy }
455eda14cbcSMatt Macy 
456eda14cbcSMatt Macy static int
457180f8225SMatt Macy zil_noop_log_record(zilog_t *zilog, const lr_t *lrc, void *tx,
458180f8225SMatt Macy     uint64_t first_txg)
459eda14cbcSMatt Macy {
460e92ffd9bSMartin Matuska 	(void) zilog, (void) lrc, (void) tx, (void) first_txg;
461eda14cbcSMatt Macy 	return (0);
462eda14cbcSMatt Macy }
463eda14cbcSMatt Macy 
464eda14cbcSMatt Macy static int
465180f8225SMatt Macy zil_claim_log_block(zilog_t *zilog, const blkptr_t *bp, void *tx,
466180f8225SMatt Macy     uint64_t first_txg)
467eda14cbcSMatt Macy {
468eda14cbcSMatt Macy 	/*
469eda14cbcSMatt Macy 	 * Claim log block if not already committed and not already claimed.
470eda14cbcSMatt Macy 	 * If tx == NULL, just verify that the block is claimable.
471eda14cbcSMatt Macy 	 */
472eda14cbcSMatt Macy 	if (BP_IS_HOLE(bp) || bp->blk_birth < first_txg ||
473eda14cbcSMatt Macy 	    zil_bp_tree_add(zilog, bp) != 0)
474eda14cbcSMatt Macy 		return (0);
475eda14cbcSMatt Macy 
476eda14cbcSMatt Macy 	return (zio_wait(zio_claim(NULL, zilog->zl_spa,
477eda14cbcSMatt Macy 	    tx == NULL ? 0 : first_txg, bp, spa_claim_notify, NULL,
478eda14cbcSMatt Macy 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB)));
479eda14cbcSMatt Macy }
480eda14cbcSMatt Macy 
481eda14cbcSMatt Macy static int
482180f8225SMatt Macy zil_claim_log_record(zilog_t *zilog, const lr_t *lrc, void *tx,
483180f8225SMatt Macy     uint64_t first_txg)
484eda14cbcSMatt Macy {
485eda14cbcSMatt Macy 	lr_write_t *lr = (lr_write_t *)lrc;
486eda14cbcSMatt Macy 	int error;
487eda14cbcSMatt Macy 
488eda14cbcSMatt Macy 	if (lrc->lrc_txtype != TX_WRITE)
489eda14cbcSMatt Macy 		return (0);
490eda14cbcSMatt Macy 
491eda14cbcSMatt Macy 	/*
492eda14cbcSMatt Macy 	 * If the block is not readable, don't claim it.  This can happen
493eda14cbcSMatt Macy 	 * in normal operation when a log block is written to disk before
494eda14cbcSMatt Macy 	 * some of the dmu_sync() blocks it points to.  In this case, the
495eda14cbcSMatt Macy 	 * transaction cannot have been committed to anyone (we would have
496eda14cbcSMatt Macy 	 * waited for all writes to be stable first), so it is semantically
497eda14cbcSMatt Macy 	 * correct to declare this the end of the log.
498eda14cbcSMatt Macy 	 */
499eda14cbcSMatt Macy 	if (lr->lr_blkptr.blk_birth >= first_txg) {
500eda14cbcSMatt Macy 		error = zil_read_log_data(zilog, lr, NULL);
501eda14cbcSMatt Macy 		if (error != 0)
502eda14cbcSMatt Macy 			return (error);
503eda14cbcSMatt Macy 	}
504eda14cbcSMatt Macy 
505eda14cbcSMatt Macy 	return (zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg));
506eda14cbcSMatt Macy }
507eda14cbcSMatt Macy 
508eda14cbcSMatt Macy static int
509180f8225SMatt Macy zil_free_log_block(zilog_t *zilog, const blkptr_t *bp, void *tx,
510180f8225SMatt Macy     uint64_t claim_txg)
511eda14cbcSMatt Macy {
512e92ffd9bSMartin Matuska 	(void) claim_txg;
513e92ffd9bSMartin Matuska 
514eda14cbcSMatt Macy 	zio_free(zilog->zl_spa, dmu_tx_get_txg(tx), bp);
515eda14cbcSMatt Macy 
516eda14cbcSMatt Macy 	return (0);
517eda14cbcSMatt Macy }
518eda14cbcSMatt Macy 
519eda14cbcSMatt Macy static int
520180f8225SMatt Macy zil_free_log_record(zilog_t *zilog, const lr_t *lrc, void *tx,
521180f8225SMatt Macy     uint64_t claim_txg)
522eda14cbcSMatt Macy {
523eda14cbcSMatt Macy 	lr_write_t *lr = (lr_write_t *)lrc;
524eda14cbcSMatt Macy 	blkptr_t *bp = &lr->lr_blkptr;
525eda14cbcSMatt Macy 
526eda14cbcSMatt Macy 	/*
527eda14cbcSMatt Macy 	 * If we previously claimed it, we need to free it.
528eda14cbcSMatt Macy 	 */
529eda14cbcSMatt Macy 	if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE &&
530eda14cbcSMatt Macy 	    bp->blk_birth >= claim_txg && zil_bp_tree_add(zilog, bp) == 0 &&
531eda14cbcSMatt Macy 	    !BP_IS_HOLE(bp))
532eda14cbcSMatt Macy 		zio_free(zilog->zl_spa, dmu_tx_get_txg(tx), bp);
533eda14cbcSMatt Macy 
534eda14cbcSMatt Macy 	return (0);
535eda14cbcSMatt Macy }
536eda14cbcSMatt Macy 
537eda14cbcSMatt Macy static int
538eda14cbcSMatt Macy zil_lwb_vdev_compare(const void *x1, const void *x2)
539eda14cbcSMatt Macy {
540eda14cbcSMatt Macy 	const uint64_t v1 = ((zil_vdev_node_t *)x1)->zv_vdev;
541eda14cbcSMatt Macy 	const uint64_t v2 = ((zil_vdev_node_t *)x2)->zv_vdev;
542eda14cbcSMatt Macy 
543eda14cbcSMatt Macy 	return (TREE_CMP(v1, v2));
544eda14cbcSMatt Macy }
545eda14cbcSMatt Macy 
546eda14cbcSMatt Macy static lwb_t *
547eda14cbcSMatt Macy zil_alloc_lwb(zilog_t *zilog, blkptr_t *bp, boolean_t slog, uint64_t txg,
548eda14cbcSMatt Macy     boolean_t fastwrite)
549eda14cbcSMatt Macy {
550eda14cbcSMatt Macy 	lwb_t *lwb;
551eda14cbcSMatt Macy 
552eda14cbcSMatt Macy 	lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP);
553eda14cbcSMatt Macy 	lwb->lwb_zilog = zilog;
554eda14cbcSMatt Macy 	lwb->lwb_blk = *bp;
555eda14cbcSMatt Macy 	lwb->lwb_fastwrite = fastwrite;
556eda14cbcSMatt Macy 	lwb->lwb_slog = slog;
557eda14cbcSMatt Macy 	lwb->lwb_state = LWB_STATE_CLOSED;
558eda14cbcSMatt Macy 	lwb->lwb_buf = zio_buf_alloc(BP_GET_LSIZE(bp));
559eda14cbcSMatt Macy 	lwb->lwb_max_txg = txg;
560eda14cbcSMatt Macy 	lwb->lwb_write_zio = NULL;
561eda14cbcSMatt Macy 	lwb->lwb_root_zio = NULL;
562eda14cbcSMatt Macy 	lwb->lwb_issued_timestamp = 0;
563e3aa18adSMartin Matuska 	lwb->lwb_issued_txg = 0;
564eda14cbcSMatt Macy 	if (BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_ZILOG2) {
565eda14cbcSMatt Macy 		lwb->lwb_nused = sizeof (zil_chain_t);
566eda14cbcSMatt Macy 		lwb->lwb_sz = BP_GET_LSIZE(bp);
567eda14cbcSMatt Macy 	} else {
568eda14cbcSMatt Macy 		lwb->lwb_nused = 0;
569eda14cbcSMatt Macy 		lwb->lwb_sz = BP_GET_LSIZE(bp) - sizeof (zil_chain_t);
570eda14cbcSMatt Macy 	}
571eda14cbcSMatt Macy 
572eda14cbcSMatt Macy 	mutex_enter(&zilog->zl_lock);
573eda14cbcSMatt Macy 	list_insert_tail(&zilog->zl_lwb_list, lwb);
574eda14cbcSMatt Macy 	mutex_exit(&zilog->zl_lock);
575eda14cbcSMatt Macy 
576eda14cbcSMatt Macy 	ASSERT(!MUTEX_HELD(&lwb->lwb_vdev_lock));
577eda14cbcSMatt Macy 	ASSERT(avl_is_empty(&lwb->lwb_vdev_tree));
578eda14cbcSMatt Macy 	VERIFY(list_is_empty(&lwb->lwb_waiters));
579eda14cbcSMatt Macy 	VERIFY(list_is_empty(&lwb->lwb_itxs));
580eda14cbcSMatt Macy 
581eda14cbcSMatt Macy 	return (lwb);
582eda14cbcSMatt Macy }
583eda14cbcSMatt Macy 
584eda14cbcSMatt Macy static void
585eda14cbcSMatt Macy zil_free_lwb(zilog_t *zilog, lwb_t *lwb)
586eda14cbcSMatt Macy {
587eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&zilog->zl_lock));
588eda14cbcSMatt Macy 	ASSERT(!MUTEX_HELD(&lwb->lwb_vdev_lock));
589eda14cbcSMatt Macy 	VERIFY(list_is_empty(&lwb->lwb_waiters));
590eda14cbcSMatt Macy 	VERIFY(list_is_empty(&lwb->lwb_itxs));
591eda14cbcSMatt Macy 	ASSERT(avl_is_empty(&lwb->lwb_vdev_tree));
592eda14cbcSMatt Macy 	ASSERT3P(lwb->lwb_write_zio, ==, NULL);
593eda14cbcSMatt Macy 	ASSERT3P(lwb->lwb_root_zio, ==, NULL);
594eda14cbcSMatt Macy 	ASSERT3U(lwb->lwb_max_txg, <=, spa_syncing_txg(zilog->zl_spa));
595eda14cbcSMatt Macy 	ASSERT(lwb->lwb_state == LWB_STATE_CLOSED ||
596eda14cbcSMatt Macy 	    lwb->lwb_state == LWB_STATE_FLUSH_DONE);
597eda14cbcSMatt Macy 
598eda14cbcSMatt Macy 	/*
599eda14cbcSMatt Macy 	 * Clear the zilog's field to indicate this lwb is no longer
600eda14cbcSMatt Macy 	 * valid, and prevent use-after-free errors.
601eda14cbcSMatt Macy 	 */
602eda14cbcSMatt Macy 	if (zilog->zl_last_lwb_opened == lwb)
603eda14cbcSMatt Macy 		zilog->zl_last_lwb_opened = NULL;
604eda14cbcSMatt Macy 
605eda14cbcSMatt Macy 	kmem_cache_free(zil_lwb_cache, lwb);
606eda14cbcSMatt Macy }
607eda14cbcSMatt Macy 
608eda14cbcSMatt Macy /*
609eda14cbcSMatt Macy  * Called when we create in-memory log transactions so that we know
610eda14cbcSMatt Macy  * to cleanup the itxs at the end of spa_sync().
611eda14cbcSMatt Macy  */
612eda14cbcSMatt Macy static void
613eda14cbcSMatt Macy zilog_dirty(zilog_t *zilog, uint64_t txg)
614eda14cbcSMatt Macy {
615eda14cbcSMatt Macy 	dsl_pool_t *dp = zilog->zl_dmu_pool;
616eda14cbcSMatt Macy 	dsl_dataset_t *ds = dmu_objset_ds(zilog->zl_os);
617eda14cbcSMatt Macy 
618eda14cbcSMatt Macy 	ASSERT(spa_writeable(zilog->zl_spa));
619eda14cbcSMatt Macy 
620eda14cbcSMatt Macy 	if (ds->ds_is_snapshot)
621eda14cbcSMatt Macy 		panic("dirtying snapshot!");
622eda14cbcSMatt Macy 
623eda14cbcSMatt Macy 	if (txg_list_add(&dp->dp_dirty_zilogs, zilog, txg)) {
624eda14cbcSMatt Macy 		/* up the hold count until we can be written out */
625eda14cbcSMatt Macy 		dmu_buf_add_ref(ds->ds_dbuf, zilog);
626eda14cbcSMatt Macy 
627eda14cbcSMatt Macy 		zilog->zl_dirty_max_txg = MAX(txg, zilog->zl_dirty_max_txg);
628eda14cbcSMatt Macy 	}
629eda14cbcSMatt Macy }
630eda14cbcSMatt Macy 
631eda14cbcSMatt Macy /*
632eda14cbcSMatt Macy  * Determine if the zil is dirty in the specified txg. Callers wanting to
633eda14cbcSMatt Macy  * ensure that the dirty state does not change must hold the itxg_lock for
634eda14cbcSMatt Macy  * the specified txg. Holding the lock will ensure that the zil cannot be
635eda14cbcSMatt Macy  * dirtied (zil_itx_assign) or cleaned (zil_clean) while we check its current
636eda14cbcSMatt Macy  * state.
637eda14cbcSMatt Macy  */
638eda14cbcSMatt Macy static boolean_t __maybe_unused
639eda14cbcSMatt Macy zilog_is_dirty_in_txg(zilog_t *zilog, uint64_t txg)
640eda14cbcSMatt Macy {
641eda14cbcSMatt Macy 	dsl_pool_t *dp = zilog->zl_dmu_pool;
642eda14cbcSMatt Macy 
643eda14cbcSMatt Macy 	if (txg_list_member(&dp->dp_dirty_zilogs, zilog, txg & TXG_MASK))
644eda14cbcSMatt Macy 		return (B_TRUE);
645eda14cbcSMatt Macy 	return (B_FALSE);
646eda14cbcSMatt Macy }
647eda14cbcSMatt Macy 
648eda14cbcSMatt Macy /*
649eda14cbcSMatt Macy  * Determine if the zil is dirty. The zil is considered dirty if it has
650eda14cbcSMatt Macy  * any pending itx records that have not been cleaned by zil_clean().
651eda14cbcSMatt Macy  */
652eda14cbcSMatt Macy static boolean_t
653eda14cbcSMatt Macy zilog_is_dirty(zilog_t *zilog)
654eda14cbcSMatt Macy {
655eda14cbcSMatt Macy 	dsl_pool_t *dp = zilog->zl_dmu_pool;
656eda14cbcSMatt Macy 
657eda14cbcSMatt Macy 	for (int t = 0; t < TXG_SIZE; t++) {
658eda14cbcSMatt Macy 		if (txg_list_member(&dp->dp_dirty_zilogs, zilog, t))
659eda14cbcSMatt Macy 			return (B_TRUE);
660eda14cbcSMatt Macy 	}
661eda14cbcSMatt Macy 	return (B_FALSE);
662eda14cbcSMatt Macy }
663eda14cbcSMatt Macy 
664eda14cbcSMatt Macy /*
665c03c5b1cSMartin Matuska  * Its called in zil_commit context (zil_process_commit_list()/zil_create()).
666c03c5b1cSMartin Matuska  * It activates SPA_FEATURE_ZILSAXATTR feature, if its enabled.
667c03c5b1cSMartin Matuska  * Check dsl_dataset_feature_is_active to avoid txg_wait_synced() on every
668c03c5b1cSMartin Matuska  * zil_commit.
669c03c5b1cSMartin Matuska  */
670c03c5b1cSMartin Matuska static void
671c03c5b1cSMartin Matuska zil_commit_activate_saxattr_feature(zilog_t *zilog)
672c03c5b1cSMartin Matuska {
673c03c5b1cSMartin Matuska 	dsl_dataset_t *ds = dmu_objset_ds(zilog->zl_os);
674c03c5b1cSMartin Matuska 	uint64_t txg = 0;
675c03c5b1cSMartin Matuska 	dmu_tx_t *tx = NULL;
676c03c5b1cSMartin Matuska 
677c03c5b1cSMartin Matuska 	if (spa_feature_is_enabled(zilog->zl_spa,
678c03c5b1cSMartin Matuska 	    SPA_FEATURE_ZILSAXATTR) &&
679c03c5b1cSMartin Matuska 	    dmu_objset_type(zilog->zl_os) != DMU_OST_ZVOL &&
680c03c5b1cSMartin Matuska 	    !dsl_dataset_feature_is_active(ds,
681c03c5b1cSMartin Matuska 	    SPA_FEATURE_ZILSAXATTR)) {
682c03c5b1cSMartin Matuska 		tx = dmu_tx_create(zilog->zl_os);
683c03c5b1cSMartin Matuska 		VERIFY0(dmu_tx_assign(tx, TXG_WAIT));
684c03c5b1cSMartin Matuska 		dsl_dataset_dirty(ds, tx);
685c03c5b1cSMartin Matuska 		txg = dmu_tx_get_txg(tx);
686c03c5b1cSMartin Matuska 
687c03c5b1cSMartin Matuska 		mutex_enter(&ds->ds_lock);
688c03c5b1cSMartin Matuska 		ds->ds_feature_activation[SPA_FEATURE_ZILSAXATTR] =
689c03c5b1cSMartin Matuska 		    (void *)B_TRUE;
690c03c5b1cSMartin Matuska 		mutex_exit(&ds->ds_lock);
691c03c5b1cSMartin Matuska 		dmu_tx_commit(tx);
692c03c5b1cSMartin Matuska 		txg_wait_synced(zilog->zl_dmu_pool, txg);
693c03c5b1cSMartin Matuska 	}
694c03c5b1cSMartin Matuska }
695c03c5b1cSMartin Matuska 
696c03c5b1cSMartin Matuska /*
697eda14cbcSMatt Macy  * Create an on-disk intent log.
698eda14cbcSMatt Macy  */
699eda14cbcSMatt Macy static lwb_t *
700eda14cbcSMatt Macy zil_create(zilog_t *zilog)
701eda14cbcSMatt Macy {
702eda14cbcSMatt Macy 	const zil_header_t *zh = zilog->zl_header;
703eda14cbcSMatt Macy 	lwb_t *lwb = NULL;
704eda14cbcSMatt Macy 	uint64_t txg = 0;
705eda14cbcSMatt Macy 	dmu_tx_t *tx = NULL;
706eda14cbcSMatt Macy 	blkptr_t blk;
707eda14cbcSMatt Macy 	int error = 0;
708eda14cbcSMatt Macy 	boolean_t fastwrite = FALSE;
709eda14cbcSMatt Macy 	boolean_t slog = FALSE;
710c03c5b1cSMartin Matuska 	dsl_dataset_t *ds = dmu_objset_ds(zilog->zl_os);
711c03c5b1cSMartin Matuska 
712eda14cbcSMatt Macy 
713eda14cbcSMatt Macy 	/*
714eda14cbcSMatt Macy 	 * Wait for any previous destroy to complete.
715eda14cbcSMatt Macy 	 */
716eda14cbcSMatt Macy 	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
717eda14cbcSMatt Macy 
718eda14cbcSMatt Macy 	ASSERT(zh->zh_claim_txg == 0);
719eda14cbcSMatt Macy 	ASSERT(zh->zh_replay_seq == 0);
720eda14cbcSMatt Macy 
721eda14cbcSMatt Macy 	blk = zh->zh_log;
722eda14cbcSMatt Macy 
723eda14cbcSMatt Macy 	/*
724eda14cbcSMatt Macy 	 * Allocate an initial log block if:
725eda14cbcSMatt Macy 	 *    - there isn't one already
726eda14cbcSMatt Macy 	 *    - the existing block is the wrong endianness
727eda14cbcSMatt Macy 	 */
728eda14cbcSMatt Macy 	if (BP_IS_HOLE(&blk) || BP_SHOULD_BYTESWAP(&blk)) {
729eda14cbcSMatt Macy 		tx = dmu_tx_create(zilog->zl_os);
730eda14cbcSMatt Macy 		VERIFY0(dmu_tx_assign(tx, TXG_WAIT));
731eda14cbcSMatt Macy 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
732eda14cbcSMatt Macy 		txg = dmu_tx_get_txg(tx);
733eda14cbcSMatt Macy 
734eda14cbcSMatt Macy 		if (!BP_IS_HOLE(&blk)) {
735eda14cbcSMatt Macy 			zio_free(zilog->zl_spa, txg, &blk);
736eda14cbcSMatt Macy 			BP_ZERO(&blk);
737eda14cbcSMatt Macy 		}
738eda14cbcSMatt Macy 
739eda14cbcSMatt Macy 		error = zio_alloc_zil(zilog->zl_spa, zilog->zl_os, txg, &blk,
740eda14cbcSMatt Macy 		    ZIL_MIN_BLKSZ, &slog);
741eda14cbcSMatt Macy 		fastwrite = TRUE;
742eda14cbcSMatt Macy 
743eda14cbcSMatt Macy 		if (error == 0)
744eda14cbcSMatt Macy 			zil_init_log_chain(zilog, &blk);
745eda14cbcSMatt Macy 	}
746eda14cbcSMatt Macy 
747eda14cbcSMatt Macy 	/*
748eda14cbcSMatt Macy 	 * Allocate a log write block (lwb) for the first log block.
749eda14cbcSMatt Macy 	 */
750eda14cbcSMatt Macy 	if (error == 0)
751eda14cbcSMatt Macy 		lwb = zil_alloc_lwb(zilog, &blk, slog, txg, fastwrite);
752eda14cbcSMatt Macy 
753eda14cbcSMatt Macy 	/*
754eda14cbcSMatt Macy 	 * If we just allocated the first log block, commit our transaction
755eda14cbcSMatt Macy 	 * and wait for zil_sync() to stuff the block pointer into zh_log.
756eda14cbcSMatt Macy 	 * (zh is part of the MOS, so we cannot modify it in open context.)
757eda14cbcSMatt Macy 	 */
758eda14cbcSMatt Macy 	if (tx != NULL) {
759c03c5b1cSMartin Matuska 		/*
760c03c5b1cSMartin Matuska 		 * If "zilsaxattr" feature is enabled on zpool, then activate
761c03c5b1cSMartin Matuska 		 * it now when we're creating the ZIL chain. We can't wait with
762c03c5b1cSMartin Matuska 		 * this until we write the first xattr log record because we
763c03c5b1cSMartin Matuska 		 * need to wait for the feature activation to sync out.
764c03c5b1cSMartin Matuska 		 */
765c03c5b1cSMartin Matuska 		if (spa_feature_is_enabled(zilog->zl_spa,
766c03c5b1cSMartin Matuska 		    SPA_FEATURE_ZILSAXATTR) && dmu_objset_type(zilog->zl_os) !=
767c03c5b1cSMartin Matuska 		    DMU_OST_ZVOL) {
768c03c5b1cSMartin Matuska 			mutex_enter(&ds->ds_lock);
769c03c5b1cSMartin Matuska 			ds->ds_feature_activation[SPA_FEATURE_ZILSAXATTR] =
770c03c5b1cSMartin Matuska 			    (void *)B_TRUE;
771c03c5b1cSMartin Matuska 			mutex_exit(&ds->ds_lock);
772c03c5b1cSMartin Matuska 		}
773c03c5b1cSMartin Matuska 
774eda14cbcSMatt Macy 		dmu_tx_commit(tx);
775eda14cbcSMatt Macy 		txg_wait_synced(zilog->zl_dmu_pool, txg);
776c03c5b1cSMartin Matuska 	} else {
777c03c5b1cSMartin Matuska 		/*
778c03c5b1cSMartin Matuska 		 * This branch covers the case where we enable the feature on a
779c03c5b1cSMartin Matuska 		 * zpool that has existing ZIL headers.
780c03c5b1cSMartin Matuska 		 */
781c03c5b1cSMartin Matuska 		zil_commit_activate_saxattr_feature(zilog);
782eda14cbcSMatt Macy 	}
783c03c5b1cSMartin Matuska 	IMPLY(spa_feature_is_enabled(zilog->zl_spa, SPA_FEATURE_ZILSAXATTR) &&
784c03c5b1cSMartin Matuska 	    dmu_objset_type(zilog->zl_os) != DMU_OST_ZVOL,
785c03c5b1cSMartin Matuska 	    dsl_dataset_feature_is_active(ds, SPA_FEATURE_ZILSAXATTR));
786eda14cbcSMatt Macy 
787da5137abSMartin Matuska 	ASSERT(error != 0 || memcmp(&blk, &zh->zh_log, sizeof (blk)) == 0);
788eda14cbcSMatt Macy 	IMPLY(error == 0, lwb != NULL);
789eda14cbcSMatt Macy 
790eda14cbcSMatt Macy 	return (lwb);
791eda14cbcSMatt Macy }
792eda14cbcSMatt Macy 
793eda14cbcSMatt Macy /*
794eda14cbcSMatt Macy  * In one tx, free all log blocks and clear the log header. If keep_first
795eda14cbcSMatt Macy  * is set, then we're replaying a log with no content. We want to keep the
796eda14cbcSMatt Macy  * first block, however, so that the first synchronous transaction doesn't
797eda14cbcSMatt Macy  * require a txg_wait_synced() in zil_create(). We don't need to
798eda14cbcSMatt Macy  * txg_wait_synced() here either when keep_first is set, because both
799eda14cbcSMatt Macy  * zil_create() and zil_destroy() will wait for any in-progress destroys
800eda14cbcSMatt Macy  * to complete.
801eda14cbcSMatt Macy  */
802eda14cbcSMatt Macy void
803eda14cbcSMatt Macy zil_destroy(zilog_t *zilog, boolean_t keep_first)
804eda14cbcSMatt Macy {
805eda14cbcSMatt Macy 	const zil_header_t *zh = zilog->zl_header;
806eda14cbcSMatt Macy 	lwb_t *lwb;
807eda14cbcSMatt Macy 	dmu_tx_t *tx;
808eda14cbcSMatt Macy 	uint64_t txg;
809eda14cbcSMatt Macy 
810eda14cbcSMatt Macy 	/*
811eda14cbcSMatt Macy 	 * Wait for any previous destroy to complete.
812eda14cbcSMatt Macy 	 */
813eda14cbcSMatt Macy 	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
814eda14cbcSMatt Macy 
815eda14cbcSMatt Macy 	zilog->zl_old_header = *zh;		/* debugging aid */
816eda14cbcSMatt Macy 
817eda14cbcSMatt Macy 	if (BP_IS_HOLE(&zh->zh_log))
818eda14cbcSMatt Macy 		return;
819eda14cbcSMatt Macy 
820eda14cbcSMatt Macy 	tx = dmu_tx_create(zilog->zl_os);
821eda14cbcSMatt Macy 	VERIFY0(dmu_tx_assign(tx, TXG_WAIT));
822eda14cbcSMatt Macy 	dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
823eda14cbcSMatt Macy 	txg = dmu_tx_get_txg(tx);
824eda14cbcSMatt Macy 
825eda14cbcSMatt Macy 	mutex_enter(&zilog->zl_lock);
826eda14cbcSMatt Macy 
827eda14cbcSMatt Macy 	ASSERT3U(zilog->zl_destroy_txg, <, txg);
828eda14cbcSMatt Macy 	zilog->zl_destroy_txg = txg;
829eda14cbcSMatt Macy 	zilog->zl_keep_first = keep_first;
830eda14cbcSMatt Macy 
831eda14cbcSMatt Macy 	if (!list_is_empty(&zilog->zl_lwb_list)) {
832eda14cbcSMatt Macy 		ASSERT(zh->zh_claim_txg == 0);
833eda14cbcSMatt Macy 		VERIFY(!keep_first);
834eda14cbcSMatt Macy 		while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
835eda14cbcSMatt Macy 			if (lwb->lwb_fastwrite)
836eda14cbcSMatt Macy 				metaslab_fastwrite_unmark(zilog->zl_spa,
837eda14cbcSMatt Macy 				    &lwb->lwb_blk);
838eda14cbcSMatt Macy 
839eda14cbcSMatt Macy 			list_remove(&zilog->zl_lwb_list, lwb);
840eda14cbcSMatt Macy 			if (lwb->lwb_buf != NULL)
841eda14cbcSMatt Macy 				zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
842eda14cbcSMatt Macy 			zio_free(zilog->zl_spa, txg, &lwb->lwb_blk);
843eda14cbcSMatt Macy 			zil_free_lwb(zilog, lwb);
844eda14cbcSMatt Macy 		}
845eda14cbcSMatt Macy 	} else if (!keep_first) {
846eda14cbcSMatt Macy 		zil_destroy_sync(zilog, tx);
847eda14cbcSMatt Macy 	}
848eda14cbcSMatt Macy 	mutex_exit(&zilog->zl_lock);
849eda14cbcSMatt Macy 
850eda14cbcSMatt Macy 	dmu_tx_commit(tx);
851eda14cbcSMatt Macy }
852eda14cbcSMatt Macy 
853eda14cbcSMatt Macy void
854eda14cbcSMatt Macy zil_destroy_sync(zilog_t *zilog, dmu_tx_t *tx)
855eda14cbcSMatt Macy {
856eda14cbcSMatt Macy 	ASSERT(list_is_empty(&zilog->zl_lwb_list));
857eda14cbcSMatt Macy 	(void) zil_parse(zilog, zil_free_log_block,
858eda14cbcSMatt Macy 	    zil_free_log_record, tx, zilog->zl_header->zh_claim_txg, B_FALSE);
859eda14cbcSMatt Macy }
860eda14cbcSMatt Macy 
861eda14cbcSMatt Macy int
862eda14cbcSMatt Macy zil_claim(dsl_pool_t *dp, dsl_dataset_t *ds, void *txarg)
863eda14cbcSMatt Macy {
864eda14cbcSMatt Macy 	dmu_tx_t *tx = txarg;
865eda14cbcSMatt Macy 	zilog_t *zilog;
866eda14cbcSMatt Macy 	uint64_t first_txg;
867eda14cbcSMatt Macy 	zil_header_t *zh;
868eda14cbcSMatt Macy 	objset_t *os;
869eda14cbcSMatt Macy 	int error;
870eda14cbcSMatt Macy 
871eda14cbcSMatt Macy 	error = dmu_objset_own_obj(dp, ds->ds_object,
872eda14cbcSMatt Macy 	    DMU_OST_ANY, B_FALSE, B_FALSE, FTAG, &os);
873eda14cbcSMatt Macy 	if (error != 0) {
874eda14cbcSMatt Macy 		/*
875eda14cbcSMatt Macy 		 * EBUSY indicates that the objset is inconsistent, in which
876eda14cbcSMatt Macy 		 * case it can not have a ZIL.
877eda14cbcSMatt Macy 		 */
878eda14cbcSMatt Macy 		if (error != EBUSY) {
879eda14cbcSMatt Macy 			cmn_err(CE_WARN, "can't open objset for %llu, error %u",
880eda14cbcSMatt Macy 			    (unsigned long long)ds->ds_object, error);
881eda14cbcSMatt Macy 		}
882eda14cbcSMatt Macy 
883eda14cbcSMatt Macy 		return (0);
884eda14cbcSMatt Macy 	}
885eda14cbcSMatt Macy 
886eda14cbcSMatt Macy 	zilog = dmu_objset_zil(os);
887eda14cbcSMatt Macy 	zh = zil_header_in_syncing_context(zilog);
888eda14cbcSMatt Macy 	ASSERT3U(tx->tx_txg, ==, spa_first_txg(zilog->zl_spa));
889eda14cbcSMatt Macy 	first_txg = spa_min_claim_txg(zilog->zl_spa);
890eda14cbcSMatt Macy 
891eda14cbcSMatt Macy 	/*
892eda14cbcSMatt Macy 	 * If the spa_log_state is not set to be cleared, check whether
893eda14cbcSMatt Macy 	 * the current uberblock is a checkpoint one and if the current
894eda14cbcSMatt Macy 	 * header has been claimed before moving on.
895eda14cbcSMatt Macy 	 *
896eda14cbcSMatt Macy 	 * If the current uberblock is a checkpointed uberblock then
897eda14cbcSMatt Macy 	 * one of the following scenarios took place:
898eda14cbcSMatt Macy 	 *
899eda14cbcSMatt Macy 	 * 1] We are currently rewinding to the checkpoint of the pool.
900eda14cbcSMatt Macy 	 * 2] We crashed in the middle of a checkpoint rewind but we
901eda14cbcSMatt Macy 	 *    did manage to write the checkpointed uberblock to the
902eda14cbcSMatt Macy 	 *    vdev labels, so when we tried to import the pool again
903eda14cbcSMatt Macy 	 *    the checkpointed uberblock was selected from the import
904eda14cbcSMatt Macy 	 *    procedure.
905eda14cbcSMatt Macy 	 *
906eda14cbcSMatt Macy 	 * In both cases we want to zero out all the ZIL blocks, except
907eda14cbcSMatt Macy 	 * the ones that have been claimed at the time of the checkpoint
908eda14cbcSMatt Macy 	 * (their zh_claim_txg != 0). The reason is that these blocks
909eda14cbcSMatt Macy 	 * may be corrupted since we may have reused their locations on
910eda14cbcSMatt Macy 	 * disk after we took the checkpoint.
911eda14cbcSMatt Macy 	 *
912eda14cbcSMatt Macy 	 * We could try to set spa_log_state to SPA_LOG_CLEAR earlier
913eda14cbcSMatt Macy 	 * when we first figure out whether the current uberblock is
914eda14cbcSMatt Macy 	 * checkpointed or not. Unfortunately, that would discard all
915eda14cbcSMatt Macy 	 * the logs, including the ones that are claimed, and we would
916eda14cbcSMatt Macy 	 * leak space.
917eda14cbcSMatt Macy 	 */
918eda14cbcSMatt Macy 	if (spa_get_log_state(zilog->zl_spa) == SPA_LOG_CLEAR ||
919eda14cbcSMatt Macy 	    (zilog->zl_spa->spa_uberblock.ub_checkpoint_txg != 0 &&
920eda14cbcSMatt Macy 	    zh->zh_claim_txg == 0)) {
921eda14cbcSMatt Macy 		if (!BP_IS_HOLE(&zh->zh_log)) {
922eda14cbcSMatt Macy 			(void) zil_parse(zilog, zil_clear_log_block,
923eda14cbcSMatt Macy 			    zil_noop_log_record, tx, first_txg, B_FALSE);
924eda14cbcSMatt Macy 		}
925eda14cbcSMatt Macy 		BP_ZERO(&zh->zh_log);
926eda14cbcSMatt Macy 		if (os->os_encrypted)
927eda14cbcSMatt Macy 			os->os_next_write_raw[tx->tx_txg & TXG_MASK] = B_TRUE;
928eda14cbcSMatt Macy 		dsl_dataset_dirty(dmu_objset_ds(os), tx);
929eda14cbcSMatt Macy 		dmu_objset_disown(os, B_FALSE, FTAG);
930eda14cbcSMatt Macy 		return (0);
931eda14cbcSMatt Macy 	}
932eda14cbcSMatt Macy 
933eda14cbcSMatt Macy 	/*
934eda14cbcSMatt Macy 	 * If we are not rewinding and opening the pool normally, then
935eda14cbcSMatt Macy 	 * the min_claim_txg should be equal to the first txg of the pool.
936eda14cbcSMatt Macy 	 */
937eda14cbcSMatt Macy 	ASSERT3U(first_txg, ==, spa_first_txg(zilog->zl_spa));
938eda14cbcSMatt Macy 
939eda14cbcSMatt Macy 	/*
940eda14cbcSMatt Macy 	 * Claim all log blocks if we haven't already done so, and remember
941eda14cbcSMatt Macy 	 * the highest claimed sequence number.  This ensures that if we can
942eda14cbcSMatt Macy 	 * read only part of the log now (e.g. due to a missing device),
943eda14cbcSMatt Macy 	 * but we can read the entire log later, we will not try to replay
944eda14cbcSMatt Macy 	 * or destroy beyond the last block we successfully claimed.
945eda14cbcSMatt Macy 	 */
946eda14cbcSMatt Macy 	ASSERT3U(zh->zh_claim_txg, <=, first_txg);
947eda14cbcSMatt Macy 	if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) {
948eda14cbcSMatt Macy 		(void) zil_parse(zilog, zil_claim_log_block,
949eda14cbcSMatt Macy 		    zil_claim_log_record, tx, first_txg, B_FALSE);
950eda14cbcSMatt Macy 		zh->zh_claim_txg = first_txg;
951eda14cbcSMatt Macy 		zh->zh_claim_blk_seq = zilog->zl_parse_blk_seq;
952eda14cbcSMatt Macy 		zh->zh_claim_lr_seq = zilog->zl_parse_lr_seq;
953eda14cbcSMatt Macy 		if (zilog->zl_parse_lr_count || zilog->zl_parse_blk_count > 1)
954eda14cbcSMatt Macy 			zh->zh_flags |= ZIL_REPLAY_NEEDED;
955eda14cbcSMatt Macy 		zh->zh_flags |= ZIL_CLAIM_LR_SEQ_VALID;
956eda14cbcSMatt Macy 		if (os->os_encrypted)
957eda14cbcSMatt Macy 			os->os_next_write_raw[tx->tx_txg & TXG_MASK] = B_TRUE;
958eda14cbcSMatt Macy 		dsl_dataset_dirty(dmu_objset_ds(os), tx);
959eda14cbcSMatt Macy 	}
960eda14cbcSMatt Macy 
961eda14cbcSMatt Macy 	ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1));
962eda14cbcSMatt Macy 	dmu_objset_disown(os, B_FALSE, FTAG);
963eda14cbcSMatt Macy 	return (0);
964eda14cbcSMatt Macy }
965eda14cbcSMatt Macy 
966eda14cbcSMatt Macy /*
967eda14cbcSMatt Macy  * Check the log by walking the log chain.
968eda14cbcSMatt Macy  * Checksum errors are ok as they indicate the end of the chain.
969eda14cbcSMatt Macy  * Any other error (no device or read failure) returns an error.
970eda14cbcSMatt Macy  */
971eda14cbcSMatt Macy int
972eda14cbcSMatt Macy zil_check_log_chain(dsl_pool_t *dp, dsl_dataset_t *ds, void *tx)
973eda14cbcSMatt Macy {
974e92ffd9bSMartin Matuska 	(void) dp;
975eda14cbcSMatt Macy 	zilog_t *zilog;
976eda14cbcSMatt Macy 	objset_t *os;
977eda14cbcSMatt Macy 	blkptr_t *bp;
978eda14cbcSMatt Macy 	int error;
979eda14cbcSMatt Macy 
980eda14cbcSMatt Macy 	ASSERT(tx == NULL);
981eda14cbcSMatt Macy 
982eda14cbcSMatt Macy 	error = dmu_objset_from_ds(ds, &os);
983eda14cbcSMatt Macy 	if (error != 0) {
984eda14cbcSMatt Macy 		cmn_err(CE_WARN, "can't open objset %llu, error %d",
985eda14cbcSMatt Macy 		    (unsigned long long)ds->ds_object, error);
986eda14cbcSMatt Macy 		return (0);
987eda14cbcSMatt Macy 	}
988eda14cbcSMatt Macy 
989eda14cbcSMatt Macy 	zilog = dmu_objset_zil(os);
990eda14cbcSMatt Macy 	bp = (blkptr_t *)&zilog->zl_header->zh_log;
991eda14cbcSMatt Macy 
992eda14cbcSMatt Macy 	if (!BP_IS_HOLE(bp)) {
993eda14cbcSMatt Macy 		vdev_t *vd;
994eda14cbcSMatt Macy 		boolean_t valid = B_TRUE;
995eda14cbcSMatt Macy 
996eda14cbcSMatt Macy 		/*
997eda14cbcSMatt Macy 		 * Check the first block and determine if it's on a log device
998eda14cbcSMatt Macy 		 * which may have been removed or faulted prior to loading this
999eda14cbcSMatt Macy 		 * pool.  If so, there's no point in checking the rest of the
1000eda14cbcSMatt Macy 		 * log as its content should have already been synced to the
1001eda14cbcSMatt Macy 		 * pool.
1002eda14cbcSMatt Macy 		 */
1003eda14cbcSMatt Macy 		spa_config_enter(os->os_spa, SCL_STATE, FTAG, RW_READER);
1004eda14cbcSMatt Macy 		vd = vdev_lookup_top(os->os_spa, DVA_GET_VDEV(&bp->blk_dva[0]));
1005eda14cbcSMatt Macy 		if (vd->vdev_islog && vdev_is_dead(vd))
1006eda14cbcSMatt Macy 			valid = vdev_log_state_valid(vd);
1007eda14cbcSMatt Macy 		spa_config_exit(os->os_spa, SCL_STATE, FTAG);
1008eda14cbcSMatt Macy 
1009eda14cbcSMatt Macy 		if (!valid)
1010eda14cbcSMatt Macy 			return (0);
1011eda14cbcSMatt Macy 
1012eda14cbcSMatt Macy 		/*
1013eda14cbcSMatt Macy 		 * Check whether the current uberblock is checkpointed (e.g.
1014eda14cbcSMatt Macy 		 * we are rewinding) and whether the current header has been
1015eda14cbcSMatt Macy 		 * claimed or not. If it hasn't then skip verifying it. We
1016eda14cbcSMatt Macy 		 * do this because its ZIL blocks may be part of the pool's
1017eda14cbcSMatt Macy 		 * state before the rewind, which is no longer valid.
1018eda14cbcSMatt Macy 		 */
1019eda14cbcSMatt Macy 		zil_header_t *zh = zil_header_in_syncing_context(zilog);
1020eda14cbcSMatt Macy 		if (zilog->zl_spa->spa_uberblock.ub_checkpoint_txg != 0 &&
1021eda14cbcSMatt Macy 		    zh->zh_claim_txg == 0)
1022eda14cbcSMatt Macy 			return (0);
1023eda14cbcSMatt Macy 	}
1024eda14cbcSMatt Macy 
1025eda14cbcSMatt Macy 	/*
1026eda14cbcSMatt Macy 	 * Because tx == NULL, zil_claim_log_block() will not actually claim
1027eda14cbcSMatt Macy 	 * any blocks, but just determine whether it is possible to do so.
1028eda14cbcSMatt Macy 	 * In addition to checking the log chain, zil_claim_log_block()
1029eda14cbcSMatt Macy 	 * will invoke zio_claim() with a done func of spa_claim_notify(),
1030eda14cbcSMatt Macy 	 * which will update spa_max_claim_txg.  See spa_load() for details.
1031eda14cbcSMatt Macy 	 */
1032eda14cbcSMatt Macy 	error = zil_parse(zilog, zil_claim_log_block, zil_claim_log_record, tx,
1033eda14cbcSMatt Macy 	    zilog->zl_header->zh_claim_txg ? -1ULL :
1034eda14cbcSMatt Macy 	    spa_min_claim_txg(os->os_spa), B_FALSE);
1035eda14cbcSMatt Macy 
1036eda14cbcSMatt Macy 	return ((error == ECKSUM || error == ENOENT) ? 0 : error);
1037eda14cbcSMatt Macy }
1038eda14cbcSMatt Macy 
1039eda14cbcSMatt Macy /*
1040eda14cbcSMatt Macy  * When an itx is "skipped", this function is used to properly mark the
1041eda14cbcSMatt Macy  * waiter as "done, and signal any thread(s) waiting on it. An itx can
1042eda14cbcSMatt Macy  * be skipped (and not committed to an lwb) for a variety of reasons,
1043eda14cbcSMatt Macy  * one of them being that the itx was committed via spa_sync(), prior to
1044eda14cbcSMatt Macy  * it being committed to an lwb; this can happen if a thread calling
1045eda14cbcSMatt Macy  * zil_commit() is racing with spa_sync().
1046eda14cbcSMatt Macy  */
1047eda14cbcSMatt Macy static void
1048eda14cbcSMatt Macy zil_commit_waiter_skip(zil_commit_waiter_t *zcw)
1049eda14cbcSMatt Macy {
1050eda14cbcSMatt Macy 	mutex_enter(&zcw->zcw_lock);
1051eda14cbcSMatt Macy 	ASSERT3B(zcw->zcw_done, ==, B_FALSE);
1052eda14cbcSMatt Macy 	zcw->zcw_done = B_TRUE;
1053eda14cbcSMatt Macy 	cv_broadcast(&zcw->zcw_cv);
1054eda14cbcSMatt Macy 	mutex_exit(&zcw->zcw_lock);
1055eda14cbcSMatt Macy }
1056eda14cbcSMatt Macy 
1057eda14cbcSMatt Macy /*
1058eda14cbcSMatt Macy  * This function is used when the given waiter is to be linked into an
1059eda14cbcSMatt Macy  * lwb's "lwb_waiter" list; i.e. when the itx is committed to the lwb.
1060eda14cbcSMatt Macy  * At this point, the waiter will no longer be referenced by the itx,
1061eda14cbcSMatt Macy  * and instead, will be referenced by the lwb.
1062eda14cbcSMatt Macy  */
1063eda14cbcSMatt Macy static void
1064eda14cbcSMatt Macy zil_commit_waiter_link_lwb(zil_commit_waiter_t *zcw, lwb_t *lwb)
1065eda14cbcSMatt Macy {
1066eda14cbcSMatt Macy 	/*
1067eda14cbcSMatt Macy 	 * The lwb_waiters field of the lwb is protected by the zilog's
1068eda14cbcSMatt Macy 	 * zl_lock, thus it must be held when calling this function.
1069eda14cbcSMatt Macy 	 */
1070eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&lwb->lwb_zilog->zl_lock));
1071eda14cbcSMatt Macy 
1072eda14cbcSMatt Macy 	mutex_enter(&zcw->zcw_lock);
1073eda14cbcSMatt Macy 	ASSERT(!list_link_active(&zcw->zcw_node));
1074eda14cbcSMatt Macy 	ASSERT3P(zcw->zcw_lwb, ==, NULL);
1075eda14cbcSMatt Macy 	ASSERT3P(lwb, !=, NULL);
1076eda14cbcSMatt Macy 	ASSERT(lwb->lwb_state == LWB_STATE_OPENED ||
1077eda14cbcSMatt Macy 	    lwb->lwb_state == LWB_STATE_ISSUED ||
1078eda14cbcSMatt Macy 	    lwb->lwb_state == LWB_STATE_WRITE_DONE);
1079eda14cbcSMatt Macy 
1080eda14cbcSMatt Macy 	list_insert_tail(&lwb->lwb_waiters, zcw);
1081eda14cbcSMatt Macy 	zcw->zcw_lwb = lwb;
1082eda14cbcSMatt Macy 	mutex_exit(&zcw->zcw_lock);
1083eda14cbcSMatt Macy }
1084eda14cbcSMatt Macy 
1085eda14cbcSMatt Macy /*
1086eda14cbcSMatt Macy  * This function is used when zio_alloc_zil() fails to allocate a ZIL
1087eda14cbcSMatt Macy  * block, and the given waiter must be linked to the "nolwb waiters"
1088eda14cbcSMatt Macy  * list inside of zil_process_commit_list().
1089eda14cbcSMatt Macy  */
1090eda14cbcSMatt Macy static void
1091eda14cbcSMatt Macy zil_commit_waiter_link_nolwb(zil_commit_waiter_t *zcw, list_t *nolwb)
1092eda14cbcSMatt Macy {
1093eda14cbcSMatt Macy 	mutex_enter(&zcw->zcw_lock);
1094eda14cbcSMatt Macy 	ASSERT(!list_link_active(&zcw->zcw_node));
1095eda14cbcSMatt Macy 	ASSERT3P(zcw->zcw_lwb, ==, NULL);
1096eda14cbcSMatt Macy 	list_insert_tail(nolwb, zcw);
1097eda14cbcSMatt Macy 	mutex_exit(&zcw->zcw_lock);
1098eda14cbcSMatt Macy }
1099eda14cbcSMatt Macy 
1100eda14cbcSMatt Macy void
1101eda14cbcSMatt Macy zil_lwb_add_block(lwb_t *lwb, const blkptr_t *bp)
1102eda14cbcSMatt Macy {
1103eda14cbcSMatt Macy 	avl_tree_t *t = &lwb->lwb_vdev_tree;
1104eda14cbcSMatt Macy 	avl_index_t where;
1105eda14cbcSMatt Macy 	zil_vdev_node_t *zv, zvsearch;
1106eda14cbcSMatt Macy 	int ndvas = BP_GET_NDVAS(bp);
1107eda14cbcSMatt Macy 	int i;
1108eda14cbcSMatt Macy 
1109eda14cbcSMatt Macy 	if (zil_nocacheflush)
1110eda14cbcSMatt Macy 		return;
1111eda14cbcSMatt Macy 
1112eda14cbcSMatt Macy 	mutex_enter(&lwb->lwb_vdev_lock);
1113eda14cbcSMatt Macy 	for (i = 0; i < ndvas; i++) {
1114eda14cbcSMatt Macy 		zvsearch.zv_vdev = DVA_GET_VDEV(&bp->blk_dva[i]);
1115eda14cbcSMatt Macy 		if (avl_find(t, &zvsearch, &where) == NULL) {
1116eda14cbcSMatt Macy 			zv = kmem_alloc(sizeof (*zv), KM_SLEEP);
1117eda14cbcSMatt Macy 			zv->zv_vdev = zvsearch.zv_vdev;
1118eda14cbcSMatt Macy 			avl_insert(t, zv, where);
1119eda14cbcSMatt Macy 		}
1120eda14cbcSMatt Macy 	}
1121eda14cbcSMatt Macy 	mutex_exit(&lwb->lwb_vdev_lock);
1122eda14cbcSMatt Macy }
1123eda14cbcSMatt Macy 
1124eda14cbcSMatt Macy static void
1125eda14cbcSMatt Macy zil_lwb_flush_defer(lwb_t *lwb, lwb_t *nlwb)
1126eda14cbcSMatt Macy {
1127eda14cbcSMatt Macy 	avl_tree_t *src = &lwb->lwb_vdev_tree;
1128eda14cbcSMatt Macy 	avl_tree_t *dst = &nlwb->lwb_vdev_tree;
1129eda14cbcSMatt Macy 	void *cookie = NULL;
1130eda14cbcSMatt Macy 	zil_vdev_node_t *zv;
1131eda14cbcSMatt Macy 
1132eda14cbcSMatt Macy 	ASSERT3S(lwb->lwb_state, ==, LWB_STATE_WRITE_DONE);
1133eda14cbcSMatt Macy 	ASSERT3S(nlwb->lwb_state, !=, LWB_STATE_WRITE_DONE);
1134eda14cbcSMatt Macy 	ASSERT3S(nlwb->lwb_state, !=, LWB_STATE_FLUSH_DONE);
1135eda14cbcSMatt Macy 
1136eda14cbcSMatt Macy 	/*
1137eda14cbcSMatt Macy 	 * While 'lwb' is at a point in its lifetime where lwb_vdev_tree does
1138eda14cbcSMatt Macy 	 * not need the protection of lwb_vdev_lock (it will only be modified
1139eda14cbcSMatt Macy 	 * while holding zilog->zl_lock) as its writes and those of its
1140eda14cbcSMatt Macy 	 * children have all completed.  The younger 'nlwb' may be waiting on
1141eda14cbcSMatt Macy 	 * future writes to additional vdevs.
1142eda14cbcSMatt Macy 	 */
1143eda14cbcSMatt Macy 	mutex_enter(&nlwb->lwb_vdev_lock);
1144eda14cbcSMatt Macy 	/*
1145eda14cbcSMatt Macy 	 * Tear down the 'lwb' vdev tree, ensuring that entries which do not
1146eda14cbcSMatt Macy 	 * exist in 'nlwb' are moved to it, freeing any would-be duplicates.
1147eda14cbcSMatt Macy 	 */
1148eda14cbcSMatt Macy 	while ((zv = avl_destroy_nodes(src, &cookie)) != NULL) {
1149eda14cbcSMatt Macy 		avl_index_t where;
1150eda14cbcSMatt Macy 
1151eda14cbcSMatt Macy 		if (avl_find(dst, zv, &where) == NULL) {
1152eda14cbcSMatt Macy 			avl_insert(dst, zv, where);
1153eda14cbcSMatt Macy 		} else {
1154eda14cbcSMatt Macy 			kmem_free(zv, sizeof (*zv));
1155eda14cbcSMatt Macy 		}
1156eda14cbcSMatt Macy 	}
1157eda14cbcSMatt Macy 	mutex_exit(&nlwb->lwb_vdev_lock);
1158eda14cbcSMatt Macy }
1159eda14cbcSMatt Macy 
1160eda14cbcSMatt Macy void
1161eda14cbcSMatt Macy zil_lwb_add_txg(lwb_t *lwb, uint64_t txg)
1162eda14cbcSMatt Macy {
1163eda14cbcSMatt Macy 	lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg);
1164eda14cbcSMatt Macy }
1165eda14cbcSMatt Macy 
1166eda14cbcSMatt Macy /*
1167eda14cbcSMatt Macy  * This function is a called after all vdevs associated with a given lwb
1168eda14cbcSMatt Macy  * write have completed their DKIOCFLUSHWRITECACHE command; or as soon
1169eda14cbcSMatt Macy  * as the lwb write completes, if "zil_nocacheflush" is set. Further,
1170eda14cbcSMatt Macy  * all "previous" lwb's will have completed before this function is
1171eda14cbcSMatt Macy  * called; i.e. this function is called for all previous lwbs before
1172eda14cbcSMatt Macy  * it's called for "this" lwb (enforced via zio the dependencies
1173eda14cbcSMatt Macy  * configured in zil_lwb_set_zio_dependency()).
1174eda14cbcSMatt Macy  *
1175eda14cbcSMatt Macy  * The intention is for this function to be called as soon as the
1176eda14cbcSMatt Macy  * contents of an lwb are considered "stable" on disk, and will survive
1177eda14cbcSMatt Macy  * any sudden loss of power. At this point, any threads waiting for the
1178eda14cbcSMatt Macy  * lwb to reach this state are signalled, and the "waiter" structures
1179eda14cbcSMatt Macy  * are marked "done".
1180eda14cbcSMatt Macy  */
1181eda14cbcSMatt Macy static void
1182eda14cbcSMatt Macy zil_lwb_flush_vdevs_done(zio_t *zio)
1183eda14cbcSMatt Macy {
1184eda14cbcSMatt Macy 	lwb_t *lwb = zio->io_private;
1185eda14cbcSMatt Macy 	zilog_t *zilog = lwb->lwb_zilog;
1186eda14cbcSMatt Macy 	zil_commit_waiter_t *zcw;
1187eda14cbcSMatt Macy 	itx_t *itx;
1188e3aa18adSMartin Matuska 	uint64_t txg;
1189eda14cbcSMatt Macy 
1190eda14cbcSMatt Macy 	spa_config_exit(zilog->zl_spa, SCL_STATE, lwb);
1191eda14cbcSMatt Macy 
1192eda14cbcSMatt Macy 	zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
1193eda14cbcSMatt Macy 
1194eda14cbcSMatt Macy 	mutex_enter(&zilog->zl_lock);
1195eda14cbcSMatt Macy 
1196eda14cbcSMatt Macy 	/*
1197e3aa18adSMartin Matuska 	 * If we have had an allocation failure and the txg is
1198eda14cbcSMatt Macy 	 * waiting to sync then we want zil_sync() to remove the lwb so
1199eda14cbcSMatt Macy 	 * that it's not picked up as the next new one in
1200eda14cbcSMatt Macy 	 * zil_process_commit_list(). zil_sync() will only remove the
1201eda14cbcSMatt Macy 	 * lwb if lwb_buf is null.
1202eda14cbcSMatt Macy 	 */
1203eda14cbcSMatt Macy 	lwb->lwb_buf = NULL;
1204eda14cbcSMatt Macy 
1205eda14cbcSMatt Macy 	ASSERT3U(lwb->lwb_issued_timestamp, >, 0);
1206eda14cbcSMatt Macy 	zilog->zl_last_lwb_latency = gethrtime() - lwb->lwb_issued_timestamp;
1207eda14cbcSMatt Macy 
1208eda14cbcSMatt Macy 	lwb->lwb_root_zio = NULL;
1209eda14cbcSMatt Macy 
1210eda14cbcSMatt Macy 	ASSERT3S(lwb->lwb_state, ==, LWB_STATE_WRITE_DONE);
1211eda14cbcSMatt Macy 	lwb->lwb_state = LWB_STATE_FLUSH_DONE;
1212eda14cbcSMatt Macy 
1213eda14cbcSMatt Macy 	if (zilog->zl_last_lwb_opened == lwb) {
1214eda14cbcSMatt Macy 		/*
1215eda14cbcSMatt Macy 		 * Remember the highest committed log sequence number
1216eda14cbcSMatt Macy 		 * for ztest. We only update this value when all the log
1217eda14cbcSMatt Macy 		 * writes succeeded, because ztest wants to ASSERT that
1218eda14cbcSMatt Macy 		 * it got the whole log chain.
1219eda14cbcSMatt Macy 		 */
1220eda14cbcSMatt Macy 		zilog->zl_commit_lr_seq = zilog->zl_lr_seq;
1221eda14cbcSMatt Macy 	}
1222eda14cbcSMatt Macy 
1223eda14cbcSMatt Macy 	while ((itx = list_head(&lwb->lwb_itxs)) != NULL) {
1224eda14cbcSMatt Macy 		list_remove(&lwb->lwb_itxs, itx);
1225eda14cbcSMatt Macy 		zil_itx_destroy(itx);
1226eda14cbcSMatt Macy 	}
1227eda14cbcSMatt Macy 
1228eda14cbcSMatt Macy 	while ((zcw = list_head(&lwb->lwb_waiters)) != NULL) {
1229eda14cbcSMatt Macy 		mutex_enter(&zcw->zcw_lock);
1230eda14cbcSMatt Macy 
1231eda14cbcSMatt Macy 		ASSERT(list_link_active(&zcw->zcw_node));
1232eda14cbcSMatt Macy 		list_remove(&lwb->lwb_waiters, zcw);
1233eda14cbcSMatt Macy 
1234eda14cbcSMatt Macy 		ASSERT3P(zcw->zcw_lwb, ==, lwb);
1235eda14cbcSMatt Macy 		zcw->zcw_lwb = NULL;
123653b70c86SMartin Matuska 		/*
123753b70c86SMartin Matuska 		 * We expect any ZIO errors from child ZIOs to have been
123853b70c86SMartin Matuska 		 * propagated "up" to this specific LWB's root ZIO, in
123953b70c86SMartin Matuska 		 * order for this error handling to work correctly. This
124053b70c86SMartin Matuska 		 * includes ZIO errors from either this LWB's write or
124153b70c86SMartin Matuska 		 * flush, as well as any errors from other dependent LWBs
124253b70c86SMartin Matuska 		 * (e.g. a root LWB ZIO that might be a child of this LWB).
124353b70c86SMartin Matuska 		 *
124453b70c86SMartin Matuska 		 * With that said, it's important to note that LWB flush
124553b70c86SMartin Matuska 		 * errors are not propagated up to the LWB root ZIO.
124653b70c86SMartin Matuska 		 * This is incorrect behavior, and results in VDEV flush
124753b70c86SMartin Matuska 		 * errors not being handled correctly here. See the
124853b70c86SMartin Matuska 		 * comment above the call to "zio_flush" for details.
124953b70c86SMartin Matuska 		 */
1250eda14cbcSMatt Macy 
1251eda14cbcSMatt Macy 		zcw->zcw_zio_error = zio->io_error;
1252eda14cbcSMatt Macy 
1253eda14cbcSMatt Macy 		ASSERT3B(zcw->zcw_done, ==, B_FALSE);
1254eda14cbcSMatt Macy 		zcw->zcw_done = B_TRUE;
1255eda14cbcSMatt Macy 		cv_broadcast(&zcw->zcw_cv);
1256eda14cbcSMatt Macy 
1257eda14cbcSMatt Macy 		mutex_exit(&zcw->zcw_lock);
1258eda14cbcSMatt Macy 	}
1259eda14cbcSMatt Macy 
1260eda14cbcSMatt Macy 	mutex_exit(&zilog->zl_lock);
1261eda14cbcSMatt Macy 
1262e3aa18adSMartin Matuska 	mutex_enter(&zilog->zl_lwb_io_lock);
1263e3aa18adSMartin Matuska 	txg = lwb->lwb_issued_txg;
1264e3aa18adSMartin Matuska 	ASSERT3U(zilog->zl_lwb_inflight[txg & TXG_MASK], >, 0);
1265e3aa18adSMartin Matuska 	zilog->zl_lwb_inflight[txg & TXG_MASK]--;
1266e3aa18adSMartin Matuska 	if (zilog->zl_lwb_inflight[txg & TXG_MASK] == 0)
1267e3aa18adSMartin Matuska 		cv_broadcast(&zilog->zl_lwb_io_cv);
1268e3aa18adSMartin Matuska 	mutex_exit(&zilog->zl_lwb_io_lock);
1269e3aa18adSMartin Matuska }
1270e3aa18adSMartin Matuska 
1271eda14cbcSMatt Macy /*
1272e3aa18adSMartin Matuska  * Wait for the completion of all issued write/flush of that txg provided.
1273e3aa18adSMartin Matuska  * It guarantees zil_lwb_flush_vdevs_done() is called and returned.
1274eda14cbcSMatt Macy  */
1275e3aa18adSMartin Matuska static void
1276e3aa18adSMartin Matuska zil_lwb_flush_wait_all(zilog_t *zilog, uint64_t txg)
1277e3aa18adSMartin Matuska {
1278e3aa18adSMartin Matuska 	ASSERT3U(txg, ==, spa_syncing_txg(zilog->zl_spa));
1279e3aa18adSMartin Matuska 
1280e3aa18adSMartin Matuska 	mutex_enter(&zilog->zl_lwb_io_lock);
1281e3aa18adSMartin Matuska 	while (zilog->zl_lwb_inflight[txg & TXG_MASK] > 0)
1282e3aa18adSMartin Matuska 		cv_wait(&zilog->zl_lwb_io_cv, &zilog->zl_lwb_io_lock);
1283e3aa18adSMartin Matuska 	mutex_exit(&zilog->zl_lwb_io_lock);
1284e3aa18adSMartin Matuska 
1285e3aa18adSMartin Matuska #ifdef ZFS_DEBUG
1286e3aa18adSMartin Matuska 	mutex_enter(&zilog->zl_lock);
1287e3aa18adSMartin Matuska 	mutex_enter(&zilog->zl_lwb_io_lock);
1288e3aa18adSMartin Matuska 	lwb_t *lwb = list_head(&zilog->zl_lwb_list);
1289e3aa18adSMartin Matuska 	while (lwb != NULL && lwb->lwb_max_txg <= txg) {
1290e3aa18adSMartin Matuska 		if (lwb->lwb_issued_txg <= txg) {
1291e3aa18adSMartin Matuska 			ASSERT(lwb->lwb_state != LWB_STATE_ISSUED);
1292e3aa18adSMartin Matuska 			ASSERT(lwb->lwb_state != LWB_STATE_WRITE_DONE);
1293e3aa18adSMartin Matuska 			IMPLY(lwb->lwb_issued_txg > 0,
1294e3aa18adSMartin Matuska 			    lwb->lwb_state == LWB_STATE_FLUSH_DONE);
1295e3aa18adSMartin Matuska 		}
1296e3aa18adSMartin Matuska 		IMPLY(lwb->lwb_state == LWB_STATE_FLUSH_DONE,
1297e3aa18adSMartin Matuska 		    lwb->lwb_buf == NULL);
1298e3aa18adSMartin Matuska 		lwb = list_next(&zilog->zl_lwb_list, lwb);
1299e3aa18adSMartin Matuska 	}
1300e3aa18adSMartin Matuska 	mutex_exit(&zilog->zl_lwb_io_lock);
1301e3aa18adSMartin Matuska 	mutex_exit(&zilog->zl_lock);
1302e3aa18adSMartin Matuska #endif
1303eda14cbcSMatt Macy }
1304eda14cbcSMatt Macy 
1305eda14cbcSMatt Macy /*
1306eda14cbcSMatt Macy  * This is called when an lwb's write zio completes. The callback's
1307eda14cbcSMatt Macy  * purpose is to issue the DKIOCFLUSHWRITECACHE commands for the vdevs
1308eda14cbcSMatt Macy  * in the lwb's lwb_vdev_tree. The tree will contain the vdevs involved
1309eda14cbcSMatt Macy  * in writing out this specific lwb's data, and in the case that cache
1310eda14cbcSMatt Macy  * flushes have been deferred, vdevs involved in writing the data for
1311eda14cbcSMatt Macy  * previous lwbs. The writes corresponding to all the vdevs in the
1312eda14cbcSMatt Macy  * lwb_vdev_tree will have completed by the time this is called, due to
1313eda14cbcSMatt Macy  * the zio dependencies configured in zil_lwb_set_zio_dependency(),
1314eda14cbcSMatt Macy  * which takes deferred flushes into account. The lwb will be "done"
1315eda14cbcSMatt Macy  * once zil_lwb_flush_vdevs_done() is called, which occurs in the zio
1316eda14cbcSMatt Macy  * completion callback for the lwb's root zio.
1317eda14cbcSMatt Macy  */
1318eda14cbcSMatt Macy static void
1319eda14cbcSMatt Macy zil_lwb_write_done(zio_t *zio)
1320eda14cbcSMatt Macy {
1321eda14cbcSMatt Macy 	lwb_t *lwb = zio->io_private;
1322eda14cbcSMatt Macy 	spa_t *spa = zio->io_spa;
1323eda14cbcSMatt Macy 	zilog_t *zilog = lwb->lwb_zilog;
1324eda14cbcSMatt Macy 	avl_tree_t *t = &lwb->lwb_vdev_tree;
1325eda14cbcSMatt Macy 	void *cookie = NULL;
1326eda14cbcSMatt Macy 	zil_vdev_node_t *zv;
1327eda14cbcSMatt Macy 	lwb_t *nlwb;
1328eda14cbcSMatt Macy 
1329eda14cbcSMatt Macy 	ASSERT3S(spa_config_held(spa, SCL_STATE, RW_READER), !=, 0);
1330eda14cbcSMatt Macy 
1331eda14cbcSMatt Macy 	ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF);
1332eda14cbcSMatt Macy 	ASSERT(BP_GET_TYPE(zio->io_bp) == DMU_OT_INTENT_LOG);
1333eda14cbcSMatt Macy 	ASSERT(BP_GET_LEVEL(zio->io_bp) == 0);
1334eda14cbcSMatt Macy 	ASSERT(BP_GET_BYTEORDER(zio->io_bp) == ZFS_HOST_BYTEORDER);
1335eda14cbcSMatt Macy 	ASSERT(!BP_IS_GANG(zio->io_bp));
1336eda14cbcSMatt Macy 	ASSERT(!BP_IS_HOLE(zio->io_bp));
1337eda14cbcSMatt Macy 	ASSERT(BP_GET_FILL(zio->io_bp) == 0);
1338eda14cbcSMatt Macy 
1339184c1b94SMartin Matuska 	abd_free(zio->io_abd);
1340eda14cbcSMatt Macy 
1341eda14cbcSMatt Macy 	mutex_enter(&zilog->zl_lock);
1342eda14cbcSMatt Macy 	ASSERT3S(lwb->lwb_state, ==, LWB_STATE_ISSUED);
1343eda14cbcSMatt Macy 	lwb->lwb_state = LWB_STATE_WRITE_DONE;
1344eda14cbcSMatt Macy 	lwb->lwb_write_zio = NULL;
1345eda14cbcSMatt Macy 	lwb->lwb_fastwrite = FALSE;
1346eda14cbcSMatt Macy 	nlwb = list_next(&zilog->zl_lwb_list, lwb);
1347eda14cbcSMatt Macy 	mutex_exit(&zilog->zl_lock);
1348eda14cbcSMatt Macy 
1349eda14cbcSMatt Macy 	if (avl_numnodes(t) == 0)
1350eda14cbcSMatt Macy 		return;
1351eda14cbcSMatt Macy 
1352eda14cbcSMatt Macy 	/*
1353eda14cbcSMatt Macy 	 * If there was an IO error, we're not going to call zio_flush()
1354eda14cbcSMatt Macy 	 * on these vdevs, so we simply empty the tree and free the
1355eda14cbcSMatt Macy 	 * nodes. We avoid calling zio_flush() since there isn't any
1356eda14cbcSMatt Macy 	 * good reason for doing so, after the lwb block failed to be
1357eda14cbcSMatt Macy 	 * written out.
135853b70c86SMartin Matuska 	 *
135953b70c86SMartin Matuska 	 * Additionally, we don't perform any further error handling at
136053b70c86SMartin Matuska 	 * this point (e.g. setting "zcw_zio_error" appropriately), as
136153b70c86SMartin Matuska 	 * we expect that to occur in "zil_lwb_flush_vdevs_done" (thus,
136253b70c86SMartin Matuska 	 * we expect any error seen here, to have been propagated to
136353b70c86SMartin Matuska 	 * that function).
1364eda14cbcSMatt Macy 	 */
1365eda14cbcSMatt Macy 	if (zio->io_error != 0) {
1366eda14cbcSMatt Macy 		while ((zv = avl_destroy_nodes(t, &cookie)) != NULL)
1367eda14cbcSMatt Macy 			kmem_free(zv, sizeof (*zv));
1368eda14cbcSMatt Macy 		return;
1369eda14cbcSMatt Macy 	}
1370eda14cbcSMatt Macy 
1371eda14cbcSMatt Macy 	/*
1372eda14cbcSMatt Macy 	 * If this lwb does not have any threads waiting for it to
1373eda14cbcSMatt Macy 	 * complete, we want to defer issuing the DKIOCFLUSHWRITECACHE
1374eda14cbcSMatt Macy 	 * command to the vdevs written to by "this" lwb, and instead
1375eda14cbcSMatt Macy 	 * rely on the "next" lwb to handle the DKIOCFLUSHWRITECACHE
1376eda14cbcSMatt Macy 	 * command for those vdevs. Thus, we merge the vdev tree of
1377eda14cbcSMatt Macy 	 * "this" lwb with the vdev tree of the "next" lwb in the list,
1378eda14cbcSMatt Macy 	 * and assume the "next" lwb will handle flushing the vdevs (or
1379eda14cbcSMatt Macy 	 * deferring the flush(s) again).
1380eda14cbcSMatt Macy 	 *
1381eda14cbcSMatt Macy 	 * This is a useful performance optimization, especially for
1382eda14cbcSMatt Macy 	 * workloads with lots of async write activity and few sync
1383eda14cbcSMatt Macy 	 * write and/or fsync activity, as it has the potential to
1384eda14cbcSMatt Macy 	 * coalesce multiple flush commands to a vdev into one.
1385eda14cbcSMatt Macy 	 */
1386eda14cbcSMatt Macy 	if (list_head(&lwb->lwb_waiters) == NULL && nlwb != NULL) {
1387eda14cbcSMatt Macy 		zil_lwb_flush_defer(lwb, nlwb);
1388eda14cbcSMatt Macy 		ASSERT(avl_is_empty(&lwb->lwb_vdev_tree));
1389eda14cbcSMatt Macy 		return;
1390eda14cbcSMatt Macy 	}
1391eda14cbcSMatt Macy 
1392eda14cbcSMatt Macy 	while ((zv = avl_destroy_nodes(t, &cookie)) != NULL) {
1393eda14cbcSMatt Macy 		vdev_t *vd = vdev_lookup_top(spa, zv->zv_vdev);
139453b70c86SMartin Matuska 		if (vd != NULL) {
139553b70c86SMartin Matuska 			/*
139653b70c86SMartin Matuska 			 * The "ZIO_FLAG_DONT_PROPAGATE" is currently
139753b70c86SMartin Matuska 			 * always used within "zio_flush". This means,
139853b70c86SMartin Matuska 			 * any errors when flushing the vdev(s), will
139953b70c86SMartin Matuska 			 * (unfortunately) not be handled correctly,
140053b70c86SMartin Matuska 			 * since these "zio_flush" errors will not be
140153b70c86SMartin Matuska 			 * propagated up to "zil_lwb_flush_vdevs_done".
140253b70c86SMartin Matuska 			 */
1403eda14cbcSMatt Macy 			zio_flush(lwb->lwb_root_zio, vd);
140453b70c86SMartin Matuska 		}
1405eda14cbcSMatt Macy 		kmem_free(zv, sizeof (*zv));
1406eda14cbcSMatt Macy 	}
1407eda14cbcSMatt Macy }
1408eda14cbcSMatt Macy 
1409eda14cbcSMatt Macy static void
1410eda14cbcSMatt Macy zil_lwb_set_zio_dependency(zilog_t *zilog, lwb_t *lwb)
1411eda14cbcSMatt Macy {
1412eda14cbcSMatt Macy 	lwb_t *last_lwb_opened = zilog->zl_last_lwb_opened;
1413eda14cbcSMatt Macy 
1414eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&zilog->zl_issuer_lock));
1415eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&zilog->zl_lock));
1416eda14cbcSMatt Macy 
1417eda14cbcSMatt Macy 	/*
1418eda14cbcSMatt Macy 	 * The zilog's "zl_last_lwb_opened" field is used to build the
1419eda14cbcSMatt Macy 	 * lwb/zio dependency chain, which is used to preserve the
1420eda14cbcSMatt Macy 	 * ordering of lwb completions that is required by the semantics
1421eda14cbcSMatt Macy 	 * of the ZIL. Each new lwb zio becomes a parent of the
1422eda14cbcSMatt Macy 	 * "previous" lwb zio, such that the new lwb's zio cannot
1423eda14cbcSMatt Macy 	 * complete until the "previous" lwb's zio completes.
1424eda14cbcSMatt Macy 	 *
1425eda14cbcSMatt Macy 	 * This is required by the semantics of zil_commit(); the commit
1426eda14cbcSMatt Macy 	 * waiters attached to the lwbs will be woken in the lwb zio's
1427eda14cbcSMatt Macy 	 * completion callback, so this zio dependency graph ensures the
1428eda14cbcSMatt Macy 	 * waiters are woken in the correct order (the same order the
1429eda14cbcSMatt Macy 	 * lwbs were created).
1430eda14cbcSMatt Macy 	 */
1431eda14cbcSMatt Macy 	if (last_lwb_opened != NULL &&
1432eda14cbcSMatt Macy 	    last_lwb_opened->lwb_state != LWB_STATE_FLUSH_DONE) {
1433eda14cbcSMatt Macy 		ASSERT(last_lwb_opened->lwb_state == LWB_STATE_OPENED ||
1434eda14cbcSMatt Macy 		    last_lwb_opened->lwb_state == LWB_STATE_ISSUED ||
1435eda14cbcSMatt Macy 		    last_lwb_opened->lwb_state == LWB_STATE_WRITE_DONE);
1436eda14cbcSMatt Macy 
1437eda14cbcSMatt Macy 		ASSERT3P(last_lwb_opened->lwb_root_zio, !=, NULL);
1438eda14cbcSMatt Macy 		zio_add_child(lwb->lwb_root_zio,
1439eda14cbcSMatt Macy 		    last_lwb_opened->lwb_root_zio);
1440eda14cbcSMatt Macy 
1441eda14cbcSMatt Macy 		/*
1442eda14cbcSMatt Macy 		 * If the previous lwb's write hasn't already completed,
1443eda14cbcSMatt Macy 		 * we also want to order the completion of the lwb write
1444eda14cbcSMatt Macy 		 * zios (above, we only order the completion of the lwb
1445eda14cbcSMatt Macy 		 * root zios). This is required because of how we can
1446eda14cbcSMatt Macy 		 * defer the DKIOCFLUSHWRITECACHE commands for each lwb.
1447eda14cbcSMatt Macy 		 *
1448eda14cbcSMatt Macy 		 * When the DKIOCFLUSHWRITECACHE commands are deferred,
1449eda14cbcSMatt Macy 		 * the previous lwb will rely on this lwb to flush the
1450eda14cbcSMatt Macy 		 * vdevs written to by that previous lwb. Thus, we need
1451eda14cbcSMatt Macy 		 * to ensure this lwb doesn't issue the flush until
1452eda14cbcSMatt Macy 		 * after the previous lwb's write completes. We ensure
1453eda14cbcSMatt Macy 		 * this ordering by setting the zio parent/child
1454eda14cbcSMatt Macy 		 * relationship here.
1455eda14cbcSMatt Macy 		 *
1456eda14cbcSMatt Macy 		 * Without this relationship on the lwb's write zio,
1457eda14cbcSMatt Macy 		 * it's possible for this lwb's write to complete prior
1458eda14cbcSMatt Macy 		 * to the previous lwb's write completing; and thus, the
1459eda14cbcSMatt Macy 		 * vdevs for the previous lwb would be flushed prior to
1460eda14cbcSMatt Macy 		 * that lwb's data being written to those vdevs (the
1461eda14cbcSMatt Macy 		 * vdevs are flushed in the lwb write zio's completion
1462eda14cbcSMatt Macy 		 * handler, zil_lwb_write_done()).
1463eda14cbcSMatt Macy 		 */
1464eda14cbcSMatt Macy 		if (last_lwb_opened->lwb_state != LWB_STATE_WRITE_DONE) {
1465eda14cbcSMatt Macy 			ASSERT(last_lwb_opened->lwb_state == LWB_STATE_OPENED ||
1466eda14cbcSMatt Macy 			    last_lwb_opened->lwb_state == LWB_STATE_ISSUED);
1467eda14cbcSMatt Macy 
1468eda14cbcSMatt Macy 			ASSERT3P(last_lwb_opened->lwb_write_zio, !=, NULL);
1469eda14cbcSMatt Macy 			zio_add_child(lwb->lwb_write_zio,
1470eda14cbcSMatt Macy 			    last_lwb_opened->lwb_write_zio);
1471eda14cbcSMatt Macy 		}
1472eda14cbcSMatt Macy 	}
1473eda14cbcSMatt Macy }
1474eda14cbcSMatt Macy 
1475eda14cbcSMatt Macy 
1476eda14cbcSMatt Macy /*
1477eda14cbcSMatt Macy  * This function's purpose is to "open" an lwb such that it is ready to
1478eda14cbcSMatt Macy  * accept new itxs being committed to it. To do this, the lwb's zio
1479eda14cbcSMatt Macy  * structures are created, and linked to the lwb. This function is
1480eda14cbcSMatt Macy  * idempotent; if the passed in lwb has already been opened, this
1481eda14cbcSMatt Macy  * function is essentially a no-op.
1482eda14cbcSMatt Macy  */
1483eda14cbcSMatt Macy static void
1484eda14cbcSMatt Macy zil_lwb_write_open(zilog_t *zilog, lwb_t *lwb)
1485eda14cbcSMatt Macy {
1486eda14cbcSMatt Macy 	zbookmark_phys_t zb;
1487eda14cbcSMatt Macy 	zio_priority_t prio;
1488eda14cbcSMatt Macy 
1489eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&zilog->zl_issuer_lock));
1490eda14cbcSMatt Macy 	ASSERT3P(lwb, !=, NULL);
1491eda14cbcSMatt Macy 	EQUIV(lwb->lwb_root_zio == NULL, lwb->lwb_state == LWB_STATE_CLOSED);
1492eda14cbcSMatt Macy 	EQUIV(lwb->lwb_root_zio != NULL, lwb->lwb_state == LWB_STATE_OPENED);
1493eda14cbcSMatt Macy 
1494eda14cbcSMatt Macy 	SET_BOOKMARK(&zb, lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET],
1495eda14cbcSMatt Macy 	    ZB_ZIL_OBJECT, ZB_ZIL_LEVEL,
1496eda14cbcSMatt Macy 	    lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ]);
1497eda14cbcSMatt Macy 
1498eda14cbcSMatt Macy 	/* Lock so zil_sync() doesn't fastwrite_unmark after zio is created */
1499eda14cbcSMatt Macy 	mutex_enter(&zilog->zl_lock);
1500eda14cbcSMatt Macy 	if (lwb->lwb_root_zio == NULL) {
1501eda14cbcSMatt Macy 		abd_t *lwb_abd = abd_get_from_buf(lwb->lwb_buf,
1502eda14cbcSMatt Macy 		    BP_GET_LSIZE(&lwb->lwb_blk));
1503eda14cbcSMatt Macy 
1504eda14cbcSMatt Macy 		if (!lwb->lwb_fastwrite) {
1505eda14cbcSMatt Macy 			metaslab_fastwrite_mark(zilog->zl_spa, &lwb->lwb_blk);
1506eda14cbcSMatt Macy 			lwb->lwb_fastwrite = 1;
1507eda14cbcSMatt Macy 		}
1508eda14cbcSMatt Macy 
1509eda14cbcSMatt Macy 		if (!lwb->lwb_slog || zilog->zl_cur_used <= zil_slog_bulk)
1510eda14cbcSMatt Macy 			prio = ZIO_PRIORITY_SYNC_WRITE;
1511eda14cbcSMatt Macy 		else
1512eda14cbcSMatt Macy 			prio = ZIO_PRIORITY_ASYNC_WRITE;
1513eda14cbcSMatt Macy 
1514eda14cbcSMatt Macy 		lwb->lwb_root_zio = zio_root(zilog->zl_spa,
1515eda14cbcSMatt Macy 		    zil_lwb_flush_vdevs_done, lwb, ZIO_FLAG_CANFAIL);
1516eda14cbcSMatt Macy 		ASSERT3P(lwb->lwb_root_zio, !=, NULL);
1517eda14cbcSMatt Macy 
1518eda14cbcSMatt Macy 		lwb->lwb_write_zio = zio_rewrite(lwb->lwb_root_zio,
1519eda14cbcSMatt Macy 		    zilog->zl_spa, 0, &lwb->lwb_blk, lwb_abd,
1520eda14cbcSMatt Macy 		    BP_GET_LSIZE(&lwb->lwb_blk), zil_lwb_write_done, lwb,
152153b70c86SMartin Matuska 		    prio, ZIO_FLAG_CANFAIL | ZIO_FLAG_FASTWRITE, &zb);
1522eda14cbcSMatt Macy 		ASSERT3P(lwb->lwb_write_zio, !=, NULL);
1523eda14cbcSMatt Macy 
1524eda14cbcSMatt Macy 		lwb->lwb_state = LWB_STATE_OPENED;
1525eda14cbcSMatt Macy 
1526eda14cbcSMatt Macy 		zil_lwb_set_zio_dependency(zilog, lwb);
1527eda14cbcSMatt Macy 		zilog->zl_last_lwb_opened = lwb;
1528eda14cbcSMatt Macy 	}
1529eda14cbcSMatt Macy 	mutex_exit(&zilog->zl_lock);
1530eda14cbcSMatt Macy 
1531eda14cbcSMatt Macy 	ASSERT3P(lwb->lwb_root_zio, !=, NULL);
1532eda14cbcSMatt Macy 	ASSERT3P(lwb->lwb_write_zio, !=, NULL);
1533eda14cbcSMatt Macy 	ASSERT3S(lwb->lwb_state, ==, LWB_STATE_OPENED);
1534eda14cbcSMatt Macy }
1535eda14cbcSMatt Macy 
1536eda14cbcSMatt Macy /*
1537eda14cbcSMatt Macy  * Define a limited set of intent log block sizes.
1538eda14cbcSMatt Macy  *
1539eda14cbcSMatt Macy  * These must be a multiple of 4KB. Note only the amount used (again
1540eda14cbcSMatt Macy  * aligned to 4KB) actually gets written. However, we can't always just
1541eda14cbcSMatt Macy  * allocate SPA_OLD_MAXBLOCKSIZE as the slog space could be exhausted.
1542eda14cbcSMatt Macy  */
1543e92ffd9bSMartin Matuska static const struct {
1544eda14cbcSMatt Macy 	uint64_t	limit;
1545eda14cbcSMatt Macy 	uint64_t	blksz;
1546eda14cbcSMatt Macy } zil_block_buckets[] = {
1547eda14cbcSMatt Macy 	{ 4096,		4096 },			/* non TX_WRITE */
1548eda14cbcSMatt Macy 	{ 8192 + 4096,	8192 + 4096 },		/* database */
1549eda14cbcSMatt Macy 	{ 32768 + 4096,	32768 + 4096 },		/* NFS writes */
1550eda14cbcSMatt Macy 	{ 65536 + 4096,	65536 + 4096 },		/* 64KB writes */
1551eda14cbcSMatt Macy 	{ 131072,	131072 },		/* < 128KB writes */
1552eda14cbcSMatt Macy 	{ 131072 +4096,	65536 + 4096 },		/* 128KB writes */
1553eda14cbcSMatt Macy 	{ UINT64_MAX,	SPA_OLD_MAXBLOCKSIZE},	/* > 128KB writes */
1554eda14cbcSMatt Macy };
1555eda14cbcSMatt Macy 
1556eda14cbcSMatt Macy /*
1557eda14cbcSMatt Macy  * Maximum block size used by the ZIL.  This is picked up when the ZIL is
1558eda14cbcSMatt Macy  * initialized.  Otherwise this should not be used directly; see
1559eda14cbcSMatt Macy  * zl_max_block_size instead.
1560eda14cbcSMatt Macy  */
1561e92ffd9bSMartin Matuska static int zil_maxblocksize = SPA_OLD_MAXBLOCKSIZE;
1562eda14cbcSMatt Macy 
1563eda14cbcSMatt Macy /*
1564eda14cbcSMatt Macy  * Start a log block write and advance to the next log block.
1565eda14cbcSMatt Macy  * Calls are serialized.
1566eda14cbcSMatt Macy  */
1567eda14cbcSMatt Macy static lwb_t *
1568eda14cbcSMatt Macy zil_lwb_write_issue(zilog_t *zilog, lwb_t *lwb)
1569eda14cbcSMatt Macy {
1570eda14cbcSMatt Macy 	lwb_t *nlwb = NULL;
1571eda14cbcSMatt Macy 	zil_chain_t *zilc;
1572eda14cbcSMatt Macy 	spa_t *spa = zilog->zl_spa;
1573eda14cbcSMatt Macy 	blkptr_t *bp;
1574eda14cbcSMatt Macy 	dmu_tx_t *tx;
1575eda14cbcSMatt Macy 	uint64_t txg;
1576eda14cbcSMatt Macy 	uint64_t zil_blksz, wsz;
1577eda14cbcSMatt Macy 	int i, error;
1578eda14cbcSMatt Macy 	boolean_t slog;
1579eda14cbcSMatt Macy 
1580eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&zilog->zl_issuer_lock));
1581eda14cbcSMatt Macy 	ASSERT3P(lwb->lwb_root_zio, !=, NULL);
1582eda14cbcSMatt Macy 	ASSERT3P(lwb->lwb_write_zio, !=, NULL);
1583eda14cbcSMatt Macy 	ASSERT3S(lwb->lwb_state, ==, LWB_STATE_OPENED);
1584eda14cbcSMatt Macy 
1585eda14cbcSMatt Macy 	if (BP_GET_CHECKSUM(&lwb->lwb_blk) == ZIO_CHECKSUM_ZILOG2) {
1586eda14cbcSMatt Macy 		zilc = (zil_chain_t *)lwb->lwb_buf;
1587eda14cbcSMatt Macy 		bp = &zilc->zc_next_blk;
1588eda14cbcSMatt Macy 	} else {
1589eda14cbcSMatt Macy 		zilc = (zil_chain_t *)(lwb->lwb_buf + lwb->lwb_sz);
1590eda14cbcSMatt Macy 		bp = &zilc->zc_next_blk;
1591eda14cbcSMatt Macy 	}
1592eda14cbcSMatt Macy 
1593eda14cbcSMatt Macy 	ASSERT(lwb->lwb_nused <= lwb->lwb_sz);
1594eda14cbcSMatt Macy 
1595eda14cbcSMatt Macy 	/*
1596eda14cbcSMatt Macy 	 * Allocate the next block and save its address in this block
1597eda14cbcSMatt Macy 	 * before writing it in order to establish the log chain.
1598eda14cbcSMatt Macy 	 */
1599eda14cbcSMatt Macy 
1600eda14cbcSMatt Macy 	tx = dmu_tx_create(zilog->zl_os);
1601eda14cbcSMatt Macy 
1602eda14cbcSMatt Macy 	/*
1603eda14cbcSMatt Macy 	 * Since we are not going to create any new dirty data, and we
1604eda14cbcSMatt Macy 	 * can even help with clearing the existing dirty data, we
1605eda14cbcSMatt Macy 	 * should not be subject to the dirty data based delays. We
1606eda14cbcSMatt Macy 	 * use TXG_NOTHROTTLE to bypass the delay mechanism.
1607eda14cbcSMatt Macy 	 */
1608eda14cbcSMatt Macy 	VERIFY0(dmu_tx_assign(tx, TXG_WAIT | TXG_NOTHROTTLE));
1609eda14cbcSMatt Macy 
1610eda14cbcSMatt Macy 	dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
1611eda14cbcSMatt Macy 	txg = dmu_tx_get_txg(tx);
1612eda14cbcSMatt Macy 
1613e3aa18adSMartin Matuska 	mutex_enter(&zilog->zl_lwb_io_lock);
1614e3aa18adSMartin Matuska 	lwb->lwb_issued_txg = txg;
1615e3aa18adSMartin Matuska 	zilog->zl_lwb_inflight[txg & TXG_MASK]++;
1616e3aa18adSMartin Matuska 	zilog->zl_lwb_max_issued_txg = MAX(txg, zilog->zl_lwb_max_issued_txg);
1617e3aa18adSMartin Matuska 	mutex_exit(&zilog->zl_lwb_io_lock);
1618eda14cbcSMatt Macy 
1619eda14cbcSMatt Macy 	/*
1620eda14cbcSMatt Macy 	 * Log blocks are pre-allocated. Here we select the size of the next
1621eda14cbcSMatt Macy 	 * block, based on size used in the last block.
1622eda14cbcSMatt Macy 	 * - first find the smallest bucket that will fit the block from a
1623eda14cbcSMatt Macy 	 *   limited set of block sizes. This is because it's faster to write
1624eda14cbcSMatt Macy 	 *   blocks allocated from the same metaslab as they are adjacent or
1625eda14cbcSMatt Macy 	 *   close.
1626eda14cbcSMatt Macy 	 * - next find the maximum from the new suggested size and an array of
1627eda14cbcSMatt Macy 	 *   previous sizes. This lessens a picket fence effect of wrongly
1628eda14cbcSMatt Macy 	 *   guessing the size if we have a stream of say 2k, 64k, 2k, 64k
1629eda14cbcSMatt Macy 	 *   requests.
1630eda14cbcSMatt Macy 	 *
1631eda14cbcSMatt Macy 	 * Note we only write what is used, but we can't just allocate
1632eda14cbcSMatt Macy 	 * the maximum block size because we can exhaust the available
1633eda14cbcSMatt Macy 	 * pool log space.
1634eda14cbcSMatt Macy 	 */
1635eda14cbcSMatt Macy 	zil_blksz = zilog->zl_cur_used + sizeof (zil_chain_t);
1636eda14cbcSMatt Macy 	for (i = 0; zil_blksz > zil_block_buckets[i].limit; i++)
1637eda14cbcSMatt Macy 		continue;
1638eda14cbcSMatt Macy 	zil_blksz = MIN(zil_block_buckets[i].blksz, zilog->zl_max_block_size);
1639eda14cbcSMatt Macy 	zilog->zl_prev_blks[zilog->zl_prev_rotor] = zil_blksz;
1640eda14cbcSMatt Macy 	for (i = 0; i < ZIL_PREV_BLKS; i++)
1641eda14cbcSMatt Macy 		zil_blksz = MAX(zil_blksz, zilog->zl_prev_blks[i]);
1642eda14cbcSMatt Macy 	zilog->zl_prev_rotor = (zilog->zl_prev_rotor + 1) & (ZIL_PREV_BLKS - 1);
1643eda14cbcSMatt Macy 
1644eda14cbcSMatt Macy 	BP_ZERO(bp);
1645eda14cbcSMatt Macy 	error = zio_alloc_zil(spa, zilog->zl_os, txg, bp, zil_blksz, &slog);
1646eda14cbcSMatt Macy 	if (slog) {
1647eda14cbcSMatt Macy 		ZIL_STAT_BUMP(zil_itx_metaslab_slog_count);
1648eda14cbcSMatt Macy 		ZIL_STAT_INCR(zil_itx_metaslab_slog_bytes, lwb->lwb_nused);
1649eda14cbcSMatt Macy 	} else {
1650eda14cbcSMatt Macy 		ZIL_STAT_BUMP(zil_itx_metaslab_normal_count);
1651eda14cbcSMatt Macy 		ZIL_STAT_INCR(zil_itx_metaslab_normal_bytes, lwb->lwb_nused);
1652eda14cbcSMatt Macy 	}
1653eda14cbcSMatt Macy 	if (error == 0) {
1654eda14cbcSMatt Macy 		ASSERT3U(bp->blk_birth, ==, txg);
1655eda14cbcSMatt Macy 		bp->blk_cksum = lwb->lwb_blk.blk_cksum;
1656eda14cbcSMatt Macy 		bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++;
1657eda14cbcSMatt Macy 
1658eda14cbcSMatt Macy 		/*
1659eda14cbcSMatt Macy 		 * Allocate a new log write block (lwb).
1660eda14cbcSMatt Macy 		 */
1661eda14cbcSMatt Macy 		nlwb = zil_alloc_lwb(zilog, bp, slog, txg, TRUE);
1662eda14cbcSMatt Macy 	}
1663eda14cbcSMatt Macy 
1664eda14cbcSMatt Macy 	if (BP_GET_CHECKSUM(&lwb->lwb_blk) == ZIO_CHECKSUM_ZILOG2) {
1665eda14cbcSMatt Macy 		/* For Slim ZIL only write what is used. */
1666eda14cbcSMatt Macy 		wsz = P2ROUNDUP_TYPED(lwb->lwb_nused, ZIL_MIN_BLKSZ, uint64_t);
1667eda14cbcSMatt Macy 		ASSERT3U(wsz, <=, lwb->lwb_sz);
1668eda14cbcSMatt Macy 		zio_shrink(lwb->lwb_write_zio, wsz);
1669eda14cbcSMatt Macy 
1670eda14cbcSMatt Macy 	} else {
1671eda14cbcSMatt Macy 		wsz = lwb->lwb_sz;
1672eda14cbcSMatt Macy 	}
1673eda14cbcSMatt Macy 
1674eda14cbcSMatt Macy 	zilc->zc_pad = 0;
1675eda14cbcSMatt Macy 	zilc->zc_nused = lwb->lwb_nused;
1676eda14cbcSMatt Macy 	zilc->zc_eck.zec_cksum = lwb->lwb_blk.blk_cksum;
1677eda14cbcSMatt Macy 
1678eda14cbcSMatt Macy 	/*
1679eda14cbcSMatt Macy 	 * clear unused data for security
1680eda14cbcSMatt Macy 	 */
1681da5137abSMartin Matuska 	memset(lwb->lwb_buf + lwb->lwb_nused, 0, wsz - lwb->lwb_nused);
1682eda14cbcSMatt Macy 
1683eda14cbcSMatt Macy 	spa_config_enter(zilog->zl_spa, SCL_STATE, lwb, RW_READER);
1684eda14cbcSMatt Macy 
1685eda14cbcSMatt Macy 	zil_lwb_add_block(lwb, &lwb->lwb_blk);
1686eda14cbcSMatt Macy 	lwb->lwb_issued_timestamp = gethrtime();
1687eda14cbcSMatt Macy 	lwb->lwb_state = LWB_STATE_ISSUED;
1688eda14cbcSMatt Macy 
1689eda14cbcSMatt Macy 	zio_nowait(lwb->lwb_root_zio);
1690eda14cbcSMatt Macy 	zio_nowait(lwb->lwb_write_zio);
1691eda14cbcSMatt Macy 
1692e3aa18adSMartin Matuska 	dmu_tx_commit(tx);
1693e3aa18adSMartin Matuska 
1694eda14cbcSMatt Macy 	/*
1695eda14cbcSMatt Macy 	 * If there was an allocation failure then nlwb will be null which
1696eda14cbcSMatt Macy 	 * forces a txg_wait_synced().
1697eda14cbcSMatt Macy 	 */
1698eda14cbcSMatt Macy 	return (nlwb);
1699eda14cbcSMatt Macy }
1700eda14cbcSMatt Macy 
1701eda14cbcSMatt Macy /*
1702eda14cbcSMatt Macy  * Maximum amount of write data that can be put into single log block.
1703eda14cbcSMatt Macy  */
1704eda14cbcSMatt Macy uint64_t
1705eda14cbcSMatt Macy zil_max_log_data(zilog_t *zilog)
1706eda14cbcSMatt Macy {
1707eda14cbcSMatt Macy 	return (zilog->zl_max_block_size -
1708eda14cbcSMatt Macy 	    sizeof (zil_chain_t) - sizeof (lr_write_t));
1709eda14cbcSMatt Macy }
1710eda14cbcSMatt Macy 
1711eda14cbcSMatt Macy /*
1712eda14cbcSMatt Macy  * Maximum amount of log space we agree to waste to reduce number of
1713eda14cbcSMatt Macy  * WR_NEED_COPY chunks to reduce zl_get_data() overhead (~12%).
1714eda14cbcSMatt Macy  */
1715eda14cbcSMatt Macy static inline uint64_t
1716eda14cbcSMatt Macy zil_max_waste_space(zilog_t *zilog)
1717eda14cbcSMatt Macy {
1718eda14cbcSMatt Macy 	return (zil_max_log_data(zilog) / 8);
1719eda14cbcSMatt Macy }
1720eda14cbcSMatt Macy 
1721eda14cbcSMatt Macy /*
1722eda14cbcSMatt Macy  * Maximum amount of write data for WR_COPIED.  For correctness, consumers
1723eda14cbcSMatt Macy  * must fall back to WR_NEED_COPY if we can't fit the entire record into one
1724eda14cbcSMatt Macy  * maximum sized log block, because each WR_COPIED record must fit in a
1725eda14cbcSMatt Macy  * single log block.  For space efficiency, we want to fit two records into a
1726eda14cbcSMatt Macy  * max-sized log block.
1727eda14cbcSMatt Macy  */
1728eda14cbcSMatt Macy uint64_t
1729eda14cbcSMatt Macy zil_max_copied_data(zilog_t *zilog)
1730eda14cbcSMatt Macy {
1731eda14cbcSMatt Macy 	return ((zilog->zl_max_block_size - sizeof (zil_chain_t)) / 2 -
1732eda14cbcSMatt Macy 	    sizeof (lr_write_t));
1733eda14cbcSMatt Macy }
1734eda14cbcSMatt Macy 
1735eda14cbcSMatt Macy static lwb_t *
1736eda14cbcSMatt Macy zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb)
1737eda14cbcSMatt Macy {
1738eda14cbcSMatt Macy 	lr_t *lrcb, *lrc;
1739eda14cbcSMatt Macy 	lr_write_t *lrwb, *lrw;
1740eda14cbcSMatt Macy 	char *lr_buf;
17411f88aa09SMartin Matuska 	uint64_t dlen, dnow, dpad, lwb_sp, reclen, txg, max_log_data;
1742eda14cbcSMatt Macy 
1743eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&zilog->zl_issuer_lock));
1744eda14cbcSMatt Macy 	ASSERT3P(lwb, !=, NULL);
1745eda14cbcSMatt Macy 	ASSERT3P(lwb->lwb_buf, !=, NULL);
1746eda14cbcSMatt Macy 
1747eda14cbcSMatt Macy 	zil_lwb_write_open(zilog, lwb);
1748eda14cbcSMatt Macy 
1749eda14cbcSMatt Macy 	lrc = &itx->itx_lr;
1750eda14cbcSMatt Macy 	lrw = (lr_write_t *)lrc;
1751eda14cbcSMatt Macy 
1752eda14cbcSMatt Macy 	/*
1753eda14cbcSMatt Macy 	 * A commit itx doesn't represent any on-disk state; instead
1754eda14cbcSMatt Macy 	 * it's simply used as a place holder on the commit list, and
1755eda14cbcSMatt Macy 	 * provides a mechanism for attaching a "commit waiter" onto the
1756eda14cbcSMatt Macy 	 * correct lwb (such that the waiter can be signalled upon
1757eda14cbcSMatt Macy 	 * completion of that lwb). Thus, we don't process this itx's
1758eda14cbcSMatt Macy 	 * log record if it's a commit itx (these itx's don't have log
1759eda14cbcSMatt Macy 	 * records), and instead link the itx's waiter onto the lwb's
1760eda14cbcSMatt Macy 	 * list of waiters.
1761eda14cbcSMatt Macy 	 *
1762eda14cbcSMatt Macy 	 * For more details, see the comment above zil_commit().
1763eda14cbcSMatt Macy 	 */
1764eda14cbcSMatt Macy 	if (lrc->lrc_txtype == TX_COMMIT) {
1765eda14cbcSMatt Macy 		mutex_enter(&zilog->zl_lock);
1766eda14cbcSMatt Macy 		zil_commit_waiter_link_lwb(itx->itx_private, lwb);
1767eda14cbcSMatt Macy 		itx->itx_private = NULL;
1768eda14cbcSMatt Macy 		mutex_exit(&zilog->zl_lock);
1769eda14cbcSMatt Macy 		return (lwb);
1770eda14cbcSMatt Macy 	}
1771eda14cbcSMatt Macy 
1772eda14cbcSMatt Macy 	if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY) {
1773eda14cbcSMatt Macy 		dlen = P2ROUNDUP_TYPED(
1774eda14cbcSMatt Macy 		    lrw->lr_length, sizeof (uint64_t), uint64_t);
17751f88aa09SMartin Matuska 		dpad = dlen - lrw->lr_length;
1776eda14cbcSMatt Macy 	} else {
17771f88aa09SMartin Matuska 		dlen = dpad = 0;
1778eda14cbcSMatt Macy 	}
1779eda14cbcSMatt Macy 	reclen = lrc->lrc_reclen;
1780eda14cbcSMatt Macy 	zilog->zl_cur_used += (reclen + dlen);
1781eda14cbcSMatt Macy 	txg = lrc->lrc_txg;
1782eda14cbcSMatt Macy 
1783eda14cbcSMatt Macy 	ASSERT3U(zilog->zl_cur_used, <, UINT64_MAX - (reclen + dlen));
1784eda14cbcSMatt Macy 
1785eda14cbcSMatt Macy cont:
1786eda14cbcSMatt Macy 	/*
1787eda14cbcSMatt Macy 	 * If this record won't fit in the current log block, start a new one.
1788eda14cbcSMatt Macy 	 * For WR_NEED_COPY optimize layout for minimal number of chunks.
1789eda14cbcSMatt Macy 	 */
1790eda14cbcSMatt Macy 	lwb_sp = lwb->lwb_sz - lwb->lwb_nused;
1791eda14cbcSMatt Macy 	max_log_data = zil_max_log_data(zilog);
1792eda14cbcSMatt Macy 	if (reclen > lwb_sp || (reclen + dlen > lwb_sp &&
1793eda14cbcSMatt Macy 	    lwb_sp < zil_max_waste_space(zilog) &&
1794eda14cbcSMatt Macy 	    (dlen % max_log_data == 0 ||
1795eda14cbcSMatt Macy 	    lwb_sp < reclen + dlen % max_log_data))) {
1796eda14cbcSMatt Macy 		lwb = zil_lwb_write_issue(zilog, lwb);
1797eda14cbcSMatt Macy 		if (lwb == NULL)
1798eda14cbcSMatt Macy 			return (NULL);
1799eda14cbcSMatt Macy 		zil_lwb_write_open(zilog, lwb);
1800eda14cbcSMatt Macy 		ASSERT(LWB_EMPTY(lwb));
1801eda14cbcSMatt Macy 		lwb_sp = lwb->lwb_sz - lwb->lwb_nused;
1802eda14cbcSMatt Macy 
1803eda14cbcSMatt Macy 		/*
1804eda14cbcSMatt Macy 		 * There must be enough space in the new, empty log block to
1805eda14cbcSMatt Macy 		 * hold reclen.  For WR_COPIED, we need to fit the whole
1806eda14cbcSMatt Macy 		 * record in one block, and reclen is the header size + the
1807eda14cbcSMatt Macy 		 * data size. For WR_NEED_COPY, we can create multiple
1808eda14cbcSMatt Macy 		 * records, splitting the data into multiple blocks, so we
1809eda14cbcSMatt Macy 		 * only need to fit one word of data per block; in this case
1810eda14cbcSMatt Macy 		 * reclen is just the header size (no data).
1811eda14cbcSMatt Macy 		 */
1812eda14cbcSMatt Macy 		ASSERT3U(reclen + MIN(dlen, sizeof (uint64_t)), <=, lwb_sp);
1813eda14cbcSMatt Macy 	}
1814eda14cbcSMatt Macy 
1815eda14cbcSMatt Macy 	dnow = MIN(dlen, lwb_sp - reclen);
1816eda14cbcSMatt Macy 	lr_buf = lwb->lwb_buf + lwb->lwb_nused;
1817da5137abSMartin Matuska 	memcpy(lr_buf, lrc, reclen);
1818eda14cbcSMatt Macy 	lrcb = (lr_t *)lr_buf;		/* Like lrc, but inside lwb. */
1819eda14cbcSMatt Macy 	lrwb = (lr_write_t *)lrcb;	/* Like lrw, but inside lwb. */
1820eda14cbcSMatt Macy 
1821eda14cbcSMatt Macy 	ZIL_STAT_BUMP(zil_itx_count);
1822eda14cbcSMatt Macy 
1823eda14cbcSMatt Macy 	/*
1824eda14cbcSMatt Macy 	 * If it's a write, fetch the data or get its blkptr as appropriate.
1825eda14cbcSMatt Macy 	 */
1826eda14cbcSMatt Macy 	if (lrc->lrc_txtype == TX_WRITE) {
1827eda14cbcSMatt Macy 		if (txg > spa_freeze_txg(zilog->zl_spa))
1828eda14cbcSMatt Macy 			txg_wait_synced(zilog->zl_dmu_pool, txg);
1829eda14cbcSMatt Macy 		if (itx->itx_wr_state == WR_COPIED) {
1830eda14cbcSMatt Macy 			ZIL_STAT_BUMP(zil_itx_copied_count);
1831eda14cbcSMatt Macy 			ZIL_STAT_INCR(zil_itx_copied_bytes, lrw->lr_length);
1832eda14cbcSMatt Macy 		} else {
1833eda14cbcSMatt Macy 			char *dbuf;
1834eda14cbcSMatt Macy 			int error;
1835eda14cbcSMatt Macy 
1836eda14cbcSMatt Macy 			if (itx->itx_wr_state == WR_NEED_COPY) {
1837eda14cbcSMatt Macy 				dbuf = lr_buf + reclen;
1838eda14cbcSMatt Macy 				lrcb->lrc_reclen += dnow;
1839eda14cbcSMatt Macy 				if (lrwb->lr_length > dnow)
1840eda14cbcSMatt Macy 					lrwb->lr_length = dnow;
1841eda14cbcSMatt Macy 				lrw->lr_offset += dnow;
1842eda14cbcSMatt Macy 				lrw->lr_length -= dnow;
1843eda14cbcSMatt Macy 				ZIL_STAT_BUMP(zil_itx_needcopy_count);
1844eda14cbcSMatt Macy 				ZIL_STAT_INCR(zil_itx_needcopy_bytes, dnow);
1845eda14cbcSMatt Macy 			} else {
1846eda14cbcSMatt Macy 				ASSERT3S(itx->itx_wr_state, ==, WR_INDIRECT);
1847eda14cbcSMatt Macy 				dbuf = NULL;
1848eda14cbcSMatt Macy 				ZIL_STAT_BUMP(zil_itx_indirect_count);
1849eda14cbcSMatt Macy 				ZIL_STAT_INCR(zil_itx_indirect_bytes,
1850eda14cbcSMatt Macy 				    lrw->lr_length);
1851eda14cbcSMatt Macy 			}
1852eda14cbcSMatt Macy 
1853eda14cbcSMatt Macy 			/*
1854eda14cbcSMatt Macy 			 * We pass in the "lwb_write_zio" rather than
1855eda14cbcSMatt Macy 			 * "lwb_root_zio" so that the "lwb_write_zio"
1856eda14cbcSMatt Macy 			 * becomes the parent of any zio's created by
1857eda14cbcSMatt Macy 			 * the "zl_get_data" callback. The vdevs are
1858eda14cbcSMatt Macy 			 * flushed after the "lwb_write_zio" completes,
1859eda14cbcSMatt Macy 			 * so we want to make sure that completion
1860eda14cbcSMatt Macy 			 * callback waits for these additional zio's,
1861eda14cbcSMatt Macy 			 * such that the vdevs used by those zio's will
1862eda14cbcSMatt Macy 			 * be included in the lwb's vdev tree, and those
1863eda14cbcSMatt Macy 			 * vdevs will be properly flushed. If we passed
1864eda14cbcSMatt Macy 			 * in "lwb_root_zio" here, then these additional
1865eda14cbcSMatt Macy 			 * vdevs may not be flushed; e.g. if these zio's
1866eda14cbcSMatt Macy 			 * completed after "lwb_write_zio" completed.
1867eda14cbcSMatt Macy 			 */
1868eda14cbcSMatt Macy 			error = zilog->zl_get_data(itx->itx_private,
1869f9693befSMartin Matuska 			    itx->itx_gen, lrwb, dbuf, lwb,
1870f9693befSMartin Matuska 			    lwb->lwb_write_zio);
18711f88aa09SMartin Matuska 			if (dbuf != NULL && error == 0 && dnow == dlen)
18721f88aa09SMartin Matuska 				/* Zero any padding bytes in the last block. */
1873da5137abSMartin Matuska 				memset((char *)dbuf + lrwb->lr_length, 0, dpad);
1874eda14cbcSMatt Macy 
1875eda14cbcSMatt Macy 			if (error == EIO) {
1876eda14cbcSMatt Macy 				txg_wait_synced(zilog->zl_dmu_pool, txg);
1877eda14cbcSMatt Macy 				return (lwb);
1878eda14cbcSMatt Macy 			}
1879eda14cbcSMatt Macy 			if (error != 0) {
1880eda14cbcSMatt Macy 				ASSERT(error == ENOENT || error == EEXIST ||
1881eda14cbcSMatt Macy 				    error == EALREADY);
1882eda14cbcSMatt Macy 				return (lwb);
1883eda14cbcSMatt Macy 			}
1884eda14cbcSMatt Macy 		}
1885eda14cbcSMatt Macy 	}
1886eda14cbcSMatt Macy 
1887eda14cbcSMatt Macy 	/*
1888eda14cbcSMatt Macy 	 * We're actually making an entry, so update lrc_seq to be the
1889eda14cbcSMatt Macy 	 * log record sequence number.  Note that this is generally not
1890eda14cbcSMatt Macy 	 * equal to the itx sequence number because not all transactions
1891eda14cbcSMatt Macy 	 * are synchronous, and sometimes spa_sync() gets there first.
1892eda14cbcSMatt Macy 	 */
1893eda14cbcSMatt Macy 	lrcb->lrc_seq = ++zilog->zl_lr_seq;
1894eda14cbcSMatt Macy 	lwb->lwb_nused += reclen + dnow;
1895eda14cbcSMatt Macy 
1896eda14cbcSMatt Macy 	zil_lwb_add_txg(lwb, txg);
1897eda14cbcSMatt Macy 
1898eda14cbcSMatt Macy 	ASSERT3U(lwb->lwb_nused, <=, lwb->lwb_sz);
1899eda14cbcSMatt Macy 	ASSERT0(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)));
1900eda14cbcSMatt Macy 
1901eda14cbcSMatt Macy 	dlen -= dnow;
1902eda14cbcSMatt Macy 	if (dlen > 0) {
1903eda14cbcSMatt Macy 		zilog->zl_cur_used += reclen;
1904eda14cbcSMatt Macy 		goto cont;
1905eda14cbcSMatt Macy 	}
1906eda14cbcSMatt Macy 
1907eda14cbcSMatt Macy 	return (lwb);
1908eda14cbcSMatt Macy }
1909eda14cbcSMatt Macy 
1910eda14cbcSMatt Macy itx_t *
19111f88aa09SMartin Matuska zil_itx_create(uint64_t txtype, size_t olrsize)
1912eda14cbcSMatt Macy {
19131f88aa09SMartin Matuska 	size_t itxsize, lrsize;
1914eda14cbcSMatt Macy 	itx_t *itx;
1915eda14cbcSMatt Macy 
19161f88aa09SMartin Matuska 	lrsize = P2ROUNDUP_TYPED(olrsize, sizeof (uint64_t), size_t);
1917eda14cbcSMatt Macy 	itxsize = offsetof(itx_t, itx_lr) + lrsize;
1918eda14cbcSMatt Macy 
1919eda14cbcSMatt Macy 	itx = zio_data_buf_alloc(itxsize);
1920eda14cbcSMatt Macy 	itx->itx_lr.lrc_txtype = txtype;
1921eda14cbcSMatt Macy 	itx->itx_lr.lrc_reclen = lrsize;
1922eda14cbcSMatt Macy 	itx->itx_lr.lrc_seq = 0;	/* defensive */
1923da5137abSMartin Matuska 	memset((char *)&itx->itx_lr + olrsize, 0, lrsize - olrsize);
1924eda14cbcSMatt Macy 	itx->itx_sync = B_TRUE;		/* default is synchronous */
1925eda14cbcSMatt Macy 	itx->itx_callback = NULL;
1926eda14cbcSMatt Macy 	itx->itx_callback_data = NULL;
1927eda14cbcSMatt Macy 	itx->itx_size = itxsize;
1928eda14cbcSMatt Macy 
1929eda14cbcSMatt Macy 	return (itx);
1930eda14cbcSMatt Macy }
1931eda14cbcSMatt Macy 
1932eda14cbcSMatt Macy void
1933eda14cbcSMatt Macy zil_itx_destroy(itx_t *itx)
1934eda14cbcSMatt Macy {
1935eda14cbcSMatt Macy 	IMPLY(itx->itx_lr.lrc_txtype == TX_COMMIT, itx->itx_callback == NULL);
1936eda14cbcSMatt Macy 	IMPLY(itx->itx_callback != NULL, itx->itx_lr.lrc_txtype != TX_COMMIT);
1937eda14cbcSMatt Macy 
1938eda14cbcSMatt Macy 	if (itx->itx_callback != NULL)
1939eda14cbcSMatt Macy 		itx->itx_callback(itx->itx_callback_data);
1940eda14cbcSMatt Macy 
1941eda14cbcSMatt Macy 	zio_data_buf_free(itx, itx->itx_size);
1942eda14cbcSMatt Macy }
1943eda14cbcSMatt Macy 
1944eda14cbcSMatt Macy /*
1945eda14cbcSMatt Macy  * Free up the sync and async itxs. The itxs_t has already been detached
1946eda14cbcSMatt Macy  * so no locks are needed.
1947eda14cbcSMatt Macy  */
1948eda14cbcSMatt Macy static void
19493f9d360cSMartin Matuska zil_itxg_clean(void *arg)
1950eda14cbcSMatt Macy {
1951eda14cbcSMatt Macy 	itx_t *itx;
1952eda14cbcSMatt Macy 	list_t *list;
1953eda14cbcSMatt Macy 	avl_tree_t *t;
1954eda14cbcSMatt Macy 	void *cookie;
19553f9d360cSMartin Matuska 	itxs_t *itxs = arg;
1956eda14cbcSMatt Macy 	itx_async_node_t *ian;
1957eda14cbcSMatt Macy 
1958eda14cbcSMatt Macy 	list = &itxs->i_sync_list;
1959eda14cbcSMatt Macy 	while ((itx = list_head(list)) != NULL) {
1960eda14cbcSMatt Macy 		/*
1961eda14cbcSMatt Macy 		 * In the general case, commit itxs will not be found
1962eda14cbcSMatt Macy 		 * here, as they'll be committed to an lwb via
1963eda14cbcSMatt Macy 		 * zil_lwb_commit(), and free'd in that function. Having
1964eda14cbcSMatt Macy 		 * said that, it is still possible for commit itxs to be
1965eda14cbcSMatt Macy 		 * found here, due to the following race:
1966eda14cbcSMatt Macy 		 *
1967eda14cbcSMatt Macy 		 *	- a thread calls zil_commit() which assigns the
1968eda14cbcSMatt Macy 		 *	  commit itx to a per-txg i_sync_list
1969eda14cbcSMatt Macy 		 *	- zil_itxg_clean() is called (e.g. via spa_sync())
1970eda14cbcSMatt Macy 		 *	  while the waiter is still on the i_sync_list
1971eda14cbcSMatt Macy 		 *
1972eda14cbcSMatt Macy 		 * There's nothing to prevent syncing the txg while the
1973eda14cbcSMatt Macy 		 * waiter is on the i_sync_list. This normally doesn't
1974eda14cbcSMatt Macy 		 * happen because spa_sync() is slower than zil_commit(),
1975eda14cbcSMatt Macy 		 * but if zil_commit() calls txg_wait_synced() (e.g.
1976eda14cbcSMatt Macy 		 * because zil_create() or zil_commit_writer_stall() is
1977eda14cbcSMatt Macy 		 * called) we will hit this case.
1978eda14cbcSMatt Macy 		 */
1979eda14cbcSMatt Macy 		if (itx->itx_lr.lrc_txtype == TX_COMMIT)
1980eda14cbcSMatt Macy 			zil_commit_waiter_skip(itx->itx_private);
1981eda14cbcSMatt Macy 
1982eda14cbcSMatt Macy 		list_remove(list, itx);
1983eda14cbcSMatt Macy 		zil_itx_destroy(itx);
1984eda14cbcSMatt Macy 	}
1985eda14cbcSMatt Macy 
1986eda14cbcSMatt Macy 	cookie = NULL;
1987eda14cbcSMatt Macy 	t = &itxs->i_async_tree;
1988eda14cbcSMatt Macy 	while ((ian = avl_destroy_nodes(t, &cookie)) != NULL) {
1989eda14cbcSMatt Macy 		list = &ian->ia_list;
1990eda14cbcSMatt Macy 		while ((itx = list_head(list)) != NULL) {
1991eda14cbcSMatt Macy 			list_remove(list, itx);
1992eda14cbcSMatt Macy 			/* commit itxs should never be on the async lists. */
1993eda14cbcSMatt Macy 			ASSERT3U(itx->itx_lr.lrc_txtype, !=, TX_COMMIT);
1994eda14cbcSMatt Macy 			zil_itx_destroy(itx);
1995eda14cbcSMatt Macy 		}
1996eda14cbcSMatt Macy 		list_destroy(list);
1997eda14cbcSMatt Macy 		kmem_free(ian, sizeof (itx_async_node_t));
1998eda14cbcSMatt Macy 	}
1999eda14cbcSMatt Macy 	avl_destroy(t);
2000eda14cbcSMatt Macy 
2001eda14cbcSMatt Macy 	kmem_free(itxs, sizeof (itxs_t));
2002eda14cbcSMatt Macy }
2003eda14cbcSMatt Macy 
2004eda14cbcSMatt Macy static int
2005eda14cbcSMatt Macy zil_aitx_compare(const void *x1, const void *x2)
2006eda14cbcSMatt Macy {
2007eda14cbcSMatt Macy 	const uint64_t o1 = ((itx_async_node_t *)x1)->ia_foid;
2008eda14cbcSMatt Macy 	const uint64_t o2 = ((itx_async_node_t *)x2)->ia_foid;
2009eda14cbcSMatt Macy 
2010eda14cbcSMatt Macy 	return (TREE_CMP(o1, o2));
2011eda14cbcSMatt Macy }
2012eda14cbcSMatt Macy 
2013eda14cbcSMatt Macy /*
2014eda14cbcSMatt Macy  * Remove all async itx with the given oid.
2015eda14cbcSMatt Macy  */
2016eda14cbcSMatt Macy void
2017eda14cbcSMatt Macy zil_remove_async(zilog_t *zilog, uint64_t oid)
2018eda14cbcSMatt Macy {
2019eda14cbcSMatt Macy 	uint64_t otxg, txg;
2020eda14cbcSMatt Macy 	itx_async_node_t *ian;
2021eda14cbcSMatt Macy 	avl_tree_t *t;
2022eda14cbcSMatt Macy 	avl_index_t where;
2023eda14cbcSMatt Macy 	list_t clean_list;
2024eda14cbcSMatt Macy 	itx_t *itx;
2025eda14cbcSMatt Macy 
2026eda14cbcSMatt Macy 	ASSERT(oid != 0);
2027eda14cbcSMatt Macy 	list_create(&clean_list, sizeof (itx_t), offsetof(itx_t, itx_node));
2028eda14cbcSMatt Macy 
2029eda14cbcSMatt Macy 	if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */
2030eda14cbcSMatt Macy 		otxg = ZILTEST_TXG;
2031eda14cbcSMatt Macy 	else
2032eda14cbcSMatt Macy 		otxg = spa_last_synced_txg(zilog->zl_spa) + 1;
2033eda14cbcSMatt Macy 
2034eda14cbcSMatt Macy 	for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) {
2035eda14cbcSMatt Macy 		itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK];
2036eda14cbcSMatt Macy 
2037eda14cbcSMatt Macy 		mutex_enter(&itxg->itxg_lock);
2038eda14cbcSMatt Macy 		if (itxg->itxg_txg != txg) {
2039eda14cbcSMatt Macy 			mutex_exit(&itxg->itxg_lock);
2040eda14cbcSMatt Macy 			continue;
2041eda14cbcSMatt Macy 		}
2042eda14cbcSMatt Macy 
2043eda14cbcSMatt Macy 		/*
2044eda14cbcSMatt Macy 		 * Locate the object node and append its list.
2045eda14cbcSMatt Macy 		 */
2046eda14cbcSMatt Macy 		t = &itxg->itxg_itxs->i_async_tree;
2047eda14cbcSMatt Macy 		ian = avl_find(t, &oid, &where);
2048eda14cbcSMatt Macy 		if (ian != NULL)
2049eda14cbcSMatt Macy 			list_move_tail(&clean_list, &ian->ia_list);
2050eda14cbcSMatt Macy 		mutex_exit(&itxg->itxg_lock);
2051eda14cbcSMatt Macy 	}
2052eda14cbcSMatt Macy 	while ((itx = list_head(&clean_list)) != NULL) {
2053eda14cbcSMatt Macy 		list_remove(&clean_list, itx);
2054eda14cbcSMatt Macy 		/* commit itxs should never be on the async lists. */
2055eda14cbcSMatt Macy 		ASSERT3U(itx->itx_lr.lrc_txtype, !=, TX_COMMIT);
2056eda14cbcSMatt Macy 		zil_itx_destroy(itx);
2057eda14cbcSMatt Macy 	}
2058eda14cbcSMatt Macy 	list_destroy(&clean_list);
2059eda14cbcSMatt Macy }
2060eda14cbcSMatt Macy 
2061eda14cbcSMatt Macy void
2062eda14cbcSMatt Macy zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx)
2063eda14cbcSMatt Macy {
2064eda14cbcSMatt Macy 	uint64_t txg;
2065eda14cbcSMatt Macy 	itxg_t *itxg;
2066eda14cbcSMatt Macy 	itxs_t *itxs, *clean = NULL;
2067eda14cbcSMatt Macy 
2068eda14cbcSMatt Macy 	/*
2069eda14cbcSMatt Macy 	 * Ensure the data of a renamed file is committed before the rename.
2070eda14cbcSMatt Macy 	 */
2071eda14cbcSMatt Macy 	if ((itx->itx_lr.lrc_txtype & ~TX_CI) == TX_RENAME)
2072eda14cbcSMatt Macy 		zil_async_to_sync(zilog, itx->itx_oid);
2073eda14cbcSMatt Macy 
2074eda14cbcSMatt Macy 	if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX)
2075eda14cbcSMatt Macy 		txg = ZILTEST_TXG;
2076eda14cbcSMatt Macy 	else
2077eda14cbcSMatt Macy 		txg = dmu_tx_get_txg(tx);
2078eda14cbcSMatt Macy 
2079eda14cbcSMatt Macy 	itxg = &zilog->zl_itxg[txg & TXG_MASK];
2080eda14cbcSMatt Macy 	mutex_enter(&itxg->itxg_lock);
2081eda14cbcSMatt Macy 	itxs = itxg->itxg_itxs;
2082eda14cbcSMatt Macy 	if (itxg->itxg_txg != txg) {
2083eda14cbcSMatt Macy 		if (itxs != NULL) {
2084eda14cbcSMatt Macy 			/*
2085eda14cbcSMatt Macy 			 * The zil_clean callback hasn't got around to cleaning
2086eda14cbcSMatt Macy 			 * this itxg. Save the itxs for release below.
2087eda14cbcSMatt Macy 			 * This should be rare.
2088eda14cbcSMatt Macy 			 */
2089eda14cbcSMatt Macy 			zfs_dbgmsg("zil_itx_assign: missed itx cleanup for "
209033b8c039SMartin Matuska 			    "txg %llu", (u_longlong_t)itxg->itxg_txg);
2091eda14cbcSMatt Macy 			clean = itxg->itxg_itxs;
2092eda14cbcSMatt Macy 		}
2093eda14cbcSMatt Macy 		itxg->itxg_txg = txg;
2094eda14cbcSMatt Macy 		itxs = itxg->itxg_itxs = kmem_zalloc(sizeof (itxs_t),
2095eda14cbcSMatt Macy 		    KM_SLEEP);
2096eda14cbcSMatt Macy 
2097eda14cbcSMatt Macy 		list_create(&itxs->i_sync_list, sizeof (itx_t),
2098eda14cbcSMatt Macy 		    offsetof(itx_t, itx_node));
2099eda14cbcSMatt Macy 		avl_create(&itxs->i_async_tree, zil_aitx_compare,
2100eda14cbcSMatt Macy 		    sizeof (itx_async_node_t),
2101eda14cbcSMatt Macy 		    offsetof(itx_async_node_t, ia_node));
2102eda14cbcSMatt Macy 	}
2103eda14cbcSMatt Macy 	if (itx->itx_sync) {
2104eda14cbcSMatt Macy 		list_insert_tail(&itxs->i_sync_list, itx);
2105eda14cbcSMatt Macy 	} else {
2106eda14cbcSMatt Macy 		avl_tree_t *t = &itxs->i_async_tree;
2107eda14cbcSMatt Macy 		uint64_t foid =
2108eda14cbcSMatt Macy 		    LR_FOID_GET_OBJ(((lr_ooo_t *)&itx->itx_lr)->lr_foid);
2109eda14cbcSMatt Macy 		itx_async_node_t *ian;
2110eda14cbcSMatt Macy 		avl_index_t where;
2111eda14cbcSMatt Macy 
2112eda14cbcSMatt Macy 		ian = avl_find(t, &foid, &where);
2113eda14cbcSMatt Macy 		if (ian == NULL) {
2114eda14cbcSMatt Macy 			ian = kmem_alloc(sizeof (itx_async_node_t),
2115eda14cbcSMatt Macy 			    KM_SLEEP);
2116eda14cbcSMatt Macy 			list_create(&ian->ia_list, sizeof (itx_t),
2117eda14cbcSMatt Macy 			    offsetof(itx_t, itx_node));
2118eda14cbcSMatt Macy 			ian->ia_foid = foid;
2119eda14cbcSMatt Macy 			avl_insert(t, ian, where);
2120eda14cbcSMatt Macy 		}
2121eda14cbcSMatt Macy 		list_insert_tail(&ian->ia_list, itx);
2122eda14cbcSMatt Macy 	}
2123eda14cbcSMatt Macy 
2124eda14cbcSMatt Macy 	itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx);
2125eda14cbcSMatt Macy 
2126eda14cbcSMatt Macy 	/*
2127eda14cbcSMatt Macy 	 * We don't want to dirty the ZIL using ZILTEST_TXG, because
2128eda14cbcSMatt Macy 	 * zil_clean() will never be called using ZILTEST_TXG. Thus, we
2129eda14cbcSMatt Macy 	 * need to be careful to always dirty the ZIL using the "real"
2130eda14cbcSMatt Macy 	 * TXG (not itxg_txg) even when the SPA is frozen.
2131eda14cbcSMatt Macy 	 */
2132eda14cbcSMatt Macy 	zilog_dirty(zilog, dmu_tx_get_txg(tx));
2133eda14cbcSMatt Macy 	mutex_exit(&itxg->itxg_lock);
2134eda14cbcSMatt Macy 
2135eda14cbcSMatt Macy 	/* Release the old itxs now we've dropped the lock */
2136eda14cbcSMatt Macy 	if (clean != NULL)
2137eda14cbcSMatt Macy 		zil_itxg_clean(clean);
2138eda14cbcSMatt Macy }
2139eda14cbcSMatt Macy 
2140eda14cbcSMatt Macy /*
2141eda14cbcSMatt Macy  * If there are any in-memory intent log transactions which have now been
2142eda14cbcSMatt Macy  * synced then start up a taskq to free them. We should only do this after we
2143eda14cbcSMatt Macy  * have written out the uberblocks (i.e. txg has been committed) so that
2144eda14cbcSMatt Macy  * don't inadvertently clean out in-memory log records that would be required
2145eda14cbcSMatt Macy  * by zil_commit().
2146eda14cbcSMatt Macy  */
2147eda14cbcSMatt Macy void
2148eda14cbcSMatt Macy zil_clean(zilog_t *zilog, uint64_t synced_txg)
2149eda14cbcSMatt Macy {
2150eda14cbcSMatt Macy 	itxg_t *itxg = &zilog->zl_itxg[synced_txg & TXG_MASK];
2151eda14cbcSMatt Macy 	itxs_t *clean_me;
2152eda14cbcSMatt Macy 
2153eda14cbcSMatt Macy 	ASSERT3U(synced_txg, <, ZILTEST_TXG);
2154eda14cbcSMatt Macy 
2155eda14cbcSMatt Macy 	mutex_enter(&itxg->itxg_lock);
2156eda14cbcSMatt Macy 	if (itxg->itxg_itxs == NULL || itxg->itxg_txg == ZILTEST_TXG) {
2157eda14cbcSMatt Macy 		mutex_exit(&itxg->itxg_lock);
2158eda14cbcSMatt Macy 		return;
2159eda14cbcSMatt Macy 	}
2160eda14cbcSMatt Macy 	ASSERT3U(itxg->itxg_txg, <=, synced_txg);
2161eda14cbcSMatt Macy 	ASSERT3U(itxg->itxg_txg, !=, 0);
2162eda14cbcSMatt Macy 	clean_me = itxg->itxg_itxs;
2163eda14cbcSMatt Macy 	itxg->itxg_itxs = NULL;
2164eda14cbcSMatt Macy 	itxg->itxg_txg = 0;
2165eda14cbcSMatt Macy 	mutex_exit(&itxg->itxg_lock);
2166eda14cbcSMatt Macy 	/*
2167eda14cbcSMatt Macy 	 * Preferably start a task queue to free up the old itxs but
2168eda14cbcSMatt Macy 	 * if taskq_dispatch can't allocate resources to do that then
2169eda14cbcSMatt Macy 	 * free it in-line. This should be rare. Note, using TQ_SLEEP
2170eda14cbcSMatt Macy 	 * created a bad performance problem.
2171eda14cbcSMatt Macy 	 */
2172eda14cbcSMatt Macy 	ASSERT3P(zilog->zl_dmu_pool, !=, NULL);
2173eda14cbcSMatt Macy 	ASSERT3P(zilog->zl_dmu_pool->dp_zil_clean_taskq, !=, NULL);
2174eda14cbcSMatt Macy 	taskqid_t id = taskq_dispatch(zilog->zl_dmu_pool->dp_zil_clean_taskq,
21753f9d360cSMartin Matuska 	    zil_itxg_clean, clean_me, TQ_NOSLEEP);
2176eda14cbcSMatt Macy 	if (id == TASKQID_INVALID)
2177eda14cbcSMatt Macy 		zil_itxg_clean(clean_me);
2178eda14cbcSMatt Macy }
2179eda14cbcSMatt Macy 
2180eda14cbcSMatt Macy /*
2181eda14cbcSMatt Macy  * This function will traverse the queue of itxs that need to be
2182eda14cbcSMatt Macy  * committed, and move them onto the ZIL's zl_itx_commit_list.
2183eda14cbcSMatt Macy  */
2184eda14cbcSMatt Macy static void
2185eda14cbcSMatt Macy zil_get_commit_list(zilog_t *zilog)
2186eda14cbcSMatt Macy {
2187eda14cbcSMatt Macy 	uint64_t otxg, txg;
2188eda14cbcSMatt Macy 	list_t *commit_list = &zilog->zl_itx_commit_list;
2189eda14cbcSMatt Macy 
2190eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&zilog->zl_issuer_lock));
2191eda14cbcSMatt Macy 
2192eda14cbcSMatt Macy 	if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */
2193eda14cbcSMatt Macy 		otxg = ZILTEST_TXG;
2194eda14cbcSMatt Macy 	else
2195eda14cbcSMatt Macy 		otxg = spa_last_synced_txg(zilog->zl_spa) + 1;
2196eda14cbcSMatt Macy 
2197eda14cbcSMatt Macy 	/*
2198eda14cbcSMatt Macy 	 * This is inherently racy, since there is nothing to prevent
2199eda14cbcSMatt Macy 	 * the last synced txg from changing. That's okay since we'll
2200eda14cbcSMatt Macy 	 * only commit things in the future.
2201eda14cbcSMatt Macy 	 */
2202eda14cbcSMatt Macy 	for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) {
2203eda14cbcSMatt Macy 		itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK];
2204eda14cbcSMatt Macy 
2205eda14cbcSMatt Macy 		mutex_enter(&itxg->itxg_lock);
2206eda14cbcSMatt Macy 		if (itxg->itxg_txg != txg) {
2207eda14cbcSMatt Macy 			mutex_exit(&itxg->itxg_lock);
2208eda14cbcSMatt Macy 			continue;
2209eda14cbcSMatt Macy 		}
2210eda14cbcSMatt Macy 
2211eda14cbcSMatt Macy 		/*
2212eda14cbcSMatt Macy 		 * If we're adding itx records to the zl_itx_commit_list,
2213eda14cbcSMatt Macy 		 * then the zil better be dirty in this "txg". We can assert
2214eda14cbcSMatt Macy 		 * that here since we're holding the itxg_lock which will
2215eda14cbcSMatt Macy 		 * prevent spa_sync from cleaning it. Once we add the itxs
2216eda14cbcSMatt Macy 		 * to the zl_itx_commit_list we must commit it to disk even
2217eda14cbcSMatt Macy 		 * if it's unnecessary (i.e. the txg was synced).
2218eda14cbcSMatt Macy 		 */
2219eda14cbcSMatt Macy 		ASSERT(zilog_is_dirty_in_txg(zilog, txg) ||
2220eda14cbcSMatt Macy 		    spa_freeze_txg(zilog->zl_spa) != UINT64_MAX);
2221eda14cbcSMatt Macy 		list_move_tail(commit_list, &itxg->itxg_itxs->i_sync_list);
2222eda14cbcSMatt Macy 
2223eda14cbcSMatt Macy 		mutex_exit(&itxg->itxg_lock);
2224eda14cbcSMatt Macy 	}
2225eda14cbcSMatt Macy }
2226eda14cbcSMatt Macy 
2227eda14cbcSMatt Macy /*
2228eda14cbcSMatt Macy  * Move the async itxs for a specified object to commit into sync lists.
2229eda14cbcSMatt Macy  */
2230eda14cbcSMatt Macy void
2231eda14cbcSMatt Macy zil_async_to_sync(zilog_t *zilog, uint64_t foid)
2232eda14cbcSMatt Macy {
2233eda14cbcSMatt Macy 	uint64_t otxg, txg;
2234eda14cbcSMatt Macy 	itx_async_node_t *ian;
2235eda14cbcSMatt Macy 	avl_tree_t *t;
2236eda14cbcSMatt Macy 	avl_index_t where;
2237eda14cbcSMatt Macy 
2238eda14cbcSMatt Macy 	if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */
2239eda14cbcSMatt Macy 		otxg = ZILTEST_TXG;
2240eda14cbcSMatt Macy 	else
2241eda14cbcSMatt Macy 		otxg = spa_last_synced_txg(zilog->zl_spa) + 1;
2242eda14cbcSMatt Macy 
2243eda14cbcSMatt Macy 	/*
2244eda14cbcSMatt Macy 	 * This is inherently racy, since there is nothing to prevent
2245eda14cbcSMatt Macy 	 * the last synced txg from changing.
2246eda14cbcSMatt Macy 	 */
2247eda14cbcSMatt Macy 	for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) {
2248eda14cbcSMatt Macy 		itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK];
2249eda14cbcSMatt Macy 
2250eda14cbcSMatt Macy 		mutex_enter(&itxg->itxg_lock);
2251eda14cbcSMatt Macy 		if (itxg->itxg_txg != txg) {
2252eda14cbcSMatt Macy 			mutex_exit(&itxg->itxg_lock);
2253eda14cbcSMatt Macy 			continue;
2254eda14cbcSMatt Macy 		}
2255eda14cbcSMatt Macy 
2256eda14cbcSMatt Macy 		/*
2257eda14cbcSMatt Macy 		 * If a foid is specified then find that node and append its
2258eda14cbcSMatt Macy 		 * list. Otherwise walk the tree appending all the lists
2259eda14cbcSMatt Macy 		 * to the sync list. We add to the end rather than the
2260eda14cbcSMatt Macy 		 * beginning to ensure the create has happened.
2261eda14cbcSMatt Macy 		 */
2262eda14cbcSMatt Macy 		t = &itxg->itxg_itxs->i_async_tree;
2263eda14cbcSMatt Macy 		if (foid != 0) {
2264eda14cbcSMatt Macy 			ian = avl_find(t, &foid, &where);
2265eda14cbcSMatt Macy 			if (ian != NULL) {
2266eda14cbcSMatt Macy 				list_move_tail(&itxg->itxg_itxs->i_sync_list,
2267eda14cbcSMatt Macy 				    &ian->ia_list);
2268eda14cbcSMatt Macy 			}
2269eda14cbcSMatt Macy 		} else {
2270eda14cbcSMatt Macy 			void *cookie = NULL;
2271eda14cbcSMatt Macy 
2272eda14cbcSMatt Macy 			while ((ian = avl_destroy_nodes(t, &cookie)) != NULL) {
2273eda14cbcSMatt Macy 				list_move_tail(&itxg->itxg_itxs->i_sync_list,
2274eda14cbcSMatt Macy 				    &ian->ia_list);
2275eda14cbcSMatt Macy 				list_destroy(&ian->ia_list);
2276eda14cbcSMatt Macy 				kmem_free(ian, sizeof (itx_async_node_t));
2277eda14cbcSMatt Macy 			}
2278eda14cbcSMatt Macy 		}
2279eda14cbcSMatt Macy 		mutex_exit(&itxg->itxg_lock);
2280eda14cbcSMatt Macy 	}
2281eda14cbcSMatt Macy }
2282eda14cbcSMatt Macy 
2283eda14cbcSMatt Macy /*
2284eda14cbcSMatt Macy  * This function will prune commit itxs that are at the head of the
2285eda14cbcSMatt Macy  * commit list (it won't prune past the first non-commit itx), and
2286eda14cbcSMatt Macy  * either: a) attach them to the last lwb that's still pending
2287eda14cbcSMatt Macy  * completion, or b) skip them altogether.
2288eda14cbcSMatt Macy  *
2289eda14cbcSMatt Macy  * This is used as a performance optimization to prevent commit itxs
2290eda14cbcSMatt Macy  * from generating new lwbs when it's unnecessary to do so.
2291eda14cbcSMatt Macy  */
2292eda14cbcSMatt Macy static void
2293eda14cbcSMatt Macy zil_prune_commit_list(zilog_t *zilog)
2294eda14cbcSMatt Macy {
2295eda14cbcSMatt Macy 	itx_t *itx;
2296eda14cbcSMatt Macy 
2297eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&zilog->zl_issuer_lock));
2298eda14cbcSMatt Macy 
2299eda14cbcSMatt Macy 	while ((itx = list_head(&zilog->zl_itx_commit_list)) != NULL) {
2300eda14cbcSMatt Macy 		lr_t *lrc = &itx->itx_lr;
2301eda14cbcSMatt Macy 		if (lrc->lrc_txtype != TX_COMMIT)
2302eda14cbcSMatt Macy 			break;
2303eda14cbcSMatt Macy 
2304eda14cbcSMatt Macy 		mutex_enter(&zilog->zl_lock);
2305eda14cbcSMatt Macy 
2306eda14cbcSMatt Macy 		lwb_t *last_lwb = zilog->zl_last_lwb_opened;
2307eda14cbcSMatt Macy 		if (last_lwb == NULL ||
2308eda14cbcSMatt Macy 		    last_lwb->lwb_state == LWB_STATE_FLUSH_DONE) {
2309eda14cbcSMatt Macy 			/*
2310eda14cbcSMatt Macy 			 * All of the itxs this waiter was waiting on
2311eda14cbcSMatt Macy 			 * must have already completed (or there were
2312eda14cbcSMatt Macy 			 * never any itx's for it to wait on), so it's
2313eda14cbcSMatt Macy 			 * safe to skip this waiter and mark it done.
2314eda14cbcSMatt Macy 			 */
2315eda14cbcSMatt Macy 			zil_commit_waiter_skip(itx->itx_private);
2316eda14cbcSMatt Macy 		} else {
2317eda14cbcSMatt Macy 			zil_commit_waiter_link_lwb(itx->itx_private, last_lwb);
2318eda14cbcSMatt Macy 			itx->itx_private = NULL;
2319eda14cbcSMatt Macy 		}
2320eda14cbcSMatt Macy 
2321eda14cbcSMatt Macy 		mutex_exit(&zilog->zl_lock);
2322eda14cbcSMatt Macy 
2323eda14cbcSMatt Macy 		list_remove(&zilog->zl_itx_commit_list, itx);
2324eda14cbcSMatt Macy 		zil_itx_destroy(itx);
2325eda14cbcSMatt Macy 	}
2326eda14cbcSMatt Macy 
2327eda14cbcSMatt Macy 	IMPLY(itx != NULL, itx->itx_lr.lrc_txtype != TX_COMMIT);
2328eda14cbcSMatt Macy }
2329eda14cbcSMatt Macy 
2330eda14cbcSMatt Macy static void
2331eda14cbcSMatt Macy zil_commit_writer_stall(zilog_t *zilog)
2332eda14cbcSMatt Macy {
2333eda14cbcSMatt Macy 	/*
2334eda14cbcSMatt Macy 	 * When zio_alloc_zil() fails to allocate the next lwb block on
2335eda14cbcSMatt Macy 	 * disk, we must call txg_wait_synced() to ensure all of the
2336eda14cbcSMatt Macy 	 * lwbs in the zilog's zl_lwb_list are synced and then freed (in
2337eda14cbcSMatt Macy 	 * zil_sync()), such that any subsequent ZIL writer (i.e. a call
2338eda14cbcSMatt Macy 	 * to zil_process_commit_list()) will have to call zil_create(),
2339eda14cbcSMatt Macy 	 * and start a new ZIL chain.
2340eda14cbcSMatt Macy 	 *
2341eda14cbcSMatt Macy 	 * Since zil_alloc_zil() failed, the lwb that was previously
2342eda14cbcSMatt Macy 	 * issued does not have a pointer to the "next" lwb on disk.
2343eda14cbcSMatt Macy 	 * Thus, if another ZIL writer thread was to allocate the "next"
2344eda14cbcSMatt Macy 	 * on-disk lwb, that block could be leaked in the event of a
2345eda14cbcSMatt Macy 	 * crash (because the previous lwb on-disk would not point to
2346eda14cbcSMatt Macy 	 * it).
2347eda14cbcSMatt Macy 	 *
2348eda14cbcSMatt Macy 	 * We must hold the zilog's zl_issuer_lock while we do this, to
2349eda14cbcSMatt Macy 	 * ensure no new threads enter zil_process_commit_list() until
2350eda14cbcSMatt Macy 	 * all lwb's in the zl_lwb_list have been synced and freed
2351eda14cbcSMatt Macy 	 * (which is achieved via the txg_wait_synced() call).
2352eda14cbcSMatt Macy 	 */
2353eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&zilog->zl_issuer_lock));
2354eda14cbcSMatt Macy 	txg_wait_synced(zilog->zl_dmu_pool, 0);
2355eda14cbcSMatt Macy 	ASSERT3P(list_tail(&zilog->zl_lwb_list), ==, NULL);
2356eda14cbcSMatt Macy }
2357eda14cbcSMatt Macy 
2358eda14cbcSMatt Macy /*
2359eda14cbcSMatt Macy  * This function will traverse the commit list, creating new lwbs as
2360eda14cbcSMatt Macy  * needed, and committing the itxs from the commit list to these newly
2361eda14cbcSMatt Macy  * created lwbs. Additionally, as a new lwb is created, the previous
2362eda14cbcSMatt Macy  * lwb will be issued to the zio layer to be written to disk.
2363eda14cbcSMatt Macy  */
2364eda14cbcSMatt Macy static void
2365eda14cbcSMatt Macy zil_process_commit_list(zilog_t *zilog)
2366eda14cbcSMatt Macy {
2367eda14cbcSMatt Macy 	spa_t *spa = zilog->zl_spa;
2368eda14cbcSMatt Macy 	list_t nolwb_itxs;
2369eda14cbcSMatt Macy 	list_t nolwb_waiters;
2370eda14cbcSMatt Macy 	lwb_t *lwb;
2371eda14cbcSMatt Macy 	itx_t *itx;
2372eda14cbcSMatt Macy 
2373eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&zilog->zl_issuer_lock));
2374eda14cbcSMatt Macy 
2375eda14cbcSMatt Macy 	/*
2376eda14cbcSMatt Macy 	 * Return if there's nothing to commit before we dirty the fs by
2377eda14cbcSMatt Macy 	 * calling zil_create().
2378eda14cbcSMatt Macy 	 */
2379eda14cbcSMatt Macy 	if (list_head(&zilog->zl_itx_commit_list) == NULL)
2380eda14cbcSMatt Macy 		return;
2381eda14cbcSMatt Macy 
2382eda14cbcSMatt Macy 	list_create(&nolwb_itxs, sizeof (itx_t), offsetof(itx_t, itx_node));
2383eda14cbcSMatt Macy 	list_create(&nolwb_waiters, sizeof (zil_commit_waiter_t),
2384eda14cbcSMatt Macy 	    offsetof(zil_commit_waiter_t, zcw_node));
2385eda14cbcSMatt Macy 
2386eda14cbcSMatt Macy 	lwb = list_tail(&zilog->zl_lwb_list);
2387eda14cbcSMatt Macy 	if (lwb == NULL) {
2388eda14cbcSMatt Macy 		lwb = zil_create(zilog);
2389eda14cbcSMatt Macy 	} else {
2390c03c5b1cSMartin Matuska 		/*
2391c03c5b1cSMartin Matuska 		 * Activate SPA_FEATURE_ZILSAXATTR for the cases where ZIL will
2392c03c5b1cSMartin Matuska 		 * have already been created (zl_lwb_list not empty).
2393c03c5b1cSMartin Matuska 		 */
2394c03c5b1cSMartin Matuska 		zil_commit_activate_saxattr_feature(zilog);
2395eda14cbcSMatt Macy 		ASSERT3S(lwb->lwb_state, !=, LWB_STATE_ISSUED);
2396eda14cbcSMatt Macy 		ASSERT3S(lwb->lwb_state, !=, LWB_STATE_WRITE_DONE);
2397eda14cbcSMatt Macy 		ASSERT3S(lwb->lwb_state, !=, LWB_STATE_FLUSH_DONE);
2398eda14cbcSMatt Macy 	}
2399eda14cbcSMatt Macy 
2400eda14cbcSMatt Macy 	while ((itx = list_head(&zilog->zl_itx_commit_list)) != NULL) {
2401eda14cbcSMatt Macy 		lr_t *lrc = &itx->itx_lr;
2402eda14cbcSMatt Macy 		uint64_t txg = lrc->lrc_txg;
2403eda14cbcSMatt Macy 
2404eda14cbcSMatt Macy 		ASSERT3U(txg, !=, 0);
2405eda14cbcSMatt Macy 
2406eda14cbcSMatt Macy 		if (lrc->lrc_txtype == TX_COMMIT) {
2407eda14cbcSMatt Macy 			DTRACE_PROBE2(zil__process__commit__itx,
2408eda14cbcSMatt Macy 			    zilog_t *, zilog, itx_t *, itx);
2409eda14cbcSMatt Macy 		} else {
2410eda14cbcSMatt Macy 			DTRACE_PROBE2(zil__process__normal__itx,
2411eda14cbcSMatt Macy 			    zilog_t *, zilog, itx_t *, itx);
2412eda14cbcSMatt Macy 		}
2413eda14cbcSMatt Macy 
2414eda14cbcSMatt Macy 		list_remove(&zilog->zl_itx_commit_list, itx);
2415eda14cbcSMatt Macy 
2416eda14cbcSMatt Macy 		boolean_t synced = txg <= spa_last_synced_txg(spa);
2417eda14cbcSMatt Macy 		boolean_t frozen = txg > spa_freeze_txg(spa);
2418eda14cbcSMatt Macy 
2419eda14cbcSMatt Macy 		/*
2420eda14cbcSMatt Macy 		 * If the txg of this itx has already been synced out, then
2421eda14cbcSMatt Macy 		 * we don't need to commit this itx to an lwb. This is
2422eda14cbcSMatt Macy 		 * because the data of this itx will have already been
2423eda14cbcSMatt Macy 		 * written to the main pool. This is inherently racy, and
2424eda14cbcSMatt Macy 		 * it's still ok to commit an itx whose txg has already
2425eda14cbcSMatt Macy 		 * been synced; this will result in a write that's
2426eda14cbcSMatt Macy 		 * unnecessary, but will do no harm.
2427eda14cbcSMatt Macy 		 *
2428eda14cbcSMatt Macy 		 * With that said, we always want to commit TX_COMMIT itxs
2429eda14cbcSMatt Macy 		 * to an lwb, regardless of whether or not that itx's txg
2430eda14cbcSMatt Macy 		 * has been synced out. We do this to ensure any OPENED lwb
2431eda14cbcSMatt Macy 		 * will always have at least one zil_commit_waiter_t linked
2432eda14cbcSMatt Macy 		 * to the lwb.
2433eda14cbcSMatt Macy 		 *
2434eda14cbcSMatt Macy 		 * As a counter-example, if we skipped TX_COMMIT itx's
2435eda14cbcSMatt Macy 		 * whose txg had already been synced, the following
2436eda14cbcSMatt Macy 		 * situation could occur if we happened to be racing with
2437eda14cbcSMatt Macy 		 * spa_sync:
2438eda14cbcSMatt Macy 		 *
2439eda14cbcSMatt Macy 		 * 1. We commit a non-TX_COMMIT itx to an lwb, where the
2440eda14cbcSMatt Macy 		 *    itx's txg is 10 and the last synced txg is 9.
2441eda14cbcSMatt Macy 		 * 2. spa_sync finishes syncing out txg 10.
2442eda14cbcSMatt Macy 		 * 3. We move to the next itx in the list, it's a TX_COMMIT
2443eda14cbcSMatt Macy 		 *    whose txg is 10, so we skip it rather than committing
2444eda14cbcSMatt Macy 		 *    it to the lwb used in (1).
2445eda14cbcSMatt Macy 		 *
2446eda14cbcSMatt Macy 		 * If the itx that is skipped in (3) is the last TX_COMMIT
2447eda14cbcSMatt Macy 		 * itx in the commit list, than it's possible for the lwb
2448eda14cbcSMatt Macy 		 * used in (1) to remain in the OPENED state indefinitely.
2449eda14cbcSMatt Macy 		 *
2450eda14cbcSMatt Macy 		 * To prevent the above scenario from occurring, ensuring
2451eda14cbcSMatt Macy 		 * that once an lwb is OPENED it will transition to ISSUED
2452eda14cbcSMatt Macy 		 * and eventually DONE, we always commit TX_COMMIT itx's to
2453eda14cbcSMatt Macy 		 * an lwb here, even if that itx's txg has already been
2454eda14cbcSMatt Macy 		 * synced.
2455eda14cbcSMatt Macy 		 *
2456eda14cbcSMatt Macy 		 * Finally, if the pool is frozen, we _always_ commit the
2457eda14cbcSMatt Macy 		 * itx.  The point of freezing the pool is to prevent data
2458eda14cbcSMatt Macy 		 * from being written to the main pool via spa_sync, and
2459eda14cbcSMatt Macy 		 * instead rely solely on the ZIL to persistently store the
2460eda14cbcSMatt Macy 		 * data; i.e.  when the pool is frozen, the last synced txg
2461eda14cbcSMatt Macy 		 * value can't be trusted.
2462eda14cbcSMatt Macy 		 */
2463eda14cbcSMatt Macy 		if (frozen || !synced || lrc->lrc_txtype == TX_COMMIT) {
2464eda14cbcSMatt Macy 			if (lwb != NULL) {
2465eda14cbcSMatt Macy 				lwb = zil_lwb_commit(zilog, itx, lwb);
2466eda14cbcSMatt Macy 
2467eda14cbcSMatt Macy 				if (lwb == NULL)
2468eda14cbcSMatt Macy 					list_insert_tail(&nolwb_itxs, itx);
2469eda14cbcSMatt Macy 				else
2470eda14cbcSMatt Macy 					list_insert_tail(&lwb->lwb_itxs, itx);
2471eda14cbcSMatt Macy 			} else {
2472eda14cbcSMatt Macy 				if (lrc->lrc_txtype == TX_COMMIT) {
2473eda14cbcSMatt Macy 					zil_commit_waiter_link_nolwb(
2474eda14cbcSMatt Macy 					    itx->itx_private, &nolwb_waiters);
2475eda14cbcSMatt Macy 				}
2476eda14cbcSMatt Macy 
2477eda14cbcSMatt Macy 				list_insert_tail(&nolwb_itxs, itx);
2478eda14cbcSMatt Macy 			}
2479eda14cbcSMatt Macy 		} else {
2480eda14cbcSMatt Macy 			ASSERT3S(lrc->lrc_txtype, !=, TX_COMMIT);
2481eda14cbcSMatt Macy 			zil_itx_destroy(itx);
2482eda14cbcSMatt Macy 		}
2483eda14cbcSMatt Macy 	}
2484eda14cbcSMatt Macy 
2485eda14cbcSMatt Macy 	if (lwb == NULL) {
2486eda14cbcSMatt Macy 		/*
2487eda14cbcSMatt Macy 		 * This indicates zio_alloc_zil() failed to allocate the
2488eda14cbcSMatt Macy 		 * "next" lwb on-disk. When this happens, we must stall
2489eda14cbcSMatt Macy 		 * the ZIL write pipeline; see the comment within
2490eda14cbcSMatt Macy 		 * zil_commit_writer_stall() for more details.
2491eda14cbcSMatt Macy 		 */
2492eda14cbcSMatt Macy 		zil_commit_writer_stall(zilog);
2493eda14cbcSMatt Macy 
2494eda14cbcSMatt Macy 		/*
2495eda14cbcSMatt Macy 		 * Additionally, we have to signal and mark the "nolwb"
2496eda14cbcSMatt Macy 		 * waiters as "done" here, since without an lwb, we
2497eda14cbcSMatt Macy 		 * can't do this via zil_lwb_flush_vdevs_done() like
2498eda14cbcSMatt Macy 		 * normal.
2499eda14cbcSMatt Macy 		 */
2500eda14cbcSMatt Macy 		zil_commit_waiter_t *zcw;
2501eda14cbcSMatt Macy 		while ((zcw = list_head(&nolwb_waiters)) != NULL) {
2502eda14cbcSMatt Macy 			zil_commit_waiter_skip(zcw);
2503eda14cbcSMatt Macy 			list_remove(&nolwb_waiters, zcw);
2504eda14cbcSMatt Macy 		}
2505eda14cbcSMatt Macy 
2506eda14cbcSMatt Macy 		/*
2507eda14cbcSMatt Macy 		 * And finally, we have to destroy the itx's that
2508eda14cbcSMatt Macy 		 * couldn't be committed to an lwb; this will also call
2509eda14cbcSMatt Macy 		 * the itx's callback if one exists for the itx.
2510eda14cbcSMatt Macy 		 */
2511eda14cbcSMatt Macy 		while ((itx = list_head(&nolwb_itxs)) != NULL) {
2512eda14cbcSMatt Macy 			list_remove(&nolwb_itxs, itx);
2513eda14cbcSMatt Macy 			zil_itx_destroy(itx);
2514eda14cbcSMatt Macy 		}
2515eda14cbcSMatt Macy 	} else {
2516eda14cbcSMatt Macy 		ASSERT(list_is_empty(&nolwb_waiters));
2517eda14cbcSMatt Macy 		ASSERT3P(lwb, !=, NULL);
2518eda14cbcSMatt Macy 		ASSERT3S(lwb->lwb_state, !=, LWB_STATE_ISSUED);
2519eda14cbcSMatt Macy 		ASSERT3S(lwb->lwb_state, !=, LWB_STATE_WRITE_DONE);
2520eda14cbcSMatt Macy 		ASSERT3S(lwb->lwb_state, !=, LWB_STATE_FLUSH_DONE);
2521eda14cbcSMatt Macy 
2522eda14cbcSMatt Macy 		/*
2523eda14cbcSMatt Macy 		 * At this point, the ZIL block pointed at by the "lwb"
2524eda14cbcSMatt Macy 		 * variable is in one of the following states: "closed"
2525eda14cbcSMatt Macy 		 * or "open".
2526eda14cbcSMatt Macy 		 *
2527eda14cbcSMatt Macy 		 * If it's "closed", then no itxs have been committed to
2528eda14cbcSMatt Macy 		 * it, so there's no point in issuing its zio (i.e. it's
2529eda14cbcSMatt Macy 		 * "empty").
2530eda14cbcSMatt Macy 		 *
2531eda14cbcSMatt Macy 		 * If it's "open", then it contains one or more itxs that
2532eda14cbcSMatt Macy 		 * eventually need to be committed to stable storage. In
2533eda14cbcSMatt Macy 		 * this case we intentionally do not issue the lwb's zio
2534eda14cbcSMatt Macy 		 * to disk yet, and instead rely on one of the following
2535eda14cbcSMatt Macy 		 * two mechanisms for issuing the zio:
2536eda14cbcSMatt Macy 		 *
2537eda14cbcSMatt Macy 		 * 1. Ideally, there will be more ZIL activity occurring
2538eda14cbcSMatt Macy 		 * on the system, such that this function will be
2539eda14cbcSMatt Macy 		 * immediately called again (not necessarily by the same
2540eda14cbcSMatt Macy 		 * thread) and this lwb's zio will be issued via
2541eda14cbcSMatt Macy 		 * zil_lwb_commit(). This way, the lwb is guaranteed to
2542eda14cbcSMatt Macy 		 * be "full" when it is issued to disk, and we'll make
2543eda14cbcSMatt Macy 		 * use of the lwb's size the best we can.
2544eda14cbcSMatt Macy 		 *
2545eda14cbcSMatt Macy 		 * 2. If there isn't sufficient ZIL activity occurring on
2546eda14cbcSMatt Macy 		 * the system, such that this lwb's zio isn't issued via
2547eda14cbcSMatt Macy 		 * zil_lwb_commit(), zil_commit_waiter() will issue the
2548eda14cbcSMatt Macy 		 * lwb's zio. If this occurs, the lwb is not guaranteed
2549eda14cbcSMatt Macy 		 * to be "full" by the time its zio is issued, and means
2550eda14cbcSMatt Macy 		 * the size of the lwb was "too large" given the amount
2551eda14cbcSMatt Macy 		 * of ZIL activity occurring on the system at that time.
2552eda14cbcSMatt Macy 		 *
2553eda14cbcSMatt Macy 		 * We do this for a couple of reasons:
2554eda14cbcSMatt Macy 		 *
2555eda14cbcSMatt Macy 		 * 1. To try and reduce the number of IOPs needed to
2556eda14cbcSMatt Macy 		 * write the same number of itxs. If an lwb has space
2557eda14cbcSMatt Macy 		 * available in its buffer for more itxs, and more itxs
2558eda14cbcSMatt Macy 		 * will be committed relatively soon (relative to the
2559eda14cbcSMatt Macy 		 * latency of performing a write), then it's beneficial
2560eda14cbcSMatt Macy 		 * to wait for these "next" itxs. This way, more itxs
2561eda14cbcSMatt Macy 		 * can be committed to stable storage with fewer writes.
2562eda14cbcSMatt Macy 		 *
2563eda14cbcSMatt Macy 		 * 2. To try and use the largest lwb block size that the
2564eda14cbcSMatt Macy 		 * incoming rate of itxs can support. Again, this is to
2565eda14cbcSMatt Macy 		 * try and pack as many itxs into as few lwbs as
2566eda14cbcSMatt Macy 		 * possible, without significantly impacting the latency
2567eda14cbcSMatt Macy 		 * of each individual itx.
2568eda14cbcSMatt Macy 		 */
2569eda14cbcSMatt Macy 	}
2570eda14cbcSMatt Macy }
2571eda14cbcSMatt Macy 
2572eda14cbcSMatt Macy /*
2573eda14cbcSMatt Macy  * This function is responsible for ensuring the passed in commit waiter
2574eda14cbcSMatt Macy  * (and associated commit itx) is committed to an lwb. If the waiter is
2575eda14cbcSMatt Macy  * not already committed to an lwb, all itxs in the zilog's queue of
2576eda14cbcSMatt Macy  * itxs will be processed. The assumption is the passed in waiter's
2577eda14cbcSMatt Macy  * commit itx will found in the queue just like the other non-commit
2578eda14cbcSMatt Macy  * itxs, such that when the entire queue is processed, the waiter will
2579eda14cbcSMatt Macy  * have been committed to an lwb.
2580eda14cbcSMatt Macy  *
2581eda14cbcSMatt Macy  * The lwb associated with the passed in waiter is not guaranteed to
2582eda14cbcSMatt Macy  * have been issued by the time this function completes. If the lwb is
2583eda14cbcSMatt Macy  * not issued, we rely on future calls to zil_commit_writer() to issue
2584eda14cbcSMatt Macy  * the lwb, or the timeout mechanism found in zil_commit_waiter().
2585eda14cbcSMatt Macy  */
2586eda14cbcSMatt Macy static void
2587eda14cbcSMatt Macy zil_commit_writer(zilog_t *zilog, zil_commit_waiter_t *zcw)
2588eda14cbcSMatt Macy {
2589eda14cbcSMatt Macy 	ASSERT(!MUTEX_HELD(&zilog->zl_lock));
2590eda14cbcSMatt Macy 	ASSERT(spa_writeable(zilog->zl_spa));
2591eda14cbcSMatt Macy 
2592eda14cbcSMatt Macy 	mutex_enter(&zilog->zl_issuer_lock);
2593eda14cbcSMatt Macy 
2594eda14cbcSMatt Macy 	if (zcw->zcw_lwb != NULL || zcw->zcw_done) {
2595eda14cbcSMatt Macy 		/*
2596eda14cbcSMatt Macy 		 * It's possible that, while we were waiting to acquire
2597eda14cbcSMatt Macy 		 * the "zl_issuer_lock", another thread committed this
2598eda14cbcSMatt Macy 		 * waiter to an lwb. If that occurs, we bail out early,
2599eda14cbcSMatt Macy 		 * without processing any of the zilog's queue of itxs.
2600eda14cbcSMatt Macy 		 *
2601eda14cbcSMatt Macy 		 * On certain workloads and system configurations, the
2602eda14cbcSMatt Macy 		 * "zl_issuer_lock" can become highly contended. In an
2603eda14cbcSMatt Macy 		 * attempt to reduce this contention, we immediately drop
2604eda14cbcSMatt Macy 		 * the lock if the waiter has already been processed.
2605eda14cbcSMatt Macy 		 *
2606eda14cbcSMatt Macy 		 * We've measured this optimization to reduce CPU spent
2607eda14cbcSMatt Macy 		 * contending on this lock by up to 5%, using a system
2608eda14cbcSMatt Macy 		 * with 32 CPUs, low latency storage (~50 usec writes),
2609eda14cbcSMatt Macy 		 * and 1024 threads performing sync writes.
2610eda14cbcSMatt Macy 		 */
2611eda14cbcSMatt Macy 		goto out;
2612eda14cbcSMatt Macy 	}
2613eda14cbcSMatt Macy 
2614eda14cbcSMatt Macy 	ZIL_STAT_BUMP(zil_commit_writer_count);
2615eda14cbcSMatt Macy 
2616eda14cbcSMatt Macy 	zil_get_commit_list(zilog);
2617eda14cbcSMatt Macy 	zil_prune_commit_list(zilog);
2618eda14cbcSMatt Macy 	zil_process_commit_list(zilog);
2619eda14cbcSMatt Macy 
2620eda14cbcSMatt Macy out:
2621eda14cbcSMatt Macy 	mutex_exit(&zilog->zl_issuer_lock);
2622eda14cbcSMatt Macy }
2623eda14cbcSMatt Macy 
2624eda14cbcSMatt Macy static void
2625eda14cbcSMatt Macy zil_commit_waiter_timeout(zilog_t *zilog, zil_commit_waiter_t *zcw)
2626eda14cbcSMatt Macy {
2627eda14cbcSMatt Macy 	ASSERT(!MUTEX_HELD(&zilog->zl_issuer_lock));
2628eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&zcw->zcw_lock));
2629eda14cbcSMatt Macy 	ASSERT3B(zcw->zcw_done, ==, B_FALSE);
2630eda14cbcSMatt Macy 
2631eda14cbcSMatt Macy 	lwb_t *lwb = zcw->zcw_lwb;
2632eda14cbcSMatt Macy 	ASSERT3P(lwb, !=, NULL);
2633eda14cbcSMatt Macy 	ASSERT3S(lwb->lwb_state, !=, LWB_STATE_CLOSED);
2634eda14cbcSMatt Macy 
2635eda14cbcSMatt Macy 	/*
2636eda14cbcSMatt Macy 	 * If the lwb has already been issued by another thread, we can
2637eda14cbcSMatt Macy 	 * immediately return since there's no work to be done (the
2638eda14cbcSMatt Macy 	 * point of this function is to issue the lwb). Additionally, we
2639eda14cbcSMatt Macy 	 * do this prior to acquiring the zl_issuer_lock, to avoid
2640eda14cbcSMatt Macy 	 * acquiring it when it's not necessary to do so.
2641eda14cbcSMatt Macy 	 */
2642eda14cbcSMatt Macy 	if (lwb->lwb_state == LWB_STATE_ISSUED ||
2643eda14cbcSMatt Macy 	    lwb->lwb_state == LWB_STATE_WRITE_DONE ||
2644eda14cbcSMatt Macy 	    lwb->lwb_state == LWB_STATE_FLUSH_DONE)
2645eda14cbcSMatt Macy 		return;
2646eda14cbcSMatt Macy 
2647eda14cbcSMatt Macy 	/*
2648eda14cbcSMatt Macy 	 * In order to call zil_lwb_write_issue() we must hold the
2649eda14cbcSMatt Macy 	 * zilog's "zl_issuer_lock". We can't simply acquire that lock,
2650eda14cbcSMatt Macy 	 * since we're already holding the commit waiter's "zcw_lock",
2651eda14cbcSMatt Macy 	 * and those two locks are acquired in the opposite order
2652eda14cbcSMatt Macy 	 * elsewhere.
2653eda14cbcSMatt Macy 	 */
2654eda14cbcSMatt Macy 	mutex_exit(&zcw->zcw_lock);
2655eda14cbcSMatt Macy 	mutex_enter(&zilog->zl_issuer_lock);
2656eda14cbcSMatt Macy 	mutex_enter(&zcw->zcw_lock);
2657eda14cbcSMatt Macy 
2658eda14cbcSMatt Macy 	/*
2659eda14cbcSMatt Macy 	 * Since we just dropped and re-acquired the commit waiter's
2660eda14cbcSMatt Macy 	 * lock, we have to re-check to see if the waiter was marked
2661eda14cbcSMatt Macy 	 * "done" during that process. If the waiter was marked "done",
2662eda14cbcSMatt Macy 	 * the "lwb" pointer is no longer valid (it can be free'd after
2663eda14cbcSMatt Macy 	 * the waiter is marked "done"), so without this check we could
2664eda14cbcSMatt Macy 	 * wind up with a use-after-free error below.
2665eda14cbcSMatt Macy 	 */
2666eda14cbcSMatt Macy 	if (zcw->zcw_done)
2667eda14cbcSMatt Macy 		goto out;
2668eda14cbcSMatt Macy 
2669eda14cbcSMatt Macy 	ASSERT3P(lwb, ==, zcw->zcw_lwb);
2670eda14cbcSMatt Macy 
2671eda14cbcSMatt Macy 	/*
2672eda14cbcSMatt Macy 	 * We've already checked this above, but since we hadn't acquired
2673eda14cbcSMatt Macy 	 * the zilog's zl_issuer_lock, we have to perform this check a
2674eda14cbcSMatt Macy 	 * second time while holding the lock.
2675eda14cbcSMatt Macy 	 *
2676eda14cbcSMatt Macy 	 * We don't need to hold the zl_lock since the lwb cannot transition
2677eda14cbcSMatt Macy 	 * from OPENED to ISSUED while we hold the zl_issuer_lock. The lwb
2678eda14cbcSMatt Macy 	 * _can_ transition from ISSUED to DONE, but it's OK to race with
2679eda14cbcSMatt Macy 	 * that transition since we treat the lwb the same, whether it's in
2680eda14cbcSMatt Macy 	 * the ISSUED or DONE states.
2681eda14cbcSMatt Macy 	 *
2682eda14cbcSMatt Macy 	 * The important thing, is we treat the lwb differently depending on
2683eda14cbcSMatt Macy 	 * if it's ISSUED or OPENED, and block any other threads that might
2684eda14cbcSMatt Macy 	 * attempt to issue this lwb. For that reason we hold the
2685eda14cbcSMatt Macy 	 * zl_issuer_lock when checking the lwb_state; we must not call
2686eda14cbcSMatt Macy 	 * zil_lwb_write_issue() if the lwb had already been issued.
2687eda14cbcSMatt Macy 	 *
2688eda14cbcSMatt Macy 	 * See the comment above the lwb_state_t structure definition for
2689eda14cbcSMatt Macy 	 * more details on the lwb states, and locking requirements.
2690eda14cbcSMatt Macy 	 */
2691eda14cbcSMatt Macy 	if (lwb->lwb_state == LWB_STATE_ISSUED ||
2692eda14cbcSMatt Macy 	    lwb->lwb_state == LWB_STATE_WRITE_DONE ||
2693eda14cbcSMatt Macy 	    lwb->lwb_state == LWB_STATE_FLUSH_DONE)
2694eda14cbcSMatt Macy 		goto out;
2695eda14cbcSMatt Macy 
2696eda14cbcSMatt Macy 	ASSERT3S(lwb->lwb_state, ==, LWB_STATE_OPENED);
2697eda14cbcSMatt Macy 
2698eda14cbcSMatt Macy 	/*
2699eda14cbcSMatt Macy 	 * As described in the comments above zil_commit_waiter() and
2700eda14cbcSMatt Macy 	 * zil_process_commit_list(), we need to issue this lwb's zio
2701eda14cbcSMatt Macy 	 * since we've reached the commit waiter's timeout and it still
2702eda14cbcSMatt Macy 	 * hasn't been issued.
2703eda14cbcSMatt Macy 	 */
2704eda14cbcSMatt Macy 	lwb_t *nlwb = zil_lwb_write_issue(zilog, lwb);
2705eda14cbcSMatt Macy 
2706eda14cbcSMatt Macy 	IMPLY(nlwb != NULL, lwb->lwb_state != LWB_STATE_OPENED);
2707eda14cbcSMatt Macy 
2708eda14cbcSMatt Macy 	/*
2709eda14cbcSMatt Macy 	 * Since the lwb's zio hadn't been issued by the time this thread
2710eda14cbcSMatt Macy 	 * reached its timeout, we reset the zilog's "zl_cur_used" field
2711eda14cbcSMatt Macy 	 * to influence the zil block size selection algorithm.
2712eda14cbcSMatt Macy 	 *
2713eda14cbcSMatt Macy 	 * By having to issue the lwb's zio here, it means the size of the
2714eda14cbcSMatt Macy 	 * lwb was too large, given the incoming throughput of itxs.  By
2715eda14cbcSMatt Macy 	 * setting "zl_cur_used" to zero, we communicate this fact to the
2716eda14cbcSMatt Macy 	 * block size selection algorithm, so it can take this information
2717eda14cbcSMatt Macy 	 * into account, and potentially select a smaller size for the
2718eda14cbcSMatt Macy 	 * next lwb block that is allocated.
2719eda14cbcSMatt Macy 	 */
2720eda14cbcSMatt Macy 	zilog->zl_cur_used = 0;
2721eda14cbcSMatt Macy 
2722eda14cbcSMatt Macy 	if (nlwb == NULL) {
2723eda14cbcSMatt Macy 		/*
2724eda14cbcSMatt Macy 		 * When zil_lwb_write_issue() returns NULL, this
2725eda14cbcSMatt Macy 		 * indicates zio_alloc_zil() failed to allocate the
2726eda14cbcSMatt Macy 		 * "next" lwb on-disk. When this occurs, the ZIL write
2727eda14cbcSMatt Macy 		 * pipeline must be stalled; see the comment within the
2728eda14cbcSMatt Macy 		 * zil_commit_writer_stall() function for more details.
2729eda14cbcSMatt Macy 		 *
2730eda14cbcSMatt Macy 		 * We must drop the commit waiter's lock prior to
2731eda14cbcSMatt Macy 		 * calling zil_commit_writer_stall() or else we can wind
2732eda14cbcSMatt Macy 		 * up with the following deadlock:
2733eda14cbcSMatt Macy 		 *
2734eda14cbcSMatt Macy 		 * - This thread is waiting for the txg to sync while
2735eda14cbcSMatt Macy 		 *   holding the waiter's lock; txg_wait_synced() is
2736eda14cbcSMatt Macy 		 *   used within txg_commit_writer_stall().
2737eda14cbcSMatt Macy 		 *
2738eda14cbcSMatt Macy 		 * - The txg can't sync because it is waiting for this
2739eda14cbcSMatt Macy 		 *   lwb's zio callback to call dmu_tx_commit().
2740eda14cbcSMatt Macy 		 *
2741eda14cbcSMatt Macy 		 * - The lwb's zio callback can't call dmu_tx_commit()
2742eda14cbcSMatt Macy 		 *   because it's blocked trying to acquire the waiter's
2743eda14cbcSMatt Macy 		 *   lock, which occurs prior to calling dmu_tx_commit()
2744eda14cbcSMatt Macy 		 */
2745eda14cbcSMatt Macy 		mutex_exit(&zcw->zcw_lock);
2746eda14cbcSMatt Macy 		zil_commit_writer_stall(zilog);
2747eda14cbcSMatt Macy 		mutex_enter(&zcw->zcw_lock);
2748eda14cbcSMatt Macy 	}
2749eda14cbcSMatt Macy 
2750eda14cbcSMatt Macy out:
2751eda14cbcSMatt Macy 	mutex_exit(&zilog->zl_issuer_lock);
2752eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&zcw->zcw_lock));
2753eda14cbcSMatt Macy }
2754eda14cbcSMatt Macy 
2755eda14cbcSMatt Macy /*
2756eda14cbcSMatt Macy  * This function is responsible for performing the following two tasks:
2757eda14cbcSMatt Macy  *
2758eda14cbcSMatt Macy  * 1. its primary responsibility is to block until the given "commit
2759eda14cbcSMatt Macy  *    waiter" is considered "done".
2760eda14cbcSMatt Macy  *
2761eda14cbcSMatt Macy  * 2. its secondary responsibility is to issue the zio for the lwb that
2762eda14cbcSMatt Macy  *    the given "commit waiter" is waiting on, if this function has
2763eda14cbcSMatt Macy  *    waited "long enough" and the lwb is still in the "open" state.
2764eda14cbcSMatt Macy  *
2765eda14cbcSMatt Macy  * Given a sufficient amount of itxs being generated and written using
2766eda14cbcSMatt Macy  * the ZIL, the lwb's zio will be issued via the zil_lwb_commit()
2767eda14cbcSMatt Macy  * function. If this does not occur, this secondary responsibility will
2768eda14cbcSMatt Macy  * ensure the lwb is issued even if there is not other synchronous
2769eda14cbcSMatt Macy  * activity on the system.
2770eda14cbcSMatt Macy  *
2771eda14cbcSMatt Macy  * For more details, see zil_process_commit_list(); more specifically,
2772eda14cbcSMatt Macy  * the comment at the bottom of that function.
2773eda14cbcSMatt Macy  */
2774eda14cbcSMatt Macy static void
2775eda14cbcSMatt Macy zil_commit_waiter(zilog_t *zilog, zil_commit_waiter_t *zcw)
2776eda14cbcSMatt Macy {
2777eda14cbcSMatt Macy 	ASSERT(!MUTEX_HELD(&zilog->zl_lock));
2778eda14cbcSMatt Macy 	ASSERT(!MUTEX_HELD(&zilog->zl_issuer_lock));
2779eda14cbcSMatt Macy 	ASSERT(spa_writeable(zilog->zl_spa));
2780eda14cbcSMatt Macy 
2781eda14cbcSMatt Macy 	mutex_enter(&zcw->zcw_lock);
2782eda14cbcSMatt Macy 
2783eda14cbcSMatt Macy 	/*
2784eda14cbcSMatt Macy 	 * The timeout is scaled based on the lwb latency to avoid
2785eda14cbcSMatt Macy 	 * significantly impacting the latency of each individual itx.
2786eda14cbcSMatt Macy 	 * For more details, see the comment at the bottom of the
2787eda14cbcSMatt Macy 	 * zil_process_commit_list() function.
2788eda14cbcSMatt Macy 	 */
2789eda14cbcSMatt Macy 	int pct = MAX(zfs_commit_timeout_pct, 1);
2790eda14cbcSMatt Macy 	hrtime_t sleep = (zilog->zl_last_lwb_latency * pct) / 100;
2791eda14cbcSMatt Macy 	hrtime_t wakeup = gethrtime() + sleep;
2792eda14cbcSMatt Macy 	boolean_t timedout = B_FALSE;
2793eda14cbcSMatt Macy 
2794eda14cbcSMatt Macy 	while (!zcw->zcw_done) {
2795eda14cbcSMatt Macy 		ASSERT(MUTEX_HELD(&zcw->zcw_lock));
2796eda14cbcSMatt Macy 
2797eda14cbcSMatt Macy 		lwb_t *lwb = zcw->zcw_lwb;
2798eda14cbcSMatt Macy 
2799eda14cbcSMatt Macy 		/*
2800eda14cbcSMatt Macy 		 * Usually, the waiter will have a non-NULL lwb field here,
2801eda14cbcSMatt Macy 		 * but it's possible for it to be NULL as a result of
2802eda14cbcSMatt Macy 		 * zil_commit() racing with spa_sync().
2803eda14cbcSMatt Macy 		 *
2804eda14cbcSMatt Macy 		 * When zil_clean() is called, it's possible for the itxg
2805eda14cbcSMatt Macy 		 * list (which may be cleaned via a taskq) to contain
2806eda14cbcSMatt Macy 		 * commit itxs. When this occurs, the commit waiters linked
2807eda14cbcSMatt Macy 		 * off of these commit itxs will not be committed to an
2808eda14cbcSMatt Macy 		 * lwb.  Additionally, these commit waiters will not be
2809eda14cbcSMatt Macy 		 * marked done until zil_commit_waiter_skip() is called via
2810eda14cbcSMatt Macy 		 * zil_itxg_clean().
2811eda14cbcSMatt Macy 		 *
2812eda14cbcSMatt Macy 		 * Thus, it's possible for this commit waiter (i.e. the
2813eda14cbcSMatt Macy 		 * "zcw" variable) to be found in this "in between" state;
2814eda14cbcSMatt Macy 		 * where it's "zcw_lwb" field is NULL, and it hasn't yet
2815eda14cbcSMatt Macy 		 * been skipped, so it's "zcw_done" field is still B_FALSE.
2816eda14cbcSMatt Macy 		 */
2817eda14cbcSMatt Macy 		IMPLY(lwb != NULL, lwb->lwb_state != LWB_STATE_CLOSED);
2818eda14cbcSMatt Macy 
2819eda14cbcSMatt Macy 		if (lwb != NULL && lwb->lwb_state == LWB_STATE_OPENED) {
2820eda14cbcSMatt Macy 			ASSERT3B(timedout, ==, B_FALSE);
2821eda14cbcSMatt Macy 
2822eda14cbcSMatt Macy 			/*
2823eda14cbcSMatt Macy 			 * If the lwb hasn't been issued yet, then we
2824eda14cbcSMatt Macy 			 * need to wait with a timeout, in case this
2825eda14cbcSMatt Macy 			 * function needs to issue the lwb after the
2826eda14cbcSMatt Macy 			 * timeout is reached; responsibility (2) from
2827eda14cbcSMatt Macy 			 * the comment above this function.
2828eda14cbcSMatt Macy 			 */
2829eda14cbcSMatt Macy 			int rc = cv_timedwait_hires(&zcw->zcw_cv,
2830eda14cbcSMatt Macy 			    &zcw->zcw_lock, wakeup, USEC2NSEC(1),
2831eda14cbcSMatt Macy 			    CALLOUT_FLAG_ABSOLUTE);
2832eda14cbcSMatt Macy 
2833eda14cbcSMatt Macy 			if (rc != -1 || zcw->zcw_done)
2834eda14cbcSMatt Macy 				continue;
2835eda14cbcSMatt Macy 
2836eda14cbcSMatt Macy 			timedout = B_TRUE;
2837eda14cbcSMatt Macy 			zil_commit_waiter_timeout(zilog, zcw);
2838eda14cbcSMatt Macy 
2839eda14cbcSMatt Macy 			if (!zcw->zcw_done) {
2840eda14cbcSMatt Macy 				/*
2841eda14cbcSMatt Macy 				 * If the commit waiter has already been
2842eda14cbcSMatt Macy 				 * marked "done", it's possible for the
2843eda14cbcSMatt Macy 				 * waiter's lwb structure to have already
2844eda14cbcSMatt Macy 				 * been freed.  Thus, we can only reliably
2845eda14cbcSMatt Macy 				 * make these assertions if the waiter
2846eda14cbcSMatt Macy 				 * isn't done.
2847eda14cbcSMatt Macy 				 */
2848eda14cbcSMatt Macy 				ASSERT3P(lwb, ==, zcw->zcw_lwb);
2849eda14cbcSMatt Macy 				ASSERT3S(lwb->lwb_state, !=, LWB_STATE_OPENED);
2850eda14cbcSMatt Macy 			}
2851eda14cbcSMatt Macy 		} else {
2852eda14cbcSMatt Macy 			/*
2853eda14cbcSMatt Macy 			 * If the lwb isn't open, then it must have already
2854eda14cbcSMatt Macy 			 * been issued. In that case, there's no need to
2855eda14cbcSMatt Macy 			 * use a timeout when waiting for the lwb to
2856eda14cbcSMatt Macy 			 * complete.
2857eda14cbcSMatt Macy 			 *
2858eda14cbcSMatt Macy 			 * Additionally, if the lwb is NULL, the waiter
2859eda14cbcSMatt Macy 			 * will soon be signaled and marked done via
2860eda14cbcSMatt Macy 			 * zil_clean() and zil_itxg_clean(), so no timeout
2861eda14cbcSMatt Macy 			 * is required.
2862eda14cbcSMatt Macy 			 */
2863eda14cbcSMatt Macy 
2864eda14cbcSMatt Macy 			IMPLY(lwb != NULL,
2865eda14cbcSMatt Macy 			    lwb->lwb_state == LWB_STATE_ISSUED ||
2866eda14cbcSMatt Macy 			    lwb->lwb_state == LWB_STATE_WRITE_DONE ||
2867eda14cbcSMatt Macy 			    lwb->lwb_state == LWB_STATE_FLUSH_DONE);
2868eda14cbcSMatt Macy 			cv_wait(&zcw->zcw_cv, &zcw->zcw_lock);
2869eda14cbcSMatt Macy 		}
2870eda14cbcSMatt Macy 	}
2871eda14cbcSMatt Macy 
2872eda14cbcSMatt Macy 	mutex_exit(&zcw->zcw_lock);
2873eda14cbcSMatt Macy }
2874eda14cbcSMatt Macy 
2875eda14cbcSMatt Macy static zil_commit_waiter_t *
2876eda14cbcSMatt Macy zil_alloc_commit_waiter(void)
2877eda14cbcSMatt Macy {
2878eda14cbcSMatt Macy 	zil_commit_waiter_t *zcw = kmem_cache_alloc(zil_zcw_cache, KM_SLEEP);
2879eda14cbcSMatt Macy 
2880eda14cbcSMatt Macy 	cv_init(&zcw->zcw_cv, NULL, CV_DEFAULT, NULL);
2881eda14cbcSMatt Macy 	mutex_init(&zcw->zcw_lock, NULL, MUTEX_DEFAULT, NULL);
2882eda14cbcSMatt Macy 	list_link_init(&zcw->zcw_node);
2883eda14cbcSMatt Macy 	zcw->zcw_lwb = NULL;
2884eda14cbcSMatt Macy 	zcw->zcw_done = B_FALSE;
2885eda14cbcSMatt Macy 	zcw->zcw_zio_error = 0;
2886eda14cbcSMatt Macy 
2887eda14cbcSMatt Macy 	return (zcw);
2888eda14cbcSMatt Macy }
2889eda14cbcSMatt Macy 
2890eda14cbcSMatt Macy static void
2891eda14cbcSMatt Macy zil_free_commit_waiter(zil_commit_waiter_t *zcw)
2892eda14cbcSMatt Macy {
2893eda14cbcSMatt Macy 	ASSERT(!list_link_active(&zcw->zcw_node));
2894eda14cbcSMatt Macy 	ASSERT3P(zcw->zcw_lwb, ==, NULL);
2895eda14cbcSMatt Macy 	ASSERT3B(zcw->zcw_done, ==, B_TRUE);
2896eda14cbcSMatt Macy 	mutex_destroy(&zcw->zcw_lock);
2897eda14cbcSMatt Macy 	cv_destroy(&zcw->zcw_cv);
2898eda14cbcSMatt Macy 	kmem_cache_free(zil_zcw_cache, zcw);
2899eda14cbcSMatt Macy }
2900eda14cbcSMatt Macy 
2901eda14cbcSMatt Macy /*
2902eda14cbcSMatt Macy  * This function is used to create a TX_COMMIT itx and assign it. This
2903eda14cbcSMatt Macy  * way, it will be linked into the ZIL's list of synchronous itxs, and
2904eda14cbcSMatt Macy  * then later committed to an lwb (or skipped) when
2905eda14cbcSMatt Macy  * zil_process_commit_list() is called.
2906eda14cbcSMatt Macy  */
2907eda14cbcSMatt Macy static void
2908eda14cbcSMatt Macy zil_commit_itx_assign(zilog_t *zilog, zil_commit_waiter_t *zcw)
2909eda14cbcSMatt Macy {
2910eda14cbcSMatt Macy 	dmu_tx_t *tx = dmu_tx_create(zilog->zl_os);
2911eda14cbcSMatt Macy 	VERIFY0(dmu_tx_assign(tx, TXG_WAIT));
2912eda14cbcSMatt Macy 
2913eda14cbcSMatt Macy 	itx_t *itx = zil_itx_create(TX_COMMIT, sizeof (lr_t));
2914eda14cbcSMatt Macy 	itx->itx_sync = B_TRUE;
2915eda14cbcSMatt Macy 	itx->itx_private = zcw;
2916eda14cbcSMatt Macy 
2917eda14cbcSMatt Macy 	zil_itx_assign(zilog, itx, tx);
2918eda14cbcSMatt Macy 
2919eda14cbcSMatt Macy 	dmu_tx_commit(tx);
2920eda14cbcSMatt Macy }
2921eda14cbcSMatt Macy 
2922eda14cbcSMatt Macy /*
2923eda14cbcSMatt Macy  * Commit ZFS Intent Log transactions (itxs) to stable storage.
2924eda14cbcSMatt Macy  *
2925eda14cbcSMatt Macy  * When writing ZIL transactions to the on-disk representation of the
2926eda14cbcSMatt Macy  * ZIL, the itxs are committed to a Log Write Block (lwb). Multiple
2927eda14cbcSMatt Macy  * itxs can be committed to a single lwb. Once a lwb is written and
2928eda14cbcSMatt Macy  * committed to stable storage (i.e. the lwb is written, and vdevs have
2929eda14cbcSMatt Macy  * been flushed), each itx that was committed to that lwb is also
2930eda14cbcSMatt Macy  * considered to be committed to stable storage.
2931eda14cbcSMatt Macy  *
2932eda14cbcSMatt Macy  * When an itx is committed to an lwb, the log record (lr_t) contained
2933eda14cbcSMatt Macy  * by the itx is copied into the lwb's zio buffer, and once this buffer
2934eda14cbcSMatt Macy  * is written to disk, it becomes an on-disk ZIL block.
2935eda14cbcSMatt Macy  *
2936eda14cbcSMatt Macy  * As itxs are generated, they're inserted into the ZIL's queue of
2937eda14cbcSMatt Macy  * uncommitted itxs. The semantics of zil_commit() are such that it will
2938eda14cbcSMatt Macy  * block until all itxs that were in the queue when it was called, are
2939eda14cbcSMatt Macy  * committed to stable storage.
2940eda14cbcSMatt Macy  *
2941eda14cbcSMatt Macy  * If "foid" is zero, this means all "synchronous" and "asynchronous"
2942eda14cbcSMatt Macy  * itxs, for all objects in the dataset, will be committed to stable
2943eda14cbcSMatt Macy  * storage prior to zil_commit() returning. If "foid" is non-zero, all
2944eda14cbcSMatt Macy  * "synchronous" itxs for all objects, but only "asynchronous" itxs
2945eda14cbcSMatt Macy  * that correspond to the foid passed in, will be committed to stable
2946eda14cbcSMatt Macy  * storage prior to zil_commit() returning.
2947eda14cbcSMatt Macy  *
2948eda14cbcSMatt Macy  * Generally speaking, when zil_commit() is called, the consumer doesn't
2949eda14cbcSMatt Macy  * actually care about _all_ of the uncommitted itxs. Instead, they're
2950eda14cbcSMatt Macy  * simply trying to waiting for a specific itx to be committed to disk,
2951eda14cbcSMatt Macy  * but the interface(s) for interacting with the ZIL don't allow such
2952eda14cbcSMatt Macy  * fine-grained communication. A better interface would allow a consumer
2953eda14cbcSMatt Macy  * to create and assign an itx, and then pass a reference to this itx to
2954eda14cbcSMatt Macy  * zil_commit(); such that zil_commit() would return as soon as that
2955eda14cbcSMatt Macy  * specific itx was committed to disk (instead of waiting for _all_
2956eda14cbcSMatt Macy  * itxs to be committed).
2957eda14cbcSMatt Macy  *
2958eda14cbcSMatt Macy  * When a thread calls zil_commit() a special "commit itx" will be
2959eda14cbcSMatt Macy  * generated, along with a corresponding "waiter" for this commit itx.
2960eda14cbcSMatt Macy  * zil_commit() will wait on this waiter's CV, such that when the waiter
2961eda14cbcSMatt Macy  * is marked done, and signaled, zil_commit() will return.
2962eda14cbcSMatt Macy  *
2963eda14cbcSMatt Macy  * This commit itx is inserted into the queue of uncommitted itxs. This
2964eda14cbcSMatt Macy  * provides an easy mechanism for determining which itxs were in the
2965eda14cbcSMatt Macy  * queue prior to zil_commit() having been called, and which itxs were
2966eda14cbcSMatt Macy  * added after zil_commit() was called.
2967eda14cbcSMatt Macy  *
2968e3aa18adSMartin Matuska  * The commit itx is special; it doesn't have any on-disk representation.
2969eda14cbcSMatt Macy  * When a commit itx is "committed" to an lwb, the waiter associated
2970eda14cbcSMatt Macy  * with it is linked onto the lwb's list of waiters. Then, when that lwb
2971eda14cbcSMatt Macy  * completes, each waiter on the lwb's list is marked done and signaled
2972eda14cbcSMatt Macy  * -- allowing the thread waiting on the waiter to return from zil_commit().
2973eda14cbcSMatt Macy  *
2974eda14cbcSMatt Macy  * It's important to point out a few critical factors that allow us
2975eda14cbcSMatt Macy  * to make use of the commit itxs, commit waiters, per-lwb lists of
2976eda14cbcSMatt Macy  * commit waiters, and zio completion callbacks like we're doing:
2977eda14cbcSMatt Macy  *
2978eda14cbcSMatt Macy  *   1. The list of waiters for each lwb is traversed, and each commit
2979eda14cbcSMatt Macy  *      waiter is marked "done" and signaled, in the zio completion
2980eda14cbcSMatt Macy  *      callback of the lwb's zio[*].
2981eda14cbcSMatt Macy  *
2982eda14cbcSMatt Macy  *      * Actually, the waiters are signaled in the zio completion
2983eda14cbcSMatt Macy  *        callback of the root zio for the DKIOCFLUSHWRITECACHE commands
2984eda14cbcSMatt Macy  *        that are sent to the vdevs upon completion of the lwb zio.
2985eda14cbcSMatt Macy  *
2986eda14cbcSMatt Macy  *   2. When the itxs are inserted into the ZIL's queue of uncommitted
2987eda14cbcSMatt Macy  *      itxs, the order in which they are inserted is preserved[*]; as
2988eda14cbcSMatt Macy  *      itxs are added to the queue, they are added to the tail of
2989eda14cbcSMatt Macy  *      in-memory linked lists.
2990eda14cbcSMatt Macy  *
2991eda14cbcSMatt Macy  *      When committing the itxs to lwbs (to be written to disk), they
2992eda14cbcSMatt Macy  *      are committed in the same order in which the itxs were added to
2993eda14cbcSMatt Macy  *      the uncommitted queue's linked list(s); i.e. the linked list of
2994eda14cbcSMatt Macy  *      itxs to commit is traversed from head to tail, and each itx is
2995eda14cbcSMatt Macy  *      committed to an lwb in that order.
2996eda14cbcSMatt Macy  *
2997eda14cbcSMatt Macy  *      * To clarify:
2998eda14cbcSMatt Macy  *
2999eda14cbcSMatt Macy  *        - the order of "sync" itxs is preserved w.r.t. other
3000eda14cbcSMatt Macy  *          "sync" itxs, regardless of the corresponding objects.
3001eda14cbcSMatt Macy  *        - the order of "async" itxs is preserved w.r.t. other
3002eda14cbcSMatt Macy  *          "async" itxs corresponding to the same object.
3003eda14cbcSMatt Macy  *        - the order of "async" itxs is *not* preserved w.r.t. other
3004eda14cbcSMatt Macy  *          "async" itxs corresponding to different objects.
3005eda14cbcSMatt Macy  *        - the order of "sync" itxs w.r.t. "async" itxs (or vice
3006eda14cbcSMatt Macy  *          versa) is *not* preserved, even for itxs that correspond
3007eda14cbcSMatt Macy  *          to the same object.
3008eda14cbcSMatt Macy  *
3009eda14cbcSMatt Macy  *      For more details, see: zil_itx_assign(), zil_async_to_sync(),
3010eda14cbcSMatt Macy  *      zil_get_commit_list(), and zil_process_commit_list().
3011eda14cbcSMatt Macy  *
3012eda14cbcSMatt Macy  *   3. The lwbs represent a linked list of blocks on disk. Thus, any
3013eda14cbcSMatt Macy  *      lwb cannot be considered committed to stable storage, until its
3014eda14cbcSMatt Macy  *      "previous" lwb is also committed to stable storage. This fact,
3015eda14cbcSMatt Macy  *      coupled with the fact described above, means that itxs are
3016eda14cbcSMatt Macy  *      committed in (roughly) the order in which they were generated.
3017eda14cbcSMatt Macy  *      This is essential because itxs are dependent on prior itxs.
3018eda14cbcSMatt Macy  *      Thus, we *must not* deem an itx as being committed to stable
3019eda14cbcSMatt Macy  *      storage, until *all* prior itxs have also been committed to
3020eda14cbcSMatt Macy  *      stable storage.
3021eda14cbcSMatt Macy  *
3022eda14cbcSMatt Macy  *      To enforce this ordering of lwb zio's, while still leveraging as
3023eda14cbcSMatt Macy  *      much of the underlying storage performance as possible, we rely
3024eda14cbcSMatt Macy  *      on two fundamental concepts:
3025eda14cbcSMatt Macy  *
3026eda14cbcSMatt Macy  *          1. The creation and issuance of lwb zio's is protected by
3027eda14cbcSMatt Macy  *             the zilog's "zl_issuer_lock", which ensures only a single
3028eda14cbcSMatt Macy  *             thread is creating and/or issuing lwb's at a time
3029eda14cbcSMatt Macy  *          2. The "previous" lwb is a child of the "current" lwb
3030eda14cbcSMatt Macy  *             (leveraging the zio parent-child dependency graph)
3031eda14cbcSMatt Macy  *
3032eda14cbcSMatt Macy  *      By relying on this parent-child zio relationship, we can have
3033eda14cbcSMatt Macy  *      many lwb zio's concurrently issued to the underlying storage,
3034eda14cbcSMatt Macy  *      but the order in which they complete will be the same order in
3035eda14cbcSMatt Macy  *      which they were created.
3036eda14cbcSMatt Macy  */
3037eda14cbcSMatt Macy void
3038eda14cbcSMatt Macy zil_commit(zilog_t *zilog, uint64_t foid)
3039eda14cbcSMatt Macy {
3040eda14cbcSMatt Macy 	/*
3041eda14cbcSMatt Macy 	 * We should never attempt to call zil_commit on a snapshot for
3042eda14cbcSMatt Macy 	 * a couple of reasons:
3043eda14cbcSMatt Macy 	 *
3044eda14cbcSMatt Macy 	 * 1. A snapshot may never be modified, thus it cannot have any
3045eda14cbcSMatt Macy 	 *    in-flight itxs that would have modified the dataset.
3046eda14cbcSMatt Macy 	 *
3047eda14cbcSMatt Macy 	 * 2. By design, when zil_commit() is called, a commit itx will
3048eda14cbcSMatt Macy 	 *    be assigned to this zilog; as a result, the zilog will be
3049eda14cbcSMatt Macy 	 *    dirtied. We must not dirty the zilog of a snapshot; there's
3050eda14cbcSMatt Macy 	 *    checks in the code that enforce this invariant, and will
3051eda14cbcSMatt Macy 	 *    cause a panic if it's not upheld.
3052eda14cbcSMatt Macy 	 */
3053eda14cbcSMatt Macy 	ASSERT3B(dmu_objset_is_snapshot(zilog->zl_os), ==, B_FALSE);
3054eda14cbcSMatt Macy 
3055eda14cbcSMatt Macy 	if (zilog->zl_sync == ZFS_SYNC_DISABLED)
3056eda14cbcSMatt Macy 		return;
3057eda14cbcSMatt Macy 
3058eda14cbcSMatt Macy 	if (!spa_writeable(zilog->zl_spa)) {
3059eda14cbcSMatt Macy 		/*
3060eda14cbcSMatt Macy 		 * If the SPA is not writable, there should never be any
3061eda14cbcSMatt Macy 		 * pending itxs waiting to be committed to disk. If that
3062eda14cbcSMatt Macy 		 * weren't true, we'd skip writing those itxs out, and
3063eda14cbcSMatt Macy 		 * would break the semantics of zil_commit(); thus, we're
3064eda14cbcSMatt Macy 		 * verifying that truth before we return to the caller.
3065eda14cbcSMatt Macy 		 */
3066eda14cbcSMatt Macy 		ASSERT(list_is_empty(&zilog->zl_lwb_list));
3067eda14cbcSMatt Macy 		ASSERT3P(zilog->zl_last_lwb_opened, ==, NULL);
3068eda14cbcSMatt Macy 		for (int i = 0; i < TXG_SIZE; i++)
3069eda14cbcSMatt Macy 			ASSERT3P(zilog->zl_itxg[i].itxg_itxs, ==, NULL);
3070eda14cbcSMatt Macy 		return;
3071eda14cbcSMatt Macy 	}
3072eda14cbcSMatt Macy 
3073eda14cbcSMatt Macy 	/*
3074eda14cbcSMatt Macy 	 * If the ZIL is suspended, we don't want to dirty it by calling
3075eda14cbcSMatt Macy 	 * zil_commit_itx_assign() below, nor can we write out
3076eda14cbcSMatt Macy 	 * lwbs like would be done in zil_commit_write(). Thus, we
3077eda14cbcSMatt Macy 	 * simply rely on txg_wait_synced() to maintain the necessary
3078eda14cbcSMatt Macy 	 * semantics, and avoid calling those functions altogether.
3079eda14cbcSMatt Macy 	 */
3080eda14cbcSMatt Macy 	if (zilog->zl_suspend > 0) {
3081eda14cbcSMatt Macy 		txg_wait_synced(zilog->zl_dmu_pool, 0);
3082eda14cbcSMatt Macy 		return;
3083eda14cbcSMatt Macy 	}
3084eda14cbcSMatt Macy 
3085eda14cbcSMatt Macy 	zil_commit_impl(zilog, foid);
3086eda14cbcSMatt Macy }
3087eda14cbcSMatt Macy 
3088eda14cbcSMatt Macy void
3089eda14cbcSMatt Macy zil_commit_impl(zilog_t *zilog, uint64_t foid)
3090eda14cbcSMatt Macy {
3091eda14cbcSMatt Macy 	ZIL_STAT_BUMP(zil_commit_count);
3092eda14cbcSMatt Macy 
3093eda14cbcSMatt Macy 	/*
3094eda14cbcSMatt Macy 	 * Move the "async" itxs for the specified foid to the "sync"
3095eda14cbcSMatt Macy 	 * queues, such that they will be later committed (or skipped)
3096eda14cbcSMatt Macy 	 * to an lwb when zil_process_commit_list() is called.
3097eda14cbcSMatt Macy 	 *
3098eda14cbcSMatt Macy 	 * Since these "async" itxs must be committed prior to this
3099eda14cbcSMatt Macy 	 * call to zil_commit returning, we must perform this operation
3100eda14cbcSMatt Macy 	 * before we call zil_commit_itx_assign().
3101eda14cbcSMatt Macy 	 */
3102eda14cbcSMatt Macy 	zil_async_to_sync(zilog, foid);
3103eda14cbcSMatt Macy 
3104eda14cbcSMatt Macy 	/*
3105eda14cbcSMatt Macy 	 * We allocate a new "waiter" structure which will initially be
3106eda14cbcSMatt Macy 	 * linked to the commit itx using the itx's "itx_private" field.
3107eda14cbcSMatt Macy 	 * Since the commit itx doesn't represent any on-disk state,
3108eda14cbcSMatt Macy 	 * when it's committed to an lwb, rather than copying the its
3109eda14cbcSMatt Macy 	 * lr_t into the lwb's buffer, the commit itx's "waiter" will be
3110eda14cbcSMatt Macy 	 * added to the lwb's list of waiters. Then, when the lwb is
3111eda14cbcSMatt Macy 	 * committed to stable storage, each waiter in the lwb's list of
3112eda14cbcSMatt Macy 	 * waiters will be marked "done", and signalled.
3113eda14cbcSMatt Macy 	 *
3114eda14cbcSMatt Macy 	 * We must create the waiter and assign the commit itx prior to
3115eda14cbcSMatt Macy 	 * calling zil_commit_writer(), or else our specific commit itx
3116eda14cbcSMatt Macy 	 * is not guaranteed to be committed to an lwb prior to calling
3117eda14cbcSMatt Macy 	 * zil_commit_waiter().
3118eda14cbcSMatt Macy 	 */
3119eda14cbcSMatt Macy 	zil_commit_waiter_t *zcw = zil_alloc_commit_waiter();
3120eda14cbcSMatt Macy 	zil_commit_itx_assign(zilog, zcw);
3121eda14cbcSMatt Macy 
3122eda14cbcSMatt Macy 	zil_commit_writer(zilog, zcw);
3123eda14cbcSMatt Macy 	zil_commit_waiter(zilog, zcw);
3124eda14cbcSMatt Macy 
3125eda14cbcSMatt Macy 	if (zcw->zcw_zio_error != 0) {
3126eda14cbcSMatt Macy 		/*
3127eda14cbcSMatt Macy 		 * If there was an error writing out the ZIL blocks that
3128eda14cbcSMatt Macy 		 * this thread is waiting on, then we fallback to
3129eda14cbcSMatt Macy 		 * relying on spa_sync() to write out the data this
3130eda14cbcSMatt Macy 		 * thread is waiting on. Obviously this has performance
3131eda14cbcSMatt Macy 		 * implications, but the expectation is for this to be
3132eda14cbcSMatt Macy 		 * an exceptional case, and shouldn't occur often.
3133eda14cbcSMatt Macy 		 */
3134eda14cbcSMatt Macy 		DTRACE_PROBE2(zil__commit__io__error,
3135eda14cbcSMatt Macy 		    zilog_t *, zilog, zil_commit_waiter_t *, zcw);
3136eda14cbcSMatt Macy 		txg_wait_synced(zilog->zl_dmu_pool, 0);
3137eda14cbcSMatt Macy 	}
3138eda14cbcSMatt Macy 
3139eda14cbcSMatt Macy 	zil_free_commit_waiter(zcw);
3140eda14cbcSMatt Macy }
3141eda14cbcSMatt Macy 
3142eda14cbcSMatt Macy /*
3143eda14cbcSMatt Macy  * Called in syncing context to free committed log blocks and update log header.
3144eda14cbcSMatt Macy  */
3145eda14cbcSMatt Macy void
3146eda14cbcSMatt Macy zil_sync(zilog_t *zilog, dmu_tx_t *tx)
3147eda14cbcSMatt Macy {
3148eda14cbcSMatt Macy 	zil_header_t *zh = zil_header_in_syncing_context(zilog);
3149eda14cbcSMatt Macy 	uint64_t txg = dmu_tx_get_txg(tx);
3150eda14cbcSMatt Macy 	spa_t *spa = zilog->zl_spa;
3151eda14cbcSMatt Macy 	uint64_t *replayed_seq = &zilog->zl_replayed_seq[txg & TXG_MASK];
3152eda14cbcSMatt Macy 	lwb_t *lwb;
3153eda14cbcSMatt Macy 
3154eda14cbcSMatt Macy 	/*
3155eda14cbcSMatt Macy 	 * We don't zero out zl_destroy_txg, so make sure we don't try
3156eda14cbcSMatt Macy 	 * to destroy it twice.
3157eda14cbcSMatt Macy 	 */
3158eda14cbcSMatt Macy 	if (spa_sync_pass(spa) != 1)
3159eda14cbcSMatt Macy 		return;
3160eda14cbcSMatt Macy 
3161e3aa18adSMartin Matuska 	zil_lwb_flush_wait_all(zilog, txg);
3162e3aa18adSMartin Matuska 
3163eda14cbcSMatt Macy 	mutex_enter(&zilog->zl_lock);
3164eda14cbcSMatt Macy 
3165eda14cbcSMatt Macy 	ASSERT(zilog->zl_stop_sync == 0);
3166eda14cbcSMatt Macy 
3167eda14cbcSMatt Macy 	if (*replayed_seq != 0) {
3168eda14cbcSMatt Macy 		ASSERT(zh->zh_replay_seq < *replayed_seq);
3169eda14cbcSMatt Macy 		zh->zh_replay_seq = *replayed_seq;
3170eda14cbcSMatt Macy 		*replayed_seq = 0;
3171eda14cbcSMatt Macy 	}
3172eda14cbcSMatt Macy 
3173eda14cbcSMatt Macy 	if (zilog->zl_destroy_txg == txg) {
3174eda14cbcSMatt Macy 		blkptr_t blk = zh->zh_log;
3175c03c5b1cSMartin Matuska 		dsl_dataset_t *ds = dmu_objset_ds(zilog->zl_os);
3176eda14cbcSMatt Macy 
3177eda14cbcSMatt Macy 		ASSERT(list_head(&zilog->zl_lwb_list) == NULL);
3178eda14cbcSMatt Macy 
3179da5137abSMartin Matuska 		memset(zh, 0, sizeof (zil_header_t));
3180da5137abSMartin Matuska 		memset(zilog->zl_replayed_seq, 0,
3181da5137abSMartin Matuska 		    sizeof (zilog->zl_replayed_seq));
3182eda14cbcSMatt Macy 
3183eda14cbcSMatt Macy 		if (zilog->zl_keep_first) {
3184eda14cbcSMatt Macy 			/*
3185eda14cbcSMatt Macy 			 * If this block was part of log chain that couldn't
3186eda14cbcSMatt Macy 			 * be claimed because a device was missing during
3187eda14cbcSMatt Macy 			 * zil_claim(), but that device later returns,
3188eda14cbcSMatt Macy 			 * then this block could erroneously appear valid.
3189eda14cbcSMatt Macy 			 * To guard against this, assign a new GUID to the new
3190eda14cbcSMatt Macy 			 * log chain so it doesn't matter what blk points to.
3191eda14cbcSMatt Macy 			 */
3192eda14cbcSMatt Macy 			zil_init_log_chain(zilog, &blk);
3193eda14cbcSMatt Macy 			zh->zh_log = blk;
3194c03c5b1cSMartin Matuska 		} else {
3195c03c5b1cSMartin Matuska 			/*
3196c03c5b1cSMartin Matuska 			 * A destroyed ZIL chain can't contain any TX_SETSAXATTR
3197c03c5b1cSMartin Matuska 			 * records. So, deactivate the feature for this dataset.
3198c03c5b1cSMartin Matuska 			 * We activate it again when we start a new ZIL chain.
3199c03c5b1cSMartin Matuska 			 */
3200c03c5b1cSMartin Matuska 			if (dsl_dataset_feature_is_active(ds,
3201c03c5b1cSMartin Matuska 			    SPA_FEATURE_ZILSAXATTR))
3202c03c5b1cSMartin Matuska 				dsl_dataset_deactivate_feature(ds,
3203c03c5b1cSMartin Matuska 				    SPA_FEATURE_ZILSAXATTR, tx);
3204eda14cbcSMatt Macy 		}
3205eda14cbcSMatt Macy 	}
3206eda14cbcSMatt Macy 
3207eda14cbcSMatt Macy 	while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
3208eda14cbcSMatt Macy 		zh->zh_log = lwb->lwb_blk;
3209eda14cbcSMatt Macy 		if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg)
3210eda14cbcSMatt Macy 			break;
3211eda14cbcSMatt Macy 		list_remove(&zilog->zl_lwb_list, lwb);
3212eda14cbcSMatt Macy 		zio_free(spa, txg, &lwb->lwb_blk);
3213eda14cbcSMatt Macy 		zil_free_lwb(zilog, lwb);
3214eda14cbcSMatt Macy 
3215eda14cbcSMatt Macy 		/*
3216eda14cbcSMatt Macy 		 * If we don't have anything left in the lwb list then
3217eda14cbcSMatt Macy 		 * we've had an allocation failure and we need to zero
3218eda14cbcSMatt Macy 		 * out the zil_header blkptr so that we don't end
3219eda14cbcSMatt Macy 		 * up freeing the same block twice.
3220eda14cbcSMatt Macy 		 */
3221eda14cbcSMatt Macy 		if (list_head(&zilog->zl_lwb_list) == NULL)
3222eda14cbcSMatt Macy 			BP_ZERO(&zh->zh_log);
3223eda14cbcSMatt Macy 	}
3224eda14cbcSMatt Macy 
3225eda14cbcSMatt Macy 	/*
3226eda14cbcSMatt Macy 	 * Remove fastwrite on any blocks that have been pre-allocated for
3227eda14cbcSMatt Macy 	 * the next commit. This prevents fastwrite counter pollution by
3228eda14cbcSMatt Macy 	 * unused, long-lived LWBs.
3229eda14cbcSMatt Macy 	 */
3230eda14cbcSMatt Macy 	for (; lwb != NULL; lwb = list_next(&zilog->zl_lwb_list, lwb)) {
3231eda14cbcSMatt Macy 		if (lwb->lwb_fastwrite && !lwb->lwb_write_zio) {
3232eda14cbcSMatt Macy 			metaslab_fastwrite_unmark(zilog->zl_spa, &lwb->lwb_blk);
3233eda14cbcSMatt Macy 			lwb->lwb_fastwrite = 0;
3234eda14cbcSMatt Macy 		}
3235eda14cbcSMatt Macy 	}
3236eda14cbcSMatt Macy 
3237eda14cbcSMatt Macy 	mutex_exit(&zilog->zl_lock);
3238eda14cbcSMatt Macy }
3239eda14cbcSMatt Macy 
3240eda14cbcSMatt Macy static int
3241eda14cbcSMatt Macy zil_lwb_cons(void *vbuf, void *unused, int kmflag)
3242eda14cbcSMatt Macy {
3243e92ffd9bSMartin Matuska 	(void) unused, (void) kmflag;
3244eda14cbcSMatt Macy 	lwb_t *lwb = vbuf;
3245eda14cbcSMatt Macy 	list_create(&lwb->lwb_itxs, sizeof (itx_t), offsetof(itx_t, itx_node));
3246eda14cbcSMatt Macy 	list_create(&lwb->lwb_waiters, sizeof (zil_commit_waiter_t),
3247eda14cbcSMatt Macy 	    offsetof(zil_commit_waiter_t, zcw_node));
3248eda14cbcSMatt Macy 	avl_create(&lwb->lwb_vdev_tree, zil_lwb_vdev_compare,
3249eda14cbcSMatt Macy 	    sizeof (zil_vdev_node_t), offsetof(zil_vdev_node_t, zv_node));
3250eda14cbcSMatt Macy 	mutex_init(&lwb->lwb_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
3251eda14cbcSMatt Macy 	return (0);
3252eda14cbcSMatt Macy }
3253eda14cbcSMatt Macy 
3254eda14cbcSMatt Macy static void
3255eda14cbcSMatt Macy zil_lwb_dest(void *vbuf, void *unused)
3256eda14cbcSMatt Macy {
3257e92ffd9bSMartin Matuska 	(void) unused;
3258eda14cbcSMatt Macy 	lwb_t *lwb = vbuf;
3259eda14cbcSMatt Macy 	mutex_destroy(&lwb->lwb_vdev_lock);
3260eda14cbcSMatt Macy 	avl_destroy(&lwb->lwb_vdev_tree);
3261eda14cbcSMatt Macy 	list_destroy(&lwb->lwb_waiters);
3262eda14cbcSMatt Macy 	list_destroy(&lwb->lwb_itxs);
3263eda14cbcSMatt Macy }
3264eda14cbcSMatt Macy 
3265eda14cbcSMatt Macy void
3266eda14cbcSMatt Macy zil_init(void)
3267eda14cbcSMatt Macy {
3268eda14cbcSMatt Macy 	zil_lwb_cache = kmem_cache_create("zil_lwb_cache",
3269eda14cbcSMatt Macy 	    sizeof (lwb_t), 0, zil_lwb_cons, zil_lwb_dest, NULL, NULL, NULL, 0);
3270eda14cbcSMatt Macy 
3271eda14cbcSMatt Macy 	zil_zcw_cache = kmem_cache_create("zil_zcw_cache",
3272eda14cbcSMatt Macy 	    sizeof (zil_commit_waiter_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
3273eda14cbcSMatt Macy 
3274eda14cbcSMatt Macy 	zil_ksp = kstat_create("zfs", 0, "zil", "misc",
3275eda14cbcSMatt Macy 	    KSTAT_TYPE_NAMED, sizeof (zil_stats) / sizeof (kstat_named_t),
3276eda14cbcSMatt Macy 	    KSTAT_FLAG_VIRTUAL);
3277eda14cbcSMatt Macy 
3278eda14cbcSMatt Macy 	if (zil_ksp != NULL) {
3279eda14cbcSMatt Macy 		zil_ksp->ks_data = &zil_stats;
3280eda14cbcSMatt Macy 		kstat_install(zil_ksp);
3281eda14cbcSMatt Macy 	}
3282eda14cbcSMatt Macy }
3283eda14cbcSMatt Macy 
3284eda14cbcSMatt Macy void
3285eda14cbcSMatt Macy zil_fini(void)
3286eda14cbcSMatt Macy {
3287eda14cbcSMatt Macy 	kmem_cache_destroy(zil_zcw_cache);
3288eda14cbcSMatt Macy 	kmem_cache_destroy(zil_lwb_cache);
3289eda14cbcSMatt Macy 
3290eda14cbcSMatt Macy 	if (zil_ksp != NULL) {
3291eda14cbcSMatt Macy 		kstat_delete(zil_ksp);
3292eda14cbcSMatt Macy 		zil_ksp = NULL;
3293eda14cbcSMatt Macy 	}
3294eda14cbcSMatt Macy }
3295eda14cbcSMatt Macy 
3296eda14cbcSMatt Macy void
3297eda14cbcSMatt Macy zil_set_sync(zilog_t *zilog, uint64_t sync)
3298eda14cbcSMatt Macy {
3299eda14cbcSMatt Macy 	zilog->zl_sync = sync;
3300eda14cbcSMatt Macy }
3301eda14cbcSMatt Macy 
3302eda14cbcSMatt Macy void
3303eda14cbcSMatt Macy zil_set_logbias(zilog_t *zilog, uint64_t logbias)
3304eda14cbcSMatt Macy {
3305eda14cbcSMatt Macy 	zilog->zl_logbias = logbias;
3306eda14cbcSMatt Macy }
3307eda14cbcSMatt Macy 
3308eda14cbcSMatt Macy zilog_t *
3309eda14cbcSMatt Macy zil_alloc(objset_t *os, zil_header_t *zh_phys)
3310eda14cbcSMatt Macy {
3311eda14cbcSMatt Macy 	zilog_t *zilog;
3312eda14cbcSMatt Macy 
3313eda14cbcSMatt Macy 	zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP);
3314eda14cbcSMatt Macy 
3315eda14cbcSMatt Macy 	zilog->zl_header = zh_phys;
3316eda14cbcSMatt Macy 	zilog->zl_os = os;
3317eda14cbcSMatt Macy 	zilog->zl_spa = dmu_objset_spa(os);
3318eda14cbcSMatt Macy 	zilog->zl_dmu_pool = dmu_objset_pool(os);
3319eda14cbcSMatt Macy 	zilog->zl_destroy_txg = TXG_INITIAL - 1;
3320eda14cbcSMatt Macy 	zilog->zl_logbias = dmu_objset_logbias(os);
3321eda14cbcSMatt Macy 	zilog->zl_sync = dmu_objset_syncprop(os);
3322eda14cbcSMatt Macy 	zilog->zl_dirty_max_txg = 0;
3323eda14cbcSMatt Macy 	zilog->zl_last_lwb_opened = NULL;
3324eda14cbcSMatt Macy 	zilog->zl_last_lwb_latency = 0;
3325eda14cbcSMatt Macy 	zilog->zl_max_block_size = zil_maxblocksize;
3326eda14cbcSMatt Macy 
3327eda14cbcSMatt Macy 	mutex_init(&zilog->zl_lock, NULL, MUTEX_DEFAULT, NULL);
3328eda14cbcSMatt Macy 	mutex_init(&zilog->zl_issuer_lock, NULL, MUTEX_DEFAULT, NULL);
3329e3aa18adSMartin Matuska 	mutex_init(&zilog->zl_lwb_io_lock, NULL, MUTEX_DEFAULT, NULL);
3330eda14cbcSMatt Macy 
3331eda14cbcSMatt Macy 	for (int i = 0; i < TXG_SIZE; i++) {
3332eda14cbcSMatt Macy 		mutex_init(&zilog->zl_itxg[i].itxg_lock, NULL,
3333eda14cbcSMatt Macy 		    MUTEX_DEFAULT, NULL);
3334eda14cbcSMatt Macy 	}
3335eda14cbcSMatt Macy 
3336eda14cbcSMatt Macy 	list_create(&zilog->zl_lwb_list, sizeof (lwb_t),
3337eda14cbcSMatt Macy 	    offsetof(lwb_t, lwb_node));
3338eda14cbcSMatt Macy 
3339eda14cbcSMatt Macy 	list_create(&zilog->zl_itx_commit_list, sizeof (itx_t),
3340eda14cbcSMatt Macy 	    offsetof(itx_t, itx_node));
3341eda14cbcSMatt Macy 
3342eda14cbcSMatt Macy 	cv_init(&zilog->zl_cv_suspend, NULL, CV_DEFAULT, NULL);
3343e3aa18adSMartin Matuska 	cv_init(&zilog->zl_lwb_io_cv, NULL, CV_DEFAULT, NULL);
3344eda14cbcSMatt Macy 
3345eda14cbcSMatt Macy 	return (zilog);
3346eda14cbcSMatt Macy }
3347eda14cbcSMatt Macy 
3348eda14cbcSMatt Macy void
3349eda14cbcSMatt Macy zil_free(zilog_t *zilog)
3350eda14cbcSMatt Macy {
3351eda14cbcSMatt Macy 	int i;
3352eda14cbcSMatt Macy 
3353eda14cbcSMatt Macy 	zilog->zl_stop_sync = 1;
3354eda14cbcSMatt Macy 
3355eda14cbcSMatt Macy 	ASSERT0(zilog->zl_suspend);
3356eda14cbcSMatt Macy 	ASSERT0(zilog->zl_suspending);
3357eda14cbcSMatt Macy 
3358eda14cbcSMatt Macy 	ASSERT(list_is_empty(&zilog->zl_lwb_list));
3359eda14cbcSMatt Macy 	list_destroy(&zilog->zl_lwb_list);
3360eda14cbcSMatt Macy 
3361eda14cbcSMatt Macy 	ASSERT(list_is_empty(&zilog->zl_itx_commit_list));
3362eda14cbcSMatt Macy 	list_destroy(&zilog->zl_itx_commit_list);
3363eda14cbcSMatt Macy 
3364eda14cbcSMatt Macy 	for (i = 0; i < TXG_SIZE; i++) {
3365eda14cbcSMatt Macy 		/*
3366eda14cbcSMatt Macy 		 * It's possible for an itx to be generated that doesn't dirty
3367eda14cbcSMatt Macy 		 * a txg (e.g. ztest TX_TRUNCATE). So there's no zil_clean()
3368eda14cbcSMatt Macy 		 * callback to remove the entry. We remove those here.
3369eda14cbcSMatt Macy 		 *
3370eda14cbcSMatt Macy 		 * Also free up the ziltest itxs.
3371eda14cbcSMatt Macy 		 */
3372eda14cbcSMatt Macy 		if (zilog->zl_itxg[i].itxg_itxs)
3373eda14cbcSMatt Macy 			zil_itxg_clean(zilog->zl_itxg[i].itxg_itxs);
3374eda14cbcSMatt Macy 		mutex_destroy(&zilog->zl_itxg[i].itxg_lock);
3375eda14cbcSMatt Macy 	}
3376eda14cbcSMatt Macy 
3377eda14cbcSMatt Macy 	mutex_destroy(&zilog->zl_issuer_lock);
3378eda14cbcSMatt Macy 	mutex_destroy(&zilog->zl_lock);
3379e3aa18adSMartin Matuska 	mutex_destroy(&zilog->zl_lwb_io_lock);
3380eda14cbcSMatt Macy 
3381eda14cbcSMatt Macy 	cv_destroy(&zilog->zl_cv_suspend);
3382e3aa18adSMartin Matuska 	cv_destroy(&zilog->zl_lwb_io_cv);
3383eda14cbcSMatt Macy 
3384eda14cbcSMatt Macy 	kmem_free(zilog, sizeof (zilog_t));
3385eda14cbcSMatt Macy }
3386eda14cbcSMatt Macy 
3387eda14cbcSMatt Macy /*
3388eda14cbcSMatt Macy  * Open an intent log.
3389eda14cbcSMatt Macy  */
3390eda14cbcSMatt Macy zilog_t *
3391eda14cbcSMatt Macy zil_open(objset_t *os, zil_get_data_t *get_data)
3392eda14cbcSMatt Macy {
3393eda14cbcSMatt Macy 	zilog_t *zilog = dmu_objset_zil(os);
3394eda14cbcSMatt Macy 
3395eda14cbcSMatt Macy 	ASSERT3P(zilog->zl_get_data, ==, NULL);
3396eda14cbcSMatt Macy 	ASSERT3P(zilog->zl_last_lwb_opened, ==, NULL);
3397eda14cbcSMatt Macy 	ASSERT(list_is_empty(&zilog->zl_lwb_list));
3398eda14cbcSMatt Macy 
3399eda14cbcSMatt Macy 	zilog->zl_get_data = get_data;
3400eda14cbcSMatt Macy 
3401eda14cbcSMatt Macy 	return (zilog);
3402eda14cbcSMatt Macy }
3403eda14cbcSMatt Macy 
3404eda14cbcSMatt Macy /*
3405eda14cbcSMatt Macy  * Close an intent log.
3406eda14cbcSMatt Macy  */
3407eda14cbcSMatt Macy void
3408eda14cbcSMatt Macy zil_close(zilog_t *zilog)
3409eda14cbcSMatt Macy {
3410eda14cbcSMatt Macy 	lwb_t *lwb;
3411eda14cbcSMatt Macy 	uint64_t txg;
3412eda14cbcSMatt Macy 
3413eda14cbcSMatt Macy 	if (!dmu_objset_is_snapshot(zilog->zl_os)) {
3414eda14cbcSMatt Macy 		zil_commit(zilog, 0);
3415eda14cbcSMatt Macy 	} else {
3416eda14cbcSMatt Macy 		ASSERT3P(list_tail(&zilog->zl_lwb_list), ==, NULL);
3417eda14cbcSMatt Macy 		ASSERT0(zilog->zl_dirty_max_txg);
3418eda14cbcSMatt Macy 		ASSERT3B(zilog_is_dirty(zilog), ==, B_FALSE);
3419eda14cbcSMatt Macy 	}
3420eda14cbcSMatt Macy 
3421eda14cbcSMatt Macy 	mutex_enter(&zilog->zl_lock);
3422eda14cbcSMatt Macy 	lwb = list_tail(&zilog->zl_lwb_list);
3423eda14cbcSMatt Macy 	if (lwb == NULL)
3424eda14cbcSMatt Macy 		txg = zilog->zl_dirty_max_txg;
3425eda14cbcSMatt Macy 	else
3426eda14cbcSMatt Macy 		txg = MAX(zilog->zl_dirty_max_txg, lwb->lwb_max_txg);
3427eda14cbcSMatt Macy 	mutex_exit(&zilog->zl_lock);
3428eda14cbcSMatt Macy 
3429eda14cbcSMatt Macy 	/*
3430e3aa18adSMartin Matuska 	 * zl_lwb_max_issued_txg may be larger than lwb_max_txg. It depends
3431e3aa18adSMartin Matuska 	 * on the time when the dmu_tx transaction is assigned in
3432e3aa18adSMartin Matuska 	 * zil_lwb_write_issue().
3433e3aa18adSMartin Matuska 	 */
3434e3aa18adSMartin Matuska 	mutex_enter(&zilog->zl_lwb_io_lock);
3435e3aa18adSMartin Matuska 	txg = MAX(zilog->zl_lwb_max_issued_txg, txg);
3436e3aa18adSMartin Matuska 	mutex_exit(&zilog->zl_lwb_io_lock);
3437e3aa18adSMartin Matuska 
3438e3aa18adSMartin Matuska 	/*
3439e3aa18adSMartin Matuska 	 * We need to use txg_wait_synced() to wait until that txg is synced.
3440e3aa18adSMartin Matuska 	 * zil_sync() will guarantee all lwbs up to that txg have been
3441e3aa18adSMartin Matuska 	 * written out, flushed, and cleaned.
3442eda14cbcSMatt Macy 	 */
3443eda14cbcSMatt Macy 	if (txg != 0)
3444eda14cbcSMatt Macy 		txg_wait_synced(zilog->zl_dmu_pool, txg);
3445eda14cbcSMatt Macy 
3446eda14cbcSMatt Macy 	if (zilog_is_dirty(zilog))
344733b8c039SMartin Matuska 		zfs_dbgmsg("zil (%px) is dirty, txg %llu", zilog,
344833b8c039SMartin Matuska 		    (u_longlong_t)txg);
3449eda14cbcSMatt Macy 	if (txg < spa_freeze_txg(zilog->zl_spa))
3450eda14cbcSMatt Macy 		VERIFY(!zilog_is_dirty(zilog));
3451eda14cbcSMatt Macy 
3452eda14cbcSMatt Macy 	zilog->zl_get_data = NULL;
3453eda14cbcSMatt Macy 
3454eda14cbcSMatt Macy 	/*
3455eda14cbcSMatt Macy 	 * We should have only one lwb left on the list; remove it now.
3456eda14cbcSMatt Macy 	 */
3457eda14cbcSMatt Macy 	mutex_enter(&zilog->zl_lock);
3458eda14cbcSMatt Macy 	lwb = list_head(&zilog->zl_lwb_list);
3459eda14cbcSMatt Macy 	if (lwb != NULL) {
3460eda14cbcSMatt Macy 		ASSERT3P(lwb, ==, list_tail(&zilog->zl_lwb_list));
3461eda14cbcSMatt Macy 		ASSERT3S(lwb->lwb_state, !=, LWB_STATE_ISSUED);
3462eda14cbcSMatt Macy 
3463eda14cbcSMatt Macy 		if (lwb->lwb_fastwrite)
3464eda14cbcSMatt Macy 			metaslab_fastwrite_unmark(zilog->zl_spa, &lwb->lwb_blk);
3465eda14cbcSMatt Macy 
3466eda14cbcSMatt Macy 		list_remove(&zilog->zl_lwb_list, lwb);
3467eda14cbcSMatt Macy 		zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
3468eda14cbcSMatt Macy 		zil_free_lwb(zilog, lwb);
3469eda14cbcSMatt Macy 	}
3470eda14cbcSMatt Macy 	mutex_exit(&zilog->zl_lock);
3471eda14cbcSMatt Macy }
3472eda14cbcSMatt Macy 
3473*a0b956f5SMartin Matuska static const char *suspend_tag = "zil suspending";
3474eda14cbcSMatt Macy 
3475eda14cbcSMatt Macy /*
3476eda14cbcSMatt Macy  * Suspend an intent log.  While in suspended mode, we still honor
3477eda14cbcSMatt Macy  * synchronous semantics, but we rely on txg_wait_synced() to do it.
3478eda14cbcSMatt Macy  * On old version pools, we suspend the log briefly when taking a
3479eda14cbcSMatt Macy  * snapshot so that it will have an empty intent log.
3480eda14cbcSMatt Macy  *
3481eda14cbcSMatt Macy  * Long holds are not really intended to be used the way we do here --
3482eda14cbcSMatt Macy  * held for such a short time.  A concurrent caller of dsl_dataset_long_held()
3483eda14cbcSMatt Macy  * could fail.  Therefore we take pains to only put a long hold if it is
3484eda14cbcSMatt Macy  * actually necessary.  Fortunately, it will only be necessary if the
3485eda14cbcSMatt Macy  * objset is currently mounted (or the ZVOL equivalent).  In that case it
3486eda14cbcSMatt Macy  * will already have a long hold, so we are not really making things any worse.
3487eda14cbcSMatt Macy  *
3488eda14cbcSMatt Macy  * Ideally, we would locate the existing long-holder (i.e. the zfsvfs_t or
3489eda14cbcSMatt Macy  * zvol_state_t), and use their mechanism to prevent their hold from being
3490eda14cbcSMatt Macy  * dropped (e.g. VFS_HOLD()).  However, that would be even more pain for
3491eda14cbcSMatt Macy  * very little gain.
3492eda14cbcSMatt Macy  *
3493eda14cbcSMatt Macy  * if cookiep == NULL, this does both the suspend & resume.
3494eda14cbcSMatt Macy  * Otherwise, it returns with the dataset "long held", and the cookie
3495eda14cbcSMatt Macy  * should be passed into zil_resume().
3496eda14cbcSMatt Macy  */
3497eda14cbcSMatt Macy int
3498eda14cbcSMatt Macy zil_suspend(const char *osname, void **cookiep)
3499eda14cbcSMatt Macy {
3500eda14cbcSMatt Macy 	objset_t *os;
3501eda14cbcSMatt Macy 	zilog_t *zilog;
3502eda14cbcSMatt Macy 	const zil_header_t *zh;
3503eda14cbcSMatt Macy 	int error;
3504eda14cbcSMatt Macy 
3505eda14cbcSMatt Macy 	error = dmu_objset_hold(osname, suspend_tag, &os);
3506eda14cbcSMatt Macy 	if (error != 0)
3507eda14cbcSMatt Macy 		return (error);
3508eda14cbcSMatt Macy 	zilog = dmu_objset_zil(os);
3509eda14cbcSMatt Macy 
3510eda14cbcSMatt Macy 	mutex_enter(&zilog->zl_lock);
3511eda14cbcSMatt Macy 	zh = zilog->zl_header;
3512eda14cbcSMatt Macy 
3513eda14cbcSMatt Macy 	if (zh->zh_flags & ZIL_REPLAY_NEEDED) {		/* unplayed log */
3514eda14cbcSMatt Macy 		mutex_exit(&zilog->zl_lock);
3515eda14cbcSMatt Macy 		dmu_objset_rele(os, suspend_tag);
3516eda14cbcSMatt Macy 		return (SET_ERROR(EBUSY));
3517eda14cbcSMatt Macy 	}
3518eda14cbcSMatt Macy 
3519eda14cbcSMatt Macy 	/*
3520eda14cbcSMatt Macy 	 * Don't put a long hold in the cases where we can avoid it.  This
3521eda14cbcSMatt Macy 	 * is when there is no cookie so we are doing a suspend & resume
3522eda14cbcSMatt Macy 	 * (i.e. called from zil_vdev_offline()), and there's nothing to do
3523eda14cbcSMatt Macy 	 * for the suspend because it's already suspended, or there's no ZIL.
3524eda14cbcSMatt Macy 	 */
3525eda14cbcSMatt Macy 	if (cookiep == NULL && !zilog->zl_suspending &&
3526eda14cbcSMatt Macy 	    (zilog->zl_suspend > 0 || BP_IS_HOLE(&zh->zh_log))) {
3527eda14cbcSMatt Macy 		mutex_exit(&zilog->zl_lock);
3528eda14cbcSMatt Macy 		dmu_objset_rele(os, suspend_tag);
3529eda14cbcSMatt Macy 		return (0);
3530eda14cbcSMatt Macy 	}
3531eda14cbcSMatt Macy 
3532eda14cbcSMatt Macy 	dsl_dataset_long_hold(dmu_objset_ds(os), suspend_tag);
3533eda14cbcSMatt Macy 	dsl_pool_rele(dmu_objset_pool(os), suspend_tag);
3534eda14cbcSMatt Macy 
3535eda14cbcSMatt Macy 	zilog->zl_suspend++;
3536eda14cbcSMatt Macy 
3537eda14cbcSMatt Macy 	if (zilog->zl_suspend > 1) {
3538eda14cbcSMatt Macy 		/*
3539eda14cbcSMatt Macy 		 * Someone else is already suspending it.
3540eda14cbcSMatt Macy 		 * Just wait for them to finish.
3541eda14cbcSMatt Macy 		 */
3542eda14cbcSMatt Macy 
3543eda14cbcSMatt Macy 		while (zilog->zl_suspending)
3544eda14cbcSMatt Macy 			cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock);
3545eda14cbcSMatt Macy 		mutex_exit(&zilog->zl_lock);
3546eda14cbcSMatt Macy 
3547eda14cbcSMatt Macy 		if (cookiep == NULL)
3548eda14cbcSMatt Macy 			zil_resume(os);
3549eda14cbcSMatt Macy 		else
3550eda14cbcSMatt Macy 			*cookiep = os;
3551eda14cbcSMatt Macy 		return (0);
3552eda14cbcSMatt Macy 	}
3553eda14cbcSMatt Macy 
3554eda14cbcSMatt Macy 	/*
3555eda14cbcSMatt Macy 	 * If there is no pointer to an on-disk block, this ZIL must not
3556eda14cbcSMatt Macy 	 * be active (e.g. filesystem not mounted), so there's nothing
3557eda14cbcSMatt Macy 	 * to clean up.
3558eda14cbcSMatt Macy 	 */
3559eda14cbcSMatt Macy 	if (BP_IS_HOLE(&zh->zh_log)) {
3560eda14cbcSMatt Macy 		ASSERT(cookiep != NULL); /* fast path already handled */
3561eda14cbcSMatt Macy 
3562eda14cbcSMatt Macy 		*cookiep = os;
3563eda14cbcSMatt Macy 		mutex_exit(&zilog->zl_lock);
3564eda14cbcSMatt Macy 		return (0);
3565eda14cbcSMatt Macy 	}
3566eda14cbcSMatt Macy 
3567eda14cbcSMatt Macy 	/*
3568eda14cbcSMatt Macy 	 * The ZIL has work to do. Ensure that the associated encryption
3569eda14cbcSMatt Macy 	 * key will remain mapped while we are committing the log by
3570eda14cbcSMatt Macy 	 * grabbing a reference to it. If the key isn't loaded we have no
3571eda14cbcSMatt Macy 	 * choice but to return an error until the wrapping key is loaded.
3572eda14cbcSMatt Macy 	 */
3573eda14cbcSMatt Macy 	if (os->os_encrypted &&
3574eda14cbcSMatt Macy 	    dsl_dataset_create_key_mapping(dmu_objset_ds(os)) != 0) {
3575eda14cbcSMatt Macy 		zilog->zl_suspend--;
3576eda14cbcSMatt Macy 		mutex_exit(&zilog->zl_lock);
3577eda14cbcSMatt Macy 		dsl_dataset_long_rele(dmu_objset_ds(os), suspend_tag);
3578eda14cbcSMatt Macy 		dsl_dataset_rele(dmu_objset_ds(os), suspend_tag);
3579eda14cbcSMatt Macy 		return (SET_ERROR(EACCES));
3580eda14cbcSMatt Macy 	}
3581eda14cbcSMatt Macy 
3582eda14cbcSMatt Macy 	zilog->zl_suspending = B_TRUE;
3583eda14cbcSMatt Macy 	mutex_exit(&zilog->zl_lock);
3584eda14cbcSMatt Macy 
3585eda14cbcSMatt Macy 	/*
3586eda14cbcSMatt Macy 	 * We need to use zil_commit_impl to ensure we wait for all
3587eda14cbcSMatt Macy 	 * LWB_STATE_OPENED and LWB_STATE_ISSUED lwbs to be committed
3588eda14cbcSMatt Macy 	 * to disk before proceeding. If we used zil_commit instead, it
3589eda14cbcSMatt Macy 	 * would just call txg_wait_synced(), because zl_suspend is set.
3590eda14cbcSMatt Macy 	 * txg_wait_synced() doesn't wait for these lwb's to be
3591eda14cbcSMatt Macy 	 * LWB_STATE_FLUSH_DONE before returning.
3592eda14cbcSMatt Macy 	 */
3593eda14cbcSMatt Macy 	zil_commit_impl(zilog, 0);
3594eda14cbcSMatt Macy 
3595eda14cbcSMatt Macy 	/*
3596eda14cbcSMatt Macy 	 * Now that we've ensured all lwb's are LWB_STATE_FLUSH_DONE, we
3597eda14cbcSMatt Macy 	 * use txg_wait_synced() to ensure the data from the zilog has
3598eda14cbcSMatt Macy 	 * migrated to the main pool before calling zil_destroy().
3599eda14cbcSMatt Macy 	 */
3600eda14cbcSMatt Macy 	txg_wait_synced(zilog->zl_dmu_pool, 0);
3601eda14cbcSMatt Macy 
3602eda14cbcSMatt Macy 	zil_destroy(zilog, B_FALSE);
3603eda14cbcSMatt Macy 
3604eda14cbcSMatt Macy 	mutex_enter(&zilog->zl_lock);
3605eda14cbcSMatt Macy 	zilog->zl_suspending = B_FALSE;
3606eda14cbcSMatt Macy 	cv_broadcast(&zilog->zl_cv_suspend);
3607eda14cbcSMatt Macy 	mutex_exit(&zilog->zl_lock);
3608eda14cbcSMatt Macy 
3609eda14cbcSMatt Macy 	if (os->os_encrypted)
3610eda14cbcSMatt Macy 		dsl_dataset_remove_key_mapping(dmu_objset_ds(os));
3611eda14cbcSMatt Macy 
3612eda14cbcSMatt Macy 	if (cookiep == NULL)
3613eda14cbcSMatt Macy 		zil_resume(os);
3614eda14cbcSMatt Macy 	else
3615eda14cbcSMatt Macy 		*cookiep = os;
3616eda14cbcSMatt Macy 	return (0);
3617eda14cbcSMatt Macy }
3618eda14cbcSMatt Macy 
3619eda14cbcSMatt Macy void
3620eda14cbcSMatt Macy zil_resume(void *cookie)
3621eda14cbcSMatt Macy {
3622eda14cbcSMatt Macy 	objset_t *os = cookie;
3623eda14cbcSMatt Macy 	zilog_t *zilog = dmu_objset_zil(os);
3624eda14cbcSMatt Macy 
3625eda14cbcSMatt Macy 	mutex_enter(&zilog->zl_lock);
3626eda14cbcSMatt Macy 	ASSERT(zilog->zl_suspend != 0);
3627eda14cbcSMatt Macy 	zilog->zl_suspend--;
3628eda14cbcSMatt Macy 	mutex_exit(&zilog->zl_lock);
3629eda14cbcSMatt Macy 	dsl_dataset_long_rele(dmu_objset_ds(os), suspend_tag);
3630eda14cbcSMatt Macy 	dsl_dataset_rele(dmu_objset_ds(os), suspend_tag);
3631eda14cbcSMatt Macy }
3632eda14cbcSMatt Macy 
3633eda14cbcSMatt Macy typedef struct zil_replay_arg {
3634e92ffd9bSMartin Matuska 	zil_replay_func_t *const *zr_replay;
3635eda14cbcSMatt Macy 	void		*zr_arg;
3636eda14cbcSMatt Macy 	boolean_t	zr_byteswap;
3637eda14cbcSMatt Macy 	char		*zr_lr;
3638eda14cbcSMatt Macy } zil_replay_arg_t;
3639eda14cbcSMatt Macy 
3640eda14cbcSMatt Macy static int
3641180f8225SMatt Macy zil_replay_error(zilog_t *zilog, const lr_t *lr, int error)
3642eda14cbcSMatt Macy {
3643eda14cbcSMatt Macy 	char name[ZFS_MAX_DATASET_NAME_LEN];
3644eda14cbcSMatt Macy 
3645eda14cbcSMatt Macy 	zilog->zl_replaying_seq--;	/* didn't actually replay this one */
3646eda14cbcSMatt Macy 
3647eda14cbcSMatt Macy 	dmu_objset_name(zilog->zl_os, name);
3648eda14cbcSMatt Macy 
3649eda14cbcSMatt Macy 	cmn_err(CE_WARN, "ZFS replay transaction error %d, "
3650eda14cbcSMatt Macy 	    "dataset %s, seq 0x%llx, txtype %llu %s\n", error, name,
3651eda14cbcSMatt Macy 	    (u_longlong_t)lr->lrc_seq,
3652eda14cbcSMatt Macy 	    (u_longlong_t)(lr->lrc_txtype & ~TX_CI),
3653eda14cbcSMatt Macy 	    (lr->lrc_txtype & TX_CI) ? "CI" : "");
3654eda14cbcSMatt Macy 
3655eda14cbcSMatt Macy 	return (error);
3656eda14cbcSMatt Macy }
3657eda14cbcSMatt Macy 
3658eda14cbcSMatt Macy static int
3659180f8225SMatt Macy zil_replay_log_record(zilog_t *zilog, const lr_t *lr, void *zra,
3660180f8225SMatt Macy     uint64_t claim_txg)
3661eda14cbcSMatt Macy {
3662eda14cbcSMatt Macy 	zil_replay_arg_t *zr = zra;
3663eda14cbcSMatt Macy 	const zil_header_t *zh = zilog->zl_header;
3664eda14cbcSMatt Macy 	uint64_t reclen = lr->lrc_reclen;
3665eda14cbcSMatt Macy 	uint64_t txtype = lr->lrc_txtype;
3666eda14cbcSMatt Macy 	int error = 0;
3667eda14cbcSMatt Macy 
3668eda14cbcSMatt Macy 	zilog->zl_replaying_seq = lr->lrc_seq;
3669eda14cbcSMatt Macy 
3670eda14cbcSMatt Macy 	if (lr->lrc_seq <= zh->zh_replay_seq)	/* already replayed */
3671eda14cbcSMatt Macy 		return (0);
3672eda14cbcSMatt Macy 
3673eda14cbcSMatt Macy 	if (lr->lrc_txg < claim_txg)		/* already committed */
3674eda14cbcSMatt Macy 		return (0);
3675eda14cbcSMatt Macy 
3676eda14cbcSMatt Macy 	/* Strip case-insensitive bit, still present in log record */
3677eda14cbcSMatt Macy 	txtype &= ~TX_CI;
3678eda14cbcSMatt Macy 
3679eda14cbcSMatt Macy 	if (txtype == 0 || txtype >= TX_MAX_TYPE)
3680eda14cbcSMatt Macy 		return (zil_replay_error(zilog, lr, EINVAL));
3681eda14cbcSMatt Macy 
3682eda14cbcSMatt Macy 	/*
3683eda14cbcSMatt Macy 	 * If this record type can be logged out of order, the object
3684eda14cbcSMatt Macy 	 * (lr_foid) may no longer exist.  That's legitimate, not an error.
3685eda14cbcSMatt Macy 	 */
3686eda14cbcSMatt Macy 	if (TX_OOO(txtype)) {
3687eda14cbcSMatt Macy 		error = dmu_object_info(zilog->zl_os,
3688eda14cbcSMatt Macy 		    LR_FOID_GET_OBJ(((lr_ooo_t *)lr)->lr_foid), NULL);
3689eda14cbcSMatt Macy 		if (error == ENOENT || error == EEXIST)
3690eda14cbcSMatt Macy 			return (0);
3691eda14cbcSMatt Macy 	}
3692eda14cbcSMatt Macy 
3693eda14cbcSMatt Macy 	/*
3694eda14cbcSMatt Macy 	 * Make a copy of the data so we can revise and extend it.
3695eda14cbcSMatt Macy 	 */
3696da5137abSMartin Matuska 	memcpy(zr->zr_lr, lr, reclen);
3697eda14cbcSMatt Macy 
3698eda14cbcSMatt Macy 	/*
3699eda14cbcSMatt Macy 	 * If this is a TX_WRITE with a blkptr, suck in the data.
3700eda14cbcSMatt Macy 	 */
3701eda14cbcSMatt Macy 	if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) {
3702eda14cbcSMatt Macy 		error = zil_read_log_data(zilog, (lr_write_t *)lr,
3703eda14cbcSMatt Macy 		    zr->zr_lr + reclen);
3704eda14cbcSMatt Macy 		if (error != 0)
3705eda14cbcSMatt Macy 			return (zil_replay_error(zilog, lr, error));
3706eda14cbcSMatt Macy 	}
3707eda14cbcSMatt Macy 
3708eda14cbcSMatt Macy 	/*
3709eda14cbcSMatt Macy 	 * The log block containing this lr may have been byteswapped
3710eda14cbcSMatt Macy 	 * so that we can easily examine common fields like lrc_txtype.
3711eda14cbcSMatt Macy 	 * However, the log is a mix of different record types, and only the
3712eda14cbcSMatt Macy 	 * replay vectors know how to byteswap their records.  Therefore, if
3713eda14cbcSMatt Macy 	 * the lr was byteswapped, undo it before invoking the replay vector.
3714eda14cbcSMatt Macy 	 */
3715eda14cbcSMatt Macy 	if (zr->zr_byteswap)
3716eda14cbcSMatt Macy 		byteswap_uint64_array(zr->zr_lr, reclen);
3717eda14cbcSMatt Macy 
3718eda14cbcSMatt Macy 	/*
3719eda14cbcSMatt Macy 	 * We must now do two things atomically: replay this log record,
3720eda14cbcSMatt Macy 	 * and update the log header sequence number to reflect the fact that
3721eda14cbcSMatt Macy 	 * we did so. At the end of each replay function the sequence number
3722eda14cbcSMatt Macy 	 * is updated if we are in replay mode.
3723eda14cbcSMatt Macy 	 */
3724eda14cbcSMatt Macy 	error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lr, zr->zr_byteswap);
3725eda14cbcSMatt Macy 	if (error != 0) {
3726eda14cbcSMatt Macy 		/*
3727eda14cbcSMatt Macy 		 * The DMU's dnode layer doesn't see removes until the txg
3728eda14cbcSMatt Macy 		 * commits, so a subsequent claim can spuriously fail with
3729eda14cbcSMatt Macy 		 * EEXIST. So if we receive any error we try syncing out
3730eda14cbcSMatt Macy 		 * any removes then retry the transaction.  Note that we
3731eda14cbcSMatt Macy 		 * specify B_FALSE for byteswap now, so we don't do it twice.
3732eda14cbcSMatt Macy 		 */
3733eda14cbcSMatt Macy 		txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0);
3734eda14cbcSMatt Macy 		error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lr, B_FALSE);
3735eda14cbcSMatt Macy 		if (error != 0)
3736eda14cbcSMatt Macy 			return (zil_replay_error(zilog, lr, error));
3737eda14cbcSMatt Macy 	}
3738eda14cbcSMatt Macy 	return (0);
3739eda14cbcSMatt Macy }
3740eda14cbcSMatt Macy 
3741eda14cbcSMatt Macy static int
3742180f8225SMatt Macy zil_incr_blks(zilog_t *zilog, const blkptr_t *bp, void *arg, uint64_t claim_txg)
3743eda14cbcSMatt Macy {
3744e92ffd9bSMartin Matuska 	(void) bp, (void) arg, (void) claim_txg;
3745e92ffd9bSMartin Matuska 
3746eda14cbcSMatt Macy 	zilog->zl_replay_blks++;
3747eda14cbcSMatt Macy 
3748eda14cbcSMatt Macy 	return (0);
3749eda14cbcSMatt Macy }
3750eda14cbcSMatt Macy 
3751eda14cbcSMatt Macy /*
3752eda14cbcSMatt Macy  * If this dataset has a non-empty intent log, replay it and destroy it.
3753eda14cbcSMatt Macy  */
3754eda14cbcSMatt Macy void
3755e92ffd9bSMartin Matuska zil_replay(objset_t *os, void *arg,
3756e92ffd9bSMartin Matuska     zil_replay_func_t *const replay_func[TX_MAX_TYPE])
3757eda14cbcSMatt Macy {
3758eda14cbcSMatt Macy 	zilog_t *zilog = dmu_objset_zil(os);
3759eda14cbcSMatt Macy 	const zil_header_t *zh = zilog->zl_header;
3760eda14cbcSMatt Macy 	zil_replay_arg_t zr;
3761eda14cbcSMatt Macy 
3762eda14cbcSMatt Macy 	if ((zh->zh_flags & ZIL_REPLAY_NEEDED) == 0) {
3763eda14cbcSMatt Macy 		zil_destroy(zilog, B_TRUE);
3764eda14cbcSMatt Macy 		return;
3765eda14cbcSMatt Macy 	}
3766eda14cbcSMatt Macy 
3767eda14cbcSMatt Macy 	zr.zr_replay = replay_func;
3768eda14cbcSMatt Macy 	zr.zr_arg = arg;
3769eda14cbcSMatt Macy 	zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log);
3770eda14cbcSMatt Macy 	zr.zr_lr = vmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP);
3771eda14cbcSMatt Macy 
3772eda14cbcSMatt Macy 	/*
3773eda14cbcSMatt Macy 	 * Wait for in-progress removes to sync before starting replay.
3774eda14cbcSMatt Macy 	 */
3775eda14cbcSMatt Macy 	txg_wait_synced(zilog->zl_dmu_pool, 0);
3776eda14cbcSMatt Macy 
3777eda14cbcSMatt Macy 	zilog->zl_replay = B_TRUE;
3778eda14cbcSMatt Macy 	zilog->zl_replay_time = ddi_get_lbolt();
3779eda14cbcSMatt Macy 	ASSERT(zilog->zl_replay_blks == 0);
3780eda14cbcSMatt Macy 	(void) zil_parse(zilog, zil_incr_blks, zil_replay_log_record, &zr,
3781eda14cbcSMatt Macy 	    zh->zh_claim_txg, B_TRUE);
3782eda14cbcSMatt Macy 	vmem_free(zr.zr_lr, 2 * SPA_MAXBLOCKSIZE);
3783eda14cbcSMatt Macy 
3784eda14cbcSMatt Macy 	zil_destroy(zilog, B_FALSE);
3785eda14cbcSMatt Macy 	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
3786eda14cbcSMatt Macy 	zilog->zl_replay = B_FALSE;
3787eda14cbcSMatt Macy }
3788eda14cbcSMatt Macy 
3789eda14cbcSMatt Macy boolean_t
3790eda14cbcSMatt Macy zil_replaying(zilog_t *zilog, dmu_tx_t *tx)
3791eda14cbcSMatt Macy {
3792eda14cbcSMatt Macy 	if (zilog->zl_sync == ZFS_SYNC_DISABLED)
3793eda14cbcSMatt Macy 		return (B_TRUE);
3794eda14cbcSMatt Macy 
3795eda14cbcSMatt Macy 	if (zilog->zl_replay) {
3796eda14cbcSMatt Macy 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
3797eda14cbcSMatt Macy 		zilog->zl_replayed_seq[dmu_tx_get_txg(tx) & TXG_MASK] =
3798eda14cbcSMatt Macy 		    zilog->zl_replaying_seq;
3799eda14cbcSMatt Macy 		return (B_TRUE);
3800eda14cbcSMatt Macy 	}
3801eda14cbcSMatt Macy 
3802eda14cbcSMatt Macy 	return (B_FALSE);
3803eda14cbcSMatt Macy }
3804eda14cbcSMatt Macy 
3805eda14cbcSMatt Macy int
3806eda14cbcSMatt Macy zil_reset(const char *osname, void *arg)
3807eda14cbcSMatt Macy {
3808e92ffd9bSMartin Matuska 	(void) arg;
3809eda14cbcSMatt Macy 
3810e92ffd9bSMartin Matuska 	int error = zil_suspend(osname, NULL);
3811eda14cbcSMatt Macy 	/* EACCES means crypto key not loaded */
3812eda14cbcSMatt Macy 	if ((error == EACCES) || (error == EBUSY))
3813eda14cbcSMatt Macy 		return (SET_ERROR(error));
3814eda14cbcSMatt Macy 	if (error != 0)
3815eda14cbcSMatt Macy 		return (SET_ERROR(EEXIST));
3816eda14cbcSMatt Macy 	return (0);
3817eda14cbcSMatt Macy }
3818eda14cbcSMatt Macy 
3819eda14cbcSMatt Macy EXPORT_SYMBOL(zil_alloc);
3820eda14cbcSMatt Macy EXPORT_SYMBOL(zil_free);
3821eda14cbcSMatt Macy EXPORT_SYMBOL(zil_open);
3822eda14cbcSMatt Macy EXPORT_SYMBOL(zil_close);
3823eda14cbcSMatt Macy EXPORT_SYMBOL(zil_replay);
3824eda14cbcSMatt Macy EXPORT_SYMBOL(zil_replaying);
3825eda14cbcSMatt Macy EXPORT_SYMBOL(zil_destroy);
3826eda14cbcSMatt Macy EXPORT_SYMBOL(zil_destroy_sync);
3827eda14cbcSMatt Macy EXPORT_SYMBOL(zil_itx_create);
3828eda14cbcSMatt Macy EXPORT_SYMBOL(zil_itx_destroy);
3829eda14cbcSMatt Macy EXPORT_SYMBOL(zil_itx_assign);
3830eda14cbcSMatt Macy EXPORT_SYMBOL(zil_commit);
3831eda14cbcSMatt Macy EXPORT_SYMBOL(zil_claim);
3832eda14cbcSMatt Macy EXPORT_SYMBOL(zil_check_log_chain);
3833eda14cbcSMatt Macy EXPORT_SYMBOL(zil_sync);
3834eda14cbcSMatt Macy EXPORT_SYMBOL(zil_clean);
3835eda14cbcSMatt Macy EXPORT_SYMBOL(zil_suspend);
3836eda14cbcSMatt Macy EXPORT_SYMBOL(zil_resume);
3837eda14cbcSMatt Macy EXPORT_SYMBOL(zil_lwb_add_block);
3838eda14cbcSMatt Macy EXPORT_SYMBOL(zil_bp_tree_add);
3839eda14cbcSMatt Macy EXPORT_SYMBOL(zil_set_sync);
3840eda14cbcSMatt Macy EXPORT_SYMBOL(zil_set_logbias);
3841eda14cbcSMatt Macy 
3842eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs, zfs_, commit_timeout_pct, INT, ZMOD_RW,
3843eda14cbcSMatt Macy 	"ZIL block open timeout percentage");
3844eda14cbcSMatt Macy 
3845eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs_zil, zil_, replay_disable, INT, ZMOD_RW,
3846eda14cbcSMatt Macy 	"Disable intent logging replay");
3847eda14cbcSMatt Macy 
3848eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs_zil, zil_, nocacheflush, INT, ZMOD_RW,
3849eda14cbcSMatt Macy 	"Disable ZIL cache flushes");
3850eda14cbcSMatt Macy 
3851eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs_zil, zil_, slog_bulk, ULONG, ZMOD_RW,
3852eda14cbcSMatt Macy 	"Limit in bytes slog sync writes per commit");
3853eda14cbcSMatt Macy 
3854eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs_zil, zil_, maxblocksize, INT, ZMOD_RW,
3855eda14cbcSMatt Macy 	"Limit in bytes of ZIL log block size");
3856