xref: /onnv-gate/usr/src/uts/common/fs/zfs/zil.c (revision 2986:c782fcf7a319)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51472Sperrin  * Common Development and Distribution License (the "License").
61472Sperrin  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
221362Sperrin  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
27789Sahrens 
28789Sahrens #include <sys/zfs_context.h>
29789Sahrens #include <sys/spa.h>
30789Sahrens #include <sys/dmu.h>
31789Sahrens #include <sys/zap.h>
32789Sahrens #include <sys/arc.h>
33789Sahrens #include <sys/stat.h>
34789Sahrens #include <sys/resource.h>
35789Sahrens #include <sys/zil.h>
36789Sahrens #include <sys/zil_impl.h>
37789Sahrens #include <sys/dsl_dataset.h>
38789Sahrens #include <sys/vdev.h>
39789Sahrens 
40789Sahrens /*
41789Sahrens  * The zfs intent log (ZIL) saves transaction records of system calls
42789Sahrens  * that change the file system in memory with enough information
43789Sahrens  * to be able to replay them. These are stored in memory until
44789Sahrens  * either the DMU transaction group (txg) commits them to the stable pool
45789Sahrens  * and they can be discarded, or they are flushed to the stable log
46789Sahrens  * (also in the pool) due to a fsync, O_DSYNC or other synchronous
47789Sahrens  * requirement. In the event of a panic or power fail then those log
48789Sahrens  * records (transactions) are replayed.
49789Sahrens  *
50789Sahrens  * There is one ZIL per file system. Its on-disk (pool) format consists
51789Sahrens  * of 3 parts:
52789Sahrens  *
53789Sahrens  * 	- ZIL header
54789Sahrens  * 	- ZIL blocks
55789Sahrens  * 	- ZIL records
56789Sahrens  *
57789Sahrens  * A log record holds a system call transaction. Log blocks can
58789Sahrens  * hold many log records and the blocks are chained together.
59789Sahrens  * Each ZIL block contains a block pointer (blkptr_t) to the next
60789Sahrens  * ZIL block in the chain. The ZIL header points to the first
61789Sahrens  * block in the chain. Note there is not a fixed place in the pool
62789Sahrens  * to hold blocks. They are dynamically allocated and freed as
63789Sahrens  * needed from the blocks available. Figure X shows the ZIL structure:
64789Sahrens  */
65789Sahrens 
66789Sahrens /*
67*2986Sek110237  * This global ZIL switch affects all pools
68789Sahrens  */
69789Sahrens int zil_disable = 0;	/* disable intent logging */
70*2986Sek110237 
71*2986Sek110237 /*
72*2986Sek110237  * Tunable parameter for debugging or performance analysis.  Setting
73*2986Sek110237  * zfs_nocacheflush will cause corruption on power loss if a volatile
74*2986Sek110237  * out-of-order write cache is enabled.
75*2986Sek110237  */
76*2986Sek110237 boolean_t zfs_nocacheflush = B_FALSE;
77789Sahrens 
78789Sahrens static kmem_cache_t *zil_lwb_cache;
79789Sahrens 
80789Sahrens static int
81789Sahrens zil_dva_compare(const void *x1, const void *x2)
82789Sahrens {
83789Sahrens 	const dva_t *dva1 = x1;
84789Sahrens 	const dva_t *dva2 = x2;
85789Sahrens 
86789Sahrens 	if (DVA_GET_VDEV(dva1) < DVA_GET_VDEV(dva2))
87789Sahrens 		return (-1);
88789Sahrens 	if (DVA_GET_VDEV(dva1) > DVA_GET_VDEV(dva2))
89789Sahrens 		return (1);
90789Sahrens 
91789Sahrens 	if (DVA_GET_OFFSET(dva1) < DVA_GET_OFFSET(dva2))
92789Sahrens 		return (-1);
93789Sahrens 	if (DVA_GET_OFFSET(dva1) > DVA_GET_OFFSET(dva2))
94789Sahrens 		return (1);
95789Sahrens 
96789Sahrens 	return (0);
97789Sahrens }
98789Sahrens 
99789Sahrens static void
100789Sahrens zil_dva_tree_init(avl_tree_t *t)
101789Sahrens {
102789Sahrens 	avl_create(t, zil_dva_compare, sizeof (zil_dva_node_t),
103789Sahrens 	    offsetof(zil_dva_node_t, zn_node));
104789Sahrens }
105789Sahrens 
106789Sahrens static void
107789Sahrens zil_dva_tree_fini(avl_tree_t *t)
108789Sahrens {
109789Sahrens 	zil_dva_node_t *zn;
110789Sahrens 	void *cookie = NULL;
111789Sahrens 
112789Sahrens 	while ((zn = avl_destroy_nodes(t, &cookie)) != NULL)
113789Sahrens 		kmem_free(zn, sizeof (zil_dva_node_t));
114789Sahrens 
115789Sahrens 	avl_destroy(t);
116789Sahrens }
117789Sahrens 
118789Sahrens static int
119789Sahrens zil_dva_tree_add(avl_tree_t *t, dva_t *dva)
120789Sahrens {
121789Sahrens 	zil_dva_node_t *zn;
122789Sahrens 	avl_index_t where;
123789Sahrens 
124789Sahrens 	if (avl_find(t, dva, &where) != NULL)
125789Sahrens 		return (EEXIST);
126789Sahrens 
127789Sahrens 	zn = kmem_alloc(sizeof (zil_dva_node_t), KM_SLEEP);
128789Sahrens 	zn->zn_dva = *dva;
129789Sahrens 	avl_insert(t, zn, where);
130789Sahrens 
131789Sahrens 	return (0);
132789Sahrens }
133789Sahrens 
1341807Sbonwick static zil_header_t *
1351807Sbonwick zil_header_in_syncing_context(zilog_t *zilog)
1361807Sbonwick {
1371807Sbonwick 	return ((zil_header_t *)zilog->zl_header);
1381807Sbonwick }
1391807Sbonwick 
1401807Sbonwick static void
1411807Sbonwick zil_init_log_chain(zilog_t *zilog, blkptr_t *bp)
1421807Sbonwick {
1431807Sbonwick 	zio_cksum_t *zc = &bp->blk_cksum;
1441807Sbonwick 
1451807Sbonwick 	zc->zc_word[ZIL_ZC_GUID_0] = spa_get_random(-1ULL);
1461807Sbonwick 	zc->zc_word[ZIL_ZC_GUID_1] = spa_get_random(-1ULL);
1471807Sbonwick 	zc->zc_word[ZIL_ZC_OBJSET] = dmu_objset_id(zilog->zl_os);
1481807Sbonwick 	zc->zc_word[ZIL_ZC_SEQ] = 1ULL;
1491807Sbonwick }
1501807Sbonwick 
151789Sahrens /*
152789Sahrens  * Read a log block, make sure it's valid, and byteswap it if necessary.
153789Sahrens  */
154789Sahrens static int
1551807Sbonwick zil_read_log_block(zilog_t *zilog, const blkptr_t *bp, arc_buf_t **abufpp)
156789Sahrens {
1571807Sbonwick 	blkptr_t blk = *bp;
1581544Seschrock 	zbookmark_t zb;
1592391Smaybee 	uint32_t aflags = ARC_WAIT;
160789Sahrens 	int error;
161789Sahrens 
1621807Sbonwick 	zb.zb_objset = bp->blk_cksum.zc_word[ZIL_ZC_OBJSET];
1631544Seschrock 	zb.zb_object = 0;
1641544Seschrock 	zb.zb_level = -1;
1651807Sbonwick 	zb.zb_blkid = bp->blk_cksum.zc_word[ZIL_ZC_SEQ];
1661807Sbonwick 
1671807Sbonwick 	*abufpp = NULL;
1681807Sbonwick 
1691807Sbonwick 	error = arc_read(NULL, zilog->zl_spa, &blk, byteswap_uint64_array,
1701807Sbonwick 	    arc_getbuf_func, abufpp, ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL |
1712391Smaybee 	    ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB, &aflags, &zb);
1721807Sbonwick 
1731807Sbonwick 	if (error == 0) {
1741807Sbonwick 		char *data = (*abufpp)->b_data;
1751807Sbonwick 		uint64_t blksz = BP_GET_LSIZE(bp);
1761807Sbonwick 		zil_trailer_t *ztp = (zil_trailer_t *)(data + blksz) - 1;
1771807Sbonwick 		zio_cksum_t cksum = bp->blk_cksum;
1781544Seschrock 
1791807Sbonwick 		/*
1801807Sbonwick 		 * Sequence numbers should be... sequential.  The checksum
1811807Sbonwick 		 * verifier for the next block should be bp's checksum plus 1.
1821807Sbonwick 		 */
1831807Sbonwick 		cksum.zc_word[ZIL_ZC_SEQ]++;
1841807Sbonwick 
1851807Sbonwick 		if (bcmp(&cksum, &ztp->zit_next_blk.blk_cksum, sizeof (cksum)))
1861807Sbonwick 			error = ESTALE;
1871807Sbonwick 		else if (BP_IS_HOLE(&ztp->zit_next_blk))
1881807Sbonwick 			error = ENOENT;
1891807Sbonwick 		else if (ztp->zit_nused > (blksz - sizeof (zil_trailer_t)))
1901807Sbonwick 			error = EOVERFLOW;
1911807Sbonwick 
1921807Sbonwick 		if (error) {
1931807Sbonwick 			VERIFY(arc_buf_remove_ref(*abufpp, abufpp) == 1);
1941807Sbonwick 			*abufpp = NULL;
1951807Sbonwick 		}
196789Sahrens 	}
197789Sahrens 
1981807Sbonwick 	dprintf("error %d on %llu:%llu\n", error, zb.zb_objset, zb.zb_blkid);
199789Sahrens 
2001807Sbonwick 	return (error);
201789Sahrens }
202789Sahrens 
203789Sahrens /*
204789Sahrens  * Parse the intent log, and call parse_func for each valid record within.
2051807Sbonwick  * Return the highest sequence number.
206789Sahrens  */
2071807Sbonwick uint64_t
208789Sahrens zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func,
209789Sahrens     zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg)
210789Sahrens {
2111807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
2121807Sbonwick 	uint64_t claim_seq = zh->zh_claim_seq;
2131807Sbonwick 	uint64_t seq = 0;
2141807Sbonwick 	uint64_t max_seq = 0;
2151807Sbonwick 	blkptr_t blk = zh->zh_log;
2161807Sbonwick 	arc_buf_t *abuf;
217789Sahrens 	char *lrbuf, *lrp;
218789Sahrens 	zil_trailer_t *ztp;
219789Sahrens 	int reclen, error;
220789Sahrens 
221789Sahrens 	if (BP_IS_HOLE(&blk))
2221807Sbonwick 		return (max_seq);
223789Sahrens 
224789Sahrens 	/*
225789Sahrens 	 * Starting at the block pointed to by zh_log we read the log chain.
226789Sahrens 	 * For each block in the chain we strongly check that block to
227789Sahrens 	 * ensure its validity.  We stop when an invalid block is found.
228789Sahrens 	 * For each block pointer in the chain we call parse_blk_func().
229789Sahrens 	 * For each record in each valid block we call parse_lr_func().
2301807Sbonwick 	 * If the log has been claimed, stop if we encounter a sequence
2311807Sbonwick 	 * number greater than the highest claimed sequence number.
232789Sahrens 	 */
233789Sahrens 	zil_dva_tree_init(&zilog->zl_dva_tree);
234789Sahrens 	for (;;) {
2351807Sbonwick 		seq = blk.blk_cksum.zc_word[ZIL_ZC_SEQ];
2361807Sbonwick 
2371807Sbonwick 		if (claim_seq != 0 && seq > claim_seq)
2381807Sbonwick 			break;
2391807Sbonwick 
2401807Sbonwick 		ASSERT(max_seq < seq);
2411807Sbonwick 		max_seq = seq;
2421807Sbonwick 
2431807Sbonwick 		error = zil_read_log_block(zilog, &blk, &abuf);
244789Sahrens 
245789Sahrens 		if (parse_blk_func != NULL)
246789Sahrens 			parse_blk_func(zilog, &blk, arg, txg);
247789Sahrens 
248789Sahrens 		if (error)
249789Sahrens 			break;
250789Sahrens 
2511807Sbonwick 		lrbuf = abuf->b_data;
252789Sahrens 		ztp = (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1;
253789Sahrens 		blk = ztp->zit_next_blk;
254789Sahrens 
2551807Sbonwick 		if (parse_lr_func == NULL) {
2561807Sbonwick 			VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
257789Sahrens 			continue;
2581807Sbonwick 		}
259789Sahrens 
260789Sahrens 		for (lrp = lrbuf; lrp < lrbuf + ztp->zit_nused; lrp += reclen) {
261789Sahrens 			lr_t *lr = (lr_t *)lrp;
262789Sahrens 			reclen = lr->lrc_reclen;
263789Sahrens 			ASSERT3U(reclen, >=, sizeof (lr_t));
264789Sahrens 			parse_lr_func(zilog, lr, arg, txg);
265789Sahrens 		}
2661807Sbonwick 		VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
267789Sahrens 	}
268789Sahrens 	zil_dva_tree_fini(&zilog->zl_dva_tree);
2691807Sbonwick 
2701807Sbonwick 	return (max_seq);
271789Sahrens }
272789Sahrens 
273789Sahrens /* ARGSUSED */
274789Sahrens static void
275789Sahrens zil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg)
276789Sahrens {
277789Sahrens 	spa_t *spa = zilog->zl_spa;
278789Sahrens 	int err;
279789Sahrens 
280789Sahrens 	/*
281789Sahrens 	 * Claim log block if not already committed and not already claimed.
282789Sahrens 	 */
283789Sahrens 	if (bp->blk_birth >= first_txg &&
284789Sahrens 	    zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp)) == 0) {
285789Sahrens 		err = zio_wait(zio_claim(NULL, spa, first_txg, bp, NULL, NULL));
286789Sahrens 		ASSERT(err == 0);
287789Sahrens 	}
288789Sahrens }
289789Sahrens 
290789Sahrens static void
291789Sahrens zil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg)
292789Sahrens {
293789Sahrens 	if (lrc->lrc_txtype == TX_WRITE) {
294789Sahrens 		lr_write_t *lr = (lr_write_t *)lrc;
295789Sahrens 		zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg);
296789Sahrens 	}
297789Sahrens }
298789Sahrens 
299789Sahrens /* ARGSUSED */
300789Sahrens static void
301789Sahrens zil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg)
302789Sahrens {
303789Sahrens 	zio_free_blk(zilog->zl_spa, bp, dmu_tx_get_txg(tx));
304789Sahrens }
305789Sahrens 
306789Sahrens static void
307789Sahrens zil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg)
308789Sahrens {
309789Sahrens 	/*
310789Sahrens 	 * If we previously claimed it, we need to free it.
311789Sahrens 	 */
312789Sahrens 	if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE) {
313789Sahrens 		lr_write_t *lr = (lr_write_t *)lrc;
314789Sahrens 		blkptr_t *bp = &lr->lr_blkptr;
315789Sahrens 		if (bp->blk_birth >= claim_txg &&
316789Sahrens 		    !zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp))) {
317789Sahrens 			(void) arc_free(NULL, zilog->zl_spa,
318789Sahrens 			    dmu_tx_get_txg(tx), bp, NULL, NULL, ARC_WAIT);
319789Sahrens 		}
320789Sahrens 	}
321789Sahrens }
322789Sahrens 
323789Sahrens /*
324789Sahrens  * Create an on-disk intent log.
325789Sahrens  */
326789Sahrens static void
327789Sahrens zil_create(zilog_t *zilog)
328789Sahrens {
3291807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
330789Sahrens 	lwb_t *lwb;
3311807Sbonwick 	uint64_t txg = 0;
3321807Sbonwick 	dmu_tx_t *tx = NULL;
333789Sahrens 	blkptr_t blk;
3341807Sbonwick 	int error = 0;
335789Sahrens 
336789Sahrens 	/*
3371807Sbonwick 	 * Wait for any previous destroy to complete.
338789Sahrens 	 */
3391807Sbonwick 	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
3401807Sbonwick 
3411807Sbonwick 	ASSERT(zh->zh_claim_txg == 0);
3421807Sbonwick 	ASSERT(zh->zh_replay_seq == 0);
3431807Sbonwick 
3441807Sbonwick 	blk = zh->zh_log;
345789Sahrens 
346789Sahrens 	/*
3471807Sbonwick 	 * If we don't already have an initial log block, allocate one now.
348789Sahrens 	 */
3491807Sbonwick 	if (BP_IS_HOLE(&blk)) {
3501807Sbonwick 		tx = dmu_tx_create(zilog->zl_os);
3511807Sbonwick 		(void) dmu_tx_assign(tx, TXG_WAIT);
3521807Sbonwick 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
3531807Sbonwick 		txg = dmu_tx_get_txg(tx);
3541807Sbonwick 
3551807Sbonwick 		error = zio_alloc_blk(zilog->zl_spa, ZIL_MIN_BLKSZ, &blk, txg);
3561807Sbonwick 
3571807Sbonwick 		if (error == 0)
3581807Sbonwick 			zil_init_log_chain(zilog, &blk);
3591362Sperrin 	}
3601807Sbonwick 
3611807Sbonwick 	/*
3621807Sbonwick 	 * Allocate a log write buffer (lwb) for the first log block.
3631807Sbonwick 	 */
364789Sahrens 	if (error == 0) {
365789Sahrens 		lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP);
366789Sahrens 		lwb->lwb_zilog = zilog;
367789Sahrens 		lwb->lwb_blk = blk;
368789Sahrens 		lwb->lwb_nused = 0;
369789Sahrens 		lwb->lwb_sz = BP_GET_LSIZE(&lwb->lwb_blk);
370789Sahrens 		lwb->lwb_buf = zio_buf_alloc(lwb->lwb_sz);
371789Sahrens 		lwb->lwb_max_txg = txg;
3722237Smaybee 		lwb->lwb_zio = NULL;
3732237Smaybee 
374789Sahrens 		mutex_enter(&zilog->zl_lock);
375789Sahrens 		list_insert_tail(&zilog->zl_lwb_list, lwb);
376789Sahrens 		mutex_exit(&zilog->zl_lock);
377789Sahrens 	}
378789Sahrens 
3791807Sbonwick 	/*
3801807Sbonwick 	 * If we just allocated the first log block, commit our transaction
3811807Sbonwick 	 * and wait for zil_sync() to stuff the block poiner into zh_log.
3821807Sbonwick 	 * (zh is part of the MOS, so we cannot modify it in open context.)
3831807Sbonwick 	 */
3841807Sbonwick 	if (tx != NULL) {
3851807Sbonwick 		dmu_tx_commit(tx);
3861362Sperrin 		txg_wait_synced(zilog->zl_dmu_pool, txg);
3871807Sbonwick 	}
3881807Sbonwick 
3891807Sbonwick 	ASSERT(bcmp(&blk, &zh->zh_log, sizeof (blk)) == 0);
390789Sahrens }
391789Sahrens 
392789Sahrens /*
393789Sahrens  * In one tx, free all log blocks and clear the log header.
3941807Sbonwick  * If keep_first is set, then we're replaying a log with no content.
3951807Sbonwick  * We want to keep the first block, however, so that the first
3961807Sbonwick  * synchronous transaction doesn't require a txg_wait_synced()
3971807Sbonwick  * in zil_create().  We don't need to txg_wait_synced() here either
3981807Sbonwick  * when keep_first is set, because both zil_create() and zil_destroy()
3991807Sbonwick  * will wait for any in-progress destroys to complete.
400789Sahrens  */
401789Sahrens void
4021807Sbonwick zil_destroy(zilog_t *zilog, boolean_t keep_first)
403789Sahrens {
4041807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
4051807Sbonwick 	lwb_t *lwb;
406789Sahrens 	dmu_tx_t *tx;
407789Sahrens 	uint64_t txg;
408789Sahrens 
4091807Sbonwick 	/*
4101807Sbonwick 	 * Wait for any previous destroy to complete.
4111807Sbonwick 	 */
4121807Sbonwick 	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
413789Sahrens 
4141807Sbonwick 	if (BP_IS_HOLE(&zh->zh_log))
415789Sahrens 		return;
416789Sahrens 
417789Sahrens 	tx = dmu_tx_create(zilog->zl_os);
418789Sahrens 	(void) dmu_tx_assign(tx, TXG_WAIT);
419789Sahrens 	dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
420789Sahrens 	txg = dmu_tx_get_txg(tx);
421789Sahrens 
4221807Sbonwick 	mutex_enter(&zilog->zl_lock);
4231807Sbonwick 
4241807Sbonwick 	ASSERT3U(zilog->zl_destroy_txg, <, txg);
425789Sahrens 	zilog->zl_destroy_txg = txg;
4261807Sbonwick 	zilog->zl_keep_first = keep_first;
4271807Sbonwick 
4281807Sbonwick 	if (!list_is_empty(&zilog->zl_lwb_list)) {
4291807Sbonwick 		ASSERT(zh->zh_claim_txg == 0);
4301807Sbonwick 		ASSERT(!keep_first);
4311807Sbonwick 		while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
4321807Sbonwick 			list_remove(&zilog->zl_lwb_list, lwb);
4331807Sbonwick 			if (lwb->lwb_buf != NULL)
4341807Sbonwick 				zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
4351807Sbonwick 			zio_free_blk(zilog->zl_spa, &lwb->lwb_blk, txg);
4361807Sbonwick 			kmem_cache_free(zil_lwb_cache, lwb);
4371807Sbonwick 		}
4381807Sbonwick 	} else {
4391807Sbonwick 		if (!keep_first) {
4401807Sbonwick 			(void) zil_parse(zilog, zil_free_log_block,
4411807Sbonwick 			    zil_free_log_record, tx, zh->zh_claim_txg);
4421807Sbonwick 		}
4431807Sbonwick 	}
4442638Sperrin 	mutex_exit(&zilog->zl_lock);
445789Sahrens 
446789Sahrens 	dmu_tx_commit(tx);
447789Sahrens 
4481807Sbonwick 	if (keep_first)			/* no need to wait in this case */
4491807Sbonwick 		return;
4501807Sbonwick 
4511807Sbonwick 	txg_wait_synced(zilog->zl_dmu_pool, txg);
4521807Sbonwick 	ASSERT(BP_IS_HOLE(&zh->zh_log));
453789Sahrens }
454789Sahrens 
4552199Sahrens int
456789Sahrens zil_claim(char *osname, void *txarg)
457789Sahrens {
458789Sahrens 	dmu_tx_t *tx = txarg;
459789Sahrens 	uint64_t first_txg = dmu_tx_get_txg(tx);
460789Sahrens 	zilog_t *zilog;
461789Sahrens 	zil_header_t *zh;
462789Sahrens 	objset_t *os;
463789Sahrens 	int error;
464789Sahrens 
465789Sahrens 	error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_STANDARD, &os);
466789Sahrens 	if (error) {
467789Sahrens 		cmn_err(CE_WARN, "can't process intent log for %s", osname);
4682199Sahrens 		return (0);
469789Sahrens 	}
470789Sahrens 
471789Sahrens 	zilog = dmu_objset_zil(os);
4721807Sbonwick 	zh = zil_header_in_syncing_context(zilog);
473789Sahrens 
474789Sahrens 	/*
4751807Sbonwick 	 * Claim all log blocks if we haven't already done so, and remember
4761807Sbonwick 	 * the highest claimed sequence number.  This ensures that if we can
4771807Sbonwick 	 * read only part of the log now (e.g. due to a missing device),
4781807Sbonwick 	 * but we can read the entire log later, we will not try to replay
4791807Sbonwick 	 * or destroy beyond the last block we successfully claimed.
480789Sahrens 	 */
481789Sahrens 	ASSERT3U(zh->zh_claim_txg, <=, first_txg);
482789Sahrens 	if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) {
483789Sahrens 		zh->zh_claim_txg = first_txg;
4841807Sbonwick 		zh->zh_claim_seq = zil_parse(zilog, zil_claim_log_block,
4851807Sbonwick 		    zil_claim_log_record, tx, first_txg);
486789Sahrens 		dsl_dataset_dirty(dmu_objset_ds(os), tx);
487789Sahrens 	}
4881807Sbonwick 
489789Sahrens 	ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1));
490789Sahrens 	dmu_objset_close(os);
4912199Sahrens 	return (0);
492789Sahrens }
493789Sahrens 
494789Sahrens void
4952638Sperrin zil_add_vdev(zilog_t *zilog, uint64_t vdev)
496789Sahrens {
497789Sahrens 	zil_vdev_t *zv;
498789Sahrens 
499*2986Sek110237 	if (zfs_nocacheflush)
500789Sahrens 		return;
501789Sahrens 
502789Sahrens 	ASSERT(MUTEX_HELD(&zilog->zl_lock));
503789Sahrens 	zv = kmem_alloc(sizeof (zil_vdev_t), KM_SLEEP);
504789Sahrens 	zv->vdev = vdev;
505789Sahrens 	list_insert_tail(&zilog->zl_vdev_list, zv);
506789Sahrens }
507789Sahrens 
508789Sahrens void
5092638Sperrin zil_flush_vdevs(zilog_t *zilog)
510789Sahrens {
511789Sahrens 	vdev_t *vd;
512789Sahrens 	zil_vdev_t *zv, *zv2;
513789Sahrens 	zio_t *zio;
514789Sahrens 	spa_t *spa;
515789Sahrens 	uint64_t vdev;
516789Sahrens 
517*2986Sek110237 	if (zfs_nocacheflush)
518789Sahrens 		return;
519789Sahrens 
520789Sahrens 	ASSERT(MUTEX_HELD(&zilog->zl_lock));
521789Sahrens 
522789Sahrens 	spa = zilog->zl_spa;
523789Sahrens 	zio = NULL;
524789Sahrens 
5252638Sperrin 	while ((zv = list_head(&zilog->zl_vdev_list)) != NULL) {
526789Sahrens 		vdev = zv->vdev;
527789Sahrens 		list_remove(&zilog->zl_vdev_list, zv);
528789Sahrens 		kmem_free(zv, sizeof (zil_vdev_t));
529789Sahrens 
530789Sahrens 		/*
5312638Sperrin 		 * remove all chained entries with same vdev
532789Sahrens 		 */
533789Sahrens 		zv = list_head(&zilog->zl_vdev_list);
5342638Sperrin 		while (zv) {
535789Sahrens 			zv2 = list_next(&zilog->zl_vdev_list, zv);
536789Sahrens 			if (zv->vdev == vdev) {
537789Sahrens 				list_remove(&zilog->zl_vdev_list, zv);
538789Sahrens 				kmem_free(zv, sizeof (zil_vdev_t));
539789Sahrens 			}
540789Sahrens 			zv = zv2;
541789Sahrens 		}
542789Sahrens 
543789Sahrens 		/* flush the write cache for this vdev */
544789Sahrens 		mutex_exit(&zilog->zl_lock);
545789Sahrens 		if (zio == NULL)
546789Sahrens 			zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
547789Sahrens 		vd = vdev_lookup_top(spa, vdev);
548789Sahrens 		ASSERT(vd);
549789Sahrens 		(void) zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE,
550789Sahrens 		    NULL, NULL, ZIO_PRIORITY_NOW,
551789Sahrens 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
552789Sahrens 		mutex_enter(&zilog->zl_lock);
553789Sahrens 	}
554789Sahrens 
555789Sahrens 	/*
556789Sahrens 	 * Wait for all the flushes to complete.  Not all devices actually
557789Sahrens 	 * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails.
558789Sahrens 	 */
5591141Sperrin 	if (zio != NULL) {
5601141Sperrin 		mutex_exit(&zilog->zl_lock);
561789Sahrens 		(void) zio_wait(zio);
5621141Sperrin 		mutex_enter(&zilog->zl_lock);
5631141Sperrin 	}
564789Sahrens }
565789Sahrens 
566789Sahrens /*
567789Sahrens  * Function called when a log block write completes
568789Sahrens  */
569789Sahrens static void
570789Sahrens zil_lwb_write_done(zio_t *zio)
571789Sahrens {
572789Sahrens 	lwb_t *lwb = zio->io_private;
573789Sahrens 	zilog_t *zilog = lwb->lwb_zilog;
574789Sahrens 
575789Sahrens 	/*
576789Sahrens 	 * Now that we've written this log block, we have a stable pointer
577789Sahrens 	 * to the next block in the chain, so it's OK to let the txg in
578789Sahrens 	 * which we allocated the next block sync.
579789Sahrens 	 */
580789Sahrens 	txg_rele_to_sync(&lwb->lwb_txgh);
581789Sahrens 
582789Sahrens 	zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
583789Sahrens 	mutex_enter(&zilog->zl_lock);
584789Sahrens 	lwb->lwb_buf = NULL;
585789Sahrens 	if (zio->io_error) {
586789Sahrens 		zilog->zl_log_error = B_TRUE;
587789Sahrens 		mutex_exit(&zilog->zl_lock);
588789Sahrens 		return;
589789Sahrens 	}
590789Sahrens 	mutex_exit(&zilog->zl_lock);
591789Sahrens }
592789Sahrens 
593789Sahrens /*
5942237Smaybee  * Initialize the io for a log block.
5952237Smaybee  *
5962237Smaybee  * Note, we should not initialize the IO until we are about
5972237Smaybee  * to use it, since zio_rewrite() does a spa_config_enter().
5982237Smaybee  */
5992237Smaybee static void
6002237Smaybee zil_lwb_write_init(zilog_t *zilog, lwb_t *lwb)
6012237Smaybee {
6022237Smaybee 	zbookmark_t zb;
6032237Smaybee 
6042237Smaybee 	zb.zb_objset = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET];
6052237Smaybee 	zb.zb_object = 0;
6062237Smaybee 	zb.zb_level = -1;
6072237Smaybee 	zb.zb_blkid = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ];
6082237Smaybee 
6092638Sperrin 	if (zilog->zl_root_zio == NULL) {
6102638Sperrin 		zilog->zl_root_zio = zio_root(zilog->zl_spa, NULL, NULL,
6112638Sperrin 		    ZIO_FLAG_CANFAIL);
6122638Sperrin 	}
6132638Sperrin 	lwb->lwb_zio = zio_rewrite(zilog->zl_root_zio, zilog->zl_spa,
6142237Smaybee 	    ZIO_CHECKSUM_ZILOG, 0, &lwb->lwb_blk, lwb->lwb_buf,
6152237Smaybee 	    lwb->lwb_sz, zil_lwb_write_done, lwb,
6162237Smaybee 	    ZIO_PRIORITY_LOG_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
6172237Smaybee }
6182237Smaybee 
6192237Smaybee /*
620789Sahrens  * Start a log block write and advance to the next log block.
621789Sahrens  * Calls are serialized.
622789Sahrens  */
623789Sahrens static lwb_t *
624789Sahrens zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb)
625789Sahrens {
626789Sahrens 	lwb_t *nlwb;
627789Sahrens 	zil_trailer_t *ztp = (zil_trailer_t *)(lwb->lwb_buf + lwb->lwb_sz) - 1;
6281807Sbonwick 	spa_t *spa = zilog->zl_spa;
6291807Sbonwick 	blkptr_t *bp = &ztp->zit_next_blk;
630789Sahrens 	uint64_t txg;
631789Sahrens 	uint64_t zil_blksz;
632789Sahrens 	int error;
633789Sahrens 
634789Sahrens 	ASSERT(lwb->lwb_nused <= ZIL_BLK_DATA_SZ(lwb));
635789Sahrens 
636789Sahrens 	/*
637789Sahrens 	 * Allocate the next block and save its address in this block
638789Sahrens 	 * before writing it in order to establish the log chain.
639789Sahrens 	 * Note that if the allocation of nlwb synced before we wrote
640789Sahrens 	 * the block that points at it (lwb), we'd leak it if we crashed.
641789Sahrens 	 * Therefore, we don't do txg_rele_to_sync() until zil_lwb_write_done().
642789Sahrens 	 */
643789Sahrens 	txg = txg_hold_open(zilog->zl_dmu_pool, &lwb->lwb_txgh);
644789Sahrens 	txg_rele_to_quiesce(&lwb->lwb_txgh);
645789Sahrens 
646789Sahrens 	/*
6471141Sperrin 	 * Pick a ZIL blocksize. We request a size that is the
6481141Sperrin 	 * maximum of the previous used size, the current used size and
6491141Sperrin 	 * the amount waiting in the queue.
650789Sahrens 	 */
6512237Smaybee 	zil_blksz = MAX(zilog->zl_prev_used,
6522237Smaybee 	    zilog->zl_cur_used + sizeof (*ztp));
6531141Sperrin 	zil_blksz = MAX(zil_blksz, zilog->zl_itx_list_sz + sizeof (*ztp));
6541842Sperrin 	zil_blksz = P2ROUNDUP_TYPED(zil_blksz, ZIL_MIN_BLKSZ, uint64_t);
6551141Sperrin 	if (zil_blksz > ZIL_MAX_BLKSZ)
6561141Sperrin 		zil_blksz = ZIL_MAX_BLKSZ;
657789Sahrens 
6581807Sbonwick 	error = zio_alloc_blk(spa, zil_blksz, bp, txg);
659789Sahrens 	if (error) {
6601544Seschrock 		/*
6611544Seschrock 		 * Reinitialise the lwb.
6621544Seschrock 		 * By returning NULL the caller will call tx_wait_synced()
6631544Seschrock 		 */
6641544Seschrock 		mutex_enter(&zilog->zl_lock);
6651544Seschrock 		lwb->lwb_nused = 0;
6661544Seschrock 		mutex_exit(&zilog->zl_lock);
667789Sahrens 		txg_rele_to_sync(&lwb->lwb_txgh);
668789Sahrens 		return (NULL);
669789Sahrens 	}
670789Sahrens 
6711807Sbonwick 	ASSERT3U(bp->blk_birth, ==, txg);
6721544Seschrock 	ztp->zit_pad = 0;
673789Sahrens 	ztp->zit_nused = lwb->lwb_nused;
674789Sahrens 	ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum;
6751807Sbonwick 	bp->blk_cksum = lwb->lwb_blk.blk_cksum;
6761807Sbonwick 	bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++;
677789Sahrens 
678789Sahrens 	/*
679789Sahrens 	 * Allocate a new log write buffer (lwb).
680789Sahrens 	 */
681789Sahrens 	nlwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP);
682789Sahrens 
683789Sahrens 	nlwb->lwb_zilog = zilog;
6841807Sbonwick 	nlwb->lwb_blk = *bp;
685789Sahrens 	nlwb->lwb_nused = 0;
686789Sahrens 	nlwb->lwb_sz = BP_GET_LSIZE(&nlwb->lwb_blk);
687789Sahrens 	nlwb->lwb_buf = zio_buf_alloc(nlwb->lwb_sz);
688789Sahrens 	nlwb->lwb_max_txg = txg;
6892237Smaybee 	nlwb->lwb_zio = NULL;
690789Sahrens 
691789Sahrens 	/*
692789Sahrens 	 * Put new lwb at the end of the log chain,
693789Sahrens 	 * and record the vdev for later flushing
694789Sahrens 	 */
695789Sahrens 	mutex_enter(&zilog->zl_lock);
696789Sahrens 	list_insert_tail(&zilog->zl_lwb_list, nlwb);
6972638Sperrin 	zil_add_vdev(zilog, DVA_GET_VDEV(BP_IDENTITY(&(lwb->lwb_blk))));
698789Sahrens 	mutex_exit(&zilog->zl_lock);
699789Sahrens 
700789Sahrens 	/*
7012237Smaybee 	 * kick off the write for the old log block
702789Sahrens 	 */
7032237Smaybee 	dprintf_bp(&lwb->lwb_blk, "lwb %p txg %llu: ", lwb, txg);
7042638Sperrin 	if (lwb->lwb_zio == NULL)
7052237Smaybee 		zil_lwb_write_init(zilog, lwb);
7062237Smaybee 	zio_nowait(lwb->lwb_zio);
707789Sahrens 
708789Sahrens 	return (nlwb);
709789Sahrens }
710789Sahrens 
711789Sahrens static lwb_t *
712789Sahrens zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb)
713789Sahrens {
714789Sahrens 	lr_t *lrc = &itx->itx_lr; /* common log record */
7152237Smaybee 	lr_write_t *lr = (lr_write_t *)lrc;
716789Sahrens 	uint64_t txg = lrc->lrc_txg;
717789Sahrens 	uint64_t reclen = lrc->lrc_reclen;
7182237Smaybee 	uint64_t dlen;
719789Sahrens 
720789Sahrens 	if (lwb == NULL)
721789Sahrens 		return (NULL);
722789Sahrens 	ASSERT(lwb->lwb_buf != NULL);
723789Sahrens 
7242237Smaybee 	if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY)
7252237Smaybee 		dlen = P2ROUNDUP_TYPED(
7262237Smaybee 		    lr->lr_length, sizeof (uint64_t), uint64_t);
7272237Smaybee 	else
7282237Smaybee 		dlen = 0;
7291669Sperrin 
7301669Sperrin 	zilog->zl_cur_used += (reclen + dlen);
7311669Sperrin 
7321669Sperrin 	/*
7331669Sperrin 	 * If this record won't fit in the current log block, start a new one.
7341669Sperrin 	 */
7351669Sperrin 	if (lwb->lwb_nused + reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) {
7361669Sperrin 		lwb = zil_lwb_write_start(zilog, lwb);
7372237Smaybee 		if (lwb == NULL)
7381669Sperrin 			return (NULL);
7391669Sperrin 		ASSERT(lwb->lwb_nused == 0);
7401669Sperrin 		if (reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) {
7411669Sperrin 			txg_wait_synced(zilog->zl_dmu_pool, txg);
742789Sahrens 			return (lwb);
743789Sahrens 		}
744789Sahrens 	}
745789Sahrens 
7462638Sperrin 	/*
7472638Sperrin 	 * Update the lrc_seq, to be log record sequence number. See zil.h
7482638Sperrin 	 * Then copy the record to the log buffer.
7492638Sperrin 	 */
7502638Sperrin 	lrc->lrc_seq = ++zilog->zl_lr_seq; /* we are single threaded */
751789Sahrens 	bcopy(lrc, lwb->lwb_buf + lwb->lwb_nused, reclen);
7522237Smaybee 
7532237Smaybee 	/*
7542237Smaybee 	 * If it's a write, fetch the data or get its blkptr as appropriate.
7552237Smaybee 	 */
7562237Smaybee 	if (lrc->lrc_txtype == TX_WRITE) {
7572237Smaybee 		if (txg > spa_freeze_txg(zilog->zl_spa))
7582237Smaybee 			txg_wait_synced(zilog->zl_dmu_pool, txg);
7592237Smaybee 		if (itx->itx_wr_state != WR_COPIED) {
7602237Smaybee 			char *dbuf;
7612237Smaybee 			int error;
7622237Smaybee 
7632237Smaybee 			/* alignment is guaranteed */
7642237Smaybee 			lr = (lr_write_t *)(lwb->lwb_buf + lwb->lwb_nused);
7652237Smaybee 			if (dlen) {
7662237Smaybee 				ASSERT(itx->itx_wr_state == WR_NEED_COPY);
7672237Smaybee 				dbuf = lwb->lwb_buf + lwb->lwb_nused + reclen;
7682237Smaybee 				lr->lr_common.lrc_reclen += dlen;
7692237Smaybee 			} else {
7702237Smaybee 				ASSERT(itx->itx_wr_state == WR_INDIRECT);
7712237Smaybee 				dbuf = NULL;
7722237Smaybee 			}
7732237Smaybee 			error = zilog->zl_get_data(
7742237Smaybee 			    itx->itx_private, lr, dbuf, lwb->lwb_zio);
7752237Smaybee 			if (error) {
7762237Smaybee 				ASSERT(error == ENOENT || error == EEXIST ||
7772237Smaybee 				    error == EALREADY);
7782237Smaybee 				return (lwb);
7792237Smaybee 			}
7802237Smaybee 		}
7811669Sperrin 	}
7822237Smaybee 
7832237Smaybee 	lwb->lwb_nused += reclen + dlen;
784789Sahrens 	lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg);
785789Sahrens 	ASSERT3U(lwb->lwb_nused, <=, ZIL_BLK_DATA_SZ(lwb));
786789Sahrens 	ASSERT3U(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)), ==, 0);
787789Sahrens 
788789Sahrens 	return (lwb);
789789Sahrens }
790789Sahrens 
791789Sahrens itx_t *
792789Sahrens zil_itx_create(int txtype, size_t lrsize)
793789Sahrens {
794789Sahrens 	itx_t *itx;
795789Sahrens 
7961842Sperrin 	lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t);
797789Sahrens 
798789Sahrens 	itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP);
799789Sahrens 	itx->itx_lr.lrc_txtype = txtype;
800789Sahrens 	itx->itx_lr.lrc_reclen = lrsize;
801789Sahrens 	itx->itx_lr.lrc_seq = 0;	/* defensive */
802789Sahrens 
803789Sahrens 	return (itx);
804789Sahrens }
805789Sahrens 
806789Sahrens uint64_t
807789Sahrens zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx)
808789Sahrens {
809789Sahrens 	uint64_t seq;
810789Sahrens 
811789Sahrens 	ASSERT(itx->itx_lr.lrc_seq == 0);
812789Sahrens 
813789Sahrens 	mutex_enter(&zilog->zl_lock);
814789Sahrens 	list_insert_tail(&zilog->zl_itx_list, itx);
815789Sahrens 	zilog->zl_itx_list_sz += itx->itx_lr.lrc_reclen;
816789Sahrens 	itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx);
817789Sahrens 	itx->itx_lr.lrc_seq = seq = ++zilog->zl_itx_seq;
818789Sahrens 	mutex_exit(&zilog->zl_lock);
819789Sahrens 
820789Sahrens 	return (seq);
821789Sahrens }
822789Sahrens 
823789Sahrens /*
824789Sahrens  * Free up all in-memory intent log transactions that have now been synced.
825789Sahrens  */
826789Sahrens static void
827789Sahrens zil_itx_clean(zilog_t *zilog)
828789Sahrens {
829789Sahrens 	uint64_t synced_txg = spa_last_synced_txg(zilog->zl_spa);
830789Sahrens 	uint64_t freeze_txg = spa_freeze_txg(zilog->zl_spa);
831789Sahrens 	itx_t *itx;
832789Sahrens 
833789Sahrens 	mutex_enter(&zilog->zl_lock);
8342638Sperrin 	/* wait for a log writer to finish walking list */
8352638Sperrin 	while (zilog->zl_writer) {
8362638Sperrin 		cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
8372638Sperrin 	}
8382638Sperrin 	/* no need to set zl_writer as we never drop zl_lock */
839789Sahrens 	while ((itx = list_head(&zilog->zl_itx_list)) != NULL &&
840789Sahrens 	    itx->itx_lr.lrc_txg <= MIN(synced_txg, freeze_txg)) {
841789Sahrens 		list_remove(&zilog->zl_itx_list, itx);
842789Sahrens 		zilog->zl_itx_list_sz -= itx->itx_lr.lrc_reclen;
843789Sahrens 		kmem_free(itx, offsetof(itx_t, itx_lr)
844789Sahrens 		    + itx->itx_lr.lrc_reclen);
845789Sahrens 	}
846789Sahrens 	mutex_exit(&zilog->zl_lock);
847789Sahrens }
848789Sahrens 
8492638Sperrin /*
8502638Sperrin  * If there are in-memory intent log transactions then
8512638Sperrin  * start up a taskq to free up any that have now been synced.
8522638Sperrin  */
853789Sahrens void
854789Sahrens zil_clean(zilog_t *zilog)
855789Sahrens {
856789Sahrens 	mutex_enter(&zilog->zl_lock);
857789Sahrens 	if (list_head(&zilog->zl_itx_list) != NULL)
858789Sahrens 		(void) taskq_dispatch(zilog->zl_clean_taskq,
859789Sahrens 		    (void (*)(void *))zil_itx_clean, zilog, TQ_NOSLEEP);
860789Sahrens 	mutex_exit(&zilog->zl_lock);
861789Sahrens }
862789Sahrens 
863789Sahrens void
8642638Sperrin zil_commit_writer(zilog_t *zilog, uint64_t seq, uint64_t foid)
865789Sahrens {
866789Sahrens 	uint64_t txg;
867789Sahrens 	uint64_t reclen;
8682638Sperrin 	itx_t *itx, *itx_next = (itx_t *)-1;
869789Sahrens 	lwb_t *lwb;
870789Sahrens 	spa_t *spa;
871789Sahrens 
8722638Sperrin 	zilog->zl_writer = B_TRUE;
8732638Sperrin 	zilog->zl_root_zio = NULL;
874789Sahrens 	spa = zilog->zl_spa;
875789Sahrens 
876789Sahrens 	if (zilog->zl_suspend) {
877789Sahrens 		lwb = NULL;
878789Sahrens 	} else {
879789Sahrens 		lwb = list_tail(&zilog->zl_lwb_list);
880789Sahrens 		if (lwb == NULL) {
8812638Sperrin 			/*
8822638Sperrin 			 * Return if there's nothing to flush before we
8832638Sperrin 			 * dirty the fs by calling zil_create()
8842638Sperrin 			 */
8852638Sperrin 			if (list_is_empty(&zilog->zl_itx_list)) {
8862638Sperrin 				/* wake up others waiting to start a write */
8872638Sperrin 				zilog->zl_writer = B_FALSE;
8882638Sperrin 				cv_broadcast(&zilog->zl_cv_writer);
8892638Sperrin 				mutex_exit(&zilog->zl_lock);
8902638Sperrin 				return;
8912638Sperrin 			}
8922638Sperrin 
893789Sahrens 			mutex_exit(&zilog->zl_lock);
894789Sahrens 			zil_create(zilog);
895789Sahrens 			mutex_enter(&zilog->zl_lock);
896789Sahrens 			lwb = list_tail(&zilog->zl_lwb_list);
897789Sahrens 		}
898789Sahrens 	}
899789Sahrens 
900789Sahrens 	/*
901789Sahrens 	 * Loop through in-memory log transactions filling log blocks,
902789Sahrens 	 * until we reach the given sequence number and there's no more
903789Sahrens 	 * room in the write buffer.
904789Sahrens 	 */
9052638Sperrin 	DTRACE_PROBE1(zil__cw1, zilog_t *, zilog);
906789Sahrens 	for (;;) {
9072638Sperrin 		/*
9082638Sperrin 		 * Find the next itx to push:
9092638Sperrin 		 * Push all transactions related to specified foid and all
9102638Sperrin 		 * other transactions except TX_WRITE, TX_TRUNCATE,
9112638Sperrin 		 * TX_SETATTR and TX_ACL for all other files.
9122638Sperrin 		 */
9132638Sperrin 		if (itx_next != (itx_t *)-1)
9142638Sperrin 			itx = itx_next;
9152638Sperrin 		else
9162638Sperrin 			itx = list_head(&zilog->zl_itx_list);
9172638Sperrin 		for (; itx != NULL; itx = list_next(&zilog->zl_itx_list, itx)) {
9182638Sperrin 			if (foid == 0) /* push all foids? */
9192638Sperrin 				break;
9202638Sperrin 			switch (itx->itx_lr.lrc_txtype) {
9212638Sperrin 			case TX_SETATTR:
9222638Sperrin 			case TX_WRITE:
9232638Sperrin 			case TX_TRUNCATE:
9242638Sperrin 			case TX_ACL:
9252638Sperrin 				/* lr_foid is same offset for these records */
9262638Sperrin 				if (((lr_write_t *)&itx->itx_lr)->lr_foid
9272638Sperrin 				    != foid) {
9282638Sperrin 					continue; /* skip this record */
9292638Sperrin 				}
9302638Sperrin 			}
9312638Sperrin 			break;
9322638Sperrin 		}
933789Sahrens 		if (itx == NULL)
934789Sahrens 			break;
935789Sahrens 
936789Sahrens 		reclen = itx->itx_lr.lrc_reclen;
937789Sahrens 		if ((itx->itx_lr.lrc_seq > seq) &&
9382638Sperrin 		    ((lwb == NULL) || (lwb->lwb_nused == 0) ||
9392638Sperrin 		    (lwb->lwb_nused + reclen > ZIL_BLK_DATA_SZ(lwb))))
940789Sahrens 			break;
941789Sahrens 
9422638Sperrin 		/*
9432638Sperrin 		 * Save the next pointer.  Even though we soon drop
9442638Sperrin 		 * zl_lock all threads that may change the list
9452638Sperrin 		 * (another writer or zil_itx_clean) can't do so until
9462638Sperrin 		 * they have zl_writer.
9472638Sperrin 		 */
9482638Sperrin 		itx_next = list_next(&zilog->zl_itx_list, itx);
949789Sahrens 		list_remove(&zilog->zl_itx_list, itx);
950789Sahrens 		txg = itx->itx_lr.lrc_txg;
951789Sahrens 		ASSERT(txg);
952789Sahrens 
953789Sahrens 		mutex_exit(&zilog->zl_lock);
954789Sahrens 		if (txg > spa_last_synced_txg(spa) ||
955789Sahrens 		    txg > spa_freeze_txg(spa))
956789Sahrens 			lwb = zil_lwb_commit(zilog, itx, lwb);
957789Sahrens 		kmem_free(itx, offsetof(itx_t, itx_lr)
958789Sahrens 		    + itx->itx_lr.lrc_reclen);
959789Sahrens 		mutex_enter(&zilog->zl_lock);
960789Sahrens 		zilog->zl_itx_list_sz -= reclen;
961789Sahrens 	}
9622638Sperrin 	DTRACE_PROBE1(zil__cw2, zilog_t *, zilog);
963789Sahrens 	mutex_exit(&zilog->zl_lock);
964789Sahrens 
965789Sahrens 	/* write the last block out */
966789Sahrens 	if (lwb != NULL && lwb->lwb_nused != 0)
967789Sahrens 		lwb = zil_lwb_write_start(zilog, lwb);
968789Sahrens 
9691141Sperrin 	zilog->zl_prev_used = zilog->zl_cur_used;
9701141Sperrin 	zilog->zl_cur_used = 0;
9711141Sperrin 
9722638Sperrin 	/*
9732638Sperrin 	 * Wait if necessary for the log blocks to be on stable storage.
9742638Sperrin 	 */
975789Sahrens 	mutex_enter(&zilog->zl_lock);
9762638Sperrin 	if (zilog->zl_root_zio) {
9772638Sperrin 		mutex_exit(&zilog->zl_lock);
9782638Sperrin 		DTRACE_PROBE1(zil__cw3, zilog_t *, zilog);
9792638Sperrin 		(void) zio_wait(zilog->zl_root_zio);
9802638Sperrin 		DTRACE_PROBE1(zil__cw4, zilog_t *, zilog);
9812638Sperrin 		mutex_enter(&zilog->zl_lock);
9822638Sperrin 		zil_flush_vdevs(zilog);
983789Sahrens 	}
9841141Sperrin 
985789Sahrens 	if (zilog->zl_log_error || lwb == NULL) {
986789Sahrens 		zilog->zl_log_error = 0;
987789Sahrens 		mutex_exit(&zilog->zl_lock);
988789Sahrens 		txg_wait_synced(zilog->zl_dmu_pool, 0);
989789Sahrens 		mutex_enter(&zilog->zl_lock);
990789Sahrens 	}
9911141Sperrin 	/* wake up others waiting to start a write */
9921141Sperrin 	zilog->zl_writer = B_FALSE;
9932638Sperrin 	cv_broadcast(&zilog->zl_cv_writer);
994789Sahrens 	mutex_exit(&zilog->zl_lock);
9952638Sperrin }
9962638Sperrin 
9972638Sperrin /*
9982638Sperrin  * Push zfs transactions to stable storage up to the supplied sequence number.
9992638Sperrin  * If foid is 0 push out all transactions, otherwise push only those
10002638Sperrin  * for that file or might have been used to create that file.
10012638Sperrin  */
10022638Sperrin void
10032638Sperrin zil_commit(zilog_t *zilog, uint64_t seq, uint64_t foid)
10042638Sperrin {
10052638Sperrin 	if (zilog == NULL || seq == 0)
10062638Sperrin 		return;
10072638Sperrin 
10082638Sperrin 	mutex_enter(&zilog->zl_lock);
10092638Sperrin 
10102638Sperrin 	seq = MIN(seq, zilog->zl_itx_seq);	/* cap seq at largest itx seq */
10112638Sperrin 
10122638Sperrin 	while (zilog->zl_writer)
10132638Sperrin 		cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
10142638Sperrin 	zil_commit_writer(zilog, seq, foid); /* drops zl_lock */
1015789Sahrens }
1016789Sahrens 
1017789Sahrens /*
1018789Sahrens  * Called in syncing context to free committed log blocks and update log header.
1019789Sahrens  */
1020789Sahrens void
1021789Sahrens zil_sync(zilog_t *zilog, dmu_tx_t *tx)
1022789Sahrens {
10231807Sbonwick 	zil_header_t *zh = zil_header_in_syncing_context(zilog);
1024789Sahrens 	uint64_t txg = dmu_tx_get_txg(tx);
1025789Sahrens 	spa_t *spa = zilog->zl_spa;
1026789Sahrens 	lwb_t *lwb;
1027789Sahrens 
10281807Sbonwick 	mutex_enter(&zilog->zl_lock);
10291807Sbonwick 
1030789Sahrens 	ASSERT(zilog->zl_stop_sync == 0);
1031789Sahrens 
10321807Sbonwick 	zh->zh_replay_seq = zilog->zl_replay_seq[txg & TXG_MASK];
1033789Sahrens 
1034789Sahrens 	if (zilog->zl_destroy_txg == txg) {
10351807Sbonwick 		blkptr_t blk = zh->zh_log;
10361807Sbonwick 
10371807Sbonwick 		ASSERT(list_head(&zilog->zl_lwb_list) == NULL);
10381807Sbonwick 		ASSERT(spa_sync_pass(spa) == 1);
10391807Sbonwick 
10401807Sbonwick 		bzero(zh, sizeof (zil_header_t));
1041789Sahrens 		bzero(zilog->zl_replay_seq, sizeof (zilog->zl_replay_seq));
10421807Sbonwick 
10431807Sbonwick 		if (zilog->zl_keep_first) {
10441807Sbonwick 			/*
10451807Sbonwick 			 * If this block was part of log chain that couldn't
10461807Sbonwick 			 * be claimed because a device was missing during
10471807Sbonwick 			 * zil_claim(), but that device later returns,
10481807Sbonwick 			 * then this block could erroneously appear valid.
10491807Sbonwick 			 * To guard against this, assign a new GUID to the new
10501807Sbonwick 			 * log chain so it doesn't matter what blk points to.
10511807Sbonwick 			 */
10521807Sbonwick 			zil_init_log_chain(zilog, &blk);
10531807Sbonwick 			zh->zh_log = blk;
10541807Sbonwick 		}
1055789Sahrens 	}
1056789Sahrens 
1057789Sahrens 	for (;;) {
1058789Sahrens 		lwb = list_head(&zilog->zl_lwb_list);
1059789Sahrens 		if (lwb == NULL) {
1060789Sahrens 			mutex_exit(&zilog->zl_lock);
1061789Sahrens 			return;
1062789Sahrens 		}
10632638Sperrin 		zh->zh_log = lwb->lwb_blk;
1064789Sahrens 		if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg)
1065789Sahrens 			break;
1066789Sahrens 		list_remove(&zilog->zl_lwb_list, lwb);
1067789Sahrens 		zio_free_blk(spa, &lwb->lwb_blk, txg);
1068789Sahrens 		kmem_cache_free(zil_lwb_cache, lwb);
1069789Sahrens 	}
1070789Sahrens 	mutex_exit(&zilog->zl_lock);
1071789Sahrens }
1072789Sahrens 
1073789Sahrens void
1074789Sahrens zil_init(void)
1075789Sahrens {
1076789Sahrens 	zil_lwb_cache = kmem_cache_create("zil_lwb_cache",
10772856Snd150628 	    sizeof (struct lwb), 0, NULL, NULL, NULL, NULL, NULL, 0);
1078789Sahrens }
1079789Sahrens 
1080789Sahrens void
1081789Sahrens zil_fini(void)
1082789Sahrens {
1083789Sahrens 	kmem_cache_destroy(zil_lwb_cache);
1084789Sahrens }
1085789Sahrens 
1086789Sahrens zilog_t *
1087789Sahrens zil_alloc(objset_t *os, zil_header_t *zh_phys)
1088789Sahrens {
1089789Sahrens 	zilog_t *zilog;
1090789Sahrens 
1091789Sahrens 	zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP);
1092789Sahrens 
1093789Sahrens 	zilog->zl_header = zh_phys;
1094789Sahrens 	zilog->zl_os = os;
1095789Sahrens 	zilog->zl_spa = dmu_objset_spa(os);
1096789Sahrens 	zilog->zl_dmu_pool = dmu_objset_pool(os);
10971807Sbonwick 	zilog->zl_destroy_txg = TXG_INITIAL - 1;
1098789Sahrens 
10992856Snd150628 	mutex_init(&zilog->zl_lock, NULL, MUTEX_DEFAULT, NULL);
11002856Snd150628 
1101789Sahrens 	list_create(&zilog->zl_itx_list, sizeof (itx_t),
1102789Sahrens 	    offsetof(itx_t, itx_node));
1103789Sahrens 
1104789Sahrens 	list_create(&zilog->zl_lwb_list, sizeof (lwb_t),
1105789Sahrens 	    offsetof(lwb_t, lwb_node));
1106789Sahrens 
1107789Sahrens 	list_create(&zilog->zl_vdev_list, sizeof (zil_vdev_t),
1108789Sahrens 	    offsetof(zil_vdev_t, vdev_seq_node));
1109789Sahrens 
1110789Sahrens 	return (zilog);
1111789Sahrens }
1112789Sahrens 
1113789Sahrens void
1114789Sahrens zil_free(zilog_t *zilog)
1115789Sahrens {
1116789Sahrens 	lwb_t *lwb;
1117789Sahrens 	zil_vdev_t *zv;
1118789Sahrens 
1119789Sahrens 	zilog->zl_stop_sync = 1;
1120789Sahrens 
1121789Sahrens 	while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
1122789Sahrens 		list_remove(&zilog->zl_lwb_list, lwb);
1123789Sahrens 		if (lwb->lwb_buf != NULL)
1124789Sahrens 			zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
1125789Sahrens 		kmem_cache_free(zil_lwb_cache, lwb);
1126789Sahrens 	}
1127789Sahrens 	list_destroy(&zilog->zl_lwb_list);
1128789Sahrens 
1129789Sahrens 	while ((zv = list_head(&zilog->zl_vdev_list)) != NULL) {
1130789Sahrens 		list_remove(&zilog->zl_vdev_list, zv);
1131789Sahrens 		kmem_free(zv, sizeof (zil_vdev_t));
1132789Sahrens 	}
1133789Sahrens 	list_destroy(&zilog->zl_vdev_list);
1134789Sahrens 
1135789Sahrens 	ASSERT(list_head(&zilog->zl_itx_list) == NULL);
1136789Sahrens 	list_destroy(&zilog->zl_itx_list);
11372856Snd150628 	mutex_destroy(&zilog->zl_lock);
1138789Sahrens 
1139789Sahrens 	kmem_free(zilog, sizeof (zilog_t));
1140789Sahrens }
1141789Sahrens 
1142789Sahrens /*
11431646Sperrin  * return true if the initial log block is not valid
11441362Sperrin  */
11451362Sperrin static int
11461362Sperrin zil_empty(zilog_t *zilog)
11471362Sperrin {
11481807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
11491807Sbonwick 	arc_buf_t *abuf = NULL;
11501362Sperrin 
11511807Sbonwick 	if (BP_IS_HOLE(&zh->zh_log))
11521362Sperrin 		return (1);
11531362Sperrin 
11541807Sbonwick 	if (zil_read_log_block(zilog, &zh->zh_log, &abuf) != 0)
11551807Sbonwick 		return (1);
11561807Sbonwick 
11571807Sbonwick 	VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
11581807Sbonwick 	return (0);
11591362Sperrin }
11601362Sperrin 
11611362Sperrin /*
1162789Sahrens  * Open an intent log.
1163789Sahrens  */
1164789Sahrens zilog_t *
1165789Sahrens zil_open(objset_t *os, zil_get_data_t *get_data)
1166789Sahrens {
1167789Sahrens 	zilog_t *zilog = dmu_objset_zil(os);
1168789Sahrens 
1169789Sahrens 	zilog->zl_get_data = get_data;
1170789Sahrens 	zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri,
1171789Sahrens 	    2, 2, TASKQ_PREPOPULATE);
1172789Sahrens 
1173789Sahrens 	return (zilog);
1174789Sahrens }
1175789Sahrens 
1176789Sahrens /*
1177789Sahrens  * Close an intent log.
1178789Sahrens  */
1179789Sahrens void
1180789Sahrens zil_close(zilog_t *zilog)
1181789Sahrens {
11821807Sbonwick 	/*
11831807Sbonwick 	 * If the log isn't already committed, mark the objset dirty
11841807Sbonwick 	 * (so zil_sync() will be called) and wait for that txg to sync.
11851807Sbonwick 	 */
11861807Sbonwick 	if (!zil_is_committed(zilog)) {
11871807Sbonwick 		uint64_t txg;
11881807Sbonwick 		dmu_tx_t *tx = dmu_tx_create(zilog->zl_os);
11891807Sbonwick 		(void) dmu_tx_assign(tx, TXG_WAIT);
11901807Sbonwick 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
11911807Sbonwick 		txg = dmu_tx_get_txg(tx);
11921807Sbonwick 		dmu_tx_commit(tx);
11931807Sbonwick 		txg_wait_synced(zilog->zl_dmu_pool, txg);
11941807Sbonwick 	}
11951807Sbonwick 
1196789Sahrens 	taskq_destroy(zilog->zl_clean_taskq);
1197789Sahrens 	zilog->zl_clean_taskq = NULL;
1198789Sahrens 	zilog->zl_get_data = NULL;
1199789Sahrens 
1200789Sahrens 	zil_itx_clean(zilog);
1201789Sahrens 	ASSERT(list_head(&zilog->zl_itx_list) == NULL);
1202789Sahrens }
1203789Sahrens 
1204789Sahrens /*
1205789Sahrens  * Suspend an intent log.  While in suspended mode, we still honor
1206789Sahrens  * synchronous semantics, but we rely on txg_wait_synced() to do it.
1207789Sahrens  * We suspend the log briefly when taking a snapshot so that the snapshot
1208789Sahrens  * contains all the data it's supposed to, and has an empty intent log.
1209789Sahrens  */
1210789Sahrens int
1211789Sahrens zil_suspend(zilog_t *zilog)
1212789Sahrens {
12131807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
1214789Sahrens 
1215789Sahrens 	mutex_enter(&zilog->zl_lock);
12161807Sbonwick 	if (zh->zh_claim_txg != 0) {		/* unplayed log */
1217789Sahrens 		mutex_exit(&zilog->zl_lock);
1218789Sahrens 		return (EBUSY);
1219789Sahrens 	}
12201807Sbonwick 	if (zilog->zl_suspend++ != 0) {
12211807Sbonwick 		/*
12221807Sbonwick 		 * Someone else already began a suspend.
12231807Sbonwick 		 * Just wait for them to finish.
12241807Sbonwick 		 */
12251807Sbonwick 		while (zilog->zl_suspending)
12261807Sbonwick 			cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock);
12271807Sbonwick 		ASSERT(BP_IS_HOLE(&zh->zh_log));
12281807Sbonwick 		mutex_exit(&zilog->zl_lock);
12291807Sbonwick 		return (0);
12301807Sbonwick 	}
12311807Sbonwick 	zilog->zl_suspending = B_TRUE;
1232789Sahrens 	mutex_exit(&zilog->zl_lock);
1233789Sahrens 
12342638Sperrin 	zil_commit(zilog, UINT64_MAX, 0);
1235789Sahrens 
12362638Sperrin 	/*
12372638Sperrin 	 * Wait for any in-flight log writes to complete.
12382638Sperrin 	 */
1239789Sahrens 	mutex_enter(&zilog->zl_lock);
12402638Sperrin 	while (zilog->zl_writer)
12412638Sperrin 		cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
1242789Sahrens 	mutex_exit(&zilog->zl_lock);
1243789Sahrens 
12441807Sbonwick 	zil_destroy(zilog, B_FALSE);
12451807Sbonwick 
12461807Sbonwick 	mutex_enter(&zilog->zl_lock);
12471807Sbonwick 	ASSERT(BP_IS_HOLE(&zh->zh_log));
12481807Sbonwick 	zilog->zl_suspending = B_FALSE;
12491807Sbonwick 	cv_broadcast(&zilog->zl_cv_suspend);
12501807Sbonwick 	mutex_exit(&zilog->zl_lock);
1251789Sahrens 
1252789Sahrens 	return (0);
1253789Sahrens }
1254789Sahrens 
1255789Sahrens void
1256789Sahrens zil_resume(zilog_t *zilog)
1257789Sahrens {
1258789Sahrens 	mutex_enter(&zilog->zl_lock);
1259789Sahrens 	ASSERT(zilog->zl_suspend != 0);
1260789Sahrens 	zilog->zl_suspend--;
1261789Sahrens 	mutex_exit(&zilog->zl_lock);
1262789Sahrens }
1263789Sahrens 
1264789Sahrens typedef struct zil_replay_arg {
1265789Sahrens 	objset_t	*zr_os;
1266789Sahrens 	zil_replay_func_t **zr_replay;
1267789Sahrens 	void		*zr_arg;
1268789Sahrens 	void		(*zr_rm_sync)(void *arg);
1269789Sahrens 	uint64_t	*zr_txgp;
1270789Sahrens 	boolean_t	zr_byteswap;
1271789Sahrens 	char		*zr_lrbuf;
1272789Sahrens } zil_replay_arg_t;
1273789Sahrens 
1274789Sahrens static void
1275789Sahrens zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg)
1276789Sahrens {
1277789Sahrens 	zil_replay_arg_t *zr = zra;
12781807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
1279789Sahrens 	uint64_t reclen = lr->lrc_reclen;
1280789Sahrens 	uint64_t txtype = lr->lrc_txtype;
1281789Sahrens 	int pass, error;
1282789Sahrens 
1283789Sahrens 	if (zilog->zl_stop_replay)
1284789Sahrens 		return;
1285789Sahrens 
1286789Sahrens 	if (lr->lrc_txg < claim_txg)		/* already committed */
1287789Sahrens 		return;
1288789Sahrens 
1289789Sahrens 	if (lr->lrc_seq <= zh->zh_replay_seq)	/* already replayed */
1290789Sahrens 		return;
1291789Sahrens 
1292789Sahrens 	/*
1293789Sahrens 	 * Make a copy of the data so we can revise and extend it.
1294789Sahrens 	 */
1295789Sahrens 	bcopy(lr, zr->zr_lrbuf, reclen);
1296789Sahrens 
1297789Sahrens 	/*
1298789Sahrens 	 * The log block containing this lr may have been byteswapped
1299789Sahrens 	 * so that we can easily examine common fields like lrc_txtype.
1300789Sahrens 	 * However, the log is a mix of different data types, and only the
1301789Sahrens 	 * replay vectors know how to byteswap their records.  Therefore, if
1302789Sahrens 	 * the lr was byteswapped, undo it before invoking the replay vector.
1303789Sahrens 	 */
1304789Sahrens 	if (zr->zr_byteswap)
1305789Sahrens 		byteswap_uint64_array(zr->zr_lrbuf, reclen);
1306789Sahrens 
1307789Sahrens 	/*
1308789Sahrens 	 * If this is a TX_WRITE with a blkptr, suck in the data.
1309789Sahrens 	 */
1310789Sahrens 	if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) {
1311789Sahrens 		lr_write_t *lrw = (lr_write_t *)lr;
1312789Sahrens 		blkptr_t *wbp = &lrw->lr_blkptr;
1313789Sahrens 		uint64_t wlen = lrw->lr_length;
1314789Sahrens 		char *wbuf = zr->zr_lrbuf + reclen;
1315789Sahrens 
1316789Sahrens 		if (BP_IS_HOLE(wbp)) {	/* compressed to a hole */
1317789Sahrens 			bzero(wbuf, wlen);
1318789Sahrens 		} else {
1319789Sahrens 			/*
1320789Sahrens 			 * A subsequent write may have overwritten this block,
1321789Sahrens 			 * in which case wbp may have been been freed and
1322789Sahrens 			 * reallocated, and our read of wbp may fail with a
1323789Sahrens 			 * checksum error.  We can safely ignore this because
1324789Sahrens 			 * the later write will provide the correct data.
1325789Sahrens 			 */
13261544Seschrock 			zbookmark_t zb;
13271544Seschrock 
13281544Seschrock 			zb.zb_objset = dmu_objset_id(zilog->zl_os);
13291544Seschrock 			zb.zb_object = lrw->lr_foid;
13301544Seschrock 			zb.zb_level = -1;
13311544Seschrock 			zb.zb_blkid = lrw->lr_offset / BP_GET_LSIZE(wbp);
13321544Seschrock 
1333789Sahrens 			(void) zio_wait(zio_read(NULL, zilog->zl_spa,
1334789Sahrens 			    wbp, wbuf, BP_GET_LSIZE(wbp), NULL, NULL,
1335789Sahrens 			    ZIO_PRIORITY_SYNC_READ,
13361544Seschrock 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &zb));
1337789Sahrens 			(void) memmove(wbuf, wbuf + lrw->lr_blkoff, wlen);
1338789Sahrens 		}
1339789Sahrens 	}
1340789Sahrens 
1341789Sahrens 	/*
1342789Sahrens 	 * We must now do two things atomically: replay this log record,
1343789Sahrens 	 * and update the log header to reflect the fact that we did so.
1344789Sahrens 	 * We use the DMU's ability to assign into a specific txg to do this.
1345789Sahrens 	 */
1346789Sahrens 	for (pass = 1; /* CONSTANTCONDITION */; pass++) {
1347789Sahrens 		uint64_t replay_txg;
1348789Sahrens 		dmu_tx_t *replay_tx;
1349789Sahrens 
1350789Sahrens 		replay_tx = dmu_tx_create(zr->zr_os);
1351789Sahrens 		error = dmu_tx_assign(replay_tx, TXG_WAIT);
1352789Sahrens 		if (error) {
1353789Sahrens 			dmu_tx_abort(replay_tx);
1354789Sahrens 			break;
1355789Sahrens 		}
1356789Sahrens 
1357789Sahrens 		replay_txg = dmu_tx_get_txg(replay_tx);
1358789Sahrens 
1359789Sahrens 		if (txtype == 0 || txtype >= TX_MAX_TYPE) {
1360789Sahrens 			error = EINVAL;
1361789Sahrens 		} else {
1362789Sahrens 			/*
1363789Sahrens 			 * On the first pass, arrange for the replay vector
1364789Sahrens 			 * to fail its dmu_tx_assign().  That's the only way
1365789Sahrens 			 * to ensure that those code paths remain well tested.
1366789Sahrens 			 */
1367789Sahrens 			*zr->zr_txgp = replay_txg - (pass == 1);
1368789Sahrens 			error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lrbuf,
1369789Sahrens 			    zr->zr_byteswap);
1370789Sahrens 			*zr->zr_txgp = TXG_NOWAIT;
1371789Sahrens 		}
1372789Sahrens 
1373789Sahrens 		if (error == 0) {
1374789Sahrens 			dsl_dataset_dirty(dmu_objset_ds(zr->zr_os), replay_tx);
1375789Sahrens 			zilog->zl_replay_seq[replay_txg & TXG_MASK] =
1376789Sahrens 			    lr->lrc_seq;
1377789Sahrens 		}
1378789Sahrens 
1379789Sahrens 		dmu_tx_commit(replay_tx);
1380789Sahrens 
1381789Sahrens 		if (error != ERESTART)
1382789Sahrens 			break;
1383789Sahrens 
1384789Sahrens 		if (pass != 1)
1385789Sahrens 			txg_wait_open(spa_get_dsl(zilog->zl_spa),
1386789Sahrens 			    replay_txg + 1);
1387789Sahrens 
1388789Sahrens 		dprintf("pass %d, retrying\n", pass);
1389789Sahrens 	}
1390789Sahrens 
1391789Sahrens 	if (error) {
1392789Sahrens 		char *name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1393789Sahrens 		dmu_objset_name(zr->zr_os, name);
1394789Sahrens 		cmn_err(CE_WARN, "ZFS replay transaction error %d, "
1395789Sahrens 		    "dataset %s, seq 0x%llx, txtype %llu\n",
1396789Sahrens 		    error, name,
1397789Sahrens 		    (u_longlong_t)lr->lrc_seq, (u_longlong_t)txtype);
1398789Sahrens 		zilog->zl_stop_replay = 1;
1399789Sahrens 		kmem_free(name, MAXNAMELEN);
1400789Sahrens 	}
1401789Sahrens 
1402789Sahrens 	/*
1403789Sahrens 	 * The DMU's dnode layer doesn't see removes until the txg commits,
1404789Sahrens 	 * so a subsequent claim can spuriously fail with EEXIST.
1405789Sahrens 	 * To prevent this, if we might have removed an object,
1406789Sahrens 	 * wait for the delete thread to delete it, and then
1407789Sahrens 	 * wait for the transaction group to sync.
1408789Sahrens 	 */
1409789Sahrens 	if (txtype == TX_REMOVE || txtype == TX_RMDIR || txtype == TX_RENAME) {
1410789Sahrens 		if (zr->zr_rm_sync != NULL)
1411789Sahrens 			zr->zr_rm_sync(zr->zr_arg);
1412789Sahrens 		txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0);
1413789Sahrens 	}
1414789Sahrens }
1415789Sahrens 
1416789Sahrens /*
14171362Sperrin  * If this dataset has a non-empty intent log, replay it and destroy it.
1418789Sahrens  */
1419789Sahrens void
1420789Sahrens zil_replay(objset_t *os, void *arg, uint64_t *txgp,
1421789Sahrens 	zil_replay_func_t *replay_func[TX_MAX_TYPE], void (*rm_sync)(void *arg))
1422789Sahrens {
1423789Sahrens 	zilog_t *zilog = dmu_objset_zil(os);
14241807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
14251807Sbonwick 	zil_replay_arg_t zr;
14261362Sperrin 
14271362Sperrin 	if (zil_empty(zilog)) {
14281807Sbonwick 		zil_destroy(zilog, B_TRUE);
14291362Sperrin 		return;
14301362Sperrin 	}
1431789Sahrens 
1432789Sahrens 	zr.zr_os = os;
1433789Sahrens 	zr.zr_replay = replay_func;
1434789Sahrens 	zr.zr_arg = arg;
1435789Sahrens 	zr.zr_rm_sync = rm_sync;
1436789Sahrens 	zr.zr_txgp = txgp;
14371807Sbonwick 	zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log);
1438789Sahrens 	zr.zr_lrbuf = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP);
1439789Sahrens 
1440789Sahrens 	/*
1441789Sahrens 	 * Wait for in-progress removes to sync before starting replay.
1442789Sahrens 	 */
1443789Sahrens 	if (rm_sync != NULL)
1444789Sahrens 		rm_sync(arg);
1445789Sahrens 	txg_wait_synced(zilog->zl_dmu_pool, 0);
1446789Sahrens 
1447789Sahrens 	zilog->zl_stop_replay = 0;
14481807Sbonwick 	(void) zil_parse(zilog, NULL, zil_replay_log_record, &zr,
14491807Sbonwick 	    zh->zh_claim_txg);
1450789Sahrens 	kmem_free(zr.zr_lrbuf, 2 * SPA_MAXBLOCKSIZE);
1451789Sahrens 
14521807Sbonwick 	zil_destroy(zilog, B_FALSE);
1453789Sahrens }
14541646Sperrin 
14551646Sperrin /*
14561646Sperrin  * Report whether all transactions are committed
14571646Sperrin  */
14581646Sperrin int
14591646Sperrin zil_is_committed(zilog_t *zilog)
14601646Sperrin {
14611646Sperrin 	lwb_t *lwb;
14622638Sperrin 	int ret;
14631646Sperrin 
14642638Sperrin 	mutex_enter(&zilog->zl_lock);
14652638Sperrin 	while (zilog->zl_writer)
14662638Sperrin 		cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
14672638Sperrin 
14682638Sperrin 	/* recent unpushed intent log transactions? */
14692638Sperrin 	if (!list_is_empty(&zilog->zl_itx_list)) {
14702638Sperrin 		ret = B_FALSE;
14712638Sperrin 		goto out;
14722638Sperrin 	}
14732638Sperrin 
14742638Sperrin 	/* intent log never used? */
14752638Sperrin 	lwb = list_head(&zilog->zl_lwb_list);
14762638Sperrin 	if (lwb == NULL) {
14772638Sperrin 		ret = B_TRUE;
14782638Sperrin 		goto out;
14792638Sperrin 	}
14801646Sperrin 
14811646Sperrin 	/*
14822638Sperrin 	 * more than 1 log buffer means zil_sync() hasn't yet freed
14832638Sperrin 	 * entries after a txg has committed
14841646Sperrin 	 */
14852638Sperrin 	if (list_next(&zilog->zl_lwb_list, lwb)) {
14862638Sperrin 		ret = B_FALSE;
14872638Sperrin 		goto out;
14882638Sperrin 	}
14892638Sperrin 
14901646Sperrin 	ASSERT(zil_empty(zilog));
14912638Sperrin 	ret = B_TRUE;
14922638Sperrin out:
14932638Sperrin 	cv_broadcast(&zilog->zl_cv_writer);
14942638Sperrin 	mutex_exit(&zilog->zl_lock);
14952638Sperrin 	return (ret);
14961646Sperrin }
1497