xref: /illumos-gate/usr/src/uts/common/fs/zfs/zil.c (revision 17f17c2d)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5fe9cf88cSperrin  * Common Development and Distribution License (the "License").
6fe9cf88cSperrin  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
22893a6d32Sahrens  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
27fa9e4066Sahrens 
28fa9e4066Sahrens #include <sys/zfs_context.h>
29fa9e4066Sahrens #include <sys/spa.h>
30fa9e4066Sahrens #include <sys/dmu.h>
31fa9e4066Sahrens #include <sys/zap.h>
32fa9e4066Sahrens #include <sys/arc.h>
33fa9e4066Sahrens #include <sys/stat.h>
34fa9e4066Sahrens #include <sys/resource.h>
35fa9e4066Sahrens #include <sys/zil.h>
36fa9e4066Sahrens #include <sys/zil_impl.h>
37fa9e4066Sahrens #include <sys/dsl_dataset.h>
38fa9e4066Sahrens #include <sys/vdev.h>
39d63d470bSgw #include <sys/dmu_tx.h>
40fa9e4066Sahrens 
41fa9e4066Sahrens /*
42fa9e4066Sahrens  * The zfs intent log (ZIL) saves transaction records of system calls
43fa9e4066Sahrens  * that change the file system in memory with enough information
44fa9e4066Sahrens  * to be able to replay them. These are stored in memory until
45fa9e4066Sahrens  * either the DMU transaction group (txg) commits them to the stable pool
46fa9e4066Sahrens  * and they can be discarded, or they are flushed to the stable log
47fa9e4066Sahrens  * (also in the pool) due to a fsync, O_DSYNC or other synchronous
48fa9e4066Sahrens  * requirement. In the event of a panic or power fail then those log
49fa9e4066Sahrens  * records (transactions) are replayed.
50fa9e4066Sahrens  *
51fa9e4066Sahrens  * There is one ZIL per file system. Its on-disk (pool) format consists
52fa9e4066Sahrens  * of 3 parts:
53fa9e4066Sahrens  *
54fa9e4066Sahrens  * 	- ZIL header
55fa9e4066Sahrens  * 	- ZIL blocks
56fa9e4066Sahrens  * 	- ZIL records
57fa9e4066Sahrens  *
58fa9e4066Sahrens  * A log record holds a system call transaction. Log blocks can
59fa9e4066Sahrens  * hold many log records and the blocks are chained together.
60fa9e4066Sahrens  * Each ZIL block contains a block pointer (blkptr_t) to the next
61fa9e4066Sahrens  * ZIL block in the chain. The ZIL header points to the first
62fa9e4066Sahrens  * block in the chain. Note there is not a fixed place in the pool
63fa9e4066Sahrens  * to hold blocks. They are dynamically allocated and freed as
64fa9e4066Sahrens  * needed from the blocks available. Figure X shows the ZIL structure:
65fa9e4066Sahrens  */
66fa9e4066Sahrens 
67fa9e4066Sahrens /*
68416e0cd8Sek  * This global ZIL switch affects all pools
69fa9e4066Sahrens  */
70fa9e4066Sahrens int zil_disable = 0;	/* disable intent logging */
71416e0cd8Sek 
72416e0cd8Sek /*
73416e0cd8Sek  * Tunable parameter for debugging or performance analysis.  Setting
74416e0cd8Sek  * zfs_nocacheflush will cause corruption on power loss if a volatile
75416e0cd8Sek  * out-of-order write cache is enabled.
76416e0cd8Sek  */
77416e0cd8Sek boolean_t zfs_nocacheflush = B_FALSE;
78fa9e4066Sahrens 
79fa9e4066Sahrens static kmem_cache_t *zil_lwb_cache;
80fa9e4066Sahrens 
81fa9e4066Sahrens static int
82fa9e4066Sahrens zil_dva_compare(const void *x1, const void *x2)
83fa9e4066Sahrens {
84fa9e4066Sahrens 	const dva_t *dva1 = x1;
85fa9e4066Sahrens 	const dva_t *dva2 = x2;
86fa9e4066Sahrens 
87fa9e4066Sahrens 	if (DVA_GET_VDEV(dva1) < DVA_GET_VDEV(dva2))
88fa9e4066Sahrens 		return (-1);
89fa9e4066Sahrens 	if (DVA_GET_VDEV(dva1) > DVA_GET_VDEV(dva2))
90fa9e4066Sahrens 		return (1);
91fa9e4066Sahrens 
92fa9e4066Sahrens 	if (DVA_GET_OFFSET(dva1) < DVA_GET_OFFSET(dva2))
93fa9e4066Sahrens 		return (-1);
94fa9e4066Sahrens 	if (DVA_GET_OFFSET(dva1) > DVA_GET_OFFSET(dva2))
95fa9e4066Sahrens 		return (1);
96fa9e4066Sahrens 
97fa9e4066Sahrens 	return (0);
98fa9e4066Sahrens }
99fa9e4066Sahrens 
100fa9e4066Sahrens static void
101fa9e4066Sahrens zil_dva_tree_init(avl_tree_t *t)
102fa9e4066Sahrens {
103fa9e4066Sahrens 	avl_create(t, zil_dva_compare, sizeof (zil_dva_node_t),
104fa9e4066Sahrens 	    offsetof(zil_dva_node_t, zn_node));
105fa9e4066Sahrens }
106fa9e4066Sahrens 
107fa9e4066Sahrens static void
108fa9e4066Sahrens zil_dva_tree_fini(avl_tree_t *t)
109fa9e4066Sahrens {
110fa9e4066Sahrens 	zil_dva_node_t *zn;
111fa9e4066Sahrens 	void *cookie = NULL;
112fa9e4066Sahrens 
113fa9e4066Sahrens 	while ((zn = avl_destroy_nodes(t, &cookie)) != NULL)
114fa9e4066Sahrens 		kmem_free(zn, sizeof (zil_dva_node_t));
115fa9e4066Sahrens 
116fa9e4066Sahrens 	avl_destroy(t);
117fa9e4066Sahrens }
118fa9e4066Sahrens 
119fa9e4066Sahrens static int
120fa9e4066Sahrens zil_dva_tree_add(avl_tree_t *t, dva_t *dva)
121fa9e4066Sahrens {
122fa9e4066Sahrens 	zil_dva_node_t *zn;
123fa9e4066Sahrens 	avl_index_t where;
124fa9e4066Sahrens 
125fa9e4066Sahrens 	if (avl_find(t, dva, &where) != NULL)
126fa9e4066Sahrens 		return (EEXIST);
127fa9e4066Sahrens 
128fa9e4066Sahrens 	zn = kmem_alloc(sizeof (zil_dva_node_t), KM_SLEEP);
129fa9e4066Sahrens 	zn->zn_dva = *dva;
130fa9e4066Sahrens 	avl_insert(t, zn, where);
131fa9e4066Sahrens 
132fa9e4066Sahrens 	return (0);
133fa9e4066Sahrens }
134fa9e4066Sahrens 
135d80c45e0Sbonwick static zil_header_t *
136d80c45e0Sbonwick zil_header_in_syncing_context(zilog_t *zilog)
137d80c45e0Sbonwick {
138d80c45e0Sbonwick 	return ((zil_header_t *)zilog->zl_header);
139d80c45e0Sbonwick }
140d80c45e0Sbonwick 
141d80c45e0Sbonwick static void
142d80c45e0Sbonwick zil_init_log_chain(zilog_t *zilog, blkptr_t *bp)
143d80c45e0Sbonwick {
144d80c45e0Sbonwick 	zio_cksum_t *zc = &bp->blk_cksum;
145d80c45e0Sbonwick 
146d80c45e0Sbonwick 	zc->zc_word[ZIL_ZC_GUID_0] = spa_get_random(-1ULL);
147d80c45e0Sbonwick 	zc->zc_word[ZIL_ZC_GUID_1] = spa_get_random(-1ULL);
148d80c45e0Sbonwick 	zc->zc_word[ZIL_ZC_OBJSET] = dmu_objset_id(zilog->zl_os);
149d80c45e0Sbonwick 	zc->zc_word[ZIL_ZC_SEQ] = 1ULL;
150d80c45e0Sbonwick }
151d80c45e0Sbonwick 
152fa9e4066Sahrens /*
153fa9e4066Sahrens  * Read a log block, make sure it's valid, and byteswap it if necessary.
154fa9e4066Sahrens  */
155fa9e4066Sahrens static int
156d80c45e0Sbonwick zil_read_log_block(zilog_t *zilog, const blkptr_t *bp, arc_buf_t **abufpp)
157fa9e4066Sahrens {
158d80c45e0Sbonwick 	blkptr_t blk = *bp;
159ea8dc4b6Seschrock 	zbookmark_t zb;
16013506d1eSmaybee 	uint32_t aflags = ARC_WAIT;
161fa9e4066Sahrens 	int error;
162fa9e4066Sahrens 
163d80c45e0Sbonwick 	zb.zb_objset = bp->blk_cksum.zc_word[ZIL_ZC_OBJSET];
164ea8dc4b6Seschrock 	zb.zb_object = 0;
165ea8dc4b6Seschrock 	zb.zb_level = -1;
166d80c45e0Sbonwick 	zb.zb_blkid = bp->blk_cksum.zc_word[ZIL_ZC_SEQ];
167ea8dc4b6Seschrock 
168d80c45e0Sbonwick 	*abufpp = NULL;
169fa9e4066Sahrens 
170d80c45e0Sbonwick 	error = arc_read(NULL, zilog->zl_spa, &blk, byteswap_uint64_array,
171d80c45e0Sbonwick 	    arc_getbuf_func, abufpp, ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL |
17213506d1eSmaybee 	    ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB, &aflags, &zb);
173fa9e4066Sahrens 
174d80c45e0Sbonwick 	if (error == 0) {
175d80c45e0Sbonwick 		char *data = (*abufpp)->b_data;
176d80c45e0Sbonwick 		uint64_t blksz = BP_GET_LSIZE(bp);
177d80c45e0Sbonwick 		zil_trailer_t *ztp = (zil_trailer_t *)(data + blksz) - 1;
178d80c45e0Sbonwick 		zio_cksum_t cksum = bp->blk_cksum;
179fa9e4066Sahrens 
180d80c45e0Sbonwick 		/*
181d80c45e0Sbonwick 		 * Sequence numbers should be... sequential.  The checksum
182d80c45e0Sbonwick 		 * verifier for the next block should be bp's checksum plus 1.
183d80c45e0Sbonwick 		 */
184d80c45e0Sbonwick 		cksum.zc_word[ZIL_ZC_SEQ]++;
185d80c45e0Sbonwick 
186d80c45e0Sbonwick 		if (bcmp(&cksum, &ztp->zit_next_blk.blk_cksum, sizeof (cksum)))
187d80c45e0Sbonwick 			error = ESTALE;
188d80c45e0Sbonwick 		else if (BP_IS_HOLE(&ztp->zit_next_blk))
189d80c45e0Sbonwick 			error = ENOENT;
190d80c45e0Sbonwick 		else if (ztp->zit_nused > (blksz - sizeof (zil_trailer_t)))
191d80c45e0Sbonwick 			error = EOVERFLOW;
192fa9e4066Sahrens 
193d80c45e0Sbonwick 		if (error) {
194d80c45e0Sbonwick 			VERIFY(arc_buf_remove_ref(*abufpp, abufpp) == 1);
195d80c45e0Sbonwick 			*abufpp = NULL;
196d80c45e0Sbonwick 		}
197fa9e4066Sahrens 	}
198fa9e4066Sahrens 
199d80c45e0Sbonwick 	dprintf("error %d on %llu:%llu\n", error, zb.zb_objset, zb.zb_blkid);
200fa9e4066Sahrens 
201d80c45e0Sbonwick 	return (error);
202fa9e4066Sahrens }
203fa9e4066Sahrens 
204fa9e4066Sahrens /*
205fa9e4066Sahrens  * Parse the intent log, and call parse_func for each valid record within.
206d80c45e0Sbonwick  * Return the highest sequence number.
207fa9e4066Sahrens  */
208d80c45e0Sbonwick uint64_t
209fa9e4066Sahrens zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func,
210fa9e4066Sahrens     zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg)
211fa9e4066Sahrens {
212d80c45e0Sbonwick 	const zil_header_t *zh = zilog->zl_header;
213d80c45e0Sbonwick 	uint64_t claim_seq = zh->zh_claim_seq;
214d80c45e0Sbonwick 	uint64_t seq = 0;
215d80c45e0Sbonwick 	uint64_t max_seq = 0;
216d80c45e0Sbonwick 	blkptr_t blk = zh->zh_log;
217d80c45e0Sbonwick 	arc_buf_t *abuf;
218fa9e4066Sahrens 	char *lrbuf, *lrp;
219fa9e4066Sahrens 	zil_trailer_t *ztp;
220fa9e4066Sahrens 	int reclen, error;
221fa9e4066Sahrens 
222fa9e4066Sahrens 	if (BP_IS_HOLE(&blk))
223d80c45e0Sbonwick 		return (max_seq);
224fa9e4066Sahrens 
225fa9e4066Sahrens 	/*
226fa9e4066Sahrens 	 * Starting at the block pointed to by zh_log we read the log chain.
227fa9e4066Sahrens 	 * For each block in the chain we strongly check that block to
228fa9e4066Sahrens 	 * ensure its validity.  We stop when an invalid block is found.
229fa9e4066Sahrens 	 * For each block pointer in the chain we call parse_blk_func().
230fa9e4066Sahrens 	 * For each record in each valid block we call parse_lr_func().
231d80c45e0Sbonwick 	 * If the log has been claimed, stop if we encounter a sequence
232d80c45e0Sbonwick 	 * number greater than the highest claimed sequence number.
233fa9e4066Sahrens 	 */
234fa9e4066Sahrens 	zil_dva_tree_init(&zilog->zl_dva_tree);
235fa9e4066Sahrens 	for (;;) {
236d80c45e0Sbonwick 		seq = blk.blk_cksum.zc_word[ZIL_ZC_SEQ];
237d80c45e0Sbonwick 
238d80c45e0Sbonwick 		if (claim_seq != 0 && seq > claim_seq)
239d80c45e0Sbonwick 			break;
240d80c45e0Sbonwick 
241d80c45e0Sbonwick 		ASSERT(max_seq < seq);
242d80c45e0Sbonwick 		max_seq = seq;
243d80c45e0Sbonwick 
244d80c45e0Sbonwick 		error = zil_read_log_block(zilog, &blk, &abuf);
245fa9e4066Sahrens 
246fa9e4066Sahrens 		if (parse_blk_func != NULL)
247fa9e4066Sahrens 			parse_blk_func(zilog, &blk, arg, txg);
248fa9e4066Sahrens 
249fa9e4066Sahrens 		if (error)
250fa9e4066Sahrens 			break;
251fa9e4066Sahrens 
252d80c45e0Sbonwick 		lrbuf = abuf->b_data;
253fa9e4066Sahrens 		ztp = (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1;
254fa9e4066Sahrens 		blk = ztp->zit_next_blk;
255fa9e4066Sahrens 
256d80c45e0Sbonwick 		if (parse_lr_func == NULL) {
257d80c45e0Sbonwick 			VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
258fa9e4066Sahrens 			continue;
259d80c45e0Sbonwick 		}
260fa9e4066Sahrens 
261fa9e4066Sahrens 		for (lrp = lrbuf; lrp < lrbuf + ztp->zit_nused; lrp += reclen) {
262fa9e4066Sahrens 			lr_t *lr = (lr_t *)lrp;
263fa9e4066Sahrens 			reclen = lr->lrc_reclen;
264fa9e4066Sahrens 			ASSERT3U(reclen, >=, sizeof (lr_t));
265fa9e4066Sahrens 			parse_lr_func(zilog, lr, arg, txg);
266fa9e4066Sahrens 		}
267d80c45e0Sbonwick 		VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
268fa9e4066Sahrens 	}
269fa9e4066Sahrens 	zil_dva_tree_fini(&zilog->zl_dva_tree);
270d80c45e0Sbonwick 
271d80c45e0Sbonwick 	return (max_seq);
272fa9e4066Sahrens }
273fa9e4066Sahrens 
274fa9e4066Sahrens /* ARGSUSED */
275fa9e4066Sahrens static void
276fa9e4066Sahrens zil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg)
277fa9e4066Sahrens {
278fa9e4066Sahrens 	spa_t *spa = zilog->zl_spa;
279fa9e4066Sahrens 	int err;
280fa9e4066Sahrens 
281fa9e4066Sahrens 	/*
282fa9e4066Sahrens 	 * Claim log block if not already committed and not already claimed.
283fa9e4066Sahrens 	 */
284fa9e4066Sahrens 	if (bp->blk_birth >= first_txg &&
285fa9e4066Sahrens 	    zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp)) == 0) {
286fa9e4066Sahrens 		err = zio_wait(zio_claim(NULL, spa, first_txg, bp, NULL, NULL));
287fa9e4066Sahrens 		ASSERT(err == 0);
288fa9e4066Sahrens 	}
289fa9e4066Sahrens }
290fa9e4066Sahrens 
291fa9e4066Sahrens static void
292fa9e4066Sahrens zil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg)
293fa9e4066Sahrens {
294fa9e4066Sahrens 	if (lrc->lrc_txtype == TX_WRITE) {
295fa9e4066Sahrens 		lr_write_t *lr = (lr_write_t *)lrc;
296fa9e4066Sahrens 		zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg);
297fa9e4066Sahrens 	}
298fa9e4066Sahrens }
299fa9e4066Sahrens 
300fa9e4066Sahrens /* ARGSUSED */
301fa9e4066Sahrens static void
302fa9e4066Sahrens zil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg)
303fa9e4066Sahrens {
304fa9e4066Sahrens 	zio_free_blk(zilog->zl_spa, bp, dmu_tx_get_txg(tx));
305fa9e4066Sahrens }
306fa9e4066Sahrens 
307fa9e4066Sahrens static void
308fa9e4066Sahrens zil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg)
309fa9e4066Sahrens {
310fa9e4066Sahrens 	/*
311fa9e4066Sahrens 	 * If we previously claimed it, we need to free it.
312fa9e4066Sahrens 	 */
313fa9e4066Sahrens 	if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE) {
314fa9e4066Sahrens 		lr_write_t *lr = (lr_write_t *)lrc;
315fa9e4066Sahrens 		blkptr_t *bp = &lr->lr_blkptr;
316fa9e4066Sahrens 		if (bp->blk_birth >= claim_txg &&
317fa9e4066Sahrens 		    !zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp))) {
318fa9e4066Sahrens 			(void) arc_free(NULL, zilog->zl_spa,
319fa9e4066Sahrens 			    dmu_tx_get_txg(tx), bp, NULL, NULL, ARC_WAIT);
320fa9e4066Sahrens 		}
321fa9e4066Sahrens 	}
322fa9e4066Sahrens }
323fa9e4066Sahrens 
324fa9e4066Sahrens /*
325fa9e4066Sahrens  * Create an on-disk intent log.
326fa9e4066Sahrens  */
327fa9e4066Sahrens static void
328fa9e4066Sahrens zil_create(zilog_t *zilog)
329fa9e4066Sahrens {
330d80c45e0Sbonwick 	const zil_header_t *zh = zilog->zl_header;
331fa9e4066Sahrens 	lwb_t *lwb;
332d80c45e0Sbonwick 	uint64_t txg = 0;
333d80c45e0Sbonwick 	dmu_tx_t *tx = NULL;
334fa9e4066Sahrens 	blkptr_t blk;
335d80c45e0Sbonwick 	int error = 0;
336fa9e4066Sahrens 
337fa9e4066Sahrens 	/*
338d80c45e0Sbonwick 	 * Wait for any previous destroy to complete.
339fa9e4066Sahrens 	 */
340d80c45e0Sbonwick 	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
341d80c45e0Sbonwick 
342d80c45e0Sbonwick 	ASSERT(zh->zh_claim_txg == 0);
343d80c45e0Sbonwick 	ASSERT(zh->zh_replay_seq == 0);
344d80c45e0Sbonwick 
345d80c45e0Sbonwick 	blk = zh->zh_log;
346fa9e4066Sahrens 
347fa9e4066Sahrens 	/*
348d80c45e0Sbonwick 	 * If we don't already have an initial log block, allocate one now.
349fa9e4066Sahrens 	 */
350d80c45e0Sbonwick 	if (BP_IS_HOLE(&blk)) {
351d80c45e0Sbonwick 		tx = dmu_tx_create(zilog->zl_os);
352d80c45e0Sbonwick 		(void) dmu_tx_assign(tx, TXG_WAIT);
353d80c45e0Sbonwick 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
354d80c45e0Sbonwick 		txg = dmu_tx_get_txg(tx);
355d80c45e0Sbonwick 
35667bd71c6Sperrin 		error = zio_alloc_blk(zilog->zl_spa, ZIL_MIN_BLKSZ, &blk,
35767bd71c6Sperrin 		    NULL, txg);
358d80c45e0Sbonwick 
359d80c45e0Sbonwick 		if (error == 0)
360d80c45e0Sbonwick 			zil_init_log_chain(zilog, &blk);
36113f5297eSperrin 	}
362fa9e4066Sahrens 
363d80c45e0Sbonwick 	/*
364d80c45e0Sbonwick 	 * Allocate a log write buffer (lwb) for the first log block.
365d80c45e0Sbonwick 	 */
366d80c45e0Sbonwick 	if (error == 0) {
367fa9e4066Sahrens 		lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP);
368fa9e4066Sahrens 		lwb->lwb_zilog = zilog;
369fa9e4066Sahrens 		lwb->lwb_blk = blk;
370fa9e4066Sahrens 		lwb->lwb_nused = 0;
371fa9e4066Sahrens 		lwb->lwb_sz = BP_GET_LSIZE(&lwb->lwb_blk);
372fa9e4066Sahrens 		lwb->lwb_buf = zio_buf_alloc(lwb->lwb_sz);
373fa9e4066Sahrens 		lwb->lwb_max_txg = txg;
374c5c6ffa0Smaybee 		lwb->lwb_zio = NULL;
375c5c6ffa0Smaybee 
376fa9e4066Sahrens 		mutex_enter(&zilog->zl_lock);
377fa9e4066Sahrens 		list_insert_tail(&zilog->zl_lwb_list, lwb);
378fa9e4066Sahrens 		mutex_exit(&zilog->zl_lock);
379fa9e4066Sahrens 	}
380fa9e4066Sahrens 
381d80c45e0Sbonwick 	/*
382d80c45e0Sbonwick 	 * If we just allocated the first log block, commit our transaction
383d80c45e0Sbonwick 	 * and wait for zil_sync() to stuff the block poiner into zh_log.
384d80c45e0Sbonwick 	 * (zh is part of the MOS, so we cannot modify it in open context.)
385d80c45e0Sbonwick 	 */
386d80c45e0Sbonwick 	if (tx != NULL) {
387d80c45e0Sbonwick 		dmu_tx_commit(tx);
38813f5297eSperrin 		txg_wait_synced(zilog->zl_dmu_pool, txg);
389d80c45e0Sbonwick 	}
390d80c45e0Sbonwick 
391d80c45e0Sbonwick 	ASSERT(bcmp(&blk, &zh->zh_log, sizeof (blk)) == 0);
392fa9e4066Sahrens }
393fa9e4066Sahrens 
394fa9e4066Sahrens /*
395fa9e4066Sahrens  * In one tx, free all log blocks and clear the log header.
396d80c45e0Sbonwick  * If keep_first is set, then we're replaying a log with no content.
397d80c45e0Sbonwick  * We want to keep the first block, however, so that the first
398d80c45e0Sbonwick  * synchronous transaction doesn't require a txg_wait_synced()
399d80c45e0Sbonwick  * in zil_create().  We don't need to txg_wait_synced() here either
400d80c45e0Sbonwick  * when keep_first is set, because both zil_create() and zil_destroy()
401d80c45e0Sbonwick  * will wait for any in-progress destroys to complete.
402fa9e4066Sahrens  */
403fa9e4066Sahrens void
404d80c45e0Sbonwick zil_destroy(zilog_t *zilog, boolean_t keep_first)
405fa9e4066Sahrens {
406d80c45e0Sbonwick 	const zil_header_t *zh = zilog->zl_header;
407d80c45e0Sbonwick 	lwb_t *lwb;
408fa9e4066Sahrens 	dmu_tx_t *tx;
409fa9e4066Sahrens 	uint64_t txg;
410fa9e4066Sahrens 
411d80c45e0Sbonwick 	/*
412d80c45e0Sbonwick 	 * Wait for any previous destroy to complete.
413d80c45e0Sbonwick 	 */
414d80c45e0Sbonwick 	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
415fa9e4066Sahrens 
416d80c45e0Sbonwick 	if (BP_IS_HOLE(&zh->zh_log))
417fa9e4066Sahrens 		return;
418fa9e4066Sahrens 
419fa9e4066Sahrens 	tx = dmu_tx_create(zilog->zl_os);
420fa9e4066Sahrens 	(void) dmu_tx_assign(tx, TXG_WAIT);
421fa9e4066Sahrens 	dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
422fa9e4066Sahrens 	txg = dmu_tx_get_txg(tx);
423fa9e4066Sahrens 
424d80c45e0Sbonwick 	mutex_enter(&zilog->zl_lock);
425d80c45e0Sbonwick 
426587c2a35Sperrin 	/*
427587c2a35Sperrin 	 * It is possible for the ZIL to get the previously mounted zilog
428587c2a35Sperrin 	 * structure of the same dataset if quickly remounted and the dbuf
429587c2a35Sperrin 	 * eviction has not completed. In this case we can see a non
430587c2a35Sperrin 	 * empty lwb list and keep_first will be set. We fix this by
431587c2a35Sperrin 	 * clearing the keep_first. This will be slower but it's very rare.
432587c2a35Sperrin 	 */
433587c2a35Sperrin 	if (!list_is_empty(&zilog->zl_lwb_list) && keep_first)
434587c2a35Sperrin 		keep_first = B_FALSE;
435587c2a35Sperrin 
436d80c45e0Sbonwick 	ASSERT3U(zilog->zl_destroy_txg, <, txg);
437fa9e4066Sahrens 	zilog->zl_destroy_txg = txg;
438d80c45e0Sbonwick 	zilog->zl_keep_first = keep_first;
439d80c45e0Sbonwick 
440d80c45e0Sbonwick 	if (!list_is_empty(&zilog->zl_lwb_list)) {
441d80c45e0Sbonwick 		ASSERT(zh->zh_claim_txg == 0);
442d80c45e0Sbonwick 		ASSERT(!keep_first);
443d80c45e0Sbonwick 		while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
444d80c45e0Sbonwick 			list_remove(&zilog->zl_lwb_list, lwb);
445d80c45e0Sbonwick 			if (lwb->lwb_buf != NULL)
446d80c45e0Sbonwick 				zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
447d80c45e0Sbonwick 			zio_free_blk(zilog->zl_spa, &lwb->lwb_blk, txg);
448d80c45e0Sbonwick 			kmem_cache_free(zil_lwb_cache, lwb);
449d80c45e0Sbonwick 		}
450d80c45e0Sbonwick 	} else {
451d80c45e0Sbonwick 		if (!keep_first) {
452d80c45e0Sbonwick 			(void) zil_parse(zilog, zil_free_log_block,
453d80c45e0Sbonwick 			    zil_free_log_record, tx, zh->zh_claim_txg);
454d80c45e0Sbonwick 		}
455d80c45e0Sbonwick 	}
456b19a79ecSperrin 	mutex_exit(&zilog->zl_lock);
457fa9e4066Sahrens 
458fa9e4066Sahrens 	dmu_tx_commit(tx);
459fa9e4066Sahrens 
460d80c45e0Sbonwick 	if (keep_first)			/* no need to wait in this case */
461d80c45e0Sbonwick 		return;
462d80c45e0Sbonwick 
463d80c45e0Sbonwick 	txg_wait_synced(zilog->zl_dmu_pool, txg);
464d80c45e0Sbonwick 	ASSERT(BP_IS_HOLE(&zh->zh_log));
465fa9e4066Sahrens }
466fa9e4066Sahrens 
4673a8a1de4Sperrin /*
4683a8a1de4Sperrin  * zil_rollback_destroy() is only called by the rollback code.
4693a8a1de4Sperrin  * We already have a syncing tx. Rollback has exclusive access to the
4703a8a1de4Sperrin  * dataset, so we don't have to worry about concurrent zil access.
4713a8a1de4Sperrin  * The actual freeing of any log blocks occurs in zil_sync() later in
4723a8a1de4Sperrin  * this txg syncing phase.
4733a8a1de4Sperrin  */
4743a8a1de4Sperrin void
4753a8a1de4Sperrin zil_rollback_destroy(zilog_t *zilog, dmu_tx_t *tx)
4763a8a1de4Sperrin {
4773a8a1de4Sperrin 	const zil_header_t *zh = zilog->zl_header;
4783a8a1de4Sperrin 	uint64_t txg;
4793a8a1de4Sperrin 
4803a8a1de4Sperrin 	if (BP_IS_HOLE(&zh->zh_log))
4813a8a1de4Sperrin 		return;
4823a8a1de4Sperrin 
4833a8a1de4Sperrin 	txg = dmu_tx_get_txg(tx);
4843a8a1de4Sperrin 	ASSERT3U(zilog->zl_destroy_txg, <, txg);
4853a8a1de4Sperrin 	zilog->zl_destroy_txg = txg;
4863a8a1de4Sperrin 	zilog->zl_keep_first = B_FALSE;
4873a8a1de4Sperrin 
4883a8a1de4Sperrin 	ASSERT(list_is_empty(&zilog->zl_lwb_list));
4893a8a1de4Sperrin 	(void) zil_parse(zilog, zil_free_log_block, zil_free_log_record,
4903a8a1de4Sperrin 	    tx, zh->zh_claim_txg);
4913a8a1de4Sperrin }
4923a8a1de4Sperrin 
4931d452cf5Sahrens int
494fa9e4066Sahrens zil_claim(char *osname, void *txarg)
495fa9e4066Sahrens {
496fa9e4066Sahrens 	dmu_tx_t *tx = txarg;
497fa9e4066Sahrens 	uint64_t first_txg = dmu_tx_get_txg(tx);
498fa9e4066Sahrens 	zilog_t *zilog;
499fa9e4066Sahrens 	zil_header_t *zh;
500fa9e4066Sahrens 	objset_t *os;
501fa9e4066Sahrens 	int error;
502fa9e4066Sahrens 
503fa9e4066Sahrens 	error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_STANDARD, &os);
504fa9e4066Sahrens 	if (error) {
505fa9e4066Sahrens 		cmn_err(CE_WARN, "can't process intent log for %s", osname);
5061d452cf5Sahrens 		return (0);
507fa9e4066Sahrens 	}
508fa9e4066Sahrens 
509fa9e4066Sahrens 	zilog = dmu_objset_zil(os);
510d80c45e0Sbonwick 	zh = zil_header_in_syncing_context(zilog);
511fa9e4066Sahrens 
512fa9e4066Sahrens 	/*
513d80c45e0Sbonwick 	 * Claim all log blocks if we haven't already done so, and remember
514d80c45e0Sbonwick 	 * the highest claimed sequence number.  This ensures that if we can
515d80c45e0Sbonwick 	 * read only part of the log now (e.g. due to a missing device),
516d80c45e0Sbonwick 	 * but we can read the entire log later, we will not try to replay
517d80c45e0Sbonwick 	 * or destroy beyond the last block we successfully claimed.
518fa9e4066Sahrens 	 */
519fa9e4066Sahrens 	ASSERT3U(zh->zh_claim_txg, <=, first_txg);
520fa9e4066Sahrens 	if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) {
521fa9e4066Sahrens 		zh->zh_claim_txg = first_txg;
522d80c45e0Sbonwick 		zh->zh_claim_seq = zil_parse(zilog, zil_claim_log_block,
523d80c45e0Sbonwick 		    zil_claim_log_record, tx, first_txg);
524fa9e4066Sahrens 		dsl_dataset_dirty(dmu_objset_ds(os), tx);
525fa9e4066Sahrens 	}
526d80c45e0Sbonwick 
527fa9e4066Sahrens 	ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1));
528fa9e4066Sahrens 	dmu_objset_close(os);
5291d452cf5Sahrens 	return (0);
530fa9e4066Sahrens }
531fa9e4066Sahrens 
532*17f17c2dSbonwick static int
533*17f17c2dSbonwick zil_vdev_compare(const void *x1, const void *x2)
534*17f17c2dSbonwick {
535*17f17c2dSbonwick 	const uint64_t *v1 = x1;
536*17f17c2dSbonwick 	const uint64_t *v2 = x2;
537*17f17c2dSbonwick 
538*17f17c2dSbonwick 	if (v1 < v2)
539*17f17c2dSbonwick 		return (-1);
540*17f17c2dSbonwick 	if (v1 > v2)
541*17f17c2dSbonwick 		return (1);
542*17f17c2dSbonwick 
543*17f17c2dSbonwick 	return (0);
544*17f17c2dSbonwick }
545*17f17c2dSbonwick 
546fa9e4066Sahrens void
547*17f17c2dSbonwick zil_add_block(zilog_t *zilog, blkptr_t *bp)
548fa9e4066Sahrens {
549*17f17c2dSbonwick 	avl_tree_t *t = &zilog->zl_vdev_tree;
550*17f17c2dSbonwick 	avl_index_t where;
551*17f17c2dSbonwick 	zil_vdev_node_t *zv, zvsearch;
552*17f17c2dSbonwick 	int ndvas = BP_GET_NDVAS(bp);
553*17f17c2dSbonwick 	int i;
554fa9e4066Sahrens 
555416e0cd8Sek 	if (zfs_nocacheflush)
556fa9e4066Sahrens 		return;
557fa9e4066Sahrens 
558*17f17c2dSbonwick 	ASSERT(zilog->zl_writer);
559*17f17c2dSbonwick 
560*17f17c2dSbonwick 	/*
561*17f17c2dSbonwick 	 * Even though we're zl_writer, we still need a lock because the
562*17f17c2dSbonwick 	 * zl_get_data() callbacks may have dmu_sync() done callbacks
563*17f17c2dSbonwick 	 * that will run concurrently.
564*17f17c2dSbonwick 	 */
565*17f17c2dSbonwick 	mutex_enter(&zilog->zl_vdev_lock);
566*17f17c2dSbonwick 	for (i = 0; i < ndvas; i++) {
567*17f17c2dSbonwick 		zvsearch.zv_vdev = DVA_GET_VDEV(&bp->blk_dva[i]);
568*17f17c2dSbonwick 		if (avl_find(t, &zvsearch, &where) == NULL) {
569*17f17c2dSbonwick 			zv = kmem_alloc(sizeof (*zv), KM_SLEEP);
570*17f17c2dSbonwick 			zv->zv_vdev = zvsearch.zv_vdev;
571*17f17c2dSbonwick 			avl_insert(t, zv, where);
57267bd71c6Sperrin 		}
57367bd71c6Sperrin 	}
574*17f17c2dSbonwick 	mutex_exit(&zilog->zl_vdev_lock);
575fa9e4066Sahrens }
576fa9e4066Sahrens 
57767bd71c6Sperrin void
57867bd71c6Sperrin zil_flush_vdevs(zilog_t *zilog)
57967bd71c6Sperrin {
58067bd71c6Sperrin 	spa_t *spa = zilog->zl_spa;
581*17f17c2dSbonwick 	avl_tree_t *t = &zilog->zl_vdev_tree;
582*17f17c2dSbonwick 	void *cookie = NULL;
583*17f17c2dSbonwick 	zil_vdev_node_t *zv;
584*17f17c2dSbonwick 	zio_t *zio;
585fa9e4066Sahrens 
58667bd71c6Sperrin 	ASSERT(zilog->zl_writer);
58767bd71c6Sperrin 
588*17f17c2dSbonwick 	/*
589*17f17c2dSbonwick 	 * We don't need zl_vdev_lock here because we're the zl_writer,
590*17f17c2dSbonwick 	 * and all zl_get_data() callbacks are done.
591*17f17c2dSbonwick 	 */
592*17f17c2dSbonwick 	if (avl_numnodes(t) == 0)
593*17f17c2dSbonwick 		return;
594*17f17c2dSbonwick 
595*17f17c2dSbonwick 	spa_config_enter(spa, RW_READER, FTAG);
596*17f17c2dSbonwick 
597*17f17c2dSbonwick 	zio = zio_root(spa, NULL, NULL,
598*17f17c2dSbonwick 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
599fa9e4066Sahrens 
600*17f17c2dSbonwick 	while ((zv = avl_destroy_nodes(t, &cookie)) != NULL) {
601*17f17c2dSbonwick 		vdev_t *vd = vdev_lookup_top(spa, zv->zv_vdev);
602*17f17c2dSbonwick 		if (vd != NULL)
603*17f17c2dSbonwick 			zio_flush(zio, vd);
604*17f17c2dSbonwick 		kmem_free(zv, sizeof (*zv));
60567bd71c6Sperrin 	}
606*17f17c2dSbonwick 
607fa9e4066Sahrens 	/*
608fa9e4066Sahrens 	 * Wait for all the flushes to complete.  Not all devices actually
609fa9e4066Sahrens 	 * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails.
610fa9e4066Sahrens 	 */
611*17f17c2dSbonwick 	(void) zio_wait(zio);
612*17f17c2dSbonwick 
613*17f17c2dSbonwick 	spa_config_exit(spa, FTAG);
614fa9e4066Sahrens }
615fa9e4066Sahrens 
616fa9e4066Sahrens /*
617fa9e4066Sahrens  * Function called when a log block write completes
618fa9e4066Sahrens  */
619fa9e4066Sahrens static void
620fa9e4066Sahrens zil_lwb_write_done(zio_t *zio)
621fa9e4066Sahrens {
622fa9e4066Sahrens 	lwb_t *lwb = zio->io_private;
623fa9e4066Sahrens 	zilog_t *zilog = lwb->lwb_zilog;
624fa9e4066Sahrens 
625fa9e4066Sahrens 	/*
626fa9e4066Sahrens 	 * Now that we've written this log block, we have a stable pointer
627fa9e4066Sahrens 	 * to the next block in the chain, so it's OK to let the txg in
628fa9e4066Sahrens 	 * which we allocated the next block sync.
629fa9e4066Sahrens 	 */
630fa9e4066Sahrens 	txg_rele_to_sync(&lwb->lwb_txgh);
631fa9e4066Sahrens 
632fa9e4066Sahrens 	zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
633fa9e4066Sahrens 	mutex_enter(&zilog->zl_lock);
634fa9e4066Sahrens 	lwb->lwb_buf = NULL;
6358654d025Sperrin 	if (zio->io_error)
636fa9e4066Sahrens 		zilog->zl_log_error = B_TRUE;
637fa9e4066Sahrens 	mutex_exit(&zilog->zl_lock);
638fa9e4066Sahrens }
639fa9e4066Sahrens 
640c5c6ffa0Smaybee /*
641c5c6ffa0Smaybee  * Initialize the io for a log block.
642c5c6ffa0Smaybee  *
643c5c6ffa0Smaybee  * Note, we should not initialize the IO until we are about
644c5c6ffa0Smaybee  * to use it, since zio_rewrite() does a spa_config_enter().
645c5c6ffa0Smaybee  */
646c5c6ffa0Smaybee static void
647c5c6ffa0Smaybee zil_lwb_write_init(zilog_t *zilog, lwb_t *lwb)
648c5c6ffa0Smaybee {
649c5c6ffa0Smaybee 	zbookmark_t zb;
650c5c6ffa0Smaybee 
651c5c6ffa0Smaybee 	zb.zb_objset = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET];
652c5c6ffa0Smaybee 	zb.zb_object = 0;
653c5c6ffa0Smaybee 	zb.zb_level = -1;
654c5c6ffa0Smaybee 	zb.zb_blkid = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ];
655c5c6ffa0Smaybee 
656b19a79ecSperrin 	if (zilog->zl_root_zio == NULL) {
657b19a79ecSperrin 		zilog->zl_root_zio = zio_root(zilog->zl_spa, NULL, NULL,
658b19a79ecSperrin 		    ZIO_FLAG_CANFAIL);
659b19a79ecSperrin 	}
66067bd71c6Sperrin 	if (lwb->lwb_zio == NULL) {
66167bd71c6Sperrin 		lwb->lwb_zio = zio_rewrite(zilog->zl_root_zio, zilog->zl_spa,
66267bd71c6Sperrin 		    ZIO_CHECKSUM_ZILOG, 0, &lwb->lwb_blk, lwb->lwb_buf,
66367bd71c6Sperrin 		    lwb->lwb_sz, zil_lwb_write_done, lwb,
6648654d025Sperrin 		    ZIO_PRIORITY_LOG_WRITE, ZIO_FLAG_CANFAIL, &zb);
66567bd71c6Sperrin 	}
666c5c6ffa0Smaybee }
667c5c6ffa0Smaybee 
668fa9e4066Sahrens /*
669fa9e4066Sahrens  * Start a log block write and advance to the next log block.
670fa9e4066Sahrens  * Calls are serialized.
671fa9e4066Sahrens  */
672fa9e4066Sahrens static lwb_t *
673fa9e4066Sahrens zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb)
674fa9e4066Sahrens {
675fa9e4066Sahrens 	lwb_t *nlwb;
676fa9e4066Sahrens 	zil_trailer_t *ztp = (zil_trailer_t *)(lwb->lwb_buf + lwb->lwb_sz) - 1;
677d80c45e0Sbonwick 	spa_t *spa = zilog->zl_spa;
678d80c45e0Sbonwick 	blkptr_t *bp = &ztp->zit_next_blk;
679fa9e4066Sahrens 	uint64_t txg;
680fa9e4066Sahrens 	uint64_t zil_blksz;
681fa9e4066Sahrens 	int error;
682fa9e4066Sahrens 
683fa9e4066Sahrens 	ASSERT(lwb->lwb_nused <= ZIL_BLK_DATA_SZ(lwb));
684fa9e4066Sahrens 
685fa9e4066Sahrens 	/*
686fa9e4066Sahrens 	 * Allocate the next block and save its address in this block
687fa9e4066Sahrens 	 * before writing it in order to establish the log chain.
688fa9e4066Sahrens 	 * Note that if the allocation of nlwb synced before we wrote
689fa9e4066Sahrens 	 * the block that points at it (lwb), we'd leak it if we crashed.
690fa9e4066Sahrens 	 * Therefore, we don't do txg_rele_to_sync() until zil_lwb_write_done().
691fa9e4066Sahrens 	 */
692fa9e4066Sahrens 	txg = txg_hold_open(zilog->zl_dmu_pool, &lwb->lwb_txgh);
693fa9e4066Sahrens 	txg_rele_to_quiesce(&lwb->lwb_txgh);
694fa9e4066Sahrens 
695fa9e4066Sahrens 	/*
69622ac5be4Sperrin 	 * Pick a ZIL blocksize. We request a size that is the
69722ac5be4Sperrin 	 * maximum of the previous used size, the current used size and
69822ac5be4Sperrin 	 * the amount waiting in the queue.
699fa9e4066Sahrens 	 */
700c5c6ffa0Smaybee 	zil_blksz = MAX(zilog->zl_prev_used,
701c5c6ffa0Smaybee 	    zilog->zl_cur_used + sizeof (*ztp));
70222ac5be4Sperrin 	zil_blksz = MAX(zil_blksz, zilog->zl_itx_list_sz + sizeof (*ztp));
703b4d654b0Sperrin 	zil_blksz = P2ROUNDUP_TYPED(zil_blksz, ZIL_MIN_BLKSZ, uint64_t);
70422ac5be4Sperrin 	if (zil_blksz > ZIL_MAX_BLKSZ)
70522ac5be4Sperrin 		zil_blksz = ZIL_MAX_BLKSZ;
706fa9e4066Sahrens 
70767bd71c6Sperrin 	BP_ZERO(bp);
70867bd71c6Sperrin 	/* pass the old blkptr in order to spread log blocks across devs */
70967bd71c6Sperrin 	error = zio_alloc_blk(spa, zil_blksz, bp, &lwb->lwb_blk, txg);
710fa9e4066Sahrens 	if (error) {
711d63d470bSgw 		dmu_tx_t *tx = dmu_tx_create_assigned(zilog->zl_dmu_pool, txg);
712d63d470bSgw 
713d63d470bSgw 		/*
714d63d470bSgw 		 * We dirty the dataset to ensure that zil_sync() will
715d63d470bSgw 		 * be called to remove this lwb from our zl_lwb_list.
716d63d470bSgw 		 * Failing to do so, may leave an lwb with a NULL lwb_buf
717d63d470bSgw 		 * hanging around on the zl_lwb_list.
718d63d470bSgw 		 */
719d63d470bSgw 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
720aeb1c1b6Sgw 		dmu_tx_commit(tx);
721d63d470bSgw 
722d63d470bSgw 		/*
723d63d470bSgw 		 * Since we've just experienced an allocation failure so we
724d63d470bSgw 		 * terminate the current lwb and send it on its way.
725d63d470bSgw 		 */
726d63d470bSgw 		ztp->zit_pad = 0;
727d63d470bSgw 		ztp->zit_nused = lwb->lwb_nused;
728d63d470bSgw 		ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum;
729d63d470bSgw 		zio_nowait(lwb->lwb_zio);
730d63d470bSgw 
731ea8dc4b6Seschrock 		/*
732ea8dc4b6Seschrock 		 * By returning NULL the caller will call tx_wait_synced()
733ea8dc4b6Seschrock 		 */
734fa9e4066Sahrens 		return (NULL);
735fa9e4066Sahrens 	}
736fa9e4066Sahrens 
737d80c45e0Sbonwick 	ASSERT3U(bp->blk_birth, ==, txg);
738ea8dc4b6Seschrock 	ztp->zit_pad = 0;
739fa9e4066Sahrens 	ztp->zit_nused = lwb->lwb_nused;
740fa9e4066Sahrens 	ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum;
741d80c45e0Sbonwick 	bp->blk_cksum = lwb->lwb_blk.blk_cksum;
742d80c45e0Sbonwick 	bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++;
743fa9e4066Sahrens 
744fa9e4066Sahrens 	/*
745fa9e4066Sahrens 	 * Allocate a new log write buffer (lwb).
746fa9e4066Sahrens 	 */
747fa9e4066Sahrens 	nlwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP);
748fa9e4066Sahrens 
749fa9e4066Sahrens 	nlwb->lwb_zilog = zilog;
750d80c45e0Sbonwick 	nlwb->lwb_blk = *bp;
751fa9e4066Sahrens 	nlwb->lwb_nused = 0;
752fa9e4066Sahrens 	nlwb->lwb_sz = BP_GET_LSIZE(&nlwb->lwb_blk);
753fa9e4066Sahrens 	nlwb->lwb_buf = zio_buf_alloc(nlwb->lwb_sz);
754fa9e4066Sahrens 	nlwb->lwb_max_txg = txg;
755c5c6ffa0Smaybee 	nlwb->lwb_zio = NULL;
756fa9e4066Sahrens 
757fa9e4066Sahrens 	/*
75867bd71c6Sperrin 	 * Put new lwb at the end of the log chain
759fa9e4066Sahrens 	 */
760fa9e4066Sahrens 	mutex_enter(&zilog->zl_lock);
761fa9e4066Sahrens 	list_insert_tail(&zilog->zl_lwb_list, nlwb);
762fa9e4066Sahrens 	mutex_exit(&zilog->zl_lock);
763fa9e4066Sahrens 
764*17f17c2dSbonwick 	/* Record the block for later vdev flushing */
765*17f17c2dSbonwick 	zil_add_block(zilog, &lwb->lwb_blk);
76667bd71c6Sperrin 
767fa9e4066Sahrens 	/*
768c5c6ffa0Smaybee 	 * kick off the write for the old log block
769fa9e4066Sahrens 	 */
770c5c6ffa0Smaybee 	dprintf_bp(&lwb->lwb_blk, "lwb %p txg %llu: ", lwb, txg);
77167bd71c6Sperrin 	ASSERT(lwb->lwb_zio);
772c5c6ffa0Smaybee 	zio_nowait(lwb->lwb_zio);
773fa9e4066Sahrens 
774fa9e4066Sahrens 	return (nlwb);
775fa9e4066Sahrens }
776fa9e4066Sahrens 
777fa9e4066Sahrens static lwb_t *
778fa9e4066Sahrens zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb)
779fa9e4066Sahrens {
780fa9e4066Sahrens 	lr_t *lrc = &itx->itx_lr; /* common log record */
781c5c6ffa0Smaybee 	lr_write_t *lr = (lr_write_t *)lrc;
782fa9e4066Sahrens 	uint64_t txg = lrc->lrc_txg;
783fa9e4066Sahrens 	uint64_t reclen = lrc->lrc_reclen;
784c5c6ffa0Smaybee 	uint64_t dlen;
785fa9e4066Sahrens 
786fa9e4066Sahrens 	if (lwb == NULL)
787fa9e4066Sahrens 		return (NULL);
788fa9e4066Sahrens 	ASSERT(lwb->lwb_buf != NULL);
789fa9e4066Sahrens 
790c5c6ffa0Smaybee 	if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY)
791c5c6ffa0Smaybee 		dlen = P2ROUNDUP_TYPED(
792c5c6ffa0Smaybee 		    lr->lr_length, sizeof (uint64_t), uint64_t);
793c5c6ffa0Smaybee 	else
794c5c6ffa0Smaybee 		dlen = 0;
795fa9e4066Sahrens 
796104e2ed7Sperrin 	zilog->zl_cur_used += (reclen + dlen);
79722ac5be4Sperrin 
79867bd71c6Sperrin 	zil_lwb_write_init(zilog, lwb);
79967bd71c6Sperrin 
800fa9e4066Sahrens 	/*
801fa9e4066Sahrens 	 * If this record won't fit in the current log block, start a new one.
802fa9e4066Sahrens 	 */
803104e2ed7Sperrin 	if (lwb->lwb_nused + reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) {
804fa9e4066Sahrens 		lwb = zil_lwb_write_start(zilog, lwb);
805c5c6ffa0Smaybee 		if (lwb == NULL)
806fa9e4066Sahrens 			return (NULL);
80767bd71c6Sperrin 		zil_lwb_write_init(zilog, lwb);
808ea8dc4b6Seschrock 		ASSERT(lwb->lwb_nused == 0);
809104e2ed7Sperrin 		if (reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) {
810fa9e4066Sahrens 			txg_wait_synced(zilog->zl_dmu_pool, txg);
811fa9e4066Sahrens 			return (lwb);
812fa9e4066Sahrens 		}
813fa9e4066Sahrens 	}
814fa9e4066Sahrens 
815b19a79ecSperrin 	/*
816b19a79ecSperrin 	 * Update the lrc_seq, to be log record sequence number. See zil.h
817b19a79ecSperrin 	 * Then copy the record to the log buffer.
818b19a79ecSperrin 	 */
819b19a79ecSperrin 	lrc->lrc_seq = ++zilog->zl_lr_seq; /* we are single threaded */
820fa9e4066Sahrens 	bcopy(lrc, lwb->lwb_buf + lwb->lwb_nused, reclen);
821c5c6ffa0Smaybee 
822c5c6ffa0Smaybee 	/*
823c5c6ffa0Smaybee 	 * If it's a write, fetch the data or get its blkptr as appropriate.
824c5c6ffa0Smaybee 	 */
825c5c6ffa0Smaybee 	if (lrc->lrc_txtype == TX_WRITE) {
826c5c6ffa0Smaybee 		if (txg > spa_freeze_txg(zilog->zl_spa))
827c5c6ffa0Smaybee 			txg_wait_synced(zilog->zl_dmu_pool, txg);
828c5c6ffa0Smaybee 		if (itx->itx_wr_state != WR_COPIED) {
829c5c6ffa0Smaybee 			char *dbuf;
830c5c6ffa0Smaybee 			int error;
831c5c6ffa0Smaybee 
832c5c6ffa0Smaybee 			/* alignment is guaranteed */
833c5c6ffa0Smaybee 			lr = (lr_write_t *)(lwb->lwb_buf + lwb->lwb_nused);
834c5c6ffa0Smaybee 			if (dlen) {
835c5c6ffa0Smaybee 				ASSERT(itx->itx_wr_state == WR_NEED_COPY);
836c5c6ffa0Smaybee 				dbuf = lwb->lwb_buf + lwb->lwb_nused + reclen;
837c5c6ffa0Smaybee 				lr->lr_common.lrc_reclen += dlen;
838c5c6ffa0Smaybee 			} else {
839c5c6ffa0Smaybee 				ASSERT(itx->itx_wr_state == WR_INDIRECT);
840c5c6ffa0Smaybee 				dbuf = NULL;
841c5c6ffa0Smaybee 			}
842c5c6ffa0Smaybee 			error = zilog->zl_get_data(
843c5c6ffa0Smaybee 			    itx->itx_private, lr, dbuf, lwb->lwb_zio);
844c5c6ffa0Smaybee 			if (error) {
845c5c6ffa0Smaybee 				ASSERT(error == ENOENT || error == EEXIST ||
846c5c6ffa0Smaybee 				    error == EALREADY);
847c5c6ffa0Smaybee 				return (lwb);
848c5c6ffa0Smaybee 			}
849c5c6ffa0Smaybee 		}
850104e2ed7Sperrin 	}
851c5c6ffa0Smaybee 
852c5c6ffa0Smaybee 	lwb->lwb_nused += reclen + dlen;
853fa9e4066Sahrens 	lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg);
854fa9e4066Sahrens 	ASSERT3U(lwb->lwb_nused, <=, ZIL_BLK_DATA_SZ(lwb));
855fa9e4066Sahrens 	ASSERT3U(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)), ==, 0);
856fa9e4066Sahrens 
857fa9e4066Sahrens 	return (lwb);
858fa9e4066Sahrens }
859fa9e4066Sahrens 
860fa9e4066Sahrens itx_t *
861da6c28aaSamw zil_itx_create(uint64_t txtype, size_t lrsize)
862fa9e4066Sahrens {
863fa9e4066Sahrens 	itx_t *itx;
864fa9e4066Sahrens 
865b4d654b0Sperrin 	lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t);
866fa9e4066Sahrens 
867fa9e4066Sahrens 	itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP);
868fa9e4066Sahrens 	itx->itx_lr.lrc_txtype = txtype;
869fa9e4066Sahrens 	itx->itx_lr.lrc_reclen = lrsize;
870fa9e4066Sahrens 	itx->itx_lr.lrc_seq = 0;	/* defensive */
871fa9e4066Sahrens 
872fa9e4066Sahrens 	return (itx);
873fa9e4066Sahrens }
874fa9e4066Sahrens 
875fa9e4066Sahrens uint64_t
876fa9e4066Sahrens zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx)
877fa9e4066Sahrens {
878fa9e4066Sahrens 	uint64_t seq;
879fa9e4066Sahrens 
880fa9e4066Sahrens 	ASSERT(itx->itx_lr.lrc_seq == 0);
881fa9e4066Sahrens 
882fa9e4066Sahrens 	mutex_enter(&zilog->zl_lock);
883fa9e4066Sahrens 	list_insert_tail(&zilog->zl_itx_list, itx);
884fa9e4066Sahrens 	zilog->zl_itx_list_sz += itx->itx_lr.lrc_reclen;
885fa9e4066Sahrens 	itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx);
886fa9e4066Sahrens 	itx->itx_lr.lrc_seq = seq = ++zilog->zl_itx_seq;
887fa9e4066Sahrens 	mutex_exit(&zilog->zl_lock);
888fa9e4066Sahrens 
889fa9e4066Sahrens 	return (seq);
890fa9e4066Sahrens }
891fa9e4066Sahrens 
892fa9e4066Sahrens /*
893fa9e4066Sahrens  * Free up all in-memory intent log transactions that have now been synced.
894fa9e4066Sahrens  */
895fa9e4066Sahrens static void
896fa9e4066Sahrens zil_itx_clean(zilog_t *zilog)
897fa9e4066Sahrens {
898fa9e4066Sahrens 	uint64_t synced_txg = spa_last_synced_txg(zilog->zl_spa);
899fa9e4066Sahrens 	uint64_t freeze_txg = spa_freeze_txg(zilog->zl_spa);
900a584ef65Sjohansen 	list_t clean_list;
901fa9e4066Sahrens 	itx_t *itx;
902fa9e4066Sahrens 
903a584ef65Sjohansen 	list_create(&clean_list, sizeof (itx_t), offsetof(itx_t, itx_node));
904a584ef65Sjohansen 
905fa9e4066Sahrens 	mutex_enter(&zilog->zl_lock);
906b19a79ecSperrin 	/* wait for a log writer to finish walking list */
907b19a79ecSperrin 	while (zilog->zl_writer) {
908b19a79ecSperrin 		cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
909b19a79ecSperrin 	}
910a584ef65Sjohansen 
911a584ef65Sjohansen 	/*
912a584ef65Sjohansen 	 * Move the sync'd log transactions to a separate list so we can call
913a584ef65Sjohansen 	 * kmem_free without holding the zl_lock.
914a584ef65Sjohansen 	 *
915a584ef65Sjohansen 	 * There is no need to set zl_writer as we don't drop zl_lock here
916a584ef65Sjohansen 	 */
917fa9e4066Sahrens 	while ((itx = list_head(&zilog->zl_itx_list)) != NULL &&
918fa9e4066Sahrens 	    itx->itx_lr.lrc_txg <= MIN(synced_txg, freeze_txg)) {
919fa9e4066Sahrens 		list_remove(&zilog->zl_itx_list, itx);
920fa9e4066Sahrens 		zilog->zl_itx_list_sz -= itx->itx_lr.lrc_reclen;
921a584ef65Sjohansen 		list_insert_tail(&clean_list, itx);
922fa9e4066Sahrens 	}
92367bd71c6Sperrin 	cv_broadcast(&zilog->zl_cv_writer);
924fa9e4066Sahrens 	mutex_exit(&zilog->zl_lock);
925a584ef65Sjohansen 
926a584ef65Sjohansen 	/* destroy sync'd log transactions */
927a584ef65Sjohansen 	while ((itx = list_head(&clean_list)) != NULL) {
928a584ef65Sjohansen 		list_remove(&clean_list, itx);
929a584ef65Sjohansen 		kmem_free(itx, offsetof(itx_t, itx_lr)
930a584ef65Sjohansen 		    + itx->itx_lr.lrc_reclen);
931a584ef65Sjohansen 	}
932a584ef65Sjohansen 	list_destroy(&clean_list);
933fa9e4066Sahrens }
934fa9e4066Sahrens 
935b19a79ecSperrin /*
93667bd71c6Sperrin  * If there are any in-memory intent log transactions which have now been
93767bd71c6Sperrin  * synced then start up a taskq to free them.
938b19a79ecSperrin  */
939fa9e4066Sahrens void
940fa9e4066Sahrens zil_clean(zilog_t *zilog)
941fa9e4066Sahrens {
94267bd71c6Sperrin 	itx_t *itx;
94367bd71c6Sperrin 
944fa9e4066Sahrens 	mutex_enter(&zilog->zl_lock);
94567bd71c6Sperrin 	itx = list_head(&zilog->zl_itx_list);
94667bd71c6Sperrin 	if ((itx != NULL) &&
94767bd71c6Sperrin 	    (itx->itx_lr.lrc_txg <= spa_last_synced_txg(zilog->zl_spa))) {
948fa9e4066Sahrens 		(void) taskq_dispatch(zilog->zl_clean_taskq,
949fa9e4066Sahrens 		    (void (*)(void *))zil_itx_clean, zilog, TQ_NOSLEEP);
95067bd71c6Sperrin 	}
951fa9e4066Sahrens 	mutex_exit(&zilog->zl_lock);
952fa9e4066Sahrens }
953fa9e4066Sahrens 
954fa9e4066Sahrens void
955b19a79ecSperrin zil_commit_writer(zilog_t *zilog, uint64_t seq, uint64_t foid)
956fa9e4066Sahrens {
957fa9e4066Sahrens 	uint64_t txg;
958fa9e4066Sahrens 	uint64_t reclen;
95967bd71c6Sperrin 	uint64_t commit_seq = 0;
960b19a79ecSperrin 	itx_t *itx, *itx_next = (itx_t *)-1;
961fa9e4066Sahrens 	lwb_t *lwb;
962fa9e4066Sahrens 	spa_t *spa;
963fa9e4066Sahrens 
964fa9e4066Sahrens 	zilog->zl_writer = B_TRUE;
965b19a79ecSperrin 	zilog->zl_root_zio = NULL;
966b19a79ecSperrin 	spa = zilog->zl_spa;
967fa9e4066Sahrens 
968fa9e4066Sahrens 	if (zilog->zl_suspend) {
969fa9e4066Sahrens 		lwb = NULL;
970fa9e4066Sahrens 	} else {
971fa9e4066Sahrens 		lwb = list_tail(&zilog->zl_lwb_list);
972fa9e4066Sahrens 		if (lwb == NULL) {
973b19a79ecSperrin 			/*
974b19a79ecSperrin 			 * Return if there's nothing to flush before we
975b19a79ecSperrin 			 * dirty the fs by calling zil_create()
976b19a79ecSperrin 			 */
977b19a79ecSperrin 			if (list_is_empty(&zilog->zl_itx_list)) {
978b19a79ecSperrin 				zilog->zl_writer = B_FALSE;
979b19a79ecSperrin 				return;
980b19a79ecSperrin 			}
981fa9e4066Sahrens 			mutex_exit(&zilog->zl_lock);
982fa9e4066Sahrens 			zil_create(zilog);
983fa9e4066Sahrens 			mutex_enter(&zilog->zl_lock);
984fa9e4066Sahrens 			lwb = list_tail(&zilog->zl_lwb_list);
985fa9e4066Sahrens 		}
986fa9e4066Sahrens 	}
987fa9e4066Sahrens 
98867bd71c6Sperrin 	/* Loop through in-memory log transactions filling log blocks. */
989b19a79ecSperrin 	DTRACE_PROBE1(zil__cw1, zilog_t *, zilog);
990fa9e4066Sahrens 	for (;;) {
991b19a79ecSperrin 		/*
992b19a79ecSperrin 		 * Find the next itx to push:
993b19a79ecSperrin 		 * Push all transactions related to specified foid and all
994b19a79ecSperrin 		 * other transactions except TX_WRITE, TX_TRUNCATE,
995b19a79ecSperrin 		 * TX_SETATTR and TX_ACL for all other files.
996b19a79ecSperrin 		 */
997b19a79ecSperrin 		if (itx_next != (itx_t *)-1)
998b19a79ecSperrin 			itx = itx_next;
999b19a79ecSperrin 		else
1000b19a79ecSperrin 			itx = list_head(&zilog->zl_itx_list);
1001b19a79ecSperrin 		for (; itx != NULL; itx = list_next(&zilog->zl_itx_list, itx)) {
1002b19a79ecSperrin 			if (foid == 0) /* push all foids? */
1003b19a79ecSperrin 				break;
100467bd71c6Sperrin 			if (itx->itx_sync) /* push all O_[D]SYNC */
100567bd71c6Sperrin 				break;
1006b19a79ecSperrin 			switch (itx->itx_lr.lrc_txtype) {
1007b19a79ecSperrin 			case TX_SETATTR:
1008b19a79ecSperrin 			case TX_WRITE:
1009b19a79ecSperrin 			case TX_TRUNCATE:
1010b19a79ecSperrin 			case TX_ACL:
1011b19a79ecSperrin 				/* lr_foid is same offset for these records */
1012b19a79ecSperrin 				if (((lr_write_t *)&itx->itx_lr)->lr_foid
1013b19a79ecSperrin 				    != foid) {
1014b19a79ecSperrin 					continue; /* skip this record */
1015b19a79ecSperrin 				}
1016b19a79ecSperrin 			}
1017b19a79ecSperrin 			break;
1018b19a79ecSperrin 		}
1019fa9e4066Sahrens 		if (itx == NULL)
1020fa9e4066Sahrens 			break;
1021fa9e4066Sahrens 
1022fa9e4066Sahrens 		reclen = itx->itx_lr.lrc_reclen;
1023fa9e4066Sahrens 		if ((itx->itx_lr.lrc_seq > seq) &&
1024b19a79ecSperrin 		    ((lwb == NULL) || (lwb->lwb_nused == 0) ||
102567bd71c6Sperrin 		    (lwb->lwb_nused + reclen > ZIL_BLK_DATA_SZ(lwb)))) {
1026fa9e4066Sahrens 			break;
102767bd71c6Sperrin 		}
1028fa9e4066Sahrens 
1029b19a79ecSperrin 		/*
1030b19a79ecSperrin 		 * Save the next pointer.  Even though we soon drop
1031b19a79ecSperrin 		 * zl_lock all threads that may change the list
1032b19a79ecSperrin 		 * (another writer or zil_itx_clean) can't do so until
1033b19a79ecSperrin 		 * they have zl_writer.
1034b19a79ecSperrin 		 */
1035b19a79ecSperrin 		itx_next = list_next(&zilog->zl_itx_list, itx);
1036fa9e4066Sahrens 		list_remove(&zilog->zl_itx_list, itx);
103767bd71c6Sperrin 		mutex_exit(&zilog->zl_lock);
1038fa9e4066Sahrens 		txg = itx->itx_lr.lrc_txg;
1039fa9e4066Sahrens 		ASSERT(txg);
1040fa9e4066Sahrens 
1041fa9e4066Sahrens 		if (txg > spa_last_synced_txg(spa) ||
1042fa9e4066Sahrens 		    txg > spa_freeze_txg(spa))
1043fa9e4066Sahrens 			lwb = zil_lwb_commit(zilog, itx, lwb);
1044fa9e4066Sahrens 		kmem_free(itx, offsetof(itx_t, itx_lr)
1045fa9e4066Sahrens 		    + itx->itx_lr.lrc_reclen);
1046fa9e4066Sahrens 		mutex_enter(&zilog->zl_lock);
1047fa9e4066Sahrens 		zilog->zl_itx_list_sz -= reclen;
1048fa9e4066Sahrens 	}
1049b19a79ecSperrin 	DTRACE_PROBE1(zil__cw2, zilog_t *, zilog);
105067bd71c6Sperrin 	/* determine commit sequence number */
105167bd71c6Sperrin 	itx = list_head(&zilog->zl_itx_list);
105267bd71c6Sperrin 	if (itx)
105367bd71c6Sperrin 		commit_seq = itx->itx_lr.lrc_seq;
105467bd71c6Sperrin 	else
105567bd71c6Sperrin 		commit_seq = zilog->zl_itx_seq;
1056fa9e4066Sahrens 	mutex_exit(&zilog->zl_lock);
1057fa9e4066Sahrens 
1058fa9e4066Sahrens 	/* write the last block out */
105967bd71c6Sperrin 	if (lwb != NULL && lwb->lwb_zio != NULL)
1060fa9e4066Sahrens 		lwb = zil_lwb_write_start(zilog, lwb);
1061fa9e4066Sahrens 
106222ac5be4Sperrin 	zilog->zl_prev_used = zilog->zl_cur_used;
106322ac5be4Sperrin 	zilog->zl_cur_used = 0;
1064fa9e4066Sahrens 
1065fa9e4066Sahrens 	/*
1066b19a79ecSperrin 	 * Wait if necessary for the log blocks to be on stable storage.
1067fa9e4066Sahrens 	 */
1068b19a79ecSperrin 	if (zilog->zl_root_zio) {
1069b19a79ecSperrin 		DTRACE_PROBE1(zil__cw3, zilog_t *, zilog);
1070b19a79ecSperrin 		(void) zio_wait(zilog->zl_root_zio);
1071b19a79ecSperrin 		DTRACE_PROBE1(zil__cw4, zilog_t *, zilog);
1072*17f17c2dSbonwick 		zil_flush_vdevs(zilog);
1073fa9e4066Sahrens 	}
107422ac5be4Sperrin 
1075fa9e4066Sahrens 	if (zilog->zl_log_error || lwb == NULL) {
1076fa9e4066Sahrens 		zilog->zl_log_error = 0;
1077fa9e4066Sahrens 		txg_wait_synced(zilog->zl_dmu_pool, 0);
1078fa9e4066Sahrens 	}
107967bd71c6Sperrin 
108067bd71c6Sperrin 	mutex_enter(&zilog->zl_lock);
108122ac5be4Sperrin 	zilog->zl_writer = B_FALSE;
108267bd71c6Sperrin 
108367bd71c6Sperrin 	ASSERT3U(commit_seq, >=, zilog->zl_commit_seq);
108467bd71c6Sperrin 	zilog->zl_commit_seq = commit_seq;
1085b19a79ecSperrin }
1086b19a79ecSperrin 
1087b19a79ecSperrin /*
1088b19a79ecSperrin  * Push zfs transactions to stable storage up to the supplied sequence number.
1089b19a79ecSperrin  * If foid is 0 push out all transactions, otherwise push only those
1090b19a79ecSperrin  * for that file or might have been used to create that file.
1091b19a79ecSperrin  */
1092b19a79ecSperrin void
1093b19a79ecSperrin zil_commit(zilog_t *zilog, uint64_t seq, uint64_t foid)
1094b19a79ecSperrin {
1095b19a79ecSperrin 	if (zilog == NULL || seq == 0)
1096b19a79ecSperrin 		return;
1097b19a79ecSperrin 
1098b19a79ecSperrin 	mutex_enter(&zilog->zl_lock);
1099b19a79ecSperrin 
1100b19a79ecSperrin 	seq = MIN(seq, zilog->zl_itx_seq);	/* cap seq at largest itx seq */
1101b19a79ecSperrin 
110267bd71c6Sperrin 	while (zilog->zl_writer) {
1103b19a79ecSperrin 		cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
110467bd71c6Sperrin 		if (seq < zilog->zl_commit_seq) {
110567bd71c6Sperrin 			mutex_exit(&zilog->zl_lock);
110667bd71c6Sperrin 			return;
110767bd71c6Sperrin 		}
110867bd71c6Sperrin 	}
1109b19a79ecSperrin 	zil_commit_writer(zilog, seq, foid); /* drops zl_lock */
111067bd71c6Sperrin 	/* wake up others waiting on the commit */
111167bd71c6Sperrin 	cv_broadcast(&zilog->zl_cv_writer);
111267bd71c6Sperrin 	mutex_exit(&zilog->zl_lock);
1113fa9e4066Sahrens }
1114fa9e4066Sahrens 
1115fa9e4066Sahrens /*
1116fa9e4066Sahrens  * Called in syncing context to free committed log blocks and update log header.
1117fa9e4066Sahrens  */
1118fa9e4066Sahrens void
1119fa9e4066Sahrens zil_sync(zilog_t *zilog, dmu_tx_t *tx)
1120fa9e4066Sahrens {
1121d80c45e0Sbonwick 	zil_header_t *zh = zil_header_in_syncing_context(zilog);
1122fa9e4066Sahrens 	uint64_t txg = dmu_tx_get_txg(tx);
1123fa9e4066Sahrens 	spa_t *spa = zilog->zl_spa;
1124fa9e4066Sahrens 	lwb_t *lwb;
1125fa9e4066Sahrens 
1126d80c45e0Sbonwick 	mutex_enter(&zilog->zl_lock);
1127d80c45e0Sbonwick 
1128fa9e4066Sahrens 	ASSERT(zilog->zl_stop_sync == 0);
1129fa9e4066Sahrens 
1130d80c45e0Sbonwick 	zh->zh_replay_seq = zilog->zl_replay_seq[txg & TXG_MASK];
1131fa9e4066Sahrens 
1132fa9e4066Sahrens 	if (zilog->zl_destroy_txg == txg) {
1133d80c45e0Sbonwick 		blkptr_t blk = zh->zh_log;
1134d80c45e0Sbonwick 
1135d80c45e0Sbonwick 		ASSERT(list_head(&zilog->zl_lwb_list) == NULL);
1136d80c45e0Sbonwick 		ASSERT(spa_sync_pass(spa) == 1);
1137d80c45e0Sbonwick 
1138d80c45e0Sbonwick 		bzero(zh, sizeof (zil_header_t));
1139fa9e4066Sahrens 		bzero(zilog->zl_replay_seq, sizeof (zilog->zl_replay_seq));
1140d80c45e0Sbonwick 
1141d80c45e0Sbonwick 		if (zilog->zl_keep_first) {
1142d80c45e0Sbonwick 			/*
1143d80c45e0Sbonwick 			 * If this block was part of log chain that couldn't
1144d80c45e0Sbonwick 			 * be claimed because a device was missing during
1145d80c45e0Sbonwick 			 * zil_claim(), but that device later returns,
1146d80c45e0Sbonwick 			 * then this block could erroneously appear valid.
1147d80c45e0Sbonwick 			 * To guard against this, assign a new GUID to the new
1148d80c45e0Sbonwick 			 * log chain so it doesn't matter what blk points to.
1149d80c45e0Sbonwick 			 */
1150d80c45e0Sbonwick 			zil_init_log_chain(zilog, &blk);
1151d80c45e0Sbonwick 			zh->zh_log = blk;
1152d80c45e0Sbonwick 		}
1153fa9e4066Sahrens 	}
1154fa9e4066Sahrens 
1155fa9e4066Sahrens 	for (;;) {
1156fa9e4066Sahrens 		lwb = list_head(&zilog->zl_lwb_list);
1157fa9e4066Sahrens 		if (lwb == NULL) {
1158fa9e4066Sahrens 			mutex_exit(&zilog->zl_lock);
1159fa9e4066Sahrens 			return;
1160fa9e4066Sahrens 		}
1161b19a79ecSperrin 		zh->zh_log = lwb->lwb_blk;
1162fa9e4066Sahrens 		if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg)
1163fa9e4066Sahrens 			break;
1164fa9e4066Sahrens 		list_remove(&zilog->zl_lwb_list, lwb);
1165fa9e4066Sahrens 		zio_free_blk(spa, &lwb->lwb_blk, txg);
1166fa9e4066Sahrens 		kmem_cache_free(zil_lwb_cache, lwb);
1167d63d470bSgw 
1168d63d470bSgw 		/*
1169d63d470bSgw 		 * If we don't have anything left in the lwb list then
1170d63d470bSgw 		 * we've had an allocation failure and we need to zero
1171d63d470bSgw 		 * out the zil_header blkptr so that we don't end
1172d63d470bSgw 		 * up freeing the same block twice.
1173d63d470bSgw 		 */
1174d63d470bSgw 		if (list_head(&zilog->zl_lwb_list) == NULL)
1175d63d470bSgw 			BP_ZERO(&zh->zh_log);
1176fa9e4066Sahrens 	}
1177fa9e4066Sahrens 	mutex_exit(&zilog->zl_lock);
1178fa9e4066Sahrens }
1179fa9e4066Sahrens 
1180fa9e4066Sahrens void
1181fa9e4066Sahrens zil_init(void)
1182fa9e4066Sahrens {
1183fa9e4066Sahrens 	zil_lwb_cache = kmem_cache_create("zil_lwb_cache",
11845ad82045Snd 	    sizeof (struct lwb), 0, NULL, NULL, NULL, NULL, NULL, 0);
1185fa9e4066Sahrens }
1186fa9e4066Sahrens 
1187fa9e4066Sahrens void
1188fa9e4066Sahrens zil_fini(void)
1189fa9e4066Sahrens {
1190fa9e4066Sahrens 	kmem_cache_destroy(zil_lwb_cache);
1191fa9e4066Sahrens }
1192fa9e4066Sahrens 
1193fa9e4066Sahrens zilog_t *
1194fa9e4066Sahrens zil_alloc(objset_t *os, zil_header_t *zh_phys)
1195fa9e4066Sahrens {
1196fa9e4066Sahrens 	zilog_t *zilog;
1197fa9e4066Sahrens 
1198fa9e4066Sahrens 	zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP);
1199fa9e4066Sahrens 
1200fa9e4066Sahrens 	zilog->zl_header = zh_phys;
1201fa9e4066Sahrens 	zilog->zl_os = os;
1202fa9e4066Sahrens 	zilog->zl_spa = dmu_objset_spa(os);
1203fa9e4066Sahrens 	zilog->zl_dmu_pool = dmu_objset_pool(os);
1204d80c45e0Sbonwick 	zilog->zl_destroy_txg = TXG_INITIAL - 1;
1205fa9e4066Sahrens 
12065ad82045Snd 	mutex_init(&zilog->zl_lock, NULL, MUTEX_DEFAULT, NULL);
12075ad82045Snd 
1208fa9e4066Sahrens 	list_create(&zilog->zl_itx_list, sizeof (itx_t),
1209fa9e4066Sahrens 	    offsetof(itx_t, itx_node));
1210fa9e4066Sahrens 
1211fa9e4066Sahrens 	list_create(&zilog->zl_lwb_list, sizeof (lwb_t),
1212fa9e4066Sahrens 	    offsetof(lwb_t, lwb_node));
1213fa9e4066Sahrens 
1214*17f17c2dSbonwick 	mutex_init(&zilog->zl_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
1215*17f17c2dSbonwick 
1216*17f17c2dSbonwick 	avl_create(&zilog->zl_vdev_tree, zil_vdev_compare,
1217*17f17c2dSbonwick 	    sizeof (zil_vdev_node_t), offsetof(zil_vdev_node_t, zv_node));
1218fa9e4066Sahrens 
1219fa9e4066Sahrens 	return (zilog);
1220fa9e4066Sahrens }
1221fa9e4066Sahrens 
1222fa9e4066Sahrens void
1223fa9e4066Sahrens zil_free(zilog_t *zilog)
1224fa9e4066Sahrens {
1225fa9e4066Sahrens 	lwb_t *lwb;
1226fa9e4066Sahrens 
1227fa9e4066Sahrens 	zilog->zl_stop_sync = 1;
1228fa9e4066Sahrens 
1229fa9e4066Sahrens 	while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
1230fa9e4066Sahrens 		list_remove(&zilog->zl_lwb_list, lwb);
1231fa9e4066Sahrens 		if (lwb->lwb_buf != NULL)
1232fa9e4066Sahrens 			zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
1233fa9e4066Sahrens 		kmem_cache_free(zil_lwb_cache, lwb);
1234fa9e4066Sahrens 	}
1235fa9e4066Sahrens 	list_destroy(&zilog->zl_lwb_list);
1236fa9e4066Sahrens 
1237*17f17c2dSbonwick 	avl_destroy(&zilog->zl_vdev_tree);
1238*17f17c2dSbonwick 	mutex_destroy(&zilog->zl_vdev_lock);
1239fa9e4066Sahrens 
1240fa9e4066Sahrens 	ASSERT(list_head(&zilog->zl_itx_list) == NULL);
1241fa9e4066Sahrens 	list_destroy(&zilog->zl_itx_list);
12425ad82045Snd 	mutex_destroy(&zilog->zl_lock);
1243fa9e4066Sahrens 
1244fa9e4066Sahrens 	kmem_free(zilog, sizeof (zilog_t));
1245fa9e4066Sahrens }
1246fa9e4066Sahrens 
124713f5297eSperrin /*
1248436b2950Sperrin  * return true if the initial log block is not valid
124913f5297eSperrin  */
125013f5297eSperrin static int
125113f5297eSperrin zil_empty(zilog_t *zilog)
125213f5297eSperrin {
1253d80c45e0Sbonwick 	const zil_header_t *zh = zilog->zl_header;
1254d80c45e0Sbonwick 	arc_buf_t *abuf = NULL;
125513f5297eSperrin 
1256d80c45e0Sbonwick 	if (BP_IS_HOLE(&zh->zh_log))
125713f5297eSperrin 		return (1);
125813f5297eSperrin 
1259d80c45e0Sbonwick 	if (zil_read_log_block(zilog, &zh->zh_log, &abuf) != 0)
1260d80c45e0Sbonwick 		return (1);
1261d80c45e0Sbonwick 
1262d80c45e0Sbonwick 	VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
1263d80c45e0Sbonwick 	return (0);
126413f5297eSperrin }
126513f5297eSperrin 
1266fa9e4066Sahrens /*
1267fa9e4066Sahrens  * Open an intent log.
1268fa9e4066Sahrens  */
1269fa9e4066Sahrens zilog_t *
1270fa9e4066Sahrens zil_open(objset_t *os, zil_get_data_t *get_data)
1271fa9e4066Sahrens {
1272fa9e4066Sahrens 	zilog_t *zilog = dmu_objset_zil(os);
1273fa9e4066Sahrens 
1274fa9e4066Sahrens 	zilog->zl_get_data = get_data;
1275fa9e4066Sahrens 	zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri,
1276fa9e4066Sahrens 	    2, 2, TASKQ_PREPOPULATE);
1277fa9e4066Sahrens 
1278fa9e4066Sahrens 	return (zilog);
1279fa9e4066Sahrens }
1280fa9e4066Sahrens 
1281fa9e4066Sahrens /*
1282fa9e4066Sahrens  * Close an intent log.
1283fa9e4066Sahrens  */
1284fa9e4066Sahrens void
1285fa9e4066Sahrens zil_close(zilog_t *zilog)
1286fa9e4066Sahrens {
1287d80c45e0Sbonwick 	/*
1288d80c45e0Sbonwick 	 * If the log isn't already committed, mark the objset dirty
1289d80c45e0Sbonwick 	 * (so zil_sync() will be called) and wait for that txg to sync.
1290d80c45e0Sbonwick 	 */
1291d80c45e0Sbonwick 	if (!zil_is_committed(zilog)) {
1292d80c45e0Sbonwick 		uint64_t txg;
1293d80c45e0Sbonwick 		dmu_tx_t *tx = dmu_tx_create(zilog->zl_os);
1294d80c45e0Sbonwick 		(void) dmu_tx_assign(tx, TXG_WAIT);
1295d80c45e0Sbonwick 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
1296d80c45e0Sbonwick 		txg = dmu_tx_get_txg(tx);
1297d80c45e0Sbonwick 		dmu_tx_commit(tx);
1298d80c45e0Sbonwick 		txg_wait_synced(zilog->zl_dmu_pool, txg);
1299d80c45e0Sbonwick 	}
1300d80c45e0Sbonwick 
1301fa9e4066Sahrens 	taskq_destroy(zilog->zl_clean_taskq);
1302fa9e4066Sahrens 	zilog->zl_clean_taskq = NULL;
1303fa9e4066Sahrens 	zilog->zl_get_data = NULL;
1304fa9e4066Sahrens 
1305fa9e4066Sahrens 	zil_itx_clean(zilog);
1306fa9e4066Sahrens 	ASSERT(list_head(&zilog->zl_itx_list) == NULL);
1307fa9e4066Sahrens }
1308fa9e4066Sahrens 
1309fa9e4066Sahrens /*
1310fa9e4066Sahrens  * Suspend an intent log.  While in suspended mode, we still honor
1311fa9e4066Sahrens  * synchronous semantics, but we rely on txg_wait_synced() to do it.
1312fa9e4066Sahrens  * We suspend the log briefly when taking a snapshot so that the snapshot
1313fa9e4066Sahrens  * contains all the data it's supposed to, and has an empty intent log.
1314fa9e4066Sahrens  */
1315fa9e4066Sahrens int
1316fa9e4066Sahrens zil_suspend(zilog_t *zilog)
1317fa9e4066Sahrens {
1318d80c45e0Sbonwick 	const zil_header_t *zh = zilog->zl_header;
1319fa9e4066Sahrens 
1320fa9e4066Sahrens 	mutex_enter(&zilog->zl_lock);
1321d80c45e0Sbonwick 	if (zh->zh_claim_txg != 0) {		/* unplayed log */
1322fa9e4066Sahrens 		mutex_exit(&zilog->zl_lock);
1323fa9e4066Sahrens 		return (EBUSY);
1324fa9e4066Sahrens 	}
1325d80c45e0Sbonwick 	if (zilog->zl_suspend++ != 0) {
1326d80c45e0Sbonwick 		/*
1327d80c45e0Sbonwick 		 * Someone else already began a suspend.
1328d80c45e0Sbonwick 		 * Just wait for them to finish.
1329d80c45e0Sbonwick 		 */
1330d80c45e0Sbonwick 		while (zilog->zl_suspending)
1331d80c45e0Sbonwick 			cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock);
1332d80c45e0Sbonwick 		ASSERT(BP_IS_HOLE(&zh->zh_log));
1333d80c45e0Sbonwick 		mutex_exit(&zilog->zl_lock);
1334d80c45e0Sbonwick 		return (0);
1335d80c45e0Sbonwick 	}
1336d80c45e0Sbonwick 	zilog->zl_suspending = B_TRUE;
1337fa9e4066Sahrens 	mutex_exit(&zilog->zl_lock);
1338fa9e4066Sahrens 
1339b19a79ecSperrin 	zil_commit(zilog, UINT64_MAX, 0);
1340fa9e4066Sahrens 
1341b19a79ecSperrin 	/*
1342b19a79ecSperrin 	 * Wait for any in-flight log writes to complete.
1343b19a79ecSperrin 	 */
1344fa9e4066Sahrens 	mutex_enter(&zilog->zl_lock);
1345b19a79ecSperrin 	while (zilog->zl_writer)
1346b19a79ecSperrin 		cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
1347fa9e4066Sahrens 	mutex_exit(&zilog->zl_lock);
1348fa9e4066Sahrens 
1349d80c45e0Sbonwick 	zil_destroy(zilog, B_FALSE);
1350d80c45e0Sbonwick 
1351d80c45e0Sbonwick 	mutex_enter(&zilog->zl_lock);
1352d80c45e0Sbonwick 	ASSERT(BP_IS_HOLE(&zh->zh_log));
1353d80c45e0Sbonwick 	zilog->zl_suspending = B_FALSE;
1354d80c45e0Sbonwick 	cv_broadcast(&zilog->zl_cv_suspend);
1355d80c45e0Sbonwick 	mutex_exit(&zilog->zl_lock);
1356fa9e4066Sahrens 
1357fa9e4066Sahrens 	return (0);
1358fa9e4066Sahrens }
1359fa9e4066Sahrens 
1360fa9e4066Sahrens void
1361fa9e4066Sahrens zil_resume(zilog_t *zilog)
1362fa9e4066Sahrens {
1363fa9e4066Sahrens 	mutex_enter(&zilog->zl_lock);
1364fa9e4066Sahrens 	ASSERT(zilog->zl_suspend != 0);
1365fa9e4066Sahrens 	zilog->zl_suspend--;
1366fa9e4066Sahrens 	mutex_exit(&zilog->zl_lock);
1367fa9e4066Sahrens }
1368fa9e4066Sahrens 
1369fa9e4066Sahrens typedef struct zil_replay_arg {
1370fa9e4066Sahrens 	objset_t	*zr_os;
1371fa9e4066Sahrens 	zil_replay_func_t **zr_replay;
1372fa9e4066Sahrens 	void		*zr_arg;
1373fa9e4066Sahrens 	uint64_t	*zr_txgp;
1374fa9e4066Sahrens 	boolean_t	zr_byteswap;
1375fa9e4066Sahrens 	char		*zr_lrbuf;
1376fa9e4066Sahrens } zil_replay_arg_t;
1377fa9e4066Sahrens 
1378fa9e4066Sahrens static void
1379fa9e4066Sahrens zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg)
1380fa9e4066Sahrens {
1381fa9e4066Sahrens 	zil_replay_arg_t *zr = zra;
1382d80c45e0Sbonwick 	const zil_header_t *zh = zilog->zl_header;
1383fa9e4066Sahrens 	uint64_t reclen = lr->lrc_reclen;
1384fa9e4066Sahrens 	uint64_t txtype = lr->lrc_txtype;
138567bd71c6Sperrin 	char *name;
138667bd71c6Sperrin 	int pass, error, sunk;
1387fa9e4066Sahrens 
1388fa9e4066Sahrens 	if (zilog->zl_stop_replay)
1389fa9e4066Sahrens 		return;
1390fa9e4066Sahrens 
1391fa9e4066Sahrens 	if (lr->lrc_txg < claim_txg)		/* already committed */
1392fa9e4066Sahrens 		return;
1393fa9e4066Sahrens 
1394fa9e4066Sahrens 	if (lr->lrc_seq <= zh->zh_replay_seq)	/* already replayed */
1395fa9e4066Sahrens 		return;
1396fa9e4066Sahrens 
1397da6c28aaSamw 	/* Strip case-insensitive bit, still present in log record */
1398da6c28aaSamw 	txtype &= ~TX_CI;
1399da6c28aaSamw 
1400fa9e4066Sahrens 	/*
1401fa9e4066Sahrens 	 * Make a copy of the data so we can revise and extend it.
1402fa9e4066Sahrens 	 */
1403fa9e4066Sahrens 	bcopy(lr, zr->zr_lrbuf, reclen);
1404fa9e4066Sahrens 
1405fa9e4066Sahrens 	/*
1406fa9e4066Sahrens 	 * The log block containing this lr may have been byteswapped
1407fa9e4066Sahrens 	 * so that we can easily examine common fields like lrc_txtype.
1408fa9e4066Sahrens 	 * However, the log is a mix of different data types, and only the
1409fa9e4066Sahrens 	 * replay vectors know how to byteswap their records.  Therefore, if
1410fa9e4066Sahrens 	 * the lr was byteswapped, undo it before invoking the replay vector.
1411fa9e4066Sahrens 	 */
1412fa9e4066Sahrens 	if (zr->zr_byteswap)
1413fa9e4066Sahrens 		byteswap_uint64_array(zr->zr_lrbuf, reclen);
1414fa9e4066Sahrens 
1415fa9e4066Sahrens 	/*
1416fa9e4066Sahrens 	 * If this is a TX_WRITE with a blkptr, suck in the data.
1417fa9e4066Sahrens 	 */
1418fa9e4066Sahrens 	if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) {
1419fa9e4066Sahrens 		lr_write_t *lrw = (lr_write_t *)lr;
1420fa9e4066Sahrens 		blkptr_t *wbp = &lrw->lr_blkptr;
1421fa9e4066Sahrens 		uint64_t wlen = lrw->lr_length;
1422fa9e4066Sahrens 		char *wbuf = zr->zr_lrbuf + reclen;
1423fa9e4066Sahrens 
1424fa9e4066Sahrens 		if (BP_IS_HOLE(wbp)) {	/* compressed to a hole */
1425fa9e4066Sahrens 			bzero(wbuf, wlen);
1426fa9e4066Sahrens 		} else {
1427fa9e4066Sahrens 			/*
1428fa9e4066Sahrens 			 * A subsequent write may have overwritten this block,
1429fa9e4066Sahrens 			 * in which case wbp may have been been freed and
1430fa9e4066Sahrens 			 * reallocated, and our read of wbp may fail with a
1431fa9e4066Sahrens 			 * checksum error.  We can safely ignore this because
1432fa9e4066Sahrens 			 * the later write will provide the correct data.
1433fa9e4066Sahrens 			 */
1434ea8dc4b6Seschrock 			zbookmark_t zb;
1435ea8dc4b6Seschrock 
1436ea8dc4b6Seschrock 			zb.zb_objset = dmu_objset_id(zilog->zl_os);
1437ea8dc4b6Seschrock 			zb.zb_object = lrw->lr_foid;
1438ea8dc4b6Seschrock 			zb.zb_level = -1;
1439ea8dc4b6Seschrock 			zb.zb_blkid = lrw->lr_offset / BP_GET_LSIZE(wbp);
1440ea8dc4b6Seschrock 
1441fa9e4066Sahrens 			(void) zio_wait(zio_read(NULL, zilog->zl_spa,
1442fa9e4066Sahrens 			    wbp, wbuf, BP_GET_LSIZE(wbp), NULL, NULL,
1443fa9e4066Sahrens 			    ZIO_PRIORITY_SYNC_READ,
1444ea8dc4b6Seschrock 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &zb));
1445fa9e4066Sahrens 			(void) memmove(wbuf, wbuf + lrw->lr_blkoff, wlen);
1446fa9e4066Sahrens 		}
1447fa9e4066Sahrens 	}
1448fa9e4066Sahrens 
1449fa9e4066Sahrens 	/*
1450fa9e4066Sahrens 	 * We must now do two things atomically: replay this log record,
1451fa9e4066Sahrens 	 * and update the log header to reflect the fact that we did so.
1452fa9e4066Sahrens 	 * We use the DMU's ability to assign into a specific txg to do this.
1453fa9e4066Sahrens 	 */
145467bd71c6Sperrin 	for (pass = 1, sunk = B_FALSE; /* CONSTANTCONDITION */; pass++) {
1455fa9e4066Sahrens 		uint64_t replay_txg;
1456fa9e4066Sahrens 		dmu_tx_t *replay_tx;
1457fa9e4066Sahrens 
1458fa9e4066Sahrens 		replay_tx = dmu_tx_create(zr->zr_os);
1459fa9e4066Sahrens 		error = dmu_tx_assign(replay_tx, TXG_WAIT);
1460fa9e4066Sahrens 		if (error) {
1461fa9e4066Sahrens 			dmu_tx_abort(replay_tx);
1462fa9e4066Sahrens 			break;
1463fa9e4066Sahrens 		}
1464fa9e4066Sahrens 
1465fa9e4066Sahrens 		replay_txg = dmu_tx_get_txg(replay_tx);
1466fa9e4066Sahrens 
1467fa9e4066Sahrens 		if (txtype == 0 || txtype >= TX_MAX_TYPE) {
1468fa9e4066Sahrens 			error = EINVAL;
1469fa9e4066Sahrens 		} else {
1470fa9e4066Sahrens 			/*
1471fa9e4066Sahrens 			 * On the first pass, arrange for the replay vector
1472fa9e4066Sahrens 			 * to fail its dmu_tx_assign().  That's the only way
1473fa9e4066Sahrens 			 * to ensure that those code paths remain well tested.
1474cf43fb6bSperrin 			 *
1475cf43fb6bSperrin 			 * Only byteswap (if needed) on the 1st pass.
1476fa9e4066Sahrens 			 */
1477fa9e4066Sahrens 			*zr->zr_txgp = replay_txg - (pass == 1);
1478fa9e4066Sahrens 			error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lrbuf,
1479cf43fb6bSperrin 			    zr->zr_byteswap && pass == 1);
1480fa9e4066Sahrens 			*zr->zr_txgp = TXG_NOWAIT;
1481fa9e4066Sahrens 		}
1482fa9e4066Sahrens 
1483fa9e4066Sahrens 		if (error == 0) {
1484fa9e4066Sahrens 			dsl_dataset_dirty(dmu_objset_ds(zr->zr_os), replay_tx);
1485fa9e4066Sahrens 			zilog->zl_replay_seq[replay_txg & TXG_MASK] =
1486fa9e4066Sahrens 			    lr->lrc_seq;
1487fa9e4066Sahrens 		}
1488fa9e4066Sahrens 
1489fa9e4066Sahrens 		dmu_tx_commit(replay_tx);
1490fa9e4066Sahrens 
149167bd71c6Sperrin 		if (!error)
149267bd71c6Sperrin 			return;
149367bd71c6Sperrin 
149467bd71c6Sperrin 		/*
149567bd71c6Sperrin 		 * The DMU's dnode layer doesn't see removes until the txg
149667bd71c6Sperrin 		 * commits, so a subsequent claim can spuriously fail with
149767bd71c6Sperrin 		 * EEXIST. So if we receive any error other than ERESTART
149867bd71c6Sperrin 		 * we try syncing out any removes then retrying the
149967bd71c6Sperrin 		 * transaction.
150067bd71c6Sperrin 		 */
150167bd71c6Sperrin 		if (error != ERESTART && !sunk) {
150267bd71c6Sperrin 			txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0);
150367bd71c6Sperrin 			sunk = B_TRUE;
150467bd71c6Sperrin 			continue; /* retry */
150567bd71c6Sperrin 		}
150667bd71c6Sperrin 
1507fa9e4066Sahrens 		if (error != ERESTART)
1508fa9e4066Sahrens 			break;
1509fa9e4066Sahrens 
1510fa9e4066Sahrens 		if (pass != 1)
1511fa9e4066Sahrens 			txg_wait_open(spa_get_dsl(zilog->zl_spa),
1512fa9e4066Sahrens 			    replay_txg + 1);
1513fa9e4066Sahrens 
1514fa9e4066Sahrens 		dprintf("pass %d, retrying\n", pass);
1515fa9e4066Sahrens 	}
1516fa9e4066Sahrens 
151767bd71c6Sperrin 	ASSERT(error && error != ERESTART);
151867bd71c6Sperrin 	name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
151967bd71c6Sperrin 	dmu_objset_name(zr->zr_os, name);
152067bd71c6Sperrin 	cmn_err(CE_WARN, "ZFS replay transaction error %d, "
1521da6c28aaSamw 	    "dataset %s, seq 0x%llx, txtype %llu %s\n",
1522da6c28aaSamw 	    error, name, (u_longlong_t)lr->lrc_seq, (u_longlong_t)txtype,
1523da6c28aaSamw 	    (lr->lrc_txtype & TX_CI) ? "CI" : "");
152467bd71c6Sperrin 	zilog->zl_stop_replay = 1;
152567bd71c6Sperrin 	kmem_free(name, MAXNAMELEN);
152667bd71c6Sperrin }
1527fa9e4066Sahrens 
152867bd71c6Sperrin /* ARGSUSED */
152967bd71c6Sperrin static void
153067bd71c6Sperrin zil_incr_blks(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
153167bd71c6Sperrin {
153267bd71c6Sperrin 	zilog->zl_replay_blks++;
1533fa9e4066Sahrens }
1534fa9e4066Sahrens 
1535fa9e4066Sahrens /*
153613f5297eSperrin  * If this dataset has a non-empty intent log, replay it and destroy it.
1537fa9e4066Sahrens  */
1538fa9e4066Sahrens void
1539fa9e4066Sahrens zil_replay(objset_t *os, void *arg, uint64_t *txgp,
1540893a6d32Sahrens 	zil_replay_func_t *replay_func[TX_MAX_TYPE])
1541fa9e4066Sahrens {
1542fa9e4066Sahrens 	zilog_t *zilog = dmu_objset_zil(os);
1543d80c45e0Sbonwick 	const zil_header_t *zh = zilog->zl_header;
1544d80c45e0Sbonwick 	zil_replay_arg_t zr;
154513f5297eSperrin 
154613f5297eSperrin 	if (zil_empty(zilog)) {
1547d80c45e0Sbonwick 		zil_destroy(zilog, B_TRUE);
154813f5297eSperrin 		return;
154913f5297eSperrin 	}
1550fa9e4066Sahrens 
1551fa9e4066Sahrens 	zr.zr_os = os;
1552fa9e4066Sahrens 	zr.zr_replay = replay_func;
1553fa9e4066Sahrens 	zr.zr_arg = arg;
1554fa9e4066Sahrens 	zr.zr_txgp = txgp;
1555d80c45e0Sbonwick 	zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log);
1556fa9e4066Sahrens 	zr.zr_lrbuf = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP);
1557fa9e4066Sahrens 
1558fa9e4066Sahrens 	/*
1559fa9e4066Sahrens 	 * Wait for in-progress removes to sync before starting replay.
1560fa9e4066Sahrens 	 */
1561fa9e4066Sahrens 	txg_wait_synced(zilog->zl_dmu_pool, 0);
1562fa9e4066Sahrens 
1563fa9e4066Sahrens 	zilog->zl_stop_replay = 0;
156467bd71c6Sperrin 	zilog->zl_replay_time = lbolt;
156567bd71c6Sperrin 	ASSERT(zilog->zl_replay_blks == 0);
156667bd71c6Sperrin 	(void) zil_parse(zilog, zil_incr_blks, zil_replay_log_record, &zr,
1567d80c45e0Sbonwick 	    zh->zh_claim_txg);
1568fa9e4066Sahrens 	kmem_free(zr.zr_lrbuf, 2 * SPA_MAXBLOCKSIZE);
1569fa9e4066Sahrens 
1570d80c45e0Sbonwick 	zil_destroy(zilog, B_FALSE);
1571fa9e4066Sahrens }
1572436b2950Sperrin 
1573436b2950Sperrin /*
1574436b2950Sperrin  * Report whether all transactions are committed
1575436b2950Sperrin  */
1576436b2950Sperrin int
1577436b2950Sperrin zil_is_committed(zilog_t *zilog)
1578436b2950Sperrin {
1579436b2950Sperrin 	lwb_t *lwb;
1580b19a79ecSperrin 	int ret;
1581b19a79ecSperrin 
1582b19a79ecSperrin 	mutex_enter(&zilog->zl_lock);
1583b19a79ecSperrin 	while (zilog->zl_writer)
1584b19a79ecSperrin 		cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
1585b19a79ecSperrin 
1586b19a79ecSperrin 	/* recent unpushed intent log transactions? */
1587b19a79ecSperrin 	if (!list_is_empty(&zilog->zl_itx_list)) {
1588b19a79ecSperrin 		ret = B_FALSE;
1589b19a79ecSperrin 		goto out;
1590b19a79ecSperrin 	}
1591436b2950Sperrin 
1592b19a79ecSperrin 	/* intent log never used? */
1593b19a79ecSperrin 	lwb = list_head(&zilog->zl_lwb_list);
1594b19a79ecSperrin 	if (lwb == NULL) {
1595b19a79ecSperrin 		ret = B_TRUE;
1596b19a79ecSperrin 		goto out;
1597b19a79ecSperrin 	}
1598436b2950Sperrin 
1599436b2950Sperrin 	/*
1600b19a79ecSperrin 	 * more than 1 log buffer means zil_sync() hasn't yet freed
1601b19a79ecSperrin 	 * entries after a txg has committed
1602436b2950Sperrin 	 */
1603b19a79ecSperrin 	if (list_next(&zilog->zl_lwb_list, lwb)) {
1604b19a79ecSperrin 		ret = B_FALSE;
1605b19a79ecSperrin 		goto out;
1606b19a79ecSperrin 	}
1607b19a79ecSperrin 
1608436b2950Sperrin 	ASSERT(zil_empty(zilog));
1609b19a79ecSperrin 	ret = B_TRUE;
1610b19a79ecSperrin out:
1611b19a79ecSperrin 	cv_broadcast(&zilog->zl_cv_writer);
1612b19a79ecSperrin 	mutex_exit(&zilog->zl_lock);
1613b19a79ecSperrin 	return (ret);
1614436b2950Sperrin }
1615