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