]> git.proxmox.com Git - mirror_zfs.git/blobdiff - module/zfs/vdev_label.c
Simplify spa_sync by breaking it up to smaller functions
[mirror_zfs.git] / module / zfs / vdev_label.c
index 26fc936459b2f8745873347e621d6bf68529bc14..65b847d66470275a024bcb8ffbea82fbaec722c6 100644 (file)
@@ -21,7 +21,9 @@
 
 /*
  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
- * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
+ * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
+ * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
+ * Copyright (c) 2017, Intel Corporation.
  */
 
 /*
  *     1. Uniquely identify this device as part of a ZFS pool and confirm its
  *        identity within the pool.
  *
- *     2. Verify that all the devices given in a configuration are present
+ *     2. Verify that all the devices given in a configuration are present
  *         within the pool.
  *
- *     3. Determine the uberblock for the pool.
+ *     3. Determine the uberblock for the pool.
  *
- *     4. In case of an import operation, determine the configuration of the
+ *     4. In case of an import operation, determine the configuration of the
  *         toplevel vdev of which it is a part.
  *
- *     5. If an import operation cannot find all the devices in the pool,
+ *     5. If an import operation cannot find all the devices in the pool,
  *         provide enough information to the administrator to determine which
  *         devices are missing.
  *
@@ -77,9 +79,9 @@
  * In order to identify which labels are valid, the labels are written in the
  * following manner:
  *
- *     1. For each vdev, update 'L1' to the new label
- *     2. Update the uberblock
- *     3. For each vdev, update 'L2' to the new label
+ *     1. For each vdev, update 'L1' to the new label
+ *     2. Update the uberblock
+ *     3. For each vdev, update 'L2' to the new label
  *
  * Given arbitrary failure, we can determine the correct label to use based on
  * the transaction group.  If we fail after updating L1 but before updating the
  *
  * The nvlist describing the pool and vdev contains the following elements:
  *
- *     version         ZFS on-disk version
- *     name            Pool name
- *     state           Pool state
- *     txg             Transaction group in which this label was written
- *     pool_guid       Unique identifier for this pool
- *     vdev_tree       An nvlist describing vdev tree.
+ *     version         ZFS on-disk version
+ *     name            Pool name
+ *     state           Pool state
+ *     txg             Transaction group in which this label was written
+ *     pool_guid       Unique identifier for this pool
+ *     vdev_tree       An nvlist describing vdev tree.
  *     features_for_read
  *                     An nvlist of the features necessary for reading the MOS.
  *
  * Each leaf device label also contains the following:
  *
- *     top_guid        Unique ID for top-level vdev in which this is contained
- *     guid            Unique ID for the leaf vdev
+ *     top_guid        Unique ID for top-level vdev in which this is contained
+ *     guid            Unique ID for the leaf vdev
  *
  * The 'vs' configuration follows the format described in 'spa_config.c'.
  */
 #include <sys/vdev_impl.h>
 #include <sys/uberblock_impl.h>
 #include <sys/metaslab.h>
+#include <sys/metaslab_impl.h>
 #include <sys/zio.h>
 #include <sys/dsl_scan.h>
 #include <sys/abd.h>
@@ -225,8 +228,6 @@ vdev_config_generate_stats(vdev_t *vd, nvlist_t *nv)
        fnvlist_add_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
            (uint64_t *)vs, sizeof (*vs) / sizeof (uint64_t));
 
-       kmem_free(vs, sizeof (*vs));
-
        /*
         * Add extended stats into a special extended stats nvlist.  This keeps
         * all the extended stats nicely grouped together.  The extended stats
@@ -344,13 +345,48 @@ vdev_config_generate_stats(vdev_t *vd, nvlist_t *nv)
            vsx->vsx_agg_histo[ZIO_PRIORITY_SCRUB],
            ARRAY_SIZE(vsx->vsx_agg_histo[ZIO_PRIORITY_SCRUB]));
 
+       /* IO delays */
+       fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_SLOW_IOS, vs->vs_slow_ios);
+
        /* Add extended stats nvlist to main nvlist */
        fnvlist_add_nvlist(nv, ZPOOL_CONFIG_VDEV_STATS_EX, nvx);
 
        fnvlist_free(nvx);
+       kmem_free(vs, sizeof (*vs));
        kmem_free(vsx, sizeof (*vsx));
 }
 
+static void
+root_vdev_actions_getprogress(vdev_t *vd, nvlist_t *nvl)
+{
+       spa_t *spa = vd->vdev_spa;
+
+       if (vd != spa->spa_root_vdev)
+               return;
+
+       /* provide either current or previous scan information */
+       pool_scan_stat_t ps;
+       if (spa_scan_get_stats(spa, &ps) == 0) {
+               fnvlist_add_uint64_array(nvl,
+                   ZPOOL_CONFIG_SCAN_STATS, (uint64_t *)&ps,
+                   sizeof (pool_scan_stat_t) / sizeof (uint64_t));
+       }
+
+       pool_removal_stat_t prs;
+       if (spa_removal_get_stats(spa, &prs) == 0) {
+               fnvlist_add_uint64_array(nvl,
+                   ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t *)&prs,
+                   sizeof (prs) / sizeof (uint64_t));
+       }
+
+       pool_checkpoint_stat_t pcs;
+       if (spa_checkpoint_get_stats(spa, &pcs) == 0) {
+               fnvlist_add_uint64_array(nvl,
+                   ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t *)&pcs,
+                   sizeof (pcs) / sizeof (uint64_t));
+       }
+}
+
 /*
  * Generate the nvlist representing this vdev's config.
  */
@@ -359,6 +395,8 @@ vdev_config_generate(spa_t *spa, vdev_t *vd, boolean_t getstats,
     vdev_config_flag_t flags)
 {
        nvlist_t *nv = NULL;
+       vdev_indirect_config_t *vic = &vd->vdev_indirect_config;
+
        nv = fnvlist_alloc();
 
        fnvlist_add_string(nv, ZPOOL_CONFIG_TYPE, vd->vdev_ops->vdev_op_type);
@@ -409,7 +447,7 @@ vdev_config_generate(spa_t *spa, vdev_t *vd, boolean_t getstats,
                fnvlist_add_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
                    vd->vdev_wholedisk);
 
-       if (vd->vdev_not_present)
+       if (vd->vdev_not_present && !(flags & VDEV_CONFIG_MISSING))
                fnvlist_add_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 1);
 
        if (vd->vdev_isspare)
@@ -425,9 +463,32 @@ vdev_config_generate(spa_t *spa, vdev_t *vd, boolean_t getstats,
                fnvlist_add_uint64(nv, ZPOOL_CONFIG_ASIZE,
                    vd->vdev_asize);
                fnvlist_add_uint64(nv, ZPOOL_CONFIG_IS_LOG, vd->vdev_islog);
-               if (vd->vdev_removing)
+               if (vd->vdev_removing) {
                        fnvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVING,
                            vd->vdev_removing);
+               }
+
+               /* zpool command expects alloc class data */
+               if (getstats && vd->vdev_alloc_bias != VDEV_BIAS_NONE) {
+                       const char *bias = NULL;
+
+                       switch (vd->vdev_alloc_bias) {
+                       case VDEV_BIAS_LOG:
+                               bias = VDEV_ALLOC_BIAS_LOG;
+                               break;
+                       case VDEV_BIAS_SPECIAL:
+                               bias = VDEV_ALLOC_BIAS_SPECIAL;
+                               break;
+                       case VDEV_BIAS_DEDUP:
+                               bias = VDEV_ALLOC_BIAS_DEDUP;
+                               break;
+                       default:
+                               ASSERT3U(vd->vdev_alloc_bias, ==,
+                                   VDEV_BIAS_NONE);
+                       }
+                       fnvlist_add_string(nv, ZPOOL_CONFIG_ALLOCATION_BIAS,
+                           bias);
+               }
        }
 
        if (vd->vdev_dtl_sm != NULL) {
@@ -435,9 +496,28 @@ vdev_config_generate(spa_t *spa, vdev_t *vd, boolean_t getstats,
                    space_map_object(vd->vdev_dtl_sm));
        }
 
+       if (vic->vic_mapping_object != 0) {
+               fnvlist_add_uint64(nv, ZPOOL_CONFIG_INDIRECT_OBJECT,
+                   vic->vic_mapping_object);
+       }
+
+       if (vic->vic_births_object != 0) {
+               fnvlist_add_uint64(nv, ZPOOL_CONFIG_INDIRECT_BIRTHS,
+                   vic->vic_births_object);
+       }
+
+       if (vic->vic_prev_indirect_vdev != UINT64_MAX) {
+               fnvlist_add_uint64(nv, ZPOOL_CONFIG_PREV_INDIRECT_VDEV,
+                   vic->vic_prev_indirect_vdev);
+       }
+
        if (vd->vdev_crtxg)
                fnvlist_add_uint64(nv, ZPOOL_CONFIG_CREATE_TXG, vd->vdev_crtxg);
 
+       if (vd->vdev_expansion_time)
+               fnvlist_add_uint64(nv, ZPOOL_CONFIG_EXPANSION_TIME,
+                   vd->vdev_expansion_time);
+
        if (flags & VDEV_CONFIG_MOS) {
                if (vd->vdev_leaf_zap != 0) {
                        ASSERT(vd->vdev_ops->vdev_op_leaf);
@@ -450,18 +530,76 @@ vdev_config_generate(spa_t *spa, vdev_t *vd, boolean_t getstats,
                        fnvlist_add_uint64(nv, ZPOOL_CONFIG_VDEV_TOP_ZAP,
                            vd->vdev_top_zap);
                }
+
+               if (vd->vdev_resilver_deferred) {
+                       ASSERT(vd->vdev_ops->vdev_op_leaf);
+                       ASSERT(spa->spa_resilver_deferred);
+                       fnvlist_add_boolean(nv, ZPOOL_CONFIG_RESILVER_DEFER);
+               }
        }
 
        if (getstats) {
-               pool_scan_stat_t ps;
-
                vdev_config_generate_stats(vd, nv);
 
-               /* provide either current or previous scan information */
-               if (spa_scan_get_stats(spa, &ps) == 0) {
-                       fnvlist_add_uint64_array(nv,
-                           ZPOOL_CONFIG_SCAN_STATS, (uint64_t *)&ps,
-                           sizeof (pool_scan_stat_t) / sizeof (uint64_t));
+               root_vdev_actions_getprogress(vd, nv);
+
+               /*
+                * Note: this can be called from open context
+                * (spa_get_stats()), so we need the rwlock to prevent
+                * the mapping from being changed by condensing.
+                */
+               rw_enter(&vd->vdev_indirect_rwlock, RW_READER);
+               if (vd->vdev_indirect_mapping != NULL) {
+                       ASSERT(vd->vdev_indirect_births != NULL);
+                       vdev_indirect_mapping_t *vim =
+                           vd->vdev_indirect_mapping;
+                       fnvlist_add_uint64(nv, ZPOOL_CONFIG_INDIRECT_SIZE,
+                           vdev_indirect_mapping_size(vim));
+               }
+               rw_exit(&vd->vdev_indirect_rwlock);
+               if (vd->vdev_mg != NULL &&
+                   vd->vdev_mg->mg_fragmentation != ZFS_FRAG_INVALID) {
+                       /*
+                        * Compute approximately how much memory would be used
+                        * for the indirect mapping if this device were to
+                        * be removed.
+                        *
+                        * Note: If the frag metric is invalid, then not
+                        * enough metaslabs have been converted to have
+                        * histograms.
+                        */
+                       uint64_t seg_count = 0;
+                       uint64_t to_alloc = vd->vdev_stat.vs_alloc;
+
+                       /*
+                        * There are the same number of allocated segments
+                        * as free segments, so we will have at least one
+                        * entry per free segment.  However, small free
+                        * segments (smaller than vdev_removal_max_span)
+                        * will be combined with adjacent allocated segments
+                        * as a single mapping.
+                        */
+                       for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++) {
+                               if (1ULL << (i + 1) < vdev_removal_max_span) {
+                                       to_alloc +=
+                                           vd->vdev_mg->mg_histogram[i] <<
+                                           (i + 1);
+                               } else {
+                                       seg_count +=
+                                           vd->vdev_mg->mg_histogram[i];
+                               }
+                       }
+
+                       /*
+                        * The maximum length of a mapping is
+                        * zfs_remove_max_segment, so we need at least one entry
+                        * per zfs_remove_max_segment of allocated data.
+                        */
+                       seg_count += to_alloc / zfs_remove_max_segment;
+
+                       fnvlist_add_uint64(nv, ZPOOL_CONFIG_INDIRECT_SIZE,
+                           seg_count *
+                           sizeof (vdev_indirect_mapping_entry_phys_t));
                }
        }
 
@@ -567,8 +705,9 @@ vdev_top_config_generate(spa_t *spa, nvlist_t *config)
        for (c = 0, idx = 0; c < rvd->vdev_children; c++) {
                vdev_t *tvd = rvd->vdev_child[c];
 
-               if (tvd->vdev_ishole)
+               if (tvd->vdev_ishole) {
                        array[idx++] = c;
+               }
        }
 
        if (idx) {
@@ -599,6 +738,7 @@ vdev_label_read_config(vdev_t *vd, uint64_t txg)
        abd_t *vp_abd;
        zio_t *zio;
        uint64_t best_txg = 0;
+       uint64_t label_txg = 0;
        int error = 0;
        int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL |
            ZIO_FLAG_SPECULATIVE;
@@ -624,8 +764,6 @@ retry:
                if (zio_wait(zio) == 0 &&
                    nvlist_unpack(vp->vp_nvlist, sizeof (vp->vp_nvlist),
                    &label, 0) == 0) {
-                       uint64_t label_txg = 0;
-
                        /*
                         * Auxiliary vdevs won't have txg values in their
                         * labels and newly added vdevs may not have been
@@ -656,6 +794,15 @@ retry:
                goto retry;
        }
 
+       /*
+        * We found a valid label but it didn't pass txg restrictions.
+        */
+       if (config == NULL && label_txg != 0) {
+               vdev_dbgmsg(vd, "label discarded as txg is too large "
+                   "(%llu > %llu)", (u_longlong_t)label_txg,
+                   (u_longlong_t)txg);
+       }
+
        abd_free(vp_abd);
 
        return (config);
@@ -1130,8 +1277,20 @@ vdev_uberblock_load(vdev_t *rvd, uberblock_t *ub, nvlist_t **config)
         * Search all labels on this vdev to find the configuration that
         * matches the txg for our uberblock.
         */
-       if (cb.ubl_vd != NULL)
+       if (cb.ubl_vd != NULL) {
+               vdev_dbgmsg(cb.ubl_vd, "best uberblock found for spa %s. "
+                   "txg %llu", spa->spa_name, (u_longlong_t)ub->ub_txg);
+
                *config = vdev_label_read_config(cb.ubl_vd, ub->ub_txg);
+               if (*config == NULL && spa->spa_extreme_rewind) {
+                       vdev_dbgmsg(cb.ubl_vd, "failed to read label config. "
+                           "Trying again without txg restrictions.");
+                       *config = vdev_label_read_config(cb.ubl_vd, UINT64_MAX);
+               }
+               if (*config == NULL) {
+                       vdev_dbgmsg(cb.ubl_vd, "failed to read label config");
+               }
+       }
        spa_config_exit(spa, SCL_ALL, FTAG);
 }
 
@@ -1206,10 +1365,13 @@ vdev_uberblock_sync_done(zio_t *zio)
  * Write the uberblock to all labels of all leaves of the specified vdev.
  */
 static void
-vdev_uberblock_sync(zio_t *zio, uberblock_t *ub, vdev_t *vd, int flags)
+vdev_uberblock_sync(zio_t *zio, uint64_t *good_writes,
+    uberblock_t *ub, vdev_t *vd, int flags)
 {
-       for (int c = 0; c < vd->vdev_children; c++)
-               vdev_uberblock_sync(zio, ub, vd->vdev_child[c], flags);
+       for (uint64_t c = 0; c < vd->vdev_children; c++) {
+               vdev_uberblock_sync(zio, good_writes,
+                   ub, vd->vdev_child[c], flags);
+       }
 
        if (!vd->vdev_ops->vdev_op_leaf)
                return;
@@ -1235,7 +1397,7 @@ vdev_uberblock_sync(zio_t *zio, uberblock_t *ub, vdev_t *vd, int flags)
        for (int l = 0; l < VDEV_LABELS; l++)
                vdev_label_write(zio, vd, l, ub_abd,
                    VDEV_UBERBLOCK_OFFSET(vd, n), VDEV_UBERBLOCK_SIZE(vd),
-                   vdev_uberblock_sync_done, zio->io_private,
+                   vdev_uberblock_sync_done, good_writes,
                    flags | ZIO_FLAG_DONT_PROPAGATE);
 
        abd_free(ub_abd);
@@ -1249,10 +1411,10 @@ vdev_uberblock_sync_list(vdev_t **svd, int svdcount, uberblock_t *ub, int flags)
        zio_t *zio;
        uint64_t good_writes = 0;
 
-       zio = zio_root(spa, NULL, &good_writes, flags);
+       zio = zio_root(spa, NULL, NULL, flags);
 
        for (int v = 0; v < svdcount; v++)
-               vdev_uberblock_sync(zio, ub, svd[v], flags);
+               vdev_uberblock_sync(zio, &good_writes, ub, svd[v], flags);
 
        (void) zio_wait(zio);
 
@@ -1263,8 +1425,11 @@ vdev_uberblock_sync_list(vdev_t **svd, int svdcount, uberblock_t *ub, int flags)
         */
        zio = zio_root(spa, NULL, NULL, flags);
 
-       for (int v = 0; v < svdcount; v++)
-               zio_flush(zio, svd[v]);
+       for (int v = 0; v < svdcount; v++) {
+               if (vdev_writeable(svd[v])) {
+                       zio_flush(zio, svd[v]);
+               }
+       }
 
        (void) zio_wait(zio);
 
@@ -1310,7 +1475,8 @@ vdev_label_sync_ignore_done(zio_t *zio)
  * Write all even or odd labels to all leaves of the specified vdev.
  */
 static void
-vdev_label_sync(zio_t *zio, vdev_t *vd, int l, uint64_t txg, int flags)
+vdev_label_sync(zio_t *zio, uint64_t *good_writes,
+    vdev_t *vd, int l, uint64_t txg, int flags)
 {
        nvlist_t *label;
        vdev_phys_t *vp;
@@ -1318,8 +1484,10 @@ vdev_label_sync(zio_t *zio, vdev_t *vd, int l, uint64_t txg, int flags)
        char *buf;
        size_t buflen;
 
-       for (int c = 0; c < vd->vdev_children; c++)
-               vdev_label_sync(zio, vd->vdev_child[c], l, txg, flags);
+       for (int c = 0; c < vd->vdev_children; c++) {
+               vdev_label_sync(zio, good_writes,
+                   vd->vdev_child[c], l, txg, flags);
+       }
 
        if (!vd->vdev_ops->vdev_op_leaf)
                return;
@@ -1344,7 +1512,7 @@ vdev_label_sync(zio_t *zio, vdev_t *vd, int l, uint64_t txg, int flags)
                        vdev_label_write(zio, vd, l, vp_abd,
                            offsetof(vdev_label_t, vl_vdev_phys),
                            sizeof (vdev_phys_t),
-                           vdev_label_sync_done, zio->io_private,
+                           vdev_label_sync_done, good_writes,
                            flags | ZIO_FLAG_DONT_PROPAGATE);
                }
        }
@@ -1376,7 +1544,7 @@ vdev_label_sync_list(spa_t *spa, int l, uint64_t txg, int flags)
                    (vd->vdev_islog || vd->vdev_aux != NULL) ?
                    vdev_label_sync_ignore_done : vdev_label_sync_top_done,
                    good_writes, flags);
-               vdev_label_sync(vio, vd, l, txg, flags);
+               vdev_label_sync(vio, good_writes, vd, l, txg, flags);
                zio_nowait(vio);
        }
 
@@ -1411,11 +1579,10 @@ vdev_config_sync(vdev_t **svd, int svdcount, uint64_t txg)
 {
        spa_t *spa = svd[0]->vdev_spa;
        uberblock_t *ub = &spa->spa_uberblock;
-       vdev_t *vd;
-       zio_t *zio;
        int error = 0;
        int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL;
 
+       ASSERT(svdcount != 0);
 retry:
        /*
         * Normally, we don't want to try too hard to write every label and
@@ -1457,9 +1624,10 @@ retry:
         * written in this txg will be committed to stable storage
         * before any uberblock that references them.
         */
-       zio = zio_root(spa, NULL, NULL, flags);
+       zio_t *zio = zio_root(spa, NULL, NULL, flags);
 
-       for (vd = txg_list_head(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)); vd;
+       for (vdev_t *vd =
+           txg_list_head(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)); vd != NULL;
            vd = txg_list_next(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)))
                zio_flush(zio, vd);
 
@@ -1474,8 +1642,14 @@ retry:
         * the new labels to disk to ensure that all even-label updates
         * are committed to stable storage before the uberblock update.
         */
-       if ((error = vdev_label_sync_list(spa, 0, txg, flags)) != 0)
+       if ((error = vdev_label_sync_list(spa, 0, txg, flags)) != 0) {
+               if ((flags & ZIO_FLAG_TRYHARD) != 0) {
+                       zfs_dbgmsg("vdev_label_sync_list() returned error %d "
+                           "for pool '%s' when syncing out the even labels "
+                           "of dirty vdevs", error, spa_name(spa));
+               }
                goto retry;
+       }
 
        /*
         * Sync the uberblocks to all vdevs in svd[].
@@ -1492,9 +1666,13 @@ retry:
         *      been successfully committed) will be valid with respect
         *      to the new uberblocks.
         */
-       if ((error = vdev_uberblock_sync_list(svd, svdcount, ub, flags)) != 0)
+       if ((error = vdev_uberblock_sync_list(svd, svdcount, ub, flags)) != 0) {
+               if ((flags & ZIO_FLAG_TRYHARD) != 0) {
+                       zfs_dbgmsg("vdev_uberblock_sync_list() returned error "
+                           "%d for pool '%s'", error, spa_name(spa));
+               }
                goto retry;
-
+       }
 
        if (spa_multihost(spa))
                mmp_update_uberblock(spa, ub);
@@ -1509,8 +1687,14 @@ retry:
         * to disk to ensure that all odd-label updates are committed to
         * stable storage before the next transaction group begins.
         */
-       if ((error = vdev_label_sync_list(spa, 1, txg, flags)) != 0)
+       if ((error = vdev_label_sync_list(spa, 1, txg, flags)) != 0) {
+               if ((flags & ZIO_FLAG_TRYHARD) != 0) {
+                       zfs_dbgmsg("vdev_label_sync_list() returned error %d "
+                           "for pool '%s' when syncing out the odd labels of "
+                           "dirty vdevs", error, spa_name(spa));
+               }
                goto retry;
+       }
 
        return (0);
 }