xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_fm.c (revision ea8dc4b6d2251b437950c0056bc626b311c73c27)
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 2006 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.
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 
103 	/*
104 	 * If we are doing a spa_tryimport(), ignore errors.
105 	 */
106 	if (spa->spa_load_state == SPA_LOAD_TRYIMPORT)
107 		return;
108 
109 	/*
110 	 * If we are in the middle of opening a pool, and the previous attempt
111 	 * failed, don't bother logging any new ereports - we're just going to
112 	 * get the same diagnosis anyway.
113 	 */
114 	if (spa->spa_load_state != SPA_LOAD_NONE &&
115 	    spa->spa_last_open_failed)
116 		return;
117 
118 	/*
119 	 * Ignore any errors from I/Os that we are going to retry anyway - we
120 	 * only generate errors from the final failure.
121 	 */
122 	if (zio && zio_should_retry(zio))
123 		return;
124 
125 	if ((ereport = fm_nvlist_create(NULL)) == NULL)
126 		return;
127 
128 	if ((detector = fm_nvlist_create(NULL)) == NULL) {
129 		fm_nvlist_destroy(ereport, FM_NVA_FREE);
130 		return;
131 	}
132 
133 	/*
134 	 * Serialize ereport generation
135 	 */
136 	mutex_enter(&spa->spa_errlist_lock);
137 
138 	/*
139 	 * Determine the ENA to use for this event.  If we are in a loading
140 	 * state, use a SPA-wide ENA.  Otherwise, if we are in an I/O state, use
141 	 * a root zio-wide ENA.  Otherwise, simply use a unique ENA.
142 	 */
143 	if (spa->spa_load_state != SPA_LOAD_NONE) {
144 		if (spa->spa_ena == 0)
145 			spa->spa_ena = fm_ena_generate(0, FM_ENA_FMT1);
146 		ena = spa->spa_ena;
147 	} else if (zio != NULL && zio->io_logical != NULL) {
148 		if (zio->io_logical->io_ena == 0)
149 			zio->io_logical->io_ena =
150 			    fm_ena_generate(0, FM_ENA_FMT1);
151 		ena = zio->io_logical->io_ena;
152 	} else {
153 		ena = fm_ena_generate(0, FM_ENA_FMT1);
154 	}
155 
156 	/*
157 	 * Construct the full class, detector, and other standard FMA fields.
158 	 */
159 	(void) snprintf(class, sizeof (class), "%s.%s",
160 	    ZFS_ERROR_CLASS, subclass);
161 
162 	fm_fmri_zfs_set(detector, FM_ZFS_SCHEME_VERSION, spa_guid(spa),
163 	    vd != NULL ? vd->vdev_guid : 0);
164 
165 	fm_ereport_set(ereport, FM_EREPORT_VERSION, class, ena, detector, NULL);
166 
167 	/*
168 	 * Construct the per-ereport payload, depending on which parameters are
169 	 * passed in.
170 	 */
171 
172 	/*
173 	 * Generic payload members common to all ereports.
174 	 *
175 	 * The direct reference to spa_name is used rather than spa_name()
176 	 * because of the asynchronous nature of the zio pipeline.  spa_name()
177 	 * asserts that the config lock is held in some form.  This is always
178 	 * the case in I/O context, but because the check for RW_WRITER compares
179 	 * against 'curthread', we may be in an asynchronous context and blow
180 	 * this assert.  Rather than loosen this assert, we acknowledge that all
181 	 * contexts in which this function is called (pool open, I/O) are safe,
182 	 * and dereference the name directly.
183 	 */
184 	fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_POOL,
185 	    DATA_TYPE_STRING, spa->spa_name, FM_EREPORT_PAYLOAD_ZFS_POOL_GUID,
186 	    DATA_TYPE_UINT64, spa_guid(spa),
187 	    FM_EREPORT_PAYLOAD_ZFS_POOL_CONTEXT, DATA_TYPE_INT32,
188 	    spa->spa_load_state, NULL);
189 
190 	if (vd != NULL) {
191 		vdev_t *pvd = vd->vdev_parent;
192 
193 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID,
194 		    DATA_TYPE_UINT64, vd->vdev_guid,
195 		    FM_EREPORT_PAYLOAD_ZFS_VDEV_TYPE,
196 		    DATA_TYPE_STRING, vd->vdev_ops->vdev_op_type, NULL);
197 		if (vd->vdev_path)
198 			fm_payload_set(ereport,
199 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_PATH,
200 			    DATA_TYPE_STRING, vd->vdev_path, NULL);
201 		if (vd->vdev_devid)
202 			fm_payload_set(ereport,
203 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_DEVID,
204 			    DATA_TYPE_STRING, vd->vdev_devid, NULL);
205 
206 		if (pvd != NULL) {
207 			fm_payload_set(ereport,
208 			    FM_EREPORT_PAYLOAD_ZFS_PARENT_GUID,
209 			    DATA_TYPE_UINT64, pvd->vdev_guid,
210 			    FM_EREPORT_PAYLOAD_ZFS_PARENT_TYPE,
211 			    DATA_TYPE_STRING, pvd->vdev_ops->vdev_op_type,
212 			    NULL);
213 			if (pvd->vdev_path)
214 				fm_payload_set(ereport,
215 				    FM_EREPORT_PAYLOAD_ZFS_PARENT_PATH,
216 				    DATA_TYPE_STRING, vd->vdev_path, NULL);
217 			if (pvd->vdev_devid)
218 				fm_payload_set(ereport,
219 				    FM_EREPORT_PAYLOAD_ZFS_PARENT_DEVID,
220 				    DATA_TYPE_STRING, pvd->vdev_devid, NULL);
221 		}
222 	}
223 
224 	if (zio != NULL) {
225 		/*
226 		 * Payload common to all I/Os.
227 		 */
228 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_ERR,
229 		    DATA_TYPE_INT32, zio->io_error, NULL);
230 
231 		/*
232 		 * If the 'size' parameter is non-zero, it indicates this is a
233 		 * RAID-Z or other I/O where the physical offset and length are
234 		 * provided for us, instead of within the zio_t.
235 		 */
236 		if (vd != NULL) {
237 			if (size)
238 				fm_payload_set(ereport,
239 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET,
240 				    DATA_TYPE_UINT64, stateoroffset,
241 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE,
242 				    DATA_TYPE_UINT64, size);
243 			else
244 				fm_payload_set(ereport,
245 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET,
246 				    DATA_TYPE_UINT64, zio->io_offset,
247 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE,
248 				    DATA_TYPE_UINT64, zio->io_size);
249 		}
250 
251 		/*
252 		 * Payload for I/Os with corresponding logical information.
253 		 */
254 		if (zio->io_logical != NULL)
255 			fm_payload_set(ereport,
256 			    FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJSET,
257 			    DATA_TYPE_UINT64,
258 			    zio->io_logical->io_bookmark.zb_objset,
259 			    FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJECT,
260 			    DATA_TYPE_UINT64,
261 			    zio->io_logical->io_bookmark.zb_object,
262 			    FM_EREPORT_PAYLOAD_ZFS_ZIO_LEVEL,
263 			    DATA_TYPE_INT32,
264 			    zio->io_logical->io_bookmark.zb_level,
265 			    FM_EREPORT_PAYLOAD_ZFS_ZIO_BLKID,
266 			    DATA_TYPE_UINT64,
267 			    zio->io_logical->io_bookmark.zb_blkid);
268 	} else if (vd != NULL) {
269 		/*
270 		 * If we have a vdev but no zio, this is a device fault, and the
271 		 * 'stateoroffset' parameter indicates the previous state of the
272 		 * vdev.
273 		 */
274 		fm_payload_set(ereport,
275 		    FM_EREPORT_PAYLOAD_ZFS_PREV_STATE,
276 		    DATA_TYPE_UINT64, stateoroffset, NULL);
277 	}
278 	mutex_exit(&spa->spa_errlist_lock);
279 
280 	fm_ereport_post(ereport, EVCH_SLEEP);
281 
282 	fm_nvlist_destroy(ereport, FM_NVA_FREE);
283 	fm_nvlist_destroy(detector, FM_NVA_FREE);
284 #endif
285 }
286 
287 /*
288  * The 'resource.fs.zfs.ok' event is an internal signal that the associated
289  * resource (pool or disk) has been identified by ZFS as healthy.  This will
290  * then trigger the DE to close the associated case, if any.
291  */
292 void
293 zfs_post_ok(spa_t *spa, vdev_t *vd)
294 {
295 #ifdef _KERNEL
296 	nvlist_t *resource;
297 	char class[64];
298 
299 	if ((resource = fm_nvlist_create(NULL)) == NULL)
300 		return;
301 
302 	(void) snprintf(class, sizeof (class), "%s.%s.%s", FM_RSRC_RESOURCE,
303 	    ZFS_ERROR_CLASS, FM_RESOURCE_OK);
304 	VERIFY(nvlist_add_uint8(resource, FM_VERSION, FM_RSRC_VERSION) == 0);
305 	VERIFY(nvlist_add_string(resource, FM_CLASS, class) == 0);
306 	VERIFY(nvlist_add_uint64(resource,
307 	    FM_EREPORT_PAYLOAD_ZFS_POOL_GUID, spa_guid(spa)) == 0);
308 	if (vd)
309 		VERIFY(nvlist_add_uint64(resource,
310 		    FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID, vd->vdev_guid) == 0);
311 
312 	fm_ereport_post(resource, EVCH_SLEEP);
313 
314 	fm_nvlist_destroy(resource, FM_NVA_FREE);
315 #endif
316 }
317