xref: /illumos-gate/usr/src/uts/common/fs/zfs/zil.c (revision fd136879)
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 /*
22ab69d62fSMatthew Ahrens  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens #include <sys/zfs_context.h>
27fa9e4066Sahrens #include <sys/spa.h>
28fa9e4066Sahrens #include <sys/dmu.h>
29fa9e4066Sahrens #include <sys/zap.h>
30fa9e4066Sahrens #include <sys/arc.h>
31fa9e4066Sahrens #include <sys/stat.h>
32fa9e4066Sahrens #include <sys/resource.h>
33fa9e4066Sahrens #include <sys/zil.h>
34fa9e4066Sahrens #include <sys/zil_impl.h>
35fa9e4066Sahrens #include <sys/dsl_dataset.h>
36fa9e4066Sahrens #include <sys/vdev.h>
37d63d470bSgw #include <sys/dmu_tx.h>
38fa9e4066Sahrens 
39fa9e4066Sahrens /*
40fa9e4066Sahrens  * The zfs intent log (ZIL) saves transaction records of system calls
41fa9e4066Sahrens  * that change the file system in memory with enough information
42fa9e4066Sahrens  * to be able to replay them. These are stored in memory until
43fa9e4066Sahrens  * either the DMU transaction group (txg) commits them to the stable pool
44fa9e4066Sahrens  * and they can be discarded, or they are flushed to the stable log
45fa9e4066Sahrens  * (also in the pool) due to a fsync, O_DSYNC or other synchronous
46fa9e4066Sahrens  * requirement. In the event of a panic or power fail then those log
47fa9e4066Sahrens  * records (transactions) are replayed.
48fa9e4066Sahrens  *
49fa9e4066Sahrens  * There is one ZIL per file system. Its on-disk (pool) format consists
50fa9e4066Sahrens  * of 3 parts:
51fa9e4066Sahrens  *
52fa9e4066Sahrens  * 	- ZIL header
53fa9e4066Sahrens  * 	- ZIL blocks
54fa9e4066Sahrens  * 	- ZIL records
55fa9e4066Sahrens  *
56fa9e4066Sahrens  * A log record holds a system call transaction. Log blocks can
57fa9e4066Sahrens  * hold many log records and the blocks are chained together.
58fa9e4066Sahrens  * Each ZIL block contains a block pointer (blkptr_t) to the next
59fa9e4066Sahrens  * ZIL block in the chain. The ZIL header points to the first
60fa9e4066Sahrens  * block in the chain. Note there is not a fixed place in the pool
61fa9e4066Sahrens  * to hold blocks. They are dynamically allocated and freed as
62fa9e4066Sahrens  * needed from the blocks available. Figure X shows the ZIL structure:
63fa9e4066Sahrens  */
64fa9e4066Sahrens 
65fa9e4066Sahrens /*
66416e0cd8Sek  * This global ZIL switch affects all pools
67fa9e4066Sahrens  */
68fa9e4066Sahrens int zil_disable = 0;	/* disable intent logging */
69416e0cd8Sek 
70416e0cd8Sek /*
71416e0cd8Sek  * Tunable parameter for debugging or performance analysis.  Setting
72416e0cd8Sek  * zfs_nocacheflush will cause corruption on power loss if a volatile
73416e0cd8Sek  * out-of-order write cache is enabled.
74416e0cd8Sek  */
75416e0cd8Sek boolean_t zfs_nocacheflush = B_FALSE;
76fa9e4066Sahrens 
77fa9e4066Sahrens static kmem_cache_t *zil_lwb_cache;
78fa9e4066Sahrens 
798f18d1faSGeorge Wilson static boolean_t zil_empty(zilog_t *zilog);
808f18d1faSGeorge Wilson 
81fa9e4066Sahrens static int
82b24ab676SJeff Bonwick zil_bp_compare(const void *x1, const void *x2)
83fa9e4066Sahrens {
84b24ab676SJeff Bonwick 	const dva_t *dva1 = &((zil_bp_node_t *)x1)->zn_dva;
85b24ab676SJeff Bonwick 	const dva_t *dva2 = &((zil_bp_node_t *)x2)->zn_dva;
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
101b24ab676SJeff Bonwick zil_bp_tree_init(zilog_t *zilog)
102fa9e4066Sahrens {
103b24ab676SJeff Bonwick 	avl_create(&zilog->zl_bp_tree, zil_bp_compare,
104b24ab676SJeff Bonwick 	    sizeof (zil_bp_node_t), offsetof(zil_bp_node_t, zn_node));
105fa9e4066Sahrens }
106fa9e4066Sahrens 
107fa9e4066Sahrens static void
108b24ab676SJeff Bonwick zil_bp_tree_fini(zilog_t *zilog)
109fa9e4066Sahrens {
110b24ab676SJeff Bonwick 	avl_tree_t *t = &zilog->zl_bp_tree;
111b24ab676SJeff Bonwick 	zil_bp_node_t *zn;
112fa9e4066Sahrens 	void *cookie = NULL;
113fa9e4066Sahrens 
114fa9e4066Sahrens 	while ((zn = avl_destroy_nodes(t, &cookie)) != NULL)
115b24ab676SJeff Bonwick 		kmem_free(zn, sizeof (zil_bp_node_t));
116fa9e4066Sahrens 
117fa9e4066Sahrens 	avl_destroy(t);
118fa9e4066Sahrens }
119fa9e4066Sahrens 
120b24ab676SJeff Bonwick int
121b24ab676SJeff Bonwick zil_bp_tree_add(zilog_t *zilog, const blkptr_t *bp)
122fa9e4066Sahrens {
123b24ab676SJeff Bonwick 	avl_tree_t *t = &zilog->zl_bp_tree;
124b24ab676SJeff Bonwick 	const dva_t *dva = BP_IDENTITY(bp);
125b24ab676SJeff Bonwick 	zil_bp_node_t *zn;
126fa9e4066Sahrens 	avl_index_t where;
127fa9e4066Sahrens 
128fa9e4066Sahrens 	if (avl_find(t, dva, &where) != NULL)
129fa9e4066Sahrens 		return (EEXIST);
130fa9e4066Sahrens 
131b24ab676SJeff Bonwick 	zn = kmem_alloc(sizeof (zil_bp_node_t), KM_SLEEP);
132fa9e4066Sahrens 	zn->zn_dva = *dva;
133fa9e4066Sahrens 	avl_insert(t, zn, where);
134fa9e4066Sahrens 
135fa9e4066Sahrens 	return (0);
136fa9e4066Sahrens }
137fa9e4066Sahrens 
138d80c45e0Sbonwick static zil_header_t *
139d80c45e0Sbonwick zil_header_in_syncing_context(zilog_t *zilog)
140d80c45e0Sbonwick {
141d80c45e0Sbonwick 	return ((zil_header_t *)zilog->zl_header);
142d80c45e0Sbonwick }
143d80c45e0Sbonwick 
144d80c45e0Sbonwick static void
145d80c45e0Sbonwick zil_init_log_chain(zilog_t *zilog, blkptr_t *bp)
146d80c45e0Sbonwick {
147d80c45e0Sbonwick 	zio_cksum_t *zc = &bp->blk_cksum;
148d80c45e0Sbonwick 
149d80c45e0Sbonwick 	zc->zc_word[ZIL_ZC_GUID_0] = spa_get_random(-1ULL);
150d80c45e0Sbonwick 	zc->zc_word[ZIL_ZC_GUID_1] = spa_get_random(-1ULL);
151d80c45e0Sbonwick 	zc->zc_word[ZIL_ZC_OBJSET] = dmu_objset_id(zilog->zl_os);
152d80c45e0Sbonwick 	zc->zc_word[ZIL_ZC_SEQ] = 1ULL;
153d80c45e0Sbonwick }
154d80c45e0Sbonwick 
155fa9e4066Sahrens /*
156b24ab676SJeff Bonwick  * Read a log block and make sure it's valid.
157fa9e4066Sahrens  */
158fa9e4066Sahrens static int
159b24ab676SJeff Bonwick zil_read_log_block(zilog_t *zilog, const blkptr_t *bp, blkptr_t *nbp, void *dst)
160fa9e4066Sahrens {
161b24ab676SJeff Bonwick 	enum zio_flag zio_flags = ZIO_FLAG_CANFAIL;
16213506d1eSmaybee 	uint32_t aflags = ARC_WAIT;
163b24ab676SJeff Bonwick 	arc_buf_t *abuf = NULL;
164b24ab676SJeff Bonwick 	zbookmark_t zb;
165fa9e4066Sahrens 	int error;
166fa9e4066Sahrens 
167b24ab676SJeff Bonwick 	if (zilog->zl_header->zh_claim_txg == 0)
168b24ab676SJeff Bonwick 		zio_flags |= ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB;
169ea8dc4b6Seschrock 
170b24ab676SJeff Bonwick 	if (!(zilog->zl_header->zh_flags & ZIL_CLAIM_LR_SEQ_VALID))
171b24ab676SJeff Bonwick 		zio_flags |= ZIO_FLAG_SPECULATIVE;
172fa9e4066Sahrens 
173b24ab676SJeff Bonwick 	SET_BOOKMARK(&zb, bp->blk_cksum.zc_word[ZIL_ZC_OBJSET],
174b24ab676SJeff Bonwick 	    ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, bp->blk_cksum.zc_word[ZIL_ZC_SEQ]);
175b24ab676SJeff Bonwick 
176b24ab676SJeff Bonwick 	error = arc_read_nolock(NULL, zilog->zl_spa, bp, arc_getbuf_func, &abuf,
177b24ab676SJeff Bonwick 	    ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb);
178fa9e4066Sahrens 
179d80c45e0Sbonwick 	if (error == 0) {
180b24ab676SJeff Bonwick 		char *data = abuf->b_data;
181b24ab676SJeff Bonwick 		uint64_t size = BP_GET_LSIZE(bp);
182b24ab676SJeff Bonwick 		zil_trailer_t *ztp = (zil_trailer_t *)(data + size) - 1;
183d80c45e0Sbonwick 		zio_cksum_t cksum = bp->blk_cksum;
184fa9e4066Sahrens 
185b24ab676SJeff Bonwick 		bcopy(data, dst, size);
186b24ab676SJeff Bonwick 		*nbp = ztp->zit_next_blk;
187b24ab676SJeff Bonwick 
188d80c45e0Sbonwick 		/*
189f5e6e722SNeil Perrin 		 * Validate the checksummed log block.
190f5e6e722SNeil Perrin 		 *
191d80c45e0Sbonwick 		 * Sequence numbers should be... sequential.  The checksum
192d80c45e0Sbonwick 		 * verifier for the next block should be bp's checksum plus 1.
193f5e6e722SNeil Perrin 		 *
194f5e6e722SNeil Perrin 		 * Also check the log chain linkage and size used.
195d80c45e0Sbonwick 		 */
196d80c45e0Sbonwick 		cksum.zc_word[ZIL_ZC_SEQ]++;
197d80c45e0Sbonwick 
198f5e6e722SNeil Perrin 		if (bcmp(&cksum, &ztp->zit_next_blk.blk_cksum,
199f5e6e722SNeil Perrin 		    sizeof (cksum)) || BP_IS_HOLE(&ztp->zit_next_blk) ||
200b24ab676SJeff Bonwick 		    (ztp->zit_nused > (size - sizeof (zil_trailer_t))))
201f5e6e722SNeil Perrin 			error = ECKSUM;
202fa9e4066Sahrens 
203b24ab676SJeff Bonwick 		VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
204fa9e4066Sahrens 	}
205fa9e4066Sahrens 
206b24ab676SJeff Bonwick 	return (error);
207b24ab676SJeff Bonwick }
208b24ab676SJeff Bonwick 
209b24ab676SJeff Bonwick /*
210b24ab676SJeff Bonwick  * Read a TX_WRITE log data block.
211b24ab676SJeff Bonwick  */
212b24ab676SJeff Bonwick static int
213b24ab676SJeff Bonwick zil_read_log_data(zilog_t *zilog, const lr_write_t *lr, void *wbuf)
214b24ab676SJeff Bonwick {
215b24ab676SJeff Bonwick 	enum zio_flag zio_flags = ZIO_FLAG_CANFAIL;
216b24ab676SJeff Bonwick 	const blkptr_t *bp = &lr->lr_blkptr;
217b24ab676SJeff Bonwick 	uint32_t aflags = ARC_WAIT;
218b24ab676SJeff Bonwick 	arc_buf_t *abuf = NULL;
219b24ab676SJeff Bonwick 	zbookmark_t zb;
220b24ab676SJeff Bonwick 	int error;
221b24ab676SJeff Bonwick 
222b24ab676SJeff Bonwick 	if (BP_IS_HOLE(bp)) {
223b24ab676SJeff Bonwick 		if (wbuf != NULL)
224b24ab676SJeff Bonwick 			bzero(wbuf, MAX(BP_GET_LSIZE(bp), lr->lr_length));
225b24ab676SJeff Bonwick 		return (0);
226b24ab676SJeff Bonwick 	}
227b24ab676SJeff Bonwick 
228b24ab676SJeff Bonwick 	if (zilog->zl_header->zh_claim_txg == 0)
229b24ab676SJeff Bonwick 		zio_flags |= ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB;
230b24ab676SJeff Bonwick 
231b24ab676SJeff Bonwick 	SET_BOOKMARK(&zb, dmu_objset_id(zilog->zl_os), lr->lr_foid,
232b24ab676SJeff Bonwick 	    ZB_ZIL_LEVEL, lr->lr_offset / BP_GET_LSIZE(bp));
233b24ab676SJeff Bonwick 
234b24ab676SJeff Bonwick 	error = arc_read_nolock(NULL, zilog->zl_spa, bp, arc_getbuf_func, &abuf,
235b24ab676SJeff Bonwick 	    ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb);
236b24ab676SJeff Bonwick 
237b24ab676SJeff Bonwick 	if (error == 0) {
238b24ab676SJeff Bonwick 		if (wbuf != NULL)
239b24ab676SJeff Bonwick 			bcopy(abuf->b_data, wbuf, arc_buf_size(abuf));
240b24ab676SJeff Bonwick 		(void) arc_buf_remove_ref(abuf, &abuf);
241b24ab676SJeff Bonwick 	}
242fa9e4066Sahrens 
243d80c45e0Sbonwick 	return (error);
244fa9e4066Sahrens }
245fa9e4066Sahrens 
246fa9e4066Sahrens /*
247fa9e4066Sahrens  * Parse the intent log, and call parse_func for each valid record within.
248fa9e4066Sahrens  */
249b24ab676SJeff Bonwick int
250fa9e4066Sahrens zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func,
251fa9e4066Sahrens     zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg)
252fa9e4066Sahrens {
253d80c45e0Sbonwick 	const zil_header_t *zh = zilog->zl_header;
254b24ab676SJeff Bonwick 	boolean_t claimed = !!zh->zh_claim_txg;
255b24ab676SJeff Bonwick 	uint64_t claim_blk_seq = claimed ? zh->zh_claim_blk_seq : UINT64_MAX;
256b24ab676SJeff Bonwick 	uint64_t claim_lr_seq = claimed ? zh->zh_claim_lr_seq : UINT64_MAX;
257b24ab676SJeff Bonwick 	uint64_t max_blk_seq = 0;
258b24ab676SJeff Bonwick 	uint64_t max_lr_seq = 0;
259b24ab676SJeff Bonwick 	uint64_t blk_count = 0;
260b24ab676SJeff Bonwick 	uint64_t lr_count = 0;
261b24ab676SJeff Bonwick 	blkptr_t blk, next_blk;
262fa9e4066Sahrens 	char *lrbuf, *lrp;
263b24ab676SJeff Bonwick 	int error = 0;
264fa9e4066Sahrens 
265b24ab676SJeff Bonwick 	/*
266b24ab676SJeff Bonwick 	 * Old logs didn't record the maximum zh_claim_lr_seq.
267b24ab676SJeff Bonwick 	 */
268b24ab676SJeff Bonwick 	if (!(zh->zh_flags & ZIL_CLAIM_LR_SEQ_VALID))
269b24ab676SJeff Bonwick 		claim_lr_seq = UINT64_MAX;
270fa9e4066Sahrens 
271fa9e4066Sahrens 	/*
272fa9e4066Sahrens 	 * Starting at the block pointed to by zh_log we read the log chain.
273fa9e4066Sahrens 	 * For each block in the chain we strongly check that block to
274fa9e4066Sahrens 	 * ensure its validity.  We stop when an invalid block is found.
275fa9e4066Sahrens 	 * For each block pointer in the chain we call parse_blk_func().
276fa9e4066Sahrens 	 * For each record in each valid block we call parse_lr_func().
277d80c45e0Sbonwick 	 * If the log has been claimed, stop if we encounter a sequence
278d80c45e0Sbonwick 	 * number greater than the highest claimed sequence number.
279fa9e4066Sahrens 	 */
280b24ab676SJeff Bonwick 	lrbuf = zio_buf_alloc(SPA_MAXBLOCKSIZE);
281b24ab676SJeff Bonwick 	zil_bp_tree_init(zilog);
282d80c45e0Sbonwick 
283b24ab676SJeff Bonwick 	for (blk = zh->zh_log; !BP_IS_HOLE(&blk); blk = next_blk) {
284b24ab676SJeff Bonwick 		zil_trailer_t *ztp =
285b24ab676SJeff Bonwick 		    (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1;
286b24ab676SJeff Bonwick 		uint64_t blk_seq = blk.blk_cksum.zc_word[ZIL_ZC_SEQ];
287b24ab676SJeff Bonwick 		int reclen;
288d80c45e0Sbonwick 
289b24ab676SJeff Bonwick 		if (blk_seq > claim_blk_seq)
290b24ab676SJeff Bonwick 			break;
291b24ab676SJeff Bonwick 		if ((error = parse_blk_func(zilog, &blk, arg, txg)) != 0)
292b24ab676SJeff Bonwick 			break;
293b24ab676SJeff Bonwick 		ASSERT(max_blk_seq < blk_seq);
294b24ab676SJeff Bonwick 		max_blk_seq = blk_seq;
295b24ab676SJeff Bonwick 		blk_count++;
296fa9e4066Sahrens 
297b24ab676SJeff Bonwick 		if (max_lr_seq == claim_lr_seq && max_blk_seq == claim_blk_seq)
298b24ab676SJeff Bonwick 			break;
299fa9e4066Sahrens 
300b24ab676SJeff Bonwick 		error = zil_read_log_block(zilog, &blk, &next_blk, lrbuf);
301fa9e4066Sahrens 		if (error)
302fa9e4066Sahrens 			break;
303fa9e4066Sahrens 
304fa9e4066Sahrens 		for (lrp = lrbuf; lrp < lrbuf + ztp->zit_nused; lrp += reclen) {
305fa9e4066Sahrens 			lr_t *lr = (lr_t *)lrp;
306fa9e4066Sahrens 			reclen = lr->lrc_reclen;
307fa9e4066Sahrens 			ASSERT3U(reclen, >=, sizeof (lr_t));
308b24ab676SJeff Bonwick 			if (lr->lrc_seq > claim_lr_seq)
309b24ab676SJeff Bonwick 				goto done;
310b24ab676SJeff Bonwick 			if ((error = parse_lr_func(zilog, lr, arg, txg)) != 0)
311b24ab676SJeff Bonwick 				goto done;
312b24ab676SJeff Bonwick 			ASSERT(max_lr_seq < lr->lrc_seq);
313b24ab676SJeff Bonwick 			max_lr_seq = lr->lrc_seq;
314b24ab676SJeff Bonwick 			lr_count++;
315fa9e4066Sahrens 		}
316fa9e4066Sahrens 	}
317b24ab676SJeff Bonwick done:
318b24ab676SJeff Bonwick 	zilog->zl_parse_error = error;
319b24ab676SJeff Bonwick 	zilog->zl_parse_blk_seq = max_blk_seq;
320b24ab676SJeff Bonwick 	zilog->zl_parse_lr_seq = max_lr_seq;
321b24ab676SJeff Bonwick 	zilog->zl_parse_blk_count = blk_count;
322b24ab676SJeff Bonwick 	zilog->zl_parse_lr_count = lr_count;
323b24ab676SJeff Bonwick 
324b24ab676SJeff Bonwick 	ASSERT(!claimed || !(zh->zh_flags & ZIL_CLAIM_LR_SEQ_VALID) ||
325b24ab676SJeff Bonwick 	    (max_blk_seq == claim_blk_seq && max_lr_seq == claim_lr_seq));
326d80c45e0Sbonwick 
327b24ab676SJeff Bonwick 	zil_bp_tree_fini(zilog);
328b24ab676SJeff Bonwick 	zio_buf_free(lrbuf, SPA_MAXBLOCKSIZE);
329b24ab676SJeff Bonwick 
330b24ab676SJeff Bonwick 	return (error);
331fa9e4066Sahrens }
332fa9e4066Sahrens 
333b24ab676SJeff Bonwick static int
334fa9e4066Sahrens zil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg)
335fa9e4066Sahrens {
336fa9e4066Sahrens 	/*
337fa9e4066Sahrens 	 * Claim log block if not already committed and not already claimed.
338b24ab676SJeff Bonwick 	 * If tx == NULL, just verify that the block is claimable.
339fa9e4066Sahrens 	 */
340b24ab676SJeff Bonwick 	if (bp->blk_birth < first_txg || zil_bp_tree_add(zilog, bp) != 0)
341b24ab676SJeff Bonwick 		return (0);
342b24ab676SJeff Bonwick 
343b24ab676SJeff Bonwick 	return (zio_wait(zio_claim(NULL, zilog->zl_spa,
344b24ab676SJeff Bonwick 	    tx == NULL ? 0 : first_txg, bp, spa_claim_notify, NULL,
345b24ab676SJeff Bonwick 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB)));
346fa9e4066Sahrens }
347fa9e4066Sahrens 
348b24ab676SJeff Bonwick static int
349fa9e4066Sahrens zil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg)
350fa9e4066Sahrens {
351b24ab676SJeff Bonwick 	lr_write_t *lr = (lr_write_t *)lrc;
352b24ab676SJeff Bonwick 	int error;
353b24ab676SJeff Bonwick 
354b24ab676SJeff Bonwick 	if (lrc->lrc_txtype != TX_WRITE)
355b24ab676SJeff Bonwick 		return (0);
356b24ab676SJeff Bonwick 
357b24ab676SJeff Bonwick 	/*
358b24ab676SJeff Bonwick 	 * If the block is not readable, don't claim it.  This can happen
359b24ab676SJeff Bonwick 	 * in normal operation when a log block is written to disk before
360b24ab676SJeff Bonwick 	 * some of the dmu_sync() blocks it points to.  In this case, the
361b24ab676SJeff Bonwick 	 * transaction cannot have been committed to anyone (we would have
362b24ab676SJeff Bonwick 	 * waited for all writes to be stable first), so it is semantically
363b24ab676SJeff Bonwick 	 * correct to declare this the end of the log.
364b24ab676SJeff Bonwick 	 */
365b24ab676SJeff Bonwick 	if (lr->lr_blkptr.blk_birth >= first_txg &&
366b24ab676SJeff Bonwick 	    (error = zil_read_log_data(zilog, lr, NULL)) != 0)
367b24ab676SJeff Bonwick 		return (error);
368b24ab676SJeff Bonwick 
369b24ab676SJeff Bonwick 	return (zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg));
370fa9e4066Sahrens }
371fa9e4066Sahrens 
372fa9e4066Sahrens /* ARGSUSED */
373b24ab676SJeff Bonwick static int
374fa9e4066Sahrens zil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg)
375fa9e4066Sahrens {
376b24ab676SJeff Bonwick 	zio_free_zil(zilog->zl_spa, dmu_tx_get_txg(tx), bp);
377b24ab676SJeff Bonwick 
378b24ab676SJeff Bonwick 	return (0);
379fa9e4066Sahrens }
380fa9e4066Sahrens 
381b24ab676SJeff Bonwick static int
382fa9e4066Sahrens zil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg)
383fa9e4066Sahrens {
384b24ab676SJeff Bonwick 	lr_write_t *lr = (lr_write_t *)lrc;
385b24ab676SJeff Bonwick 	blkptr_t *bp = &lr->lr_blkptr;
386b24ab676SJeff Bonwick 
387fa9e4066Sahrens 	/*
388fa9e4066Sahrens 	 * If we previously claimed it, we need to free it.
389fa9e4066Sahrens 	 */
390b24ab676SJeff Bonwick 	if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE &&
391b24ab676SJeff Bonwick 	    bp->blk_birth >= claim_txg && zil_bp_tree_add(zilog, bp) == 0)
392b24ab676SJeff Bonwick 		zio_free(zilog->zl_spa, dmu_tx_get_txg(tx), bp);
393b24ab676SJeff Bonwick 
394b24ab676SJeff Bonwick 	return (0);
395fa9e4066Sahrens }
396fa9e4066Sahrens 
397fa9e4066Sahrens /*
398fa9e4066Sahrens  * Create an on-disk intent log.
399fa9e4066Sahrens  */
400fa9e4066Sahrens static void
401fa9e4066Sahrens zil_create(zilog_t *zilog)
402fa9e4066Sahrens {
403d80c45e0Sbonwick 	const zil_header_t *zh = zilog->zl_header;
404fa9e4066Sahrens 	lwb_t *lwb;
405d80c45e0Sbonwick 	uint64_t txg = 0;
406d80c45e0Sbonwick 	dmu_tx_t *tx = NULL;
407fa9e4066Sahrens 	blkptr_t blk;
408d80c45e0Sbonwick 	int error = 0;
409fa9e4066Sahrens 
410fa9e4066Sahrens 	/*
411d80c45e0Sbonwick 	 * Wait for any previous destroy to complete.
412fa9e4066Sahrens 	 */
413d80c45e0Sbonwick 	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
414d80c45e0Sbonwick 
415d80c45e0Sbonwick 	ASSERT(zh->zh_claim_txg == 0);
416d80c45e0Sbonwick 	ASSERT(zh->zh_replay_seq == 0);
417d80c45e0Sbonwick 
418d80c45e0Sbonwick 	blk = zh->zh_log;
419fa9e4066Sahrens 
420fa9e4066Sahrens 	/*
421899217ddSNeil Perrin 	 * If we don't already have an initial log block or we have one
422899217ddSNeil Perrin 	 * but it's the wrong endianness then allocate one.
423fa9e4066Sahrens 	 */
424899217ddSNeil Perrin 	if (BP_IS_HOLE(&blk) || BP_SHOULD_BYTESWAP(&blk)) {
425d80c45e0Sbonwick 		tx = dmu_tx_create(zilog->zl_os);
426b24ab676SJeff Bonwick 		VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0);
427d80c45e0Sbonwick 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
428d80c45e0Sbonwick 		txg = dmu_tx_get_txg(tx);
429d80c45e0Sbonwick 
430899217ddSNeil Perrin 		if (!BP_IS_HOLE(&blk)) {
431b24ab676SJeff Bonwick 			zio_free_zil(zilog->zl_spa, txg, &blk);
432899217ddSNeil Perrin 			BP_ZERO(&blk);
433899217ddSNeil Perrin 		}
434899217ddSNeil Perrin 
435b24ab676SJeff Bonwick 		error = zio_alloc_zil(zilog->zl_spa, txg, &blk, NULL,
436b24ab676SJeff Bonwick 		    ZIL_MIN_BLKSZ, zilog->zl_logbias == ZFS_LOGBIAS_LATENCY);
437d80c45e0Sbonwick 
438d80c45e0Sbonwick 		if (error == 0)
439d80c45e0Sbonwick 			zil_init_log_chain(zilog, &blk);
44013f5297eSperrin 	}
441fa9e4066Sahrens 
442d80c45e0Sbonwick 	/*
443d80c45e0Sbonwick 	 * Allocate a log write buffer (lwb) for the first log block.
444d80c45e0Sbonwick 	 */
445d80c45e0Sbonwick 	if (error == 0) {
446fa9e4066Sahrens 		lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP);
447fa9e4066Sahrens 		lwb->lwb_zilog = zilog;
448fa9e4066Sahrens 		lwb->lwb_blk = blk;
449fa9e4066Sahrens 		lwb->lwb_nused = 0;
450fa9e4066Sahrens 		lwb->lwb_sz = BP_GET_LSIZE(&lwb->lwb_blk);
451fa9e4066Sahrens 		lwb->lwb_buf = zio_buf_alloc(lwb->lwb_sz);
452fa9e4066Sahrens 		lwb->lwb_max_txg = txg;
453c5c6ffa0Smaybee 		lwb->lwb_zio = NULL;
454b24ab676SJeff Bonwick 		lwb->lwb_tx = NULL;
455c5c6ffa0Smaybee 
456fa9e4066Sahrens 		mutex_enter(&zilog->zl_lock);
457fa9e4066Sahrens 		list_insert_tail(&zilog->zl_lwb_list, lwb);
458fa9e4066Sahrens 		mutex_exit(&zilog->zl_lock);
459fa9e4066Sahrens 	}
460fa9e4066Sahrens 
461d80c45e0Sbonwick 	/*
462d80c45e0Sbonwick 	 * If we just allocated the first log block, commit our transaction
463d80c45e0Sbonwick 	 * and wait for zil_sync() to stuff the block poiner into zh_log.
464d80c45e0Sbonwick 	 * (zh is part of the MOS, so we cannot modify it in open context.)
465d80c45e0Sbonwick 	 */
466d80c45e0Sbonwick 	if (tx != NULL) {
467d80c45e0Sbonwick 		dmu_tx_commit(tx);
46813f5297eSperrin 		txg_wait_synced(zilog->zl_dmu_pool, txg);
469d80c45e0Sbonwick 	}
470d80c45e0Sbonwick 
471d80c45e0Sbonwick 	ASSERT(bcmp(&blk, &zh->zh_log, sizeof (blk)) == 0);
472fa9e4066Sahrens }
473fa9e4066Sahrens 
474fa9e4066Sahrens /*
475fa9e4066Sahrens  * In one tx, free all log blocks and clear the log header.
476d80c45e0Sbonwick  * If keep_first is set, then we're replaying a log with no content.
477d80c45e0Sbonwick  * We want to keep the first block, however, so that the first
478d80c45e0Sbonwick  * synchronous transaction doesn't require a txg_wait_synced()
479d80c45e0Sbonwick  * in zil_create().  We don't need to txg_wait_synced() here either
480d80c45e0Sbonwick  * when keep_first is set, because both zil_create() and zil_destroy()
481d80c45e0Sbonwick  * will wait for any in-progress destroys to complete.
482fa9e4066Sahrens  */
483fa9e4066Sahrens void
484d80c45e0Sbonwick zil_destroy(zilog_t *zilog, boolean_t keep_first)
485fa9e4066Sahrens {
486d80c45e0Sbonwick 	const zil_header_t *zh = zilog->zl_header;
487d80c45e0Sbonwick 	lwb_t *lwb;
488fa9e4066Sahrens 	dmu_tx_t *tx;
489fa9e4066Sahrens 	uint64_t txg;
490fa9e4066Sahrens 
491d80c45e0Sbonwick 	/*
492d80c45e0Sbonwick 	 * Wait for any previous destroy to complete.
493d80c45e0Sbonwick 	 */
494d80c45e0Sbonwick 	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
495fa9e4066Sahrens 
496b24ab676SJeff Bonwick 	zilog->zl_old_header = *zh;		/* debugging aid */
497b24ab676SJeff Bonwick 
498d80c45e0Sbonwick 	if (BP_IS_HOLE(&zh->zh_log))
499fa9e4066Sahrens 		return;
500fa9e4066Sahrens 
501fa9e4066Sahrens 	tx = dmu_tx_create(zilog->zl_os);
502b24ab676SJeff Bonwick 	VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0);
503fa9e4066Sahrens 	dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
504fa9e4066Sahrens 	txg = dmu_tx_get_txg(tx);
505fa9e4066Sahrens 
506d80c45e0Sbonwick 	mutex_enter(&zilog->zl_lock);
507d80c45e0Sbonwick 
508d80c45e0Sbonwick 	ASSERT3U(zilog->zl_destroy_txg, <, txg);
509fa9e4066Sahrens 	zilog->zl_destroy_txg = txg;
510b24ab676SJeff Bonwick 	zilog->zl_keep_first = keep_first;
511d80c45e0Sbonwick 
512d80c45e0Sbonwick 	if (!list_is_empty(&zilog->zl_lwb_list)) {
513d80c45e0Sbonwick 		ASSERT(zh->zh_claim_txg == 0);
514b24ab676SJeff Bonwick 		ASSERT(!keep_first);
515d80c45e0Sbonwick 		while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
516d80c45e0Sbonwick 			list_remove(&zilog->zl_lwb_list, lwb);
517d80c45e0Sbonwick 			if (lwb->lwb_buf != NULL)
518d80c45e0Sbonwick 				zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
519b24ab676SJeff Bonwick 			zio_free_zil(zilog->zl_spa, txg, &lwb->lwb_blk);
520d80c45e0Sbonwick 			kmem_cache_free(zil_lwb_cache, lwb);
521d80c45e0Sbonwick 		}
522b24ab676SJeff Bonwick 	} else if (!keep_first) {
523b24ab676SJeff Bonwick 		(void) zil_parse(zilog, zil_free_log_block,
524b24ab676SJeff Bonwick 		    zil_free_log_record, tx, zh->zh_claim_txg);
525d80c45e0Sbonwick 	}
526b19a79ecSperrin 	mutex_exit(&zilog->zl_lock);
527fa9e4066Sahrens 
528fa9e4066Sahrens 	dmu_tx_commit(tx);
529fa9e4066Sahrens }
530fa9e4066Sahrens 
5311d452cf5Sahrens int
532*fd136879SMatthew Ahrens zil_claim(const char *osname, void *txarg)
533fa9e4066Sahrens {
534fa9e4066Sahrens 	dmu_tx_t *tx = txarg;
535fa9e4066Sahrens 	uint64_t first_txg = dmu_tx_get_txg(tx);
536fa9e4066Sahrens 	zilog_t *zilog;
537fa9e4066Sahrens 	zil_header_t *zh;
538fa9e4066Sahrens 	objset_t *os;
539fa9e4066Sahrens 	int error;
540fa9e4066Sahrens 
541503ad85cSMatthew Ahrens 	error = dmu_objset_hold(osname, FTAG, &os);
542fa9e4066Sahrens 	if (error) {
543b87f3af3Sperrin 		cmn_err(CE_WARN, "can't open objset for %s", osname);
5441d452cf5Sahrens 		return (0);
545fa9e4066Sahrens 	}
546fa9e4066Sahrens 
547fa9e4066Sahrens 	zilog = dmu_objset_zil(os);
548d80c45e0Sbonwick 	zh = zil_header_in_syncing_context(zilog);
549fa9e4066Sahrens 
550b24ab676SJeff Bonwick 	if (spa_get_log_state(zilog->zl_spa) == SPA_LOG_CLEAR) {
551e6ca193dSGeorge Wilson 		if (!BP_IS_HOLE(&zh->zh_log))
552b24ab676SJeff Bonwick 			zio_free_zil(zilog->zl_spa, first_txg, &zh->zh_log);
553e6ca193dSGeorge Wilson 		BP_ZERO(&zh->zh_log);
554e6ca193dSGeorge Wilson 		dsl_dataset_dirty(dmu_objset_ds(os), tx);
555468c413aSTim Haley 		dmu_objset_rele(os, FTAG);
556468c413aSTim Haley 		return (0);
557e6ca193dSGeorge Wilson 	}
558e6ca193dSGeorge Wilson 
559fa9e4066Sahrens 	/*
560d80c45e0Sbonwick 	 * Claim all log blocks if we haven't already done so, and remember
561d80c45e0Sbonwick 	 * the highest claimed sequence number.  This ensures that if we can
562d80c45e0Sbonwick 	 * read only part of the log now (e.g. due to a missing device),
563d80c45e0Sbonwick 	 * but we can read the entire log later, we will not try to replay
564d80c45e0Sbonwick 	 * or destroy beyond the last block we successfully claimed.
565fa9e4066Sahrens 	 */
566fa9e4066Sahrens 	ASSERT3U(zh->zh_claim_txg, <=, first_txg);
567fa9e4066Sahrens 	if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) {
568b24ab676SJeff Bonwick 		(void) zil_parse(zilog, zil_claim_log_block,
569d80c45e0Sbonwick 		    zil_claim_log_record, tx, first_txg);
570b24ab676SJeff Bonwick 		zh->zh_claim_txg = first_txg;
571b24ab676SJeff Bonwick 		zh->zh_claim_blk_seq = zilog->zl_parse_blk_seq;
572b24ab676SJeff Bonwick 		zh->zh_claim_lr_seq = zilog->zl_parse_lr_seq;
573b24ab676SJeff Bonwick 		if (zilog->zl_parse_lr_count || zilog->zl_parse_blk_count > 1)
574b24ab676SJeff Bonwick 			zh->zh_flags |= ZIL_REPLAY_NEEDED;
575b24ab676SJeff Bonwick 		zh->zh_flags |= ZIL_CLAIM_LR_SEQ_VALID;
576fa9e4066Sahrens 		dsl_dataset_dirty(dmu_objset_ds(os), tx);
577fa9e4066Sahrens 	}
578d80c45e0Sbonwick 
579fa9e4066Sahrens 	ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1));
580503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
5811d452cf5Sahrens 	return (0);
582b87f3af3Sperrin }
583b87f3af3Sperrin 
584b87f3af3Sperrin /*
585b87f3af3Sperrin  * Check the log by walking the log chain.
586b87f3af3Sperrin  * Checksum errors are ok as they indicate the end of the chain.
587b87f3af3Sperrin  * Any other error (no device or read failure) returns an error.
588b87f3af3Sperrin  */
589b87f3af3Sperrin int
590*fd136879SMatthew Ahrens zil_check_log_chain(const char *osname, void *tx)
591b87f3af3Sperrin {
592b87f3af3Sperrin 	zilog_t *zilog;
593b87f3af3Sperrin 	objset_t *os;
594b87f3af3Sperrin 	int error;
595b87f3af3Sperrin 
596b24ab676SJeff Bonwick 	ASSERT(tx == NULL);
597b24ab676SJeff Bonwick 
598503ad85cSMatthew Ahrens 	error = dmu_objset_hold(osname, FTAG, &os);
599b87f3af3Sperrin 	if (error) {
600b87f3af3Sperrin 		cmn_err(CE_WARN, "can't open objset for %s", osname);
601b87f3af3Sperrin 		return (0);
602b87f3af3Sperrin 	}
603b87f3af3Sperrin 
604b87f3af3Sperrin 	zilog = dmu_objset_zil(os);
605b87f3af3Sperrin 
606b24ab676SJeff Bonwick 	/*
607b24ab676SJeff Bonwick 	 * Because tx == NULL, zil_claim_log_block() will not actually claim
608b24ab676SJeff Bonwick 	 * any blocks, but just determine whether it is possible to do so.
609b24ab676SJeff Bonwick 	 * In addition to checking the log chain, zil_claim_log_block()
610b24ab676SJeff Bonwick 	 * will invoke zio_claim() with a done func of spa_claim_notify(),
611b24ab676SJeff Bonwick 	 * which will update spa_max_claim_txg.  See spa_load() for details.
612b24ab676SJeff Bonwick 	 */
613b24ab676SJeff Bonwick 	error = zil_parse(zilog, zil_claim_log_block, zil_claim_log_record, tx,
614b24ab676SJeff Bonwick 	    zilog->zl_header->zh_claim_txg ? -1ULL : spa_first_txg(os->os_spa));
615b24ab676SJeff Bonwick 
616503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
617b24ab676SJeff Bonwick 
618b24ab676SJeff Bonwick 	return ((error == ECKSUM || error == ENOENT) ? 0 : error);
619b87f3af3Sperrin }
620b87f3af3Sperrin 
62117f17c2dSbonwick static int
62217f17c2dSbonwick zil_vdev_compare(const void *x1, const void *x2)
62317f17c2dSbonwick {
624c0d19b33Sperrin 	uint64_t v1 = ((zil_vdev_node_t *)x1)->zv_vdev;
625c0d19b33Sperrin 	uint64_t v2 = ((zil_vdev_node_t *)x2)->zv_vdev;
62617f17c2dSbonwick 
62717f17c2dSbonwick 	if (v1 < v2)
62817f17c2dSbonwick 		return (-1);
62917f17c2dSbonwick 	if (v1 > v2)
63017f17c2dSbonwick 		return (1);
63117f17c2dSbonwick 
63217f17c2dSbonwick 	return (0);
63317f17c2dSbonwick }
63417f17c2dSbonwick 
635fa9e4066Sahrens void
636b24ab676SJeff Bonwick zil_add_block(zilog_t *zilog, const blkptr_t *bp)
637fa9e4066Sahrens {
63817f17c2dSbonwick 	avl_tree_t *t = &zilog->zl_vdev_tree;
63917f17c2dSbonwick 	avl_index_t where;
64017f17c2dSbonwick 	zil_vdev_node_t *zv, zvsearch;
64117f17c2dSbonwick 	int ndvas = BP_GET_NDVAS(bp);
64217f17c2dSbonwick 	int i;
643fa9e4066Sahrens 
644416e0cd8Sek 	if (zfs_nocacheflush)
645fa9e4066Sahrens 		return;
646fa9e4066Sahrens 
64717f17c2dSbonwick 	ASSERT(zilog->zl_writer);
64817f17c2dSbonwick 
64917f17c2dSbonwick 	/*
65017f17c2dSbonwick 	 * Even though we're zl_writer, we still need a lock because the
65117f17c2dSbonwick 	 * zl_get_data() callbacks may have dmu_sync() done callbacks
65217f17c2dSbonwick 	 * that will run concurrently.
65317f17c2dSbonwick 	 */
65417f17c2dSbonwick 	mutex_enter(&zilog->zl_vdev_lock);
65517f17c2dSbonwick 	for (i = 0; i < ndvas; i++) {
65617f17c2dSbonwick 		zvsearch.zv_vdev = DVA_GET_VDEV(&bp->blk_dva[i]);
65717f17c2dSbonwick 		if (avl_find(t, &zvsearch, &where) == NULL) {
65817f17c2dSbonwick 			zv = kmem_alloc(sizeof (*zv), KM_SLEEP);
65917f17c2dSbonwick 			zv->zv_vdev = zvsearch.zv_vdev;
66017f17c2dSbonwick 			avl_insert(t, zv, where);
66167bd71c6Sperrin 		}
66267bd71c6Sperrin 	}
66317f17c2dSbonwick 	mutex_exit(&zilog->zl_vdev_lock);
664fa9e4066Sahrens }
665fa9e4066Sahrens 
66667bd71c6Sperrin void
66767bd71c6Sperrin zil_flush_vdevs(zilog_t *zilog)
66867bd71c6Sperrin {
66967bd71c6Sperrin 	spa_t *spa = zilog->zl_spa;
67017f17c2dSbonwick 	avl_tree_t *t = &zilog->zl_vdev_tree;
67117f17c2dSbonwick 	void *cookie = NULL;
67217f17c2dSbonwick 	zil_vdev_node_t *zv;
67317f17c2dSbonwick 	zio_t *zio;
674fa9e4066Sahrens 
67567bd71c6Sperrin 	ASSERT(zilog->zl_writer);
67667bd71c6Sperrin 
67717f17c2dSbonwick 	/*
67817f17c2dSbonwick 	 * We don't need zl_vdev_lock here because we're the zl_writer,
67917f17c2dSbonwick 	 * and all zl_get_data() callbacks are done.
68017f17c2dSbonwick 	 */
68117f17c2dSbonwick 	if (avl_numnodes(t) == 0)
68217f17c2dSbonwick 		return;
68317f17c2dSbonwick 
684e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
68517f17c2dSbonwick 
686e14bb325SJeff Bonwick 	zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
687fa9e4066Sahrens 
68817f17c2dSbonwick 	while ((zv = avl_destroy_nodes(t, &cookie)) != NULL) {
68917f17c2dSbonwick 		vdev_t *vd = vdev_lookup_top(spa, zv->zv_vdev);
69017f17c2dSbonwick 		if (vd != NULL)
69117f17c2dSbonwick 			zio_flush(zio, vd);
69217f17c2dSbonwick 		kmem_free(zv, sizeof (*zv));
69367bd71c6Sperrin 	}
69417f17c2dSbonwick 
695fa9e4066Sahrens 	/*
696fa9e4066Sahrens 	 * Wait for all the flushes to complete.  Not all devices actually
697fa9e4066Sahrens 	 * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails.
698fa9e4066Sahrens 	 */
69917f17c2dSbonwick 	(void) zio_wait(zio);
70017f17c2dSbonwick 
701e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, FTAG);
702fa9e4066Sahrens }
703fa9e4066Sahrens 
704fa9e4066Sahrens /*
705fa9e4066Sahrens  * Function called when a log block write completes
706fa9e4066Sahrens  */
707fa9e4066Sahrens static void
708fa9e4066Sahrens zil_lwb_write_done(zio_t *zio)
709fa9e4066Sahrens {
710fa9e4066Sahrens 	lwb_t *lwb = zio->io_private;
711fa9e4066Sahrens 	zilog_t *zilog = lwb->lwb_zilog;
712b24ab676SJeff Bonwick 	dmu_tx_t *tx = lwb->lwb_tx;
713fa9e4066Sahrens 
714e14bb325SJeff Bonwick 	ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF);
715e14bb325SJeff Bonwick 	ASSERT(BP_GET_CHECKSUM(zio->io_bp) == ZIO_CHECKSUM_ZILOG);
716e14bb325SJeff Bonwick 	ASSERT(BP_GET_TYPE(zio->io_bp) == DMU_OT_INTENT_LOG);
717e14bb325SJeff Bonwick 	ASSERT(BP_GET_LEVEL(zio->io_bp) == 0);
718e14bb325SJeff Bonwick 	ASSERT(BP_GET_BYTEORDER(zio->io_bp) == ZFS_HOST_BYTEORDER);
719e14bb325SJeff Bonwick 	ASSERT(!BP_IS_GANG(zio->io_bp));
720e14bb325SJeff Bonwick 	ASSERT(!BP_IS_HOLE(zio->io_bp));
721e14bb325SJeff Bonwick 	ASSERT(zio->io_bp->blk_fill == 0);
722e14bb325SJeff Bonwick 
723fa9e4066Sahrens 	/*
724ef0d8e11SNeil Perrin 	 * Ensure the lwb buffer pointer is cleared before releasing
725ef0d8e11SNeil Perrin 	 * the txg. If we have had an allocation failure and
726ef0d8e11SNeil Perrin 	 * the txg is waiting to sync then we want want zil_sync()
727ef0d8e11SNeil Perrin 	 * to remove the lwb so that it's not picked up as the next new
728ef0d8e11SNeil Perrin 	 * one in zil_commit_writer(). zil_sync() will only remove
729ef0d8e11SNeil Perrin 	 * the lwb if lwb_buf is null.
730fa9e4066Sahrens 	 */
731fa9e4066Sahrens 	zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
732fa9e4066Sahrens 	mutex_enter(&zilog->zl_lock);
733fa9e4066Sahrens 	lwb->lwb_buf = NULL;
734b24ab676SJeff Bonwick 	lwb->lwb_tx = NULL;
735b24ab676SJeff Bonwick 	mutex_exit(&zilog->zl_lock);
736ef0d8e11SNeil Perrin 
737ef0d8e11SNeil Perrin 	/*
738ef0d8e11SNeil Perrin 	 * Now that we've written this log block, we have a stable pointer
739ef0d8e11SNeil Perrin 	 * to the next block in the chain, so it's OK to let the txg in
740b24ab676SJeff Bonwick 	 * which we allocated the next block sync.
741ef0d8e11SNeil Perrin 	 */
742b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
743fa9e4066Sahrens }
744fa9e4066Sahrens 
745c5c6ffa0Smaybee /*
746c5c6ffa0Smaybee  * Initialize the io for a log block.
747c5c6ffa0Smaybee  */
748c5c6ffa0Smaybee static void
749c5c6ffa0Smaybee zil_lwb_write_init(zilog_t *zilog, lwb_t *lwb)
750c5c6ffa0Smaybee {
751c5c6ffa0Smaybee 	zbookmark_t zb;
752c5c6ffa0Smaybee 
753b24ab676SJeff Bonwick 	SET_BOOKMARK(&zb, lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET],
754b24ab676SJeff Bonwick 	    ZB_ZIL_OBJECT, ZB_ZIL_LEVEL,
755b24ab676SJeff Bonwick 	    lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ]);
756c5c6ffa0Smaybee 
757b19a79ecSperrin 	if (zilog->zl_root_zio == NULL) {
758b19a79ecSperrin 		zilog->zl_root_zio = zio_root(zilog->zl_spa, NULL, NULL,
759b19a79ecSperrin 		    ZIO_FLAG_CANFAIL);
760b19a79ecSperrin 	}
76167bd71c6Sperrin 	if (lwb->lwb_zio == NULL) {
76267bd71c6Sperrin 		lwb->lwb_zio = zio_rewrite(zilog->zl_root_zio, zilog->zl_spa,
763e6ca193dSGeorge Wilson 		    0, &lwb->lwb_blk, lwb->lwb_buf, lwb->lwb_sz,
764e6ca193dSGeorge Wilson 		    zil_lwb_write_done, lwb, ZIO_PRIORITY_LOG_WRITE,
7658f18d1faSGeorge Wilson 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE, &zb);
76667bd71c6Sperrin 	}
767c5c6ffa0Smaybee }
768c5c6ffa0Smaybee 
769d48e086fSNeil Perrin /*
770d48e086fSNeil Perrin  * Use the slog as long as the logbias is 'latency' and the current commit size
771d48e086fSNeil Perrin  * is less than the limit or the total list size is less than 2X the limit.
772d48e086fSNeil Perrin  * Limit checking is disabled by setting zil_slog_limit to UINT64_MAX.
773d48e086fSNeil Perrin  */
774d48e086fSNeil Perrin uint64_t zil_slog_limit = 1024 * 1024;
775d48e086fSNeil Perrin #define	USE_SLOG(zilog) (((zilog)->zl_logbias == ZFS_LOGBIAS_LATENCY) && \
776d48e086fSNeil Perrin 	(((zilog)->zl_cur_used < zil_slog_limit) || \
777d48e086fSNeil Perrin 	((zilog)->zl_itx_list_sz < (zil_slog_limit << 1))))
778d48e086fSNeil Perrin 
779fa9e4066Sahrens /*
780fa9e4066Sahrens  * Start a log block write and advance to the next log block.
781fa9e4066Sahrens  * Calls are serialized.
782fa9e4066Sahrens  */
783fa9e4066Sahrens static lwb_t *
784fa9e4066Sahrens zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb)
785fa9e4066Sahrens {
786fa9e4066Sahrens 	lwb_t *nlwb;
787fa9e4066Sahrens 	zil_trailer_t *ztp = (zil_trailer_t *)(lwb->lwb_buf + lwb->lwb_sz) - 1;
788d80c45e0Sbonwick 	spa_t *spa = zilog->zl_spa;
789d80c45e0Sbonwick 	blkptr_t *bp = &ztp->zit_next_blk;
790b24ab676SJeff Bonwick 	dmu_tx_t *tx;
791fa9e4066Sahrens 	uint64_t txg;
792fa9e4066Sahrens 	uint64_t zil_blksz;
793fa9e4066Sahrens 	int error;
794fa9e4066Sahrens 
795fa9e4066Sahrens 	ASSERT(lwb->lwb_nused <= ZIL_BLK_DATA_SZ(lwb));
796fa9e4066Sahrens 
797fa9e4066Sahrens 	/*
798fa9e4066Sahrens 	 * Allocate the next block and save its address in this block
799fa9e4066Sahrens 	 * before writing it in order to establish the log chain.
800fa9e4066Sahrens 	 * Note that if the allocation of nlwb synced before we wrote
801fa9e4066Sahrens 	 * the block that points at it (lwb), we'd leak it if we crashed.
802b24ab676SJeff Bonwick 	 * Therefore, we don't do dmu_tx_commit() until zil_lwb_write_done().
803b24ab676SJeff Bonwick 	 * We dirty the dataset to ensure that zil_sync() will be called
804b24ab676SJeff Bonwick 	 * to clean up in the event of allocation failure or I/O failure.
805fa9e4066Sahrens 	 */
806b24ab676SJeff Bonwick 	tx = dmu_tx_create(zilog->zl_os);
807b24ab676SJeff Bonwick 	VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0);
808b24ab676SJeff Bonwick 	dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
809b24ab676SJeff Bonwick 	txg = dmu_tx_get_txg(tx);
810b24ab676SJeff Bonwick 
811b24ab676SJeff Bonwick 	lwb->lwb_tx = tx;
812fa9e4066Sahrens 
813fa9e4066Sahrens 	/*
81422ac5be4Sperrin 	 * Pick a ZIL blocksize. We request a size that is the
81522ac5be4Sperrin 	 * maximum of the previous used size, the current used size and
81622ac5be4Sperrin 	 * the amount waiting in the queue.
817fa9e4066Sahrens 	 */
818c5c6ffa0Smaybee 	zil_blksz = MAX(zilog->zl_prev_used,
819c5c6ffa0Smaybee 	    zilog->zl_cur_used + sizeof (*ztp));
82022ac5be4Sperrin 	zil_blksz = MAX(zil_blksz, zilog->zl_itx_list_sz + sizeof (*ztp));
821b4d654b0Sperrin 	zil_blksz = P2ROUNDUP_TYPED(zil_blksz, ZIL_MIN_BLKSZ, uint64_t);
82222ac5be4Sperrin 	if (zil_blksz > ZIL_MAX_BLKSZ)
82322ac5be4Sperrin 		zil_blksz = ZIL_MAX_BLKSZ;
824fa9e4066Sahrens 
82567bd71c6Sperrin 	BP_ZERO(bp);
82667bd71c6Sperrin 	/* pass the old blkptr in order to spread log blocks across devs */
827b24ab676SJeff Bonwick 	error = zio_alloc_zil(spa, txg, bp, &lwb->lwb_blk, zil_blksz,
828d48e086fSNeil Perrin 	    USE_SLOG(zilog));
829fa9e4066Sahrens 	if (error) {
830d63d470bSgw 		/*
831b24ab676SJeff Bonwick 		 * Since we've just experienced an allocation failure,
832d63d470bSgw 		 * terminate the current lwb and send it on its way.
833d63d470bSgw 		 */
834d63d470bSgw 		ztp->zit_pad = 0;
835d63d470bSgw 		ztp->zit_nused = lwb->lwb_nused;
836d63d470bSgw 		ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum;
837d63d470bSgw 		zio_nowait(lwb->lwb_zio);
838d63d470bSgw 
839ea8dc4b6Seschrock 		/*
840ea8dc4b6Seschrock 		 * By returning NULL the caller will call tx_wait_synced()
841ea8dc4b6Seschrock 		 */
842fa9e4066Sahrens 		return (NULL);
843fa9e4066Sahrens 	}
844fa9e4066Sahrens 
845d80c45e0Sbonwick 	ASSERT3U(bp->blk_birth, ==, txg);
846ea8dc4b6Seschrock 	ztp->zit_pad = 0;
847fa9e4066Sahrens 	ztp->zit_nused = lwb->lwb_nused;
848fa9e4066Sahrens 	ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum;
849d80c45e0Sbonwick 	bp->blk_cksum = lwb->lwb_blk.blk_cksum;
850d80c45e0Sbonwick 	bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++;
851fa9e4066Sahrens 
852fa9e4066Sahrens 	/*
853fa9e4066Sahrens 	 * Allocate a new log write buffer (lwb).
854fa9e4066Sahrens 	 */
855fa9e4066Sahrens 	nlwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP);
856fa9e4066Sahrens 	nlwb->lwb_zilog = zilog;
857d80c45e0Sbonwick 	nlwb->lwb_blk = *bp;
858fa9e4066Sahrens 	nlwb->lwb_nused = 0;
859fa9e4066Sahrens 	nlwb->lwb_sz = BP_GET_LSIZE(&nlwb->lwb_blk);
860fa9e4066Sahrens 	nlwb->lwb_buf = zio_buf_alloc(nlwb->lwb_sz);
861fa9e4066Sahrens 	nlwb->lwb_max_txg = txg;
862c5c6ffa0Smaybee 	nlwb->lwb_zio = NULL;
863b24ab676SJeff Bonwick 	nlwb->lwb_tx = NULL;
864fa9e4066Sahrens 
865fa9e4066Sahrens 	/*
86667bd71c6Sperrin 	 * Put new lwb at the end of the log chain
867fa9e4066Sahrens 	 */
868fa9e4066Sahrens 	mutex_enter(&zilog->zl_lock);
869fa9e4066Sahrens 	list_insert_tail(&zilog->zl_lwb_list, nlwb);
870fa9e4066Sahrens 	mutex_exit(&zilog->zl_lock);
871fa9e4066Sahrens 
87217f17c2dSbonwick 	/* Record the block for later vdev flushing */
87317f17c2dSbonwick 	zil_add_block(zilog, &lwb->lwb_blk);
87467bd71c6Sperrin 
875fa9e4066Sahrens 	/*
876c5c6ffa0Smaybee 	 * kick off the write for the old log block
877fa9e4066Sahrens 	 */
87867bd71c6Sperrin 	ASSERT(lwb->lwb_zio);
879c5c6ffa0Smaybee 	zio_nowait(lwb->lwb_zio);
880fa9e4066Sahrens 
881fa9e4066Sahrens 	return (nlwb);
882fa9e4066Sahrens }
883fa9e4066Sahrens 
884fa9e4066Sahrens static lwb_t *
885fa9e4066Sahrens zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb)
886fa9e4066Sahrens {
887fa9e4066Sahrens 	lr_t *lrc = &itx->itx_lr; /* common log record */
888b24ab676SJeff Bonwick 	lr_write_t *lrw = (lr_write_t *)lrc;
889b24ab676SJeff Bonwick 	char *lr_buf;
890fa9e4066Sahrens 	uint64_t txg = lrc->lrc_txg;
891fa9e4066Sahrens 	uint64_t reclen = lrc->lrc_reclen;
892b24ab676SJeff Bonwick 	uint64_t dlen = 0;
893fa9e4066Sahrens 
894fa9e4066Sahrens 	if (lwb == NULL)
895fa9e4066Sahrens 		return (NULL);
896b24ab676SJeff Bonwick 
897fa9e4066Sahrens 	ASSERT(lwb->lwb_buf != NULL);
898fa9e4066Sahrens 
899c5c6ffa0Smaybee 	if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY)
900c5c6ffa0Smaybee 		dlen = P2ROUNDUP_TYPED(
901b24ab676SJeff Bonwick 		    lrw->lr_length, sizeof (uint64_t), uint64_t);
902fa9e4066Sahrens 
903104e2ed7Sperrin 	zilog->zl_cur_used += (reclen + dlen);
90422ac5be4Sperrin 
90567bd71c6Sperrin 	zil_lwb_write_init(zilog, lwb);
90667bd71c6Sperrin 
907fa9e4066Sahrens 	/*
908fa9e4066Sahrens 	 * If this record won't fit in the current log block, start a new one.
909fa9e4066Sahrens 	 */
910104e2ed7Sperrin 	if (lwb->lwb_nused + reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) {
911fa9e4066Sahrens 		lwb = zil_lwb_write_start(zilog, lwb);
912c5c6ffa0Smaybee 		if (lwb == NULL)
913fa9e4066Sahrens 			return (NULL);
91467bd71c6Sperrin 		zil_lwb_write_init(zilog, lwb);
915ea8dc4b6Seschrock 		ASSERT(lwb->lwb_nused == 0);
916104e2ed7Sperrin 		if (reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) {
917fa9e4066Sahrens 			txg_wait_synced(zilog->zl_dmu_pool, txg);
918fa9e4066Sahrens 			return (lwb);
919fa9e4066Sahrens 		}
920fa9e4066Sahrens 	}
921fa9e4066Sahrens 
922b24ab676SJeff Bonwick 	lr_buf = lwb->lwb_buf + lwb->lwb_nused;
923b24ab676SJeff Bonwick 	bcopy(lrc, lr_buf, reclen);
924b24ab676SJeff Bonwick 	lrc = (lr_t *)lr_buf;
925b24ab676SJeff Bonwick 	lrw = (lr_write_t *)lrc;
926c5c6ffa0Smaybee 
927c5c6ffa0Smaybee 	/*
928c5c6ffa0Smaybee 	 * If it's a write, fetch the data or get its blkptr as appropriate.
929c5c6ffa0Smaybee 	 */
930c5c6ffa0Smaybee 	if (lrc->lrc_txtype == TX_WRITE) {
931c5c6ffa0Smaybee 		if (txg > spa_freeze_txg(zilog->zl_spa))
932c5c6ffa0Smaybee 			txg_wait_synced(zilog->zl_dmu_pool, txg);
933c5c6ffa0Smaybee 		if (itx->itx_wr_state != WR_COPIED) {
934c5c6ffa0Smaybee 			char *dbuf;
935c5c6ffa0Smaybee 			int error;
936c5c6ffa0Smaybee 
937c5c6ffa0Smaybee 			if (dlen) {
938c5c6ffa0Smaybee 				ASSERT(itx->itx_wr_state == WR_NEED_COPY);
939b24ab676SJeff Bonwick 				dbuf = lr_buf + reclen;
940b24ab676SJeff Bonwick 				lrw->lr_common.lrc_reclen += dlen;
941c5c6ffa0Smaybee 			} else {
942c5c6ffa0Smaybee 				ASSERT(itx->itx_wr_state == WR_INDIRECT);
943c5c6ffa0Smaybee 				dbuf = NULL;
944c5c6ffa0Smaybee 			}
945c5c6ffa0Smaybee 			error = zilog->zl_get_data(
946b24ab676SJeff Bonwick 			    itx->itx_private, lrw, dbuf, lwb->lwb_zio);
947c87b8fc5SMark J Musante 			if (error == EIO) {
948c87b8fc5SMark J Musante 				txg_wait_synced(zilog->zl_dmu_pool, txg);
949c87b8fc5SMark J Musante 				return (lwb);
950c87b8fc5SMark J Musante 			}
951c5c6ffa0Smaybee 			if (error) {
952c5c6ffa0Smaybee 				ASSERT(error == ENOENT || error == EEXIST ||
953c5c6ffa0Smaybee 				    error == EALREADY);
954c5c6ffa0Smaybee 				return (lwb);
955c5c6ffa0Smaybee 			}
956c5c6ffa0Smaybee 		}
957104e2ed7Sperrin 	}
958c5c6ffa0Smaybee 
959b24ab676SJeff Bonwick 	/*
960b24ab676SJeff Bonwick 	 * We're actually making an entry, so update lrc_seq to be the
961b24ab676SJeff Bonwick 	 * log record sequence number.  Note that this is generally not
962b24ab676SJeff Bonwick 	 * equal to the itx sequence number because not all transactions
963b24ab676SJeff Bonwick 	 * are synchronous, and sometimes spa_sync() gets there first.
964b24ab676SJeff Bonwick 	 */
965b24ab676SJeff Bonwick 	lrc->lrc_seq = ++zilog->zl_lr_seq; /* we are single threaded */
966c5c6ffa0Smaybee 	lwb->lwb_nused += reclen + dlen;
967fa9e4066Sahrens 	lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg);
968fa9e4066Sahrens 	ASSERT3U(lwb->lwb_nused, <=, ZIL_BLK_DATA_SZ(lwb));
969fa9e4066Sahrens 	ASSERT3U(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)), ==, 0);
970fa9e4066Sahrens 
971fa9e4066Sahrens 	return (lwb);
972fa9e4066Sahrens }
973fa9e4066Sahrens 
974fa9e4066Sahrens itx_t *
975da6c28aaSamw zil_itx_create(uint64_t txtype, size_t lrsize)
976fa9e4066Sahrens {
977fa9e4066Sahrens 	itx_t *itx;
978fa9e4066Sahrens 
979b4d654b0Sperrin 	lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t);
980fa9e4066Sahrens 
981fa9e4066Sahrens 	itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP);
982fa9e4066Sahrens 	itx->itx_lr.lrc_txtype = txtype;
983fa9e4066Sahrens 	itx->itx_lr.lrc_reclen = lrsize;
984abf76b6eSperrin 	itx->itx_sod = lrsize; /* if write & WR_NEED_COPY will be increased */
985fa9e4066Sahrens 	itx->itx_lr.lrc_seq = 0;	/* defensive */
986fa9e4066Sahrens 
987fa9e4066Sahrens 	return (itx);
988fa9e4066Sahrens }
989fa9e4066Sahrens 
990b24ab676SJeff Bonwick void
991b24ab676SJeff Bonwick zil_itx_destroy(itx_t *itx)
992b24ab676SJeff Bonwick {
993b24ab676SJeff Bonwick 	kmem_free(itx, offsetof(itx_t, itx_lr) + itx->itx_lr.lrc_reclen);
994b24ab676SJeff Bonwick }
995b24ab676SJeff Bonwick 
996fa9e4066Sahrens uint64_t
997fa9e4066Sahrens zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx)
998fa9e4066Sahrens {
999fa9e4066Sahrens 	uint64_t seq;
1000fa9e4066Sahrens 
1001fa9e4066Sahrens 	ASSERT(itx->itx_lr.lrc_seq == 0);
1002b24ab676SJeff Bonwick 	ASSERT(!zilog->zl_replay);
1003fa9e4066Sahrens 
1004fa9e4066Sahrens 	mutex_enter(&zilog->zl_lock);
1005fa9e4066Sahrens 	list_insert_tail(&zilog->zl_itx_list, itx);
1006abf76b6eSperrin 	zilog->zl_itx_list_sz += itx->itx_sod;
1007fa9e4066Sahrens 	itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx);
1008fa9e4066Sahrens 	itx->itx_lr.lrc_seq = seq = ++zilog->zl_itx_seq;
1009fa9e4066Sahrens 	mutex_exit(&zilog->zl_lock);
1010fa9e4066Sahrens 
1011fa9e4066Sahrens 	return (seq);
1012fa9e4066Sahrens }
1013fa9e4066Sahrens 
1014fa9e4066Sahrens /*
1015fa9e4066Sahrens  * Free up all in-memory intent log transactions that have now been synced.
1016fa9e4066Sahrens  */
1017fa9e4066Sahrens static void
1018fa9e4066Sahrens zil_itx_clean(zilog_t *zilog)
1019fa9e4066Sahrens {
1020fa9e4066Sahrens 	uint64_t synced_txg = spa_last_synced_txg(zilog->zl_spa);
1021fa9e4066Sahrens 	uint64_t freeze_txg = spa_freeze_txg(zilog->zl_spa);
1022a584ef65Sjohansen 	list_t clean_list;
1023fa9e4066Sahrens 	itx_t *itx;
1024fa9e4066Sahrens 
1025a584ef65Sjohansen 	list_create(&clean_list, sizeof (itx_t), offsetof(itx_t, itx_node));
1026a584ef65Sjohansen 
1027fa9e4066Sahrens 	mutex_enter(&zilog->zl_lock);
1028b19a79ecSperrin 	/* wait for a log writer to finish walking list */
1029b19a79ecSperrin 	while (zilog->zl_writer) {
1030b19a79ecSperrin 		cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
1031b19a79ecSperrin 	}
1032a584ef65Sjohansen 
1033a584ef65Sjohansen 	/*
1034a584ef65Sjohansen 	 * Move the sync'd log transactions to a separate list so we can call
1035a584ef65Sjohansen 	 * kmem_free without holding the zl_lock.
1036a584ef65Sjohansen 	 *
1037a584ef65Sjohansen 	 * There is no need to set zl_writer as we don't drop zl_lock here
1038a584ef65Sjohansen 	 */
1039fa9e4066Sahrens 	while ((itx = list_head(&zilog->zl_itx_list)) != NULL &&
1040fa9e4066Sahrens 	    itx->itx_lr.lrc_txg <= MIN(synced_txg, freeze_txg)) {
1041fa9e4066Sahrens 		list_remove(&zilog->zl_itx_list, itx);
1042abf76b6eSperrin 		zilog->zl_itx_list_sz -= itx->itx_sod;
1043a584ef65Sjohansen 		list_insert_tail(&clean_list, itx);
1044fa9e4066Sahrens 	}
104567bd71c6Sperrin 	cv_broadcast(&zilog->zl_cv_writer);
1046fa9e4066Sahrens 	mutex_exit(&zilog->zl_lock);
1047a584ef65Sjohansen 
1048a584ef65Sjohansen 	/* destroy sync'd log transactions */
1049a584ef65Sjohansen 	while ((itx = list_head(&clean_list)) != NULL) {
1050a584ef65Sjohansen 		list_remove(&clean_list, itx);
1051b24ab676SJeff Bonwick 		zil_itx_destroy(itx);
1052a584ef65Sjohansen 	}
1053a584ef65Sjohansen 	list_destroy(&clean_list);
1054fa9e4066Sahrens }
1055fa9e4066Sahrens 
1056b19a79ecSperrin /*
105767bd71c6Sperrin  * If there are any in-memory intent log transactions which have now been
105867bd71c6Sperrin  * synced then start up a taskq to free them.
1059b19a79ecSperrin  */
1060fa9e4066Sahrens void
1061fa9e4066Sahrens zil_clean(zilog_t *zilog)
1062fa9e4066Sahrens {
106367bd71c6Sperrin 	itx_t *itx;
106467bd71c6Sperrin 
1065fa9e4066Sahrens 	mutex_enter(&zilog->zl_lock);
106667bd71c6Sperrin 	itx = list_head(&zilog->zl_itx_list);
106767bd71c6Sperrin 	if ((itx != NULL) &&
106867bd71c6Sperrin 	    (itx->itx_lr.lrc_txg <= spa_last_synced_txg(zilog->zl_spa))) {
1069fa9e4066Sahrens 		(void) taskq_dispatch(zilog->zl_clean_taskq,
1070d48e086fSNeil Perrin 		    (task_func_t *)zil_itx_clean, zilog, TQ_NOSLEEP);
107167bd71c6Sperrin 	}
1072fa9e4066Sahrens 	mutex_exit(&zilog->zl_lock);
1073fa9e4066Sahrens }
1074fa9e4066Sahrens 
1075e14bb325SJeff Bonwick static void
1076b19a79ecSperrin zil_commit_writer(zilog_t *zilog, uint64_t seq, uint64_t foid)
1077fa9e4066Sahrens {
1078fa9e4066Sahrens 	uint64_t txg;
107967bd71c6Sperrin 	uint64_t commit_seq = 0;
1080b24ab676SJeff Bonwick 	itx_t *itx, *itx_next;
1081fa9e4066Sahrens 	lwb_t *lwb;
1082fa9e4066Sahrens 	spa_t *spa;
1083b24ab676SJeff Bonwick 	int error = 0;
1084fa9e4066Sahrens 
1085fa9e4066Sahrens 	zilog->zl_writer = B_TRUE;
1086e14bb325SJeff Bonwick 	ASSERT(zilog->zl_root_zio == NULL);
1087b19a79ecSperrin 	spa = zilog->zl_spa;
1088fa9e4066Sahrens 
1089fa9e4066Sahrens 	if (zilog->zl_suspend) {
1090fa9e4066Sahrens 		lwb = NULL;
1091fa9e4066Sahrens 	} else {
1092fa9e4066Sahrens 		lwb = list_tail(&zilog->zl_lwb_list);
1093fa9e4066Sahrens 		if (lwb == NULL) {
1094b19a79ecSperrin 			/*
1095b19a79ecSperrin 			 * Return if there's nothing to flush before we
1096b19a79ecSperrin 			 * dirty the fs by calling zil_create()
1097b19a79ecSperrin 			 */
1098b19a79ecSperrin 			if (list_is_empty(&zilog->zl_itx_list)) {
1099b19a79ecSperrin 				zilog->zl_writer = B_FALSE;
1100b19a79ecSperrin 				return;
1101b19a79ecSperrin 			}
1102fa9e4066Sahrens 			mutex_exit(&zilog->zl_lock);
1103fa9e4066Sahrens 			zil_create(zilog);
1104fa9e4066Sahrens 			mutex_enter(&zilog->zl_lock);
1105fa9e4066Sahrens 			lwb = list_tail(&zilog->zl_lwb_list);
1106fa9e4066Sahrens 		}
1107fa9e4066Sahrens 	}
1108fa9e4066Sahrens 
110967bd71c6Sperrin 	/* Loop through in-memory log transactions filling log blocks. */
1110b19a79ecSperrin 	DTRACE_PROBE1(zil__cw1, zilog_t *, zilog);
1111b24ab676SJeff Bonwick 
1112b24ab676SJeff Bonwick 	for (itx = list_head(&zilog->zl_itx_list); itx; itx = itx_next) {
1113b19a79ecSperrin 		/*
1114b24ab676SJeff Bonwick 		 * Save the next pointer.  Even though we drop zl_lock below,
1115b24ab676SJeff Bonwick 		 * all threads that can remove itx list entries (other writers
1116b24ab676SJeff Bonwick 		 * and zil_itx_clean()) can't do so until they have zl_writer.
1117b19a79ecSperrin 		 */
1118b24ab676SJeff Bonwick 		itx_next = list_next(&zilog->zl_itx_list, itx);
1119b24ab676SJeff Bonwick 
1120b24ab676SJeff Bonwick 		/*
1121b24ab676SJeff Bonwick 		 * Determine whether to push this itx.
1122b24ab676SJeff Bonwick 		 * Push all transactions related to specified foid and
1123b24ab676SJeff Bonwick 		 * all other transactions except those that can be logged
1124b24ab676SJeff Bonwick 		 * out of order (TX_WRITE, TX_TRUNCATE, TX_SETATTR, TX_ACL)
1125b24ab676SJeff Bonwick 		 * for all other files.
1126b24ab676SJeff Bonwick 		 *
1127b24ab676SJeff Bonwick 		 * If foid == 0 (meaning "push all foids") or
1128b24ab676SJeff Bonwick 		 * itx->itx_sync is set (meaning O_[D]SYNC), push regardless.
1129b24ab676SJeff Bonwick 		 */
1130b24ab676SJeff Bonwick 		if (foid != 0 && !itx->itx_sync &&
1131b24ab676SJeff Bonwick 		    TX_OOO(itx->itx_lr.lrc_txtype) &&
1132b24ab676SJeff Bonwick 		    ((lr_ooo_t *)&itx->itx_lr)->lr_foid != foid)
1133b24ab676SJeff Bonwick 			continue; /* skip this record */
1134fa9e4066Sahrens 
1135fa9e4066Sahrens 		if ((itx->itx_lr.lrc_seq > seq) &&
1136b19a79ecSperrin 		    ((lwb == NULL) || (lwb->lwb_nused == 0) ||
1137b24ab676SJeff Bonwick 		    (lwb->lwb_nused + itx->itx_sod > ZIL_BLK_DATA_SZ(lwb))))
1138fa9e4066Sahrens 			break;
1139fa9e4066Sahrens 
1140fa9e4066Sahrens 		list_remove(&zilog->zl_itx_list, itx);
1141abf76b6eSperrin 		zilog->zl_itx_list_sz -= itx->itx_sod;
1142b24ab676SJeff Bonwick 
114367bd71c6Sperrin 		mutex_exit(&zilog->zl_lock);
1144b24ab676SJeff Bonwick 
1145fa9e4066Sahrens 		txg = itx->itx_lr.lrc_txg;
1146fa9e4066Sahrens 		ASSERT(txg);
1147fa9e4066Sahrens 
1148fa9e4066Sahrens 		if (txg > spa_last_synced_txg(spa) ||
1149fa9e4066Sahrens 		    txg > spa_freeze_txg(spa))
1150fa9e4066Sahrens 			lwb = zil_lwb_commit(zilog, itx, lwb);
1151b24ab676SJeff Bonwick 
1152b24ab676SJeff Bonwick 		zil_itx_destroy(itx);
1153b24ab676SJeff Bonwick 
1154fa9e4066Sahrens 		mutex_enter(&zilog->zl_lock);
1155fa9e4066Sahrens 	}
1156b19a79ecSperrin 	DTRACE_PROBE1(zil__cw2, zilog_t *, zilog);
115767bd71c6Sperrin 	/* determine commit sequence number */
115867bd71c6Sperrin 	itx = list_head(&zilog->zl_itx_list);
115967bd71c6Sperrin 	if (itx)
1160b24ab676SJeff Bonwick 		commit_seq = itx->itx_lr.lrc_seq - 1;
116167bd71c6Sperrin 	else
116267bd71c6Sperrin 		commit_seq = zilog->zl_itx_seq;
1163fa9e4066Sahrens 	mutex_exit(&zilog->zl_lock);
1164fa9e4066Sahrens 
1165fa9e4066Sahrens 	/* write the last block out */
116667bd71c6Sperrin 	if (lwb != NULL && lwb->lwb_zio != NULL)
1167fa9e4066Sahrens 		lwb = zil_lwb_write_start(zilog, lwb);
1168fa9e4066Sahrens 
116922ac5be4Sperrin 	zilog->zl_prev_used = zilog->zl_cur_used;
117022ac5be4Sperrin 	zilog->zl_cur_used = 0;
1171fa9e4066Sahrens 
1172fa9e4066Sahrens 	/*
1173b19a79ecSperrin 	 * Wait if necessary for the log blocks to be on stable storage.
1174fa9e4066Sahrens 	 */
1175b19a79ecSperrin 	if (zilog->zl_root_zio) {
1176b19a79ecSperrin 		DTRACE_PROBE1(zil__cw3, zilog_t *, zilog);
1177b24ab676SJeff Bonwick 		error = zio_wait(zilog->zl_root_zio);
1178e14bb325SJeff Bonwick 		zilog->zl_root_zio = NULL;
1179b19a79ecSperrin 		DTRACE_PROBE1(zil__cw4, zilog_t *, zilog);
118017f17c2dSbonwick 		zil_flush_vdevs(zilog);
1181fa9e4066Sahrens 	}
118222ac5be4Sperrin 
1183b24ab676SJeff Bonwick 	if (error || lwb == NULL)
1184fa9e4066Sahrens 		txg_wait_synced(zilog->zl_dmu_pool, 0);
118567bd71c6Sperrin 
118667bd71c6Sperrin 	mutex_enter(&zilog->zl_lock);
118722ac5be4Sperrin 	zilog->zl_writer = B_FALSE;
118867bd71c6Sperrin 
118967bd71c6Sperrin 	ASSERT3U(commit_seq, >=, zilog->zl_commit_seq);
119067bd71c6Sperrin 	zilog->zl_commit_seq = commit_seq;
1191b24ab676SJeff Bonwick 
1192b24ab676SJeff Bonwick 	/*
1193b24ab676SJeff Bonwick 	 * Remember the highest committed log sequence number for ztest.
1194b24ab676SJeff Bonwick 	 * We only update this value when all the log writes succeeded,
1195b24ab676SJeff Bonwick 	 * because ztest wants to ASSERT that it got the whole log chain.
1196b24ab676SJeff Bonwick 	 */
1197b24ab676SJeff Bonwick 	if (error == 0 && lwb != NULL)
1198b24ab676SJeff Bonwick 		zilog->zl_commit_lr_seq = zilog->zl_lr_seq;
1199b19a79ecSperrin }
1200b19a79ecSperrin 
1201b19a79ecSperrin /*
1202b19a79ecSperrin  * Push zfs transactions to stable storage up to the supplied sequence number.
1203b19a79ecSperrin  * If foid is 0 push out all transactions, otherwise push only those
1204b19a79ecSperrin  * for that file or might have been used to create that file.
1205b19a79ecSperrin  */
1206b19a79ecSperrin void
1207b19a79ecSperrin zil_commit(zilog_t *zilog, uint64_t seq, uint64_t foid)
1208b19a79ecSperrin {
1209b19a79ecSperrin 	if (zilog == NULL || seq == 0)
1210b19a79ecSperrin 		return;
1211b19a79ecSperrin 
1212b19a79ecSperrin 	mutex_enter(&zilog->zl_lock);
1213b19a79ecSperrin 
1214b19a79ecSperrin 	seq = MIN(seq, zilog->zl_itx_seq);	/* cap seq at largest itx seq */
1215b19a79ecSperrin 
121667bd71c6Sperrin 	while (zilog->zl_writer) {
1217b19a79ecSperrin 		cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
1218b24ab676SJeff Bonwick 		if (seq <= zilog->zl_commit_seq) {
121967bd71c6Sperrin 			mutex_exit(&zilog->zl_lock);
122067bd71c6Sperrin 			return;
122167bd71c6Sperrin 		}
122267bd71c6Sperrin 	}
1223b19a79ecSperrin 	zil_commit_writer(zilog, seq, foid); /* drops zl_lock */
122467bd71c6Sperrin 	/* wake up others waiting on the commit */
122567bd71c6Sperrin 	cv_broadcast(&zilog->zl_cv_writer);
122667bd71c6Sperrin 	mutex_exit(&zilog->zl_lock);
1227fa9e4066Sahrens }
1228fa9e4066Sahrens 
1229b24ab676SJeff Bonwick /*
1230b24ab676SJeff Bonwick  * Report whether all transactions are committed.
1231b24ab676SJeff Bonwick  */
1232b24ab676SJeff Bonwick static boolean_t
1233b24ab676SJeff Bonwick zil_is_committed(zilog_t *zilog)
1234b24ab676SJeff Bonwick {
1235b24ab676SJeff Bonwick 	lwb_t *lwb;
1236b24ab676SJeff Bonwick 	boolean_t committed;
1237b24ab676SJeff Bonwick 
1238b24ab676SJeff Bonwick 	mutex_enter(&zilog->zl_lock);
1239b24ab676SJeff Bonwick 
1240b24ab676SJeff Bonwick 	while (zilog->zl_writer)
1241b24ab676SJeff Bonwick 		cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
1242b24ab676SJeff Bonwick 
1243b24ab676SJeff Bonwick 	if (!list_is_empty(&zilog->zl_itx_list))
1244b24ab676SJeff Bonwick 		committed = B_FALSE;		/* unpushed transactions */
1245b24ab676SJeff Bonwick 	else if ((lwb = list_head(&zilog->zl_lwb_list)) == NULL)
1246b24ab676SJeff Bonwick 		committed = B_TRUE;		/* intent log never used */
1247b24ab676SJeff Bonwick 	else if (list_next(&zilog->zl_lwb_list, lwb) != NULL)
1248b24ab676SJeff Bonwick 		committed = B_FALSE;		/* zil_sync() not done yet */
1249b24ab676SJeff Bonwick 	else
1250b24ab676SJeff Bonwick 		committed = B_TRUE;		/* everything synced */
1251b24ab676SJeff Bonwick 
1252b24ab676SJeff Bonwick 	mutex_exit(&zilog->zl_lock);
1253b24ab676SJeff Bonwick 	return (committed);
1254b24ab676SJeff Bonwick }
1255b24ab676SJeff Bonwick 
1256fa9e4066Sahrens /*
1257fa9e4066Sahrens  * Called in syncing context to free committed log blocks and update log header.
1258fa9e4066Sahrens  */
1259fa9e4066Sahrens void
1260fa9e4066Sahrens zil_sync(zilog_t *zilog, dmu_tx_t *tx)
1261fa9e4066Sahrens {
1262d80c45e0Sbonwick 	zil_header_t *zh = zil_header_in_syncing_context(zilog);
1263fa9e4066Sahrens 	uint64_t txg = dmu_tx_get_txg(tx);
1264fa9e4066Sahrens 	spa_t *spa = zilog->zl_spa;
1265b24ab676SJeff Bonwick 	uint64_t *replayed_seq = &zilog->zl_replayed_seq[txg & TXG_MASK];
1266fa9e4066Sahrens 	lwb_t *lwb;
1267fa9e4066Sahrens 
126814843421SMatthew Ahrens 	/*
126914843421SMatthew Ahrens 	 * We don't zero out zl_destroy_txg, so make sure we don't try
127014843421SMatthew Ahrens 	 * to destroy it twice.
127114843421SMatthew Ahrens 	 */
127214843421SMatthew Ahrens 	if (spa_sync_pass(spa) != 1)
127314843421SMatthew Ahrens 		return;
127414843421SMatthew Ahrens 
1275d80c45e0Sbonwick 	mutex_enter(&zilog->zl_lock);
1276d80c45e0Sbonwick 
1277fa9e4066Sahrens 	ASSERT(zilog->zl_stop_sync == 0);
1278fa9e4066Sahrens 
1279b24ab676SJeff Bonwick 	if (*replayed_seq != 0) {
1280b24ab676SJeff Bonwick 		ASSERT(zh->zh_replay_seq < *replayed_seq);
1281b24ab676SJeff Bonwick 		zh->zh_replay_seq = *replayed_seq;
1282b24ab676SJeff Bonwick 		*replayed_seq = 0;
1283b24ab676SJeff Bonwick 	}
1284fa9e4066Sahrens 
1285fa9e4066Sahrens 	if (zilog->zl_destroy_txg == txg) {
1286d80c45e0Sbonwick 		blkptr_t blk = zh->zh_log;
1287d80c45e0Sbonwick 
1288d80c45e0Sbonwick 		ASSERT(list_head(&zilog->zl_lwb_list) == NULL);
1289d80c45e0Sbonwick 
1290d80c45e0Sbonwick 		bzero(zh, sizeof (zil_header_t));
12911209a471SNeil Perrin 		bzero(zilog->zl_replayed_seq, sizeof (zilog->zl_replayed_seq));
1292d80c45e0Sbonwick 
1293d80c45e0Sbonwick 		if (zilog->zl_keep_first) {
1294d80c45e0Sbonwick 			/*
1295d80c45e0Sbonwick 			 * If this block was part of log chain that couldn't
1296d80c45e0Sbonwick 			 * be claimed because a device was missing during
1297d80c45e0Sbonwick 			 * zil_claim(), but that device later returns,
1298d80c45e0Sbonwick 			 * then this block could erroneously appear valid.
1299d80c45e0Sbonwick 			 * To guard against this, assign a new GUID to the new
1300d80c45e0Sbonwick 			 * log chain so it doesn't matter what blk points to.
1301d80c45e0Sbonwick 			 */
1302d80c45e0Sbonwick 			zil_init_log_chain(zilog, &blk);
1303d80c45e0Sbonwick 			zh->zh_log = blk;
1304d80c45e0Sbonwick 		}
1305fa9e4066Sahrens 	}
1306fa9e4066Sahrens 
1307e6ca193dSGeorge Wilson 	while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
1308b19a79ecSperrin 		zh->zh_log = lwb->lwb_blk;
1309fa9e4066Sahrens 		if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg)
1310fa9e4066Sahrens 			break;
1311fa9e4066Sahrens 		list_remove(&zilog->zl_lwb_list, lwb);
1312b24ab676SJeff Bonwick 		zio_free_zil(spa, txg, &lwb->lwb_blk);
1313fa9e4066Sahrens 		kmem_cache_free(zil_lwb_cache, lwb);
1314d63d470bSgw 
1315d63d470bSgw 		/*
1316d63d470bSgw 		 * If we don't have anything left in the lwb list then
1317d63d470bSgw 		 * we've had an allocation failure and we need to zero
1318d63d470bSgw 		 * out the zil_header blkptr so that we don't end
1319d63d470bSgw 		 * up freeing the same block twice.
1320d63d470bSgw 		 */
1321d63d470bSgw 		if (list_head(&zilog->zl_lwb_list) == NULL)
1322d63d470bSgw 			BP_ZERO(&zh->zh_log);
1323fa9e4066Sahrens 	}
1324fa9e4066Sahrens 	mutex_exit(&zilog->zl_lock);
1325fa9e4066Sahrens }
1326fa9e4066Sahrens 
1327fa9e4066Sahrens void
1328fa9e4066Sahrens zil_init(void)
1329fa9e4066Sahrens {
1330fa9e4066Sahrens 	zil_lwb_cache = kmem_cache_create("zil_lwb_cache",
13315ad82045Snd 	    sizeof (struct lwb), 0, NULL, NULL, NULL, NULL, NULL, 0);
1332fa9e4066Sahrens }
1333fa9e4066Sahrens 
1334fa9e4066Sahrens void
1335fa9e4066Sahrens zil_fini(void)
1336fa9e4066Sahrens {
1337fa9e4066Sahrens 	kmem_cache_destroy(zil_lwb_cache);
1338fa9e4066Sahrens }
1339fa9e4066Sahrens 
1340e09fa4daSNeil Perrin void
1341e09fa4daSNeil Perrin zil_set_logbias(zilog_t *zilog, uint64_t logbias)
1342e09fa4daSNeil Perrin {
1343e09fa4daSNeil Perrin 	zilog->zl_logbias = logbias;
1344e09fa4daSNeil Perrin }
1345e09fa4daSNeil Perrin 
1346fa9e4066Sahrens zilog_t *
1347fa9e4066Sahrens zil_alloc(objset_t *os, zil_header_t *zh_phys)
1348fa9e4066Sahrens {
1349fa9e4066Sahrens 	zilog_t *zilog;
1350fa9e4066Sahrens 
1351fa9e4066Sahrens 	zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP);
1352fa9e4066Sahrens 
1353fa9e4066Sahrens 	zilog->zl_header = zh_phys;
1354fa9e4066Sahrens 	zilog->zl_os = os;
1355fa9e4066Sahrens 	zilog->zl_spa = dmu_objset_spa(os);
1356fa9e4066Sahrens 	zilog->zl_dmu_pool = dmu_objset_pool(os);
1357d80c45e0Sbonwick 	zilog->zl_destroy_txg = TXG_INITIAL - 1;
1358e09fa4daSNeil Perrin 	zilog->zl_logbias = dmu_objset_logbias(os);
1359fa9e4066Sahrens 
13605ad82045Snd 	mutex_init(&zilog->zl_lock, NULL, MUTEX_DEFAULT, NULL);
13615ad82045Snd 
1362fa9e4066Sahrens 	list_create(&zilog->zl_itx_list, sizeof (itx_t),
1363fa9e4066Sahrens 	    offsetof(itx_t, itx_node));
1364fa9e4066Sahrens 
1365fa9e4066Sahrens 	list_create(&zilog->zl_lwb_list, sizeof (lwb_t),
1366fa9e4066Sahrens 	    offsetof(lwb_t, lwb_node));
1367fa9e4066Sahrens 
136817f17c2dSbonwick 	mutex_init(&zilog->zl_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
136917f17c2dSbonwick 
137017f17c2dSbonwick 	avl_create(&zilog->zl_vdev_tree, zil_vdev_compare,
137117f17c2dSbonwick 	    sizeof (zil_vdev_node_t), offsetof(zil_vdev_node_t, zv_node));
1372fa9e4066Sahrens 
1373b7b97454Sperrin 	cv_init(&zilog->zl_cv_writer, NULL, CV_DEFAULT, NULL);
1374b7b97454Sperrin 	cv_init(&zilog->zl_cv_suspend, NULL, CV_DEFAULT, NULL);
1375b7b97454Sperrin 
1376fa9e4066Sahrens 	return (zilog);
1377fa9e4066Sahrens }
1378fa9e4066Sahrens 
1379fa9e4066Sahrens void
1380fa9e4066Sahrens zil_free(zilog_t *zilog)
1381fa9e4066Sahrens {
1382fa9e4066Sahrens 	lwb_t *lwb;
1383fa9e4066Sahrens 
1384fa9e4066Sahrens 	zilog->zl_stop_sync = 1;
1385fa9e4066Sahrens 
1386fa9e4066Sahrens 	while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
1387fa9e4066Sahrens 		list_remove(&zilog->zl_lwb_list, lwb);
1388fa9e4066Sahrens 		if (lwb->lwb_buf != NULL)
1389fa9e4066Sahrens 			zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
1390fa9e4066Sahrens 		kmem_cache_free(zil_lwb_cache, lwb);
1391fa9e4066Sahrens 	}
1392fa9e4066Sahrens 	list_destroy(&zilog->zl_lwb_list);
1393fa9e4066Sahrens 
139417f17c2dSbonwick 	avl_destroy(&zilog->zl_vdev_tree);
139517f17c2dSbonwick 	mutex_destroy(&zilog->zl_vdev_lock);
1396fa9e4066Sahrens 
1397fa9e4066Sahrens 	ASSERT(list_head(&zilog->zl_itx_list) == NULL);
1398fa9e4066Sahrens 	list_destroy(&zilog->zl_itx_list);
13995ad82045Snd 	mutex_destroy(&zilog->zl_lock);
1400fa9e4066Sahrens 
1401b7b97454Sperrin 	cv_destroy(&zilog->zl_cv_writer);
1402b7b97454Sperrin 	cv_destroy(&zilog->zl_cv_suspend);
1403b7b97454Sperrin 
1404fa9e4066Sahrens 	kmem_free(zilog, sizeof (zilog_t));
1405fa9e4066Sahrens }
1406fa9e4066Sahrens 
1407fa9e4066Sahrens /*
1408fa9e4066Sahrens  * Open an intent log.
1409fa9e4066Sahrens  */
1410fa9e4066Sahrens zilog_t *
1411fa9e4066Sahrens zil_open(objset_t *os, zil_get_data_t *get_data)
1412fa9e4066Sahrens {
1413fa9e4066Sahrens 	zilog_t *zilog = dmu_objset_zil(os);
1414fa9e4066Sahrens 
1415fa9e4066Sahrens 	zilog->zl_get_data = get_data;
1416fa9e4066Sahrens 	zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri,
1417fa9e4066Sahrens 	    2, 2, TASKQ_PREPOPULATE);
1418fa9e4066Sahrens 
1419fa9e4066Sahrens 	return (zilog);
1420fa9e4066Sahrens }
1421fa9e4066Sahrens 
1422fa9e4066Sahrens /*
1423fa9e4066Sahrens  * Close an intent log.
1424fa9e4066Sahrens  */
1425fa9e4066Sahrens void
1426fa9e4066Sahrens zil_close(zilog_t *zilog)
1427fa9e4066Sahrens {
1428d80c45e0Sbonwick 	/*
1429d80c45e0Sbonwick 	 * If the log isn't already committed, mark the objset dirty
1430d80c45e0Sbonwick 	 * (so zil_sync() will be called) and wait for that txg to sync.
1431d80c45e0Sbonwick 	 */
1432d80c45e0Sbonwick 	if (!zil_is_committed(zilog)) {
1433d80c45e0Sbonwick 		uint64_t txg;
1434d80c45e0Sbonwick 		dmu_tx_t *tx = dmu_tx_create(zilog->zl_os);
1435b24ab676SJeff Bonwick 		VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0);
1436d80c45e0Sbonwick 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
1437d80c45e0Sbonwick 		txg = dmu_tx_get_txg(tx);
1438d80c45e0Sbonwick 		dmu_tx_commit(tx);
1439d80c45e0Sbonwick 		txg_wait_synced(zilog->zl_dmu_pool, txg);
1440d80c45e0Sbonwick 	}
1441d80c45e0Sbonwick 
1442fa9e4066Sahrens 	taskq_destroy(zilog->zl_clean_taskq);
1443fa9e4066Sahrens 	zilog->zl_clean_taskq = NULL;
1444fa9e4066Sahrens 	zilog->zl_get_data = NULL;
1445fa9e4066Sahrens 
1446fa9e4066Sahrens 	zil_itx_clean(zilog);
1447fa9e4066Sahrens 	ASSERT(list_head(&zilog->zl_itx_list) == NULL);
1448fa9e4066Sahrens }
1449fa9e4066Sahrens 
1450fa9e4066Sahrens /*
1451fa9e4066Sahrens  * Suspend an intent log.  While in suspended mode, we still honor
1452fa9e4066Sahrens  * synchronous semantics, but we rely on txg_wait_synced() to do it.
1453fa9e4066Sahrens  * We suspend the log briefly when taking a snapshot so that the snapshot
1454fa9e4066Sahrens  * contains all the data it's supposed to, and has an empty intent log.
1455fa9e4066Sahrens  */
1456fa9e4066Sahrens int
1457fa9e4066Sahrens zil_suspend(zilog_t *zilog)
1458fa9e4066Sahrens {
1459d80c45e0Sbonwick 	const zil_header_t *zh = zilog->zl_header;
1460fa9e4066Sahrens 
1461fa9e4066Sahrens 	mutex_enter(&zilog->zl_lock);
14623589c4f0SNeil Perrin 	if (zh->zh_flags & ZIL_REPLAY_NEEDED) {		/* unplayed log */
1463fa9e4066Sahrens 		mutex_exit(&zilog->zl_lock);
1464fa9e4066Sahrens 		return (EBUSY);
1465fa9e4066Sahrens 	}
1466d80c45e0Sbonwick 	if (zilog->zl_suspend++ != 0) {
1467d80c45e0Sbonwick 		/*
1468d80c45e0Sbonwick 		 * Someone else already began a suspend.
1469d80c45e0Sbonwick 		 * Just wait for them to finish.
1470d80c45e0Sbonwick 		 */
1471d80c45e0Sbonwick 		while (zilog->zl_suspending)
1472d80c45e0Sbonwick 			cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock);
1473d80c45e0Sbonwick 		mutex_exit(&zilog->zl_lock);
1474d80c45e0Sbonwick 		return (0);
1475d80c45e0Sbonwick 	}
1476d80c45e0Sbonwick 	zilog->zl_suspending = B_TRUE;
1477fa9e4066Sahrens 	mutex_exit(&zilog->zl_lock);
1478fa9e4066Sahrens 
1479b19a79ecSperrin 	zil_commit(zilog, UINT64_MAX, 0);
1480fa9e4066Sahrens 
1481b19a79ecSperrin 	/*
1482b19a79ecSperrin 	 * Wait for any in-flight log writes to complete.
1483b19a79ecSperrin 	 */
1484fa9e4066Sahrens 	mutex_enter(&zilog->zl_lock);
1485b19a79ecSperrin 	while (zilog->zl_writer)
1486b19a79ecSperrin 		cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
1487fa9e4066Sahrens 	mutex_exit(&zilog->zl_lock);
1488fa9e4066Sahrens 
1489d80c45e0Sbonwick 	zil_destroy(zilog, B_FALSE);
1490d80c45e0Sbonwick 
1491d80c45e0Sbonwick 	mutex_enter(&zilog->zl_lock);
1492d80c45e0Sbonwick 	zilog->zl_suspending = B_FALSE;
1493d80c45e0Sbonwick 	cv_broadcast(&zilog->zl_cv_suspend);
1494d80c45e0Sbonwick 	mutex_exit(&zilog->zl_lock);
1495fa9e4066Sahrens 
1496fa9e4066Sahrens 	return (0);
1497fa9e4066Sahrens }
1498fa9e4066Sahrens 
1499fa9e4066Sahrens void
1500fa9e4066Sahrens zil_resume(zilog_t *zilog)
1501fa9e4066Sahrens {
1502fa9e4066Sahrens 	mutex_enter(&zilog->zl_lock);
1503fa9e4066Sahrens 	ASSERT(zilog->zl_suspend != 0);
1504fa9e4066Sahrens 	zilog->zl_suspend--;
1505fa9e4066Sahrens 	mutex_exit(&zilog->zl_lock);
1506fa9e4066Sahrens }
1507fa9e4066Sahrens 
1508fa9e4066Sahrens typedef struct zil_replay_arg {
1509fa9e4066Sahrens 	zil_replay_func_t **zr_replay;
1510fa9e4066Sahrens 	void		*zr_arg;
1511fa9e4066Sahrens 	boolean_t	zr_byteswap;
1512b24ab676SJeff Bonwick 	char		*zr_lr;
1513fa9e4066Sahrens } zil_replay_arg_t;
1514fa9e4066Sahrens 
1515b24ab676SJeff Bonwick static int
1516b24ab676SJeff Bonwick zil_replay_error(zilog_t *zilog, lr_t *lr, int error)
1517b24ab676SJeff Bonwick {
1518b24ab676SJeff Bonwick 	char name[MAXNAMELEN];
1519b24ab676SJeff Bonwick 
1520b24ab676SJeff Bonwick 	zilog->zl_replaying_seq--;	/* didn't actually replay this one */
1521b24ab676SJeff Bonwick 
1522b24ab676SJeff Bonwick 	dmu_objset_name(zilog->zl_os, name);
1523b24ab676SJeff Bonwick 
1524b24ab676SJeff Bonwick 	cmn_err(CE_WARN, "ZFS replay transaction error %d, "
1525b24ab676SJeff Bonwick 	    "dataset %s, seq 0x%llx, txtype %llu %s\n", error, name,
1526b24ab676SJeff Bonwick 	    (u_longlong_t)lr->lrc_seq,
1527b24ab676SJeff Bonwick 	    (u_longlong_t)(lr->lrc_txtype & ~TX_CI),
1528b24ab676SJeff Bonwick 	    (lr->lrc_txtype & TX_CI) ? "CI" : "");
1529b24ab676SJeff Bonwick 
1530b24ab676SJeff Bonwick 	return (error);
1531b24ab676SJeff Bonwick }
1532b24ab676SJeff Bonwick 
1533b24ab676SJeff Bonwick static int
1534fa9e4066Sahrens zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg)
1535fa9e4066Sahrens {
1536fa9e4066Sahrens 	zil_replay_arg_t *zr = zra;
1537d80c45e0Sbonwick 	const zil_header_t *zh = zilog->zl_header;
1538fa9e4066Sahrens 	uint64_t reclen = lr->lrc_reclen;
1539fa9e4066Sahrens 	uint64_t txtype = lr->lrc_txtype;
1540b24ab676SJeff Bonwick 	int error = 0;
1541fa9e4066Sahrens 
1542b24ab676SJeff Bonwick 	zilog->zl_replaying_seq = lr->lrc_seq;
1543fa9e4066Sahrens 
1544fa9e4066Sahrens 	if (lr->lrc_seq <= zh->zh_replay_seq)	/* already replayed */
1545b24ab676SJeff Bonwick 		return (0);
1546b24ab676SJeff Bonwick 
1547b24ab676SJeff Bonwick 	if (lr->lrc_txg < claim_txg)		/* already committed */
1548b24ab676SJeff Bonwick 		return (0);
1549fa9e4066Sahrens 
1550da6c28aaSamw 	/* Strip case-insensitive bit, still present in log record */
1551da6c28aaSamw 	txtype &= ~TX_CI;
1552da6c28aaSamw 
1553b24ab676SJeff Bonwick 	if (txtype == 0 || txtype >= TX_MAX_TYPE)
1554b24ab676SJeff Bonwick 		return (zil_replay_error(zilog, lr, EINVAL));
1555b24ab676SJeff Bonwick 
1556b24ab676SJeff Bonwick 	/*
1557b24ab676SJeff Bonwick 	 * If this record type can be logged out of order, the object
1558b24ab676SJeff Bonwick 	 * (lr_foid) may no longer exist.  That's legitimate, not an error.
1559b24ab676SJeff Bonwick 	 */
1560b24ab676SJeff Bonwick 	if (TX_OOO(txtype)) {
1561b24ab676SJeff Bonwick 		error = dmu_object_info(zilog->zl_os,
1562b24ab676SJeff Bonwick 		    ((lr_ooo_t *)lr)->lr_foid, NULL);
1563b24ab676SJeff Bonwick 		if (error == ENOENT || error == EEXIST)
1564b24ab676SJeff Bonwick 			return (0);
15651209a471SNeil Perrin 	}
15661209a471SNeil Perrin 
1567fa9e4066Sahrens 	/*
1568fa9e4066Sahrens 	 * Make a copy of the data so we can revise and extend it.
1569fa9e4066Sahrens 	 */
1570b24ab676SJeff Bonwick 	bcopy(lr, zr->zr_lr, reclen);
1571b24ab676SJeff Bonwick 
1572b24ab676SJeff Bonwick 	/*
1573b24ab676SJeff Bonwick 	 * If this is a TX_WRITE with a blkptr, suck in the data.
1574b24ab676SJeff Bonwick 	 */
1575b24ab676SJeff Bonwick 	if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) {
1576b24ab676SJeff Bonwick 		error = zil_read_log_data(zilog, (lr_write_t *)lr,
1577b24ab676SJeff Bonwick 		    zr->zr_lr + reclen);
1578b24ab676SJeff Bonwick 		if (error)
1579b24ab676SJeff Bonwick 			return (zil_replay_error(zilog, lr, error));
1580b24ab676SJeff Bonwick 	}
1581fa9e4066Sahrens 
1582fa9e4066Sahrens 	/*
1583fa9e4066Sahrens 	 * The log block containing this lr may have been byteswapped
1584fa9e4066Sahrens 	 * so that we can easily examine common fields like lrc_txtype.
1585b24ab676SJeff Bonwick 	 * However, the log is a mix of different record types, and only the
1586fa9e4066Sahrens 	 * replay vectors know how to byteswap their records.  Therefore, if
1587fa9e4066Sahrens 	 * the lr was byteswapped, undo it before invoking the replay vector.
1588fa9e4066Sahrens 	 */
1589fa9e4066Sahrens 	if (zr->zr_byteswap)
1590b24ab676SJeff Bonwick 		byteswap_uint64_array(zr->zr_lr, reclen);
1591fa9e4066Sahrens 
1592fa9e4066Sahrens 	/*
1593fa9e4066Sahrens 	 * We must now do two things atomically: replay this log record,
15941209a471SNeil Perrin 	 * and update the log header sequence number to reflect the fact that
15951209a471SNeil Perrin 	 * we did so. At the end of each replay function the sequence number
15961209a471SNeil Perrin 	 * is updated if we are in replay mode.
1597fa9e4066Sahrens 	 */
1598b24ab676SJeff Bonwick 	error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lr, zr->zr_byteswap);
1599b24ab676SJeff Bonwick 	if (error) {
160067bd71c6Sperrin 		/*
160167bd71c6Sperrin 		 * The DMU's dnode layer doesn't see removes until the txg
160267bd71c6Sperrin 		 * commits, so a subsequent claim can spuriously fail with
16031209a471SNeil Perrin 		 * EEXIST. So if we receive any error we try syncing out
1604b24ab676SJeff Bonwick 		 * any removes then retry the transaction.  Note that we
1605b24ab676SJeff Bonwick 		 * specify B_FALSE for byteswap now, so we don't do it twice.
160667bd71c6Sperrin 		 */
1607b24ab676SJeff Bonwick 		txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0);
1608b24ab676SJeff Bonwick 		error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lr, B_FALSE);
1609b24ab676SJeff Bonwick 		if (error)
1610b24ab676SJeff Bonwick 			return (zil_replay_error(zilog, lr, error));
1611fa9e4066Sahrens 	}
1612b24ab676SJeff Bonwick 	return (0);
161367bd71c6Sperrin }
1614fa9e4066Sahrens 
161567bd71c6Sperrin /* ARGSUSED */
1616b24ab676SJeff Bonwick static int
161767bd71c6Sperrin zil_incr_blks(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
161867bd71c6Sperrin {
161967bd71c6Sperrin 	zilog->zl_replay_blks++;
1620b24ab676SJeff Bonwick 
1621b24ab676SJeff Bonwick 	return (0);
1622fa9e4066Sahrens }
1623fa9e4066Sahrens 
1624fa9e4066Sahrens /*
162513f5297eSperrin  * If this dataset has a non-empty intent log, replay it and destroy it.
1626fa9e4066Sahrens  */
1627fa9e4066Sahrens void
16281209a471SNeil Perrin zil_replay(objset_t *os, void *arg, zil_replay_func_t *replay_func[TX_MAX_TYPE])
1629fa9e4066Sahrens {
1630fa9e4066Sahrens 	zilog_t *zilog = dmu_objset_zil(os);
1631d80c45e0Sbonwick 	const zil_header_t *zh = zilog->zl_header;
1632d80c45e0Sbonwick 	zil_replay_arg_t zr;
163313f5297eSperrin 
16343589c4f0SNeil Perrin 	if ((zh->zh_flags & ZIL_REPLAY_NEEDED) == 0) {
1635d80c45e0Sbonwick 		zil_destroy(zilog, B_TRUE);
163613f5297eSperrin 		return;
163713f5297eSperrin 	}
1638fa9e4066Sahrens 
1639fa9e4066Sahrens 	zr.zr_replay = replay_func;
1640fa9e4066Sahrens 	zr.zr_arg = arg;
1641d80c45e0Sbonwick 	zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log);
1642b24ab676SJeff Bonwick 	zr.zr_lr = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP);
1643fa9e4066Sahrens 
1644fa9e4066Sahrens 	/*
1645fa9e4066Sahrens 	 * Wait for in-progress removes to sync before starting replay.
1646fa9e4066Sahrens 	 */
1647fa9e4066Sahrens 	txg_wait_synced(zilog->zl_dmu_pool, 0);
1648fa9e4066Sahrens 
16491209a471SNeil Perrin 	zilog->zl_replay = B_TRUE;
1650d3d50737SRafael Vanoni 	zilog->zl_replay_time = ddi_get_lbolt();
165167bd71c6Sperrin 	ASSERT(zilog->zl_replay_blks == 0);
165267bd71c6Sperrin 	(void) zil_parse(zilog, zil_incr_blks, zil_replay_log_record, &zr,
1653d80c45e0Sbonwick 	    zh->zh_claim_txg);
1654b24ab676SJeff Bonwick 	kmem_free(zr.zr_lr, 2 * SPA_MAXBLOCKSIZE);
1655fa9e4066Sahrens 
1656d80c45e0Sbonwick 	zil_destroy(zilog, B_FALSE);
1657a4611edeSahrens 	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
16581209a471SNeil Perrin 	zilog->zl_replay = B_FALSE;
1659fa9e4066Sahrens }
1660436b2950Sperrin 
1661b24ab676SJeff Bonwick boolean_t
1662b24ab676SJeff Bonwick zil_replaying(zilog_t *zilog, dmu_tx_t *tx)
1663436b2950Sperrin {
1664b24ab676SJeff Bonwick 	if (zilog == NULL)
1665b24ab676SJeff Bonwick 		return (B_TRUE);
1666436b2950Sperrin 
1667b24ab676SJeff Bonwick 	if (zilog->zl_replay) {
1668b24ab676SJeff Bonwick 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
1669b24ab676SJeff Bonwick 		zilog->zl_replayed_seq[dmu_tx_get_txg(tx) & TXG_MASK] =
1670b24ab676SJeff Bonwick 		    zilog->zl_replaying_seq;
1671b24ab676SJeff Bonwick 		return (B_TRUE);
1672b19a79ecSperrin 	}
1673b19a79ecSperrin 
1674b24ab676SJeff Bonwick 	return (B_FALSE);
1675436b2950Sperrin }
1676e6ca193dSGeorge Wilson 
1677e6ca193dSGeorge Wilson /* ARGSUSED */
1678e6ca193dSGeorge Wilson int
1679*fd136879SMatthew Ahrens zil_vdev_offline(const char *osname, void *arg)
1680e6ca193dSGeorge Wilson {
1681e6ca193dSGeorge Wilson 	objset_t *os;
1682e6ca193dSGeorge Wilson 	zilog_t *zilog;
1683e6ca193dSGeorge Wilson 	int error;
1684e6ca193dSGeorge Wilson 
1685503ad85cSMatthew Ahrens 	error = dmu_objset_hold(osname, FTAG, &os);
1686e6ca193dSGeorge Wilson 	if (error)
1687e6ca193dSGeorge Wilson 		return (error);
1688e6ca193dSGeorge Wilson 
1689e6ca193dSGeorge Wilson 	zilog = dmu_objset_zil(os);
1690e6ca193dSGeorge Wilson 	if (zil_suspend(zilog) != 0)
1691e6ca193dSGeorge Wilson 		error = EEXIST;
1692e6ca193dSGeorge Wilson 	else
1693e6ca193dSGeorge Wilson 		zil_resume(zilog);
1694503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
1695e6ca193dSGeorge Wilson 	return (error);
1696e6ca193dSGeorge Wilson }
1697