]> git.proxmox.com Git - mirror_zfs.git/blobdiff - module/zfs/txg.c
Undo c89 workarounds to match with upstream
[mirror_zfs.git] / module / zfs / txg.c
index c779c4b9a28a7d60090721c81961e522cb791c59..078839a3203bc74ec124f24dee994ca6c2f860ef 100644 (file)
@@ -21,7 +21,7 @@
 /*
  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  * Portions Copyright 2011 Martin Matuska
- * Copyright (c) 2013 by Delphix. All rights reserved.
+ * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
  */
 
 #include <sys/zfs_context.h>
@@ -31,7 +31,9 @@
 #include <sys/dmu_tx.h>
 #include <sys/dsl_pool.h>
 #include <sys/dsl_scan.h>
+#include <sys/zil.h>
 #include <sys/callb.h>
+#include <sys/trace_txg.h>
 
 /*
  * ZFS Transaction Groups
  * now transition to the syncing state.
  */
 
-static void txg_sync_thread(dsl_pool_t *dp);
-static void txg_quiesce_thread(dsl_pool_t *dp);
+static void txg_sync_thread(void *arg);
+static void txg_quiesce_thread(void *arg);
 
 int zfs_txg_timeout = 5;       /* max seconds worth of delta per txg */
 
@@ -127,7 +129,7 @@ txg_init(dsl_pool_t *dp, uint64_t txg)
                int i;
 
                mutex_init(&tx->tx_cpu[c].tc_lock, NULL, MUTEX_DEFAULT, NULL);
-               mutex_init(&tx->tx_cpu[c].tc_open_lock, NULL, MUTEX_DEFAULT,
+               mutex_init(&tx->tx_cpu[c].tc_open_lock, NULL, MUTEX_NOLOCKDEP,
                    NULL);
                for (i = 0; i < TXG_SIZE; i++) {
                        cv_init(&tx->tx_cpu[c].tc_cv[i], NULL, CV_DEFAULT,
@@ -204,15 +206,15 @@ txg_sync_start(dsl_pool_t *dp)
        tx->tx_threads = 2;
 
        tx->tx_quiesce_thread = thread_create(NULL, 0, txg_quiesce_thread,
-           dp, 0, &p0, TS_RUN, minclsyspri);
+           dp, 0, &p0, TS_RUN, defclsyspri);
 
        /*
         * The sync thread can need a larger-than-default stack size on
         * 32-bit x86.  This is due in part to nested pools and
         * scrub_visitbp() recursion.
         */
-       tx->tx_sync_thread = thread_create(NULL, 32<<10, txg_sync_thread,
-           dp, 0, &p0, TS_RUN, minclsyspri);
+       tx->tx_sync_thread = thread_create(NULL, 0, txg_sync_thread,
+           dp, 0, &p0, TS_RUN, defclsyspri);
 
        mutex_exit(&tx->tx_sync_lock);
 }
@@ -241,10 +243,10 @@ txg_thread_wait(tx_state_t *tx, callb_cpr_t *cpr, kcondvar_t *cv, clock_t time)
        CALLB_CPR_SAFE_BEGIN(cpr);
 
        if (time)
-               (void) cv_timedwait_interruptible(cv, &tx->tx_sync_lock,
+               (void) cv_timedwait_sig(cv, &tx->tx_sync_lock,
                    ddi_get_lbolt() + time);
        else
-               cv_wait_interruptible(cv, &tx->tx_sync_lock);
+               cv_wait_sig(cv, &tx->tx_sync_lock);
 
        CALLB_CPR_SAFE_END(cpr, &tx->tx_sync_lock);
 }
@@ -364,6 +366,7 @@ static void
 txg_quiesce(dsl_pool_t *dp, uint64_t txg)
 {
        tx_state_t *tx = &dp->dp_tx;
+       uint64_t tx_open_time;
        int g = txg & TXG_MASK;
        int c;
 
@@ -375,10 +378,7 @@ txg_quiesce(dsl_pool_t *dp, uint64_t txg)
 
        ASSERT(txg == tx->tx_open_txg);
        tx->tx_open_txg++;
-       tx->tx_open_time = gethrtime();
-
-       spa_txg_history_set(dp->dp_spa, txg, TXG_STATE_OPEN, gethrtime());
-       spa_txg_history_add(dp->dp_spa, tx->tx_open_txg);
+       tx->tx_open_time = tx_open_time = gethrtime();
 
        DTRACE_PROBE2(txg__quiescing, dsl_pool_t *, dp, uint64_t, txg);
        DTRACE_PROBE2(txg__opened, dsl_pool_t *, dp, uint64_t, tx->tx_open_txg);
@@ -390,6 +390,9 @@ txg_quiesce(dsl_pool_t *dp, uint64_t txg)
        for (c = 0; c < max_ncpus; c++)
                mutex_exit(&tx->tx_cpu[c].tc_open_lock);
 
+       spa_txg_history_set(dp->dp_spa, txg, TXG_STATE_OPEN, tx_open_time);
+       spa_txg_history_add(dp->dp_spa, txg + 1, tx_open_time);
+
        /*
         * Quiesce the transaction group by waiting for everyone to txg_exit().
         */
@@ -444,11 +447,11 @@ txg_dispatch_callbacks(dsl_pool_t *dp, uint64_t txg)
                         * Commit callback taskq hasn't been created yet.
                         */
                        tx->tx_commit_cb_taskq = taskq_create("tx_commit_cb",
-                           100, minclsyspri, max_ncpus, INT_MAX,
-                           TASKQ_THREADS_CPU_PCT | TASKQ_PREPOPULATE);
+                           max_ncpus, defclsyspri, max_ncpus, max_ncpus * 2,
+                           TASKQ_PREPOPULATE | TASKQ_DYNAMIC);
                }
 
-               cb_list = kmem_alloc(sizeof (list_t), KM_PUSHPAGE);
+               cb_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
                list_create(cb_list, sizeof (dmu_tx_callback_t),
                    offsetof(dmu_tx_callback_t, dcb_node));
 
@@ -470,38 +473,27 @@ txg_wait_callbacks(dsl_pool_t *dp)
        tx_state_t *tx = &dp->dp_tx;
 
        if (tx->tx_commit_cb_taskq != NULL)
-               taskq_wait(tx->tx_commit_cb_taskq);
+               taskq_wait_outstanding(tx->tx_commit_cb_taskq, 0);
 }
 
 static void
-txg_sync_thread(dsl_pool_t *dp)
+txg_sync_thread(void *arg)
 {
+       dsl_pool_t *dp = arg;
        spa_t *spa = dp->dp_spa;
        tx_state_t *tx = &dp->dp_tx;
        callb_cpr_t cpr;
-       vdev_stat_t *vs1, *vs2;
-       uint64_t start, delta;
-
-#ifdef _KERNEL
-       /*
-        * Annotate this process with a flag that indicates that it is
-        * unsafe to use KM_SLEEP during memory allocations due to the
-        * potential for a deadlock.  KM_PUSHPAGE should be used instead.
-        */
-       current->flags |= PF_NOFS;
-#endif /* _KERNEL */
+       clock_t start, delta;
 
+       (void) spl_fstrans_mark();
        txg_thread_enter(tx, &cpr);
 
-       vs1 = kmem_alloc(sizeof (vdev_stat_t), KM_PUSHPAGE);
-       vs2 = kmem_alloc(sizeof (vdev_stat_t), KM_PUSHPAGE);
-
        start = delta = 0;
        for (;;) {
-               uint64_t timer, timeout;
+               clock_t timeout = zfs_txg_timeout * hz;
+               clock_t timer;
                uint64_t txg;
-
-               timeout = zfs_txg_timeout * hz;
+               txg_stat_t *ts;
 
                /*
                 * We sync when we're scanning, there's someone waiting
@@ -532,13 +524,8 @@ txg_sync_thread(dsl_pool_t *dp)
                        txg_thread_wait(tx, &cpr, &tx->tx_quiesce_done_cv, 0);
                }
 
-               if (tx->tx_exiting) {
-                       kmem_free(vs2, sizeof (vdev_stat_t));
-                       kmem_free(vs1, sizeof (vdev_stat_t));
+               if (tx->tx_exiting)
                        txg_thread_exit(tx, &cpr, &tx->tx_sync_thread);
-               }
-
-               vdev_get_stats(spa->spa_root_vdev, vs1);
 
                /*
                 * Consume the quiesced txg which has been handed off to
@@ -549,6 +536,7 @@ txg_sync_thread(dsl_pool_t *dp)
                tx->tx_quiesced_txg = 0;
                tx->tx_syncing_txg = txg;
                DTRACE_PROBE2(txg__syncing, dsl_pool_t *, dp, uint64_t, txg);
+               ts = spa_txg_history_init_io(spa, txg, dp);
                cv_broadcast(&tx->tx_quiesce_more_cv);
 
                dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
@@ -563,27 +551,20 @@ txg_sync_thread(dsl_pool_t *dp)
                tx->tx_synced_txg = txg;
                tx->tx_syncing_txg = 0;
                DTRACE_PROBE2(txg__synced, dsl_pool_t *, dp, uint64_t, txg);
+               spa_txg_history_fini_io(spa, ts);
                cv_broadcast(&tx->tx_sync_done_cv);
 
                /*
                 * Dispatch commit callbacks to worker threads.
                 */
                txg_dispatch_callbacks(dp, txg);
-
-               vdev_get_stats(spa->spa_root_vdev, vs2);
-               spa_txg_history_set_io(spa, txg,
-                   vs2->vs_bytes[ZIO_TYPE_READ]-vs1->vs_bytes[ZIO_TYPE_READ],
-                   vs2->vs_bytes[ZIO_TYPE_WRITE]-vs1->vs_bytes[ZIO_TYPE_WRITE],
-                   vs2->vs_ops[ZIO_TYPE_READ]-vs1->vs_ops[ZIO_TYPE_READ],
-                   vs2->vs_ops[ZIO_TYPE_WRITE]-vs1->vs_ops[ZIO_TYPE_WRITE],
-                   dp->dp_dirty_pertxg[txg & TXG_MASK]);
-               spa_txg_history_set(spa, txg, TXG_STATE_SYNCED, gethrtime());
        }
 }
 
 static void
-txg_quiesce_thread(dsl_pool_t *dp)
+txg_quiesce_thread(void *arg)
 {
+       dsl_pool_t *dp = arg;
        tx_state_t *tx = &dp->dp_tx;
        callb_cpr_t cpr;
 
@@ -744,17 +725,33 @@ txg_sync_waiting(dsl_pool_t *dp)
            tx->tx_quiesced_txg != 0);
 }
 
+/*
+ * Verify that this txg is active (open, quiescing, syncing).  Non-active
+ * txg's should not be manipulated.
+ */
+void
+txg_verify(spa_t *spa, uint64_t txg)
+{
+       ASSERTV(dsl_pool_t *dp = spa_get_dsl(spa));
+       if (txg <= TXG_INITIAL || txg == ZILTEST_TXG)
+               return;
+       ASSERT3U(txg, <=, dp->dp_tx.tx_open_txg);
+       ASSERT3U(txg, >=, dp->dp_tx.tx_synced_txg);
+       ASSERT3U(txg, >=, dp->dp_tx.tx_open_txg - TXG_CONCURRENT_STATES);
+}
+
 /*
  * Per-txg object lists.
  */
 void
-txg_list_create(txg_list_t *tl, size_t offset)
+txg_list_create(txg_list_t *tl, spa_t *spa, size_t offset)
 {
        int t;
 
        mutex_init(&tl->tl_lock, NULL, MUTEX_DEFAULT, NULL);
 
        tl->tl_offset = offset;
+       tl->tl_spa = spa;
 
        for (t = 0; t < TXG_SIZE; t++)
                tl->tl_head[t] = NULL;
@@ -774,9 +771,28 @@ txg_list_destroy(txg_list_t *tl)
 boolean_t
 txg_list_empty(txg_list_t *tl, uint64_t txg)
 {
+       txg_verify(tl->tl_spa, txg);
        return (tl->tl_head[txg & TXG_MASK] == NULL);
 }
 
+/*
+ * Returns true if all txg lists are empty.
+ *
+ * Warning: this is inherently racy (an item could be added immediately
+ * after this function returns). We don't bother with the lock because
+ * it wouldn't change the semantics.
+ */
+boolean_t
+txg_all_lists_empty(txg_list_t *tl)
+{
+       for (int i = 0; i < TXG_SIZE; i++) {
+               if (!txg_list_empty(tl, i)) {
+                       return (B_FALSE);
+               }
+       }
+       return (B_TRUE);
+}
+
 /*
  * Add an entry to the list (unless it's already on the list).
  * Returns B_TRUE if it was actually added.
@@ -788,6 +804,7 @@ txg_list_add(txg_list_t *tl, void *p, uint64_t txg)
        txg_node_t *tn = (txg_node_t *)((char *)p + tl->tl_offset);
        boolean_t add;
 
+       txg_verify(tl->tl_spa, txg);
        mutex_enter(&tl->tl_lock);
        add = (tn->tn_member[t] == 0);
        if (add) {
@@ -812,6 +829,7 @@ txg_list_add_tail(txg_list_t *tl, void *p, uint64_t txg)
        txg_node_t *tn = (txg_node_t *)((char *)p + tl->tl_offset);
        boolean_t add;
 
+       txg_verify(tl->tl_spa, txg);
        mutex_enter(&tl->tl_lock);
        add = (tn->tn_member[t] == 0);
        if (add) {
@@ -839,6 +857,7 @@ txg_list_remove(txg_list_t *tl, uint64_t txg)
        txg_node_t *tn;
        void *p = NULL;
 
+       txg_verify(tl->tl_spa, txg);
        mutex_enter(&tl->tl_lock);
        if ((tn = tl->tl_head[t]) != NULL) {
                p = (char *)tn - tl->tl_offset;
@@ -860,6 +879,7 @@ txg_list_remove_this(txg_list_t *tl, void *p, uint64_t txg)
        int t = txg & TXG_MASK;
        txg_node_t *tn, **tp;
 
+       txg_verify(tl->tl_spa, txg);
        mutex_enter(&tl->tl_lock);
 
        for (tp = &tl->tl_head[t]; (tn = *tp) != NULL; tp = &tn->tn_next[t]) {
@@ -883,6 +903,7 @@ txg_list_member(txg_list_t *tl, void *p, uint64_t txg)
        int t = txg & TXG_MASK;
        txg_node_t *tn = (txg_node_t *)((char *)p + tl->tl_offset);
 
+       txg_verify(tl->tl_spa, txg);
        return (tn->tn_member[t] != 0);
 }
 
@@ -895,6 +916,7 @@ txg_list_head(txg_list_t *tl, uint64_t txg)
        int t = txg & TXG_MASK;
        txg_node_t *tn = tl->tl_head[t];
 
+       txg_verify(tl->tl_spa, txg);
        return (tn == NULL ? NULL : (char *)tn - tl->tl_offset);
 }
 
@@ -904,6 +926,7 @@ txg_list_next(txg_list_t *tl, void *p, uint64_t txg)
        int t = txg & TXG_MASK;
        txg_node_t *tn = (txg_node_t *)((char *)p + tl->tl_offset);
 
+       txg_verify(tl->tl_spa, txg);
        tn = tn->tn_next[t];
 
        return (tn == NULL ? NULL : (char *)tn - tl->tl_offset);