xref: /illumos-gate/usr/src/uts/common/fs/zfs/zil.c (revision 55da60b91d96984f12de050ce428373ea25c7f35)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 
25 /* Portions Copyright 2010 Robert Milkowski */
26 
27 #include <sys/zfs_context.h>
28 #include <sys/spa.h>
29 #include <sys/dmu.h>
30 #include <sys/zap.h>
31 #include <sys/arc.h>
32 #include <sys/stat.h>
33 #include <sys/resource.h>
34 #include <sys/zil.h>
35 #include <sys/zil_impl.h>
36 #include <sys/dsl_dataset.h>
37 #include <sys/vdev.h>
38 #include <sys/dmu_tx.h>
39 
40 /*
41  * The zfs intent log (ZIL) saves transaction records of system calls
42  * that change the file system in memory with enough information
43  * to be able to replay them. These are stored in memory until
44  * either the DMU transaction group (txg) commits them to the stable pool
45  * and they can be discarded, or they are flushed to the stable log
46  * (also in the pool) due to a fsync, O_DSYNC or other synchronous
47  * requirement. In the event of a panic or power fail then those log
48  * records (transactions) are replayed.
49  *
50  * There is one ZIL per file system. Its on-disk (pool) format consists
51  * of 3 parts:
52  *
53  * 	- ZIL header
54  * 	- ZIL blocks
55  * 	- ZIL records
56  *
57  * A log record holds a system call transaction. Log blocks can
58  * hold many log records and the blocks are chained together.
59  * Each ZIL block contains a block pointer (blkptr_t) to the next
60  * ZIL block in the chain. The ZIL header points to the first
61  * block in the chain. Note there is not a fixed place in the pool
62  * to hold blocks. They are dynamically allocated and freed as
63  * needed from the blocks available. Figure X shows the ZIL structure:
64  */
65 
66 /*
67  * This global ZIL switch affects all pools
68  */
69 int zil_replay_disable = 0;    /* disable intent logging replay */
70 
71 /*
72  * Tunable parameter for debugging or performance analysis.  Setting
73  * zfs_nocacheflush will cause corruption on power loss if a volatile
74  * out-of-order write cache is enabled.
75  */
76 boolean_t zfs_nocacheflush = B_FALSE;
77 
78 static kmem_cache_t *zil_lwb_cache;
79 
80 static boolean_t zil_empty(zilog_t *zilog);
81 
82 #define	LWB_EMPTY(lwb) ((BP_GET_LSIZE(&lwb->lwb_blk) - \
83     sizeof (zil_chain_t)) == (lwb->lwb_sz - lwb->lwb_nused))
84 
85 
86 static int
87 zil_bp_compare(const void *x1, const void *x2)
88 {
89 	const dva_t *dva1 = &((zil_bp_node_t *)x1)->zn_dva;
90 	const dva_t *dva2 = &((zil_bp_node_t *)x2)->zn_dva;
91 
92 	if (DVA_GET_VDEV(dva1) < DVA_GET_VDEV(dva2))
93 		return (-1);
94 	if (DVA_GET_VDEV(dva1) > DVA_GET_VDEV(dva2))
95 		return (1);
96 
97 	if (DVA_GET_OFFSET(dva1) < DVA_GET_OFFSET(dva2))
98 		return (-1);
99 	if (DVA_GET_OFFSET(dva1) > DVA_GET_OFFSET(dva2))
100 		return (1);
101 
102 	return (0);
103 }
104 
105 static void
106 zil_bp_tree_init(zilog_t *zilog)
107 {
108 	avl_create(&zilog->zl_bp_tree, zil_bp_compare,
109 	    sizeof (zil_bp_node_t), offsetof(zil_bp_node_t, zn_node));
110 }
111 
112 static void
113 zil_bp_tree_fini(zilog_t *zilog)
114 {
115 	avl_tree_t *t = &zilog->zl_bp_tree;
116 	zil_bp_node_t *zn;
117 	void *cookie = NULL;
118 
119 	while ((zn = avl_destroy_nodes(t, &cookie)) != NULL)
120 		kmem_free(zn, sizeof (zil_bp_node_t));
121 
122 	avl_destroy(t);
123 }
124 
125 int
126 zil_bp_tree_add(zilog_t *zilog, const blkptr_t *bp)
127 {
128 	avl_tree_t *t = &zilog->zl_bp_tree;
129 	const dva_t *dva = BP_IDENTITY(bp);
130 	zil_bp_node_t *zn;
131 	avl_index_t where;
132 
133 	if (avl_find(t, dva, &where) != NULL)
134 		return (EEXIST);
135 
136 	zn = kmem_alloc(sizeof (zil_bp_node_t), KM_SLEEP);
137 	zn->zn_dva = *dva;
138 	avl_insert(t, zn, where);
139 
140 	return (0);
141 }
142 
143 static zil_header_t *
144 zil_header_in_syncing_context(zilog_t *zilog)
145 {
146 	return ((zil_header_t *)zilog->zl_header);
147 }
148 
149 static void
150 zil_init_log_chain(zilog_t *zilog, blkptr_t *bp)
151 {
152 	zio_cksum_t *zc = &bp->blk_cksum;
153 
154 	zc->zc_word[ZIL_ZC_GUID_0] = spa_get_random(-1ULL);
155 	zc->zc_word[ZIL_ZC_GUID_1] = spa_get_random(-1ULL);
156 	zc->zc_word[ZIL_ZC_OBJSET] = dmu_objset_id(zilog->zl_os);
157 	zc->zc_word[ZIL_ZC_SEQ] = 1ULL;
158 }
159 
160 /*
161  * Read a log block and make sure it's valid.
162  */
163 static int
164 zil_read_log_block(zilog_t *zilog, const blkptr_t *bp, blkptr_t *nbp, void *dst,
165     char **end)
166 {
167 	enum zio_flag zio_flags = ZIO_FLAG_CANFAIL;
168 	uint32_t aflags = ARC_WAIT;
169 	arc_buf_t *abuf = NULL;
170 	zbookmark_t zb;
171 	int error;
172 
173 	if (zilog->zl_header->zh_claim_txg == 0)
174 		zio_flags |= ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB;
175 
176 	if (!(zilog->zl_header->zh_flags & ZIL_CLAIM_LR_SEQ_VALID))
177 		zio_flags |= ZIO_FLAG_SPECULATIVE;
178 
179 	SET_BOOKMARK(&zb, bp->blk_cksum.zc_word[ZIL_ZC_OBJSET],
180 	    ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, bp->blk_cksum.zc_word[ZIL_ZC_SEQ]);
181 
182 	error = arc_read_nolock(NULL, zilog->zl_spa, bp, arc_getbuf_func, &abuf,
183 	    ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb);
184 
185 	if (error == 0) {
186 		zio_cksum_t cksum = bp->blk_cksum;
187 
188 		/*
189 		 * Validate the checksummed log block.
190 		 *
191 		 * Sequence numbers should be... sequential.  The checksum
192 		 * verifier for the next block should be bp's checksum plus 1.
193 		 *
194 		 * Also check the log chain linkage and size used.
195 		 */
196 		cksum.zc_word[ZIL_ZC_SEQ]++;
197 
198 		if (BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_ZILOG2) {
199 			zil_chain_t *zilc = abuf->b_data;
200 			char *lr = (char *)(zilc + 1);
201 			uint64_t len = zilc->zc_nused - sizeof (zil_chain_t);
202 
203 			if (bcmp(&cksum, &zilc->zc_next_blk.blk_cksum,
204 			    sizeof (cksum)) || BP_IS_HOLE(&zilc->zc_next_blk)) {
205 				error = ECKSUM;
206 			} else {
207 				bcopy(lr, dst, len);
208 				*end = (char *)dst + len;
209 				*nbp = zilc->zc_next_blk;
210 			}
211 		} else {
212 			char *lr = abuf->b_data;
213 			uint64_t size = BP_GET_LSIZE(bp);
214 			zil_chain_t *zilc = (zil_chain_t *)(lr + size) - 1;
215 
216 			if (bcmp(&cksum, &zilc->zc_next_blk.blk_cksum,
217 			    sizeof (cksum)) || BP_IS_HOLE(&zilc->zc_next_blk) ||
218 			    (zilc->zc_nused > (size - sizeof (*zilc)))) {
219 				error = ECKSUM;
220 			} else {
221 				bcopy(lr, dst, zilc->zc_nused);
222 				*end = (char *)dst + zilc->zc_nused;
223 				*nbp = zilc->zc_next_blk;
224 			}
225 		}
226 
227 		VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
228 	}
229 
230 	return (error);
231 }
232 
233 /*
234  * Read a TX_WRITE log data block.
235  */
236 static int
237 zil_read_log_data(zilog_t *zilog, const lr_write_t *lr, void *wbuf)
238 {
239 	enum zio_flag zio_flags = ZIO_FLAG_CANFAIL;
240 	const blkptr_t *bp = &lr->lr_blkptr;
241 	uint32_t aflags = ARC_WAIT;
242 	arc_buf_t *abuf = NULL;
243 	zbookmark_t zb;
244 	int error;
245 
246 	if (BP_IS_HOLE(bp)) {
247 		if (wbuf != NULL)
248 			bzero(wbuf, MAX(BP_GET_LSIZE(bp), lr->lr_length));
249 		return (0);
250 	}
251 
252 	if (zilog->zl_header->zh_claim_txg == 0)
253 		zio_flags |= ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB;
254 
255 	SET_BOOKMARK(&zb, dmu_objset_id(zilog->zl_os), lr->lr_foid,
256 	    ZB_ZIL_LEVEL, lr->lr_offset / BP_GET_LSIZE(bp));
257 
258 	error = arc_read_nolock(NULL, zilog->zl_spa, bp, arc_getbuf_func, &abuf,
259 	    ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb);
260 
261 	if (error == 0) {
262 		if (wbuf != NULL)
263 			bcopy(abuf->b_data, wbuf, arc_buf_size(abuf));
264 		(void) arc_buf_remove_ref(abuf, &abuf);
265 	}
266 
267 	return (error);
268 }
269 
270 /*
271  * Parse the intent log, and call parse_func for each valid record within.
272  */
273 int
274 zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func,
275     zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg)
276 {
277 	const zil_header_t *zh = zilog->zl_header;
278 	boolean_t claimed = !!zh->zh_claim_txg;
279 	uint64_t claim_blk_seq = claimed ? zh->zh_claim_blk_seq : UINT64_MAX;
280 	uint64_t claim_lr_seq = claimed ? zh->zh_claim_lr_seq : UINT64_MAX;
281 	uint64_t max_blk_seq = 0;
282 	uint64_t max_lr_seq = 0;
283 	uint64_t blk_count = 0;
284 	uint64_t lr_count = 0;
285 	blkptr_t blk, next_blk;
286 	char *lrbuf, *lrp;
287 	int error = 0;
288 
289 	/*
290 	 * Old logs didn't record the maximum zh_claim_lr_seq.
291 	 */
292 	if (!(zh->zh_flags & ZIL_CLAIM_LR_SEQ_VALID))
293 		claim_lr_seq = UINT64_MAX;
294 
295 	/*
296 	 * Starting at the block pointed to by zh_log we read the log chain.
297 	 * For each block in the chain we strongly check that block to
298 	 * ensure its validity.  We stop when an invalid block is found.
299 	 * For each block pointer in the chain we call parse_blk_func().
300 	 * For each record in each valid block we call parse_lr_func().
301 	 * If the log has been claimed, stop if we encounter a sequence
302 	 * number greater than the highest claimed sequence number.
303 	 */
304 	lrbuf = zio_buf_alloc(SPA_MAXBLOCKSIZE);
305 	zil_bp_tree_init(zilog);
306 
307 	for (blk = zh->zh_log; !BP_IS_HOLE(&blk); blk = next_blk) {
308 		uint64_t blk_seq = blk.blk_cksum.zc_word[ZIL_ZC_SEQ];
309 		int reclen;
310 		char *end;
311 
312 		if (blk_seq > claim_blk_seq)
313 			break;
314 		if ((error = parse_blk_func(zilog, &blk, arg, txg)) != 0)
315 			break;
316 		ASSERT3U(max_blk_seq, <, blk_seq);
317 		max_blk_seq = blk_seq;
318 		blk_count++;
319 
320 		if (max_lr_seq == claim_lr_seq && max_blk_seq == claim_blk_seq)
321 			break;
322 
323 		error = zil_read_log_block(zilog, &blk, &next_blk, lrbuf, &end);
324 		if (error)
325 			break;
326 
327 		for (lrp = lrbuf; lrp < end; lrp += reclen) {
328 			lr_t *lr = (lr_t *)lrp;
329 			reclen = lr->lrc_reclen;
330 			ASSERT3U(reclen, >=, sizeof (lr_t));
331 			if (lr->lrc_seq > claim_lr_seq)
332 				goto done;
333 			if ((error = parse_lr_func(zilog, lr, arg, txg)) != 0)
334 				goto done;
335 			ASSERT3U(max_lr_seq, <, lr->lrc_seq);
336 			max_lr_seq = lr->lrc_seq;
337 			lr_count++;
338 		}
339 	}
340 done:
341 	zilog->zl_parse_error = error;
342 	zilog->zl_parse_blk_seq = max_blk_seq;
343 	zilog->zl_parse_lr_seq = max_lr_seq;
344 	zilog->zl_parse_blk_count = blk_count;
345 	zilog->zl_parse_lr_count = lr_count;
346 
347 	ASSERT(!claimed || !(zh->zh_flags & ZIL_CLAIM_LR_SEQ_VALID) ||
348 	    (max_blk_seq == claim_blk_seq && max_lr_seq == claim_lr_seq));
349 
350 	zil_bp_tree_fini(zilog);
351 	zio_buf_free(lrbuf, SPA_MAXBLOCKSIZE);
352 
353 	return (error);
354 }
355 
356 static int
357 zil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg)
358 {
359 	/*
360 	 * Claim log block if not already committed and not already claimed.
361 	 * If tx == NULL, just verify that the block is claimable.
362 	 */
363 	if (bp->blk_birth < first_txg || zil_bp_tree_add(zilog, bp) != 0)
364 		return (0);
365 
366 	return (zio_wait(zio_claim(NULL, zilog->zl_spa,
367 	    tx == NULL ? 0 : first_txg, bp, spa_claim_notify, NULL,
368 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB)));
369 }
370 
371 static int
372 zil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg)
373 {
374 	lr_write_t *lr = (lr_write_t *)lrc;
375 	int error;
376 
377 	if (lrc->lrc_txtype != TX_WRITE)
378 		return (0);
379 
380 	/*
381 	 * If the block is not readable, don't claim it.  This can happen
382 	 * in normal operation when a log block is written to disk before
383 	 * some of the dmu_sync() blocks it points to.  In this case, the
384 	 * transaction cannot have been committed to anyone (we would have
385 	 * waited for all writes to be stable first), so it is semantically
386 	 * correct to declare this the end of the log.
387 	 */
388 	if (lr->lr_blkptr.blk_birth >= first_txg &&
389 	    (error = zil_read_log_data(zilog, lr, NULL)) != 0)
390 		return (error);
391 	return (zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg));
392 }
393 
394 /* ARGSUSED */
395 static int
396 zil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg)
397 {
398 	zio_free_zil(zilog->zl_spa, dmu_tx_get_txg(tx), bp);
399 
400 	return (0);
401 }
402 
403 static int
404 zil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg)
405 {
406 	lr_write_t *lr = (lr_write_t *)lrc;
407 	blkptr_t *bp = &lr->lr_blkptr;
408 
409 	/*
410 	 * If we previously claimed it, we need to free it.
411 	 */
412 	if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE &&
413 	    bp->blk_birth >= claim_txg && zil_bp_tree_add(zilog, bp) == 0)
414 		zio_free(zilog->zl_spa, dmu_tx_get_txg(tx), bp);
415 
416 	return (0);
417 }
418 
419 static lwb_t *
420 zil_alloc_lwb(zilog_t *zilog, blkptr_t *bp, uint64_t txg)
421 {
422 	lwb_t *lwb;
423 
424 	lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP);
425 	lwb->lwb_zilog = zilog;
426 	lwb->lwb_blk = *bp;
427 	lwb->lwb_buf = zio_buf_alloc(BP_GET_LSIZE(bp));
428 	lwb->lwb_max_txg = txg;
429 	lwb->lwb_zio = NULL;
430 	lwb->lwb_tx = NULL;
431 	if (BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_ZILOG2) {
432 		lwb->lwb_nused = sizeof (zil_chain_t);
433 		lwb->lwb_sz = BP_GET_LSIZE(bp);
434 	} else {
435 		lwb->lwb_nused = 0;
436 		lwb->lwb_sz = BP_GET_LSIZE(bp) - sizeof (zil_chain_t);
437 	}
438 
439 	mutex_enter(&zilog->zl_lock);
440 	list_insert_tail(&zilog->zl_lwb_list, lwb);
441 	mutex_exit(&zilog->zl_lock);
442 
443 	return (lwb);
444 }
445 
446 /*
447  * Create an on-disk intent log.
448  */
449 static lwb_t *
450 zil_create(zilog_t *zilog)
451 {
452 	const zil_header_t *zh = zilog->zl_header;
453 	lwb_t *lwb = NULL;
454 	uint64_t txg = 0;
455 	dmu_tx_t *tx = NULL;
456 	blkptr_t blk;
457 	int error = 0;
458 
459 	/*
460 	 * Wait for any previous destroy to complete.
461 	 */
462 	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
463 
464 	ASSERT(zh->zh_claim_txg == 0);
465 	ASSERT(zh->zh_replay_seq == 0);
466 
467 	blk = zh->zh_log;
468 
469 	/*
470 	 * Allocate an initial log block if:
471 	 *    - there isn't one already
472 	 *    - the existing block is the wrong endianess
473 	 */
474 	if (BP_IS_HOLE(&blk) || BP_SHOULD_BYTESWAP(&blk)) {
475 		tx = dmu_tx_create(zilog->zl_os);
476 		VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0);
477 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
478 		txg = dmu_tx_get_txg(tx);
479 
480 		if (!BP_IS_HOLE(&blk)) {
481 			zio_free_zil(zilog->zl_spa, txg, &blk);
482 			BP_ZERO(&blk);
483 		}
484 
485 		error = zio_alloc_zil(zilog->zl_spa, txg, &blk, NULL,
486 		    ZIL_MIN_BLKSZ, zilog->zl_logbias == ZFS_LOGBIAS_LATENCY);
487 
488 		if (error == 0)
489 			zil_init_log_chain(zilog, &blk);
490 	}
491 
492 	/*
493 	 * Allocate a log write buffer (lwb) for the first log block.
494 	 */
495 	if (error == 0)
496 		lwb = zil_alloc_lwb(zilog, &blk, txg);
497 
498 	/*
499 	 * If we just allocated the first log block, commit our transaction
500 	 * and wait for zil_sync() to stuff the block poiner into zh_log.
501 	 * (zh is part of the MOS, so we cannot modify it in open context.)
502 	 */
503 	if (tx != NULL) {
504 		dmu_tx_commit(tx);
505 		txg_wait_synced(zilog->zl_dmu_pool, txg);
506 	}
507 
508 	ASSERT(bcmp(&blk, &zh->zh_log, sizeof (blk)) == 0);
509 
510 	return (lwb);
511 }
512 
513 /*
514  * In one tx, free all log blocks and clear the log header.
515  * If keep_first is set, then we're replaying a log with no content.
516  * We want to keep the first block, however, so that the first
517  * synchronous transaction doesn't require a txg_wait_synced()
518  * in zil_create().  We don't need to txg_wait_synced() here either
519  * when keep_first is set, because both zil_create() and zil_destroy()
520  * will wait for any in-progress destroys to complete.
521  */
522 void
523 zil_destroy(zilog_t *zilog, boolean_t keep_first)
524 {
525 	const zil_header_t *zh = zilog->zl_header;
526 	lwb_t *lwb;
527 	dmu_tx_t *tx;
528 	uint64_t txg;
529 
530 	/*
531 	 * Wait for any previous destroy to complete.
532 	 */
533 	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
534 
535 	zilog->zl_old_header = *zh;		/* debugging aid */
536 
537 	if (BP_IS_HOLE(&zh->zh_log))
538 		return;
539 
540 	tx = dmu_tx_create(zilog->zl_os);
541 	VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0);
542 	dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
543 	txg = dmu_tx_get_txg(tx);
544 
545 	mutex_enter(&zilog->zl_lock);
546 
547 	ASSERT3U(zilog->zl_destroy_txg, <, txg);
548 	zilog->zl_destroy_txg = txg;
549 	zilog->zl_keep_first = keep_first;
550 
551 	if (!list_is_empty(&zilog->zl_lwb_list)) {
552 		ASSERT(zh->zh_claim_txg == 0);
553 		ASSERT(!keep_first);
554 		while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
555 			list_remove(&zilog->zl_lwb_list, lwb);
556 			if (lwb->lwb_buf != NULL)
557 				zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
558 			zio_free_zil(zilog->zl_spa, txg, &lwb->lwb_blk);
559 			kmem_cache_free(zil_lwb_cache, lwb);
560 		}
561 	} else if (!keep_first) {
562 		(void) zil_parse(zilog, zil_free_log_block,
563 		    zil_free_log_record, tx, zh->zh_claim_txg);
564 	}
565 	mutex_exit(&zilog->zl_lock);
566 
567 	dmu_tx_commit(tx);
568 }
569 
570 int
571 zil_claim(const char *osname, void *txarg)
572 {
573 	dmu_tx_t *tx = txarg;
574 	uint64_t first_txg = dmu_tx_get_txg(tx);
575 	zilog_t *zilog;
576 	zil_header_t *zh;
577 	objset_t *os;
578 	int error;
579 
580 	error = dmu_objset_hold(osname, FTAG, &os);
581 	if (error) {
582 		cmn_err(CE_WARN, "can't open objset for %s", osname);
583 		return (0);
584 	}
585 
586 	zilog = dmu_objset_zil(os);
587 	zh = zil_header_in_syncing_context(zilog);
588 
589 	if (spa_get_log_state(zilog->zl_spa) == SPA_LOG_CLEAR) {
590 		if (!BP_IS_HOLE(&zh->zh_log))
591 			zio_free_zil(zilog->zl_spa, first_txg, &zh->zh_log);
592 		BP_ZERO(&zh->zh_log);
593 		dsl_dataset_dirty(dmu_objset_ds(os), tx);
594 		dmu_objset_rele(os, FTAG);
595 		return (0);
596 	}
597 
598 	/*
599 	 * Claim all log blocks if we haven't already done so, and remember
600 	 * the highest claimed sequence number.  This ensures that if we can
601 	 * read only part of the log now (e.g. due to a missing device),
602 	 * but we can read the entire log later, we will not try to replay
603 	 * or destroy beyond the last block we successfully claimed.
604 	 */
605 	ASSERT3U(zh->zh_claim_txg, <=, first_txg);
606 	if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) {
607 		(void) zil_parse(zilog, zil_claim_log_block,
608 		    zil_claim_log_record, tx, first_txg);
609 		zh->zh_claim_txg = first_txg;
610 		zh->zh_claim_blk_seq = zilog->zl_parse_blk_seq;
611 		zh->zh_claim_lr_seq = zilog->zl_parse_lr_seq;
612 		if (zilog->zl_parse_lr_count || zilog->zl_parse_blk_count > 1)
613 			zh->zh_flags |= ZIL_REPLAY_NEEDED;
614 		zh->zh_flags |= ZIL_CLAIM_LR_SEQ_VALID;
615 		dsl_dataset_dirty(dmu_objset_ds(os), tx);
616 	}
617 
618 	ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1));
619 	dmu_objset_rele(os, FTAG);
620 	return (0);
621 }
622 
623 /*
624  * Check the log by walking the log chain.
625  * Checksum errors are ok as they indicate the end of the chain.
626  * Any other error (no device or read failure) returns an error.
627  */
628 int
629 zil_check_log_chain(const char *osname, void *tx)
630 {
631 	zilog_t *zilog;
632 	objset_t *os;
633 	int error;
634 
635 	ASSERT(tx == NULL);
636 
637 	error = dmu_objset_hold(osname, FTAG, &os);
638 	if (error) {
639 		cmn_err(CE_WARN, "can't open objset for %s", osname);
640 		return (0);
641 	}
642 
643 	zilog = dmu_objset_zil(os);
644 
645 	/*
646 	 * Because tx == NULL, zil_claim_log_block() will not actually claim
647 	 * any blocks, but just determine whether it is possible to do so.
648 	 * In addition to checking the log chain, zil_claim_log_block()
649 	 * will invoke zio_claim() with a done func of spa_claim_notify(),
650 	 * which will update spa_max_claim_txg.  See spa_load() for details.
651 	 */
652 	error = zil_parse(zilog, zil_claim_log_block, zil_claim_log_record, tx,
653 	    zilog->zl_header->zh_claim_txg ? -1ULL : spa_first_txg(os->os_spa));
654 
655 	dmu_objset_rele(os, FTAG);
656 
657 	return ((error == ECKSUM || error == ENOENT) ? 0 : error);
658 }
659 
660 static int
661 zil_vdev_compare(const void *x1, const void *x2)
662 {
663 	uint64_t v1 = ((zil_vdev_node_t *)x1)->zv_vdev;
664 	uint64_t v2 = ((zil_vdev_node_t *)x2)->zv_vdev;
665 
666 	if (v1 < v2)
667 		return (-1);
668 	if (v1 > v2)
669 		return (1);
670 
671 	return (0);
672 }
673 
674 void
675 zil_add_block(zilog_t *zilog, const blkptr_t *bp)
676 {
677 	avl_tree_t *t = &zilog->zl_vdev_tree;
678 	avl_index_t where;
679 	zil_vdev_node_t *zv, zvsearch;
680 	int ndvas = BP_GET_NDVAS(bp);
681 	int i;
682 
683 	if (zfs_nocacheflush)
684 		return;
685 
686 	ASSERT(zilog->zl_writer);
687 
688 	/*
689 	 * Even though we're zl_writer, we still need a lock because the
690 	 * zl_get_data() callbacks may have dmu_sync() done callbacks
691 	 * that will run concurrently.
692 	 */
693 	mutex_enter(&zilog->zl_vdev_lock);
694 	for (i = 0; i < ndvas; i++) {
695 		zvsearch.zv_vdev = DVA_GET_VDEV(&bp->blk_dva[i]);
696 		if (avl_find(t, &zvsearch, &where) == NULL) {
697 			zv = kmem_alloc(sizeof (*zv), KM_SLEEP);
698 			zv->zv_vdev = zvsearch.zv_vdev;
699 			avl_insert(t, zv, where);
700 		}
701 	}
702 	mutex_exit(&zilog->zl_vdev_lock);
703 }
704 
705 void
706 zil_flush_vdevs(zilog_t *zilog)
707 {
708 	spa_t *spa = zilog->zl_spa;
709 	avl_tree_t *t = &zilog->zl_vdev_tree;
710 	void *cookie = NULL;
711 	zil_vdev_node_t *zv;
712 	zio_t *zio;
713 
714 	ASSERT(zilog->zl_writer);
715 
716 	/*
717 	 * We don't need zl_vdev_lock here because we're the zl_writer,
718 	 * and all zl_get_data() callbacks are done.
719 	 */
720 	if (avl_numnodes(t) == 0)
721 		return;
722 
723 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
724 
725 	zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
726 
727 	while ((zv = avl_destroy_nodes(t, &cookie)) != NULL) {
728 		vdev_t *vd = vdev_lookup_top(spa, zv->zv_vdev);
729 		if (vd != NULL)
730 			zio_flush(zio, vd);
731 		kmem_free(zv, sizeof (*zv));
732 	}
733 
734 	/*
735 	 * Wait for all the flushes to complete.  Not all devices actually
736 	 * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails.
737 	 */
738 	(void) zio_wait(zio);
739 
740 	spa_config_exit(spa, SCL_STATE, FTAG);
741 }
742 
743 /*
744  * Function called when a log block write completes
745  */
746 static void
747 zil_lwb_write_done(zio_t *zio)
748 {
749 	lwb_t *lwb = zio->io_private;
750 	zilog_t *zilog = lwb->lwb_zilog;
751 	dmu_tx_t *tx = lwb->lwb_tx;
752 
753 	ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF);
754 	ASSERT(BP_GET_TYPE(zio->io_bp) == DMU_OT_INTENT_LOG);
755 	ASSERT(BP_GET_LEVEL(zio->io_bp) == 0);
756 	ASSERT(BP_GET_BYTEORDER(zio->io_bp) == ZFS_HOST_BYTEORDER);
757 	ASSERT(!BP_IS_GANG(zio->io_bp));
758 	ASSERT(!BP_IS_HOLE(zio->io_bp));
759 	ASSERT(zio->io_bp->blk_fill == 0);
760 
761 	/*
762 	 * Ensure the lwb buffer pointer is cleared before releasing
763 	 * the txg. If we have had an allocation failure and
764 	 * the txg is waiting to sync then we want want zil_sync()
765 	 * to remove the lwb so that it's not picked up as the next new
766 	 * one in zil_commit_writer(). zil_sync() will only remove
767 	 * the lwb if lwb_buf is null.
768 	 */
769 	zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
770 	mutex_enter(&zilog->zl_lock);
771 	lwb->lwb_buf = NULL;
772 	lwb->lwb_tx = NULL;
773 	mutex_exit(&zilog->zl_lock);
774 
775 	/*
776 	 * Now that we've written this log block, we have a stable pointer
777 	 * to the next block in the chain, so it's OK to let the txg in
778 	 * which we allocated the next block sync.
779 	 */
780 	dmu_tx_commit(tx);
781 }
782 
783 /*
784  * Initialize the io for a log block.
785  */
786 static void
787 zil_lwb_write_init(zilog_t *zilog, lwb_t *lwb)
788 {
789 	zbookmark_t zb;
790 
791 	SET_BOOKMARK(&zb, lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET],
792 	    ZB_ZIL_OBJECT, ZB_ZIL_LEVEL,
793 	    lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ]);
794 
795 	if (zilog->zl_root_zio == NULL) {
796 		zilog->zl_root_zio = zio_root(zilog->zl_spa, NULL, NULL,
797 		    ZIO_FLAG_CANFAIL);
798 	}
799 	if (lwb->lwb_zio == NULL) {
800 		lwb->lwb_zio = zio_rewrite(zilog->zl_root_zio, zilog->zl_spa,
801 		    0, &lwb->lwb_blk, lwb->lwb_buf, BP_GET_LSIZE(&lwb->lwb_blk),
802 		    zil_lwb_write_done, lwb, ZIO_PRIORITY_LOG_WRITE,
803 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE, &zb);
804 	}
805 }
806 
807 /*
808  * Define a limited set of intent log block sizes.
809  * These must be a multiple of 4KB. Note only the amount used (again
810  * aligned to 4KB) actually gets written. However, we can't always just
811  * allocate SPA_MAXBLOCKSIZE as the slog space could be exhausted.
812  */
813 uint64_t zil_block_buckets[] = {
814     4096,		/* non TX_WRITE */
815     8192+4096,		/* data base */
816     32*1024 + 4096, 	/* NFS writes */
817     UINT64_MAX
818 };
819 
820 /*
821  * Use the slog as long as the logbias is 'latency' and the current commit size
822  * is less than the limit or the total list size is less than 2X the limit.
823  * Limit checking is disabled by setting zil_slog_limit to UINT64_MAX.
824  */
825 uint64_t zil_slog_limit = 1024 * 1024;
826 #define	USE_SLOG(zilog) (((zilog)->zl_logbias == ZFS_LOGBIAS_LATENCY) && \
827 	(((zilog)->zl_cur_used < zil_slog_limit) || \
828 	((zilog)->zl_itx_list_sz < (zil_slog_limit << 1))))
829 
830 /*
831  * Start a log block write and advance to the next log block.
832  * Calls are serialized.
833  */
834 static lwb_t *
835 zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb)
836 {
837 	lwb_t *nlwb = NULL;
838 	zil_chain_t *zilc;
839 	spa_t *spa = zilog->zl_spa;
840 	blkptr_t *bp;
841 	dmu_tx_t *tx;
842 	uint64_t txg;
843 	uint64_t zil_blksz, wsz;
844 	int i, error;
845 
846 	if (BP_GET_CHECKSUM(&lwb->lwb_blk) == ZIO_CHECKSUM_ZILOG2) {
847 		zilc = (zil_chain_t *)lwb->lwb_buf;
848 		bp = &zilc->zc_next_blk;
849 	} else {
850 		zilc = (zil_chain_t *)(lwb->lwb_buf + lwb->lwb_sz);
851 		bp = &zilc->zc_next_blk;
852 	}
853 
854 	ASSERT(lwb->lwb_nused <= lwb->lwb_sz);
855 
856 	/*
857 	 * Allocate the next block and save its address in this block
858 	 * before writing it in order to establish the log chain.
859 	 * Note that if the allocation of nlwb synced before we wrote
860 	 * the block that points at it (lwb), we'd leak it if we crashed.
861 	 * Therefore, we don't do dmu_tx_commit() until zil_lwb_write_done().
862 	 * We dirty the dataset to ensure that zil_sync() will be called
863 	 * to clean up in the event of allocation failure or I/O failure.
864 	 */
865 	tx = dmu_tx_create(zilog->zl_os);
866 	VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0);
867 	dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
868 	txg = dmu_tx_get_txg(tx);
869 
870 	lwb->lwb_tx = tx;
871 
872 	/*
873 	 * Log blocks are pre-allocated. Here we select the size of the next
874 	 * block, based on size used in the last block.
875 	 * - first find the smallest bucket that will fit the block from a
876 	 *   limited set of block sizes. This is because it's faster to write
877 	 *   blocks allocated from the same metaslab as they are adjacent or
878 	 *   close.
879 	 * - next find the maximum from the new suggested size and an array of
880 	 *   previous sizes. This lessens a picket fence effect of wrongly
881 	 *   guesssing the size if we have a stream of say 2k, 64k, 2k, 64k
882 	 *   requests.
883 	 *
884 	 * Note we only write what is used, but we can't just allocate
885 	 * the maximum block size because we can exhaust the available
886 	 * pool log space.
887 	 */
888 	zil_blksz = zilog->zl_cur_used + sizeof (zil_chain_t);
889 	for (i = 0; zil_blksz > zil_block_buckets[i]; i++)
890 		continue;
891 	zil_blksz = zil_block_buckets[i];
892 	if (zil_blksz == UINT64_MAX)
893 		zil_blksz = SPA_MAXBLOCKSIZE;
894 	zilog->zl_prev_blks[zilog->zl_prev_rotor] = zil_blksz;
895 	for (i = 0; i < ZIL_PREV_BLKS; i++)
896 		zil_blksz = MAX(zil_blksz, zilog->zl_prev_blks[i]);
897 	zilog->zl_prev_rotor = (zilog->zl_prev_rotor + 1) & (ZIL_PREV_BLKS - 1);
898 
899 	BP_ZERO(bp);
900 	/* pass the old blkptr in order to spread log blocks across devs */
901 	error = zio_alloc_zil(spa, txg, bp, &lwb->lwb_blk, zil_blksz,
902 	    USE_SLOG(zilog));
903 	if (!error) {
904 		ASSERT3U(bp->blk_birth, ==, txg);
905 		bp->blk_cksum = lwb->lwb_blk.blk_cksum;
906 		bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++;
907 
908 		/*
909 		 * Allocate a new log write buffer (lwb).
910 		 */
911 		nlwb = zil_alloc_lwb(zilog, bp, txg);
912 
913 		/* Record the block for later vdev flushing */
914 		zil_add_block(zilog, &lwb->lwb_blk);
915 	}
916 
917 	if (BP_GET_CHECKSUM(&lwb->lwb_blk) == ZIO_CHECKSUM_ZILOG2) {
918 		/* For Slim ZIL only write what is used. */
919 		wsz = P2ROUNDUP_TYPED(lwb->lwb_nused, ZIL_MIN_BLKSZ, uint64_t);
920 		ASSERT3U(wsz, <=, lwb->lwb_sz);
921 		zio_shrink(lwb->lwb_zio, wsz);
922 
923 	} else {
924 		wsz = lwb->lwb_sz;
925 	}
926 
927 	zilc->zc_pad = 0;
928 	zilc->zc_nused = lwb->lwb_nused;
929 	zilc->zc_eck.zec_cksum = lwb->lwb_blk.blk_cksum;
930 
931 	/*
932 	 * clear unused data for security
933 	 */
934 	bzero(lwb->lwb_buf + lwb->lwb_nused, wsz - lwb->lwb_nused);
935 
936 	zio_nowait(lwb->lwb_zio); /* Kick off the write for the old log block */
937 
938 	/*
939 	 * If there was an allocation failure then nlwb will be null which
940 	 * forces a txg_wait_synced().
941 	 */
942 	return (nlwb);
943 }
944 
945 static lwb_t *
946 zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb)
947 {
948 	lr_t *lrc = &itx->itx_lr; /* common log record */
949 	lr_write_t *lrw = (lr_write_t *)lrc;
950 	char *lr_buf;
951 	uint64_t txg = lrc->lrc_txg;
952 	uint64_t reclen = lrc->lrc_reclen;
953 	uint64_t dlen = 0;
954 
955 	if (lwb == NULL)
956 		return (NULL);
957 
958 	ASSERT(lwb->lwb_buf != NULL);
959 
960 	if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY)
961 		dlen = P2ROUNDUP_TYPED(
962 		    lrw->lr_length, sizeof (uint64_t), uint64_t);
963 
964 	zilog->zl_cur_used += (reclen + dlen);
965 
966 	zil_lwb_write_init(zilog, lwb);
967 
968 	/*
969 	 * If this record won't fit in the current log block, start a new one.
970 	 */
971 	if (lwb->lwb_nused + reclen + dlen > lwb->lwb_sz) {
972 		lwb = zil_lwb_write_start(zilog, lwb);
973 		if (lwb == NULL)
974 			return (NULL);
975 		zil_lwb_write_init(zilog, lwb);
976 		ASSERT(LWB_EMPTY(lwb));
977 		if (lwb->lwb_nused + reclen + dlen > lwb->lwb_sz) {
978 			txg_wait_synced(zilog->zl_dmu_pool, txg);
979 			return (lwb);
980 		}
981 	}
982 
983 	lr_buf = lwb->lwb_buf + lwb->lwb_nused;
984 	bcopy(lrc, lr_buf, reclen);
985 	lrc = (lr_t *)lr_buf;
986 	lrw = (lr_write_t *)lrc;
987 
988 	/*
989 	 * If it's a write, fetch the data or get its blkptr as appropriate.
990 	 */
991 	if (lrc->lrc_txtype == TX_WRITE) {
992 		if (txg > spa_freeze_txg(zilog->zl_spa))
993 			txg_wait_synced(zilog->zl_dmu_pool, txg);
994 		if (itx->itx_wr_state != WR_COPIED) {
995 			char *dbuf;
996 			int error;
997 
998 			if (dlen) {
999 				ASSERT(itx->itx_wr_state == WR_NEED_COPY);
1000 				dbuf = lr_buf + reclen;
1001 				lrw->lr_common.lrc_reclen += dlen;
1002 			} else {
1003 				ASSERT(itx->itx_wr_state == WR_INDIRECT);
1004 				dbuf = NULL;
1005 			}
1006 			error = zilog->zl_get_data(
1007 			    itx->itx_private, lrw, dbuf, lwb->lwb_zio);
1008 			if (error == EIO) {
1009 				txg_wait_synced(zilog->zl_dmu_pool, txg);
1010 				return (lwb);
1011 			}
1012 			if (error) {
1013 				ASSERT(error == ENOENT || error == EEXIST ||
1014 				    error == EALREADY);
1015 				return (lwb);
1016 			}
1017 		}
1018 	}
1019 
1020 	/*
1021 	 * We're actually making an entry, so update lrc_seq to be the
1022 	 * log record sequence number.  Note that this is generally not
1023 	 * equal to the itx sequence number because not all transactions
1024 	 * are synchronous, and sometimes spa_sync() gets there first.
1025 	 */
1026 	lrc->lrc_seq = ++zilog->zl_lr_seq; /* we are single threaded */
1027 	lwb->lwb_nused += reclen + dlen;
1028 	lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg);
1029 	ASSERT3U(lwb->lwb_nused, <=, lwb->lwb_sz);
1030 	ASSERT3U(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)), ==, 0);
1031 
1032 	return (lwb);
1033 }
1034 
1035 itx_t *
1036 zil_itx_create(uint64_t txtype, size_t lrsize)
1037 {
1038 	itx_t *itx;
1039 
1040 	lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t);
1041 
1042 	itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP);
1043 	itx->itx_lr.lrc_txtype = txtype;
1044 	itx->itx_lr.lrc_reclen = lrsize;
1045 	itx->itx_sod = lrsize; /* if write & WR_NEED_COPY will be increased */
1046 	itx->itx_lr.lrc_seq = 0;	/* defensive */
1047 
1048 	return (itx);
1049 }
1050 
1051 void
1052 zil_itx_destroy(itx_t *itx)
1053 {
1054 	kmem_free(itx, offsetof(itx_t, itx_lr) + itx->itx_lr.lrc_reclen);
1055 }
1056 
1057 uint64_t
1058 zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx)
1059 {
1060 	uint64_t seq;
1061 
1062 	ASSERT(itx->itx_lr.lrc_seq == 0);
1063 	ASSERT(!zilog->zl_replay);
1064 
1065 	mutex_enter(&zilog->zl_lock);
1066 	list_insert_tail(&zilog->zl_itx_list, itx);
1067 	zilog->zl_itx_list_sz += itx->itx_sod;
1068 	itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx);
1069 	itx->itx_lr.lrc_seq = seq = ++zilog->zl_itx_seq;
1070 	mutex_exit(&zilog->zl_lock);
1071 
1072 	return (seq);
1073 }
1074 
1075 /*
1076  * Free up all in-memory intent log transactions that have now been synced.
1077  */
1078 static void
1079 zil_itx_clean(zilog_t *zilog)
1080 {
1081 	uint64_t synced_txg = spa_last_synced_txg(zilog->zl_spa);
1082 	uint64_t freeze_txg = spa_freeze_txg(zilog->zl_spa);
1083 	list_t clean_list;
1084 	itx_t *itx;
1085 
1086 	list_create(&clean_list, sizeof (itx_t), offsetof(itx_t, itx_node));
1087 
1088 	mutex_enter(&zilog->zl_lock);
1089 	/* wait for a log writer to finish walking list */
1090 	while (zilog->zl_writer) {
1091 		cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
1092 	}
1093 
1094 	/*
1095 	 * Move the sync'd log transactions to a separate list so we can call
1096 	 * kmem_free without holding the zl_lock.
1097 	 *
1098 	 * There is no need to set zl_writer as we don't drop zl_lock here
1099 	 */
1100 	while ((itx = list_head(&zilog->zl_itx_list)) != NULL &&
1101 	    itx->itx_lr.lrc_txg <= MIN(synced_txg, freeze_txg)) {
1102 		list_remove(&zilog->zl_itx_list, itx);
1103 		zilog->zl_itx_list_sz -= itx->itx_sod;
1104 		list_insert_tail(&clean_list, itx);
1105 	}
1106 	cv_broadcast(&zilog->zl_cv_writer);
1107 	mutex_exit(&zilog->zl_lock);
1108 
1109 	/* destroy sync'd log transactions */
1110 	while ((itx = list_head(&clean_list)) != NULL) {
1111 		list_remove(&clean_list, itx);
1112 		zil_itx_destroy(itx);
1113 	}
1114 	list_destroy(&clean_list);
1115 }
1116 
1117 /*
1118  * If there are any in-memory intent log transactions which have now been
1119  * synced then start up a taskq to free them.
1120  */
1121 void
1122 zil_clean(zilog_t *zilog)
1123 {
1124 	itx_t *itx;
1125 
1126 	mutex_enter(&zilog->zl_lock);
1127 	itx = list_head(&zilog->zl_itx_list);
1128 	if ((itx != NULL) &&
1129 	    (itx->itx_lr.lrc_txg <= spa_last_synced_txg(zilog->zl_spa))) {
1130 		(void) taskq_dispatch(zilog->zl_clean_taskq,
1131 		    (task_func_t *)zil_itx_clean, zilog, TQ_NOSLEEP);
1132 	}
1133 	mutex_exit(&zilog->zl_lock);
1134 }
1135 
1136 static void
1137 zil_commit_writer(zilog_t *zilog, uint64_t seq, uint64_t foid)
1138 {
1139 	uint64_t txg;
1140 	uint64_t commit_seq = 0;
1141 	itx_t *itx, *itx_next;
1142 	lwb_t *lwb;
1143 	spa_t *spa;
1144 	int error = 0;
1145 
1146 	zilog->zl_writer = B_TRUE;
1147 	ASSERT(zilog->zl_root_zio == NULL);
1148 	spa = zilog->zl_spa;
1149 
1150 	if (zilog->zl_suspend) {
1151 		lwb = NULL;
1152 	} else {
1153 		lwb = list_tail(&zilog->zl_lwb_list);
1154 		if (lwb == NULL) {
1155 			/*
1156 			 * Return if there's nothing to flush before we
1157 			 * dirty the fs by calling zil_create()
1158 			 */
1159 			if (list_is_empty(&zilog->zl_itx_list)) {
1160 				zilog->zl_writer = B_FALSE;
1161 				return;
1162 			}
1163 			mutex_exit(&zilog->zl_lock);
1164 			lwb = zil_create(zilog);
1165 			mutex_enter(&zilog->zl_lock);
1166 		}
1167 	}
1168 	ASSERT(lwb == NULL || lwb->lwb_zio == NULL);
1169 
1170 	/* Loop through in-memory log transactions filling log blocks. */
1171 	DTRACE_PROBE1(zil__cw1, zilog_t *, zilog);
1172 
1173 	for (itx = list_head(&zilog->zl_itx_list); itx; itx = itx_next) {
1174 		/*
1175 		 * Save the next pointer.  Even though we drop zl_lock below,
1176 		 * all threads that can remove itx list entries (other writers
1177 		 * and zil_itx_clean()) can't do so until they have zl_writer.
1178 		 */
1179 		itx_next = list_next(&zilog->zl_itx_list, itx);
1180 
1181 		/*
1182 		 * Determine whether to push this itx.
1183 		 * Push all transactions related to specified foid and
1184 		 * all other transactions except those that can be logged
1185 		 * out of order (TX_WRITE, TX_TRUNCATE, TX_SETATTR, TX_ACL)
1186 		 * for all other files.
1187 		 *
1188 		 * If foid == 0 (meaning "push all foids") or
1189 		 * itx->itx_sync is set (meaning O_[D]SYNC), push regardless.
1190 		 */
1191 		if (foid != 0 && !itx->itx_sync &&
1192 		    TX_OOO(itx->itx_lr.lrc_txtype) &&
1193 		    ((lr_ooo_t *)&itx->itx_lr)->lr_foid != foid)
1194 			continue; /* skip this record */
1195 
1196 		if ((itx->itx_lr.lrc_seq > seq) &&
1197 		    ((lwb == NULL) || (LWB_EMPTY(lwb)) ||
1198 		    (lwb->lwb_nused + itx->itx_sod > lwb->lwb_sz)))
1199 			break;
1200 
1201 		list_remove(&zilog->zl_itx_list, itx);
1202 		zilog->zl_itx_list_sz -= itx->itx_sod;
1203 
1204 		mutex_exit(&zilog->zl_lock);
1205 
1206 		txg = itx->itx_lr.lrc_txg;
1207 		ASSERT(txg);
1208 
1209 		if (txg > spa_last_synced_txg(spa) ||
1210 		    txg > spa_freeze_txg(spa))
1211 			lwb = zil_lwb_commit(zilog, itx, lwb);
1212 
1213 		zil_itx_destroy(itx);
1214 
1215 		mutex_enter(&zilog->zl_lock);
1216 	}
1217 	DTRACE_PROBE1(zil__cw2, zilog_t *, zilog);
1218 	/* determine commit sequence number */
1219 	itx = list_head(&zilog->zl_itx_list);
1220 	if (itx)
1221 		commit_seq = itx->itx_lr.lrc_seq - 1;
1222 	else
1223 		commit_seq = zilog->zl_itx_seq;
1224 	mutex_exit(&zilog->zl_lock);
1225 
1226 	/* write the last block out */
1227 	if (lwb != NULL && lwb->lwb_zio != NULL)
1228 		lwb = zil_lwb_write_start(zilog, lwb);
1229 
1230 	zilog->zl_prev_used = zilog->zl_cur_used;
1231 	zilog->zl_cur_used = 0;
1232 
1233 	/*
1234 	 * Wait if necessary for the log blocks to be on stable storage.
1235 	 */
1236 	if (zilog->zl_root_zio) {
1237 		DTRACE_PROBE1(zil__cw3, zilog_t *, zilog);
1238 		error = zio_wait(zilog->zl_root_zio);
1239 		zilog->zl_root_zio = NULL;
1240 		DTRACE_PROBE1(zil__cw4, zilog_t *, zilog);
1241 		zil_flush_vdevs(zilog);
1242 	}
1243 
1244 	if (error || lwb == NULL)
1245 		txg_wait_synced(zilog->zl_dmu_pool, 0);
1246 
1247 	mutex_enter(&zilog->zl_lock);
1248 	zilog->zl_writer = B_FALSE;
1249 
1250 	ASSERT3U(commit_seq, >=, zilog->zl_commit_seq);
1251 	zilog->zl_commit_seq = commit_seq;
1252 
1253 	/*
1254 	 * Remember the highest committed log sequence number for ztest.
1255 	 * We only update this value when all the log writes succeeded,
1256 	 * because ztest wants to ASSERT that it got the whole log chain.
1257 	 */
1258 	if (error == 0 && lwb != NULL)
1259 		zilog->zl_commit_lr_seq = zilog->zl_lr_seq;
1260 }
1261 
1262 /*
1263  * Push zfs transactions to stable storage up to the supplied sequence number.
1264  * If foid is 0 push out all transactions, otherwise push only those
1265  * for that file or might have been used to create that file.
1266  */
1267 void
1268 zil_commit(zilog_t *zilog, uint64_t seq, uint64_t foid)
1269 {
1270 	if (zilog->zl_sync == ZFS_SYNC_DISABLED || seq == 0)
1271 		return;
1272 
1273 	mutex_enter(&zilog->zl_lock);
1274 
1275 	seq = MIN(seq, zilog->zl_itx_seq);	/* cap seq at largest itx seq */
1276 
1277 	while (zilog->zl_writer) {
1278 		cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
1279 		if (seq <= zilog->zl_commit_seq) {
1280 			mutex_exit(&zilog->zl_lock);
1281 			return;
1282 		}
1283 	}
1284 	zil_commit_writer(zilog, seq, foid); /* drops zl_lock */
1285 	/* wake up others waiting on the commit */
1286 	cv_broadcast(&zilog->zl_cv_writer);
1287 	mutex_exit(&zilog->zl_lock);
1288 }
1289 
1290 /*
1291  * Report whether all transactions are committed.
1292  */
1293 static boolean_t
1294 zil_is_committed(zilog_t *zilog)
1295 {
1296 	lwb_t *lwb;
1297 	boolean_t committed;
1298 
1299 	mutex_enter(&zilog->zl_lock);
1300 
1301 	while (zilog->zl_writer)
1302 		cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
1303 
1304 	if (!list_is_empty(&zilog->zl_itx_list))
1305 		committed = B_FALSE;		/* unpushed transactions */
1306 	else if ((lwb = list_head(&zilog->zl_lwb_list)) == NULL)
1307 		committed = B_TRUE;		/* intent log never used */
1308 	else if (list_next(&zilog->zl_lwb_list, lwb) != NULL)
1309 		committed = B_FALSE;		/* zil_sync() not done yet */
1310 	else
1311 		committed = B_TRUE;		/* everything synced */
1312 
1313 	mutex_exit(&zilog->zl_lock);
1314 	return (committed);
1315 }
1316 
1317 /*
1318  * Called in syncing context to free committed log blocks and update log header.
1319  */
1320 void
1321 zil_sync(zilog_t *zilog, dmu_tx_t *tx)
1322 {
1323 	zil_header_t *zh = zil_header_in_syncing_context(zilog);
1324 	uint64_t txg = dmu_tx_get_txg(tx);
1325 	spa_t *spa = zilog->zl_spa;
1326 	uint64_t *replayed_seq = &zilog->zl_replayed_seq[txg & TXG_MASK];
1327 	lwb_t *lwb;
1328 
1329 	/*
1330 	 * We don't zero out zl_destroy_txg, so make sure we don't try
1331 	 * to destroy it twice.
1332 	 */
1333 	if (spa_sync_pass(spa) != 1)
1334 		return;
1335 
1336 	mutex_enter(&zilog->zl_lock);
1337 
1338 	ASSERT(zilog->zl_stop_sync == 0);
1339 
1340 	if (*replayed_seq != 0) {
1341 		ASSERT(zh->zh_replay_seq < *replayed_seq);
1342 		zh->zh_replay_seq = *replayed_seq;
1343 		*replayed_seq = 0;
1344 	}
1345 
1346 	if (zilog->zl_destroy_txg == txg) {
1347 		blkptr_t blk = zh->zh_log;
1348 
1349 		ASSERT(list_head(&zilog->zl_lwb_list) == NULL);
1350 
1351 		bzero(zh, sizeof (zil_header_t));
1352 		bzero(zilog->zl_replayed_seq, sizeof (zilog->zl_replayed_seq));
1353 
1354 		if (zilog->zl_keep_first) {
1355 			/*
1356 			 * If this block was part of log chain that couldn't
1357 			 * be claimed because a device was missing during
1358 			 * zil_claim(), but that device later returns,
1359 			 * then this block could erroneously appear valid.
1360 			 * To guard against this, assign a new GUID to the new
1361 			 * log chain so it doesn't matter what blk points to.
1362 			 */
1363 			zil_init_log_chain(zilog, &blk);
1364 			zh->zh_log = blk;
1365 		}
1366 	}
1367 
1368 	while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
1369 		zh->zh_log = lwb->lwb_blk;
1370 		if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg)
1371 			break;
1372 		list_remove(&zilog->zl_lwb_list, lwb);
1373 		zio_free_zil(spa, txg, &lwb->lwb_blk);
1374 		kmem_cache_free(zil_lwb_cache, lwb);
1375 
1376 		/*
1377 		 * If we don't have anything left in the lwb list then
1378 		 * we've had an allocation failure and we need to zero
1379 		 * out the zil_header blkptr so that we don't end
1380 		 * up freeing the same block twice.
1381 		 */
1382 		if (list_head(&zilog->zl_lwb_list) == NULL)
1383 			BP_ZERO(&zh->zh_log);
1384 	}
1385 	mutex_exit(&zilog->zl_lock);
1386 }
1387 
1388 void
1389 zil_init(void)
1390 {
1391 	zil_lwb_cache = kmem_cache_create("zil_lwb_cache",
1392 	    sizeof (struct lwb), 0, NULL, NULL, NULL, NULL, NULL, 0);
1393 }
1394 
1395 void
1396 zil_fini(void)
1397 {
1398 	kmem_cache_destroy(zil_lwb_cache);
1399 }
1400 
1401 void
1402 zil_set_sync(zilog_t *zilog, uint64_t sync)
1403 {
1404 	zilog->zl_sync = sync;
1405 }
1406 
1407 void
1408 zil_set_logbias(zilog_t *zilog, uint64_t logbias)
1409 {
1410 	zilog->zl_logbias = logbias;
1411 }
1412 
1413 zilog_t *
1414 zil_alloc(objset_t *os, zil_header_t *zh_phys)
1415 {
1416 	zilog_t *zilog;
1417 
1418 	zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP);
1419 
1420 	zilog->zl_header = zh_phys;
1421 	zilog->zl_os = os;
1422 	zilog->zl_spa = dmu_objset_spa(os);
1423 	zilog->zl_dmu_pool = dmu_objset_pool(os);
1424 	zilog->zl_destroy_txg = TXG_INITIAL - 1;
1425 	zilog->zl_logbias = dmu_objset_logbias(os);
1426 	zilog->zl_sync = dmu_objset_syncprop(os);
1427 
1428 	mutex_init(&zilog->zl_lock, NULL, MUTEX_DEFAULT, NULL);
1429 
1430 	list_create(&zilog->zl_itx_list, sizeof (itx_t),
1431 	    offsetof(itx_t, itx_node));
1432 
1433 	list_create(&zilog->zl_lwb_list, sizeof (lwb_t),
1434 	    offsetof(lwb_t, lwb_node));
1435 
1436 	mutex_init(&zilog->zl_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
1437 
1438 	avl_create(&zilog->zl_vdev_tree, zil_vdev_compare,
1439 	    sizeof (zil_vdev_node_t), offsetof(zil_vdev_node_t, zv_node));
1440 
1441 	cv_init(&zilog->zl_cv_writer, NULL, CV_DEFAULT, NULL);
1442 	cv_init(&zilog->zl_cv_suspend, NULL, CV_DEFAULT, NULL);
1443 
1444 	return (zilog);
1445 }
1446 
1447 void
1448 zil_free(zilog_t *zilog)
1449 {
1450 	lwb_t *lwb;
1451 
1452 	zilog->zl_stop_sync = 1;
1453 
1454 	while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
1455 		list_remove(&zilog->zl_lwb_list, lwb);
1456 		if (lwb->lwb_buf != NULL)
1457 			zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
1458 		kmem_cache_free(zil_lwb_cache, lwb);
1459 	}
1460 	list_destroy(&zilog->zl_lwb_list);
1461 
1462 	avl_destroy(&zilog->zl_vdev_tree);
1463 	mutex_destroy(&zilog->zl_vdev_lock);
1464 
1465 	ASSERT(list_head(&zilog->zl_itx_list) == NULL);
1466 	list_destroy(&zilog->zl_itx_list);
1467 	mutex_destroy(&zilog->zl_lock);
1468 
1469 	cv_destroy(&zilog->zl_cv_writer);
1470 	cv_destroy(&zilog->zl_cv_suspend);
1471 
1472 	kmem_free(zilog, sizeof (zilog_t));
1473 }
1474 
1475 /*
1476  * Open an intent log.
1477  */
1478 zilog_t *
1479 zil_open(objset_t *os, zil_get_data_t *get_data)
1480 {
1481 	zilog_t *zilog = dmu_objset_zil(os);
1482 
1483 	zilog->zl_get_data = get_data;
1484 	zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri,
1485 	    2, 2, TASKQ_PREPOPULATE);
1486 
1487 	return (zilog);
1488 }
1489 
1490 /*
1491  * Close an intent log.
1492  */
1493 void
1494 zil_close(zilog_t *zilog)
1495 {
1496 	/*
1497 	 * If the log isn't already committed, mark the objset dirty
1498 	 * (so zil_sync() will be called) and wait for that txg to sync.
1499 	 */
1500 	if (!zil_is_committed(zilog)) {
1501 		uint64_t txg;
1502 		dmu_tx_t *tx = dmu_tx_create(zilog->zl_os);
1503 		VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0);
1504 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
1505 		txg = dmu_tx_get_txg(tx);
1506 		dmu_tx_commit(tx);
1507 		txg_wait_synced(zilog->zl_dmu_pool, txg);
1508 	}
1509 
1510 	taskq_destroy(zilog->zl_clean_taskq);
1511 	zilog->zl_clean_taskq = NULL;
1512 	zilog->zl_get_data = NULL;
1513 
1514 	zil_itx_clean(zilog);
1515 	ASSERT(list_head(&zilog->zl_itx_list) == NULL);
1516 }
1517 
1518 /*
1519  * Suspend an intent log.  While in suspended mode, we still honor
1520  * synchronous semantics, but we rely on txg_wait_synced() to do it.
1521  * We suspend the log briefly when taking a snapshot so that the snapshot
1522  * contains all the data it's supposed to, and has an empty intent log.
1523  */
1524 int
1525 zil_suspend(zilog_t *zilog)
1526 {
1527 	const zil_header_t *zh = zilog->zl_header;
1528 
1529 	mutex_enter(&zilog->zl_lock);
1530 	if (zh->zh_flags & ZIL_REPLAY_NEEDED) {		/* unplayed log */
1531 		mutex_exit(&zilog->zl_lock);
1532 		return (EBUSY);
1533 	}
1534 	if (zilog->zl_suspend++ != 0) {
1535 		/*
1536 		 * Someone else already began a suspend.
1537 		 * Just wait for them to finish.
1538 		 */
1539 		while (zilog->zl_suspending)
1540 			cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock);
1541 		mutex_exit(&zilog->zl_lock);
1542 		return (0);
1543 	}
1544 	zilog->zl_suspending = B_TRUE;
1545 	mutex_exit(&zilog->zl_lock);
1546 
1547 	zil_commit(zilog, UINT64_MAX, 0);
1548 
1549 	/*
1550 	 * Wait for any in-flight log writes to complete.
1551 	 */
1552 	mutex_enter(&zilog->zl_lock);
1553 	while (zilog->zl_writer)
1554 		cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
1555 	mutex_exit(&zilog->zl_lock);
1556 
1557 	zil_destroy(zilog, B_FALSE);
1558 
1559 	mutex_enter(&zilog->zl_lock);
1560 	zilog->zl_suspending = B_FALSE;
1561 	cv_broadcast(&zilog->zl_cv_suspend);
1562 	mutex_exit(&zilog->zl_lock);
1563 
1564 	return (0);
1565 }
1566 
1567 void
1568 zil_resume(zilog_t *zilog)
1569 {
1570 	mutex_enter(&zilog->zl_lock);
1571 	ASSERT(zilog->zl_suspend != 0);
1572 	zilog->zl_suspend--;
1573 	mutex_exit(&zilog->zl_lock);
1574 }
1575 
1576 typedef struct zil_replay_arg {
1577 	zil_replay_func_t **zr_replay;
1578 	void		*zr_arg;
1579 	boolean_t	zr_byteswap;
1580 	char		*zr_lr;
1581 } zil_replay_arg_t;
1582 
1583 static int
1584 zil_replay_error(zilog_t *zilog, lr_t *lr, int error)
1585 {
1586 	char name[MAXNAMELEN];
1587 
1588 	zilog->zl_replaying_seq--;	/* didn't actually replay this one */
1589 
1590 	dmu_objset_name(zilog->zl_os, name);
1591 
1592 	cmn_err(CE_WARN, "ZFS replay transaction error %d, "
1593 	    "dataset %s, seq 0x%llx, txtype %llu %s\n", error, name,
1594 	    (u_longlong_t)lr->lrc_seq,
1595 	    (u_longlong_t)(lr->lrc_txtype & ~TX_CI),
1596 	    (lr->lrc_txtype & TX_CI) ? "CI" : "");
1597 
1598 	return (error);
1599 }
1600 
1601 static int
1602 zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg)
1603 {
1604 	zil_replay_arg_t *zr = zra;
1605 	const zil_header_t *zh = zilog->zl_header;
1606 	uint64_t reclen = lr->lrc_reclen;
1607 	uint64_t txtype = lr->lrc_txtype;
1608 	int error = 0;
1609 
1610 	zilog->zl_replaying_seq = lr->lrc_seq;
1611 
1612 	if (lr->lrc_seq <= zh->zh_replay_seq)	/* already replayed */
1613 		return (0);
1614 
1615 	if (lr->lrc_txg < claim_txg)		/* already committed */
1616 		return (0);
1617 
1618 	/* Strip case-insensitive bit, still present in log record */
1619 	txtype &= ~TX_CI;
1620 
1621 	if (txtype == 0 || txtype >= TX_MAX_TYPE)
1622 		return (zil_replay_error(zilog, lr, EINVAL));
1623 
1624 	/*
1625 	 * If this record type can be logged out of order, the object
1626 	 * (lr_foid) may no longer exist.  That's legitimate, not an error.
1627 	 */
1628 	if (TX_OOO(txtype)) {
1629 		error = dmu_object_info(zilog->zl_os,
1630 		    ((lr_ooo_t *)lr)->lr_foid, NULL);
1631 		if (error == ENOENT || error == EEXIST)
1632 			return (0);
1633 	}
1634 
1635 	/*
1636 	 * Make a copy of the data so we can revise and extend it.
1637 	 */
1638 	bcopy(lr, zr->zr_lr, reclen);
1639 
1640 	/*
1641 	 * If this is a TX_WRITE with a blkptr, suck in the data.
1642 	 */
1643 	if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) {
1644 		error = zil_read_log_data(zilog, (lr_write_t *)lr,
1645 		    zr->zr_lr + reclen);
1646 		if (error)
1647 			return (zil_replay_error(zilog, lr, error));
1648 	}
1649 
1650 	/*
1651 	 * The log block containing this lr may have been byteswapped
1652 	 * so that we can easily examine common fields like lrc_txtype.
1653 	 * However, the log is a mix of different record types, and only the
1654 	 * replay vectors know how to byteswap their records.  Therefore, if
1655 	 * the lr was byteswapped, undo it before invoking the replay vector.
1656 	 */
1657 	if (zr->zr_byteswap)
1658 		byteswap_uint64_array(zr->zr_lr, reclen);
1659 
1660 	/*
1661 	 * We must now do two things atomically: replay this log record,
1662 	 * and update the log header sequence number to reflect the fact that
1663 	 * we did so. At the end of each replay function the sequence number
1664 	 * is updated if we are in replay mode.
1665 	 */
1666 	error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lr, zr->zr_byteswap);
1667 	if (error) {
1668 		/*
1669 		 * The DMU's dnode layer doesn't see removes until the txg
1670 		 * commits, so a subsequent claim can spuriously fail with
1671 		 * EEXIST. So if we receive any error we try syncing out
1672 		 * any removes then retry the transaction.  Note that we
1673 		 * specify B_FALSE for byteswap now, so we don't do it twice.
1674 		 */
1675 		txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0);
1676 		error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lr, B_FALSE);
1677 		if (error)
1678 			return (zil_replay_error(zilog, lr, error));
1679 	}
1680 	return (0);
1681 }
1682 
1683 /* ARGSUSED */
1684 static int
1685 zil_incr_blks(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
1686 {
1687 	zilog->zl_replay_blks++;
1688 
1689 	return (0);
1690 }
1691 
1692 /*
1693  * If this dataset has a non-empty intent log, replay it and destroy it.
1694  */
1695 void
1696 zil_replay(objset_t *os, void *arg, zil_replay_func_t *replay_func[TX_MAX_TYPE])
1697 {
1698 	zilog_t *zilog = dmu_objset_zil(os);
1699 	const zil_header_t *zh = zilog->zl_header;
1700 	zil_replay_arg_t zr;
1701 
1702 	if ((zh->zh_flags & ZIL_REPLAY_NEEDED) == 0) {
1703 		zil_destroy(zilog, B_TRUE);
1704 		return;
1705 	}
1706 
1707 	zr.zr_replay = replay_func;
1708 	zr.zr_arg = arg;
1709 	zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log);
1710 	zr.zr_lr = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP);
1711 
1712 	/*
1713 	 * Wait for in-progress removes to sync before starting replay.
1714 	 */
1715 	txg_wait_synced(zilog->zl_dmu_pool, 0);
1716 
1717 	zilog->zl_replay = B_TRUE;
1718 	zilog->zl_replay_time = ddi_get_lbolt();
1719 	ASSERT(zilog->zl_replay_blks == 0);
1720 	(void) zil_parse(zilog, zil_incr_blks, zil_replay_log_record, &zr,
1721 	    zh->zh_claim_txg);
1722 	kmem_free(zr.zr_lr, 2 * SPA_MAXBLOCKSIZE);
1723 
1724 	zil_destroy(zilog, B_FALSE);
1725 	txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
1726 	zilog->zl_replay = B_FALSE;
1727 }
1728 
1729 boolean_t
1730 zil_replaying(zilog_t *zilog, dmu_tx_t *tx)
1731 {
1732 	if (zilog->zl_sync == ZFS_SYNC_DISABLED)
1733 		return (B_TRUE);
1734 
1735 	if (zilog->zl_replay) {
1736 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
1737 		zilog->zl_replayed_seq[dmu_tx_get_txg(tx) & TXG_MASK] =
1738 		    zilog->zl_replaying_seq;
1739 		return (B_TRUE);
1740 	}
1741 
1742 	return (B_FALSE);
1743 }
1744 
1745 /* ARGSUSED */
1746 int
1747 zil_vdev_offline(const char *osname, void *arg)
1748 {
1749 	objset_t *os;
1750 	zilog_t *zilog;
1751 	int error;
1752 
1753 	error = dmu_objset_hold(osname, FTAG, &os);
1754 	if (error)
1755 		return (error);
1756 
1757 	zilog = dmu_objset_zil(os);
1758 	if (zil_suspend(zilog) != 0)
1759 		error = EEXIST;
1760 	else
1761 		zil_resume(zilog);
1762 	dmu_objset_rele(os, FTAG);
1763 	return (error);
1764 }
1765