xref: /titanic_44/usr/src/common/mdesc/mdesc_scandag.c (revision 1ae0874509b6811fdde1dfd46f0d93fd09867a3f)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*1ae08745Sheppo  * Common Development and Distribution License (the "License").
6*1ae08745Sheppo  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21*1ae08745Sheppo 
227c478bd9Sstevel@tonic-gate /*
23*1ae08745Sheppo  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/param.h>
317c478bd9Sstevel@tonic-gate #ifdef _KERNEL
327c478bd9Sstevel@tonic-gate #include <sys/systm.h>
337c478bd9Sstevel@tonic-gate #else
347c478bd9Sstevel@tonic-gate #include <string.h>
357c478bd9Sstevel@tonic-gate #include <strings.h>
367c478bd9Sstevel@tonic-gate #endif
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <sys/mdesc.h>
397c478bd9Sstevel@tonic-gate #include <sys/mdesc_impl.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate static int
427c478bd9Sstevel@tonic-gate mdl_scan_dag(md_impl_t *mdp,
437c478bd9Sstevel@tonic-gate 	int nodeidx,
447c478bd9Sstevel@tonic-gate 	mde_str_cookie_t node_cookie,
457c478bd9Sstevel@tonic-gate 	mde_str_cookie_t arc_cookie,
467c478bd9Sstevel@tonic-gate 	uint8_t *dseenp,
477c478bd9Sstevel@tonic-gate 	int *idxp,
487c478bd9Sstevel@tonic-gate 	mde_cookie_t *stashp,
497c478bd9Sstevel@tonic-gate 	int level);
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate int
md_scan_dag(md_t * ptr,mde_cookie_t startnode,mde_str_cookie_t node_name_cookie,mde_str_cookie_t arc_name_cookie,mde_cookie_t * stashp)537c478bd9Sstevel@tonic-gate md_scan_dag(md_t *ptr,
547c478bd9Sstevel@tonic-gate 	mde_cookie_t startnode,
557c478bd9Sstevel@tonic-gate 	mde_str_cookie_t node_name_cookie,
567c478bd9Sstevel@tonic-gate 	mde_str_cookie_t arc_name_cookie,
577c478bd9Sstevel@tonic-gate 	mde_cookie_t *stashp)
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate 	int	res;
607c478bd9Sstevel@tonic-gate 	int	idx;
617c478bd9Sstevel@tonic-gate 	uint8_t *seenp;
627c478bd9Sstevel@tonic-gate 	md_impl_t *mdp;
637c478bd9Sstevel@tonic-gate 	int	start;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	mdp = (md_impl_t *)ptr;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	/*
687c478bd9Sstevel@tonic-gate 	 * Possible the caller was lazy and didn't check the
697c478bd9Sstevel@tonic-gate 	 * validitiy of either the node name or the arc name
707c478bd9Sstevel@tonic-gate 	 * on calling ... in which case fail to find any
717c478bd9Sstevel@tonic-gate 	 * nodes.
727c478bd9Sstevel@tonic-gate 	 * This is distinct, from a fail (-1) since we return
737c478bd9Sstevel@tonic-gate 	 * that nothing was found.
747c478bd9Sstevel@tonic-gate 	 */
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	if (node_name_cookie == MDE_INVAL_STR_COOKIE ||
777c478bd9Sstevel@tonic-gate 		arc_name_cookie == MDE_INVAL_STR_COOKIE) return 0;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	/*
807c478bd9Sstevel@tonic-gate 	 * if we want to start at the top, start at index 0
817c478bd9Sstevel@tonic-gate 	 */
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	start = (int)startnode;
847c478bd9Sstevel@tonic-gate 	if (start == MDE_INVAL_ELEM_COOKIE) start = 0;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	/*
877c478bd9Sstevel@tonic-gate 	 * Scan from the start point until the first node.
887c478bd9Sstevel@tonic-gate 	 */
897c478bd9Sstevel@tonic-gate 	while (MDE_TAG(&mdp->mdep[start]) == MDET_NULL) start++;
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	/*
927c478bd9Sstevel@tonic-gate 	 * This was a bogus start point if no node found
937c478bd9Sstevel@tonic-gate 	 */
947c478bd9Sstevel@tonic-gate 	if (MDE_TAG(&mdp->mdep[start]) != MDET_NODE) {
957c478bd9Sstevel@tonic-gate 		return (-1);	/* illegal start node specified */
967c478bd9Sstevel@tonic-gate 	}
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	/*
997c478bd9Sstevel@tonic-gate 	 * Allocate a recursion mask on the local stack fail
1007c478bd9Sstevel@tonic-gate 	 * if we can't allocate the recursion detection.
1017c478bd9Sstevel@tonic-gate 	 */
1027c478bd9Sstevel@tonic-gate 	seenp = (uint8_t *)mdp->allocp(mdp->element_count);
1037c478bd9Sstevel@tonic-gate 	if (seenp == NULL)
1047c478bd9Sstevel@tonic-gate 		return (-1);
1057c478bd9Sstevel@tonic-gate 	(void) memset(seenp, 0, mdp->element_count);
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	/*
1087c478bd9Sstevel@tonic-gate 	 * Now build the list of requested nodes.
1097c478bd9Sstevel@tonic-gate 	 */
1107c478bd9Sstevel@tonic-gate 	idx = 0;
1117c478bd9Sstevel@tonic-gate 	res = mdl_scan_dag(mdp, start,
1127c478bd9Sstevel@tonic-gate 		node_name_cookie, arc_name_cookie,
1137c478bd9Sstevel@tonic-gate 		seenp, &idx, stashp, 0);
1147c478bd9Sstevel@tonic-gate 
11526cf27f0Sla135387 	mdp->freep(seenp, mdp->element_count);
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	return (res >= 0 ? idx : res);
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 
124*1ae08745Sheppo static int
mdl_scan_dag(md_impl_t * mdp,int nodeidx,mde_str_cookie_t node_name_cookie,mde_str_cookie_t arc_name_cookie,uint8_t * seenp,int * idxp,mde_cookie_t * stashp,int level)125*1ae08745Sheppo mdl_scan_dag(md_impl_t *mdp,
1267c478bd9Sstevel@tonic-gate 	int nodeidx,
1277c478bd9Sstevel@tonic-gate 	mde_str_cookie_t node_name_cookie,
1287c478bd9Sstevel@tonic-gate 	mde_str_cookie_t arc_name_cookie,
1297c478bd9Sstevel@tonic-gate 	uint8_t *seenp,
1307c478bd9Sstevel@tonic-gate 	int *idxp,
1317c478bd9Sstevel@tonic-gate 	mde_cookie_t *stashp,
1327c478bd9Sstevel@tonic-gate 	int level)
1337c478bd9Sstevel@tonic-gate {
1347c478bd9Sstevel@tonic-gate 	md_element_t *mdep;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	mdep = &(mdp->mdep[nodeidx]);
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	/* see if cookie is infact a node */
1397c478bd9Sstevel@tonic-gate 	if (MDE_TAG(mdep) != MDET_NODE)
1407c478bd9Sstevel@tonic-gate 		return (-1);
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	/* have we been here before ? */
1437c478bd9Sstevel@tonic-gate 	if (seenp[nodeidx])
1447c478bd9Sstevel@tonic-gate 		return (0);
1457c478bd9Sstevel@tonic-gate 	seenp[nodeidx] = 1;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	/* is this node of the type we seek ? */
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate #ifdef	DEBUG_LIBMDESC
1507c478bd9Sstevel@tonic-gate 	{
1517c478bd9Sstevel@tonic-gate 	int x;
1527c478bd9Sstevel@tonic-gate 	for (x = 0; x < level; x++)
1537c478bd9Sstevel@tonic-gate 		printf("-");
1547c478bd9Sstevel@tonic-gate 	printf("%d (%s)\n", nodeidx, (char *)(mdp->datap + MDE_NAME(mdep)));
1557c478bd9Sstevel@tonic-gate 	}
1567c478bd9Sstevel@tonic-gate #endif
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	if (MDE_NAME(mdep) == node_name_cookie) {
1597c478bd9Sstevel@tonic-gate 		/* record the node in the list and keep searching */
1607c478bd9Sstevel@tonic-gate 		if (stashp != NULL) {
1617c478bd9Sstevel@tonic-gate 			stashp[*idxp] = (mde_cookie_t)nodeidx;
1627c478bd9Sstevel@tonic-gate 		}
1637c478bd9Sstevel@tonic-gate 		(*idxp)++;
1647c478bd9Sstevel@tonic-gate #ifdef	DEBUG_LIBMDESC
1657c478bd9Sstevel@tonic-gate 		printf("\t* %d\n", *idxp);
1667c478bd9Sstevel@tonic-gate #endif
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	/*
1707c478bd9Sstevel@tonic-gate 	 * Simply walk the elements in the node.
1717c478bd9Sstevel@tonic-gate 	 * if we find a matching arc, then recursively call
1727c478bd9Sstevel@tonic-gate 	 * the subordinate looking for a match
1737c478bd9Sstevel@tonic-gate 	 */
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	for (mdep++; MDE_TAG(mdep) != MDET_NODE_END; mdep++) {
1767c478bd9Sstevel@tonic-gate 		if (MDE_TAG(mdep) == MDET_PROP_ARC &&
1777c478bd9Sstevel@tonic-gate 			MDE_NAME(mdep) == arc_name_cookie) {
1787c478bd9Sstevel@tonic-gate 			int res;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 			res = mdl_scan_dag(mdp,
1817c478bd9Sstevel@tonic-gate 			    (int)mdep->d.prop_idx,
1827c478bd9Sstevel@tonic-gate 			    node_name_cookie,
1837c478bd9Sstevel@tonic-gate 			    arc_name_cookie,
1847c478bd9Sstevel@tonic-gate 			    seenp, idxp, stashp, level+1);
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 			if (res == -1)
1877c478bd9Sstevel@tonic-gate 				return (res);
1887c478bd9Sstevel@tonic-gate 		}
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	return (0);
1927c478bd9Sstevel@tonic-gate }
193