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 https://opensource.org/licenses/CDDL-1.0. 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 static 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 static 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 static void 209 zfs_ereport_cleaner(void *arg) 210 { 211 recent_events_node_t *entry; 212 uint64_t now = gethrtime(); 213 214 /* 215 * purge expired entries 216 */ 217 mutex_enter(&recent_events_lock); 218 while ((entry = list_tail(&recent_events_list)) != NULL) { 219 uint64_t age = NSEC2SEC(now - entry->re_timestamp); 220 if (age <= zfs_zevent_retain_expire_secs) 221 break; 222 223 /* remove expired node */ 224 avl_remove(&recent_events_tree, entry); 225 list_remove(&recent_events_list, entry); 226 kmem_free(entry, sizeof (*entry)); 227 } 228 229 /* Restart the cleaner if more entries remain */ 230 recent_events_cleaner_tqid = 0; 231 if (!list_is_empty(&recent_events_list)) 232 zfs_ereport_schedule_cleaner(); 233 234 mutex_exit(&recent_events_lock); 235 } 236 237 static void 238 zfs_ereport_schedule_cleaner(void) 239 { 240 ASSERT(MUTEX_HELD(&recent_events_lock)); 241 242 uint64_t timeout = SEC2NSEC(zfs_zevent_retain_expire_secs + 1); 243 244 recent_events_cleaner_tqid = taskq_dispatch_delay( 245 system_delay_taskq, zfs_ereport_cleaner, NULL, TQ_SLEEP, 246 ddi_get_lbolt() + NSEC_TO_TICK(timeout)); 247 } 248 249 /* 250 * Clear entries for a given vdev or all vdevs in a pool when vdev == NULL 251 */ 252 void 253 zfs_ereport_clear(spa_t *spa, vdev_t *vd) 254 { 255 uint64_t vdev_guid, pool_guid; 256 257 ASSERT(vd != NULL || spa != NULL); 258 if (vd == NULL) { 259 vdev_guid = 0; 260 pool_guid = spa_guid(spa); 261 } else { 262 vdev_guid = vd->vdev_guid; 263 pool_guid = 0; 264 } 265 266 mutex_enter(&recent_events_lock); 267 268 recent_events_node_t *next = list_head(&recent_events_list); 269 while (next != NULL) { 270 recent_events_node_t *entry = next; 271 272 next = list_next(&recent_events_list, next); 273 274 if (entry->re_vdev_guid == vdev_guid || 275 entry->re_pool_guid == pool_guid) { 276 avl_remove(&recent_events_tree, entry); 277 list_remove(&recent_events_list, entry); 278 kmem_free(entry, sizeof (*entry)); 279 } 280 } 281 282 mutex_exit(&recent_events_lock); 283 } 284 285 /* 286 * Check if an ereport would be a duplicate of one recently posted. 287 * 288 * An ereport is considered a duplicate if the set of criteria in 289 * recent_events_node_t all match. 290 * 291 * Only FM_EREPORT_ZFS_IO, FM_EREPORT_ZFS_DATA, and FM_EREPORT_ZFS_CHECKSUM 292 * are candidates for duplicate checking. 293 */ 294 static boolean_t 295 zfs_ereport_is_duplicate(const char *subclass, spa_t *spa, vdev_t *vd, 296 const zbookmark_phys_t *zb, zio_t *zio, uint64_t offset, uint64_t size) 297 { 298 recent_events_node_t search = {0}, *entry; 299 300 if (vd == NULL || zio == NULL) 301 return (B_FALSE); 302 303 if (zfs_zevent_retain_max == 0) 304 return (B_FALSE); 305 306 if (strcmp(subclass, FM_EREPORT_ZFS_IO) == 0) 307 search.re_subclass = ZSC_IO; 308 else if (strcmp(subclass, FM_EREPORT_ZFS_DATA) == 0) 309 search.re_subclass = ZSC_DATA; 310 else if (strcmp(subclass, FM_EREPORT_ZFS_CHECKSUM) == 0) 311 search.re_subclass = ZSC_CHECKSUM; 312 else 313 return (B_FALSE); 314 315 search.re_pool_guid = spa_guid(spa); 316 search.re_vdev_guid = vd->vdev_guid; 317 search.re_io_error = zio->io_error; 318 search.re_io_priority = zio->io_priority; 319 /* if size is supplied use it over what's in zio */ 320 if (size) { 321 search.re_io_size = size; 322 search.re_io_offset = offset; 323 } else { 324 search.re_io_size = zio->io_size; 325 search.re_io_offset = zio->io_offset; 326 } 327 328 /* grab optional logical zio criteria */ 329 if (zb != NULL) { 330 search.re_io_bookmark.zb_objset = zb->zb_objset; 331 search.re_io_bookmark.zb_object = zb->zb_object; 332 search.re_io_bookmark.zb_level = zb->zb_level; 333 search.re_io_bookmark.zb_blkid = zb->zb_blkid; 334 } 335 336 uint64_t now = gethrtime(); 337 338 mutex_enter(&recent_events_lock); 339 340 /* check if we have seen this one recently */ 341 entry = avl_find(&recent_events_tree, &search, NULL); 342 if (entry != NULL) { 343 uint64_t age = NSEC2SEC(now - entry->re_timestamp); 344 345 /* 346 * There is still an active cleaner (since we're here). 347 * Reset the last seen time for this duplicate entry 348 * so that its lifespand gets extended. 349 */ 350 list_remove(&recent_events_list, entry); 351 list_insert_head(&recent_events_list, entry); 352 entry->re_timestamp = now; 353 354 zfs_zevent_track_duplicate(); 355 mutex_exit(&recent_events_lock); 356 357 return (age <= zfs_zevent_retain_expire_secs); 358 } 359 360 if (avl_numnodes(&recent_events_tree) >= zfs_zevent_retain_max) { 361 /* recycle oldest node */ 362 entry = list_tail(&recent_events_list); 363 ASSERT(entry != NULL); 364 list_remove(&recent_events_list, entry); 365 avl_remove(&recent_events_tree, entry); 366 } else { 367 entry = kmem_alloc(sizeof (recent_events_node_t), KM_SLEEP); 368 } 369 370 /* record this as a recent ereport */ 371 *entry = search; 372 avl_add(&recent_events_tree, entry); 373 list_insert_head(&recent_events_list, entry); 374 entry->re_timestamp = now; 375 376 /* Start a cleaner if not already scheduled */ 377 if (recent_events_cleaner_tqid == 0) 378 zfs_ereport_schedule_cleaner(); 379 380 mutex_exit(&recent_events_lock); 381 return (B_FALSE); 382 } 383 384 void 385 zfs_zevent_post_cb(nvlist_t *nvl, nvlist_t *detector) 386 { 387 if (nvl) 388 fm_nvlist_destroy(nvl, FM_NVA_FREE); 389 390 if (detector) 391 fm_nvlist_destroy(detector, FM_NVA_FREE); 392 } 393 394 /* 395 * We want to rate limit ZIO delay, deadman, and checksum events so as to not 396 * flood zevent consumers when a disk is acting up. 397 * 398 * Returns 1 if we're ratelimiting, 0 if not. 399 */ 400 static int 401 zfs_is_ratelimiting_event(const char *subclass, vdev_t *vd) 402 { 403 int rc = 0; 404 /* 405 * zfs_ratelimit() returns 1 if we're *not* ratelimiting and 0 if we 406 * are. Invert it to get our return value. 407 */ 408 if (strcmp(subclass, FM_EREPORT_ZFS_DELAY) == 0) { 409 rc = !zfs_ratelimit(&vd->vdev_delay_rl); 410 } else if (strcmp(subclass, FM_EREPORT_ZFS_DEADMAN) == 0) { 411 rc = !zfs_ratelimit(&vd->vdev_deadman_rl); 412 } else if (strcmp(subclass, FM_EREPORT_ZFS_CHECKSUM) == 0) { 413 rc = !zfs_ratelimit(&vd->vdev_checksum_rl); 414 } 415 416 if (rc) { 417 /* We're rate limiting */ 418 fm_erpt_dropped_increment(); 419 } 420 421 return (rc); 422 } 423 424 /* 425 * Return B_TRUE if the event actually posted, B_FALSE if not. 426 */ 427 static boolean_t 428 zfs_ereport_start(nvlist_t **ereport_out, nvlist_t **detector_out, 429 const char *subclass, spa_t *spa, vdev_t *vd, const zbookmark_phys_t *zb, 430 zio_t *zio, uint64_t stateoroffset, uint64_t size) 431 { 432 nvlist_t *ereport, *detector; 433 434 uint64_t ena; 435 char class[64]; 436 437 if ((ereport = fm_nvlist_create(NULL)) == NULL) 438 return (B_FALSE); 439 440 if ((detector = fm_nvlist_create(NULL)) == NULL) { 441 fm_nvlist_destroy(ereport, FM_NVA_FREE); 442 return (B_FALSE); 443 } 444 445 /* 446 * Serialize ereport generation 447 */ 448 mutex_enter(&spa->spa_errlist_lock); 449 450 /* 451 * Determine the ENA to use for this event. If we are in a loading 452 * state, use a SPA-wide ENA. Otherwise, if we are in an I/O state, use 453 * a root zio-wide ENA. Otherwise, simply use a unique ENA. 454 */ 455 if (spa_load_state(spa) != SPA_LOAD_NONE) { 456 if (spa->spa_ena == 0) 457 spa->spa_ena = fm_ena_generate(0, FM_ENA_FMT1); 458 ena = spa->spa_ena; 459 } else if (zio != NULL && zio->io_logical != NULL) { 460 if (zio->io_logical->io_ena == 0) 461 zio->io_logical->io_ena = 462 fm_ena_generate(0, FM_ENA_FMT1); 463 ena = zio->io_logical->io_ena; 464 } else { 465 ena = fm_ena_generate(0, FM_ENA_FMT1); 466 } 467 468 /* 469 * Construct the full class, detector, and other standard FMA fields. 470 */ 471 (void) snprintf(class, sizeof (class), "%s.%s", 472 ZFS_ERROR_CLASS, subclass); 473 474 fm_fmri_zfs_set(detector, FM_ZFS_SCHEME_VERSION, spa_guid(spa), 475 vd != NULL ? vd->vdev_guid : 0); 476 477 fm_ereport_set(ereport, FM_EREPORT_VERSION, class, ena, detector, NULL); 478 479 /* 480 * Construct the per-ereport payload, depending on which parameters are 481 * passed in. 482 */ 483 484 /* 485 * Generic payload members common to all ereports. 486 */ 487 fm_payload_set(ereport, 488 FM_EREPORT_PAYLOAD_ZFS_POOL, DATA_TYPE_STRING, spa_name(spa), 489 FM_EREPORT_PAYLOAD_ZFS_POOL_GUID, DATA_TYPE_UINT64, spa_guid(spa), 490 FM_EREPORT_PAYLOAD_ZFS_POOL_STATE, DATA_TYPE_UINT64, 491 (uint64_t)spa_state(spa), 492 FM_EREPORT_PAYLOAD_ZFS_POOL_CONTEXT, DATA_TYPE_INT32, 493 (int32_t)spa_load_state(spa), NULL); 494 495 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_POOL_FAILMODE, 496 DATA_TYPE_STRING, 497 spa_get_failmode(spa) == ZIO_FAILURE_MODE_WAIT ? 498 FM_EREPORT_FAILMODE_WAIT : 499 spa_get_failmode(spa) == ZIO_FAILURE_MODE_CONTINUE ? 500 FM_EREPORT_FAILMODE_CONTINUE : FM_EREPORT_FAILMODE_PANIC, 501 NULL); 502 503 if (vd != NULL) { 504 vdev_t *pvd = vd->vdev_parent; 505 vdev_queue_t *vq = &vd->vdev_queue; 506 vdev_stat_t *vs = &vd->vdev_stat; 507 vdev_t *spare_vd; 508 uint64_t *spare_guids; 509 char **spare_paths; 510 int i, spare_count; 511 512 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID, 513 DATA_TYPE_UINT64, vd->vdev_guid, 514 FM_EREPORT_PAYLOAD_ZFS_VDEV_TYPE, 515 DATA_TYPE_STRING, vd->vdev_ops->vdev_op_type, NULL); 516 if (vd->vdev_path != NULL) 517 fm_payload_set(ereport, 518 FM_EREPORT_PAYLOAD_ZFS_VDEV_PATH, 519 DATA_TYPE_STRING, vd->vdev_path, NULL); 520 if (vd->vdev_devid != NULL) 521 fm_payload_set(ereport, 522 FM_EREPORT_PAYLOAD_ZFS_VDEV_DEVID, 523 DATA_TYPE_STRING, vd->vdev_devid, NULL); 524 if (vd->vdev_fru != NULL) 525 fm_payload_set(ereport, 526 FM_EREPORT_PAYLOAD_ZFS_VDEV_FRU, 527 DATA_TYPE_STRING, vd->vdev_fru, NULL); 528 if (vd->vdev_enc_sysfs_path != NULL) 529 fm_payload_set(ereport, 530 FM_EREPORT_PAYLOAD_ZFS_VDEV_ENC_SYSFS_PATH, 531 DATA_TYPE_STRING, vd->vdev_enc_sysfs_path, NULL); 532 if (vd->vdev_ashift) 533 fm_payload_set(ereport, 534 FM_EREPORT_PAYLOAD_ZFS_VDEV_ASHIFT, 535 DATA_TYPE_UINT64, vd->vdev_ashift, NULL); 536 537 if (vq != NULL) { 538 fm_payload_set(ereport, 539 FM_EREPORT_PAYLOAD_ZFS_VDEV_COMP_TS, 540 DATA_TYPE_UINT64, vq->vq_io_complete_ts, NULL); 541 fm_payload_set(ereport, 542 FM_EREPORT_PAYLOAD_ZFS_VDEV_DELTA_TS, 543 DATA_TYPE_UINT64, vq->vq_io_delta_ts, NULL); 544 } 545 546 if (vs != NULL) { 547 fm_payload_set(ereport, 548 FM_EREPORT_PAYLOAD_ZFS_VDEV_READ_ERRORS, 549 DATA_TYPE_UINT64, vs->vs_read_errors, 550 FM_EREPORT_PAYLOAD_ZFS_VDEV_WRITE_ERRORS, 551 DATA_TYPE_UINT64, vs->vs_write_errors, 552 FM_EREPORT_PAYLOAD_ZFS_VDEV_CKSUM_ERRORS, 553 DATA_TYPE_UINT64, vs->vs_checksum_errors, 554 FM_EREPORT_PAYLOAD_ZFS_VDEV_DELAYS, 555 DATA_TYPE_UINT64, vs->vs_slow_ios, 556 NULL); 557 } 558 559 if (pvd != NULL) { 560 fm_payload_set(ereport, 561 FM_EREPORT_PAYLOAD_ZFS_PARENT_GUID, 562 DATA_TYPE_UINT64, pvd->vdev_guid, 563 FM_EREPORT_PAYLOAD_ZFS_PARENT_TYPE, 564 DATA_TYPE_STRING, pvd->vdev_ops->vdev_op_type, 565 NULL); 566 if (pvd->vdev_path) 567 fm_payload_set(ereport, 568 FM_EREPORT_PAYLOAD_ZFS_PARENT_PATH, 569 DATA_TYPE_STRING, pvd->vdev_path, NULL); 570 if (pvd->vdev_devid) 571 fm_payload_set(ereport, 572 FM_EREPORT_PAYLOAD_ZFS_PARENT_DEVID, 573 DATA_TYPE_STRING, pvd->vdev_devid, NULL); 574 } 575 576 spare_count = spa->spa_spares.sav_count; 577 spare_paths = kmem_zalloc(sizeof (char *) * spare_count, 578 KM_SLEEP); 579 spare_guids = kmem_zalloc(sizeof (uint64_t) * spare_count, 580 KM_SLEEP); 581 582 for (i = 0; i < spare_count; i++) { 583 spare_vd = spa->spa_spares.sav_vdevs[i]; 584 if (spare_vd) { 585 spare_paths[i] = spare_vd->vdev_path; 586 spare_guids[i] = spare_vd->vdev_guid; 587 } 588 } 589 590 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_VDEV_SPARE_PATHS, 591 DATA_TYPE_STRING_ARRAY, spare_count, spare_paths, 592 FM_EREPORT_PAYLOAD_ZFS_VDEV_SPARE_GUIDS, 593 DATA_TYPE_UINT64_ARRAY, spare_count, spare_guids, NULL); 594 595 kmem_free(spare_guids, sizeof (uint64_t) * spare_count); 596 kmem_free(spare_paths, sizeof (char *) * spare_count); 597 } 598 599 if (zio != NULL) { 600 /* 601 * Payload common to all I/Os. 602 */ 603 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_ERR, 604 DATA_TYPE_INT32, zio->io_error, NULL); 605 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_FLAGS, 606 DATA_TYPE_INT32, zio->io_flags, NULL); 607 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_STAGE, 608 DATA_TYPE_UINT32, zio->io_stage, NULL); 609 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_PIPELINE, 610 DATA_TYPE_UINT32, zio->io_pipeline, NULL); 611 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_DELAY, 612 DATA_TYPE_UINT64, zio->io_delay, NULL); 613 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_TIMESTAMP, 614 DATA_TYPE_UINT64, zio->io_timestamp, NULL); 615 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_DELTA, 616 DATA_TYPE_UINT64, zio->io_delta, NULL); 617 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_PRIORITY, 618 DATA_TYPE_UINT32, zio->io_priority, NULL); 619 620 /* 621 * If the 'size' parameter is non-zero, it indicates this is a 622 * RAID-Z or other I/O where the physical offset and length are 623 * provided for us, instead of within the zio_t. 624 */ 625 if (vd != NULL) { 626 if (size) 627 fm_payload_set(ereport, 628 FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET, 629 DATA_TYPE_UINT64, stateoroffset, 630 FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE, 631 DATA_TYPE_UINT64, size, NULL); 632 else 633 fm_payload_set(ereport, 634 FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET, 635 DATA_TYPE_UINT64, zio->io_offset, 636 FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE, 637 DATA_TYPE_UINT64, zio->io_size, NULL); 638 } 639 } else if (vd != NULL) { 640 /* 641 * If we have a vdev but no zio, this is a device fault, and the 642 * 'stateoroffset' parameter indicates the previous state of the 643 * vdev. 644 */ 645 fm_payload_set(ereport, 646 FM_EREPORT_PAYLOAD_ZFS_PREV_STATE, 647 DATA_TYPE_UINT64, stateoroffset, NULL); 648 } 649 650 /* 651 * Payload for I/Os with corresponding logical information. 652 */ 653 if (zb != NULL && (zio == NULL || zio->io_logical != NULL)) { 654 fm_payload_set(ereport, 655 FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJSET, 656 DATA_TYPE_UINT64, zb->zb_objset, 657 FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJECT, 658 DATA_TYPE_UINT64, zb->zb_object, 659 FM_EREPORT_PAYLOAD_ZFS_ZIO_LEVEL, 660 DATA_TYPE_INT64, zb->zb_level, 661 FM_EREPORT_PAYLOAD_ZFS_ZIO_BLKID, 662 DATA_TYPE_UINT64, zb->zb_blkid, NULL); 663 } 664 665 mutex_exit(&spa->spa_errlist_lock); 666 667 *ereport_out = ereport; 668 *detector_out = detector; 669 return (B_TRUE); 670 } 671 672 /* if it's <= 128 bytes, save the corruption directly */ 673 #define ZFM_MAX_INLINE (128 / sizeof (uint64_t)) 674 675 #define MAX_RANGES 16 676 677 typedef struct zfs_ecksum_info { 678 /* histograms of set and cleared bits by bit number in a 64-bit word */ 679 uint32_t zei_histogram_set[sizeof (uint64_t) * NBBY]; 680 uint32_t zei_histogram_cleared[sizeof (uint64_t) * NBBY]; 681 682 /* inline arrays of bits set and cleared. */ 683 uint64_t zei_bits_set[ZFM_MAX_INLINE]; 684 uint64_t zei_bits_cleared[ZFM_MAX_INLINE]; 685 686 /* 687 * for each range, the number of bits set and cleared. The Hamming 688 * distance between the good and bad buffers is the sum of them all. 689 */ 690 uint32_t zei_range_sets[MAX_RANGES]; 691 uint32_t zei_range_clears[MAX_RANGES]; 692 693 struct zei_ranges { 694 uint32_t zr_start; 695 uint32_t zr_end; 696 } zei_ranges[MAX_RANGES]; 697 698 size_t zei_range_count; 699 uint32_t zei_mingap; 700 uint32_t zei_allowed_mingap; 701 702 } zfs_ecksum_info_t; 703 704 static void 705 update_histogram(uint64_t value_arg, uint32_t *hist, uint32_t *count) 706 { 707 size_t i; 708 size_t bits = 0; 709 uint64_t value = BE_64(value_arg); 710 711 /* We store the bits in big-endian (largest-first) order */ 712 for (i = 0; i < 64; i++) { 713 if (value & (1ull << i)) { 714 hist[63 - i]++; 715 ++bits; 716 } 717 } 718 /* update the count of bits changed */ 719 *count += bits; 720 } 721 722 /* 723 * We've now filled up the range array, and need to increase "mingap" and 724 * shrink the range list accordingly. zei_mingap is always the smallest 725 * distance between array entries, so we set the new_allowed_gap to be 726 * one greater than that. We then go through the list, joining together 727 * any ranges which are closer than the new_allowed_gap. 728 * 729 * By construction, there will be at least one. We also update zei_mingap 730 * to the new smallest gap, to prepare for our next invocation. 731 */ 732 static void 733 zei_shrink_ranges(zfs_ecksum_info_t *eip) 734 { 735 uint32_t mingap = UINT32_MAX; 736 uint32_t new_allowed_gap = eip->zei_mingap + 1; 737 738 size_t idx, output; 739 size_t max = eip->zei_range_count; 740 741 struct zei_ranges *r = eip->zei_ranges; 742 743 ASSERT3U(eip->zei_range_count, >, 0); 744 ASSERT3U(eip->zei_range_count, <=, MAX_RANGES); 745 746 output = idx = 0; 747 while (idx < max - 1) { 748 uint32_t start = r[idx].zr_start; 749 uint32_t end = r[idx].zr_end; 750 751 while (idx < max - 1) { 752 idx++; 753 754 uint32_t nstart = r[idx].zr_start; 755 uint32_t nend = r[idx].zr_end; 756 757 uint32_t gap = nstart - end; 758 if (gap < new_allowed_gap) { 759 end = nend; 760 continue; 761 } 762 if (gap < mingap) 763 mingap = gap; 764 break; 765 } 766 r[output].zr_start = start; 767 r[output].zr_end = end; 768 output++; 769 } 770 ASSERT3U(output, <, eip->zei_range_count); 771 eip->zei_range_count = output; 772 eip->zei_mingap = mingap; 773 eip->zei_allowed_mingap = new_allowed_gap; 774 } 775 776 static void 777 zei_add_range(zfs_ecksum_info_t *eip, int start, int end) 778 { 779 struct zei_ranges *r = eip->zei_ranges; 780 size_t count = eip->zei_range_count; 781 782 if (count >= MAX_RANGES) { 783 zei_shrink_ranges(eip); 784 count = eip->zei_range_count; 785 } 786 if (count == 0) { 787 eip->zei_mingap = UINT32_MAX; 788 eip->zei_allowed_mingap = 1; 789 } else { 790 int gap = start - r[count - 1].zr_end; 791 792 if (gap < eip->zei_allowed_mingap) { 793 r[count - 1].zr_end = end; 794 return; 795 } 796 if (gap < eip->zei_mingap) 797 eip->zei_mingap = gap; 798 } 799 r[count].zr_start = start; 800 r[count].zr_end = end; 801 eip->zei_range_count++; 802 } 803 804 static size_t 805 zei_range_total_size(zfs_ecksum_info_t *eip) 806 { 807 struct zei_ranges *r = eip->zei_ranges; 808 size_t count = eip->zei_range_count; 809 size_t result = 0; 810 size_t idx; 811 812 for (idx = 0; idx < count; idx++) 813 result += (r[idx].zr_end - r[idx].zr_start); 814 815 return (result); 816 } 817 818 static zfs_ecksum_info_t * 819 annotate_ecksum(nvlist_t *ereport, zio_bad_cksum_t *info, 820 const abd_t *goodabd, const abd_t *badabd, size_t size, 821 boolean_t drop_if_identical) 822 { 823 const uint64_t *good; 824 const uint64_t *bad; 825 826 size_t nui64s = size / sizeof (uint64_t); 827 828 size_t inline_size; 829 int no_inline = 0; 830 size_t idx; 831 size_t range; 832 833 size_t offset = 0; 834 ssize_t start = -1; 835 836 zfs_ecksum_info_t *eip = kmem_zalloc(sizeof (*eip), KM_SLEEP); 837 838 /* don't do any annotation for injected checksum errors */ 839 if (info != NULL && info->zbc_injected) 840 return (eip); 841 842 if (info != NULL && info->zbc_has_cksum) { 843 fm_payload_set(ereport, 844 FM_EREPORT_PAYLOAD_ZFS_CKSUM_EXPECTED, 845 DATA_TYPE_UINT64_ARRAY, 846 sizeof (info->zbc_expected) / sizeof (uint64_t), 847 (uint64_t *)&info->zbc_expected, 848 FM_EREPORT_PAYLOAD_ZFS_CKSUM_ACTUAL, 849 DATA_TYPE_UINT64_ARRAY, 850 sizeof (info->zbc_actual) / sizeof (uint64_t), 851 (uint64_t *)&info->zbc_actual, 852 FM_EREPORT_PAYLOAD_ZFS_CKSUM_ALGO, 853 DATA_TYPE_STRING, 854 info->zbc_checksum_name, 855 NULL); 856 857 if (info->zbc_byteswapped) { 858 fm_payload_set(ereport, 859 FM_EREPORT_PAYLOAD_ZFS_CKSUM_BYTESWAP, 860 DATA_TYPE_BOOLEAN, 1, 861 NULL); 862 } 863 } 864 865 if (badabd == NULL || goodabd == NULL) 866 return (eip); 867 868 ASSERT3U(nui64s, <=, UINT32_MAX); 869 ASSERT3U(size, ==, nui64s * sizeof (uint64_t)); 870 ASSERT3U(size, <=, SPA_MAXBLOCKSIZE); 871 ASSERT3U(size, <=, UINT32_MAX); 872 873 good = (const uint64_t *) abd_borrow_buf_copy((abd_t *)goodabd, size); 874 bad = (const uint64_t *) abd_borrow_buf_copy((abd_t *)badabd, size); 875 876 /* build up the range list by comparing the two buffers. */ 877 for (idx = 0; idx < nui64s; idx++) { 878 if (good[idx] == bad[idx]) { 879 if (start == -1) 880 continue; 881 882 zei_add_range(eip, start, idx); 883 start = -1; 884 } else { 885 if (start != -1) 886 continue; 887 888 start = idx; 889 } 890 } 891 if (start != -1) 892 zei_add_range(eip, start, idx); 893 894 /* See if it will fit in our inline buffers */ 895 inline_size = zei_range_total_size(eip); 896 if (inline_size > ZFM_MAX_INLINE) 897 no_inline = 1; 898 899 /* 900 * If there is no change and we want to drop if the buffers are 901 * identical, do so. 902 */ 903 if (inline_size == 0 && drop_if_identical) { 904 kmem_free(eip, sizeof (*eip)); 905 abd_return_buf((abd_t *)goodabd, (void *)good, size); 906 abd_return_buf((abd_t *)badabd, (void *)bad, size); 907 return (NULL); 908 } 909 910 /* 911 * Now walk through the ranges, filling in the details of the 912 * differences. Also convert our uint64_t-array offsets to byte 913 * offsets. 914 */ 915 for (range = 0; range < eip->zei_range_count; range++) { 916 size_t start = eip->zei_ranges[range].zr_start; 917 size_t end = eip->zei_ranges[range].zr_end; 918 919 for (idx = start; idx < end; idx++) { 920 uint64_t set, cleared; 921 922 // bits set in bad, but not in good 923 set = ((~good[idx]) & bad[idx]); 924 // bits set in good, but not in bad 925 cleared = (good[idx] & (~bad[idx])); 926 927 if (!no_inline) { 928 ASSERT3U(offset, <, inline_size); 929 eip->zei_bits_set[offset] = set; 930 eip->zei_bits_cleared[offset] = cleared; 931 offset++; 932 } 933 934 update_histogram(set, eip->zei_histogram_set, 935 &eip->zei_range_sets[range]); 936 update_histogram(cleared, eip->zei_histogram_cleared, 937 &eip->zei_range_clears[range]); 938 } 939 940 /* convert to byte offsets */ 941 eip->zei_ranges[range].zr_start *= sizeof (uint64_t); 942 eip->zei_ranges[range].zr_end *= sizeof (uint64_t); 943 } 944 945 abd_return_buf((abd_t *)goodabd, (void *)good, size); 946 abd_return_buf((abd_t *)badabd, (void *)bad, size); 947 948 eip->zei_allowed_mingap *= sizeof (uint64_t); 949 inline_size *= sizeof (uint64_t); 950 951 /* fill in ereport */ 952 fm_payload_set(ereport, 953 FM_EREPORT_PAYLOAD_ZFS_BAD_OFFSET_RANGES, 954 DATA_TYPE_UINT32_ARRAY, 2 * eip->zei_range_count, 955 (uint32_t *)eip->zei_ranges, 956 FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_MIN_GAP, 957 DATA_TYPE_UINT32, eip->zei_allowed_mingap, 958 FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_SETS, 959 DATA_TYPE_UINT32_ARRAY, eip->zei_range_count, eip->zei_range_sets, 960 FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_CLEARS, 961 DATA_TYPE_UINT32_ARRAY, eip->zei_range_count, eip->zei_range_clears, 962 NULL); 963 964 if (!no_inline) { 965 fm_payload_set(ereport, 966 FM_EREPORT_PAYLOAD_ZFS_BAD_SET_BITS, 967 DATA_TYPE_UINT8_ARRAY, 968 inline_size, (uint8_t *)eip->zei_bits_set, 969 FM_EREPORT_PAYLOAD_ZFS_BAD_CLEARED_BITS, 970 DATA_TYPE_UINT8_ARRAY, 971 inline_size, (uint8_t *)eip->zei_bits_cleared, 972 NULL); 973 } else { 974 fm_payload_set(ereport, 975 FM_EREPORT_PAYLOAD_ZFS_BAD_SET_HISTOGRAM, 976 DATA_TYPE_UINT32_ARRAY, 977 NBBY * sizeof (uint64_t), eip->zei_histogram_set, 978 FM_EREPORT_PAYLOAD_ZFS_BAD_CLEARED_HISTOGRAM, 979 DATA_TYPE_UINT32_ARRAY, 980 NBBY * sizeof (uint64_t), eip->zei_histogram_cleared, 981 NULL); 982 } 983 return (eip); 984 } 985 #else 986 void 987 zfs_ereport_clear(spa_t *spa, vdev_t *vd) 988 { 989 (void) spa, (void) vd; 990 } 991 #endif 992 993 /* 994 * Make sure our event is still valid for the given zio/vdev/pool. For example, 995 * we don't want to keep logging events for a faulted or missing vdev. 996 */ 997 boolean_t 998 zfs_ereport_is_valid(const char *subclass, spa_t *spa, vdev_t *vd, zio_t *zio) 999 { 1000 #ifdef _KERNEL 1001 /* 1002 * If we are doing a spa_tryimport() or in recovery mode, 1003 * ignore errors. 1004 */ 1005 if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT || 1006 spa_load_state(spa) == SPA_LOAD_RECOVER) 1007 return (B_FALSE); 1008 1009 /* 1010 * If we are in the middle of opening a pool, and the previous attempt 1011 * failed, don't bother logging any new ereports - we're just going to 1012 * get the same diagnosis anyway. 1013 */ 1014 if (spa_load_state(spa) != SPA_LOAD_NONE && 1015 spa->spa_last_open_failed) 1016 return (B_FALSE); 1017 1018 if (zio != NULL) { 1019 /* 1020 * If this is not a read or write zio, ignore the error. This 1021 * can occur if the DKIOCFLUSHWRITECACHE ioctl fails. 1022 */ 1023 if (zio->io_type != ZIO_TYPE_READ && 1024 zio->io_type != ZIO_TYPE_WRITE) 1025 return (B_FALSE); 1026 1027 if (vd != NULL) { 1028 /* 1029 * If the vdev has already been marked as failing due 1030 * to a failed probe, then ignore any subsequent I/O 1031 * errors, as the DE will automatically fault the vdev 1032 * on the first such failure. This also catches cases 1033 * where vdev_remove_wanted is set and the device has 1034 * not yet been asynchronously placed into the REMOVED 1035 * state. 1036 */ 1037 if (zio->io_vd == vd && !vdev_accessible(vd, zio)) 1038 return (B_FALSE); 1039 1040 /* 1041 * Ignore checksum errors for reads from DTL regions of 1042 * leaf vdevs. 1043 */ 1044 if (zio->io_type == ZIO_TYPE_READ && 1045 zio->io_error == ECKSUM && 1046 vd->vdev_ops->vdev_op_leaf && 1047 vdev_dtl_contains(vd, DTL_MISSING, zio->io_txg, 1)) 1048 return (B_FALSE); 1049 } 1050 } 1051 1052 /* 1053 * For probe failure, we want to avoid posting ereports if we've 1054 * already removed the device in the meantime. 1055 */ 1056 if (vd != NULL && 1057 strcmp(subclass, FM_EREPORT_ZFS_PROBE_FAILURE) == 0 && 1058 (vd->vdev_remove_wanted || vd->vdev_state == VDEV_STATE_REMOVED)) 1059 return (B_FALSE); 1060 1061 /* Ignore bogus delay events (like from ioctls or unqueued IOs) */ 1062 if ((strcmp(subclass, FM_EREPORT_ZFS_DELAY) == 0) && 1063 (zio != NULL) && (!zio->io_timestamp)) { 1064 return (B_FALSE); 1065 } 1066 #else 1067 (void) subclass, (void) spa, (void) vd, (void) zio; 1068 #endif 1069 return (B_TRUE); 1070 } 1071 1072 /* 1073 * Post an ereport for the given subclass 1074 * 1075 * Returns 1076 * - 0 if an event was posted 1077 * - EINVAL if there was a problem posting event 1078 * - EBUSY if the event was rate limited 1079 * - EALREADY if the event was already posted (duplicate) 1080 */ 1081 int 1082 zfs_ereport_post(const char *subclass, spa_t *spa, vdev_t *vd, 1083 const zbookmark_phys_t *zb, zio_t *zio, uint64_t state) 1084 { 1085 int rc = 0; 1086 #ifdef _KERNEL 1087 nvlist_t *ereport = NULL; 1088 nvlist_t *detector = NULL; 1089 1090 if (!zfs_ereport_is_valid(subclass, spa, vd, zio)) 1091 return (EINVAL); 1092 1093 if (zfs_ereport_is_duplicate(subclass, spa, vd, zb, zio, 0, 0)) 1094 return (SET_ERROR(EALREADY)); 1095 1096 if (zfs_is_ratelimiting_event(subclass, vd)) 1097 return (SET_ERROR(EBUSY)); 1098 1099 if (!zfs_ereport_start(&ereport, &detector, subclass, spa, vd, 1100 zb, zio, state, 0)) 1101 return (SET_ERROR(EINVAL)); /* couldn't post event */ 1102 1103 if (ereport == NULL) 1104 return (SET_ERROR(EINVAL)); 1105 1106 /* Cleanup is handled by the callback function */ 1107 rc = zfs_zevent_post(ereport, detector, zfs_zevent_post_cb); 1108 #else 1109 (void) subclass, (void) spa, (void) vd, (void) zb, (void) zio, 1110 (void) state; 1111 #endif 1112 return (rc); 1113 } 1114 1115 /* 1116 * Prepare a checksum ereport 1117 * 1118 * Returns 1119 * - 0 if an event was posted 1120 * - EINVAL if there was a problem posting event 1121 * - EBUSY if the event was rate limited 1122 * - EALREADY if the event was already posted (duplicate) 1123 */ 1124 int 1125 zfs_ereport_start_checksum(spa_t *spa, vdev_t *vd, const zbookmark_phys_t *zb, 1126 struct zio *zio, uint64_t offset, uint64_t length, zio_bad_cksum_t *info) 1127 { 1128 zio_cksum_report_t *report; 1129 1130 #ifdef _KERNEL 1131 if (!zfs_ereport_is_valid(FM_EREPORT_ZFS_CHECKSUM, spa, vd, zio)) 1132 return (SET_ERROR(EINVAL)); 1133 1134 if (zfs_ereport_is_duplicate(FM_EREPORT_ZFS_CHECKSUM, spa, vd, zb, zio, 1135 offset, length)) 1136 return (SET_ERROR(EALREADY)); 1137 1138 if (zfs_is_ratelimiting_event(FM_EREPORT_ZFS_CHECKSUM, vd)) 1139 return (SET_ERROR(EBUSY)); 1140 #else 1141 (void) zb, (void) offset; 1142 #endif 1143 1144 report = kmem_zalloc(sizeof (*report), KM_SLEEP); 1145 1146 zio_vsd_default_cksum_report(zio, report); 1147 1148 /* copy the checksum failure information if it was provided */ 1149 if (info != NULL) { 1150 report->zcr_ckinfo = kmem_zalloc(sizeof (*info), KM_SLEEP); 1151 memcpy(report->zcr_ckinfo, info, sizeof (*info)); 1152 } 1153 1154 report->zcr_sector = 1ULL << vd->vdev_top->vdev_ashift; 1155 report->zcr_align = 1156 vdev_psize_to_asize(vd->vdev_top, report->zcr_sector); 1157 report->zcr_length = length; 1158 1159 #ifdef _KERNEL 1160 (void) zfs_ereport_start(&report->zcr_ereport, &report->zcr_detector, 1161 FM_EREPORT_ZFS_CHECKSUM, spa, vd, zb, zio, offset, length); 1162 1163 if (report->zcr_ereport == NULL) { 1164 zfs_ereport_free_checksum(report); 1165 return (0); 1166 } 1167 #endif 1168 1169 mutex_enter(&spa->spa_errlist_lock); 1170 report->zcr_next = zio->io_logical->io_cksum_report; 1171 zio->io_logical->io_cksum_report = report; 1172 mutex_exit(&spa->spa_errlist_lock); 1173 return (0); 1174 } 1175 1176 void 1177 zfs_ereport_finish_checksum(zio_cksum_report_t *report, const abd_t *good_data, 1178 const abd_t *bad_data, boolean_t drop_if_identical) 1179 { 1180 #ifdef _KERNEL 1181 zfs_ecksum_info_t *info; 1182 1183 info = annotate_ecksum(report->zcr_ereport, report->zcr_ckinfo, 1184 good_data, bad_data, report->zcr_length, drop_if_identical); 1185 if (info != NULL) 1186 zfs_zevent_post(report->zcr_ereport, 1187 report->zcr_detector, zfs_zevent_post_cb); 1188 else 1189 zfs_zevent_post_cb(report->zcr_ereport, report->zcr_detector); 1190 1191 report->zcr_ereport = report->zcr_detector = NULL; 1192 if (info != NULL) 1193 kmem_free(info, sizeof (*info)); 1194 #else 1195 (void) report, (void) good_data, (void) bad_data, 1196 (void) drop_if_identical; 1197 #endif 1198 } 1199 1200 void 1201 zfs_ereport_free_checksum(zio_cksum_report_t *rpt) 1202 { 1203 #ifdef _KERNEL 1204 if (rpt->zcr_ereport != NULL) { 1205 fm_nvlist_destroy(rpt->zcr_ereport, 1206 FM_NVA_FREE); 1207 fm_nvlist_destroy(rpt->zcr_detector, 1208 FM_NVA_FREE); 1209 } 1210 #endif 1211 rpt->zcr_free(rpt->zcr_cbdata, rpt->zcr_cbinfo); 1212 1213 if (rpt->zcr_ckinfo != NULL) 1214 kmem_free(rpt->zcr_ckinfo, sizeof (*rpt->zcr_ckinfo)); 1215 1216 kmem_free(rpt, sizeof (*rpt)); 1217 } 1218 1219 /* 1220 * Post a checksum ereport 1221 * 1222 * Returns 1223 * - 0 if an event was posted 1224 * - EINVAL if there was a problem posting event 1225 * - EBUSY if the event was rate limited 1226 * - EALREADY if the event was already posted (duplicate) 1227 */ 1228 int 1229 zfs_ereport_post_checksum(spa_t *spa, vdev_t *vd, const zbookmark_phys_t *zb, 1230 struct zio *zio, uint64_t offset, uint64_t length, 1231 const abd_t *good_data, const abd_t *bad_data, zio_bad_cksum_t *zbc) 1232 { 1233 int rc = 0; 1234 #ifdef _KERNEL 1235 nvlist_t *ereport = NULL; 1236 nvlist_t *detector = NULL; 1237 zfs_ecksum_info_t *info; 1238 1239 if (!zfs_ereport_is_valid(FM_EREPORT_ZFS_CHECKSUM, spa, vd, zio)) 1240 return (SET_ERROR(EINVAL)); 1241 1242 if (zfs_ereport_is_duplicate(FM_EREPORT_ZFS_CHECKSUM, spa, vd, zb, zio, 1243 offset, length)) 1244 return (SET_ERROR(EALREADY)); 1245 1246 if (zfs_is_ratelimiting_event(FM_EREPORT_ZFS_CHECKSUM, vd)) 1247 return (SET_ERROR(EBUSY)); 1248 1249 if (!zfs_ereport_start(&ereport, &detector, FM_EREPORT_ZFS_CHECKSUM, 1250 spa, vd, zb, zio, offset, length) || (ereport == NULL)) { 1251 return (SET_ERROR(EINVAL)); 1252 } 1253 1254 info = annotate_ecksum(ereport, zbc, good_data, bad_data, length, 1255 B_FALSE); 1256 1257 if (info != NULL) { 1258 rc = zfs_zevent_post(ereport, detector, zfs_zevent_post_cb); 1259 kmem_free(info, sizeof (*info)); 1260 } 1261 #else 1262 (void) spa, (void) vd, (void) zb, (void) zio, (void) offset, 1263 (void) length, (void) good_data, (void) bad_data, (void) zbc; 1264 #endif 1265 return (rc); 1266 } 1267 1268 /* 1269 * The 'sysevent.fs.zfs.*' events are signals posted to notify user space of 1270 * change in the pool. All sysevents are listed in sys/sysevent/eventdefs.h 1271 * and are designed to be consumed by the ZFS Event Daemon (ZED). For 1272 * additional details refer to the zed(8) man page. 1273 */ 1274 nvlist_t * 1275 zfs_event_create(spa_t *spa, vdev_t *vd, const char *type, const char *name, 1276 nvlist_t *aux) 1277 { 1278 nvlist_t *resource = NULL; 1279 #ifdef _KERNEL 1280 char class[64]; 1281 1282 if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT) 1283 return (NULL); 1284 1285 if ((resource = fm_nvlist_create(NULL)) == NULL) 1286 return (NULL); 1287 1288 (void) snprintf(class, sizeof (class), "%s.%s.%s", type, 1289 ZFS_ERROR_CLASS, name); 1290 VERIFY0(nvlist_add_uint8(resource, FM_VERSION, FM_RSRC_VERSION)); 1291 VERIFY0(nvlist_add_string(resource, FM_CLASS, class)); 1292 VERIFY0(nvlist_add_string(resource, 1293 FM_EREPORT_PAYLOAD_ZFS_POOL, spa_name(spa))); 1294 VERIFY0(nvlist_add_uint64(resource, 1295 FM_EREPORT_PAYLOAD_ZFS_POOL_GUID, spa_guid(spa))); 1296 VERIFY0(nvlist_add_uint64(resource, 1297 FM_EREPORT_PAYLOAD_ZFS_POOL_STATE, spa_state(spa))); 1298 VERIFY0(nvlist_add_int32(resource, 1299 FM_EREPORT_PAYLOAD_ZFS_POOL_CONTEXT, spa_load_state(spa))); 1300 1301 if (vd) { 1302 VERIFY0(nvlist_add_uint64(resource, 1303 FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID, vd->vdev_guid)); 1304 VERIFY0(nvlist_add_uint64(resource, 1305 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE, vd->vdev_state)); 1306 if (vd->vdev_path != NULL) 1307 VERIFY0(nvlist_add_string(resource, 1308 FM_EREPORT_PAYLOAD_ZFS_VDEV_PATH, vd->vdev_path)); 1309 if (vd->vdev_devid != NULL) 1310 VERIFY0(nvlist_add_string(resource, 1311 FM_EREPORT_PAYLOAD_ZFS_VDEV_DEVID, vd->vdev_devid)); 1312 if (vd->vdev_fru != NULL) 1313 VERIFY0(nvlist_add_string(resource, 1314 FM_EREPORT_PAYLOAD_ZFS_VDEV_FRU, vd->vdev_fru)); 1315 if (vd->vdev_enc_sysfs_path != NULL) 1316 VERIFY0(nvlist_add_string(resource, 1317 FM_EREPORT_PAYLOAD_ZFS_VDEV_ENC_SYSFS_PATH, 1318 vd->vdev_enc_sysfs_path)); 1319 } 1320 1321 /* also copy any optional payload data */ 1322 if (aux) { 1323 nvpair_t *elem = NULL; 1324 1325 while ((elem = nvlist_next_nvpair(aux, elem)) != NULL) 1326 (void) nvlist_add_nvpair(resource, elem); 1327 } 1328 #else 1329 (void) spa, (void) vd, (void) type, (void) name, (void) aux; 1330 #endif 1331 return (resource); 1332 } 1333 1334 static void 1335 zfs_post_common(spa_t *spa, vdev_t *vd, const char *type, const char *name, 1336 nvlist_t *aux) 1337 { 1338 #ifdef _KERNEL 1339 nvlist_t *resource; 1340 1341 resource = zfs_event_create(spa, vd, type, name, aux); 1342 if (resource) 1343 zfs_zevent_post(resource, NULL, zfs_zevent_post_cb); 1344 #else 1345 (void) spa, (void) vd, (void) type, (void) name, (void) aux; 1346 #endif 1347 } 1348 1349 /* 1350 * The 'resource.fs.zfs.removed' event is an internal signal that the given vdev 1351 * has been removed from the system. This will cause the DE to ignore any 1352 * recent I/O errors, inferring that they are due to the asynchronous device 1353 * removal. 1354 */ 1355 void 1356 zfs_post_remove(spa_t *spa, vdev_t *vd) 1357 { 1358 zfs_post_common(spa, vd, FM_RSRC_CLASS, FM_RESOURCE_REMOVED, NULL); 1359 } 1360 1361 /* 1362 * The 'resource.fs.zfs.autoreplace' event is an internal signal that the pool 1363 * has the 'autoreplace' property set, and therefore any broken vdevs will be 1364 * handled by higher level logic, and no vdev fault should be generated. 1365 */ 1366 void 1367 zfs_post_autoreplace(spa_t *spa, vdev_t *vd) 1368 { 1369 zfs_post_common(spa, vd, FM_RSRC_CLASS, FM_RESOURCE_AUTOREPLACE, NULL); 1370 } 1371 1372 /* 1373 * The 'resource.fs.zfs.statechange' event is an internal signal that the 1374 * given vdev has transitioned its state to DEGRADED or HEALTHY. This will 1375 * cause the retire agent to repair any outstanding fault management cases 1376 * open because the device was not found (fault.fs.zfs.device). 1377 */ 1378 void 1379 zfs_post_state_change(spa_t *spa, vdev_t *vd, uint64_t laststate) 1380 { 1381 #ifdef _KERNEL 1382 nvlist_t *aux; 1383 1384 /* 1385 * Add optional supplemental keys to payload 1386 */ 1387 aux = fm_nvlist_create(NULL); 1388 if (vd && aux) { 1389 if (vd->vdev_physpath) { 1390 fnvlist_add_string(aux, 1391 FM_EREPORT_PAYLOAD_ZFS_VDEV_PHYSPATH, 1392 vd->vdev_physpath); 1393 } 1394 if (vd->vdev_enc_sysfs_path) { 1395 fnvlist_add_string(aux, 1396 FM_EREPORT_PAYLOAD_ZFS_VDEV_ENC_SYSFS_PATH, 1397 vd->vdev_enc_sysfs_path); 1398 } 1399 1400 fnvlist_add_uint64(aux, 1401 FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE, laststate); 1402 } 1403 1404 zfs_post_common(spa, vd, FM_RSRC_CLASS, FM_RESOURCE_STATECHANGE, 1405 aux); 1406 1407 if (aux) 1408 fm_nvlist_destroy(aux, FM_NVA_FREE); 1409 #else 1410 (void) spa, (void) vd, (void) laststate; 1411 #endif 1412 } 1413 1414 #ifdef _KERNEL 1415 void 1416 zfs_ereport_init(void) 1417 { 1418 mutex_init(&recent_events_lock, NULL, MUTEX_DEFAULT, NULL); 1419 list_create(&recent_events_list, sizeof (recent_events_node_t), 1420 offsetof(recent_events_node_t, re_list_link)); 1421 avl_create(&recent_events_tree, recent_events_compare, 1422 sizeof (recent_events_node_t), offsetof(recent_events_node_t, 1423 re_tree_link)); 1424 } 1425 1426 /* 1427 * This 'early' fini needs to run before zfs_fini() which on Linux waits 1428 * for the system_delay_taskq to drain. 1429 */ 1430 void 1431 zfs_ereport_taskq_fini(void) 1432 { 1433 mutex_enter(&recent_events_lock); 1434 if (recent_events_cleaner_tqid != 0) { 1435 taskq_cancel_id(system_delay_taskq, recent_events_cleaner_tqid); 1436 recent_events_cleaner_tqid = 0; 1437 } 1438 mutex_exit(&recent_events_lock); 1439 } 1440 1441 void 1442 zfs_ereport_fini(void) 1443 { 1444 recent_events_node_t *entry; 1445 1446 while ((entry = list_head(&recent_events_list)) != NULL) { 1447 avl_remove(&recent_events_tree, entry); 1448 list_remove(&recent_events_list, entry); 1449 kmem_free(entry, sizeof (*entry)); 1450 } 1451 avl_destroy(&recent_events_tree); 1452 list_destroy(&recent_events_list); 1453 mutex_destroy(&recent_events_lock); 1454 } 1455 1456 void 1457 zfs_ereport_snapshot_post(const char *subclass, spa_t *spa, const char *name) 1458 { 1459 nvlist_t *aux; 1460 1461 aux = fm_nvlist_create(NULL); 1462 fnvlist_add_string(aux, FM_EREPORT_PAYLOAD_ZFS_SNAPSHOT_NAME, name); 1463 1464 zfs_post_common(spa, NULL, FM_RSRC_CLASS, subclass, aux); 1465 fm_nvlist_destroy(aux, FM_NVA_FREE); 1466 } 1467 1468 /* 1469 * Post when a event when a zvol is created or removed 1470 * 1471 * This is currently only used by macOS, since it uses the event to create 1472 * symlinks between the volume name (mypool/myvol) and the actual /dev 1473 * device (/dev/disk3). For example: 1474 * 1475 * /var/run/zfs/dsk/mypool/myvol -> /dev/disk3 1476 * 1477 * name: The full name of the zvol ("mypool/myvol") 1478 * dev_name: The full /dev name for the zvol ("/dev/disk3") 1479 * raw_name: The raw /dev name for the zvol ("/dev/rdisk3") 1480 */ 1481 void 1482 zfs_ereport_zvol_post(const char *subclass, const char *name, 1483 const char *dev_name, const char *raw_name) 1484 { 1485 nvlist_t *aux; 1486 char *r; 1487 1488 boolean_t locked = mutex_owned(&spa_namespace_lock); 1489 if (!locked) mutex_enter(&spa_namespace_lock); 1490 spa_t *spa = spa_lookup(name); 1491 if (!locked) mutex_exit(&spa_namespace_lock); 1492 1493 if (spa == NULL) 1494 return; 1495 1496 aux = fm_nvlist_create(NULL); 1497 fnvlist_add_string(aux, FM_EREPORT_PAYLOAD_ZFS_DEVICE_NAME, dev_name); 1498 fnvlist_add_string(aux, FM_EREPORT_PAYLOAD_ZFS_RAW_DEVICE_NAME, 1499 raw_name); 1500 r = strchr(name, '/'); 1501 if (r && r[1]) 1502 fnvlist_add_string(aux, FM_EREPORT_PAYLOAD_ZFS_VOLUME, &r[1]); 1503 1504 zfs_post_common(spa, NULL, FM_RSRC_CLASS, subclass, aux); 1505 fm_nvlist_destroy(aux, FM_NVA_FREE); 1506 } 1507 1508 EXPORT_SYMBOL(zfs_ereport_post); 1509 EXPORT_SYMBOL(zfs_ereport_is_valid); 1510 EXPORT_SYMBOL(zfs_ereport_post_checksum); 1511 EXPORT_SYMBOL(zfs_post_remove); 1512 EXPORT_SYMBOL(zfs_post_autoreplace); 1513 EXPORT_SYMBOL(zfs_post_state_change); 1514 1515 ZFS_MODULE_PARAM(zfs_zevent, zfs_zevent_, retain_max, UINT, ZMOD_RW, 1516 "Maximum recent zevents records to retain for duplicate checking"); 1517 ZFS_MODULE_PARAM(zfs_zevent, zfs_zevent_, retain_expire_secs, UINT, ZMOD_RW, 1518 "Expiration time for recent zevents records"); 1519 #endif /* _KERNEL */ 1520