]> 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 7a3da8647d17e352e5cfbbd273f4ae296d8b9b0d..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
@@ -46,7 +48,7 @@
  * either be processing, or blocked waiting to enter the next state. There may
  * be up to three active txgs, and there is always a txg in the open state
  * (though it may be blocked waiting to enter the quiescing state). In broad
- * strokes, transactions — operations that change in-memory structures — are
+ * strokes, transactions -- operations that change in-memory structures -- are
  * accepted into the txg in the open state, and are completed while the txg is
  * in the open or quiescing states. The accumulated changes are written to
  * disk in the syncing state.
@@ -54,7 +56,7 @@
  * Open
  *
  * When a new txg becomes active, it first enters the open state. New
- * transactions — updates to in-memory structures — are assigned to the
+ * transactions -- updates to in-memory structures -- are assigned to the
  * currently open txg. There is always a txg in the open state so that ZFS can
  * accept new changes (though the txg may refuse new changes if it has hit
  * some limit). ZFS advances the open txg to the next state for a variety of
  * 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,6 +129,8 @@ 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_NOLOCKDEP,
+                   NULL);
                for (i = 0; i < TXG_SIZE; i++) {
                        cv_init(&tx->tx_cpu[c].tc_cv[i], NULL, CV_DEFAULT,
                            NULL);
@@ -169,6 +173,7 @@ txg_fini(dsl_pool_t *dp)
        for (c = 0; c < max_ncpus; c++) {
                int i;
 
+               mutex_destroy(&tx->tx_cpu[c].tc_open_lock);
                mutex_destroy(&tx->tx_cpu[c].tc_lock);
                for (i = 0; i < TXG_SIZE; i++) {
                        cv_destroy(&tx->tx_cpu[c].tc_cv[i]);
@@ -201,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);
 }
@@ -233,15 +238,15 @@ txg_thread_exit(tx_state_t *tx, callb_cpr_t *cpr, kthread_t **tpp)
 }
 
 static void
-txg_thread_wait(tx_state_t *tx, callb_cpr_t *cpr, kcondvar_t *cv, uint64_t time)
+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);
 }
@@ -303,10 +308,12 @@ txg_hold_open(dsl_pool_t *dp, txg_handle_t *th)
        tc = &tx->tx_cpu[CPU_SEQID];
        kpreempt_enable();
 
-       mutex_enter(&tc->tc_lock);
-
+       mutex_enter(&tc->tc_open_lock);
        txg = tx->tx_open_txg;
+
+       mutex_enter(&tc->tc_lock);
        tc->tc_count[txg & TXG_MASK]++;
+       mutex_exit(&tc->tc_lock);
 
        th->th_cpu = tc;
        th->th_txg = txg;
@@ -319,7 +326,8 @@ txg_rele_to_quiesce(txg_handle_t *th)
 {
        tx_cpu_t *tc = th->th_cpu;
 
-       mutex_exit(&tc->tc_lock);
+       ASSERT(!MUTEX_HELD(&tc->tc_lock));
+       mutex_exit(&tc->tc_open_lock);
 }
 
 void
@@ -348,31 +356,42 @@ txg_rele_to_sync(txg_handle_t *th)
        th->th_cpu = NULL;      /* defensive */
 }
 
+/*
+ * Blocks until all transactions in the group are committed.
+ *
+ * On return, the transaction group has reached a stable state in which it can
+ * then be passed off to the syncing context.
+ */
 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;
 
        /*
-        * Grab all tx_cpu locks so nobody else can get into this txg.
+        * Grab all tc_open_locks so nobody else can get into this txg.
         */
        for (c = 0; c < max_ncpus; c++)
-               mutex_enter(&tx->tx_cpu[c].tc_lock);
+               mutex_enter(&tx->tx_cpu[c].tc_open_lock);
 
        ASSERT(txg == tx->tx_open_txg);
        tx->tx_open_txg++;
+       tx->tx_open_time = 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);
+       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);
 
        /*
         * Now that we've incremented tx_open_txg, we can let threads
         * enter the next transaction group.
         */
        for (c = 0; c < max_ncpus; c++)
-               mutex_exit(&tx->tx_cpu[c].tc_lock);
+               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().
@@ -400,6 +419,9 @@ txg_do_callbacks(list_t *cb_list)
 
 /*
  * Dispatch the commit callbacks registered on this txg to worker threads.
+ *
+ * If no callbacks are registered for a given TXG, nothing happens.
+ * This function creates a taskq for the associated pool, if needed.
  */
 static void
 txg_dispatch_callbacks(dsl_pool_t *dp, uint64_t txg)
@@ -410,7 +432,10 @@ txg_dispatch_callbacks(dsl_pool_t *dp, uint64_t txg)
 
        for (c = 0; c < max_ncpus; c++) {
                tx_cpu_t *tc = &tx->tx_cpu[c];
-               /* No need to lock tx_cpu_t at this point */
+               /*
+                * No need to lock tx_cpu_t at this point, since this can
+                * only be called once a txg has been synced.
+                */
 
                int g = txg & TXG_MASK;
 
@@ -422,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));
 
@@ -448,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
@@ -490,7 +504,8 @@ txg_sync_thread(dsl_pool_t *dp)
                while (!dsl_scan_active(dp->dp_scan) &&
                    !tx->tx_exiting && timer > 0 &&
                    tx->tx_synced_txg >= tx->tx_sync_txg_waiting &&
-                   tx->tx_quiesced_txg == 0) {
+                   tx->tx_quiesced_txg == 0 &&
+                   dp->dp_dirty_total < zfs_dirty_data_sync) {
                        dprintf("waiting; tx_synced=%llu waiting=%llu dp=%p\n",
                            tx->tx_synced_txg, tx->tx_sync_txg_waiting, dp);
                        txg_thread_wait(tx, &cpr, &tx->tx_sync_more_cv, timer);
@@ -509,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
@@ -525,6 +535,8 @@ txg_sync_thread(dsl_pool_t *dp)
                txg = tx->tx_quiesced_txg;
                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",
@@ -538,28 +550,21 @@ txg_sync_thread(dsl_pool_t *dp)
                mutex_enter(&tx->tx_sync_lock);
                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_space_towrite[txg & TXG_MASK] +
-                   dp->dp_tempreserved[txg & TXG_MASK] / 2);
-               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;
 
@@ -596,23 +601,24 @@ txg_quiesce_thread(dsl_pool_t *dp)
                 */
                dprintf("quiesce done, handing off txg %llu\n", txg);
                tx->tx_quiesced_txg = txg;
+               DTRACE_PROBE2(txg__quiesced, dsl_pool_t *, dp, uint64_t, txg);
                cv_broadcast(&tx->tx_sync_more_cv);
                cv_broadcast(&tx->tx_quiesce_done_cv);
        }
 }
 
 /*
- * Delay this thread by 'ticks' if we are still in the open transaction
- * group and there is already a waiting txg quiesing or quiesced.  Abort
- * the delay if this txg stalls or enters the quiesing state.
+ * Delay this thread by delay nanoseconds if we are still in the open
+ * transaction group and there is already a waiting txg quiesing or quiesced.
+ * Abort the delay if this txg stalls or enters the quiesing state.
  */
 void
-txg_delay(dsl_pool_t *dp, uint64_t txg, int ticks)
+txg_delay(dsl_pool_t *dp, uint64_t txg, hrtime_t delay, hrtime_t resolution)
 {
        tx_state_t *tx = &dp->dp_tx;
-       clock_t timeout = ddi_get_lbolt() + ticks;
+       hrtime_t start = gethrtime();
 
-       /* don't delay if this txg could transition to quiesing immediately */
+       /* don't delay if this txg could transition to quiescing immediately */
        if (tx->tx_open_txg > txg ||
            tx->tx_syncing_txg == txg-1 || tx->tx_synced_txg == txg-1)
                return;
@@ -623,10 +629,11 @@ txg_delay(dsl_pool_t *dp, uint64_t txg, int ticks)
                return;
        }
 
-       while (ddi_get_lbolt() < timeout &&
-           tx->tx_syncing_txg < txg-1 && !txg_stalled(dp))
-               (void) cv_timedwait(&tx->tx_quiesce_more_cv, &tx->tx_sync_lock,
-                   timeout);
+       while (gethrtime() - start < delay &&
+           tx->tx_syncing_txg < txg-1 && !txg_stalled(dp)) {
+               (void) cv_timedwait_hires(&tx->tx_quiesce_more_cv,
+                   &tx->tx_sync_lock, delay, resolution, 0);
+       }
 
        DMU_TX_STAT_BUMP(dmu_tx_delay);
 
@@ -680,6 +687,28 @@ txg_wait_open(dsl_pool_t *dp, uint64_t txg)
        mutex_exit(&tx->tx_sync_lock);
 }
 
+/*
+ * If there isn't a txg syncing or in the pipeline, push another txg through
+ * the pipeline by queiscing the open txg.
+ */
+void
+txg_kick(dsl_pool_t *dp)
+{
+       tx_state_t *tx = &dp->dp_tx;
+
+       ASSERT(!dsl_pool_config_held(dp));
+
+       mutex_enter(&tx->tx_sync_lock);
+       if (tx->tx_syncing_txg == 0 &&
+           tx->tx_quiesce_txg_waiting <= tx->tx_open_txg &&
+           tx->tx_sync_txg_waiting <= tx->tx_synced_txg &&
+           tx->tx_quiesced_txg <= tx->tx_synced_txg) {
+               tx->tx_quiesce_txg_waiting = tx->tx_open_txg + 1;
+               cv_broadcast(&tx->tx_quiesce_more_cv);
+       }
+       mutex_exit(&tx->tx_sync_lock);
+}
+
 boolean_t
 txg_stalled(dsl_pool_t *dp)
 {
@@ -696,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;
@@ -726,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.
@@ -740,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) {
@@ -764,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) {
@@ -791,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;
@@ -812,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]) {
@@ -835,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);
 }
 
@@ -847,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);
 }
 
@@ -856,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);