]> git.proxmox.com Git - mirror_zfs.git/blobdiff - module/zfs/vdev_mirror.c
OpenZFS 8473 - scrub does not detect errors on active spares
[mirror_zfs.git] / module / zfs / vdev_mirror.c
index dc2a346426ca4375e48cfba383fa9e34e462735a..a92d956cdaa2d5c0274387db7ac8367bc6778ce3 100644 (file)
 
 #include <sys/zfs_context.h>
 #include <sys/spa.h>
+#include <sys/spa_impl.h>
+#include <sys/dsl_pool.h>
+#include <sys/dsl_scan.h>
 #include <sys/vdev_impl.h>
 #include <sys/zio.h>
 #include <sys/abd.h>
 #include <sys/fs/zfs.h>
 
+/*
+ * Vdev mirror kstats
+ */
+static kstat_t *mirror_ksp = NULL;
+
+typedef struct mirror_stats {
+       kstat_named_t vdev_mirror_stat_rotating_linear;
+       kstat_named_t vdev_mirror_stat_rotating_offset;
+       kstat_named_t vdev_mirror_stat_rotating_seek;
+       kstat_named_t vdev_mirror_stat_non_rotating_linear;
+       kstat_named_t vdev_mirror_stat_non_rotating_seek;
+
+       kstat_named_t vdev_mirror_stat_preferred_found;
+       kstat_named_t vdev_mirror_stat_preferred_not_found;
+} mirror_stats_t;
+
+static mirror_stats_t mirror_stats = {
+       /* New I/O follows directly the last I/O */
+       { "rotating_linear",                    KSTAT_DATA_UINT64 },
+       /* New I/O is within zfs_vdev_mirror_rotating_seek_offset of the last */
+       { "rotating_offset",                    KSTAT_DATA_UINT64 },
+       /* New I/O requires random seek */
+       { "rotating_seek",                      KSTAT_DATA_UINT64 },
+       /* New I/O follows directly the last I/O  (nonrot) */
+       { "non_rotating_linear",                KSTAT_DATA_UINT64 },
+       /* New I/O requires random seek (nonrot) */
+       { "non_rotating_seek",                  KSTAT_DATA_UINT64 },
+       /* Preferred child vdev found */
+       { "preferred_found",                    KSTAT_DATA_UINT64 },
+       /* Preferred child vdev not found or equal load  */
+       { "preferred_not_found",                KSTAT_DATA_UINT64 },
+
+};
+
+#define        MIRROR_STAT(stat)               (mirror_stats.stat.value.ui64)
+#define        MIRROR_INCR(stat, val)          atomic_add_64(&MIRROR_STAT(stat), val)
+#define        MIRROR_BUMP(stat)               MIRROR_INCR(stat, 1)
+
+void
+vdev_mirror_stat_init(void)
+{
+       mirror_ksp = kstat_create("zfs", 0, "vdev_mirror_stats",
+           "misc", KSTAT_TYPE_NAMED,
+           sizeof (mirror_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL);
+       if (mirror_ksp != NULL) {
+               mirror_ksp->ks_data = &mirror_stats;
+               kstat_install(mirror_ksp);
+       }
+}
+
+void
+vdev_mirror_stat_fini(void)
+{
+       if (mirror_ksp != NULL) {
+               kstat_delete(mirror_ksp);
+               mirror_ksp = NULL;
+       }
+}
+
 /*
  * Virtual device vector for mirroring.
  */
@@ -52,7 +114,7 @@ typedef struct mirror_map {
        int             *mm_preferred;
        int             mm_preferred_cnt;
        int             mm_children;
-       boolean_t       mm_replacing;
+       boolean_t       mm_resilvering;
        boolean_t       mm_root;
        mirror_child_t  mm_child[];
 } mirror_map_t;
@@ -86,13 +148,13 @@ vdev_mirror_map_size(int children)
 }
 
 static inline mirror_map_t *
-vdev_mirror_map_alloc(int children, boolean_t replacing, boolean_t root)
+vdev_mirror_map_alloc(int children, boolean_t resilvering, boolean_t root)
 {
        mirror_map_t *mm;
 
        mm = kmem_zalloc(vdev_mirror_map_size(children), KM_SLEEP);
        mm->mm_children = children;
-       mm->mm_replacing = replacing;
+       mm->mm_resilvering = resilvering;
        mm->mm_root = root;
        mm->mm_preferred = (int *)((uintptr_t)mm +
            offsetof(mirror_map_t, mm_child[children]));
@@ -109,14 +171,15 @@ vdev_mirror_map_free(zio_t *zio)
 }
 
 static const zio_vsd_ops_t vdev_mirror_vsd_ops = {
-       vdev_mirror_map_free,
-       zio_vsd_default_cksum_report
+       .vsd_free = vdev_mirror_map_free,
+       .vsd_cksum_report = zio_vsd_default_cksum_report
 };
 
 static int
 vdev_mirror_load(mirror_map_t *mm, vdev_t *vd, uint64_t zio_offset)
 {
-       uint64_t lastoffset;
+       uint64_t last_offset;
+       int64_t offset_diff;
        int load;
 
        /* All DVAs have equal weight at the root. */
@@ -129,14 +192,20 @@ vdev_mirror_load(mirror_map_t *mm, vdev_t *vd, uint64_t zio_offset)
         * worse overall when resilvering with compared to without.
         */
 
+       /* Fix zio_offset for leaf vdevs */
+       if (vd->vdev_ops->vdev_op_leaf)
+               zio_offset += VDEV_LABEL_START_SIZE;
+
        /* Standard load based on pending queue length. */
        load = vdev_queue_length(vd);
-       lastoffset = vdev_queue_lastoffset(vd);
+       last_offset = vdev_queue_last_offset(vd);
 
        if (vd->vdev_nonrot) {
                /* Non-rotating media. */
-               if (lastoffset == zio_offset)
+               if (last_offset == zio_offset) {
+                       MIRROR_BUMP(vdev_mirror_stat_non_rotating_linear);
                        return (load + zfs_vdev_mirror_non_rotating_inc);
+               }
 
                /*
                 * Apply a seek penalty even for non-rotating devices as
@@ -144,23 +213,29 @@ vdev_mirror_load(mirror_map_t *mm, vdev_t *vd, uint64_t zio_offset)
                 * the device, thus avoiding unnecessary per-command overhead
                 * and boosting performance.
                 */
+               MIRROR_BUMP(vdev_mirror_stat_non_rotating_seek);
                return (load + zfs_vdev_mirror_non_rotating_seek_inc);
        }
 
        /* Rotating media I/O's which directly follow the last I/O. */
-       if (lastoffset == zio_offset)
+       if (last_offset == zio_offset) {
+               MIRROR_BUMP(vdev_mirror_stat_rotating_linear);
                return (load + zfs_vdev_mirror_rotating_inc);
+       }
 
        /*
         * Apply half the seek increment to I/O's within seek offset
-        * of the last I/O queued to this vdev as they should incur less
+        * of the last I/O issued to this vdev as they should incur less
         * of a seek increment.
         */
-       if (ABS(lastoffset - zio_offset) <
-           zfs_vdev_mirror_rotating_seek_offset)
+       offset_diff = (int64_t)(last_offset - zio_offset);
+       if (ABS(offset_diff) < zfs_vdev_mirror_rotating_seek_offset) {
+               MIRROR_BUMP(vdev_mirror_stat_rotating_offset);
                return (load + (zfs_vdev_mirror_rotating_seek_inc / 2));
+       }
 
        /* Apply the full seek increment to all other I/O's. */
+       MIRROR_BUMP(vdev_mirror_stat_rotating_seek);
        return (load + zfs_vdev_mirror_rotating_seek_inc);
 }
 
@@ -179,9 +254,33 @@ vdev_mirror_map_init(zio_t *zio)
        if (vd == NULL) {
                dva_t *dva = zio->io_bp->blk_dva;
                spa_t *spa = zio->io_spa;
+               dva_t dva_copy[SPA_DVAS_PER_BP];
 
-               mm = vdev_mirror_map_alloc(BP_GET_NDVAS(zio->io_bp), B_FALSE,
-                   B_TRUE);
+               c = BP_GET_NDVAS(zio->io_bp);
+
+               /*
+                * If we do not trust the pool config, some DVAs might be
+                * invalid or point to vdevs that do not exist. We skip them.
+                */
+               if (!spa_trust_config(spa)) {
+                       ASSERT3U(zio->io_type, ==, ZIO_TYPE_READ);
+                       int j = 0;
+                       for (int i = 0; i < c; i++) {
+                               if (zfs_dva_valid(spa, &dva[i], zio->io_bp))
+                                       dva_copy[j++] = dva[i];
+                       }
+                       if (j == 0) {
+                               zio->io_vsd = NULL;
+                               zio->io_error = ENXIO;
+                               return (NULL);
+                       }
+                       if (j < c) {
+                               dva = dva_copy;
+                               c = j;
+                       }
+               }
+
+               mm = vdev_mirror_map_alloc(c, B_FALSE, B_TRUE);
                for (c = 0; c < mm->mm_children; c++) {
                        mc = &mm->mm_child[c];
 
@@ -189,9 +288,39 @@ vdev_mirror_map_init(zio_t *zio)
                        mc->mc_offset = DVA_GET_OFFSET(&dva[c]);
                }
        } else {
-               mm = vdev_mirror_map_alloc(vd->vdev_children,
-                   (vd->vdev_ops == &vdev_replacing_ops ||
-                   vd->vdev_ops == &vdev_spare_ops), B_FALSE);
+               /*
+                * If we are resilvering, then we should handle scrub reads
+                * differently; we shouldn't issue them to the resilvering
+                * device because it might not have those blocks.
+                *
+                * We are resilvering iff:
+                * 1) We are a replacing vdev (ie our name is "replacing-1" or
+                *    "spare-1" or something like that), and
+                * 2) The pool is currently being resilvered.
+                *
+                * We cannot simply check vd->vdev_resilver_txg, because it's
+                * not set in this path.
+                *
+                * Nor can we just check our vdev_ops; there are cases (such as
+                * when a user types "zpool replace pool odev spare_dev" and
+                * spare_dev is in the spare list, or when a spare device is
+                * automatically used to replace a DEGRADED device) when
+                * resilvering is complete but both the original vdev and the
+                * spare vdev remain in the pool.  That behavior is intentional.
+                * It helps implement the policy that a spare should be
+                * automatically removed from the pool after the user replaces
+                * the device that originally failed.
+                *
+                * If a spa load is in progress, then spa_dsl_pool may be
+                * uninitialized.  But we shouldn't be resilvering during a spa
+                * load anyway.
+                */
+               boolean_t replacing = (vd->vdev_ops == &vdev_replacing_ops ||
+                   vd->vdev_ops == &vdev_spare_ops) &&
+                   spa_load_state(vd->vdev_spa) == SPA_LOAD_NONE &&
+                   dsl_scan_resilvering(vd->vdev_spa->spa_dsl_pool);
+               mm = vdev_mirror_map_alloc(vd->vdev_children, replacing,
+                   B_FALSE);
                for (c = 0; c < mm->mm_children; c++) {
                        mc = &mm->mm_child[c];
                        mc->mc_vd = vd->vdev_child[c];
@@ -210,7 +339,6 @@ vdev_mirror_open(vdev_t *vd, uint64_t *asize, uint64_t *max_asize,
 {
        int numerrors = 0;
        int lasterror = 0;
-       int c;
 
        if (vd->vdev_children == 0) {
                vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
@@ -219,7 +347,7 @@ vdev_mirror_open(vdev_t *vd, uint64_t *asize, uint64_t *max_asize,
 
        vdev_open_children(vd);
 
-       for (c = 0; c < vd->vdev_children; c++) {
+       for (int c = 0; c < vd->vdev_children; c++) {
                vdev_t *cvd = vd->vdev_child[c];
 
                if (cvd->vdev_open_error) {
@@ -234,7 +362,10 @@ vdev_mirror_open(vdev_t *vd, uint64_t *asize, uint64_t *max_asize,
        }
 
        if (numerrors == vd->vdev_children) {
-               vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS;
+               if (vdev_children_are_offline(vd))
+                       vd->vdev_stat.vs_aux = VDEV_AUX_CHILDREN_OFFLINE;
+               else
+                       vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS;
                return (lasterror);
        }
 
@@ -244,9 +375,7 @@ vdev_mirror_open(vdev_t *vd, uint64_t *asize, uint64_t *max_asize,
 static void
 vdev_mirror_close(vdev_t *vd)
 {
-       int c;
-
-       for (c = 0; c < vd->vdev_children; c++)
+       for (int c = 0; c < vd->vdev_children; c++)
                vdev_close(vd->vdev_child[c]);
 }
 
@@ -383,16 +512,13 @@ vdev_mirror_child_select(zio_t *zio)
        }
 
        if (mm->mm_preferred_cnt == 1) {
-               vdev_queue_register_lastoffset(
-                   mm->mm_child[mm->mm_preferred[0]].mc_vd, zio);
+               MIRROR_BUMP(vdev_mirror_stat_preferred_found);
                return (mm->mm_preferred[0]);
        }
 
        if (mm->mm_preferred_cnt > 1) {
-               int c = vdev_mirror_preferred_child_randomize(zio);
-
-               vdev_queue_register_lastoffset(mm->mm_child[c].mc_vd, zio);
-               return (c);
+               MIRROR_BUMP(vdev_mirror_stat_preferred_not_found);
+               return (vdev_mirror_preferred_child_randomize(zio));
        }
 
        /*
@@ -400,11 +526,8 @@ vdev_mirror_child_select(zio_t *zio)
         * Look for any child we haven't already tried before giving up.
         */
        for (c = 0; c < mm->mm_children; c++) {
-               if (!mm->mm_child[c].mc_tried) {
-                       vdev_queue_register_lastoffset(mm->mm_child[c].mc_vd,
-                           zio);
+               if (!mm->mm_child[c].mc_tried)
                        return (c);
-               }
        }
 
        /*
@@ -422,13 +545,23 @@ vdev_mirror_io_start(zio_t *zio)
 
        mm = vdev_mirror_map_init(zio);
 
+       if (mm == NULL) {
+               ASSERT(!spa_trust_config(zio->io_spa));
+               ASSERT(zio->io_type == ZIO_TYPE_READ);
+               zio_execute(zio);
+               return;
+       }
+
        if (zio->io_type == ZIO_TYPE_READ) {
-               if ((zio->io_flags & ZIO_FLAG_SCRUB) && !mm->mm_replacing) {
+               if (zio->io_bp != NULL &&
+                   (zio->io_flags & ZIO_FLAG_SCRUB) && !mm->mm_resilvering) {
                        /*
-                        * For scrubbing reads we need to allocate a read
-                        * buffer for each child and issue reads to all
-                        * children.  If any child succeeds, it will copy its
-                        * data into zio->io_data in vdev_mirror_scrub_done.
+                        * For scrubbing reads (if we can verify the
+                        * checksum here, as indicated by io_bp being
+                        * non-NULL) we need to allocate a read buffer for
+                        * each child and issue reads to all children.  If
+                        * any child succeeds, it will copy its data into
+                        * zio->io_data in vdev_mirror_scrub_done.
                         */
                        for (c = 0; c < mm->mm_children; c++) {
                                mc = &mm->mm_child[c];
@@ -472,9 +605,9 @@ vdev_mirror_io_start(zio_t *zio)
 static int
 vdev_mirror_worst_error(mirror_map_t *mm)
 {
-       int c, error[2] = { 0, 0 };
+       int error[2] = { 0, 0 };
 
-       for (c = 0; c < mm->mm_children; c++) {
+       for (int c = 0; c < mm->mm_children; c++) {
                mirror_child_t *mc = &mm->mm_child[c];
                int s = mc->mc_speculative;
                error[s] = zio_worst_error(error[s], mc->mc_error);
@@ -492,6 +625,9 @@ vdev_mirror_io_done(zio_t *zio)
        int good_copies = 0;
        int unexpected_errors = 0;
 
+       if (mm == NULL)
+               return;
+
        for (c = 0; c < mm->mm_children; c++) {
                mc = &mm->mm_child[c];
 
@@ -560,7 +696,7 @@ vdev_mirror_io_done(zio_t *zio)
        if (good_copies && spa_writeable(zio->io_spa) &&
            (unexpected_errors ||
            (zio->io_flags & ZIO_FLAG_RESILVER) ||
-           ((zio->io_flags & ZIO_FLAG_SCRUB) && mm->mm_replacing))) {
+           ((zio->io_flags & ZIO_FLAG_SCRUB) && mm->mm_resilvering))) {
                /*
                 * Use the good data we have in hand to repair damaged children.
                 */
@@ -577,7 +713,21 @@ vdev_mirror_io_done(zio_t *zio)
                        if (mc->mc_error == 0) {
                                if (mc->mc_tried)
                                        continue;
+                               /*
+                                * We didn't try this child.  We need to
+                                * repair it if:
+                                * 1. it's a scrub (in which case we have
+                                * tried everything that was healthy)
+                                *  - or -
+                                * 2. it's an indirect vdev (in which case
+                                * it could point to any other vdev, which
+                                * might have a bad DTL)
+                                *  - or -
+                                * 3. the DTL indicates that this data is
+                                * missing from this vdev
+                                */
                                if (!(zio->io_flags & ZIO_FLAG_SCRUB) &&
+                                   mc->mc_vd->vdev_ops != &vdev_indirect_ops &&
                                    !vdev_dtl_contains(mc->mc_vd, DTL_PARTIAL,
                                    zio->io_txg, 1))
                                        continue;
@@ -597,13 +747,19 @@ vdev_mirror_io_done(zio_t *zio)
 static void
 vdev_mirror_state_change(vdev_t *vd, int faulted, int degraded)
 {
-       if (faulted == vd->vdev_children)
-               vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
-                   VDEV_AUX_NO_REPLICAS);
-       else if (degraded + faulted != 0)
+       if (faulted == vd->vdev_children) {
+               if (vdev_children_are_offline(vd)) {
+                       vdev_set_state(vd, B_FALSE, VDEV_STATE_OFFLINE,
+                           VDEV_AUX_CHILDREN_OFFLINE);
+               } else {
+                       vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
+                           VDEV_AUX_NO_REPLICAS);
+               }
+       } else if (degraded + faulted != 0) {
                vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE);
-       else
+       } else {
                vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE);
+       }
 }
 
 vdev_ops_t vdev_mirror_ops = {
@@ -615,6 +771,9 @@ vdev_ops_t vdev_mirror_ops = {
        vdev_mirror_state_change,
        NULL,
        NULL,
+       NULL,
+       NULL,
+       vdev_default_xlate,
        VDEV_TYPE_MIRROR,       /* name of this vdev type */
        B_FALSE                 /* not a leaf vdev */
 };
@@ -628,6 +787,9 @@ vdev_ops_t vdev_replacing_ops = {
        vdev_mirror_state_change,
        NULL,
        NULL,
+       NULL,
+       NULL,
+       vdev_default_xlate,
        VDEV_TYPE_REPLACING,    /* name of this vdev type */
        B_FALSE                 /* not a leaf vdev */
 };
@@ -641,11 +803,15 @@ vdev_ops_t vdev_spare_ops = {
        vdev_mirror_state_change,
        NULL,
        NULL,
+       NULL,
+       NULL,
+       vdev_default_xlate,
        VDEV_TYPE_SPARE,        /* name of this vdev type */
        B_FALSE                 /* not a leaf vdev */
 };
 
-#if defined(_KERNEL) && defined(HAVE_SPL)
+#if defined(_KERNEL)
+/* BEGIN CSTYLED */
 module_param(zfs_vdev_mirror_rotating_inc, int, 0644);
 MODULE_PARM_DESC(zfs_vdev_mirror_rotating_inc,
        "Rotating media load increment for non-seeking I/O's");
@@ -655,6 +821,7 @@ MODULE_PARM_DESC(zfs_vdev_mirror_rotating_seek_inc,
        "Rotating media load increment for seeking I/O's");
 
 module_param(zfs_vdev_mirror_rotating_seek_offset, int, 0644);
+
 MODULE_PARM_DESC(zfs_vdev_mirror_rotating_seek_offset,
        "Offset in bytes from the last I/O which "
        "triggers a reduced rotating media seek increment");
@@ -666,5 +833,5 @@ MODULE_PARM_DESC(zfs_vdev_mirror_non_rotating_inc,
 module_param(zfs_vdev_mirror_non_rotating_seek_inc, int, 0644);
 MODULE_PARM_DESC(zfs_vdev_mirror_non_rotating_seek_inc,
        "Non-rotating media load increment for seeking I/O's");
-
+/* END CSTYLED */
 #endif