xref: /illumos-gate/usr/src/cmd/fm/fmd/common/fmd_topo.c (revision 6a634c9d)
10eb822a1Scindi /*
20eb822a1Scindi  * CDDL HEADER START
30eb822a1Scindi  *
40eb822a1Scindi  * The contents of this file are subject to the terms of the
50eb822a1Scindi  * Common Development and Distribution License (the "License").
60eb822a1Scindi  * You may not use this file except in compliance with the License.
70eb822a1Scindi  *
80eb822a1Scindi  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90eb822a1Scindi  * or http://www.opensolaris.org/os/licensing.
100eb822a1Scindi  * See the License for the specific language governing permissions
110eb822a1Scindi  * and limitations under the License.
120eb822a1Scindi  *
130eb822a1Scindi  * When distributing Covered Code, include this CDDL HEADER in each
140eb822a1Scindi  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150eb822a1Scindi  * If applicable, add the following below this CDDL HEADER, with the
160eb822a1Scindi  * fields enclosed by brackets "[]" replaced with your own identifying
170eb822a1Scindi  * information: Portions Copyright [yyyy] [name of copyright owner]
180eb822a1Scindi  *
190eb822a1Scindi  * CDDL HEADER END
200eb822a1Scindi  */
210eb822a1Scindi /*
22*8393544eSHyon Kim  * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
230eb822a1Scindi  */
240eb822a1Scindi 
250eb822a1Scindi /*
260eb822a1Scindi  * FMD Topology Handling
270eb822a1Scindi  *
280eb822a1Scindi  * Fault manager scheme and module plug-ins may need access to the latest
290eb822a1Scindi  * libtopo snapshot.  Upon fmd initialization, a snapshot is taken and
300eb822a1Scindi  * made available via fmd_fmri_topology() and fmd_hdl_topology().  Each
310eb822a1Scindi  * of these routines returns a libtopo snapshot handle back to the caller.
320eb822a1Scindi  * New snapshots are taken if and when a DR event causes the DR generation
330eb822a1Scindi  * number to increase.  The current snapshot is retained to assure consistency
340eb822a1Scindi  * for modules still using older snapshots and the latest snapshot handle is
350eb822a1Scindi  * returned to the caller.
360eb822a1Scindi  */
370eb822a1Scindi 
380eb822a1Scindi #include <fmd_alloc.h>
390eb822a1Scindi #include <fmd_error.h>
400eb822a1Scindi #include <fmd_subr.h>
410eb822a1Scindi #include <fmd_topo.h>
420eb822a1Scindi #include <fmd.h>
430eb822a1Scindi 
440eb822a1Scindi #include <string.h>
450eb822a1Scindi #include <unistd.h>
460eb822a1Scindi #include <sys/types.h>
470eb822a1Scindi #include <fm/fmd_fmri.h>
480eb822a1Scindi #include <fm/libtopo.h>
490eb822a1Scindi 
500eb822a1Scindi static void
fmd_topo_rele_locked(fmd_topo_t * ftp)5124db4641Seschrock fmd_topo_rele_locked(fmd_topo_t *ftp)
5224db4641Seschrock {
5324db4641Seschrock 	ASSERT(MUTEX_HELD(&fmd.d_topo_lock));
5424db4641Seschrock 
5524db4641Seschrock 	if (--ftp->ft_refcount == 0) {
5624db4641Seschrock 		fmd_list_delete(&fmd.d_topo_list, ftp);
5724db4641Seschrock 		topo_close(ftp->ft_hdl);
5824db4641Seschrock 		fmd_free(ftp, sizeof (fmd_topo_t));
5924db4641Seschrock 	}
6024db4641Seschrock }
6124db4641Seschrock 
6224db4641Seschrock void
fmd_topo_update(void)63*8393544eSHyon Kim fmd_topo_update(void)
640eb822a1Scindi {
650eb822a1Scindi 	int err;
660eb822a1Scindi 	topo_hdl_t *tp;
6724db4641Seschrock 	fmd_topo_t *ftp, *prev;
680eb822a1Scindi 	char *id;
690eb822a1Scindi 	const char *name;
700eb822a1Scindi 
7124db4641Seschrock 	(void) pthread_mutex_lock(&fmd.d_topo_lock);
7224db4641Seschrock 
7324db4641Seschrock 	fmd.d_stats->ds_topo_drgen.fmds_value.ui64 = fmd_fmri_get_drgen();
740eb822a1Scindi 
750eb822a1Scindi 	name = fmd.d_rootdir != NULL &&
760eb822a1Scindi 	    *fmd.d_rootdir != '\0' ? fmd.d_rootdir : NULL;
770eb822a1Scindi 
780eb822a1Scindi 	/*
790eb822a1Scindi 	 * Update the topology snapshot.
800eb822a1Scindi 	 */
810eb822a1Scindi 	if ((tp = topo_open(TOPO_VERSION, name, &err)) == NULL)
820eb822a1Scindi 		fmd_panic("failed to open topology library: %s",
830eb822a1Scindi 		    topo_strerror(err));
840eb822a1Scindi 
8507312882SEric Schrock 	ftp = fmd_alloc(sizeof (fmd_topo_t), FMD_SLEEP);
8607312882SEric Schrock 	ftp->ft_hdl = tp;
8707312882SEric Schrock 	ftp->ft_time_begin = fmd_time_gethrtime();
8807312882SEric Schrock 
89*8393544eSHyon Kim 	if ((id = topo_snap_hold(tp, NULL, &err)) == NULL)
90*8393544eSHyon Kim 		fmd_panic("failed to get topology snapshot: %s",
91*8393544eSHyon Kim 		    topo_strerror(err));
920eb822a1Scindi 
930eb822a1Scindi 	topo_hdl_strfree(tp, id);
940eb822a1Scindi 
9507312882SEric Schrock 	ftp->ft_time_end = fmd_time_gethrtime();
960eb822a1Scindi 	fmd.d_stats->ds_topo_gen.fmds_value.ui64++;
9724db4641Seschrock 
9824db4641Seschrock 	/*
9924db4641Seschrock 	 * We always keep a reference count on the last topo snapshot taken.
10024db4641Seschrock 	 * Release the previous snapshot (if present), and set the current
10124db4641Seschrock 	 * reference count to 1.
10224db4641Seschrock 	 */
10324db4641Seschrock 	if ((prev = fmd_list_next(&fmd.d_topo_list)) != NULL)
10424db4641Seschrock 		fmd_topo_rele_locked(prev);
10524db4641Seschrock 	ftp->ft_refcount = 1;
1060eb822a1Scindi 	fmd_list_prepend(&fmd.d_topo_list, ftp);
1070eb822a1Scindi 
10824db4641Seschrock 	(void) pthread_mutex_unlock(&fmd.d_topo_lock);
1090eb822a1Scindi }
1100eb822a1Scindi 
11124db4641Seschrock fmd_topo_t *
fmd_topo_hold(void)11224db4641Seschrock fmd_topo_hold(void)
1130eb822a1Scindi {
1140eb822a1Scindi 	fmd_topo_t *ftp;
1150eb822a1Scindi 
1160eb822a1Scindi 	(void) pthread_mutex_lock(&fmd.d_topo_lock);
1170eb822a1Scindi 	ftp = fmd_list_next(&fmd.d_topo_list);
11824db4641Seschrock 	ftp->ft_refcount++;
1190eb822a1Scindi 	(void) pthread_mutex_unlock(&fmd.d_topo_lock);
1200eb822a1Scindi 
12124db4641Seschrock 	return (ftp);
1220eb822a1Scindi }
1230eb822a1Scindi 
1240eb822a1Scindi void
fmd_topo_addref(fmd_topo_t * ftp)12524db4641Seschrock fmd_topo_addref(fmd_topo_t *ftp)
1260eb822a1Scindi {
1270eb822a1Scindi 	(void) pthread_mutex_lock(&fmd.d_topo_lock);
12824db4641Seschrock 	ftp->ft_refcount++;
12924db4641Seschrock 	(void) pthread_mutex_unlock(&fmd.d_topo_lock);
13024db4641Seschrock }
13124db4641Seschrock 
13224db4641Seschrock void
fmd_topo_rele(fmd_topo_t * ftp)13324db4641Seschrock fmd_topo_rele(fmd_topo_t *ftp)
13424db4641Seschrock {
13524db4641Seschrock 	(void) pthread_mutex_lock(&fmd.d_topo_lock);
13624db4641Seschrock 
13724db4641Seschrock 	fmd_topo_rele_locked(ftp);
13824db4641Seschrock 
1390eb822a1Scindi 	(void) pthread_mutex_unlock(&fmd.d_topo_lock);
1400eb822a1Scindi }
1410eb822a1Scindi 
14224db4641Seschrock void
fmd_topo_rele_hdl(topo_hdl_t * thp)14324db4641Seschrock fmd_topo_rele_hdl(topo_hdl_t *thp)
14424db4641Seschrock {
14524db4641Seschrock 	fmd_topo_t *ftp;
14624db4641Seschrock 
14724db4641Seschrock 	(void) pthread_mutex_lock(&fmd.d_topo_lock);
14824db4641Seschrock 	for (ftp = fmd_list_next(&fmd.d_topo_list); ftp != NULL;
14924db4641Seschrock 	    ftp = fmd_list_next(ftp)) {
15024db4641Seschrock 		if (ftp->ft_hdl == thp)
15124db4641Seschrock 			break;
15224db4641Seschrock 	}
15324db4641Seschrock 	ASSERT(ftp != NULL);
15424db4641Seschrock 
15524db4641Seschrock 	fmd_topo_rele_locked(ftp);
15624db4641Seschrock 	(void) pthread_mutex_unlock(&fmd.d_topo_lock);
15724db4641Seschrock }
15824db4641Seschrock 
15924db4641Seschrock void
fmd_topo_init(void)16024db4641Seschrock fmd_topo_init(void)
16124db4641Seschrock {
162*8393544eSHyon Kim 	fmd_topo_update();
16324db4641Seschrock }
16424db4641Seschrock 
1650eb822a1Scindi void
fmd_topo_fini(void)1660eb822a1Scindi fmd_topo_fini(void)
1670eb822a1Scindi {
1680eb822a1Scindi 	fmd_topo_t *ftp;
1690eb822a1Scindi 
1700eb822a1Scindi 	(void) pthread_mutex_lock(&fmd.d_topo_lock);
1710eb822a1Scindi 	while ((ftp = fmd_list_next(&fmd.d_topo_list)) != NULL) {
1720eb822a1Scindi 		fmd_list_delete(&fmd.d_topo_list, ftp);
1730eb822a1Scindi 		topo_close(ftp->ft_hdl);
1740eb822a1Scindi 		fmd_free(ftp, sizeof (fmd_topo_t));
1750eb822a1Scindi 	}
1760eb822a1Scindi 	(void) pthread_mutex_unlock(&fmd.d_topo_lock);
1770eb822a1Scindi }
178