ZTS: Add LUKS sanity test
[zfs.git] / module / zfs / zfs_fm.c
blob25b05abd3650484eb9b4862cadae009f8cf60c1f
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 * Copyright (c) 2012,2021 by Delphix. All rights reserved.
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>
37 #include <sys/fm/fs/zfs.h>
38 #include <sys/fm/protocol.h>
39 #include <sys/fm/util.h>
40 #include <sys/sysevent.h>
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:
47 * EREPORT POOL VDEV IO
48 * block X X X
49 * data X X
50 * device X X
51 * pool X
53 * If we are in a loading state, all errors are chained together by the same
54 * SPA-wide ENA (Error Numeric Association).
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:
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.
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).
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.
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).
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.
105 #ifdef _KERNEL
107 * Duplicate ereport Detection
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.
113 * The lifespan of these recent ereports is bounded (15 mins) and a cleaner
114 * task is used to purge stale entries.
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;
122 * Each node is about 128 bytes so 2,000 would consume 1/4 MiB.
124 * This setting can be changed dynamically and setting it to zero
125 * disables duplicate detection.
127 static unsigned int zfs_zevent_retain_max = 2000;
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.
134 static unsigned int zfs_zevent_retain_expire_secs = 900;
136 typedef enum zfs_subclass {
137 ZSC_IO,
138 ZSC_DATA,
139 ZSC_CHECKSUM
140 } zfs_subclass_t;
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;
152 /* logical zio criteria (optional) */
153 zbookmark_phys_t re_io_bookmark;
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;
161 static int
162 recent_events_compare(const void *a, const void *b)
164 const recent_events_node_t *node1 = a;
165 const recent_events_node_t *node2 = b;
166 int cmp;
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)
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);
188 const zbookmark_phys_t *zb1 = &node1->re_io_bookmark;
189 const zbookmark_phys_t *zb2 = &node2->re_io_bookmark;
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);
200 return (0);
204 * workaround: vdev properties don't have inheritance
206 static uint64_t
207 vdev_prop_get_inherited(vdev_t *vd, vdev_prop_t prop)
209 uint64_t propdef, propval;
211 propdef = vdev_prop_default_numeric(prop);
212 switch (prop) {
213 case VDEV_PROP_CHECKSUM_N:
214 propval = vd->vdev_checksum_n;
215 break;
216 case VDEV_PROP_CHECKSUM_T:
217 propval = vd->vdev_checksum_t;
218 break;
219 case VDEV_PROP_IO_N:
220 propval = vd->vdev_io_n;
221 break;
222 case VDEV_PROP_IO_T:
223 propval = vd->vdev_io_t;
224 break;
225 case VDEV_PROP_SLOW_IO_N:
226 propval = vd->vdev_slow_io_n;
227 break;
228 case VDEV_PROP_SLOW_IO_T:
229 propval = vd->vdev_slow_io_t;
230 break;
231 default:
232 propval = propdef;
233 break;
236 if (propval != propdef)
237 return (propval);
239 if (vd->vdev_parent == NULL)
240 return (propdef);
242 return (vdev_prop_get_inherited(vd->vdev_parent, prop));
245 static void zfs_ereport_schedule_cleaner(void);
248 * background task to clean stale recent event nodes.
250 static void
251 zfs_ereport_cleaner(void *arg)
253 recent_events_node_t *entry;
254 uint64_t now = gethrtime();
257 * purge expired entries
259 mutex_enter(&recent_events_lock);
260 while ((entry = list_tail(&recent_events_list)) != NULL) {
261 uint64_t age = NSEC2SEC(now - entry->re_timestamp);
262 if (age <= zfs_zevent_retain_expire_secs)
263 break;
265 /* remove expired node */
266 avl_remove(&recent_events_tree, entry);
267 list_remove(&recent_events_list, entry);
268 kmem_free(entry, sizeof (*entry));
271 /* Restart the cleaner if more entries remain */
272 recent_events_cleaner_tqid = 0;
273 if (!list_is_empty(&recent_events_list))
274 zfs_ereport_schedule_cleaner();
276 mutex_exit(&recent_events_lock);
279 static void
280 zfs_ereport_schedule_cleaner(void)
282 ASSERT(MUTEX_HELD(&recent_events_lock));
284 uint64_t timeout = SEC2NSEC(zfs_zevent_retain_expire_secs + 1);
286 recent_events_cleaner_tqid = taskq_dispatch_delay(
287 system_delay_taskq, zfs_ereport_cleaner, NULL, TQ_SLEEP,
288 ddi_get_lbolt() + NSEC_TO_TICK(timeout));
292 * Clear entries for a given vdev or all vdevs in a pool when vdev == NULL
294 void
295 zfs_ereport_clear(spa_t *spa, vdev_t *vd)
297 uint64_t vdev_guid, pool_guid;
299 ASSERT(vd != NULL || spa != NULL);
300 if (vd == NULL) {
301 vdev_guid = 0;
302 pool_guid = spa_guid(spa);
303 } else {
304 vdev_guid = vd->vdev_guid;
305 pool_guid = 0;
308 mutex_enter(&recent_events_lock);
310 recent_events_node_t *next = list_head(&recent_events_list);
311 while (next != NULL) {
312 recent_events_node_t *entry = next;
314 next = list_next(&recent_events_list, next);
316 if (entry->re_vdev_guid == vdev_guid ||
317 entry->re_pool_guid == pool_guid) {
318 avl_remove(&recent_events_tree, entry);
319 list_remove(&recent_events_list, entry);
320 kmem_free(entry, sizeof (*entry));
324 mutex_exit(&recent_events_lock);
328 * Check if an ereport would be a duplicate of one recently posted.
330 * An ereport is considered a duplicate if the set of criteria in
331 * recent_events_node_t all match.
333 * Only FM_EREPORT_ZFS_IO, FM_EREPORT_ZFS_DATA, and FM_EREPORT_ZFS_CHECKSUM
334 * are candidates for duplicate checking.
336 static boolean_t
337 zfs_ereport_is_duplicate(const char *subclass, spa_t *spa, vdev_t *vd,
338 const zbookmark_phys_t *zb, zio_t *zio, uint64_t offset, uint64_t size)
340 recent_events_node_t search = {0}, *entry;
342 if (vd == NULL || zio == NULL)
343 return (B_FALSE);
345 if (zfs_zevent_retain_max == 0)
346 return (B_FALSE);
348 if (strcmp(subclass, FM_EREPORT_ZFS_IO) == 0)
349 search.re_subclass = ZSC_IO;
350 else if (strcmp(subclass, FM_EREPORT_ZFS_DATA) == 0)
351 search.re_subclass = ZSC_DATA;
352 else if (strcmp(subclass, FM_EREPORT_ZFS_CHECKSUM) == 0)
353 search.re_subclass = ZSC_CHECKSUM;
354 else
355 return (B_FALSE);
357 search.re_pool_guid = spa_guid(spa);
358 search.re_vdev_guid = vd->vdev_guid;
359 search.re_io_error = zio->io_error;
360 search.re_io_priority = zio->io_priority;
361 /* if size is supplied use it over what's in zio */
362 if (size) {
363 search.re_io_size = size;
364 search.re_io_offset = offset;
365 } else {
366 search.re_io_size = zio->io_size;
367 search.re_io_offset = zio->io_offset;
370 /* grab optional logical zio criteria */
371 if (zb != NULL) {
372 search.re_io_bookmark.zb_objset = zb->zb_objset;
373 search.re_io_bookmark.zb_object = zb->zb_object;
374 search.re_io_bookmark.zb_level = zb->zb_level;
375 search.re_io_bookmark.zb_blkid = zb->zb_blkid;
378 uint64_t now = gethrtime();
380 mutex_enter(&recent_events_lock);
382 /* check if we have seen this one recently */
383 entry = avl_find(&recent_events_tree, &search, NULL);
384 if (entry != NULL) {
385 uint64_t age = NSEC2SEC(now - entry->re_timestamp);
388 * There is still an active cleaner (since we're here).
389 * Reset the last seen time for this duplicate entry
390 * so that its lifespand gets extended.
392 list_remove(&recent_events_list, entry);
393 list_insert_head(&recent_events_list, entry);
394 entry->re_timestamp = now;
396 zfs_zevent_track_duplicate();
397 mutex_exit(&recent_events_lock);
399 return (age <= zfs_zevent_retain_expire_secs);
402 if (avl_numnodes(&recent_events_tree) >= zfs_zevent_retain_max) {
403 /* recycle oldest node */
404 entry = list_tail(&recent_events_list);
405 ASSERT(entry != NULL);
406 list_remove(&recent_events_list, entry);
407 avl_remove(&recent_events_tree, entry);
408 } else {
409 entry = kmem_alloc(sizeof (recent_events_node_t), KM_SLEEP);
412 /* record this as a recent ereport */
413 *entry = search;
414 avl_add(&recent_events_tree, entry);
415 list_insert_head(&recent_events_list, entry);
416 entry->re_timestamp = now;
418 /* Start a cleaner if not already scheduled */
419 if (recent_events_cleaner_tqid == 0)
420 zfs_ereport_schedule_cleaner();
422 mutex_exit(&recent_events_lock);
423 return (B_FALSE);
426 void
427 zfs_zevent_post_cb(nvlist_t *nvl, nvlist_t *detector)
429 if (nvl)
430 fm_nvlist_destroy(nvl, FM_NVA_FREE);
432 if (detector)
433 fm_nvlist_destroy(detector, FM_NVA_FREE);
437 * We want to rate limit ZIO delay, deadman, and checksum events so as to not
438 * flood zevent consumers when a disk is acting up.
440 * Returns 1 if we're ratelimiting, 0 if not.
442 static int
443 zfs_is_ratelimiting_event(const char *subclass, vdev_t *vd)
445 int rc = 0;
447 * zfs_ratelimit() returns 1 if we're *not* ratelimiting and 0 if we
448 * are. Invert it to get our return value.
450 if (strcmp(subclass, FM_EREPORT_ZFS_DELAY) == 0) {
451 rc = !zfs_ratelimit(&vd->vdev_delay_rl);
452 } else if (strcmp(subclass, FM_EREPORT_ZFS_DEADMAN) == 0) {
453 rc = !zfs_ratelimit(&vd->vdev_deadman_rl);
454 } else if (strcmp(subclass, FM_EREPORT_ZFS_CHECKSUM) == 0) {
455 rc = !zfs_ratelimit(&vd->vdev_checksum_rl);
458 if (rc) {
459 /* We're rate limiting */
460 fm_erpt_dropped_increment();
463 return (rc);
467 * Return B_TRUE if the event actually posted, B_FALSE if not.
469 static boolean_t
470 zfs_ereport_start(nvlist_t **ereport_out, nvlist_t **detector_out,
471 const char *subclass, spa_t *spa, vdev_t *vd, const zbookmark_phys_t *zb,
472 zio_t *zio, uint64_t stateoroffset, uint64_t size)
474 nvlist_t *ereport, *detector;
476 uint64_t ena;
477 char class[64];
479 if ((ereport = fm_nvlist_create(NULL)) == NULL)
480 return (B_FALSE);
482 if ((detector = fm_nvlist_create(NULL)) == NULL) {
483 fm_nvlist_destroy(ereport, FM_NVA_FREE);
484 return (B_FALSE);
488 * Serialize ereport generation
490 mutex_enter(&spa->spa_errlist_lock);
493 * Determine the ENA to use for this event. If we are in a loading
494 * state, use a SPA-wide ENA. Otherwise, if we are in an I/O state, use
495 * a root zio-wide ENA. Otherwise, simply use a unique ENA.
497 if (spa_load_state(spa) != SPA_LOAD_NONE) {
498 if (spa->spa_ena == 0)
499 spa->spa_ena = fm_ena_generate(0, FM_ENA_FMT1);
500 ena = spa->spa_ena;
501 } else if (zio != NULL && zio->io_logical != NULL) {
502 if (zio->io_logical->io_ena == 0)
503 zio->io_logical->io_ena =
504 fm_ena_generate(0, FM_ENA_FMT1);
505 ena = zio->io_logical->io_ena;
506 } else {
507 ena = fm_ena_generate(0, FM_ENA_FMT1);
511 * Construct the full class, detector, and other standard FMA fields.
513 (void) snprintf(class, sizeof (class), "%s.%s",
514 ZFS_ERROR_CLASS, subclass);
516 fm_fmri_zfs_set(detector, FM_ZFS_SCHEME_VERSION, spa_guid(spa),
517 vd != NULL ? vd->vdev_guid : 0);
519 fm_ereport_set(ereport, FM_EREPORT_VERSION, class, ena, detector, NULL);
522 * Construct the per-ereport payload, depending on which parameters are
523 * passed in.
527 * Generic payload members common to all ereports.
529 fm_payload_set(ereport,
530 FM_EREPORT_PAYLOAD_ZFS_POOL, DATA_TYPE_STRING, spa_name(spa),
531 FM_EREPORT_PAYLOAD_ZFS_POOL_GUID, DATA_TYPE_UINT64, spa_guid(spa),
532 FM_EREPORT_PAYLOAD_ZFS_POOL_STATE, DATA_TYPE_UINT64,
533 (uint64_t)spa_state(spa),
534 FM_EREPORT_PAYLOAD_ZFS_POOL_CONTEXT, DATA_TYPE_INT32,
535 (int32_t)spa_load_state(spa), NULL);
537 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_POOL_FAILMODE,
538 DATA_TYPE_STRING,
539 spa_get_failmode(spa) == ZIO_FAILURE_MODE_WAIT ?
540 FM_EREPORT_FAILMODE_WAIT :
541 spa_get_failmode(spa) == ZIO_FAILURE_MODE_CONTINUE ?
542 FM_EREPORT_FAILMODE_CONTINUE : FM_EREPORT_FAILMODE_PANIC,
543 NULL);
545 if (vd != NULL) {
546 vdev_t *pvd = vd->vdev_parent;
547 vdev_queue_t *vq = &vd->vdev_queue;
548 vdev_stat_t *vs = &vd->vdev_stat;
549 vdev_t *spare_vd;
550 uint64_t *spare_guids;
551 char **spare_paths;
552 int i, spare_count;
554 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID,
555 DATA_TYPE_UINT64, vd->vdev_guid,
556 FM_EREPORT_PAYLOAD_ZFS_VDEV_TYPE,
557 DATA_TYPE_STRING, vd->vdev_ops->vdev_op_type, NULL);
558 if (vd->vdev_path != NULL)
559 fm_payload_set(ereport,
560 FM_EREPORT_PAYLOAD_ZFS_VDEV_PATH,
561 DATA_TYPE_STRING, vd->vdev_path, NULL);
562 if (vd->vdev_devid != NULL)
563 fm_payload_set(ereport,
564 FM_EREPORT_PAYLOAD_ZFS_VDEV_DEVID,
565 DATA_TYPE_STRING, vd->vdev_devid, NULL);
566 if (vd->vdev_fru != NULL)
567 fm_payload_set(ereport,
568 FM_EREPORT_PAYLOAD_ZFS_VDEV_FRU,
569 DATA_TYPE_STRING, vd->vdev_fru, NULL);
570 if (vd->vdev_enc_sysfs_path != NULL)
571 fm_payload_set(ereport,
572 FM_EREPORT_PAYLOAD_ZFS_VDEV_ENC_SYSFS_PATH,
573 DATA_TYPE_STRING, vd->vdev_enc_sysfs_path, NULL);
574 if (vd->vdev_ashift)
575 fm_payload_set(ereport,
576 FM_EREPORT_PAYLOAD_ZFS_VDEV_ASHIFT,
577 DATA_TYPE_UINT64, vd->vdev_ashift, NULL);
579 if (vq != NULL) {
580 fm_payload_set(ereport,
581 FM_EREPORT_PAYLOAD_ZFS_VDEV_COMP_TS,
582 DATA_TYPE_UINT64, vq->vq_io_complete_ts, NULL);
583 fm_payload_set(ereport,
584 FM_EREPORT_PAYLOAD_ZFS_VDEV_DELTA_TS,
585 DATA_TYPE_UINT64, vq->vq_io_delta_ts, NULL);
588 if (vs != NULL) {
589 fm_payload_set(ereport,
590 FM_EREPORT_PAYLOAD_ZFS_VDEV_READ_ERRORS,
591 DATA_TYPE_UINT64, vs->vs_read_errors,
592 FM_EREPORT_PAYLOAD_ZFS_VDEV_WRITE_ERRORS,
593 DATA_TYPE_UINT64, vs->vs_write_errors,
594 FM_EREPORT_PAYLOAD_ZFS_VDEV_CKSUM_ERRORS,
595 DATA_TYPE_UINT64, vs->vs_checksum_errors,
596 FM_EREPORT_PAYLOAD_ZFS_VDEV_DELAYS,
597 DATA_TYPE_UINT64, vs->vs_slow_ios,
598 FM_EREPORT_PAYLOAD_ZFS_VDEV_DIO_VERIFY_ERRORS,
599 DATA_TYPE_UINT64, vs->vs_dio_verify_errors,
600 NULL);
603 if (pvd != NULL) {
604 fm_payload_set(ereport,
605 FM_EREPORT_PAYLOAD_ZFS_PARENT_GUID,
606 DATA_TYPE_UINT64, pvd->vdev_guid,
607 FM_EREPORT_PAYLOAD_ZFS_PARENT_TYPE,
608 DATA_TYPE_STRING, pvd->vdev_ops->vdev_op_type,
609 NULL);
610 if (pvd->vdev_path)
611 fm_payload_set(ereport,
612 FM_EREPORT_PAYLOAD_ZFS_PARENT_PATH,
613 DATA_TYPE_STRING, pvd->vdev_path, NULL);
614 if (pvd->vdev_devid)
615 fm_payload_set(ereport,
616 FM_EREPORT_PAYLOAD_ZFS_PARENT_DEVID,
617 DATA_TYPE_STRING, pvd->vdev_devid, NULL);
620 spare_count = spa->spa_spares.sav_count;
621 spare_paths = kmem_zalloc(sizeof (char *) * spare_count,
622 KM_SLEEP);
623 spare_guids = kmem_zalloc(sizeof (uint64_t) * spare_count,
624 KM_SLEEP);
626 for (i = 0; i < spare_count; i++) {
627 spare_vd = spa->spa_spares.sav_vdevs[i];
628 if (spare_vd) {
629 spare_paths[i] = spare_vd->vdev_path;
630 spare_guids[i] = spare_vd->vdev_guid;
634 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_VDEV_SPARE_PATHS,
635 DATA_TYPE_STRING_ARRAY, spare_count, spare_paths,
636 FM_EREPORT_PAYLOAD_ZFS_VDEV_SPARE_GUIDS,
637 DATA_TYPE_UINT64_ARRAY, spare_count, spare_guids, NULL);
639 kmem_free(spare_guids, sizeof (uint64_t) * spare_count);
640 kmem_free(spare_paths, sizeof (char *) * spare_count);
643 if (zio != NULL) {
645 * Payload common to all I/Os.
647 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_ERR,
648 DATA_TYPE_INT32, zio->io_error, NULL);
649 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_FLAGS,
650 DATA_TYPE_UINT64, zio->io_flags, NULL);
651 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_STAGE,
652 DATA_TYPE_UINT32, zio->io_stage, NULL);
653 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_PIPELINE,
654 DATA_TYPE_UINT32, zio->io_pipeline, NULL);
655 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_DELAY,
656 DATA_TYPE_UINT64, zio->io_delay, NULL);
657 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_TIMESTAMP,
658 DATA_TYPE_UINT64, zio->io_timestamp, NULL);
659 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_DELTA,
660 DATA_TYPE_UINT64, zio->io_delta, NULL);
661 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_PRIORITY,
662 DATA_TYPE_UINT32, zio->io_priority, NULL);
665 * If the 'size' parameter is non-zero, it indicates this is a
666 * RAID-Z or other I/O where the physical offset and length are
667 * provided for us, instead of within the zio_t.
669 if (vd != NULL) {
670 if (size)
671 fm_payload_set(ereport,
672 FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET,
673 DATA_TYPE_UINT64, stateoroffset,
674 FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE,
675 DATA_TYPE_UINT64, size, NULL);
676 else
677 fm_payload_set(ereport,
678 FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET,
679 DATA_TYPE_UINT64, zio->io_offset,
680 FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE,
681 DATA_TYPE_UINT64, zio->io_size, NULL);
683 } else if (vd != NULL) {
685 * If we have a vdev but no zio, this is a device fault, and the
686 * 'stateoroffset' parameter indicates the previous state of the
687 * vdev.
689 fm_payload_set(ereport,
690 FM_EREPORT_PAYLOAD_ZFS_PREV_STATE,
691 DATA_TYPE_UINT64, stateoroffset, NULL);
695 * Payload for I/Os with corresponding logical information.
697 if (zb != NULL && (zio == NULL || zio->io_logical != NULL)) {
698 fm_payload_set(ereport,
699 FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJSET,
700 DATA_TYPE_UINT64, zb->zb_objset,
701 FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJECT,
702 DATA_TYPE_UINT64, zb->zb_object,
703 FM_EREPORT_PAYLOAD_ZFS_ZIO_LEVEL,
704 DATA_TYPE_INT64, zb->zb_level,
705 FM_EREPORT_PAYLOAD_ZFS_ZIO_BLKID,
706 DATA_TYPE_UINT64, zb->zb_blkid, NULL);
710 * Payload for tuning the zed
712 if (vd != NULL && strcmp(subclass, FM_EREPORT_ZFS_CHECKSUM) == 0) {
713 uint64_t cksum_n, cksum_t;
715 cksum_n = vdev_prop_get_inherited(vd, VDEV_PROP_CHECKSUM_N);
716 if (cksum_n != vdev_prop_default_numeric(VDEV_PROP_CHECKSUM_N))
717 fm_payload_set(ereport,
718 FM_EREPORT_PAYLOAD_ZFS_VDEV_CKSUM_N,
719 DATA_TYPE_UINT64,
720 cksum_n,
721 NULL);
723 cksum_t = vdev_prop_get_inherited(vd, VDEV_PROP_CHECKSUM_T);
724 if (cksum_t != vdev_prop_default_numeric(VDEV_PROP_CHECKSUM_T))
725 fm_payload_set(ereport,
726 FM_EREPORT_PAYLOAD_ZFS_VDEV_CKSUM_T,
727 DATA_TYPE_UINT64,
728 cksum_t,
729 NULL);
732 if (vd != NULL && strcmp(subclass, FM_EREPORT_ZFS_IO) == 0) {
733 uint64_t io_n, io_t;
735 io_n = vdev_prop_get_inherited(vd, VDEV_PROP_IO_N);
736 if (io_n != vdev_prop_default_numeric(VDEV_PROP_IO_N))
737 fm_payload_set(ereport,
738 FM_EREPORT_PAYLOAD_ZFS_VDEV_IO_N,
739 DATA_TYPE_UINT64,
740 io_n,
741 NULL);
743 io_t = vdev_prop_get_inherited(vd, VDEV_PROP_IO_T);
744 if (io_t != vdev_prop_default_numeric(VDEV_PROP_IO_T))
745 fm_payload_set(ereport,
746 FM_EREPORT_PAYLOAD_ZFS_VDEV_IO_T,
747 DATA_TYPE_UINT64,
748 io_t,
749 NULL);
752 if (vd != NULL && strcmp(subclass, FM_EREPORT_ZFS_DELAY) == 0) {
753 uint64_t slow_io_n, slow_io_t;
755 slow_io_n = vdev_prop_get_inherited(vd, VDEV_PROP_SLOW_IO_N);
756 if (slow_io_n != vdev_prop_default_numeric(VDEV_PROP_SLOW_IO_N))
757 fm_payload_set(ereport,
758 FM_EREPORT_PAYLOAD_ZFS_VDEV_SLOW_IO_N,
759 DATA_TYPE_UINT64,
760 slow_io_n,
761 NULL);
763 slow_io_t = vdev_prop_get_inherited(vd, VDEV_PROP_SLOW_IO_T);
764 if (slow_io_t != vdev_prop_default_numeric(VDEV_PROP_SLOW_IO_T))
765 fm_payload_set(ereport,
766 FM_EREPORT_PAYLOAD_ZFS_VDEV_SLOW_IO_T,
767 DATA_TYPE_UINT64,
768 slow_io_t,
769 NULL);
772 mutex_exit(&spa->spa_errlist_lock);
774 *ereport_out = ereport;
775 *detector_out = detector;
776 return (B_TRUE);
779 /* if it's <= 128 bytes, save the corruption directly */
780 #define ZFM_MAX_INLINE (128 / sizeof (uint64_t))
782 #define MAX_RANGES 16
784 typedef struct zfs_ecksum_info {
785 /* inline arrays of bits set and cleared. */
786 uint64_t zei_bits_set[ZFM_MAX_INLINE];
787 uint64_t zei_bits_cleared[ZFM_MAX_INLINE];
790 * for each range, the number of bits set and cleared. The Hamming
791 * distance between the good and bad buffers is the sum of them all.
793 uint32_t zei_range_sets[MAX_RANGES];
794 uint32_t zei_range_clears[MAX_RANGES];
796 struct zei_ranges {
797 uint32_t zr_start;
798 uint32_t zr_end;
799 } zei_ranges[MAX_RANGES];
801 size_t zei_range_count;
802 uint32_t zei_mingap;
803 uint32_t zei_allowed_mingap;
805 } zfs_ecksum_info_t;
807 static void
808 update_bad_bits(uint64_t value_arg, uint32_t *count)
810 size_t i;
811 size_t bits = 0;
812 uint64_t value = BE_64(value_arg);
814 /* We store the bits in big-endian (largest-first) order */
815 for (i = 0; i < 64; i++) {
816 if (value & (1ull << i))
817 ++bits;
819 /* update the count of bits changed */
820 *count += bits;
824 * We've now filled up the range array, and need to increase "mingap" and
825 * shrink the range list accordingly. zei_mingap is always the smallest
826 * distance between array entries, so we set the new_allowed_gap to be
827 * one greater than that. We then go through the list, joining together
828 * any ranges which are closer than the new_allowed_gap.
830 * By construction, there will be at least one. We also update zei_mingap
831 * to the new smallest gap, to prepare for our next invocation.
833 static void
834 zei_shrink_ranges(zfs_ecksum_info_t *eip)
836 uint32_t mingap = UINT32_MAX;
837 uint32_t new_allowed_gap = eip->zei_mingap + 1;
839 size_t idx, output;
840 size_t max = eip->zei_range_count;
842 struct zei_ranges *r = eip->zei_ranges;
844 ASSERT3U(eip->zei_range_count, >, 0);
845 ASSERT3U(eip->zei_range_count, <=, MAX_RANGES);
847 output = idx = 0;
848 while (idx < max - 1) {
849 uint32_t start = r[idx].zr_start;
850 uint32_t end = r[idx].zr_end;
852 while (idx < max - 1) {
853 idx++;
855 uint32_t nstart = r[idx].zr_start;
856 uint32_t nend = r[idx].zr_end;
858 uint32_t gap = nstart - end;
859 if (gap < new_allowed_gap) {
860 end = nend;
861 continue;
863 if (gap < mingap)
864 mingap = gap;
865 break;
867 r[output].zr_start = start;
868 r[output].zr_end = end;
869 output++;
871 ASSERT3U(output, <, eip->zei_range_count);
872 eip->zei_range_count = output;
873 eip->zei_mingap = mingap;
874 eip->zei_allowed_mingap = new_allowed_gap;
877 static void
878 zei_add_range(zfs_ecksum_info_t *eip, int start, int end)
880 struct zei_ranges *r = eip->zei_ranges;
881 size_t count = eip->zei_range_count;
883 if (count >= MAX_RANGES) {
884 zei_shrink_ranges(eip);
885 count = eip->zei_range_count;
887 if (count == 0) {
888 eip->zei_mingap = UINT32_MAX;
889 eip->zei_allowed_mingap = 1;
890 } else {
891 int gap = start - r[count - 1].zr_end;
893 if (gap < eip->zei_allowed_mingap) {
894 r[count - 1].zr_end = end;
895 return;
897 if (gap < eip->zei_mingap)
898 eip->zei_mingap = gap;
900 r[count].zr_start = start;
901 r[count].zr_end = end;
902 eip->zei_range_count++;
905 static size_t
906 zei_range_total_size(zfs_ecksum_info_t *eip)
908 struct zei_ranges *r = eip->zei_ranges;
909 size_t count = eip->zei_range_count;
910 size_t result = 0;
911 size_t idx;
913 for (idx = 0; idx < count; idx++)
914 result += (r[idx].zr_end - r[idx].zr_start);
916 return (result);
919 static zfs_ecksum_info_t *
920 annotate_ecksum(nvlist_t *ereport, zio_bad_cksum_t *info,
921 const abd_t *goodabd, const abd_t *badabd, size_t size,
922 boolean_t drop_if_identical)
924 const uint64_t *good;
925 const uint64_t *bad;
927 size_t nui64s = size / sizeof (uint64_t);
929 size_t inline_size;
930 int no_inline = 0;
931 size_t idx;
932 size_t range;
934 size_t offset = 0;
935 ssize_t start = -1;
937 zfs_ecksum_info_t *eip = kmem_zalloc(sizeof (*eip), KM_SLEEP);
939 /* don't do any annotation for injected checksum errors */
940 if (info != NULL && info->zbc_injected)
941 return (eip);
943 if (info != NULL && info->zbc_has_cksum) {
944 fm_payload_set(ereport,
945 FM_EREPORT_PAYLOAD_ZFS_CKSUM_ALGO,
946 DATA_TYPE_STRING,
947 info->zbc_checksum_name,
948 NULL);
950 if (info->zbc_byteswapped) {
951 fm_payload_set(ereport,
952 FM_EREPORT_PAYLOAD_ZFS_CKSUM_BYTESWAP,
953 DATA_TYPE_BOOLEAN, 1,
954 NULL);
958 if (badabd == NULL || goodabd == NULL)
959 return (eip);
961 ASSERT3U(nui64s, <=, UINT32_MAX);
962 ASSERT3U(size, ==, nui64s * sizeof (uint64_t));
963 ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
964 ASSERT3U(size, <=, UINT32_MAX);
966 good = (const uint64_t *) abd_borrow_buf_copy((abd_t *)goodabd, size);
967 bad = (const uint64_t *) abd_borrow_buf_copy((abd_t *)badabd, size);
969 /* build up the range list by comparing the two buffers. */
970 for (idx = 0; idx < nui64s; idx++) {
971 if (good[idx] == bad[idx]) {
972 if (start == -1)
973 continue;
975 zei_add_range(eip, start, idx);
976 start = -1;
977 } else {
978 if (start != -1)
979 continue;
981 start = idx;
984 if (start != -1)
985 zei_add_range(eip, start, idx);
987 /* See if it will fit in our inline buffers */
988 inline_size = zei_range_total_size(eip);
989 if (inline_size > ZFM_MAX_INLINE)
990 no_inline = 1;
993 * If there is no change and we want to drop if the buffers are
994 * identical, do so.
996 if (inline_size == 0 && drop_if_identical) {
997 kmem_free(eip, sizeof (*eip));
998 abd_return_buf((abd_t *)goodabd, (void *)good, size);
999 abd_return_buf((abd_t *)badabd, (void *)bad, size);
1000 return (NULL);
1004 * Now walk through the ranges, filling in the details of the
1005 * differences. Also convert our uint64_t-array offsets to byte
1006 * offsets.
1008 for (range = 0; range < eip->zei_range_count; range++) {
1009 size_t start = eip->zei_ranges[range].zr_start;
1010 size_t end = eip->zei_ranges[range].zr_end;
1012 for (idx = start; idx < end; idx++) {
1013 uint64_t set, cleared;
1015 // bits set in bad, but not in good
1016 set = ((~good[idx]) & bad[idx]);
1017 // bits set in good, but not in bad
1018 cleared = (good[idx] & (~bad[idx]));
1020 if (!no_inline) {
1021 ASSERT3U(offset, <, inline_size);
1022 eip->zei_bits_set[offset] = set;
1023 eip->zei_bits_cleared[offset] = cleared;
1024 offset++;
1027 update_bad_bits(set, &eip->zei_range_sets[range]);
1028 update_bad_bits(cleared, &eip->zei_range_clears[range]);
1031 /* convert to byte offsets */
1032 eip->zei_ranges[range].zr_start *= sizeof (uint64_t);
1033 eip->zei_ranges[range].zr_end *= sizeof (uint64_t);
1036 abd_return_buf((abd_t *)goodabd, (void *)good, size);
1037 abd_return_buf((abd_t *)badabd, (void *)bad, size);
1039 eip->zei_allowed_mingap *= sizeof (uint64_t);
1040 inline_size *= sizeof (uint64_t);
1042 /* fill in ereport */
1043 fm_payload_set(ereport,
1044 FM_EREPORT_PAYLOAD_ZFS_BAD_OFFSET_RANGES,
1045 DATA_TYPE_UINT32_ARRAY, 2 * eip->zei_range_count,
1046 (uint32_t *)eip->zei_ranges,
1047 FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_MIN_GAP,
1048 DATA_TYPE_UINT32, eip->zei_allowed_mingap,
1049 FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_SETS,
1050 DATA_TYPE_UINT32_ARRAY, eip->zei_range_count, eip->zei_range_sets,
1051 FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_CLEARS,
1052 DATA_TYPE_UINT32_ARRAY, eip->zei_range_count, eip->zei_range_clears,
1053 NULL);
1055 if (!no_inline) {
1056 fm_payload_set(ereport,
1057 FM_EREPORT_PAYLOAD_ZFS_BAD_SET_BITS,
1058 DATA_TYPE_UINT8_ARRAY,
1059 inline_size, (uint8_t *)eip->zei_bits_set,
1060 FM_EREPORT_PAYLOAD_ZFS_BAD_CLEARED_BITS,
1061 DATA_TYPE_UINT8_ARRAY,
1062 inline_size, (uint8_t *)eip->zei_bits_cleared,
1063 NULL);
1065 return (eip);
1067 #else
1068 void
1069 zfs_ereport_clear(spa_t *spa, vdev_t *vd)
1071 (void) spa, (void) vd;
1073 #endif
1076 * Make sure our event is still valid for the given zio/vdev/pool. For example,
1077 * we don't want to keep logging events for a faulted or missing vdev.
1079 boolean_t
1080 zfs_ereport_is_valid(const char *subclass, spa_t *spa, vdev_t *vd, zio_t *zio)
1082 #ifdef _KERNEL
1084 * If we are doing a spa_tryimport() or in recovery mode,
1085 * ignore errors.
1087 if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT ||
1088 spa_load_state(spa) == SPA_LOAD_RECOVER)
1089 return (B_FALSE);
1092 * If we are in the middle of opening a pool, and the previous attempt
1093 * failed, don't bother logging any new ereports - we're just going to
1094 * get the same diagnosis anyway.
1096 if (spa_load_state(spa) != SPA_LOAD_NONE &&
1097 spa->spa_last_open_failed)
1098 return (B_FALSE);
1100 if (zio != NULL) {
1101 /* If this is not a read or write zio, ignore the error */
1102 if (zio->io_type != ZIO_TYPE_READ &&
1103 zio->io_type != ZIO_TYPE_WRITE)
1104 return (B_FALSE);
1106 if (vd != NULL) {
1108 * If the vdev has already been marked as failing due
1109 * to a failed probe, then ignore any subsequent I/O
1110 * errors, as the DE will automatically fault the vdev
1111 * on the first such failure. This also catches cases
1112 * where vdev_remove_wanted is set and the device has
1113 * not yet been asynchronously placed into the REMOVED
1114 * state.
1116 if (zio->io_vd == vd && !vdev_accessible(vd, zio))
1117 return (B_FALSE);
1120 * Ignore checksum errors for reads from DTL regions of
1121 * leaf vdevs.
1123 if (zio->io_type == ZIO_TYPE_READ &&
1124 zio->io_error == ECKSUM &&
1125 vd->vdev_ops->vdev_op_leaf &&
1126 vdev_dtl_contains(vd, DTL_MISSING, zio->io_txg, 1))
1127 return (B_FALSE);
1132 * For probe failure, we want to avoid posting ereports if we've
1133 * already removed the device in the meantime.
1135 if (vd != NULL &&
1136 strcmp(subclass, FM_EREPORT_ZFS_PROBE_FAILURE) == 0 &&
1137 (vd->vdev_remove_wanted || vd->vdev_state == VDEV_STATE_REMOVED))
1138 return (B_FALSE);
1140 /* Ignore bogus delay events (like from ioctls or unqueued IOs) */
1141 if ((strcmp(subclass, FM_EREPORT_ZFS_DELAY) == 0) &&
1142 (zio != NULL) && (!zio->io_timestamp)) {
1143 return (B_FALSE);
1145 #else
1146 (void) subclass, (void) spa, (void) vd, (void) zio;
1147 #endif
1148 return (B_TRUE);
1152 * Post an ereport for the given subclass
1154 * Returns
1155 * - 0 if an event was posted
1156 * - EINVAL if there was a problem posting event
1157 * - EBUSY if the event was rate limited
1158 * - EALREADY if the event was already posted (duplicate)
1161 zfs_ereport_post(const char *subclass, spa_t *spa, vdev_t *vd,
1162 const zbookmark_phys_t *zb, zio_t *zio, uint64_t state)
1164 int rc = 0;
1165 #ifdef _KERNEL
1166 nvlist_t *ereport = NULL;
1167 nvlist_t *detector = NULL;
1169 if (!zfs_ereport_is_valid(subclass, spa, vd, zio))
1170 return (EINVAL);
1172 if (zfs_ereport_is_duplicate(subclass, spa, vd, zb, zio, 0, 0))
1173 return (SET_ERROR(EALREADY));
1175 if (zfs_is_ratelimiting_event(subclass, vd))
1176 return (SET_ERROR(EBUSY));
1178 if (!zfs_ereport_start(&ereport, &detector, subclass, spa, vd,
1179 zb, zio, state, 0))
1180 return (SET_ERROR(EINVAL)); /* couldn't post event */
1182 if (ereport == NULL)
1183 return (SET_ERROR(EINVAL));
1185 /* Cleanup is handled by the callback function */
1186 rc = zfs_zevent_post(ereport, detector, zfs_zevent_post_cb);
1187 #else
1188 (void) subclass, (void) spa, (void) vd, (void) zb, (void) zio,
1189 (void) state;
1190 #endif
1191 return (rc);
1195 * Prepare a checksum ereport
1197 * Returns
1198 * - 0 if an event was posted
1199 * - EINVAL if there was a problem posting event
1200 * - EBUSY if the event was rate limited
1201 * - EALREADY if the event was already posted (duplicate)
1204 zfs_ereport_start_checksum(spa_t *spa, vdev_t *vd, const zbookmark_phys_t *zb,
1205 struct zio *zio, uint64_t offset, uint64_t length, zio_bad_cksum_t *info)
1207 zio_cksum_report_t *report;
1209 #ifdef _KERNEL
1210 if (!zfs_ereport_is_valid(FM_EREPORT_ZFS_CHECKSUM, spa, vd, zio))
1211 return (SET_ERROR(EINVAL));
1213 if (zfs_ereport_is_duplicate(FM_EREPORT_ZFS_CHECKSUM, spa, vd, zb, zio,
1214 offset, length))
1215 return (SET_ERROR(EALREADY));
1217 if (zfs_is_ratelimiting_event(FM_EREPORT_ZFS_CHECKSUM, vd))
1218 return (SET_ERROR(EBUSY));
1219 #else
1220 (void) zb, (void) offset;
1221 #endif
1223 report = kmem_zalloc(sizeof (*report), KM_SLEEP);
1225 zio_vsd_default_cksum_report(zio, report);
1227 /* copy the checksum failure information if it was provided */
1228 if (info != NULL) {
1229 report->zcr_ckinfo = kmem_zalloc(sizeof (*info), KM_SLEEP);
1230 memcpy(report->zcr_ckinfo, info, sizeof (*info));
1233 report->zcr_sector = 1ULL << vd->vdev_top->vdev_ashift;
1234 report->zcr_align =
1235 vdev_psize_to_asize(vd->vdev_top, report->zcr_sector);
1236 report->zcr_length = length;
1238 #ifdef _KERNEL
1239 (void) zfs_ereport_start(&report->zcr_ereport, &report->zcr_detector,
1240 FM_EREPORT_ZFS_CHECKSUM, spa, vd, zb, zio, offset, length);
1242 if (report->zcr_ereport == NULL) {
1243 zfs_ereport_free_checksum(report);
1244 return (0);
1246 #endif
1248 mutex_enter(&spa->spa_errlist_lock);
1249 report->zcr_next = zio->io_logical->io_cksum_report;
1250 zio->io_logical->io_cksum_report = report;
1251 mutex_exit(&spa->spa_errlist_lock);
1252 return (0);
1255 void
1256 zfs_ereport_finish_checksum(zio_cksum_report_t *report, const abd_t *good_data,
1257 const abd_t *bad_data, boolean_t drop_if_identical)
1259 #ifdef _KERNEL
1260 zfs_ecksum_info_t *info;
1262 info = annotate_ecksum(report->zcr_ereport, report->zcr_ckinfo,
1263 good_data, bad_data, report->zcr_length, drop_if_identical);
1264 if (info != NULL)
1265 zfs_zevent_post(report->zcr_ereport,
1266 report->zcr_detector, zfs_zevent_post_cb);
1267 else
1268 zfs_zevent_post_cb(report->zcr_ereport, report->zcr_detector);
1270 report->zcr_ereport = report->zcr_detector = NULL;
1271 if (info != NULL)
1272 kmem_free(info, sizeof (*info));
1273 #else
1274 (void) report, (void) good_data, (void) bad_data,
1275 (void) drop_if_identical;
1276 #endif
1279 void
1280 zfs_ereport_free_checksum(zio_cksum_report_t *rpt)
1282 #ifdef _KERNEL
1283 if (rpt->zcr_ereport != NULL) {
1284 fm_nvlist_destroy(rpt->zcr_ereport,
1285 FM_NVA_FREE);
1286 fm_nvlist_destroy(rpt->zcr_detector,
1287 FM_NVA_FREE);
1289 #endif
1290 rpt->zcr_free(rpt->zcr_cbdata, rpt->zcr_cbinfo);
1292 if (rpt->zcr_ckinfo != NULL)
1293 kmem_free(rpt->zcr_ckinfo, sizeof (*rpt->zcr_ckinfo));
1295 kmem_free(rpt, sizeof (*rpt));
1299 * Post a checksum ereport
1301 * Returns
1302 * - 0 if an event was posted
1303 * - EINVAL if there was a problem posting event
1304 * - EBUSY if the event was rate limited
1305 * - EALREADY if the event was already posted (duplicate)
1308 zfs_ereport_post_checksum(spa_t *spa, vdev_t *vd, const zbookmark_phys_t *zb,
1309 struct zio *zio, uint64_t offset, uint64_t length,
1310 const abd_t *good_data, const abd_t *bad_data, zio_bad_cksum_t *zbc)
1312 int rc = 0;
1313 #ifdef _KERNEL
1314 nvlist_t *ereport = NULL;
1315 nvlist_t *detector = NULL;
1316 zfs_ecksum_info_t *info;
1318 if (!zfs_ereport_is_valid(FM_EREPORT_ZFS_CHECKSUM, spa, vd, zio))
1319 return (SET_ERROR(EINVAL));
1321 if (zfs_ereport_is_duplicate(FM_EREPORT_ZFS_CHECKSUM, spa, vd, zb, zio,
1322 offset, length))
1323 return (SET_ERROR(EALREADY));
1325 if (zfs_is_ratelimiting_event(FM_EREPORT_ZFS_CHECKSUM, vd))
1326 return (SET_ERROR(EBUSY));
1328 if (!zfs_ereport_start(&ereport, &detector, FM_EREPORT_ZFS_CHECKSUM,
1329 spa, vd, zb, zio, offset, length) || (ereport == NULL)) {
1330 return (SET_ERROR(EINVAL));
1333 info = annotate_ecksum(ereport, zbc, good_data, bad_data, length,
1334 B_FALSE);
1336 if (info != NULL) {
1337 rc = zfs_zevent_post(ereport, detector, zfs_zevent_post_cb);
1338 kmem_free(info, sizeof (*info));
1340 #else
1341 (void) spa, (void) vd, (void) zb, (void) zio, (void) offset,
1342 (void) length, (void) good_data, (void) bad_data, (void) zbc;
1343 #endif
1344 return (rc);
1348 * The 'sysevent.fs.zfs.*' events are signals posted to notify user space of
1349 * change in the pool. All sysevents are listed in sys/sysevent/eventdefs.h
1350 * and are designed to be consumed by the ZFS Event Daemon (ZED). For
1351 * additional details refer to the zed(8) man page.
1353 nvlist_t *
1354 zfs_event_create(spa_t *spa, vdev_t *vd, const char *type, const char *name,
1355 nvlist_t *aux)
1357 nvlist_t *resource = NULL;
1358 #ifdef _KERNEL
1359 char class[64];
1361 if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT)
1362 return (NULL);
1364 if ((resource = fm_nvlist_create(NULL)) == NULL)
1365 return (NULL);
1367 (void) snprintf(class, sizeof (class), "%s.%s.%s", type,
1368 ZFS_ERROR_CLASS, name);
1369 VERIFY0(nvlist_add_uint8(resource, FM_VERSION, FM_RSRC_VERSION));
1370 VERIFY0(nvlist_add_string(resource, FM_CLASS, class));
1371 VERIFY0(nvlist_add_string(resource,
1372 FM_EREPORT_PAYLOAD_ZFS_POOL, spa_name(spa)));
1373 VERIFY0(nvlist_add_uint64(resource,
1374 FM_EREPORT_PAYLOAD_ZFS_POOL_GUID, spa_guid(spa)));
1375 VERIFY0(nvlist_add_uint64(resource,
1376 FM_EREPORT_PAYLOAD_ZFS_POOL_STATE, spa_state(spa)));
1377 VERIFY0(nvlist_add_int32(resource,
1378 FM_EREPORT_PAYLOAD_ZFS_POOL_CONTEXT, spa_load_state(spa)));
1380 if (vd) {
1381 VERIFY0(nvlist_add_uint64(resource,
1382 FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID, vd->vdev_guid));
1383 VERIFY0(nvlist_add_uint64(resource,
1384 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE, vd->vdev_state));
1385 if (vd->vdev_path != NULL)
1386 VERIFY0(nvlist_add_string(resource,
1387 FM_EREPORT_PAYLOAD_ZFS_VDEV_PATH, vd->vdev_path));
1388 if (vd->vdev_devid != NULL)
1389 VERIFY0(nvlist_add_string(resource,
1390 FM_EREPORT_PAYLOAD_ZFS_VDEV_DEVID, vd->vdev_devid));
1391 if (vd->vdev_fru != NULL)
1392 VERIFY0(nvlist_add_string(resource,
1393 FM_EREPORT_PAYLOAD_ZFS_VDEV_FRU, vd->vdev_fru));
1394 if (vd->vdev_enc_sysfs_path != NULL)
1395 VERIFY0(nvlist_add_string(resource,
1396 FM_EREPORT_PAYLOAD_ZFS_VDEV_ENC_SYSFS_PATH,
1397 vd->vdev_enc_sysfs_path));
1400 /* also copy any optional payload data */
1401 if (aux) {
1402 nvpair_t *elem = NULL;
1404 while ((elem = nvlist_next_nvpair(aux, elem)) != NULL)
1405 (void) nvlist_add_nvpair(resource, elem);
1407 #else
1408 (void) spa, (void) vd, (void) type, (void) name, (void) aux;
1409 #endif
1410 return (resource);
1413 static void
1414 zfs_post_common(spa_t *spa, vdev_t *vd, const char *type, const char *name,
1415 nvlist_t *aux)
1417 #ifdef _KERNEL
1418 nvlist_t *resource;
1420 resource = zfs_event_create(spa, vd, type, name, aux);
1421 if (resource)
1422 zfs_zevent_post(resource, NULL, zfs_zevent_post_cb);
1423 #else
1424 (void) spa, (void) vd, (void) type, (void) name, (void) aux;
1425 #endif
1429 * The 'resource.fs.zfs.removed' event is an internal signal that the given vdev
1430 * has been removed from the system. This will cause the DE to ignore any
1431 * recent I/O errors, inferring that they are due to the asynchronous device
1432 * removal.
1434 void
1435 zfs_post_remove(spa_t *spa, vdev_t *vd)
1437 zfs_post_common(spa, vd, FM_RSRC_CLASS, FM_RESOURCE_REMOVED, NULL);
1441 * The 'resource.fs.zfs.autoreplace' event is an internal signal that the pool
1442 * has the 'autoreplace' property set, and therefore any broken vdevs will be
1443 * handled by higher level logic, and no vdev fault should be generated.
1445 void
1446 zfs_post_autoreplace(spa_t *spa, vdev_t *vd)
1448 zfs_post_common(spa, vd, FM_RSRC_CLASS, FM_RESOURCE_AUTOREPLACE, NULL);
1452 * The 'resource.fs.zfs.statechange' event is an internal signal that the
1453 * given vdev has transitioned its state to DEGRADED or HEALTHY. This will
1454 * cause the retire agent to repair any outstanding fault management cases
1455 * open because the device was not found (fault.fs.zfs.device).
1457 void
1458 zfs_post_state_change(spa_t *spa, vdev_t *vd, uint64_t laststate)
1460 #ifdef _KERNEL
1461 nvlist_t *aux;
1464 * Add optional supplemental keys to payload
1466 aux = fm_nvlist_create(NULL);
1467 if (vd && aux) {
1468 if (vd->vdev_physpath) {
1469 fnvlist_add_string(aux,
1470 FM_EREPORT_PAYLOAD_ZFS_VDEV_PHYSPATH,
1471 vd->vdev_physpath);
1473 if (vd->vdev_enc_sysfs_path) {
1474 fnvlist_add_string(aux,
1475 FM_EREPORT_PAYLOAD_ZFS_VDEV_ENC_SYSFS_PATH,
1476 vd->vdev_enc_sysfs_path);
1479 fnvlist_add_uint64(aux,
1480 FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE, laststate);
1483 zfs_post_common(spa, vd, FM_RSRC_CLASS, FM_RESOURCE_STATECHANGE,
1484 aux);
1486 if (aux)
1487 fm_nvlist_destroy(aux, FM_NVA_FREE);
1488 #else
1489 (void) spa, (void) vd, (void) laststate;
1490 #endif
1493 #ifdef _KERNEL
1494 void
1495 zfs_ereport_init(void)
1497 mutex_init(&recent_events_lock, NULL, MUTEX_DEFAULT, NULL);
1498 list_create(&recent_events_list, sizeof (recent_events_node_t),
1499 offsetof(recent_events_node_t, re_list_link));
1500 avl_create(&recent_events_tree, recent_events_compare,
1501 sizeof (recent_events_node_t), offsetof(recent_events_node_t,
1502 re_tree_link));
1506 * This 'early' fini needs to run before zfs_fini() which on Linux waits
1507 * for the system_delay_taskq to drain.
1509 void
1510 zfs_ereport_taskq_fini(void)
1512 mutex_enter(&recent_events_lock);
1513 if (recent_events_cleaner_tqid != 0) {
1514 taskq_cancel_id(system_delay_taskq, recent_events_cleaner_tqid);
1515 recent_events_cleaner_tqid = 0;
1517 mutex_exit(&recent_events_lock);
1520 void
1521 zfs_ereport_fini(void)
1523 recent_events_node_t *entry;
1525 while ((entry = list_remove_head(&recent_events_list)) != NULL) {
1526 avl_remove(&recent_events_tree, entry);
1527 kmem_free(entry, sizeof (*entry));
1529 avl_destroy(&recent_events_tree);
1530 list_destroy(&recent_events_list);
1531 mutex_destroy(&recent_events_lock);
1534 void
1535 zfs_ereport_snapshot_post(const char *subclass, spa_t *spa, const char *name)
1537 nvlist_t *aux;
1539 aux = fm_nvlist_create(NULL);
1540 fnvlist_add_string(aux, FM_EREPORT_PAYLOAD_ZFS_SNAPSHOT_NAME, name);
1542 zfs_post_common(spa, NULL, FM_RSRC_CLASS, subclass, aux);
1543 fm_nvlist_destroy(aux, FM_NVA_FREE);
1547 * Post when a event when a zvol is created or removed
1549 * This is currently only used by macOS, since it uses the event to create
1550 * symlinks between the volume name (mypool/myvol) and the actual /dev
1551 * device (/dev/disk3). For example:
1553 * /var/run/zfs/dsk/mypool/myvol -> /dev/disk3
1555 * name: The full name of the zvol ("mypool/myvol")
1556 * dev_name: The full /dev name for the zvol ("/dev/disk3")
1557 * raw_name: The raw /dev name for the zvol ("/dev/rdisk3")
1559 void
1560 zfs_ereport_zvol_post(const char *subclass, const char *name,
1561 const char *dev_name, const char *raw_name)
1563 nvlist_t *aux;
1564 char *r;
1566 boolean_t locked = mutex_owned(&spa_namespace_lock);
1567 if (!locked) mutex_enter(&spa_namespace_lock);
1568 spa_t *spa = spa_lookup(name);
1569 if (!locked) mutex_exit(&spa_namespace_lock);
1571 if (spa == NULL)
1572 return;
1574 aux = fm_nvlist_create(NULL);
1575 fnvlist_add_string(aux, FM_EREPORT_PAYLOAD_ZFS_DEVICE_NAME, dev_name);
1576 fnvlist_add_string(aux, FM_EREPORT_PAYLOAD_ZFS_RAW_DEVICE_NAME,
1577 raw_name);
1578 r = strchr(name, '/');
1579 if (r && r[1])
1580 fnvlist_add_string(aux, FM_EREPORT_PAYLOAD_ZFS_VOLUME, &r[1]);
1582 zfs_post_common(spa, NULL, FM_RSRC_CLASS, subclass, aux);
1583 fm_nvlist_destroy(aux, FM_NVA_FREE);
1586 EXPORT_SYMBOL(zfs_ereport_post);
1587 EXPORT_SYMBOL(zfs_ereport_is_valid);
1588 EXPORT_SYMBOL(zfs_ereport_post_checksum);
1589 EXPORT_SYMBOL(zfs_post_remove);
1590 EXPORT_SYMBOL(zfs_post_autoreplace);
1591 EXPORT_SYMBOL(zfs_post_state_change);
1593 ZFS_MODULE_PARAM(zfs_zevent, zfs_zevent_, retain_max, UINT, ZMOD_RW,
1594 "Maximum recent zevents records to retain for duplicate checking");
1595 ZFS_MODULE_PARAM(zfs_zevent, zfs_zevent_, retain_expire_secs, UINT, ZMOD_RW,
1596 "Expiration time for recent zevents records");
1597 #endif /* _KERNEL */