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.
23249643Smm * Copyright (c) 2013 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/*
69252751Sdelphij * Disable intent logging replay.  This global ZIL switch affects all pools.
70168404Spjd */
71252751Sdelphijint zil_replay_disable = 0;
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");
86251419Ssmhboolean_t zfs_trim_enabled = B_TRUE;
87251419SsmhSYSCTL_DECL(_vfs_zfs_trim);
88251419SsmhTUNABLE_INT("vfs.zfs.trim.enabled", &zfs_trim_enabled);
89251419SsmhSYSCTL_INT(_vfs_zfs_trim, OID_AUTO, enabled, CTLFLAG_RDTUN, &zfs_trim_enabled, 0,
90251419Ssmh    "Enable ZFS TRIM");
91168404Spjd
92168404Spjdstatic kmem_cache_t *zil_lwb_cache;
93168404Spjd
94219089Spjdstatic void zil_async_to_sync(zilog_t *zilog, uint64_t foid);
95219089Spjd
96219089Spjd#define	LWB_EMPTY(lwb) ((BP_GET_LSIZE(&lwb->lwb_blk) - \
97219089Spjd    sizeof (zil_chain_t)) == (lwb->lwb_sz - lwb->lwb_nused))
98219089Spjd
99219089Spjd
100219089Spjd/*
101219089Spjd * ziltest is by and large an ugly hack, but very useful in
102219089Spjd * checking replay without tedious work.
103219089Spjd * When running ziltest we want to keep all itx's and so maintain
104219089Spjd * a single list in the zl_itxg[] that uses a high txg: ZILTEST_TXG
105219089Spjd * We subtract TXG_CONCURRENT_STATES to allow for common code.
106219089Spjd */
107219089Spjd#define	ZILTEST_TXG (UINT64_MAX - TXG_CONCURRENT_STATES)
108219089Spjd
109168404Spjdstatic int
110219089Spjdzil_bp_compare(const void *x1, const void *x2)
111168404Spjd{
112219089Spjd	const dva_t *dva1 = &((zil_bp_node_t *)x1)->zn_dva;
113219089Spjd	const dva_t *dva2 = &((zil_bp_node_t *)x2)->zn_dva;
114168404Spjd
115168404Spjd	if (DVA_GET_VDEV(dva1) < DVA_GET_VDEV(dva2))
116168404Spjd		return (-1);
117168404Spjd	if (DVA_GET_VDEV(dva1) > DVA_GET_VDEV(dva2))
118168404Spjd		return (1);
119168404Spjd
120168404Spjd	if (DVA_GET_OFFSET(dva1) < DVA_GET_OFFSET(dva2))
121168404Spjd		return (-1);
122168404Spjd	if (DVA_GET_OFFSET(dva1) > DVA_GET_OFFSET(dva2))
123168404Spjd		return (1);
124168404Spjd
125168404Spjd	return (0);
126168404Spjd}
127168404Spjd
128168404Spjdstatic void
129219089Spjdzil_bp_tree_init(zilog_t *zilog)
130168404Spjd{
131219089Spjd	avl_create(&zilog->zl_bp_tree, zil_bp_compare,
132219089Spjd	    sizeof (zil_bp_node_t), offsetof(zil_bp_node_t, zn_node));
133168404Spjd}
134168404Spjd
135168404Spjdstatic void
136219089Spjdzil_bp_tree_fini(zilog_t *zilog)
137168404Spjd{
138219089Spjd	avl_tree_t *t = &zilog->zl_bp_tree;
139219089Spjd	zil_bp_node_t *zn;
140168404Spjd	void *cookie = NULL;
141168404Spjd
142168404Spjd	while ((zn = avl_destroy_nodes(t, &cookie)) != NULL)
143219089Spjd		kmem_free(zn, sizeof (zil_bp_node_t));
144168404Spjd
145168404Spjd	avl_destroy(t);
146168404Spjd}
147168404Spjd
148219089Spjdint
149219089Spjdzil_bp_tree_add(zilog_t *zilog, const blkptr_t *bp)
150168404Spjd{
151219089Spjd	avl_tree_t *t = &zilog->zl_bp_tree;
152219089Spjd	const dva_t *dva = BP_IDENTITY(bp);
153219089Spjd	zil_bp_node_t *zn;
154168404Spjd	avl_index_t where;
155168404Spjd
156168404Spjd	if (avl_find(t, dva, &where) != NULL)
157249643Smm		return (SET_ERROR(EEXIST));
158168404Spjd
159219089Spjd	zn = kmem_alloc(sizeof (zil_bp_node_t), KM_SLEEP);
160168404Spjd	zn->zn_dva = *dva;
161168404Spjd	avl_insert(t, zn, where);
162168404Spjd
163168404Spjd	return (0);
164168404Spjd}
165168404Spjd
166168404Spjdstatic zil_header_t *
167168404Spjdzil_header_in_syncing_context(zilog_t *zilog)
168168404Spjd{
169168404Spjd	return ((zil_header_t *)zilog->zl_header);
170168404Spjd}
171168404Spjd
172168404Spjdstatic void
173168404Spjdzil_init_log_chain(zilog_t *zilog, blkptr_t *bp)
174168404Spjd{
175168404Spjd	zio_cksum_t *zc = &bp->blk_cksum;
176168404Spjd
177168404Spjd	zc->zc_word[ZIL_ZC_GUID_0] = spa_get_random(-1ULL);
178168404Spjd	zc->zc_word[ZIL_ZC_GUID_1] = spa_get_random(-1ULL);
179168404Spjd	zc->zc_word[ZIL_ZC_OBJSET] = dmu_objset_id(zilog->zl_os);
180168404Spjd	zc->zc_word[ZIL_ZC_SEQ] = 1ULL;
181168404Spjd}
182168404Spjd
183168404Spjd/*
184219089Spjd * Read a log block and make sure it's valid.
185168404Spjd */
186168404Spjdstatic int
187219089Spjdzil_read_log_block(zilog_t *zilog, const blkptr_t *bp, blkptr_t *nbp, void *dst,
188219089Spjd    char **end)
189168404Spjd{
190219089Spjd	enum zio_flag zio_flags = ZIO_FLAG_CANFAIL;
191219089Spjd	uint32_t aflags = ARC_WAIT;
192219089Spjd	arc_buf_t *abuf = NULL;
193168404Spjd	zbookmark_t zb;
194168404Spjd	int error;
195168404Spjd
196219089Spjd	if (zilog->zl_header->zh_claim_txg == 0)
197219089Spjd		zio_flags |= ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB;
198168404Spjd
199219089Spjd	if (!(zilog->zl_header->zh_flags & ZIL_CLAIM_LR_SEQ_VALID))
200219089Spjd		zio_flags |= ZIO_FLAG_SPECULATIVE;
201168404Spjd
202219089Spjd	SET_BOOKMARK(&zb, bp->blk_cksum.zc_word[ZIL_ZC_OBJSET],
203219089Spjd	    ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, bp->blk_cksum.zc_word[ZIL_ZC_SEQ]);
204168404Spjd
205247406Smm	error = arc_read(NULL, zilog->zl_spa, bp, arc_getbuf_func, &abuf,
206219089Spjd	    ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb);
207219089Spjd
208168404Spjd	if (error == 0) {
209168404Spjd		zio_cksum_t cksum = bp->blk_cksum;
210168404Spjd
211168404Spjd		/*
212185029Spjd		 * Validate the checksummed log block.
213185029Spjd		 *
214168404Spjd		 * Sequence numbers should be... sequential.  The checksum
215168404Spjd		 * verifier for the next block should be bp's checksum plus 1.
216185029Spjd		 *
217185029Spjd		 * Also check the log chain linkage and size used.
218168404Spjd		 */
219168404Spjd		cksum.zc_word[ZIL_ZC_SEQ]++;
220168404Spjd
221219089Spjd		if (BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_ZILOG2) {
222219089Spjd			zil_chain_t *zilc = abuf->b_data;
223219089Spjd			char *lr = (char *)(zilc + 1);
224219089Spjd			uint64_t len = zilc->zc_nused - sizeof (zil_chain_t);
225219089Spjd
226219089Spjd			if (bcmp(&cksum, &zilc->zc_next_blk.blk_cksum,
227219089Spjd			    sizeof (cksum)) || BP_IS_HOLE(&zilc->zc_next_blk)) {
228249643Smm				error = SET_ERROR(ECKSUM);
229219089Spjd			} else {
230219089Spjd				bcopy(lr, dst, len);
231219089Spjd				*end = (char *)dst + len;
232219089Spjd				*nbp = zilc->zc_next_blk;
233219089Spjd			}
234219089Spjd		} else {
235219089Spjd			char *lr = abuf->b_data;
236219089Spjd			uint64_t size = BP_GET_LSIZE(bp);
237219089Spjd			zil_chain_t *zilc = (zil_chain_t *)(lr + size) - 1;
238219089Spjd
239219089Spjd			if (bcmp(&cksum, &zilc->zc_next_blk.blk_cksum,
240219089Spjd			    sizeof (cksum)) || BP_IS_HOLE(&zilc->zc_next_blk) ||
241219089Spjd			    (zilc->zc_nused > (size - sizeof (*zilc)))) {
242249643Smm				error = SET_ERROR(ECKSUM);
243219089Spjd			} else {
244219089Spjd				bcopy(lr, dst, zilc->zc_nused);
245219089Spjd				*end = (char *)dst + zilc->zc_nused;
246219089Spjd				*nbp = zilc->zc_next_blk;
247219089Spjd			}
248185029Spjd		}
249168404Spjd
250249643Smm		VERIFY(arc_buf_remove_ref(abuf, &abuf));
251168404Spjd	}
252168404Spjd
253219089Spjd	return (error);
254219089Spjd}
255168404Spjd
256219089Spjd/*
257219089Spjd * Read a TX_WRITE log data block.
258219089Spjd */
259219089Spjdstatic int
260219089Spjdzil_read_log_data(zilog_t *zilog, const lr_write_t *lr, void *wbuf)
261219089Spjd{
262219089Spjd	enum zio_flag zio_flags = ZIO_FLAG_CANFAIL;
263219089Spjd	const blkptr_t *bp = &lr->lr_blkptr;
264219089Spjd	uint32_t aflags = ARC_WAIT;
265219089Spjd	arc_buf_t *abuf = NULL;
266219089Spjd	zbookmark_t zb;
267219089Spjd	int error;
268219089Spjd
269219089Spjd	if (BP_IS_HOLE(bp)) {
270219089Spjd		if (wbuf != NULL)
271219089Spjd			bzero(wbuf, MAX(BP_GET_LSIZE(bp), lr->lr_length));
272219089Spjd		return (0);
273219089Spjd	}
274219089Spjd
275219089Spjd	if (zilog->zl_header->zh_claim_txg == 0)
276219089Spjd		zio_flags |= ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB;
277219089Spjd
278219089Spjd	SET_BOOKMARK(&zb, dmu_objset_id(zilog->zl_os), lr->lr_foid,
279219089Spjd	    ZB_ZIL_LEVEL, lr->lr_offset / BP_GET_LSIZE(bp));
280219089Spjd
281247406Smm	error = arc_read(NULL, zilog->zl_spa, bp, arc_getbuf_func, &abuf,
282219089Spjd	    ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb);
283219089Spjd
284219089Spjd	if (error == 0) {
285219089Spjd		if (wbuf != NULL)
286219089Spjd			bcopy(abuf->b_data, wbuf, arc_buf_size(abuf));
287219089Spjd		(void) arc_buf_remove_ref(abuf, &abuf);
288219089Spjd	}
289219089Spjd
290168404Spjd	return (error);
291168404Spjd}
292168404Spjd
293168404Spjd/*
294168404Spjd * Parse the intent log, and call parse_func for each valid record within.
295168404Spjd */
296219089Spjdint
297168404Spjdzil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func,
298168404Spjd    zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg)
299168404Spjd{
300168404Spjd	const zil_header_t *zh = zilog->zl_header;
301219089Spjd	boolean_t claimed = !!zh->zh_claim_txg;
302219089Spjd	uint64_t claim_blk_seq = claimed ? zh->zh_claim_blk_seq : UINT64_MAX;
303219089Spjd	uint64_t claim_lr_seq = claimed ? zh->zh_claim_lr_seq : UINT64_MAX;
304219089Spjd	uint64_t max_blk_seq = 0;
305219089Spjd	uint64_t max_lr_seq = 0;
306219089Spjd	uint64_t blk_count = 0;
307219089Spjd	uint64_t lr_count = 0;
308219089Spjd	blkptr_t blk, next_blk;
309168404Spjd	char *lrbuf, *lrp;
310219089Spjd	int error = 0;
311168404Spjd
312219089Spjd	/*
313219089Spjd	 * Old logs didn't record the maximum zh_claim_lr_seq.
314219089Spjd	 */
315219089Spjd	if (!(zh->zh_flags & ZIL_CLAIM_LR_SEQ_VALID))
316219089Spjd		claim_lr_seq = UINT64_MAX;
317168404Spjd
318168404Spjd	/*
319168404Spjd	 * Starting at the block pointed to by zh_log we read the log chain.
320168404Spjd	 * For each block in the chain we strongly check that block to
321168404Spjd	 * ensure its validity.  We stop when an invalid block is found.
322168404Spjd	 * For each block pointer in the chain we call parse_blk_func().
323168404Spjd	 * For each record in each valid block we call parse_lr_func().
324168404Spjd	 * If the log has been claimed, stop if we encounter a sequence
325168404Spjd	 * number greater than the highest claimed sequence number.
326168404Spjd	 */
327219089Spjd	lrbuf = zio_buf_alloc(SPA_MAXBLOCKSIZE);
328219089Spjd	zil_bp_tree_init(zilog);
329168404Spjd
330219089Spjd	for (blk = zh->zh_log; !BP_IS_HOLE(&blk); blk = next_blk) {
331219089Spjd		uint64_t blk_seq = blk.blk_cksum.zc_word[ZIL_ZC_SEQ];
332219089Spjd		int reclen;
333219089Spjd		char *end;
334219089Spjd
335219089Spjd		if (blk_seq > claim_blk_seq)
336168404Spjd			break;
337219089Spjd		if ((error = parse_blk_func(zilog, &blk, arg, txg)) != 0)
338219089Spjd			break;
339219089Spjd		ASSERT3U(max_blk_seq, <, blk_seq);
340219089Spjd		max_blk_seq = blk_seq;
341219089Spjd		blk_count++;
342168404Spjd
343219089Spjd		if (max_lr_seq == claim_lr_seq && max_blk_seq == claim_blk_seq)
344219089Spjd			break;
345168404Spjd
346219089Spjd		error = zil_read_log_block(zilog, &blk, &next_blk, lrbuf, &end);
347249643Smm		if (error != 0)
348168404Spjd			break;
349168404Spjd
350219089Spjd		for (lrp = lrbuf; lrp < end; lrp += reclen) {
351168404Spjd			lr_t *lr = (lr_t *)lrp;
352168404Spjd			reclen = lr->lrc_reclen;
353168404Spjd			ASSERT3U(reclen, >=, sizeof (lr_t));
354219089Spjd			if (lr->lrc_seq > claim_lr_seq)
355219089Spjd				goto done;
356219089Spjd			if ((error = parse_lr_func(zilog, lr, arg, txg)) != 0)
357219089Spjd				goto done;
358219089Spjd			ASSERT3U(max_lr_seq, <, lr->lrc_seq);
359219089Spjd			max_lr_seq = lr->lrc_seq;
360219089Spjd			lr_count++;
361168404Spjd		}
362168404Spjd	}
363219089Spjddone:
364219089Spjd	zilog->zl_parse_error = error;
365219089Spjd	zilog->zl_parse_blk_seq = max_blk_seq;
366219089Spjd	zilog->zl_parse_lr_seq = max_lr_seq;
367219089Spjd	zilog->zl_parse_blk_count = blk_count;
368219089Spjd	zilog->zl_parse_lr_count = lr_count;
369168404Spjd
370219089Spjd	ASSERT(!claimed || !(zh->zh_flags & ZIL_CLAIM_LR_SEQ_VALID) ||
371219089Spjd	    (max_blk_seq == claim_blk_seq && max_lr_seq == claim_lr_seq));
372219089Spjd
373219089Spjd	zil_bp_tree_fini(zilog);
374219089Spjd	zio_buf_free(lrbuf, SPA_MAXBLOCKSIZE);
375219089Spjd
376219089Spjd	return (error);
377168404Spjd}
378168404Spjd
379219089Spjdstatic int
380168404Spjdzil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg)
381168404Spjd{
382168404Spjd	/*
383168404Spjd	 * Claim log block if not already committed and not already claimed.
384219089Spjd	 * If tx == NULL, just verify that the block is claimable.
385168404Spjd	 */
386263398Sdelphij	if (BP_IS_HOLE(bp) || bp->blk_birth < first_txg ||
387263398Sdelphij	    zil_bp_tree_add(zilog, bp) != 0)
388219089Spjd		return (0);
389219089Spjd
390219089Spjd	return (zio_wait(zio_claim(NULL, zilog->zl_spa,
391219089Spjd	    tx == NULL ? 0 : first_txg, bp, spa_claim_notify, NULL,
392219089Spjd	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB)));
393168404Spjd}
394168404Spjd
395219089Spjdstatic int
396168404Spjdzil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg)
397168404Spjd{
398219089Spjd	lr_write_t *lr = (lr_write_t *)lrc;
399219089Spjd	int error;
400219089Spjd
401219089Spjd	if (lrc->lrc_txtype != TX_WRITE)
402219089Spjd		return (0);
403219089Spjd
404219089Spjd	/*
405219089Spjd	 * If the block is not readable, don't claim it.  This can happen
406219089Spjd	 * in normal operation when a log block is written to disk before
407219089Spjd	 * some of the dmu_sync() blocks it points to.  In this case, the
408219089Spjd	 * transaction cannot have been committed to anyone (we would have
409219089Spjd	 * waited for all writes to be stable first), so it is semantically
410219089Spjd	 * correct to declare this the end of the log.
411219089Spjd	 */
412219089Spjd	if (lr->lr_blkptr.blk_birth >= first_txg &&
413219089Spjd	    (error = zil_read_log_data(zilog, lr, NULL)) != 0)
414219089Spjd		return (error);
415219089Spjd	return (zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg));
416168404Spjd}
417168404Spjd
418168404Spjd/* ARGSUSED */
419219089Spjdstatic int
420168404Spjdzil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg)
421168404Spjd{
422219089Spjd	zio_free_zil(zilog->zl_spa, dmu_tx_get_txg(tx), bp);
423219089Spjd
424219089Spjd	return (0);
425168404Spjd}
426168404Spjd
427219089Spjdstatic int
428168404Spjdzil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg)
429168404Spjd{
430219089Spjd	lr_write_t *lr = (lr_write_t *)lrc;
431219089Spjd	blkptr_t *bp = &lr->lr_blkptr;
432219089Spjd
433168404Spjd	/*
434168404Spjd	 * If we previously claimed it, we need to free it.
435168404Spjd	 */
436219089Spjd	if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE &&
437263398Sdelphij	    bp->blk_birth >= claim_txg && zil_bp_tree_add(zilog, bp) == 0 &&
438263398Sdelphij	    !BP_IS_HOLE(bp))
439219089Spjd		zio_free(zilog->zl_spa, dmu_tx_get_txg(tx), bp);
440219089Spjd
441219089Spjd	return (0);
442219089Spjd}
443219089Spjd
444219089Spjdstatic lwb_t *
445219089Spjdzil_alloc_lwb(zilog_t *zilog, blkptr_t *bp, uint64_t txg)
446219089Spjd{
447219089Spjd	lwb_t *lwb;
448219089Spjd
449219089Spjd	lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP);
450219089Spjd	lwb->lwb_zilog = zilog;
451219089Spjd	lwb->lwb_blk = *bp;
452219089Spjd	lwb->lwb_buf = zio_buf_alloc(BP_GET_LSIZE(bp));
453219089Spjd	lwb->lwb_max_txg = txg;
454219089Spjd	lwb->lwb_zio = NULL;
455219089Spjd	lwb->lwb_tx = NULL;
456219089Spjd	if (BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_ZILOG2) {
457219089Spjd		lwb->lwb_nused = sizeof (zil_chain_t);
458219089Spjd		lwb->lwb_sz = BP_GET_LSIZE(bp);
459219089Spjd	} else {
460219089Spjd		lwb->lwb_nused = 0;
461219089Spjd		lwb->lwb_sz = BP_GET_LSIZE(bp) - sizeof (zil_chain_t);
462168404Spjd	}
463219089Spjd
464219089Spjd	mutex_enter(&zilog->zl_lock);
465219089Spjd	list_insert_tail(&zilog->zl_lwb_list, lwb);
466219089Spjd	mutex_exit(&zilog->zl_lock);
467219089Spjd
468219089Spjd	return (lwb);
469168404Spjd}
470168404Spjd
471168404Spjd/*
472243674Smm * Called when we create in-memory log transactions so that we know
473243674Smm * to cleanup the itxs at the end of spa_sync().
474243674Smm */
475243674Smmvoid
476243674Smmzilog_dirty(zilog_t *zilog, uint64_t txg)
477243674Smm{
478243674Smm	dsl_pool_t *dp = zilog->zl_dmu_pool;
479243674Smm	dsl_dataset_t *ds = dmu_objset_ds(zilog->zl_os);
480243674Smm
481243674Smm	if (dsl_dataset_is_snapshot(ds))
482243674Smm		panic("dirtying snapshot!");
483243674Smm
484249643Smm	if (txg_list_add(&dp->dp_dirty_zilogs, zilog, txg)) {
485243674Smm		/* up the hold count until we can be written out */
486243674Smm		dmu_buf_add_ref(ds->ds_dbuf, zilog);
487243674Smm	}
488243674Smm}
489243674Smm
490243674Smmboolean_t
491243674Smmzilog_is_dirty(zilog_t *zilog)
492243674Smm{
493243674Smm	dsl_pool_t *dp = zilog->zl_dmu_pool;
494243674Smm
495243674Smm	for (int t = 0; t < TXG_SIZE; t++) {
496243674Smm		if (txg_list_member(&dp->dp_dirty_zilogs, zilog, t))
497243674Smm			return (B_TRUE);
498243674Smm	}
499243674Smm	return (B_FALSE);
500243674Smm}
501243674Smm
502243674Smm/*
503168404Spjd * Create an on-disk intent log.
504168404Spjd */
505219089Spjdstatic lwb_t *
506168404Spjdzil_create(zilog_t *zilog)
507168404Spjd{
508168404Spjd	const zil_header_t *zh = zilog->zl_header;
509219089Spjd	lwb_t *lwb = NULL;
510168404Spjd	uint64_t txg = 0;
511168404Spjd	dmu_tx_t *tx = NULL;
512168404Spjd	blkptr_t blk;
513168404Spjd	int error = 0;
514168404Spjd
515168404Spjd	/*
516168404Spjd	 * Wait for any previous destroy to complete.
517168404Spjd	 */
518168404Spjd	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
519168404Spjd
520168404Spjd	ASSERT(zh->zh_claim_txg == 0);
521168404Spjd	ASSERT(zh->zh_replay_seq == 0);
522168404Spjd
523168404Spjd	blk = zh->zh_log;
524168404Spjd
525168404Spjd	/*
526219089Spjd	 * Allocate an initial log block if:
527219089Spjd	 *    - there isn't one already
528219089Spjd	 *    - the existing block is the wrong endianess
529168404Spjd	 */
530207908Smm	if (BP_IS_HOLE(&blk) || BP_SHOULD_BYTESWAP(&blk)) {
531168404Spjd		tx = dmu_tx_create(zilog->zl_os);
532219089Spjd		VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0);
533168404Spjd		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
534168404Spjd		txg = dmu_tx_get_txg(tx);
535168404Spjd
536207908Smm		if (!BP_IS_HOLE(&blk)) {
537219089Spjd			zio_free_zil(zilog->zl_spa, txg, &blk);
538207908Smm			BP_ZERO(&blk);
539207908Smm		}
540207908Smm
541219089Spjd		error = zio_alloc_zil(zilog->zl_spa, txg, &blk, NULL,
542219089Spjd		    ZIL_MIN_BLKSZ, zilog->zl_logbias == ZFS_LOGBIAS_LATENCY);
543168404Spjd
544168404Spjd		if (error == 0)
545168404Spjd			zil_init_log_chain(zilog, &blk);
546168404Spjd	}
547168404Spjd
548168404Spjd	/*
549168404Spjd	 * Allocate a log write buffer (lwb) for the first log block.
550168404Spjd	 */
551219089Spjd	if (error == 0)
552219089Spjd		lwb = zil_alloc_lwb(zilog, &blk, txg);
553168404Spjd
554168404Spjd	/*
555168404Spjd	 * If we just allocated the first log block, commit our transaction
556168404Spjd	 * and wait for zil_sync() to stuff the block poiner into zh_log.
557168404Spjd	 * (zh is part of the MOS, so we cannot modify it in open context.)
558168404Spjd	 */
559168404Spjd	if (tx != NULL) {
560168404Spjd		dmu_tx_commit(tx);
561168404Spjd		txg_wait_synced(zilog->zl_dmu_pool, txg);
562168404Spjd	}
563168404Spjd
564168404Spjd	ASSERT(bcmp(&blk, &zh->zh_log, sizeof (blk)) == 0);
565219089Spjd
566219089Spjd	return (lwb);
567168404Spjd}
568168404Spjd
569168404Spjd/*
570168404Spjd * In one tx, free all log blocks and clear the log header.
571168404Spjd * If keep_first is set, then we're replaying a log with no content.
572168404Spjd * We want to keep the first block, however, so that the first
573168404Spjd * synchronous transaction doesn't require a txg_wait_synced()
574168404Spjd * in zil_create().  We don't need to txg_wait_synced() here either
575168404Spjd * when keep_first is set, because both zil_create() and zil_destroy()
576168404Spjd * will wait for any in-progress destroys to complete.
577168404Spjd */
578168404Spjdvoid
579168404Spjdzil_destroy(zilog_t *zilog, boolean_t keep_first)
580168404Spjd{
581168404Spjd	const zil_header_t *zh = zilog->zl_header;
582168404Spjd	lwb_t *lwb;
583168404Spjd	dmu_tx_t *tx;
584168404Spjd	uint64_t txg;
585168404Spjd
586168404Spjd	/*
587168404Spjd	 * Wait for any previous destroy to complete.
588168404Spjd	 */
589168404Spjd	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
590168404Spjd
591219089Spjd	zilog->zl_old_header = *zh;		/* debugging aid */
592219089Spjd
593168404Spjd	if (BP_IS_HOLE(&zh->zh_log))
594168404Spjd		return;
595168404Spjd
596168404Spjd	tx = dmu_tx_create(zilog->zl_os);
597219089Spjd	VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0);
598168404Spjd	dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
599168404Spjd	txg = dmu_tx_get_txg(tx);
600168404Spjd
601168404Spjd	mutex_enter(&zilog->zl_lock);
602168404Spjd
603168404Spjd	ASSERT3U(zilog->zl_destroy_txg, <, txg);
604168404Spjd	zilog->zl_destroy_txg = txg;
605168404Spjd	zilog->zl_keep_first = keep_first;
606168404Spjd
607168404Spjd	if (!list_is_empty(&zilog->zl_lwb_list)) {
608168404Spjd		ASSERT(zh->zh_claim_txg == 0);
609224526Smm		VERIFY(!keep_first);
610168404Spjd		while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
611168404Spjd			list_remove(&zilog->zl_lwb_list, lwb);
612168404Spjd			if (lwb->lwb_buf != NULL)
613168404Spjd				zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
614219089Spjd			zio_free_zil(zilog->zl_spa, txg, &lwb->lwb_blk);
615168404Spjd			kmem_cache_free(zil_lwb_cache, lwb);
616168404Spjd		}
617219089Spjd	} else if (!keep_first) {
618243674Smm		zil_destroy_sync(zilog, tx);
619168404Spjd	}
620168404Spjd	mutex_exit(&zilog->zl_lock);
621168404Spjd
622168404Spjd	dmu_tx_commit(tx);
623185029Spjd}
624168404Spjd
625243674Smmvoid
626243674Smmzil_destroy_sync(zilog_t *zilog, dmu_tx_t *tx)
627243674Smm{
628243674Smm	ASSERT(list_is_empty(&zilog->zl_lwb_list));
629243674Smm	(void) zil_parse(zilog, zil_free_log_block,
630243674Smm	    zil_free_log_record, tx, zilog->zl_header->zh_claim_txg);
631243674Smm}
632243674Smm
633168404Spjdint
634219089Spjdzil_claim(const char *osname, void *txarg)
635168404Spjd{
636168404Spjd	dmu_tx_t *tx = txarg;
637168404Spjd	uint64_t first_txg = dmu_tx_get_txg(tx);
638168404Spjd	zilog_t *zilog;
639168404Spjd	zil_header_t *zh;
640168404Spjd	objset_t *os;
641168404Spjd	int error;
642168404Spjd
643249643Smm	error = dmu_objset_own(osname, DMU_OST_ANY, B_FALSE, FTAG, &os);
644249643Smm	if (error != 0) {
645185029Spjd		cmn_err(CE_WARN, "can't open objset for %s", osname);
646168404Spjd		return (0);
647168404Spjd	}
648168404Spjd
649168404Spjd	zilog = dmu_objset_zil(os);
650168404Spjd	zh = zil_header_in_syncing_context(zilog);
651168404Spjd
652219089Spjd	if (spa_get_log_state(zilog->zl_spa) == SPA_LOG_CLEAR) {
653213197Smm		if (!BP_IS_HOLE(&zh->zh_log))
654219089Spjd			zio_free_zil(zilog->zl_spa, first_txg, &zh->zh_log);
655213197Smm		BP_ZERO(&zh->zh_log);
656213197Smm		dsl_dataset_dirty(dmu_objset_ds(os), tx);
657249643Smm		dmu_objset_disown(os, FTAG);
658219089Spjd		return (0);
659213197Smm	}
660213197Smm
661168404Spjd	/*
662168404Spjd	 * Claim all log blocks if we haven't already done so, and remember
663168404Spjd	 * the highest claimed sequence number.  This ensures that if we can
664168404Spjd	 * read only part of the log now (e.g. due to a missing device),
665168404Spjd	 * but we can read the entire log later, we will not try to replay
666168404Spjd	 * or destroy beyond the last block we successfully claimed.
667168404Spjd	 */
668168404Spjd	ASSERT3U(zh->zh_claim_txg, <=, first_txg);
669168404Spjd	if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) {
670219089Spjd		(void) zil_parse(zilog, zil_claim_log_block,
671219089Spjd		    zil_claim_log_record, tx, first_txg);
672168404Spjd		zh->zh_claim_txg = first_txg;
673219089Spjd		zh->zh_claim_blk_seq = zilog->zl_parse_blk_seq;
674219089Spjd		zh->zh_claim_lr_seq = zilog->zl_parse_lr_seq;
675219089Spjd		if (zilog->zl_parse_lr_count || zilog->zl_parse_blk_count > 1)
676219089Spjd			zh->zh_flags |= ZIL_REPLAY_NEEDED;
677219089Spjd		zh->zh_flags |= ZIL_CLAIM_LR_SEQ_VALID;
678168404Spjd		dsl_dataset_dirty(dmu_objset_ds(os), tx);
679168404Spjd	}
680168404Spjd
681168404Spjd	ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1));
682249643Smm	dmu_objset_disown(os, FTAG);
683168404Spjd	return (0);
684168404Spjd}
685168404Spjd
686185029Spjd/*
687185029Spjd * Check the log by walking the log chain.
688185029Spjd * Checksum errors are ok as they indicate the end of the chain.
689185029Spjd * Any other error (no device or read failure) returns an error.
690185029Spjd */
691185029Spjdint
692219089Spjdzil_check_log_chain(const char *osname, void *tx)
693168404Spjd{
694185029Spjd	zilog_t *zilog;
695185029Spjd	objset_t *os;
696219089Spjd	blkptr_t *bp;
697185029Spjd	int error;
698168404Spjd
699219089Spjd	ASSERT(tx == NULL);
700219089Spjd
701219089Spjd	error = dmu_objset_hold(osname, FTAG, &os);
702249643Smm	if (error != 0) {
703185029Spjd		cmn_err(CE_WARN, "can't open objset for %s", osname);
704185029Spjd		return (0);
705185029Spjd	}
706168404Spjd
707185029Spjd	zilog = dmu_objset_zil(os);
708219089Spjd	bp = (blkptr_t *)&zilog->zl_header->zh_log;
709219089Spjd
710219089Spjd	/*
711219089Spjd	 * Check the first block and determine if it's on a log device
712219089Spjd	 * which may have been removed or faulted prior to loading this
713219089Spjd	 * pool.  If so, there's no point in checking the rest of the log
714219089Spjd	 * as its content should have already been synced to the pool.
715219089Spjd	 */
716219089Spjd	if (!BP_IS_HOLE(bp)) {
717219089Spjd		vdev_t *vd;
718219089Spjd		boolean_t valid = B_TRUE;
719219089Spjd
720219089Spjd		spa_config_enter(os->os_spa, SCL_STATE, FTAG, RW_READER);
721219089Spjd		vd = vdev_lookup_top(os->os_spa, DVA_GET_VDEV(&bp->blk_dva[0]));
722219089Spjd		if (vd->vdev_islog && vdev_is_dead(vd))
723219089Spjd			valid = vdev_log_state_valid(vd);
724219089Spjd		spa_config_exit(os->os_spa, SCL_STATE, FTAG);
725219089Spjd
726219089Spjd		if (!valid) {
727219089Spjd			dmu_objset_rele(os, FTAG);
728219089Spjd			return (0);
729219089Spjd		}
730168404Spjd	}
731185029Spjd
732219089Spjd	/*
733219089Spjd	 * Because tx == NULL, zil_claim_log_block() will not actually claim
734219089Spjd	 * any blocks, but just determine whether it is possible to do so.
735219089Spjd	 * In addition to checking the log chain, zil_claim_log_block()
736219089Spjd	 * will invoke zio_claim() with a done func of spa_claim_notify(),
737219089Spjd	 * which will update spa_max_claim_txg.  See spa_load() for details.
738219089Spjd	 */
739219089Spjd	error = zil_parse(zilog, zil_claim_log_block, zil_claim_log_record, tx,
740219089Spjd	    zilog->zl_header->zh_claim_txg ? -1ULL : spa_first_txg(os->os_spa));
741219089Spjd
742219089Spjd	dmu_objset_rele(os, FTAG);
743219089Spjd
744219089Spjd	return ((error == ECKSUM || error == ENOENT) ? 0 : error);
745168404Spjd}
746168404Spjd
747185029Spjdstatic int
748185029Spjdzil_vdev_compare(const void *x1, const void *x2)
749185029Spjd{
750219089Spjd	const uint64_t v1 = ((zil_vdev_node_t *)x1)->zv_vdev;
751219089Spjd	const uint64_t v2 = ((zil_vdev_node_t *)x2)->zv_vdev;
752185029Spjd
753185029Spjd	if (v1 < v2)
754185029Spjd		return (-1);
755185029Spjd	if (v1 > v2)
756185029Spjd		return (1);
757185029Spjd
758185029Spjd	return (0);
759185029Spjd}
760185029Spjd
761168404Spjdvoid
762219089Spjdzil_add_block(zilog_t *zilog, const blkptr_t *bp)
763168404Spjd{
764185029Spjd	avl_tree_t *t = &zilog->zl_vdev_tree;
765185029Spjd	avl_index_t where;
766185029Spjd	zil_vdev_node_t *zv, zvsearch;
767185029Spjd	int ndvas = BP_GET_NDVAS(bp);
768185029Spjd	int i;
769168404Spjd
770185029Spjd	if (zfs_nocacheflush)
771185029Spjd		return;
772168404Spjd
773185029Spjd	ASSERT(zilog->zl_writer);
774168404Spjd
775185029Spjd	/*
776185029Spjd	 * Even though we're zl_writer, we still need a lock because the
777185029Spjd	 * zl_get_data() callbacks may have dmu_sync() done callbacks
778185029Spjd	 * that will run concurrently.
779185029Spjd	 */
780185029Spjd	mutex_enter(&zilog->zl_vdev_lock);
781185029Spjd	for (i = 0; i < ndvas; i++) {
782185029Spjd		zvsearch.zv_vdev = DVA_GET_VDEV(&bp->blk_dva[i]);
783185029Spjd		if (avl_find(t, &zvsearch, &where) == NULL) {
784185029Spjd			zv = kmem_alloc(sizeof (*zv), KM_SLEEP);
785185029Spjd			zv->zv_vdev = zvsearch.zv_vdev;
786185029Spjd			avl_insert(t, zv, where);
787185029Spjd		}
788185029Spjd	}
789185029Spjd	mutex_exit(&zilog->zl_vdev_lock);
790168404Spjd}
791168404Spjd
792219089Spjdstatic void
793168404Spjdzil_flush_vdevs(zilog_t *zilog)
794168404Spjd{
795168404Spjd	spa_t *spa = zilog->zl_spa;
796185029Spjd	avl_tree_t *t = &zilog->zl_vdev_tree;
797185029Spjd	void *cookie = NULL;
798185029Spjd	zil_vdev_node_t *zv;
799185029Spjd	zio_t *zio;
800168404Spjd
801168404Spjd	ASSERT(zilog->zl_writer);
802168404Spjd
803185029Spjd	/*
804185029Spjd	 * We don't need zl_vdev_lock here because we're the zl_writer,
805185029Spjd	 * and all zl_get_data() callbacks are done.
806185029Spjd	 */
807185029Spjd	if (avl_numnodes(t) == 0)
808185029Spjd		return;
809185029Spjd
810185029Spjd	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
811185029Spjd
812185029Spjd	zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
813185029Spjd
814185029Spjd	while ((zv = avl_destroy_nodes(t, &cookie)) != NULL) {
815185029Spjd		vdev_t *vd = vdev_lookup_top(spa, zv->zv_vdev);
816185029Spjd		if (vd != NULL)
817185029Spjd			zio_flush(zio, vd);
818185029Spjd		kmem_free(zv, sizeof (*zv));
819168404Spjd	}
820168404Spjd
821168404Spjd	/*
822168404Spjd	 * Wait for all the flushes to complete.  Not all devices actually
823168404Spjd	 * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails.
824168404Spjd	 */
825185029Spjd	(void) zio_wait(zio);
826185029Spjd
827185029Spjd	spa_config_exit(spa, SCL_STATE, FTAG);
828168404Spjd}
829168404Spjd
830168404Spjd/*
831168404Spjd * Function called when a log block write completes
832168404Spjd */
833168404Spjdstatic void
834168404Spjdzil_lwb_write_done(zio_t *zio)
835168404Spjd{
836168404Spjd	lwb_t *lwb = zio->io_private;
837168404Spjd	zilog_t *zilog = lwb->lwb_zilog;
838219089Spjd	dmu_tx_t *tx = lwb->lwb_tx;
839168404Spjd
840185029Spjd	ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF);
841185029Spjd	ASSERT(BP_GET_TYPE(zio->io_bp) == DMU_OT_INTENT_LOG);
842185029Spjd	ASSERT(BP_GET_LEVEL(zio->io_bp) == 0);
843185029Spjd	ASSERT(BP_GET_BYTEORDER(zio->io_bp) == ZFS_HOST_BYTEORDER);
844185029Spjd	ASSERT(!BP_IS_GANG(zio->io_bp));
845185029Spjd	ASSERT(!BP_IS_HOLE(zio->io_bp));
846185029Spjd	ASSERT(zio->io_bp->blk_fill == 0);
847185029Spjd
848168404Spjd	/*
849209962Smm	 * Ensure the lwb buffer pointer is cleared before releasing
850209962Smm	 * the txg. If we have had an allocation failure and
851209962Smm	 * the txg is waiting to sync then we want want zil_sync()
852209962Smm	 * to remove the lwb so that it's not picked up as the next new
853209962Smm	 * one in zil_commit_writer(). zil_sync() will only remove
854209962Smm	 * the lwb if lwb_buf is null.
855168404Spjd	 */
856168404Spjd	zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
857168404Spjd	mutex_enter(&zilog->zl_lock);
858168404Spjd	lwb->lwb_buf = NULL;
859219089Spjd	lwb->lwb_tx = NULL;
860219089Spjd	mutex_exit(&zilog->zl_lock);
861209962Smm
862209962Smm	/*
863209962Smm	 * Now that we've written this log block, we have a stable pointer
864209962Smm	 * to the next block in the chain, so it's OK to let the txg in
865219089Spjd	 * which we allocated the next block sync.
866209962Smm	 */
867219089Spjd	dmu_tx_commit(tx);
868168404Spjd}
869168404Spjd
870168404Spjd/*
871168404Spjd * Initialize the io for a log block.
872168404Spjd */
873168404Spjdstatic void
874168404Spjdzil_lwb_write_init(zilog_t *zilog, lwb_t *lwb)
875168404Spjd{
876168404Spjd	zbookmark_t zb;
877168404Spjd
878219089Spjd	SET_BOOKMARK(&zb, lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET],
879219089Spjd	    ZB_ZIL_OBJECT, ZB_ZIL_LEVEL,
880219089Spjd	    lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ]);
881168404Spjd
882168404Spjd	if (zilog->zl_root_zio == NULL) {
883168404Spjd		zilog->zl_root_zio = zio_root(zilog->zl_spa, NULL, NULL,
884168404Spjd		    ZIO_FLAG_CANFAIL);
885168404Spjd	}
886168404Spjd	if (lwb->lwb_zio == NULL) {
887168404Spjd		lwb->lwb_zio = zio_rewrite(zilog->zl_root_zio, zilog->zl_spa,
888219089Spjd		    0, &lwb->lwb_blk, lwb->lwb_buf, BP_GET_LSIZE(&lwb->lwb_blk),
889260764Savg		    zil_lwb_write_done, lwb, ZIO_PRIORITY_SYNC_WRITE,
890219089Spjd		    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE, &zb);
891168404Spjd	}
892168404Spjd}
893168404Spjd
894168404Spjd/*
895219089Spjd * Define a limited set of intent log block sizes.
896252751Sdelphij *
897219089Spjd * These must be a multiple of 4KB. Note only the amount used (again
898219089Spjd * aligned to 4KB) actually gets written. However, we can't always just
899219089Spjd * allocate SPA_MAXBLOCKSIZE as the slog space could be exhausted.
900219089Spjd */
901219089Spjduint64_t zil_block_buckets[] = {
902219089Spjd    4096,		/* non TX_WRITE */
903219089Spjd    8192+4096,		/* data base */
904219089Spjd    32*1024 + 4096, 	/* NFS writes */
905219089Spjd    UINT64_MAX
906219089Spjd};
907219089Spjd
908219089Spjd/*
909219089Spjd * Use the slog as long as the logbias is 'latency' and the current commit size
910219089Spjd * is less than the limit or the total list size is less than 2X the limit.
911219089Spjd * Limit checking is disabled by setting zil_slog_limit to UINT64_MAX.
912219089Spjd */
913219089Spjduint64_t zil_slog_limit = 1024 * 1024;
914219089Spjd#define	USE_SLOG(zilog) (((zilog)->zl_logbias == ZFS_LOGBIAS_LATENCY) && \
915219089Spjd	(((zilog)->zl_cur_used < zil_slog_limit) || \
916219089Spjd	((zilog)->zl_itx_list_sz < (zil_slog_limit << 1))))
917219089Spjd
918219089Spjd/*
919168404Spjd * Start a log block write and advance to the next log block.
920168404Spjd * Calls are serialized.
921168404Spjd */
922168404Spjdstatic lwb_t *
923168404Spjdzil_lwb_write_start(zilog_t *zilog, lwb_t *lwb)
924168404Spjd{
925219089Spjd	lwb_t *nlwb = NULL;
926219089Spjd	zil_chain_t *zilc;
927168404Spjd	spa_t *spa = zilog->zl_spa;
928219089Spjd	blkptr_t *bp;
929219089Spjd	dmu_tx_t *tx;
930168404Spjd	uint64_t txg;
931219089Spjd	uint64_t zil_blksz, wsz;
932219089Spjd	int i, error;
933168404Spjd
934219089Spjd	if (BP_GET_CHECKSUM(&lwb->lwb_blk) == ZIO_CHECKSUM_ZILOG2) {
935219089Spjd		zilc = (zil_chain_t *)lwb->lwb_buf;
936219089Spjd		bp = &zilc->zc_next_blk;
937219089Spjd	} else {
938219089Spjd		zilc = (zil_chain_t *)(lwb->lwb_buf + lwb->lwb_sz);
939219089Spjd		bp = &zilc->zc_next_blk;
940219089Spjd	}
941168404Spjd
942219089Spjd	ASSERT(lwb->lwb_nused <= lwb->lwb_sz);
943219089Spjd
944168404Spjd	/*
945168404Spjd	 * Allocate the next block and save its address in this block
946168404Spjd	 * before writing it in order to establish the log chain.
947168404Spjd	 * Note that if the allocation of nlwb synced before we wrote
948168404Spjd	 * the block that points at it (lwb), we'd leak it if we crashed.
949219089Spjd	 * Therefore, we don't do dmu_tx_commit() until zil_lwb_write_done().
950219089Spjd	 * We dirty the dataset to ensure that zil_sync() will be called
951219089Spjd	 * to clean up in the event of allocation failure or I/O failure.
952168404Spjd	 */
953219089Spjd	tx = dmu_tx_create(zilog->zl_os);
954219089Spjd	VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0);
955219089Spjd	dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
956219089Spjd	txg = dmu_tx_get_txg(tx);
957168404Spjd
958219089Spjd	lwb->lwb_tx = tx;
959219089Spjd
960168404Spjd	/*
961219089Spjd	 * Log blocks are pre-allocated. Here we select the size of the next
962219089Spjd	 * block, based on size used in the last block.
963219089Spjd	 * - first find the smallest bucket that will fit the block from a
964219089Spjd	 *   limited set of block sizes. This is because it's faster to write
965219089Spjd	 *   blocks allocated from the same metaslab as they are adjacent or
966219089Spjd	 *   close.
967219089Spjd	 * - next find the maximum from the new suggested size and an array of
968219089Spjd	 *   previous sizes. This lessens a picket fence effect of wrongly
969219089Spjd	 *   guesssing the size if we have a stream of say 2k, 64k, 2k, 64k
970219089Spjd	 *   requests.
971219089Spjd	 *
972219089Spjd	 * Note we only write what is used, but we can't just allocate
973219089Spjd	 * the maximum block size because we can exhaust the available
974219089Spjd	 * pool log space.
975168404Spjd	 */
976219089Spjd	zil_blksz = zilog->zl_cur_used + sizeof (zil_chain_t);
977219089Spjd	for (i = 0; zil_blksz > zil_block_buckets[i]; i++)
978219089Spjd		continue;
979219089Spjd	zil_blksz = zil_block_buckets[i];
980219089Spjd	if (zil_blksz == UINT64_MAX)
981219089Spjd		zil_blksz = SPA_MAXBLOCKSIZE;
982219089Spjd	zilog->zl_prev_blks[zilog->zl_prev_rotor] = zil_blksz;
983219089Spjd	for (i = 0; i < ZIL_PREV_BLKS; i++)
984219089Spjd		zil_blksz = MAX(zil_blksz, zilog->zl_prev_blks[i]);
985219089Spjd	zilog->zl_prev_rotor = (zilog->zl_prev_rotor + 1) & (ZIL_PREV_BLKS - 1);
986168404Spjd
987168404Spjd	BP_ZERO(bp);
988168404Spjd	/* pass the old blkptr in order to spread log blocks across devs */
989219089Spjd	error = zio_alloc_zil(spa, txg, bp, &lwb->lwb_blk, zil_blksz,
990219089Spjd	    USE_SLOG(zilog));
991249643Smm	if (error == 0) {
992219089Spjd		ASSERT3U(bp->blk_birth, ==, txg);
993219089Spjd		bp->blk_cksum = lwb->lwb_blk.blk_cksum;
994219089Spjd		bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++;
995168404Spjd
996168404Spjd		/*
997219089Spjd		 * Allocate a new log write buffer (lwb).
998168404Spjd		 */
999219089Spjd		nlwb = zil_alloc_lwb(zilog, bp, txg);
1000168404Spjd
1001219089Spjd		/* Record the block for later vdev flushing */
1002219089Spjd		zil_add_block(zilog, &lwb->lwb_blk);
1003168404Spjd	}
1004168404Spjd
1005219089Spjd	if (BP_GET_CHECKSUM(&lwb->lwb_blk) == ZIO_CHECKSUM_ZILOG2) {
1006219089Spjd		/* For Slim ZIL only write what is used. */
1007219089Spjd		wsz = P2ROUNDUP_TYPED(lwb->lwb_nused, ZIL_MIN_BLKSZ, uint64_t);
1008219089Spjd		ASSERT3U(wsz, <=, lwb->lwb_sz);
1009219089Spjd		zio_shrink(lwb->lwb_zio, wsz);
1010168404Spjd
1011219089Spjd	} else {
1012219089Spjd		wsz = lwb->lwb_sz;
1013219089Spjd	}
1014168404Spjd
1015219089Spjd	zilc->zc_pad = 0;
1016219089Spjd	zilc->zc_nused = lwb->lwb_nused;
1017219089Spjd	zilc->zc_eck.zec_cksum = lwb->lwb_blk.blk_cksum;
1018168404Spjd
1019168404Spjd	/*
1020219089Spjd	 * clear unused data for security
1021168404Spjd	 */
1022219089Spjd	bzero(lwb->lwb_buf + lwb->lwb_nused, wsz - lwb->lwb_nused);
1023168404Spjd
1024219089Spjd	zio_nowait(lwb->lwb_zio); /* Kick off the write for the old log block */
1025168404Spjd
1026168404Spjd	/*
1027219089Spjd	 * If there was an allocation failure then nlwb will be null which
1028219089Spjd	 * forces a txg_wait_synced().
1029168404Spjd	 */
1030168404Spjd	return (nlwb);
1031168404Spjd}
1032168404Spjd
1033168404Spjdstatic lwb_t *
1034168404Spjdzil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb)
1035168404Spjd{
1036168404Spjd	lr_t *lrc = &itx->itx_lr; /* common log record */
1037219089Spjd	lr_write_t *lrw = (lr_write_t *)lrc;
1038219089Spjd	char *lr_buf;
1039168404Spjd	uint64_t txg = lrc->lrc_txg;
1040168404Spjd	uint64_t reclen = lrc->lrc_reclen;
1041219089Spjd	uint64_t dlen = 0;
1042168404Spjd
1043168404Spjd	if (lwb == NULL)
1044168404Spjd		return (NULL);
1045219089Spjd
1046168404Spjd	ASSERT(lwb->lwb_buf != NULL);
1047243674Smm	ASSERT(zilog_is_dirty(zilog) ||
1048243674Smm	    spa_freeze_txg(zilog->zl_spa) != UINT64_MAX);
1049168404Spjd
1050168404Spjd	if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY)
1051168404Spjd		dlen = P2ROUNDUP_TYPED(
1052219089Spjd		    lrw->lr_length, sizeof (uint64_t), uint64_t);
1053168404Spjd
1054168404Spjd	zilog->zl_cur_used += (reclen + dlen);
1055168404Spjd
1056168404Spjd	zil_lwb_write_init(zilog, lwb);
1057168404Spjd
1058168404Spjd	/*
1059168404Spjd	 * If this record won't fit in the current log block, start a new one.
1060168404Spjd	 */
1061219089Spjd	if (lwb->lwb_nused + reclen + dlen > lwb->lwb_sz) {
1062168404Spjd		lwb = zil_lwb_write_start(zilog, lwb);
1063168404Spjd		if (lwb == NULL)
1064168404Spjd			return (NULL);
1065168404Spjd		zil_lwb_write_init(zilog, lwb);
1066219089Spjd		ASSERT(LWB_EMPTY(lwb));
1067219089Spjd		if (lwb->lwb_nused + reclen + dlen > lwb->lwb_sz) {
1068168404Spjd			txg_wait_synced(zilog->zl_dmu_pool, txg);
1069168404Spjd			return (lwb);
1070168404Spjd		}
1071168404Spjd	}
1072168404Spjd
1073219089Spjd	lr_buf = lwb->lwb_buf + lwb->lwb_nused;
1074219089Spjd	bcopy(lrc, lr_buf, reclen);
1075219089Spjd	lrc = (lr_t *)lr_buf;
1076219089Spjd	lrw = (lr_write_t *)lrc;
1077168404Spjd
1078168404Spjd	/*
1079168404Spjd	 * If it's a write, fetch the data or get its blkptr as appropriate.
1080168404Spjd	 */
1081168404Spjd	if (lrc->lrc_txtype == TX_WRITE) {
1082168404Spjd		if (txg > spa_freeze_txg(zilog->zl_spa))
1083168404Spjd			txg_wait_synced(zilog->zl_dmu_pool, txg);
1084168404Spjd		if (itx->itx_wr_state != WR_COPIED) {
1085168404Spjd			char *dbuf;
1086168404Spjd			int error;
1087168404Spjd
1088168404Spjd			if (dlen) {
1089168404Spjd				ASSERT(itx->itx_wr_state == WR_NEED_COPY);
1090219089Spjd				dbuf = lr_buf + reclen;
1091219089Spjd				lrw->lr_common.lrc_reclen += dlen;
1092168404Spjd			} else {
1093168404Spjd				ASSERT(itx->itx_wr_state == WR_INDIRECT);
1094168404Spjd				dbuf = NULL;
1095168404Spjd			}
1096168404Spjd			error = zilog->zl_get_data(
1097219089Spjd			    itx->itx_private, lrw, dbuf, lwb->lwb_zio);
1098214378Smm			if (error == EIO) {
1099214378Smm				txg_wait_synced(zilog->zl_dmu_pool, txg);
1100214378Smm				return (lwb);
1101214378Smm			}
1102249643Smm			if (error != 0) {
1103168404Spjd				ASSERT(error == ENOENT || error == EEXIST ||
1104168404Spjd				    error == EALREADY);
1105168404Spjd				return (lwb);
1106168404Spjd			}
1107168404Spjd		}
1108168404Spjd	}
1109168404Spjd
1110219089Spjd	/*
1111219089Spjd	 * We're actually making an entry, so update lrc_seq to be the
1112219089Spjd	 * log record sequence number.  Note that this is generally not
1113219089Spjd	 * equal to the itx sequence number because not all transactions
1114219089Spjd	 * are synchronous, and sometimes spa_sync() gets there first.
1115219089Spjd	 */
1116219089Spjd	lrc->lrc_seq = ++zilog->zl_lr_seq; /* we are single threaded */
1117168404Spjd	lwb->lwb_nused += reclen + dlen;
1118168404Spjd	lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg);
1119219089Spjd	ASSERT3U(lwb->lwb_nused, <=, lwb->lwb_sz);
1120243674Smm	ASSERT0(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)));
1121168404Spjd
1122168404Spjd	return (lwb);
1123168404Spjd}
1124168404Spjd
1125168404Spjditx_t *
1126185029Spjdzil_itx_create(uint64_t txtype, size_t lrsize)
1127168404Spjd{
1128168404Spjd	itx_t *itx;
1129168404Spjd
1130168404Spjd	lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t);
1131168404Spjd
1132168404Spjd	itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP);
1133168404Spjd	itx->itx_lr.lrc_txtype = txtype;
1134168404Spjd	itx->itx_lr.lrc_reclen = lrsize;
1135185029Spjd	itx->itx_sod = lrsize; /* if write & WR_NEED_COPY will be increased */
1136168404Spjd	itx->itx_lr.lrc_seq = 0;	/* defensive */
1137219089Spjd	itx->itx_sync = B_TRUE;		/* default is synchronous */
1138168404Spjd
1139168404Spjd	return (itx);
1140168404Spjd}
1141168404Spjd
1142219089Spjdvoid
1143219089Spjdzil_itx_destroy(itx_t *itx)
1144168404Spjd{
1145219089Spjd	kmem_free(itx, offsetof(itx_t, itx_lr) + itx->itx_lr.lrc_reclen);
1146219089Spjd}
1147168404Spjd
1148219089Spjd/*
1149219089Spjd * Free up the sync and async itxs. The itxs_t has already been detached
1150219089Spjd * so no locks are needed.
1151219089Spjd */
1152219089Spjdstatic void
1153219089Spjdzil_itxg_clean(itxs_t *itxs)
1154219089Spjd{
1155219089Spjd	itx_t *itx;
1156219089Spjd	list_t *list;
1157219089Spjd	avl_tree_t *t;
1158219089Spjd	void *cookie;
1159219089Spjd	itx_async_node_t *ian;
1160168404Spjd
1161219089Spjd	list = &itxs->i_sync_list;
1162219089Spjd	while ((itx = list_head(list)) != NULL) {
1163219089Spjd		list_remove(list, itx);
1164219089Spjd		kmem_free(itx, offsetof(itx_t, itx_lr) +
1165219089Spjd		    itx->itx_lr.lrc_reclen);
1166219089Spjd	}
1167168404Spjd
1168219089Spjd	cookie = NULL;
1169219089Spjd	t = &itxs->i_async_tree;
1170219089Spjd	while ((ian = avl_destroy_nodes(t, &cookie)) != NULL) {
1171219089Spjd		list = &ian->ia_list;
1172219089Spjd		while ((itx = list_head(list)) != NULL) {
1173219089Spjd			list_remove(list, itx);
1174219089Spjd			kmem_free(itx, offsetof(itx_t, itx_lr) +
1175219089Spjd			    itx->itx_lr.lrc_reclen);
1176219089Spjd		}
1177219089Spjd		list_destroy(list);
1178219089Spjd		kmem_free(ian, sizeof (itx_async_node_t));
1179219089Spjd	}
1180219089Spjd	avl_destroy(t);
1181219089Spjd
1182219089Spjd	kmem_free(itxs, sizeof (itxs_t));
1183168404Spjd}
1184168404Spjd
1185219089Spjdstatic int
1186219089Spjdzil_aitx_compare(const void *x1, const void *x2)
1187219089Spjd{
1188219089Spjd	const uint64_t o1 = ((itx_async_node_t *)x1)->ia_foid;
1189219089Spjd	const uint64_t o2 = ((itx_async_node_t *)x2)->ia_foid;
1190219089Spjd
1191219089Spjd	if (o1 < o2)
1192219089Spjd		return (-1);
1193219089Spjd	if (o1 > o2)
1194219089Spjd		return (1);
1195219089Spjd
1196219089Spjd	return (0);
1197219089Spjd}
1198219089Spjd
1199168404Spjd/*
1200219089Spjd * Remove all async itx with the given oid.
1201168404Spjd */
1202168404Spjdstatic void
1203219089Spjdzil_remove_async(zilog_t *zilog, uint64_t oid)
1204168404Spjd{
1205219089Spjd	uint64_t otxg, txg;
1206219089Spjd	itx_async_node_t *ian;
1207219089Spjd	avl_tree_t *t;
1208219089Spjd	avl_index_t where;
1209168404Spjd	list_t clean_list;
1210168404Spjd	itx_t *itx;
1211168404Spjd
1212219089Spjd	ASSERT(oid != 0);
1213168404Spjd	list_create(&clean_list, sizeof (itx_t), offsetof(itx_t, itx_node));
1214168404Spjd
1215219089Spjd	if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */
1216219089Spjd		otxg = ZILTEST_TXG;
1217219089Spjd	else
1218219089Spjd		otxg = spa_last_synced_txg(zilog->zl_spa) + 1;
1219219089Spjd
1220219089Spjd	for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) {
1221219089Spjd		itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK];
1222219089Spjd
1223219089Spjd		mutex_enter(&itxg->itxg_lock);
1224219089Spjd		if (itxg->itxg_txg != txg) {
1225219089Spjd			mutex_exit(&itxg->itxg_lock);
1226219089Spjd			continue;
1227219089Spjd		}
1228219089Spjd
1229219089Spjd		/*
1230219089Spjd		 * Locate the object node and append its list.
1231219089Spjd		 */
1232219089Spjd		t = &itxg->itxg_itxs->i_async_tree;
1233219089Spjd		ian = avl_find(t, &oid, &where);
1234219089Spjd		if (ian != NULL)
1235219089Spjd			list_move_tail(&clean_list, &ian->ia_list);
1236219089Spjd		mutex_exit(&itxg->itxg_lock);
1237168404Spjd	}
1238219089Spjd	while ((itx = list_head(&clean_list)) != NULL) {
1239219089Spjd		list_remove(&clean_list, itx);
1240219089Spjd		kmem_free(itx, offsetof(itx_t, itx_lr) +
1241219089Spjd		    itx->itx_lr.lrc_reclen);
1242219089Spjd	}
1243219089Spjd	list_destroy(&clean_list);
1244219089Spjd}
1245168404Spjd
1246219089Spjdvoid
1247219089Spjdzil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx)
1248219089Spjd{
1249219089Spjd	uint64_t txg;
1250219089Spjd	itxg_t *itxg;
1251219089Spjd	itxs_t *itxs, *clean = NULL;
1252219089Spjd
1253168404Spjd	/*
1254219089Spjd	 * Object ids can be re-instantiated in the next txg so
1255219089Spjd	 * remove any async transactions to avoid future leaks.
1256219089Spjd	 * This can happen if a fsync occurs on the re-instantiated
1257219089Spjd	 * object for a WR_INDIRECT or WR_NEED_COPY write, which gets
1258219089Spjd	 * the new file data and flushes a write record for the old object.
1259168404Spjd	 */
1260219089Spjd	if ((itx->itx_lr.lrc_txtype & ~TX_CI) == TX_REMOVE)
1261219089Spjd		zil_remove_async(zilog, itx->itx_oid);
1262219089Spjd
1263219089Spjd	/*
1264219089Spjd	 * Ensure the data of a renamed file is committed before the rename.
1265219089Spjd	 */
1266219089Spjd	if ((itx->itx_lr.lrc_txtype & ~TX_CI) == TX_RENAME)
1267219089Spjd		zil_async_to_sync(zilog, itx->itx_oid);
1268219089Spjd
1269243674Smm	if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX)
1270219089Spjd		txg = ZILTEST_TXG;
1271219089Spjd	else
1272219089Spjd		txg = dmu_tx_get_txg(tx);
1273219089Spjd
1274219089Spjd	itxg = &zilog->zl_itxg[txg & TXG_MASK];
1275219089Spjd	mutex_enter(&itxg->itxg_lock);
1276219089Spjd	itxs = itxg->itxg_itxs;
1277219089Spjd	if (itxg->itxg_txg != txg) {
1278219089Spjd		if (itxs != NULL) {
1279219089Spjd			/*
1280219089Spjd			 * The zil_clean callback hasn't got around to cleaning
1281219089Spjd			 * this itxg. Save the itxs for release below.
1282219089Spjd			 * This should be rare.
1283219089Spjd			 */
1284219089Spjd			atomic_add_64(&zilog->zl_itx_list_sz, -itxg->itxg_sod);
1285219089Spjd			itxg->itxg_sod = 0;
1286219089Spjd			clean = itxg->itxg_itxs;
1287219089Spjd		}
1288219089Spjd		ASSERT(itxg->itxg_sod == 0);
1289219089Spjd		itxg->itxg_txg = txg;
1290219089Spjd		itxs = itxg->itxg_itxs = kmem_zalloc(sizeof (itxs_t), KM_SLEEP);
1291219089Spjd
1292219089Spjd		list_create(&itxs->i_sync_list, sizeof (itx_t),
1293219089Spjd		    offsetof(itx_t, itx_node));
1294219089Spjd		avl_create(&itxs->i_async_tree, zil_aitx_compare,
1295219089Spjd		    sizeof (itx_async_node_t),
1296219089Spjd		    offsetof(itx_async_node_t, ia_node));
1297168404Spjd	}
1298219089Spjd	if (itx->itx_sync) {
1299219089Spjd		list_insert_tail(&itxs->i_sync_list, itx);
1300219089Spjd		atomic_add_64(&zilog->zl_itx_list_sz, itx->itx_sod);
1301219089Spjd		itxg->itxg_sod += itx->itx_sod;
1302219089Spjd	} else {
1303219089Spjd		avl_tree_t *t = &itxs->i_async_tree;
1304219089Spjd		uint64_t foid = ((lr_ooo_t *)&itx->itx_lr)->lr_foid;
1305219089Spjd		itx_async_node_t *ian;
1306219089Spjd		avl_index_t where;
1307168404Spjd
1308219089Spjd		ian = avl_find(t, &foid, &where);
1309219089Spjd		if (ian == NULL) {
1310219089Spjd			ian = kmem_alloc(sizeof (itx_async_node_t), KM_SLEEP);
1311219089Spjd			list_create(&ian->ia_list, sizeof (itx_t),
1312219089Spjd			    offsetof(itx_t, itx_node));
1313219089Spjd			ian->ia_foid = foid;
1314219089Spjd			avl_insert(t, ian, where);
1315219089Spjd		}
1316219089Spjd		list_insert_tail(&ian->ia_list, itx);
1317168404Spjd	}
1318219089Spjd
1319219089Spjd	itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx);
1320243674Smm	zilog_dirty(zilog, txg);
1321219089Spjd	mutex_exit(&itxg->itxg_lock);
1322219089Spjd
1323219089Spjd	/* Release the old itxs now we've dropped the lock */
1324219089Spjd	if (clean != NULL)
1325219089Spjd		zil_itxg_clean(clean);
1326168404Spjd}
1327168404Spjd
1328168404Spjd/*
1329168404Spjd * If there are any in-memory intent log transactions which have now been
1330243674Smm * synced then start up a taskq to free them. We should only do this after we
1331243674Smm * have written out the uberblocks (i.e. txg has been comitted) so that
1332243674Smm * don't inadvertently clean out in-memory log records that would be required
1333243674Smm * by zil_commit().
1334168404Spjd */
1335168404Spjdvoid
1336219089Spjdzil_clean(zilog_t *zilog, uint64_t synced_txg)
1337168404Spjd{
1338219089Spjd	itxg_t *itxg = &zilog->zl_itxg[synced_txg & TXG_MASK];
1339219089Spjd	itxs_t *clean_me;
1340168404Spjd
1341219089Spjd	mutex_enter(&itxg->itxg_lock);
1342219089Spjd	if (itxg->itxg_itxs == NULL || itxg->itxg_txg == ZILTEST_TXG) {
1343219089Spjd		mutex_exit(&itxg->itxg_lock);
1344219089Spjd		return;
1345168404Spjd	}
1346219089Spjd	ASSERT3U(itxg->itxg_txg, <=, synced_txg);
1347219089Spjd	ASSERT(itxg->itxg_txg != 0);
1348219089Spjd	ASSERT(zilog->zl_clean_taskq != NULL);
1349219089Spjd	atomic_add_64(&zilog->zl_itx_list_sz, -itxg->itxg_sod);
1350219089Spjd	itxg->itxg_sod = 0;
1351219089Spjd	clean_me = itxg->itxg_itxs;
1352219089Spjd	itxg->itxg_itxs = NULL;
1353219089Spjd	itxg->itxg_txg = 0;
1354219089Spjd	mutex_exit(&itxg->itxg_lock);
1355219089Spjd	/*
1356219089Spjd	 * Preferably start a task queue to free up the old itxs but
1357219089Spjd	 * if taskq_dispatch can't allocate resources to do that then
1358219089Spjd	 * free it in-line. This should be rare. Note, using TQ_SLEEP
1359219089Spjd	 * created a bad performance problem.
1360219089Spjd	 */
1361219089Spjd	if (taskq_dispatch(zilog->zl_clean_taskq,
1362219089Spjd	    (void (*)(void *))zil_itxg_clean, clean_me, TQ_NOSLEEP) == 0)
1363219089Spjd		zil_itxg_clean(clean_me);
1364168404Spjd}
1365168404Spjd
1366219089Spjd/*
1367219089Spjd * Get the list of itxs to commit into zl_itx_commit_list.
1368219089Spjd */
1369185029Spjdstatic void
1370219089Spjdzil_get_commit_list(zilog_t *zilog)
1371168404Spjd{
1372219089Spjd	uint64_t otxg, txg;
1373219089Spjd	list_t *commit_list = &zilog->zl_itx_commit_list;
1374219089Spjd	uint64_t push_sod = 0;
1375219089Spjd
1376219089Spjd	if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */
1377219089Spjd		otxg = ZILTEST_TXG;
1378219089Spjd	else
1379219089Spjd		otxg = spa_last_synced_txg(zilog->zl_spa) + 1;
1380219089Spjd
1381219089Spjd	for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) {
1382219089Spjd		itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK];
1383219089Spjd
1384219089Spjd		mutex_enter(&itxg->itxg_lock);
1385219089Spjd		if (itxg->itxg_txg != txg) {
1386219089Spjd			mutex_exit(&itxg->itxg_lock);
1387219089Spjd			continue;
1388219089Spjd		}
1389219089Spjd
1390219089Spjd		list_move_tail(commit_list, &itxg->itxg_itxs->i_sync_list);
1391219089Spjd		push_sod += itxg->itxg_sod;
1392219089Spjd		itxg->itxg_sod = 0;
1393219089Spjd
1394219089Spjd		mutex_exit(&itxg->itxg_lock);
1395219089Spjd	}
1396219089Spjd	atomic_add_64(&zilog->zl_itx_list_sz, -push_sod);
1397219089Spjd}
1398219089Spjd
1399219089Spjd/*
1400219089Spjd * Move the async itxs for a specified object to commit into sync lists.
1401219089Spjd */
1402219089Spjdstatic void
1403219089Spjdzil_async_to_sync(zilog_t *zilog, uint64_t foid)
1404219089Spjd{
1405219089Spjd	uint64_t otxg, txg;
1406219089Spjd	itx_async_node_t *ian;
1407219089Spjd	avl_tree_t *t;
1408219089Spjd	avl_index_t where;
1409219089Spjd
1410219089Spjd	if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */
1411219089Spjd		otxg = ZILTEST_TXG;
1412219089Spjd	else
1413219089Spjd		otxg = spa_last_synced_txg(zilog->zl_spa) + 1;
1414219089Spjd
1415219089Spjd	for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) {
1416219089Spjd		itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK];
1417219089Spjd
1418219089Spjd		mutex_enter(&itxg->itxg_lock);
1419219089Spjd		if (itxg->itxg_txg != txg) {
1420219089Spjd			mutex_exit(&itxg->itxg_lock);
1421219089Spjd			continue;
1422219089Spjd		}
1423219089Spjd
1424219089Spjd		/*
1425219089Spjd		 * If a foid is specified then find that node and append its
1426219089Spjd		 * list. Otherwise walk the tree appending all the lists
1427219089Spjd		 * to the sync list. We add to the end rather than the
1428219089Spjd		 * beginning to ensure the create has happened.
1429219089Spjd		 */
1430219089Spjd		t = &itxg->itxg_itxs->i_async_tree;
1431219089Spjd		if (foid != 0) {
1432219089Spjd			ian = avl_find(t, &foid, &where);
1433219089Spjd			if (ian != NULL) {
1434219089Spjd				list_move_tail(&itxg->itxg_itxs->i_sync_list,
1435219089Spjd				    &ian->ia_list);
1436219089Spjd			}
1437219089Spjd		} else {
1438219089Spjd			void *cookie = NULL;
1439219089Spjd
1440219089Spjd			while ((ian = avl_destroy_nodes(t, &cookie)) != NULL) {
1441219089Spjd				list_move_tail(&itxg->itxg_itxs->i_sync_list,
1442219089Spjd				    &ian->ia_list);
1443219089Spjd				list_destroy(&ian->ia_list);
1444219089Spjd				kmem_free(ian, sizeof (itx_async_node_t));
1445219089Spjd			}
1446219089Spjd		}
1447219089Spjd		mutex_exit(&itxg->itxg_lock);
1448219089Spjd	}
1449219089Spjd}
1450219089Spjd
1451219089Spjdstatic void
1452219089Spjdzil_commit_writer(zilog_t *zilog)
1453219089Spjd{
1454168404Spjd	uint64_t txg;
1455219089Spjd	itx_t *itx;
1456168404Spjd	lwb_t *lwb;
1457219089Spjd	spa_t *spa = zilog->zl_spa;
1458219089Spjd	int error = 0;
1459168404Spjd
1460185029Spjd	ASSERT(zilog->zl_root_zio == NULL);
1461168404Spjd
1462219089Spjd	mutex_exit(&zilog->zl_lock);
1463219089Spjd
1464219089Spjd	zil_get_commit_list(zilog);
1465219089Spjd
1466219089Spjd	/*
1467219089Spjd	 * Return if there's nothing to commit before we dirty the fs by
1468219089Spjd	 * calling zil_create().
1469219089Spjd	 */
1470219089Spjd	if (list_head(&zilog->zl_itx_commit_list) == NULL) {
1471219089Spjd		mutex_enter(&zilog->zl_lock);
1472219089Spjd		return;
1473219089Spjd	}
1474219089Spjd
1475168404Spjd	if (zilog->zl_suspend) {
1476168404Spjd		lwb = NULL;
1477168404Spjd	} else {
1478168404Spjd		lwb = list_tail(&zilog->zl_lwb_list);
1479219089Spjd		if (lwb == NULL)
1480219089Spjd			lwb = zil_create(zilog);
1481168404Spjd	}
1482168404Spjd
1483168404Spjd	DTRACE_PROBE1(zil__cw1, zilog_t *, zilog);
1484219089Spjd	while (itx = list_head(&zilog->zl_itx_commit_list)) {
1485168404Spjd		txg = itx->itx_lr.lrc_txg;
1486168404Spjd		ASSERT(txg);
1487168404Spjd
1488219089Spjd		if (txg > spa_last_synced_txg(spa) || txg > spa_freeze_txg(spa))
1489168404Spjd			lwb = zil_lwb_commit(zilog, itx, lwb);
1490219089Spjd		list_remove(&zilog->zl_itx_commit_list, itx);
1491168404Spjd		kmem_free(itx, offsetof(itx_t, itx_lr)
1492168404Spjd		    + itx->itx_lr.lrc_reclen);
1493168404Spjd	}
1494168404Spjd	DTRACE_PROBE1(zil__cw2, zilog_t *, zilog);
1495168404Spjd
1496168404Spjd	/* write the last block out */
1497168404Spjd	if (lwb != NULL && lwb->lwb_zio != NULL)
1498168404Spjd		lwb = zil_lwb_write_start(zilog, lwb);
1499168404Spjd
1500168404Spjd	zilog->zl_cur_used = 0;
1501168404Spjd
1502168404Spjd	/*
1503168404Spjd	 * Wait if necessary for the log blocks to be on stable storage.
1504168404Spjd	 */
1505168404Spjd	if (zilog->zl_root_zio) {
1506219089Spjd		error = zio_wait(zilog->zl_root_zio);
1507185029Spjd		zilog->zl_root_zio = NULL;
1508185029Spjd		zil_flush_vdevs(zilog);
1509168404Spjd	}
1510168404Spjd
1511219089Spjd	if (error || lwb == NULL)
1512168404Spjd		txg_wait_synced(zilog->zl_dmu_pool, 0);
1513168404Spjd
1514168404Spjd	mutex_enter(&zilog->zl_lock);
1515168404Spjd
1516219089Spjd	/*
1517219089Spjd	 * Remember the highest committed log sequence number for ztest.
1518219089Spjd	 * We only update this value when all the log writes succeeded,
1519219089Spjd	 * because ztest wants to ASSERT that it got the whole log chain.
1520219089Spjd	 */
1521219089Spjd	if (error == 0 && lwb != NULL)
1522219089Spjd		zilog->zl_commit_lr_seq = zilog->zl_lr_seq;
1523168404Spjd}
1524168404Spjd
1525168404Spjd/*
1526219089Spjd * Commit zfs transactions to stable storage.
1527168404Spjd * If foid is 0 push out all transactions, otherwise push only those
1528219089Spjd * for that object or might reference that object.
1529219089Spjd *
1530219089Spjd * itxs are committed in batches. In a heavily stressed zil there will be
1531219089Spjd * a commit writer thread who is writing out a bunch of itxs to the log
1532219089Spjd * for a set of committing threads (cthreads) in the same batch as the writer.
1533219089Spjd * Those cthreads are all waiting on the same cv for that batch.
1534219089Spjd *
1535219089Spjd * There will also be a different and growing batch of threads that are
1536219089Spjd * waiting to commit (qthreads). When the committing batch completes
1537219089Spjd * a transition occurs such that the cthreads exit and the qthreads become
1538219089Spjd * cthreads. One of the new cthreads becomes the writer thread for the
1539219089Spjd * batch. Any new threads arriving become new qthreads.
1540219089Spjd *
1541219089Spjd * Only 2 condition variables are needed and there's no transition
1542219089Spjd * between the two cvs needed. They just flip-flop between qthreads
1543219089Spjd * and cthreads.
1544219089Spjd *
1545219089Spjd * Using this scheme we can efficiently wakeup up only those threads
1546219089Spjd * that have been committed.
1547168404Spjd */
1548168404Spjdvoid
1549219089Spjdzil_commit(zilog_t *zilog, uint64_t foid)
1550168404Spjd{
1551219089Spjd	uint64_t mybatch;
1552219089Spjd
1553219089Spjd	if (zilog->zl_sync == ZFS_SYNC_DISABLED)
1554168404Spjd		return;
1555168404Spjd
1556219089Spjd	/* move the async itxs for the foid to the sync queues */
1557219089Spjd	zil_async_to_sync(zilog, foid);
1558219089Spjd
1559168404Spjd	mutex_enter(&zilog->zl_lock);
1560219089Spjd	mybatch = zilog->zl_next_batch;
1561168404Spjd	while (zilog->zl_writer) {
1562219089Spjd		cv_wait(&zilog->zl_cv_batch[mybatch & 1], &zilog->zl_lock);
1563219089Spjd		if (mybatch <= zilog->zl_com_batch) {
1564168404Spjd			mutex_exit(&zilog->zl_lock);
1565168404Spjd			return;
1566168404Spjd		}
1567168404Spjd	}
1568219089Spjd
1569219089Spjd	zilog->zl_next_batch++;
1570219089Spjd	zilog->zl_writer = B_TRUE;
1571219089Spjd	zil_commit_writer(zilog);
1572219089Spjd	zilog->zl_com_batch = mybatch;
1573219089Spjd	zilog->zl_writer = B_FALSE;
1574168404Spjd	mutex_exit(&zilog->zl_lock);
1575219089Spjd
1576219089Spjd	/* wake up one thread to become the next writer */
1577219089Spjd	cv_signal(&zilog->zl_cv_batch[(mybatch+1) & 1]);
1578219089Spjd
1579219089Spjd	/* wake up all threads waiting for this batch to be committed */
1580219089Spjd	cv_broadcast(&zilog->zl_cv_batch[mybatch & 1]);
1581168404Spjd}
1582168404Spjd
1583168404Spjd/*
1584168404Spjd * Called in syncing context to free committed log blocks and update log header.
1585168404Spjd */
1586168404Spjdvoid
1587168404Spjdzil_sync(zilog_t *zilog, dmu_tx_t *tx)
1588168404Spjd{
1589168404Spjd	zil_header_t *zh = zil_header_in_syncing_context(zilog);
1590168404Spjd	uint64_t txg = dmu_tx_get_txg(tx);
1591168404Spjd	spa_t *spa = zilog->zl_spa;
1592219089Spjd	uint64_t *replayed_seq = &zilog->zl_replayed_seq[txg & TXG_MASK];
1593168404Spjd	lwb_t *lwb;
1594168404Spjd
1595209962Smm	/*
1596209962Smm	 * We don't zero out zl_destroy_txg, so make sure we don't try
1597209962Smm	 * to destroy it twice.
1598209962Smm	 */
1599209962Smm	if (spa_sync_pass(spa) != 1)
1600209962Smm		return;
1601209962Smm
1602168404Spjd	mutex_enter(&zilog->zl_lock);
1603168404Spjd
1604168404Spjd	ASSERT(zilog->zl_stop_sync == 0);
1605168404Spjd
1606219089Spjd	if (*replayed_seq != 0) {
1607219089Spjd		ASSERT(zh->zh_replay_seq < *replayed_seq);
1608219089Spjd		zh->zh_replay_seq = *replayed_seq;
1609219089Spjd		*replayed_seq = 0;
1610219089Spjd	}
1611168404Spjd
1612168404Spjd	if (zilog->zl_destroy_txg == txg) {
1613168404Spjd		blkptr_t blk = zh->zh_log;
1614168404Spjd
1615168404Spjd		ASSERT(list_head(&zilog->zl_lwb_list) == NULL);
1616168404Spjd
1617168404Spjd		bzero(zh, sizeof (zil_header_t));
1618209962Smm		bzero(zilog->zl_replayed_seq, sizeof (zilog->zl_replayed_seq));
1619168404Spjd
1620168404Spjd		if (zilog->zl_keep_first) {
1621168404Spjd			/*
1622168404Spjd			 * If this block was part of log chain that couldn't
1623168404Spjd			 * be claimed because a device was missing during
1624168404Spjd			 * zil_claim(), but that device later returns,
1625168404Spjd			 * then this block could erroneously appear valid.
1626168404Spjd			 * To guard against this, assign a new GUID to the new
1627168404Spjd			 * log chain so it doesn't matter what blk points to.
1628168404Spjd			 */
1629168404Spjd			zil_init_log_chain(zilog, &blk);
1630168404Spjd			zh->zh_log = blk;
1631168404Spjd		}
1632168404Spjd	}
1633168404Spjd
1634213197Smm	while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
1635168404Spjd		zh->zh_log = lwb->lwb_blk;
1636168404Spjd		if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg)
1637168404Spjd			break;
1638168404Spjd		list_remove(&zilog->zl_lwb_list, lwb);
1639219089Spjd		zio_free_zil(spa, txg, &lwb->lwb_blk);
1640168404Spjd		kmem_cache_free(zil_lwb_cache, lwb);
1641168404Spjd
1642168404Spjd		/*
1643168404Spjd		 * If we don't have anything left in the lwb list then
1644168404Spjd		 * we've had an allocation failure and we need to zero
1645168404Spjd		 * out the zil_header blkptr so that we don't end
1646168404Spjd		 * up freeing the same block twice.
1647168404Spjd		 */
1648168404Spjd		if (list_head(&zilog->zl_lwb_list) == NULL)
1649168404Spjd			BP_ZERO(&zh->zh_log);
1650168404Spjd	}
1651168404Spjd	mutex_exit(&zilog->zl_lock);
1652168404Spjd}
1653168404Spjd
1654168404Spjdvoid
1655168404Spjdzil_init(void)
1656168404Spjd{
1657168404Spjd	zil_lwb_cache = kmem_cache_create("zil_lwb_cache",
1658168404Spjd	    sizeof (struct lwb), 0, NULL, NULL, NULL, NULL, NULL, 0);
1659168404Spjd}
1660168404Spjd
1661168404Spjdvoid
1662168404Spjdzil_fini(void)
1663168404Spjd{
1664168404Spjd	kmem_cache_destroy(zil_lwb_cache);
1665168404Spjd}
1666168404Spjd
1667219089Spjdvoid
1668219089Spjdzil_set_sync(zilog_t *zilog, uint64_t sync)
1669219089Spjd{
1670219089Spjd	zilog->zl_sync = sync;
1671219089Spjd}
1672219089Spjd
1673219089Spjdvoid
1674219089Spjdzil_set_logbias(zilog_t *zilog, uint64_t logbias)
1675219089Spjd{
1676219089Spjd	zilog->zl_logbias = logbias;
1677219089Spjd}
1678219089Spjd
1679168404Spjdzilog_t *
1680168404Spjdzil_alloc(objset_t *os, zil_header_t *zh_phys)
1681168404Spjd{
1682168404Spjd	zilog_t *zilog;
1683168404Spjd
1684168404Spjd	zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP);
1685168404Spjd
1686168404Spjd	zilog->zl_header = zh_phys;
1687168404Spjd	zilog->zl_os = os;
1688168404Spjd	zilog->zl_spa = dmu_objset_spa(os);
1689168404Spjd	zilog->zl_dmu_pool = dmu_objset_pool(os);
1690168404Spjd	zilog->zl_destroy_txg = TXG_INITIAL - 1;
1691219089Spjd	zilog->zl_logbias = dmu_objset_logbias(os);
1692219089Spjd	zilog->zl_sync = dmu_objset_syncprop(os);
1693219089Spjd	zilog->zl_next_batch = 1;
1694168404Spjd
1695168404Spjd	mutex_init(&zilog->zl_lock, NULL, MUTEX_DEFAULT, NULL);
1696168404Spjd
1697219089Spjd	for (int i = 0; i < TXG_SIZE; i++) {
1698219089Spjd		mutex_init(&zilog->zl_itxg[i].itxg_lock, NULL,
1699219089Spjd		    MUTEX_DEFAULT, NULL);
1700219089Spjd	}
1701168404Spjd
1702168404Spjd	list_create(&zilog->zl_lwb_list, sizeof (lwb_t),
1703168404Spjd	    offsetof(lwb_t, lwb_node));
1704168404Spjd
1705219089Spjd	list_create(&zilog->zl_itx_commit_list, sizeof (itx_t),
1706219089Spjd	    offsetof(itx_t, itx_node));
1707219089Spjd
1708185029Spjd	mutex_init(&zilog->zl_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
1709168404Spjd
1710185029Spjd	avl_create(&zilog->zl_vdev_tree, zil_vdev_compare,
1711185029Spjd	    sizeof (zil_vdev_node_t), offsetof(zil_vdev_node_t, zv_node));
1712185029Spjd
1713185029Spjd	cv_init(&zilog->zl_cv_writer, NULL, CV_DEFAULT, NULL);
1714185029Spjd	cv_init(&zilog->zl_cv_suspend, NULL, CV_DEFAULT, NULL);
1715219089Spjd	cv_init(&zilog->zl_cv_batch[0], NULL, CV_DEFAULT, NULL);
1716219089Spjd	cv_init(&zilog->zl_cv_batch[1], NULL, CV_DEFAULT, NULL);
1717185029Spjd
1718168404Spjd	return (zilog);
1719168404Spjd}
1720168404Spjd
1721168404Spjdvoid
1722168404Spjdzil_free(zilog_t *zilog)
1723168404Spjd{
1724168404Spjd	zilog->zl_stop_sync = 1;
1725168404Spjd
1726249643Smm	ASSERT0(zilog->zl_suspend);
1727249643Smm	ASSERT0(zilog->zl_suspending);
1728249643Smm
1729224526Smm	ASSERT(list_is_empty(&zilog->zl_lwb_list));
1730168404Spjd	list_destroy(&zilog->zl_lwb_list);
1731168404Spjd
1732185029Spjd	avl_destroy(&zilog->zl_vdev_tree);
1733185029Spjd	mutex_destroy(&zilog->zl_vdev_lock);
1734168404Spjd
1735219089Spjd	ASSERT(list_is_empty(&zilog->zl_itx_commit_list));
1736219089Spjd	list_destroy(&zilog->zl_itx_commit_list);
1737219089Spjd
1738219089Spjd	for (int i = 0; i < TXG_SIZE; i++) {
1739219089Spjd		/*
1740219089Spjd		 * It's possible for an itx to be generated that doesn't dirty
1741219089Spjd		 * a txg (e.g. ztest TX_TRUNCATE). So there's no zil_clean()
1742219089Spjd		 * callback to remove the entry. We remove those here.
1743219089Spjd		 *
1744219089Spjd		 * Also free up the ziltest itxs.
1745219089Spjd		 */
1746219089Spjd		if (zilog->zl_itxg[i].itxg_itxs)
1747219089Spjd			zil_itxg_clean(zilog->zl_itxg[i].itxg_itxs);
1748219089Spjd		mutex_destroy(&zilog->zl_itxg[i].itxg_lock);
1749219089Spjd	}
1750219089Spjd
1751168404Spjd	mutex_destroy(&zilog->zl_lock);
1752168404Spjd
1753185029Spjd	cv_destroy(&zilog->zl_cv_writer);
1754185029Spjd	cv_destroy(&zilog->zl_cv_suspend);
1755219089Spjd	cv_destroy(&zilog->zl_cv_batch[0]);
1756219089Spjd	cv_destroy(&zilog->zl_cv_batch[1]);
1757185029Spjd
1758168404Spjd	kmem_free(zilog, sizeof (zilog_t));
1759168404Spjd}
1760168404Spjd
1761168404Spjd/*
1762168404Spjd * Open an intent log.
1763168404Spjd */
1764168404Spjdzilog_t *
1765168404Spjdzil_open(objset_t *os, zil_get_data_t *get_data)
1766168404Spjd{
1767168404Spjd	zilog_t *zilog = dmu_objset_zil(os);
1768168404Spjd
1769224526Smm	ASSERT(zilog->zl_clean_taskq == NULL);
1770224526Smm	ASSERT(zilog->zl_get_data == NULL);
1771224526Smm	ASSERT(list_is_empty(&zilog->zl_lwb_list));
1772224526Smm
1773168404Spjd	zilog->zl_get_data = get_data;
1774168404Spjd	zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri,
1775168404Spjd	    2, 2, TASKQ_PREPOPULATE);
1776168404Spjd
1777168404Spjd	return (zilog);
1778168404Spjd}
1779168404Spjd
1780168404Spjd/*
1781168404Spjd * Close an intent log.
1782168404Spjd */
1783168404Spjdvoid
1784168404Spjdzil_close(zilog_t *zilog)
1785168404Spjd{
1786224526Smm	lwb_t *lwb;
1787219089Spjd	uint64_t txg = 0;
1788219089Spjd
1789219089Spjd	zil_commit(zilog, 0); /* commit all itx */
1790219089Spjd
1791168404Spjd	/*
1792219089Spjd	 * The lwb_max_txg for the stubby lwb will reflect the last activity
1793219089Spjd	 * for the zil.  After a txg_wait_synced() on the txg we know all the
1794219089Spjd	 * callbacks have occurred that may clean the zil.  Only then can we
1795219089Spjd	 * destroy the zl_clean_taskq.
1796168404Spjd	 */
1797219089Spjd	mutex_enter(&zilog->zl_lock);
1798224526Smm	lwb = list_tail(&zilog->zl_lwb_list);
1799224526Smm	if (lwb != NULL)
1800224526Smm		txg = lwb->lwb_max_txg;
1801219089Spjd	mutex_exit(&zilog->zl_lock);
1802219089Spjd	if (txg)
1803168404Spjd		txg_wait_synced(zilog->zl_dmu_pool, txg);
1804243674Smm	ASSERT(!zilog_is_dirty(zilog));
1805168404Spjd
1806168404Spjd	taskq_destroy(zilog->zl_clean_taskq);
1807168404Spjd	zilog->zl_clean_taskq = NULL;
1808168404Spjd	zilog->zl_get_data = NULL;
1809224526Smm
1810224526Smm	/*
1811224526Smm	 * We should have only one LWB left on the list; remove it now.
1812224526Smm	 */
1813224526Smm	mutex_enter(&zilog->zl_lock);
1814224526Smm	lwb = list_head(&zilog->zl_lwb_list);
1815224526Smm	if (lwb != NULL) {
1816224526Smm		ASSERT(lwb == list_tail(&zilog->zl_lwb_list));
1817224526Smm		list_remove(&zilog->zl_lwb_list, lwb);
1818224526Smm		zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
1819224526Smm		kmem_cache_free(zil_lwb_cache, lwb);
1820224526Smm	}
1821224526Smm	mutex_exit(&zilog->zl_lock);
1822168404Spjd}
1823168404Spjd
1824249643Smmstatic char *suspend_tag = "zil suspending";
1825249643Smm
1826168404Spjd/*
1827168404Spjd * Suspend an intent log.  While in suspended mode, we still honor
1828168404Spjd * synchronous semantics, but we rely on txg_wait_synced() to do it.
1829249643Smm * On old version pools, we suspend the log briefly when taking a
1830249643Smm * snapshot so that it will have an empty intent log.
1831249643Smm *
1832249643Smm * Long holds are not really intended to be used the way we do here --
1833249643Smm * held for such a short time.  A concurrent caller of dsl_dataset_long_held()
1834249643Smm * could fail.  Therefore we take pains to only put a long hold if it is
1835249643Smm * actually necessary.  Fortunately, it will only be necessary if the
1836249643Smm * objset is currently mounted (or the ZVOL equivalent).  In that case it
1837249643Smm * will already have a long hold, so we are not really making things any worse.
1838249643Smm *
1839249643Smm * Ideally, we would locate the existing long-holder (i.e. the zfsvfs_t or
1840249643Smm * zvol_state_t), and use their mechanism to prevent their hold from being
1841249643Smm * dropped (e.g. VFS_HOLD()).  However, that would be even more pain for
1842249643Smm * very little gain.
1843249643Smm *
1844249643Smm * if cookiep == NULL, this does both the suspend & resume.
1845249643Smm * Otherwise, it returns with the dataset "long held", and the cookie
1846249643Smm * should be passed into zil_resume().
1847168404Spjd */
1848168404Spjdint
1849249643Smmzil_suspend(const char *osname, void **cookiep)
1850168404Spjd{
1851249643Smm	objset_t *os;
1852249643Smm	zilog_t *zilog;
1853249643Smm	const zil_header_t *zh;
1854249643Smm	int error;
1855168404Spjd
1856249643Smm	error = dmu_objset_hold(osname, suspend_tag, &os);
1857249643Smm	if (error != 0)
1858249643Smm		return (error);
1859249643Smm	zilog = dmu_objset_zil(os);
1860249643Smm
1861168404Spjd	mutex_enter(&zilog->zl_lock);
1862249643Smm	zh = zilog->zl_header;
1863249643Smm
1864200724Sdelphij	if (zh->zh_flags & ZIL_REPLAY_NEEDED) {		/* unplayed log */
1865168404Spjd		mutex_exit(&zilog->zl_lock);
1866249643Smm		dmu_objset_rele(os, suspend_tag);
1867249643Smm		return (SET_ERROR(EBUSY));
1868168404Spjd	}
1869249643Smm
1870249643Smm	/*
1871249643Smm	 * Don't put a long hold in the cases where we can avoid it.  This
1872249643Smm	 * is when there is no cookie so we are doing a suspend & resume
1873249643Smm	 * (i.e. called from zil_vdev_offline()), and there's nothing to do
1874249643Smm	 * for the suspend because it's already suspended, or there's no ZIL.
1875249643Smm	 */
1876249643Smm	if (cookiep == NULL && !zilog->zl_suspending &&
1877249643Smm	    (zilog->zl_suspend > 0 || BP_IS_HOLE(&zh->zh_log))) {
1878249643Smm		mutex_exit(&zilog->zl_lock);
1879249643Smm		dmu_objset_rele(os, suspend_tag);
1880249643Smm		return (0);
1881249643Smm	}
1882249643Smm
1883249643Smm	dsl_dataset_long_hold(dmu_objset_ds(os), suspend_tag);
1884249643Smm	dsl_pool_rele(dmu_objset_pool(os), suspend_tag);
1885249643Smm
1886249643Smm	zilog->zl_suspend++;
1887249643Smm
1888249643Smm	if (zilog->zl_suspend > 1) {
1889168404Spjd		/*
1890249643Smm		 * Someone else is already suspending it.
1891168404Spjd		 * Just wait for them to finish.
1892168404Spjd		 */
1893249643Smm
1894168404Spjd		while (zilog->zl_suspending)
1895168404Spjd			cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock);
1896168404Spjd		mutex_exit(&zilog->zl_lock);
1897249643Smm
1898249643Smm		if (cookiep == NULL)
1899249643Smm			zil_resume(os);
1900249643Smm		else
1901249643Smm			*cookiep = os;
1902168404Spjd		return (0);
1903168404Spjd	}
1904249643Smm
1905249643Smm	/*
1906249643Smm	 * If there is no pointer to an on-disk block, this ZIL must not
1907249643Smm	 * be active (e.g. filesystem not mounted), so there's nothing
1908249643Smm	 * to clean up.
1909249643Smm	 */
1910249643Smm	if (BP_IS_HOLE(&zh->zh_log)) {
1911249643Smm		ASSERT(cookiep != NULL); /* fast path already handled */
1912249643Smm
1913249643Smm		*cookiep = os;
1914249643Smm		mutex_exit(&zilog->zl_lock);
1915249643Smm		return (0);
1916249643Smm	}
1917249643Smm
1918168404Spjd	zilog->zl_suspending = B_TRUE;
1919168404Spjd	mutex_exit(&zilog->zl_lock);
1920168404Spjd
1921219089Spjd	zil_commit(zilog, 0);
1922168404Spjd
1923168404Spjd	zil_destroy(zilog, B_FALSE);
1924168404Spjd
1925168404Spjd	mutex_enter(&zilog->zl_lock);
1926168404Spjd	zilog->zl_suspending = B_FALSE;
1927168404Spjd	cv_broadcast(&zilog->zl_cv_suspend);
1928168404Spjd	mutex_exit(&zilog->zl_lock);
1929168404Spjd
1930249643Smm	if (cookiep == NULL)
1931249643Smm		zil_resume(os);
1932249643Smm	else
1933249643Smm		*cookiep = os;
1934168404Spjd	return (0);
1935168404Spjd}
1936168404Spjd
1937168404Spjdvoid
1938249643Smmzil_resume(void *cookie)
1939168404Spjd{
1940249643Smm	objset_t *os = cookie;
1941249643Smm	zilog_t *zilog = dmu_objset_zil(os);
1942249643Smm
1943168404Spjd	mutex_enter(&zilog->zl_lock);
1944168404Spjd	ASSERT(zilog->zl_suspend != 0);
1945168404Spjd	zilog->zl_suspend--;
1946168404Spjd	mutex_exit(&zilog->zl_lock);
1947249643Smm	dsl_dataset_long_rele(dmu_objset_ds(os), suspend_tag);
1948249643Smm	dsl_dataset_rele(dmu_objset_ds(os), suspend_tag);
1949168404Spjd}
1950168404Spjd
1951219089Spjdtypedef struct zil_replay_arg {
1952219089Spjd	zil_replay_func_t **zr_replay;
1953219089Spjd	void		*zr_arg;
1954219089Spjd	boolean_t	zr_byteswap;
1955219089Spjd	char		*zr_lr;
1956219089Spjd} zil_replay_arg_t;
1957219089Spjd
1958219089Spjdstatic int
1959219089Spjdzil_replay_error(zilog_t *zilog, lr_t *lr, int error)
1960209962Smm{
1961219089Spjd	char name[MAXNAMELEN];
1962209962Smm
1963219089Spjd	zilog->zl_replaying_seq--;	/* didn't actually replay this one */
1964209962Smm
1965219089Spjd	dmu_objset_name(zilog->zl_os, name);
1966209962Smm
1967219089Spjd	cmn_err(CE_WARN, "ZFS replay transaction error %d, "
1968219089Spjd	    "dataset %s, seq 0x%llx, txtype %llu %s\n", error, name,
1969219089Spjd	    (u_longlong_t)lr->lrc_seq,
1970219089Spjd	    (u_longlong_t)(lr->lrc_txtype & ~TX_CI),
1971219089Spjd	    (lr->lrc_txtype & TX_CI) ? "CI" : "");
1972219089Spjd
1973219089Spjd	return (error);
1974209962Smm}
1975209962Smm
1976219089Spjdstatic int
1977168404Spjdzil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg)
1978168404Spjd{
1979168404Spjd	zil_replay_arg_t *zr = zra;
1980168404Spjd	const zil_header_t *zh = zilog->zl_header;
1981168404Spjd	uint64_t reclen = lr->lrc_reclen;
1982168404Spjd	uint64_t txtype = lr->lrc_txtype;
1983219089Spjd	int error = 0;
1984168404Spjd
1985219089Spjd	zilog->zl_replaying_seq = lr->lrc_seq;
1986168404Spjd
1987219089Spjd	if (lr->lrc_seq <= zh->zh_replay_seq)	/* already replayed */
1988219089Spjd		return (0);
1989219089Spjd
1990168404Spjd	if (lr->lrc_txg < claim_txg)		/* already committed */
1991219089Spjd		return (0);
1992168404Spjd
1993185029Spjd	/* Strip case-insensitive bit, still present in log record */
1994185029Spjd	txtype &= ~TX_CI;
1995185029Spjd
1996219089Spjd	if (txtype == 0 || txtype >= TX_MAX_TYPE)
1997219089Spjd		return (zil_replay_error(zilog, lr, EINVAL));
1998219089Spjd
1999219089Spjd	/*
2000219089Spjd	 * If this record type can be logged out of order, the object
2001219089Spjd	 * (lr_foid) may no longer exist.  That's legitimate, not an error.
2002219089Spjd	 */
2003219089Spjd	if (TX_OOO(txtype)) {
2004219089Spjd		error = dmu_object_info(zilog->zl_os,
2005219089Spjd		    ((lr_ooo_t *)lr)->lr_foid, NULL);
2006219089Spjd		if (error == ENOENT || error == EEXIST)
2007219089Spjd			return (0);
2008209962Smm	}
2009209962Smm
2010168404Spjd	/*
2011168404Spjd	 * Make a copy of the data so we can revise and extend it.
2012168404Spjd	 */
2013219089Spjd	bcopy(lr, zr->zr_lr, reclen);
2014168404Spjd
2015168404Spjd	/*
2016219089Spjd	 * If this is a TX_WRITE with a blkptr, suck in the data.
2017219089Spjd	 */
2018219089Spjd	if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) {
2019219089Spjd		error = zil_read_log_data(zilog, (lr_write_t *)lr,
2020219089Spjd		    zr->zr_lr + reclen);
2021249643Smm		if (error != 0)
2022219089Spjd			return (zil_replay_error(zilog, lr, error));
2023219089Spjd	}
2024219089Spjd
2025219089Spjd	/*
2026168404Spjd	 * The log block containing this lr may have been byteswapped
2027168404Spjd	 * so that we can easily examine common fields like lrc_txtype.
2028219089Spjd	 * However, the log is a mix of different record types, and only the
2029168404Spjd	 * replay vectors know how to byteswap their records.  Therefore, if
2030168404Spjd	 * the lr was byteswapped, undo it before invoking the replay vector.
2031168404Spjd	 */
2032168404Spjd	if (zr->zr_byteswap)
2033219089Spjd		byteswap_uint64_array(zr->zr_lr, reclen);
2034168404Spjd
2035168404Spjd	/*
2036168404Spjd	 * We must now do two things atomically: replay this log record,
2037209962Smm	 * and update the log header sequence number to reflect the fact that
2038209962Smm	 * we did so. At the end of each replay function the sequence number
2039209962Smm	 * is updated if we are in replay mode.
2040168404Spjd	 */
2041219089Spjd	error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lr, zr->zr_byteswap);
2042249643Smm	if (error != 0) {
2043168404Spjd		/*
2044168404Spjd		 * The DMU's dnode layer doesn't see removes until the txg
2045168404Spjd		 * commits, so a subsequent claim can spuriously fail with
2046209962Smm		 * EEXIST. So if we receive any error we try syncing out
2047219089Spjd		 * any removes then retry the transaction.  Note that we
2048219089Spjd		 * specify B_FALSE for byteswap now, so we don't do it twice.
2049168404Spjd		 */
2050219089Spjd		txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0);
2051219089Spjd		error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lr, B_FALSE);
2052249643Smm		if (error != 0)
2053219089Spjd			return (zil_replay_error(zilog, lr, error));
2054168404Spjd	}
2055219089Spjd	return (0);
2056168404Spjd}
2057168404Spjd
2058168404Spjd/* ARGSUSED */
2059219089Spjdstatic int
2060168404Spjdzil_incr_blks(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
2061168404Spjd{
2062168404Spjd	zilog->zl_replay_blks++;
2063219089Spjd
2064219089Spjd	return (0);
2065168404Spjd}
2066168404Spjd
2067168404Spjd/*
2068168404Spjd * If this dataset has a non-empty intent log, replay it and destroy it.
2069168404Spjd */
2070168404Spjdvoid
2071209962Smmzil_replay(objset_t *os, void *arg, zil_replay_func_t *replay_func[TX_MAX_TYPE])
2072168404Spjd{
2073168404Spjd	zilog_t *zilog = dmu_objset_zil(os);
2074168404Spjd	const zil_header_t *zh = zilog->zl_header;
2075168404Spjd	zil_replay_arg_t zr;
2076168404Spjd
2077200724Sdelphij	if ((zh->zh_flags & ZIL_REPLAY_NEEDED) == 0) {
2078168404Spjd		zil_destroy(zilog, B_TRUE);
2079168404Spjd		return;
2080168404Spjd	}
2081168404Spjd	//printf("ZFS: Replaying ZIL on %s...\n", os->os->os_spa->spa_name);
2082168404Spjd
2083168404Spjd	zr.zr_replay = replay_func;
2084168404Spjd	zr.zr_arg = arg;
2085168404Spjd	zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log);
2086219089Spjd	zr.zr_lr = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP);
2087168404Spjd
2088168404Spjd	/*
2089168404Spjd	 * Wait for in-progress removes to sync before starting replay.
2090168404Spjd	 */
2091168404Spjd	txg_wait_synced(zilog->zl_dmu_pool, 0);
2092168404Spjd
2093209962Smm	zilog->zl_replay = B_TRUE;
2094219089Spjd	zilog->zl_replay_time = ddi_get_lbolt();
2095168404Spjd	ASSERT(zilog->zl_replay_blks == 0);
2096168404Spjd	(void) zil_parse(zilog, zil_incr_blks, zil_replay_log_record, &zr,
2097168404Spjd	    zh->zh_claim_txg);
2098219089Spjd	kmem_free(zr.zr_lr, 2 * SPA_MAXBLOCKSIZE);
2099168404Spjd
2100168404Spjd	zil_destroy(zilog, B_FALSE);
2101185029Spjd	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
2102209962Smm	zilog->zl_replay = B_FALSE;
2103168404Spjd	//printf("ZFS: Replay of ZIL on %s finished.\n", os->os->os_spa->spa_name);
2104168404Spjd}
2105168404Spjd
2106219089Spjdboolean_t
2107219089Spjdzil_replaying(zilog_t *zilog, dmu_tx_t *tx)
2108168404Spjd{
2109219089Spjd	if (zilog->zl_sync == ZFS_SYNC_DISABLED)
2110219089Spjd		return (B_TRUE);
2111168404Spjd
2112219089Spjd	if (zilog->zl_replay) {
2113219089Spjd		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
2114219089Spjd		zilog->zl_replayed_seq[dmu_tx_get_txg(tx) & TXG_MASK] =
2115219089Spjd		    zilog->zl_replaying_seq;
2116219089Spjd		return (B_TRUE);
2117168404Spjd	}
2118168404Spjd
2119219089Spjd	return (B_FALSE);
2120168404Spjd}
2121213197Smm
2122213197Smm/* ARGSUSED */
2123213197Smmint
2124219089Spjdzil_vdev_offline(const char *osname, void *arg)
2125213197Smm{
2126213197Smm	int error;
2127213197Smm
2128249643Smm	error = zil_suspend(osname, NULL);
2129249643Smm	if (error != 0)
2130249643Smm		return (SET_ERROR(EEXIST));
2131249643Smm	return (0);
2132213197Smm}
2133