xref: /titanic_50/usr/src/cmd/fm/fmd/common/fmd_topo.c (revision 0c19630b1592aa30d3e4d9db1a2a8cf9a91c0e72)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * FMD Topology Handling
28  *
29  * Fault manager scheme and module plug-ins may need access to the latest
30  * libtopo snapshot.  Upon fmd initialization, a snapshot is taken and
31  * made available via fmd_fmri_topology() and fmd_hdl_topology().  Each
32  * of these routines returns a libtopo snapshot handle back to the caller.
33  * New snapshots are taken if and when a DR event causes the DR generation
34  * number to increase.  The current snapshot is retained to assure consistency
35  * for modules still using older snapshots and the latest snapshot handle is
36  * returned to the caller.
37  */
38 
39 #include <fmd_alloc.h>
40 #include <fmd_error.h>
41 #include <fmd_subr.h>
42 #include <fmd_topo.h>
43 #include <fmd.h>
44 
45 #include <string.h>
46 #include <unistd.h>
47 #include <sys/types.h>
48 #include <fm/fmd_fmri.h>
49 #include <fm/libtopo.h>
50 
51 static void
52 fmd_topo_rele_locked(fmd_topo_t *ftp)
53 {
54 	ASSERT(MUTEX_HELD(&fmd.d_topo_lock));
55 
56 	if (--ftp->ft_refcount == 0) {
57 		fmd_list_delete(&fmd.d_topo_list, ftp);
58 		topo_close(ftp->ft_hdl);
59 		fmd_free(ftp, sizeof (fmd_topo_t));
60 	}
61 }
62 
63 void
64 fmd_topo_update(boolean_t need_force)
65 {
66 	int err;
67 	topo_hdl_t *tp;
68 	fmd_topo_t *ftp, *prev;
69 	char *id;
70 	const char *name;
71 
72 	(void) pthread_mutex_lock(&fmd.d_topo_lock);
73 
74 	fmd.d_stats->ds_topo_drgen.fmds_value.ui64 = fmd_fmri_get_drgen();
75 
76 	name = fmd.d_rootdir != NULL &&
77 	    *fmd.d_rootdir != '\0' ? fmd.d_rootdir : NULL;
78 
79 	/*
80 	 * Update the topology snapshot.
81 	 */
82 	if ((tp = topo_open(TOPO_VERSION, name, &err)) == NULL)
83 		fmd_panic("failed to open topology library: %s",
84 		    topo_strerror(err));
85 
86 	ftp = fmd_alloc(sizeof (fmd_topo_t), FMD_SLEEP);
87 	ftp->ft_hdl = tp;
88 	ftp->ft_time_begin = fmd_time_gethrtime();
89 
90 	if (need_force) {
91 		if ((id = topo_snap_hold(tp, NULL, &err)) == NULL)
92 			fmd_panic("failed to get topology snapshot: %s",
93 			    topo_strerror(err));
94 	} else {
95 		if ((id = topo_snap_hold_no_forceload(tp, NULL, &err)) == NULL)
96 			fmd_panic("failed to get topology snapshot: %s",
97 			    topo_strerror(err));
98 	}
99 
100 	topo_hdl_strfree(tp, id);
101 
102 	ftp->ft_time_end = fmd_time_gethrtime();
103 	fmd.d_stats->ds_topo_gen.fmds_value.ui64++;
104 
105 	/*
106 	 * We always keep a reference count on the last topo snapshot taken.
107 	 * Release the previous snapshot (if present), and set the current
108 	 * reference count to 1.
109 	 */
110 	if ((prev = fmd_list_next(&fmd.d_topo_list)) != NULL)
111 		fmd_topo_rele_locked(prev);
112 	ftp->ft_refcount = 1;
113 	fmd_list_prepend(&fmd.d_topo_list, ftp);
114 
115 	(void) pthread_mutex_unlock(&fmd.d_topo_lock);
116 }
117 
118 fmd_topo_t *
119 fmd_topo_hold(void)
120 {
121 	fmd_topo_t *ftp;
122 
123 	(void) pthread_mutex_lock(&fmd.d_topo_lock);
124 	ftp = fmd_list_next(&fmd.d_topo_list);
125 	ftp->ft_refcount++;
126 	(void) pthread_mutex_unlock(&fmd.d_topo_lock);
127 
128 	return (ftp);
129 }
130 
131 void
132 fmd_topo_addref(fmd_topo_t *ftp)
133 {
134 	(void) pthread_mutex_lock(&fmd.d_topo_lock);
135 	ftp->ft_refcount++;
136 	(void) pthread_mutex_unlock(&fmd.d_topo_lock);
137 }
138 
139 void
140 fmd_topo_rele(fmd_topo_t *ftp)
141 {
142 	(void) pthread_mutex_lock(&fmd.d_topo_lock);
143 
144 	fmd_topo_rele_locked(ftp);
145 
146 	(void) pthread_mutex_unlock(&fmd.d_topo_lock);
147 }
148 
149 void
150 fmd_topo_rele_hdl(topo_hdl_t *thp)
151 {
152 	fmd_topo_t *ftp;
153 
154 	(void) pthread_mutex_lock(&fmd.d_topo_lock);
155 	for (ftp = fmd_list_next(&fmd.d_topo_list); ftp != NULL;
156 	    ftp = fmd_list_next(ftp)) {
157 		if (ftp->ft_hdl == thp)
158 			break;
159 	}
160 	ASSERT(ftp != NULL);
161 
162 	fmd_topo_rele_locked(ftp);
163 	(void) pthread_mutex_unlock(&fmd.d_topo_lock);
164 }
165 
166 void
167 fmd_topo_init(void)
168 {
169 	fmd_topo_update(B_TRUE);
170 }
171 
172 void
173 fmd_topo_fini(void)
174 {
175 	fmd_topo_t *ftp;
176 
177 	(void) pthread_mutex_lock(&fmd.d_topo_lock);
178 	while ((ftp = fmd_list_next(&fmd.d_topo_list)) != NULL) {
179 		fmd_list_delete(&fmd.d_topo_list, ftp);
180 		topo_close(ftp->ft_hdl);
181 		fmd_free(ftp, sizeof (fmd_topo_t));
182 	}
183 	(void) pthread_mutex_unlock(&fmd.d_topo_lock);
184 }
185