1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate #include <sys/types.h> 30*0Sstevel@tonic-gate #include <sys/param.h> 31*0Sstevel@tonic-gate #include <sys/mdesc.h> 32*0Sstevel@tonic-gate #include <sys/mdesc_impl.h> 33*0Sstevel@tonic-gate 34*0Sstevel@tonic-gate md_t * 35*0Sstevel@tonic-gate md_init_intern(uint64_t *ptr, void *(*allocp)(size_t), void (*freep)(void *)) 36*0Sstevel@tonic-gate { 37*0Sstevel@tonic-gate md_impl_t *mdp; 38*0Sstevel@tonic-gate int idx; 39*0Sstevel@tonic-gate int count; 40*0Sstevel@tonic-gate int done; 41*0Sstevel@tonic-gate mde_str_cookie_t root_name; 42*0Sstevel@tonic-gate 43*0Sstevel@tonic-gate /* 44*0Sstevel@tonic-gate * Very basic checkup for alignment to avoid 45*0Sstevel@tonic-gate * bus error issues. 46*0Sstevel@tonic-gate */ 47*0Sstevel@tonic-gate if ((((uint64_t)ptr)&7) != 0) 48*0Sstevel@tonic-gate return (NULL); 49*0Sstevel@tonic-gate 50*0Sstevel@tonic-gate mdp = (md_impl_t *)allocp(sizeof (md_impl_t)); 51*0Sstevel@tonic-gate if (mdp == NULL) 52*0Sstevel@tonic-gate return (NULL); 53*0Sstevel@tonic-gate 54*0Sstevel@tonic-gate mdp->allocp = allocp; 55*0Sstevel@tonic-gate mdp->freep = freep; 56*0Sstevel@tonic-gate 57*0Sstevel@tonic-gate mdp->caddr = (char *)ptr; 58*0Sstevel@tonic-gate 59*0Sstevel@tonic-gate /* 60*0Sstevel@tonic-gate * setup internal structures 61*0Sstevel@tonic-gate */ 62*0Sstevel@tonic-gate mdp->headerp = (md_header_t *)mdp->caddr; 63*0Sstevel@tonic-gate 64*0Sstevel@tonic-gate if (mdtoh32(mdp->headerp->transport_version) != MD_TRANSPORT_VERSION) { 65*0Sstevel@tonic-gate goto cleanup_nohash; 66*0Sstevel@tonic-gate } 67*0Sstevel@tonic-gate 68*0Sstevel@tonic-gate mdp->node_blk_size = mdtoh32(mdp->headerp->node_blk_sz); 69*0Sstevel@tonic-gate mdp->name_blk_size = mdtoh32(mdp->headerp->name_blk_sz); 70*0Sstevel@tonic-gate mdp->data_blk_size = mdtoh32(mdp->headerp->data_blk_sz); 71*0Sstevel@tonic-gate 72*0Sstevel@tonic-gate mdp->size = MD_HEADER_SIZE+mdp->node_blk_size+ 73*0Sstevel@tonic-gate mdp->name_blk_size+mdp->data_blk_size; 74*0Sstevel@tonic-gate 75*0Sstevel@tonic-gate mdp->mdep = (md_element_t *)(mdp->caddr+MD_HEADER_SIZE); 76*0Sstevel@tonic-gate mdp->namep = (char *)(mdp->caddr+MD_HEADER_SIZE+mdp->node_blk_size); 77*0Sstevel@tonic-gate mdp->datap = (uint8_t *)(mdp->caddr+MD_HEADER_SIZE+mdp->name_blk_size+ 78*0Sstevel@tonic-gate mdp->node_blk_size); 79*0Sstevel@tonic-gate 80*0Sstevel@tonic-gate mdp->root_node = MDE_INVAL_ELEM_COOKIE; 81*0Sstevel@tonic-gate 82*0Sstevel@tonic-gate 83*0Sstevel@tonic-gate /* 84*0Sstevel@tonic-gate * Should do a lot more sanity checking here. 85*0Sstevel@tonic-gate */ 86*0Sstevel@tonic-gate 87*0Sstevel@tonic-gate /* 88*0Sstevel@tonic-gate * Should initialize a name hash here if we intend to use one 89*0Sstevel@tonic-gate */ 90*0Sstevel@tonic-gate 91*0Sstevel@tonic-gate /* 92*0Sstevel@tonic-gate * Setup to find the root node 93*0Sstevel@tonic-gate */ 94*0Sstevel@tonic-gate root_name = md_find_name((md_t *)mdp, "root"); 95*0Sstevel@tonic-gate if (root_name == MDE_INVAL_STR_COOKIE) { 96*0Sstevel@tonic-gate goto cleanup; 97*0Sstevel@tonic-gate } 98*0Sstevel@tonic-gate 99*0Sstevel@tonic-gate /* 100*0Sstevel@tonic-gate * One more property we need is the count of nodes in the 101*0Sstevel@tonic-gate * DAG, not just the number of elements. 102*0Sstevel@tonic-gate * 103*0Sstevel@tonic-gate * We try and pickup the root node along the way here. 104*0Sstevel@tonic-gate */ 105*0Sstevel@tonic-gate 106*0Sstevel@tonic-gate for (done = 0, idx = 0, count = 0; !done; ) { 107*0Sstevel@tonic-gate md_element_t *np; 108*0Sstevel@tonic-gate 109*0Sstevel@tonic-gate np = &(mdp->mdep[idx]); 110*0Sstevel@tonic-gate 111*0Sstevel@tonic-gate switch (MDE_TAG(np)) { 112*0Sstevel@tonic-gate case MDET_LIST_END: 113*0Sstevel@tonic-gate done = 1; 114*0Sstevel@tonic-gate break; 115*0Sstevel@tonic-gate 116*0Sstevel@tonic-gate case MDET_NODE: 117*0Sstevel@tonic-gate if (root_name == MDE_NAME(np)) { 118*0Sstevel@tonic-gate if (mdp->root_node != MDE_INVAL_ELEM_COOKIE) { 119*0Sstevel@tonic-gate /* Gah .. more than one root */ 120*0Sstevel@tonic-gate goto cleanup; 121*0Sstevel@tonic-gate } 122*0Sstevel@tonic-gate mdp->root_node = (mde_cookie_t)idx; 123*0Sstevel@tonic-gate } 124*0Sstevel@tonic-gate idx = MDE_PROP_INDEX(np); 125*0Sstevel@tonic-gate count ++; 126*0Sstevel@tonic-gate break; 127*0Sstevel@tonic-gate 128*0Sstevel@tonic-gate default: 129*0Sstevel@tonic-gate idx++; /* ignore */ 130*0Sstevel@tonic-gate } 131*0Sstevel@tonic-gate } 132*0Sstevel@tonic-gate 133*0Sstevel@tonic-gate /* 134*0Sstevel@tonic-gate * Ensure there is a root node 135*0Sstevel@tonic-gate */ 136*0Sstevel@tonic-gate if (mdp->root_node == MDE_INVAL_ELEM_COOKIE) { 137*0Sstevel@tonic-gate goto cleanup; 138*0Sstevel@tonic-gate } 139*0Sstevel@tonic-gate 140*0Sstevel@tonic-gate /* 141*0Sstevel@tonic-gate * Register the counts 142*0Sstevel@tonic-gate */ 143*0Sstevel@tonic-gate 144*0Sstevel@tonic-gate mdp->element_count = idx+1; /* include LIST_END */ 145*0Sstevel@tonic-gate mdp->node_count = count; 146*0Sstevel@tonic-gate 147*0Sstevel@tonic-gate /* 148*0Sstevel@tonic-gate * Final sanity check that everything adds up 149*0Sstevel@tonic-gate */ 150*0Sstevel@tonic-gate if (mdp->element_count != (mdp->node_blk_size/MD_ELEMENT_SIZE)) 151*0Sstevel@tonic-gate goto cleanup; 152*0Sstevel@tonic-gate 153*0Sstevel@tonic-gate mdp->md_magic = LIBMD_MAGIC; 154*0Sstevel@tonic-gate 155*0Sstevel@tonic-gate return ((md_t *)mdp); 156*0Sstevel@tonic-gate 157*0Sstevel@tonic-gate cleanup:; 158*0Sstevel@tonic-gate /* 159*0Sstevel@tonic-gate * Clean up here - including a name hash if 160*0Sstevel@tonic-gate * we build one. 161*0Sstevel@tonic-gate */ 162*0Sstevel@tonic-gate cleanup_nohash:; 163*0Sstevel@tonic-gate mdp->freep(mdp); 164*0Sstevel@tonic-gate return (NULL); 165*0Sstevel@tonic-gate } 166