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 #include <sys/types.h>
287c478bd9Sstevel@tonic-gate #include <sys/param.h>
297c478bd9Sstevel@tonic-gate #ifdef _KERNEL
307c478bd9Sstevel@tonic-gate #include <sys/systm.h>
317c478bd9Sstevel@tonic-gate #else
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate #include <strings.h>
347c478bd9Sstevel@tonic-gate #endif
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate #include <sys/mdesc.h>
377c478bd9Sstevel@tonic-gate #include <sys/mdesc_impl.h>
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate static int
407c478bd9Sstevel@tonic-gate mdl_scan_dag(md_impl_t *mdp,
417c478bd9Sstevel@tonic-gate int nodeidx,
427c478bd9Sstevel@tonic-gate mde_str_cookie_t node_cookie,
437c478bd9Sstevel@tonic-gate mde_str_cookie_t arc_cookie,
447c478bd9Sstevel@tonic-gate uint8_t *dseenp,
457c478bd9Sstevel@tonic-gate int *idxp,
467c478bd9Sstevel@tonic-gate mde_cookie_t *stashp,
477c478bd9Sstevel@tonic-gate int level);
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@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)517c478bd9Sstevel@tonic-gate md_scan_dag(md_t *ptr,
527c478bd9Sstevel@tonic-gate mde_cookie_t startnode,
537c478bd9Sstevel@tonic-gate mde_str_cookie_t node_name_cookie,
547c478bd9Sstevel@tonic-gate mde_str_cookie_t arc_name_cookie,
557c478bd9Sstevel@tonic-gate mde_cookie_t *stashp)
567c478bd9Sstevel@tonic-gate {
577c478bd9Sstevel@tonic-gate int res;
587c478bd9Sstevel@tonic-gate int idx;
597c478bd9Sstevel@tonic-gate uint8_t *seenp;
607c478bd9Sstevel@tonic-gate md_impl_t *mdp;
617c478bd9Sstevel@tonic-gate int start;
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate mdp = (md_impl_t *)ptr;
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate * Possible the caller was lazy and didn't check the
677c478bd9Sstevel@tonic-gate * validitiy of either the node name or the arc name
687c478bd9Sstevel@tonic-gate * on calling ... in which case fail to find any
697c478bd9Sstevel@tonic-gate * nodes.
707c478bd9Sstevel@tonic-gate * This is distinct, from a fail (-1) since we return
717c478bd9Sstevel@tonic-gate * that nothing was found.
727c478bd9Sstevel@tonic-gate */
737c478bd9Sstevel@tonic-gate
747c478bd9Sstevel@tonic-gate if (node_name_cookie == MDE_INVAL_STR_COOKIE ||
757c478bd9Sstevel@tonic-gate arc_name_cookie == MDE_INVAL_STR_COOKIE) return 0;
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate /*
787c478bd9Sstevel@tonic-gate * if we want to start at the top, start at index 0
797c478bd9Sstevel@tonic-gate */
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate start = (int)startnode;
827c478bd9Sstevel@tonic-gate if (start == MDE_INVAL_ELEM_COOKIE) start = 0;
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate /*
857c478bd9Sstevel@tonic-gate * Scan from the start point until the first node.
867c478bd9Sstevel@tonic-gate */
877c478bd9Sstevel@tonic-gate while (MDE_TAG(&mdp->mdep[start]) == MDET_NULL) start++;
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate /*
907c478bd9Sstevel@tonic-gate * This was a bogus start point if no node found
917c478bd9Sstevel@tonic-gate */
927c478bd9Sstevel@tonic-gate if (MDE_TAG(&mdp->mdep[start]) != MDET_NODE) {
937c478bd9Sstevel@tonic-gate return (-1); /* illegal start node specified */
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate /*
977c478bd9Sstevel@tonic-gate * Allocate a recursion mask on the local stack fail
987c478bd9Sstevel@tonic-gate * if we can't allocate the recursion detection.
997c478bd9Sstevel@tonic-gate */
1007c478bd9Sstevel@tonic-gate seenp = (uint8_t *)mdp->allocp(mdp->element_count);
1017c478bd9Sstevel@tonic-gate if (seenp == NULL)
1027c478bd9Sstevel@tonic-gate return (-1);
1037c478bd9Sstevel@tonic-gate (void) memset(seenp, 0, mdp->element_count);
1047c478bd9Sstevel@tonic-gate
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate * Now build the list of requested nodes.
1077c478bd9Sstevel@tonic-gate */
1087c478bd9Sstevel@tonic-gate idx = 0;
1097c478bd9Sstevel@tonic-gate res = mdl_scan_dag(mdp, start,
1107c478bd9Sstevel@tonic-gate node_name_cookie, arc_name_cookie,
1117c478bd9Sstevel@tonic-gate seenp, &idx, stashp, 0);
1127c478bd9Sstevel@tonic-gate
11326cf27f0Sla135387 mdp->freep(seenp, mdp->element_count);
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate return (res >= 0 ? idx : res);
1167c478bd9Sstevel@tonic-gate }
1177c478bd9Sstevel@tonic-gate
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate
122*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)123*1ae08745Sheppo mdl_scan_dag(md_impl_t *mdp,
1247c478bd9Sstevel@tonic-gate int nodeidx,
1257c478bd9Sstevel@tonic-gate mde_str_cookie_t node_name_cookie,
1267c478bd9Sstevel@tonic-gate mde_str_cookie_t arc_name_cookie,
1277c478bd9Sstevel@tonic-gate uint8_t *seenp,
1287c478bd9Sstevel@tonic-gate int *idxp,
1297c478bd9Sstevel@tonic-gate mde_cookie_t *stashp,
1307c478bd9Sstevel@tonic-gate int level)
1317c478bd9Sstevel@tonic-gate {
1327c478bd9Sstevel@tonic-gate md_element_t *mdep;
1337c478bd9Sstevel@tonic-gate
1347c478bd9Sstevel@tonic-gate mdep = &(mdp->mdep[nodeidx]);
1357c478bd9Sstevel@tonic-gate
1367c478bd9Sstevel@tonic-gate /* see if cookie is infact a node */
1377c478bd9Sstevel@tonic-gate if (MDE_TAG(mdep) != MDET_NODE)
1387c478bd9Sstevel@tonic-gate return (-1);
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate /* have we been here before ? */
1417c478bd9Sstevel@tonic-gate if (seenp[nodeidx])
1427c478bd9Sstevel@tonic-gate return (0);
1437c478bd9Sstevel@tonic-gate seenp[nodeidx] = 1;
1447c478bd9Sstevel@tonic-gate
1457c478bd9Sstevel@tonic-gate /* is this node of the type we seek ? */
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate #ifdef DEBUG_LIBMDESC
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate int x;
1507c478bd9Sstevel@tonic-gate for (x = 0; x < level; x++)
1517c478bd9Sstevel@tonic-gate printf("-");
1527c478bd9Sstevel@tonic-gate printf("%d (%s)\n", nodeidx, (char *)(mdp->datap + MDE_NAME(mdep)));
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate #endif
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate if (MDE_NAME(mdep) == node_name_cookie) {
1577c478bd9Sstevel@tonic-gate /* record the node in the list and keep searching */
1587c478bd9Sstevel@tonic-gate if (stashp != NULL) {
1597c478bd9Sstevel@tonic-gate stashp[*idxp] = (mde_cookie_t)nodeidx;
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate (*idxp)++;
1627c478bd9Sstevel@tonic-gate #ifdef DEBUG_LIBMDESC
1637c478bd9Sstevel@tonic-gate printf("\t* %d\n", *idxp);
1647c478bd9Sstevel@tonic-gate #endif
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate
1677c478bd9Sstevel@tonic-gate /*
1687c478bd9Sstevel@tonic-gate * Simply walk the elements in the node.
1697c478bd9Sstevel@tonic-gate * if we find a matching arc, then recursively call
1707c478bd9Sstevel@tonic-gate * the subordinate looking for a match
1717c478bd9Sstevel@tonic-gate */
1727c478bd9Sstevel@tonic-gate
1737c478bd9Sstevel@tonic-gate for (mdep++; MDE_TAG(mdep) != MDET_NODE_END; mdep++) {
1747c478bd9Sstevel@tonic-gate if (MDE_TAG(mdep) == MDET_PROP_ARC &&
1757c478bd9Sstevel@tonic-gate MDE_NAME(mdep) == arc_name_cookie) {
1767c478bd9Sstevel@tonic-gate int res;
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate res = mdl_scan_dag(mdp,
1797c478bd9Sstevel@tonic-gate (int)mdep->d.prop_idx,
1807c478bd9Sstevel@tonic-gate node_name_cookie,
1817c478bd9Sstevel@tonic-gate arc_name_cookie,
1827c478bd9Sstevel@tonic-gate seenp, idxp, stashp, level+1);
1837c478bd9Sstevel@tonic-gate
1847c478bd9Sstevel@tonic-gate if (res == -1)
1857c478bd9Sstevel@tonic-gate return (res);
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate
1897c478bd9Sstevel@tonic-gate return (0);
1907c478bd9Sstevel@tonic-gate }
191