Home | History | Annotate | Line # | Download | only in zfs
      1      1.1  haad /*
      2      1.1  haad  * CDDL HEADER START
      3      1.1  haad  *
      4      1.1  haad  * The contents of this file are subject to the terms of the
      5      1.1  haad  * Common Development and Distribution License (the "License").
      6      1.1  haad  * You may not use this file except in compliance with the License.
      7      1.1  haad  *
      8      1.1  haad  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9      1.1  haad  * or http://www.opensolaris.org/os/licensing.
     10      1.1  haad  * See the License for the specific language governing permissions
     11      1.1  haad  * and limitations under the License.
     12      1.1  haad  *
     13      1.1  haad  * When distributing Covered Code, include this CDDL HEADER in each
     14      1.1  haad  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15      1.1  haad  * If applicable, add the following below this CDDL HEADER, with the
     16      1.1  haad  * fields enclosed by brackets "[]" replaced with your own identifying
     17      1.1  haad  * information: Portions Copyright [yyyy] [name of copyright owner]
     18      1.1  haad  *
     19      1.1  haad  * CDDL HEADER END
     20      1.1  haad  */
     21      1.1  haad /*
     22  1.1.1.2  haad  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
     23      1.1  haad  * Use is subject to license terms.
     24      1.1  haad  */
     25      1.1  haad 
     26  1.1.1.3   chs /*
     27  1.1.1.3   chs  * Copyright (c) 2012 by Delphix. All rights reserved.
     28  1.1.1.3   chs  */
     29  1.1.1.3   chs 
     30      1.1  haad #include <sys/spa.h>
     31      1.1  haad #include <sys/spa_impl.h>
     32      1.1  haad #include <sys/vdev.h>
     33      1.1  haad #include <sys/vdev_impl.h>
     34      1.1  haad #include <sys/zio.h>
     35  1.1.1.2  haad #include <sys/zio_checksum.h>
     36      1.1  haad 
     37      1.1  haad #include <sys/fm/fs/zfs.h>
     38      1.1  haad #include <sys/fm/protocol.h>
     39      1.1  haad #include <sys/fm/util.h>
     40      1.1  haad #include <sys/sysevent.h>
     41      1.1  haad 
     42      1.1  haad /*
     43      1.1  haad  * This general routine is responsible for generating all the different ZFS
     44      1.1  haad  * ereports.  The payload is dependent on the class, and which arguments are
     45      1.1  haad  * supplied to the function:
     46      1.1  haad  *
     47      1.1  haad  * 	EREPORT			POOL	VDEV	IO
     48      1.1  haad  * 	block			X	X	X
     49      1.1  haad  * 	data			X		X
     50      1.1  haad  * 	device			X	X
     51      1.1  haad  * 	pool			X
     52      1.1  haad  *
     53      1.1  haad  * If we are in a loading state, all errors are chained together by the same
     54      1.1  haad  * SPA-wide ENA (Error Numeric Association).
     55      1.1  haad  *
     56      1.1  haad  * For isolated I/O requests, we get the ENA from the zio_t. The propagation
     57      1.1  haad  * gets very complicated due to RAID-Z, gang blocks, and vdev caching.  We want
     58      1.1  haad  * to chain together all ereports associated with a logical piece of data.  For
     59      1.1  haad  * read I/Os, there  are basically three 'types' of I/O, which form a roughly
     60      1.1  haad  * layered diagram:
     61      1.1  haad  *
     62      1.1  haad  *      +---------------+
     63      1.1  haad  * 	| Aggregate I/O |	No associated logical data or device
     64      1.1  haad  * 	+---------------+
     65      1.1  haad  *              |
     66      1.1  haad  *              V
     67      1.1  haad  * 	+---------------+	Reads associated with a piece of logical data.
     68      1.1  haad  * 	|   Read I/O    |	This includes reads on behalf of RAID-Z,
     69      1.1  haad  * 	+---------------+       mirrors, gang blocks, retries, etc.
     70      1.1  haad  *              |
     71      1.1  haad  *              V
     72      1.1  haad  * 	+---------------+	Reads associated with a particular device, but
     73      1.1  haad  * 	| Physical I/O  |	no logical data.  Issued as part of vdev caching
     74      1.1  haad  * 	+---------------+	and I/O aggregation.
     75      1.1  haad  *
     76      1.1  haad  * Note that 'physical I/O' here is not the same terminology as used in the rest
     77      1.1  haad  * of ZIO.  Typically, 'physical I/O' simply means that there is no attached
     78      1.1  haad  * blockpointer.  But I/O with no associated block pointer can still be related
     79      1.1  haad  * to a logical piece of data (i.e. RAID-Z requests).
     80      1.1  haad  *
     81      1.1  haad  * Purely physical I/O always have unique ENAs.  They are not related to a
     82      1.1  haad  * particular piece of logical data, and therefore cannot be chained together.
     83      1.1  haad  * We still generate an ereport, but the DE doesn't correlate it with any
     84      1.1  haad  * logical piece of data.  When such an I/O fails, the delegated I/O requests
     85      1.1  haad  * will issue a retry, which will trigger the 'real' ereport with the correct
     86      1.1  haad  * ENA.
     87      1.1  haad  *
     88      1.1  haad  * We keep track of the ENA for a ZIO chain through the 'io_logical' member.
     89      1.1  haad  * When a new logical I/O is issued, we set this to point to itself.  Child I/Os
     90      1.1  haad  * then inherit this pointer, so that when it is first set subsequent failures
     91      1.1  haad  * will use the same ENA.  For vdev cache fill and queue aggregation I/O,
     92      1.1  haad  * this pointer is set to NULL, and no ereport will be generated (since it
     93      1.1  haad  * doesn't actually correspond to any particular device or piece of data,
     94      1.1  haad  * and the caller will always retry without caching or queueing anyway).
     95  1.1.1.2  haad  *
     96  1.1.1.2  haad  * For checksum errors, we want to include more information about the actual
     97  1.1.1.2  haad  * error which occurs.  Accordingly, we build an ereport when the error is
     98  1.1.1.2  haad  * noticed, but instead of sending it in immediately, we hang it off of the
     99  1.1.1.2  haad  * io_cksum_report field of the logical IO.  When the logical IO completes
    100  1.1.1.2  haad  * (successfully or not), zfs_ereport_finish_checksum() is called with the
    101  1.1.1.2  haad  * good and bad versions of the buffer (if available), and we annotate the
    102  1.1.1.2  haad  * ereport with information about the differences.
    103      1.1  haad  */
    104  1.1.1.2  haad #ifdef _KERNEL
    105  1.1.1.2  haad static void
    106  1.1.1.2  haad zfs_ereport_start(nvlist_t **ereport_out, nvlist_t **detector_out,
    107  1.1.1.2  haad     const char *subclass, spa_t *spa, vdev_t *vd, zio_t *zio,
    108      1.1  haad     uint64_t stateoroffset, uint64_t size)
    109      1.1  haad {
    110      1.1  haad 	nvlist_t *ereport, *detector;
    111  1.1.1.2  haad 
    112      1.1  haad 	uint64_t ena;
    113      1.1  haad 	char class[64];
    114      1.1  haad 
    115      1.1  haad 	/*
    116  1.1.1.2  haad 	 * If we are doing a spa_tryimport() or in recovery mode,
    117  1.1.1.2  haad 	 * ignore errors.
    118      1.1  haad 	 */
    119  1.1.1.2  haad 	if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT ||
    120  1.1.1.2  haad 	    spa_load_state(spa) == SPA_LOAD_RECOVER)
    121      1.1  haad 		return;
    122      1.1  haad 
    123      1.1  haad 	/*
    124      1.1  haad 	 * If we are in the middle of opening a pool, and the previous attempt
    125      1.1  haad 	 * failed, don't bother logging any new ereports - we're just going to
    126      1.1  haad 	 * get the same diagnosis anyway.
    127      1.1  haad 	 */
    128  1.1.1.2  haad 	if (spa_load_state(spa) != SPA_LOAD_NONE &&
    129      1.1  haad 	    spa->spa_last_open_failed)
    130      1.1  haad 		return;
    131      1.1  haad 
    132      1.1  haad 	if (zio != NULL) {
    133      1.1  haad 		/*
    134      1.1  haad 		 * If this is not a read or write zio, ignore the error.  This
    135      1.1  haad 		 * can occur if the DKIOCFLUSHWRITECACHE ioctl fails.
    136      1.1  haad 		 */
    137      1.1  haad 		if (zio->io_type != ZIO_TYPE_READ &&
    138      1.1  haad 		    zio->io_type != ZIO_TYPE_WRITE)
    139      1.1  haad 			return;
    140      1.1  haad 
    141      1.1  haad 		/*
    142      1.1  haad 		 * Ignore any errors from speculative I/Os, as failure is an
    143      1.1  haad 		 * expected result.
    144      1.1  haad 		 */
    145      1.1  haad 		if (zio->io_flags & ZIO_FLAG_SPECULATIVE)
    146      1.1  haad 			return;
    147      1.1  haad 
    148      1.1  haad 		/*
    149  1.1.1.2  haad 		 * If this I/O is not a retry I/O, don't post an ereport.
    150  1.1.1.2  haad 		 * Otherwise, we risk making bad diagnoses based on B_FAILFAST
    151  1.1.1.2  haad 		 * I/Os.
    152      1.1  haad 		 */
    153  1.1.1.2  haad 		if (zio->io_error == EIO &&
    154  1.1.1.2  haad 		    !(zio->io_flags & ZIO_FLAG_IO_RETRY))
    155      1.1  haad 			return;
    156  1.1.1.2  haad 
    157  1.1.1.2  haad 		if (vd != NULL) {
    158  1.1.1.2  haad 			/*
    159  1.1.1.2  haad 			 * If the vdev has already been marked as failing due
    160  1.1.1.2  haad 			 * to a failed probe, then ignore any subsequent I/O
    161  1.1.1.2  haad 			 * errors, as the DE will automatically fault the vdev
    162  1.1.1.2  haad 			 * on the first such failure.  This also catches cases
    163  1.1.1.2  haad 			 * where vdev_remove_wanted is set and the device has
    164  1.1.1.2  haad 			 * not yet been asynchronously placed into the REMOVED
    165  1.1.1.2  haad 			 * state.
    166  1.1.1.2  haad 			 */
    167  1.1.1.2  haad 			if (zio->io_vd == vd && !vdev_accessible(vd, zio))
    168  1.1.1.2  haad 				return;
    169  1.1.1.2  haad 
    170  1.1.1.2  haad 			/*
    171  1.1.1.2  haad 			 * Ignore checksum errors for reads from DTL regions of
    172  1.1.1.2  haad 			 * leaf vdevs.
    173  1.1.1.2  haad 			 */
    174  1.1.1.2  haad 			if (zio->io_type == ZIO_TYPE_READ &&
    175  1.1.1.2  haad 			    zio->io_error == ECKSUM &&
    176  1.1.1.2  haad 			    vd->vdev_ops->vdev_op_leaf &&
    177  1.1.1.2  haad 			    vdev_dtl_contains(vd, DTL_MISSING, zio->io_txg, 1))
    178  1.1.1.2  haad 				return;
    179  1.1.1.2  haad 		}
    180      1.1  haad 	}
    181      1.1  haad 
    182  1.1.1.2  haad 	/*
    183  1.1.1.2  haad 	 * For probe failure, we want to avoid posting ereports if we've
    184  1.1.1.2  haad 	 * already removed the device in the meantime.
    185  1.1.1.2  haad 	 */
    186  1.1.1.2  haad 	if (vd != NULL &&
    187  1.1.1.2  haad 	    strcmp(subclass, FM_EREPORT_ZFS_PROBE_FAILURE) == 0 &&
    188  1.1.1.2  haad 	    (vd->vdev_remove_wanted || vd->vdev_state == VDEV_STATE_REMOVED))
    189  1.1.1.2  haad 		return;
    190  1.1.1.2  haad 
    191      1.1  haad 	if ((ereport = fm_nvlist_create(NULL)) == NULL)
    192      1.1  haad 		return;
    193      1.1  haad 
    194      1.1  haad 	if ((detector = fm_nvlist_create(NULL)) == NULL) {
    195      1.1  haad 		fm_nvlist_destroy(ereport, FM_NVA_FREE);
    196      1.1  haad 		return;
    197      1.1  haad 	}
    198      1.1  haad 
    199      1.1  haad 	/*
    200      1.1  haad 	 * Serialize ereport generation
    201      1.1  haad 	 */
    202      1.1  haad 	mutex_enter(&spa->spa_errlist_lock);
    203      1.1  haad 
    204      1.1  haad 	/*
    205      1.1  haad 	 * Determine the ENA to use for this event.  If we are in a loading
    206      1.1  haad 	 * state, use a SPA-wide ENA.  Otherwise, if we are in an I/O state, use
    207      1.1  haad 	 * a root zio-wide ENA.  Otherwise, simply use a unique ENA.
    208      1.1  haad 	 */
    209  1.1.1.2  haad 	if (spa_load_state(spa) != SPA_LOAD_NONE) {
    210      1.1  haad 		if (spa->spa_ena == 0)
    211      1.1  haad 			spa->spa_ena = fm_ena_generate(0, FM_ENA_FMT1);
    212      1.1  haad 		ena = spa->spa_ena;
    213      1.1  haad 	} else if (zio != NULL && zio->io_logical != NULL) {
    214      1.1  haad 		if (zio->io_logical->io_ena == 0)
    215      1.1  haad 			zio->io_logical->io_ena =
    216      1.1  haad 			    fm_ena_generate(0, FM_ENA_FMT1);
    217      1.1  haad 		ena = zio->io_logical->io_ena;
    218      1.1  haad 	} else {
    219      1.1  haad 		ena = fm_ena_generate(0, FM_ENA_FMT1);
    220      1.1  haad 	}
    221      1.1  haad 
    222      1.1  haad 	/*
    223      1.1  haad 	 * Construct the full class, detector, and other standard FMA fields.
    224      1.1  haad 	 */
    225      1.1  haad 	(void) snprintf(class, sizeof (class), "%s.%s",
    226      1.1  haad 	    ZFS_ERROR_CLASS, subclass);
    227      1.1  haad 
    228      1.1  haad 	fm_fmri_zfs_set(detector, FM_ZFS_SCHEME_VERSION, spa_guid(spa),
    229      1.1  haad 	    vd != NULL ? vd->vdev_guid : 0);
    230      1.1  haad 
    231      1.1  haad 	fm_ereport_set(ereport, FM_EREPORT_VERSION, class, ena, detector, NULL);
    232      1.1  haad 
    233      1.1  haad 	/*
    234      1.1  haad 	 * Construct the per-ereport payload, depending on which parameters are
    235      1.1  haad 	 * passed in.
    236      1.1  haad 	 */
    237      1.1  haad 
    238      1.1  haad 	/*
    239      1.1  haad 	 * Generic payload members common to all ereports.
    240      1.1  haad 	 */
    241      1.1  haad 	fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_POOL,
    242      1.1  haad 	    DATA_TYPE_STRING, spa_name(spa), FM_EREPORT_PAYLOAD_ZFS_POOL_GUID,
    243      1.1  haad 	    DATA_TYPE_UINT64, spa_guid(spa),
    244      1.1  haad 	    FM_EREPORT_PAYLOAD_ZFS_POOL_CONTEXT, DATA_TYPE_INT32,
    245  1.1.1.2  haad 	    spa_load_state(spa), NULL);
    246      1.1  haad 
    247      1.1  haad 	if (spa != NULL) {
    248      1.1  haad 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_POOL_FAILMODE,
    249      1.1  haad 		    DATA_TYPE_STRING,
    250      1.1  haad 		    spa_get_failmode(spa) == ZIO_FAILURE_MODE_WAIT ?
    251      1.1  haad 		    FM_EREPORT_FAILMODE_WAIT :
    252      1.1  haad 		    spa_get_failmode(spa) == ZIO_FAILURE_MODE_CONTINUE ?
    253      1.1  haad 		    FM_EREPORT_FAILMODE_CONTINUE : FM_EREPORT_FAILMODE_PANIC,
    254      1.1  haad 		    NULL);
    255      1.1  haad 	}
    256      1.1  haad 
    257      1.1  haad 	if (vd != NULL) {
    258      1.1  haad 		vdev_t *pvd = vd->vdev_parent;
    259      1.1  haad 
    260      1.1  haad 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID,
    261      1.1  haad 		    DATA_TYPE_UINT64, vd->vdev_guid,
    262      1.1  haad 		    FM_EREPORT_PAYLOAD_ZFS_VDEV_TYPE,
    263      1.1  haad 		    DATA_TYPE_STRING, vd->vdev_ops->vdev_op_type, NULL);
    264  1.1.1.2  haad 		if (vd->vdev_path != NULL)
    265      1.1  haad 			fm_payload_set(ereport,
    266      1.1  haad 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_PATH,
    267      1.1  haad 			    DATA_TYPE_STRING, vd->vdev_path, NULL);
    268  1.1.1.2  haad 		if (vd->vdev_devid != NULL)
    269      1.1  haad 			fm_payload_set(ereport,
    270      1.1  haad 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_DEVID,
    271      1.1  haad 			    DATA_TYPE_STRING, vd->vdev_devid, NULL);
    272  1.1.1.2  haad 		if (vd->vdev_fru != NULL)
    273  1.1.1.2  haad 			fm_payload_set(ereport,
    274  1.1.1.2  haad 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_FRU,
    275  1.1.1.2  haad 			    DATA_TYPE_STRING, vd->vdev_fru, NULL);
    276      1.1  haad 
    277      1.1  haad 		if (pvd != NULL) {
    278      1.1  haad 			fm_payload_set(ereport,
    279      1.1  haad 			    FM_EREPORT_PAYLOAD_ZFS_PARENT_GUID,
    280      1.1  haad 			    DATA_TYPE_UINT64, pvd->vdev_guid,
    281      1.1  haad 			    FM_EREPORT_PAYLOAD_ZFS_PARENT_TYPE,
    282      1.1  haad 			    DATA_TYPE_STRING, pvd->vdev_ops->vdev_op_type,
    283      1.1  haad 			    NULL);
    284      1.1  haad 			if (pvd->vdev_path)
    285      1.1  haad 				fm_payload_set(ereport,
    286      1.1  haad 				    FM_EREPORT_PAYLOAD_ZFS_PARENT_PATH,
    287      1.1  haad 				    DATA_TYPE_STRING, pvd->vdev_path, NULL);
    288      1.1  haad 			if (pvd->vdev_devid)
    289      1.1  haad 				fm_payload_set(ereport,
    290      1.1  haad 				    FM_EREPORT_PAYLOAD_ZFS_PARENT_DEVID,
    291      1.1  haad 				    DATA_TYPE_STRING, pvd->vdev_devid, NULL);
    292      1.1  haad 		}
    293      1.1  haad 	}
    294      1.1  haad 
    295      1.1  haad 	if (zio != NULL) {
    296      1.1  haad 		/*
    297      1.1  haad 		 * Payload common to all I/Os.
    298      1.1  haad 		 */
    299      1.1  haad 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_ERR,
    300      1.1  haad 		    DATA_TYPE_INT32, zio->io_error, NULL);
    301      1.1  haad 
    302      1.1  haad 		/*
    303      1.1  haad 		 * If the 'size' parameter is non-zero, it indicates this is a
    304      1.1  haad 		 * RAID-Z or other I/O where the physical offset and length are
    305      1.1  haad 		 * provided for us, instead of within the zio_t.
    306      1.1  haad 		 */
    307      1.1  haad 		if (vd != NULL) {
    308      1.1  haad 			if (size)
    309      1.1  haad 				fm_payload_set(ereport,
    310      1.1  haad 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET,
    311      1.1  haad 				    DATA_TYPE_UINT64, stateoroffset,
    312      1.1  haad 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE,
    313      1.1  haad 				    DATA_TYPE_UINT64, size, NULL);
    314      1.1  haad 			else
    315      1.1  haad 				fm_payload_set(ereport,
    316      1.1  haad 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET,
    317      1.1  haad 				    DATA_TYPE_UINT64, zio->io_offset,
    318      1.1  haad 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE,
    319      1.1  haad 				    DATA_TYPE_UINT64, zio->io_size, NULL);
    320      1.1  haad 		}
    321      1.1  haad 
    322      1.1  haad 		/*
    323      1.1  haad 		 * Payload for I/Os with corresponding logical information.
    324      1.1  haad 		 */
    325      1.1  haad 		if (zio->io_logical != NULL)
    326      1.1  haad 			fm_payload_set(ereport,
    327      1.1  haad 			    FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJSET,
    328      1.1  haad 			    DATA_TYPE_UINT64,
    329      1.1  haad 			    zio->io_logical->io_bookmark.zb_objset,
    330      1.1  haad 			    FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJECT,
    331      1.1  haad 			    DATA_TYPE_UINT64,
    332      1.1  haad 			    zio->io_logical->io_bookmark.zb_object,
    333      1.1  haad 			    FM_EREPORT_PAYLOAD_ZFS_ZIO_LEVEL,
    334      1.1  haad 			    DATA_TYPE_INT64,
    335      1.1  haad 			    zio->io_logical->io_bookmark.zb_level,
    336      1.1  haad 			    FM_EREPORT_PAYLOAD_ZFS_ZIO_BLKID,
    337      1.1  haad 			    DATA_TYPE_UINT64,
    338      1.1  haad 			    zio->io_logical->io_bookmark.zb_blkid, NULL);
    339      1.1  haad 	} else if (vd != NULL) {
    340      1.1  haad 		/*
    341      1.1  haad 		 * If we have a vdev but no zio, this is a device fault, and the
    342      1.1  haad 		 * 'stateoroffset' parameter indicates the previous state of the
    343      1.1  haad 		 * vdev.
    344      1.1  haad 		 */
    345      1.1  haad 		fm_payload_set(ereport,
    346      1.1  haad 		    FM_EREPORT_PAYLOAD_ZFS_PREV_STATE,
    347      1.1  haad 		    DATA_TYPE_UINT64, stateoroffset, NULL);
    348      1.1  haad 	}
    349  1.1.1.2  haad 
    350      1.1  haad 	mutex_exit(&spa->spa_errlist_lock);
    351      1.1  haad 
    352  1.1.1.2  haad 	*ereport_out = ereport;
    353  1.1.1.2  haad 	*detector_out = detector;
    354  1.1.1.2  haad }
    355  1.1.1.2  haad 
    356  1.1.1.2  haad /* if it's <= 128 bytes, save the corruption directly */
    357  1.1.1.2  haad #define	ZFM_MAX_INLINE		(128 / sizeof (uint64_t))
    358  1.1.1.2  haad 
    359  1.1.1.2  haad #define	MAX_RANGES		16
    360  1.1.1.2  haad 
    361  1.1.1.2  haad typedef struct zfs_ecksum_info {
    362  1.1.1.2  haad 	/* histograms of set and cleared bits by bit number in a 64-bit word */
    363  1.1.1.2  haad 	uint16_t zei_histogram_set[sizeof (uint64_t) * NBBY];
    364  1.1.1.2  haad 	uint16_t zei_histogram_cleared[sizeof (uint64_t) * NBBY];
    365  1.1.1.2  haad 
    366  1.1.1.2  haad 	/* inline arrays of bits set and cleared. */
    367  1.1.1.2  haad 	uint64_t zei_bits_set[ZFM_MAX_INLINE];
    368  1.1.1.2  haad 	uint64_t zei_bits_cleared[ZFM_MAX_INLINE];
    369  1.1.1.2  haad 
    370  1.1.1.2  haad 	/*
    371  1.1.1.2  haad 	 * for each range, the number of bits set and cleared.  The Hamming
    372  1.1.1.2  haad 	 * distance between the good and bad buffers is the sum of them all.
    373  1.1.1.2  haad 	 */
    374  1.1.1.2  haad 	uint32_t zei_range_sets[MAX_RANGES];
    375  1.1.1.2  haad 	uint32_t zei_range_clears[MAX_RANGES];
    376  1.1.1.2  haad 
    377  1.1.1.2  haad 	struct zei_ranges {
    378  1.1.1.2  haad 		uint32_t	zr_start;
    379  1.1.1.2  haad 		uint32_t	zr_end;
    380  1.1.1.2  haad 	} zei_ranges[MAX_RANGES];
    381  1.1.1.2  haad 
    382  1.1.1.2  haad 	size_t	zei_range_count;
    383  1.1.1.2  haad 	uint32_t zei_mingap;
    384  1.1.1.2  haad 	uint32_t zei_allowed_mingap;
    385  1.1.1.2  haad 
    386  1.1.1.2  haad } zfs_ecksum_info_t;
    387  1.1.1.2  haad 
    388  1.1.1.2  haad static void
    389  1.1.1.2  haad update_histogram(uint64_t value_arg, uint16_t *hist, uint32_t *count)
    390  1.1.1.2  haad {
    391  1.1.1.2  haad 	size_t i;
    392  1.1.1.2  haad 	size_t bits = 0;
    393  1.1.1.2  haad 	uint64_t value = BE_64(value_arg);
    394  1.1.1.2  haad 
    395  1.1.1.2  haad 	/* We store the bits in big-endian (largest-first) order */
    396  1.1.1.2  haad 	for (i = 0; i < 64; i++) {
    397  1.1.1.2  haad 		if (value & (1ull << i)) {
    398  1.1.1.2  haad 			hist[63 - i]++;
    399  1.1.1.2  haad 			++bits;
    400  1.1.1.2  haad 		}
    401  1.1.1.2  haad 	}
    402  1.1.1.2  haad 	/* update the count of bits changed */
    403  1.1.1.2  haad 	*count += bits;
    404  1.1.1.2  haad }
    405  1.1.1.2  haad 
    406  1.1.1.2  haad /*
    407  1.1.1.2  haad  * We've now filled up the range array, and need to increase "mingap" and
    408  1.1.1.2  haad  * shrink the range list accordingly.  zei_mingap is always the smallest
    409  1.1.1.2  haad  * distance between array entries, so we set the new_allowed_gap to be
    410  1.1.1.2  haad  * one greater than that.  We then go through the list, joining together
    411  1.1.1.2  haad  * any ranges which are closer than the new_allowed_gap.
    412  1.1.1.2  haad  *
    413  1.1.1.2  haad  * By construction, there will be at least one.  We also update zei_mingap
    414  1.1.1.2  haad  * to the new smallest gap, to prepare for our next invocation.
    415  1.1.1.2  haad  */
    416  1.1.1.2  haad static void
    417  1.1.1.2  haad shrink_ranges(zfs_ecksum_info_t *eip)
    418  1.1.1.2  haad {
    419  1.1.1.2  haad 	uint32_t mingap = UINT32_MAX;
    420  1.1.1.2  haad 	uint32_t new_allowed_gap = eip->zei_mingap + 1;
    421  1.1.1.2  haad 
    422  1.1.1.2  haad 	size_t idx, output;
    423  1.1.1.2  haad 	size_t max = eip->zei_range_count;
    424  1.1.1.2  haad 
    425  1.1.1.2  haad 	struct zei_ranges *r = eip->zei_ranges;
    426  1.1.1.2  haad 
    427  1.1.1.2  haad 	ASSERT3U(eip->zei_range_count, >, 0);
    428  1.1.1.2  haad 	ASSERT3U(eip->zei_range_count, <=, MAX_RANGES);
    429  1.1.1.2  haad 
    430  1.1.1.2  haad 	output = idx = 0;
    431  1.1.1.2  haad 	while (idx < max - 1) {
    432  1.1.1.2  haad 		uint32_t start = r[idx].zr_start;
    433  1.1.1.2  haad 		uint32_t end = r[idx].zr_end;
    434  1.1.1.2  haad 
    435  1.1.1.2  haad 		while (idx < max - 1) {
    436  1.1.1.2  haad 			idx++;
    437  1.1.1.2  haad 
    438  1.1.1.2  haad 			uint32_t nstart = r[idx].zr_start;
    439  1.1.1.2  haad 			uint32_t nend = r[idx].zr_end;
    440  1.1.1.2  haad 
    441  1.1.1.2  haad 			uint32_t gap = nstart - end;
    442  1.1.1.2  haad 			if (gap < new_allowed_gap) {
    443  1.1.1.2  haad 				end = nend;
    444  1.1.1.2  haad 				continue;
    445  1.1.1.2  haad 			}
    446  1.1.1.2  haad 			if (gap < mingap)
    447  1.1.1.2  haad 				mingap = gap;
    448  1.1.1.2  haad 			break;
    449  1.1.1.2  haad 		}
    450  1.1.1.2  haad 		r[output].zr_start = start;
    451  1.1.1.2  haad 		r[output].zr_end = end;
    452  1.1.1.2  haad 		output++;
    453  1.1.1.2  haad 	}
    454  1.1.1.2  haad 	ASSERT3U(output, <, eip->zei_range_count);
    455  1.1.1.2  haad 	eip->zei_range_count = output;
    456  1.1.1.2  haad 	eip->zei_mingap = mingap;
    457  1.1.1.2  haad 	eip->zei_allowed_mingap = new_allowed_gap;
    458  1.1.1.2  haad }
    459  1.1.1.2  haad 
    460  1.1.1.2  haad static void
    461  1.1.1.2  haad add_range(zfs_ecksum_info_t *eip, int start, int end)
    462  1.1.1.2  haad {
    463  1.1.1.2  haad 	struct zei_ranges *r = eip->zei_ranges;
    464  1.1.1.2  haad 	size_t count = eip->zei_range_count;
    465  1.1.1.2  haad 
    466  1.1.1.2  haad 	if (count >= MAX_RANGES) {
    467  1.1.1.2  haad 		shrink_ranges(eip);
    468  1.1.1.2  haad 		count = eip->zei_range_count;
    469  1.1.1.2  haad 	}
    470  1.1.1.2  haad 	if (count == 0) {
    471  1.1.1.2  haad 		eip->zei_mingap = UINT32_MAX;
    472  1.1.1.2  haad 		eip->zei_allowed_mingap = 1;
    473  1.1.1.2  haad 	} else {
    474  1.1.1.2  haad 		int gap = start - r[count - 1].zr_end;
    475  1.1.1.2  haad 
    476  1.1.1.2  haad 		if (gap < eip->zei_allowed_mingap) {
    477  1.1.1.2  haad 			r[count - 1].zr_end = end;
    478  1.1.1.2  haad 			return;
    479  1.1.1.2  haad 		}
    480  1.1.1.2  haad 		if (gap < eip->zei_mingap)
    481  1.1.1.2  haad 			eip->zei_mingap = gap;
    482  1.1.1.2  haad 	}
    483  1.1.1.2  haad 	r[count].zr_start = start;
    484  1.1.1.2  haad 	r[count].zr_end = end;
    485  1.1.1.2  haad 	eip->zei_range_count++;
    486  1.1.1.2  haad }
    487  1.1.1.2  haad 
    488  1.1.1.2  haad static size_t
    489  1.1.1.2  haad range_total_size(zfs_ecksum_info_t *eip)
    490  1.1.1.2  haad {
    491  1.1.1.2  haad 	struct zei_ranges *r = eip->zei_ranges;
    492  1.1.1.2  haad 	size_t count = eip->zei_range_count;
    493  1.1.1.2  haad 	size_t result = 0;
    494  1.1.1.2  haad 	size_t idx;
    495  1.1.1.2  haad 
    496  1.1.1.2  haad 	for (idx = 0; idx < count; idx++)
    497  1.1.1.2  haad 		result += (r[idx].zr_end - r[idx].zr_start);
    498  1.1.1.2  haad 
    499  1.1.1.2  haad 	return (result);
    500  1.1.1.2  haad }
    501  1.1.1.2  haad 
    502  1.1.1.2  haad static zfs_ecksum_info_t *
    503  1.1.1.2  haad annotate_ecksum(nvlist_t *ereport, zio_bad_cksum_t *info,
    504  1.1.1.2  haad     const uint8_t *goodbuf, const uint8_t *badbuf, size_t size,
    505  1.1.1.2  haad     boolean_t drop_if_identical)
    506  1.1.1.2  haad {
    507  1.1.1.2  haad 	const uint64_t *good = (const uint64_t *)goodbuf;
    508  1.1.1.2  haad 	const uint64_t *bad = (const uint64_t *)badbuf;
    509  1.1.1.2  haad 
    510  1.1.1.2  haad 	uint64_t allset = 0;
    511  1.1.1.2  haad 	uint64_t allcleared = 0;
    512  1.1.1.2  haad 
    513  1.1.1.2  haad 	size_t nui64s = size / sizeof (uint64_t);
    514  1.1.1.2  haad 
    515  1.1.1.2  haad 	size_t inline_size;
    516  1.1.1.2  haad 	int no_inline = 0;
    517  1.1.1.2  haad 	size_t idx;
    518  1.1.1.2  haad 	size_t range;
    519  1.1.1.2  haad 
    520  1.1.1.2  haad 	size_t offset = 0;
    521  1.1.1.2  haad 	ssize_t start = -1;
    522  1.1.1.2  haad 
    523  1.1.1.2  haad 	zfs_ecksum_info_t *eip = kmem_zalloc(sizeof (*eip), KM_SLEEP);
    524  1.1.1.2  haad 
    525  1.1.1.2  haad 	/* don't do any annotation for injected checksum errors */
    526  1.1.1.2  haad 	if (info != NULL && info->zbc_injected)
    527  1.1.1.2  haad 		return (eip);
    528  1.1.1.2  haad 
    529  1.1.1.2  haad 	if (info != NULL && info->zbc_has_cksum) {
    530  1.1.1.2  haad 		fm_payload_set(ereport,
    531  1.1.1.2  haad 		    FM_EREPORT_PAYLOAD_ZFS_CKSUM_EXPECTED,
    532  1.1.1.2  haad 		    DATA_TYPE_UINT64_ARRAY,
    533  1.1.1.2  haad 		    sizeof (info->zbc_expected) / sizeof (uint64_t),
    534  1.1.1.2  haad 		    (uint64_t *)&info->zbc_expected,
    535  1.1.1.2  haad 		    FM_EREPORT_PAYLOAD_ZFS_CKSUM_ACTUAL,
    536  1.1.1.2  haad 		    DATA_TYPE_UINT64_ARRAY,
    537  1.1.1.2  haad 		    sizeof (info->zbc_actual) / sizeof (uint64_t),
    538  1.1.1.2  haad 		    (uint64_t *)&info->zbc_actual,
    539  1.1.1.2  haad 		    FM_EREPORT_PAYLOAD_ZFS_CKSUM_ALGO,
    540  1.1.1.2  haad 		    DATA_TYPE_STRING,
    541  1.1.1.2  haad 		    info->zbc_checksum_name,
    542  1.1.1.2  haad 		    NULL);
    543  1.1.1.2  haad 
    544  1.1.1.2  haad 		if (info->zbc_byteswapped) {
    545  1.1.1.2  haad 			fm_payload_set(ereport,
    546  1.1.1.2  haad 			    FM_EREPORT_PAYLOAD_ZFS_CKSUM_BYTESWAP,
    547  1.1.1.2  haad 			    DATA_TYPE_BOOLEAN, 1,
    548  1.1.1.2  haad 			    NULL);
    549  1.1.1.2  haad 		}
    550  1.1.1.2  haad 	}
    551  1.1.1.2  haad 
    552  1.1.1.2  haad 	if (badbuf == NULL || goodbuf == NULL)
    553  1.1.1.2  haad 		return (eip);
    554  1.1.1.2  haad 
    555  1.1.1.2  haad 	ASSERT3U(nui64s, <=, UINT16_MAX);
    556  1.1.1.2  haad 	ASSERT3U(size, ==, nui64s * sizeof (uint64_t));
    557  1.1.1.2  haad 	ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
    558  1.1.1.2  haad 	ASSERT3U(size, <=, UINT32_MAX);
    559  1.1.1.2  haad 
    560  1.1.1.2  haad 	/* build up the range list by comparing the two buffers. */
    561  1.1.1.2  haad 	for (idx = 0; idx < nui64s; idx++) {
    562  1.1.1.2  haad 		if (good[idx] == bad[idx]) {
    563  1.1.1.2  haad 			if (start == -1)
    564  1.1.1.2  haad 				continue;
    565  1.1.1.2  haad 
    566  1.1.1.2  haad 			add_range(eip, start, idx);
    567  1.1.1.2  haad 			start = -1;
    568  1.1.1.2  haad 		} else {
    569  1.1.1.2  haad 			if (start != -1)
    570  1.1.1.2  haad 				continue;
    571  1.1.1.2  haad 
    572  1.1.1.2  haad 			start = idx;
    573  1.1.1.2  haad 		}
    574  1.1.1.2  haad 	}
    575  1.1.1.2  haad 	if (start != -1)
    576  1.1.1.2  haad 		add_range(eip, start, idx);
    577  1.1.1.2  haad 
    578  1.1.1.2  haad 	/* See if it will fit in our inline buffers */
    579  1.1.1.2  haad 	inline_size = range_total_size(eip);
    580  1.1.1.2  haad 	if (inline_size > ZFM_MAX_INLINE)
    581  1.1.1.2  haad 		no_inline = 1;
    582  1.1.1.2  haad 
    583  1.1.1.2  haad 	/*
    584  1.1.1.2  haad 	 * If there is no change and we want to drop if the buffers are
    585  1.1.1.2  haad 	 * identical, do so.
    586  1.1.1.2  haad 	 */
    587  1.1.1.2  haad 	if (inline_size == 0 && drop_if_identical) {
    588  1.1.1.2  haad 		kmem_free(eip, sizeof (*eip));
    589  1.1.1.2  haad 		return (NULL);
    590  1.1.1.2  haad 	}
    591  1.1.1.2  haad 
    592  1.1.1.2  haad 	/*
    593  1.1.1.2  haad 	 * Now walk through the ranges, filling in the details of the
    594  1.1.1.2  haad 	 * differences.  Also convert our uint64_t-array offsets to byte
    595  1.1.1.2  haad 	 * offsets.
    596  1.1.1.2  haad 	 */
    597  1.1.1.2  haad 	for (range = 0; range < eip->zei_range_count; range++) {
    598  1.1.1.2  haad 		size_t start = eip->zei_ranges[range].zr_start;
    599  1.1.1.2  haad 		size_t end = eip->zei_ranges[range].zr_end;
    600  1.1.1.2  haad 
    601  1.1.1.2  haad 		for (idx = start; idx < end; idx++) {
    602  1.1.1.2  haad 			uint64_t set, cleared;
    603  1.1.1.2  haad 
    604  1.1.1.2  haad 			// bits set in bad, but not in good
    605  1.1.1.2  haad 			set = ((~good[idx]) & bad[idx]);
    606  1.1.1.2  haad 			// bits set in good, but not in bad
    607  1.1.1.2  haad 			cleared = (good[idx] & (~bad[idx]));
    608  1.1.1.2  haad 
    609  1.1.1.2  haad 			allset |= set;
    610  1.1.1.2  haad 			allcleared |= cleared;
    611  1.1.1.2  haad 
    612  1.1.1.2  haad 			if (!no_inline) {
    613  1.1.1.2  haad 				ASSERT3U(offset, <, inline_size);
    614  1.1.1.2  haad 				eip->zei_bits_set[offset] = set;
    615  1.1.1.2  haad 				eip->zei_bits_cleared[offset] = cleared;
    616  1.1.1.2  haad 				offset++;
    617  1.1.1.2  haad 			}
    618  1.1.1.2  haad 
    619  1.1.1.2  haad 			update_histogram(set, eip->zei_histogram_set,
    620  1.1.1.2  haad 			    &eip->zei_range_sets[range]);
    621  1.1.1.2  haad 			update_histogram(cleared, eip->zei_histogram_cleared,
    622  1.1.1.2  haad 			    &eip->zei_range_clears[range]);
    623  1.1.1.2  haad 		}
    624  1.1.1.2  haad 
    625  1.1.1.2  haad 		/* convert to byte offsets */
    626  1.1.1.2  haad 		eip->zei_ranges[range].zr_start	*= sizeof (uint64_t);
    627  1.1.1.2  haad 		eip->zei_ranges[range].zr_end	*= sizeof (uint64_t);
    628  1.1.1.2  haad 	}
    629  1.1.1.2  haad 	eip->zei_allowed_mingap	*= sizeof (uint64_t);
    630  1.1.1.2  haad 	inline_size		*= sizeof (uint64_t);
    631  1.1.1.2  haad 
    632  1.1.1.2  haad 	/* fill in ereport */
    633  1.1.1.2  haad 	fm_payload_set(ereport,
    634  1.1.1.2  haad 	    FM_EREPORT_PAYLOAD_ZFS_BAD_OFFSET_RANGES,
    635  1.1.1.2  haad 	    DATA_TYPE_UINT32_ARRAY, 2 * eip->zei_range_count,
    636  1.1.1.2  haad 	    (uint32_t *)eip->zei_ranges,
    637  1.1.1.2  haad 	    FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_MIN_GAP,
    638  1.1.1.2  haad 	    DATA_TYPE_UINT32, eip->zei_allowed_mingap,
    639  1.1.1.2  haad 	    FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_SETS,
    640  1.1.1.2  haad 	    DATA_TYPE_UINT32_ARRAY, eip->zei_range_count, eip->zei_range_sets,
    641  1.1.1.2  haad 	    FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_CLEARS,
    642  1.1.1.2  haad 	    DATA_TYPE_UINT32_ARRAY, eip->zei_range_count, eip->zei_range_clears,
    643  1.1.1.2  haad 	    NULL);
    644  1.1.1.2  haad 
    645  1.1.1.2  haad 	if (!no_inline) {
    646  1.1.1.2  haad 		fm_payload_set(ereport,
    647  1.1.1.2  haad 		    FM_EREPORT_PAYLOAD_ZFS_BAD_SET_BITS,
    648  1.1.1.2  haad 		    DATA_TYPE_UINT8_ARRAY,
    649  1.1.1.2  haad 		    inline_size, (uint8_t *)eip->zei_bits_set,
    650  1.1.1.2  haad 		    FM_EREPORT_PAYLOAD_ZFS_BAD_CLEARED_BITS,
    651  1.1.1.2  haad 		    DATA_TYPE_UINT8_ARRAY,
    652  1.1.1.2  haad 		    inline_size, (uint8_t *)eip->zei_bits_cleared,
    653  1.1.1.2  haad 		    NULL);
    654  1.1.1.2  haad 	} else {
    655  1.1.1.2  haad 		fm_payload_set(ereport,
    656  1.1.1.2  haad 		    FM_EREPORT_PAYLOAD_ZFS_BAD_SET_HISTOGRAM,
    657  1.1.1.2  haad 		    DATA_TYPE_UINT16_ARRAY,
    658  1.1.1.2  haad 		    NBBY * sizeof (uint64_t), eip->zei_histogram_set,
    659  1.1.1.2  haad 		    FM_EREPORT_PAYLOAD_ZFS_BAD_CLEARED_HISTOGRAM,
    660  1.1.1.2  haad 		    DATA_TYPE_UINT16_ARRAY,
    661  1.1.1.2  haad 		    NBBY * sizeof (uint64_t), eip->zei_histogram_cleared,
    662  1.1.1.2  haad 		    NULL);
    663  1.1.1.2  haad 	}
    664  1.1.1.2  haad 	return (eip);
    665  1.1.1.2  haad }
    666  1.1.1.2  haad #endif
    667  1.1.1.2  haad 
    668  1.1.1.2  haad void
    669  1.1.1.2  haad zfs_ereport_post(const char *subclass, spa_t *spa, vdev_t *vd, zio_t *zio,
    670  1.1.1.2  haad     uint64_t stateoroffset, uint64_t size)
    671  1.1.1.2  haad {
    672  1.1.1.2  haad #ifdef _KERNEL
    673  1.1.1.2  haad 	nvlist_t *ereport = NULL;
    674  1.1.1.2  haad 	nvlist_t *detector = NULL;
    675  1.1.1.2  haad 
    676  1.1.1.2  haad 	zfs_ereport_start(&ereport, &detector,
    677  1.1.1.2  haad 	    subclass, spa, vd, zio, stateoroffset, size);
    678  1.1.1.2  haad 
    679  1.1.1.2  haad 	if (ereport == NULL)
    680  1.1.1.2  haad 		return;
    681  1.1.1.2  haad 
    682      1.1  haad 	fm_ereport_post(ereport, EVCH_SLEEP);
    683      1.1  haad 
    684      1.1  haad 	fm_nvlist_destroy(ereport, FM_NVA_FREE);
    685      1.1  haad 	fm_nvlist_destroy(detector, FM_NVA_FREE);
    686      1.1  haad #endif
    687      1.1  haad }
    688      1.1  haad 
    689  1.1.1.2  haad void
    690  1.1.1.2  haad zfs_ereport_start_checksum(spa_t *spa, vdev_t *vd,
    691  1.1.1.2  haad     struct zio *zio, uint64_t offset, uint64_t length, void *arg,
    692  1.1.1.2  haad     zio_bad_cksum_t *info)
    693  1.1.1.2  haad {
    694  1.1.1.2  haad 	zio_cksum_report_t *report = kmem_zalloc(sizeof (*report), KM_SLEEP);
    695  1.1.1.2  haad 
    696  1.1.1.2  haad 	if (zio->io_vsd != NULL)
    697  1.1.1.2  haad 		zio->io_vsd_ops->vsd_cksum_report(zio, report, arg);
    698  1.1.1.2  haad 	else
    699  1.1.1.2  haad 		zio_vsd_default_cksum_report(zio, report, arg);
    700  1.1.1.2  haad 
    701  1.1.1.2  haad 	/* copy the checksum failure information if it was provided */
    702  1.1.1.2  haad 	if (info != NULL) {
    703  1.1.1.2  haad 		report->zcr_ckinfo = kmem_zalloc(sizeof (*info), KM_SLEEP);
    704  1.1.1.2  haad 		bcopy(info, report->zcr_ckinfo, sizeof (*info));
    705  1.1.1.2  haad 	}
    706  1.1.1.2  haad 
    707  1.1.1.2  haad 	report->zcr_align = 1ULL << vd->vdev_top->vdev_ashift;
    708  1.1.1.2  haad 	report->zcr_length = length;
    709  1.1.1.2  haad 
    710  1.1.1.2  haad #ifdef _KERNEL
    711  1.1.1.2  haad 	zfs_ereport_start(&report->zcr_ereport, &report->zcr_detector,
    712  1.1.1.2  haad 	    FM_EREPORT_ZFS_CHECKSUM, spa, vd, zio, offset, length);
    713  1.1.1.2  haad 
    714  1.1.1.2  haad 	if (report->zcr_ereport == NULL) {
    715  1.1.1.2  haad 		report->zcr_free(report->zcr_cbdata, report->zcr_cbinfo);
    716  1.1.1.3   chs 		if (report->zcr_ckinfo != NULL) {
    717  1.1.1.3   chs 			kmem_free(report->zcr_ckinfo,
    718  1.1.1.3   chs 			    sizeof (*report->zcr_ckinfo));
    719  1.1.1.3   chs 		}
    720  1.1.1.2  haad 		kmem_free(report, sizeof (*report));
    721  1.1.1.2  haad 		return;
    722  1.1.1.2  haad 	}
    723  1.1.1.2  haad #endif
    724  1.1.1.2  haad 
    725  1.1.1.2  haad 	mutex_enter(&spa->spa_errlist_lock);
    726  1.1.1.2  haad 	report->zcr_next = zio->io_logical->io_cksum_report;
    727  1.1.1.2  haad 	zio->io_logical->io_cksum_report = report;
    728  1.1.1.2  haad 	mutex_exit(&spa->spa_errlist_lock);
    729  1.1.1.2  haad }
    730  1.1.1.2  haad 
    731  1.1.1.2  haad void
    732  1.1.1.2  haad zfs_ereport_finish_checksum(zio_cksum_report_t *report,
    733  1.1.1.2  haad     const void *good_data, const void *bad_data, boolean_t drop_if_identical)
    734  1.1.1.2  haad {
    735  1.1.1.2  haad #ifdef _KERNEL
    736  1.1.1.2  haad 	zfs_ecksum_info_t *info = NULL;
    737  1.1.1.2  haad 	info = annotate_ecksum(report->zcr_ereport, report->zcr_ckinfo,
    738  1.1.1.2  haad 	    good_data, bad_data, report->zcr_length, drop_if_identical);
    739  1.1.1.2  haad 
    740  1.1.1.2  haad 	if (info != NULL)
    741  1.1.1.2  haad 		fm_ereport_post(report->zcr_ereport, EVCH_SLEEP);
    742  1.1.1.2  haad 
    743  1.1.1.2  haad 	fm_nvlist_destroy(report->zcr_ereport, FM_NVA_FREE);
    744  1.1.1.2  haad 	fm_nvlist_destroy(report->zcr_detector, FM_NVA_FREE);
    745  1.1.1.2  haad 	report->zcr_ereport = report->zcr_detector = NULL;
    746  1.1.1.2  haad 
    747  1.1.1.2  haad 	if (info != NULL)
    748  1.1.1.2  haad 		kmem_free(info, sizeof (*info));
    749  1.1.1.2  haad #endif
    750  1.1.1.2  haad }
    751  1.1.1.2  haad 
    752  1.1.1.2  haad void
    753  1.1.1.2  haad zfs_ereport_free_checksum(zio_cksum_report_t *rpt)
    754  1.1.1.2  haad {
    755  1.1.1.2  haad #ifdef _KERNEL
    756  1.1.1.2  haad 	if (rpt->zcr_ereport != NULL) {
    757  1.1.1.2  haad 		fm_nvlist_destroy(rpt->zcr_ereport,
    758  1.1.1.2  haad 		    FM_NVA_FREE);
    759  1.1.1.2  haad 		fm_nvlist_destroy(rpt->zcr_detector,
    760  1.1.1.2  haad 		    FM_NVA_FREE);
    761  1.1.1.2  haad 	}
    762  1.1.1.2  haad #endif
    763  1.1.1.2  haad 	rpt->zcr_free(rpt->zcr_cbdata, rpt->zcr_cbinfo);
    764  1.1.1.2  haad 
    765  1.1.1.2  haad 	if (rpt->zcr_ckinfo != NULL)
    766  1.1.1.2  haad 		kmem_free(rpt->zcr_ckinfo, sizeof (*rpt->zcr_ckinfo));
    767  1.1.1.2  haad 
    768  1.1.1.2  haad 	kmem_free(rpt, sizeof (*rpt));
    769  1.1.1.2  haad }
    770  1.1.1.2  haad 
    771  1.1.1.2  haad void
    772  1.1.1.2  haad zfs_ereport_send_interim_checksum(zio_cksum_report_t *report)
    773  1.1.1.2  haad {
    774  1.1.1.2  haad #ifdef _KERNEL
    775  1.1.1.2  haad 	fm_ereport_post(report->zcr_ereport, EVCH_SLEEP);
    776  1.1.1.2  haad #endif
    777  1.1.1.2  haad }
    778  1.1.1.2  haad 
    779  1.1.1.2  haad void
    780  1.1.1.2  haad zfs_ereport_post_checksum(spa_t *spa, vdev_t *vd,
    781  1.1.1.2  haad     struct zio *zio, uint64_t offset, uint64_t length,
    782  1.1.1.2  haad     const void *good_data, const void *bad_data, zio_bad_cksum_t *zbc)
    783  1.1.1.2  haad {
    784  1.1.1.2  haad #ifdef _KERNEL
    785  1.1.1.2  haad 	nvlist_t *ereport = NULL;
    786  1.1.1.2  haad 	nvlist_t *detector = NULL;
    787  1.1.1.2  haad 	zfs_ecksum_info_t *info;
    788  1.1.1.2  haad 
    789  1.1.1.2  haad 	zfs_ereport_start(&ereport, &detector,
    790  1.1.1.2  haad 	    FM_EREPORT_ZFS_CHECKSUM, spa, vd, zio, offset, length);
    791  1.1.1.2  haad 
    792  1.1.1.2  haad 	if (ereport == NULL)
    793  1.1.1.2  haad 		return;
    794  1.1.1.2  haad 
    795  1.1.1.2  haad 	info = annotate_ecksum(ereport, zbc, good_data, bad_data, length,
    796  1.1.1.2  haad 	    B_FALSE);
    797  1.1.1.2  haad 
    798  1.1.1.2  haad 	if (info != NULL)
    799  1.1.1.2  haad 		fm_ereport_post(ereport, EVCH_SLEEP);
    800  1.1.1.2  haad 
    801  1.1.1.2  haad 	fm_nvlist_destroy(ereport, FM_NVA_FREE);
    802  1.1.1.2  haad 	fm_nvlist_destroy(detector, FM_NVA_FREE);
    803  1.1.1.2  haad 
    804  1.1.1.2  haad 	if (info != NULL)
    805  1.1.1.2  haad 		kmem_free(info, sizeof (*info));
    806  1.1.1.2  haad #endif
    807  1.1.1.2  haad }
    808  1.1.1.2  haad 
    809      1.1  haad static void
    810      1.1  haad zfs_post_common(spa_t *spa, vdev_t *vd, const char *name)
    811      1.1  haad {
    812      1.1  haad #ifdef _KERNEL
    813      1.1  haad 	nvlist_t *resource;
    814      1.1  haad 	char class[64];
    815      1.1  haad 
    816  1.1.1.2  haad 	if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT)
    817  1.1.1.2  haad 		return;
    818  1.1.1.2  haad 
    819      1.1  haad 	if ((resource = fm_nvlist_create(NULL)) == NULL)
    820      1.1  haad 		return;
    821      1.1  haad 
    822      1.1  haad 	(void) snprintf(class, sizeof (class), "%s.%s.%s", FM_RSRC_RESOURCE,
    823      1.1  haad 	    ZFS_ERROR_CLASS, name);
    824      1.1  haad 	VERIFY(nvlist_add_uint8(resource, FM_VERSION, FM_RSRC_VERSION) == 0);
    825      1.1  haad 	VERIFY(nvlist_add_string(resource, FM_CLASS, class) == 0);
    826      1.1  haad 	VERIFY(nvlist_add_uint64(resource,
    827      1.1  haad 	    FM_EREPORT_PAYLOAD_ZFS_POOL_GUID, spa_guid(spa)) == 0);
    828      1.1  haad 	if (vd)
    829      1.1  haad 		VERIFY(nvlist_add_uint64(resource,
    830      1.1  haad 		    FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID, vd->vdev_guid) == 0);
    831      1.1  haad 
    832      1.1  haad 	fm_ereport_post(resource, EVCH_SLEEP);
    833      1.1  haad 
    834      1.1  haad 	fm_nvlist_destroy(resource, FM_NVA_FREE);
    835      1.1  haad #endif
    836      1.1  haad }
    837      1.1  haad 
    838      1.1  haad /*
    839      1.1  haad  * The 'resource.fs.zfs.removed' event is an internal signal that the given vdev
    840      1.1  haad  * has been removed from the system.  This will cause the DE to ignore any
    841      1.1  haad  * recent I/O errors, inferring that they are due to the asynchronous device
    842      1.1  haad  * removal.
    843      1.1  haad  */
    844      1.1  haad void
    845      1.1  haad zfs_post_remove(spa_t *spa, vdev_t *vd)
    846      1.1  haad {
    847      1.1  haad 	zfs_post_common(spa, vd, FM_RESOURCE_REMOVED);
    848      1.1  haad }
    849      1.1  haad 
    850      1.1  haad /*
    851      1.1  haad  * The 'resource.fs.zfs.autoreplace' event is an internal signal that the pool
    852      1.1  haad  * has the 'autoreplace' property set, and therefore any broken vdevs will be
    853      1.1  haad  * handled by higher level logic, and no vdev fault should be generated.
    854      1.1  haad  */
    855      1.1  haad void
    856      1.1  haad zfs_post_autoreplace(spa_t *spa, vdev_t *vd)
    857      1.1  haad {
    858      1.1  haad 	zfs_post_common(spa, vd, FM_RESOURCE_AUTOREPLACE);
    859      1.1  haad }
    860  1.1.1.2  haad 
    861  1.1.1.2  haad /*
    862  1.1.1.2  haad  * The 'resource.fs.zfs.statechange' event is an internal signal that the
    863  1.1.1.2  haad  * given vdev has transitioned its state to DEGRADED or HEALTHY.  This will
    864  1.1.1.2  haad  * cause the retire agent to repair any outstanding fault management cases
    865  1.1.1.2  haad  * open because the device was not found (fault.fs.zfs.device).
    866  1.1.1.2  haad  */
    867  1.1.1.2  haad void
    868  1.1.1.2  haad zfs_post_state_change(spa_t *spa, vdev_t *vd)
    869  1.1.1.2  haad {
    870  1.1.1.2  haad 	zfs_post_common(spa, vd, FM_RESOURCE_STATECHANGE);
    871  1.1.1.2  haad }
    872