Illumos #3006
[zfs.git] / module / zfs / zil.c
index 4aa4d10..64de856 100644 (file)
@@ -20,6 +20,7 @@
  */
 /*
  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012 by Delphix. All rights reserved.
  */
 
 /* Portions Copyright 2010 Robert Milkowski */
 #include <sys/zil.h>
 #include <sys/zil_impl.h>
 #include <sys/dsl_dataset.h>
-#include <sys/vdev.h>
+#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
  */
 
 /*
+ * See zil.h for more information about these fields.
+ */
+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;
+
+/*
  * This global ZIL switch affects all pools
  */
 int zil_replay_disable = 0;    /* disable intent logging replay */
@@ -74,16 +97,25 @@ int zil_replay_disable = 0;    /* disable intent logging replay */
  * 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;
 
-static boolean_t zil_empty(zilog_t *zilog);
+static void zil_async_to_sync(zilog_t *zilog, uint64_t foid);
 
 #define        LWB_EMPTY(lwb) ((BP_GET_LSIZE(&lwb->lwb_blk) - \
     sizeof (zil_chain_t)) == (lwb->lwb_sz - lwb->lwb_nused))
 
 
+/*
+ * ziltest is by and large an ugly hack, but very useful in
+ * checking replay without tedious work.
+ * When running ziltest we want to keep all itx's and so maintain
+ * a single list in the zl_itxg[] that uses a high txg: ZILTEST_TXG
+ * We subtract TXG_CONCURRENT_STATES to allow for common code.
+ */
+#define        ZILTEST_TXG (UINT64_MAX - TXG_CONCURRENT_STATES)
+
 static int
 zil_bp_compare(const void *x1, const void *x2)
 {
@@ -134,7 +166,7 @@ zil_bp_tree_add(zilog_t *zilog, const blkptr_t *bp)
        if (avl_find(t, dva, &where) != NULL)
                return (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);
 
@@ -287,6 +319,8 @@ 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));
+
        /*
         * Old logs didn't record the maximum zh_claim_lr_seq.
         */
@@ -308,7 +342,7 @@ zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func,
        for (blk = zh->zh_log; !BP_IS_HOLE(&blk); blk = next_blk) {
                uint64_t blk_seq = blk.blk_cksum.zc_word[ZIL_ZC_SEQ];
                int reclen;
-               char *end;
+               char *end = NULL;
 
                if (blk_seq > claim_blk_seq)
                        break;
@@ -418,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;
@@ -445,6 +480,38 @@ zil_alloc_lwb(zilog_t *zilog, blkptr_t *bp, uint64_t txg)
 }
 
 /*
+ * 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) == 0) {
+               /* 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.
  */
 static lwb_t *
@@ -456,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.
@@ -483,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);
@@ -494,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
@@ -551,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);
@@ -560,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)
 {
@@ -631,6 +711,7 @@ zil_check_log_chain(const char *osname, void *tx)
 {
        zilog_t *zilog;
        objset_t *os;
+       blkptr_t *bp;
        int error;
 
        ASSERT(tx == NULL);
@@ -642,6 +723,29 @@ zil_check_log_chain(const char *osname, void *tx)
        }
 
        zilog = dmu_objset_zil(os);
+       bp = (blkptr_t *)&zilog->zl_header->zh_log;
+
+       /*
+        * Check the first block and determine if it's on a log device
+        * which may have been removed or faulted prior to loading this
+        * pool.  If so, there's no point in checking the rest of the log
+        * as its content should have already been synced to the pool.
+        */
+       if (!BP_IS_HOLE(bp)) {
+               vdev_t *vd;
+               boolean_t valid = B_TRUE;
+
+               spa_config_enter(os->os_spa, SCL_STATE, FTAG, RW_READER);
+               vd = vdev_lookup_top(os->os_spa, DVA_GET_VDEV(&bp->blk_dva[0]));
+               if (vd->vdev_islog && vdev_is_dead(vd))
+                       valid = vdev_log_state_valid(vd);
+               spa_config_exit(os->os_spa, SCL_STATE, FTAG);
+
+               if (!valid) {
+                       dmu_objset_rele(os, FTAG);
+                       return (0);
+               }
+       }
 
        /*
         * Because tx == NULL, zil_claim_log_block() will not actually claim
@@ -661,8 +765,8 @@ zil_check_log_chain(const char *osname, void *tx)
 static int
 zil_vdev_compare(const void *x1, const void *x2)
 {
-       uint64_t v1 = ((zil_vdev_node_t *)x1)->zv_vdev;
-       uint64_t v2 = ((zil_vdev_node_t *)x2)->zv_vdev;
+       const uint64_t v1 = ((zil_vdev_node_t *)x1)->zv_vdev;
+       const uint64_t v2 = ((zil_vdev_node_t *)x2)->zv_vdev;
 
        if (v1 < v2)
                return (-1);
@@ -695,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);
                }
@@ -703,7 +807,7 @@ zil_add_block(zilog_t *zilog, const blkptr_t *bp)
        mutex_exit(&zilog->zl_vdev_lock);
 }
 
-void
+static void
 zil_flush_vdevs(zilog_t *zilog)
 {
        spa_t *spa = zilog->zl_spa;
@@ -769,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);
@@ -797,12 +903,21 @@ 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);
+                   ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE |
+                   ZIO_FLAG_FASTWRITE, &zb);
        }
+       mutex_exit(&zilog->zl_lock);
 }
 
 /*
@@ -819,14 +934,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.
@@ -843,6 +957,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;
@@ -898,9 +1013,18 @@ 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(zilog));
+       use_slog = USE_SLOG(zilog);
+       error = zio_alloc_zil(spa, txg, bp, zil_blksz, USE_SLOG(zilog));
+       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) {
                ASSERT3U(bp->blk_birth, ==, txg);
                bp->blk_cksum = lwb->lwb_blk.blk_cksum;
@@ -909,7 +1033,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);
@@ -957,6 +1081,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(
@@ -986,13 +1112,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;
 
@@ -1000,9 +1131,13 @@ 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);
@@ -1028,7 +1163,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);
 }
@@ -1040,11 +1175,13 @@ 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 */
 
        return (itx);
 }
@@ -1055,190 +1192,366 @@ zil_itx_destroy(itx_t *itx)
        kmem_free(itx, offsetof(itx_t, itx_lr) + itx->itx_lr.lrc_reclen);
 }
 
-uint64_t
-zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx)
+/*
+ * Free up the sync and async itxs. The itxs_t has already been detached
+ * so no locks are needed.
+ */
+static void
+zil_itxg_clean(itxs_t *itxs)
 {
-       uint64_t seq;
+       itx_t *itx;
+       list_t *list;
+       avl_tree_t *t;
+       void *cookie;
+       itx_async_node_t *ian;
+
+       list = &itxs->i_sync_list;
+       while ((itx = list_head(list)) != NULL) {
+               list_remove(list, itx);
+               kmem_free(itx, offsetof(itx_t, itx_lr) +
+                   itx->itx_lr.lrc_reclen);
+       }
 
-       ASSERT(itx->itx_lr.lrc_seq == 0);
-       ASSERT(!zilog->zl_replay);
+       cookie = NULL;
+       t = &itxs->i_async_tree;
+       while ((ian = avl_destroy_nodes(t, &cookie)) != NULL) {
+               list = &ian->ia_list;
+               while ((itx = list_head(list)) != NULL) {
+                       list_remove(list, itx);
+                       kmem_free(itx, offsetof(itx_t, itx_lr) +
+                           itx->itx_lr.lrc_reclen);
+               }
+               list_destroy(list);
+               kmem_free(ian, sizeof (itx_async_node_t));
+       }
+       avl_destroy(t);
 
-       mutex_enter(&zilog->zl_lock);
-       list_insert_tail(&zilog->zl_itx_list, itx);
-       zilog->zl_itx_list_sz += itx->itx_sod;
-       itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx);
-       itx->itx_lr.lrc_seq = seq = ++zilog->zl_itx_seq;
-       mutex_exit(&zilog->zl_lock);
+       kmem_free(itxs, sizeof (itxs_t));
+}
+
+static int
+zil_aitx_compare(const void *x1, const void *x2)
+{
+       const uint64_t o1 = ((itx_async_node_t *)x1)->ia_foid;
+       const uint64_t o2 = ((itx_async_node_t *)x2)->ia_foid;
+
+       if (o1 < o2)
+               return (-1);
+       if (o1 > o2)
+               return (1);
 
-       return (seq);
+       return (0);
 }
 
 /*
- * Free up all in-memory intent log transactions that have now been synced.
+ * Remove all async itx with the given oid.
  */
 static void
-zil_itx_clean(zilog_t *zilog)
+zil_remove_async(zilog_t *zilog, uint64_t oid)
 {
-       uint64_t synced_txg = spa_last_synced_txg(zilog->zl_spa);
-       uint64_t freeze_txg = spa_freeze_txg(zilog->zl_spa);
+       uint64_t otxg, txg;
+       itx_async_node_t *ian;
+       avl_tree_t *t;
+       avl_index_t where;
        list_t clean_list;
        itx_t *itx;
 
+       ASSERT(oid != 0);
        list_create(&clean_list, sizeof (itx_t), offsetof(itx_t, itx_node));
 
-       mutex_enter(&zilog->zl_lock);
-       /* wait for a log writer to finish walking list */
-       while (zilog->zl_writer) {
-               cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
-       }
+       if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */
+               otxg = ZILTEST_TXG;
+       else
+               otxg = spa_last_synced_txg(zilog->zl_spa) + 1;
 
-       /*
-        * Move the sync'd log transactions to a separate list so we can call
-        * kmem_free without holding the zl_lock.
-        *
-        * There is no need to set zl_writer as we don't drop zl_lock here
-        */
-       while ((itx = list_head(&zilog->zl_itx_list)) != NULL &&
-           itx->itx_lr.lrc_txg <= MIN(synced_txg, freeze_txg)) {
-               list_remove(&zilog->zl_itx_list, itx);
-               zilog->zl_itx_list_sz -= itx->itx_sod;
-               list_insert_tail(&clean_list, itx);
-       }
-       cv_broadcast(&zilog->zl_cv_writer);
-       mutex_exit(&zilog->zl_lock);
+       for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) {
+               itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK];
+
+               mutex_enter(&itxg->itxg_lock);
+               if (itxg->itxg_txg != txg) {
+                       mutex_exit(&itxg->itxg_lock);
+                       continue;
+               }
 
-       /* destroy sync'd log transactions */
+               /*
+                * Locate the object node and append its list.
+                */
+               t = &itxg->itxg_itxs->i_async_tree;
+               ian = avl_find(t, &oid, &where);
+               if (ian != NULL)
+                       list_move_tail(&clean_list, &ian->ia_list);
+               mutex_exit(&itxg->itxg_lock);
+       }
        while ((itx = list_head(&clean_list)) != NULL) {
                list_remove(&clean_list, itx);
-               zil_itx_destroy(itx);
+               kmem_free(itx, offsetof(itx_t, itx_lr) +
+                   itx->itx_lr.lrc_reclen);
        }
        list_destroy(&clean_list);
 }
 
+void
+zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx)
+{
+       uint64_t txg;
+       itxg_t *itxg;
+       itxs_t *itxs, *clean = NULL;
+
+       /*
+        * Object ids can be re-instantiated in the next txg so
+        * remove any async transactions to avoid future leaks.
+        * This can happen if a fsync occurs on the re-instantiated
+        * object for a WR_INDIRECT or WR_NEED_COPY write, which gets
+        * the new file data and flushes a write record for the old object.
+        */
+       if ((itx->itx_lr.lrc_txtype & ~TX_CI) == TX_REMOVE)
+               zil_remove_async(zilog, itx->itx_oid);
+
+       /*
+        * Ensure the data of a renamed file is committed before the rename.
+        */
+       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)
+               txg = ZILTEST_TXG;
+       else
+               txg = dmu_tx_get_txg(tx);
+
+       itxg = &zilog->zl_itxg[txg & TXG_MASK];
+       mutex_enter(&itxg->itxg_lock);
+       itxs = itxg->itxg_itxs;
+       if (itxg->itxg_txg != txg) {
+               if (itxs != NULL) {
+                       /*
+                        * The zil_clean callback hasn't got around to cleaning
+                        * this itxg. Save the itxs for release below.
+                        * This should be rare.
+                        */
+                       atomic_add_64(&zilog->zl_itx_list_sz, -itxg->itxg_sod);
+                       itxg->itxg_sod = 0;
+                       clean = itxg->itxg_itxs;
+               }
+               ASSERT(itxg->itxg_sod == 0);
+               itxg->itxg_txg = txg;
+               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));
+               avl_create(&itxs->i_async_tree, zil_aitx_compare,
+                   sizeof (itx_async_node_t),
+                   offsetof(itx_async_node_t, ia_node));
+       }
+       if (itx->itx_sync) {
+               list_insert_tail(&itxs->i_sync_list, itx);
+               atomic_add_64(&zilog->zl_itx_list_sz, itx->itx_sod);
+               itxg->itxg_sod += itx->itx_sod;
+       } else {
+               avl_tree_t *t = &itxs->i_async_tree;
+               uint64_t foid = ((lr_ooo_t *)&itx->itx_lr)->lr_foid;
+               itx_async_node_t *ian;
+               avl_index_t where;
+
+               ian = avl_find(t, &foid, &where);
+               if (ian == NULL) {
+                       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;
+                       avl_insert(t, ian, where);
+               }
+               list_insert_tail(&ian->ia_list, itx);
+       }
+
+       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 */
+       if (clean != NULL)
+               zil_itxg_clean(clean);
+}
+
 /*
  * 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)
+zil_clean(zilog_t *zilog, uint64_t synced_txg)
 {
-       itx_t *itx;
+       itxg_t *itxg = &zilog->zl_itxg[synced_txg & TXG_MASK];
+       itxs_t *clean_me;
 
-       mutex_enter(&zilog->zl_lock);
-       itx = list_head(&zilog->zl_itx_list);
-       if ((itx != NULL) &&
-           (itx->itx_lr.lrc_txg <= spa_last_synced_txg(zilog->zl_spa))) {
-               (void) taskq_dispatch(zilog->zl_clean_taskq,
-                   (task_func_t *)zil_itx_clean, zilog, TQ_NOSLEEP);
+       mutex_enter(&itxg->itxg_lock);
+       if (itxg->itxg_itxs == NULL || itxg->itxg_txg == ZILTEST_TXG) {
+               mutex_exit(&itxg->itxg_lock);
+               return;
        }
-       mutex_exit(&zilog->zl_lock);
+       ASSERT3U(itxg->itxg_txg, <=, synced_txg);
+       ASSERT(itxg->itxg_txg != 0);
+       ASSERT(zilog->zl_clean_taskq != NULL);
+       atomic_add_64(&zilog->zl_itx_list_sz, -itxg->itxg_sod);
+       itxg->itxg_sod = 0;
+       clean_me = itxg->itxg_itxs;
+       itxg->itxg_itxs = NULL;
+       itxg->itxg_txg = 0;
+       mutex_exit(&itxg->itxg_lock);
+       /*
+        * Preferably start a task queue to free up the old itxs but
+        * if taskq_dispatch can't allocate resources to do that then
+        * free it in-line. This should be rare. Note, using TQ_SLEEP
+        * created a bad performance problem.
+        */
+       if (taskq_dispatch(zilog->zl_clean_taskq,
+           (void (*)(void *))zil_itxg_clean, clean_me, TQ_NOSLEEP) == 0)
+               zil_itxg_clean(clean_me);
 }
 
+/*
+ * Get the list of itxs to commit into zl_itx_commit_list.
+ */
+static void
+zil_get_commit_list(zilog_t *zilog)
+{
+       uint64_t otxg, txg;
+       list_t *commit_list = &zilog->zl_itx_commit_list;
+       uint64_t push_sod = 0;
+
+       if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */
+               otxg = ZILTEST_TXG;
+       else
+               otxg = spa_last_synced_txg(zilog->zl_spa) + 1;
+
+       for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) {
+               itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK];
+
+               mutex_enter(&itxg->itxg_lock);
+               if (itxg->itxg_txg != txg) {
+                       mutex_exit(&itxg->itxg_lock);
+                       continue;
+               }
+
+               list_move_tail(commit_list, &itxg->itxg_itxs->i_sync_list);
+               push_sod += itxg->itxg_sod;
+               itxg->itxg_sod = 0;
+
+               mutex_exit(&itxg->itxg_lock);
+       }
+       atomic_add_64(&zilog->zl_itx_list_sz, -push_sod);
+}
+
+/*
+ * Move the async itxs for a specified object to commit into sync lists.
+ */
 static void
-zil_commit_writer(zilog_t *zilog, uint64_t seq, uint64_t foid)
+zil_async_to_sync(zilog_t *zilog, uint64_t foid)
+{
+       uint64_t otxg, txg;
+       itx_async_node_t *ian;
+       avl_tree_t *t;
+       avl_index_t where;
+
+       if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */
+               otxg = ZILTEST_TXG;
+       else
+               otxg = spa_last_synced_txg(zilog->zl_spa) + 1;
+
+       for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) {
+               itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK];
+
+               mutex_enter(&itxg->itxg_lock);
+               if (itxg->itxg_txg != txg) {
+                       mutex_exit(&itxg->itxg_lock);
+                       continue;
+               }
+
+               /*
+                * If a foid is specified then find that node and append its
+                * list. Otherwise walk the tree appending all the lists
+                * to the sync list. We add to the end rather than the
+                * beginning to ensure the create has happened.
+                */
+               t = &itxg->itxg_itxs->i_async_tree;
+               if (foid != 0) {
+                       ian = avl_find(t, &foid, &where);
+                       if (ian != NULL) {
+                               list_move_tail(&itxg->itxg_itxs->i_sync_list,
+                                   &ian->ia_list);
+                       }
+               } else {
+                       void *cookie = NULL;
+
+                       while ((ian = avl_destroy_nodes(t, &cookie)) != NULL) {
+                               list_move_tail(&itxg->itxg_itxs->i_sync_list,
+                                   &ian->ia_list);
+                               list_destroy(&ian->ia_list);
+                               kmem_free(ian, sizeof (itx_async_node_t));
+                       }
+               }
+               mutex_exit(&itxg->itxg_lock);
+       }
+}
+
+static void
+zil_commit_writer(zilog_t *zilog)
 {
        uint64_t txg;
-       uint64_t commit_seq = 0;
-       itx_t *itx, *itx_next;
+       itx_t *itx;
        lwb_t *lwb;
-       spa_t *spa;
+       spa_t *spa = zilog->zl_spa;
        int error = 0;
 
-       zilog->zl_writer = B_TRUE;
        ASSERT(zilog->zl_root_zio == NULL);
-       spa = zilog->zl_spa;
+
+       mutex_exit(&zilog->zl_lock);
+
+       zil_get_commit_list(zilog);
+
+       /*
+        * Return if there's nothing to commit before we dirty the fs by
+        * calling zil_create().
+        */
+       if (list_head(&zilog->zl_itx_commit_list) == NULL) {
+               mutex_enter(&zilog->zl_lock);
+               return;
+       }
 
        if (zilog->zl_suspend) {
                lwb = NULL;
        } else {
                lwb = list_tail(&zilog->zl_lwb_list);
-               if (lwb == NULL) {
-                       /*
-                        * Return if there's nothing to flush before we
-                        * dirty the fs by calling zil_create()
-                        */
-                       if (list_is_empty(&zilog->zl_itx_list)) {
-                               zilog->zl_writer = B_FALSE;
-                               return;
-                       }
-                       mutex_exit(&zilog->zl_lock);
+               if (lwb == NULL)
                        lwb = zil_create(zilog);
-                       mutex_enter(&zilog->zl_lock);
-               }
        }
-       ASSERT(lwb == NULL || lwb->lwb_zio == NULL);
 
-       /* Loop through in-memory log transactions filling log blocks. */
        DTRACE_PROBE1(zil__cw1, zilog_t *, zilog);
-
-       for (itx = list_head(&zilog->zl_itx_list); itx; itx = itx_next) {
-               /*
-                * Save the next pointer.  Even though we drop zl_lock below,
-                * all threads that can remove itx list entries (other writers
-                * and zil_itx_clean()) can't do so until they have zl_writer.
-                */
-               itx_next = list_next(&zilog->zl_itx_list, itx);
-
-               /*
-                * Determine whether to push this itx.
-                * Push all transactions related to specified foid and
-                * all other transactions except those that can be logged
-                * out of order (TX_WRITE, TX_TRUNCATE, TX_SETATTR, TX_ACL)
-                * for all other files.
-                *
-                * If foid == 0 (meaning "push all foids") or
-                * itx->itx_sync is set (meaning O_[D]SYNC), push regardless.
-                */
-               if (foid != 0 && !itx->itx_sync &&
-                   TX_OOO(itx->itx_lr.lrc_txtype) &&
-                   ((lr_ooo_t *)&itx->itx_lr)->lr_foid != foid)
-                       continue; /* skip this record */
-
-               if ((itx->itx_lr.lrc_seq > seq) &&
-                   ((lwb == NULL) || (LWB_EMPTY(lwb)) ||
-                   (lwb->lwb_nused + itx->itx_sod > lwb->lwb_sz)))
-                       break;
-
-               list_remove(&zilog->zl_itx_list, itx);
-               zilog->zl_itx_list_sz -= itx->itx_sod;
-
-               mutex_exit(&zilog->zl_lock);
-
+       while ((itx = list_head(&zilog->zl_itx_commit_list))) {
                txg = itx->itx_lr.lrc_txg;
                ASSERT(txg);
 
-               if (txg > spa_last_synced_txg(spa) ||
-                   txg > spa_freeze_txg(spa))
+               if (txg > spa_last_synced_txg(spa) || txg > spa_freeze_txg(spa))
                        lwb = zil_lwb_commit(zilog, itx, lwb);
-
-               zil_itx_destroy(itx);
-
-               mutex_enter(&zilog->zl_lock);
+               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);
-       /* determine commit sequence number */
-       itx = list_head(&zilog->zl_itx_list);
-       if (itx)
-               commit_seq = itx->itx_lr.lrc_seq - 1;
-       else
-               commit_seq = zilog->zl_itx_seq;
-       mutex_exit(&zilog->zl_lock);
 
        /* write the last block out */
        if (lwb != NULL && lwb->lwb_zio != NULL)
                lwb = zil_lwb_write_start(zilog, lwb);
 
-       zilog->zl_prev_used = zilog->zl_cur_used;
        zilog->zl_cur_used = 0;
 
        /*
         * Wait if necessary for the log blocks to be on stable storage.
         */
        if (zilog->zl_root_zio) {
-               DTRACE_PROBE1(zil__cw3, zilog_t *, zilog);
                error = zio_wait(zilog->zl_root_zio);
                zilog->zl_root_zio = NULL;
-               DTRACE_PROBE1(zil__cw4, zilog_t *, zilog);
                zil_flush_vdevs(zilog);
        }
 
@@ -1246,10 +1559,6 @@ zil_commit_writer(zilog_t *zilog, uint64_t seq, uint64_t foid)
                txg_wait_synced(zilog->zl_dmu_pool, 0);
 
        mutex_enter(&zilog->zl_lock);
-       zilog->zl_writer = B_FALSE;
-
-       ASSERT3U(commit_seq, >=, zilog->zl_commit_seq);
-       zilog->zl_commit_seq = commit_seq;
 
        /*
         * Remember the highest committed log sequence number for ztest.
@@ -1261,58 +1570,65 @@ zil_commit_writer(zilog_t *zilog, uint64_t seq, uint64_t foid)
 }
 
 /*
- * Push zfs transactions to stable storage up to the supplied sequence number.
+ * Commit zfs transactions to stable storage.
  * If foid is 0 push out all transactions, otherwise push only those
- * for that file or might have been used to create that file.
+ * for that object or might reference that object.
+ *
+ * itxs are committed in batches. In a heavily stressed zil there will be
+ * a commit writer thread who is writing out a bunch of itxs to the log
+ * for a set of committing threads (cthreads) in the same batch as the writer.
+ * Those cthreads are all waiting on the same cv for that batch.
+ *
+ * There will also be a different and growing batch of threads that are
+ * waiting to commit (qthreads). When the committing batch completes
+ * a transition occurs such that the cthreads exit and the qthreads become
+ * cthreads. One of the new cthreads becomes the writer thread for the
+ * batch. Any new threads arriving become new qthreads.
+ *
+ * Only 2 condition variables are needed and there's no transition
+ * between the two cvs needed. They just flip-flop between qthreads
+ * and cthreads.
+ *
+ * Using this scheme we can efficiently wakeup up only those threads
+ * that have been committed.
  */
 void
-zil_commit(zilog_t *zilog, uint64_t seq, uint64_t foid)
+zil_commit(zilog_t *zilog, uint64_t foid)
 {
-       if (zilog->zl_sync == ZFS_SYNC_DISABLED || seq == 0)
+       uint64_t mybatch;
+
+       if (zilog->zl_sync == ZFS_SYNC_DISABLED)
                return;
 
-       mutex_enter(&zilog->zl_lock);
+       ZIL_STAT_BUMP(zil_commit_count);
 
-       seq = MIN(seq, zilog->zl_itx_seq);      /* cap seq at largest itx seq */
+       /* move the async itxs for the foid to the sync queues */
+       zil_async_to_sync(zilog, foid);
 
+       mutex_enter(&zilog->zl_lock);
+       mybatch = zilog->zl_next_batch;
        while (zilog->zl_writer) {
-               cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
-               if (seq <= zilog->zl_commit_seq) {
+               cv_wait(&zilog->zl_cv_batch[mybatch & 1], &zilog->zl_lock);
+               if (mybatch <= zilog->zl_com_batch) {
                        mutex_exit(&zilog->zl_lock);
                        return;
                }
        }
-       zil_commit_writer(zilog, seq, foid); /* drops zl_lock */
-       /* wake up others waiting on the commit */
-       cv_broadcast(&zilog->zl_cv_writer);
-       mutex_exit(&zilog->zl_lock);
-}
 
-/*
- * Report whether all transactions are committed.
- */
-static boolean_t
-zil_is_committed(zilog_t *zilog)
-{
-       lwb_t *lwb;
-       boolean_t committed;
-
-       mutex_enter(&zilog->zl_lock);
+       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;
 
-       while (zilog->zl_writer)
-               cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
+       /* wake up one thread to become the next writer */
+       cv_signal(&zilog->zl_cv_batch[(mybatch+1) & 1]);
 
-       if (!list_is_empty(&zilog->zl_itx_list))
-               committed = B_FALSE;            /* unpushed transactions */
-       else if ((lwb = list_head(&zilog->zl_lwb_list)) == NULL)
-               committed = B_TRUE;             /* intent log never used */
-       else if (list_next(&zilog->zl_lwb_list, lwb) != NULL)
-               committed = B_FALSE;            /* zil_sync() not done yet */
-       else
-               committed = B_TRUE;             /* everything synced */
+       /* wake up all threads waiting for this batch to be committed */
+       cv_broadcast(&zilog->zl_cv_batch[mybatch & 1]);
 
        mutex_exit(&zilog->zl_lock);
-       return (committed);
 }
 
 /*
@@ -1370,6 +1686,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);
@@ -1383,6 +1702,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);
 }
 
@@ -1391,12 +1723,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
@@ -1415,8 +1761,9 @@ zilog_t *
 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;
@@ -1425,15 +1772,21 @@ zil_alloc(objset_t *os, zil_header_t *zh_phys)
        zilog->zl_destroy_txg = TXG_INITIAL - 1;
        zilog->zl_logbias = dmu_objset_logbias(os);
        zilog->zl_sync = dmu_objset_syncprop(os);
+       zilog->zl_next_batch = 1;
 
        mutex_init(&zilog->zl_lock, NULL, MUTEX_DEFAULT, NULL);
 
-       list_create(&zilog->zl_itx_list, sizeof (itx_t),
-           offsetof(itx_t, itx_node));
+       for (i = 0; i < TXG_SIZE; i++) {
+               mutex_init(&zilog->zl_itxg[i].itxg_lock, NULL,
+                   MUTEX_DEFAULT, NULL);
+       }
 
        list_create(&zilog->zl_lwb_list, sizeof (lwb_t),
            offsetof(lwb_t, lwb_node));
 
+       list_create(&zilog->zl_itx_commit_list, sizeof (itx_t),
+           offsetof(itx_t, itx_node));
+
        mutex_init(&zilog->zl_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
 
        avl_create(&zilog->zl_vdev_tree, zil_vdev_compare,
@@ -1441,6 +1794,8 @@ zil_alloc(objset_t *os, zil_header_t *zh_phys)
 
        cv_init(&zilog->zl_cv_writer, NULL, CV_DEFAULT, NULL);
        cv_init(&zilog->zl_cv_suspend, NULL, CV_DEFAULT, NULL);
+       cv_init(&zilog->zl_cv_batch[0], NULL, CV_DEFAULT, NULL);
+       cv_init(&zilog->zl_cv_batch[1], NULL, CV_DEFAULT, NULL);
 
        return (zilog);
 }
@@ -1448,27 +1803,38 @@ zil_alloc(objset_t *os, zil_header_t *zh_phys)
 void
 zil_free(zilog_t *zilog)
 {
-       lwb_t *lwb;
+       int i;
 
        zilog->zl_stop_sync = 1;
 
-       while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) {
-               list_remove(&zilog->zl_lwb_list, lwb);
-               if (lwb->lwb_buf != NULL)
-                       zio_buf_free(lwb->lwb_buf, lwb->lwb_sz);
-               kmem_cache_free(zil_lwb_cache, lwb);
-       }
+       ASSERT(list_is_empty(&zilog->zl_lwb_list));
        list_destroy(&zilog->zl_lwb_list);
 
        avl_destroy(&zilog->zl_vdev_tree);
        mutex_destroy(&zilog->zl_vdev_lock);
 
-       ASSERT(list_head(&zilog->zl_itx_list) == NULL);
-       list_destroy(&zilog->zl_itx_list);
+       ASSERT(list_is_empty(&zilog->zl_itx_commit_list));
+       list_destroy(&zilog->zl_itx_commit_list);
+
+       for (i = 0; i < TXG_SIZE; i++) {
+               /*
+                * It's possible for an itx to be generated that doesn't dirty
+                * a txg (e.g. ztest TX_TRUNCATE). So there's no zil_clean()
+                * callback to remove the entry. We remove those here.
+                *
+                * Also free up the ziltest itxs.
+                */
+               if (zilog->zl_itxg[i].itxg_itxs)
+                       zil_itxg_clean(zilog->zl_itxg[i].itxg_itxs);
+               mutex_destroy(&zilog->zl_itxg[i].itxg_lock);
+       }
+
        mutex_destroy(&zilog->zl_lock);
 
        cv_destroy(&zilog->zl_cv_writer);
        cv_destroy(&zilog->zl_cv_suspend);
+       cv_destroy(&zilog->zl_cv_batch[0]);
+       cv_destroy(&zilog->zl_cv_batch[1]);
 
        kmem_free(zilog, sizeof (zilog_t));
 }
@@ -1481,6 +1847,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);
@@ -1494,26 +1864,45 @@ zil_open(objset_t *os, zil_get_data_t *get_data)
 void
 zil_close(zilog_t *zilog)
 {
+       lwb_t *lwb;
+       uint64_t txg = 0;
+
+       zil_commit(zilog, 0); /* commit all itx */
+
        /*
-        * If the log isn't already committed, mark the objset dirty
-        * (so zil_sync() will be called) and wait for that txg to sync.
+        * The lwb_max_txg for the stubby lwb will reflect the last activity
+        * for the zil.  After a txg_wait_synced() on the txg we know all the
+        * callbacks have occurred that may clean the zil.  Only then can we
+        * destroy the zl_clean_taskq.
         */
-       if (!zil_is_committed(zilog)) {
-               uint64_t txg;
-               dmu_tx_t *tx = dmu_tx_create(zilog->zl_os);
-               VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0);
-               dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
-               txg = dmu_tx_get_txg(tx);
-               dmu_tx_commit(tx);
+       mutex_enter(&zilog->zl_lock);
+       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;
 
-       zil_itx_clean(zilog);
-       ASSERT(list_head(&zilog->zl_itx_list) == 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);
 }
 
 /*
@@ -1545,15 +1934,7 @@ zil_suspend(zilog_t *zilog)
        zilog->zl_suspending = B_TRUE;
        mutex_exit(&zilog->zl_lock);
 
-       zil_commit(zilog, UINT64_MAX, 0);
-
-       /*
-        * Wait for any in-flight log writes to complete.
-        */
-       mutex_enter(&zilog->zl_lock);
-       while (zilog->zl_writer)
-               cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock);
-       mutex_exit(&zilog->zl_lock);
+       zil_commit(zilog, 0);
 
        zil_destroy(zilog, B_FALSE);
 
@@ -1575,7 +1956,7 @@ zil_resume(zilog_t *zilog)
 }
 
 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;
@@ -1694,7 +2075,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;
@@ -1708,7 +2089,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.
@@ -1720,7 +2101,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);
@@ -1763,3 +2144,14 @@ zil_vdev_offline(const char *osname, void *arg)
        dmu_objset_rele(os, FTAG);
        return (error);
 }
+
+#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