Home | History | Annotate | Download | only in zfs
      1 /*
      2  * CDDL HEADER START
      3  *
      4  * The contents of this file are subject to the terms of the
      5  * Common Development and Distribution License (the "License").
      6  * You may not use this file except in compliance with the License.
      7  *
      8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9  * or http://www.opensolaris.org/os/licensing.
     10  * See the License for the specific language governing permissions
     11  * and limitations under the License.
     12  *
     13  * When distributing Covered Code, include this CDDL HEADER in each
     14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15  * If applicable, add the following below this CDDL HEADER, with the
     16  * fields enclosed by brackets "[]" replaced with your own identifying
     17  * information: Portions Copyright [yyyy] [name of copyright owner]
     18  *
     19  * CDDL HEADER END
     20  */
     21 /*
     22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
     23  * Use is subject to license terms.
     24  */
     25 
     26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
     27 
     28 #include <sys/spa.h>
     29 #include <sys/spa_impl.h>
     30 #include <sys/vdev.h>
     31 #include <sys/vdev_impl.h>
     32 #include <sys/zio.h>
     33 
     34 #include <sys/fm/fs/zfs.h>
     35 #include <sys/fm/protocol.h>
     36 #include <sys/fm/util.h>
     37 #include <sys/sysevent.h>
     38 
     39 /*
     40  * This general routine is responsible for generating all the different ZFS
     41  * ereports.  The payload is dependent on the class, and which arguments are
     42  * supplied to the function:
     43  *
     44  * 	EREPORT			POOL	VDEV	IO
     45  * 	block			X	X	X
     46  * 	data			X		X
     47  * 	device			X	X
     48  * 	pool			X
     49  *
     50  * If we are in a loading state, all errors are chained together by the same
     51  * SPA-wide ENA (Error Numeric Association).
     52  *
     53  * For isolated I/O requests, we get the ENA from the zio_t. The propagation
     54  * gets very complicated due to RAID-Z, gang blocks, and vdev caching.  We want
     55  * to chain together all ereports associated with a logical piece of data.  For
     56  * read I/Os, there  are basically three 'types' of I/O, which form a roughly
     57  * layered diagram:
     58  *
     59  *      +---------------+
     60  * 	| Aggregate I/O |	No associated logical data or device
     61  * 	+---------------+
     62  *              |
     63  *              V
     64  * 	+---------------+	Reads associated with a piece of logical data.
     65  * 	|   Read I/O    |	This includes reads on behalf of RAID-Z,
     66  * 	+---------------+       mirrors, gang blocks, retries, etc.
     67  *              |
     68  *              V
     69  * 	+---------------+	Reads associated with a particular device, but
     70  * 	| Physical I/O  |	no logical data.  Issued as part of vdev caching
     71  * 	+---------------+	and I/O aggregation.
     72  *
     73  * Note that 'physical I/O' here is not the same terminology as used in the rest
     74  * of ZIO.  Typically, 'physical I/O' simply means that there is no attached
     75  * blockpointer.  But I/O with no associated block pointer can still be related
     76  * to a logical piece of data (i.e. RAID-Z requests).
     77  *
     78  * Purely physical I/O always have unique ENAs.  They are not related to a
     79  * particular piece of logical data, and therefore cannot be chained together.
     80  * We still generate an ereport, but the DE doesn't correlate it with any
     81  * logical piece of data.  When such an I/O fails, the delegated I/O requests
     82  * will issue a retry, which will trigger the 'real' ereport with the correct
     83  * ENA.
     84  *
     85  * We keep track of the ENA for a ZIO chain through the 'io_logical' member.
     86  * When a new logical I/O is issued, we set this to point to itself.  Child I/Os
     87  * then inherit this pointer, so that when it is first set subsequent failures
     88  * will use the same ENA.  If a physical I/O is issued (by passing the
     89  * ZIO_FLAG_NOBOOKMARK flag), then this pointer is reset, guaranteeing that a
     90  * unique ENA will be generated.  For an aggregate I/O, this pointer is set to
     91  * NULL, and no ereport will be generated (since it doesn't actually correspond
     92  * to any particular device or piece of data).
     93  */
     94 void
     95 zfs_ereport_post(const char *subclass, spa_t *spa, vdev_t *vd, zio_t *zio,
     96     uint64_t stateoroffset, uint64_t size)
     97 {
     98 #ifdef _KERNEL
     99 	nvlist_t *ereport, *detector;
    100 	uint64_t ena;
    101 	char class[64];
    102 	int state;
    103 
    104 	/*
    105 	 * If we are doing a spa_tryimport(), ignore errors.
    106 	 */
    107 	if (spa->spa_load_state == SPA_LOAD_TRYIMPORT)
    108 		return;
    109 
    110 	/*
    111 	 * If we are in the middle of opening a pool, and the previous attempt
    112 	 * failed, don't bother logging any new ereports - we're just going to
    113 	 * get the same diagnosis anyway.
    114 	 */
    115 	if (spa->spa_load_state != SPA_LOAD_NONE &&
    116 	    spa->spa_last_open_failed)
    117 		return;
    118 
    119 	if (zio != NULL) {
    120 		/*
    121 		 * Ignore any errors from I/Os that we are going to retry
    122 		 * anyway - we only generate errors from the final failure.
    123 		 * Checksum errors are generated after the pipeline stage
    124 		 * responsible for retrying the I/O (VDEV_IO_ASSESS), so this
    125 		 * only applies to standard I/O errors.
    126 		 */
    127 		if (zio_should_retry(zio) && zio->io_error != ECKSUM)
    128 			return;
    129 
    130 		/*
    131 		 * If this is not a read or write zio, ignore the error.  This
    132 		 * can occur if the DKIOCFLUSHWRITECACHE ioctl fails.
    133 		 */
    134 		if (zio->io_type != ZIO_TYPE_READ &&
    135 		    zio->io_type != ZIO_TYPE_WRITE)
    136 			return;
    137 
    138 		/*
    139 		 * Ignore any errors from speculative I/Os, as failure is an
    140 		 * expected result.
    141 		 */
    142 		if (zio->io_flags & ZIO_FLAG_SPECULATIVE)
    143 			return;
    144 	}
    145 
    146 	if ((ereport = fm_nvlist_create(NULL)) == NULL)
    147 		return;
    148 
    149 	if ((detector = fm_nvlist_create(NULL)) == NULL) {
    150 		fm_nvlist_destroy(ereport, FM_NVA_FREE);
    151 		return;
    152 	}
    153 
    154 	/*
    155 	 * Serialize ereport generation
    156 	 */
    157 	mutex_enter(&spa->spa_errlist_lock);
    158 
    159 	/*
    160 	 * Determine the ENA to use for this event.  If we are in a loading
    161 	 * state, use a SPA-wide ENA.  Otherwise, if we are in an I/O state, use
    162 	 * a root zio-wide ENA.  Otherwise, simply use a unique ENA.
    163 	 */
    164 	if (spa->spa_load_state != SPA_LOAD_NONE) {
    165 		if (spa->spa_ena == 0)
    166 			spa->spa_ena = fm_ena_generate(0, FM_ENA_FMT1);
    167 		ena = spa->spa_ena;
    168 	} else if (zio != NULL && zio->io_logical != NULL) {
    169 		if (zio->io_logical->io_ena == 0)
    170 			zio->io_logical->io_ena =
    171 			    fm_ena_generate(0, FM_ENA_FMT1);
    172 		ena = zio->io_logical->io_ena;
    173 	} else {
    174 		ena = fm_ena_generate(0, FM_ENA_FMT1);
    175 	}
    176 
    177 	/*
    178 	 * Construct the full class, detector, and other standard FMA fields.
    179 	 */
    180 	(void) snprintf(class, sizeof (class), "%s.%s",
    181 	    ZFS_ERROR_CLASS, subclass);
    182 
    183 	fm_fmri_zfs_set(detector, FM_ZFS_SCHEME_VERSION, spa_guid(spa),
    184 	    vd != NULL ? vd->vdev_guid : 0);
    185 
    186 	fm_ereport_set(ereport, FM_EREPORT_VERSION, class, ena, detector, NULL);
    187 
    188 	/*
    189 	 * Construct the per-ereport payload, depending on which parameters are
    190 	 * passed in.
    191 	 */
    192 
    193 	/*
    194 	 * If we are importing a faulted pool, then we treat it like an open,
    195 	 * not an import.  Otherwise, the DE will ignore all faults during
    196 	 * import, since the default behavior is to mark the devices as
    197 	 * persistently unavailable, not leave them in the faulted state.
    198 	 */
    199 	state = spa->spa_import_faulted ? SPA_LOAD_OPEN : spa->spa_load_state;
    200 
    201 	/*
    202 	 * Generic payload members common to all ereports.
    203 	 *
    204 	 * The direct reference to spa_name is used rather than spa_name()
    205 	 * because of the asynchronous nature of the zio pipeline.  spa_name()
    206 	 * asserts that the config lock is held in some form.  This is always
    207 	 * the case in I/O context, but because the check for RW_WRITER compares
    208 	 * against 'curthread', we may be in an asynchronous context and blow
    209 	 * this assert.  Rather than loosen this assert, we acknowledge that all
    210 	 * contexts in which this function is called (pool open, I/O) are safe,
    211 	 * and dereference the name directly.
    212 	 */
    213 	fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_POOL,
    214 	    DATA_TYPE_STRING, spa->spa_name, FM_EREPORT_PAYLOAD_ZFS_POOL_GUID,
    215 	    DATA_TYPE_UINT64, spa_guid(spa),
    216 	    FM_EREPORT_PAYLOAD_ZFS_POOL_CONTEXT, DATA_TYPE_INT32,
    217 	    state, NULL);
    218 
    219 	if (spa != NULL) {
    220 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_POOL_FAILMODE,
    221 		    DATA_TYPE_STRING,
    222 		    spa_get_failmode(spa) == ZIO_FAILURE_MODE_WAIT ?
    223 		    FM_EREPORT_FAILMODE_WAIT :
    224 		    spa_get_failmode(spa) == ZIO_FAILURE_MODE_CONTINUE ?
    225 		    FM_EREPORT_FAILMODE_CONTINUE : FM_EREPORT_FAILMODE_PANIC,
    226 		    NULL);
    227 	}
    228 
    229 	if (vd != NULL) {
    230 		vdev_t *pvd = vd->vdev_parent;
    231 
    232 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID,
    233 		    DATA_TYPE_UINT64, vd->vdev_guid,
    234 		    FM_EREPORT_PAYLOAD_ZFS_VDEV_TYPE,
    235 		    DATA_TYPE_STRING, vd->vdev_ops->vdev_op_type, NULL);
    236 		if (vd->vdev_path)
    237 			fm_payload_set(ereport,
    238 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_PATH,
    239 			    DATA_TYPE_STRING, vd->vdev_path, NULL);
    240 		if (vd->vdev_devid)
    241 			fm_payload_set(ereport,
    242 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_DEVID,
    243 			    DATA_TYPE_STRING, vd->vdev_devid, NULL);
    244 
    245 		if (pvd != NULL) {
    246 			fm_payload_set(ereport,
    247 			    FM_EREPORT_PAYLOAD_ZFS_PARENT_GUID,
    248 			    DATA_TYPE_UINT64, pvd->vdev_guid,
    249 			    FM_EREPORT_PAYLOAD_ZFS_PARENT_TYPE,
    250 			    DATA_TYPE_STRING, pvd->vdev_ops->vdev_op_type,
    251 			    NULL);
    252 			if (pvd->vdev_path)
    253 				fm_payload_set(ereport,
    254 				    FM_EREPORT_PAYLOAD_ZFS_PARENT_PATH,
    255 				    DATA_TYPE_STRING, pvd->vdev_path, NULL);
    256 			if (pvd->vdev_devid)
    257 				fm_payload_set(ereport,
    258 				    FM_EREPORT_PAYLOAD_ZFS_PARENT_DEVID,
    259 				    DATA_TYPE_STRING, pvd->vdev_devid, NULL);
    260 		}
    261 	}
    262 
    263 	if (zio != NULL) {
    264 		/*
    265 		 * Payload common to all I/Os.
    266 		 */
    267 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_ERR,
    268 		    DATA_TYPE_INT32, zio->io_error, NULL);
    269 
    270 		/*
    271 		 * If the 'size' parameter is non-zero, it indicates this is a
    272 		 * RAID-Z or other I/O where the physical offset and length are
    273 		 * provided for us, instead of within the zio_t.
    274 		 */
    275 		if (vd != NULL) {
    276 			if (size)
    277 				fm_payload_set(ereport,
    278 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET,
    279 				    DATA_TYPE_UINT64, stateoroffset,
    280 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE,
    281 				    DATA_TYPE_UINT64, size, NULL);
    282 			else
    283 				fm_payload_set(ereport,
    284 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET,
    285 				    DATA_TYPE_UINT64, zio->io_offset,
    286 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE,
    287 				    DATA_TYPE_UINT64, zio->io_size, NULL);
    288 		}
    289 
    290 		/*
    291 		 * Payload for I/Os with corresponding logical information.
    292 		 */
    293 		if (zio->io_logical != NULL)
    294 			fm_payload_set(ereport,
    295 			    FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJSET,
    296 			    DATA_TYPE_UINT64,
    297 			    zio->io_logical->io_bookmark.zb_objset,
    298 			    FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJECT,
    299 			    DATA_TYPE_UINT64,
    300 			    zio->io_logical->io_bookmark.zb_object,
    301 			    FM_EREPORT_PAYLOAD_ZFS_ZIO_LEVEL,
    302 			    DATA_TYPE_INT64,
    303 			    zio->io_logical->io_bookmark.zb_level,
    304 			    FM_EREPORT_PAYLOAD_ZFS_ZIO_BLKID,
    305 			    DATA_TYPE_UINT64,
    306 			    zio->io_logical->io_bookmark.zb_blkid, NULL);
    307 	} else if (vd != NULL) {
    308 		/*
    309 		 * If we have a vdev but no zio, this is a device fault, and the
    310 		 * 'stateoroffset' parameter indicates the previous state of the
    311 		 * vdev.
    312 		 */
    313 		fm_payload_set(ereport,
    314 		    FM_EREPORT_PAYLOAD_ZFS_PREV_STATE,
    315 		    DATA_TYPE_UINT64, stateoroffset, NULL);
    316 	}
    317 	mutex_exit(&spa->spa_errlist_lock);
    318 
    319 	fm_ereport_post(ereport, EVCH_SLEEP);
    320 
    321 	fm_nvlist_destroy(ereport, FM_NVA_FREE);
    322 	fm_nvlist_destroy(detector, FM_NVA_FREE);
    323 #endif
    324 }
    325 
    326 static void
    327 zfs_post_common(spa_t *spa, vdev_t *vd, const char *name)
    328 {
    329 #ifdef _KERNEL
    330 	nvlist_t *resource;
    331 	char class[64];
    332 
    333 	if ((resource = fm_nvlist_create(NULL)) == NULL)
    334 		return;
    335 
    336 	(void) snprintf(class, sizeof (class), "%s.%s.%s", FM_RSRC_RESOURCE,
    337 	    ZFS_ERROR_CLASS, name);
    338 	VERIFY(nvlist_add_uint8(resource, FM_VERSION, FM_RSRC_VERSION) == 0);
    339 	VERIFY(nvlist_add_string(resource, FM_CLASS, class) == 0);
    340 	VERIFY(nvlist_add_uint64(resource,
    341 	    FM_EREPORT_PAYLOAD_ZFS_POOL_GUID, spa_guid(spa)) == 0);
    342 	if (vd)
    343 		VERIFY(nvlist_add_uint64(resource,
    344 		    FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID, vd->vdev_guid) == 0);
    345 
    346 	fm_ereport_post(resource, EVCH_SLEEP);
    347 
    348 	fm_nvlist_destroy(resource, FM_NVA_FREE);
    349 #endif
    350 }
    351 
    352 /*
    353  * The 'resource.fs.zfs.removed' event is an internal signal that the given vdev
    354  * has been removed from the system.  This will cause the DE to ignore any
    355  * recent I/O errors, inferring that they are due to the asynchronous device
    356  * removal.
    357  */
    358 void
    359 zfs_post_remove(spa_t *spa, vdev_t *vd)
    360 {
    361 	zfs_post_common(spa, vd, FM_RESOURCE_REMOVED);
    362 }
    363 
    364 /*
    365  * The 'resource.fs.zfs.autoreplace' event is an internal signal that the pool
    366  * has the 'autoreplace' property set, and therefore any broken vdevs will be
    367  * handled by higher level logic, and no vdev fault should be generated.
    368  */
    369 void
    370 zfs_post_autoreplace(spa_t *spa, vdev_t *vd)
    371 {
    372 	zfs_post_common(spa, vd, FM_RESOURCE_AUTOREPLACE);
    373 }
    374