xref: /titanic_51/usr/src/cmd/fm/fmd/common/fmd_topo.c (revision 15d9d0b528387242011cdcc6190c9e598cfe3a07)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * FMD Topology Handling
30  *
31  * Fault manager scheme and module plug-ins may need access to the latest
32  * libtopo snapshot.  Upon fmd initialization, a snapshot is taken and
33  * made available via fmd_fmri_topology() and fmd_hdl_topology().  Each
34  * of these routines returns a libtopo snapshot handle back to the caller.
35  * New snapshots are taken if and when a DR event causes the DR generation
36  * number to increase.  The current snapshot is retained to assure consistency
37  * for modules still using older snapshots and the latest snapshot handle is
38  * returned to the caller.
39  */
40 
41 #include <fmd_alloc.h>
42 #include <fmd_error.h>
43 #include <fmd_subr.h>
44 #include <fmd_topo.h>
45 #include <fmd.h>
46 
47 #include <string.h>
48 #include <unistd.h>
49 #include <sys/types.h>
50 #include <fm/fmd_fmri.h>
51 #include <fm/libtopo.h>
52 
53 static void
54 fmd_topo_rele_locked(fmd_topo_t *ftp)
55 {
56 	ASSERT(MUTEX_HELD(&fmd.d_topo_lock));
57 
58 	if (--ftp->ft_refcount == 0) {
59 		fmd_list_delete(&fmd.d_topo_list, ftp);
60 		topo_close(ftp->ft_hdl);
61 		fmd_free(ftp, sizeof (fmd_topo_t));
62 	}
63 }
64 
65 void
66 fmd_topo_update(void)
67 {
68 	int err;
69 	topo_hdl_t *tp;
70 	fmd_topo_t *ftp, *prev;
71 	char *id;
72 	const char *name;
73 
74 	(void) pthread_mutex_lock(&fmd.d_topo_lock);
75 
76 	fmd.d_stats->ds_topo_drgen.fmds_value.ui64 = fmd_fmri_get_drgen();
77 
78 	name = fmd.d_rootdir != NULL &&
79 	    *fmd.d_rootdir != '\0' ? fmd.d_rootdir : NULL;
80 
81 	/*
82 	 * Update the topology snapshot.
83 	 */
84 	if ((tp = topo_open(TOPO_VERSION, name, &err)) == NULL)
85 		fmd_panic("failed to open topology library: %s",
86 		    topo_strerror(err));
87 
88 	if ((id = topo_snap_hold(tp, NULL, &err)) == NULL)
89 		fmd_panic("failed to get topology snapshot: %s",
90 		    topo_strerror(err));
91 
92 	topo_hdl_strfree(tp, id);
93 
94 	ftp = fmd_alloc(sizeof (fmd_topo_t), FMD_SLEEP);
95 	ftp->ft_hdl = tp;
96 	ftp->ft_time = fmd_time_gethrtime();
97 	fmd.d_stats->ds_topo_gen.fmds_value.ui64++;
98 
99 	/*
100 	 * We always keep a reference count on the last topo snapshot taken.
101 	 * Release the previous snapshot (if present), and set the current
102 	 * reference count to 1.
103 	 */
104 	if ((prev = fmd_list_next(&fmd.d_topo_list)) != NULL)
105 		fmd_topo_rele_locked(prev);
106 	ftp->ft_refcount = 1;
107 	fmd_list_prepend(&fmd.d_topo_list, ftp);
108 
109 	(void) pthread_mutex_unlock(&fmd.d_topo_lock);
110 }
111 
112 fmd_topo_t *
113 fmd_topo_hold(void)
114 {
115 	fmd_topo_t *ftp;
116 
117 	(void) pthread_mutex_lock(&fmd.d_topo_lock);
118 	ftp = fmd_list_next(&fmd.d_topo_list);
119 	ftp->ft_refcount++;
120 	(void) pthread_mutex_unlock(&fmd.d_topo_lock);
121 
122 	return (ftp);
123 }
124 
125 void
126 fmd_topo_addref(fmd_topo_t *ftp)
127 {
128 	(void) pthread_mutex_lock(&fmd.d_topo_lock);
129 	ftp->ft_refcount++;
130 	(void) pthread_mutex_unlock(&fmd.d_topo_lock);
131 }
132 
133 void
134 fmd_topo_rele(fmd_topo_t *ftp)
135 {
136 	(void) pthread_mutex_lock(&fmd.d_topo_lock);
137 
138 	fmd_topo_rele_locked(ftp);
139 
140 	(void) pthread_mutex_unlock(&fmd.d_topo_lock);
141 }
142 
143 void
144 fmd_topo_rele_hdl(topo_hdl_t *thp)
145 {
146 	fmd_topo_t *ftp;
147 
148 	(void) pthread_mutex_lock(&fmd.d_topo_lock);
149 	for (ftp = fmd_list_next(&fmd.d_topo_list); ftp != NULL;
150 	    ftp = fmd_list_next(ftp)) {
151 		if (ftp->ft_hdl == thp)
152 			break;
153 	}
154 	ASSERT(ftp != NULL);
155 
156 	fmd_topo_rele_locked(ftp);
157 	(void) pthread_mutex_unlock(&fmd.d_topo_lock);
158 }
159 
160 void
161 fmd_topo_init(void)
162 {
163 	fmd_topo_update();
164 }
165 
166 void
167 fmd_topo_fini(void)
168 {
169 	fmd_topo_t *ftp;
170 
171 	(void) pthread_mutex_lock(&fmd.d_topo_lock);
172 	while ((ftp = fmd_list_next(&fmd.d_topo_list)) != NULL) {
173 		fmd_list_delete(&fmd.d_topo_list, ftp);
174 		topo_close(ftp->ft_hdl);
175 		fmd_free(ftp, sizeof (fmd_topo_t));
176 	}
177 	(void) pthread_mutex_unlock(&fmd.d_topo_lock);
178 }
179