xref: /illumos-gate/usr/src/lib/fm/topo/libtopo/common/topo_snap.c (revision 4a7ceb24cfcc0a97f96d86cfe5852ae445b50e57)
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  * Snapshot Library Interfaces
30  *
31  * Consumers of topology data may use the interfaces in this file to open,
32  * snapshot and close a topology exported by FMRI scheme (hc, mem and cpu)
33  * builtin plugins and their helper modules.  A topology handle is obtained
34  * by calling topo_open().  Upon a successful return, the caller may use this
35  * handle to open a new snapshot.  Each snapshot is assigned a Universally
36  * Unique Identifier that in a future enchancement to the libtopo API will be
37  * used as the file locator in /var/fm/topo to persist new snapshots or lookup
38  * a previously captured snapshot.  topo_snap_hold() will capture the current
39  * system topology.  All consumers of the topo_hdl_t argument will be
40  * blocked from accessing the topology trees until the snapshot completes.
41  *
42  * A snapshot may be cleared by calling topo_snap_rele().  As with
43  * topo_snap_hold(), all topology accesses are blocked until the topology
44  * trees have been released and deallocated.
45  *
46  * Walker Library Interfaces
47  *
48  * Once a snapshot has been taken with topo_snap_hold(), topo_hdl_t holders
49  * may initiate topology tree walks on a scheme-tree basis.  topo_walk_init()
50  * will initiate the data structures required to walk any one one of the
51  * FMRI scheme trees.  The walker data structure, topo_walk_t, is an opaque
52  * handle passed to topo_walk_step to begin the walk.  At each node in the
53  * topology tree, a callback function is called with access to the node at
54  * which our current walk falls.  The callback function is passed in during
55  * calls to topo_walk_init() and used throughout the walk_step of the
56  * scheme tree.  At any time, the callback may terminate the walk by returning
57  * TOPO_WALK_TERMINATE or TOPO_WALK_ERR.  TOPO_WALK_NEXT will continue the walk.
58  *
59  * The type of walk through the tree may be breadth first or depth first by
60  * respectively passing in TOPO_WALK_SIBLING or TOPO_WALK_CHILD to
61  * the topo_walk_step() function.  Topology nodes
62  * associated with an outstanding walk are held in place and will not be
63  * deallocated until the walk through that node completes.
64  *
65  * Once the walk has terminated, the walking process should call
66  * topo_walk_fini() to clean-up resources created in topo_walk_init()
67  * and release nodes that may be still held.
68  */
69 
70 #include <alloca.h>
71 #include <ctype.h>
72 #include <pthread.h>
73 #include <limits.h>
74 #include <assert.h>
75 #include <fcntl.h>
76 #include <smbios.h>
77 #include <sys/param.h>
78 #include <sys/types.h>
79 #include <sys/stat.h>
80 #include <sys/systeminfo.h>
81 #include <sys/utsname.h>
82 #include <uuid/uuid.h>
83 
84 #include <fm/libtopo.h>
85 
86 #include <topo_alloc.h>
87 #include <topo_builtin.h>
88 #include <topo_string.h>
89 #include <topo_error.h>
90 #include <topo_subr.h>
91 
92 static void topo_snap_destroy(topo_hdl_t *);
93 
94 static topo_hdl_t *
95 set_open_errno(topo_hdl_t *thp, int *errp, int err)
96 {
97 	if (thp != NULL) {
98 		topo_close(thp);
99 	}
100 	if (errp != NULL)
101 		*errp = err;
102 	return (NULL);
103 }
104 
105 topo_hdl_t *
106 topo_open(int version, const char *rootdir, int *errp)
107 {
108 	topo_hdl_t *thp = NULL;
109 	topo_alloc_t *tap;
110 
111 	char platform[MAXNAMELEN];
112 	char isa[MAXNAMELEN];
113 	struct utsname uts;
114 	struct stat st;
115 
116 	smbios_hdl_t *shp;
117 	smbios_system_t s1;
118 	smbios_info_t s2;
119 	id_t id;
120 
121 	char *dbflags, *dbout;
122 
123 	if (version != TOPO_VERSION)
124 		return (set_open_errno(thp, errp, ETOPO_HDL_ABIVER));
125 
126 	if (rootdir != NULL && stat(rootdir, &st) < 0)
127 		return (set_open_errno(thp, errp, ETOPO_HDL_INVAL));
128 
129 	if ((thp = topo_zalloc(sizeof (topo_hdl_t), 0)) == NULL)
130 		return (set_open_errno(thp, errp, ETOPO_NOMEM));
131 
132 	(void) pthread_mutex_init(&thp->th_lock, NULL);
133 
134 	if ((tap = topo_zalloc(sizeof (topo_alloc_t), 0)) == NULL)
135 		return (set_open_errno(thp, errp, ETOPO_NOMEM));
136 
137 	/*
138 	 * Install default allocators
139 	 */
140 	tap->ta_flags = 0;
141 	tap->ta_alloc = topo_alloc;
142 	tap->ta_zalloc = topo_zalloc;
143 	tap->ta_free = topo_free;
144 	tap->ta_nvops.nv_ao_alloc = topo_nv_alloc;
145 	tap->ta_nvops.nv_ao_free = topo_nv_free;
146 	(void) nv_alloc_init(&tap->ta_nva, &tap->ta_nvops);
147 	thp->th_alloc = tap;
148 
149 	if ((thp->th_modhash = topo_modhash_create(thp)) == NULL)
150 		return (set_open_errno(thp, errp, ETOPO_NOMEM));
151 
152 	/*
153 	 * Set-up system information and search paths for modules
154 	 * and topology map files
155 	 */
156 	if (rootdir == NULL) {
157 		rootdir = topo_hdl_strdup(thp, "/");
158 		thp->th_rootdir = (char *)rootdir;
159 	} else {
160 		int len;
161 		char *rpath;
162 
163 		len = strlen(rootdir);
164 		if (len >= PATH_MAX)
165 			return (set_open_errno(thp, errp, EINVAL));
166 
167 		if (rootdir[len] != '/') {
168 			rpath = alloca(len + 1);
169 			(void) snprintf(rpath, len + 1, "%s/", rootdir);
170 		} else {
171 			rpath = (char *)rootdir;
172 		}
173 		thp->th_rootdir = topo_hdl_strdup(thp, rpath);
174 	}
175 
176 	platform[0] = '\0';
177 	isa[0] = '\0';
178 	(void) sysinfo(SI_PLATFORM, platform, sizeof (platform));
179 	(void) sysinfo(SI_ARCHITECTURE, isa, sizeof (isa));
180 	(void) uname(&uts);
181 	thp->th_platform = topo_hdl_strdup(thp, platform);
182 	thp->th_isa = topo_hdl_strdup(thp, isa);
183 	thp->th_machine = topo_hdl_strdup(thp, uts.machine);
184 	if ((shp = smbios_open(NULL, SMB_VERSION, 0, NULL)) != NULL) {
185 		if ((id = smbios_info_system(shp, &s1)) != SMB_ERR &&
186 		    smbios_info_common(shp, id, &s2) != SMB_ERR) {
187 
188 			if (strcmp(s2.smbi_product, SMB_DEFAULT1) != 0 &&
189 			    strcmp(s2.smbi_product, SMB_DEFAULT2) != 0) {
190 				thp->th_product = topo_cleanup_auth_str(thp,
191 				    (char *)s2.smbi_product);
192 			}
193 		}
194 		smbios_close(shp);
195 	} else {
196 		thp->th_product = topo_hdl_strdup(thp, thp->th_platform);
197 	}
198 
199 	if (thp->th_rootdir == NULL || thp->th_platform == NULL ||
200 	    thp->th_machine == NULL)
201 		return (set_open_errno(thp, errp, ETOPO_NOMEM));
202 
203 	dbflags	 = getenv("TOPO_DEBUG");
204 	dbout = getenv("TOPO_DEBUG_OUT");
205 	if (dbflags != NULL)
206 		topo_debug_set(thp, dbflags, dbout);
207 
208 	if (topo_builtin_create(thp, thp->th_rootdir) != 0) {
209 		topo_dprintf(thp, TOPO_DBG_ERR,
210 		    "failed to load builtin modules: %s\n",
211 		    topo_hdl_errmsg(thp));
212 		topo_close(thp);
213 		return (NULL);
214 	}
215 
216 	return (thp);
217 }
218 
219 void
220 topo_close(topo_hdl_t *thp)
221 {
222 	ttree_t *tp;
223 
224 	topo_hdl_lock(thp);
225 	if (thp->th_platform != NULL)
226 		topo_hdl_strfree(thp, thp->th_platform);
227 	if (thp->th_isa != NULL)
228 		topo_hdl_strfree(thp, thp->th_isa);
229 	if (thp->th_machine != NULL)
230 		topo_hdl_strfree(thp, thp->th_machine);
231 	if (thp->th_product != NULL)
232 		topo_hdl_strfree(thp, thp->th_product);
233 	if (thp->th_rootdir != NULL)
234 		topo_hdl_strfree(thp, thp->th_rootdir);
235 
236 	/*
237 	 * Clean-up snapshot
238 	 */
239 	topo_snap_destroy(thp);
240 
241 	/*
242 	 * Clean-up trees
243 	 */
244 	while ((tp = topo_list_next(&thp->th_trees)) != NULL) {
245 		topo_list_delete(&thp->th_trees, tp);
246 		topo_tree_destroy(tp);
247 	}
248 
249 	/*
250 	 * Unload all plugins
251 	 */
252 	topo_modhash_unload_all(thp);
253 
254 	if (thp->th_modhash != NULL)
255 		topo_modhash_destroy(thp);
256 	if (thp->th_alloc != NULL)
257 		topo_free(thp->th_alloc, sizeof (topo_alloc_t));
258 
259 	topo_hdl_unlock(thp);
260 
261 	topo_free(thp, sizeof (topo_hdl_t));
262 }
263 
264 static char *
265 topo_snap_create(topo_hdl_t *thp, int *errp)
266 {
267 	uuid_t uuid;
268 	char *ustr = NULL;
269 
270 	topo_hdl_lock(thp);
271 	if (thp->th_uuid != NULL) {
272 		*errp = ETOPO_HDL_UUID;
273 		topo_hdl_unlock(thp);
274 		return (NULL);
275 	}
276 
277 	if ((thp->th_uuid = topo_hdl_zalloc(thp, TOPO_UUID_SIZE)) == NULL) {
278 		*errp = ETOPO_NOMEM;
279 		topo_dprintf(thp, TOPO_DBG_ERR, "unable to allocate uuid: %s\n",
280 		    topo_strerror(*errp));
281 		topo_hdl_unlock(thp);
282 		return (NULL);
283 	}
284 
285 	uuid_generate(uuid);
286 	uuid_unparse(uuid, thp->th_uuid);
287 
288 	if (topo_tree_enum_all(thp) < 0) {
289 		topo_dprintf(thp, TOPO_DBG_ERR, "enumeration failure: %s\n",
290 		    topo_hdl_errmsg(thp));
291 		if (topo_hdl_errno(thp) == ETOPO_ENUM_FATAL) {
292 			*errp = thp->th_errno;
293 			topo_hdl_unlock(thp);
294 			return (NULL);
295 		}
296 	}
297 
298 	if ((ustr = topo_hdl_strdup(thp, thp->th_uuid)) == NULL)
299 		*errp = ETOPO_NOMEM;
300 
301 	thp->th_di = DI_NODE_NIL;
302 	thp->th_pi = DI_PROM_HANDLE_NIL;
303 
304 	topo_hdl_unlock(thp);
305 
306 	return (ustr);
307 }
308 
309 /*ARGSUSED*/
310 static char *
311 topo_snap_log_create(topo_hdl_t *thp, const char *uuid, int *errp)
312 {
313 	return ((char *)uuid);
314 }
315 
316 /*
317  * Return snapshot id
318  */
319 char *
320 topo_snap_hold(topo_hdl_t *thp, const char *uuid, int *errp)
321 {
322 	if (thp == NULL)
323 		return (NULL);
324 
325 	if (uuid == NULL)
326 		return (topo_snap_create(thp, errp));
327 	else
328 		return (topo_snap_log_create(thp, uuid, errp));
329 }
330 
331 /*ARGSUSED*/
332 static int
333 topo_walk_destroy(topo_hdl_t *thp, tnode_t *node, void *notused)
334 {
335 	tnode_t *cnode;
336 
337 	cnode = topo_child_first(node);
338 
339 	if (cnode != NULL)
340 		return (TOPO_WALK_NEXT);
341 
342 	topo_node_unbind(node);
343 
344 	return (TOPO_WALK_NEXT);
345 }
346 
347 static void
348 topo_snap_destroy(topo_hdl_t *thp)
349 {
350 	int i;
351 	ttree_t *tp;
352 	topo_walk_t *twp;
353 	tnode_t *root;
354 	topo_nodehash_t *nhp;
355 	topo_mod_t *mod;
356 
357 	for (tp = topo_list_next(&thp->th_trees); tp != NULL;
358 	    tp = topo_list_next(tp)) {
359 
360 		root = tp->tt_root;
361 		twp = tp->tt_walk;
362 		/*
363 		 * Clean-up tree nodes from the bottom-up
364 		 */
365 		if ((twp->tw_node = topo_child_first(root)) != NULL) {
366 			twp->tw_cb = topo_walk_destroy;
367 			topo_node_hold(root);
368 			topo_node_hold(twp->tw_node); /* released at walk end */
369 			(void) topo_walk_bottomup(twp, TOPO_WALK_CHILD);
370 			topo_node_rele(root);
371 		}
372 
373 		/*
374 		 * Tidy-up the root node
375 		 */
376 		while ((nhp = topo_list_next(&root->tn_children)) != NULL) {
377 			for (i = 0; i < nhp->th_arrlen; i++) {
378 				assert(nhp->th_nodearr[i] == NULL);
379 			}
380 			mod = nhp->th_enum;
381 			topo_mod_strfree(mod, nhp->th_name);
382 			topo_mod_free(mod, nhp->th_nodearr,
383 			    nhp->th_arrlen * sizeof (tnode_t *));
384 			topo_list_delete(&root->tn_children, nhp);
385 			topo_mod_free(mod, nhp, sizeof (topo_nodehash_t));
386 			topo_mod_rele(mod);
387 		}
388 
389 	}
390 
391 	if (thp->th_uuid != NULL) {
392 		topo_hdl_free(thp, thp->th_uuid, TOPO_UUID_SIZE);
393 		thp->th_uuid = NULL;
394 	}
395 }
396 
397 void
398 topo_snap_release(topo_hdl_t *thp)
399 {
400 	if (thp == NULL)
401 		return;
402 
403 	topo_hdl_lock(thp);
404 	topo_snap_destroy(thp);
405 	topo_hdl_unlock(thp);
406 
407 }
408 
409 topo_walk_t *
410 topo_walk_init(topo_hdl_t *thp, const char *scheme, topo_walk_cb_t cb_f,
411     void *pdata, int *errp)
412 {
413 	ttree_t *tp;
414 	topo_walk_t *wp;
415 
416 	for (tp = topo_list_next(&thp->th_trees); tp != NULL;
417 	    tp = topo_list_next(tp)) {
418 		if (strcmp(scheme, tp->tt_scheme) == 0) {
419 
420 			/*
421 			 * Hold the root node and start walk at the first
422 			 * child node
423 			 */
424 			assert(tp->tt_root != NULL);
425 
426 			if ((wp = topo_node_walk_init(thp, NULL, tp->tt_root,
427 			    cb_f, pdata, errp)) == NULL) /* errp set */
428 				return (NULL);
429 
430 			return (wp);
431 		}
432 	}
433 
434 	*errp = ETOPO_WALK_NOTFOUND;
435 	return (NULL);
436 }
437 
438 static int
439 step_child(tnode_t *cnp, topo_walk_t *wp, int bottomup)
440 {
441 	int status;
442 	tnode_t *nnp;
443 
444 	nnp = topo_child_first(cnp);
445 
446 	if (nnp == NULL) {
447 		topo_dprintf(wp->tw_thp, TOPO_DBG_WALK,
448 		    "step_child: TOPO_WALK_TERMINATE for %s=%d\n",
449 		    cnp->tn_name, cnp->tn_instance);
450 		return (TOPO_WALK_TERMINATE);
451 	}
452 
453 	topo_dprintf(wp->tw_thp, TOPO_DBG_WALK,
454 	    "step_child: walk through node %s=%d to %s=%d\n",
455 	    cnp->tn_name, cnp->tn_instance, nnp->tn_name, nnp->tn_instance);
456 
457 	topo_node_hold(nnp); /* released on return from walk_step */
458 	wp->tw_node = nnp;
459 	if (bottomup == 1)
460 		status = topo_walk_bottomup(wp, TOPO_WALK_CHILD);
461 	else
462 		status = topo_walk_step(wp, TOPO_WALK_CHILD);
463 
464 	return (status);
465 }
466 
467 static int
468 step_sibling(tnode_t *cnp, topo_walk_t *wp, int bottomup)
469 {
470 	int status;
471 	tnode_t *nnp;
472 
473 	nnp = topo_child_next(cnp->tn_parent, cnp);
474 
475 	if (nnp == NULL) {
476 		topo_dprintf(wp->tw_thp, TOPO_DBG_WALK,
477 		    "step_sibling: TOPO_WALK_TERMINATE for %s=%d\n",
478 		    cnp->tn_name, cnp->tn_instance);
479 		return (TOPO_WALK_TERMINATE);
480 	}
481 
482 	topo_dprintf(wp->tw_thp, TOPO_DBG_WALK,
483 	    "step_sibling: through sibling node %s=%d to %s=%d\n",
484 	    cnp->tn_name, cnp->tn_instance, nnp->tn_name, nnp->tn_instance);
485 
486 	topo_node_hold(nnp); /* released on return from walk_step */
487 	wp->tw_node = nnp;
488 	if (bottomup == 1)
489 		status = topo_walk_bottomup(wp, TOPO_WALK_CHILD);
490 	else
491 		status = topo_walk_step(wp, TOPO_WALK_CHILD);
492 
493 	return (status);
494 }
495 
496 int
497 topo_walk_byid(topo_walk_t *wp, const char *name, topo_instance_t inst)
498 {
499 	int status;
500 	tnode_t *nnp, *cnp;
501 
502 	cnp = wp->tw_node;
503 	nnp = topo_node_lookup(cnp, name, inst);
504 	if (nnp == NULL)
505 		return (TOPO_WALK_TERMINATE);
506 
507 	topo_node_hold(nnp);
508 	wp->tw_node = nnp;
509 	if (wp->tw_mod != NULL)
510 		status = wp->tw_cb(wp->tw_mod, nnp, wp->tw_pdata);
511 	else
512 		status = wp->tw_cb(wp->tw_thp, nnp, wp->tw_pdata);
513 	topo_node_rele(nnp);
514 	wp->tw_node = cnp;
515 
516 	return (status);
517 }
518 
519 int
520 topo_walk_step(topo_walk_t *wp, int flag)
521 {
522 	int status;
523 	tnode_t *cnp = wp->tw_node;
524 
525 	if (flag != TOPO_WALK_CHILD && flag != TOPO_WALK_SIBLING) {
526 		topo_node_rele(cnp);
527 		return (TOPO_WALK_ERR);
528 	}
529 
530 	/*
531 	 * No more nodes to walk
532 	 */
533 	if (cnp == NULL) {
534 		topo_dprintf(wp->tw_thp, TOPO_DBG_WALK,
535 		    "walk_step terminated\n");
536 		topo_node_rele(cnp);
537 		return (TOPO_WALK_TERMINATE);
538 	}
539 
540 
541 	if (wp->tw_mod != NULL)
542 		status = wp->tw_cb(wp->tw_mod, cnp, wp->tw_pdata);
543 	else
544 		status = wp->tw_cb(wp->tw_thp, cnp, wp->tw_pdata);
545 
546 	/*
547 	 * Walker callback says we're done
548 	 */
549 	if (status != TOPO_WALK_NEXT) {
550 		topo_node_rele(cnp);
551 		return (status);
552 	}
553 
554 	flag == TOPO_WALK_CHILD ? (status = step_child(cnp, wp, 0)) :
555 	    (status = step_sibling(cnp, wp, 0));
556 
557 	/*
558 	 * No more nodes in this hash, skip to next node hash by stepping
559 	 * to next sibling (depth-first walk) or next child (breadth-first
560 	 * walk).
561 	 */
562 	if (status == TOPO_WALK_TERMINATE) {
563 
564 		flag == TOPO_WALK_CHILD ? (status = step_sibling(cnp, wp, 0)) :
565 		    (status = step_child(cnp, wp, 0));
566 	}
567 
568 	topo_node_rele(cnp); /* done with current node */
569 
570 	return (status);
571 }
572 
573 void
574 topo_walk_fini(topo_walk_t *wp)
575 {
576 	if (wp == NULL)
577 		return;
578 
579 	topo_node_rele(wp->tw_root);
580 
581 	topo_hdl_free(wp->tw_thp, wp, sizeof (topo_walk_t));
582 }
583 
584 int
585 topo_walk_bottomup(topo_walk_t *wp, int flag)
586 {
587 	int status;
588 	tnode_t *cnp;
589 
590 	if (wp == NULL)
591 		return (TOPO_WALK_ERR);
592 
593 	cnp = wp->tw_node;
594 	if (flag != TOPO_WALK_CHILD && flag != TOPO_WALK_SIBLING) {
595 		topo_node_rele(cnp);
596 		return (TOPO_WALK_ERR);
597 	}
598 
599 	/*
600 	 * End of the line
601 	 */
602 	if (cnp == NULL) {
603 		topo_dprintf(wp->tw_thp, TOPO_DBG_WALK,
604 		    "walk_bottomup terminated\n");
605 		topo_node_rele(cnp);
606 		return (TOPO_WALK_TERMINATE);
607 	}
608 
609 	topo_dprintf(wp->tw_thp, TOPO_DBG_WALK,
610 	    "%s walk_bottomup through node %s=%d\n",
611 	    (flag == TOPO_WALK_CHILD ? "TOPO_WALK_CHILD" : "TOPO_WALK_SIBLING"),
612 	    cnp->tn_name, cnp->tn_instance);
613 
614 	if (flag == TOPO_WALK_CHILD)
615 		status = step_child(cnp, wp, 1);
616 	else
617 		status = step_sibling(cnp, wp, 1);
618 
619 	/*
620 	 * At a leaf, run the callback
621 	 */
622 	if (status == TOPO_WALK_TERMINATE) {
623 		if ((status = wp->tw_cb(wp->tw_thp, cnp, wp->tw_pdata))
624 		    != TOPO_WALK_NEXT) {
625 			topo_node_rele(cnp);
626 			return (status);
627 		}
628 	}
629 
630 	/*
631 	 * Try next child or sibling
632 	 */
633 	if (status == TOPO_WALK_NEXT) {
634 		if (flag == TOPO_WALK_CHILD)
635 			status = step_sibling(cnp, wp, 1);
636 		else
637 			status = step_child(cnp, wp, 1);
638 	}
639 
640 	topo_node_rele(cnp); /* done with current node */
641 
642 	return (status);
643 }
644