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.
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.
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]
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
28 #include <sys/zfs_context.h>
34 #include <sys/resource.h>
36 #include <sys/zil_impl.h>
37 #include <sys/dsl_dataset.h>
39 #include <sys/dmu_tx.h>
42 * The zfs intent log (ZIL) saves transaction records of system calls
43 * that change the file system in memory with enough information
44 * to be able to replay them. These are stored in memory until
45 * either the DMU transaction group (txg) commits them to the stable pool
46 * and they can be discarded, or they are flushed to the stable log
47 * (also in the pool) due to a fsync, O_DSYNC or other synchronous
48 * requirement. In the event of a panic or power fail then those log
49 * records (transactions) are replayed.
51 * There is one ZIL per file system. Its on-disk (pool) format consists
58 * A log record holds a system call transaction. Log blocks can
59 * hold many log records and the blocks are chained together.
60 * Each ZIL block contains a block pointer (blkptr_t) to the next
61 * ZIL block in the chain. The ZIL header points to the first
62 * block in the chain. Note there is not a fixed place in the pool
63 * to hold blocks. They are dynamically allocated and freed as
64 * needed from the blocks available. Figure X shows the ZIL structure:
68 * This global ZIL switch affects all pools
70 int zil_disable = 0; /* disable intent logging */
71 SYSCTL_DECL(_vfs_zfs);
72 TUNABLE_INT("vfs.zfs.zil_disable", &zil_disable);
73 SYSCTL_INT(_vfs_zfs, OID_AUTO, zil_disable, CTLFLAG_RW, &zil_disable, 0,
74 "Disable ZFS Intent Log (ZIL)");
77 * Tunable parameter for debugging or performance analysis. Setting
78 * zfs_nocacheflush will cause corruption on power loss if a volatile
79 * out-of-order write cache is enabled.
81 boolean_t zfs_nocacheflush = B_FALSE;
82 TUNABLE_INT("vfs.zfs.cache_flush_disable", &zfs_nocacheflush);
83 SYSCTL_INT(_vfs_zfs, OID_AUTO, cache_flush_disable, CTLFLAG_RDTUN,
84 &zfs_nocacheflush, 0, "Disable cache flush");
86 static kmem_cache_t *zil_lwb_cache;
89 zil_dva_compare(const void *x1, const void *x2)
91 const dva_t *dva1 = x1;
92 const dva_t *dva2 = x2;
94 if (DVA_GET_VDEV(dva1) < DVA_GET_VDEV(dva2))
96 if (DVA_GET_VDEV(dva1) > DVA_GET_VDEV(dva2))
99 if (DVA_GET_OFFSET(dva1) < DVA_GET_OFFSET(dva2))
101 if (DVA_GET_OFFSET(dva1) > DVA_GET_OFFSET(dva2))
108 zil_dva_tree_init(avl_tree_t *t)
110 avl_create(t, zil_dva_compare, sizeof (zil_dva_node_t),
111 offsetof(zil_dva_node_t, zn_node));
115 zil_dva_tree_fini(avl_tree_t *t)
120 while ((zn = avl_destroy_nodes(t, &cookie)) != NULL)
121 kmem_free(zn, sizeof (zil_dva_node_t));
127 zil_dva_tree_add(avl_tree_t *t, dva_t *dva)
132 if (avl_find(t, dva, &where) != NULL)
135 zn = kmem_alloc(sizeof (zil_dva_node_t), KM_SLEEP);
137 avl_insert(t, zn, where);
142 static zil_header_t *
143 zil_header_in_syncing_context(zilog_t *zilog)
145 return ((zil_header_t *)zilog->zl_header);
149 zil_init_log_chain(zilog_t *zilog, blkptr_t *bp)
151 zio_cksum_t *zc = &bp->blk_cksum;
153 zc->zc_word[ZIL_ZC_GUID_0] = spa_get_random(-1ULL);
154 zc->zc_word[ZIL_ZC_GUID_1] = spa_get_random(-1ULL);
155 zc->zc_word[ZIL_ZC_OBJSET] = dmu_objset_id(zilog->zl_os);
156 zc->zc_word[ZIL_ZC_SEQ] = 1ULL;
160 * Read a log block, make sure it's valid, and byteswap it if necessary.
163 zil_read_log_block(zilog_t *zilog, const blkptr_t *bp, arc_buf_t **abufpp)
167 uint32_t aflags = ARC_WAIT;
170 zb.zb_objset = bp->blk_cksum.zc_word[ZIL_ZC_OBJSET];
173 zb.zb_blkid = bp->blk_cksum.zc_word[ZIL_ZC_SEQ];
177 error = arc_read(NULL, zilog->zl_spa, &blk, byteswap_uint64_array,
178 arc_getbuf_func, abufpp, ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL |
179 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB, &aflags, &zb);
182 char *data = (*abufpp)->b_data;
183 uint64_t blksz = BP_GET_LSIZE(bp);
184 zil_trailer_t *ztp = (zil_trailer_t *)(data + blksz) - 1;
185 zio_cksum_t cksum = bp->blk_cksum;
188 * Sequence numbers should be... sequential. The checksum
189 * verifier for the next block should be bp's checksum plus 1.
191 cksum.zc_word[ZIL_ZC_SEQ]++;
193 if (bcmp(&cksum, &ztp->zit_next_blk.blk_cksum, sizeof (cksum)))
195 else if (BP_IS_HOLE(&ztp->zit_next_blk))
197 else if (ztp->zit_nused > (blksz - sizeof (zil_trailer_t)))
201 VERIFY(arc_buf_remove_ref(*abufpp, abufpp) == 1);
206 dprintf("error %d on %llu:%llu\n", error, zb.zb_objset, zb.zb_blkid);
212 * Parse the intent log, and call parse_func for each valid record within.
213 * Return the highest sequence number.
216 zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func,
217 zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg)
219 const zil_header_t *zh = zilog->zl_header;
220 uint64_t claim_seq = zh->zh_claim_seq;
222 uint64_t max_seq = 0;
223 blkptr_t blk = zh->zh_log;
229 if (BP_IS_HOLE(&blk))
233 * Starting at the block pointed to by zh_log we read the log chain.
234 * For each block in the chain we strongly check that block to
235 * ensure its validity. We stop when an invalid block is found.
236 * For each block pointer in the chain we call parse_blk_func().
237 * For each record in each valid block we call parse_lr_func().
238 * If the log has been claimed, stop if we encounter a sequence
239 * number greater than the highest claimed sequence number.
241 zil_dva_tree_init(&zilog->zl_dva_tree);
243 seq = blk.blk_cksum.zc_word[ZIL_ZC_SEQ];
245 if (claim_seq != 0 && seq > claim_seq)
248 ASSERT(max_seq < seq);
251 error = zil_read_log_block(zilog, &blk, &abuf);
253 if (parse_blk_func != NULL)
254 parse_blk_func(zilog, &blk, arg, txg);
259 lrbuf = abuf->b_data;
260 ztp = (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1;
261 blk = ztp->zit_next_blk;
263 if (parse_lr_func == NULL) {
264 VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
268 for (lrp = lrbuf; lrp < lrbuf + ztp->zit_nused; lrp += reclen) {
269 lr_t *lr = (lr_t *)lrp;
270 reclen = lr->lrc_reclen;
271 ASSERT3U(reclen, >=, sizeof (lr_t));
272 parse_lr_func(zilog, lr, arg, txg);
274 VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
276 zil_dva_tree_fini(&zilog->zl_dva_tree);
283 zil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg)
285 spa_t *spa = zilog->zl_spa;
289 * Claim log block if not already committed and not already claimed.
291 if (bp->blk_birth >= first_txg &&
292 zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp)) == 0) {
293 err = zio_wait(zio_claim(NULL, spa, first_txg, bp, NULL, NULL));
299 zil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg)
301 if (lrc->lrc_txtype == TX_WRITE) {
302 lr_write_t *lr = (lr_write_t *)lrc;
303 zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg);
309 zil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg)
311 zio_free_blk(zilog->zl_spa, bp, dmu_tx_get_txg(tx));
315 zil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg)
318 * If we previously claimed it, we need to free it.
320 if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE) {
321 lr_write_t *lr = (lr_write_t *)lrc;
322 blkptr_t *bp = &lr->lr_blkptr;
323 if (bp->blk_birth >= claim_txg &&
324 !zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp))) {
325 (void) arc_free(NULL, zilog->zl_spa,
326 dmu_tx_get_txg(tx), bp, NULL, NULL, ARC_WAIT);
332 * Create an on-disk intent log.
335 zil_create(zilog_t *zilog)
337 const zil_header_t *zh = zilog->zl_header;
345 * Wait for any previous destroy to complete.
347 txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
349 ASSERT(zh->zh_claim_txg == 0);
350 ASSERT(zh->zh_replay_seq == 0);
355 * If we don't already have an initial log block, allocate one now.
357 if (BP_IS_HOLE(&blk)) {
358 tx = dmu_tx_create(zilog->zl_os);
359 (void) dmu_tx_assign(tx, TXG_WAIT);
360 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
361 txg = dmu_tx_get_txg(tx);
363 error = zio_alloc_blk(zilog->zl_spa, ZIL_MIN_BLKSZ, &blk,
367 zil_init_log_chain(zilog, &blk);
371 * Allocate a log write buffer (lwb) for the first log block.
374 lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP);
375 lwb->lwb_zilog = zilog;
378 lwb->lwb_sz = BP_GET_LSIZE(&lwb->lwb_blk);
379 lwb->lwb_buf = zio_buf_alloc(lwb->lwb_sz);
380 lwb->lwb_max_txg = txg;
383 mutex_enter(&zilog->zl_lock);
384 list_insert_tail(&zilog->zl_lwb_list, lwb);
385 mutex_exit(&zilog->zl_lock);
389 * If we just allocated the first log block, commit our transaction
390 * and wait for zil_sync() to stuff the block poiner into zh_log.
391 * (zh is part of the MOS, so we cannot modify it in open context.)
395 txg_wait_synced(zilog->zl_dmu_pool, txg);
398 ASSERT(bcmp(&blk, &zh->zh_log, sizeof (blk)) == 0);
402 * In one tx, free all log blocks and clear the log header.
403 * If keep_first is set, then we're replaying a log with no content.
404 * We want to keep the first block, however, so that the first
405 * synchronous transaction doesn't require a txg_wait_synced()
406 * in zil_create(). We don't need to txg_wait_synced() here either
407 * when keep_first is set, because both zil_create() and zil_destroy()
408 * will wait for any in-progress destroys to complete.
411 zil_destroy(zilog_t *zilog, boolean_t keep_first)
413 const zil_header_t *zh = zilog->zl_header;
419 * Wait for any previous destroy to complete.
421 txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
423 if (BP_IS_HOLE(&zh->zh_log))
426 tx = dmu_tx_create(zilog->zl_os);
427 (void) dmu_tx_assign(tx, TXG_WAIT);
428 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
429 txg = dmu_tx_get_txg(tx);
431 mutex_enter(&zilog->zl_lock);
433 ASSERT3U(zilog->zl_destroy_txg, <, txg);
434 zilog->zl_destroy_txg = txg;
435 zilog->zl_keep_first = keep_first;
437 if (!list_is_empty(&zilog->zl_lwb_list)) {
438 ASSERT(zh->zh_claim_txg == 0);
440 while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
441 list_remove(&zilog->zl_lwb_list, lwb);
442 if (lwb->lwb_buf != NULL)
443 zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
444 zio_free_blk(zilog->zl_spa, &lwb->lwb_blk, txg);
445 kmem_cache_free(zil_lwb_cache, lwb);
449 (void) zil_parse(zilog, zil_free_log_block,
450 zil_free_log_record, tx, zh->zh_claim_txg);
453 mutex_exit(&zilog->zl_lock);
457 if (keep_first) /* no need to wait in this case */
460 txg_wait_synced(zilog->zl_dmu_pool, txg);
461 ASSERT(BP_IS_HOLE(&zh->zh_log));
465 zil_claim(char *osname, void *txarg)
467 dmu_tx_t *tx = txarg;
468 uint64_t first_txg = dmu_tx_get_txg(tx);
474 error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_STANDARD, &os);
476 cmn_err(CE_WARN, "can't process intent log for %s", osname);
480 zilog = dmu_objset_zil(os);
481 zh = zil_header_in_syncing_context(zilog);
484 * Claim all log blocks if we haven't already done so, and remember
485 * the highest claimed sequence number. This ensures that if we can
486 * read only part of the log now (e.g. due to a missing device),
487 * but we can read the entire log later, we will not try to replay
488 * or destroy beyond the last block we successfully claimed.
490 ASSERT3U(zh->zh_claim_txg, <=, first_txg);
491 if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) {
492 zh->zh_claim_txg = first_txg;
493 zh->zh_claim_seq = zil_parse(zilog, zil_claim_log_block,
494 zil_claim_log_record, tx, first_txg);
495 dsl_dataset_dirty(dmu_objset_ds(os), tx);
498 ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1));
499 dmu_objset_close(os);
504 zil_add_vdev(zilog_t *zilog, uint64_t vdev)
506 zil_vdev_t *zv, *new;
507 uint64_t bmap_sz = sizeof (zilog->zl_vdev_bmap) << 3;
510 if (zfs_nocacheflush)
513 if (vdev < bmap_sz) {
514 cp = zilog->zl_vdev_bmap + (vdev / 8);
515 atomic_or_8(cp, 1 << (vdev % 8));
518 * insert into ordered list
520 mutex_enter(&zilog->zl_lock);
521 for (zv = list_head(&zilog->zl_vdev_list); zv != NULL;
522 zv = list_next(&zilog->zl_vdev_list, zv)) {
523 if (zv->vdev == vdev) {
524 /* duplicate found - just return */
525 mutex_exit(&zilog->zl_lock);
528 if (zv->vdev > vdev) {
529 /* insert before this entry */
530 new = kmem_alloc(sizeof (zil_vdev_t),
533 list_insert_before(&zilog->zl_vdev_list,
535 mutex_exit(&zilog->zl_lock);
539 /* ran off end of list, insert at the end */
541 new = kmem_alloc(sizeof (zil_vdev_t), KM_SLEEP);
543 list_insert_tail(&zilog->zl_vdev_list, new);
544 mutex_exit(&zilog->zl_lock);
548 /* start an async flush of the write cache for this vdev */
550 zil_flush_vdev(spa_t *spa, uint64_t vdev, zio_t **zio)
555 *zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
557 vd = vdev_lookup_top(spa, vdev);
560 (void) zio_nowait(zio_ioctl(*zio, spa, vd, DKIOCFLUSHWRITECACHE,
561 NULL, NULL, ZIO_PRIORITY_NOW,
562 ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
566 zil_flush_vdevs(zilog_t *zilog)
570 spa_t *spa = zilog->zl_spa;
575 ASSERT(zilog->zl_writer);
577 for (i = 0; i < sizeof (zilog->zl_vdev_bmap); i++) {
578 b = zilog->zl_vdev_bmap[i];
581 for (j = 0; j < 8; j++) {
584 zil_flush_vdev(spa, vdev, &zio);
587 zilog->zl_vdev_bmap[i] = 0;
590 while ((zv = list_head(&zilog->zl_vdev_list)) != NULL) {
591 zil_flush_vdev(spa, zv->vdev, &zio);
592 list_remove(&zilog->zl_vdev_list, zv);
593 kmem_free(zv, sizeof (zil_vdev_t));
596 * Wait for all the flushes to complete. Not all devices actually
597 * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails.
600 (void) zio_wait(zio);
604 * Function called when a log block write completes
607 zil_lwb_write_done(zio_t *zio)
609 lwb_t *lwb = zio->io_private;
610 zilog_t *zilog = lwb->lwb_zilog;
613 * Now that we've written this log block, we have a stable pointer
614 * to the next block in the chain, so it's OK to let the txg in
615 * which we allocated the next block sync.
617 txg_rele_to_sync(&lwb->lwb_txgh);
619 zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
620 mutex_enter(&zilog->zl_lock);
623 zilog->zl_log_error = B_TRUE;
624 mutex_exit(&zilog->zl_lock);
627 mutex_exit(&zilog->zl_lock);
631 * Initialize the io for a log block.
633 * Note, we should not initialize the IO until we are about
634 * to use it, since zio_rewrite() does a spa_config_enter().
637 zil_lwb_write_init(zilog_t *zilog, lwb_t *lwb)
641 zb.zb_objset = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET];
644 zb.zb_blkid = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ];
646 if (zilog->zl_root_zio == NULL) {
647 zilog->zl_root_zio = zio_root(zilog->zl_spa, NULL, NULL,
650 if (lwb->lwb_zio == NULL) {
651 lwb->lwb_zio = zio_rewrite(zilog->zl_root_zio, zilog->zl_spa,
652 ZIO_CHECKSUM_ZILOG, 0, &lwb->lwb_blk, lwb->lwb_buf,
653 lwb->lwb_sz, zil_lwb_write_done, lwb,
654 ZIO_PRIORITY_LOG_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
659 * Start a log block write and advance to the next log block.
660 * Calls are serialized.
663 zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb)
666 zil_trailer_t *ztp = (zil_trailer_t *)(lwb->lwb_buf + lwb->lwb_sz) - 1;
667 spa_t *spa = zilog->zl_spa;
668 blkptr_t *bp = &ztp->zit_next_blk;
673 ASSERT(lwb->lwb_nused <= ZIL_BLK_DATA_SZ(lwb));
676 * Allocate the next block and save its address in this block
677 * before writing it in order to establish the log chain.
678 * Note that if the allocation of nlwb synced before we wrote
679 * the block that points at it (lwb), we'd leak it if we crashed.
680 * Therefore, we don't do txg_rele_to_sync() until zil_lwb_write_done().
682 txg = txg_hold_open(zilog->zl_dmu_pool, &lwb->lwb_txgh);
683 txg_rele_to_quiesce(&lwb->lwb_txgh);
686 * Pick a ZIL blocksize. We request a size that is the
687 * maximum of the previous used size, the current used size and
688 * the amount waiting in the queue.
690 zil_blksz = MAX(zilog->zl_prev_used,
691 zilog->zl_cur_used + sizeof (*ztp));
692 zil_blksz = MAX(zil_blksz, zilog->zl_itx_list_sz + sizeof (*ztp));
693 zil_blksz = P2ROUNDUP_TYPED(zil_blksz, ZIL_MIN_BLKSZ, uint64_t);
694 if (zil_blksz > ZIL_MAX_BLKSZ)
695 zil_blksz = ZIL_MAX_BLKSZ;
698 /* pass the old blkptr in order to spread log blocks across devs */
699 error = zio_alloc_blk(spa, zil_blksz, bp, &lwb->lwb_blk, txg);
701 dmu_tx_t *tx = dmu_tx_create_assigned(zilog->zl_dmu_pool, txg);
704 * We dirty the dataset to ensure that zil_sync() will
705 * be called to remove this lwb from our zl_lwb_list.
706 * Failing to do so, may leave an lwb with a NULL lwb_buf
707 * hanging around on the zl_lwb_list.
709 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
713 * Since we've just experienced an allocation failure so we
714 * terminate the current lwb and send it on its way.
717 ztp->zit_nused = lwb->lwb_nused;
718 ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum;
719 zio_nowait(lwb->lwb_zio);
722 * By returning NULL the caller will call tx_wait_synced()
727 ASSERT3U(bp->blk_birth, ==, txg);
729 ztp->zit_nused = lwb->lwb_nused;
730 ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum;
731 bp->blk_cksum = lwb->lwb_blk.blk_cksum;
732 bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++;
735 * Allocate a new log write buffer (lwb).
737 nlwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP);
739 nlwb->lwb_zilog = zilog;
742 nlwb->lwb_sz = BP_GET_LSIZE(&nlwb->lwb_blk);
743 nlwb->lwb_buf = zio_buf_alloc(nlwb->lwb_sz);
744 nlwb->lwb_max_txg = txg;
745 nlwb->lwb_zio = NULL;
748 * Put new lwb at the end of the log chain
750 mutex_enter(&zilog->zl_lock);
751 list_insert_tail(&zilog->zl_lwb_list, nlwb);
752 mutex_exit(&zilog->zl_lock);
754 /* Record the vdev for later flushing */
755 zil_add_vdev(zilog, DVA_GET_VDEV(BP_IDENTITY(&(lwb->lwb_blk))));
758 * kick off the write for the old log block
760 dprintf_bp(&lwb->lwb_blk, "lwb %p txg %llu: ", lwb, txg);
761 ASSERT(lwb->lwb_zio);
762 zio_nowait(lwb->lwb_zio);
768 zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb)
770 lr_t *lrc = &itx->itx_lr; /* common log record */
771 lr_write_t *lr = (lr_write_t *)lrc;
772 uint64_t txg = lrc->lrc_txg;
773 uint64_t reclen = lrc->lrc_reclen;
778 ASSERT(lwb->lwb_buf != NULL);
780 if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY)
781 dlen = P2ROUNDUP_TYPED(
782 lr->lr_length, sizeof (uint64_t), uint64_t);
786 zilog->zl_cur_used += (reclen + dlen);
788 zil_lwb_write_init(zilog, lwb);
791 * If this record won't fit in the current log block, start a new one.
793 if (lwb->lwb_nused + reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) {
794 lwb = zil_lwb_write_start(zilog, lwb);
797 zil_lwb_write_init(zilog, lwb);
798 ASSERT(lwb->lwb_nused == 0);
799 if (reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) {
800 txg_wait_synced(zilog->zl_dmu_pool, txg);
806 * Update the lrc_seq, to be log record sequence number. See zil.h
807 * Then copy the record to the log buffer.
809 lrc->lrc_seq = ++zilog->zl_lr_seq; /* we are single threaded */
810 bcopy(lrc, lwb->lwb_buf + lwb->lwb_nused, reclen);
813 * If it's a write, fetch the data or get its blkptr as appropriate.
815 if (lrc->lrc_txtype == TX_WRITE) {
816 if (txg > spa_freeze_txg(zilog->zl_spa))
817 txg_wait_synced(zilog->zl_dmu_pool, txg);
818 if (itx->itx_wr_state != WR_COPIED) {
822 /* alignment is guaranteed */
823 lr = (lr_write_t *)(lwb->lwb_buf + lwb->lwb_nused);
825 ASSERT(itx->itx_wr_state == WR_NEED_COPY);
826 dbuf = lwb->lwb_buf + lwb->lwb_nused + reclen;
827 lr->lr_common.lrc_reclen += dlen;
829 ASSERT(itx->itx_wr_state == WR_INDIRECT);
832 error = zilog->zl_get_data(
833 itx->itx_private, lr, dbuf, lwb->lwb_zio);
835 ASSERT(error == ENOENT || error == EEXIST ||
842 lwb->lwb_nused += reclen + dlen;
843 lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg);
844 ASSERT3U(lwb->lwb_nused, <=, ZIL_BLK_DATA_SZ(lwb));
845 ASSERT3U(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)), ==, 0);
851 zil_itx_create(int txtype, size_t lrsize)
855 lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t);
857 itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP);
858 itx->itx_lr.lrc_txtype = txtype;
859 itx->itx_lr.lrc_reclen = lrsize;
860 itx->itx_lr.lrc_seq = 0; /* defensive */
866 zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx)
870 ASSERT(itx->itx_lr.lrc_seq == 0);
872 mutex_enter(&zilog->zl_lock);
873 list_insert_tail(&zilog->zl_itx_list, itx);
874 zilog->zl_itx_list_sz += itx->itx_lr.lrc_reclen;
875 itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx);
876 itx->itx_lr.lrc_seq = seq = ++zilog->zl_itx_seq;
877 mutex_exit(&zilog->zl_lock);
883 * Free up all in-memory intent log transactions that have now been synced.
886 zil_itx_clean(zilog_t *zilog)
888 uint64_t synced_txg = spa_last_synced_txg(zilog->zl_spa);
889 uint64_t freeze_txg = spa_freeze_txg(zilog->zl_spa);
893 list_create(&clean_list, sizeof (itx_t), offsetof(itx_t, itx_node));
895 mutex_enter(&zilog->zl_lock);
896 /* wait for a log writer to finish walking list */
897 while (zilog->zl_writer) {
898 cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
902 * Move the sync'd log transactions to a separate list so we can call
903 * kmem_free without holding the zl_lock.
905 * There is no need to set zl_writer as we don't drop zl_lock here
907 while ((itx = list_head(&zilog->zl_itx_list)) != NULL &&
908 itx->itx_lr.lrc_txg <= MIN(synced_txg, freeze_txg)) {
909 list_remove(&zilog->zl_itx_list, itx);
910 zilog->zl_itx_list_sz -= itx->itx_lr.lrc_reclen;
911 list_insert_tail(&clean_list, itx);
913 cv_broadcast(&zilog->zl_cv_writer);
914 mutex_exit(&zilog->zl_lock);
916 /* destroy sync'd log transactions */
917 while ((itx = list_head(&clean_list)) != NULL) {
918 list_remove(&clean_list, itx);
919 kmem_free(itx, offsetof(itx_t, itx_lr)
920 + itx->itx_lr.lrc_reclen);
922 list_destroy(&clean_list);
926 * If there are any in-memory intent log transactions which have now been
927 * synced then start up a taskq to free them.
930 zil_clean(zilog_t *zilog)
934 mutex_enter(&zilog->zl_lock);
935 itx = list_head(&zilog->zl_itx_list);
937 (itx->itx_lr.lrc_txg <= spa_last_synced_txg(zilog->zl_spa))) {
938 (void) taskq_dispatch(zilog->zl_clean_taskq,
939 (void (*)(void *))zil_itx_clean, zilog, TQ_NOSLEEP);
941 mutex_exit(&zilog->zl_lock);
945 zil_commit_writer(zilog_t *zilog, uint64_t seq, uint64_t foid)
949 uint64_t commit_seq = 0;
950 itx_t *itx, *itx_next = (itx_t *)-1;
954 zilog->zl_writer = B_TRUE;
955 zilog->zl_root_zio = NULL;
958 if (zilog->zl_suspend) {
961 lwb = list_tail(&zilog->zl_lwb_list);
964 * Return if there's nothing to flush before we
965 * dirty the fs by calling zil_create()
967 if (list_is_empty(&zilog->zl_itx_list)) {
968 zilog->zl_writer = B_FALSE;
971 mutex_exit(&zilog->zl_lock);
973 mutex_enter(&zilog->zl_lock);
974 lwb = list_tail(&zilog->zl_lwb_list);
978 /* Loop through in-memory log transactions filling log blocks. */
979 DTRACE_PROBE1(zil__cw1, zilog_t *, zilog);
982 * Find the next itx to push:
983 * Push all transactions related to specified foid and all
984 * other transactions except TX_WRITE, TX_TRUNCATE,
985 * TX_SETATTR and TX_ACL for all other files.
987 if (itx_next != (itx_t *)-1)
990 itx = list_head(&zilog->zl_itx_list);
991 for (; itx != NULL; itx = list_next(&zilog->zl_itx_list, itx)) {
992 if (foid == 0) /* push all foids? */
994 if (itx->itx_sync) /* push all O_[D]SYNC */
996 switch (itx->itx_lr.lrc_txtype) {
1001 /* lr_foid is same offset for these records */
1002 if (((lr_write_t *)&itx->itx_lr)->lr_foid
1004 continue; /* skip this record */
1012 reclen = itx->itx_lr.lrc_reclen;
1013 if ((itx->itx_lr.lrc_seq > seq) &&
1014 ((lwb == NULL) || (lwb->lwb_nused == 0) ||
1015 (lwb->lwb_nused + reclen > ZIL_BLK_DATA_SZ(lwb)))) {
1020 * Save the next pointer. Even though we soon drop
1021 * zl_lock all threads that may change the list
1022 * (another writer or zil_itx_clean) can't do so until
1023 * they have zl_writer.
1025 itx_next = list_next(&zilog->zl_itx_list, itx);
1026 list_remove(&zilog->zl_itx_list, itx);
1027 mutex_exit(&zilog->zl_lock);
1028 txg = itx->itx_lr.lrc_txg;
1031 if (txg > spa_last_synced_txg(spa) ||
1032 txg > spa_freeze_txg(spa))
1033 lwb = zil_lwb_commit(zilog, itx, lwb);
1034 kmem_free(itx, offsetof(itx_t, itx_lr)
1035 + itx->itx_lr.lrc_reclen);
1036 mutex_enter(&zilog->zl_lock);
1037 zilog->zl_itx_list_sz -= reclen;
1039 DTRACE_PROBE1(zil__cw2, zilog_t *, zilog);
1040 /* determine commit sequence number */
1041 itx = list_head(&zilog->zl_itx_list);
1043 commit_seq = itx->itx_lr.lrc_seq;
1045 commit_seq = zilog->zl_itx_seq;
1046 mutex_exit(&zilog->zl_lock);
1048 /* write the last block out */
1049 if (lwb != NULL && lwb->lwb_zio != NULL)
1050 lwb = zil_lwb_write_start(zilog, lwb);
1052 zilog->zl_prev_used = zilog->zl_cur_used;
1053 zilog->zl_cur_used = 0;
1056 * Wait if necessary for the log blocks to be on stable storage.
1058 if (zilog->zl_root_zio) {
1059 DTRACE_PROBE1(zil__cw3, zilog_t *, zilog);
1060 (void) zio_wait(zilog->zl_root_zio);
1061 DTRACE_PROBE1(zil__cw4, zilog_t *, zilog);
1062 if (!zfs_nocacheflush)
1063 zil_flush_vdevs(zilog);
1066 if (zilog->zl_log_error || lwb == NULL) {
1067 zilog->zl_log_error = 0;
1068 txg_wait_synced(zilog->zl_dmu_pool, 0);
1071 mutex_enter(&zilog->zl_lock);
1072 zilog->zl_writer = B_FALSE;
1074 ASSERT3U(commit_seq, >=, zilog->zl_commit_seq);
1075 zilog->zl_commit_seq = commit_seq;
1079 * Push zfs transactions to stable storage up to the supplied sequence number.
1080 * If foid is 0 push out all transactions, otherwise push only those
1081 * for that file or might have been used to create that file.
1084 zil_commit(zilog_t *zilog, uint64_t seq, uint64_t foid)
1086 if (zilog == NULL || seq == 0)
1089 mutex_enter(&zilog->zl_lock);
1091 seq = MIN(seq, zilog->zl_itx_seq); /* cap seq at largest itx seq */
1093 while (zilog->zl_writer) {
1094 cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
1095 if (seq < zilog->zl_commit_seq) {
1096 mutex_exit(&zilog->zl_lock);
1100 zil_commit_writer(zilog, seq, foid); /* drops zl_lock */
1101 /* wake up others waiting on the commit */
1102 cv_broadcast(&zilog->zl_cv_writer);
1103 mutex_exit(&zilog->zl_lock);
1107 * Called in syncing context to free committed log blocks and update log header.
1110 zil_sync(zilog_t *zilog, dmu_tx_t *tx)
1112 zil_header_t *zh = zil_header_in_syncing_context(zilog);
1113 uint64_t txg = dmu_tx_get_txg(tx);
1114 spa_t *spa = zilog->zl_spa;
1117 mutex_enter(&zilog->zl_lock);
1119 ASSERT(zilog->zl_stop_sync == 0);
1121 zh->zh_replay_seq = zilog->zl_replay_seq[txg & TXG_MASK];
1123 if (zilog->zl_destroy_txg == txg) {
1124 blkptr_t blk = zh->zh_log;
1126 ASSERT(list_head(&zilog->zl_lwb_list) == NULL);
1127 ASSERT(spa_sync_pass(spa) == 1);
1129 bzero(zh, sizeof (zil_header_t));
1130 bzero(zilog->zl_replay_seq, sizeof (zilog->zl_replay_seq));
1132 if (zilog->zl_keep_first) {
1134 * If this block was part of log chain that couldn't
1135 * be claimed because a device was missing during
1136 * zil_claim(), but that device later returns,
1137 * then this block could erroneously appear valid.
1138 * To guard against this, assign a new GUID to the new
1139 * log chain so it doesn't matter what blk points to.
1141 zil_init_log_chain(zilog, &blk);
1147 lwb = list_head(&zilog->zl_lwb_list);
1149 mutex_exit(&zilog->zl_lock);
1152 zh->zh_log = lwb->lwb_blk;
1153 if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg)
1155 list_remove(&zilog->zl_lwb_list, lwb);
1156 zio_free_blk(spa, &lwb->lwb_blk, txg);
1157 kmem_cache_free(zil_lwb_cache, lwb);
1160 * If we don't have anything left in the lwb list then
1161 * we've had an allocation failure and we need to zero
1162 * out the zil_header blkptr so that we don't end
1163 * up freeing the same block twice.
1165 if (list_head(&zilog->zl_lwb_list) == NULL)
1166 BP_ZERO(&zh->zh_log);
1168 mutex_exit(&zilog->zl_lock);
1174 zil_lwb_cache = kmem_cache_create("zil_lwb_cache",
1175 sizeof (struct lwb), 0, NULL, NULL, NULL, NULL, NULL, 0);
1181 kmem_cache_destroy(zil_lwb_cache);
1185 zil_alloc(objset_t *os, zil_header_t *zh_phys)
1189 zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP);
1191 zilog->zl_header = zh_phys;
1193 zilog->zl_spa = dmu_objset_spa(os);
1194 zilog->zl_dmu_pool = dmu_objset_pool(os);
1195 zilog->zl_destroy_txg = TXG_INITIAL - 1;
1197 mutex_init(&zilog->zl_lock, NULL, MUTEX_DEFAULT, NULL);
1198 cv_init(&zilog->zl_cv_writer, NULL, CV_DEFAULT, NULL);
1199 cv_init(&zilog->zl_cv_suspend, NULL, CV_DEFAULT, NULL);
1201 list_create(&zilog->zl_itx_list, sizeof (itx_t),
1202 offsetof(itx_t, itx_node));
1204 list_create(&zilog->zl_lwb_list, sizeof (lwb_t),
1205 offsetof(lwb_t, lwb_node));
1207 list_create(&zilog->zl_vdev_list, sizeof (zil_vdev_t),
1208 offsetof(zil_vdev_t, vdev_seq_node));
1214 zil_free(zilog_t *zilog)
1219 zilog->zl_stop_sync = 1;
1221 while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
1222 list_remove(&zilog->zl_lwb_list, lwb);
1223 if (lwb->lwb_buf != NULL)
1224 zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
1225 kmem_cache_free(zil_lwb_cache, lwb);
1227 list_destroy(&zilog->zl_lwb_list);
1229 while ((zv = list_head(&zilog->zl_vdev_list)) != NULL) {
1230 list_remove(&zilog->zl_vdev_list, zv);
1231 kmem_free(zv, sizeof (zil_vdev_t));
1233 list_destroy(&zilog->zl_vdev_list);
1235 ASSERT(list_head(&zilog->zl_itx_list) == NULL);
1236 list_destroy(&zilog->zl_itx_list);
1237 cv_destroy(&zilog->zl_cv_suspend);
1238 cv_destroy(&zilog->zl_cv_writer);
1239 mutex_destroy(&zilog->zl_lock);
1241 kmem_free(zilog, sizeof (zilog_t));
1245 * return true if the initial log block is not valid
1248 zil_empty(zilog_t *zilog)
1250 const zil_header_t *zh = zilog->zl_header;
1251 arc_buf_t *abuf = NULL;
1253 if (BP_IS_HOLE(&zh->zh_log))
1256 if (zil_read_log_block(zilog, &zh->zh_log, &abuf) != 0)
1259 VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
1264 * Open an intent log.
1267 zil_open(objset_t *os, zil_get_data_t *get_data)
1269 zilog_t *zilog = dmu_objset_zil(os);
1271 zilog->zl_get_data = get_data;
1272 zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri,
1273 2, 2, TASKQ_PREPOPULATE);
1279 * Close an intent log.
1282 zil_close(zilog_t *zilog)
1285 * If the log isn't already committed, mark the objset dirty
1286 * (so zil_sync() will be called) and wait for that txg to sync.
1288 if (!zil_is_committed(zilog)) {
1290 dmu_tx_t *tx = dmu_tx_create(zilog->zl_os);
1291 (void) dmu_tx_assign(tx, TXG_WAIT);
1292 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
1293 txg = dmu_tx_get_txg(tx);
1295 txg_wait_synced(zilog->zl_dmu_pool, txg);
1298 taskq_destroy(zilog->zl_clean_taskq);
1299 zilog->zl_clean_taskq = NULL;
1300 zilog->zl_get_data = NULL;
1302 zil_itx_clean(zilog);
1303 ASSERT(list_head(&zilog->zl_itx_list) == NULL);
1307 * Suspend an intent log. While in suspended mode, we still honor
1308 * synchronous semantics, but we rely on txg_wait_synced() to do it.
1309 * We suspend the log briefly when taking a snapshot so that the snapshot
1310 * contains all the data it's supposed to, and has an empty intent log.
1313 zil_suspend(zilog_t *zilog)
1315 const zil_header_t *zh = zilog->zl_header;
1317 mutex_enter(&zilog->zl_lock);
1318 if (zh->zh_claim_txg != 0) { /* unplayed log */
1319 mutex_exit(&zilog->zl_lock);
1322 if (zilog->zl_suspend++ != 0) {
1324 * Someone else already began a suspend.
1325 * Just wait for them to finish.
1327 while (zilog->zl_suspending)
1328 cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock);
1329 ASSERT(BP_IS_HOLE(&zh->zh_log));
1330 mutex_exit(&zilog->zl_lock);
1333 zilog->zl_suspending = B_TRUE;
1334 mutex_exit(&zilog->zl_lock);
1336 zil_commit(zilog, UINT64_MAX, 0);
1339 * Wait for any in-flight log writes to complete.
1341 mutex_enter(&zilog->zl_lock);
1342 while (zilog->zl_writer)
1343 cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
1344 mutex_exit(&zilog->zl_lock);
1346 zil_destroy(zilog, B_FALSE);
1348 mutex_enter(&zilog->zl_lock);
1349 ASSERT(BP_IS_HOLE(&zh->zh_log));
1350 zilog->zl_suspending = B_FALSE;
1351 cv_broadcast(&zilog->zl_cv_suspend);
1352 mutex_exit(&zilog->zl_lock);
1358 zil_resume(zilog_t *zilog)
1360 mutex_enter(&zilog->zl_lock);
1361 ASSERT(zilog->zl_suspend != 0);
1362 zilog->zl_suspend--;
1363 mutex_exit(&zilog->zl_lock);
1366 typedef struct zil_replay_arg {
1368 zil_replay_func_t **zr_replay;
1371 boolean_t zr_byteswap;
1376 zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg)
1378 zil_replay_arg_t *zr = zra;
1379 const zil_header_t *zh = zilog->zl_header;
1380 uint64_t reclen = lr->lrc_reclen;
1381 uint64_t txtype = lr->lrc_txtype;
1383 int pass, error, sunk;
1385 if (zilog->zl_stop_replay)
1388 if (lr->lrc_txg < claim_txg) /* already committed */
1391 if (lr->lrc_seq <= zh->zh_replay_seq) /* already replayed */
1395 * Make a copy of the data so we can revise and extend it.
1397 bcopy(lr, zr->zr_lrbuf, reclen);
1400 * The log block containing this lr may have been byteswapped
1401 * so that we can easily examine common fields like lrc_txtype.
1402 * However, the log is a mix of different data types, and only the
1403 * replay vectors know how to byteswap their records. Therefore, if
1404 * the lr was byteswapped, undo it before invoking the replay vector.
1406 if (zr->zr_byteswap)
1407 byteswap_uint64_array(zr->zr_lrbuf, reclen);
1410 * If this is a TX_WRITE with a blkptr, suck in the data.
1412 if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) {
1413 lr_write_t *lrw = (lr_write_t *)lr;
1414 blkptr_t *wbp = &lrw->lr_blkptr;
1415 uint64_t wlen = lrw->lr_length;
1416 char *wbuf = zr->zr_lrbuf + reclen;
1418 if (BP_IS_HOLE(wbp)) { /* compressed to a hole */
1422 * A subsequent write may have overwritten this block,
1423 * in which case wbp may have been been freed and
1424 * reallocated, and our read of wbp may fail with a
1425 * checksum error. We can safely ignore this because
1426 * the later write will provide the correct data.
1430 zb.zb_objset = dmu_objset_id(zilog->zl_os);
1431 zb.zb_object = lrw->lr_foid;
1433 zb.zb_blkid = lrw->lr_offset / BP_GET_LSIZE(wbp);
1435 (void) zio_wait(zio_read(NULL, zilog->zl_spa,
1436 wbp, wbuf, BP_GET_LSIZE(wbp), NULL, NULL,
1437 ZIO_PRIORITY_SYNC_READ,
1438 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &zb));
1439 (void) memmove(wbuf, wbuf + lrw->lr_blkoff, wlen);
1444 * We must now do two things atomically: replay this log record,
1445 * and update the log header to reflect the fact that we did so.
1446 * We use the DMU's ability to assign into a specific txg to do this.
1448 for (pass = 1, sunk = B_FALSE; /* CONSTANTCONDITION */; pass++) {
1449 uint64_t replay_txg;
1450 dmu_tx_t *replay_tx;
1452 replay_tx = dmu_tx_create(zr->zr_os);
1453 error = dmu_tx_assign(replay_tx, TXG_WAIT);
1455 dmu_tx_abort(replay_tx);
1459 replay_txg = dmu_tx_get_txg(replay_tx);
1461 if (txtype == 0 || txtype >= TX_MAX_TYPE) {
1465 * On the first pass, arrange for the replay vector
1466 * to fail its dmu_tx_assign(). That's the only way
1467 * to ensure that those code paths remain well tested.
1469 *zr->zr_txgp = replay_txg - (pass == 1);
1470 error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lrbuf,
1472 *zr->zr_txgp = TXG_NOWAIT;
1476 dsl_dataset_dirty(dmu_objset_ds(zr->zr_os), replay_tx);
1477 zilog->zl_replay_seq[replay_txg & TXG_MASK] =
1481 dmu_tx_commit(replay_tx);
1487 * The DMU's dnode layer doesn't see removes until the txg
1488 * commits, so a subsequent claim can spuriously fail with
1489 * EEXIST. So if we receive any error other than ERESTART
1490 * we try syncing out any removes then retrying the
1493 if (error != ERESTART && !sunk) {
1494 txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0);
1496 continue; /* retry */
1499 if (error != ERESTART)
1503 txg_wait_open(spa_get_dsl(zilog->zl_spa),
1506 dprintf("pass %d, retrying\n", pass);
1509 ASSERT(error && error != ERESTART);
1510 name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1511 dmu_objset_name(zr->zr_os, name);
1512 cmn_err(CE_WARN, "ZFS replay transaction error %d, "
1513 "dataset %s, seq 0x%llx, txtype %llu\n",
1514 error, name, (u_longlong_t)lr->lrc_seq, (u_longlong_t)txtype);
1515 zilog->zl_stop_replay = 1;
1516 kmem_free(name, MAXNAMELEN);
1521 zil_incr_blks(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
1523 zilog->zl_replay_blks++;
1527 * If this dataset has a non-empty intent log, replay it and destroy it.
1530 zil_replay(objset_t *os, void *arg, uint64_t *txgp,
1531 zil_replay_func_t *replay_func[TX_MAX_TYPE])
1533 zilog_t *zilog = dmu_objset_zil(os);
1534 const zil_header_t *zh = zilog->zl_header;
1535 zil_replay_arg_t zr;
1537 if (zil_empty(zilog)) {
1538 zil_destroy(zilog, B_TRUE);
1541 //printf("ZFS: Replaying ZIL on %s...\n", os->os->os_spa->spa_name);
1544 zr.zr_replay = replay_func;
1547 zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log);
1548 zr.zr_lrbuf = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP);
1551 * Wait for in-progress removes to sync before starting replay.
1553 txg_wait_synced(zilog->zl_dmu_pool, 0);
1555 zilog->zl_stop_replay = 0;
1556 zilog->zl_replay_time = LBOLT;
1557 ASSERT(zilog->zl_replay_blks == 0);
1558 (void) zil_parse(zilog, zil_incr_blks, zil_replay_log_record, &zr,
1560 kmem_free(zr.zr_lrbuf, 2 * SPA_MAXBLOCKSIZE);
1562 zil_destroy(zilog, B_FALSE);
1563 //printf("ZFS: Replay of ZIL on %s finished.\n", os->os->os_spa->spa_name);
1567 * Report whether all transactions are committed
1570 zil_is_committed(zilog_t *zilog)
1575 mutex_enter(&zilog->zl_lock);
1576 while (zilog->zl_writer)
1577 cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
1579 /* recent unpushed intent log transactions? */
1580 if (!list_is_empty(&zilog->zl_itx_list)) {
1585 /* intent log never used? */
1586 lwb = list_head(&zilog->zl_lwb_list);
1593 * more than 1 log buffer means zil_sync() hasn't yet freed
1594 * entries after a txg has committed
1596 if (list_next(&zilog->zl_lwb_list, lwb)) {
1601 ASSERT(zil_empty(zilog));
1604 cv_broadcast(&zilog->zl_cv_writer);
1605 mutex_exit(&zilog->zl_lock);