xref: /onnv-gate/usr/src/uts/common/fs/zfs/zil.c (revision 7181)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27789Sahrens 
28789Sahrens #include <sys/zfs_context.h>
29789Sahrens #include <sys/spa.h>
30789Sahrens #include <sys/dmu.h>
31789Sahrens #include <sys/zap.h>
32789Sahrens #include <sys/arc.h>
33789Sahrens #include <sys/stat.h>
34789Sahrens #include <sys/resource.h>
35789Sahrens #include <sys/zil.h>
36789Sahrens #include <sys/zil_impl.h>
37789Sahrens #include <sys/dsl_dataset.h>
38789Sahrens #include <sys/vdev.h>
393668Sgw25295 #include <sys/dmu_tx.h>
40789Sahrens 
41789Sahrens /*
42789Sahrens  * The zfs intent log (ZIL) saves transaction records of system calls
43789Sahrens  * that change the file system in memory with enough information
44789Sahrens  * to be able to replay them. These are stored in memory until
45789Sahrens  * either the DMU transaction group (txg) commits them to the stable pool
46789Sahrens  * and they can be discarded, or they are flushed to the stable log
47789Sahrens  * (also in the pool) due to a fsync, O_DSYNC or other synchronous
48789Sahrens  * requirement. In the event of a panic or power fail then those log
49789Sahrens  * records (transactions) are replayed.
50789Sahrens  *
51789Sahrens  * There is one ZIL per file system. Its on-disk (pool) format consists
52789Sahrens  * of 3 parts:
53789Sahrens  *
54789Sahrens  * 	- ZIL header
55789Sahrens  * 	- ZIL blocks
56789Sahrens  * 	- ZIL records
57789Sahrens  *
58789Sahrens  * A log record holds a system call transaction. Log blocks can
59789Sahrens  * hold many log records and the blocks are chained together.
60789Sahrens  * Each ZIL block contains a block pointer (blkptr_t) to the next
61789Sahrens  * ZIL block in the chain. The ZIL header points to the first
62789Sahrens  * block in the chain. Note there is not a fixed place in the pool
63789Sahrens  * to hold blocks. They are dynamically allocated and freed as
64789Sahrens  * needed from the blocks available. Figure X shows the ZIL structure:
65789Sahrens  */
66789Sahrens 
67789Sahrens /*
682986Sek110237  * This global ZIL switch affects all pools
69789Sahrens  */
70789Sahrens int zil_disable = 0;	/* disable intent logging */
712986Sek110237 
722986Sek110237 /*
732986Sek110237  * Tunable parameter for debugging or performance analysis.  Setting
742986Sek110237  * zfs_nocacheflush will cause corruption on power loss if a volatile
752986Sek110237  * out-of-order write cache is enabled.
762986Sek110237  */
772986Sek110237 boolean_t zfs_nocacheflush = B_FALSE;
78789Sahrens 
79789Sahrens static kmem_cache_t *zil_lwb_cache;
80789Sahrens 
81789Sahrens static int
82789Sahrens zil_dva_compare(const void *x1, const void *x2)
83789Sahrens {
84789Sahrens 	const dva_t *dva1 = x1;
85789Sahrens 	const dva_t *dva2 = x2;
86789Sahrens 
87789Sahrens 	if (DVA_GET_VDEV(dva1) < DVA_GET_VDEV(dva2))
88789Sahrens 		return (-1);
89789Sahrens 	if (DVA_GET_VDEV(dva1) > DVA_GET_VDEV(dva2))
90789Sahrens 		return (1);
91789Sahrens 
92789Sahrens 	if (DVA_GET_OFFSET(dva1) < DVA_GET_OFFSET(dva2))
93789Sahrens 		return (-1);
94789Sahrens 	if (DVA_GET_OFFSET(dva1) > DVA_GET_OFFSET(dva2))
95789Sahrens 		return (1);
96789Sahrens 
97789Sahrens 	return (0);
98789Sahrens }
99789Sahrens 
100789Sahrens static void
101789Sahrens zil_dva_tree_init(avl_tree_t *t)
102789Sahrens {
103789Sahrens 	avl_create(t, zil_dva_compare, sizeof (zil_dva_node_t),
104789Sahrens 	    offsetof(zil_dva_node_t, zn_node));
105789Sahrens }
106789Sahrens 
107789Sahrens static void
108789Sahrens zil_dva_tree_fini(avl_tree_t *t)
109789Sahrens {
110789Sahrens 	zil_dva_node_t *zn;
111789Sahrens 	void *cookie = NULL;
112789Sahrens 
113789Sahrens 	while ((zn = avl_destroy_nodes(t, &cookie)) != NULL)
114789Sahrens 		kmem_free(zn, sizeof (zil_dva_node_t));
115789Sahrens 
116789Sahrens 	avl_destroy(t);
117789Sahrens }
118789Sahrens 
119789Sahrens static int
120789Sahrens zil_dva_tree_add(avl_tree_t *t, dva_t *dva)
121789Sahrens {
122789Sahrens 	zil_dva_node_t *zn;
123789Sahrens 	avl_index_t where;
124789Sahrens 
125789Sahrens 	if (avl_find(t, dva, &where) != NULL)
126789Sahrens 		return (EEXIST);
127789Sahrens 
128789Sahrens 	zn = kmem_alloc(sizeof (zil_dva_node_t), KM_SLEEP);
129789Sahrens 	zn->zn_dva = *dva;
130789Sahrens 	avl_insert(t, zn, where);
131789Sahrens 
132789Sahrens 	return (0);
133789Sahrens }
134789Sahrens 
1351807Sbonwick static zil_header_t *
1361807Sbonwick zil_header_in_syncing_context(zilog_t *zilog)
1371807Sbonwick {
1381807Sbonwick 	return ((zil_header_t *)zilog->zl_header);
1391807Sbonwick }
1401807Sbonwick 
1411807Sbonwick static void
1421807Sbonwick zil_init_log_chain(zilog_t *zilog, blkptr_t *bp)
1431807Sbonwick {
1441807Sbonwick 	zio_cksum_t *zc = &bp->blk_cksum;
1451807Sbonwick 
1461807Sbonwick 	zc->zc_word[ZIL_ZC_GUID_0] = spa_get_random(-1ULL);
1471807Sbonwick 	zc->zc_word[ZIL_ZC_GUID_1] = spa_get_random(-1ULL);
1481807Sbonwick 	zc->zc_word[ZIL_ZC_OBJSET] = dmu_objset_id(zilog->zl_os);
1491807Sbonwick 	zc->zc_word[ZIL_ZC_SEQ] = 1ULL;
1501807Sbonwick }
1511807Sbonwick 
152789Sahrens /*
153789Sahrens  * Read a log block, make sure it's valid, and byteswap it if necessary.
154789Sahrens  */
155789Sahrens static int
1561807Sbonwick zil_read_log_block(zilog_t *zilog, const blkptr_t *bp, arc_buf_t **abufpp)
157789Sahrens {
1581807Sbonwick 	blkptr_t blk = *bp;
1591544Seschrock 	zbookmark_t zb;
1602391Smaybee 	uint32_t aflags = ARC_WAIT;
161789Sahrens 	int error;
162789Sahrens 
1631807Sbonwick 	zb.zb_objset = bp->blk_cksum.zc_word[ZIL_ZC_OBJSET];
1641544Seschrock 	zb.zb_object = 0;
1651544Seschrock 	zb.zb_level = -1;
1661807Sbonwick 	zb.zb_blkid = bp->blk_cksum.zc_word[ZIL_ZC_SEQ];
1671807Sbonwick 
1681807Sbonwick 	*abufpp = NULL;
1691807Sbonwick 
1707046Sahrens 	/*
1717046Sahrens 	 * We shouldn't be doing any scrubbing while we're doing log
1727046Sahrens 	 * replay, it's OK to not lock.
1737046Sahrens 	 */
1747046Sahrens 	error = arc_read_nolock(NULL, zilog->zl_spa, &blk,
1751807Sbonwick 	    arc_getbuf_func, abufpp, ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL |
1762391Smaybee 	    ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB, &aflags, &zb);
1771807Sbonwick 
1781807Sbonwick 	if (error == 0) {
1791807Sbonwick 		char *data = (*abufpp)->b_data;
1801807Sbonwick 		uint64_t blksz = BP_GET_LSIZE(bp);
1811807Sbonwick 		zil_trailer_t *ztp = (zil_trailer_t *)(data + blksz) - 1;
1821807Sbonwick 		zio_cksum_t cksum = bp->blk_cksum;
1831544Seschrock 
1841807Sbonwick 		/*
1851807Sbonwick 		 * Sequence numbers should be... sequential.  The checksum
1861807Sbonwick 		 * verifier for the next block should be bp's checksum plus 1.
1871807Sbonwick 		 */
1881807Sbonwick 		cksum.zc_word[ZIL_ZC_SEQ]++;
1891807Sbonwick 
1901807Sbonwick 		if (bcmp(&cksum, &ztp->zit_next_blk.blk_cksum, sizeof (cksum)))
1911807Sbonwick 			error = ESTALE;
1921807Sbonwick 		else if (BP_IS_HOLE(&ztp->zit_next_blk))
1931807Sbonwick 			error = ENOENT;
1941807Sbonwick 		else if (ztp->zit_nused > (blksz - sizeof (zil_trailer_t)))
1951807Sbonwick 			error = EOVERFLOW;
1961807Sbonwick 
1971807Sbonwick 		if (error) {
1981807Sbonwick 			VERIFY(arc_buf_remove_ref(*abufpp, abufpp) == 1);
1991807Sbonwick 			*abufpp = NULL;
2001807Sbonwick 		}
201789Sahrens 	}
202789Sahrens 
2031807Sbonwick 	dprintf("error %d on %llu:%llu\n", error, zb.zb_objset, zb.zb_blkid);
204789Sahrens 
2051807Sbonwick 	return (error);
206789Sahrens }
207789Sahrens 
208789Sahrens /*
209789Sahrens  * Parse the intent log, and call parse_func for each valid record within.
2101807Sbonwick  * Return the highest sequence number.
211789Sahrens  */
2121807Sbonwick uint64_t
213789Sahrens zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func,
214789Sahrens     zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg)
215789Sahrens {
2161807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
2171807Sbonwick 	uint64_t claim_seq = zh->zh_claim_seq;
2181807Sbonwick 	uint64_t seq = 0;
2191807Sbonwick 	uint64_t max_seq = 0;
2201807Sbonwick 	blkptr_t blk = zh->zh_log;
2211807Sbonwick 	arc_buf_t *abuf;
222789Sahrens 	char *lrbuf, *lrp;
223789Sahrens 	zil_trailer_t *ztp;
224789Sahrens 	int reclen, error;
225789Sahrens 
226789Sahrens 	if (BP_IS_HOLE(&blk))
2271807Sbonwick 		return (max_seq);
228789Sahrens 
229789Sahrens 	/*
230789Sahrens 	 * Starting at the block pointed to by zh_log we read the log chain.
231789Sahrens 	 * For each block in the chain we strongly check that block to
232789Sahrens 	 * ensure its validity.  We stop when an invalid block is found.
233789Sahrens 	 * For each block pointer in the chain we call parse_blk_func().
234789Sahrens 	 * For each record in each valid block we call parse_lr_func().
2351807Sbonwick 	 * If the log has been claimed, stop if we encounter a sequence
2361807Sbonwick 	 * number greater than the highest claimed sequence number.
237789Sahrens 	 */
238789Sahrens 	zil_dva_tree_init(&zilog->zl_dva_tree);
239789Sahrens 	for (;;) {
2401807Sbonwick 		seq = blk.blk_cksum.zc_word[ZIL_ZC_SEQ];
2411807Sbonwick 
2421807Sbonwick 		if (claim_seq != 0 && seq > claim_seq)
2431807Sbonwick 			break;
2441807Sbonwick 
2451807Sbonwick 		ASSERT(max_seq < seq);
2461807Sbonwick 		max_seq = seq;
2471807Sbonwick 
2481807Sbonwick 		error = zil_read_log_block(zilog, &blk, &abuf);
249789Sahrens 
250789Sahrens 		if (parse_blk_func != NULL)
251789Sahrens 			parse_blk_func(zilog, &blk, arg, txg);
252789Sahrens 
253789Sahrens 		if (error)
254789Sahrens 			break;
255789Sahrens 
2561807Sbonwick 		lrbuf = abuf->b_data;
257789Sahrens 		ztp = (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1;
258789Sahrens 		blk = ztp->zit_next_blk;
259789Sahrens 
2601807Sbonwick 		if (parse_lr_func == NULL) {
2611807Sbonwick 			VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
262789Sahrens 			continue;
2631807Sbonwick 		}
264789Sahrens 
265789Sahrens 		for (lrp = lrbuf; lrp < lrbuf + ztp->zit_nused; lrp += reclen) {
266789Sahrens 			lr_t *lr = (lr_t *)lrp;
267789Sahrens 			reclen = lr->lrc_reclen;
268789Sahrens 			ASSERT3U(reclen, >=, sizeof (lr_t));
269789Sahrens 			parse_lr_func(zilog, lr, arg, txg);
270789Sahrens 		}
2711807Sbonwick 		VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
272789Sahrens 	}
273789Sahrens 	zil_dva_tree_fini(&zilog->zl_dva_tree);
2741807Sbonwick 
2751807Sbonwick 	return (max_seq);
276789Sahrens }
277789Sahrens 
278789Sahrens /* ARGSUSED */
279789Sahrens static void
280789Sahrens zil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg)
281789Sahrens {
282789Sahrens 	spa_t *spa = zilog->zl_spa;
283789Sahrens 	int err;
284789Sahrens 
285789Sahrens 	/*
286789Sahrens 	 * Claim log block if not already committed and not already claimed.
287789Sahrens 	 */
288789Sahrens 	if (bp->blk_birth >= first_txg &&
289789Sahrens 	    zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp)) == 0) {
290789Sahrens 		err = zio_wait(zio_claim(NULL, spa, first_txg, bp, NULL, NULL));
291789Sahrens 		ASSERT(err == 0);
292789Sahrens 	}
293789Sahrens }
294789Sahrens 
295789Sahrens static void
296789Sahrens zil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg)
297789Sahrens {
298789Sahrens 	if (lrc->lrc_txtype == TX_WRITE) {
299789Sahrens 		lr_write_t *lr = (lr_write_t *)lrc;
300789Sahrens 		zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg);
301789Sahrens 	}
302789Sahrens }
303789Sahrens 
304789Sahrens /* ARGSUSED */
305789Sahrens static void
306789Sahrens zil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg)
307789Sahrens {
308789Sahrens 	zio_free_blk(zilog->zl_spa, bp, dmu_tx_get_txg(tx));
309789Sahrens }
310789Sahrens 
311789Sahrens static void
312789Sahrens zil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg)
313789Sahrens {
314789Sahrens 	/*
315789Sahrens 	 * If we previously claimed it, we need to free it.
316789Sahrens 	 */
317789Sahrens 	if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE) {
318789Sahrens 		lr_write_t *lr = (lr_write_t *)lrc;
319789Sahrens 		blkptr_t *bp = &lr->lr_blkptr;
320789Sahrens 		if (bp->blk_birth >= claim_txg &&
321789Sahrens 		    !zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp))) {
322789Sahrens 			(void) arc_free(NULL, zilog->zl_spa,
323789Sahrens 			    dmu_tx_get_txg(tx), bp, NULL, NULL, ARC_WAIT);
324789Sahrens 		}
325789Sahrens 	}
326789Sahrens }
327789Sahrens 
328789Sahrens /*
329789Sahrens  * Create an on-disk intent log.
330789Sahrens  */
331789Sahrens static void
332789Sahrens zil_create(zilog_t *zilog)
333789Sahrens {
3341807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
335789Sahrens 	lwb_t *lwb;
3361807Sbonwick 	uint64_t txg = 0;
3371807Sbonwick 	dmu_tx_t *tx = NULL;
338789Sahrens 	blkptr_t blk;
3391807Sbonwick 	int error = 0;
340789Sahrens 
341789Sahrens 	/*
3421807Sbonwick 	 * Wait for any previous destroy to complete.
343789Sahrens 	 */
3441807Sbonwick 	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
3451807Sbonwick 
3461807Sbonwick 	ASSERT(zh->zh_claim_txg == 0);
3471807Sbonwick 	ASSERT(zh->zh_replay_seq == 0);
3481807Sbonwick 
3491807Sbonwick 	blk = zh->zh_log;
350789Sahrens 
351789Sahrens 	/*
3521807Sbonwick 	 * If we don't already have an initial log block, allocate one now.
353789Sahrens 	 */
3541807Sbonwick 	if (BP_IS_HOLE(&blk)) {
3551807Sbonwick 		tx = dmu_tx_create(zilog->zl_os);
3561807Sbonwick 		(void) dmu_tx_assign(tx, TXG_WAIT);
3571807Sbonwick 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
3581807Sbonwick 		txg = dmu_tx_get_txg(tx);
3591807Sbonwick 
3603063Sperrin 		error = zio_alloc_blk(zilog->zl_spa, ZIL_MIN_BLKSZ, &blk,
3613063Sperrin 		    NULL, txg);
3621807Sbonwick 
3631807Sbonwick 		if (error == 0)
3641807Sbonwick 			zil_init_log_chain(zilog, &blk);
3651362Sperrin 	}
3661807Sbonwick 
3671807Sbonwick 	/*
3681807Sbonwick 	 * Allocate a log write buffer (lwb) for the first log block.
3691807Sbonwick 	 */
370789Sahrens 	if (error == 0) {
371789Sahrens 		lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP);
372789Sahrens 		lwb->lwb_zilog = zilog;
373789Sahrens 		lwb->lwb_blk = blk;
374789Sahrens 		lwb->lwb_nused = 0;
375789Sahrens 		lwb->lwb_sz = BP_GET_LSIZE(&lwb->lwb_blk);
376789Sahrens 		lwb->lwb_buf = zio_buf_alloc(lwb->lwb_sz);
377789Sahrens 		lwb->lwb_max_txg = txg;
3782237Smaybee 		lwb->lwb_zio = NULL;
3792237Smaybee 
380789Sahrens 		mutex_enter(&zilog->zl_lock);
381789Sahrens 		list_insert_tail(&zilog->zl_lwb_list, lwb);
382789Sahrens 		mutex_exit(&zilog->zl_lock);
383789Sahrens 	}
384789Sahrens 
3851807Sbonwick 	/*
3861807Sbonwick 	 * If we just allocated the first log block, commit our transaction
3871807Sbonwick 	 * and wait for zil_sync() to stuff the block poiner into zh_log.
3881807Sbonwick 	 * (zh is part of the MOS, so we cannot modify it in open context.)
3891807Sbonwick 	 */
3901807Sbonwick 	if (tx != NULL) {
3911807Sbonwick 		dmu_tx_commit(tx);
3921362Sperrin 		txg_wait_synced(zilog->zl_dmu_pool, txg);
3931807Sbonwick 	}
3941807Sbonwick 
3951807Sbonwick 	ASSERT(bcmp(&blk, &zh->zh_log, sizeof (blk)) == 0);
396789Sahrens }
397789Sahrens 
398789Sahrens /*
399789Sahrens  * In one tx, free all log blocks and clear the log header.
4001807Sbonwick  * If keep_first is set, then we're replaying a log with no content.
4011807Sbonwick  * We want to keep the first block, however, so that the first
4021807Sbonwick  * synchronous transaction doesn't require a txg_wait_synced()
4031807Sbonwick  * in zil_create().  We don't need to txg_wait_synced() here either
4041807Sbonwick  * when keep_first is set, because both zil_create() and zil_destroy()
4051807Sbonwick  * will wait for any in-progress destroys to complete.
406789Sahrens  */
407789Sahrens void
4081807Sbonwick zil_destroy(zilog_t *zilog, boolean_t keep_first)
409789Sahrens {
4101807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
4111807Sbonwick 	lwb_t *lwb;
412789Sahrens 	dmu_tx_t *tx;
413789Sahrens 	uint64_t txg;
414789Sahrens 
4151807Sbonwick 	/*
4161807Sbonwick 	 * Wait for any previous destroy to complete.
4171807Sbonwick 	 */
4181807Sbonwick 	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
419789Sahrens 
4201807Sbonwick 	if (BP_IS_HOLE(&zh->zh_log))
421789Sahrens 		return;
422789Sahrens 
423789Sahrens 	tx = dmu_tx_create(zilog->zl_os);
424789Sahrens 	(void) dmu_tx_assign(tx, TXG_WAIT);
425789Sahrens 	dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
426789Sahrens 	txg = dmu_tx_get_txg(tx);
427789Sahrens 
4281807Sbonwick 	mutex_enter(&zilog->zl_lock);
4291807Sbonwick 
4305223Sperrin 	/*
4315223Sperrin 	 * It is possible for the ZIL to get the previously mounted zilog
4325223Sperrin 	 * structure of the same dataset if quickly remounted and the dbuf
4335223Sperrin 	 * eviction has not completed. In this case we can see a non
4345223Sperrin 	 * empty lwb list and keep_first will be set. We fix this by
4355223Sperrin 	 * clearing the keep_first. This will be slower but it's very rare.
4365223Sperrin 	 */
4375223Sperrin 	if (!list_is_empty(&zilog->zl_lwb_list) && keep_first)
4385223Sperrin 		keep_first = B_FALSE;
4395223Sperrin 
4401807Sbonwick 	ASSERT3U(zilog->zl_destroy_txg, <, txg);
441789Sahrens 	zilog->zl_destroy_txg = txg;
4421807Sbonwick 	zilog->zl_keep_first = keep_first;
4431807Sbonwick 
4441807Sbonwick 	if (!list_is_empty(&zilog->zl_lwb_list)) {
4451807Sbonwick 		ASSERT(zh->zh_claim_txg == 0);
4461807Sbonwick 		ASSERT(!keep_first);
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 {
4551807Sbonwick 		if (!keep_first) {
4561807Sbonwick 			(void) zil_parse(zilog, zil_free_log_block,
4571807Sbonwick 			    zil_free_log_record, tx, zh->zh_claim_txg);
4581807Sbonwick 		}
4591807Sbonwick 	}
4602638Sperrin 	mutex_exit(&zilog->zl_lock);
461789Sahrens 
462789Sahrens 	dmu_tx_commit(tx);
463789Sahrens }
464789Sahrens 
4654935Sperrin /*
4664935Sperrin  * zil_rollback_destroy() is only called by the rollback code.
4674935Sperrin  * We already have a syncing tx. Rollback has exclusive access to the
4684935Sperrin  * dataset, so we don't have to worry about concurrent zil access.
4694935Sperrin  * The actual freeing of any log blocks occurs in zil_sync() later in
4704935Sperrin  * this txg syncing phase.
4714935Sperrin  */
4724935Sperrin void
4734935Sperrin zil_rollback_destroy(zilog_t *zilog, dmu_tx_t *tx)
4744935Sperrin {
4754935Sperrin 	const zil_header_t *zh = zilog->zl_header;
4764935Sperrin 	uint64_t txg;
4774935Sperrin 
4784935Sperrin 	if (BP_IS_HOLE(&zh->zh_log))
4794935Sperrin 		return;
4804935Sperrin 
4814935Sperrin 	txg = dmu_tx_get_txg(tx);
4824935Sperrin 	ASSERT3U(zilog->zl_destroy_txg, <, txg);
4834935Sperrin 	zilog->zl_destroy_txg = txg;
4844935Sperrin 	zilog->zl_keep_first = B_FALSE;
4854935Sperrin 
4865809Sperrin 	/*
4875809Sperrin 	 * Ensure there's no outstanding ZIL IO.  No lwbs or just the
4885809Sperrin 	 * unused one that allocated in advance is ok.
4895809Sperrin 	 */
4905809Sperrin 	ASSERT(zilog->zl_lwb_list.list_head.list_next ==
4915809Sperrin 	    zilog->zl_lwb_list.list_head.list_prev);
4924935Sperrin 	(void) zil_parse(zilog, zil_free_log_block, zil_free_log_record,
4934935Sperrin 	    tx, zh->zh_claim_txg);
4944935Sperrin }
4954935Sperrin 
4962199Sahrens int
497789Sahrens zil_claim(char *osname, void *txarg)
498789Sahrens {
499789Sahrens 	dmu_tx_t *tx = txarg;
500789Sahrens 	uint64_t first_txg = dmu_tx_get_txg(tx);
501789Sahrens 	zilog_t *zilog;
502789Sahrens 	zil_header_t *zh;
503789Sahrens 	objset_t *os;
504789Sahrens 	int error;
505789Sahrens 
5066689Smaybee 	error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_USER, &os);
507789Sahrens 	if (error) {
508789Sahrens 		cmn_err(CE_WARN, "can't process intent log for %s", osname);
5092199Sahrens 		return (0);
510789Sahrens 	}
511789Sahrens 
512789Sahrens 	zilog = dmu_objset_zil(os);
5131807Sbonwick 	zh = zil_header_in_syncing_context(zilog);
514789Sahrens 
515789Sahrens 	/*
5161807Sbonwick 	 * Claim all log blocks if we haven't already done so, and remember
5171807Sbonwick 	 * the highest claimed sequence number.  This ensures that if we can
5181807Sbonwick 	 * read only part of the log now (e.g. due to a missing device),
5191807Sbonwick 	 * but we can read the entire log later, we will not try to replay
5201807Sbonwick 	 * or destroy beyond the last block we successfully claimed.
521789Sahrens 	 */
522789Sahrens 	ASSERT3U(zh->zh_claim_txg, <=, first_txg);
523789Sahrens 	if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) {
524789Sahrens 		zh->zh_claim_txg = first_txg;
5251807Sbonwick 		zh->zh_claim_seq = zil_parse(zilog, zil_claim_log_block,
5261807Sbonwick 		    zil_claim_log_record, tx, first_txg);
527789Sahrens 		dsl_dataset_dirty(dmu_objset_ds(os), tx);
528789Sahrens 	}
5291807Sbonwick 
530789Sahrens 	ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1));
531789Sahrens 	dmu_objset_close(os);
5322199Sahrens 	return (0);
533789Sahrens }
534789Sahrens 
5355688Sbonwick static int
5365688Sbonwick zil_vdev_compare(const void *x1, const void *x2)
537789Sahrens {
5385875Sperrin 	uint64_t v1 = ((zil_vdev_node_t *)x1)->zv_vdev;
5395875Sperrin 	uint64_t v2 = ((zil_vdev_node_t *)x2)->zv_vdev;
5405688Sbonwick 
5415688Sbonwick 	if (v1 < v2)
5425688Sbonwick 		return (-1);
5435688Sbonwick 	if (v1 > v2)
5445688Sbonwick 		return (1);
5455688Sbonwick 
5465688Sbonwick 	return (0);
5475688Sbonwick }
5485688Sbonwick 
5495688Sbonwick void
5505688Sbonwick zil_add_block(zilog_t *zilog, blkptr_t *bp)
5515688Sbonwick {
5525688Sbonwick 	avl_tree_t *t = &zilog->zl_vdev_tree;
5535688Sbonwick 	avl_index_t where;
5545688Sbonwick 	zil_vdev_node_t *zv, zvsearch;
5555688Sbonwick 	int ndvas = BP_GET_NDVAS(bp);
5565688Sbonwick 	int i;
557789Sahrens 
5582986Sek110237 	if (zfs_nocacheflush)
559789Sahrens 		return;
560789Sahrens 
5615688Sbonwick 	ASSERT(zilog->zl_writer);
5625688Sbonwick 
5635688Sbonwick 	/*
5645688Sbonwick 	 * Even though we're zl_writer, we still need a lock because the
5655688Sbonwick 	 * zl_get_data() callbacks may have dmu_sync() done callbacks
5665688Sbonwick 	 * that will run concurrently.
5675688Sbonwick 	 */
5685688Sbonwick 	mutex_enter(&zilog->zl_vdev_lock);
5695688Sbonwick 	for (i = 0; i < ndvas; i++) {
5705688Sbonwick 		zvsearch.zv_vdev = DVA_GET_VDEV(&bp->blk_dva[i]);
5715688Sbonwick 		if (avl_find(t, &zvsearch, &where) == NULL) {
5725688Sbonwick 			zv = kmem_alloc(sizeof (*zv), KM_SLEEP);
5735688Sbonwick 			zv->zv_vdev = zvsearch.zv_vdev;
5745688Sbonwick 			avl_insert(t, zv, where);
5753063Sperrin 		}
5763063Sperrin 	}
5775688Sbonwick 	mutex_exit(&zilog->zl_vdev_lock);
5783063Sperrin }
5793063Sperrin 
580789Sahrens void
5812638Sperrin zil_flush_vdevs(zilog_t *zilog)
582789Sahrens {
5833063Sperrin 	spa_t *spa = zilog->zl_spa;
5845688Sbonwick 	avl_tree_t *t = &zilog->zl_vdev_tree;
5855688Sbonwick 	void *cookie = NULL;
5865688Sbonwick 	zil_vdev_node_t *zv;
5875688Sbonwick 	zio_t *zio;
5883063Sperrin 
5893063Sperrin 	ASSERT(zilog->zl_writer);
590789Sahrens 
5915688Sbonwick 	/*
5925688Sbonwick 	 * We don't need zl_vdev_lock here because we're the zl_writer,
5935688Sbonwick 	 * and all zl_get_data() callbacks are done.
5945688Sbonwick 	 */
5955688Sbonwick 	if (avl_numnodes(t) == 0)
5965688Sbonwick 		return;
5975688Sbonwick 
5985688Sbonwick 	spa_config_enter(spa, RW_READER, FTAG);
5995688Sbonwick 
6005688Sbonwick 	zio = zio_root(spa, NULL, NULL,
6015688Sbonwick 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
6025688Sbonwick 
6035688Sbonwick 	while ((zv = avl_destroy_nodes(t, &cookie)) != NULL) {
6045688Sbonwick 		vdev_t *vd = vdev_lookup_top(spa, zv->zv_vdev);
6055688Sbonwick 		if (vd != NULL)
6065688Sbonwick 			zio_flush(zio, vd);
6075688Sbonwick 		kmem_free(zv, sizeof (*zv));
6083063Sperrin 	}
609789Sahrens 
610789Sahrens 	/*
611789Sahrens 	 * Wait for all the flushes to complete.  Not all devices actually
612789Sahrens 	 * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails.
613789Sahrens 	 */
6145688Sbonwick 	(void) zio_wait(zio);
6155688Sbonwick 
6165688Sbonwick 	spa_config_exit(spa, FTAG);
617789Sahrens }
618789Sahrens 
619789Sahrens /*
620789Sahrens  * Function called when a log block write completes
621789Sahrens  */
622789Sahrens static void
623789Sahrens zil_lwb_write_done(zio_t *zio)
624789Sahrens {
625789Sahrens 	lwb_t *lwb = zio->io_private;
626789Sahrens 	zilog_t *zilog = lwb->lwb_zilog;
627789Sahrens 
628789Sahrens 	/*
629789Sahrens 	 * Now that we've written this log block, we have a stable pointer
630789Sahrens 	 * to the next block in the chain, so it's OK to let the txg in
631789Sahrens 	 * which we allocated the next block sync.
632789Sahrens 	 */
633789Sahrens 	txg_rele_to_sync(&lwb->lwb_txgh);
634789Sahrens 
635789Sahrens 	zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
636789Sahrens 	mutex_enter(&zilog->zl_lock);
637789Sahrens 	lwb->lwb_buf = NULL;
6384527Sperrin 	if (zio->io_error)
639789Sahrens 		zilog->zl_log_error = B_TRUE;
640789Sahrens 	mutex_exit(&zilog->zl_lock);
641789Sahrens }
642789Sahrens 
643789Sahrens /*
6442237Smaybee  * Initialize the io for a log block.
6452237Smaybee  *
6462237Smaybee  * Note, we should not initialize the IO until we are about
6472237Smaybee  * to use it, since zio_rewrite() does a spa_config_enter().
6482237Smaybee  */
6492237Smaybee static void
6502237Smaybee zil_lwb_write_init(zilog_t *zilog, lwb_t *lwb)
6512237Smaybee {
6522237Smaybee 	zbookmark_t zb;
6532237Smaybee 
6542237Smaybee 	zb.zb_objset = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET];
6552237Smaybee 	zb.zb_object = 0;
6562237Smaybee 	zb.zb_level = -1;
6572237Smaybee 	zb.zb_blkid = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ];
6582237Smaybee 
6592638Sperrin 	if (zilog->zl_root_zio == NULL) {
6602638Sperrin 		zilog->zl_root_zio = zio_root(zilog->zl_spa, NULL, NULL,
6612638Sperrin 		    ZIO_FLAG_CANFAIL);
6622638Sperrin 	}
6633063Sperrin 	if (lwb->lwb_zio == NULL) {
6643063Sperrin 		lwb->lwb_zio = zio_rewrite(zilog->zl_root_zio, zilog->zl_spa,
665*7181Sperrin 		    ZIO_CHECKSUM_ZILOG, 0, &lwb->lwb_blk, lwb->lwb_buf,
6663063Sperrin 		    lwb->lwb_sz, zil_lwb_write_done, lwb,
6674527Sperrin 		    ZIO_PRIORITY_LOG_WRITE, ZIO_FLAG_CANFAIL, &zb);
6683063Sperrin 	}
6692237Smaybee }
6702237Smaybee 
6712237Smaybee /*
672789Sahrens  * Start a log block write and advance to the next log block.
673789Sahrens  * Calls are serialized.
674789Sahrens  */
675789Sahrens static lwb_t *
676789Sahrens zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb)
677789Sahrens {
678789Sahrens 	lwb_t *nlwb;
679789Sahrens 	zil_trailer_t *ztp = (zil_trailer_t *)(lwb->lwb_buf + lwb->lwb_sz) - 1;
6801807Sbonwick 	spa_t *spa = zilog->zl_spa;
6811807Sbonwick 	blkptr_t *bp = &ztp->zit_next_blk;
682789Sahrens 	uint64_t txg;
683789Sahrens 	uint64_t zil_blksz;
684789Sahrens 	int error;
685789Sahrens 
686789Sahrens 	ASSERT(lwb->lwb_nused <= ZIL_BLK_DATA_SZ(lwb));
687789Sahrens 
688789Sahrens 	/*
689789Sahrens 	 * Allocate the next block and save its address in this block
690789Sahrens 	 * before writing it in order to establish the log chain.
691789Sahrens 	 * Note that if the allocation of nlwb synced before we wrote
692789Sahrens 	 * the block that points at it (lwb), we'd leak it if we crashed.
693789Sahrens 	 * Therefore, we don't do txg_rele_to_sync() until zil_lwb_write_done().
694789Sahrens 	 */
695789Sahrens 	txg = txg_hold_open(zilog->zl_dmu_pool, &lwb->lwb_txgh);
696789Sahrens 	txg_rele_to_quiesce(&lwb->lwb_txgh);
697789Sahrens 
698789Sahrens 	/*
6991141Sperrin 	 * Pick a ZIL blocksize. We request a size that is the
7001141Sperrin 	 * maximum of the previous used size, the current used size and
7011141Sperrin 	 * the amount waiting in the queue.
702789Sahrens 	 */
7032237Smaybee 	zil_blksz = MAX(zilog->zl_prev_used,
7042237Smaybee 	    zilog->zl_cur_used + sizeof (*ztp));
7051141Sperrin 	zil_blksz = MAX(zil_blksz, zilog->zl_itx_list_sz + sizeof (*ztp));
7061842Sperrin 	zil_blksz = P2ROUNDUP_TYPED(zil_blksz, ZIL_MIN_BLKSZ, uint64_t);
7071141Sperrin 	if (zil_blksz > ZIL_MAX_BLKSZ)
7081141Sperrin 		zil_blksz = ZIL_MAX_BLKSZ;
709789Sahrens 
7103063Sperrin 	BP_ZERO(bp);
7113063Sperrin 	/* pass the old blkptr in order to spread log blocks across devs */
7123063Sperrin 	error = zio_alloc_blk(spa, zil_blksz, bp, &lwb->lwb_blk, txg);
713789Sahrens 	if (error) {
7143668Sgw25295 		dmu_tx_t *tx = dmu_tx_create_assigned(zilog->zl_dmu_pool, txg);
7153668Sgw25295 
7161544Seschrock 		/*
7173668Sgw25295 		 * We dirty the dataset to ensure that zil_sync() will
7183668Sgw25295 		 * be called to remove this lwb from our zl_lwb_list.
7193668Sgw25295 		 * Failing to do so, may leave an lwb with a NULL lwb_buf
7203668Sgw25295 		 * hanging around on the zl_lwb_list.
7213668Sgw25295 		 */
7223668Sgw25295 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
7233848Sgw25295 		dmu_tx_commit(tx);
7243668Sgw25295 
7253668Sgw25295 		/*
7263668Sgw25295 		 * Since we've just experienced an allocation failure so we
7273668Sgw25295 		 * terminate the current lwb and send it on its way.
7283668Sgw25295 		 */
7293668Sgw25295 		ztp->zit_pad = 0;
7303668Sgw25295 		ztp->zit_nused = lwb->lwb_nused;
7313668Sgw25295 		ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum;
7323668Sgw25295 		zio_nowait(lwb->lwb_zio);
7333668Sgw25295 
7343668Sgw25295 		/*
7351544Seschrock 		 * By returning NULL the caller will call tx_wait_synced()
7361544Seschrock 		 */
737789Sahrens 		return (NULL);
738789Sahrens 	}
739789Sahrens 
7401807Sbonwick 	ASSERT3U(bp->blk_birth, ==, txg);
7411544Seschrock 	ztp->zit_pad = 0;
742789Sahrens 	ztp->zit_nused = lwb->lwb_nused;
743789Sahrens 	ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum;
7441807Sbonwick 	bp->blk_cksum = lwb->lwb_blk.blk_cksum;
7451807Sbonwick 	bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++;
746789Sahrens 
747789Sahrens 	/*
748789Sahrens 	 * Allocate a new log write buffer (lwb).
749789Sahrens 	 */
750789Sahrens 	nlwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP);
751789Sahrens 
752789Sahrens 	nlwb->lwb_zilog = zilog;
7531807Sbonwick 	nlwb->lwb_blk = *bp;
754789Sahrens 	nlwb->lwb_nused = 0;
755789Sahrens 	nlwb->lwb_sz = BP_GET_LSIZE(&nlwb->lwb_blk);
756789Sahrens 	nlwb->lwb_buf = zio_buf_alloc(nlwb->lwb_sz);
757789Sahrens 	nlwb->lwb_max_txg = txg;
7582237Smaybee 	nlwb->lwb_zio = NULL;
759789Sahrens 
760789Sahrens 	/*
7613063Sperrin 	 * Put new lwb at the end of the log chain
762789Sahrens 	 */
763789Sahrens 	mutex_enter(&zilog->zl_lock);
764789Sahrens 	list_insert_tail(&zilog->zl_lwb_list, nlwb);
7653063Sperrin 	mutex_exit(&zilog->zl_lock);
7663063Sperrin 
7675688Sbonwick 	/* Record the block for later vdev flushing */
7685688Sbonwick 	zil_add_block(zilog, &lwb->lwb_blk);
769789Sahrens 
770789Sahrens 	/*
7712237Smaybee 	 * kick off the write for the old log block
772789Sahrens 	 */
7732237Smaybee 	dprintf_bp(&lwb->lwb_blk, "lwb %p txg %llu: ", lwb, txg);
7743063Sperrin 	ASSERT(lwb->lwb_zio);
7752237Smaybee 	zio_nowait(lwb->lwb_zio);
776789Sahrens 
777789Sahrens 	return (nlwb);
778789Sahrens }
779789Sahrens 
780789Sahrens static lwb_t *
781789Sahrens zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb)
782789Sahrens {
783789Sahrens 	lr_t *lrc = &itx->itx_lr; /* common log record */
7842237Smaybee 	lr_write_t *lr = (lr_write_t *)lrc;
785789Sahrens 	uint64_t txg = lrc->lrc_txg;
786789Sahrens 	uint64_t reclen = lrc->lrc_reclen;
7872237Smaybee 	uint64_t dlen;
788789Sahrens 
789789Sahrens 	if (lwb == NULL)
790789Sahrens 		return (NULL);
791789Sahrens 	ASSERT(lwb->lwb_buf != NULL);
792789Sahrens 
7932237Smaybee 	if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY)
7942237Smaybee 		dlen = P2ROUNDUP_TYPED(
7952237Smaybee 		    lr->lr_length, sizeof (uint64_t), uint64_t);
7962237Smaybee 	else
7972237Smaybee 		dlen = 0;
7981669Sperrin 
7991669Sperrin 	zilog->zl_cur_used += (reclen + dlen);
8001669Sperrin 
8013063Sperrin 	zil_lwb_write_init(zilog, lwb);
8023063Sperrin 
8031669Sperrin 	/*
8041669Sperrin 	 * If this record won't fit in the current log block, start a new one.
8051669Sperrin 	 */
8061669Sperrin 	if (lwb->lwb_nused + reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) {
8071669Sperrin 		lwb = zil_lwb_write_start(zilog, lwb);
8082237Smaybee 		if (lwb == NULL)
8091669Sperrin 			return (NULL);
8103063Sperrin 		zil_lwb_write_init(zilog, lwb);
8111669Sperrin 		ASSERT(lwb->lwb_nused == 0);
8121669Sperrin 		if (reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) {
8131669Sperrin 			txg_wait_synced(zilog->zl_dmu_pool, txg);
814789Sahrens 			return (lwb);
815789Sahrens 		}
816789Sahrens 	}
817789Sahrens 
8182638Sperrin 	/*
8192638Sperrin 	 * Update the lrc_seq, to be log record sequence number. See zil.h
8202638Sperrin 	 * Then copy the record to the log buffer.
8212638Sperrin 	 */
8222638Sperrin 	lrc->lrc_seq = ++zilog->zl_lr_seq; /* we are single threaded */
823789Sahrens 	bcopy(lrc, lwb->lwb_buf + lwb->lwb_nused, reclen);
8242237Smaybee 
8252237Smaybee 	/*
8262237Smaybee 	 * If it's a write, fetch the data or get its blkptr as appropriate.
8272237Smaybee 	 */
8282237Smaybee 	if (lrc->lrc_txtype == TX_WRITE) {
8292237Smaybee 		if (txg > spa_freeze_txg(zilog->zl_spa))
8302237Smaybee 			txg_wait_synced(zilog->zl_dmu_pool, txg);
8312237Smaybee 		if (itx->itx_wr_state != WR_COPIED) {
8322237Smaybee 			char *dbuf;
8332237Smaybee 			int error;
8342237Smaybee 
8352237Smaybee 			/* alignment is guaranteed */
8362237Smaybee 			lr = (lr_write_t *)(lwb->lwb_buf + lwb->lwb_nused);
8372237Smaybee 			if (dlen) {
8382237Smaybee 				ASSERT(itx->itx_wr_state == WR_NEED_COPY);
8392237Smaybee 				dbuf = lwb->lwb_buf + lwb->lwb_nused + reclen;
8402237Smaybee 				lr->lr_common.lrc_reclen += dlen;
8412237Smaybee 			} else {
8422237Smaybee 				ASSERT(itx->itx_wr_state == WR_INDIRECT);
8432237Smaybee 				dbuf = NULL;
8442237Smaybee 			}
8452237Smaybee 			error = zilog->zl_get_data(
8462237Smaybee 			    itx->itx_private, lr, dbuf, lwb->lwb_zio);
8472237Smaybee 			if (error) {
8482237Smaybee 				ASSERT(error == ENOENT || error == EEXIST ||
8492237Smaybee 				    error == EALREADY);
8502237Smaybee 				return (lwb);
8512237Smaybee 			}
8522237Smaybee 		}
8531669Sperrin 	}
8542237Smaybee 
8552237Smaybee 	lwb->lwb_nused += reclen + dlen;
856789Sahrens 	lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg);
857789Sahrens 	ASSERT3U(lwb->lwb_nused, <=, ZIL_BLK_DATA_SZ(lwb));
858789Sahrens 	ASSERT3U(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)), ==, 0);
859789Sahrens 
860789Sahrens 	return (lwb);
861789Sahrens }
862789Sahrens 
863789Sahrens itx_t *
8645331Samw zil_itx_create(uint64_t txtype, size_t lrsize)
865789Sahrens {
866789Sahrens 	itx_t *itx;
867789Sahrens 
8681842Sperrin 	lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t);
869789Sahrens 
870789Sahrens 	itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP);
871789Sahrens 	itx->itx_lr.lrc_txtype = txtype;
872789Sahrens 	itx->itx_lr.lrc_reclen = lrsize;
8736101Sperrin 	itx->itx_sod = lrsize; /* if write & WR_NEED_COPY will be increased */
874789Sahrens 	itx->itx_lr.lrc_seq = 0;	/* defensive */
875789Sahrens 
876789Sahrens 	return (itx);
877789Sahrens }
878789Sahrens 
879789Sahrens uint64_t
880789Sahrens zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx)
881789Sahrens {
882789Sahrens 	uint64_t seq;
883789Sahrens 
884789Sahrens 	ASSERT(itx->itx_lr.lrc_seq == 0);
885789Sahrens 
886789Sahrens 	mutex_enter(&zilog->zl_lock);
887789Sahrens 	list_insert_tail(&zilog->zl_itx_list, itx);
8886101Sperrin 	zilog->zl_itx_list_sz += itx->itx_sod;
889789Sahrens 	itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx);
890789Sahrens 	itx->itx_lr.lrc_seq = seq = ++zilog->zl_itx_seq;
891789Sahrens 	mutex_exit(&zilog->zl_lock);
892789Sahrens 
893789Sahrens 	return (seq);
894789Sahrens }
895789Sahrens 
896789Sahrens /*
897789Sahrens  * Free up all in-memory intent log transactions that have now been synced.
898789Sahrens  */
899789Sahrens static void
900789Sahrens zil_itx_clean(zilog_t *zilog)
901789Sahrens {
902789Sahrens 	uint64_t synced_txg = spa_last_synced_txg(zilog->zl_spa);
903789Sahrens 	uint64_t freeze_txg = spa_freeze_txg(zilog->zl_spa);
9043778Sjohansen 	list_t clean_list;
905789Sahrens 	itx_t *itx;
906789Sahrens 
9073778Sjohansen 	list_create(&clean_list, sizeof (itx_t), offsetof(itx_t, itx_node));
9083778Sjohansen 
909789Sahrens 	mutex_enter(&zilog->zl_lock);
9102638Sperrin 	/* wait for a log writer to finish walking list */
9112638Sperrin 	while (zilog->zl_writer) {
9122638Sperrin 		cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
9132638Sperrin 	}
9143778Sjohansen 
9153778Sjohansen 	/*
9163778Sjohansen 	 * Move the sync'd log transactions to a separate list so we can call
9173778Sjohansen 	 * kmem_free without holding the zl_lock.
9183778Sjohansen 	 *
9193778Sjohansen 	 * There is no need to set zl_writer as we don't drop zl_lock here
9203778Sjohansen 	 */
921789Sahrens 	while ((itx = list_head(&zilog->zl_itx_list)) != NULL &&
922789Sahrens 	    itx->itx_lr.lrc_txg <= MIN(synced_txg, freeze_txg)) {
923789Sahrens 		list_remove(&zilog->zl_itx_list, itx);
9246101Sperrin 		zilog->zl_itx_list_sz -= itx->itx_sod;
9253778Sjohansen 		list_insert_tail(&clean_list, itx);
9263778Sjohansen 	}
9273778Sjohansen 	cv_broadcast(&zilog->zl_cv_writer);
9283778Sjohansen 	mutex_exit(&zilog->zl_lock);
9293778Sjohansen 
9303778Sjohansen 	/* destroy sync'd log transactions */
9313778Sjohansen 	while ((itx = list_head(&clean_list)) != NULL) {
9323778Sjohansen 		list_remove(&clean_list, itx);
933789Sahrens 		kmem_free(itx, offsetof(itx_t, itx_lr)
934789Sahrens 		    + itx->itx_lr.lrc_reclen);
935789Sahrens 	}
9363778Sjohansen 	list_destroy(&clean_list);
937789Sahrens }
938789Sahrens 
9392638Sperrin /*
9403063Sperrin  * If there are any in-memory intent log transactions which have now been
9413063Sperrin  * synced then start up a taskq to free them.
9422638Sperrin  */
943789Sahrens void
944789Sahrens zil_clean(zilog_t *zilog)
945789Sahrens {
9463063Sperrin 	itx_t *itx;
9473063Sperrin 
948789Sahrens 	mutex_enter(&zilog->zl_lock);
9493063Sperrin 	itx = list_head(&zilog->zl_itx_list);
9503063Sperrin 	if ((itx != NULL) &&
9513063Sperrin 	    (itx->itx_lr.lrc_txg <= spa_last_synced_txg(zilog->zl_spa))) {
952789Sahrens 		(void) taskq_dispatch(zilog->zl_clean_taskq,
953789Sahrens 		    (void (*)(void *))zil_itx_clean, zilog, TQ_NOSLEEP);
9543063Sperrin 	}
955789Sahrens 	mutex_exit(&zilog->zl_lock);
956789Sahrens }
957789Sahrens 
958789Sahrens void
9592638Sperrin zil_commit_writer(zilog_t *zilog, uint64_t seq, uint64_t foid)
960789Sahrens {
961789Sahrens 	uint64_t txg;
9623063Sperrin 	uint64_t commit_seq = 0;
9632638Sperrin 	itx_t *itx, *itx_next = (itx_t *)-1;
964789Sahrens 	lwb_t *lwb;
965789Sahrens 	spa_t *spa;
966789Sahrens 
9672638Sperrin 	zilog->zl_writer = B_TRUE;
9682638Sperrin 	zilog->zl_root_zio = NULL;
969789Sahrens 	spa = zilog->zl_spa;
970789Sahrens 
971789Sahrens 	if (zilog->zl_suspend) {
972789Sahrens 		lwb = NULL;
973789Sahrens 	} else {
974789Sahrens 		lwb = list_tail(&zilog->zl_lwb_list);
975789Sahrens 		if (lwb == NULL) {
9762638Sperrin 			/*
9772638Sperrin 			 * Return if there's nothing to flush before we
9782638Sperrin 			 * dirty the fs by calling zil_create()
9792638Sperrin 			 */
9802638Sperrin 			if (list_is_empty(&zilog->zl_itx_list)) {
9812638Sperrin 				zilog->zl_writer = B_FALSE;
9822638Sperrin 				return;
9832638Sperrin 			}
984789Sahrens 			mutex_exit(&zilog->zl_lock);
985789Sahrens 			zil_create(zilog);
986789Sahrens 			mutex_enter(&zilog->zl_lock);
987789Sahrens 			lwb = list_tail(&zilog->zl_lwb_list);
988789Sahrens 		}
989789Sahrens 	}
990789Sahrens 
9913063Sperrin 	/* Loop through in-memory log transactions filling log blocks. */
9922638Sperrin 	DTRACE_PROBE1(zil__cw1, zilog_t *, zilog);
993789Sahrens 	for (;;) {
9942638Sperrin 		/*
9952638Sperrin 		 * Find the next itx to push:
9962638Sperrin 		 * Push all transactions related to specified foid and all
9972638Sperrin 		 * other transactions except TX_WRITE, TX_TRUNCATE,
9982638Sperrin 		 * TX_SETATTR and TX_ACL for all other files.
9992638Sperrin 		 */
10002638Sperrin 		if (itx_next != (itx_t *)-1)
10012638Sperrin 			itx = itx_next;
10022638Sperrin 		else
10032638Sperrin 			itx = list_head(&zilog->zl_itx_list);
10042638Sperrin 		for (; itx != NULL; itx = list_next(&zilog->zl_itx_list, itx)) {
10052638Sperrin 			if (foid == 0) /* push all foids? */
10062638Sperrin 				break;
10073063Sperrin 			if (itx->itx_sync) /* push all O_[D]SYNC */
10083063Sperrin 				break;
10092638Sperrin 			switch (itx->itx_lr.lrc_txtype) {
10102638Sperrin 			case TX_SETATTR:
10112638Sperrin 			case TX_WRITE:
10122638Sperrin 			case TX_TRUNCATE:
10132638Sperrin 			case TX_ACL:
10142638Sperrin 				/* lr_foid is same offset for these records */
10152638Sperrin 				if (((lr_write_t *)&itx->itx_lr)->lr_foid
10162638Sperrin 				    != foid) {
10172638Sperrin 					continue; /* skip this record */
10182638Sperrin 				}
10192638Sperrin 			}
10202638Sperrin 			break;
10212638Sperrin 		}
1022789Sahrens 		if (itx == NULL)
1023789Sahrens 			break;
1024789Sahrens 
1025789Sahrens 		if ((itx->itx_lr.lrc_seq > seq) &&
10262638Sperrin 		    ((lwb == NULL) || (lwb->lwb_nused == 0) ||
10276101Sperrin 		    (lwb->lwb_nused + itx->itx_sod > ZIL_BLK_DATA_SZ(lwb)))) {
1028789Sahrens 			break;
10293063Sperrin 		}
1030789Sahrens 
10312638Sperrin 		/*
10322638Sperrin 		 * Save the next pointer.  Even though we soon drop
10332638Sperrin 		 * zl_lock all threads that may change the list
10342638Sperrin 		 * (another writer or zil_itx_clean) can't do so until
10352638Sperrin 		 * they have zl_writer.
10362638Sperrin 		 */
10372638Sperrin 		itx_next = list_next(&zilog->zl_itx_list, itx);
1038789Sahrens 		list_remove(&zilog->zl_itx_list, itx);
10396101Sperrin 		zilog->zl_itx_list_sz -= itx->itx_sod;
10403063Sperrin 		mutex_exit(&zilog->zl_lock);
1041789Sahrens 		txg = itx->itx_lr.lrc_txg;
1042789Sahrens 		ASSERT(txg);
1043789Sahrens 
1044789Sahrens 		if (txg > spa_last_synced_txg(spa) ||
1045789Sahrens 		    txg > spa_freeze_txg(spa))
1046789Sahrens 			lwb = zil_lwb_commit(zilog, itx, lwb);
1047789Sahrens 		kmem_free(itx, offsetof(itx_t, itx_lr)
1048789Sahrens 		    + itx->itx_lr.lrc_reclen);
1049789Sahrens 		mutex_enter(&zilog->zl_lock);
1050789Sahrens 	}
10512638Sperrin 	DTRACE_PROBE1(zil__cw2, zilog_t *, zilog);
10523063Sperrin 	/* determine commit sequence number */
10533063Sperrin 	itx = list_head(&zilog->zl_itx_list);
10543063Sperrin 	if (itx)
10553063Sperrin 		commit_seq = itx->itx_lr.lrc_seq;
10563063Sperrin 	else
10573063Sperrin 		commit_seq = zilog->zl_itx_seq;
1058789Sahrens 	mutex_exit(&zilog->zl_lock);
1059789Sahrens 
1060789Sahrens 	/* write the last block out */
10613063Sperrin 	if (lwb != NULL && lwb->lwb_zio != NULL)
1062789Sahrens 		lwb = zil_lwb_write_start(zilog, lwb);
1063789Sahrens 
10641141Sperrin 	zilog->zl_prev_used = zilog->zl_cur_used;
10651141Sperrin 	zilog->zl_cur_used = 0;
10661141Sperrin 
10672638Sperrin 	/*
10682638Sperrin 	 * Wait if necessary for the log blocks to be on stable storage.
10692638Sperrin 	 */
10702638Sperrin 	if (zilog->zl_root_zio) {
10712638Sperrin 		DTRACE_PROBE1(zil__cw3, zilog_t *, zilog);
10722638Sperrin 		(void) zio_wait(zilog->zl_root_zio);
10732638Sperrin 		DTRACE_PROBE1(zil__cw4, zilog_t *, zilog);
10745688Sbonwick 		zil_flush_vdevs(zilog);
1075789Sahrens 	}
10761141Sperrin 
1077789Sahrens 	if (zilog->zl_log_error || lwb == NULL) {
1078789Sahrens 		zilog->zl_log_error = 0;
1079789Sahrens 		txg_wait_synced(zilog->zl_dmu_pool, 0);
1080789Sahrens 	}
10813063Sperrin 
10823063Sperrin 	mutex_enter(&zilog->zl_lock);
10831141Sperrin 	zilog->zl_writer = B_FALSE;
10843063Sperrin 
10853063Sperrin 	ASSERT3U(commit_seq, >=, zilog->zl_commit_seq);
10863063Sperrin 	zilog->zl_commit_seq = commit_seq;
10872638Sperrin }
10882638Sperrin 
10892638Sperrin /*
10902638Sperrin  * Push zfs transactions to stable storage up to the supplied sequence number.
10912638Sperrin  * If foid is 0 push out all transactions, otherwise push only those
10922638Sperrin  * for that file or might have been used to create that file.
10932638Sperrin  */
10942638Sperrin void
10952638Sperrin zil_commit(zilog_t *zilog, uint64_t seq, uint64_t foid)
10962638Sperrin {
10972638Sperrin 	if (zilog == NULL || seq == 0)
10982638Sperrin 		return;
10992638Sperrin 
11002638Sperrin 	mutex_enter(&zilog->zl_lock);
11012638Sperrin 
11022638Sperrin 	seq = MIN(seq, zilog->zl_itx_seq);	/* cap seq at largest itx seq */
11032638Sperrin 
11043063Sperrin 	while (zilog->zl_writer) {
11052638Sperrin 		cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
11063063Sperrin 		if (seq < zilog->zl_commit_seq) {
11073063Sperrin 			mutex_exit(&zilog->zl_lock);
11083063Sperrin 			return;
11093063Sperrin 		}
11103063Sperrin 	}
11112638Sperrin 	zil_commit_writer(zilog, seq, foid); /* drops zl_lock */
11123063Sperrin 	/* wake up others waiting on the commit */
11133063Sperrin 	cv_broadcast(&zilog->zl_cv_writer);
11143063Sperrin 	mutex_exit(&zilog->zl_lock);
1115789Sahrens }
1116789Sahrens 
1117789Sahrens /*
1118789Sahrens  * Called in syncing context to free committed log blocks and update log header.
1119789Sahrens  */
1120789Sahrens void
1121789Sahrens zil_sync(zilog_t *zilog, dmu_tx_t *tx)
1122789Sahrens {
11231807Sbonwick 	zil_header_t *zh = zil_header_in_syncing_context(zilog);
1124789Sahrens 	uint64_t txg = dmu_tx_get_txg(tx);
1125789Sahrens 	spa_t *spa = zilog->zl_spa;
1126789Sahrens 	lwb_t *lwb;
1127789Sahrens 
11281807Sbonwick 	mutex_enter(&zilog->zl_lock);
11291807Sbonwick 
1130789Sahrens 	ASSERT(zilog->zl_stop_sync == 0);
1131789Sahrens 
11321807Sbonwick 	zh->zh_replay_seq = zilog->zl_replay_seq[txg & TXG_MASK];
1133789Sahrens 
1134789Sahrens 	if (zilog->zl_destroy_txg == txg) {
11351807Sbonwick 		blkptr_t blk = zh->zh_log;
11361807Sbonwick 
11371807Sbonwick 		ASSERT(list_head(&zilog->zl_lwb_list) == NULL);
11381807Sbonwick 		ASSERT(spa_sync_pass(spa) == 1);
11391807Sbonwick 
11401807Sbonwick 		bzero(zh, sizeof (zil_header_t));
1141789Sahrens 		bzero(zilog->zl_replay_seq, sizeof (zilog->zl_replay_seq));
11421807Sbonwick 
11431807Sbonwick 		if (zilog->zl_keep_first) {
11441807Sbonwick 			/*
11451807Sbonwick 			 * If this block was part of log chain that couldn't
11461807Sbonwick 			 * be claimed because a device was missing during
11471807Sbonwick 			 * zil_claim(), but that device later returns,
11481807Sbonwick 			 * then this block could erroneously appear valid.
11491807Sbonwick 			 * To guard against this, assign a new GUID to the new
11501807Sbonwick 			 * log chain so it doesn't matter what blk points to.
11511807Sbonwick 			 */
11521807Sbonwick 			zil_init_log_chain(zilog, &blk);
11531807Sbonwick 			zh->zh_log = blk;
11541807Sbonwick 		}
1155789Sahrens 	}
1156789Sahrens 
1157789Sahrens 	for (;;) {
1158789Sahrens 		lwb = list_head(&zilog->zl_lwb_list);
1159789Sahrens 		if (lwb == NULL) {
1160789Sahrens 			mutex_exit(&zilog->zl_lock);
1161789Sahrens 			return;
1162789Sahrens 		}
11632638Sperrin 		zh->zh_log = lwb->lwb_blk;
1164789Sahrens 		if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg)
1165789Sahrens 			break;
1166789Sahrens 		list_remove(&zilog->zl_lwb_list, lwb);
1167789Sahrens 		zio_free_blk(spa, &lwb->lwb_blk, txg);
1168789Sahrens 		kmem_cache_free(zil_lwb_cache, lwb);
11693668Sgw25295 
11703668Sgw25295 		/*
11713668Sgw25295 		 * If we don't have anything left in the lwb list then
11723668Sgw25295 		 * we've had an allocation failure and we need to zero
11733668Sgw25295 		 * out the zil_header blkptr so that we don't end
11743668Sgw25295 		 * up freeing the same block twice.
11753668Sgw25295 		 */
11763668Sgw25295 		if (list_head(&zilog->zl_lwb_list) == NULL)
11773668Sgw25295 			BP_ZERO(&zh->zh_log);
1178789Sahrens 	}
1179789Sahrens 	mutex_exit(&zilog->zl_lock);
1180789Sahrens }
1181789Sahrens 
1182789Sahrens void
1183789Sahrens zil_init(void)
1184789Sahrens {
1185789Sahrens 	zil_lwb_cache = kmem_cache_create("zil_lwb_cache",
11862856Snd150628 	    sizeof (struct lwb), 0, NULL, NULL, NULL, NULL, NULL, 0);
1187789Sahrens }
1188789Sahrens 
1189789Sahrens void
1190789Sahrens zil_fini(void)
1191789Sahrens {
1192789Sahrens 	kmem_cache_destroy(zil_lwb_cache);
1193789Sahrens }
1194789Sahrens 
1195789Sahrens zilog_t *
1196789Sahrens zil_alloc(objset_t *os, zil_header_t *zh_phys)
1197789Sahrens {
1198789Sahrens 	zilog_t *zilog;
1199789Sahrens 
1200789Sahrens 	zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP);
1201789Sahrens 
1202789Sahrens 	zilog->zl_header = zh_phys;
1203789Sahrens 	zilog->zl_os = os;
1204789Sahrens 	zilog->zl_spa = dmu_objset_spa(os);
1205789Sahrens 	zilog->zl_dmu_pool = dmu_objset_pool(os);
12061807Sbonwick 	zilog->zl_destroy_txg = TXG_INITIAL - 1;
1207789Sahrens 
12082856Snd150628 	mutex_init(&zilog->zl_lock, NULL, MUTEX_DEFAULT, NULL);
12092856Snd150628 
1210789Sahrens 	list_create(&zilog->zl_itx_list, sizeof (itx_t),
1211789Sahrens 	    offsetof(itx_t, itx_node));
1212789Sahrens 
1213789Sahrens 	list_create(&zilog->zl_lwb_list, sizeof (lwb_t),
1214789Sahrens 	    offsetof(lwb_t, lwb_node));
1215789Sahrens 
12165688Sbonwick 	mutex_init(&zilog->zl_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
12175688Sbonwick 
12185688Sbonwick 	avl_create(&zilog->zl_vdev_tree, zil_vdev_compare,
12195688Sbonwick 	    sizeof (zil_vdev_node_t), offsetof(zil_vdev_node_t, zv_node));
1220789Sahrens 
12215913Sperrin 	cv_init(&zilog->zl_cv_writer, NULL, CV_DEFAULT, NULL);
12225913Sperrin 	cv_init(&zilog->zl_cv_suspend, NULL, CV_DEFAULT, NULL);
12235913Sperrin 
1224789Sahrens 	return (zilog);
1225789Sahrens }
1226789Sahrens 
1227789Sahrens void
1228789Sahrens zil_free(zilog_t *zilog)
1229789Sahrens {
1230789Sahrens 	lwb_t *lwb;
1231789Sahrens 
1232789Sahrens 	zilog->zl_stop_sync = 1;
1233789Sahrens 
1234789Sahrens 	while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
1235789Sahrens 		list_remove(&zilog->zl_lwb_list, lwb);
1236789Sahrens 		if (lwb->lwb_buf != NULL)
1237789Sahrens 			zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
1238789Sahrens 		kmem_cache_free(zil_lwb_cache, lwb);
1239789Sahrens 	}
1240789Sahrens 	list_destroy(&zilog->zl_lwb_list);
1241789Sahrens 
12425688Sbonwick 	avl_destroy(&zilog->zl_vdev_tree);
12435688Sbonwick 	mutex_destroy(&zilog->zl_vdev_lock);
1244789Sahrens 
1245789Sahrens 	ASSERT(list_head(&zilog->zl_itx_list) == NULL);
1246789Sahrens 	list_destroy(&zilog->zl_itx_list);
12472856Snd150628 	mutex_destroy(&zilog->zl_lock);
1248789Sahrens 
12495913Sperrin 	cv_destroy(&zilog->zl_cv_writer);
12505913Sperrin 	cv_destroy(&zilog->zl_cv_suspend);
12515913Sperrin 
1252789Sahrens 	kmem_free(zilog, sizeof (zilog_t));
1253789Sahrens }
1254789Sahrens 
1255789Sahrens /*
12561646Sperrin  * return true if the initial log block is not valid
12571362Sperrin  */
12581362Sperrin static int
12591362Sperrin zil_empty(zilog_t *zilog)
12601362Sperrin {
12611807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
12621807Sbonwick 	arc_buf_t *abuf = NULL;
12631362Sperrin 
12641807Sbonwick 	if (BP_IS_HOLE(&zh->zh_log))
12651362Sperrin 		return (1);
12661362Sperrin 
12671807Sbonwick 	if (zil_read_log_block(zilog, &zh->zh_log, &abuf) != 0)
12681807Sbonwick 		return (1);
12691807Sbonwick 
12701807Sbonwick 	VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
12711807Sbonwick 	return (0);
12721362Sperrin }
12731362Sperrin 
12741362Sperrin /*
1275789Sahrens  * Open an intent log.
1276789Sahrens  */
1277789Sahrens zilog_t *
1278789Sahrens zil_open(objset_t *os, zil_get_data_t *get_data)
1279789Sahrens {
1280789Sahrens 	zilog_t *zilog = dmu_objset_zil(os);
1281789Sahrens 
1282789Sahrens 	zilog->zl_get_data = get_data;
1283789Sahrens 	zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri,
1284789Sahrens 	    2, 2, TASKQ_PREPOPULATE);
1285789Sahrens 
1286789Sahrens 	return (zilog);
1287789Sahrens }
1288789Sahrens 
1289789Sahrens /*
1290789Sahrens  * Close an intent log.
1291789Sahrens  */
1292789Sahrens void
1293789Sahrens zil_close(zilog_t *zilog)
1294789Sahrens {
12951807Sbonwick 	/*
12961807Sbonwick 	 * If the log isn't already committed, mark the objset dirty
12971807Sbonwick 	 * (so zil_sync() will be called) and wait for that txg to sync.
12981807Sbonwick 	 */
12991807Sbonwick 	if (!zil_is_committed(zilog)) {
13001807Sbonwick 		uint64_t txg;
13011807Sbonwick 		dmu_tx_t *tx = dmu_tx_create(zilog->zl_os);
13021807Sbonwick 		(void) dmu_tx_assign(tx, TXG_WAIT);
13031807Sbonwick 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
13041807Sbonwick 		txg = dmu_tx_get_txg(tx);
13051807Sbonwick 		dmu_tx_commit(tx);
13061807Sbonwick 		txg_wait_synced(zilog->zl_dmu_pool, txg);
13071807Sbonwick 	}
13081807Sbonwick 
1309789Sahrens 	taskq_destroy(zilog->zl_clean_taskq);
1310789Sahrens 	zilog->zl_clean_taskq = NULL;
1311789Sahrens 	zilog->zl_get_data = NULL;
1312789Sahrens 
1313789Sahrens 	zil_itx_clean(zilog);
1314789Sahrens 	ASSERT(list_head(&zilog->zl_itx_list) == NULL);
1315789Sahrens }
1316789Sahrens 
1317789Sahrens /*
1318789Sahrens  * Suspend an intent log.  While in suspended mode, we still honor
1319789Sahrens  * synchronous semantics, but we rely on txg_wait_synced() to do it.
1320789Sahrens  * We suspend the log briefly when taking a snapshot so that the snapshot
1321789Sahrens  * contains all the data it's supposed to, and has an empty intent log.
1322789Sahrens  */
1323789Sahrens int
1324789Sahrens zil_suspend(zilog_t *zilog)
1325789Sahrens {
13261807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
1327789Sahrens 
1328789Sahrens 	mutex_enter(&zilog->zl_lock);
13291807Sbonwick 	if (zh->zh_claim_txg != 0) {		/* unplayed log */
1330789Sahrens 		mutex_exit(&zilog->zl_lock);
1331789Sahrens 		return (EBUSY);
1332789Sahrens 	}
13331807Sbonwick 	if (zilog->zl_suspend++ != 0) {
13341807Sbonwick 		/*
13351807Sbonwick 		 * Someone else already began a suspend.
13361807Sbonwick 		 * Just wait for them to finish.
13371807Sbonwick 		 */
13381807Sbonwick 		while (zilog->zl_suspending)
13391807Sbonwick 			cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock);
13401807Sbonwick 		mutex_exit(&zilog->zl_lock);
13411807Sbonwick 		return (0);
13421807Sbonwick 	}
13431807Sbonwick 	zilog->zl_suspending = B_TRUE;
1344789Sahrens 	mutex_exit(&zilog->zl_lock);
1345789Sahrens 
13462638Sperrin 	zil_commit(zilog, UINT64_MAX, 0);
1347789Sahrens 
13482638Sperrin 	/*
13492638Sperrin 	 * Wait for any in-flight log writes to complete.
13502638Sperrin 	 */
1351789Sahrens 	mutex_enter(&zilog->zl_lock);
13522638Sperrin 	while (zilog->zl_writer)
13532638Sperrin 		cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
1354789Sahrens 	mutex_exit(&zilog->zl_lock);
1355789Sahrens 
13561807Sbonwick 	zil_destroy(zilog, B_FALSE);
13571807Sbonwick 
13581807Sbonwick 	mutex_enter(&zilog->zl_lock);
13591807Sbonwick 	zilog->zl_suspending = B_FALSE;
13601807Sbonwick 	cv_broadcast(&zilog->zl_cv_suspend);
13611807Sbonwick 	mutex_exit(&zilog->zl_lock);
1362789Sahrens 
1363789Sahrens 	return (0);
1364789Sahrens }
1365789Sahrens 
1366789Sahrens void
1367789Sahrens zil_resume(zilog_t *zilog)
1368789Sahrens {
1369789Sahrens 	mutex_enter(&zilog->zl_lock);
1370789Sahrens 	ASSERT(zilog->zl_suspend != 0);
1371789Sahrens 	zilog->zl_suspend--;
1372789Sahrens 	mutex_exit(&zilog->zl_lock);
1373789Sahrens }
1374789Sahrens 
1375789Sahrens typedef struct zil_replay_arg {
1376789Sahrens 	objset_t	*zr_os;
1377789Sahrens 	zil_replay_func_t **zr_replay;
1378789Sahrens 	void		*zr_arg;
1379789Sahrens 	uint64_t	*zr_txgp;
1380789Sahrens 	boolean_t	zr_byteswap;
1381789Sahrens 	char		*zr_lrbuf;
1382789Sahrens } zil_replay_arg_t;
1383789Sahrens 
1384789Sahrens static void
1385789Sahrens zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg)
1386789Sahrens {
1387789Sahrens 	zil_replay_arg_t *zr = zra;
13881807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
1389789Sahrens 	uint64_t reclen = lr->lrc_reclen;
1390789Sahrens 	uint64_t txtype = lr->lrc_txtype;
13913063Sperrin 	char *name;
13923063Sperrin 	int pass, error, sunk;
1393789Sahrens 
1394789Sahrens 	if (zilog->zl_stop_replay)
1395789Sahrens 		return;
1396789Sahrens 
1397789Sahrens 	if (lr->lrc_txg < claim_txg)		/* already committed */
1398789Sahrens 		return;
1399789Sahrens 
1400789Sahrens 	if (lr->lrc_seq <= zh->zh_replay_seq)	/* already replayed */
1401789Sahrens 		return;
1402789Sahrens 
14035331Samw 	/* Strip case-insensitive bit, still present in log record */
14045331Samw 	txtype &= ~TX_CI;
14055331Samw 
1406789Sahrens 	/*
1407789Sahrens 	 * Make a copy of the data so we can revise and extend it.
1408789Sahrens 	 */
1409789Sahrens 	bcopy(lr, zr->zr_lrbuf, reclen);
1410789Sahrens 
1411789Sahrens 	/*
1412789Sahrens 	 * The log block containing this lr may have been byteswapped
1413789Sahrens 	 * so that we can easily examine common fields like lrc_txtype.
1414789Sahrens 	 * However, the log is a mix of different data types, and only the
1415789Sahrens 	 * replay vectors know how to byteswap their records.  Therefore, if
1416789Sahrens 	 * the lr was byteswapped, undo it before invoking the replay vector.
1417789Sahrens 	 */
1418789Sahrens 	if (zr->zr_byteswap)
1419789Sahrens 		byteswap_uint64_array(zr->zr_lrbuf, reclen);
1420789Sahrens 
1421789Sahrens 	/*
1422789Sahrens 	 * If this is a TX_WRITE with a blkptr, suck in the data.
1423789Sahrens 	 */
1424789Sahrens 	if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) {
1425789Sahrens 		lr_write_t *lrw = (lr_write_t *)lr;
1426789Sahrens 		blkptr_t *wbp = &lrw->lr_blkptr;
1427789Sahrens 		uint64_t wlen = lrw->lr_length;
1428789Sahrens 		char *wbuf = zr->zr_lrbuf + reclen;
1429789Sahrens 
1430789Sahrens 		if (BP_IS_HOLE(wbp)) {	/* compressed to a hole */
1431789Sahrens 			bzero(wbuf, wlen);
1432789Sahrens 		} else {
1433789Sahrens 			/*
1434789Sahrens 			 * A subsequent write may have overwritten this block,
1435789Sahrens 			 * in which case wbp may have been been freed and
1436789Sahrens 			 * reallocated, and our read of wbp may fail with a
1437789Sahrens 			 * checksum error.  We can safely ignore this because
1438789Sahrens 			 * the later write will provide the correct data.
1439789Sahrens 			 */
14401544Seschrock 			zbookmark_t zb;
14411544Seschrock 
14421544Seschrock 			zb.zb_objset = dmu_objset_id(zilog->zl_os);
14431544Seschrock 			zb.zb_object = lrw->lr_foid;
14441544Seschrock 			zb.zb_level = -1;
14451544Seschrock 			zb.zb_blkid = lrw->lr_offset / BP_GET_LSIZE(wbp);
14461544Seschrock 
1447789Sahrens 			(void) zio_wait(zio_read(NULL, zilog->zl_spa,
1448789Sahrens 			    wbp, wbuf, BP_GET_LSIZE(wbp), NULL, NULL,
1449789Sahrens 			    ZIO_PRIORITY_SYNC_READ,
14501544Seschrock 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &zb));
1451789Sahrens 			(void) memmove(wbuf, wbuf + lrw->lr_blkoff, wlen);
1452789Sahrens 		}
1453789Sahrens 	}
1454789Sahrens 
1455789Sahrens 	/*
1456789Sahrens 	 * We must now do two things atomically: replay this log record,
1457789Sahrens 	 * and update the log header to reflect the fact that we did so.
1458789Sahrens 	 * We use the DMU's ability to assign into a specific txg to do this.
1459789Sahrens 	 */
14603063Sperrin 	for (pass = 1, sunk = B_FALSE; /* CONSTANTCONDITION */; pass++) {
1461789Sahrens 		uint64_t replay_txg;
1462789Sahrens 		dmu_tx_t *replay_tx;
1463789Sahrens 
1464789Sahrens 		replay_tx = dmu_tx_create(zr->zr_os);
1465789Sahrens 		error = dmu_tx_assign(replay_tx, TXG_WAIT);
1466789Sahrens 		if (error) {
1467789Sahrens 			dmu_tx_abort(replay_tx);
1468789Sahrens 			break;
1469789Sahrens 		}
1470789Sahrens 
1471789Sahrens 		replay_txg = dmu_tx_get_txg(replay_tx);
1472789Sahrens 
1473789Sahrens 		if (txtype == 0 || txtype >= TX_MAX_TYPE) {
1474789Sahrens 			error = EINVAL;
1475789Sahrens 		} else {
1476789Sahrens 			/*
1477789Sahrens 			 * On the first pass, arrange for the replay vector
1478789Sahrens 			 * to fail its dmu_tx_assign().  That's the only way
1479789Sahrens 			 * to ensure that those code paths remain well tested.
14805676Sperrin 			 *
14815676Sperrin 			 * Only byteswap (if needed) on the 1st pass.
1482789Sahrens 			 */
1483789Sahrens 			*zr->zr_txgp = replay_txg - (pass == 1);
1484789Sahrens 			error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lrbuf,
14855676Sperrin 			    zr->zr_byteswap && pass == 1);
1486789Sahrens 			*zr->zr_txgp = TXG_NOWAIT;
1487789Sahrens 		}
1488789Sahrens 
1489789Sahrens 		if (error == 0) {
1490789Sahrens 			dsl_dataset_dirty(dmu_objset_ds(zr->zr_os), replay_tx);
1491789Sahrens 			zilog->zl_replay_seq[replay_txg & TXG_MASK] =
1492789Sahrens 			    lr->lrc_seq;
1493789Sahrens 		}
1494789Sahrens 
1495789Sahrens 		dmu_tx_commit(replay_tx);
1496789Sahrens 
14973063Sperrin 		if (!error)
14983063Sperrin 			return;
14993063Sperrin 
15003063Sperrin 		/*
15013063Sperrin 		 * The DMU's dnode layer doesn't see removes until the txg
15023063Sperrin 		 * commits, so a subsequent claim can spuriously fail with
15033063Sperrin 		 * EEXIST. So if we receive any error other than ERESTART
15043063Sperrin 		 * we try syncing out any removes then retrying the
15053063Sperrin 		 * transaction.
15063063Sperrin 		 */
15073063Sperrin 		if (error != ERESTART && !sunk) {
15083063Sperrin 			txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0);
15093063Sperrin 			sunk = B_TRUE;
15103063Sperrin 			continue; /* retry */
15113063Sperrin 		}
15123063Sperrin 
1513789Sahrens 		if (error != ERESTART)
1514789Sahrens 			break;
1515789Sahrens 
1516789Sahrens 		if (pass != 1)
1517789Sahrens 			txg_wait_open(spa_get_dsl(zilog->zl_spa),
1518789Sahrens 			    replay_txg + 1);
1519789Sahrens 
1520789Sahrens 		dprintf("pass %d, retrying\n", pass);
1521789Sahrens 	}
1522789Sahrens 
15233063Sperrin 	ASSERT(error && error != ERESTART);
15243063Sperrin 	name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
15253063Sperrin 	dmu_objset_name(zr->zr_os, name);
15263063Sperrin 	cmn_err(CE_WARN, "ZFS replay transaction error %d, "
15275331Samw 	    "dataset %s, seq 0x%llx, txtype %llu %s\n",
15285331Samw 	    error, name, (u_longlong_t)lr->lrc_seq, (u_longlong_t)txtype,
15295331Samw 	    (lr->lrc_txtype & TX_CI) ? "CI" : "");
15303063Sperrin 	zilog->zl_stop_replay = 1;
15313063Sperrin 	kmem_free(name, MAXNAMELEN);
15323063Sperrin }
1533789Sahrens 
15343063Sperrin /* ARGSUSED */
15353063Sperrin static void
15363063Sperrin zil_incr_blks(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
15373063Sperrin {
15383063Sperrin 	zilog->zl_replay_blks++;
1539789Sahrens }
1540789Sahrens 
1541789Sahrens /*
15421362Sperrin  * If this dataset has a non-empty intent log, replay it and destroy it.
1543789Sahrens  */
1544789Sahrens void
1545789Sahrens zil_replay(objset_t *os, void *arg, uint64_t *txgp,
15463461Sahrens 	zil_replay_func_t *replay_func[TX_MAX_TYPE])
1547789Sahrens {
1548789Sahrens 	zilog_t *zilog = dmu_objset_zil(os);
15491807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
15501807Sbonwick 	zil_replay_arg_t zr;
15511362Sperrin 
15521362Sperrin 	if (zil_empty(zilog)) {
15531807Sbonwick 		zil_destroy(zilog, B_TRUE);
15541362Sperrin 		return;
15551362Sperrin 	}
1556789Sahrens 
1557789Sahrens 	zr.zr_os = os;
1558789Sahrens 	zr.zr_replay = replay_func;
1559789Sahrens 	zr.zr_arg = arg;
1560789Sahrens 	zr.zr_txgp = txgp;
15611807Sbonwick 	zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log);
1562789Sahrens 	zr.zr_lrbuf = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP);
1563789Sahrens 
1564789Sahrens 	/*
1565789Sahrens 	 * Wait for in-progress removes to sync before starting replay.
1566789Sahrens 	 */
1567789Sahrens 	txg_wait_synced(zilog->zl_dmu_pool, 0);
1568789Sahrens 
1569789Sahrens 	zilog->zl_stop_replay = 0;
15703063Sperrin 	zilog->zl_replay_time = lbolt;
15713063Sperrin 	ASSERT(zilog->zl_replay_blks == 0);
15723063Sperrin 	(void) zil_parse(zilog, zil_incr_blks, zil_replay_log_record, &zr,
15731807Sbonwick 	    zh->zh_claim_txg);
1574789Sahrens 	kmem_free(zr.zr_lrbuf, 2 * SPA_MAXBLOCKSIZE);
1575789Sahrens 
15761807Sbonwick 	zil_destroy(zilog, B_FALSE);
15775712Sahrens 	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
1578789Sahrens }
15791646Sperrin 
15801646Sperrin /*
15811646Sperrin  * Report whether all transactions are committed
15821646Sperrin  */
15831646Sperrin int
15841646Sperrin zil_is_committed(zilog_t *zilog)
15851646Sperrin {
15861646Sperrin 	lwb_t *lwb;
15872638Sperrin 	int ret;
15881646Sperrin 
15892638Sperrin 	mutex_enter(&zilog->zl_lock);
15902638Sperrin 	while (zilog->zl_writer)
15912638Sperrin 		cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
15922638Sperrin 
15932638Sperrin 	/* recent unpushed intent log transactions? */
15942638Sperrin 	if (!list_is_empty(&zilog->zl_itx_list)) {
15952638Sperrin 		ret = B_FALSE;
15962638Sperrin 		goto out;
15972638Sperrin 	}
15982638Sperrin 
15992638Sperrin 	/* intent log never used? */
16002638Sperrin 	lwb = list_head(&zilog->zl_lwb_list);
16012638Sperrin 	if (lwb == NULL) {
16022638Sperrin 		ret = B_TRUE;
16032638Sperrin 		goto out;
16042638Sperrin 	}
16051646Sperrin 
16061646Sperrin 	/*
16072638Sperrin 	 * more than 1 log buffer means zil_sync() hasn't yet freed
16082638Sperrin 	 * entries after a txg has committed
16091646Sperrin 	 */
16102638Sperrin 	if (list_next(&zilog->zl_lwb_list, lwb)) {
16112638Sperrin 		ret = B_FALSE;
16122638Sperrin 		goto out;
16132638Sperrin 	}
16142638Sperrin 
16151646Sperrin 	ASSERT(zil_empty(zilog));
16162638Sperrin 	ret = B_TRUE;
16172638Sperrin out:
16182638Sperrin 	cv_broadcast(&zilog->zl_cv_writer);
16192638Sperrin 	mutex_exit(&zilog->zl_lock);
16202638Sperrin 	return (ret);
16211646Sperrin }
1622