xref: /onnv-gate/usr/src/uts/common/fs/zfs/zil.c (revision 2237:45affe88ed99)
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 /*
67789Sahrens  * These global ZIL switches affect all pools
68789Sahrens  */
69789Sahrens int zil_disable = 0;	/* disable intent logging */
70789Sahrens int zil_always = 0;	/* make every transaction synchronous */
71789Sahrens int zil_purge = 0;	/* at pool open, just throw everything away */
72789Sahrens int zil_noflush = 0;	/* don't flush write cache buffers on disks */
73789Sahrens 
74789Sahrens static kmem_cache_t *zil_lwb_cache;
75789Sahrens 
76789Sahrens static int
77789Sahrens zil_dva_compare(const void *x1, const void *x2)
78789Sahrens {
79789Sahrens 	const dva_t *dva1 = x1;
80789Sahrens 	const dva_t *dva2 = x2;
81789Sahrens 
82789Sahrens 	if (DVA_GET_VDEV(dva1) < DVA_GET_VDEV(dva2))
83789Sahrens 		return (-1);
84789Sahrens 	if (DVA_GET_VDEV(dva1) > DVA_GET_VDEV(dva2))
85789Sahrens 		return (1);
86789Sahrens 
87789Sahrens 	if (DVA_GET_OFFSET(dva1) < DVA_GET_OFFSET(dva2))
88789Sahrens 		return (-1);
89789Sahrens 	if (DVA_GET_OFFSET(dva1) > DVA_GET_OFFSET(dva2))
90789Sahrens 		return (1);
91789Sahrens 
92789Sahrens 	return (0);
93789Sahrens }
94789Sahrens 
95789Sahrens static void
96789Sahrens zil_dva_tree_init(avl_tree_t *t)
97789Sahrens {
98789Sahrens 	avl_create(t, zil_dva_compare, sizeof (zil_dva_node_t),
99789Sahrens 	    offsetof(zil_dva_node_t, zn_node));
100789Sahrens }
101789Sahrens 
102789Sahrens static void
103789Sahrens zil_dva_tree_fini(avl_tree_t *t)
104789Sahrens {
105789Sahrens 	zil_dva_node_t *zn;
106789Sahrens 	void *cookie = NULL;
107789Sahrens 
108789Sahrens 	while ((zn = avl_destroy_nodes(t, &cookie)) != NULL)
109789Sahrens 		kmem_free(zn, sizeof (zil_dva_node_t));
110789Sahrens 
111789Sahrens 	avl_destroy(t);
112789Sahrens }
113789Sahrens 
114789Sahrens static int
115789Sahrens zil_dva_tree_add(avl_tree_t *t, dva_t *dva)
116789Sahrens {
117789Sahrens 	zil_dva_node_t *zn;
118789Sahrens 	avl_index_t where;
119789Sahrens 
120789Sahrens 	if (avl_find(t, dva, &where) != NULL)
121789Sahrens 		return (EEXIST);
122789Sahrens 
123789Sahrens 	zn = kmem_alloc(sizeof (zil_dva_node_t), KM_SLEEP);
124789Sahrens 	zn->zn_dva = *dva;
125789Sahrens 	avl_insert(t, zn, where);
126789Sahrens 
127789Sahrens 	return (0);
128789Sahrens }
129789Sahrens 
1301807Sbonwick static zil_header_t *
1311807Sbonwick zil_header_in_syncing_context(zilog_t *zilog)
1321807Sbonwick {
1331807Sbonwick 	return ((zil_header_t *)zilog->zl_header);
1341807Sbonwick }
1351807Sbonwick 
1361807Sbonwick static void
1371807Sbonwick zil_init_log_chain(zilog_t *zilog, blkptr_t *bp)
1381807Sbonwick {
1391807Sbonwick 	zio_cksum_t *zc = &bp->blk_cksum;
1401807Sbonwick 
1411807Sbonwick 	zc->zc_word[ZIL_ZC_GUID_0] = spa_get_random(-1ULL);
1421807Sbonwick 	zc->zc_word[ZIL_ZC_GUID_1] = spa_get_random(-1ULL);
1431807Sbonwick 	zc->zc_word[ZIL_ZC_OBJSET] = dmu_objset_id(zilog->zl_os);
1441807Sbonwick 	zc->zc_word[ZIL_ZC_SEQ] = 1ULL;
1451807Sbonwick }
1461807Sbonwick 
147789Sahrens /*
148789Sahrens  * Read a log block, make sure it's valid, and byteswap it if necessary.
149789Sahrens  */
150789Sahrens static int
1511807Sbonwick zil_read_log_block(zilog_t *zilog, const blkptr_t *bp, arc_buf_t **abufpp)
152789Sahrens {
1531807Sbonwick 	blkptr_t blk = *bp;
1541544Seschrock 	zbookmark_t zb;
155789Sahrens 	int error;
156789Sahrens 
1571807Sbonwick 	zb.zb_objset = bp->blk_cksum.zc_word[ZIL_ZC_OBJSET];
1581544Seschrock 	zb.zb_object = 0;
1591544Seschrock 	zb.zb_level = -1;
1601807Sbonwick 	zb.zb_blkid = bp->blk_cksum.zc_word[ZIL_ZC_SEQ];
1611807Sbonwick 
1621807Sbonwick 	*abufpp = NULL;
1631807Sbonwick 
1641807Sbonwick 	error = arc_read(NULL, zilog->zl_spa, &blk, byteswap_uint64_array,
1651807Sbonwick 	    arc_getbuf_func, abufpp, ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL |
1661807Sbonwick 	    ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB, ARC_WAIT, &zb);
1671807Sbonwick 
1681807Sbonwick 	if (error == 0) {
1691807Sbonwick 		char *data = (*abufpp)->b_data;
1701807Sbonwick 		uint64_t blksz = BP_GET_LSIZE(bp);
1711807Sbonwick 		zil_trailer_t *ztp = (zil_trailer_t *)(data + blksz) - 1;
1721807Sbonwick 		zio_cksum_t cksum = bp->blk_cksum;
1731544Seschrock 
1741807Sbonwick 		/*
1751807Sbonwick 		 * Sequence numbers should be... sequential.  The checksum
1761807Sbonwick 		 * verifier for the next block should be bp's checksum plus 1.
1771807Sbonwick 		 */
1781807Sbonwick 		cksum.zc_word[ZIL_ZC_SEQ]++;
1791807Sbonwick 
1801807Sbonwick 		if (bcmp(&cksum, &ztp->zit_next_blk.blk_cksum, sizeof (cksum)))
1811807Sbonwick 			error = ESTALE;
1821807Sbonwick 		else if (BP_IS_HOLE(&ztp->zit_next_blk))
1831807Sbonwick 			error = ENOENT;
1841807Sbonwick 		else if (ztp->zit_nused > (blksz - sizeof (zil_trailer_t)))
1851807Sbonwick 			error = EOVERFLOW;
1861807Sbonwick 
1871807Sbonwick 		if (error) {
1881807Sbonwick 			VERIFY(arc_buf_remove_ref(*abufpp, abufpp) == 1);
1891807Sbonwick 			*abufpp = NULL;
1901807Sbonwick 		}
191789Sahrens 	}
192789Sahrens 
1931807Sbonwick 	dprintf("error %d on %llu:%llu\n", error, zb.zb_objset, zb.zb_blkid);
194789Sahrens 
1951807Sbonwick 	return (error);
196789Sahrens }
197789Sahrens 
198789Sahrens /*
199789Sahrens  * Parse the intent log, and call parse_func for each valid record within.
2001807Sbonwick  * Return the highest sequence number.
201789Sahrens  */
2021807Sbonwick uint64_t
203789Sahrens zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func,
204789Sahrens     zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg)
205789Sahrens {
2061807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
2071807Sbonwick 	uint64_t claim_seq = zh->zh_claim_seq;
2081807Sbonwick 	uint64_t seq = 0;
2091807Sbonwick 	uint64_t max_seq = 0;
2101807Sbonwick 	blkptr_t blk = zh->zh_log;
2111807Sbonwick 	arc_buf_t *abuf;
212789Sahrens 	char *lrbuf, *lrp;
213789Sahrens 	zil_trailer_t *ztp;
214789Sahrens 	int reclen, error;
215789Sahrens 
216789Sahrens 	if (BP_IS_HOLE(&blk))
2171807Sbonwick 		return (max_seq);
218789Sahrens 
219789Sahrens 	/*
220789Sahrens 	 * Starting at the block pointed to by zh_log we read the log chain.
221789Sahrens 	 * For each block in the chain we strongly check that block to
222789Sahrens 	 * ensure its validity.  We stop when an invalid block is found.
223789Sahrens 	 * For each block pointer in the chain we call parse_blk_func().
224789Sahrens 	 * For each record in each valid block we call parse_lr_func().
2251807Sbonwick 	 * If the log has been claimed, stop if we encounter a sequence
2261807Sbonwick 	 * number greater than the highest claimed sequence number.
227789Sahrens 	 */
228789Sahrens 	zil_dva_tree_init(&zilog->zl_dva_tree);
229789Sahrens 	for (;;) {
2301807Sbonwick 		seq = blk.blk_cksum.zc_word[ZIL_ZC_SEQ];
2311807Sbonwick 
2321807Sbonwick 		if (claim_seq != 0 && seq > claim_seq)
2331807Sbonwick 			break;
2341807Sbonwick 
2351807Sbonwick 		ASSERT(max_seq < seq);
2361807Sbonwick 		max_seq = seq;
2371807Sbonwick 
2381807Sbonwick 		error = zil_read_log_block(zilog, &blk, &abuf);
239789Sahrens 
240789Sahrens 		if (parse_blk_func != NULL)
241789Sahrens 			parse_blk_func(zilog, &blk, arg, txg);
242789Sahrens 
243789Sahrens 		if (error)
244789Sahrens 			break;
245789Sahrens 
2461807Sbonwick 		lrbuf = abuf->b_data;
247789Sahrens 		ztp = (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1;
248789Sahrens 		blk = ztp->zit_next_blk;
249789Sahrens 
2501807Sbonwick 		if (parse_lr_func == NULL) {
2511807Sbonwick 			VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
252789Sahrens 			continue;
2531807Sbonwick 		}
254789Sahrens 
255789Sahrens 		for (lrp = lrbuf; lrp < lrbuf + ztp->zit_nused; lrp += reclen) {
256789Sahrens 			lr_t *lr = (lr_t *)lrp;
257789Sahrens 			reclen = lr->lrc_reclen;
258789Sahrens 			ASSERT3U(reclen, >=, sizeof (lr_t));
259789Sahrens 			parse_lr_func(zilog, lr, arg, txg);
260789Sahrens 		}
2611807Sbonwick 		VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
262789Sahrens 	}
263789Sahrens 	zil_dva_tree_fini(&zilog->zl_dva_tree);
2641807Sbonwick 
2651807Sbonwick 	return (max_seq);
266789Sahrens }
267789Sahrens 
268789Sahrens /* ARGSUSED */
269789Sahrens static void
270789Sahrens zil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg)
271789Sahrens {
272789Sahrens 	spa_t *spa = zilog->zl_spa;
273789Sahrens 	int err;
274789Sahrens 
275789Sahrens 	/*
276789Sahrens 	 * Claim log block if not already committed and not already claimed.
277789Sahrens 	 */
278789Sahrens 	if (bp->blk_birth >= first_txg &&
279789Sahrens 	    zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp)) == 0) {
280789Sahrens 		err = zio_wait(zio_claim(NULL, spa, first_txg, bp, NULL, NULL));
281789Sahrens 		ASSERT(err == 0);
282789Sahrens 	}
283789Sahrens }
284789Sahrens 
285789Sahrens static void
286789Sahrens zil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg)
287789Sahrens {
288789Sahrens 	if (lrc->lrc_txtype == TX_WRITE) {
289789Sahrens 		lr_write_t *lr = (lr_write_t *)lrc;
290789Sahrens 		zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg);
291789Sahrens 	}
292789Sahrens }
293789Sahrens 
294789Sahrens /* ARGSUSED */
295789Sahrens static void
296789Sahrens zil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg)
297789Sahrens {
298789Sahrens 	zio_free_blk(zilog->zl_spa, bp, dmu_tx_get_txg(tx));
299789Sahrens }
300789Sahrens 
301789Sahrens static void
302789Sahrens zil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg)
303789Sahrens {
304789Sahrens 	/*
305789Sahrens 	 * If we previously claimed it, we need to free it.
306789Sahrens 	 */
307789Sahrens 	if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE) {
308789Sahrens 		lr_write_t *lr = (lr_write_t *)lrc;
309789Sahrens 		blkptr_t *bp = &lr->lr_blkptr;
310789Sahrens 		if (bp->blk_birth >= claim_txg &&
311789Sahrens 		    !zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp))) {
312789Sahrens 			(void) arc_free(NULL, zilog->zl_spa,
313789Sahrens 			    dmu_tx_get_txg(tx), bp, NULL, NULL, ARC_WAIT);
314789Sahrens 		}
315789Sahrens 	}
316789Sahrens }
317789Sahrens 
318789Sahrens /*
319789Sahrens  * Create an on-disk intent log.
320789Sahrens  */
321789Sahrens static void
322789Sahrens zil_create(zilog_t *zilog)
323789Sahrens {
3241807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
325789Sahrens 	lwb_t *lwb;
3261807Sbonwick 	uint64_t txg = 0;
3271807Sbonwick 	dmu_tx_t *tx = NULL;
328789Sahrens 	blkptr_t blk;
3291807Sbonwick 	int error = 0;
330789Sahrens 
331789Sahrens 	/*
3321807Sbonwick 	 * Wait for any previous destroy to complete.
333789Sahrens 	 */
3341807Sbonwick 	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
3351807Sbonwick 
3361807Sbonwick 	ASSERT(zh->zh_claim_txg == 0);
3371807Sbonwick 	ASSERT(zh->zh_replay_seq == 0);
3381807Sbonwick 
3391807Sbonwick 	blk = zh->zh_log;
340789Sahrens 
341789Sahrens 	/*
3421807Sbonwick 	 * If we don't already have an initial log block, allocate one now.
343789Sahrens 	 */
3441807Sbonwick 	if (BP_IS_HOLE(&blk)) {
3451807Sbonwick 		tx = dmu_tx_create(zilog->zl_os);
3461807Sbonwick 		(void) dmu_tx_assign(tx, TXG_WAIT);
3471807Sbonwick 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
3481807Sbonwick 		txg = dmu_tx_get_txg(tx);
3491807Sbonwick 
3501807Sbonwick 		error = zio_alloc_blk(zilog->zl_spa, ZIL_MIN_BLKSZ, &blk, txg);
3511807Sbonwick 
3521807Sbonwick 		if (error == 0)
3531807Sbonwick 			zil_init_log_chain(zilog, &blk);
3541362Sperrin 	}
3551807Sbonwick 
3561807Sbonwick 	/*
3571807Sbonwick 	 * Allocate a log write buffer (lwb) for the first log block.
3581807Sbonwick 	 */
359789Sahrens 	if (error == 0) {
360789Sahrens 		lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP);
361789Sahrens 		lwb->lwb_zilog = zilog;
362789Sahrens 		lwb->lwb_blk = blk;
363789Sahrens 		lwb->lwb_nused = 0;
364789Sahrens 		lwb->lwb_sz = BP_GET_LSIZE(&lwb->lwb_blk);
365789Sahrens 		lwb->lwb_buf = zio_buf_alloc(lwb->lwb_sz);
366789Sahrens 		lwb->lwb_max_txg = txg;
367789Sahrens 		lwb->lwb_seq = 0;
368789Sahrens 		lwb->lwb_state = UNWRITTEN;
369*2237Smaybee 		lwb->lwb_zio = NULL;
370*2237Smaybee 
371789Sahrens 		mutex_enter(&zilog->zl_lock);
372789Sahrens 		list_insert_tail(&zilog->zl_lwb_list, lwb);
373789Sahrens 		mutex_exit(&zilog->zl_lock);
374789Sahrens 	}
375789Sahrens 
3761807Sbonwick 	/*
3771807Sbonwick 	 * If we just allocated the first log block, commit our transaction
3781807Sbonwick 	 * and wait for zil_sync() to stuff the block poiner into zh_log.
3791807Sbonwick 	 * (zh is part of the MOS, so we cannot modify it in open context.)
3801807Sbonwick 	 */
3811807Sbonwick 	if (tx != NULL) {
3821807Sbonwick 		dmu_tx_commit(tx);
3831362Sperrin 		txg_wait_synced(zilog->zl_dmu_pool, txg);
3841807Sbonwick 	}
3851807Sbonwick 
3861807Sbonwick 	ASSERT(bcmp(&blk, &zh->zh_log, sizeof (blk)) == 0);
387789Sahrens }
388789Sahrens 
389789Sahrens /*
390789Sahrens  * In one tx, free all log blocks and clear the log header.
3911807Sbonwick  * If keep_first is set, then we're replaying a log with no content.
3921807Sbonwick  * We want to keep the first block, however, so that the first
3931807Sbonwick  * synchronous transaction doesn't require a txg_wait_synced()
3941807Sbonwick  * in zil_create().  We don't need to txg_wait_synced() here either
3951807Sbonwick  * when keep_first is set, because both zil_create() and zil_destroy()
3961807Sbonwick  * will wait for any in-progress destroys to complete.
397789Sahrens  */
398789Sahrens void
3991807Sbonwick zil_destroy(zilog_t *zilog, boolean_t keep_first)
400789Sahrens {
4011807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
4021807Sbonwick 	lwb_t *lwb;
403789Sahrens 	dmu_tx_t *tx;
404789Sahrens 	uint64_t txg;
405789Sahrens 
4061807Sbonwick 	/*
4071807Sbonwick 	 * Wait for any previous destroy to complete.
4081807Sbonwick 	 */
4091807Sbonwick 	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
410789Sahrens 
4111807Sbonwick 	if (BP_IS_HOLE(&zh->zh_log))
412789Sahrens 		return;
413789Sahrens 
414789Sahrens 	tx = dmu_tx_create(zilog->zl_os);
415789Sahrens 	(void) dmu_tx_assign(tx, TXG_WAIT);
416789Sahrens 	dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
417789Sahrens 	txg = dmu_tx_get_txg(tx);
418789Sahrens 
4191807Sbonwick 	mutex_enter(&zilog->zl_lock);
4201807Sbonwick 
4211807Sbonwick 	ASSERT3U(zilog->zl_destroy_txg, <, txg);
422789Sahrens 	zilog->zl_destroy_txg = txg;
4231807Sbonwick 	zilog->zl_keep_first = keep_first;
4241807Sbonwick 
4251807Sbonwick 	if (!list_is_empty(&zilog->zl_lwb_list)) {
4261807Sbonwick 		ASSERT(zh->zh_claim_txg == 0);
4271807Sbonwick 		ASSERT(!keep_first);
4281807Sbonwick 		while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
4291807Sbonwick 			list_remove(&zilog->zl_lwb_list, lwb);
4301807Sbonwick 			if (lwb->lwb_buf != NULL)
4311807Sbonwick 				zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
4321807Sbonwick 			zio_free_blk(zilog->zl_spa, &lwb->lwb_blk, txg);
4331807Sbonwick 			kmem_cache_free(zil_lwb_cache, lwb);
4341807Sbonwick 		}
4351807Sbonwick 		mutex_exit(&zilog->zl_lock);
4361807Sbonwick 	} else {
4371807Sbonwick 		mutex_exit(&zilog->zl_lock);
4381807Sbonwick 		if (!keep_first) {
4391807Sbonwick 			(void) zil_parse(zilog, zil_free_log_block,
4401807Sbonwick 			    zil_free_log_record, tx, zh->zh_claim_txg);
4411807Sbonwick 		}
4421807Sbonwick 	}
443789Sahrens 
444789Sahrens 	dmu_tx_commit(tx);
445789Sahrens 
4461807Sbonwick 	if (keep_first)			/* no need to wait in this case */
4471807Sbonwick 		return;
4481807Sbonwick 
4491807Sbonwick 	txg_wait_synced(zilog->zl_dmu_pool, txg);
4501807Sbonwick 	ASSERT(BP_IS_HOLE(&zh->zh_log));
451789Sahrens }
452789Sahrens 
4532199Sahrens int
454789Sahrens zil_claim(char *osname, void *txarg)
455789Sahrens {
456789Sahrens 	dmu_tx_t *tx = txarg;
457789Sahrens 	uint64_t first_txg = dmu_tx_get_txg(tx);
458789Sahrens 	zilog_t *zilog;
459789Sahrens 	zil_header_t *zh;
460789Sahrens 	objset_t *os;
461789Sahrens 	int error;
462789Sahrens 
463789Sahrens 	error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_STANDARD, &os);
464789Sahrens 	if (error) {
465789Sahrens 		cmn_err(CE_WARN, "can't process intent log for %s", osname);
4662199Sahrens 		return (0);
467789Sahrens 	}
468789Sahrens 
469789Sahrens 	zilog = dmu_objset_zil(os);
4701807Sbonwick 	zh = zil_header_in_syncing_context(zilog);
471789Sahrens 
472789Sahrens 	/*
4731807Sbonwick 	 * Claim all log blocks if we haven't already done so, and remember
4741807Sbonwick 	 * the highest claimed sequence number.  This ensures that if we can
4751807Sbonwick 	 * read only part of the log now (e.g. due to a missing device),
4761807Sbonwick 	 * but we can read the entire log later, we will not try to replay
4771807Sbonwick 	 * or destroy beyond the last block we successfully claimed.
478789Sahrens 	 */
479789Sahrens 	ASSERT3U(zh->zh_claim_txg, <=, first_txg);
480789Sahrens 	if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) {
481789Sahrens 		zh->zh_claim_txg = first_txg;
4821807Sbonwick 		zh->zh_claim_seq = zil_parse(zilog, zil_claim_log_block,
4831807Sbonwick 		    zil_claim_log_record, tx, first_txg);
484789Sahrens 		dsl_dataset_dirty(dmu_objset_ds(os), tx);
485789Sahrens 	}
4861807Sbonwick 
487789Sahrens 	ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1));
488789Sahrens 	dmu_objset_close(os);
4892199Sahrens 	return (0);
490789Sahrens }
491789Sahrens 
492789Sahrens void
493789Sahrens zil_add_vdev(zilog_t *zilog, uint64_t vdev, uint64_t seq)
494789Sahrens {
495789Sahrens 	zil_vdev_t *zv;
496789Sahrens 
497789Sahrens 	if (zil_noflush)
498789Sahrens 		return;
499789Sahrens 
500789Sahrens 	ASSERT(MUTEX_HELD(&zilog->zl_lock));
501789Sahrens 	zv = kmem_alloc(sizeof (zil_vdev_t), KM_SLEEP);
502789Sahrens 	zv->vdev = vdev;
503789Sahrens 	zv->seq = seq;
504789Sahrens 	list_insert_tail(&zilog->zl_vdev_list, zv);
505789Sahrens }
506789Sahrens 
507789Sahrens void
508789Sahrens zil_flush_vdevs(zilog_t *zilog, uint64_t seq)
509789Sahrens {
510789Sahrens 	vdev_t *vd;
511789Sahrens 	zil_vdev_t *zv, *zv2;
512789Sahrens 	zio_t *zio;
513789Sahrens 	spa_t *spa;
514789Sahrens 	uint64_t vdev;
515789Sahrens 
516789Sahrens 	if (zil_noflush)
517789Sahrens 		return;
518789Sahrens 
519789Sahrens 	ASSERT(MUTEX_HELD(&zilog->zl_lock));
520789Sahrens 
521789Sahrens 	spa = zilog->zl_spa;
522789Sahrens 	zio = NULL;
523789Sahrens 
524789Sahrens 	while ((zv = list_head(&zilog->zl_vdev_list)) != NULL &&
525789Sahrens 	    zv->seq <= seq) {
526789Sahrens 		vdev = zv->vdev;
527789Sahrens 		list_remove(&zilog->zl_vdev_list, zv);
528789Sahrens 		kmem_free(zv, sizeof (zil_vdev_t));
529789Sahrens 
530789Sahrens 		/*
531789Sahrens 		 * remove all chained entries <= seq with same vdev
532789Sahrens 		 */
533789Sahrens 		zv = list_head(&zilog->zl_vdev_list);
534789Sahrens 		while (zv && zv->seq <= seq) {
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 *prev;
573789Sahrens 	lwb_t *lwb = zio->io_private;
574789Sahrens 	zilog_t *zilog = lwb->lwb_zilog;
575789Sahrens 	uint64_t max_seq;
576789Sahrens 
577789Sahrens 	/*
578789Sahrens 	 * Now that we've written this log block, we have a stable pointer
579789Sahrens 	 * to the next block in the chain, so it's OK to let the txg in
580789Sahrens 	 * which we allocated the next block sync.
581789Sahrens 	 */
582789Sahrens 	txg_rele_to_sync(&lwb->lwb_txgh);
583789Sahrens 
584789Sahrens 	zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
585789Sahrens 	mutex_enter(&zilog->zl_lock);
586789Sahrens 	lwb->lwb_buf = NULL;
587789Sahrens 	if (zio->io_error) {
588789Sahrens 		zilog->zl_log_error = B_TRUE;
589789Sahrens 		mutex_exit(&zilog->zl_lock);
590789Sahrens 		cv_broadcast(&zilog->zl_cv_seq);
591789Sahrens 		return;
592789Sahrens 	}
593789Sahrens 
594789Sahrens 	prev = list_prev(&zilog->zl_lwb_list, lwb);
595789Sahrens 	if (prev && prev->lwb_state != SEQ_COMPLETE) {
596789Sahrens 		/* There's an unwritten buffer in the chain before this one */
597789Sahrens 		lwb->lwb_state = SEQ_INCOMPLETE;
598789Sahrens 		mutex_exit(&zilog->zl_lock);
599789Sahrens 		return;
600789Sahrens 	}
601789Sahrens 
602789Sahrens 	max_seq = lwb->lwb_seq;
603789Sahrens 	lwb->lwb_state = SEQ_COMPLETE;
604789Sahrens 	/*
605789Sahrens 	 * We must also follow up the chain for already written buffers
606789Sahrens 	 * to see if we can set zl_ss_seq even higher.
607789Sahrens 	 */
608789Sahrens 	while (lwb = list_next(&zilog->zl_lwb_list, lwb)) {
609789Sahrens 		if (lwb->lwb_state != SEQ_INCOMPLETE)
610789Sahrens 			break;
611789Sahrens 		lwb->lwb_state = SEQ_COMPLETE;
612789Sahrens 		/* lwb_seq will be zero if we've written an empty buffer */
613789Sahrens 		if (lwb->lwb_seq) {
614789Sahrens 			ASSERT3U(max_seq, <, lwb->lwb_seq);
615789Sahrens 			max_seq = lwb->lwb_seq;
616789Sahrens 		}
617789Sahrens 	}
618789Sahrens 	zilog->zl_ss_seq = MAX(max_seq, zilog->zl_ss_seq);
619789Sahrens 	mutex_exit(&zilog->zl_lock);
620789Sahrens 	cv_broadcast(&zilog->zl_cv_seq);
621789Sahrens }
622789Sahrens 
623789Sahrens /*
624*2237Smaybee  * Initialize the io for a log block.
625*2237Smaybee  *
626*2237Smaybee  * Note, we should not initialize the IO until we are about
627*2237Smaybee  * to use it, since zio_rewrite() does a spa_config_enter().
628*2237Smaybee  */
629*2237Smaybee static void
630*2237Smaybee zil_lwb_write_init(zilog_t *zilog, lwb_t *lwb)
631*2237Smaybee {
632*2237Smaybee 	zbookmark_t zb;
633*2237Smaybee 
634*2237Smaybee 	zb.zb_objset = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET];
635*2237Smaybee 	zb.zb_object = 0;
636*2237Smaybee 	zb.zb_level = -1;
637*2237Smaybee 	zb.zb_blkid = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ];
638*2237Smaybee 
639*2237Smaybee 	ASSERT(lwb->lwb_zio == NULL);
640*2237Smaybee 	lwb->lwb_zio = zio_rewrite(NULL, zilog->zl_spa,
641*2237Smaybee 	    ZIO_CHECKSUM_ZILOG, 0, &lwb->lwb_blk, lwb->lwb_buf,
642*2237Smaybee 	    lwb->lwb_sz, zil_lwb_write_done, lwb,
643*2237Smaybee 	    ZIO_PRIORITY_LOG_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
644*2237Smaybee }
645*2237Smaybee 
646*2237Smaybee /*
647789Sahrens  * Start a log block write and advance to the next log block.
648789Sahrens  * Calls are serialized.
649789Sahrens  */
650789Sahrens static lwb_t *
651789Sahrens zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb)
652789Sahrens {
653789Sahrens 	lwb_t *nlwb;
654789Sahrens 	zil_trailer_t *ztp = (zil_trailer_t *)(lwb->lwb_buf + lwb->lwb_sz) - 1;
6551807Sbonwick 	spa_t *spa = zilog->zl_spa;
6561807Sbonwick 	blkptr_t *bp = &ztp->zit_next_blk;
657789Sahrens 	uint64_t txg;
658789Sahrens 	uint64_t zil_blksz;
659789Sahrens 	int error;
660789Sahrens 
661789Sahrens 	ASSERT(lwb->lwb_nused <= ZIL_BLK_DATA_SZ(lwb));
662789Sahrens 
663789Sahrens 	/*
664789Sahrens 	 * Allocate the next block and save its address in this block
665789Sahrens 	 * before writing it in order to establish the log chain.
666789Sahrens 	 * Note that if the allocation of nlwb synced before we wrote
667789Sahrens 	 * the block that points at it (lwb), we'd leak it if we crashed.
668789Sahrens 	 * Therefore, we don't do txg_rele_to_sync() until zil_lwb_write_done().
669789Sahrens 	 */
670789Sahrens 	txg = txg_hold_open(zilog->zl_dmu_pool, &lwb->lwb_txgh);
671789Sahrens 	txg_rele_to_quiesce(&lwb->lwb_txgh);
672789Sahrens 
673789Sahrens 	/*
6741141Sperrin 	 * Pick a ZIL blocksize. We request a size that is the
6751141Sperrin 	 * maximum of the previous used size, the current used size and
6761141Sperrin 	 * the amount waiting in the queue.
677789Sahrens 	 */
678*2237Smaybee 	zil_blksz = MAX(zilog->zl_prev_used,
679*2237Smaybee 	    zilog->zl_cur_used + sizeof (*ztp));
6801141Sperrin 	zil_blksz = MAX(zil_blksz, zilog->zl_itx_list_sz + sizeof (*ztp));
6811842Sperrin 	zil_blksz = P2ROUNDUP_TYPED(zil_blksz, ZIL_MIN_BLKSZ, uint64_t);
6821141Sperrin 	if (zil_blksz > ZIL_MAX_BLKSZ)
6831141Sperrin 		zil_blksz = ZIL_MAX_BLKSZ;
684789Sahrens 
6851807Sbonwick 	error = zio_alloc_blk(spa, zil_blksz, bp, txg);
686789Sahrens 	if (error) {
6871544Seschrock 		/*
6881544Seschrock 		 * Reinitialise the lwb.
6891544Seschrock 		 * By returning NULL the caller will call tx_wait_synced()
6901544Seschrock 		 */
6911544Seschrock 		mutex_enter(&zilog->zl_lock);
6921544Seschrock 		ASSERT(lwb->lwb_state == UNWRITTEN);
6931544Seschrock 		lwb->lwb_nused = 0;
6941544Seschrock 		lwb->lwb_seq = 0;
6951544Seschrock 		mutex_exit(&zilog->zl_lock);
696789Sahrens 		txg_rele_to_sync(&lwb->lwb_txgh);
697789Sahrens 		return (NULL);
698789Sahrens 	}
699789Sahrens 
7001807Sbonwick 	ASSERT3U(bp->blk_birth, ==, txg);
7011544Seschrock 	ztp->zit_pad = 0;
702789Sahrens 	ztp->zit_nused = lwb->lwb_nused;
703789Sahrens 	ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum;
7041807Sbonwick 	bp->blk_cksum = lwb->lwb_blk.blk_cksum;
7051807Sbonwick 	bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++;
706789Sahrens 
707789Sahrens 	/*
708789Sahrens 	 * Allocate a new log write buffer (lwb).
709789Sahrens 	 */
710789Sahrens 	nlwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP);
711789Sahrens 
712789Sahrens 	nlwb->lwb_zilog = zilog;
7131807Sbonwick 	nlwb->lwb_blk = *bp;
714789Sahrens 	nlwb->lwb_nused = 0;
715789Sahrens 	nlwb->lwb_sz = BP_GET_LSIZE(&nlwb->lwb_blk);
716789Sahrens 	nlwb->lwb_buf = zio_buf_alloc(nlwb->lwb_sz);
717789Sahrens 	nlwb->lwb_max_txg = txg;
718789Sahrens 	nlwb->lwb_seq = 0;
719789Sahrens 	nlwb->lwb_state = UNWRITTEN;
720*2237Smaybee 	nlwb->lwb_zio = NULL;
721789Sahrens 
722789Sahrens 	/*
723789Sahrens 	 * Put new lwb at the end of the log chain,
724789Sahrens 	 * and record the vdev for later flushing
725789Sahrens 	 */
726789Sahrens 	mutex_enter(&zilog->zl_lock);
727789Sahrens 	list_insert_tail(&zilog->zl_lwb_list, nlwb);
728789Sahrens 	zil_add_vdev(zilog, DVA_GET_VDEV(BP_IDENTITY(&(lwb->lwb_blk))),
729789Sahrens 	    lwb->lwb_seq);
730789Sahrens 	mutex_exit(&zilog->zl_lock);
731789Sahrens 
732789Sahrens 	/*
733*2237Smaybee 	 * kick off the write for the old log block
734789Sahrens 	 */
735*2237Smaybee 	dprintf_bp(&lwb->lwb_blk, "lwb %p txg %llu: ", lwb, txg);
736*2237Smaybee 	if (lwb->lwb_zio == NULL) {
737*2237Smaybee 		/*
738*2237Smaybee 		 * This can only happen if there are no log records in this
739*2237Smaybee 		 * block (i.e. the first record to go in was too big to fit).
740*2237Smaybee 		 * XXX - would be nice if we could avoid this IO
741*2237Smaybee 		 */
742*2237Smaybee 		ASSERT(lwb->lwb_nused == 0);
743*2237Smaybee 		zil_lwb_write_init(zilog, lwb);
744*2237Smaybee 	}
745*2237Smaybee 	zio_nowait(lwb->lwb_zio);
746789Sahrens 
747789Sahrens 	return (nlwb);
748789Sahrens }
749789Sahrens 
750789Sahrens static lwb_t *
751789Sahrens zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb)
752789Sahrens {
753789Sahrens 	lr_t *lrc = &itx->itx_lr; /* common log record */
754*2237Smaybee 	lr_write_t *lr = (lr_write_t *)lrc;
755789Sahrens 	uint64_t seq = lrc->lrc_seq;
756789Sahrens 	uint64_t txg = lrc->lrc_txg;
757789Sahrens 	uint64_t reclen = lrc->lrc_reclen;
758*2237Smaybee 	uint64_t dlen;
759789Sahrens 
760789Sahrens 	if (lwb == NULL)
761789Sahrens 		return (NULL);
762789Sahrens 	ASSERT(lwb->lwb_buf != NULL);
763789Sahrens 
764*2237Smaybee 	if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY)
765*2237Smaybee 		dlen = P2ROUNDUP_TYPED(
766*2237Smaybee 		    lr->lr_length, sizeof (uint64_t), uint64_t);
767*2237Smaybee 	else
768*2237Smaybee 		dlen = 0;
7691669Sperrin 
7701669Sperrin 	zilog->zl_cur_used += (reclen + dlen);
7711669Sperrin 
7721669Sperrin 	/*
7731669Sperrin 	 * If this record won't fit in the current log block, start a new one.
7741669Sperrin 	 */
7751669Sperrin 	if (lwb->lwb_nused + reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) {
7761669Sperrin 		lwb = zil_lwb_write_start(zilog, lwb);
777*2237Smaybee 		if (lwb == NULL)
7781669Sperrin 			return (NULL);
7791669Sperrin 		ASSERT(lwb->lwb_nused == 0);
7801669Sperrin 		if (reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) {
7811669Sperrin 			txg_wait_synced(zilog->zl_dmu_pool, txg);
782789Sahrens 			mutex_enter(&zilog->zl_lock);
7831669Sperrin 			zilog->zl_ss_seq = MAX(seq, zilog->zl_ss_seq);
784789Sahrens 			mutex_exit(&zilog->zl_lock);
785789Sahrens 			return (lwb);
786789Sahrens 		}
787789Sahrens 	}
788789Sahrens 
789*2237Smaybee 	if (lwb->lwb_zio == NULL)
790*2237Smaybee 		zil_lwb_write_init(zilog, lwb);
791*2237Smaybee 
792789Sahrens 	bcopy(lrc, lwb->lwb_buf + lwb->lwb_nused, reclen);
793*2237Smaybee 
794*2237Smaybee 	/*
795*2237Smaybee 	 * If it's a write, fetch the data or get its blkptr as appropriate.
796*2237Smaybee 	 */
797*2237Smaybee 	if (lrc->lrc_txtype == TX_WRITE) {
798*2237Smaybee 		if (txg > spa_freeze_txg(zilog->zl_spa))
799*2237Smaybee 			txg_wait_synced(zilog->zl_dmu_pool, txg);
800*2237Smaybee 		if (itx->itx_wr_state != WR_COPIED) {
801*2237Smaybee 			char *dbuf;
802*2237Smaybee 			int error;
803*2237Smaybee 
804*2237Smaybee 			/* alignment is guaranteed */
805*2237Smaybee 			lr = (lr_write_t *)(lwb->lwb_buf + lwb->lwb_nused);
806*2237Smaybee 			if (dlen) {
807*2237Smaybee 				ASSERT(itx->itx_wr_state == WR_NEED_COPY);
808*2237Smaybee 				dbuf = lwb->lwb_buf + lwb->lwb_nused + reclen;
809*2237Smaybee 				lr->lr_common.lrc_reclen += dlen;
810*2237Smaybee 			} else {
811*2237Smaybee 				ASSERT(itx->itx_wr_state == WR_INDIRECT);
812*2237Smaybee 				dbuf = NULL;
813*2237Smaybee 			}
814*2237Smaybee 			error = zilog->zl_get_data(
815*2237Smaybee 			    itx->itx_private, lr, dbuf, lwb->lwb_zio);
816*2237Smaybee 			if (error) {
817*2237Smaybee 				ASSERT(error == ENOENT || error == EEXIST ||
818*2237Smaybee 				    error == EALREADY);
819*2237Smaybee 				return (lwb);
820*2237Smaybee 			}
821*2237Smaybee 		}
8221669Sperrin 	}
823*2237Smaybee 
824*2237Smaybee 	mutex_enter(&zilog->zl_lock);
825*2237Smaybee 	ASSERT(seq > zilog->zl_wait_seq);
826*2237Smaybee 	zilog->zl_wait_seq = seq;
827*2237Smaybee 	mutex_exit(&zilog->zl_lock);
828*2237Smaybee 	lwb->lwb_nused += reclen + dlen;
829789Sahrens 	lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg);
830789Sahrens 	ASSERT3U(lwb->lwb_seq, <, seq);
831789Sahrens 	lwb->lwb_seq = seq;
832789Sahrens 	ASSERT3U(lwb->lwb_nused, <=, ZIL_BLK_DATA_SZ(lwb));
833789Sahrens 	ASSERT3U(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)), ==, 0);
834789Sahrens 
835789Sahrens 	return (lwb);
836789Sahrens }
837789Sahrens 
838789Sahrens itx_t *
839789Sahrens zil_itx_create(int txtype, size_t lrsize)
840789Sahrens {
841789Sahrens 	itx_t *itx;
842789Sahrens 
8431842Sperrin 	lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t);
844789Sahrens 
845789Sahrens 	itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP);
846789Sahrens 	itx->itx_lr.lrc_txtype = txtype;
847789Sahrens 	itx->itx_lr.lrc_reclen = lrsize;
848789Sahrens 	itx->itx_lr.lrc_seq = 0;	/* defensive */
849789Sahrens 
850789Sahrens 	return (itx);
851789Sahrens }
852789Sahrens 
853789Sahrens uint64_t
854789Sahrens zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx)
855789Sahrens {
856789Sahrens 	uint64_t seq;
857789Sahrens 
858789Sahrens 	ASSERT(itx->itx_lr.lrc_seq == 0);
859789Sahrens 
860789Sahrens 	mutex_enter(&zilog->zl_lock);
861789Sahrens 	list_insert_tail(&zilog->zl_itx_list, itx);
862789Sahrens 	zilog->zl_itx_list_sz += itx->itx_lr.lrc_reclen;
863789Sahrens 	itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx);
864789Sahrens 	itx->itx_lr.lrc_seq = seq = ++zilog->zl_itx_seq;
865789Sahrens 	mutex_exit(&zilog->zl_lock);
866789Sahrens 
867789Sahrens 	return (seq);
868789Sahrens }
869789Sahrens 
870789Sahrens /*
871789Sahrens  * Free up all in-memory intent log transactions that have now been synced.
872789Sahrens  */
873789Sahrens static void
874789Sahrens zil_itx_clean(zilog_t *zilog)
875789Sahrens {
876789Sahrens 	uint64_t synced_txg = spa_last_synced_txg(zilog->zl_spa);
877789Sahrens 	uint64_t freeze_txg = spa_freeze_txg(zilog->zl_spa);
878789Sahrens 	uint64_t max_seq = 0;
879789Sahrens 	itx_t *itx;
880789Sahrens 
881789Sahrens 	mutex_enter(&zilog->zl_lock);
882789Sahrens 	while ((itx = list_head(&zilog->zl_itx_list)) != NULL &&
883789Sahrens 	    itx->itx_lr.lrc_txg <= MIN(synced_txg, freeze_txg)) {
884789Sahrens 		list_remove(&zilog->zl_itx_list, itx);
885789Sahrens 		zilog->zl_itx_list_sz -= itx->itx_lr.lrc_reclen;
886789Sahrens 		ASSERT3U(max_seq, <, itx->itx_lr.lrc_seq);
887789Sahrens 		max_seq = itx->itx_lr.lrc_seq;
888789Sahrens 		kmem_free(itx, offsetof(itx_t, itx_lr)
889789Sahrens 		    + itx->itx_lr.lrc_reclen);
890789Sahrens 	}
891789Sahrens 	if (max_seq > zilog->zl_ss_seq) {
892789Sahrens 		zilog->zl_ss_seq = max_seq;
893789Sahrens 		cv_broadcast(&zilog->zl_cv_seq);
894789Sahrens 	}
895789Sahrens 	mutex_exit(&zilog->zl_lock);
896789Sahrens }
897789Sahrens 
898789Sahrens void
899789Sahrens zil_clean(zilog_t *zilog)
900789Sahrens {
901789Sahrens 	/*
902789Sahrens 	 * Check for any log blocks that can be freed.
903789Sahrens 	 * Log blocks are only freed when the log block allocation and
904789Sahrens 	 * log records contained within are both known to be committed.
905789Sahrens 	 */
906789Sahrens 	mutex_enter(&zilog->zl_lock);
907789Sahrens 	if (list_head(&zilog->zl_itx_list) != NULL)
908789Sahrens 		(void) taskq_dispatch(zilog->zl_clean_taskq,
909789Sahrens 		    (void (*)(void *))zil_itx_clean, zilog, TQ_NOSLEEP);
910789Sahrens 	mutex_exit(&zilog->zl_lock);
911789Sahrens }
912789Sahrens 
913789Sahrens /*
914789Sahrens  * Push zfs transactions to stable storage up to the supplied sequence number.
915789Sahrens  */
916789Sahrens void
917789Sahrens zil_commit(zilog_t *zilog, uint64_t seq, int ioflag)
918789Sahrens {
919789Sahrens 	uint64_t txg;
920789Sahrens 	uint64_t max_seq;
921789Sahrens 	uint64_t reclen;
922789Sahrens 	itx_t *itx;
923789Sahrens 	lwb_t *lwb;
924789Sahrens 	spa_t *spa;
925789Sahrens 
926789Sahrens 	if (zilog == NULL || seq == 0 ||
927789Sahrens 	    ((ioflag & (FSYNC | FDSYNC | FRSYNC)) == 0 && !zil_always))
928789Sahrens 		return;
929789Sahrens 
930789Sahrens 	spa = zilog->zl_spa;
931789Sahrens 	mutex_enter(&zilog->zl_lock);
932789Sahrens 
933789Sahrens 	seq = MIN(seq, zilog->zl_itx_seq);	/* cap seq at largest itx seq */
934789Sahrens 
935789Sahrens 	for (;;) {
936789Sahrens 		if (zilog->zl_ss_seq >= seq) {	/* already on stable storage */
937789Sahrens 			mutex_exit(&zilog->zl_lock);
938789Sahrens 			return;
939789Sahrens 		}
940789Sahrens 
941789Sahrens 		if (zilog->zl_writer == B_FALSE) /* no one writing, do it */
942789Sahrens 			break;
943789Sahrens 
944789Sahrens 		cv_wait(&zilog->zl_cv_write, &zilog->zl_lock);
945789Sahrens 	}
946789Sahrens 
947789Sahrens 	zilog->zl_writer = B_TRUE;
948789Sahrens 	max_seq = 0;
949789Sahrens 
950789Sahrens 	if (zilog->zl_suspend) {
951789Sahrens 		lwb = NULL;
952789Sahrens 	} else {
953789Sahrens 		lwb = list_tail(&zilog->zl_lwb_list);
954789Sahrens 		if (lwb == NULL) {
955789Sahrens 			mutex_exit(&zilog->zl_lock);
956789Sahrens 			zil_create(zilog);
957789Sahrens 			mutex_enter(&zilog->zl_lock);
958789Sahrens 			lwb = list_tail(&zilog->zl_lwb_list);
959789Sahrens 		}
960789Sahrens 	}
961789Sahrens 
962789Sahrens 	/*
963789Sahrens 	 * Loop through in-memory log transactions filling log blocks,
964789Sahrens 	 * until we reach the given sequence number and there's no more
965789Sahrens 	 * room in the write buffer.
966789Sahrens 	 */
967789Sahrens 	for (;;) {
968789Sahrens 		itx = list_head(&zilog->zl_itx_list);
969789Sahrens 		if (itx == NULL)
970789Sahrens 			break;
971789Sahrens 
972789Sahrens 		reclen = itx->itx_lr.lrc_reclen;
973789Sahrens 		if ((itx->itx_lr.lrc_seq > seq) &&
974789Sahrens 		    ((lwb == NULL) || (lwb->lwb_nused + reclen >
975789Sahrens 		    ZIL_BLK_DATA_SZ(lwb))))
976789Sahrens 			break;
977789Sahrens 
978789Sahrens 		list_remove(&zilog->zl_itx_list, itx);
979789Sahrens 		txg = itx->itx_lr.lrc_txg;
980789Sahrens 		ASSERT(txg);
981789Sahrens 
982789Sahrens 		mutex_exit(&zilog->zl_lock);
983789Sahrens 		if (txg > spa_last_synced_txg(spa) ||
984789Sahrens 		    txg > spa_freeze_txg(spa))
985789Sahrens 			lwb = zil_lwb_commit(zilog, itx, lwb);
986789Sahrens 		else
987789Sahrens 			max_seq = itx->itx_lr.lrc_seq;
988789Sahrens 		kmem_free(itx, offsetof(itx_t, itx_lr)
989789Sahrens 		    + itx->itx_lr.lrc_reclen);
990789Sahrens 		mutex_enter(&zilog->zl_lock);
991789Sahrens 		zilog->zl_itx_list_sz -= reclen;
992789Sahrens 	}
993789Sahrens 
994789Sahrens 	mutex_exit(&zilog->zl_lock);
995789Sahrens 
996789Sahrens 	/* write the last block out */
997789Sahrens 	if (lwb != NULL && lwb->lwb_nused != 0)
998789Sahrens 		lwb = zil_lwb_write_start(zilog, lwb);
999789Sahrens 
10001141Sperrin 	zilog->zl_prev_used = zilog->zl_cur_used;
10011141Sperrin 	zilog->zl_cur_used = 0;
10021141Sperrin 
1003789Sahrens 	mutex_enter(&zilog->zl_lock);
1004789Sahrens 	if (max_seq > zilog->zl_ss_seq) {
1005789Sahrens 		zilog->zl_ss_seq = max_seq;
1006789Sahrens 		cv_broadcast(&zilog->zl_cv_seq);
1007789Sahrens 	}
1008789Sahrens 	/*
1009789Sahrens 	 * Wait if necessary for our seq to be committed.
1010789Sahrens 	 */
1011*2237Smaybee 	if (lwb && zilog->zl_wait_seq) {
1012*2237Smaybee 		while (zilog->zl_ss_seq < zilog->zl_wait_seq &&
1013*2237Smaybee 		    zilog->zl_log_error == 0)
1014789Sahrens 			cv_wait(&zilog->zl_cv_seq, &zilog->zl_lock);
1015789Sahrens 		zil_flush_vdevs(zilog, seq);
1016789Sahrens 	}
10171141Sperrin 
1018789Sahrens 	if (zilog->zl_log_error || lwb == NULL) {
1019789Sahrens 		zilog->zl_log_error = 0;
1020789Sahrens 		max_seq = zilog->zl_itx_seq;
1021789Sahrens 		mutex_exit(&zilog->zl_lock);
1022789Sahrens 		txg_wait_synced(zilog->zl_dmu_pool, 0);
1023789Sahrens 		mutex_enter(&zilog->zl_lock);
1024789Sahrens 		zilog->zl_ss_seq = MAX(max_seq, zilog->zl_ss_seq);
1025789Sahrens 		cv_broadcast(&zilog->zl_cv_seq);
1026789Sahrens 	}
10271141Sperrin 	/* wake up others waiting to start a write */
1028*2237Smaybee 	zilog->zl_wait_seq = 0;
10291141Sperrin 	zilog->zl_writer = B_FALSE;
1030789Sahrens 	mutex_exit(&zilog->zl_lock);
10311472Sperrin 	cv_broadcast(&zilog->zl_cv_write);
1032789Sahrens }
1033789Sahrens 
1034789Sahrens /*
1035789Sahrens  * Called in syncing context to free committed log blocks and update log header.
1036789Sahrens  */
1037789Sahrens void
1038789Sahrens zil_sync(zilog_t *zilog, dmu_tx_t *tx)
1039789Sahrens {
10401807Sbonwick 	zil_header_t *zh = zil_header_in_syncing_context(zilog);
1041789Sahrens 	uint64_t txg = dmu_tx_get_txg(tx);
1042789Sahrens 	spa_t *spa = zilog->zl_spa;
1043789Sahrens 	lwb_t *lwb;
1044789Sahrens 
10451807Sbonwick 	mutex_enter(&zilog->zl_lock);
10461807Sbonwick 
1047789Sahrens 	ASSERT(zilog->zl_stop_sync == 0);
1048789Sahrens 
10491807Sbonwick 	zh->zh_replay_seq = zilog->zl_replay_seq[txg & TXG_MASK];
1050789Sahrens 
1051789Sahrens 	if (zilog->zl_destroy_txg == txg) {
10521807Sbonwick 		blkptr_t blk = zh->zh_log;
10531807Sbonwick 
10541807Sbonwick 		ASSERT(list_head(&zilog->zl_lwb_list) == NULL);
10551807Sbonwick 		ASSERT(spa_sync_pass(spa) == 1);
10561807Sbonwick 
10571807Sbonwick 		bzero(zh, sizeof (zil_header_t));
1058789Sahrens 		bzero(zilog->zl_replay_seq, sizeof (zilog->zl_replay_seq));
10591807Sbonwick 
10601807Sbonwick 		if (zilog->zl_keep_first) {
10611807Sbonwick 			/*
10621807Sbonwick 			 * If this block was part of log chain that couldn't
10631807Sbonwick 			 * be claimed because a device was missing during
10641807Sbonwick 			 * zil_claim(), but that device later returns,
10651807Sbonwick 			 * then this block could erroneously appear valid.
10661807Sbonwick 			 * To guard against this, assign a new GUID to the new
10671807Sbonwick 			 * log chain so it doesn't matter what blk points to.
10681807Sbonwick 			 */
10691807Sbonwick 			zil_init_log_chain(zilog, &blk);
10701807Sbonwick 			zh->zh_log = blk;
10711807Sbonwick 		}
1072789Sahrens 	}
1073789Sahrens 
1074789Sahrens 	for (;;) {
1075789Sahrens 		lwb = list_head(&zilog->zl_lwb_list);
1076789Sahrens 		if (lwb == NULL) {
1077789Sahrens 			mutex_exit(&zilog->zl_lock);
1078789Sahrens 			return;
1079789Sahrens 		}
1080789Sahrens 		if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg)
1081789Sahrens 			break;
1082789Sahrens 		list_remove(&zilog->zl_lwb_list, lwb);
1083789Sahrens 		zio_free_blk(spa, &lwb->lwb_blk, txg);
1084789Sahrens 		kmem_cache_free(zil_lwb_cache, lwb);
1085789Sahrens 	}
10861807Sbonwick 	zh->zh_log = lwb->lwb_blk;
1087789Sahrens 	mutex_exit(&zilog->zl_lock);
1088789Sahrens }
1089789Sahrens 
1090789Sahrens void
1091789Sahrens zil_init(void)
1092789Sahrens {
1093789Sahrens 	zil_lwb_cache = kmem_cache_create("zil_lwb_cache",
1094789Sahrens 	    sizeof (struct lwb), NULL, NULL, NULL, NULL, NULL, NULL, 0);
1095789Sahrens }
1096789Sahrens 
1097789Sahrens void
1098789Sahrens zil_fini(void)
1099789Sahrens {
1100789Sahrens 	kmem_cache_destroy(zil_lwb_cache);
1101789Sahrens }
1102789Sahrens 
1103789Sahrens zilog_t *
1104789Sahrens zil_alloc(objset_t *os, zil_header_t *zh_phys)
1105789Sahrens {
1106789Sahrens 	zilog_t *zilog;
1107789Sahrens 
1108789Sahrens 	zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP);
1109789Sahrens 
1110789Sahrens 	zilog->zl_header = zh_phys;
1111789Sahrens 	zilog->zl_os = os;
1112789Sahrens 	zilog->zl_spa = dmu_objset_spa(os);
1113789Sahrens 	zilog->zl_dmu_pool = dmu_objset_pool(os);
11141807Sbonwick 	zilog->zl_destroy_txg = TXG_INITIAL - 1;
1115789Sahrens 
1116789Sahrens 	list_create(&zilog->zl_itx_list, sizeof (itx_t),
1117789Sahrens 	    offsetof(itx_t, itx_node));
1118789Sahrens 
1119789Sahrens 	list_create(&zilog->zl_lwb_list, sizeof (lwb_t),
1120789Sahrens 	    offsetof(lwb_t, lwb_node));
1121789Sahrens 
1122789Sahrens 	list_create(&zilog->zl_vdev_list, sizeof (zil_vdev_t),
1123789Sahrens 	    offsetof(zil_vdev_t, vdev_seq_node));
1124789Sahrens 
1125789Sahrens 	return (zilog);
1126789Sahrens }
1127789Sahrens 
1128789Sahrens void
1129789Sahrens zil_free(zilog_t *zilog)
1130789Sahrens {
1131789Sahrens 	lwb_t *lwb;
1132789Sahrens 	zil_vdev_t *zv;
1133789Sahrens 
1134789Sahrens 	zilog->zl_stop_sync = 1;
1135789Sahrens 
1136789Sahrens 	while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
1137789Sahrens 		list_remove(&zilog->zl_lwb_list, lwb);
1138789Sahrens 		if (lwb->lwb_buf != NULL)
1139789Sahrens 			zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
1140789Sahrens 		kmem_cache_free(zil_lwb_cache, lwb);
1141789Sahrens 	}
1142789Sahrens 	list_destroy(&zilog->zl_lwb_list);
1143789Sahrens 
1144789Sahrens 	while ((zv = list_head(&zilog->zl_vdev_list)) != NULL) {
1145789Sahrens 		list_remove(&zilog->zl_vdev_list, zv);
1146789Sahrens 		kmem_free(zv, sizeof (zil_vdev_t));
1147789Sahrens 	}
1148789Sahrens 	list_destroy(&zilog->zl_vdev_list);
1149789Sahrens 
1150789Sahrens 	ASSERT(list_head(&zilog->zl_itx_list) == NULL);
1151789Sahrens 	list_destroy(&zilog->zl_itx_list);
1152789Sahrens 
1153789Sahrens 	kmem_free(zilog, sizeof (zilog_t));
1154789Sahrens }
1155789Sahrens 
1156789Sahrens /*
11571646Sperrin  * return true if the initial log block is not valid
11581362Sperrin  */
11591362Sperrin static int
11601362Sperrin zil_empty(zilog_t *zilog)
11611362Sperrin {
11621807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
11631807Sbonwick 	arc_buf_t *abuf = NULL;
11641362Sperrin 
11651807Sbonwick 	if (BP_IS_HOLE(&zh->zh_log))
11661362Sperrin 		return (1);
11671362Sperrin 
11681807Sbonwick 	if (zil_read_log_block(zilog, &zh->zh_log, &abuf) != 0)
11691807Sbonwick 		return (1);
11701807Sbonwick 
11711807Sbonwick 	VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
11721807Sbonwick 	return (0);
11731362Sperrin }
11741362Sperrin 
11751362Sperrin /*
1176789Sahrens  * Open an intent log.
1177789Sahrens  */
1178789Sahrens zilog_t *
1179789Sahrens zil_open(objset_t *os, zil_get_data_t *get_data)
1180789Sahrens {
1181789Sahrens 	zilog_t *zilog = dmu_objset_zil(os);
1182789Sahrens 
1183789Sahrens 	zilog->zl_get_data = get_data;
1184789Sahrens 	zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri,
1185789Sahrens 	    2, 2, TASKQ_PREPOPULATE);
1186789Sahrens 
1187789Sahrens 	return (zilog);
1188789Sahrens }
1189789Sahrens 
1190789Sahrens /*
1191789Sahrens  * Close an intent log.
1192789Sahrens  */
1193789Sahrens void
1194789Sahrens zil_close(zilog_t *zilog)
1195789Sahrens {
11961807Sbonwick 	/*
11971807Sbonwick 	 * If the log isn't already committed, mark the objset dirty
11981807Sbonwick 	 * (so zil_sync() will be called) and wait for that txg to sync.
11991807Sbonwick 	 */
12001807Sbonwick 	if (!zil_is_committed(zilog)) {
12011807Sbonwick 		uint64_t txg;
12021807Sbonwick 		dmu_tx_t *tx = dmu_tx_create(zilog->zl_os);
12031807Sbonwick 		(void) dmu_tx_assign(tx, TXG_WAIT);
12041807Sbonwick 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
12051807Sbonwick 		txg = dmu_tx_get_txg(tx);
12061807Sbonwick 		dmu_tx_commit(tx);
12071807Sbonwick 		txg_wait_synced(zilog->zl_dmu_pool, txg);
12081807Sbonwick 	}
12091807Sbonwick 
1210789Sahrens 	taskq_destroy(zilog->zl_clean_taskq);
1211789Sahrens 	zilog->zl_clean_taskq = NULL;
1212789Sahrens 	zilog->zl_get_data = NULL;
1213789Sahrens 
1214789Sahrens 	zil_itx_clean(zilog);
1215789Sahrens 	ASSERT(list_head(&zilog->zl_itx_list) == NULL);
1216789Sahrens }
1217789Sahrens 
1218789Sahrens /*
1219789Sahrens  * Suspend an intent log.  While in suspended mode, we still honor
1220789Sahrens  * synchronous semantics, but we rely on txg_wait_synced() to do it.
1221789Sahrens  * We suspend the log briefly when taking a snapshot so that the snapshot
1222789Sahrens  * contains all the data it's supposed to, and has an empty intent log.
1223789Sahrens  */
1224789Sahrens int
1225789Sahrens zil_suspend(zilog_t *zilog)
1226789Sahrens {
12271807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
1228789Sahrens 	lwb_t *lwb;
1229789Sahrens 
1230789Sahrens 	mutex_enter(&zilog->zl_lock);
12311807Sbonwick 	if (zh->zh_claim_txg != 0) {		/* unplayed log */
1232789Sahrens 		mutex_exit(&zilog->zl_lock);
1233789Sahrens 		return (EBUSY);
1234789Sahrens 	}
12351807Sbonwick 	if (zilog->zl_suspend++ != 0) {
12361807Sbonwick 		/*
12371807Sbonwick 		 * Someone else already began a suspend.
12381807Sbonwick 		 * Just wait for them to finish.
12391807Sbonwick 		 */
12401807Sbonwick 		while (zilog->zl_suspending)
12411807Sbonwick 			cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock);
12421807Sbonwick 		ASSERT(BP_IS_HOLE(&zh->zh_log));
12431807Sbonwick 		mutex_exit(&zilog->zl_lock);
12441807Sbonwick 		return (0);
12451807Sbonwick 	}
12461807Sbonwick 	zilog->zl_suspending = B_TRUE;
1247789Sahrens 	mutex_exit(&zilog->zl_lock);
1248789Sahrens 
1249789Sahrens 	zil_commit(zilog, UINT64_MAX, FSYNC);
1250789Sahrens 
1251789Sahrens 	mutex_enter(&zilog->zl_lock);
12521807Sbonwick 	for (;;) {
12531807Sbonwick 		/*
12541807Sbonwick 		 * Wait for any in-flight log writes to complete.
12551807Sbonwick 		 */
12561807Sbonwick 		for (lwb = list_head(&zilog->zl_lwb_list); lwb != NULL;
12571807Sbonwick 		    lwb = list_next(&zilog->zl_lwb_list, lwb))
12581807Sbonwick 			if (lwb->lwb_seq != 0 && lwb->lwb_state != SEQ_COMPLETE)
12591807Sbonwick 				break;
12601807Sbonwick 
12611807Sbonwick 		if (lwb == NULL)
12621807Sbonwick 			break;
12631807Sbonwick 
12641807Sbonwick 		cv_wait(&zilog->zl_cv_seq, &zilog->zl_lock);
1265789Sahrens 	}
12661807Sbonwick 
1267789Sahrens 	mutex_exit(&zilog->zl_lock);
1268789Sahrens 
12691807Sbonwick 	zil_destroy(zilog, B_FALSE);
12701807Sbonwick 
12711807Sbonwick 	mutex_enter(&zilog->zl_lock);
12721807Sbonwick 	ASSERT(BP_IS_HOLE(&zh->zh_log));
12731807Sbonwick 	zilog->zl_suspending = B_FALSE;
12741807Sbonwick 	cv_broadcast(&zilog->zl_cv_suspend);
12751807Sbonwick 	mutex_exit(&zilog->zl_lock);
1276789Sahrens 
1277789Sahrens 	return (0);
1278789Sahrens }
1279789Sahrens 
1280789Sahrens void
1281789Sahrens zil_resume(zilog_t *zilog)
1282789Sahrens {
1283789Sahrens 	mutex_enter(&zilog->zl_lock);
1284789Sahrens 	ASSERT(zilog->zl_suspend != 0);
1285789Sahrens 	zilog->zl_suspend--;
1286789Sahrens 	mutex_exit(&zilog->zl_lock);
1287789Sahrens }
1288789Sahrens 
1289789Sahrens typedef struct zil_replay_arg {
1290789Sahrens 	objset_t	*zr_os;
1291789Sahrens 	zil_replay_func_t **zr_replay;
1292789Sahrens 	void		*zr_arg;
1293789Sahrens 	void		(*zr_rm_sync)(void *arg);
1294789Sahrens 	uint64_t	*zr_txgp;
1295789Sahrens 	boolean_t	zr_byteswap;
1296789Sahrens 	char		*zr_lrbuf;
1297789Sahrens } zil_replay_arg_t;
1298789Sahrens 
1299789Sahrens static void
1300789Sahrens zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg)
1301789Sahrens {
1302789Sahrens 	zil_replay_arg_t *zr = zra;
13031807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
1304789Sahrens 	uint64_t reclen = lr->lrc_reclen;
1305789Sahrens 	uint64_t txtype = lr->lrc_txtype;
1306789Sahrens 	int pass, error;
1307789Sahrens 
1308789Sahrens 	if (zilog->zl_stop_replay)
1309789Sahrens 		return;
1310789Sahrens 
1311789Sahrens 	if (lr->lrc_txg < claim_txg)		/* already committed */
1312789Sahrens 		return;
1313789Sahrens 
1314789Sahrens 	if (lr->lrc_seq <= zh->zh_replay_seq)	/* already replayed */
1315789Sahrens 		return;
1316789Sahrens 
1317789Sahrens 	/*
1318789Sahrens 	 * Make a copy of the data so we can revise and extend it.
1319789Sahrens 	 */
1320789Sahrens 	bcopy(lr, zr->zr_lrbuf, reclen);
1321789Sahrens 
1322789Sahrens 	/*
1323789Sahrens 	 * The log block containing this lr may have been byteswapped
1324789Sahrens 	 * so that we can easily examine common fields like lrc_txtype.
1325789Sahrens 	 * However, the log is a mix of different data types, and only the
1326789Sahrens 	 * replay vectors know how to byteswap their records.  Therefore, if
1327789Sahrens 	 * the lr was byteswapped, undo it before invoking the replay vector.
1328789Sahrens 	 */
1329789Sahrens 	if (zr->zr_byteswap)
1330789Sahrens 		byteswap_uint64_array(zr->zr_lrbuf, reclen);
1331789Sahrens 
1332789Sahrens 	/*
1333789Sahrens 	 * If this is a TX_WRITE with a blkptr, suck in the data.
1334789Sahrens 	 */
1335789Sahrens 	if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) {
1336789Sahrens 		lr_write_t *lrw = (lr_write_t *)lr;
1337789Sahrens 		blkptr_t *wbp = &lrw->lr_blkptr;
1338789Sahrens 		uint64_t wlen = lrw->lr_length;
1339789Sahrens 		char *wbuf = zr->zr_lrbuf + reclen;
1340789Sahrens 
1341789Sahrens 		if (BP_IS_HOLE(wbp)) {	/* compressed to a hole */
1342789Sahrens 			bzero(wbuf, wlen);
1343789Sahrens 		} else {
1344789Sahrens 			/*
1345789Sahrens 			 * A subsequent write may have overwritten this block,
1346789Sahrens 			 * in which case wbp may have been been freed and
1347789Sahrens 			 * reallocated, and our read of wbp may fail with a
1348789Sahrens 			 * checksum error.  We can safely ignore this because
1349789Sahrens 			 * the later write will provide the correct data.
1350789Sahrens 			 */
13511544Seschrock 			zbookmark_t zb;
13521544Seschrock 
13531544Seschrock 			zb.zb_objset = dmu_objset_id(zilog->zl_os);
13541544Seschrock 			zb.zb_object = lrw->lr_foid;
13551544Seschrock 			zb.zb_level = -1;
13561544Seschrock 			zb.zb_blkid = lrw->lr_offset / BP_GET_LSIZE(wbp);
13571544Seschrock 
1358789Sahrens 			(void) zio_wait(zio_read(NULL, zilog->zl_spa,
1359789Sahrens 			    wbp, wbuf, BP_GET_LSIZE(wbp), NULL, NULL,
1360789Sahrens 			    ZIO_PRIORITY_SYNC_READ,
13611544Seschrock 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &zb));
1362789Sahrens 			(void) memmove(wbuf, wbuf + lrw->lr_blkoff, wlen);
1363789Sahrens 		}
1364789Sahrens 	}
1365789Sahrens 
1366789Sahrens 	/*
1367789Sahrens 	 * We must now do two things atomically: replay this log record,
1368789Sahrens 	 * and update the log header to reflect the fact that we did so.
1369789Sahrens 	 * We use the DMU's ability to assign into a specific txg to do this.
1370789Sahrens 	 */
1371789Sahrens 	for (pass = 1; /* CONSTANTCONDITION */; pass++) {
1372789Sahrens 		uint64_t replay_txg;
1373789Sahrens 		dmu_tx_t *replay_tx;
1374789Sahrens 
1375789Sahrens 		replay_tx = dmu_tx_create(zr->zr_os);
1376789Sahrens 		error = dmu_tx_assign(replay_tx, TXG_WAIT);
1377789Sahrens 		if (error) {
1378789Sahrens 			dmu_tx_abort(replay_tx);
1379789Sahrens 			break;
1380789Sahrens 		}
1381789Sahrens 
1382789Sahrens 		replay_txg = dmu_tx_get_txg(replay_tx);
1383789Sahrens 
1384789Sahrens 		if (txtype == 0 || txtype >= TX_MAX_TYPE) {
1385789Sahrens 			error = EINVAL;
1386789Sahrens 		} else {
1387789Sahrens 			/*
1388789Sahrens 			 * On the first pass, arrange for the replay vector
1389789Sahrens 			 * to fail its dmu_tx_assign().  That's the only way
1390789Sahrens 			 * to ensure that those code paths remain well tested.
1391789Sahrens 			 */
1392789Sahrens 			*zr->zr_txgp = replay_txg - (pass == 1);
1393789Sahrens 			error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lrbuf,
1394789Sahrens 			    zr->zr_byteswap);
1395789Sahrens 			*zr->zr_txgp = TXG_NOWAIT;
1396789Sahrens 		}
1397789Sahrens 
1398789Sahrens 		if (error == 0) {
1399789Sahrens 			dsl_dataset_dirty(dmu_objset_ds(zr->zr_os), replay_tx);
1400789Sahrens 			zilog->zl_replay_seq[replay_txg & TXG_MASK] =
1401789Sahrens 			    lr->lrc_seq;
1402789Sahrens 		}
1403789Sahrens 
1404789Sahrens 		dmu_tx_commit(replay_tx);
1405789Sahrens 
1406789Sahrens 		if (error != ERESTART)
1407789Sahrens 			break;
1408789Sahrens 
1409789Sahrens 		if (pass != 1)
1410789Sahrens 			txg_wait_open(spa_get_dsl(zilog->zl_spa),
1411789Sahrens 			    replay_txg + 1);
1412789Sahrens 
1413789Sahrens 		dprintf("pass %d, retrying\n", pass);
1414789Sahrens 	}
1415789Sahrens 
1416789Sahrens 	if (error) {
1417789Sahrens 		char *name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1418789Sahrens 		dmu_objset_name(zr->zr_os, name);
1419789Sahrens 		cmn_err(CE_WARN, "ZFS replay transaction error %d, "
1420789Sahrens 		    "dataset %s, seq 0x%llx, txtype %llu\n",
1421789Sahrens 		    error, name,
1422789Sahrens 		    (u_longlong_t)lr->lrc_seq, (u_longlong_t)txtype);
1423789Sahrens 		zilog->zl_stop_replay = 1;
1424789Sahrens 		kmem_free(name, MAXNAMELEN);
1425789Sahrens 	}
1426789Sahrens 
1427789Sahrens 	/*
1428789Sahrens 	 * The DMU's dnode layer doesn't see removes until the txg commits,
1429789Sahrens 	 * so a subsequent claim can spuriously fail with EEXIST.
1430789Sahrens 	 * To prevent this, if we might have removed an object,
1431789Sahrens 	 * wait for the delete thread to delete it, and then
1432789Sahrens 	 * wait for the transaction group to sync.
1433789Sahrens 	 */
1434789Sahrens 	if (txtype == TX_REMOVE || txtype == TX_RMDIR || txtype == TX_RENAME) {
1435789Sahrens 		if (zr->zr_rm_sync != NULL)
1436789Sahrens 			zr->zr_rm_sync(zr->zr_arg);
1437789Sahrens 		txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0);
1438789Sahrens 	}
1439789Sahrens }
1440789Sahrens 
1441789Sahrens /*
14421362Sperrin  * If this dataset has a non-empty intent log, replay it and destroy it.
1443789Sahrens  */
1444789Sahrens void
1445789Sahrens zil_replay(objset_t *os, void *arg, uint64_t *txgp,
1446789Sahrens 	zil_replay_func_t *replay_func[TX_MAX_TYPE], void (*rm_sync)(void *arg))
1447789Sahrens {
1448789Sahrens 	zilog_t *zilog = dmu_objset_zil(os);
14491807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
14501807Sbonwick 	zil_replay_arg_t zr;
14511362Sperrin 
14521362Sperrin 	if (zil_empty(zilog)) {
14531807Sbonwick 		zil_destroy(zilog, B_TRUE);
14541362Sperrin 		return;
14551362Sperrin 	}
1456789Sahrens 
1457789Sahrens 	zr.zr_os = os;
1458789Sahrens 	zr.zr_replay = replay_func;
1459789Sahrens 	zr.zr_arg = arg;
1460789Sahrens 	zr.zr_rm_sync = rm_sync;
1461789Sahrens 	zr.zr_txgp = txgp;
14621807Sbonwick 	zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log);
1463789Sahrens 	zr.zr_lrbuf = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP);
1464789Sahrens 
1465789Sahrens 	/*
1466789Sahrens 	 * Wait for in-progress removes to sync before starting replay.
1467789Sahrens 	 */
1468789Sahrens 	if (rm_sync != NULL)
1469789Sahrens 		rm_sync(arg);
1470789Sahrens 	txg_wait_synced(zilog->zl_dmu_pool, 0);
1471789Sahrens 
1472789Sahrens 	zilog->zl_stop_replay = 0;
14731807Sbonwick 	(void) zil_parse(zilog, NULL, zil_replay_log_record, &zr,
14741807Sbonwick 	    zh->zh_claim_txg);
1475789Sahrens 	kmem_free(zr.zr_lrbuf, 2 * SPA_MAXBLOCKSIZE);
1476789Sahrens 
14771807Sbonwick 	zil_destroy(zilog, B_FALSE);
1478789Sahrens }
14791646Sperrin 
14801646Sperrin /*
14811646Sperrin  * Report whether all transactions are committed
14821646Sperrin  */
14831646Sperrin int
14841646Sperrin zil_is_committed(zilog_t *zilog)
14851646Sperrin {
14861646Sperrin 	lwb_t *lwb;
14871646Sperrin 
14881807Sbonwick 	if (!list_is_empty(&zilog->zl_itx_list))
14891646Sperrin 		return (B_FALSE);
14901646Sperrin 
14911646Sperrin 	/*
14921646Sperrin 	 * A log write buffer at the head of the list that is not UNWRITTEN
14931646Sperrin 	 * means there's a lwb yet to be freed after a txg commit
14941646Sperrin 	 */
14951646Sperrin 	lwb = list_head(&zilog->zl_lwb_list);
14961646Sperrin 	if (lwb && lwb->lwb_state != UNWRITTEN)
14971646Sperrin 		return (B_FALSE);
14981646Sperrin 	ASSERT(zil_empty(zilog));
14991646Sperrin 	return (B_TRUE);
15001646Sperrin }
1501