xref: /illumos-gate/usr/src/cmd/fm/fmd/common/fmd_api.c (revision 705e9f42059a29645037854181bc0bf216398c4c)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
580ab886dSwesolows  * Common Development and Distribution License (the "License").
680ab886dSwesolows  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21d9638e54Smws 
227c478bd9Sstevel@tonic-gate /*
23*705e9f42SStephen Hanson  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <sys/types.h>
287c478bd9Sstevel@tonic-gate #include <sys/fm/protocol.h>
29b7d3956bSstephh #include <fm/topo_hc.h>
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <unistd.h>
327c478bd9Sstevel@tonic-gate #include <signal.h>
337c478bd9Sstevel@tonic-gate #include <limits.h>
347c478bd9Sstevel@tonic-gate #include <syslog.h>
357c478bd9Sstevel@tonic-gate #include <alloca.h>
3624db4641Seschrock #include <stddef.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <fmd_module.h>
397c478bd9Sstevel@tonic-gate #include <fmd_api.h>
407c478bd9Sstevel@tonic-gate #include <fmd_string.h>
417c478bd9Sstevel@tonic-gate #include <fmd_subr.h>
427c478bd9Sstevel@tonic-gate #include <fmd_error.h>
437c478bd9Sstevel@tonic-gate #include <fmd_event.h>
447c478bd9Sstevel@tonic-gate #include <fmd_eventq.h>
457c478bd9Sstevel@tonic-gate #include <fmd_dispq.h>
467c478bd9Sstevel@tonic-gate #include <fmd_timerq.h>
477c478bd9Sstevel@tonic-gate #include <fmd_thread.h>
487c478bd9Sstevel@tonic-gate #include <fmd_ustat.h>
497c478bd9Sstevel@tonic-gate #include <fmd_case.h>
507c478bd9Sstevel@tonic-gate #include <fmd_protocol.h>
517c478bd9Sstevel@tonic-gate #include <fmd_buf.h>
527c478bd9Sstevel@tonic-gate #include <fmd_asru.h>
537c478bd9Sstevel@tonic-gate #include <fmd_fmri.h>
540eb822a1Scindi #include <fmd_topo.h>
557c478bd9Sstevel@tonic-gate #include <fmd_ckpt.h>
56d9638e54Smws #include <fmd_xprt.h>
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #include <fmd.h>
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate  * Table of configuration file variable types ops-vector pointers.  We use this
627c478bd9Sstevel@tonic-gate  * to convert from the property description array specified by the module to an
637c478bd9Sstevel@tonic-gate  * array of fmd_conf_formal_t's.  The order of this array must match the order
647c478bd9Sstevel@tonic-gate  * of #define values specified in <fmd_api.h> (i.e. FMD_TYPE_BOOL must be 0).
657c478bd9Sstevel@tonic-gate  * For now, the fmd_conf_list and fmd_conf_path types are not supported as we
667c478bd9Sstevel@tonic-gate  * do not believe modules need them and they would require more complexity.
677c478bd9Sstevel@tonic-gate  */
687c478bd9Sstevel@tonic-gate static const fmd_conf_ops_t *const _fmd_prop_ops[] = {
697c478bd9Sstevel@tonic-gate 	&fmd_conf_bool,		/* FMD_TYPE_BOOL */
707c478bd9Sstevel@tonic-gate 	&fmd_conf_int32,	/* FMD_TYPE_INT32 */
717c478bd9Sstevel@tonic-gate 	&fmd_conf_uint32,	/* FMD_TYPE_UINT32 */
727c478bd9Sstevel@tonic-gate 	&fmd_conf_int64,	/* FMD_TYPE_INT64 */
737c478bd9Sstevel@tonic-gate 	&fmd_conf_uint64,	/* FMD_TYPE_UINT64 */
747c478bd9Sstevel@tonic-gate 	&fmd_conf_string,	/* FMD_TYPE_STRING */
757c478bd9Sstevel@tonic-gate 	&fmd_conf_time,		/* FMD_TYPE_TIME */
767c478bd9Sstevel@tonic-gate 	&fmd_conf_size,		/* FMD_TYPE_SIZE */
777c478bd9Sstevel@tonic-gate };
787c478bd9Sstevel@tonic-gate 
7980ab886dSwesolows static void fmd_api_verror(fmd_module_t *, int, const char *, va_list)
8080ab886dSwesolows     __NORETURN;
8180ab886dSwesolows static void fmd_api_error(fmd_module_t *, int, const char *, ...) __NORETURN;
8280ab886dSwesolows 
837c478bd9Sstevel@tonic-gate /*
847c478bd9Sstevel@tonic-gate  * fmd_api_vxerror() provides the engine underlying the fmd_hdl_[v]error() API
857c478bd9Sstevel@tonic-gate  * calls and the fmd_api_[v]error() utility routine defined below.  The routine
867c478bd9Sstevel@tonic-gate  * formats the error, optionally associated with a particular errno code 'err',
877c478bd9Sstevel@tonic-gate  * and logs it as an ereport associated with the calling module.  Depending on
887c478bd9Sstevel@tonic-gate  * other optional properties, we also emit a message to stderr and to syslog.
897c478bd9Sstevel@tonic-gate  */
907c478bd9Sstevel@tonic-gate static void
917c478bd9Sstevel@tonic-gate fmd_api_vxerror(fmd_module_t *mp, int err, const char *format, va_list ap)
927c478bd9Sstevel@tonic-gate {
93d9638e54Smws 	int raw_err = err;
947c478bd9Sstevel@tonic-gate 	nvlist_t *nvl;
957c478bd9Sstevel@tonic-gate 	fmd_event_t *e;
967c478bd9Sstevel@tonic-gate 	char *class, *msg;
977c478bd9Sstevel@tonic-gate 	size_t len1, len2;
987c478bd9Sstevel@tonic-gate 	char c;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	/*
1017c478bd9Sstevel@tonic-gate 	 * fmd_api_vxerror() counts as both an error of class EFMD_MODULE
1027c478bd9Sstevel@tonic-gate 	 * as well as an instance of 'err' w.r.t. our internal bean counters.
1037c478bd9Sstevel@tonic-gate 	 */
1047c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&fmd.d_err_lock);
1057c478bd9Sstevel@tonic-gate 	fmd.d_errstats[EFMD_MODULE - EFMD_UNKNOWN].fmds_value.ui64++;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	if (err > EFMD_UNKNOWN && err < EFMD_END)
1087c478bd9Sstevel@tonic-gate 		fmd.d_errstats[err - EFMD_UNKNOWN].fmds_value.ui64++;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&fmd.d_err_lock);
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	/*
1137c478bd9Sstevel@tonic-gate 	 * Format the message using vsnprintf().  As usual, if the format has a
1147c478bd9Sstevel@tonic-gate 	 * newline in it, it is printed alone; otherwise strerror() is added.
1157c478bd9Sstevel@tonic-gate 	 */
1167c478bd9Sstevel@tonic-gate 	if (strchr(format, '\n') != NULL)
1177c478bd9Sstevel@tonic-gate 		err = 0; /* err is not relevant in the message */
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	len1 = vsnprintf(&c, 1, format, ap);
1207c478bd9Sstevel@tonic-gate 	len2 = err != 0 ? snprintf(&c, 1, ": %s\n", fmd_strerror(err)) : 0;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	msg = fmd_alloc(len1 + len2 + 1, FMD_SLEEP);
1237c478bd9Sstevel@tonic-gate 	(void) vsnprintf(msg, len1 + 1, format, ap);
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	if (err != 0) {
1267c478bd9Sstevel@tonic-gate 		(void) snprintf(&msg[len1], len2 + 1,
1277c478bd9Sstevel@tonic-gate 		    ": %s\n", fmd_strerror(err));
1287c478bd9Sstevel@tonic-gate 	}
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	/*
1317c478bd9Sstevel@tonic-gate 	 * Create an error event corresponding to the error, insert it into the
1327c478bd9Sstevel@tonic-gate 	 * error log, and dispatch it to the fmd-self-diagnosis engine.
1337c478bd9Sstevel@tonic-gate 	 */
134d9638e54Smws 	if (mp != fmd.d_self && (raw_err != EFMD_HDL_ABORT || fmd.d_running)) {
1357c478bd9Sstevel@tonic-gate 		if ((c = msg[len1 + len2 - 1]) == '\n')
1367c478bd9Sstevel@tonic-gate 			msg[len1 + len2 - 1] = '\0'; /* strip \n for event */
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 		nvl = fmd_protocol_moderror(mp, err, msg);
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 		if (c == '\n')
1417c478bd9Sstevel@tonic-gate 			msg[len1 + len2 - 1] = c;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 		(void) nvlist_lookup_string(nvl, FM_CLASS, &class);
1447c478bd9Sstevel@tonic-gate 		e = fmd_event_create(FMD_EVT_PROTOCOL, FMD_HRT_NOW, nvl, class);
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 		(void) pthread_rwlock_rdlock(&fmd.d_log_lock);
1477c478bd9Sstevel@tonic-gate 		fmd_log_append(fmd.d_errlog, e, NULL);
1487c478bd9Sstevel@tonic-gate 		(void) pthread_rwlock_unlock(&fmd.d_log_lock);
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 		fmd_event_transition(e, FMD_EVS_ACCEPTED);
1517c478bd9Sstevel@tonic-gate 		fmd_event_commit(e);
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 		fmd_dispq_dispatch(fmd.d_disp, e, class);
1547c478bd9Sstevel@tonic-gate 	}
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	/*
1577c478bd9Sstevel@tonic-gate 	 * Similar to fmd_vdebug(), if the debugging switches are enabled we
1587c478bd9Sstevel@tonic-gate 	 * echo the module name and message to stderr and/or syslog.  Unlike
1597c478bd9Sstevel@tonic-gate 	 * fmd_vdebug(), we also print to stderr if foreground mode is enabled.
160d9638e54Smws 	 * We also print the message if a built-in module is aborting before
161d9638e54Smws 	 * fmd has detached from its parent (e.g. default transport failure).
1627c478bd9Sstevel@tonic-gate 	 */
163d9638e54Smws 	if (fmd.d_fg || (fmd.d_hdl_dbout & FMD_DBOUT_STDERR) || (
164d9638e54Smws 	    raw_err == EFMD_HDL_ABORT && !fmd.d_running)) {
1657c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_lock(&fmd.d_err_lock);
1667c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s: %s",
1677c478bd9Sstevel@tonic-gate 		    fmd.d_pname, mp->mod_name, msg);
1687c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&fmd.d_err_lock);
1697c478bd9Sstevel@tonic-gate 	}
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	if (fmd.d_hdl_dbout & FMD_DBOUT_SYSLOG) {
1727c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR | LOG_DAEMON, "%s ERROR: %s: %s",
1737c478bd9Sstevel@tonic-gate 		    fmd.d_pname, mp->mod_name, msg);
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	fmd_free(msg, len1 + len2 + 1);
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate /*PRINTFLIKE3*/
1807c478bd9Sstevel@tonic-gate static void
1817c478bd9Sstevel@tonic-gate fmd_api_xerror(fmd_module_t *mp, int err, const char *format, ...)
1827c478bd9Sstevel@tonic-gate {
1837c478bd9Sstevel@tonic-gate 	va_list ap;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	va_start(ap, format);
1867c478bd9Sstevel@tonic-gate 	fmd_api_vxerror(mp, err, format, ap);
1877c478bd9Sstevel@tonic-gate 	va_end(ap);
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate /*
1917c478bd9Sstevel@tonic-gate  * fmd_api_verror() is a wrapper around fmd_api_vxerror() for API subroutines.
1927c478bd9Sstevel@tonic-gate  * It calls fmd_module_unlock() on behalf of its caller, logs the error, and
1937c478bd9Sstevel@tonic-gate  * then aborts the API call and the surrounding module entry point by doing an
1947c478bd9Sstevel@tonic-gate  * fmd_module_abort(), which longjmps to the place where we entered the module.
1957c478bd9Sstevel@tonic-gate  */
1967c478bd9Sstevel@tonic-gate static void
1977c478bd9Sstevel@tonic-gate fmd_api_verror(fmd_module_t *mp, int err, const char *format, va_list ap)
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate 	if (fmd_module_locked(mp))
2007c478bd9Sstevel@tonic-gate 		fmd_module_unlock(mp);
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	fmd_api_vxerror(mp, err, format, ap);
2037c478bd9Sstevel@tonic-gate 	fmd_module_abort(mp, err);
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate /*PRINTFLIKE3*/
2077c478bd9Sstevel@tonic-gate static void
2087c478bd9Sstevel@tonic-gate fmd_api_error(fmd_module_t *mp, int err, const char *format, ...)
2097c478bd9Sstevel@tonic-gate {
2107c478bd9Sstevel@tonic-gate 	va_list ap;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	va_start(ap, format);
2137c478bd9Sstevel@tonic-gate 	fmd_api_verror(mp, err, format, ap);
2147c478bd9Sstevel@tonic-gate 	va_end(ap);
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate /*
218d9638e54Smws  * Common code for fmd_api_module_lock() and fmd_api_transport_impl().  This
219d9638e54Smws  * code verifies that the handle is valid and associated with a proper thread.
2207c478bd9Sstevel@tonic-gate  */
2217c478bd9Sstevel@tonic-gate static fmd_module_t *
222d9638e54Smws fmd_api_module(fmd_hdl_t *hdl)
2237c478bd9Sstevel@tonic-gate {
2247c478bd9Sstevel@tonic-gate 	fmd_thread_t *tp;
2257c478bd9Sstevel@tonic-gate 	fmd_module_t *mp;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	/*
2287c478bd9Sstevel@tonic-gate 	 * If our TSD is not present at all, this is either a serious bug or
2297c478bd9Sstevel@tonic-gate 	 * someone has created a thread behind our back and is using fmd's API.
2307c478bd9Sstevel@tonic-gate 	 * We can't call fmd_api_error() because we can't be sure that we can
2317c478bd9Sstevel@tonic-gate 	 * unwind our state back to an enclosing fmd_module_dispatch(), so we
2327c478bd9Sstevel@tonic-gate 	 * must panic instead.  This is likely a module design or coding error.
2337c478bd9Sstevel@tonic-gate 	 */
2347c478bd9Sstevel@tonic-gate 	if ((tp = pthread_getspecific(fmd.d_key)) == NULL) {
2357c478bd9Sstevel@tonic-gate 		fmd_panic("fmd module api call made using "
2367c478bd9Sstevel@tonic-gate 		    "client handle %p from unknown thread\n", (void *)hdl);
2377c478bd9Sstevel@tonic-gate 	}
2387c478bd9Sstevel@tonic-gate 
239d9638e54Smws 	/*
240d9638e54Smws 	 * If our TSD refers to the root module and is a door server thread,
241d9638e54Smws 	 * then it was created asynchronously at the request of a module but
242d9638e54Smws 	 * is using now the module API as an auxiliary module thread.  We reset
243d9638e54Smws 	 * tp->thr_mod to the module handle so it can act as a module thread.
244d9638e54Smws 	 */
245d9638e54Smws 	if (tp->thr_mod == fmd.d_rmod && tp->thr_func == &fmd_door_server)
246d9638e54Smws 		tp->thr_mod = (fmd_module_t *)hdl;
247d9638e54Smws 
2487c478bd9Sstevel@tonic-gate 	if ((mp = tp->thr_mod) != (fmd_module_t *)hdl) {
2497c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_HDL_INVAL,
2507c478bd9Sstevel@tonic-gate 		    "client handle %p is not valid\n", (void *)hdl);
2517c478bd9Sstevel@tonic-gate 	}
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	if (mp->mod_flags & FMD_MOD_FAIL) {
2547c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_MOD_FAIL,
2557c478bd9Sstevel@tonic-gate 		    "module has experienced an unrecoverable error\n");
2567c478bd9Sstevel@tonic-gate 	}
2577c478bd9Sstevel@tonic-gate 
258d9638e54Smws 	return (mp);
259d9638e54Smws }
260d9638e54Smws 
261d9638e54Smws /*
262d9638e54Smws  * fmd_api_module_lock() is used as a wrapper around fmd_module_lock() and a
263d9638e54Smws  * common prologue to each fmd_api.c routine.  It verifies that the handle is
264d9638e54Smws  * valid and owned by the current server thread, locks the handle, and then
265d9638e54Smws  * verifies that the caller is performing an operation on a registered handle.
266d9638e54Smws  * If any tests fail, the entire API call is aborted by fmd_api_error().
267d9638e54Smws  */
268d9638e54Smws static fmd_module_t *
269d9638e54Smws fmd_api_module_lock(fmd_hdl_t *hdl)
270d9638e54Smws {
271d9638e54Smws 	fmd_module_t *mp = fmd_api_module(hdl);
272d9638e54Smws 
2737c478bd9Sstevel@tonic-gate 	fmd_module_lock(mp);
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	if (mp->mod_info == NULL) {
2767c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_HDL_NOTREG,
2777c478bd9Sstevel@tonic-gate 		    "client handle %p has not been registered\n", (void *)hdl);
2787c478bd9Sstevel@tonic-gate 	}
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	return (mp);
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate /*
2847c478bd9Sstevel@tonic-gate  * Utility function for API entry points that accept fmd_case_t's.  We cast cp
2857c478bd9Sstevel@tonic-gate  * to fmd_case_impl_t and check to make sure the case is owned by the caller.
2867c478bd9Sstevel@tonic-gate  */
2877c478bd9Sstevel@tonic-gate static fmd_case_impl_t *
2887c478bd9Sstevel@tonic-gate fmd_api_case_impl(fmd_module_t *mp, fmd_case_t *cp)
2897c478bd9Sstevel@tonic-gate {
2907c478bd9Sstevel@tonic-gate 	fmd_case_impl_t *cip = (fmd_case_impl_t *)cp;
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	if (cip == NULL || cip->ci_mod != mp) {
2937c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_CASE_OWNER,
294d9638e54Smws 		    "case %p is invalid or not owned by caller\n", (void *)cip);
2957c478bd9Sstevel@tonic-gate 	}
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	return (cip);
2987c478bd9Sstevel@tonic-gate }
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate /*
301d9638e54Smws  * Utility function for API entry points that accept fmd_xprt_t's.  We cast xp
302d9638e54Smws  * to fmd_transport_t and check to make sure the case is owned by the caller.
303d9638e54Smws  * Note that we could make this check safer by actually walking mp's transport
304d9638e54Smws  * list, but that requires holding the module lock and this routine needs to be
305d9638e54Smws  * MT-hot w.r.t. auxiliary module threads.  Ultimately any loadable module can
306d9638e54Smws  * cause us to crash anyway, so we optimize for scalability over safety here.
307d9638e54Smws  */
308d9638e54Smws static fmd_xprt_impl_t *
309d9638e54Smws fmd_api_transport_impl(fmd_hdl_t *hdl, fmd_xprt_t *xp)
310d9638e54Smws {
311d9638e54Smws 	fmd_module_t *mp = fmd_api_module(hdl);
312d9638e54Smws 	fmd_xprt_impl_t *xip = (fmd_xprt_impl_t *)xp;
313d9638e54Smws 
314d9638e54Smws 	if (xip == NULL || xip->xi_queue->eq_mod != mp) {
315d9638e54Smws 		fmd_api_error(mp, EFMD_XPRT_OWNER,
316d9638e54Smws 		    "xprt %p is invalid or not owned by caller\n", (void *)xp);
317d9638e54Smws 	}
318d9638e54Smws 
319d9638e54Smws 	return (xip);
320d9638e54Smws }
321d9638e54Smws 
322d9638e54Smws /*
3237c478bd9Sstevel@tonic-gate  * fmd_hdl_register() is the one function which cannot use fmd_api_error() to
3247c478bd9Sstevel@tonic-gate  * report errors, because that routine causes the module to abort.  Failure to
3257c478bd9Sstevel@tonic-gate  * register is instead handled by having fmd_hdl_register() return an error to
3267c478bd9Sstevel@tonic-gate  * the _fmd_init() function and then detecting no registration when it returns.
3277c478bd9Sstevel@tonic-gate  * So we use this routine for fmd_hdl_register() error paths instead.
3287c478bd9Sstevel@tonic-gate  */
3297c478bd9Sstevel@tonic-gate static int
3307c478bd9Sstevel@tonic-gate fmd_hdl_register_error(fmd_module_t *mp, int err)
3317c478bd9Sstevel@tonic-gate {
3327c478bd9Sstevel@tonic-gate 	if (fmd_module_locked(mp))
3337c478bd9Sstevel@tonic-gate 		fmd_module_unlock(mp);
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	fmd_api_xerror(mp, err, "failed to register");
3367c478bd9Sstevel@tonic-gate 	return (fmd_set_errno(err));
3377c478bd9Sstevel@tonic-gate }
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate static void
3407c478bd9Sstevel@tonic-gate fmd_hdl_nop(void)
3417c478bd9Sstevel@tonic-gate {
3427c478bd9Sstevel@tonic-gate 	/* empty function for use with unspecified module entry points */
3437c478bd9Sstevel@tonic-gate }
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate int
3467c478bd9Sstevel@tonic-gate fmd_hdl_register(fmd_hdl_t *hdl, int version, const fmd_hdl_info_t *mip)
3477c478bd9Sstevel@tonic-gate {
3487c478bd9Sstevel@tonic-gate 	fmd_thread_t *tp = pthread_getspecific(fmd.d_key);
3497c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = tp->thr_mod;
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	const fmd_prop_t *prop;
3527c478bd9Sstevel@tonic-gate 	const fmd_conf_path_t *pap;
3537c478bd9Sstevel@tonic-gate 	fmd_conf_formal_t *cfp;
354d9638e54Smws 	fmd_hdl_ops_t ops;
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	const char *conf = NULL;
3577c478bd9Sstevel@tonic-gate 	char buf[PATH_MAX];
3587c478bd9Sstevel@tonic-gate 	int i;
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	if (mp != (fmd_module_t *)hdl)
3617c478bd9Sstevel@tonic-gate 		return (fmd_hdl_register_error(mp, EFMD_HDL_INVAL));
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	fmd_module_lock(mp);
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	/*
3667c478bd9Sstevel@tonic-gate 	 * First perform some sanity checks on our input.  The API version must
3677c478bd9Sstevel@tonic-gate 	 * be supported by FMD and the handle can only be registered once by
3687c478bd9Sstevel@tonic-gate 	 * the module thread to which we assigned this client handle.  The info
3697c478bd9Sstevel@tonic-gate 	 * provided for the handle must be valid and have the minimal settings.
3707c478bd9Sstevel@tonic-gate 	 */
37124db4641Seschrock 	if (version > FMD_API_VERSION_4)
3727c478bd9Sstevel@tonic-gate 		return (fmd_hdl_register_error(mp, EFMD_VER_NEW));
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	if (version < FMD_API_VERSION_1)
3757c478bd9Sstevel@tonic-gate 		return (fmd_hdl_register_error(mp, EFMD_VER_OLD));
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	if (mp->mod_conf != NULL)
3787c478bd9Sstevel@tonic-gate 		return (fmd_hdl_register_error(mp, EFMD_HDL_REG));
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	if (pthread_self() != mp->mod_thread->thr_tid)
3817c478bd9Sstevel@tonic-gate 		return (fmd_hdl_register_error(mp, EFMD_HDL_TID));
3827c478bd9Sstevel@tonic-gate 
383d9638e54Smws 	if (mip == NULL || mip->fmdi_desc == NULL ||
384d9638e54Smws 	    mip->fmdi_vers == NULL || mip->fmdi_ops == NULL)
3857c478bd9Sstevel@tonic-gate 		return (fmd_hdl_register_error(mp, EFMD_HDL_INFO));
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	/*
388d9638e54Smws 	 * Copy the module's ops vector into a local variable to account for
389d9638e54Smws 	 * changes in the module ABI.  Then if any of the optional entry points
390d9638e54Smws 	 * are NULL, set them to nop so we don't have to check before calling.
391d9638e54Smws 	 */
392d9638e54Smws 	bzero(&ops, sizeof (ops));
393d9638e54Smws 
394d9638e54Smws 	if (version < FMD_API_VERSION_3)
39524db4641Seschrock 		bcopy(mip->fmdi_ops, &ops, offsetof(fmd_hdl_ops_t, fmdo_send));
39624db4641Seschrock 	else if (version < FMD_API_VERSION_4)
39724db4641Seschrock 		bcopy(mip->fmdi_ops, &ops,
39824db4641Seschrock 		    offsetof(fmd_hdl_ops_t, fmdo_topo));
399d9638e54Smws 	else
400d9638e54Smws 		bcopy(mip->fmdi_ops, &ops, sizeof (ops));
401d9638e54Smws 
402d9638e54Smws 	if (ops.fmdo_recv == NULL)
403d9638e54Smws 		ops.fmdo_recv = (void (*)())fmd_hdl_nop;
404d9638e54Smws 	if (ops.fmdo_timeout == NULL)
405d9638e54Smws 		ops.fmdo_timeout = (void (*)())fmd_hdl_nop;
406d9638e54Smws 	if (ops.fmdo_close == NULL)
407d9638e54Smws 		ops.fmdo_close = (void (*)())fmd_hdl_nop;
408d9638e54Smws 	if (ops.fmdo_stats == NULL)
409d9638e54Smws 		ops.fmdo_stats = (void (*)())fmd_hdl_nop;
410d9638e54Smws 	if (ops.fmdo_gc == NULL)
411d9638e54Smws 		ops.fmdo_gc = (void (*)())fmd_hdl_nop;
412d9638e54Smws 	if (ops.fmdo_send == NULL)
413d9638e54Smws 		ops.fmdo_send = (int (*)())fmd_hdl_nop;
41424db4641Seschrock 	if (ops.fmdo_topo == NULL)
41524db4641Seschrock 		ops.fmdo_topo = (void (*)())fmd_hdl_nop;
416d9638e54Smws 
417d9638e54Smws 	/*
4187c478bd9Sstevel@tonic-gate 	 * Make two passes through the property array to initialize the formals
4197c478bd9Sstevel@tonic-gate 	 * to use for processing the module's .conf file.  In the first pass,
4207c478bd9Sstevel@tonic-gate 	 * we validate the types and count the number of properties.  In the
4217c478bd9Sstevel@tonic-gate 	 * second pass we copy the strings and fill in the appropriate ops.
4227c478bd9Sstevel@tonic-gate 	 */
4237c478bd9Sstevel@tonic-gate 	for (prop = mip->fmdi_props, i = 0; prop != NULL &&
4247c478bd9Sstevel@tonic-gate 	    prop->fmdp_name != NULL; prop++, i++) {
4257c478bd9Sstevel@tonic-gate 		if (prop->fmdp_type >=
4267c478bd9Sstevel@tonic-gate 		    sizeof (_fmd_prop_ops) / sizeof (_fmd_prop_ops[0])) {
4277c478bd9Sstevel@tonic-gate 			fmd_api_xerror(mp, EFMD_HDL_PROP,
4287c478bd9Sstevel@tonic-gate 			    "property %s uses invalid type %u\n",
4297c478bd9Sstevel@tonic-gate 			    prop->fmdp_name, prop->fmdp_type);
4307c478bd9Sstevel@tonic-gate 			return (fmd_hdl_register_error(mp, EFMD_HDL_PROP));
4317c478bd9Sstevel@tonic-gate 		}
4327c478bd9Sstevel@tonic-gate 	}
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	mp->mod_argc = i;
4357c478bd9Sstevel@tonic-gate 	mp->mod_argv = fmd_zalloc(sizeof (fmd_conf_formal_t) * i, FMD_SLEEP);
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	prop = mip->fmdi_props;
4387c478bd9Sstevel@tonic-gate 	cfp = mp->mod_argv;
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	for (i = 0; i < mp->mod_argc; i++, prop++, cfp++) {
4417c478bd9Sstevel@tonic-gate 		cfp->cf_name = fmd_strdup(prop->fmdp_name, FMD_SLEEP);
4427c478bd9Sstevel@tonic-gate 		cfp->cf_ops = _fmd_prop_ops[prop->fmdp_type];
4437c478bd9Sstevel@tonic-gate 		cfp->cf_default = fmd_strdup(prop->fmdp_defv, FMD_SLEEP);
4447c478bd9Sstevel@tonic-gate 	}
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	/*
4477c478bd9Sstevel@tonic-gate 	 * If this module came from an on-disk file, compute the name of the
4487c478bd9Sstevel@tonic-gate 	 * corresponding .conf file and parse properties from it if it exists.
4497c478bd9Sstevel@tonic-gate 	 */
4507c478bd9Sstevel@tonic-gate 	if (mp->mod_path != NULL) {
4517c478bd9Sstevel@tonic-gate 		(void) strlcpy(buf, mp->mod_path, sizeof (buf));
4527c478bd9Sstevel@tonic-gate 		(void) fmd_strdirname(buf);
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 		(void) strlcat(buf, "/", sizeof (buf));
4557c478bd9Sstevel@tonic-gate 		(void) strlcat(buf, mp->mod_name, sizeof (buf));
4567c478bd9Sstevel@tonic-gate 		(void) strlcat(buf, ".conf", sizeof (buf));
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 		if (access(buf, F_OK) == 0)
4597c478bd9Sstevel@tonic-gate 			conf = buf;
4607c478bd9Sstevel@tonic-gate 	}
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	if ((mp->mod_conf = fmd_conf_open(conf,
463d9638e54Smws 	    mp->mod_argc, mp->mod_argv, 0)) == NULL)
4647c478bd9Sstevel@tonic-gate 		return (fmd_hdl_register_error(mp, EFMD_MOD_CONF));
4657c478bd9Sstevel@tonic-gate 
466d9638e54Smws 	fmd_conf_propagate(fmd.d_conf, mp->mod_conf, mp->mod_name);
467d9638e54Smws 
4687c478bd9Sstevel@tonic-gate 	/*
4697c478bd9Sstevel@tonic-gate 	 * Look up the list of the libdiagcode dictionaries associated with the
4707c478bd9Sstevel@tonic-gate 	 * module.  If none were specified, use the value from daemon's config.
4717c478bd9Sstevel@tonic-gate 	 * We only fail if the module specified an explicit dictionary.
4727c478bd9Sstevel@tonic-gate 	 */
4737c478bd9Sstevel@tonic-gate 	(void) fmd_conf_getprop(mp->mod_conf, FMD_PROP_DICTIONARIES, &pap);
4747c478bd9Sstevel@tonic-gate 	if (pap->cpa_argc == 0 && mp->mod_ops == &fmd_bltin_ops)
4757c478bd9Sstevel@tonic-gate 		(void) fmd_conf_getprop(fmd.d_conf, "self.dict", &pap);
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	for (i = 0; i < pap->cpa_argc; i++) {
4787c478bd9Sstevel@tonic-gate 		if (fmd_module_dc_opendict(mp, pap->cpa_argv[i]) != 0) {
4797c478bd9Sstevel@tonic-gate 			fmd_api_xerror(mp, errno,
4807c478bd9Sstevel@tonic-gate 			    "failed to open dictionary %s", pap->cpa_argv[i]);
4817c478bd9Sstevel@tonic-gate 			return (fmd_hdl_register_error(mp, EFMD_MOD_CONF));
4827c478bd9Sstevel@tonic-gate 		}
4837c478bd9Sstevel@tonic-gate 	}
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	/*
4867c478bd9Sstevel@tonic-gate 	 * Make a copy of the handle information and store it in mod_info.  We
4877c478bd9Sstevel@tonic-gate 	 * do not need to bother copying fmdi_props since they're already read.
4887c478bd9Sstevel@tonic-gate 	 */
4897c478bd9Sstevel@tonic-gate 	mp->mod_info = fmd_alloc(sizeof (fmd_hdl_info_t), FMD_SLEEP);
4907c478bd9Sstevel@tonic-gate 	mp->mod_info->fmdi_desc = fmd_strdup(mip->fmdi_desc, FMD_SLEEP);
4917c478bd9Sstevel@tonic-gate 	mp->mod_info->fmdi_vers = fmd_strdup(mip->fmdi_vers, FMD_SLEEP);
4927c478bd9Sstevel@tonic-gate 	mp->mod_info->fmdi_ops = fmd_alloc(sizeof (fmd_hdl_ops_t), FMD_SLEEP);
493d9638e54Smws 	bcopy(&ops, (void *)mp->mod_info->fmdi_ops, sizeof (fmd_hdl_ops_t));
4947c478bd9Sstevel@tonic-gate 	mp->mod_info->fmdi_props = NULL;
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	/*
49719e1255fScy152378 	 * Store a copy of module version in mp for fmd_scheme_fmd_present()
49819e1255fScy152378 	 */
49919e1255fScy152378 	if (mp->mod_vers == NULL)
50019e1255fScy152378 		mp->mod_vers = fmd_strdup(mip->fmdi_vers, FMD_SLEEP);
50119e1255fScy152378 
50219e1255fScy152378 	/*
5037c478bd9Sstevel@tonic-gate 	 * Allocate an FMRI representing this module.  We'll use this later
5047c478bd9Sstevel@tonic-gate 	 * if the module decides to publish any events (e.g. list.suspects).
5057c478bd9Sstevel@tonic-gate 	 */
5067c478bd9Sstevel@tonic-gate 	mp->mod_fmri = fmd_protocol_fmri_module(mp);
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	/*
5097c478bd9Sstevel@tonic-gate 	 * Any subscriptions specified in the conf file are now stored in the
5107c478bd9Sstevel@tonic-gate 	 * corresponding property.  Add all of these to the dispatch queue.
5117c478bd9Sstevel@tonic-gate 	 */
5127c478bd9Sstevel@tonic-gate 	(void) fmd_conf_getprop(mp->mod_conf, FMD_PROP_SUBSCRIPTIONS, &pap);
5137c478bd9Sstevel@tonic-gate 
514d9638e54Smws 	for (i = 0; i < pap->cpa_argc; i++) {
515d9638e54Smws 		fmd_dispq_insert(fmd.d_disp, mp->mod_queue, pap->cpa_argv[i]);
516d9638e54Smws 		fmd_xprt_subscribe_all(pap->cpa_argv[i]);
517d9638e54Smws 	}
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	/*
5207c478bd9Sstevel@tonic-gate 	 * Unlock the module and restore any pre-existing module checkpoint.
5217c478bd9Sstevel@tonic-gate 	 * If the checkpoint is missing or corrupt, we just keep going.
5227c478bd9Sstevel@tonic-gate 	 */
5237c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
5247c478bd9Sstevel@tonic-gate 	fmd_ckpt_restore(mp);
5257c478bd9Sstevel@tonic-gate 	return (0);
5267c478bd9Sstevel@tonic-gate }
5277c478bd9Sstevel@tonic-gate 
528d9638e54Smws /*
529d9638e54Smws  * If an auxiliary thread exists for the specified module at unregistration
530d9638e54Smws  * time, send it an asynchronous cancellation to force it to exit and then
531d9638e54Smws  * join with it (we expect this to either succeed quickly or return ESRCH).
532d9638e54Smws  * Once this is complete we can destroy the associated fmd_thread_t data.
533d9638e54Smws  */
534d9638e54Smws static void
535d9638e54Smws fmd_module_thrcancel(fmd_idspace_t *ids, id_t id, fmd_module_t *mp)
536d9638e54Smws {
537d9638e54Smws 	fmd_thread_t *tp = fmd_idspace_getspecific(ids, id);
538d9638e54Smws 
539d9638e54Smws 	fmd_dprintf(FMD_DBG_MOD, "cancelling %s auxiliary thread %u\n",
540d9638e54Smws 	    mp->mod_name, tp->thr_tid);
541d9638e54Smws 
542d9638e54Smws 	ASSERT(tp->thr_tid == id);
543d9638e54Smws 	(void) pthread_cancel(tp->thr_tid);
544d9638e54Smws 	(void) pthread_join(tp->thr_tid, NULL);
545d9638e54Smws 
546d9638e54Smws 	fmd_thread_destroy(tp, FMD_THREAD_NOJOIN);
547d9638e54Smws }
548d9638e54Smws 
5497c478bd9Sstevel@tonic-gate void
5507c478bd9Sstevel@tonic-gate fmd_module_unregister(fmd_module_t *mp)
5517c478bd9Sstevel@tonic-gate {
5527c478bd9Sstevel@tonic-gate 	fmd_conf_formal_t *cfp = mp->mod_argv;
5537c478bd9Sstevel@tonic-gate 	const fmd_conf_path_t *pap;
554d9638e54Smws 	fmd_case_t *cp;
555d9638e54Smws 	fmd_xprt_t *xp;
5567c478bd9Sstevel@tonic-gate 	int i;
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 	TRACE((FMD_DBG_MOD, "unregister %p (%s)", (void *)mp, mp->mod_name));
5597c478bd9Sstevel@tonic-gate 	ASSERT(fmd_module_locked(mp));
5607c478bd9Sstevel@tonic-gate 
561d9638e54Smws 	/*
562d9638e54Smws 	 * If any transports are still open, they have send threads that are
563d9638e54Smws 	 * using the module handle: shut them down and join with these threads.
564d9638e54Smws 	 */
565d9638e54Smws 	while ((xp = fmd_list_next(&mp->mod_transports)) != NULL)
566d9638e54Smws 		fmd_xprt_destroy(xp);
567d9638e54Smws 
568d9638e54Smws 	/*
569d9638e54Smws 	 * If any auxiliary threads exist, they may be using our module handle,
570d9638e54Smws 	 * and therefore could cause a fault as soon as we start destroying it.
571d9638e54Smws 	 * Module writers should clean up any threads before unregistering: we
572d9638e54Smws 	 * forcibly cancel any remaining auxiliary threads before proceeding.
573d9638e54Smws 	 */
574d9638e54Smws 	fmd_idspace_apply(mp->mod_threads,
575d9638e54Smws 	    (void (*)())fmd_module_thrcancel, mp);
576d9638e54Smws 
57733129b33Sayznaga 	if (mp->mod_error == 0)
57833129b33Sayznaga 		fmd_ckpt_save(mp); /* take one more checkpoint if needed */
57933129b33Sayznaga 
580d9638e54Smws 	/*
581d9638e54Smws 	 * Delete any cases associated with the module (UNSOLVED, SOLVED, or
582d9638e54Smws 	 * CLOSE_WAIT) as if fmdo_close() has finished processing them.
583d9638e54Smws 	 */
584d9638e54Smws 	while ((cp = fmd_list_next(&mp->mod_cases)) != NULL)
585d9638e54Smws 		fmd_case_delete(cp);
586d9638e54Smws 
587d9638e54Smws 	fmd_ustat_delete_references(mp->mod_ustat);
5887c478bd9Sstevel@tonic-gate 	(void) fmd_conf_getprop(mp->mod_conf, FMD_PROP_SUBSCRIPTIONS, &pap);
5897c478bd9Sstevel@tonic-gate 
590d9638e54Smws 	for (i = 0; i < pap->cpa_argc; i++) {
591d9638e54Smws 		fmd_xprt_unsubscribe_all(pap->cpa_argv[i]);
592d9638e54Smws 		fmd_dispq_delete(fmd.d_disp, mp->mod_queue, pap->cpa_argv[i]);
5937c478bd9Sstevel@tonic-gate 	}
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	fmd_conf_close(mp->mod_conf);
5967c478bd9Sstevel@tonic-gate 	mp->mod_conf = NULL;
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 	for (i = 0; i < mp->mod_argc; i++, cfp++) {
5997c478bd9Sstevel@tonic-gate 		fmd_strfree((char *)cfp->cf_name);
6007c478bd9Sstevel@tonic-gate 		fmd_strfree((char *)cfp->cf_default);
6017c478bd9Sstevel@tonic-gate 	}
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	fmd_free(mp->mod_argv, sizeof (fmd_conf_formal_t) * mp->mod_argc);
6047c478bd9Sstevel@tonic-gate 	mp->mod_argv = NULL;
6057c478bd9Sstevel@tonic-gate 	mp->mod_argc = 0;
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	nvlist_free(mp->mod_fmri);
6087c478bd9Sstevel@tonic-gate 	mp->mod_fmri = NULL;
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	fmd_strfree((char *)mp->mod_info->fmdi_desc);
6117c478bd9Sstevel@tonic-gate 	fmd_strfree((char *)mp->mod_info->fmdi_vers);
6127c478bd9Sstevel@tonic-gate 	fmd_free((void *)mp->mod_info->fmdi_ops, sizeof (fmd_hdl_ops_t));
6137c478bd9Sstevel@tonic-gate 	fmd_free(mp->mod_info, sizeof (fmd_hdl_info_t));
6147c478bd9Sstevel@tonic-gate 	mp->mod_info = NULL;
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 	fmd_eventq_abort(mp->mod_queue);
6177c478bd9Sstevel@tonic-gate }
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate void
6207c478bd9Sstevel@tonic-gate fmd_hdl_unregister(fmd_hdl_t *hdl)
6217c478bd9Sstevel@tonic-gate {
6227c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
6237c478bd9Sstevel@tonic-gate 	fmd_module_unregister(mp);
6247c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
6257c478bd9Sstevel@tonic-gate }
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate void
6287c478bd9Sstevel@tonic-gate fmd_hdl_subscribe(fmd_hdl_t *hdl, const char *class)
6297c478bd9Sstevel@tonic-gate {
6307c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
6317c478bd9Sstevel@tonic-gate 
632d9638e54Smws 	if (fmd_conf_setprop(mp->mod_conf,
633d9638e54Smws 	    FMD_PROP_SUBSCRIPTIONS, class) == 0) {
634d9638e54Smws 		fmd_dispq_insert(fmd.d_disp, mp->mod_queue, class);
635d9638e54Smws 		fmd_xprt_subscribe_all(class);
636d9638e54Smws 	}
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
6397c478bd9Sstevel@tonic-gate }
6407c478bd9Sstevel@tonic-gate 
641d9638e54Smws 
6427c478bd9Sstevel@tonic-gate void
6437c478bd9Sstevel@tonic-gate fmd_hdl_unsubscribe(fmd_hdl_t *hdl, const char *class)
6447c478bd9Sstevel@tonic-gate {
6457c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
6467c478bd9Sstevel@tonic-gate 
647d9638e54Smws 	if (fmd_conf_delprop(mp->mod_conf,
648d9638e54Smws 	    FMD_PROP_SUBSCRIPTIONS, class) == 0) {
649d9638e54Smws 		fmd_xprt_unsubscribe_all(class);
650d9638e54Smws 		fmd_dispq_delete(fmd.d_disp, mp->mod_queue, class);
651d9638e54Smws 	}
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
6547c478bd9Sstevel@tonic-gate 	fmd_eventq_cancel(mp->mod_queue, FMD_EVT_PROTOCOL, (void *)class);
6557c478bd9Sstevel@tonic-gate }
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate void
6587c478bd9Sstevel@tonic-gate fmd_hdl_setspecific(fmd_hdl_t *hdl, void *spec)
6597c478bd9Sstevel@tonic-gate {
6607c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 	mp->mod_spec = spec;
6637c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
6647c478bd9Sstevel@tonic-gate }
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate void *
6677c478bd9Sstevel@tonic-gate fmd_hdl_getspecific(fmd_hdl_t *hdl)
6687c478bd9Sstevel@tonic-gate {
6697c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
6707c478bd9Sstevel@tonic-gate 	void *spec = mp->mod_spec;
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
6737c478bd9Sstevel@tonic-gate 	return (spec);
6747c478bd9Sstevel@tonic-gate }
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate void
6777c478bd9Sstevel@tonic-gate fmd_hdl_opendict(fmd_hdl_t *hdl, const char *dict)
6787c478bd9Sstevel@tonic-gate {
6797c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
6807c478bd9Sstevel@tonic-gate 	const fmd_conf_path_t *pap;
6817c478bd9Sstevel@tonic-gate 	int i;
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	/*
6847c478bd9Sstevel@tonic-gate 	 * Update the dictionary property in order to preserve the list of
6857c478bd9Sstevel@tonic-gate 	 * pathnames and expand any % tokens in the path.  Then retrieve the
6867c478bd9Sstevel@tonic-gate 	 * new dictionary names from cpa_argv[] and open them one at a time.
6877c478bd9Sstevel@tonic-gate 	 */
6887c478bd9Sstevel@tonic-gate 	(void) fmd_conf_setprop(mp->mod_conf, FMD_PROP_DICTIONARIES, dict);
6897c478bd9Sstevel@tonic-gate 	(void) fmd_conf_getprop(mp->mod_conf, FMD_PROP_DICTIONARIES, &pap);
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 	ASSERT(pap->cpa_argc > mp->mod_dictc);
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	for (i = mp->mod_dictc; i < pap->cpa_argc; i++) {
6947c478bd9Sstevel@tonic-gate 		if (fmd_module_dc_opendict(mp, pap->cpa_argv[i]) != 0) {
6957c478bd9Sstevel@tonic-gate 			fmd_api_error(mp, EFMD_MOD_DICT,
6967c478bd9Sstevel@tonic-gate 			    "failed to open dictionary %s for module %s",
6977c478bd9Sstevel@tonic-gate 			    pap->cpa_argv[i], mp->mod_name);
6987c478bd9Sstevel@tonic-gate 		}
6997c478bd9Sstevel@tonic-gate 	}
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
7027c478bd9Sstevel@tonic-gate }
7037c478bd9Sstevel@tonic-gate 
7047aec1d6eScindi topo_hdl_t *
70524db4641Seschrock fmd_hdl_topo_hold(fmd_hdl_t *hdl, int v)
7067aec1d6eScindi {
7077aec1d6eScindi 	fmd_module_t *mp = fmd_api_module_lock(hdl);
7087aec1d6eScindi 	topo_hdl_t *thp;
7097aec1d6eScindi 
7107aec1d6eScindi 	if (v != TOPO_VERSION) {
7117aec1d6eScindi 		fmd_api_error(mp, EFMD_MOD_TOPO, "libtopo version mismatch: "
7127aec1d6eScindi 		    "fmd version %d != client version %d\n", TOPO_VERSION, v);
7137aec1d6eScindi 	}
7147aec1d6eScindi 
71524db4641Seschrock 	thp = fmd_module_topo_hold(mp);
71624db4641Seschrock 	ASSERT(thp != NULL);
7170eb822a1Scindi 
7187aec1d6eScindi 	fmd_module_unlock(mp);
7197aec1d6eScindi 	return (thp);
7207aec1d6eScindi }
7217aec1d6eScindi 
72224db4641Seschrock void
72324db4641Seschrock fmd_hdl_topo_rele(fmd_hdl_t *hdl, topo_hdl_t *thp)
72424db4641Seschrock {
72524db4641Seschrock 	fmd_module_t *mp = fmd_api_module_lock(hdl);
72624db4641Seschrock 
72724db4641Seschrock 	if (fmd_module_topo_rele(mp, thp) != 0)
72824db4641Seschrock 		fmd_api_error(mp, EFMD_MOD_TOPO, "failed to release invalid "
72924db4641Seschrock 		    "topo handle: %p\n", (void *)thp);
73024db4641Seschrock 
73124db4641Seschrock 	fmd_module_unlock(mp);
73224db4641Seschrock }
73324db4641Seschrock 
7349af3851aSeschrock static void *
7359af3851aSeschrock fmd_hdl_alloc_locked(fmd_module_t *mp, size_t size, int flags)
7367c478bd9Sstevel@tonic-gate {
7377c478bd9Sstevel@tonic-gate 	void *data;
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 	if (mp->mod_stats->ms_memlimit.fmds_value.ui64 -
7407c478bd9Sstevel@tonic-gate 	    mp->mod_stats->ms_memtotal.fmds_value.ui64 < size) {
7417c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_HDL_NOMEM, "%s's allocation of %lu "
7427c478bd9Sstevel@tonic-gate 		    "bytes exceeds module memory limit (%llu)\n",
7437c478bd9Sstevel@tonic-gate 		    mp->mod_name, (ulong_t)size, (u_longlong_t)
7447c478bd9Sstevel@tonic-gate 		    mp->mod_stats->ms_memtotal.fmds_value.ui64);
7457c478bd9Sstevel@tonic-gate 	}
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	if ((data = fmd_alloc(size, flags)) != NULL)
7487c478bd9Sstevel@tonic-gate 		mp->mod_stats->ms_memtotal.fmds_value.ui64 += size;
7497c478bd9Sstevel@tonic-gate 
7509af3851aSeschrock 	return (data);
7519af3851aSeschrock }
7529af3851aSeschrock 
7539af3851aSeschrock void *
7549af3851aSeschrock fmd_hdl_alloc(fmd_hdl_t *hdl, size_t size, int flags)
7559af3851aSeschrock {
7569af3851aSeschrock 	fmd_module_t *mp = fmd_api_module_lock(hdl);
7579af3851aSeschrock 	void *data;
7589af3851aSeschrock 
7599af3851aSeschrock 	data = fmd_hdl_alloc_locked(mp, size, flags);
7609af3851aSeschrock 
7617c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
7627c478bd9Sstevel@tonic-gate 	return (data);
7637c478bd9Sstevel@tonic-gate }
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate void *
7667c478bd9Sstevel@tonic-gate fmd_hdl_zalloc(fmd_hdl_t *hdl, size_t size, int flags)
7677c478bd9Sstevel@tonic-gate {
7687c478bd9Sstevel@tonic-gate 	void *data = fmd_hdl_alloc(hdl, size, flags);
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	if (data != NULL)
7717c478bd9Sstevel@tonic-gate 		bzero(data, size);
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	return (data);
7747c478bd9Sstevel@tonic-gate }
7757c478bd9Sstevel@tonic-gate 
7769af3851aSeschrock static void
7779af3851aSeschrock fmd_hdl_free_locked(fmd_module_t *mp, void *data, size_t size)
7789af3851aSeschrock {
7799af3851aSeschrock 	fmd_free(data, size);
7809af3851aSeschrock 	mp->mod_stats->ms_memtotal.fmds_value.ui64 -= size;
7819af3851aSeschrock }
7829af3851aSeschrock 
7837c478bd9Sstevel@tonic-gate void
7847c478bd9Sstevel@tonic-gate fmd_hdl_free(fmd_hdl_t *hdl, void *data, size_t size)
7857c478bd9Sstevel@tonic-gate {
7867c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
7877c478bd9Sstevel@tonic-gate 
7889af3851aSeschrock 	fmd_hdl_free_locked(mp, data, size);
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
7917c478bd9Sstevel@tonic-gate }
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate char *
7947c478bd9Sstevel@tonic-gate fmd_hdl_strdup(fmd_hdl_t *hdl, const char *s, int flags)
7957c478bd9Sstevel@tonic-gate {
7967c478bd9Sstevel@tonic-gate 	char *p;
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 	if (s != NULL)
7997c478bd9Sstevel@tonic-gate 		p = fmd_hdl_alloc(hdl, strlen(s) + 1, flags);
8007c478bd9Sstevel@tonic-gate 	else
8017c478bd9Sstevel@tonic-gate 		p = NULL;
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 	if (p != NULL)
8047c478bd9Sstevel@tonic-gate 		(void) strcpy(p, s);
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	return (p);
8077c478bd9Sstevel@tonic-gate }
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate void
8107c478bd9Sstevel@tonic-gate fmd_hdl_strfree(fmd_hdl_t *hdl, char *s)
8117c478bd9Sstevel@tonic-gate {
8127c478bd9Sstevel@tonic-gate 	if (s != NULL)
8137c478bd9Sstevel@tonic-gate 		fmd_hdl_free(hdl, s, strlen(s) + 1);
8147c478bd9Sstevel@tonic-gate }
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate void
8177c478bd9Sstevel@tonic-gate fmd_hdl_vabort(fmd_hdl_t *hdl, const char *format, va_list ap)
8187c478bd9Sstevel@tonic-gate {
8197c478bd9Sstevel@tonic-gate 	fmd_api_verror(fmd_api_module_lock(hdl), EFMD_HDL_ABORT, format, ap);
8207c478bd9Sstevel@tonic-gate }
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
8237c478bd9Sstevel@tonic-gate void
8247c478bd9Sstevel@tonic-gate fmd_hdl_abort(fmd_hdl_t *hdl, const char *format, ...)
8257c478bd9Sstevel@tonic-gate {
8267c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
8277c478bd9Sstevel@tonic-gate 	va_list ap;
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 	va_start(ap, format);
8307c478bd9Sstevel@tonic-gate 	fmd_api_verror(mp, EFMD_HDL_ABORT, format, ap);
8317c478bd9Sstevel@tonic-gate 	va_end(ap);
8327c478bd9Sstevel@tonic-gate }
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate void
8357c478bd9Sstevel@tonic-gate fmd_hdl_verror(fmd_hdl_t *hdl, const char *format, va_list ap)
8367c478bd9Sstevel@tonic-gate {
8377c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
8387c478bd9Sstevel@tonic-gate 	fmd_api_vxerror(mp, errno, format, ap);
8397c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
8407c478bd9Sstevel@tonic-gate }
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
8437c478bd9Sstevel@tonic-gate void
8447c478bd9Sstevel@tonic-gate fmd_hdl_error(fmd_hdl_t *hdl, const char *format, ...)
8457c478bd9Sstevel@tonic-gate {
8467c478bd9Sstevel@tonic-gate 	va_list ap;
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate 	va_start(ap, format);
8497c478bd9Sstevel@tonic-gate 	fmd_hdl_verror(hdl, format, ap);
8507c478bd9Sstevel@tonic-gate 	va_end(ap);
8517c478bd9Sstevel@tonic-gate }
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate void
8547c478bd9Sstevel@tonic-gate fmd_hdl_vdebug(fmd_hdl_t *hdl, const char *format, va_list ap)
8557c478bd9Sstevel@tonic-gate {
8567c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 	char *msg;
8597c478bd9Sstevel@tonic-gate 	size_t len;
8607c478bd9Sstevel@tonic-gate 	char c;
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 	if (!(fmd.d_hdl_debug)) {
8637c478bd9Sstevel@tonic-gate 		mp->mod_stats->ms_debugdrop.fmds_value.ui64++;
8647c478bd9Sstevel@tonic-gate 		fmd_module_unlock(mp);
8657c478bd9Sstevel@tonic-gate 		return;
8667c478bd9Sstevel@tonic-gate 	}
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	len = vsnprintf(&c, 1, format, ap);
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 	if ((msg = fmd_alloc(len + 2, FMD_NOSLEEP)) == NULL) {
8717c478bd9Sstevel@tonic-gate 		mp->mod_stats->ms_debugdrop.fmds_value.ui64++;
8727c478bd9Sstevel@tonic-gate 		fmd_module_unlock(mp);
8737c478bd9Sstevel@tonic-gate 		return;
8747c478bd9Sstevel@tonic-gate 	}
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 	(void) vsnprintf(msg, len + 1, format, ap);
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 	if (msg[len - 1] != '\n')
8797c478bd9Sstevel@tonic-gate 		(void) strcpy(&msg[len], "\n");
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 	if (fmd.d_hdl_dbout & FMD_DBOUT_STDERR) {
8827c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_lock(&fmd.d_err_lock);
8837c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s DEBUG: %s: %s",
8847c478bd9Sstevel@tonic-gate 		    fmd.d_pname, mp->mod_name, msg);
8857c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&fmd.d_err_lock);
8867c478bd9Sstevel@tonic-gate 	}
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 	if (fmd.d_hdl_dbout & FMD_DBOUT_SYSLOG) {
8897c478bd9Sstevel@tonic-gate 		syslog(LOG_DEBUG | LOG_DAEMON, "%s DEBUG: %s: %s",
8907c478bd9Sstevel@tonic-gate 		    fmd.d_pname, mp->mod_name, msg);
8917c478bd9Sstevel@tonic-gate 	}
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 	fmd_free(msg, len + 2);
8947c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
8957c478bd9Sstevel@tonic-gate }
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
8987c478bd9Sstevel@tonic-gate void
8997c478bd9Sstevel@tonic-gate fmd_hdl_debug(fmd_hdl_t *hdl, const char *format, ...)
9007c478bd9Sstevel@tonic-gate {
9017c478bd9Sstevel@tonic-gate 	va_list ap;
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 	va_start(ap, format);
9047c478bd9Sstevel@tonic-gate 	fmd_hdl_vdebug(hdl, format, ap);
9057c478bd9Sstevel@tonic-gate 	va_end(ap);
9067c478bd9Sstevel@tonic-gate }
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate int32_t
9097c478bd9Sstevel@tonic-gate fmd_prop_get_int32(fmd_hdl_t *hdl, const char *name)
9107c478bd9Sstevel@tonic-gate {
9117c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
9127c478bd9Sstevel@tonic-gate 	const fmd_conf_ops_t *ops = fmd_conf_gettype(mp->mod_conf, name);
9137c478bd9Sstevel@tonic-gate 	int32_t value = 0;
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate 	if (ops == &fmd_conf_bool || ops == &fmd_conf_int32 ||
9167c478bd9Sstevel@tonic-gate 	    ops == &fmd_conf_uint32)
9177c478bd9Sstevel@tonic-gate 		(void) fmd_conf_getprop(mp->mod_conf, name, &value);
9187c478bd9Sstevel@tonic-gate 	else if (ops != NULL) {
9197c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_PROP_TYPE,
9207c478bd9Sstevel@tonic-gate 		    "property %s is not of int32 type\n", name);
9217c478bd9Sstevel@tonic-gate 	} else {
9227c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_PROP_DEFN,
9237c478bd9Sstevel@tonic-gate 		    "property %s is not defined\n", name);
9247c478bd9Sstevel@tonic-gate 	}
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
9277c478bd9Sstevel@tonic-gate 	return (value);
9287c478bd9Sstevel@tonic-gate }
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate int64_t
9317c478bd9Sstevel@tonic-gate fmd_prop_get_int64(fmd_hdl_t *hdl, const char *name)
9327c478bd9Sstevel@tonic-gate {
9337c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
9347c478bd9Sstevel@tonic-gate 	const fmd_conf_ops_t *ops = fmd_conf_gettype(mp->mod_conf, name);
9357c478bd9Sstevel@tonic-gate 	int64_t value = 0;
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 	if (ops == &fmd_conf_int64 || ops == &fmd_conf_uint64 ||
9387c478bd9Sstevel@tonic-gate 	    ops == &fmd_conf_time || ops == &fmd_conf_size)
9397c478bd9Sstevel@tonic-gate 		(void) fmd_conf_getprop(mp->mod_conf, name, &value);
9407c478bd9Sstevel@tonic-gate 	else if (ops != NULL) {
9417c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_PROP_TYPE,
9427c478bd9Sstevel@tonic-gate 		    "property %s is not of int64 type\n", name);
9437c478bd9Sstevel@tonic-gate 	} else {
9447c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_PROP_DEFN,
9457c478bd9Sstevel@tonic-gate 		    "property %s is not defined\n", name);
9467c478bd9Sstevel@tonic-gate 	}
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
9497c478bd9Sstevel@tonic-gate 	return (value);
9507c478bd9Sstevel@tonic-gate }
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate char *
9537c478bd9Sstevel@tonic-gate fmd_prop_get_string(fmd_hdl_t *hdl, const char *name)
9547c478bd9Sstevel@tonic-gate {
9557c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
9567c478bd9Sstevel@tonic-gate 	const fmd_conf_ops_t *ops = fmd_conf_gettype(mp->mod_conf, name);
9577c478bd9Sstevel@tonic-gate 	char *value = NULL;
9587c478bd9Sstevel@tonic-gate 	const char *s;
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	if (ops == &fmd_conf_string) {
9617c478bd9Sstevel@tonic-gate 		(void) fmd_conf_getprop(mp->mod_conf, name, &s);
9627c478bd9Sstevel@tonic-gate 		value = fmd_strdup(s, FMD_SLEEP);
9637c478bd9Sstevel@tonic-gate 	} else if (ops != NULL) {
9647c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_PROP_TYPE,
9657c478bd9Sstevel@tonic-gate 		    "property %s is not of string type\n", name);
9667c478bd9Sstevel@tonic-gate 	} else {
9677c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_PROP_DEFN,
9687c478bd9Sstevel@tonic-gate 		    "property %s is not defined\n", name);
9697c478bd9Sstevel@tonic-gate 	}
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
9727c478bd9Sstevel@tonic-gate 	return (value);
9737c478bd9Sstevel@tonic-gate }
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate void
9767c478bd9Sstevel@tonic-gate fmd_prop_free_string(fmd_hdl_t *hdl, char *s)
9777c478bd9Sstevel@tonic-gate {
9787c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
9797c478bd9Sstevel@tonic-gate 	fmd_strfree(s);
9807c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
9817c478bd9Sstevel@tonic-gate }
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate fmd_stat_t *
9847c478bd9Sstevel@tonic-gate fmd_stat_create(fmd_hdl_t *hdl, uint_t flags, uint_t argc, fmd_stat_t *argv)
9857c478bd9Sstevel@tonic-gate {
9867c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
9877c478bd9Sstevel@tonic-gate 	fmd_stat_t *ep, *sp;
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate 	if (flags & ~FMD_STAT_ALLOC) {
9907c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_STAT_FLAGS,
9917c478bd9Sstevel@tonic-gate 		    "invalid flags 0x%x passed to fmd_stat_create\n", flags);
9927c478bd9Sstevel@tonic-gate 	}
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate 	if ((sp = fmd_ustat_insert(mp->mod_ustat,
9957c478bd9Sstevel@tonic-gate 	    flags | FMD_USTAT_VALIDATE, argc, argv, &ep)) == NULL) {
9967c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, errno,
9977c478bd9Sstevel@tonic-gate 		    "failed to publish stat '%s'", ep->fmds_name);
9987c478bd9Sstevel@tonic-gate 	}
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
10017c478bd9Sstevel@tonic-gate 	return (sp);
10027c478bd9Sstevel@tonic-gate }
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate void
10057c478bd9Sstevel@tonic-gate fmd_stat_destroy(fmd_hdl_t *hdl, uint_t argc, fmd_stat_t *argv)
10067c478bd9Sstevel@tonic-gate {
10077c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
10087c478bd9Sstevel@tonic-gate 	fmd_ustat_delete(mp->mod_ustat, argc, argv);
10097c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
10107c478bd9Sstevel@tonic-gate }
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate void
10137c478bd9Sstevel@tonic-gate fmd_stat_setstr(fmd_hdl_t *hdl, fmd_stat_t *sp, const char *s)
10147c478bd9Sstevel@tonic-gate {
10157c478bd9Sstevel@tonic-gate 	char *str = fmd_strdup(s, FMD_SLEEP);
10167c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate 	if (sp->fmds_type != FMD_TYPE_STRING) {
10197c478bd9Sstevel@tonic-gate 		fmd_strfree(str);
10207c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_STAT_TYPE,
10217c478bd9Sstevel@tonic-gate 		    "stat '%s' is not a string\n", sp->fmds_name);
10227c478bd9Sstevel@tonic-gate 	}
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate 	fmd_strfree(sp->fmds_value.str);
10257c478bd9Sstevel@tonic-gate 	sp->fmds_value.str = str;
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
10287c478bd9Sstevel@tonic-gate }
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate fmd_case_t *
10317c478bd9Sstevel@tonic-gate fmd_case_open(fmd_hdl_t *hdl, void *data)
10327c478bd9Sstevel@tonic-gate {
10337c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
10347c478bd9Sstevel@tonic-gate 	fmd_case_t *cp = fmd_case_create(mp, data);
10357c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
10367c478bd9Sstevel@tonic-gate 	return (cp);
10377c478bd9Sstevel@tonic-gate }
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate void
10407c478bd9Sstevel@tonic-gate fmd_case_reset(fmd_hdl_t *hdl, fmd_case_t *cp)
10417c478bd9Sstevel@tonic-gate {
10427c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
10437c478bd9Sstevel@tonic-gate 	fmd_case_impl_t *cip = fmd_api_case_impl(mp, cp);
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate 	if (cip->ci_state >= FMD_CASE_SOLVED) {
10467c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_CASE_STATE, "cannot solve %s: "
10477c478bd9Sstevel@tonic-gate 		    "case is already solved or closed\n", cip->ci_uuid);
10487c478bd9Sstevel@tonic-gate 	}
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 	fmd_case_reset_suspects(cp);
10517c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
10527c478bd9Sstevel@tonic-gate }
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate void
10557c478bd9Sstevel@tonic-gate fmd_case_solve(fmd_hdl_t *hdl, fmd_case_t *cp)
10567c478bd9Sstevel@tonic-gate {
10577c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
10587c478bd9Sstevel@tonic-gate 	fmd_case_impl_t *cip = fmd_api_case_impl(mp, cp);
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 	if (cip->ci_state >= FMD_CASE_SOLVED) {
10617c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_CASE_STATE, "cannot solve %s: "
10627c478bd9Sstevel@tonic-gate 		    "case is already solved or closed\n", cip->ci_uuid);
10637c478bd9Sstevel@tonic-gate 	}
10647c478bd9Sstevel@tonic-gate 
1065d9638e54Smws 	fmd_case_transition(cp, FMD_CASE_SOLVED, FMD_CF_SOLVED);
10667c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
10677c478bd9Sstevel@tonic-gate }
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate void
10707c478bd9Sstevel@tonic-gate fmd_case_close(fmd_hdl_t *hdl, fmd_case_t *cp)
10717c478bd9Sstevel@tonic-gate {
10727c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 	(void) fmd_api_case_impl(mp, cp); /* validate 'cp' */
1075d9638e54Smws 	fmd_case_transition(cp, FMD_CASE_CLOSE_WAIT, FMD_CF_ISOLATED);
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
10787c478bd9Sstevel@tonic-gate }
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate const char *
10817c478bd9Sstevel@tonic-gate fmd_case_uuid(fmd_hdl_t *hdl, fmd_case_t *cp)
10827c478bd9Sstevel@tonic-gate {
10837c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
10847c478bd9Sstevel@tonic-gate 	fmd_case_impl_t *cip = fmd_api_case_impl(mp, cp);
10857c478bd9Sstevel@tonic-gate 	const char *uuid = cip->ci_uuid;
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
10887c478bd9Sstevel@tonic-gate 	return (uuid);
10897c478bd9Sstevel@tonic-gate }
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate fmd_case_t *
10927c478bd9Sstevel@tonic-gate fmd_case_uulookup(fmd_hdl_t *hdl, const char *uuid)
10937c478bd9Sstevel@tonic-gate {
10947c478bd9Sstevel@tonic-gate 	fmd_module_t *cmp, *mp = fmd_api_module_lock(hdl);
10957c478bd9Sstevel@tonic-gate 	fmd_case_t *cp = fmd_case_hash_lookup(fmd.d_cases, uuid);
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 	if (cp != NULL) {
10987c478bd9Sstevel@tonic-gate 		cmp = ((fmd_case_impl_t *)cp)->ci_mod;
10997c478bd9Sstevel@tonic-gate 		fmd_case_rele(cp);
11007c478bd9Sstevel@tonic-gate 	} else
11017c478bd9Sstevel@tonic-gate 		cmp = NULL;
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
11047c478bd9Sstevel@tonic-gate 	return (cmp == mp ? cp : NULL);
11057c478bd9Sstevel@tonic-gate }
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate void
11087c478bd9Sstevel@tonic-gate fmd_case_uuclose(fmd_hdl_t *hdl, const char *uuid)
11097c478bd9Sstevel@tonic-gate {
11107c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
11117c478bd9Sstevel@tonic-gate 	fmd_case_t *cp = fmd_case_hash_lookup(fmd.d_cases, uuid);
11127c478bd9Sstevel@tonic-gate 
1113d9638e54Smws 	if (cp != NULL) {
1114d9638e54Smws 		fmd_case_transition(cp, FMD_CASE_CLOSE_WAIT, FMD_CF_ISOLATED);
11157c478bd9Sstevel@tonic-gate 		fmd_case_rele(cp);
1116d9638e54Smws 	}
1117d9638e54Smws 
11187c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
11197c478bd9Sstevel@tonic-gate }
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate int
11227c478bd9Sstevel@tonic-gate fmd_case_uuclosed(fmd_hdl_t *hdl, const char *uuid)
11237c478bd9Sstevel@tonic-gate {
11247c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
11257c478bd9Sstevel@tonic-gate 	fmd_case_t *cp = fmd_case_hash_lookup(fmd.d_cases, uuid);
11267c478bd9Sstevel@tonic-gate 	fmd_case_impl_t *cip = (fmd_case_impl_t *)cp;
11277c478bd9Sstevel@tonic-gate 	int rv = FMD_B_TRUE;
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 	if (cip != NULL) {
1130d9638e54Smws 		rv = cip->ci_state >= FMD_CASE_CLOSE_WAIT;
11317c478bd9Sstevel@tonic-gate 		fmd_case_rele(cp);
11327c478bd9Sstevel@tonic-gate 	}
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
11357c478bd9Sstevel@tonic-gate 	return (rv);
11367c478bd9Sstevel@tonic-gate }
11377c478bd9Sstevel@tonic-gate 
113825c6ff4bSstephh void
113925c6ff4bSstephh fmd_case_uuresolved(fmd_hdl_t *hdl, const char *uuid)
114025c6ff4bSstephh {
114125c6ff4bSstephh 	fmd_module_t *mp = fmd_api_module_lock(hdl);
114225c6ff4bSstephh 	fmd_case_t *cp = fmd_case_hash_lookup(fmd.d_cases, uuid);
114325c6ff4bSstephh 
114425c6ff4bSstephh 	if (cp != NULL) {
1145cbf75e67SStephen Hanson 		fmd_case_impl_t *cip = (fmd_case_impl_t *)cp;
1146cbf75e67SStephen Hanson 		/*
1147cbf75e67SStephen Hanson 		 * For a proxy, we notify the diagnosing side, and then
1148cbf75e67SStephen Hanson 		 * wait for it to send us back a list.resolved.
1149cbf75e67SStephen Hanson 		 */
1150cbf75e67SStephen Hanson 		if (cip->ci_xprt != NULL)
1151cbf75e67SStephen Hanson 			fmd_xprt_uuresolved(cip->ci_xprt, cip->ci_uuid);
1152cbf75e67SStephen Hanson 		else
115325c6ff4bSstephh 			fmd_case_transition(cp, FMD_CASE_RESOLVED, 0);
115425c6ff4bSstephh 		fmd_case_rele(cp);
115525c6ff4bSstephh 	}
115625c6ff4bSstephh 
115725c6ff4bSstephh 	fmd_module_unlock(mp);
115825c6ff4bSstephh }
115925c6ff4bSstephh 
11607c478bd9Sstevel@tonic-gate static int
11617c478bd9Sstevel@tonic-gate fmd_case_instate(fmd_hdl_t *hdl, fmd_case_t *cp, uint_t state)
11627c478bd9Sstevel@tonic-gate {
11637c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
11647c478bd9Sstevel@tonic-gate 	fmd_case_impl_t *cip = fmd_api_case_impl(mp, cp);
11657c478bd9Sstevel@tonic-gate 	int rv = cip->ci_state >= state;
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
11687c478bd9Sstevel@tonic-gate 	return (rv);
11697c478bd9Sstevel@tonic-gate }
11707c478bd9Sstevel@tonic-gate 
11717c478bd9Sstevel@tonic-gate int
11727c478bd9Sstevel@tonic-gate fmd_case_solved(fmd_hdl_t *hdl, fmd_case_t *cp)
11737c478bd9Sstevel@tonic-gate {
11747c478bd9Sstevel@tonic-gate 	return (fmd_case_instate(hdl, cp, FMD_CASE_SOLVED));
11757c478bd9Sstevel@tonic-gate }
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate int
11787c478bd9Sstevel@tonic-gate fmd_case_closed(fmd_hdl_t *hdl, fmd_case_t *cp)
11797c478bd9Sstevel@tonic-gate {
1180d9638e54Smws 	return (fmd_case_instate(hdl, cp, FMD_CASE_CLOSE_WAIT));
11817c478bd9Sstevel@tonic-gate }
11827c478bd9Sstevel@tonic-gate 
11837c478bd9Sstevel@tonic-gate void
11847c478bd9Sstevel@tonic-gate fmd_case_add_ereport(fmd_hdl_t *hdl, fmd_case_t *cp, fmd_event_t *ep)
11857c478bd9Sstevel@tonic-gate {
11867c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate 	(void) fmd_api_case_impl(mp, cp); /* validate 'cp' */
11897aec1d6eScindi 
11907aec1d6eScindi 	if (fmd_case_insert_event(cp, ep))
11917c478bd9Sstevel@tonic-gate 		mp->mod_stats->ms_accepted.fmds_value.ui64++;
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
11947c478bd9Sstevel@tonic-gate }
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate void
11977c478bd9Sstevel@tonic-gate fmd_case_add_serd(fmd_hdl_t *hdl, fmd_case_t *cp, const char *name)
11987c478bd9Sstevel@tonic-gate {
11997c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
12007c478bd9Sstevel@tonic-gate 	fmd_serd_elem_t *sep;
12017c478bd9Sstevel@tonic-gate 	fmd_serd_eng_t *sgp;
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate 	if ((sgp = fmd_serd_eng_lookup(&mp->mod_serds, name)) == NULL) {
12047c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_SERD_NAME,
12057c478bd9Sstevel@tonic-gate 		    "failed to add events from serd engine '%s'", name);
12067c478bd9Sstevel@tonic-gate 	}
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate 	(void) fmd_api_case_impl(mp, cp); /* validate 'cp' */
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate 	for (sep = fmd_list_next(&sgp->sg_list);
12117aec1d6eScindi 	    sep != NULL; sep = fmd_list_next(sep)) {
12127aec1d6eScindi 		if (fmd_case_insert_event(cp, sep->se_event))
12137aec1d6eScindi 			mp->mod_stats->ms_accepted.fmds_value.ui64++;
12147aec1d6eScindi 	}
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
12177c478bd9Sstevel@tonic-gate }
12187c478bd9Sstevel@tonic-gate 
12197c478bd9Sstevel@tonic-gate void
12207c478bd9Sstevel@tonic-gate fmd_case_add_suspect(fmd_hdl_t *hdl, fmd_case_t *cp, nvlist_t *nvl)
12217c478bd9Sstevel@tonic-gate {
12227c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
12237c478bd9Sstevel@tonic-gate 	fmd_case_impl_t *cip = fmd_api_case_impl(mp, cp);
12247c478bd9Sstevel@tonic-gate 	char *class;
1225b7d3956bSstephh 	topo_hdl_t *thp;
1226b7d3956bSstephh 	int err;
12278e7248e5SStephen Hanson 	nvlist_t *rsrc = NULL, *asru_prop = NULL, *asru = NULL, *fru = NULL;
1228b7d3956bSstephh 	char *loc = NULL, *serial = NULL;
12297c478bd9Sstevel@tonic-gate 
12307c478bd9Sstevel@tonic-gate 	if (cip->ci_state >= FMD_CASE_SOLVED) {
12317c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_CASE_STATE, "cannot add suspect to "
12327c478bd9Sstevel@tonic-gate 		    "%s: case is already solved or closed\n", cip->ci_uuid);
12337c478bd9Sstevel@tonic-gate 	}
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 	if (nvlist_lookup_string(nvl, FM_CLASS, &class) != 0 ||
12367c478bd9Sstevel@tonic-gate 	    class == NULL || *class == '\0') {
12377c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_CASE_EVENT, "cannot add suspect to "
12387c478bd9Sstevel@tonic-gate 		    "%s: suspect event is missing a class\n", cip->ci_uuid);
12397c478bd9Sstevel@tonic-gate 	}
12407c478bd9Sstevel@tonic-gate 
1241b7d3956bSstephh 	thp = fmd_module_topo_hold(mp);
1242b7d3956bSstephh 	(void) nvlist_lookup_nvlist(nvl, FM_FAULT_RESOURCE, &rsrc);
1243b7d3956bSstephh 	(void) nvlist_lookup_nvlist(nvl, FM_FAULT_ASRU, &asru);
1244b7d3956bSstephh 	(void) nvlist_lookup_nvlist(nvl, FM_FAULT_FRU, &fru);
1245b7d3956bSstephh 	if (rsrc != NULL) {
1246b7d3956bSstephh 		if (strncmp(class, "defect", 6) == 0) {
1247b7d3956bSstephh 			if (asru == NULL && topo_fmri_getprop(thp, rsrc,
1248b7d3956bSstephh 			    TOPO_PGROUP_IO, TOPO_IO_MODULE, rsrc,
12498e7248e5SStephen Hanson 			    &asru_prop, &err) == 0 &&
12508e7248e5SStephen Hanson 			    nvlist_lookup_nvlist(asru_prop, TOPO_PROP_VAL_VAL,
12518e7248e5SStephen Hanson 			    &asru) == 0) {
1252b7d3956bSstephh 				(void) nvlist_add_nvlist(nvl, FM_FAULT_ASRU,
1253b7d3956bSstephh 				    asru);
1254*705e9f42SStephen Hanson 				nvlist_free(asru_prop);
1255b7d3956bSstephh 				(void) nvlist_lookup_nvlist(nvl, FM_FAULT_ASRU,
1256b7d3956bSstephh 				    &asru);
1257b7d3956bSstephh 			}
1258b7d3956bSstephh 		} else {
1259b7d3956bSstephh 			if (topo_fmri_asru(thp, rsrc, &asru, &err) == 0) {
1260b7d3956bSstephh 				(void) nvlist_remove(nvl, FM_FAULT_ASRU,
1261b7d3956bSstephh 				    DATA_TYPE_NVLIST);
1262b7d3956bSstephh 				(void) nvlist_add_nvlist(nvl, FM_FAULT_ASRU,
1263b7d3956bSstephh 				    asru);
1264b7d3956bSstephh 				nvlist_free(asru);
1265b7d3956bSstephh 				(void) nvlist_lookup_nvlist(nvl, FM_FAULT_ASRU,
1266b7d3956bSstephh 				    &asru);
1267b7d3956bSstephh 			}
1268b7d3956bSstephh 			if (topo_fmri_fru(thp, rsrc, &fru, &err) == 0) {
1269b7d3956bSstephh 				(void) nvlist_remove(nvl, FM_FAULT_FRU,
1270b7d3956bSstephh 				    DATA_TYPE_NVLIST);
1271b7d3956bSstephh 				(void) nvlist_add_nvlist(nvl, FM_FAULT_FRU,
1272b7d3956bSstephh 				    fru);
1273b7d3956bSstephh 				nvlist_free(fru);
1274b7d3956bSstephh 				(void) nvlist_lookup_nvlist(nvl, FM_FAULT_FRU,
1275b7d3956bSstephh 				    &fru);
1276b7d3956bSstephh 			}
1277b7d3956bSstephh 		}
1278b7d3956bSstephh 	}
1279b7d3956bSstephh 
1280b7d3956bSstephh 	/*
1281b7d3956bSstephh 	 * Try to find the location label for this resource
1282b7d3956bSstephh 	 */
1283*705e9f42SStephen Hanson 	if (strncmp(class, "defect", 6) != 0) {
1284b7d3956bSstephh 		if (fru != NULL)
1285b7d3956bSstephh 			(void) topo_fmri_label(thp, fru, &loc, &err);
1286b7d3956bSstephh 		else if (rsrc != NULL)
1287b7d3956bSstephh 			(void) topo_fmri_label(thp, rsrc, &loc, &err);
1288*705e9f42SStephen Hanson 		if (loc != NULL) {
1289*705e9f42SStephen Hanson 			(void) nvlist_remove(nvl, FM_FAULT_LOCATION,
1290*705e9f42SStephen Hanson 			    DATA_TYPE_STRING);
1291b7d3956bSstephh 			(void) nvlist_add_string(nvl, FM_FAULT_LOCATION, loc);
1292b7d3956bSstephh 			topo_hdl_strfree(thp, loc);
1293b7d3956bSstephh 		}
1294*705e9f42SStephen Hanson 	}
1295b7d3956bSstephh 
1296b7d3956bSstephh 	/*
1297b7d3956bSstephh 	 * In some cases, serial information for the resource will not be
1298b7d3956bSstephh 	 * available at enumeration but may instead be available by invoking
1299b7d3956bSstephh 	 * a dynamic property method on the FRU.  In order to ensure the serial
1300b7d3956bSstephh 	 * number is persisted properly in the ASRU cache, we'll fetch the
1301b7d3956bSstephh 	 * property, if it exists, and add it to the resource and fru fmris.
130283031c4cSSrihari Venkatesan 	 * If the DE has not listed a fru in the suspect, see if we can
130383031c4cSSrihari Venkatesan 	 * retrieve the serial from the resource instead.
1304b7d3956bSstephh 	 */
1305b7d3956bSstephh 	if (fru != NULL) {
1306b7d3956bSstephh 		(void) topo_fmri_serial(thp, fru, &serial, &err);
1307b7d3956bSstephh 		if (serial != NULL) {
1308b7d3956bSstephh 			(void) nvlist_add_string(fru, "serial", serial);
1309b7d3956bSstephh 			topo_hdl_strfree(thp, serial);
1310b7d3956bSstephh 		}
1311b7d3956bSstephh 	}
1312b7d3956bSstephh 
1313b7d3956bSstephh 	err = fmd_module_topo_rele(mp, thp);
1314b7d3956bSstephh 	ASSERT(err == 0);
1315b7d3956bSstephh 
13167c478bd9Sstevel@tonic-gate 	fmd_case_insert_suspect(cp, nvl);
13177c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
13187c478bd9Sstevel@tonic-gate }
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate void
13217c478bd9Sstevel@tonic-gate fmd_case_setspecific(fmd_hdl_t *hdl, fmd_case_t *cp, void *data)
13227c478bd9Sstevel@tonic-gate {
13237c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
13247c478bd9Sstevel@tonic-gate 	fmd_case_impl_t *cip = fmd_api_case_impl(mp, cp);
13257c478bd9Sstevel@tonic-gate 
13267c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&cip->ci_lock);
13277c478bd9Sstevel@tonic-gate 	cip->ci_data = data;
13287c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&cip->ci_lock);
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
13317c478bd9Sstevel@tonic-gate }
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate void *
13347c478bd9Sstevel@tonic-gate fmd_case_getspecific(fmd_hdl_t *hdl, fmd_case_t *cp)
13357c478bd9Sstevel@tonic-gate {
13367c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
13377c478bd9Sstevel@tonic-gate 	fmd_case_impl_t *cip = fmd_api_case_impl(mp, cp);
13387c478bd9Sstevel@tonic-gate 	void *data;
13397c478bd9Sstevel@tonic-gate 
13407c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&cip->ci_lock);
13417c478bd9Sstevel@tonic-gate 	data = cip->ci_data;
13427c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&cip->ci_lock);
13437c478bd9Sstevel@tonic-gate 
13447c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
13457c478bd9Sstevel@tonic-gate 	return (data);
13467c478bd9Sstevel@tonic-gate }
13477c478bd9Sstevel@tonic-gate 
13487c478bd9Sstevel@tonic-gate void
13497c478bd9Sstevel@tonic-gate fmd_case_setprincipal(fmd_hdl_t *hdl, fmd_case_t *cp, fmd_event_t *ep)
13507c478bd9Sstevel@tonic-gate {
13517c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
13527c478bd9Sstevel@tonic-gate 
13537c478bd9Sstevel@tonic-gate 	(void) fmd_api_case_impl(mp, cp); /* validate 'cp' */
13547aec1d6eScindi 
13557aec1d6eScindi 	if (fmd_case_insert_principal(cp, ep))
13567c478bd9Sstevel@tonic-gate 		mp->mod_stats->ms_accepted.fmds_value.ui64++;
13577c478bd9Sstevel@tonic-gate 
13587c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
13597c478bd9Sstevel@tonic-gate }
13607c478bd9Sstevel@tonic-gate 
13617c478bd9Sstevel@tonic-gate fmd_event_t *
13627c478bd9Sstevel@tonic-gate fmd_case_getprincipal(fmd_hdl_t *hdl, fmd_case_t *cp)
13637c478bd9Sstevel@tonic-gate {
13647c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
13657c478bd9Sstevel@tonic-gate 	fmd_case_impl_t *cip = fmd_api_case_impl(mp, cp);
13667c478bd9Sstevel@tonic-gate 	fmd_event_t *ep;
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&cip->ci_lock);
13697c478bd9Sstevel@tonic-gate 	ep = cip->ci_principal;
13707c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&cip->ci_lock);
13717c478bd9Sstevel@tonic-gate 
13727c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
13737c478bd9Sstevel@tonic-gate 	return (ep);
13747c478bd9Sstevel@tonic-gate }
13757c478bd9Sstevel@tonic-gate 
13767c478bd9Sstevel@tonic-gate fmd_case_t *
13777c478bd9Sstevel@tonic-gate fmd_case_next(fmd_hdl_t *hdl, fmd_case_t *cp)
13787c478bd9Sstevel@tonic-gate {
13797c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
13807c478bd9Sstevel@tonic-gate 
13817c478bd9Sstevel@tonic-gate 	if (cp != NULL)
13827c478bd9Sstevel@tonic-gate 		cp = fmd_list_next(fmd_api_case_impl(mp, cp));
13837c478bd9Sstevel@tonic-gate 	else
13847c478bd9Sstevel@tonic-gate 		cp = fmd_list_next(&mp->mod_cases);
13857c478bd9Sstevel@tonic-gate 
13867c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
13877c478bd9Sstevel@tonic-gate 	return (cp);
13887c478bd9Sstevel@tonic-gate }
13897c478bd9Sstevel@tonic-gate 
13907c478bd9Sstevel@tonic-gate fmd_case_t *
13917c478bd9Sstevel@tonic-gate fmd_case_prev(fmd_hdl_t *hdl, fmd_case_t *cp)
13927c478bd9Sstevel@tonic-gate {
13937c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
13947c478bd9Sstevel@tonic-gate 
13957c478bd9Sstevel@tonic-gate 	if (cp != NULL)
13967c478bd9Sstevel@tonic-gate 		cp = fmd_list_prev(fmd_api_case_impl(mp, cp));
13977c478bd9Sstevel@tonic-gate 	else
13987c478bd9Sstevel@tonic-gate 		cp = fmd_list_prev(&mp->mod_cases);
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
14017c478bd9Sstevel@tonic-gate 	return (cp);
14027c478bd9Sstevel@tonic-gate }
14037c478bd9Sstevel@tonic-gate 
14047c478bd9Sstevel@tonic-gate /*
14057c478bd9Sstevel@tonic-gate  * Utility function for fmd_buf_* routines.  If a case is specified, use the
14067c478bd9Sstevel@tonic-gate  * case's ci_bufs hash; otherwise use the module's global mod_bufs hash.
14077c478bd9Sstevel@tonic-gate  */
14087c478bd9Sstevel@tonic-gate static fmd_buf_hash_t *
14097c478bd9Sstevel@tonic-gate fmd_buf_gethash(fmd_module_t *mp, fmd_case_t *cp)
14107c478bd9Sstevel@tonic-gate {
14117c478bd9Sstevel@tonic-gate 	return (cp ? &fmd_api_case_impl(mp, cp)->ci_bufs : &mp->mod_bufs);
14127c478bd9Sstevel@tonic-gate }
14137c478bd9Sstevel@tonic-gate 
14147c478bd9Sstevel@tonic-gate void
14157c478bd9Sstevel@tonic-gate fmd_buf_create(fmd_hdl_t *hdl, fmd_case_t *cp, const char *name, size_t size)
14167c478bd9Sstevel@tonic-gate {
14177c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
14187c478bd9Sstevel@tonic-gate 	fmd_buf_hash_t *bhp = fmd_buf_gethash(mp, cp);
14197c478bd9Sstevel@tonic-gate 	fmd_buf_t *bp = fmd_buf_lookup(bhp, name);
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate 	if (bp == NULL) {
14227c478bd9Sstevel@tonic-gate 		if (fmd_strbadid(name, FMD_B_TRUE) != NULL || size == 0) {
14237c478bd9Sstevel@tonic-gate 			fmd_api_error(mp, EFMD_BUF_INVAL, "cannot create '%s' "
14247c478bd9Sstevel@tonic-gate 			    "(size %lu): %s\n", name, (ulong_t)size,
14257c478bd9Sstevel@tonic-gate 			    fmd_strerror(EFMD_BUF_INVAL));
14267c478bd9Sstevel@tonic-gate 		}
14277c478bd9Sstevel@tonic-gate 
14287c478bd9Sstevel@tonic-gate 		if (mp->mod_stats->ms_buflimit.fmds_value.ui64 -
14297c478bd9Sstevel@tonic-gate 		    mp->mod_stats->ms_buftotal.fmds_value.ui64 < size) {
14307c478bd9Sstevel@tonic-gate 			fmd_api_error(mp, EFMD_BUF_LIMIT, "cannot create '%s': "
14317c478bd9Sstevel@tonic-gate 			    "buf limit exceeded (%llu)\n", name, (u_longlong_t)
14327c478bd9Sstevel@tonic-gate 			    mp->mod_stats->ms_buflimit.fmds_value.ui64);
14337c478bd9Sstevel@tonic-gate 		}
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate 		mp->mod_stats->ms_buftotal.fmds_value.ui64 += size;
14367c478bd9Sstevel@tonic-gate 		bp = fmd_buf_insert(bhp, name, size);
14377c478bd9Sstevel@tonic-gate 
14387c478bd9Sstevel@tonic-gate 	} else {
14397c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_BUF_EXISTS,
14407c478bd9Sstevel@tonic-gate 		    "cannot create '%s': buffer already exists\n", name);
14417c478bd9Sstevel@tonic-gate 	}
14427c478bd9Sstevel@tonic-gate 
14437c478bd9Sstevel@tonic-gate 	if (cp != NULL)
14447c478bd9Sstevel@tonic-gate 		fmd_case_setdirty(cp);
14457c478bd9Sstevel@tonic-gate 	else
14467c478bd9Sstevel@tonic-gate 		fmd_module_setdirty(mp);
14477c478bd9Sstevel@tonic-gate 
14487c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
14497c478bd9Sstevel@tonic-gate }
14507c478bd9Sstevel@tonic-gate 
14517c478bd9Sstevel@tonic-gate void
14527c478bd9Sstevel@tonic-gate fmd_buf_destroy(fmd_hdl_t *hdl, fmd_case_t *cp, const char *name)
14537c478bd9Sstevel@tonic-gate {
14547c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
14557c478bd9Sstevel@tonic-gate 	fmd_buf_hash_t *bhp = fmd_buf_gethash(mp, cp);
14567c478bd9Sstevel@tonic-gate 	fmd_buf_t *bp = fmd_buf_lookup(bhp, name);
14577c478bd9Sstevel@tonic-gate 
14587c478bd9Sstevel@tonic-gate 	if (bp != NULL) {
14597c478bd9Sstevel@tonic-gate 		mp->mod_stats->ms_buftotal.fmds_value.ui64 -= bp->buf_size;
14607c478bd9Sstevel@tonic-gate 		fmd_buf_delete(bhp, name);
14617c478bd9Sstevel@tonic-gate 
14627c478bd9Sstevel@tonic-gate 		if (cp != NULL)
14637c478bd9Sstevel@tonic-gate 			fmd_case_setdirty(cp);
14647c478bd9Sstevel@tonic-gate 		else
14657c478bd9Sstevel@tonic-gate 			fmd_module_setdirty(mp);
14667c478bd9Sstevel@tonic-gate 	}
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
14697c478bd9Sstevel@tonic-gate }
14707c478bd9Sstevel@tonic-gate 
14717c478bd9Sstevel@tonic-gate void
14727c478bd9Sstevel@tonic-gate fmd_buf_read(fmd_hdl_t *hdl, fmd_case_t *cp,
14737c478bd9Sstevel@tonic-gate     const char *name, void *buf, size_t size)
14747c478bd9Sstevel@tonic-gate {
14757c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
14767c478bd9Sstevel@tonic-gate 	fmd_buf_t *bp = fmd_buf_lookup(fmd_buf_gethash(mp, cp), name);
14777c478bd9Sstevel@tonic-gate 
14787c478bd9Sstevel@tonic-gate 	if (bp == NULL) {
14797c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_BUF_NOENT, "no buf named '%s' is "
14807c478bd9Sstevel@tonic-gate 		    "associated with %s\n", name, cp ? "case" : "module");
14817c478bd9Sstevel@tonic-gate 	}
14827c478bd9Sstevel@tonic-gate 
14837c478bd9Sstevel@tonic-gate 	bcopy(bp->buf_data, buf, MIN(bp->buf_size, size));
14847c478bd9Sstevel@tonic-gate 	if (size > bp->buf_size)
14857c478bd9Sstevel@tonic-gate 		bzero((char *)buf + bp->buf_size, size - bp->buf_size);
14867c478bd9Sstevel@tonic-gate 
14877c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
14887c478bd9Sstevel@tonic-gate }
14897c478bd9Sstevel@tonic-gate 
14907c478bd9Sstevel@tonic-gate void
14917c478bd9Sstevel@tonic-gate fmd_buf_write(fmd_hdl_t *hdl, fmd_case_t *cp,
14927c478bd9Sstevel@tonic-gate     const char *name, const void *buf, size_t size)
14937c478bd9Sstevel@tonic-gate {
14947c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
14957c478bd9Sstevel@tonic-gate 	fmd_buf_hash_t *bhp = fmd_buf_gethash(mp, cp);
14967c478bd9Sstevel@tonic-gate 	fmd_buf_t *bp = fmd_buf_lookup(bhp, name);
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate 	if (bp == NULL) {
14997c478bd9Sstevel@tonic-gate 		if (fmd_strbadid(name, FMD_B_TRUE) != NULL || size == 0) {
15007c478bd9Sstevel@tonic-gate 			fmd_api_error(mp, EFMD_BUF_INVAL, "cannot write '%s' "
15017c478bd9Sstevel@tonic-gate 			    "(size %lu): %s\n", name, (ulong_t)size,
15027c478bd9Sstevel@tonic-gate 			    fmd_strerror(EFMD_BUF_INVAL));
15037c478bd9Sstevel@tonic-gate 		}
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate 		if (mp->mod_stats->ms_buflimit.fmds_value.ui64 -
15067c478bd9Sstevel@tonic-gate 		    mp->mod_stats->ms_buftotal.fmds_value.ui64 < size) {
15077c478bd9Sstevel@tonic-gate 			fmd_api_error(mp, EFMD_BUF_LIMIT, "cannot write '%s': "
15087c478bd9Sstevel@tonic-gate 			    "buf limit exceeded (%llu)\n", name, (u_longlong_t)
15097c478bd9Sstevel@tonic-gate 			    mp->mod_stats->ms_buflimit.fmds_value.ui64);
15107c478bd9Sstevel@tonic-gate 		}
15117c478bd9Sstevel@tonic-gate 
15127c478bd9Sstevel@tonic-gate 		mp->mod_stats->ms_buftotal.fmds_value.ui64 += size;
15137c478bd9Sstevel@tonic-gate 		bp = fmd_buf_insert(bhp, name, size);
15147c478bd9Sstevel@tonic-gate 
15157c478bd9Sstevel@tonic-gate 	} else if (size > bp->buf_size) {
15167c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_BUF_OFLOW,
15177c478bd9Sstevel@tonic-gate 		    "write to buf '%s' overflows buf size (%lu > %lu)\n",
15187c478bd9Sstevel@tonic-gate 		    name, (ulong_t)size, (ulong_t)bp->buf_size);
15197c478bd9Sstevel@tonic-gate 	}
15207c478bd9Sstevel@tonic-gate 
15217c478bd9Sstevel@tonic-gate 	bcopy(buf, bp->buf_data, MIN(bp->buf_size, size));
15227c478bd9Sstevel@tonic-gate 	bp->buf_flags |= FMD_BUF_DIRTY;
15237c478bd9Sstevel@tonic-gate 
15247c478bd9Sstevel@tonic-gate 	if (cp != NULL)
15257c478bd9Sstevel@tonic-gate 		fmd_case_setdirty(cp);
15267c478bd9Sstevel@tonic-gate 	else
15277c478bd9Sstevel@tonic-gate 		fmd_module_setdirty(mp);
15287c478bd9Sstevel@tonic-gate 
15297c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
15307c478bd9Sstevel@tonic-gate }
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate size_t
15337c478bd9Sstevel@tonic-gate fmd_buf_size(fmd_hdl_t *hdl, fmd_case_t *cp, const char *name)
15347c478bd9Sstevel@tonic-gate {
15357c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
15367c478bd9Sstevel@tonic-gate 	fmd_buf_hash_t *bhp = fmd_buf_gethash(mp, cp);
15377c478bd9Sstevel@tonic-gate 
15387c478bd9Sstevel@tonic-gate 	fmd_buf_t *bp;
15397c478bd9Sstevel@tonic-gate 	size_t size;
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate 	if ((bp = fmd_buf_lookup(bhp, name)) != NULL)
15427c478bd9Sstevel@tonic-gate 		size = bp->buf_size;
15437c478bd9Sstevel@tonic-gate 	else
15447c478bd9Sstevel@tonic-gate 		size = 0;
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
15477c478bd9Sstevel@tonic-gate 	return (size);
15487c478bd9Sstevel@tonic-gate }
15497c478bd9Sstevel@tonic-gate 
15507c478bd9Sstevel@tonic-gate void
15517c478bd9Sstevel@tonic-gate fmd_serd_create(fmd_hdl_t *hdl, const char *name, uint_t n, hrtime_t t)
15527c478bd9Sstevel@tonic-gate {
15537c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
15547c478bd9Sstevel@tonic-gate 
15557c478bd9Sstevel@tonic-gate 	if (fmd_serd_eng_lookup(&mp->mod_serds, name) != NULL) {
15567c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_SERD_EXISTS,
15577c478bd9Sstevel@tonic-gate 		    "failed to create serd engine '%s': %s\n",
15587c478bd9Sstevel@tonic-gate 		    name, fmd_strerror(EFMD_SERD_EXISTS));
15597c478bd9Sstevel@tonic-gate 	}
15607c478bd9Sstevel@tonic-gate 
15617c478bd9Sstevel@tonic-gate 	(void) fmd_serd_eng_insert(&mp->mod_serds, name, n, t);
15627c478bd9Sstevel@tonic-gate 	fmd_module_setdirty(mp);
15637c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
15647c478bd9Sstevel@tonic-gate }
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate void
15677c478bd9Sstevel@tonic-gate fmd_serd_destroy(fmd_hdl_t *hdl, const char *name)
15687c478bd9Sstevel@tonic-gate {
15697c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
15707c478bd9Sstevel@tonic-gate 
15717c478bd9Sstevel@tonic-gate 	fmd_serd_eng_delete(&mp->mod_serds, name);
15727c478bd9Sstevel@tonic-gate 	fmd_module_setdirty(mp);
15737c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
15747c478bd9Sstevel@tonic-gate }
15757c478bd9Sstevel@tonic-gate 
15767c478bd9Sstevel@tonic-gate int
15777c478bd9Sstevel@tonic-gate fmd_serd_exists(fmd_hdl_t *hdl, const char *name)
15787c478bd9Sstevel@tonic-gate {
15797c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
15807c478bd9Sstevel@tonic-gate 	int rv = (fmd_serd_eng_lookup(&mp->mod_serds, name) != NULL);
15817c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
15827c478bd9Sstevel@tonic-gate 
15837c478bd9Sstevel@tonic-gate 	return (rv);
15847c478bd9Sstevel@tonic-gate }
15857c478bd9Sstevel@tonic-gate 
15867c478bd9Sstevel@tonic-gate void
15877c478bd9Sstevel@tonic-gate fmd_serd_reset(fmd_hdl_t *hdl, const char *name)
15887c478bd9Sstevel@tonic-gate {
15897c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
15907c478bd9Sstevel@tonic-gate 	fmd_serd_eng_t *sgp;
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate 	if ((sgp = fmd_serd_eng_lookup(&mp->mod_serds, name)) == NULL) {
15937c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_SERD_NAME,
15947c478bd9Sstevel@tonic-gate 		    "serd engine '%s' does not exist\n", name);
15957c478bd9Sstevel@tonic-gate 	}
15967c478bd9Sstevel@tonic-gate 
15977c478bd9Sstevel@tonic-gate 	fmd_serd_eng_reset(sgp);
15987c478bd9Sstevel@tonic-gate 	fmd_module_setdirty(mp);
15997c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
16007c478bd9Sstevel@tonic-gate }
16017c478bd9Sstevel@tonic-gate 
16027c478bd9Sstevel@tonic-gate int
16037c478bd9Sstevel@tonic-gate fmd_serd_record(fmd_hdl_t *hdl, const char *name, fmd_event_t *ep)
16047c478bd9Sstevel@tonic-gate {
16057c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
16067c478bd9Sstevel@tonic-gate 	fmd_serd_eng_t *sgp;
16077c478bd9Sstevel@tonic-gate 	int err;
16087c478bd9Sstevel@tonic-gate 
16097c478bd9Sstevel@tonic-gate 	if ((sgp = fmd_serd_eng_lookup(&mp->mod_serds, name)) == NULL) {
16107c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_SERD_NAME,
16117c478bd9Sstevel@tonic-gate 		    "failed to add record to serd engine '%s'", name);
16127c478bd9Sstevel@tonic-gate 	}
16137c478bd9Sstevel@tonic-gate 
16147c478bd9Sstevel@tonic-gate 	err = fmd_serd_eng_record(sgp, ep);
16157c478bd9Sstevel@tonic-gate 
16167c478bd9Sstevel@tonic-gate 	if (sgp->sg_flags & FMD_SERD_DIRTY)
16177c478bd9Sstevel@tonic-gate 		fmd_module_setdirty(mp);
16187c478bd9Sstevel@tonic-gate 
16197c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
16207c478bd9Sstevel@tonic-gate 	return (err);
16217c478bd9Sstevel@tonic-gate }
16227c478bd9Sstevel@tonic-gate 
16237c478bd9Sstevel@tonic-gate int
16247c478bd9Sstevel@tonic-gate fmd_serd_fired(fmd_hdl_t *hdl, const char *name)
16257c478bd9Sstevel@tonic-gate {
16267c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
16277c478bd9Sstevel@tonic-gate 	fmd_serd_eng_t *sgp;
16287c478bd9Sstevel@tonic-gate 	int err;
16297c478bd9Sstevel@tonic-gate 
16307c478bd9Sstevel@tonic-gate 	if ((sgp = fmd_serd_eng_lookup(&mp->mod_serds, name)) == NULL) {
16317c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_SERD_NAME,
16327c478bd9Sstevel@tonic-gate 		    "serd engine '%s' does not exist\n", name);
16337c478bd9Sstevel@tonic-gate 	}
16347c478bd9Sstevel@tonic-gate 
16357c478bd9Sstevel@tonic-gate 	err = fmd_serd_eng_fired(sgp);
16367c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
16377c478bd9Sstevel@tonic-gate 	return (err);
16387c478bd9Sstevel@tonic-gate }
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate int
16417c478bd9Sstevel@tonic-gate fmd_serd_empty(fmd_hdl_t *hdl, const char *name)
16427c478bd9Sstevel@tonic-gate {
16437c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
16447c478bd9Sstevel@tonic-gate 	fmd_serd_eng_t *sgp;
16457c478bd9Sstevel@tonic-gate 	int empty;
16467c478bd9Sstevel@tonic-gate 
16477c478bd9Sstevel@tonic-gate 	if ((sgp = fmd_serd_eng_lookup(&mp->mod_serds, name)) == NULL) {
16487c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_SERD_NAME,
16497c478bd9Sstevel@tonic-gate 		    "serd engine '%s' does not exist\n", name);
16507c478bd9Sstevel@tonic-gate 	}
16517c478bd9Sstevel@tonic-gate 
16527c478bd9Sstevel@tonic-gate 	empty = fmd_serd_eng_empty(sgp);
16537c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
16547c478bd9Sstevel@tonic-gate 	return (empty);
16557c478bd9Sstevel@tonic-gate }
16567c478bd9Sstevel@tonic-gate 
16577c478bd9Sstevel@tonic-gate pthread_t
16587c478bd9Sstevel@tonic-gate fmd_thr_create(fmd_hdl_t *hdl, void (*func)(void *), void *arg)
16597c478bd9Sstevel@tonic-gate {
16607c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
16617c478bd9Sstevel@tonic-gate 	fmd_thread_t *tp;
16627c478bd9Sstevel@tonic-gate 	pthread_t tid;
16637c478bd9Sstevel@tonic-gate 
16647c478bd9Sstevel@tonic-gate 	if (mp->mod_stats->ms_thrtotal.fmds_value.ui32 >=
16657c478bd9Sstevel@tonic-gate 	    mp->mod_stats->ms_thrlimit.fmds_value.ui32) {
16667c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_THR_LIMIT, "%s request to create an "
16677c478bd9Sstevel@tonic-gate 		    "auxiliary thread exceeds module thread limit (%u)\n",
16687c478bd9Sstevel@tonic-gate 		    mp->mod_name, mp->mod_stats->ms_thrlimit.fmds_value.ui32);
16697c478bd9Sstevel@tonic-gate 	}
16707c478bd9Sstevel@tonic-gate 
16717c478bd9Sstevel@tonic-gate 	if ((tp = fmd_thread_create(mp, func, arg)) == NULL) {
16727c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_THR_CREATE,
16737c478bd9Sstevel@tonic-gate 		    "failed to create auxiliary thread");
16747c478bd9Sstevel@tonic-gate 	}
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate 	tid = tp->thr_tid;
16777c478bd9Sstevel@tonic-gate 	mp->mod_stats->ms_thrtotal.fmds_value.ui32++;
16787c478bd9Sstevel@tonic-gate 	(void) fmd_idspace_xalloc(mp->mod_threads, tid, tp);
16797c478bd9Sstevel@tonic-gate 
16807c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
16817c478bd9Sstevel@tonic-gate 	return (tid);
16827c478bd9Sstevel@tonic-gate }
16837c478bd9Sstevel@tonic-gate 
16847c478bd9Sstevel@tonic-gate void
16857c478bd9Sstevel@tonic-gate fmd_thr_destroy(fmd_hdl_t *hdl, pthread_t tid)
16867c478bd9Sstevel@tonic-gate {
16877c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
16887c478bd9Sstevel@tonic-gate 	fmd_thread_t *tp;
16897c478bd9Sstevel@tonic-gate 	int err;
16907c478bd9Sstevel@tonic-gate 
16917c478bd9Sstevel@tonic-gate 	if (pthread_self() == tid) {
16927c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_THR_INVAL, "auxiliary thread tried to "
16937c478bd9Sstevel@tonic-gate 		    "destroy itself (tid %u)\n", tid);
16947c478bd9Sstevel@tonic-gate 	}
16957c478bd9Sstevel@tonic-gate 
16967c478bd9Sstevel@tonic-gate 	if ((tp = fmd_idspace_getspecific(mp->mod_threads, tid)) == NULL) {
16977c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_THR_INVAL, "auxiliary thread tried to "
16987c478bd9Sstevel@tonic-gate 		    "destroy an invalid thread (tid %u)\n", tid);
16997c478bd9Sstevel@tonic-gate 	}
17007c478bd9Sstevel@tonic-gate 
17017c478bd9Sstevel@tonic-gate 	/*
17027c478bd9Sstevel@tonic-gate 	 * Wait for the specified thread to exit and then join with it.  Since
17037c478bd9Sstevel@tonic-gate 	 * the thread may need to make API calls in order to complete its work
17047c478bd9Sstevel@tonic-gate 	 * we must sleep with the module lock unheld, and then reacquire it.
17057c478bd9Sstevel@tonic-gate 	 */
17067c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
17077c478bd9Sstevel@tonic-gate 	err = pthread_join(tid, NULL);
17087c478bd9Sstevel@tonic-gate 	mp = fmd_api_module_lock(hdl);
17097c478bd9Sstevel@tonic-gate 
17107c478bd9Sstevel@tonic-gate 	/*
17117c478bd9Sstevel@tonic-gate 	 * Since pthread_join() was called without the module lock held, if
17127c478bd9Sstevel@tonic-gate 	 * multiple callers attempted to destroy the same auxiliary thread
17137c478bd9Sstevel@tonic-gate 	 * simultaneously, one will succeed and the others will get ESRCH.
17147c478bd9Sstevel@tonic-gate 	 * Therefore we silently ignore ESRCH but only allow the caller who
17157c478bd9Sstevel@tonic-gate 	 * succeessfully joined with the auxiliary thread to destroy it.
17167c478bd9Sstevel@tonic-gate 	 */
17177c478bd9Sstevel@tonic-gate 	if (err != 0 && err != ESRCH) {
17187c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_THR_JOIN,
17197c478bd9Sstevel@tonic-gate 		    "failed to join with auxiliary thread %u\n", tid);
17207c478bd9Sstevel@tonic-gate 	}
17217c478bd9Sstevel@tonic-gate 
17227c478bd9Sstevel@tonic-gate 	if (err == 0) {
17237c478bd9Sstevel@tonic-gate 		fmd_thread_destroy(tp, FMD_THREAD_NOJOIN);
17247c478bd9Sstevel@tonic-gate 		mp->mod_stats->ms_thrtotal.fmds_value.ui32--;
17257c478bd9Sstevel@tonic-gate 		(void) fmd_idspace_free(mp->mod_threads, tid);
17267c478bd9Sstevel@tonic-gate 	}
17277c478bd9Sstevel@tonic-gate 
17287c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
17297c478bd9Sstevel@tonic-gate }
17307c478bd9Sstevel@tonic-gate 
17317c478bd9Sstevel@tonic-gate void
17327c478bd9Sstevel@tonic-gate fmd_thr_signal(fmd_hdl_t *hdl, pthread_t tid)
17337c478bd9Sstevel@tonic-gate {
17347c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
17357c478bd9Sstevel@tonic-gate 
17367c478bd9Sstevel@tonic-gate 	if (tid != mp->mod_thread->thr_tid &&
17377c478bd9Sstevel@tonic-gate 	    fmd_idspace_getspecific(mp->mod_threads, tid) == NULL) {
17387c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_THR_INVAL, "tid %u is not a valid "
17397c478bd9Sstevel@tonic-gate 		    "thread id for module %s\n", tid, mp->mod_name);
17407c478bd9Sstevel@tonic-gate 	}
17417c478bd9Sstevel@tonic-gate 
17427c478bd9Sstevel@tonic-gate 	(void) pthread_kill(tid, fmd.d_thr_sig);
17437c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
17447c478bd9Sstevel@tonic-gate }
17457c478bd9Sstevel@tonic-gate 
174625351652SVuong Nguyen void
174725351652SVuong Nguyen fmd_thr_checkpoint(fmd_hdl_t *hdl)
174825351652SVuong Nguyen {
174925351652SVuong Nguyen 	fmd_module_t *mp = fmd_api_module_lock(hdl);
175025351652SVuong Nguyen 	pthread_t tid = pthread_self();
175125351652SVuong Nguyen 
175225351652SVuong Nguyen 	if (tid == mp->mod_thread->thr_tid ||
175325351652SVuong Nguyen 	    fmd_idspace_getspecific(mp->mod_threads, tid) == NULL) {
175425351652SVuong Nguyen 		fmd_api_error(mp, EFMD_THR_INVAL, "tid %u is not a valid "
175525351652SVuong Nguyen 		    "auxiliary thread id for module %s\n", tid, mp->mod_name);
175625351652SVuong Nguyen 	}
175725351652SVuong Nguyen 
175825351652SVuong Nguyen 	fmd_ckpt_save(mp);
175925351652SVuong Nguyen 
176025351652SVuong Nguyen 	fmd_module_unlock(mp);
176125351652SVuong Nguyen }
176225351652SVuong Nguyen 
17637c478bd9Sstevel@tonic-gate id_t
17647c478bd9Sstevel@tonic-gate fmd_timer_install(fmd_hdl_t *hdl, void *arg, fmd_event_t *ep, hrtime_t delta)
17657c478bd9Sstevel@tonic-gate {
17667c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
17677c478bd9Sstevel@tonic-gate 	fmd_modtimer_t *t;
17687c478bd9Sstevel@tonic-gate 	id_t id;
17697c478bd9Sstevel@tonic-gate 
17707c478bd9Sstevel@tonic-gate 	if (delta < 0) {
17717c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_TIMER_INVAL,
17727c478bd9Sstevel@tonic-gate 		    "timer delta %lld is not a valid interval\n", delta);
17737c478bd9Sstevel@tonic-gate 	}
17747c478bd9Sstevel@tonic-gate 
17757c478bd9Sstevel@tonic-gate 	t = fmd_alloc(sizeof (fmd_modtimer_t), FMD_SLEEP);
17767c478bd9Sstevel@tonic-gate 	t->mt_mod = mp;
17777c478bd9Sstevel@tonic-gate 	t->mt_arg = arg;
17787c478bd9Sstevel@tonic-gate 	t->mt_id = -1;
17797c478bd9Sstevel@tonic-gate 
17807c478bd9Sstevel@tonic-gate 	if ((id = fmd_timerq_install(fmd.d_timers, mp->mod_timerids,
17817c478bd9Sstevel@tonic-gate 	    (fmd_timer_f *)fmd_module_timeout, t, ep, delta)) == -1) {
17827c478bd9Sstevel@tonic-gate 		fmd_free(t, sizeof (fmd_modtimer_t));
17837c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_TIMER_LIMIT,
17847c478bd9Sstevel@tonic-gate 		    "failed to install timer +%lld", delta);
17857c478bd9Sstevel@tonic-gate 	}
17867c478bd9Sstevel@tonic-gate 
17877c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
17887c478bd9Sstevel@tonic-gate 	return (id);
17897c478bd9Sstevel@tonic-gate }
17907c478bd9Sstevel@tonic-gate 
17917c478bd9Sstevel@tonic-gate void
17927c478bd9Sstevel@tonic-gate fmd_timer_remove(fmd_hdl_t *hdl, id_t id)
17937c478bd9Sstevel@tonic-gate {
17947c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
17957c478bd9Sstevel@tonic-gate 	fmd_modtimer_t *t;
17967c478bd9Sstevel@tonic-gate 
17977c478bd9Sstevel@tonic-gate 	if (!fmd_idspace_valid(mp->mod_timerids, id)) {
17987c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_TIMER_INVAL,
17997c478bd9Sstevel@tonic-gate 		    "id %ld is not a valid timer id\n", id);
18007c478bd9Sstevel@tonic-gate 	}
18017c478bd9Sstevel@tonic-gate 
18029dd0f810Scindi 	/*
18039dd0f810Scindi 	 * If the timer has not fired (t != NULL), remove it from the timer
18049dd0f810Scindi 	 * queue.  If the timer has fired (t == NULL), we could be in one of
18059dd0f810Scindi 	 * two situations: a) we are processing the timer callback or b)
18069dd0f810Scindi 	 * the timer event is on the module queue awaiting dispatch.  For a),
18079dd0f810Scindi 	 * fmd_timerq_remove() will wait for the timer callback function
18089dd0f810Scindi 	 * to complete and queue an event for dispatch.  For a) and b),
18099dd0f810Scindi 	 * we cancel the outstanding timer event from the module's dispatch
18109dd0f810Scindi 	 * queue.
18119dd0f810Scindi 	 */
18129dd0f810Scindi 	if ((t = fmd_timerq_remove(fmd.d_timers, mp->mod_timerids, id)) != NULL)
18139dd0f810Scindi 		fmd_free(t, sizeof (fmd_modtimer_t));
18147c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
18157c478bd9Sstevel@tonic-gate 
18169dd0f810Scindi 	fmd_eventq_cancel(mp->mod_queue, FMD_EVT_TIMEOUT, (void *)id);
18177c478bd9Sstevel@tonic-gate }
18187c478bd9Sstevel@tonic-gate 
18197c478bd9Sstevel@tonic-gate nvlist_t *
18207c478bd9Sstevel@tonic-gate fmd_nvl_create_fault(fmd_hdl_t *hdl, const char *class,
18217c478bd9Sstevel@tonic-gate     uint8_t certainty, nvlist_t *asru, nvlist_t *fru, nvlist_t *rsrc)
18227c478bd9Sstevel@tonic-gate {
18239dd0f810Scindi 	fmd_module_t *mp;
18247c478bd9Sstevel@tonic-gate 	nvlist_t *nvl;
18257c478bd9Sstevel@tonic-gate 
18269dd0f810Scindi 	mp = fmd_api_module_lock(hdl);
18277c478bd9Sstevel@tonic-gate 	if (class == NULL || class[0] == '\0')
18287c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_NVL_INVAL, "invalid fault class\n");
18297c478bd9Sstevel@tonic-gate 
1830b7d3956bSstephh 	nvl = fmd_protocol_fault(class, certainty, asru, fru, rsrc, NULL);
183124db4641Seschrock 
183224db4641Seschrock 	fmd_module_unlock(mp);
183324db4641Seschrock 
18347c478bd9Sstevel@tonic-gate 	return (nvl);
18357c478bd9Sstevel@tonic-gate }
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate int
18387c478bd9Sstevel@tonic-gate fmd_nvl_class_match(fmd_hdl_t *hdl, nvlist_t *nvl, const char *pattern)
18397c478bd9Sstevel@tonic-gate {
18407c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
18417c478bd9Sstevel@tonic-gate 	char *class;
18427c478bd9Sstevel@tonic-gate 	int rv;
18437c478bd9Sstevel@tonic-gate 
18447c478bd9Sstevel@tonic-gate 	rv = (nvl != NULL && nvlist_lookup_string(nvl,
18457c478bd9Sstevel@tonic-gate 	    FM_CLASS, &class) == 0 && fmd_strmatch(class, pattern));
18467c478bd9Sstevel@tonic-gate 
18477c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
18487c478bd9Sstevel@tonic-gate 	return (rv);
18497c478bd9Sstevel@tonic-gate }
18507c478bd9Sstevel@tonic-gate 
18517c478bd9Sstevel@tonic-gate int
18527c478bd9Sstevel@tonic-gate fmd_nvl_fmri_expand(fmd_hdl_t *hdl, nvlist_t *nvl)
18537c478bd9Sstevel@tonic-gate {
18547c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
18557c478bd9Sstevel@tonic-gate 	int rv;
18567c478bd9Sstevel@tonic-gate 
18577c478bd9Sstevel@tonic-gate 	if (nvl == NULL) {
18587c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_NVL_INVAL,
18597c478bd9Sstevel@tonic-gate 		    "invalid nvlist %p\n", (void *)nvl);
18607c478bd9Sstevel@tonic-gate 	}
18617c478bd9Sstevel@tonic-gate 
18627c478bd9Sstevel@tonic-gate 	rv = fmd_fmri_expand(nvl);
18637c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
18647c478bd9Sstevel@tonic-gate 	return (rv);
18657c478bd9Sstevel@tonic-gate }
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate int
18687c478bd9Sstevel@tonic-gate fmd_nvl_fmri_present(fmd_hdl_t *hdl, nvlist_t *nvl)
18697c478bd9Sstevel@tonic-gate {
18707c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
18717c478bd9Sstevel@tonic-gate 	int rv;
18727c478bd9Sstevel@tonic-gate 
18737c478bd9Sstevel@tonic-gate 	if (nvl == NULL) {
18747c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_NVL_INVAL,
18757c478bd9Sstevel@tonic-gate 		    "invalid nvlist %p\n", (void *)nvl);
18767c478bd9Sstevel@tonic-gate 	}
18777c478bd9Sstevel@tonic-gate 
18787c478bd9Sstevel@tonic-gate 	rv = fmd_fmri_present(nvl);
18797c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
18807c478bd9Sstevel@tonic-gate 
18817c478bd9Sstevel@tonic-gate 	if (rv < 0) {
18827c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_FMRI_OP, "invalid fmri for "
18837c478bd9Sstevel@tonic-gate 		    "fmd_nvl_fmri_present\n");
18847c478bd9Sstevel@tonic-gate 	}
18857c478bd9Sstevel@tonic-gate 
18867c478bd9Sstevel@tonic-gate 	return (rv);
18877c478bd9Sstevel@tonic-gate }
18887c478bd9Sstevel@tonic-gate 
18897c478bd9Sstevel@tonic-gate int
189025c6ff4bSstephh fmd_nvl_fmri_replaced(fmd_hdl_t *hdl, nvlist_t *nvl)
189125c6ff4bSstephh {
189225c6ff4bSstephh 	fmd_module_t *mp = fmd_api_module_lock(hdl);
189325c6ff4bSstephh 	int rv;
189425c6ff4bSstephh 
189525c6ff4bSstephh 	if (nvl == NULL) {
189625c6ff4bSstephh 		fmd_api_error(mp, EFMD_NVL_INVAL,
189725c6ff4bSstephh 		    "invalid nvlist %p\n", (void *)nvl);
189825c6ff4bSstephh 	}
189925c6ff4bSstephh 
190025c6ff4bSstephh 	rv = fmd_fmri_replaced(nvl);
190125c6ff4bSstephh 	fmd_module_unlock(mp);
190225c6ff4bSstephh 
190325c6ff4bSstephh 	return (rv);
190425c6ff4bSstephh }
190525c6ff4bSstephh 
190625c6ff4bSstephh int
19077c478bd9Sstevel@tonic-gate fmd_nvl_fmri_unusable(fmd_hdl_t *hdl, nvlist_t *nvl)
19087c478bd9Sstevel@tonic-gate {
19097c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
19107c478bd9Sstevel@tonic-gate 	int rv;
19117c478bd9Sstevel@tonic-gate 
19127c478bd9Sstevel@tonic-gate 	if (nvl == NULL) {
19137c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_NVL_INVAL,
19147c478bd9Sstevel@tonic-gate 		    "invalid nvlist %p\n", (void *)nvl);
19157c478bd9Sstevel@tonic-gate 	}
19167c478bd9Sstevel@tonic-gate 
19177c478bd9Sstevel@tonic-gate 	rv = fmd_fmri_unusable(nvl);
19187c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
19197c478bd9Sstevel@tonic-gate 
19207c478bd9Sstevel@tonic-gate 	if (rv < 0) {
19217c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_FMRI_OP, "invalid fmri for "
19227c478bd9Sstevel@tonic-gate 		    "fmd_nvl_fmri_unusable\n");
19237c478bd9Sstevel@tonic-gate 	}
19247c478bd9Sstevel@tonic-gate 
19257c478bd9Sstevel@tonic-gate 	return (rv);
19267c478bd9Sstevel@tonic-gate }
19277c478bd9Sstevel@tonic-gate 
19287c478bd9Sstevel@tonic-gate int
1929e4b86885SCheng Sean Ye fmd_nvl_fmri_retire(fmd_hdl_t *hdl, nvlist_t *nvl)
1930e4b86885SCheng Sean Ye {
1931e4b86885SCheng Sean Ye 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1932e4b86885SCheng Sean Ye 	int rv;
1933e4b86885SCheng Sean Ye 
1934e4b86885SCheng Sean Ye 	if (nvl == NULL) {
1935e4b86885SCheng Sean Ye 		fmd_api_error(mp, EFMD_NVL_INVAL,
1936e4b86885SCheng Sean Ye 		    "invalid nvlist %p\n", (void *)nvl);
1937e4b86885SCheng Sean Ye 	}
1938e4b86885SCheng Sean Ye 
1939e4b86885SCheng Sean Ye 	rv = fmd_fmri_retire(nvl);
1940e4b86885SCheng Sean Ye 	fmd_module_unlock(mp);
1941e4b86885SCheng Sean Ye 
1942e4b86885SCheng Sean Ye 	return (rv);
1943e4b86885SCheng Sean Ye }
1944e4b86885SCheng Sean Ye 
1945e4b86885SCheng Sean Ye int
1946e4b86885SCheng Sean Ye fmd_nvl_fmri_unretire(fmd_hdl_t *hdl, nvlist_t *nvl)
1947e4b86885SCheng Sean Ye {
1948e4b86885SCheng Sean Ye 	fmd_module_t *mp = fmd_api_module_lock(hdl);
1949e4b86885SCheng Sean Ye 	int rv;
1950e4b86885SCheng Sean Ye 
1951e4b86885SCheng Sean Ye 	if (nvl == NULL) {
1952e4b86885SCheng Sean Ye 		fmd_api_error(mp, EFMD_NVL_INVAL,
1953e4b86885SCheng Sean Ye 		    "invalid nvlist %p\n", (void *)nvl);
1954e4b86885SCheng Sean Ye 	}
1955e4b86885SCheng Sean Ye 
1956e4b86885SCheng Sean Ye 	rv = fmd_fmri_unretire(nvl);
1957e4b86885SCheng Sean Ye 	fmd_module_unlock(mp);
1958e4b86885SCheng Sean Ye 
1959e4b86885SCheng Sean Ye 	return (rv);
1960e4b86885SCheng Sean Ye }
1961e4b86885SCheng Sean Ye 
1962e4b86885SCheng Sean Ye int
196325c6ff4bSstephh fmd_nvl_fmri_service_state(fmd_hdl_t *hdl, nvlist_t *nvl)
19647aec1d6eScindi {
19657aec1d6eScindi 	fmd_module_t *mp = fmd_api_module_lock(hdl);
196625c6ff4bSstephh 	int rv;
19677aec1d6eScindi 
19687aec1d6eScindi 	if (nvl == NULL) {
19697aec1d6eScindi 		fmd_api_error(mp, EFMD_NVL_INVAL,
19707aec1d6eScindi 		    "invalid nvlist %p\n", (void *)nvl);
19717aec1d6eScindi 	}
19727aec1d6eScindi 
197325c6ff4bSstephh 	rv = fmd_fmri_service_state(nvl);
197425c6ff4bSstephh 	if (rv < 0)
197525c6ff4bSstephh 		rv = fmd_fmri_unusable(nvl) ? FMD_SERVICE_STATE_UNUSABLE :
197625c6ff4bSstephh 		    FMD_SERVICE_STATE_OK;
197725c6ff4bSstephh 	fmd_module_unlock(mp);
197825c6ff4bSstephh 
197925c6ff4bSstephh 	if (rv < 0) {
198025c6ff4bSstephh 		fmd_api_error(mp, EFMD_FMRI_OP, "invalid fmri for "
198125c6ff4bSstephh 		    "fmd_nvl_fmri_service_state\n");
19827aec1d6eScindi 	}
19837aec1d6eScindi 
198425c6ff4bSstephh 	return (rv);
198525c6ff4bSstephh }
198625c6ff4bSstephh 
198725c6ff4bSstephh typedef struct {
198825c6ff4bSstephh 	const char	*class;
198925c6ff4bSstephh 	int	*rvp;
199025c6ff4bSstephh } fmd_has_fault_arg_t;
199125c6ff4bSstephh 
199225c6ff4bSstephh static void
199325c6ff4bSstephh fmd_rsrc_has_fault(fmd_asru_link_t *alp, void *arg)
199425c6ff4bSstephh {
199525c6ff4bSstephh 	fmd_has_fault_arg_t *fhfp = (fmd_has_fault_arg_t *)arg;
199625c6ff4bSstephh 	char *class;
199725c6ff4bSstephh 
199825c6ff4bSstephh 	if (fhfp->class == NULL) {
199925c6ff4bSstephh 		if (alp->al_flags & FMD_ASRU_FAULTY)
200025c6ff4bSstephh 			*fhfp->rvp = 1;
200125c6ff4bSstephh 	} else {
200225c6ff4bSstephh 		if ((alp->al_flags & FMD_ASRU_FAULTY) &&
200325c6ff4bSstephh 		    alp->al_event != NULL && nvlist_lookup_string(alp->al_event,
200425c6ff4bSstephh 		    FM_CLASS, &class) == 0 && fmd_strmatch(class, fhfp->class))
200525c6ff4bSstephh 			*fhfp->rvp = 1;
200625c6ff4bSstephh 	}
200725c6ff4bSstephh }
200825c6ff4bSstephh 
200925c6ff4bSstephh int
201025c6ff4bSstephh fmd_nvl_fmri_has_fault(fmd_hdl_t *hdl, nvlist_t *nvl, int type, char *class)
201125c6ff4bSstephh {
201225c6ff4bSstephh 	fmd_module_t *mp = fmd_api_module_lock(hdl);
201325c6ff4bSstephh 	fmd_asru_hash_t *ahp = fmd.d_asrus;
201425c6ff4bSstephh 	int rv = 0;
201525c6ff4bSstephh 	char *name;
201625c6ff4bSstephh 	int namelen;
201725c6ff4bSstephh 	fmd_has_fault_arg_t fhf;
201825c6ff4bSstephh 
201925c6ff4bSstephh 	if (nvl == NULL) {
202025c6ff4bSstephh 		fmd_api_error(mp, EFMD_NVL_INVAL,
202125c6ff4bSstephh 		    "invalid nvlist %p\n", (void *)nvl);
202225c6ff4bSstephh 	}
202325c6ff4bSstephh 	if ((namelen = fmd_fmri_nvl2str(nvl, NULL, 0)) == -1)
202425c6ff4bSstephh 		fmd_api_error(mp, EFMD_NVL_INVAL,
202525c6ff4bSstephh 		    "invalid nvlist: %p\n", (void *)nvl);
202625c6ff4bSstephh 	name = fmd_alloc(namelen + 1, FMD_SLEEP);
202725c6ff4bSstephh 	if (fmd_fmri_nvl2str(nvl, name, namelen + 1) == -1) {
202825c6ff4bSstephh 		if (name != NULL)
202925c6ff4bSstephh 			fmd_free(name, namelen + 1);
203025c6ff4bSstephh 		fmd_api_error(mp, EFMD_NVL_INVAL,
203125c6ff4bSstephh 		    "invalid nvlist: %p\n", (void *)nvl);
203225c6ff4bSstephh 	}
203325c6ff4bSstephh 
203425c6ff4bSstephh 	fhf.class = class;
203525c6ff4bSstephh 	fhf.rvp = &rv;
203625c6ff4bSstephh 	if (type == FMD_HAS_FAULT_RESOURCE)
203725c6ff4bSstephh 		fmd_asru_hash_apply_by_rsrc(ahp, name, fmd_rsrc_has_fault,
203825c6ff4bSstephh 		    &fhf);
203925c6ff4bSstephh 	else if (type == FMD_HAS_FAULT_ASRU)
204025c6ff4bSstephh 		fmd_asru_hash_apply_by_asru(ahp, name, fmd_rsrc_has_fault,
204125c6ff4bSstephh 		    &fhf);
204225c6ff4bSstephh 	else if (type == FMD_HAS_FAULT_FRU)
204325c6ff4bSstephh 		fmd_asru_hash_apply_by_fru(ahp, name, fmd_rsrc_has_fault,
204425c6ff4bSstephh 		    &fhf);
204525c6ff4bSstephh 
204625c6ff4bSstephh 	if (name != NULL)
204725c6ff4bSstephh 		fmd_free(name, namelen + 1);
20487aec1d6eScindi 	fmd_module_unlock(mp);
20497aec1d6eScindi 	return (rv);
20507aec1d6eScindi }
20517aec1d6eScindi 
20527aec1d6eScindi int
20537c478bd9Sstevel@tonic-gate fmd_nvl_fmri_contains(fmd_hdl_t *hdl, nvlist_t *n1, nvlist_t *n2)
20547c478bd9Sstevel@tonic-gate {
20557c478bd9Sstevel@tonic-gate 	fmd_module_t *mp = fmd_api_module_lock(hdl);
20567c478bd9Sstevel@tonic-gate 	int rv;
20577c478bd9Sstevel@tonic-gate 
20587c478bd9Sstevel@tonic-gate 	if (n1 == NULL || n2 == NULL) {
20597c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_NVL_INVAL,
20607c478bd9Sstevel@tonic-gate 		    "invalid nvlist(s): %p, %p\n", (void *)n1, (void *)n2);
20617c478bd9Sstevel@tonic-gate 	}
20627c478bd9Sstevel@tonic-gate 
20637c478bd9Sstevel@tonic-gate 	rv = fmd_fmri_contains(n1, n2);
20647c478bd9Sstevel@tonic-gate 	fmd_module_unlock(mp);
20657c478bd9Sstevel@tonic-gate 
20667c478bd9Sstevel@tonic-gate 	if (rv < 0) {
20677c478bd9Sstevel@tonic-gate 		fmd_api_error(mp, EFMD_FMRI_OP, "invalid fmri for "
20687c478bd9Sstevel@tonic-gate 		    "fmd_nvl_fmri_contains\n");
20697c478bd9Sstevel@tonic-gate 	}
20707c478bd9Sstevel@tonic-gate 
20717c478bd9Sstevel@tonic-gate 	return (rv);
20727c478bd9Sstevel@tonic-gate }
2073d9638e54Smws 
2074d9638e54Smws nvlist_t *
2075d9638e54Smws fmd_nvl_fmri_translate(fmd_hdl_t *hdl, nvlist_t *fmri, nvlist_t *auth)
2076d9638e54Smws {
2077d9638e54Smws 	fmd_module_t *mp = fmd_api_module_lock(hdl);
2078d9638e54Smws 	nvlist_t *xfmri;
2079d9638e54Smws 
2080d9638e54Smws 	if (fmri == NULL || auth == NULL) {
2081d9638e54Smws 		fmd_api_error(mp, EFMD_NVL_INVAL,
2082d9638e54Smws 		    "invalid nvlist(s): %p, %p\n", (void *)fmri, (void *)auth);
2083d9638e54Smws 	}
2084d9638e54Smws 
2085d9638e54Smws 	xfmri = fmd_fmri_translate(fmri, auth);
2086d9638e54Smws 	fmd_module_unlock(mp);
2087d9638e54Smws 	return (xfmri);
2088d9638e54Smws }
2089d9638e54Smws 
20909af3851aSeschrock static int
20919af3851aSeschrock fmd_nvl_op_init(nv_alloc_t *ops, va_list ap)
20929af3851aSeschrock {
20939af3851aSeschrock 	fmd_module_t *mp = va_arg(ap, fmd_module_t *);
20949af3851aSeschrock 
20959af3851aSeschrock 	ops->nva_arg = mp;
20969af3851aSeschrock 
20979af3851aSeschrock 	return (0);
20989af3851aSeschrock }
20999af3851aSeschrock 
21009af3851aSeschrock static void *
21019af3851aSeschrock fmd_nvl_op_alloc_sleep(nv_alloc_t *ops, size_t size)
21029af3851aSeschrock {
21039af3851aSeschrock 	fmd_module_t *mp = ops->nva_arg;
21049af3851aSeschrock 
21059af3851aSeschrock 	return (fmd_hdl_alloc_locked(mp, size, FMD_SLEEP));
21069af3851aSeschrock }
21079af3851aSeschrock 
21089af3851aSeschrock static void *
21099af3851aSeschrock fmd_nvl_op_alloc_nosleep(nv_alloc_t *ops, size_t size)
21109af3851aSeschrock {
21119af3851aSeschrock 	fmd_module_t *mp = ops->nva_arg;
21129af3851aSeschrock 
21139af3851aSeschrock 	return (fmd_hdl_alloc_locked(mp, size, FMD_NOSLEEP));
21149af3851aSeschrock }
21159af3851aSeschrock 
21169af3851aSeschrock static void
21179af3851aSeschrock fmd_nvl_op_free(nv_alloc_t *ops, void *data, size_t size)
21189af3851aSeschrock {
21199af3851aSeschrock 	fmd_module_t *mp = ops->nva_arg;
21209af3851aSeschrock 
21219af3851aSeschrock 	fmd_hdl_free_locked(mp, data, size);
21229af3851aSeschrock }
21239af3851aSeschrock 
21249af3851aSeschrock nv_alloc_ops_t fmd_module_nva_ops_sleep = {
21259af3851aSeschrock 	fmd_nvl_op_init,
21269af3851aSeschrock 	NULL,
21279af3851aSeschrock 	fmd_nvl_op_alloc_sleep,
21289af3851aSeschrock 	fmd_nvl_op_free,
21299af3851aSeschrock 	NULL
21309af3851aSeschrock };
21319af3851aSeschrock 
21329af3851aSeschrock nv_alloc_ops_t fmd_module_nva_ops_nosleep = {
21339af3851aSeschrock 	fmd_nvl_op_init,
21349af3851aSeschrock 	NULL,
21359af3851aSeschrock 	fmd_nvl_op_alloc_nosleep,
21369af3851aSeschrock 	fmd_nvl_op_free,
21379af3851aSeschrock 	NULL
21389af3851aSeschrock };
21399af3851aSeschrock 
21409af3851aSeschrock nvlist_t *
21419af3851aSeschrock fmd_nvl_alloc(fmd_hdl_t *hdl, int flags)
21429af3851aSeschrock {
21439af3851aSeschrock 	fmd_module_t *mp = fmd_api_module_lock(hdl);
21449af3851aSeschrock 	nv_alloc_t *nva;
21459af3851aSeschrock 	nvlist_t *nvl;
21469af3851aSeschrock 	int ret;
21479af3851aSeschrock 
21489af3851aSeschrock 	if (flags == FMD_SLEEP)
21499af3851aSeschrock 		nva = &mp->mod_nva_sleep;
21509af3851aSeschrock 	else
21519af3851aSeschrock 		nva = &mp->mod_nva_nosleep;
21529af3851aSeschrock 
21539af3851aSeschrock 	ret = nvlist_xalloc(&nvl, NV_UNIQUE_NAME, nva);
21549af3851aSeschrock 
21559af3851aSeschrock 	fmd_module_unlock(mp);
21569af3851aSeschrock 
21579af3851aSeschrock 	if (ret != 0)
21589af3851aSeschrock 		return (NULL);
21599af3851aSeschrock 	else
21609af3851aSeschrock 		return (nvl);
21619af3851aSeschrock }
21629af3851aSeschrock 
21639af3851aSeschrock nvlist_t *
21649af3851aSeschrock fmd_nvl_dup(fmd_hdl_t *hdl, nvlist_t *src, int flags)
21659af3851aSeschrock {
21669af3851aSeschrock 	fmd_module_t *mp = fmd_api_module_lock(hdl);
21679af3851aSeschrock 	nv_alloc_t *nva;
21689af3851aSeschrock 	nvlist_t *nvl;
21699af3851aSeschrock 	int ret;
21709af3851aSeschrock 
21719af3851aSeschrock 	if (flags == FMD_SLEEP)
21729af3851aSeschrock 		nva = &mp->mod_nva_sleep;
21739af3851aSeschrock 	else
21749af3851aSeschrock 		nva = &mp->mod_nva_nosleep;
21759af3851aSeschrock 
21769af3851aSeschrock 	ret = nvlist_xdup(src, &nvl, nva);
21779af3851aSeschrock 
21789af3851aSeschrock 	fmd_module_unlock(mp);
21799af3851aSeschrock 
21809af3851aSeschrock 	if (ret != 0)
21819af3851aSeschrock 		return (NULL);
21829af3851aSeschrock 	else
21839af3851aSeschrock 		return (nvl);
21849af3851aSeschrock }
21859af3851aSeschrock 
2186e5dcf7beSRobert Johnston /*ARGSUSED*/
2187e5dcf7beSRobert Johnston void
2188e5dcf7beSRobert Johnston fmd_repair_fru(fmd_hdl_t *hdl, const char *fmri)
2189e5dcf7beSRobert Johnston {
2190e5dcf7beSRobert Johnston 	int err;
21916e1fa242SStephen Hanson 	fmd_asru_rep_arg_t fara;
2192e5dcf7beSRobert Johnston 
21936e1fa242SStephen Hanson 	fara.fara_reason = FMD_ASRU_REPAIRED;
21946e1fa242SStephen Hanson 	fara.fara_bywhat = FARA_BY_FRU;
21956e1fa242SStephen Hanson 	fara.fara_rval = &err;
2196e5dcf7beSRobert Johnston 	fmd_asru_hash_apply_by_fru(fmd.d_asrus, (char *)fmri,
21976e1fa242SStephen Hanson 	    fmd_asru_repaired, &fara);
2198e5dcf7beSRobert Johnston }
2199e5dcf7beSRobert Johnston 
2200a62774dfSSinanallur Balasubramanian /*ARGSUSED*/
2201a62774dfSSinanallur Balasubramanian int
2202a62774dfSSinanallur Balasubramanian fmd_repair_asru(fmd_hdl_t *hdl, const char *fmri)
2203a62774dfSSinanallur Balasubramanian {
2204a62774dfSSinanallur Balasubramanian 	int err = FARA_ERR_RSRCNOTF;
2205a62774dfSSinanallur Balasubramanian 	fmd_asru_rep_arg_t fara;
2206a62774dfSSinanallur Balasubramanian 
2207a62774dfSSinanallur Balasubramanian 	fara.fara_reason = FMD_ASRU_REPAIRED;
2208a62774dfSSinanallur Balasubramanian 	fara.fara_rval = &err;
2209a62774dfSSinanallur Balasubramanian 	fara.fara_uuid = NULL;
2210a62774dfSSinanallur Balasubramanian 	fara.fara_bywhat = FARA_BY_ASRU;
2211a62774dfSSinanallur Balasubramanian 	fmd_asru_hash_apply_by_asru(fmd.d_asrus, fmri,
2212a62774dfSSinanallur Balasubramanian 	    fmd_asru_repaired, &fara);
2213a62774dfSSinanallur Balasubramanian 	return (err);
2214a62774dfSSinanallur Balasubramanian }
2215a62774dfSSinanallur Balasubramanian 
2216d9638e54Smws int
2217d9638e54Smws fmd_event_local(fmd_hdl_t *hdl, fmd_event_t *ep)
2218d9638e54Smws {
2219d9638e54Smws 	if (hdl == NULL || ep == NULL) {
2220d9638e54Smws 		fmd_api_error(fmd_api_module_lock(hdl), EFMD_EVENT_INVAL,
2221d9638e54Smws 		    "NULL parameter specified to fmd_event_local\n");
2222d9638e54Smws 	}
2223d9638e54Smws 
2224d9638e54Smws 	return (((fmd_event_impl_t *)ep)->ev_flags & FMD_EVF_LOCAL);
2225d9638e54Smws }
2226d9638e54Smws 
222724db4641Seschrock /*ARGSUSED*/
222824db4641Seschrock uint64_t
222924db4641Seschrock fmd_event_ena_create(fmd_hdl_t *hdl)
223024db4641Seschrock {
223124db4641Seschrock 	return (fmd_ena());
223224db4641Seschrock }
223324db4641Seschrock 
2234d9638e54Smws fmd_xprt_t *
2235d9638e54Smws fmd_xprt_open(fmd_hdl_t *hdl, uint_t flags, nvlist_t *auth, void *data)
2236d9638e54Smws {
2237d9638e54Smws 	fmd_module_t *mp = fmd_api_module_lock(hdl);
2238d9638e54Smws 	fmd_xprt_t *xp;
2239d9638e54Smws 
2240d9638e54Smws 	if (flags & ~FMD_XPRT_CMASK) {
2241d9638e54Smws 		fmd_api_error(mp, EFMD_XPRT_INVAL,
2242d9638e54Smws 		    "invalid transport flags 0x%x\n", flags);
2243d9638e54Smws 	}
2244d9638e54Smws 
2245d9638e54Smws 	if ((flags & FMD_XPRT_RDWR) != FMD_XPRT_RDWR &&
2246d9638e54Smws 	    (flags & FMD_XPRT_RDWR) != FMD_XPRT_RDONLY) {
2247d9638e54Smws 		fmd_api_error(mp, EFMD_XPRT_INVAL,
2248d9638e54Smws 		    "cannot open write-only transport\n");
2249d9638e54Smws 	}
2250d9638e54Smws 
2251d9638e54Smws 	if (mp->mod_stats->ms_xprtopen.fmds_value.ui32 >=
2252d9638e54Smws 	    mp->mod_stats->ms_xprtlimit.fmds_value.ui32) {
2253d9638e54Smws 		fmd_api_error(mp, EFMD_XPRT_LIMIT, "%s request to create a "
2254d9638e54Smws 		    "transport exceeds module transport limit (%u)\n",
2255d9638e54Smws 		    mp->mod_name, mp->mod_stats->ms_xprtlimit.fmds_value.ui32);
2256d9638e54Smws 	}
2257d9638e54Smws 
2258d9638e54Smws 	if ((xp = fmd_xprt_create(mp, flags, auth, data)) == NULL)
2259d9638e54Smws 		fmd_api_error(mp, errno, "cannot create transport");
2260d9638e54Smws 
2261d9638e54Smws 	fmd_module_unlock(mp);
2262d9638e54Smws 	return (xp);
2263d9638e54Smws }
2264d9638e54Smws 
2265d9638e54Smws void
2266d9638e54Smws fmd_xprt_close(fmd_hdl_t *hdl, fmd_xprt_t *xp)
2267d9638e54Smws {
2268d9638e54Smws 	fmd_module_t *mp = fmd_api_module_lock(hdl);
2269d9638e54Smws 	fmd_xprt_impl_t *xip = fmd_api_transport_impl(hdl, xp);
2270d9638e54Smws 
2271d9638e54Smws 	/*
2272d9638e54Smws 	 * Although this could be supported, it doesn't seem necessary or worth
2273d9638e54Smws 	 * the trouble.  For now, just detect this and trigger a module abort.
2274d9638e54Smws 	 * If it is needed, transports should grow reference counts and a new
2275d9638e54Smws 	 * event type will need to be enqueued for the main thread to reap it.
2276d9638e54Smws 	 */
2277d9638e54Smws 	if (xip->xi_thread != NULL &&
2278d9638e54Smws 	    xip->xi_thread->thr_tid == pthread_self()) {
2279d9638e54Smws 		fmd_api_error(mp, EFMD_XPRT_INVAL,
2280d9638e54Smws 		    "fmd_xprt_close() cannot be called from fmdo_send()\n");
2281d9638e54Smws 	}
2282d9638e54Smws 
2283d9638e54Smws 	fmd_xprt_destroy(xp);
2284d9638e54Smws 	fmd_module_unlock(mp);
2285d9638e54Smws }
2286d9638e54Smws 
2287d9638e54Smws void
2288d9638e54Smws fmd_xprt_post(fmd_hdl_t *hdl, fmd_xprt_t *xp, nvlist_t *nvl, hrtime_t hrt)
2289d9638e54Smws {
22907793aa8bSEric Schrock 	nv_alloc_t *nva = nvlist_lookup_nv_alloc(nvl);
22917793aa8bSEric Schrock 	fmd_module_t *mp = fmd_api_module(hdl);
2292d9638e54Smws 	fmd_xprt_impl_t *xip = fmd_api_transport_impl(hdl, xp);
22937793aa8bSEric Schrock 	nvlist_t *tmp;
22947793aa8bSEric Schrock 
22957793aa8bSEric Schrock 	/*
22967793aa8bSEric Schrock 	 * If this event was allocated using the module-specific nvlist ops, we
22977793aa8bSEric Schrock 	 * need to create a copy using the standard fmd nvlist ops.  Otherwise,
22987793aa8bSEric Schrock 	 * the event may persist after the module has been unloaded and we'll
22997793aa8bSEric Schrock 	 * die when attempting to free the nvlist.
23007793aa8bSEric Schrock 	 */
23017793aa8bSEric Schrock 	if (nva == &mp->mod_nva_sleep || nva == &mp->mod_nva_nosleep) {
23027793aa8bSEric Schrock 		(void) nvlist_xdup(nvl, &tmp, &fmd.d_nva);
23037793aa8bSEric Schrock 		nvlist_free(nvl);
23047793aa8bSEric Schrock 		nvl = tmp;
23057793aa8bSEric Schrock 	}
2306d9638e54Smws 
2307d9638e54Smws 	/*
2308d9638e54Smws 	 * fmd_xprt_recv() must block during startup waiting for fmd to globally
2309d9638e54Smws 	 * clear FMD_XPRT_DSUSPENDED.  As such, we can't allow it to be called
2310d9638e54Smws 	 * from a module's _fmd_init() routine, because that would block
2311d9638e54Smws 	 * fmd from completing initial module loading, resulting in a deadlock.
2312d9638e54Smws 	 */
2313d9638e54Smws 	if ((xip->xi_flags & FMD_XPRT_ISUSPENDED) &&
2314d9638e54Smws 	    (pthread_self() == xip->xi_queue->eq_mod->mod_thread->thr_tid)) {
2315d9638e54Smws 		fmd_api_error(fmd_api_module_lock(hdl), EFMD_XPRT_INVAL,
2316d9638e54Smws 		    "fmd_xprt_post() cannot be called from _fmd_init()\n");
2317d9638e54Smws 	}
2318d9638e54Smws 
231925351652SVuong Nguyen 	fmd_xprt_recv(xp, nvl, hrt, FMD_B_FALSE);
232025351652SVuong Nguyen }
232125351652SVuong Nguyen 
232225351652SVuong Nguyen void
232325351652SVuong Nguyen fmd_xprt_log(fmd_hdl_t *hdl, fmd_xprt_t *xp, nvlist_t *nvl, hrtime_t hrt)
232425351652SVuong Nguyen {
232525351652SVuong Nguyen 	fmd_xprt_impl_t *xip = fmd_api_transport_impl(hdl, xp);
232625351652SVuong Nguyen 
232725351652SVuong Nguyen 	/*
232825351652SVuong Nguyen 	 * fmd_xprt_recv() must block during startup waiting for fmd to globally
232925351652SVuong Nguyen 	 * clear FMD_XPRT_DSUSPENDED.  As such, we can't allow it to be called
233025351652SVuong Nguyen 	 * from a module's _fmd_init() routine, because that would block
233125351652SVuong Nguyen 	 * fmd from completing initial module loading, resulting in a deadlock.
233225351652SVuong Nguyen 	 */
233325351652SVuong Nguyen 	if ((xip->xi_flags & FMD_XPRT_ISUSPENDED) &&
233425351652SVuong Nguyen 	    (pthread_self() == xip->xi_queue->eq_mod->mod_thread->thr_tid)) {
233525351652SVuong Nguyen 		fmd_api_error(fmd_api_module_lock(hdl), EFMD_XPRT_INVAL,
233625351652SVuong Nguyen 		    "fmd_xprt_log() cannot be called from _fmd_init()\n");
233725351652SVuong Nguyen 	}
233825351652SVuong Nguyen 
233925351652SVuong Nguyen 	fmd_xprt_recv(xp, nvl, hrt, FMD_B_TRUE);
2340d9638e54Smws }
2341d9638e54Smws 
2342d9638e54Smws void
2343d9638e54Smws fmd_xprt_suspend(fmd_hdl_t *hdl, fmd_xprt_t *xp)
2344d9638e54Smws {
2345d9638e54Smws 	(void) fmd_api_transport_impl(hdl, xp); /* validate 'xp' */
2346d9638e54Smws 	fmd_xprt_xsuspend(xp, FMD_XPRT_SUSPENDED);
2347d9638e54Smws }
2348d9638e54Smws 
2349d9638e54Smws void
2350d9638e54Smws fmd_xprt_resume(fmd_hdl_t *hdl, fmd_xprt_t *xp)
2351d9638e54Smws {
2352d9638e54Smws 	(void) fmd_api_transport_impl(hdl, xp); /* validate 'xp' */
2353d9638e54Smws 	fmd_xprt_xresume(xp, FMD_XPRT_SUSPENDED);
2354d9638e54Smws }
2355d9638e54Smws 
2356d9638e54Smws int
2357d9638e54Smws fmd_xprt_error(fmd_hdl_t *hdl, fmd_xprt_t *xp)
2358d9638e54Smws {
2359d9638e54Smws 	fmd_xprt_impl_t *xip = fmd_api_transport_impl(hdl, xp);
2360d9638e54Smws 	return (xip->xi_state == _fmd_xprt_state_err);
2361d9638e54Smws }
2362d9638e54Smws 
2363d9638e54Smws /*
2364d9638e54Smws  * Translate all FMRIs in the specified name-value pair list for the specified
2365d9638e54Smws  * FMRI authority, and return a new name-value pair list for the translation.
2366d9638e54Smws  * This function is the recursive engine used by fmd_xprt_translate(), below.
2367d9638e54Smws  */
2368d9638e54Smws static nvlist_t *
2369d9638e54Smws fmd_xprt_xtranslate(nvlist_t *nvl, nvlist_t *auth)
2370d9638e54Smws {
2371d9638e54Smws 	uint_t i, j, n;
2372d9638e54Smws 	nvpair_t *nvp, **nvps;
2373d9638e54Smws 	uint_t nvpslen = 0;
2374d9638e54Smws 	char *name;
2375d9638e54Smws 	size_t namelen = 0;
2376d9638e54Smws 
2377d9638e54Smws 	nvlist_t **a, **b;
2378d9638e54Smws 	nvlist_t *l, *r;
2379d9638e54Smws 	data_type_t type;
2380d9638e54Smws 	char *s;
2381d9638e54Smws 	int err;
2382d9638e54Smws 
2383d9638e54Smws 	(void) nvlist_xdup(nvl, &nvl, &fmd.d_nva);
2384d9638e54Smws 
2385d9638e54Smws 	/*
2386d9638e54Smws 	 * Count up the number of name-value pairs in 'nvl' and compute the
2387d9638e54Smws 	 * maximum length of a name used in this list for use below.
2388d9638e54Smws 	 */
2389d9638e54Smws 	for (nvp = nvlist_next_nvpair(nvl, NULL);
2390d9638e54Smws 	    nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp), nvpslen++) {
2391d9638e54Smws 		size_t len = strlen(nvpair_name(nvp));
2392d9638e54Smws 		namelen = MAX(namelen, len);
2393d9638e54Smws 	}
2394d9638e54Smws 
2395d9638e54Smws 	nvps = alloca(sizeof (nvpair_t *) * nvpslen);
2396d9638e54Smws 	name = alloca(namelen + 1);
2397d9638e54Smws 
2398d9638e54Smws 	/*
2399d9638e54Smws 	 * Store a snapshot of the name-value pairs in 'nvl' into nvps[] so
2400d9638e54Smws 	 * that we can iterate over the original pairs in the loop below while
2401d9638e54Smws 	 * performing arbitrary insert and delete operations on 'nvl' itself.
2402d9638e54Smws 	 */
2403d9638e54Smws 	for (i = 0, nvp = nvlist_next_nvpair(nvl, NULL);
2404d9638e54Smws 	    nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp))
2405d9638e54Smws 		nvps[i++] = nvp;
2406d9638e54Smws 
2407d9638e54Smws 	/*
2408d9638e54Smws 	 * Now iterate over the snapshot of the name-value pairs.  If we find a
2409d9638e54Smws 	 * value that is of type NVLIST or NVLIST_ARRAY, we translate that
2410d9638e54Smws 	 * object by either calling ourself recursively on it, or calling into
2411d9638e54Smws 	 * fmd_fmri_translate() if the object is an FMRI.  We then rip out the
2412d9638e54Smws 	 * original name-value pair and replace it with the translated one.
2413d9638e54Smws 	 */
2414d9638e54Smws 	for (i = 0; i < nvpslen; i++) {
2415d9638e54Smws 		nvp = nvps[i];
2416d9638e54Smws 		type = nvpair_type(nvp);
2417d9638e54Smws 
2418d9638e54Smws 		switch (type) {
2419d9638e54Smws 		case DATA_TYPE_NVLIST_ARRAY:
2420d9638e54Smws 			if (nvpair_value_nvlist_array(nvp, &a, &n) != 0 ||
2421d9638e54Smws 			    a == NULL || n == 0)
2422d9638e54Smws 				continue; /* array is zero-sized; skip it */
2423d9638e54Smws 
2424d9638e54Smws 			b = fmd_alloc(sizeof (nvlist_t *) * n, FMD_SLEEP);
2425d9638e54Smws 
2426d9638e54Smws 			/*
2427d9638e54Smws 			 * If the first array nvlist element looks like an FMRI
2428d9638e54Smws 			 * then assume the other elements are FMRIs as well.
2429d9638e54Smws 			 * If any b[j]'s can't be translated, then EINVAL will
2430d9638e54Smws 			 * be returned from nvlist_add_nvlist_array() below.
2431d9638e54Smws 			 */
2432d9638e54Smws 			if (nvlist_lookup_string(*a, FM_FMRI_SCHEME, &s) == 0) {
2433d9638e54Smws 				for (j = 0; j < n; j++)
2434d9638e54Smws 					b[j] = fmd_fmri_translate(a[j], auth);
2435d9638e54Smws 			} else {
2436d9638e54Smws 				for (j = 0; j < n; j++)
2437d9638e54Smws 					b[j] = fmd_xprt_xtranslate(a[j], auth);
2438d9638e54Smws 			}
2439d9638e54Smws 
2440d9638e54Smws 			(void) strcpy(name, nvpair_name(nvp));
2441d9638e54Smws 			(void) nvlist_remove(nvl, name, type);
2442d9638e54Smws 			err = nvlist_add_nvlist_array(nvl, name, b, n);
2443d9638e54Smws 
2444d9638e54Smws 			for (j = 0; j < n; j++)
2445d9638e54Smws 				nvlist_free(b[j]);
2446d9638e54Smws 
2447d9638e54Smws 			fmd_free(b, sizeof (nvlist_t *) * n);
2448d9638e54Smws 
2449d9638e54Smws 			if (err != 0) {
2450d9638e54Smws 				nvlist_free(nvl);
2451d9638e54Smws 				errno = err;
2452d9638e54Smws 				return (NULL);
2453d9638e54Smws 			}
2454d9638e54Smws 			break;
2455d9638e54Smws 
2456d9638e54Smws 		case DATA_TYPE_NVLIST:
2457d9638e54Smws 			if (nvpair_value_nvlist(nvp, &l) == 0 &&
2458d9638e54Smws 			    nvlist_lookup_string(l, FM_FMRI_SCHEME, &s) == 0)
2459d9638e54Smws 				r = fmd_fmri_translate(l, auth);
2460d9638e54Smws 			else
2461d9638e54Smws 				r = fmd_xprt_xtranslate(l, auth);
2462d9638e54Smws 
2463d9638e54Smws 			if (r == NULL) {
2464d9638e54Smws 				nvlist_free(nvl);
2465d9638e54Smws 				return (NULL);
2466d9638e54Smws 			}
2467d9638e54Smws 
2468d9638e54Smws 			(void) strcpy(name, nvpair_name(nvp));
2469d9638e54Smws 			(void) nvlist_remove(nvl, name, type);
2470d9638e54Smws 			(void) nvlist_add_nvlist(nvl, name, r);
2471d9638e54Smws 
2472d9638e54Smws 			nvlist_free(r);
2473d9638e54Smws 			break;
2474d9638e54Smws 		}
2475d9638e54Smws 	}
2476d9638e54Smws 
2477d9638e54Smws 	return (nvl);
2478d9638e54Smws }
2479d9638e54Smws 
2480d9638e54Smws nvlist_t *
2481d9638e54Smws fmd_xprt_translate(fmd_hdl_t *hdl, fmd_xprt_t *xp, fmd_event_t *ep)
2482d9638e54Smws {
2483d9638e54Smws 	fmd_xprt_impl_t *xip = fmd_api_transport_impl(hdl, xp);
2484d9638e54Smws 
2485d9638e54Smws 	if (xip->xi_auth == NULL) {
2486d9638e54Smws 		fmd_api_error(fmd_api_module_lock(hdl), EFMD_XPRT_INVAL,
2487d9638e54Smws 		    "no authority defined for transport %p\n", (void *)xp);
2488d9638e54Smws 	}
2489d9638e54Smws 
2490d9638e54Smws 	return (fmd_xprt_xtranslate(FMD_EVENT_NVL(ep), xip->xi_auth));
2491d9638e54Smws }
2492d9638e54Smws 
2493cbf75e67SStephen Hanson /*ARGSUSED*/
2494cbf75e67SStephen Hanson void
2495cbf75e67SStephen Hanson fmd_xprt_add_domain(fmd_hdl_t *hdl, nvlist_t *nvl, char *domain)
2496cbf75e67SStephen Hanson {
2497cbf75e67SStephen Hanson 	nvpair_t *nvp, *nvp2;
2498cbf75e67SStephen Hanson 	nvlist_t *nvl2, *nvl3;
2499cbf75e67SStephen Hanson 	char *class;
2500cbf75e67SStephen Hanson 
2501cbf75e67SStephen Hanson 	if (nvl == NULL || domain == NULL)
2502cbf75e67SStephen Hanson 		return;
2503cbf75e67SStephen Hanson 	for (nvp = nvlist_next_nvpair(nvl, NULL); nvp != NULL;
2504cbf75e67SStephen Hanson 	    nvp = nvlist_next_nvpair(nvl, nvp)) {
2505cbf75e67SStephen Hanson 		if (strcmp(nvpair_name(nvp), FM_CLASS) == 0) {
2506cbf75e67SStephen Hanson 			(void) nvpair_value_string(nvp, &class);
2507cbf75e67SStephen Hanson 			if (strcmp(class, FM_LIST_SUSPECT_CLASS) != 0)
2508cbf75e67SStephen Hanson 				return;
2509cbf75e67SStephen Hanson 		}
2510cbf75e67SStephen Hanson 	}
2511cbf75e67SStephen Hanson 	for (nvp = nvlist_next_nvpair(nvl, NULL); nvp != NULL;
2512cbf75e67SStephen Hanson 	    nvp = nvlist_next_nvpair(nvl, nvp)) {
2513cbf75e67SStephen Hanson 		if (strcmp(nvpair_name(nvp), FM_SUSPECT_DE) == 0) {
2514cbf75e67SStephen Hanson 			(void) nvpair_value_nvlist(nvp, &nvl2);
2515cbf75e67SStephen Hanson 			for (nvp2 = nvlist_next_nvpair(nvl2, NULL);
2516cbf75e67SStephen Hanson 			    nvp2 != NULL;
2517cbf75e67SStephen Hanson 			    nvp2 = nvlist_next_nvpair(nvl2, nvp2)) {
2518cbf75e67SStephen Hanson 				if (strcmp(nvpair_name(nvp2),
2519cbf75e67SStephen Hanson 				    FM_FMRI_AUTHORITY) == 0) {
2520cbf75e67SStephen Hanson 					(void) nvpair_value_nvlist(nvp2, &nvl3);
2521cbf75e67SStephen Hanson 					(void) nvlist_add_string(nvl3,
2522cbf75e67SStephen Hanson 					    FM_FMRI_AUTH_DOMAIN, domain);
2523cbf75e67SStephen Hanson 					break;
2524cbf75e67SStephen Hanson 				}
2525cbf75e67SStephen Hanson 			}
2526cbf75e67SStephen Hanson 			break;
2527cbf75e67SStephen Hanson 		}
2528cbf75e67SStephen Hanson 	}
2529cbf75e67SStephen Hanson }
2530cbf75e67SStephen Hanson 
2531d9638e54Smws void
2532d9638e54Smws fmd_xprt_setspecific(fmd_hdl_t *hdl, fmd_xprt_t *xp, void *data)
2533d9638e54Smws {
2534d9638e54Smws 	fmd_api_transport_impl(hdl, xp)->xi_data = data;
2535d9638e54Smws }
2536d9638e54Smws 
2537d9638e54Smws void *
2538d9638e54Smws fmd_xprt_getspecific(fmd_hdl_t *hdl, fmd_xprt_t *xp)
2539d9638e54Smws {
2540d9638e54Smws 	return (fmd_api_transport_impl(hdl, xp)->xi_data);
2541d9638e54Smws }
2542