1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* 27 * Copyright (c) 2012,2021 by Delphix. All rights reserved. 28 */ 29 30 #include <sys/spa.h> 31 #include <sys/spa_impl.h> 32 #include <sys/vdev.h> 33 #include <sys/vdev_impl.h> 34 #include <sys/zio.h> 35 #include <sys/zio_checksum.h> 36 37 #include <sys/fm/fs/zfs.h> 38 #include <sys/fm/protocol.h> 39 #include <sys/fm/util.h> 40 #include <sys/sysevent.h> 41 42 /* 43 * This general routine is responsible for generating all the different ZFS 44 * ereports. The payload is dependent on the class, and which arguments are 45 * supplied to the function: 46 * 47 * EREPORT POOL VDEV IO 48 * block X X X 49 * data X X 50 * device X X 51 * pool X 52 * 53 * If we are in a loading state, all errors are chained together by the same 54 * SPA-wide ENA (Error Numeric Association). 55 * 56 * For isolated I/O requests, we get the ENA from the zio_t. The propagation 57 * gets very complicated due to RAID-Z, gang blocks, and vdev caching. We want 58 * to chain together all ereports associated with a logical piece of data. For 59 * read I/Os, there are basically three 'types' of I/O, which form a roughly 60 * layered diagram: 61 * 62 * +---------------+ 63 * | Aggregate I/O | No associated logical data or device 64 * +---------------+ 65 * | 66 * V 67 * +---------------+ Reads associated with a piece of logical data. 68 * | Read I/O | This includes reads on behalf of RAID-Z, 69 * +---------------+ mirrors, gang blocks, retries, etc. 70 * | 71 * V 72 * +---------------+ Reads associated with a particular device, but 73 * | Physical I/O | no logical data. Issued as part of vdev caching 74 * +---------------+ and I/O aggregation. 75 * 76 * Note that 'physical I/O' here is not the same terminology as used in the rest 77 * of ZIO. Typically, 'physical I/O' simply means that there is no attached 78 * blockpointer. But I/O with no associated block pointer can still be related 79 * to a logical piece of data (i.e. RAID-Z requests). 80 * 81 * Purely physical I/O always have unique ENAs. They are not related to a 82 * particular piece of logical data, and therefore cannot be chained together. 83 * We still generate an ereport, but the DE doesn't correlate it with any 84 * logical piece of data. When such an I/O fails, the delegated I/O requests 85 * will issue a retry, which will trigger the 'real' ereport with the correct 86 * ENA. 87 * 88 * We keep track of the ENA for a ZIO chain through the 'io_logical' member. 89 * When a new logical I/O is issued, we set this to point to itself. Child I/Os 90 * then inherit this pointer, so that when it is first set subsequent failures 91 * will use the same ENA. For vdev cache fill and queue aggregation I/O, 92 * this pointer is set to NULL, and no ereport will be generated (since it 93 * doesn't actually correspond to any particular device or piece of data, 94 * and the caller will always retry without caching or queueing anyway). 95 * 96 * For checksum errors, we want to include more information about the actual 97 * error which occurs. Accordingly, we build an ereport when the error is 98 * noticed, but instead of sending it in immediately, we hang it off of the 99 * io_cksum_report field of the logical IO. When the logical IO completes 100 * (successfully or not), zfs_ereport_finish_checksum() is called with the 101 * good and bad versions of the buffer (if available), and we annotate the 102 * ereport with information about the differences. 103 */ 104 105 #ifdef _KERNEL 106 /* 107 * Duplicate ereport Detection 108 * 109 * Some ereports are retained momentarily for detecting duplicates. These 110 * are kept in a recent_events_node_t in both a time-ordered list and an AVL 111 * tree of recent unique ereports. 112 * 113 * The lifespan of these recent ereports is bounded (15 mins) and a cleaner 114 * task is used to purge stale entries. 115 */ 116 static list_t recent_events_list; 117 static avl_tree_t recent_events_tree; 118 static kmutex_t recent_events_lock; 119 static taskqid_t recent_events_cleaner_tqid; 120 121 /* 122 * Each node is about 128 bytes so 2,000 would consume 1/4 MiB. 123 * 124 * This setting can be changed dynamically and setting it to zero 125 * disables duplicate detection. 126 */ 127 unsigned int zfs_zevent_retain_max = 2000; 128 129 /* 130 * The lifespan for a recent ereport entry. The default of 15 minutes is 131 * intended to outlive the zfs diagnosis engine's threshold of 10 errors 132 * over a period of 10 minutes. 133 */ 134 unsigned int zfs_zevent_retain_expire_secs = 900; 135 136 typedef enum zfs_subclass { 137 ZSC_IO, 138 ZSC_DATA, 139 ZSC_CHECKSUM 140 } zfs_subclass_t; 141 142 typedef struct { 143 /* common criteria */ 144 uint64_t re_pool_guid; 145 uint64_t re_vdev_guid; 146 int re_io_error; 147 uint64_t re_io_size; 148 uint64_t re_io_offset; 149 zfs_subclass_t re_subclass; 150 zio_priority_t re_io_priority; 151 152 /* logical zio criteria (optional) */ 153 zbookmark_phys_t re_io_bookmark; 154 155 /* internal state */ 156 avl_node_t re_tree_link; 157 list_node_t re_list_link; 158 uint64_t re_timestamp; 159 } recent_events_node_t; 160 161 static int 162 recent_events_compare(const void *a, const void *b) 163 { 164 const recent_events_node_t *node1 = a; 165 const recent_events_node_t *node2 = b; 166 int cmp; 167 168 /* 169 * The comparison order here is somewhat arbitrary. 170 * What's important is that if every criteria matches, then it 171 * is a duplicate (i.e. compare returns 0) 172 */ 173 if ((cmp = TREE_CMP(node1->re_subclass, node2->re_subclass)) != 0) 174 return (cmp); 175 if ((cmp = TREE_CMP(node1->re_pool_guid, node2->re_pool_guid)) != 0) 176 return (cmp); 177 if ((cmp = TREE_CMP(node1->re_vdev_guid, node2->re_vdev_guid)) != 0) 178 return (cmp); 179 if ((cmp = TREE_CMP(node1->re_io_error, node2->re_io_error)) != 0) 180 return (cmp); 181 if ((cmp = TREE_CMP(node1->re_io_priority, node2->re_io_priority)) != 0) 182 return (cmp); 183 if ((cmp = TREE_CMP(node1->re_io_size, node2->re_io_size)) != 0) 184 return (cmp); 185 if ((cmp = TREE_CMP(node1->re_io_offset, node2->re_io_offset)) != 0) 186 return (cmp); 187 188 const zbookmark_phys_t *zb1 = &node1->re_io_bookmark; 189 const zbookmark_phys_t *zb2 = &node2->re_io_bookmark; 190 191 if ((cmp = TREE_CMP(zb1->zb_objset, zb2->zb_objset)) != 0) 192 return (cmp); 193 if ((cmp = TREE_CMP(zb1->zb_object, zb2->zb_object)) != 0) 194 return (cmp); 195 if ((cmp = TREE_CMP(zb1->zb_level, zb2->zb_level)) != 0) 196 return (cmp); 197 if ((cmp = TREE_CMP(zb1->zb_blkid, zb2->zb_blkid)) != 0) 198 return (cmp); 199 200 return (0); 201 } 202 203 static void zfs_ereport_schedule_cleaner(void); 204 205 /* 206 * background task to clean stale recent event nodes. 207 */ 208 /*ARGSUSED*/ 209 static void 210 zfs_ereport_cleaner(void *arg) 211 { 212 recent_events_node_t *entry; 213 uint64_t now = gethrtime(); 214 215 /* 216 * purge expired entries 217 */ 218 mutex_enter(&recent_events_lock); 219 while ((entry = list_tail(&recent_events_list)) != NULL) { 220 uint64_t age = NSEC2SEC(now - entry->re_timestamp); 221 if (age <= zfs_zevent_retain_expire_secs) 222 break; 223 224 /* remove expired node */ 225 avl_remove(&recent_events_tree, entry); 226 list_remove(&recent_events_list, entry); 227 kmem_free(entry, sizeof (*entry)); 228 } 229 230 /* Restart the cleaner if more entries remain */ 231 recent_events_cleaner_tqid = 0; 232 if (!list_is_empty(&recent_events_list)) 233 zfs_ereport_schedule_cleaner(); 234 235 mutex_exit(&recent_events_lock); 236 } 237 238 static void 239 zfs_ereport_schedule_cleaner(void) 240 { 241 ASSERT(MUTEX_HELD(&recent_events_lock)); 242 243 uint64_t timeout = SEC2NSEC(zfs_zevent_retain_expire_secs + 1); 244 245 recent_events_cleaner_tqid = taskq_dispatch_delay( 246 system_delay_taskq, zfs_ereport_cleaner, NULL, TQ_SLEEP, 247 ddi_get_lbolt() + NSEC_TO_TICK(timeout)); 248 } 249 250 /* 251 * Clear entries for a given vdev or all vdevs in a pool when vdev == NULL 252 */ 253 void 254 zfs_ereport_clear(spa_t *spa, vdev_t *vd) 255 { 256 uint64_t vdev_guid, pool_guid; 257 int cnt = 0; 258 259 ASSERT(vd != NULL || spa != NULL); 260 if (vd == NULL) { 261 vdev_guid = 0; 262 pool_guid = spa_guid(spa); 263 } else { 264 vdev_guid = vd->vdev_guid; 265 pool_guid = 0; 266 } 267 268 mutex_enter(&recent_events_lock); 269 270 recent_events_node_t *next = list_head(&recent_events_list); 271 while (next != NULL) { 272 recent_events_node_t *entry = next; 273 274 next = list_next(&recent_events_list, next); 275 276 if (entry->re_vdev_guid == vdev_guid || 277 entry->re_pool_guid == pool_guid) { 278 avl_remove(&recent_events_tree, entry); 279 list_remove(&recent_events_list, entry); 280 kmem_free(entry, sizeof (*entry)); 281 cnt++; 282 } 283 } 284 285 mutex_exit(&recent_events_lock); 286 } 287 288 /* 289 * Check if an ereport would be a duplicate of one recently posted. 290 * 291 * An ereport is considered a duplicate if the set of criteria in 292 * recent_events_node_t all match. 293 * 294 * Only FM_EREPORT_ZFS_IO, FM_EREPORT_ZFS_DATA, and FM_EREPORT_ZFS_CHECKSUM 295 * are candidates for duplicate checking. 296 */ 297 static boolean_t 298 zfs_ereport_is_duplicate(const char *subclass, spa_t *spa, vdev_t *vd, 299 const zbookmark_phys_t *zb, zio_t *zio, uint64_t offset, uint64_t size) 300 { 301 recent_events_node_t search = {0}, *entry; 302 303 if (vd == NULL || zio == NULL) 304 return (B_FALSE); 305 306 if (zfs_zevent_retain_max == 0) 307 return (B_FALSE); 308 309 if (strcmp(subclass, FM_EREPORT_ZFS_IO) == 0) 310 search.re_subclass = ZSC_IO; 311 else if (strcmp(subclass, FM_EREPORT_ZFS_DATA) == 0) 312 search.re_subclass = ZSC_DATA; 313 else if (strcmp(subclass, FM_EREPORT_ZFS_CHECKSUM) == 0) 314 search.re_subclass = ZSC_CHECKSUM; 315 else 316 return (B_FALSE); 317 318 search.re_pool_guid = spa_guid(spa); 319 search.re_vdev_guid = vd->vdev_guid; 320 search.re_io_error = zio->io_error; 321 search.re_io_priority = zio->io_priority; 322 /* if size is supplied use it over what's in zio */ 323 if (size) { 324 search.re_io_size = size; 325 search.re_io_offset = offset; 326 } else { 327 search.re_io_size = zio->io_size; 328 search.re_io_offset = zio->io_offset; 329 } 330 331 /* grab optional logical zio criteria */ 332 if (zb != NULL) { 333 search.re_io_bookmark.zb_objset = zb->zb_objset; 334 search.re_io_bookmark.zb_object = zb->zb_object; 335 search.re_io_bookmark.zb_level = zb->zb_level; 336 search.re_io_bookmark.zb_blkid = zb->zb_blkid; 337 } 338 339 uint64_t now = gethrtime(); 340 341 mutex_enter(&recent_events_lock); 342 343 /* check if we have seen this one recently */ 344 entry = avl_find(&recent_events_tree, &search, NULL); 345 if (entry != NULL) { 346 uint64_t age = NSEC2SEC(now - entry->re_timestamp); 347 348 /* 349 * There is still an active cleaner (since we're here). 350 * Reset the last seen time for this duplicate entry 351 * so that its lifespand gets extended. 352 */ 353 list_remove(&recent_events_list, entry); 354 list_insert_head(&recent_events_list, entry); 355 entry->re_timestamp = now; 356 357 zfs_zevent_track_duplicate(); 358 mutex_exit(&recent_events_lock); 359 360 return (age <= zfs_zevent_retain_expire_secs); 361 } 362 363 if (avl_numnodes(&recent_events_tree) >= zfs_zevent_retain_max) { 364 /* recycle oldest node */ 365 entry = list_tail(&recent_events_list); 366 ASSERT(entry != NULL); 367 list_remove(&recent_events_list, entry); 368 avl_remove(&recent_events_tree, entry); 369 } else { 370 entry = kmem_alloc(sizeof (recent_events_node_t), KM_SLEEP); 371 } 372 373 /* record this as a recent ereport */ 374 *entry = search; 375 avl_add(&recent_events_tree, entry); 376 list_insert_head(&recent_events_list, entry); 377 entry->re_timestamp = now; 378 379 /* Start a cleaner if not already scheduled */ 380 if (recent_events_cleaner_tqid == 0) 381 zfs_ereport_schedule_cleaner(); 382 383 mutex_exit(&recent_events_lock); 384 return (B_FALSE); 385 } 386 387 void 388 zfs_zevent_post_cb(nvlist_t *nvl, nvlist_t *detector) 389 { 390 if (nvl) 391 fm_nvlist_destroy(nvl, FM_NVA_FREE); 392 393 if (detector) 394 fm_nvlist_destroy(detector, FM_NVA_FREE); 395 } 396 397 /* 398 * We want to rate limit ZIO delay, deadman, and checksum events so as to not 399 * flood zevent consumers when a disk is acting up. 400 * 401 * Returns 1 if we're ratelimiting, 0 if not. 402 */ 403 static int 404 zfs_is_ratelimiting_event(const char *subclass, vdev_t *vd) 405 { 406 int rc = 0; 407 /* 408 * zfs_ratelimit() returns 1 if we're *not* ratelimiting and 0 if we 409 * are. Invert it to get our return value. 410 */ 411 if (strcmp(subclass, FM_EREPORT_ZFS_DELAY) == 0) { 412 rc = !zfs_ratelimit(&vd->vdev_delay_rl); 413 } else if (strcmp(subclass, FM_EREPORT_ZFS_DEADMAN) == 0) { 414 rc = !zfs_ratelimit(&vd->vdev_deadman_rl); 415 } else if (strcmp(subclass, FM_EREPORT_ZFS_CHECKSUM) == 0) { 416 rc = !zfs_ratelimit(&vd->vdev_checksum_rl); 417 } 418 419 if (rc) { 420 /* We're rate limiting */ 421 fm_erpt_dropped_increment(); 422 } 423 424 return (rc); 425 } 426 427 /* 428 * Return B_TRUE if the event actually posted, B_FALSE if not. 429 */ 430 static boolean_t 431 zfs_ereport_start(nvlist_t **ereport_out, nvlist_t **detector_out, 432 const char *subclass, spa_t *spa, vdev_t *vd, const zbookmark_phys_t *zb, 433 zio_t *zio, uint64_t stateoroffset, uint64_t size) 434 { 435 nvlist_t *ereport, *detector; 436 437 uint64_t ena; 438 char class[64]; 439 440 if ((ereport = fm_nvlist_create(NULL)) == NULL) 441 return (B_FALSE); 442 443 if ((detector = fm_nvlist_create(NULL)) == NULL) { 444 fm_nvlist_destroy(ereport, FM_NVA_FREE); 445 return (B_FALSE); 446 } 447 448 /* 449 * Serialize ereport generation 450 */ 451 mutex_enter(&spa->spa_errlist_lock); 452 453 /* 454 * Determine the ENA to use for this event. If we are in a loading 455 * state, use a SPA-wide ENA. Otherwise, if we are in an I/O state, use 456 * a root zio-wide ENA. Otherwise, simply use a unique ENA. 457 */ 458 if (spa_load_state(spa) != SPA_LOAD_NONE) { 459 if (spa->spa_ena == 0) 460 spa->spa_ena = fm_ena_generate(0, FM_ENA_FMT1); 461 ena = spa->spa_ena; 462 } else if (zio != NULL && zio->io_logical != NULL) { 463 if (zio->io_logical->io_ena == 0) 464 zio->io_logical->io_ena = 465 fm_ena_generate(0, FM_ENA_FMT1); 466 ena = zio->io_logical->io_ena; 467 } else { 468 ena = fm_ena_generate(0, FM_ENA_FMT1); 469 } 470 471 /* 472 * Construct the full class, detector, and other standard FMA fields. 473 */ 474 (void) snprintf(class, sizeof (class), "%s.%s", 475 ZFS_ERROR_CLASS, subclass); 476 477 fm_fmri_zfs_set(detector, FM_ZFS_SCHEME_VERSION, spa_guid(spa), 478 vd != NULL ? vd->vdev_guid : 0); 479 480 fm_ereport_set(ereport, FM_EREPORT_VERSION, class, ena, detector, NULL); 481 482 /* 483 * Construct the per-ereport payload, depending on which parameters are 484 * passed in. 485 */ 486 487 /* 488 * Generic payload members common to all ereports. 489 */ 490 fm_payload_set(ereport, 491 FM_EREPORT_PAYLOAD_ZFS_POOL, DATA_TYPE_STRING, spa_name(spa), 492 FM_EREPORT_PAYLOAD_ZFS_POOL_GUID, DATA_TYPE_UINT64, spa_guid(spa), 493 FM_EREPORT_PAYLOAD_ZFS_POOL_STATE, DATA_TYPE_UINT64, 494 (uint64_t)spa_state(spa), 495 FM_EREPORT_PAYLOAD_ZFS_POOL_CONTEXT, DATA_TYPE_INT32, 496 (int32_t)spa_load_state(spa), NULL); 497 498 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_POOL_FAILMODE, 499 DATA_TYPE_STRING, 500 spa_get_failmode(spa) == ZIO_FAILURE_MODE_WAIT ? 501 FM_EREPORT_FAILMODE_WAIT : 502 spa_get_failmode(spa) == ZIO_FAILURE_MODE_CONTINUE ? 503 FM_EREPORT_FAILMODE_CONTINUE : FM_EREPORT_FAILMODE_PANIC, 504 NULL); 505 506 if (vd != NULL) { 507 vdev_t *pvd = vd->vdev_parent; 508 vdev_queue_t *vq = &vd->vdev_queue; 509 vdev_stat_t *vs = &vd->vdev_stat; 510 vdev_t *spare_vd; 511 uint64_t *spare_guids; 512 char **spare_paths; 513 int i, spare_count; 514 515 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID, 516 DATA_TYPE_UINT64, vd->vdev_guid, 517 FM_EREPORT_PAYLOAD_ZFS_VDEV_TYPE, 518 DATA_TYPE_STRING, vd->vdev_ops->vdev_op_type, NULL); 519 if (vd->vdev_path != NULL) 520 fm_payload_set(ereport, 521 FM_EREPORT_PAYLOAD_ZFS_VDEV_PATH, 522 DATA_TYPE_STRING, vd->vdev_path, NULL); 523 if (vd->vdev_devid != NULL) 524 fm_payload_set(ereport, 525 FM_EREPORT_PAYLOAD_ZFS_VDEV_DEVID, 526 DATA_TYPE_STRING, vd->vdev_devid, NULL); 527 if (vd->vdev_fru != NULL) 528 fm_payload_set(ereport, 529 FM_EREPORT_PAYLOAD_ZFS_VDEV_FRU, 530 DATA_TYPE_STRING, vd->vdev_fru, NULL); 531 if (vd->vdev_enc_sysfs_path != NULL) 532 fm_payload_set(ereport, 533 FM_EREPORT_PAYLOAD_ZFS_VDEV_ENC_SYSFS_PATH, 534 DATA_TYPE_STRING, vd->vdev_enc_sysfs_path, NULL); 535 if (vd->vdev_ashift) 536 fm_payload_set(ereport, 537 FM_EREPORT_PAYLOAD_ZFS_VDEV_ASHIFT, 538 DATA_TYPE_UINT64, vd->vdev_ashift, NULL); 539 540 if (vq != NULL) { 541 fm_payload_set(ereport, 542 FM_EREPORT_PAYLOAD_ZFS_VDEV_COMP_TS, 543 DATA_TYPE_UINT64, vq->vq_io_complete_ts, NULL); 544 fm_payload_set(ereport, 545 FM_EREPORT_PAYLOAD_ZFS_VDEV_DELTA_TS, 546 DATA_TYPE_UINT64, vq->vq_io_delta_ts, NULL); 547 } 548 549 if (vs != NULL) { 550 fm_payload_set(ereport, 551 FM_EREPORT_PAYLOAD_ZFS_VDEV_READ_ERRORS, 552 DATA_TYPE_UINT64, vs->vs_read_errors, 553 FM_EREPORT_PAYLOAD_ZFS_VDEV_WRITE_ERRORS, 554 DATA_TYPE_UINT64, vs->vs_write_errors, 555 FM_EREPORT_PAYLOAD_ZFS_VDEV_CKSUM_ERRORS, 556 DATA_TYPE_UINT64, vs->vs_checksum_errors, 557 FM_EREPORT_PAYLOAD_ZFS_VDEV_DELAYS, 558 DATA_TYPE_UINT64, vs->vs_slow_ios, 559 NULL); 560 } 561 562 if (pvd != NULL) { 563 fm_payload_set(ereport, 564 FM_EREPORT_PAYLOAD_ZFS_PARENT_GUID, 565 DATA_TYPE_UINT64, pvd->vdev_guid, 566 FM_EREPORT_PAYLOAD_ZFS_PARENT_TYPE, 567 DATA_TYPE_STRING, pvd->vdev_ops->vdev_op_type, 568 NULL); 569 if (pvd->vdev_path) 570 fm_payload_set(ereport, 571 FM_EREPORT_PAYLOAD_ZFS_PARENT_PATH, 572 DATA_TYPE_STRING, pvd->vdev_path, NULL); 573 if (pvd->vdev_devid) 574 fm_payload_set(ereport, 575 FM_EREPORT_PAYLOAD_ZFS_PARENT_DEVID, 576 DATA_TYPE_STRING, pvd->vdev_devid, NULL); 577 } 578 579 spare_count = spa->spa_spares.sav_count; 580 spare_paths = kmem_zalloc(sizeof (char *) * spare_count, 581 KM_SLEEP); 582 spare_guids = kmem_zalloc(sizeof (uint64_t) * spare_count, 583 KM_SLEEP); 584 585 for (i = 0; i < spare_count; i++) { 586 spare_vd = spa->spa_spares.sav_vdevs[i]; 587 if (spare_vd) { 588 spare_paths[i] = spare_vd->vdev_path; 589 spare_guids[i] = spare_vd->vdev_guid; 590 } 591 } 592 593 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_VDEV_SPARE_PATHS, 594 DATA_TYPE_STRING_ARRAY, spare_count, spare_paths, 595 FM_EREPORT_PAYLOAD_ZFS_VDEV_SPARE_GUIDS, 596 DATA_TYPE_UINT64_ARRAY, spare_count, spare_guids, NULL); 597 598 kmem_free(spare_guids, sizeof (uint64_t) * spare_count); 599 kmem_free(spare_paths, sizeof (char *) * spare_count); 600 } 601 602 if (zio != NULL) { 603 /* 604 * Payload common to all I/Os. 605 */ 606 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_ERR, 607 DATA_TYPE_INT32, zio->io_error, NULL); 608 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_FLAGS, 609 DATA_TYPE_INT32, zio->io_flags, NULL); 610 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_STAGE, 611 DATA_TYPE_UINT32, zio->io_stage, NULL); 612 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_PIPELINE, 613 DATA_TYPE_UINT32, zio->io_pipeline, NULL); 614 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_DELAY, 615 DATA_TYPE_UINT64, zio->io_delay, NULL); 616 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_TIMESTAMP, 617 DATA_TYPE_UINT64, zio->io_timestamp, NULL); 618 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_DELTA, 619 DATA_TYPE_UINT64, zio->io_delta, NULL); 620 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_PRIORITY, 621 DATA_TYPE_UINT32, zio->io_priority, NULL); 622 623 /* 624 * If the 'size' parameter is non-zero, it indicates this is a 625 * RAID-Z or other I/O where the physical offset and length are 626 * provided for us, instead of within the zio_t. 627 */ 628 if (vd != NULL) { 629 if (size) 630 fm_payload_set(ereport, 631 FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET, 632 DATA_TYPE_UINT64, stateoroffset, 633 FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE, 634 DATA_TYPE_UINT64, size, NULL); 635 else 636 fm_payload_set(ereport, 637 FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET, 638 DATA_TYPE_UINT64, zio->io_offset, 639 FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE, 640 DATA_TYPE_UINT64, zio->io_size, NULL); 641 } 642 } else if (vd != NULL) { 643 /* 644 * If we have a vdev but no zio, this is a device fault, and the 645 * 'stateoroffset' parameter indicates the previous state of the 646 * vdev. 647 */ 648 fm_payload_set(ereport, 649 FM_EREPORT_PAYLOAD_ZFS_PREV_STATE, 650 DATA_TYPE_UINT64, stateoroffset, NULL); 651 } 652 653 /* 654 * Payload for I/Os with corresponding logical information. 655 */ 656 if (zb != NULL && (zio == NULL || zio->io_logical != NULL)) { 657 fm_payload_set(ereport, 658 FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJSET, 659 DATA_TYPE_UINT64, zb->zb_objset, 660 FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJECT, 661 DATA_TYPE_UINT64, zb->zb_object, 662 FM_EREPORT_PAYLOAD_ZFS_ZIO_LEVEL, 663 DATA_TYPE_INT64, zb->zb_level, 664 FM_EREPORT_PAYLOAD_ZFS_ZIO_BLKID, 665 DATA_TYPE_UINT64, zb->zb_blkid, NULL); 666 } 667 668 mutex_exit(&spa->spa_errlist_lock); 669 670 *ereport_out = ereport; 671 *detector_out = detector; 672 return (B_TRUE); 673 } 674 675 /* if it's <= 128 bytes, save the corruption directly */ 676 #define ZFM_MAX_INLINE (128 / sizeof (uint64_t)) 677 678 #define MAX_RANGES 16 679 680 typedef struct zfs_ecksum_info { 681 /* histograms of set and cleared bits by bit number in a 64-bit word */ 682 uint32_t zei_histogram_set[sizeof (uint64_t) * NBBY]; 683 uint32_t zei_histogram_cleared[sizeof (uint64_t) * NBBY]; 684 685 /* inline arrays of bits set and cleared. */ 686 uint64_t zei_bits_set[ZFM_MAX_INLINE]; 687 uint64_t zei_bits_cleared[ZFM_MAX_INLINE]; 688 689 /* 690 * for each range, the number of bits set and cleared. The Hamming 691 * distance between the good and bad buffers is the sum of them all. 692 */ 693 uint32_t zei_range_sets[MAX_RANGES]; 694 uint32_t zei_range_clears[MAX_RANGES]; 695 696 struct zei_ranges { 697 uint32_t zr_start; 698 uint32_t zr_end; 699 } zei_ranges[MAX_RANGES]; 700 701 size_t zei_range_count; 702 uint32_t zei_mingap; 703 uint32_t zei_allowed_mingap; 704 705 } zfs_ecksum_info_t; 706 707 static void 708 update_histogram(uint64_t value_arg, uint32_t *hist, uint32_t *count) 709 { 710 size_t i; 711 size_t bits = 0; 712 uint64_t value = BE_64(value_arg); 713 714 /* We store the bits in big-endian (largest-first) order */ 715 for (i = 0; i < 64; i++) { 716 if (value & (1ull << i)) { 717 hist[63 - i]++; 718 ++bits; 719 } 720 } 721 /* update the count of bits changed */ 722 *count += bits; 723 } 724 725 /* 726 * We've now filled up the range array, and need to increase "mingap" and 727 * shrink the range list accordingly. zei_mingap is always the smallest 728 * distance between array entries, so we set the new_allowed_gap to be 729 * one greater than that. We then go through the list, joining together 730 * any ranges which are closer than the new_allowed_gap. 731 * 732 * By construction, there will be at least one. We also update zei_mingap 733 * to the new smallest gap, to prepare for our next invocation. 734 */ 735 static void 736 zei_shrink_ranges(zfs_ecksum_info_t *eip) 737 { 738 uint32_t mingap = UINT32_MAX; 739 uint32_t new_allowed_gap = eip->zei_mingap + 1; 740 741 size_t idx, output; 742 size_t max = eip->zei_range_count; 743 744 struct zei_ranges *r = eip->zei_ranges; 745 746 ASSERT3U(eip->zei_range_count, >, 0); 747 ASSERT3U(eip->zei_range_count, <=, MAX_RANGES); 748 749 output = idx = 0; 750 while (idx < max - 1) { 751 uint32_t start = r[idx].zr_start; 752 uint32_t end = r[idx].zr_end; 753 754 while (idx < max - 1) { 755 idx++; 756 757 uint32_t nstart = r[idx].zr_start; 758 uint32_t nend = r[idx].zr_end; 759 760 uint32_t gap = nstart - end; 761 if (gap < new_allowed_gap) { 762 end = nend; 763 continue; 764 } 765 if (gap < mingap) 766 mingap = gap; 767 break; 768 } 769 r[output].zr_start = start; 770 r[output].zr_end = end; 771 output++; 772 } 773 ASSERT3U(output, <, eip->zei_range_count); 774 eip->zei_range_count = output; 775 eip->zei_mingap = mingap; 776 eip->zei_allowed_mingap = new_allowed_gap; 777 } 778 779 static void 780 zei_add_range(zfs_ecksum_info_t *eip, int start, int end) 781 { 782 struct zei_ranges *r = eip->zei_ranges; 783 size_t count = eip->zei_range_count; 784 785 if (count >= MAX_RANGES) { 786 zei_shrink_ranges(eip); 787 count = eip->zei_range_count; 788 } 789 if (count == 0) { 790 eip->zei_mingap = UINT32_MAX; 791 eip->zei_allowed_mingap = 1; 792 } else { 793 int gap = start - r[count - 1].zr_end; 794 795 if (gap < eip->zei_allowed_mingap) { 796 r[count - 1].zr_end = end; 797 return; 798 } 799 if (gap < eip->zei_mingap) 800 eip->zei_mingap = gap; 801 } 802 r[count].zr_start = start; 803 r[count].zr_end = end; 804 eip->zei_range_count++; 805 } 806 807 static size_t 808 zei_range_total_size(zfs_ecksum_info_t *eip) 809 { 810 struct zei_ranges *r = eip->zei_ranges; 811 size_t count = eip->zei_range_count; 812 size_t result = 0; 813 size_t idx; 814 815 for (idx = 0; idx < count; idx++) 816 result += (r[idx].zr_end - r[idx].zr_start); 817 818 return (result); 819 } 820 821 static zfs_ecksum_info_t * 822 annotate_ecksum(nvlist_t *ereport, zio_bad_cksum_t *info, 823 const abd_t *goodabd, const abd_t *badabd, size_t size, 824 boolean_t drop_if_identical) 825 { 826 const uint64_t *good; 827 const uint64_t *bad; 828 829 uint64_t allset = 0; 830 uint64_t allcleared = 0; 831 832 size_t nui64s = size / sizeof (uint64_t); 833 834 size_t inline_size; 835 int no_inline = 0; 836 size_t idx; 837 size_t range; 838 839 size_t offset = 0; 840 ssize_t start = -1; 841 842 zfs_ecksum_info_t *eip = kmem_zalloc(sizeof (*eip), KM_SLEEP); 843 844 /* don't do any annotation for injected checksum errors */ 845 if (info != NULL && info->zbc_injected) 846 return (eip); 847 848 if (info != NULL && info->zbc_has_cksum) { 849 fm_payload_set(ereport, 850 FM_EREPORT_PAYLOAD_ZFS_CKSUM_EXPECTED, 851 DATA_TYPE_UINT64_ARRAY, 852 sizeof (info->zbc_expected) / sizeof (uint64_t), 853 (uint64_t *)&info->zbc_expected, 854 FM_EREPORT_PAYLOAD_ZFS_CKSUM_ACTUAL, 855 DATA_TYPE_UINT64_ARRAY, 856 sizeof (info->zbc_actual) / sizeof (uint64_t), 857 (uint64_t *)&info->zbc_actual, 858 FM_EREPORT_PAYLOAD_ZFS_CKSUM_ALGO, 859 DATA_TYPE_STRING, 860 info->zbc_checksum_name, 861 NULL); 862 863 if (info->zbc_byteswapped) { 864 fm_payload_set(ereport, 865 FM_EREPORT_PAYLOAD_ZFS_CKSUM_BYTESWAP, 866 DATA_TYPE_BOOLEAN, 1, 867 NULL); 868 } 869 } 870 871 if (badabd == NULL || goodabd == NULL) 872 return (eip); 873 874 ASSERT3U(nui64s, <=, UINT32_MAX); 875 ASSERT3U(size, ==, nui64s * sizeof (uint64_t)); 876 ASSERT3U(size, <=, SPA_MAXBLOCKSIZE); 877 ASSERT3U(size, <=, UINT32_MAX); 878 879 good = (const uint64_t *) abd_borrow_buf_copy((abd_t *)goodabd, size); 880 bad = (const uint64_t *) abd_borrow_buf_copy((abd_t *)badabd, size); 881 882 /* build up the range list by comparing the two buffers. */ 883 for (idx = 0; idx < nui64s; idx++) { 884 if (good[idx] == bad[idx]) { 885 if (start == -1) 886 continue; 887 888 zei_add_range(eip, start, idx); 889 start = -1; 890 } else { 891 if (start != -1) 892 continue; 893 894 start = idx; 895 } 896 } 897 if (start != -1) 898 zei_add_range(eip, start, idx); 899 900 /* See if it will fit in our inline buffers */ 901 inline_size = zei_range_total_size(eip); 902 if (inline_size > ZFM_MAX_INLINE) 903 no_inline = 1; 904 905 /* 906 * If there is no change and we want to drop if the buffers are 907 * identical, do so. 908 */ 909 if (inline_size == 0 && drop_if_identical) { 910 kmem_free(eip, sizeof (*eip)); 911 abd_return_buf((abd_t *)goodabd, (void *)good, size); 912 abd_return_buf((abd_t *)badabd, (void *)bad, size); 913 return (NULL); 914 } 915 916 /* 917 * Now walk through the ranges, filling in the details of the 918 * differences. Also convert our uint64_t-array offsets to byte 919 * offsets. 920 */ 921 for (range = 0; range < eip->zei_range_count; range++) { 922 size_t start = eip->zei_ranges[range].zr_start; 923 size_t end = eip->zei_ranges[range].zr_end; 924 925 for (idx = start; idx < end; idx++) { 926 uint64_t set, cleared; 927 928 // bits set in bad, but not in good 929 set = ((~good[idx]) & bad[idx]); 930 // bits set in good, but not in bad 931 cleared = (good[idx] & (~bad[idx])); 932 933 allset |= set; 934 allcleared |= cleared; 935 936 if (!no_inline) { 937 ASSERT3U(offset, <, inline_size); 938 eip->zei_bits_set[offset] = set; 939 eip->zei_bits_cleared[offset] = cleared; 940 offset++; 941 } 942 943 update_histogram(set, eip->zei_histogram_set, 944 &eip->zei_range_sets[range]); 945 update_histogram(cleared, eip->zei_histogram_cleared, 946 &eip->zei_range_clears[range]); 947 } 948 949 /* convert to byte offsets */ 950 eip->zei_ranges[range].zr_start *= sizeof (uint64_t); 951 eip->zei_ranges[range].zr_end *= sizeof (uint64_t); 952 } 953 954 abd_return_buf((abd_t *)goodabd, (void *)good, size); 955 abd_return_buf((abd_t *)badabd, (void *)bad, size); 956 957 eip->zei_allowed_mingap *= sizeof (uint64_t); 958 inline_size *= sizeof (uint64_t); 959 960 /* fill in ereport */ 961 fm_payload_set(ereport, 962 FM_EREPORT_PAYLOAD_ZFS_BAD_OFFSET_RANGES, 963 DATA_TYPE_UINT32_ARRAY, 2 * eip->zei_range_count, 964 (uint32_t *)eip->zei_ranges, 965 FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_MIN_GAP, 966 DATA_TYPE_UINT32, eip->zei_allowed_mingap, 967 FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_SETS, 968 DATA_TYPE_UINT32_ARRAY, eip->zei_range_count, eip->zei_range_sets, 969 FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_CLEARS, 970 DATA_TYPE_UINT32_ARRAY, eip->zei_range_count, eip->zei_range_clears, 971 NULL); 972 973 if (!no_inline) { 974 fm_payload_set(ereport, 975 FM_EREPORT_PAYLOAD_ZFS_BAD_SET_BITS, 976 DATA_TYPE_UINT8_ARRAY, 977 inline_size, (uint8_t *)eip->zei_bits_set, 978 FM_EREPORT_PAYLOAD_ZFS_BAD_CLEARED_BITS, 979 DATA_TYPE_UINT8_ARRAY, 980 inline_size, (uint8_t *)eip->zei_bits_cleared, 981 NULL); 982 } else { 983 fm_payload_set(ereport, 984 FM_EREPORT_PAYLOAD_ZFS_BAD_SET_HISTOGRAM, 985 DATA_TYPE_UINT32_ARRAY, 986 NBBY * sizeof (uint64_t), eip->zei_histogram_set, 987 FM_EREPORT_PAYLOAD_ZFS_BAD_CLEARED_HISTOGRAM, 988 DATA_TYPE_UINT32_ARRAY, 989 NBBY * sizeof (uint64_t), eip->zei_histogram_cleared, 990 NULL); 991 } 992 return (eip); 993 } 994 #else 995 /*ARGSUSED*/ 996 void 997 zfs_ereport_clear(spa_t *spa, vdev_t *vd) 998 { 999 } 1000 #endif 1001 1002 /* 1003 * Make sure our event is still valid for the given zio/vdev/pool. For example, 1004 * we don't want to keep logging events for a faulted or missing vdev. 1005 */ 1006 boolean_t 1007 zfs_ereport_is_valid(const char *subclass, spa_t *spa, vdev_t *vd, zio_t *zio) 1008 { 1009 #ifdef _KERNEL 1010 /* 1011 * If we are doing a spa_tryimport() or in recovery mode, 1012 * ignore errors. 1013 */ 1014 if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT || 1015 spa_load_state(spa) == SPA_LOAD_RECOVER) 1016 return (B_FALSE); 1017 1018 /* 1019 * If we are in the middle of opening a pool, and the previous attempt 1020 * failed, don't bother logging any new ereports - we're just going to 1021 * get the same diagnosis anyway. 1022 */ 1023 if (spa_load_state(spa) != SPA_LOAD_NONE && 1024 spa->spa_last_open_failed) 1025 return (B_FALSE); 1026 1027 if (zio != NULL) { 1028 /* 1029 * If this is not a read or write zio, ignore the error. This 1030 * can occur if the DKIOCFLUSHWRITECACHE ioctl fails. 1031 */ 1032 if (zio->io_type != ZIO_TYPE_READ && 1033 zio->io_type != ZIO_TYPE_WRITE) 1034 return (B_FALSE); 1035 1036 if (vd != NULL) { 1037 /* 1038 * If the vdev has already been marked as failing due 1039 * to a failed probe, then ignore any subsequent I/O 1040 * errors, as the DE will automatically fault the vdev 1041 * on the first such failure. This also catches cases 1042 * where vdev_remove_wanted is set and the device has 1043 * not yet been asynchronously placed into the REMOVED 1044 * state. 1045 */ 1046 if (zio->io_vd == vd && !vdev_accessible(vd, zio)) 1047 return (B_FALSE); 1048 1049 /* 1050 * Ignore checksum errors for reads from DTL regions of 1051 * leaf vdevs. 1052 */ 1053 if (zio->io_type == ZIO_TYPE_READ && 1054 zio->io_error == ECKSUM && 1055 vd->vdev_ops->vdev_op_leaf && 1056 vdev_dtl_contains(vd, DTL_MISSING, zio->io_txg, 1)) 1057 return (B_FALSE); 1058 } 1059 } 1060 1061 /* 1062 * For probe failure, we want to avoid posting ereports if we've 1063 * already removed the device in the meantime. 1064 */ 1065 if (vd != NULL && 1066 strcmp(subclass, FM_EREPORT_ZFS_PROBE_FAILURE) == 0 && 1067 (vd->vdev_remove_wanted || vd->vdev_state == VDEV_STATE_REMOVED)) 1068 return (B_FALSE); 1069 1070 /* Ignore bogus delay events (like from ioctls or unqueued IOs) */ 1071 if ((strcmp(subclass, FM_EREPORT_ZFS_DELAY) == 0) && 1072 (zio != NULL) && (!zio->io_timestamp)) { 1073 return (B_FALSE); 1074 } 1075 #endif 1076 return (B_TRUE); 1077 } 1078 1079 /* 1080 * Post an ereport for the given subclass 1081 * 1082 * Returns 1083 * - 0 if an event was posted 1084 * - EINVAL if there was a problem posting event 1085 * - EBUSY if the event was rate limited 1086 * - EALREADY if the event was already posted (duplicate) 1087 */ 1088 int 1089 zfs_ereport_post(const char *subclass, spa_t *spa, vdev_t *vd, 1090 const zbookmark_phys_t *zb, zio_t *zio, uint64_t state) 1091 { 1092 int rc = 0; 1093 #ifdef _KERNEL 1094 nvlist_t *ereport = NULL; 1095 nvlist_t *detector = NULL; 1096 1097 if (!zfs_ereport_is_valid(subclass, spa, vd, zio)) 1098 return (EINVAL); 1099 1100 if (zfs_ereport_is_duplicate(subclass, spa, vd, zb, zio, 0, 0)) 1101 return (SET_ERROR(EALREADY)); 1102 1103 if (zfs_is_ratelimiting_event(subclass, vd)) 1104 return (SET_ERROR(EBUSY)); 1105 1106 if (!zfs_ereport_start(&ereport, &detector, subclass, spa, vd, 1107 zb, zio, state, 0)) 1108 return (SET_ERROR(EINVAL)); /* couldn't post event */ 1109 1110 if (ereport == NULL) 1111 return (SET_ERROR(EINVAL)); 1112 1113 /* Cleanup is handled by the callback function */ 1114 rc = zfs_zevent_post(ereport, detector, zfs_zevent_post_cb); 1115 #endif 1116 return (rc); 1117 } 1118 1119 /* 1120 * Prepare a checksum ereport 1121 * 1122 * Returns 1123 * - 0 if an event was posted 1124 * - EINVAL if there was a problem posting event 1125 * - EBUSY if the event was rate limited 1126 * - EALREADY if the event was already posted (duplicate) 1127 */ 1128 int 1129 zfs_ereport_start_checksum(spa_t *spa, vdev_t *vd, const zbookmark_phys_t *zb, 1130 struct zio *zio, uint64_t offset, uint64_t length, zio_bad_cksum_t *info) 1131 { 1132 zio_cksum_report_t *report; 1133 1134 #ifdef _KERNEL 1135 if (!zfs_ereport_is_valid(FM_EREPORT_ZFS_CHECKSUM, spa, vd, zio)) 1136 return (SET_ERROR(EINVAL)); 1137 1138 if (zfs_ereport_is_duplicate(FM_EREPORT_ZFS_CHECKSUM, spa, vd, zb, zio, 1139 offset, length)) 1140 return (SET_ERROR(EALREADY)); 1141 1142 if (zfs_is_ratelimiting_event(FM_EREPORT_ZFS_CHECKSUM, vd)) 1143 return (SET_ERROR(EBUSY)); 1144 #endif 1145 1146 report = kmem_zalloc(sizeof (*report), KM_SLEEP); 1147 1148 zio_vsd_default_cksum_report(zio, report); 1149 1150 /* copy the checksum failure information if it was provided */ 1151 if (info != NULL) { 1152 report->zcr_ckinfo = kmem_zalloc(sizeof (*info), KM_SLEEP); 1153 bcopy(info, report->zcr_ckinfo, sizeof (*info)); 1154 } 1155 1156 report->zcr_sector = 1ULL << vd->vdev_top->vdev_ashift; 1157 report->zcr_align = 1158 vdev_psize_to_asize(vd->vdev_top, report->zcr_sector); 1159 report->zcr_length = length; 1160 1161 #ifdef _KERNEL 1162 (void) zfs_ereport_start(&report->zcr_ereport, &report->zcr_detector, 1163 FM_EREPORT_ZFS_CHECKSUM, spa, vd, zb, zio, offset, length); 1164 1165 if (report->zcr_ereport == NULL) { 1166 zfs_ereport_free_checksum(report); 1167 return (0); 1168 } 1169 #endif 1170 1171 mutex_enter(&spa->spa_errlist_lock); 1172 report->zcr_next = zio->io_logical->io_cksum_report; 1173 zio->io_logical->io_cksum_report = report; 1174 mutex_exit(&spa->spa_errlist_lock); 1175 return (0); 1176 } 1177 1178 void 1179 zfs_ereport_finish_checksum(zio_cksum_report_t *report, const abd_t *good_data, 1180 const abd_t *bad_data, boolean_t drop_if_identical) 1181 { 1182 #ifdef _KERNEL 1183 zfs_ecksum_info_t *info; 1184 1185 info = annotate_ecksum(report->zcr_ereport, report->zcr_ckinfo, 1186 good_data, bad_data, report->zcr_length, drop_if_identical); 1187 if (info != NULL) 1188 zfs_zevent_post(report->zcr_ereport, 1189 report->zcr_detector, zfs_zevent_post_cb); 1190 else 1191 zfs_zevent_post_cb(report->zcr_ereport, report->zcr_detector); 1192 1193 report->zcr_ereport = report->zcr_detector = NULL; 1194 if (info != NULL) 1195 kmem_free(info, sizeof (*info)); 1196 #endif 1197 } 1198 1199 void 1200 zfs_ereport_free_checksum(zio_cksum_report_t *rpt) 1201 { 1202 #ifdef _KERNEL 1203 if (rpt->zcr_ereport != NULL) { 1204 fm_nvlist_destroy(rpt->zcr_ereport, 1205 FM_NVA_FREE); 1206 fm_nvlist_destroy(rpt->zcr_detector, 1207 FM_NVA_FREE); 1208 } 1209 #endif 1210 rpt->zcr_free(rpt->zcr_cbdata, rpt->zcr_cbinfo); 1211 1212 if (rpt->zcr_ckinfo != NULL) 1213 kmem_free(rpt->zcr_ckinfo, sizeof (*rpt->zcr_ckinfo)); 1214 1215 kmem_free(rpt, sizeof (*rpt)); 1216 } 1217 1218 /* 1219 * Post a checksum ereport 1220 * 1221 * Returns 1222 * - 0 if an event was posted 1223 * - EINVAL if there was a problem posting event 1224 * - EBUSY if the event was rate limited 1225 * - EALREADY if the event was already posted (duplicate) 1226 */ 1227 int 1228 zfs_ereport_post_checksum(spa_t *spa, vdev_t *vd, const zbookmark_phys_t *zb, 1229 struct zio *zio, uint64_t offset, uint64_t length, 1230 const abd_t *good_data, const abd_t *bad_data, zio_bad_cksum_t *zbc) 1231 { 1232 int rc = 0; 1233 #ifdef _KERNEL 1234 nvlist_t *ereport = NULL; 1235 nvlist_t *detector = NULL; 1236 zfs_ecksum_info_t *info; 1237 1238 if (!zfs_ereport_is_valid(FM_EREPORT_ZFS_CHECKSUM, spa, vd, zio)) 1239 return (SET_ERROR(EINVAL)); 1240 1241 if (zfs_ereport_is_duplicate(FM_EREPORT_ZFS_CHECKSUM, spa, vd, zb, zio, 1242 offset, length)) 1243 return (SET_ERROR(EALREADY)); 1244 1245 if (zfs_is_ratelimiting_event(FM_EREPORT_ZFS_CHECKSUM, vd)) 1246 return (SET_ERROR(EBUSY)); 1247 1248 if (!zfs_ereport_start(&ereport, &detector, FM_EREPORT_ZFS_CHECKSUM, 1249 spa, vd, zb, zio, offset, length) || (ereport == NULL)) { 1250 return (SET_ERROR(EINVAL)); 1251 } 1252 1253 info = annotate_ecksum(ereport, zbc, good_data, bad_data, length, 1254 B_FALSE); 1255 1256 if (info != NULL) { 1257 rc = zfs_zevent_post(ereport, detector, zfs_zevent_post_cb); 1258 kmem_free(info, sizeof (*info)); 1259 } 1260 #endif 1261 return (rc); 1262 } 1263 1264 /* 1265 * The 'sysevent.fs.zfs.*' events are signals posted to notify user space of 1266 * change in the pool. All sysevents are listed in sys/sysevent/eventdefs.h 1267 * and are designed to be consumed by the ZFS Event Daemon (ZED). For 1268 * additional details refer to the zed(8) man page. 1269 */ 1270 nvlist_t * 1271 zfs_event_create(spa_t *spa, vdev_t *vd, const char *type, const char *name, 1272 nvlist_t *aux) 1273 { 1274 nvlist_t *resource = NULL; 1275 #ifdef _KERNEL 1276 char class[64]; 1277 1278 if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT) 1279 return (NULL); 1280 1281 if ((resource = fm_nvlist_create(NULL)) == NULL) 1282 return (NULL); 1283 1284 (void) snprintf(class, sizeof (class), "%s.%s.%s", type, 1285 ZFS_ERROR_CLASS, name); 1286 VERIFY0(nvlist_add_uint8(resource, FM_VERSION, FM_RSRC_VERSION)); 1287 VERIFY0(nvlist_add_string(resource, FM_CLASS, class)); 1288 VERIFY0(nvlist_add_string(resource, 1289 FM_EREPORT_PAYLOAD_ZFS_POOL, spa_name(spa))); 1290 VERIFY0(nvlist_add_uint64(resource, 1291 FM_EREPORT_PAYLOAD_ZFS_POOL_GUID, spa_guid(spa))); 1292 VERIFY0(nvlist_add_uint64(resource, 1293 FM_EREPORT_PAYLOAD_ZFS_POOL_STATE, spa_state(spa))); 1294 VERIFY0(nvlist_add_int32(resource, 1295 FM_EREPORT_PAYLOAD_ZFS_POOL_CONTEXT, spa_load_state(spa))); 1296 1297 if (vd) { 1298 VERIFY0(nvlist_add_uint64(resource, 1299 FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID, vd->vdev_guid)); 1300 VERIFY0(nvlist_add_uint64(resource, 1301 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE, vd->vdev_state)); 1302 if (vd->vdev_path != NULL) 1303 VERIFY0(nvlist_add_string(resource, 1304 FM_EREPORT_PAYLOAD_ZFS_VDEV_PATH, vd->vdev_path)); 1305 if (vd->vdev_devid != NULL) 1306 VERIFY0(nvlist_add_string(resource, 1307 FM_EREPORT_PAYLOAD_ZFS_VDEV_DEVID, vd->vdev_devid)); 1308 if (vd->vdev_fru != NULL) 1309 VERIFY0(nvlist_add_string(resource, 1310 FM_EREPORT_PAYLOAD_ZFS_VDEV_FRU, vd->vdev_fru)); 1311 if (vd->vdev_enc_sysfs_path != NULL) 1312 VERIFY0(nvlist_add_string(resource, 1313 FM_EREPORT_PAYLOAD_ZFS_VDEV_ENC_SYSFS_PATH, 1314 vd->vdev_enc_sysfs_path)); 1315 } 1316 1317 /* also copy any optional payload data */ 1318 if (aux) { 1319 nvpair_t *elem = NULL; 1320 1321 while ((elem = nvlist_next_nvpair(aux, elem)) != NULL) 1322 (void) nvlist_add_nvpair(resource, elem); 1323 } 1324 1325 #endif 1326 return (resource); 1327 } 1328 1329 static void 1330 zfs_post_common(spa_t *spa, vdev_t *vd, const char *type, const char *name, 1331 nvlist_t *aux) 1332 { 1333 #ifdef _KERNEL 1334 nvlist_t *resource; 1335 1336 resource = zfs_event_create(spa, vd, type, name, aux); 1337 if (resource) 1338 zfs_zevent_post(resource, NULL, zfs_zevent_post_cb); 1339 #endif 1340 } 1341 1342 /* 1343 * The 'resource.fs.zfs.removed' event is an internal signal that the given vdev 1344 * has been removed from the system. This will cause the DE to ignore any 1345 * recent I/O errors, inferring that they are due to the asynchronous device 1346 * removal. 1347 */ 1348 void 1349 zfs_post_remove(spa_t *spa, vdev_t *vd) 1350 { 1351 zfs_post_common(spa, vd, FM_RSRC_CLASS, FM_RESOURCE_REMOVED, NULL); 1352 } 1353 1354 /* 1355 * The 'resource.fs.zfs.autoreplace' event is an internal signal that the pool 1356 * has the 'autoreplace' property set, and therefore any broken vdevs will be 1357 * handled by higher level logic, and no vdev fault should be generated. 1358 */ 1359 void 1360 zfs_post_autoreplace(spa_t *spa, vdev_t *vd) 1361 { 1362 zfs_post_common(spa, vd, FM_RSRC_CLASS, FM_RESOURCE_AUTOREPLACE, NULL); 1363 } 1364 1365 /* 1366 * The 'resource.fs.zfs.statechange' event is an internal signal that the 1367 * given vdev has transitioned its state to DEGRADED or HEALTHY. This will 1368 * cause the retire agent to repair any outstanding fault management cases 1369 * open because the device was not found (fault.fs.zfs.device). 1370 */ 1371 void 1372 zfs_post_state_change(spa_t *spa, vdev_t *vd, uint64_t laststate) 1373 { 1374 #ifdef _KERNEL 1375 nvlist_t *aux; 1376 1377 /* 1378 * Add optional supplemental keys to payload 1379 */ 1380 aux = fm_nvlist_create(NULL); 1381 if (vd && aux) { 1382 if (vd->vdev_physpath) { 1383 (void) nvlist_add_string(aux, 1384 FM_EREPORT_PAYLOAD_ZFS_VDEV_PHYSPATH, 1385 vd->vdev_physpath); 1386 } 1387 if (vd->vdev_enc_sysfs_path) { 1388 (void) nvlist_add_string(aux, 1389 FM_EREPORT_PAYLOAD_ZFS_VDEV_ENC_SYSFS_PATH, 1390 vd->vdev_enc_sysfs_path); 1391 } 1392 1393 (void) nvlist_add_uint64(aux, 1394 FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE, laststate); 1395 } 1396 1397 zfs_post_common(spa, vd, FM_RSRC_CLASS, FM_RESOURCE_STATECHANGE, 1398 aux); 1399 1400 if (aux) 1401 fm_nvlist_destroy(aux, FM_NVA_FREE); 1402 #endif 1403 } 1404 1405 #ifdef _KERNEL 1406 void 1407 zfs_ereport_init(void) 1408 { 1409 mutex_init(&recent_events_lock, NULL, MUTEX_DEFAULT, NULL); 1410 list_create(&recent_events_list, sizeof (recent_events_node_t), 1411 offsetof(recent_events_node_t, re_list_link)); 1412 avl_create(&recent_events_tree, recent_events_compare, 1413 sizeof (recent_events_node_t), offsetof(recent_events_node_t, 1414 re_tree_link)); 1415 } 1416 1417 /* 1418 * This 'early' fini needs to run before zfs_fini() which on Linux waits 1419 * for the system_delay_taskq to drain. 1420 */ 1421 void 1422 zfs_ereport_taskq_fini(void) 1423 { 1424 mutex_enter(&recent_events_lock); 1425 if (recent_events_cleaner_tqid != 0) { 1426 taskq_cancel_id(system_delay_taskq, recent_events_cleaner_tqid); 1427 recent_events_cleaner_tqid = 0; 1428 } 1429 mutex_exit(&recent_events_lock); 1430 } 1431 1432 void 1433 zfs_ereport_fini(void) 1434 { 1435 recent_events_node_t *entry; 1436 1437 while ((entry = list_head(&recent_events_list)) != NULL) { 1438 avl_remove(&recent_events_tree, entry); 1439 list_remove(&recent_events_list, entry); 1440 kmem_free(entry, sizeof (*entry)); 1441 } 1442 avl_destroy(&recent_events_tree); 1443 list_destroy(&recent_events_list); 1444 mutex_destroy(&recent_events_lock); 1445 } 1446 1447 EXPORT_SYMBOL(zfs_ereport_post); 1448 EXPORT_SYMBOL(zfs_ereport_is_valid); 1449 EXPORT_SYMBOL(zfs_ereport_post_checksum); 1450 EXPORT_SYMBOL(zfs_post_remove); 1451 EXPORT_SYMBOL(zfs_post_autoreplace); 1452 EXPORT_SYMBOL(zfs_post_state_change); 1453 1454 ZFS_MODULE_PARAM(zfs_zevent, zfs_zevent_, retain_max, UINT, ZMOD_RW, 1455 "Maximum recent zevents records to retain for duplicate checking"); 1456 ZFS_MODULE_PARAM(zfs_zevent, zfs_zevent_, retain_expire_secs, UINT, ZMOD_RW, 1457 "Expiration time for recent zevents records"); 1458 #endif /* _KERNEL */ 1459