xref: /onnv-gate/usr/src/uts/common/fs/zfs/zil.c (revision 1807)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51472Sperrin  * Common Development and Distribution License (the "License").
61472Sperrin  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
221362Sperrin  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
27789Sahrens 
28789Sahrens #include <sys/zfs_context.h>
29789Sahrens #include <sys/spa.h>
30789Sahrens #include <sys/dmu.h>
31789Sahrens #include <sys/zap.h>
32789Sahrens #include <sys/arc.h>
33789Sahrens #include <sys/stat.h>
34789Sahrens #include <sys/resource.h>
35789Sahrens #include <sys/zil.h>
36789Sahrens #include <sys/zil_impl.h>
37789Sahrens #include <sys/dsl_dataset.h>
38789Sahrens #include <sys/vdev.h>
39789Sahrens 
40789Sahrens /*
41789Sahrens  * The zfs intent log (ZIL) saves transaction records of system calls
42789Sahrens  * that change the file system in memory with enough information
43789Sahrens  * to be able to replay them. These are stored in memory until
44789Sahrens  * either the DMU transaction group (txg) commits them to the stable pool
45789Sahrens  * and they can be discarded, or they are flushed to the stable log
46789Sahrens  * (also in the pool) due to a fsync, O_DSYNC or other synchronous
47789Sahrens  * requirement. In the event of a panic or power fail then those log
48789Sahrens  * records (transactions) are replayed.
49789Sahrens  *
50789Sahrens  * There is one ZIL per file system. Its on-disk (pool) format consists
51789Sahrens  * of 3 parts:
52789Sahrens  *
53789Sahrens  * 	- ZIL header
54789Sahrens  * 	- ZIL blocks
55789Sahrens  * 	- ZIL records
56789Sahrens  *
57789Sahrens  * A log record holds a system call transaction. Log blocks can
58789Sahrens  * hold many log records and the blocks are chained together.
59789Sahrens  * Each ZIL block contains a block pointer (blkptr_t) to the next
60789Sahrens  * ZIL block in the chain. The ZIL header points to the first
61789Sahrens  * block in the chain. Note there is not a fixed place in the pool
62789Sahrens  * to hold blocks. They are dynamically allocated and freed as
63789Sahrens  * needed from the blocks available. Figure X shows the ZIL structure:
64789Sahrens  */
65789Sahrens 
66789Sahrens /*
67789Sahrens  * These global ZIL switches affect all pools
68789Sahrens  */
69789Sahrens int zil_disable = 0;	/* disable intent logging */
70789Sahrens int zil_always = 0;	/* make every transaction synchronous */
71789Sahrens int zil_purge = 0;	/* at pool open, just throw everything away */
72789Sahrens int zil_noflush = 0;	/* don't flush write cache buffers on disks */
73789Sahrens 
74789Sahrens static kmem_cache_t *zil_lwb_cache;
75789Sahrens 
76789Sahrens static int
77789Sahrens zil_dva_compare(const void *x1, const void *x2)
78789Sahrens {
79789Sahrens 	const dva_t *dva1 = x1;
80789Sahrens 	const dva_t *dva2 = x2;
81789Sahrens 
82789Sahrens 	if (DVA_GET_VDEV(dva1) < DVA_GET_VDEV(dva2))
83789Sahrens 		return (-1);
84789Sahrens 	if (DVA_GET_VDEV(dva1) > DVA_GET_VDEV(dva2))
85789Sahrens 		return (1);
86789Sahrens 
87789Sahrens 	if (DVA_GET_OFFSET(dva1) < DVA_GET_OFFSET(dva2))
88789Sahrens 		return (-1);
89789Sahrens 	if (DVA_GET_OFFSET(dva1) > DVA_GET_OFFSET(dva2))
90789Sahrens 		return (1);
91789Sahrens 
92789Sahrens 	return (0);
93789Sahrens }
94789Sahrens 
95789Sahrens static void
96789Sahrens zil_dva_tree_init(avl_tree_t *t)
97789Sahrens {
98789Sahrens 	avl_create(t, zil_dva_compare, sizeof (zil_dva_node_t),
99789Sahrens 	    offsetof(zil_dva_node_t, zn_node));
100789Sahrens }
101789Sahrens 
102789Sahrens static void
103789Sahrens zil_dva_tree_fini(avl_tree_t *t)
104789Sahrens {
105789Sahrens 	zil_dva_node_t *zn;
106789Sahrens 	void *cookie = NULL;
107789Sahrens 
108789Sahrens 	while ((zn = avl_destroy_nodes(t, &cookie)) != NULL)
109789Sahrens 		kmem_free(zn, sizeof (zil_dva_node_t));
110789Sahrens 
111789Sahrens 	avl_destroy(t);
112789Sahrens }
113789Sahrens 
114789Sahrens static int
115789Sahrens zil_dva_tree_add(avl_tree_t *t, dva_t *dva)
116789Sahrens {
117789Sahrens 	zil_dva_node_t *zn;
118789Sahrens 	avl_index_t where;
119789Sahrens 
120789Sahrens 	if (avl_find(t, dva, &where) != NULL)
121789Sahrens 		return (EEXIST);
122789Sahrens 
123789Sahrens 	zn = kmem_alloc(sizeof (zil_dva_node_t), KM_SLEEP);
124789Sahrens 	zn->zn_dva = *dva;
125789Sahrens 	avl_insert(t, zn, where);
126789Sahrens 
127789Sahrens 	return (0);
128789Sahrens }
129789Sahrens 
130*1807Sbonwick static zil_header_t *
131*1807Sbonwick zil_header_in_syncing_context(zilog_t *zilog)
132*1807Sbonwick {
133*1807Sbonwick 	return ((zil_header_t *)zilog->zl_header);
134*1807Sbonwick }
135*1807Sbonwick 
136*1807Sbonwick static void
137*1807Sbonwick zil_init_log_chain(zilog_t *zilog, blkptr_t *bp)
138*1807Sbonwick {
139*1807Sbonwick 	zio_cksum_t *zc = &bp->blk_cksum;
140*1807Sbonwick 
141*1807Sbonwick 	zc->zc_word[ZIL_ZC_GUID_0] = spa_get_random(-1ULL);
142*1807Sbonwick 	zc->zc_word[ZIL_ZC_GUID_1] = spa_get_random(-1ULL);
143*1807Sbonwick 	zc->zc_word[ZIL_ZC_OBJSET] = dmu_objset_id(zilog->zl_os);
144*1807Sbonwick 	zc->zc_word[ZIL_ZC_SEQ] = 1ULL;
145*1807Sbonwick }
146*1807Sbonwick 
147789Sahrens /*
148789Sahrens  * Read a log block, make sure it's valid, and byteswap it if necessary.
149789Sahrens  */
150789Sahrens static int
151*1807Sbonwick zil_read_log_block(zilog_t *zilog, const blkptr_t *bp, arc_buf_t **abufpp)
152789Sahrens {
153*1807Sbonwick 	blkptr_t blk = *bp;
1541544Seschrock 	zbookmark_t zb;
155789Sahrens 	int error;
156789Sahrens 
157*1807Sbonwick 	zb.zb_objset = bp->blk_cksum.zc_word[ZIL_ZC_OBJSET];
1581544Seschrock 	zb.zb_object = 0;
1591544Seschrock 	zb.zb_level = -1;
160*1807Sbonwick 	zb.zb_blkid = bp->blk_cksum.zc_word[ZIL_ZC_SEQ];
161*1807Sbonwick 
162*1807Sbonwick 	*abufpp = NULL;
163*1807Sbonwick 
164*1807Sbonwick 	error = arc_read(NULL, zilog->zl_spa, &blk, byteswap_uint64_array,
165*1807Sbonwick 	    arc_getbuf_func, abufpp, ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL |
166*1807Sbonwick 	    ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB, ARC_WAIT, &zb);
167*1807Sbonwick 
168*1807Sbonwick 	if (error == 0) {
169*1807Sbonwick 		char *data = (*abufpp)->b_data;
170*1807Sbonwick 		uint64_t blksz = BP_GET_LSIZE(bp);
171*1807Sbonwick 		zil_trailer_t *ztp = (zil_trailer_t *)(data + blksz) - 1;
172*1807Sbonwick 		zio_cksum_t cksum = bp->blk_cksum;
1731544Seschrock 
174*1807Sbonwick 		/*
175*1807Sbonwick 		 * Sequence numbers should be... sequential.  The checksum
176*1807Sbonwick 		 * verifier for the next block should be bp's checksum plus 1.
177*1807Sbonwick 		 */
178*1807Sbonwick 		cksum.zc_word[ZIL_ZC_SEQ]++;
179*1807Sbonwick 
180*1807Sbonwick 		if (bcmp(&cksum, &ztp->zit_next_blk.blk_cksum, sizeof (cksum)))
181*1807Sbonwick 			error = ESTALE;
182*1807Sbonwick 		else if (BP_IS_HOLE(&ztp->zit_next_blk))
183*1807Sbonwick 			error = ENOENT;
184*1807Sbonwick 		else if (ztp->zit_nused > (blksz - sizeof (zil_trailer_t)))
185*1807Sbonwick 			error = EOVERFLOW;
186*1807Sbonwick 
187*1807Sbonwick 		if (error) {
188*1807Sbonwick 			VERIFY(arc_buf_remove_ref(*abufpp, abufpp) == 1);
189*1807Sbonwick 			*abufpp = NULL;
190*1807Sbonwick 		}
191789Sahrens 	}
192789Sahrens 
193*1807Sbonwick 	dprintf("error %d on %llu:%llu\n", error, zb.zb_objset, zb.zb_blkid);
194789Sahrens 
195*1807Sbonwick 	return (error);
196789Sahrens }
197789Sahrens 
198789Sahrens /*
199789Sahrens  * Parse the intent log, and call parse_func for each valid record within.
200*1807Sbonwick  * Return the highest sequence number.
201789Sahrens  */
202*1807Sbonwick uint64_t
203789Sahrens zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func,
204789Sahrens     zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg)
205789Sahrens {
206*1807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
207*1807Sbonwick 	uint64_t claim_seq = zh->zh_claim_seq;
208*1807Sbonwick 	uint64_t seq = 0;
209*1807Sbonwick 	uint64_t max_seq = 0;
210*1807Sbonwick 	blkptr_t blk = zh->zh_log;
211*1807Sbonwick 	arc_buf_t *abuf;
212789Sahrens 	char *lrbuf, *lrp;
213789Sahrens 	zil_trailer_t *ztp;
214789Sahrens 	int reclen, error;
215789Sahrens 
216789Sahrens 	if (BP_IS_HOLE(&blk))
217*1807Sbonwick 		return (max_seq);
218789Sahrens 
219789Sahrens 	/*
220789Sahrens 	 * Starting at the block pointed to by zh_log we read the log chain.
221789Sahrens 	 * For each block in the chain we strongly check that block to
222789Sahrens 	 * ensure its validity.  We stop when an invalid block is found.
223789Sahrens 	 * For each block pointer in the chain we call parse_blk_func().
224789Sahrens 	 * For each record in each valid block we call parse_lr_func().
225*1807Sbonwick 	 * If the log has been claimed, stop if we encounter a sequence
226*1807Sbonwick 	 * number greater than the highest claimed sequence number.
227789Sahrens 	 */
228789Sahrens 	zil_dva_tree_init(&zilog->zl_dva_tree);
229789Sahrens 	for (;;) {
230*1807Sbonwick 		seq = blk.blk_cksum.zc_word[ZIL_ZC_SEQ];
231*1807Sbonwick 
232*1807Sbonwick 		if (claim_seq != 0 && seq > claim_seq)
233*1807Sbonwick 			break;
234*1807Sbonwick 
235*1807Sbonwick 		ASSERT(max_seq < seq);
236*1807Sbonwick 		max_seq = seq;
237*1807Sbonwick 
238*1807Sbonwick 		error = zil_read_log_block(zilog, &blk, &abuf);
239789Sahrens 
240789Sahrens 		if (parse_blk_func != NULL)
241789Sahrens 			parse_blk_func(zilog, &blk, arg, txg);
242789Sahrens 
243789Sahrens 		if (error)
244789Sahrens 			break;
245789Sahrens 
246*1807Sbonwick 		lrbuf = abuf->b_data;
247789Sahrens 		ztp = (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1;
248789Sahrens 		blk = ztp->zit_next_blk;
249789Sahrens 
250*1807Sbonwick 		if (parse_lr_func == NULL) {
251*1807Sbonwick 			VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
252789Sahrens 			continue;
253*1807Sbonwick 		}
254789Sahrens 
255789Sahrens 		for (lrp = lrbuf; lrp < lrbuf + ztp->zit_nused; lrp += reclen) {
256789Sahrens 			lr_t *lr = (lr_t *)lrp;
257789Sahrens 			reclen = lr->lrc_reclen;
258789Sahrens 			ASSERT3U(reclen, >=, sizeof (lr_t));
259789Sahrens 			parse_lr_func(zilog, lr, arg, txg);
260789Sahrens 		}
261*1807Sbonwick 		VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
262789Sahrens 	}
263789Sahrens 	zil_dva_tree_fini(&zilog->zl_dva_tree);
264*1807Sbonwick 
265*1807Sbonwick 	return (max_seq);
266789Sahrens }
267789Sahrens 
268789Sahrens /* ARGSUSED */
269789Sahrens static void
270789Sahrens zil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg)
271789Sahrens {
272789Sahrens 	spa_t *spa = zilog->zl_spa;
273789Sahrens 	int err;
274789Sahrens 
275789Sahrens 	/*
276789Sahrens 	 * Claim log block if not already committed and not already claimed.
277789Sahrens 	 */
278789Sahrens 	if (bp->blk_birth >= first_txg &&
279789Sahrens 	    zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp)) == 0) {
280789Sahrens 		err = zio_wait(zio_claim(NULL, spa, first_txg, bp, NULL, NULL));
281789Sahrens 		ASSERT(err == 0);
282789Sahrens 	}
283789Sahrens }
284789Sahrens 
285789Sahrens static void
286789Sahrens zil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg)
287789Sahrens {
288789Sahrens 	if (lrc->lrc_txtype == TX_WRITE) {
289789Sahrens 		lr_write_t *lr = (lr_write_t *)lrc;
290789Sahrens 		zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg);
291789Sahrens 	}
292789Sahrens }
293789Sahrens 
294789Sahrens /* ARGSUSED */
295789Sahrens static void
296789Sahrens zil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg)
297789Sahrens {
298789Sahrens 	zio_free_blk(zilog->zl_spa, bp, dmu_tx_get_txg(tx));
299789Sahrens }
300789Sahrens 
301789Sahrens static void
302789Sahrens zil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg)
303789Sahrens {
304789Sahrens 	/*
305789Sahrens 	 * If we previously claimed it, we need to free it.
306789Sahrens 	 */
307789Sahrens 	if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE) {
308789Sahrens 		lr_write_t *lr = (lr_write_t *)lrc;
309789Sahrens 		blkptr_t *bp = &lr->lr_blkptr;
310789Sahrens 		if (bp->blk_birth >= claim_txg &&
311789Sahrens 		    !zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp))) {
312789Sahrens 			(void) arc_free(NULL, zilog->zl_spa,
313789Sahrens 			    dmu_tx_get_txg(tx), bp, NULL, NULL, ARC_WAIT);
314789Sahrens 		}
315789Sahrens 	}
316789Sahrens }
317789Sahrens 
318789Sahrens /*
319789Sahrens  * Create an on-disk intent log.
320789Sahrens  */
321789Sahrens static void
322789Sahrens zil_create(zilog_t *zilog)
323789Sahrens {
324*1807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
325789Sahrens 	lwb_t *lwb;
326*1807Sbonwick 	uint64_t txg = 0;
327*1807Sbonwick 	dmu_tx_t *tx = NULL;
328789Sahrens 	blkptr_t blk;
329*1807Sbonwick 	int error = 0;
330789Sahrens 
331789Sahrens 	/*
332*1807Sbonwick 	 * Wait for any previous destroy to complete.
333789Sahrens 	 */
334*1807Sbonwick 	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
335*1807Sbonwick 
336*1807Sbonwick 	ASSERT(zh->zh_claim_txg == 0);
337*1807Sbonwick 	ASSERT(zh->zh_replay_seq == 0);
338*1807Sbonwick 
339*1807Sbonwick 	blk = zh->zh_log;
340789Sahrens 
341789Sahrens 	/*
342*1807Sbonwick 	 * If we don't already have an initial log block, allocate one now.
343789Sahrens 	 */
344*1807Sbonwick 	if (BP_IS_HOLE(&blk)) {
345*1807Sbonwick 		tx = dmu_tx_create(zilog->zl_os);
346*1807Sbonwick 		(void) dmu_tx_assign(tx, TXG_WAIT);
347*1807Sbonwick 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
348*1807Sbonwick 		txg = dmu_tx_get_txg(tx);
349*1807Sbonwick 
350*1807Sbonwick 		error = zio_alloc_blk(zilog->zl_spa, ZIL_MIN_BLKSZ, &blk, txg);
351*1807Sbonwick 
352*1807Sbonwick 		if (error == 0)
353*1807Sbonwick 			zil_init_log_chain(zilog, &blk);
3541362Sperrin 	}
355*1807Sbonwick 
356*1807Sbonwick 	/*
357*1807Sbonwick 	 * Allocate a log write buffer (lwb) for the first log block.
358*1807Sbonwick 	 */
359789Sahrens 	if (error == 0) {
360789Sahrens 		lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP);
361789Sahrens 		lwb->lwb_zilog = zilog;
362789Sahrens 		lwb->lwb_blk = blk;
363789Sahrens 		lwb->lwb_nused = 0;
364789Sahrens 		lwb->lwb_sz = BP_GET_LSIZE(&lwb->lwb_blk);
365789Sahrens 		lwb->lwb_buf = zio_buf_alloc(lwb->lwb_sz);
366789Sahrens 		lwb->lwb_max_txg = txg;
367789Sahrens 		lwb->lwb_seq = 0;
368789Sahrens 		lwb->lwb_state = UNWRITTEN;
369789Sahrens 		mutex_enter(&zilog->zl_lock);
370789Sahrens 		list_insert_tail(&zilog->zl_lwb_list, lwb);
371789Sahrens 		mutex_exit(&zilog->zl_lock);
372789Sahrens 	}
373789Sahrens 
374*1807Sbonwick 	/*
375*1807Sbonwick 	 * If we just allocated the first log block, commit our transaction
376*1807Sbonwick 	 * and wait for zil_sync() to stuff the block poiner into zh_log.
377*1807Sbonwick 	 * (zh is part of the MOS, so we cannot modify it in open context.)
378*1807Sbonwick 	 */
379*1807Sbonwick 	if (tx != NULL) {
380*1807Sbonwick 		dmu_tx_commit(tx);
3811362Sperrin 		txg_wait_synced(zilog->zl_dmu_pool, txg);
382*1807Sbonwick 	}
383*1807Sbonwick 
384*1807Sbonwick 	ASSERT(bcmp(&blk, &zh->zh_log, sizeof (blk)) == 0);
385789Sahrens }
386789Sahrens 
387789Sahrens /*
388789Sahrens  * In one tx, free all log blocks and clear the log header.
389*1807Sbonwick  * If keep_first is set, then we're replaying a log with no content.
390*1807Sbonwick  * We want to keep the first block, however, so that the first
391*1807Sbonwick  * synchronous transaction doesn't require a txg_wait_synced()
392*1807Sbonwick  * in zil_create().  We don't need to txg_wait_synced() here either
393*1807Sbonwick  * when keep_first is set, because both zil_create() and zil_destroy()
394*1807Sbonwick  * will wait for any in-progress destroys to complete.
395789Sahrens  */
396789Sahrens void
397*1807Sbonwick zil_destroy(zilog_t *zilog, boolean_t keep_first)
398789Sahrens {
399*1807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
400*1807Sbonwick 	lwb_t *lwb;
401789Sahrens 	dmu_tx_t *tx;
402789Sahrens 	uint64_t txg;
403789Sahrens 
404*1807Sbonwick 	/*
405*1807Sbonwick 	 * Wait for any previous destroy to complete.
406*1807Sbonwick 	 */
407*1807Sbonwick 	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
408789Sahrens 
409*1807Sbonwick 	if (BP_IS_HOLE(&zh->zh_log))
410789Sahrens 		return;
411789Sahrens 
412789Sahrens 	tx = dmu_tx_create(zilog->zl_os);
413789Sahrens 	(void) dmu_tx_assign(tx, TXG_WAIT);
414789Sahrens 	dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
415789Sahrens 	txg = dmu_tx_get_txg(tx);
416789Sahrens 
417*1807Sbonwick 	mutex_enter(&zilog->zl_lock);
418*1807Sbonwick 
419*1807Sbonwick 	ASSERT3U(zilog->zl_destroy_txg, <, txg);
420789Sahrens 	zilog->zl_destroy_txg = txg;
421*1807Sbonwick 	zilog->zl_keep_first = keep_first;
422*1807Sbonwick 
423*1807Sbonwick 	if (!list_is_empty(&zilog->zl_lwb_list)) {
424*1807Sbonwick 		ASSERT(zh->zh_claim_txg == 0);
425*1807Sbonwick 		ASSERT(!keep_first);
426*1807Sbonwick 		while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
427*1807Sbonwick 			list_remove(&zilog->zl_lwb_list, lwb);
428*1807Sbonwick 			if (lwb->lwb_buf != NULL)
429*1807Sbonwick 				zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
430*1807Sbonwick 			zio_free_blk(zilog->zl_spa, &lwb->lwb_blk, txg);
431*1807Sbonwick 			kmem_cache_free(zil_lwb_cache, lwb);
432*1807Sbonwick 		}
433*1807Sbonwick 		mutex_exit(&zilog->zl_lock);
434*1807Sbonwick 	} else {
435*1807Sbonwick 		mutex_exit(&zilog->zl_lock);
436*1807Sbonwick 		if (!keep_first) {
437*1807Sbonwick 			(void) zil_parse(zilog, zil_free_log_block,
438*1807Sbonwick 			    zil_free_log_record, tx, zh->zh_claim_txg);
439*1807Sbonwick 		}
440*1807Sbonwick 	}
441789Sahrens 
442789Sahrens 	dmu_tx_commit(tx);
443789Sahrens 
444*1807Sbonwick 	if (keep_first)			/* no need to wait in this case */
445*1807Sbonwick 		return;
446*1807Sbonwick 
447*1807Sbonwick 	txg_wait_synced(zilog->zl_dmu_pool, txg);
448*1807Sbonwick 	ASSERT(BP_IS_HOLE(&zh->zh_log));
449789Sahrens }
450789Sahrens 
451789Sahrens void
452789Sahrens zil_claim(char *osname, void *txarg)
453789Sahrens {
454789Sahrens 	dmu_tx_t *tx = txarg;
455789Sahrens 	uint64_t first_txg = dmu_tx_get_txg(tx);
456789Sahrens 	zilog_t *zilog;
457789Sahrens 	zil_header_t *zh;
458789Sahrens 	objset_t *os;
459789Sahrens 	int error;
460789Sahrens 
461789Sahrens 	error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_STANDARD, &os);
462789Sahrens 	if (error) {
463789Sahrens 		cmn_err(CE_WARN, "can't process intent log for %s", osname);
464789Sahrens 		return;
465789Sahrens 	}
466789Sahrens 
467789Sahrens 	zilog = dmu_objset_zil(os);
468*1807Sbonwick 	zh = zil_header_in_syncing_context(zilog);
469789Sahrens 
470789Sahrens 	/*
471*1807Sbonwick 	 * Claim all log blocks if we haven't already done so, and remember
472*1807Sbonwick 	 * the highest claimed sequence number.  This ensures that if we can
473*1807Sbonwick 	 * read only part of the log now (e.g. due to a missing device),
474*1807Sbonwick 	 * but we can read the entire log later, we will not try to replay
475*1807Sbonwick 	 * or destroy beyond the last block we successfully claimed.
476789Sahrens 	 */
477789Sahrens 	ASSERT3U(zh->zh_claim_txg, <=, first_txg);
478789Sahrens 	if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) {
479789Sahrens 		zh->zh_claim_txg = first_txg;
480*1807Sbonwick 		zh->zh_claim_seq = zil_parse(zilog, zil_claim_log_block,
481*1807Sbonwick 		    zil_claim_log_record, tx, first_txg);
482789Sahrens 		dsl_dataset_dirty(dmu_objset_ds(os), tx);
483789Sahrens 	}
484*1807Sbonwick 
485789Sahrens 	ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1));
486789Sahrens 	dmu_objset_close(os);
487789Sahrens }
488789Sahrens 
489789Sahrens void
490789Sahrens zil_add_vdev(zilog_t *zilog, uint64_t vdev, uint64_t seq)
491789Sahrens {
492789Sahrens 	zil_vdev_t *zv;
493789Sahrens 
494789Sahrens 	if (zil_noflush)
495789Sahrens 		return;
496789Sahrens 
497789Sahrens 	ASSERT(MUTEX_HELD(&zilog->zl_lock));
498789Sahrens 	zv = kmem_alloc(sizeof (zil_vdev_t), KM_SLEEP);
499789Sahrens 	zv->vdev = vdev;
500789Sahrens 	zv->seq = seq;
501789Sahrens 	list_insert_tail(&zilog->zl_vdev_list, zv);
502789Sahrens }
503789Sahrens 
504789Sahrens void
505789Sahrens zil_flush_vdevs(zilog_t *zilog, uint64_t seq)
506789Sahrens {
507789Sahrens 	vdev_t *vd;
508789Sahrens 	zil_vdev_t *zv, *zv2;
509789Sahrens 	zio_t *zio;
510789Sahrens 	spa_t *spa;
511789Sahrens 	uint64_t vdev;
512789Sahrens 
513789Sahrens 	if (zil_noflush)
514789Sahrens 		return;
515789Sahrens 
516789Sahrens 	ASSERT(MUTEX_HELD(&zilog->zl_lock));
517789Sahrens 
518789Sahrens 	spa = zilog->zl_spa;
519789Sahrens 	zio = NULL;
520789Sahrens 
521789Sahrens 	while ((zv = list_head(&zilog->zl_vdev_list)) != NULL &&
522789Sahrens 	    zv->seq <= seq) {
523789Sahrens 		vdev = zv->vdev;
524789Sahrens 		list_remove(&zilog->zl_vdev_list, zv);
525789Sahrens 		kmem_free(zv, sizeof (zil_vdev_t));
526789Sahrens 
527789Sahrens 		/*
528789Sahrens 		 * remove all chained entries <= seq with same vdev
529789Sahrens 		 */
530789Sahrens 		zv = list_head(&zilog->zl_vdev_list);
531789Sahrens 		while (zv && zv->seq <= seq) {
532789Sahrens 			zv2 = list_next(&zilog->zl_vdev_list, zv);
533789Sahrens 			if (zv->vdev == vdev) {
534789Sahrens 				list_remove(&zilog->zl_vdev_list, zv);
535789Sahrens 				kmem_free(zv, sizeof (zil_vdev_t));
536789Sahrens 			}
537789Sahrens 			zv = zv2;
538789Sahrens 		}
539789Sahrens 
540789Sahrens 		/* flush the write cache for this vdev */
541789Sahrens 		mutex_exit(&zilog->zl_lock);
542789Sahrens 		if (zio == NULL)
543789Sahrens 			zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
544789Sahrens 		vd = vdev_lookup_top(spa, vdev);
545789Sahrens 		ASSERT(vd);
546789Sahrens 		(void) zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE,
547789Sahrens 		    NULL, NULL, ZIO_PRIORITY_NOW,
548789Sahrens 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
549789Sahrens 		mutex_enter(&zilog->zl_lock);
550789Sahrens 	}
551789Sahrens 
552789Sahrens 	/*
553789Sahrens 	 * Wait for all the flushes to complete.  Not all devices actually
554789Sahrens 	 * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails.
555789Sahrens 	 */
5561141Sperrin 	if (zio != NULL) {
5571141Sperrin 		mutex_exit(&zilog->zl_lock);
558789Sahrens 		(void) zio_wait(zio);
5591141Sperrin 		mutex_enter(&zilog->zl_lock);
5601141Sperrin 	}
561789Sahrens }
562789Sahrens 
563789Sahrens /*
564789Sahrens  * Function called when a log block write completes
565789Sahrens  */
566789Sahrens static void
567789Sahrens zil_lwb_write_done(zio_t *zio)
568789Sahrens {
569789Sahrens 	lwb_t *prev;
570789Sahrens 	lwb_t *lwb = zio->io_private;
571789Sahrens 	zilog_t *zilog = lwb->lwb_zilog;
572789Sahrens 	uint64_t max_seq;
573789Sahrens 
574789Sahrens 	/*
575789Sahrens 	 * Now that we've written this log block, we have a stable pointer
576789Sahrens 	 * to the next block in the chain, so it's OK to let the txg in
577789Sahrens 	 * which we allocated the next block sync.
578789Sahrens 	 */
579789Sahrens 	txg_rele_to_sync(&lwb->lwb_txgh);
580789Sahrens 
581789Sahrens 	zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
582789Sahrens 	mutex_enter(&zilog->zl_lock);
583789Sahrens 	lwb->lwb_buf = NULL;
584789Sahrens 	if (zio->io_error) {
585789Sahrens 		zilog->zl_log_error = B_TRUE;
586789Sahrens 		mutex_exit(&zilog->zl_lock);
587789Sahrens 		cv_broadcast(&zilog->zl_cv_seq);
588789Sahrens 		return;
589789Sahrens 	}
590789Sahrens 
591789Sahrens 	prev = list_prev(&zilog->zl_lwb_list, lwb);
592789Sahrens 	if (prev && prev->lwb_state != SEQ_COMPLETE) {
593789Sahrens 		/* There's an unwritten buffer in the chain before this one */
594789Sahrens 		lwb->lwb_state = SEQ_INCOMPLETE;
595789Sahrens 		mutex_exit(&zilog->zl_lock);
596789Sahrens 		return;
597789Sahrens 	}
598789Sahrens 
599789Sahrens 	max_seq = lwb->lwb_seq;
600789Sahrens 	lwb->lwb_state = SEQ_COMPLETE;
601789Sahrens 	/*
602789Sahrens 	 * We must also follow up the chain for already written buffers
603789Sahrens 	 * to see if we can set zl_ss_seq even higher.
604789Sahrens 	 */
605789Sahrens 	while (lwb = list_next(&zilog->zl_lwb_list, lwb)) {
606789Sahrens 		if (lwb->lwb_state != SEQ_INCOMPLETE)
607789Sahrens 			break;
608789Sahrens 		lwb->lwb_state = SEQ_COMPLETE;
609789Sahrens 		/* lwb_seq will be zero if we've written an empty buffer */
610789Sahrens 		if (lwb->lwb_seq) {
611789Sahrens 			ASSERT3U(max_seq, <, lwb->lwb_seq);
612789Sahrens 			max_seq = lwb->lwb_seq;
613789Sahrens 		}
614789Sahrens 	}
615789Sahrens 	zilog->zl_ss_seq = MAX(max_seq, zilog->zl_ss_seq);
616789Sahrens 	mutex_exit(&zilog->zl_lock);
617789Sahrens 	cv_broadcast(&zilog->zl_cv_seq);
618789Sahrens }
619789Sahrens 
620789Sahrens /*
621789Sahrens  * Start a log block write and advance to the next log block.
622789Sahrens  * Calls are serialized.
623789Sahrens  */
624789Sahrens static lwb_t *
625789Sahrens zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb)
626789Sahrens {
627789Sahrens 	lwb_t *nlwb;
628789Sahrens 	zil_trailer_t *ztp = (zil_trailer_t *)(lwb->lwb_buf + lwb->lwb_sz) - 1;
629*1807Sbonwick 	spa_t *spa = zilog->zl_spa;
630*1807Sbonwick 	blkptr_t *bp = &ztp->zit_next_blk;
631789Sahrens 	uint64_t txg;
632789Sahrens 	uint64_t zil_blksz;
6331544Seschrock 	zbookmark_t zb;
634789Sahrens 	int error;
635789Sahrens 
636789Sahrens 	ASSERT(lwb->lwb_nused <= ZIL_BLK_DATA_SZ(lwb));
637789Sahrens 
638789Sahrens 	/*
639789Sahrens 	 * Allocate the next block and save its address in this block
640789Sahrens 	 * before writing it in order to establish the log chain.
641789Sahrens 	 * Note that if the allocation of nlwb synced before we wrote
642789Sahrens 	 * the block that points at it (lwb), we'd leak it if we crashed.
643789Sahrens 	 * Therefore, we don't do txg_rele_to_sync() until zil_lwb_write_done().
644789Sahrens 	 */
645789Sahrens 	txg = txg_hold_open(zilog->zl_dmu_pool, &lwb->lwb_txgh);
646789Sahrens 	txg_rele_to_quiesce(&lwb->lwb_txgh);
647789Sahrens 
648789Sahrens 	/*
6491141Sperrin 	 * Pick a ZIL blocksize. We request a size that is the
6501141Sperrin 	 * maximum of the previous used size, the current used size and
6511141Sperrin 	 * the amount waiting in the queue.
652789Sahrens 	 */
6531141Sperrin 	zil_blksz = MAX(zilog->zl_cur_used, zilog->zl_prev_used);
6541141Sperrin 	zil_blksz = MAX(zil_blksz, zilog->zl_itx_list_sz + sizeof (*ztp));
6551141Sperrin 	zil_blksz = P2ROUNDUP(zil_blksz, ZIL_MIN_BLKSZ);
6561141Sperrin 	if (zil_blksz > ZIL_MAX_BLKSZ)
6571141Sperrin 		zil_blksz = ZIL_MAX_BLKSZ;
658789Sahrens 
659*1807Sbonwick 	error = zio_alloc_blk(spa, zil_blksz, bp, txg);
660789Sahrens 	if (error) {
6611544Seschrock 		/*
6621544Seschrock 		 * Reinitialise the lwb.
6631544Seschrock 		 * By returning NULL the caller will call tx_wait_synced()
6641544Seschrock 		 */
6651544Seschrock 		mutex_enter(&zilog->zl_lock);
6661544Seschrock 		ASSERT(lwb->lwb_state == UNWRITTEN);
6671544Seschrock 		lwb->lwb_nused = 0;
6681544Seschrock 		lwb->lwb_seq = 0;
6691544Seschrock 		mutex_exit(&zilog->zl_lock);
670789Sahrens 		txg_rele_to_sync(&lwb->lwb_txgh);
671789Sahrens 		return (NULL);
672789Sahrens 	}
673789Sahrens 
674*1807Sbonwick 	ASSERT3U(bp->blk_birth, ==, txg);
6751544Seschrock 	ztp->zit_pad = 0;
676789Sahrens 	ztp->zit_nused = lwb->lwb_nused;
677789Sahrens 	ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum;
678*1807Sbonwick 	bp->blk_cksum = lwb->lwb_blk.blk_cksum;
679*1807Sbonwick 	bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++;
680789Sahrens 
681789Sahrens 	/*
682789Sahrens 	 * Allocate a new log write buffer (lwb).
683789Sahrens 	 */
684789Sahrens 	nlwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP);
685789Sahrens 
686789Sahrens 	nlwb->lwb_zilog = zilog;
687*1807Sbonwick 	nlwb->lwb_blk = *bp;
688789Sahrens 	nlwb->lwb_nused = 0;
689789Sahrens 	nlwb->lwb_sz = BP_GET_LSIZE(&nlwb->lwb_blk);
690789Sahrens 	nlwb->lwb_buf = zio_buf_alloc(nlwb->lwb_sz);
691789Sahrens 	nlwb->lwb_max_txg = txg;
692789Sahrens 	nlwb->lwb_seq = 0;
693789Sahrens 	nlwb->lwb_state = UNWRITTEN;
694789Sahrens 
695789Sahrens 	/*
696789Sahrens 	 * Put new lwb at the end of the log chain,
697789Sahrens 	 * and record the vdev for later flushing
698789Sahrens 	 */
699789Sahrens 	mutex_enter(&zilog->zl_lock);
700789Sahrens 	list_insert_tail(&zilog->zl_lwb_list, nlwb);
701789Sahrens 	zil_add_vdev(zilog, DVA_GET_VDEV(BP_IDENTITY(&(lwb->lwb_blk))),
702789Sahrens 	    lwb->lwb_seq);
703789Sahrens 	mutex_exit(&zilog->zl_lock);
704789Sahrens 
705789Sahrens 	/*
706789Sahrens 	 * write the old log block
707789Sahrens 	 */
708*1807Sbonwick 	zb.zb_objset = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET];
7091544Seschrock 	zb.zb_object = 0;
7101544Seschrock 	zb.zb_level = -1;
711*1807Sbonwick 	zb.zb_blkid = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ];
7121544Seschrock 
713*1807Sbonwick 	zio_nowait(zio_rewrite(NULL, spa, ZIO_CHECKSUM_ZILOG, 0,
714789Sahrens 	    &lwb->lwb_blk, lwb->lwb_buf, lwb->lwb_sz, zil_lwb_write_done, lwb,
7151544Seschrock 	    ZIO_PRIORITY_LOG_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb));
716789Sahrens 
717789Sahrens 	return (nlwb);
718789Sahrens }
719789Sahrens 
720789Sahrens static lwb_t *
721789Sahrens zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb)
722789Sahrens {
723789Sahrens 	lr_t *lrc = &itx->itx_lr; /* common log record */
7241669Sperrin 	lr_write_t *lr;
7251669Sperrin 	char *dbuf;
726789Sahrens 	uint64_t seq = lrc->lrc_seq;
727789Sahrens 	uint64_t txg = lrc->lrc_txg;
728789Sahrens 	uint64_t reclen = lrc->lrc_reclen;
7291669Sperrin 	uint64_t dlen = 0;
730789Sahrens 	int error;
731789Sahrens 
732789Sahrens 	if (lwb == NULL)
733789Sahrens 		return (NULL);
734789Sahrens 	ASSERT(lwb->lwb_buf != NULL);
735789Sahrens 
736789Sahrens 	/*
737789Sahrens 	 * If it's a write, fetch the data or get its blkptr as appropriate.
738789Sahrens 	 */
739789Sahrens 	if (lrc->lrc_txtype == TX_WRITE) {
7401669Sperrin 		lr = (lr_write_t *)lrc;
741789Sahrens 		if (txg > spa_freeze_txg(zilog->zl_spa))
742789Sahrens 			txg_wait_synced(zilog->zl_dmu_pool, txg);
7431669Sperrin 		if (itx->itx_wr_state != WR_COPIED) {
7441669Sperrin 			if (itx->itx_wr_state == WR_NEED_COPY) {
7451669Sperrin 				dlen = P2ROUNDUP(lr->lr_length,
7461669Sperrin 				    sizeof (uint64_t));
7471669Sperrin 				ASSERT(dlen);
7481669Sperrin 				dbuf = kmem_alloc(dlen, KM_NOSLEEP);
7491669Sperrin 				/* on memory shortage use dmu_sync */
7501669Sperrin 				if (dbuf == NULL) {
7511669Sperrin 					itx->itx_wr_state = WR_INDIRECT;
7521669Sperrin 					dlen = 0;
7531669Sperrin 				}
7541669Sperrin 			} else {
7551669Sperrin 				ASSERT(itx->itx_wr_state == WR_INDIRECT);
7561669Sperrin 				dbuf = NULL;
7571669Sperrin 			}
7581669Sperrin 			error = zilog->zl_get_data(itx->itx_private, lr, dbuf);
7591669Sperrin 			if (error) {
7601669Sperrin 				if (dlen)
7611669Sperrin 					kmem_free(dbuf, dlen);
7621669Sperrin 				if (error != ENOENT && error != EALREADY) {
7631669Sperrin 					txg_wait_synced(zilog->zl_dmu_pool,
7641669Sperrin 					    txg);
7651669Sperrin 					mutex_enter(&zilog->zl_lock);
7661669Sperrin 					zilog->zl_ss_seq =
7671669Sperrin 					    MAX(seq, zilog->zl_ss_seq);
7681669Sperrin 					mutex_exit(&zilog->zl_lock);
7691669Sperrin 					return (lwb);
7701669Sperrin 				}
771789Sahrens 				mutex_enter(&zilog->zl_lock);
7721669Sperrin 				zil_add_vdev(zilog, DVA_GET_VDEV(BP_IDENTITY(
7731669Sperrin 				    &(lr->lr_blkptr))), seq);
774789Sahrens 				mutex_exit(&zilog->zl_lock);
775789Sahrens 				return (lwb);
776789Sahrens 			}
7771669Sperrin 		}
7781669Sperrin 	}
7791669Sperrin 
7801669Sperrin 	zilog->zl_cur_used += (reclen + dlen);
7811669Sperrin 
7821669Sperrin 	/*
7831669Sperrin 	 * If this record won't fit in the current log block, start a new one.
7841669Sperrin 	 */
7851669Sperrin 	if (lwb->lwb_nused + reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) {
7861669Sperrin 		lwb = zil_lwb_write_start(zilog, lwb);
7871669Sperrin 		if (lwb == NULL) {
7881669Sperrin 			if (dlen)
7891669Sperrin 				kmem_free(dbuf, dlen);
7901669Sperrin 			return (NULL);
7911669Sperrin 		}
7921669Sperrin 		ASSERT(lwb->lwb_nused == 0);
7931669Sperrin 		if (reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) {
7941669Sperrin 			txg_wait_synced(zilog->zl_dmu_pool, txg);
795789Sahrens 			mutex_enter(&zilog->zl_lock);
7961669Sperrin 			zilog->zl_ss_seq = MAX(seq, zilog->zl_ss_seq);
797789Sahrens 			mutex_exit(&zilog->zl_lock);
7981669Sperrin 			if (dlen)
7991669Sperrin 				kmem_free(dbuf, dlen);
800789Sahrens 			return (lwb);
801789Sahrens 		}
802789Sahrens 	}
803789Sahrens 
8041669Sperrin 	lrc->lrc_reclen += dlen;
805789Sahrens 	bcopy(lrc, lwb->lwb_buf + lwb->lwb_nused, reclen);
806789Sahrens 	lwb->lwb_nused += reclen;
8071669Sperrin 	if (dlen) {
8081669Sperrin 		bcopy(dbuf, lwb->lwb_buf + lwb->lwb_nused, dlen);
8091669Sperrin 		lwb->lwb_nused += dlen;
8101669Sperrin 		kmem_free(dbuf, dlen);
8111669Sperrin 		lrc->lrc_reclen -= dlen; /* for kmem_free of itx */
8121669Sperrin 	}
813789Sahrens 	lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg);
814789Sahrens 	ASSERT3U(lwb->lwb_seq, <, seq);
815789Sahrens 	lwb->lwb_seq = seq;
816789Sahrens 	ASSERT3U(lwb->lwb_nused, <=, ZIL_BLK_DATA_SZ(lwb));
817789Sahrens 	ASSERT3U(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)), ==, 0);
818789Sahrens 
819789Sahrens 	return (lwb);
820789Sahrens }
821789Sahrens 
822789Sahrens itx_t *
823789Sahrens zil_itx_create(int txtype, size_t lrsize)
824789Sahrens {
825789Sahrens 	itx_t *itx;
826789Sahrens 
827789Sahrens 	lrsize = P2ROUNDUP(lrsize, sizeof (uint64_t));
828789Sahrens 
829789Sahrens 	itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP);
830789Sahrens 	itx->itx_lr.lrc_txtype = txtype;
831789Sahrens 	itx->itx_lr.lrc_reclen = lrsize;
832789Sahrens 	itx->itx_lr.lrc_seq = 0;	/* defensive */
833789Sahrens 
834789Sahrens 	return (itx);
835789Sahrens }
836789Sahrens 
837789Sahrens uint64_t
838789Sahrens zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx)
839789Sahrens {
840789Sahrens 	uint64_t seq;
841789Sahrens 
842789Sahrens 	ASSERT(itx->itx_lr.lrc_seq == 0);
843789Sahrens 
844789Sahrens 	mutex_enter(&zilog->zl_lock);
845789Sahrens 	list_insert_tail(&zilog->zl_itx_list, itx);
846789Sahrens 	zilog->zl_itx_list_sz += itx->itx_lr.lrc_reclen;
847789Sahrens 	itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx);
848789Sahrens 	itx->itx_lr.lrc_seq = seq = ++zilog->zl_itx_seq;
849789Sahrens 	mutex_exit(&zilog->zl_lock);
850789Sahrens 
851789Sahrens 	return (seq);
852789Sahrens }
853789Sahrens 
854789Sahrens /*
855789Sahrens  * Free up all in-memory intent log transactions that have now been synced.
856789Sahrens  */
857789Sahrens static void
858789Sahrens zil_itx_clean(zilog_t *zilog)
859789Sahrens {
860789Sahrens 	uint64_t synced_txg = spa_last_synced_txg(zilog->zl_spa);
861789Sahrens 	uint64_t freeze_txg = spa_freeze_txg(zilog->zl_spa);
862789Sahrens 	uint64_t max_seq = 0;
863789Sahrens 	itx_t *itx;
864789Sahrens 
865789Sahrens 	mutex_enter(&zilog->zl_lock);
866789Sahrens 	while ((itx = list_head(&zilog->zl_itx_list)) != NULL &&
867789Sahrens 	    itx->itx_lr.lrc_txg <= MIN(synced_txg, freeze_txg)) {
868789Sahrens 		list_remove(&zilog->zl_itx_list, itx);
869789Sahrens 		zilog->zl_itx_list_sz -= itx->itx_lr.lrc_reclen;
870789Sahrens 		ASSERT3U(max_seq, <, itx->itx_lr.lrc_seq);
871789Sahrens 		max_seq = itx->itx_lr.lrc_seq;
872789Sahrens 		kmem_free(itx, offsetof(itx_t, itx_lr)
873789Sahrens 		    + itx->itx_lr.lrc_reclen);
874789Sahrens 	}
875789Sahrens 	if (max_seq > zilog->zl_ss_seq) {
876789Sahrens 		zilog->zl_ss_seq = max_seq;
877789Sahrens 		cv_broadcast(&zilog->zl_cv_seq);
878789Sahrens 	}
879789Sahrens 	mutex_exit(&zilog->zl_lock);
880789Sahrens }
881789Sahrens 
882789Sahrens void
883789Sahrens zil_clean(zilog_t *zilog)
884789Sahrens {
885789Sahrens 	/*
886789Sahrens 	 * Check for any log blocks that can be freed.
887789Sahrens 	 * Log blocks are only freed when the log block allocation and
888789Sahrens 	 * log records contained within are both known to be committed.
889789Sahrens 	 */
890789Sahrens 	mutex_enter(&zilog->zl_lock);
891789Sahrens 	if (list_head(&zilog->zl_itx_list) != NULL)
892789Sahrens 		(void) taskq_dispatch(zilog->zl_clean_taskq,
893789Sahrens 		    (void (*)(void *))zil_itx_clean, zilog, TQ_NOSLEEP);
894789Sahrens 	mutex_exit(&zilog->zl_lock);
895789Sahrens }
896789Sahrens 
897789Sahrens /*
898789Sahrens  * Push zfs transactions to stable storage up to the supplied sequence number.
899789Sahrens  */
900789Sahrens void
901789Sahrens zil_commit(zilog_t *zilog, uint64_t seq, int ioflag)
902789Sahrens {
903789Sahrens 	uint64_t txg;
904789Sahrens 	uint64_t max_seq;
905789Sahrens 	uint64_t reclen;
906789Sahrens 	itx_t *itx;
907789Sahrens 	lwb_t *lwb;
908789Sahrens 	spa_t *spa;
909789Sahrens 
910789Sahrens 	if (zilog == NULL || seq == 0 ||
911789Sahrens 	    ((ioflag & (FSYNC | FDSYNC | FRSYNC)) == 0 && !zil_always))
912789Sahrens 		return;
913789Sahrens 
914789Sahrens 	spa = zilog->zl_spa;
915789Sahrens 	mutex_enter(&zilog->zl_lock);
916789Sahrens 
917789Sahrens 	seq = MIN(seq, zilog->zl_itx_seq);	/* cap seq at largest itx seq */
918789Sahrens 
919789Sahrens 	for (;;) {
920789Sahrens 		if (zilog->zl_ss_seq >= seq) {	/* already on stable storage */
921789Sahrens 			mutex_exit(&zilog->zl_lock);
922789Sahrens 			return;
923789Sahrens 		}
924789Sahrens 
925789Sahrens 		if (zilog->zl_writer == B_FALSE) /* no one writing, do it */
926789Sahrens 			break;
927789Sahrens 
928789Sahrens 		cv_wait(&zilog->zl_cv_write, &zilog->zl_lock);
929789Sahrens 	}
930789Sahrens 
931789Sahrens 	zilog->zl_writer = B_TRUE;
932789Sahrens 	max_seq = 0;
933789Sahrens 
934789Sahrens 	if (zilog->zl_suspend) {
935789Sahrens 		lwb = NULL;
936789Sahrens 	} else {
937789Sahrens 		lwb = list_tail(&zilog->zl_lwb_list);
938789Sahrens 		if (lwb == NULL) {
939789Sahrens 			mutex_exit(&zilog->zl_lock);
940789Sahrens 			zil_create(zilog);
941789Sahrens 			mutex_enter(&zilog->zl_lock);
942789Sahrens 			lwb = list_tail(&zilog->zl_lwb_list);
943789Sahrens 		}
944789Sahrens 	}
945789Sahrens 
946789Sahrens 	/*
947789Sahrens 	 * Loop through in-memory log transactions filling log blocks,
948789Sahrens 	 * until we reach the given sequence number and there's no more
949789Sahrens 	 * room in the write buffer.
950789Sahrens 	 */
951789Sahrens 	for (;;) {
952789Sahrens 		itx = list_head(&zilog->zl_itx_list);
953789Sahrens 		if (itx == NULL)
954789Sahrens 			break;
955789Sahrens 
956789Sahrens 		reclen = itx->itx_lr.lrc_reclen;
957789Sahrens 		if ((itx->itx_lr.lrc_seq > seq) &&
958789Sahrens 		    ((lwb == NULL) || (lwb->lwb_nused + reclen >
959789Sahrens 		    ZIL_BLK_DATA_SZ(lwb))))
960789Sahrens 			break;
961789Sahrens 
962789Sahrens 		list_remove(&zilog->zl_itx_list, itx);
963789Sahrens 		txg = itx->itx_lr.lrc_txg;
964789Sahrens 		ASSERT(txg);
965789Sahrens 
966789Sahrens 		mutex_exit(&zilog->zl_lock);
967789Sahrens 		if (txg > spa_last_synced_txg(spa) ||
968789Sahrens 		    txg > spa_freeze_txg(spa))
969789Sahrens 			lwb = zil_lwb_commit(zilog, itx, lwb);
970789Sahrens 		else
971789Sahrens 			max_seq = itx->itx_lr.lrc_seq;
972789Sahrens 		kmem_free(itx, offsetof(itx_t, itx_lr)
973789Sahrens 		    + itx->itx_lr.lrc_reclen);
974789Sahrens 		mutex_enter(&zilog->zl_lock);
975789Sahrens 		zilog->zl_itx_list_sz -= reclen;
976789Sahrens 	}
977789Sahrens 
978789Sahrens 	mutex_exit(&zilog->zl_lock);
979789Sahrens 
980789Sahrens 	/* write the last block out */
981789Sahrens 	if (lwb != NULL && lwb->lwb_nused != 0)
982789Sahrens 		lwb = zil_lwb_write_start(zilog, lwb);
983789Sahrens 
9841141Sperrin 	zilog->zl_prev_used = zilog->zl_cur_used;
9851141Sperrin 	zilog->zl_cur_used = 0;
9861141Sperrin 
987789Sahrens 	mutex_enter(&zilog->zl_lock);
988789Sahrens 	if (max_seq > zilog->zl_ss_seq) {
989789Sahrens 		zilog->zl_ss_seq = max_seq;
990789Sahrens 		cv_broadcast(&zilog->zl_cv_seq);
991789Sahrens 	}
992789Sahrens 	/*
993789Sahrens 	 * Wait if necessary for our seq to be committed.
994789Sahrens 	 */
995789Sahrens 	if (lwb) {
996789Sahrens 		while (zilog->zl_ss_seq < seq && zilog->zl_log_error == 0)
997789Sahrens 			cv_wait(&zilog->zl_cv_seq, &zilog->zl_lock);
998789Sahrens 		zil_flush_vdevs(zilog, seq);
999789Sahrens 	}
10001141Sperrin 
1001789Sahrens 	if (zilog->zl_log_error || lwb == NULL) {
1002789Sahrens 		zilog->zl_log_error = 0;
1003789Sahrens 		max_seq = zilog->zl_itx_seq;
1004789Sahrens 		mutex_exit(&zilog->zl_lock);
1005789Sahrens 		txg_wait_synced(zilog->zl_dmu_pool, 0);
1006789Sahrens 		mutex_enter(&zilog->zl_lock);
1007789Sahrens 		zilog->zl_ss_seq = MAX(max_seq, zilog->zl_ss_seq);
1008789Sahrens 		cv_broadcast(&zilog->zl_cv_seq);
1009789Sahrens 	}
10101141Sperrin 	/* wake up others waiting to start a write */
10111141Sperrin 	zilog->zl_writer = B_FALSE;
1012789Sahrens 	mutex_exit(&zilog->zl_lock);
10131472Sperrin 	cv_broadcast(&zilog->zl_cv_write);
1014789Sahrens }
1015789Sahrens 
1016789Sahrens /*
1017789Sahrens  * Called in syncing context to free committed log blocks and update log header.
1018789Sahrens  */
1019789Sahrens void
1020789Sahrens zil_sync(zilog_t *zilog, dmu_tx_t *tx)
1021789Sahrens {
1022*1807Sbonwick 	zil_header_t *zh = zil_header_in_syncing_context(zilog);
1023789Sahrens 	uint64_t txg = dmu_tx_get_txg(tx);
1024789Sahrens 	spa_t *spa = zilog->zl_spa;
1025789Sahrens 	lwb_t *lwb;
1026789Sahrens 
1027*1807Sbonwick 	mutex_enter(&zilog->zl_lock);
1028*1807Sbonwick 
1029789Sahrens 	ASSERT(zilog->zl_stop_sync == 0);
1030789Sahrens 
1031*1807Sbonwick 	zh->zh_replay_seq = zilog->zl_replay_seq[txg & TXG_MASK];
1032789Sahrens 
1033789Sahrens 	if (zilog->zl_destroy_txg == txg) {
1034*1807Sbonwick 		blkptr_t blk = zh->zh_log;
1035*1807Sbonwick 
1036*1807Sbonwick 		ASSERT(list_head(&zilog->zl_lwb_list) == NULL);
1037*1807Sbonwick 		ASSERT(spa_sync_pass(spa) == 1);
1038*1807Sbonwick 
1039*1807Sbonwick 		bzero(zh, sizeof (zil_header_t));
1040789Sahrens 		bzero(zilog->zl_replay_seq, sizeof (zilog->zl_replay_seq));
1041*1807Sbonwick 
1042*1807Sbonwick 		if (zilog->zl_keep_first) {
1043*1807Sbonwick 			/*
1044*1807Sbonwick 			 * If this block was part of log chain that couldn't
1045*1807Sbonwick 			 * be claimed because a device was missing during
1046*1807Sbonwick 			 * zil_claim(), but that device later returns,
1047*1807Sbonwick 			 * then this block could erroneously appear valid.
1048*1807Sbonwick 			 * To guard against this, assign a new GUID to the new
1049*1807Sbonwick 			 * log chain so it doesn't matter what blk points to.
1050*1807Sbonwick 			 */
1051*1807Sbonwick 			zil_init_log_chain(zilog, &blk);
1052*1807Sbonwick 			zh->zh_log = blk;
1053*1807Sbonwick 		}
1054789Sahrens 	}
1055789Sahrens 
1056789Sahrens 	for (;;) {
1057789Sahrens 		lwb = list_head(&zilog->zl_lwb_list);
1058789Sahrens 		if (lwb == NULL) {
1059789Sahrens 			mutex_exit(&zilog->zl_lock);
1060789Sahrens 			return;
1061789Sahrens 		}
1062789Sahrens 		if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg)
1063789Sahrens 			break;
1064789Sahrens 		list_remove(&zilog->zl_lwb_list, lwb);
1065789Sahrens 		zio_free_blk(spa, &lwb->lwb_blk, txg);
1066789Sahrens 		kmem_cache_free(zil_lwb_cache, lwb);
1067789Sahrens 	}
1068*1807Sbonwick 	zh->zh_log = lwb->lwb_blk;
1069789Sahrens 	mutex_exit(&zilog->zl_lock);
1070789Sahrens }
1071789Sahrens 
1072789Sahrens void
1073789Sahrens zil_init(void)
1074789Sahrens {
1075789Sahrens 	zil_lwb_cache = kmem_cache_create("zil_lwb_cache",
1076789Sahrens 	    sizeof (struct lwb), NULL, NULL, NULL, NULL, NULL, NULL, 0);
1077789Sahrens }
1078789Sahrens 
1079789Sahrens void
1080789Sahrens zil_fini(void)
1081789Sahrens {
1082789Sahrens 	kmem_cache_destroy(zil_lwb_cache);
1083789Sahrens }
1084789Sahrens 
1085789Sahrens zilog_t *
1086789Sahrens zil_alloc(objset_t *os, zil_header_t *zh_phys)
1087789Sahrens {
1088789Sahrens 	zilog_t *zilog;
1089789Sahrens 
1090789Sahrens 	zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP);
1091789Sahrens 
1092789Sahrens 	zilog->zl_header = zh_phys;
1093789Sahrens 	zilog->zl_os = os;
1094789Sahrens 	zilog->zl_spa = dmu_objset_spa(os);
1095789Sahrens 	zilog->zl_dmu_pool = dmu_objset_pool(os);
1096*1807Sbonwick 	zilog->zl_destroy_txg = TXG_INITIAL - 1;
1097789Sahrens 
1098789Sahrens 	list_create(&zilog->zl_itx_list, sizeof (itx_t),
1099789Sahrens 	    offsetof(itx_t, itx_node));
1100789Sahrens 
1101789Sahrens 	list_create(&zilog->zl_lwb_list, sizeof (lwb_t),
1102789Sahrens 	    offsetof(lwb_t, lwb_node));
1103789Sahrens 
1104789Sahrens 	list_create(&zilog->zl_vdev_list, sizeof (zil_vdev_t),
1105789Sahrens 	    offsetof(zil_vdev_t, vdev_seq_node));
1106789Sahrens 
1107789Sahrens 	return (zilog);
1108789Sahrens }
1109789Sahrens 
1110789Sahrens void
1111789Sahrens zil_free(zilog_t *zilog)
1112789Sahrens {
1113789Sahrens 	lwb_t *lwb;
1114789Sahrens 	zil_vdev_t *zv;
1115789Sahrens 
1116789Sahrens 	zilog->zl_stop_sync = 1;
1117789Sahrens 
1118789Sahrens 	while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
1119789Sahrens 		list_remove(&zilog->zl_lwb_list, lwb);
1120789Sahrens 		if (lwb->lwb_buf != NULL)
1121789Sahrens 			zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
1122789Sahrens 		kmem_cache_free(zil_lwb_cache, lwb);
1123789Sahrens 	}
1124789Sahrens 	list_destroy(&zilog->zl_lwb_list);
1125789Sahrens 
1126789Sahrens 	while ((zv = list_head(&zilog->zl_vdev_list)) != NULL) {
1127789Sahrens 		list_remove(&zilog->zl_vdev_list, zv);
1128789Sahrens 		kmem_free(zv, sizeof (zil_vdev_t));
1129789Sahrens 	}
1130789Sahrens 	list_destroy(&zilog->zl_vdev_list);
1131789Sahrens 
1132789Sahrens 	ASSERT(list_head(&zilog->zl_itx_list) == NULL);
1133789Sahrens 	list_destroy(&zilog->zl_itx_list);
1134789Sahrens 
1135789Sahrens 	kmem_free(zilog, sizeof (zilog_t));
1136789Sahrens }
1137789Sahrens 
1138789Sahrens /*
11391646Sperrin  * return true if the initial log block is not valid
11401362Sperrin  */
11411362Sperrin static int
11421362Sperrin zil_empty(zilog_t *zilog)
11431362Sperrin {
1144*1807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
1145*1807Sbonwick 	arc_buf_t *abuf = NULL;
11461362Sperrin 
1147*1807Sbonwick 	if (BP_IS_HOLE(&zh->zh_log))
11481362Sperrin 		return (1);
11491362Sperrin 
1150*1807Sbonwick 	if (zil_read_log_block(zilog, &zh->zh_log, &abuf) != 0)
1151*1807Sbonwick 		return (1);
1152*1807Sbonwick 
1153*1807Sbonwick 	VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
1154*1807Sbonwick 	return (0);
11551362Sperrin }
11561362Sperrin 
11571362Sperrin /*
1158789Sahrens  * Open an intent log.
1159789Sahrens  */
1160789Sahrens zilog_t *
1161789Sahrens zil_open(objset_t *os, zil_get_data_t *get_data)
1162789Sahrens {
1163789Sahrens 	zilog_t *zilog = dmu_objset_zil(os);
1164789Sahrens 
1165789Sahrens 	zilog->zl_get_data = get_data;
1166789Sahrens 	zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri,
1167789Sahrens 	    2, 2, TASKQ_PREPOPULATE);
1168789Sahrens 
1169789Sahrens 	return (zilog);
1170789Sahrens }
1171789Sahrens 
1172789Sahrens /*
1173789Sahrens  * Close an intent log.
1174789Sahrens  */
1175789Sahrens void
1176789Sahrens zil_close(zilog_t *zilog)
1177789Sahrens {
1178*1807Sbonwick 	/*
1179*1807Sbonwick 	 * If the log isn't already committed, mark the objset dirty
1180*1807Sbonwick 	 * (so zil_sync() will be called) and wait for that txg to sync.
1181*1807Sbonwick 	 */
1182*1807Sbonwick 	if (!zil_is_committed(zilog)) {
1183*1807Sbonwick 		uint64_t txg;
1184*1807Sbonwick 		dmu_tx_t *tx = dmu_tx_create(zilog->zl_os);
1185*1807Sbonwick 		(void) dmu_tx_assign(tx, TXG_WAIT);
1186*1807Sbonwick 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
1187*1807Sbonwick 		txg = dmu_tx_get_txg(tx);
1188*1807Sbonwick 		dmu_tx_commit(tx);
1189*1807Sbonwick 		txg_wait_synced(zilog->zl_dmu_pool, txg);
1190*1807Sbonwick 	}
1191*1807Sbonwick 
1192789Sahrens 	taskq_destroy(zilog->zl_clean_taskq);
1193789Sahrens 	zilog->zl_clean_taskq = NULL;
1194789Sahrens 	zilog->zl_get_data = NULL;
1195789Sahrens 
1196789Sahrens 	zil_itx_clean(zilog);
1197789Sahrens 	ASSERT(list_head(&zilog->zl_itx_list) == NULL);
1198789Sahrens }
1199789Sahrens 
1200789Sahrens /*
1201789Sahrens  * Suspend an intent log.  While in suspended mode, we still honor
1202789Sahrens  * synchronous semantics, but we rely on txg_wait_synced() to do it.
1203789Sahrens  * We suspend the log briefly when taking a snapshot so that the snapshot
1204789Sahrens  * contains all the data it's supposed to, and has an empty intent log.
1205789Sahrens  */
1206789Sahrens int
1207789Sahrens zil_suspend(zilog_t *zilog)
1208789Sahrens {
1209*1807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
1210789Sahrens 	lwb_t *lwb;
1211789Sahrens 
1212789Sahrens 	mutex_enter(&zilog->zl_lock);
1213*1807Sbonwick 	if (zh->zh_claim_txg != 0) {		/* unplayed log */
1214789Sahrens 		mutex_exit(&zilog->zl_lock);
1215789Sahrens 		return (EBUSY);
1216789Sahrens 	}
1217*1807Sbonwick 	if (zilog->zl_suspend++ != 0) {
1218*1807Sbonwick 		/*
1219*1807Sbonwick 		 * Someone else already began a suspend.
1220*1807Sbonwick 		 * Just wait for them to finish.
1221*1807Sbonwick 		 */
1222*1807Sbonwick 		while (zilog->zl_suspending)
1223*1807Sbonwick 			cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock);
1224*1807Sbonwick 		ASSERT(BP_IS_HOLE(&zh->zh_log));
1225*1807Sbonwick 		mutex_exit(&zilog->zl_lock);
1226*1807Sbonwick 		return (0);
1227*1807Sbonwick 	}
1228*1807Sbonwick 	zilog->zl_suspending = B_TRUE;
1229789Sahrens 	mutex_exit(&zilog->zl_lock);
1230789Sahrens 
1231789Sahrens 	zil_commit(zilog, UINT64_MAX, FSYNC);
1232789Sahrens 
1233789Sahrens 	mutex_enter(&zilog->zl_lock);
1234*1807Sbonwick 	for (;;) {
1235*1807Sbonwick 		/*
1236*1807Sbonwick 		 * Wait for any in-flight log writes to complete.
1237*1807Sbonwick 		 */
1238*1807Sbonwick 		for (lwb = list_head(&zilog->zl_lwb_list); lwb != NULL;
1239*1807Sbonwick 		    lwb = list_next(&zilog->zl_lwb_list, lwb))
1240*1807Sbonwick 			if (lwb->lwb_seq != 0 && lwb->lwb_state != SEQ_COMPLETE)
1241*1807Sbonwick 				break;
1242*1807Sbonwick 
1243*1807Sbonwick 		if (lwb == NULL)
1244*1807Sbonwick 			break;
1245*1807Sbonwick 
1246*1807Sbonwick 		cv_wait(&zilog->zl_cv_seq, &zilog->zl_lock);
1247789Sahrens 	}
1248*1807Sbonwick 
1249789Sahrens 	mutex_exit(&zilog->zl_lock);
1250789Sahrens 
1251*1807Sbonwick 	zil_destroy(zilog, B_FALSE);
1252*1807Sbonwick 
1253*1807Sbonwick 	mutex_enter(&zilog->zl_lock);
1254*1807Sbonwick 	ASSERT(BP_IS_HOLE(&zh->zh_log));
1255*1807Sbonwick 	zilog->zl_suspending = B_FALSE;
1256*1807Sbonwick 	cv_broadcast(&zilog->zl_cv_suspend);
1257*1807Sbonwick 	mutex_exit(&zilog->zl_lock);
1258789Sahrens 
1259789Sahrens 	return (0);
1260789Sahrens }
1261789Sahrens 
1262789Sahrens void
1263789Sahrens zil_resume(zilog_t *zilog)
1264789Sahrens {
1265789Sahrens 	mutex_enter(&zilog->zl_lock);
1266789Sahrens 	ASSERT(zilog->zl_suspend != 0);
1267789Sahrens 	zilog->zl_suspend--;
1268789Sahrens 	mutex_exit(&zilog->zl_lock);
1269789Sahrens }
1270789Sahrens 
1271789Sahrens typedef struct zil_replay_arg {
1272789Sahrens 	objset_t	*zr_os;
1273789Sahrens 	zil_replay_func_t **zr_replay;
1274789Sahrens 	void		*zr_arg;
1275789Sahrens 	void		(*zr_rm_sync)(void *arg);
1276789Sahrens 	uint64_t	*zr_txgp;
1277789Sahrens 	boolean_t	zr_byteswap;
1278789Sahrens 	char		*zr_lrbuf;
1279789Sahrens } zil_replay_arg_t;
1280789Sahrens 
1281789Sahrens static void
1282789Sahrens zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg)
1283789Sahrens {
1284789Sahrens 	zil_replay_arg_t *zr = zra;
1285*1807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
1286789Sahrens 	uint64_t reclen = lr->lrc_reclen;
1287789Sahrens 	uint64_t txtype = lr->lrc_txtype;
1288789Sahrens 	int pass, error;
1289789Sahrens 
1290789Sahrens 	if (zilog->zl_stop_replay)
1291789Sahrens 		return;
1292789Sahrens 
1293789Sahrens 	if (lr->lrc_txg < claim_txg)		/* already committed */
1294789Sahrens 		return;
1295789Sahrens 
1296789Sahrens 	if (lr->lrc_seq <= zh->zh_replay_seq)	/* already replayed */
1297789Sahrens 		return;
1298789Sahrens 
1299789Sahrens 	/*
1300789Sahrens 	 * Make a copy of the data so we can revise and extend it.
1301789Sahrens 	 */
1302789Sahrens 	bcopy(lr, zr->zr_lrbuf, reclen);
1303789Sahrens 
1304789Sahrens 	/*
1305789Sahrens 	 * The log block containing this lr may have been byteswapped
1306789Sahrens 	 * so that we can easily examine common fields like lrc_txtype.
1307789Sahrens 	 * However, the log is a mix of different data types, and only the
1308789Sahrens 	 * replay vectors know how to byteswap their records.  Therefore, if
1309789Sahrens 	 * the lr was byteswapped, undo it before invoking the replay vector.
1310789Sahrens 	 */
1311789Sahrens 	if (zr->zr_byteswap)
1312789Sahrens 		byteswap_uint64_array(zr->zr_lrbuf, reclen);
1313789Sahrens 
1314789Sahrens 	/*
1315789Sahrens 	 * If this is a TX_WRITE with a blkptr, suck in the data.
1316789Sahrens 	 */
1317789Sahrens 	if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) {
1318789Sahrens 		lr_write_t *lrw = (lr_write_t *)lr;
1319789Sahrens 		blkptr_t *wbp = &lrw->lr_blkptr;
1320789Sahrens 		uint64_t wlen = lrw->lr_length;
1321789Sahrens 		char *wbuf = zr->zr_lrbuf + reclen;
1322789Sahrens 
1323789Sahrens 		if (BP_IS_HOLE(wbp)) {	/* compressed to a hole */
1324789Sahrens 			bzero(wbuf, wlen);
1325789Sahrens 		} else {
1326789Sahrens 			/*
1327789Sahrens 			 * A subsequent write may have overwritten this block,
1328789Sahrens 			 * in which case wbp may have been been freed and
1329789Sahrens 			 * reallocated, and our read of wbp may fail with a
1330789Sahrens 			 * checksum error.  We can safely ignore this because
1331789Sahrens 			 * the later write will provide the correct data.
1332789Sahrens 			 */
13331544Seschrock 			zbookmark_t zb;
13341544Seschrock 
13351544Seschrock 			zb.zb_objset = dmu_objset_id(zilog->zl_os);
13361544Seschrock 			zb.zb_object = lrw->lr_foid;
13371544Seschrock 			zb.zb_level = -1;
13381544Seschrock 			zb.zb_blkid = lrw->lr_offset / BP_GET_LSIZE(wbp);
13391544Seschrock 
1340789Sahrens 			(void) zio_wait(zio_read(NULL, zilog->zl_spa,
1341789Sahrens 			    wbp, wbuf, BP_GET_LSIZE(wbp), NULL, NULL,
1342789Sahrens 			    ZIO_PRIORITY_SYNC_READ,
13431544Seschrock 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &zb));
1344789Sahrens 			(void) memmove(wbuf, wbuf + lrw->lr_blkoff, wlen);
1345789Sahrens 		}
1346789Sahrens 	}
1347789Sahrens 
1348789Sahrens 	/*
1349789Sahrens 	 * We must now do two things atomically: replay this log record,
1350789Sahrens 	 * and update the log header to reflect the fact that we did so.
1351789Sahrens 	 * We use the DMU's ability to assign into a specific txg to do this.
1352789Sahrens 	 */
1353789Sahrens 	for (pass = 1; /* CONSTANTCONDITION */; pass++) {
1354789Sahrens 		uint64_t replay_txg;
1355789Sahrens 		dmu_tx_t *replay_tx;
1356789Sahrens 
1357789Sahrens 		replay_tx = dmu_tx_create(zr->zr_os);
1358789Sahrens 		error = dmu_tx_assign(replay_tx, TXG_WAIT);
1359789Sahrens 		if (error) {
1360789Sahrens 			dmu_tx_abort(replay_tx);
1361789Sahrens 			break;
1362789Sahrens 		}
1363789Sahrens 
1364789Sahrens 		replay_txg = dmu_tx_get_txg(replay_tx);
1365789Sahrens 
1366789Sahrens 		if (txtype == 0 || txtype >= TX_MAX_TYPE) {
1367789Sahrens 			error = EINVAL;
1368789Sahrens 		} else {
1369789Sahrens 			/*
1370789Sahrens 			 * On the first pass, arrange for the replay vector
1371789Sahrens 			 * to fail its dmu_tx_assign().  That's the only way
1372789Sahrens 			 * to ensure that those code paths remain well tested.
1373789Sahrens 			 */
1374789Sahrens 			*zr->zr_txgp = replay_txg - (pass == 1);
1375789Sahrens 			error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lrbuf,
1376789Sahrens 			    zr->zr_byteswap);
1377789Sahrens 			*zr->zr_txgp = TXG_NOWAIT;
1378789Sahrens 		}
1379789Sahrens 
1380789Sahrens 		if (error == 0) {
1381789Sahrens 			dsl_dataset_dirty(dmu_objset_ds(zr->zr_os), replay_tx);
1382789Sahrens 			zilog->zl_replay_seq[replay_txg & TXG_MASK] =
1383789Sahrens 			    lr->lrc_seq;
1384789Sahrens 		}
1385789Sahrens 
1386789Sahrens 		dmu_tx_commit(replay_tx);
1387789Sahrens 
1388789Sahrens 		if (error != ERESTART)
1389789Sahrens 			break;
1390789Sahrens 
1391789Sahrens 		if (pass != 1)
1392789Sahrens 			txg_wait_open(spa_get_dsl(zilog->zl_spa),
1393789Sahrens 			    replay_txg + 1);
1394789Sahrens 
1395789Sahrens 		dprintf("pass %d, retrying\n", pass);
1396789Sahrens 	}
1397789Sahrens 
1398789Sahrens 	if (error) {
1399789Sahrens 		char *name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1400789Sahrens 		dmu_objset_name(zr->zr_os, name);
1401789Sahrens 		cmn_err(CE_WARN, "ZFS replay transaction error %d, "
1402789Sahrens 		    "dataset %s, seq 0x%llx, txtype %llu\n",
1403789Sahrens 		    error, name,
1404789Sahrens 		    (u_longlong_t)lr->lrc_seq, (u_longlong_t)txtype);
1405789Sahrens 		zilog->zl_stop_replay = 1;
1406789Sahrens 		kmem_free(name, MAXNAMELEN);
1407789Sahrens 	}
1408789Sahrens 
1409789Sahrens 	/*
1410789Sahrens 	 * The DMU's dnode layer doesn't see removes until the txg commits,
1411789Sahrens 	 * so a subsequent claim can spuriously fail with EEXIST.
1412789Sahrens 	 * To prevent this, if we might have removed an object,
1413789Sahrens 	 * wait for the delete thread to delete it, and then
1414789Sahrens 	 * wait for the transaction group to sync.
1415789Sahrens 	 */
1416789Sahrens 	if (txtype == TX_REMOVE || txtype == TX_RMDIR || txtype == TX_RENAME) {
1417789Sahrens 		if (zr->zr_rm_sync != NULL)
1418789Sahrens 			zr->zr_rm_sync(zr->zr_arg);
1419789Sahrens 		txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0);
1420789Sahrens 	}
1421789Sahrens }
1422789Sahrens 
1423789Sahrens /*
14241362Sperrin  * If this dataset has a non-empty intent log, replay it and destroy it.
1425789Sahrens  */
1426789Sahrens void
1427789Sahrens zil_replay(objset_t *os, void *arg, uint64_t *txgp,
1428789Sahrens 	zil_replay_func_t *replay_func[TX_MAX_TYPE], void (*rm_sync)(void *arg))
1429789Sahrens {
1430789Sahrens 	zilog_t *zilog = dmu_objset_zil(os);
1431*1807Sbonwick 	const zil_header_t *zh = zilog->zl_header;
1432*1807Sbonwick 	zil_replay_arg_t zr;
14331362Sperrin 
14341362Sperrin 	if (zil_empty(zilog)) {
1435*1807Sbonwick 		zil_destroy(zilog, B_TRUE);
14361362Sperrin 		return;
14371362Sperrin 	}
1438789Sahrens 
1439789Sahrens 	zr.zr_os = os;
1440789Sahrens 	zr.zr_replay = replay_func;
1441789Sahrens 	zr.zr_arg = arg;
1442789Sahrens 	zr.zr_rm_sync = rm_sync;
1443789Sahrens 	zr.zr_txgp = txgp;
1444*1807Sbonwick 	zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log);
1445789Sahrens 	zr.zr_lrbuf = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP);
1446789Sahrens 
1447789Sahrens 	/*
1448789Sahrens 	 * Wait for in-progress removes to sync before starting replay.
1449789Sahrens 	 */
1450789Sahrens 	if (rm_sync != NULL)
1451789Sahrens 		rm_sync(arg);
1452789Sahrens 	txg_wait_synced(zilog->zl_dmu_pool, 0);
1453789Sahrens 
1454789Sahrens 	zilog->zl_stop_replay = 0;
1455*1807Sbonwick 	(void) zil_parse(zilog, NULL, zil_replay_log_record, &zr,
1456*1807Sbonwick 	    zh->zh_claim_txg);
1457789Sahrens 	kmem_free(zr.zr_lrbuf, 2 * SPA_MAXBLOCKSIZE);
1458789Sahrens 
1459*1807Sbonwick 	zil_destroy(zilog, B_FALSE);
1460789Sahrens }
14611646Sperrin 
14621646Sperrin /*
14631646Sperrin  * Report whether all transactions are committed
14641646Sperrin  */
14651646Sperrin int
14661646Sperrin zil_is_committed(zilog_t *zilog)
14671646Sperrin {
14681646Sperrin 	lwb_t *lwb;
14691646Sperrin 
1470*1807Sbonwick 	if (!list_is_empty(&zilog->zl_itx_list))
14711646Sperrin 		return (B_FALSE);
14721646Sperrin 
14731646Sperrin 	/*
14741646Sperrin 	 * A log write buffer at the head of the list that is not UNWRITTEN
14751646Sperrin 	 * means there's a lwb yet to be freed after a txg commit
14761646Sperrin 	 */
14771646Sperrin 	lwb = list_head(&zilog->zl_lwb_list);
14781646Sperrin 	if (lwb && lwb->lwb_state != UNWRITTEN)
14791646Sperrin 		return (B_FALSE);
14801646Sperrin 	ASSERT(zil_empty(zilog));
14811646Sperrin 	return (B_TRUE);
14821646Sperrin }
1483