10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*1991Sheppo * Common Development and Distribution License (the "License").
6*1991Sheppo * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
21*1991Sheppo
220Sstevel@tonic-gate /*
23*1991Sheppo * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
280Sstevel@tonic-gate
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/param.h>
310Sstevel@tonic-gate #include <sys/mdesc.h>
320Sstevel@tonic-gate #include <sys/mdesc_impl.h>
330Sstevel@tonic-gate
340Sstevel@tonic-gate md_t *
md_init_intern(uint64_t * ptr,void * (* allocp)(size_t),void (* freep)(void *,size_t))35*1991Sheppo md_init_intern(uint64_t *ptr, void *(*allocp)(size_t),
36*1991Sheppo void (*freep)(void *, size_t))
370Sstevel@tonic-gate {
380Sstevel@tonic-gate md_impl_t *mdp;
390Sstevel@tonic-gate int idx;
400Sstevel@tonic-gate int count;
410Sstevel@tonic-gate int done;
42*1991Sheppo uint64_t gen;
430Sstevel@tonic-gate mde_str_cookie_t root_name;
440Sstevel@tonic-gate
450Sstevel@tonic-gate /*
460Sstevel@tonic-gate * Very basic checkup for alignment to avoid
470Sstevel@tonic-gate * bus error issues.
480Sstevel@tonic-gate */
49*1991Sheppo if ((((uintptr_t)ptr) & 7) != 0)
500Sstevel@tonic-gate return (NULL);
510Sstevel@tonic-gate
520Sstevel@tonic-gate mdp = (md_impl_t *)allocp(sizeof (md_impl_t));
53*1991Sheppo
540Sstevel@tonic-gate if (mdp == NULL)
550Sstevel@tonic-gate return (NULL);
560Sstevel@tonic-gate
570Sstevel@tonic-gate mdp->allocp = allocp;
580Sstevel@tonic-gate mdp->freep = freep;
590Sstevel@tonic-gate
600Sstevel@tonic-gate mdp->caddr = (char *)ptr;
610Sstevel@tonic-gate
620Sstevel@tonic-gate /*
630Sstevel@tonic-gate * setup internal structures
640Sstevel@tonic-gate */
65*1991Sheppo
660Sstevel@tonic-gate mdp->headerp = (md_header_t *)mdp->caddr;
670Sstevel@tonic-gate
680Sstevel@tonic-gate if (mdtoh32(mdp->headerp->transport_version) != MD_TRANSPORT_VERSION) {
690Sstevel@tonic-gate goto cleanup_nohash;
700Sstevel@tonic-gate }
710Sstevel@tonic-gate
720Sstevel@tonic-gate mdp->node_blk_size = mdtoh32(mdp->headerp->node_blk_sz);
730Sstevel@tonic-gate mdp->name_blk_size = mdtoh32(mdp->headerp->name_blk_sz);
740Sstevel@tonic-gate mdp->data_blk_size = mdtoh32(mdp->headerp->data_blk_sz);
750Sstevel@tonic-gate
76*1991Sheppo mdp->size = MD_HEADER_SIZE + mdp->node_blk_size +
77*1991Sheppo mdp->name_blk_size + mdp->data_blk_size;
780Sstevel@tonic-gate
79*1991Sheppo mdp->mdep = (md_element_t *)(mdp->caddr + MD_HEADER_SIZE);
80*1991Sheppo mdp->namep = (char *)(mdp->caddr + MD_HEADER_SIZE + mdp->node_blk_size);
81*1991Sheppo mdp->datap = (uint8_t *)(mdp->caddr + MD_HEADER_SIZE +
82*1991Sheppo mdp->name_blk_size + mdp->node_blk_size);
830Sstevel@tonic-gate
840Sstevel@tonic-gate mdp->root_node = MDE_INVAL_ELEM_COOKIE;
850Sstevel@tonic-gate
860Sstevel@tonic-gate
870Sstevel@tonic-gate /*
880Sstevel@tonic-gate * Should do a lot more sanity checking here.
890Sstevel@tonic-gate */
900Sstevel@tonic-gate
910Sstevel@tonic-gate /*
920Sstevel@tonic-gate * Should initialize a name hash here if we intend to use one
930Sstevel@tonic-gate */
940Sstevel@tonic-gate
950Sstevel@tonic-gate /*
960Sstevel@tonic-gate * Setup to find the root node
970Sstevel@tonic-gate */
980Sstevel@tonic-gate root_name = md_find_name((md_t *)mdp, "root");
990Sstevel@tonic-gate if (root_name == MDE_INVAL_STR_COOKIE) {
1000Sstevel@tonic-gate goto cleanup;
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate /*
1040Sstevel@tonic-gate * One more property we need is the count of nodes in the
1050Sstevel@tonic-gate * DAG, not just the number of elements.
1060Sstevel@tonic-gate *
1070Sstevel@tonic-gate * We try and pickup the root node along the way here.
1080Sstevel@tonic-gate */
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate for (done = 0, idx = 0, count = 0; !done; ) {
1110Sstevel@tonic-gate md_element_t *np;
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate np = &(mdp->mdep[idx]);
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate switch (MDE_TAG(np)) {
1160Sstevel@tonic-gate case MDET_LIST_END:
1170Sstevel@tonic-gate done = 1;
1180Sstevel@tonic-gate break;
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate case MDET_NODE:
1210Sstevel@tonic-gate if (root_name == MDE_NAME(np)) {
1220Sstevel@tonic-gate if (mdp->root_node != MDE_INVAL_ELEM_COOKIE) {
1230Sstevel@tonic-gate /* Gah .. more than one root */
1240Sstevel@tonic-gate goto cleanup;
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate mdp->root_node = (mde_cookie_t)idx;
1270Sstevel@tonic-gate }
1280Sstevel@tonic-gate idx = MDE_PROP_INDEX(np);
129*1991Sheppo count++;
1300Sstevel@tonic-gate break;
1310Sstevel@tonic-gate
1320Sstevel@tonic-gate default:
1330Sstevel@tonic-gate idx++; /* ignore */
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate
1370Sstevel@tonic-gate /*
1380Sstevel@tonic-gate * Ensure there is a root node
1390Sstevel@tonic-gate */
1400Sstevel@tonic-gate if (mdp->root_node == MDE_INVAL_ELEM_COOKIE) {
1410Sstevel@tonic-gate goto cleanup;
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate /*
1450Sstevel@tonic-gate * Register the counts
1460Sstevel@tonic-gate */
1470Sstevel@tonic-gate
148*1991Sheppo mdp->element_count = idx + 1; /* include LIST_END */
1490Sstevel@tonic-gate mdp->node_count = count;
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate /*
1520Sstevel@tonic-gate * Final sanity check that everything adds up
1530Sstevel@tonic-gate */
154*1991Sheppo if (mdp->element_count != (mdp->node_blk_size / MD_ELEMENT_SIZE))
1550Sstevel@tonic-gate goto cleanup;
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate mdp->md_magic = LIBMD_MAGIC;
1580Sstevel@tonic-gate
159*1991Sheppo /*
160*1991Sheppo * Setup MD generation
161*1991Sheppo */
162*1991Sheppo if (md_get_prop_val((md_t *)mdp, mdp->root_node,
163*1991Sheppo "md-generation#", &gen) != 0)
164*1991Sheppo mdp->gen = MDESC_INVAL_GEN;
165*1991Sheppo else
166*1991Sheppo mdp->gen = gen;
167*1991Sheppo
1680Sstevel@tonic-gate return ((md_t *)mdp);
1690Sstevel@tonic-gate
170*1991Sheppo cleanup:
1710Sstevel@tonic-gate /*
1720Sstevel@tonic-gate * Clean up here - including a name hash if
1730Sstevel@tonic-gate * we build one.
1740Sstevel@tonic-gate */
175*1991Sheppo
176*1991Sheppo cleanup_nohash:
177221Sla135387 mdp->freep(mdp, sizeof (md_impl_t));
1780Sstevel@tonic-gate return (NULL);
1790Sstevel@tonic-gate }
180