xref: /onnv-gate/usr/src/cmd/fm/fmd/common/fmd_topo.c (revision 13131:87d7bfd32811)
13062Scindi /*
23062Scindi  * CDDL HEADER START
33062Scindi  *
43062Scindi  * The contents of this file are subject to the terms of the
53062Scindi  * Common Development and Distribution License (the "License").
63062Scindi  * You may not use this file except in compliance with the License.
73062Scindi  *
83062Scindi  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
93062Scindi  * or http://www.opensolaris.org/os/licensing.
103062Scindi  * See the License for the specific language governing permissions
113062Scindi  * and limitations under the License.
123062Scindi  *
133062Scindi  * When distributing Covered Code, include this CDDL HEADER in each
143062Scindi  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
153062Scindi  * If applicable, add the following below this CDDL HEADER, with the
163062Scindi  * fields enclosed by brackets "[]" replaced with your own identifying
173062Scindi  * information: Portions Copyright [yyyy] [name of copyright owner]
183062Scindi  *
193062Scindi  * CDDL HEADER END
203062Scindi  */
213062Scindi /*
22*13131SHyon.Kim@Sun.COM  * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
233062Scindi  */
243062Scindi 
253062Scindi /*
263062Scindi  * FMD Topology Handling
273062Scindi  *
283062Scindi  * Fault manager scheme and module plug-ins may need access to the latest
293062Scindi  * libtopo snapshot.  Upon fmd initialization, a snapshot is taken and
303062Scindi  * made available via fmd_fmri_topology() and fmd_hdl_topology().  Each
313062Scindi  * of these routines returns a libtopo snapshot handle back to the caller.
323062Scindi  * New snapshots are taken if and when a DR event causes the DR generation
333062Scindi  * number to increase.  The current snapshot is retained to assure consistency
343062Scindi  * for modules still using older snapshots and the latest snapshot handle is
353062Scindi  * returned to the caller.
363062Scindi  */
373062Scindi 
383062Scindi #include <fmd_alloc.h>
393062Scindi #include <fmd_error.h>
403062Scindi #include <fmd_subr.h>
413062Scindi #include <fmd_topo.h>
423062Scindi #include <fmd.h>
433062Scindi 
443062Scindi #include <string.h>
453062Scindi #include <unistd.h>
463062Scindi #include <sys/types.h>
473062Scindi #include <fm/fmd_fmri.h>
483062Scindi #include <fm/libtopo.h>
493062Scindi 
503062Scindi static void
fmd_topo_rele_locked(fmd_topo_t * ftp)514198Seschrock fmd_topo_rele_locked(fmd_topo_t *ftp)
524198Seschrock {
534198Seschrock 	ASSERT(MUTEX_HELD(&fmd.d_topo_lock));
544198Seschrock 
554198Seschrock 	if (--ftp->ft_refcount == 0) {
564198Seschrock 		fmd_list_delete(&fmd.d_topo_list, ftp);
574198Seschrock 		topo_close(ftp->ft_hdl);
584198Seschrock 		fmd_free(ftp, sizeof (fmd_topo_t));
594198Seschrock 	}
604198Seschrock }
614198Seschrock 
624198Seschrock void
fmd_topo_update(void)63*13131SHyon.Kim@Sun.COM fmd_topo_update(void)
643062Scindi {
653062Scindi 	int err;
663062Scindi 	topo_hdl_t *tp;
674198Seschrock 	fmd_topo_t *ftp, *prev;
683062Scindi 	char *id;
693062Scindi 	const char *name;
703062Scindi 
714198Seschrock 	(void) pthread_mutex_lock(&fmd.d_topo_lock);
724198Seschrock 
734198Seschrock 	fmd.d_stats->ds_topo_drgen.fmds_value.ui64 = fmd_fmri_get_drgen();
743062Scindi 
753062Scindi 	name = fmd.d_rootdir != NULL &&
763062Scindi 	    *fmd.d_rootdir != '\0' ? fmd.d_rootdir : NULL;
773062Scindi 
783062Scindi 	/*
793062Scindi 	 * Update the topology snapshot.
803062Scindi 	 */
813062Scindi 	if ((tp = topo_open(TOPO_VERSION, name, &err)) == NULL)
823062Scindi 		fmd_panic("failed to open topology library: %s",
833062Scindi 		    topo_strerror(err));
843062Scindi 
859728SEric.Schrock@Sun.COM 	ftp = fmd_alloc(sizeof (fmd_topo_t), FMD_SLEEP);
869728SEric.Schrock@Sun.COM 	ftp->ft_hdl = tp;
879728SEric.Schrock@Sun.COM 	ftp->ft_time_begin = fmd_time_gethrtime();
889728SEric.Schrock@Sun.COM 
89*13131SHyon.Kim@Sun.COM 	if ((id = topo_snap_hold(tp, NULL, &err)) == NULL)
90*13131SHyon.Kim@Sun.COM 		fmd_panic("failed to get topology snapshot: %s",
91*13131SHyon.Kim@Sun.COM 		    topo_strerror(err));
923062Scindi 
933062Scindi 	topo_hdl_strfree(tp, id);
943062Scindi 
959728SEric.Schrock@Sun.COM 	ftp->ft_time_end = fmd_time_gethrtime();
963062Scindi 	fmd.d_stats->ds_topo_gen.fmds_value.ui64++;
974198Seschrock 
984198Seschrock 	/*
994198Seschrock 	 * We always keep a reference count on the last topo snapshot taken.
1004198Seschrock 	 * Release the previous snapshot (if present), and set the current
1014198Seschrock 	 * reference count to 1.
1024198Seschrock 	 */
1034198Seschrock 	if ((prev = fmd_list_next(&fmd.d_topo_list)) != NULL)
1044198Seschrock 		fmd_topo_rele_locked(prev);
1054198Seschrock 	ftp->ft_refcount = 1;
1063062Scindi 	fmd_list_prepend(&fmd.d_topo_list, ftp);
1073062Scindi 
1084198Seschrock 	(void) pthread_mutex_unlock(&fmd.d_topo_lock);
1093062Scindi }
1103062Scindi 
1114198Seschrock fmd_topo_t *
fmd_topo_hold(void)1124198Seschrock fmd_topo_hold(void)
1133062Scindi {
1143062Scindi 	fmd_topo_t *ftp;
1153062Scindi 
1164198Seschrock 	(void) pthread_mutex_lock(&fmd.d_topo_lock);
1174198Seschrock 	ftp = fmd_list_next(&fmd.d_topo_list);
1184198Seschrock 	ftp->ft_refcount++;
1194198Seschrock 	(void) pthread_mutex_unlock(&fmd.d_topo_lock);
1204198Seschrock 
1214198Seschrock 	return (ftp);
1224198Seschrock }
1234198Seschrock 
1244198Seschrock void
fmd_topo_addref(fmd_topo_t * ftp)1254198Seschrock fmd_topo_addref(fmd_topo_t *ftp)
1264198Seschrock {
1274198Seschrock 	(void) pthread_mutex_lock(&fmd.d_topo_lock);
1284198Seschrock 	ftp->ft_refcount++;
1294198Seschrock 	(void) pthread_mutex_unlock(&fmd.d_topo_lock);
1304198Seschrock }
1314198Seschrock 
1324198Seschrock void
fmd_topo_rele(fmd_topo_t * ftp)1334198Seschrock fmd_topo_rele(fmd_topo_t *ftp)
1344198Seschrock {
1354198Seschrock 	(void) pthread_mutex_lock(&fmd.d_topo_lock);
1364198Seschrock 
1374198Seschrock 	fmd_topo_rele_locked(ftp);
1384198Seschrock 
1394198Seschrock 	(void) pthread_mutex_unlock(&fmd.d_topo_lock);
1404198Seschrock }
1414198Seschrock 
1424198Seschrock void
fmd_topo_rele_hdl(topo_hdl_t * thp)1434198Seschrock fmd_topo_rele_hdl(topo_hdl_t *thp)
1444198Seschrock {
1454198Seschrock 	fmd_topo_t *ftp;
1463062Scindi 
1473062Scindi 	(void) pthread_mutex_lock(&fmd.d_topo_lock);
1484198Seschrock 	for (ftp = fmd_list_next(&fmd.d_topo_list); ftp != NULL;
1494198Seschrock 	    ftp = fmd_list_next(ftp)) {
1504198Seschrock 		if (ftp->ft_hdl == thp)
1514198Seschrock 			break;
1523062Scindi 	}
1534198Seschrock 	ASSERT(ftp != NULL);
1544198Seschrock 
1554198Seschrock 	fmd_topo_rele_locked(ftp);
1563062Scindi 	(void) pthread_mutex_unlock(&fmd.d_topo_lock);
1573062Scindi }
1583062Scindi 
1593062Scindi void
fmd_topo_init(void)1603062Scindi fmd_topo_init(void)
1613062Scindi {
162*13131SHyon.Kim@Sun.COM 	fmd_topo_update();
1633062Scindi }
1643062Scindi 
1653062Scindi void
fmd_topo_fini(void)1663062Scindi fmd_topo_fini(void)
1673062Scindi {
1683062Scindi 	fmd_topo_t *ftp;
1693062Scindi 
1703062Scindi 	(void) pthread_mutex_lock(&fmd.d_topo_lock);
1713062Scindi 	while ((ftp = fmd_list_next(&fmd.d_topo_list)) != NULL) {
1723062Scindi 		fmd_list_delete(&fmd.d_topo_list, ftp);
1733062Scindi 		topo_close(ftp->ft_hdl);
1743062Scindi 		fmd_free(ftp, sizeof (fmd_topo_t));
1753062Scindi 	}
1763062Scindi 	(void) pthread_mutex_unlock(&fmd.d_topo_lock);
1773062Scindi }
178