]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/fm.c
Change KM_PUSHPAGE -> KM_SLEEP
[mirror_zfs.git] / module / zfs / fm.c
CommitLineData
fa42225a
BB
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/*
428870ff 22 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
fa42225a
BB
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>
26685276 56#include <sys/list.h>
fa42225a
BB
57#include <sys/nvpair.h>
58#include <sys/cmn_err.h>
fa42225a 59#include <sys/sysmacros.h>
fa42225a 60#include <sys/compress.h>
fa42225a
BB
61#include <sys/sunddi.h>
62#include <sys/systeminfo.h>
fa42225a
BB
63#include <sys/fm/util.h>
64#include <sys/fm/protocol.h>
26685276
BB
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>
fa42225a 78
c409e464
BB
79int zfs_zevent_len_max = 0;
80int zfs_zevent_cols = 80;
81int zfs_zevent_console = 0;
fa42225a 82
26685276
BB
83static int zevent_len_cur = 0;
84static int zevent_waiters = 0;
85static int zevent_flags = 0;
fa42225a 86
a2f1945e
BB
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
26685276
BB
95static kmutex_t zevent_lock;
96static list_t zevent_list;
97static kcondvar_t zevent_cv;
98#endif /* _KERNEL */
fa42225a 99
428870ff
BB
100extern void fastreboot_disable_highpil(void);
101
fa42225a 102/*
26685276 103 * Common fault management kstats to record event generation failures
fa42225a
BB
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
26685276 120kstat_t *fm_ksp;
fa42225a 121
26685276 122#ifdef _KERNEL
fa42225a
BB
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) {
26685276 141 console_printf("\n");
fa42225a
BB
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/*
157 * Recursively print a nvlist in the specified column width and return the
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);
26685276 203 c = fm_printf(d + 1, c, cols, "0x%x", i8);
fa42225a
BB
204 break;
205
206 case DATA_TYPE_INT8:
207 (void) nvpair_value_int8(nvp, (void *)&i8);
26685276 208 c = fm_printf(d + 1, c, cols, "0x%x", i8);
fa42225a
BB
209 break;
210
211 case DATA_TYPE_UINT8:
212 (void) nvpair_value_uint8(nvp, &i8);
26685276 213 c = fm_printf(d + 1, c, cols, "0x%x", i8);
fa42225a
BB
214 break;
215
216 case DATA_TYPE_INT16:
217 (void) nvpair_value_int16(nvp, (void *)&i16);
26685276 218 c = fm_printf(d + 1, c, cols, "0x%x", i16);
fa42225a
BB
219 break;
220
221 case DATA_TYPE_UINT16:
222 (void) nvpair_value_uint16(nvp, &i16);
26685276 223 c = fm_printf(d + 1, c, cols, "0x%x", i16);
fa42225a
BB
224 break;
225
226 case DATA_TYPE_INT32:
227 (void) nvpair_value_int32(nvp, (void *)&i32);
26685276 228 c = fm_printf(d + 1, c, cols, "0x%x", i32);
fa42225a
BB
229 break;
230
231 case DATA_TYPE_UINT32:
232 (void) nvpair_value_uint32(nvp, &i32);
26685276 233 c = fm_printf(d + 1, c, cols, "0x%x", i32);
fa42225a
BB
234 break;
235
236 case DATA_TYPE_INT64:
237 (void) nvpair_value_int64(nvp, (void *)&i64);
26685276 238 c = fm_printf(d + 1, c, cols, "0x%llx",
fa42225a
BB
239 (u_longlong_t)i64);
240 break;
241
242 case DATA_TYPE_UINT64:
243 (void) nvpair_value_uint64(nvp, &i64);
26685276 244 c = fm_printf(d + 1, c, cols, "0x%llx",
fa42225a
BB
245 (u_longlong_t)i64);
246 break;
247
248 case DATA_TYPE_HRTIME:
249 (void) nvpair_value_hrtime(nvp, (void *)&i64);
26685276 250 c = fm_printf(d + 1, c, cols, "0x%llx",
fa42225a
BB
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
26685276
BB
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++)
d1d7e268
MK
287 c = fm_printf(d + 1, c, cols, "0x%llx ",
288 (u_longlong_t)val[i]);
26685276
BB
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++)
d1d7e268
MK
301 c = fm_printf(d + 1, c, cols, "0x%llx ",
302 (u_longlong_t)val[i]);
26685276
BB
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++)
d1d7e268
MK
315 c = fm_printf(d + 1, c, cols, "0x%llx ",
316 (u_longlong_t)val[i]);
26685276
BB
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++)
d1d7e268
MK
329 c = fm_printf(d + 1, c, cols, "0x%llx ",
330 (u_longlong_t)val[i]);
26685276
BB
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++)
d1d7e268
MK
343 c = fm_printf(d + 1, c, cols, "0x%llx ",
344 (u_longlong_t)val[i]);
26685276
BB
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++)
d1d7e268
MK
357 c = fm_printf(d + 1, c, cols, "0x%llx ",
358 (u_longlong_t)val[i]);
26685276
BB
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++)
d1d7e268
MK
371 c = fm_printf(d + 1, c, cols, "0x%llx ",
372 (u_longlong_t)val[i]);
26685276
BB
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++)
d1d7e268
MK
385 c = fm_printf(d + 1, c, cols, "0x%llx ",
386 (u_longlong_t)val[i]);
26685276
BB
387
388 c = fm_printf(d + 1, c, cols, "]");
389 break;
390 }
391
392 case DATA_TYPE_STRING_ARRAY:
fa42225a
BB
393 case DATA_TYPE_BOOLEAN_ARRAY:
394 case DATA_TYPE_BYTE_ARRAY:
fa42225a
BB
395 c = fm_printf(d + 1, c, cols, "[...]");
396 break;
26685276 397
fa42225a
BB
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
26685276 413 console_printf("\n");
fa42225a
BB
414
415 if (nvlist_lookup_string(nvl, FM_CLASS, &class) == 0)
c409e464 416 c = fm_printf(0, c, zfs_zevent_cols, "%s", class);
fa42225a 417
c409e464 418 if (fm_nvprintr(nvl, 0, c, zfs_zevent_cols) != 0)
fa42225a
BB
419 console_printf("\n");
420
421 console_printf("\n");
422}
423
26685276
BB
424static zevent_t *
425zfs_zevent_alloc(void)
426{
427 zevent_t *ev;
428
79c76d5b 429 ev = kmem_zalloc(sizeof (zevent_t), KM_SLEEP);
26685276 430 if (ev == NULL)
d1d7e268 431 return (NULL);
26685276 432
d1d7e268 433 list_create(&ev->ev_ze_list, sizeof (zfs_zevent_t),
26685276
BB
434 offsetof(zfs_zevent_t, ze_node));
435 list_link_init(&ev->ev_node);
436
d1d7e268 437 return (ev);
26685276
BB
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);
d1d7e268 447 kmem_free(ev, sizeof (zevent_t));
26685276
BB
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
fa42225a 468void
26685276 469zfs_zevent_drain_all(int *count)
fa42225a 470{
26685276 471 zevent_t *ev;
fa42225a 472
26685276
BB
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);
fa42225a
BB
480}
481
572e2857 482/*
26685276
BB
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.
572e2857 488 */
26685276
BB
489static void
490zfs_zevent_insert(zevent_t *ev)
572e2857 491{
99db9bfd 492 ASSERT(MUTEX_HELD(&zevent_lock));
26685276 493 list_insert_head(&zevent_list, ev);
99db9bfd 494
c409e464 495 if (zevent_len_cur >= zfs_zevent_len_max)
26685276 496 zfs_zevent_drain(list_tail(&zevent_list));
572e2857 497 else
26685276 498 zevent_len_cur++;
572e2857
BB
499}
500
fa42225a 501/*
0426c168
IH
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.
fa42225a 507 */
0426c168 508int
26685276 509zfs_zevent_post(nvlist_t *nvl, nvlist_t *detector, zevent_cb_t *cb)
fa42225a 510{
26685276
BB
511 int64_t tv_array[2];
512 timestruc_t tv;
a2f1945e 513 uint64_t eid;
26685276
BB
514 size_t nvl_size = 0;
515 zevent_t *ev;
0426c168
IH
516 int error;
517
518 ASSERT(cb != NULL);
fa42225a 519
26685276
BB
520 gethrestime(&tv);
521 tv_array[0] = tv.tv_sec;
522 tv_array[1] = tv.tv_nsec;
0426c168
IH
523
524 error = nvlist_add_int64_array(nvl, FM_EREPORT_TIME, tv_array, 2);
525 if (error) {
26685276 526 atomic_add_64(&erpt_kstat_data.erpt_set_failed.value.ui64, 1);
0426c168 527 goto out;
26685276 528 }
fa42225a 529
a2f1945e 530 eid = atomic_inc_64_nv(&zevent_eid);
0426c168
IH
531 error = nvlist_add_uint64(nvl, FM_EREPORT_EID, eid);
532 if (error) {
a2f1945e 533 atomic_add_64(&erpt_kstat_data.erpt_set_failed.value.ui64, 1);
0426c168
IH
534 goto out;
535 }
536
537 error = nvlist_size(nvl, &nvl_size, NV_ENCODE_NATIVE);
538 if (error) {
539 atomic_add_64(&erpt_kstat_data.erpt_dropped.value.ui64, 1);
540 goto out;
a2f1945e
BB
541 }
542
26685276
BB
543 if (nvl_size > ERPT_DATA_SZ || nvl_size == 0) {
544 atomic_add_64(&erpt_kstat_data.erpt_dropped.value.ui64, 1);
0426c168
IH
545 error = EOVERFLOW;
546 goto out;
fa42225a
BB
547 }
548
c409e464 549 if (zfs_zevent_console)
26685276 550 fm_nvprint(nvl);
fa42225a 551
26685276
BB
552 ev = zfs_zevent_alloc();
553 if (ev == NULL) {
554 atomic_add_64(&erpt_kstat_data.erpt_dropped.value.ui64, 1);
0426c168
IH
555 error = ENOMEM;
556 goto out;
26685276 557 }
fa42225a 558
d1d7e268 559 ev->ev_nvl = nvl;
26685276
BB
560 ev->ev_detector = detector;
561 ev->ev_cb = cb;
a2f1945e 562 ev->ev_eid = eid;
99db9bfd
BB
563
564 mutex_enter(&zevent_lock);
26685276
BB
565 zfs_zevent_insert(ev);
566 cv_broadcast(&zevent_cv);
99db9bfd 567 mutex_exit(&zevent_lock);
0426c168
IH
568
569out:
570 if (error)
571 cb(nvl, detector);
572
573 return (error);
26685276 574}
fa42225a 575
26685276
BB
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);
fa42225a 582
26685276
BB
583 return (0);
584}
fa42225a 585
26685276
BB
586int
587zfs_zevent_fd_hold(int fd, minor_t *minorp, zfs_zevent_t **ze)
588{
589 file_t *fp;
590 int error;
591
d1d7e268
MK
592 fp = getf(fd);
593 if (fp == NULL)
594 return (EBADF);
26685276 595
d1d7e268
MK
596 *minorp = zfsdev_getminor(fp->f_file);
597 error = zfs_zevent_minor_to_state(*minorp, ze);
26685276
BB
598
599 if (error)
600 zfs_zevent_fd_rele(fd);
601
602 return (error);
603}
604
605void
606zfs_zevent_fd_rele(int fd)
607{
608 releasef(fd);
fa42225a
BB
609}
610
611/*
baa40d45
BB
612 * Get the next zevent in the stream and place a copy in 'event'. This
613 * may fail with ENOMEM if the encoded nvlist size exceeds the passed
614 * 'event_size'. In this case the stream pointer is not advanced and
615 * and 'event_size' is set to the minimum required buffer size.
fa42225a 616 */
26685276 617int
baa40d45 618zfs_zevent_next(zfs_zevent_t *ze, nvlist_t **event, uint64_t *event_size,
d1d7e268 619 uint64_t *dropped)
fa42225a 620{
26685276 621 zevent_t *ev;
baa40d45
BB
622 size_t size;
623 int error = 0;
26685276
BB
624
625 mutex_enter(&zevent_lock);
626 if (ze->ze_zevent == NULL) {
627 /* New stream start at the beginning/tail */
628 ev = list_tail(&zevent_list);
629 if (ev == NULL) {
630 error = ENOENT;
631 goto out;
632 }
fa42225a 633 } else {
d1d7e268
MK
634 /*
635 * Existing stream continue with the next element and remove
636 * ourselves from the wait queue for the previous element
637 */
26685276
BB
638 ev = list_prev(&zevent_list, ze->ze_zevent);
639 if (ev == NULL) {
640 error = ENOENT;
641 goto out;
642 }
baa40d45 643 }
26685276 644
baa40d45
BB
645 VERIFY(nvlist_size(ev->ev_nvl, &size, NV_ENCODE_NATIVE) == 0);
646 if (size > *event_size) {
647 *event_size = size;
648 error = ENOMEM;
649 goto out;
fa42225a
BB
650 }
651
baa40d45
BB
652 if (ze->ze_zevent)
653 list_remove(&ze->ze_zevent->ev_ze_list, ze);
654
26685276
BB
655 ze->ze_zevent = ev;
656 list_insert_head(&ev->ev_ze_list, ze);
657 nvlist_dup(ev->ev_nvl, event, KM_SLEEP);
658 *dropped = ze->ze_dropped;
659 ze->ze_dropped = 0;
660out:
661 mutex_exit(&zevent_lock);
fa42225a 662
d1d7e268 663 return (error);
26685276
BB
664}
665
666int
667zfs_zevent_wait(zfs_zevent_t *ze)
668{
669 int error = 0;
670
671 mutex_enter(&zevent_lock);
fa42225a 672
26685276
BB
673 if (zevent_flags & ZEVENT_SHUTDOWN) {
674 error = ESHUTDOWN;
675 goto out;
fa42225a
BB
676 }
677
26685276
BB
678 zevent_waiters++;
679 cv_wait_interruptible(&zevent_cv, &zevent_lock);
680 if (issig(JUSTLOOKING))
681 error = EINTR;
682
683 zevent_waiters--;
684out:
685 mutex_exit(&zevent_lock);
686
d1d7e268 687 return (error);
fa42225a
BB
688}
689
75e3ff58
BB
690/*
691 * The caller may seek to a specific EID by passing that EID. If the EID
692 * is still available in the posted list of events the cursor is positioned
693 * there. Otherwise ENOENT is returned and the cursor is not moved.
694 *
695 * There are two reserved EIDs which may be passed and will never fail.
696 * ZEVENT_SEEK_START positions the cursor at the start of the list, and
697 * ZEVENT_SEEK_END positions the cursor at the end of the list.
698 */
699int
700zfs_zevent_seek(zfs_zevent_t *ze, uint64_t eid)
701{
702 zevent_t *ev;
703 int error = 0;
704
705 mutex_enter(&zevent_lock);
706
707 if (eid == ZEVENT_SEEK_START) {
708 if (ze->ze_zevent)
709 list_remove(&ze->ze_zevent->ev_ze_list, ze);
710
711 ze->ze_zevent = NULL;
712 goto out;
713 }
714
715 if (eid == ZEVENT_SEEK_END) {
716 if (ze->ze_zevent)
717 list_remove(&ze->ze_zevent->ev_ze_list, ze);
718
719 ev = list_head(&zevent_list);
720 if (ev) {
721 ze->ze_zevent = ev;
722 list_insert_head(&ev->ev_ze_list, ze);
723 } else {
724 ze->ze_zevent = NULL;
725 }
726
727 goto out;
728 }
729
730 for (ev = list_tail(&zevent_list); ev != NULL;
731 ev = list_prev(&zevent_list, ev)) {
732 if (ev->ev_eid == eid) {
733 if (ze->ze_zevent)
734 list_remove(&ze->ze_zevent->ev_ze_list, ze);
735
736 ze->ze_zevent = ev;
737 list_insert_head(&ev->ev_ze_list, ze);
738 break;
739 }
740 }
741
742 if (ev == NULL)
743 error = ENOENT;
744
745out:
746 mutex_exit(&zevent_lock);
747
748 return (error);
749}
750
fa42225a 751void
26685276 752zfs_zevent_init(zfs_zevent_t **zep)
fa42225a 753{
26685276 754 zfs_zevent_t *ze;
fa42225a 755
26685276
BB
756 ze = *zep = kmem_zalloc(sizeof (zfs_zevent_t), KM_SLEEP);
757 list_link_init(&ze->ze_node);
758}
fa42225a 759
26685276
BB
760void
761zfs_zevent_destroy(zfs_zevent_t *ze)
762{
763 mutex_enter(&zevent_lock);
764 if (ze->ze_zevent)
765 list_remove(&ze->ze_zevent->ev_ze_list, ze);
766 mutex_exit(&zevent_lock);
fa42225a 767
26685276 768 kmem_free(ze, sizeof (zfs_zevent_t));
fa42225a 769}
26685276 770#endif /* _KERNEL */
fa42225a
BB
771
772/*
773 * Wrapppers for FM nvlist allocators
774 */
775/* ARGSUSED */
776static void *
777i_fm_alloc(nv_alloc_t *nva, size_t size)
778{
79c76d5b 779 return (kmem_zalloc(size, KM_SLEEP));
fa42225a
BB
780}
781
782/* ARGSUSED */
783static void
784i_fm_free(nv_alloc_t *nva, void *buf, size_t size)
785{
786 kmem_free(buf, size);
787}
788
789const nv_alloc_ops_t fm_mem_alloc_ops = {
790 NULL,
791 NULL,
792 i_fm_alloc,
793 i_fm_free,
794 NULL
795};
796
797/*
798 * Create and initialize a new nv_alloc_t for a fixed buffer, buf. A pointer
799 * to the newly allocated nv_alloc_t structure is returned upon success or NULL
800 * is returned to indicate that the nv_alloc structure could not be created.
801 */
802nv_alloc_t *
803fm_nva_xcreate(char *buf, size_t bufsz)
804{
805 nv_alloc_t *nvhdl = kmem_zalloc(sizeof (nv_alloc_t), KM_SLEEP);
806
807 if (bufsz == 0 || nv_alloc_init(nvhdl, nv_fixed_ops, buf, bufsz) != 0) {
808 kmem_free(nvhdl, sizeof (nv_alloc_t));
809 return (NULL);
810 }
811
812 return (nvhdl);
813}
814
815/*
816 * Destroy a previously allocated nv_alloc structure. The fixed buffer
817 * associated with nva must be freed by the caller.
818 */
819void
820fm_nva_xdestroy(nv_alloc_t *nva)
821{
822 nv_alloc_fini(nva);
823 kmem_free(nva, sizeof (nv_alloc_t));
824}
825
826/*
827 * Create a new nv list. A pointer to a new nv list structure is returned
828 * upon success or NULL is returned to indicate that the structure could
829 * not be created. The newly created nv list is created and managed by the
830 * operations installed in nva. If nva is NULL, the default FMA nva
831 * operations are installed and used.
832 *
833 * When called from the kernel and nva == NULL, this function must be called
834 * from passive kernel context with no locks held that can prevent a
835 * sleeping memory allocation from occurring. Otherwise, this function may
836 * be called from other kernel contexts as long a valid nva created via
837 * fm_nva_create() is supplied.
838 */
839nvlist_t *
840fm_nvlist_create(nv_alloc_t *nva)
841{
842 int hdl_alloced = 0;
843 nvlist_t *nvl;
844 nv_alloc_t *nvhdl;
845
846 if (nva == NULL) {
79c76d5b 847 nvhdl = kmem_zalloc(sizeof (nv_alloc_t), KM_SLEEP);
fa42225a
BB
848
849 if (nv_alloc_init(nvhdl, &fm_mem_alloc_ops, NULL, 0) != 0) {
850 kmem_free(nvhdl, sizeof (nv_alloc_t));
851 return (NULL);
852 }
853 hdl_alloced = 1;
854 } else {
855 nvhdl = nva;
856 }
857
858 if (nvlist_xalloc(&nvl, NV_UNIQUE_NAME, nvhdl) != 0) {
859 if (hdl_alloced) {
fa42225a 860 nv_alloc_fini(nvhdl);
572e2857 861 kmem_free(nvhdl, sizeof (nv_alloc_t));
fa42225a
BB
862 }
863 return (NULL);
864 }
865
866 return (nvl);
867}
868
869/*
870 * Destroy a previously allocated nvlist structure. flag indicates whether
871 * or not the associated nva structure should be freed (FM_NVA_FREE) or
872 * retained (FM_NVA_RETAIN). Retaining the nv alloc structure allows
873 * it to be re-used for future nvlist creation operations.
874 */
875void
876fm_nvlist_destroy(nvlist_t *nvl, int flag)
877{
878 nv_alloc_t *nva = nvlist_lookup_nv_alloc(nvl);
879
880 nvlist_free(nvl);
881
882 if (nva != NULL) {
883 if (flag == FM_NVA_FREE)
884 fm_nva_xdestroy(nva);
885 }
886}
887
888int
889i_fm_payload_set(nvlist_t *payload, const char *name, va_list ap)
890{
891 int nelem, ret = 0;
892 data_type_t type;
893
894 while (ret == 0 && name != NULL) {
895 type = va_arg(ap, data_type_t);
896 switch (type) {
897 case DATA_TYPE_BYTE:
898 ret = nvlist_add_byte(payload, name,
899 va_arg(ap, uint_t));
900 break;
901 case DATA_TYPE_BYTE_ARRAY:
902 nelem = va_arg(ap, int);
903 ret = nvlist_add_byte_array(payload, name,
904 va_arg(ap, uchar_t *), nelem);
905 break;
906 case DATA_TYPE_BOOLEAN_VALUE:
907 ret = nvlist_add_boolean_value(payload, name,
908 va_arg(ap, boolean_t));
909 break;
910 case DATA_TYPE_BOOLEAN_ARRAY:
911 nelem = va_arg(ap, int);
912 ret = nvlist_add_boolean_array(payload, name,
913 va_arg(ap, boolean_t *), nelem);
914 break;
915 case DATA_TYPE_INT8:
916 ret = nvlist_add_int8(payload, name,
917 va_arg(ap, int));
918 break;
919 case DATA_TYPE_INT8_ARRAY:
920 nelem = va_arg(ap, int);
921 ret = nvlist_add_int8_array(payload, name,
922 va_arg(ap, int8_t *), nelem);
923 break;
924 case DATA_TYPE_UINT8:
925 ret = nvlist_add_uint8(payload, name,
926 va_arg(ap, uint_t));
927 break;
928 case DATA_TYPE_UINT8_ARRAY:
929 nelem = va_arg(ap, int);
930 ret = nvlist_add_uint8_array(payload, name,
931 va_arg(ap, uint8_t *), nelem);
932 break;
933 case DATA_TYPE_INT16:
934 ret = nvlist_add_int16(payload, name,
935 va_arg(ap, int));
936 break;
937 case DATA_TYPE_INT16_ARRAY:
938 nelem = va_arg(ap, int);
939 ret = nvlist_add_int16_array(payload, name,
940 va_arg(ap, int16_t *), nelem);
941 break;
942 case DATA_TYPE_UINT16:
943 ret = nvlist_add_uint16(payload, name,
944 va_arg(ap, uint_t));
945 break;
946 case DATA_TYPE_UINT16_ARRAY:
947 nelem = va_arg(ap, int);
948 ret = nvlist_add_uint16_array(payload, name,
949 va_arg(ap, uint16_t *), nelem);
950 break;
951 case DATA_TYPE_INT32:
952 ret = nvlist_add_int32(payload, name,
953 va_arg(ap, int32_t));
954 break;
955 case DATA_TYPE_INT32_ARRAY:
956 nelem = va_arg(ap, int);
957 ret = nvlist_add_int32_array(payload, name,
958 va_arg(ap, int32_t *), nelem);
959 break;
960 case DATA_TYPE_UINT32:
961 ret = nvlist_add_uint32(payload, name,
962 va_arg(ap, uint32_t));
963 break;
964 case DATA_TYPE_UINT32_ARRAY:
965 nelem = va_arg(ap, int);
966 ret = nvlist_add_uint32_array(payload, name,
967 va_arg(ap, uint32_t *), nelem);
968 break;
969 case DATA_TYPE_INT64:
970 ret = nvlist_add_int64(payload, name,
971 va_arg(ap, int64_t));
972 break;
973 case DATA_TYPE_INT64_ARRAY:
974 nelem = va_arg(ap, int);
975 ret = nvlist_add_int64_array(payload, name,
976 va_arg(ap, int64_t *), nelem);
977 break;
978 case DATA_TYPE_UINT64:
979 ret = nvlist_add_uint64(payload, name,
980 va_arg(ap, uint64_t));
981 break;
982 case DATA_TYPE_UINT64_ARRAY:
983 nelem = va_arg(ap, int);
984 ret = nvlist_add_uint64_array(payload, name,
985 va_arg(ap, uint64_t *), nelem);
986 break;
987 case DATA_TYPE_STRING:
988 ret = nvlist_add_string(payload, name,
989 va_arg(ap, char *));
990 break;
991 case DATA_TYPE_STRING_ARRAY:
992 nelem = va_arg(ap, int);
993 ret = nvlist_add_string_array(payload, name,
994 va_arg(ap, char **), nelem);
995 break;
996 case DATA_TYPE_NVLIST:
997 ret = nvlist_add_nvlist(payload, name,
998 va_arg(ap, nvlist_t *));
999 break;
1000 case DATA_TYPE_NVLIST_ARRAY:
1001 nelem = va_arg(ap, int);
1002 ret = nvlist_add_nvlist_array(payload, name,
1003 va_arg(ap, nvlist_t **), nelem);
1004 break;
1005 default:
1006 ret = EINVAL;
1007 }
1008
1009 name = va_arg(ap, char *);
1010 }
1011 return (ret);
1012}
1013
1014void
1015fm_payload_set(nvlist_t *payload, ...)
1016{
1017 int ret;
1018 const char *name;
1019 va_list ap;
1020
1021 va_start(ap, payload);
1022 name = va_arg(ap, char *);
1023 ret = i_fm_payload_set(payload, name, ap);
1024 va_end(ap);
1025
1026 if (ret)
1027 atomic_add_64(
1028 &erpt_kstat_data.payload_set_failed.value.ui64, 1);
1029}
1030
1031/*
1032 * Set-up and validate the members of an ereport event according to:
1033 *
1034 * Member name Type Value
1035 * ====================================================
1036 * class string ereport
1037 * version uint8_t 0
1038 * ena uint64_t <ena>
1039 * detector nvlist_t <detector>
1040 * ereport-payload nvlist_t <var args>
1041 *
428870ff
BB
1042 * We don't actually add a 'version' member to the payload. Really,
1043 * the version quoted to us by our caller is that of the category 1
1044 * "ereport" event class (and we require FM_EREPORT_VERS0) but
1045 * the payload version of the actual leaf class event under construction
1046 * may be something else. Callers should supply a version in the varargs,
1047 * or (better) we could take two version arguments - one for the
1048 * ereport category 1 classification (expect FM_EREPORT_VERS0) and one
1049 * for the leaf class.
fa42225a
BB
1050 */
1051void
1052fm_ereport_set(nvlist_t *ereport, int version, const char *erpt_class,
1053 uint64_t ena, const nvlist_t *detector, ...)
1054{
1055 char ereport_class[FM_MAX_CLASS];
1056 const char *name;
1057 va_list ap;
1058 int ret;
1059
1060 if (version != FM_EREPORT_VERS0) {
1061 atomic_add_64(&erpt_kstat_data.erpt_set_failed.value.ui64, 1);
1062 return;
1063 }
1064
1065 (void) snprintf(ereport_class, FM_MAX_CLASS, "%s.%s",
1066 FM_EREPORT_CLASS, erpt_class);
1067 if (nvlist_add_string(ereport, FM_CLASS, ereport_class) != 0) {
1068 atomic_add_64(&erpt_kstat_data.erpt_set_failed.value.ui64, 1);
1069 return;
1070 }
1071
1072 if (nvlist_add_uint64(ereport, FM_EREPORT_ENA, ena)) {
1073 atomic_add_64(&erpt_kstat_data.erpt_set_failed.value.ui64, 1);
1074 }
1075
1076 if (nvlist_add_nvlist(ereport, FM_EREPORT_DETECTOR,
1077 (nvlist_t *)detector) != 0) {
1078 atomic_add_64(&erpt_kstat_data.erpt_set_failed.value.ui64, 1);
1079 }
1080
1081 va_start(ap, detector);
1082 name = va_arg(ap, const char *);
1083 ret = i_fm_payload_set(ereport, name, ap);
1084 va_end(ap);
1085
1086 if (ret)
1087 atomic_add_64(&erpt_kstat_data.erpt_set_failed.value.ui64, 1);
1088}
1089
1090/*
1091 * Set-up and validate the members of an hc fmri according to;
1092 *
1093 * Member name Type Value
1094 * ===================================================
1095 * version uint8_t 0
1096 * auth nvlist_t <auth>
1097 * hc-name string <name>
1098 * hc-id string <id>
1099 *
1100 * Note that auth and hc-id are optional members.
1101 */
1102
1103#define HC_MAXPAIRS 20
1104#define HC_MAXNAMELEN 50
1105
1106static int
1107fm_fmri_hc_set_common(nvlist_t *fmri, int version, const nvlist_t *auth)
1108{
1109 if (version != FM_HC_SCHEME_VERSION) {
1110 atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
1111 return (0);
1112 }
1113
1114 if (nvlist_add_uint8(fmri, FM_VERSION, version) != 0 ||
1115 nvlist_add_string(fmri, FM_FMRI_SCHEME, FM_FMRI_SCHEME_HC) != 0) {
1116 atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
1117 return (0);
1118 }
1119
1120 if (auth != NULL && nvlist_add_nvlist(fmri, FM_FMRI_AUTHORITY,
1121 (nvlist_t *)auth) != 0) {
1122 atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
1123 return (0);
1124 }
1125
1126 return (1);
1127}
1128
1129void
1130fm_fmri_hc_set(nvlist_t *fmri, int version, const nvlist_t *auth,
1131 nvlist_t *snvl, int npairs, ...)
1132{
1133 nv_alloc_t *nva = nvlist_lookup_nv_alloc(fmri);
1134 nvlist_t *pairs[HC_MAXPAIRS];
1135 va_list ap;
1136 int i;
1137
1138 if (!fm_fmri_hc_set_common(fmri, version, auth))
1139 return;
1140
1141 npairs = MIN(npairs, HC_MAXPAIRS);
1142
1143 va_start(ap, npairs);
1144 for (i = 0; i < npairs; i++) {
1145 const char *name = va_arg(ap, const char *);
1146 uint32_t id = va_arg(ap, uint32_t);
1147 char idstr[11];
1148
1149 (void) snprintf(idstr, sizeof (idstr), "%u", id);
1150
1151 pairs[i] = fm_nvlist_create(nva);
1152 if (nvlist_add_string(pairs[i], FM_FMRI_HC_NAME, name) != 0 ||
1153 nvlist_add_string(pairs[i], FM_FMRI_HC_ID, idstr) != 0) {
1154 atomic_add_64(
1155 &erpt_kstat_data.fmri_set_failed.value.ui64, 1);
1156 }
1157 }
1158 va_end(ap);
1159
1160 if (nvlist_add_nvlist_array(fmri, FM_FMRI_HC_LIST, pairs, npairs) != 0)
1161 atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
1162
1163 for (i = 0; i < npairs; i++)
1164 fm_nvlist_destroy(pairs[i], FM_NVA_RETAIN);
1165
1166 if (snvl != NULL) {
1167 if (nvlist_add_nvlist(fmri, FM_FMRI_HC_SPECIFIC, snvl) != 0) {
1168 atomic_add_64(
1169 &erpt_kstat_data.fmri_set_failed.value.ui64, 1);
1170 }
1171 }
1172}
1173
26685276
BB
1174void
1175fm_fmri_hc_create(nvlist_t *fmri, int version, const nvlist_t *auth,
1176 nvlist_t *snvl, nvlist_t *bboard, int npairs, ...)
1177{
1178 nv_alloc_t *nva = nvlist_lookup_nv_alloc(fmri);
1179 nvlist_t *pairs[HC_MAXPAIRS];
1180 nvlist_t **hcl;
1181 uint_t n;
1182 int i, j;
1183 va_list ap;
1184 char *hcname, *hcid;
1185
1186 if (!fm_fmri_hc_set_common(fmri, version, auth))
1187 return;
1188
1189 /*
1190 * copy the bboard nvpairs to the pairs array
1191 */
1192 if (nvlist_lookup_nvlist_array(bboard, FM_FMRI_HC_LIST, &hcl, &n)
1193 != 0) {
1194 atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
1195 return;
1196 }
1197
1198 for (i = 0; i < n; i++) {
1199 if (nvlist_lookup_string(hcl[i], FM_FMRI_HC_NAME,
1200 &hcname) != 0) {
1201 atomic_add_64(
1202 &erpt_kstat_data.fmri_set_failed.value.ui64, 1);
1203 return;
1204 }
1205 if (nvlist_lookup_string(hcl[i], FM_FMRI_HC_ID, &hcid) != 0) {
1206 atomic_add_64(
1207 &erpt_kstat_data.fmri_set_failed.value.ui64, 1);
1208 return;
1209 }
1210
1211 pairs[i] = fm_nvlist_create(nva);
1212 if (nvlist_add_string(pairs[i], FM_FMRI_HC_NAME, hcname) != 0 ||
1213 nvlist_add_string(pairs[i], FM_FMRI_HC_ID, hcid) != 0) {
1214 for (j = 0; j <= i; j++) {
1215 if (pairs[j] != NULL)
1216 fm_nvlist_destroy(pairs[j],
1217 FM_NVA_RETAIN);
1218 }
1219 atomic_add_64(
1220 &erpt_kstat_data.fmri_set_failed.value.ui64, 1);
1221 return;
1222 }
1223 }
1224
1225 /*
1226 * create the pairs from passed in pairs
1227 */
1228 npairs = MIN(npairs, HC_MAXPAIRS);
1229
1230 va_start(ap, npairs);
1231 for (i = n; i < npairs + n; i++) {
1232 const char *name = va_arg(ap, const char *);
1233 uint32_t id = va_arg(ap, uint32_t);
1234 char idstr[11];
1235 (void) snprintf(idstr, sizeof (idstr), "%u", id);
1236 pairs[i] = fm_nvlist_create(nva);
1237 if (nvlist_add_string(pairs[i], FM_FMRI_HC_NAME, name) != 0 ||
1238 nvlist_add_string(pairs[i], FM_FMRI_HC_ID, idstr) != 0) {
1239 for (j = 0; j <= i; j++) {
1240 if (pairs[j] != NULL)
1241 fm_nvlist_destroy(pairs[j],
1242 FM_NVA_RETAIN);
1243 }
1244 atomic_add_64(
1245 &erpt_kstat_data.fmri_set_failed.value.ui64, 1);
1246 return;
1247 }
1248 }
1249 va_end(ap);
1250
1251 /*
1252 * Create the fmri hc list
1253 */
1254 if (nvlist_add_nvlist_array(fmri, FM_FMRI_HC_LIST, pairs,
1255 npairs + n) != 0) {
1256 atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
1257 return;
1258 }
1259
1260 for (i = 0; i < npairs + n; i++) {
1261 fm_nvlist_destroy(pairs[i], FM_NVA_RETAIN);
1262 }
1263
1264 if (snvl != NULL) {
1265 if (nvlist_add_nvlist(fmri, FM_FMRI_HC_SPECIFIC, snvl) != 0) {
1266 atomic_add_64(
1267 &erpt_kstat_data.fmri_set_failed.value.ui64, 1);
1268 return;
1269 }
1270 }
1271}
1272
fa42225a
BB
1273/*
1274 * Set-up and validate the members of an dev fmri according to:
1275 *
1276 * Member name Type Value
1277 * ====================================================
1278 * version uint8_t 0
1279 * auth nvlist_t <auth>
1280 * devpath string <devpath>
428870ff
BB
1281 * [devid] string <devid>
1282 * [target-port-l0id] string <target-port-lun0-id>
fa42225a
BB
1283 *
1284 * Note that auth and devid are optional members.
1285 */
1286void
1287fm_fmri_dev_set(nvlist_t *fmri_dev, int version, const nvlist_t *auth,
428870ff 1288 const char *devpath, const char *devid, const char *tpl0)
fa42225a 1289{
428870ff
BB
1290 int err = 0;
1291
fa42225a
BB
1292 if (version != DEV_SCHEME_VERSION0) {
1293 atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
1294 return;
1295 }
1296
428870ff
BB
1297 err |= nvlist_add_uint8(fmri_dev, FM_VERSION, version);
1298 err |= nvlist_add_string(fmri_dev, FM_FMRI_SCHEME, FM_FMRI_SCHEME_DEV);
fa42225a
BB
1299
1300 if (auth != NULL) {
428870ff
BB
1301 err |= nvlist_add_nvlist(fmri_dev, FM_FMRI_AUTHORITY,
1302 (nvlist_t *)auth);
fa42225a
BB
1303 }
1304
428870ff 1305 err |= nvlist_add_string(fmri_dev, FM_FMRI_DEV_PATH, devpath);
fa42225a
BB
1306
1307 if (devid != NULL)
428870ff
BB
1308 err |= nvlist_add_string(fmri_dev, FM_FMRI_DEV_ID, devid);
1309
1310 if (tpl0 != NULL)
1311 err |= nvlist_add_string(fmri_dev, FM_FMRI_DEV_TGTPTLUN0, tpl0);
1312
1313 if (err)
1314 atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
1315
fa42225a
BB
1316}
1317
1318/*
1319 * Set-up and validate the members of an cpu fmri according to:
1320 *
1321 * Member name Type Value
1322 * ====================================================
1323 * version uint8_t 0
1324 * auth nvlist_t <auth>
1325 * cpuid uint32_t <cpu_id>
1326 * cpumask uint8_t <cpu_mask>
1327 * serial uint64_t <serial_id>
1328 *
1329 * Note that auth, cpumask, serial are optional members.
1330 *
1331 */
1332void
1333fm_fmri_cpu_set(nvlist_t *fmri_cpu, int version, const nvlist_t *auth,
1334 uint32_t cpu_id, uint8_t *cpu_maskp, const char *serial_idp)
1335{
1336 uint64_t *failedp = &erpt_kstat_data.fmri_set_failed.value.ui64;
1337
1338 if (version < CPU_SCHEME_VERSION1) {
1339 atomic_add_64(failedp, 1);
1340 return;
1341 }
1342
1343 if (nvlist_add_uint8(fmri_cpu, FM_VERSION, version) != 0) {
1344 atomic_add_64(failedp, 1);
1345 return;
1346 }
1347
1348 if (nvlist_add_string(fmri_cpu, FM_FMRI_SCHEME,
1349 FM_FMRI_SCHEME_CPU) != 0) {
1350 atomic_add_64(failedp, 1);
1351 return;
1352 }
1353
1354 if (auth != NULL && nvlist_add_nvlist(fmri_cpu, FM_FMRI_AUTHORITY,
1355 (nvlist_t *)auth) != 0)
1356 atomic_add_64(failedp, 1);
1357
1358 if (nvlist_add_uint32(fmri_cpu, FM_FMRI_CPU_ID, cpu_id) != 0)
1359 atomic_add_64(failedp, 1);
1360
1361 if (cpu_maskp != NULL && nvlist_add_uint8(fmri_cpu, FM_FMRI_CPU_MASK,
1362 *cpu_maskp) != 0)
1363 atomic_add_64(failedp, 1);
1364
1365 if (serial_idp == NULL || nvlist_add_string(fmri_cpu,
1366 FM_FMRI_CPU_SERIAL_ID, (char *)serial_idp) != 0)
1367 atomic_add_64(failedp, 1);
1368}
1369
1370/*
1371 * Set-up and validate the members of a mem according to:
1372 *
1373 * Member name Type Value
1374 * ====================================================
1375 * version uint8_t 0
1376 * auth nvlist_t <auth> [optional]
1377 * unum string <unum>
1378 * serial string <serial> [optional*]
1379 * offset uint64_t <offset> [optional]
1380 *
1381 * * serial is required if offset is present
1382 */
1383void
1384fm_fmri_mem_set(nvlist_t *fmri, int version, const nvlist_t *auth,
1385 const char *unum, const char *serial, uint64_t offset)
1386{
1387 if (version != MEM_SCHEME_VERSION0) {
1388 atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
1389 return;
1390 }
1391
1392 if (!serial && (offset != (uint64_t)-1)) {
1393 atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
1394 return;
1395 }
1396
1397 if (nvlist_add_uint8(fmri, FM_VERSION, version) != 0) {
1398 atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
1399 return;
1400 }
1401
1402 if (nvlist_add_string(fmri, FM_FMRI_SCHEME, FM_FMRI_SCHEME_MEM) != 0) {
1403 atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
1404 return;
1405 }
1406
1407 if (auth != NULL) {
1408 if (nvlist_add_nvlist(fmri, FM_FMRI_AUTHORITY,
1409 (nvlist_t *)auth) != 0) {
1410 atomic_add_64(
1411 &erpt_kstat_data.fmri_set_failed.value.ui64, 1);
1412 }
1413 }
1414
1415 if (nvlist_add_string(fmri, FM_FMRI_MEM_UNUM, unum) != 0) {
1416 atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
1417 }
1418
1419 if (serial != NULL) {
1420 if (nvlist_add_string_array(fmri, FM_FMRI_MEM_SERIAL_ID,
1421 (char **)&serial, 1) != 0) {
1422 atomic_add_64(
1423 &erpt_kstat_data.fmri_set_failed.value.ui64, 1);
1424 }
1425 if (offset != (uint64_t)-1) {
1426 if (nvlist_add_uint64(fmri, FM_FMRI_MEM_OFFSET,
1427 offset) != 0) {
1428 atomic_add_64(&erpt_kstat_data.
1429 fmri_set_failed.value.ui64, 1);
1430 }
1431 }
1432 }
1433}
1434
1435void
1436fm_fmri_zfs_set(nvlist_t *fmri, int version, uint64_t pool_guid,
1437 uint64_t vdev_guid)
1438{
1439 if (version != ZFS_SCHEME_VERSION0) {
1440 atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
1441 return;
1442 }
1443
1444 if (nvlist_add_uint8(fmri, FM_VERSION, version) != 0) {
1445 atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
1446 return;
1447 }
1448
1449 if (nvlist_add_string(fmri, FM_FMRI_SCHEME, FM_FMRI_SCHEME_ZFS) != 0) {
1450 atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
1451 return;
1452 }
1453
1454 if (nvlist_add_uint64(fmri, FM_FMRI_ZFS_POOL, pool_guid) != 0) {
1455 atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
1456 }
1457
1458 if (vdev_guid != 0) {
1459 if (nvlist_add_uint64(fmri, FM_FMRI_ZFS_VDEV, vdev_guid) != 0) {
1460 atomic_add_64(
1461 &erpt_kstat_data.fmri_set_failed.value.ui64, 1);
1462 }
1463 }
1464}
1465
1466uint64_t
1467fm_ena_increment(uint64_t ena)
1468{
1469 uint64_t new_ena;
1470
1471 switch (ENA_FORMAT(ena)) {
1472 case FM_ENA_FMT1:
1473 new_ena = ena + (1 << ENA_FMT1_GEN_SHFT);
1474 break;
1475 case FM_ENA_FMT2:
1476 new_ena = ena + (1 << ENA_FMT2_GEN_SHFT);
1477 break;
1478 default:
1479 new_ena = 0;
1480 }
1481
1482 return (new_ena);
1483}
1484
1485uint64_t
1486fm_ena_generate_cpu(uint64_t timestamp, processorid_t cpuid, uchar_t format)
1487{
1488 uint64_t ena = 0;
1489
1490 switch (format) {
1491 case FM_ENA_FMT1:
1492 if (timestamp) {
1493 ena = (uint64_t)((format & ENA_FORMAT_MASK) |
1494 ((cpuid << ENA_FMT1_CPUID_SHFT) &
1495 ENA_FMT1_CPUID_MASK) |
1496 ((timestamp << ENA_FMT1_TIME_SHFT) &
1497 ENA_FMT1_TIME_MASK));
1498 } else {
1499 ena = (uint64_t)((format & ENA_FORMAT_MASK) |
1500 ((cpuid << ENA_FMT1_CPUID_SHFT) &
1501 ENA_FMT1_CPUID_MASK) |
26685276 1502 ((gethrtime() << ENA_FMT1_TIME_SHFT) &
fa42225a
BB
1503 ENA_FMT1_TIME_MASK));
1504 }
1505 break;
1506 case FM_ENA_FMT2:
1507 ena = (uint64_t)((format & ENA_FORMAT_MASK) |
1508 ((timestamp << ENA_FMT2_TIME_SHFT) & ENA_FMT2_TIME_MASK));
1509 break;
1510 default:
1511 break;
1512 }
1513
1514 return (ena);
1515}
1516
1517uint64_t
1518fm_ena_generate(uint64_t timestamp, uchar_t format)
1519{
15a9e033
PS
1520 uint64_t ena;
1521
1522 kpreempt_disable();
1523 ena = fm_ena_generate_cpu(timestamp, getcpuid(), format);
1524 kpreempt_enable();
1525
1526 return (ena);
fa42225a
BB
1527}
1528
1529uint64_t
1530fm_ena_generation_get(uint64_t ena)
1531{
1532 uint64_t gen;
1533
1534 switch (ENA_FORMAT(ena)) {
1535 case FM_ENA_FMT1:
1536 gen = (ena & ENA_FMT1_GEN_MASK) >> ENA_FMT1_GEN_SHFT;
1537 break;
1538 case FM_ENA_FMT2:
1539 gen = (ena & ENA_FMT2_GEN_MASK) >> ENA_FMT2_GEN_SHFT;
1540 break;
1541 default:
1542 gen = 0;
1543 break;
1544 }
1545
1546 return (gen);
1547}
1548
1549uchar_t
1550fm_ena_format_get(uint64_t ena)
1551{
1552
1553 return (ENA_FORMAT(ena));
1554}
1555
1556uint64_t
1557fm_ena_id_get(uint64_t ena)
1558{
1559 uint64_t id;
1560
1561 switch (ENA_FORMAT(ena)) {
1562 case FM_ENA_FMT1:
1563 id = (ena & ENA_FMT1_ID_MASK) >> ENA_FMT1_ID_SHFT;
1564 break;
1565 case FM_ENA_FMT2:
1566 id = (ena & ENA_FMT2_ID_MASK) >> ENA_FMT2_ID_SHFT;
1567 break;
1568 default:
1569 id = 0;
1570 }
1571
1572 return (id);
1573}
1574
1575uint64_t
1576fm_ena_time_get(uint64_t ena)
1577{
1578 uint64_t time;
1579
1580 switch (ENA_FORMAT(ena)) {
1581 case FM_ENA_FMT1:
1582 time = (ena & ENA_FMT1_TIME_MASK) >> ENA_FMT1_TIME_SHFT;
1583 break;
1584 case FM_ENA_FMT2:
1585 time = (ena & ENA_FMT2_TIME_MASK) >> ENA_FMT2_TIME_SHFT;
1586 break;
1587 default:
1588 time = 0;
1589 }
1590
1591 return (time);
1592}
1593
26685276 1594#ifdef _KERNEL
fa42225a 1595void
26685276 1596fm_init(void)
fa42225a 1597{
26685276
BB
1598 zevent_len_cur = 0;
1599 zevent_flags = 0;
fa42225a 1600
c409e464
BB
1601 if (zfs_zevent_len_max == 0)
1602 zfs_zevent_len_max = ERPT_MAX_ERRS * MAX(max_ncpus, 4);
fa42225a 1603
26685276
BB
1604 /* Initialize zevent allocation and generation kstats */
1605 fm_ksp = kstat_create("zfs", 0, "fm", "misc", KSTAT_TYPE_NAMED,
1606 sizeof (struct erpt_kstat) / sizeof (kstat_named_t),
1607 KSTAT_FLAG_VIRTUAL);
1608
1609 if (fm_ksp != NULL) {
1610 fm_ksp->ks_data = &erpt_kstat_data;
1611 kstat_install(fm_ksp);
1612 } else {
1613 cmn_err(CE_NOTE, "failed to create fm/misc kstat\n");
1614 }
1615
1616 mutex_init(&zevent_lock, NULL, MUTEX_DEFAULT, NULL);
d1d7e268
MK
1617 list_create(&zevent_list, sizeof (zevent_t),
1618 offsetof(zevent_t, ev_node));
26685276 1619 cv_init(&zevent_cv, NULL, CV_DEFAULT, NULL);
fa42225a 1620}
428870ff
BB
1621
1622void
26685276 1623fm_fini(void)
428870ff 1624{
26685276 1625 int count;
428870ff 1626
26685276 1627 zfs_zevent_drain_all(&count);
428870ff 1628
26685276 1629 mutex_enter(&zevent_lock);
99db9bfd
BB
1630 cv_broadcast(&zevent_cv);
1631
26685276
BB
1632 zevent_flags |= ZEVENT_SHUTDOWN;
1633 while (zevent_waiters > 0) {
1634 mutex_exit(&zevent_lock);
1635 schedule();
1636 mutex_enter(&zevent_lock);
428870ff 1637 }
26685276 1638 mutex_exit(&zevent_lock);
428870ff 1639
26685276
BB
1640 cv_destroy(&zevent_cv);
1641 list_destroy(&zevent_list);
1642 mutex_destroy(&zevent_lock);
428870ff 1643
26685276
BB
1644 if (fm_ksp != NULL) {
1645 kstat_delete(fm_ksp);
1646 fm_ksp = NULL;
428870ff 1647 }
26685276 1648}
428870ff 1649
c409e464
BB
1650module_param(zfs_zevent_len_max, int, 0644);
1651MODULE_PARM_DESC(zfs_zevent_len_max, "Max event queue length");
428870ff 1652
c409e464
BB
1653module_param(zfs_zevent_cols, int, 0644);
1654MODULE_PARM_DESC(zfs_zevent_cols, "Max event column width");
428870ff 1655
c409e464
BB
1656module_param(zfs_zevent_console, int, 0644);
1657MODULE_PARM_DESC(zfs_zevent_console, "Log events to the console");
428870ff 1658
26685276 1659#endif /* _KERNEL */