1*9483bda7Sdholland /* $NetBSD: sem.c,v 1.40 2012/03/11 08:21:53 dholland 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 475ecc953bSthorpej #include <sys/param.h> 485ecc953bSthorpej #include <ctype.h> 495ecc953bSthorpej #include <stdio.h> 505ecc953bSthorpej #include <stdlib.h> 515ecc953bSthorpej #include <string.h> 52d0fb8901Schristos #include <util.h> 535ecc953bSthorpej #include "defs.h" 545ecc953bSthorpej #include "sem.h" 555ecc953bSthorpej 565ecc953bSthorpej /* 575ecc953bSthorpej * config semantics. 585ecc953bSthorpej */ 595ecc953bSthorpej 605ecc953bSthorpej #define NAMESIZE 100 /* local name buffers */ 615ecc953bSthorpej 625ecc953bSthorpej const char *s_ifnet; /* magic attribute */ 635ecc953bSthorpej const char *s_qmark; 645ecc953bSthorpej const char *s_none; 655ecc953bSthorpej 665ecc953bSthorpej static struct hashtab *cfhashtab; /* for config lookup */ 675ecc953bSthorpej struct hashtab *devitab; /* etc */ 685ecc953bSthorpej 695ecc953bSthorpej static struct attr errattr; 705ecc953bSthorpej static struct devbase errdev; 715ecc953bSthorpej static struct deva errdeva; 725ecc953bSthorpej 734caf067bSdholland static int has_errobj(struct attrlist *, struct attr *); 745ecc953bSthorpej static struct nvlist *addtoattr(struct nvlist *, struct devbase *); 755ecc953bSthorpej static int resolve(struct nvlist **, const char *, const char *, 765ecc953bSthorpej struct nvlist *, int); 775ecc953bSthorpej static struct pspec *getpspec(struct attr *, struct devbase *, int); 785ecc953bSthorpej static struct devi *newdevi(const char *, int, struct devbase *d); 795ecc953bSthorpej static struct devi *getdevi(const char *); 80c130d400Scube static void remove_devi(struct devi *); 815ecc953bSthorpej static const char *concat(const char *, int); 825ecc953bSthorpej static char *extend(char *, const char *); 835ecc953bSthorpej static int split(const char *, size_t, char *, size_t, int *); 845ecc953bSthorpej static void selectbase(struct devbase *, struct deva *); 855ecc953bSthorpej static const char **fixloc(const char *, struct attr *, struct nvlist *); 86d767912bSdrochner static const char *makedevstr(devmajor_t, devminor_t); 87d767912bSdrochner static const char *major2name(devmajor_t); 88d767912bSdrochner static devmajor_t dev2major(struct devbase *); 895ecc953bSthorpej 905ecc953bSthorpej extern const char *yyfile; 917aa6070dScube extern int vflag; 925ecc953bSthorpej 935ecc953bSthorpej void 945ecc953bSthorpej initsem(void) 955ecc953bSthorpej { 965ecc953bSthorpej 975ecc953bSthorpej attrtab = ht_new(); 985ecc953bSthorpej errattr.a_name = "<internal>"; 995ecc953bSthorpej 1005ecc953bSthorpej TAILQ_INIT(&allbases); 1015ecc953bSthorpej 1025ecc953bSthorpej TAILQ_INIT(&alldevas); 1035ecc953bSthorpej 1045ecc953bSthorpej TAILQ_INIT(&allpspecs); 1055ecc953bSthorpej 1065ecc953bSthorpej cfhashtab = ht_new(); 1075ecc953bSthorpej TAILQ_INIT(&allcf); 1085ecc953bSthorpej 1095ecc953bSthorpej TAILQ_INIT(&alldevi); 1105ecc953bSthorpej errdev.d_name = "<internal>"; 1115ecc953bSthorpej 1125ecc953bSthorpej TAILQ_INIT(&allpseudo); 1135ecc953bSthorpej 1145ecc953bSthorpej TAILQ_INIT(&alldevms); 1155ecc953bSthorpej 1165ecc953bSthorpej s_ifnet = intern("ifnet"); 1175ecc953bSthorpej s_qmark = intern("?"); 1185ecc953bSthorpej s_none = intern("none"); 1195ecc953bSthorpej } 1205ecc953bSthorpej 1215ecc953bSthorpej /* Name of include file just ended (set in scan.l) */ 1225ecc953bSthorpej extern const char *lastfile; 1235ecc953bSthorpej 1245ecc953bSthorpej void 1255ecc953bSthorpej enddefs(void) 1265ecc953bSthorpej { 1275ecc953bSthorpej struct devbase *dev; 1285ecc953bSthorpej 1295ecc953bSthorpej TAILQ_FOREACH(dev, &allbases, d_next) { 1305ecc953bSthorpej if (!dev->d_isdef) { 1315ecc953bSthorpej (void)fprintf(stderr, 1325ecc953bSthorpej "%s: device `%s' used but not defined\n", 1335ecc953bSthorpej lastfile, dev->d_name); 1345ecc953bSthorpej errors++; 1355ecc953bSthorpej continue; 1365ecc953bSthorpej } 1375ecc953bSthorpej } 1385ecc953bSthorpej if (errors) { 1395ecc953bSthorpej (void)fprintf(stderr, "*** Stop.\n"); 1405ecc953bSthorpej exit(1); 1415ecc953bSthorpej } 1425ecc953bSthorpej } 1435ecc953bSthorpej 1445ecc953bSthorpej void 1455ecc953bSthorpej setdefmaxusers(int min, int def, int max) 1465ecc953bSthorpej { 1475ecc953bSthorpej 1485ecc953bSthorpej if (min < 1 || min > def || def > max) 149c7295a4cSchristos cfgerror("maxusers must have 1 <= min (%d) <= default (%d) " 150c7295a4cSchristos "<= max (%d)", min, def, max); 1515ecc953bSthorpej else { 1525ecc953bSthorpej minmaxusers = min; 1535ecc953bSthorpej defmaxusers = def; 1545ecc953bSthorpej maxmaxusers = max; 1555ecc953bSthorpej } 1565ecc953bSthorpej } 1575ecc953bSthorpej 1585ecc953bSthorpej void 1595ecc953bSthorpej setmaxusers(int n) 1605ecc953bSthorpej { 1615ecc953bSthorpej 162895e5687Scube if (maxusers == n) { 163c7295a4cSchristos cfgerror("duplicate maxusers parameter"); 1645ecc953bSthorpej return; 1655ecc953bSthorpej } 166895e5687Scube if (vflag && maxusers != 0) 167c7295a4cSchristos cfgwarn("warning: maxusers already defined"); 1685ecc953bSthorpej maxusers = n; 1695ecc953bSthorpej if (n < minmaxusers) { 170c7295a4cSchristos cfgerror("warning: minimum of %d maxusers assumed", 171c7295a4cSchristos minmaxusers); 1725ecc953bSthorpej errors--; /* take it away */ 1735ecc953bSthorpej maxusers = minmaxusers; 1745ecc953bSthorpej } else if (n > maxmaxusers) { 175c7295a4cSchristos cfgerror("warning: maxusers (%d) > %d", n, maxmaxusers); 1765ecc953bSthorpej errors--; 1775ecc953bSthorpej } 1785ecc953bSthorpej } 1795ecc953bSthorpej 1805ecc953bSthorpej void 1815ecc953bSthorpej setident(const char *i) 1825ecc953bSthorpej { 1835ecc953bSthorpej 1845ecc953bSthorpej ident = intern(i); 1855ecc953bSthorpej } 1865ecc953bSthorpej 1875ecc953bSthorpej /* 1885ecc953bSthorpej * Define an attribute, optionally with an interface (a locator list) 1895ecc953bSthorpej * and a set of attribute-dependencies. 1905ecc953bSthorpej * 1915ecc953bSthorpej * Attribute dependencies MAY NOT be interface attributes. 1925ecc953bSthorpej * 1935ecc953bSthorpej * Since an empty locator list is logically different from "no interface", 1945ecc953bSthorpej * all locator lists include a dummy head node, which we discard here. 1955ecc953bSthorpej */ 1965ecc953bSthorpej int 1974caf067bSdholland defattr(const char *name, struct nvlist *locs, struct attrlist *deps, 1985ecc953bSthorpej int devclass) 1995ecc953bSthorpej { 2005ecc953bSthorpej struct attr *a, *dep; 2014caf067bSdholland struct attrlist *al; 2025ecc953bSthorpej struct nvlist *nv; 2035ecc953bSthorpej int len; 2045ecc953bSthorpej 2055ecc953bSthorpej if (locs != NULL && devclass) 2065ecc953bSthorpej panic("defattr(%s): locators and devclass", name); 2075ecc953bSthorpej 2085ecc953bSthorpej if (deps != NULL && devclass) 2095ecc953bSthorpej panic("defattr(%s): dependencies and devclass", name); 2105ecc953bSthorpej 2115ecc953bSthorpej /* 2125ecc953bSthorpej * If this attribute depends on any others, make sure none of 2135ecc953bSthorpej * the dependencies are interface attributes. 2145ecc953bSthorpej */ 2154caf067bSdholland for (al = deps; al != NULL; al = al->al_next) { 2164caf067bSdholland dep = al->al_this; 2175ecc953bSthorpej if (dep->a_iattr) { 218c7295a4cSchristos cfgerror("`%s' dependency `%s' is an interface " 219c7295a4cSchristos "attribute", name, dep->a_name); 2205ecc953bSthorpej return (1); 2215ecc953bSthorpej } 2225ecc953bSthorpej } 2235ecc953bSthorpej 2245ecc953bSthorpej a = ecalloc(1, sizeof *a); 2255ecc953bSthorpej if (ht_insert(attrtab, name, a)) { 2265ecc953bSthorpej free(a); 227c7295a4cSchristos cfgerror("attribute `%s' already defined", name); 2285ecc953bSthorpej nvfreel(locs); 2295ecc953bSthorpej return (1); 2305ecc953bSthorpej } 2315ecc953bSthorpej 2325ecc953bSthorpej a->a_name = name; 2335ecc953bSthorpej if (locs != NULL) { 2345ecc953bSthorpej a->a_iattr = 1; 2355ecc953bSthorpej a->a_locs = locs->nv_next; 2365ecc953bSthorpej nvfree(locs); 2375ecc953bSthorpej } else { 2385ecc953bSthorpej a->a_iattr = 0; 2395ecc953bSthorpej a->a_locs = NULL; 2405ecc953bSthorpej } 2415ecc953bSthorpej if (devclass) { 242bbfbbde1Schristos char classenum[256], *cp; 2435ecc953bSthorpej int errored = 0; 2445ecc953bSthorpej 245bbfbbde1Schristos (void)snprintf(classenum, sizeof(classenum), "DV_%s", name); 2465ecc953bSthorpej for (cp = classenum + 3; *cp; cp++) { 2475ecc953bSthorpej if (!errored && 2485ecc953bSthorpej (!isalnum((unsigned char)*cp) || 2495ecc953bSthorpej (isalpha((unsigned char)*cp) && !islower((unsigned char)*cp)))) { 250c7295a4cSchristos cfgerror("device class names must be " 251c7295a4cSchristos "lower-case alphanumeric characters"); 2525ecc953bSthorpej errored = 1; 2535ecc953bSthorpej } 2545ecc953bSthorpej *cp = toupper((unsigned char)*cp); 2555ecc953bSthorpej } 2565ecc953bSthorpej a->a_devclass = intern(classenum); 2575ecc953bSthorpej } else 2585ecc953bSthorpej a->a_devclass = NULL; 2595ecc953bSthorpej len = 0; 2605ecc953bSthorpej for (nv = a->a_locs; nv != NULL; nv = nv->nv_next) 2615ecc953bSthorpej len++; 2625ecc953bSthorpej a->a_loclen = len; 2635ecc953bSthorpej a->a_devs = NULL; 2645ecc953bSthorpej a->a_refs = NULL; 2655ecc953bSthorpej a->a_deps = deps; 2665ecc953bSthorpej a->a_expanding = 0; 2675ecc953bSthorpej 2685ecc953bSthorpej /* Expand the attribute to check for cycles in the graph. */ 2695ecc953bSthorpej expandattr(a, NULL); 2705ecc953bSthorpej 2715ecc953bSthorpej return (0); 2725ecc953bSthorpej } 2735ecc953bSthorpej 2745ecc953bSthorpej /* 2755ecc953bSthorpej * Return true if the given `error object' is embedded in the given 2765ecc953bSthorpej * pointer list. 2775ecc953bSthorpej */ 2785ecc953bSthorpej static int 2794caf067bSdholland has_errobj(struct attrlist *al, struct attr *obj) 2805ecc953bSthorpej { 2815ecc953bSthorpej 2824caf067bSdholland for (; al != NULL; al = al->al_next) 2834caf067bSdholland if (al->al_this == obj) 2845ecc953bSthorpej return (1); 2855ecc953bSthorpej return (0); 2865ecc953bSthorpej } 2875ecc953bSthorpej 2885ecc953bSthorpej /* 2895ecc953bSthorpej * Return true if the given attribute is embedded in the given 2905ecc953bSthorpej * pointer list. 2915ecc953bSthorpej */ 2925ecc953bSthorpej int 2934caf067bSdholland has_attr(struct attrlist *al, const char *attr) 2945ecc953bSthorpej { 2955ecc953bSthorpej struct attr *a; 2965ecc953bSthorpej 2975ecc953bSthorpej if ((a = getattr(attr)) == NULL) 2985ecc953bSthorpej return (0); 2995ecc953bSthorpej 3004caf067bSdholland for (; al != NULL; al = al->al_next) 3014caf067bSdholland if (al->al_this == a) 3025ecc953bSthorpej return (1); 3035ecc953bSthorpej return (0); 3045ecc953bSthorpej } 3055ecc953bSthorpej 3065ecc953bSthorpej /* 3075ecc953bSthorpej * Add a device base to a list in an attribute (actually, to any list). 3085ecc953bSthorpej * Note that this does not check for duplicates, and does reverse the 3095ecc953bSthorpej * list order, but no one cares anyway. 3105ecc953bSthorpej */ 3115ecc953bSthorpej static struct nvlist * 3125ecc953bSthorpej addtoattr(struct nvlist *l, struct devbase *dev) 3135ecc953bSthorpej { 3145ecc953bSthorpej struct nvlist *n; 3155ecc953bSthorpej 3165ecc953bSthorpej n = newnv(NULL, NULL, dev, 0, l); 3175ecc953bSthorpej return (n); 3185ecc953bSthorpej } 3195ecc953bSthorpej 3205ecc953bSthorpej /* 3215ecc953bSthorpej * Define a device. This may (or may not) also define an interface 3225ecc953bSthorpej * attribute and/or refer to existing attributes. 3235ecc953bSthorpej */ 3245ecc953bSthorpej void 3254caf067bSdholland defdev(struct devbase *dev, struct nvlist *loclist, struct attrlist *attrs, 3265ecc953bSthorpej int ispseudo) 3275ecc953bSthorpej { 3285ecc953bSthorpej struct nvlist *nv; 3294caf067bSdholland struct attrlist *al; 3305ecc953bSthorpej struct attr *a; 3315ecc953bSthorpej 3325ecc953bSthorpej if (dev == &errdev) 3335ecc953bSthorpej goto bad; 3345ecc953bSthorpej if (dev->d_isdef) { 335c7295a4cSchristos cfgerror("redefinition of `%s'", dev->d_name); 3365ecc953bSthorpej goto bad; 3375ecc953bSthorpej } 3385ecc953bSthorpej 3395ecc953bSthorpej dev->d_isdef = 1; 3405ecc953bSthorpej if (has_errobj(attrs, &errattr)) 3415ecc953bSthorpej goto bad; 3425ecc953bSthorpej 3435ecc953bSthorpej /* 3445ecc953bSthorpej * Handle implicit attribute definition from locator list. Do 3455ecc953bSthorpej * this before scanning the `at' list so that we can have, e.g.: 3465ecc953bSthorpej * device foo at other, foo { slot = -1 } 3475ecc953bSthorpej * (where you can plug in a foo-bus extender to a foo-bus). 3485ecc953bSthorpej */ 3495ecc953bSthorpej if (loclist != NULL) { 3505ecc953bSthorpej nv = loclist; 3515ecc953bSthorpej loclist = NULL; /* defattr disposes of them for us */ 3525ecc953bSthorpej if (defattr(dev->d_name, nv, NULL, 0)) 3535ecc953bSthorpej goto bad; 3544caf067bSdholland attrs = attrlist_cons(attrs, getattr(dev->d_name)); 3554caf067bSdholland /* This used to be stored but was never used */ 3564caf067bSdholland /* attrs->al_name = dev->d_name; */ 357c454f920Scube } 358c454f920Scube 359c130d400Scube /* 360c130d400Scube * Pseudo-devices can have children. Consider them as 361c130d400Scube * attaching at root. 362c130d400Scube */ 363c454f920Scube if (ispseudo) { 3644caf067bSdholland for (al = attrs; al != NULL; al = al->al_next) 3654caf067bSdholland if (al->al_this->a_iattr) 366c454f920Scube break; 3674caf067bSdholland if (al != NULL) { 368b66156c7Sdrochner if (ispseudo < 2) { 369b66156c7Sdrochner if (version >= 20080610) 370b66156c7Sdrochner cfgerror("interface attribute on " 371b66156c7Sdrochner "non-device pseudo `%s'", dev->d_name); 372b66156c7Sdrochner else { 373b66156c7Sdrochner ispseudo = 2; 374b66156c7Sdrochner } 375b66156c7Sdrochner } 376c130d400Scube ht_insert(devroottab, dev->d_name, dev); 3775ecc953bSthorpej } 378b66156c7Sdrochner } 3795ecc953bSthorpej 3805ecc953bSthorpej /* Committed! Set up fields. */ 3815ecc953bSthorpej dev->d_ispseudo = ispseudo; 3825ecc953bSthorpej dev->d_attrs = attrs; 3835ecc953bSthorpej dev->d_classattr = NULL; /* for now */ 3845ecc953bSthorpej 3855ecc953bSthorpej /* 3865ecc953bSthorpej * For each interface attribute this device refers to, add this 3875ecc953bSthorpej * device to its reference list. This makes, e.g., finding all 3885ecc953bSthorpej * "scsi"s easier. 3895ecc953bSthorpej * 3905ecc953bSthorpej * While looking through the attributes, set up the device 3915ecc953bSthorpej * class if any are devclass attributes (and error out if the 3925ecc953bSthorpej * device has two classes). 3935ecc953bSthorpej */ 3944caf067bSdholland for (al = attrs; al != NULL; al = al->al_next) { 3954caf067bSdholland a = al->al_this; 3965ecc953bSthorpej if (a->a_iattr) 3975ecc953bSthorpej a->a_refs = addtoattr(a->a_refs, dev); 3985ecc953bSthorpej if (a->a_devclass != NULL) { 3995ecc953bSthorpej if (dev->d_classattr != NULL) { 400c7295a4cSchristos cfgerror("device `%s' has multiple classes " 401c7295a4cSchristos "(`%s' and `%s')", 4025ecc953bSthorpej dev->d_name, dev->d_classattr->a_name, 4035ecc953bSthorpej a->a_name); 4045ecc953bSthorpej } 4055ecc953bSthorpej dev->d_classattr = a; 4065ecc953bSthorpej } 4075ecc953bSthorpej } 4085ecc953bSthorpej return; 4095ecc953bSthorpej bad: 4105ecc953bSthorpej nvfreel(loclist); 4114caf067bSdholland attrlist_destroyall(attrs); 4125ecc953bSthorpej } 4135ecc953bSthorpej 4145ecc953bSthorpej /* 4155ecc953bSthorpej * Look up a devbase. Also makes sure it is a reasonable name, 4165ecc953bSthorpej * i.e., does not end in a digit or contain special characters. 4175ecc953bSthorpej */ 4185ecc953bSthorpej struct devbase * 4195ecc953bSthorpej getdevbase(const char *name) 4205ecc953bSthorpej { 421c7295a4cSchristos const u_char *p; 4225ecc953bSthorpej struct devbase *dev; 4235ecc953bSthorpej 424c7295a4cSchristos p = (const u_char *)name; 4255ecc953bSthorpej if (!isalpha(*p)) 4265ecc953bSthorpej goto badname; 4275ecc953bSthorpej while (*++p) { 4285ecc953bSthorpej if (!isalnum(*p) && *p != '_') 4295ecc953bSthorpej goto badname; 4305ecc953bSthorpej } 4315ecc953bSthorpej if (isdigit(*--p)) { 4325ecc953bSthorpej badname: 433c7295a4cSchristos cfgerror("bad device base name `%s'", name); 4345ecc953bSthorpej return (&errdev); 4355ecc953bSthorpej } 4365ecc953bSthorpej dev = ht_lookup(devbasetab, name); 4375ecc953bSthorpej if (dev == NULL) { 4385ecc953bSthorpej dev = ecalloc(1, sizeof *dev); 4395ecc953bSthorpej dev->d_name = name; 4405ecc953bSthorpej dev->d_isdef = 0; 441d767912bSdrochner dev->d_major = NODEVMAJOR; 4425ecc953bSthorpej dev->d_attrs = NULL; 4435ecc953bSthorpej dev->d_ihead = NULL; 4445ecc953bSthorpej dev->d_ipp = &dev->d_ihead; 4455ecc953bSthorpej dev->d_ahead = NULL; 4465ecc953bSthorpej dev->d_app = &dev->d_ahead; 4475ecc953bSthorpej dev->d_umax = 0; 4485ecc953bSthorpej TAILQ_INSERT_TAIL(&allbases, dev, d_next); 4495ecc953bSthorpej if (ht_insert(devbasetab, name, dev)) 4505ecc953bSthorpej panic("getdevbase(%s)", name); 4515ecc953bSthorpej } 4525ecc953bSthorpej return (dev); 4535ecc953bSthorpej } 4545ecc953bSthorpej 4555ecc953bSthorpej /* 4565ecc953bSthorpej * Define some of a device's allowable parent attachments. 4575ecc953bSthorpej * There may be a list of (plain) attributes. 4585ecc953bSthorpej */ 4595ecc953bSthorpej void 4605ecc953bSthorpej defdevattach(struct deva *deva, struct devbase *dev, struct nvlist *atlist, 4614caf067bSdholland struct attrlist *attrs) 4625ecc953bSthorpej { 4635ecc953bSthorpej struct nvlist *nv; 4644caf067bSdholland struct attrlist *al; 4655ecc953bSthorpej struct attr *a; 4665ecc953bSthorpej struct deva *da; 4675ecc953bSthorpej 4685ecc953bSthorpej if (dev == &errdev) 4695ecc953bSthorpej goto bad; 4705ecc953bSthorpej if (deva == NULL) 4715ecc953bSthorpej deva = getdevattach(dev->d_name); 4725ecc953bSthorpej if (deva == &errdeva) 4735ecc953bSthorpej goto bad; 4745ecc953bSthorpej if (!dev->d_isdef) { 475c7295a4cSchristos cfgerror("attaching undefined device `%s'", dev->d_name); 4765ecc953bSthorpej goto bad; 4775ecc953bSthorpej } 4785ecc953bSthorpej if (deva->d_isdef) { 479c7295a4cSchristos cfgerror("redefinition of `%s'", deva->d_name); 4805ecc953bSthorpej goto bad; 4815ecc953bSthorpej } 4825ecc953bSthorpej if (dev->d_ispseudo) { 483c7295a4cSchristos cfgerror("pseudo-devices can't attach"); 4845ecc953bSthorpej goto bad; 4855ecc953bSthorpej } 4865ecc953bSthorpej 4875ecc953bSthorpej deva->d_isdef = 1; 4885ecc953bSthorpej if (has_errobj(attrs, &errattr)) 4895ecc953bSthorpej goto bad; 4904caf067bSdholland for (al = attrs; al != NULL; al = al->al_next) { 4914caf067bSdholland a = al->al_this; 4925ecc953bSthorpej if (a == &errattr) 4935ecc953bSthorpej continue; /* already complained */ 4945ecc953bSthorpej if (a->a_iattr || a->a_devclass != NULL) 495c7295a4cSchristos cfgerror("`%s' is not a plain attribute", a->a_name); 4965ecc953bSthorpej } 4975ecc953bSthorpej 4985ecc953bSthorpej /* Committed! Set up fields. */ 4995ecc953bSthorpej deva->d_attrs = attrs; 5005ecc953bSthorpej deva->d_atlist = atlist; 5015ecc953bSthorpej deva->d_devbase = dev; 5025ecc953bSthorpej 5035ecc953bSthorpej /* 5045ecc953bSthorpej * Turn the `at' list into interface attributes (map each 5055ecc953bSthorpej * nv_name to an attribute, or to NULL for root), and add 5065ecc953bSthorpej * this device to those attributes, so that children can 5075ecc953bSthorpej * be listed at this particular device if they are supported 5085ecc953bSthorpej * by that attribute. 5095ecc953bSthorpej */ 5105ecc953bSthorpej for (nv = atlist; nv != NULL; nv = nv->nv_next) { 5115ecc953bSthorpej if (nv->nv_name == NULL) 5125ecc953bSthorpej nv->nv_ptr = a = NULL; /* at root */ 5135ecc953bSthorpej else 5145ecc953bSthorpej nv->nv_ptr = a = getattr(nv->nv_name); 5155ecc953bSthorpej if (a == &errattr) 5165ecc953bSthorpej continue; /* already complained */ 5175ecc953bSthorpej 5185ecc953bSthorpej /* 5195ecc953bSthorpej * Make sure that an attachment spec doesn't 5205ecc953bSthorpej * already say how to attach to this attribute. 5215ecc953bSthorpej */ 5225ecc953bSthorpej for (da = dev->d_ahead; da != NULL; da = da->d_bsame) 5235ecc953bSthorpej if (onlist(da->d_atlist, a)) 524c7295a4cSchristos cfgerror("attach at `%s' already done by `%s'", 5255ecc953bSthorpej a ? a->a_name : "root", da->d_name); 5265ecc953bSthorpej 527c130d400Scube if (a == NULL) { 528c130d400Scube ht_insert(devroottab, dev->d_name, dev); 5295ecc953bSthorpej continue; /* at root; don't add */ 530c130d400Scube } 5315ecc953bSthorpej if (!a->a_iattr) 532c7295a4cSchristos cfgerror("%s cannot be at plain attribute `%s'", 5335ecc953bSthorpej dev->d_name, a->a_name); 5345ecc953bSthorpej else 5355ecc953bSthorpej a->a_devs = addtoattr(a->a_devs, dev); 5365ecc953bSthorpej } 5375ecc953bSthorpej 5385ecc953bSthorpej /* attach to parent */ 5395ecc953bSthorpej *dev->d_app = deva; 5405ecc953bSthorpej dev->d_app = &deva->d_bsame; 5415ecc953bSthorpej return; 5425ecc953bSthorpej bad: 5435ecc953bSthorpej nvfreel(atlist); 5444caf067bSdholland attrlist_destroyall(attrs); 5455ecc953bSthorpej } 5465ecc953bSthorpej 5475ecc953bSthorpej /* 5485ecc953bSthorpej * Look up a device attachment. Also makes sure it is a reasonable 5495ecc953bSthorpej * name, i.e., does not contain digits or special characters. 5505ecc953bSthorpej */ 5515ecc953bSthorpej struct deva * 5525ecc953bSthorpej getdevattach(const char *name) 5535ecc953bSthorpej { 554c7295a4cSchristos const u_char *p; 5555ecc953bSthorpej struct deva *deva; 5565ecc953bSthorpej 557c7295a4cSchristos p = (const u_char *)name; 5585ecc953bSthorpej if (!isalpha(*p)) 5595ecc953bSthorpej goto badname; 5605ecc953bSthorpej while (*++p) { 5615ecc953bSthorpej if (!isalnum(*p) && *p != '_') 5625ecc953bSthorpej goto badname; 5635ecc953bSthorpej } 5645ecc953bSthorpej if (isdigit(*--p)) { 5655ecc953bSthorpej badname: 566c7295a4cSchristos cfgerror("bad device attachment name `%s'", name); 5675ecc953bSthorpej return (&errdeva); 5685ecc953bSthorpej } 5695ecc953bSthorpej deva = ht_lookup(devatab, name); 5705ecc953bSthorpej if (deva == NULL) { 5715ecc953bSthorpej deva = ecalloc(1, sizeof *deva); 5725ecc953bSthorpej deva->d_name = name; 5735ecc953bSthorpej deva->d_bsame = NULL; 5745ecc953bSthorpej deva->d_isdef = 0; 5755ecc953bSthorpej deva->d_devbase = NULL; 5765ecc953bSthorpej deva->d_atlist = NULL; 5775ecc953bSthorpej deva->d_attrs = NULL; 5785ecc953bSthorpej deva->d_ihead = NULL; 5795ecc953bSthorpej deva->d_ipp = &deva->d_ihead; 5805ecc953bSthorpej TAILQ_INSERT_TAIL(&alldevas, deva, d_next); 5815ecc953bSthorpej if (ht_insert(devatab, name, deva)) 5825ecc953bSthorpej panic("getdeva(%s)", name); 5835ecc953bSthorpej } 5845ecc953bSthorpej return (deva); 5855ecc953bSthorpej } 5865ecc953bSthorpej 5875ecc953bSthorpej /* 5885ecc953bSthorpej * Look up an attribute. 5895ecc953bSthorpej */ 5905ecc953bSthorpej struct attr * 5915ecc953bSthorpej getattr(const char *name) 5925ecc953bSthorpej { 5935ecc953bSthorpej struct attr *a; 5945ecc953bSthorpej 5955ecc953bSthorpej if ((a = ht_lookup(attrtab, name)) == NULL) { 596c7295a4cSchristos cfgerror("undefined attribute `%s'", name); 5975ecc953bSthorpej a = &errattr; 5985ecc953bSthorpej } 5995ecc953bSthorpej return (a); 6005ecc953bSthorpej } 6015ecc953bSthorpej 6025ecc953bSthorpej /* 6035ecc953bSthorpej * Recursively expand an attribute and its dependencies, checking for 6045ecc953bSthorpej * cycles, and invoking a callback for each attribute found. 6055ecc953bSthorpej */ 6065ecc953bSthorpej void 6075ecc953bSthorpej expandattr(struct attr *a, void (*callback)(struct attr *)) 6085ecc953bSthorpej { 6094caf067bSdholland struct attrlist *al; 6105ecc953bSthorpej struct attr *dep; 6115ecc953bSthorpej 6125ecc953bSthorpej if (a->a_expanding) { 613c7295a4cSchristos cfgerror("circular dependency on attribute `%s'", a->a_name); 6145ecc953bSthorpej return; 6155ecc953bSthorpej } 6165ecc953bSthorpej 6175ecc953bSthorpej a->a_expanding = 1; 6185ecc953bSthorpej 6195ecc953bSthorpej /* First expand all of this attribute's dependencies. */ 6204caf067bSdholland for (al = a->a_deps; al != NULL; al = al->al_next) { 6214caf067bSdholland dep = al->al_this; 6225ecc953bSthorpej expandattr(dep, callback); 6235ecc953bSthorpej } 6245ecc953bSthorpej 6255ecc953bSthorpej /* ...and now invoke the callback for ourself. */ 6265ecc953bSthorpej if (callback != NULL) 6275ecc953bSthorpej (*callback)(a); 6285ecc953bSthorpej 6295ecc953bSthorpej a->a_expanding = 0; 6305ecc953bSthorpej } 6315ecc953bSthorpej 6325ecc953bSthorpej /* 6335ecc953bSthorpej * Set the major device number for a device, so that it can be used 6345ecc953bSthorpej * as a root/dumps "on" device in a configuration. 6355ecc953bSthorpej */ 6365ecc953bSthorpej void 637d767912bSdrochner setmajor(struct devbase *d, devmajor_t n) 6385ecc953bSthorpej { 6395ecc953bSthorpej 640d767912bSdrochner if (d != &errdev && d->d_major != NODEVMAJOR) 641d767912bSdrochner cfgerror("device `%s' is already major %d", 642d767912bSdrochner d->d_name, d->d_major); 6435ecc953bSthorpej else 6445ecc953bSthorpej d->d_major = n; 6455ecc953bSthorpej } 6465ecc953bSthorpej 6475ecc953bSthorpej const char * 648d767912bSdrochner major2name(devmajor_t maj) 6495ecc953bSthorpej { 6505ecc953bSthorpej struct devbase *dev; 6515ecc953bSthorpej struct devm *dm; 6525ecc953bSthorpej 6535ecc953bSthorpej if (!do_devsw) { 6545ecc953bSthorpej TAILQ_FOREACH(dev, &allbases, d_next) { 6555ecc953bSthorpej if (dev->d_major == maj) 6565ecc953bSthorpej return (dev->d_name); 6575ecc953bSthorpej } 6585ecc953bSthorpej } else { 6595ecc953bSthorpej TAILQ_FOREACH(dm, &alldevms, dm_next) { 6605ecc953bSthorpej if (dm->dm_bmajor == maj) 6615ecc953bSthorpej return (dm->dm_name); 6625ecc953bSthorpej } 6635ecc953bSthorpej } 6645ecc953bSthorpej return (NULL); 6655ecc953bSthorpej } 6665ecc953bSthorpej 667d767912bSdrochner devmajor_t 6685ecc953bSthorpej dev2major(struct devbase *dev) 6695ecc953bSthorpej { 6705ecc953bSthorpej struct devm *dm; 6715ecc953bSthorpej 6725ecc953bSthorpej if (!do_devsw) 6735ecc953bSthorpej return (dev->d_major); 6745ecc953bSthorpej 6755ecc953bSthorpej TAILQ_FOREACH(dm, &alldevms, dm_next) { 6765ecc953bSthorpej if (strcmp(dm->dm_name, dev->d_name) == 0) 6775ecc953bSthorpej return (dm->dm_bmajor); 6785ecc953bSthorpej } 679d767912bSdrochner return (NODEVMAJOR); 6805ecc953bSthorpej } 6815ecc953bSthorpej 6825ecc953bSthorpej /* 6835ecc953bSthorpej * Make a string description of the device at maj/min. 6845ecc953bSthorpej */ 6855ecc953bSthorpej static const char * 686d767912bSdrochner makedevstr(devmajor_t maj, devminor_t min) 6875ecc953bSthorpej { 688c7295a4cSchristos const char *devicename; 6895ecc953bSthorpej char buf[32]; 6905ecc953bSthorpej 691c7295a4cSchristos devicename = major2name(maj); 692c7295a4cSchristos if (devicename == NULL) 693d767912bSdrochner (void)snprintf(buf, sizeof(buf), "<%d/%d>", maj, min); 6945ecc953bSthorpej else 695d767912bSdrochner (void)snprintf(buf, sizeof(buf), "%s%d%c", devicename, 696d767912bSdrochner min / maxpartitions, (min % maxpartitions) + 'a'); 6975ecc953bSthorpej 6985ecc953bSthorpej return (intern(buf)); 6995ecc953bSthorpej } 7005ecc953bSthorpej 7015ecc953bSthorpej /* 7025ecc953bSthorpej * Map things like "ra0b" => makedev(major("ra"), 0*maxpartitions + 'b'-'a'). 7035ecc953bSthorpej * Handle the case where the device number is given but there is no 7045ecc953bSthorpej * corresponding name, and map NULL to the default. 7055ecc953bSthorpej */ 7065ecc953bSthorpej static int 7075ecc953bSthorpej resolve(struct nvlist **nvp, const char *name, const char *what, 7085ecc953bSthorpej struct nvlist *dflt, int part) 7095ecc953bSthorpej { 7105ecc953bSthorpej struct nvlist *nv; 7115ecc953bSthorpej struct devbase *dev; 7125ecc953bSthorpej const char *cp; 713d767912bSdrochner devmajor_t maj; 714d767912bSdrochner devminor_t min; 7150001b928Schristos int i, l; 7165ecc953bSthorpej int unit; 7175ecc953bSthorpej char buf[NAMESIZE]; 7185ecc953bSthorpej 7195cc303e1Slukem if ((part -= 'a') >= maxpartitions || part < 0) 7205ecc953bSthorpej panic("resolve"); 7215ecc953bSthorpej if ((nv = *nvp) == NULL) { 7225ecc953bSthorpej dev_t d = NODEV; 7235ecc953bSthorpej /* 7245ecc953bSthorpej * Apply default. Easiest to do this by number. 7255ecc953bSthorpej * Make sure to retain NODEVness, if this is dflt's disposition. 7265ecc953bSthorpej */ 7275cc303e1Slukem if ((dev_t)dflt->nv_num != NODEV) { 7280001b928Schristos maj = major(dflt->nv_num); 7290001b928Schristos min = ((minor(dflt->nv_num) / maxpartitions) * 7305ecc953bSthorpej maxpartitions) + part; 7315ecc953bSthorpej d = makedev(maj, min); 7325ecc953bSthorpej cp = makedevstr(maj, min); 7335ecc953bSthorpej } else 7345ecc953bSthorpej cp = NULL; 7355ecc953bSthorpej *nvp = nv = newnv(NULL, cp, NULL, d, NULL); 7365ecc953bSthorpej } 7375cc303e1Slukem if ((dev_t)nv->nv_num != NODEV) { 7385ecc953bSthorpej /* 7395ecc953bSthorpej * By the numbers. Find the appropriate major number 7405ecc953bSthorpej * to make a name. 7415ecc953bSthorpej */ 7420001b928Schristos maj = major(nv->nv_num); 7430001b928Schristos min = minor(nv->nv_num); 7445ecc953bSthorpej nv->nv_str = makedevstr(maj, min); 7455ecc953bSthorpej return (0); 7465ecc953bSthorpej } 7475ecc953bSthorpej 7485ecc953bSthorpej if (nv->nv_str == NULL || nv->nv_str == s_qmark) 7495ecc953bSthorpej /* 7505ecc953bSthorpej * Wildcarded or unspecified; leave it as NODEV. 7515ecc953bSthorpej */ 7525ecc953bSthorpej return (0); 7535ecc953bSthorpej 7545ecc953bSthorpej /* 7555ecc953bSthorpej * The normal case: things like "ra2b". Check for partition 7565ecc953bSthorpej * suffix, remove it if there, and split into name ("ra") and 7575ecc953bSthorpej * unit (2). 7585ecc953bSthorpej */ 7595ecc953bSthorpej l = i = strlen(nv->nv_str); 7605ecc953bSthorpej cp = &nv->nv_str[l]; 7615ecc953bSthorpej if (l > 1 && *--cp >= 'a' && *cp < 'a' + maxpartitions && 7625ecc953bSthorpej isdigit((unsigned char)cp[-1])) { 7635ecc953bSthorpej l--; 7645ecc953bSthorpej part = *cp - 'a'; 7655ecc953bSthorpej } 7665ecc953bSthorpej cp = nv->nv_str; 7675ecc953bSthorpej if (split(cp, l, buf, sizeof buf, &unit)) { 768c7295a4cSchristos cfgerror("%s: invalid %s device name `%s'", name, what, cp); 7695ecc953bSthorpej return (1); 7705ecc953bSthorpej } 7715ecc953bSthorpej dev = ht_lookup(devbasetab, intern(buf)); 7725ecc953bSthorpej if (dev == NULL) { 773c7295a4cSchristos cfgerror("%s: device `%s' does not exist", name, buf); 7745ecc953bSthorpej return (1); 7755ecc953bSthorpej } 7765ecc953bSthorpej 7775ecc953bSthorpej /* 7785ecc953bSthorpej * Check for the magic network interface attribute, and 7795ecc953bSthorpej * don't bother making a device number. 7805ecc953bSthorpej */ 7815ecc953bSthorpej if (has_attr(dev->d_attrs, s_ifnet)) { 7820001b928Schristos nv->nv_num = NODEV; 7835ecc953bSthorpej nv->nv_ifunit = unit; /* XXX XXX XXX */ 7845ecc953bSthorpej } else { 7855ecc953bSthorpej maj = dev2major(dev); 7865cc303e1Slukem if (maj == NODEVMAJOR) { 787c7295a4cSchristos cfgerror("%s: can't make %s device from `%s'", 7885ecc953bSthorpej name, what, nv->nv_str); 7895ecc953bSthorpej return (1); 7905ecc953bSthorpej } 7910001b928Schristos nv->nv_num = makedev(maj, unit * maxpartitions + part); 7925ecc953bSthorpej } 7935ecc953bSthorpej 7945ecc953bSthorpej nv->nv_name = dev->d_name; 7955ecc953bSthorpej return (0); 7965ecc953bSthorpej } 7975ecc953bSthorpej 7985ecc953bSthorpej /* 7995ecc953bSthorpej * Add a completed configuration to the list. 8005ecc953bSthorpej */ 8015ecc953bSthorpej void 8025ecc953bSthorpej addconf(struct config *cf0) 8035ecc953bSthorpej { 8045ecc953bSthorpej struct config *cf; 8055ecc953bSthorpej const char *name; 8065ecc953bSthorpej 8075ecc953bSthorpej name = cf0->cf_name; 8085ecc953bSthorpej cf = ecalloc(1, sizeof *cf); 8095ecc953bSthorpej if (ht_insert(cfhashtab, name, cf)) { 810c7295a4cSchristos cfgerror("configuration `%s' already defined", name); 8115ecc953bSthorpej free(cf); 8125ecc953bSthorpej goto bad; 8135ecc953bSthorpej } 8145ecc953bSthorpej *cf = *cf0; 8155ecc953bSthorpej 8165ecc953bSthorpej /* 8175ecc953bSthorpej * Resolve the root device. 8185ecc953bSthorpej */ 819cd429362Serh if (cf->cf_root == NULL) { 820c7295a4cSchristos cfgerror("%s: no root device specified", name); 8215ecc953bSthorpej goto bad; 8225ecc953bSthorpej } 823cd429362Serh if (cf->cf_root && cf->cf_root->nv_str != s_qmark) { 824cd429362Serh struct nvlist *nv; 825cd429362Serh nv = cf->cf_root; 8265ecc953bSthorpej if (resolve(&cf->cf_root, name, "root", nv, 'a')) 8275ecc953bSthorpej goto bad; 8285ecc953bSthorpej } 8295ecc953bSthorpej 8305ecc953bSthorpej /* 8315ecc953bSthorpej * Resolve the dump device. 8325ecc953bSthorpej */ 8335ecc953bSthorpej if (cf->cf_dump == NULL || cf->cf_dump->nv_str == s_qmark) { 8345ecc953bSthorpej /* 8355ecc953bSthorpej * Wildcarded dump device is equivalent to unspecified. 8365ecc953bSthorpej */ 8375ecc953bSthorpej cf->cf_dump = NULL; 8385ecc953bSthorpej } else if (cf->cf_dump->nv_str == s_none) { 8395ecc953bSthorpej /* 8405ecc953bSthorpej * Operator has requested that no dump device should be 8415ecc953bSthorpej * configured; do nothing. 8425ecc953bSthorpej */ 8435ecc953bSthorpej } else { 8445ecc953bSthorpej if (resolve(&cf->cf_dump, name, "dumps", cf->cf_dump, 'b')) 8455ecc953bSthorpej goto bad; 8465ecc953bSthorpej } 8475ecc953bSthorpej 8485ecc953bSthorpej /* Wildcarded fstype is `unspecified'. */ 8495ecc953bSthorpej if (cf->cf_fstype == s_qmark) 8505ecc953bSthorpej cf->cf_fstype = NULL; 8515ecc953bSthorpej 8525ecc953bSthorpej TAILQ_INSERT_TAIL(&allcf, cf, cf_next); 8535ecc953bSthorpej return; 8545ecc953bSthorpej bad: 8555ecc953bSthorpej nvfreel(cf0->cf_root); 8565ecc953bSthorpej nvfreel(cf0->cf_dump); 8575ecc953bSthorpej } 8585ecc953bSthorpej 8595ecc953bSthorpej void 8605ecc953bSthorpej setconf(struct nvlist **npp, const char *what, struct nvlist *v) 8615ecc953bSthorpej { 8625ecc953bSthorpej 8635ecc953bSthorpej if (*npp != NULL) { 864c7295a4cSchristos cfgerror("duplicate %s specification", what); 8655ecc953bSthorpej nvfreel(v); 8665ecc953bSthorpej } else 8675ecc953bSthorpej *npp = v; 8685ecc953bSthorpej } 8695ecc953bSthorpej 8705ecc953bSthorpej void 87142b52b8aScube delconf(const char *name) 87242b52b8aScube { 87342b52b8aScube struct config *cf; 87442b52b8aScube 87542b52b8aScube if (ht_lookup(cfhashtab, name) == NULL) { 876c7295a4cSchristos cfgerror("configuration `%s' undefined", name); 87742b52b8aScube return; 87842b52b8aScube } 87942b52b8aScube (void)ht_remove(cfhashtab, name); 88042b52b8aScube 88142b52b8aScube TAILQ_FOREACH(cf, &allcf, cf_next) 88242b52b8aScube if (!strcmp(cf->cf_name, name)) 88342b52b8aScube break; 88442b52b8aScube if (cf == NULL) 88542b52b8aScube panic("lost configuration `%s'", name); 88642b52b8aScube 88742b52b8aScube TAILQ_REMOVE(&allcf, cf, cf_next); 88842b52b8aScube } 88942b52b8aScube 89042b52b8aScube void 8915ecc953bSthorpej setfstype(const char **fstp, const char *v) 8925ecc953bSthorpej { 8935ecc953bSthorpej 8945ecc953bSthorpej if (*fstp != NULL) { 895c7295a4cSchristos cfgerror("multiple fstype specifications"); 8965ecc953bSthorpej return; 8975ecc953bSthorpej } 8985ecc953bSthorpej 8995ecc953bSthorpej if (v != s_qmark && OPT_FSOPT(v)) { 900c7295a4cSchristos cfgerror("\"%s\" is not a configured file system", v); 9015ecc953bSthorpej return; 9025ecc953bSthorpej } 9035ecc953bSthorpej 9045ecc953bSthorpej *fstp = v; 9055ecc953bSthorpej } 9065ecc953bSthorpej 9075ecc953bSthorpej static struct devi * 9085ecc953bSthorpej newdevi(const char *name, int unit, struct devbase *d) 9095ecc953bSthorpej { 9105ecc953bSthorpej struct devi *i; 9115ecc953bSthorpej 9125ecc953bSthorpej i = ecalloc(1, sizeof *i); 9135ecc953bSthorpej i->i_name = name; 9145ecc953bSthorpej i->i_unit = unit; 9155ecc953bSthorpej i->i_base = d; 9165ecc953bSthorpej i->i_bsame = NULL; 9175ecc953bSthorpej i->i_asame = NULL; 9185ecc953bSthorpej i->i_alias = NULL; 9195ecc953bSthorpej i->i_at = NULL; 9205ecc953bSthorpej i->i_pspec = NULL; 9215ecc953bSthorpej i->i_atdeva = NULL; 9225ecc953bSthorpej i->i_locs = NULL; 9235ecc953bSthorpej i->i_cfflags = 0; 9245ecc953bSthorpej i->i_lineno = currentline(); 9250dbd1c0eScube i->i_srcfile = yyfile; 9267aa6070dScube i->i_active = DEVI_ORPHAN; /* Proper analysis comes later */ 927a31ff6b4Scube i->i_level = devilevel; 92890ac64deSpooka i->i_pseudoroot = 0; 9295ecc953bSthorpej if (unit >= d->d_umax) 9305ecc953bSthorpej d->d_umax = unit + 1; 9315ecc953bSthorpej return (i); 9325ecc953bSthorpej } 9335ecc953bSthorpej 9345ecc953bSthorpej /* 9355ecc953bSthorpej * Add the named device as attaching to the named attribute (or perhaps 9365ecc953bSthorpej * another device instead) plus unit number. 9375ecc953bSthorpej */ 9385ecc953bSthorpej void 9395ecc953bSthorpej adddev(const char *name, const char *at, struct nvlist *loclist, int flags) 9405ecc953bSthorpej { 9415ecc953bSthorpej struct devi *i; /* the new instance */ 9425ecc953bSthorpej struct pspec *p; /* and its pspec */ 9435ecc953bSthorpej struct attr *attr; /* attribute that allows attach */ 9445ecc953bSthorpej struct devbase *ib; /* i->i_base */ 9455ecc953bSthorpej struct devbase *ab; /* not NULL => at another dev */ 9464caf067bSdholland struct attrlist *al; 9475ecc953bSthorpej struct deva *iba; /* devbase attachment used */ 9485ecc953bSthorpej const char *cp; 9495ecc953bSthorpej int atunit; 9505ecc953bSthorpej char atbuf[NAMESIZE]; 9515ecc953bSthorpej int hit; 9525ecc953bSthorpej 9535ecc953bSthorpej ab = NULL; 9545ecc953bSthorpej iba = NULL; 9555ecc953bSthorpej if (at == NULL) { 9565ecc953bSthorpej /* "at root" */ 9575ecc953bSthorpej p = NULL; 9585ecc953bSthorpej if ((i = getdevi(name)) == NULL) 9595ecc953bSthorpej goto bad; 9605ecc953bSthorpej /* 9615ecc953bSthorpej * Must warn about i_unit > 0 later, after taking care of 9625ecc953bSthorpej * the STAR cases (we could do non-star's here but why 9635ecc953bSthorpej * bother?). Make sure this device can be at root. 9645ecc953bSthorpej */ 9655ecc953bSthorpej ib = i->i_base; 9665ecc953bSthorpej hit = 0; 9675ecc953bSthorpej for (iba = ib->d_ahead; iba != NULL; iba = iba->d_bsame) 9685ecc953bSthorpej if (onlist(iba->d_atlist, NULL)) { 9695ecc953bSthorpej hit = 1; 9705ecc953bSthorpej break; 9715ecc953bSthorpej } 9725ecc953bSthorpej if (!hit) { 973c7295a4cSchristos cfgerror("`%s' cannot attach to the root", ib->d_name); 9745ec393e9Scube i->i_active = DEVI_BROKEN; 9755ecc953bSthorpej goto bad; 9765ecc953bSthorpej } 9775ecc953bSthorpej attr = &errattr; /* a convenient "empty" attr */ 9785ecc953bSthorpej } else { 9795ecc953bSthorpej if (split(at, strlen(at), atbuf, sizeof atbuf, &atunit)) { 980c7295a4cSchristos cfgerror("invalid attachment name `%s'", at); 9815ecc953bSthorpej /* (void)getdevi(name); -- ??? */ 9825ecc953bSthorpej goto bad; 9835ecc953bSthorpej } 9845ecc953bSthorpej if ((i = getdevi(name)) == NULL) 9855ecc953bSthorpej goto bad; 9865ecc953bSthorpej ib = i->i_base; 9875ecc953bSthorpej 9885ecc953bSthorpej /* 9895ecc953bSthorpej * Devices can attach to two types of things: Attributes, 9905ecc953bSthorpej * and other devices (which have the appropriate attributes 9915ecc953bSthorpej * to allow attachment). 9925ecc953bSthorpej * 9935ecc953bSthorpej * (1) If we're attached to an attribute, then we don't need 9945ecc953bSthorpej * look at the parent base device to see what attributes 9955ecc953bSthorpej * it has, and make sure that we can attach to them. 9965ecc953bSthorpej * 9975ecc953bSthorpej * (2) If we're attached to a real device (i.e. named in 9985ecc953bSthorpej * the config file), we want to remember that so that 9995ecc953bSthorpej * at cross-check time, if the device we're attached to 10005ecc953bSthorpej * is missing but other devices which also provide the 10015ecc953bSthorpej * attribute are present, we don't get a false "OK." 10025ecc953bSthorpej * 10035ecc953bSthorpej * (3) If the thing we're attached to is an attribute 10045ecc953bSthorpej * but is actually named in the config file, we still 10055ecc953bSthorpej * have to remember its devbase. 10065ecc953bSthorpej */ 10075ecc953bSthorpej cp = intern(atbuf); 10085ecc953bSthorpej 10095ecc953bSthorpej /* Figure out parent's devbase, to satisfy case (3). */ 10105ecc953bSthorpej ab = ht_lookup(devbasetab, cp); 10115ecc953bSthorpej 10125ecc953bSthorpej /* Find out if it's an attribute. */ 10135ecc953bSthorpej attr = ht_lookup(attrtab, cp); 10145ecc953bSthorpej 10155ecc953bSthorpej /* Make sure we're _really_ attached to the attr. Case (1). */ 10165ecc953bSthorpej if (attr != NULL && onlist(attr->a_devs, ib)) 10175ecc953bSthorpej goto findattachment; 10185ecc953bSthorpej 10195ecc953bSthorpej /* 10205ecc953bSthorpej * Else a real device, and not just an attribute. Case (2). 10215ecc953bSthorpej * 10225ecc953bSthorpej * Have to work a bit harder to see whether we have 10235ecc953bSthorpej * something like "tg0 at esp0" (where esp is merely 10245ecc953bSthorpej * not an attribute) or "tg0 at nonesuch0" (where 10255ecc953bSthorpej * nonesuch is not even a device). 10265ecc953bSthorpej */ 10275ecc953bSthorpej if (ab == NULL) { 1028c7295a4cSchristos cfgerror("%s at %s: `%s' unknown", 10295ecc953bSthorpej name, at, atbuf); 10305ec393e9Scube i->i_active = DEVI_BROKEN; 10315ecc953bSthorpej goto bad; 10325ecc953bSthorpej } 10335ecc953bSthorpej 10345ecc953bSthorpej /* 10355ecc953bSthorpej * See if the named parent carries an attribute 10365ecc953bSthorpej * that allows it to supervise device ib. 10375ecc953bSthorpej */ 10384caf067bSdholland for (al = ab->d_attrs; al != NULL; al = al->al_next) { 10394caf067bSdholland attr = al->al_this; 10405ecc953bSthorpej if (onlist(attr->a_devs, ib)) 10415ecc953bSthorpej goto findattachment; 10425ecc953bSthorpej } 1043c7295a4cSchristos cfgerror("`%s' cannot attach to `%s'", ib->d_name, atbuf); 10445ec393e9Scube i->i_active = DEVI_BROKEN; 10455ecc953bSthorpej goto bad; 10465ecc953bSthorpej 10475ecc953bSthorpej findattachment: 10485ecc953bSthorpej /* 10495ecc953bSthorpej * Find the parent spec. If a matching one has not yet been 10505ecc953bSthorpej * created, create one. 10515ecc953bSthorpej */ 10525ecc953bSthorpej p = getpspec(attr, ab, atunit); 10535ecc953bSthorpej p->p_devs = newnv(NULL, NULL, i, 0, p->p_devs); 10545ecc953bSthorpej 10555ecc953bSthorpej /* find out which attachment it uses */ 10565ecc953bSthorpej hit = 0; 10575ecc953bSthorpej for (iba = ib->d_ahead; iba != NULL; iba = iba->d_bsame) 10585ecc953bSthorpej if (onlist(iba->d_atlist, attr)) { 10595ecc953bSthorpej hit = 1; 10605ecc953bSthorpej break; 10615ecc953bSthorpej } 10625ecc953bSthorpej if (!hit) 10635ecc953bSthorpej panic("adddev: can't figure out attachment"); 10645ecc953bSthorpej } 10655ec393e9Scube if ((i->i_locs = fixloc(name, attr, loclist)) == NULL) { 10665ec393e9Scube i->i_active = DEVI_BROKEN; 10675ecc953bSthorpej goto bad; 10685ec393e9Scube } 10695ecc953bSthorpej i->i_at = at; 10705ecc953bSthorpej i->i_pspec = p; 10715ecc953bSthorpej i->i_atdeva = iba; 10725ecc953bSthorpej i->i_cfflags = flags; 10735ecc953bSthorpej 10745ecc953bSthorpej *iba->d_ipp = i; 10755ecc953bSthorpej iba->d_ipp = &i->i_asame; 10765ecc953bSthorpej 10775ecc953bSthorpej /* all done, fall into ... */ 10785ecc953bSthorpej bad: 10795ecc953bSthorpej nvfreel(loclist); 10805ecc953bSthorpej return; 10815ecc953bSthorpej } 10825ecc953bSthorpej 10835ecc953bSthorpej void 10847b7c582aScube deldevi(const char *name, const char *at) 10855ecc953bSthorpej { 10867b7c582aScube struct devi *firsti, *i; 10875ecc953bSthorpej struct devbase *d; 10885ecc953bSthorpej int unit; 10895ecc953bSthorpej char base[NAMESIZE]; 10905ecc953bSthorpej 10915ecc953bSthorpej if (split(name, strlen(name), base, sizeof base, &unit)) { 1092c7295a4cSchristos cfgerror("invalid device name `%s'", name); 10935ecc953bSthorpej return; 10945ecc953bSthorpej } 10955ecc953bSthorpej d = ht_lookup(devbasetab, intern(base)); 10965ecc953bSthorpej if (d == NULL) { 1097c7295a4cSchristos cfgerror("%s: unknown device `%s'", name, base); 10985ecc953bSthorpej return; 10995ecc953bSthorpej } 11005ecc953bSthorpej if (d->d_ispseudo) { 1101c7295a4cSchristos cfgerror("%s: %s is a pseudo-device", name, base); 11025ecc953bSthorpej return; 11035ecc953bSthorpej } 11045ecc953bSthorpej if ((firsti = ht_lookup(devitab, name)) == NULL) { 1105c7295a4cSchristos cfgerror("`%s' not defined", name); 11065ecc953bSthorpej return; 11075ecc953bSthorpej } 11087b7c582aScube if (at == NULL && firsti->i_at == NULL) { 1109e499d8b5Scube /* 'at root' */ 11107b7c582aScube remove_devi(firsti); 11117b7c582aScube return; 11127b7c582aScube } else if (at != NULL) 11137b7c582aScube for (i = firsti; i != NULL; i = i->i_alias) 1114bfc250f2Scube if (i->i_active != DEVI_BROKEN && 1115bfc250f2Scube strcmp(at, i->i_at) == 0) { 11167b7c582aScube remove_devi(i); 11175ecc953bSthorpej return; 11185ecc953bSthorpej } 1119c7295a4cSchristos cfgerror("`%s' at `%s' not found", name, at ? at : "root"); 1120e499d8b5Scube } 11215ecc953bSthorpej 11227b7c582aScube static void 11237b7c582aScube remove_devi(struct devi *i) 11247b7c582aScube { 11257b7c582aScube struct devbase *d = i->i_base; 11267b7c582aScube struct devi *f, *j, **ppi; 11277b7c582aScube struct deva *iba; 11287b7c582aScube 11297b7c582aScube f = ht_lookup(devitab, i->i_name); 11306a6299ebScube if (f == NULL) 11316a6299ebScube panic("remove_devi(): instance %s disappeared from devitab", 11326a6299ebScube i->i_name); 11337b7c582aScube 1134bfc250f2Scube if (i->i_active == DEVI_BROKEN) { 1135bfc250f2Scube cfgerror("not removing broken instance `%s'", i->i_name); 1136bfc250f2Scube return; 1137bfc250f2Scube } 1138bfc250f2Scube 1139e499d8b5Scube /* 1140e499d8b5Scube * We have the device instance, i. 1141e499d8b5Scube * We have to: 1142e499d8b5Scube * - delete the alias 1143e499d8b5Scube * 1144e499d8b5Scube * If the devi was an alias of an already listed devi, all is 1145e499d8b5Scube * good we don't have to do more. 1146e499d8b5Scube * If it was the first alias, we have to replace i's entry in 1147e499d8b5Scube * d's list by its first alias. 1148e499d8b5Scube * If it was the only entry, we must remove i's entry from d's 1149e499d8b5Scube * list. 1150e499d8b5Scube */ 11517b7c582aScube if (i != f) { 11527b7c582aScube for (j = f; j->i_alias != i; j = j->i_alias); 11537b7c582aScube j->i_alias = i->i_alias; 11543b405775Scube } else { 11553b405775Scube if (i->i_alias == NULL) { 1156e499d8b5Scube /* No alias, must unlink the entry from devitab */ 11577b7c582aScube ht_remove(devitab, i->i_name); 11587b7c582aScube j = i->i_bsame; 11593b405775Scube } else { 1160e499d8b5Scube /* Or have the first alias replace i in d's list */ 1161e499d8b5Scube i->i_alias->i_bsame = i->i_bsame; 11627b7c582aScube j = i->i_alias; 11637b7c582aScube if (i == f) 11647b7c582aScube ht_replace(devitab, i->i_name, i->i_alias); 11653b405775Scube } 11663b405775Scube 1167e499d8b5Scube /* 1168e499d8b5Scube * - remove/replace the instance from the devbase's list 1169e499d8b5Scube * 1170e499d8b5Scube * A double-linked list would make this much easier. Oh, well, 1171e499d8b5Scube * what is done is done. 1172e499d8b5Scube */ 1173e499d8b5Scube for (ppi = &d->d_ihead; 1174e499d8b5Scube *ppi != NULL && *ppi != i && (*ppi)->i_bsame != i; 1175e499d8b5Scube ppi = &(*ppi)->i_bsame); 1176e499d8b5Scube if (*ppi == NULL) 1177c3414672Scube panic("deldev: dev (%s) doesn't list the devi" 1178c3414672Scube " (%s at %s)", d->d_name, i->i_name, i->i_at); 11797b7c582aScube f = *ppi; 11807b7c582aScube if (f == i) 1181c3414672Scube /* That implies d->d_ihead == i */ 11827b7c582aScube *ppi = j; 1183e499d8b5Scube else 11847b7c582aScube (*ppi)->i_bsame = j; 1185e499d8b5Scube if (d->d_ipp == &i->i_bsame) { 11863b405775Scube if (i->i_alias == NULL) { 11877b7c582aScube if (f == i) 1188e499d8b5Scube d->d_ipp = &d->d_ihead; 1189e499d8b5Scube else 11907b7c582aScube d->d_ipp = &f->i_bsame; 11913b405775Scube } else 11923b405775Scube d->d_ipp = &i->i_alias->i_bsame; 1193e499d8b5Scube } 1194e499d8b5Scube } 1195e499d8b5Scube /* 1196e499d8b5Scube * - delete the attachment instance 1197e499d8b5Scube */ 1198e499d8b5Scube iba = i->i_atdeva; 1199e499d8b5Scube for (ppi = &iba->d_ihead; 1200e499d8b5Scube *ppi != NULL && *ppi != i && (*ppi)->i_asame != i; 1201e499d8b5Scube ppi = &(*ppi)->i_asame); 1202e499d8b5Scube if (*ppi == NULL) 1203e499d8b5Scube panic("deldev: deva (%s) doesn't list the devi (%s)", 1204e499d8b5Scube iba->d_name, i->i_name); 12057b7c582aScube f = *ppi; 12067b7c582aScube if (f == i) 1207c3414672Scube /* That implies iba->d_ihead == i */ 1208e499d8b5Scube *ppi = i->i_asame; 1209e499d8b5Scube else 1210e499d8b5Scube (*ppi)->i_asame = i->i_asame; 1211e499d8b5Scube if (iba->d_ipp == &i->i_asame) { 12127b7c582aScube if (f == i) 1213e499d8b5Scube iba->d_ipp = &iba->d_ihead; 1214e499d8b5Scube else 12157b7c582aScube iba->d_ipp = &f->i_asame; 1216e499d8b5Scube } 1217e499d8b5Scube /* 1218e499d8b5Scube * - delete the pspec 1219e499d8b5Scube */ 1220e499d8b5Scube if (i->i_pspec) { 1221e499d8b5Scube struct pspec *p = i->i_pspec; 1222e499d8b5Scube struct nvlist *nv, *onv; 1223e499d8b5Scube 1224e499d8b5Scube /* Double-linked nvlist anyone? */ 1225c0024066Scube for (nv = p->p_devs; nv->nv_next != NULL; nv = nv->nv_next) { 1226e499d8b5Scube if (nv->nv_next && nv->nv_next->nv_ptr == i) { 1227e499d8b5Scube onv = nv->nv_next; 1228e499d8b5Scube nv->nv_next = onv->nv_next; 1229e499d8b5Scube nvfree(onv); 1230e499d8b5Scube break; 1231c0024066Scube } 1232c0024066Scube if (nv->nv_ptr == i) { 1233e499d8b5Scube /* nv is p->p_devs in that case */ 1234e499d8b5Scube p->p_devs = nv->nv_next; 1235e499d8b5Scube nvfree(nv); 1236e499d8b5Scube break; 1237e499d8b5Scube } 1238e499d8b5Scube } 1239e499d8b5Scube if (p->p_devs == NULL) 1240e499d8b5Scube TAILQ_REMOVE(&allpspecs, p, p_list); 1241e499d8b5Scube } 1242e499d8b5Scube /* 1243e499d8b5Scube * - delete the alldevi entry 1244e499d8b5Scube */ 1245e499d8b5Scube TAILQ_REMOVE(&alldevi, i, i_next); 1246e499d8b5Scube ndevi--; 12477aa6070dScube /* 12487aa6070dScube * Put it in deaddevitab 1249a31ff6b4Scube * 1250a31ff6b4Scube * Each time a devi is removed, devilevel is increased so that later on 1251a31ff6b4Scube * it is possible to tell if an instance was added before or after the 1252a31ff6b4Scube * removal of its parent. 1253a31ff6b4Scube * 1254a31ff6b4Scube * For active instances, i_level contains the number of devi removed so 1255a31ff6b4Scube * far, and for dead devis, it contains its index. 12567aa6070dScube */ 1257a31ff6b4Scube i->i_level = devilevel++; 12587aa6070dScube i->i_alias = NULL; 12597aa6070dScube f = ht_lookup(deaddevitab, i->i_name); 12607aa6070dScube if (f == NULL) { 12617aa6070dScube if (ht_insert(deaddevitab, i->i_name, i)) 12627aa6070dScube panic("remove_devi(%s) - can't add to deaddevitab", 12637aa6070dScube i->i_name); 12647aa6070dScube } else { 12657aa6070dScube for (j = f; j->i_alias != NULL; j = j->i_alias); 12667aa6070dScube j->i_alias = i; 12677aa6070dScube } 1268e499d8b5Scube /* 12695d1e8b27Swiz * - reconstruct d->d_umax 1270e499d8b5Scube */ 1271e499d8b5Scube d->d_umax = 0; 1272e499d8b5Scube for (i = d->d_ihead; i != NULL; i = i->i_bsame) 1273e499d8b5Scube if (i->i_unit >= d->d_umax) 1274e499d8b5Scube d->d_umax = i->i_unit + 1; 12755ecc953bSthorpej } 12765ecc953bSthorpej 12775ecc953bSthorpej void 12787b7c582aScube deldeva(const char *at) 12797b7c582aScube { 12807b7c582aScube int unit; 12817b7c582aScube const char *cp; 12827b7c582aScube struct devbase *d, *ad; 12837b7c582aScube struct devi *i, *j; 12847b7c582aScube struct attr *a; 12857b7c582aScube struct pspec *p; 12867b7c582aScube struct nvlist *nv, *stack = NULL; 12877b7c582aScube 12887b7c582aScube if (at == NULL) { 12897b7c582aScube TAILQ_FOREACH(i, &alldevi, i_next) 12907b7c582aScube if (i->i_at == NULL) 12917b7c582aScube stack = newnv(NULL, NULL, i, 0, stack); 12927b7c582aScube } else { 12937b7c582aScube int l; 12947b7c582aScube 12957b7c582aScube l = strlen(at) - 1; 12967b7c582aScube if (at[l] == '?' || isdigit((unsigned char)at[l])) { 12977b7c582aScube char base[NAMESIZE]; 12987b7c582aScube 12997b7c582aScube if (split(at, l+1, base, sizeof base, &unit)) { 1300c7295a4cSchristos cfgerror("invalid attachment name `%s'", at); 13017b7c582aScube return; 13027b7c582aScube } 13037b7c582aScube cp = intern(base); 13047b7c582aScube } else { 13057b7c582aScube cp = intern(at); 13067b7c582aScube unit = STAR; 13077b7c582aScube } 13087b7c582aScube 13097b7c582aScube ad = ht_lookup(devbasetab, cp); 13107b7c582aScube a = ht_lookup(attrtab, cp); 13117b7c582aScube if (a == NULL) { 1312c7295a4cSchristos cfgerror("unknown attachment attribute or device `%s'", 13137b7c582aScube cp); 13147b7c582aScube return; 13157b7c582aScube } 13167b7c582aScube if (!a->a_iattr) { 1317c7295a4cSchristos cfgerror("plain attribute `%s' cannot have children", 13187b7c582aScube a->a_name); 13197b7c582aScube return; 13207b7c582aScube } 13217b7c582aScube 13227b7c582aScube /* 13237b7c582aScube * remove_devi() makes changes to the devbase's list and the 13247b7c582aScube * alias list, * so the actual deletion of the instances must 13257b7c582aScube * be delayed. 13267b7c582aScube */ 13277b7c582aScube for (nv = a->a_devs; nv != NULL; nv = nv->nv_next) { 13287b7c582aScube d = nv->nv_ptr; 13297b7c582aScube for (i = d->d_ihead; i != NULL; i = i->i_bsame) 13307b7c582aScube for (j = i; j != NULL; j = j->i_alias) { 13317b7c582aScube /* Ignore devices at root */ 13327b7c582aScube if (j->i_at == NULL) 13337b7c582aScube continue; 13347b7c582aScube p = j->i_pspec; 13357b7c582aScube /* 13367b7c582aScube * There are three cases: 13377b7c582aScube * 13387b7c582aScube * 1. unit is not STAR. Consider 'at' 13397b7c582aScube * to be explicit, even if it 13407b7c582aScube * references an interface 13417b7c582aScube * attribute. 13427b7c582aScube * 13437b7c582aScube * 2. unit is STAR and 'at' references 13447b7c582aScube * a real device. Look for pspec 13457b7c582aScube * that have a matching p_atdev 13467b7c582aScube * field. 13477b7c582aScube * 13487b7c582aScube * 3. unit is STAR and 'at' references 13497b7c582aScube * an interface attribute. Look 13507b7c582aScube * for pspec that have a matching 13517b7c582aScube * p_iattr field. 13527b7c582aScube */ 13537b7c582aScube if ((unit != STAR && /* Case */ 13547b7c582aScube !strcmp(j->i_at, at)) || /* 1 */ 13557b7c582aScube (unit == STAR && 13567b7c582aScube ((ad != NULL && /* Case */ 13577b7c582aScube p->p_atdev == ad) || /* 2 */ 13587b7c582aScube (ad == NULL && /* Case */ 13597b7c582aScube p->p_iattr == a)))) /* 3 */ 13607b7c582aScube stack = newnv(NULL, NULL, j, 0, 13617b7c582aScube stack); 13627b7c582aScube } 13637b7c582aScube } 13647b7c582aScube } 13657b7c582aScube 13667b7c582aScube for (nv = stack; nv != NULL; nv = nv->nv_next) 13677b7c582aScube remove_devi(nv->nv_ptr); 1368e50e4ee4Scube nvfreel(stack); 13697b7c582aScube } 13707b7c582aScube 13717b7c582aScube void 13727b7c582aScube deldev(const char *name) 13737b7c582aScube { 13747b7c582aScube int l; 13757b7c582aScube struct devi *firsti, *i; 13767b7c582aScube struct nvlist *nv, *stack = NULL; 13777b7c582aScube 13787b7c582aScube l = strlen(name) - 1; 13797b7c582aScube if (name[l] == '*' || isdigit((unsigned char)name[l])) { 13807b7c582aScube /* `no mydev0' or `no mydev*' */ 13817b7c582aScube firsti = ht_lookup(devitab, name); 13827b7c582aScube if (firsti == NULL) { 1383c7295a4cSchristos cfgerror("unknown instance %s", name); 13847b7c582aScube return; 13857b7c582aScube } 13867b7c582aScube for (i = firsti; i != NULL; i = i->i_alias) 13877b7c582aScube stack = newnv(NULL, NULL, i, 0, stack); 13887b7c582aScube } else { 13897b7c582aScube struct devbase *d = ht_lookup(devbasetab, name); 13907b7c582aScube 13917b7c582aScube if (d == NULL) { 1392c7295a4cSchristos cfgerror("unknown device %s", name); 13937b7c582aScube return; 13947b7c582aScube } 139571af9028Scube if (d->d_ispseudo) { 1396c7295a4cSchristos cfgerror("%s is a pseudo-device; " 139771af9028Scube "use \"no pseudo-device %s\" instead", name, 139871af9028Scube name); 139971af9028Scube return; 140071af9028Scube } 14017b7c582aScube 14027b7c582aScube for (firsti = d->d_ihead; firsti != NULL; 14037b7c582aScube firsti = firsti->i_bsame) 14047b7c582aScube for (i = firsti; i != NULL; i = i->i_alias) 14057b7c582aScube stack = newnv(NULL, NULL, i, 0, stack); 14067b7c582aScube } 14077b7c582aScube 14087b7c582aScube for (nv = stack; nv != NULL; nv = nv->nv_next) 14097b7c582aScube remove_devi(nv->nv_ptr); 1410e50e4ee4Scube nvfreel(stack); 14117b7c582aScube } 14127b7c582aScube 141300ed8a38Spooka /* 141400ed8a38Spooka * Insert given device "name" into devroottab. In case "name" 141500ed8a38Spooka * designates a pure interface attribute, create a fake device 141600ed8a38Spooka * instance for the attribute and insert that into the roottab 141700ed8a38Spooka * (this scheme avoids mucking around with the orphanage analysis). 141800ed8a38Spooka */ 14197b7c582aScube void 142090ac64deSpooka addpseudoroot(const char *name) 142190ac64deSpooka { 1422194e1c80Spooka char buf[NAMESIZE]; 1423194e1c80Spooka int unit; 1424194e1c80Spooka struct attr *attr; 142590ac64deSpooka struct devi *i; 142690ac64deSpooka struct deva *iba; 142790ac64deSpooka struct devbase *ib; 142890ac64deSpooka 142990ac64deSpooka fprintf(stderr, "WARNING: pseudo-root is an experimental feature\n"); 143090ac64deSpooka 1431194e1c80Spooka if (split(name, strlen(name), buf, sizeof(buf), &unit)) { 1432194e1c80Spooka cfgerror("invalid pseudo-root name `%s'", name); 1433194e1c80Spooka return; 1434194e1c80Spooka } 1435194e1c80Spooka 1436194e1c80Spooka /* 143700ed8a38Spooka * Prefer device because devices with locators define an 143800ed8a38Spooka * implicit interface attribute. However, if a device is 143900ed8a38Spooka * not available, try to attach to the interface attribute. 144000ed8a38Spooka * This makes sure adddev() doesn't get confused when we 144100ed8a38Spooka * are really attaching to a device (alternatively we maybe 144200ed8a38Spooka * could specify a non-NULL atlist to defdevattach() below). 1443194e1c80Spooka */ 144400ed8a38Spooka ib = ht_lookup(devbasetab, intern(buf)); 144500ed8a38Spooka if (ib == NULL) { 1446194e1c80Spooka struct devbase *fakedev; 1447194e1c80Spooka char fakename[NAMESIZE]; 1448194e1c80Spooka 144900ed8a38Spooka attr = ht_lookup(attrtab, intern(buf)); 145000ed8a38Spooka if (!(attr && attr->a_iattr)) { 145100ed8a38Spooka cfgerror("pseudo-root `%s' not available", name); 145200ed8a38Spooka return; 145300ed8a38Spooka } 145400ed8a38Spooka 1455194e1c80Spooka /* 1456194e1c80Spooka * here we cheat a bit: create a fake devbase with the 1457194e1c80Spooka * interface attribute and instantiate it. quick, cheap, 1458194e1c80Spooka * dirty & bad for you, much like the stuff in the fridge. 1459194e1c80Spooka * and, it works, since the pseudoroot device is not included 1460194e1c80Spooka * in ioconf, just used by config to make sure we start from 1461194e1c80Spooka * the right place. 1462194e1c80Spooka */ 146300ed8a38Spooka snprintf(fakename, sizeof(fakename), "%s_devattrs", buf); 1464194e1c80Spooka fakedev = getdevbase(intern(fakename)); 1465194e1c80Spooka fakedev->d_isdef = 1; 1466194e1c80Spooka fakedev->d_ispseudo = 0; 14674caf067bSdholland fakedev->d_attrs = attrlist_cons(NULL, attr); 1468194e1c80Spooka defdevattach(NULL, fakedev, NULL, NULL); 1469194e1c80Spooka 1470194e1c80Spooka if (unit == STAR) 1471194e1c80Spooka snprintf(buf, sizeof(buf), "%s*", fakename); 1472194e1c80Spooka else 1473194e1c80Spooka snprintf(buf, sizeof(buf), "%s%d", fakename, unit); 1474194e1c80Spooka name = buf; 1475194e1c80Spooka } 1476194e1c80Spooka 1477194e1c80Spooka /* ok, everything should be set up, so instantiate a fake device */ 147890ac64deSpooka i = getdevi(name); 147990ac64deSpooka if (i == NULL) 148000ed8a38Spooka panic("device `%s' expected to be present", name); 148190ac64deSpooka ib = i->i_base; 1482194e1c80Spooka iba = ib->d_ahead; 148390ac64deSpooka 148490ac64deSpooka i->i_atdeva = iba; 148590ac64deSpooka i->i_cfflags = 0; 148690ac64deSpooka i->i_locs = fixloc(name, &errattr, NULL); 148790ac64deSpooka i->i_pseudoroot = 1; 148890ac64deSpooka i->i_active = DEVI_ORPHAN; /* set active by kill_orphans() */ 148990ac64deSpooka 149090ac64deSpooka *iba->d_ipp = i; 149190ac64deSpooka iba->d_ipp = &i->i_asame; 149290ac64deSpooka 149390ac64deSpooka ht_insert(devroottab, ib->d_name, ib); 149490ac64deSpooka } 149590ac64deSpooka 149690ac64deSpooka void 14975ecc953bSthorpej addpseudo(const char *name, int number) 14985ecc953bSthorpej { 14995ecc953bSthorpej struct devbase *d; 15005ecc953bSthorpej struct devi *i; 15015ecc953bSthorpej 15025ecc953bSthorpej d = ht_lookup(devbasetab, name); 15035ecc953bSthorpej if (d == NULL) { 1504c7295a4cSchristos cfgerror("undefined pseudo-device %s", name); 15055ecc953bSthorpej return; 15065ecc953bSthorpej } 15075ecc953bSthorpej if (!d->d_ispseudo) { 1508c7295a4cSchristos cfgerror("%s is a real device, not a pseudo-device", name); 15095ecc953bSthorpej return; 15105ecc953bSthorpej } 15115ecc953bSthorpej if (ht_lookup(devitab, name) != NULL) { 1512c7295a4cSchristos cfgerror("`%s' already defined", name); 15135ecc953bSthorpej return; 15145ecc953bSthorpej } 15155ecc953bSthorpej i = newdevi(name, number - 1, d); /* foo 16 => "foo0..foo15" */ 15165ecc953bSthorpej if (ht_insert(devitab, name, i)) 15175ecc953bSthorpej panic("addpseudo(%s)", name); 15187b7c582aScube /* Useful to retrieve the instance from the devbase */ 15197b7c582aScube d->d_ihead = i; 15207aa6070dScube i->i_active = DEVI_ACTIVE; 15215ecc953bSthorpej TAILQ_INSERT_TAIL(&allpseudo, i, i_next); 15225ecc953bSthorpej } 15235ecc953bSthorpej 15245ecc953bSthorpej void 15255ecc953bSthorpej delpseudo(const char *name) 15265ecc953bSthorpej { 15275ecc953bSthorpej struct devbase *d; 15285ecc953bSthorpej struct devi *i; 15295ecc953bSthorpej 15305ecc953bSthorpej d = ht_lookup(devbasetab, name); 15315ecc953bSthorpej if (d == NULL) { 1532c7295a4cSchristos cfgerror("undefined pseudo-device %s", name); 15335ecc953bSthorpej return; 15345ecc953bSthorpej } 15355ecc953bSthorpej if (!d->d_ispseudo) { 1536c7295a4cSchristos cfgerror("%s is a real device, not a pseudo-device", name); 15375ecc953bSthorpej return; 15385ecc953bSthorpej } 15395ecc953bSthorpej if ((i = ht_lookup(devitab, name)) == NULL) { 1540c7295a4cSchristos cfgerror("`%s' not defined", name); 15415ecc953bSthorpej return; 15425ecc953bSthorpej } 15435ecc953bSthorpej d->d_umax = 0; /* clear neads-count entries */ 15447aa6070dScube d->d_ihead = NULL; /* make sure it won't be considered active */ 15455ecc953bSthorpej TAILQ_REMOVE(&allpseudo, i, i_next); 15465ecc953bSthorpej if (ht_remove(devitab, name)) 15475ecc953bSthorpej panic("delpseudo(%s) - can't remove from devitab", name); 15487aa6070dScube if (ht_insert(deaddevitab, name, i)) 15497aa6070dScube panic("delpseudo(%s) - can't add to deaddevitab", name); 15505ecc953bSthorpej } 15515ecc953bSthorpej 15525ecc953bSthorpej void 1553d767912bSdrochner adddevm(const char *name, devmajor_t cmajor, devmajor_t bmajor, 1554*9483bda7Sdholland struct condexpr *cond, struct nvlist *nv_nodes) 15555ecc953bSthorpej { 15565ecc953bSthorpej struct devm *dm; 15575ecc953bSthorpej 1558d767912bSdrochner if (cmajor != NODEVMAJOR && (cmajor < 0 || cmajor >= 4096)) { 1559c7295a4cSchristos cfgerror("character major %d is invalid", cmajor); 1560*9483bda7Sdholland condexpr_destroy(cond); 15613da3ab25Spooka nvfreel(nv_nodes); 15625ecc953bSthorpej return; 15635ecc953bSthorpej } 15645ecc953bSthorpej 1565d767912bSdrochner if (bmajor != NODEVMAJOR && (bmajor < 0 || bmajor >= 4096)) { 1566c7295a4cSchristos cfgerror("block major %d is invalid", bmajor); 1567*9483bda7Sdholland condexpr_destroy(cond); 15683da3ab25Spooka nvfreel(nv_nodes); 15695ecc953bSthorpej return; 15705ecc953bSthorpej } 1571d767912bSdrochner if (cmajor == NODEVMAJOR && bmajor == NODEVMAJOR) { 1572c7295a4cSchristos cfgerror("both character/block majors are not specified"); 1573*9483bda7Sdholland condexpr_destroy(cond); 15743da3ab25Spooka nvfreel(nv_nodes); 15755ecc953bSthorpej return; 15765ecc953bSthorpej } 15775ecc953bSthorpej 15785ecc953bSthorpej dm = ecalloc(1, sizeof(*dm)); 15795ecc953bSthorpej dm->dm_srcfile = yyfile; 15805ecc953bSthorpej dm->dm_srcline = currentline(); 15815ecc953bSthorpej dm->dm_name = name; 15825ecc953bSthorpej dm->dm_cmajor = cmajor; 15835ecc953bSthorpej dm->dm_bmajor = bmajor; 1584*9483bda7Sdholland dm->dm_opts = cond; 15853da3ab25Spooka dm->dm_devnodes = nv_nodes; 15865ecc953bSthorpej 15875ecc953bSthorpej TAILQ_INSERT_TAIL(&alldevms, dm, dm_next); 15885ecc953bSthorpej 15895ecc953bSthorpej maxcdevm = MAX(maxcdevm, dm->dm_cmajor); 15905ecc953bSthorpej maxbdevm = MAX(maxbdevm, dm->dm_bmajor); 15915ecc953bSthorpej } 15925ecc953bSthorpej 15937aa6070dScube int 15945ecc953bSthorpej fixdevis(void) 15955ecc953bSthorpej { 15965ecc953bSthorpej struct devi *i; 15977aa6070dScube int error = 0; 15985ecc953bSthorpej 15995ecc953bSthorpej TAILQ_FOREACH(i, &alldevi, i_next) 16007aa6070dScube if (i->i_active == DEVI_ACTIVE) 16015ecc953bSthorpej selectbase(i->i_base, i->i_atdeva); 16027aa6070dScube else if (i->i_active == DEVI_ORPHAN) { 1603c130d400Scube /* 1604c130d400Scube * At this point, we can't have instances for which 1605c130d400Scube * i_at or i_pspec are NULL. 1606c130d400Scube */ 16077aa6070dScube ++error; 1608c7295a4cSchristos cfgxerror(i->i_srcfile, i->i_lineno, 16090dbd1c0eScube "`%s at %s' is orphaned (%s `%s' found)", 1610c130d400Scube i->i_name, i->i_at, i->i_pspec->p_atunit == WILD ? 1611c130d400Scube "nothing matching" : "no", i->i_at); 16127aa6070dScube } else if (vflag && i->i_active == DEVI_IGNORED) 1613c7295a4cSchristos cfgxwarn(i->i_srcfile, i->i_lineno, "ignoring " 1614c7295a4cSchristos "explicitly orphaned instance `%s at %s'", 1615c7295a4cSchristos i->i_name, i->i_at); 16167aa6070dScube 16177aa6070dScube if (error) 16187aa6070dScube return error; 16195ecc953bSthorpej 16205ecc953bSthorpej TAILQ_FOREACH(i, &allpseudo, i_next) 16217aa6070dScube if (i->i_active == DEVI_ACTIVE) 16225ecc953bSthorpej selectbase(i->i_base, NULL); 16237aa6070dScube return 0; 16245ecc953bSthorpej } 16255ecc953bSthorpej 16265ecc953bSthorpej /* 16275ecc953bSthorpej * Look up a parent spec, creating a new one if it does not exist. 16285ecc953bSthorpej */ 16295ecc953bSthorpej static struct pspec * 16305ecc953bSthorpej getpspec(struct attr *attr, struct devbase *ab, int atunit) 16315ecc953bSthorpej { 16325ecc953bSthorpej struct pspec *p; 16335ecc953bSthorpej 16345ecc953bSthorpej TAILQ_FOREACH(p, &allpspecs, p_list) { 16355ecc953bSthorpej if (p->p_iattr == attr && 16365ecc953bSthorpej p->p_atdev == ab && 16375ecc953bSthorpej p->p_atunit == atunit) 16385ecc953bSthorpej return (p); 16395ecc953bSthorpej } 16405ecc953bSthorpej 16415ecc953bSthorpej p = ecalloc(1, sizeof(*p)); 16425ecc953bSthorpej 16435ecc953bSthorpej p->p_iattr = attr; 16445ecc953bSthorpej p->p_atdev = ab; 16455ecc953bSthorpej p->p_atunit = atunit; 16465ecc953bSthorpej p->p_inst = npspecs++; 1647c130d400Scube p->p_active = 0; 16485ecc953bSthorpej 16495ecc953bSthorpej TAILQ_INSERT_TAIL(&allpspecs, p, p_list); 16505ecc953bSthorpej 16515ecc953bSthorpej return (p); 16525ecc953bSthorpej } 16535ecc953bSthorpej 16545ecc953bSthorpej /* 16555ecc953bSthorpej * Define a new instance of a specific device. 16565ecc953bSthorpej */ 16575ecc953bSthorpej static struct devi * 16585ecc953bSthorpej getdevi(const char *name) 16595ecc953bSthorpej { 16605ecc953bSthorpej struct devi *i, *firsti; 16615ecc953bSthorpej struct devbase *d; 16625ecc953bSthorpej int unit; 16635ecc953bSthorpej char base[NAMESIZE]; 16645ecc953bSthorpej 16655ecc953bSthorpej if (split(name, strlen(name), base, sizeof base, &unit)) { 1666c7295a4cSchristos cfgerror("invalid device name `%s'", name); 16675ecc953bSthorpej return (NULL); 16685ecc953bSthorpej } 16695ecc953bSthorpej d = ht_lookup(devbasetab, intern(base)); 16705ecc953bSthorpej if (d == NULL) { 1671c7295a4cSchristos cfgerror("%s: unknown device `%s'", name, base); 16725ecc953bSthorpej return (NULL); 16735ecc953bSthorpej } 16745ecc953bSthorpej if (d->d_ispseudo) { 1675c7295a4cSchristos cfgerror("%s: %s is a pseudo-device", name, base); 16765ecc953bSthorpej return (NULL); 16775ecc953bSthorpej } 16785ecc953bSthorpej firsti = ht_lookup(devitab, name); 16795ecc953bSthorpej i = newdevi(name, unit, d); 16805ecc953bSthorpej if (firsti == NULL) { 16815ecc953bSthorpej if (ht_insert(devitab, name, i)) 16825ecc953bSthorpej panic("getdevi(%s)", name); 16835ecc953bSthorpej *d->d_ipp = i; 16845ecc953bSthorpej d->d_ipp = &i->i_bsame; 16855ecc953bSthorpej } else { 16865ecc953bSthorpej while (firsti->i_alias) 16875ecc953bSthorpej firsti = firsti->i_alias; 16885ecc953bSthorpej firsti->i_alias = i; 16895ecc953bSthorpej } 16905ecc953bSthorpej TAILQ_INSERT_TAIL(&alldevi, i, i_next); 16915ecc953bSthorpej ndevi++; 16925ecc953bSthorpej return (i); 16935ecc953bSthorpej } 16945ecc953bSthorpej 16955ecc953bSthorpej static const char * 16965ecc953bSthorpej concat(const char *name, int c) 16975ecc953bSthorpej { 1698c7295a4cSchristos size_t len; 16995ecc953bSthorpej char buf[NAMESIZE]; 17005ecc953bSthorpej 17015ecc953bSthorpej len = strlen(name); 17025ecc953bSthorpej if (len + 2 > sizeof(buf)) { 1703c7295a4cSchristos cfgerror("device name `%s%c' too long", name, c); 17045ecc953bSthorpej len = sizeof(buf) - 2; 17055ecc953bSthorpej } 17065ecc953bSthorpej memmove(buf, name, len); 17075ecc953bSthorpej buf[len] = c; 17085ecc953bSthorpej buf[len + 1] = 0; 17095ecc953bSthorpej return (intern(buf)); 17105ecc953bSthorpej } 17115ecc953bSthorpej 17125ecc953bSthorpej const char * 17135ecc953bSthorpej starref(const char *name) 17145ecc953bSthorpej { 17155ecc953bSthorpej 17165ecc953bSthorpej return (concat(name, '*')); 17175ecc953bSthorpej } 17185ecc953bSthorpej 17195ecc953bSthorpej const char * 17205ecc953bSthorpej wildref(const char *name) 17215ecc953bSthorpej { 17225ecc953bSthorpej 17235ecc953bSthorpej return (concat(name, '?')); 17245ecc953bSthorpej } 17255ecc953bSthorpej 17265ecc953bSthorpej /* 17275ecc953bSthorpej * Split a name like "foo0" into base name (foo) and unit number (0). 17285ecc953bSthorpej * Return 0 on success. To make this useful for names like "foo0a", 17295ecc953bSthorpej * the length of the "foo0" part is one of the arguments. 17305ecc953bSthorpej */ 17315ecc953bSthorpej static int 17325ecc953bSthorpej split(const char *name, size_t nlen, char *base, size_t bsize, int *aunit) 17335ecc953bSthorpej { 17345ecc953bSthorpej const char *cp; 1735c7295a4cSchristos int c; 1736c7295a4cSchristos size_t l; 17375ecc953bSthorpej 17385ecc953bSthorpej l = nlen; 17395ecc953bSthorpej if (l < 2 || l >= bsize || isdigit((unsigned char)*name)) 17405ecc953bSthorpej return (1); 17415ecc953bSthorpej c = (u_char)name[--l]; 17425ecc953bSthorpej if (!isdigit(c)) { 17435ecc953bSthorpej if (c == '*') 17445ecc953bSthorpej *aunit = STAR; 17455ecc953bSthorpej else if (c == '?') 17465ecc953bSthorpej *aunit = WILD; 17475ecc953bSthorpej else 17485ecc953bSthorpej return (1); 17495ecc953bSthorpej } else { 17505ecc953bSthorpej cp = &name[l]; 17515ecc953bSthorpej while (isdigit((unsigned char)cp[-1])) 17525ecc953bSthorpej l--, cp--; 17535ecc953bSthorpej *aunit = atoi(cp); 17545ecc953bSthorpej } 17555ecc953bSthorpej memmove(base, name, l); 17565ecc953bSthorpej base[l] = 0; 17575ecc953bSthorpej return (0); 17585ecc953bSthorpej } 17595ecc953bSthorpej 17605ecc953bSthorpej void 17615ecc953bSthorpej selectattr(struct attr *a) 17625ecc953bSthorpej { 17635ecc953bSthorpej 1764c7295a4cSchristos (void)ht_insert(selecttab, a->a_name, __UNCONST(a->a_name)); 17655ecc953bSthorpej } 17665ecc953bSthorpej 17675ecc953bSthorpej /* 17685ecc953bSthorpej * We have an instance of the base foo, so select it and all its 17695ecc953bSthorpej * attributes for "optional foo". 17705ecc953bSthorpej */ 17715ecc953bSthorpej static void 17725ecc953bSthorpej selectbase(struct devbase *d, struct deva *da) 17735ecc953bSthorpej { 17745ecc953bSthorpej struct attr *a; 17754caf067bSdholland struct attrlist *al; 17765ecc953bSthorpej 1777c7295a4cSchristos (void)ht_insert(selecttab, d->d_name, __UNCONST(d->d_name)); 17784caf067bSdholland for (al = d->d_attrs; al != NULL; al = al->al_next) { 17794caf067bSdholland a = al->al_this; 17805ecc953bSthorpej expandattr(a, selectattr); 17815ecc953bSthorpej } 17825ecc953bSthorpej if (da != NULL) { 1783c7295a4cSchristos (void)ht_insert(selecttab, da->d_name, __UNCONST(da->d_name)); 17844caf067bSdholland for (al = da->d_attrs; al != NULL; al = al->al_next) { 17854caf067bSdholland a = al->al_this; 17865ecc953bSthorpej expandattr(a, selectattr); 17875ecc953bSthorpej } 17885ecc953bSthorpej } 17895ecc953bSthorpej } 17905ecc953bSthorpej 17915ecc953bSthorpej /* 17925ecc953bSthorpej * Is the given pointer on the given list of pointers? 17935ecc953bSthorpej */ 179459c94545Scube int 17955ecc953bSthorpej onlist(struct nvlist *nv, void *ptr) 17965ecc953bSthorpej { 17975ecc953bSthorpej for (; nv != NULL; nv = nv->nv_next) 17985ecc953bSthorpej if (nv->nv_ptr == ptr) 17995ecc953bSthorpej return (1); 18005ecc953bSthorpej return (0); 18015ecc953bSthorpej } 18025ecc953bSthorpej 18035ecc953bSthorpej static char * 18045ecc953bSthorpej extend(char *p, const char *name) 18055ecc953bSthorpej { 1806c7295a4cSchristos size_t l; 18075ecc953bSthorpej 18085ecc953bSthorpej l = strlen(name); 18095ecc953bSthorpej memmove(p, name, l); 18105ecc953bSthorpej p += l; 18115ecc953bSthorpej *p++ = ','; 18125ecc953bSthorpej *p++ = ' '; 18135ecc953bSthorpej return (p); 18145ecc953bSthorpej } 18155ecc953bSthorpej 18165ecc953bSthorpej /* 18175ecc953bSthorpej * Check that we got all required locators, and default any that are 18185ecc953bSthorpej * given as "?" and have defaults. Return 0 on success. 18195ecc953bSthorpej */ 18205ecc953bSthorpej static const char ** 18215ecc953bSthorpej fixloc(const char *name, struct attr *attr, struct nvlist *got) 18225ecc953bSthorpej { 18235ecc953bSthorpej struct nvlist *m, *n; 18245ecc953bSthorpej int ord; 18255ecc953bSthorpej const char **lp; 18265ecc953bSthorpej int nmissing, nextra, nnodefault; 18275ecc953bSthorpej char *mp, *ep, *ndp; 18285ecc953bSthorpej char missing[1000], extra[1000], nodefault[1000]; 18295ecc953bSthorpej static const char *nullvec[1]; 18305ecc953bSthorpej 18315ecc953bSthorpej /* 18325ecc953bSthorpej * Look for all required locators, and number the given ones 18335ecc953bSthorpej * according to the required order. While we are numbering, 18345ecc953bSthorpej * set default values for defaulted locators. 18355ecc953bSthorpej */ 18365ecc953bSthorpej if (attr->a_loclen == 0) /* e.g., "at root" */ 18375ecc953bSthorpej lp = nullvec; 18385ecc953bSthorpej else 18395ecc953bSthorpej lp = emalloc((attr->a_loclen + 1) * sizeof(const char *)); 18405ecc953bSthorpej for (n = got; n != NULL; n = n->nv_next) 18410001b928Schristos n->nv_num = -1; 18425ecc953bSthorpej nmissing = 0; 18435ecc953bSthorpej mp = missing; 18445ecc953bSthorpej /* yes, this is O(mn), but m and n should be small */ 18455ecc953bSthorpej for (ord = 0, m = attr->a_locs; m != NULL; m = m->nv_next, ord++) { 18465ecc953bSthorpej for (n = got; n != NULL; n = n->nv_next) { 18475ecc953bSthorpej if (n->nv_name == m->nv_name) { 18480001b928Schristos n->nv_num = ord; 18495ecc953bSthorpej break; 18505ecc953bSthorpej } 18515ecc953bSthorpej } 18520001b928Schristos if (n == NULL && m->nv_num == 0) { 18535ecc953bSthorpej nmissing++; 18545ecc953bSthorpej mp = extend(mp, m->nv_name); 18555ecc953bSthorpej } 18565ecc953bSthorpej lp[ord] = m->nv_str; 18575ecc953bSthorpej } 18585ecc953bSthorpej if (ord != attr->a_loclen) 18595ecc953bSthorpej panic("fixloc"); 18605ecc953bSthorpej lp[ord] = NULL; 18615ecc953bSthorpej nextra = 0; 18625ecc953bSthorpej ep = extra; 18635ecc953bSthorpej nnodefault = 0; 18645ecc953bSthorpej ndp = nodefault; 18655ecc953bSthorpej for (n = got; n != NULL; n = n->nv_next) { 18660001b928Schristos if (n->nv_num >= 0) { 18675ecc953bSthorpej if (n->nv_str != NULL) 18680001b928Schristos lp[n->nv_num] = n->nv_str; 18690001b928Schristos else if (lp[n->nv_num] == NULL) { 18705ecc953bSthorpej nnodefault++; 18715ecc953bSthorpej ndp = extend(ndp, n->nv_name); 18725ecc953bSthorpej } 18735ecc953bSthorpej } else { 18745ecc953bSthorpej nextra++; 18755ecc953bSthorpej ep = extend(ep, n->nv_name); 18765ecc953bSthorpej } 18775ecc953bSthorpej } 18785ecc953bSthorpej if (nextra) { 18795ecc953bSthorpej ep[-2] = 0; /* kill ", " */ 1880c7295a4cSchristos cfgerror("%s: extraneous locator%s: %s", 18815ecc953bSthorpej name, nextra > 1 ? "s" : "", extra); 18825ecc953bSthorpej } 18835ecc953bSthorpej if (nmissing) { 18845ecc953bSthorpej mp[-2] = 0; 1885c7295a4cSchristos cfgerror("%s: must specify %s", name, missing); 18865ecc953bSthorpej } 18875ecc953bSthorpej if (nnodefault) { 18885ecc953bSthorpej ndp[-2] = 0; 1889c7295a4cSchristos cfgerror("%s: cannot wildcard %s", name, nodefault); 18905ecc953bSthorpej } 18915ecc953bSthorpej if (nmissing || nnodefault) { 18925ecc953bSthorpej free(lp); 18935ecc953bSthorpej lp = NULL; 18945ecc953bSthorpej } 18955ecc953bSthorpej return (lp); 18965ecc953bSthorpej } 1897437f8925Scube 1898437f8925Scube void 1899437f8925Scube setversion(int newver) 1900437f8925Scube { 1901437f8925Scube if (newver > CONFIG_VERSION) 1902c7295a4cSchristos cfgerror("your sources require a newer version of config(1) " 1903437f8925Scube "-- please rebuild it."); 1904437f8925Scube else if (newver < CONFIG_MINVERSION) 1905c7295a4cSchristos cfgerror("your sources are out of date -- please update."); 1906437f8925Scube else 1907437f8925Scube version = newver; 1908437f8925Scube } 1909