xref: /onnv-gate/usr/src/uts/common/fs/zfs/zil.c (revision 8227)
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 /*
225809Sperrin  * Copyright 2008 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>
28789Sahrens #include <sys/dmu.h>
29789Sahrens #include <sys/zap.h>
30789Sahrens #include <sys/arc.h>
31789Sahrens #include <sys/stat.h>
32789Sahrens #include <sys/resource.h>
33789Sahrens #include <sys/zil.h>
34789Sahrens #include <sys/zil_impl.h>
35789Sahrens #include <sys/dsl_dataset.h>
36789Sahrens #include <sys/vdev.h>
373668Sgw25295 #include <sys/dmu_tx.h>
38789Sahrens 
39789Sahrens /*
40789Sahrens  * The zfs intent log (ZIL) saves transaction records of system calls
41789Sahrens  * that change the file system in memory with enough information
42789Sahrens  * to be able to replay them. These are stored in memory until
43789Sahrens  * either the DMU transaction group (txg) commits them to the stable pool
44789Sahrens  * and they can be discarded, or they are flushed to the stable log
45789Sahrens  * (also in the pool) due to a fsync, O_DSYNC or other synchronous
46789Sahrens  * requirement. In the event of a panic or power fail then those log
47789Sahrens  * records (transactions) are replayed.
48789Sahrens  *
49789Sahrens  * There is one ZIL per file system. Its on-disk (pool) format consists
50789Sahrens  * of 3 parts:
51789Sahrens  *
52789Sahrens  * 	- ZIL header
53789Sahrens  * 	- ZIL blocks
54789Sahrens  * 	- ZIL records
55789Sahrens  *
56789Sahrens  * A log record holds a system call transaction. Log blocks can
57789Sahrens  * hold many log records and the blocks are chained together.
58789Sahrens  * Each ZIL block contains a block pointer (blkptr_t) to the next
59789Sahrens  * ZIL block in the chain. The ZIL header points to the first
60789Sahrens  * block in the chain. Note there is not a fixed place in the pool
61789Sahrens  * to hold blocks. They are dynamically allocated and freed as
62789Sahrens  * needed from the blocks available. Figure X shows the ZIL structure:
63789Sahrens  */
64789Sahrens 
65789Sahrens /*
662986Sek110237  * This global ZIL switch affects all pools
67789Sahrens  */
68789Sahrens int zil_disable = 0;	/* disable intent logging */
692986Sek110237 
702986Sek110237 /*
712986Sek110237  * Tunable parameter for debugging or performance analysis.  Setting
722986Sek110237  * zfs_nocacheflush will cause corruption on power loss if a volatile
732986Sek110237  * out-of-order write cache is enabled.
742986Sek110237  */
752986Sek110237 boolean_t zfs_nocacheflush = B_FALSE;
76789Sahrens 
77789Sahrens static kmem_cache_t *zil_lwb_cache;
78789Sahrens 
79789Sahrens static int
80789Sahrens zil_dva_compare(const void *x1, const void *x2)
81789Sahrens {
82789Sahrens 	const dva_t *dva1 = x1;
83789Sahrens 	const dva_t *dva2 = x2;
84789Sahrens 
85789Sahrens 	if (DVA_GET_VDEV(dva1) < DVA_GET_VDEV(dva2))
86789Sahrens 		return (-1);
87789Sahrens 	if (DVA_GET_VDEV(dva1) > DVA_GET_VDEV(dva2))
88789Sahrens 		return (1);
89789Sahrens 
90789Sahrens 	if (DVA_GET_OFFSET(dva1) < DVA_GET_OFFSET(dva2))
91789Sahrens 		return (-1);
92789Sahrens 	if (DVA_GET_OFFSET(dva1) > DVA_GET_OFFSET(dva2))
93789Sahrens 		return (1);
94789Sahrens 
95789Sahrens 	return (0);
96789Sahrens }
97789Sahrens 
98789Sahrens static void
99789Sahrens zil_dva_tree_init(avl_tree_t *t)
100789Sahrens {
101789Sahrens 	avl_create(t, zil_dva_compare, sizeof (zil_dva_node_t),
102789Sahrens 	    offsetof(zil_dva_node_t, zn_node));
103789Sahrens }
104789Sahrens 
105789Sahrens static void
106789Sahrens zil_dva_tree_fini(avl_tree_t *t)
107789Sahrens {
108789Sahrens 	zil_dva_node_t *zn;
109789Sahrens 	void *cookie = NULL;
110789Sahrens 
111789Sahrens 	while ((zn = avl_destroy_nodes(t, &cookie)) != NULL)
112789Sahrens 		kmem_free(zn, sizeof (zil_dva_node_t));
113789Sahrens 
114789Sahrens 	avl_destroy(t);
115789Sahrens }
116789Sahrens 
117789Sahrens static int
118789Sahrens zil_dva_tree_add(avl_tree_t *t, dva_t *dva)
119789Sahrens {
120789Sahrens 	zil_dva_node_t *zn;
121789Sahrens 	avl_index_t where;
122789Sahrens 
123789Sahrens 	if (avl_find(t, dva, &where) != NULL)
124789Sahrens 		return (EEXIST);
125789Sahrens 
126789Sahrens 	zn = kmem_alloc(sizeof (zil_dva_node_t), KM_SLEEP);
127789Sahrens 	zn->zn_dva = *dva;
128789Sahrens 	avl_insert(t, zn, where);
129789Sahrens 
130789Sahrens 	return (0);
131789Sahrens }
132789Sahrens 
1331807Sbonwick static zil_header_t *
1341807Sbonwick zil_header_in_syncing_context(zilog_t *zilog)
1351807Sbonwick {
1361807Sbonwick 	return ((zil_header_t *)zilog->zl_header);
1371807Sbonwick }
1381807Sbonwick 
1391807Sbonwick static void
1401807Sbonwick zil_init_log_chain(zilog_t *zilog, blkptr_t *bp)
1411807Sbonwick {
1421807Sbonwick 	zio_cksum_t *zc = &bp->blk_cksum;
1431807Sbonwick 
1441807Sbonwick 	zc->zc_word[ZIL_ZC_GUID_0] = spa_get_random(-1ULL);
1451807Sbonwick 	zc->zc_word[ZIL_ZC_GUID_1] = spa_get_random(-1ULL);
1461807Sbonwick 	zc->zc_word[ZIL_ZC_OBJSET] = dmu_objset_id(zilog->zl_os);
1471807Sbonwick 	zc->zc_word[ZIL_ZC_SEQ] = 1ULL;
1481807Sbonwick }
1491807Sbonwick 
150789Sahrens /*
151789Sahrens  * Read a log block, make sure it's valid, and byteswap it if necessary.
152789Sahrens  */
153789Sahrens static int
1541807Sbonwick zil_read_log_block(zilog_t *zilog, const blkptr_t *bp, arc_buf_t **abufpp)
155789Sahrens {
1561807Sbonwick 	blkptr_t blk = *bp;
1571544Seschrock 	zbookmark_t zb;
1582391Smaybee 	uint32_t aflags = ARC_WAIT;
159789Sahrens 	int error;
160789Sahrens 
1611807Sbonwick 	zb.zb_objset = bp->blk_cksum.zc_word[ZIL_ZC_OBJSET];
1621544Seschrock 	zb.zb_object = 0;
1631544Seschrock 	zb.zb_level = -1;
1641807Sbonwick 	zb.zb_blkid = bp->blk_cksum.zc_word[ZIL_ZC_SEQ];
1651807Sbonwick 
1661807Sbonwick 	*abufpp = NULL;
1671807Sbonwick 
1687046Sahrens 	/*
1697046Sahrens 	 * We shouldn't be doing any scrubbing while we're doing log
1707046Sahrens 	 * replay, it's OK to not lock.
1717046Sahrens 	 */
1727046Sahrens 	error = arc_read_nolock(NULL, zilog->zl_spa, &blk,
1731807Sbonwick 	    arc_getbuf_func, abufpp, ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL |
1742391Smaybee 	    ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB, &aflags, &zb);
1751807Sbonwick 
1761807Sbonwick 	if (error == 0) {
1771807Sbonwick 		char *data = (*abufpp)->b_data;
1781807Sbonwick 		uint64_t blksz = BP_GET_LSIZE(bp);
1791807Sbonwick 		zil_trailer_t *ztp = (zil_trailer_t *)(data + blksz) - 1;
1801807Sbonwick 		zio_cksum_t cksum = bp->blk_cksum;
1811544Seschrock 
1821807Sbonwick 		/*
1837522SNeil.Perrin@Sun.COM 		 * Validate the checksummed log block.
1847522SNeil.Perrin@Sun.COM 		 *
1851807Sbonwick 		 * Sequence numbers should be... sequential.  The checksum
1861807Sbonwick 		 * verifier for the next block should be bp's checksum plus 1.
1877522SNeil.Perrin@Sun.COM 		 *
1887522SNeil.Perrin@Sun.COM 		 * Also check the log chain linkage and size used.
1891807Sbonwick 		 */
1901807Sbonwick 		cksum.zc_word[ZIL_ZC_SEQ]++;
1911807Sbonwick 
1927522SNeil.Perrin@Sun.COM 		if (bcmp(&cksum, &ztp->zit_next_blk.blk_cksum,
1937522SNeil.Perrin@Sun.COM 		    sizeof (cksum)) || BP_IS_HOLE(&ztp->zit_next_blk) ||
1947522SNeil.Perrin@Sun.COM 		    (ztp->zit_nused > (blksz - sizeof (zil_trailer_t)))) {
1957522SNeil.Perrin@Sun.COM 			error = ECKSUM;
1967522SNeil.Perrin@Sun.COM 		}
1971807Sbonwick 
1981807Sbonwick 		if (error) {
1991807Sbonwick 			VERIFY(arc_buf_remove_ref(*abufpp, abufpp) == 1);
2001807Sbonwick 			*abufpp = NULL;
2011807Sbonwick 		}
202789Sahrens 	}
203789Sahrens 
2041807Sbonwick 	dprintf("error %d on %llu:%llu\n", error, zb.zb_objset, zb.zb_blkid);
205789Sahrens 
2061807Sbonwick 	return (error);
207789Sahrens }
208789Sahrens 
209789Sahrens /*
210789Sahrens  * Parse the intent log, and call parse_func for each valid record within.
2111807Sbonwick  * Return the highest sequence number.
212789Sahrens  */
2131807Sbonwick uint64_t
214789Sahrens zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func,
215789Sahrens     zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg)
216789Sahrens {
2171807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
2181807Sbonwick 	uint64_t claim_seq = zh->zh_claim_seq;
2191807Sbonwick 	uint64_t seq = 0;
2201807Sbonwick 	uint64_t max_seq = 0;
2211807Sbonwick 	blkptr_t blk = zh->zh_log;
2221807Sbonwick 	arc_buf_t *abuf;
223789Sahrens 	char *lrbuf, *lrp;
224789Sahrens 	zil_trailer_t *ztp;
225789Sahrens 	int reclen, error;
226789Sahrens 
227789Sahrens 	if (BP_IS_HOLE(&blk))
2281807Sbonwick 		return (max_seq);
229789Sahrens 
230789Sahrens 	/*
231789Sahrens 	 * Starting at the block pointed to by zh_log we read the log chain.
232789Sahrens 	 * For each block in the chain we strongly check that block to
233789Sahrens 	 * ensure its validity.  We stop when an invalid block is found.
234789Sahrens 	 * For each block pointer in the chain we call parse_blk_func().
235789Sahrens 	 * For each record in each valid block we call parse_lr_func().
2361807Sbonwick 	 * If the log has been claimed, stop if we encounter a sequence
2371807Sbonwick 	 * number greater than the highest claimed sequence number.
238789Sahrens 	 */
239789Sahrens 	zil_dva_tree_init(&zilog->zl_dva_tree);
240789Sahrens 	for (;;) {
2411807Sbonwick 		seq = blk.blk_cksum.zc_word[ZIL_ZC_SEQ];
2421807Sbonwick 
2431807Sbonwick 		if (claim_seq != 0 && seq > claim_seq)
2441807Sbonwick 			break;
2451807Sbonwick 
2461807Sbonwick 		ASSERT(max_seq < seq);
2471807Sbonwick 		max_seq = seq;
2481807Sbonwick 
2491807Sbonwick 		error = zil_read_log_block(zilog, &blk, &abuf);
250789Sahrens 
251789Sahrens 		if (parse_blk_func != NULL)
252789Sahrens 			parse_blk_func(zilog, &blk, arg, txg);
253789Sahrens 
254789Sahrens 		if (error)
255789Sahrens 			break;
256789Sahrens 
2571807Sbonwick 		lrbuf = abuf->b_data;
258789Sahrens 		ztp = (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1;
259789Sahrens 		blk = ztp->zit_next_blk;
260789Sahrens 
2611807Sbonwick 		if (parse_lr_func == NULL) {
2621807Sbonwick 			VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
263789Sahrens 			continue;
2641807Sbonwick 		}
265789Sahrens 
266789Sahrens 		for (lrp = lrbuf; lrp < lrbuf + ztp->zit_nused; lrp += reclen) {
267789Sahrens 			lr_t *lr = (lr_t *)lrp;
268789Sahrens 			reclen = lr->lrc_reclen;
269789Sahrens 			ASSERT3U(reclen, >=, sizeof (lr_t));
270789Sahrens 			parse_lr_func(zilog, lr, arg, txg);
271789Sahrens 		}
2721807Sbonwick 		VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
273789Sahrens 	}
274789Sahrens 	zil_dva_tree_fini(&zilog->zl_dva_tree);
2751807Sbonwick 
2761807Sbonwick 	return (max_seq);
277789Sahrens }
278789Sahrens 
279789Sahrens /* ARGSUSED */
280789Sahrens static void
281789Sahrens zil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg)
282789Sahrens {
283789Sahrens 	spa_t *spa = zilog->zl_spa;
284789Sahrens 	int err;
285789Sahrens 
286789Sahrens 	/*
287789Sahrens 	 * Claim log block if not already committed and not already claimed.
288789Sahrens 	 */
289789Sahrens 	if (bp->blk_birth >= first_txg &&
290789Sahrens 	    zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp)) == 0) {
2917754SJeff.Bonwick@Sun.COM 		err = zio_wait(zio_claim(NULL, spa, first_txg, bp, NULL, NULL,
2927754SJeff.Bonwick@Sun.COM 		    ZIO_FLAG_MUSTSUCCEED));
293789Sahrens 		ASSERT(err == 0);
294789Sahrens 	}
295789Sahrens }
296789Sahrens 
297789Sahrens static void
298789Sahrens zil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg)
299789Sahrens {
300789Sahrens 	if (lrc->lrc_txtype == TX_WRITE) {
301789Sahrens 		lr_write_t *lr = (lr_write_t *)lrc;
302789Sahrens 		zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg);
303789Sahrens 	}
304789Sahrens }
305789Sahrens 
306789Sahrens /* ARGSUSED */
307789Sahrens static void
308789Sahrens zil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg)
309789Sahrens {
310789Sahrens 	zio_free_blk(zilog->zl_spa, bp, dmu_tx_get_txg(tx));
311789Sahrens }
312789Sahrens 
313789Sahrens static void
314789Sahrens zil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg)
315789Sahrens {
316789Sahrens 	/*
317789Sahrens 	 * If we previously claimed it, we need to free it.
318789Sahrens 	 */
319789Sahrens 	if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE) {
320789Sahrens 		lr_write_t *lr = (lr_write_t *)lrc;
321789Sahrens 		blkptr_t *bp = &lr->lr_blkptr;
322789Sahrens 		if (bp->blk_birth >= claim_txg &&
323789Sahrens 		    !zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp))) {
324789Sahrens 			(void) arc_free(NULL, zilog->zl_spa,
325789Sahrens 			    dmu_tx_get_txg(tx), bp, NULL, NULL, ARC_WAIT);
326789Sahrens 		}
327789Sahrens 	}
328789Sahrens }
329789Sahrens 
330789Sahrens /*
331789Sahrens  * Create an on-disk intent log.
332789Sahrens  */
333789Sahrens static void
334789Sahrens zil_create(zilog_t *zilog)
335789Sahrens {
3361807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
337789Sahrens 	lwb_t *lwb;
3381807Sbonwick 	uint64_t txg = 0;
3391807Sbonwick 	dmu_tx_t *tx = NULL;
340789Sahrens 	blkptr_t blk;
3411807Sbonwick 	int error = 0;
342789Sahrens 
343789Sahrens 	/*
3441807Sbonwick 	 * Wait for any previous destroy to complete.
345789Sahrens 	 */
3461807Sbonwick 	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
3471807Sbonwick 
3481807Sbonwick 	ASSERT(zh->zh_claim_txg == 0);
3491807Sbonwick 	ASSERT(zh->zh_replay_seq == 0);
3501807Sbonwick 
3511807Sbonwick 	blk = zh->zh_log;
352789Sahrens 
353789Sahrens 	/*
3548109SNeil.Perrin@Sun.COM 	 * If we don't already have an initial log block or we have one
3558109SNeil.Perrin@Sun.COM 	 * but it's the wrong endianness then allocate one.
356789Sahrens 	 */
3578109SNeil.Perrin@Sun.COM 	if (BP_IS_HOLE(&blk) || BP_SHOULD_BYTESWAP(&blk)) {
3581807Sbonwick 		tx = dmu_tx_create(zilog->zl_os);
3591807Sbonwick 		(void) dmu_tx_assign(tx, TXG_WAIT);
3601807Sbonwick 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
3611807Sbonwick 		txg = dmu_tx_get_txg(tx);
3621807Sbonwick 
3638109SNeil.Perrin@Sun.COM 		if (!BP_IS_HOLE(&blk)) {
3648109SNeil.Perrin@Sun.COM 			zio_free_blk(zilog->zl_spa, &blk, txg);
3658109SNeil.Perrin@Sun.COM 			BP_ZERO(&blk);
3668109SNeil.Perrin@Sun.COM 		}
3678109SNeil.Perrin@Sun.COM 
3683063Sperrin 		error = zio_alloc_blk(zilog->zl_spa, ZIL_MIN_BLKSZ, &blk,
3693063Sperrin 		    NULL, txg);
3701807Sbonwick 
3711807Sbonwick 		if (error == 0)
3721807Sbonwick 			zil_init_log_chain(zilog, &blk);
3731362Sperrin 	}
3741807Sbonwick 
3751807Sbonwick 	/*
3761807Sbonwick 	 * Allocate a log write buffer (lwb) for the first log block.
3771807Sbonwick 	 */
378789Sahrens 	if (error == 0) {
379789Sahrens 		lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP);
380789Sahrens 		lwb->lwb_zilog = zilog;
381789Sahrens 		lwb->lwb_blk = blk;
382789Sahrens 		lwb->lwb_nused = 0;
383789Sahrens 		lwb->lwb_sz = BP_GET_LSIZE(&lwb->lwb_blk);
384789Sahrens 		lwb->lwb_buf = zio_buf_alloc(lwb->lwb_sz);
385789Sahrens 		lwb->lwb_max_txg = txg;
3862237Smaybee 		lwb->lwb_zio = NULL;
3872237Smaybee 
388789Sahrens 		mutex_enter(&zilog->zl_lock);
389789Sahrens 		list_insert_tail(&zilog->zl_lwb_list, lwb);
390789Sahrens 		mutex_exit(&zilog->zl_lock);
391789Sahrens 	}
392789Sahrens 
3931807Sbonwick 	/*
3941807Sbonwick 	 * If we just allocated the first log block, commit our transaction
3951807Sbonwick 	 * and wait for zil_sync() to stuff the block poiner into zh_log.
3961807Sbonwick 	 * (zh is part of the MOS, so we cannot modify it in open context.)
3971807Sbonwick 	 */
3981807Sbonwick 	if (tx != NULL) {
3991807Sbonwick 		dmu_tx_commit(tx);
4001362Sperrin 		txg_wait_synced(zilog->zl_dmu_pool, txg);
4011807Sbonwick 	}
4021807Sbonwick 
4031807Sbonwick 	ASSERT(bcmp(&blk, &zh->zh_log, sizeof (blk)) == 0);
404789Sahrens }
405789Sahrens 
406789Sahrens /*
407789Sahrens  * In one tx, free all log blocks and clear the log header.
4081807Sbonwick  * If keep_first is set, then we're replaying a log with no content.
4091807Sbonwick  * We want to keep the first block, however, so that the first
4101807Sbonwick  * synchronous transaction doesn't require a txg_wait_synced()
4111807Sbonwick  * in zil_create().  We don't need to txg_wait_synced() here either
4121807Sbonwick  * when keep_first is set, because both zil_create() and zil_destroy()
4131807Sbonwick  * will wait for any in-progress destroys to complete.
414789Sahrens  */
415789Sahrens void
4161807Sbonwick zil_destroy(zilog_t *zilog, boolean_t keep_first)
417789Sahrens {
4181807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
4191807Sbonwick 	lwb_t *lwb;
420789Sahrens 	dmu_tx_t *tx;
421789Sahrens 	uint64_t txg;
422789Sahrens 
4231807Sbonwick 	/*
4241807Sbonwick 	 * Wait for any previous destroy to complete.
4251807Sbonwick 	 */
4261807Sbonwick 	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
427789Sahrens 
4281807Sbonwick 	if (BP_IS_HOLE(&zh->zh_log))
429789Sahrens 		return;
430789Sahrens 
431789Sahrens 	tx = dmu_tx_create(zilog->zl_os);
432789Sahrens 	(void) dmu_tx_assign(tx, TXG_WAIT);
433789Sahrens 	dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
434789Sahrens 	txg = dmu_tx_get_txg(tx);
435789Sahrens 
4361807Sbonwick 	mutex_enter(&zilog->zl_lock);
4371807Sbonwick 
4385223Sperrin 	/*
4395223Sperrin 	 * It is possible for the ZIL to get the previously mounted zilog
4405223Sperrin 	 * structure of the same dataset if quickly remounted and the dbuf
4415223Sperrin 	 * eviction has not completed. In this case we can see a non
4425223Sperrin 	 * empty lwb list and keep_first will be set. We fix this by
4435223Sperrin 	 * clearing the keep_first. This will be slower but it's very rare.
4445223Sperrin 	 */
4455223Sperrin 	if (!list_is_empty(&zilog->zl_lwb_list) && keep_first)
4465223Sperrin 		keep_first = B_FALSE;
4475223Sperrin 
4481807Sbonwick 	ASSERT3U(zilog->zl_destroy_txg, <, txg);
449789Sahrens 	zilog->zl_destroy_txg = txg;
4501807Sbonwick 	zilog->zl_keep_first = keep_first;
4511807Sbonwick 
4521807Sbonwick 	if (!list_is_empty(&zilog->zl_lwb_list)) {
4531807Sbonwick 		ASSERT(zh->zh_claim_txg == 0);
4541807Sbonwick 		ASSERT(!keep_first);
4551807Sbonwick 		while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
4561807Sbonwick 			list_remove(&zilog->zl_lwb_list, lwb);
4571807Sbonwick 			if (lwb->lwb_buf != NULL)
4581807Sbonwick 				zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
4591807Sbonwick 			zio_free_blk(zilog->zl_spa, &lwb->lwb_blk, txg);
4601807Sbonwick 			kmem_cache_free(zil_lwb_cache, lwb);
4611807Sbonwick 		}
4621807Sbonwick 	} else {
4631807Sbonwick 		if (!keep_first) {
4641807Sbonwick 			(void) zil_parse(zilog, zil_free_log_block,
4651807Sbonwick 			    zil_free_log_record, tx, zh->zh_claim_txg);
4661807Sbonwick 		}
4671807Sbonwick 	}
4682638Sperrin 	mutex_exit(&zilog->zl_lock);
469789Sahrens 
470789Sahrens 	dmu_tx_commit(tx);
471789Sahrens }
472789Sahrens 
4734935Sperrin /*
4744935Sperrin  * zil_rollback_destroy() is only called by the rollback code.
4754935Sperrin  * We already have a syncing tx. Rollback has exclusive access to the
4764935Sperrin  * dataset, so we don't have to worry about concurrent zil access.
4774935Sperrin  * The actual freeing of any log blocks occurs in zil_sync() later in
4784935Sperrin  * this txg syncing phase.
4794935Sperrin  */
4804935Sperrin void
4814935Sperrin zil_rollback_destroy(zilog_t *zilog, dmu_tx_t *tx)
4824935Sperrin {
4834935Sperrin 	const zil_header_t *zh = zilog->zl_header;
4844935Sperrin 	uint64_t txg;
4854935Sperrin 
4864935Sperrin 	if (BP_IS_HOLE(&zh->zh_log))
4874935Sperrin 		return;
4884935Sperrin 
4894935Sperrin 	txg = dmu_tx_get_txg(tx);
4904935Sperrin 	ASSERT3U(zilog->zl_destroy_txg, <, txg);
4914935Sperrin 	zilog->zl_destroy_txg = txg;
4924935Sperrin 	zilog->zl_keep_first = B_FALSE;
4934935Sperrin 
4945809Sperrin 	/*
4955809Sperrin 	 * Ensure there's no outstanding ZIL IO.  No lwbs or just the
4965809Sperrin 	 * unused one that allocated in advance is ok.
4975809Sperrin 	 */
4985809Sperrin 	ASSERT(zilog->zl_lwb_list.list_head.list_next ==
4995809Sperrin 	    zilog->zl_lwb_list.list_head.list_prev);
5004935Sperrin 	(void) zil_parse(zilog, zil_free_log_block, zil_free_log_record,
5014935Sperrin 	    tx, zh->zh_claim_txg);
5024935Sperrin }
5034935Sperrin 
5042199Sahrens int
505789Sahrens zil_claim(char *osname, void *txarg)
506789Sahrens {
507789Sahrens 	dmu_tx_t *tx = txarg;
508789Sahrens 	uint64_t first_txg = dmu_tx_get_txg(tx);
509789Sahrens 	zilog_t *zilog;
510789Sahrens 	zil_header_t *zh;
511789Sahrens 	objset_t *os;
512789Sahrens 	int error;
513789Sahrens 
5146689Smaybee 	error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_USER, &os);
515789Sahrens 	if (error) {
5167294Sperrin 		cmn_err(CE_WARN, "can't open objset for %s", osname);
5172199Sahrens 		return (0);
518789Sahrens 	}
519789Sahrens 
520789Sahrens 	zilog = dmu_objset_zil(os);
5211807Sbonwick 	zh = zil_header_in_syncing_context(zilog);
522789Sahrens 
523789Sahrens 	/*
5241807Sbonwick 	 * Claim all log blocks if we haven't already done so, and remember
5251807Sbonwick 	 * the highest claimed sequence number.  This ensures that if we can
5261807Sbonwick 	 * read only part of the log now (e.g. due to a missing device),
5271807Sbonwick 	 * but we can read the entire log later, we will not try to replay
5281807Sbonwick 	 * or destroy beyond the last block we successfully claimed.
529789Sahrens 	 */
530789Sahrens 	ASSERT3U(zh->zh_claim_txg, <=, first_txg);
531789Sahrens 	if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) {
532789Sahrens 		zh->zh_claim_txg = first_txg;
5331807Sbonwick 		zh->zh_claim_seq = zil_parse(zilog, zil_claim_log_block,
5341807Sbonwick 		    zil_claim_log_record, tx, first_txg);
535789Sahrens 		dsl_dataset_dirty(dmu_objset_ds(os), tx);
536789Sahrens 	}
5371807Sbonwick 
538789Sahrens 	ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1));
539789Sahrens 	dmu_objset_close(os);
5402199Sahrens 	return (0);
541789Sahrens }
542789Sahrens 
5437294Sperrin /*
5447294Sperrin  * Check the log by walking the log chain.
5457294Sperrin  * Checksum errors are ok as they indicate the end of the chain.
5467294Sperrin  * Any other error (no device or read failure) returns an error.
5477294Sperrin  */
5487294Sperrin /* ARGSUSED */
5497294Sperrin int
5507294Sperrin zil_check_log_chain(char *osname, void *txarg)
5517294Sperrin {
5527294Sperrin 	zilog_t *zilog;
5537294Sperrin 	zil_header_t *zh;
5547294Sperrin 	blkptr_t blk;
5557294Sperrin 	arc_buf_t *abuf;
5567294Sperrin 	objset_t *os;
5577294Sperrin 	char *lrbuf;
5587294Sperrin 	zil_trailer_t *ztp;
5597294Sperrin 	int error;
5607294Sperrin 
5617294Sperrin 	error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_USER, &os);
5627294Sperrin 	if (error) {
5637294Sperrin 		cmn_err(CE_WARN, "can't open objset for %s", osname);
5647294Sperrin 		return (0);
5657294Sperrin 	}
5667294Sperrin 
5677294Sperrin 	zilog = dmu_objset_zil(os);
5687294Sperrin 	zh = zil_header_in_syncing_context(zilog);
5697294Sperrin 	blk = zh->zh_log;
5707294Sperrin 	if (BP_IS_HOLE(&blk)) {
5717294Sperrin 		dmu_objset_close(os);
5727294Sperrin 		return (0); /* no chain */
5737294Sperrin 	}
5747294Sperrin 
5757294Sperrin 	for (;;) {
5767294Sperrin 		error = zil_read_log_block(zilog, &blk, &abuf);
5777294Sperrin 		if (error)
5787294Sperrin 			break;
5797294Sperrin 		lrbuf = abuf->b_data;
5807294Sperrin 		ztp = (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1;
5817294Sperrin 		blk = ztp->zit_next_blk;
5827294Sperrin 		VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
5837294Sperrin 	}
5847294Sperrin 	dmu_objset_close(os);
5857294Sperrin 	if (error == ECKSUM)
5867294Sperrin 		return (0); /* normal end of chain */
5877294Sperrin 	return (error);
5887294Sperrin }
5897294Sperrin 
5907294Sperrin /*
5917294Sperrin  * Clear a log chain
5927294Sperrin  */
5937294Sperrin /* ARGSUSED */
5947294Sperrin int
5957294Sperrin zil_clear_log_chain(char *osname, void *txarg)
5967294Sperrin {
5977294Sperrin 	zilog_t *zilog;
5987294Sperrin 	zil_header_t *zh;
5997294Sperrin 	objset_t *os;
6007294Sperrin 	dmu_tx_t *tx;
6017294Sperrin 	int error;
6027294Sperrin 
6037294Sperrin 	error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_USER, &os);
6047294Sperrin 	if (error) {
6057294Sperrin 		cmn_err(CE_WARN, "can't open objset for %s", osname);
6067294Sperrin 		return (0);
6077294Sperrin 	}
6087294Sperrin 
6097294Sperrin 	zilog = dmu_objset_zil(os);
6107294Sperrin 	tx = dmu_tx_create(zilog->zl_os);
6117294Sperrin 	(void) dmu_tx_assign(tx, TXG_WAIT);
6127294Sperrin 	zh = zil_header_in_syncing_context(zilog);
6137294Sperrin 	BP_ZERO(&zh->zh_log);
6147294Sperrin 	dsl_dataset_dirty(dmu_objset_ds(os), tx);
6157294Sperrin 	dmu_tx_commit(tx);
6167294Sperrin 	dmu_objset_close(os);
6177294Sperrin 	return (0);
6187294Sperrin }
6197294Sperrin 
6205688Sbonwick static int
6215688Sbonwick zil_vdev_compare(const void *x1, const void *x2)
622789Sahrens {
6235875Sperrin 	uint64_t v1 = ((zil_vdev_node_t *)x1)->zv_vdev;
6245875Sperrin 	uint64_t v2 = ((zil_vdev_node_t *)x2)->zv_vdev;
6255688Sbonwick 
6265688Sbonwick 	if (v1 < v2)
6275688Sbonwick 		return (-1);
6285688Sbonwick 	if (v1 > v2)
6295688Sbonwick 		return (1);
6305688Sbonwick 
6315688Sbonwick 	return (0);
6325688Sbonwick }
6335688Sbonwick 
6345688Sbonwick void
6355688Sbonwick zil_add_block(zilog_t *zilog, blkptr_t *bp)
6365688Sbonwick {
6375688Sbonwick 	avl_tree_t *t = &zilog->zl_vdev_tree;
6385688Sbonwick 	avl_index_t where;
6395688Sbonwick 	zil_vdev_node_t *zv, zvsearch;
6405688Sbonwick 	int ndvas = BP_GET_NDVAS(bp);
6415688Sbonwick 	int i;
642789Sahrens 
6432986Sek110237 	if (zfs_nocacheflush)
644789Sahrens 		return;
645789Sahrens 
6465688Sbonwick 	ASSERT(zilog->zl_writer);
6475688Sbonwick 
6485688Sbonwick 	/*
6495688Sbonwick 	 * Even though we're zl_writer, we still need a lock because the
6505688Sbonwick 	 * zl_get_data() callbacks may have dmu_sync() done callbacks
6515688Sbonwick 	 * that will run concurrently.
6525688Sbonwick 	 */
6535688Sbonwick 	mutex_enter(&zilog->zl_vdev_lock);
6545688Sbonwick 	for (i = 0; i < ndvas; i++) {
6555688Sbonwick 		zvsearch.zv_vdev = DVA_GET_VDEV(&bp->blk_dva[i]);
6565688Sbonwick 		if (avl_find(t, &zvsearch, &where) == NULL) {
6575688Sbonwick 			zv = kmem_alloc(sizeof (*zv), KM_SLEEP);
6585688Sbonwick 			zv->zv_vdev = zvsearch.zv_vdev;
6595688Sbonwick 			avl_insert(t, zv, where);
6603063Sperrin 		}
6613063Sperrin 	}
6625688Sbonwick 	mutex_exit(&zilog->zl_vdev_lock);
6633063Sperrin }
6643063Sperrin 
665789Sahrens void
6662638Sperrin zil_flush_vdevs(zilog_t *zilog)
667789Sahrens {
6683063Sperrin 	spa_t *spa = zilog->zl_spa;
6695688Sbonwick 	avl_tree_t *t = &zilog->zl_vdev_tree;
6705688Sbonwick 	void *cookie = NULL;
6715688Sbonwick 	zil_vdev_node_t *zv;
6725688Sbonwick 	zio_t *zio;
6733063Sperrin 
6743063Sperrin 	ASSERT(zilog->zl_writer);
675789Sahrens 
6765688Sbonwick 	/*
6775688Sbonwick 	 * We don't need zl_vdev_lock here because we're the zl_writer,
6785688Sbonwick 	 * and all zl_get_data() callbacks are done.
6795688Sbonwick 	 */
6805688Sbonwick 	if (avl_numnodes(t) == 0)
6815688Sbonwick 		return;
6825688Sbonwick 
6837754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6845688Sbonwick 
6857754SJeff.Bonwick@Sun.COM 	zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
6865688Sbonwick 
6875688Sbonwick 	while ((zv = avl_destroy_nodes(t, &cookie)) != NULL) {
6885688Sbonwick 		vdev_t *vd = vdev_lookup_top(spa, zv->zv_vdev);
6895688Sbonwick 		if (vd != NULL)
6905688Sbonwick 			zio_flush(zio, vd);
6915688Sbonwick 		kmem_free(zv, sizeof (*zv));
6923063Sperrin 	}
693789Sahrens 
694789Sahrens 	/*
695789Sahrens 	 * Wait for all the flushes to complete.  Not all devices actually
696789Sahrens 	 * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails.
697789Sahrens 	 */
6985688Sbonwick 	(void) zio_wait(zio);
6995688Sbonwick 
7007754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, FTAG);
701789Sahrens }
702789Sahrens 
703789Sahrens /*
704789Sahrens  * Function called when a log block write completes
705789Sahrens  */
706789Sahrens static void
707789Sahrens zil_lwb_write_done(zio_t *zio)
708789Sahrens {
709789Sahrens 	lwb_t *lwb = zio->io_private;
710789Sahrens 	zilog_t *zilog = lwb->lwb_zilog;
711789Sahrens 
7127754SJeff.Bonwick@Sun.COM 	ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF);
7137754SJeff.Bonwick@Sun.COM 	ASSERT(BP_GET_CHECKSUM(zio->io_bp) == ZIO_CHECKSUM_ZILOG);
7147754SJeff.Bonwick@Sun.COM 	ASSERT(BP_GET_TYPE(zio->io_bp) == DMU_OT_INTENT_LOG);
7157754SJeff.Bonwick@Sun.COM 	ASSERT(BP_GET_LEVEL(zio->io_bp) == 0);
7167754SJeff.Bonwick@Sun.COM 	ASSERT(BP_GET_BYTEORDER(zio->io_bp) == ZFS_HOST_BYTEORDER);
7177754SJeff.Bonwick@Sun.COM 	ASSERT(!BP_IS_GANG(zio->io_bp));
7187754SJeff.Bonwick@Sun.COM 	ASSERT(!BP_IS_HOLE(zio->io_bp));
7197754SJeff.Bonwick@Sun.COM 	ASSERT(zio->io_bp->blk_fill == 0);
7207754SJeff.Bonwick@Sun.COM 
721789Sahrens 	/*
722789Sahrens 	 * Now that we've written this log block, we have a stable pointer
723789Sahrens 	 * to the next block in the chain, so it's OK to let the txg in
724789Sahrens 	 * which we allocated the next block sync.
725789Sahrens 	 */
726789Sahrens 	txg_rele_to_sync(&lwb->lwb_txgh);
727789Sahrens 
728789Sahrens 	zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
729789Sahrens 	mutex_enter(&zilog->zl_lock);
730789Sahrens 	lwb->lwb_buf = NULL;
7314527Sperrin 	if (zio->io_error)
732789Sahrens 		zilog->zl_log_error = B_TRUE;
733789Sahrens 	mutex_exit(&zilog->zl_lock);
734789Sahrens }
735789Sahrens 
736789Sahrens /*
7372237Smaybee  * Initialize the io for a log block.
7382237Smaybee  */
7392237Smaybee static void
7402237Smaybee zil_lwb_write_init(zilog_t *zilog, lwb_t *lwb)
7412237Smaybee {
7422237Smaybee 	zbookmark_t zb;
7432237Smaybee 
7442237Smaybee 	zb.zb_objset = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET];
7452237Smaybee 	zb.zb_object = 0;
7462237Smaybee 	zb.zb_level = -1;
7472237Smaybee 	zb.zb_blkid = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ];
7482237Smaybee 
7492638Sperrin 	if (zilog->zl_root_zio == NULL) {
7502638Sperrin 		zilog->zl_root_zio = zio_root(zilog->zl_spa, NULL, NULL,
7512638Sperrin 		    ZIO_FLAG_CANFAIL);
7522638Sperrin 	}
7533063Sperrin 	if (lwb->lwb_zio == NULL) {
7543063Sperrin 		lwb->lwb_zio = zio_rewrite(zilog->zl_root_zio, zilog->zl_spa,
7557754SJeff.Bonwick@Sun.COM 		    0, &lwb->lwb_blk, lwb->lwb_buf,
7563063Sperrin 		    lwb->lwb_sz, zil_lwb_write_done, lwb,
7574527Sperrin 		    ZIO_PRIORITY_LOG_WRITE, ZIO_FLAG_CANFAIL, &zb);
7583063Sperrin 	}
7592237Smaybee }
7602237Smaybee 
7612237Smaybee /*
762789Sahrens  * Start a log block write and advance to the next log block.
763789Sahrens  * Calls are serialized.
764789Sahrens  */
765789Sahrens static lwb_t *
766789Sahrens zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb)
767789Sahrens {
768789Sahrens 	lwb_t *nlwb;
769789Sahrens 	zil_trailer_t *ztp = (zil_trailer_t *)(lwb->lwb_buf + lwb->lwb_sz) - 1;
7701807Sbonwick 	spa_t *spa = zilog->zl_spa;
7711807Sbonwick 	blkptr_t *bp = &ztp->zit_next_blk;
772789Sahrens 	uint64_t txg;
773789Sahrens 	uint64_t zil_blksz;
774789Sahrens 	int error;
775789Sahrens 
776789Sahrens 	ASSERT(lwb->lwb_nused <= ZIL_BLK_DATA_SZ(lwb));
777789Sahrens 
778789Sahrens 	/*
779789Sahrens 	 * Allocate the next block and save its address in this block
780789Sahrens 	 * before writing it in order to establish the log chain.
781789Sahrens 	 * Note that if the allocation of nlwb synced before we wrote
782789Sahrens 	 * the block that points at it (lwb), we'd leak it if we crashed.
783789Sahrens 	 * Therefore, we don't do txg_rele_to_sync() until zil_lwb_write_done().
784789Sahrens 	 */
785789Sahrens 	txg = txg_hold_open(zilog->zl_dmu_pool, &lwb->lwb_txgh);
786789Sahrens 	txg_rele_to_quiesce(&lwb->lwb_txgh);
787789Sahrens 
788789Sahrens 	/*
7891141Sperrin 	 * Pick a ZIL blocksize. We request a size that is the
7901141Sperrin 	 * maximum of the previous used size, the current used size and
7911141Sperrin 	 * the amount waiting in the queue.
792789Sahrens 	 */
7932237Smaybee 	zil_blksz = MAX(zilog->zl_prev_used,
7942237Smaybee 	    zilog->zl_cur_used + sizeof (*ztp));
7951141Sperrin 	zil_blksz = MAX(zil_blksz, zilog->zl_itx_list_sz + sizeof (*ztp));
7961842Sperrin 	zil_blksz = P2ROUNDUP_TYPED(zil_blksz, ZIL_MIN_BLKSZ, uint64_t);
7971141Sperrin 	if (zil_blksz > ZIL_MAX_BLKSZ)
7981141Sperrin 		zil_blksz = ZIL_MAX_BLKSZ;
799789Sahrens 
8003063Sperrin 	BP_ZERO(bp);
8013063Sperrin 	/* pass the old blkptr in order to spread log blocks across devs */
8023063Sperrin 	error = zio_alloc_blk(spa, zil_blksz, bp, &lwb->lwb_blk, txg);
803789Sahrens 	if (error) {
8043668Sgw25295 		dmu_tx_t *tx = dmu_tx_create_assigned(zilog->zl_dmu_pool, txg);
8053668Sgw25295 
8061544Seschrock 		/*
8073668Sgw25295 		 * We dirty the dataset to ensure that zil_sync() will
8083668Sgw25295 		 * be called to remove this lwb from our zl_lwb_list.
8093668Sgw25295 		 * Failing to do so, may leave an lwb with a NULL lwb_buf
8103668Sgw25295 		 * hanging around on the zl_lwb_list.
8113668Sgw25295 		 */
8123668Sgw25295 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
8133848Sgw25295 		dmu_tx_commit(tx);
8143668Sgw25295 
8153668Sgw25295 		/*
8163668Sgw25295 		 * Since we've just experienced an allocation failure so we
8173668Sgw25295 		 * terminate the current lwb and send it on its way.
8183668Sgw25295 		 */
8193668Sgw25295 		ztp->zit_pad = 0;
8203668Sgw25295 		ztp->zit_nused = lwb->lwb_nused;
8213668Sgw25295 		ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum;
8223668Sgw25295 		zio_nowait(lwb->lwb_zio);
8233668Sgw25295 
8243668Sgw25295 		/*
8251544Seschrock 		 * By returning NULL the caller will call tx_wait_synced()
8261544Seschrock 		 */
827789Sahrens 		return (NULL);
828789Sahrens 	}
829789Sahrens 
8301807Sbonwick 	ASSERT3U(bp->blk_birth, ==, txg);
8311544Seschrock 	ztp->zit_pad = 0;
832789Sahrens 	ztp->zit_nused = lwb->lwb_nused;
833789Sahrens 	ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum;
8341807Sbonwick 	bp->blk_cksum = lwb->lwb_blk.blk_cksum;
8351807Sbonwick 	bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++;
836789Sahrens 
837789Sahrens 	/*
838789Sahrens 	 * Allocate a new log write buffer (lwb).
839789Sahrens 	 */
840789Sahrens 	nlwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP);
841789Sahrens 
842789Sahrens 	nlwb->lwb_zilog = zilog;
8431807Sbonwick 	nlwb->lwb_blk = *bp;
844789Sahrens 	nlwb->lwb_nused = 0;
845789Sahrens 	nlwb->lwb_sz = BP_GET_LSIZE(&nlwb->lwb_blk);
846789Sahrens 	nlwb->lwb_buf = zio_buf_alloc(nlwb->lwb_sz);
847789Sahrens 	nlwb->lwb_max_txg = txg;
8482237Smaybee 	nlwb->lwb_zio = NULL;
849789Sahrens 
850789Sahrens 	/*
8513063Sperrin 	 * Put new lwb at the end of the log chain
852789Sahrens 	 */
853789Sahrens 	mutex_enter(&zilog->zl_lock);
854789Sahrens 	list_insert_tail(&zilog->zl_lwb_list, nlwb);
8553063Sperrin 	mutex_exit(&zilog->zl_lock);
8563063Sperrin 
8575688Sbonwick 	/* Record the block for later vdev flushing */
8585688Sbonwick 	zil_add_block(zilog, &lwb->lwb_blk);
859789Sahrens 
860789Sahrens 	/*
8612237Smaybee 	 * kick off the write for the old log block
862789Sahrens 	 */
8632237Smaybee 	dprintf_bp(&lwb->lwb_blk, "lwb %p txg %llu: ", lwb, txg);
8643063Sperrin 	ASSERT(lwb->lwb_zio);
8652237Smaybee 	zio_nowait(lwb->lwb_zio);
866789Sahrens 
867789Sahrens 	return (nlwb);
868789Sahrens }
869789Sahrens 
870789Sahrens static lwb_t *
871789Sahrens zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb)
872789Sahrens {
873789Sahrens 	lr_t *lrc = &itx->itx_lr; /* common log record */
8742237Smaybee 	lr_write_t *lr = (lr_write_t *)lrc;
875789Sahrens 	uint64_t txg = lrc->lrc_txg;
876789Sahrens 	uint64_t reclen = lrc->lrc_reclen;
8772237Smaybee 	uint64_t dlen;
878789Sahrens 
879789Sahrens 	if (lwb == NULL)
880789Sahrens 		return (NULL);
881789Sahrens 	ASSERT(lwb->lwb_buf != NULL);
882789Sahrens 
8832237Smaybee 	if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY)
8842237Smaybee 		dlen = P2ROUNDUP_TYPED(
8852237Smaybee 		    lr->lr_length, sizeof (uint64_t), uint64_t);
8862237Smaybee 	else
8872237Smaybee 		dlen = 0;
8881669Sperrin 
8891669Sperrin 	zilog->zl_cur_used += (reclen + dlen);
8901669Sperrin 
8913063Sperrin 	zil_lwb_write_init(zilog, lwb);
8923063Sperrin 
8931669Sperrin 	/*
8941669Sperrin 	 * If this record won't fit in the current log block, start a new one.
8951669Sperrin 	 */
8961669Sperrin 	if (lwb->lwb_nused + reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) {
8971669Sperrin 		lwb = zil_lwb_write_start(zilog, lwb);
8982237Smaybee 		if (lwb == NULL)
8991669Sperrin 			return (NULL);
9003063Sperrin 		zil_lwb_write_init(zilog, lwb);
9011669Sperrin 		ASSERT(lwb->lwb_nused == 0);
9021669Sperrin 		if (reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) {
9031669Sperrin 			txg_wait_synced(zilog->zl_dmu_pool, txg);
904789Sahrens 			return (lwb);
905789Sahrens 		}
906789Sahrens 	}
907789Sahrens 
9082638Sperrin 	/*
9092638Sperrin 	 * Update the lrc_seq, to be log record sequence number. See zil.h
9102638Sperrin 	 * Then copy the record to the log buffer.
9112638Sperrin 	 */
9122638Sperrin 	lrc->lrc_seq = ++zilog->zl_lr_seq; /* we are single threaded */
913789Sahrens 	bcopy(lrc, lwb->lwb_buf + lwb->lwb_nused, reclen);
9142237Smaybee 
9152237Smaybee 	/*
9162237Smaybee 	 * If it's a write, fetch the data or get its blkptr as appropriate.
9172237Smaybee 	 */
9182237Smaybee 	if (lrc->lrc_txtype == TX_WRITE) {
9192237Smaybee 		if (txg > spa_freeze_txg(zilog->zl_spa))
9202237Smaybee 			txg_wait_synced(zilog->zl_dmu_pool, txg);
9212237Smaybee 		if (itx->itx_wr_state != WR_COPIED) {
9222237Smaybee 			char *dbuf;
9232237Smaybee 			int error;
9242237Smaybee 
9252237Smaybee 			/* alignment is guaranteed */
9262237Smaybee 			lr = (lr_write_t *)(lwb->lwb_buf + lwb->lwb_nused);
9272237Smaybee 			if (dlen) {
9282237Smaybee 				ASSERT(itx->itx_wr_state == WR_NEED_COPY);
9292237Smaybee 				dbuf = lwb->lwb_buf + lwb->lwb_nused + reclen;
9302237Smaybee 				lr->lr_common.lrc_reclen += dlen;
9312237Smaybee 			} else {
9322237Smaybee 				ASSERT(itx->itx_wr_state == WR_INDIRECT);
9332237Smaybee 				dbuf = NULL;
9342237Smaybee 			}
9352237Smaybee 			error = zilog->zl_get_data(
9362237Smaybee 			    itx->itx_private, lr, dbuf, lwb->lwb_zio);
9372237Smaybee 			if (error) {
9382237Smaybee 				ASSERT(error == ENOENT || error == EEXIST ||
9392237Smaybee 				    error == EALREADY);
9402237Smaybee 				return (lwb);
9412237Smaybee 			}
9422237Smaybee 		}
9431669Sperrin 	}
9442237Smaybee 
9452237Smaybee 	lwb->lwb_nused += reclen + dlen;
946789Sahrens 	lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg);
947789Sahrens 	ASSERT3U(lwb->lwb_nused, <=, ZIL_BLK_DATA_SZ(lwb));
948789Sahrens 	ASSERT3U(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)), ==, 0);
949789Sahrens 
950789Sahrens 	return (lwb);
951789Sahrens }
952789Sahrens 
953789Sahrens itx_t *
9545331Samw zil_itx_create(uint64_t txtype, size_t lrsize)
955789Sahrens {
956789Sahrens 	itx_t *itx;
957789Sahrens 
9581842Sperrin 	lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t);
959789Sahrens 
960789Sahrens 	itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP);
961789Sahrens 	itx->itx_lr.lrc_txtype = txtype;
962789Sahrens 	itx->itx_lr.lrc_reclen = lrsize;
9636101Sperrin 	itx->itx_sod = lrsize; /* if write & WR_NEED_COPY will be increased */
964789Sahrens 	itx->itx_lr.lrc_seq = 0;	/* defensive */
965789Sahrens 
966789Sahrens 	return (itx);
967789Sahrens }
968789Sahrens 
969789Sahrens uint64_t
970789Sahrens zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx)
971789Sahrens {
972789Sahrens 	uint64_t seq;
973789Sahrens 
974789Sahrens 	ASSERT(itx->itx_lr.lrc_seq == 0);
975789Sahrens 
976789Sahrens 	mutex_enter(&zilog->zl_lock);
977789Sahrens 	list_insert_tail(&zilog->zl_itx_list, itx);
9786101Sperrin 	zilog->zl_itx_list_sz += itx->itx_sod;
979789Sahrens 	itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx);
980789Sahrens 	itx->itx_lr.lrc_seq = seq = ++zilog->zl_itx_seq;
981789Sahrens 	mutex_exit(&zilog->zl_lock);
982789Sahrens 
983789Sahrens 	return (seq);
984789Sahrens }
985789Sahrens 
986789Sahrens /*
987789Sahrens  * Free up all in-memory intent log transactions that have now been synced.
988789Sahrens  */
989789Sahrens static void
990789Sahrens zil_itx_clean(zilog_t *zilog)
991789Sahrens {
992789Sahrens 	uint64_t synced_txg = spa_last_synced_txg(zilog->zl_spa);
993789Sahrens 	uint64_t freeze_txg = spa_freeze_txg(zilog->zl_spa);
9943778Sjohansen 	list_t clean_list;
995789Sahrens 	itx_t *itx;
996789Sahrens 
9973778Sjohansen 	list_create(&clean_list, sizeof (itx_t), offsetof(itx_t, itx_node));
9983778Sjohansen 
999789Sahrens 	mutex_enter(&zilog->zl_lock);
10002638Sperrin 	/* wait for a log writer to finish walking list */
10012638Sperrin 	while (zilog->zl_writer) {
10022638Sperrin 		cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
10032638Sperrin 	}
10043778Sjohansen 
10053778Sjohansen 	/*
10063778Sjohansen 	 * Move the sync'd log transactions to a separate list so we can call
10073778Sjohansen 	 * kmem_free without holding the zl_lock.
10083778Sjohansen 	 *
10093778Sjohansen 	 * There is no need to set zl_writer as we don't drop zl_lock here
10103778Sjohansen 	 */
1011789Sahrens 	while ((itx = list_head(&zilog->zl_itx_list)) != NULL &&
1012789Sahrens 	    itx->itx_lr.lrc_txg <= MIN(synced_txg, freeze_txg)) {
1013789Sahrens 		list_remove(&zilog->zl_itx_list, itx);
10146101Sperrin 		zilog->zl_itx_list_sz -= itx->itx_sod;
10153778Sjohansen 		list_insert_tail(&clean_list, itx);
10163778Sjohansen 	}
10173778Sjohansen 	cv_broadcast(&zilog->zl_cv_writer);
10183778Sjohansen 	mutex_exit(&zilog->zl_lock);
10193778Sjohansen 
10203778Sjohansen 	/* destroy sync'd log transactions */
10213778Sjohansen 	while ((itx = list_head(&clean_list)) != NULL) {
10223778Sjohansen 		list_remove(&clean_list, itx);
1023789Sahrens 		kmem_free(itx, offsetof(itx_t, itx_lr)
1024789Sahrens 		    + itx->itx_lr.lrc_reclen);
1025789Sahrens 	}
10263778Sjohansen 	list_destroy(&clean_list);
1027789Sahrens }
1028789Sahrens 
10292638Sperrin /*
10303063Sperrin  * If there are any in-memory intent log transactions which have now been
10313063Sperrin  * synced then start up a taskq to free them.
10322638Sperrin  */
1033789Sahrens void
1034789Sahrens zil_clean(zilog_t *zilog)
1035789Sahrens {
10363063Sperrin 	itx_t *itx;
10373063Sperrin 
1038789Sahrens 	mutex_enter(&zilog->zl_lock);
10393063Sperrin 	itx = list_head(&zilog->zl_itx_list);
10403063Sperrin 	if ((itx != NULL) &&
10413063Sperrin 	    (itx->itx_lr.lrc_txg <= spa_last_synced_txg(zilog->zl_spa))) {
1042789Sahrens 		(void) taskq_dispatch(zilog->zl_clean_taskq,
1043789Sahrens 		    (void (*)(void *))zil_itx_clean, zilog, TQ_NOSLEEP);
10443063Sperrin 	}
1045789Sahrens 	mutex_exit(&zilog->zl_lock);
1046789Sahrens }
1047789Sahrens 
10487754SJeff.Bonwick@Sun.COM static void
10492638Sperrin zil_commit_writer(zilog_t *zilog, uint64_t seq, uint64_t foid)
1050789Sahrens {
1051789Sahrens 	uint64_t txg;
10523063Sperrin 	uint64_t commit_seq = 0;
10532638Sperrin 	itx_t *itx, *itx_next = (itx_t *)-1;
1054789Sahrens 	lwb_t *lwb;
1055789Sahrens 	spa_t *spa;
1056789Sahrens 
10572638Sperrin 	zilog->zl_writer = B_TRUE;
10587754SJeff.Bonwick@Sun.COM 	ASSERT(zilog->zl_root_zio == NULL);
1059789Sahrens 	spa = zilog->zl_spa;
1060789Sahrens 
1061789Sahrens 	if (zilog->zl_suspend) {
1062789Sahrens 		lwb = NULL;
1063789Sahrens 	} else {
1064789Sahrens 		lwb = list_tail(&zilog->zl_lwb_list);
1065789Sahrens 		if (lwb == NULL) {
10662638Sperrin 			/*
10672638Sperrin 			 * Return if there's nothing to flush before we
10682638Sperrin 			 * dirty the fs by calling zil_create()
10692638Sperrin 			 */
10702638Sperrin 			if (list_is_empty(&zilog->zl_itx_list)) {
10712638Sperrin 				zilog->zl_writer = B_FALSE;
10722638Sperrin 				return;
10732638Sperrin 			}
1074789Sahrens 			mutex_exit(&zilog->zl_lock);
1075789Sahrens 			zil_create(zilog);
1076789Sahrens 			mutex_enter(&zilog->zl_lock);
1077789Sahrens 			lwb = list_tail(&zilog->zl_lwb_list);
1078789Sahrens 		}
1079789Sahrens 	}
1080789Sahrens 
10813063Sperrin 	/* Loop through in-memory log transactions filling log blocks. */
10822638Sperrin 	DTRACE_PROBE1(zil__cw1, zilog_t *, zilog);
1083789Sahrens 	for (;;) {
10842638Sperrin 		/*
10852638Sperrin 		 * Find the next itx to push:
10862638Sperrin 		 * Push all transactions related to specified foid and all
10872638Sperrin 		 * other transactions except TX_WRITE, TX_TRUNCATE,
10882638Sperrin 		 * TX_SETATTR and TX_ACL for all other files.
10892638Sperrin 		 */
10902638Sperrin 		if (itx_next != (itx_t *)-1)
10912638Sperrin 			itx = itx_next;
10922638Sperrin 		else
10932638Sperrin 			itx = list_head(&zilog->zl_itx_list);
10942638Sperrin 		for (; itx != NULL; itx = list_next(&zilog->zl_itx_list, itx)) {
10952638Sperrin 			if (foid == 0) /* push all foids? */
10962638Sperrin 				break;
10973063Sperrin 			if (itx->itx_sync) /* push all O_[D]SYNC */
10983063Sperrin 				break;
10992638Sperrin 			switch (itx->itx_lr.lrc_txtype) {
11002638Sperrin 			case TX_SETATTR:
11012638Sperrin 			case TX_WRITE:
11022638Sperrin 			case TX_TRUNCATE:
11032638Sperrin 			case TX_ACL:
11042638Sperrin 				/* lr_foid is same offset for these records */
11052638Sperrin 				if (((lr_write_t *)&itx->itx_lr)->lr_foid
11062638Sperrin 				    != foid) {
11072638Sperrin 					continue; /* skip this record */
11082638Sperrin 				}
11092638Sperrin 			}
11102638Sperrin 			break;
11112638Sperrin 		}
1112789Sahrens 		if (itx == NULL)
1113789Sahrens 			break;
1114789Sahrens 
1115789Sahrens 		if ((itx->itx_lr.lrc_seq > seq) &&
11162638Sperrin 		    ((lwb == NULL) || (lwb->lwb_nused == 0) ||
11176101Sperrin 		    (lwb->lwb_nused + itx->itx_sod > ZIL_BLK_DATA_SZ(lwb)))) {
1118789Sahrens 			break;
11193063Sperrin 		}
1120789Sahrens 
11212638Sperrin 		/*
11222638Sperrin 		 * Save the next pointer.  Even though we soon drop
11232638Sperrin 		 * zl_lock all threads that may change the list
11242638Sperrin 		 * (another writer or zil_itx_clean) can't do so until
11252638Sperrin 		 * they have zl_writer.
11262638Sperrin 		 */
11272638Sperrin 		itx_next = list_next(&zilog->zl_itx_list, itx);
1128789Sahrens 		list_remove(&zilog->zl_itx_list, itx);
11296101Sperrin 		zilog->zl_itx_list_sz -= itx->itx_sod;
11303063Sperrin 		mutex_exit(&zilog->zl_lock);
1131789Sahrens 		txg = itx->itx_lr.lrc_txg;
1132789Sahrens 		ASSERT(txg);
1133789Sahrens 
1134789Sahrens 		if (txg > spa_last_synced_txg(spa) ||
1135789Sahrens 		    txg > spa_freeze_txg(spa))
1136789Sahrens 			lwb = zil_lwb_commit(zilog, itx, lwb);
1137789Sahrens 		kmem_free(itx, offsetof(itx_t, itx_lr)
1138789Sahrens 		    + itx->itx_lr.lrc_reclen);
1139789Sahrens 		mutex_enter(&zilog->zl_lock);
1140789Sahrens 	}
11412638Sperrin 	DTRACE_PROBE1(zil__cw2, zilog_t *, zilog);
11423063Sperrin 	/* determine commit sequence number */
11433063Sperrin 	itx = list_head(&zilog->zl_itx_list);
11443063Sperrin 	if (itx)
11453063Sperrin 		commit_seq = itx->itx_lr.lrc_seq;
11463063Sperrin 	else
11473063Sperrin 		commit_seq = zilog->zl_itx_seq;
1148789Sahrens 	mutex_exit(&zilog->zl_lock);
1149789Sahrens 
1150789Sahrens 	/* write the last block out */
11513063Sperrin 	if (lwb != NULL && lwb->lwb_zio != NULL)
1152789Sahrens 		lwb = zil_lwb_write_start(zilog, lwb);
1153789Sahrens 
11541141Sperrin 	zilog->zl_prev_used = zilog->zl_cur_used;
11551141Sperrin 	zilog->zl_cur_used = 0;
11561141Sperrin 
11572638Sperrin 	/*
11582638Sperrin 	 * Wait if necessary for the log blocks to be on stable storage.
11592638Sperrin 	 */
11602638Sperrin 	if (zilog->zl_root_zio) {
11612638Sperrin 		DTRACE_PROBE1(zil__cw3, zilog_t *, zilog);
11622638Sperrin 		(void) zio_wait(zilog->zl_root_zio);
11637754SJeff.Bonwick@Sun.COM 		zilog->zl_root_zio = NULL;
11642638Sperrin 		DTRACE_PROBE1(zil__cw4, zilog_t *, zilog);
11655688Sbonwick 		zil_flush_vdevs(zilog);
1166789Sahrens 	}
11671141Sperrin 
1168789Sahrens 	if (zilog->zl_log_error || lwb == NULL) {
1169789Sahrens 		zilog->zl_log_error = 0;
1170789Sahrens 		txg_wait_synced(zilog->zl_dmu_pool, 0);
1171789Sahrens 	}
11723063Sperrin 
11733063Sperrin 	mutex_enter(&zilog->zl_lock);
11741141Sperrin 	zilog->zl_writer = B_FALSE;
11753063Sperrin 
11763063Sperrin 	ASSERT3U(commit_seq, >=, zilog->zl_commit_seq);
11773063Sperrin 	zilog->zl_commit_seq = commit_seq;
11782638Sperrin }
11792638Sperrin 
11802638Sperrin /*
11812638Sperrin  * Push zfs transactions to stable storage up to the supplied sequence number.
11822638Sperrin  * If foid is 0 push out all transactions, otherwise push only those
11832638Sperrin  * for that file or might have been used to create that file.
11842638Sperrin  */
11852638Sperrin void
11862638Sperrin zil_commit(zilog_t *zilog, uint64_t seq, uint64_t foid)
11872638Sperrin {
11882638Sperrin 	if (zilog == NULL || seq == 0)
11892638Sperrin 		return;
11902638Sperrin 
11912638Sperrin 	mutex_enter(&zilog->zl_lock);
11922638Sperrin 
11932638Sperrin 	seq = MIN(seq, zilog->zl_itx_seq);	/* cap seq at largest itx seq */
11942638Sperrin 
11953063Sperrin 	while (zilog->zl_writer) {
11962638Sperrin 		cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
11973063Sperrin 		if (seq < zilog->zl_commit_seq) {
11983063Sperrin 			mutex_exit(&zilog->zl_lock);
11993063Sperrin 			return;
12003063Sperrin 		}
12013063Sperrin 	}
12022638Sperrin 	zil_commit_writer(zilog, seq, foid); /* drops zl_lock */
12033063Sperrin 	/* wake up others waiting on the commit */
12043063Sperrin 	cv_broadcast(&zilog->zl_cv_writer);
12053063Sperrin 	mutex_exit(&zilog->zl_lock);
1206789Sahrens }
1207789Sahrens 
1208789Sahrens /*
1209789Sahrens  * Called in syncing context to free committed log blocks and update log header.
1210789Sahrens  */
1211789Sahrens void
1212789Sahrens zil_sync(zilog_t *zilog, dmu_tx_t *tx)
1213789Sahrens {
12141807Sbonwick 	zil_header_t *zh = zil_header_in_syncing_context(zilog);
1215789Sahrens 	uint64_t txg = dmu_tx_get_txg(tx);
1216789Sahrens 	spa_t *spa = zilog->zl_spa;
1217789Sahrens 	lwb_t *lwb;
1218789Sahrens 
12191807Sbonwick 	mutex_enter(&zilog->zl_lock);
12201807Sbonwick 
1221789Sahrens 	ASSERT(zilog->zl_stop_sync == 0);
1222789Sahrens 
1223*8227SNeil.Perrin@Sun.COM 	zh->zh_replay_seq = zilog->zl_replayed_seq[txg & TXG_MASK];
1224789Sahrens 
1225789Sahrens 	if (zilog->zl_destroy_txg == txg) {
12261807Sbonwick 		blkptr_t blk = zh->zh_log;
12271807Sbonwick 
12281807Sbonwick 		ASSERT(list_head(&zilog->zl_lwb_list) == NULL);
12291807Sbonwick 		ASSERT(spa_sync_pass(spa) == 1);
12301807Sbonwick 
12311807Sbonwick 		bzero(zh, sizeof (zil_header_t));
1232*8227SNeil.Perrin@Sun.COM 		bzero(zilog->zl_replayed_seq, sizeof (zilog->zl_replayed_seq));
12331807Sbonwick 
12341807Sbonwick 		if (zilog->zl_keep_first) {
12351807Sbonwick 			/*
12361807Sbonwick 			 * If this block was part of log chain that couldn't
12371807Sbonwick 			 * be claimed because a device was missing during
12381807Sbonwick 			 * zil_claim(), but that device later returns,
12391807Sbonwick 			 * then this block could erroneously appear valid.
12401807Sbonwick 			 * To guard against this, assign a new GUID to the new
12411807Sbonwick 			 * log chain so it doesn't matter what blk points to.
12421807Sbonwick 			 */
12431807Sbonwick 			zil_init_log_chain(zilog, &blk);
12441807Sbonwick 			zh->zh_log = blk;
12451807Sbonwick 		}
1246789Sahrens 	}
1247789Sahrens 
1248789Sahrens 	for (;;) {
1249789Sahrens 		lwb = list_head(&zilog->zl_lwb_list);
1250789Sahrens 		if (lwb == NULL) {
1251789Sahrens 			mutex_exit(&zilog->zl_lock);
1252789Sahrens 			return;
1253789Sahrens 		}
12542638Sperrin 		zh->zh_log = lwb->lwb_blk;
1255789Sahrens 		if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg)
1256789Sahrens 			break;
1257789Sahrens 		list_remove(&zilog->zl_lwb_list, lwb);
1258789Sahrens 		zio_free_blk(spa, &lwb->lwb_blk, txg);
1259789Sahrens 		kmem_cache_free(zil_lwb_cache, lwb);
12603668Sgw25295 
12613668Sgw25295 		/*
12623668Sgw25295 		 * If we don't have anything left in the lwb list then
12633668Sgw25295 		 * we've had an allocation failure and we need to zero
12643668Sgw25295 		 * out the zil_header blkptr so that we don't end
12653668Sgw25295 		 * up freeing the same block twice.
12663668Sgw25295 		 */
12673668Sgw25295 		if (list_head(&zilog->zl_lwb_list) == NULL)
12683668Sgw25295 			BP_ZERO(&zh->zh_log);
1269789Sahrens 	}
1270789Sahrens 	mutex_exit(&zilog->zl_lock);
1271789Sahrens }
1272789Sahrens 
1273789Sahrens void
1274789Sahrens zil_init(void)
1275789Sahrens {
1276789Sahrens 	zil_lwb_cache = kmem_cache_create("zil_lwb_cache",
12772856Snd150628 	    sizeof (struct lwb), 0, NULL, NULL, NULL, NULL, NULL, 0);
1278789Sahrens }
1279789Sahrens 
1280789Sahrens void
1281789Sahrens zil_fini(void)
1282789Sahrens {
1283789Sahrens 	kmem_cache_destroy(zil_lwb_cache);
1284789Sahrens }
1285789Sahrens 
1286789Sahrens zilog_t *
1287789Sahrens zil_alloc(objset_t *os, zil_header_t *zh_phys)
1288789Sahrens {
1289789Sahrens 	zilog_t *zilog;
1290789Sahrens 
1291789Sahrens 	zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP);
1292789Sahrens 
1293789Sahrens 	zilog->zl_header = zh_phys;
1294789Sahrens 	zilog->zl_os = os;
1295789Sahrens 	zilog->zl_spa = dmu_objset_spa(os);
1296789Sahrens 	zilog->zl_dmu_pool = dmu_objset_pool(os);
12971807Sbonwick 	zilog->zl_destroy_txg = TXG_INITIAL - 1;
1298789Sahrens 
12992856Snd150628 	mutex_init(&zilog->zl_lock, NULL, MUTEX_DEFAULT, NULL);
13002856Snd150628 
1301789Sahrens 	list_create(&zilog->zl_itx_list, sizeof (itx_t),
1302789Sahrens 	    offsetof(itx_t, itx_node));
1303789Sahrens 
1304789Sahrens 	list_create(&zilog->zl_lwb_list, sizeof (lwb_t),
1305789Sahrens 	    offsetof(lwb_t, lwb_node));
1306789Sahrens 
13075688Sbonwick 	mutex_init(&zilog->zl_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
13085688Sbonwick 
13095688Sbonwick 	avl_create(&zilog->zl_vdev_tree, zil_vdev_compare,
13105688Sbonwick 	    sizeof (zil_vdev_node_t), offsetof(zil_vdev_node_t, zv_node));
1311789Sahrens 
13125913Sperrin 	cv_init(&zilog->zl_cv_writer, NULL, CV_DEFAULT, NULL);
13135913Sperrin 	cv_init(&zilog->zl_cv_suspend, NULL, CV_DEFAULT, NULL);
13145913Sperrin 
1315789Sahrens 	return (zilog);
1316789Sahrens }
1317789Sahrens 
1318789Sahrens void
1319789Sahrens zil_free(zilog_t *zilog)
1320789Sahrens {
1321789Sahrens 	lwb_t *lwb;
1322789Sahrens 
1323789Sahrens 	zilog->zl_stop_sync = 1;
1324789Sahrens 
1325789Sahrens 	while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
1326789Sahrens 		list_remove(&zilog->zl_lwb_list, lwb);
1327789Sahrens 		if (lwb->lwb_buf != NULL)
1328789Sahrens 			zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
1329789Sahrens 		kmem_cache_free(zil_lwb_cache, lwb);
1330789Sahrens 	}
1331789Sahrens 	list_destroy(&zilog->zl_lwb_list);
1332789Sahrens 
13335688Sbonwick 	avl_destroy(&zilog->zl_vdev_tree);
13345688Sbonwick 	mutex_destroy(&zilog->zl_vdev_lock);
1335789Sahrens 
1336789Sahrens 	ASSERT(list_head(&zilog->zl_itx_list) == NULL);
1337789Sahrens 	list_destroy(&zilog->zl_itx_list);
13382856Snd150628 	mutex_destroy(&zilog->zl_lock);
1339789Sahrens 
13405913Sperrin 	cv_destroy(&zilog->zl_cv_writer);
13415913Sperrin 	cv_destroy(&zilog->zl_cv_suspend);
13425913Sperrin 
1343789Sahrens 	kmem_free(zilog, sizeof (zilog_t));
1344789Sahrens }
1345789Sahrens 
1346789Sahrens /*
13471646Sperrin  * return true if the initial log block is not valid
13481362Sperrin  */
13497522SNeil.Perrin@Sun.COM static boolean_t
13501362Sperrin zil_empty(zilog_t *zilog)
13511362Sperrin {
13521807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
13531807Sbonwick 	arc_buf_t *abuf = NULL;
13541362Sperrin 
13551807Sbonwick 	if (BP_IS_HOLE(&zh->zh_log))
13567522SNeil.Perrin@Sun.COM 		return (B_TRUE);
13571362Sperrin 
13581807Sbonwick 	if (zil_read_log_block(zilog, &zh->zh_log, &abuf) != 0)
13597522SNeil.Perrin@Sun.COM 		return (B_TRUE);
13601807Sbonwick 
13611807Sbonwick 	VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
13627522SNeil.Perrin@Sun.COM 	return (B_FALSE);
13631362Sperrin }
13641362Sperrin 
13651362Sperrin /*
1366789Sahrens  * Open an intent log.
1367789Sahrens  */
1368789Sahrens zilog_t *
1369789Sahrens zil_open(objset_t *os, zil_get_data_t *get_data)
1370789Sahrens {
1371789Sahrens 	zilog_t *zilog = dmu_objset_zil(os);
1372789Sahrens 
1373789Sahrens 	zilog->zl_get_data = get_data;
1374789Sahrens 	zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri,
1375789Sahrens 	    2, 2, TASKQ_PREPOPULATE);
1376789Sahrens 
1377789Sahrens 	return (zilog);
1378789Sahrens }
1379789Sahrens 
1380789Sahrens /*
1381789Sahrens  * Close an intent log.
1382789Sahrens  */
1383789Sahrens void
1384789Sahrens zil_close(zilog_t *zilog)
1385789Sahrens {
13861807Sbonwick 	/*
13871807Sbonwick 	 * If the log isn't already committed, mark the objset dirty
13881807Sbonwick 	 * (so zil_sync() will be called) and wait for that txg to sync.
13891807Sbonwick 	 */
13901807Sbonwick 	if (!zil_is_committed(zilog)) {
13911807Sbonwick 		uint64_t txg;
13921807Sbonwick 		dmu_tx_t *tx = dmu_tx_create(zilog->zl_os);
13931807Sbonwick 		(void) dmu_tx_assign(tx, TXG_WAIT);
13941807Sbonwick 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
13951807Sbonwick 		txg = dmu_tx_get_txg(tx);
13961807Sbonwick 		dmu_tx_commit(tx);
13971807Sbonwick 		txg_wait_synced(zilog->zl_dmu_pool, txg);
13981807Sbonwick 	}
13991807Sbonwick 
1400789Sahrens 	taskq_destroy(zilog->zl_clean_taskq);
1401789Sahrens 	zilog->zl_clean_taskq = NULL;
1402789Sahrens 	zilog->zl_get_data = NULL;
1403789Sahrens 
1404789Sahrens 	zil_itx_clean(zilog);
1405789Sahrens 	ASSERT(list_head(&zilog->zl_itx_list) == NULL);
1406789Sahrens }
1407789Sahrens 
1408789Sahrens /*
1409789Sahrens  * Suspend an intent log.  While in suspended mode, we still honor
1410789Sahrens  * synchronous semantics, but we rely on txg_wait_synced() to do it.
1411789Sahrens  * We suspend the log briefly when taking a snapshot so that the snapshot
1412789Sahrens  * contains all the data it's supposed to, and has an empty intent log.
1413789Sahrens  */
1414789Sahrens int
1415789Sahrens zil_suspend(zilog_t *zilog)
1416789Sahrens {
14171807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
1418789Sahrens 
1419789Sahrens 	mutex_enter(&zilog->zl_lock);
14201807Sbonwick 	if (zh->zh_claim_txg != 0) {		/* unplayed log */
1421789Sahrens 		mutex_exit(&zilog->zl_lock);
1422789Sahrens 		return (EBUSY);
1423789Sahrens 	}
14241807Sbonwick 	if (zilog->zl_suspend++ != 0) {
14251807Sbonwick 		/*
14261807Sbonwick 		 * Someone else already began a suspend.
14271807Sbonwick 		 * Just wait for them to finish.
14281807Sbonwick 		 */
14291807Sbonwick 		while (zilog->zl_suspending)
14301807Sbonwick 			cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock);
14311807Sbonwick 		mutex_exit(&zilog->zl_lock);
14321807Sbonwick 		return (0);
14331807Sbonwick 	}
14341807Sbonwick 	zilog->zl_suspending = B_TRUE;
1435789Sahrens 	mutex_exit(&zilog->zl_lock);
1436789Sahrens 
14372638Sperrin 	zil_commit(zilog, UINT64_MAX, 0);
1438789Sahrens 
14392638Sperrin 	/*
14402638Sperrin 	 * Wait for any in-flight log writes to complete.
14412638Sperrin 	 */
1442789Sahrens 	mutex_enter(&zilog->zl_lock);
14432638Sperrin 	while (zilog->zl_writer)
14442638Sperrin 		cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
1445789Sahrens 	mutex_exit(&zilog->zl_lock);
1446789Sahrens 
14471807Sbonwick 	zil_destroy(zilog, B_FALSE);
14481807Sbonwick 
14491807Sbonwick 	mutex_enter(&zilog->zl_lock);
14501807Sbonwick 	zilog->zl_suspending = B_FALSE;
14511807Sbonwick 	cv_broadcast(&zilog->zl_cv_suspend);
14521807Sbonwick 	mutex_exit(&zilog->zl_lock);
1453789Sahrens 
1454789Sahrens 	return (0);
1455789Sahrens }
1456789Sahrens 
1457789Sahrens void
1458789Sahrens zil_resume(zilog_t *zilog)
1459789Sahrens {
1460789Sahrens 	mutex_enter(&zilog->zl_lock);
1461789Sahrens 	ASSERT(zilog->zl_suspend != 0);
1462789Sahrens 	zilog->zl_suspend--;
1463789Sahrens 	mutex_exit(&zilog->zl_lock);
1464789Sahrens }
1465789Sahrens 
1466789Sahrens typedef struct zil_replay_arg {
1467789Sahrens 	objset_t	*zr_os;
1468789Sahrens 	zil_replay_func_t **zr_replay;
1469789Sahrens 	void		*zr_arg;
1470789Sahrens 	boolean_t	zr_byteswap;
1471789Sahrens 	char		*zr_lrbuf;
1472789Sahrens } zil_replay_arg_t;
1473789Sahrens 
1474789Sahrens static void
1475789Sahrens zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg)
1476789Sahrens {
1477789Sahrens 	zil_replay_arg_t *zr = zra;
14781807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
1479789Sahrens 	uint64_t reclen = lr->lrc_reclen;
1480789Sahrens 	uint64_t txtype = lr->lrc_txtype;
14813063Sperrin 	char *name;
1482*8227SNeil.Perrin@Sun.COM 	int pass, error;
1483789Sahrens 
1484*8227SNeil.Perrin@Sun.COM 	if (!zilog->zl_replay)			/* giving up */
1485789Sahrens 		return;
1486789Sahrens 
1487789Sahrens 	if (lr->lrc_txg < claim_txg)		/* already committed */
1488789Sahrens 		return;
1489789Sahrens 
1490789Sahrens 	if (lr->lrc_seq <= zh->zh_replay_seq)	/* already replayed */
1491789Sahrens 		return;
1492789Sahrens 
14935331Samw 	/* Strip case-insensitive bit, still present in log record */
14945331Samw 	txtype &= ~TX_CI;
14955331Samw 
1496*8227SNeil.Perrin@Sun.COM 	if (txtype == 0 || txtype >= TX_MAX_TYPE) {
1497*8227SNeil.Perrin@Sun.COM 		error = EINVAL;
1498*8227SNeil.Perrin@Sun.COM 		goto bad;
1499*8227SNeil.Perrin@Sun.COM 	}
1500*8227SNeil.Perrin@Sun.COM 
1501789Sahrens 	/*
1502789Sahrens 	 * Make a copy of the data so we can revise and extend it.
1503789Sahrens 	 */
1504789Sahrens 	bcopy(lr, zr->zr_lrbuf, reclen);
1505789Sahrens 
1506789Sahrens 	/*
1507789Sahrens 	 * The log block containing this lr may have been byteswapped
1508789Sahrens 	 * so that we can easily examine common fields like lrc_txtype.
1509789Sahrens 	 * However, the log is a mix of different data types, and only the
1510789Sahrens 	 * replay vectors know how to byteswap their records.  Therefore, if
1511789Sahrens 	 * the lr was byteswapped, undo it before invoking the replay vector.
1512789Sahrens 	 */
1513789Sahrens 	if (zr->zr_byteswap)
1514789Sahrens 		byteswap_uint64_array(zr->zr_lrbuf, reclen);
1515789Sahrens 
1516789Sahrens 	/*
1517789Sahrens 	 * If this is a TX_WRITE with a blkptr, suck in the data.
1518789Sahrens 	 */
1519789Sahrens 	if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) {
1520789Sahrens 		lr_write_t *lrw = (lr_write_t *)lr;
1521789Sahrens 		blkptr_t *wbp = &lrw->lr_blkptr;
1522789Sahrens 		uint64_t wlen = lrw->lr_length;
1523789Sahrens 		char *wbuf = zr->zr_lrbuf + reclen;
1524789Sahrens 
1525789Sahrens 		if (BP_IS_HOLE(wbp)) {	/* compressed to a hole */
1526789Sahrens 			bzero(wbuf, wlen);
1527789Sahrens 		} else {
1528789Sahrens 			/*
1529789Sahrens 			 * A subsequent write may have overwritten this block,
1530789Sahrens 			 * in which case wbp may have been been freed and
1531789Sahrens 			 * reallocated, and our read of wbp may fail with a
1532789Sahrens 			 * checksum error.  We can safely ignore this because
1533789Sahrens 			 * the later write will provide the correct data.
1534789Sahrens 			 */
15351544Seschrock 			zbookmark_t zb;
15361544Seschrock 
15371544Seschrock 			zb.zb_objset = dmu_objset_id(zilog->zl_os);
15381544Seschrock 			zb.zb_object = lrw->lr_foid;
15391544Seschrock 			zb.zb_level = -1;
15401544Seschrock 			zb.zb_blkid = lrw->lr_offset / BP_GET_LSIZE(wbp);
15411544Seschrock 
1542789Sahrens 			(void) zio_wait(zio_read(NULL, zilog->zl_spa,
1543789Sahrens 			    wbp, wbuf, BP_GET_LSIZE(wbp), NULL, NULL,
1544789Sahrens 			    ZIO_PRIORITY_SYNC_READ,
15451544Seschrock 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &zb));
1546789Sahrens 			(void) memmove(wbuf, wbuf + lrw->lr_blkoff, wlen);
1547789Sahrens 		}
1548789Sahrens 	}
1549789Sahrens 
1550789Sahrens 	/*
1551789Sahrens 	 * We must now do two things atomically: replay this log record,
1552*8227SNeil.Perrin@Sun.COM 	 * and update the log header sequence number to reflect the fact that
1553*8227SNeil.Perrin@Sun.COM 	 * we did so. At the end of each replay function the sequence number
1554*8227SNeil.Perrin@Sun.COM 	 * is updated if we are in replay mode.
1555789Sahrens 	 */
1556*8227SNeil.Perrin@Sun.COM 	for (pass = 1; pass <= 2; pass++) {
1557*8227SNeil.Perrin@Sun.COM 		zilog->zl_replaying_seq = lr->lrc_seq;
1558*8227SNeil.Perrin@Sun.COM 		/* Only byteswap (if needed) on the 1st pass.  */
1559*8227SNeil.Perrin@Sun.COM 		error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lrbuf,
1560*8227SNeil.Perrin@Sun.COM 		    zr->zr_byteswap && pass == 1);
1561789Sahrens 
15623063Sperrin 		if (!error)
15633063Sperrin 			return;
15643063Sperrin 
15653063Sperrin 		/*
15663063Sperrin 		 * The DMU's dnode layer doesn't see removes until the txg
15673063Sperrin 		 * commits, so a subsequent claim can spuriously fail with
1568*8227SNeil.Perrin@Sun.COM 		 * EEXIST. So if we receive any error we try syncing out
1569*8227SNeil.Perrin@Sun.COM 		 * any removes then retry the transaction.
15703063Sperrin 		 */
1571*8227SNeil.Perrin@Sun.COM 		if (pass == 1)
15723063Sperrin 			txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0);
1573789Sahrens 	}
1574789Sahrens 
15757904SNeil.Perrin@Sun.COM bad:
1576*8227SNeil.Perrin@Sun.COM 	ASSERT(error);
15773063Sperrin 	name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
15783063Sperrin 	dmu_objset_name(zr->zr_os, name);
15793063Sperrin 	cmn_err(CE_WARN, "ZFS replay transaction error %d, "
15805331Samw 	    "dataset %s, seq 0x%llx, txtype %llu %s\n",
15815331Samw 	    error, name, (u_longlong_t)lr->lrc_seq, (u_longlong_t)txtype,
15825331Samw 	    (lr->lrc_txtype & TX_CI) ? "CI" : "");
1583*8227SNeil.Perrin@Sun.COM 	zilog->zl_replay = B_FALSE;
15843063Sperrin 	kmem_free(name, MAXNAMELEN);
15853063Sperrin }
1586789Sahrens 
15873063Sperrin /* ARGSUSED */
15883063Sperrin static void
15893063Sperrin zil_incr_blks(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
15903063Sperrin {
15913063Sperrin 	zilog->zl_replay_blks++;
1592789Sahrens }
1593789Sahrens 
1594789Sahrens /*
15951362Sperrin  * If this dataset has a non-empty intent log, replay it and destroy it.
1596789Sahrens  */
1597789Sahrens void
1598*8227SNeil.Perrin@Sun.COM zil_replay(objset_t *os, void *arg, zil_replay_func_t *replay_func[TX_MAX_TYPE])
1599789Sahrens {
1600789Sahrens 	zilog_t *zilog = dmu_objset_zil(os);
16011807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
16021807Sbonwick 	zil_replay_arg_t zr;
16031362Sperrin 
16041362Sperrin 	if (zil_empty(zilog)) {
16051807Sbonwick 		zil_destroy(zilog, B_TRUE);
16061362Sperrin 		return;
16071362Sperrin 	}
1608789Sahrens 
1609789Sahrens 	zr.zr_os = os;
1610789Sahrens 	zr.zr_replay = replay_func;
1611789Sahrens 	zr.zr_arg = arg;
16121807Sbonwick 	zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log);
1613789Sahrens 	zr.zr_lrbuf = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP);
1614789Sahrens 
1615789Sahrens 	/*
1616789Sahrens 	 * Wait for in-progress removes to sync before starting replay.
1617789Sahrens 	 */
1618789Sahrens 	txg_wait_synced(zilog->zl_dmu_pool, 0);
1619789Sahrens 
1620*8227SNeil.Perrin@Sun.COM 	zilog->zl_replay = B_TRUE;
16213063Sperrin 	zilog->zl_replay_time = lbolt;
16223063Sperrin 	ASSERT(zilog->zl_replay_blks == 0);
16233063Sperrin 	(void) zil_parse(zilog, zil_incr_blks, zil_replay_log_record, &zr,
16241807Sbonwick 	    zh->zh_claim_txg);
1625789Sahrens 	kmem_free(zr.zr_lrbuf, 2 * SPA_MAXBLOCKSIZE);
1626789Sahrens 
16271807Sbonwick 	zil_destroy(zilog, B_FALSE);
16285712Sahrens 	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
1629*8227SNeil.Perrin@Sun.COM 	zilog->zl_replay = B_FALSE;
1630789Sahrens }
16311646Sperrin 
16321646Sperrin /*
16331646Sperrin  * Report whether all transactions are committed
16341646Sperrin  */
16351646Sperrin int
16361646Sperrin zil_is_committed(zilog_t *zilog)
16371646Sperrin {
16381646Sperrin 	lwb_t *lwb;
16392638Sperrin 	int ret;
16401646Sperrin 
16412638Sperrin 	mutex_enter(&zilog->zl_lock);
16422638Sperrin 	while (zilog->zl_writer)
16432638Sperrin 		cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
16442638Sperrin 
16452638Sperrin 	/* recent unpushed intent log transactions? */
16462638Sperrin 	if (!list_is_empty(&zilog->zl_itx_list)) {
16472638Sperrin 		ret = B_FALSE;
16482638Sperrin 		goto out;
16492638Sperrin 	}
16502638Sperrin 
16512638Sperrin 	/* intent log never used? */
16522638Sperrin 	lwb = list_head(&zilog->zl_lwb_list);
16532638Sperrin 	if (lwb == NULL) {
16542638Sperrin 		ret = B_TRUE;
16552638Sperrin 		goto out;
16562638Sperrin 	}
16571646Sperrin 
16581646Sperrin 	/*
16592638Sperrin 	 * more than 1 log buffer means zil_sync() hasn't yet freed
16602638Sperrin 	 * entries after a txg has committed
16611646Sperrin 	 */
16622638Sperrin 	if (list_next(&zilog->zl_lwb_list, lwb)) {
16632638Sperrin 		ret = B_FALSE;
16642638Sperrin 		goto out;
16652638Sperrin 	}
16662638Sperrin 
16671646Sperrin 	ASSERT(zil_empty(zilog));
16682638Sperrin 	ret = B_TRUE;
16692638Sperrin out:
16702638Sperrin 	cv_broadcast(&zilog->zl_cv_writer);
16712638Sperrin 	mutex_exit(&zilog->zl_lock);
16722638Sperrin 	return (ret);
16731646Sperrin }
1674