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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2011 by Delphix. All rights reserved.
26 /* Portions Copyright 2010 Robert Milkowski */
28 #include <sys/zfs_context.h>
34 #include <sys/resource.h>
36 #include <sys/zil_impl.h>
37 #include <sys/dsl_dataset.h>
38 #include <sys/vdev_impl.h>
39 #include <sys/dmu_tx.h>
40 #include <sys/dsl_pool.h>
43 * The zfs intent log (ZIL) saves transaction records of system calls
44 * that change the file system in memory with enough information
45 * to be able to replay them. These are stored in memory until
46 * either the DMU transaction group (txg) commits them to the stable pool
47 * and they can be discarded, or they are flushed to the stable log
48 * (also in the pool) due to a fsync, O_DSYNC or other synchronous
49 * requirement. In the event of a panic or power fail then those log
50 * records (transactions) are replayed.
52 * There is one ZIL per file system. Its on-disk (pool) format consists
59 * A log record holds a system call transaction. Log blocks can
60 * hold many log records and the blocks are chained together.
61 * Each ZIL block contains a block pointer (blkptr_t) to the next
62 * ZIL block in the chain. The ZIL header points to the first
63 * block in the chain. Note there is not a fixed place in the pool
64 * to hold blocks. They are dynamically allocated and freed as
65 * needed from the blocks available. Figure X shows the ZIL structure:
69 * See zil.h for more information about these fields.
71 zil_stats_t zil_stats = {
72 { "zil_commit_count", KSTAT_DATA_UINT64 },
73 { "zil_commit_writer_count", KSTAT_DATA_UINT64 },
74 { "zil_itx_count", KSTAT_DATA_UINT64 },
75 { "zil_itx_indirect_count", KSTAT_DATA_UINT64 },
76 { "zil_itx_indirect_bytes", KSTAT_DATA_UINT64 },
77 { "zil_itx_copied_count", KSTAT_DATA_UINT64 },
78 { "zil_itx_copied_bytes", KSTAT_DATA_UINT64 },
79 { "zil_itx_needcopy_count", KSTAT_DATA_UINT64 },
80 { "zil_itx_needcopy_bytes", KSTAT_DATA_UINT64 },
81 { "zil_itx_metaslab_normal_count", KSTAT_DATA_UINT64 },
82 { "zil_itx_metaslab_normal_bytes", KSTAT_DATA_UINT64 },
83 { "zil_itx_metaslab_slog_count", KSTAT_DATA_UINT64 },
84 { "zil_itx_metaslab_slog_bytes", KSTAT_DATA_UINT64 },
87 static kstat_t *zil_ksp;
90 * This global ZIL switch affects all pools
92 int zil_replay_disable = 0; /* disable intent logging replay */
95 * Tunable parameter for debugging or performance analysis. Setting
96 * zfs_nocacheflush will cause corruption on power loss if a volatile
97 * out-of-order write cache is enabled.
99 int zfs_nocacheflush = 0;
101 static kmem_cache_t *zil_lwb_cache;
103 static void zil_async_to_sync(zilog_t *zilog, uint64_t foid);
105 #define LWB_EMPTY(lwb) ((BP_GET_LSIZE(&lwb->lwb_blk) - \
106 sizeof (zil_chain_t)) == (lwb->lwb_sz - lwb->lwb_nused))
110 * ziltest is by and large an ugly hack, but very useful in
111 * checking replay without tedious work.
112 * When running ziltest we want to keep all itx's and so maintain
113 * a single list in the zl_itxg[] that uses a high txg: ZILTEST_TXG
114 * We subtract TXG_CONCURRENT_STATES to allow for common code.
116 #define ZILTEST_TXG (UINT64_MAX - TXG_CONCURRENT_STATES)
119 zil_bp_compare(const void *x1, const void *x2)
121 const dva_t *dva1 = &((zil_bp_node_t *)x1)->zn_dva;
122 const dva_t *dva2 = &((zil_bp_node_t *)x2)->zn_dva;
124 if (DVA_GET_VDEV(dva1) < DVA_GET_VDEV(dva2))
126 if (DVA_GET_VDEV(dva1) > DVA_GET_VDEV(dva2))
129 if (DVA_GET_OFFSET(dva1) < DVA_GET_OFFSET(dva2))
131 if (DVA_GET_OFFSET(dva1) > DVA_GET_OFFSET(dva2))
138 zil_bp_tree_init(zilog_t *zilog)
140 avl_create(&zilog->zl_bp_tree, zil_bp_compare,
141 sizeof (zil_bp_node_t), offsetof(zil_bp_node_t, zn_node));
145 zil_bp_tree_fini(zilog_t *zilog)
147 avl_tree_t *t = &zilog->zl_bp_tree;
151 while ((zn = avl_destroy_nodes(t, &cookie)) != NULL)
152 kmem_free(zn, sizeof (zil_bp_node_t));
158 zil_bp_tree_add(zilog_t *zilog, const blkptr_t *bp)
160 avl_tree_t *t = &zilog->zl_bp_tree;
161 const dva_t *dva = BP_IDENTITY(bp);
165 if (avl_find(t, dva, &where) != NULL)
168 zn = kmem_alloc(sizeof (zil_bp_node_t), KM_SLEEP);
170 avl_insert(t, zn, where);
175 static zil_header_t *
176 zil_header_in_syncing_context(zilog_t *zilog)
178 return ((zil_header_t *)zilog->zl_header);
182 zil_init_log_chain(zilog_t *zilog, blkptr_t *bp)
184 zio_cksum_t *zc = &bp->blk_cksum;
186 zc->zc_word[ZIL_ZC_GUID_0] = spa_get_random(-1ULL);
187 zc->zc_word[ZIL_ZC_GUID_1] = spa_get_random(-1ULL);
188 zc->zc_word[ZIL_ZC_OBJSET] = dmu_objset_id(zilog->zl_os);
189 zc->zc_word[ZIL_ZC_SEQ] = 1ULL;
193 * Read a log block and make sure it's valid.
196 zil_read_log_block(zilog_t *zilog, const blkptr_t *bp, blkptr_t *nbp, void *dst,
199 enum zio_flag zio_flags = ZIO_FLAG_CANFAIL;
200 uint32_t aflags = ARC_WAIT;
201 arc_buf_t *abuf = NULL;
205 if (zilog->zl_header->zh_claim_txg == 0)
206 zio_flags |= ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB;
208 if (!(zilog->zl_header->zh_flags & ZIL_CLAIM_LR_SEQ_VALID))
209 zio_flags |= ZIO_FLAG_SPECULATIVE;
211 SET_BOOKMARK(&zb, bp->blk_cksum.zc_word[ZIL_ZC_OBJSET],
212 ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, bp->blk_cksum.zc_word[ZIL_ZC_SEQ]);
214 error = dsl_read_nolock(NULL, zilog->zl_spa, bp, arc_getbuf_func, &abuf,
215 ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb);
218 zio_cksum_t cksum = bp->blk_cksum;
221 * Validate the checksummed log block.
223 * Sequence numbers should be... sequential. The checksum
224 * verifier for the next block should be bp's checksum plus 1.
226 * Also check the log chain linkage and size used.
228 cksum.zc_word[ZIL_ZC_SEQ]++;
230 if (BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_ZILOG2) {
231 zil_chain_t *zilc = abuf->b_data;
232 char *lr = (char *)(zilc + 1);
233 uint64_t len = zilc->zc_nused - sizeof (zil_chain_t);
235 if (bcmp(&cksum, &zilc->zc_next_blk.blk_cksum,
236 sizeof (cksum)) || BP_IS_HOLE(&zilc->zc_next_blk)) {
240 *end = (char *)dst + len;
241 *nbp = zilc->zc_next_blk;
244 char *lr = abuf->b_data;
245 uint64_t size = BP_GET_LSIZE(bp);
246 zil_chain_t *zilc = (zil_chain_t *)(lr + size) - 1;
248 if (bcmp(&cksum, &zilc->zc_next_blk.blk_cksum,
249 sizeof (cksum)) || BP_IS_HOLE(&zilc->zc_next_blk) ||
250 (zilc->zc_nused > (size - sizeof (*zilc)))) {
253 bcopy(lr, dst, zilc->zc_nused);
254 *end = (char *)dst + zilc->zc_nused;
255 *nbp = zilc->zc_next_blk;
259 VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
266 * Read a TX_WRITE log data block.
269 zil_read_log_data(zilog_t *zilog, const lr_write_t *lr, void *wbuf)
271 enum zio_flag zio_flags = ZIO_FLAG_CANFAIL;
272 const blkptr_t *bp = &lr->lr_blkptr;
273 uint32_t aflags = ARC_WAIT;
274 arc_buf_t *abuf = NULL;
278 if (BP_IS_HOLE(bp)) {
280 bzero(wbuf, MAX(BP_GET_LSIZE(bp), lr->lr_length));
284 if (zilog->zl_header->zh_claim_txg == 0)
285 zio_flags |= ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB;
287 SET_BOOKMARK(&zb, dmu_objset_id(zilog->zl_os), lr->lr_foid,
288 ZB_ZIL_LEVEL, lr->lr_offset / BP_GET_LSIZE(bp));
290 error = arc_read_nolock(NULL, zilog->zl_spa, bp, arc_getbuf_func, &abuf,
291 ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb);
295 bcopy(abuf->b_data, wbuf, arc_buf_size(abuf));
296 (void) arc_buf_remove_ref(abuf, &abuf);
303 * Parse the intent log, and call parse_func for each valid record within.
306 zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func,
307 zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg)
309 const zil_header_t *zh = zilog->zl_header;
310 boolean_t claimed = !!zh->zh_claim_txg;
311 uint64_t claim_blk_seq = claimed ? zh->zh_claim_blk_seq : UINT64_MAX;
312 uint64_t claim_lr_seq = claimed ? zh->zh_claim_lr_seq : UINT64_MAX;
313 uint64_t max_blk_seq = 0;
314 uint64_t max_lr_seq = 0;
315 uint64_t blk_count = 0;
316 uint64_t lr_count = 0;
317 blkptr_t blk, next_blk;
321 bzero(&next_blk, sizeof(blkptr_t));
324 * Old logs didn't record the maximum zh_claim_lr_seq.
326 if (!(zh->zh_flags & ZIL_CLAIM_LR_SEQ_VALID))
327 claim_lr_seq = UINT64_MAX;
330 * Starting at the block pointed to by zh_log we read the log chain.
331 * For each block in the chain we strongly check that block to
332 * ensure its validity. We stop when an invalid block is found.
333 * For each block pointer in the chain we call parse_blk_func().
334 * For each record in each valid block we call parse_lr_func().
335 * If the log has been claimed, stop if we encounter a sequence
336 * number greater than the highest claimed sequence number.
338 lrbuf = zio_buf_alloc(SPA_MAXBLOCKSIZE);
339 zil_bp_tree_init(zilog);
341 for (blk = zh->zh_log; !BP_IS_HOLE(&blk); blk = next_blk) {
342 uint64_t blk_seq = blk.blk_cksum.zc_word[ZIL_ZC_SEQ];
346 if (blk_seq > claim_blk_seq)
348 if ((error = parse_blk_func(zilog, &blk, arg, txg)) != 0)
350 ASSERT3U(max_blk_seq, <, blk_seq);
351 max_blk_seq = blk_seq;
354 if (max_lr_seq == claim_lr_seq && max_blk_seq == claim_blk_seq)
357 error = zil_read_log_block(zilog, &blk, &next_blk, lrbuf, &end);
361 for (lrp = lrbuf; lrp < end; lrp += reclen) {
362 lr_t *lr = (lr_t *)lrp;
363 reclen = lr->lrc_reclen;
364 ASSERT3U(reclen, >=, sizeof (lr_t));
365 if (lr->lrc_seq > claim_lr_seq)
367 if ((error = parse_lr_func(zilog, lr, arg, txg)) != 0)
369 ASSERT3U(max_lr_seq, <, lr->lrc_seq);
370 max_lr_seq = lr->lrc_seq;
375 zilog->zl_parse_error = error;
376 zilog->zl_parse_blk_seq = max_blk_seq;
377 zilog->zl_parse_lr_seq = max_lr_seq;
378 zilog->zl_parse_blk_count = blk_count;
379 zilog->zl_parse_lr_count = lr_count;
381 ASSERT(!claimed || !(zh->zh_flags & ZIL_CLAIM_LR_SEQ_VALID) ||
382 (max_blk_seq == claim_blk_seq && max_lr_seq == claim_lr_seq));
384 zil_bp_tree_fini(zilog);
385 zio_buf_free(lrbuf, SPA_MAXBLOCKSIZE);
391 zil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg)
394 * Claim log block if not already committed and not already claimed.
395 * If tx == NULL, just verify that the block is claimable.
397 if (bp->blk_birth < first_txg || zil_bp_tree_add(zilog, bp) != 0)
400 return (zio_wait(zio_claim(NULL, zilog->zl_spa,
401 tx == NULL ? 0 : first_txg, bp, spa_claim_notify, NULL,
402 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB)));
406 zil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg)
408 lr_write_t *lr = (lr_write_t *)lrc;
411 if (lrc->lrc_txtype != TX_WRITE)
415 * If the block is not readable, don't claim it. This can happen
416 * in normal operation when a log block is written to disk before
417 * some of the dmu_sync() blocks it points to. In this case, the
418 * transaction cannot have been committed to anyone (we would have
419 * waited for all writes to be stable first), so it is semantically
420 * correct to declare this the end of the log.
422 if (lr->lr_blkptr.blk_birth >= first_txg &&
423 (error = zil_read_log_data(zilog, lr, NULL)) != 0)
425 return (zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg));
430 zil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg)
432 zio_free_zil(zilog->zl_spa, dmu_tx_get_txg(tx), bp);
438 zil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg)
440 lr_write_t *lr = (lr_write_t *)lrc;
441 blkptr_t *bp = &lr->lr_blkptr;
444 * If we previously claimed it, we need to free it.
446 if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE &&
447 bp->blk_birth >= claim_txg && zil_bp_tree_add(zilog, bp) == 0)
448 zio_free(zilog->zl_spa, dmu_tx_get_txg(tx), bp);
454 zil_alloc_lwb(zilog_t *zilog, blkptr_t *bp, uint64_t txg)
458 lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP);
459 lwb->lwb_zilog = zilog;
461 lwb->lwb_buf = zio_buf_alloc(BP_GET_LSIZE(bp));
462 lwb->lwb_max_txg = txg;
465 if (BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_ZILOG2) {
466 lwb->lwb_nused = sizeof (zil_chain_t);
467 lwb->lwb_sz = BP_GET_LSIZE(bp);
470 lwb->lwb_sz = BP_GET_LSIZE(bp) - sizeof (zil_chain_t);
473 mutex_enter(&zilog->zl_lock);
474 list_insert_tail(&zilog->zl_lwb_list, lwb);
475 mutex_exit(&zilog->zl_lock);
481 * Create an on-disk intent log.
484 zil_create(zilog_t *zilog)
486 const zil_header_t *zh = zilog->zl_header;
494 * Wait for any previous destroy to complete.
496 txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
498 ASSERT(zh->zh_claim_txg == 0);
499 ASSERT(zh->zh_replay_seq == 0);
504 * Allocate an initial log block if:
505 * - there isn't one already
506 * - the existing block is the wrong endianess
508 if (BP_IS_HOLE(&blk) || BP_SHOULD_BYTESWAP(&blk)) {
509 tx = dmu_tx_create(zilog->zl_os);
510 VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0);
511 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
512 txg = dmu_tx_get_txg(tx);
514 if (!BP_IS_HOLE(&blk)) {
515 zio_free_zil(zilog->zl_spa, txg, &blk);
519 error = zio_alloc_zil(zilog->zl_spa, txg, &blk, NULL,
520 ZIL_MIN_BLKSZ, zilog->zl_logbias == ZFS_LOGBIAS_LATENCY);
523 zil_init_log_chain(zilog, &blk);
527 * Allocate a log write buffer (lwb) for the first log block.
530 lwb = zil_alloc_lwb(zilog, &blk, txg);
533 * If we just allocated the first log block, commit our transaction
534 * and wait for zil_sync() to stuff the block poiner into zh_log.
535 * (zh is part of the MOS, so we cannot modify it in open context.)
539 txg_wait_synced(zilog->zl_dmu_pool, txg);
542 ASSERT(bcmp(&blk, &zh->zh_log, sizeof (blk)) == 0);
548 * In one tx, free all log blocks and clear the log header.
549 * If keep_first is set, then we're replaying a log with no content.
550 * We want to keep the first block, however, so that the first
551 * synchronous transaction doesn't require a txg_wait_synced()
552 * in zil_create(). We don't need to txg_wait_synced() here either
553 * when keep_first is set, because both zil_create() and zil_destroy()
554 * will wait for any in-progress destroys to complete.
557 zil_destroy(zilog_t *zilog, boolean_t keep_first)
559 const zil_header_t *zh = zilog->zl_header;
565 * Wait for any previous destroy to complete.
567 txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
569 zilog->zl_old_header = *zh; /* debugging aid */
571 if (BP_IS_HOLE(&zh->zh_log))
574 tx = dmu_tx_create(zilog->zl_os);
575 VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0);
576 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
577 txg = dmu_tx_get_txg(tx);
579 mutex_enter(&zilog->zl_lock);
581 ASSERT3U(zilog->zl_destroy_txg, <, txg);
582 zilog->zl_destroy_txg = txg;
583 zilog->zl_keep_first = keep_first;
585 if (!list_is_empty(&zilog->zl_lwb_list)) {
586 ASSERT(zh->zh_claim_txg == 0);
588 while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
589 list_remove(&zilog->zl_lwb_list, lwb);
590 if (lwb->lwb_buf != NULL)
591 zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
592 zio_free_zil(zilog->zl_spa, txg, &lwb->lwb_blk);
593 kmem_cache_free(zil_lwb_cache, lwb);
595 } else if (!keep_first) {
596 (void) zil_parse(zilog, zil_free_log_block,
597 zil_free_log_record, tx, zh->zh_claim_txg);
599 mutex_exit(&zilog->zl_lock);
605 zil_claim(const char *osname, void *txarg)
607 dmu_tx_t *tx = txarg;
608 uint64_t first_txg = dmu_tx_get_txg(tx);
614 error = dmu_objset_hold(osname, FTAG, &os);
616 cmn_err(CE_WARN, "can't open objset for %s", osname);
620 zilog = dmu_objset_zil(os);
621 zh = zil_header_in_syncing_context(zilog);
623 if (spa_get_log_state(zilog->zl_spa) == SPA_LOG_CLEAR) {
624 if (!BP_IS_HOLE(&zh->zh_log))
625 zio_free_zil(zilog->zl_spa, first_txg, &zh->zh_log);
626 BP_ZERO(&zh->zh_log);
627 dsl_dataset_dirty(dmu_objset_ds(os), tx);
628 dmu_objset_rele(os, FTAG);
633 * Claim all log blocks if we haven't already done so, and remember
634 * the highest claimed sequence number. This ensures that if we can
635 * read only part of the log now (e.g. due to a missing device),
636 * but we can read the entire log later, we will not try to replay
637 * or destroy beyond the last block we successfully claimed.
639 ASSERT3U(zh->zh_claim_txg, <=, first_txg);
640 if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) {
641 (void) zil_parse(zilog, zil_claim_log_block,
642 zil_claim_log_record, tx, first_txg);
643 zh->zh_claim_txg = first_txg;
644 zh->zh_claim_blk_seq = zilog->zl_parse_blk_seq;
645 zh->zh_claim_lr_seq = zilog->zl_parse_lr_seq;
646 if (zilog->zl_parse_lr_count || zilog->zl_parse_blk_count > 1)
647 zh->zh_flags |= ZIL_REPLAY_NEEDED;
648 zh->zh_flags |= ZIL_CLAIM_LR_SEQ_VALID;
649 dsl_dataset_dirty(dmu_objset_ds(os), tx);
652 ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1));
653 dmu_objset_rele(os, FTAG);
658 * Check the log by walking the log chain.
659 * Checksum errors are ok as they indicate the end of the chain.
660 * Any other error (no device or read failure) returns an error.
663 zil_check_log_chain(const char *osname, void *tx)
672 error = dmu_objset_hold(osname, FTAG, &os);
674 cmn_err(CE_WARN, "can't open objset for %s", osname);
678 zilog = dmu_objset_zil(os);
679 bp = (blkptr_t *)&zilog->zl_header->zh_log;
682 * Check the first block and determine if it's on a log device
683 * which may have been removed or faulted prior to loading this
684 * pool. If so, there's no point in checking the rest of the log
685 * as its content should have already been synced to the pool.
687 if (!BP_IS_HOLE(bp)) {
689 boolean_t valid = B_TRUE;
691 spa_config_enter(os->os_spa, SCL_STATE, FTAG, RW_READER);
692 vd = vdev_lookup_top(os->os_spa, DVA_GET_VDEV(&bp->blk_dva[0]));
693 if (vd->vdev_islog && vdev_is_dead(vd))
694 valid = vdev_log_state_valid(vd);
695 spa_config_exit(os->os_spa, SCL_STATE, FTAG);
698 dmu_objset_rele(os, FTAG);
704 * Because tx == NULL, zil_claim_log_block() will not actually claim
705 * any blocks, but just determine whether it is possible to do so.
706 * In addition to checking the log chain, zil_claim_log_block()
707 * will invoke zio_claim() with a done func of spa_claim_notify(),
708 * which will update spa_max_claim_txg. See spa_load() for details.
710 error = zil_parse(zilog, zil_claim_log_block, zil_claim_log_record, tx,
711 zilog->zl_header->zh_claim_txg ? -1ULL : spa_first_txg(os->os_spa));
713 dmu_objset_rele(os, FTAG);
715 return ((error == ECKSUM || error == ENOENT) ? 0 : error);
719 zil_vdev_compare(const void *x1, const void *x2)
721 const uint64_t v1 = ((zil_vdev_node_t *)x1)->zv_vdev;
722 const uint64_t v2 = ((zil_vdev_node_t *)x2)->zv_vdev;
733 zil_add_block(zilog_t *zilog, const blkptr_t *bp)
735 avl_tree_t *t = &zilog->zl_vdev_tree;
737 zil_vdev_node_t *zv, zvsearch;
738 int ndvas = BP_GET_NDVAS(bp);
741 if (zfs_nocacheflush)
744 ASSERT(zilog->zl_writer);
747 * Even though we're zl_writer, we still need a lock because the
748 * zl_get_data() callbacks may have dmu_sync() done callbacks
749 * that will run concurrently.
751 mutex_enter(&zilog->zl_vdev_lock);
752 for (i = 0; i < ndvas; i++) {
753 zvsearch.zv_vdev = DVA_GET_VDEV(&bp->blk_dva[i]);
754 if (avl_find(t, &zvsearch, &where) == NULL) {
755 zv = kmem_alloc(sizeof (*zv), KM_SLEEP);
756 zv->zv_vdev = zvsearch.zv_vdev;
757 avl_insert(t, zv, where);
760 mutex_exit(&zilog->zl_vdev_lock);
764 zil_flush_vdevs(zilog_t *zilog)
766 spa_t *spa = zilog->zl_spa;
767 avl_tree_t *t = &zilog->zl_vdev_tree;
772 ASSERT(zilog->zl_writer);
775 * We don't need zl_vdev_lock here because we're the zl_writer,
776 * and all zl_get_data() callbacks are done.
778 if (avl_numnodes(t) == 0)
781 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
783 zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
785 while ((zv = avl_destroy_nodes(t, &cookie)) != NULL) {
786 vdev_t *vd = vdev_lookup_top(spa, zv->zv_vdev);
789 kmem_free(zv, sizeof (*zv));
793 * Wait for all the flushes to complete. Not all devices actually
794 * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails.
796 (void) zio_wait(zio);
798 spa_config_exit(spa, SCL_STATE, FTAG);
802 * Function called when a log block write completes
805 zil_lwb_write_done(zio_t *zio)
807 lwb_t *lwb = zio->io_private;
808 zilog_t *zilog = lwb->lwb_zilog;
809 dmu_tx_t *tx = lwb->lwb_tx;
811 ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF);
812 ASSERT(BP_GET_TYPE(zio->io_bp) == DMU_OT_INTENT_LOG);
813 ASSERT(BP_GET_LEVEL(zio->io_bp) == 0);
814 ASSERT(BP_GET_BYTEORDER(zio->io_bp) == ZFS_HOST_BYTEORDER);
815 ASSERT(!BP_IS_GANG(zio->io_bp));
816 ASSERT(!BP_IS_HOLE(zio->io_bp));
817 ASSERT(zio->io_bp->blk_fill == 0);
820 * Ensure the lwb buffer pointer is cleared before releasing
821 * the txg. If we have had an allocation failure and
822 * the txg is waiting to sync then we want want zil_sync()
823 * to remove the lwb so that it's not picked up as the next new
824 * one in zil_commit_writer(). zil_sync() will only remove
825 * the lwb if lwb_buf is null.
827 zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
828 mutex_enter(&zilog->zl_lock);
831 mutex_exit(&zilog->zl_lock);
834 * Now that we've written this log block, we have a stable pointer
835 * to the next block in the chain, so it's OK to let the txg in
836 * which we allocated the next block sync.
842 * Initialize the io for a log block.
845 zil_lwb_write_init(zilog_t *zilog, lwb_t *lwb)
849 SET_BOOKMARK(&zb, lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET],
850 ZB_ZIL_OBJECT, ZB_ZIL_LEVEL,
851 lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ]);
853 if (zilog->zl_root_zio == NULL) {
854 zilog->zl_root_zio = zio_root(zilog->zl_spa, NULL, NULL,
857 if (lwb->lwb_zio == NULL) {
858 lwb->lwb_zio = zio_rewrite(zilog->zl_root_zio, zilog->zl_spa,
859 0, &lwb->lwb_blk, lwb->lwb_buf, BP_GET_LSIZE(&lwb->lwb_blk),
860 zil_lwb_write_done, lwb, ZIO_PRIORITY_LOG_WRITE,
861 ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE, &zb);
866 * Define a limited set of intent log block sizes.
867 * These must be a multiple of 4KB. Note only the amount used (again
868 * aligned to 4KB) actually gets written. However, we can't always just
869 * allocate SPA_MAXBLOCKSIZE as the slog space could be exhausted.
871 uint64_t zil_block_buckets[] = {
872 4096, /* non TX_WRITE */
873 8192+4096, /* data base */
874 32*1024 + 4096, /* NFS writes */
879 * Use the slog as long as the logbias is 'latency' and the current commit size
880 * is less than the limit or the total list size is less than 2X the limit.
881 * Limit checking is disabled by setting zil_slog_limit to UINT64_MAX.
883 unsigned long zil_slog_limit = 1024 * 1024;
884 #define USE_SLOG(zilog) (((zilog)->zl_logbias == ZFS_LOGBIAS_LATENCY) && \
885 (((zilog)->zl_cur_used < zil_slog_limit) || \
886 ((zilog)->zl_itx_list_sz < (zil_slog_limit << 1))))
889 * Start a log block write and advance to the next log block.
890 * Calls are serialized.
893 zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb)
897 spa_t *spa = zilog->zl_spa;
901 uint64_t zil_blksz, wsz;
905 if (BP_GET_CHECKSUM(&lwb->lwb_blk) == ZIO_CHECKSUM_ZILOG2) {
906 zilc = (zil_chain_t *)lwb->lwb_buf;
907 bp = &zilc->zc_next_blk;
909 zilc = (zil_chain_t *)(lwb->lwb_buf + lwb->lwb_sz);
910 bp = &zilc->zc_next_blk;
913 ASSERT(lwb->lwb_nused <= lwb->lwb_sz);
916 * Allocate the next block and save its address in this block
917 * before writing it in order to establish the log chain.
918 * Note that if the allocation of nlwb synced before we wrote
919 * the block that points at it (lwb), we'd leak it if we crashed.
920 * Therefore, we don't do dmu_tx_commit() until zil_lwb_write_done().
921 * We dirty the dataset to ensure that zil_sync() will be called
922 * to clean up in the event of allocation failure or I/O failure.
924 tx = dmu_tx_create(zilog->zl_os);
925 VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0);
926 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
927 txg = dmu_tx_get_txg(tx);
932 * Log blocks are pre-allocated. Here we select the size of the next
933 * block, based on size used in the last block.
934 * - first find the smallest bucket that will fit the block from a
935 * limited set of block sizes. This is because it's faster to write
936 * blocks allocated from the same metaslab as they are adjacent or
938 * - next find the maximum from the new suggested size and an array of
939 * previous sizes. This lessens a picket fence effect of wrongly
940 * guesssing the size if we have a stream of say 2k, 64k, 2k, 64k
943 * Note we only write what is used, but we can't just allocate
944 * the maximum block size because we can exhaust the available
947 zil_blksz = zilog->zl_cur_used + sizeof (zil_chain_t);
948 for (i = 0; zil_blksz > zil_block_buckets[i]; i++)
950 zil_blksz = zil_block_buckets[i];
951 if (zil_blksz == UINT64_MAX)
952 zil_blksz = SPA_MAXBLOCKSIZE;
953 zilog->zl_prev_blks[zilog->zl_prev_rotor] = zil_blksz;
954 for (i = 0; i < ZIL_PREV_BLKS; i++)
955 zil_blksz = MAX(zil_blksz, zilog->zl_prev_blks[i]);
956 zilog->zl_prev_rotor = (zilog->zl_prev_rotor + 1) & (ZIL_PREV_BLKS - 1);
959 /* pass the old blkptr in order to spread log blocks across devs */
960 use_slog = USE_SLOG(zilog);
961 error = zio_alloc_zil(spa, txg, bp, &lwb->lwb_blk, zil_blksz,
965 ZIL_STAT_BUMP(zil_itx_metaslab_slog_count);
966 ZIL_STAT_INCR(zil_itx_metaslab_slog_bytes, lwb->lwb_nused);
970 ZIL_STAT_BUMP(zil_itx_metaslab_normal_count);
971 ZIL_STAT_INCR(zil_itx_metaslab_normal_bytes, lwb->lwb_nused);
974 ASSERT3U(bp->blk_birth, ==, txg);
975 bp->blk_cksum = lwb->lwb_blk.blk_cksum;
976 bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++;
979 * Allocate a new log write buffer (lwb).
981 nlwb = zil_alloc_lwb(zilog, bp, txg);
983 /* Record the block for later vdev flushing */
984 zil_add_block(zilog, &lwb->lwb_blk);
987 if (BP_GET_CHECKSUM(&lwb->lwb_blk) == ZIO_CHECKSUM_ZILOG2) {
988 /* For Slim ZIL only write what is used. */
989 wsz = P2ROUNDUP_TYPED(lwb->lwb_nused, ZIL_MIN_BLKSZ, uint64_t);
990 ASSERT3U(wsz, <=, lwb->lwb_sz);
991 zio_shrink(lwb->lwb_zio, wsz);
998 zilc->zc_nused = lwb->lwb_nused;
999 zilc->zc_eck.zec_cksum = lwb->lwb_blk.blk_cksum;
1002 * clear unused data for security
1004 bzero(lwb->lwb_buf + lwb->lwb_nused, wsz - lwb->lwb_nused);
1006 zio_nowait(lwb->lwb_zio); /* Kick off the write for the old log block */
1009 * If there was an allocation failure then nlwb will be null which
1010 * forces a txg_wait_synced().
1016 zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb)
1018 lr_t *lrc = &itx->itx_lr; /* common log record */
1019 lr_write_t *lrw = (lr_write_t *)lrc;
1021 uint64_t txg = lrc->lrc_txg;
1022 uint64_t reclen = lrc->lrc_reclen;
1028 ASSERT(lwb->lwb_buf != NULL);
1030 if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY)
1031 dlen = P2ROUNDUP_TYPED(
1032 lrw->lr_length, sizeof (uint64_t), uint64_t);
1034 zilog->zl_cur_used += (reclen + dlen);
1036 zil_lwb_write_init(zilog, lwb);
1039 * If this record won't fit in the current log block, start a new one.
1041 if (lwb->lwb_nused + reclen + dlen > lwb->lwb_sz) {
1042 lwb = zil_lwb_write_start(zilog, lwb);
1045 zil_lwb_write_init(zilog, lwb);
1046 ASSERT(LWB_EMPTY(lwb));
1047 if (lwb->lwb_nused + reclen + dlen > lwb->lwb_sz) {
1048 txg_wait_synced(zilog->zl_dmu_pool, txg);
1053 lr_buf = lwb->lwb_buf + lwb->lwb_nused;
1054 bcopy(lrc, lr_buf, reclen);
1055 lrc = (lr_t *)lr_buf;
1056 lrw = (lr_write_t *)lrc;
1058 ZIL_STAT_BUMP(zil_itx_count);
1061 * If it's a write, fetch the data or get its blkptr as appropriate.
1063 if (lrc->lrc_txtype == TX_WRITE) {
1064 if (txg > spa_freeze_txg(zilog->zl_spa))
1065 txg_wait_synced(zilog->zl_dmu_pool, txg);
1066 if (itx->itx_wr_state == WR_COPIED) {
1067 ZIL_STAT_BUMP(zil_itx_copied_count);
1068 ZIL_STAT_INCR(zil_itx_copied_bytes, lrw->lr_length);
1074 ASSERT(itx->itx_wr_state == WR_NEED_COPY);
1075 dbuf = lr_buf + reclen;
1076 lrw->lr_common.lrc_reclen += dlen;
1077 ZIL_STAT_BUMP(zil_itx_needcopy_count);
1078 ZIL_STAT_INCR(zil_itx_needcopy_bytes, lrw->lr_length);
1080 ASSERT(itx->itx_wr_state == WR_INDIRECT);
1082 ZIL_STAT_BUMP(zil_itx_indirect_count);
1083 ZIL_STAT_INCR(zil_itx_indirect_bytes, lrw->lr_length);
1085 error = zilog->zl_get_data(
1086 itx->itx_private, lrw, dbuf, lwb->lwb_zio);
1088 txg_wait_synced(zilog->zl_dmu_pool, txg);
1092 ASSERT(error == ENOENT || error == EEXIST ||
1100 * We're actually making an entry, so update lrc_seq to be the
1101 * log record sequence number. Note that this is generally not
1102 * equal to the itx sequence number because not all transactions
1103 * are synchronous, and sometimes spa_sync() gets there first.
1105 lrc->lrc_seq = ++zilog->zl_lr_seq; /* we are single threaded */
1106 lwb->lwb_nused += reclen + dlen;
1107 lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg);
1108 ASSERT3U(lwb->lwb_nused, <=, lwb->lwb_sz);
1109 ASSERT3U(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)), ==, 0);
1115 zil_itx_create(uint64_t txtype, size_t lrsize)
1119 lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t);
1121 itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize,
1122 KM_PUSHPAGE | KM_NODEBUG);
1123 itx->itx_lr.lrc_txtype = txtype;
1124 itx->itx_lr.lrc_reclen = lrsize;
1125 itx->itx_sod = lrsize; /* if write & WR_NEED_COPY will be increased */
1126 itx->itx_lr.lrc_seq = 0; /* defensive */
1127 itx->itx_sync = B_TRUE; /* default is synchronous */
1133 zil_itx_destroy(itx_t *itx)
1135 kmem_free(itx, offsetof(itx_t, itx_lr) + itx->itx_lr.lrc_reclen);
1139 * Free up the sync and async itxs. The itxs_t has already been detached
1140 * so no locks are needed.
1143 zil_itxg_clean(itxs_t *itxs)
1149 itx_async_node_t *ian;
1151 list = &itxs->i_sync_list;
1152 while ((itx = list_head(list)) != NULL) {
1153 list_remove(list, itx);
1154 kmem_free(itx, offsetof(itx_t, itx_lr) +
1155 itx->itx_lr.lrc_reclen);
1159 t = &itxs->i_async_tree;
1160 while ((ian = avl_destroy_nodes(t, &cookie)) != NULL) {
1161 list = &ian->ia_list;
1162 while ((itx = list_head(list)) != NULL) {
1163 list_remove(list, itx);
1164 kmem_free(itx, offsetof(itx_t, itx_lr) +
1165 itx->itx_lr.lrc_reclen);
1168 kmem_free(ian, sizeof (itx_async_node_t));
1172 kmem_free(itxs, sizeof (itxs_t));
1176 zil_aitx_compare(const void *x1, const void *x2)
1178 const uint64_t o1 = ((itx_async_node_t *)x1)->ia_foid;
1179 const uint64_t o2 = ((itx_async_node_t *)x2)->ia_foid;
1190 * Remove all async itx with the given oid.
1193 zil_remove_async(zilog_t *zilog, uint64_t oid)
1196 itx_async_node_t *ian;
1203 list_create(&clean_list, sizeof (itx_t), offsetof(itx_t, itx_node));
1205 if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */
1208 otxg = spa_last_synced_txg(zilog->zl_spa) + 1;
1210 for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) {
1211 itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK];
1213 mutex_enter(&itxg->itxg_lock);
1214 if (itxg->itxg_txg != txg) {
1215 mutex_exit(&itxg->itxg_lock);
1220 * Locate the object node and append its list.
1222 t = &itxg->itxg_itxs->i_async_tree;
1223 ian = avl_find(t, &oid, &where);
1225 list_move_tail(&clean_list, &ian->ia_list);
1226 mutex_exit(&itxg->itxg_lock);
1228 while ((itx = list_head(&clean_list)) != NULL) {
1229 list_remove(&clean_list, itx);
1230 kmem_free(itx, offsetof(itx_t, itx_lr) +
1231 itx->itx_lr.lrc_reclen);
1233 list_destroy(&clean_list);
1237 zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx)
1241 itxs_t *itxs, *clean = NULL;
1244 * Object ids can be re-instantiated in the next txg so
1245 * remove any async transactions to avoid future leaks.
1246 * This can happen if a fsync occurs on the re-instantiated
1247 * object for a WR_INDIRECT or WR_NEED_COPY write, which gets
1248 * the new file data and flushes a write record for the old object.
1250 if ((itx->itx_lr.lrc_txtype & ~TX_CI) == TX_REMOVE)
1251 zil_remove_async(zilog, itx->itx_oid);
1254 * Ensure the data of a renamed file is committed before the rename.
1256 if ((itx->itx_lr.lrc_txtype & ~TX_CI) == TX_RENAME)
1257 zil_async_to_sync(zilog, itx->itx_oid);
1259 if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX)
1262 txg = dmu_tx_get_txg(tx);
1264 itxg = &zilog->zl_itxg[txg & TXG_MASK];
1265 mutex_enter(&itxg->itxg_lock);
1266 itxs = itxg->itxg_itxs;
1267 if (itxg->itxg_txg != txg) {
1270 * The zil_clean callback hasn't got around to cleaning
1271 * this itxg. Save the itxs for release below.
1272 * This should be rare.
1274 atomic_add_64(&zilog->zl_itx_list_sz, -itxg->itxg_sod);
1276 clean = itxg->itxg_itxs;
1278 ASSERT(itxg->itxg_sod == 0);
1279 itxg->itxg_txg = txg;
1280 itxs = itxg->itxg_itxs = kmem_zalloc(sizeof (itxs_t), KM_SLEEP);
1282 list_create(&itxs->i_sync_list, sizeof (itx_t),
1283 offsetof(itx_t, itx_node));
1284 avl_create(&itxs->i_async_tree, zil_aitx_compare,
1285 sizeof (itx_async_node_t),
1286 offsetof(itx_async_node_t, ia_node));
1288 if (itx->itx_sync) {
1289 list_insert_tail(&itxs->i_sync_list, itx);
1290 atomic_add_64(&zilog->zl_itx_list_sz, itx->itx_sod);
1291 itxg->itxg_sod += itx->itx_sod;
1293 avl_tree_t *t = &itxs->i_async_tree;
1294 uint64_t foid = ((lr_ooo_t *)&itx->itx_lr)->lr_foid;
1295 itx_async_node_t *ian;
1298 ian = avl_find(t, &foid, &where);
1300 ian = kmem_alloc(sizeof (itx_async_node_t), KM_SLEEP);
1301 list_create(&ian->ia_list, sizeof (itx_t),
1302 offsetof(itx_t, itx_node));
1303 ian->ia_foid = foid;
1304 avl_insert(t, ian, where);
1306 list_insert_tail(&ian->ia_list, itx);
1309 itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx);
1310 mutex_exit(&itxg->itxg_lock);
1312 /* Release the old itxs now we've dropped the lock */
1314 zil_itxg_clean(clean);
1318 * If there are any in-memory intent log transactions which have now been
1319 * synced then start up a taskq to free them.
1322 zil_clean(zilog_t *zilog, uint64_t synced_txg)
1324 itxg_t *itxg = &zilog->zl_itxg[synced_txg & TXG_MASK];
1327 mutex_enter(&itxg->itxg_lock);
1328 if (itxg->itxg_itxs == NULL || itxg->itxg_txg == ZILTEST_TXG) {
1329 mutex_exit(&itxg->itxg_lock);
1332 ASSERT3U(itxg->itxg_txg, <=, synced_txg);
1333 ASSERT(itxg->itxg_txg != 0);
1334 ASSERT(zilog->zl_clean_taskq != NULL);
1335 atomic_add_64(&zilog->zl_itx_list_sz, -itxg->itxg_sod);
1337 clean_me = itxg->itxg_itxs;
1338 itxg->itxg_itxs = NULL;
1340 mutex_exit(&itxg->itxg_lock);
1342 * Preferably start a task queue to free up the old itxs but
1343 * if taskq_dispatch can't allocate resources to do that then
1344 * free it in-line. This should be rare. Note, using TQ_SLEEP
1345 * created a bad performance problem.
1347 if (taskq_dispatch(zilog->zl_clean_taskq,
1348 (void (*)(void *))zil_itxg_clean, clean_me, TQ_NOSLEEP) == 0)
1349 zil_itxg_clean(clean_me);
1353 * Get the list of itxs to commit into zl_itx_commit_list.
1356 zil_get_commit_list(zilog_t *zilog)
1359 list_t *commit_list = &zilog->zl_itx_commit_list;
1360 uint64_t push_sod = 0;
1362 if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */
1365 otxg = spa_last_synced_txg(zilog->zl_spa) + 1;
1367 for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) {
1368 itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK];
1370 mutex_enter(&itxg->itxg_lock);
1371 if (itxg->itxg_txg != txg) {
1372 mutex_exit(&itxg->itxg_lock);
1376 list_move_tail(commit_list, &itxg->itxg_itxs->i_sync_list);
1377 push_sod += itxg->itxg_sod;
1380 mutex_exit(&itxg->itxg_lock);
1382 atomic_add_64(&zilog->zl_itx_list_sz, -push_sod);
1386 * Move the async itxs for a specified object to commit into sync lists.
1389 zil_async_to_sync(zilog_t *zilog, uint64_t foid)
1392 itx_async_node_t *ian;
1396 if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */
1399 otxg = spa_last_synced_txg(zilog->zl_spa) + 1;
1401 for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) {
1402 itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK];
1404 mutex_enter(&itxg->itxg_lock);
1405 if (itxg->itxg_txg != txg) {
1406 mutex_exit(&itxg->itxg_lock);
1411 * If a foid is specified then find that node and append its
1412 * list. Otherwise walk the tree appending all the lists
1413 * to the sync list. We add to the end rather than the
1414 * beginning to ensure the create has happened.
1416 t = &itxg->itxg_itxs->i_async_tree;
1418 ian = avl_find(t, &foid, &where);
1420 list_move_tail(&itxg->itxg_itxs->i_sync_list,
1424 void *cookie = NULL;
1426 while ((ian = avl_destroy_nodes(t, &cookie)) != NULL) {
1427 list_move_tail(&itxg->itxg_itxs->i_sync_list,
1429 list_destroy(&ian->ia_list);
1430 kmem_free(ian, sizeof (itx_async_node_t));
1433 mutex_exit(&itxg->itxg_lock);
1438 zil_commit_writer(zilog_t *zilog)
1443 spa_t *spa = zilog->zl_spa;
1446 ASSERT(zilog->zl_root_zio == NULL);
1448 mutex_exit(&zilog->zl_lock);
1450 zil_get_commit_list(zilog);
1453 * Return if there's nothing to commit before we dirty the fs by
1454 * calling zil_create().
1456 if (list_head(&zilog->zl_itx_commit_list) == NULL) {
1457 mutex_enter(&zilog->zl_lock);
1461 if (zilog->zl_suspend) {
1464 lwb = list_tail(&zilog->zl_lwb_list);
1466 lwb = zil_create(zilog);
1469 DTRACE_PROBE1(zil__cw1, zilog_t *, zilog);
1470 while ((itx = list_head(&zilog->zl_itx_commit_list))) {
1471 txg = itx->itx_lr.lrc_txg;
1474 if (txg > spa_last_synced_txg(spa) || txg > spa_freeze_txg(spa))
1475 lwb = zil_lwb_commit(zilog, itx, lwb);
1476 list_remove(&zilog->zl_itx_commit_list, itx);
1477 kmem_free(itx, offsetof(itx_t, itx_lr)
1478 + itx->itx_lr.lrc_reclen);
1480 DTRACE_PROBE1(zil__cw2, zilog_t *, zilog);
1482 /* write the last block out */
1483 if (lwb != NULL && lwb->lwb_zio != NULL)
1484 lwb = zil_lwb_write_start(zilog, lwb);
1486 zilog->zl_cur_used = 0;
1489 * Wait if necessary for the log blocks to be on stable storage.
1491 if (zilog->zl_root_zio) {
1492 error = zio_wait(zilog->zl_root_zio);
1493 zilog->zl_root_zio = NULL;
1494 zil_flush_vdevs(zilog);
1497 if (error || lwb == NULL)
1498 txg_wait_synced(zilog->zl_dmu_pool, 0);
1500 mutex_enter(&zilog->zl_lock);
1503 * Remember the highest committed log sequence number for ztest.
1504 * We only update this value when all the log writes succeeded,
1505 * because ztest wants to ASSERT that it got the whole log chain.
1507 if (error == 0 && lwb != NULL)
1508 zilog->zl_commit_lr_seq = zilog->zl_lr_seq;
1512 * Commit zfs transactions to stable storage.
1513 * If foid is 0 push out all transactions, otherwise push only those
1514 * for that object or might reference that object.
1516 * itxs are committed in batches. In a heavily stressed zil there will be
1517 * a commit writer thread who is writing out a bunch of itxs to the log
1518 * for a set of committing threads (cthreads) in the same batch as the writer.
1519 * Those cthreads are all waiting on the same cv for that batch.
1521 * There will also be a different and growing batch of threads that are
1522 * waiting to commit (qthreads). When the committing batch completes
1523 * a transition occurs such that the cthreads exit and the qthreads become
1524 * cthreads. One of the new cthreads becomes the writer thread for the
1525 * batch. Any new threads arriving become new qthreads.
1527 * Only 2 condition variables are needed and there's no transition
1528 * between the two cvs needed. They just flip-flop between qthreads
1531 * Using this scheme we can efficiently wakeup up only those threads
1532 * that have been committed.
1535 zil_commit(zilog_t *zilog, uint64_t foid)
1539 if (zilog->zl_sync == ZFS_SYNC_DISABLED)
1542 ZIL_STAT_BUMP(zil_commit_count);
1544 /* move the async itxs for the foid to the sync queues */
1545 zil_async_to_sync(zilog, foid);
1547 mutex_enter(&zilog->zl_lock);
1548 mybatch = zilog->zl_next_batch;
1549 while (zilog->zl_writer) {
1550 cv_wait(&zilog->zl_cv_batch[mybatch & 1], &zilog->zl_lock);
1551 if (mybatch <= zilog->zl_com_batch) {
1552 mutex_exit(&zilog->zl_lock);
1557 zilog->zl_next_batch++;
1558 zilog->zl_writer = B_TRUE;
1559 ZIL_STAT_BUMP(zil_commit_writer_count);
1560 zil_commit_writer(zilog);
1561 zilog->zl_com_batch = mybatch;
1562 zilog->zl_writer = B_FALSE;
1563 mutex_exit(&zilog->zl_lock);
1565 /* wake up one thread to become the next writer */
1566 cv_signal(&zilog->zl_cv_batch[(mybatch+1) & 1]);
1568 /* wake up all threads waiting for this batch to be committed */
1569 cv_broadcast(&zilog->zl_cv_batch[mybatch & 1]);
1573 * Called in syncing context to free committed log blocks and update log header.
1576 zil_sync(zilog_t *zilog, dmu_tx_t *tx)
1578 zil_header_t *zh = zil_header_in_syncing_context(zilog);
1579 uint64_t txg = dmu_tx_get_txg(tx);
1580 spa_t *spa = zilog->zl_spa;
1581 uint64_t *replayed_seq = &zilog->zl_replayed_seq[txg & TXG_MASK];
1585 * We don't zero out zl_destroy_txg, so make sure we don't try
1586 * to destroy it twice.
1588 if (spa_sync_pass(spa) != 1)
1591 mutex_enter(&zilog->zl_lock);
1593 ASSERT(zilog->zl_stop_sync == 0);
1595 if (*replayed_seq != 0) {
1596 ASSERT(zh->zh_replay_seq < *replayed_seq);
1597 zh->zh_replay_seq = *replayed_seq;
1601 if (zilog->zl_destroy_txg == txg) {
1602 blkptr_t blk = zh->zh_log;
1604 ASSERT(list_head(&zilog->zl_lwb_list) == NULL);
1606 bzero(zh, sizeof (zil_header_t));
1607 bzero(zilog->zl_replayed_seq, sizeof (zilog->zl_replayed_seq));
1609 if (zilog->zl_keep_first) {
1611 * If this block was part of log chain that couldn't
1612 * be claimed because a device was missing during
1613 * zil_claim(), but that device later returns,
1614 * then this block could erroneously appear valid.
1615 * To guard against this, assign a new GUID to the new
1616 * log chain so it doesn't matter what blk points to.
1618 zil_init_log_chain(zilog, &blk);
1623 while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
1624 zh->zh_log = lwb->lwb_blk;
1625 if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg)
1627 list_remove(&zilog->zl_lwb_list, lwb);
1628 zio_free_zil(spa, txg, &lwb->lwb_blk);
1629 kmem_cache_free(zil_lwb_cache, lwb);
1632 * If we don't have anything left in the lwb list then
1633 * we've had an allocation failure and we need to zero
1634 * out the zil_header blkptr so that we don't end
1635 * up freeing the same block twice.
1637 if (list_head(&zilog->zl_lwb_list) == NULL)
1638 BP_ZERO(&zh->zh_log);
1640 mutex_exit(&zilog->zl_lock);
1646 zil_lwb_cache = kmem_cache_create("zil_lwb_cache",
1647 sizeof (struct lwb), 0, NULL, NULL, NULL, NULL, NULL, 0);
1649 zil_ksp = kstat_create("zfs", 0, "zil", "misc",
1650 KSTAT_TYPE_NAMED, sizeof(zil_stats) / sizeof(kstat_named_t),
1651 KSTAT_FLAG_VIRTUAL);
1653 if (zil_ksp != NULL) {
1654 zil_ksp->ks_data = &zil_stats;
1655 kstat_install(zil_ksp);
1662 kmem_cache_destroy(zil_lwb_cache);
1664 if (zil_ksp != NULL) {
1665 kstat_delete(zil_ksp);
1671 zil_set_sync(zilog_t *zilog, uint64_t sync)
1673 zilog->zl_sync = sync;
1677 zil_set_logbias(zilog_t *zilog, uint64_t logbias)
1679 zilog->zl_logbias = logbias;
1683 zil_alloc(objset_t *os, zil_header_t *zh_phys)
1688 zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP);
1690 zilog->zl_header = zh_phys;
1692 zilog->zl_spa = dmu_objset_spa(os);
1693 zilog->zl_dmu_pool = dmu_objset_pool(os);
1694 zilog->zl_destroy_txg = TXG_INITIAL - 1;
1695 zilog->zl_logbias = dmu_objset_logbias(os);
1696 zilog->zl_sync = dmu_objset_syncprop(os);
1697 zilog->zl_next_batch = 1;
1699 mutex_init(&zilog->zl_lock, NULL, MUTEX_DEFAULT, NULL);
1701 for (i = 0; i < TXG_SIZE; i++) {
1702 mutex_init(&zilog->zl_itxg[i].itxg_lock, NULL,
1703 MUTEX_DEFAULT, NULL);
1706 list_create(&zilog->zl_lwb_list, sizeof (lwb_t),
1707 offsetof(lwb_t, lwb_node));
1709 list_create(&zilog->zl_itx_commit_list, sizeof (itx_t),
1710 offsetof(itx_t, itx_node));
1712 mutex_init(&zilog->zl_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
1714 avl_create(&zilog->zl_vdev_tree, zil_vdev_compare,
1715 sizeof (zil_vdev_node_t), offsetof(zil_vdev_node_t, zv_node));
1717 cv_init(&zilog->zl_cv_writer, NULL, CV_DEFAULT, NULL);
1718 cv_init(&zilog->zl_cv_suspend, NULL, CV_DEFAULT, NULL);
1719 cv_init(&zilog->zl_cv_batch[0], NULL, CV_DEFAULT, NULL);
1720 cv_init(&zilog->zl_cv_batch[1], NULL, CV_DEFAULT, NULL);
1726 zil_free(zilog_t *zilog)
1730 zilog->zl_stop_sync = 1;
1732 ASSERT(list_is_empty(&zilog->zl_lwb_list));
1733 list_destroy(&zilog->zl_lwb_list);
1735 avl_destroy(&zilog->zl_vdev_tree);
1736 mutex_destroy(&zilog->zl_vdev_lock);
1738 ASSERT(list_is_empty(&zilog->zl_itx_commit_list));
1739 list_destroy(&zilog->zl_itx_commit_list);
1741 for (i = 0; i < TXG_SIZE; i++) {
1743 * It's possible for an itx to be generated that doesn't dirty
1744 * a txg (e.g. ztest TX_TRUNCATE). So there's no zil_clean()
1745 * callback to remove the entry. We remove those here.
1747 * Also free up the ziltest itxs.
1749 if (zilog->zl_itxg[i].itxg_itxs)
1750 zil_itxg_clean(zilog->zl_itxg[i].itxg_itxs);
1751 mutex_destroy(&zilog->zl_itxg[i].itxg_lock);
1754 mutex_destroy(&zilog->zl_lock);
1756 cv_destroy(&zilog->zl_cv_writer);
1757 cv_destroy(&zilog->zl_cv_suspend);
1758 cv_destroy(&zilog->zl_cv_batch[0]);
1759 cv_destroy(&zilog->zl_cv_batch[1]);
1761 kmem_free(zilog, sizeof (zilog_t));
1765 * Open an intent log.
1768 zil_open(objset_t *os, zil_get_data_t *get_data)
1770 zilog_t *zilog = dmu_objset_zil(os);
1772 ASSERT(zilog->zl_clean_taskq == NULL);
1773 ASSERT(zilog->zl_get_data == NULL);
1774 ASSERT(list_is_empty(&zilog->zl_lwb_list));
1776 zilog->zl_get_data = get_data;
1777 zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri,
1778 2, 2, TASKQ_PREPOPULATE);
1784 * Close an intent log.
1787 zil_close(zilog_t *zilog)
1792 zil_commit(zilog, 0); /* commit all itx */
1795 * The lwb_max_txg for the stubby lwb will reflect the last activity
1796 * for the zil. After a txg_wait_synced() on the txg we know all the
1797 * callbacks have occurred that may clean the zil. Only then can we
1798 * destroy the zl_clean_taskq.
1800 mutex_enter(&zilog->zl_lock);
1801 lwb = list_tail(&zilog->zl_lwb_list);
1803 txg = lwb->lwb_max_txg;
1804 mutex_exit(&zilog->zl_lock);
1806 txg_wait_synced(zilog->zl_dmu_pool, txg);
1808 taskq_destroy(zilog->zl_clean_taskq);
1809 zilog->zl_clean_taskq = NULL;
1810 zilog->zl_get_data = NULL;
1813 * We should have only one LWB left on the list; remove it now.
1815 mutex_enter(&zilog->zl_lock);
1816 lwb = list_head(&zilog->zl_lwb_list);
1818 ASSERT(lwb == list_tail(&zilog->zl_lwb_list));
1819 list_remove(&zilog->zl_lwb_list, lwb);
1820 zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
1821 kmem_cache_free(zil_lwb_cache, lwb);
1823 mutex_exit(&zilog->zl_lock);
1827 * Suspend an intent log. While in suspended mode, we still honor
1828 * synchronous semantics, but we rely on txg_wait_synced() to do it.
1829 * We suspend the log briefly when taking a snapshot so that the snapshot
1830 * contains all the data it's supposed to, and has an empty intent log.
1833 zil_suspend(zilog_t *zilog)
1835 const zil_header_t *zh = zilog->zl_header;
1837 mutex_enter(&zilog->zl_lock);
1838 if (zh->zh_flags & ZIL_REPLAY_NEEDED) { /* unplayed log */
1839 mutex_exit(&zilog->zl_lock);
1842 if (zilog->zl_suspend++ != 0) {
1844 * Someone else already began a suspend.
1845 * Just wait for them to finish.
1847 while (zilog->zl_suspending)
1848 cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock);
1849 mutex_exit(&zilog->zl_lock);
1852 zilog->zl_suspending = B_TRUE;
1853 mutex_exit(&zilog->zl_lock);
1855 zil_commit(zilog, 0);
1857 zil_destroy(zilog, B_FALSE);
1859 mutex_enter(&zilog->zl_lock);
1860 zilog->zl_suspending = B_FALSE;
1861 cv_broadcast(&zilog->zl_cv_suspend);
1862 mutex_exit(&zilog->zl_lock);
1868 zil_resume(zilog_t *zilog)
1870 mutex_enter(&zilog->zl_lock);
1871 ASSERT(zilog->zl_suspend != 0);
1872 zilog->zl_suspend--;
1873 mutex_exit(&zilog->zl_lock);
1876 typedef struct zil_replay_arg {
1877 zil_replay_func_t **zr_replay;
1879 boolean_t zr_byteswap;
1884 zil_replay_error(zilog_t *zilog, lr_t *lr, int error)
1886 char name[MAXNAMELEN];
1888 zilog->zl_replaying_seq--; /* didn't actually replay this one */
1890 dmu_objset_name(zilog->zl_os, name);
1892 cmn_err(CE_WARN, "ZFS replay transaction error %d, "
1893 "dataset %s, seq 0x%llx, txtype %llu %s\n", error, name,
1894 (u_longlong_t)lr->lrc_seq,
1895 (u_longlong_t)(lr->lrc_txtype & ~TX_CI),
1896 (lr->lrc_txtype & TX_CI) ? "CI" : "");
1902 zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg)
1904 zil_replay_arg_t *zr = zra;
1905 const zil_header_t *zh = zilog->zl_header;
1906 uint64_t reclen = lr->lrc_reclen;
1907 uint64_t txtype = lr->lrc_txtype;
1910 zilog->zl_replaying_seq = lr->lrc_seq;
1912 if (lr->lrc_seq <= zh->zh_replay_seq) /* already replayed */
1915 if (lr->lrc_txg < claim_txg) /* already committed */
1918 /* Strip case-insensitive bit, still present in log record */
1921 if (txtype == 0 || txtype >= TX_MAX_TYPE)
1922 return (zil_replay_error(zilog, lr, EINVAL));
1925 * If this record type can be logged out of order, the object
1926 * (lr_foid) may no longer exist. That's legitimate, not an error.
1928 if (TX_OOO(txtype)) {
1929 error = dmu_object_info(zilog->zl_os,
1930 ((lr_ooo_t *)lr)->lr_foid, NULL);
1931 if (error == ENOENT || error == EEXIST)
1936 * Make a copy of the data so we can revise and extend it.
1938 bcopy(lr, zr->zr_lr, reclen);
1941 * If this is a TX_WRITE with a blkptr, suck in the data.
1943 if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) {
1944 error = zil_read_log_data(zilog, (lr_write_t *)lr,
1945 zr->zr_lr + reclen);
1947 return (zil_replay_error(zilog, lr, error));
1951 * The log block containing this lr may have been byteswapped
1952 * so that we can easily examine common fields like lrc_txtype.
1953 * However, the log is a mix of different record types, and only the
1954 * replay vectors know how to byteswap their records. Therefore, if
1955 * the lr was byteswapped, undo it before invoking the replay vector.
1957 if (zr->zr_byteswap)
1958 byteswap_uint64_array(zr->zr_lr, reclen);
1961 * We must now do two things atomically: replay this log record,
1962 * and update the log header sequence number to reflect the fact that
1963 * we did so. At the end of each replay function the sequence number
1964 * is updated if we are in replay mode.
1966 error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lr, zr->zr_byteswap);
1969 * The DMU's dnode layer doesn't see removes until the txg
1970 * commits, so a subsequent claim can spuriously fail with
1971 * EEXIST. So if we receive any error we try syncing out
1972 * any removes then retry the transaction. Note that we
1973 * specify B_FALSE for byteswap now, so we don't do it twice.
1975 txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0);
1976 error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lr, B_FALSE);
1978 return (zil_replay_error(zilog, lr, error));
1985 zil_incr_blks(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
1987 zilog->zl_replay_blks++;
1993 * If this dataset has a non-empty intent log, replay it and destroy it.
1996 zil_replay(objset_t *os, void *arg, zil_replay_func_t *replay_func[TX_MAX_TYPE])
1998 zilog_t *zilog = dmu_objset_zil(os);
1999 const zil_header_t *zh = zilog->zl_header;
2000 zil_replay_arg_t zr;
2002 if ((zh->zh_flags & ZIL_REPLAY_NEEDED) == 0) {
2003 zil_destroy(zilog, B_TRUE);
2007 zr.zr_replay = replay_func;
2009 zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log);
2010 zr.zr_lr = vmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP);
2013 * Wait for in-progress removes to sync before starting replay.
2015 txg_wait_synced(zilog->zl_dmu_pool, 0);
2017 zilog->zl_replay = B_TRUE;
2018 zilog->zl_replay_time = ddi_get_lbolt();
2019 ASSERT(zilog->zl_replay_blks == 0);
2020 (void) zil_parse(zilog, zil_incr_blks, zil_replay_log_record, &zr,
2022 vmem_free(zr.zr_lr, 2 * SPA_MAXBLOCKSIZE);
2024 zil_destroy(zilog, B_FALSE);
2025 txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
2026 zilog->zl_replay = B_FALSE;
2030 zil_replaying(zilog_t *zilog, dmu_tx_t *tx)
2032 if (zilog->zl_sync == ZFS_SYNC_DISABLED)
2035 if (zilog->zl_replay) {
2036 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
2037 zilog->zl_replayed_seq[dmu_tx_get_txg(tx) & TXG_MASK] =
2038 zilog->zl_replaying_seq;
2047 zil_vdev_offline(const char *osname, void *arg)
2053 error = dmu_objset_hold(osname, FTAG, &os);
2057 zilog = dmu_objset_zil(os);
2058 if (zil_suspend(zilog) != 0)
2062 dmu_objset_rele(os, FTAG);
2066 #if defined(_KERNEL) && defined(HAVE_SPL)
2067 module_param(zil_replay_disable, int, 0644);
2068 MODULE_PARM_DESC(zil_replay_disable, "Disable intent logging replay");
2070 module_param(zfs_nocacheflush, int, 0644);
2071 MODULE_PARM_DESC(zfs_nocacheflush, "Disable cache flushes");
2073 module_param(zil_slog_limit, ulong, 0644);
2074 MODULE_PARM_DESC(zil_slog_limit, "Max commit bytes to separate log device");