1*79cd2972Smlelstv /* $NetBSD: sem.c,v 1.74 2016/04/29 18:18:22 mlelstv Exp $ */ 25ecc953bSthorpej 35ecc953bSthorpej /* 45ecc953bSthorpej * Copyright (c) 1992, 1993 55ecc953bSthorpej * The Regents of the University of California. All rights reserved. 65ecc953bSthorpej * 75ecc953bSthorpej * This software was developed by the Computer Systems Engineering group 85ecc953bSthorpej * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 95ecc953bSthorpej * contributed to Berkeley. 105ecc953bSthorpej * 115ecc953bSthorpej * All advertising materials mentioning features or use of this software 125ecc953bSthorpej * must display the following acknowledgement: 135ecc953bSthorpej * This product includes software developed by the University of 145ecc953bSthorpej * California, Lawrence Berkeley Laboratories. 155ecc953bSthorpej * 165ecc953bSthorpej * Redistribution and use in source and binary forms, with or without 175ecc953bSthorpej * modification, are permitted provided that the following conditions 185ecc953bSthorpej * are met: 195ecc953bSthorpej * 1. Redistributions of source code must retain the above copyright 205ecc953bSthorpej * notice, this list of conditions and the following disclaimer. 215ecc953bSthorpej * 2. Redistributions in binary form must reproduce the above copyright 225ecc953bSthorpej * notice, this list of conditions and the following disclaimer in the 235ecc953bSthorpej * documentation and/or other materials provided with the distribution. 245ecc953bSthorpej * 3. Neither the name of the University nor the names of its contributors 255ecc953bSthorpej * may be used to endorse or promote products derived from this software 265ecc953bSthorpej * without specific prior written permission. 275ecc953bSthorpej * 285ecc953bSthorpej * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 295ecc953bSthorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 305ecc953bSthorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 315ecc953bSthorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 325ecc953bSthorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 335ecc953bSthorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 345ecc953bSthorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 355ecc953bSthorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 365ecc953bSthorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 375ecc953bSthorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 385ecc953bSthorpej * SUCH DAMAGE. 395ecc953bSthorpej * 405ecc953bSthorpej * from: @(#)sem.c 8.1 (Berkeley) 6/6/93 415ecc953bSthorpej */ 425ecc953bSthorpej 435ecc953bSthorpej #if HAVE_NBTOOL_CONFIG_H 445ecc953bSthorpej #include "nbtool_config.h" 455ecc953bSthorpej #endif 465ecc953bSthorpej 479a7f4bbaSchristos #include <sys/cdefs.h> 48*79cd2972Smlelstv __RCSID("$NetBSD: sem.c,v 1.74 2016/04/29 18:18:22 mlelstv Exp $"); 499a7f4bbaSchristos 505ecc953bSthorpej #include <sys/param.h> 515ecc953bSthorpej #include <ctype.h> 525ecc953bSthorpej #include <stdio.h> 535ecc953bSthorpej #include <stdlib.h> 545ecc953bSthorpej #include <string.h> 55d0fb8901Schristos #include <util.h> 565ecc953bSthorpej #include "defs.h" 575ecc953bSthorpej #include "sem.h" 585ecc953bSthorpej 595ecc953bSthorpej /* 605ecc953bSthorpej * config semantics. 615ecc953bSthorpej */ 625ecc953bSthorpej 635ecc953bSthorpej #define NAMESIZE 100 /* local name buffers */ 645ecc953bSthorpej 655ecc953bSthorpej const char *s_ifnet; /* magic attribute */ 665ecc953bSthorpej const char *s_qmark; 675ecc953bSthorpej const char *s_none; 685ecc953bSthorpej 695ecc953bSthorpej static struct hashtab *cfhashtab; /* for config lookup */ 705ecc953bSthorpej struct hashtab *devitab; /* etc */ 717c7d91e6Suebayasi struct attr allattr; 72374a3e4fSchristos size_t nattrs; 735ecc953bSthorpej 745ecc953bSthorpej static struct attr errattr; 755ecc953bSthorpej static struct devbase errdev; 765ecc953bSthorpej static struct deva errdeva; 775ecc953bSthorpej 784caf067bSdholland static int has_errobj(struct attrlist *, struct attr *); 795ecc953bSthorpej static struct nvlist *addtoattr(struct nvlist *, struct devbase *); 805ecc953bSthorpej static int resolve(struct nvlist **, const char *, const char *, 815ecc953bSthorpej struct nvlist *, int); 825ecc953bSthorpej static struct pspec *getpspec(struct attr *, struct devbase *, int); 835ecc953bSthorpej static struct devi *newdevi(const char *, int, struct devbase *d); 845ecc953bSthorpej static struct devi *getdevi(const char *); 85c130d400Scube static void remove_devi(struct devi *); 865ecc953bSthorpej static const char *concat(const char *, int); 875ecc953bSthorpej static char *extend(char *, const char *); 885ecc953bSthorpej static int split(const char *, size_t, char *, size_t, int *); 895ecc953bSthorpej static void selectbase(struct devbase *, struct deva *); 9016a8771bSdholland static const char **fixloc(const char *, struct attr *, struct loclist *); 91d767912bSdrochner static const char *makedevstr(devmajor_t, devminor_t); 92d767912bSdrochner static const char *major2name(devmajor_t); 93d767912bSdrochner static devmajor_t dev2major(struct devbase *); 945ecc953bSthorpej 955ecc953bSthorpej extern const char *yyfile; 967aa6070dScube extern int vflag; 975ecc953bSthorpej 985ecc953bSthorpej void 995ecc953bSthorpej initsem(void) 1005ecc953bSthorpej { 1015ecc953bSthorpej 1025ecc953bSthorpej attrtab = ht_new(); 1036d1bba4aSuebayasi attrdeptab = ht_new(); 1047c7d91e6Suebayasi 1057c7d91e6Suebayasi allattr.a_name = "netbsd"; 1067c7d91e6Suebayasi TAILQ_INIT(&allattr.a_files); 1077c7d91e6Suebayasi (void)ht_insert(attrtab, allattr.a_name, &allattr); 108cd06fea3Suebayasi selectattr(&allattr); 1097c7d91e6Suebayasi 1105ecc953bSthorpej errattr.a_name = "<internal>"; 1115ecc953bSthorpej 1125ecc953bSthorpej TAILQ_INIT(&allbases); 1135ecc953bSthorpej 1145ecc953bSthorpej TAILQ_INIT(&alldevas); 1155ecc953bSthorpej 1165ecc953bSthorpej TAILQ_INIT(&allpspecs); 1175ecc953bSthorpej 1185ecc953bSthorpej cfhashtab = ht_new(); 1195ecc953bSthorpej TAILQ_INIT(&allcf); 1205ecc953bSthorpej 1215ecc953bSthorpej TAILQ_INIT(&alldevi); 1225ecc953bSthorpej errdev.d_name = "<internal>"; 1235ecc953bSthorpej 1245ecc953bSthorpej TAILQ_INIT(&allpseudo); 1255ecc953bSthorpej 1265ecc953bSthorpej TAILQ_INIT(&alldevms); 1275ecc953bSthorpej 1285ecc953bSthorpej s_ifnet = intern("ifnet"); 1295ecc953bSthorpej s_qmark = intern("?"); 1305ecc953bSthorpej s_none = intern("none"); 1315ecc953bSthorpej } 1325ecc953bSthorpej 1335ecc953bSthorpej /* Name of include file just ended (set in scan.l) */ 1345ecc953bSthorpej extern const char *lastfile; 1355ecc953bSthorpej 136a87832feSuebayasi static struct attr * 137da706167Suebayasi finddep(struct attr *a, const char *name) 138da706167Suebayasi { 139da706167Suebayasi struct attrlist *al; 140da706167Suebayasi 141da706167Suebayasi for (al = a->a_deps; al != NULL; al = al->al_next) { 142da706167Suebayasi struct attr *this = al->al_this; 143da706167Suebayasi if (strcmp(this->a_name, name) == 0) 144da706167Suebayasi return this; 145da706167Suebayasi } 146da706167Suebayasi return NULL; 147da706167Suebayasi } 148da706167Suebayasi 149da706167Suebayasi static void 150c80681c9Suebayasi mergedeps(const char *dname, const char *name) 151da706167Suebayasi { 152da706167Suebayasi struct attr *a, *newa; 153da706167Suebayasi 154c80681c9Suebayasi CFGDBG(4, "merging attr `%s' to devbase `%s'", name, dname); 155c80681c9Suebayasi a = refattr(dname); 156da706167Suebayasi if (finddep(a, name) == NULL) { 157da706167Suebayasi newa = refattr(name); 158da706167Suebayasi a->a_deps = attrlist_cons(a->a_deps, newa); 159da706167Suebayasi CFGDBG(3, "attr `%s' merged to attr `%s'", newa->a_name, 160da706167Suebayasi a->a_name); 161da706167Suebayasi } 162da706167Suebayasi } 163da706167Suebayasi 164da706167Suebayasi static void 165da706167Suebayasi fixdev(struct devbase *dev) 166da706167Suebayasi { 167da706167Suebayasi struct attrlist *al; 168da706167Suebayasi struct attr *devattr, *a; 169da706167Suebayasi 170da706167Suebayasi devattr = refattr(dev->d_name); 171da706167Suebayasi if (devattr->a_devclass) 172a87832feSuebayasi panic("%s: dev %s is devclass!", __func__, devattr->a_name); 173da706167Suebayasi 174c4bac1d0Suebayasi CFGDBG(4, "fixing devbase `%s'", dev->d_name); 175da706167Suebayasi for (al = dev->d_attrs; al != NULL; al = al->al_next) { 176da706167Suebayasi a = al->al_this; 177bc707469Suebayasi CFGDBG(4, "fixing devbase `%s' attr `%s'", dev->d_name, a->a_name); 178da706167Suebayasi if (a->a_iattr) { 179da706167Suebayasi a->a_refs = addtoattr(a->a_refs, dev); 180c4bac1d0Suebayasi CFGDBG(3, "device `%s' has iattr `%s'", dev->d_name, 181c4bac1d0Suebayasi a->a_name); 182da706167Suebayasi } else if (a->a_devclass != NULL) { 183c5a6c986Suebayasi if (dev->d_classattr != NULL && dev->d_classattr != a) { 184da706167Suebayasi cfgwarn("device `%s' has multiple classes " 185da706167Suebayasi "(`%s' and `%s')", 186da706167Suebayasi dev->d_name, dev->d_classattr->a_name, 187da706167Suebayasi a->a_name); 188da706167Suebayasi } 189c5a6c986Suebayasi if (dev->d_classattr == NULL) { 190da706167Suebayasi dev->d_classattr = a; 191c4bac1d0Suebayasi CFGDBG(3, "device `%s' is devclass `%s'", dev->d_name, 192c4bac1d0Suebayasi a->a_name); 193c5a6c986Suebayasi } 194da706167Suebayasi } else { 195da706167Suebayasi if (strcmp(dev->d_name, a->a_name) != 0) { 196bc707469Suebayasi mergedeps(dev->d_name, a->a_name); 197da706167Suebayasi } 198da706167Suebayasi } 199da706167Suebayasi } 200da706167Suebayasi } 201da706167Suebayasi 2025ecc953bSthorpej void 2035ecc953bSthorpej enddefs(void) 2045ecc953bSthorpej { 2055ecc953bSthorpej struct devbase *dev; 2065ecc953bSthorpej 207c4bac1d0Suebayasi yyfile = "enddefs"; 208c4bac1d0Suebayasi 2095ecc953bSthorpej TAILQ_FOREACH(dev, &allbases, d_next) { 2105ecc953bSthorpej if (!dev->d_isdef) { 2115ecc953bSthorpej (void)fprintf(stderr, 2125ecc953bSthorpej "%s: device `%s' used but not defined\n", 2135ecc953bSthorpej lastfile, dev->d_name); 2145ecc953bSthorpej errors++; 2155ecc953bSthorpej continue; 2165ecc953bSthorpej } 217bc707469Suebayasi fixdev(dev); 2185ecc953bSthorpej } 2195ecc953bSthorpej if (errors) { 2205ecc953bSthorpej (void)fprintf(stderr, "*** Stop.\n"); 2215ecc953bSthorpej exit(1); 2225ecc953bSthorpej } 2235ecc953bSthorpej } 2245ecc953bSthorpej 2255ecc953bSthorpej void 2265ecc953bSthorpej setdefmaxusers(int min, int def, int max) 2275ecc953bSthorpej { 2285ecc953bSthorpej 2295ecc953bSthorpej if (min < 1 || min > def || def > max) 230c7295a4cSchristos cfgerror("maxusers must have 1 <= min (%d) <= default (%d) " 231c7295a4cSchristos "<= max (%d)", min, def, max); 2325ecc953bSthorpej else { 2335ecc953bSthorpej minmaxusers = min; 2345ecc953bSthorpej defmaxusers = def; 2355ecc953bSthorpej maxmaxusers = max; 2365ecc953bSthorpej } 2375ecc953bSthorpej } 2385ecc953bSthorpej 2395ecc953bSthorpej void 2405ecc953bSthorpej setmaxusers(int n) 2415ecc953bSthorpej { 2425ecc953bSthorpej 243895e5687Scube if (maxusers == n) { 244c7295a4cSchristos cfgerror("duplicate maxusers parameter"); 2455ecc953bSthorpej return; 2465ecc953bSthorpej } 247895e5687Scube if (vflag && maxusers != 0) 248c7295a4cSchristos cfgwarn("warning: maxusers already defined"); 2495ecc953bSthorpej maxusers = n; 2505ecc953bSthorpej if (n < minmaxusers) { 251c7295a4cSchristos cfgerror("warning: minimum of %d maxusers assumed", 252c7295a4cSchristos minmaxusers); 2535ecc953bSthorpej errors--; /* take it away */ 2545ecc953bSthorpej maxusers = minmaxusers; 2555ecc953bSthorpej } else if (n > maxmaxusers) { 256c7295a4cSchristos cfgerror("warning: maxusers (%d) > %d", n, maxmaxusers); 2575ecc953bSthorpej errors--; 2585ecc953bSthorpej } 2595ecc953bSthorpej } 2605ecc953bSthorpej 2615ecc953bSthorpej void 2625ecc953bSthorpej setident(const char *i) 2635ecc953bSthorpej { 2645ecc953bSthorpej 2654a7be1f9Smrg if (i) 2665ecc953bSthorpej ident = intern(i); 2674a7be1f9Smrg else 2684a7be1f9Smrg ident = NULL; 2695ecc953bSthorpej } 2705ecc953bSthorpej 2715ecc953bSthorpej /* 2725ecc953bSthorpej * Define an attribute, optionally with an interface (a locator list) 2735ecc953bSthorpej * and a set of attribute-dependencies. 2745ecc953bSthorpej * 2755ecc953bSthorpej * Attribute dependencies MAY NOT be interface attributes. 2765ecc953bSthorpej * 2775ecc953bSthorpej * Since an empty locator list is logically different from "no interface", 2785ecc953bSthorpej * all locator lists include a dummy head node, which we discard here. 2795ecc953bSthorpej */ 2805ecc953bSthorpej int 281bb4307a6Suebayasi defattr0(const char *name, struct loclist *locs, struct attrlist *deps, 282bb4307a6Suebayasi int devclass) 283bb4307a6Suebayasi { 284bb4307a6Suebayasi 285bb4307a6Suebayasi if (locs != NULL) 286bb4307a6Suebayasi return defiattr(name, locs, deps, devclass); 287bb4307a6Suebayasi else if (devclass) 288bb4307a6Suebayasi return defdevclass(name, locs, deps, devclass); 289bb4307a6Suebayasi else 290bb4307a6Suebayasi return defattr(name, locs, deps, devclass); 291bb4307a6Suebayasi } 292bb4307a6Suebayasi 293bb4307a6Suebayasi int 29416a8771bSdholland defattr(const char *name, struct loclist *locs, struct attrlist *deps, 2955ecc953bSthorpej int devclass) 2965ecc953bSthorpej { 2975ecc953bSthorpej struct attr *a, *dep; 2984caf067bSdholland struct attrlist *al; 2995ecc953bSthorpej 3005ecc953bSthorpej /* 3015ecc953bSthorpej * If this attribute depends on any others, make sure none of 3025ecc953bSthorpej * the dependencies are interface attributes. 3035ecc953bSthorpej */ 3044caf067bSdholland for (al = deps; al != NULL; al = al->al_next) { 3054caf067bSdholland dep = al->al_this; 3065ecc953bSthorpej if (dep->a_iattr) { 307c7295a4cSchristos cfgerror("`%s' dependency `%s' is an interface " 308c7295a4cSchristos "attribute", name, dep->a_name); 3095ecc953bSthorpej return (1); 3105ecc953bSthorpej } 3116d1bba4aSuebayasi (void)ht_insert2(attrdeptab, name, dep->a_name, NULL); 312bc707469Suebayasi CFGDBG(2, "attr `%s' depends on attr `%s'", name, dep->a_name); 3135ecc953bSthorpej } 3145ecc953bSthorpej 315bb52c144Suebayasi if (getrefattr(name, &a)) { 316c7295a4cSchristos cfgerror("attribute `%s' already defined", name); 31716a8771bSdholland loclist_destroy(locs); 3185ecc953bSthorpej return (1); 3195ecc953bSthorpej } 320bb52c144Suebayasi if (a == NULL) 321bb52c144Suebayasi a = mkattr(name); 3225ecc953bSthorpej 323a949e288Suebayasi a->a_deps = deps; 324bb4307a6Suebayasi expandattr(a, NULL); 3252f2f3382Suebayasi CFGDBG(3, "attr `%s' defined", a->a_name); 326bb4307a6Suebayasi 327bb4307a6Suebayasi return (0); 328bb4307a6Suebayasi } 329a949e288Suebayasi 3302f2f3382Suebayasi struct attr * 3312f2f3382Suebayasi mkattr(const char *name) 3322f2f3382Suebayasi { 3332f2f3382Suebayasi struct attr *a; 3342f2f3382Suebayasi 3352f2f3382Suebayasi a = ecalloc(1, sizeof *a); 3362f2f3382Suebayasi if (ht_insert(attrtab, name, a)) { 3372f2f3382Suebayasi free(a); 3382f2f3382Suebayasi return NULL; 3392f2f3382Suebayasi } 3402f2f3382Suebayasi a->a_name = name; 3412f2f3382Suebayasi TAILQ_INIT(&a->a_files); 3422f2f3382Suebayasi CFGDBG(3, "attr `%s' allocated", name); 3432f2f3382Suebayasi 3442f2f3382Suebayasi return a; 3452f2f3382Suebayasi } 3462f2f3382Suebayasi 347a949e288Suebayasi /* "interface attribute" initialization */ 348bb4307a6Suebayasi int 349bb4307a6Suebayasi defiattr(const char *name, struct loclist *locs, struct attrlist *deps, 350bb4307a6Suebayasi int devclass) 351bb4307a6Suebayasi { 352bb4307a6Suebayasi struct attr *a; 353bb4307a6Suebayasi int len; 354bb4307a6Suebayasi struct loclist *ll; 355bb4307a6Suebayasi 356bb4307a6Suebayasi if (devclass) 357bb4307a6Suebayasi panic("defattr(%s): locators and devclass", name); 358bb4307a6Suebayasi 359bb4307a6Suebayasi if (defattr(name, locs, deps, devclass) != 0) 360bb4307a6Suebayasi return (1); 361bb4307a6Suebayasi 362bb4307a6Suebayasi a = getattr(name); 3635ecc953bSthorpej a->a_iattr = 1; 36416a8771bSdholland /* unwrap */ 36516a8771bSdholland a->a_locs = locs->ll_next; 36616a8771bSdholland locs->ll_next = NULL; 36716a8771bSdholland loclist_destroy(locs); 368a949e288Suebayasi len = 0; 369a949e288Suebayasi for (ll = a->a_locs; ll != NULL; ll = ll->ll_next) 370a949e288Suebayasi len++; 371a949e288Suebayasi a->a_loclen = len; 372bb4307a6Suebayasi if (deps) 373bb4307a6Suebayasi CFGDBG(2, "attr `%s' iface with deps", a->a_name); 374bb4307a6Suebayasi return (0); 3755ecc953bSthorpej } 376a949e288Suebayasi 377a949e288Suebayasi /* "device class" initialization */ 378bb4307a6Suebayasi int 379bb4307a6Suebayasi defdevclass(const char *name, struct loclist *locs, struct attrlist *deps, 380bb4307a6Suebayasi int devclass) 381bb4307a6Suebayasi { 382bb4307a6Suebayasi struct attr *a; 383bbfbbde1Schristos char classenum[256], *cp; 3845ecc953bSthorpej int errored = 0; 3855ecc953bSthorpej 386bb4307a6Suebayasi if (deps) 387bb4307a6Suebayasi panic("defattr(%s): dependencies and devclass", name); 388bb4307a6Suebayasi 389bb4307a6Suebayasi if (defattr(name, locs, deps, devclass) != 0) 390bb4307a6Suebayasi return (1); 391bb4307a6Suebayasi 392bb4307a6Suebayasi a = getattr(name); 393bbfbbde1Schristos (void)snprintf(classenum, sizeof(classenum), "DV_%s", name); 3945ecc953bSthorpej for (cp = classenum + 3; *cp; cp++) { 3955ecc953bSthorpej if (!errored && 3965ecc953bSthorpej (!isalnum((unsigned char)*cp) || 3975ecc953bSthorpej (isalpha((unsigned char)*cp) && !islower((unsigned char)*cp)))) { 398c7295a4cSchristos cfgerror("device class names must be " 399c7295a4cSchristos "lower-case alphanumeric characters"); 4005ecc953bSthorpej errored = 1; 4015ecc953bSthorpej } 40264a07b54Sjoerg *cp = (char)toupper((unsigned char)*cp); 4035ecc953bSthorpej } 4045ecc953bSthorpej a->a_devclass = intern(classenum); 4055ecc953bSthorpej 4065ecc953bSthorpej return (0); 4075ecc953bSthorpej } 4085ecc953bSthorpej 4095ecc953bSthorpej /* 4105ecc953bSthorpej * Return true if the given `error object' is embedded in the given 4115ecc953bSthorpej * pointer list. 4125ecc953bSthorpej */ 4135ecc953bSthorpej static int 4144caf067bSdholland has_errobj(struct attrlist *al, struct attr *obj) 4155ecc953bSthorpej { 4165ecc953bSthorpej 4174caf067bSdholland for (; al != NULL; al = al->al_next) 4184caf067bSdholland if (al->al_this == obj) 4195ecc953bSthorpej return (1); 4205ecc953bSthorpej return (0); 4215ecc953bSthorpej } 4225ecc953bSthorpej 4235ecc953bSthorpej /* 4245ecc953bSthorpej * Return true if the given attribute is embedded in the given 4255ecc953bSthorpej * pointer list. 4265ecc953bSthorpej */ 4275ecc953bSthorpej int 4284caf067bSdholland has_attr(struct attrlist *al, const char *attr) 4295ecc953bSthorpej { 4305ecc953bSthorpej struct attr *a; 4315ecc953bSthorpej 4325ecc953bSthorpej if ((a = getattr(attr)) == NULL) 4335ecc953bSthorpej return (0); 4345ecc953bSthorpej 4354caf067bSdholland for (; al != NULL; al = al->al_next) 4364caf067bSdholland if (al->al_this == a) 4375ecc953bSthorpej return (1); 4385ecc953bSthorpej return (0); 4395ecc953bSthorpej } 4405ecc953bSthorpej 4415ecc953bSthorpej /* 4425ecc953bSthorpej * Add a device base to a list in an attribute (actually, to any list). 4435ecc953bSthorpej * Note that this does not check for duplicates, and does reverse the 4445ecc953bSthorpej * list order, but no one cares anyway. 4455ecc953bSthorpej */ 4465ecc953bSthorpej static struct nvlist * 4475ecc953bSthorpej addtoattr(struct nvlist *l, struct devbase *dev) 4485ecc953bSthorpej { 4495ecc953bSthorpej struct nvlist *n; 4505ecc953bSthorpej 4515ecc953bSthorpej n = newnv(NULL, NULL, dev, 0, l); 4525ecc953bSthorpej return (n); 4535ecc953bSthorpej } 4545ecc953bSthorpej 4555ecc953bSthorpej /* 4565ecc953bSthorpej * Define a device. This may (or may not) also define an interface 4575ecc953bSthorpej * attribute and/or refer to existing attributes. 4585ecc953bSthorpej */ 4595ecc953bSthorpej void 46016a8771bSdholland defdev(struct devbase *dev, struct loclist *loclist, struct attrlist *attrs, 4615ecc953bSthorpej int ispseudo) 4625ecc953bSthorpej { 46316a8771bSdholland struct loclist *ll; 4644caf067bSdholland struct attrlist *al; 4655ecc953bSthorpej 4665ecc953bSthorpej if (dev == &errdev) 4675ecc953bSthorpej goto bad; 4685ecc953bSthorpej if (dev->d_isdef) { 469c7295a4cSchristos cfgerror("redefinition of `%s'", dev->d_name); 4705ecc953bSthorpej goto bad; 4715ecc953bSthorpej } 4725ecc953bSthorpej 4735ecc953bSthorpej dev->d_isdef = 1; 4745ecc953bSthorpej if (has_errobj(attrs, &errattr)) 4755ecc953bSthorpej goto bad; 4765ecc953bSthorpej 4775ecc953bSthorpej /* 4785ecc953bSthorpej * Handle implicit attribute definition from locator list. Do 4795ecc953bSthorpej * this before scanning the `at' list so that we can have, e.g.: 4805ecc953bSthorpej * device foo at other, foo { slot = -1 } 4815ecc953bSthorpej * (where you can plug in a foo-bus extender to a foo-bus). 4825ecc953bSthorpej */ 4835ecc953bSthorpej if (loclist != NULL) { 48416a8771bSdholland ll = loclist; 4855ecc953bSthorpej loclist = NULL; /* defattr disposes of them for us */ 486bb4307a6Suebayasi if (defiattr(dev->d_name, ll, NULL, 0)) 4875ecc953bSthorpej goto bad; 4884caf067bSdholland attrs = attrlist_cons(attrs, getattr(dev->d_name)); 4894caf067bSdholland /* This used to be stored but was never used */ 4904caf067bSdholland /* attrs->al_name = dev->d_name; */ 491c454f920Scube } 492c454f920Scube 493c130d400Scube /* 494c130d400Scube * Pseudo-devices can have children. Consider them as 495c130d400Scube * attaching at root. 496c130d400Scube */ 497c454f920Scube if (ispseudo) { 4984caf067bSdholland for (al = attrs; al != NULL; al = al->al_next) 4994caf067bSdholland if (al->al_this->a_iattr) 500c454f920Scube break; 5014caf067bSdholland if (al != NULL) { 502b66156c7Sdrochner if (ispseudo < 2) { 503b66156c7Sdrochner if (version >= 20080610) 504b66156c7Sdrochner cfgerror("interface attribute on " 505b66156c7Sdrochner "non-device pseudo `%s'", dev->d_name); 506b66156c7Sdrochner else { 507b66156c7Sdrochner ispseudo = 2; 508b66156c7Sdrochner } 509b66156c7Sdrochner } 510c130d400Scube ht_insert(devroottab, dev->d_name, dev); 5115ecc953bSthorpej } 512b66156c7Sdrochner } 5135ecc953bSthorpej 5145ecc953bSthorpej /* Committed! Set up fields. */ 5155ecc953bSthorpej dev->d_ispseudo = ispseudo; 5165ecc953bSthorpej dev->d_attrs = attrs; 5175ecc953bSthorpej dev->d_classattr = NULL; /* for now */ 518da706167Suebayasi CFGDBG(3, "dev `%s' defined", dev->d_name); 5195ecc953bSthorpej 5205ecc953bSthorpej /* 521a2dd9676Suebayasi * Implicit attribute definition for device. 522a2dd9676Suebayasi */ 52301b2a130Smartin refattr(dev->d_name); 524a2dd9676Suebayasi 525a2dd9676Suebayasi /* 5265ecc953bSthorpej * For each interface attribute this device refers to, add this 5275ecc953bSthorpej * device to its reference list. This makes, e.g., finding all 5285ecc953bSthorpej * "scsi"s easier. 5295ecc953bSthorpej * 5305ecc953bSthorpej * While looking through the attributes, set up the device 5315ecc953bSthorpej * class if any are devclass attributes (and error out if the 5325ecc953bSthorpej * device has two classes). 5335ecc953bSthorpej */ 5344caf067bSdholland for (al = attrs; al != NULL; al = al->al_next) { 535da706167Suebayasi /* 536da706167Suebayasi * Implicit attribute definition for device dependencies. 537da706167Suebayasi */ 538bc707469Suebayasi refattr(al->al_this->a_name); 5396d1bba4aSuebayasi (void)ht_insert2(attrdeptab, dev->d_name, al->al_this->a_name, NULL); 540bc707469Suebayasi CFGDBG(2, "device `%s' depends on attr `%s'", dev->d_name, 541bc707469Suebayasi al->al_this->a_name); 5425ecc953bSthorpej } 5435ecc953bSthorpej return; 5445ecc953bSthorpej bad: 54516a8771bSdholland loclist_destroy(loclist); 5464caf067bSdholland attrlist_destroyall(attrs); 5475ecc953bSthorpej } 5485ecc953bSthorpej 5495ecc953bSthorpej /* 5505ecc953bSthorpej * Look up a devbase. Also makes sure it is a reasonable name, 5515ecc953bSthorpej * i.e., does not end in a digit or contain special characters. 5525ecc953bSthorpej */ 5535ecc953bSthorpej struct devbase * 5545ecc953bSthorpej getdevbase(const char *name) 5555ecc953bSthorpej { 556c7295a4cSchristos const u_char *p; 5575ecc953bSthorpej struct devbase *dev; 5585ecc953bSthorpej 559c7295a4cSchristos p = (const u_char *)name; 5605ecc953bSthorpej if (!isalpha(*p)) 5615ecc953bSthorpej goto badname; 5625ecc953bSthorpej while (*++p) { 5635ecc953bSthorpej if (!isalnum(*p) && *p != '_') 5645ecc953bSthorpej goto badname; 5655ecc953bSthorpej } 5665ecc953bSthorpej if (isdigit(*--p)) { 5675ecc953bSthorpej badname: 568c7295a4cSchristos cfgerror("bad device base name `%s'", name); 5695ecc953bSthorpej return (&errdev); 5705ecc953bSthorpej } 5715ecc953bSthorpej dev = ht_lookup(devbasetab, name); 5725ecc953bSthorpej if (dev == NULL) { 5735ecc953bSthorpej dev = ecalloc(1, sizeof *dev); 5745ecc953bSthorpej dev->d_name = name; 5755ecc953bSthorpej dev->d_isdef = 0; 576d767912bSdrochner dev->d_major = NODEVMAJOR; 5775ecc953bSthorpej dev->d_attrs = NULL; 5785ecc953bSthorpej dev->d_ihead = NULL; 5795ecc953bSthorpej dev->d_ipp = &dev->d_ihead; 5805ecc953bSthorpej dev->d_ahead = NULL; 5815ecc953bSthorpej dev->d_app = &dev->d_ahead; 5825ecc953bSthorpej dev->d_umax = 0; 5835ecc953bSthorpej TAILQ_INSERT_TAIL(&allbases, dev, d_next); 5845ecc953bSthorpej if (ht_insert(devbasetab, name, dev)) 5855ecc953bSthorpej panic("getdevbase(%s)", name); 586da706167Suebayasi CFGDBG(3, "devbase defined `%s'", dev->d_name); 5875ecc953bSthorpej } 5885ecc953bSthorpej return (dev); 5895ecc953bSthorpej } 5905ecc953bSthorpej 5915ecc953bSthorpej /* 5925ecc953bSthorpej * Define some of a device's allowable parent attachments. 5935ecc953bSthorpej * There may be a list of (plain) attributes. 5945ecc953bSthorpej */ 5955ecc953bSthorpej void 5965ecc953bSthorpej defdevattach(struct deva *deva, struct devbase *dev, struct nvlist *atlist, 5974caf067bSdholland struct attrlist *attrs) 5985ecc953bSthorpej { 5995ecc953bSthorpej struct nvlist *nv; 6004caf067bSdholland struct attrlist *al; 6015ecc953bSthorpej struct attr *a; 6025ecc953bSthorpej struct deva *da; 6035ecc953bSthorpej 6045ecc953bSthorpej if (dev == &errdev) 6055ecc953bSthorpej goto bad; 6065ecc953bSthorpej if (deva == NULL) 6075ecc953bSthorpej deva = getdevattach(dev->d_name); 6085ecc953bSthorpej if (deva == &errdeva) 6095ecc953bSthorpej goto bad; 6105ecc953bSthorpej if (!dev->d_isdef) { 611c7295a4cSchristos cfgerror("attaching undefined device `%s'", dev->d_name); 6125ecc953bSthorpej goto bad; 6135ecc953bSthorpej } 6145ecc953bSthorpej if (deva->d_isdef) { 615c7295a4cSchristos cfgerror("redefinition of `%s'", deva->d_name); 6165ecc953bSthorpej goto bad; 6175ecc953bSthorpej } 6185ecc953bSthorpej if (dev->d_ispseudo) { 619c7295a4cSchristos cfgerror("pseudo-devices can't attach"); 6205ecc953bSthorpej goto bad; 6215ecc953bSthorpej } 6225ecc953bSthorpej 6235ecc953bSthorpej deva->d_isdef = 1; 6245ecc953bSthorpej if (has_errobj(attrs, &errattr)) 6255ecc953bSthorpej goto bad; 6264caf067bSdholland for (al = attrs; al != NULL; al = al->al_next) { 6274caf067bSdholland a = al->al_this; 6285ecc953bSthorpej if (a == &errattr) 6295ecc953bSthorpej continue; /* already complained */ 6305ecc953bSthorpej if (a->a_iattr || a->a_devclass != NULL) 631c7295a4cSchristos cfgerror("`%s' is not a plain attribute", a->a_name); 6325ecc953bSthorpej } 6335ecc953bSthorpej 6345ecc953bSthorpej /* Committed! Set up fields. */ 6355ecc953bSthorpej deva->d_attrs = attrs; 6365ecc953bSthorpej deva->d_atlist = atlist; 6375ecc953bSthorpej deva->d_devbase = dev; 6382f2f3382Suebayasi CFGDBG(3, "deva `%s' defined", deva->d_name); 6392f2f3382Suebayasi 6402f2f3382Suebayasi /* 641a5a68b71Suebayasi * Implicit attribute definition for device attachment. 6422f2f3382Suebayasi */ 6432f2f3382Suebayasi refattr(deva->d_name); 6445ecc953bSthorpej 6455ecc953bSthorpej /* 6465ecc953bSthorpej * Turn the `at' list into interface attributes (map each 6475ecc953bSthorpej * nv_name to an attribute, or to NULL for root), and add 6485ecc953bSthorpej * this device to those attributes, so that children can 6495ecc953bSthorpej * be listed at this particular device if they are supported 6505ecc953bSthorpej * by that attribute. 6515ecc953bSthorpej */ 6525ecc953bSthorpej for (nv = atlist; nv != NULL; nv = nv->nv_next) { 6535ecc953bSthorpej if (nv->nv_name == NULL) 6545ecc953bSthorpej nv->nv_ptr = a = NULL; /* at root */ 6555ecc953bSthorpej else 6565ecc953bSthorpej nv->nv_ptr = a = getattr(nv->nv_name); 6575ecc953bSthorpej if (a == &errattr) 6585ecc953bSthorpej continue; /* already complained */ 6595ecc953bSthorpej 6605ecc953bSthorpej /* 6615ecc953bSthorpej * Make sure that an attachment spec doesn't 6625ecc953bSthorpej * already say how to attach to this attribute. 6635ecc953bSthorpej */ 6645ecc953bSthorpej for (da = dev->d_ahead; da != NULL; da = da->d_bsame) 6655ecc953bSthorpej if (onlist(da->d_atlist, a)) 666c7295a4cSchristos cfgerror("attach at `%s' already done by `%s'", 6675ecc953bSthorpej a ? a->a_name : "root", da->d_name); 6685ecc953bSthorpej 669c130d400Scube if (a == NULL) { 670c130d400Scube ht_insert(devroottab, dev->d_name, dev); 6715ecc953bSthorpej continue; /* at root; don't add */ 672c130d400Scube } 6735ecc953bSthorpej if (!a->a_iattr) 674c7295a4cSchristos cfgerror("%s cannot be at plain attribute `%s'", 6755ecc953bSthorpej dev->d_name, a->a_name); 6765ecc953bSthorpej else 6775ecc953bSthorpej a->a_devs = addtoattr(a->a_devs, dev); 6785ecc953bSthorpej } 6795ecc953bSthorpej 6805ecc953bSthorpej /* attach to parent */ 6815ecc953bSthorpej *dev->d_app = deva; 6825ecc953bSthorpej dev->d_app = &deva->d_bsame; 6835ecc953bSthorpej return; 6845ecc953bSthorpej bad: 6855ecc953bSthorpej nvfreel(atlist); 6864caf067bSdholland attrlist_destroyall(attrs); 6875ecc953bSthorpej } 6885ecc953bSthorpej 6895ecc953bSthorpej /* 6905ecc953bSthorpej * Look up a device attachment. Also makes sure it is a reasonable 6915ecc953bSthorpej * name, i.e., does not contain digits or special characters. 6925ecc953bSthorpej */ 6935ecc953bSthorpej struct deva * 6945ecc953bSthorpej getdevattach(const char *name) 6955ecc953bSthorpej { 696c7295a4cSchristos const u_char *p; 6975ecc953bSthorpej struct deva *deva; 6985ecc953bSthorpej 699c7295a4cSchristos p = (const u_char *)name; 7005ecc953bSthorpej if (!isalpha(*p)) 7015ecc953bSthorpej goto badname; 7025ecc953bSthorpej while (*++p) { 7035ecc953bSthorpej if (!isalnum(*p) && *p != '_') 7045ecc953bSthorpej goto badname; 7055ecc953bSthorpej } 7065ecc953bSthorpej if (isdigit(*--p)) { 7075ecc953bSthorpej badname: 708c7295a4cSchristos cfgerror("bad device attachment name `%s'", name); 7095ecc953bSthorpej return (&errdeva); 7105ecc953bSthorpej } 7115ecc953bSthorpej deva = ht_lookup(devatab, name); 7125ecc953bSthorpej if (deva == NULL) { 7135ecc953bSthorpej deva = ecalloc(1, sizeof *deva); 7145ecc953bSthorpej deva->d_name = name; 7155ecc953bSthorpej deva->d_bsame = NULL; 7165ecc953bSthorpej deva->d_isdef = 0; 7175ecc953bSthorpej deva->d_devbase = NULL; 7185ecc953bSthorpej deva->d_atlist = NULL; 7195ecc953bSthorpej deva->d_attrs = NULL; 7205ecc953bSthorpej deva->d_ihead = NULL; 7215ecc953bSthorpej deva->d_ipp = &deva->d_ihead; 7225ecc953bSthorpej TAILQ_INSERT_TAIL(&alldevas, deva, d_next); 7235ecc953bSthorpej if (ht_insert(devatab, name, deva)) 7245ecc953bSthorpej panic("getdeva(%s)", name); 7255ecc953bSthorpej } 7265ecc953bSthorpej return (deva); 7275ecc953bSthorpej } 7285ecc953bSthorpej 7295ecc953bSthorpej /* 7305ecc953bSthorpej * Look up an attribute. 7315ecc953bSthorpej */ 7325ecc953bSthorpej struct attr * 7335ecc953bSthorpej getattr(const char *name) 7345ecc953bSthorpej { 7355ecc953bSthorpej struct attr *a; 7365ecc953bSthorpej 7375ecc953bSthorpej if ((a = ht_lookup(attrtab, name)) == NULL) { 738c7295a4cSchristos cfgerror("undefined attribute `%s'", name); 7395ecc953bSthorpej a = &errattr; 7405ecc953bSthorpej } 7415ecc953bSthorpej return (a); 7425ecc953bSthorpej } 7435ecc953bSthorpej 7445ecc953bSthorpej /* 7452f2f3382Suebayasi * Implicit attribute definition. 7462f2f3382Suebayasi */ 74751deed3eSuebayasi struct attr * 7482f2f3382Suebayasi refattr(const char *name) 7492f2f3382Suebayasi { 75051deed3eSuebayasi struct attr *a; 7512f2f3382Suebayasi 75251deed3eSuebayasi if ((a = ht_lookup(attrtab, name)) == NULL) 75351deed3eSuebayasi a = mkattr(name); 75451deed3eSuebayasi return a; 7552f2f3382Suebayasi } 7562f2f3382Suebayasi 757bb52c144Suebayasi int 758bb52c144Suebayasi getrefattr(const char *name, struct attr **ra) 759bb52c144Suebayasi { 760bb52c144Suebayasi struct attr *a; 761bb52c144Suebayasi 762bb52c144Suebayasi a = ht_lookup(attrtab, name); 763bb52c144Suebayasi if (a == NULL) { 764bb52c144Suebayasi *ra = NULL; 765bb52c144Suebayasi return (0); 766bb52c144Suebayasi } 767bb52c144Suebayasi /* 768bb52c144Suebayasi * Check if the existing attr is only referenced, not really defined. 769bb52c144Suebayasi */ 770bb52c144Suebayasi if (a->a_deps == NULL && 771bb52c144Suebayasi a->a_iattr == 0 && 772bb52c144Suebayasi a->a_devclass == 0) { 773bb52c144Suebayasi *ra = a; 774bb52c144Suebayasi return (0); 775bb52c144Suebayasi } 776bb52c144Suebayasi return (1); 777bb52c144Suebayasi } 778bb52c144Suebayasi 7792f2f3382Suebayasi /* 7805ecc953bSthorpej * Recursively expand an attribute and its dependencies, checking for 7815ecc953bSthorpej * cycles, and invoking a callback for each attribute found. 7825ecc953bSthorpej */ 7835ecc953bSthorpej void 7845ecc953bSthorpej expandattr(struct attr *a, void (*callback)(struct attr *)) 7855ecc953bSthorpej { 7864caf067bSdholland struct attrlist *al; 7875ecc953bSthorpej struct attr *dep; 7885ecc953bSthorpej 7895ecc953bSthorpej if (a->a_expanding) { 790c7295a4cSchristos cfgerror("circular dependency on attribute `%s'", a->a_name); 7915ecc953bSthorpej return; 7925ecc953bSthorpej } 7935ecc953bSthorpej 7945ecc953bSthorpej a->a_expanding = 1; 7955ecc953bSthorpej 7965ecc953bSthorpej /* First expand all of this attribute's dependencies. */ 7974caf067bSdholland for (al = a->a_deps; al != NULL; al = al->al_next) { 7984caf067bSdholland dep = al->al_this; 7995ecc953bSthorpej expandattr(dep, callback); 8005ecc953bSthorpej } 8015ecc953bSthorpej 8025ecc953bSthorpej /* ...and now invoke the callback for ourself. */ 8035ecc953bSthorpej if (callback != NULL) 8045ecc953bSthorpej (*callback)(a); 8055ecc953bSthorpej 8065ecc953bSthorpej a->a_expanding = 0; 8075ecc953bSthorpej } 8085ecc953bSthorpej 8095ecc953bSthorpej /* 8105ecc953bSthorpej * Set the major device number for a device, so that it can be used 8115ecc953bSthorpej * as a root/dumps "on" device in a configuration. 8125ecc953bSthorpej */ 8135ecc953bSthorpej void 814d767912bSdrochner setmajor(struct devbase *d, devmajor_t n) 8155ecc953bSthorpej { 8165ecc953bSthorpej 817d767912bSdrochner if (d != &errdev && d->d_major != NODEVMAJOR) 818d767912bSdrochner cfgerror("device `%s' is already major %d", 819d767912bSdrochner d->d_name, d->d_major); 8205ecc953bSthorpej else 8215ecc953bSthorpej d->d_major = n; 8225ecc953bSthorpej } 8235ecc953bSthorpej 8245ecc953bSthorpej const char * 825d767912bSdrochner major2name(devmajor_t maj) 8265ecc953bSthorpej { 8275ecc953bSthorpej struct devbase *dev; 8285ecc953bSthorpej struct devm *dm; 8295ecc953bSthorpej 8305ecc953bSthorpej if (!do_devsw) { 8315ecc953bSthorpej TAILQ_FOREACH(dev, &allbases, d_next) { 8325ecc953bSthorpej if (dev->d_major == maj) 8335ecc953bSthorpej return (dev->d_name); 8345ecc953bSthorpej } 8355ecc953bSthorpej } else { 8365ecc953bSthorpej TAILQ_FOREACH(dm, &alldevms, dm_next) { 8375ecc953bSthorpej if (dm->dm_bmajor == maj) 8385ecc953bSthorpej return (dm->dm_name); 8395ecc953bSthorpej } 8405ecc953bSthorpej } 8415ecc953bSthorpej return (NULL); 8425ecc953bSthorpej } 8435ecc953bSthorpej 844d767912bSdrochner devmajor_t 8455ecc953bSthorpej dev2major(struct devbase *dev) 8465ecc953bSthorpej { 8475ecc953bSthorpej struct devm *dm; 8485ecc953bSthorpej 8495ecc953bSthorpej if (!do_devsw) 8505ecc953bSthorpej return (dev->d_major); 8515ecc953bSthorpej 8525ecc953bSthorpej TAILQ_FOREACH(dm, &alldevms, dm_next) { 8535ecc953bSthorpej if (strcmp(dm->dm_name, dev->d_name) == 0) 8545ecc953bSthorpej return (dm->dm_bmajor); 8555ecc953bSthorpej } 856d767912bSdrochner return (NODEVMAJOR); 8575ecc953bSthorpej } 8585ecc953bSthorpej 8595ecc953bSthorpej /* 8605ecc953bSthorpej * Make a string description of the device at maj/min. 8615ecc953bSthorpej */ 8625ecc953bSthorpej static const char * 863d767912bSdrochner makedevstr(devmajor_t maj, devminor_t min) 8645ecc953bSthorpej { 865c7295a4cSchristos const char *devicename; 8665ecc953bSthorpej char buf[32]; 8675ecc953bSthorpej 868c7295a4cSchristos devicename = major2name(maj); 869c7295a4cSchristos if (devicename == NULL) 870d767912bSdrochner (void)snprintf(buf, sizeof(buf), "<%d/%d>", maj, min); 8715ecc953bSthorpej else 872d767912bSdrochner (void)snprintf(buf, sizeof(buf), "%s%d%c", devicename, 873d767912bSdrochner min / maxpartitions, (min % maxpartitions) + 'a'); 8745ecc953bSthorpej 8755ecc953bSthorpej return (intern(buf)); 8765ecc953bSthorpej } 8775ecc953bSthorpej 8785ecc953bSthorpej /* 8795ecc953bSthorpej * Map things like "ra0b" => makedev(major("ra"), 0*maxpartitions + 'b'-'a'). 8805ecc953bSthorpej * Handle the case where the device number is given but there is no 8815ecc953bSthorpej * corresponding name, and map NULL to the default. 8825ecc953bSthorpej */ 8835ecc953bSthorpej static int 8845ecc953bSthorpej resolve(struct nvlist **nvp, const char *name, const char *what, 8855ecc953bSthorpej struct nvlist *dflt, int part) 8865ecc953bSthorpej { 8875ecc953bSthorpej struct nvlist *nv; 8885ecc953bSthorpej struct devbase *dev; 8895ecc953bSthorpej const char *cp; 890d767912bSdrochner devmajor_t maj; 891d767912bSdrochner devminor_t min; 8929a7f4bbaSchristos size_t i, l; 8935ecc953bSthorpej int unit; 8945ecc953bSthorpej char buf[NAMESIZE]; 8955ecc953bSthorpej 8965cc303e1Slukem if ((part -= 'a') >= maxpartitions || part < 0) 8975ecc953bSthorpej panic("resolve"); 8985ecc953bSthorpej if ((nv = *nvp) == NULL) { 8995ecc953bSthorpej dev_t d = NODEV; 9005ecc953bSthorpej /* 9015ecc953bSthorpej * Apply default. Easiest to do this by number. 9025ecc953bSthorpej * Make sure to retain NODEVness, if this is dflt's disposition. 9035ecc953bSthorpej */ 9045cc303e1Slukem if ((dev_t)dflt->nv_num != NODEV) { 9050001b928Schristos maj = major(dflt->nv_num); 9060001b928Schristos min = ((minor(dflt->nv_num) / maxpartitions) * 9075ecc953bSthorpej maxpartitions) + part; 9085ecc953bSthorpej d = makedev(maj, min); 9095ecc953bSthorpej cp = makedevstr(maj, min); 9105ecc953bSthorpej } else 9115ecc953bSthorpej cp = NULL; 9129a7f4bbaSchristos *nvp = nv = newnv(NULL, cp, NULL, (long long)d, NULL); 9135ecc953bSthorpej } 9145cc303e1Slukem if ((dev_t)nv->nv_num != NODEV) { 9155ecc953bSthorpej /* 9165ecc953bSthorpej * By the numbers. Find the appropriate major number 9175ecc953bSthorpej * to make a name. 9185ecc953bSthorpej */ 9190001b928Schristos maj = major(nv->nv_num); 9200001b928Schristos min = minor(nv->nv_num); 9215ecc953bSthorpej nv->nv_str = makedevstr(maj, min); 9225ecc953bSthorpej return (0); 9235ecc953bSthorpej } 9245ecc953bSthorpej 9255ecc953bSthorpej if (nv->nv_str == NULL || nv->nv_str == s_qmark) 9265ecc953bSthorpej /* 9275ecc953bSthorpej * Wildcarded or unspecified; leave it as NODEV. 9285ecc953bSthorpej */ 9295ecc953bSthorpej return (0); 9305ecc953bSthorpej 931*79cd2972Smlelstv if (nv->nv_ptr != NULL && strcmp(nv->nv_ptr, "spec") == 0) 932*79cd2972Smlelstv /* 933*79cd2972Smlelstv * spec string, interpreted by kernel 934*79cd2972Smlelstv */ 935*79cd2972Smlelstv return (0); 936*79cd2972Smlelstv 9375ecc953bSthorpej /* 9385ecc953bSthorpej * The normal case: things like "ra2b". Check for partition 9395ecc953bSthorpej * suffix, remove it if there, and split into name ("ra") and 9405ecc953bSthorpej * unit (2). 9415ecc953bSthorpej */ 9425ecc953bSthorpej l = i = strlen(nv->nv_str); 9435ecc953bSthorpej cp = &nv->nv_str[l]; 9445ecc953bSthorpej if (l > 1 && *--cp >= 'a' && *cp < 'a' + maxpartitions && 9455ecc953bSthorpej isdigit((unsigned char)cp[-1])) { 9465ecc953bSthorpej l--; 9475ecc953bSthorpej part = *cp - 'a'; 9485ecc953bSthorpej } 9495ecc953bSthorpej cp = nv->nv_str; 9505ecc953bSthorpej if (split(cp, l, buf, sizeof buf, &unit)) { 951c7295a4cSchristos cfgerror("%s: invalid %s device name `%s'", name, what, cp); 9525ecc953bSthorpej return (1); 9535ecc953bSthorpej } 9545ecc953bSthorpej dev = ht_lookup(devbasetab, intern(buf)); 9555ecc953bSthorpej if (dev == NULL) { 956c7295a4cSchristos cfgerror("%s: device `%s' does not exist", name, buf); 9575ecc953bSthorpej return (1); 9585ecc953bSthorpej } 9595ecc953bSthorpej 9605ecc953bSthorpej /* 9615ecc953bSthorpej * Check for the magic network interface attribute, and 9625ecc953bSthorpej * don't bother making a device number. 9635ecc953bSthorpej */ 9645ecc953bSthorpej if (has_attr(dev->d_attrs, s_ifnet)) { 9659a7f4bbaSchristos nv->nv_num = (long long)NODEV; 9665ecc953bSthorpej nv->nv_ifunit = unit; /* XXX XXX XXX */ 9675ecc953bSthorpej } else { 9685ecc953bSthorpej maj = dev2major(dev); 9695cc303e1Slukem if (maj == NODEVMAJOR) { 970c7295a4cSchristos cfgerror("%s: can't make %s device from `%s'", 9715ecc953bSthorpej name, what, nv->nv_str); 9725ecc953bSthorpej return (1); 9735ecc953bSthorpej } 9745beb9d5aSjoerg nv->nv_num = (long long)makedev(maj, unit * maxpartitions + part); 9755ecc953bSthorpej } 9765ecc953bSthorpej 9775ecc953bSthorpej nv->nv_name = dev->d_name; 9785ecc953bSthorpej return (0); 9795ecc953bSthorpej } 9805ecc953bSthorpej 9815ecc953bSthorpej /* 9825ecc953bSthorpej * Add a completed configuration to the list. 9835ecc953bSthorpej */ 9845ecc953bSthorpej void 9855ecc953bSthorpej addconf(struct config *cf0) 9865ecc953bSthorpej { 9875ecc953bSthorpej struct config *cf; 9885ecc953bSthorpej const char *name; 9895ecc953bSthorpej 9905ecc953bSthorpej name = cf0->cf_name; 9915ecc953bSthorpej cf = ecalloc(1, sizeof *cf); 9925ecc953bSthorpej if (ht_insert(cfhashtab, name, cf)) { 993c7295a4cSchristos cfgerror("configuration `%s' already defined", name); 9945ecc953bSthorpej free(cf); 9955ecc953bSthorpej goto bad; 9965ecc953bSthorpej } 9975ecc953bSthorpej *cf = *cf0; 9985ecc953bSthorpej 9995ecc953bSthorpej /* 10005ecc953bSthorpej * Resolve the root device. 10015ecc953bSthorpej */ 1002cd429362Serh if (cf->cf_root == NULL) { 1003c7295a4cSchristos cfgerror("%s: no root device specified", name); 10045ecc953bSthorpej goto bad; 10055ecc953bSthorpej } 1006cd429362Serh if (cf->cf_root && cf->cf_root->nv_str != s_qmark) { 1007cd429362Serh struct nvlist *nv; 1008cd429362Serh nv = cf->cf_root; 10095ecc953bSthorpej if (resolve(&cf->cf_root, name, "root", nv, 'a')) 10105ecc953bSthorpej goto bad; 10115ecc953bSthorpej } 10125ecc953bSthorpej 10135ecc953bSthorpej /* 10145ecc953bSthorpej * Resolve the dump device. 10155ecc953bSthorpej */ 10165ecc953bSthorpej if (cf->cf_dump == NULL || cf->cf_dump->nv_str == s_qmark) { 10175ecc953bSthorpej /* 10185ecc953bSthorpej * Wildcarded dump device is equivalent to unspecified. 10195ecc953bSthorpej */ 10205ecc953bSthorpej cf->cf_dump = NULL; 10215ecc953bSthorpej } else if (cf->cf_dump->nv_str == s_none) { 10225ecc953bSthorpej /* 10235ecc953bSthorpej * Operator has requested that no dump device should be 10245ecc953bSthorpej * configured; do nothing. 10255ecc953bSthorpej */ 10265ecc953bSthorpej } else { 10275ecc953bSthorpej if (resolve(&cf->cf_dump, name, "dumps", cf->cf_dump, 'b')) 10285ecc953bSthorpej goto bad; 10295ecc953bSthorpej } 10305ecc953bSthorpej 10315ecc953bSthorpej /* Wildcarded fstype is `unspecified'. */ 10325ecc953bSthorpej if (cf->cf_fstype == s_qmark) 10335ecc953bSthorpej cf->cf_fstype = NULL; 10345ecc953bSthorpej 10355ecc953bSthorpej TAILQ_INSERT_TAIL(&allcf, cf, cf_next); 10365ecc953bSthorpej return; 10375ecc953bSthorpej bad: 10385ecc953bSthorpej nvfreel(cf0->cf_root); 10395ecc953bSthorpej nvfreel(cf0->cf_dump); 10405ecc953bSthorpej } 10415ecc953bSthorpej 10425ecc953bSthorpej void 10435ecc953bSthorpej setconf(struct nvlist **npp, const char *what, struct nvlist *v) 10445ecc953bSthorpej { 10455ecc953bSthorpej 10465ecc953bSthorpej if (*npp != NULL) { 1047c7295a4cSchristos cfgerror("duplicate %s specification", what); 10485ecc953bSthorpej nvfreel(v); 10495ecc953bSthorpej } else 10505ecc953bSthorpej *npp = v; 10515ecc953bSthorpej } 10525ecc953bSthorpej 10535ecc953bSthorpej void 105442b52b8aScube delconf(const char *name) 105542b52b8aScube { 105642b52b8aScube struct config *cf; 105742b52b8aScube 105875eb07f2Suebayasi CFGDBG(5, "deselecting config `%s'", name); 105942b52b8aScube if (ht_lookup(cfhashtab, name) == NULL) { 1060c7295a4cSchristos cfgerror("configuration `%s' undefined", name); 106142b52b8aScube return; 106242b52b8aScube } 106342b52b8aScube (void)ht_remove(cfhashtab, name); 106442b52b8aScube 106542b52b8aScube TAILQ_FOREACH(cf, &allcf, cf_next) 106642b52b8aScube if (!strcmp(cf->cf_name, name)) 106742b52b8aScube break; 106842b52b8aScube if (cf == NULL) 106942b52b8aScube panic("lost configuration `%s'", name); 107042b52b8aScube 107142b52b8aScube TAILQ_REMOVE(&allcf, cf, cf_next); 107242b52b8aScube } 107342b52b8aScube 107442b52b8aScube void 10755ecc953bSthorpej setfstype(const char **fstp, const char *v) 10765ecc953bSthorpej { 10775ecc953bSthorpej 10785ecc953bSthorpej if (*fstp != NULL) { 1079c7295a4cSchristos cfgerror("multiple fstype specifications"); 10805ecc953bSthorpej return; 10815ecc953bSthorpej } 10825ecc953bSthorpej 10835ecc953bSthorpej if (v != s_qmark && OPT_FSOPT(v)) { 1084c7295a4cSchristos cfgerror("\"%s\" is not a configured file system", v); 10855ecc953bSthorpej return; 10865ecc953bSthorpej } 10875ecc953bSthorpej 10885ecc953bSthorpej *fstp = v; 10895ecc953bSthorpej } 10905ecc953bSthorpej 10915ecc953bSthorpej static struct devi * 10925ecc953bSthorpej newdevi(const char *name, int unit, struct devbase *d) 10935ecc953bSthorpej { 10945ecc953bSthorpej struct devi *i; 10955ecc953bSthorpej 10965ecc953bSthorpej i = ecalloc(1, sizeof *i); 10975ecc953bSthorpej i->i_name = name; 10985ecc953bSthorpej i->i_unit = unit; 10995ecc953bSthorpej i->i_base = d; 11005ecc953bSthorpej i->i_bsame = NULL; 11015ecc953bSthorpej i->i_asame = NULL; 11025ecc953bSthorpej i->i_alias = NULL; 11035ecc953bSthorpej i->i_at = NULL; 11045ecc953bSthorpej i->i_pspec = NULL; 11055ecc953bSthorpej i->i_atdeva = NULL; 11065ecc953bSthorpej i->i_locs = NULL; 11075ecc953bSthorpej i->i_cfflags = 0; 11085ecc953bSthorpej i->i_lineno = currentline(); 11090dbd1c0eScube i->i_srcfile = yyfile; 11107aa6070dScube i->i_active = DEVI_ORPHAN; /* Proper analysis comes later */ 1111a31ff6b4Scube i->i_level = devilevel; 111290ac64deSpooka i->i_pseudoroot = 0; 11135ecc953bSthorpej if (unit >= d->d_umax) 11145ecc953bSthorpej d->d_umax = unit + 1; 11155ecc953bSthorpej return (i); 11165ecc953bSthorpej } 11175ecc953bSthorpej 11185ecc953bSthorpej /* 11195ecc953bSthorpej * Add the named device as attaching to the named attribute (or perhaps 11205ecc953bSthorpej * another device instead) plus unit number. 11215ecc953bSthorpej */ 11225ecc953bSthorpej void 112316a8771bSdholland adddev(const char *name, const char *at, struct loclist *loclist, int flags) 11245ecc953bSthorpej { 11255ecc953bSthorpej struct devi *i; /* the new instance */ 11265ecc953bSthorpej struct pspec *p; /* and its pspec */ 11275ecc953bSthorpej struct attr *attr; /* attribute that allows attach */ 11285ecc953bSthorpej struct devbase *ib; /* i->i_base */ 11295ecc953bSthorpej struct devbase *ab; /* not NULL => at another dev */ 11304caf067bSdholland struct attrlist *al; 11315ecc953bSthorpej struct deva *iba; /* devbase attachment used */ 11325ecc953bSthorpej const char *cp; 11335ecc953bSthorpej int atunit; 11345ecc953bSthorpej char atbuf[NAMESIZE]; 11355ecc953bSthorpej int hit; 11365ecc953bSthorpej 11375ecc953bSthorpej ab = NULL; 11385ecc953bSthorpej iba = NULL; 11395ecc953bSthorpej if (at == NULL) { 11405ecc953bSthorpej /* "at root" */ 11415ecc953bSthorpej p = NULL; 11425ecc953bSthorpej if ((i = getdevi(name)) == NULL) 11435ecc953bSthorpej goto bad; 11445ecc953bSthorpej /* 11455ecc953bSthorpej * Must warn about i_unit > 0 later, after taking care of 11465ecc953bSthorpej * the STAR cases (we could do non-star's here but why 11475ecc953bSthorpej * bother?). Make sure this device can be at root. 11485ecc953bSthorpej */ 11495ecc953bSthorpej ib = i->i_base; 11505ecc953bSthorpej hit = 0; 11515ecc953bSthorpej for (iba = ib->d_ahead; iba != NULL; iba = iba->d_bsame) 11525ecc953bSthorpej if (onlist(iba->d_atlist, NULL)) { 11535ecc953bSthorpej hit = 1; 11545ecc953bSthorpej break; 11555ecc953bSthorpej } 11565ecc953bSthorpej if (!hit) { 1157c7295a4cSchristos cfgerror("`%s' cannot attach to the root", ib->d_name); 11585ec393e9Scube i->i_active = DEVI_BROKEN; 11595ecc953bSthorpej goto bad; 11605ecc953bSthorpej } 11615ecc953bSthorpej attr = &errattr; /* a convenient "empty" attr */ 11625ecc953bSthorpej } else { 11635ecc953bSthorpej if (split(at, strlen(at), atbuf, sizeof atbuf, &atunit)) { 1164c7295a4cSchristos cfgerror("invalid attachment name `%s'", at); 11655ecc953bSthorpej /* (void)getdevi(name); -- ??? */ 11665ecc953bSthorpej goto bad; 11675ecc953bSthorpej } 11685ecc953bSthorpej if ((i = getdevi(name)) == NULL) 11695ecc953bSthorpej goto bad; 11705ecc953bSthorpej ib = i->i_base; 11715ecc953bSthorpej 11725ecc953bSthorpej /* 11735ecc953bSthorpej * Devices can attach to two types of things: Attributes, 11745ecc953bSthorpej * and other devices (which have the appropriate attributes 11755ecc953bSthorpej * to allow attachment). 11765ecc953bSthorpej * 11775ecc953bSthorpej * (1) If we're attached to an attribute, then we don't need 11785ecc953bSthorpej * look at the parent base device to see what attributes 11795ecc953bSthorpej * it has, and make sure that we can attach to them. 11805ecc953bSthorpej * 11815ecc953bSthorpej * (2) If we're attached to a real device (i.e. named in 11825ecc953bSthorpej * the config file), we want to remember that so that 11835ecc953bSthorpej * at cross-check time, if the device we're attached to 11845ecc953bSthorpej * is missing but other devices which also provide the 11855ecc953bSthorpej * attribute are present, we don't get a false "OK." 11865ecc953bSthorpej * 11875ecc953bSthorpej * (3) If the thing we're attached to is an attribute 11885ecc953bSthorpej * but is actually named in the config file, we still 11895ecc953bSthorpej * have to remember its devbase. 11905ecc953bSthorpej */ 11915ecc953bSthorpej cp = intern(atbuf); 11925ecc953bSthorpej 11935ecc953bSthorpej /* Figure out parent's devbase, to satisfy case (3). */ 11945ecc953bSthorpej ab = ht_lookup(devbasetab, cp); 11955ecc953bSthorpej 11965ecc953bSthorpej /* Find out if it's an attribute. */ 11975ecc953bSthorpej attr = ht_lookup(attrtab, cp); 11985ecc953bSthorpej 11995ecc953bSthorpej /* Make sure we're _really_ attached to the attr. Case (1). */ 12005ecc953bSthorpej if (attr != NULL && onlist(attr->a_devs, ib)) 12015ecc953bSthorpej goto findattachment; 12025ecc953bSthorpej 12035ecc953bSthorpej /* 12045ecc953bSthorpej * Else a real device, and not just an attribute. Case (2). 12055ecc953bSthorpej * 12065ecc953bSthorpej * Have to work a bit harder to see whether we have 12075ecc953bSthorpej * something like "tg0 at esp0" (where esp is merely 12085ecc953bSthorpej * not an attribute) or "tg0 at nonesuch0" (where 12095ecc953bSthorpej * nonesuch is not even a device). 12105ecc953bSthorpej */ 12115ecc953bSthorpej if (ab == NULL) { 1212c7295a4cSchristos cfgerror("%s at %s: `%s' unknown", 12135ecc953bSthorpej name, at, atbuf); 12145ec393e9Scube i->i_active = DEVI_BROKEN; 12155ecc953bSthorpej goto bad; 12165ecc953bSthorpej } 12175ecc953bSthorpej 12185ecc953bSthorpej /* 12195ecc953bSthorpej * See if the named parent carries an attribute 12205ecc953bSthorpej * that allows it to supervise device ib. 12215ecc953bSthorpej */ 12224caf067bSdholland for (al = ab->d_attrs; al != NULL; al = al->al_next) { 12234caf067bSdholland attr = al->al_this; 12245ecc953bSthorpej if (onlist(attr->a_devs, ib)) 12255ecc953bSthorpej goto findattachment; 12265ecc953bSthorpej } 1227c7295a4cSchristos cfgerror("`%s' cannot attach to `%s'", ib->d_name, atbuf); 12285ec393e9Scube i->i_active = DEVI_BROKEN; 12295ecc953bSthorpej goto bad; 12305ecc953bSthorpej 12315ecc953bSthorpej findattachment: 12325ecc953bSthorpej /* 12335ecc953bSthorpej * Find the parent spec. If a matching one has not yet been 12345ecc953bSthorpej * created, create one. 12355ecc953bSthorpej */ 12365ecc953bSthorpej p = getpspec(attr, ab, atunit); 12375ecc953bSthorpej p->p_devs = newnv(NULL, NULL, i, 0, p->p_devs); 12385ecc953bSthorpej 12395ecc953bSthorpej /* find out which attachment it uses */ 12405ecc953bSthorpej hit = 0; 12415ecc953bSthorpej for (iba = ib->d_ahead; iba != NULL; iba = iba->d_bsame) 12425ecc953bSthorpej if (onlist(iba->d_atlist, attr)) { 12435ecc953bSthorpej hit = 1; 12445ecc953bSthorpej break; 12455ecc953bSthorpej } 12465ecc953bSthorpej if (!hit) 12475ecc953bSthorpej panic("adddev: can't figure out attachment"); 12485ecc953bSthorpej } 12495ec393e9Scube if ((i->i_locs = fixloc(name, attr, loclist)) == NULL) { 12505ec393e9Scube i->i_active = DEVI_BROKEN; 12515ecc953bSthorpej goto bad; 12525ec393e9Scube } 12535ecc953bSthorpej i->i_at = at; 12545ecc953bSthorpej i->i_pspec = p; 12555ecc953bSthorpej i->i_atdeva = iba; 12565ecc953bSthorpej i->i_cfflags = flags; 1257bc707469Suebayasi CFGDBG(3, "devi `%s' added", i->i_name); 12585ecc953bSthorpej 12595ecc953bSthorpej *iba->d_ipp = i; 12605ecc953bSthorpej iba->d_ipp = &i->i_asame; 12615ecc953bSthorpej 12625ecc953bSthorpej /* all done, fall into ... */ 12635ecc953bSthorpej bad: 126416a8771bSdholland loclist_destroy(loclist); 12655ecc953bSthorpej return; 12665ecc953bSthorpej } 12675ecc953bSthorpej 12685ecc953bSthorpej void 12697b7c582aScube deldevi(const char *name, const char *at) 12705ecc953bSthorpej { 12717b7c582aScube struct devi *firsti, *i; 12725ecc953bSthorpej struct devbase *d; 12735ecc953bSthorpej int unit; 12745ecc953bSthorpej char base[NAMESIZE]; 12755ecc953bSthorpej 127675eb07f2Suebayasi CFGDBG(5, "deselecting devi `%s'", name); 12775ecc953bSthorpej if (split(name, strlen(name), base, sizeof base, &unit)) { 1278c7295a4cSchristos cfgerror("invalid device name `%s'", name); 12795ecc953bSthorpej return; 12805ecc953bSthorpej } 12815ecc953bSthorpej d = ht_lookup(devbasetab, intern(base)); 12825ecc953bSthorpej if (d == NULL) { 1283c7295a4cSchristos cfgerror("%s: unknown device `%s'", name, base); 12845ecc953bSthorpej return; 12855ecc953bSthorpej } 12865ecc953bSthorpej if (d->d_ispseudo) { 1287c7295a4cSchristos cfgerror("%s: %s is a pseudo-device", name, base); 12885ecc953bSthorpej return; 12895ecc953bSthorpej } 12905ecc953bSthorpej if ((firsti = ht_lookup(devitab, name)) == NULL) { 1291c7295a4cSchristos cfgerror("`%s' not defined", name); 12925ecc953bSthorpej return; 12935ecc953bSthorpej } 12947b7c582aScube if (at == NULL && firsti->i_at == NULL) { 1295e499d8b5Scube /* 'at root' */ 12967b7c582aScube remove_devi(firsti); 12977b7c582aScube return; 12987b7c582aScube } else if (at != NULL) 12997b7c582aScube for (i = firsti; i != NULL; i = i->i_alias) 1300bfc250f2Scube if (i->i_active != DEVI_BROKEN && 1301bfc250f2Scube strcmp(at, i->i_at) == 0) { 13027b7c582aScube remove_devi(i); 13035ecc953bSthorpej return; 13045ecc953bSthorpej } 1305c7295a4cSchristos cfgerror("`%s' at `%s' not found", name, at ? at : "root"); 1306e499d8b5Scube } 13075ecc953bSthorpej 13087b7c582aScube static void 13097b7c582aScube remove_devi(struct devi *i) 13107b7c582aScube { 13117b7c582aScube struct devbase *d = i->i_base; 13127b7c582aScube struct devi *f, *j, **ppi; 13137b7c582aScube struct deva *iba; 13147b7c582aScube 13158d928a75Suebayasi CFGDBG(5, "removing devi `%s'", i->i_name); 13167b7c582aScube f = ht_lookup(devitab, i->i_name); 13176a6299ebScube if (f == NULL) 13186a6299ebScube panic("remove_devi(): instance %s disappeared from devitab", 13196a6299ebScube i->i_name); 13207b7c582aScube 1321bfc250f2Scube if (i->i_active == DEVI_BROKEN) { 1322bfc250f2Scube cfgerror("not removing broken instance `%s'", i->i_name); 1323bfc250f2Scube return; 1324bfc250f2Scube } 1325bfc250f2Scube 1326e499d8b5Scube /* 1327e499d8b5Scube * We have the device instance, i. 1328e499d8b5Scube * We have to: 1329e499d8b5Scube * - delete the alias 1330e499d8b5Scube * 1331e499d8b5Scube * If the devi was an alias of an already listed devi, all is 1332e499d8b5Scube * good we don't have to do more. 1333e499d8b5Scube * If it was the first alias, we have to replace i's entry in 1334e499d8b5Scube * d's list by its first alias. 1335e499d8b5Scube * If it was the only entry, we must remove i's entry from d's 1336e499d8b5Scube * list. 1337e499d8b5Scube */ 13387b7c582aScube if (i != f) { 1339db7dbdacSuebayasi for (j = f; j->i_alias != i; j = j->i_alias) 1340db7dbdacSuebayasi continue; 13417b7c582aScube j->i_alias = i->i_alias; 13423b405775Scube } else { 13433b405775Scube if (i->i_alias == NULL) { 1344e499d8b5Scube /* No alias, must unlink the entry from devitab */ 13457b7c582aScube ht_remove(devitab, i->i_name); 13467b7c582aScube j = i->i_bsame; 13473b405775Scube } else { 1348e499d8b5Scube /* Or have the first alias replace i in d's list */ 1349e499d8b5Scube i->i_alias->i_bsame = i->i_bsame; 13507b7c582aScube j = i->i_alias; 13517b7c582aScube if (i == f) 13527b7c582aScube ht_replace(devitab, i->i_name, i->i_alias); 13533b405775Scube } 13543b405775Scube 1355e499d8b5Scube /* 1356e499d8b5Scube * - remove/replace the instance from the devbase's list 1357e499d8b5Scube * 1358e499d8b5Scube * A double-linked list would make this much easier. Oh, well, 1359e499d8b5Scube * what is done is done. 1360e499d8b5Scube */ 1361e499d8b5Scube for (ppi = &d->d_ihead; 1362e499d8b5Scube *ppi != NULL && *ppi != i && (*ppi)->i_bsame != i; 1363db7dbdacSuebayasi ppi = &(*ppi)->i_bsame) 1364db7dbdacSuebayasi continue; 1365e499d8b5Scube if (*ppi == NULL) 1366c3414672Scube panic("deldev: dev (%s) doesn't list the devi" 1367c3414672Scube " (%s at %s)", d->d_name, i->i_name, i->i_at); 13687b7c582aScube f = *ppi; 13697b7c582aScube if (f == i) 1370c3414672Scube /* That implies d->d_ihead == i */ 13717b7c582aScube *ppi = j; 1372e499d8b5Scube else 13737b7c582aScube (*ppi)->i_bsame = j; 1374e499d8b5Scube if (d->d_ipp == &i->i_bsame) { 13753b405775Scube if (i->i_alias == NULL) { 13767b7c582aScube if (f == i) 1377e499d8b5Scube d->d_ipp = &d->d_ihead; 1378e499d8b5Scube else 13797b7c582aScube d->d_ipp = &f->i_bsame; 13803b405775Scube } else 13813b405775Scube d->d_ipp = &i->i_alias->i_bsame; 1382e499d8b5Scube } 1383e499d8b5Scube } 1384e499d8b5Scube /* 1385e499d8b5Scube * - delete the attachment instance 1386e499d8b5Scube */ 1387e499d8b5Scube iba = i->i_atdeva; 1388e499d8b5Scube for (ppi = &iba->d_ihead; 1389e499d8b5Scube *ppi != NULL && *ppi != i && (*ppi)->i_asame != i; 1390db7dbdacSuebayasi ppi = &(*ppi)->i_asame) 1391db7dbdacSuebayasi continue; 1392e499d8b5Scube if (*ppi == NULL) 1393e499d8b5Scube panic("deldev: deva (%s) doesn't list the devi (%s)", 1394e499d8b5Scube iba->d_name, i->i_name); 13957b7c582aScube f = *ppi; 13967b7c582aScube if (f == i) 1397c3414672Scube /* That implies iba->d_ihead == i */ 1398e499d8b5Scube *ppi = i->i_asame; 1399e499d8b5Scube else 1400e499d8b5Scube (*ppi)->i_asame = i->i_asame; 1401e499d8b5Scube if (iba->d_ipp == &i->i_asame) { 14027b7c582aScube if (f == i) 1403e499d8b5Scube iba->d_ipp = &iba->d_ihead; 1404e499d8b5Scube else 14057b7c582aScube iba->d_ipp = &f->i_asame; 1406e499d8b5Scube } 1407e499d8b5Scube /* 1408e499d8b5Scube * - delete the pspec 1409e499d8b5Scube */ 1410e499d8b5Scube if (i->i_pspec) { 1411e499d8b5Scube struct pspec *p = i->i_pspec; 1412e499d8b5Scube struct nvlist *nv, *onv; 1413e499d8b5Scube 1414e499d8b5Scube /* Double-linked nvlist anyone? */ 1415c0024066Scube for (nv = p->p_devs; nv->nv_next != NULL; nv = nv->nv_next) { 1416e499d8b5Scube if (nv->nv_next && nv->nv_next->nv_ptr == i) { 1417e499d8b5Scube onv = nv->nv_next; 1418e499d8b5Scube nv->nv_next = onv->nv_next; 1419e499d8b5Scube nvfree(onv); 1420e499d8b5Scube break; 1421c0024066Scube } 1422c0024066Scube if (nv->nv_ptr == i) { 1423e499d8b5Scube /* nv is p->p_devs in that case */ 1424e499d8b5Scube p->p_devs = nv->nv_next; 1425e499d8b5Scube nvfree(nv); 1426e499d8b5Scube break; 1427e499d8b5Scube } 1428e499d8b5Scube } 1429e499d8b5Scube if (p->p_devs == NULL) 1430e499d8b5Scube TAILQ_REMOVE(&allpspecs, p, p_list); 1431e499d8b5Scube } 1432e499d8b5Scube /* 1433e499d8b5Scube * - delete the alldevi entry 1434e499d8b5Scube */ 1435e499d8b5Scube TAILQ_REMOVE(&alldevi, i, i_next); 1436e499d8b5Scube ndevi--; 14377aa6070dScube /* 14387aa6070dScube * Put it in deaddevitab 1439a31ff6b4Scube * 1440a31ff6b4Scube * Each time a devi is removed, devilevel is increased so that later on 1441a31ff6b4Scube * it is possible to tell if an instance was added before or after the 1442a31ff6b4Scube * removal of its parent. 1443a31ff6b4Scube * 1444a31ff6b4Scube * For active instances, i_level contains the number of devi removed so 1445a31ff6b4Scube * far, and for dead devis, it contains its index. 14467aa6070dScube */ 1447a31ff6b4Scube i->i_level = devilevel++; 14487aa6070dScube i->i_alias = NULL; 14497aa6070dScube f = ht_lookup(deaddevitab, i->i_name); 14507aa6070dScube if (f == NULL) { 14517aa6070dScube if (ht_insert(deaddevitab, i->i_name, i)) 14527aa6070dScube panic("remove_devi(%s) - can't add to deaddevitab", 14537aa6070dScube i->i_name); 14547aa6070dScube } else { 1455db7dbdacSuebayasi for (j = f; j->i_alias != NULL; j = j->i_alias) 1456db7dbdacSuebayasi continue; 14577aa6070dScube j->i_alias = i; 14587aa6070dScube } 1459e499d8b5Scube /* 14605d1e8b27Swiz * - reconstruct d->d_umax 1461e499d8b5Scube */ 1462e499d8b5Scube d->d_umax = 0; 1463e499d8b5Scube for (i = d->d_ihead; i != NULL; i = i->i_bsame) 1464e499d8b5Scube if (i->i_unit >= d->d_umax) 1465e499d8b5Scube d->d_umax = i->i_unit + 1; 14665ecc953bSthorpej } 14675ecc953bSthorpej 14685ecc953bSthorpej void 14697b7c582aScube deldeva(const char *at) 14707b7c582aScube { 14717b7c582aScube int unit; 14727b7c582aScube const char *cp; 14737b7c582aScube struct devbase *d, *ad; 14747b7c582aScube struct devi *i, *j; 14757b7c582aScube struct attr *a; 14767b7c582aScube struct pspec *p; 14777b7c582aScube struct nvlist *nv, *stack = NULL; 14787b7c582aScube 14797b7c582aScube if (at == NULL) { 14807b7c582aScube TAILQ_FOREACH(i, &alldevi, i_next) 14817b7c582aScube if (i->i_at == NULL) 14827b7c582aScube stack = newnv(NULL, NULL, i, 0, stack); 14837b7c582aScube } else { 14849a7f4bbaSchristos size_t l; 14857b7c582aScube 148675eb07f2Suebayasi CFGDBG(5, "deselecting deva `%s'", at); 14879a7f4bbaSchristos if (at[0] == '\0') 14889a7f4bbaSchristos goto out; 14899a7f4bbaSchristos 14907b7c582aScube l = strlen(at) - 1; 14917b7c582aScube if (at[l] == '?' || isdigit((unsigned char)at[l])) { 14927b7c582aScube char base[NAMESIZE]; 14937b7c582aScube 14947b7c582aScube if (split(at, l+1, base, sizeof base, &unit)) { 14959a7f4bbaSchristos out: 1496c7295a4cSchristos cfgerror("invalid attachment name `%s'", at); 14977b7c582aScube return; 14987b7c582aScube } 14997b7c582aScube cp = intern(base); 15007b7c582aScube } else { 15017b7c582aScube cp = intern(at); 15027b7c582aScube unit = STAR; 15037b7c582aScube } 15047b7c582aScube 15057b7c582aScube ad = ht_lookup(devbasetab, cp); 15067b7c582aScube a = ht_lookup(attrtab, cp); 15077b7c582aScube if (a == NULL) { 1508c7295a4cSchristos cfgerror("unknown attachment attribute or device `%s'", 15097b7c582aScube cp); 15107b7c582aScube return; 15117b7c582aScube } 15127b7c582aScube if (!a->a_iattr) { 1513c7295a4cSchristos cfgerror("plain attribute `%s' cannot have children", 15147b7c582aScube a->a_name); 15157b7c582aScube return; 15167b7c582aScube } 15177b7c582aScube 15187b7c582aScube /* 15197b7c582aScube * remove_devi() makes changes to the devbase's list and the 15207b7c582aScube * alias list, * so the actual deletion of the instances must 15217b7c582aScube * be delayed. 15227b7c582aScube */ 15237b7c582aScube for (nv = a->a_devs; nv != NULL; nv = nv->nv_next) { 15247b7c582aScube d = nv->nv_ptr; 15257b7c582aScube for (i = d->d_ihead; i != NULL; i = i->i_bsame) 15267b7c582aScube for (j = i; j != NULL; j = j->i_alias) { 15277b7c582aScube /* Ignore devices at root */ 15287b7c582aScube if (j->i_at == NULL) 15297b7c582aScube continue; 15307b7c582aScube p = j->i_pspec; 15317b7c582aScube /* 15327b7c582aScube * There are three cases: 15337b7c582aScube * 15347b7c582aScube * 1. unit is not STAR. Consider 'at' 15357b7c582aScube * to be explicit, even if it 15367b7c582aScube * references an interface 15377b7c582aScube * attribute. 15387b7c582aScube * 15397b7c582aScube * 2. unit is STAR and 'at' references 15407b7c582aScube * a real device. Look for pspec 15417b7c582aScube * that have a matching p_atdev 15427b7c582aScube * field. 15437b7c582aScube * 15447b7c582aScube * 3. unit is STAR and 'at' references 15457b7c582aScube * an interface attribute. Look 15467b7c582aScube * for pspec that have a matching 15477b7c582aScube * p_iattr field. 15487b7c582aScube */ 15497b7c582aScube if ((unit != STAR && /* Case */ 15507b7c582aScube !strcmp(j->i_at, at)) || /* 1 */ 15517b7c582aScube (unit == STAR && 15527b7c582aScube ((ad != NULL && /* Case */ 15537b7c582aScube p->p_atdev == ad) || /* 2 */ 15547b7c582aScube (ad == NULL && /* Case */ 15557b7c582aScube p->p_iattr == a)))) /* 3 */ 15567b7c582aScube stack = newnv(NULL, NULL, j, 0, 15577b7c582aScube stack); 15587b7c582aScube } 15597b7c582aScube } 15607b7c582aScube } 15617b7c582aScube 15627b7c582aScube for (nv = stack; nv != NULL; nv = nv->nv_next) 15637b7c582aScube remove_devi(nv->nv_ptr); 1564e50e4ee4Scube nvfreel(stack); 15657b7c582aScube } 15667b7c582aScube 15677b7c582aScube void 15687b7c582aScube deldev(const char *name) 15697b7c582aScube { 15709a7f4bbaSchristos size_t l; 15717b7c582aScube struct devi *firsti, *i; 15727b7c582aScube struct nvlist *nv, *stack = NULL; 15737b7c582aScube 157475eb07f2Suebayasi CFGDBG(5, "deselecting dev `%s'", name); 15759a7f4bbaSchristos if (name[0] == '\0') 15769a7f4bbaSchristos goto out; 15779a7f4bbaSchristos 15787b7c582aScube l = strlen(name) - 1; 15797b7c582aScube if (name[l] == '*' || isdigit((unsigned char)name[l])) { 15807b7c582aScube /* `no mydev0' or `no mydev*' */ 15817b7c582aScube firsti = ht_lookup(devitab, name); 15827b7c582aScube if (firsti == NULL) { 15839a7f4bbaSchristos out: 1584c7295a4cSchristos cfgerror("unknown instance %s", name); 15857b7c582aScube return; 15867b7c582aScube } 15877b7c582aScube for (i = firsti; i != NULL; i = i->i_alias) 15887b7c582aScube stack = newnv(NULL, NULL, i, 0, stack); 15897b7c582aScube } else { 15907b7c582aScube struct devbase *d = ht_lookup(devbasetab, name); 15917b7c582aScube 15927b7c582aScube if (d == NULL) { 1593c7295a4cSchristos cfgerror("unknown device %s", name); 15947b7c582aScube return; 15957b7c582aScube } 159671af9028Scube if (d->d_ispseudo) { 1597c7295a4cSchristos cfgerror("%s is a pseudo-device; " 159871af9028Scube "use \"no pseudo-device %s\" instead", name, 159971af9028Scube name); 160071af9028Scube return; 160171af9028Scube } 16027b7c582aScube 16037b7c582aScube for (firsti = d->d_ihead; firsti != NULL; 16047b7c582aScube firsti = firsti->i_bsame) 16057b7c582aScube for (i = firsti; i != NULL; i = i->i_alias) 16067b7c582aScube stack = newnv(NULL, NULL, i, 0, stack); 16077b7c582aScube } 16087b7c582aScube 16097b7c582aScube for (nv = stack; nv != NULL; nv = nv->nv_next) 16107b7c582aScube remove_devi(nv->nv_ptr); 1611e50e4ee4Scube nvfreel(stack); 16127b7c582aScube } 16137b7c582aScube 161400ed8a38Spooka /* 161500ed8a38Spooka * Insert given device "name" into devroottab. In case "name" 161600ed8a38Spooka * designates a pure interface attribute, create a fake device 161700ed8a38Spooka * instance for the attribute and insert that into the roottab 161800ed8a38Spooka * (this scheme avoids mucking around with the orphanage analysis). 161900ed8a38Spooka */ 16207b7c582aScube void 162190ac64deSpooka addpseudoroot(const char *name) 162290ac64deSpooka { 1623194e1c80Spooka char buf[NAMESIZE]; 1624194e1c80Spooka int unit; 1625194e1c80Spooka struct attr *attr; 162690ac64deSpooka struct devi *i; 162790ac64deSpooka struct deva *iba; 162890ac64deSpooka struct devbase *ib; 162990ac64deSpooka 1630194e1c80Spooka if (split(name, strlen(name), buf, sizeof(buf), &unit)) { 1631194e1c80Spooka cfgerror("invalid pseudo-root name `%s'", name); 1632194e1c80Spooka return; 1633194e1c80Spooka } 1634194e1c80Spooka 1635194e1c80Spooka /* 163600ed8a38Spooka * Prefer device because devices with locators define an 163700ed8a38Spooka * implicit interface attribute. However, if a device is 163800ed8a38Spooka * not available, try to attach to the interface attribute. 163900ed8a38Spooka * This makes sure adddev() doesn't get confused when we 164000ed8a38Spooka * are really attaching to a device (alternatively we maybe 164100ed8a38Spooka * could specify a non-NULL atlist to defdevattach() below). 1642194e1c80Spooka */ 164300ed8a38Spooka ib = ht_lookup(devbasetab, intern(buf)); 164400ed8a38Spooka if (ib == NULL) { 1645194e1c80Spooka struct devbase *fakedev; 1646194e1c80Spooka char fakename[NAMESIZE]; 1647194e1c80Spooka 164800ed8a38Spooka attr = ht_lookup(attrtab, intern(buf)); 164900ed8a38Spooka if (!(attr && attr->a_iattr)) { 165000ed8a38Spooka cfgerror("pseudo-root `%s' not available", name); 165100ed8a38Spooka return; 165200ed8a38Spooka } 165300ed8a38Spooka 1654194e1c80Spooka /* 1655194e1c80Spooka * here we cheat a bit: create a fake devbase with the 1656194e1c80Spooka * interface attribute and instantiate it. quick, cheap, 1657194e1c80Spooka * dirty & bad for you, much like the stuff in the fridge. 1658194e1c80Spooka * and, it works, since the pseudoroot device is not included 1659194e1c80Spooka * in ioconf, just used by config to make sure we start from 1660194e1c80Spooka * the right place. 1661194e1c80Spooka */ 166200ed8a38Spooka snprintf(fakename, sizeof(fakename), "%s_devattrs", buf); 1663194e1c80Spooka fakedev = getdevbase(intern(fakename)); 1664194e1c80Spooka fakedev->d_isdef = 1; 1665194e1c80Spooka fakedev->d_ispseudo = 0; 16664caf067bSdholland fakedev->d_attrs = attrlist_cons(NULL, attr); 1667194e1c80Spooka defdevattach(NULL, fakedev, NULL, NULL); 1668194e1c80Spooka 1669194e1c80Spooka if (unit == STAR) 1670194e1c80Spooka snprintf(buf, sizeof(buf), "%s*", fakename); 1671194e1c80Spooka else 1672194e1c80Spooka snprintf(buf, sizeof(buf), "%s%d", fakename, unit); 1673194e1c80Spooka name = buf; 1674194e1c80Spooka } 1675194e1c80Spooka 1676194e1c80Spooka /* ok, everything should be set up, so instantiate a fake device */ 167790ac64deSpooka i = getdevi(name); 167890ac64deSpooka if (i == NULL) 167900ed8a38Spooka panic("device `%s' expected to be present", name); 168090ac64deSpooka ib = i->i_base; 1681194e1c80Spooka iba = ib->d_ahead; 168290ac64deSpooka 168390ac64deSpooka i->i_atdeva = iba; 168490ac64deSpooka i->i_cfflags = 0; 168590ac64deSpooka i->i_locs = fixloc(name, &errattr, NULL); 168690ac64deSpooka i->i_pseudoroot = 1; 168790ac64deSpooka i->i_active = DEVI_ORPHAN; /* set active by kill_orphans() */ 168890ac64deSpooka 168990ac64deSpooka *iba->d_ipp = i; 169090ac64deSpooka iba->d_ipp = &i->i_asame; 169190ac64deSpooka 169290ac64deSpooka ht_insert(devroottab, ib->d_name, ib); 169390ac64deSpooka } 169490ac64deSpooka 169590ac64deSpooka void 16965ecc953bSthorpej addpseudo(const char *name, int number) 16975ecc953bSthorpej { 16985ecc953bSthorpej struct devbase *d; 16995ecc953bSthorpej struct devi *i; 17005ecc953bSthorpej 17015ecc953bSthorpej d = ht_lookup(devbasetab, name); 17025ecc953bSthorpej if (d == NULL) { 1703c7295a4cSchristos cfgerror("undefined pseudo-device %s", name); 17045ecc953bSthorpej return; 17055ecc953bSthorpej } 17065ecc953bSthorpej if (!d->d_ispseudo) { 1707c7295a4cSchristos cfgerror("%s is a real device, not a pseudo-device", name); 17085ecc953bSthorpej return; 17095ecc953bSthorpej } 17105ecc953bSthorpej if (ht_lookup(devitab, name) != NULL) { 1711c7295a4cSchristos cfgerror("`%s' already defined", name); 17125ecc953bSthorpej return; 17135ecc953bSthorpej } 17145ecc953bSthorpej i = newdevi(name, number - 1, d); /* foo 16 => "foo0..foo15" */ 17155ecc953bSthorpej if (ht_insert(devitab, name, i)) 17165ecc953bSthorpej panic("addpseudo(%s)", name); 17177b7c582aScube /* Useful to retrieve the instance from the devbase */ 17187b7c582aScube d->d_ihead = i; 17197aa6070dScube i->i_active = DEVI_ACTIVE; 17205ecc953bSthorpej TAILQ_INSERT_TAIL(&allpseudo, i, i_next); 17215ecc953bSthorpej } 17225ecc953bSthorpej 17235ecc953bSthorpej void 17245ecc953bSthorpej delpseudo(const char *name) 17255ecc953bSthorpej { 17265ecc953bSthorpej struct devbase *d; 17275ecc953bSthorpej struct devi *i; 17285ecc953bSthorpej 172975eb07f2Suebayasi CFGDBG(5, "deselecting pseudo `%s'", name); 17305ecc953bSthorpej d = ht_lookup(devbasetab, name); 17315ecc953bSthorpej if (d == NULL) { 1732c7295a4cSchristos cfgerror("undefined pseudo-device %s", name); 17335ecc953bSthorpej return; 17345ecc953bSthorpej } 17355ecc953bSthorpej if (!d->d_ispseudo) { 1736c7295a4cSchristos cfgerror("%s is a real device, not a pseudo-device", name); 17375ecc953bSthorpej return; 17385ecc953bSthorpej } 17395ecc953bSthorpej if ((i = ht_lookup(devitab, name)) == NULL) { 1740c7295a4cSchristos cfgerror("`%s' not defined", name); 17415ecc953bSthorpej return; 17425ecc953bSthorpej } 17435ecc953bSthorpej d->d_umax = 0; /* clear neads-count entries */ 17447aa6070dScube d->d_ihead = NULL; /* make sure it won't be considered active */ 17455ecc953bSthorpej TAILQ_REMOVE(&allpseudo, i, i_next); 17465ecc953bSthorpej if (ht_remove(devitab, name)) 17475ecc953bSthorpej panic("delpseudo(%s) - can't remove from devitab", name); 17487aa6070dScube if (ht_insert(deaddevitab, name, i)) 17497aa6070dScube panic("delpseudo(%s) - can't add to deaddevitab", name); 17505ecc953bSthorpej } 17515ecc953bSthorpej 17525ecc953bSthorpej void 1753d767912bSdrochner adddevm(const char *name, devmajor_t cmajor, devmajor_t bmajor, 17549483bda7Sdholland struct condexpr *cond, struct nvlist *nv_nodes) 17555ecc953bSthorpej { 17565ecc953bSthorpej struct devm *dm; 17575ecc953bSthorpej 1758d767912bSdrochner if (cmajor != NODEVMAJOR && (cmajor < 0 || cmajor >= 4096)) { 1759c7295a4cSchristos cfgerror("character major %d is invalid", cmajor); 17609483bda7Sdholland condexpr_destroy(cond); 17613da3ab25Spooka nvfreel(nv_nodes); 17625ecc953bSthorpej return; 17635ecc953bSthorpej } 17645ecc953bSthorpej 1765d767912bSdrochner if (bmajor != NODEVMAJOR && (bmajor < 0 || bmajor >= 4096)) { 1766c7295a4cSchristos cfgerror("block major %d is invalid", bmajor); 17679483bda7Sdholland condexpr_destroy(cond); 17683da3ab25Spooka nvfreel(nv_nodes); 17695ecc953bSthorpej return; 17705ecc953bSthorpej } 1771d767912bSdrochner if (cmajor == NODEVMAJOR && bmajor == NODEVMAJOR) { 1772c7295a4cSchristos cfgerror("both character/block majors are not specified"); 17739483bda7Sdholland condexpr_destroy(cond); 17743da3ab25Spooka nvfreel(nv_nodes); 17755ecc953bSthorpej return; 17765ecc953bSthorpej } 17775ecc953bSthorpej 17785ecc953bSthorpej dm = ecalloc(1, sizeof(*dm)); 17795ecc953bSthorpej dm->dm_srcfile = yyfile; 17805ecc953bSthorpej dm->dm_srcline = currentline(); 17815ecc953bSthorpej dm->dm_name = name; 17825ecc953bSthorpej dm->dm_cmajor = cmajor; 17835ecc953bSthorpej dm->dm_bmajor = bmajor; 17849483bda7Sdholland dm->dm_opts = cond; 17853da3ab25Spooka dm->dm_devnodes = nv_nodes; 17865ecc953bSthorpej 17875ecc953bSthorpej TAILQ_INSERT_TAIL(&alldevms, dm, dm_next); 17885ecc953bSthorpej 17895ecc953bSthorpej maxcdevm = MAX(maxcdevm, dm->dm_cmajor); 17905ecc953bSthorpej maxbdevm = MAX(maxbdevm, dm->dm_bmajor); 17915ecc953bSthorpej } 17925ecc953bSthorpej 17937aa6070dScube int 17945ecc953bSthorpej fixdevis(void) 17955ecc953bSthorpej { 17965ecc953bSthorpej struct devi *i; 17977aa6070dScube int error = 0; 17985ecc953bSthorpej 1799bc707469Suebayasi TAILQ_FOREACH(i, &alldevi, i_next) { 1800bc707469Suebayasi CFGDBG(3, "fixing devis `%s'", i->i_name); 18017aa6070dScube if (i->i_active == DEVI_ACTIVE) 18025ecc953bSthorpej selectbase(i->i_base, i->i_atdeva); 18037aa6070dScube else if (i->i_active == DEVI_ORPHAN) { 1804c130d400Scube /* 1805c130d400Scube * At this point, we can't have instances for which 1806c130d400Scube * i_at or i_pspec are NULL. 1807c130d400Scube */ 18087aa6070dScube ++error; 1809c7295a4cSchristos cfgxerror(i->i_srcfile, i->i_lineno, 18100dbd1c0eScube "`%s at %s' is orphaned (%s `%s' found)", 1811c130d400Scube i->i_name, i->i_at, i->i_pspec->p_atunit == WILD ? 1812c130d400Scube "nothing matching" : "no", i->i_at); 18137aa6070dScube } else if (vflag && i->i_active == DEVI_IGNORED) 1814c7295a4cSchristos cfgxwarn(i->i_srcfile, i->i_lineno, "ignoring " 1815c7295a4cSchristos "explicitly orphaned instance `%s at %s'", 1816c7295a4cSchristos i->i_name, i->i_at); 1817bc707469Suebayasi } 18187aa6070dScube 18197aa6070dScube if (error) 18207aa6070dScube return error; 18215ecc953bSthorpej 18225ecc953bSthorpej TAILQ_FOREACH(i, &allpseudo, i_next) 18237aa6070dScube if (i->i_active == DEVI_ACTIVE) 18245ecc953bSthorpej selectbase(i->i_base, NULL); 18257aa6070dScube return 0; 18265ecc953bSthorpej } 18275ecc953bSthorpej 18285ecc953bSthorpej /* 18295ecc953bSthorpej * Look up a parent spec, creating a new one if it does not exist. 18305ecc953bSthorpej */ 18315ecc953bSthorpej static struct pspec * 18325ecc953bSthorpej getpspec(struct attr *attr, struct devbase *ab, int atunit) 18335ecc953bSthorpej { 18345ecc953bSthorpej struct pspec *p; 18355ecc953bSthorpej 18365ecc953bSthorpej TAILQ_FOREACH(p, &allpspecs, p_list) { 18375ecc953bSthorpej if (p->p_iattr == attr && 18385ecc953bSthorpej p->p_atdev == ab && 18395ecc953bSthorpej p->p_atunit == atunit) 18405ecc953bSthorpej return (p); 18415ecc953bSthorpej } 18425ecc953bSthorpej 18435ecc953bSthorpej p = ecalloc(1, sizeof(*p)); 18445ecc953bSthorpej 18455ecc953bSthorpej p->p_iattr = attr; 18465ecc953bSthorpej p->p_atdev = ab; 18475ecc953bSthorpej p->p_atunit = atunit; 18485ecc953bSthorpej p->p_inst = npspecs++; 1849c130d400Scube p->p_active = 0; 18505ecc953bSthorpej 18515ecc953bSthorpej TAILQ_INSERT_TAIL(&allpspecs, p, p_list); 18525ecc953bSthorpej 18535ecc953bSthorpej return (p); 18545ecc953bSthorpej } 18555ecc953bSthorpej 18565ecc953bSthorpej /* 18575ecc953bSthorpej * Define a new instance of a specific device. 18585ecc953bSthorpej */ 18595ecc953bSthorpej static struct devi * 18605ecc953bSthorpej getdevi(const char *name) 18615ecc953bSthorpej { 18625ecc953bSthorpej struct devi *i, *firsti; 18635ecc953bSthorpej struct devbase *d; 18645ecc953bSthorpej int unit; 18655ecc953bSthorpej char base[NAMESIZE]; 18665ecc953bSthorpej 18675ecc953bSthorpej if (split(name, strlen(name), base, sizeof base, &unit)) { 1868c7295a4cSchristos cfgerror("invalid device name `%s'", name); 18695ecc953bSthorpej return (NULL); 18705ecc953bSthorpej } 18715ecc953bSthorpej d = ht_lookup(devbasetab, intern(base)); 18725ecc953bSthorpej if (d == NULL) { 1873c7295a4cSchristos cfgerror("%s: unknown device `%s'", name, base); 18745ecc953bSthorpej return (NULL); 18755ecc953bSthorpej } 18765ecc953bSthorpej if (d->d_ispseudo) { 1877c7295a4cSchristos cfgerror("%s: %s is a pseudo-device", name, base); 18785ecc953bSthorpej return (NULL); 18795ecc953bSthorpej } 18805ecc953bSthorpej firsti = ht_lookup(devitab, name); 18815ecc953bSthorpej i = newdevi(name, unit, d); 18825ecc953bSthorpej if (firsti == NULL) { 18835ecc953bSthorpej if (ht_insert(devitab, name, i)) 18845ecc953bSthorpej panic("getdevi(%s)", name); 18855ecc953bSthorpej *d->d_ipp = i; 18865ecc953bSthorpej d->d_ipp = &i->i_bsame; 18875ecc953bSthorpej } else { 18885ecc953bSthorpej while (firsti->i_alias) 18895ecc953bSthorpej firsti = firsti->i_alias; 18905ecc953bSthorpej firsti->i_alias = i; 18915ecc953bSthorpej } 18925ecc953bSthorpej TAILQ_INSERT_TAIL(&alldevi, i, i_next); 18935ecc953bSthorpej ndevi++; 18945ecc953bSthorpej return (i); 18955ecc953bSthorpej } 18965ecc953bSthorpej 18975ecc953bSthorpej static const char * 18985ecc953bSthorpej concat(const char *name, int c) 18995ecc953bSthorpej { 1900c7295a4cSchristos size_t len; 19015ecc953bSthorpej char buf[NAMESIZE]; 19025ecc953bSthorpej 19035ecc953bSthorpej len = strlen(name); 19045ecc953bSthorpej if (len + 2 > sizeof(buf)) { 1905c7295a4cSchristos cfgerror("device name `%s%c' too long", name, c); 19065ecc953bSthorpej len = sizeof(buf) - 2; 19075ecc953bSthorpej } 19085ecc953bSthorpej memmove(buf, name, len); 19099a7f4bbaSchristos buf[len] = (char)c; 19109a7f4bbaSchristos buf[len + 1] = '\0'; 19115ecc953bSthorpej return (intern(buf)); 19125ecc953bSthorpej } 19135ecc953bSthorpej 19145ecc953bSthorpej const char * 19155ecc953bSthorpej starref(const char *name) 19165ecc953bSthorpej { 19175ecc953bSthorpej 19185ecc953bSthorpej return (concat(name, '*')); 19195ecc953bSthorpej } 19205ecc953bSthorpej 19215ecc953bSthorpej const char * 19225ecc953bSthorpej wildref(const char *name) 19235ecc953bSthorpej { 19245ecc953bSthorpej 19255ecc953bSthorpej return (concat(name, '?')); 19265ecc953bSthorpej } 19275ecc953bSthorpej 19285ecc953bSthorpej /* 19295ecc953bSthorpej * Split a name like "foo0" into base name (foo) and unit number (0). 19305ecc953bSthorpej * Return 0 on success. To make this useful for names like "foo0a", 19315ecc953bSthorpej * the length of the "foo0" part is one of the arguments. 19325ecc953bSthorpej */ 19335ecc953bSthorpej static int 19345ecc953bSthorpej split(const char *name, size_t nlen, char *base, size_t bsize, int *aunit) 19355ecc953bSthorpej { 19365ecc953bSthorpej const char *cp; 1937c7295a4cSchristos int c; 1938c7295a4cSchristos size_t l; 19395ecc953bSthorpej 19405ecc953bSthorpej l = nlen; 19415ecc953bSthorpej if (l < 2 || l >= bsize || isdigit((unsigned char)*name)) 19425ecc953bSthorpej return (1); 19435ecc953bSthorpej c = (u_char)name[--l]; 19445ecc953bSthorpej if (!isdigit(c)) { 19455ecc953bSthorpej if (c == '*') 19465ecc953bSthorpej *aunit = STAR; 19475ecc953bSthorpej else if (c == '?') 19485ecc953bSthorpej *aunit = WILD; 19495ecc953bSthorpej else 19505ecc953bSthorpej return (1); 19515ecc953bSthorpej } else { 19525ecc953bSthorpej cp = &name[l]; 19535ecc953bSthorpej while (isdigit((unsigned char)cp[-1])) 19545ecc953bSthorpej l--, cp--; 19555ecc953bSthorpej *aunit = atoi(cp); 19565ecc953bSthorpej } 19575ecc953bSthorpej memmove(base, name, l); 19585ecc953bSthorpej base[l] = 0; 19595ecc953bSthorpej return (0); 19605ecc953bSthorpej } 19615ecc953bSthorpej 19625ecc953bSthorpej void 19638d928a75Suebayasi addattr(const char *name) 19648d928a75Suebayasi { 19658d928a75Suebayasi struct attr *a; 19668d928a75Suebayasi 19678d928a75Suebayasi a = refattr(name); 19688d928a75Suebayasi selectattr(a); 19698d928a75Suebayasi } 19708d928a75Suebayasi 19718d928a75Suebayasi void 19728d928a75Suebayasi delattr(const char *name) 19738d928a75Suebayasi { 19748d928a75Suebayasi struct attr *a; 19758d928a75Suebayasi 19768d928a75Suebayasi a = refattr(name); 19778d928a75Suebayasi deselectattr(a); 19788d928a75Suebayasi } 19798d928a75Suebayasi 19808d928a75Suebayasi void 19815ecc953bSthorpej selectattr(struct attr *a) 19825ecc953bSthorpej { 19833a03bee6Suebayasi struct attrlist *al; 19843a03bee6Suebayasi struct attr *dep; 19855ecc953bSthorpej 19868d928a75Suebayasi CFGDBG(5, "selecting attr `%s'", a->a_name); 19873a03bee6Suebayasi for (al = a->a_deps; al != NULL; al = al->al_next) { 19883a03bee6Suebayasi dep = al->al_this; 19893a03bee6Suebayasi selectattr(dep); 19903a03bee6Suebayasi } 1991d6687201Suebayasi if (ht_insert(selecttab, a->a_name, __UNCONST(a->a_name)) == 0) 1992d6687201Suebayasi nattrs++; 1993b599bf99Suebayasi CFGDBG(3, "attr selected `%s'", a->a_name); 19945ecc953bSthorpej } 19955ecc953bSthorpej 19966d1bba4aSuebayasi static int 19978d928a75Suebayasi deselectattrcb2(const char *name1, const char *name2, void *v, void *arg) 19988d928a75Suebayasi { 19998d928a75Suebayasi const char *name = arg; 20008d928a75Suebayasi 20018d928a75Suebayasi if (strcmp(name, name2) == 0) 20028d928a75Suebayasi delattr(name1); 20038d928a75Suebayasi return 0; 20048d928a75Suebayasi } 20058d928a75Suebayasi 20068d928a75Suebayasi void 20078d928a75Suebayasi deselectattr(struct attr *a) 20088d928a75Suebayasi { 20098d928a75Suebayasi 20108d928a75Suebayasi CFGDBG(5, "deselecting attr `%s'", a->a_name); 20118d928a75Suebayasi ht_enumerate2(attrdeptab, deselectattrcb2, __UNCONST(a->a_name)); 2012d6687201Suebayasi if (ht_remove(selecttab, a->a_name) == 0) 2013d6687201Suebayasi nattrs--; 20148d928a75Suebayasi CFGDBG(3, "attr deselected `%s'", a->a_name); 20158d928a75Suebayasi } 20168d928a75Suebayasi 20178d928a75Suebayasi static int 20186d1bba4aSuebayasi dumpattrdepcb2(const char *name1, const char *name2, void *v, void *arg) 20196d1bba4aSuebayasi { 20206d1bba4aSuebayasi 20216d1bba4aSuebayasi CFGDBG(3, "attr `%s' depends on attr `%s'", name1, name2); 20226d1bba4aSuebayasi return 0; 20236d1bba4aSuebayasi } 20246d1bba4aSuebayasi 20256d1bba4aSuebayasi void 20266d1bba4aSuebayasi dependattrs(void) 20276d1bba4aSuebayasi { 20286d1bba4aSuebayasi 20296d1bba4aSuebayasi ht_enumerate2(attrdeptab, dumpattrdepcb2, NULL); 20306d1bba4aSuebayasi } 20316d1bba4aSuebayasi 20325ecc953bSthorpej /* 20335ecc953bSthorpej * We have an instance of the base foo, so select it and all its 20345ecc953bSthorpej * attributes for "optional foo". 20355ecc953bSthorpej */ 20365ecc953bSthorpej static void 20375ecc953bSthorpej selectbase(struct devbase *d, struct deva *da) 20385ecc953bSthorpej { 20395ecc953bSthorpej struct attr *a; 20404caf067bSdholland struct attrlist *al; 20415ecc953bSthorpej 2042c7295a4cSchristos (void)ht_insert(selecttab, d->d_name, __UNCONST(d->d_name)); 2043b599bf99Suebayasi CFGDBG(3, "devbase selected `%s'", d->d_name); 20446d1bba4aSuebayasi CFGDBG(5, "selecting dependencies of devbase `%s'", d->d_name); 20454caf067bSdholland for (al = d->d_attrs; al != NULL; al = al->al_next) { 20464caf067bSdholland a = al->al_this; 20475ecc953bSthorpej expandattr(a, selectattr); 20485ecc953bSthorpej } 2049da706167Suebayasi 2050da706167Suebayasi struct attr *devattr; 2051da706167Suebayasi devattr = refattr(d->d_name); 2052da706167Suebayasi expandattr(devattr, selectattr); 2053da706167Suebayasi 20545ecc953bSthorpej if (da != NULL) { 2055c7295a4cSchristos (void)ht_insert(selecttab, da->d_name, __UNCONST(da->d_name)); 2056b599bf99Suebayasi CFGDBG(3, "devattr selected `%s'", da->d_name); 20574caf067bSdholland for (al = da->d_attrs; al != NULL; al = al->al_next) { 20584caf067bSdholland a = al->al_this; 20595ecc953bSthorpej expandattr(a, selectattr); 20605ecc953bSthorpej } 20615ecc953bSthorpej } 2062c4bac1d0Suebayasi 2063c4bac1d0Suebayasi fixdev(d); 20645ecc953bSthorpej } 20655ecc953bSthorpej 20665ecc953bSthorpej /* 20675ecc953bSthorpej * Is the given pointer on the given list of pointers? 20685ecc953bSthorpej */ 206959c94545Scube int 20705ecc953bSthorpej onlist(struct nvlist *nv, void *ptr) 20715ecc953bSthorpej { 20725ecc953bSthorpej for (; nv != NULL; nv = nv->nv_next) 20735ecc953bSthorpej if (nv->nv_ptr == ptr) 20745ecc953bSthorpej return (1); 20755ecc953bSthorpej return (0); 20765ecc953bSthorpej } 20775ecc953bSthorpej 20785ecc953bSthorpej static char * 20795ecc953bSthorpej extend(char *p, const char *name) 20805ecc953bSthorpej { 2081c7295a4cSchristos size_t l; 20825ecc953bSthorpej 20835ecc953bSthorpej l = strlen(name); 20845ecc953bSthorpej memmove(p, name, l); 20855ecc953bSthorpej p += l; 20865ecc953bSthorpej *p++ = ','; 20875ecc953bSthorpej *p++ = ' '; 20885ecc953bSthorpej return (p); 20895ecc953bSthorpej } 20905ecc953bSthorpej 20915ecc953bSthorpej /* 20925ecc953bSthorpej * Check that we got all required locators, and default any that are 20935ecc953bSthorpej * given as "?" and have defaults. Return 0 on success. 20945ecc953bSthorpej */ 20955ecc953bSthorpej static const char ** 209616a8771bSdholland fixloc(const char *name, struct attr *attr, struct loclist *got) 20975ecc953bSthorpej { 209816a8771bSdholland struct loclist *m, *n; 20995ecc953bSthorpej int ord; 21005ecc953bSthorpej const char **lp; 21015ecc953bSthorpej int nmissing, nextra, nnodefault; 21025ecc953bSthorpej char *mp, *ep, *ndp; 21035ecc953bSthorpej char missing[1000], extra[1000], nodefault[1000]; 21045ecc953bSthorpej static const char *nullvec[1]; 21055ecc953bSthorpej 21065ecc953bSthorpej /* 21075ecc953bSthorpej * Look for all required locators, and number the given ones 21085ecc953bSthorpej * according to the required order. While we are numbering, 21095ecc953bSthorpej * set default values for defaulted locators. 21105ecc953bSthorpej */ 21115ecc953bSthorpej if (attr->a_loclen == 0) /* e.g., "at root" */ 21125ecc953bSthorpej lp = nullvec; 21135ecc953bSthorpej else 21149a7f4bbaSchristos lp = emalloc((size_t)(attr->a_loclen + 1) * sizeof(const char *)); 211516a8771bSdholland for (n = got; n != NULL; n = n->ll_next) 211616a8771bSdholland n->ll_num = -1; 21175ecc953bSthorpej nmissing = 0; 21185ecc953bSthorpej mp = missing; 21195ecc953bSthorpej /* yes, this is O(mn), but m and n should be small */ 212016a8771bSdholland for (ord = 0, m = attr->a_locs; m != NULL; m = m->ll_next, ord++) { 212116a8771bSdholland for (n = got; n != NULL; n = n->ll_next) { 212216a8771bSdholland if (n->ll_name == m->ll_name) { 212316a8771bSdholland n->ll_num = ord; 21245ecc953bSthorpej break; 21255ecc953bSthorpej } 21265ecc953bSthorpej } 212716a8771bSdholland if (n == NULL && m->ll_num == 0) { 21285ecc953bSthorpej nmissing++; 212916a8771bSdholland mp = extend(mp, m->ll_name); 21305ecc953bSthorpej } 213116a8771bSdholland lp[ord] = m->ll_string; 21325ecc953bSthorpej } 21335ecc953bSthorpej if (ord != attr->a_loclen) 21345ecc953bSthorpej panic("fixloc"); 21355ecc953bSthorpej lp[ord] = NULL; 21365ecc953bSthorpej nextra = 0; 21375ecc953bSthorpej ep = extra; 21385ecc953bSthorpej nnodefault = 0; 21395ecc953bSthorpej ndp = nodefault; 214016a8771bSdholland for (n = got; n != NULL; n = n->ll_next) { 214116a8771bSdholland if (n->ll_num >= 0) { 214216a8771bSdholland if (n->ll_string != NULL) 214316a8771bSdholland lp[n->ll_num] = n->ll_string; 214416a8771bSdholland else if (lp[n->ll_num] == NULL) { 21455ecc953bSthorpej nnodefault++; 214616a8771bSdholland ndp = extend(ndp, n->ll_name); 21475ecc953bSthorpej } 21485ecc953bSthorpej } else { 21495ecc953bSthorpej nextra++; 215016a8771bSdholland ep = extend(ep, n->ll_name); 21515ecc953bSthorpej } 21525ecc953bSthorpej } 21535ecc953bSthorpej if (nextra) { 21545ecc953bSthorpej ep[-2] = 0; /* kill ", " */ 2155c7295a4cSchristos cfgerror("%s: extraneous locator%s: %s", 21565ecc953bSthorpej name, nextra > 1 ? "s" : "", extra); 21575ecc953bSthorpej } 21585ecc953bSthorpej if (nmissing) { 21595ecc953bSthorpej mp[-2] = 0; 2160c7295a4cSchristos cfgerror("%s: must specify %s", name, missing); 21615ecc953bSthorpej } 21625ecc953bSthorpej if (nnodefault) { 21635ecc953bSthorpej ndp[-2] = 0; 2164c7295a4cSchristos cfgerror("%s: cannot wildcard %s", name, nodefault); 21655ecc953bSthorpej } 21665ecc953bSthorpej if (nmissing || nnodefault) { 21675ecc953bSthorpej free(lp); 21685ecc953bSthorpej lp = NULL; 21695ecc953bSthorpej } 21705ecc953bSthorpej return (lp); 21715ecc953bSthorpej } 2172437f8925Scube 2173437f8925Scube void 2174437f8925Scube setversion(int newver) 2175437f8925Scube { 2176437f8925Scube if (newver > CONFIG_VERSION) 2177c7295a4cSchristos cfgerror("your sources require a newer version of config(1) " 2178437f8925Scube "-- please rebuild it."); 2179437f8925Scube else if (newver < CONFIG_MINVERSION) 2180c7295a4cSchristos cfgerror("your sources are out of date -- please update."); 2181437f8925Scube else 2182437f8925Scube version = newver; 2183437f8925Scube } 2184