xref: /onnv-gate/usr/src/uts/common/fs/zfs/zil.c (revision 2199)
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;
369789Sahrens 		mutex_enter(&zilog->zl_lock);
370789Sahrens 		list_insert_tail(&zilog->zl_lwb_list, lwb);
371789Sahrens 		mutex_exit(&zilog->zl_lock);
372789Sahrens 	}
373789Sahrens 
3741807Sbonwick 	/*
3751807Sbonwick 	 * If we just allocated the first log block, commit our transaction
3761807Sbonwick 	 * and wait for zil_sync() to stuff the block poiner into zh_log.
3771807Sbonwick 	 * (zh is part of the MOS, so we cannot modify it in open context.)
3781807Sbonwick 	 */
3791807Sbonwick 	if (tx != NULL) {
3801807Sbonwick 		dmu_tx_commit(tx);
3811362Sperrin 		txg_wait_synced(zilog->zl_dmu_pool, txg);
3821807Sbonwick 	}
3831807Sbonwick 
3841807Sbonwick 	ASSERT(bcmp(&blk, &zh->zh_log, sizeof (blk)) == 0);
385789Sahrens }
386789Sahrens 
387789Sahrens /*
388789Sahrens  * In one tx, free all log blocks and clear the log header.
3891807Sbonwick  * If keep_first is set, then we're replaying a log with no content.
3901807Sbonwick  * We want to keep the first block, however, so that the first
3911807Sbonwick  * synchronous transaction doesn't require a txg_wait_synced()
3921807Sbonwick  * in zil_create().  We don't need to txg_wait_synced() here either
3931807Sbonwick  * when keep_first is set, because both zil_create() and zil_destroy()
3941807Sbonwick  * will wait for any in-progress destroys to complete.
395789Sahrens  */
396789Sahrens void
3971807Sbonwick zil_destroy(zilog_t *zilog, boolean_t keep_first)
398789Sahrens {
3991807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
4001807Sbonwick 	lwb_t *lwb;
401789Sahrens 	dmu_tx_t *tx;
402789Sahrens 	uint64_t txg;
403789Sahrens 
4041807Sbonwick 	/*
4051807Sbonwick 	 * Wait for any previous destroy to complete.
4061807Sbonwick 	 */
4071807Sbonwick 	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
408789Sahrens 
4091807Sbonwick 	if (BP_IS_HOLE(&zh->zh_log))
410789Sahrens 		return;
411789Sahrens 
412789Sahrens 	tx = dmu_tx_create(zilog->zl_os);
413789Sahrens 	(void) dmu_tx_assign(tx, TXG_WAIT);
414789Sahrens 	dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
415789Sahrens 	txg = dmu_tx_get_txg(tx);
416789Sahrens 
4171807Sbonwick 	mutex_enter(&zilog->zl_lock);
4181807Sbonwick 
4191807Sbonwick 	ASSERT3U(zilog->zl_destroy_txg, <, txg);
420789Sahrens 	zilog->zl_destroy_txg = txg;
4211807Sbonwick 	zilog->zl_keep_first = keep_first;
4221807Sbonwick 
4231807Sbonwick 	if (!list_is_empty(&zilog->zl_lwb_list)) {
4241807Sbonwick 		ASSERT(zh->zh_claim_txg == 0);
4251807Sbonwick 		ASSERT(!keep_first);
4261807Sbonwick 		while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
4271807Sbonwick 			list_remove(&zilog->zl_lwb_list, lwb);
4281807Sbonwick 			if (lwb->lwb_buf != NULL)
4291807Sbonwick 				zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
4301807Sbonwick 			zio_free_blk(zilog->zl_spa, &lwb->lwb_blk, txg);
4311807Sbonwick 			kmem_cache_free(zil_lwb_cache, lwb);
4321807Sbonwick 		}
4331807Sbonwick 		mutex_exit(&zilog->zl_lock);
4341807Sbonwick 	} else {
4351807Sbonwick 		mutex_exit(&zilog->zl_lock);
4361807Sbonwick 		if (!keep_first) {
4371807Sbonwick 			(void) zil_parse(zilog, zil_free_log_block,
4381807Sbonwick 			    zil_free_log_record, tx, zh->zh_claim_txg);
4391807Sbonwick 		}
4401807Sbonwick 	}
441789Sahrens 
442789Sahrens 	dmu_tx_commit(tx);
443789Sahrens 
4441807Sbonwick 	if (keep_first)			/* no need to wait in this case */
4451807Sbonwick 		return;
4461807Sbonwick 
4471807Sbonwick 	txg_wait_synced(zilog->zl_dmu_pool, txg);
4481807Sbonwick 	ASSERT(BP_IS_HOLE(&zh->zh_log));
449789Sahrens }
450789Sahrens 
451*2199Sahrens int
452789Sahrens zil_claim(char *osname, void *txarg)
453789Sahrens {
454789Sahrens 	dmu_tx_t *tx = txarg;
455789Sahrens 	uint64_t first_txg = dmu_tx_get_txg(tx);
456789Sahrens 	zilog_t *zilog;
457789Sahrens 	zil_header_t *zh;
458789Sahrens 	objset_t *os;
459789Sahrens 	int error;
460789Sahrens 
461789Sahrens 	error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_STANDARD, &os);
462789Sahrens 	if (error) {
463789Sahrens 		cmn_err(CE_WARN, "can't process intent log for %s", osname);
464*2199Sahrens 		return (0);
465789Sahrens 	}
466789Sahrens 
467789Sahrens 	zilog = dmu_objset_zil(os);
4681807Sbonwick 	zh = zil_header_in_syncing_context(zilog);
469789Sahrens 
470789Sahrens 	/*
4711807Sbonwick 	 * Claim all log blocks if we haven't already done so, and remember
4721807Sbonwick 	 * the highest claimed sequence number.  This ensures that if we can
4731807Sbonwick 	 * read only part of the log now (e.g. due to a missing device),
4741807Sbonwick 	 * but we can read the entire log later, we will not try to replay
4751807Sbonwick 	 * or destroy beyond the last block we successfully claimed.
476789Sahrens 	 */
477789Sahrens 	ASSERT3U(zh->zh_claim_txg, <=, first_txg);
478789Sahrens 	if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) {
479789Sahrens 		zh->zh_claim_txg = first_txg;
4801807Sbonwick 		zh->zh_claim_seq = zil_parse(zilog, zil_claim_log_block,
4811807Sbonwick 		    zil_claim_log_record, tx, first_txg);
482789Sahrens 		dsl_dataset_dirty(dmu_objset_ds(os), tx);
483789Sahrens 	}
4841807Sbonwick 
485789Sahrens 	ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1));
486789Sahrens 	dmu_objset_close(os);
487*2199Sahrens 	return (0);
488789Sahrens }
489789Sahrens 
490789Sahrens void
491789Sahrens zil_add_vdev(zilog_t *zilog, uint64_t vdev, uint64_t seq)
492789Sahrens {
493789Sahrens 	zil_vdev_t *zv;
494789Sahrens 
495789Sahrens 	if (zil_noflush)
496789Sahrens 		return;
497789Sahrens 
498789Sahrens 	ASSERT(MUTEX_HELD(&zilog->zl_lock));
499789Sahrens 	zv = kmem_alloc(sizeof (zil_vdev_t), KM_SLEEP);
500789Sahrens 	zv->vdev = vdev;
501789Sahrens 	zv->seq = seq;
502789Sahrens 	list_insert_tail(&zilog->zl_vdev_list, zv);
503789Sahrens }
504789Sahrens 
505789Sahrens void
506789Sahrens zil_flush_vdevs(zilog_t *zilog, uint64_t seq)
507789Sahrens {
508789Sahrens 	vdev_t *vd;
509789Sahrens 	zil_vdev_t *zv, *zv2;
510789Sahrens 	zio_t *zio;
511789Sahrens 	spa_t *spa;
512789Sahrens 	uint64_t vdev;
513789Sahrens 
514789Sahrens 	if (zil_noflush)
515789Sahrens 		return;
516789Sahrens 
517789Sahrens 	ASSERT(MUTEX_HELD(&zilog->zl_lock));
518789Sahrens 
519789Sahrens 	spa = zilog->zl_spa;
520789Sahrens 	zio = NULL;
521789Sahrens 
522789Sahrens 	while ((zv = list_head(&zilog->zl_vdev_list)) != NULL &&
523789Sahrens 	    zv->seq <= seq) {
524789Sahrens 		vdev = zv->vdev;
525789Sahrens 		list_remove(&zilog->zl_vdev_list, zv);
526789Sahrens 		kmem_free(zv, sizeof (zil_vdev_t));
527789Sahrens 
528789Sahrens 		/*
529789Sahrens 		 * remove all chained entries <= seq with same vdev
530789Sahrens 		 */
531789Sahrens 		zv = list_head(&zilog->zl_vdev_list);
532789Sahrens 		while (zv && zv->seq <= seq) {
533789Sahrens 			zv2 = list_next(&zilog->zl_vdev_list, zv);
534789Sahrens 			if (zv->vdev == vdev) {
535789Sahrens 				list_remove(&zilog->zl_vdev_list, zv);
536789Sahrens 				kmem_free(zv, sizeof (zil_vdev_t));
537789Sahrens 			}
538789Sahrens 			zv = zv2;
539789Sahrens 		}
540789Sahrens 
541789Sahrens 		/* flush the write cache for this vdev */
542789Sahrens 		mutex_exit(&zilog->zl_lock);
543789Sahrens 		if (zio == NULL)
544789Sahrens 			zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
545789Sahrens 		vd = vdev_lookup_top(spa, vdev);
546789Sahrens 		ASSERT(vd);
547789Sahrens 		(void) zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE,
548789Sahrens 		    NULL, NULL, ZIO_PRIORITY_NOW,
549789Sahrens 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
550789Sahrens 		mutex_enter(&zilog->zl_lock);
551789Sahrens 	}
552789Sahrens 
553789Sahrens 	/*
554789Sahrens 	 * Wait for all the flushes to complete.  Not all devices actually
555789Sahrens 	 * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails.
556789Sahrens 	 */
5571141Sperrin 	if (zio != NULL) {
5581141Sperrin 		mutex_exit(&zilog->zl_lock);
559789Sahrens 		(void) zio_wait(zio);
5601141Sperrin 		mutex_enter(&zilog->zl_lock);
5611141Sperrin 	}
562789Sahrens }
563789Sahrens 
564789Sahrens /*
565789Sahrens  * Function called when a log block write completes
566789Sahrens  */
567789Sahrens static void
568789Sahrens zil_lwb_write_done(zio_t *zio)
569789Sahrens {
570789Sahrens 	lwb_t *prev;
571789Sahrens 	lwb_t *lwb = zio->io_private;
572789Sahrens 	zilog_t *zilog = lwb->lwb_zilog;
573789Sahrens 	uint64_t max_seq;
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 		cv_broadcast(&zilog->zl_cv_seq);
589789Sahrens 		return;
590789Sahrens 	}
591789Sahrens 
592789Sahrens 	prev = list_prev(&zilog->zl_lwb_list, lwb);
593789Sahrens 	if (prev && prev->lwb_state != SEQ_COMPLETE) {
594789Sahrens 		/* There's an unwritten buffer in the chain before this one */
595789Sahrens 		lwb->lwb_state = SEQ_INCOMPLETE;
596789Sahrens 		mutex_exit(&zilog->zl_lock);
597789Sahrens 		return;
598789Sahrens 	}
599789Sahrens 
600789Sahrens 	max_seq = lwb->lwb_seq;
601789Sahrens 	lwb->lwb_state = SEQ_COMPLETE;
602789Sahrens 	/*
603789Sahrens 	 * We must also follow up the chain for already written buffers
604789Sahrens 	 * to see if we can set zl_ss_seq even higher.
605789Sahrens 	 */
606789Sahrens 	while (lwb = list_next(&zilog->zl_lwb_list, lwb)) {
607789Sahrens 		if (lwb->lwb_state != SEQ_INCOMPLETE)
608789Sahrens 			break;
609789Sahrens 		lwb->lwb_state = SEQ_COMPLETE;
610789Sahrens 		/* lwb_seq will be zero if we've written an empty buffer */
611789Sahrens 		if (lwb->lwb_seq) {
612789Sahrens 			ASSERT3U(max_seq, <, lwb->lwb_seq);
613789Sahrens 			max_seq = lwb->lwb_seq;
614789Sahrens 		}
615789Sahrens 	}
616789Sahrens 	zilog->zl_ss_seq = MAX(max_seq, zilog->zl_ss_seq);
617789Sahrens 	mutex_exit(&zilog->zl_lock);
618789Sahrens 	cv_broadcast(&zilog->zl_cv_seq);
619789Sahrens }
620789Sahrens 
621789Sahrens /*
622789Sahrens  * Start a log block write and advance to the next log block.
623789Sahrens  * Calls are serialized.
624789Sahrens  */
625789Sahrens static lwb_t *
626789Sahrens zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb)
627789Sahrens {
628789Sahrens 	lwb_t *nlwb;
629789Sahrens 	zil_trailer_t *ztp = (zil_trailer_t *)(lwb->lwb_buf + lwb->lwb_sz) - 1;
6301807Sbonwick 	spa_t *spa = zilog->zl_spa;
6311807Sbonwick 	blkptr_t *bp = &ztp->zit_next_blk;
632789Sahrens 	uint64_t txg;
633789Sahrens 	uint64_t zil_blksz;
6341544Seschrock 	zbookmark_t zb;
635789Sahrens 	int error;
636789Sahrens 
637789Sahrens 	ASSERT(lwb->lwb_nused <= ZIL_BLK_DATA_SZ(lwb));
638789Sahrens 
639789Sahrens 	/*
640789Sahrens 	 * Allocate the next block and save its address in this block
641789Sahrens 	 * before writing it in order to establish the log chain.
642789Sahrens 	 * Note that if the allocation of nlwb synced before we wrote
643789Sahrens 	 * the block that points at it (lwb), we'd leak it if we crashed.
644789Sahrens 	 * Therefore, we don't do txg_rele_to_sync() until zil_lwb_write_done().
645789Sahrens 	 */
646789Sahrens 	txg = txg_hold_open(zilog->zl_dmu_pool, &lwb->lwb_txgh);
647789Sahrens 	txg_rele_to_quiesce(&lwb->lwb_txgh);
648789Sahrens 
649789Sahrens 	/*
6501141Sperrin 	 * Pick a ZIL blocksize. We request a size that is the
6511141Sperrin 	 * maximum of the previous used size, the current used size and
6521141Sperrin 	 * the amount waiting in the queue.
653789Sahrens 	 */
6541141Sperrin 	zil_blksz = MAX(zilog->zl_cur_used, zilog->zl_prev_used);
6551141Sperrin 	zil_blksz = MAX(zil_blksz, zilog->zl_itx_list_sz + sizeof (*ztp));
6561842Sperrin 	zil_blksz = P2ROUNDUP_TYPED(zil_blksz, ZIL_MIN_BLKSZ, uint64_t);
6571141Sperrin 	if (zil_blksz > ZIL_MAX_BLKSZ)
6581141Sperrin 		zil_blksz = ZIL_MAX_BLKSZ;
659789Sahrens 
6601807Sbonwick 	error = zio_alloc_blk(spa, zil_blksz, bp, txg);
661789Sahrens 	if (error) {
6621544Seschrock 		/*
6631544Seschrock 		 * Reinitialise the lwb.
6641544Seschrock 		 * By returning NULL the caller will call tx_wait_synced()
6651544Seschrock 		 */
6661544Seschrock 		mutex_enter(&zilog->zl_lock);
6671544Seschrock 		ASSERT(lwb->lwb_state == UNWRITTEN);
6681544Seschrock 		lwb->lwb_nused = 0;
6691544Seschrock 		lwb->lwb_seq = 0;
6701544Seschrock 		mutex_exit(&zilog->zl_lock);
671789Sahrens 		txg_rele_to_sync(&lwb->lwb_txgh);
672789Sahrens 		return (NULL);
673789Sahrens 	}
674789Sahrens 
6751807Sbonwick 	ASSERT3U(bp->blk_birth, ==, txg);
6761544Seschrock 	ztp->zit_pad = 0;
677789Sahrens 	ztp->zit_nused = lwb->lwb_nused;
678789Sahrens 	ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum;
6791807Sbonwick 	bp->blk_cksum = lwb->lwb_blk.blk_cksum;
6801807Sbonwick 	bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++;
681789Sahrens 
682789Sahrens 	/*
683789Sahrens 	 * Allocate a new log write buffer (lwb).
684789Sahrens 	 */
685789Sahrens 	nlwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP);
686789Sahrens 
687789Sahrens 	nlwb->lwb_zilog = zilog;
6881807Sbonwick 	nlwb->lwb_blk = *bp;
689789Sahrens 	nlwb->lwb_nused = 0;
690789Sahrens 	nlwb->lwb_sz = BP_GET_LSIZE(&nlwb->lwb_blk);
691789Sahrens 	nlwb->lwb_buf = zio_buf_alloc(nlwb->lwb_sz);
692789Sahrens 	nlwb->lwb_max_txg = txg;
693789Sahrens 	nlwb->lwb_seq = 0;
694789Sahrens 	nlwb->lwb_state = UNWRITTEN;
695789Sahrens 
696789Sahrens 	/*
697789Sahrens 	 * Put new lwb at the end of the log chain,
698789Sahrens 	 * and record the vdev for later flushing
699789Sahrens 	 */
700789Sahrens 	mutex_enter(&zilog->zl_lock);
701789Sahrens 	list_insert_tail(&zilog->zl_lwb_list, nlwb);
702789Sahrens 	zil_add_vdev(zilog, DVA_GET_VDEV(BP_IDENTITY(&(lwb->lwb_blk))),
703789Sahrens 	    lwb->lwb_seq);
704789Sahrens 	mutex_exit(&zilog->zl_lock);
705789Sahrens 
706789Sahrens 	/*
707789Sahrens 	 * write the old log block
708789Sahrens 	 */
7091807Sbonwick 	zb.zb_objset = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET];
7101544Seschrock 	zb.zb_object = 0;
7111544Seschrock 	zb.zb_level = -1;
7121807Sbonwick 	zb.zb_blkid = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ];
7131544Seschrock 
7141807Sbonwick 	zio_nowait(zio_rewrite(NULL, spa, ZIO_CHECKSUM_ZILOG, 0,
715789Sahrens 	    &lwb->lwb_blk, lwb->lwb_buf, lwb->lwb_sz, zil_lwb_write_done, lwb,
7161544Seschrock 	    ZIO_PRIORITY_LOG_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb));
717789Sahrens 
718789Sahrens 	return (nlwb);
719789Sahrens }
720789Sahrens 
721789Sahrens static lwb_t *
722789Sahrens zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb)
723789Sahrens {
724789Sahrens 	lr_t *lrc = &itx->itx_lr; /* common log record */
7251669Sperrin 	lr_write_t *lr;
7261669Sperrin 	char *dbuf;
727789Sahrens 	uint64_t seq = lrc->lrc_seq;
728789Sahrens 	uint64_t txg = lrc->lrc_txg;
729789Sahrens 	uint64_t reclen = lrc->lrc_reclen;
7301669Sperrin 	uint64_t dlen = 0;
731789Sahrens 	int error;
732789Sahrens 
733789Sahrens 	if (lwb == NULL)
734789Sahrens 		return (NULL);
735789Sahrens 	ASSERT(lwb->lwb_buf != NULL);
736789Sahrens 
737789Sahrens 	/*
738789Sahrens 	 * If it's a write, fetch the data or get its blkptr as appropriate.
739789Sahrens 	 */
740789Sahrens 	if (lrc->lrc_txtype == TX_WRITE) {
7411669Sperrin 		lr = (lr_write_t *)lrc;
742789Sahrens 		if (txg > spa_freeze_txg(zilog->zl_spa))
743789Sahrens 			txg_wait_synced(zilog->zl_dmu_pool, txg);
7441669Sperrin 		if (itx->itx_wr_state != WR_COPIED) {
7451669Sperrin 			if (itx->itx_wr_state == WR_NEED_COPY) {
7461842Sperrin 				dlen = P2ROUNDUP_TYPED(lr->lr_length,
7471842Sperrin 				    sizeof (uint64_t), uint64_t);
7481669Sperrin 				ASSERT(dlen);
7491669Sperrin 				dbuf = kmem_alloc(dlen, KM_NOSLEEP);
7501669Sperrin 				/* on memory shortage use dmu_sync */
7511669Sperrin 				if (dbuf == NULL) {
7521669Sperrin 					itx->itx_wr_state = WR_INDIRECT;
7531669Sperrin 					dlen = 0;
7541669Sperrin 				}
7551669Sperrin 			} else {
7561669Sperrin 				ASSERT(itx->itx_wr_state == WR_INDIRECT);
7571669Sperrin 				dbuf = NULL;
7581669Sperrin 			}
7591669Sperrin 			error = zilog->zl_get_data(itx->itx_private, lr, dbuf);
7601669Sperrin 			if (error) {
7611669Sperrin 				if (dlen)
7621669Sperrin 					kmem_free(dbuf, dlen);
7631669Sperrin 				if (error != ENOENT && error != EALREADY) {
7641669Sperrin 					txg_wait_synced(zilog->zl_dmu_pool,
7651669Sperrin 					    txg);
7661669Sperrin 					mutex_enter(&zilog->zl_lock);
7671669Sperrin 					zilog->zl_ss_seq =
7681669Sperrin 					    MAX(seq, zilog->zl_ss_seq);
7691669Sperrin 					mutex_exit(&zilog->zl_lock);
7701669Sperrin 					return (lwb);
7711669Sperrin 				}
772789Sahrens 				mutex_enter(&zilog->zl_lock);
7731669Sperrin 				zil_add_vdev(zilog, DVA_GET_VDEV(BP_IDENTITY(
7741669Sperrin 				    &(lr->lr_blkptr))), seq);
775789Sahrens 				mutex_exit(&zilog->zl_lock);
776789Sahrens 				return (lwb);
777789Sahrens 			}
7781669Sperrin 		}
7791669Sperrin 	}
7801669Sperrin 
7811669Sperrin 	zilog->zl_cur_used += (reclen + dlen);
7821669Sperrin 
7831669Sperrin 	/*
7841669Sperrin 	 * If this record won't fit in the current log block, start a new one.
7851669Sperrin 	 */
7861669Sperrin 	if (lwb->lwb_nused + reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) {
7871669Sperrin 		lwb = zil_lwb_write_start(zilog, lwb);
7881669Sperrin 		if (lwb == NULL) {
7891669Sperrin 			if (dlen)
7901669Sperrin 				kmem_free(dbuf, dlen);
7911669Sperrin 			return (NULL);
7921669Sperrin 		}
7931669Sperrin 		ASSERT(lwb->lwb_nused == 0);
7941669Sperrin 		if (reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) {
7951669Sperrin 			txg_wait_synced(zilog->zl_dmu_pool, txg);
796789Sahrens 			mutex_enter(&zilog->zl_lock);
7971669Sperrin 			zilog->zl_ss_seq = MAX(seq, zilog->zl_ss_seq);
798789Sahrens 			mutex_exit(&zilog->zl_lock);
7991669Sperrin 			if (dlen)
8001669Sperrin 				kmem_free(dbuf, dlen);
801789Sahrens 			return (lwb);
802789Sahrens 		}
803789Sahrens 	}
804789Sahrens 
8051669Sperrin 	lrc->lrc_reclen += dlen;
806789Sahrens 	bcopy(lrc, lwb->lwb_buf + lwb->lwb_nused, reclen);
807789Sahrens 	lwb->lwb_nused += reclen;
8081669Sperrin 	if (dlen) {
8091669Sperrin 		bcopy(dbuf, lwb->lwb_buf + lwb->lwb_nused, dlen);
8101669Sperrin 		lwb->lwb_nused += dlen;
8111669Sperrin 		kmem_free(dbuf, dlen);
8121669Sperrin 		lrc->lrc_reclen -= dlen; /* for kmem_free of itx */
8131669Sperrin 	}
814789Sahrens 	lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg);
815789Sahrens 	ASSERT3U(lwb->lwb_seq, <, seq);
816789Sahrens 	lwb->lwb_seq = seq;
817789Sahrens 	ASSERT3U(lwb->lwb_nused, <=, ZIL_BLK_DATA_SZ(lwb));
818789Sahrens 	ASSERT3U(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)), ==, 0);
819789Sahrens 
820789Sahrens 	return (lwb);
821789Sahrens }
822789Sahrens 
823789Sahrens itx_t *
824789Sahrens zil_itx_create(int txtype, size_t lrsize)
825789Sahrens {
826789Sahrens 	itx_t *itx;
827789Sahrens 
8281842Sperrin 	lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t);
829789Sahrens 
830789Sahrens 	itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP);
831789Sahrens 	itx->itx_lr.lrc_txtype = txtype;
832789Sahrens 	itx->itx_lr.lrc_reclen = lrsize;
833789Sahrens 	itx->itx_lr.lrc_seq = 0;	/* defensive */
834789Sahrens 
835789Sahrens 	return (itx);
836789Sahrens }
837789Sahrens 
838789Sahrens uint64_t
839789Sahrens zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx)
840789Sahrens {
841789Sahrens 	uint64_t seq;
842789Sahrens 
843789Sahrens 	ASSERT(itx->itx_lr.lrc_seq == 0);
844789Sahrens 
845789Sahrens 	mutex_enter(&zilog->zl_lock);
846789Sahrens 	list_insert_tail(&zilog->zl_itx_list, itx);
847789Sahrens 	zilog->zl_itx_list_sz += itx->itx_lr.lrc_reclen;
848789Sahrens 	itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx);
849789Sahrens 	itx->itx_lr.lrc_seq = seq = ++zilog->zl_itx_seq;
850789Sahrens 	mutex_exit(&zilog->zl_lock);
851789Sahrens 
852789Sahrens 	return (seq);
853789Sahrens }
854789Sahrens 
855789Sahrens /*
856789Sahrens  * Free up all in-memory intent log transactions that have now been synced.
857789Sahrens  */
858789Sahrens static void
859789Sahrens zil_itx_clean(zilog_t *zilog)
860789Sahrens {
861789Sahrens 	uint64_t synced_txg = spa_last_synced_txg(zilog->zl_spa);
862789Sahrens 	uint64_t freeze_txg = spa_freeze_txg(zilog->zl_spa);
863789Sahrens 	uint64_t max_seq = 0;
864789Sahrens 	itx_t *itx;
865789Sahrens 
866789Sahrens 	mutex_enter(&zilog->zl_lock);
867789Sahrens 	while ((itx = list_head(&zilog->zl_itx_list)) != NULL &&
868789Sahrens 	    itx->itx_lr.lrc_txg <= MIN(synced_txg, freeze_txg)) {
869789Sahrens 		list_remove(&zilog->zl_itx_list, itx);
870789Sahrens 		zilog->zl_itx_list_sz -= itx->itx_lr.lrc_reclen;
871789Sahrens 		ASSERT3U(max_seq, <, itx->itx_lr.lrc_seq);
872789Sahrens 		max_seq = itx->itx_lr.lrc_seq;
873789Sahrens 		kmem_free(itx, offsetof(itx_t, itx_lr)
874789Sahrens 		    + itx->itx_lr.lrc_reclen);
875789Sahrens 	}
876789Sahrens 	if (max_seq > zilog->zl_ss_seq) {
877789Sahrens 		zilog->zl_ss_seq = max_seq;
878789Sahrens 		cv_broadcast(&zilog->zl_cv_seq);
879789Sahrens 	}
880789Sahrens 	mutex_exit(&zilog->zl_lock);
881789Sahrens }
882789Sahrens 
883789Sahrens void
884789Sahrens zil_clean(zilog_t *zilog)
885789Sahrens {
886789Sahrens 	/*
887789Sahrens 	 * Check for any log blocks that can be freed.
888789Sahrens 	 * Log blocks are only freed when the log block allocation and
889789Sahrens 	 * log records contained within are both known to be committed.
890789Sahrens 	 */
891789Sahrens 	mutex_enter(&zilog->zl_lock);
892789Sahrens 	if (list_head(&zilog->zl_itx_list) != NULL)
893789Sahrens 		(void) taskq_dispatch(zilog->zl_clean_taskq,
894789Sahrens 		    (void (*)(void *))zil_itx_clean, zilog, TQ_NOSLEEP);
895789Sahrens 	mutex_exit(&zilog->zl_lock);
896789Sahrens }
897789Sahrens 
898789Sahrens /*
899789Sahrens  * Push zfs transactions to stable storage up to the supplied sequence number.
900789Sahrens  */
901789Sahrens void
902789Sahrens zil_commit(zilog_t *zilog, uint64_t seq, int ioflag)
903789Sahrens {
904789Sahrens 	uint64_t txg;
905789Sahrens 	uint64_t max_seq;
906789Sahrens 	uint64_t reclen;
907789Sahrens 	itx_t *itx;
908789Sahrens 	lwb_t *lwb;
909789Sahrens 	spa_t *spa;
910789Sahrens 
911789Sahrens 	if (zilog == NULL || seq == 0 ||
912789Sahrens 	    ((ioflag & (FSYNC | FDSYNC | FRSYNC)) == 0 && !zil_always))
913789Sahrens 		return;
914789Sahrens 
915789Sahrens 	spa = zilog->zl_spa;
916789Sahrens 	mutex_enter(&zilog->zl_lock);
917789Sahrens 
918789Sahrens 	seq = MIN(seq, zilog->zl_itx_seq);	/* cap seq at largest itx seq */
919789Sahrens 
920789Sahrens 	for (;;) {
921789Sahrens 		if (zilog->zl_ss_seq >= seq) {	/* already on stable storage */
922789Sahrens 			mutex_exit(&zilog->zl_lock);
923789Sahrens 			return;
924789Sahrens 		}
925789Sahrens 
926789Sahrens 		if (zilog->zl_writer == B_FALSE) /* no one writing, do it */
927789Sahrens 			break;
928789Sahrens 
929789Sahrens 		cv_wait(&zilog->zl_cv_write, &zilog->zl_lock);
930789Sahrens 	}
931789Sahrens 
932789Sahrens 	zilog->zl_writer = B_TRUE;
933789Sahrens 	max_seq = 0;
934789Sahrens 
935789Sahrens 	if (zilog->zl_suspend) {
936789Sahrens 		lwb = NULL;
937789Sahrens 	} else {
938789Sahrens 		lwb = list_tail(&zilog->zl_lwb_list);
939789Sahrens 		if (lwb == NULL) {
940789Sahrens 			mutex_exit(&zilog->zl_lock);
941789Sahrens 			zil_create(zilog);
942789Sahrens 			mutex_enter(&zilog->zl_lock);
943789Sahrens 			lwb = list_tail(&zilog->zl_lwb_list);
944789Sahrens 		}
945789Sahrens 	}
946789Sahrens 
947789Sahrens 	/*
948789Sahrens 	 * Loop through in-memory log transactions filling log blocks,
949789Sahrens 	 * until we reach the given sequence number and there's no more
950789Sahrens 	 * room in the write buffer.
951789Sahrens 	 */
952789Sahrens 	for (;;) {
953789Sahrens 		itx = list_head(&zilog->zl_itx_list);
954789Sahrens 		if (itx == NULL)
955789Sahrens 			break;
956789Sahrens 
957789Sahrens 		reclen = itx->itx_lr.lrc_reclen;
958789Sahrens 		if ((itx->itx_lr.lrc_seq > seq) &&
959789Sahrens 		    ((lwb == NULL) || (lwb->lwb_nused + reclen >
960789Sahrens 		    ZIL_BLK_DATA_SZ(lwb))))
961789Sahrens 			break;
962789Sahrens 
963789Sahrens 		list_remove(&zilog->zl_itx_list, itx);
964789Sahrens 		txg = itx->itx_lr.lrc_txg;
965789Sahrens 		ASSERT(txg);
966789Sahrens 
967789Sahrens 		mutex_exit(&zilog->zl_lock);
968789Sahrens 		if (txg > spa_last_synced_txg(spa) ||
969789Sahrens 		    txg > spa_freeze_txg(spa))
970789Sahrens 			lwb = zil_lwb_commit(zilog, itx, lwb);
971789Sahrens 		else
972789Sahrens 			max_seq = itx->itx_lr.lrc_seq;
973789Sahrens 		kmem_free(itx, offsetof(itx_t, itx_lr)
974789Sahrens 		    + itx->itx_lr.lrc_reclen);
975789Sahrens 		mutex_enter(&zilog->zl_lock);
976789Sahrens 		zilog->zl_itx_list_sz -= reclen;
977789Sahrens 	}
978789Sahrens 
979789Sahrens 	mutex_exit(&zilog->zl_lock);
980789Sahrens 
981789Sahrens 	/* write the last block out */
982789Sahrens 	if (lwb != NULL && lwb->lwb_nused != 0)
983789Sahrens 		lwb = zil_lwb_write_start(zilog, lwb);
984789Sahrens 
9851141Sperrin 	zilog->zl_prev_used = zilog->zl_cur_used;
9861141Sperrin 	zilog->zl_cur_used = 0;
9871141Sperrin 
988789Sahrens 	mutex_enter(&zilog->zl_lock);
989789Sahrens 	if (max_seq > zilog->zl_ss_seq) {
990789Sahrens 		zilog->zl_ss_seq = max_seq;
991789Sahrens 		cv_broadcast(&zilog->zl_cv_seq);
992789Sahrens 	}
993789Sahrens 	/*
994789Sahrens 	 * Wait if necessary for our seq to be committed.
995789Sahrens 	 */
996789Sahrens 	if (lwb) {
997789Sahrens 		while (zilog->zl_ss_seq < seq && zilog->zl_log_error == 0)
998789Sahrens 			cv_wait(&zilog->zl_cv_seq, &zilog->zl_lock);
999789Sahrens 		zil_flush_vdevs(zilog, seq);
1000789Sahrens 	}
10011141Sperrin 
1002789Sahrens 	if (zilog->zl_log_error || lwb == NULL) {
1003789Sahrens 		zilog->zl_log_error = 0;
1004789Sahrens 		max_seq = zilog->zl_itx_seq;
1005789Sahrens 		mutex_exit(&zilog->zl_lock);
1006789Sahrens 		txg_wait_synced(zilog->zl_dmu_pool, 0);
1007789Sahrens 		mutex_enter(&zilog->zl_lock);
1008789Sahrens 		zilog->zl_ss_seq = MAX(max_seq, zilog->zl_ss_seq);
1009789Sahrens 		cv_broadcast(&zilog->zl_cv_seq);
1010789Sahrens 	}
10111141Sperrin 	/* wake up others waiting to start a write */
10121141Sperrin 	zilog->zl_writer = B_FALSE;
1013789Sahrens 	mutex_exit(&zilog->zl_lock);
10141472Sperrin 	cv_broadcast(&zilog->zl_cv_write);
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 		}
1063789Sahrens 		if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg)
1064789Sahrens 			break;
1065789Sahrens 		list_remove(&zilog->zl_lwb_list, lwb);
1066789Sahrens 		zio_free_blk(spa, &lwb->lwb_blk, txg);
1067789Sahrens 		kmem_cache_free(zil_lwb_cache, lwb);
1068789Sahrens 	}
10691807Sbonwick 	zh->zh_log = lwb->lwb_blk;
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",
1077789Sahrens 	    sizeof (struct lwb), NULL, 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 
1099789Sahrens 	list_create(&zilog->zl_itx_list, sizeof (itx_t),
1100789Sahrens 	    offsetof(itx_t, itx_node));
1101789Sahrens 
1102789Sahrens 	list_create(&zilog->zl_lwb_list, sizeof (lwb_t),
1103789Sahrens 	    offsetof(lwb_t, lwb_node));
1104789Sahrens 
1105789Sahrens 	list_create(&zilog->zl_vdev_list, sizeof (zil_vdev_t),
1106789Sahrens 	    offsetof(zil_vdev_t, vdev_seq_node));
1107789Sahrens 
1108789Sahrens 	return (zilog);
1109789Sahrens }
1110789Sahrens 
1111789Sahrens void
1112789Sahrens zil_free(zilog_t *zilog)
1113789Sahrens {
1114789Sahrens 	lwb_t *lwb;
1115789Sahrens 	zil_vdev_t *zv;
1116789Sahrens 
1117789Sahrens 	zilog->zl_stop_sync = 1;
1118789Sahrens 
1119789Sahrens 	while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
1120789Sahrens 		list_remove(&zilog->zl_lwb_list, lwb);
1121789Sahrens 		if (lwb->lwb_buf != NULL)
1122789Sahrens 			zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
1123789Sahrens 		kmem_cache_free(zil_lwb_cache, lwb);
1124789Sahrens 	}
1125789Sahrens 	list_destroy(&zilog->zl_lwb_list);
1126789Sahrens 
1127789Sahrens 	while ((zv = list_head(&zilog->zl_vdev_list)) != NULL) {
1128789Sahrens 		list_remove(&zilog->zl_vdev_list, zv);
1129789Sahrens 		kmem_free(zv, sizeof (zil_vdev_t));
1130789Sahrens 	}
1131789Sahrens 	list_destroy(&zilog->zl_vdev_list);
1132789Sahrens 
1133789Sahrens 	ASSERT(list_head(&zilog->zl_itx_list) == NULL);
1134789Sahrens 	list_destroy(&zilog->zl_itx_list);
1135789Sahrens 
1136789Sahrens 	kmem_free(zilog, sizeof (zilog_t));
1137789Sahrens }
1138789Sahrens 
1139789Sahrens /*
11401646Sperrin  * return true if the initial log block is not valid
11411362Sperrin  */
11421362Sperrin static int
11431362Sperrin zil_empty(zilog_t *zilog)
11441362Sperrin {
11451807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
11461807Sbonwick 	arc_buf_t *abuf = NULL;
11471362Sperrin 
11481807Sbonwick 	if (BP_IS_HOLE(&zh->zh_log))
11491362Sperrin 		return (1);
11501362Sperrin 
11511807Sbonwick 	if (zil_read_log_block(zilog, &zh->zh_log, &abuf) != 0)
11521807Sbonwick 		return (1);
11531807Sbonwick 
11541807Sbonwick 	VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
11551807Sbonwick 	return (0);
11561362Sperrin }
11571362Sperrin 
11581362Sperrin /*
1159789Sahrens  * Open an intent log.
1160789Sahrens  */
1161789Sahrens zilog_t *
1162789Sahrens zil_open(objset_t *os, zil_get_data_t *get_data)
1163789Sahrens {
1164789Sahrens 	zilog_t *zilog = dmu_objset_zil(os);
1165789Sahrens 
1166789Sahrens 	zilog->zl_get_data = get_data;
1167789Sahrens 	zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri,
1168789Sahrens 	    2, 2, TASKQ_PREPOPULATE);
1169789Sahrens 
1170789Sahrens 	return (zilog);
1171789Sahrens }
1172789Sahrens 
1173789Sahrens /*
1174789Sahrens  * Close an intent log.
1175789Sahrens  */
1176789Sahrens void
1177789Sahrens zil_close(zilog_t *zilog)
1178789Sahrens {
11791807Sbonwick 	/*
11801807Sbonwick 	 * If the log isn't already committed, mark the objset dirty
11811807Sbonwick 	 * (so zil_sync() will be called) and wait for that txg to sync.
11821807Sbonwick 	 */
11831807Sbonwick 	if (!zil_is_committed(zilog)) {
11841807Sbonwick 		uint64_t txg;
11851807Sbonwick 		dmu_tx_t *tx = dmu_tx_create(zilog->zl_os);
11861807Sbonwick 		(void) dmu_tx_assign(tx, TXG_WAIT);
11871807Sbonwick 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
11881807Sbonwick 		txg = dmu_tx_get_txg(tx);
11891807Sbonwick 		dmu_tx_commit(tx);
11901807Sbonwick 		txg_wait_synced(zilog->zl_dmu_pool, txg);
11911807Sbonwick 	}
11921807Sbonwick 
1193789Sahrens 	taskq_destroy(zilog->zl_clean_taskq);
1194789Sahrens 	zilog->zl_clean_taskq = NULL;
1195789Sahrens 	zilog->zl_get_data = NULL;
1196789Sahrens 
1197789Sahrens 	zil_itx_clean(zilog);
1198789Sahrens 	ASSERT(list_head(&zilog->zl_itx_list) == NULL);
1199789Sahrens }
1200789Sahrens 
1201789Sahrens /*
1202789Sahrens  * Suspend an intent log.  While in suspended mode, we still honor
1203789Sahrens  * synchronous semantics, but we rely on txg_wait_synced() to do it.
1204789Sahrens  * We suspend the log briefly when taking a snapshot so that the snapshot
1205789Sahrens  * contains all the data it's supposed to, and has an empty intent log.
1206789Sahrens  */
1207789Sahrens int
1208789Sahrens zil_suspend(zilog_t *zilog)
1209789Sahrens {
12101807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
1211789Sahrens 	lwb_t *lwb;
1212789Sahrens 
1213789Sahrens 	mutex_enter(&zilog->zl_lock);
12141807Sbonwick 	if (zh->zh_claim_txg != 0) {		/* unplayed log */
1215789Sahrens 		mutex_exit(&zilog->zl_lock);
1216789Sahrens 		return (EBUSY);
1217789Sahrens 	}
12181807Sbonwick 	if (zilog->zl_suspend++ != 0) {
12191807Sbonwick 		/*
12201807Sbonwick 		 * Someone else already began a suspend.
12211807Sbonwick 		 * Just wait for them to finish.
12221807Sbonwick 		 */
12231807Sbonwick 		while (zilog->zl_suspending)
12241807Sbonwick 			cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock);
12251807Sbonwick 		ASSERT(BP_IS_HOLE(&zh->zh_log));
12261807Sbonwick 		mutex_exit(&zilog->zl_lock);
12271807Sbonwick 		return (0);
12281807Sbonwick 	}
12291807Sbonwick 	zilog->zl_suspending = B_TRUE;
1230789Sahrens 	mutex_exit(&zilog->zl_lock);
1231789Sahrens 
1232789Sahrens 	zil_commit(zilog, UINT64_MAX, FSYNC);
1233789Sahrens 
1234789Sahrens 	mutex_enter(&zilog->zl_lock);
12351807Sbonwick 	for (;;) {
12361807Sbonwick 		/*
12371807Sbonwick 		 * Wait for any in-flight log writes to complete.
12381807Sbonwick 		 */
12391807Sbonwick 		for (lwb = list_head(&zilog->zl_lwb_list); lwb != NULL;
12401807Sbonwick 		    lwb = list_next(&zilog->zl_lwb_list, lwb))
12411807Sbonwick 			if (lwb->lwb_seq != 0 && lwb->lwb_state != SEQ_COMPLETE)
12421807Sbonwick 				break;
12431807Sbonwick 
12441807Sbonwick 		if (lwb == NULL)
12451807Sbonwick 			break;
12461807Sbonwick 
12471807Sbonwick 		cv_wait(&zilog->zl_cv_seq, &zilog->zl_lock);
1248789Sahrens 	}
12491807Sbonwick 
1250789Sahrens 	mutex_exit(&zilog->zl_lock);
1251789Sahrens 
12521807Sbonwick 	zil_destroy(zilog, B_FALSE);
12531807Sbonwick 
12541807Sbonwick 	mutex_enter(&zilog->zl_lock);
12551807Sbonwick 	ASSERT(BP_IS_HOLE(&zh->zh_log));
12561807Sbonwick 	zilog->zl_suspending = B_FALSE;
12571807Sbonwick 	cv_broadcast(&zilog->zl_cv_suspend);
12581807Sbonwick 	mutex_exit(&zilog->zl_lock);
1259789Sahrens 
1260789Sahrens 	return (0);
1261789Sahrens }
1262789Sahrens 
1263789Sahrens void
1264789Sahrens zil_resume(zilog_t *zilog)
1265789Sahrens {
1266789Sahrens 	mutex_enter(&zilog->zl_lock);
1267789Sahrens 	ASSERT(zilog->zl_suspend != 0);
1268789Sahrens 	zilog->zl_suspend--;
1269789Sahrens 	mutex_exit(&zilog->zl_lock);
1270789Sahrens }
1271789Sahrens 
1272789Sahrens typedef struct zil_replay_arg {
1273789Sahrens 	objset_t	*zr_os;
1274789Sahrens 	zil_replay_func_t **zr_replay;
1275789Sahrens 	void		*zr_arg;
1276789Sahrens 	void		(*zr_rm_sync)(void *arg);
1277789Sahrens 	uint64_t	*zr_txgp;
1278789Sahrens 	boolean_t	zr_byteswap;
1279789Sahrens 	char		*zr_lrbuf;
1280789Sahrens } zil_replay_arg_t;
1281789Sahrens 
1282789Sahrens static void
1283789Sahrens zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg)
1284789Sahrens {
1285789Sahrens 	zil_replay_arg_t *zr = zra;
12861807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
1287789Sahrens 	uint64_t reclen = lr->lrc_reclen;
1288789Sahrens 	uint64_t txtype = lr->lrc_txtype;
1289789Sahrens 	int pass, error;
1290789Sahrens 
1291789Sahrens 	if (zilog->zl_stop_replay)
1292789Sahrens 		return;
1293789Sahrens 
1294789Sahrens 	if (lr->lrc_txg < claim_txg)		/* already committed */
1295789Sahrens 		return;
1296789Sahrens 
1297789Sahrens 	if (lr->lrc_seq <= zh->zh_replay_seq)	/* already replayed */
1298789Sahrens 		return;
1299789Sahrens 
1300789Sahrens 	/*
1301789Sahrens 	 * Make a copy of the data so we can revise and extend it.
1302789Sahrens 	 */
1303789Sahrens 	bcopy(lr, zr->zr_lrbuf, reclen);
1304789Sahrens 
1305789Sahrens 	/*
1306789Sahrens 	 * The log block containing this lr may have been byteswapped
1307789Sahrens 	 * so that we can easily examine common fields like lrc_txtype.
1308789Sahrens 	 * However, the log is a mix of different data types, and only the
1309789Sahrens 	 * replay vectors know how to byteswap their records.  Therefore, if
1310789Sahrens 	 * the lr was byteswapped, undo it before invoking the replay vector.
1311789Sahrens 	 */
1312789Sahrens 	if (zr->zr_byteswap)
1313789Sahrens 		byteswap_uint64_array(zr->zr_lrbuf, reclen);
1314789Sahrens 
1315789Sahrens 	/*
1316789Sahrens 	 * If this is a TX_WRITE with a blkptr, suck in the data.
1317789Sahrens 	 */
1318789Sahrens 	if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) {
1319789Sahrens 		lr_write_t *lrw = (lr_write_t *)lr;
1320789Sahrens 		blkptr_t *wbp = &lrw->lr_blkptr;
1321789Sahrens 		uint64_t wlen = lrw->lr_length;
1322789Sahrens 		char *wbuf = zr->zr_lrbuf + reclen;
1323789Sahrens 
1324789Sahrens 		if (BP_IS_HOLE(wbp)) {	/* compressed to a hole */
1325789Sahrens 			bzero(wbuf, wlen);
1326789Sahrens 		} else {
1327789Sahrens 			/*
1328789Sahrens 			 * A subsequent write may have overwritten this block,
1329789Sahrens 			 * in which case wbp may have been been freed and
1330789Sahrens 			 * reallocated, and our read of wbp may fail with a
1331789Sahrens 			 * checksum error.  We can safely ignore this because
1332789Sahrens 			 * the later write will provide the correct data.
1333789Sahrens 			 */
13341544Seschrock 			zbookmark_t zb;
13351544Seschrock 
13361544Seschrock 			zb.zb_objset = dmu_objset_id(zilog->zl_os);
13371544Seschrock 			zb.zb_object = lrw->lr_foid;
13381544Seschrock 			zb.zb_level = -1;
13391544Seschrock 			zb.zb_blkid = lrw->lr_offset / BP_GET_LSIZE(wbp);
13401544Seschrock 
1341789Sahrens 			(void) zio_wait(zio_read(NULL, zilog->zl_spa,
1342789Sahrens 			    wbp, wbuf, BP_GET_LSIZE(wbp), NULL, NULL,
1343789Sahrens 			    ZIO_PRIORITY_SYNC_READ,
13441544Seschrock 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &zb));
1345789Sahrens 			(void) memmove(wbuf, wbuf + lrw->lr_blkoff, wlen);
1346789Sahrens 		}
1347789Sahrens 	}
1348789Sahrens 
1349789Sahrens 	/*
1350789Sahrens 	 * We must now do two things atomically: replay this log record,
1351789Sahrens 	 * and update the log header to reflect the fact that we did so.
1352789Sahrens 	 * We use the DMU's ability to assign into a specific txg to do this.
1353789Sahrens 	 */
1354789Sahrens 	for (pass = 1; /* CONSTANTCONDITION */; pass++) {
1355789Sahrens 		uint64_t replay_txg;
1356789Sahrens 		dmu_tx_t *replay_tx;
1357789Sahrens 
1358789Sahrens 		replay_tx = dmu_tx_create(zr->zr_os);
1359789Sahrens 		error = dmu_tx_assign(replay_tx, TXG_WAIT);
1360789Sahrens 		if (error) {
1361789Sahrens 			dmu_tx_abort(replay_tx);
1362789Sahrens 			break;
1363789Sahrens 		}
1364789Sahrens 
1365789Sahrens 		replay_txg = dmu_tx_get_txg(replay_tx);
1366789Sahrens 
1367789Sahrens 		if (txtype == 0 || txtype >= TX_MAX_TYPE) {
1368789Sahrens 			error = EINVAL;
1369789Sahrens 		} else {
1370789Sahrens 			/*
1371789Sahrens 			 * On the first pass, arrange for the replay vector
1372789Sahrens 			 * to fail its dmu_tx_assign().  That's the only way
1373789Sahrens 			 * to ensure that those code paths remain well tested.
1374789Sahrens 			 */
1375789Sahrens 			*zr->zr_txgp = replay_txg - (pass == 1);
1376789Sahrens 			error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lrbuf,
1377789Sahrens 			    zr->zr_byteswap);
1378789Sahrens 			*zr->zr_txgp = TXG_NOWAIT;
1379789Sahrens 		}
1380789Sahrens 
1381789Sahrens 		if (error == 0) {
1382789Sahrens 			dsl_dataset_dirty(dmu_objset_ds(zr->zr_os), replay_tx);
1383789Sahrens 			zilog->zl_replay_seq[replay_txg & TXG_MASK] =
1384789Sahrens 			    lr->lrc_seq;
1385789Sahrens 		}
1386789Sahrens 
1387789Sahrens 		dmu_tx_commit(replay_tx);
1388789Sahrens 
1389789Sahrens 		if (error != ERESTART)
1390789Sahrens 			break;
1391789Sahrens 
1392789Sahrens 		if (pass != 1)
1393789Sahrens 			txg_wait_open(spa_get_dsl(zilog->zl_spa),
1394789Sahrens 			    replay_txg + 1);
1395789Sahrens 
1396789Sahrens 		dprintf("pass %d, retrying\n", pass);
1397789Sahrens 	}
1398789Sahrens 
1399789Sahrens 	if (error) {
1400789Sahrens 		char *name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1401789Sahrens 		dmu_objset_name(zr->zr_os, name);
1402789Sahrens 		cmn_err(CE_WARN, "ZFS replay transaction error %d, "
1403789Sahrens 		    "dataset %s, seq 0x%llx, txtype %llu\n",
1404789Sahrens 		    error, name,
1405789Sahrens 		    (u_longlong_t)lr->lrc_seq, (u_longlong_t)txtype);
1406789Sahrens 		zilog->zl_stop_replay = 1;
1407789Sahrens 		kmem_free(name, MAXNAMELEN);
1408789Sahrens 	}
1409789Sahrens 
1410789Sahrens 	/*
1411789Sahrens 	 * The DMU's dnode layer doesn't see removes until the txg commits,
1412789Sahrens 	 * so a subsequent claim can spuriously fail with EEXIST.
1413789Sahrens 	 * To prevent this, if we might have removed an object,
1414789Sahrens 	 * wait for the delete thread to delete it, and then
1415789Sahrens 	 * wait for the transaction group to sync.
1416789Sahrens 	 */
1417789Sahrens 	if (txtype == TX_REMOVE || txtype == TX_RMDIR || txtype == TX_RENAME) {
1418789Sahrens 		if (zr->zr_rm_sync != NULL)
1419789Sahrens 			zr->zr_rm_sync(zr->zr_arg);
1420789Sahrens 		txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0);
1421789Sahrens 	}
1422789Sahrens }
1423789Sahrens 
1424789Sahrens /*
14251362Sperrin  * If this dataset has a non-empty intent log, replay it and destroy it.
1426789Sahrens  */
1427789Sahrens void
1428789Sahrens zil_replay(objset_t *os, void *arg, uint64_t *txgp,
1429789Sahrens 	zil_replay_func_t *replay_func[TX_MAX_TYPE], void (*rm_sync)(void *arg))
1430789Sahrens {
1431789Sahrens 	zilog_t *zilog = dmu_objset_zil(os);
14321807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
14331807Sbonwick 	zil_replay_arg_t zr;
14341362Sperrin 
14351362Sperrin 	if (zil_empty(zilog)) {
14361807Sbonwick 		zil_destroy(zilog, B_TRUE);
14371362Sperrin 		return;
14381362Sperrin 	}
1439789Sahrens 
1440789Sahrens 	zr.zr_os = os;
1441789Sahrens 	zr.zr_replay = replay_func;
1442789Sahrens 	zr.zr_arg = arg;
1443789Sahrens 	zr.zr_rm_sync = rm_sync;
1444789Sahrens 	zr.zr_txgp = txgp;
14451807Sbonwick 	zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log);
1446789Sahrens 	zr.zr_lrbuf = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP);
1447789Sahrens 
1448789Sahrens 	/*
1449789Sahrens 	 * Wait for in-progress removes to sync before starting replay.
1450789Sahrens 	 */
1451789Sahrens 	if (rm_sync != NULL)
1452789Sahrens 		rm_sync(arg);
1453789Sahrens 	txg_wait_synced(zilog->zl_dmu_pool, 0);
1454789Sahrens 
1455789Sahrens 	zilog->zl_stop_replay = 0;
14561807Sbonwick 	(void) zil_parse(zilog, NULL, zil_replay_log_record, &zr,
14571807Sbonwick 	    zh->zh_claim_txg);
1458789Sahrens 	kmem_free(zr.zr_lrbuf, 2 * SPA_MAXBLOCKSIZE);
1459789Sahrens 
14601807Sbonwick 	zil_destroy(zilog, B_FALSE);
1461789Sahrens }
14621646Sperrin 
14631646Sperrin /*
14641646Sperrin  * Report whether all transactions are committed
14651646Sperrin  */
14661646Sperrin int
14671646Sperrin zil_is_committed(zilog_t *zilog)
14681646Sperrin {
14691646Sperrin 	lwb_t *lwb;
14701646Sperrin 
14711807Sbonwick 	if (!list_is_empty(&zilog->zl_itx_list))
14721646Sperrin 		return (B_FALSE);
14731646Sperrin 
14741646Sperrin 	/*
14751646Sperrin 	 * A log write buffer at the head of the list that is not UNWRITTEN
14761646Sperrin 	 * means there's a lwb yet to be freed after a txg commit
14771646Sperrin 	 */
14781646Sperrin 	lwb = list_head(&zilog->zl_lwb_list);
14791646Sperrin 	if (lwb && lwb->lwb_state != UNWRITTEN)
14801646Sperrin 		return (B_FALSE);
14811646Sperrin 	ASSERT(zil_empty(zilog));
14821646Sperrin 	return (B_TRUE);
14831646Sperrin }
1484