]> git.proxmox.com Git - mirror_zfs.git/blobdiff - module/zfs/zil.c
cstyle: Resolve C style issues
[mirror_zfs.git] / module / zfs / zil.c
index 3c18d43fa5c2037c5425f1a206cceef03e63feb5..b69a7bf56eaf63265fd3e92abf50c42759ee8ce8 100644 (file)
@@ -20,6 +20,7 @@
  */
 /*
  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013 by Delphix. All rights reserved.
  */
 
 /* Portions Copyright 2010 Robert Milkowski */
@@ -37,6 +38,7 @@
 #include <sys/vdev_impl.h>
 #include <sys/dmu_tx.h>
 #include <sys/dsl_pool.h>
+#include <sys/metaslab.h>
 
 /*
  * The zfs intent log (ZIL) saves transaction records of system calls
  */
 
 /*
- * This global ZIL switch affects all pools
+ * See zil.h for more information about these fields.
  */
-int zil_replay_disable = 0;    /* disable intent logging replay */
+zil_stats_t zil_stats = {
+       { "zil_commit_count",                   KSTAT_DATA_UINT64 },
+       { "zil_commit_writer_count",            KSTAT_DATA_UINT64 },
+       { "zil_itx_count",                      KSTAT_DATA_UINT64 },
+       { "zil_itx_indirect_count",             KSTAT_DATA_UINT64 },
+       { "zil_itx_indirect_bytes",             KSTAT_DATA_UINT64 },
+       { "zil_itx_copied_count",               KSTAT_DATA_UINT64 },
+       { "zil_itx_copied_bytes",               KSTAT_DATA_UINT64 },
+       { "zil_itx_needcopy_count",             KSTAT_DATA_UINT64 },
+       { "zil_itx_needcopy_bytes",             KSTAT_DATA_UINT64 },
+       { "zil_itx_metaslab_normal_count",      KSTAT_DATA_UINT64 },
+       { "zil_itx_metaslab_normal_bytes",      KSTAT_DATA_UINT64 },
+       { "zil_itx_metaslab_slog_count",        KSTAT_DATA_UINT64 },
+       { "zil_itx_metaslab_slog_bytes",        KSTAT_DATA_UINT64 },
+};
+
+static kstat_t *zil_ksp;
+
+/*
+ * Disable intent logging replay.  This global ZIL switch affects all pools.
+ */
+int zil_replay_disable = 0;
 
 /*
  * Tunable parameter for debugging or performance analysis.  Setting
  * zfs_nocacheflush will cause corruption on power loss if a volatile
  * out-of-order write cache is enabled.
  */
-boolean_t zfs_nocacheflush = B_FALSE;
+int zfs_nocacheflush = 0;
 
 static kmem_cache_t *zil_lwb_cache;
 
@@ -141,9 +164,9 @@ zil_bp_tree_add(zilog_t *zilog, const blkptr_t *bp)
        avl_index_t where;
 
        if (avl_find(t, dva, &where) != NULL)
-               return (EEXIST);
+               return (SET_ERROR(EEXIST));
 
-       zn = kmem_alloc(sizeof (zil_bp_node_t), KM_SLEEP);
+       zn = kmem_alloc(sizeof (zil_bp_node_t), KM_PUSHPAGE);
        zn->zn_dva = *dva;
        avl_insert(t, zn, where);
 
@@ -189,7 +212,7 @@ zil_read_log_block(zilog_t *zilog, const blkptr_t *bp, blkptr_t *nbp, void *dst,
        SET_BOOKMARK(&zb, bp->blk_cksum.zc_word[ZIL_ZC_OBJSET],
            ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, bp->blk_cksum.zc_word[ZIL_ZC_SEQ]);
 
-       error = dsl_read_nolock(NULL, zilog->zl_spa, bp, arc_getbuf_func, &abuf,
+       error = arc_read(NULL, zilog->zl_spa, bp, arc_getbuf_func, &abuf,
            ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb);
 
        if (error == 0) {
@@ -212,7 +235,7 @@ zil_read_log_block(zilog_t *zilog, const blkptr_t *bp, blkptr_t *nbp, void *dst,
 
                        if (bcmp(&cksum, &zilc->zc_next_blk.blk_cksum,
                            sizeof (cksum)) || BP_IS_HOLE(&zilc->zc_next_blk)) {
-                               error = ECKSUM;
+                               error = SET_ERROR(ECKSUM);
                        } else {
                                bcopy(lr, dst, len);
                                *end = (char *)dst + len;
@@ -226,7 +249,7 @@ zil_read_log_block(zilog_t *zilog, const blkptr_t *bp, blkptr_t *nbp, void *dst,
                        if (bcmp(&cksum, &zilc->zc_next_blk.blk_cksum,
                            sizeof (cksum)) || BP_IS_HOLE(&zilc->zc_next_blk) ||
                            (zilc->zc_nused > (size - sizeof (*zilc)))) {
-                               error = ECKSUM;
+                               error = SET_ERROR(ECKSUM);
                        } else {
                                bcopy(lr, dst, zilc->zc_nused);
                                *end = (char *)dst + zilc->zc_nused;
@@ -234,7 +257,7 @@ zil_read_log_block(zilog_t *zilog, const blkptr_t *bp, blkptr_t *nbp, void *dst,
                        }
                }
 
-               VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1);
+               VERIFY(arc_buf_remove_ref(abuf, &abuf));
        }
 
        return (error);
@@ -265,7 +288,7 @@ zil_read_log_data(zilog_t *zilog, const lr_write_t *lr, void *wbuf)
        SET_BOOKMARK(&zb, dmu_objset_id(zilog->zl_os), lr->lr_foid,
            ZB_ZIL_LEVEL, lr->lr_offset / BP_GET_LSIZE(bp));
 
-       error = arc_read_nolock(NULL, zilog->zl_spa, bp, arc_getbuf_func, &abuf,
+       error = arc_read(NULL, zilog->zl_spa, bp, arc_getbuf_func, &abuf,
            ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb);
 
        if (error == 0) {
@@ -296,7 +319,7 @@ zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func,
        char *lrbuf, *lrp;
        int error = 0;
 
-       bzero(&next_blk, sizeof(blkptr_t));
+       bzero(&next_blk, sizeof (blkptr_t));
 
        /*
         * Old logs didn't record the maximum zh_claim_lr_seq.
@@ -333,7 +356,7 @@ zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func,
                        break;
 
                error = zil_read_log_block(zilog, &blk, &next_blk, lrbuf, &end);
-               if (error)
+               if (error != 0)
                        break;
 
                for (lrp = lrbuf; lrp < end; lrp += reclen) {
@@ -429,13 +452,14 @@ zil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg)
 }
 
 static lwb_t *
-zil_alloc_lwb(zilog_t *zilog, blkptr_t *bp, uint64_t txg)
+zil_alloc_lwb(zilog_t *zilog, blkptr_t *bp, uint64_t txg, boolean_t fastwrite)
 {
        lwb_t *lwb;
 
-       lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP);
+       lwb = kmem_cache_alloc(zil_lwb_cache, KM_PUSHPAGE);
        lwb->lwb_zilog = zilog;
        lwb->lwb_blk = *bp;
+       lwb->lwb_fastwrite = fastwrite;
        lwb->lwb_buf = zio_buf_alloc(BP_GET_LSIZE(bp));
        lwb->lwb_max_txg = txg;
        lwb->lwb_zio = NULL;
@@ -455,6 +479,38 @@ zil_alloc_lwb(zilog_t *zilog, blkptr_t *bp, uint64_t txg)
        return (lwb);
 }
 
+/*
+ * Called when we create in-memory log transactions so that we know
+ * to cleanup the itxs at the end of spa_sync().
+ */
+void
+zilog_dirty(zilog_t *zilog, uint64_t txg)
+{
+       dsl_pool_t *dp = zilog->zl_dmu_pool;
+       dsl_dataset_t *ds = dmu_objset_ds(zilog->zl_os);
+
+       if (dsl_dataset_is_snapshot(ds))
+               panic("dirtying snapshot!");
+
+       if (txg_list_add(&dp->dp_dirty_zilogs, zilog, txg)) {
+               /* up the hold count until we can be written out */
+               dmu_buf_add_ref(ds->ds_dbuf, zilog);
+       }
+}
+
+boolean_t
+zilog_is_dirty(zilog_t *zilog)
+{
+       dsl_pool_t *dp = zilog->zl_dmu_pool;
+       int t;
+
+       for (t = 0; t < TXG_SIZE; t++) {
+               if (txg_list_member(&dp->dp_dirty_zilogs, zilog, t))
+                       return (B_TRUE);
+       }
+       return (B_FALSE);
+}
+
 /*
  * Create an on-disk intent log.
  */
@@ -467,6 +523,7 @@ zil_create(zilog_t *zilog)
        dmu_tx_t *tx = NULL;
        blkptr_t blk;
        int error = 0;
+       boolean_t fastwrite = FALSE;
 
        /*
         * Wait for any previous destroy to complete.
@@ -494,8 +551,9 @@ zil_create(zilog_t *zilog)
                        BP_ZERO(&blk);
                }
 
-               error = zio_alloc_zil(zilog->zl_spa, txg, &blk, NULL,
-                   ZIL_MIN_BLKSZ, zilog->zl_logbias == ZFS_LOGBIAS_LATENCY);
+               error = zio_alloc_zil(zilog->zl_spa, txg, &blk,
+                   ZIL_MIN_BLKSZ, B_TRUE);
+               fastwrite = TRUE;
 
                if (error == 0)
                        zil_init_log_chain(zilog, &blk);
@@ -505,7 +563,7 @@ zil_create(zilog_t *zilog)
         * Allocate a log write buffer (lwb) for the first log block.
         */
        if (error == 0)
-               lwb = zil_alloc_lwb(zilog, &blk, txg);
+               lwb = zil_alloc_lwb(zilog, &blk, txg, fastwrite);
 
        /*
         * If we just allocated the first log block, commit our transaction
@@ -562,8 +620,12 @@ zil_destroy(zilog_t *zilog, boolean_t keep_first)
 
        if (!list_is_empty(&zilog->zl_lwb_list)) {
                ASSERT(zh->zh_claim_txg == 0);
-               ASSERT(!keep_first);
+               VERIFY(!keep_first);
                while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
+                       ASSERT(lwb->lwb_zio == NULL);
+                       if (lwb->lwb_fastwrite)
+                               metaslab_fastwrite_unmark(zilog->zl_spa,
+                                   &lwb->lwb_blk);
                        list_remove(&zilog->zl_lwb_list, lwb);
                        if (lwb->lwb_buf != NULL)
                                zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
@@ -571,14 +633,21 @@ zil_destroy(zilog_t *zilog, boolean_t keep_first)
                        kmem_cache_free(zil_lwb_cache, lwb);
                }
        } else if (!keep_first) {
-               (void) zil_parse(zilog, zil_free_log_block,
-                   zil_free_log_record, tx, zh->zh_claim_txg);
+               zil_destroy_sync(zilog, tx);
        }
        mutex_exit(&zilog->zl_lock);
 
        dmu_tx_commit(tx);
 }
 
+void
+zil_destroy_sync(zilog_t *zilog, dmu_tx_t *tx)
+{
+       ASSERT(list_is_empty(&zilog->zl_lwb_list));
+       (void) zil_parse(zilog, zil_free_log_block,
+           zil_free_log_record, tx, zilog->zl_header->zh_claim_txg);
+}
+
 int
 zil_claim(const char *osname, void *txarg)
 {
@@ -589,8 +658,8 @@ zil_claim(const char *osname, void *txarg)
        objset_t *os;
        int error;
 
-       error = dmu_objset_hold(osname, FTAG, &os);
-       if (error) {
+       error = dmu_objset_own(osname, DMU_OST_ANY, B_FALSE, FTAG, &os);
+       if (error != 0) {
                cmn_err(CE_WARN, "can't open objset for %s", osname);
                return (0);
        }
@@ -603,7 +672,7 @@ zil_claim(const char *osname, void *txarg)
                        zio_free_zil(zilog->zl_spa, first_txg, &zh->zh_log);
                BP_ZERO(&zh->zh_log);
                dsl_dataset_dirty(dmu_objset_ds(os), tx);
-               dmu_objset_rele(os, FTAG);
+               dmu_objset_disown(os, FTAG);
                return (0);
        }
 
@@ -628,7 +697,7 @@ zil_claim(const char *osname, void *txarg)
        }
 
        ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1));
-       dmu_objset_rele(os, FTAG);
+       dmu_objset_disown(os, FTAG);
        return (0);
 }
 
@@ -648,7 +717,7 @@ zil_check_log_chain(const char *osname, void *tx)
        ASSERT(tx == NULL);
 
        error = dmu_objset_hold(osname, FTAG, &os);
-       if (error) {
+       if (error != 0) {
                cmn_err(CE_WARN, "can't open objset for %s", osname);
                return (0);
        }
@@ -730,7 +799,7 @@ zil_add_block(zilog_t *zilog, const blkptr_t *bp)
        for (i = 0; i < ndvas; i++) {
                zvsearch.zv_vdev = DVA_GET_VDEV(&bp->blk_dva[i]);
                if (avl_find(t, &zvsearch, &where) == NULL) {
-                       zv = kmem_alloc(sizeof (*zv), KM_SLEEP);
+                       zv = kmem_alloc(sizeof (*zv), KM_PUSHPAGE);
                        zv->zv_vdev = zvsearch.zv_vdev;
                        avl_insert(t, zv, where);
                }
@@ -804,6 +873,8 @@ zil_lwb_write_done(zio_t *zio)
         */
        zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
        mutex_enter(&zilog->zl_lock);
+       lwb->lwb_zio = NULL;
+       lwb->lwb_fastwrite = FALSE;
        lwb->lwb_buf = NULL;
        lwb->lwb_tx = NULL;
        mutex_exit(&zilog->zl_lock);
@@ -832,16 +903,26 @@ zil_lwb_write_init(zilog_t *zilog, lwb_t *lwb)
                zilog->zl_root_zio = zio_root(zilog->zl_spa, NULL, NULL,
                    ZIO_FLAG_CANFAIL);
        }
+
+       /* Lock so zil_sync() doesn't fastwrite_unmark after zio is created */
+       mutex_enter(&zilog->zl_lock);
        if (lwb->lwb_zio == NULL) {
+               if (!lwb->lwb_fastwrite) {
+                       metaslab_fastwrite_mark(zilog->zl_spa, &lwb->lwb_blk);
+                       lwb->lwb_fastwrite = 1;
+               }
                lwb->lwb_zio = zio_rewrite(zilog->zl_root_zio, zilog->zl_spa,
                    0, &lwb->lwb_blk, lwb->lwb_buf, BP_GET_LSIZE(&lwb->lwb_blk),
-                   zil_lwb_write_done, lwb, ZIO_PRIORITY_LOG_WRITE,
-                   ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE, &zb);
+                   zil_lwb_write_done, lwb, ZIO_PRIORITY_SYNC_WRITE,
+                   ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE |
+                   ZIO_FLAG_FASTWRITE, &zb);
        }
+       mutex_exit(&zilog->zl_lock);
 }
 
 /*
  * Define a limited set of intent log block sizes.
+ *
  * These must be a multiple of 4KB. Note only the amount used (again
  * aligned to 4KB) actually gets written. However, we can't always just
  * allocate SPA_MAXBLOCKSIZE as the slog space could be exhausted.
@@ -854,14 +935,13 @@ uint64_t zil_block_buckets[] = {
 };
 
 /*
- * Use the slog as long as the logbias is 'latency' and the current commit size
- * is less than the limit or the total list size is less than 2X the limit.
- * Limit checking is disabled by setting zil_slog_limit to UINT64_MAX.
+ * Use the slog as long as the current commit size is less than the
+ * limit or the total list size is less than 2X the limit.  Limit
+ * checking is disabled by setting zil_slog_limit to UINT64_MAX.
  */
-uint64_t zil_slog_limit = 1024 * 1024;
-#define        USE_SLOG(zilog) (((zilog)->zl_logbias == ZFS_LOGBIAS_LATENCY) && \
-       (((zilog)->zl_cur_used < zil_slog_limit) || \
-       ((zilog)->zl_itx_list_sz < (zil_slog_limit << 1))))
+unsigned long zil_slog_limit = 1024 * 1024;
+#define        USE_SLOG(zilog) (((zilog)->zl_cur_used < zil_slog_limit) || \
+       ((zilog)->zl_itx_list_sz < (zil_slog_limit << 1)))
 
 /*
  * Start a log block write and advance to the next log block.
@@ -878,6 +958,7 @@ zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb)
        uint64_t txg;
        uint64_t zil_blksz, wsz;
        int i, error;
+       boolean_t use_slog;
 
        if (BP_GET_CHECKSUM(&lwb->lwb_blk) == ZIO_CHECKSUM_ZILOG2) {
                zilc = (zil_chain_t *)lwb->lwb_buf;
@@ -933,10 +1014,17 @@ zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb)
        zilog->zl_prev_rotor = (zilog->zl_prev_rotor + 1) & (ZIL_PREV_BLKS - 1);
 
        BP_ZERO(bp);
-       /* pass the old blkptr in order to spread log blocks across devs */
-       error = zio_alloc_zil(spa, txg, bp, &lwb->lwb_blk, zil_blksz,
+       use_slog = USE_SLOG(zilog);
+       error = zio_alloc_zil(spa, txg, bp, zil_blksz,
            USE_SLOG(zilog));
-       if (!error) {
+       if (use_slog) {
+               ZIL_STAT_BUMP(zil_itx_metaslab_slog_count);
+               ZIL_STAT_INCR(zil_itx_metaslab_slog_bytes, lwb->lwb_nused);
+       } else {
+               ZIL_STAT_BUMP(zil_itx_metaslab_normal_count);
+               ZIL_STAT_INCR(zil_itx_metaslab_normal_bytes, lwb->lwb_nused);
+       }
+       if (error == 0) {
                ASSERT3U(bp->blk_birth, ==, txg);
                bp->blk_cksum = lwb->lwb_blk.blk_cksum;
                bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++;
@@ -944,7 +1032,7 @@ zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb)
                /*
                 * Allocate a new log write buffer (lwb).
                 */
-               nlwb = zil_alloc_lwb(zilog, bp, txg);
+               nlwb = zil_alloc_lwb(zilog, bp, txg, TRUE);
 
                /* Record the block for later vdev flushing */
                zil_add_block(zilog, &lwb->lwb_blk);
@@ -992,6 +1080,8 @@ zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb)
                return (NULL);
 
        ASSERT(lwb->lwb_buf != NULL);
+       ASSERT(zilog_is_dirty(zilog) ||
+           spa_freeze_txg(zilog->zl_spa) != UINT64_MAX);
 
        if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY)
                dlen = P2ROUNDUP_TYPED(
@@ -1021,13 +1111,18 @@ zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb)
        lrc = (lr_t *)lr_buf;
        lrw = (lr_write_t *)lrc;
 
+       ZIL_STAT_BUMP(zil_itx_count);
+
        /*
         * If it's a write, fetch the data or get its blkptr as appropriate.
         */
        if (lrc->lrc_txtype == TX_WRITE) {
                if (txg > spa_freeze_txg(zilog->zl_spa))
                        txg_wait_synced(zilog->zl_dmu_pool, txg);
-               if (itx->itx_wr_state != WR_COPIED) {
+               if (itx->itx_wr_state == WR_COPIED) {
+                       ZIL_STAT_BUMP(zil_itx_copied_count);
+                       ZIL_STAT_INCR(zil_itx_copied_bytes, lrw->lr_length);
+               } else {
                        char *dbuf;
                        int error;
 
@@ -1035,9 +1130,15 @@ zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb)
                                ASSERT(itx->itx_wr_state == WR_NEED_COPY);
                                dbuf = lr_buf + reclen;
                                lrw->lr_common.lrc_reclen += dlen;
+                               ZIL_STAT_BUMP(zil_itx_needcopy_count);
+                               ZIL_STAT_INCR(zil_itx_needcopy_bytes,
+                                   lrw->lr_length);
                        } else {
                                ASSERT(itx->itx_wr_state == WR_INDIRECT);
                                dbuf = NULL;
+                               ZIL_STAT_BUMP(zil_itx_indirect_count);
+                               ZIL_STAT_INCR(zil_itx_indirect_bytes,
+                                   lrw->lr_length);
                        }
                        error = zilog->zl_get_data(
                            itx->itx_private, lrw, dbuf, lwb->lwb_zio);
@@ -1045,7 +1146,7 @@ zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb)
                                txg_wait_synced(zilog->zl_dmu_pool, txg);
                                return (lwb);
                        }
-                       if (error) {
+                       if (error != 0) {
                                ASSERT(error == ENOENT || error == EEXIST ||
                                    error == EALREADY);
                                return (lwb);
@@ -1063,7 +1164,7 @@ zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb)
        lwb->lwb_nused += reclen + dlen;
        lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg);
        ASSERT3U(lwb->lwb_nused, <=, lwb->lwb_sz);
-       ASSERT3U(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)), ==, 0);
+       ASSERT0(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)));
 
        return (lwb);
 }
@@ -1075,12 +1176,15 @@ zil_itx_create(uint64_t txtype, size_t lrsize)
 
        lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t);
 
-       itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP);
+       itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize,
+           KM_PUSHPAGE | KM_NODEBUG);
        itx->itx_lr.lrc_txtype = txtype;
        itx->itx_lr.lrc_reclen = lrsize;
        itx->itx_sod = lrsize; /* if write & WR_NEED_COPY will be increased */
        itx->itx_lr.lrc_seq = 0;        /* defensive */
        itx->itx_sync = B_TRUE;         /* default is synchronous */
+       itx->itx_callback = NULL;
+       itx->itx_callback_data = NULL;
 
        return (itx);
 }
@@ -1106,6 +1210,8 @@ zil_itxg_clean(itxs_t *itxs)
 
        list = &itxs->i_sync_list;
        while ((itx = list_head(list)) != NULL) {
+               if (itx->itx_callback != NULL)
+                       itx->itx_callback(itx->itx_callback_data);
                list_remove(list, itx);
                kmem_free(itx, offsetof(itx_t, itx_lr) +
                    itx->itx_lr.lrc_reclen);
@@ -1116,6 +1222,8 @@ zil_itxg_clean(itxs_t *itxs)
        while ((ian = avl_destroy_nodes(t, &cookie)) != NULL) {
                list = &ian->ia_list;
                while ((itx = list_head(list)) != NULL) {
+                       if (itx->itx_callback != NULL)
+                               itx->itx_callback(itx->itx_callback_data);
                        list_remove(list, itx);
                        kmem_free(itx, offsetof(itx_t, itx_lr) +
                            itx->itx_lr.lrc_reclen);
@@ -1182,6 +1290,8 @@ zil_remove_async(zilog_t *zilog, uint64_t oid)
                mutex_exit(&itxg->itxg_lock);
        }
        while ((itx = list_head(&clean_list)) != NULL) {
+               if (itx->itx_callback != NULL)
+                       itx->itx_callback(itx->itx_callback_data);
                list_remove(&clean_list, itx);
                kmem_free(itx, offsetof(itx_t, itx_lr) +
                    itx->itx_lr.lrc_reclen);
@@ -1212,7 +1322,7 @@ zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx)
        if ((itx->itx_lr.lrc_txtype & ~TX_CI) == TX_RENAME)
                zil_async_to_sync(zilog, itx->itx_oid);
 
-       if (spa_freeze_txg(zilog->zl_spa) !=  UINT64_MAX)
+       if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX)
                txg = ZILTEST_TXG;
        else
                txg = dmu_tx_get_txg(tx);
@@ -1233,7 +1343,8 @@ zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx)
                }
                ASSERT(itxg->itxg_sod == 0);
                itxg->itxg_txg = txg;
-               itxs = itxg->itxg_itxs = kmem_zalloc(sizeof (itxs_t), KM_SLEEP);
+               itxs = itxg->itxg_itxs = kmem_zalloc(sizeof (itxs_t),
+                   KM_PUSHPAGE);
 
                list_create(&itxs->i_sync_list, sizeof (itx_t),
                    offsetof(itx_t, itx_node));
@@ -1253,7 +1364,8 @@ zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx)
 
                ian = avl_find(t, &foid, &where);
                if (ian == NULL) {
-                       ian = kmem_alloc(sizeof (itx_async_node_t), KM_SLEEP);
+                       ian = kmem_alloc(sizeof (itx_async_node_t),
+                           KM_PUSHPAGE);
                        list_create(&ian->ia_list, sizeof (itx_t),
                            offsetof(itx_t, itx_node));
                        ian->ia_foid = foid;
@@ -1263,6 +1375,7 @@ zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx)
        }
 
        itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx);
+       zilog_dirty(zilog, txg);
        mutex_exit(&itxg->itxg_lock);
 
        /* Release the old itxs now we've dropped the lock */
@@ -1272,7 +1385,10 @@ zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx)
 
 /*
  * If there are any in-memory intent log transactions which have now been
- * synced then start up a taskq to free them.
+ * synced then start up a taskq to free them. We should only do this after we
+ * have written out the uberblocks (i.e. txg has been comitted) so that
+ * don't inadvertently clean out in-memory log records that would be required
+ * by zil_commit().
  */
 void
 zil_clean(zilog_t *zilog, uint64_t synced_txg)
@@ -1423,15 +1539,13 @@ zil_commit_writer(zilog_t *zilog)
        }
 
        DTRACE_PROBE1(zil__cw1, zilog_t *, zilog);
-       while ((itx = list_head(&zilog->zl_itx_commit_list))) {
+       for (itx = list_head(&zilog->zl_itx_commit_list); itx != NULL;
+           itx = list_next(&zilog->zl_itx_commit_list, itx)) {
                txg = itx->itx_lr.lrc_txg;
                ASSERT(txg);
 
                if (txg > spa_last_synced_txg(spa) || txg > spa_freeze_txg(spa))
                        lwb = zil_lwb_commit(zilog, itx, lwb);
-               list_remove(&zilog->zl_itx_commit_list, itx);
-               kmem_free(itx, offsetof(itx_t, itx_lr)
-                   + itx->itx_lr.lrc_reclen);
        }
        DTRACE_PROBE1(zil__cw2, zilog_t *, zilog);
 
@@ -1453,6 +1567,17 @@ zil_commit_writer(zilog_t *zilog)
        if (error || lwb == NULL)
                txg_wait_synced(zilog->zl_dmu_pool, 0);
 
+       while ((itx = list_head(&zilog->zl_itx_commit_list))) {
+               txg = itx->itx_lr.lrc_txg;
+               ASSERT(txg);
+
+               if (itx->itx_callback != NULL)
+                       itx->itx_callback(itx->itx_callback_data);
+               list_remove(&zilog->zl_itx_commit_list, itx);
+               kmem_free(itx, offsetof(itx_t, itx_lr)
+                   + itx->itx_lr.lrc_reclen);
+       }
+
        mutex_enter(&zilog->zl_lock);
 
        /*
@@ -1495,6 +1620,8 @@ zil_commit(zilog_t *zilog, uint64_t foid)
        if (zilog->zl_sync == ZFS_SYNC_DISABLED)
                return;
 
+       ZIL_STAT_BUMP(zil_commit_count);
+
        /* move the async itxs for the foid to the sync queues */
        zil_async_to_sync(zilog, foid);
 
@@ -1510,16 +1637,18 @@ zil_commit(zilog_t *zilog, uint64_t foid)
 
        zilog->zl_next_batch++;
        zilog->zl_writer = B_TRUE;
+       ZIL_STAT_BUMP(zil_commit_writer_count);
        zil_commit_writer(zilog);
        zilog->zl_com_batch = mybatch;
        zilog->zl_writer = B_FALSE;
-       mutex_exit(&zilog->zl_lock);
 
        /* wake up one thread to become the next writer */
        cv_signal(&zilog->zl_cv_batch[(mybatch+1) & 1]);
 
        /* wake up all threads waiting for this batch to be committed */
        cv_broadcast(&zilog->zl_cv_batch[mybatch & 1]);
+
+       mutex_exit(&zilog->zl_lock);
 }
 
 /*
@@ -1577,6 +1706,9 @@ zil_sync(zilog_t *zilog, dmu_tx_t *tx)
                zh->zh_log = lwb->lwb_blk;
                if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg)
                        break;
+
+               ASSERT(lwb->lwb_zio == NULL);
+
                list_remove(&zilog->zl_lwb_list, lwb);
                zio_free_zil(spa, txg, &lwb->lwb_blk);
                kmem_cache_free(zil_lwb_cache, lwb);
@@ -1590,6 +1722,19 @@ zil_sync(zilog_t *zilog, dmu_tx_t *tx)
                if (list_head(&zilog->zl_lwb_list) == NULL)
                        BP_ZERO(&zh->zh_log);
        }
+
+       /*
+        * Remove fastwrite on any blocks that have been pre-allocated for
+        * the next commit. This prevents fastwrite counter pollution by
+        * unused, long-lived LWBs.
+        */
+       for (; lwb != NULL; lwb = list_next(&zilog->zl_lwb_list, lwb)) {
+               if (lwb->lwb_fastwrite && !lwb->lwb_zio) {
+                       metaslab_fastwrite_unmark(zilog->zl_spa, &lwb->lwb_blk);
+                       lwb->lwb_fastwrite = 0;
+               }
+       }
+
        mutex_exit(&zilog->zl_lock);
 }
 
@@ -1598,12 +1743,26 @@ zil_init(void)
 {
        zil_lwb_cache = kmem_cache_create("zil_lwb_cache",
            sizeof (struct lwb), 0, NULL, NULL, NULL, NULL, NULL, 0);
+
+       zil_ksp = kstat_create("zfs", 0, "zil", "misc",
+           KSTAT_TYPE_NAMED, sizeof (zil_stats) / sizeof (kstat_named_t),
+           KSTAT_FLAG_VIRTUAL);
+
+       if (zil_ksp != NULL) {
+               zil_ksp->ks_data = &zil_stats;
+               kstat_install(zil_ksp);
+       }
 }
 
 void
 zil_fini(void)
 {
        kmem_cache_destroy(zil_lwb_cache);
+
+       if (zil_ksp != NULL) {
+               kstat_delete(zil_ksp);
+               zil_ksp = NULL;
+       }
 }
 
 void
@@ -1624,7 +1783,7 @@ zil_alloc(objset_t *os, zil_header_t *zh_phys)
        zilog_t *zilog;
        int i;
 
-       zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP);
+       zilog = kmem_zalloc(sizeof (zilog_t), KM_PUSHPAGE);
 
        zilog->zl_header = zh_phys;
        zilog->zl_os = os;
@@ -1664,21 +1823,14 @@ zil_alloc(objset_t *os, zil_header_t *zh_phys)
 void
 zil_free(zilog_t *zilog)
 {
-       lwb_t *head_lwb;
        int i;
 
        zilog->zl_stop_sync = 1;
 
-       /*
-        * After zil_close() there should only be one lwb with a buffer.
-        */
-       head_lwb = list_head(&zilog->zl_lwb_list);
-       if (head_lwb) {
-               ASSERT(head_lwb == list_tail(&zilog->zl_lwb_list));
-               list_remove(&zilog->zl_lwb_list, head_lwb);
-               zio_buf_free(head_lwb->lwb_buf, head_lwb->lwb_sz);
-               kmem_cache_free(zil_lwb_cache, head_lwb);
-       }
+       ASSERT0(zilog->zl_suspend);
+       ASSERT0(zilog->zl_suspending);
+
+       ASSERT(list_is_empty(&zilog->zl_lwb_list));
        list_destroy(&zilog->zl_lwb_list);
 
        avl_destroy(&zilog->zl_vdev_tree);
@@ -1718,6 +1870,10 @@ zil_open(objset_t *os, zil_get_data_t *get_data)
 {
        zilog_t *zilog = dmu_objset_zil(os);
 
+       ASSERT(zilog->zl_clean_taskq == NULL);
+       ASSERT(zilog->zl_get_data == NULL);
+       ASSERT(list_is_empty(&zilog->zl_lwb_list));
+
        zilog->zl_get_data = get_data;
        zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri,
            2, 2, TASKQ_PREPOPULATE);
@@ -1731,7 +1887,7 @@ zil_open(objset_t *os, zil_get_data_t *get_data)
 void
 zil_close(zilog_t *zilog)
 {
-       lwb_t *tail_lwb;
+       lwb_t *lwb;
        uint64_t txg = 0;
 
        zil_commit(zilog, 0); /* commit all itx */
@@ -1743,44 +1899,129 @@ zil_close(zilog_t *zilog)
         * destroy the zl_clean_taskq.
         */
        mutex_enter(&zilog->zl_lock);
-       tail_lwb = list_tail(&zilog->zl_lwb_list);
-       if (tail_lwb != NULL)
-               txg = tail_lwb->lwb_max_txg;
+       lwb = list_tail(&zilog->zl_lwb_list);
+       if (lwb != NULL)
+               txg = lwb->lwb_max_txg;
        mutex_exit(&zilog->zl_lock);
        if (txg)
                txg_wait_synced(zilog->zl_dmu_pool, txg);
+       ASSERT(!zilog_is_dirty(zilog));
 
        taskq_destroy(zilog->zl_clean_taskq);
        zilog->zl_clean_taskq = NULL;
        zilog->zl_get_data = NULL;
+
+       /*
+        * We should have only one LWB left on the list; remove it now.
+        */
+       mutex_enter(&zilog->zl_lock);
+       lwb = list_head(&zilog->zl_lwb_list);
+       if (lwb != NULL) {
+               ASSERT(lwb == list_tail(&zilog->zl_lwb_list));
+               ASSERT(lwb->lwb_zio == NULL);
+               if (lwb->lwb_fastwrite)
+                       metaslab_fastwrite_unmark(zilog->zl_spa, &lwb->lwb_blk);
+               list_remove(&zilog->zl_lwb_list, lwb);
+               zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
+               kmem_cache_free(zil_lwb_cache, lwb);
+       }
+       mutex_exit(&zilog->zl_lock);
 }
 
+static char *suspend_tag = "zil suspending";
+
 /*
  * Suspend an intent log.  While in suspended mode, we still honor
  * synchronous semantics, but we rely on txg_wait_synced() to do it.
- * We suspend the log briefly when taking a snapshot so that the snapshot
- * contains all the data it's supposed to, and has an empty intent log.
+ * On old version pools, we suspend the log briefly when taking a
+ * snapshot so that it will have an empty intent log.
+ *
+ * Long holds are not really intended to be used the way we do here --
+ * held for such a short time.  A concurrent caller of dsl_dataset_long_held()
+ * could fail.  Therefore we take pains to only put a long hold if it is
+ * actually necessary.  Fortunately, it will only be necessary if the
+ * objset is currently mounted (or the ZVOL equivalent).  In that case it
+ * will already have a long hold, so we are not really making things any worse.
+ *
+ * Ideally, we would locate the existing long-holder (i.e. the zfsvfs_t or
+ * zvol_state_t), and use their mechanism to prevent their hold from being
+ * dropped (e.g. VFS_HOLD()).  However, that would be even more pain for
+ * very little gain.
+ *
+ * if cookiep == NULL, this does both the suspend & resume.
+ * Otherwise, it returns with the dataset "long held", and the cookie
+ * should be passed into zil_resume().
  */
 int
-zil_suspend(zilog_t *zilog)
+zil_suspend(const char *osname, void **cookiep)
 {
-       const zil_header_t *zh = zilog->zl_header;
+       objset_t *os;
+       zilog_t *zilog;
+       const zil_header_t *zh;
+       int error;
+
+       error = dmu_objset_hold(osname, suspend_tag, &os);
+       if (error != 0)
+               return (error);
+       zilog = dmu_objset_zil(os);
 
        mutex_enter(&zilog->zl_lock);
+       zh = zilog->zl_header;
+
        if (zh->zh_flags & ZIL_REPLAY_NEEDED) {         /* unplayed log */
                mutex_exit(&zilog->zl_lock);
-               return (EBUSY);
+               dmu_objset_rele(os, suspend_tag);
+               return (SET_ERROR(EBUSY));
+       }
+
+       /*
+        * Don't put a long hold in the cases where we can avoid it.  This
+        * is when there is no cookie so we are doing a suspend & resume
+        * (i.e. called from zil_vdev_offline()), and there's nothing to do
+        * for the suspend because it's already suspended, or there's no ZIL.
+        */
+       if (cookiep == NULL && !zilog->zl_suspending &&
+           (zilog->zl_suspend > 0 || BP_IS_HOLE(&zh->zh_log))) {
+               mutex_exit(&zilog->zl_lock);
+               dmu_objset_rele(os, suspend_tag);
+               return (0);
        }
-       if (zilog->zl_suspend++ != 0) {
+
+       dsl_dataset_long_hold(dmu_objset_ds(os), suspend_tag);
+       dsl_pool_rele(dmu_objset_pool(os), suspend_tag);
+
+       zilog->zl_suspend++;
+
+       if (zilog->zl_suspend > 1) {
                /*
-                * Someone else already began a suspend.
+                * Someone else is already suspending it.
                 * Just wait for them to finish.
                 */
+
                while (zilog->zl_suspending)
                        cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock);
                mutex_exit(&zilog->zl_lock);
+
+               if (cookiep == NULL)
+                       zil_resume(os);
+               else
+                       *cookiep = os;
+               return (0);
+       }
+
+       /*
+        * If there is no pointer to an on-disk block, this ZIL must not
+        * be active (e.g. filesystem not mounted), so there's nothing
+        * to clean up.
+        */
+       if (BP_IS_HOLE(&zh->zh_log)) {
+               ASSERT(cookiep != NULL); /* fast path already handled */
+
+               *cookiep = os;
+               mutex_exit(&zilog->zl_lock);
                return (0);
        }
+
        zilog->zl_suspending = B_TRUE;
        mutex_exit(&zilog->zl_lock);
 
@@ -1793,20 +2034,29 @@ zil_suspend(zilog_t *zilog)
        cv_broadcast(&zilog->zl_cv_suspend);
        mutex_exit(&zilog->zl_lock);
 
+       if (cookiep == NULL)
+               zil_resume(os);
+       else
+               *cookiep = os;
        return (0);
 }
 
 void
-zil_resume(zilog_t *zilog)
+zil_resume(void *cookie)
 {
+       objset_t *os = cookie;
+       zilog_t *zilog = dmu_objset_zil(os);
+
        mutex_enter(&zilog->zl_lock);
        ASSERT(zilog->zl_suspend != 0);
        zilog->zl_suspend--;
        mutex_exit(&zilog->zl_lock);
+       dsl_dataset_long_rele(dmu_objset_ds(os), suspend_tag);
+       dsl_dataset_rele(dmu_objset_ds(os), suspend_tag);
 }
 
 typedef struct zil_replay_arg {
-       zil_replay_func_t **zr_replay;
+       zil_replay_func_t *zr_replay;
        void            *zr_arg;
        boolean_t       zr_byteswap;
        char            *zr_lr;
@@ -1875,7 +2125,7 @@ zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg)
        if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) {
                error = zil_read_log_data(zilog, (lr_write_t *)lr,
                    zr->zr_lr + reclen);
-               if (error)
+               if (error != 0)
                        return (zil_replay_error(zilog, lr, error));
        }
 
@@ -1896,7 +2146,7 @@ zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg)
         * is updated if we are in replay mode.
         */
        error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lr, zr->zr_byteswap);
-       if (error) {
+       if (error != 0) {
                /*
                 * The DMU's dnode layer doesn't see removes until the txg
                 * commits, so a subsequent claim can spuriously fail with
@@ -1906,7 +2156,7 @@ zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg)
                 */
                txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0);
                error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lr, B_FALSE);
-               if (error)
+               if (error != 0)
                        return (zil_replay_error(zilog, lr, error));
        }
        return (0);
@@ -1925,7 +2175,7 @@ zil_incr_blks(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
  * If this dataset has a non-empty intent log, replay it and destroy it.
  */
 void
-zil_replay(objset_t *os, void *arg, zil_replay_func_t *replay_func[TX_MAX_TYPE])
+zil_replay(objset_t *os, void *arg, zil_replay_func_t replay_func[TX_MAX_TYPE])
 {
        zilog_t *zilog = dmu_objset_zil(os);
        const zil_header_t *zh = zilog->zl_header;
@@ -1939,7 +2189,7 @@ zil_replay(objset_t *os, void *arg, zil_replay_func_t *replay_func[TX_MAX_TYPE])
        zr.zr_replay = replay_func;
        zr.zr_arg = arg;
        zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log);
-       zr.zr_lr = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP);
+       zr.zr_lr = vmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_PUSHPAGE);
 
        /*
         * Wait for in-progress removes to sync before starting replay.
@@ -1951,7 +2201,7 @@ zil_replay(objset_t *os, void *arg, zil_replay_func_t *replay_func[TX_MAX_TYPE])
        ASSERT(zilog->zl_replay_blks == 0);
        (void) zil_parse(zilog, zil_incr_blks, zil_replay_log_record, &zr,
            zh->zh_claim_txg);
-       kmem_free(zr.zr_lr, 2 * SPA_MAXBLOCKSIZE);
+       vmem_free(zr.zr_lr, 2 * SPA_MAXBLOCKSIZE);
 
        zil_destroy(zilog, B_FALSE);
        txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg);
@@ -1978,19 +2228,21 @@ zil_replaying(zilog_t *zilog, dmu_tx_t *tx)
 int
 zil_vdev_offline(const char *osname, void *arg)
 {
-       objset_t *os;
-       zilog_t *zilog;
        int error;
 
-       error = dmu_objset_hold(osname, FTAG, &os);
-       if (error)
-               return (error);
-
-       zilog = dmu_objset_zil(os);
-       if (zil_suspend(zilog) != 0)
-               error = EEXIST;
-       else
-               zil_resume(zilog);
-       dmu_objset_rele(os, FTAG);
-       return (error);
+       error = zil_suspend(osname, NULL);
+       if (error != 0)
+               return (SET_ERROR(EEXIST));
+       return (0);
 }
+
+#if defined(_KERNEL) && defined(HAVE_SPL)
+module_param(zil_replay_disable, int, 0644);
+MODULE_PARM_DESC(zil_replay_disable, "Disable intent logging replay");
+
+module_param(zfs_nocacheflush, int, 0644);
+MODULE_PARM_DESC(zfs_nocacheflush, "Disable cache flushes");
+
+module_param(zil_slog_limit, ulong, 0644);
+MODULE_PARM_DESC(zil_slog_limit, "Max commit bytes to separate log device");
+#endif