xref: /titanic_50/usr/src/cmd/fm/fmd/common/fmd_topo.c (revision 5547f1d8c68fa119522b859bbf38315dc773e696)
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(void)
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 ((id = topo_snap_hold(tp, NULL, &err)) == NULL)
91 		fmd_panic("failed to get topology snapshot: %s",
92 		    topo_strerror(err));
93 
94 	topo_hdl_strfree(tp, id);
95 
96 	ftp->ft_time_end = 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