]> CyberLeo.Net >> Repos - FreeBSD/releng/8.2.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/zfs_fm.c
Copy stable/8 to releng/8.2 in preparation for FreeBSD-8.2 release.
[FreeBSD/releng/8.2.git] / sys / cddl / contrib / opensolaris / uts / common / fs / zfs / zfs_fm.c
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25
26 #include <sys/spa.h>
27 #include <sys/spa_impl.h>
28 #include <sys/vdev.h>
29 #include <sys/vdev_impl.h>
30 #include <sys/zio.h>
31
32 #include <sys/fm/fs/zfs.h>
33 #include <sys/fm/protocol.h>
34 #include <sys/fm/util.h>
35
36 #ifdef _KERNEL
37 /* Including sys/bus.h is just too hard, so I declare what I need here. */
38 extern void devctl_notify(const char *__system, const char *__subsystem,
39     const char *__type, const char *__data);
40 #endif
41
42 /*
43  * This general routine is responsible for generating all the different ZFS
44  * ereports.  The payload is dependent on the class, and which arguments are
45  * supplied to the function:
46  *
47  *      EREPORT                 POOL    VDEV    IO
48  *      block                   X       X       X
49  *      data                    X               X
50  *      device                  X       X
51  *      pool                    X
52  *
53  * If we are in a loading state, all errors are chained together by the same
54  * SPA-wide ENA (Error Numeric Association).
55  *
56  * For isolated I/O requests, we get the ENA from the zio_t. The propagation
57  * gets very complicated due to RAID-Z, gang blocks, and vdev caching.  We want
58  * to chain together all ereports associated with a logical piece of data.  For
59  * read I/Os, there  are basically three 'types' of I/O, which form a roughly
60  * layered diagram:
61  *
62  *      +---------------+
63  *      | Aggregate I/O |       No associated logical data or device
64  *      +---------------+
65  *              |
66  *              V
67  *      +---------------+       Reads associated with a piece of logical data.
68  *      |   Read I/O    |       This includes reads on behalf of RAID-Z,
69  *      +---------------+       mirrors, gang blocks, retries, etc.
70  *              |
71  *              V
72  *      +---------------+       Reads associated with a particular device, but
73  *      | Physical I/O  |       no logical data.  Issued as part of vdev caching
74  *      +---------------+       and I/O aggregation.
75  *
76  * Note that 'physical I/O' here is not the same terminology as used in the rest
77  * of ZIO.  Typically, 'physical I/O' simply means that there is no attached
78  * blockpointer.  But I/O with no associated block pointer can still be related
79  * to a logical piece of data (i.e. RAID-Z requests).
80  *
81  * Purely physical I/O always have unique ENAs.  They are not related to a
82  * particular piece of logical data, and therefore cannot be chained together.
83  * We still generate an ereport, but the DE doesn't correlate it with any
84  * logical piece of data.  When such an I/O fails, the delegated I/O requests
85  * will issue a retry, which will trigger the 'real' ereport with the correct
86  * ENA.
87  *
88  * We keep track of the ENA for a ZIO chain through the 'io_logical' member.
89  * When a new logical I/O is issued, we set this to point to itself.  Child I/Os
90  * then inherit this pointer, so that when it is first set subsequent failures
91  * will use the same ENA.  For vdev cache fill and queue aggregation I/O,
92  * this pointer is set to NULL, and no ereport will be generated (since it
93  * doesn't actually correspond to any particular device or piece of data,
94  * and the caller will always retry without caching or queueing anyway).
95  */
96 void
97 zfs_ereport_post(const char *subclass, spa_t *spa, vdev_t *vd, zio_t *zio,
98     uint64_t stateoroffset, uint64_t size)
99 {
100 #ifdef _KERNEL
101         char buf[1024];
102         struct sbuf sb;
103         struct timespec ts;
104
105         /*
106          * If we are doing a spa_tryimport(), ignore errors.
107          */
108         if (spa->spa_load_state == SPA_LOAD_TRYIMPORT)
109                 return;
110
111         /*
112          * If we are in the middle of opening a pool, and the previous attempt
113          * failed, don't bother logging any new ereports - we're just going to
114          * get the same diagnosis anyway.
115          */
116         if (spa->spa_load_state != SPA_LOAD_NONE &&
117             spa->spa_last_open_failed)
118                 return;
119
120         if (zio != NULL) {
121                 /*
122                  * If this is not a read or write zio, ignore the error.  This
123                  * can occur if the DKIOCFLUSHWRITECACHE ioctl fails.
124                  */
125                 if (zio->io_type != ZIO_TYPE_READ &&
126                     zio->io_type != ZIO_TYPE_WRITE)
127                         return;
128
129                 /*
130                  * Ignore any errors from speculative I/Os, as failure is an
131                  * expected result.
132                  */
133                 if (zio->io_flags & ZIO_FLAG_SPECULATIVE)
134                         return;
135
136                 /*
137                  * If this I/O is not a retry I/O, don't post an ereport.
138                  * Otherwise, we risk making bad diagnoses based on B_FAILFAST
139                  * I/Os.
140                  */
141                 if (zio->io_error == EIO &&
142                     !(zio->io_flags & ZIO_FLAG_IO_RETRY))
143                         return;
144
145                 if (vd != NULL) {
146                         /*
147                          * If the vdev has already been marked as failing due
148                          * to a failed probe, then ignore any subsequent I/O
149                          * errors, as the DE will automatically fault the vdev
150                          * on the first such failure.  This also catches cases
151                          * where vdev_remove_wanted is set and the device has
152                          * not yet been asynchronously placed into the REMOVED
153                          * state.
154                          */
155                         if (zio->io_vd == vd &&
156                             !vdev_accessible(vd, zio) &&
157                             strcmp(subclass, FM_EREPORT_ZFS_PROBE_FAILURE) != 0)
158                                 return;
159
160                         /*
161                          * Ignore checksum errors for reads from DTL regions of
162                          * leaf vdevs.
163                          */
164                         if (zio->io_type == ZIO_TYPE_READ &&
165                             zio->io_error == ECKSUM &&
166                             vd->vdev_ops->vdev_op_leaf &&
167                             vdev_dtl_contains(vd, DTL_MISSING, zio->io_txg, 1))
168                                 return;
169                 }
170         }
171         nanotime(&ts);
172
173         sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
174         sbuf_printf(&sb, "time=%ju.%ld", (uintmax_t)ts.tv_sec, ts.tv_nsec);
175
176         /*
177          * Serialize ereport generation
178          */
179         mutex_enter(&spa->spa_errlist_lock);
180
181 #if 0
182         /*
183          * Determine the ENA to use for this event.  If we are in a loading
184          * state, use a SPA-wide ENA.  Otherwise, if we are in an I/O state, use
185          * a root zio-wide ENA.  Otherwise, simply use a unique ENA.
186          */
187         if (spa->spa_load_state != SPA_LOAD_NONE) {
188 #if 0
189                 if (spa->spa_ena == 0)
190                         spa->spa_ena = fm_ena_generate(0, FM_ENA_FMT1);
191 #endif
192                 ena = spa->spa_ena;
193         } else if (zio != NULL && zio->io_logical != NULL) {
194 #if 0
195                 if (zio->io_logical->io_ena == 0)
196                         zio->io_logical->io_ena =
197                             fm_ena_generate(0, FM_ENA_FMT1);
198 #endif
199                 ena = zio->io_logical->io_ena;
200         } else {
201 #if 0
202                 ena = fm_ena_generate(0, FM_ENA_FMT1);
203 #else
204                 ena = 0;
205 #endif
206         }
207 #endif
208
209         /*
210          * Construct the full class, detector, and other standard FMA fields.
211          */
212         sbuf_printf(&sb, " ereport_version=%u", FM_EREPORT_VERSION);
213         sbuf_printf(&sb, " class=%s.%s", ZFS_ERROR_CLASS, subclass);
214
215         sbuf_printf(&sb, " zfs_scheme_version=%u", FM_ZFS_SCHEME_VERSION);
216
217         /*
218          * Construct the per-ereport payload, depending on which parameters are
219          * passed in.
220          */
221
222         /*
223          * Generic payload members common to all ereports.
224          */
225         sbuf_printf(&sb, " %s=%s", FM_EREPORT_PAYLOAD_ZFS_POOL, spa_name(spa));
226         sbuf_printf(&sb, " %s=%ju", FM_EREPORT_PAYLOAD_ZFS_POOL_GUID,
227             spa_guid(spa));
228         sbuf_printf(&sb, " %s=%d", FM_EREPORT_PAYLOAD_ZFS_POOL_CONTEXT,
229             spa->spa_load_state);
230
231         if (spa != NULL) {
232                 sbuf_printf(&sb, " %s=%s", FM_EREPORT_PAYLOAD_ZFS_POOL_FAILMODE,
233                     spa_get_failmode(spa) == ZIO_FAILURE_MODE_WAIT ?
234                     FM_EREPORT_FAILMODE_WAIT :
235                     spa_get_failmode(spa) == ZIO_FAILURE_MODE_CONTINUE ?
236                     FM_EREPORT_FAILMODE_CONTINUE : FM_EREPORT_FAILMODE_PANIC);
237         }
238
239         if (vd != NULL) {
240                 vdev_t *pvd = vd->vdev_parent;
241
242                 sbuf_printf(&sb, " %s=%ju", FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID,
243                     vd->vdev_guid);
244                 sbuf_printf(&sb, " %s=%s", FM_EREPORT_PAYLOAD_ZFS_VDEV_TYPE,
245                     vd->vdev_ops->vdev_op_type);
246                 if (vd->vdev_path != NULL)
247                         sbuf_printf(&sb, " %s=%s",
248                             FM_EREPORT_PAYLOAD_ZFS_VDEV_PATH, vd->vdev_path);
249                 if (vd->vdev_devid != NULL)
250                         sbuf_printf(&sb, " %s=%s",
251                             FM_EREPORT_PAYLOAD_ZFS_VDEV_DEVID, vd->vdev_devid);
252                 if (vd->vdev_fru != NULL)
253                         sbuf_printf(&sb, " %s=%s",
254                             FM_EREPORT_PAYLOAD_ZFS_VDEV_FRU, vd->vdev_fru);
255
256                 if (pvd != NULL) {
257                         sbuf_printf(&sb, " %s=%ju",
258                             FM_EREPORT_PAYLOAD_ZFS_PARENT_GUID, pvd->vdev_guid);
259                         sbuf_printf(&sb, " %s=%s",
260                             FM_EREPORT_PAYLOAD_ZFS_PARENT_TYPE,
261                             pvd->vdev_ops->vdev_op_type);
262                         if (pvd->vdev_path)
263                                 sbuf_printf(&sb, " %s=%s",
264                                     FM_EREPORT_PAYLOAD_ZFS_PARENT_PATH,
265                                     pvd->vdev_path);
266                         if (pvd->vdev_devid)
267                                 sbuf_printf(&sb, " %s=%s",
268                                     FM_EREPORT_PAYLOAD_ZFS_PARENT_DEVID,
269                                     pvd->vdev_devid);
270                 }
271         }
272
273         if (zio != NULL) {
274                 /*
275                  * Payload common to all I/Os.
276                  */
277                 sbuf_printf(&sb, " %s=%u", FM_EREPORT_PAYLOAD_ZFS_ZIO_ERR,
278                     zio->io_error);
279
280                 /*
281                  * If the 'size' parameter is non-zero, it indicates this is a
282                  * RAID-Z or other I/O where the physical offset and length are
283                  * provided for us, instead of within the zio_t.
284                  */
285                 if (vd != NULL) {
286                         if (size) {
287                                 sbuf_printf(&sb, " %s=%ju",
288                                     FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET,
289                                     stateoroffset);
290                                 sbuf_printf(&sb, " %s=%ju",
291                                     FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE, size);
292                         } else {
293                                 sbuf_printf(&sb, " %s=%ju",
294                                     FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET,
295                                     zio->io_offset);
296                                 sbuf_printf(&sb, " %s=%ju",
297                                     FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE,
298                                     zio->io_size);
299                         }
300                 }
301
302                 /*
303                  * Payload for I/Os with corresponding logical information.
304                  */
305                 if (zio->io_logical != NULL) {
306                         sbuf_printf(&sb, " %s=%ju",
307                             FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJECT,
308                             zio->io_logical->io_bookmark.zb_object);
309                         sbuf_printf(&sb, " %s=%ju",
310                             FM_EREPORT_PAYLOAD_ZFS_ZIO_LEVEL,
311                             zio->io_logical->io_bookmark.zb_level);
312                         sbuf_printf(&sb, " %s=%ju",
313                             FM_EREPORT_PAYLOAD_ZFS_ZIO_BLKID,
314                             zio->io_logical->io_bookmark.zb_blkid);
315                 }
316         } else if (vd != NULL) {
317                 /*
318                  * If we have a vdev but no zio, this is a device fault, and the
319                  * 'stateoroffset' parameter indicates the previous state of the
320                  * vdev.
321                  */
322                 sbuf_printf(&sb, " %s=%ju", FM_EREPORT_PAYLOAD_ZFS_PREV_STATE,
323                     stateoroffset);
324         }
325         mutex_exit(&spa->spa_errlist_lock);
326
327         sbuf_finish(&sb);
328         devctl_notify("ZFS", spa->spa_name, subclass, sbuf_data(&sb));
329         if (sbuf_overflowed(&sb))
330                 printf("ZFS WARNING: sbuf overflowed\n");
331         sbuf_delete(&sb);
332 #endif
333 }
334
335 static void
336 zfs_post_common(spa_t *spa, vdev_t *vd, const char *name)
337 {
338 #ifdef _KERNEL
339         char buf[1024];
340         char class[64];
341         struct sbuf sb;
342         struct timespec ts;
343
344         nanotime(&ts);
345
346         sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
347         sbuf_printf(&sb, "time=%ju.%ld", (uintmax_t)ts.tv_sec, ts.tv_nsec);
348
349         snprintf(class, sizeof(class), "%s.%s.%s", FM_RSRC_RESOURCE,
350             ZFS_ERROR_CLASS, name);
351         sbuf_printf(&sb, " %s=%hhu", FM_VERSION, FM_RSRC_VERSION);
352         sbuf_printf(&sb, " %s=%s", FM_CLASS, class);
353         sbuf_printf(&sb, " %s=%ju", FM_EREPORT_PAYLOAD_ZFS_POOL_GUID,
354             spa_guid(spa));
355         if (vd)
356                 sbuf_printf(&sb, " %s=%ju", FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID,
357                     vd->vdev_guid);
358         sbuf_finish(&sb);
359         ZFS_LOG(1, "%s", sbuf_data(&sb));
360         devctl_notify("ZFS", spa->spa_name, class, sbuf_data(&sb));
361         if (sbuf_overflowed(&sb))
362                 printf("ZFS WARNING: sbuf overflowed\n");
363         sbuf_delete(&sb);
364 #endif
365 }
366
367 /*
368  * The 'resource.fs.zfs.removed' event is an internal signal that the given vdev
369  * has been removed from the system.  This will cause the DE to ignore any
370  * recent I/O errors, inferring that they are due to the asynchronous device
371  * removal.
372  */
373 void
374 zfs_post_remove(spa_t *spa, vdev_t *vd)
375 {
376         zfs_post_common(spa, vd, FM_RESOURCE_REMOVED);
377 }
378
379 /*
380  * The 'resource.fs.zfs.autoreplace' event is an internal signal that the pool
381  * has the 'autoreplace' property set, and therefore any broken vdevs will be
382  * handled by higher level logic, and no vdev fault should be generated.
383  */
384 void
385 zfs_post_autoreplace(spa_t *spa, vdev_t *vd)
386 {
387         zfs_post_common(spa, vd, FM_RESOURCE_AUTOREPLACE);
388 }