xref: /onnv-gate/usr/src/uts/common/fs/zfs/zil.c (revision 10800:469478b180d9)
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 /*
228746SMatthew.Ahrens@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens #include <sys/zfs_context.h>
27789Sahrens #include <sys/spa.h>
289701SGeorge.Wilson@Sun.COM #include <sys/spa_impl.h>
29789Sahrens #include <sys/dmu.h>
30789Sahrens #include <sys/zap.h>
31789Sahrens #include <sys/arc.h>
32789Sahrens #include <sys/stat.h>
33789Sahrens #include <sys/resource.h>
34789Sahrens #include <sys/zil.h>
35789Sahrens #include <sys/zil_impl.h>
36789Sahrens #include <sys/dsl_dataset.h>
37789Sahrens #include <sys/vdev.h>
383668Sgw25295 #include <sys/dmu_tx.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 /*
672986Sek110237  * This global ZIL switch affects all pools
68789Sahrens  */
69789Sahrens int zil_disable = 0;	/* disable intent logging */
702986Sek110237 
712986Sek110237 /*
722986Sek110237  * Tunable parameter for debugging or performance analysis.  Setting
732986Sek110237  * zfs_nocacheflush will cause corruption on power loss if a volatile
742986Sek110237  * out-of-order write cache is enabled.
752986Sek110237  */
762986Sek110237 boolean_t zfs_nocacheflush = B_FALSE;
77789Sahrens 
78789Sahrens static kmem_cache_t *zil_lwb_cache;
79789Sahrens 
8010685SGeorge.Wilson@Sun.COM static boolean_t zil_empty(zilog_t *zilog);
8110685SGeorge.Wilson@Sun.COM 
82789Sahrens static int
83789Sahrens zil_dva_compare(const void *x1, const void *x2)
84789Sahrens {
85789Sahrens 	const dva_t *dva1 = x1;
86789Sahrens 	const dva_t *dva2 = x2;
87789Sahrens 
88789Sahrens 	if (DVA_GET_VDEV(dva1) < DVA_GET_VDEV(dva2))
89789Sahrens 		return (-1);
90789Sahrens 	if (DVA_GET_VDEV(dva1) > DVA_GET_VDEV(dva2))
91789Sahrens 		return (1);
92789Sahrens 
93789Sahrens 	if (DVA_GET_OFFSET(dva1) < DVA_GET_OFFSET(dva2))
94789Sahrens 		return (-1);
95789Sahrens 	if (DVA_GET_OFFSET(dva1) > DVA_GET_OFFSET(dva2))
96789Sahrens 		return (1);
97789Sahrens 
98789Sahrens 	return (0);
99789Sahrens }
100789Sahrens 
101789Sahrens static void
102789Sahrens zil_dva_tree_init(avl_tree_t *t)
103789Sahrens {
104789Sahrens 	avl_create(t, zil_dva_compare, sizeof (zil_dva_node_t),
105789Sahrens 	    offsetof(zil_dva_node_t, zn_node));
106789Sahrens }
107789Sahrens 
108789Sahrens static void
109789Sahrens zil_dva_tree_fini(avl_tree_t *t)
110789Sahrens {
111789Sahrens 	zil_dva_node_t *zn;
112789Sahrens 	void *cookie = NULL;
113789Sahrens 
114789Sahrens 	while ((zn = avl_destroy_nodes(t, &cookie)) != NULL)
115789Sahrens 		kmem_free(zn, sizeof (zil_dva_node_t));
116789Sahrens 
117789Sahrens 	avl_destroy(t);
118789Sahrens }
119789Sahrens 
120789Sahrens static int
121789Sahrens zil_dva_tree_add(avl_tree_t *t, dva_t *dva)
122789Sahrens {
123789Sahrens 	zil_dva_node_t *zn;
124789Sahrens 	avl_index_t where;
125789Sahrens 
126789Sahrens 	if (avl_find(t, dva, &where) != NULL)
127789Sahrens 		return (EEXIST);
128789Sahrens 
129789Sahrens 	zn = kmem_alloc(sizeof (zil_dva_node_t), KM_SLEEP);
130789Sahrens 	zn->zn_dva = *dva;
131789Sahrens 	avl_insert(t, zn, where);
132789Sahrens 
133789Sahrens 	return (0);
134789Sahrens }
135789Sahrens 
1361807Sbonwick static zil_header_t *
1371807Sbonwick zil_header_in_syncing_context(zilog_t *zilog)
1381807Sbonwick {
1391807Sbonwick 	return ((zil_header_t *)zilog->zl_header);
1401807Sbonwick }
1411807Sbonwick 
1421807Sbonwick static void
1431807Sbonwick zil_init_log_chain(zilog_t *zilog, blkptr_t *bp)
1441807Sbonwick {
1451807Sbonwick 	zio_cksum_t *zc = &bp->blk_cksum;
1461807Sbonwick 
1471807Sbonwick 	zc->zc_word[ZIL_ZC_GUID_0] = spa_get_random(-1ULL);
1481807Sbonwick 	zc->zc_word[ZIL_ZC_GUID_1] = spa_get_random(-1ULL);
1491807Sbonwick 	zc->zc_word[ZIL_ZC_OBJSET] = dmu_objset_id(zilog->zl_os);
1501807Sbonwick 	zc->zc_word[ZIL_ZC_SEQ] = 1ULL;
1511807Sbonwick }
1521807Sbonwick 
153789Sahrens /*
154789Sahrens  * Read a log block, make sure it's valid, and byteswap it if necessary.
155789Sahrens  */
156789Sahrens static int
1571807Sbonwick zil_read_log_block(zilog_t *zilog, const blkptr_t *bp, arc_buf_t **abufpp)
158789Sahrens {
1591807Sbonwick 	blkptr_t blk = *bp;
1601544Seschrock 	zbookmark_t zb;
1612391Smaybee 	uint32_t aflags = ARC_WAIT;
162789Sahrens 	int error;
163789Sahrens 
1641807Sbonwick 	zb.zb_objset = bp->blk_cksum.zc_word[ZIL_ZC_OBJSET];
1651544Seschrock 	zb.zb_object = 0;
1661544Seschrock 	zb.zb_level = -1;
1671807Sbonwick 	zb.zb_blkid = bp->blk_cksum.zc_word[ZIL_ZC_SEQ];
1681807Sbonwick 
1691807Sbonwick 	*abufpp = NULL;
1701807Sbonwick 
1717046Sahrens 	/*
1727046Sahrens 	 * We shouldn't be doing any scrubbing while we're doing log
1737046Sahrens 	 * replay, it's OK to not lock.
1747046Sahrens 	 */
1757046Sahrens 	error = arc_read_nolock(NULL, zilog->zl_spa, &blk,
1761807Sbonwick 	    arc_getbuf_func, abufpp, ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL |
1772391Smaybee 	    ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB, &aflags, &zb);
1781807Sbonwick 
1791807Sbonwick 	if (error == 0) {
1801807Sbonwick 		char *data = (*abufpp)->b_data;
1811807Sbonwick 		uint64_t blksz = BP_GET_LSIZE(bp);
1821807Sbonwick 		zil_trailer_t *ztp = (zil_trailer_t *)(data + blksz) - 1;
1831807Sbonwick 		zio_cksum_t cksum = bp->blk_cksum;
1841544Seschrock 
1851807Sbonwick 		/*
1867522SNeil.Perrin@Sun.COM 		 * Validate the checksummed log block.
1877522SNeil.Perrin@Sun.COM 		 *
1881807Sbonwick 		 * Sequence numbers should be... sequential.  The checksum
1891807Sbonwick 		 * verifier for the next block should be bp's checksum plus 1.
1907522SNeil.Perrin@Sun.COM 		 *
1917522SNeil.Perrin@Sun.COM 		 * Also check the log chain linkage and size used.
1921807Sbonwick 		 */
1931807Sbonwick 		cksum.zc_word[ZIL_ZC_SEQ]++;
1941807Sbonwick 
1957522SNeil.Perrin@Sun.COM 		if (bcmp(&cksum, &ztp->zit_next_blk.blk_cksum,
1967522SNeil.Perrin@Sun.COM 		    sizeof (cksum)) || BP_IS_HOLE(&ztp->zit_next_blk) ||
1977522SNeil.Perrin@Sun.COM 		    (ztp->zit_nused > (blksz - sizeof (zil_trailer_t)))) {
1987522SNeil.Perrin@Sun.COM 			error = ECKSUM;
1997522SNeil.Perrin@Sun.COM 		}
2001807Sbonwick 
2011807Sbonwick 		if (error) {
2021807Sbonwick 			VERIFY(arc_buf_remove_ref(*abufpp, abufpp) == 1);
2031807Sbonwick 			*abufpp = NULL;
2041807Sbonwick 		}
205789Sahrens 	}
206789Sahrens 
2071807Sbonwick 	dprintf("error %d on %llu:%llu\n", error, zb.zb_objset, zb.zb_blkid);
208789Sahrens 
2091807Sbonwick 	return (error);
210789Sahrens }
211789Sahrens 
212789Sahrens /*
213789Sahrens  * Parse the intent log, and call parse_func for each valid record within.
2141807Sbonwick  * Return the highest sequence number.
215789Sahrens  */
2161807Sbonwick uint64_t
217789Sahrens zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func,
218789Sahrens     zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg)
219789Sahrens {
2201807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
2211807Sbonwick 	uint64_t claim_seq = zh->zh_claim_seq;
2221807Sbonwick 	uint64_t seq = 0;
2231807Sbonwick 	uint64_t max_seq = 0;
2241807Sbonwick 	blkptr_t blk = zh->zh_log;
2251807Sbonwick 	arc_buf_t *abuf;
226789Sahrens 	char *lrbuf, *lrp;
227789Sahrens 	zil_trailer_t *ztp;
228789Sahrens 	int reclen, error;
229789Sahrens 
230789Sahrens 	if (BP_IS_HOLE(&blk))
2311807Sbonwick 		return (max_seq);
232789Sahrens 
233789Sahrens 	/*
234789Sahrens 	 * Starting at the block pointed to by zh_log we read the log chain.
235789Sahrens 	 * For each block in the chain we strongly check that block to
236789Sahrens 	 * ensure its validity.  We stop when an invalid block is found.
237789Sahrens 	 * For each block pointer in the chain we call parse_blk_func().
238789Sahrens 	 * For each record in each valid block we call parse_lr_func().
2391807Sbonwick 	 * If the log has been claimed, stop if we encounter a sequence
2401807Sbonwick 	 * number greater than the highest claimed sequence number.
241789Sahrens 	 */
242789Sahrens 	zil_dva_tree_init(&zilog->zl_dva_tree);
243789Sahrens 	for (;;) {
2441807Sbonwick 		seq = blk.blk_cksum.zc_word[ZIL_ZC_SEQ];
2451807Sbonwick 
2461807Sbonwick 		if (claim_seq != 0 && seq > claim_seq)
2471807Sbonwick 			break;
2481807Sbonwick 
2491807Sbonwick 		ASSERT(max_seq < seq);
2501807Sbonwick 		max_seq = seq;
2511807Sbonwick 
2521807Sbonwick 		error = zil_read_log_block(zilog, &blk, &abuf);
253789Sahrens 
254789Sahrens 		if (parse_blk_func != NULL)
255789Sahrens 			parse_blk_func(zilog, &blk, arg, txg);
256789Sahrens 
257789Sahrens 		if (error)
258789Sahrens 			break;
259789Sahrens 
2601807Sbonwick 		lrbuf = abuf->b_data;
261789Sahrens 		ztp = (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1;
262789Sahrens 		blk = ztp->zit_next_blk;
263789Sahrens 
2641807Sbonwick 		if (parse_lr_func == NULL) {
2651807Sbonwick 			VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
266789Sahrens 			continue;
2671807Sbonwick 		}
268789Sahrens 
269789Sahrens 		for (lrp = lrbuf; lrp < lrbuf + ztp->zit_nused; lrp += reclen) {
270789Sahrens 			lr_t *lr = (lr_t *)lrp;
271789Sahrens 			reclen = lr->lrc_reclen;
272789Sahrens 			ASSERT3U(reclen, >=, sizeof (lr_t));
273789Sahrens 			parse_lr_func(zilog, lr, arg, txg);
274789Sahrens 		}
2751807Sbonwick 		VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
276789Sahrens 	}
277789Sahrens 	zil_dva_tree_fini(&zilog->zl_dva_tree);
2781807Sbonwick 
2791807Sbonwick 	return (max_seq);
280789Sahrens }
281789Sahrens 
282789Sahrens /* ARGSUSED */
283789Sahrens static void
284789Sahrens zil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg)
285789Sahrens {
286789Sahrens 	spa_t *spa = zilog->zl_spa;
287789Sahrens 	int err;
288789Sahrens 
289789Sahrens 	/*
290789Sahrens 	 * Claim log block if not already committed and not already claimed.
291789Sahrens 	 */
292789Sahrens 	if (bp->blk_birth >= first_txg &&
293789Sahrens 	    zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp)) == 0) {
2947754SJeff.Bonwick@Sun.COM 		err = zio_wait(zio_claim(NULL, spa, first_txg, bp, NULL, NULL,
2957754SJeff.Bonwick@Sun.COM 		    ZIO_FLAG_MUSTSUCCEED));
296789Sahrens 		ASSERT(err == 0);
297789Sahrens 	}
298789Sahrens }
299789Sahrens 
300789Sahrens static void
301789Sahrens zil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg)
302789Sahrens {
303789Sahrens 	if (lrc->lrc_txtype == TX_WRITE) {
304789Sahrens 		lr_write_t *lr = (lr_write_t *)lrc;
305789Sahrens 		zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg);
306789Sahrens 	}
307789Sahrens }
308789Sahrens 
309789Sahrens /* ARGSUSED */
310789Sahrens static void
311789Sahrens zil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg)
312789Sahrens {
313789Sahrens 	zio_free_blk(zilog->zl_spa, bp, dmu_tx_get_txg(tx));
314789Sahrens }
315789Sahrens 
316789Sahrens static void
317789Sahrens zil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg)
318789Sahrens {
319789Sahrens 	/*
320789Sahrens 	 * If we previously claimed it, we need to free it.
321789Sahrens 	 */
322789Sahrens 	if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE) {
323789Sahrens 		lr_write_t *lr = (lr_write_t *)lrc;
324789Sahrens 		blkptr_t *bp = &lr->lr_blkptr;
325789Sahrens 		if (bp->blk_birth >= claim_txg &&
326789Sahrens 		    !zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp))) {
327789Sahrens 			(void) arc_free(NULL, zilog->zl_spa,
328789Sahrens 			    dmu_tx_get_txg(tx), bp, NULL, NULL, ARC_WAIT);
329789Sahrens 		}
330789Sahrens 	}
331789Sahrens }
332789Sahrens 
333789Sahrens /*
334789Sahrens  * Create an on-disk intent log.
335789Sahrens  */
336789Sahrens static void
337789Sahrens zil_create(zilog_t *zilog)
338789Sahrens {
3391807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
340789Sahrens 	lwb_t *lwb;
3411807Sbonwick 	uint64_t txg = 0;
3421807Sbonwick 	dmu_tx_t *tx = NULL;
343789Sahrens 	blkptr_t blk;
3441807Sbonwick 	int error = 0;
345789Sahrens 
346789Sahrens 	/*
3471807Sbonwick 	 * Wait for any previous destroy to complete.
348789Sahrens 	 */
3491807Sbonwick 	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
3501807Sbonwick 
3511807Sbonwick 	ASSERT(zh->zh_claim_txg == 0);
3521807Sbonwick 	ASSERT(zh->zh_replay_seq == 0);
3531807Sbonwick 
3541807Sbonwick 	blk = zh->zh_log;
355789Sahrens 
356789Sahrens 	/*
3578109SNeil.Perrin@Sun.COM 	 * If we don't already have an initial log block or we have one
3588109SNeil.Perrin@Sun.COM 	 * but it's the wrong endianness then allocate one.
359789Sahrens 	 */
3608109SNeil.Perrin@Sun.COM 	if (BP_IS_HOLE(&blk) || BP_SHOULD_BYTESWAP(&blk)) {
3611807Sbonwick 		tx = dmu_tx_create(zilog->zl_os);
3621807Sbonwick 		(void) dmu_tx_assign(tx, TXG_WAIT);
3631807Sbonwick 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
3641807Sbonwick 		txg = dmu_tx_get_txg(tx);
3651807Sbonwick 
3668109SNeil.Perrin@Sun.COM 		if (!BP_IS_HOLE(&blk)) {
3678109SNeil.Perrin@Sun.COM 			zio_free_blk(zilog->zl_spa, &blk, txg);
3688109SNeil.Perrin@Sun.COM 			BP_ZERO(&blk);
3698109SNeil.Perrin@Sun.COM 		}
3708109SNeil.Perrin@Sun.COM 
3713063Sperrin 		error = zio_alloc_blk(zilog->zl_spa, ZIL_MIN_BLKSZ, &blk,
37210310SNeil.Perrin@Sun.COM 		    NULL, txg, zilog->zl_logbias != ZFS_LOGBIAS_LATENCY);
3731807Sbonwick 
3741807Sbonwick 		if (error == 0)
3751807Sbonwick 			zil_init_log_chain(zilog, &blk);
3761362Sperrin 	}
3771807Sbonwick 
3781807Sbonwick 	/*
3791807Sbonwick 	 * Allocate a log write buffer (lwb) for the first log block.
3801807Sbonwick 	 */
381789Sahrens 	if (error == 0) {
382789Sahrens 		lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP);
383789Sahrens 		lwb->lwb_zilog = zilog;
384789Sahrens 		lwb->lwb_blk = blk;
385789Sahrens 		lwb->lwb_nused = 0;
386789Sahrens 		lwb->lwb_sz = BP_GET_LSIZE(&lwb->lwb_blk);
387789Sahrens 		lwb->lwb_buf = zio_buf_alloc(lwb->lwb_sz);
388789Sahrens 		lwb->lwb_max_txg = txg;
3892237Smaybee 		lwb->lwb_zio = NULL;
3902237Smaybee 
391789Sahrens 		mutex_enter(&zilog->zl_lock);
392789Sahrens 		list_insert_tail(&zilog->zl_lwb_list, lwb);
393789Sahrens 		mutex_exit(&zilog->zl_lock);
394789Sahrens 	}
395789Sahrens 
3961807Sbonwick 	/*
3971807Sbonwick 	 * If we just allocated the first log block, commit our transaction
3981807Sbonwick 	 * and wait for zil_sync() to stuff the block poiner into zh_log.
3991807Sbonwick 	 * (zh is part of the MOS, so we cannot modify it in open context.)
4001807Sbonwick 	 */
4011807Sbonwick 	if (tx != NULL) {
4021807Sbonwick 		dmu_tx_commit(tx);
4031362Sperrin 		txg_wait_synced(zilog->zl_dmu_pool, txg);
4041807Sbonwick 	}
4051807Sbonwick 
4061807Sbonwick 	ASSERT(bcmp(&blk, &zh->zh_log, sizeof (blk)) == 0);
407789Sahrens }
408789Sahrens 
409789Sahrens /*
410789Sahrens  * In one tx, free all log blocks and clear the log header.
4111807Sbonwick  * If keep_first is set, then we're replaying a log with no content.
4121807Sbonwick  * We want to keep the first block, however, so that the first
4131807Sbonwick  * synchronous transaction doesn't require a txg_wait_synced()
4141807Sbonwick  * in zil_create().  We don't need to txg_wait_synced() here either
4151807Sbonwick  * when keep_first is set, because both zil_create() and zil_destroy()
4161807Sbonwick  * will wait for any in-progress destroys to complete.
417789Sahrens  */
418789Sahrens void
4191807Sbonwick zil_destroy(zilog_t *zilog, boolean_t keep_first)
420789Sahrens {
4211807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
4221807Sbonwick 	lwb_t *lwb;
423789Sahrens 	dmu_tx_t *tx;
424789Sahrens 	uint64_t txg;
425789Sahrens 
4261807Sbonwick 	/*
4271807Sbonwick 	 * Wait for any previous destroy to complete.
4281807Sbonwick 	 */
4291807Sbonwick 	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
430789Sahrens 
4311807Sbonwick 	if (BP_IS_HOLE(&zh->zh_log))
432789Sahrens 		return;
433789Sahrens 
434789Sahrens 	tx = dmu_tx_create(zilog->zl_os);
435789Sahrens 	(void) dmu_tx_assign(tx, TXG_WAIT);
436789Sahrens 	dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
437789Sahrens 	txg = dmu_tx_get_txg(tx);
438789Sahrens 
4391807Sbonwick 	mutex_enter(&zilog->zl_lock);
4401807Sbonwick 
4411807Sbonwick 	ASSERT3U(zilog->zl_destroy_txg, <, txg);
442789Sahrens 	zilog->zl_destroy_txg = txg;
4431807Sbonwick 
4441807Sbonwick 	if (!list_is_empty(&zilog->zl_lwb_list)) {
4451807Sbonwick 		ASSERT(zh->zh_claim_txg == 0);
44610685SGeorge.Wilson@Sun.COM 		zilog->zl_keep_first = B_FALSE;
4471807Sbonwick 		while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
4481807Sbonwick 			list_remove(&zilog->zl_lwb_list, lwb);
4491807Sbonwick 			if (lwb->lwb_buf != NULL)
4501807Sbonwick 				zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
4511807Sbonwick 			zio_free_blk(zilog->zl_spa, &lwb->lwb_blk, txg);
4521807Sbonwick 			kmem_cache_free(zil_lwb_cache, lwb);
4531807Sbonwick 		}
4541807Sbonwick 	} else {
45510685SGeorge.Wilson@Sun.COM 		zilog->zl_keep_first = keep_first;
45610685SGeorge.Wilson@Sun.COM 		if (zh->zh_flags & ZIL_REPLAY_NEEDED) {
45710685SGeorge.Wilson@Sun.COM 			ASSERT(!keep_first);
4581807Sbonwick 			(void) zil_parse(zilog, zil_free_log_block,
4591807Sbonwick 			    zil_free_log_record, tx, zh->zh_claim_txg);
46010685SGeorge.Wilson@Sun.COM 		} else {
46110685SGeorge.Wilson@Sun.COM 			/*
46210685SGeorge.Wilson@Sun.COM 			 * Would like to assert zil_empty() but that
46310685SGeorge.Wilson@Sun.COM 			 * would force us to read the log chain which
46410685SGeorge.Wilson@Sun.COM 			 * requires us to do I/O to the log. This is
46510685SGeorge.Wilson@Sun.COM 			 * overkill since we really just want to destroy
46610685SGeorge.Wilson@Sun.COM 			 * the chain anyway.
46710685SGeorge.Wilson@Sun.COM 			 */
46810685SGeorge.Wilson@Sun.COM 			if (!keep_first) {
46910685SGeorge.Wilson@Sun.COM 				blkptr_t bp = zh->zh_log;
47010685SGeorge.Wilson@Sun.COM 				zio_free_blk(zilog->zl_spa, &bp, txg);
47110685SGeorge.Wilson@Sun.COM 			}
4721807Sbonwick 		}
4731807Sbonwick 	}
4742638Sperrin 	mutex_exit(&zilog->zl_lock);
475789Sahrens 
476789Sahrens 	dmu_tx_commit(tx);
477789Sahrens }
478789Sahrens 
4798989SNeil.Perrin@Sun.COM /*
4808989SNeil.Perrin@Sun.COM  * return true if the initial log block is not valid
4818989SNeil.Perrin@Sun.COM  */
4828989SNeil.Perrin@Sun.COM static boolean_t
4838989SNeil.Perrin@Sun.COM zil_empty(zilog_t *zilog)
4848989SNeil.Perrin@Sun.COM {
4858989SNeil.Perrin@Sun.COM 	const zil_header_t *zh = zilog->zl_header;
4868989SNeil.Perrin@Sun.COM 	arc_buf_t *abuf = NULL;
4878989SNeil.Perrin@Sun.COM 
4888989SNeil.Perrin@Sun.COM 	if (BP_IS_HOLE(&zh->zh_log))
4898989SNeil.Perrin@Sun.COM 		return (B_TRUE);
4908989SNeil.Perrin@Sun.COM 
4918989SNeil.Perrin@Sun.COM 	if (zil_read_log_block(zilog, &zh->zh_log, &abuf) != 0)
4928989SNeil.Perrin@Sun.COM 		return (B_TRUE);
4938989SNeil.Perrin@Sun.COM 
4948989SNeil.Perrin@Sun.COM 	VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
4958989SNeil.Perrin@Sun.COM 	return (B_FALSE);
4968989SNeil.Perrin@Sun.COM }
4978989SNeil.Perrin@Sun.COM 
4982199Sahrens int
499789Sahrens zil_claim(char *osname, void *txarg)
500789Sahrens {
501789Sahrens 	dmu_tx_t *tx = txarg;
502789Sahrens 	uint64_t first_txg = dmu_tx_get_txg(tx);
503789Sahrens 	zilog_t *zilog;
504789Sahrens 	zil_header_t *zh;
505789Sahrens 	objset_t *os;
506789Sahrens 	int error;
507789Sahrens 
50810298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_hold(osname, FTAG, &os);
509789Sahrens 	if (error) {
5107294Sperrin 		cmn_err(CE_WARN, "can't open objset for %s", osname);
5112199Sahrens 		return (0);
512789Sahrens 	}
513789Sahrens 
514789Sahrens 	zilog = dmu_objset_zil(os);
5151807Sbonwick 	zh = zil_header_in_syncing_context(zilog);
516789Sahrens 
5179701SGeorge.Wilson@Sun.COM 	if (zilog->zl_spa->spa_log_state == SPA_LOG_CLEAR) {
5189701SGeorge.Wilson@Sun.COM 		if (!BP_IS_HOLE(&zh->zh_log))
5199701SGeorge.Wilson@Sun.COM 			zio_free_blk(zilog->zl_spa, &zh->zh_log, first_txg);
5209701SGeorge.Wilson@Sun.COM 		BP_ZERO(&zh->zh_log);
5219701SGeorge.Wilson@Sun.COM 		dsl_dataset_dirty(dmu_objset_ds(os), tx);
5229701SGeorge.Wilson@Sun.COM 	}
5239701SGeorge.Wilson@Sun.COM 
524789Sahrens 	/*
5258989SNeil.Perrin@Sun.COM 	 * Record here whether the zil has any records to replay.
5268989SNeil.Perrin@Sun.COM 	 * If the header block pointer is null or the block points
5278989SNeil.Perrin@Sun.COM 	 * to the stubby then we know there are no valid log records.
5288989SNeil.Perrin@Sun.COM 	 * We use the header to store this state as the the zilog gets
5298989SNeil.Perrin@Sun.COM 	 * freed later in dmu_objset_close().
5308989SNeil.Perrin@Sun.COM 	 * The flags (and the rest of the header fields) are cleared in
5318989SNeil.Perrin@Sun.COM 	 * zil_sync() as a result of a zil_destroy(), after replaying the log.
5328989SNeil.Perrin@Sun.COM 	 *
5338989SNeil.Perrin@Sun.COM 	 * Note, the intent log can be empty but still need the
5348989SNeil.Perrin@Sun.COM 	 * stubby to be claimed.
5358989SNeil.Perrin@Sun.COM 	 */
5369701SGeorge.Wilson@Sun.COM 	if (!zil_empty(zilog)) {
5378989SNeil.Perrin@Sun.COM 		zh->zh_flags |= ZIL_REPLAY_NEEDED;
5389701SGeorge.Wilson@Sun.COM 		dsl_dataset_dirty(dmu_objset_ds(os), tx);
5399701SGeorge.Wilson@Sun.COM 	}
5408989SNeil.Perrin@Sun.COM 
5418989SNeil.Perrin@Sun.COM 	/*
5421807Sbonwick 	 * Claim all log blocks if we haven't already done so, and remember
5431807Sbonwick 	 * the highest claimed sequence number.  This ensures that if we can
5441807Sbonwick 	 * read only part of the log now (e.g. due to a missing device),
5451807Sbonwick 	 * but we can read the entire log later, we will not try to replay
5461807Sbonwick 	 * or destroy beyond the last block we successfully claimed.
547789Sahrens 	 */
548789Sahrens 	ASSERT3U(zh->zh_claim_txg, <=, first_txg);
549789Sahrens 	if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) {
550789Sahrens 		zh->zh_claim_txg = first_txg;
5511807Sbonwick 		zh->zh_claim_seq = zil_parse(zilog, zil_claim_log_block,
5521807Sbonwick 		    zil_claim_log_record, tx, first_txg);
553789Sahrens 		dsl_dataset_dirty(dmu_objset_ds(os), tx);
554789Sahrens 	}
5551807Sbonwick 
556789Sahrens 	ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1));
55710298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
5582199Sahrens 	return (0);
559789Sahrens }
560789Sahrens 
5617294Sperrin /*
5627294Sperrin  * Check the log by walking the log chain.
5637294Sperrin  * Checksum errors are ok as they indicate the end of the chain.
5647294Sperrin  * Any other error (no device or read failure) returns an error.
5657294Sperrin  */
5667294Sperrin /* ARGSUSED */
5677294Sperrin int
5687294Sperrin zil_check_log_chain(char *osname, void *txarg)
5697294Sperrin {
5707294Sperrin 	zilog_t *zilog;
5717294Sperrin 	zil_header_t *zh;
5727294Sperrin 	blkptr_t blk;
5737294Sperrin 	arc_buf_t *abuf;
5747294Sperrin 	objset_t *os;
5757294Sperrin 	char *lrbuf;
5767294Sperrin 	zil_trailer_t *ztp;
5777294Sperrin 	int error;
5787294Sperrin 
57910298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_hold(osname, FTAG, &os);
5807294Sperrin 	if (error) {
5817294Sperrin 		cmn_err(CE_WARN, "can't open objset for %s", osname);
5827294Sperrin 		return (0);
5837294Sperrin 	}
5847294Sperrin 
5857294Sperrin 	zilog = dmu_objset_zil(os);
5867294Sperrin 	zh = zil_header_in_syncing_context(zilog);
5877294Sperrin 	blk = zh->zh_log;
5887294Sperrin 	if (BP_IS_HOLE(&blk)) {
58910298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, FTAG);
5907294Sperrin 		return (0); /* no chain */
5917294Sperrin 	}
5927294Sperrin 
5937294Sperrin 	for (;;) {
5947294Sperrin 		error = zil_read_log_block(zilog, &blk, &abuf);
5957294Sperrin 		if (error)
5967294Sperrin 			break;
5977294Sperrin 		lrbuf = abuf->b_data;
5987294Sperrin 		ztp = (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1;
5997294Sperrin 		blk = ztp->zit_next_blk;
6007294Sperrin 		VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
6017294Sperrin 	}
60210298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
6037294Sperrin 	if (error == ECKSUM)
6047294Sperrin 		return (0); /* normal end of chain */
6057294Sperrin 	return (error);
6067294Sperrin }
6077294Sperrin 
6085688Sbonwick static int
6095688Sbonwick zil_vdev_compare(const void *x1, const void *x2)
610789Sahrens {
6115875Sperrin 	uint64_t v1 = ((zil_vdev_node_t *)x1)->zv_vdev;
6125875Sperrin 	uint64_t v2 = ((zil_vdev_node_t *)x2)->zv_vdev;
6135688Sbonwick 
6145688Sbonwick 	if (v1 < v2)
6155688Sbonwick 		return (-1);
6165688Sbonwick 	if (v1 > v2)
6175688Sbonwick 		return (1);
6185688Sbonwick 
6195688Sbonwick 	return (0);
6205688Sbonwick }
6215688Sbonwick 
6225688Sbonwick void
6235688Sbonwick zil_add_block(zilog_t *zilog, blkptr_t *bp)
6245688Sbonwick {
6255688Sbonwick 	avl_tree_t *t = &zilog->zl_vdev_tree;
6265688Sbonwick 	avl_index_t where;
6275688Sbonwick 	zil_vdev_node_t *zv, zvsearch;
6285688Sbonwick 	int ndvas = BP_GET_NDVAS(bp);
6295688Sbonwick 	int i;
630789Sahrens 
6312986Sek110237 	if (zfs_nocacheflush)
632789Sahrens 		return;
633789Sahrens 
6345688Sbonwick 	ASSERT(zilog->zl_writer);
6355688Sbonwick 
6365688Sbonwick 	/*
6375688Sbonwick 	 * Even though we're zl_writer, we still need a lock because the
6385688Sbonwick 	 * zl_get_data() callbacks may have dmu_sync() done callbacks
6395688Sbonwick 	 * that will run concurrently.
6405688Sbonwick 	 */
6415688Sbonwick 	mutex_enter(&zilog->zl_vdev_lock);
6425688Sbonwick 	for (i = 0; i < ndvas; i++) {
6435688Sbonwick 		zvsearch.zv_vdev = DVA_GET_VDEV(&bp->blk_dva[i]);
6445688Sbonwick 		if (avl_find(t, &zvsearch, &where) == NULL) {
6455688Sbonwick 			zv = kmem_alloc(sizeof (*zv), KM_SLEEP);
6465688Sbonwick 			zv->zv_vdev = zvsearch.zv_vdev;
6475688Sbonwick 			avl_insert(t, zv, where);
6483063Sperrin 		}
6493063Sperrin 	}
6505688Sbonwick 	mutex_exit(&zilog->zl_vdev_lock);
6513063Sperrin }
6523063Sperrin 
653789Sahrens void
6542638Sperrin zil_flush_vdevs(zilog_t *zilog)
655789Sahrens {
6563063Sperrin 	spa_t *spa = zilog->zl_spa;
6575688Sbonwick 	avl_tree_t *t = &zilog->zl_vdev_tree;
6585688Sbonwick 	void *cookie = NULL;
6595688Sbonwick 	zil_vdev_node_t *zv;
6605688Sbonwick 	zio_t *zio;
6613063Sperrin 
6623063Sperrin 	ASSERT(zilog->zl_writer);
663789Sahrens 
6645688Sbonwick 	/*
6655688Sbonwick 	 * We don't need zl_vdev_lock here because we're the zl_writer,
6665688Sbonwick 	 * and all zl_get_data() callbacks are done.
6675688Sbonwick 	 */
6685688Sbonwick 	if (avl_numnodes(t) == 0)
6695688Sbonwick 		return;
6705688Sbonwick 
6717754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6725688Sbonwick 
6737754SJeff.Bonwick@Sun.COM 	zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
6745688Sbonwick 
6755688Sbonwick 	while ((zv = avl_destroy_nodes(t, &cookie)) != NULL) {
6765688Sbonwick 		vdev_t *vd = vdev_lookup_top(spa, zv->zv_vdev);
6775688Sbonwick 		if (vd != NULL)
6785688Sbonwick 			zio_flush(zio, vd);
6795688Sbonwick 		kmem_free(zv, sizeof (*zv));
6803063Sperrin 	}
681789Sahrens 
682789Sahrens 	/*
683789Sahrens 	 * Wait for all the flushes to complete.  Not all devices actually
684789Sahrens 	 * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails.
685789Sahrens 	 */
6865688Sbonwick 	(void) zio_wait(zio);
6875688Sbonwick 
6887754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, FTAG);
689789Sahrens }
690789Sahrens 
691789Sahrens /*
692789Sahrens  * Function called when a log block write completes
693789Sahrens  */
694789Sahrens static void
695789Sahrens zil_lwb_write_done(zio_t *zio)
696789Sahrens {
697789Sahrens 	lwb_t *lwb = zio->io_private;
698789Sahrens 	zilog_t *zilog = lwb->lwb_zilog;
699789Sahrens 
7007754SJeff.Bonwick@Sun.COM 	ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF);
7017754SJeff.Bonwick@Sun.COM 	ASSERT(BP_GET_CHECKSUM(zio->io_bp) == ZIO_CHECKSUM_ZILOG);
7027754SJeff.Bonwick@Sun.COM 	ASSERT(BP_GET_TYPE(zio->io_bp) == DMU_OT_INTENT_LOG);
7037754SJeff.Bonwick@Sun.COM 	ASSERT(BP_GET_LEVEL(zio->io_bp) == 0);
7047754SJeff.Bonwick@Sun.COM 	ASSERT(BP_GET_BYTEORDER(zio->io_bp) == ZFS_HOST_BYTEORDER);
7057754SJeff.Bonwick@Sun.COM 	ASSERT(!BP_IS_GANG(zio->io_bp));
7067754SJeff.Bonwick@Sun.COM 	ASSERT(!BP_IS_HOLE(zio->io_bp));
7077754SJeff.Bonwick@Sun.COM 	ASSERT(zio->io_bp->blk_fill == 0);
7087754SJeff.Bonwick@Sun.COM 
709789Sahrens 	/*
7109493SNeil.Perrin@Sun.COM 	 * Ensure the lwb buffer pointer is cleared before releasing
7119493SNeil.Perrin@Sun.COM 	 * the txg. If we have had an allocation failure and
7129493SNeil.Perrin@Sun.COM 	 * the txg is waiting to sync then we want want zil_sync()
7139493SNeil.Perrin@Sun.COM 	 * to remove the lwb so that it's not picked up as the next new
7149493SNeil.Perrin@Sun.COM 	 * one in zil_commit_writer(). zil_sync() will only remove
7159493SNeil.Perrin@Sun.COM 	 * the lwb if lwb_buf is null.
716789Sahrens 	 */
717789Sahrens 	zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
718789Sahrens 	mutex_enter(&zilog->zl_lock);
719789Sahrens 	lwb->lwb_buf = NULL;
7204527Sperrin 	if (zio->io_error)
721789Sahrens 		zilog->zl_log_error = B_TRUE;
7229493SNeil.Perrin@Sun.COM 
7239493SNeil.Perrin@Sun.COM 	/*
7249493SNeil.Perrin@Sun.COM 	 * Now that we've written this log block, we have a stable pointer
7259493SNeil.Perrin@Sun.COM 	 * to the next block in the chain, so it's OK to let the txg in
7269904SNeil.Perrin@Sun.COM 	 * which we allocated the next block sync. We still have the
7279904SNeil.Perrin@Sun.COM 	 * zl_lock to ensure zil_sync doesn't kmem free the lwb.
7289493SNeil.Perrin@Sun.COM 	 */
7299493SNeil.Perrin@Sun.COM 	txg_rele_to_sync(&lwb->lwb_txgh);
7309904SNeil.Perrin@Sun.COM 	mutex_exit(&zilog->zl_lock);
731789Sahrens }
732789Sahrens 
733789Sahrens /*
7342237Smaybee  * Initialize the io for a log block.
7352237Smaybee  */
7362237Smaybee static void
7372237Smaybee zil_lwb_write_init(zilog_t *zilog, lwb_t *lwb)
7382237Smaybee {
7392237Smaybee 	zbookmark_t zb;
7402237Smaybee 
7412237Smaybee 	zb.zb_objset = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET];
7422237Smaybee 	zb.zb_object = 0;
7432237Smaybee 	zb.zb_level = -1;
7442237Smaybee 	zb.zb_blkid = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ];
7452237Smaybee 
7462638Sperrin 	if (zilog->zl_root_zio == NULL) {
7472638Sperrin 		zilog->zl_root_zio = zio_root(zilog->zl_spa, NULL, NULL,
7482638Sperrin 		    ZIO_FLAG_CANFAIL);
7492638Sperrin 	}
7503063Sperrin 	if (lwb->lwb_zio == NULL) {
7513063Sperrin 		lwb->lwb_zio = zio_rewrite(zilog->zl_root_zio, zilog->zl_spa,
7529701SGeorge.Wilson@Sun.COM 		    0, &lwb->lwb_blk, lwb->lwb_buf, lwb->lwb_sz,
7539701SGeorge.Wilson@Sun.COM 		    zil_lwb_write_done, lwb, ZIO_PRIORITY_LOG_WRITE,
75410685SGeorge.Wilson@Sun.COM 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE, &zb);
7553063Sperrin 	}
7562237Smaybee }
7572237Smaybee 
7582237Smaybee /*
759789Sahrens  * Start a log block write and advance to the next log block.
760789Sahrens  * Calls are serialized.
761789Sahrens  */
762789Sahrens static lwb_t *
763789Sahrens zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb)
764789Sahrens {
765789Sahrens 	lwb_t *nlwb;
766789Sahrens 	zil_trailer_t *ztp = (zil_trailer_t *)(lwb->lwb_buf + lwb->lwb_sz) - 1;
7671807Sbonwick 	spa_t *spa = zilog->zl_spa;
7681807Sbonwick 	blkptr_t *bp = &ztp->zit_next_blk;
769789Sahrens 	uint64_t txg;
770789Sahrens 	uint64_t zil_blksz;
771789Sahrens 	int error;
772789Sahrens 
773789Sahrens 	ASSERT(lwb->lwb_nused <= ZIL_BLK_DATA_SZ(lwb));
774789Sahrens 
775789Sahrens 	/*
776789Sahrens 	 * Allocate the next block and save its address in this block
777789Sahrens 	 * before writing it in order to establish the log chain.
778789Sahrens 	 * Note that if the allocation of nlwb synced before we wrote
779789Sahrens 	 * the block that points at it (lwb), we'd leak it if we crashed.
780789Sahrens 	 * Therefore, we don't do txg_rele_to_sync() until zil_lwb_write_done().
781789Sahrens 	 */
782789Sahrens 	txg = txg_hold_open(zilog->zl_dmu_pool, &lwb->lwb_txgh);
783789Sahrens 	txg_rele_to_quiesce(&lwb->lwb_txgh);
784789Sahrens 
785789Sahrens 	/*
7861141Sperrin 	 * Pick a ZIL blocksize. We request a size that is the
7871141Sperrin 	 * maximum of the previous used size, the current used size and
7881141Sperrin 	 * the amount waiting in the queue.
789789Sahrens 	 */
7902237Smaybee 	zil_blksz = MAX(zilog->zl_prev_used,
7912237Smaybee 	    zilog->zl_cur_used + sizeof (*ztp));
7921141Sperrin 	zil_blksz = MAX(zil_blksz, zilog->zl_itx_list_sz + sizeof (*ztp));
7931842Sperrin 	zil_blksz = P2ROUNDUP_TYPED(zil_blksz, ZIL_MIN_BLKSZ, uint64_t);
7941141Sperrin 	if (zil_blksz > ZIL_MAX_BLKSZ)
7951141Sperrin 		zil_blksz = ZIL_MAX_BLKSZ;
796789Sahrens 
7973063Sperrin 	BP_ZERO(bp);
7983063Sperrin 	/* pass the old blkptr in order to spread log blocks across devs */
79910310SNeil.Perrin@Sun.COM 	error = zio_alloc_blk(spa, zil_blksz, bp, &lwb->lwb_blk, txg,
80010310SNeil.Perrin@Sun.COM 	    zilog->zl_logbias != ZFS_LOGBIAS_LATENCY);
801789Sahrens 	if (error) {
8023668Sgw25295 		dmu_tx_t *tx = dmu_tx_create_assigned(zilog->zl_dmu_pool, txg);
8033668Sgw25295 
8041544Seschrock 		/*
8053668Sgw25295 		 * We dirty the dataset to ensure that zil_sync() will
8063668Sgw25295 		 * be called to remove this lwb from our zl_lwb_list.
8073668Sgw25295 		 * Failing to do so, may leave an lwb with a NULL lwb_buf
8083668Sgw25295 		 * hanging around on the zl_lwb_list.
8093668Sgw25295 		 */
8103668Sgw25295 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
8113848Sgw25295 		dmu_tx_commit(tx);
8123668Sgw25295 
8133668Sgw25295 		/*
8143668Sgw25295 		 * Since we've just experienced an allocation failure so we
8153668Sgw25295 		 * terminate the current lwb and send it on its way.
8163668Sgw25295 		 */
8173668Sgw25295 		ztp->zit_pad = 0;
8183668Sgw25295 		ztp->zit_nused = lwb->lwb_nused;
8193668Sgw25295 		ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum;
8203668Sgw25295 		zio_nowait(lwb->lwb_zio);
8213668Sgw25295 
8223668Sgw25295 		/*
8231544Seschrock 		 * By returning NULL the caller will call tx_wait_synced()
8241544Seschrock 		 */
825789Sahrens 		return (NULL);
826789Sahrens 	}
827789Sahrens 
8281807Sbonwick 	ASSERT3U(bp->blk_birth, ==, txg);
8291544Seschrock 	ztp->zit_pad = 0;
830789Sahrens 	ztp->zit_nused = lwb->lwb_nused;
831789Sahrens 	ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum;
8321807Sbonwick 	bp->blk_cksum = lwb->lwb_blk.blk_cksum;
8331807Sbonwick 	bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++;
834789Sahrens 
835789Sahrens 	/*
836789Sahrens 	 * Allocate a new log write buffer (lwb).
837789Sahrens 	 */
838789Sahrens 	nlwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP);
839789Sahrens 
840789Sahrens 	nlwb->lwb_zilog = zilog;
8411807Sbonwick 	nlwb->lwb_blk = *bp;
842789Sahrens 	nlwb->lwb_nused = 0;
843789Sahrens 	nlwb->lwb_sz = BP_GET_LSIZE(&nlwb->lwb_blk);
844789Sahrens 	nlwb->lwb_buf = zio_buf_alloc(nlwb->lwb_sz);
845789Sahrens 	nlwb->lwb_max_txg = txg;
8462237Smaybee 	nlwb->lwb_zio = NULL;
847789Sahrens 
848789Sahrens 	/*
8493063Sperrin 	 * Put new lwb at the end of the log chain
850789Sahrens 	 */
851789Sahrens 	mutex_enter(&zilog->zl_lock);
852789Sahrens 	list_insert_tail(&zilog->zl_lwb_list, nlwb);
8533063Sperrin 	mutex_exit(&zilog->zl_lock);
8543063Sperrin 
8555688Sbonwick 	/* Record the block for later vdev flushing */
8565688Sbonwick 	zil_add_block(zilog, &lwb->lwb_blk);
857789Sahrens 
858789Sahrens 	/*
8592237Smaybee 	 * kick off the write for the old log block
860789Sahrens 	 */
8612237Smaybee 	dprintf_bp(&lwb->lwb_blk, "lwb %p txg %llu: ", lwb, txg);
8623063Sperrin 	ASSERT(lwb->lwb_zio);
8632237Smaybee 	zio_nowait(lwb->lwb_zio);
864789Sahrens 
865789Sahrens 	return (nlwb);
866789Sahrens }
867789Sahrens 
868789Sahrens static lwb_t *
869789Sahrens zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb)
870789Sahrens {
871789Sahrens 	lr_t *lrc = &itx->itx_lr; /* common log record */
8722237Smaybee 	lr_write_t *lr = (lr_write_t *)lrc;
873789Sahrens 	uint64_t txg = lrc->lrc_txg;
874789Sahrens 	uint64_t reclen = lrc->lrc_reclen;
8752237Smaybee 	uint64_t dlen;
876789Sahrens 
877789Sahrens 	if (lwb == NULL)
878789Sahrens 		return (NULL);
879789Sahrens 	ASSERT(lwb->lwb_buf != NULL);
880789Sahrens 
8812237Smaybee 	if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY)
8822237Smaybee 		dlen = P2ROUNDUP_TYPED(
8832237Smaybee 		    lr->lr_length, sizeof (uint64_t), uint64_t);
8842237Smaybee 	else
8852237Smaybee 		dlen = 0;
8861669Sperrin 
8871669Sperrin 	zilog->zl_cur_used += (reclen + dlen);
8881669Sperrin 
8893063Sperrin 	zil_lwb_write_init(zilog, lwb);
8903063Sperrin 
8911669Sperrin 	/*
8921669Sperrin 	 * If this record won't fit in the current log block, start a new one.
8931669Sperrin 	 */
8941669Sperrin 	if (lwb->lwb_nused + reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) {
8951669Sperrin 		lwb = zil_lwb_write_start(zilog, lwb);
8962237Smaybee 		if (lwb == NULL)
8971669Sperrin 			return (NULL);
8983063Sperrin 		zil_lwb_write_init(zilog, lwb);
8991669Sperrin 		ASSERT(lwb->lwb_nused == 0);
9001669Sperrin 		if (reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) {
9011669Sperrin 			txg_wait_synced(zilog->zl_dmu_pool, txg);
902789Sahrens 			return (lwb);
903789Sahrens 		}
904789Sahrens 	}
905789Sahrens 
9062638Sperrin 	/*
9072638Sperrin 	 * Update the lrc_seq, to be log record sequence number. See zil.h
9082638Sperrin 	 * Then copy the record to the log buffer.
9092638Sperrin 	 */
9102638Sperrin 	lrc->lrc_seq = ++zilog->zl_lr_seq; /* we are single threaded */
911789Sahrens 	bcopy(lrc, lwb->lwb_buf + lwb->lwb_nused, reclen);
9122237Smaybee 
9132237Smaybee 	/*
9142237Smaybee 	 * If it's a write, fetch the data or get its blkptr as appropriate.
9152237Smaybee 	 */
9162237Smaybee 	if (lrc->lrc_txtype == TX_WRITE) {
9172237Smaybee 		if (txg > spa_freeze_txg(zilog->zl_spa))
9182237Smaybee 			txg_wait_synced(zilog->zl_dmu_pool, txg);
9192237Smaybee 		if (itx->itx_wr_state != WR_COPIED) {
9202237Smaybee 			char *dbuf;
9212237Smaybee 			int error;
9222237Smaybee 
9232237Smaybee 			/* alignment is guaranteed */
9242237Smaybee 			lr = (lr_write_t *)(lwb->lwb_buf + lwb->lwb_nused);
9252237Smaybee 			if (dlen) {
9262237Smaybee 				ASSERT(itx->itx_wr_state == WR_NEED_COPY);
9272237Smaybee 				dbuf = lwb->lwb_buf + lwb->lwb_nused + reclen;
9282237Smaybee 				lr->lr_common.lrc_reclen += dlen;
9292237Smaybee 			} else {
9302237Smaybee 				ASSERT(itx->itx_wr_state == WR_INDIRECT);
9312237Smaybee 				dbuf = NULL;
9322237Smaybee 			}
9332237Smaybee 			error = zilog->zl_get_data(
9342237Smaybee 			    itx->itx_private, lr, dbuf, lwb->lwb_zio);
93510209SMark.Musante@Sun.COM 			if (error == EIO) {
93610209SMark.Musante@Sun.COM 				txg_wait_synced(zilog->zl_dmu_pool, txg);
93710209SMark.Musante@Sun.COM 				return (lwb);
93810209SMark.Musante@Sun.COM 			}
9392237Smaybee 			if (error) {
9402237Smaybee 				ASSERT(error == ENOENT || error == EEXIST ||
9412237Smaybee 				    error == EALREADY);
9422237Smaybee 				return (lwb);
9432237Smaybee 			}
9442237Smaybee 		}
9451669Sperrin 	}
9462237Smaybee 
9472237Smaybee 	lwb->lwb_nused += reclen + dlen;
948789Sahrens 	lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg);
949789Sahrens 	ASSERT3U(lwb->lwb_nused, <=, ZIL_BLK_DATA_SZ(lwb));
950789Sahrens 	ASSERT3U(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)), ==, 0);
951789Sahrens 
952789Sahrens 	return (lwb);
953789Sahrens }
954789Sahrens 
955789Sahrens itx_t *
9565331Samw zil_itx_create(uint64_t txtype, size_t lrsize)
957789Sahrens {
958789Sahrens 	itx_t *itx;
959789Sahrens 
9601842Sperrin 	lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t);
961789Sahrens 
962789Sahrens 	itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP);
963789Sahrens 	itx->itx_lr.lrc_txtype = txtype;
964789Sahrens 	itx->itx_lr.lrc_reclen = lrsize;
9656101Sperrin 	itx->itx_sod = lrsize; /* if write & WR_NEED_COPY will be increased */
966789Sahrens 	itx->itx_lr.lrc_seq = 0;	/* defensive */
967789Sahrens 
968789Sahrens 	return (itx);
969789Sahrens }
970789Sahrens 
971789Sahrens uint64_t
972789Sahrens zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx)
973789Sahrens {
974789Sahrens 	uint64_t seq;
975789Sahrens 
976789Sahrens 	ASSERT(itx->itx_lr.lrc_seq == 0);
977789Sahrens 
978789Sahrens 	mutex_enter(&zilog->zl_lock);
979789Sahrens 	list_insert_tail(&zilog->zl_itx_list, itx);
9806101Sperrin 	zilog->zl_itx_list_sz += itx->itx_sod;
981789Sahrens 	itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx);
982789Sahrens 	itx->itx_lr.lrc_seq = seq = ++zilog->zl_itx_seq;
983789Sahrens 	mutex_exit(&zilog->zl_lock);
984789Sahrens 
985789Sahrens 	return (seq);
986789Sahrens }
987789Sahrens 
988789Sahrens /*
989789Sahrens  * Free up all in-memory intent log transactions that have now been synced.
990789Sahrens  */
991789Sahrens static void
992789Sahrens zil_itx_clean(zilog_t *zilog)
993789Sahrens {
994789Sahrens 	uint64_t synced_txg = spa_last_synced_txg(zilog->zl_spa);
995789Sahrens 	uint64_t freeze_txg = spa_freeze_txg(zilog->zl_spa);
9963778Sjohansen 	list_t clean_list;
997789Sahrens 	itx_t *itx;
998789Sahrens 
9993778Sjohansen 	list_create(&clean_list, sizeof (itx_t), offsetof(itx_t, itx_node));
10003778Sjohansen 
1001789Sahrens 	mutex_enter(&zilog->zl_lock);
10022638Sperrin 	/* wait for a log writer to finish walking list */
10032638Sperrin 	while (zilog->zl_writer) {
10042638Sperrin 		cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
10052638Sperrin 	}
10063778Sjohansen 
10073778Sjohansen 	/*
10083778Sjohansen 	 * Move the sync'd log transactions to a separate list so we can call
10093778Sjohansen 	 * kmem_free without holding the zl_lock.
10103778Sjohansen 	 *
10113778Sjohansen 	 * There is no need to set zl_writer as we don't drop zl_lock here
10123778Sjohansen 	 */
1013789Sahrens 	while ((itx = list_head(&zilog->zl_itx_list)) != NULL &&
1014789Sahrens 	    itx->itx_lr.lrc_txg <= MIN(synced_txg, freeze_txg)) {
1015789Sahrens 		list_remove(&zilog->zl_itx_list, itx);
10166101Sperrin 		zilog->zl_itx_list_sz -= itx->itx_sod;
10173778Sjohansen 		list_insert_tail(&clean_list, itx);
10183778Sjohansen 	}
10193778Sjohansen 	cv_broadcast(&zilog->zl_cv_writer);
10203778Sjohansen 	mutex_exit(&zilog->zl_lock);
10213778Sjohansen 
10223778Sjohansen 	/* destroy sync'd log transactions */
10233778Sjohansen 	while ((itx = list_head(&clean_list)) != NULL) {
10243778Sjohansen 		list_remove(&clean_list, itx);
1025789Sahrens 		kmem_free(itx, offsetof(itx_t, itx_lr)
1026789Sahrens 		    + itx->itx_lr.lrc_reclen);
1027789Sahrens 	}
10283778Sjohansen 	list_destroy(&clean_list);
1029789Sahrens }
1030789Sahrens 
10312638Sperrin /*
10323063Sperrin  * If there are any in-memory intent log transactions which have now been
10333063Sperrin  * synced then start up a taskq to free them.
10342638Sperrin  */
1035789Sahrens void
1036789Sahrens zil_clean(zilog_t *zilog)
1037789Sahrens {
10383063Sperrin 	itx_t *itx;
10393063Sperrin 
1040789Sahrens 	mutex_enter(&zilog->zl_lock);
10413063Sperrin 	itx = list_head(&zilog->zl_itx_list);
10423063Sperrin 	if ((itx != NULL) &&
10433063Sperrin 	    (itx->itx_lr.lrc_txg <= spa_last_synced_txg(zilog->zl_spa))) {
1044789Sahrens 		(void) taskq_dispatch(zilog->zl_clean_taskq,
10459321SNeil.Perrin@Sun.COM 		    (task_func_t *)zil_itx_clean, zilog, TQ_SLEEP);
10463063Sperrin 	}
1047789Sahrens 	mutex_exit(&zilog->zl_lock);
1048789Sahrens }
1049789Sahrens 
10507754SJeff.Bonwick@Sun.COM static void
10512638Sperrin zil_commit_writer(zilog_t *zilog, uint64_t seq, uint64_t foid)
1052789Sahrens {
1053789Sahrens 	uint64_t txg;
10543063Sperrin 	uint64_t commit_seq = 0;
10552638Sperrin 	itx_t *itx, *itx_next = (itx_t *)-1;
1056789Sahrens 	lwb_t *lwb;
1057789Sahrens 	spa_t *spa;
1058789Sahrens 
10592638Sperrin 	zilog->zl_writer = B_TRUE;
10607754SJeff.Bonwick@Sun.COM 	ASSERT(zilog->zl_root_zio == NULL);
1061789Sahrens 	spa = zilog->zl_spa;
1062789Sahrens 
1063789Sahrens 	if (zilog->zl_suspend) {
1064789Sahrens 		lwb = NULL;
1065789Sahrens 	} else {
1066789Sahrens 		lwb = list_tail(&zilog->zl_lwb_list);
1067789Sahrens 		if (lwb == NULL) {
10682638Sperrin 			/*
10692638Sperrin 			 * Return if there's nothing to flush before we
10702638Sperrin 			 * dirty the fs by calling zil_create()
10712638Sperrin 			 */
10722638Sperrin 			if (list_is_empty(&zilog->zl_itx_list)) {
10732638Sperrin 				zilog->zl_writer = B_FALSE;
10742638Sperrin 				return;
10752638Sperrin 			}
1076789Sahrens 			mutex_exit(&zilog->zl_lock);
1077789Sahrens 			zil_create(zilog);
1078789Sahrens 			mutex_enter(&zilog->zl_lock);
1079789Sahrens 			lwb = list_tail(&zilog->zl_lwb_list);
1080789Sahrens 		}
1081789Sahrens 	}
1082789Sahrens 
10833063Sperrin 	/* Loop through in-memory log transactions filling log blocks. */
10842638Sperrin 	DTRACE_PROBE1(zil__cw1, zilog_t *, zilog);
1085789Sahrens 	for (;;) {
10862638Sperrin 		/*
10872638Sperrin 		 * Find the next itx to push:
10882638Sperrin 		 * Push all transactions related to specified foid and all
10892638Sperrin 		 * other transactions except TX_WRITE, TX_TRUNCATE,
10902638Sperrin 		 * TX_SETATTR and TX_ACL for all other files.
10912638Sperrin 		 */
10922638Sperrin 		if (itx_next != (itx_t *)-1)
10932638Sperrin 			itx = itx_next;
10942638Sperrin 		else
10952638Sperrin 			itx = list_head(&zilog->zl_itx_list);
10962638Sperrin 		for (; itx != NULL; itx = list_next(&zilog->zl_itx_list, itx)) {
10972638Sperrin 			if (foid == 0) /* push all foids? */
10982638Sperrin 				break;
10993063Sperrin 			if (itx->itx_sync) /* push all O_[D]SYNC */
11003063Sperrin 				break;
11012638Sperrin 			switch (itx->itx_lr.lrc_txtype) {
11022638Sperrin 			case TX_SETATTR:
11032638Sperrin 			case TX_WRITE:
11042638Sperrin 			case TX_TRUNCATE:
11052638Sperrin 			case TX_ACL:
11062638Sperrin 				/* lr_foid is same offset for these records */
11072638Sperrin 				if (((lr_write_t *)&itx->itx_lr)->lr_foid
11082638Sperrin 				    != foid) {
11092638Sperrin 					continue; /* skip this record */
11102638Sperrin 				}
11112638Sperrin 			}
11122638Sperrin 			break;
11132638Sperrin 		}
1114789Sahrens 		if (itx == NULL)
1115789Sahrens 			break;
1116789Sahrens 
1117789Sahrens 		if ((itx->itx_lr.lrc_seq > seq) &&
11182638Sperrin 		    ((lwb == NULL) || (lwb->lwb_nused == 0) ||
11196101Sperrin 		    (lwb->lwb_nused + itx->itx_sod > ZIL_BLK_DATA_SZ(lwb)))) {
1120789Sahrens 			break;
11213063Sperrin 		}
1122789Sahrens 
11232638Sperrin 		/*
11242638Sperrin 		 * Save the next pointer.  Even though we soon drop
11252638Sperrin 		 * zl_lock all threads that may change the list
11262638Sperrin 		 * (another writer or zil_itx_clean) can't do so until
11272638Sperrin 		 * they have zl_writer.
11282638Sperrin 		 */
11292638Sperrin 		itx_next = list_next(&zilog->zl_itx_list, itx);
1130789Sahrens 		list_remove(&zilog->zl_itx_list, itx);
11316101Sperrin 		zilog->zl_itx_list_sz -= itx->itx_sod;
11323063Sperrin 		mutex_exit(&zilog->zl_lock);
1133789Sahrens 		txg = itx->itx_lr.lrc_txg;
1134789Sahrens 		ASSERT(txg);
1135789Sahrens 
1136789Sahrens 		if (txg > spa_last_synced_txg(spa) ||
1137789Sahrens 		    txg > spa_freeze_txg(spa))
1138789Sahrens 			lwb = zil_lwb_commit(zilog, itx, lwb);
1139789Sahrens 		kmem_free(itx, offsetof(itx_t, itx_lr)
1140789Sahrens 		    + itx->itx_lr.lrc_reclen);
1141789Sahrens 		mutex_enter(&zilog->zl_lock);
1142789Sahrens 	}
11432638Sperrin 	DTRACE_PROBE1(zil__cw2, zilog_t *, zilog);
11443063Sperrin 	/* determine commit sequence number */
11453063Sperrin 	itx = list_head(&zilog->zl_itx_list);
11463063Sperrin 	if (itx)
11473063Sperrin 		commit_seq = itx->itx_lr.lrc_seq;
11483063Sperrin 	else
11493063Sperrin 		commit_seq = zilog->zl_itx_seq;
1150789Sahrens 	mutex_exit(&zilog->zl_lock);
1151789Sahrens 
1152789Sahrens 	/* write the last block out */
11533063Sperrin 	if (lwb != NULL && lwb->lwb_zio != NULL)
1154789Sahrens 		lwb = zil_lwb_write_start(zilog, lwb);
1155789Sahrens 
11561141Sperrin 	zilog->zl_prev_used = zilog->zl_cur_used;
11571141Sperrin 	zilog->zl_cur_used = 0;
11581141Sperrin 
11592638Sperrin 	/*
11602638Sperrin 	 * Wait if necessary for the log blocks to be on stable storage.
11612638Sperrin 	 */
11622638Sperrin 	if (zilog->zl_root_zio) {
11632638Sperrin 		DTRACE_PROBE1(zil__cw3, zilog_t *, zilog);
11642638Sperrin 		(void) zio_wait(zilog->zl_root_zio);
11657754SJeff.Bonwick@Sun.COM 		zilog->zl_root_zio = NULL;
11662638Sperrin 		DTRACE_PROBE1(zil__cw4, zilog_t *, zilog);
11675688Sbonwick 		zil_flush_vdevs(zilog);
1168789Sahrens 	}
11691141Sperrin 
1170789Sahrens 	if (zilog->zl_log_error || lwb == NULL) {
1171789Sahrens 		zilog->zl_log_error = 0;
1172789Sahrens 		txg_wait_synced(zilog->zl_dmu_pool, 0);
1173789Sahrens 	}
11743063Sperrin 
11753063Sperrin 	mutex_enter(&zilog->zl_lock);
11761141Sperrin 	zilog->zl_writer = B_FALSE;
11773063Sperrin 
11783063Sperrin 	ASSERT3U(commit_seq, >=, zilog->zl_commit_seq);
11793063Sperrin 	zilog->zl_commit_seq = commit_seq;
11802638Sperrin }
11812638Sperrin 
11822638Sperrin /*
11832638Sperrin  * Push zfs transactions to stable storage up to the supplied sequence number.
11842638Sperrin  * If foid is 0 push out all transactions, otherwise push only those
11852638Sperrin  * for that file or might have been used to create that file.
11862638Sperrin  */
11872638Sperrin void
11882638Sperrin zil_commit(zilog_t *zilog, uint64_t seq, uint64_t foid)
11892638Sperrin {
11902638Sperrin 	if (zilog == NULL || seq == 0)
11912638Sperrin 		return;
11922638Sperrin 
11932638Sperrin 	mutex_enter(&zilog->zl_lock);
11942638Sperrin 
11952638Sperrin 	seq = MIN(seq, zilog->zl_itx_seq);	/* cap seq at largest itx seq */
11962638Sperrin 
11973063Sperrin 	while (zilog->zl_writer) {
11982638Sperrin 		cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
11993063Sperrin 		if (seq < zilog->zl_commit_seq) {
12003063Sperrin 			mutex_exit(&zilog->zl_lock);
12013063Sperrin 			return;
12023063Sperrin 		}
12033063Sperrin 	}
12042638Sperrin 	zil_commit_writer(zilog, seq, foid); /* drops zl_lock */
12053063Sperrin 	/* wake up others waiting on the commit */
12063063Sperrin 	cv_broadcast(&zilog->zl_cv_writer);
12073063Sperrin 	mutex_exit(&zilog->zl_lock);
1208789Sahrens }
1209789Sahrens 
1210789Sahrens /*
1211789Sahrens  * Called in syncing context to free committed log blocks and update log header.
1212789Sahrens  */
1213789Sahrens void
1214789Sahrens zil_sync(zilog_t *zilog, dmu_tx_t *tx)
1215789Sahrens {
12161807Sbonwick 	zil_header_t *zh = zil_header_in_syncing_context(zilog);
1217789Sahrens 	uint64_t txg = dmu_tx_get_txg(tx);
1218789Sahrens 	spa_t *spa = zilog->zl_spa;
1219789Sahrens 	lwb_t *lwb;
1220789Sahrens 
12219396SMatthew.Ahrens@Sun.COM 	/*
12229396SMatthew.Ahrens@Sun.COM 	 * We don't zero out zl_destroy_txg, so make sure we don't try
12239396SMatthew.Ahrens@Sun.COM 	 * to destroy it twice.
12249396SMatthew.Ahrens@Sun.COM 	 */
12259396SMatthew.Ahrens@Sun.COM 	if (spa_sync_pass(spa) != 1)
12269396SMatthew.Ahrens@Sun.COM 		return;
12279396SMatthew.Ahrens@Sun.COM 
12281807Sbonwick 	mutex_enter(&zilog->zl_lock);
12291807Sbonwick 
1230789Sahrens 	ASSERT(zilog->zl_stop_sync == 0);
1231789Sahrens 
12328227SNeil.Perrin@Sun.COM 	zh->zh_replay_seq = zilog->zl_replayed_seq[txg & TXG_MASK];
1233789Sahrens 
1234789Sahrens 	if (zilog->zl_destroy_txg == txg) {
12351807Sbonwick 		blkptr_t blk = zh->zh_log;
12361807Sbonwick 
12371807Sbonwick 		ASSERT(list_head(&zilog->zl_lwb_list) == NULL);
12381807Sbonwick 
12391807Sbonwick 		bzero(zh, sizeof (zil_header_t));
12408227SNeil.Perrin@Sun.COM 		bzero(zilog->zl_replayed_seq, sizeof (zilog->zl_replayed_seq));
12411807Sbonwick 
12421807Sbonwick 		if (zilog->zl_keep_first) {
12431807Sbonwick 			/*
12441807Sbonwick 			 * If this block was part of log chain that couldn't
12451807Sbonwick 			 * be claimed because a device was missing during
12461807Sbonwick 			 * zil_claim(), but that device later returns,
12471807Sbonwick 			 * then this block could erroneously appear valid.
12481807Sbonwick 			 * To guard against this, assign a new GUID to the new
12491807Sbonwick 			 * log chain so it doesn't matter what blk points to.
12501807Sbonwick 			 */
12511807Sbonwick 			zil_init_log_chain(zilog, &blk);
12521807Sbonwick 			zh->zh_log = blk;
12531807Sbonwick 		}
1254789Sahrens 	}
1255789Sahrens 
12569701SGeorge.Wilson@Sun.COM 	while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
12572638Sperrin 		zh->zh_log = lwb->lwb_blk;
1258789Sahrens 		if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg)
1259789Sahrens 			break;
1260789Sahrens 		list_remove(&zilog->zl_lwb_list, lwb);
1261789Sahrens 		zio_free_blk(spa, &lwb->lwb_blk, txg);
1262789Sahrens 		kmem_cache_free(zil_lwb_cache, lwb);
12633668Sgw25295 
12643668Sgw25295 		/*
12653668Sgw25295 		 * If we don't have anything left in the lwb list then
12663668Sgw25295 		 * we've had an allocation failure and we need to zero
12673668Sgw25295 		 * out the zil_header blkptr so that we don't end
12683668Sgw25295 		 * up freeing the same block twice.
12693668Sgw25295 		 */
12703668Sgw25295 		if (list_head(&zilog->zl_lwb_list) == NULL)
12713668Sgw25295 			BP_ZERO(&zh->zh_log);
1272789Sahrens 	}
1273789Sahrens 	mutex_exit(&zilog->zl_lock);
1274789Sahrens }
1275789Sahrens 
1276789Sahrens void
1277789Sahrens zil_init(void)
1278789Sahrens {
1279789Sahrens 	zil_lwb_cache = kmem_cache_create("zil_lwb_cache",
12802856Snd150628 	    sizeof (struct lwb), 0, NULL, NULL, NULL, NULL, NULL, 0);
1281789Sahrens }
1282789Sahrens 
1283789Sahrens void
1284789Sahrens zil_fini(void)
1285789Sahrens {
1286789Sahrens 	kmem_cache_destroy(zil_lwb_cache);
1287789Sahrens }
1288789Sahrens 
128910310SNeil.Perrin@Sun.COM void
129010310SNeil.Perrin@Sun.COM zil_set_logbias(zilog_t *zilog, uint64_t logbias)
129110310SNeil.Perrin@Sun.COM {
129210310SNeil.Perrin@Sun.COM 	zilog->zl_logbias = logbias;
129310310SNeil.Perrin@Sun.COM }
129410310SNeil.Perrin@Sun.COM 
1295789Sahrens zilog_t *
1296789Sahrens zil_alloc(objset_t *os, zil_header_t *zh_phys)
1297789Sahrens {
1298789Sahrens 	zilog_t *zilog;
1299789Sahrens 
1300789Sahrens 	zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP);
1301789Sahrens 
1302789Sahrens 	zilog->zl_header = zh_phys;
1303789Sahrens 	zilog->zl_os = os;
1304789Sahrens 	zilog->zl_spa = dmu_objset_spa(os);
1305789Sahrens 	zilog->zl_dmu_pool = dmu_objset_pool(os);
13061807Sbonwick 	zilog->zl_destroy_txg = TXG_INITIAL - 1;
130710310SNeil.Perrin@Sun.COM 	zilog->zl_logbias = dmu_objset_logbias(os);
1308789Sahrens 
13092856Snd150628 	mutex_init(&zilog->zl_lock, NULL, MUTEX_DEFAULT, NULL);
13102856Snd150628 
1311789Sahrens 	list_create(&zilog->zl_itx_list, sizeof (itx_t),
1312789Sahrens 	    offsetof(itx_t, itx_node));
1313789Sahrens 
1314789Sahrens 	list_create(&zilog->zl_lwb_list, sizeof (lwb_t),
1315789Sahrens 	    offsetof(lwb_t, lwb_node));
1316789Sahrens 
13175688Sbonwick 	mutex_init(&zilog->zl_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
13185688Sbonwick 
13195688Sbonwick 	avl_create(&zilog->zl_vdev_tree, zil_vdev_compare,
13205688Sbonwick 	    sizeof (zil_vdev_node_t), offsetof(zil_vdev_node_t, zv_node));
1321789Sahrens 
13225913Sperrin 	cv_init(&zilog->zl_cv_writer, NULL, CV_DEFAULT, NULL);
13235913Sperrin 	cv_init(&zilog->zl_cv_suspend, NULL, CV_DEFAULT, NULL);
13245913Sperrin 
1325789Sahrens 	return (zilog);
1326789Sahrens }
1327789Sahrens 
1328789Sahrens void
1329789Sahrens zil_free(zilog_t *zilog)
1330789Sahrens {
1331789Sahrens 	lwb_t *lwb;
1332789Sahrens 
1333789Sahrens 	zilog->zl_stop_sync = 1;
1334789Sahrens 
1335789Sahrens 	while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
1336789Sahrens 		list_remove(&zilog->zl_lwb_list, lwb);
1337789Sahrens 		if (lwb->lwb_buf != NULL)
1338789Sahrens 			zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
1339789Sahrens 		kmem_cache_free(zil_lwb_cache, lwb);
1340789Sahrens 	}
1341789Sahrens 	list_destroy(&zilog->zl_lwb_list);
1342789Sahrens 
13435688Sbonwick 	avl_destroy(&zilog->zl_vdev_tree);
13445688Sbonwick 	mutex_destroy(&zilog->zl_vdev_lock);
1345789Sahrens 
1346789Sahrens 	ASSERT(list_head(&zilog->zl_itx_list) == NULL);
1347789Sahrens 	list_destroy(&zilog->zl_itx_list);
13482856Snd150628 	mutex_destroy(&zilog->zl_lock);
1349789Sahrens 
13505913Sperrin 	cv_destroy(&zilog->zl_cv_writer);
13515913Sperrin 	cv_destroy(&zilog->zl_cv_suspend);
13525913Sperrin 
1353789Sahrens 	kmem_free(zilog, sizeof (zilog_t));
1354789Sahrens }
1355789Sahrens 
1356789Sahrens /*
1357789Sahrens  * Open an intent log.
1358789Sahrens  */
1359789Sahrens zilog_t *
1360789Sahrens zil_open(objset_t *os, zil_get_data_t *get_data)
1361789Sahrens {
1362789Sahrens 	zilog_t *zilog = dmu_objset_zil(os);
1363789Sahrens 
1364789Sahrens 	zilog->zl_get_data = get_data;
1365789Sahrens 	zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri,
1366789Sahrens 	    2, 2, TASKQ_PREPOPULATE);
1367789Sahrens 
1368789Sahrens 	return (zilog);
1369789Sahrens }
1370789Sahrens 
1371789Sahrens /*
1372789Sahrens  * Close an intent log.
1373789Sahrens  */
1374789Sahrens void
1375789Sahrens zil_close(zilog_t *zilog)
1376789Sahrens {
13771807Sbonwick 	/*
13781807Sbonwick 	 * If the log isn't already committed, mark the objset dirty
13791807Sbonwick 	 * (so zil_sync() will be called) and wait for that txg to sync.
13801807Sbonwick 	 */
13811807Sbonwick 	if (!zil_is_committed(zilog)) {
13821807Sbonwick 		uint64_t txg;
13831807Sbonwick 		dmu_tx_t *tx = dmu_tx_create(zilog->zl_os);
13841807Sbonwick 		(void) dmu_tx_assign(tx, TXG_WAIT);
13851807Sbonwick 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
13861807Sbonwick 		txg = dmu_tx_get_txg(tx);
13871807Sbonwick 		dmu_tx_commit(tx);
13881807Sbonwick 		txg_wait_synced(zilog->zl_dmu_pool, txg);
13891807Sbonwick 	}
13901807Sbonwick 
1391789Sahrens 	taskq_destroy(zilog->zl_clean_taskq);
1392789Sahrens 	zilog->zl_clean_taskq = NULL;
1393789Sahrens 	zilog->zl_get_data = NULL;
1394789Sahrens 
1395789Sahrens 	zil_itx_clean(zilog);
1396789Sahrens 	ASSERT(list_head(&zilog->zl_itx_list) == NULL);
1397789Sahrens }
1398789Sahrens 
1399789Sahrens /*
1400789Sahrens  * Suspend an intent log.  While in suspended mode, we still honor
1401789Sahrens  * synchronous semantics, but we rely on txg_wait_synced() to do it.
1402789Sahrens  * We suspend the log briefly when taking a snapshot so that the snapshot
1403789Sahrens  * contains all the data it's supposed to, and has an empty intent log.
1404789Sahrens  */
1405789Sahrens int
1406789Sahrens zil_suspend(zilog_t *zilog)
1407789Sahrens {
14081807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
1409789Sahrens 
1410789Sahrens 	mutex_enter(&zilog->zl_lock);
14118989SNeil.Perrin@Sun.COM 	if (zh->zh_flags & ZIL_REPLAY_NEEDED) {		/* unplayed log */
1412789Sahrens 		mutex_exit(&zilog->zl_lock);
1413789Sahrens 		return (EBUSY);
1414789Sahrens 	}
14151807Sbonwick 	if (zilog->zl_suspend++ != 0) {
14161807Sbonwick 		/*
14171807Sbonwick 		 * Someone else already began a suspend.
14181807Sbonwick 		 * Just wait for them to finish.
14191807Sbonwick 		 */
14201807Sbonwick 		while (zilog->zl_suspending)
14211807Sbonwick 			cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock);
14221807Sbonwick 		mutex_exit(&zilog->zl_lock);
14231807Sbonwick 		return (0);
14241807Sbonwick 	}
14251807Sbonwick 	zilog->zl_suspending = B_TRUE;
1426789Sahrens 	mutex_exit(&zilog->zl_lock);
1427789Sahrens 
14282638Sperrin 	zil_commit(zilog, UINT64_MAX, 0);
1429789Sahrens 
14302638Sperrin 	/*
14312638Sperrin 	 * Wait for any in-flight log writes to complete.
14322638Sperrin 	 */
1433789Sahrens 	mutex_enter(&zilog->zl_lock);
14342638Sperrin 	while (zilog->zl_writer)
14352638Sperrin 		cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
1436789Sahrens 	mutex_exit(&zilog->zl_lock);
1437789Sahrens 
14381807Sbonwick 	zil_destroy(zilog, B_FALSE);
14391807Sbonwick 
14401807Sbonwick 	mutex_enter(&zilog->zl_lock);
14411807Sbonwick 	zilog->zl_suspending = B_FALSE;
14421807Sbonwick 	cv_broadcast(&zilog->zl_cv_suspend);
14431807Sbonwick 	mutex_exit(&zilog->zl_lock);
1444789Sahrens 
1445789Sahrens 	return (0);
1446789Sahrens }
1447789Sahrens 
1448789Sahrens void
1449789Sahrens zil_resume(zilog_t *zilog)
1450789Sahrens {
1451789Sahrens 	mutex_enter(&zilog->zl_lock);
1452789Sahrens 	ASSERT(zilog->zl_suspend != 0);
1453789Sahrens 	zilog->zl_suspend--;
1454789Sahrens 	mutex_exit(&zilog->zl_lock);
1455789Sahrens }
1456789Sahrens 
1457*10800SNeil.Perrin@Sun.COM /*
1458*10800SNeil.Perrin@Sun.COM  * Read in the data for the dmu_sync()ed block, and change the log
1459*10800SNeil.Perrin@Sun.COM  * record to write this whole block.
1460*10800SNeil.Perrin@Sun.COM  */
1461*10800SNeil.Perrin@Sun.COM void
1462*10800SNeil.Perrin@Sun.COM zil_get_replay_data(zilog_t *zilog, lr_write_t *lr)
1463*10800SNeil.Perrin@Sun.COM {
1464*10800SNeil.Perrin@Sun.COM 	blkptr_t *wbp = &lr->lr_blkptr;
1465*10800SNeil.Perrin@Sun.COM 	char *wbuf = (char *)(lr + 1); /* data follows lr_write_t */
1466*10800SNeil.Perrin@Sun.COM 	uint64_t blksz;
1467*10800SNeil.Perrin@Sun.COM 
1468*10800SNeil.Perrin@Sun.COM 	if (BP_IS_HOLE(wbp)) {	/* compressed to a hole */
1469*10800SNeil.Perrin@Sun.COM 		blksz = BP_GET_LSIZE(&lr->lr_blkptr);
1470*10800SNeil.Perrin@Sun.COM 		/*
1471*10800SNeil.Perrin@Sun.COM 		 * If the blksz is zero then we must be replaying a log
1472*10800SNeil.Perrin@Sun.COM 		 * from an version prior to setting the blksize of null blocks.
1473*10800SNeil.Perrin@Sun.COM 		 * So we just zero the actual write size reqeusted.
1474*10800SNeil.Perrin@Sun.COM 		 */
1475*10800SNeil.Perrin@Sun.COM 		if (blksz == 0) {
1476*10800SNeil.Perrin@Sun.COM 			bzero(wbuf, lr->lr_length);
1477*10800SNeil.Perrin@Sun.COM 			return;
1478*10800SNeil.Perrin@Sun.COM 		}
1479*10800SNeil.Perrin@Sun.COM 		bzero(wbuf, blksz);
1480*10800SNeil.Perrin@Sun.COM 	} else {
1481*10800SNeil.Perrin@Sun.COM 		/*
1482*10800SNeil.Perrin@Sun.COM 		 * A subsequent write may have overwritten this block, in which
1483*10800SNeil.Perrin@Sun.COM 		 * case wbp may have been been freed and reallocated, and our
1484*10800SNeil.Perrin@Sun.COM 		 * read of wbp may fail with a checksum error.  We can safely
1485*10800SNeil.Perrin@Sun.COM 		 * ignore this because the later write will provide the
1486*10800SNeil.Perrin@Sun.COM 		 * correct data.
1487*10800SNeil.Perrin@Sun.COM 		 */
1488*10800SNeil.Perrin@Sun.COM 		zbookmark_t zb;
1489*10800SNeil.Perrin@Sun.COM 
1490*10800SNeil.Perrin@Sun.COM 		zb.zb_objset = dmu_objset_id(zilog->zl_os);
1491*10800SNeil.Perrin@Sun.COM 		zb.zb_object = lr->lr_foid;
1492*10800SNeil.Perrin@Sun.COM 		zb.zb_level = 0;
1493*10800SNeil.Perrin@Sun.COM 		zb.zb_blkid = -1; /* unknown */
1494*10800SNeil.Perrin@Sun.COM 
1495*10800SNeil.Perrin@Sun.COM 		blksz = BP_GET_LSIZE(&lr->lr_blkptr);
1496*10800SNeil.Perrin@Sun.COM 		(void) zio_wait(zio_read(NULL, zilog->zl_spa, wbp, wbuf, blksz,
1497*10800SNeil.Perrin@Sun.COM 		    NULL, NULL, ZIO_PRIORITY_SYNC_READ,
1498*10800SNeil.Perrin@Sun.COM 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &zb));
1499*10800SNeil.Perrin@Sun.COM 	}
1500*10800SNeil.Perrin@Sun.COM 	lr->lr_offset -= lr->lr_offset % blksz;
1501*10800SNeil.Perrin@Sun.COM 	lr->lr_length = blksz;
1502*10800SNeil.Perrin@Sun.COM }
1503*10800SNeil.Perrin@Sun.COM 
1504789Sahrens typedef struct zil_replay_arg {
1505789Sahrens 	objset_t	*zr_os;
1506789Sahrens 	zil_replay_func_t **zr_replay;
1507789Sahrens 	void		*zr_arg;
1508789Sahrens 	boolean_t	zr_byteswap;
1509789Sahrens 	char		*zr_lrbuf;
1510789Sahrens } zil_replay_arg_t;
1511789Sahrens 
1512789Sahrens static void
1513789Sahrens zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg)
1514789Sahrens {
1515789Sahrens 	zil_replay_arg_t *zr = zra;
15161807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
1517789Sahrens 	uint64_t reclen = lr->lrc_reclen;
1518789Sahrens 	uint64_t txtype = lr->lrc_txtype;
15193063Sperrin 	char *name;
15208227SNeil.Perrin@Sun.COM 	int pass, error;
1521789Sahrens 
15228227SNeil.Perrin@Sun.COM 	if (!zilog->zl_replay)			/* giving up */
1523789Sahrens 		return;
1524789Sahrens 
1525789Sahrens 	if (lr->lrc_txg < claim_txg)		/* already committed */
1526789Sahrens 		return;
1527789Sahrens 
1528789Sahrens 	if (lr->lrc_seq <= zh->zh_replay_seq)	/* already replayed */
1529789Sahrens 		return;
1530789Sahrens 
15315331Samw 	/* Strip case-insensitive bit, still present in log record */
15325331Samw 	txtype &= ~TX_CI;
15335331Samw 
15348227SNeil.Perrin@Sun.COM 	if (txtype == 0 || txtype >= TX_MAX_TYPE) {
15358227SNeil.Perrin@Sun.COM 		error = EINVAL;
15368227SNeil.Perrin@Sun.COM 		goto bad;
15378227SNeil.Perrin@Sun.COM 	}
15388227SNeil.Perrin@Sun.COM 
1539789Sahrens 	/*
1540789Sahrens 	 * Make a copy of the data so we can revise and extend it.
1541789Sahrens 	 */
1542789Sahrens 	bcopy(lr, zr->zr_lrbuf, reclen);
1543789Sahrens 
1544789Sahrens 	/*
1545789Sahrens 	 * The log block containing this lr may have been byteswapped
1546789Sahrens 	 * so that we can easily examine common fields like lrc_txtype.
1547789Sahrens 	 * However, the log is a mix of different data types, and only the
1548789Sahrens 	 * replay vectors know how to byteswap their records.  Therefore, if
1549789Sahrens 	 * the lr was byteswapped, undo it before invoking the replay vector.
1550789Sahrens 	 */
1551789Sahrens 	if (zr->zr_byteswap)
1552789Sahrens 		byteswap_uint64_array(zr->zr_lrbuf, reclen);
1553789Sahrens 
1554789Sahrens 	/*
1555789Sahrens 	 * We must now do two things atomically: replay this log record,
15568227SNeil.Perrin@Sun.COM 	 * and update the log header sequence number to reflect the fact that
15578227SNeil.Perrin@Sun.COM 	 * we did so. At the end of each replay function the sequence number
15588227SNeil.Perrin@Sun.COM 	 * is updated if we are in replay mode.
1559789Sahrens 	 */
15608227SNeil.Perrin@Sun.COM 	for (pass = 1; pass <= 2; pass++) {
15618227SNeil.Perrin@Sun.COM 		zilog->zl_replaying_seq = lr->lrc_seq;
15628227SNeil.Perrin@Sun.COM 		/* Only byteswap (if needed) on the 1st pass.  */
15638227SNeil.Perrin@Sun.COM 		error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lrbuf,
15648227SNeil.Perrin@Sun.COM 		    zr->zr_byteswap && pass == 1);
1565789Sahrens 
15663063Sperrin 		if (!error)
15673063Sperrin 			return;
15683063Sperrin 
15693063Sperrin 		/*
15703063Sperrin 		 * The DMU's dnode layer doesn't see removes until the txg
15713063Sperrin 		 * commits, so a subsequent claim can spuriously fail with
15728227SNeil.Perrin@Sun.COM 		 * EEXIST. So if we receive any error we try syncing out
15738227SNeil.Perrin@Sun.COM 		 * any removes then retry the transaction.
15743063Sperrin 		 */
15758227SNeil.Perrin@Sun.COM 		if (pass == 1)
15763063Sperrin 			txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0);
1577789Sahrens 	}
1578789Sahrens 
15797904SNeil.Perrin@Sun.COM bad:
15808227SNeil.Perrin@Sun.COM 	ASSERT(error);
15813063Sperrin 	name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
15823063Sperrin 	dmu_objset_name(zr->zr_os, name);
15833063Sperrin 	cmn_err(CE_WARN, "ZFS replay transaction error %d, "
15845331Samw 	    "dataset %s, seq 0x%llx, txtype %llu %s\n",
15855331Samw 	    error, name, (u_longlong_t)lr->lrc_seq, (u_longlong_t)txtype,
15865331Samw 	    (lr->lrc_txtype & TX_CI) ? "CI" : "");
15878227SNeil.Perrin@Sun.COM 	zilog->zl_replay = B_FALSE;
15883063Sperrin 	kmem_free(name, MAXNAMELEN);
15893063Sperrin }
1590789Sahrens 
15913063Sperrin /* ARGSUSED */
15923063Sperrin static void
15933063Sperrin zil_incr_blks(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
15943063Sperrin {
15953063Sperrin 	zilog->zl_replay_blks++;
1596789Sahrens }
1597789Sahrens 
1598789Sahrens /*
15991362Sperrin  * If this dataset has a non-empty intent log, replay it and destroy it.
1600789Sahrens  */
1601789Sahrens void
16028227SNeil.Perrin@Sun.COM zil_replay(objset_t *os, void *arg, zil_replay_func_t *replay_func[TX_MAX_TYPE])
1603789Sahrens {
1604789Sahrens 	zilog_t *zilog = dmu_objset_zil(os);
16051807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
16061807Sbonwick 	zil_replay_arg_t zr;
16071362Sperrin 
16088989SNeil.Perrin@Sun.COM 	if ((zh->zh_flags & ZIL_REPLAY_NEEDED) == 0) {
16091807Sbonwick 		zil_destroy(zilog, B_TRUE);
16101362Sperrin 		return;
16111362Sperrin 	}
1612789Sahrens 
1613789Sahrens 	zr.zr_os = os;
1614789Sahrens 	zr.zr_replay = replay_func;
1615789Sahrens 	zr.zr_arg = arg;
16161807Sbonwick 	zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log);
1617789Sahrens 	zr.zr_lrbuf = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP);
1618789Sahrens 
1619789Sahrens 	/*
1620789Sahrens 	 * Wait for in-progress removes to sync before starting replay.
1621789Sahrens 	 */
1622789Sahrens 	txg_wait_synced(zilog->zl_dmu_pool, 0);
1623789Sahrens 
16248227SNeil.Perrin@Sun.COM 	zilog->zl_replay = B_TRUE;
16253063Sperrin 	zilog->zl_replay_time = lbolt;
16263063Sperrin 	ASSERT(zilog->zl_replay_blks == 0);
16273063Sperrin 	(void) zil_parse(zilog, zil_incr_blks, zil_replay_log_record, &zr,
16281807Sbonwick 	    zh->zh_claim_txg);
1629789Sahrens 	kmem_free(zr.zr_lrbuf, 2 * SPA_MAXBLOCKSIZE);
1630789Sahrens 
16311807Sbonwick 	zil_destroy(zilog, B_FALSE);
16325712Sahrens 	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
16338227SNeil.Perrin@Sun.COM 	zilog->zl_replay = B_FALSE;
1634789Sahrens }
16351646Sperrin 
16361646Sperrin /*
16371646Sperrin  * Report whether all transactions are committed
16381646Sperrin  */
16391646Sperrin int
16401646Sperrin zil_is_committed(zilog_t *zilog)
16411646Sperrin {
16421646Sperrin 	lwb_t *lwb;
16432638Sperrin 	int ret;
16441646Sperrin 
16452638Sperrin 	mutex_enter(&zilog->zl_lock);
16462638Sperrin 	while (zilog->zl_writer)
16472638Sperrin 		cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
16482638Sperrin 
16492638Sperrin 	/* recent unpushed intent log transactions? */
16502638Sperrin 	if (!list_is_empty(&zilog->zl_itx_list)) {
16512638Sperrin 		ret = B_FALSE;
16522638Sperrin 		goto out;
16532638Sperrin 	}
16542638Sperrin 
16552638Sperrin 	/* intent log never used? */
16562638Sperrin 	lwb = list_head(&zilog->zl_lwb_list);
16572638Sperrin 	if (lwb == NULL) {
16582638Sperrin 		ret = B_TRUE;
16592638Sperrin 		goto out;
16602638Sperrin 	}
16611646Sperrin 
16621646Sperrin 	/*
16632638Sperrin 	 * more than 1 log buffer means zil_sync() hasn't yet freed
16642638Sperrin 	 * entries after a txg has committed
16651646Sperrin 	 */
16662638Sperrin 	if (list_next(&zilog->zl_lwb_list, lwb)) {
16672638Sperrin 		ret = B_FALSE;
16682638Sperrin 		goto out;
16692638Sperrin 	}
16702638Sperrin 
16711646Sperrin 	ASSERT(zil_empty(zilog));
16722638Sperrin 	ret = B_TRUE;
16732638Sperrin out:
16742638Sperrin 	cv_broadcast(&zilog->zl_cv_writer);
16752638Sperrin 	mutex_exit(&zilog->zl_lock);
16762638Sperrin 	return (ret);
16771646Sperrin }
16789701SGeorge.Wilson@Sun.COM 
16799701SGeorge.Wilson@Sun.COM /* ARGSUSED */
16809701SGeorge.Wilson@Sun.COM int
16819701SGeorge.Wilson@Sun.COM zil_vdev_offline(char *osname, void *arg)
16829701SGeorge.Wilson@Sun.COM {
16839701SGeorge.Wilson@Sun.COM 	objset_t *os;
16849701SGeorge.Wilson@Sun.COM 	zilog_t *zilog;
16859701SGeorge.Wilson@Sun.COM 	int error;
16869701SGeorge.Wilson@Sun.COM 
168710298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_hold(osname, FTAG, &os);
16889701SGeorge.Wilson@Sun.COM 	if (error)
16899701SGeorge.Wilson@Sun.COM 		return (error);
16909701SGeorge.Wilson@Sun.COM 
16919701SGeorge.Wilson@Sun.COM 	zilog = dmu_objset_zil(os);
16929701SGeorge.Wilson@Sun.COM 	if (zil_suspend(zilog) != 0)
16939701SGeorge.Wilson@Sun.COM 		error = EEXIST;
16949701SGeorge.Wilson@Sun.COM 	else
16959701SGeorge.Wilson@Sun.COM 		zil_resume(zilog);
169610298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
16979701SGeorge.Wilson@Sun.COM 	return (error);
16989701SGeorge.Wilson@Sun.COM }
1699