3246 ZFS I/O deadman thread
[zfs.git] / module / zfs / zfs_fm.c
index 8b7785f..af2030a 100644 (file)
  * Use is subject to license terms.
  */
 
+/*
+ * Copyright (c) 2012 by Delphix. All rights reserved.
+ */
+
 #include <sys/spa.h>
 #include <sys/spa_impl.h>
 #include <sys/vdev.h>
 #include <sys/vdev_impl.h>
 #include <sys/zio.h>
+#include <sys/zio_checksum.h>
 
 #include <sys/fm/fs/zfs.h>
 #include <sys/fm/protocol.h>
  * this pointer is set to NULL, and no ereport will be generated (since it
  * doesn't actually correspond to any particular device or piece of data,
  * and the caller will always retry without caching or queueing anyway).
+ *
+ * For checksum errors, we want to include more information about the actual
+ * error which occurs.  Accordingly, we build an ereport when the error is
+ * noticed, but instead of sending it in immediately, we hang it off of the
+ * io_cksum_report field of the logical IO.  When the logical IO completes
+ * (successfully or not), zfs_ereport_finish_checksum() is called with the
+ * good and bad versions of the buffer (if available), and we annotate the
+ * ereport with information about the differences.
  */
-void
-zfs_ereport_post(const char *subclass, spa_t *spa, vdev_t *vd, zio_t *zio,
+#ifdef _KERNEL
+static void
+zfs_zevent_post_cb(nvlist_t *nvl, nvlist_t *detector)
+{
+       if (nvl)
+               fm_nvlist_destroy(nvl, FM_NVA_FREE);
+
+       if (detector)
+               fm_nvlist_destroy(detector, FM_NVA_FREE);
+}
+
+static void
+zfs_ereport_start(nvlist_t **ereport_out, nvlist_t **detector_out,
+    const char *subclass, spa_t *spa, vdev_t *vd, zio_t *zio,
     uint64_t stateoroffset, uint64_t size)
 {
-#ifdef _KERNEL
        nvlist_t *ereport, *detector;
+
        uint64_t ena;
        char class[64];
 
        /*
-        * If we are doing a spa_tryimport(), ignore errors.
+        * If we are doing a spa_tryimport() or in recovery mode,
+        * ignore errors.
         */
-       if (spa->spa_load_state == SPA_LOAD_TRYIMPORT)
+       if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT ||
+           spa_load_state(spa) == SPA_LOAD_RECOVER)
                return;
 
        /*
@@ -108,7 +135,7 @@ zfs_ereport_post(const char *subclass, spa_t *spa, vdev_t *vd, zio_t *zio,
         * failed, don't bother logging any new ereports - we're just going to
         * get the same diagnosis anyway.
         */
-       if (spa->spa_load_state != SPA_LOAD_NONE &&
+       if (spa_load_state(spa) != SPA_LOAD_NONE &&
            spa->spa_last_open_failed)
                return;
 
@@ -121,22 +148,6 @@ zfs_ereport_post(const char *subclass, spa_t *spa, vdev_t *vd, zio_t *zio,
                    zio->io_type != ZIO_TYPE_WRITE)
                        return;
 
-               /*
-                * Ignore any errors from speculative I/Os, as failure is an
-                * expected result.
-                */
-               if (zio->io_flags & ZIO_FLAG_SPECULATIVE)
-                       return;
-
-               /*
-                * If this I/O is not a retry I/O, don't post an ereport.
-                * Otherwise, we risk making bad diagnoses based on B_FAILFAST
-                * I/Os.
-                */
-               if (zio->io_error == EIO &&
-                   !(zio->io_flags & ZIO_FLAG_IO_RETRY))
-                       return;
-
                if (vd != NULL) {
                        /*
                         * If the vdev has already been marked as failing due
@@ -147,9 +158,7 @@ zfs_ereport_post(const char *subclass, spa_t *spa, vdev_t *vd, zio_t *zio,
                         * not yet been asynchronously placed into the REMOVED
                         * state.
                         */
-                       if (zio->io_vd == vd &&
-                           !vdev_accessible(vd, zio) &&
-                           strcmp(subclass, FM_EREPORT_ZFS_PROBE_FAILURE) != 0)
+                       if (zio->io_vd == vd && !vdev_accessible(vd, zio))
                                return;
 
                        /*
@@ -164,6 +173,15 @@ zfs_ereport_post(const char *subclass, spa_t *spa, vdev_t *vd, zio_t *zio,
                }
        }
 
+       /*
+        * For probe failure, we want to avoid posting ereports if we've
+        * already removed the device in the meantime.
+        */
+       if (vd != NULL &&
+           strcmp(subclass, FM_EREPORT_ZFS_PROBE_FAILURE) == 0 &&
+           (vd->vdev_remove_wanted || vd->vdev_state == VDEV_STATE_REMOVED))
+               return;
+
        if ((ereport = fm_nvlist_create(NULL)) == NULL)
                return;
 
@@ -182,7 +200,7 @@ zfs_ereport_post(const char *subclass, spa_t *spa, vdev_t *vd, zio_t *zio,
         * state, use a SPA-wide ENA.  Otherwise, if we are in an I/O state, use
         * a root zio-wide ENA.  Otherwise, simply use a unique ENA.
         */
-       if (spa->spa_load_state != SPA_LOAD_NONE) {
+       if (spa_load_state(spa) != SPA_LOAD_NONE) {
                if (spa->spa_ena == 0)
                        spa->spa_ena = fm_ena_generate(0, FM_ENA_FMT1);
                ena = spa->spa_ena;
@@ -218,7 +236,7 @@ zfs_ereport_post(const char *subclass, spa_t *spa, vdev_t *vd, zio_t *zio,
            DATA_TYPE_STRING, spa_name(spa), FM_EREPORT_PAYLOAD_ZFS_POOL_GUID,
            DATA_TYPE_UINT64, spa_guid(spa),
            FM_EREPORT_PAYLOAD_ZFS_POOL_CONTEXT, DATA_TYPE_INT32,
-           spa->spa_load_state, NULL);
+           spa_load_state(spa), NULL);
 
        if (spa != NULL) {
                fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_POOL_FAILMODE,
@@ -232,6 +250,7 @@ zfs_ereport_post(const char *subclass, spa_t *spa, vdev_t *vd, zio_t *zio,
 
        if (vd != NULL) {
                vdev_t *pvd = vd->vdev_parent;
+               vdev_queue_t *vq = &vd->vdev_queue;
 
                fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID,
                    DATA_TYPE_UINT64, vd->vdev_guid,
@@ -249,6 +268,19 @@ zfs_ereport_post(const char *subclass, spa_t *spa, vdev_t *vd, zio_t *zio,
                        fm_payload_set(ereport,
                            FM_EREPORT_PAYLOAD_ZFS_VDEV_FRU,
                            DATA_TYPE_STRING, vd->vdev_fru, NULL);
+               if (vd->vdev_ashift)
+                       fm_payload_set(ereport,
+                           FM_EREPORT_PAYLOAD_ZFS_VDEV_ASHIFT,
+                           DATA_TYPE_UINT64, vd->vdev_ashift, NULL);
+
+               if (vq != NULL) {
+                       fm_payload_set(ereport,
+                           FM_EREPORT_PAYLOAD_ZFS_VDEV_COMP_TS,
+                           DATA_TYPE_UINT64, vq->vq_io_complete_ts, NULL);
+                       fm_payload_set(ereport,
+                           FM_EREPORT_PAYLOAD_ZFS_VDEV_DELTA_TS,
+                           DATA_TYPE_UINT64, vq->vq_io_delta_ts, NULL);
+               }
 
                if (pvd != NULL) {
                        fm_payload_set(ereport,
@@ -274,6 +306,20 @@ zfs_ereport_post(const char *subclass, spa_t *spa, vdev_t *vd, zio_t *zio,
                 */
                fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_ERR,
                    DATA_TYPE_INT32, zio->io_error, NULL);
+               fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_FLAGS,
+                   DATA_TYPE_INT32, zio->io_flags, NULL);
+               fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_STAGE,
+                   DATA_TYPE_UINT32, zio->io_stage, NULL);
+               fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_PIPELINE,
+                   DATA_TYPE_UINT32, zio->io_pipeline, NULL);
+               fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_DELAY,
+                   DATA_TYPE_UINT64, zio->io_delay, NULL);
+               fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_TIMESTAMP,
+                   DATA_TYPE_UINT64, zio->io_timestamp, NULL);
+               fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_DEADLINE,
+                   DATA_TYPE_UINT64, zio->io_deadline, NULL);
+               fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_DELTA,
+                   DATA_TYPE_UINT64, zio->io_delta, NULL);
 
                /*
                 * If the 'size' parameter is non-zero, it indicates this is a
@@ -322,12 +368,456 @@ zfs_ereport_post(const char *subclass, spa_t *spa, vdev_t *vd, zio_t *zio,
                    FM_EREPORT_PAYLOAD_ZFS_PREV_STATE,
                    DATA_TYPE_UINT64, stateoroffset, NULL);
        }
+
+       mutex_exit(&spa->spa_errlist_lock);
+
+       *ereport_out = ereport;
+       *detector_out = detector;
+}
+
+/* if it's <= 128 bytes, save the corruption directly */
+#define        ZFM_MAX_INLINE          (128 / sizeof (uint64_t))
+
+#define        MAX_RANGES              16
+
+typedef struct zfs_ecksum_info {
+       /* histograms of set and cleared bits by bit number in a 64-bit word */
+       uint16_t zei_histogram_set[sizeof (uint64_t) * NBBY];
+       uint16_t zei_histogram_cleared[sizeof (uint64_t) * NBBY];
+
+       /* inline arrays of bits set and cleared. */
+       uint64_t zei_bits_set[ZFM_MAX_INLINE];
+       uint64_t zei_bits_cleared[ZFM_MAX_INLINE];
+
+       /*
+        * for each range, the number of bits set and cleared.  The Hamming
+        * distance between the good and bad buffers is the sum of them all.
+        */
+       uint32_t zei_range_sets[MAX_RANGES];
+       uint32_t zei_range_clears[MAX_RANGES];
+
+       struct zei_ranges {
+               uint32_t        zr_start;
+               uint32_t        zr_end;
+       } zei_ranges[MAX_RANGES];
+
+       size_t  zei_range_count;
+       uint32_t zei_mingap;
+       uint32_t zei_allowed_mingap;
+
+} zfs_ecksum_info_t;
+
+static void
+update_histogram(uint64_t value_arg, uint16_t *hist, uint32_t *count)
+{
+       size_t i;
+       size_t bits = 0;
+       uint64_t value = BE_64(value_arg);
+
+       /* We store the bits in big-endian (largest-first) order */
+       for (i = 0; i < 64; i++) {
+               if (value & (1ull << i)) {
+                       hist[63 - i]++;
+                       ++bits;
+               }
+       }
+       /* update the count of bits changed */
+       *count += bits;
+}
+
+/*
+ * We've now filled up the range array, and need to increase "mingap" and
+ * shrink the range list accordingly.  zei_mingap is always the smallest
+ * distance between array entries, so we set the new_allowed_gap to be
+ * one greater than that.  We then go through the list, joining together
+ * any ranges which are closer than the new_allowed_gap.
+ *
+ * By construction, there will be at least one.  We also update zei_mingap
+ * to the new smallest gap, to prepare for our next invocation.
+ */
+static void
+zei_shrink_ranges(zfs_ecksum_info_t *eip)
+{
+       uint32_t mingap = UINT32_MAX;
+       uint32_t new_allowed_gap = eip->zei_mingap + 1;
+
+       size_t idx, output;
+       size_t max = eip->zei_range_count;
+
+       struct zei_ranges *r = eip->zei_ranges;
+
+       ASSERT3U(eip->zei_range_count, >, 0);
+       ASSERT3U(eip->zei_range_count, <=, MAX_RANGES);
+
+       output = idx = 0;
+       while (idx < max - 1) {
+               uint32_t start = r[idx].zr_start;
+               uint32_t end = r[idx].zr_end;
+
+               while (idx < max - 1) {
+                       uint32_t nstart, nend, gap;
+
+                       idx++;
+                       nstart = r[idx].zr_start;
+                       nend = r[idx].zr_end;
+
+                       gap = nstart - end;
+                       if (gap < new_allowed_gap) {
+                               end = nend;
+                               continue;
+                       }
+                       if (gap < mingap)
+                               mingap = gap;
+                       break;
+               }
+               r[output].zr_start = start;
+               r[output].zr_end = end;
+               output++;
+       }
+       ASSERT3U(output, <, eip->zei_range_count);
+       eip->zei_range_count = output;
+       eip->zei_mingap = mingap;
+       eip->zei_allowed_mingap = new_allowed_gap;
+}
+
+static void
+zei_add_range(zfs_ecksum_info_t *eip, int start, int end)
+{
+       struct zei_ranges *r = eip->zei_ranges;
+       size_t count = eip->zei_range_count;
+
+       if (count >= MAX_RANGES) {
+               zei_shrink_ranges(eip);
+               count = eip->zei_range_count;
+       }
+       if (count == 0) {
+               eip->zei_mingap = UINT32_MAX;
+               eip->zei_allowed_mingap = 1;
+       } else {
+               int gap = start - r[count - 1].zr_end;
+
+               if (gap < eip->zei_allowed_mingap) {
+                       r[count - 1].zr_end = end;
+                       return;
+               }
+               if (gap < eip->zei_mingap)
+                       eip->zei_mingap = gap;
+       }
+       r[count].zr_start = start;
+       r[count].zr_end = end;
+       eip->zei_range_count++;
+}
+
+static size_t
+zei_range_total_size(zfs_ecksum_info_t *eip)
+{
+       struct zei_ranges *r = eip->zei_ranges;
+       size_t count = eip->zei_range_count;
+       size_t result = 0;
+       size_t idx;
+
+       for (idx = 0; idx < count; idx++)
+               result += (r[idx].zr_end - r[idx].zr_start);
+
+       return (result);
+}
+
+static zfs_ecksum_info_t *
+annotate_ecksum(nvlist_t *ereport, zio_bad_cksum_t *info,
+    const uint8_t *goodbuf, const uint8_t *badbuf, size_t size,
+    boolean_t drop_if_identical)
+{
+       const uint64_t *good = (const uint64_t *)goodbuf;
+       const uint64_t *bad = (const uint64_t *)badbuf;
+
+       uint64_t allset = 0;
+       uint64_t allcleared = 0;
+
+       size_t nui64s = size / sizeof (uint64_t);
+
+       size_t inline_size;
+       int no_inline = 0;
+       size_t idx;
+       size_t range;
+
+       size_t offset = 0;
+       ssize_t start = -1;
+
+       zfs_ecksum_info_t *eip = kmem_zalloc(sizeof (*eip), KM_PUSHPAGE);
+
+       /* don't do any annotation for injected checksum errors */
+       if (info != NULL && info->zbc_injected)
+               return (eip);
+
+       if (info != NULL && info->zbc_has_cksum) {
+               fm_payload_set(ereport,
+                   FM_EREPORT_PAYLOAD_ZFS_CKSUM_EXPECTED,
+                   DATA_TYPE_UINT64_ARRAY,
+                   sizeof (info->zbc_expected) / sizeof (uint64_t),
+                   (uint64_t *)&info->zbc_expected,
+                   FM_EREPORT_PAYLOAD_ZFS_CKSUM_ACTUAL,
+                   DATA_TYPE_UINT64_ARRAY,
+                   sizeof (info->zbc_actual) / sizeof (uint64_t),
+                   (uint64_t *)&info->zbc_actual,
+                   FM_EREPORT_PAYLOAD_ZFS_CKSUM_ALGO,
+                   DATA_TYPE_STRING,
+                   info->zbc_checksum_name,
+                   NULL);
+
+               if (info->zbc_byteswapped) {
+                       fm_payload_set(ereport,
+                           FM_EREPORT_PAYLOAD_ZFS_CKSUM_BYTESWAP,
+                           DATA_TYPE_BOOLEAN, 1,
+                           NULL);
+               }
+       }
+
+       if (badbuf == NULL || goodbuf == NULL)
+               return (eip);
+
+       ASSERT3U(nui64s, <=, UINT16_MAX);
+       ASSERT3U(size, ==, nui64s * sizeof (uint64_t));
+       ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
+       ASSERT3U(size, <=, UINT32_MAX);
+
+       /* build up the range list by comparing the two buffers. */
+       for (idx = 0; idx < nui64s; idx++) {
+               if (good[idx] == bad[idx]) {
+                       if (start == -1)
+                               continue;
+
+                       zei_add_range(eip, start, idx);
+                       start = -1;
+               } else {
+                       if (start != -1)
+                               continue;
+
+                       start = idx;
+               }
+       }
+       if (start != -1)
+               zei_add_range(eip, start, idx);
+
+       /* See if it will fit in our inline buffers */
+       inline_size = zei_range_total_size(eip);
+       if (inline_size > ZFM_MAX_INLINE)
+               no_inline = 1;
+
+       /*
+        * If there is no change and we want to drop if the buffers are
+        * identical, do so.
+        */
+       if (inline_size == 0 && drop_if_identical) {
+               kmem_free(eip, sizeof (*eip));
+               return (NULL);
+       }
+
+       /*
+        * Now walk through the ranges, filling in the details of the
+        * differences.  Also convert our uint64_t-array offsets to byte
+        * offsets.
+        */
+       for (range = 0; range < eip->zei_range_count; range++) {
+               size_t start = eip->zei_ranges[range].zr_start;
+               size_t end = eip->zei_ranges[range].zr_end;
+
+               for (idx = start; idx < end; idx++) {
+                       uint64_t set, cleared;
+
+                       // bits set in bad, but not in good
+                       set = ((~good[idx]) & bad[idx]);
+                       // bits set in good, but not in bad
+                       cleared = (good[idx] & (~bad[idx]));
+
+                       allset |= set;
+                       allcleared |= cleared;
+
+                       if (!no_inline) {
+                               ASSERT3U(offset, <, inline_size);
+                               eip->zei_bits_set[offset] = set;
+                               eip->zei_bits_cleared[offset] = cleared;
+                               offset++;
+                       }
+
+                       update_histogram(set, eip->zei_histogram_set,
+                           &eip->zei_range_sets[range]);
+                       update_histogram(cleared, eip->zei_histogram_cleared,
+                           &eip->zei_range_clears[range]);
+               }
+
+               /* convert to byte offsets */
+               eip->zei_ranges[range].zr_start *= sizeof (uint64_t);
+               eip->zei_ranges[range].zr_end   *= sizeof (uint64_t);
+       }
+       eip->zei_allowed_mingap *= sizeof (uint64_t);
+       inline_size             *= sizeof (uint64_t);
+
+       /* fill in ereport */
+       fm_payload_set(ereport,
+           FM_EREPORT_PAYLOAD_ZFS_BAD_OFFSET_RANGES,
+           DATA_TYPE_UINT32_ARRAY, 2 * eip->zei_range_count,
+           (uint32_t *)eip->zei_ranges,
+           FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_MIN_GAP,
+           DATA_TYPE_UINT32, eip->zei_allowed_mingap,
+           FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_SETS,
+           DATA_TYPE_UINT32_ARRAY, eip->zei_range_count, eip->zei_range_sets,
+           FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_CLEARS,
+           DATA_TYPE_UINT32_ARRAY, eip->zei_range_count, eip->zei_range_clears,
+           NULL);
+
+       if (!no_inline) {
+               fm_payload_set(ereport,
+                   FM_EREPORT_PAYLOAD_ZFS_BAD_SET_BITS,
+                   DATA_TYPE_UINT8_ARRAY,
+                   inline_size, (uint8_t *)eip->zei_bits_set,
+                   FM_EREPORT_PAYLOAD_ZFS_BAD_CLEARED_BITS,
+                   DATA_TYPE_UINT8_ARRAY,
+                   inline_size, (uint8_t *)eip->zei_bits_cleared,
+                   NULL);
+       } else {
+               fm_payload_set(ereport,
+                   FM_EREPORT_PAYLOAD_ZFS_BAD_SET_HISTOGRAM,
+                   DATA_TYPE_UINT16_ARRAY,
+                   NBBY * sizeof (uint64_t), eip->zei_histogram_set,
+                   FM_EREPORT_PAYLOAD_ZFS_BAD_CLEARED_HISTOGRAM,
+                   DATA_TYPE_UINT16_ARRAY,
+                   NBBY * sizeof (uint64_t), eip->zei_histogram_cleared,
+                   NULL);
+       }
+       return (eip);
+}
+#endif
+
+void
+zfs_ereport_post(const char *subclass, spa_t *spa, vdev_t *vd, zio_t *zio,
+    uint64_t stateoroffset, uint64_t size)
+{
+#ifdef _KERNEL
+       nvlist_t *ereport = NULL;
+       nvlist_t *detector = NULL;
+
+       zfs_ereport_start(&ereport, &detector,
+           subclass, spa, vd, zio, stateoroffset, size);
+
+       if (ereport == NULL)
+               return;
+
+       /* Cleanup is handled by the callback function */
+       zfs_zevent_post(ereport, detector, zfs_zevent_post_cb);
+#endif
+}
+
+void
+zfs_ereport_start_checksum(spa_t *spa, vdev_t *vd,
+    struct zio *zio, uint64_t offset, uint64_t length, void *arg,
+    zio_bad_cksum_t *info)
+{
+       zio_cksum_report_t *report = kmem_zalloc(sizeof (*report), KM_PUSHPAGE);
+
+       if (zio->io_vsd != NULL)
+               zio->io_vsd_ops->vsd_cksum_report(zio, report, arg);
+       else
+               zio_vsd_default_cksum_report(zio, report, arg);
+
+       /* copy the checksum failure information if it was provided */
+       if (info != NULL) {
+               report->zcr_ckinfo = kmem_zalloc(sizeof (*info), KM_PUSHPAGE);
+               bcopy(info, report->zcr_ckinfo, sizeof (*info));
+       }
+
+       report->zcr_align = 1ULL << vd->vdev_top->vdev_ashift;
+       report->zcr_length = length;
+
+#ifdef _KERNEL
+       zfs_ereport_start(&report->zcr_ereport, &report->zcr_detector,
+           FM_EREPORT_ZFS_CHECKSUM, spa, vd, zio, offset, length);
+
+       if (report->zcr_ereport == NULL) {
+               report->zcr_free(report->zcr_cbdata, report->zcr_cbinfo);
+               if (report->zcr_ckinfo != NULL) {
+                       kmem_free(report->zcr_ckinfo,
+                           sizeof (*report->zcr_ckinfo));
+               }
+               kmem_free(report, sizeof (*report));
+               return;
+       }
+#endif
+
+       mutex_enter(&spa->spa_errlist_lock);
+       report->zcr_next = zio->io_logical->io_cksum_report;
+       zio->io_logical->io_cksum_report = report;
        mutex_exit(&spa->spa_errlist_lock);
+}
 
-       fm_ereport_post(ereport, EVCH_SLEEP);
+void
+zfs_ereport_finish_checksum(zio_cksum_report_t *report,
+    const void *good_data, const void *bad_data, boolean_t drop_if_identical)
+{
+#ifdef _KERNEL
+       zfs_ecksum_info_t *info = NULL;
+       info = annotate_ecksum(report->zcr_ereport, report->zcr_ckinfo,
+           good_data, bad_data, report->zcr_length, drop_if_identical);
+
+       if (info != NULL)
+               zfs_zevent_post(report->zcr_ereport,
+                   report->zcr_detector, zfs_zevent_post_cb);
+
+       report->zcr_ereport = report->zcr_detector = NULL;
+       if (info != NULL)
+               kmem_free(info, sizeof (*info));
+#endif
+}
 
-       fm_nvlist_destroy(ereport, FM_NVA_FREE);
-       fm_nvlist_destroy(detector, FM_NVA_FREE);
+void
+zfs_ereport_free_checksum(zio_cksum_report_t *rpt)
+{
+#ifdef _KERNEL
+       if (rpt->zcr_ereport != NULL) {
+               fm_nvlist_destroy(rpt->zcr_ereport,
+                   FM_NVA_FREE);
+               fm_nvlist_destroy(rpt->zcr_detector,
+                   FM_NVA_FREE);
+       }
+#endif
+       rpt->zcr_free(rpt->zcr_cbdata, rpt->zcr_cbinfo);
+
+       if (rpt->zcr_ckinfo != NULL)
+               kmem_free(rpt->zcr_ckinfo, sizeof (*rpt->zcr_ckinfo));
+
+       kmem_free(rpt, sizeof (*rpt));
+}
+
+void
+zfs_ereport_send_interim_checksum(zio_cksum_report_t *report)
+{
+#ifdef _KERNEL
+       zfs_zevent_post(report->zcr_ereport, report->zcr_detector, NULL);
+#endif
+}
+
+void
+zfs_ereport_post_checksum(spa_t *spa, vdev_t *vd,
+    struct zio *zio, uint64_t offset, uint64_t length,
+    const void *good_data, const void *bad_data, zio_bad_cksum_t *zbc)
+{
+#ifdef _KERNEL
+       nvlist_t *ereport = NULL;
+       nvlist_t *detector = NULL;
+       zfs_ecksum_info_t *info;
+
+       zfs_ereport_start(&ereport, &detector,
+           FM_EREPORT_ZFS_CHECKSUM, spa, vd, zio, offset, length);
+
+       if (ereport == NULL)
+               return;
+
+       info = annotate_ecksum(ereport, zbc, good_data, bad_data, length,
+           B_FALSE);
+
+       if (info != NULL) {
+               zfs_zevent_post(ereport, detector, zfs_zevent_post_cb);
+               kmem_free(info, sizeof (*info));
+       }
 #endif
 }
 
@@ -338,6 +828,9 @@ zfs_post_common(spa_t *spa, vdev_t *vd, const char *name)
        nvlist_t *resource;
        char class[64];
 
+       if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT)
+               return;
+
        if ((resource = fm_nvlist_create(NULL)) == NULL)
                return;
 
@@ -347,13 +840,14 @@ zfs_post_common(spa_t *spa, vdev_t *vd, const char *name)
        VERIFY(nvlist_add_string(resource, FM_CLASS, class) == 0);
        VERIFY(nvlist_add_uint64(resource,
            FM_EREPORT_PAYLOAD_ZFS_POOL_GUID, spa_guid(spa)) == 0);
-       if (vd)
+       if (vd) {
                VERIFY(nvlist_add_uint64(resource,
                    FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID, vd->vdev_guid) == 0);
+               VERIFY(nvlist_add_uint64(resource,
+                   FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE, vd->vdev_state) == 0);
+       }
 
-       fm_ereport_post(resource, EVCH_SLEEP);
-
-       fm_nvlist_destroy(resource, FM_NVA_FREE);
+       zfs_zevent_post(resource, NULL, zfs_zevent_post_cb);
 #endif
 }
 
@@ -366,7 +860,7 @@ zfs_post_common(spa_t *spa, vdev_t *vd, const char *name)
 void
 zfs_post_remove(spa_t *spa, vdev_t *vd)
 {
-       zfs_post_common(spa, vd, FM_RESOURCE_REMOVED);
+       zfs_post_common(spa, vd, FM_EREPORT_RESOURCE_REMOVED);
 }
 
 /*
@@ -377,5 +871,25 @@ zfs_post_remove(spa_t *spa, vdev_t *vd)
 void
 zfs_post_autoreplace(spa_t *spa, vdev_t *vd)
 {
-       zfs_post_common(spa, vd, FM_RESOURCE_AUTOREPLACE);
+       zfs_post_common(spa, vd, FM_EREPORT_RESOURCE_AUTOREPLACE);
+}
+
+/*
+ * The 'resource.fs.zfs.statechange' event is an internal signal that the
+ * given vdev has transitioned its state to DEGRADED or HEALTHY.  This will
+ * cause the retire agent to repair any outstanding fault management cases
+ * open because the device was not found (fault.fs.zfs.device).
+ */
+void
+zfs_post_state_change(spa_t *spa, vdev_t *vd)
+{
+       zfs_post_common(spa, vd, FM_EREPORT_RESOURCE_STATECHANGE);
 }
+
+#if defined(_KERNEL) && defined(HAVE_SPL)
+EXPORT_SYMBOL(zfs_ereport_post);
+EXPORT_SYMBOL(zfs_ereport_post_checksum);
+EXPORT_SYMBOL(zfs_post_remove);
+EXPORT_SYMBOL(zfs_post_autoreplace);
+EXPORT_SYMBOL(zfs_post_state_change);
+#endif /* _KERNEL */