xref: /illumos-gate/usr/src/uts/common/fs/zfs/zil.c (revision 416e0cd82df71e380f21199abde8c5f7a18bac51)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5fe9cf88cSperrin  * Common Development and Distribution License (the "License").
6fe9cf88cSperrin  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
2213f5297eSperrin  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
27fa9e4066Sahrens 
28fa9e4066Sahrens #include <sys/zfs_context.h>
29fa9e4066Sahrens #include <sys/spa.h>
30fa9e4066Sahrens #include <sys/dmu.h>
31fa9e4066Sahrens #include <sys/zap.h>
32fa9e4066Sahrens #include <sys/arc.h>
33fa9e4066Sahrens #include <sys/stat.h>
34fa9e4066Sahrens #include <sys/resource.h>
35fa9e4066Sahrens #include <sys/zil.h>
36fa9e4066Sahrens #include <sys/zil_impl.h>
37fa9e4066Sahrens #include <sys/dsl_dataset.h>
38fa9e4066Sahrens #include <sys/vdev.h>
39fa9e4066Sahrens 
40fa9e4066Sahrens /*
41fa9e4066Sahrens  * The zfs intent log (ZIL) saves transaction records of system calls
42fa9e4066Sahrens  * that change the file system in memory with enough information
43fa9e4066Sahrens  * to be able to replay them. These are stored in memory until
44fa9e4066Sahrens  * either the DMU transaction group (txg) commits them to the stable pool
45fa9e4066Sahrens  * and they can be discarded, or they are flushed to the stable log
46fa9e4066Sahrens  * (also in the pool) due to a fsync, O_DSYNC or other synchronous
47fa9e4066Sahrens  * requirement. In the event of a panic or power fail then those log
48fa9e4066Sahrens  * records (transactions) are replayed.
49fa9e4066Sahrens  *
50fa9e4066Sahrens  * There is one ZIL per file system. Its on-disk (pool) format consists
51fa9e4066Sahrens  * of 3 parts:
52fa9e4066Sahrens  *
53fa9e4066Sahrens  * 	- ZIL header
54fa9e4066Sahrens  * 	- ZIL blocks
55fa9e4066Sahrens  * 	- ZIL records
56fa9e4066Sahrens  *
57fa9e4066Sahrens  * A log record holds a system call transaction. Log blocks can
58fa9e4066Sahrens  * hold many log records and the blocks are chained together.
59fa9e4066Sahrens  * Each ZIL block contains a block pointer (blkptr_t) to the next
60fa9e4066Sahrens  * ZIL block in the chain. The ZIL header points to the first
61fa9e4066Sahrens  * block in the chain. Note there is not a fixed place in the pool
62fa9e4066Sahrens  * to hold blocks. They are dynamically allocated and freed as
63fa9e4066Sahrens  * needed from the blocks available. Figure X shows the ZIL structure:
64fa9e4066Sahrens  */
65fa9e4066Sahrens 
66fa9e4066Sahrens /*
67*416e0cd8Sek  * This global ZIL switch affects all pools
68fa9e4066Sahrens  */
69fa9e4066Sahrens int zil_disable = 0;	/* disable intent logging */
70*416e0cd8Sek 
71*416e0cd8Sek /*
72*416e0cd8Sek  * Tunable parameter for debugging or performance analysis.  Setting
73*416e0cd8Sek  * zfs_nocacheflush will cause corruption on power loss if a volatile
74*416e0cd8Sek  * out-of-order write cache is enabled.
75*416e0cd8Sek  */
76*416e0cd8Sek boolean_t zfs_nocacheflush = B_FALSE;
77fa9e4066Sahrens 
78fa9e4066Sahrens static kmem_cache_t *zil_lwb_cache;
79fa9e4066Sahrens 
80fa9e4066Sahrens static int
81fa9e4066Sahrens zil_dva_compare(const void *x1, const void *x2)
82fa9e4066Sahrens {
83fa9e4066Sahrens 	const dva_t *dva1 = x1;
84fa9e4066Sahrens 	const dva_t *dva2 = x2;
85fa9e4066Sahrens 
86fa9e4066Sahrens 	if (DVA_GET_VDEV(dva1) < DVA_GET_VDEV(dva2))
87fa9e4066Sahrens 		return (-1);
88fa9e4066Sahrens 	if (DVA_GET_VDEV(dva1) > DVA_GET_VDEV(dva2))
89fa9e4066Sahrens 		return (1);
90fa9e4066Sahrens 
91fa9e4066Sahrens 	if (DVA_GET_OFFSET(dva1) < DVA_GET_OFFSET(dva2))
92fa9e4066Sahrens 		return (-1);
93fa9e4066Sahrens 	if (DVA_GET_OFFSET(dva1) > DVA_GET_OFFSET(dva2))
94fa9e4066Sahrens 		return (1);
95fa9e4066Sahrens 
96fa9e4066Sahrens 	return (0);
97fa9e4066Sahrens }
98fa9e4066Sahrens 
99fa9e4066Sahrens static void
100fa9e4066Sahrens zil_dva_tree_init(avl_tree_t *t)
101fa9e4066Sahrens {
102fa9e4066Sahrens 	avl_create(t, zil_dva_compare, sizeof (zil_dva_node_t),
103fa9e4066Sahrens 	    offsetof(zil_dva_node_t, zn_node));
104fa9e4066Sahrens }
105fa9e4066Sahrens 
106fa9e4066Sahrens static void
107fa9e4066Sahrens zil_dva_tree_fini(avl_tree_t *t)
108fa9e4066Sahrens {
109fa9e4066Sahrens 	zil_dva_node_t *zn;
110fa9e4066Sahrens 	void *cookie = NULL;
111fa9e4066Sahrens 
112fa9e4066Sahrens 	while ((zn = avl_destroy_nodes(t, &cookie)) != NULL)
113fa9e4066Sahrens 		kmem_free(zn, sizeof (zil_dva_node_t));
114fa9e4066Sahrens 
115fa9e4066Sahrens 	avl_destroy(t);
116fa9e4066Sahrens }
117fa9e4066Sahrens 
118fa9e4066Sahrens static int
119fa9e4066Sahrens zil_dva_tree_add(avl_tree_t *t, dva_t *dva)
120fa9e4066Sahrens {
121fa9e4066Sahrens 	zil_dva_node_t *zn;
122fa9e4066Sahrens 	avl_index_t where;
123fa9e4066Sahrens 
124fa9e4066Sahrens 	if (avl_find(t, dva, &where) != NULL)
125fa9e4066Sahrens 		return (EEXIST);
126fa9e4066Sahrens 
127fa9e4066Sahrens 	zn = kmem_alloc(sizeof (zil_dva_node_t), KM_SLEEP);
128fa9e4066Sahrens 	zn->zn_dva = *dva;
129fa9e4066Sahrens 	avl_insert(t, zn, where);
130fa9e4066Sahrens 
131fa9e4066Sahrens 	return (0);
132fa9e4066Sahrens }
133fa9e4066Sahrens 
134d80c45e0Sbonwick static zil_header_t *
135d80c45e0Sbonwick zil_header_in_syncing_context(zilog_t *zilog)
136d80c45e0Sbonwick {
137d80c45e0Sbonwick 	return ((zil_header_t *)zilog->zl_header);
138d80c45e0Sbonwick }
139d80c45e0Sbonwick 
140d80c45e0Sbonwick static void
141d80c45e0Sbonwick zil_init_log_chain(zilog_t *zilog, blkptr_t *bp)
142d80c45e0Sbonwick {
143d80c45e0Sbonwick 	zio_cksum_t *zc = &bp->blk_cksum;
144d80c45e0Sbonwick 
145d80c45e0Sbonwick 	zc->zc_word[ZIL_ZC_GUID_0] = spa_get_random(-1ULL);
146d80c45e0Sbonwick 	zc->zc_word[ZIL_ZC_GUID_1] = spa_get_random(-1ULL);
147d80c45e0Sbonwick 	zc->zc_word[ZIL_ZC_OBJSET] = dmu_objset_id(zilog->zl_os);
148d80c45e0Sbonwick 	zc->zc_word[ZIL_ZC_SEQ] = 1ULL;
149d80c45e0Sbonwick }
150d80c45e0Sbonwick 
151fa9e4066Sahrens /*
152fa9e4066Sahrens  * Read a log block, make sure it's valid, and byteswap it if necessary.
153fa9e4066Sahrens  */
154fa9e4066Sahrens static int
155d80c45e0Sbonwick zil_read_log_block(zilog_t *zilog, const blkptr_t *bp, arc_buf_t **abufpp)
156fa9e4066Sahrens {
157d80c45e0Sbonwick 	blkptr_t blk = *bp;
158ea8dc4b6Seschrock 	zbookmark_t zb;
15913506d1eSmaybee 	uint32_t aflags = ARC_WAIT;
160fa9e4066Sahrens 	int error;
161fa9e4066Sahrens 
162d80c45e0Sbonwick 	zb.zb_objset = bp->blk_cksum.zc_word[ZIL_ZC_OBJSET];
163ea8dc4b6Seschrock 	zb.zb_object = 0;
164ea8dc4b6Seschrock 	zb.zb_level = -1;
165d80c45e0Sbonwick 	zb.zb_blkid = bp->blk_cksum.zc_word[ZIL_ZC_SEQ];
166ea8dc4b6Seschrock 
167d80c45e0Sbonwick 	*abufpp = NULL;
168fa9e4066Sahrens 
169d80c45e0Sbonwick 	error = arc_read(NULL, zilog->zl_spa, &blk, byteswap_uint64_array,
170d80c45e0Sbonwick 	    arc_getbuf_func, abufpp, ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL |
17113506d1eSmaybee 	    ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB, &aflags, &zb);
172fa9e4066Sahrens 
173d80c45e0Sbonwick 	if (error == 0) {
174d80c45e0Sbonwick 		char *data = (*abufpp)->b_data;
175d80c45e0Sbonwick 		uint64_t blksz = BP_GET_LSIZE(bp);
176d80c45e0Sbonwick 		zil_trailer_t *ztp = (zil_trailer_t *)(data + blksz) - 1;
177d80c45e0Sbonwick 		zio_cksum_t cksum = bp->blk_cksum;
178fa9e4066Sahrens 
179d80c45e0Sbonwick 		/*
180d80c45e0Sbonwick 		 * Sequence numbers should be... sequential.  The checksum
181d80c45e0Sbonwick 		 * verifier for the next block should be bp's checksum plus 1.
182d80c45e0Sbonwick 		 */
183d80c45e0Sbonwick 		cksum.zc_word[ZIL_ZC_SEQ]++;
184d80c45e0Sbonwick 
185d80c45e0Sbonwick 		if (bcmp(&cksum, &ztp->zit_next_blk.blk_cksum, sizeof (cksum)))
186d80c45e0Sbonwick 			error = ESTALE;
187d80c45e0Sbonwick 		else if (BP_IS_HOLE(&ztp->zit_next_blk))
188d80c45e0Sbonwick 			error = ENOENT;
189d80c45e0Sbonwick 		else if (ztp->zit_nused > (blksz - sizeof (zil_trailer_t)))
190d80c45e0Sbonwick 			error = EOVERFLOW;
191fa9e4066Sahrens 
192d80c45e0Sbonwick 		if (error) {
193d80c45e0Sbonwick 			VERIFY(arc_buf_remove_ref(*abufpp, abufpp) == 1);
194d80c45e0Sbonwick 			*abufpp = NULL;
195d80c45e0Sbonwick 		}
196fa9e4066Sahrens 	}
197fa9e4066Sahrens 
198d80c45e0Sbonwick 	dprintf("error %d on %llu:%llu\n", error, zb.zb_objset, zb.zb_blkid);
199fa9e4066Sahrens 
200d80c45e0Sbonwick 	return (error);
201fa9e4066Sahrens }
202fa9e4066Sahrens 
203fa9e4066Sahrens /*
204fa9e4066Sahrens  * Parse the intent log, and call parse_func for each valid record within.
205d80c45e0Sbonwick  * Return the highest sequence number.
206fa9e4066Sahrens  */
207d80c45e0Sbonwick uint64_t
208fa9e4066Sahrens zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func,
209fa9e4066Sahrens     zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg)
210fa9e4066Sahrens {
211d80c45e0Sbonwick 	const zil_header_t *zh = zilog->zl_header;
212d80c45e0Sbonwick 	uint64_t claim_seq = zh->zh_claim_seq;
213d80c45e0Sbonwick 	uint64_t seq = 0;
214d80c45e0Sbonwick 	uint64_t max_seq = 0;
215d80c45e0Sbonwick 	blkptr_t blk = zh->zh_log;
216d80c45e0Sbonwick 	arc_buf_t *abuf;
217fa9e4066Sahrens 	char *lrbuf, *lrp;
218fa9e4066Sahrens 	zil_trailer_t *ztp;
219fa9e4066Sahrens 	int reclen, error;
220fa9e4066Sahrens 
221fa9e4066Sahrens 	if (BP_IS_HOLE(&blk))
222d80c45e0Sbonwick 		return (max_seq);
223fa9e4066Sahrens 
224fa9e4066Sahrens 	/*
225fa9e4066Sahrens 	 * Starting at the block pointed to by zh_log we read the log chain.
226fa9e4066Sahrens 	 * For each block in the chain we strongly check that block to
227fa9e4066Sahrens 	 * ensure its validity.  We stop when an invalid block is found.
228fa9e4066Sahrens 	 * For each block pointer in the chain we call parse_blk_func().
229fa9e4066Sahrens 	 * For each record in each valid block we call parse_lr_func().
230d80c45e0Sbonwick 	 * If the log has been claimed, stop if we encounter a sequence
231d80c45e0Sbonwick 	 * number greater than the highest claimed sequence number.
232fa9e4066Sahrens 	 */
233fa9e4066Sahrens 	zil_dva_tree_init(&zilog->zl_dva_tree);
234fa9e4066Sahrens 	for (;;) {
235d80c45e0Sbonwick 		seq = blk.blk_cksum.zc_word[ZIL_ZC_SEQ];
236d80c45e0Sbonwick 
237d80c45e0Sbonwick 		if (claim_seq != 0 && seq > claim_seq)
238d80c45e0Sbonwick 			break;
239d80c45e0Sbonwick 
240d80c45e0Sbonwick 		ASSERT(max_seq < seq);
241d80c45e0Sbonwick 		max_seq = seq;
242d80c45e0Sbonwick 
243d80c45e0Sbonwick 		error = zil_read_log_block(zilog, &blk, &abuf);
244fa9e4066Sahrens 
245fa9e4066Sahrens 		if (parse_blk_func != NULL)
246fa9e4066Sahrens 			parse_blk_func(zilog, &blk, arg, txg);
247fa9e4066Sahrens 
248fa9e4066Sahrens 		if (error)
249fa9e4066Sahrens 			break;
250fa9e4066Sahrens 
251d80c45e0Sbonwick 		lrbuf = abuf->b_data;
252fa9e4066Sahrens 		ztp = (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1;
253fa9e4066Sahrens 		blk = ztp->zit_next_blk;
254fa9e4066Sahrens 
255d80c45e0Sbonwick 		if (parse_lr_func == NULL) {
256d80c45e0Sbonwick 			VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
257fa9e4066Sahrens 			continue;
258d80c45e0Sbonwick 		}
259fa9e4066Sahrens 
260fa9e4066Sahrens 		for (lrp = lrbuf; lrp < lrbuf + ztp->zit_nused; lrp += reclen) {
261fa9e4066Sahrens 			lr_t *lr = (lr_t *)lrp;
262fa9e4066Sahrens 			reclen = lr->lrc_reclen;
263fa9e4066Sahrens 			ASSERT3U(reclen, >=, sizeof (lr_t));
264fa9e4066Sahrens 			parse_lr_func(zilog, lr, arg, txg);
265fa9e4066Sahrens 		}
266d80c45e0Sbonwick 		VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
267fa9e4066Sahrens 	}
268fa9e4066Sahrens 	zil_dva_tree_fini(&zilog->zl_dva_tree);
269d80c45e0Sbonwick 
270d80c45e0Sbonwick 	return (max_seq);
271fa9e4066Sahrens }
272fa9e4066Sahrens 
273fa9e4066Sahrens /* ARGSUSED */
274fa9e4066Sahrens static void
275fa9e4066Sahrens zil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg)
276fa9e4066Sahrens {
277fa9e4066Sahrens 	spa_t *spa = zilog->zl_spa;
278fa9e4066Sahrens 	int err;
279fa9e4066Sahrens 
280fa9e4066Sahrens 	/*
281fa9e4066Sahrens 	 * Claim log block if not already committed and not already claimed.
282fa9e4066Sahrens 	 */
283fa9e4066Sahrens 	if (bp->blk_birth >= first_txg &&
284fa9e4066Sahrens 	    zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp)) == 0) {
285fa9e4066Sahrens 		err = zio_wait(zio_claim(NULL, spa, first_txg, bp, NULL, NULL));
286fa9e4066Sahrens 		ASSERT(err == 0);
287fa9e4066Sahrens 	}
288fa9e4066Sahrens }
289fa9e4066Sahrens 
290fa9e4066Sahrens static void
291fa9e4066Sahrens zil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg)
292fa9e4066Sahrens {
293fa9e4066Sahrens 	if (lrc->lrc_txtype == TX_WRITE) {
294fa9e4066Sahrens 		lr_write_t *lr = (lr_write_t *)lrc;
295fa9e4066Sahrens 		zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg);
296fa9e4066Sahrens 	}
297fa9e4066Sahrens }
298fa9e4066Sahrens 
299fa9e4066Sahrens /* ARGSUSED */
300fa9e4066Sahrens static void
301fa9e4066Sahrens zil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg)
302fa9e4066Sahrens {
303fa9e4066Sahrens 	zio_free_blk(zilog->zl_spa, bp, dmu_tx_get_txg(tx));
304fa9e4066Sahrens }
305fa9e4066Sahrens 
306fa9e4066Sahrens static void
307fa9e4066Sahrens zil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg)
308fa9e4066Sahrens {
309fa9e4066Sahrens 	/*
310fa9e4066Sahrens 	 * If we previously claimed it, we need to free it.
311fa9e4066Sahrens 	 */
312fa9e4066Sahrens 	if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE) {
313fa9e4066Sahrens 		lr_write_t *lr = (lr_write_t *)lrc;
314fa9e4066Sahrens 		blkptr_t *bp = &lr->lr_blkptr;
315fa9e4066Sahrens 		if (bp->blk_birth >= claim_txg &&
316fa9e4066Sahrens 		    !zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp))) {
317fa9e4066Sahrens 			(void) arc_free(NULL, zilog->zl_spa,
318fa9e4066Sahrens 			    dmu_tx_get_txg(tx), bp, NULL, NULL, ARC_WAIT);
319fa9e4066Sahrens 		}
320fa9e4066Sahrens 	}
321fa9e4066Sahrens }
322fa9e4066Sahrens 
323fa9e4066Sahrens /*
324fa9e4066Sahrens  * Create an on-disk intent log.
325fa9e4066Sahrens  */
326fa9e4066Sahrens static void
327fa9e4066Sahrens zil_create(zilog_t *zilog)
328fa9e4066Sahrens {
329d80c45e0Sbonwick 	const zil_header_t *zh = zilog->zl_header;
330fa9e4066Sahrens 	lwb_t *lwb;
331d80c45e0Sbonwick 	uint64_t txg = 0;
332d80c45e0Sbonwick 	dmu_tx_t *tx = NULL;
333fa9e4066Sahrens 	blkptr_t blk;
334d80c45e0Sbonwick 	int error = 0;
335fa9e4066Sahrens 
336fa9e4066Sahrens 	/*
337d80c45e0Sbonwick 	 * Wait for any previous destroy to complete.
338fa9e4066Sahrens 	 */
339d80c45e0Sbonwick 	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
340d80c45e0Sbonwick 
341d80c45e0Sbonwick 	ASSERT(zh->zh_claim_txg == 0);
342d80c45e0Sbonwick 	ASSERT(zh->zh_replay_seq == 0);
343d80c45e0Sbonwick 
344d80c45e0Sbonwick 	blk = zh->zh_log;
345fa9e4066Sahrens 
346fa9e4066Sahrens 	/*
347d80c45e0Sbonwick 	 * If we don't already have an initial log block, allocate one now.
348fa9e4066Sahrens 	 */
349d80c45e0Sbonwick 	if (BP_IS_HOLE(&blk)) {
350d80c45e0Sbonwick 		tx = dmu_tx_create(zilog->zl_os);
351d80c45e0Sbonwick 		(void) dmu_tx_assign(tx, TXG_WAIT);
352d80c45e0Sbonwick 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
353d80c45e0Sbonwick 		txg = dmu_tx_get_txg(tx);
354d80c45e0Sbonwick 
355d80c45e0Sbonwick 		error = zio_alloc_blk(zilog->zl_spa, ZIL_MIN_BLKSZ, &blk, txg);
356d80c45e0Sbonwick 
357d80c45e0Sbonwick 		if (error == 0)
358d80c45e0Sbonwick 			zil_init_log_chain(zilog, &blk);
35913f5297eSperrin 	}
360fa9e4066Sahrens 
361d80c45e0Sbonwick 	/*
362d80c45e0Sbonwick 	 * Allocate a log write buffer (lwb) for the first log block.
363d80c45e0Sbonwick 	 */
364d80c45e0Sbonwick 	if (error == 0) {
365fa9e4066Sahrens 		lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP);
366fa9e4066Sahrens 		lwb->lwb_zilog = zilog;
367fa9e4066Sahrens 		lwb->lwb_blk = blk;
368fa9e4066Sahrens 		lwb->lwb_nused = 0;
369fa9e4066Sahrens 		lwb->lwb_sz = BP_GET_LSIZE(&lwb->lwb_blk);
370fa9e4066Sahrens 		lwb->lwb_buf = zio_buf_alloc(lwb->lwb_sz);
371fa9e4066Sahrens 		lwb->lwb_max_txg = txg;
372c5c6ffa0Smaybee 		lwb->lwb_zio = NULL;
373c5c6ffa0Smaybee 
374fa9e4066Sahrens 		mutex_enter(&zilog->zl_lock);
375fa9e4066Sahrens 		list_insert_tail(&zilog->zl_lwb_list, lwb);
376fa9e4066Sahrens 		mutex_exit(&zilog->zl_lock);
377fa9e4066Sahrens 	}
378fa9e4066Sahrens 
379d80c45e0Sbonwick 	/*
380d80c45e0Sbonwick 	 * If we just allocated the first log block, commit our transaction
381d80c45e0Sbonwick 	 * and wait for zil_sync() to stuff the block poiner into zh_log.
382d80c45e0Sbonwick 	 * (zh is part of the MOS, so we cannot modify it in open context.)
383d80c45e0Sbonwick 	 */
384d80c45e0Sbonwick 	if (tx != NULL) {
385d80c45e0Sbonwick 		dmu_tx_commit(tx);
38613f5297eSperrin 		txg_wait_synced(zilog->zl_dmu_pool, txg);
387d80c45e0Sbonwick 	}
388d80c45e0Sbonwick 
389d80c45e0Sbonwick 	ASSERT(bcmp(&blk, &zh->zh_log, sizeof (blk)) == 0);
390fa9e4066Sahrens }
391fa9e4066Sahrens 
392fa9e4066Sahrens /*
393fa9e4066Sahrens  * In one tx, free all log blocks and clear the log header.
394d80c45e0Sbonwick  * If keep_first is set, then we're replaying a log with no content.
395d80c45e0Sbonwick  * We want to keep the first block, however, so that the first
396d80c45e0Sbonwick  * synchronous transaction doesn't require a txg_wait_synced()
397d80c45e0Sbonwick  * in zil_create().  We don't need to txg_wait_synced() here either
398d80c45e0Sbonwick  * when keep_first is set, because both zil_create() and zil_destroy()
399d80c45e0Sbonwick  * will wait for any in-progress destroys to complete.
400fa9e4066Sahrens  */
401fa9e4066Sahrens void
402d80c45e0Sbonwick zil_destroy(zilog_t *zilog, boolean_t keep_first)
403fa9e4066Sahrens {
404d80c45e0Sbonwick 	const zil_header_t *zh = zilog->zl_header;
405d80c45e0Sbonwick 	lwb_t *lwb;
406fa9e4066Sahrens 	dmu_tx_t *tx;
407fa9e4066Sahrens 	uint64_t txg;
408fa9e4066Sahrens 
409d80c45e0Sbonwick 	/*
410d80c45e0Sbonwick 	 * Wait for any previous destroy to complete.
411d80c45e0Sbonwick 	 */
412d80c45e0Sbonwick 	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
413fa9e4066Sahrens 
414d80c45e0Sbonwick 	if (BP_IS_HOLE(&zh->zh_log))
415fa9e4066Sahrens 		return;
416fa9e4066Sahrens 
417fa9e4066Sahrens 	tx = dmu_tx_create(zilog->zl_os);
418fa9e4066Sahrens 	(void) dmu_tx_assign(tx, TXG_WAIT);
419fa9e4066Sahrens 	dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
420fa9e4066Sahrens 	txg = dmu_tx_get_txg(tx);
421fa9e4066Sahrens 
422d80c45e0Sbonwick 	mutex_enter(&zilog->zl_lock);
423d80c45e0Sbonwick 
424d80c45e0Sbonwick 	ASSERT3U(zilog->zl_destroy_txg, <, txg);
425fa9e4066Sahrens 	zilog->zl_destroy_txg = txg;
426d80c45e0Sbonwick 	zilog->zl_keep_first = keep_first;
427d80c45e0Sbonwick 
428d80c45e0Sbonwick 	if (!list_is_empty(&zilog->zl_lwb_list)) {
429d80c45e0Sbonwick 		ASSERT(zh->zh_claim_txg == 0);
430d80c45e0Sbonwick 		ASSERT(!keep_first);
431d80c45e0Sbonwick 		while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
432d80c45e0Sbonwick 			list_remove(&zilog->zl_lwb_list, lwb);
433d80c45e0Sbonwick 			if (lwb->lwb_buf != NULL)
434d80c45e0Sbonwick 				zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
435d80c45e0Sbonwick 			zio_free_blk(zilog->zl_spa, &lwb->lwb_blk, txg);
436d80c45e0Sbonwick 			kmem_cache_free(zil_lwb_cache, lwb);
437d80c45e0Sbonwick 		}
438d80c45e0Sbonwick 	} else {
439d80c45e0Sbonwick 		if (!keep_first) {
440d80c45e0Sbonwick 			(void) zil_parse(zilog, zil_free_log_block,
441d80c45e0Sbonwick 			    zil_free_log_record, tx, zh->zh_claim_txg);
442d80c45e0Sbonwick 		}
443d80c45e0Sbonwick 	}
444b19a79ecSperrin 	mutex_exit(&zilog->zl_lock);
445fa9e4066Sahrens 
446fa9e4066Sahrens 	dmu_tx_commit(tx);
447fa9e4066Sahrens 
448d80c45e0Sbonwick 	if (keep_first)			/* no need to wait in this case */
449d80c45e0Sbonwick 		return;
450d80c45e0Sbonwick 
451d80c45e0Sbonwick 	txg_wait_synced(zilog->zl_dmu_pool, txg);
452d80c45e0Sbonwick 	ASSERT(BP_IS_HOLE(&zh->zh_log));
453fa9e4066Sahrens }
454fa9e4066Sahrens 
4551d452cf5Sahrens int
456fa9e4066Sahrens zil_claim(char *osname, void *txarg)
457fa9e4066Sahrens {
458fa9e4066Sahrens 	dmu_tx_t *tx = txarg;
459fa9e4066Sahrens 	uint64_t first_txg = dmu_tx_get_txg(tx);
460fa9e4066Sahrens 	zilog_t *zilog;
461fa9e4066Sahrens 	zil_header_t *zh;
462fa9e4066Sahrens 	objset_t *os;
463fa9e4066Sahrens 	int error;
464fa9e4066Sahrens 
465fa9e4066Sahrens 	error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_STANDARD, &os);
466fa9e4066Sahrens 	if (error) {
467fa9e4066Sahrens 		cmn_err(CE_WARN, "can't process intent log for %s", osname);
4681d452cf5Sahrens 		return (0);
469fa9e4066Sahrens 	}
470fa9e4066Sahrens 
471fa9e4066Sahrens 	zilog = dmu_objset_zil(os);
472d80c45e0Sbonwick 	zh = zil_header_in_syncing_context(zilog);
473fa9e4066Sahrens 
474fa9e4066Sahrens 	/*
475d80c45e0Sbonwick 	 * Claim all log blocks if we haven't already done so, and remember
476d80c45e0Sbonwick 	 * the highest claimed sequence number.  This ensures that if we can
477d80c45e0Sbonwick 	 * read only part of the log now (e.g. due to a missing device),
478d80c45e0Sbonwick 	 * but we can read the entire log later, we will not try to replay
479d80c45e0Sbonwick 	 * or destroy beyond the last block we successfully claimed.
480fa9e4066Sahrens 	 */
481fa9e4066Sahrens 	ASSERT3U(zh->zh_claim_txg, <=, first_txg);
482fa9e4066Sahrens 	if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) {
483fa9e4066Sahrens 		zh->zh_claim_txg = first_txg;
484d80c45e0Sbonwick 		zh->zh_claim_seq = zil_parse(zilog, zil_claim_log_block,
485d80c45e0Sbonwick 		    zil_claim_log_record, tx, first_txg);
486fa9e4066Sahrens 		dsl_dataset_dirty(dmu_objset_ds(os), tx);
487fa9e4066Sahrens 	}
488d80c45e0Sbonwick 
489fa9e4066Sahrens 	ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1));
490fa9e4066Sahrens 	dmu_objset_close(os);
4911d452cf5Sahrens 	return (0);
492fa9e4066Sahrens }
493fa9e4066Sahrens 
494fa9e4066Sahrens void
495b19a79ecSperrin zil_add_vdev(zilog_t *zilog, uint64_t vdev)
496fa9e4066Sahrens {
497fa9e4066Sahrens 	zil_vdev_t *zv;
498fa9e4066Sahrens 
499*416e0cd8Sek 	if (zfs_nocacheflush)
500fa9e4066Sahrens 		return;
501fa9e4066Sahrens 
502fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&zilog->zl_lock));
503fa9e4066Sahrens 	zv = kmem_alloc(sizeof (zil_vdev_t), KM_SLEEP);
504fa9e4066Sahrens 	zv->vdev = vdev;
505fa9e4066Sahrens 	list_insert_tail(&zilog->zl_vdev_list, zv);
506fa9e4066Sahrens }
507fa9e4066Sahrens 
508fa9e4066Sahrens void
509b19a79ecSperrin zil_flush_vdevs(zilog_t *zilog)
510fa9e4066Sahrens {
511fa9e4066Sahrens 	vdev_t *vd;
512fa9e4066Sahrens 	zil_vdev_t *zv, *zv2;
513fa9e4066Sahrens 	zio_t *zio;
514fa9e4066Sahrens 	spa_t *spa;
515fa9e4066Sahrens 	uint64_t vdev;
516fa9e4066Sahrens 
517*416e0cd8Sek 	if (zfs_nocacheflush)
518fa9e4066Sahrens 		return;
519fa9e4066Sahrens 
520fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&zilog->zl_lock));
521fa9e4066Sahrens 
522fa9e4066Sahrens 	spa = zilog->zl_spa;
523fa9e4066Sahrens 	zio = NULL;
524fa9e4066Sahrens 
525b19a79ecSperrin 	while ((zv = list_head(&zilog->zl_vdev_list)) != NULL) {
526fa9e4066Sahrens 		vdev = zv->vdev;
527fa9e4066Sahrens 		list_remove(&zilog->zl_vdev_list, zv);
528fa9e4066Sahrens 		kmem_free(zv, sizeof (zil_vdev_t));
529fa9e4066Sahrens 
530fa9e4066Sahrens 		/*
531b19a79ecSperrin 		 * remove all chained entries with same vdev
532fa9e4066Sahrens 		 */
533fa9e4066Sahrens 		zv = list_head(&zilog->zl_vdev_list);
534b19a79ecSperrin 		while (zv) {
535fa9e4066Sahrens 			zv2 = list_next(&zilog->zl_vdev_list, zv);
536fa9e4066Sahrens 			if (zv->vdev == vdev) {
537fa9e4066Sahrens 				list_remove(&zilog->zl_vdev_list, zv);
538fa9e4066Sahrens 				kmem_free(zv, sizeof (zil_vdev_t));
539fa9e4066Sahrens 			}
540fa9e4066Sahrens 			zv = zv2;
541fa9e4066Sahrens 		}
542fa9e4066Sahrens 
543fa9e4066Sahrens 		/* flush the write cache for this vdev */
544fa9e4066Sahrens 		mutex_exit(&zilog->zl_lock);
545fa9e4066Sahrens 		if (zio == NULL)
546fa9e4066Sahrens 			zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
547fa9e4066Sahrens 		vd = vdev_lookup_top(spa, vdev);
548fa9e4066Sahrens 		ASSERT(vd);
549fa9e4066Sahrens 		(void) zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE,
550fa9e4066Sahrens 		    NULL, NULL, ZIO_PRIORITY_NOW,
551fa9e4066Sahrens 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
552fa9e4066Sahrens 		mutex_enter(&zilog->zl_lock);
553fa9e4066Sahrens 	}
554fa9e4066Sahrens 
555fa9e4066Sahrens 	/*
556fa9e4066Sahrens 	 * Wait for all the flushes to complete.  Not all devices actually
557fa9e4066Sahrens 	 * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails.
558fa9e4066Sahrens 	 */
55922ac5be4Sperrin 	if (zio != NULL) {
56022ac5be4Sperrin 		mutex_exit(&zilog->zl_lock);
561fa9e4066Sahrens 		(void) zio_wait(zio);
56222ac5be4Sperrin 		mutex_enter(&zilog->zl_lock);
56322ac5be4Sperrin 	}
564fa9e4066Sahrens }
565fa9e4066Sahrens 
566fa9e4066Sahrens /*
567fa9e4066Sahrens  * Function called when a log block write completes
568fa9e4066Sahrens  */
569fa9e4066Sahrens static void
570fa9e4066Sahrens zil_lwb_write_done(zio_t *zio)
571fa9e4066Sahrens {
572fa9e4066Sahrens 	lwb_t *lwb = zio->io_private;
573fa9e4066Sahrens 	zilog_t *zilog = lwb->lwb_zilog;
574fa9e4066Sahrens 
575fa9e4066Sahrens 	/*
576fa9e4066Sahrens 	 * Now that we've written this log block, we have a stable pointer
577fa9e4066Sahrens 	 * to the next block in the chain, so it's OK to let the txg in
578fa9e4066Sahrens 	 * which we allocated the next block sync.
579fa9e4066Sahrens 	 */
580fa9e4066Sahrens 	txg_rele_to_sync(&lwb->lwb_txgh);
581fa9e4066Sahrens 
582fa9e4066Sahrens 	zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
583fa9e4066Sahrens 	mutex_enter(&zilog->zl_lock);
584fa9e4066Sahrens 	lwb->lwb_buf = NULL;
585fa9e4066Sahrens 	if (zio->io_error) {
586fa9e4066Sahrens 		zilog->zl_log_error = B_TRUE;
587fa9e4066Sahrens 		mutex_exit(&zilog->zl_lock);
588fa9e4066Sahrens 		return;
589fa9e4066Sahrens 	}
590fa9e4066Sahrens 	mutex_exit(&zilog->zl_lock);
591fa9e4066Sahrens }
592fa9e4066Sahrens 
593c5c6ffa0Smaybee /*
594c5c6ffa0Smaybee  * Initialize the io for a log block.
595c5c6ffa0Smaybee  *
596c5c6ffa0Smaybee  * Note, we should not initialize the IO until we are about
597c5c6ffa0Smaybee  * to use it, since zio_rewrite() does a spa_config_enter().
598c5c6ffa0Smaybee  */
599c5c6ffa0Smaybee static void
600c5c6ffa0Smaybee zil_lwb_write_init(zilog_t *zilog, lwb_t *lwb)
601c5c6ffa0Smaybee {
602c5c6ffa0Smaybee 	zbookmark_t zb;
603c5c6ffa0Smaybee 
604c5c6ffa0Smaybee 	zb.zb_objset = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET];
605c5c6ffa0Smaybee 	zb.zb_object = 0;
606c5c6ffa0Smaybee 	zb.zb_level = -1;
607c5c6ffa0Smaybee 	zb.zb_blkid = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ];
608c5c6ffa0Smaybee 
609b19a79ecSperrin 	if (zilog->zl_root_zio == NULL) {
610b19a79ecSperrin 		zilog->zl_root_zio = zio_root(zilog->zl_spa, NULL, NULL,
611b19a79ecSperrin 		    ZIO_FLAG_CANFAIL);
612b19a79ecSperrin 	}
613b19a79ecSperrin 	lwb->lwb_zio = zio_rewrite(zilog->zl_root_zio, zilog->zl_spa,
614c5c6ffa0Smaybee 	    ZIO_CHECKSUM_ZILOG, 0, &lwb->lwb_blk, lwb->lwb_buf,
615c5c6ffa0Smaybee 	    lwb->lwb_sz, zil_lwb_write_done, lwb,
616c5c6ffa0Smaybee 	    ZIO_PRIORITY_LOG_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
617c5c6ffa0Smaybee }
618c5c6ffa0Smaybee 
619fa9e4066Sahrens /*
620fa9e4066Sahrens  * Start a log block write and advance to the next log block.
621fa9e4066Sahrens  * Calls are serialized.
622fa9e4066Sahrens  */
623fa9e4066Sahrens static lwb_t *
624fa9e4066Sahrens zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb)
625fa9e4066Sahrens {
626fa9e4066Sahrens 	lwb_t *nlwb;
627fa9e4066Sahrens 	zil_trailer_t *ztp = (zil_trailer_t *)(lwb->lwb_buf + lwb->lwb_sz) - 1;
628d80c45e0Sbonwick 	spa_t *spa = zilog->zl_spa;
629d80c45e0Sbonwick 	blkptr_t *bp = &ztp->zit_next_blk;
630fa9e4066Sahrens 	uint64_t txg;
631fa9e4066Sahrens 	uint64_t zil_blksz;
632fa9e4066Sahrens 	int error;
633fa9e4066Sahrens 
634fa9e4066Sahrens 	ASSERT(lwb->lwb_nused <= ZIL_BLK_DATA_SZ(lwb));
635fa9e4066Sahrens 
636fa9e4066Sahrens 	/*
637fa9e4066Sahrens 	 * Allocate the next block and save its address in this block
638fa9e4066Sahrens 	 * before writing it in order to establish the log chain.
639fa9e4066Sahrens 	 * Note that if the allocation of nlwb synced before we wrote
640fa9e4066Sahrens 	 * the block that points at it (lwb), we'd leak it if we crashed.
641fa9e4066Sahrens 	 * Therefore, we don't do txg_rele_to_sync() until zil_lwb_write_done().
642fa9e4066Sahrens 	 */
643fa9e4066Sahrens 	txg = txg_hold_open(zilog->zl_dmu_pool, &lwb->lwb_txgh);
644fa9e4066Sahrens 	txg_rele_to_quiesce(&lwb->lwb_txgh);
645fa9e4066Sahrens 
646fa9e4066Sahrens 	/*
64722ac5be4Sperrin 	 * Pick a ZIL blocksize. We request a size that is the
64822ac5be4Sperrin 	 * maximum of the previous used size, the current used size and
64922ac5be4Sperrin 	 * the amount waiting in the queue.
650fa9e4066Sahrens 	 */
651c5c6ffa0Smaybee 	zil_blksz = MAX(zilog->zl_prev_used,
652c5c6ffa0Smaybee 	    zilog->zl_cur_used + sizeof (*ztp));
65322ac5be4Sperrin 	zil_blksz = MAX(zil_blksz, zilog->zl_itx_list_sz + sizeof (*ztp));
654b4d654b0Sperrin 	zil_blksz = P2ROUNDUP_TYPED(zil_blksz, ZIL_MIN_BLKSZ, uint64_t);
65522ac5be4Sperrin 	if (zil_blksz > ZIL_MAX_BLKSZ)
65622ac5be4Sperrin 		zil_blksz = ZIL_MAX_BLKSZ;
657fa9e4066Sahrens 
658d80c45e0Sbonwick 	error = zio_alloc_blk(spa, zil_blksz, bp, txg);
659fa9e4066Sahrens 	if (error) {
660ea8dc4b6Seschrock 		/*
661ea8dc4b6Seschrock 		 * Reinitialise the lwb.
662ea8dc4b6Seschrock 		 * By returning NULL the caller will call tx_wait_synced()
663ea8dc4b6Seschrock 		 */
664ea8dc4b6Seschrock 		mutex_enter(&zilog->zl_lock);
665ea8dc4b6Seschrock 		lwb->lwb_nused = 0;
666ea8dc4b6Seschrock 		mutex_exit(&zilog->zl_lock);
667fa9e4066Sahrens 		txg_rele_to_sync(&lwb->lwb_txgh);
668fa9e4066Sahrens 		return (NULL);
669fa9e4066Sahrens 	}
670fa9e4066Sahrens 
671d80c45e0Sbonwick 	ASSERT3U(bp->blk_birth, ==, txg);
672ea8dc4b6Seschrock 	ztp->zit_pad = 0;
673fa9e4066Sahrens 	ztp->zit_nused = lwb->lwb_nused;
674fa9e4066Sahrens 	ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum;
675d80c45e0Sbonwick 	bp->blk_cksum = lwb->lwb_blk.blk_cksum;
676d80c45e0Sbonwick 	bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++;
677fa9e4066Sahrens 
678fa9e4066Sahrens 	/*
679fa9e4066Sahrens 	 * Allocate a new log write buffer (lwb).
680fa9e4066Sahrens 	 */
681fa9e4066Sahrens 	nlwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP);
682fa9e4066Sahrens 
683fa9e4066Sahrens 	nlwb->lwb_zilog = zilog;
684d80c45e0Sbonwick 	nlwb->lwb_blk = *bp;
685fa9e4066Sahrens 	nlwb->lwb_nused = 0;
686fa9e4066Sahrens 	nlwb->lwb_sz = BP_GET_LSIZE(&nlwb->lwb_blk);
687fa9e4066Sahrens 	nlwb->lwb_buf = zio_buf_alloc(nlwb->lwb_sz);
688fa9e4066Sahrens 	nlwb->lwb_max_txg = txg;
689c5c6ffa0Smaybee 	nlwb->lwb_zio = NULL;
690fa9e4066Sahrens 
691fa9e4066Sahrens 	/*
692fa9e4066Sahrens 	 * Put new lwb at the end of the log chain,
693fa9e4066Sahrens 	 * and record the vdev for later flushing
694fa9e4066Sahrens 	 */
695fa9e4066Sahrens 	mutex_enter(&zilog->zl_lock);
696fa9e4066Sahrens 	list_insert_tail(&zilog->zl_lwb_list, nlwb);
697b19a79ecSperrin 	zil_add_vdev(zilog, DVA_GET_VDEV(BP_IDENTITY(&(lwb->lwb_blk))));
698fa9e4066Sahrens 	mutex_exit(&zilog->zl_lock);
699fa9e4066Sahrens 
700fa9e4066Sahrens 	/*
701c5c6ffa0Smaybee 	 * kick off the write for the old log block
702fa9e4066Sahrens 	 */
703c5c6ffa0Smaybee 	dprintf_bp(&lwb->lwb_blk, "lwb %p txg %llu: ", lwb, txg);
704b19a79ecSperrin 	if (lwb->lwb_zio == NULL)
705c5c6ffa0Smaybee 		zil_lwb_write_init(zilog, lwb);
706c5c6ffa0Smaybee 	zio_nowait(lwb->lwb_zio);
707fa9e4066Sahrens 
708fa9e4066Sahrens 	return (nlwb);
709fa9e4066Sahrens }
710fa9e4066Sahrens 
711fa9e4066Sahrens static lwb_t *
712fa9e4066Sahrens zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb)
713fa9e4066Sahrens {
714fa9e4066Sahrens 	lr_t *lrc = &itx->itx_lr; /* common log record */
715c5c6ffa0Smaybee 	lr_write_t *lr = (lr_write_t *)lrc;
716fa9e4066Sahrens 	uint64_t txg = lrc->lrc_txg;
717fa9e4066Sahrens 	uint64_t reclen = lrc->lrc_reclen;
718c5c6ffa0Smaybee 	uint64_t dlen;
719fa9e4066Sahrens 
720fa9e4066Sahrens 	if (lwb == NULL)
721fa9e4066Sahrens 		return (NULL);
722fa9e4066Sahrens 	ASSERT(lwb->lwb_buf != NULL);
723fa9e4066Sahrens 
724c5c6ffa0Smaybee 	if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY)
725c5c6ffa0Smaybee 		dlen = P2ROUNDUP_TYPED(
726c5c6ffa0Smaybee 		    lr->lr_length, sizeof (uint64_t), uint64_t);
727c5c6ffa0Smaybee 	else
728c5c6ffa0Smaybee 		dlen = 0;
729fa9e4066Sahrens 
730104e2ed7Sperrin 	zilog->zl_cur_used += (reclen + dlen);
73122ac5be4Sperrin 
732fa9e4066Sahrens 	/*
733fa9e4066Sahrens 	 * If this record won't fit in the current log block, start a new one.
734fa9e4066Sahrens 	 */
735104e2ed7Sperrin 	if (lwb->lwb_nused + reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) {
736fa9e4066Sahrens 		lwb = zil_lwb_write_start(zilog, lwb);
737c5c6ffa0Smaybee 		if (lwb == NULL)
738fa9e4066Sahrens 			return (NULL);
739ea8dc4b6Seschrock 		ASSERT(lwb->lwb_nused == 0);
740104e2ed7Sperrin 		if (reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) {
741fa9e4066Sahrens 			txg_wait_synced(zilog->zl_dmu_pool, txg);
742fa9e4066Sahrens 			return (lwb);
743fa9e4066Sahrens 		}
744fa9e4066Sahrens 	}
745fa9e4066Sahrens 
746b19a79ecSperrin 	/*
747b19a79ecSperrin 	 * Update the lrc_seq, to be log record sequence number. See zil.h
748b19a79ecSperrin 	 * Then copy the record to the log buffer.
749b19a79ecSperrin 	 */
750b19a79ecSperrin 	lrc->lrc_seq = ++zilog->zl_lr_seq; /* we are single threaded */
751fa9e4066Sahrens 	bcopy(lrc, lwb->lwb_buf + lwb->lwb_nused, reclen);
752c5c6ffa0Smaybee 
753c5c6ffa0Smaybee 	/*
754c5c6ffa0Smaybee 	 * If it's a write, fetch the data or get its blkptr as appropriate.
755c5c6ffa0Smaybee 	 */
756c5c6ffa0Smaybee 	if (lrc->lrc_txtype == TX_WRITE) {
757c5c6ffa0Smaybee 		if (txg > spa_freeze_txg(zilog->zl_spa))
758c5c6ffa0Smaybee 			txg_wait_synced(zilog->zl_dmu_pool, txg);
759c5c6ffa0Smaybee 		if (itx->itx_wr_state != WR_COPIED) {
760c5c6ffa0Smaybee 			char *dbuf;
761c5c6ffa0Smaybee 			int error;
762c5c6ffa0Smaybee 
763c5c6ffa0Smaybee 			/* alignment is guaranteed */
764c5c6ffa0Smaybee 			lr = (lr_write_t *)(lwb->lwb_buf + lwb->lwb_nused);
765c5c6ffa0Smaybee 			if (dlen) {
766c5c6ffa0Smaybee 				ASSERT(itx->itx_wr_state == WR_NEED_COPY);
767c5c6ffa0Smaybee 				dbuf = lwb->lwb_buf + lwb->lwb_nused + reclen;
768c5c6ffa0Smaybee 				lr->lr_common.lrc_reclen += dlen;
769c5c6ffa0Smaybee 			} else {
770c5c6ffa0Smaybee 				ASSERT(itx->itx_wr_state == WR_INDIRECT);
771c5c6ffa0Smaybee 				dbuf = NULL;
772c5c6ffa0Smaybee 			}
773c5c6ffa0Smaybee 			error = zilog->zl_get_data(
774c5c6ffa0Smaybee 			    itx->itx_private, lr, dbuf, lwb->lwb_zio);
775c5c6ffa0Smaybee 			if (error) {
776c5c6ffa0Smaybee 				ASSERT(error == ENOENT || error == EEXIST ||
777c5c6ffa0Smaybee 				    error == EALREADY);
778c5c6ffa0Smaybee 				return (lwb);
779c5c6ffa0Smaybee 			}
780c5c6ffa0Smaybee 		}
781104e2ed7Sperrin 	}
782c5c6ffa0Smaybee 
783c5c6ffa0Smaybee 	lwb->lwb_nused += reclen + dlen;
784fa9e4066Sahrens 	lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg);
785fa9e4066Sahrens 	ASSERT3U(lwb->lwb_nused, <=, ZIL_BLK_DATA_SZ(lwb));
786fa9e4066Sahrens 	ASSERT3U(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)), ==, 0);
787fa9e4066Sahrens 
788fa9e4066Sahrens 	return (lwb);
789fa9e4066Sahrens }
790fa9e4066Sahrens 
791fa9e4066Sahrens itx_t *
792fa9e4066Sahrens zil_itx_create(int txtype, size_t lrsize)
793fa9e4066Sahrens {
794fa9e4066Sahrens 	itx_t *itx;
795fa9e4066Sahrens 
796b4d654b0Sperrin 	lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t);
797fa9e4066Sahrens 
798fa9e4066Sahrens 	itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP);
799fa9e4066Sahrens 	itx->itx_lr.lrc_txtype = txtype;
800fa9e4066Sahrens 	itx->itx_lr.lrc_reclen = lrsize;
801fa9e4066Sahrens 	itx->itx_lr.lrc_seq = 0;	/* defensive */
802fa9e4066Sahrens 
803fa9e4066Sahrens 	return (itx);
804fa9e4066Sahrens }
805fa9e4066Sahrens 
806fa9e4066Sahrens uint64_t
807fa9e4066Sahrens zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx)
808fa9e4066Sahrens {
809fa9e4066Sahrens 	uint64_t seq;
810fa9e4066Sahrens 
811fa9e4066Sahrens 	ASSERT(itx->itx_lr.lrc_seq == 0);
812fa9e4066Sahrens 
813fa9e4066Sahrens 	mutex_enter(&zilog->zl_lock);
814fa9e4066Sahrens 	list_insert_tail(&zilog->zl_itx_list, itx);
815fa9e4066Sahrens 	zilog->zl_itx_list_sz += itx->itx_lr.lrc_reclen;
816fa9e4066Sahrens 	itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx);
817fa9e4066Sahrens 	itx->itx_lr.lrc_seq = seq = ++zilog->zl_itx_seq;
818fa9e4066Sahrens 	mutex_exit(&zilog->zl_lock);
819fa9e4066Sahrens 
820fa9e4066Sahrens 	return (seq);
821fa9e4066Sahrens }
822fa9e4066Sahrens 
823fa9e4066Sahrens /*
824fa9e4066Sahrens  * Free up all in-memory intent log transactions that have now been synced.
825fa9e4066Sahrens  */
826fa9e4066Sahrens static void
827fa9e4066Sahrens zil_itx_clean(zilog_t *zilog)
828fa9e4066Sahrens {
829fa9e4066Sahrens 	uint64_t synced_txg = spa_last_synced_txg(zilog->zl_spa);
830fa9e4066Sahrens 	uint64_t freeze_txg = spa_freeze_txg(zilog->zl_spa);
831fa9e4066Sahrens 	itx_t *itx;
832fa9e4066Sahrens 
833fa9e4066Sahrens 	mutex_enter(&zilog->zl_lock);
834b19a79ecSperrin 	/* wait for a log writer to finish walking list */
835b19a79ecSperrin 	while (zilog->zl_writer) {
836b19a79ecSperrin 		cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
837b19a79ecSperrin 	}
838b19a79ecSperrin 	/* no need to set zl_writer as we never drop zl_lock */
839fa9e4066Sahrens 	while ((itx = list_head(&zilog->zl_itx_list)) != NULL &&
840fa9e4066Sahrens 	    itx->itx_lr.lrc_txg <= MIN(synced_txg, freeze_txg)) {
841fa9e4066Sahrens 		list_remove(&zilog->zl_itx_list, itx);
842fa9e4066Sahrens 		zilog->zl_itx_list_sz -= itx->itx_lr.lrc_reclen;
843fa9e4066Sahrens 		kmem_free(itx, offsetof(itx_t, itx_lr)
844fa9e4066Sahrens 		    + itx->itx_lr.lrc_reclen);
845fa9e4066Sahrens 	}
846fa9e4066Sahrens 	mutex_exit(&zilog->zl_lock);
847fa9e4066Sahrens }
848fa9e4066Sahrens 
849b19a79ecSperrin /*
850b19a79ecSperrin  * If there are in-memory intent log transactions then
851b19a79ecSperrin  * start up a taskq to free up any that have now been synced.
852b19a79ecSperrin  */
853fa9e4066Sahrens void
854fa9e4066Sahrens zil_clean(zilog_t *zilog)
855fa9e4066Sahrens {
856fa9e4066Sahrens 	mutex_enter(&zilog->zl_lock);
857fa9e4066Sahrens 	if (list_head(&zilog->zl_itx_list) != NULL)
858fa9e4066Sahrens 		(void) taskq_dispatch(zilog->zl_clean_taskq,
859fa9e4066Sahrens 		    (void (*)(void *))zil_itx_clean, zilog, TQ_NOSLEEP);
860fa9e4066Sahrens 	mutex_exit(&zilog->zl_lock);
861fa9e4066Sahrens }
862fa9e4066Sahrens 
863fa9e4066Sahrens void
864b19a79ecSperrin zil_commit_writer(zilog_t *zilog, uint64_t seq, uint64_t foid)
865fa9e4066Sahrens {
866fa9e4066Sahrens 	uint64_t txg;
867fa9e4066Sahrens 	uint64_t reclen;
868b19a79ecSperrin 	itx_t *itx, *itx_next = (itx_t *)-1;
869fa9e4066Sahrens 	lwb_t *lwb;
870fa9e4066Sahrens 	spa_t *spa;
871fa9e4066Sahrens 
872fa9e4066Sahrens 	zilog->zl_writer = B_TRUE;
873b19a79ecSperrin 	zilog->zl_root_zio = NULL;
874b19a79ecSperrin 	spa = zilog->zl_spa;
875fa9e4066Sahrens 
876fa9e4066Sahrens 	if (zilog->zl_suspend) {
877fa9e4066Sahrens 		lwb = NULL;
878fa9e4066Sahrens 	} else {
879fa9e4066Sahrens 		lwb = list_tail(&zilog->zl_lwb_list);
880fa9e4066Sahrens 		if (lwb == NULL) {
881b19a79ecSperrin 			/*
882b19a79ecSperrin 			 * Return if there's nothing to flush before we
883b19a79ecSperrin 			 * dirty the fs by calling zil_create()
884b19a79ecSperrin 			 */
885b19a79ecSperrin 			if (list_is_empty(&zilog->zl_itx_list)) {
886b19a79ecSperrin 				/* wake up others waiting to start a write */
887b19a79ecSperrin 				zilog->zl_writer = B_FALSE;
888b19a79ecSperrin 				cv_broadcast(&zilog->zl_cv_writer);
889b19a79ecSperrin 				mutex_exit(&zilog->zl_lock);
890b19a79ecSperrin 				return;
891b19a79ecSperrin 			}
892b19a79ecSperrin 
893fa9e4066Sahrens 			mutex_exit(&zilog->zl_lock);
894fa9e4066Sahrens 			zil_create(zilog);
895fa9e4066Sahrens 			mutex_enter(&zilog->zl_lock);
896fa9e4066Sahrens 			lwb = list_tail(&zilog->zl_lwb_list);
897fa9e4066Sahrens 		}
898fa9e4066Sahrens 	}
899fa9e4066Sahrens 
900fa9e4066Sahrens 	/*
901fa9e4066Sahrens 	 * Loop through in-memory log transactions filling log blocks,
902fa9e4066Sahrens 	 * until we reach the given sequence number and there's no more
903fa9e4066Sahrens 	 * room in the write buffer.
904fa9e4066Sahrens 	 */
905b19a79ecSperrin 	DTRACE_PROBE1(zil__cw1, zilog_t *, zilog);
906fa9e4066Sahrens 	for (;;) {
907b19a79ecSperrin 		/*
908b19a79ecSperrin 		 * Find the next itx to push:
909b19a79ecSperrin 		 * Push all transactions related to specified foid and all
910b19a79ecSperrin 		 * other transactions except TX_WRITE, TX_TRUNCATE,
911b19a79ecSperrin 		 * TX_SETATTR and TX_ACL for all other files.
912b19a79ecSperrin 		 */
913b19a79ecSperrin 		if (itx_next != (itx_t *)-1)
914b19a79ecSperrin 			itx = itx_next;
915b19a79ecSperrin 		else
916b19a79ecSperrin 			itx = list_head(&zilog->zl_itx_list);
917b19a79ecSperrin 		for (; itx != NULL; itx = list_next(&zilog->zl_itx_list, itx)) {
918b19a79ecSperrin 			if (foid == 0) /* push all foids? */
919b19a79ecSperrin 				break;
920b19a79ecSperrin 			switch (itx->itx_lr.lrc_txtype) {
921b19a79ecSperrin 			case TX_SETATTR:
922b19a79ecSperrin 			case TX_WRITE:
923b19a79ecSperrin 			case TX_TRUNCATE:
924b19a79ecSperrin 			case TX_ACL:
925b19a79ecSperrin 				/* lr_foid is same offset for these records */
926b19a79ecSperrin 				if (((lr_write_t *)&itx->itx_lr)->lr_foid
927b19a79ecSperrin 				    != foid) {
928b19a79ecSperrin 					continue; /* skip this record */
929b19a79ecSperrin 				}
930b19a79ecSperrin 			}
931b19a79ecSperrin 			break;
932b19a79ecSperrin 		}
933fa9e4066Sahrens 		if (itx == NULL)
934fa9e4066Sahrens 			break;
935fa9e4066Sahrens 
936fa9e4066Sahrens 		reclen = itx->itx_lr.lrc_reclen;
937fa9e4066Sahrens 		if ((itx->itx_lr.lrc_seq > seq) &&
938b19a79ecSperrin 		    ((lwb == NULL) || (lwb->lwb_nused == 0) ||
939b19a79ecSperrin 		    (lwb->lwb_nused + reclen > ZIL_BLK_DATA_SZ(lwb))))
940fa9e4066Sahrens 			break;
941fa9e4066Sahrens 
942b19a79ecSperrin 		/*
943b19a79ecSperrin 		 * Save the next pointer.  Even though we soon drop
944b19a79ecSperrin 		 * zl_lock all threads that may change the list
945b19a79ecSperrin 		 * (another writer or zil_itx_clean) can't do so until
946b19a79ecSperrin 		 * they have zl_writer.
947b19a79ecSperrin 		 */
948b19a79ecSperrin 		itx_next = list_next(&zilog->zl_itx_list, itx);
949fa9e4066Sahrens 		list_remove(&zilog->zl_itx_list, itx);
950fa9e4066Sahrens 		txg = itx->itx_lr.lrc_txg;
951fa9e4066Sahrens 		ASSERT(txg);
952fa9e4066Sahrens 
953fa9e4066Sahrens 		mutex_exit(&zilog->zl_lock);
954fa9e4066Sahrens 		if (txg > spa_last_synced_txg(spa) ||
955fa9e4066Sahrens 		    txg > spa_freeze_txg(spa))
956fa9e4066Sahrens 			lwb = zil_lwb_commit(zilog, itx, lwb);
957fa9e4066Sahrens 		kmem_free(itx, offsetof(itx_t, itx_lr)
958fa9e4066Sahrens 		    + itx->itx_lr.lrc_reclen);
959fa9e4066Sahrens 		mutex_enter(&zilog->zl_lock);
960fa9e4066Sahrens 		zilog->zl_itx_list_sz -= reclen;
961fa9e4066Sahrens 	}
962b19a79ecSperrin 	DTRACE_PROBE1(zil__cw2, zilog_t *, zilog);
963fa9e4066Sahrens 	mutex_exit(&zilog->zl_lock);
964fa9e4066Sahrens 
965fa9e4066Sahrens 	/* write the last block out */
966fa9e4066Sahrens 	if (lwb != NULL && lwb->lwb_nused != 0)
967fa9e4066Sahrens 		lwb = zil_lwb_write_start(zilog, lwb);
968fa9e4066Sahrens 
96922ac5be4Sperrin 	zilog->zl_prev_used = zilog->zl_cur_used;
97022ac5be4Sperrin 	zilog->zl_cur_used = 0;
971fa9e4066Sahrens 
972fa9e4066Sahrens 	/*
973b19a79ecSperrin 	 * Wait if necessary for the log blocks to be on stable storage.
974fa9e4066Sahrens 	 */
975b19a79ecSperrin 	mutex_enter(&zilog->zl_lock);
976b19a79ecSperrin 	if (zilog->zl_root_zio) {
977b19a79ecSperrin 		mutex_exit(&zilog->zl_lock);
978b19a79ecSperrin 		DTRACE_PROBE1(zil__cw3, zilog_t *, zilog);
979b19a79ecSperrin 		(void) zio_wait(zilog->zl_root_zio);
980b19a79ecSperrin 		DTRACE_PROBE1(zil__cw4, zilog_t *, zilog);
981b19a79ecSperrin 		mutex_enter(&zilog->zl_lock);
982b19a79ecSperrin 		zil_flush_vdevs(zilog);
983fa9e4066Sahrens 	}
98422ac5be4Sperrin 
985fa9e4066Sahrens 	if (zilog->zl_log_error || lwb == NULL) {
986fa9e4066Sahrens 		zilog->zl_log_error = 0;
987fa9e4066Sahrens 		mutex_exit(&zilog->zl_lock);
988fa9e4066Sahrens 		txg_wait_synced(zilog->zl_dmu_pool, 0);
989fa9e4066Sahrens 		mutex_enter(&zilog->zl_lock);
990fa9e4066Sahrens 	}
99122ac5be4Sperrin 	/* wake up others waiting to start a write */
99222ac5be4Sperrin 	zilog->zl_writer = B_FALSE;
993b19a79ecSperrin 	cv_broadcast(&zilog->zl_cv_writer);
994fa9e4066Sahrens 	mutex_exit(&zilog->zl_lock);
995b19a79ecSperrin }
996b19a79ecSperrin 
997b19a79ecSperrin /*
998b19a79ecSperrin  * Push zfs transactions to stable storage up to the supplied sequence number.
999b19a79ecSperrin  * If foid is 0 push out all transactions, otherwise push only those
1000b19a79ecSperrin  * for that file or might have been used to create that file.
1001b19a79ecSperrin  */
1002b19a79ecSperrin void
1003b19a79ecSperrin zil_commit(zilog_t *zilog, uint64_t seq, uint64_t foid)
1004b19a79ecSperrin {
1005b19a79ecSperrin 	if (zilog == NULL || seq == 0)
1006b19a79ecSperrin 		return;
1007b19a79ecSperrin 
1008b19a79ecSperrin 	mutex_enter(&zilog->zl_lock);
1009b19a79ecSperrin 
1010b19a79ecSperrin 	seq = MIN(seq, zilog->zl_itx_seq);	/* cap seq at largest itx seq */
1011b19a79ecSperrin 
1012b19a79ecSperrin 	while (zilog->zl_writer)
1013b19a79ecSperrin 		cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
1014b19a79ecSperrin 	zil_commit_writer(zilog, seq, foid); /* drops zl_lock */
1015fa9e4066Sahrens }
1016fa9e4066Sahrens 
1017fa9e4066Sahrens /*
1018fa9e4066Sahrens  * Called in syncing context to free committed log blocks and update log header.
1019fa9e4066Sahrens  */
1020fa9e4066Sahrens void
1021fa9e4066Sahrens zil_sync(zilog_t *zilog, dmu_tx_t *tx)
1022fa9e4066Sahrens {
1023d80c45e0Sbonwick 	zil_header_t *zh = zil_header_in_syncing_context(zilog);
1024fa9e4066Sahrens 	uint64_t txg = dmu_tx_get_txg(tx);
1025fa9e4066Sahrens 	spa_t *spa = zilog->zl_spa;
1026fa9e4066Sahrens 	lwb_t *lwb;
1027fa9e4066Sahrens 
1028d80c45e0Sbonwick 	mutex_enter(&zilog->zl_lock);
1029d80c45e0Sbonwick 
1030fa9e4066Sahrens 	ASSERT(zilog->zl_stop_sync == 0);
1031fa9e4066Sahrens 
1032d80c45e0Sbonwick 	zh->zh_replay_seq = zilog->zl_replay_seq[txg & TXG_MASK];
1033fa9e4066Sahrens 
1034fa9e4066Sahrens 	if (zilog->zl_destroy_txg == txg) {
1035d80c45e0Sbonwick 		blkptr_t blk = zh->zh_log;
1036d80c45e0Sbonwick 
1037d80c45e0Sbonwick 		ASSERT(list_head(&zilog->zl_lwb_list) == NULL);
1038d80c45e0Sbonwick 		ASSERT(spa_sync_pass(spa) == 1);
1039d80c45e0Sbonwick 
1040d80c45e0Sbonwick 		bzero(zh, sizeof (zil_header_t));
1041fa9e4066Sahrens 		bzero(zilog->zl_replay_seq, sizeof (zilog->zl_replay_seq));
1042d80c45e0Sbonwick 
1043d80c45e0Sbonwick 		if (zilog->zl_keep_first) {
1044d80c45e0Sbonwick 			/*
1045d80c45e0Sbonwick 			 * If this block was part of log chain that couldn't
1046d80c45e0Sbonwick 			 * be claimed because a device was missing during
1047d80c45e0Sbonwick 			 * zil_claim(), but that device later returns,
1048d80c45e0Sbonwick 			 * then this block could erroneously appear valid.
1049d80c45e0Sbonwick 			 * To guard against this, assign a new GUID to the new
1050d80c45e0Sbonwick 			 * log chain so it doesn't matter what blk points to.
1051d80c45e0Sbonwick 			 */
1052d80c45e0Sbonwick 			zil_init_log_chain(zilog, &blk);
1053d80c45e0Sbonwick 			zh->zh_log = blk;
1054d80c45e0Sbonwick 		}
1055fa9e4066Sahrens 	}
1056fa9e4066Sahrens 
1057fa9e4066Sahrens 	for (;;) {
1058fa9e4066Sahrens 		lwb = list_head(&zilog->zl_lwb_list);
1059fa9e4066Sahrens 		if (lwb == NULL) {
1060fa9e4066Sahrens 			mutex_exit(&zilog->zl_lock);
1061fa9e4066Sahrens 			return;
1062fa9e4066Sahrens 		}
1063b19a79ecSperrin 		zh->zh_log = lwb->lwb_blk;
1064fa9e4066Sahrens 		if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg)
1065fa9e4066Sahrens 			break;
1066fa9e4066Sahrens 		list_remove(&zilog->zl_lwb_list, lwb);
1067fa9e4066Sahrens 		zio_free_blk(spa, &lwb->lwb_blk, txg);
1068fa9e4066Sahrens 		kmem_cache_free(zil_lwb_cache, lwb);
1069fa9e4066Sahrens 	}
1070fa9e4066Sahrens 	mutex_exit(&zilog->zl_lock);
1071fa9e4066Sahrens }
1072fa9e4066Sahrens 
1073fa9e4066Sahrens void
1074fa9e4066Sahrens zil_init(void)
1075fa9e4066Sahrens {
1076fa9e4066Sahrens 	zil_lwb_cache = kmem_cache_create("zil_lwb_cache",
10775ad82045Snd 	    sizeof (struct lwb), 0, NULL, NULL, NULL, NULL, NULL, 0);
1078fa9e4066Sahrens }
1079fa9e4066Sahrens 
1080fa9e4066Sahrens void
1081fa9e4066Sahrens zil_fini(void)
1082fa9e4066Sahrens {
1083fa9e4066Sahrens 	kmem_cache_destroy(zil_lwb_cache);
1084fa9e4066Sahrens }
1085fa9e4066Sahrens 
1086fa9e4066Sahrens zilog_t *
1087fa9e4066Sahrens zil_alloc(objset_t *os, zil_header_t *zh_phys)
1088fa9e4066Sahrens {
1089fa9e4066Sahrens 	zilog_t *zilog;
1090fa9e4066Sahrens 
1091fa9e4066Sahrens 	zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP);
1092fa9e4066Sahrens 
1093fa9e4066Sahrens 	zilog->zl_header = zh_phys;
1094fa9e4066Sahrens 	zilog->zl_os = os;
1095fa9e4066Sahrens 	zilog->zl_spa = dmu_objset_spa(os);
1096fa9e4066Sahrens 	zilog->zl_dmu_pool = dmu_objset_pool(os);
1097d80c45e0Sbonwick 	zilog->zl_destroy_txg = TXG_INITIAL - 1;
1098fa9e4066Sahrens 
10995ad82045Snd 	mutex_init(&zilog->zl_lock, NULL, MUTEX_DEFAULT, NULL);
11005ad82045Snd 
1101fa9e4066Sahrens 	list_create(&zilog->zl_itx_list, sizeof (itx_t),
1102fa9e4066Sahrens 	    offsetof(itx_t, itx_node));
1103fa9e4066Sahrens 
1104fa9e4066Sahrens 	list_create(&zilog->zl_lwb_list, sizeof (lwb_t),
1105fa9e4066Sahrens 	    offsetof(lwb_t, lwb_node));
1106fa9e4066Sahrens 
1107fa9e4066Sahrens 	list_create(&zilog->zl_vdev_list, sizeof (zil_vdev_t),
1108fa9e4066Sahrens 	    offsetof(zil_vdev_t, vdev_seq_node));
1109fa9e4066Sahrens 
1110fa9e4066Sahrens 	return (zilog);
1111fa9e4066Sahrens }
1112fa9e4066Sahrens 
1113fa9e4066Sahrens void
1114fa9e4066Sahrens zil_free(zilog_t *zilog)
1115fa9e4066Sahrens {
1116fa9e4066Sahrens 	lwb_t *lwb;
1117fa9e4066Sahrens 	zil_vdev_t *zv;
1118fa9e4066Sahrens 
1119fa9e4066Sahrens 	zilog->zl_stop_sync = 1;
1120fa9e4066Sahrens 
1121fa9e4066Sahrens 	while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
1122fa9e4066Sahrens 		list_remove(&zilog->zl_lwb_list, lwb);
1123fa9e4066Sahrens 		if (lwb->lwb_buf != NULL)
1124fa9e4066Sahrens 			zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
1125fa9e4066Sahrens 		kmem_cache_free(zil_lwb_cache, lwb);
1126fa9e4066Sahrens 	}
1127fa9e4066Sahrens 	list_destroy(&zilog->zl_lwb_list);
1128fa9e4066Sahrens 
1129fa9e4066Sahrens 	while ((zv = list_head(&zilog->zl_vdev_list)) != NULL) {
1130fa9e4066Sahrens 		list_remove(&zilog->zl_vdev_list, zv);
1131fa9e4066Sahrens 		kmem_free(zv, sizeof (zil_vdev_t));
1132fa9e4066Sahrens 	}
1133fa9e4066Sahrens 	list_destroy(&zilog->zl_vdev_list);
1134fa9e4066Sahrens 
1135fa9e4066Sahrens 	ASSERT(list_head(&zilog->zl_itx_list) == NULL);
1136fa9e4066Sahrens 	list_destroy(&zilog->zl_itx_list);
11375ad82045Snd 	mutex_destroy(&zilog->zl_lock);
1138fa9e4066Sahrens 
1139fa9e4066Sahrens 	kmem_free(zilog, sizeof (zilog_t));
1140fa9e4066Sahrens }
1141fa9e4066Sahrens 
114213f5297eSperrin /*
1143436b2950Sperrin  * return true if the initial log block is not valid
114413f5297eSperrin  */
114513f5297eSperrin static int
114613f5297eSperrin zil_empty(zilog_t *zilog)
114713f5297eSperrin {
1148d80c45e0Sbonwick 	const zil_header_t *zh = zilog->zl_header;
1149d80c45e0Sbonwick 	arc_buf_t *abuf = NULL;
115013f5297eSperrin 
1151d80c45e0Sbonwick 	if (BP_IS_HOLE(&zh->zh_log))
115213f5297eSperrin 		return (1);
115313f5297eSperrin 
1154d80c45e0Sbonwick 	if (zil_read_log_block(zilog, &zh->zh_log, &abuf) != 0)
1155d80c45e0Sbonwick 		return (1);
1156d80c45e0Sbonwick 
1157d80c45e0Sbonwick 	VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
1158d80c45e0Sbonwick 	return (0);
115913f5297eSperrin }
116013f5297eSperrin 
1161fa9e4066Sahrens /*
1162fa9e4066Sahrens  * Open an intent log.
1163fa9e4066Sahrens  */
1164fa9e4066Sahrens zilog_t *
1165fa9e4066Sahrens zil_open(objset_t *os, zil_get_data_t *get_data)
1166fa9e4066Sahrens {
1167fa9e4066Sahrens 	zilog_t *zilog = dmu_objset_zil(os);
1168fa9e4066Sahrens 
1169fa9e4066Sahrens 	zilog->zl_get_data = get_data;
1170fa9e4066Sahrens 	zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri,
1171fa9e4066Sahrens 	    2, 2, TASKQ_PREPOPULATE);
1172fa9e4066Sahrens 
1173fa9e4066Sahrens 	return (zilog);
1174fa9e4066Sahrens }
1175fa9e4066Sahrens 
1176fa9e4066Sahrens /*
1177fa9e4066Sahrens  * Close an intent log.
1178fa9e4066Sahrens  */
1179fa9e4066Sahrens void
1180fa9e4066Sahrens zil_close(zilog_t *zilog)
1181fa9e4066Sahrens {
1182d80c45e0Sbonwick 	/*
1183d80c45e0Sbonwick 	 * If the log isn't already committed, mark the objset dirty
1184d80c45e0Sbonwick 	 * (so zil_sync() will be called) and wait for that txg to sync.
1185d80c45e0Sbonwick 	 */
1186d80c45e0Sbonwick 	if (!zil_is_committed(zilog)) {
1187d80c45e0Sbonwick 		uint64_t txg;
1188d80c45e0Sbonwick 		dmu_tx_t *tx = dmu_tx_create(zilog->zl_os);
1189d80c45e0Sbonwick 		(void) dmu_tx_assign(tx, TXG_WAIT);
1190d80c45e0Sbonwick 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
1191d80c45e0Sbonwick 		txg = dmu_tx_get_txg(tx);
1192d80c45e0Sbonwick 		dmu_tx_commit(tx);
1193d80c45e0Sbonwick 		txg_wait_synced(zilog->zl_dmu_pool, txg);
1194d80c45e0Sbonwick 	}
1195d80c45e0Sbonwick 
1196fa9e4066Sahrens 	taskq_destroy(zilog->zl_clean_taskq);
1197fa9e4066Sahrens 	zilog->zl_clean_taskq = NULL;
1198fa9e4066Sahrens 	zilog->zl_get_data = NULL;
1199fa9e4066Sahrens 
1200fa9e4066Sahrens 	zil_itx_clean(zilog);
1201fa9e4066Sahrens 	ASSERT(list_head(&zilog->zl_itx_list) == NULL);
1202fa9e4066Sahrens }
1203fa9e4066Sahrens 
1204fa9e4066Sahrens /*
1205fa9e4066Sahrens  * Suspend an intent log.  While in suspended mode, we still honor
1206fa9e4066Sahrens  * synchronous semantics, but we rely on txg_wait_synced() to do it.
1207fa9e4066Sahrens  * We suspend the log briefly when taking a snapshot so that the snapshot
1208fa9e4066Sahrens  * contains all the data it's supposed to, and has an empty intent log.
1209fa9e4066Sahrens  */
1210fa9e4066Sahrens int
1211fa9e4066Sahrens zil_suspend(zilog_t *zilog)
1212fa9e4066Sahrens {
1213d80c45e0Sbonwick 	const zil_header_t *zh = zilog->zl_header;
1214fa9e4066Sahrens 
1215fa9e4066Sahrens 	mutex_enter(&zilog->zl_lock);
1216d80c45e0Sbonwick 	if (zh->zh_claim_txg != 0) {		/* unplayed log */
1217fa9e4066Sahrens 		mutex_exit(&zilog->zl_lock);
1218fa9e4066Sahrens 		return (EBUSY);
1219fa9e4066Sahrens 	}
1220d80c45e0Sbonwick 	if (zilog->zl_suspend++ != 0) {
1221d80c45e0Sbonwick 		/*
1222d80c45e0Sbonwick 		 * Someone else already began a suspend.
1223d80c45e0Sbonwick 		 * Just wait for them to finish.
1224d80c45e0Sbonwick 		 */
1225d80c45e0Sbonwick 		while (zilog->zl_suspending)
1226d80c45e0Sbonwick 			cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock);
1227d80c45e0Sbonwick 		ASSERT(BP_IS_HOLE(&zh->zh_log));
1228d80c45e0Sbonwick 		mutex_exit(&zilog->zl_lock);
1229d80c45e0Sbonwick 		return (0);
1230d80c45e0Sbonwick 	}
1231d80c45e0Sbonwick 	zilog->zl_suspending = B_TRUE;
1232fa9e4066Sahrens 	mutex_exit(&zilog->zl_lock);
1233fa9e4066Sahrens 
1234b19a79ecSperrin 	zil_commit(zilog, UINT64_MAX, 0);
1235fa9e4066Sahrens 
1236b19a79ecSperrin 	/*
1237b19a79ecSperrin 	 * Wait for any in-flight log writes to complete.
1238b19a79ecSperrin 	 */
1239fa9e4066Sahrens 	mutex_enter(&zilog->zl_lock);
1240b19a79ecSperrin 	while (zilog->zl_writer)
1241b19a79ecSperrin 		cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
1242fa9e4066Sahrens 	mutex_exit(&zilog->zl_lock);
1243fa9e4066Sahrens 
1244d80c45e0Sbonwick 	zil_destroy(zilog, B_FALSE);
1245d80c45e0Sbonwick 
1246d80c45e0Sbonwick 	mutex_enter(&zilog->zl_lock);
1247d80c45e0Sbonwick 	ASSERT(BP_IS_HOLE(&zh->zh_log));
1248d80c45e0Sbonwick 	zilog->zl_suspending = B_FALSE;
1249d80c45e0Sbonwick 	cv_broadcast(&zilog->zl_cv_suspend);
1250d80c45e0Sbonwick 	mutex_exit(&zilog->zl_lock);
1251fa9e4066Sahrens 
1252fa9e4066Sahrens 	return (0);
1253fa9e4066Sahrens }
1254fa9e4066Sahrens 
1255fa9e4066Sahrens void
1256fa9e4066Sahrens zil_resume(zilog_t *zilog)
1257fa9e4066Sahrens {
1258fa9e4066Sahrens 	mutex_enter(&zilog->zl_lock);
1259fa9e4066Sahrens 	ASSERT(zilog->zl_suspend != 0);
1260fa9e4066Sahrens 	zilog->zl_suspend--;
1261fa9e4066Sahrens 	mutex_exit(&zilog->zl_lock);
1262fa9e4066Sahrens }
1263fa9e4066Sahrens 
1264fa9e4066Sahrens typedef struct zil_replay_arg {
1265fa9e4066Sahrens 	objset_t	*zr_os;
1266fa9e4066Sahrens 	zil_replay_func_t **zr_replay;
1267fa9e4066Sahrens 	void		*zr_arg;
1268fa9e4066Sahrens 	void		(*zr_rm_sync)(void *arg);
1269fa9e4066Sahrens 	uint64_t	*zr_txgp;
1270fa9e4066Sahrens 	boolean_t	zr_byteswap;
1271fa9e4066Sahrens 	char		*zr_lrbuf;
1272fa9e4066Sahrens } zil_replay_arg_t;
1273fa9e4066Sahrens 
1274fa9e4066Sahrens static void
1275fa9e4066Sahrens zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg)
1276fa9e4066Sahrens {
1277fa9e4066Sahrens 	zil_replay_arg_t *zr = zra;
1278d80c45e0Sbonwick 	const zil_header_t *zh = zilog->zl_header;
1279fa9e4066Sahrens 	uint64_t reclen = lr->lrc_reclen;
1280fa9e4066Sahrens 	uint64_t txtype = lr->lrc_txtype;
1281fa9e4066Sahrens 	int pass, error;
1282fa9e4066Sahrens 
1283fa9e4066Sahrens 	if (zilog->zl_stop_replay)
1284fa9e4066Sahrens 		return;
1285fa9e4066Sahrens 
1286fa9e4066Sahrens 	if (lr->lrc_txg < claim_txg)		/* already committed */
1287fa9e4066Sahrens 		return;
1288fa9e4066Sahrens 
1289fa9e4066Sahrens 	if (lr->lrc_seq <= zh->zh_replay_seq)	/* already replayed */
1290fa9e4066Sahrens 		return;
1291fa9e4066Sahrens 
1292fa9e4066Sahrens 	/*
1293fa9e4066Sahrens 	 * Make a copy of the data so we can revise and extend it.
1294fa9e4066Sahrens 	 */
1295fa9e4066Sahrens 	bcopy(lr, zr->zr_lrbuf, reclen);
1296fa9e4066Sahrens 
1297fa9e4066Sahrens 	/*
1298fa9e4066Sahrens 	 * The log block containing this lr may have been byteswapped
1299fa9e4066Sahrens 	 * so that we can easily examine common fields like lrc_txtype.
1300fa9e4066Sahrens 	 * However, the log is a mix of different data types, and only the
1301fa9e4066Sahrens 	 * replay vectors know how to byteswap their records.  Therefore, if
1302fa9e4066Sahrens 	 * the lr was byteswapped, undo it before invoking the replay vector.
1303fa9e4066Sahrens 	 */
1304fa9e4066Sahrens 	if (zr->zr_byteswap)
1305fa9e4066Sahrens 		byteswap_uint64_array(zr->zr_lrbuf, reclen);
1306fa9e4066Sahrens 
1307fa9e4066Sahrens 	/*
1308fa9e4066Sahrens 	 * If this is a TX_WRITE with a blkptr, suck in the data.
1309fa9e4066Sahrens 	 */
1310fa9e4066Sahrens 	if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) {
1311fa9e4066Sahrens 		lr_write_t *lrw = (lr_write_t *)lr;
1312fa9e4066Sahrens 		blkptr_t *wbp = &lrw->lr_blkptr;
1313fa9e4066Sahrens 		uint64_t wlen = lrw->lr_length;
1314fa9e4066Sahrens 		char *wbuf = zr->zr_lrbuf + reclen;
1315fa9e4066Sahrens 
1316fa9e4066Sahrens 		if (BP_IS_HOLE(wbp)) {	/* compressed to a hole */
1317fa9e4066Sahrens 			bzero(wbuf, wlen);
1318fa9e4066Sahrens 		} else {
1319fa9e4066Sahrens 			/*
1320fa9e4066Sahrens 			 * A subsequent write may have overwritten this block,
1321fa9e4066Sahrens 			 * in which case wbp may have been been freed and
1322fa9e4066Sahrens 			 * reallocated, and our read of wbp may fail with a
1323fa9e4066Sahrens 			 * checksum error.  We can safely ignore this because
1324fa9e4066Sahrens 			 * the later write will provide the correct data.
1325fa9e4066Sahrens 			 */
1326ea8dc4b6Seschrock 			zbookmark_t zb;
1327ea8dc4b6Seschrock 
1328ea8dc4b6Seschrock 			zb.zb_objset = dmu_objset_id(zilog->zl_os);
1329ea8dc4b6Seschrock 			zb.zb_object = lrw->lr_foid;
1330ea8dc4b6Seschrock 			zb.zb_level = -1;
1331ea8dc4b6Seschrock 			zb.zb_blkid = lrw->lr_offset / BP_GET_LSIZE(wbp);
1332ea8dc4b6Seschrock 
1333fa9e4066Sahrens 			(void) zio_wait(zio_read(NULL, zilog->zl_spa,
1334fa9e4066Sahrens 			    wbp, wbuf, BP_GET_LSIZE(wbp), NULL, NULL,
1335fa9e4066Sahrens 			    ZIO_PRIORITY_SYNC_READ,
1336ea8dc4b6Seschrock 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &zb));
1337fa9e4066Sahrens 			(void) memmove(wbuf, wbuf + lrw->lr_blkoff, wlen);
1338fa9e4066Sahrens 		}
1339fa9e4066Sahrens 	}
1340fa9e4066Sahrens 
1341fa9e4066Sahrens 	/*
1342fa9e4066Sahrens 	 * We must now do two things atomically: replay this log record,
1343fa9e4066Sahrens 	 * and update the log header to reflect the fact that we did so.
1344fa9e4066Sahrens 	 * We use the DMU's ability to assign into a specific txg to do this.
1345fa9e4066Sahrens 	 */
1346fa9e4066Sahrens 	for (pass = 1; /* CONSTANTCONDITION */; pass++) {
1347fa9e4066Sahrens 		uint64_t replay_txg;
1348fa9e4066Sahrens 		dmu_tx_t *replay_tx;
1349fa9e4066Sahrens 
1350fa9e4066Sahrens 		replay_tx = dmu_tx_create(zr->zr_os);
1351fa9e4066Sahrens 		error = dmu_tx_assign(replay_tx, TXG_WAIT);
1352fa9e4066Sahrens 		if (error) {
1353fa9e4066Sahrens 			dmu_tx_abort(replay_tx);
1354fa9e4066Sahrens 			break;
1355fa9e4066Sahrens 		}
1356fa9e4066Sahrens 
1357fa9e4066Sahrens 		replay_txg = dmu_tx_get_txg(replay_tx);
1358fa9e4066Sahrens 
1359fa9e4066Sahrens 		if (txtype == 0 || txtype >= TX_MAX_TYPE) {
1360fa9e4066Sahrens 			error = EINVAL;
1361fa9e4066Sahrens 		} else {
1362fa9e4066Sahrens 			/*
1363fa9e4066Sahrens 			 * On the first pass, arrange for the replay vector
1364fa9e4066Sahrens 			 * to fail its dmu_tx_assign().  That's the only way
1365fa9e4066Sahrens 			 * to ensure that those code paths remain well tested.
1366fa9e4066Sahrens 			 */
1367fa9e4066Sahrens 			*zr->zr_txgp = replay_txg - (pass == 1);
1368fa9e4066Sahrens 			error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lrbuf,
1369fa9e4066Sahrens 			    zr->zr_byteswap);
1370fa9e4066Sahrens 			*zr->zr_txgp = TXG_NOWAIT;
1371fa9e4066Sahrens 		}
1372fa9e4066Sahrens 
1373fa9e4066Sahrens 		if (error == 0) {
1374fa9e4066Sahrens 			dsl_dataset_dirty(dmu_objset_ds(zr->zr_os), replay_tx);
1375fa9e4066Sahrens 			zilog->zl_replay_seq[replay_txg & TXG_MASK] =
1376fa9e4066Sahrens 			    lr->lrc_seq;
1377fa9e4066Sahrens 		}
1378fa9e4066Sahrens 
1379fa9e4066Sahrens 		dmu_tx_commit(replay_tx);
1380fa9e4066Sahrens 
1381fa9e4066Sahrens 		if (error != ERESTART)
1382fa9e4066Sahrens 			break;
1383fa9e4066Sahrens 
1384fa9e4066Sahrens 		if (pass != 1)
1385fa9e4066Sahrens 			txg_wait_open(spa_get_dsl(zilog->zl_spa),
1386fa9e4066Sahrens 			    replay_txg + 1);
1387fa9e4066Sahrens 
1388fa9e4066Sahrens 		dprintf("pass %d, retrying\n", pass);
1389fa9e4066Sahrens 	}
1390fa9e4066Sahrens 
1391fa9e4066Sahrens 	if (error) {
1392fa9e4066Sahrens 		char *name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1393fa9e4066Sahrens 		dmu_objset_name(zr->zr_os, name);
1394fa9e4066Sahrens 		cmn_err(CE_WARN, "ZFS replay transaction error %d, "
1395fa9e4066Sahrens 		    "dataset %s, seq 0x%llx, txtype %llu\n",
1396fa9e4066Sahrens 		    error, name,
1397fa9e4066Sahrens 		    (u_longlong_t)lr->lrc_seq, (u_longlong_t)txtype);
1398fa9e4066Sahrens 		zilog->zl_stop_replay = 1;
1399fa9e4066Sahrens 		kmem_free(name, MAXNAMELEN);
1400fa9e4066Sahrens 	}
1401fa9e4066Sahrens 
1402fa9e4066Sahrens 	/*
1403fa9e4066Sahrens 	 * The DMU's dnode layer doesn't see removes until the txg commits,
1404fa9e4066Sahrens 	 * so a subsequent claim can spuriously fail with EEXIST.
1405fa9e4066Sahrens 	 * To prevent this, if we might have removed an object,
1406fa9e4066Sahrens 	 * wait for the delete thread to delete it, and then
1407fa9e4066Sahrens 	 * wait for the transaction group to sync.
1408fa9e4066Sahrens 	 */
1409fa9e4066Sahrens 	if (txtype == TX_REMOVE || txtype == TX_RMDIR || txtype == TX_RENAME) {
1410fa9e4066Sahrens 		if (zr->zr_rm_sync != NULL)
1411fa9e4066Sahrens 			zr->zr_rm_sync(zr->zr_arg);
1412fa9e4066Sahrens 		txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0);
1413fa9e4066Sahrens 	}
1414fa9e4066Sahrens }
1415fa9e4066Sahrens 
1416fa9e4066Sahrens /*
141713f5297eSperrin  * If this dataset has a non-empty intent log, replay it and destroy it.
1418fa9e4066Sahrens  */
1419fa9e4066Sahrens void
1420fa9e4066Sahrens zil_replay(objset_t *os, void *arg, uint64_t *txgp,
1421fa9e4066Sahrens 	zil_replay_func_t *replay_func[TX_MAX_TYPE], void (*rm_sync)(void *arg))
1422fa9e4066Sahrens {
1423fa9e4066Sahrens 	zilog_t *zilog = dmu_objset_zil(os);
1424d80c45e0Sbonwick 	const zil_header_t *zh = zilog->zl_header;
1425d80c45e0Sbonwick 	zil_replay_arg_t zr;
142613f5297eSperrin 
142713f5297eSperrin 	if (zil_empty(zilog)) {
1428d80c45e0Sbonwick 		zil_destroy(zilog, B_TRUE);
142913f5297eSperrin 		return;
143013f5297eSperrin 	}
1431fa9e4066Sahrens 
1432fa9e4066Sahrens 	zr.zr_os = os;
1433fa9e4066Sahrens 	zr.zr_replay = replay_func;
1434fa9e4066Sahrens 	zr.zr_arg = arg;
1435fa9e4066Sahrens 	zr.zr_rm_sync = rm_sync;
1436fa9e4066Sahrens 	zr.zr_txgp = txgp;
1437d80c45e0Sbonwick 	zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log);
1438fa9e4066Sahrens 	zr.zr_lrbuf = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP);
1439fa9e4066Sahrens 
1440fa9e4066Sahrens 	/*
1441fa9e4066Sahrens 	 * Wait for in-progress removes to sync before starting replay.
1442fa9e4066Sahrens 	 */
1443fa9e4066Sahrens 	if (rm_sync != NULL)
1444fa9e4066Sahrens 		rm_sync(arg);
1445fa9e4066Sahrens 	txg_wait_synced(zilog->zl_dmu_pool, 0);
1446fa9e4066Sahrens 
1447fa9e4066Sahrens 	zilog->zl_stop_replay = 0;
1448d80c45e0Sbonwick 	(void) zil_parse(zilog, NULL, zil_replay_log_record, &zr,
1449d80c45e0Sbonwick 	    zh->zh_claim_txg);
1450fa9e4066Sahrens 	kmem_free(zr.zr_lrbuf, 2 * SPA_MAXBLOCKSIZE);
1451fa9e4066Sahrens 
1452d80c45e0Sbonwick 	zil_destroy(zilog, B_FALSE);
1453fa9e4066Sahrens }
1454436b2950Sperrin 
1455436b2950Sperrin /*
1456436b2950Sperrin  * Report whether all transactions are committed
1457436b2950Sperrin  */
1458436b2950Sperrin int
1459436b2950Sperrin zil_is_committed(zilog_t *zilog)
1460436b2950Sperrin {
1461436b2950Sperrin 	lwb_t *lwb;
1462b19a79ecSperrin 	int ret;
1463b19a79ecSperrin 
1464b19a79ecSperrin 	mutex_enter(&zilog->zl_lock);
1465b19a79ecSperrin 	while (zilog->zl_writer)
1466b19a79ecSperrin 		cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
1467b19a79ecSperrin 
1468b19a79ecSperrin 	/* recent unpushed intent log transactions? */
1469b19a79ecSperrin 	if (!list_is_empty(&zilog->zl_itx_list)) {
1470b19a79ecSperrin 		ret = B_FALSE;
1471b19a79ecSperrin 		goto out;
1472b19a79ecSperrin 	}
1473436b2950Sperrin 
1474b19a79ecSperrin 	/* intent log never used? */
1475b19a79ecSperrin 	lwb = list_head(&zilog->zl_lwb_list);
1476b19a79ecSperrin 	if (lwb == NULL) {
1477b19a79ecSperrin 		ret = B_TRUE;
1478b19a79ecSperrin 		goto out;
1479b19a79ecSperrin 	}
1480436b2950Sperrin 
1481436b2950Sperrin 	/*
1482b19a79ecSperrin 	 * more than 1 log buffer means zil_sync() hasn't yet freed
1483b19a79ecSperrin 	 * entries after a txg has committed
1484436b2950Sperrin 	 */
1485b19a79ecSperrin 	if (list_next(&zilog->zl_lwb_list, lwb)) {
1486b19a79ecSperrin 		ret = B_FALSE;
1487b19a79ecSperrin 		goto out;
1488b19a79ecSperrin 	}
1489b19a79ecSperrin 
1490436b2950Sperrin 	ASSERT(zil_empty(zilog));
1491b19a79ecSperrin 	ret = B_TRUE;
1492b19a79ecSperrin out:
1493b19a79ecSperrin 	cv_broadcast(&zilog->zl_cv_writer);
1494b19a79ecSperrin 	mutex_exit(&zilog->zl_lock);
1495b19a79ecSperrin 	return (ret);
1496436b2950Sperrin }
1497