]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - zfs/module/zfs/fm.c
UBUNTU: Ubuntu-4.10.0-37.41
[mirror_ubuntu-zesty-kernel.git] / zfs / module / zfs / fm.c
CommitLineData
7bdf406d
TG
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
23 */
24
25/*
26 * Fault Management Architecture (FMA) Resource and Protocol Support
27 *
28 * The routines contained herein provide services to support kernel subsystems
29 * in publishing fault management telemetry (see PSARC 2002/412 and 2003/089).
30 *
31 * Name-Value Pair Lists
32 *
33 * The embodiment of an FMA protocol element (event, fmri or authority) is a
34 * name-value pair list (nvlist_t). FMA-specific nvlist construtor 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.
39 *
40 * Protocol Event and FMRI Construction
41 *
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.
45 *
46 * ENA Manipulation
47 *
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().
52 */
53
54#include <sys/types.h>
55#include <sys/time.h>
56#include <sys/list.h>
57#include <sys/nvpair.h>
58#include <sys/cmn_err.h>
59#include <sys/sysmacros.h>
60#include <sys/compress.h>
61#include <sys/sunddi.h>
62#include <sys/systeminfo.h>
63#include <sys/fm/util.h>
64#include <sys/fm/protocol.h>
65#include <sys/kstat.h>
66#include <sys/zfs_context.h>
67#ifdef _KERNEL
68#include <sys/atomic.h>
69#include <sys/condvar.h>
70#include <sys/cpuvar.h>
71#include <sys/systm.h>
72#include <sys/dumphdr.h>
73#include <sys/cpuvar.h>
74#include <sys/console.h>
75#include <sys/kobj.h>
76#include <sys/time.h>
77#include <sys/zfs_ioctl.h>
78
79int zfs_zevent_len_max = 0;
80int zfs_zevent_cols = 80;
81int zfs_zevent_console = 0;
82
83static int zevent_len_cur = 0;
84static int zevent_waiters = 0;
85static int zevent_flags = 0;
86
87/*
88 * The EID (Event IDentifier) is used to uniquely tag a zevent when it is
89 * posted. The posted EIDs are monotonically increasing but not persistent.
90 * They will be reset to the initial value (1) each time the kernel module is
91 * loaded.
92 */
93static uint64_t zevent_eid = 0;
94
95static kmutex_t zevent_lock;
96static list_t zevent_list;
97static kcondvar_t zevent_cv;
98#endif /* _KERNEL */
99
0bd31011 100extern void fastreboot_disable_highpil(void);
7bdf406d
TG
101
102/*
103 * Common fault management kstats to record event generation failures
104 */
105
106struct erpt_kstat {
107 kstat_named_t erpt_dropped; /* num erpts dropped on post */
108 kstat_named_t erpt_set_failed; /* num erpt set failures */
109 kstat_named_t fmri_set_failed; /* num fmri set failures */
110 kstat_named_t payload_set_failed; /* num payload set failures */
111};
112
113static struct erpt_kstat erpt_kstat_data = {
114 { "erpt-dropped", KSTAT_DATA_UINT64 },
115 { "erpt-set-failed", KSTAT_DATA_UINT64 },
116 { "fmri-set-failed", KSTAT_DATA_UINT64 },
117 { "payload-set-failed", KSTAT_DATA_UINT64 }
118};
119
120kstat_t *fm_ksp;
121
122#ifdef _KERNEL
123
124/*
125 * Formatting utility function for fm_nvprintr. We attempt to wrap chunks of
126 * output so they aren't split across console lines, and return the end column.
127 */
128/*PRINTFLIKE4*/
129static int
130fm_printf(int depth, int c, int cols, const char *format, ...)
131{
132 va_list ap;
133 int width;
134 char c1;
135
136 va_start(ap, format);
137 width = vsnprintf(&c1, sizeof (c1), format, ap);
138 va_end(ap);
139
140 if (c + width >= cols) {
141 console_printf("\n");
142 c = 0;
143 if (format[0] != ' ' && depth > 0) {
144 console_printf(" ");
145 c++;
146 }
147 }
148
149 va_start(ap, format);
150 console_vprintf(format, ap);
151 va_end(ap);
152
153 return ((c + width) % cols);
154}
155
156/*
0bd31011 157 * Recursively print a nvlist in the specified column width and return the
7bdf406d
TG
158 * column we end up in. This function is called recursively by fm_nvprint(),
159 * below. We generically format the entire nvpair using hexadecimal
160 * integers and strings, and elide any integer arrays. Arrays are basically
161 * used for cache dumps right now, so we suppress them so as not to overwhelm
162 * the amount of console output we produce at panic time. This can be further
163 * enhanced as FMA technology grows based upon the needs of consumers. All
164 * FMA telemetry is logged using the dump device transport, so the console
165 * output serves only as a fallback in case this procedure is unsuccessful.
166 */
167static int
168fm_nvprintr(nvlist_t *nvl, int d, int c, int cols)
169{
170 nvpair_t *nvp;
171
172 for (nvp = nvlist_next_nvpair(nvl, NULL);
173 nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) {
174
175 data_type_t type = nvpair_type(nvp);
176 const char *name = nvpair_name(nvp);
177
178 boolean_t b;
179 uint8_t i8;
180 uint16_t i16;
181 uint32_t i32;
182 uint64_t i64;
183 char *str;
184 nvlist_t *cnv;
185
186 if (strcmp(name, FM_CLASS) == 0)
187 continue; /* already printed by caller */
188
189 c = fm_printf(d, c, cols, " %s=", name);
190
191 switch (type) {
192 case DATA_TYPE_BOOLEAN:
193 c = fm_printf(d + 1, c, cols, " 1");
194 break;
195
196 case DATA_TYPE_BOOLEAN_VALUE:
197 (void) nvpair_value_boolean_value(nvp, &b);
198 c = fm_printf(d + 1, c, cols, b ? "1" : "0");
199 break;
200
201 case DATA_TYPE_BYTE:
202 (void) nvpair_value_byte(nvp, &i8);
203 c = fm_printf(d + 1, c, cols, "0x%x", i8);
204 break;
205
206 case DATA_TYPE_INT8:
207 (void) nvpair_value_int8(nvp, (void *)&i8);
208 c = fm_printf(d + 1, c, cols, "0x%x", i8);
209 break;
210
211 case DATA_TYPE_UINT8:
212 (void) nvpair_value_uint8(nvp, &i8);
213 c = fm_printf(d + 1, c, cols, "0x%x", i8);
214 break;
215
216 case DATA_TYPE_INT16:
217 (void) nvpair_value_int16(nvp, (void *)&i16);
218 c = fm_printf(d + 1, c, cols, "0x%x", i16);
219 break;
220
221 case DATA_TYPE_UINT16:
222 (void) nvpair_value_uint16(nvp, &i16);
223 c = fm_printf(d + 1, c, cols, "0x%x", i16);
224 break;
225
226 case DATA_TYPE_INT32:
227 (void) nvpair_value_int32(nvp, (void *)&i32);
228 c = fm_printf(d + 1, c, cols, "0x%x", i32);
229 break;
230
231 case DATA_TYPE_UINT32:
232 (void) nvpair_value_uint32(nvp, &i32);
233 c = fm_printf(d + 1, c, cols, "0x%x", i32);
234 break;
235
236 case DATA_TYPE_INT64:
237 (void) nvpair_value_int64(nvp, (void *)&i64);
238 c = fm_printf(d + 1, c, cols, "0x%llx",
239 (u_longlong_t)i64);
240 break;
241
242 case DATA_TYPE_UINT64:
243 (void) nvpair_value_uint64(nvp, &i64);
244 c = fm_printf(d + 1, c, cols, "0x%llx",
245 (u_longlong_t)i64);
246 break;
247
248 case DATA_TYPE_HRTIME:
249 (void) nvpair_value_hrtime(nvp, (void *)&i64);
250 c = fm_printf(d + 1, c, cols, "0x%llx",
251 (u_longlong_t)i64);
252 break;
253
254 case DATA_TYPE_STRING:
255 (void) nvpair_value_string(nvp, &str);
256 c = fm_printf(d + 1, c, cols, "\"%s\"",
257 str ? str : "<NULL>");
258 break;
259
260 case DATA_TYPE_NVLIST:
261 c = fm_printf(d + 1, c, cols, "[");
262 (void) nvpair_value_nvlist(nvp, &cnv);
263 c = fm_nvprintr(cnv, d + 1, c, cols);
264 c = fm_printf(d + 1, c, cols, " ]");
265 break;
266
267 case DATA_TYPE_NVLIST_ARRAY: {
268 nvlist_t **val;
269 uint_t i, nelem;
270
271 c = fm_printf(d + 1, c, cols, "[");
272 (void) nvpair_value_nvlist_array(nvp, &val, &nelem);
273 for (i = 0; i < nelem; i++) {
274 c = fm_nvprintr(val[i], d + 1, c, cols);
275 }
276 c = fm_printf(d + 1, c, cols, " ]");
277 }
278 break;
279
280 case DATA_TYPE_INT8_ARRAY: {
281 int8_t *val;
282 uint_t i, nelem;
283
284 c = fm_printf(d + 1, c, cols, "[ ");
285 (void) nvpair_value_int8_array(nvp, &val, &nelem);
286 for (i = 0; i < nelem; i++)
287 c = fm_printf(d + 1, c, cols, "0x%llx ",
288 (u_longlong_t)val[i]);
289
290 c = fm_printf(d + 1, c, cols, "]");
291 break;
292 }
293
294 case DATA_TYPE_UINT8_ARRAY: {
295 uint8_t *val;
296 uint_t i, nelem;
297
298 c = fm_printf(d + 1, c, cols, "[ ");
299 (void) nvpair_value_uint8_array(nvp, &val, &nelem);
300 for (i = 0; i < nelem; i++)
301 c = fm_printf(d + 1, c, cols, "0x%llx ",
302 (u_longlong_t)val[i]);
303
304 c = fm_printf(d + 1, c, cols, "]");
305 break;
306 }
307
308 case DATA_TYPE_INT16_ARRAY: {
309 int16_t *val;
310 uint_t i, nelem;
311
312 c = fm_printf(d + 1, c, cols, "[ ");
313 (void) nvpair_value_int16_array(nvp, &val, &nelem);
314 for (i = 0; i < nelem; i++)
315 c = fm_printf(d + 1, c, cols, "0x%llx ",
316 (u_longlong_t)val[i]);
317
318 c = fm_printf(d + 1, c, cols, "]");
319 break;
320 }
321
322 case DATA_TYPE_UINT16_ARRAY: {
323 uint16_t *val;
324 uint_t i, nelem;
325
326 c = fm_printf(d + 1, c, cols, "[ ");
327 (void) nvpair_value_uint16_array(nvp, &val, &nelem);
328 for (i = 0; i < nelem; i++)
329 c = fm_printf(d + 1, c, cols, "0x%llx ",
330 (u_longlong_t)val[i]);
331
332 c = fm_printf(d + 1, c, cols, "]");
333 break;
334 }
335
336 case DATA_TYPE_INT32_ARRAY: {
337 int32_t *val;
338 uint_t i, nelem;
339
340 c = fm_printf(d + 1, c, cols, "[ ");
341 (void) nvpair_value_int32_array(nvp, &val, &nelem);
342 for (i = 0; i < nelem; i++)
343 c = fm_printf(d + 1, c, cols, "0x%llx ",
344 (u_longlong_t)val[i]);
345
346 c = fm_printf(d + 1, c, cols, "]");
347 break;
348 }
349
350 case DATA_TYPE_UINT32_ARRAY: {
351 uint32_t *val;
352 uint_t i, nelem;
353
354 c = fm_printf(d + 1, c, cols, "[ ");
355 (void) nvpair_value_uint32_array(nvp, &val, &nelem);
356 for (i = 0; i < nelem; i++)
357 c = fm_printf(d + 1, c, cols, "0x%llx ",
358 (u_longlong_t)val[i]);
359
360 c = fm_printf(d + 1, c, cols, "]");
361 break;
362 }
363
364 case DATA_TYPE_INT64_ARRAY: {
365 int64_t *val;
366 uint_t i, nelem;
367
368 c = fm_printf(d + 1, c, cols, "[ ");
369 (void) nvpair_value_int64_array(nvp, &val, &nelem);
370 for (i = 0; i < nelem; i++)
371 c = fm_printf(d + 1, c, cols, "0x%llx ",
372 (u_longlong_t)val[i]);
373
374 c = fm_printf(d + 1, c, cols, "]");
375 break;
376 }
377
378 case DATA_TYPE_UINT64_ARRAY: {
379 uint64_t *val;
380 uint_t i, nelem;
381
382 c = fm_printf(d + 1, c, cols, "[ ");
383 (void) nvpair_value_uint64_array(nvp, &val, &nelem);
384 for (i = 0; i < nelem; i++)
385 c = fm_printf(d + 1, c, cols, "0x%llx ",
386 (u_longlong_t)val[i]);
387
388 c = fm_printf(d + 1, c, cols, "]");
389 break;
390 }
391
392 case DATA_TYPE_STRING_ARRAY:
393 case DATA_TYPE_BOOLEAN_ARRAY:
394 case DATA_TYPE_BYTE_ARRAY:
395 c = fm_printf(d + 1, c, cols, "[...]");
396 break;
397
398 case DATA_TYPE_UNKNOWN:
399 c = fm_printf(d + 1, c, cols, "<unknown>");
400 break;
401 }
402 }
403
404 return (c);
405}
406
407void
408fm_nvprint(nvlist_t *nvl)
409{
410 char *class;
411 int c = 0;
412
413 console_printf("\n");
414
415 if (nvlist_lookup_string(nvl, FM_CLASS, &class) == 0)
416 c = fm_printf(0, c, zfs_zevent_cols, "%s", class);
417
418 if (fm_nvprintr(nvl, 0, c, zfs_zevent_cols) != 0)
419 console_printf("\n");
420
421 console_printf("\n");
422}
423
424static zevent_t *
425zfs_zevent_alloc(void)
426{
427 zevent_t *ev;
428
429 ev = kmem_zalloc(sizeof (zevent_t), KM_SLEEP);
0bd31011
TG
430 if (ev == NULL)
431 return (NULL);
7bdf406d
TG
432
433 list_create(&ev->ev_ze_list, sizeof (zfs_zevent_t),
434 offsetof(zfs_zevent_t, ze_node));
435 list_link_init(&ev->ev_node);
436
437 return (ev);
438}
439
440static void
441zfs_zevent_free(zevent_t *ev)
442{
443 /* Run provided cleanup callback */
444 ev->ev_cb(ev->ev_nvl, ev->ev_detector);
445
446 list_destroy(&ev->ev_ze_list);
447 kmem_free(ev, sizeof (zevent_t));
448}
449
450static void
451zfs_zevent_drain(zevent_t *ev)
452{
453 zfs_zevent_t *ze;
454
455 ASSERT(MUTEX_HELD(&zevent_lock));
456 list_remove(&zevent_list, ev);
457
458 /* Remove references to this event in all private file data */
459 while ((ze = list_head(&ev->ev_ze_list)) != NULL) {
460 list_remove(&ev->ev_ze_list, ze);
461 ze->ze_zevent = NULL;
462 ze->ze_dropped++;
463 }
464
465 zfs_zevent_free(ev);
466}
467
468void
469zfs_zevent_drain_all(int *count)
470{
471 zevent_t *ev;
472
473 mutex_enter(&zevent_lock);
474 while ((ev = list_head(&zevent_list)) != NULL)
475 zfs_zevent_drain(ev);
476
477 *count = zevent_len_cur;
478 zevent_len_cur = 0;
479 mutex_exit(&zevent_lock);
480}
481
482/*
483 * New zevents are inserted at the head. If the maximum queue
484 * length is exceeded a zevent will be drained from the tail.
485 * As part of this any user space processes which currently have
486 * a reference to this zevent_t in their private data will have
487 * this reference set to NULL.
488 */
489static void
490zfs_zevent_insert(zevent_t *ev)
491{
492 ASSERT(MUTEX_HELD(&zevent_lock));
493 list_insert_head(&zevent_list, ev);
494
495 if (zevent_len_cur >= zfs_zevent_len_max)
496 zfs_zevent_drain(list_tail(&zevent_list));
497 else
498 zevent_len_cur++;
499}
500
501/*
502 * Post a zevent. The cb will be called when nvl and detector are no longer
503 * needed, i.e.:
504 * - An error happened and a zevent can't be posted. In this case, cb is called
505 * before zfs_zevent_post() returns.
506 * - The event is being drained and freed.
507 */
508int
509zfs_zevent_post(nvlist_t *nvl, nvlist_t *detector, zevent_cb_t *cb)
510{
511 int64_t tv_array[2];
512 timestruc_t tv;
513 uint64_t eid;
514 size_t nvl_size = 0;
515 zevent_t *ev;
516 int error;
517
518 ASSERT(cb != NULL);
519
520 gethrestime(&tv);
521 tv_array[0] = tv.tv_sec;
522 tv_array[1] = tv.tv_nsec;
523
524 error = nvlist_add_int64_array(nvl, FM_EREPORT_TIME, tv_array, 2);
525 if (error) {
0bd31011 526 atomic_add_64(&erpt_kstat_data.erpt_set_failed.value.ui64, 1);
7bdf406d
TG
527 goto out;
528 }
529
530 eid = atomic_inc_64_nv(&zevent_eid);
531 error = nvlist_add_uint64(nvl, FM_EREPORT_EID, eid);
532 if (error) {
0bd31011 533 atomic_add_64(&erpt_kstat_data.erpt_set_failed.value.ui64, 1);
7bdf406d
TG
534 goto out;
535 }
536
537 error = nvlist_size(nvl, &nvl_size, NV_ENCODE_NATIVE);
538 if (error) {
0bd31011 539 atomic_add_64(&erpt_kstat_data.erpt_dropped.value.ui64, 1);
7bdf406d
TG
540 goto out;
541 }
542
543 if (nvl_size > ERPT_DATA_SZ || nvl_size == 0) {
0bd31011 544 atomic_add_64(&erpt_kstat_data.erpt_dropped.value.ui64, 1);
7bdf406d
TG
545 error = EOVERFLOW;
546 goto out;
547 }
548
549 if (zfs_zevent_console)
550 fm_nvprint(nvl);
551
552 ev = zfs_zevent_alloc();
553 if (ev == NULL) {
0bd31011 554 atomic_add_64(&erpt_kstat_data.erpt_dropped.value.ui64, 1);
7bdf406d
TG
555 error = ENOMEM;
556 goto out;
557 }
558
559 ev->ev_nvl = nvl;
560 ev->ev_detector = detector;
561 ev->ev_cb = cb;
562 ev->ev_eid = eid;
563
564 mutex_enter(&zevent_lock);
565 zfs_zevent_insert(ev);
566 cv_broadcast(&zevent_cv);
567 mutex_exit(&zevent_lock);
568
569out:
570 if (error)
571 cb(nvl, detector);
572
573 return (error);
574}
575
576static int
577zfs_zevent_minor_to_state(minor_t minor, zfs_zevent_t **ze)
578{
579 *ze = zfsdev_get_state(minor, ZST_ZEVENT);
580 if (*ze == NULL)
581 return (EBADF);
582
583 return (0);
584}
585
586int
587zfs_zevent_fd_hold(int fd, minor_t *minorp, zfs_zevent_t **ze)
588{
589 file_t *fp;
590 int error;
591
592 fp = getf(fd);
593 if (fp == NULL)
594 return (EBADF);
595
596 error = zfsdev_getminor(fp->f_file, minorp);
597 if (error == 0)
598 error = zfs_zevent_minor_to_state(*minorp, ze);
599
600 if (error)
601 zfs_zevent_fd_rele(fd);
602
603 return (error);
604}
605
606void
607zfs_zevent_fd_rele(int fd)
608{
609 releasef(fd);
610}
611
612/*
613 * Get the next zevent in the stream and place a copy in 'event'. This
614 * may fail with ENOMEM if the encoded nvlist size exceeds the passed
615 * 'event_size'. In this case the stream pointer is not advanced and
616 * and 'event_size' is set to the minimum required buffer size.
617 */
618int
619zfs_zevent_next(zfs_zevent_t *ze, nvlist_t **event, uint64_t *event_size,
620 uint64_t *dropped)
621{
622 zevent_t *ev;
623 size_t size;
624 int error = 0;
625
626 mutex_enter(&zevent_lock);
627 if (ze->ze_zevent == NULL) {
628 /* New stream start at the beginning/tail */
629 ev = list_tail(&zevent_list);
630 if (ev == NULL) {
631 error = ENOENT;
632 goto out;
633 }
634 } else {
635 /*
636 * Existing stream continue with the next element and remove
637 * ourselves from the wait queue for the previous element
638 */
639 ev = list_prev(&zevent_list, ze->ze_zevent);
640 if (ev == NULL) {
641 error = ENOENT;
642 goto out;
643 }
644 }
645
646 VERIFY(nvlist_size(ev->ev_nvl, &size, NV_ENCODE_NATIVE) == 0);
647 if (size > *event_size) {
648 *event_size = size;
649 error = ENOMEM;
650 goto out;
651 }
652
653 if (ze->ze_zevent)
654 list_remove(&ze->ze_zevent->ev_ze_list, ze);
655
656 ze->ze_zevent = ev;
657 list_insert_head(&ev->ev_ze_list, ze);
0bd31011 658 nvlist_dup(ev->ev_nvl, event, KM_SLEEP);
7bdf406d
TG
659 *dropped = ze->ze_dropped;
660 ze->ze_dropped = 0;
661out:
662 mutex_exit(&zevent_lock);
663
664 return (error);
665}
666
667int
668zfs_zevent_wait(zfs_zevent_t *ze)
669{
670 int error = 0;
671
672 mutex_enter(&zevent_lock);
673
674 if (zevent_flags & ZEVENT_SHUTDOWN) {
675 error = ESHUTDOWN;
676 goto out;
677 }
678
679 zevent_waiters++;
680 cv_wait_sig(&zevent_cv, &zevent_lock);
681 if (issig(JUSTLOOKING))
682 error = EINTR;
683
684 zevent_waiters--;
685out:
686 mutex_exit(&zevent_lock);
687
688 return (error);
689}
690
691/*
692 * The caller may seek to a specific EID by passing that EID. If the EID
693 * is still available in the posted list of events the cursor is positioned
694 * there. Otherwise ENOENT is returned and the cursor is not moved.
695 *
696 * There are two reserved EIDs which may be passed and will never fail.
697 * ZEVENT_SEEK_START positions the cursor at the start of the list, and
698 * ZEVENT_SEEK_END positions the cursor at the end of the list.
699 */
700int
701zfs_zevent_seek(zfs_zevent_t *ze, uint64_t eid)
702{
703 zevent_t *ev;
704 int error = 0;
705
706 mutex_enter(&zevent_lock);
707
708 if (eid == ZEVENT_SEEK_START) {
709 if (ze->ze_zevent)
710 list_remove(&ze->ze_zevent->ev_ze_list, ze);
711
712 ze->ze_zevent = NULL;
713 goto out;
714 }
715
716 if (eid == ZEVENT_SEEK_END) {
717 if (ze->ze_zevent)
718 list_remove(&ze->ze_zevent->ev_ze_list, ze);
719
720 ev = list_head(&zevent_list);
721 if (ev) {
722 ze->ze_zevent = ev;
723 list_insert_head(&ev->ev_ze_list, ze);
724 } else {
725 ze->ze_zevent = NULL;
726 }
727
728 goto out;
729 }
730
731 for (ev = list_tail(&zevent_list); ev != NULL;
732 ev = list_prev(&zevent_list, ev)) {
733 if (ev->ev_eid == eid) {
734 if (ze->ze_zevent)
735 list_remove(&ze->ze_zevent->ev_ze_list, ze);
736
737 ze->ze_zevent = ev;
738 list_insert_head(&ev->ev_ze_list, ze);
739 break;
740 }
741 }
742
743 if (ev == NULL)
744 error = ENOENT;
745
746out:
747 mutex_exit(&zevent_lock);
748
749 return (error);
750}
751
752void
753zfs_zevent_init(zfs_zevent_t **zep)
754{
755 zfs_zevent_t *ze;
756
757 ze = *zep = kmem_zalloc(sizeof (zfs_zevent_t), KM_SLEEP);
758 list_link_init(&ze->ze_node);
759}
760
761void
762zfs_zevent_destroy(zfs_zevent_t *ze)
763{
764 mutex_enter(&zevent_lock);
765 if (ze->ze_zevent)
766 list_remove(&ze->ze_zevent->ev_ze_list, ze);
767 mutex_exit(&zevent_lock);
768
769 kmem_free(ze, sizeof (zfs_zevent_t));
770}
771#endif /* _KERNEL */
772
773/*
774 * Wrapppers for FM nvlist allocators
775 */
776/* ARGSUSED */
777static void *
778i_fm_alloc(nv_alloc_t *nva, size_t size)
779{
780 return (kmem_zalloc(size, KM_SLEEP));
781}
782
783/* ARGSUSED */
784static void
785i_fm_free(nv_alloc_t *nva, void *buf, size_t size)
786{
787 kmem_free(buf, size);
788}
789
790const nv_alloc_ops_t fm_mem_alloc_ops = {
791 NULL,
792 NULL,
793 i_fm_alloc,
794 i_fm_free,
795 NULL
796};
797
798/*
799 * Create and initialize a new nv_alloc_t for a fixed buffer, buf. A pointer
800 * to the newly allocated nv_alloc_t structure is returned upon success or NULL
801 * is returned to indicate that the nv_alloc structure could not be created.
802 */
803nv_alloc_t *
804fm_nva_xcreate(char *buf, size_t bufsz)
805{
806 nv_alloc_t *nvhdl = kmem_zalloc(sizeof (nv_alloc_t), KM_SLEEP);
807
808 if (bufsz == 0 || nv_alloc_init(nvhdl, nv_fixed_ops, buf, bufsz) != 0) {
809 kmem_free(nvhdl, sizeof (nv_alloc_t));
810 return (NULL);
811 }
812
813 return (nvhdl);
814}
815
816/*
817 * Destroy a previously allocated nv_alloc structure. The fixed buffer
818 * associated with nva must be freed by the caller.
819 */
820void
821fm_nva_xdestroy(nv_alloc_t *nva)
822{
823 nv_alloc_fini(nva);
824 kmem_free(nva, sizeof (nv_alloc_t));
825}
826
827/*
828 * Create a new nv list. A pointer to a new nv list structure is returned
829 * upon success or NULL is returned to indicate that the structure could
830 * not be created. The newly created nv list is created and managed by the
831 * operations installed in nva. If nva is NULL, the default FMA nva
832 * operations are installed and used.
833 *
834 * When called from the kernel and nva == NULL, this function must be called
835 * from passive kernel context with no locks held that can prevent a
836 * sleeping memory allocation from occurring. Otherwise, this function may
837 * be called from other kernel contexts as long a valid nva created via
838 * fm_nva_create() is supplied.
839 */
840nvlist_t *
841fm_nvlist_create(nv_alloc_t *nva)
842{
843 int hdl_alloced = 0;
844 nvlist_t *nvl;
845 nv_alloc_t *nvhdl;
846
847 if (nva == NULL) {
848 nvhdl = kmem_zalloc(sizeof (nv_alloc_t), KM_SLEEP);
849
850 if (nv_alloc_init(nvhdl, &fm_mem_alloc_ops, NULL, 0) != 0) {
851 kmem_free(nvhdl, sizeof (nv_alloc_t));
852 return (NULL);
853 }
854 hdl_alloced = 1;
855 } else {
856 nvhdl = nva;
857 }
858
859 if (nvlist_xalloc(&nvl, NV_UNIQUE_NAME, nvhdl) != 0) {
860 if (hdl_alloced) {
861 nv_alloc_fini(nvhdl);
862 kmem_free(nvhdl, sizeof (nv_alloc_t));
863 }
864 return (NULL);
865 }
866
867 return (nvl);
868}
869
870/*
871 * Destroy a previously allocated nvlist structure. flag indicates whether
872 * or not the associated nva structure should be freed (FM_NVA_FREE) or
873 * retained (FM_NVA_RETAIN). Retaining the nv alloc structure allows
874 * it to be re-used for future nvlist creation operations.
875 */
876void
877fm_nvlist_destroy(nvlist_t *nvl, int flag)
878{
879 nv_alloc_t *nva = nvlist_lookup_nv_alloc(nvl);
880
881 nvlist_free(nvl);
882
883 if (nva != NULL) {
884 if (flag == FM_NVA_FREE)
885 fm_nva_xdestroy(nva);
886 }
887}
888
889int
890i_fm_payload_set(nvlist_t *payload, const char *name, va_list ap)
891{
892 int nelem, ret = 0;
893 data_type_t type;
894
895 while (ret == 0 && name != NULL) {
896 type = va_arg(ap, data_type_t);
897 switch (type) {
898 case DATA_TYPE_BYTE:
899 ret = nvlist_add_byte(payload, name,
900 va_arg(ap, uint_t));
901 break;
902 case DATA_TYPE_BYTE_ARRAY:
903 nelem = va_arg(ap, int);
904 ret = nvlist_add_byte_array(payload, name,
905 va_arg(ap, uchar_t *), nelem);
906 break;
907 case DATA_TYPE_BOOLEAN_VALUE:
908 ret = nvlist_add_boolean_value(payload, name,
909 va_arg(ap, boolean_t));
910 break;
911 case DATA_TYPE_BOOLEAN_ARRAY:
912 nelem = va_arg(ap, int);
913 ret = nvlist_add_boolean_array(payload, name,
914 va_arg(ap, boolean_t *), nelem);
915 break;
916 case DATA_TYPE_INT8:
917 ret = nvlist_add_int8(payload, name,
918 va_arg(ap, int));
919 break;
920 case DATA_TYPE_INT8_ARRAY:
921 nelem = va_arg(ap, int);
922 ret = nvlist_add_int8_array(payload, name,
923 va_arg(ap, int8_t *), nelem);
924 break;
925 case DATA_TYPE_UINT8:
926 ret = nvlist_add_uint8(payload, name,
927 va_arg(ap, uint_t));
928 break;
929 case DATA_TYPE_UINT8_ARRAY:
930 nelem = va_arg(ap, int);
931 ret = nvlist_add_uint8_array(payload, name,
932 va_arg(ap, uint8_t *), nelem);
933 break;
934 case DATA_TYPE_INT16:
935 ret = nvlist_add_int16(payload, name,
936 va_arg(ap, int));
937 break;
938 case DATA_TYPE_INT16_ARRAY:
939 nelem = va_arg(ap, int);
940 ret = nvlist_add_int16_array(payload, name,
941 va_arg(ap, int16_t *), nelem);
942 break;
943 case DATA_TYPE_UINT16:
944 ret = nvlist_add_uint16(payload, name,
945 va_arg(ap, uint_t));
946 break;
947 case DATA_TYPE_UINT16_ARRAY:
948 nelem = va_arg(ap, int);
949 ret = nvlist_add_uint16_array(payload, name,
950 va_arg(ap, uint16_t *), nelem);
951 break;
952 case DATA_TYPE_INT32:
953 ret = nvlist_add_int32(payload, name,
954 va_arg(ap, int32_t));
955 break;
956 case DATA_TYPE_INT32_ARRAY:
957 nelem = va_arg(ap, int);
958 ret = nvlist_add_int32_array(payload, name,
959 va_arg(ap, int32_t *), nelem);
960 break;
961 case DATA_TYPE_UINT32:
962 ret = nvlist_add_uint32(payload, name,
963 va_arg(ap, uint32_t));
964 break;
965 case DATA_TYPE_UINT32_ARRAY:
966 nelem = va_arg(ap, int);
967 ret = nvlist_add_uint32_array(payload, name,
968 va_arg(ap, uint32_t *), nelem);
969 break;
970 case DATA_TYPE_INT64:
971 ret = nvlist_add_int64(payload, name,
972 va_arg(ap, int64_t));
973 break;
974 case DATA_TYPE_INT64_ARRAY:
975 nelem = va_arg(ap, int);
976 ret = nvlist_add_int64_array(payload, name,
977 va_arg(ap, int64_t *), nelem);
978 break;
979 case DATA_TYPE_UINT64:
980 ret = nvlist_add_uint64(payload, name,
981 va_arg(ap, uint64_t));
982 break;
983 case DATA_TYPE_UINT64_ARRAY:
984 nelem = va_arg(ap, int);
985 ret = nvlist_add_uint64_array(payload, name,
986 va_arg(ap, uint64_t *), nelem);
987 break;
988 case DATA_TYPE_STRING:
989 ret = nvlist_add_string(payload, name,
990 va_arg(ap, char *));
991 break;
992 case DATA_TYPE_STRING_ARRAY:
993 nelem = va_arg(ap, int);
994 ret = nvlist_add_string_array(payload, name,
995 va_arg(ap, char **), nelem);
996 break;
997 case DATA_TYPE_NVLIST:
998 ret = nvlist_add_nvlist(payload, name,
999 va_arg(ap, nvlist_t *));
1000 break;
1001 case DATA_TYPE_NVLIST_ARRAY:
1002 nelem = va_arg(ap, int);
1003 ret = nvlist_add_nvlist_array(payload, name,
1004 va_arg(ap, nvlist_t **), nelem);
1005 break;
1006 default:
1007 ret = EINVAL;
1008 }
1009
1010 name = va_arg(ap, char *);
1011 }
1012 return (ret);
1013}
1014
1015void
1016fm_payload_set(nvlist_t *payload, ...)
1017{
1018 int ret;
1019 const char *name;
1020 va_list ap;
1021
1022 va_start(ap, payload);
1023 name = va_arg(ap, char *);
1024 ret = i_fm_payload_set(payload, name, ap);
1025 va_end(ap);
1026
1027 if (ret)
0bd31011
TG
1028 atomic_add_64(
1029 &erpt_kstat_data.payload_set_failed.value.ui64, 1);
7bdf406d
TG
1030}
1031
1032/*
1033 * Set-up and validate the members of an ereport event according to:
1034 *
1035 * Member name Type Value
1036 * ====================================================
1037 * class string ereport
1038 * version uint8_t 0
1039 * ena uint64_t <ena>
1040 * detector nvlist_t <detector>
1041 * ereport-payload nvlist_t <var args>
1042 *
1043 * We don't actually add a 'version' member to the payload. Really,
1044 * the version quoted to us by our caller is that of the category 1
1045 * "ereport" event class (and we require FM_EREPORT_VERS0) but
1046 * the payload version of the actual leaf class event under construction
1047 * may be something else. Callers should supply a version in the varargs,
1048 * or (better) we could take two version arguments - one for the
1049 * ereport category 1 classification (expect FM_EREPORT_VERS0) and one
1050 * for the leaf class.
1051 */
1052void
1053fm_ereport_set(nvlist_t *ereport, int version, const char *erpt_class,
1054 uint64_t ena, const nvlist_t *detector, ...)
1055{
1056 char ereport_class[FM_MAX_CLASS];
1057 const char *name;
1058 va_list ap;
1059 int ret;
1060
1061 if (version != FM_EREPORT_VERS0) {
0bd31011 1062 atomic_add_64(&erpt_kstat_data.erpt_set_failed.value.ui64, 1);
7bdf406d
TG
1063 return;
1064 }
1065
1066 (void) snprintf(ereport_class, FM_MAX_CLASS, "%s.%s",
1067 FM_EREPORT_CLASS, erpt_class);
1068 if (nvlist_add_string(ereport, FM_CLASS, ereport_class) != 0) {
0bd31011 1069 atomic_add_64(&erpt_kstat_data.erpt_set_failed.value.ui64, 1);
7bdf406d
TG
1070 return;
1071 }
1072
1073 if (nvlist_add_uint64(ereport, FM_EREPORT_ENA, ena)) {
0bd31011 1074 atomic_add_64(&erpt_kstat_data.erpt_set_failed.value.ui64, 1);
7bdf406d
TG
1075 }
1076
1077 if (nvlist_add_nvlist(ereport, FM_EREPORT_DETECTOR,
1078 (nvlist_t *)detector) != 0) {
0bd31011 1079 atomic_add_64(&erpt_kstat_data.erpt_set_failed.value.ui64, 1);
7bdf406d
TG
1080 }
1081
1082 va_start(ap, detector);
1083 name = va_arg(ap, const char *);
1084 ret = i_fm_payload_set(ereport, name, ap);
1085 va_end(ap);
1086
1087 if (ret)
0bd31011 1088 atomic_add_64(&erpt_kstat_data.erpt_set_failed.value.ui64, 1);
7bdf406d
TG
1089}
1090
1091/*
1092 * Set-up and validate the members of an hc fmri according to;
1093 *
1094 * Member name Type Value
1095 * ===================================================
1096 * version uint8_t 0
1097 * auth nvlist_t <auth>
1098 * hc-name string <name>
1099 * hc-id string <id>
1100 *
1101 * Note that auth and hc-id are optional members.
1102 */
1103
1104#define HC_MAXPAIRS 20
1105#define HC_MAXNAMELEN 50
1106
1107static int
1108fm_fmri_hc_set_common(nvlist_t *fmri, int version, const nvlist_t *auth)
1109{
1110 if (version != FM_HC_SCHEME_VERSION) {
0bd31011 1111 atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
7bdf406d
TG
1112 return (0);
1113 }
1114
1115 if (nvlist_add_uint8(fmri, FM_VERSION, version) != 0 ||
1116 nvlist_add_string(fmri, FM_FMRI_SCHEME, FM_FMRI_SCHEME_HC) != 0) {
0bd31011 1117 atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
7bdf406d
TG
1118 return (0);
1119 }
1120
1121 if (auth != NULL && nvlist_add_nvlist(fmri, FM_FMRI_AUTHORITY,
1122 (nvlist_t *)auth) != 0) {
0bd31011 1123 atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
7bdf406d
TG
1124 return (0);
1125 }
1126
1127 return (1);
1128}
1129
1130void
1131fm_fmri_hc_set(nvlist_t *fmri, int version, const nvlist_t *auth,
1132 nvlist_t *snvl, int npairs, ...)
1133{
1134 nv_alloc_t *nva = nvlist_lookup_nv_alloc(fmri);
1135 nvlist_t *pairs[HC_MAXPAIRS];
1136 va_list ap;
1137 int i;
1138
1139 if (!fm_fmri_hc_set_common(fmri, version, auth))
1140 return;
1141
1142 npairs = MIN(npairs, HC_MAXPAIRS);
1143
1144 va_start(ap, npairs);
1145 for (i = 0; i < npairs; i++) {
1146 const char *name = va_arg(ap, const char *);
1147 uint32_t id = va_arg(ap, uint32_t);
1148 char idstr[11];
1149
1150 (void) snprintf(idstr, sizeof (idstr), "%u", id);
1151
1152 pairs[i] = fm_nvlist_create(nva);
1153 if (nvlist_add_string(pairs[i], FM_FMRI_HC_NAME, name) != 0 ||
1154 nvlist_add_string(pairs[i], FM_FMRI_HC_ID, idstr) != 0) {
0bd31011
TG
1155 atomic_add_64(
1156 &erpt_kstat_data.fmri_set_failed.value.ui64, 1);
7bdf406d
TG
1157 }
1158 }
1159 va_end(ap);
1160
1161 if (nvlist_add_nvlist_array(fmri, FM_FMRI_HC_LIST, pairs, npairs) != 0)
0bd31011 1162 atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
7bdf406d
TG
1163
1164 for (i = 0; i < npairs; i++)
1165 fm_nvlist_destroy(pairs[i], FM_NVA_RETAIN);
1166
1167 if (snvl != NULL) {
1168 if (nvlist_add_nvlist(fmri, FM_FMRI_HC_SPECIFIC, snvl) != 0) {
0bd31011
TG
1169 atomic_add_64(
1170 &erpt_kstat_data.fmri_set_failed.value.ui64, 1);
7bdf406d
TG
1171 }
1172 }
1173}
1174
1175void
1176fm_fmri_hc_create(nvlist_t *fmri, int version, const nvlist_t *auth,
1177 nvlist_t *snvl, nvlist_t *bboard, int npairs, ...)
1178{
1179 nv_alloc_t *nva = nvlist_lookup_nv_alloc(fmri);
1180 nvlist_t *pairs[HC_MAXPAIRS];
1181 nvlist_t **hcl;
1182 uint_t n;
1183 int i, j;
1184 va_list ap;
1185 char *hcname, *hcid;
1186
1187 if (!fm_fmri_hc_set_common(fmri, version, auth))
1188 return;
1189
1190 /*
1191 * copy the bboard nvpairs to the pairs array
1192 */
1193 if (nvlist_lookup_nvlist_array(bboard, FM_FMRI_HC_LIST, &hcl, &n)
1194 != 0) {
0bd31011 1195 atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
7bdf406d
TG
1196 return;
1197 }
1198
1199 for (i = 0; i < n; i++) {
1200 if (nvlist_lookup_string(hcl[i], FM_FMRI_HC_NAME,
1201 &hcname) != 0) {
0bd31011
TG
1202 atomic_add_64(
1203 &erpt_kstat_data.fmri_set_failed.value.ui64, 1);
7bdf406d
TG
1204 return;
1205 }
1206 if (nvlist_lookup_string(hcl[i], FM_FMRI_HC_ID, &hcid) != 0) {
0bd31011
TG
1207 atomic_add_64(
1208 &erpt_kstat_data.fmri_set_failed.value.ui64, 1);
7bdf406d
TG
1209 return;
1210 }
1211
1212 pairs[i] = fm_nvlist_create(nva);
1213 if (nvlist_add_string(pairs[i], FM_FMRI_HC_NAME, hcname) != 0 ||
1214 nvlist_add_string(pairs[i], FM_FMRI_HC_ID, hcid) != 0) {
1215 for (j = 0; j <= i; j++) {
1216 if (pairs[j] != NULL)
1217 fm_nvlist_destroy(pairs[j],
1218 FM_NVA_RETAIN);
1219 }
0bd31011
TG
1220 atomic_add_64(
1221 &erpt_kstat_data.fmri_set_failed.value.ui64, 1);
7bdf406d
TG
1222 return;
1223 }
1224 }
1225
1226 /*
1227 * create the pairs from passed in pairs
1228 */
1229 npairs = MIN(npairs, HC_MAXPAIRS);
1230
1231 va_start(ap, npairs);
1232 for (i = n; i < npairs + n; i++) {
1233 const char *name = va_arg(ap, const char *);
1234 uint32_t id = va_arg(ap, uint32_t);
1235 char idstr[11];
1236 (void) snprintf(idstr, sizeof (idstr), "%u", id);
1237 pairs[i] = fm_nvlist_create(nva);
1238 if (nvlist_add_string(pairs[i], FM_FMRI_HC_NAME, name) != 0 ||
1239 nvlist_add_string(pairs[i], FM_FMRI_HC_ID, idstr) != 0) {
1240 for (j = 0; j <= i; j++) {
1241 if (pairs[j] != NULL)
1242 fm_nvlist_destroy(pairs[j],
1243 FM_NVA_RETAIN);
1244 }
0bd31011
TG
1245 atomic_add_64(
1246 &erpt_kstat_data.fmri_set_failed.value.ui64, 1);
7bdf406d
TG
1247 return;
1248 }
1249 }
1250 va_end(ap);
1251
1252 /*
1253 * Create the fmri hc list
1254 */
1255 if (nvlist_add_nvlist_array(fmri, FM_FMRI_HC_LIST, pairs,
1256 npairs + n) != 0) {
0bd31011 1257 atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
7bdf406d
TG
1258 return;
1259 }
1260
1261 for (i = 0; i < npairs + n; i++) {
1262 fm_nvlist_destroy(pairs[i], FM_NVA_RETAIN);
1263 }
1264
1265 if (snvl != NULL) {
1266 if (nvlist_add_nvlist(fmri, FM_FMRI_HC_SPECIFIC, snvl) != 0) {
0bd31011
TG
1267 atomic_add_64(
1268 &erpt_kstat_data.fmri_set_failed.value.ui64, 1);
7bdf406d
TG
1269 return;
1270 }
1271 }
1272}
1273
1274/*
1275 * Set-up and validate the members of an dev fmri according to:
1276 *
1277 * Member name Type Value
1278 * ====================================================
1279 * version uint8_t 0
1280 * auth nvlist_t <auth>
1281 * devpath string <devpath>
1282 * [devid] string <devid>
1283 * [target-port-l0id] string <target-port-lun0-id>
1284 *
1285 * Note that auth and devid are optional members.
1286 */
1287void
1288fm_fmri_dev_set(nvlist_t *fmri_dev, int version, const nvlist_t *auth,
1289 const char *devpath, const char *devid, const char *tpl0)
1290{
1291 int err = 0;
1292
1293 if (version != DEV_SCHEME_VERSION0) {
0bd31011 1294 atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
7bdf406d
TG
1295 return;
1296 }
1297
1298 err |= nvlist_add_uint8(fmri_dev, FM_VERSION, version);
1299 err |= nvlist_add_string(fmri_dev, FM_FMRI_SCHEME, FM_FMRI_SCHEME_DEV);
1300
1301 if (auth != NULL) {
1302 err |= nvlist_add_nvlist(fmri_dev, FM_FMRI_AUTHORITY,
1303 (nvlist_t *)auth);
1304 }
1305
1306 err |= nvlist_add_string(fmri_dev, FM_FMRI_DEV_PATH, devpath);
1307
1308 if (devid != NULL)
1309 err |= nvlist_add_string(fmri_dev, FM_FMRI_DEV_ID, devid);
1310
1311 if (tpl0 != NULL)
1312 err |= nvlist_add_string(fmri_dev, FM_FMRI_DEV_TGTPTLUN0, tpl0);
1313
1314 if (err)
0bd31011 1315 atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
7bdf406d
TG
1316
1317}
1318
1319/*
1320 * Set-up and validate the members of an cpu fmri according to:
1321 *
1322 * Member name Type Value
1323 * ====================================================
1324 * version uint8_t 0
1325 * auth nvlist_t <auth>
1326 * cpuid uint32_t <cpu_id>
1327 * cpumask uint8_t <cpu_mask>
1328 * serial uint64_t <serial_id>
1329 *
1330 * Note that auth, cpumask, serial are optional members.
1331 *
1332 */
1333void
1334fm_fmri_cpu_set(nvlist_t *fmri_cpu, int version, const nvlist_t *auth,
1335 uint32_t cpu_id, uint8_t *cpu_maskp, const char *serial_idp)
1336{
1337 uint64_t *failedp = &erpt_kstat_data.fmri_set_failed.value.ui64;
1338
1339 if (version < CPU_SCHEME_VERSION1) {
0bd31011 1340 atomic_add_64(failedp, 1);
7bdf406d
TG
1341 return;
1342 }
1343
1344 if (nvlist_add_uint8(fmri_cpu, FM_VERSION, version) != 0) {
0bd31011 1345 atomic_add_64(failedp, 1);
7bdf406d
TG
1346 return;
1347 }
1348
1349 if (nvlist_add_string(fmri_cpu, FM_FMRI_SCHEME,
1350 FM_FMRI_SCHEME_CPU) != 0) {
0bd31011 1351 atomic_add_64(failedp, 1);
7bdf406d
TG
1352 return;
1353 }
1354
1355 if (auth != NULL && nvlist_add_nvlist(fmri_cpu, FM_FMRI_AUTHORITY,
1356 (nvlist_t *)auth) != 0)
0bd31011 1357 atomic_add_64(failedp, 1);
7bdf406d
TG
1358
1359 if (nvlist_add_uint32(fmri_cpu, FM_FMRI_CPU_ID, cpu_id) != 0)
0bd31011 1360 atomic_add_64(failedp, 1);
7bdf406d
TG
1361
1362 if (cpu_maskp != NULL && nvlist_add_uint8(fmri_cpu, FM_FMRI_CPU_MASK,
1363 *cpu_maskp) != 0)
0bd31011 1364 atomic_add_64(failedp, 1);
7bdf406d
TG
1365
1366 if (serial_idp == NULL || nvlist_add_string(fmri_cpu,
1367 FM_FMRI_CPU_SERIAL_ID, (char *)serial_idp) != 0)
0bd31011 1368 atomic_add_64(failedp, 1);
7bdf406d
TG
1369}
1370
1371/*
1372 * Set-up and validate the members of a mem according to:
1373 *
1374 * Member name Type Value
1375 * ====================================================
1376 * version uint8_t 0
1377 * auth nvlist_t <auth> [optional]
1378 * unum string <unum>
1379 * serial string <serial> [optional*]
1380 * offset uint64_t <offset> [optional]
1381 *
1382 * * serial is required if offset is present
1383 */
1384void
1385fm_fmri_mem_set(nvlist_t *fmri, int version, const nvlist_t *auth,
1386 const char *unum, const char *serial, uint64_t offset)
1387{
1388 if (version != MEM_SCHEME_VERSION0) {
0bd31011 1389 atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
7bdf406d
TG
1390 return;
1391 }
1392
1393 if (!serial && (offset != (uint64_t)-1)) {
0bd31011 1394 atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
7bdf406d
TG
1395 return;
1396 }
1397
1398 if (nvlist_add_uint8(fmri, FM_VERSION, version) != 0) {
0bd31011 1399 atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
7bdf406d
TG
1400 return;
1401 }
1402
1403 if (nvlist_add_string(fmri, FM_FMRI_SCHEME, FM_FMRI_SCHEME_MEM) != 0) {
0bd31011 1404 atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
7bdf406d
TG
1405 return;
1406 }
1407
1408 if (auth != NULL) {
1409 if (nvlist_add_nvlist(fmri, FM_FMRI_AUTHORITY,
1410 (nvlist_t *)auth) != 0) {
0bd31011
TG
1411 atomic_add_64(
1412 &erpt_kstat_data.fmri_set_failed.value.ui64, 1);
7bdf406d
TG
1413 }
1414 }
1415
1416 if (nvlist_add_string(fmri, FM_FMRI_MEM_UNUM, unum) != 0) {
0bd31011 1417 atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
7bdf406d
TG
1418 }
1419
1420 if (serial != NULL) {
1421 if (nvlist_add_string_array(fmri, FM_FMRI_MEM_SERIAL_ID,
1422 (char **)&serial, 1) != 0) {
0bd31011
TG
1423 atomic_add_64(
1424 &erpt_kstat_data.fmri_set_failed.value.ui64, 1);
7bdf406d 1425 }
0bd31011
TG
1426 if (offset != (uint64_t)-1) {
1427 if (nvlist_add_uint64(fmri, FM_FMRI_MEM_OFFSET,
1428 offset) != 0) {
1429 atomic_add_64(&erpt_kstat_data.
1430 fmri_set_failed.value.ui64, 1);
1431 }
7bdf406d
TG
1432 }
1433 }
1434}
1435
1436void
1437fm_fmri_zfs_set(nvlist_t *fmri, int version, uint64_t pool_guid,
1438 uint64_t vdev_guid)
1439{
1440 if (version != ZFS_SCHEME_VERSION0) {
0bd31011 1441 atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
7bdf406d
TG
1442 return;
1443 }
1444
1445 if (nvlist_add_uint8(fmri, FM_VERSION, version) != 0) {
0bd31011 1446 atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
7bdf406d
TG
1447 return;
1448 }
1449
1450 if (nvlist_add_string(fmri, FM_FMRI_SCHEME, FM_FMRI_SCHEME_ZFS) != 0) {
0bd31011 1451 atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
7bdf406d
TG
1452 return;
1453 }
1454
1455 if (nvlist_add_uint64(fmri, FM_FMRI_ZFS_POOL, pool_guid) != 0) {
0bd31011 1456 atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
7bdf406d
TG
1457 }
1458
1459 if (vdev_guid != 0) {
1460 if (nvlist_add_uint64(fmri, FM_FMRI_ZFS_VDEV, vdev_guid) != 0) {
0bd31011
TG
1461 atomic_add_64(
1462 &erpt_kstat_data.fmri_set_failed.value.ui64, 1);
7bdf406d
TG
1463 }
1464 }
1465}
1466
1467uint64_t
1468fm_ena_increment(uint64_t ena)
1469{
1470 uint64_t new_ena;
1471
1472 switch (ENA_FORMAT(ena)) {
1473 case FM_ENA_FMT1:
1474 new_ena = ena + (1 << ENA_FMT1_GEN_SHFT);
1475 break;
1476 case FM_ENA_FMT2:
1477 new_ena = ena + (1 << ENA_FMT2_GEN_SHFT);
1478 break;
1479 default:
1480 new_ena = 0;
1481 }
1482
1483 return (new_ena);
1484}
1485
1486uint64_t
1487fm_ena_generate_cpu(uint64_t timestamp, processorid_t cpuid, uchar_t format)
1488{
1489 uint64_t ena = 0;
1490
1491 switch (format) {
1492 case FM_ENA_FMT1:
1493 if (timestamp) {
1494 ena = (uint64_t)((format & ENA_FORMAT_MASK) |
1495 ((cpuid << ENA_FMT1_CPUID_SHFT) &
1496 ENA_FMT1_CPUID_MASK) |
1497 ((timestamp << ENA_FMT1_TIME_SHFT) &
1498 ENA_FMT1_TIME_MASK));
1499 } else {
1500 ena = (uint64_t)((format & ENA_FORMAT_MASK) |
1501 ((cpuid << ENA_FMT1_CPUID_SHFT) &
1502 ENA_FMT1_CPUID_MASK) |
1503 ((gethrtime() << ENA_FMT1_TIME_SHFT) &
1504 ENA_FMT1_TIME_MASK));
1505 }
1506 break;
1507 case FM_ENA_FMT2:
1508 ena = (uint64_t)((format & ENA_FORMAT_MASK) |
1509 ((timestamp << ENA_FMT2_TIME_SHFT) & ENA_FMT2_TIME_MASK));
1510 break;
1511 default:
1512 break;
1513 }
1514
1515 return (ena);
1516}
1517
1518uint64_t
1519fm_ena_generate(uint64_t timestamp, uchar_t format)
1520{
1521 uint64_t ena;
1522
1523 kpreempt_disable();
1524 ena = fm_ena_generate_cpu(timestamp, getcpuid(), format);
1525 kpreempt_enable();
1526
1527 return (ena);
1528}
1529
1530uint64_t
1531fm_ena_generation_get(uint64_t ena)
1532{
1533 uint64_t gen;
1534
1535 switch (ENA_FORMAT(ena)) {
1536 case FM_ENA_FMT1:
1537 gen = (ena & ENA_FMT1_GEN_MASK) >> ENA_FMT1_GEN_SHFT;
1538 break;
1539 case FM_ENA_FMT2:
1540 gen = (ena & ENA_FMT2_GEN_MASK) >> ENA_FMT2_GEN_SHFT;
1541 break;
1542 default:
1543 gen = 0;
1544 break;
1545 }
1546
1547 return (gen);
1548}
1549
1550uchar_t
1551fm_ena_format_get(uint64_t ena)
1552{
1553
1554 return (ENA_FORMAT(ena));
1555}
1556
1557uint64_t
1558fm_ena_id_get(uint64_t ena)
1559{
1560 uint64_t id;
1561
1562 switch (ENA_FORMAT(ena)) {
1563 case FM_ENA_FMT1:
1564 id = (ena & ENA_FMT1_ID_MASK) >> ENA_FMT1_ID_SHFT;
1565 break;
1566 case FM_ENA_FMT2:
1567 id = (ena & ENA_FMT2_ID_MASK) >> ENA_FMT2_ID_SHFT;
1568 break;
1569 default:
1570 id = 0;
1571 }
1572
1573 return (id);
1574}
1575
1576uint64_t
1577fm_ena_time_get(uint64_t ena)
1578{
1579 uint64_t time;
1580
1581 switch (ENA_FORMAT(ena)) {
1582 case FM_ENA_FMT1:
1583 time = (ena & ENA_FMT1_TIME_MASK) >> ENA_FMT1_TIME_SHFT;
1584 break;
1585 case FM_ENA_FMT2:
1586 time = (ena & ENA_FMT2_TIME_MASK) >> ENA_FMT2_TIME_SHFT;
1587 break;
1588 default:
1589 time = 0;
1590 }
1591
1592 return (time);
1593}
1594
1595#ifdef _KERNEL
1596void
1597fm_init(void)
1598{
1599 zevent_len_cur = 0;
1600 zevent_flags = 0;
1601
1602 if (zfs_zevent_len_max == 0)
1603 zfs_zevent_len_max = ERPT_MAX_ERRS * MAX(max_ncpus, 4);
1604
1605 /* Initialize zevent allocation and generation kstats */
1606 fm_ksp = kstat_create("zfs", 0, "fm", "misc", KSTAT_TYPE_NAMED,
1607 sizeof (struct erpt_kstat) / sizeof (kstat_named_t),
1608 KSTAT_FLAG_VIRTUAL);
1609
1610 if (fm_ksp != NULL) {
1611 fm_ksp->ks_data = &erpt_kstat_data;
1612 kstat_install(fm_ksp);
1613 } else {
1614 cmn_err(CE_NOTE, "failed to create fm/misc kstat\n");
1615 }
1616
1617 mutex_init(&zevent_lock, NULL, MUTEX_DEFAULT, NULL);
1618 list_create(&zevent_list, sizeof (zevent_t),
1619 offsetof(zevent_t, ev_node));
1620 cv_init(&zevent_cv, NULL, CV_DEFAULT, NULL);
1621}
1622
1623void
1624fm_fini(void)
1625{
1626 int count;
1627
1628 zfs_zevent_drain_all(&count);
1629
1630 mutex_enter(&zevent_lock);
1631 cv_broadcast(&zevent_cv);
1632
1633 zevent_flags |= ZEVENT_SHUTDOWN;
1634 while (zevent_waiters > 0) {
1635 mutex_exit(&zevent_lock);
1636 schedule();
1637 mutex_enter(&zevent_lock);
1638 }
1639 mutex_exit(&zevent_lock);
1640
1641 cv_destroy(&zevent_cv);
1642 list_destroy(&zevent_list);
1643 mutex_destroy(&zevent_lock);
1644
1645 if (fm_ksp != NULL) {
1646 kstat_delete(fm_ksp);
1647 fm_ksp = NULL;
1648 }
1649}
1650
1651module_param(zfs_zevent_len_max, int, 0644);
1652MODULE_PARM_DESC(zfs_zevent_len_max, "Max event queue length");
1653
1654module_param(zfs_zevent_cols, int, 0644);
1655MODULE_PARM_DESC(zfs_zevent_cols, "Max event column width");
1656
1657module_param(zfs_zevent_console, int, 0644);
1658MODULE_PARM_DESC(zfs_zevent_console, "Log events to the console");
1659
1660#endif /* _KERNEL */