xref: /illumos-gate/usr/src/uts/common/fs/zfs/zil.c (revision 104e2ed78d9ef0a0f89f320108b8ca29ca3850d5)
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/zfs_context.h>
29 #include <sys/spa.h>
30 #include <sys/dmu.h>
31 #include <sys/zap.h>
32 #include <sys/arc.h>
33 #include <sys/stat.h>
34 #include <sys/resource.h>
35 #include <sys/zil.h>
36 #include <sys/zil_impl.h>
37 #include <sys/dsl_dataset.h>
38 #include <sys/vdev.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  * These global ZIL switches affect all pools
68  */
69 int zil_disable = 0;	/* disable intent logging */
70 int zil_always = 0;	/* make every transaction synchronous */
71 int zil_purge = 0;	/* at pool open, just throw everything away */
72 int zil_noflush = 0;	/* don't flush write cache buffers on disks */
73 
74 static kmem_cache_t *zil_lwb_cache;
75 
76 static int
77 zil_dva_compare(const void *x1, const void *x2)
78 {
79 	const dva_t *dva1 = x1;
80 	const dva_t *dva2 = x2;
81 
82 	if (DVA_GET_VDEV(dva1) < DVA_GET_VDEV(dva2))
83 		return (-1);
84 	if (DVA_GET_VDEV(dva1) > DVA_GET_VDEV(dva2))
85 		return (1);
86 
87 	if (DVA_GET_OFFSET(dva1) < DVA_GET_OFFSET(dva2))
88 		return (-1);
89 	if (DVA_GET_OFFSET(dva1) > DVA_GET_OFFSET(dva2))
90 		return (1);
91 
92 	return (0);
93 }
94 
95 static void
96 zil_dva_tree_init(avl_tree_t *t)
97 {
98 	avl_create(t, zil_dva_compare, sizeof (zil_dva_node_t),
99 	    offsetof(zil_dva_node_t, zn_node));
100 }
101 
102 static void
103 zil_dva_tree_fini(avl_tree_t *t)
104 {
105 	zil_dva_node_t *zn;
106 	void *cookie = NULL;
107 
108 	while ((zn = avl_destroy_nodes(t, &cookie)) != NULL)
109 		kmem_free(zn, sizeof (zil_dva_node_t));
110 
111 	avl_destroy(t);
112 }
113 
114 static int
115 zil_dva_tree_add(avl_tree_t *t, dva_t *dva)
116 {
117 	zil_dva_node_t *zn;
118 	avl_index_t where;
119 
120 	if (avl_find(t, dva, &where) != NULL)
121 		return (EEXIST);
122 
123 	zn = kmem_alloc(sizeof (zil_dva_node_t), KM_SLEEP);
124 	zn->zn_dva = *dva;
125 	avl_insert(t, zn, where);
126 
127 	return (0);
128 }
129 
130 /*
131  * Read a log block, make sure it's valid, and byteswap it if necessary.
132  */
133 static int
134 zil_read_log_block(zilog_t *zilog, blkptr_t *bp, char *buf)
135 {
136 	uint64_t blksz = BP_GET_LSIZE(bp);
137 	zil_trailer_t *ztp = (zil_trailer_t *)(buf + blksz) - 1;
138 	zio_cksum_t cksum;
139 	zbookmark_t zb;
140 	int error;
141 
142 	zb.zb_objset = bp->blk_cksum.zc_word[2];
143 	zb.zb_object = 0;
144 	zb.zb_level = -1;
145 	zb.zb_blkid = bp->blk_cksum.zc_word[3];
146 
147 	error = zio_wait(zio_read(NULL, zilog->zl_spa, bp, buf, blksz,
148 	    NULL, NULL, ZIO_PRIORITY_SYNC_READ,
149 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &zb));
150 	if (error) {
151 		dprintf_bp(bp, "zilog %p bp %p read failed, error %d: ",
152 		    zilog, bp, error);
153 		return (error);
154 	}
155 
156 	if (BP_SHOULD_BYTESWAP(bp))
157 		byteswap_uint64_array(buf, blksz);
158 
159 	/*
160 	 * Sequence numbers should be... sequential.  The checksum verifier for
161 	 * the next block should be: <logid[0], logid[1], objset id, seq + 1>.
162 	 */
163 	cksum = bp->blk_cksum;
164 	cksum.zc_word[3]++;
165 	if (bcmp(&cksum, &ztp->zit_next_blk.blk_cksum, sizeof (cksum)) != 0) {
166 		dprintf_bp(bp, "zilog %p bp %p stale pointer: ", zilog, bp);
167 		return (ESTALE);
168 	}
169 
170 	if (BP_IS_HOLE(&ztp->zit_next_blk)) {
171 		dprintf_bp(bp, "zilog %p bp %p hole: ", zilog, bp);
172 		return (ENOENT);
173 	}
174 
175 	if (ztp->zit_nused > (blksz - sizeof (zil_trailer_t))) {
176 		dprintf("zilog %p bp %p nused exceeds blksz\n", zilog, bp);
177 		return (EOVERFLOW);
178 	}
179 
180 	dprintf_bp(bp, "zilog %p bp %p good block: ", zilog, bp);
181 
182 	return (0);
183 }
184 
185 /*
186  * Parse the intent log, and call parse_func for each valid record within.
187  */
188 void
189 zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func,
190     zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg)
191 {
192 	blkptr_t blk;
193 	char *lrbuf, *lrp;
194 	zil_trailer_t *ztp;
195 	int reclen, error;
196 
197 	blk = zilog->zl_header->zh_log;
198 	if (BP_IS_HOLE(&blk))
199 		return;
200 
201 	/*
202 	 * Starting at the block pointed to by zh_log we read the log chain.
203 	 * For each block in the chain we strongly check that block to
204 	 * ensure its validity.  We stop when an invalid block is found.
205 	 * For each block pointer in the chain we call parse_blk_func().
206 	 * For each record in each valid block we call parse_lr_func().
207 	 */
208 	zil_dva_tree_init(&zilog->zl_dva_tree);
209 	lrbuf = zio_buf_alloc(SPA_MAXBLOCKSIZE);
210 	for (;;) {
211 		error = zil_read_log_block(zilog, &blk, lrbuf);
212 
213 		if (parse_blk_func != NULL)
214 			parse_blk_func(zilog, &blk, arg, txg);
215 
216 		if (error)
217 			break;
218 
219 		ztp = (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1;
220 		blk = ztp->zit_next_blk;
221 
222 		if (parse_lr_func == NULL)
223 			continue;
224 
225 		for (lrp = lrbuf; lrp < lrbuf + ztp->zit_nused; lrp += reclen) {
226 			lr_t *lr = (lr_t *)lrp;
227 			reclen = lr->lrc_reclen;
228 			ASSERT3U(reclen, >=, sizeof (lr_t));
229 			parse_lr_func(zilog, lr, arg, txg);
230 		}
231 	}
232 	zio_buf_free(lrbuf, SPA_MAXBLOCKSIZE);
233 	zil_dva_tree_fini(&zilog->zl_dva_tree);
234 }
235 
236 /* ARGSUSED */
237 static void
238 zil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg)
239 {
240 	spa_t *spa = zilog->zl_spa;
241 	int err;
242 
243 	dprintf_bp(bp, "first_txg %llu: ", first_txg);
244 
245 	/*
246 	 * Claim log block if not already committed and not already claimed.
247 	 */
248 	if (bp->blk_birth >= first_txg &&
249 	    zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp)) == 0) {
250 		err = zio_wait(zio_claim(NULL, spa, first_txg, bp, NULL, NULL));
251 		ASSERT(err == 0);
252 	}
253 }
254 
255 static void
256 zil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg)
257 {
258 	if (lrc->lrc_txtype == TX_WRITE) {
259 		lr_write_t *lr = (lr_write_t *)lrc;
260 		zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg);
261 	}
262 }
263 
264 /* ARGSUSED */
265 static void
266 zil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg)
267 {
268 	zio_free_blk(zilog->zl_spa, bp, dmu_tx_get_txg(tx));
269 }
270 
271 static void
272 zil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg)
273 {
274 	/*
275 	 * If we previously claimed it, we need to free it.
276 	 */
277 	if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE) {
278 		lr_write_t *lr = (lr_write_t *)lrc;
279 		blkptr_t *bp = &lr->lr_blkptr;
280 		if (bp->blk_birth >= claim_txg &&
281 		    !zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp))) {
282 			(void) arc_free(NULL, zilog->zl_spa,
283 			    dmu_tx_get_txg(tx), bp, NULL, NULL, ARC_WAIT);
284 		}
285 	}
286 }
287 
288 /*
289  * Create an on-disk intent log.
290  */
291 static void
292 zil_create(zilog_t *zilog)
293 {
294 	lwb_t *lwb;
295 	uint64_t txg;
296 	dmu_tx_t *tx;
297 	blkptr_t blk;
298 	int error;
299 	int no_blk;
300 
301 	ASSERT(zilog->zl_header->zh_claim_txg == 0);
302 	ASSERT(zilog->zl_header->zh_replay_seq == 0);
303 
304 	/*
305 	 * Initialize the log header block.
306 	 */
307 	tx = dmu_tx_create(zilog->zl_os);
308 	(void) dmu_tx_assign(tx, TXG_WAIT);
309 	dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
310 	txg = dmu_tx_get_txg(tx);
311 
312 	/*
313 	 * If we don't have a log block already then
314 	 * allocate the first log block and assign its checksum verifier.
315 	 */
316 	no_blk = BP_IS_HOLE(&zilog->zl_header->zh_log);
317 	if (no_blk) {
318 		error = zio_alloc_blk(zilog->zl_spa, ZIO_CHECKSUM_ZILOG,
319 		    ZIL_MIN_BLKSZ, &blk, txg);
320 	} else {
321 		blk = zilog->zl_header->zh_log;
322 		error = 0;
323 	}
324 	if (error == 0) {
325 		ZIO_SET_CHECKSUM(&blk.blk_cksum,
326 		    spa_get_random(-1ULL), spa_get_random(-1ULL),
327 		    dmu_objset_id(zilog->zl_os), 1ULL);
328 
329 		/*
330 		 * Allocate a log write buffer (lwb) for the first log block.
331 		 */
332 		lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP);
333 		lwb->lwb_zilog = zilog;
334 		lwb->lwb_blk = blk;
335 		lwb->lwb_nused = 0;
336 		lwb->lwb_sz = BP_GET_LSIZE(&lwb->lwb_blk);
337 		lwb->lwb_buf = zio_buf_alloc(lwb->lwb_sz);
338 		lwb->lwb_max_txg = txg;
339 		lwb->lwb_seq = 0;
340 		lwb->lwb_state = UNWRITTEN;
341 		mutex_enter(&zilog->zl_lock);
342 		list_insert_tail(&zilog->zl_lwb_list, lwb);
343 		mutex_exit(&zilog->zl_lock);
344 	}
345 
346 	dmu_tx_commit(tx);
347 	if (no_blk)
348 		txg_wait_synced(zilog->zl_dmu_pool, txg);
349 }
350 
351 /*
352  * In one tx, free all log blocks and clear the log header.
353  */
354 void
355 zil_destroy(zilog_t *zilog)
356 {
357 	dmu_tx_t *tx;
358 	uint64_t txg;
359 
360 	mutex_enter(&zilog->zl_destroy_lock);
361 
362 	if (BP_IS_HOLE(&zilog->zl_header->zh_log)) {
363 		mutex_exit(&zilog->zl_destroy_lock);
364 		return;
365 	}
366 
367 	tx = dmu_tx_create(zilog->zl_os);
368 	(void) dmu_tx_assign(tx, TXG_WAIT);
369 	dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
370 	txg = dmu_tx_get_txg(tx);
371 
372 	zil_parse(zilog, zil_free_log_block, zil_free_log_record, tx,
373 	    zilog->zl_header->zh_claim_txg);
374 	/*
375 	 * zil_sync clears the zil header as soon as the zl_destroy_txg commits
376 	 */
377 	zilog->zl_destroy_txg = txg;
378 
379 	dmu_tx_commit(tx);
380 	txg_wait_synced(zilog->zl_dmu_pool, txg);
381 
382 	mutex_exit(&zilog->zl_destroy_lock);
383 }
384 
385 void
386 zil_claim(char *osname, void *txarg)
387 {
388 	dmu_tx_t *tx = txarg;
389 	uint64_t first_txg = dmu_tx_get_txg(tx);
390 	zilog_t *zilog;
391 	zil_header_t *zh;
392 	objset_t *os;
393 	int error;
394 
395 	error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_STANDARD, &os);
396 	if (error) {
397 		cmn_err(CE_WARN, "can't process intent log for %s", osname);
398 		return;
399 	}
400 
401 	zilog = dmu_objset_zil(os);
402 	zh = zilog->zl_header;
403 
404 	/*
405 	 * Claim all log blocks if we haven't already done so.
406 	 */
407 	ASSERT3U(zh->zh_claim_txg, <=, first_txg);
408 	if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) {
409 		zh->zh_claim_txg = first_txg;
410 		zil_parse(zilog, zil_claim_log_block, zil_claim_log_record,
411 		    tx, first_txg);
412 		dsl_dataset_dirty(dmu_objset_ds(os), tx);
413 	}
414 	ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1));
415 	dmu_objset_close(os);
416 }
417 
418 void
419 zil_add_vdev(zilog_t *zilog, uint64_t vdev, uint64_t seq)
420 {
421 	zil_vdev_t *zv;
422 
423 	if (zil_noflush)
424 		return;
425 
426 	ASSERT(MUTEX_HELD(&zilog->zl_lock));
427 	zv = kmem_alloc(sizeof (zil_vdev_t), KM_SLEEP);
428 	zv->vdev = vdev;
429 	zv->seq = seq;
430 	list_insert_tail(&zilog->zl_vdev_list, zv);
431 }
432 
433 void
434 zil_flush_vdevs(zilog_t *zilog, uint64_t seq)
435 {
436 	vdev_t *vd;
437 	zil_vdev_t *zv, *zv2;
438 	zio_t *zio;
439 	spa_t *spa;
440 	uint64_t vdev;
441 
442 	if (zil_noflush)
443 		return;
444 
445 	ASSERT(MUTEX_HELD(&zilog->zl_lock));
446 
447 	spa = zilog->zl_spa;
448 	zio = NULL;
449 
450 	while ((zv = list_head(&zilog->zl_vdev_list)) != NULL &&
451 	    zv->seq <= seq) {
452 		vdev = zv->vdev;
453 		list_remove(&zilog->zl_vdev_list, zv);
454 		kmem_free(zv, sizeof (zil_vdev_t));
455 
456 		/*
457 		 * remove all chained entries <= seq with same vdev
458 		 */
459 		zv = list_head(&zilog->zl_vdev_list);
460 		while (zv && zv->seq <= seq) {
461 			zv2 = list_next(&zilog->zl_vdev_list, zv);
462 			if (zv->vdev == vdev) {
463 				list_remove(&zilog->zl_vdev_list, zv);
464 				kmem_free(zv, sizeof (zil_vdev_t));
465 			}
466 			zv = zv2;
467 		}
468 
469 		/* flush the write cache for this vdev */
470 		mutex_exit(&zilog->zl_lock);
471 		if (zio == NULL)
472 			zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
473 		vd = vdev_lookup_top(spa, vdev);
474 		ASSERT(vd);
475 		(void) zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE,
476 		    NULL, NULL, ZIO_PRIORITY_NOW,
477 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
478 		mutex_enter(&zilog->zl_lock);
479 	}
480 
481 	/*
482 	 * Wait for all the flushes to complete.  Not all devices actually
483 	 * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails.
484 	 */
485 	if (zio != NULL) {
486 		mutex_exit(&zilog->zl_lock);
487 		(void) zio_wait(zio);
488 		mutex_enter(&zilog->zl_lock);
489 	}
490 }
491 
492 /*
493  * Function called when a log block write completes
494  */
495 static void
496 zil_lwb_write_done(zio_t *zio)
497 {
498 	lwb_t *prev;
499 	lwb_t *lwb = zio->io_private;
500 	zilog_t *zilog = lwb->lwb_zilog;
501 	uint64_t max_seq;
502 
503 	/*
504 	 * Now that we've written this log block, we have a stable pointer
505 	 * to the next block in the chain, so it's OK to let the txg in
506 	 * which we allocated the next block sync.
507 	 */
508 	txg_rele_to_sync(&lwb->lwb_txgh);
509 
510 	zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
511 	mutex_enter(&zilog->zl_lock);
512 	lwb->lwb_buf = NULL;
513 	if (zio->io_error) {
514 		zilog->zl_log_error = B_TRUE;
515 		mutex_exit(&zilog->zl_lock);
516 		cv_broadcast(&zilog->zl_cv_seq);
517 		return;
518 	}
519 
520 	prev = list_prev(&zilog->zl_lwb_list, lwb);
521 	if (prev && prev->lwb_state != SEQ_COMPLETE) {
522 		/* There's an unwritten buffer in the chain before this one */
523 		lwb->lwb_state = SEQ_INCOMPLETE;
524 		mutex_exit(&zilog->zl_lock);
525 		return;
526 	}
527 
528 	max_seq = lwb->lwb_seq;
529 	lwb->lwb_state = SEQ_COMPLETE;
530 	/*
531 	 * We must also follow up the chain for already written buffers
532 	 * to see if we can set zl_ss_seq even higher.
533 	 */
534 	while (lwb = list_next(&zilog->zl_lwb_list, lwb)) {
535 		if (lwb->lwb_state != SEQ_INCOMPLETE)
536 			break;
537 		lwb->lwb_state = SEQ_COMPLETE;
538 		/* lwb_seq will be zero if we've written an empty buffer */
539 		if (lwb->lwb_seq) {
540 			ASSERT3U(max_seq, <, lwb->lwb_seq);
541 			max_seq = lwb->lwb_seq;
542 		}
543 	}
544 	zilog->zl_ss_seq = MAX(max_seq, zilog->zl_ss_seq);
545 	mutex_exit(&zilog->zl_lock);
546 	cv_broadcast(&zilog->zl_cv_seq);
547 }
548 
549 /*
550  * Start a log block write and advance to the next log block.
551  * Calls are serialized.
552  */
553 static lwb_t *
554 zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb)
555 {
556 	lwb_t *nlwb;
557 	zil_trailer_t *ztp = (zil_trailer_t *)(lwb->lwb_buf + lwb->lwb_sz) - 1;
558 	uint64_t txg;
559 	uint64_t zil_blksz;
560 	zbookmark_t zb;
561 	int error;
562 
563 	ASSERT(lwb->lwb_nused <= ZIL_BLK_DATA_SZ(lwb));
564 
565 	/*
566 	 * Allocate the next block and save its address in this block
567 	 * before writing it in order to establish the log chain.
568 	 * Note that if the allocation of nlwb synced before we wrote
569 	 * the block that points at it (lwb), we'd leak it if we crashed.
570 	 * Therefore, we don't do txg_rele_to_sync() until zil_lwb_write_done().
571 	 */
572 	txg = txg_hold_open(zilog->zl_dmu_pool, &lwb->lwb_txgh);
573 	txg_rele_to_quiesce(&lwb->lwb_txgh);
574 
575 	/*
576 	 * Pick a ZIL blocksize. We request a size that is the
577 	 * maximum of the previous used size, the current used size and
578 	 * the amount waiting in the queue.
579 	 */
580 	zil_blksz = MAX(zilog->zl_cur_used, zilog->zl_prev_used);
581 	zil_blksz = MAX(zil_blksz, zilog->zl_itx_list_sz + sizeof (*ztp));
582 	zil_blksz = P2ROUNDUP(zil_blksz, ZIL_MIN_BLKSZ);
583 	if (zil_blksz > ZIL_MAX_BLKSZ)
584 		zil_blksz = ZIL_MAX_BLKSZ;
585 
586 	error = zio_alloc_blk(zilog->zl_spa, ZIO_CHECKSUM_ZILOG,
587 	    zil_blksz, &ztp->zit_next_blk, txg);
588 	if (error) {
589 		/*
590 		 * Reinitialise the lwb.
591 		 * By returning NULL the caller will call tx_wait_synced()
592 		 */
593 		mutex_enter(&zilog->zl_lock);
594 		ASSERT(lwb->lwb_state == UNWRITTEN);
595 		lwb->lwb_nused = 0;
596 		lwb->lwb_seq = 0;
597 		mutex_exit(&zilog->zl_lock);
598 		txg_rele_to_sync(&lwb->lwb_txgh);
599 		return (NULL);
600 	}
601 
602 	ASSERT3U(ztp->zit_next_blk.blk_birth, ==, txg);
603 	ztp->zit_pad = 0;
604 	ztp->zit_nused = lwb->lwb_nused;
605 	ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum;
606 	ztp->zit_next_blk.blk_cksum = lwb->lwb_blk.blk_cksum;
607 	ztp->zit_next_blk.blk_cksum.zc_word[3]++;
608 
609 	/*
610 	 * Allocate a new log write buffer (lwb).
611 	 */
612 	nlwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP);
613 
614 	nlwb->lwb_zilog = zilog;
615 	nlwb->lwb_blk = ztp->zit_next_blk;
616 	nlwb->lwb_nused = 0;
617 	nlwb->lwb_sz = BP_GET_LSIZE(&nlwb->lwb_blk);
618 	nlwb->lwb_buf = zio_buf_alloc(nlwb->lwb_sz);
619 	nlwb->lwb_max_txg = txg;
620 	nlwb->lwb_seq = 0;
621 	nlwb->lwb_state = UNWRITTEN;
622 
623 	/*
624 	 * Put new lwb at the end of the log chain,
625 	 * and record the vdev for later flushing
626 	 */
627 	mutex_enter(&zilog->zl_lock);
628 	list_insert_tail(&zilog->zl_lwb_list, nlwb);
629 	zil_add_vdev(zilog, DVA_GET_VDEV(BP_IDENTITY(&(lwb->lwb_blk))),
630 	    lwb->lwb_seq);
631 	mutex_exit(&zilog->zl_lock);
632 
633 	/*
634 	 * write the old log block
635 	 */
636 	dprintf_bp(&lwb->lwb_blk, "lwb %p txg %llu: ", lwb, txg);
637 
638 	zb.zb_objset = lwb->lwb_blk.blk_cksum.zc_word[2];
639 	zb.zb_object = 0;
640 	zb.zb_level = -1;
641 	zb.zb_blkid = lwb->lwb_blk.blk_cksum.zc_word[3];
642 
643 	zio_nowait(zio_rewrite(NULL, zilog->zl_spa, ZIO_CHECKSUM_ZILOG, 0,
644 	    &lwb->lwb_blk, lwb->lwb_buf, lwb->lwb_sz, zil_lwb_write_done, lwb,
645 	    ZIO_PRIORITY_LOG_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb));
646 
647 	return (nlwb);
648 }
649 
650 static lwb_t *
651 zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb)
652 {
653 	lr_t *lrc = &itx->itx_lr; /* common log record */
654 	lr_write_t *lr;
655 	char *dbuf;
656 	uint64_t seq = lrc->lrc_seq;
657 	uint64_t txg = lrc->lrc_txg;
658 	uint64_t reclen = lrc->lrc_reclen;
659 	uint64_t dlen = 0;
660 	int error;
661 
662 	if (lwb == NULL)
663 		return (NULL);
664 	ASSERT(lwb->lwb_buf != NULL);
665 
666 	/*
667 	 * If it's a write, fetch the data or get its blkptr as appropriate.
668 	 */
669 	if (lrc->lrc_txtype == TX_WRITE) {
670 		lr = (lr_write_t *)lrc;
671 		if (txg > spa_freeze_txg(zilog->zl_spa))
672 			txg_wait_synced(zilog->zl_dmu_pool, txg);
673 		if (itx->itx_wr_state != WR_COPIED) {
674 			if (itx->itx_wr_state == WR_NEED_COPY) {
675 				dlen = P2ROUNDUP(lr->lr_length,
676 				    sizeof (uint64_t));
677 				ASSERT(dlen);
678 				dbuf = kmem_alloc(dlen, KM_NOSLEEP);
679 				/* on memory shortage use dmu_sync */
680 				if (dbuf == NULL) {
681 					itx->itx_wr_state = WR_INDIRECT;
682 					dlen = 0;
683 				}
684 			} else {
685 				ASSERT(itx->itx_wr_state == WR_INDIRECT);
686 				dbuf = NULL;
687 			}
688 			error = zilog->zl_get_data(itx->itx_private, lr, dbuf);
689 			if (error) {
690 				if (dlen)
691 					kmem_free(dbuf, dlen);
692 				if (error != ENOENT && error != EALREADY) {
693 					txg_wait_synced(zilog->zl_dmu_pool,
694 					    txg);
695 					mutex_enter(&zilog->zl_lock);
696 					zilog->zl_ss_seq =
697 					    MAX(seq, zilog->zl_ss_seq);
698 					mutex_exit(&zilog->zl_lock);
699 					return (lwb);
700 				}
701 				mutex_enter(&zilog->zl_lock);
702 				zil_add_vdev(zilog, DVA_GET_VDEV(BP_IDENTITY(
703 				    &(lr->lr_blkptr))), seq);
704 				mutex_exit(&zilog->zl_lock);
705 				return (lwb);
706 			}
707 		}
708 	}
709 
710 	zilog->zl_cur_used += (reclen + dlen);
711 
712 	/*
713 	 * If this record won't fit in the current log block, start a new one.
714 	 */
715 	if (lwb->lwb_nused + reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) {
716 		lwb = zil_lwb_write_start(zilog, lwb);
717 		if (lwb == NULL) {
718 			if (dlen)
719 				kmem_free(dbuf, dlen);
720 			return (NULL);
721 		}
722 		ASSERT(lwb->lwb_nused == 0);
723 		if (reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) {
724 			txg_wait_synced(zilog->zl_dmu_pool, txg);
725 			mutex_enter(&zilog->zl_lock);
726 			zilog->zl_ss_seq = MAX(seq, zilog->zl_ss_seq);
727 			mutex_exit(&zilog->zl_lock);
728 			if (dlen)
729 				kmem_free(dbuf, dlen);
730 			return (lwb);
731 		}
732 	}
733 
734 	lrc->lrc_reclen += dlen;
735 	bcopy(lrc, lwb->lwb_buf + lwb->lwb_nused, reclen);
736 	lwb->lwb_nused += reclen;
737 	if (dlen) {
738 		bcopy(dbuf, lwb->lwb_buf + lwb->lwb_nused, dlen);
739 		lwb->lwb_nused += dlen;
740 		kmem_free(dbuf, dlen);
741 		lrc->lrc_reclen -= dlen; /* for kmem_free of itx */
742 	}
743 	lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg);
744 	ASSERT3U(lwb->lwb_seq, <, seq);
745 	lwb->lwb_seq = seq;
746 	ASSERT3U(lwb->lwb_nused, <=, ZIL_BLK_DATA_SZ(lwb));
747 	ASSERT3U(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)), ==, 0);
748 
749 	return (lwb);
750 }
751 
752 itx_t *
753 zil_itx_create(int txtype, size_t lrsize)
754 {
755 	itx_t *itx;
756 
757 	lrsize = P2ROUNDUP(lrsize, sizeof (uint64_t));
758 
759 	itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP);
760 	itx->itx_lr.lrc_txtype = txtype;
761 	itx->itx_lr.lrc_reclen = lrsize;
762 	itx->itx_lr.lrc_seq = 0;	/* defensive */
763 
764 	return (itx);
765 }
766 
767 uint64_t
768 zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx)
769 {
770 	uint64_t seq;
771 
772 	ASSERT(itx->itx_lr.lrc_seq == 0);
773 
774 	mutex_enter(&zilog->zl_lock);
775 	list_insert_tail(&zilog->zl_itx_list, itx);
776 	zilog->zl_itx_list_sz += itx->itx_lr.lrc_reclen;
777 	itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx);
778 	itx->itx_lr.lrc_seq = seq = ++zilog->zl_itx_seq;
779 	mutex_exit(&zilog->zl_lock);
780 
781 	return (seq);
782 }
783 
784 /*
785  * Free up all in-memory intent log transactions that have now been synced.
786  */
787 static void
788 zil_itx_clean(zilog_t *zilog)
789 {
790 	uint64_t synced_txg = spa_last_synced_txg(zilog->zl_spa);
791 	uint64_t freeze_txg = spa_freeze_txg(zilog->zl_spa);
792 	uint64_t max_seq = 0;
793 	itx_t *itx;
794 
795 	mutex_enter(&zilog->zl_lock);
796 	while ((itx = list_head(&zilog->zl_itx_list)) != NULL &&
797 	    itx->itx_lr.lrc_txg <= MIN(synced_txg, freeze_txg)) {
798 		list_remove(&zilog->zl_itx_list, itx);
799 		zilog->zl_itx_list_sz -= itx->itx_lr.lrc_reclen;
800 		ASSERT3U(max_seq, <, itx->itx_lr.lrc_seq);
801 		max_seq = itx->itx_lr.lrc_seq;
802 		kmem_free(itx, offsetof(itx_t, itx_lr)
803 		    + itx->itx_lr.lrc_reclen);
804 	}
805 	if (max_seq > zilog->zl_ss_seq) {
806 		zilog->zl_ss_seq = max_seq;
807 		cv_broadcast(&zilog->zl_cv_seq);
808 	}
809 	mutex_exit(&zilog->zl_lock);
810 }
811 
812 void
813 zil_clean(zilog_t *zilog)
814 {
815 	/*
816 	 * Check for any log blocks that can be freed.
817 	 * Log blocks are only freed when the log block allocation and
818 	 * log records contained within are both known to be committed.
819 	 */
820 	mutex_enter(&zilog->zl_lock);
821 	if (list_head(&zilog->zl_itx_list) != NULL)
822 		(void) taskq_dispatch(zilog->zl_clean_taskq,
823 		    (void (*)(void *))zil_itx_clean, zilog, TQ_NOSLEEP);
824 	mutex_exit(&zilog->zl_lock);
825 }
826 
827 /*
828  * Push zfs transactions to stable storage up to the supplied sequence number.
829  */
830 void
831 zil_commit(zilog_t *zilog, uint64_t seq, int ioflag)
832 {
833 	uint64_t txg;
834 	uint64_t max_seq;
835 	uint64_t reclen;
836 	itx_t *itx;
837 	lwb_t *lwb;
838 	spa_t *spa;
839 
840 	if (zilog == NULL || seq == 0 ||
841 	    ((ioflag & (FSYNC | FDSYNC | FRSYNC)) == 0 && !zil_always))
842 		return;
843 
844 	spa = zilog->zl_spa;
845 	mutex_enter(&zilog->zl_lock);
846 
847 	seq = MIN(seq, zilog->zl_itx_seq);	/* cap seq at largest itx seq */
848 
849 	for (;;) {
850 		if (zilog->zl_ss_seq >= seq) {	/* already on stable storage */
851 			mutex_exit(&zilog->zl_lock);
852 			return;
853 		}
854 
855 		if (zilog->zl_writer == B_FALSE) /* no one writing, do it */
856 			break;
857 
858 		cv_wait(&zilog->zl_cv_write, &zilog->zl_lock);
859 	}
860 
861 	zilog->zl_writer = B_TRUE;
862 	max_seq = 0;
863 
864 	if (zilog->zl_suspend) {
865 		lwb = NULL;
866 	} else {
867 		lwb = list_tail(&zilog->zl_lwb_list);
868 		if (lwb == NULL) {
869 			mutex_exit(&zilog->zl_lock);
870 			zil_create(zilog);
871 			mutex_enter(&zilog->zl_lock);
872 			lwb = list_tail(&zilog->zl_lwb_list);
873 		}
874 	}
875 
876 	/*
877 	 * Loop through in-memory log transactions filling log blocks,
878 	 * until we reach the given sequence number and there's no more
879 	 * room in the write buffer.
880 	 */
881 	for (;;) {
882 		itx = list_head(&zilog->zl_itx_list);
883 		if (itx == NULL)
884 			break;
885 
886 		reclen = itx->itx_lr.lrc_reclen;
887 		if ((itx->itx_lr.lrc_seq > seq) &&
888 		    ((lwb == NULL) || (lwb->lwb_nused + reclen >
889 		    ZIL_BLK_DATA_SZ(lwb))))
890 			break;
891 
892 		list_remove(&zilog->zl_itx_list, itx);
893 		txg = itx->itx_lr.lrc_txg;
894 		ASSERT(txg);
895 
896 		mutex_exit(&zilog->zl_lock);
897 		if (txg > spa_last_synced_txg(spa) ||
898 		    txg > spa_freeze_txg(spa))
899 			lwb = zil_lwb_commit(zilog, itx, lwb);
900 		else
901 			max_seq = itx->itx_lr.lrc_seq;
902 		kmem_free(itx, offsetof(itx_t, itx_lr)
903 		    + itx->itx_lr.lrc_reclen);
904 		mutex_enter(&zilog->zl_lock);
905 		zilog->zl_itx_list_sz -= reclen;
906 	}
907 
908 	mutex_exit(&zilog->zl_lock);
909 
910 	/* write the last block out */
911 	if (lwb != NULL && lwb->lwb_nused != 0)
912 		lwb = zil_lwb_write_start(zilog, lwb);
913 
914 	zilog->zl_prev_used = zilog->zl_cur_used;
915 	zilog->zl_cur_used = 0;
916 
917 	mutex_enter(&zilog->zl_lock);
918 	if (max_seq > zilog->zl_ss_seq) {
919 		zilog->zl_ss_seq = max_seq;
920 		cv_broadcast(&zilog->zl_cv_seq);
921 	}
922 	/*
923 	 * Wait if necessary for our seq to be committed.
924 	 */
925 	if (lwb) {
926 		while (zilog->zl_ss_seq < seq && zilog->zl_log_error == 0)
927 			cv_wait(&zilog->zl_cv_seq, &zilog->zl_lock);
928 		zil_flush_vdevs(zilog, seq);
929 	}
930 
931 	if (zilog->zl_log_error || lwb == NULL) {
932 		zilog->zl_log_error = 0;
933 		max_seq = zilog->zl_itx_seq;
934 		mutex_exit(&zilog->zl_lock);
935 		txg_wait_synced(zilog->zl_dmu_pool, 0);
936 		mutex_enter(&zilog->zl_lock);
937 		zilog->zl_ss_seq = MAX(max_seq, zilog->zl_ss_seq);
938 		cv_broadcast(&zilog->zl_cv_seq);
939 	}
940 	/* wake up others waiting to start a write */
941 	zilog->zl_writer = B_FALSE;
942 	mutex_exit(&zilog->zl_lock);
943 	cv_broadcast(&zilog->zl_cv_write);
944 }
945 
946 /*
947  * Called in syncing context to free committed log blocks and update log header.
948  */
949 void
950 zil_sync(zilog_t *zilog, dmu_tx_t *tx)
951 {
952 	uint64_t txg = dmu_tx_get_txg(tx);
953 	spa_t *spa = zilog->zl_spa;
954 	lwb_t *lwb;
955 
956 	ASSERT(zilog->zl_stop_sync == 0);
957 
958 	zilog->zl_header->zh_replay_seq = zilog->zl_replay_seq[txg & TXG_MASK];
959 
960 	if (zilog->zl_destroy_txg == txg) {
961 		bzero(zilog->zl_header, sizeof (zil_header_t));
962 		bzero(zilog->zl_replay_seq, sizeof (zilog->zl_replay_seq));
963 		zilog->zl_destroy_txg = 0;
964 	}
965 
966 	mutex_enter(&zilog->zl_lock);
967 	for (;;) {
968 		lwb = list_head(&zilog->zl_lwb_list);
969 		if (lwb == NULL) {
970 			mutex_exit(&zilog->zl_lock);
971 			return;
972 		}
973 		if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg)
974 			break;
975 		list_remove(&zilog->zl_lwb_list, lwb);
976 		zio_free_blk(spa, &lwb->lwb_blk, txg);
977 		kmem_cache_free(zil_lwb_cache, lwb);
978 	}
979 	zilog->zl_header->zh_log = lwb->lwb_blk;
980 	mutex_exit(&zilog->zl_lock);
981 }
982 
983 void
984 zil_init(void)
985 {
986 	zil_lwb_cache = kmem_cache_create("zil_lwb_cache",
987 	    sizeof (struct lwb), NULL, NULL, NULL, NULL, NULL, NULL, 0);
988 }
989 
990 void
991 zil_fini(void)
992 {
993 	kmem_cache_destroy(zil_lwb_cache);
994 }
995 
996 zilog_t *
997 zil_alloc(objset_t *os, zil_header_t *zh_phys)
998 {
999 	zilog_t *zilog;
1000 
1001 	zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP);
1002 
1003 	zilog->zl_header = zh_phys;
1004 	zilog->zl_os = os;
1005 	zilog->zl_spa = dmu_objset_spa(os);
1006 	zilog->zl_dmu_pool = dmu_objset_pool(os);
1007 
1008 	list_create(&zilog->zl_itx_list, sizeof (itx_t),
1009 	    offsetof(itx_t, itx_node));
1010 
1011 	list_create(&zilog->zl_lwb_list, sizeof (lwb_t),
1012 	    offsetof(lwb_t, lwb_node));
1013 
1014 	list_create(&zilog->zl_vdev_list, sizeof (zil_vdev_t),
1015 	    offsetof(zil_vdev_t, vdev_seq_node));
1016 
1017 	return (zilog);
1018 }
1019 
1020 void
1021 zil_free(zilog_t *zilog)
1022 {
1023 	lwb_t *lwb;
1024 	zil_vdev_t *zv;
1025 
1026 	zilog->zl_stop_sync = 1;
1027 
1028 	while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
1029 		list_remove(&zilog->zl_lwb_list, lwb);
1030 		if (lwb->lwb_buf != NULL)
1031 			zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
1032 		kmem_cache_free(zil_lwb_cache, lwb);
1033 	}
1034 	list_destroy(&zilog->zl_lwb_list);
1035 
1036 	while ((zv = list_head(&zilog->zl_vdev_list)) != NULL) {
1037 		list_remove(&zilog->zl_vdev_list, zv);
1038 		kmem_free(zv, sizeof (zil_vdev_t));
1039 	}
1040 	list_destroy(&zilog->zl_vdev_list);
1041 
1042 	ASSERT(list_head(&zilog->zl_itx_list) == NULL);
1043 	list_destroy(&zilog->zl_itx_list);
1044 
1045 	kmem_free(zilog, sizeof (zilog_t));
1046 }
1047 
1048 /*
1049  * return true if the initial log block is not valid
1050  */
1051 static int
1052 zil_empty(zilog_t *zilog)
1053 {
1054 	blkptr_t blk;
1055 	char *lrbuf;
1056 	int error;
1057 
1058 	blk = zilog->zl_header->zh_log;
1059 	if (BP_IS_HOLE(&blk))
1060 		return (1);
1061 
1062 	lrbuf = zio_buf_alloc(SPA_MAXBLOCKSIZE);
1063 	error = zil_read_log_block(zilog, &blk, lrbuf);
1064 	zio_buf_free(lrbuf, SPA_MAXBLOCKSIZE);
1065 	return (error ? 1 : 0);
1066 }
1067 
1068 /*
1069  * Open an intent log.
1070  */
1071 zilog_t *
1072 zil_open(objset_t *os, zil_get_data_t *get_data)
1073 {
1074 	zilog_t *zilog = dmu_objset_zil(os);
1075 
1076 	zilog->zl_get_data = get_data;
1077 	zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri,
1078 	    2, 2, TASKQ_PREPOPULATE);
1079 
1080 	return (zilog);
1081 }
1082 
1083 /*
1084  * Close an intent log.
1085  */
1086 void
1087 zil_close(zilog_t *zilog)
1088 {
1089 	if (!zil_is_committed(zilog))
1090 		txg_wait_synced(zilog->zl_dmu_pool, 0);
1091 	taskq_destroy(zilog->zl_clean_taskq);
1092 	zilog->zl_clean_taskq = NULL;
1093 	zilog->zl_get_data = NULL;
1094 
1095 	zil_itx_clean(zilog);
1096 	ASSERT(list_head(&zilog->zl_itx_list) == NULL);
1097 }
1098 
1099 /*
1100  * Suspend an intent log.  While in suspended mode, we still honor
1101  * synchronous semantics, but we rely on txg_wait_synced() to do it.
1102  * We suspend the log briefly when taking a snapshot so that the snapshot
1103  * contains all the data it's supposed to, and has an empty intent log.
1104  */
1105 int
1106 zil_suspend(zilog_t *zilog)
1107 {
1108 	lwb_t *lwb;
1109 
1110 	mutex_enter(&zilog->zl_lock);
1111 	if (zilog->zl_header->zh_claim_txg != 0) {	/* unplayed log */
1112 		mutex_exit(&zilog->zl_lock);
1113 		return (EBUSY);
1114 	}
1115 	zilog->zl_suspend++;
1116 	mutex_exit(&zilog->zl_lock);
1117 
1118 	zil_commit(zilog, UINT64_MAX, FSYNC);
1119 
1120 	mutex_enter(&zilog->zl_lock);
1121 	while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
1122 		if (lwb->lwb_buf != NULL) {
1123 			/*
1124 			 * Wait for the buffer if it's in the process of
1125 			 * being written.
1126 			 */
1127 			if ((lwb->lwb_seq != 0) &&
1128 			    (lwb->lwb_state != SEQ_COMPLETE)) {
1129 				cv_wait(&zilog->zl_cv_seq, &zilog->zl_lock);
1130 				continue;
1131 			}
1132 			zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
1133 		}
1134 		list_remove(&zilog->zl_lwb_list, lwb);
1135 		kmem_cache_free(zil_lwb_cache, lwb);
1136 	}
1137 	mutex_exit(&zilog->zl_lock);
1138 
1139 	zil_destroy(zilog);
1140 
1141 	return (0);
1142 }
1143 
1144 void
1145 zil_resume(zilog_t *zilog)
1146 {
1147 	mutex_enter(&zilog->zl_lock);
1148 	ASSERT(zilog->zl_suspend != 0);
1149 	zilog->zl_suspend--;
1150 	mutex_exit(&zilog->zl_lock);
1151 }
1152 
1153 typedef struct zil_replay_arg {
1154 	objset_t	*zr_os;
1155 	zil_replay_func_t **zr_replay;
1156 	void		*zr_arg;
1157 	void		(*zr_rm_sync)(void *arg);
1158 	uint64_t	*zr_txgp;
1159 	boolean_t	zr_byteswap;
1160 	char		*zr_lrbuf;
1161 } zil_replay_arg_t;
1162 
1163 static void
1164 zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg)
1165 {
1166 	zil_replay_arg_t *zr = zra;
1167 	zil_header_t *zh = zilog->zl_header;
1168 	uint64_t reclen = lr->lrc_reclen;
1169 	uint64_t txtype = lr->lrc_txtype;
1170 	int pass, error;
1171 
1172 	if (zilog->zl_stop_replay)
1173 		return;
1174 
1175 	if (lr->lrc_txg < claim_txg)		/* already committed */
1176 		return;
1177 
1178 	if (lr->lrc_seq <= zh->zh_replay_seq)	/* already replayed */
1179 		return;
1180 
1181 	/*
1182 	 * Make a copy of the data so we can revise and extend it.
1183 	 */
1184 	bcopy(lr, zr->zr_lrbuf, reclen);
1185 
1186 	/*
1187 	 * The log block containing this lr may have been byteswapped
1188 	 * so that we can easily examine common fields like lrc_txtype.
1189 	 * However, the log is a mix of different data types, and only the
1190 	 * replay vectors know how to byteswap their records.  Therefore, if
1191 	 * the lr was byteswapped, undo it before invoking the replay vector.
1192 	 */
1193 	if (zr->zr_byteswap)
1194 		byteswap_uint64_array(zr->zr_lrbuf, reclen);
1195 
1196 	/*
1197 	 * If this is a TX_WRITE with a blkptr, suck in the data.
1198 	 */
1199 	if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) {
1200 		lr_write_t *lrw = (lr_write_t *)lr;
1201 		blkptr_t *wbp = &lrw->lr_blkptr;
1202 		uint64_t wlen = lrw->lr_length;
1203 		char *wbuf = zr->zr_lrbuf + reclen;
1204 
1205 		if (BP_IS_HOLE(wbp)) {	/* compressed to a hole */
1206 			bzero(wbuf, wlen);
1207 		} else {
1208 			/*
1209 			 * A subsequent write may have overwritten this block,
1210 			 * in which case wbp may have been been freed and
1211 			 * reallocated, and our read of wbp may fail with a
1212 			 * checksum error.  We can safely ignore this because
1213 			 * the later write will provide the correct data.
1214 			 */
1215 			zbookmark_t zb;
1216 
1217 			zb.zb_objset = dmu_objset_id(zilog->zl_os);
1218 			zb.zb_object = lrw->lr_foid;
1219 			zb.zb_level = -1;
1220 			zb.zb_blkid = lrw->lr_offset / BP_GET_LSIZE(wbp);
1221 
1222 			(void) zio_wait(zio_read(NULL, zilog->zl_spa,
1223 			    wbp, wbuf, BP_GET_LSIZE(wbp), NULL, NULL,
1224 			    ZIO_PRIORITY_SYNC_READ,
1225 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &zb));
1226 			(void) memmove(wbuf, wbuf + lrw->lr_blkoff, wlen);
1227 		}
1228 	}
1229 
1230 	/*
1231 	 * We must now do two things atomically: replay this log record,
1232 	 * and update the log header to reflect the fact that we did so.
1233 	 * We use the DMU's ability to assign into a specific txg to do this.
1234 	 */
1235 	for (pass = 1; /* CONSTANTCONDITION */; pass++) {
1236 		uint64_t replay_txg;
1237 		dmu_tx_t *replay_tx;
1238 
1239 		replay_tx = dmu_tx_create(zr->zr_os);
1240 		error = dmu_tx_assign(replay_tx, TXG_WAIT);
1241 		if (error) {
1242 			dmu_tx_abort(replay_tx);
1243 			break;
1244 		}
1245 
1246 		replay_txg = dmu_tx_get_txg(replay_tx);
1247 
1248 		if (txtype == 0 || txtype >= TX_MAX_TYPE) {
1249 			error = EINVAL;
1250 		} else {
1251 			/*
1252 			 * On the first pass, arrange for the replay vector
1253 			 * to fail its dmu_tx_assign().  That's the only way
1254 			 * to ensure that those code paths remain well tested.
1255 			 */
1256 			*zr->zr_txgp = replay_txg - (pass == 1);
1257 			error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lrbuf,
1258 			    zr->zr_byteswap);
1259 			*zr->zr_txgp = TXG_NOWAIT;
1260 		}
1261 
1262 		if (error == 0) {
1263 			dsl_dataset_dirty(dmu_objset_ds(zr->zr_os), replay_tx);
1264 			zilog->zl_replay_seq[replay_txg & TXG_MASK] =
1265 			    lr->lrc_seq;
1266 		}
1267 
1268 		dmu_tx_commit(replay_tx);
1269 
1270 		if (error != ERESTART)
1271 			break;
1272 
1273 		if (pass != 1)
1274 			txg_wait_open(spa_get_dsl(zilog->zl_spa),
1275 			    replay_txg + 1);
1276 
1277 		dprintf("pass %d, retrying\n", pass);
1278 	}
1279 
1280 	if (error) {
1281 		char *name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1282 		dmu_objset_name(zr->zr_os, name);
1283 		cmn_err(CE_WARN, "ZFS replay transaction error %d, "
1284 		    "dataset %s, seq 0x%llx, txtype %llu\n",
1285 		    error, name,
1286 		    (u_longlong_t)lr->lrc_seq, (u_longlong_t)txtype);
1287 		zilog->zl_stop_replay = 1;
1288 		kmem_free(name, MAXNAMELEN);
1289 	}
1290 
1291 	/*
1292 	 * The DMU's dnode layer doesn't see removes until the txg commits,
1293 	 * so a subsequent claim can spuriously fail with EEXIST.
1294 	 * To prevent this, if we might have removed an object,
1295 	 * wait for the delete thread to delete it, and then
1296 	 * wait for the transaction group to sync.
1297 	 */
1298 	if (txtype == TX_REMOVE || txtype == TX_RMDIR || txtype == TX_RENAME) {
1299 		if (zr->zr_rm_sync != NULL)
1300 			zr->zr_rm_sync(zr->zr_arg);
1301 		txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0);
1302 	}
1303 }
1304 
1305 /*
1306  * If this dataset has a non-empty intent log, replay it and destroy it.
1307  */
1308 void
1309 zil_replay(objset_t *os, void *arg, uint64_t *txgp,
1310 	zil_replay_func_t *replay_func[TX_MAX_TYPE], void (*rm_sync)(void *arg))
1311 {
1312 	zilog_t *zilog = dmu_objset_zil(os);
1313 		zil_replay_arg_t zr;
1314 
1315 	if (zil_empty(zilog)) {
1316 		/*
1317 		 * Initialise the log header but don't free the log block
1318 		 * which will get reused.
1319 		 */
1320 		zilog->zl_header->zh_claim_txg = 0;
1321 		zilog->zl_header->zh_replay_seq = 0;
1322 		return;
1323 	}
1324 
1325 	zr.zr_os = os;
1326 	zr.zr_replay = replay_func;
1327 	zr.zr_arg = arg;
1328 	zr.zr_rm_sync = rm_sync;
1329 	zr.zr_txgp = txgp;
1330 	zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zilog->zl_header->zh_log);
1331 	zr.zr_lrbuf = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP);
1332 
1333 	/*
1334 	 * Wait for in-progress removes to sync before starting replay.
1335 	 */
1336 	if (rm_sync != NULL)
1337 		rm_sync(arg);
1338 	txg_wait_synced(zilog->zl_dmu_pool, 0);
1339 
1340 	zilog->zl_stop_replay = 0;
1341 	zil_parse(zilog, NULL, zil_replay_log_record, &zr,
1342 	    zilog->zl_header->zh_claim_txg);
1343 	kmem_free(zr.zr_lrbuf, 2 * SPA_MAXBLOCKSIZE);
1344 
1345 	zil_destroy(zilog);
1346 }
1347 
1348 /*
1349  * Report whether all transactions are committed
1350  */
1351 int
1352 zil_is_committed(zilog_t *zilog)
1353 {
1354 	lwb_t *lwb;
1355 
1356 	if (zilog == NULL || list_head(&zilog->zl_itx_list))
1357 		return (B_FALSE);
1358 
1359 	/*
1360 	 * A log write buffer at the head of the list that is not UNWRITTEN
1361 	 * means there's a lwb yet to be freed after a txg commit
1362 	 */
1363 	lwb = list_head(&zilog->zl_lwb_list);
1364 	if (lwb && lwb->lwb_state != UNWRITTEN)
1365 		return (B_FALSE);
1366 	ASSERT(zil_empty(zilog));
1367 	return (B_TRUE);
1368 }
1369