zil.c revision 243674
1168404Spjd/*
2168404Spjd * CDDL HEADER START
3168404Spjd *
4168404Spjd * The contents of this file are subject to the terms of the
5168404Spjd * Common Development and Distribution License (the "License").
6168404Spjd * You may not use this file except in compliance with the License.
7168404Spjd *
8168404Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9168404Spjd * or http://www.opensolaris.org/os/licensing.
10168404Spjd * See the License for the specific language governing permissions
11168404Spjd * and limitations under the License.
12168404Spjd *
13168404Spjd * When distributing Covered Code, include this CDDL HEADER in each
14168404Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15168404Spjd * If applicable, add the following below this CDDL HEADER, with the
16168404Spjd * fields enclosed by brackets "[]" replaced with your own identifying
17168404Spjd * information: Portions Copyright [yyyy] [name of copyright owner]
18168404Spjd *
19168404Spjd * CDDL HEADER END
20168404Spjd */
21168404Spjd/*
22219089Spjd * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23243674Smm * Copyright (c) 2012 by Delphix. All rights reserved.
24168404Spjd */
25168404Spjd
26219089Spjd/* Portions Copyright 2010 Robert Milkowski */
27219089Spjd
28168404Spjd#include <sys/zfs_context.h>
29168404Spjd#include <sys/spa.h>
30168404Spjd#include <sys/dmu.h>
31168404Spjd#include <sys/zap.h>
32168404Spjd#include <sys/arc.h>
33168404Spjd#include <sys/stat.h>
34168404Spjd#include <sys/resource.h>
35168404Spjd#include <sys/zil.h>
36168404Spjd#include <sys/zil_impl.h>
37168404Spjd#include <sys/dsl_dataset.h>
38219089Spjd#include <sys/vdev_impl.h>
39168404Spjd#include <sys/dmu_tx.h>
40219089Spjd#include <sys/dsl_pool.h>
41168404Spjd
42168404Spjd/*
43168404Spjd * The zfs intent log (ZIL) saves transaction records of system calls
44168404Spjd * that change the file system in memory with enough information
45168404Spjd * to be able to replay them. These are stored in memory until
46168404Spjd * either the DMU transaction group (txg) commits them to the stable pool
47168404Spjd * and they can be discarded, or they are flushed to the stable log
48168404Spjd * (also in the pool) due to a fsync, O_DSYNC or other synchronous
49168404Spjd * requirement. In the event of a panic or power fail then those log
50168404Spjd * records (transactions) are replayed.
51168404Spjd *
52168404Spjd * There is one ZIL per file system. Its on-disk (pool) format consists
53168404Spjd * of 3 parts:
54168404Spjd *
55168404Spjd * 	- ZIL header
56168404Spjd * 	- ZIL blocks
57168404Spjd * 	- ZIL records
58168404Spjd *
59168404Spjd * A log record holds a system call transaction. Log blocks can
60168404Spjd * hold many log records and the blocks are chained together.
61168404Spjd * Each ZIL block contains a block pointer (blkptr_t) to the next
62168404Spjd * ZIL block in the chain. The ZIL header points to the first
63168404Spjd * block in the chain. Note there is not a fixed place in the pool
64168404Spjd * to hold blocks. They are dynamically allocated and freed as
65168404Spjd * needed from the blocks available. Figure X shows the ZIL structure:
66168404Spjd */
67168404Spjd
68168404Spjd/*
69168404Spjd * This global ZIL switch affects all pools
70168404Spjd */
71219089Spjdint zil_replay_disable = 0;    /* disable intent logging replay */
72168404SpjdSYSCTL_DECL(_vfs_zfs);
73219089SpjdTUNABLE_INT("vfs.zfs.zil_replay_disable", &zil_replay_disable);
74219089SpjdSYSCTL_INT(_vfs_zfs, OID_AUTO, zil_replay_disable, CTLFLAG_RW,
75219089Spjd    &zil_replay_disable, 0, "Disable intent logging replay");
76168404Spjd
77168404Spjd/*
78168404Spjd * Tunable parameter for debugging or performance analysis.  Setting
79168404Spjd * zfs_nocacheflush will cause corruption on power loss if a volatile
80168404Spjd * out-of-order write cache is enabled.
81168404Spjd */
82168404Spjdboolean_t zfs_nocacheflush = B_FALSE;
83168404SpjdTUNABLE_INT("vfs.zfs.cache_flush_disable", &zfs_nocacheflush);
84168404SpjdSYSCTL_INT(_vfs_zfs, OID_AUTO, cache_flush_disable, CTLFLAG_RDTUN,
85168404Spjd    &zfs_nocacheflush, 0, "Disable cache flush");
86168404Spjd
87168404Spjdstatic kmem_cache_t *zil_lwb_cache;
88168404Spjd
89219089Spjdstatic void zil_async_to_sync(zilog_t *zilog, uint64_t foid);
90219089Spjd
91219089Spjd#define	LWB_EMPTY(lwb) ((BP_GET_LSIZE(&lwb->lwb_blk) - \
92219089Spjd    sizeof (zil_chain_t)) == (lwb->lwb_sz - lwb->lwb_nused))
93219089Spjd
94219089Spjd
95219089Spjd/*
96219089Spjd * ziltest is by and large an ugly hack, but very useful in
97219089Spjd * checking replay without tedious work.
98219089Spjd * When running ziltest we want to keep all itx's and so maintain
99219089Spjd * a single list in the zl_itxg[] that uses a high txg: ZILTEST_TXG
100219089Spjd * We subtract TXG_CONCURRENT_STATES to allow for common code.
101219089Spjd */
102219089Spjd#define	ZILTEST_TXG (UINT64_MAX - TXG_CONCURRENT_STATES)
103219089Spjd
104168404Spjdstatic int
105219089Spjdzil_bp_compare(const void *x1, const void *x2)
106168404Spjd{
107219089Spjd	const dva_t *dva1 = &((zil_bp_node_t *)x1)->zn_dva;
108219089Spjd	const dva_t *dva2 = &((zil_bp_node_t *)x2)->zn_dva;
109168404Spjd
110168404Spjd	if (DVA_GET_VDEV(dva1) < DVA_GET_VDEV(dva2))
111168404Spjd		return (-1);
112168404Spjd	if (DVA_GET_VDEV(dva1) > DVA_GET_VDEV(dva2))
113168404Spjd		return (1);
114168404Spjd
115168404Spjd	if (DVA_GET_OFFSET(dva1) < DVA_GET_OFFSET(dva2))
116168404Spjd		return (-1);
117168404Spjd	if (DVA_GET_OFFSET(dva1) > DVA_GET_OFFSET(dva2))
118168404Spjd		return (1);
119168404Spjd
120168404Spjd	return (0);
121168404Spjd}
122168404Spjd
123168404Spjdstatic void
124219089Spjdzil_bp_tree_init(zilog_t *zilog)
125168404Spjd{
126219089Spjd	avl_create(&zilog->zl_bp_tree, zil_bp_compare,
127219089Spjd	    sizeof (zil_bp_node_t), offsetof(zil_bp_node_t, zn_node));
128168404Spjd}
129168404Spjd
130168404Spjdstatic void
131219089Spjdzil_bp_tree_fini(zilog_t *zilog)
132168404Spjd{
133219089Spjd	avl_tree_t *t = &zilog->zl_bp_tree;
134219089Spjd	zil_bp_node_t *zn;
135168404Spjd	void *cookie = NULL;
136168404Spjd
137168404Spjd	while ((zn = avl_destroy_nodes(t, &cookie)) != NULL)
138219089Spjd		kmem_free(zn, sizeof (zil_bp_node_t));
139168404Spjd
140168404Spjd	avl_destroy(t);
141168404Spjd}
142168404Spjd
143219089Spjdint
144219089Spjdzil_bp_tree_add(zilog_t *zilog, const blkptr_t *bp)
145168404Spjd{
146219089Spjd	avl_tree_t *t = &zilog->zl_bp_tree;
147219089Spjd	const dva_t *dva = BP_IDENTITY(bp);
148219089Spjd	zil_bp_node_t *zn;
149168404Spjd	avl_index_t where;
150168404Spjd
151168404Spjd	if (avl_find(t, dva, &where) != NULL)
152168404Spjd		return (EEXIST);
153168404Spjd
154219089Spjd	zn = kmem_alloc(sizeof (zil_bp_node_t), KM_SLEEP);
155168404Spjd	zn->zn_dva = *dva;
156168404Spjd	avl_insert(t, zn, where);
157168404Spjd
158168404Spjd	return (0);
159168404Spjd}
160168404Spjd
161168404Spjdstatic zil_header_t *
162168404Spjdzil_header_in_syncing_context(zilog_t *zilog)
163168404Spjd{
164168404Spjd	return ((zil_header_t *)zilog->zl_header);
165168404Spjd}
166168404Spjd
167168404Spjdstatic void
168168404Spjdzil_init_log_chain(zilog_t *zilog, blkptr_t *bp)
169168404Spjd{
170168404Spjd	zio_cksum_t *zc = &bp->blk_cksum;
171168404Spjd
172168404Spjd	zc->zc_word[ZIL_ZC_GUID_0] = spa_get_random(-1ULL);
173168404Spjd	zc->zc_word[ZIL_ZC_GUID_1] = spa_get_random(-1ULL);
174168404Spjd	zc->zc_word[ZIL_ZC_OBJSET] = dmu_objset_id(zilog->zl_os);
175168404Spjd	zc->zc_word[ZIL_ZC_SEQ] = 1ULL;
176168404Spjd}
177168404Spjd
178168404Spjd/*
179219089Spjd * Read a log block and make sure it's valid.
180168404Spjd */
181168404Spjdstatic int
182219089Spjdzil_read_log_block(zilog_t *zilog, const blkptr_t *bp, blkptr_t *nbp, void *dst,
183219089Spjd    char **end)
184168404Spjd{
185219089Spjd	enum zio_flag zio_flags = ZIO_FLAG_CANFAIL;
186219089Spjd	uint32_t aflags = ARC_WAIT;
187219089Spjd	arc_buf_t *abuf = NULL;
188168404Spjd	zbookmark_t zb;
189168404Spjd	int error;
190168404Spjd
191219089Spjd	if (zilog->zl_header->zh_claim_txg == 0)
192219089Spjd		zio_flags |= ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB;
193168404Spjd
194219089Spjd	if (!(zilog->zl_header->zh_flags & ZIL_CLAIM_LR_SEQ_VALID))
195219089Spjd		zio_flags |= ZIO_FLAG_SPECULATIVE;
196168404Spjd
197219089Spjd	SET_BOOKMARK(&zb, bp->blk_cksum.zc_word[ZIL_ZC_OBJSET],
198219089Spjd	    ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, bp->blk_cksum.zc_word[ZIL_ZC_SEQ]);
199168404Spjd
200219089Spjd	error = dsl_read_nolock(NULL, zilog->zl_spa, bp, arc_getbuf_func, &abuf,
201219089Spjd	    ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb);
202219089Spjd
203168404Spjd	if (error == 0) {
204168404Spjd		zio_cksum_t cksum = bp->blk_cksum;
205168404Spjd
206168404Spjd		/*
207185029Spjd		 * Validate the checksummed log block.
208185029Spjd		 *
209168404Spjd		 * Sequence numbers should be... sequential.  The checksum
210168404Spjd		 * verifier for the next block should be bp's checksum plus 1.
211185029Spjd		 *
212185029Spjd		 * Also check the log chain linkage and size used.
213168404Spjd		 */
214168404Spjd		cksum.zc_word[ZIL_ZC_SEQ]++;
215168404Spjd
216219089Spjd		if (BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_ZILOG2) {
217219089Spjd			zil_chain_t *zilc = abuf->b_data;
218219089Spjd			char *lr = (char *)(zilc + 1);
219219089Spjd			uint64_t len = zilc->zc_nused - sizeof (zil_chain_t);
220219089Spjd
221219089Spjd			if (bcmp(&cksum, &zilc->zc_next_blk.blk_cksum,
222219089Spjd			    sizeof (cksum)) || BP_IS_HOLE(&zilc->zc_next_blk)) {
223219089Spjd				error = ECKSUM;
224219089Spjd			} else {
225219089Spjd				bcopy(lr, dst, len);
226219089Spjd				*end = (char *)dst + len;
227219089Spjd				*nbp = zilc->zc_next_blk;
228219089Spjd			}
229219089Spjd		} else {
230219089Spjd			char *lr = abuf->b_data;
231219089Spjd			uint64_t size = BP_GET_LSIZE(bp);
232219089Spjd			zil_chain_t *zilc = (zil_chain_t *)(lr + size) - 1;
233219089Spjd
234219089Spjd			if (bcmp(&cksum, &zilc->zc_next_blk.blk_cksum,
235219089Spjd			    sizeof (cksum)) || BP_IS_HOLE(&zilc->zc_next_blk) ||
236219089Spjd			    (zilc->zc_nused > (size - sizeof (*zilc)))) {
237219089Spjd				error = ECKSUM;
238219089Spjd			} else {
239219089Spjd				bcopy(lr, dst, zilc->zc_nused);
240219089Spjd				*end = (char *)dst + zilc->zc_nused;
241219089Spjd				*nbp = zilc->zc_next_blk;
242219089Spjd			}
243185029Spjd		}
244168404Spjd
245219089Spjd		VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
246168404Spjd	}
247168404Spjd
248219089Spjd	return (error);
249219089Spjd}
250168404Spjd
251219089Spjd/*
252219089Spjd * Read a TX_WRITE log data block.
253219089Spjd */
254219089Spjdstatic int
255219089Spjdzil_read_log_data(zilog_t *zilog, const lr_write_t *lr, void *wbuf)
256219089Spjd{
257219089Spjd	enum zio_flag zio_flags = ZIO_FLAG_CANFAIL;
258219089Spjd	const blkptr_t *bp = &lr->lr_blkptr;
259219089Spjd	uint32_t aflags = ARC_WAIT;
260219089Spjd	arc_buf_t *abuf = NULL;
261219089Spjd	zbookmark_t zb;
262219089Spjd	int error;
263219089Spjd
264219089Spjd	if (BP_IS_HOLE(bp)) {
265219089Spjd		if (wbuf != NULL)
266219089Spjd			bzero(wbuf, MAX(BP_GET_LSIZE(bp), lr->lr_length));
267219089Spjd		return (0);
268219089Spjd	}
269219089Spjd
270219089Spjd	if (zilog->zl_header->zh_claim_txg == 0)
271219089Spjd		zio_flags |= ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB;
272219089Spjd
273219089Spjd	SET_BOOKMARK(&zb, dmu_objset_id(zilog->zl_os), lr->lr_foid,
274219089Spjd	    ZB_ZIL_LEVEL, lr->lr_offset / BP_GET_LSIZE(bp));
275219089Spjd
276219089Spjd	error = arc_read_nolock(NULL, zilog->zl_spa, bp, arc_getbuf_func, &abuf,
277219089Spjd	    ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb);
278219089Spjd
279219089Spjd	if (error == 0) {
280219089Spjd		if (wbuf != NULL)
281219089Spjd			bcopy(abuf->b_data, wbuf, arc_buf_size(abuf));
282219089Spjd		(void) arc_buf_remove_ref(abuf, &abuf);
283219089Spjd	}
284219089Spjd
285168404Spjd	return (error);
286168404Spjd}
287168404Spjd
288168404Spjd/*
289168404Spjd * Parse the intent log, and call parse_func for each valid record within.
290168404Spjd */
291219089Spjdint
292168404Spjdzil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func,
293168404Spjd    zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg)
294168404Spjd{
295168404Spjd	const zil_header_t *zh = zilog->zl_header;
296219089Spjd	boolean_t claimed = !!zh->zh_claim_txg;
297219089Spjd	uint64_t claim_blk_seq = claimed ? zh->zh_claim_blk_seq : UINT64_MAX;
298219089Spjd	uint64_t claim_lr_seq = claimed ? zh->zh_claim_lr_seq : UINT64_MAX;
299219089Spjd	uint64_t max_blk_seq = 0;
300219089Spjd	uint64_t max_lr_seq = 0;
301219089Spjd	uint64_t blk_count = 0;
302219089Spjd	uint64_t lr_count = 0;
303219089Spjd	blkptr_t blk, next_blk;
304168404Spjd	char *lrbuf, *lrp;
305219089Spjd	int error = 0;
306168404Spjd
307219089Spjd	/*
308219089Spjd	 * Old logs didn't record the maximum zh_claim_lr_seq.
309219089Spjd	 */
310219089Spjd	if (!(zh->zh_flags & ZIL_CLAIM_LR_SEQ_VALID))
311219089Spjd		claim_lr_seq = UINT64_MAX;
312168404Spjd
313168404Spjd	/*
314168404Spjd	 * Starting at the block pointed to by zh_log we read the log chain.
315168404Spjd	 * For each block in the chain we strongly check that block to
316168404Spjd	 * ensure its validity.  We stop when an invalid block is found.
317168404Spjd	 * For each block pointer in the chain we call parse_blk_func().
318168404Spjd	 * For each record in each valid block we call parse_lr_func().
319168404Spjd	 * If the log has been claimed, stop if we encounter a sequence
320168404Spjd	 * number greater than the highest claimed sequence number.
321168404Spjd	 */
322219089Spjd	lrbuf = zio_buf_alloc(SPA_MAXBLOCKSIZE);
323219089Spjd	zil_bp_tree_init(zilog);
324168404Spjd
325219089Spjd	for (blk = zh->zh_log; !BP_IS_HOLE(&blk); blk = next_blk) {
326219089Spjd		uint64_t blk_seq = blk.blk_cksum.zc_word[ZIL_ZC_SEQ];
327219089Spjd		int reclen;
328219089Spjd		char *end;
329219089Spjd
330219089Spjd		if (blk_seq > claim_blk_seq)
331168404Spjd			break;
332219089Spjd		if ((error = parse_blk_func(zilog, &blk, arg, txg)) != 0)
333219089Spjd			break;
334219089Spjd		ASSERT3U(max_blk_seq, <, blk_seq);
335219089Spjd		max_blk_seq = blk_seq;
336219089Spjd		blk_count++;
337168404Spjd
338219089Spjd		if (max_lr_seq == claim_lr_seq && max_blk_seq == claim_blk_seq)
339219089Spjd			break;
340168404Spjd
341219089Spjd		error = zil_read_log_block(zilog, &blk, &next_blk, lrbuf, &end);
342168404Spjd		if (error)
343168404Spjd			break;
344168404Spjd
345219089Spjd		for (lrp = lrbuf; lrp < end; lrp += reclen) {
346168404Spjd			lr_t *lr = (lr_t *)lrp;
347168404Spjd			reclen = lr->lrc_reclen;
348168404Spjd			ASSERT3U(reclen, >=, sizeof (lr_t));
349219089Spjd			if (lr->lrc_seq > claim_lr_seq)
350219089Spjd				goto done;
351219089Spjd			if ((error = parse_lr_func(zilog, lr, arg, txg)) != 0)
352219089Spjd				goto done;
353219089Spjd			ASSERT3U(max_lr_seq, <, lr->lrc_seq);
354219089Spjd			max_lr_seq = lr->lrc_seq;
355219089Spjd			lr_count++;
356168404Spjd		}
357168404Spjd	}
358219089Spjddone:
359219089Spjd	zilog->zl_parse_error = error;
360219089Spjd	zilog->zl_parse_blk_seq = max_blk_seq;
361219089Spjd	zilog->zl_parse_lr_seq = max_lr_seq;
362219089Spjd	zilog->zl_parse_blk_count = blk_count;
363219089Spjd	zilog->zl_parse_lr_count = lr_count;
364168404Spjd
365219089Spjd	ASSERT(!claimed || !(zh->zh_flags & ZIL_CLAIM_LR_SEQ_VALID) ||
366219089Spjd	    (max_blk_seq == claim_blk_seq && max_lr_seq == claim_lr_seq));
367219089Spjd
368219089Spjd	zil_bp_tree_fini(zilog);
369219089Spjd	zio_buf_free(lrbuf, SPA_MAXBLOCKSIZE);
370219089Spjd
371219089Spjd	return (error);
372168404Spjd}
373168404Spjd
374219089Spjdstatic int
375168404Spjdzil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg)
376168404Spjd{
377168404Spjd	/*
378168404Spjd	 * Claim log block if not already committed and not already claimed.
379219089Spjd	 * If tx == NULL, just verify that the block is claimable.
380168404Spjd	 */
381219089Spjd	if (bp->blk_birth < first_txg || zil_bp_tree_add(zilog, bp) != 0)
382219089Spjd		return (0);
383219089Spjd
384219089Spjd	return (zio_wait(zio_claim(NULL, zilog->zl_spa,
385219089Spjd	    tx == NULL ? 0 : first_txg, bp, spa_claim_notify, NULL,
386219089Spjd	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB)));
387168404Spjd}
388168404Spjd
389219089Spjdstatic int
390168404Spjdzil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg)
391168404Spjd{
392219089Spjd	lr_write_t *lr = (lr_write_t *)lrc;
393219089Spjd	int error;
394219089Spjd
395219089Spjd	if (lrc->lrc_txtype != TX_WRITE)
396219089Spjd		return (0);
397219089Spjd
398219089Spjd	/*
399219089Spjd	 * If the block is not readable, don't claim it.  This can happen
400219089Spjd	 * in normal operation when a log block is written to disk before
401219089Spjd	 * some of the dmu_sync() blocks it points to.  In this case, the
402219089Spjd	 * transaction cannot have been committed to anyone (we would have
403219089Spjd	 * waited for all writes to be stable first), so it is semantically
404219089Spjd	 * correct to declare this the end of the log.
405219089Spjd	 */
406219089Spjd	if (lr->lr_blkptr.blk_birth >= first_txg &&
407219089Spjd	    (error = zil_read_log_data(zilog, lr, NULL)) != 0)
408219089Spjd		return (error);
409219089Spjd	return (zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg));
410168404Spjd}
411168404Spjd
412168404Spjd/* ARGSUSED */
413219089Spjdstatic int
414168404Spjdzil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg)
415168404Spjd{
416219089Spjd	zio_free_zil(zilog->zl_spa, dmu_tx_get_txg(tx), bp);
417219089Spjd
418219089Spjd	return (0);
419168404Spjd}
420168404Spjd
421219089Spjdstatic int
422168404Spjdzil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg)
423168404Spjd{
424219089Spjd	lr_write_t *lr = (lr_write_t *)lrc;
425219089Spjd	blkptr_t *bp = &lr->lr_blkptr;
426219089Spjd
427168404Spjd	/*
428168404Spjd	 * If we previously claimed it, we need to free it.
429168404Spjd	 */
430219089Spjd	if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE &&
431219089Spjd	    bp->blk_birth >= claim_txg && zil_bp_tree_add(zilog, bp) == 0)
432219089Spjd		zio_free(zilog->zl_spa, dmu_tx_get_txg(tx), bp);
433219089Spjd
434219089Spjd	return (0);
435219089Spjd}
436219089Spjd
437219089Spjdstatic lwb_t *
438219089Spjdzil_alloc_lwb(zilog_t *zilog, blkptr_t *bp, uint64_t txg)
439219089Spjd{
440219089Spjd	lwb_t *lwb;
441219089Spjd
442219089Spjd	lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP);
443219089Spjd	lwb->lwb_zilog = zilog;
444219089Spjd	lwb->lwb_blk = *bp;
445219089Spjd	lwb->lwb_buf = zio_buf_alloc(BP_GET_LSIZE(bp));
446219089Spjd	lwb->lwb_max_txg = txg;
447219089Spjd	lwb->lwb_zio = NULL;
448219089Spjd	lwb->lwb_tx = NULL;
449219089Spjd	if (BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_ZILOG2) {
450219089Spjd		lwb->lwb_nused = sizeof (zil_chain_t);
451219089Spjd		lwb->lwb_sz = BP_GET_LSIZE(bp);
452219089Spjd	} else {
453219089Spjd		lwb->lwb_nused = 0;
454219089Spjd		lwb->lwb_sz = BP_GET_LSIZE(bp) - sizeof (zil_chain_t);
455168404Spjd	}
456219089Spjd
457219089Spjd	mutex_enter(&zilog->zl_lock);
458219089Spjd	list_insert_tail(&zilog->zl_lwb_list, lwb);
459219089Spjd	mutex_exit(&zilog->zl_lock);
460219089Spjd
461219089Spjd	return (lwb);
462168404Spjd}
463168404Spjd
464168404Spjd/*
465243674Smm * Called when we create in-memory log transactions so that we know
466243674Smm * to cleanup the itxs at the end of spa_sync().
467243674Smm */
468243674Smmvoid
469243674Smmzilog_dirty(zilog_t *zilog, uint64_t txg)
470243674Smm{
471243674Smm	dsl_pool_t *dp = zilog->zl_dmu_pool;
472243674Smm	dsl_dataset_t *ds = dmu_objset_ds(zilog->zl_os);
473243674Smm
474243674Smm	if (dsl_dataset_is_snapshot(ds))
475243674Smm		panic("dirtying snapshot!");
476243674Smm
477243674Smm	if (txg_list_add(&dp->dp_dirty_zilogs, zilog, txg) == 0) {
478243674Smm		/* up the hold count until we can be written out */
479243674Smm		dmu_buf_add_ref(ds->ds_dbuf, zilog);
480243674Smm	}
481243674Smm}
482243674Smm
483243674Smmboolean_t
484243674Smmzilog_is_dirty(zilog_t *zilog)
485243674Smm{
486243674Smm	dsl_pool_t *dp = zilog->zl_dmu_pool;
487243674Smm
488243674Smm	for (int t = 0; t < TXG_SIZE; t++) {
489243674Smm		if (txg_list_member(&dp->dp_dirty_zilogs, zilog, t))
490243674Smm			return (B_TRUE);
491243674Smm	}
492243674Smm	return (B_FALSE);
493243674Smm}
494243674Smm
495243674Smm/*
496168404Spjd * Create an on-disk intent log.
497168404Spjd */
498219089Spjdstatic lwb_t *
499168404Spjdzil_create(zilog_t *zilog)
500168404Spjd{
501168404Spjd	const zil_header_t *zh = zilog->zl_header;
502219089Spjd	lwb_t *lwb = NULL;
503168404Spjd	uint64_t txg = 0;
504168404Spjd	dmu_tx_t *tx = NULL;
505168404Spjd	blkptr_t blk;
506168404Spjd	int error = 0;
507168404Spjd
508168404Spjd	/*
509168404Spjd	 * Wait for any previous destroy to complete.
510168404Spjd	 */
511168404Spjd	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
512168404Spjd
513168404Spjd	ASSERT(zh->zh_claim_txg == 0);
514168404Spjd	ASSERT(zh->zh_replay_seq == 0);
515168404Spjd
516168404Spjd	blk = zh->zh_log;
517168404Spjd
518168404Spjd	/*
519219089Spjd	 * Allocate an initial log block if:
520219089Spjd	 *    - there isn't one already
521219089Spjd	 *    - the existing block is the wrong endianess
522168404Spjd	 */
523207908Smm	if (BP_IS_HOLE(&blk) || BP_SHOULD_BYTESWAP(&blk)) {
524168404Spjd		tx = dmu_tx_create(zilog->zl_os);
525219089Spjd		VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0);
526168404Spjd		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
527168404Spjd		txg = dmu_tx_get_txg(tx);
528168404Spjd
529207908Smm		if (!BP_IS_HOLE(&blk)) {
530219089Spjd			zio_free_zil(zilog->zl_spa, txg, &blk);
531207908Smm			BP_ZERO(&blk);
532207908Smm		}
533207908Smm
534219089Spjd		error = zio_alloc_zil(zilog->zl_spa, txg, &blk, NULL,
535219089Spjd		    ZIL_MIN_BLKSZ, zilog->zl_logbias == ZFS_LOGBIAS_LATENCY);
536168404Spjd
537168404Spjd		if (error == 0)
538168404Spjd			zil_init_log_chain(zilog, &blk);
539168404Spjd	}
540168404Spjd
541168404Spjd	/*
542168404Spjd	 * Allocate a log write buffer (lwb) for the first log block.
543168404Spjd	 */
544219089Spjd	if (error == 0)
545219089Spjd		lwb = zil_alloc_lwb(zilog, &blk, txg);
546168404Spjd
547168404Spjd	/*
548168404Spjd	 * If we just allocated the first log block, commit our transaction
549168404Spjd	 * and wait for zil_sync() to stuff the block poiner into zh_log.
550168404Spjd	 * (zh is part of the MOS, so we cannot modify it in open context.)
551168404Spjd	 */
552168404Spjd	if (tx != NULL) {
553168404Spjd		dmu_tx_commit(tx);
554168404Spjd		txg_wait_synced(zilog->zl_dmu_pool, txg);
555168404Spjd	}
556168404Spjd
557168404Spjd	ASSERT(bcmp(&blk, &zh->zh_log, sizeof (blk)) == 0);
558219089Spjd
559219089Spjd	return (lwb);
560168404Spjd}
561168404Spjd
562168404Spjd/*
563168404Spjd * In one tx, free all log blocks and clear the log header.
564168404Spjd * If keep_first is set, then we're replaying a log with no content.
565168404Spjd * We want to keep the first block, however, so that the first
566168404Spjd * synchronous transaction doesn't require a txg_wait_synced()
567168404Spjd * in zil_create().  We don't need to txg_wait_synced() here either
568168404Spjd * when keep_first is set, because both zil_create() and zil_destroy()
569168404Spjd * will wait for any in-progress destroys to complete.
570168404Spjd */
571168404Spjdvoid
572168404Spjdzil_destroy(zilog_t *zilog, boolean_t keep_first)
573168404Spjd{
574168404Spjd	const zil_header_t *zh = zilog->zl_header;
575168404Spjd	lwb_t *lwb;
576168404Spjd	dmu_tx_t *tx;
577168404Spjd	uint64_t txg;
578168404Spjd
579168404Spjd	/*
580168404Spjd	 * Wait for any previous destroy to complete.
581168404Spjd	 */
582168404Spjd	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
583168404Spjd
584219089Spjd	zilog->zl_old_header = *zh;		/* debugging aid */
585219089Spjd
586168404Spjd	if (BP_IS_HOLE(&zh->zh_log))
587168404Spjd		return;
588168404Spjd
589168404Spjd	tx = dmu_tx_create(zilog->zl_os);
590219089Spjd	VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0);
591168404Spjd	dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
592168404Spjd	txg = dmu_tx_get_txg(tx);
593168404Spjd
594168404Spjd	mutex_enter(&zilog->zl_lock);
595168404Spjd
596168404Spjd	ASSERT3U(zilog->zl_destroy_txg, <, txg);
597168404Spjd	zilog->zl_destroy_txg = txg;
598168404Spjd	zilog->zl_keep_first = keep_first;
599168404Spjd
600168404Spjd	if (!list_is_empty(&zilog->zl_lwb_list)) {
601168404Spjd		ASSERT(zh->zh_claim_txg == 0);
602224526Smm		VERIFY(!keep_first);
603168404Spjd		while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
604168404Spjd			list_remove(&zilog->zl_lwb_list, lwb);
605168404Spjd			if (lwb->lwb_buf != NULL)
606168404Spjd				zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
607219089Spjd			zio_free_zil(zilog->zl_spa, txg, &lwb->lwb_blk);
608168404Spjd			kmem_cache_free(zil_lwb_cache, lwb);
609168404Spjd		}
610219089Spjd	} else if (!keep_first) {
611243674Smm		zil_destroy_sync(zilog, tx);
612168404Spjd	}
613168404Spjd	mutex_exit(&zilog->zl_lock);
614168404Spjd
615168404Spjd	dmu_tx_commit(tx);
616185029Spjd}
617168404Spjd
618243674Smmvoid
619243674Smmzil_destroy_sync(zilog_t *zilog, dmu_tx_t *tx)
620243674Smm{
621243674Smm	ASSERT(list_is_empty(&zilog->zl_lwb_list));
622243674Smm	(void) zil_parse(zilog, zil_free_log_block,
623243674Smm	    zil_free_log_record, tx, zilog->zl_header->zh_claim_txg);
624243674Smm}
625243674Smm
626168404Spjdint
627219089Spjdzil_claim(const char *osname, void *txarg)
628168404Spjd{
629168404Spjd	dmu_tx_t *tx = txarg;
630168404Spjd	uint64_t first_txg = dmu_tx_get_txg(tx);
631168404Spjd	zilog_t *zilog;
632168404Spjd	zil_header_t *zh;
633168404Spjd	objset_t *os;
634168404Spjd	int error;
635168404Spjd
636219089Spjd	error = dmu_objset_hold(osname, FTAG, &os);
637168404Spjd	if (error) {
638185029Spjd		cmn_err(CE_WARN, "can't open objset for %s", osname);
639168404Spjd		return (0);
640168404Spjd	}
641168404Spjd
642168404Spjd	zilog = dmu_objset_zil(os);
643168404Spjd	zh = zil_header_in_syncing_context(zilog);
644168404Spjd
645219089Spjd	if (spa_get_log_state(zilog->zl_spa) == SPA_LOG_CLEAR) {
646213197Smm		if (!BP_IS_HOLE(&zh->zh_log))
647219089Spjd			zio_free_zil(zilog->zl_spa, first_txg, &zh->zh_log);
648213197Smm		BP_ZERO(&zh->zh_log);
649213197Smm		dsl_dataset_dirty(dmu_objset_ds(os), tx);
650219089Spjd		dmu_objset_rele(os, FTAG);
651219089Spjd		return (0);
652213197Smm	}
653213197Smm
654168404Spjd	/*
655168404Spjd	 * Claim all log blocks if we haven't already done so, and remember
656168404Spjd	 * the highest claimed sequence number.  This ensures that if we can
657168404Spjd	 * read only part of the log now (e.g. due to a missing device),
658168404Spjd	 * but we can read the entire log later, we will not try to replay
659168404Spjd	 * or destroy beyond the last block we successfully claimed.
660168404Spjd	 */
661168404Spjd	ASSERT3U(zh->zh_claim_txg, <=, first_txg);
662168404Spjd	if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) {
663219089Spjd		(void) zil_parse(zilog, zil_claim_log_block,
664219089Spjd		    zil_claim_log_record, tx, first_txg);
665168404Spjd		zh->zh_claim_txg = first_txg;
666219089Spjd		zh->zh_claim_blk_seq = zilog->zl_parse_blk_seq;
667219089Spjd		zh->zh_claim_lr_seq = zilog->zl_parse_lr_seq;
668219089Spjd		if (zilog->zl_parse_lr_count || zilog->zl_parse_blk_count > 1)
669219089Spjd			zh->zh_flags |= ZIL_REPLAY_NEEDED;
670219089Spjd		zh->zh_flags |= ZIL_CLAIM_LR_SEQ_VALID;
671168404Spjd		dsl_dataset_dirty(dmu_objset_ds(os), tx);
672168404Spjd	}
673168404Spjd
674168404Spjd	ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1));
675219089Spjd	dmu_objset_rele(os, FTAG);
676168404Spjd	return (0);
677168404Spjd}
678168404Spjd
679185029Spjd/*
680185029Spjd * Check the log by walking the log chain.
681185029Spjd * Checksum errors are ok as they indicate the end of the chain.
682185029Spjd * Any other error (no device or read failure) returns an error.
683185029Spjd */
684185029Spjdint
685219089Spjdzil_check_log_chain(const char *osname, void *tx)
686168404Spjd{
687185029Spjd	zilog_t *zilog;
688185029Spjd	objset_t *os;
689219089Spjd	blkptr_t *bp;
690185029Spjd	int error;
691168404Spjd
692219089Spjd	ASSERT(tx == NULL);
693219089Spjd
694219089Spjd	error = dmu_objset_hold(osname, FTAG, &os);
695185029Spjd	if (error) {
696185029Spjd		cmn_err(CE_WARN, "can't open objset for %s", osname);
697185029Spjd		return (0);
698185029Spjd	}
699168404Spjd
700185029Spjd	zilog = dmu_objset_zil(os);
701219089Spjd	bp = (blkptr_t *)&zilog->zl_header->zh_log;
702219089Spjd
703219089Spjd	/*
704219089Spjd	 * Check the first block and determine if it's on a log device
705219089Spjd	 * which may have been removed or faulted prior to loading this
706219089Spjd	 * pool.  If so, there's no point in checking the rest of the log
707219089Spjd	 * as its content should have already been synced to the pool.
708219089Spjd	 */
709219089Spjd	if (!BP_IS_HOLE(bp)) {
710219089Spjd		vdev_t *vd;
711219089Spjd		boolean_t valid = B_TRUE;
712219089Spjd
713219089Spjd		spa_config_enter(os->os_spa, SCL_STATE, FTAG, RW_READER);
714219089Spjd		vd = vdev_lookup_top(os->os_spa, DVA_GET_VDEV(&bp->blk_dva[0]));
715219089Spjd		if (vd->vdev_islog && vdev_is_dead(vd))
716219089Spjd			valid = vdev_log_state_valid(vd);
717219089Spjd		spa_config_exit(os->os_spa, SCL_STATE, FTAG);
718219089Spjd
719219089Spjd		if (!valid) {
720219089Spjd			dmu_objset_rele(os, FTAG);
721219089Spjd			return (0);
722219089Spjd		}
723168404Spjd	}
724185029Spjd
725219089Spjd	/*
726219089Spjd	 * Because tx == NULL, zil_claim_log_block() will not actually claim
727219089Spjd	 * any blocks, but just determine whether it is possible to do so.
728219089Spjd	 * In addition to checking the log chain, zil_claim_log_block()
729219089Spjd	 * will invoke zio_claim() with a done func of spa_claim_notify(),
730219089Spjd	 * which will update spa_max_claim_txg.  See spa_load() for details.
731219089Spjd	 */
732219089Spjd	error = zil_parse(zilog, zil_claim_log_block, zil_claim_log_record, tx,
733219089Spjd	    zilog->zl_header->zh_claim_txg ? -1ULL : spa_first_txg(os->os_spa));
734219089Spjd
735219089Spjd	dmu_objset_rele(os, FTAG);
736219089Spjd
737219089Spjd	return ((error == ECKSUM || error == ENOENT) ? 0 : error);
738168404Spjd}
739168404Spjd
740185029Spjdstatic int
741185029Spjdzil_vdev_compare(const void *x1, const void *x2)
742185029Spjd{
743219089Spjd	const uint64_t v1 = ((zil_vdev_node_t *)x1)->zv_vdev;
744219089Spjd	const uint64_t v2 = ((zil_vdev_node_t *)x2)->zv_vdev;
745185029Spjd
746185029Spjd	if (v1 < v2)
747185029Spjd		return (-1);
748185029Spjd	if (v1 > v2)
749185029Spjd		return (1);
750185029Spjd
751185029Spjd	return (0);
752185029Spjd}
753185029Spjd
754168404Spjdvoid
755219089Spjdzil_add_block(zilog_t *zilog, const blkptr_t *bp)
756168404Spjd{
757185029Spjd	avl_tree_t *t = &zilog->zl_vdev_tree;
758185029Spjd	avl_index_t where;
759185029Spjd	zil_vdev_node_t *zv, zvsearch;
760185029Spjd	int ndvas = BP_GET_NDVAS(bp);
761185029Spjd	int i;
762168404Spjd
763185029Spjd	if (zfs_nocacheflush)
764185029Spjd		return;
765168404Spjd
766185029Spjd	ASSERT(zilog->zl_writer);
767168404Spjd
768185029Spjd	/*
769185029Spjd	 * Even though we're zl_writer, we still need a lock because the
770185029Spjd	 * zl_get_data() callbacks may have dmu_sync() done callbacks
771185029Spjd	 * that will run concurrently.
772185029Spjd	 */
773185029Spjd	mutex_enter(&zilog->zl_vdev_lock);
774185029Spjd	for (i = 0; i < ndvas; i++) {
775185029Spjd		zvsearch.zv_vdev = DVA_GET_VDEV(&bp->blk_dva[i]);
776185029Spjd		if (avl_find(t, &zvsearch, &where) == NULL) {
777185029Spjd			zv = kmem_alloc(sizeof (*zv), KM_SLEEP);
778185029Spjd			zv->zv_vdev = zvsearch.zv_vdev;
779185029Spjd			avl_insert(t, zv, where);
780185029Spjd		}
781185029Spjd	}
782185029Spjd	mutex_exit(&zilog->zl_vdev_lock);
783168404Spjd}
784168404Spjd
785219089Spjdstatic void
786168404Spjdzil_flush_vdevs(zilog_t *zilog)
787168404Spjd{
788168404Spjd	spa_t *spa = zilog->zl_spa;
789185029Spjd	avl_tree_t *t = &zilog->zl_vdev_tree;
790185029Spjd	void *cookie = NULL;
791185029Spjd	zil_vdev_node_t *zv;
792185029Spjd	zio_t *zio;
793168404Spjd
794168404Spjd	ASSERT(zilog->zl_writer);
795168404Spjd
796185029Spjd	/*
797185029Spjd	 * We don't need zl_vdev_lock here because we're the zl_writer,
798185029Spjd	 * and all zl_get_data() callbacks are done.
799185029Spjd	 */
800185029Spjd	if (avl_numnodes(t) == 0)
801185029Spjd		return;
802185029Spjd
803185029Spjd	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
804185029Spjd
805185029Spjd	zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
806185029Spjd
807185029Spjd	while ((zv = avl_destroy_nodes(t, &cookie)) != NULL) {
808185029Spjd		vdev_t *vd = vdev_lookup_top(spa, zv->zv_vdev);
809185029Spjd		if (vd != NULL)
810185029Spjd			zio_flush(zio, vd);
811185029Spjd		kmem_free(zv, sizeof (*zv));
812168404Spjd	}
813168404Spjd
814168404Spjd	/*
815168404Spjd	 * Wait for all the flushes to complete.  Not all devices actually
816168404Spjd	 * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails.
817168404Spjd	 */
818185029Spjd	(void) zio_wait(zio);
819185029Spjd
820185029Spjd	spa_config_exit(spa, SCL_STATE, FTAG);
821168404Spjd}
822168404Spjd
823168404Spjd/*
824168404Spjd * Function called when a log block write completes
825168404Spjd */
826168404Spjdstatic void
827168404Spjdzil_lwb_write_done(zio_t *zio)
828168404Spjd{
829168404Spjd	lwb_t *lwb = zio->io_private;
830168404Spjd	zilog_t *zilog = lwb->lwb_zilog;
831219089Spjd	dmu_tx_t *tx = lwb->lwb_tx;
832168404Spjd
833185029Spjd	ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF);
834185029Spjd	ASSERT(BP_GET_TYPE(zio->io_bp) == DMU_OT_INTENT_LOG);
835185029Spjd	ASSERT(BP_GET_LEVEL(zio->io_bp) == 0);
836185029Spjd	ASSERT(BP_GET_BYTEORDER(zio->io_bp) == ZFS_HOST_BYTEORDER);
837185029Spjd	ASSERT(!BP_IS_GANG(zio->io_bp));
838185029Spjd	ASSERT(!BP_IS_HOLE(zio->io_bp));
839185029Spjd	ASSERT(zio->io_bp->blk_fill == 0);
840185029Spjd
841168404Spjd	/*
842209962Smm	 * Ensure the lwb buffer pointer is cleared before releasing
843209962Smm	 * the txg. If we have had an allocation failure and
844209962Smm	 * the txg is waiting to sync then we want want zil_sync()
845209962Smm	 * to remove the lwb so that it's not picked up as the next new
846209962Smm	 * one in zil_commit_writer(). zil_sync() will only remove
847209962Smm	 * the lwb if lwb_buf is null.
848168404Spjd	 */
849168404Spjd	zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
850168404Spjd	mutex_enter(&zilog->zl_lock);
851168404Spjd	lwb->lwb_buf = NULL;
852219089Spjd	lwb->lwb_tx = NULL;
853219089Spjd	mutex_exit(&zilog->zl_lock);
854209962Smm
855209962Smm	/*
856209962Smm	 * Now that we've written this log block, we have a stable pointer
857209962Smm	 * to the next block in the chain, so it's OK to let the txg in
858219089Spjd	 * which we allocated the next block sync.
859209962Smm	 */
860219089Spjd	dmu_tx_commit(tx);
861168404Spjd}
862168404Spjd
863168404Spjd/*
864168404Spjd * Initialize the io for a log block.
865168404Spjd */
866168404Spjdstatic void
867168404Spjdzil_lwb_write_init(zilog_t *zilog, lwb_t *lwb)
868168404Spjd{
869168404Spjd	zbookmark_t zb;
870168404Spjd
871219089Spjd	SET_BOOKMARK(&zb, lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET],
872219089Spjd	    ZB_ZIL_OBJECT, ZB_ZIL_LEVEL,
873219089Spjd	    lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ]);
874168404Spjd
875168404Spjd	if (zilog->zl_root_zio == NULL) {
876168404Spjd		zilog->zl_root_zio = zio_root(zilog->zl_spa, NULL, NULL,
877168404Spjd		    ZIO_FLAG_CANFAIL);
878168404Spjd	}
879168404Spjd	if (lwb->lwb_zio == NULL) {
880168404Spjd		lwb->lwb_zio = zio_rewrite(zilog->zl_root_zio, zilog->zl_spa,
881219089Spjd		    0, &lwb->lwb_blk, lwb->lwb_buf, BP_GET_LSIZE(&lwb->lwb_blk),
882213197Smm		    zil_lwb_write_done, lwb, ZIO_PRIORITY_LOG_WRITE,
883219089Spjd		    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE, &zb);
884168404Spjd	}
885168404Spjd}
886168404Spjd
887168404Spjd/*
888219089Spjd * Define a limited set of intent log block sizes.
889219089Spjd * These must be a multiple of 4KB. Note only the amount used (again
890219089Spjd * aligned to 4KB) actually gets written. However, we can't always just
891219089Spjd * allocate SPA_MAXBLOCKSIZE as the slog space could be exhausted.
892219089Spjd */
893219089Spjduint64_t zil_block_buckets[] = {
894219089Spjd    4096,		/* non TX_WRITE */
895219089Spjd    8192+4096,		/* data base */
896219089Spjd    32*1024 + 4096, 	/* NFS writes */
897219089Spjd    UINT64_MAX
898219089Spjd};
899219089Spjd
900219089Spjd/*
901219089Spjd * Use the slog as long as the logbias is 'latency' and the current commit size
902219089Spjd * is less than the limit or the total list size is less than 2X the limit.
903219089Spjd * Limit checking is disabled by setting zil_slog_limit to UINT64_MAX.
904219089Spjd */
905219089Spjduint64_t zil_slog_limit = 1024 * 1024;
906219089Spjd#define	USE_SLOG(zilog) (((zilog)->zl_logbias == ZFS_LOGBIAS_LATENCY) && \
907219089Spjd	(((zilog)->zl_cur_used < zil_slog_limit) || \
908219089Spjd	((zilog)->zl_itx_list_sz < (zil_slog_limit << 1))))
909219089Spjd
910219089Spjd/*
911168404Spjd * Start a log block write and advance to the next log block.
912168404Spjd * Calls are serialized.
913168404Spjd */
914168404Spjdstatic lwb_t *
915168404Spjdzil_lwb_write_start(zilog_t *zilog, lwb_t *lwb)
916168404Spjd{
917219089Spjd	lwb_t *nlwb = NULL;
918219089Spjd	zil_chain_t *zilc;
919168404Spjd	spa_t *spa = zilog->zl_spa;
920219089Spjd	blkptr_t *bp;
921219089Spjd	dmu_tx_t *tx;
922168404Spjd	uint64_t txg;
923219089Spjd	uint64_t zil_blksz, wsz;
924219089Spjd	int i, error;
925168404Spjd
926219089Spjd	if (BP_GET_CHECKSUM(&lwb->lwb_blk) == ZIO_CHECKSUM_ZILOG2) {
927219089Spjd		zilc = (zil_chain_t *)lwb->lwb_buf;
928219089Spjd		bp = &zilc->zc_next_blk;
929219089Spjd	} else {
930219089Spjd		zilc = (zil_chain_t *)(lwb->lwb_buf + lwb->lwb_sz);
931219089Spjd		bp = &zilc->zc_next_blk;
932219089Spjd	}
933168404Spjd
934219089Spjd	ASSERT(lwb->lwb_nused <= lwb->lwb_sz);
935219089Spjd
936168404Spjd	/*
937168404Spjd	 * Allocate the next block and save its address in this block
938168404Spjd	 * before writing it in order to establish the log chain.
939168404Spjd	 * Note that if the allocation of nlwb synced before we wrote
940168404Spjd	 * the block that points at it (lwb), we'd leak it if we crashed.
941219089Spjd	 * Therefore, we don't do dmu_tx_commit() until zil_lwb_write_done().
942219089Spjd	 * We dirty the dataset to ensure that zil_sync() will be called
943219089Spjd	 * to clean up in the event of allocation failure or I/O failure.
944168404Spjd	 */
945219089Spjd	tx = dmu_tx_create(zilog->zl_os);
946219089Spjd	VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0);
947219089Spjd	dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
948219089Spjd	txg = dmu_tx_get_txg(tx);
949168404Spjd
950219089Spjd	lwb->lwb_tx = tx;
951219089Spjd
952168404Spjd	/*
953219089Spjd	 * Log blocks are pre-allocated. Here we select the size of the next
954219089Spjd	 * block, based on size used in the last block.
955219089Spjd	 * - first find the smallest bucket that will fit the block from a
956219089Spjd	 *   limited set of block sizes. This is because it's faster to write
957219089Spjd	 *   blocks allocated from the same metaslab as they are adjacent or
958219089Spjd	 *   close.
959219089Spjd	 * - next find the maximum from the new suggested size and an array of
960219089Spjd	 *   previous sizes. This lessens a picket fence effect of wrongly
961219089Spjd	 *   guesssing the size if we have a stream of say 2k, 64k, 2k, 64k
962219089Spjd	 *   requests.
963219089Spjd	 *
964219089Spjd	 * Note we only write what is used, but we can't just allocate
965219089Spjd	 * the maximum block size because we can exhaust the available
966219089Spjd	 * pool log space.
967168404Spjd	 */
968219089Spjd	zil_blksz = zilog->zl_cur_used + sizeof (zil_chain_t);
969219089Spjd	for (i = 0; zil_blksz > zil_block_buckets[i]; i++)
970219089Spjd		continue;
971219089Spjd	zil_blksz = zil_block_buckets[i];
972219089Spjd	if (zil_blksz == UINT64_MAX)
973219089Spjd		zil_blksz = SPA_MAXBLOCKSIZE;
974219089Spjd	zilog->zl_prev_blks[zilog->zl_prev_rotor] = zil_blksz;
975219089Spjd	for (i = 0; i < ZIL_PREV_BLKS; i++)
976219089Spjd		zil_blksz = MAX(zil_blksz, zilog->zl_prev_blks[i]);
977219089Spjd	zilog->zl_prev_rotor = (zilog->zl_prev_rotor + 1) & (ZIL_PREV_BLKS - 1);
978168404Spjd
979168404Spjd	BP_ZERO(bp);
980168404Spjd	/* pass the old blkptr in order to spread log blocks across devs */
981219089Spjd	error = zio_alloc_zil(spa, txg, bp, &lwb->lwb_blk, zil_blksz,
982219089Spjd	    USE_SLOG(zilog));
983219089Spjd	if (!error) {
984219089Spjd		ASSERT3U(bp->blk_birth, ==, txg);
985219089Spjd		bp->blk_cksum = lwb->lwb_blk.blk_cksum;
986219089Spjd		bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++;
987168404Spjd
988168404Spjd		/*
989219089Spjd		 * Allocate a new log write buffer (lwb).
990168404Spjd		 */
991219089Spjd		nlwb = zil_alloc_lwb(zilog, bp, txg);
992168404Spjd
993219089Spjd		/* Record the block for later vdev flushing */
994219089Spjd		zil_add_block(zilog, &lwb->lwb_blk);
995168404Spjd	}
996168404Spjd
997219089Spjd	if (BP_GET_CHECKSUM(&lwb->lwb_blk) == ZIO_CHECKSUM_ZILOG2) {
998219089Spjd		/* For Slim ZIL only write what is used. */
999219089Spjd		wsz = P2ROUNDUP_TYPED(lwb->lwb_nused, ZIL_MIN_BLKSZ, uint64_t);
1000219089Spjd		ASSERT3U(wsz, <=, lwb->lwb_sz);
1001219089Spjd		zio_shrink(lwb->lwb_zio, wsz);
1002168404Spjd
1003219089Spjd	} else {
1004219089Spjd		wsz = lwb->lwb_sz;
1005219089Spjd	}
1006168404Spjd
1007219089Spjd	zilc->zc_pad = 0;
1008219089Spjd	zilc->zc_nused = lwb->lwb_nused;
1009219089Spjd	zilc->zc_eck.zec_cksum = lwb->lwb_blk.blk_cksum;
1010168404Spjd
1011168404Spjd	/*
1012219089Spjd	 * clear unused data for security
1013168404Spjd	 */
1014219089Spjd	bzero(lwb->lwb_buf + lwb->lwb_nused, wsz - lwb->lwb_nused);
1015168404Spjd
1016219089Spjd	zio_nowait(lwb->lwb_zio); /* Kick off the write for the old log block */
1017168404Spjd
1018168404Spjd	/*
1019219089Spjd	 * If there was an allocation failure then nlwb will be null which
1020219089Spjd	 * forces a txg_wait_synced().
1021168404Spjd	 */
1022168404Spjd	return (nlwb);
1023168404Spjd}
1024168404Spjd
1025168404Spjdstatic lwb_t *
1026168404Spjdzil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb)
1027168404Spjd{
1028168404Spjd	lr_t *lrc = &itx->itx_lr; /* common log record */
1029219089Spjd	lr_write_t *lrw = (lr_write_t *)lrc;
1030219089Spjd	char *lr_buf;
1031168404Spjd	uint64_t txg = lrc->lrc_txg;
1032168404Spjd	uint64_t reclen = lrc->lrc_reclen;
1033219089Spjd	uint64_t dlen = 0;
1034168404Spjd
1035168404Spjd	if (lwb == NULL)
1036168404Spjd		return (NULL);
1037219089Spjd
1038168404Spjd	ASSERT(lwb->lwb_buf != NULL);
1039243674Smm	ASSERT(zilog_is_dirty(zilog) ||
1040243674Smm	    spa_freeze_txg(zilog->zl_spa) != UINT64_MAX);
1041168404Spjd
1042168404Spjd	if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY)
1043168404Spjd		dlen = P2ROUNDUP_TYPED(
1044219089Spjd		    lrw->lr_length, sizeof (uint64_t), uint64_t);
1045168404Spjd
1046168404Spjd	zilog->zl_cur_used += (reclen + dlen);
1047168404Spjd
1048168404Spjd	zil_lwb_write_init(zilog, lwb);
1049168404Spjd
1050168404Spjd	/*
1051168404Spjd	 * If this record won't fit in the current log block, start a new one.
1052168404Spjd	 */
1053219089Spjd	if (lwb->lwb_nused + reclen + dlen > lwb->lwb_sz) {
1054168404Spjd		lwb = zil_lwb_write_start(zilog, lwb);
1055168404Spjd		if (lwb == NULL)
1056168404Spjd			return (NULL);
1057168404Spjd		zil_lwb_write_init(zilog, lwb);
1058219089Spjd		ASSERT(LWB_EMPTY(lwb));
1059219089Spjd		if (lwb->lwb_nused + reclen + dlen > lwb->lwb_sz) {
1060168404Spjd			txg_wait_synced(zilog->zl_dmu_pool, txg);
1061168404Spjd			return (lwb);
1062168404Spjd		}
1063168404Spjd	}
1064168404Spjd
1065219089Spjd	lr_buf = lwb->lwb_buf + lwb->lwb_nused;
1066219089Spjd	bcopy(lrc, lr_buf, reclen);
1067219089Spjd	lrc = (lr_t *)lr_buf;
1068219089Spjd	lrw = (lr_write_t *)lrc;
1069168404Spjd
1070168404Spjd	/*
1071168404Spjd	 * If it's a write, fetch the data or get its blkptr as appropriate.
1072168404Spjd	 */
1073168404Spjd	if (lrc->lrc_txtype == TX_WRITE) {
1074168404Spjd		if (txg > spa_freeze_txg(zilog->zl_spa))
1075168404Spjd			txg_wait_synced(zilog->zl_dmu_pool, txg);
1076168404Spjd		if (itx->itx_wr_state != WR_COPIED) {
1077168404Spjd			char *dbuf;
1078168404Spjd			int error;
1079168404Spjd
1080168404Spjd			if (dlen) {
1081168404Spjd				ASSERT(itx->itx_wr_state == WR_NEED_COPY);
1082219089Spjd				dbuf = lr_buf + reclen;
1083219089Spjd				lrw->lr_common.lrc_reclen += dlen;
1084168404Spjd			} else {
1085168404Spjd				ASSERT(itx->itx_wr_state == WR_INDIRECT);
1086168404Spjd				dbuf = NULL;
1087168404Spjd			}
1088168404Spjd			error = zilog->zl_get_data(
1089219089Spjd			    itx->itx_private, lrw, dbuf, lwb->lwb_zio);
1090214378Smm			if (error == EIO) {
1091214378Smm				txg_wait_synced(zilog->zl_dmu_pool, txg);
1092214378Smm				return (lwb);
1093214378Smm			}
1094168404Spjd			if (error) {
1095168404Spjd				ASSERT(error == ENOENT || error == EEXIST ||
1096168404Spjd				    error == EALREADY);
1097168404Spjd				return (lwb);
1098168404Spjd			}
1099168404Spjd		}
1100168404Spjd	}
1101168404Spjd
1102219089Spjd	/*
1103219089Spjd	 * We're actually making an entry, so update lrc_seq to be the
1104219089Spjd	 * log record sequence number.  Note that this is generally not
1105219089Spjd	 * equal to the itx sequence number because not all transactions
1106219089Spjd	 * are synchronous, and sometimes spa_sync() gets there first.
1107219089Spjd	 */
1108219089Spjd	lrc->lrc_seq = ++zilog->zl_lr_seq; /* we are single threaded */
1109168404Spjd	lwb->lwb_nused += reclen + dlen;
1110168404Spjd	lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg);
1111219089Spjd	ASSERT3U(lwb->lwb_nused, <=, lwb->lwb_sz);
1112243674Smm	ASSERT0(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)));
1113168404Spjd
1114168404Spjd	return (lwb);
1115168404Spjd}
1116168404Spjd
1117168404Spjditx_t *
1118185029Spjdzil_itx_create(uint64_t txtype, size_t lrsize)
1119168404Spjd{
1120168404Spjd	itx_t *itx;
1121168404Spjd
1122168404Spjd	lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t);
1123168404Spjd
1124168404Spjd	itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP);
1125168404Spjd	itx->itx_lr.lrc_txtype = txtype;
1126168404Spjd	itx->itx_lr.lrc_reclen = lrsize;
1127185029Spjd	itx->itx_sod = lrsize; /* if write & WR_NEED_COPY will be increased */
1128168404Spjd	itx->itx_lr.lrc_seq = 0;	/* defensive */
1129219089Spjd	itx->itx_sync = B_TRUE;		/* default is synchronous */
1130168404Spjd
1131168404Spjd	return (itx);
1132168404Spjd}
1133168404Spjd
1134219089Spjdvoid
1135219089Spjdzil_itx_destroy(itx_t *itx)
1136168404Spjd{
1137219089Spjd	kmem_free(itx, offsetof(itx_t, itx_lr) + itx->itx_lr.lrc_reclen);
1138219089Spjd}
1139168404Spjd
1140219089Spjd/*
1141219089Spjd * Free up the sync and async itxs. The itxs_t has already been detached
1142219089Spjd * so no locks are needed.
1143219089Spjd */
1144219089Spjdstatic void
1145219089Spjdzil_itxg_clean(itxs_t *itxs)
1146219089Spjd{
1147219089Spjd	itx_t *itx;
1148219089Spjd	list_t *list;
1149219089Spjd	avl_tree_t *t;
1150219089Spjd	void *cookie;
1151219089Spjd	itx_async_node_t *ian;
1152168404Spjd
1153219089Spjd	list = &itxs->i_sync_list;
1154219089Spjd	while ((itx = list_head(list)) != NULL) {
1155219089Spjd		list_remove(list, itx);
1156219089Spjd		kmem_free(itx, offsetof(itx_t, itx_lr) +
1157219089Spjd		    itx->itx_lr.lrc_reclen);
1158219089Spjd	}
1159168404Spjd
1160219089Spjd	cookie = NULL;
1161219089Spjd	t = &itxs->i_async_tree;
1162219089Spjd	while ((ian = avl_destroy_nodes(t, &cookie)) != NULL) {
1163219089Spjd		list = &ian->ia_list;
1164219089Spjd		while ((itx = list_head(list)) != NULL) {
1165219089Spjd			list_remove(list, itx);
1166219089Spjd			kmem_free(itx, offsetof(itx_t, itx_lr) +
1167219089Spjd			    itx->itx_lr.lrc_reclen);
1168219089Spjd		}
1169219089Spjd		list_destroy(list);
1170219089Spjd		kmem_free(ian, sizeof (itx_async_node_t));
1171219089Spjd	}
1172219089Spjd	avl_destroy(t);
1173219089Spjd
1174219089Spjd	kmem_free(itxs, sizeof (itxs_t));
1175168404Spjd}
1176168404Spjd
1177219089Spjdstatic int
1178219089Spjdzil_aitx_compare(const void *x1, const void *x2)
1179219089Spjd{
1180219089Spjd	const uint64_t o1 = ((itx_async_node_t *)x1)->ia_foid;
1181219089Spjd	const uint64_t o2 = ((itx_async_node_t *)x2)->ia_foid;
1182219089Spjd
1183219089Spjd	if (o1 < o2)
1184219089Spjd		return (-1);
1185219089Spjd	if (o1 > o2)
1186219089Spjd		return (1);
1187219089Spjd
1188219089Spjd	return (0);
1189219089Spjd}
1190219089Spjd
1191168404Spjd/*
1192219089Spjd * Remove all async itx with the given oid.
1193168404Spjd */
1194168404Spjdstatic void
1195219089Spjdzil_remove_async(zilog_t *zilog, uint64_t oid)
1196168404Spjd{
1197219089Spjd	uint64_t otxg, txg;
1198219089Spjd	itx_async_node_t *ian;
1199219089Spjd	avl_tree_t *t;
1200219089Spjd	avl_index_t where;
1201168404Spjd	list_t clean_list;
1202168404Spjd	itx_t *itx;
1203168404Spjd
1204219089Spjd	ASSERT(oid != 0);
1205168404Spjd	list_create(&clean_list, sizeof (itx_t), offsetof(itx_t, itx_node));
1206168404Spjd
1207219089Spjd	if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */
1208219089Spjd		otxg = ZILTEST_TXG;
1209219089Spjd	else
1210219089Spjd		otxg = spa_last_synced_txg(zilog->zl_spa) + 1;
1211219089Spjd
1212219089Spjd	for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) {
1213219089Spjd		itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK];
1214219089Spjd
1215219089Spjd		mutex_enter(&itxg->itxg_lock);
1216219089Spjd		if (itxg->itxg_txg != txg) {
1217219089Spjd			mutex_exit(&itxg->itxg_lock);
1218219089Spjd			continue;
1219219089Spjd		}
1220219089Spjd
1221219089Spjd		/*
1222219089Spjd		 * Locate the object node and append its list.
1223219089Spjd		 */
1224219089Spjd		t = &itxg->itxg_itxs->i_async_tree;
1225219089Spjd		ian = avl_find(t, &oid, &where);
1226219089Spjd		if (ian != NULL)
1227219089Spjd			list_move_tail(&clean_list, &ian->ia_list);
1228219089Spjd		mutex_exit(&itxg->itxg_lock);
1229168404Spjd	}
1230219089Spjd	while ((itx = list_head(&clean_list)) != NULL) {
1231219089Spjd		list_remove(&clean_list, itx);
1232219089Spjd		kmem_free(itx, offsetof(itx_t, itx_lr) +
1233219089Spjd		    itx->itx_lr.lrc_reclen);
1234219089Spjd	}
1235219089Spjd	list_destroy(&clean_list);
1236219089Spjd}
1237168404Spjd
1238219089Spjdvoid
1239219089Spjdzil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx)
1240219089Spjd{
1241219089Spjd	uint64_t txg;
1242219089Spjd	itxg_t *itxg;
1243219089Spjd	itxs_t *itxs, *clean = NULL;
1244219089Spjd
1245168404Spjd	/*
1246219089Spjd	 * Object ids can be re-instantiated in the next txg so
1247219089Spjd	 * remove any async transactions to avoid future leaks.
1248219089Spjd	 * This can happen if a fsync occurs on the re-instantiated
1249219089Spjd	 * object for a WR_INDIRECT or WR_NEED_COPY write, which gets
1250219089Spjd	 * the new file data and flushes a write record for the old object.
1251168404Spjd	 */
1252219089Spjd	if ((itx->itx_lr.lrc_txtype & ~TX_CI) == TX_REMOVE)
1253219089Spjd		zil_remove_async(zilog, itx->itx_oid);
1254219089Spjd
1255219089Spjd	/*
1256219089Spjd	 * Ensure the data of a renamed file is committed before the rename.
1257219089Spjd	 */
1258219089Spjd	if ((itx->itx_lr.lrc_txtype & ~TX_CI) == TX_RENAME)
1259219089Spjd		zil_async_to_sync(zilog, itx->itx_oid);
1260219089Spjd
1261243674Smm	if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX)
1262219089Spjd		txg = ZILTEST_TXG;
1263219089Spjd	else
1264219089Spjd		txg = dmu_tx_get_txg(tx);
1265219089Spjd
1266219089Spjd	itxg = &zilog->zl_itxg[txg & TXG_MASK];
1267219089Spjd	mutex_enter(&itxg->itxg_lock);
1268219089Spjd	itxs = itxg->itxg_itxs;
1269219089Spjd	if (itxg->itxg_txg != txg) {
1270219089Spjd		if (itxs != NULL) {
1271219089Spjd			/*
1272219089Spjd			 * The zil_clean callback hasn't got around to cleaning
1273219089Spjd			 * this itxg. Save the itxs for release below.
1274219089Spjd			 * This should be rare.
1275219089Spjd			 */
1276219089Spjd			atomic_add_64(&zilog->zl_itx_list_sz, -itxg->itxg_sod);
1277219089Spjd			itxg->itxg_sod = 0;
1278219089Spjd			clean = itxg->itxg_itxs;
1279219089Spjd		}
1280219089Spjd		ASSERT(itxg->itxg_sod == 0);
1281219089Spjd		itxg->itxg_txg = txg;
1282219089Spjd		itxs = itxg->itxg_itxs = kmem_zalloc(sizeof (itxs_t), KM_SLEEP);
1283219089Spjd
1284219089Spjd		list_create(&itxs->i_sync_list, sizeof (itx_t),
1285219089Spjd		    offsetof(itx_t, itx_node));
1286219089Spjd		avl_create(&itxs->i_async_tree, zil_aitx_compare,
1287219089Spjd		    sizeof (itx_async_node_t),
1288219089Spjd		    offsetof(itx_async_node_t, ia_node));
1289168404Spjd	}
1290219089Spjd	if (itx->itx_sync) {
1291219089Spjd		list_insert_tail(&itxs->i_sync_list, itx);
1292219089Spjd		atomic_add_64(&zilog->zl_itx_list_sz, itx->itx_sod);
1293219089Spjd		itxg->itxg_sod += itx->itx_sod;
1294219089Spjd	} else {
1295219089Spjd		avl_tree_t *t = &itxs->i_async_tree;
1296219089Spjd		uint64_t foid = ((lr_ooo_t *)&itx->itx_lr)->lr_foid;
1297219089Spjd		itx_async_node_t *ian;
1298219089Spjd		avl_index_t where;
1299168404Spjd
1300219089Spjd		ian = avl_find(t, &foid, &where);
1301219089Spjd		if (ian == NULL) {
1302219089Spjd			ian = kmem_alloc(sizeof (itx_async_node_t), KM_SLEEP);
1303219089Spjd			list_create(&ian->ia_list, sizeof (itx_t),
1304219089Spjd			    offsetof(itx_t, itx_node));
1305219089Spjd			ian->ia_foid = foid;
1306219089Spjd			avl_insert(t, ian, where);
1307219089Spjd		}
1308219089Spjd		list_insert_tail(&ian->ia_list, itx);
1309168404Spjd	}
1310219089Spjd
1311219089Spjd	itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx);
1312243674Smm	zilog_dirty(zilog, txg);
1313219089Spjd	mutex_exit(&itxg->itxg_lock);
1314219089Spjd
1315219089Spjd	/* Release the old itxs now we've dropped the lock */
1316219089Spjd	if (clean != NULL)
1317219089Spjd		zil_itxg_clean(clean);
1318168404Spjd}
1319168404Spjd
1320168404Spjd/*
1321168404Spjd * If there are any in-memory intent log transactions which have now been
1322243674Smm * synced then start up a taskq to free them. We should only do this after we
1323243674Smm * have written out the uberblocks (i.e. txg has been comitted) so that
1324243674Smm * don't inadvertently clean out in-memory log records that would be required
1325243674Smm * by zil_commit().
1326168404Spjd */
1327168404Spjdvoid
1328219089Spjdzil_clean(zilog_t *zilog, uint64_t synced_txg)
1329168404Spjd{
1330219089Spjd	itxg_t *itxg = &zilog->zl_itxg[synced_txg & TXG_MASK];
1331219089Spjd	itxs_t *clean_me;
1332168404Spjd
1333219089Spjd	mutex_enter(&itxg->itxg_lock);
1334219089Spjd	if (itxg->itxg_itxs == NULL || itxg->itxg_txg == ZILTEST_TXG) {
1335219089Spjd		mutex_exit(&itxg->itxg_lock);
1336219089Spjd		return;
1337168404Spjd	}
1338219089Spjd	ASSERT3U(itxg->itxg_txg, <=, synced_txg);
1339219089Spjd	ASSERT(itxg->itxg_txg != 0);
1340219089Spjd	ASSERT(zilog->zl_clean_taskq != NULL);
1341219089Spjd	atomic_add_64(&zilog->zl_itx_list_sz, -itxg->itxg_sod);
1342219089Spjd	itxg->itxg_sod = 0;
1343219089Spjd	clean_me = itxg->itxg_itxs;
1344219089Spjd	itxg->itxg_itxs = NULL;
1345219089Spjd	itxg->itxg_txg = 0;
1346219089Spjd	mutex_exit(&itxg->itxg_lock);
1347219089Spjd	/*
1348219089Spjd	 * Preferably start a task queue to free up the old itxs but
1349219089Spjd	 * if taskq_dispatch can't allocate resources to do that then
1350219089Spjd	 * free it in-line. This should be rare. Note, using TQ_SLEEP
1351219089Spjd	 * created a bad performance problem.
1352219089Spjd	 */
1353219089Spjd	if (taskq_dispatch(zilog->zl_clean_taskq,
1354219089Spjd	    (void (*)(void *))zil_itxg_clean, clean_me, TQ_NOSLEEP) == 0)
1355219089Spjd		zil_itxg_clean(clean_me);
1356168404Spjd}
1357168404Spjd
1358219089Spjd/*
1359219089Spjd * Get the list of itxs to commit into zl_itx_commit_list.
1360219089Spjd */
1361185029Spjdstatic void
1362219089Spjdzil_get_commit_list(zilog_t *zilog)
1363168404Spjd{
1364219089Spjd	uint64_t otxg, txg;
1365219089Spjd	list_t *commit_list = &zilog->zl_itx_commit_list;
1366219089Spjd	uint64_t push_sod = 0;
1367219089Spjd
1368219089Spjd	if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */
1369219089Spjd		otxg = ZILTEST_TXG;
1370219089Spjd	else
1371219089Spjd		otxg = spa_last_synced_txg(zilog->zl_spa) + 1;
1372219089Spjd
1373219089Spjd	for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) {
1374219089Spjd		itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK];
1375219089Spjd
1376219089Spjd		mutex_enter(&itxg->itxg_lock);
1377219089Spjd		if (itxg->itxg_txg != txg) {
1378219089Spjd			mutex_exit(&itxg->itxg_lock);
1379219089Spjd			continue;
1380219089Spjd		}
1381219089Spjd
1382219089Spjd		list_move_tail(commit_list, &itxg->itxg_itxs->i_sync_list);
1383219089Spjd		push_sod += itxg->itxg_sod;
1384219089Spjd		itxg->itxg_sod = 0;
1385219089Spjd
1386219089Spjd		mutex_exit(&itxg->itxg_lock);
1387219089Spjd	}
1388219089Spjd	atomic_add_64(&zilog->zl_itx_list_sz, -push_sod);
1389219089Spjd}
1390219089Spjd
1391219089Spjd/*
1392219089Spjd * Move the async itxs for a specified object to commit into sync lists.
1393219089Spjd */
1394219089Spjdstatic void
1395219089Spjdzil_async_to_sync(zilog_t *zilog, uint64_t foid)
1396219089Spjd{
1397219089Spjd	uint64_t otxg, txg;
1398219089Spjd	itx_async_node_t *ian;
1399219089Spjd	avl_tree_t *t;
1400219089Spjd	avl_index_t where;
1401219089Spjd
1402219089Spjd	if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */
1403219089Spjd		otxg = ZILTEST_TXG;
1404219089Spjd	else
1405219089Spjd		otxg = spa_last_synced_txg(zilog->zl_spa) + 1;
1406219089Spjd
1407219089Spjd	for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) {
1408219089Spjd		itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK];
1409219089Spjd
1410219089Spjd		mutex_enter(&itxg->itxg_lock);
1411219089Spjd		if (itxg->itxg_txg != txg) {
1412219089Spjd			mutex_exit(&itxg->itxg_lock);
1413219089Spjd			continue;
1414219089Spjd		}
1415219089Spjd
1416219089Spjd		/*
1417219089Spjd		 * If a foid is specified then find that node and append its
1418219089Spjd		 * list. Otherwise walk the tree appending all the lists
1419219089Spjd		 * to the sync list. We add to the end rather than the
1420219089Spjd		 * beginning to ensure the create has happened.
1421219089Spjd		 */
1422219089Spjd		t = &itxg->itxg_itxs->i_async_tree;
1423219089Spjd		if (foid != 0) {
1424219089Spjd			ian = avl_find(t, &foid, &where);
1425219089Spjd			if (ian != NULL) {
1426219089Spjd				list_move_tail(&itxg->itxg_itxs->i_sync_list,
1427219089Spjd				    &ian->ia_list);
1428219089Spjd			}
1429219089Spjd		} else {
1430219089Spjd			void *cookie = NULL;
1431219089Spjd
1432219089Spjd			while ((ian = avl_destroy_nodes(t, &cookie)) != NULL) {
1433219089Spjd				list_move_tail(&itxg->itxg_itxs->i_sync_list,
1434219089Spjd				    &ian->ia_list);
1435219089Spjd				list_destroy(&ian->ia_list);
1436219089Spjd				kmem_free(ian, sizeof (itx_async_node_t));
1437219089Spjd			}
1438219089Spjd		}
1439219089Spjd		mutex_exit(&itxg->itxg_lock);
1440219089Spjd	}
1441219089Spjd}
1442219089Spjd
1443219089Spjdstatic void
1444219089Spjdzil_commit_writer(zilog_t *zilog)
1445219089Spjd{
1446168404Spjd	uint64_t txg;
1447219089Spjd	itx_t *itx;
1448168404Spjd	lwb_t *lwb;
1449219089Spjd	spa_t *spa = zilog->zl_spa;
1450219089Spjd	int error = 0;
1451168404Spjd
1452185029Spjd	ASSERT(zilog->zl_root_zio == NULL);
1453168404Spjd
1454219089Spjd	mutex_exit(&zilog->zl_lock);
1455219089Spjd
1456219089Spjd	zil_get_commit_list(zilog);
1457219089Spjd
1458219089Spjd	/*
1459219089Spjd	 * Return if there's nothing to commit before we dirty the fs by
1460219089Spjd	 * calling zil_create().
1461219089Spjd	 */
1462219089Spjd	if (list_head(&zilog->zl_itx_commit_list) == NULL) {
1463219089Spjd		mutex_enter(&zilog->zl_lock);
1464219089Spjd		return;
1465219089Spjd	}
1466219089Spjd
1467168404Spjd	if (zilog->zl_suspend) {
1468168404Spjd		lwb = NULL;
1469168404Spjd	} else {
1470168404Spjd		lwb = list_tail(&zilog->zl_lwb_list);
1471219089Spjd		if (lwb == NULL)
1472219089Spjd			lwb = zil_create(zilog);
1473168404Spjd	}
1474168404Spjd
1475168404Spjd	DTRACE_PROBE1(zil__cw1, zilog_t *, zilog);
1476219089Spjd	while (itx = list_head(&zilog->zl_itx_commit_list)) {
1477168404Spjd		txg = itx->itx_lr.lrc_txg;
1478168404Spjd		ASSERT(txg);
1479168404Spjd
1480219089Spjd		if (txg > spa_last_synced_txg(spa) || txg > spa_freeze_txg(spa))
1481168404Spjd			lwb = zil_lwb_commit(zilog, itx, lwb);
1482219089Spjd		list_remove(&zilog->zl_itx_commit_list, itx);
1483168404Spjd		kmem_free(itx, offsetof(itx_t, itx_lr)
1484168404Spjd		    + itx->itx_lr.lrc_reclen);
1485168404Spjd	}
1486168404Spjd	DTRACE_PROBE1(zil__cw2, zilog_t *, zilog);
1487168404Spjd
1488168404Spjd	/* write the last block out */
1489168404Spjd	if (lwb != NULL && lwb->lwb_zio != NULL)
1490168404Spjd		lwb = zil_lwb_write_start(zilog, lwb);
1491168404Spjd
1492168404Spjd	zilog->zl_cur_used = 0;
1493168404Spjd
1494168404Spjd	/*
1495168404Spjd	 * Wait if necessary for the log blocks to be on stable storage.
1496168404Spjd	 */
1497168404Spjd	if (zilog->zl_root_zio) {
1498219089Spjd		error = zio_wait(zilog->zl_root_zio);
1499185029Spjd		zilog->zl_root_zio = NULL;
1500185029Spjd		zil_flush_vdevs(zilog);
1501168404Spjd	}
1502168404Spjd
1503219089Spjd	if (error || lwb == NULL)
1504168404Spjd		txg_wait_synced(zilog->zl_dmu_pool, 0);
1505168404Spjd
1506168404Spjd	mutex_enter(&zilog->zl_lock);
1507168404Spjd
1508219089Spjd	/*
1509219089Spjd	 * Remember the highest committed log sequence number for ztest.
1510219089Spjd	 * We only update this value when all the log writes succeeded,
1511219089Spjd	 * because ztest wants to ASSERT that it got the whole log chain.
1512219089Spjd	 */
1513219089Spjd	if (error == 0 && lwb != NULL)
1514219089Spjd		zilog->zl_commit_lr_seq = zilog->zl_lr_seq;
1515168404Spjd}
1516168404Spjd
1517168404Spjd/*
1518219089Spjd * Commit zfs transactions to stable storage.
1519168404Spjd * If foid is 0 push out all transactions, otherwise push only those
1520219089Spjd * for that object or might reference that object.
1521219089Spjd *
1522219089Spjd * itxs are committed in batches. In a heavily stressed zil there will be
1523219089Spjd * a commit writer thread who is writing out a bunch of itxs to the log
1524219089Spjd * for a set of committing threads (cthreads) in the same batch as the writer.
1525219089Spjd * Those cthreads are all waiting on the same cv for that batch.
1526219089Spjd *
1527219089Spjd * There will also be a different and growing batch of threads that are
1528219089Spjd * waiting to commit (qthreads). When the committing batch completes
1529219089Spjd * a transition occurs such that the cthreads exit and the qthreads become
1530219089Spjd * cthreads. One of the new cthreads becomes the writer thread for the
1531219089Spjd * batch. Any new threads arriving become new qthreads.
1532219089Spjd *
1533219089Spjd * Only 2 condition variables are needed and there's no transition
1534219089Spjd * between the two cvs needed. They just flip-flop between qthreads
1535219089Spjd * and cthreads.
1536219089Spjd *
1537219089Spjd * Using this scheme we can efficiently wakeup up only those threads
1538219089Spjd * that have been committed.
1539168404Spjd */
1540168404Spjdvoid
1541219089Spjdzil_commit(zilog_t *zilog, uint64_t foid)
1542168404Spjd{
1543219089Spjd	uint64_t mybatch;
1544219089Spjd
1545219089Spjd	if (zilog->zl_sync == ZFS_SYNC_DISABLED)
1546168404Spjd		return;
1547168404Spjd
1548219089Spjd	/* move the async itxs for the foid to the sync queues */
1549219089Spjd	zil_async_to_sync(zilog, foid);
1550219089Spjd
1551168404Spjd	mutex_enter(&zilog->zl_lock);
1552219089Spjd	mybatch = zilog->zl_next_batch;
1553168404Spjd	while (zilog->zl_writer) {
1554219089Spjd		cv_wait(&zilog->zl_cv_batch[mybatch & 1], &zilog->zl_lock);
1555219089Spjd		if (mybatch <= zilog->zl_com_batch) {
1556168404Spjd			mutex_exit(&zilog->zl_lock);
1557168404Spjd			return;
1558168404Spjd		}
1559168404Spjd	}
1560219089Spjd
1561219089Spjd	zilog->zl_next_batch++;
1562219089Spjd	zilog->zl_writer = B_TRUE;
1563219089Spjd	zil_commit_writer(zilog);
1564219089Spjd	zilog->zl_com_batch = mybatch;
1565219089Spjd	zilog->zl_writer = B_FALSE;
1566168404Spjd	mutex_exit(&zilog->zl_lock);
1567219089Spjd
1568219089Spjd	/* wake up one thread to become the next writer */
1569219089Spjd	cv_signal(&zilog->zl_cv_batch[(mybatch+1) & 1]);
1570219089Spjd
1571219089Spjd	/* wake up all threads waiting for this batch to be committed */
1572219089Spjd	cv_broadcast(&zilog->zl_cv_batch[mybatch & 1]);
1573168404Spjd}
1574168404Spjd
1575168404Spjd/*
1576168404Spjd * Called in syncing context to free committed log blocks and update log header.
1577168404Spjd */
1578168404Spjdvoid
1579168404Spjdzil_sync(zilog_t *zilog, dmu_tx_t *tx)
1580168404Spjd{
1581168404Spjd	zil_header_t *zh = zil_header_in_syncing_context(zilog);
1582168404Spjd	uint64_t txg = dmu_tx_get_txg(tx);
1583168404Spjd	spa_t *spa = zilog->zl_spa;
1584219089Spjd	uint64_t *replayed_seq = &zilog->zl_replayed_seq[txg & TXG_MASK];
1585168404Spjd	lwb_t *lwb;
1586168404Spjd
1587209962Smm	/*
1588209962Smm	 * We don't zero out zl_destroy_txg, so make sure we don't try
1589209962Smm	 * to destroy it twice.
1590209962Smm	 */
1591209962Smm	if (spa_sync_pass(spa) != 1)
1592209962Smm		return;
1593209962Smm
1594168404Spjd	mutex_enter(&zilog->zl_lock);
1595168404Spjd
1596168404Spjd	ASSERT(zilog->zl_stop_sync == 0);
1597168404Spjd
1598219089Spjd	if (*replayed_seq != 0) {
1599219089Spjd		ASSERT(zh->zh_replay_seq < *replayed_seq);
1600219089Spjd		zh->zh_replay_seq = *replayed_seq;
1601219089Spjd		*replayed_seq = 0;
1602219089Spjd	}
1603168404Spjd
1604168404Spjd	if (zilog->zl_destroy_txg == txg) {
1605168404Spjd		blkptr_t blk = zh->zh_log;
1606168404Spjd
1607168404Spjd		ASSERT(list_head(&zilog->zl_lwb_list) == NULL);
1608168404Spjd
1609168404Spjd		bzero(zh, sizeof (zil_header_t));
1610209962Smm		bzero(zilog->zl_replayed_seq, sizeof (zilog->zl_replayed_seq));
1611168404Spjd
1612168404Spjd		if (zilog->zl_keep_first) {
1613168404Spjd			/*
1614168404Spjd			 * If this block was part of log chain that couldn't
1615168404Spjd			 * be claimed because a device was missing during
1616168404Spjd			 * zil_claim(), but that device later returns,
1617168404Spjd			 * then this block could erroneously appear valid.
1618168404Spjd			 * To guard against this, assign a new GUID to the new
1619168404Spjd			 * log chain so it doesn't matter what blk points to.
1620168404Spjd			 */
1621168404Spjd			zil_init_log_chain(zilog, &blk);
1622168404Spjd			zh->zh_log = blk;
1623168404Spjd		}
1624168404Spjd	}
1625168404Spjd
1626213197Smm	while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
1627168404Spjd		zh->zh_log = lwb->lwb_blk;
1628168404Spjd		if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg)
1629168404Spjd			break;
1630168404Spjd		list_remove(&zilog->zl_lwb_list, lwb);
1631219089Spjd		zio_free_zil(spa, txg, &lwb->lwb_blk);
1632168404Spjd		kmem_cache_free(zil_lwb_cache, lwb);
1633168404Spjd
1634168404Spjd		/*
1635168404Spjd		 * If we don't have anything left in the lwb list then
1636168404Spjd		 * we've had an allocation failure and we need to zero
1637168404Spjd		 * out the zil_header blkptr so that we don't end
1638168404Spjd		 * up freeing the same block twice.
1639168404Spjd		 */
1640168404Spjd		if (list_head(&zilog->zl_lwb_list) == NULL)
1641168404Spjd			BP_ZERO(&zh->zh_log);
1642168404Spjd	}
1643168404Spjd	mutex_exit(&zilog->zl_lock);
1644168404Spjd}
1645168404Spjd
1646168404Spjdvoid
1647168404Spjdzil_init(void)
1648168404Spjd{
1649168404Spjd	zil_lwb_cache = kmem_cache_create("zil_lwb_cache",
1650168404Spjd	    sizeof (struct lwb), 0, NULL, NULL, NULL, NULL, NULL, 0);
1651168404Spjd}
1652168404Spjd
1653168404Spjdvoid
1654168404Spjdzil_fini(void)
1655168404Spjd{
1656168404Spjd	kmem_cache_destroy(zil_lwb_cache);
1657168404Spjd}
1658168404Spjd
1659219089Spjdvoid
1660219089Spjdzil_set_sync(zilog_t *zilog, uint64_t sync)
1661219089Spjd{
1662219089Spjd	zilog->zl_sync = sync;
1663219089Spjd}
1664219089Spjd
1665219089Spjdvoid
1666219089Spjdzil_set_logbias(zilog_t *zilog, uint64_t logbias)
1667219089Spjd{
1668219089Spjd	zilog->zl_logbias = logbias;
1669219089Spjd}
1670219089Spjd
1671168404Spjdzilog_t *
1672168404Spjdzil_alloc(objset_t *os, zil_header_t *zh_phys)
1673168404Spjd{
1674168404Spjd	zilog_t *zilog;
1675168404Spjd
1676168404Spjd	zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP);
1677168404Spjd
1678168404Spjd	zilog->zl_header = zh_phys;
1679168404Spjd	zilog->zl_os = os;
1680168404Spjd	zilog->zl_spa = dmu_objset_spa(os);
1681168404Spjd	zilog->zl_dmu_pool = dmu_objset_pool(os);
1682168404Spjd	zilog->zl_destroy_txg = TXG_INITIAL - 1;
1683219089Spjd	zilog->zl_logbias = dmu_objset_logbias(os);
1684219089Spjd	zilog->zl_sync = dmu_objset_syncprop(os);
1685219089Spjd	zilog->zl_next_batch = 1;
1686168404Spjd
1687168404Spjd	mutex_init(&zilog->zl_lock, NULL, MUTEX_DEFAULT, NULL);
1688168404Spjd
1689219089Spjd	for (int i = 0; i < TXG_SIZE; i++) {
1690219089Spjd		mutex_init(&zilog->zl_itxg[i].itxg_lock, NULL,
1691219089Spjd		    MUTEX_DEFAULT, NULL);
1692219089Spjd	}
1693168404Spjd
1694168404Spjd	list_create(&zilog->zl_lwb_list, sizeof (lwb_t),
1695168404Spjd	    offsetof(lwb_t, lwb_node));
1696168404Spjd
1697219089Spjd	list_create(&zilog->zl_itx_commit_list, sizeof (itx_t),
1698219089Spjd	    offsetof(itx_t, itx_node));
1699219089Spjd
1700185029Spjd	mutex_init(&zilog->zl_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
1701168404Spjd
1702185029Spjd	avl_create(&zilog->zl_vdev_tree, zil_vdev_compare,
1703185029Spjd	    sizeof (zil_vdev_node_t), offsetof(zil_vdev_node_t, zv_node));
1704185029Spjd
1705185029Spjd	cv_init(&zilog->zl_cv_writer, NULL, CV_DEFAULT, NULL);
1706185029Spjd	cv_init(&zilog->zl_cv_suspend, NULL, CV_DEFAULT, NULL);
1707219089Spjd	cv_init(&zilog->zl_cv_batch[0], NULL, CV_DEFAULT, NULL);
1708219089Spjd	cv_init(&zilog->zl_cv_batch[1], NULL, CV_DEFAULT, NULL);
1709185029Spjd
1710168404Spjd	return (zilog);
1711168404Spjd}
1712168404Spjd
1713168404Spjdvoid
1714168404Spjdzil_free(zilog_t *zilog)
1715168404Spjd{
1716168404Spjd	zilog->zl_stop_sync = 1;
1717168404Spjd
1718224526Smm	ASSERT(list_is_empty(&zilog->zl_lwb_list));
1719168404Spjd	list_destroy(&zilog->zl_lwb_list);
1720168404Spjd
1721185029Spjd	avl_destroy(&zilog->zl_vdev_tree);
1722185029Spjd	mutex_destroy(&zilog->zl_vdev_lock);
1723168404Spjd
1724219089Spjd	ASSERT(list_is_empty(&zilog->zl_itx_commit_list));
1725219089Spjd	list_destroy(&zilog->zl_itx_commit_list);
1726219089Spjd
1727219089Spjd	for (int i = 0; i < TXG_SIZE; i++) {
1728219089Spjd		/*
1729219089Spjd		 * It's possible for an itx to be generated that doesn't dirty
1730219089Spjd		 * a txg (e.g. ztest TX_TRUNCATE). So there's no zil_clean()
1731219089Spjd		 * callback to remove the entry. We remove those here.
1732219089Spjd		 *
1733219089Spjd		 * Also free up the ziltest itxs.
1734219089Spjd		 */
1735219089Spjd		if (zilog->zl_itxg[i].itxg_itxs)
1736219089Spjd			zil_itxg_clean(zilog->zl_itxg[i].itxg_itxs);
1737219089Spjd		mutex_destroy(&zilog->zl_itxg[i].itxg_lock);
1738219089Spjd	}
1739219089Spjd
1740168404Spjd	mutex_destroy(&zilog->zl_lock);
1741168404Spjd
1742185029Spjd	cv_destroy(&zilog->zl_cv_writer);
1743185029Spjd	cv_destroy(&zilog->zl_cv_suspend);
1744219089Spjd	cv_destroy(&zilog->zl_cv_batch[0]);
1745219089Spjd	cv_destroy(&zilog->zl_cv_batch[1]);
1746185029Spjd
1747168404Spjd	kmem_free(zilog, sizeof (zilog_t));
1748168404Spjd}
1749168404Spjd
1750168404Spjd/*
1751168404Spjd * Open an intent log.
1752168404Spjd */
1753168404Spjdzilog_t *
1754168404Spjdzil_open(objset_t *os, zil_get_data_t *get_data)
1755168404Spjd{
1756168404Spjd	zilog_t *zilog = dmu_objset_zil(os);
1757168404Spjd
1758224526Smm	ASSERT(zilog->zl_clean_taskq == NULL);
1759224526Smm	ASSERT(zilog->zl_get_data == NULL);
1760224526Smm	ASSERT(list_is_empty(&zilog->zl_lwb_list));
1761224526Smm
1762168404Spjd	zilog->zl_get_data = get_data;
1763168404Spjd	zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri,
1764168404Spjd	    2, 2, TASKQ_PREPOPULATE);
1765168404Spjd
1766168404Spjd	return (zilog);
1767168404Spjd}
1768168404Spjd
1769168404Spjd/*
1770168404Spjd * Close an intent log.
1771168404Spjd */
1772168404Spjdvoid
1773168404Spjdzil_close(zilog_t *zilog)
1774168404Spjd{
1775224526Smm	lwb_t *lwb;
1776219089Spjd	uint64_t txg = 0;
1777219089Spjd
1778219089Spjd	zil_commit(zilog, 0); /* commit all itx */
1779219089Spjd
1780168404Spjd	/*
1781219089Spjd	 * The lwb_max_txg for the stubby lwb will reflect the last activity
1782219089Spjd	 * for the zil.  After a txg_wait_synced() on the txg we know all the
1783219089Spjd	 * callbacks have occurred that may clean the zil.  Only then can we
1784219089Spjd	 * destroy the zl_clean_taskq.
1785168404Spjd	 */
1786219089Spjd	mutex_enter(&zilog->zl_lock);
1787224526Smm	lwb = list_tail(&zilog->zl_lwb_list);
1788224526Smm	if (lwb != NULL)
1789224526Smm		txg = lwb->lwb_max_txg;
1790219089Spjd	mutex_exit(&zilog->zl_lock);
1791219089Spjd	if (txg)
1792168404Spjd		txg_wait_synced(zilog->zl_dmu_pool, txg);
1793243674Smm	ASSERT(!zilog_is_dirty(zilog));
1794168404Spjd
1795168404Spjd	taskq_destroy(zilog->zl_clean_taskq);
1796168404Spjd	zilog->zl_clean_taskq = NULL;
1797168404Spjd	zilog->zl_get_data = NULL;
1798224526Smm
1799224526Smm	/*
1800224526Smm	 * We should have only one LWB left on the list; remove it now.
1801224526Smm	 */
1802224526Smm	mutex_enter(&zilog->zl_lock);
1803224526Smm	lwb = list_head(&zilog->zl_lwb_list);
1804224526Smm	if (lwb != NULL) {
1805224526Smm		ASSERT(lwb == list_tail(&zilog->zl_lwb_list));
1806224526Smm		list_remove(&zilog->zl_lwb_list, lwb);
1807224526Smm		zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
1808224526Smm		kmem_cache_free(zil_lwb_cache, lwb);
1809224526Smm	}
1810224526Smm	mutex_exit(&zilog->zl_lock);
1811168404Spjd}
1812168404Spjd
1813168404Spjd/*
1814168404Spjd * Suspend an intent log.  While in suspended mode, we still honor
1815168404Spjd * synchronous semantics, but we rely on txg_wait_synced() to do it.
1816168404Spjd * We suspend the log briefly when taking a snapshot so that the snapshot
1817168404Spjd * contains all the data it's supposed to, and has an empty intent log.
1818168404Spjd */
1819168404Spjdint
1820168404Spjdzil_suspend(zilog_t *zilog)
1821168404Spjd{
1822168404Spjd	const zil_header_t *zh = zilog->zl_header;
1823168404Spjd
1824168404Spjd	mutex_enter(&zilog->zl_lock);
1825200724Sdelphij	if (zh->zh_flags & ZIL_REPLAY_NEEDED) {		/* unplayed log */
1826168404Spjd		mutex_exit(&zilog->zl_lock);
1827168404Spjd		return (EBUSY);
1828168404Spjd	}
1829168404Spjd	if (zilog->zl_suspend++ != 0) {
1830168404Spjd		/*
1831168404Spjd		 * Someone else already began a suspend.
1832168404Spjd		 * Just wait for them to finish.
1833168404Spjd		 */
1834168404Spjd		while (zilog->zl_suspending)
1835168404Spjd			cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock);
1836168404Spjd		mutex_exit(&zilog->zl_lock);
1837168404Spjd		return (0);
1838168404Spjd	}
1839168404Spjd	zilog->zl_suspending = B_TRUE;
1840168404Spjd	mutex_exit(&zilog->zl_lock);
1841168404Spjd
1842219089Spjd	zil_commit(zilog, 0);
1843168404Spjd
1844168404Spjd	zil_destroy(zilog, B_FALSE);
1845168404Spjd
1846168404Spjd	mutex_enter(&zilog->zl_lock);
1847168404Spjd	zilog->zl_suspending = B_FALSE;
1848168404Spjd	cv_broadcast(&zilog->zl_cv_suspend);
1849168404Spjd	mutex_exit(&zilog->zl_lock);
1850168404Spjd
1851168404Spjd	return (0);
1852168404Spjd}
1853168404Spjd
1854168404Spjdvoid
1855168404Spjdzil_resume(zilog_t *zilog)
1856168404Spjd{
1857168404Spjd	mutex_enter(&zilog->zl_lock);
1858168404Spjd	ASSERT(zilog->zl_suspend != 0);
1859168404Spjd	zilog->zl_suspend--;
1860168404Spjd	mutex_exit(&zilog->zl_lock);
1861168404Spjd}
1862168404Spjd
1863219089Spjdtypedef struct zil_replay_arg {
1864219089Spjd	zil_replay_func_t **zr_replay;
1865219089Spjd	void		*zr_arg;
1866219089Spjd	boolean_t	zr_byteswap;
1867219089Spjd	char		*zr_lr;
1868219089Spjd} zil_replay_arg_t;
1869219089Spjd
1870219089Spjdstatic int
1871219089Spjdzil_replay_error(zilog_t *zilog, lr_t *lr, int error)
1872209962Smm{
1873219089Spjd	char name[MAXNAMELEN];
1874209962Smm
1875219089Spjd	zilog->zl_replaying_seq--;	/* didn't actually replay this one */
1876209962Smm
1877219089Spjd	dmu_objset_name(zilog->zl_os, name);
1878209962Smm
1879219089Spjd	cmn_err(CE_WARN, "ZFS replay transaction error %d, "
1880219089Spjd	    "dataset %s, seq 0x%llx, txtype %llu %s\n", error, name,
1881219089Spjd	    (u_longlong_t)lr->lrc_seq,
1882219089Spjd	    (u_longlong_t)(lr->lrc_txtype & ~TX_CI),
1883219089Spjd	    (lr->lrc_txtype & TX_CI) ? "CI" : "");
1884219089Spjd
1885219089Spjd	return (error);
1886209962Smm}
1887209962Smm
1888219089Spjdstatic int
1889168404Spjdzil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg)
1890168404Spjd{
1891168404Spjd	zil_replay_arg_t *zr = zra;
1892168404Spjd	const zil_header_t *zh = zilog->zl_header;
1893168404Spjd	uint64_t reclen = lr->lrc_reclen;
1894168404Spjd	uint64_t txtype = lr->lrc_txtype;
1895219089Spjd	int error = 0;
1896168404Spjd
1897219089Spjd	zilog->zl_replaying_seq = lr->lrc_seq;
1898168404Spjd
1899219089Spjd	if (lr->lrc_seq <= zh->zh_replay_seq)	/* already replayed */
1900219089Spjd		return (0);
1901219089Spjd
1902168404Spjd	if (lr->lrc_txg < claim_txg)		/* already committed */
1903219089Spjd		return (0);
1904168404Spjd
1905185029Spjd	/* Strip case-insensitive bit, still present in log record */
1906185029Spjd	txtype &= ~TX_CI;
1907185029Spjd
1908219089Spjd	if (txtype == 0 || txtype >= TX_MAX_TYPE)
1909219089Spjd		return (zil_replay_error(zilog, lr, EINVAL));
1910219089Spjd
1911219089Spjd	/*
1912219089Spjd	 * If this record type can be logged out of order, the object
1913219089Spjd	 * (lr_foid) may no longer exist.  That's legitimate, not an error.
1914219089Spjd	 */
1915219089Spjd	if (TX_OOO(txtype)) {
1916219089Spjd		error = dmu_object_info(zilog->zl_os,
1917219089Spjd		    ((lr_ooo_t *)lr)->lr_foid, NULL);
1918219089Spjd		if (error == ENOENT || error == EEXIST)
1919219089Spjd			return (0);
1920209962Smm	}
1921209962Smm
1922168404Spjd	/*
1923168404Spjd	 * Make a copy of the data so we can revise and extend it.
1924168404Spjd	 */
1925219089Spjd	bcopy(lr, zr->zr_lr, reclen);
1926168404Spjd
1927168404Spjd	/*
1928219089Spjd	 * If this is a TX_WRITE with a blkptr, suck in the data.
1929219089Spjd	 */
1930219089Spjd	if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) {
1931219089Spjd		error = zil_read_log_data(zilog, (lr_write_t *)lr,
1932219089Spjd		    zr->zr_lr + reclen);
1933219089Spjd		if (error)
1934219089Spjd			return (zil_replay_error(zilog, lr, error));
1935219089Spjd	}
1936219089Spjd
1937219089Spjd	/*
1938168404Spjd	 * The log block containing this lr may have been byteswapped
1939168404Spjd	 * so that we can easily examine common fields like lrc_txtype.
1940219089Spjd	 * However, the log is a mix of different record types, and only the
1941168404Spjd	 * replay vectors know how to byteswap their records.  Therefore, if
1942168404Spjd	 * the lr was byteswapped, undo it before invoking the replay vector.
1943168404Spjd	 */
1944168404Spjd	if (zr->zr_byteswap)
1945219089Spjd		byteswap_uint64_array(zr->zr_lr, reclen);
1946168404Spjd
1947168404Spjd	/*
1948168404Spjd	 * We must now do two things atomically: replay this log record,
1949209962Smm	 * and update the log header sequence number to reflect the fact that
1950209962Smm	 * we did so. At the end of each replay function the sequence number
1951209962Smm	 * is updated if we are in replay mode.
1952168404Spjd	 */
1953219089Spjd	error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lr, zr->zr_byteswap);
1954219089Spjd	if (error) {
1955168404Spjd		/*
1956168404Spjd		 * The DMU's dnode layer doesn't see removes until the txg
1957168404Spjd		 * commits, so a subsequent claim can spuriously fail with
1958209962Smm		 * EEXIST. So if we receive any error we try syncing out
1959219089Spjd		 * any removes then retry the transaction.  Note that we
1960219089Spjd		 * specify B_FALSE for byteswap now, so we don't do it twice.
1961168404Spjd		 */
1962219089Spjd		txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0);
1963219089Spjd		error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lr, B_FALSE);
1964219089Spjd		if (error)
1965219089Spjd			return (zil_replay_error(zilog, lr, error));
1966168404Spjd	}
1967219089Spjd	return (0);
1968168404Spjd}
1969168404Spjd
1970168404Spjd/* ARGSUSED */
1971219089Spjdstatic int
1972168404Spjdzil_incr_blks(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
1973168404Spjd{
1974168404Spjd	zilog->zl_replay_blks++;
1975219089Spjd
1976219089Spjd	return (0);
1977168404Spjd}
1978168404Spjd
1979168404Spjd/*
1980168404Spjd * If this dataset has a non-empty intent log, replay it and destroy it.
1981168404Spjd */
1982168404Spjdvoid
1983209962Smmzil_replay(objset_t *os, void *arg, zil_replay_func_t *replay_func[TX_MAX_TYPE])
1984168404Spjd{
1985168404Spjd	zilog_t *zilog = dmu_objset_zil(os);
1986168404Spjd	const zil_header_t *zh = zilog->zl_header;
1987168404Spjd	zil_replay_arg_t zr;
1988168404Spjd
1989200724Sdelphij	if ((zh->zh_flags & ZIL_REPLAY_NEEDED) == 0) {
1990168404Spjd		zil_destroy(zilog, B_TRUE);
1991168404Spjd		return;
1992168404Spjd	}
1993168404Spjd	//printf("ZFS: Replaying ZIL on %s...\n", os->os->os_spa->spa_name);
1994168404Spjd
1995168404Spjd	zr.zr_replay = replay_func;
1996168404Spjd	zr.zr_arg = arg;
1997168404Spjd	zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log);
1998219089Spjd	zr.zr_lr = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP);
1999168404Spjd
2000168404Spjd	/*
2001168404Spjd	 * Wait for in-progress removes to sync before starting replay.
2002168404Spjd	 */
2003168404Spjd	txg_wait_synced(zilog->zl_dmu_pool, 0);
2004168404Spjd
2005209962Smm	zilog->zl_replay = B_TRUE;
2006219089Spjd	zilog->zl_replay_time = ddi_get_lbolt();
2007168404Spjd	ASSERT(zilog->zl_replay_blks == 0);
2008168404Spjd	(void) zil_parse(zilog, zil_incr_blks, zil_replay_log_record, &zr,
2009168404Spjd	    zh->zh_claim_txg);
2010219089Spjd	kmem_free(zr.zr_lr, 2 * SPA_MAXBLOCKSIZE);
2011168404Spjd
2012168404Spjd	zil_destroy(zilog, B_FALSE);
2013185029Spjd	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
2014209962Smm	zilog->zl_replay = B_FALSE;
2015168404Spjd	//printf("ZFS: Replay of ZIL on %s finished.\n", os->os->os_spa->spa_name);
2016168404Spjd}
2017168404Spjd
2018219089Spjdboolean_t
2019219089Spjdzil_replaying(zilog_t *zilog, dmu_tx_t *tx)
2020168404Spjd{
2021219089Spjd	if (zilog->zl_sync == ZFS_SYNC_DISABLED)
2022219089Spjd		return (B_TRUE);
2023168404Spjd
2024219089Spjd	if (zilog->zl_replay) {
2025219089Spjd		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
2026219089Spjd		zilog->zl_replayed_seq[dmu_tx_get_txg(tx) & TXG_MASK] =
2027219089Spjd		    zilog->zl_replaying_seq;
2028219089Spjd		return (B_TRUE);
2029168404Spjd	}
2030168404Spjd
2031219089Spjd	return (B_FALSE);
2032168404Spjd}
2033213197Smm
2034213197Smm/* ARGSUSED */
2035213197Smmint
2036219089Spjdzil_vdev_offline(const char *osname, void *arg)
2037213197Smm{
2038213197Smm	objset_t *os;
2039213197Smm	zilog_t *zilog;
2040213197Smm	int error;
2041213197Smm
2042219089Spjd	error = dmu_objset_hold(osname, FTAG, &os);
2043213197Smm	if (error)
2044213197Smm		return (error);
2045213197Smm
2046213197Smm	zilog = dmu_objset_zil(os);
2047213197Smm	if (zil_suspend(zilog) != 0)
2048213197Smm		error = EEXIST;
2049213197Smm	else
2050213197Smm		zil_resume(zilog);
2051219089Spjd	dmu_objset_rele(os, FTAG);
2052213197Smm	return (error);
2053213197Smm}
2054