xref: /illumos-gate/usr/src/uts/common/fs/zfs/zil.c (revision e09fa4da)
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>
28e6ca193dSGeorge Wilson #include <sys/spa_impl.h>
29fa9e4066Sahrens #include <sys/dmu.h>
30fa9e4066Sahrens #include <sys/zap.h>
31fa9e4066Sahrens #include <sys/arc.h>
32fa9e4066Sahrens #include <sys/stat.h>
33fa9e4066Sahrens #include <sys/resource.h>
34fa9e4066Sahrens #include <sys/zil.h>
35fa9e4066Sahrens #include <sys/zil_impl.h>
36fa9e4066Sahrens #include <sys/dsl_dataset.h>
37fa9e4066Sahrens #include <sys/vdev.h>
38d63d470bSgw #include <sys/dmu_tx.h>
39fa9e4066Sahrens 
40fa9e4066Sahrens /*
41fa9e4066Sahrens  * The zfs intent log (ZIL) saves transaction records of system calls
42fa9e4066Sahrens  * that change the file system in memory with enough information
43fa9e4066Sahrens  * to be able to replay them. These are stored in memory until
44fa9e4066Sahrens  * either the DMU transaction group (txg) commits them to the stable pool
45fa9e4066Sahrens  * and they can be discarded, or they are flushed to the stable log
46fa9e4066Sahrens  * (also in the pool) due to a fsync, O_DSYNC or other synchronous
47fa9e4066Sahrens  * requirement. In the event of a panic or power fail then those log
48fa9e4066Sahrens  * records (transactions) are replayed.
49fa9e4066Sahrens  *
50fa9e4066Sahrens  * There is one ZIL per file system. Its on-disk (pool) format consists
51fa9e4066Sahrens  * of 3 parts:
52fa9e4066Sahrens  *
53fa9e4066Sahrens  * 	- ZIL header
54fa9e4066Sahrens  * 	- ZIL blocks
55fa9e4066Sahrens  * 	- ZIL records
56fa9e4066Sahrens  *
57fa9e4066Sahrens  * A log record holds a system call transaction. Log blocks can
58fa9e4066Sahrens  * hold many log records and the blocks are chained together.
59fa9e4066Sahrens  * Each ZIL block contains a block pointer (blkptr_t) to the next
60fa9e4066Sahrens  * ZIL block in the chain. The ZIL header points to the first
61fa9e4066Sahrens  * block in the chain. Note there is not a fixed place in the pool
62fa9e4066Sahrens  * to hold blocks. They are dynamically allocated and freed as
63fa9e4066Sahrens  * needed from the blocks available. Figure X shows the ZIL structure:
64fa9e4066Sahrens  */
65fa9e4066Sahrens 
66fa9e4066Sahrens /*
67416e0cd8Sek  * This global ZIL switch affects all pools
68fa9e4066Sahrens  */
69fa9e4066Sahrens int zil_disable = 0;	/* disable intent logging */
70416e0cd8Sek 
71416e0cd8Sek /*
72416e0cd8Sek  * Tunable parameter for debugging or performance analysis.  Setting
73416e0cd8Sek  * zfs_nocacheflush will cause corruption on power loss if a volatile
74416e0cd8Sek  * out-of-order write cache is enabled.
75416e0cd8Sek  */
76416e0cd8Sek boolean_t zfs_nocacheflush = B_FALSE;
77fa9e4066Sahrens 
78fa9e4066Sahrens static kmem_cache_t *zil_lwb_cache;
79fa9e4066Sahrens 
80fa9e4066Sahrens static int
81fa9e4066Sahrens zil_dva_compare(const void *x1, const void *x2)
82fa9e4066Sahrens {
83fa9e4066Sahrens 	const dva_t *dva1 = x1;
84fa9e4066Sahrens 	const dva_t *dva2 = x2;
85fa9e4066Sahrens 
86fa9e4066Sahrens 	if (DVA_GET_VDEV(dva1) < DVA_GET_VDEV(dva2))
87fa9e4066Sahrens 		return (-1);
88fa9e4066Sahrens 	if (DVA_GET_VDEV(dva1) > DVA_GET_VDEV(dva2))
89fa9e4066Sahrens 		return (1);
90fa9e4066Sahrens 
91fa9e4066Sahrens 	if (DVA_GET_OFFSET(dva1) < DVA_GET_OFFSET(dva2))
92fa9e4066Sahrens 		return (-1);
93fa9e4066Sahrens 	if (DVA_GET_OFFSET(dva1) > DVA_GET_OFFSET(dva2))
94fa9e4066Sahrens 		return (1);
95fa9e4066Sahrens 
96fa9e4066Sahrens 	return (0);
97fa9e4066Sahrens }
98fa9e4066Sahrens 
99fa9e4066Sahrens static void
100fa9e4066Sahrens zil_dva_tree_init(avl_tree_t *t)
101fa9e4066Sahrens {
102fa9e4066Sahrens 	avl_create(t, zil_dva_compare, sizeof (zil_dva_node_t),
103fa9e4066Sahrens 	    offsetof(zil_dva_node_t, zn_node));
104fa9e4066Sahrens }
105fa9e4066Sahrens 
106fa9e4066Sahrens static void
107fa9e4066Sahrens zil_dva_tree_fini(avl_tree_t *t)
108fa9e4066Sahrens {
109fa9e4066Sahrens 	zil_dva_node_t *zn;
110fa9e4066Sahrens 	void *cookie = NULL;
111fa9e4066Sahrens 
112fa9e4066Sahrens 	while ((zn = avl_destroy_nodes(t, &cookie)) != NULL)
113fa9e4066Sahrens 		kmem_free(zn, sizeof (zil_dva_node_t));
114fa9e4066Sahrens 
115fa9e4066Sahrens 	avl_destroy(t);
116fa9e4066Sahrens }
117fa9e4066Sahrens 
118fa9e4066Sahrens static int
119fa9e4066Sahrens zil_dva_tree_add(avl_tree_t *t, dva_t *dva)
120fa9e4066Sahrens {
121fa9e4066Sahrens 	zil_dva_node_t *zn;
122fa9e4066Sahrens 	avl_index_t where;
123fa9e4066Sahrens 
124fa9e4066Sahrens 	if (avl_find(t, dva, &where) != NULL)
125fa9e4066Sahrens 		return (EEXIST);
126fa9e4066Sahrens 
127fa9e4066Sahrens 	zn = kmem_alloc(sizeof (zil_dva_node_t), KM_SLEEP);
128fa9e4066Sahrens 	zn->zn_dva = *dva;
129fa9e4066Sahrens 	avl_insert(t, zn, where);
130fa9e4066Sahrens 
131fa9e4066Sahrens 	return (0);
132fa9e4066Sahrens }
133fa9e4066Sahrens 
134d80c45e0Sbonwick static zil_header_t *
135d80c45e0Sbonwick zil_header_in_syncing_context(zilog_t *zilog)
136d80c45e0Sbonwick {
137d80c45e0Sbonwick 	return ((zil_header_t *)zilog->zl_header);
138d80c45e0Sbonwick }
139d80c45e0Sbonwick 
140d80c45e0Sbonwick static void
141d80c45e0Sbonwick zil_init_log_chain(zilog_t *zilog, blkptr_t *bp)
142d80c45e0Sbonwick {
143d80c45e0Sbonwick 	zio_cksum_t *zc = &bp->blk_cksum;
144d80c45e0Sbonwick 
145d80c45e0Sbonwick 	zc->zc_word[ZIL_ZC_GUID_0] = spa_get_random(-1ULL);
146d80c45e0Sbonwick 	zc->zc_word[ZIL_ZC_GUID_1] = spa_get_random(-1ULL);
147d80c45e0Sbonwick 	zc->zc_word[ZIL_ZC_OBJSET] = dmu_objset_id(zilog->zl_os);
148d80c45e0Sbonwick 	zc->zc_word[ZIL_ZC_SEQ] = 1ULL;
149d80c45e0Sbonwick }
150d80c45e0Sbonwick 
151fa9e4066Sahrens /*
152fa9e4066Sahrens  * Read a log block, make sure it's valid, and byteswap it if necessary.
153fa9e4066Sahrens  */
154fa9e4066Sahrens static int
155d80c45e0Sbonwick zil_read_log_block(zilog_t *zilog, const blkptr_t *bp, arc_buf_t **abufpp)
156fa9e4066Sahrens {
157d80c45e0Sbonwick 	blkptr_t blk = *bp;
158ea8dc4b6Seschrock 	zbookmark_t zb;
15913506d1eSmaybee 	uint32_t aflags = ARC_WAIT;
160fa9e4066Sahrens 	int error;
161fa9e4066Sahrens 
162d80c45e0Sbonwick 	zb.zb_objset = bp->blk_cksum.zc_word[ZIL_ZC_OBJSET];
163ea8dc4b6Seschrock 	zb.zb_object = 0;
164ea8dc4b6Seschrock 	zb.zb_level = -1;
165d80c45e0Sbonwick 	zb.zb_blkid = bp->blk_cksum.zc_word[ZIL_ZC_SEQ];
166ea8dc4b6Seschrock 
167d80c45e0Sbonwick 	*abufpp = NULL;
168fa9e4066Sahrens 
169088f3894Sahrens 	/*
170088f3894Sahrens 	 * We shouldn't be doing any scrubbing while we're doing log
171088f3894Sahrens 	 * replay, it's OK to not lock.
172088f3894Sahrens 	 */
173088f3894Sahrens 	error = arc_read_nolock(NULL, zilog->zl_spa, &blk,
174d80c45e0Sbonwick 	    arc_getbuf_func, abufpp, ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL |
17513506d1eSmaybee 	    ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB, &aflags, &zb);
176fa9e4066Sahrens 
177d80c45e0Sbonwick 	if (error == 0) {
178d80c45e0Sbonwick 		char *data = (*abufpp)->b_data;
179d80c45e0Sbonwick 		uint64_t blksz = BP_GET_LSIZE(bp);
180d80c45e0Sbonwick 		zil_trailer_t *ztp = (zil_trailer_t *)(data + blksz) - 1;
181d80c45e0Sbonwick 		zio_cksum_t cksum = bp->blk_cksum;
182fa9e4066Sahrens 
183d80c45e0Sbonwick 		/*
184f5e6e722SNeil Perrin 		 * Validate the checksummed log block.
185f5e6e722SNeil Perrin 		 *
186d80c45e0Sbonwick 		 * Sequence numbers should be... sequential.  The checksum
187d80c45e0Sbonwick 		 * verifier for the next block should be bp's checksum plus 1.
188f5e6e722SNeil Perrin 		 *
189f5e6e722SNeil Perrin 		 * Also check the log chain linkage and size used.
190d80c45e0Sbonwick 		 */
191d80c45e0Sbonwick 		cksum.zc_word[ZIL_ZC_SEQ]++;
192d80c45e0Sbonwick 
193f5e6e722SNeil Perrin 		if (bcmp(&cksum, &ztp->zit_next_blk.blk_cksum,
194f5e6e722SNeil Perrin 		    sizeof (cksum)) || BP_IS_HOLE(&ztp->zit_next_blk) ||
195f5e6e722SNeil Perrin 		    (ztp->zit_nused > (blksz - sizeof (zil_trailer_t)))) {
196f5e6e722SNeil Perrin 			error = ECKSUM;
197f5e6e722SNeil Perrin 		}
198fa9e4066Sahrens 
199d80c45e0Sbonwick 		if (error) {
200d80c45e0Sbonwick 			VERIFY(arc_buf_remove_ref(*abufpp, abufpp) == 1);
201d80c45e0Sbonwick 			*abufpp = NULL;
202d80c45e0Sbonwick 		}
203fa9e4066Sahrens 	}
204fa9e4066Sahrens 
205d80c45e0Sbonwick 	dprintf("error %d on %llu:%llu\n", error, zb.zb_objset, zb.zb_blkid);
206fa9e4066Sahrens 
207d80c45e0Sbonwick 	return (error);
208fa9e4066Sahrens }
209fa9e4066Sahrens 
210fa9e4066Sahrens /*
211fa9e4066Sahrens  * Parse the intent log, and call parse_func for each valid record within.
212d80c45e0Sbonwick  * Return the highest sequence number.
213fa9e4066Sahrens  */
214d80c45e0Sbonwick uint64_t
215fa9e4066Sahrens zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func,
216fa9e4066Sahrens     zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg)
217fa9e4066Sahrens {
218d80c45e0Sbonwick 	const zil_header_t *zh = zilog->zl_header;
219d80c45e0Sbonwick 	uint64_t claim_seq = zh->zh_claim_seq;
220d80c45e0Sbonwick 	uint64_t seq = 0;
221d80c45e0Sbonwick 	uint64_t max_seq = 0;
222d80c45e0Sbonwick 	blkptr_t blk = zh->zh_log;
223d80c45e0Sbonwick 	arc_buf_t *abuf;
224fa9e4066Sahrens 	char *lrbuf, *lrp;
225fa9e4066Sahrens 	zil_trailer_t *ztp;
226fa9e4066Sahrens 	int reclen, error;
227fa9e4066Sahrens 
228fa9e4066Sahrens 	if (BP_IS_HOLE(&blk))
229d80c45e0Sbonwick 		return (max_seq);
230fa9e4066Sahrens 
231fa9e4066Sahrens 	/*
232fa9e4066Sahrens 	 * Starting at the block pointed to by zh_log we read the log chain.
233fa9e4066Sahrens 	 * For each block in the chain we strongly check that block to
234fa9e4066Sahrens 	 * ensure its validity.  We stop when an invalid block is found.
235fa9e4066Sahrens 	 * For each block pointer in the chain we call parse_blk_func().
236fa9e4066Sahrens 	 * For each record in each valid block we call parse_lr_func().
237d80c45e0Sbonwick 	 * If the log has been claimed, stop if we encounter a sequence
238d80c45e0Sbonwick 	 * number greater than the highest claimed sequence number.
239fa9e4066Sahrens 	 */
240fa9e4066Sahrens 	zil_dva_tree_init(&zilog->zl_dva_tree);
241fa9e4066Sahrens 	for (;;) {
242d80c45e0Sbonwick 		seq = blk.blk_cksum.zc_word[ZIL_ZC_SEQ];
243d80c45e0Sbonwick 
244d80c45e0Sbonwick 		if (claim_seq != 0 && seq > claim_seq)
245d80c45e0Sbonwick 			break;
246d80c45e0Sbonwick 
247d80c45e0Sbonwick 		ASSERT(max_seq < seq);
248d80c45e0Sbonwick 		max_seq = seq;
249d80c45e0Sbonwick 
250d80c45e0Sbonwick 		error = zil_read_log_block(zilog, &blk, &abuf);
251fa9e4066Sahrens 
252fa9e4066Sahrens 		if (parse_blk_func != NULL)
253fa9e4066Sahrens 			parse_blk_func(zilog, &blk, arg, txg);
254fa9e4066Sahrens 
255fa9e4066Sahrens 		if (error)
256fa9e4066Sahrens 			break;
257fa9e4066Sahrens 
258d80c45e0Sbonwick 		lrbuf = abuf->b_data;
259fa9e4066Sahrens 		ztp = (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1;
260fa9e4066Sahrens 		blk = ztp->zit_next_blk;
261fa9e4066Sahrens 
262d80c45e0Sbonwick 		if (parse_lr_func == NULL) {
263d80c45e0Sbonwick 			VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
264fa9e4066Sahrens 			continue;
265d80c45e0Sbonwick 		}
266fa9e4066Sahrens 
267fa9e4066Sahrens 		for (lrp = lrbuf; lrp < lrbuf + ztp->zit_nused; lrp += reclen) {
268fa9e4066Sahrens 			lr_t *lr = (lr_t *)lrp;
269fa9e4066Sahrens 			reclen = lr->lrc_reclen;
270fa9e4066Sahrens 			ASSERT3U(reclen, >=, sizeof (lr_t));
271fa9e4066Sahrens 			parse_lr_func(zilog, lr, arg, txg);
272fa9e4066Sahrens 		}
273d80c45e0Sbonwick 		VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
274fa9e4066Sahrens 	}
275fa9e4066Sahrens 	zil_dva_tree_fini(&zilog->zl_dva_tree);
276d80c45e0Sbonwick 
277d80c45e0Sbonwick 	return (max_seq);
278fa9e4066Sahrens }
279fa9e4066Sahrens 
280fa9e4066Sahrens /* ARGSUSED */
281fa9e4066Sahrens static void
282fa9e4066Sahrens zil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg)
283fa9e4066Sahrens {
284fa9e4066Sahrens 	spa_t *spa = zilog->zl_spa;
285fa9e4066Sahrens 	int err;
286fa9e4066Sahrens 
287fa9e4066Sahrens 	/*
288fa9e4066Sahrens 	 * Claim log block if not already committed and not already claimed.
289fa9e4066Sahrens 	 */
290fa9e4066Sahrens 	if (bp->blk_birth >= first_txg &&
291fa9e4066Sahrens 	    zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp)) == 0) {
292e14bb325SJeff Bonwick 		err = zio_wait(zio_claim(NULL, spa, first_txg, bp, NULL, NULL,
293e14bb325SJeff Bonwick 		    ZIO_FLAG_MUSTSUCCEED));
294fa9e4066Sahrens 		ASSERT(err == 0);
295fa9e4066Sahrens 	}
296fa9e4066Sahrens }
297fa9e4066Sahrens 
298fa9e4066Sahrens static void
299fa9e4066Sahrens zil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg)
300fa9e4066Sahrens {
301fa9e4066Sahrens 	if (lrc->lrc_txtype == TX_WRITE) {
302fa9e4066Sahrens 		lr_write_t *lr = (lr_write_t *)lrc;
303fa9e4066Sahrens 		zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg);
304fa9e4066Sahrens 	}
305fa9e4066Sahrens }
306fa9e4066Sahrens 
307fa9e4066Sahrens /* ARGSUSED */
308fa9e4066Sahrens static void
309fa9e4066Sahrens zil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg)
310fa9e4066Sahrens {
311fa9e4066Sahrens 	zio_free_blk(zilog->zl_spa, bp, dmu_tx_get_txg(tx));
312fa9e4066Sahrens }
313fa9e4066Sahrens 
314fa9e4066Sahrens static void
315fa9e4066Sahrens zil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg)
316fa9e4066Sahrens {
317fa9e4066Sahrens 	/*
318fa9e4066Sahrens 	 * If we previously claimed it, we need to free it.
319fa9e4066Sahrens 	 */
320fa9e4066Sahrens 	if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE) {
321fa9e4066Sahrens 		lr_write_t *lr = (lr_write_t *)lrc;
322fa9e4066Sahrens 		blkptr_t *bp = &lr->lr_blkptr;
323fa9e4066Sahrens 		if (bp->blk_birth >= claim_txg &&
324fa9e4066Sahrens 		    !zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp))) {
325fa9e4066Sahrens 			(void) arc_free(NULL, zilog->zl_spa,
326fa9e4066Sahrens 			    dmu_tx_get_txg(tx), bp, NULL, NULL, ARC_WAIT);
327fa9e4066Sahrens 		}
328fa9e4066Sahrens 	}
329fa9e4066Sahrens }
330fa9e4066Sahrens 
331fa9e4066Sahrens /*
332fa9e4066Sahrens  * Create an on-disk intent log.
333fa9e4066Sahrens  */
334fa9e4066Sahrens static void
335fa9e4066Sahrens zil_create(zilog_t *zilog)
336fa9e4066Sahrens {
337d80c45e0Sbonwick 	const zil_header_t *zh = zilog->zl_header;
338fa9e4066Sahrens 	lwb_t *lwb;
339d80c45e0Sbonwick 	uint64_t txg = 0;
340d80c45e0Sbonwick 	dmu_tx_t *tx = NULL;
341fa9e4066Sahrens 	blkptr_t blk;
342d80c45e0Sbonwick 	int error = 0;
343fa9e4066Sahrens 
344fa9e4066Sahrens 	/*
345d80c45e0Sbonwick 	 * Wait for any previous destroy to complete.
346fa9e4066Sahrens 	 */
347d80c45e0Sbonwick 	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
348d80c45e0Sbonwick 
349d80c45e0Sbonwick 	ASSERT(zh->zh_claim_txg == 0);
350d80c45e0Sbonwick 	ASSERT(zh->zh_replay_seq == 0);
351d80c45e0Sbonwick 
352d80c45e0Sbonwick 	blk = zh->zh_log;
353fa9e4066Sahrens 
354fa9e4066Sahrens 	/*
355899217ddSNeil Perrin 	 * If we don't already have an initial log block or we have one
356899217ddSNeil Perrin 	 * but it's the wrong endianness then allocate one.
357fa9e4066Sahrens 	 */
358899217ddSNeil Perrin 	if (BP_IS_HOLE(&blk) || BP_SHOULD_BYTESWAP(&blk)) {
359d80c45e0Sbonwick 		tx = dmu_tx_create(zilog->zl_os);
360d80c45e0Sbonwick 		(void) dmu_tx_assign(tx, TXG_WAIT);
361d80c45e0Sbonwick 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
362d80c45e0Sbonwick 		txg = dmu_tx_get_txg(tx);
363d80c45e0Sbonwick 
364899217ddSNeil Perrin 		if (!BP_IS_HOLE(&blk)) {
365899217ddSNeil Perrin 			zio_free_blk(zilog->zl_spa, &blk, txg);
366899217ddSNeil Perrin 			BP_ZERO(&blk);
367899217ddSNeil Perrin 		}
368899217ddSNeil Perrin 
36967bd71c6Sperrin 		error = zio_alloc_blk(zilog->zl_spa, ZIL_MIN_BLKSZ, &blk,
370*e09fa4daSNeil Perrin 		    NULL, txg, zilog->zl_logbias != ZFS_LOGBIAS_LATENCY);
371d80c45e0Sbonwick 
372d80c45e0Sbonwick 		if (error == 0)
373d80c45e0Sbonwick 			zil_init_log_chain(zilog, &blk);
37413f5297eSperrin 	}
375fa9e4066Sahrens 
376d80c45e0Sbonwick 	/*
377d80c45e0Sbonwick 	 * Allocate a log write buffer (lwb) for the first log block.
378d80c45e0Sbonwick 	 */
379d80c45e0Sbonwick 	if (error == 0) {
380fa9e4066Sahrens 		lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP);
381fa9e4066Sahrens 		lwb->lwb_zilog = zilog;
382fa9e4066Sahrens 		lwb->lwb_blk = blk;
383fa9e4066Sahrens 		lwb->lwb_nused = 0;
384fa9e4066Sahrens 		lwb->lwb_sz = BP_GET_LSIZE(&lwb->lwb_blk);
385fa9e4066Sahrens 		lwb->lwb_buf = zio_buf_alloc(lwb->lwb_sz);
386fa9e4066Sahrens 		lwb->lwb_max_txg = txg;
387c5c6ffa0Smaybee 		lwb->lwb_zio = NULL;
388c5c6ffa0Smaybee 
389fa9e4066Sahrens 		mutex_enter(&zilog->zl_lock);
390fa9e4066Sahrens 		list_insert_tail(&zilog->zl_lwb_list, lwb);
391fa9e4066Sahrens 		mutex_exit(&zilog->zl_lock);
392fa9e4066Sahrens 	}
393fa9e4066Sahrens 
394d80c45e0Sbonwick 	/*
395d80c45e0Sbonwick 	 * If we just allocated the first log block, commit our transaction
396d80c45e0Sbonwick 	 * and wait for zil_sync() to stuff the block poiner into zh_log.
397d80c45e0Sbonwick 	 * (zh is part of the MOS, so we cannot modify it in open context.)
398d80c45e0Sbonwick 	 */
399d80c45e0Sbonwick 	if (tx != NULL) {
400d80c45e0Sbonwick 		dmu_tx_commit(tx);
40113f5297eSperrin 		txg_wait_synced(zilog->zl_dmu_pool, txg);
402d80c45e0Sbonwick 	}
403d80c45e0Sbonwick 
404d80c45e0Sbonwick 	ASSERT(bcmp(&blk, &zh->zh_log, sizeof (blk)) == 0);
405fa9e4066Sahrens }
406fa9e4066Sahrens 
407fa9e4066Sahrens /*
408fa9e4066Sahrens  * In one tx, free all log blocks and clear the log header.
409d80c45e0Sbonwick  * If keep_first is set, then we're replaying a log with no content.
410d80c45e0Sbonwick  * We want to keep the first block, however, so that the first
411d80c45e0Sbonwick  * synchronous transaction doesn't require a txg_wait_synced()
412d80c45e0Sbonwick  * in zil_create().  We don't need to txg_wait_synced() here either
413d80c45e0Sbonwick  * when keep_first is set, because both zil_create() and zil_destroy()
414d80c45e0Sbonwick  * will wait for any in-progress destroys to complete.
415fa9e4066Sahrens  */
416fa9e4066Sahrens void
417d80c45e0Sbonwick zil_destroy(zilog_t *zilog, boolean_t keep_first)
418fa9e4066Sahrens {
419d80c45e0Sbonwick 	const zil_header_t *zh = zilog->zl_header;
420d80c45e0Sbonwick 	lwb_t *lwb;
421fa9e4066Sahrens 	dmu_tx_t *tx;
422fa9e4066Sahrens 	uint64_t txg;
423fa9e4066Sahrens 
424d80c45e0Sbonwick 	/*
425d80c45e0Sbonwick 	 * Wait for any previous destroy to complete.
426d80c45e0Sbonwick 	 */
427d80c45e0Sbonwick 	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
428fa9e4066Sahrens 
429d80c45e0Sbonwick 	if (BP_IS_HOLE(&zh->zh_log))
430fa9e4066Sahrens 		return;
431fa9e4066Sahrens 
432fa9e4066Sahrens 	tx = dmu_tx_create(zilog->zl_os);
433fa9e4066Sahrens 	(void) dmu_tx_assign(tx, TXG_WAIT);
434fa9e4066Sahrens 	dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
435fa9e4066Sahrens 	txg = dmu_tx_get_txg(tx);
436fa9e4066Sahrens 
437d80c45e0Sbonwick 	mutex_enter(&zilog->zl_lock);
438d80c45e0Sbonwick 
439587c2a35Sperrin 	/*
440587c2a35Sperrin 	 * It is possible for the ZIL to get the previously mounted zilog
441587c2a35Sperrin 	 * structure of the same dataset if quickly remounted and the dbuf
442587c2a35Sperrin 	 * eviction has not completed. In this case we can see a non
443587c2a35Sperrin 	 * empty lwb list and keep_first will be set. We fix this by
444587c2a35Sperrin 	 * clearing the keep_first. This will be slower but it's very rare.
445587c2a35Sperrin 	 */
446587c2a35Sperrin 	if (!list_is_empty(&zilog->zl_lwb_list) && keep_first)
447587c2a35Sperrin 		keep_first = B_FALSE;
448587c2a35Sperrin 
449d80c45e0Sbonwick 	ASSERT3U(zilog->zl_destroy_txg, <, txg);
450fa9e4066Sahrens 	zilog->zl_destroy_txg = txg;
451d80c45e0Sbonwick 	zilog->zl_keep_first = keep_first;
452d80c45e0Sbonwick 
453d80c45e0Sbonwick 	if (!list_is_empty(&zilog->zl_lwb_list)) {
454d80c45e0Sbonwick 		ASSERT(zh->zh_claim_txg == 0);
455d80c45e0Sbonwick 		ASSERT(!keep_first);
456d80c45e0Sbonwick 		while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
457d80c45e0Sbonwick 			list_remove(&zilog->zl_lwb_list, lwb);
458d80c45e0Sbonwick 			if (lwb->lwb_buf != NULL)
459d80c45e0Sbonwick 				zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
460d80c45e0Sbonwick 			zio_free_blk(zilog->zl_spa, &lwb->lwb_blk, txg);
461d80c45e0Sbonwick 			kmem_cache_free(zil_lwb_cache, lwb);
462d80c45e0Sbonwick 		}
463d80c45e0Sbonwick 	} else {
464d80c45e0Sbonwick 		if (!keep_first) {
465d80c45e0Sbonwick 			(void) zil_parse(zilog, zil_free_log_block,
466d80c45e0Sbonwick 			    zil_free_log_record, tx, zh->zh_claim_txg);
467d80c45e0Sbonwick 		}
468d80c45e0Sbonwick 	}
469b19a79ecSperrin 	mutex_exit(&zilog->zl_lock);
470fa9e4066Sahrens 
471fa9e4066Sahrens 	dmu_tx_commit(tx);
472fa9e4066Sahrens }
473fa9e4066Sahrens 
4743589c4f0SNeil Perrin /*
4753589c4f0SNeil Perrin  * return true if the initial log block is not valid
4763589c4f0SNeil Perrin  */
4773589c4f0SNeil Perrin static boolean_t
4783589c4f0SNeil Perrin zil_empty(zilog_t *zilog)
4793589c4f0SNeil Perrin {
4803589c4f0SNeil Perrin 	const zil_header_t *zh = zilog->zl_header;
4813589c4f0SNeil Perrin 	arc_buf_t *abuf = NULL;
4823589c4f0SNeil Perrin 
4833589c4f0SNeil Perrin 	if (BP_IS_HOLE(&zh->zh_log))
4843589c4f0SNeil Perrin 		return (B_TRUE);
4853589c4f0SNeil Perrin 
4863589c4f0SNeil Perrin 	if (zil_read_log_block(zilog, &zh->zh_log, &abuf) != 0)
4873589c4f0SNeil Perrin 		return (B_TRUE);
4883589c4f0SNeil Perrin 
4893589c4f0SNeil Perrin 	VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
4903589c4f0SNeil Perrin 	return (B_FALSE);
4913589c4f0SNeil Perrin }
4923589c4f0SNeil Perrin 
4931d452cf5Sahrens int
494fa9e4066Sahrens zil_claim(char *osname, void *txarg)
495fa9e4066Sahrens {
496fa9e4066Sahrens 	dmu_tx_t *tx = txarg;
497fa9e4066Sahrens 	uint64_t first_txg = dmu_tx_get_txg(tx);
498fa9e4066Sahrens 	zilog_t *zilog;
499fa9e4066Sahrens 	zil_header_t *zh;
500fa9e4066Sahrens 	objset_t *os;
501fa9e4066Sahrens 	int error;
502fa9e4066Sahrens 
503503ad85cSMatthew Ahrens 	error = dmu_objset_hold(osname, FTAG, &os);
504fa9e4066Sahrens 	if (error) {
505b87f3af3Sperrin 		cmn_err(CE_WARN, "can't open objset for %s", osname);
5061d452cf5Sahrens 		return (0);
507fa9e4066Sahrens 	}
508fa9e4066Sahrens 
509fa9e4066Sahrens 	zilog = dmu_objset_zil(os);
510d80c45e0Sbonwick 	zh = zil_header_in_syncing_context(zilog);
511fa9e4066Sahrens 
512e6ca193dSGeorge Wilson 	if (zilog->zl_spa->spa_log_state == SPA_LOG_CLEAR) {
513e6ca193dSGeorge Wilson 		if (!BP_IS_HOLE(&zh->zh_log))
514e6ca193dSGeorge Wilson 			zio_free_blk(zilog->zl_spa, &zh->zh_log, first_txg);
515e6ca193dSGeorge Wilson 		BP_ZERO(&zh->zh_log);
516e6ca193dSGeorge Wilson 		dsl_dataset_dirty(dmu_objset_ds(os), tx);
517e6ca193dSGeorge Wilson 	}
518e6ca193dSGeorge Wilson 
5193589c4f0SNeil Perrin 	/*
5203589c4f0SNeil Perrin 	 * Record here whether the zil has any records to replay.
5213589c4f0SNeil Perrin 	 * If the header block pointer is null or the block points
5223589c4f0SNeil Perrin 	 * to the stubby then we know there are no valid log records.
5233589c4f0SNeil Perrin 	 * We use the header to store this state as the the zilog gets
5243589c4f0SNeil Perrin 	 * freed later in dmu_objset_close().
5253589c4f0SNeil Perrin 	 * The flags (and the rest of the header fields) are cleared in
5263589c4f0SNeil Perrin 	 * zil_sync() as a result of a zil_destroy(), after replaying the log.
5273589c4f0SNeil Perrin 	 *
5283589c4f0SNeil Perrin 	 * Note, the intent log can be empty but still need the
5293589c4f0SNeil Perrin 	 * stubby to be claimed.
5303589c4f0SNeil Perrin 	 */
531e6ca193dSGeorge Wilson 	if (!zil_empty(zilog)) {
5323589c4f0SNeil Perrin 		zh->zh_flags |= ZIL_REPLAY_NEEDED;
533e6ca193dSGeorge Wilson 		dsl_dataset_dirty(dmu_objset_ds(os), tx);
534e6ca193dSGeorge Wilson 	}
5353589c4f0SNeil Perrin 
536fa9e4066Sahrens 	/*
537d80c45e0Sbonwick 	 * Claim all log blocks if we haven't already done so, and remember
538d80c45e0Sbonwick 	 * the highest claimed sequence number.  This ensures that if we can
539d80c45e0Sbonwick 	 * read only part of the log now (e.g. due to a missing device),
540d80c45e0Sbonwick 	 * but we can read the entire log later, we will not try to replay
541d80c45e0Sbonwick 	 * or destroy beyond the last block we successfully claimed.
542fa9e4066Sahrens 	 */
543fa9e4066Sahrens 	ASSERT3U(zh->zh_claim_txg, <=, first_txg);
544fa9e4066Sahrens 	if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) {
545fa9e4066Sahrens 		zh->zh_claim_txg = first_txg;
546d80c45e0Sbonwick 		zh->zh_claim_seq = zil_parse(zilog, zil_claim_log_block,
547d80c45e0Sbonwick 		    zil_claim_log_record, tx, first_txg);
548fa9e4066Sahrens 		dsl_dataset_dirty(dmu_objset_ds(os), tx);
549fa9e4066Sahrens 	}
550d80c45e0Sbonwick 
551fa9e4066Sahrens 	ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1));
552503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
5531d452cf5Sahrens 	return (0);
554b87f3af3Sperrin }
555b87f3af3Sperrin 
556b87f3af3Sperrin /*
557b87f3af3Sperrin  * Check the log by walking the log chain.
558b87f3af3Sperrin  * Checksum errors are ok as they indicate the end of the chain.
559b87f3af3Sperrin  * Any other error (no device or read failure) returns an error.
560b87f3af3Sperrin  */
561b87f3af3Sperrin /* ARGSUSED */
562b87f3af3Sperrin int
563b87f3af3Sperrin zil_check_log_chain(char *osname, void *txarg)
564b87f3af3Sperrin {
565b87f3af3Sperrin 	zilog_t *zilog;
566b87f3af3Sperrin 	zil_header_t *zh;
567b87f3af3Sperrin 	blkptr_t blk;
568b87f3af3Sperrin 	arc_buf_t *abuf;
569b87f3af3Sperrin 	objset_t *os;
570b87f3af3Sperrin 	char *lrbuf;
571b87f3af3Sperrin 	zil_trailer_t *ztp;
572b87f3af3Sperrin 	int error;
573b87f3af3Sperrin 
574503ad85cSMatthew Ahrens 	error = dmu_objset_hold(osname, FTAG, &os);
575b87f3af3Sperrin 	if (error) {
576b87f3af3Sperrin 		cmn_err(CE_WARN, "can't open objset for %s", osname);
577b87f3af3Sperrin 		return (0);
578b87f3af3Sperrin 	}
579b87f3af3Sperrin 
580b87f3af3Sperrin 	zilog = dmu_objset_zil(os);
581b87f3af3Sperrin 	zh = zil_header_in_syncing_context(zilog);
582b87f3af3Sperrin 	blk = zh->zh_log;
583b87f3af3Sperrin 	if (BP_IS_HOLE(&blk)) {
584503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
585b87f3af3Sperrin 		return (0); /* no chain */
586b87f3af3Sperrin 	}
587b87f3af3Sperrin 
588b87f3af3Sperrin 	for (;;) {
589b87f3af3Sperrin 		error = zil_read_log_block(zilog, &blk, &abuf);
590b87f3af3Sperrin 		if (error)
591b87f3af3Sperrin 			break;
592b87f3af3Sperrin 		lrbuf = abuf->b_data;
593b87f3af3Sperrin 		ztp = (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1;
594b87f3af3Sperrin 		blk = ztp->zit_next_blk;
595b87f3af3Sperrin 		VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
596b87f3af3Sperrin 	}
597503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
598b87f3af3Sperrin 	if (error == ECKSUM)
599b87f3af3Sperrin 		return (0); /* normal end of chain */
600b87f3af3Sperrin 	return (error);
601b87f3af3Sperrin }
602b87f3af3Sperrin 
60317f17c2dSbonwick static int
60417f17c2dSbonwick zil_vdev_compare(const void *x1, const void *x2)
60517f17c2dSbonwick {
606c0d19b33Sperrin 	uint64_t v1 = ((zil_vdev_node_t *)x1)->zv_vdev;
607c0d19b33Sperrin 	uint64_t v2 = ((zil_vdev_node_t *)x2)->zv_vdev;
60817f17c2dSbonwick 
60917f17c2dSbonwick 	if (v1 < v2)
61017f17c2dSbonwick 		return (-1);
61117f17c2dSbonwick 	if (v1 > v2)
61217f17c2dSbonwick 		return (1);
61317f17c2dSbonwick 
61417f17c2dSbonwick 	return (0);
61517f17c2dSbonwick }
61617f17c2dSbonwick 
617fa9e4066Sahrens void
61817f17c2dSbonwick zil_add_block(zilog_t *zilog, blkptr_t *bp)
619fa9e4066Sahrens {
62017f17c2dSbonwick 	avl_tree_t *t = &zilog->zl_vdev_tree;
62117f17c2dSbonwick 	avl_index_t where;
62217f17c2dSbonwick 	zil_vdev_node_t *zv, zvsearch;
62317f17c2dSbonwick 	int ndvas = BP_GET_NDVAS(bp);
62417f17c2dSbonwick 	int i;
625fa9e4066Sahrens 
626416e0cd8Sek 	if (zfs_nocacheflush)
627fa9e4066Sahrens 		return;
628fa9e4066Sahrens 
62917f17c2dSbonwick 	ASSERT(zilog->zl_writer);
63017f17c2dSbonwick 
63117f17c2dSbonwick 	/*
63217f17c2dSbonwick 	 * Even though we're zl_writer, we still need a lock because the
63317f17c2dSbonwick 	 * zl_get_data() callbacks may have dmu_sync() done callbacks
63417f17c2dSbonwick 	 * that will run concurrently.
63517f17c2dSbonwick 	 */
63617f17c2dSbonwick 	mutex_enter(&zilog->zl_vdev_lock);
63717f17c2dSbonwick 	for (i = 0; i < ndvas; i++) {
63817f17c2dSbonwick 		zvsearch.zv_vdev = DVA_GET_VDEV(&bp->blk_dva[i]);
63917f17c2dSbonwick 		if (avl_find(t, &zvsearch, &where) == NULL) {
64017f17c2dSbonwick 			zv = kmem_alloc(sizeof (*zv), KM_SLEEP);
64117f17c2dSbonwick 			zv->zv_vdev = zvsearch.zv_vdev;
64217f17c2dSbonwick 			avl_insert(t, zv, where);
64367bd71c6Sperrin 		}
64467bd71c6Sperrin 	}
64517f17c2dSbonwick 	mutex_exit(&zilog->zl_vdev_lock);
646fa9e4066Sahrens }
647fa9e4066Sahrens 
64867bd71c6Sperrin void
64967bd71c6Sperrin zil_flush_vdevs(zilog_t *zilog)
65067bd71c6Sperrin {
65167bd71c6Sperrin 	spa_t *spa = zilog->zl_spa;
65217f17c2dSbonwick 	avl_tree_t *t = &zilog->zl_vdev_tree;
65317f17c2dSbonwick 	void *cookie = NULL;
65417f17c2dSbonwick 	zil_vdev_node_t *zv;
65517f17c2dSbonwick 	zio_t *zio;
656fa9e4066Sahrens 
65767bd71c6Sperrin 	ASSERT(zilog->zl_writer);
65867bd71c6Sperrin 
65917f17c2dSbonwick 	/*
66017f17c2dSbonwick 	 * We don't need zl_vdev_lock here because we're the zl_writer,
66117f17c2dSbonwick 	 * and all zl_get_data() callbacks are done.
66217f17c2dSbonwick 	 */
66317f17c2dSbonwick 	if (avl_numnodes(t) == 0)
66417f17c2dSbonwick 		return;
66517f17c2dSbonwick 
666e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
66717f17c2dSbonwick 
668e14bb325SJeff Bonwick 	zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
669fa9e4066Sahrens 
67017f17c2dSbonwick 	while ((zv = avl_destroy_nodes(t, &cookie)) != NULL) {
67117f17c2dSbonwick 		vdev_t *vd = vdev_lookup_top(spa, zv->zv_vdev);
67217f17c2dSbonwick 		if (vd != NULL)
67317f17c2dSbonwick 			zio_flush(zio, vd);
67417f17c2dSbonwick 		kmem_free(zv, sizeof (*zv));
67567bd71c6Sperrin 	}
67617f17c2dSbonwick 
677fa9e4066Sahrens 	/*
678fa9e4066Sahrens 	 * Wait for all the flushes to complete.  Not all devices actually
679fa9e4066Sahrens 	 * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails.
680fa9e4066Sahrens 	 */
68117f17c2dSbonwick 	(void) zio_wait(zio);
68217f17c2dSbonwick 
683e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, FTAG);
684fa9e4066Sahrens }
685fa9e4066Sahrens 
686fa9e4066Sahrens /*
687fa9e4066Sahrens  * Function called when a log block write completes
688fa9e4066Sahrens  */
689fa9e4066Sahrens static void
690fa9e4066Sahrens zil_lwb_write_done(zio_t *zio)
691fa9e4066Sahrens {
692fa9e4066Sahrens 	lwb_t *lwb = zio->io_private;
693fa9e4066Sahrens 	zilog_t *zilog = lwb->lwb_zilog;
694fa9e4066Sahrens 
695e14bb325SJeff Bonwick 	ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF);
696e14bb325SJeff Bonwick 	ASSERT(BP_GET_CHECKSUM(zio->io_bp) == ZIO_CHECKSUM_ZILOG);
697e14bb325SJeff Bonwick 	ASSERT(BP_GET_TYPE(zio->io_bp) == DMU_OT_INTENT_LOG);
698e14bb325SJeff Bonwick 	ASSERT(BP_GET_LEVEL(zio->io_bp) == 0);
699e14bb325SJeff Bonwick 	ASSERT(BP_GET_BYTEORDER(zio->io_bp) == ZFS_HOST_BYTEORDER);
700e14bb325SJeff Bonwick 	ASSERT(!BP_IS_GANG(zio->io_bp));
701e14bb325SJeff Bonwick 	ASSERT(!BP_IS_HOLE(zio->io_bp));
702e14bb325SJeff Bonwick 	ASSERT(zio->io_bp->blk_fill == 0);
703e14bb325SJeff Bonwick 
704fa9e4066Sahrens 	/*
705ef0d8e11SNeil Perrin 	 * Ensure the lwb buffer pointer is cleared before releasing
706ef0d8e11SNeil Perrin 	 * the txg. If we have had an allocation failure and
707ef0d8e11SNeil Perrin 	 * the txg is waiting to sync then we want want zil_sync()
708ef0d8e11SNeil Perrin 	 * to remove the lwb so that it's not picked up as the next new
709ef0d8e11SNeil Perrin 	 * one in zil_commit_writer(). zil_sync() will only remove
710ef0d8e11SNeil Perrin 	 * the lwb if lwb_buf is null.
711fa9e4066Sahrens 	 */
712fa9e4066Sahrens 	zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
713fa9e4066Sahrens 	mutex_enter(&zilog->zl_lock);
714fa9e4066Sahrens 	lwb->lwb_buf = NULL;
7158654d025Sperrin 	if (zio->io_error)
716fa9e4066Sahrens 		zilog->zl_log_error = B_TRUE;
717ef0d8e11SNeil Perrin 
718ef0d8e11SNeil Perrin 	/*
719ef0d8e11SNeil Perrin 	 * Now that we've written this log block, we have a stable pointer
720ef0d8e11SNeil Perrin 	 * to the next block in the chain, so it's OK to let the txg in
7212672fd86SNeil Perrin 	 * which we allocated the next block sync. We still have the
7222672fd86SNeil Perrin 	 * zl_lock to ensure zil_sync doesn't kmem free the lwb.
723ef0d8e11SNeil Perrin 	 */
724ef0d8e11SNeil Perrin 	txg_rele_to_sync(&lwb->lwb_txgh);
7252672fd86SNeil Perrin 	mutex_exit(&zilog->zl_lock);
726fa9e4066Sahrens }
727fa9e4066Sahrens 
728c5c6ffa0Smaybee /*
729c5c6ffa0Smaybee  * Initialize the io for a log block.
730c5c6ffa0Smaybee  */
731c5c6ffa0Smaybee static void
732c5c6ffa0Smaybee zil_lwb_write_init(zilog_t *zilog, lwb_t *lwb)
733c5c6ffa0Smaybee {
734c5c6ffa0Smaybee 	zbookmark_t zb;
735c5c6ffa0Smaybee 
736c5c6ffa0Smaybee 	zb.zb_objset = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET];
737c5c6ffa0Smaybee 	zb.zb_object = 0;
738c5c6ffa0Smaybee 	zb.zb_level = -1;
739c5c6ffa0Smaybee 	zb.zb_blkid = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ];
740c5c6ffa0Smaybee 
741b19a79ecSperrin 	if (zilog->zl_root_zio == NULL) {
742b19a79ecSperrin 		zilog->zl_root_zio = zio_root(zilog->zl_spa, NULL, NULL,
743b19a79ecSperrin 		    ZIO_FLAG_CANFAIL);
744b19a79ecSperrin 	}
74567bd71c6Sperrin 	if (lwb->lwb_zio == NULL) {
74667bd71c6Sperrin 		lwb->lwb_zio = zio_rewrite(zilog->zl_root_zio, zilog->zl_spa,
747e6ca193dSGeorge Wilson 		    0, &lwb->lwb_blk, lwb->lwb_buf, lwb->lwb_sz,
748e6ca193dSGeorge Wilson 		    zil_lwb_write_done, lwb, ZIO_PRIORITY_LOG_WRITE,
749e6ca193dSGeorge Wilson 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &zb);
75067bd71c6Sperrin 	}
751c5c6ffa0Smaybee }
752c5c6ffa0Smaybee 
753fa9e4066Sahrens /*
754fa9e4066Sahrens  * Start a log block write and advance to the next log block.
755fa9e4066Sahrens  * Calls are serialized.
756fa9e4066Sahrens  */
757fa9e4066Sahrens static lwb_t *
758fa9e4066Sahrens zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb)
759fa9e4066Sahrens {
760fa9e4066Sahrens 	lwb_t *nlwb;
761fa9e4066Sahrens 	zil_trailer_t *ztp = (zil_trailer_t *)(lwb->lwb_buf + lwb->lwb_sz) - 1;
762d80c45e0Sbonwick 	spa_t *spa = zilog->zl_spa;
763d80c45e0Sbonwick 	blkptr_t *bp = &ztp->zit_next_blk;
764fa9e4066Sahrens 	uint64_t txg;
765fa9e4066Sahrens 	uint64_t zil_blksz;
766fa9e4066Sahrens 	int error;
767fa9e4066Sahrens 
768fa9e4066Sahrens 	ASSERT(lwb->lwb_nused <= ZIL_BLK_DATA_SZ(lwb));
769fa9e4066Sahrens 
770fa9e4066Sahrens 	/*
771fa9e4066Sahrens 	 * Allocate the next block and save its address in this block
772fa9e4066Sahrens 	 * before writing it in order to establish the log chain.
773fa9e4066Sahrens 	 * Note that if the allocation of nlwb synced before we wrote
774fa9e4066Sahrens 	 * the block that points at it (lwb), we'd leak it if we crashed.
775fa9e4066Sahrens 	 * Therefore, we don't do txg_rele_to_sync() until zil_lwb_write_done().
776fa9e4066Sahrens 	 */
777fa9e4066Sahrens 	txg = txg_hold_open(zilog->zl_dmu_pool, &lwb->lwb_txgh);
778fa9e4066Sahrens 	txg_rele_to_quiesce(&lwb->lwb_txgh);
779fa9e4066Sahrens 
780fa9e4066Sahrens 	/*
78122ac5be4Sperrin 	 * Pick a ZIL blocksize. We request a size that is the
78222ac5be4Sperrin 	 * maximum of the previous used size, the current used size and
78322ac5be4Sperrin 	 * the amount waiting in the queue.
784fa9e4066Sahrens 	 */
785c5c6ffa0Smaybee 	zil_blksz = MAX(zilog->zl_prev_used,
786c5c6ffa0Smaybee 	    zilog->zl_cur_used + sizeof (*ztp));
78722ac5be4Sperrin 	zil_blksz = MAX(zil_blksz, zilog->zl_itx_list_sz + sizeof (*ztp));
788b4d654b0Sperrin 	zil_blksz = P2ROUNDUP_TYPED(zil_blksz, ZIL_MIN_BLKSZ, uint64_t);
78922ac5be4Sperrin 	if (zil_blksz > ZIL_MAX_BLKSZ)
79022ac5be4Sperrin 		zil_blksz = ZIL_MAX_BLKSZ;
791fa9e4066Sahrens 
79267bd71c6Sperrin 	BP_ZERO(bp);
79367bd71c6Sperrin 	/* pass the old blkptr in order to spread log blocks across devs */
794*e09fa4daSNeil Perrin 	error = zio_alloc_blk(spa, zil_blksz, bp, &lwb->lwb_blk, txg,
795*e09fa4daSNeil Perrin 	    zilog->zl_logbias != ZFS_LOGBIAS_LATENCY);
796fa9e4066Sahrens 	if (error) {
797d63d470bSgw 		dmu_tx_t *tx = dmu_tx_create_assigned(zilog->zl_dmu_pool, txg);
798d63d470bSgw 
799d63d470bSgw 		/*
800d63d470bSgw 		 * We dirty the dataset to ensure that zil_sync() will
801d63d470bSgw 		 * be called to remove this lwb from our zl_lwb_list.
802d63d470bSgw 		 * Failing to do so, may leave an lwb with a NULL lwb_buf
803d63d470bSgw 		 * hanging around on the zl_lwb_list.
804d63d470bSgw 		 */
805d63d470bSgw 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
806aeb1c1b6Sgw 		dmu_tx_commit(tx);
807d63d470bSgw 
808d63d470bSgw 		/*
809d63d470bSgw 		 * Since we've just experienced an allocation failure so we
810d63d470bSgw 		 * terminate the current lwb and send it on its way.
811d63d470bSgw 		 */
812d63d470bSgw 		ztp->zit_pad = 0;
813d63d470bSgw 		ztp->zit_nused = lwb->lwb_nused;
814d63d470bSgw 		ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum;
815d63d470bSgw 		zio_nowait(lwb->lwb_zio);
816d63d470bSgw 
817ea8dc4b6Seschrock 		/*
818ea8dc4b6Seschrock 		 * By returning NULL the caller will call tx_wait_synced()
819ea8dc4b6Seschrock 		 */
820fa9e4066Sahrens 		return (NULL);
821fa9e4066Sahrens 	}
822fa9e4066Sahrens 
823d80c45e0Sbonwick 	ASSERT3U(bp->blk_birth, ==, txg);
824ea8dc4b6Seschrock 	ztp->zit_pad = 0;
825fa9e4066Sahrens 	ztp->zit_nused = lwb->lwb_nused;
826fa9e4066Sahrens 	ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum;
827d80c45e0Sbonwick 	bp->blk_cksum = lwb->lwb_blk.blk_cksum;
828d80c45e0Sbonwick 	bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++;
829fa9e4066Sahrens 
830fa9e4066Sahrens 	/*
831fa9e4066Sahrens 	 * Allocate a new log write buffer (lwb).
832fa9e4066Sahrens 	 */
833fa9e4066Sahrens 	nlwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP);
834fa9e4066Sahrens 
835fa9e4066Sahrens 	nlwb->lwb_zilog = zilog;
836d80c45e0Sbonwick 	nlwb->lwb_blk = *bp;
837fa9e4066Sahrens 	nlwb->lwb_nused = 0;
838fa9e4066Sahrens 	nlwb->lwb_sz = BP_GET_LSIZE(&nlwb->lwb_blk);
839fa9e4066Sahrens 	nlwb->lwb_buf = zio_buf_alloc(nlwb->lwb_sz);
840fa9e4066Sahrens 	nlwb->lwb_max_txg = txg;
841c5c6ffa0Smaybee 	nlwb->lwb_zio = NULL;
842fa9e4066Sahrens 
843fa9e4066Sahrens 	/*
84467bd71c6Sperrin 	 * Put new lwb at the end of the log chain
845fa9e4066Sahrens 	 */
846fa9e4066Sahrens 	mutex_enter(&zilog->zl_lock);
847fa9e4066Sahrens 	list_insert_tail(&zilog->zl_lwb_list, nlwb);
848fa9e4066Sahrens 	mutex_exit(&zilog->zl_lock);
849fa9e4066Sahrens 
85017f17c2dSbonwick 	/* Record the block for later vdev flushing */
85117f17c2dSbonwick 	zil_add_block(zilog, &lwb->lwb_blk);
85267bd71c6Sperrin 
853fa9e4066Sahrens 	/*
854c5c6ffa0Smaybee 	 * kick off the write for the old log block
855fa9e4066Sahrens 	 */
856c5c6ffa0Smaybee 	dprintf_bp(&lwb->lwb_blk, "lwb %p txg %llu: ", lwb, txg);
85767bd71c6Sperrin 	ASSERT(lwb->lwb_zio);
858c5c6ffa0Smaybee 	zio_nowait(lwb->lwb_zio);
859fa9e4066Sahrens 
860fa9e4066Sahrens 	return (nlwb);
861fa9e4066Sahrens }
862fa9e4066Sahrens 
863fa9e4066Sahrens static lwb_t *
864fa9e4066Sahrens zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb)
865fa9e4066Sahrens {
866fa9e4066Sahrens 	lr_t *lrc = &itx->itx_lr; /* common log record */
867c5c6ffa0Smaybee 	lr_write_t *lr = (lr_write_t *)lrc;
868fa9e4066Sahrens 	uint64_t txg = lrc->lrc_txg;
869fa9e4066Sahrens 	uint64_t reclen = lrc->lrc_reclen;
870c5c6ffa0Smaybee 	uint64_t dlen;
871fa9e4066Sahrens 
872fa9e4066Sahrens 	if (lwb == NULL)
873fa9e4066Sahrens 		return (NULL);
874fa9e4066Sahrens 	ASSERT(lwb->lwb_buf != NULL);
875fa9e4066Sahrens 
876c5c6ffa0Smaybee 	if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY)
877c5c6ffa0Smaybee 		dlen = P2ROUNDUP_TYPED(
878c5c6ffa0Smaybee 		    lr->lr_length, sizeof (uint64_t), uint64_t);
879c5c6ffa0Smaybee 	else
880c5c6ffa0Smaybee 		dlen = 0;
881fa9e4066Sahrens 
882104e2ed7Sperrin 	zilog->zl_cur_used += (reclen + dlen);
88322ac5be4Sperrin 
88467bd71c6Sperrin 	zil_lwb_write_init(zilog, lwb);
88567bd71c6Sperrin 
886fa9e4066Sahrens 	/*
887fa9e4066Sahrens 	 * If this record won't fit in the current log block, start a new one.
888fa9e4066Sahrens 	 */
889104e2ed7Sperrin 	if (lwb->lwb_nused + reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) {
890fa9e4066Sahrens 		lwb = zil_lwb_write_start(zilog, lwb);
891c5c6ffa0Smaybee 		if (lwb == NULL)
892fa9e4066Sahrens 			return (NULL);
89367bd71c6Sperrin 		zil_lwb_write_init(zilog, lwb);
894ea8dc4b6Seschrock 		ASSERT(lwb->lwb_nused == 0);
895104e2ed7Sperrin 		if (reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) {
896fa9e4066Sahrens 			txg_wait_synced(zilog->zl_dmu_pool, txg);
897fa9e4066Sahrens 			return (lwb);
898fa9e4066Sahrens 		}
899fa9e4066Sahrens 	}
900fa9e4066Sahrens 
901b19a79ecSperrin 	/*
902b19a79ecSperrin 	 * Update the lrc_seq, to be log record sequence number. See zil.h
903b19a79ecSperrin 	 * Then copy the record to the log buffer.
904b19a79ecSperrin 	 */
905b19a79ecSperrin 	lrc->lrc_seq = ++zilog->zl_lr_seq; /* we are single threaded */
906fa9e4066Sahrens 	bcopy(lrc, lwb->lwb_buf + lwb->lwb_nused, reclen);
907c5c6ffa0Smaybee 
908c5c6ffa0Smaybee 	/*
909c5c6ffa0Smaybee 	 * If it's a write, fetch the data or get its blkptr as appropriate.
910c5c6ffa0Smaybee 	 */
911c5c6ffa0Smaybee 	if (lrc->lrc_txtype == TX_WRITE) {
912c5c6ffa0Smaybee 		if (txg > spa_freeze_txg(zilog->zl_spa))
913c5c6ffa0Smaybee 			txg_wait_synced(zilog->zl_dmu_pool, txg);
914c5c6ffa0Smaybee 		if (itx->itx_wr_state != WR_COPIED) {
915c5c6ffa0Smaybee 			char *dbuf;
916c5c6ffa0Smaybee 			int error;
917c5c6ffa0Smaybee 
918c5c6ffa0Smaybee 			/* alignment is guaranteed */
919c5c6ffa0Smaybee 			lr = (lr_write_t *)(lwb->lwb_buf + lwb->lwb_nused);
920c5c6ffa0Smaybee 			if (dlen) {
921c5c6ffa0Smaybee 				ASSERT(itx->itx_wr_state == WR_NEED_COPY);
922c5c6ffa0Smaybee 				dbuf = lwb->lwb_buf + lwb->lwb_nused + reclen;
923c5c6ffa0Smaybee 				lr->lr_common.lrc_reclen += dlen;
924c5c6ffa0Smaybee 			} else {
925c5c6ffa0Smaybee 				ASSERT(itx->itx_wr_state == WR_INDIRECT);
926c5c6ffa0Smaybee 				dbuf = NULL;
927c5c6ffa0Smaybee 			}
928c5c6ffa0Smaybee 			error = zilog->zl_get_data(
929c5c6ffa0Smaybee 			    itx->itx_private, lr, dbuf, lwb->lwb_zio);
930c87b8fc5SMark J Musante 			if (error == EIO) {
931c87b8fc5SMark J Musante 				txg_wait_synced(zilog->zl_dmu_pool, txg);
932c87b8fc5SMark J Musante 				return (lwb);
933c87b8fc5SMark J Musante 			}
934c5c6ffa0Smaybee 			if (error) {
935c5c6ffa0Smaybee 				ASSERT(error == ENOENT || error == EEXIST ||
936c5c6ffa0Smaybee 				    error == EALREADY);
937c5c6ffa0Smaybee 				return (lwb);
938c5c6ffa0Smaybee 			}
939c5c6ffa0Smaybee 		}
940104e2ed7Sperrin 	}
941c5c6ffa0Smaybee 
942c5c6ffa0Smaybee 	lwb->lwb_nused += reclen + dlen;
943fa9e4066Sahrens 	lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg);
944fa9e4066Sahrens 	ASSERT3U(lwb->lwb_nused, <=, ZIL_BLK_DATA_SZ(lwb));
945fa9e4066Sahrens 	ASSERT3U(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)), ==, 0);
946fa9e4066Sahrens 
947fa9e4066Sahrens 	return (lwb);
948fa9e4066Sahrens }
949fa9e4066Sahrens 
950fa9e4066Sahrens itx_t *
951da6c28aaSamw zil_itx_create(uint64_t txtype, size_t lrsize)
952fa9e4066Sahrens {
953fa9e4066Sahrens 	itx_t *itx;
954fa9e4066Sahrens 
955b4d654b0Sperrin 	lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t);
956fa9e4066Sahrens 
957fa9e4066Sahrens 	itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP);
958fa9e4066Sahrens 	itx->itx_lr.lrc_txtype = txtype;
959fa9e4066Sahrens 	itx->itx_lr.lrc_reclen = lrsize;
960abf76b6eSperrin 	itx->itx_sod = lrsize; /* if write & WR_NEED_COPY will be increased */
961fa9e4066Sahrens 	itx->itx_lr.lrc_seq = 0;	/* defensive */
962fa9e4066Sahrens 
963fa9e4066Sahrens 	return (itx);
964fa9e4066Sahrens }
965fa9e4066Sahrens 
966fa9e4066Sahrens uint64_t
967fa9e4066Sahrens zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx)
968fa9e4066Sahrens {
969fa9e4066Sahrens 	uint64_t seq;
970fa9e4066Sahrens 
971fa9e4066Sahrens 	ASSERT(itx->itx_lr.lrc_seq == 0);
972fa9e4066Sahrens 
973fa9e4066Sahrens 	mutex_enter(&zilog->zl_lock);
974fa9e4066Sahrens 	list_insert_tail(&zilog->zl_itx_list, itx);
975abf76b6eSperrin 	zilog->zl_itx_list_sz += itx->itx_sod;
976fa9e4066Sahrens 	itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx);
977fa9e4066Sahrens 	itx->itx_lr.lrc_seq = seq = ++zilog->zl_itx_seq;
978fa9e4066Sahrens 	mutex_exit(&zilog->zl_lock);
979fa9e4066Sahrens 
980fa9e4066Sahrens 	return (seq);
981fa9e4066Sahrens }
982fa9e4066Sahrens 
983fa9e4066Sahrens /*
984fa9e4066Sahrens  * Free up all in-memory intent log transactions that have now been synced.
985fa9e4066Sahrens  */
986fa9e4066Sahrens static void
987fa9e4066Sahrens zil_itx_clean(zilog_t *zilog)
988fa9e4066Sahrens {
989fa9e4066Sahrens 	uint64_t synced_txg = spa_last_synced_txg(zilog->zl_spa);
990fa9e4066Sahrens 	uint64_t freeze_txg = spa_freeze_txg(zilog->zl_spa);
991a584ef65Sjohansen 	list_t clean_list;
992fa9e4066Sahrens 	itx_t *itx;
993fa9e4066Sahrens 
994a584ef65Sjohansen 	list_create(&clean_list, sizeof (itx_t), offsetof(itx_t, itx_node));
995a584ef65Sjohansen 
996fa9e4066Sahrens 	mutex_enter(&zilog->zl_lock);
997b19a79ecSperrin 	/* wait for a log writer to finish walking list */
998b19a79ecSperrin 	while (zilog->zl_writer) {
999b19a79ecSperrin 		cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
1000b19a79ecSperrin 	}
1001a584ef65Sjohansen 
1002a584ef65Sjohansen 	/*
1003a584ef65Sjohansen 	 * Move the sync'd log transactions to a separate list so we can call
1004a584ef65Sjohansen 	 * kmem_free without holding the zl_lock.
1005a584ef65Sjohansen 	 *
1006a584ef65Sjohansen 	 * There is no need to set zl_writer as we don't drop zl_lock here
1007a584ef65Sjohansen 	 */
1008fa9e4066Sahrens 	while ((itx = list_head(&zilog->zl_itx_list)) != NULL &&
1009fa9e4066Sahrens 	    itx->itx_lr.lrc_txg <= MIN(synced_txg, freeze_txg)) {
1010fa9e4066Sahrens 		list_remove(&zilog->zl_itx_list, itx);
1011abf76b6eSperrin 		zilog->zl_itx_list_sz -= itx->itx_sod;
1012a584ef65Sjohansen 		list_insert_tail(&clean_list, itx);
1013fa9e4066Sahrens 	}
101467bd71c6Sperrin 	cv_broadcast(&zilog->zl_cv_writer);
1015fa9e4066Sahrens 	mutex_exit(&zilog->zl_lock);
1016a584ef65Sjohansen 
1017a584ef65Sjohansen 	/* destroy sync'd log transactions */
1018a584ef65Sjohansen 	while ((itx = list_head(&clean_list)) != NULL) {
1019a584ef65Sjohansen 		list_remove(&clean_list, itx);
1020a584ef65Sjohansen 		kmem_free(itx, offsetof(itx_t, itx_lr)
1021a584ef65Sjohansen 		    + itx->itx_lr.lrc_reclen);
1022a584ef65Sjohansen 	}
1023a584ef65Sjohansen 	list_destroy(&clean_list);
1024fa9e4066Sahrens }
1025fa9e4066Sahrens 
1026b19a79ecSperrin /*
102767bd71c6Sperrin  * If there are any in-memory intent log transactions which have now been
102867bd71c6Sperrin  * synced then start up a taskq to free them.
1029b19a79ecSperrin  */
1030fa9e4066Sahrens void
1031fa9e4066Sahrens zil_clean(zilog_t *zilog)
1032fa9e4066Sahrens {
103367bd71c6Sperrin 	itx_t *itx;
103467bd71c6Sperrin 
1035fa9e4066Sahrens 	mutex_enter(&zilog->zl_lock);
103667bd71c6Sperrin 	itx = list_head(&zilog->zl_itx_list);
103767bd71c6Sperrin 	if ((itx != NULL) &&
103867bd71c6Sperrin 	    (itx->itx_lr.lrc_txg <= spa_last_synced_txg(zilog->zl_spa))) {
1039fa9e4066Sahrens 		(void) taskq_dispatch(zilog->zl_clean_taskq,
10409d3574bfSNeil Perrin 		    (task_func_t *)zil_itx_clean, zilog, TQ_SLEEP);
104167bd71c6Sperrin 	}
1042fa9e4066Sahrens 	mutex_exit(&zilog->zl_lock);
1043fa9e4066Sahrens }
1044fa9e4066Sahrens 
1045e14bb325SJeff Bonwick static void
1046b19a79ecSperrin zil_commit_writer(zilog_t *zilog, uint64_t seq, uint64_t foid)
1047fa9e4066Sahrens {
1048fa9e4066Sahrens 	uint64_t txg;
104967bd71c6Sperrin 	uint64_t commit_seq = 0;
1050b19a79ecSperrin 	itx_t *itx, *itx_next = (itx_t *)-1;
1051fa9e4066Sahrens 	lwb_t *lwb;
1052fa9e4066Sahrens 	spa_t *spa;
1053fa9e4066Sahrens 
1054fa9e4066Sahrens 	zilog->zl_writer = B_TRUE;
1055e14bb325SJeff Bonwick 	ASSERT(zilog->zl_root_zio == NULL);
1056b19a79ecSperrin 	spa = zilog->zl_spa;
1057fa9e4066Sahrens 
1058fa9e4066Sahrens 	if (zilog->zl_suspend) {
1059fa9e4066Sahrens 		lwb = NULL;
1060fa9e4066Sahrens 	} else {
1061fa9e4066Sahrens 		lwb = list_tail(&zilog->zl_lwb_list);
1062fa9e4066Sahrens 		if (lwb == NULL) {
1063b19a79ecSperrin 			/*
1064b19a79ecSperrin 			 * Return if there's nothing to flush before we
1065b19a79ecSperrin 			 * dirty the fs by calling zil_create()
1066b19a79ecSperrin 			 */
1067b19a79ecSperrin 			if (list_is_empty(&zilog->zl_itx_list)) {
1068b19a79ecSperrin 				zilog->zl_writer = B_FALSE;
1069b19a79ecSperrin 				return;
1070b19a79ecSperrin 			}
1071fa9e4066Sahrens 			mutex_exit(&zilog->zl_lock);
1072fa9e4066Sahrens 			zil_create(zilog);
1073fa9e4066Sahrens 			mutex_enter(&zilog->zl_lock);
1074fa9e4066Sahrens 			lwb = list_tail(&zilog->zl_lwb_list);
1075fa9e4066Sahrens 		}
1076fa9e4066Sahrens 	}
1077fa9e4066Sahrens 
107867bd71c6Sperrin 	/* Loop through in-memory log transactions filling log blocks. */
1079b19a79ecSperrin 	DTRACE_PROBE1(zil__cw1, zilog_t *, zilog);
1080fa9e4066Sahrens 	for (;;) {
1081b19a79ecSperrin 		/*
1082b19a79ecSperrin 		 * Find the next itx to push:
1083b19a79ecSperrin 		 * Push all transactions related to specified foid and all
1084b19a79ecSperrin 		 * other transactions except TX_WRITE, TX_TRUNCATE,
1085b19a79ecSperrin 		 * TX_SETATTR and TX_ACL for all other files.
1086b19a79ecSperrin 		 */
1087b19a79ecSperrin 		if (itx_next != (itx_t *)-1)
1088b19a79ecSperrin 			itx = itx_next;
1089b19a79ecSperrin 		else
1090b19a79ecSperrin 			itx = list_head(&zilog->zl_itx_list);
1091b19a79ecSperrin 		for (; itx != NULL; itx = list_next(&zilog->zl_itx_list, itx)) {
1092b19a79ecSperrin 			if (foid == 0) /* push all foids? */
1093b19a79ecSperrin 				break;
109467bd71c6Sperrin 			if (itx->itx_sync) /* push all O_[D]SYNC */
109567bd71c6Sperrin 				break;
1096b19a79ecSperrin 			switch (itx->itx_lr.lrc_txtype) {
1097b19a79ecSperrin 			case TX_SETATTR:
1098b19a79ecSperrin 			case TX_WRITE:
1099b19a79ecSperrin 			case TX_TRUNCATE:
1100b19a79ecSperrin 			case TX_ACL:
1101b19a79ecSperrin 				/* lr_foid is same offset for these records */
1102b19a79ecSperrin 				if (((lr_write_t *)&itx->itx_lr)->lr_foid
1103b19a79ecSperrin 				    != foid) {
1104b19a79ecSperrin 					continue; /* skip this record */
1105b19a79ecSperrin 				}
1106b19a79ecSperrin 			}
1107b19a79ecSperrin 			break;
1108b19a79ecSperrin 		}
1109fa9e4066Sahrens 		if (itx == NULL)
1110fa9e4066Sahrens 			break;
1111fa9e4066Sahrens 
1112fa9e4066Sahrens 		if ((itx->itx_lr.lrc_seq > seq) &&
1113b19a79ecSperrin 		    ((lwb == NULL) || (lwb->lwb_nused == 0) ||
1114abf76b6eSperrin 		    (lwb->lwb_nused + itx->itx_sod > ZIL_BLK_DATA_SZ(lwb)))) {
1115fa9e4066Sahrens 			break;
111667bd71c6Sperrin 		}
1117fa9e4066Sahrens 
1118b19a79ecSperrin 		/*
1119b19a79ecSperrin 		 * Save the next pointer.  Even though we soon drop
1120b19a79ecSperrin 		 * zl_lock all threads that may change the list
1121b19a79ecSperrin 		 * (another writer or zil_itx_clean) can't do so until
1122b19a79ecSperrin 		 * they have zl_writer.
1123b19a79ecSperrin 		 */
1124b19a79ecSperrin 		itx_next = list_next(&zilog->zl_itx_list, itx);
1125fa9e4066Sahrens 		list_remove(&zilog->zl_itx_list, itx);
1126abf76b6eSperrin 		zilog->zl_itx_list_sz -= itx->itx_sod;
112767bd71c6Sperrin 		mutex_exit(&zilog->zl_lock);
1128fa9e4066Sahrens 		txg = itx->itx_lr.lrc_txg;
1129fa9e4066Sahrens 		ASSERT(txg);
1130fa9e4066Sahrens 
1131fa9e4066Sahrens 		if (txg > spa_last_synced_txg(spa) ||
1132fa9e4066Sahrens 		    txg > spa_freeze_txg(spa))
1133fa9e4066Sahrens 			lwb = zil_lwb_commit(zilog, itx, lwb);
1134fa9e4066Sahrens 		kmem_free(itx, offsetof(itx_t, itx_lr)
1135fa9e4066Sahrens 		    + itx->itx_lr.lrc_reclen);
1136fa9e4066Sahrens 		mutex_enter(&zilog->zl_lock);
1137fa9e4066Sahrens 	}
1138b19a79ecSperrin 	DTRACE_PROBE1(zil__cw2, zilog_t *, zilog);
113967bd71c6Sperrin 	/* determine commit sequence number */
114067bd71c6Sperrin 	itx = list_head(&zilog->zl_itx_list);
114167bd71c6Sperrin 	if (itx)
114267bd71c6Sperrin 		commit_seq = itx->itx_lr.lrc_seq;
114367bd71c6Sperrin 	else
114467bd71c6Sperrin 		commit_seq = zilog->zl_itx_seq;
1145fa9e4066Sahrens 	mutex_exit(&zilog->zl_lock);
1146fa9e4066Sahrens 
1147fa9e4066Sahrens 	/* write the last block out */
114867bd71c6Sperrin 	if (lwb != NULL && lwb->lwb_zio != NULL)
1149fa9e4066Sahrens 		lwb = zil_lwb_write_start(zilog, lwb);
1150fa9e4066Sahrens 
115122ac5be4Sperrin 	zilog->zl_prev_used = zilog->zl_cur_used;
115222ac5be4Sperrin 	zilog->zl_cur_used = 0;
1153fa9e4066Sahrens 
1154fa9e4066Sahrens 	/*
1155b19a79ecSperrin 	 * Wait if necessary for the log blocks to be on stable storage.
1156fa9e4066Sahrens 	 */
1157b19a79ecSperrin 	if (zilog->zl_root_zio) {
1158b19a79ecSperrin 		DTRACE_PROBE1(zil__cw3, zilog_t *, zilog);
1159b19a79ecSperrin 		(void) zio_wait(zilog->zl_root_zio);
1160e14bb325SJeff Bonwick 		zilog->zl_root_zio = NULL;
1161b19a79ecSperrin 		DTRACE_PROBE1(zil__cw4, zilog_t *, zilog);
116217f17c2dSbonwick 		zil_flush_vdevs(zilog);
1163fa9e4066Sahrens 	}
116422ac5be4Sperrin 
1165fa9e4066Sahrens 	if (zilog->zl_log_error || lwb == NULL) {
1166fa9e4066Sahrens 		zilog->zl_log_error = 0;
1167fa9e4066Sahrens 		txg_wait_synced(zilog->zl_dmu_pool, 0);
1168fa9e4066Sahrens 	}
116967bd71c6Sperrin 
117067bd71c6Sperrin 	mutex_enter(&zilog->zl_lock);
117122ac5be4Sperrin 	zilog->zl_writer = B_FALSE;
117267bd71c6Sperrin 
117367bd71c6Sperrin 	ASSERT3U(commit_seq, >=, zilog->zl_commit_seq);
117467bd71c6Sperrin 	zilog->zl_commit_seq = commit_seq;
1175b19a79ecSperrin }
1176b19a79ecSperrin 
1177b19a79ecSperrin /*
1178b19a79ecSperrin  * Push zfs transactions to stable storage up to the supplied sequence number.
1179b19a79ecSperrin  * If foid is 0 push out all transactions, otherwise push only those
1180b19a79ecSperrin  * for that file or might have been used to create that file.
1181b19a79ecSperrin  */
1182b19a79ecSperrin void
1183b19a79ecSperrin zil_commit(zilog_t *zilog, uint64_t seq, uint64_t foid)
1184b19a79ecSperrin {
1185b19a79ecSperrin 	if (zilog == NULL || seq == 0)
1186b19a79ecSperrin 		return;
1187b19a79ecSperrin 
1188b19a79ecSperrin 	mutex_enter(&zilog->zl_lock);
1189b19a79ecSperrin 
1190b19a79ecSperrin 	seq = MIN(seq, zilog->zl_itx_seq);	/* cap seq at largest itx seq */
1191b19a79ecSperrin 
119267bd71c6Sperrin 	while (zilog->zl_writer) {
1193b19a79ecSperrin 		cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
119467bd71c6Sperrin 		if (seq < zilog->zl_commit_seq) {
119567bd71c6Sperrin 			mutex_exit(&zilog->zl_lock);
119667bd71c6Sperrin 			return;
119767bd71c6Sperrin 		}
119867bd71c6Sperrin 	}
1199b19a79ecSperrin 	zil_commit_writer(zilog, seq, foid); /* drops zl_lock */
120067bd71c6Sperrin 	/* wake up others waiting on the commit */
120167bd71c6Sperrin 	cv_broadcast(&zilog->zl_cv_writer);
120267bd71c6Sperrin 	mutex_exit(&zilog->zl_lock);
1203fa9e4066Sahrens }
1204fa9e4066Sahrens 
1205fa9e4066Sahrens /*
1206fa9e4066Sahrens  * Called in syncing context to free committed log blocks and update log header.
1207fa9e4066Sahrens  */
1208fa9e4066Sahrens void
1209fa9e4066Sahrens zil_sync(zilog_t *zilog, dmu_tx_t *tx)
1210fa9e4066Sahrens {
1211d80c45e0Sbonwick 	zil_header_t *zh = zil_header_in_syncing_context(zilog);
1212fa9e4066Sahrens 	uint64_t txg = dmu_tx_get_txg(tx);
1213fa9e4066Sahrens 	spa_t *spa = zilog->zl_spa;
1214fa9e4066Sahrens 	lwb_t *lwb;
1215fa9e4066Sahrens 
121614843421SMatthew Ahrens 	/*
121714843421SMatthew Ahrens 	 * We don't zero out zl_destroy_txg, so make sure we don't try
121814843421SMatthew Ahrens 	 * to destroy it twice.
121914843421SMatthew Ahrens 	 */
122014843421SMatthew Ahrens 	if (spa_sync_pass(spa) != 1)
122114843421SMatthew Ahrens 		return;
122214843421SMatthew Ahrens 
1223d80c45e0Sbonwick 	mutex_enter(&zilog->zl_lock);
1224d80c45e0Sbonwick 
1225fa9e4066Sahrens 	ASSERT(zilog->zl_stop_sync == 0);
1226fa9e4066Sahrens 
12271209a471SNeil Perrin 	zh->zh_replay_seq = zilog->zl_replayed_seq[txg & TXG_MASK];
1228fa9e4066Sahrens 
1229fa9e4066Sahrens 	if (zilog->zl_destroy_txg == txg) {
1230d80c45e0Sbonwick 		blkptr_t blk = zh->zh_log;
1231d80c45e0Sbonwick 
1232d80c45e0Sbonwick 		ASSERT(list_head(&zilog->zl_lwb_list) == NULL);
1233d80c45e0Sbonwick 
1234d80c45e0Sbonwick 		bzero(zh, sizeof (zil_header_t));
12351209a471SNeil Perrin 		bzero(zilog->zl_replayed_seq, sizeof (zilog->zl_replayed_seq));
1236d80c45e0Sbonwick 
1237d80c45e0Sbonwick 		if (zilog->zl_keep_first) {
1238d80c45e0Sbonwick 			/*
1239d80c45e0Sbonwick 			 * If this block was part of log chain that couldn't
1240d80c45e0Sbonwick 			 * be claimed because a device was missing during
1241d80c45e0Sbonwick 			 * zil_claim(), but that device later returns,
1242d80c45e0Sbonwick 			 * then this block could erroneously appear valid.
1243d80c45e0Sbonwick 			 * To guard against this, assign a new GUID to the new
1244d80c45e0Sbonwick 			 * log chain so it doesn't matter what blk points to.
1245d80c45e0Sbonwick 			 */
1246d80c45e0Sbonwick 			zil_init_log_chain(zilog, &blk);
1247d80c45e0Sbonwick 			zh->zh_log = blk;
1248d80c45e0Sbonwick 		}
1249fa9e4066Sahrens 	}
1250fa9e4066Sahrens 
1251e6ca193dSGeorge Wilson 	while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
1252b19a79ecSperrin 		zh->zh_log = lwb->lwb_blk;
1253fa9e4066Sahrens 		if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg)
1254fa9e4066Sahrens 			break;
1255fa9e4066Sahrens 		list_remove(&zilog->zl_lwb_list, lwb);
1256fa9e4066Sahrens 		zio_free_blk(spa, &lwb->lwb_blk, txg);
1257fa9e4066Sahrens 		kmem_cache_free(zil_lwb_cache, lwb);
1258d63d470bSgw 
1259d63d470bSgw 		/*
1260d63d470bSgw 		 * If we don't have anything left in the lwb list then
1261d63d470bSgw 		 * we've had an allocation failure and we need to zero
1262d63d470bSgw 		 * out the zil_header blkptr so that we don't end
1263d63d470bSgw 		 * up freeing the same block twice.
1264d63d470bSgw 		 */
1265d63d470bSgw 		if (list_head(&zilog->zl_lwb_list) == NULL)
1266d63d470bSgw 			BP_ZERO(&zh->zh_log);
1267fa9e4066Sahrens 	}
1268fa9e4066Sahrens 	mutex_exit(&zilog->zl_lock);
1269fa9e4066Sahrens }
1270fa9e4066Sahrens 
1271fa9e4066Sahrens void
1272fa9e4066Sahrens zil_init(void)
1273fa9e4066Sahrens {
1274fa9e4066Sahrens 	zil_lwb_cache = kmem_cache_create("zil_lwb_cache",
12755ad82045Snd 	    sizeof (struct lwb), 0, NULL, NULL, NULL, NULL, NULL, 0);
1276fa9e4066Sahrens }
1277fa9e4066Sahrens 
1278fa9e4066Sahrens void
1279fa9e4066Sahrens zil_fini(void)
1280fa9e4066Sahrens {
1281fa9e4066Sahrens 	kmem_cache_destroy(zil_lwb_cache);
1282fa9e4066Sahrens }
1283fa9e4066Sahrens 
1284*e09fa4daSNeil Perrin void
1285*e09fa4daSNeil Perrin zil_set_logbias(zilog_t *zilog, uint64_t logbias)
1286*e09fa4daSNeil Perrin {
1287*e09fa4daSNeil Perrin 	zilog->zl_logbias = logbias;
1288*e09fa4daSNeil Perrin }
1289*e09fa4daSNeil Perrin 
1290fa9e4066Sahrens zilog_t *
1291fa9e4066Sahrens zil_alloc(objset_t *os, zil_header_t *zh_phys)
1292fa9e4066Sahrens {
1293fa9e4066Sahrens 	zilog_t *zilog;
1294fa9e4066Sahrens 
1295fa9e4066Sahrens 	zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP);
1296fa9e4066Sahrens 
1297fa9e4066Sahrens 	zilog->zl_header = zh_phys;
1298fa9e4066Sahrens 	zilog->zl_os = os;
1299fa9e4066Sahrens 	zilog->zl_spa = dmu_objset_spa(os);
1300fa9e4066Sahrens 	zilog->zl_dmu_pool = dmu_objset_pool(os);
1301d80c45e0Sbonwick 	zilog->zl_destroy_txg = TXG_INITIAL - 1;
1302*e09fa4daSNeil Perrin 	zilog->zl_logbias = dmu_objset_logbias(os);
1303fa9e4066Sahrens 
13045ad82045Snd 	mutex_init(&zilog->zl_lock, NULL, MUTEX_DEFAULT, NULL);
13055ad82045Snd 
1306fa9e4066Sahrens 	list_create(&zilog->zl_itx_list, sizeof (itx_t),
1307fa9e4066Sahrens 	    offsetof(itx_t, itx_node));
1308fa9e4066Sahrens 
1309fa9e4066Sahrens 	list_create(&zilog->zl_lwb_list, sizeof (lwb_t),
1310fa9e4066Sahrens 	    offsetof(lwb_t, lwb_node));
1311fa9e4066Sahrens 
131217f17c2dSbonwick 	mutex_init(&zilog->zl_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
131317f17c2dSbonwick 
131417f17c2dSbonwick 	avl_create(&zilog->zl_vdev_tree, zil_vdev_compare,
131517f17c2dSbonwick 	    sizeof (zil_vdev_node_t), offsetof(zil_vdev_node_t, zv_node));
1316fa9e4066Sahrens 
1317b7b97454Sperrin 	cv_init(&zilog->zl_cv_writer, NULL, CV_DEFAULT, NULL);
1318b7b97454Sperrin 	cv_init(&zilog->zl_cv_suspend, NULL, CV_DEFAULT, NULL);
1319b7b97454Sperrin 
1320fa9e4066Sahrens 	return (zilog);
1321fa9e4066Sahrens }
1322fa9e4066Sahrens 
1323fa9e4066Sahrens void
1324fa9e4066Sahrens zil_free(zilog_t *zilog)
1325fa9e4066Sahrens {
1326fa9e4066Sahrens 	lwb_t *lwb;
1327fa9e4066Sahrens 
1328fa9e4066Sahrens 	zilog->zl_stop_sync = 1;
1329fa9e4066Sahrens 
1330fa9e4066Sahrens 	while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
1331fa9e4066Sahrens 		list_remove(&zilog->zl_lwb_list, lwb);
1332fa9e4066Sahrens 		if (lwb->lwb_buf != NULL)
1333fa9e4066Sahrens 			zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
1334fa9e4066Sahrens 		kmem_cache_free(zil_lwb_cache, lwb);
1335fa9e4066Sahrens 	}
1336fa9e4066Sahrens 	list_destroy(&zilog->zl_lwb_list);
1337fa9e4066Sahrens 
133817f17c2dSbonwick 	avl_destroy(&zilog->zl_vdev_tree);
133917f17c2dSbonwick 	mutex_destroy(&zilog->zl_vdev_lock);
1340fa9e4066Sahrens 
1341fa9e4066Sahrens 	ASSERT(list_head(&zilog->zl_itx_list) == NULL);
1342fa9e4066Sahrens 	list_destroy(&zilog->zl_itx_list);
13435ad82045Snd 	mutex_destroy(&zilog->zl_lock);
1344fa9e4066Sahrens 
1345b7b97454Sperrin 	cv_destroy(&zilog->zl_cv_writer);
1346b7b97454Sperrin 	cv_destroy(&zilog->zl_cv_suspend);
1347b7b97454Sperrin 
1348fa9e4066Sahrens 	kmem_free(zilog, sizeof (zilog_t));
1349fa9e4066Sahrens }
1350fa9e4066Sahrens 
1351fa9e4066Sahrens /*
1352fa9e4066Sahrens  * Open an intent log.
1353fa9e4066Sahrens  */
1354fa9e4066Sahrens zilog_t *
1355fa9e4066Sahrens zil_open(objset_t *os, zil_get_data_t *get_data)
1356fa9e4066Sahrens {
1357fa9e4066Sahrens 	zilog_t *zilog = dmu_objset_zil(os);
1358fa9e4066Sahrens 
1359fa9e4066Sahrens 	zilog->zl_get_data = get_data;
1360fa9e4066Sahrens 	zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri,
1361fa9e4066Sahrens 	    2, 2, TASKQ_PREPOPULATE);
1362fa9e4066Sahrens 
1363fa9e4066Sahrens 	return (zilog);
1364fa9e4066Sahrens }
1365fa9e4066Sahrens 
1366fa9e4066Sahrens /*
1367fa9e4066Sahrens  * Close an intent log.
1368fa9e4066Sahrens  */
1369fa9e4066Sahrens void
1370fa9e4066Sahrens zil_close(zilog_t *zilog)
1371fa9e4066Sahrens {
1372d80c45e0Sbonwick 	/*
1373d80c45e0Sbonwick 	 * If the log isn't already committed, mark the objset dirty
1374d80c45e0Sbonwick 	 * (so zil_sync() will be called) and wait for that txg to sync.
1375d80c45e0Sbonwick 	 */
1376d80c45e0Sbonwick 	if (!zil_is_committed(zilog)) {
1377d80c45e0Sbonwick 		uint64_t txg;
1378d80c45e0Sbonwick 		dmu_tx_t *tx = dmu_tx_create(zilog->zl_os);
1379d80c45e0Sbonwick 		(void) dmu_tx_assign(tx, TXG_WAIT);
1380d80c45e0Sbonwick 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
1381d80c45e0Sbonwick 		txg = dmu_tx_get_txg(tx);
1382d80c45e0Sbonwick 		dmu_tx_commit(tx);
1383d80c45e0Sbonwick 		txg_wait_synced(zilog->zl_dmu_pool, txg);
1384d80c45e0Sbonwick 	}
1385d80c45e0Sbonwick 
1386fa9e4066Sahrens 	taskq_destroy(zilog->zl_clean_taskq);
1387fa9e4066Sahrens 	zilog->zl_clean_taskq = NULL;
1388fa9e4066Sahrens 	zilog->zl_get_data = NULL;
1389fa9e4066Sahrens 
1390fa9e4066Sahrens 	zil_itx_clean(zilog);
1391fa9e4066Sahrens 	ASSERT(list_head(&zilog->zl_itx_list) == NULL);
1392fa9e4066Sahrens }
1393fa9e4066Sahrens 
1394fa9e4066Sahrens /*
1395fa9e4066Sahrens  * Suspend an intent log.  While in suspended mode, we still honor
1396fa9e4066Sahrens  * synchronous semantics, but we rely on txg_wait_synced() to do it.
1397fa9e4066Sahrens  * We suspend the log briefly when taking a snapshot so that the snapshot
1398fa9e4066Sahrens  * contains all the data it's supposed to, and has an empty intent log.
1399fa9e4066Sahrens  */
1400fa9e4066Sahrens int
1401fa9e4066Sahrens zil_suspend(zilog_t *zilog)
1402fa9e4066Sahrens {
1403d80c45e0Sbonwick 	const zil_header_t *zh = zilog->zl_header;
1404fa9e4066Sahrens 
1405fa9e4066Sahrens 	mutex_enter(&zilog->zl_lock);
14063589c4f0SNeil Perrin 	if (zh->zh_flags & ZIL_REPLAY_NEEDED) {		/* unplayed log */
1407fa9e4066Sahrens 		mutex_exit(&zilog->zl_lock);
1408fa9e4066Sahrens 		return (EBUSY);
1409fa9e4066Sahrens 	}
1410d80c45e0Sbonwick 	if (zilog->zl_suspend++ != 0) {
1411d80c45e0Sbonwick 		/*
1412d80c45e0Sbonwick 		 * Someone else already began a suspend.
1413d80c45e0Sbonwick 		 * Just wait for them to finish.
1414d80c45e0Sbonwick 		 */
1415d80c45e0Sbonwick 		while (zilog->zl_suspending)
1416d80c45e0Sbonwick 			cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock);
1417d80c45e0Sbonwick 		mutex_exit(&zilog->zl_lock);
1418d80c45e0Sbonwick 		return (0);
1419d80c45e0Sbonwick 	}
1420d80c45e0Sbonwick 	zilog->zl_suspending = B_TRUE;
1421fa9e4066Sahrens 	mutex_exit(&zilog->zl_lock);
1422fa9e4066Sahrens 
1423b19a79ecSperrin 	zil_commit(zilog, UINT64_MAX, 0);
1424fa9e4066Sahrens 
1425b19a79ecSperrin 	/*
1426b19a79ecSperrin 	 * Wait for any in-flight log writes to complete.
1427b19a79ecSperrin 	 */
1428fa9e4066Sahrens 	mutex_enter(&zilog->zl_lock);
1429b19a79ecSperrin 	while (zilog->zl_writer)
1430b19a79ecSperrin 		cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
1431fa9e4066Sahrens 	mutex_exit(&zilog->zl_lock);
1432fa9e4066Sahrens 
1433d80c45e0Sbonwick 	zil_destroy(zilog, B_FALSE);
1434d80c45e0Sbonwick 
1435d80c45e0Sbonwick 	mutex_enter(&zilog->zl_lock);
1436d80c45e0Sbonwick 	zilog->zl_suspending = B_FALSE;
1437d80c45e0Sbonwick 	cv_broadcast(&zilog->zl_cv_suspend);
1438d80c45e0Sbonwick 	mutex_exit(&zilog->zl_lock);
1439fa9e4066Sahrens 
1440fa9e4066Sahrens 	return (0);
1441fa9e4066Sahrens }
1442fa9e4066Sahrens 
1443fa9e4066Sahrens void
1444fa9e4066Sahrens zil_resume(zilog_t *zilog)
1445fa9e4066Sahrens {
1446fa9e4066Sahrens 	mutex_enter(&zilog->zl_lock);
1447fa9e4066Sahrens 	ASSERT(zilog->zl_suspend != 0);
1448fa9e4066Sahrens 	zilog->zl_suspend--;
1449fa9e4066Sahrens 	mutex_exit(&zilog->zl_lock);
1450fa9e4066Sahrens }
1451fa9e4066Sahrens 
1452fa9e4066Sahrens typedef struct zil_replay_arg {
1453fa9e4066Sahrens 	objset_t	*zr_os;
1454fa9e4066Sahrens 	zil_replay_func_t **zr_replay;
1455fa9e4066Sahrens 	void		*zr_arg;
1456fa9e4066Sahrens 	boolean_t	zr_byteswap;
1457fa9e4066Sahrens 	char		*zr_lrbuf;
1458fa9e4066Sahrens } zil_replay_arg_t;
1459fa9e4066Sahrens 
1460fa9e4066Sahrens static void
1461fa9e4066Sahrens zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg)
1462fa9e4066Sahrens {
1463fa9e4066Sahrens 	zil_replay_arg_t *zr = zra;
1464d80c45e0Sbonwick 	const zil_header_t *zh = zilog->zl_header;
1465fa9e4066Sahrens 	uint64_t reclen = lr->lrc_reclen;
1466fa9e4066Sahrens 	uint64_t txtype = lr->lrc_txtype;
146767bd71c6Sperrin 	char *name;
14681209a471SNeil Perrin 	int pass, error;
1469fa9e4066Sahrens 
14701209a471SNeil Perrin 	if (!zilog->zl_replay)			/* giving up */
1471fa9e4066Sahrens 		return;
1472fa9e4066Sahrens 
1473fa9e4066Sahrens 	if (lr->lrc_txg < claim_txg)		/* already committed */
1474fa9e4066Sahrens 		return;
1475fa9e4066Sahrens 
1476fa9e4066Sahrens 	if (lr->lrc_seq <= zh->zh_replay_seq)	/* already replayed */
1477fa9e4066Sahrens 		return;
1478fa9e4066Sahrens 
1479da6c28aaSamw 	/* Strip case-insensitive bit, still present in log record */
1480da6c28aaSamw 	txtype &= ~TX_CI;
1481da6c28aaSamw 
14821209a471SNeil Perrin 	if (txtype == 0 || txtype >= TX_MAX_TYPE) {
14831209a471SNeil Perrin 		error = EINVAL;
14841209a471SNeil Perrin 		goto bad;
14851209a471SNeil Perrin 	}
14861209a471SNeil Perrin 
1487fa9e4066Sahrens 	/*
1488fa9e4066Sahrens 	 * Make a copy of the data so we can revise and extend it.
1489fa9e4066Sahrens 	 */
1490fa9e4066Sahrens 	bcopy(lr, zr->zr_lrbuf, reclen);
1491fa9e4066Sahrens 
1492fa9e4066Sahrens 	/*
1493fa9e4066Sahrens 	 * The log block containing this lr may have been byteswapped
1494fa9e4066Sahrens 	 * so that we can easily examine common fields like lrc_txtype.
1495fa9e4066Sahrens 	 * However, the log is a mix of different data types, and only the
1496fa9e4066Sahrens 	 * replay vectors know how to byteswap their records.  Therefore, if
1497fa9e4066Sahrens 	 * the lr was byteswapped, undo it before invoking the replay vector.
1498fa9e4066Sahrens 	 */
1499fa9e4066Sahrens 	if (zr->zr_byteswap)
1500fa9e4066Sahrens 		byteswap_uint64_array(zr->zr_lrbuf, reclen);
1501fa9e4066Sahrens 
1502fa9e4066Sahrens 	/*
1503fa9e4066Sahrens 	 * If this is a TX_WRITE with a blkptr, suck in the data.
1504fa9e4066Sahrens 	 */
1505fa9e4066Sahrens 	if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) {
1506fa9e4066Sahrens 		lr_write_t *lrw = (lr_write_t *)lr;
1507fa9e4066Sahrens 		blkptr_t *wbp = &lrw->lr_blkptr;
1508fa9e4066Sahrens 		uint64_t wlen = lrw->lr_length;
1509fa9e4066Sahrens 		char *wbuf = zr->zr_lrbuf + reclen;
1510fa9e4066Sahrens 
1511fa9e4066Sahrens 		if (BP_IS_HOLE(wbp)) {	/* compressed to a hole */
1512fa9e4066Sahrens 			bzero(wbuf, wlen);
1513fa9e4066Sahrens 		} else {
1514fa9e4066Sahrens 			/*
1515fa9e4066Sahrens 			 * A subsequent write may have overwritten this block,
1516fa9e4066Sahrens 			 * in which case wbp may have been been freed and
1517fa9e4066Sahrens 			 * reallocated, and our read of wbp may fail with a
1518fa9e4066Sahrens 			 * checksum error.  We can safely ignore this because
1519fa9e4066Sahrens 			 * the later write will provide the correct data.
1520fa9e4066Sahrens 			 */
1521ea8dc4b6Seschrock 			zbookmark_t zb;
1522ea8dc4b6Seschrock 
1523ea8dc4b6Seschrock 			zb.zb_objset = dmu_objset_id(zilog->zl_os);
1524ea8dc4b6Seschrock 			zb.zb_object = lrw->lr_foid;
1525ea8dc4b6Seschrock 			zb.zb_level = -1;
1526ea8dc4b6Seschrock 			zb.zb_blkid = lrw->lr_offset / BP_GET_LSIZE(wbp);
1527ea8dc4b6Seschrock 
1528fa9e4066Sahrens 			(void) zio_wait(zio_read(NULL, zilog->zl_spa,
1529fa9e4066Sahrens 			    wbp, wbuf, BP_GET_LSIZE(wbp), NULL, NULL,
1530fa9e4066Sahrens 			    ZIO_PRIORITY_SYNC_READ,
1531ea8dc4b6Seschrock 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &zb));
1532fa9e4066Sahrens 			(void) memmove(wbuf, wbuf + lrw->lr_blkoff, wlen);
1533fa9e4066Sahrens 		}
1534fa9e4066Sahrens 	}
1535fa9e4066Sahrens 
1536fa9e4066Sahrens 	/*
1537fa9e4066Sahrens 	 * We must now do two things atomically: replay this log record,
15381209a471SNeil Perrin 	 * and update the log header sequence number to reflect the fact that
15391209a471SNeil Perrin 	 * we did so. At the end of each replay function the sequence number
15401209a471SNeil Perrin 	 * is updated if we are in replay mode.
1541fa9e4066Sahrens 	 */
15421209a471SNeil Perrin 	for (pass = 1; pass <= 2; pass++) {
15431209a471SNeil Perrin 		zilog->zl_replaying_seq = lr->lrc_seq;
15441209a471SNeil Perrin 		/* Only byteswap (if needed) on the 1st pass.  */
15451209a471SNeil Perrin 		error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lrbuf,
15461209a471SNeil Perrin 		    zr->zr_byteswap && pass == 1);
1547fa9e4066Sahrens 
154867bd71c6Sperrin 		if (!error)
154967bd71c6Sperrin 			return;
155067bd71c6Sperrin 
155167bd71c6Sperrin 		/*
155267bd71c6Sperrin 		 * The DMU's dnode layer doesn't see removes until the txg
155367bd71c6Sperrin 		 * commits, so a subsequent claim can spuriously fail with
15541209a471SNeil Perrin 		 * EEXIST. So if we receive any error we try syncing out
15551209a471SNeil Perrin 		 * any removes then retry the transaction.
155667bd71c6Sperrin 		 */
15571209a471SNeil Perrin 		if (pass == 1)
155867bd71c6Sperrin 			txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0);
1559fa9e4066Sahrens 	}
1560fa9e4066Sahrens 
1561d30e856aSNeil Perrin bad:
15621209a471SNeil Perrin 	ASSERT(error);
156367bd71c6Sperrin 	name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
156467bd71c6Sperrin 	dmu_objset_name(zr->zr_os, name);
156567bd71c6Sperrin 	cmn_err(CE_WARN, "ZFS replay transaction error %d, "
1566da6c28aaSamw 	    "dataset %s, seq 0x%llx, txtype %llu %s\n",
1567da6c28aaSamw 	    error, name, (u_longlong_t)lr->lrc_seq, (u_longlong_t)txtype,
1568da6c28aaSamw 	    (lr->lrc_txtype & TX_CI) ? "CI" : "");
15691209a471SNeil Perrin 	zilog->zl_replay = B_FALSE;
157067bd71c6Sperrin 	kmem_free(name, MAXNAMELEN);
157167bd71c6Sperrin }
1572fa9e4066Sahrens 
157367bd71c6Sperrin /* ARGSUSED */
157467bd71c6Sperrin static void
157567bd71c6Sperrin zil_incr_blks(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
157667bd71c6Sperrin {
157767bd71c6Sperrin 	zilog->zl_replay_blks++;
1578fa9e4066Sahrens }
1579fa9e4066Sahrens 
1580fa9e4066Sahrens /*
158113f5297eSperrin  * If this dataset has a non-empty intent log, replay it and destroy it.
1582fa9e4066Sahrens  */
1583fa9e4066Sahrens void
15841209a471SNeil Perrin zil_replay(objset_t *os, void *arg, zil_replay_func_t *replay_func[TX_MAX_TYPE])
1585fa9e4066Sahrens {
1586fa9e4066Sahrens 	zilog_t *zilog = dmu_objset_zil(os);
1587d80c45e0Sbonwick 	const zil_header_t *zh = zilog->zl_header;
1588d80c45e0Sbonwick 	zil_replay_arg_t zr;
158913f5297eSperrin 
15903589c4f0SNeil Perrin 	if ((zh->zh_flags & ZIL_REPLAY_NEEDED) == 0) {
1591d80c45e0Sbonwick 		zil_destroy(zilog, B_TRUE);
159213f5297eSperrin 		return;
159313f5297eSperrin 	}
1594fa9e4066Sahrens 
1595fa9e4066Sahrens 	zr.zr_os = os;
1596fa9e4066Sahrens 	zr.zr_replay = replay_func;
1597fa9e4066Sahrens 	zr.zr_arg = arg;
1598d80c45e0Sbonwick 	zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log);
1599fa9e4066Sahrens 	zr.zr_lrbuf = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP);
1600fa9e4066Sahrens 
1601fa9e4066Sahrens 	/*
1602fa9e4066Sahrens 	 * Wait for in-progress removes to sync before starting replay.
1603fa9e4066Sahrens 	 */
1604fa9e4066Sahrens 	txg_wait_synced(zilog->zl_dmu_pool, 0);
1605fa9e4066Sahrens 
16061209a471SNeil Perrin 	zilog->zl_replay = B_TRUE;
160767bd71c6Sperrin 	zilog->zl_replay_time = lbolt;
160867bd71c6Sperrin 	ASSERT(zilog->zl_replay_blks == 0);
160967bd71c6Sperrin 	(void) zil_parse(zilog, zil_incr_blks, zil_replay_log_record, &zr,
1610d80c45e0Sbonwick 	    zh->zh_claim_txg);
1611fa9e4066Sahrens 	kmem_free(zr.zr_lrbuf, 2 * SPA_MAXBLOCKSIZE);
1612fa9e4066Sahrens 
1613d80c45e0Sbonwick 	zil_destroy(zilog, B_FALSE);
1614a4611edeSahrens 	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
16151209a471SNeil Perrin 	zilog->zl_replay = B_FALSE;
1616fa9e4066Sahrens }
1617436b2950Sperrin 
1618436b2950Sperrin /*
1619436b2950Sperrin  * Report whether all transactions are committed
1620436b2950Sperrin  */
1621436b2950Sperrin int
1622436b2950Sperrin zil_is_committed(zilog_t *zilog)
1623436b2950Sperrin {
1624436b2950Sperrin 	lwb_t *lwb;
1625b19a79ecSperrin 	int ret;
1626b19a79ecSperrin 
1627b19a79ecSperrin 	mutex_enter(&zilog->zl_lock);
1628b19a79ecSperrin 	while (zilog->zl_writer)
1629b19a79ecSperrin 		cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
1630b19a79ecSperrin 
1631b19a79ecSperrin 	/* recent unpushed intent log transactions? */
1632b19a79ecSperrin 	if (!list_is_empty(&zilog->zl_itx_list)) {
1633b19a79ecSperrin 		ret = B_FALSE;
1634b19a79ecSperrin 		goto out;
1635b19a79ecSperrin 	}
1636436b2950Sperrin 
1637b19a79ecSperrin 	/* intent log never used? */
1638b19a79ecSperrin 	lwb = list_head(&zilog->zl_lwb_list);
1639b19a79ecSperrin 	if (lwb == NULL) {
1640b19a79ecSperrin 		ret = B_TRUE;
1641b19a79ecSperrin 		goto out;
1642b19a79ecSperrin 	}
1643436b2950Sperrin 
1644436b2950Sperrin 	/*
1645b19a79ecSperrin 	 * more than 1 log buffer means zil_sync() hasn't yet freed
1646b19a79ecSperrin 	 * entries after a txg has committed
1647436b2950Sperrin 	 */
1648b19a79ecSperrin 	if (list_next(&zilog->zl_lwb_list, lwb)) {
1649b19a79ecSperrin 		ret = B_FALSE;
1650b19a79ecSperrin 		goto out;
1651b19a79ecSperrin 	}
1652b19a79ecSperrin 
1653436b2950Sperrin 	ASSERT(zil_empty(zilog));
1654b19a79ecSperrin 	ret = B_TRUE;
1655b19a79ecSperrin out:
1656b19a79ecSperrin 	cv_broadcast(&zilog->zl_cv_writer);
1657b19a79ecSperrin 	mutex_exit(&zilog->zl_lock);
1658b19a79ecSperrin 	return (ret);
1659436b2950Sperrin }
1660e6ca193dSGeorge Wilson 
1661e6ca193dSGeorge Wilson /* ARGSUSED */
1662e6ca193dSGeorge Wilson int
1663e6ca193dSGeorge Wilson zil_vdev_offline(char *osname, void *arg)
1664e6ca193dSGeorge Wilson {
1665e6ca193dSGeorge Wilson 	objset_t *os;
1666e6ca193dSGeorge Wilson 	zilog_t *zilog;
1667e6ca193dSGeorge Wilson 	int error;
1668e6ca193dSGeorge Wilson 
1669503ad85cSMatthew Ahrens 	error = dmu_objset_hold(osname, FTAG, &os);
1670e6ca193dSGeorge Wilson 	if (error)
1671e6ca193dSGeorge Wilson 		return (error);
1672e6ca193dSGeorge Wilson 
1673e6ca193dSGeorge Wilson 	zilog = dmu_objset_zil(os);
1674e6ca193dSGeorge Wilson 	if (zil_suspend(zilog) != 0)
1675e6ca193dSGeorge Wilson 		error = EEXIST;
1676e6ca193dSGeorge Wilson 	else
1677e6ca193dSGeorge Wilson 		zil_resume(zilog);
1678503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
1679e6ca193dSGeorge Wilson 	return (error);
1680e6ca193dSGeorge Wilson }
1681