]> git.proxmox.com Git - mirror_zfs-debian.git/blame - cmd/zed/agents/zfs_diagnosis.c
New upstream version 0.7.2
[mirror_zfs-debian.git] / cmd / zed / agents / zfs_diagnosis.c
CommitLineData
cae5b340
AX
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/*
23 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2016, Intel Corporation.
26 */
27
28#include <stddef.h>
29#include <strings.h>
30#include <libuutil.h>
31#include <libzfs.h>
32#include <sys/types.h>
33#include <sys/time.h>
34#include <sys/fs/zfs.h>
35#include <sys/fm/protocol.h>
36#include <sys/fm/fs/zfs.h>
37
38#include "zfs_agents.h"
39#include "fmd_api.h"
40
41/*
42 * Our serd engines are named 'zfs_<pool_guid>_<vdev_guid>_{checksum,io}'. This
43 * #define reserves enough space for two 64-bit hex values plus the length of
44 * the longest string.
45 */
46#define MAX_SERDLEN (16 * 2 + sizeof ("zfs___checksum"))
47
48/*
49 * On-disk case structure. This must maintain backwards compatibility with
50 * previous versions of the DE. By default, any members appended to the end
51 * will be filled with zeros if they don't exist in a previous version.
52 */
53typedef struct zfs_case_data {
54 uint64_t zc_version;
55 uint64_t zc_ena;
56 uint64_t zc_pool_guid;
57 uint64_t zc_vdev_guid;
58 int zc_pool_state;
59 char zc_serd_checksum[MAX_SERDLEN];
60 char zc_serd_io[MAX_SERDLEN];
61 int zc_has_remove_timer;
62} zfs_case_data_t;
63
64/*
65 * Time-of-day
66 */
67typedef struct er_timeval {
68 uint64_t ertv_sec;
69 uint64_t ertv_nsec;
70} er_timeval_t;
71
72/*
73 * In-core case structure.
74 */
75typedef struct zfs_case {
76 boolean_t zc_present;
77 uint32_t zc_version;
78 zfs_case_data_t zc_data;
79 fmd_case_t *zc_case;
80 uu_list_node_t zc_node;
81 id_t zc_remove_timer;
82 char *zc_fru;
83 er_timeval_t zc_when;
84} zfs_case_t;
85
86#define CASE_DATA "data"
87#define CASE_FRU "fru"
88#define CASE_DATA_VERSION_INITIAL 1
89#define CASE_DATA_VERSION_SERD 2
90
91typedef struct zfs_de_stats {
92 fmd_stat_t old_drops;
93 fmd_stat_t dev_drops;
94 fmd_stat_t vdev_drops;
95 fmd_stat_t import_drops;
96 fmd_stat_t resource_drops;
97} zfs_de_stats_t;
98
99zfs_de_stats_t zfs_stats = {
100 { "old_drops", FMD_TYPE_UINT64, "ereports dropped (from before load)" },
101 { "dev_drops", FMD_TYPE_UINT64, "ereports dropped (dev during open)"},
102 { "vdev_drops", FMD_TYPE_UINT64, "ereports dropped (weird vdev types)"},
103 { "import_drops", FMD_TYPE_UINT64, "ereports dropped (during import)" },
104 { "resource_drops", FMD_TYPE_UINT64, "resource related ereports" }
105};
106
107static hrtime_t zfs_remove_timeout;
108
109uu_list_pool_t *zfs_case_pool;
110uu_list_t *zfs_cases;
111
112#define ZFS_MAKE_RSRC(type) \
113 FM_RSRC_CLASS "." ZFS_ERROR_CLASS "." type
114#define ZFS_MAKE_EREPORT(type) \
115 FM_EREPORT_CLASS "." ZFS_ERROR_CLASS "." type
116
117/*
118 * Write out the persistent representation of an active case.
119 */
120static void
121zfs_case_serialize(fmd_hdl_t *hdl, zfs_case_t *zcp)
122{
123 zcp->zc_data.zc_version = CASE_DATA_VERSION_SERD;
124}
125
126/*
127 * Read back the persistent representation of an active case.
128 */
129static zfs_case_t *
130zfs_case_unserialize(fmd_hdl_t *hdl, fmd_case_t *cp)
131{
132 zfs_case_t *zcp;
133
134 zcp = fmd_hdl_zalloc(hdl, sizeof (zfs_case_t), FMD_SLEEP);
135 zcp->zc_case = cp;
136
137 fmd_buf_read(hdl, cp, CASE_DATA, &zcp->zc_data,
138 sizeof (zcp->zc_data));
139
140 if (zcp->zc_data.zc_version > CASE_DATA_VERSION_SERD) {
141 fmd_hdl_free(hdl, zcp, sizeof (zfs_case_t));
142 return (NULL);
143 }
144
145 /*
146 * fmd_buf_read() will have already zeroed out the remainder of the
147 * buffer, so we don't have to do anything special if the version
148 * doesn't include the SERD engine name.
149 */
150
151 if (zcp->zc_data.zc_has_remove_timer)
152 zcp->zc_remove_timer = fmd_timer_install(hdl, zcp,
153 NULL, zfs_remove_timeout);
154
155 uu_list_node_init(zcp, &zcp->zc_node, zfs_case_pool);
156 (void) uu_list_insert_before(zfs_cases, NULL, zcp);
157
158 fmd_case_setspecific(hdl, cp, zcp);
159
160 return (zcp);
161}
162
163/*
164 * Iterate over any active cases. If any cases are associated with a pool or
165 * vdev which is no longer present on the system, close the associated case.
166 */
167static void
168zfs_mark_vdev(uint64_t pool_guid, nvlist_t *vd, er_timeval_t *loaded)
169{
170 uint64_t vdev_guid;
171 uint_t c, children;
172 nvlist_t **child;
173 zfs_case_t *zcp;
174 int ret;
175
176 ret = nvlist_lookup_uint64(vd, ZPOOL_CONFIG_GUID, &vdev_guid);
177 assert(ret == 0);
178
179 /*
180 * Mark any cases associated with this (pool, vdev) pair.
181 */
182 for (zcp = uu_list_first(zfs_cases); zcp != NULL;
183 zcp = uu_list_next(zfs_cases, zcp)) {
184 if (zcp->zc_data.zc_pool_guid == pool_guid &&
185 zcp->zc_data.zc_vdev_guid == vdev_guid) {
186 zcp->zc_present = B_TRUE;
187 zcp->zc_when = *loaded;
188 }
189 }
190
191 /*
192 * Iterate over all children.
193 */
194 if (nvlist_lookup_nvlist_array(vd, ZPOOL_CONFIG_CHILDREN, &child,
195 &children) == 0) {
196 for (c = 0; c < children; c++)
197 zfs_mark_vdev(pool_guid, child[c], loaded);
198 }
199
200 if (nvlist_lookup_nvlist_array(vd, ZPOOL_CONFIG_L2CACHE, &child,
201 &children) == 0) {
202 for (c = 0; c < children; c++)
203 zfs_mark_vdev(pool_guid, child[c], loaded);
204 }
205
206 if (nvlist_lookup_nvlist_array(vd, ZPOOL_CONFIG_SPARES, &child,
207 &children) == 0) {
208 for (c = 0; c < children; c++)
209 zfs_mark_vdev(pool_guid, child[c], loaded);
210 }
211}
212
213/*ARGSUSED*/
214static int
215zfs_mark_pool(zpool_handle_t *zhp, void *unused)
216{
217 zfs_case_t *zcp;
218 uint64_t pool_guid;
219 uint64_t *tod;
220 er_timeval_t loaded = { 0 };
221 nvlist_t *config, *vd;
222 uint_t nelem = 0;
223 int ret;
224
225 pool_guid = zpool_get_prop_int(zhp, ZPOOL_PROP_GUID, NULL);
226 /*
227 * Mark any cases associated with just this pool.
228 */
229 for (zcp = uu_list_first(zfs_cases); zcp != NULL;
230 zcp = uu_list_next(zfs_cases, zcp)) {
231 if (zcp->zc_data.zc_pool_guid == pool_guid &&
232 zcp->zc_data.zc_vdev_guid == 0)
233 zcp->zc_present = B_TRUE;
234 }
235
236 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
237 zpool_close(zhp);
238 return (-1);
239 }
240
241 (void) nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_LOADED_TIME,
242 &tod, &nelem);
243 if (nelem == 2) {
244 loaded.ertv_sec = tod[0];
245 loaded.ertv_nsec = tod[1];
246 for (zcp = uu_list_first(zfs_cases); zcp != NULL;
247 zcp = uu_list_next(zfs_cases, zcp)) {
248 if (zcp->zc_data.zc_pool_guid == pool_guid &&
249 zcp->zc_data.zc_vdev_guid == 0) {
250 zcp->zc_when = loaded;
251 }
252 }
253 }
254
255 ret = nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &vd);
256 assert(ret == 0);
257
258 zfs_mark_vdev(pool_guid, vd, &loaded);
259
260 zpool_close(zhp);
261
262 return (0);
263}
264
265struct load_time_arg {
266 uint64_t lt_guid;
267 er_timeval_t *lt_time;
268 boolean_t lt_found;
269};
270
271static int
272zpool_find_load_time(zpool_handle_t *zhp, void *arg)
273{
274 struct load_time_arg *lta = arg;
275 uint64_t pool_guid;
276 uint64_t *tod;
277 nvlist_t *config;
278 uint_t nelem;
279
280 if (lta->lt_found) {
281 zpool_close(zhp);
282 return (0);
283 }
284
285 pool_guid = zpool_get_prop_int(zhp, ZPOOL_PROP_GUID, NULL);
286 if (pool_guid != lta->lt_guid) {
287 zpool_close(zhp);
288 return (0);
289 }
290
291 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
292 zpool_close(zhp);
293 return (-1);
294 }
295
296 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_LOADED_TIME,
297 &tod, &nelem) == 0 && nelem == 2) {
298 lta->lt_found = B_TRUE;
299 lta->lt_time->ertv_sec = tod[0];
300 lta->lt_time->ertv_nsec = tod[1];
301 }
302
303 zpool_close(zhp);
304
305 return (0);
306}
307
308static void
309zfs_purge_cases(fmd_hdl_t *hdl)
310{
311 zfs_case_t *zcp;
312 uu_list_walk_t *walk;
313 libzfs_handle_t *zhdl = fmd_hdl_getspecific(hdl);
314
315 /*
316 * There is no way to open a pool by GUID, or lookup a vdev by GUID. No
317 * matter what we do, we're going to have to stomach an O(vdevs * cases)
318 * algorithm. In reality, both quantities are likely so small that
319 * neither will matter. Given that iterating over pools is more
320 * expensive than iterating over the in-memory case list, we opt for a
321 * 'present' flag in each case that starts off cleared. We then iterate
322 * over all pools, marking those that are still present, and removing
323 * those that aren't found.
324 *
325 * Note that we could also construct an FMRI and rely on
326 * fmd_nvl_fmri_present(), but this would end up doing the same search.
327 */
328
329 /*
330 * Mark the cases as not present.
331 */
332 for (zcp = uu_list_first(zfs_cases); zcp != NULL;
333 zcp = uu_list_next(zfs_cases, zcp))
334 zcp->zc_present = B_FALSE;
335
336 /*
337 * Iterate over all pools and mark the pools and vdevs found. If this
338 * fails (most probably because we're out of memory), then don't close
339 * any of the cases and we cannot be sure they are accurate.
340 */
341 if (zpool_iter(zhdl, zfs_mark_pool, NULL) != 0)
342 return;
343
344 /*
345 * Remove those cases which were not found.
346 */
347 walk = uu_list_walk_start(zfs_cases, UU_WALK_ROBUST);
348 while ((zcp = uu_list_walk_next(walk)) != NULL) {
349 if (!zcp->zc_present)
350 fmd_case_close(hdl, zcp->zc_case);
351 }
352 uu_list_walk_end(walk);
353}
354
355/*
356 * Construct the name of a serd engine given the pool/vdev GUID and type (io or
357 * checksum).
358 */
359static void
360zfs_serd_name(char *buf, uint64_t pool_guid, uint64_t vdev_guid,
361 const char *type)
362{
363 (void) snprintf(buf, MAX_SERDLEN, "zfs_%llx_%llx_%s",
364 (long long unsigned int)pool_guid,
365 (long long unsigned int)vdev_guid, type);
366}
367
368/*
369 * Solve a given ZFS case. This first checks to make sure the diagnosis is
370 * still valid, as well as cleaning up any pending timer associated with the
371 * case.
372 */
373static void
374zfs_case_solve(fmd_hdl_t *hdl, zfs_case_t *zcp, const char *faultname,
375 boolean_t checkunusable)
376{
377 nvlist_t *detector, *fault;
378 boolean_t serialize;
379 nvlist_t *fru = NULL;
380#ifdef HAVE_LIBTOPO
381 nvlist_t *fmri;
382 topo_hdl_t *thp;
383 int err;
384#endif
385 fmd_hdl_debug(hdl, "solving fault '%s'", faultname);
386
387 /*
388 * Construct the detector from the case data. The detector is in the
389 * ZFS scheme, and is either the pool or the vdev, depending on whether
390 * this is a vdev or pool fault.
391 */
392 detector = fmd_nvl_alloc(hdl, FMD_SLEEP);
393
394 (void) nvlist_add_uint8(detector, FM_VERSION, ZFS_SCHEME_VERSION0);
395 (void) nvlist_add_string(detector, FM_FMRI_SCHEME, FM_FMRI_SCHEME_ZFS);
396 (void) nvlist_add_uint64(detector, FM_FMRI_ZFS_POOL,
397 zcp->zc_data.zc_pool_guid);
398 if (zcp->zc_data.zc_vdev_guid != 0) {
399 (void) nvlist_add_uint64(detector, FM_FMRI_ZFS_VDEV,
400 zcp->zc_data.zc_vdev_guid);
401 }
402
403#ifdef HAVE_LIBTOPO
404 /*
405 * We also want to make sure that the detector (pool or vdev) properly
406 * reflects the diagnosed state, when the fault corresponds to internal
407 * ZFS state (i.e. not checksum or I/O error-induced). Otherwise, a
408 * device which was unavailable early in boot (because the driver/file
409 * wasn't available) and is now healthy will be mis-diagnosed.
410 */
411 if (!fmd_nvl_fmri_present(hdl, detector) ||
412 (checkunusable && !fmd_nvl_fmri_unusable(hdl, detector))) {
413 fmd_case_close(hdl, zcp->zc_case);
414 nvlist_free(detector);
415 return;
416 }
417
418
419 fru = NULL;
420 if (zcp->zc_fru != NULL &&
421 (thp = fmd_hdl_topo_hold(hdl, TOPO_VERSION)) != NULL) {
422 /*
423 * If the vdev had an associated FRU, then get the FRU nvlist
424 * from the topo handle and use that in the suspect list. We
425 * explicitly lookup the FRU because the fmri reported from the
426 * kernel may not have up to date details about the disk itself
427 * (serial, part, etc).
428 */
429 if (topo_fmri_str2nvl(thp, zcp->zc_fru, &fmri, &err) == 0) {
430 libzfs_handle_t *zhdl = fmd_hdl_getspecific(hdl);
431
432 /*
433 * If the disk is part of the system chassis, but the
434 * FRU indicates a different chassis ID than our
435 * current system, then ignore the error. This
436 * indicates that the device was part of another
437 * cluster head, and for obvious reasons cannot be
438 * imported on this system.
439 */
440 if (libzfs_fru_notself(zhdl, zcp->zc_fru)) {
441 fmd_case_close(hdl, zcp->zc_case);
442 nvlist_free(fmri);
443 fmd_hdl_topo_rele(hdl, thp);
444 nvlist_free(detector);
445 return;
446 }
447
448 /*
449 * If the device is no longer present on the system, or
450 * topo_fmri_fru() fails for other reasons, then fall
451 * back to the fmri specified in the vdev.
452 */
453 if (topo_fmri_fru(thp, fmri, &fru, &err) != 0)
454 fru = fmd_nvl_dup(hdl, fmri, FMD_SLEEP);
455 nvlist_free(fmri);
456 }
457
458 fmd_hdl_topo_rele(hdl, thp);
459 }
460#endif
461 fault = fmd_nvl_create_fault(hdl, faultname, 100, detector,
462 fru, detector);
463 fmd_case_add_suspect(hdl, zcp->zc_case, fault);
464
465 nvlist_free(fru);
466
467 fmd_case_solve(hdl, zcp->zc_case);
468
469 serialize = B_FALSE;
470 if (zcp->zc_data.zc_has_remove_timer) {
471 fmd_timer_remove(hdl, zcp->zc_remove_timer);
472 zcp->zc_data.zc_has_remove_timer = 0;
473 serialize = B_TRUE;
474 }
475 if (serialize)
476 zfs_case_serialize(hdl, zcp);
477
478 nvlist_free(detector);
479}
480
481static boolean_t
482timeval_earlier(er_timeval_t *a, er_timeval_t *b)
483{
484 return (a->ertv_sec < b->ertv_sec ||
485 (a->ertv_sec == b->ertv_sec && a->ertv_nsec < b->ertv_nsec));
486}
487
488/*ARGSUSED*/
489static void
490zfs_ereport_when(fmd_hdl_t *hdl, nvlist_t *nvl, er_timeval_t *when)
491{
492 int64_t *tod;
493 uint_t nelem;
494
495 if (nvlist_lookup_int64_array(nvl, FM_EREPORT_TIME, &tod,
496 &nelem) == 0 && nelem == 2) {
497 when->ertv_sec = tod[0];
498 when->ertv_nsec = tod[1];
499 } else {
500 when->ertv_sec = when->ertv_nsec = UINT64_MAX;
501 }
502}
503
504/*
505 * Main fmd entry point.
506 */
507/*ARGSUSED*/
508static void
509zfs_fm_recv(fmd_hdl_t *hdl, fmd_event_t *ep, nvlist_t *nvl, const char *class)
510{
511 zfs_case_t *zcp, *dcp;
512 int32_t pool_state;
513 uint64_t ena, pool_guid, vdev_guid;
514 er_timeval_t pool_load;
515 er_timeval_t er_when;
516 nvlist_t *detector;
517 boolean_t pool_found = B_FALSE;
518 boolean_t isresource;
519 char *type;
520
521 /*
522 * We subscribe to notifications for vdev or pool removal. In these
523 * cases, there may be cases that no longer apply. Purge any cases
524 * that no longer apply.
525 */
526 if (fmd_nvl_class_match(hdl, nvl, "sysevent.fs.zfs.*")) {
527 fmd_hdl_debug(hdl, "purging orphaned cases from %s",
528 strrchr(class, '.') + 1);
529 zfs_purge_cases(hdl);
530 zfs_stats.resource_drops.fmds_value.ui64++;
531 return;
532 }
533
534 isresource = fmd_nvl_class_match(hdl, nvl, "resource.fs.zfs.*");
535
536 if (isresource) {
537 /*
538 * For resources, we don't have a normal payload.
539 */
540 if (nvlist_lookup_uint64(nvl, FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID,
541 &vdev_guid) != 0)
542 pool_state = SPA_LOAD_OPEN;
543 else
544 pool_state = SPA_LOAD_NONE;
545 detector = NULL;
546 } else {
547 (void) nvlist_lookup_nvlist(nvl,
548 FM_EREPORT_DETECTOR, &detector);
549 (void) nvlist_lookup_int32(nvl,
550 FM_EREPORT_PAYLOAD_ZFS_POOL_CONTEXT, &pool_state);
551 }
552
553 /*
554 * We also ignore all ereports generated during an import of a pool,
555 * since the only possible fault (.pool) would result in import failure,
556 * and hence no persistent fault. Some day we may want to do something
557 * with these ereports, so we continue generating them internally.
558 */
559 if (pool_state == SPA_LOAD_IMPORT) {
560 zfs_stats.import_drops.fmds_value.ui64++;
561 fmd_hdl_debug(hdl, "ignoring '%s' during import", class);
562 return;
563 }
564
565 /*
566 * Device I/O errors are ignored during pool open.
567 */
568 if (pool_state == SPA_LOAD_OPEN &&
569 (fmd_nvl_class_match(hdl, nvl,
570 ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_CHECKSUM)) ||
571 fmd_nvl_class_match(hdl, nvl,
572 ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_IO)) ||
573 fmd_nvl_class_match(hdl, nvl,
574 ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_PROBE_FAILURE)))) {
575 fmd_hdl_debug(hdl, "ignoring '%s' during pool open", class);
576 zfs_stats.dev_drops.fmds_value.ui64++;
577 return;
578 }
579
580 /*
581 * We ignore ereports for anything except disks and files.
582 */
583 if (nvlist_lookup_string(nvl, FM_EREPORT_PAYLOAD_ZFS_VDEV_TYPE,
584 &type) == 0) {
585 if (strcmp(type, VDEV_TYPE_DISK) != 0 &&
586 strcmp(type, VDEV_TYPE_FILE) != 0) {
587 zfs_stats.vdev_drops.fmds_value.ui64++;
588 return;
589 }
590 }
591
592 /*
593 * Determine if this ereport corresponds to an open case.
594 * Each vdev or pool can have a single case.
595 */
596 (void) nvlist_lookup_uint64(nvl,
597 FM_EREPORT_PAYLOAD_ZFS_POOL_GUID, &pool_guid);
598 if (nvlist_lookup_uint64(nvl,
599 FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID, &vdev_guid) != 0)
600 vdev_guid = 0;
601 if (nvlist_lookup_uint64(nvl, FM_EREPORT_ENA, &ena) != 0)
602 ena = 0;
603
604 zfs_ereport_when(hdl, nvl, &er_when);
605
606 for (zcp = uu_list_first(zfs_cases); zcp != NULL;
607 zcp = uu_list_next(zfs_cases, zcp)) {
608 if (zcp->zc_data.zc_pool_guid == pool_guid) {
609 pool_found = B_TRUE;
610 pool_load = zcp->zc_when;
611 }
612 if (zcp->zc_data.zc_vdev_guid == vdev_guid)
613 break;
614 }
615
616 /*
617 * Avoid falsely accusing a pool of being faulty. Do so by
618 * not replaying ereports that were generated prior to the
619 * current import. If the failure that generated them was
620 * transient because the device was actually removed but we
621 * didn't receive the normal asynchronous notification, we
622 * don't want to mark it as faulted and potentially panic. If
623 * there is still a problem we'd expect not to be able to
624 * import the pool, or that new ereports will be generated
625 * once the pool is used.
626 */
627 if (pool_found && timeval_earlier(&er_when, &pool_load)) {
628 fmd_hdl_debug(hdl, "ignoring pool %llx, "
629 "ereport time %lld.%lld, pool load time = %lld.%lld",
630 pool_guid, er_when.ertv_sec, er_when.ertv_nsec,
631 pool_load.ertv_sec, pool_load.ertv_nsec);
632 zfs_stats.old_drops.fmds_value.ui64++;
633 return;
634 }
635
636 if (!pool_found) {
637 /*
638 * Haven't yet seen this pool, but same situation
639 * may apply.
640 */
641 libzfs_handle_t *zhdl = fmd_hdl_getspecific(hdl);
642 struct load_time_arg la;
643
644 la.lt_guid = pool_guid;
645 la.lt_time = &pool_load;
646 la.lt_found = B_FALSE;
647
648 if (zhdl != NULL &&
649 zpool_iter(zhdl, zpool_find_load_time, &la) == 0 &&
650 la.lt_found == B_TRUE) {
651 pool_found = B_TRUE;
652
653 if (timeval_earlier(&er_when, &pool_load)) {
654 fmd_hdl_debug(hdl, "ignoring pool %llx, "
655 "ereport time %lld.%lld, "
656 "pool load time = %lld.%lld",
657 pool_guid, er_when.ertv_sec,
658 er_when.ertv_nsec, pool_load.ertv_sec,
659 pool_load.ertv_nsec);
660 zfs_stats.old_drops.fmds_value.ui64++;
661 return;
662 }
663 }
664 }
665
666 if (zcp == NULL) {
667 fmd_case_t *cs;
668 zfs_case_data_t data = { 0 };
669
670 /*
671 * If this is one of our 'fake' resource ereports, and there is
672 * no case open, simply discard it.
673 */
674 if (isresource) {
675 zfs_stats.resource_drops.fmds_value.ui64++;
676 fmd_hdl_debug(hdl, "discarding '%s for vdev %llu",
677 class, vdev_guid);
678 return;
679 }
680
681 /*
682 * Skip tracking some ereports
683 */
684 if (strcmp(class,
685 ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_DATA)) == 0 ||
686 strcmp(class,
687 ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_CONFIG_CACHE_WRITE)) == 0 ||
688 strcmp(class,
689 ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_DELAY)) == 0) {
690 zfs_stats.resource_drops.fmds_value.ui64++;
691 return;
692 }
693
694 /*
695 * Open a new case.
696 */
697 cs = fmd_case_open(hdl, NULL);
698
699 fmd_hdl_debug(hdl, "opening case for vdev %llu due to '%s'",
700 vdev_guid, class);
701
702 /*
703 * Initialize the case buffer. To commonize code, we actually
704 * create the buffer with existing data, and then call
705 * zfs_case_unserialize() to instantiate the in-core structure.
706 */
707 fmd_buf_create(hdl, cs, CASE_DATA, sizeof (zfs_case_data_t));
708
709 data.zc_version = CASE_DATA_VERSION_SERD;
710 data.zc_ena = ena;
711 data.zc_pool_guid = pool_guid;
712 data.zc_vdev_guid = vdev_guid;
713 data.zc_pool_state = (int)pool_state;
714
715 fmd_buf_write(hdl, cs, CASE_DATA, &data, sizeof (data));
716
717 zcp = zfs_case_unserialize(hdl, cs);
718 assert(zcp != NULL);
719 if (pool_found)
720 zcp->zc_when = pool_load;
721 }
722
723 if (isresource) {
724 fmd_hdl_debug(hdl, "resource event '%s'", class);
725
726 if (fmd_nvl_class_match(hdl, nvl,
727 ZFS_MAKE_RSRC(FM_RESOURCE_AUTOREPLACE))) {
728 /*
729 * The 'resource.fs.zfs.autoreplace' event indicates
730 * that the pool was loaded with the 'autoreplace'
731 * property set. In this case, any pending device
732 * failures should be ignored, as the asynchronous
733 * autoreplace handling will take care of them.
734 */
735 fmd_case_close(hdl, zcp->zc_case);
736 } else if (fmd_nvl_class_match(hdl, nvl,
737 ZFS_MAKE_RSRC(FM_RESOURCE_REMOVED))) {
738 /*
739 * The 'resource.fs.zfs.removed' event indicates that
740 * device removal was detected, and the device was
741 * closed asynchronously. If this is the case, we
742 * assume that any recent I/O errors were due to the
743 * device removal, not any fault of the device itself.
744 * We reset the SERD engine, and cancel any pending
745 * timers.
746 */
747 if (zcp->zc_data.zc_has_remove_timer) {
748 fmd_timer_remove(hdl, zcp->zc_remove_timer);
749 zcp->zc_data.zc_has_remove_timer = 0;
750 zfs_case_serialize(hdl, zcp);
751 }
752 if (zcp->zc_data.zc_serd_io[0] != '\0')
753 fmd_serd_reset(hdl, zcp->zc_data.zc_serd_io);
754 if (zcp->zc_data.zc_serd_checksum[0] != '\0')
755 fmd_serd_reset(hdl,
756 zcp->zc_data.zc_serd_checksum);
757 } else if (fmd_nvl_class_match(hdl, nvl,
758 ZFS_MAKE_RSRC(FM_RESOURCE_STATECHANGE))) {
759 uint64_t state = 0;
760
761 if (zcp != NULL &&
762 nvlist_lookup_uint64(nvl,
763 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE, &state) == 0 &&
764 state == VDEV_STATE_HEALTHY) {
765 fmd_hdl_debug(hdl, "closing case after a "
766 "device statechange to healthy");
767 fmd_case_close(hdl, zcp->zc_case);
768 }
769 }
770 zfs_stats.resource_drops.fmds_value.ui64++;
771 return;
772 }
773
774 /*
775 * Associate the ereport with this case.
776 */
777 fmd_case_add_ereport(hdl, zcp->zc_case, ep);
778
779 /*
780 * Don't do anything else if this case is already solved.
781 */
782 if (fmd_case_solved(hdl, zcp->zc_case))
783 return;
784
785 fmd_hdl_debug(hdl, "error event '%s'", class);
786
787 /*
788 * Determine if we should solve the case and generate a fault. We solve
789 * a case if:
790 *
791 * a. A pool failed to open (ereport.fs.zfs.pool)
792 * b. A device failed to open (ereport.fs.zfs.pool) while a pool
793 * was up and running.
794 *
795 * We may see a series of ereports associated with a pool open, all
796 * chained together by the same ENA. If the pool open succeeds, then
797 * we'll see no further ereports. To detect when a pool open has
798 * succeeded, we associate a timer with the event. When it expires, we
799 * close the case.
800 */
801 if (fmd_nvl_class_match(hdl, nvl,
802 ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_POOL))) {
803 /*
804 * Pool level fault. Before solving the case, go through and
805 * close any open device cases that may be pending.
806 */
807 for (dcp = uu_list_first(zfs_cases); dcp != NULL;
808 dcp = uu_list_next(zfs_cases, dcp)) {
809 if (dcp->zc_data.zc_pool_guid ==
810 zcp->zc_data.zc_pool_guid &&
811 dcp->zc_data.zc_vdev_guid != 0)
812 fmd_case_close(hdl, dcp->zc_case);
813 }
814
815 zfs_case_solve(hdl, zcp, "fault.fs.zfs.pool", B_TRUE);
816 } else if (fmd_nvl_class_match(hdl, nvl,
817 ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_LOG_REPLAY))) {
818 /*
819 * Pool level fault for reading the intent logs.
820 */
821 zfs_case_solve(hdl, zcp, "fault.fs.zfs.log_replay", B_TRUE);
822 } else if (fmd_nvl_class_match(hdl, nvl, "ereport.fs.zfs.vdev.*")) {
823 /*
824 * Device fault.
825 */
826 zfs_case_solve(hdl, zcp, "fault.fs.zfs.device", B_TRUE);
827 } else if (fmd_nvl_class_match(hdl, nvl,
828 ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_IO)) ||
829 fmd_nvl_class_match(hdl, nvl,
830 ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_CHECKSUM)) ||
831 fmd_nvl_class_match(hdl, nvl,
832 ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_IO_FAILURE)) ||
833 fmd_nvl_class_match(hdl, nvl,
834 ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_PROBE_FAILURE))) {
835 char *failmode = NULL;
836 boolean_t checkremove = B_FALSE;
837
838 /*
839 * If this is a checksum or I/O error, then toss it into the
840 * appropriate SERD engine and check to see if it has fired.
841 * Ideally, we want to do something more sophisticated,
842 * (persistent errors for a single data block, etc). For now,
843 * a single SERD engine is sufficient.
844 */
845 if (fmd_nvl_class_match(hdl, nvl,
846 ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_IO))) {
847 if (zcp->zc_data.zc_serd_io[0] == '\0') {
848 zfs_serd_name(zcp->zc_data.zc_serd_io,
849 pool_guid, vdev_guid, "io");
850 fmd_serd_create(hdl, zcp->zc_data.zc_serd_io,
851 fmd_prop_get_int32(hdl, "io_N"),
852 fmd_prop_get_int64(hdl, "io_T"));
853 zfs_case_serialize(hdl, zcp);
854 }
855 if (fmd_serd_record(hdl, zcp->zc_data.zc_serd_io, ep))
856 checkremove = B_TRUE;
857 } else if (fmd_nvl_class_match(hdl, nvl,
858 ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_CHECKSUM))) {
859 if (zcp->zc_data.zc_serd_checksum[0] == '\0') {
860 zfs_serd_name(zcp->zc_data.zc_serd_checksum,
861 pool_guid, vdev_guid, "checksum");
862 fmd_serd_create(hdl,
863 zcp->zc_data.zc_serd_checksum,
864 fmd_prop_get_int32(hdl, "checksum_N"),
865 fmd_prop_get_int64(hdl, "checksum_T"));
866 zfs_case_serialize(hdl, zcp);
867 }
868 if (fmd_serd_record(hdl,
869 zcp->zc_data.zc_serd_checksum, ep)) {
870 zfs_case_solve(hdl, zcp,
871 "fault.fs.zfs.vdev.checksum", B_FALSE);
872 }
873 } else if (fmd_nvl_class_match(hdl, nvl,
874 ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_IO_FAILURE)) &&
875 (nvlist_lookup_string(nvl,
876 FM_EREPORT_PAYLOAD_ZFS_POOL_FAILMODE, &failmode) == 0) &&
877 failmode != NULL) {
878 if (strncmp(failmode, FM_EREPORT_FAILMODE_CONTINUE,
879 strlen(FM_EREPORT_FAILMODE_CONTINUE)) == 0) {
880 zfs_case_solve(hdl, zcp,
881 "fault.fs.zfs.io_failure_continue",
882 B_FALSE);
883 } else if (strncmp(failmode, FM_EREPORT_FAILMODE_WAIT,
884 strlen(FM_EREPORT_FAILMODE_WAIT)) == 0) {
885 zfs_case_solve(hdl, zcp,
886 "fault.fs.zfs.io_failure_wait", B_FALSE);
887 }
888 } else if (fmd_nvl_class_match(hdl, nvl,
889 ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_PROBE_FAILURE))) {
890#ifndef __linux__
891 /* This causes an unexpected fault diagnosis on linux */
892 checkremove = B_TRUE;
893#endif
894 }
895
896 /*
897 * Because I/O errors may be due to device removal, we postpone
898 * any diagnosis until we're sure that we aren't about to
899 * receive a 'resource.fs.zfs.removed' event.
900 */
901 if (checkremove) {
902 if (zcp->zc_data.zc_has_remove_timer)
903 fmd_timer_remove(hdl, zcp->zc_remove_timer);
904 zcp->zc_remove_timer = fmd_timer_install(hdl, zcp, NULL,
905 zfs_remove_timeout);
906 if (!zcp->zc_data.zc_has_remove_timer) {
907 zcp->zc_data.zc_has_remove_timer = 1;
908 zfs_case_serialize(hdl, zcp);
909 }
910 }
911 }
912}
913
914/*
915 * The timeout is fired when we diagnosed an I/O error, and it was not due to
916 * device removal (which would cause the timeout to be cancelled).
917 */
918/* ARGSUSED */
919static void
920zfs_fm_timeout(fmd_hdl_t *hdl, id_t id, void *data)
921{
922 zfs_case_t *zcp = data;
923
924 if (id == zcp->zc_remove_timer)
925 zfs_case_solve(hdl, zcp, "fault.fs.zfs.vdev.io", B_FALSE);
926}
927
928/*
929 * The specified case has been closed and any case-specific
930 * data structures should be deallocated.
931 */
932static void
933zfs_fm_close(fmd_hdl_t *hdl, fmd_case_t *cs)
934{
935 zfs_case_t *zcp = fmd_case_getspecific(hdl, cs);
936
937 if (zcp->zc_data.zc_serd_checksum[0] != '\0')
938 fmd_serd_destroy(hdl, zcp->zc_data.zc_serd_checksum);
939 if (zcp->zc_data.zc_serd_io[0] != '\0')
940 fmd_serd_destroy(hdl, zcp->zc_data.zc_serd_io);
941 if (zcp->zc_data.zc_has_remove_timer)
942 fmd_timer_remove(hdl, zcp->zc_remove_timer);
943
944 uu_list_remove(zfs_cases, zcp);
945 uu_list_node_fini(zcp, &zcp->zc_node, zfs_case_pool);
946 fmd_hdl_free(hdl, zcp, sizeof (zfs_case_t));
947}
948
949/*
950 * We use the fmd gc entry point to look for old cases that no longer apply.
951 * This allows us to keep our set of case data small in a long running system.
952 */
953static void
954zfs_fm_gc(fmd_hdl_t *hdl)
955{
956 zfs_purge_cases(hdl);
957}
958
959static const fmd_hdl_ops_t fmd_ops = {
960 zfs_fm_recv, /* fmdo_recv */
961 zfs_fm_timeout, /* fmdo_timeout */
962 zfs_fm_close, /* fmdo_close */
963 NULL, /* fmdo_stats */
964 zfs_fm_gc, /* fmdo_gc */
965};
966
967static const fmd_prop_t fmd_props[] = {
968 { "checksum_N", FMD_TYPE_UINT32, "10" },
969 { "checksum_T", FMD_TYPE_TIME, "10min" },
970 { "io_N", FMD_TYPE_UINT32, "10" },
971 { "io_T", FMD_TYPE_TIME, "10min" },
972 { "remove_timeout", FMD_TYPE_TIME, "15sec" },
973 { NULL, 0, NULL }
974};
975
976static const fmd_hdl_info_t fmd_info = {
977 "ZFS Diagnosis Engine", "1.0", &fmd_ops, fmd_props
978};
979
980void
981_zfs_diagnosis_init(fmd_hdl_t *hdl)
982{
983 libzfs_handle_t *zhdl;
984
985 if ((zhdl = __libzfs_init()) == NULL)
986 return;
987
988 if ((zfs_case_pool = uu_list_pool_create("zfs_case_pool",
989 sizeof (zfs_case_t), offsetof(zfs_case_t, zc_node),
990 NULL, UU_LIST_POOL_DEBUG)) == NULL) {
991 __libzfs_fini(zhdl);
992 return;
993 }
994
995 if ((zfs_cases = uu_list_create(zfs_case_pool, NULL,
996 UU_LIST_DEBUG)) == NULL) {
997 uu_list_pool_destroy(zfs_case_pool);
998 __libzfs_fini(zhdl);
999 return;
1000 }
1001
1002 if (fmd_hdl_register(hdl, FMD_API_VERSION, &fmd_info) != 0) {
1003 uu_list_destroy(zfs_cases);
1004 uu_list_pool_destroy(zfs_case_pool);
1005 __libzfs_fini(zhdl);
1006 return;
1007 }
1008
1009 fmd_hdl_setspecific(hdl, zhdl);
1010
1011 (void) fmd_stat_create(hdl, FMD_STAT_NOALLOC, sizeof (zfs_stats) /
1012 sizeof (fmd_stat_t), (fmd_stat_t *)&zfs_stats);
1013
1014 zfs_remove_timeout = fmd_prop_get_int64(hdl, "remove_timeout");
1015}
1016
1017void
1018_zfs_diagnosis_fini(fmd_hdl_t *hdl)
1019{
1020 zfs_case_t *zcp;
1021 uu_list_walk_t *walk;
1022 libzfs_handle_t *zhdl;
1023
1024 /*
1025 * Remove all active cases.
1026 */
1027 walk = uu_list_walk_start(zfs_cases, UU_WALK_ROBUST);
1028 while ((zcp = uu_list_walk_next(walk)) != NULL) {
1029 fmd_hdl_debug(hdl, "removing case ena %llu",
1030 (long long unsigned)zcp->zc_data.zc_ena);
1031 uu_list_remove(zfs_cases, zcp);
1032 uu_list_node_fini(zcp, &zcp->zc_node, zfs_case_pool);
1033 fmd_hdl_free(hdl, zcp, sizeof (zfs_case_t));
1034 }
1035 uu_list_walk_end(walk);
1036
1037 uu_list_destroy(zfs_cases);
1038 uu_list_pool_destroy(zfs_case_pool);
1039
1040 zhdl = fmd_hdl_getspecific(hdl);
1041 __libzfs_fini(zhdl);
1042}