Path: blob/main/sys/contrib/openzfs/cmd/zed/agents/zfs_diagnosis.c
109520 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* 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.LICENSE9* or https://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/2122/*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* Copyright (c) 2023, Klara Inc.27*/2829#include <stddef.h>30#include <string.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#include <sys/zio.h>3839#include "zfs_agents.h"40#include "fmd_api.h"4142/*43* Default values for the serd engine when processing checksum or io errors. The44* semantics are N <events> in T <seconds>.45*/46#define DEFAULT_CHECKSUM_N 10 /* events */47#define DEFAULT_CHECKSUM_T 600 /* seconds */48#define DEFAULT_IO_N 10 /* events */49#define DEFAULT_IO_T 600 /* seconds */50#define DEFAULT_SLOW_IO_N 10 /* events */51#define DEFAULT_SLOW_IO_T 30 /* seconds */5253#define CASE_GC_TIMEOUT_SECS 43200 /* 12 hours */5455/*56* Our serd engines are named in the following format:57* 'zfs_<pool_guid>_<vdev_guid>_{checksum,io,slow_io}'58* This #define reserves enough space for two 64-bit hex values plus the59* length of the longest string.60*/61#define MAX_SERDLEN (16 * 2 + sizeof ("zfs___checksum"))6263/*64* On-disk case structure. This must maintain backwards compatibility with65* previous versions of the DE. By default, any members appended to the end66* will be filled with zeros if they don't exist in a previous version.67*/68typedef struct zfs_case_data {69uint64_t zc_version;70uint64_t zc_ena;71uint64_t zc_pool_guid;72uint64_t zc_vdev_guid;73uint64_t zc_parent_guid;74int zc_pool_state;75char zc_serd_checksum[MAX_SERDLEN];76char zc_serd_io[MAX_SERDLEN];77char zc_serd_slow_io[MAX_SERDLEN];78int zc_has_remove_timer;79} zfs_case_data_t;8081/*82* Time-of-day83*/84typedef struct er_timeval {85uint64_t ertv_sec;86uint64_t ertv_nsec;87} er_timeval_t;8889/*90* In-core case structure.91*/92typedef struct zfs_case {93boolean_t zc_present;94uint32_t zc_version;95zfs_case_data_t zc_data;96fmd_case_t *zc_case;97list_node_t zc_node;98id_t zc_remove_timer;99char *zc_fru;100er_timeval_t zc_when;101} zfs_case_t;102103#define CASE_DATA "data"104#define CASE_FRU "fru"105#define CASE_DATA_VERSION_INITIAL 1106#define CASE_DATA_VERSION_SERD 2107108typedef struct zfs_de_stats {109fmd_stat_t old_drops;110fmd_stat_t dev_drops;111fmd_stat_t vdev_drops;112fmd_stat_t import_drops;113fmd_stat_t resource_drops;114} zfs_de_stats_t;115116zfs_de_stats_t zfs_stats = {117{ "old_drops", FMD_TYPE_UINT64, "ereports dropped (from before load)" },118{ "dev_drops", FMD_TYPE_UINT64, "ereports dropped (dev during open)"},119{ "vdev_drops", FMD_TYPE_UINT64, "ereports dropped (weird vdev types)"},120{ "import_drops", FMD_TYPE_UINT64, "ereports dropped (during import)" },121{ "resource_drops", FMD_TYPE_UINT64, "resource related ereports" }122};123124/* wait 15 seconds after a removal */125static hrtime_t zfs_remove_timeout = SEC2NSEC(15);126127static list_t zfs_cases;128129#define ZFS_MAKE_RSRC(type) \130FM_RSRC_CLASS "." ZFS_ERROR_CLASS "." type131#define ZFS_MAKE_EREPORT(type) \132FM_EREPORT_CLASS "." ZFS_ERROR_CLASS "." type133134static void zfs_purge_cases(fmd_hdl_t *hdl);135136/*137* Write out the persistent representation of an active case.138*/139static void140zfs_case_serialize(zfs_case_t *zcp)141{142zcp->zc_data.zc_version = CASE_DATA_VERSION_SERD;143}144145/*146* Read back the persistent representation of an active case.147*/148static zfs_case_t *149zfs_case_unserialize(fmd_hdl_t *hdl, fmd_case_t *cp)150{151zfs_case_t *zcp;152153zcp = fmd_hdl_zalloc(hdl, sizeof (zfs_case_t), FMD_SLEEP);154zcp->zc_case = cp;155156fmd_buf_read(hdl, cp, CASE_DATA, &zcp->zc_data,157sizeof (zcp->zc_data));158159if (zcp->zc_data.zc_version > CASE_DATA_VERSION_SERD) {160fmd_hdl_free(hdl, zcp, sizeof (zfs_case_t));161return (NULL);162}163164/*165* fmd_buf_read() will have already zeroed out the remainder of the166* buffer, so we don't have to do anything special if the version167* doesn't include the SERD engine name.168*/169170if (zcp->zc_data.zc_has_remove_timer)171zcp->zc_remove_timer = fmd_timer_install(hdl, zcp,172NULL, zfs_remove_timeout);173174list_link_init(&zcp->zc_node);175list_insert_head(&zfs_cases, zcp);176177fmd_case_setspecific(hdl, cp, zcp);178179return (zcp);180}181182/*183* Return count of other unique SERD cases under same vdev parent184*/185static uint_t186zfs_other_serd_cases(fmd_hdl_t *hdl, const zfs_case_data_t *zfs_case)187{188zfs_case_t *zcp;189uint_t cases = 0;190static hrtime_t next_check = 0;191192/*193* Note that plumbing in some external GC would require adding locking,194* since most of this module code is not thread safe and assumes there195* is only one thread running against the module. So we perform GC here196* inline periodically so that future delay induced faults will be197* possible once the issue causing multiple vdev delays is resolved.198*/199if (gethrestime_sec() > next_check) {200/* Periodically purge old SERD entries and stale cases */201fmd_serd_gc(hdl);202zfs_purge_cases(hdl);203next_check = gethrestime_sec() + CASE_GC_TIMEOUT_SECS;204}205206for (zcp = list_head(&zfs_cases); zcp != NULL;207zcp = list_next(&zfs_cases, zcp)) {208zfs_case_data_t *zcd = &zcp->zc_data;209210/*211* must be same pool and parent vdev but different leaf vdev212*/213if (zcd->zc_pool_guid != zfs_case->zc_pool_guid ||214zcd->zc_parent_guid != zfs_case->zc_parent_guid ||215zcd->zc_vdev_guid == zfs_case->zc_vdev_guid) {216continue;217}218219/*220* Check if there is another active serd case besides zfs_case221*222* Only one serd engine will be assigned to the case223*/224if (zcd->zc_serd_checksum[0] == zfs_case->zc_serd_checksum[0] &&225fmd_serd_active(hdl, zcd->zc_serd_checksum)) {226cases++;227}228if (zcd->zc_serd_io[0] == zfs_case->zc_serd_io[0] &&229fmd_serd_active(hdl, zcd->zc_serd_io)) {230cases++;231}232if (zcd->zc_serd_slow_io[0] == zfs_case->zc_serd_slow_io[0] &&233fmd_serd_active(hdl, zcd->zc_serd_slow_io)) {234cases++;235}236}237return (cases);238}239240/*241* Iterate over any active cases. If any cases are associated with a pool or242* vdev which is no longer present on the system, close the associated case.243*/244static void245zfs_mark_vdev(uint64_t pool_guid, nvlist_t *vd, er_timeval_t *loaded)246{247uint64_t vdev_guid = 0;248uint_t c, children;249nvlist_t **child;250zfs_case_t *zcp;251252(void) nvlist_lookup_uint64(vd, ZPOOL_CONFIG_GUID, &vdev_guid);253254/*255* Mark any cases associated with this (pool, vdev) pair.256*/257for (zcp = list_head(&zfs_cases); zcp != NULL;258zcp = list_next(&zfs_cases, zcp)) {259if (zcp->zc_data.zc_pool_guid == pool_guid &&260zcp->zc_data.zc_vdev_guid == vdev_guid) {261zcp->zc_present = B_TRUE;262zcp->zc_when = *loaded;263}264}265266/*267* Iterate over all children.268*/269if (nvlist_lookup_nvlist_array(vd, ZPOOL_CONFIG_CHILDREN, &child,270&children) == 0) {271for (c = 0; c < children; c++)272zfs_mark_vdev(pool_guid, child[c], loaded);273}274275if (nvlist_lookup_nvlist_array(vd, ZPOOL_CONFIG_L2CACHE, &child,276&children) == 0) {277for (c = 0; c < children; c++)278zfs_mark_vdev(pool_guid, child[c], loaded);279}280281if (nvlist_lookup_nvlist_array(vd, ZPOOL_CONFIG_SPARES, &child,282&children) == 0) {283for (c = 0; c < children; c++)284zfs_mark_vdev(pool_guid, child[c], loaded);285}286}287288static int289zfs_mark_pool(zpool_handle_t *zhp, void *unused)290{291(void) unused;292zfs_case_t *zcp;293uint64_t pool_guid;294uint64_t *tod;295er_timeval_t loaded = { 0 };296nvlist_t *config, *vd;297uint_t nelem = 0;298int ret;299300pool_guid = zpool_get_prop_int(zhp, ZPOOL_PROP_GUID, NULL);301/*302* Mark any cases associated with just this pool.303*/304for (zcp = list_head(&zfs_cases); zcp != NULL;305zcp = list_next(&zfs_cases, zcp)) {306if (zcp->zc_data.zc_pool_guid == pool_guid &&307zcp->zc_data.zc_vdev_guid == 0)308zcp->zc_present = B_TRUE;309}310311if ((config = zpool_get_config(zhp, NULL)) == NULL) {312zpool_close(zhp);313return (-1);314}315316(void) nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_LOADED_TIME,317&tod, &nelem);318if (nelem == 2) {319loaded.ertv_sec = tod[0];320loaded.ertv_nsec = tod[1];321for (zcp = list_head(&zfs_cases); zcp != NULL;322zcp = list_next(&zfs_cases, zcp)) {323if (zcp->zc_data.zc_pool_guid == pool_guid &&324zcp->zc_data.zc_vdev_guid == 0) {325zcp->zc_when = loaded;326}327}328}329330ret = nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &vd);331if (ret) {332zpool_close(zhp);333return (-1);334}335336zfs_mark_vdev(pool_guid, vd, &loaded);337338zpool_close(zhp);339340return (0);341}342343struct load_time_arg {344uint64_t lt_guid;345er_timeval_t *lt_time;346boolean_t lt_found;347};348349static int350zpool_find_load_time(zpool_handle_t *zhp, void *arg)351{352struct load_time_arg *lta = arg;353uint64_t pool_guid;354uint64_t *tod;355nvlist_t *config;356uint_t nelem;357358if (lta->lt_found) {359zpool_close(zhp);360return (0);361}362363pool_guid = zpool_get_prop_int(zhp, ZPOOL_PROP_GUID, NULL);364if (pool_guid != lta->lt_guid) {365zpool_close(zhp);366return (0);367}368369if ((config = zpool_get_config(zhp, NULL)) == NULL) {370zpool_close(zhp);371return (-1);372}373374if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_LOADED_TIME,375&tod, &nelem) == 0 && nelem == 2) {376lta->lt_found = B_TRUE;377lta->lt_time->ertv_sec = tod[0];378lta->lt_time->ertv_nsec = tod[1];379}380381zpool_close(zhp);382383return (0);384}385386static void387zfs_purge_cases(fmd_hdl_t *hdl)388{389zfs_case_t *zcp, *next;390libzfs_handle_t *zhdl = fmd_hdl_getspecific(hdl);391392/*393* There is no way to open a pool by GUID, or lookup a vdev by GUID. No394* matter what we do, we're going to have to stomach an O(vdevs * cases)395* algorithm. In reality, both quantities are likely so small that396* neither will matter. Given that iterating over pools is more397* expensive than iterating over the in-memory case list, we opt for a398* 'present' flag in each case that starts off cleared. We then iterate399* over all pools, marking those that are still present, and removing400* those that aren't found.401*402* Note that we could also construct an FMRI and rely on403* fmd_nvl_fmri_present(), but this would end up doing the same search.404*/405406/*407* Mark the cases as not present.408*/409for (zcp = list_head(&zfs_cases); zcp != NULL;410zcp = list_next(&zfs_cases, zcp))411zcp->zc_present = B_FALSE;412413/*414* Iterate over all pools and mark the pools and vdevs found. If this415* fails (most probably because we're out of memory), then don't close416* any of the cases and we cannot be sure they are accurate.417*/418if (zpool_iter(zhdl, zfs_mark_pool, NULL) != 0)419return;420421/*422* Remove those cases which were not found.423*/424for (zcp = list_head(&zfs_cases); zcp != NULL; zcp = next) {425next = list_next(&zfs_cases, zcp);426if (!zcp->zc_present)427fmd_case_close(hdl, zcp->zc_case);428}429}430431/*432* Construct the name of a serd engine given the pool/vdev GUID and type (io or433* checksum).434*/435static void436zfs_serd_name(char *buf, uint64_t pool_guid, uint64_t vdev_guid,437const char *type)438{439(void) snprintf(buf, MAX_SERDLEN, "zfs_%llx_%llx_%s",440(long long unsigned int)pool_guid,441(long long unsigned int)vdev_guid, type);442}443444static void445zfs_case_retire(fmd_hdl_t *hdl, zfs_case_t *zcp)446{447fmd_hdl_debug(hdl, "retiring case");448449fmd_case_close(hdl, zcp->zc_case);450}451452/*453* Solve a given ZFS case. This first checks to make sure the diagnosis is454* still valid, as well as cleaning up any pending timer associated with the455* case.456*/457static void458zfs_case_solve(fmd_hdl_t *hdl, zfs_case_t *zcp, const char *faultname)459{460nvlist_t *detector, *fault;461boolean_t serialize;462nvlist_t *fru = NULL;463fmd_hdl_debug(hdl, "solving fault '%s'", faultname);464465/*466* Construct the detector from the case data. The detector is in the467* ZFS scheme, and is either the pool or the vdev, depending on whether468* this is a vdev or pool fault.469*/470detector = fmd_nvl_alloc(hdl, FMD_SLEEP);471472(void) nvlist_add_uint8(detector, FM_VERSION, ZFS_SCHEME_VERSION0);473(void) nvlist_add_string(detector, FM_FMRI_SCHEME, FM_FMRI_SCHEME_ZFS);474(void) nvlist_add_uint64(detector, FM_FMRI_ZFS_POOL,475zcp->zc_data.zc_pool_guid);476if (zcp->zc_data.zc_vdev_guid != 0) {477(void) nvlist_add_uint64(detector, FM_FMRI_ZFS_VDEV,478zcp->zc_data.zc_vdev_guid);479}480481fault = fmd_nvl_create_fault(hdl, faultname, 100, detector,482fru, detector);483fmd_case_add_suspect(hdl, zcp->zc_case, fault);484485nvlist_free(fru);486487fmd_case_solve(hdl, zcp->zc_case);488489serialize = B_FALSE;490if (zcp->zc_data.zc_has_remove_timer) {491fmd_timer_remove(hdl, zcp->zc_remove_timer);492zcp->zc_data.zc_has_remove_timer = 0;493serialize = B_TRUE;494}495if (serialize)496zfs_case_serialize(zcp);497498nvlist_free(detector);499}500501static boolean_t502timeval_earlier(er_timeval_t *a, er_timeval_t *b)503{504return (a->ertv_sec < b->ertv_sec ||505(a->ertv_sec == b->ertv_sec && a->ertv_nsec < b->ertv_nsec));506}507508static void509zfs_ereport_when(fmd_hdl_t *hdl, nvlist_t *nvl, er_timeval_t *when)510{511(void) hdl;512int64_t *tod;513uint_t nelem;514515if (nvlist_lookup_int64_array(nvl, FM_EREPORT_TIME, &tod,516&nelem) == 0 && nelem == 2) {517when->ertv_sec = tod[0];518when->ertv_nsec = tod[1];519} else {520when->ertv_sec = when->ertv_nsec = UINT64_MAX;521}522}523524/*525* Record the specified event in the SERD engine and return a526* boolean value indicating whether or not the engine fired as527* the result of inserting this event.528*529* When the pool has similar active cases on other vdevs, then530* the fired state is disregarded and the case is retired.531*/532static int533zfs_fm_serd_record(fmd_hdl_t *hdl, const char *name, fmd_event_t *ep,534zfs_case_t *zcp, const char *err_type)535{536int fired = fmd_serd_record(hdl, name, ep);537int peers = 0;538539if (fired && (peers = zfs_other_serd_cases(hdl, &zcp->zc_data)) > 0) {540fmd_hdl_debug(hdl, "pool %llu is tracking %d other %s cases "541"-- skip faulting the vdev %llu",542(u_longlong_t)zcp->zc_data.zc_pool_guid,543peers, err_type,544(u_longlong_t)zcp->zc_data.zc_vdev_guid);545zfs_case_retire(hdl, zcp);546fired = 0;547}548549return (fired);550}551552/*553* Main fmd entry point.554*/555static void556zfs_fm_recv(fmd_hdl_t *hdl, fmd_event_t *ep, nvlist_t *nvl, const char *class)557{558zfs_case_t *zcp, *dcp;559int32_t pool_state;560uint64_t ena, pool_guid, vdev_guid, parent_guid;561uint64_t checksum_n, checksum_t;562uint64_t io_n, io_t;563er_timeval_t pool_load;564er_timeval_t er_when;565nvlist_t *detector;566boolean_t pool_found = B_FALSE;567boolean_t isresource;568const char *type;569570/*571* We subscribe to notifications for vdev or pool removal. In these572* cases, there may be cases that no longer apply. Purge any cases573* that no longer apply.574*/575if (fmd_nvl_class_match(hdl, nvl, "sysevent.fs.zfs.*")) {576fmd_hdl_debug(hdl, "purging orphaned cases from %s",577strrchr(class, '.') + 1);578zfs_purge_cases(hdl);579zfs_stats.resource_drops.fmds_value.ui64++;580return;581}582583isresource = fmd_nvl_class_match(hdl, nvl, "resource.fs.zfs.*");584585if (isresource) {586/*587* For resources, we don't have a normal payload.588*/589if (nvlist_lookup_uint64(nvl, FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID,590&vdev_guid) != 0)591pool_state = SPA_LOAD_OPEN;592else593pool_state = SPA_LOAD_NONE;594detector = NULL;595} else {596(void) nvlist_lookup_nvlist(nvl,597FM_EREPORT_DETECTOR, &detector);598(void) nvlist_lookup_int32(nvl,599FM_EREPORT_PAYLOAD_ZFS_POOL_CONTEXT, &pool_state);600}601602/*603* We also ignore all ereports generated during an import of a pool,604* since the only possible fault (.pool) would result in import failure,605* and hence no persistent fault. Some day we may want to do something606* with these ereports, so we continue generating them internally.607*/608if (pool_state == SPA_LOAD_IMPORT) {609zfs_stats.import_drops.fmds_value.ui64++;610fmd_hdl_debug(hdl, "ignoring '%s' during import", class);611return;612}613614/*615* Device I/O errors are ignored during pool open.616*/617if (pool_state == SPA_LOAD_OPEN &&618(fmd_nvl_class_match(hdl, nvl,619ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_CHECKSUM)) ||620fmd_nvl_class_match(hdl, nvl,621ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_IO)) ||622fmd_nvl_class_match(hdl, nvl,623ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_PROBE_FAILURE)))) {624fmd_hdl_debug(hdl, "ignoring '%s' during pool open", class);625zfs_stats.dev_drops.fmds_value.ui64++;626return;627}628629/*630* We ignore ereports for anything except disks and files.631*/632if (nvlist_lookup_string(nvl, FM_EREPORT_PAYLOAD_ZFS_VDEV_TYPE,633&type) == 0) {634if (strcmp(type, VDEV_TYPE_DISK) != 0 &&635strcmp(type, VDEV_TYPE_FILE) != 0) {636zfs_stats.vdev_drops.fmds_value.ui64++;637return;638}639}640641/*642* Determine if this ereport corresponds to an open case.643* Each vdev or pool can have a single case.644*/645(void) nvlist_lookup_uint64(nvl,646FM_EREPORT_PAYLOAD_ZFS_POOL_GUID, &pool_guid);647if (nvlist_lookup_uint64(nvl,648FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID, &vdev_guid) != 0)649vdev_guid = 0;650if (nvlist_lookup_uint64(nvl,651FM_EREPORT_PAYLOAD_ZFS_PARENT_GUID, &parent_guid) != 0)652parent_guid = 0;653if (nvlist_lookup_uint64(nvl, FM_EREPORT_ENA, &ena) != 0)654ena = 0;655656zfs_ereport_when(hdl, nvl, &er_when);657658for (zcp = list_head(&zfs_cases); zcp != NULL;659zcp = list_next(&zfs_cases, zcp)) {660if (zcp->zc_data.zc_pool_guid == pool_guid) {661pool_found = B_TRUE;662pool_load = zcp->zc_when;663}664if (zcp->zc_data.zc_vdev_guid == vdev_guid)665break;666}667668/*669* Avoid falsely accusing a pool of being faulty. Do so by670* not replaying ereports that were generated prior to the671* current import. If the failure that generated them was672* transient because the device was actually removed but we673* didn't receive the normal asynchronous notification, we674* don't want to mark it as faulted and potentially panic. If675* there is still a problem we'd expect not to be able to676* import the pool, or that new ereports will be generated677* once the pool is used.678*/679if (pool_found && timeval_earlier(&er_when, &pool_load)) {680fmd_hdl_debug(hdl, "ignoring pool %llx, "681"ereport time %lld.%lld, pool load time = %lld.%lld",682pool_guid, er_when.ertv_sec, er_when.ertv_nsec,683pool_load.ertv_sec, pool_load.ertv_nsec);684zfs_stats.old_drops.fmds_value.ui64++;685return;686}687688if (!pool_found) {689/*690* Haven't yet seen this pool, but same situation691* may apply.692*/693libzfs_handle_t *zhdl = fmd_hdl_getspecific(hdl);694struct load_time_arg la;695696la.lt_guid = pool_guid;697la.lt_time = &pool_load;698la.lt_found = B_FALSE;699700if (zhdl != NULL &&701zpool_iter(zhdl, zpool_find_load_time, &la) == 0 &&702la.lt_found == B_TRUE) {703pool_found = B_TRUE;704705if (timeval_earlier(&er_when, &pool_load)) {706fmd_hdl_debug(hdl, "ignoring pool %llx, "707"ereport time %lld.%lld, "708"pool load time = %lld.%lld",709pool_guid, er_when.ertv_sec,710er_when.ertv_nsec, pool_load.ertv_sec,711pool_load.ertv_nsec);712zfs_stats.old_drops.fmds_value.ui64++;713return;714}715}716}717718if (zcp == NULL) {719fmd_case_t *cs;720zfs_case_data_t data = { 0 };721722/*723* If this is one of our 'fake' resource ereports, and there is724* no case open, simply discard it.725*/726if (isresource) {727zfs_stats.resource_drops.fmds_value.ui64++;728fmd_hdl_debug(hdl, "discarding '%s for vdev %llu",729class, vdev_guid);730return;731}732733/*734* Skip tracking some ereports735*/736if (strcmp(class,737ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_DATA)) == 0 ||738strcmp(class,739ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_CONFIG_CACHE_WRITE)) == 0) {740zfs_stats.resource_drops.fmds_value.ui64++;741return;742}743744/*745* Open a new case.746*/747cs = fmd_case_open(hdl, NULL);748749fmd_hdl_debug(hdl, "opening case for vdev %llu due to '%s'",750vdev_guid, class);751752/*753* Initialize the case buffer. To commonize code, we actually754* create the buffer with existing data, and then call755* zfs_case_unserialize() to instantiate the in-core structure.756*/757fmd_buf_create(hdl, cs, CASE_DATA, sizeof (zfs_case_data_t));758759data.zc_version = CASE_DATA_VERSION_SERD;760data.zc_ena = ena;761data.zc_pool_guid = pool_guid;762data.zc_vdev_guid = vdev_guid;763data.zc_parent_guid = parent_guid;764data.zc_pool_state = (int)pool_state;765766fmd_buf_write(hdl, cs, CASE_DATA, &data, sizeof (data));767768zcp = zfs_case_unserialize(hdl, cs);769assert(zcp != NULL);770if (pool_found)771zcp->zc_when = pool_load;772}773774if (isresource) {775fmd_hdl_debug(hdl, "resource event '%s'", class);776777if (fmd_nvl_class_match(hdl, nvl,778ZFS_MAKE_RSRC(FM_RESOURCE_AUTOREPLACE))) {779/*780* The 'resource.fs.zfs.autoreplace' event indicates781* that the pool was loaded with the 'autoreplace'782* property set. In this case, any pending device783* failures should be ignored, as the asynchronous784* autoreplace handling will take care of them.785*/786fmd_case_close(hdl, zcp->zc_case);787} else if (fmd_nvl_class_match(hdl, nvl,788ZFS_MAKE_RSRC(FM_RESOURCE_REMOVED))) {789/*790* The 'resource.fs.zfs.removed' event indicates that791* device removal was detected, and the device was792* closed asynchronously. If this is the case, we793* assume that any recent I/O errors were due to the794* device removal, not any fault of the device itself.795* We reset the SERD engine, and cancel any pending796* timers.797*/798if (zcp->zc_data.zc_has_remove_timer) {799fmd_timer_remove(hdl, zcp->zc_remove_timer);800zcp->zc_data.zc_has_remove_timer = 0;801zfs_case_serialize(zcp);802}803if (zcp->zc_data.zc_serd_io[0] != '\0')804fmd_serd_reset(hdl, zcp->zc_data.zc_serd_io);805if (zcp->zc_data.zc_serd_checksum[0] != '\0')806fmd_serd_reset(hdl,807zcp->zc_data.zc_serd_checksum);808if (zcp->zc_data.zc_serd_slow_io[0] != '\0')809fmd_serd_reset(hdl,810zcp->zc_data.zc_serd_slow_io);811} else if (fmd_nvl_class_match(hdl, nvl,812ZFS_MAKE_RSRC(FM_RESOURCE_STATECHANGE))) {813uint64_t state = 0;814815if (zcp != NULL &&816nvlist_lookup_uint64(nvl,817FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE, &state) == 0 &&818state == VDEV_STATE_HEALTHY) {819fmd_hdl_debug(hdl, "closing case after a "820"device statechange to healthy");821fmd_case_close(hdl, zcp->zc_case);822}823}824zfs_stats.resource_drops.fmds_value.ui64++;825return;826}827828/*829* Associate the ereport with this case.830*/831fmd_case_add_ereport(hdl, zcp->zc_case, ep);832833/*834* Don't do anything else if this case is already solved.835*/836if (fmd_case_solved(hdl, zcp->zc_case))837return;838839if (vdev_guid)840fmd_hdl_debug(hdl, "error event '%s', vdev %llu", class,841vdev_guid);842else843fmd_hdl_debug(hdl, "error event '%s'", class);844845/*846* Determine if we should solve the case and generate a fault. We solve847* a case if:848*849* a. A pool failed to open (ereport.fs.zfs.pool)850* b. A device failed to open (ereport.fs.zfs.pool) while a pool851* was up and running.852*853* We may see a series of ereports associated with a pool open, all854* chained together by the same ENA. If the pool open succeeds, then855* we'll see no further ereports. To detect when a pool open has856* succeeded, we associate a timer with the event. When it expires, we857* close the case.858*/859if (fmd_nvl_class_match(hdl, nvl,860ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_POOL))) {861/*862* Pool level fault. Before solving the case, go through and863* close any open device cases that may be pending.864*/865for (dcp = list_head(&zfs_cases); dcp != NULL;866dcp = list_next(&zfs_cases, dcp)) {867if (dcp->zc_data.zc_pool_guid ==868zcp->zc_data.zc_pool_guid &&869dcp->zc_data.zc_vdev_guid != 0)870fmd_case_close(hdl, dcp->zc_case);871}872873zfs_case_solve(hdl, zcp, "fault.fs.zfs.pool");874} else if (fmd_nvl_class_match(hdl, nvl,875ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_LOG_REPLAY))) {876/*877* Pool level fault for reading the intent logs.878*/879zfs_case_solve(hdl, zcp, "fault.fs.zfs.log_replay");880} else if (fmd_nvl_class_match(hdl, nvl, "ereport.fs.zfs.vdev.*")) {881/*882* Device fault.883*/884zfs_case_solve(hdl, zcp, "fault.fs.zfs.device");885} else if (fmd_nvl_class_match(hdl, nvl,886ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_IO)) ||887fmd_nvl_class_match(hdl, nvl,888ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_CHECKSUM)) ||889fmd_nvl_class_match(hdl, nvl,890ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_IO_FAILURE)) ||891fmd_nvl_class_match(hdl, nvl,892ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_DELAY)) ||893fmd_nvl_class_match(hdl, nvl,894ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_PROBE_FAILURE))) {895const char *failmode = NULL;896boolean_t checkremove = B_FALSE;897uint32_t pri = 0;898899/*900* If this is a checksum or I/O error, then toss it into the901* appropriate SERD engine and check to see if it has fired.902* Ideally, we want to do something more sophisticated,903* (persistent errors for a single data block, etc). For now,904* a single SERD engine is sufficient.905*/906if (fmd_nvl_class_match(hdl, nvl,907ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_IO))) {908if (zcp->zc_data.zc_serd_io[0] == '\0') {909if (nvlist_lookup_uint64(nvl,910FM_EREPORT_PAYLOAD_ZFS_VDEV_IO_N,911&io_n) != 0) {912io_n = DEFAULT_IO_N;913}914if (nvlist_lookup_uint64(nvl,915FM_EREPORT_PAYLOAD_ZFS_VDEV_IO_T,916&io_t) != 0) {917io_t = DEFAULT_IO_T;918}919zfs_serd_name(zcp->zc_data.zc_serd_io,920pool_guid, vdev_guid, "io");921fmd_serd_create(hdl, zcp->zc_data.zc_serd_io,922io_n,923SEC2NSEC(io_t));924zfs_case_serialize(zcp);925}926if (zfs_fm_serd_record(hdl, zcp->zc_data.zc_serd_io,927ep, zcp, "io error")) {928checkremove = B_TRUE;929}930} else if (fmd_nvl_class_match(hdl, nvl,931ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_DELAY))) {932uint64_t slow_io_n, slow_io_t;933934/*935* Create a slow io SERD engine when the VDEV has the936* 'vdev_slow_io_n' and 'vdev_slow_io_n' properties.937*/938if (zcp->zc_data.zc_serd_slow_io[0] == '\0' &&939nvlist_lookup_uint64(nvl,940FM_EREPORT_PAYLOAD_ZFS_VDEV_SLOW_IO_N,941&slow_io_n) == 0 &&942nvlist_lookup_uint64(nvl,943FM_EREPORT_PAYLOAD_ZFS_VDEV_SLOW_IO_T,944&slow_io_t) == 0) {945zfs_serd_name(zcp->zc_data.zc_serd_slow_io,946pool_guid, vdev_guid, "slow_io");947fmd_serd_create(hdl,948zcp->zc_data.zc_serd_slow_io,949slow_io_n,950SEC2NSEC(slow_io_t));951zfs_case_serialize(zcp);952}953/* Pass event to SERD engine and see if this triggers */954if (zcp->zc_data.zc_serd_slow_io[0] != '\0' &&955zfs_fm_serd_record(hdl,956zcp->zc_data.zc_serd_slow_io, ep, zcp, "slow io")) {957zfs_case_solve(hdl, zcp,958"fault.fs.zfs.vdev.slow_io");959}960} else if (fmd_nvl_class_match(hdl, nvl,961ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_CHECKSUM))) {962uint64_t flags = 0;963int32_t flags32 = 0;964/*965* We ignore ereports for checksum errors generated by966* scrub/resilver I/O to avoid potentially further967* degrading the pool while it's being repaired.968*969* Note that FM_EREPORT_PAYLOAD_ZFS_ZIO_FLAGS used to970* be int32. To allow newer zed to work on older971* kernels, if we don't find the flags, we look for972* the older ones too.973*/974if (((nvlist_lookup_uint32(nvl,975FM_EREPORT_PAYLOAD_ZFS_ZIO_PRIORITY, &pri) == 0) &&976(pri == ZIO_PRIORITY_SCRUB ||977pri == ZIO_PRIORITY_REBUILD)) ||978((nvlist_lookup_uint64(nvl,979FM_EREPORT_PAYLOAD_ZFS_ZIO_FLAGS, &flags) == 0) &&980(flags & (ZIO_FLAG_SCRUB | ZIO_FLAG_RESILVER))) ||981((nvlist_lookup_int32(nvl,982FM_EREPORT_PAYLOAD_ZFS_ZIO_FLAGS, &flags32) == 0) &&983(flags32 & (ZIO_FLAG_SCRUB | ZIO_FLAG_RESILVER)))) {984fmd_hdl_debug(hdl, "ignoring '%s' for "985"scrub/resilver I/O", class);986return;987}988989if (zcp->zc_data.zc_serd_checksum[0] == '\0') {990if (nvlist_lookup_uint64(nvl,991FM_EREPORT_PAYLOAD_ZFS_VDEV_CKSUM_N,992&checksum_n) != 0) {993checksum_n = DEFAULT_CHECKSUM_N;994}995if (nvlist_lookup_uint64(nvl,996FM_EREPORT_PAYLOAD_ZFS_VDEV_CKSUM_T,997&checksum_t) != 0) {998checksum_t = DEFAULT_CHECKSUM_T;999}10001001zfs_serd_name(zcp->zc_data.zc_serd_checksum,1002pool_guid, vdev_guid, "checksum");1003fmd_serd_create(hdl,1004zcp->zc_data.zc_serd_checksum,1005checksum_n,1006SEC2NSEC(checksum_t));1007zfs_case_serialize(zcp);1008}1009if (zfs_fm_serd_record(hdl,1010zcp->zc_data.zc_serd_checksum, ep, zcp,1011"checksum")) {1012zfs_case_solve(hdl, zcp,1013"fault.fs.zfs.vdev.checksum");1014}1015} else if (fmd_nvl_class_match(hdl, nvl,1016ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_IO_FAILURE)) &&1017(nvlist_lookup_string(nvl,1018FM_EREPORT_PAYLOAD_ZFS_POOL_FAILMODE, &failmode) == 0) &&1019failmode != NULL) {1020if (strncmp(failmode, FM_EREPORT_FAILMODE_CONTINUE,1021strlen(FM_EREPORT_FAILMODE_CONTINUE)) == 0) {1022zfs_case_solve(hdl, zcp,1023"fault.fs.zfs.io_failure_continue");1024} else if (strncmp(failmode, FM_EREPORT_FAILMODE_WAIT,1025strlen(FM_EREPORT_FAILMODE_WAIT)) == 0) {1026zfs_case_solve(hdl, zcp,1027"fault.fs.zfs.io_failure_wait");1028}1029} else if (fmd_nvl_class_match(hdl, nvl,1030ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_PROBE_FAILURE))) {1031#ifndef __linux__1032/* This causes an unexpected fault diagnosis on linux */1033checkremove = B_TRUE;1034#endif1035}10361037/*1038* Because I/O errors may be due to device removal, we postpone1039* any diagnosis until we're sure that we aren't about to1040* receive a 'resource.fs.zfs.removed' event.1041*/1042if (checkremove) {1043if (zcp->zc_data.zc_has_remove_timer)1044fmd_timer_remove(hdl, zcp->zc_remove_timer);1045zcp->zc_remove_timer = fmd_timer_install(hdl, zcp, NULL,1046zfs_remove_timeout);1047if (!zcp->zc_data.zc_has_remove_timer) {1048zcp->zc_data.zc_has_remove_timer = 1;1049zfs_case_serialize(zcp);1050}1051}1052}1053}10541055/*1056* The timeout is fired when we diagnosed an I/O error, and it was not due to1057* device removal (which would cause the timeout to be cancelled).1058*/1059static void1060zfs_fm_timeout(fmd_hdl_t *hdl, id_t id, void *data)1061{1062zfs_case_t *zcp = data;10631064if (id == zcp->zc_remove_timer)1065zfs_case_solve(hdl, zcp, "fault.fs.zfs.vdev.io");1066}10671068/*1069* The specified case has been closed and any case-specific1070* data structures should be deallocated.1071*/1072static void1073zfs_fm_close(fmd_hdl_t *hdl, fmd_case_t *cs)1074{1075zfs_case_t *zcp = fmd_case_getspecific(hdl, cs);10761077if (zcp->zc_data.zc_serd_checksum[0] != '\0')1078fmd_serd_destroy(hdl, zcp->zc_data.zc_serd_checksum);1079if (zcp->zc_data.zc_serd_io[0] != '\0')1080fmd_serd_destroy(hdl, zcp->zc_data.zc_serd_io);1081if (zcp->zc_data.zc_serd_slow_io[0] != '\0')1082fmd_serd_destroy(hdl, zcp->zc_data.zc_serd_slow_io);1083if (zcp->zc_data.zc_has_remove_timer)1084fmd_timer_remove(hdl, zcp->zc_remove_timer);10851086list_remove(&zfs_cases, zcp);1087fmd_hdl_free(hdl, zcp, sizeof (zfs_case_t));1088}10891090static const fmd_hdl_ops_t fmd_ops = {1091zfs_fm_recv, /* fmdo_recv */1092zfs_fm_timeout, /* fmdo_timeout */1093zfs_fm_close, /* fmdo_close */1094NULL, /* fmdo_stats */1095NULL, /* fmdo_gc */1096};10971098static const fmd_prop_t fmd_props[] = {1099{ NULL, 0, NULL }1100};11011102static const fmd_hdl_info_t fmd_info = {1103"ZFS Diagnosis Engine", "1.0", &fmd_ops, fmd_props1104};11051106void1107_zfs_diagnosis_init(fmd_hdl_t *hdl)1108{1109libzfs_handle_t *zhdl;11101111if ((zhdl = libzfs_init()) == NULL)1112return;11131114list_create(&zfs_cases,1115sizeof (zfs_case_t), offsetof(zfs_case_t, zc_node));11161117if (fmd_hdl_register(hdl, FMD_API_VERSION, &fmd_info) != 0) {1118list_destroy(&zfs_cases);1119libzfs_fini(zhdl);1120return;1121}11221123fmd_hdl_setspecific(hdl, zhdl);11241125(void) fmd_stat_create(hdl, FMD_STAT_NOALLOC, sizeof (zfs_stats) /1126sizeof (fmd_stat_t), (fmd_stat_t *)&zfs_stats);1127}11281129void1130_zfs_diagnosis_fini(fmd_hdl_t *hdl)1131{1132zfs_case_t *zcp;1133libzfs_handle_t *zhdl;11341135/*1136* Remove all active cases.1137*/1138while ((zcp = list_remove_head(&zfs_cases)) != NULL) {1139fmd_hdl_debug(hdl, "removing case ena %llu",1140(long long unsigned)zcp->zc_data.zc_ena);1141fmd_hdl_free(hdl, zcp, sizeof (zfs_case_t));1142}11431144list_destroy(&zfs_cases);11451146zhdl = fmd_hdl_getspecific(hdl);1147libzfs_fini(zhdl);1148}114911501151