topo_tree.c revision c93c462eec9d46f84d567abf52eb29a27c2e134b
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * CDDL HEADER START
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * The contents of this file are subject to the terms of the
12054bfce7d1b03f143c8287d83cc15b25ff43cfnd * Common Development and Distribution License (the "License").
12054bfce7d1b03f143c8287d83cc15b25ff43cfnd * You may not use this file except in compliance with the License.
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
fa9e4066f08beec538e775443c5be79dd423fcabahrens * or http://www.opensolaris.org/os/licensing.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * See the License for the specific language governing permissions
fa9e4066f08beec538e775443c5be79dd423fcabahrens * and limitations under the License.
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * When distributing Covered Code, include this CDDL HEADER in each
fa9e4066f08beec538e775443c5be79dd423fcabahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * If applicable, add the following below this CDDL HEADER, with the
fa9e4066f08beec538e775443c5be79dd423fcabahrens * fields enclosed by brackets "[]" replaced with your own identifying
fa9e4066f08beec538e775443c5be79dd423fcabahrens * information: Portions Copyright [yyyy] [name of copyright owner]
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * CDDL HEADER END
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
c9431fa1e59a88c2f0abf611f25b97af964449e5ahl/*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
91de656b0815c0aeab4e6fc638e9bb4577aa9513Neil Perrin * Use is subject to license terms.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
91de656b0815c0aeab4e6fc638e9bb4577aa9513Neil Perrin * Topology Trees
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick *
12054bfce7d1b03f143c8287d83cc15b25ff43cfnd * Toplogy trees are instantiated for each builtin (FMRI) scheme specified
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * in topo_builtin.c. Each ttree_t data structure contains the
12054bfce7d1b03f143c8287d83cc15b25ff43cfnd * skeleton of the topology tree (scheme, root node, and file information).
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * The root node of a topology does not represent any FMRI but rather serves
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * as the entry point for topology access interfaces. The file information
fa9e4066f08beec538e775443c5be79dd423fcabahrens * provides a handle to access static .xml files that seed scheme-specifc
91de656b0815c0aeab4e6fc638e9bb4577aa9513Neil Perrin * topologies
91de656b0815c0aeab4e6fc638e9bb4577aa9513Neil Perrin *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Topology trees will remain unpopulated until topo_snap_hold() is called.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * At that time, a ttree_t structure is allocated and added to the list
fa9e4066f08beec538e775443c5be79dd423fcabahrens * trees maintained in topo_hdl_t. Builtin scheme-specific enumerators are
d5285cae913f4e01ffa0e6693a6d8ef1fbea30baGeorge Wilson * called upon to create nodes that represent FMRIs for resources present in the
fa9e4066f08beec538e775443c5be79dd423fcabahrens * system. If a <scheme>-topology.xml file exists in a standard file
fa9e4066f08beec538e775443c5be79dd423fcabahrens * location, the file is used to seed the topology while the rest is
fa9e4066f08beec538e775443c5be79dd423fcabahrens * dynamically created by the builtin or helper enumerator modules.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * For example, the 'hc' tree is enumerated by the hc enumerator (hc.c)
fa9e4066f08beec538e775443c5be79dd423fcabahrens * after the hc-topology.xml is read from /usr/platform/`uname -i`/lib/fm/topo,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * /usr/platform/`uname -r`/lib/fm/topo, or /usr/lib/fm/topo. Each node
fa9e4066f08beec538e775443c5be79dd423fcabahrens * is created with a properly formatted hc FMRI resource.
c9431fa1e59a88c2f0abf611f25b97af964449e5ahl *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Toplogy trees are released and deallocated when topo_snap_hold is called.
c9431fa1e59a88c2f0abf611f25b97af964449e5ahl * Upon return from topo_snap_rele(), all node resources are deallocated
fa9e4066f08beec538e775443c5be79dd423fcabahrens * and all that remains is the ttree_t structure containing the root node.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
fa9e4066f08beec538e775443c5be79dd423fcabahrens
d5285cae913f4e01ffa0e6693a6d8ef1fbea30baGeorge Wilson#include <pthread.h>
d5285cae913f4e01ffa0e6693a6d8ef1fbea30baGeorge Wilson#include <limits.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <assert.h>
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick#include <sys/param.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/systeminfo.h>
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick#include <sys/utsname.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <topo_alloc.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <topo_error.h>
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick#include <topo_file.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <topo_module.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <topo_string.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <topo_subr.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <topo_tree.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensstatic ttree_t *
fa9e4066f08beec538e775443c5be79dd423fcabahrensset_create_error(topo_hdl_t *thp, ttree_t *tp, int err)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (tp != NULL)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick topo_tree_destroy(tp);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (err != 0)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick (void) topo_hdl_seterrno(thp, err);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (NULL);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensttree_t *
fa9e4066f08beec538e775443c5be79dd423fcabahrenstopo_tree_create(topo_hdl_t *thp, topo_mod_t *mod, const char *scheme)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens ttree_t *tp;
fa9e4066f08beec538e775443c5be79dd423fcabahrens tnode_t *rp;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens if ((tp = topo_mod_zalloc(mod, sizeof (ttree_t))) == NULL)
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (set_create_error(thp, NULL, ETOPO_NOMEM));
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens tp->tt_mod = mod;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens if ((tp->tt_scheme = topo_mod_strdup(mod, scheme)) == NULL)
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (set_create_error(thp, tp, ETOPO_NOMEM));
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens /*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Initialize a private walker for internal use
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
fa9e4066f08beec538e775443c5be79dd423fcabahrens if ((tp->tt_walk = topo_mod_zalloc(mod, sizeof (topo_walk_t))) == NULL)
c9431fa1e59a88c2f0abf611f25b97af964449e5ahl return (set_create_error(thp, tp, ETOPO_NOMEM));
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens /*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Create the root of this tree: LINKED but never BOUND
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
d5285cae913f4e01ffa0e6693a6d8ef1fbea30baGeorge Wilson if ((rp = topo_mod_zalloc(mod, sizeof (tnode_t))) == NULL)
d5285cae913f4e01ffa0e6693a6d8ef1fbea30baGeorge Wilson return (set_create_error(thp, tp, 0)); /* th_errno set */
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens rp->tn_state = TOPO_NODE_ROOT | TOPO_NODE_INIT;
fa9e4066f08beec538e775443c5be79dd423fcabahrens rp->tn_name = tp->tt_scheme;
fa9e4066f08beec538e775443c5be79dd423fcabahrens rp->tn_instance = 0;
fa9e4066f08beec538e775443c5be79dd423fcabahrens rp->tn_enum = mod;
fa9e4066f08beec538e775443c5be79dd423fcabahrens rp->tn_hdl = thp;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens topo_node_hold(rp);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens tp->tt_walk->tw_root = rp;
fa9e4066f08beec538e775443c5be79dd423fcabahrens tp->tt_walk->tw_thp = thp;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens topo_mod_hold(mod); /* released when root node destroyed */
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens tp->tt_root = rp;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (tp);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensvoid
fa9e4066f08beec538e775443c5be79dd423fcabahrenstopo_tree_destroy(ttree_t *tp)
{
topo_mod_t *mod;
if (tp == NULL)
return;
mod = tp->tt_mod;
if (tp->tt_walk != NULL)
topo_mod_free(mod, tp->tt_walk, sizeof (topo_walk_t));
if (tp->tt_root != NULL) {
assert(tp->tt_root->tn_refs == 1);
topo_node_rele(tp->tt_root);
}
/*
* Deallocate this last, because a pointer alias for tt_scheme
* (stored in the root node's name field) may be used in
* topo_node_rele().
*/
if (tp->tt_scheme != NULL)
topo_mod_strfree(mod, tp->tt_scheme);
topo_mod_free(mod, tp, sizeof (ttree_t));
}
static int
topo_tree_enum(topo_hdl_t *thp, ttree_t *tp)
{
int rv = 0;
char *pp;
/*
* Attempt to enumerate the tree from a topology map in the
* following order:
* <product-name>-<scheme>-topology
* <platform-name>-<scheme>-topology (uname -i)
* <machine-name>-<scheme>-topology (uname -m)
* <scheme>-topology
*
* Trim any SUNW, from the product or platform name
* before loading file
*/
if (thp->th_product == NULL ||
(pp = strchr(thp->th_product, ',')) == NULL)
pp = thp->th_product;
else
pp++;
if (topo_file_load(tp->tt_root->tn_enum, tp->tt_root,
pp, tp->tt_scheme, 0) < 0) {
if ((pp = strchr(thp->th_platform, ',')) == NULL)
pp = thp->th_platform;
else
pp++;
if (topo_file_load(tp->tt_root->tn_enum, tp->tt_root,
pp, tp->tt_scheme, 0) < 0) {
if (topo_file_load(tp->tt_root->tn_enum, tp->tt_root,
thp->th_machine, tp->tt_scheme, 0) < 0) {
if ((rv = topo_file_load(tp->tt_root->tn_enum,
tp->tt_root, NULL, tp->tt_scheme, 0)) < 0) {
topo_dprintf(thp, TOPO_DBG_ERR, "no "
"topology map found for the %s "
"FMRI set\n", tp->tt_scheme);
}
}
}
}
if (rv != 0)
return (topo_hdl_seterrno(thp, ETOPO_ENUM_NOMAP));
return (0);
}
int
topo_tree_enum_all(topo_hdl_t *thp)
{
int err = 0;
ttree_t *tp;
for (tp = topo_list_next(&thp->th_trees); tp != NULL;
tp = topo_list_next(tp)) {
err |= topo_tree_enum(thp, tp);
}
if (err != 0)
return (-1);
else
return (0);
}