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]
22 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
26 * Fault Management Architecture (FMA) Resource and Protocol Support
28 * The routines contained herein provide services to support kernel subsystems
29 * in publishing fault management telemetry (see PSARC 2002/412 and 2003/089).
31 * Name-Value Pair Lists
33 * The embodiment of an FMA protocol element (event, fmri or authority) is a
34 * name-value pair list (nvlist_t). FMA-specific nvlist constructor and
35 * destructor functions, fm_nvlist_create() and fm_nvlist_destroy(), are used
36 * to create an nvpair list using custom allocators. Callers may choose to
37 * allocate either from the kernel memory allocator, or from a preallocated
38 * buffer, useful in constrained contexts like high-level interrupt routines.
40 * Protocol Event and FMRI Construction
42 * Convenience routines are provided to construct nvlist events according to
43 * the FMA Event Protocol and Naming Schema specification for ereports and
44 * FMRIs for the dev, cpu, hc, mem, legacy hc and de schemes.
48 * Routines to generate ENA formats 0, 1 and 2 are available as well as
49 * routines to increment formats 1 and 2. Individual fields within the
50 * ENA are extractable via fm_ena_time_get(), fm_ena_id_get(),
51 * fm_ena_format_get() and fm_ena_gen_get().
54 #include <sys/types.h>
57 #include <sys/nvpair.h>
58 #include <sys/cmn_err.h>
59 #include <sys/sysmacros.h>
60 #include <sys/sunddi.h>
61 #include <sys/systeminfo.h>
62 #include <sys/fm/util.h>
63 #include <sys/fm/protocol.h>
64 #include <sys/kstat.h>
65 #include <sys/zfs_context.h>
67 #include <sys/atomic.h>
68 #include <sys/condvar.h>
69 #include <sys/zfs_ioctl.h>
71 static uint_t zfs_zevent_len_max
= 512;
73 static uint_t zevent_len_cur
= 0;
74 static int zevent_waiters
= 0;
75 static int zevent_flags
= 0;
77 /* Num events rate limited since the last time zfs_zevent_next() was called */
78 static uint64_t ratelimit_dropped
= 0;
81 * The EID (Event IDentifier) is used to uniquely tag a zevent when it is
82 * posted. The posted EIDs are monotonically increasing but not persistent.
83 * They will be reset to the initial value (1) each time the kernel module is
86 static uint64_t zevent_eid
= 0;
88 static kmutex_t zevent_lock
;
89 static list_t zevent_list
;
90 static kcondvar_t zevent_cv
;
95 * Common fault management kstats to record event generation failures
99 kstat_named_t erpt_dropped
; /* num erpts dropped on post */
100 kstat_named_t erpt_set_failed
; /* num erpt set failures */
101 kstat_named_t fmri_set_failed
; /* num fmri set failures */
102 kstat_named_t payload_set_failed
; /* num payload set failures */
103 kstat_named_t erpt_duplicates
; /* num duplicate erpts */
106 static struct erpt_kstat erpt_kstat_data
= {
107 { "erpt-dropped", KSTAT_DATA_UINT64
},
108 { "erpt-set-failed", KSTAT_DATA_UINT64
},
109 { "fmri-set-failed", KSTAT_DATA_UINT64
},
110 { "payload-set-failed", KSTAT_DATA_UINT64
},
111 { "erpt-duplicates", KSTAT_DATA_UINT64
}
119 zfs_zevent_alloc(void)
123 ev
= kmem_zalloc(sizeof (zevent_t
), KM_SLEEP
);
125 list_create(&ev
->ev_ze_list
, sizeof (zfs_zevent_t
),
126 offsetof(zfs_zevent_t
, ze_node
));
127 list_link_init(&ev
->ev_node
);
133 zfs_zevent_free(zevent_t
*ev
)
135 /* Run provided cleanup callback */
136 ev
->ev_cb(ev
->ev_nvl
, ev
->ev_detector
);
138 list_destroy(&ev
->ev_ze_list
);
139 kmem_free(ev
, sizeof (zevent_t
));
143 zfs_zevent_drain(zevent_t
*ev
)
147 ASSERT(MUTEX_HELD(&zevent_lock
));
148 list_remove(&zevent_list
, ev
);
150 /* Remove references to this event in all private file data */
151 while ((ze
= list_remove_head(&ev
->ev_ze_list
)) != NULL
) {
152 ze
->ze_zevent
= NULL
;
160 zfs_zevent_drain_all(uint_t
*count
)
164 mutex_enter(&zevent_lock
);
165 while ((ev
= list_head(&zevent_list
)) != NULL
)
166 zfs_zevent_drain(ev
);
168 *count
= zevent_len_cur
;
170 mutex_exit(&zevent_lock
);
174 * New zevents are inserted at the head. If the maximum queue
175 * length is exceeded a zevent will be drained from the tail.
176 * As part of this any user space processes which currently have
177 * a reference to this zevent_t in their private data will have
178 * this reference set to NULL.
181 zfs_zevent_insert(zevent_t
*ev
)
183 ASSERT(MUTEX_HELD(&zevent_lock
));
184 list_insert_head(&zevent_list
, ev
);
186 if (zevent_len_cur
>= zfs_zevent_len_max
)
187 zfs_zevent_drain(list_tail(&zevent_list
));
193 * Post a zevent. The cb will be called when nvl and detector are no longer
195 * - An error happened and a zevent can't be posted. In this case, cb is called
196 * before zfs_zevent_post() returns.
197 * - The event is being drained and freed.
200 zfs_zevent_post(nvlist_t
*nvl
, nvlist_t
*detector
, zevent_cb_t
*cb
)
212 tv_array
[0] = tv
.tv_sec
;
213 tv_array
[1] = tv
.tv_nsec
;
215 error
= nvlist_add_int64_array(nvl
, FM_EREPORT_TIME
, tv_array
, 2);
217 atomic_inc_64(&erpt_kstat_data
.erpt_set_failed
.value
.ui64
);
221 eid
= atomic_inc_64_nv(&zevent_eid
);
222 error
= nvlist_add_uint64(nvl
, FM_EREPORT_EID
, eid
);
224 atomic_inc_64(&erpt_kstat_data
.erpt_set_failed
.value
.ui64
);
228 error
= nvlist_size(nvl
, &nvl_size
, NV_ENCODE_NATIVE
);
230 atomic_inc_64(&erpt_kstat_data
.erpt_dropped
.value
.ui64
);
234 if (nvl_size
> ERPT_DATA_SZ
|| nvl_size
== 0) {
235 atomic_inc_64(&erpt_kstat_data
.erpt_dropped
.value
.ui64
);
240 ev
= zfs_zevent_alloc();
242 atomic_inc_64(&erpt_kstat_data
.erpt_dropped
.value
.ui64
);
248 ev
->ev_detector
= detector
;
252 mutex_enter(&zevent_lock
);
253 zfs_zevent_insert(ev
);
254 cv_broadcast(&zevent_cv
);
255 mutex_exit(&zevent_lock
);
265 zfs_zevent_track_duplicate(void)
267 atomic_inc_64(&erpt_kstat_data
.erpt_duplicates
.value
.ui64
);
271 zfs_zevent_minor_to_state(minor_t minor
, zfs_zevent_t
**ze
)
273 *ze
= zfsdev_get_state(minor
, ZST_ZEVENT
);
275 return (SET_ERROR(EBADF
));
281 zfs_zevent_fd_hold(int fd
, minor_t
*minorp
, zfs_zevent_t
**ze
)
283 zfs_file_t
*fp
= zfs_file_get(fd
);
287 int error
= zfsdev_getminor(fp
, minorp
);
289 error
= zfs_zevent_minor_to_state(*minorp
, ze
);
292 zfs_zevent_fd_rele(fp
);
300 zfs_zevent_fd_rele(zfs_file_t
*fp
)
306 * Get the next zevent in the stream and place a copy in 'event'. This
307 * may fail with ENOMEM if the encoded nvlist size exceeds the passed
308 * 'event_size'. In this case the stream pointer is not advanced and
309 * and 'event_size' is set to the minimum required buffer size.
312 zfs_zevent_next(zfs_zevent_t
*ze
, nvlist_t
**event
, uint64_t *event_size
,
319 mutex_enter(&zevent_lock
);
320 if (ze
->ze_zevent
== NULL
) {
321 /* New stream start at the beginning/tail */
322 ev
= list_tail(&zevent_list
);
329 * Existing stream continue with the next element and remove
330 * ourselves from the wait queue for the previous element
332 ev
= list_prev(&zevent_list
, ze
->ze_zevent
);
339 VERIFY(nvlist_size(ev
->ev_nvl
, &size
, NV_ENCODE_NATIVE
) == 0);
340 if (size
> *event_size
) {
347 list_remove(&ze
->ze_zevent
->ev_ze_list
, ze
);
350 list_insert_head(&ev
->ev_ze_list
, ze
);
351 (void) nvlist_dup(ev
->ev_nvl
, event
, KM_SLEEP
);
352 *dropped
= ze
->ze_dropped
;
355 /* Include events dropped due to rate limiting */
356 *dropped
+= atomic_swap_64(&ratelimit_dropped
, 0);
360 mutex_exit(&zevent_lock
);
366 * Wait in an interruptible state for any new events.
369 zfs_zevent_wait(zfs_zevent_t
*ze
)
373 mutex_enter(&zevent_lock
);
376 while (error
== EAGAIN
) {
377 if (zevent_flags
& ZEVENT_SHUTDOWN
) {
378 error
= SET_ERROR(ESHUTDOWN
);
382 if (cv_wait_sig(&zevent_cv
, &zevent_lock
) == 0) {
383 error
= SET_ERROR(EINTR
);
385 } else if (!list_is_empty(&zevent_list
)) {
394 mutex_exit(&zevent_lock
);
400 * The caller may seek to a specific EID by passing that EID. If the EID
401 * is still available in the posted list of events the cursor is positioned
402 * there. Otherwise ENOENT is returned and the cursor is not moved.
404 * There are two reserved EIDs which may be passed and will never fail.
405 * ZEVENT_SEEK_START positions the cursor at the start of the list, and
406 * ZEVENT_SEEK_END positions the cursor at the end of the list.
409 zfs_zevent_seek(zfs_zevent_t
*ze
, uint64_t eid
)
414 mutex_enter(&zevent_lock
);
416 if (eid
== ZEVENT_SEEK_START
) {
418 list_remove(&ze
->ze_zevent
->ev_ze_list
, ze
);
420 ze
->ze_zevent
= NULL
;
424 if (eid
== ZEVENT_SEEK_END
) {
426 list_remove(&ze
->ze_zevent
->ev_ze_list
, ze
);
428 ev
= list_head(&zevent_list
);
431 list_insert_head(&ev
->ev_ze_list
, ze
);
433 ze
->ze_zevent
= NULL
;
439 for (ev
= list_tail(&zevent_list
); ev
!= NULL
;
440 ev
= list_prev(&zevent_list
, ev
)) {
441 if (ev
->ev_eid
== eid
) {
443 list_remove(&ze
->ze_zevent
->ev_ze_list
, ze
);
446 list_insert_head(&ev
->ev_ze_list
, ze
);
455 mutex_exit(&zevent_lock
);
461 zfs_zevent_init(zfs_zevent_t
**zep
)
465 ze
= *zep
= kmem_zalloc(sizeof (zfs_zevent_t
), KM_SLEEP
);
466 list_link_init(&ze
->ze_node
);
470 zfs_zevent_destroy(zfs_zevent_t
*ze
)
472 mutex_enter(&zevent_lock
);
474 list_remove(&ze
->ze_zevent
->ev_ze_list
, ze
);
475 mutex_exit(&zevent_lock
);
477 kmem_free(ze
, sizeof (zfs_zevent_t
));
482 * Wrappers for FM nvlist allocators
485 i_fm_alloc(nv_alloc_t
*nva
, size_t size
)
488 return (kmem_alloc(size
, KM_SLEEP
));
492 i_fm_free(nv_alloc_t
*nva
, void *buf
, size_t size
)
495 kmem_free(buf
, size
);
498 static const nv_alloc_ops_t fm_mem_alloc_ops
= {
501 .nv_ao_alloc
= i_fm_alloc
,
502 .nv_ao_free
= i_fm_free
,
507 * Create and initialize a new nv_alloc_t for a fixed buffer, buf. A pointer
508 * to the newly allocated nv_alloc_t structure is returned upon success or NULL
509 * is returned to indicate that the nv_alloc structure could not be created.
512 fm_nva_xcreate(char *buf
, size_t bufsz
)
514 nv_alloc_t
*nvhdl
= kmem_zalloc(sizeof (nv_alloc_t
), KM_SLEEP
);
516 if (bufsz
== 0 || nv_alloc_init(nvhdl
, nv_fixed_ops
, buf
, bufsz
) != 0) {
517 kmem_free(nvhdl
, sizeof (nv_alloc_t
));
525 * Destroy a previously allocated nv_alloc structure. The fixed buffer
526 * associated with nva must be freed by the caller.
529 fm_nva_xdestroy(nv_alloc_t
*nva
)
532 kmem_free(nva
, sizeof (nv_alloc_t
));
536 * Create a new nv list. A pointer to a new nv list structure is returned
537 * upon success or NULL is returned to indicate that the structure could
538 * not be created. The newly created nv list is created and managed by the
539 * operations installed in nva. If nva is NULL, the default FMA nva
540 * operations are installed and used.
542 * When called from the kernel and nva == NULL, this function must be called
543 * from passive kernel context with no locks held that can prevent a
544 * sleeping memory allocation from occurring. Otherwise, this function may
545 * be called from other kernel contexts as long a valid nva created via
546 * fm_nva_create() is supplied.
549 fm_nvlist_create(nv_alloc_t
*nva
)
556 nvhdl
= kmem_zalloc(sizeof (nv_alloc_t
), KM_SLEEP
);
558 if (nv_alloc_init(nvhdl
, &fm_mem_alloc_ops
, NULL
, 0) != 0) {
559 kmem_free(nvhdl
, sizeof (nv_alloc_t
));
567 if (nvlist_xalloc(&nvl
, NV_UNIQUE_NAME
, nvhdl
) != 0) {
569 nv_alloc_fini(nvhdl
);
570 kmem_free(nvhdl
, sizeof (nv_alloc_t
));
579 * Destroy a previously allocated nvlist structure. flag indicates whether
580 * or not the associated nva structure should be freed (FM_NVA_FREE) or
581 * retained (FM_NVA_RETAIN). Retaining the nv alloc structure allows
582 * it to be re-used for future nvlist creation operations.
585 fm_nvlist_destroy(nvlist_t
*nvl
, int flag
)
587 nv_alloc_t
*nva
= nvlist_lookup_nv_alloc(nvl
);
592 if (flag
== FM_NVA_FREE
)
593 fm_nva_xdestroy(nva
);
598 i_fm_payload_set(nvlist_t
*payload
, const char *name
, va_list ap
)
603 while (ret
== 0 && name
!= NULL
) {
604 type
= va_arg(ap
, data_type_t
);
607 ret
= nvlist_add_byte(payload
, name
,
610 case DATA_TYPE_BYTE_ARRAY
:
611 nelem
= va_arg(ap
, int);
612 ret
= nvlist_add_byte_array(payload
, name
,
613 va_arg(ap
, uchar_t
*), nelem
);
615 case DATA_TYPE_BOOLEAN_VALUE
:
616 ret
= nvlist_add_boolean_value(payload
, name
,
617 va_arg(ap
, boolean_t
));
619 case DATA_TYPE_BOOLEAN_ARRAY
:
620 nelem
= va_arg(ap
, int);
621 ret
= nvlist_add_boolean_array(payload
, name
,
622 va_arg(ap
, boolean_t
*), nelem
);
625 ret
= nvlist_add_int8(payload
, name
,
628 case DATA_TYPE_INT8_ARRAY
:
629 nelem
= va_arg(ap
, int);
630 ret
= nvlist_add_int8_array(payload
, name
,
631 va_arg(ap
, int8_t *), nelem
);
633 case DATA_TYPE_UINT8
:
634 ret
= nvlist_add_uint8(payload
, name
,
637 case DATA_TYPE_UINT8_ARRAY
:
638 nelem
= va_arg(ap
, int);
639 ret
= nvlist_add_uint8_array(payload
, name
,
640 va_arg(ap
, uint8_t *), nelem
);
642 case DATA_TYPE_INT16
:
643 ret
= nvlist_add_int16(payload
, name
,
646 case DATA_TYPE_INT16_ARRAY
:
647 nelem
= va_arg(ap
, int);
648 ret
= nvlist_add_int16_array(payload
, name
,
649 va_arg(ap
, int16_t *), nelem
);
651 case DATA_TYPE_UINT16
:
652 ret
= nvlist_add_uint16(payload
, name
,
655 case DATA_TYPE_UINT16_ARRAY
:
656 nelem
= va_arg(ap
, int);
657 ret
= nvlist_add_uint16_array(payload
, name
,
658 va_arg(ap
, uint16_t *), nelem
);
660 case DATA_TYPE_INT32
:
661 ret
= nvlist_add_int32(payload
, name
,
662 va_arg(ap
, int32_t));
664 case DATA_TYPE_INT32_ARRAY
:
665 nelem
= va_arg(ap
, int);
666 ret
= nvlist_add_int32_array(payload
, name
,
667 va_arg(ap
, int32_t *), nelem
);
669 case DATA_TYPE_UINT32
:
670 ret
= nvlist_add_uint32(payload
, name
,
671 va_arg(ap
, uint32_t));
673 case DATA_TYPE_UINT32_ARRAY
:
674 nelem
= va_arg(ap
, int);
675 ret
= nvlist_add_uint32_array(payload
, name
,
676 va_arg(ap
, uint32_t *), nelem
);
678 case DATA_TYPE_INT64
:
679 ret
= nvlist_add_int64(payload
, name
,
680 va_arg(ap
, int64_t));
682 case DATA_TYPE_INT64_ARRAY
:
683 nelem
= va_arg(ap
, int);
684 ret
= nvlist_add_int64_array(payload
, name
,
685 va_arg(ap
, int64_t *), nelem
);
687 case DATA_TYPE_UINT64
:
688 ret
= nvlist_add_uint64(payload
, name
,
689 va_arg(ap
, uint64_t));
691 case DATA_TYPE_UINT64_ARRAY
:
692 nelem
= va_arg(ap
, int);
693 ret
= nvlist_add_uint64_array(payload
, name
,
694 va_arg(ap
, uint64_t *), nelem
);
696 case DATA_TYPE_STRING
:
697 ret
= nvlist_add_string(payload
, name
,
700 case DATA_TYPE_STRING_ARRAY
:
701 nelem
= va_arg(ap
, int);
702 ret
= nvlist_add_string_array(payload
, name
,
703 va_arg(ap
, const char **), nelem
);
705 case DATA_TYPE_NVLIST
:
706 ret
= nvlist_add_nvlist(payload
, name
,
707 va_arg(ap
, nvlist_t
*));
709 case DATA_TYPE_NVLIST_ARRAY
:
710 nelem
= va_arg(ap
, int);
711 ret
= nvlist_add_nvlist_array(payload
, name
,
712 va_arg(ap
, const nvlist_t
**), nelem
);
718 name
= va_arg(ap
, char *);
724 fm_payload_set(nvlist_t
*payload
, ...)
730 va_start(ap
, payload
);
731 name
= va_arg(ap
, char *);
732 ret
= i_fm_payload_set(payload
, name
, ap
);
736 atomic_inc_64(&erpt_kstat_data
.payload_set_failed
.value
.ui64
);
740 * Set-up and validate the members of an ereport event according to:
742 * Member name Type Value
743 * ====================================================
744 * class string ereport
747 * detector nvlist_t <detector>
748 * ereport-payload nvlist_t <var args>
750 * We don't actually add a 'version' member to the payload. Really,
751 * the version quoted to us by our caller is that of the category 1
752 * "ereport" event class (and we require FM_EREPORT_VERS0) but
753 * the payload version of the actual leaf class event under construction
754 * may be something else. Callers should supply a version in the varargs,
755 * or (better) we could take two version arguments - one for the
756 * ereport category 1 classification (expect FM_EREPORT_VERS0) and one
757 * for the leaf class.
760 fm_ereport_set(nvlist_t
*ereport
, int version
, const char *erpt_class
,
761 uint64_t ena
, const nvlist_t
*detector
, ...)
763 char ereport_class
[FM_MAX_CLASS
];
768 if (version
!= FM_EREPORT_VERS0
) {
769 atomic_inc_64(&erpt_kstat_data
.erpt_set_failed
.value
.ui64
);
773 (void) snprintf(ereport_class
, FM_MAX_CLASS
, "%s.%s",
774 FM_EREPORT_CLASS
, erpt_class
);
775 if (nvlist_add_string(ereport
, FM_CLASS
, ereport_class
) != 0) {
776 atomic_inc_64(&erpt_kstat_data
.erpt_set_failed
.value
.ui64
);
780 if (nvlist_add_uint64(ereport
, FM_EREPORT_ENA
, ena
)) {
781 atomic_inc_64(&erpt_kstat_data
.erpt_set_failed
.value
.ui64
);
784 if (nvlist_add_nvlist(ereport
, FM_EREPORT_DETECTOR
,
785 (nvlist_t
*)detector
) != 0) {
786 atomic_inc_64(&erpt_kstat_data
.erpt_set_failed
.value
.ui64
);
789 va_start(ap
, detector
);
790 name
= va_arg(ap
, const char *);
791 ret
= i_fm_payload_set(ereport
, name
, ap
);
795 atomic_inc_64(&erpt_kstat_data
.erpt_set_failed
.value
.ui64
);
799 * Set-up and validate the members of an hc fmri according to;
801 * Member name Type Value
802 * ===================================================
804 * auth nvlist_t <auth>
805 * hc-name string <name>
808 * Note that auth and hc-id are optional members.
811 #define HC_MAXPAIRS 20
812 #define HC_MAXNAMELEN 50
815 fm_fmri_hc_set_common(nvlist_t
*fmri
, int version
, const nvlist_t
*auth
)
817 if (version
!= FM_HC_SCHEME_VERSION
) {
818 atomic_inc_64(&erpt_kstat_data
.fmri_set_failed
.value
.ui64
);
822 if (nvlist_add_uint8(fmri
, FM_VERSION
, version
) != 0 ||
823 nvlist_add_string(fmri
, FM_FMRI_SCHEME
, FM_FMRI_SCHEME_HC
) != 0) {
824 atomic_inc_64(&erpt_kstat_data
.fmri_set_failed
.value
.ui64
);
828 if (auth
!= NULL
&& nvlist_add_nvlist(fmri
, FM_FMRI_AUTHORITY
,
829 (nvlist_t
*)auth
) != 0) {
830 atomic_inc_64(&erpt_kstat_data
.fmri_set_failed
.value
.ui64
);
838 fm_fmri_hc_set(nvlist_t
*fmri
, int version
, const nvlist_t
*auth
,
839 nvlist_t
*snvl
, int npairs
, ...)
841 nv_alloc_t
*nva
= nvlist_lookup_nv_alloc(fmri
);
842 nvlist_t
*pairs
[HC_MAXPAIRS
];
846 if (!fm_fmri_hc_set_common(fmri
, version
, auth
))
849 npairs
= MIN(npairs
, HC_MAXPAIRS
);
851 va_start(ap
, npairs
);
852 for (i
= 0; i
< npairs
; i
++) {
853 const char *name
= va_arg(ap
, const char *);
854 uint32_t id
= va_arg(ap
, uint32_t);
857 (void) snprintf(idstr
, sizeof (idstr
), "%u", id
);
859 pairs
[i
] = fm_nvlist_create(nva
);
860 if (nvlist_add_string(pairs
[i
], FM_FMRI_HC_NAME
, name
) != 0 ||
861 nvlist_add_string(pairs
[i
], FM_FMRI_HC_ID
, idstr
) != 0) {
863 &erpt_kstat_data
.fmri_set_failed
.value
.ui64
);
868 if (nvlist_add_nvlist_array(fmri
, FM_FMRI_HC_LIST
,
869 (const nvlist_t
**)pairs
, npairs
) != 0) {
870 atomic_inc_64(&erpt_kstat_data
.fmri_set_failed
.value
.ui64
);
873 for (i
= 0; i
< npairs
; i
++)
874 fm_nvlist_destroy(pairs
[i
], FM_NVA_RETAIN
);
877 if (nvlist_add_nvlist(fmri
, FM_FMRI_HC_SPECIFIC
, snvl
) != 0) {
879 &erpt_kstat_data
.fmri_set_failed
.value
.ui64
);
885 fm_fmri_hc_create(nvlist_t
*fmri
, int version
, const nvlist_t
*auth
,
886 nvlist_t
*snvl
, nvlist_t
*bboard
, int npairs
, ...)
888 nv_alloc_t
*nva
= nvlist_lookup_nv_alloc(fmri
);
889 nvlist_t
*pairs
[HC_MAXPAIRS
];
894 const char *hcname
, *hcid
;
896 if (!fm_fmri_hc_set_common(fmri
, version
, auth
))
900 * copy the bboard nvpairs to the pairs array
902 if (nvlist_lookup_nvlist_array(bboard
, FM_FMRI_HC_LIST
, &hcl
, &n
)
904 atomic_inc_64(&erpt_kstat_data
.fmri_set_failed
.value
.ui64
);
908 for (i
= 0; i
< n
; i
++) {
909 if (nvlist_lookup_string(hcl
[i
], FM_FMRI_HC_NAME
,
912 &erpt_kstat_data
.fmri_set_failed
.value
.ui64
);
915 if (nvlist_lookup_string(hcl
[i
], FM_FMRI_HC_ID
, &hcid
) != 0) {
917 &erpt_kstat_data
.fmri_set_failed
.value
.ui64
);
921 pairs
[i
] = fm_nvlist_create(nva
);
922 if (nvlist_add_string(pairs
[i
], FM_FMRI_HC_NAME
, hcname
) != 0 ||
923 nvlist_add_string(pairs
[i
], FM_FMRI_HC_ID
, hcid
) != 0) {
924 for (j
= 0; j
<= i
; j
++) {
925 if (pairs
[j
] != NULL
)
926 fm_nvlist_destroy(pairs
[j
],
930 &erpt_kstat_data
.fmri_set_failed
.value
.ui64
);
936 * create the pairs from passed in pairs
938 npairs
= MIN(npairs
, HC_MAXPAIRS
);
940 va_start(ap
, npairs
);
941 for (i
= n
; i
< npairs
+ n
; i
++) {
942 const char *name
= va_arg(ap
, const char *);
943 uint32_t id
= va_arg(ap
, uint32_t);
945 (void) snprintf(idstr
, sizeof (idstr
), "%u", id
);
946 pairs
[i
] = fm_nvlist_create(nva
);
947 if (nvlist_add_string(pairs
[i
], FM_FMRI_HC_NAME
, name
) != 0 ||
948 nvlist_add_string(pairs
[i
], FM_FMRI_HC_ID
, idstr
) != 0) {
949 for (j
= 0; j
<= i
; j
++) {
950 if (pairs
[j
] != NULL
)
951 fm_nvlist_destroy(pairs
[j
],
955 &erpt_kstat_data
.fmri_set_failed
.value
.ui64
);
963 * Create the fmri hc list
965 if (nvlist_add_nvlist_array(fmri
, FM_FMRI_HC_LIST
,
966 (const nvlist_t
**)pairs
, npairs
+ n
) != 0) {
967 atomic_inc_64(&erpt_kstat_data
.fmri_set_failed
.value
.ui64
);
971 for (i
= 0; i
< npairs
+ n
; i
++) {
972 fm_nvlist_destroy(pairs
[i
], FM_NVA_RETAIN
);
976 if (nvlist_add_nvlist(fmri
, FM_FMRI_HC_SPECIFIC
, snvl
) != 0) {
978 &erpt_kstat_data
.fmri_set_failed
.value
.ui64
);
985 * Set-up and validate the members of an dev fmri according to:
987 * Member name Type Value
988 * ====================================================
990 * auth nvlist_t <auth>
991 * devpath string <devpath>
992 * [devid] string <devid>
993 * [target-port-l0id] string <target-port-lun0-id>
995 * Note that auth and devid are optional members.
998 fm_fmri_dev_set(nvlist_t
*fmri_dev
, int version
, const nvlist_t
*auth
,
999 const char *devpath
, const char *devid
, const char *tpl0
)
1003 if (version
!= DEV_SCHEME_VERSION0
) {
1004 atomic_inc_64(&erpt_kstat_data
.fmri_set_failed
.value
.ui64
);
1008 err
|= nvlist_add_uint8(fmri_dev
, FM_VERSION
, version
);
1009 err
|= nvlist_add_string(fmri_dev
, FM_FMRI_SCHEME
, FM_FMRI_SCHEME_DEV
);
1012 err
|= nvlist_add_nvlist(fmri_dev
, FM_FMRI_AUTHORITY
,
1016 err
|= nvlist_add_string(fmri_dev
, FM_FMRI_DEV_PATH
, devpath
);
1019 err
|= nvlist_add_string(fmri_dev
, FM_FMRI_DEV_ID
, devid
);
1022 err
|= nvlist_add_string(fmri_dev
, FM_FMRI_DEV_TGTPTLUN0
, tpl0
);
1025 atomic_inc_64(&erpt_kstat_data
.fmri_set_failed
.value
.ui64
);
1030 * Set-up and validate the members of an cpu fmri according to:
1032 * Member name Type Value
1033 * ====================================================
1035 * auth nvlist_t <auth>
1036 * cpuid uint32_t <cpu_id>
1037 * cpumask uint8_t <cpu_mask>
1038 * serial uint64_t <serial_id>
1040 * Note that auth, cpumask, serial are optional members.
1044 fm_fmri_cpu_set(nvlist_t
*fmri_cpu
, int version
, const nvlist_t
*auth
,
1045 uint32_t cpu_id
, uint8_t *cpu_maskp
, const char *serial_idp
)
1047 uint64_t *failedp
= &erpt_kstat_data
.fmri_set_failed
.value
.ui64
;
1049 if (version
< CPU_SCHEME_VERSION1
) {
1050 atomic_inc_64(failedp
);
1054 if (nvlist_add_uint8(fmri_cpu
, FM_VERSION
, version
) != 0) {
1055 atomic_inc_64(failedp
);
1059 if (nvlist_add_string(fmri_cpu
, FM_FMRI_SCHEME
,
1060 FM_FMRI_SCHEME_CPU
) != 0) {
1061 atomic_inc_64(failedp
);
1065 if (auth
!= NULL
&& nvlist_add_nvlist(fmri_cpu
, FM_FMRI_AUTHORITY
,
1066 (nvlist_t
*)auth
) != 0)
1067 atomic_inc_64(failedp
);
1069 if (nvlist_add_uint32(fmri_cpu
, FM_FMRI_CPU_ID
, cpu_id
) != 0)
1070 atomic_inc_64(failedp
);
1072 if (cpu_maskp
!= NULL
&& nvlist_add_uint8(fmri_cpu
, FM_FMRI_CPU_MASK
,
1074 atomic_inc_64(failedp
);
1076 if (serial_idp
== NULL
|| nvlist_add_string(fmri_cpu
,
1077 FM_FMRI_CPU_SERIAL_ID
, (char *)serial_idp
) != 0)
1078 atomic_inc_64(failedp
);
1082 * Set-up and validate the members of a mem according to:
1084 * Member name Type Value
1085 * ====================================================
1087 * auth nvlist_t <auth> [optional]
1088 * unum string <unum>
1089 * serial string <serial> [optional*]
1090 * offset uint64_t <offset> [optional]
1092 * * serial is required if offset is present
1095 fm_fmri_mem_set(nvlist_t
*fmri
, int version
, const nvlist_t
*auth
,
1096 const char *unum
, const char *serial
, uint64_t offset
)
1098 if (version
!= MEM_SCHEME_VERSION0
) {
1099 atomic_inc_64(&erpt_kstat_data
.fmri_set_failed
.value
.ui64
);
1103 if (!serial
&& (offset
!= (uint64_t)-1)) {
1104 atomic_inc_64(&erpt_kstat_data
.fmri_set_failed
.value
.ui64
);
1108 if (nvlist_add_uint8(fmri
, FM_VERSION
, version
) != 0) {
1109 atomic_inc_64(&erpt_kstat_data
.fmri_set_failed
.value
.ui64
);
1113 if (nvlist_add_string(fmri
, FM_FMRI_SCHEME
, FM_FMRI_SCHEME_MEM
) != 0) {
1114 atomic_inc_64(&erpt_kstat_data
.fmri_set_failed
.value
.ui64
);
1119 if (nvlist_add_nvlist(fmri
, FM_FMRI_AUTHORITY
,
1120 (nvlist_t
*)auth
) != 0) {
1122 &erpt_kstat_data
.fmri_set_failed
.value
.ui64
);
1126 if (nvlist_add_string(fmri
, FM_FMRI_MEM_UNUM
, unum
) != 0) {
1127 atomic_inc_64(&erpt_kstat_data
.fmri_set_failed
.value
.ui64
);
1130 if (serial
!= NULL
) {
1131 if (nvlist_add_string_array(fmri
, FM_FMRI_MEM_SERIAL_ID
,
1132 (const char **)&serial
, 1) != 0) {
1134 &erpt_kstat_data
.fmri_set_failed
.value
.ui64
);
1136 if (offset
!= (uint64_t)-1 && nvlist_add_uint64(fmri
,
1137 FM_FMRI_MEM_OFFSET
, offset
) != 0) {
1139 &erpt_kstat_data
.fmri_set_failed
.value
.ui64
);
1145 fm_fmri_zfs_set(nvlist_t
*fmri
, int version
, uint64_t pool_guid
,
1148 if (version
!= ZFS_SCHEME_VERSION0
) {
1149 atomic_inc_64(&erpt_kstat_data
.fmri_set_failed
.value
.ui64
);
1153 if (nvlist_add_uint8(fmri
, FM_VERSION
, version
) != 0) {
1154 atomic_inc_64(&erpt_kstat_data
.fmri_set_failed
.value
.ui64
);
1158 if (nvlist_add_string(fmri
, FM_FMRI_SCHEME
, FM_FMRI_SCHEME_ZFS
) != 0) {
1159 atomic_inc_64(&erpt_kstat_data
.fmri_set_failed
.value
.ui64
);
1163 if (nvlist_add_uint64(fmri
, FM_FMRI_ZFS_POOL
, pool_guid
) != 0) {
1164 atomic_inc_64(&erpt_kstat_data
.fmri_set_failed
.value
.ui64
);
1167 if (vdev_guid
!= 0) {
1168 if (nvlist_add_uint64(fmri
, FM_FMRI_ZFS_VDEV
, vdev_guid
) != 0) {
1170 &erpt_kstat_data
.fmri_set_failed
.value
.ui64
);
1176 fm_ena_increment(uint64_t ena
)
1180 switch (ENA_FORMAT(ena
)) {
1182 new_ena
= ena
+ (1 << ENA_FMT1_GEN_SHFT
);
1185 new_ena
= ena
+ (1 << ENA_FMT2_GEN_SHFT
);
1195 fm_ena_generate_cpu(uint64_t timestamp
, processorid_t cpuid
, uchar_t format
)
1202 ena
= (uint64_t)((format
& ENA_FORMAT_MASK
) |
1203 ((cpuid
<< ENA_FMT1_CPUID_SHFT
) &
1204 ENA_FMT1_CPUID_MASK
) |
1205 ((timestamp
<< ENA_FMT1_TIME_SHFT
) &
1206 ENA_FMT1_TIME_MASK
));
1208 ena
= (uint64_t)((format
& ENA_FORMAT_MASK
) |
1209 ((cpuid
<< ENA_FMT1_CPUID_SHFT
) &
1210 ENA_FMT1_CPUID_MASK
) |
1211 ((gethrtime() << ENA_FMT1_TIME_SHFT
) &
1212 ENA_FMT1_TIME_MASK
));
1216 ena
= (uint64_t)((format
& ENA_FORMAT_MASK
) |
1217 ((timestamp
<< ENA_FMT2_TIME_SHFT
) & ENA_FMT2_TIME_MASK
));
1227 fm_ena_generate(uint64_t timestamp
, uchar_t format
)
1232 ena
= fm_ena_generate_cpu(timestamp
, getcpuid(), format
);
1239 fm_ena_generation_get(uint64_t ena
)
1243 switch (ENA_FORMAT(ena
)) {
1245 gen
= (ena
& ENA_FMT1_GEN_MASK
) >> ENA_FMT1_GEN_SHFT
;
1248 gen
= (ena
& ENA_FMT2_GEN_MASK
) >> ENA_FMT2_GEN_SHFT
;
1259 fm_ena_format_get(uint64_t ena
)
1262 return (ENA_FORMAT(ena
));
1266 fm_ena_id_get(uint64_t ena
)
1270 switch (ENA_FORMAT(ena
)) {
1272 id
= (ena
& ENA_FMT1_ID_MASK
) >> ENA_FMT1_ID_SHFT
;
1275 id
= (ena
& ENA_FMT2_ID_MASK
) >> ENA_FMT2_ID_SHFT
;
1285 fm_ena_time_get(uint64_t ena
)
1289 switch (ENA_FORMAT(ena
)) {
1291 time
= (ena
& ENA_FMT1_TIME_MASK
) >> ENA_FMT1_TIME_SHFT
;
1294 time
= (ena
& ENA_FMT2_TIME_MASK
) >> ENA_FMT2_TIME_SHFT
;
1305 * Helper function to increment ereport dropped count. Used by the event
1306 * rate limiting code to give feedback to the user about how many events were
1307 * rate limited by including them in the 'dropped' count.
1310 fm_erpt_dropped_increment(void)
1312 atomic_inc_64(&ratelimit_dropped
);
1321 /* Initialize zevent allocation and generation kstats */
1322 fm_ksp
= kstat_create("zfs", 0, "fm", "misc", KSTAT_TYPE_NAMED
,
1323 sizeof (struct erpt_kstat
) / sizeof (kstat_named_t
),
1324 KSTAT_FLAG_VIRTUAL
);
1326 if (fm_ksp
!= NULL
) {
1327 fm_ksp
->ks_data
= &erpt_kstat_data
;
1328 kstat_install(fm_ksp
);
1330 cmn_err(CE_NOTE
, "failed to create fm/misc kstat\n");
1333 mutex_init(&zevent_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1334 list_create(&zevent_list
, sizeof (zevent_t
),
1335 offsetof(zevent_t
, ev_node
));
1336 cv_init(&zevent_cv
, NULL
, CV_DEFAULT
, NULL
);
1348 zfs_zevent_drain_all(&count
);
1350 mutex_enter(&zevent_lock
);
1351 cv_broadcast(&zevent_cv
);
1353 zevent_flags
|= ZEVENT_SHUTDOWN
;
1354 while (zevent_waiters
> 0) {
1355 mutex_exit(&zevent_lock
);
1356 kpreempt(KPREEMPT_SYNC
);
1357 mutex_enter(&zevent_lock
);
1359 mutex_exit(&zevent_lock
);
1361 cv_destroy(&zevent_cv
);
1362 list_destroy(&zevent_list
);
1363 mutex_destroy(&zevent_lock
);
1365 if (fm_ksp
!= NULL
) {
1366 kstat_delete(fm_ksp
);
1370 #endif /* _KERNEL */
1372 ZFS_MODULE_PARAM(zfs_zevent
, zfs_zevent_
, len_max
, UINT
, ZMOD_RW
,
1373 "Max event queue length");