1*bb4307a6Suebayasi /* $NetBSD: sem.c,v 1.49 2014/10/10 06:13:30 uebayasi 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 */ 687c7d91e6Suebayasi struct attr allattr; 695ecc953bSthorpej 705ecc953bSthorpej static struct attr errattr; 715ecc953bSthorpej static struct devbase errdev; 725ecc953bSthorpej static struct deva errdeva; 735ecc953bSthorpej 744caf067bSdholland static int has_errobj(struct attrlist *, struct attr *); 755ecc953bSthorpej static struct nvlist *addtoattr(struct nvlist *, struct devbase *); 765ecc953bSthorpej static int resolve(struct nvlist **, const char *, const char *, 775ecc953bSthorpej struct nvlist *, int); 785ecc953bSthorpej static struct pspec *getpspec(struct attr *, struct devbase *, int); 795ecc953bSthorpej static struct devi *newdevi(const char *, int, struct devbase *d); 805ecc953bSthorpej static struct devi *getdevi(const char *); 81c130d400Scube static void remove_devi(struct devi *); 825ecc953bSthorpej static const char *concat(const char *, int); 835ecc953bSthorpej static char *extend(char *, const char *); 845ecc953bSthorpej static int split(const char *, size_t, char *, size_t, int *); 855ecc953bSthorpej static void selectbase(struct devbase *, struct deva *); 8616a8771bSdholland static const char **fixloc(const char *, struct attr *, struct loclist *); 87d767912bSdrochner static const char *makedevstr(devmajor_t, devminor_t); 88d767912bSdrochner static const char *major2name(devmajor_t); 89d767912bSdrochner static devmajor_t dev2major(struct devbase *); 905ecc953bSthorpej 915ecc953bSthorpej extern const char *yyfile; 927aa6070dScube extern int vflag; 935ecc953bSthorpej 945ecc953bSthorpej void 955ecc953bSthorpej initsem(void) 965ecc953bSthorpej { 975ecc953bSthorpej 985ecc953bSthorpej attrtab = ht_new(); 997c7d91e6Suebayasi 1007c7d91e6Suebayasi allattr.a_name = "netbsd"; 1017c7d91e6Suebayasi TAILQ_INIT(&allattr.a_files); 1027c7d91e6Suebayasi (void)ht_insert(attrtab, allattr.a_name, &allattr); 103cd06fea3Suebayasi selectattr(&allattr); 1047c7d91e6Suebayasi 1055ecc953bSthorpej errattr.a_name = "<internal>"; 1065ecc953bSthorpej 1075ecc953bSthorpej TAILQ_INIT(&allbases); 1085ecc953bSthorpej 1095ecc953bSthorpej TAILQ_INIT(&alldevas); 1105ecc953bSthorpej 1115ecc953bSthorpej TAILQ_INIT(&allpspecs); 1125ecc953bSthorpej 1135ecc953bSthorpej cfhashtab = ht_new(); 1145ecc953bSthorpej TAILQ_INIT(&allcf); 1155ecc953bSthorpej 1165ecc953bSthorpej TAILQ_INIT(&alldevi); 1175ecc953bSthorpej errdev.d_name = "<internal>"; 1185ecc953bSthorpej 1195ecc953bSthorpej TAILQ_INIT(&allpseudo); 1205ecc953bSthorpej 1215ecc953bSthorpej TAILQ_INIT(&alldevms); 1225ecc953bSthorpej 1235ecc953bSthorpej s_ifnet = intern("ifnet"); 1245ecc953bSthorpej s_qmark = intern("?"); 1255ecc953bSthorpej s_none = intern("none"); 1265ecc953bSthorpej } 1275ecc953bSthorpej 1285ecc953bSthorpej /* Name of include file just ended (set in scan.l) */ 1295ecc953bSthorpej extern const char *lastfile; 1305ecc953bSthorpej 1315ecc953bSthorpej void 1325ecc953bSthorpej enddefs(void) 1335ecc953bSthorpej { 1345ecc953bSthorpej struct devbase *dev; 1355ecc953bSthorpej 1365ecc953bSthorpej TAILQ_FOREACH(dev, &allbases, d_next) { 1375ecc953bSthorpej if (!dev->d_isdef) { 1385ecc953bSthorpej (void)fprintf(stderr, 1395ecc953bSthorpej "%s: device `%s' used but not defined\n", 1405ecc953bSthorpej lastfile, dev->d_name); 1415ecc953bSthorpej errors++; 1425ecc953bSthorpej continue; 1435ecc953bSthorpej } 1445ecc953bSthorpej } 1455ecc953bSthorpej if (errors) { 1465ecc953bSthorpej (void)fprintf(stderr, "*** Stop.\n"); 1475ecc953bSthorpej exit(1); 1485ecc953bSthorpej } 1495ecc953bSthorpej } 1505ecc953bSthorpej 1515ecc953bSthorpej void 1525ecc953bSthorpej setdefmaxusers(int min, int def, int max) 1535ecc953bSthorpej { 1545ecc953bSthorpej 1555ecc953bSthorpej if (min < 1 || min > def || def > max) 156c7295a4cSchristos cfgerror("maxusers must have 1 <= min (%d) <= default (%d) " 157c7295a4cSchristos "<= max (%d)", min, def, max); 1585ecc953bSthorpej else { 1595ecc953bSthorpej minmaxusers = min; 1605ecc953bSthorpej defmaxusers = def; 1615ecc953bSthorpej maxmaxusers = max; 1625ecc953bSthorpej } 1635ecc953bSthorpej } 1645ecc953bSthorpej 1655ecc953bSthorpej void 1665ecc953bSthorpej setmaxusers(int n) 1675ecc953bSthorpej { 1685ecc953bSthorpej 169895e5687Scube if (maxusers == n) { 170c7295a4cSchristos cfgerror("duplicate maxusers parameter"); 1715ecc953bSthorpej return; 1725ecc953bSthorpej } 173895e5687Scube if (vflag && maxusers != 0) 174c7295a4cSchristos cfgwarn("warning: maxusers already defined"); 1755ecc953bSthorpej maxusers = n; 1765ecc953bSthorpej if (n < minmaxusers) { 177c7295a4cSchristos cfgerror("warning: minimum of %d maxusers assumed", 178c7295a4cSchristos minmaxusers); 1795ecc953bSthorpej errors--; /* take it away */ 1805ecc953bSthorpej maxusers = minmaxusers; 1815ecc953bSthorpej } else if (n > maxmaxusers) { 182c7295a4cSchristos cfgerror("warning: maxusers (%d) > %d", n, maxmaxusers); 1835ecc953bSthorpej errors--; 1845ecc953bSthorpej } 1855ecc953bSthorpej } 1865ecc953bSthorpej 1875ecc953bSthorpej void 1885ecc953bSthorpej setident(const char *i) 1895ecc953bSthorpej { 1905ecc953bSthorpej 1914a7be1f9Smrg if (i) 1925ecc953bSthorpej ident = intern(i); 1934a7be1f9Smrg else 1944a7be1f9Smrg ident = NULL; 1955ecc953bSthorpej } 1965ecc953bSthorpej 1975ecc953bSthorpej /* 1985ecc953bSthorpej * Define an attribute, optionally with an interface (a locator list) 1995ecc953bSthorpej * and a set of attribute-dependencies. 2005ecc953bSthorpej * 2015ecc953bSthorpej * Attribute dependencies MAY NOT be interface attributes. 2025ecc953bSthorpej * 2035ecc953bSthorpej * Since an empty locator list is logically different from "no interface", 2045ecc953bSthorpej * all locator lists include a dummy head node, which we discard here. 2055ecc953bSthorpej */ 2065ecc953bSthorpej int 207*bb4307a6Suebayasi defattr0(const char *name, struct loclist *locs, struct attrlist *deps, 208*bb4307a6Suebayasi int devclass) 209*bb4307a6Suebayasi { 210*bb4307a6Suebayasi 211*bb4307a6Suebayasi if (locs != NULL) 212*bb4307a6Suebayasi return defiattr(name, locs, deps, devclass); 213*bb4307a6Suebayasi else if (devclass) 214*bb4307a6Suebayasi return defdevclass(name, locs, deps, devclass); 215*bb4307a6Suebayasi else 216*bb4307a6Suebayasi return defattr(name, locs, deps, devclass); 217*bb4307a6Suebayasi } 218*bb4307a6Suebayasi 219*bb4307a6Suebayasi int 22016a8771bSdholland defattr(const char *name, struct loclist *locs, struct attrlist *deps, 2215ecc953bSthorpej int devclass) 2225ecc953bSthorpej { 2235ecc953bSthorpej struct attr *a, *dep; 2244caf067bSdholland struct attrlist *al; 2255ecc953bSthorpej 2265ecc953bSthorpej /* 2275ecc953bSthorpej * If this attribute depends on any others, make sure none of 2285ecc953bSthorpej * the dependencies are interface attributes. 2295ecc953bSthorpej */ 2304caf067bSdholland for (al = deps; al != NULL; al = al->al_next) { 2314caf067bSdholland dep = al->al_this; 2325ecc953bSthorpej if (dep->a_iattr) { 233c7295a4cSchristos cfgerror("`%s' dependency `%s' is an interface " 234c7295a4cSchristos "attribute", name, dep->a_name); 2355ecc953bSthorpej return (1); 2365ecc953bSthorpej } 2375ecc953bSthorpej } 2385ecc953bSthorpej 2395ecc953bSthorpej a = ecalloc(1, sizeof *a); 2405ecc953bSthorpej if (ht_insert(attrtab, name, a)) { 2415ecc953bSthorpej free(a); 242c7295a4cSchristos cfgerror("attribute `%s' already defined", name); 24316a8771bSdholland loclist_destroy(locs); 2445ecc953bSthorpej return (1); 2455ecc953bSthorpej } 2465ecc953bSthorpej 2475ecc953bSthorpej a->a_name = name; 248a949e288Suebayasi a->a_deps = deps; 249a949e288Suebayasi a->a_expanding = 0; 250a949e288Suebayasi TAILQ_INIT(&a->a_files); 251*bb4307a6Suebayasi expandattr(a, NULL); 252*bb4307a6Suebayasi 253*bb4307a6Suebayasi return (0); 254*bb4307a6Suebayasi } 255a949e288Suebayasi 256a949e288Suebayasi /* "interface attribute" initialization */ 257*bb4307a6Suebayasi int 258*bb4307a6Suebayasi defiattr(const char *name, struct loclist *locs, struct attrlist *deps, 259*bb4307a6Suebayasi int devclass) 260*bb4307a6Suebayasi { 261*bb4307a6Suebayasi struct attr *a; 262*bb4307a6Suebayasi int len; 263*bb4307a6Suebayasi struct loclist *ll; 264*bb4307a6Suebayasi 265*bb4307a6Suebayasi if (devclass) 266*bb4307a6Suebayasi panic("defattr(%s): locators and devclass", name); 267*bb4307a6Suebayasi 268*bb4307a6Suebayasi if (defattr(name, locs, deps, devclass) != 0) 269*bb4307a6Suebayasi return (1); 270*bb4307a6Suebayasi 271*bb4307a6Suebayasi a = getattr(name); 2725ecc953bSthorpej a->a_iattr = 1; 27316a8771bSdholland /* unwrap */ 27416a8771bSdholland a->a_locs = locs->ll_next; 27516a8771bSdholland locs->ll_next = NULL; 27616a8771bSdholland loclist_destroy(locs); 277a949e288Suebayasi len = 0; 278a949e288Suebayasi for (ll = a->a_locs; ll != NULL; ll = ll->ll_next) 279a949e288Suebayasi len++; 280a949e288Suebayasi a->a_loclen = len; 281*bb4307a6Suebayasi if (deps) 282*bb4307a6Suebayasi CFGDBG(2, "attr `%s' iface with deps", a->a_name); 283*bb4307a6Suebayasi return (0); 2845ecc953bSthorpej } 285a949e288Suebayasi 286a949e288Suebayasi /* "device class" initialization */ 287*bb4307a6Suebayasi int 288*bb4307a6Suebayasi defdevclass(const char *name, struct loclist *locs, struct attrlist *deps, 289*bb4307a6Suebayasi int devclass) 290*bb4307a6Suebayasi { 291*bb4307a6Suebayasi struct attr *a; 292bbfbbde1Schristos char classenum[256], *cp; 2935ecc953bSthorpej int errored = 0; 2945ecc953bSthorpej 295*bb4307a6Suebayasi if (deps) 296*bb4307a6Suebayasi panic("defattr(%s): dependencies and devclass", name); 297*bb4307a6Suebayasi 298*bb4307a6Suebayasi if (defattr(name, locs, deps, devclass) != 0) 299*bb4307a6Suebayasi return (1); 300*bb4307a6Suebayasi 301*bb4307a6Suebayasi a = getattr(name); 302bbfbbde1Schristos (void)snprintf(classenum, sizeof(classenum), "DV_%s", name); 3035ecc953bSthorpej for (cp = classenum + 3; *cp; cp++) { 3045ecc953bSthorpej if (!errored && 3055ecc953bSthorpej (!isalnum((unsigned char)*cp) || 3065ecc953bSthorpej (isalpha((unsigned char)*cp) && !islower((unsigned char)*cp)))) { 307c7295a4cSchristos cfgerror("device class names must be " 308c7295a4cSchristos "lower-case alphanumeric characters"); 3095ecc953bSthorpej errored = 1; 3105ecc953bSthorpej } 3115ecc953bSthorpej *cp = toupper((unsigned char)*cp); 3125ecc953bSthorpej } 3135ecc953bSthorpej a->a_devclass = intern(classenum); 3145ecc953bSthorpej 3155ecc953bSthorpej return (0); 3165ecc953bSthorpej } 3175ecc953bSthorpej 3185ecc953bSthorpej /* 3195ecc953bSthorpej * Return true if the given `error object' is embedded in the given 3205ecc953bSthorpej * pointer list. 3215ecc953bSthorpej */ 3225ecc953bSthorpej static int 3234caf067bSdholland has_errobj(struct attrlist *al, struct attr *obj) 3245ecc953bSthorpej { 3255ecc953bSthorpej 3264caf067bSdholland for (; al != NULL; al = al->al_next) 3274caf067bSdholland if (al->al_this == obj) 3285ecc953bSthorpej return (1); 3295ecc953bSthorpej return (0); 3305ecc953bSthorpej } 3315ecc953bSthorpej 3325ecc953bSthorpej /* 3335ecc953bSthorpej * Return true if the given attribute is embedded in the given 3345ecc953bSthorpej * pointer list. 3355ecc953bSthorpej */ 3365ecc953bSthorpej int 3374caf067bSdholland has_attr(struct attrlist *al, const char *attr) 3385ecc953bSthorpej { 3395ecc953bSthorpej struct attr *a; 3405ecc953bSthorpej 3415ecc953bSthorpej if ((a = getattr(attr)) == NULL) 3425ecc953bSthorpej return (0); 3435ecc953bSthorpej 3444caf067bSdholland for (; al != NULL; al = al->al_next) 3454caf067bSdholland if (al->al_this == a) 3465ecc953bSthorpej return (1); 3475ecc953bSthorpej return (0); 3485ecc953bSthorpej } 3495ecc953bSthorpej 3505ecc953bSthorpej /* 3515ecc953bSthorpej * Add a device base to a list in an attribute (actually, to any list). 3525ecc953bSthorpej * Note that this does not check for duplicates, and does reverse the 3535ecc953bSthorpej * list order, but no one cares anyway. 3545ecc953bSthorpej */ 3555ecc953bSthorpej static struct nvlist * 3565ecc953bSthorpej addtoattr(struct nvlist *l, struct devbase *dev) 3575ecc953bSthorpej { 3585ecc953bSthorpej struct nvlist *n; 3595ecc953bSthorpej 3605ecc953bSthorpej n = newnv(NULL, NULL, dev, 0, l); 3615ecc953bSthorpej return (n); 3625ecc953bSthorpej } 3635ecc953bSthorpej 3645ecc953bSthorpej /* 3655ecc953bSthorpej * Define a device. This may (or may not) also define an interface 3665ecc953bSthorpej * attribute and/or refer to existing attributes. 3675ecc953bSthorpej */ 3685ecc953bSthorpej void 36916a8771bSdholland defdev(struct devbase *dev, struct loclist *loclist, struct attrlist *attrs, 3705ecc953bSthorpej int ispseudo) 3715ecc953bSthorpej { 37216a8771bSdholland struct loclist *ll; 3734caf067bSdholland struct attrlist *al; 3745ecc953bSthorpej struct attr *a; 3755ecc953bSthorpej 3765ecc953bSthorpej if (dev == &errdev) 3775ecc953bSthorpej goto bad; 3785ecc953bSthorpej if (dev->d_isdef) { 379c7295a4cSchristos cfgerror("redefinition of `%s'", dev->d_name); 3805ecc953bSthorpej goto bad; 3815ecc953bSthorpej } 3825ecc953bSthorpej 3835ecc953bSthorpej dev->d_isdef = 1; 3845ecc953bSthorpej if (has_errobj(attrs, &errattr)) 3855ecc953bSthorpej goto bad; 3865ecc953bSthorpej 3875ecc953bSthorpej /* 3885ecc953bSthorpej * Handle implicit attribute definition from locator list. Do 3895ecc953bSthorpej * this before scanning the `at' list so that we can have, e.g.: 3905ecc953bSthorpej * device foo at other, foo { slot = -1 } 3915ecc953bSthorpej * (where you can plug in a foo-bus extender to a foo-bus). 3925ecc953bSthorpej */ 3935ecc953bSthorpej if (loclist != NULL) { 39416a8771bSdholland ll = loclist; 3955ecc953bSthorpej loclist = NULL; /* defattr disposes of them for us */ 396*bb4307a6Suebayasi if (defiattr(dev->d_name, ll, NULL, 0)) 3975ecc953bSthorpej goto bad; 3984caf067bSdholland attrs = attrlist_cons(attrs, getattr(dev->d_name)); 3994caf067bSdholland /* This used to be stored but was never used */ 4004caf067bSdholland /* attrs->al_name = dev->d_name; */ 401c454f920Scube } 402c454f920Scube 403c130d400Scube /* 404c130d400Scube * Pseudo-devices can have children. Consider them as 405c130d400Scube * attaching at root. 406c130d400Scube */ 407c454f920Scube if (ispseudo) { 4084caf067bSdholland for (al = attrs; al != NULL; al = al->al_next) 4094caf067bSdholland if (al->al_this->a_iattr) 410c454f920Scube break; 4114caf067bSdholland if (al != NULL) { 412b66156c7Sdrochner if (ispseudo < 2) { 413b66156c7Sdrochner if (version >= 20080610) 414b66156c7Sdrochner cfgerror("interface attribute on " 415b66156c7Sdrochner "non-device pseudo `%s'", dev->d_name); 416b66156c7Sdrochner else { 417b66156c7Sdrochner ispseudo = 2; 418b66156c7Sdrochner } 419b66156c7Sdrochner } 420c130d400Scube ht_insert(devroottab, dev->d_name, dev); 4215ecc953bSthorpej } 422b66156c7Sdrochner } 4235ecc953bSthorpej 4245ecc953bSthorpej /* Committed! Set up fields. */ 4255ecc953bSthorpej dev->d_ispseudo = ispseudo; 4265ecc953bSthorpej dev->d_attrs = attrs; 4275ecc953bSthorpej dev->d_classattr = NULL; /* for now */ 4285ecc953bSthorpej 4295ecc953bSthorpej /* 4305ecc953bSthorpej * For each interface attribute this device refers to, add this 4315ecc953bSthorpej * device to its reference list. This makes, e.g., finding all 4325ecc953bSthorpej * "scsi"s easier. 4335ecc953bSthorpej * 4345ecc953bSthorpej * While looking through the attributes, set up the device 4355ecc953bSthorpej * class if any are devclass attributes (and error out if the 4365ecc953bSthorpej * device has two classes). 4375ecc953bSthorpej */ 4384caf067bSdholland for (al = attrs; al != NULL; al = al->al_next) { 4394caf067bSdholland a = al->al_this; 4405ecc953bSthorpej if (a->a_iattr) 4415ecc953bSthorpej a->a_refs = addtoattr(a->a_refs, dev); 4425ecc953bSthorpej if (a->a_devclass != NULL) { 4435ecc953bSthorpej if (dev->d_classattr != NULL) { 444c7295a4cSchristos cfgerror("device `%s' has multiple classes " 445c7295a4cSchristos "(`%s' and `%s')", 4465ecc953bSthorpej dev->d_name, dev->d_classattr->a_name, 4475ecc953bSthorpej a->a_name); 4485ecc953bSthorpej } 4495ecc953bSthorpej dev->d_classattr = a; 4505ecc953bSthorpej } 4515ecc953bSthorpej } 4525ecc953bSthorpej return; 4535ecc953bSthorpej bad: 45416a8771bSdholland loclist_destroy(loclist); 4554caf067bSdholland attrlist_destroyall(attrs); 4565ecc953bSthorpej } 4575ecc953bSthorpej 4585ecc953bSthorpej /* 4595ecc953bSthorpej * Look up a devbase. Also makes sure it is a reasonable name, 4605ecc953bSthorpej * i.e., does not end in a digit or contain special characters. 4615ecc953bSthorpej */ 4625ecc953bSthorpej struct devbase * 4635ecc953bSthorpej getdevbase(const char *name) 4645ecc953bSthorpej { 465c7295a4cSchristos const u_char *p; 4665ecc953bSthorpej struct devbase *dev; 4675ecc953bSthorpej 468c7295a4cSchristos p = (const u_char *)name; 4695ecc953bSthorpej if (!isalpha(*p)) 4705ecc953bSthorpej goto badname; 4715ecc953bSthorpej while (*++p) { 4725ecc953bSthorpej if (!isalnum(*p) && *p != '_') 4735ecc953bSthorpej goto badname; 4745ecc953bSthorpej } 4755ecc953bSthorpej if (isdigit(*--p)) { 4765ecc953bSthorpej badname: 477c7295a4cSchristos cfgerror("bad device base name `%s'", name); 4785ecc953bSthorpej return (&errdev); 4795ecc953bSthorpej } 4805ecc953bSthorpej dev = ht_lookup(devbasetab, name); 4815ecc953bSthorpej if (dev == NULL) { 4825ecc953bSthorpej dev = ecalloc(1, sizeof *dev); 4835ecc953bSthorpej dev->d_name = name; 4845ecc953bSthorpej dev->d_isdef = 0; 485d767912bSdrochner dev->d_major = NODEVMAJOR; 4865ecc953bSthorpej dev->d_attrs = NULL; 4875ecc953bSthorpej dev->d_ihead = NULL; 4885ecc953bSthorpej dev->d_ipp = &dev->d_ihead; 4895ecc953bSthorpej dev->d_ahead = NULL; 4905ecc953bSthorpej dev->d_app = &dev->d_ahead; 4915ecc953bSthorpej dev->d_umax = 0; 4925ecc953bSthorpej TAILQ_INSERT_TAIL(&allbases, dev, d_next); 4935ecc953bSthorpej if (ht_insert(devbasetab, name, dev)) 4945ecc953bSthorpej panic("getdevbase(%s)", name); 4955ecc953bSthorpej } 4965ecc953bSthorpej return (dev); 4975ecc953bSthorpej } 4985ecc953bSthorpej 4995ecc953bSthorpej /* 5005ecc953bSthorpej * Define some of a device's allowable parent attachments. 5015ecc953bSthorpej * There may be a list of (plain) attributes. 5025ecc953bSthorpej */ 5035ecc953bSthorpej void 5045ecc953bSthorpej defdevattach(struct deva *deva, struct devbase *dev, struct nvlist *atlist, 5054caf067bSdholland struct attrlist *attrs) 5065ecc953bSthorpej { 5075ecc953bSthorpej struct nvlist *nv; 5084caf067bSdholland struct attrlist *al; 5095ecc953bSthorpej struct attr *a; 5105ecc953bSthorpej struct deva *da; 5115ecc953bSthorpej 5125ecc953bSthorpej if (dev == &errdev) 5135ecc953bSthorpej goto bad; 5145ecc953bSthorpej if (deva == NULL) 5155ecc953bSthorpej deva = getdevattach(dev->d_name); 5165ecc953bSthorpej if (deva == &errdeva) 5175ecc953bSthorpej goto bad; 5185ecc953bSthorpej if (!dev->d_isdef) { 519c7295a4cSchristos cfgerror("attaching undefined device `%s'", dev->d_name); 5205ecc953bSthorpej goto bad; 5215ecc953bSthorpej } 5225ecc953bSthorpej if (deva->d_isdef) { 523c7295a4cSchristos cfgerror("redefinition of `%s'", deva->d_name); 5245ecc953bSthorpej goto bad; 5255ecc953bSthorpej } 5265ecc953bSthorpej if (dev->d_ispseudo) { 527c7295a4cSchristos cfgerror("pseudo-devices can't attach"); 5285ecc953bSthorpej goto bad; 5295ecc953bSthorpej } 5305ecc953bSthorpej 5315ecc953bSthorpej deva->d_isdef = 1; 5325ecc953bSthorpej if (has_errobj(attrs, &errattr)) 5335ecc953bSthorpej goto bad; 5344caf067bSdholland for (al = attrs; al != NULL; al = al->al_next) { 5354caf067bSdholland a = al->al_this; 5365ecc953bSthorpej if (a == &errattr) 5375ecc953bSthorpej continue; /* already complained */ 5385ecc953bSthorpej if (a->a_iattr || a->a_devclass != NULL) 539c7295a4cSchristos cfgerror("`%s' is not a plain attribute", a->a_name); 5405ecc953bSthorpej } 5415ecc953bSthorpej 5425ecc953bSthorpej /* Committed! Set up fields. */ 5435ecc953bSthorpej deva->d_attrs = attrs; 5445ecc953bSthorpej deva->d_atlist = atlist; 5455ecc953bSthorpej deva->d_devbase = dev; 5465ecc953bSthorpej 5475ecc953bSthorpej /* 5485ecc953bSthorpej * Turn the `at' list into interface attributes (map each 5495ecc953bSthorpej * nv_name to an attribute, or to NULL for root), and add 5505ecc953bSthorpej * this device to those attributes, so that children can 5515ecc953bSthorpej * be listed at this particular device if they are supported 5525ecc953bSthorpej * by that attribute. 5535ecc953bSthorpej */ 5545ecc953bSthorpej for (nv = atlist; nv != NULL; nv = nv->nv_next) { 5555ecc953bSthorpej if (nv->nv_name == NULL) 5565ecc953bSthorpej nv->nv_ptr = a = NULL; /* at root */ 5575ecc953bSthorpej else 5585ecc953bSthorpej nv->nv_ptr = a = getattr(nv->nv_name); 5595ecc953bSthorpej if (a == &errattr) 5605ecc953bSthorpej continue; /* already complained */ 5615ecc953bSthorpej 5625ecc953bSthorpej /* 5635ecc953bSthorpej * Make sure that an attachment spec doesn't 5645ecc953bSthorpej * already say how to attach to this attribute. 5655ecc953bSthorpej */ 5665ecc953bSthorpej for (da = dev->d_ahead; da != NULL; da = da->d_bsame) 5675ecc953bSthorpej if (onlist(da->d_atlist, a)) 568c7295a4cSchristos cfgerror("attach at `%s' already done by `%s'", 5695ecc953bSthorpej a ? a->a_name : "root", da->d_name); 5705ecc953bSthorpej 571c130d400Scube if (a == NULL) { 572c130d400Scube ht_insert(devroottab, dev->d_name, dev); 5735ecc953bSthorpej continue; /* at root; don't add */ 574c130d400Scube } 5755ecc953bSthorpej if (!a->a_iattr) 576c7295a4cSchristos cfgerror("%s cannot be at plain attribute `%s'", 5775ecc953bSthorpej dev->d_name, a->a_name); 5785ecc953bSthorpej else 5795ecc953bSthorpej a->a_devs = addtoattr(a->a_devs, dev); 5805ecc953bSthorpej } 5815ecc953bSthorpej 5825ecc953bSthorpej /* attach to parent */ 5835ecc953bSthorpej *dev->d_app = deva; 5845ecc953bSthorpej dev->d_app = &deva->d_bsame; 5855ecc953bSthorpej return; 5865ecc953bSthorpej bad: 5875ecc953bSthorpej nvfreel(atlist); 5884caf067bSdholland attrlist_destroyall(attrs); 5895ecc953bSthorpej } 5905ecc953bSthorpej 5915ecc953bSthorpej /* 5925ecc953bSthorpej * Look up a device attachment. Also makes sure it is a reasonable 5935ecc953bSthorpej * name, i.e., does not contain digits or special characters. 5945ecc953bSthorpej */ 5955ecc953bSthorpej struct deva * 5965ecc953bSthorpej getdevattach(const char *name) 5975ecc953bSthorpej { 598c7295a4cSchristos const u_char *p; 5995ecc953bSthorpej struct deva *deva; 6005ecc953bSthorpej 601c7295a4cSchristos p = (const u_char *)name; 6025ecc953bSthorpej if (!isalpha(*p)) 6035ecc953bSthorpej goto badname; 6045ecc953bSthorpej while (*++p) { 6055ecc953bSthorpej if (!isalnum(*p) && *p != '_') 6065ecc953bSthorpej goto badname; 6075ecc953bSthorpej } 6085ecc953bSthorpej if (isdigit(*--p)) { 6095ecc953bSthorpej badname: 610c7295a4cSchristos cfgerror("bad device attachment name `%s'", name); 6115ecc953bSthorpej return (&errdeva); 6125ecc953bSthorpej } 6135ecc953bSthorpej deva = ht_lookup(devatab, name); 6145ecc953bSthorpej if (deva == NULL) { 6155ecc953bSthorpej deva = ecalloc(1, sizeof *deva); 6165ecc953bSthorpej deva->d_name = name; 6175ecc953bSthorpej deva->d_bsame = NULL; 6185ecc953bSthorpej deva->d_isdef = 0; 6195ecc953bSthorpej deva->d_devbase = NULL; 6205ecc953bSthorpej deva->d_atlist = NULL; 6215ecc953bSthorpej deva->d_attrs = NULL; 6225ecc953bSthorpej deva->d_ihead = NULL; 6235ecc953bSthorpej deva->d_ipp = &deva->d_ihead; 6245ecc953bSthorpej TAILQ_INSERT_TAIL(&alldevas, deva, d_next); 6255ecc953bSthorpej if (ht_insert(devatab, name, deva)) 6265ecc953bSthorpej panic("getdeva(%s)", name); 6275ecc953bSthorpej } 6285ecc953bSthorpej return (deva); 6295ecc953bSthorpej } 6305ecc953bSthorpej 6315ecc953bSthorpej /* 6325ecc953bSthorpej * Look up an attribute. 6335ecc953bSthorpej */ 6345ecc953bSthorpej struct attr * 6355ecc953bSthorpej getattr(const char *name) 6365ecc953bSthorpej { 6375ecc953bSthorpej struct attr *a; 6385ecc953bSthorpej 6395ecc953bSthorpej if ((a = ht_lookup(attrtab, name)) == NULL) { 640c7295a4cSchristos cfgerror("undefined attribute `%s'", name); 6415ecc953bSthorpej a = &errattr; 6425ecc953bSthorpej } 6435ecc953bSthorpej return (a); 6445ecc953bSthorpej } 6455ecc953bSthorpej 6465ecc953bSthorpej /* 6475ecc953bSthorpej * Recursively expand an attribute and its dependencies, checking for 6485ecc953bSthorpej * cycles, and invoking a callback for each attribute found. 6495ecc953bSthorpej */ 6505ecc953bSthorpej void 6515ecc953bSthorpej expandattr(struct attr *a, void (*callback)(struct attr *)) 6525ecc953bSthorpej { 6534caf067bSdholland struct attrlist *al; 6545ecc953bSthorpej struct attr *dep; 6555ecc953bSthorpej 6565ecc953bSthorpej if (a->a_expanding) { 657c7295a4cSchristos cfgerror("circular dependency on attribute `%s'", a->a_name); 6585ecc953bSthorpej return; 6595ecc953bSthorpej } 6605ecc953bSthorpej 6615ecc953bSthorpej a->a_expanding = 1; 6625ecc953bSthorpej 6635ecc953bSthorpej /* First expand all of this attribute's dependencies. */ 6644caf067bSdholland for (al = a->a_deps; al != NULL; al = al->al_next) { 6654caf067bSdholland dep = al->al_this; 6665ecc953bSthorpej expandattr(dep, callback); 6675ecc953bSthorpej } 6685ecc953bSthorpej 6695ecc953bSthorpej /* ...and now invoke the callback for ourself. */ 6705ecc953bSthorpej if (callback != NULL) 6715ecc953bSthorpej (*callback)(a); 6725ecc953bSthorpej 6735ecc953bSthorpej a->a_expanding = 0; 6745ecc953bSthorpej } 6755ecc953bSthorpej 6765ecc953bSthorpej /* 6775ecc953bSthorpej * Set the major device number for a device, so that it can be used 6785ecc953bSthorpej * as a root/dumps "on" device in a configuration. 6795ecc953bSthorpej */ 6805ecc953bSthorpej void 681d767912bSdrochner setmajor(struct devbase *d, devmajor_t n) 6825ecc953bSthorpej { 6835ecc953bSthorpej 684d767912bSdrochner if (d != &errdev && d->d_major != NODEVMAJOR) 685d767912bSdrochner cfgerror("device `%s' is already major %d", 686d767912bSdrochner d->d_name, d->d_major); 6875ecc953bSthorpej else 6885ecc953bSthorpej d->d_major = n; 6895ecc953bSthorpej } 6905ecc953bSthorpej 6915ecc953bSthorpej const char * 692d767912bSdrochner major2name(devmajor_t maj) 6935ecc953bSthorpej { 6945ecc953bSthorpej struct devbase *dev; 6955ecc953bSthorpej struct devm *dm; 6965ecc953bSthorpej 6975ecc953bSthorpej if (!do_devsw) { 6985ecc953bSthorpej TAILQ_FOREACH(dev, &allbases, d_next) { 6995ecc953bSthorpej if (dev->d_major == maj) 7005ecc953bSthorpej return (dev->d_name); 7015ecc953bSthorpej } 7025ecc953bSthorpej } else { 7035ecc953bSthorpej TAILQ_FOREACH(dm, &alldevms, dm_next) { 7045ecc953bSthorpej if (dm->dm_bmajor == maj) 7055ecc953bSthorpej return (dm->dm_name); 7065ecc953bSthorpej } 7075ecc953bSthorpej } 7085ecc953bSthorpej return (NULL); 7095ecc953bSthorpej } 7105ecc953bSthorpej 711d767912bSdrochner devmajor_t 7125ecc953bSthorpej dev2major(struct devbase *dev) 7135ecc953bSthorpej { 7145ecc953bSthorpej struct devm *dm; 7155ecc953bSthorpej 7165ecc953bSthorpej if (!do_devsw) 7175ecc953bSthorpej return (dev->d_major); 7185ecc953bSthorpej 7195ecc953bSthorpej TAILQ_FOREACH(dm, &alldevms, dm_next) { 7205ecc953bSthorpej if (strcmp(dm->dm_name, dev->d_name) == 0) 7215ecc953bSthorpej return (dm->dm_bmajor); 7225ecc953bSthorpej } 723d767912bSdrochner return (NODEVMAJOR); 7245ecc953bSthorpej } 7255ecc953bSthorpej 7265ecc953bSthorpej /* 7275ecc953bSthorpej * Make a string description of the device at maj/min. 7285ecc953bSthorpej */ 7295ecc953bSthorpej static const char * 730d767912bSdrochner makedevstr(devmajor_t maj, devminor_t min) 7315ecc953bSthorpej { 732c7295a4cSchristos const char *devicename; 7335ecc953bSthorpej char buf[32]; 7345ecc953bSthorpej 735c7295a4cSchristos devicename = major2name(maj); 736c7295a4cSchristos if (devicename == NULL) 737d767912bSdrochner (void)snprintf(buf, sizeof(buf), "<%d/%d>", maj, min); 7385ecc953bSthorpej else 739d767912bSdrochner (void)snprintf(buf, sizeof(buf), "%s%d%c", devicename, 740d767912bSdrochner min / maxpartitions, (min % maxpartitions) + 'a'); 7415ecc953bSthorpej 7425ecc953bSthorpej return (intern(buf)); 7435ecc953bSthorpej } 7445ecc953bSthorpej 7455ecc953bSthorpej /* 7465ecc953bSthorpej * Map things like "ra0b" => makedev(major("ra"), 0*maxpartitions + 'b'-'a'). 7475ecc953bSthorpej * Handle the case where the device number is given but there is no 7485ecc953bSthorpej * corresponding name, and map NULL to the default. 7495ecc953bSthorpej */ 7505ecc953bSthorpej static int 7515ecc953bSthorpej resolve(struct nvlist **nvp, const char *name, const char *what, 7525ecc953bSthorpej struct nvlist *dflt, int part) 7535ecc953bSthorpej { 7545ecc953bSthorpej struct nvlist *nv; 7555ecc953bSthorpej struct devbase *dev; 7565ecc953bSthorpej const char *cp; 757d767912bSdrochner devmajor_t maj; 758d767912bSdrochner devminor_t min; 7590001b928Schristos int i, l; 7605ecc953bSthorpej int unit; 7615ecc953bSthorpej char buf[NAMESIZE]; 7625ecc953bSthorpej 7635cc303e1Slukem if ((part -= 'a') >= maxpartitions || part < 0) 7645ecc953bSthorpej panic("resolve"); 7655ecc953bSthorpej if ((nv = *nvp) == NULL) { 7665ecc953bSthorpej dev_t d = NODEV; 7675ecc953bSthorpej /* 7685ecc953bSthorpej * Apply default. Easiest to do this by number. 7695ecc953bSthorpej * Make sure to retain NODEVness, if this is dflt's disposition. 7705ecc953bSthorpej */ 7715cc303e1Slukem if ((dev_t)dflt->nv_num != NODEV) { 7720001b928Schristos maj = major(dflt->nv_num); 7730001b928Schristos min = ((minor(dflt->nv_num) / maxpartitions) * 7745ecc953bSthorpej maxpartitions) + part; 7755ecc953bSthorpej d = makedev(maj, min); 7765ecc953bSthorpej cp = makedevstr(maj, min); 7775ecc953bSthorpej } else 7785ecc953bSthorpej cp = NULL; 7795ecc953bSthorpej *nvp = nv = newnv(NULL, cp, NULL, d, NULL); 7805ecc953bSthorpej } 7815cc303e1Slukem if ((dev_t)nv->nv_num != NODEV) { 7825ecc953bSthorpej /* 7835ecc953bSthorpej * By the numbers. Find the appropriate major number 7845ecc953bSthorpej * to make a name. 7855ecc953bSthorpej */ 7860001b928Schristos maj = major(nv->nv_num); 7870001b928Schristos min = minor(nv->nv_num); 7885ecc953bSthorpej nv->nv_str = makedevstr(maj, min); 7895ecc953bSthorpej return (0); 7905ecc953bSthorpej } 7915ecc953bSthorpej 7925ecc953bSthorpej if (nv->nv_str == NULL || nv->nv_str == s_qmark) 7935ecc953bSthorpej /* 7945ecc953bSthorpej * Wildcarded or unspecified; leave it as NODEV. 7955ecc953bSthorpej */ 7965ecc953bSthorpej return (0); 7975ecc953bSthorpej 7985ecc953bSthorpej /* 7995ecc953bSthorpej * The normal case: things like "ra2b". Check for partition 8005ecc953bSthorpej * suffix, remove it if there, and split into name ("ra") and 8015ecc953bSthorpej * unit (2). 8025ecc953bSthorpej */ 8035ecc953bSthorpej l = i = strlen(nv->nv_str); 8045ecc953bSthorpej cp = &nv->nv_str[l]; 8055ecc953bSthorpej if (l > 1 && *--cp >= 'a' && *cp < 'a' + maxpartitions && 8065ecc953bSthorpej isdigit((unsigned char)cp[-1])) { 8075ecc953bSthorpej l--; 8085ecc953bSthorpej part = *cp - 'a'; 8095ecc953bSthorpej } 8105ecc953bSthorpej cp = nv->nv_str; 8115ecc953bSthorpej if (split(cp, l, buf, sizeof buf, &unit)) { 812c7295a4cSchristos cfgerror("%s: invalid %s device name `%s'", name, what, cp); 8135ecc953bSthorpej return (1); 8145ecc953bSthorpej } 8155ecc953bSthorpej dev = ht_lookup(devbasetab, intern(buf)); 8165ecc953bSthorpej if (dev == NULL) { 817c7295a4cSchristos cfgerror("%s: device `%s' does not exist", name, buf); 8185ecc953bSthorpej return (1); 8195ecc953bSthorpej } 8205ecc953bSthorpej 8215ecc953bSthorpej /* 8225ecc953bSthorpej * Check for the magic network interface attribute, and 8235ecc953bSthorpej * don't bother making a device number. 8245ecc953bSthorpej */ 8255ecc953bSthorpej if (has_attr(dev->d_attrs, s_ifnet)) { 8260001b928Schristos nv->nv_num = NODEV; 8275ecc953bSthorpej nv->nv_ifunit = unit; /* XXX XXX XXX */ 8285ecc953bSthorpej } else { 8295ecc953bSthorpej maj = dev2major(dev); 8305cc303e1Slukem if (maj == NODEVMAJOR) { 831c7295a4cSchristos cfgerror("%s: can't make %s device from `%s'", 8325ecc953bSthorpej name, what, nv->nv_str); 8335ecc953bSthorpej return (1); 8345ecc953bSthorpej } 8350001b928Schristos nv->nv_num = makedev(maj, unit * maxpartitions + part); 8365ecc953bSthorpej } 8375ecc953bSthorpej 8385ecc953bSthorpej nv->nv_name = dev->d_name; 8395ecc953bSthorpej return (0); 8405ecc953bSthorpej } 8415ecc953bSthorpej 8425ecc953bSthorpej /* 8435ecc953bSthorpej * Add a completed configuration to the list. 8445ecc953bSthorpej */ 8455ecc953bSthorpej void 8465ecc953bSthorpej addconf(struct config *cf0) 8475ecc953bSthorpej { 8485ecc953bSthorpej struct config *cf; 8495ecc953bSthorpej const char *name; 8505ecc953bSthorpej 8515ecc953bSthorpej name = cf0->cf_name; 8525ecc953bSthorpej cf = ecalloc(1, sizeof *cf); 8535ecc953bSthorpej if (ht_insert(cfhashtab, name, cf)) { 854c7295a4cSchristos cfgerror("configuration `%s' already defined", name); 8555ecc953bSthorpej free(cf); 8565ecc953bSthorpej goto bad; 8575ecc953bSthorpej } 8585ecc953bSthorpej *cf = *cf0; 8595ecc953bSthorpej 8605ecc953bSthorpej /* 8615ecc953bSthorpej * Resolve the root device. 8625ecc953bSthorpej */ 863cd429362Serh if (cf->cf_root == NULL) { 864c7295a4cSchristos cfgerror("%s: no root device specified", name); 8655ecc953bSthorpej goto bad; 8665ecc953bSthorpej } 867cd429362Serh if (cf->cf_root && cf->cf_root->nv_str != s_qmark) { 868cd429362Serh struct nvlist *nv; 869cd429362Serh nv = cf->cf_root; 8705ecc953bSthorpej if (resolve(&cf->cf_root, name, "root", nv, 'a')) 8715ecc953bSthorpej goto bad; 8725ecc953bSthorpej } 8735ecc953bSthorpej 8745ecc953bSthorpej /* 8755ecc953bSthorpej * Resolve the dump device. 8765ecc953bSthorpej */ 8775ecc953bSthorpej if (cf->cf_dump == NULL || cf->cf_dump->nv_str == s_qmark) { 8785ecc953bSthorpej /* 8795ecc953bSthorpej * Wildcarded dump device is equivalent to unspecified. 8805ecc953bSthorpej */ 8815ecc953bSthorpej cf->cf_dump = NULL; 8825ecc953bSthorpej } else if (cf->cf_dump->nv_str == s_none) { 8835ecc953bSthorpej /* 8845ecc953bSthorpej * Operator has requested that no dump device should be 8855ecc953bSthorpej * configured; do nothing. 8865ecc953bSthorpej */ 8875ecc953bSthorpej } else { 8885ecc953bSthorpej if (resolve(&cf->cf_dump, name, "dumps", cf->cf_dump, 'b')) 8895ecc953bSthorpej goto bad; 8905ecc953bSthorpej } 8915ecc953bSthorpej 8925ecc953bSthorpej /* Wildcarded fstype is `unspecified'. */ 8935ecc953bSthorpej if (cf->cf_fstype == s_qmark) 8945ecc953bSthorpej cf->cf_fstype = NULL; 8955ecc953bSthorpej 8965ecc953bSthorpej TAILQ_INSERT_TAIL(&allcf, cf, cf_next); 8975ecc953bSthorpej return; 8985ecc953bSthorpej bad: 8995ecc953bSthorpej nvfreel(cf0->cf_root); 9005ecc953bSthorpej nvfreel(cf0->cf_dump); 9015ecc953bSthorpej } 9025ecc953bSthorpej 9035ecc953bSthorpej void 9045ecc953bSthorpej setconf(struct nvlist **npp, const char *what, struct nvlist *v) 9055ecc953bSthorpej { 9065ecc953bSthorpej 9075ecc953bSthorpej if (*npp != NULL) { 908c7295a4cSchristos cfgerror("duplicate %s specification", what); 9095ecc953bSthorpej nvfreel(v); 9105ecc953bSthorpej } else 9115ecc953bSthorpej *npp = v; 9125ecc953bSthorpej } 9135ecc953bSthorpej 9145ecc953bSthorpej void 91542b52b8aScube delconf(const char *name) 91642b52b8aScube { 91742b52b8aScube struct config *cf; 91842b52b8aScube 91942b52b8aScube if (ht_lookup(cfhashtab, name) == NULL) { 920c7295a4cSchristos cfgerror("configuration `%s' undefined", name); 92142b52b8aScube return; 92242b52b8aScube } 92342b52b8aScube (void)ht_remove(cfhashtab, name); 92442b52b8aScube 92542b52b8aScube TAILQ_FOREACH(cf, &allcf, cf_next) 92642b52b8aScube if (!strcmp(cf->cf_name, name)) 92742b52b8aScube break; 92842b52b8aScube if (cf == NULL) 92942b52b8aScube panic("lost configuration `%s'", name); 93042b52b8aScube 93142b52b8aScube TAILQ_REMOVE(&allcf, cf, cf_next); 93242b52b8aScube } 93342b52b8aScube 93442b52b8aScube void 9355ecc953bSthorpej setfstype(const char **fstp, const char *v) 9365ecc953bSthorpej { 9375ecc953bSthorpej 9385ecc953bSthorpej if (*fstp != NULL) { 939c7295a4cSchristos cfgerror("multiple fstype specifications"); 9405ecc953bSthorpej return; 9415ecc953bSthorpej } 9425ecc953bSthorpej 9435ecc953bSthorpej if (v != s_qmark && OPT_FSOPT(v)) { 944c7295a4cSchristos cfgerror("\"%s\" is not a configured file system", v); 9455ecc953bSthorpej return; 9465ecc953bSthorpej } 9475ecc953bSthorpej 9485ecc953bSthorpej *fstp = v; 9495ecc953bSthorpej } 9505ecc953bSthorpej 9515ecc953bSthorpej static struct devi * 9525ecc953bSthorpej newdevi(const char *name, int unit, struct devbase *d) 9535ecc953bSthorpej { 9545ecc953bSthorpej struct devi *i; 9555ecc953bSthorpej 9565ecc953bSthorpej i = ecalloc(1, sizeof *i); 9575ecc953bSthorpej i->i_name = name; 9585ecc953bSthorpej i->i_unit = unit; 9595ecc953bSthorpej i->i_base = d; 9605ecc953bSthorpej i->i_bsame = NULL; 9615ecc953bSthorpej i->i_asame = NULL; 9625ecc953bSthorpej i->i_alias = NULL; 9635ecc953bSthorpej i->i_at = NULL; 9645ecc953bSthorpej i->i_pspec = NULL; 9655ecc953bSthorpej i->i_atdeva = NULL; 9665ecc953bSthorpej i->i_locs = NULL; 9675ecc953bSthorpej i->i_cfflags = 0; 9685ecc953bSthorpej i->i_lineno = currentline(); 9690dbd1c0eScube i->i_srcfile = yyfile; 9707aa6070dScube i->i_active = DEVI_ORPHAN; /* Proper analysis comes later */ 971a31ff6b4Scube i->i_level = devilevel; 97290ac64deSpooka i->i_pseudoroot = 0; 9735ecc953bSthorpej if (unit >= d->d_umax) 9745ecc953bSthorpej d->d_umax = unit + 1; 9755ecc953bSthorpej return (i); 9765ecc953bSthorpej } 9775ecc953bSthorpej 9785ecc953bSthorpej /* 9795ecc953bSthorpej * Add the named device as attaching to the named attribute (or perhaps 9805ecc953bSthorpej * another device instead) plus unit number. 9815ecc953bSthorpej */ 9825ecc953bSthorpej void 98316a8771bSdholland adddev(const char *name, const char *at, struct loclist *loclist, int flags) 9845ecc953bSthorpej { 9855ecc953bSthorpej struct devi *i; /* the new instance */ 9865ecc953bSthorpej struct pspec *p; /* and its pspec */ 9875ecc953bSthorpej struct attr *attr; /* attribute that allows attach */ 9885ecc953bSthorpej struct devbase *ib; /* i->i_base */ 9895ecc953bSthorpej struct devbase *ab; /* not NULL => at another dev */ 9904caf067bSdholland struct attrlist *al; 9915ecc953bSthorpej struct deva *iba; /* devbase attachment used */ 9925ecc953bSthorpej const char *cp; 9935ecc953bSthorpej int atunit; 9945ecc953bSthorpej char atbuf[NAMESIZE]; 9955ecc953bSthorpej int hit; 9965ecc953bSthorpej 9975ecc953bSthorpej ab = NULL; 9985ecc953bSthorpej iba = NULL; 9995ecc953bSthorpej if (at == NULL) { 10005ecc953bSthorpej /* "at root" */ 10015ecc953bSthorpej p = NULL; 10025ecc953bSthorpej if ((i = getdevi(name)) == NULL) 10035ecc953bSthorpej goto bad; 10045ecc953bSthorpej /* 10055ecc953bSthorpej * Must warn about i_unit > 0 later, after taking care of 10065ecc953bSthorpej * the STAR cases (we could do non-star's here but why 10075ecc953bSthorpej * bother?). Make sure this device can be at root. 10085ecc953bSthorpej */ 10095ecc953bSthorpej ib = i->i_base; 10105ecc953bSthorpej hit = 0; 10115ecc953bSthorpej for (iba = ib->d_ahead; iba != NULL; iba = iba->d_bsame) 10125ecc953bSthorpej if (onlist(iba->d_atlist, NULL)) { 10135ecc953bSthorpej hit = 1; 10145ecc953bSthorpej break; 10155ecc953bSthorpej } 10165ecc953bSthorpej if (!hit) { 1017c7295a4cSchristos cfgerror("`%s' cannot attach to the root", ib->d_name); 10185ec393e9Scube i->i_active = DEVI_BROKEN; 10195ecc953bSthorpej goto bad; 10205ecc953bSthorpej } 10215ecc953bSthorpej attr = &errattr; /* a convenient "empty" attr */ 10225ecc953bSthorpej } else { 10235ecc953bSthorpej if (split(at, strlen(at), atbuf, sizeof atbuf, &atunit)) { 1024c7295a4cSchristos cfgerror("invalid attachment name `%s'", at); 10255ecc953bSthorpej /* (void)getdevi(name); -- ??? */ 10265ecc953bSthorpej goto bad; 10275ecc953bSthorpej } 10285ecc953bSthorpej if ((i = getdevi(name)) == NULL) 10295ecc953bSthorpej goto bad; 10305ecc953bSthorpej ib = i->i_base; 10315ecc953bSthorpej 10325ecc953bSthorpej /* 10335ecc953bSthorpej * Devices can attach to two types of things: Attributes, 10345ecc953bSthorpej * and other devices (which have the appropriate attributes 10355ecc953bSthorpej * to allow attachment). 10365ecc953bSthorpej * 10375ecc953bSthorpej * (1) If we're attached to an attribute, then we don't need 10385ecc953bSthorpej * look at the parent base device to see what attributes 10395ecc953bSthorpej * it has, and make sure that we can attach to them. 10405ecc953bSthorpej * 10415ecc953bSthorpej * (2) If we're attached to a real device (i.e. named in 10425ecc953bSthorpej * the config file), we want to remember that so that 10435ecc953bSthorpej * at cross-check time, if the device we're attached to 10445ecc953bSthorpej * is missing but other devices which also provide the 10455ecc953bSthorpej * attribute are present, we don't get a false "OK." 10465ecc953bSthorpej * 10475ecc953bSthorpej * (3) If the thing we're attached to is an attribute 10485ecc953bSthorpej * but is actually named in the config file, we still 10495ecc953bSthorpej * have to remember its devbase. 10505ecc953bSthorpej */ 10515ecc953bSthorpej cp = intern(atbuf); 10525ecc953bSthorpej 10535ecc953bSthorpej /* Figure out parent's devbase, to satisfy case (3). */ 10545ecc953bSthorpej ab = ht_lookup(devbasetab, cp); 10555ecc953bSthorpej 10565ecc953bSthorpej /* Find out if it's an attribute. */ 10575ecc953bSthorpej attr = ht_lookup(attrtab, cp); 10585ecc953bSthorpej 10595ecc953bSthorpej /* Make sure we're _really_ attached to the attr. Case (1). */ 10605ecc953bSthorpej if (attr != NULL && onlist(attr->a_devs, ib)) 10615ecc953bSthorpej goto findattachment; 10625ecc953bSthorpej 10635ecc953bSthorpej /* 10645ecc953bSthorpej * Else a real device, and not just an attribute. Case (2). 10655ecc953bSthorpej * 10665ecc953bSthorpej * Have to work a bit harder to see whether we have 10675ecc953bSthorpej * something like "tg0 at esp0" (where esp is merely 10685ecc953bSthorpej * not an attribute) or "tg0 at nonesuch0" (where 10695ecc953bSthorpej * nonesuch is not even a device). 10705ecc953bSthorpej */ 10715ecc953bSthorpej if (ab == NULL) { 1072c7295a4cSchristos cfgerror("%s at %s: `%s' unknown", 10735ecc953bSthorpej name, at, atbuf); 10745ec393e9Scube i->i_active = DEVI_BROKEN; 10755ecc953bSthorpej goto bad; 10765ecc953bSthorpej } 10775ecc953bSthorpej 10785ecc953bSthorpej /* 10795ecc953bSthorpej * See if the named parent carries an attribute 10805ecc953bSthorpej * that allows it to supervise device ib. 10815ecc953bSthorpej */ 10824caf067bSdholland for (al = ab->d_attrs; al != NULL; al = al->al_next) { 10834caf067bSdholland attr = al->al_this; 10845ecc953bSthorpej if (onlist(attr->a_devs, ib)) 10855ecc953bSthorpej goto findattachment; 10865ecc953bSthorpej } 1087c7295a4cSchristos cfgerror("`%s' cannot attach to `%s'", ib->d_name, atbuf); 10885ec393e9Scube i->i_active = DEVI_BROKEN; 10895ecc953bSthorpej goto bad; 10905ecc953bSthorpej 10915ecc953bSthorpej findattachment: 10925ecc953bSthorpej /* 10935ecc953bSthorpej * Find the parent spec. If a matching one has not yet been 10945ecc953bSthorpej * created, create one. 10955ecc953bSthorpej */ 10965ecc953bSthorpej p = getpspec(attr, ab, atunit); 10975ecc953bSthorpej p->p_devs = newnv(NULL, NULL, i, 0, p->p_devs); 10985ecc953bSthorpej 10995ecc953bSthorpej /* find out which attachment it uses */ 11005ecc953bSthorpej hit = 0; 11015ecc953bSthorpej for (iba = ib->d_ahead; iba != NULL; iba = iba->d_bsame) 11025ecc953bSthorpej if (onlist(iba->d_atlist, attr)) { 11035ecc953bSthorpej hit = 1; 11045ecc953bSthorpej break; 11055ecc953bSthorpej } 11065ecc953bSthorpej if (!hit) 11075ecc953bSthorpej panic("adddev: can't figure out attachment"); 11085ecc953bSthorpej } 11095ec393e9Scube if ((i->i_locs = fixloc(name, attr, loclist)) == NULL) { 11105ec393e9Scube i->i_active = DEVI_BROKEN; 11115ecc953bSthorpej goto bad; 11125ec393e9Scube } 11135ecc953bSthorpej i->i_at = at; 11145ecc953bSthorpej i->i_pspec = p; 11155ecc953bSthorpej i->i_atdeva = iba; 11165ecc953bSthorpej i->i_cfflags = flags; 11175ecc953bSthorpej 11185ecc953bSthorpej *iba->d_ipp = i; 11195ecc953bSthorpej iba->d_ipp = &i->i_asame; 11205ecc953bSthorpej 11215ecc953bSthorpej /* all done, fall into ... */ 11225ecc953bSthorpej bad: 112316a8771bSdholland loclist_destroy(loclist); 11245ecc953bSthorpej return; 11255ecc953bSthorpej } 11265ecc953bSthorpej 11275ecc953bSthorpej void 11287b7c582aScube deldevi(const char *name, const char *at) 11295ecc953bSthorpej { 11307b7c582aScube struct devi *firsti, *i; 11315ecc953bSthorpej struct devbase *d; 11325ecc953bSthorpej int unit; 11335ecc953bSthorpej char base[NAMESIZE]; 11345ecc953bSthorpej 11355ecc953bSthorpej if (split(name, strlen(name), base, sizeof base, &unit)) { 1136c7295a4cSchristos cfgerror("invalid device name `%s'", name); 11375ecc953bSthorpej return; 11385ecc953bSthorpej } 11395ecc953bSthorpej d = ht_lookup(devbasetab, intern(base)); 11405ecc953bSthorpej if (d == NULL) { 1141c7295a4cSchristos cfgerror("%s: unknown device `%s'", name, base); 11425ecc953bSthorpej return; 11435ecc953bSthorpej } 11445ecc953bSthorpej if (d->d_ispseudo) { 1145c7295a4cSchristos cfgerror("%s: %s is a pseudo-device", name, base); 11465ecc953bSthorpej return; 11475ecc953bSthorpej } 11485ecc953bSthorpej if ((firsti = ht_lookup(devitab, name)) == NULL) { 1149c7295a4cSchristos cfgerror("`%s' not defined", name); 11505ecc953bSthorpej return; 11515ecc953bSthorpej } 11527b7c582aScube if (at == NULL && firsti->i_at == NULL) { 1153e499d8b5Scube /* 'at root' */ 11547b7c582aScube remove_devi(firsti); 11557b7c582aScube return; 11567b7c582aScube } else if (at != NULL) 11577b7c582aScube for (i = firsti; i != NULL; i = i->i_alias) 1158bfc250f2Scube if (i->i_active != DEVI_BROKEN && 1159bfc250f2Scube strcmp(at, i->i_at) == 0) { 11607b7c582aScube remove_devi(i); 11615ecc953bSthorpej return; 11625ecc953bSthorpej } 1163c7295a4cSchristos cfgerror("`%s' at `%s' not found", name, at ? at : "root"); 1164e499d8b5Scube } 11655ecc953bSthorpej 11667b7c582aScube static void 11677b7c582aScube remove_devi(struct devi *i) 11687b7c582aScube { 11697b7c582aScube struct devbase *d = i->i_base; 11707b7c582aScube struct devi *f, *j, **ppi; 11717b7c582aScube struct deva *iba; 11727b7c582aScube 11737b7c582aScube f = ht_lookup(devitab, i->i_name); 11746a6299ebScube if (f == NULL) 11756a6299ebScube panic("remove_devi(): instance %s disappeared from devitab", 11766a6299ebScube i->i_name); 11777b7c582aScube 1178bfc250f2Scube if (i->i_active == DEVI_BROKEN) { 1179bfc250f2Scube cfgerror("not removing broken instance `%s'", i->i_name); 1180bfc250f2Scube return; 1181bfc250f2Scube } 1182bfc250f2Scube 1183e499d8b5Scube /* 1184e499d8b5Scube * We have the device instance, i. 1185e499d8b5Scube * We have to: 1186e499d8b5Scube * - delete the alias 1187e499d8b5Scube * 1188e499d8b5Scube * If the devi was an alias of an already listed devi, all is 1189e499d8b5Scube * good we don't have to do more. 1190e499d8b5Scube * If it was the first alias, we have to replace i's entry in 1191e499d8b5Scube * d's list by its first alias. 1192e499d8b5Scube * If it was the only entry, we must remove i's entry from d's 1193e499d8b5Scube * list. 1194e499d8b5Scube */ 11957b7c582aScube if (i != f) { 11967b7c582aScube for (j = f; j->i_alias != i; j = j->i_alias); 11977b7c582aScube j->i_alias = i->i_alias; 11983b405775Scube } else { 11993b405775Scube if (i->i_alias == NULL) { 1200e499d8b5Scube /* No alias, must unlink the entry from devitab */ 12017b7c582aScube ht_remove(devitab, i->i_name); 12027b7c582aScube j = i->i_bsame; 12033b405775Scube } else { 1204e499d8b5Scube /* Or have the first alias replace i in d's list */ 1205e499d8b5Scube i->i_alias->i_bsame = i->i_bsame; 12067b7c582aScube j = i->i_alias; 12077b7c582aScube if (i == f) 12087b7c582aScube ht_replace(devitab, i->i_name, i->i_alias); 12093b405775Scube } 12103b405775Scube 1211e499d8b5Scube /* 1212e499d8b5Scube * - remove/replace the instance from the devbase's list 1213e499d8b5Scube * 1214e499d8b5Scube * A double-linked list would make this much easier. Oh, well, 1215e499d8b5Scube * what is done is done. 1216e499d8b5Scube */ 1217e499d8b5Scube for (ppi = &d->d_ihead; 1218e499d8b5Scube *ppi != NULL && *ppi != i && (*ppi)->i_bsame != i; 1219e499d8b5Scube ppi = &(*ppi)->i_bsame); 1220e499d8b5Scube if (*ppi == NULL) 1221c3414672Scube panic("deldev: dev (%s) doesn't list the devi" 1222c3414672Scube " (%s at %s)", d->d_name, i->i_name, i->i_at); 12237b7c582aScube f = *ppi; 12247b7c582aScube if (f == i) 1225c3414672Scube /* That implies d->d_ihead == i */ 12267b7c582aScube *ppi = j; 1227e499d8b5Scube else 12287b7c582aScube (*ppi)->i_bsame = j; 1229e499d8b5Scube if (d->d_ipp == &i->i_bsame) { 12303b405775Scube if (i->i_alias == NULL) { 12317b7c582aScube if (f == i) 1232e499d8b5Scube d->d_ipp = &d->d_ihead; 1233e499d8b5Scube else 12347b7c582aScube d->d_ipp = &f->i_bsame; 12353b405775Scube } else 12363b405775Scube d->d_ipp = &i->i_alias->i_bsame; 1237e499d8b5Scube } 1238e499d8b5Scube } 1239e499d8b5Scube /* 1240e499d8b5Scube * - delete the attachment instance 1241e499d8b5Scube */ 1242e499d8b5Scube iba = i->i_atdeva; 1243e499d8b5Scube for (ppi = &iba->d_ihead; 1244e499d8b5Scube *ppi != NULL && *ppi != i && (*ppi)->i_asame != i; 1245e499d8b5Scube ppi = &(*ppi)->i_asame); 1246e499d8b5Scube if (*ppi == NULL) 1247e499d8b5Scube panic("deldev: deva (%s) doesn't list the devi (%s)", 1248e499d8b5Scube iba->d_name, i->i_name); 12497b7c582aScube f = *ppi; 12507b7c582aScube if (f == i) 1251c3414672Scube /* That implies iba->d_ihead == i */ 1252e499d8b5Scube *ppi = i->i_asame; 1253e499d8b5Scube else 1254e499d8b5Scube (*ppi)->i_asame = i->i_asame; 1255e499d8b5Scube if (iba->d_ipp == &i->i_asame) { 12567b7c582aScube if (f == i) 1257e499d8b5Scube iba->d_ipp = &iba->d_ihead; 1258e499d8b5Scube else 12597b7c582aScube iba->d_ipp = &f->i_asame; 1260e499d8b5Scube } 1261e499d8b5Scube /* 1262e499d8b5Scube * - delete the pspec 1263e499d8b5Scube */ 1264e499d8b5Scube if (i->i_pspec) { 1265e499d8b5Scube struct pspec *p = i->i_pspec; 1266e499d8b5Scube struct nvlist *nv, *onv; 1267e499d8b5Scube 1268e499d8b5Scube /* Double-linked nvlist anyone? */ 1269c0024066Scube for (nv = p->p_devs; nv->nv_next != NULL; nv = nv->nv_next) { 1270e499d8b5Scube if (nv->nv_next && nv->nv_next->nv_ptr == i) { 1271e499d8b5Scube onv = nv->nv_next; 1272e499d8b5Scube nv->nv_next = onv->nv_next; 1273e499d8b5Scube nvfree(onv); 1274e499d8b5Scube break; 1275c0024066Scube } 1276c0024066Scube if (nv->nv_ptr == i) { 1277e499d8b5Scube /* nv is p->p_devs in that case */ 1278e499d8b5Scube p->p_devs = nv->nv_next; 1279e499d8b5Scube nvfree(nv); 1280e499d8b5Scube break; 1281e499d8b5Scube } 1282e499d8b5Scube } 1283e499d8b5Scube if (p->p_devs == NULL) 1284e499d8b5Scube TAILQ_REMOVE(&allpspecs, p, p_list); 1285e499d8b5Scube } 1286e499d8b5Scube /* 1287e499d8b5Scube * - delete the alldevi entry 1288e499d8b5Scube */ 1289e499d8b5Scube TAILQ_REMOVE(&alldevi, i, i_next); 1290e499d8b5Scube ndevi--; 12917aa6070dScube /* 12927aa6070dScube * Put it in deaddevitab 1293a31ff6b4Scube * 1294a31ff6b4Scube * Each time a devi is removed, devilevel is increased so that later on 1295a31ff6b4Scube * it is possible to tell if an instance was added before or after the 1296a31ff6b4Scube * removal of its parent. 1297a31ff6b4Scube * 1298a31ff6b4Scube * For active instances, i_level contains the number of devi removed so 1299a31ff6b4Scube * far, and for dead devis, it contains its index. 13007aa6070dScube */ 1301a31ff6b4Scube i->i_level = devilevel++; 13027aa6070dScube i->i_alias = NULL; 13037aa6070dScube f = ht_lookup(deaddevitab, i->i_name); 13047aa6070dScube if (f == NULL) { 13057aa6070dScube if (ht_insert(deaddevitab, i->i_name, i)) 13067aa6070dScube panic("remove_devi(%s) - can't add to deaddevitab", 13077aa6070dScube i->i_name); 13087aa6070dScube } else { 13097aa6070dScube for (j = f; j->i_alias != NULL; j = j->i_alias); 13107aa6070dScube j->i_alias = i; 13117aa6070dScube } 1312e499d8b5Scube /* 13135d1e8b27Swiz * - reconstruct d->d_umax 1314e499d8b5Scube */ 1315e499d8b5Scube d->d_umax = 0; 1316e499d8b5Scube for (i = d->d_ihead; i != NULL; i = i->i_bsame) 1317e499d8b5Scube if (i->i_unit >= d->d_umax) 1318e499d8b5Scube d->d_umax = i->i_unit + 1; 13195ecc953bSthorpej } 13205ecc953bSthorpej 13215ecc953bSthorpej void 13227b7c582aScube deldeva(const char *at) 13237b7c582aScube { 13247b7c582aScube int unit; 13257b7c582aScube const char *cp; 13267b7c582aScube struct devbase *d, *ad; 13277b7c582aScube struct devi *i, *j; 13287b7c582aScube struct attr *a; 13297b7c582aScube struct pspec *p; 13307b7c582aScube struct nvlist *nv, *stack = NULL; 13317b7c582aScube 13327b7c582aScube if (at == NULL) { 13337b7c582aScube TAILQ_FOREACH(i, &alldevi, i_next) 13347b7c582aScube if (i->i_at == NULL) 13357b7c582aScube stack = newnv(NULL, NULL, i, 0, stack); 13367b7c582aScube } else { 13377b7c582aScube int l; 13387b7c582aScube 13397b7c582aScube l = strlen(at) - 1; 13407b7c582aScube if (at[l] == '?' || isdigit((unsigned char)at[l])) { 13417b7c582aScube char base[NAMESIZE]; 13427b7c582aScube 13437b7c582aScube if (split(at, l+1, base, sizeof base, &unit)) { 1344c7295a4cSchristos cfgerror("invalid attachment name `%s'", at); 13457b7c582aScube return; 13467b7c582aScube } 13477b7c582aScube cp = intern(base); 13487b7c582aScube } else { 13497b7c582aScube cp = intern(at); 13507b7c582aScube unit = STAR; 13517b7c582aScube } 13527b7c582aScube 13537b7c582aScube ad = ht_lookup(devbasetab, cp); 13547b7c582aScube a = ht_lookup(attrtab, cp); 13557b7c582aScube if (a == NULL) { 1356c7295a4cSchristos cfgerror("unknown attachment attribute or device `%s'", 13577b7c582aScube cp); 13587b7c582aScube return; 13597b7c582aScube } 13607b7c582aScube if (!a->a_iattr) { 1361c7295a4cSchristos cfgerror("plain attribute `%s' cannot have children", 13627b7c582aScube a->a_name); 13637b7c582aScube return; 13647b7c582aScube } 13657b7c582aScube 13667b7c582aScube /* 13677b7c582aScube * remove_devi() makes changes to the devbase's list and the 13687b7c582aScube * alias list, * so the actual deletion of the instances must 13697b7c582aScube * be delayed. 13707b7c582aScube */ 13717b7c582aScube for (nv = a->a_devs; nv != NULL; nv = nv->nv_next) { 13727b7c582aScube d = nv->nv_ptr; 13737b7c582aScube for (i = d->d_ihead; i != NULL; i = i->i_bsame) 13747b7c582aScube for (j = i; j != NULL; j = j->i_alias) { 13757b7c582aScube /* Ignore devices at root */ 13767b7c582aScube if (j->i_at == NULL) 13777b7c582aScube continue; 13787b7c582aScube p = j->i_pspec; 13797b7c582aScube /* 13807b7c582aScube * There are three cases: 13817b7c582aScube * 13827b7c582aScube * 1. unit is not STAR. Consider 'at' 13837b7c582aScube * to be explicit, even if it 13847b7c582aScube * references an interface 13857b7c582aScube * attribute. 13867b7c582aScube * 13877b7c582aScube * 2. unit is STAR and 'at' references 13887b7c582aScube * a real device. Look for pspec 13897b7c582aScube * that have a matching p_atdev 13907b7c582aScube * field. 13917b7c582aScube * 13927b7c582aScube * 3. unit is STAR and 'at' references 13937b7c582aScube * an interface attribute. Look 13947b7c582aScube * for pspec that have a matching 13957b7c582aScube * p_iattr field. 13967b7c582aScube */ 13977b7c582aScube if ((unit != STAR && /* Case */ 13987b7c582aScube !strcmp(j->i_at, at)) || /* 1 */ 13997b7c582aScube (unit == STAR && 14007b7c582aScube ((ad != NULL && /* Case */ 14017b7c582aScube p->p_atdev == ad) || /* 2 */ 14027b7c582aScube (ad == NULL && /* Case */ 14037b7c582aScube p->p_iattr == a)))) /* 3 */ 14047b7c582aScube stack = newnv(NULL, NULL, j, 0, 14057b7c582aScube stack); 14067b7c582aScube } 14077b7c582aScube } 14087b7c582aScube } 14097b7c582aScube 14107b7c582aScube for (nv = stack; nv != NULL; nv = nv->nv_next) 14117b7c582aScube remove_devi(nv->nv_ptr); 1412e50e4ee4Scube nvfreel(stack); 14137b7c582aScube } 14147b7c582aScube 14157b7c582aScube void 14167b7c582aScube deldev(const char *name) 14177b7c582aScube { 14187b7c582aScube int l; 14197b7c582aScube struct devi *firsti, *i; 14207b7c582aScube struct nvlist *nv, *stack = NULL; 14217b7c582aScube 14227b7c582aScube l = strlen(name) - 1; 14237b7c582aScube if (name[l] == '*' || isdigit((unsigned char)name[l])) { 14247b7c582aScube /* `no mydev0' or `no mydev*' */ 14257b7c582aScube firsti = ht_lookup(devitab, name); 14267b7c582aScube if (firsti == NULL) { 1427c7295a4cSchristos cfgerror("unknown instance %s", name); 14287b7c582aScube return; 14297b7c582aScube } 14307b7c582aScube for (i = firsti; i != NULL; i = i->i_alias) 14317b7c582aScube stack = newnv(NULL, NULL, i, 0, stack); 14327b7c582aScube } else { 14337b7c582aScube struct devbase *d = ht_lookup(devbasetab, name); 14347b7c582aScube 14357b7c582aScube if (d == NULL) { 1436c7295a4cSchristos cfgerror("unknown device %s", name); 14377b7c582aScube return; 14387b7c582aScube } 143971af9028Scube if (d->d_ispseudo) { 1440c7295a4cSchristos cfgerror("%s is a pseudo-device; " 144171af9028Scube "use \"no pseudo-device %s\" instead", name, 144271af9028Scube name); 144371af9028Scube return; 144471af9028Scube } 14457b7c582aScube 14467b7c582aScube for (firsti = d->d_ihead; firsti != NULL; 14477b7c582aScube firsti = firsti->i_bsame) 14487b7c582aScube for (i = firsti; i != NULL; i = i->i_alias) 14497b7c582aScube stack = newnv(NULL, NULL, i, 0, stack); 14507b7c582aScube } 14517b7c582aScube 14527b7c582aScube for (nv = stack; nv != NULL; nv = nv->nv_next) 14537b7c582aScube remove_devi(nv->nv_ptr); 1454e50e4ee4Scube nvfreel(stack); 14557b7c582aScube } 14567b7c582aScube 145700ed8a38Spooka /* 145800ed8a38Spooka * Insert given device "name" into devroottab. In case "name" 145900ed8a38Spooka * designates a pure interface attribute, create a fake device 146000ed8a38Spooka * instance for the attribute and insert that into the roottab 146100ed8a38Spooka * (this scheme avoids mucking around with the orphanage analysis). 146200ed8a38Spooka */ 14637b7c582aScube void 146490ac64deSpooka addpseudoroot(const char *name) 146590ac64deSpooka { 1466194e1c80Spooka char buf[NAMESIZE]; 1467194e1c80Spooka int unit; 1468194e1c80Spooka struct attr *attr; 146990ac64deSpooka struct devi *i; 147090ac64deSpooka struct deva *iba; 147190ac64deSpooka struct devbase *ib; 147290ac64deSpooka 1473194e1c80Spooka if (split(name, strlen(name), buf, sizeof(buf), &unit)) { 1474194e1c80Spooka cfgerror("invalid pseudo-root name `%s'", name); 1475194e1c80Spooka return; 1476194e1c80Spooka } 1477194e1c80Spooka 1478194e1c80Spooka /* 147900ed8a38Spooka * Prefer device because devices with locators define an 148000ed8a38Spooka * implicit interface attribute. However, if a device is 148100ed8a38Spooka * not available, try to attach to the interface attribute. 148200ed8a38Spooka * This makes sure adddev() doesn't get confused when we 148300ed8a38Spooka * are really attaching to a device (alternatively we maybe 148400ed8a38Spooka * could specify a non-NULL atlist to defdevattach() below). 1485194e1c80Spooka */ 148600ed8a38Spooka ib = ht_lookup(devbasetab, intern(buf)); 148700ed8a38Spooka if (ib == NULL) { 1488194e1c80Spooka struct devbase *fakedev; 1489194e1c80Spooka char fakename[NAMESIZE]; 1490194e1c80Spooka 149100ed8a38Spooka attr = ht_lookup(attrtab, intern(buf)); 149200ed8a38Spooka if (!(attr && attr->a_iattr)) { 149300ed8a38Spooka cfgerror("pseudo-root `%s' not available", name); 149400ed8a38Spooka return; 149500ed8a38Spooka } 149600ed8a38Spooka 1497194e1c80Spooka /* 1498194e1c80Spooka * here we cheat a bit: create a fake devbase with the 1499194e1c80Spooka * interface attribute and instantiate it. quick, cheap, 1500194e1c80Spooka * dirty & bad for you, much like the stuff in the fridge. 1501194e1c80Spooka * and, it works, since the pseudoroot device is not included 1502194e1c80Spooka * in ioconf, just used by config to make sure we start from 1503194e1c80Spooka * the right place. 1504194e1c80Spooka */ 150500ed8a38Spooka snprintf(fakename, sizeof(fakename), "%s_devattrs", buf); 1506194e1c80Spooka fakedev = getdevbase(intern(fakename)); 1507194e1c80Spooka fakedev->d_isdef = 1; 1508194e1c80Spooka fakedev->d_ispseudo = 0; 15094caf067bSdholland fakedev->d_attrs = attrlist_cons(NULL, attr); 1510194e1c80Spooka defdevattach(NULL, fakedev, NULL, NULL); 1511194e1c80Spooka 1512194e1c80Spooka if (unit == STAR) 1513194e1c80Spooka snprintf(buf, sizeof(buf), "%s*", fakename); 1514194e1c80Spooka else 1515194e1c80Spooka snprintf(buf, sizeof(buf), "%s%d", fakename, unit); 1516194e1c80Spooka name = buf; 1517194e1c80Spooka } 1518194e1c80Spooka 1519194e1c80Spooka /* ok, everything should be set up, so instantiate a fake device */ 152090ac64deSpooka i = getdevi(name); 152190ac64deSpooka if (i == NULL) 152200ed8a38Spooka panic("device `%s' expected to be present", name); 152390ac64deSpooka ib = i->i_base; 1524194e1c80Spooka iba = ib->d_ahead; 152590ac64deSpooka 152690ac64deSpooka i->i_atdeva = iba; 152790ac64deSpooka i->i_cfflags = 0; 152890ac64deSpooka i->i_locs = fixloc(name, &errattr, NULL); 152990ac64deSpooka i->i_pseudoroot = 1; 153090ac64deSpooka i->i_active = DEVI_ORPHAN; /* set active by kill_orphans() */ 153190ac64deSpooka 153290ac64deSpooka *iba->d_ipp = i; 153390ac64deSpooka iba->d_ipp = &i->i_asame; 153490ac64deSpooka 153590ac64deSpooka ht_insert(devroottab, ib->d_name, ib); 153690ac64deSpooka } 153790ac64deSpooka 153890ac64deSpooka void 15395ecc953bSthorpej addpseudo(const char *name, int number) 15405ecc953bSthorpej { 15415ecc953bSthorpej struct devbase *d; 15425ecc953bSthorpej struct devi *i; 15435ecc953bSthorpej 15445ecc953bSthorpej d = ht_lookup(devbasetab, name); 15455ecc953bSthorpej if (d == NULL) { 1546c7295a4cSchristos cfgerror("undefined pseudo-device %s", name); 15475ecc953bSthorpej return; 15485ecc953bSthorpej } 15495ecc953bSthorpej if (!d->d_ispseudo) { 1550c7295a4cSchristos cfgerror("%s is a real device, not a pseudo-device", name); 15515ecc953bSthorpej return; 15525ecc953bSthorpej } 15535ecc953bSthorpej if (ht_lookup(devitab, name) != NULL) { 1554c7295a4cSchristos cfgerror("`%s' already defined", name); 15555ecc953bSthorpej return; 15565ecc953bSthorpej } 15575ecc953bSthorpej i = newdevi(name, number - 1, d); /* foo 16 => "foo0..foo15" */ 15585ecc953bSthorpej if (ht_insert(devitab, name, i)) 15595ecc953bSthorpej panic("addpseudo(%s)", name); 15607b7c582aScube /* Useful to retrieve the instance from the devbase */ 15617b7c582aScube d->d_ihead = i; 15627aa6070dScube i->i_active = DEVI_ACTIVE; 15635ecc953bSthorpej TAILQ_INSERT_TAIL(&allpseudo, i, i_next); 15645ecc953bSthorpej } 15655ecc953bSthorpej 15665ecc953bSthorpej void 15675ecc953bSthorpej delpseudo(const char *name) 15685ecc953bSthorpej { 15695ecc953bSthorpej struct devbase *d; 15705ecc953bSthorpej struct devi *i; 15715ecc953bSthorpej 15725ecc953bSthorpej d = ht_lookup(devbasetab, name); 15735ecc953bSthorpej if (d == NULL) { 1574c7295a4cSchristos cfgerror("undefined pseudo-device %s", name); 15755ecc953bSthorpej return; 15765ecc953bSthorpej } 15775ecc953bSthorpej if (!d->d_ispseudo) { 1578c7295a4cSchristos cfgerror("%s is a real device, not a pseudo-device", name); 15795ecc953bSthorpej return; 15805ecc953bSthorpej } 15815ecc953bSthorpej if ((i = ht_lookup(devitab, name)) == NULL) { 1582c7295a4cSchristos cfgerror("`%s' not defined", name); 15835ecc953bSthorpej return; 15845ecc953bSthorpej } 15855ecc953bSthorpej d->d_umax = 0; /* clear neads-count entries */ 15867aa6070dScube d->d_ihead = NULL; /* make sure it won't be considered active */ 15875ecc953bSthorpej TAILQ_REMOVE(&allpseudo, i, i_next); 15885ecc953bSthorpej if (ht_remove(devitab, name)) 15895ecc953bSthorpej panic("delpseudo(%s) - can't remove from devitab", name); 15907aa6070dScube if (ht_insert(deaddevitab, name, i)) 15917aa6070dScube panic("delpseudo(%s) - can't add to deaddevitab", name); 15925ecc953bSthorpej } 15935ecc953bSthorpej 15945ecc953bSthorpej void 1595d767912bSdrochner adddevm(const char *name, devmajor_t cmajor, devmajor_t bmajor, 15969483bda7Sdholland struct condexpr *cond, struct nvlist *nv_nodes) 15975ecc953bSthorpej { 15985ecc953bSthorpej struct devm *dm; 15995ecc953bSthorpej 1600d767912bSdrochner if (cmajor != NODEVMAJOR && (cmajor < 0 || cmajor >= 4096)) { 1601c7295a4cSchristos cfgerror("character major %d is invalid", cmajor); 16029483bda7Sdholland condexpr_destroy(cond); 16033da3ab25Spooka nvfreel(nv_nodes); 16045ecc953bSthorpej return; 16055ecc953bSthorpej } 16065ecc953bSthorpej 1607d767912bSdrochner if (bmajor != NODEVMAJOR && (bmajor < 0 || bmajor >= 4096)) { 1608c7295a4cSchristos cfgerror("block major %d is invalid", bmajor); 16099483bda7Sdholland condexpr_destroy(cond); 16103da3ab25Spooka nvfreel(nv_nodes); 16115ecc953bSthorpej return; 16125ecc953bSthorpej } 1613d767912bSdrochner if (cmajor == NODEVMAJOR && bmajor == NODEVMAJOR) { 1614c7295a4cSchristos cfgerror("both character/block majors are not specified"); 16159483bda7Sdholland condexpr_destroy(cond); 16163da3ab25Spooka nvfreel(nv_nodes); 16175ecc953bSthorpej return; 16185ecc953bSthorpej } 16195ecc953bSthorpej 16205ecc953bSthorpej dm = ecalloc(1, sizeof(*dm)); 16215ecc953bSthorpej dm->dm_srcfile = yyfile; 16225ecc953bSthorpej dm->dm_srcline = currentline(); 16235ecc953bSthorpej dm->dm_name = name; 16245ecc953bSthorpej dm->dm_cmajor = cmajor; 16255ecc953bSthorpej dm->dm_bmajor = bmajor; 16269483bda7Sdholland dm->dm_opts = cond; 16273da3ab25Spooka dm->dm_devnodes = nv_nodes; 16285ecc953bSthorpej 16295ecc953bSthorpej TAILQ_INSERT_TAIL(&alldevms, dm, dm_next); 16305ecc953bSthorpej 16315ecc953bSthorpej maxcdevm = MAX(maxcdevm, dm->dm_cmajor); 16325ecc953bSthorpej maxbdevm = MAX(maxbdevm, dm->dm_bmajor); 16335ecc953bSthorpej } 16345ecc953bSthorpej 16357aa6070dScube int 16365ecc953bSthorpej fixdevis(void) 16375ecc953bSthorpej { 16385ecc953bSthorpej struct devi *i; 16397aa6070dScube int error = 0; 16405ecc953bSthorpej 16415ecc953bSthorpej TAILQ_FOREACH(i, &alldevi, i_next) 16427aa6070dScube if (i->i_active == DEVI_ACTIVE) 16435ecc953bSthorpej selectbase(i->i_base, i->i_atdeva); 16447aa6070dScube else if (i->i_active == DEVI_ORPHAN) { 1645c130d400Scube /* 1646c130d400Scube * At this point, we can't have instances for which 1647c130d400Scube * i_at or i_pspec are NULL. 1648c130d400Scube */ 16497aa6070dScube ++error; 1650c7295a4cSchristos cfgxerror(i->i_srcfile, i->i_lineno, 16510dbd1c0eScube "`%s at %s' is orphaned (%s `%s' found)", 1652c130d400Scube i->i_name, i->i_at, i->i_pspec->p_atunit == WILD ? 1653c130d400Scube "nothing matching" : "no", i->i_at); 16547aa6070dScube } else if (vflag && i->i_active == DEVI_IGNORED) 1655c7295a4cSchristos cfgxwarn(i->i_srcfile, i->i_lineno, "ignoring " 1656c7295a4cSchristos "explicitly orphaned instance `%s at %s'", 1657c7295a4cSchristos i->i_name, i->i_at); 16587aa6070dScube 16597aa6070dScube if (error) 16607aa6070dScube return error; 16615ecc953bSthorpej 16625ecc953bSthorpej TAILQ_FOREACH(i, &allpseudo, i_next) 16637aa6070dScube if (i->i_active == DEVI_ACTIVE) 16645ecc953bSthorpej selectbase(i->i_base, NULL); 16657aa6070dScube return 0; 16665ecc953bSthorpej } 16675ecc953bSthorpej 16685ecc953bSthorpej /* 16695ecc953bSthorpej * Look up a parent spec, creating a new one if it does not exist. 16705ecc953bSthorpej */ 16715ecc953bSthorpej static struct pspec * 16725ecc953bSthorpej getpspec(struct attr *attr, struct devbase *ab, int atunit) 16735ecc953bSthorpej { 16745ecc953bSthorpej struct pspec *p; 16755ecc953bSthorpej 16765ecc953bSthorpej TAILQ_FOREACH(p, &allpspecs, p_list) { 16775ecc953bSthorpej if (p->p_iattr == attr && 16785ecc953bSthorpej p->p_atdev == ab && 16795ecc953bSthorpej p->p_atunit == atunit) 16805ecc953bSthorpej return (p); 16815ecc953bSthorpej } 16825ecc953bSthorpej 16835ecc953bSthorpej p = ecalloc(1, sizeof(*p)); 16845ecc953bSthorpej 16855ecc953bSthorpej p->p_iattr = attr; 16865ecc953bSthorpej p->p_atdev = ab; 16875ecc953bSthorpej p->p_atunit = atunit; 16885ecc953bSthorpej p->p_inst = npspecs++; 1689c130d400Scube p->p_active = 0; 16905ecc953bSthorpej 16915ecc953bSthorpej TAILQ_INSERT_TAIL(&allpspecs, p, p_list); 16925ecc953bSthorpej 16935ecc953bSthorpej return (p); 16945ecc953bSthorpej } 16955ecc953bSthorpej 16965ecc953bSthorpej /* 16975ecc953bSthorpej * Define a new instance of a specific device. 16985ecc953bSthorpej */ 16995ecc953bSthorpej static struct devi * 17005ecc953bSthorpej getdevi(const char *name) 17015ecc953bSthorpej { 17025ecc953bSthorpej struct devi *i, *firsti; 17035ecc953bSthorpej struct devbase *d; 17045ecc953bSthorpej int unit; 17055ecc953bSthorpej char base[NAMESIZE]; 17065ecc953bSthorpej 17075ecc953bSthorpej if (split(name, strlen(name), base, sizeof base, &unit)) { 1708c7295a4cSchristos cfgerror("invalid device name `%s'", name); 17095ecc953bSthorpej return (NULL); 17105ecc953bSthorpej } 17115ecc953bSthorpej d = ht_lookup(devbasetab, intern(base)); 17125ecc953bSthorpej if (d == NULL) { 1713c7295a4cSchristos cfgerror("%s: unknown device `%s'", name, base); 17145ecc953bSthorpej return (NULL); 17155ecc953bSthorpej } 17165ecc953bSthorpej if (d->d_ispseudo) { 1717c7295a4cSchristos cfgerror("%s: %s is a pseudo-device", name, base); 17185ecc953bSthorpej return (NULL); 17195ecc953bSthorpej } 17205ecc953bSthorpej firsti = ht_lookup(devitab, name); 17215ecc953bSthorpej i = newdevi(name, unit, d); 17225ecc953bSthorpej if (firsti == NULL) { 17235ecc953bSthorpej if (ht_insert(devitab, name, i)) 17245ecc953bSthorpej panic("getdevi(%s)", name); 17255ecc953bSthorpej *d->d_ipp = i; 17265ecc953bSthorpej d->d_ipp = &i->i_bsame; 17275ecc953bSthorpej } else { 17285ecc953bSthorpej while (firsti->i_alias) 17295ecc953bSthorpej firsti = firsti->i_alias; 17305ecc953bSthorpej firsti->i_alias = i; 17315ecc953bSthorpej } 17325ecc953bSthorpej TAILQ_INSERT_TAIL(&alldevi, i, i_next); 17335ecc953bSthorpej ndevi++; 17345ecc953bSthorpej return (i); 17355ecc953bSthorpej } 17365ecc953bSthorpej 17375ecc953bSthorpej static const char * 17385ecc953bSthorpej concat(const char *name, int c) 17395ecc953bSthorpej { 1740c7295a4cSchristos size_t len; 17415ecc953bSthorpej char buf[NAMESIZE]; 17425ecc953bSthorpej 17435ecc953bSthorpej len = strlen(name); 17445ecc953bSthorpej if (len + 2 > sizeof(buf)) { 1745c7295a4cSchristos cfgerror("device name `%s%c' too long", name, c); 17465ecc953bSthorpej len = sizeof(buf) - 2; 17475ecc953bSthorpej } 17485ecc953bSthorpej memmove(buf, name, len); 17495ecc953bSthorpej buf[len] = c; 17505ecc953bSthorpej buf[len + 1] = 0; 17515ecc953bSthorpej return (intern(buf)); 17525ecc953bSthorpej } 17535ecc953bSthorpej 17545ecc953bSthorpej const char * 17555ecc953bSthorpej starref(const char *name) 17565ecc953bSthorpej { 17575ecc953bSthorpej 17585ecc953bSthorpej return (concat(name, '*')); 17595ecc953bSthorpej } 17605ecc953bSthorpej 17615ecc953bSthorpej const char * 17625ecc953bSthorpej wildref(const char *name) 17635ecc953bSthorpej { 17645ecc953bSthorpej 17655ecc953bSthorpej return (concat(name, '?')); 17665ecc953bSthorpej } 17675ecc953bSthorpej 17685ecc953bSthorpej /* 17695ecc953bSthorpej * Split a name like "foo0" into base name (foo) and unit number (0). 17705ecc953bSthorpej * Return 0 on success. To make this useful for names like "foo0a", 17715ecc953bSthorpej * the length of the "foo0" part is one of the arguments. 17725ecc953bSthorpej */ 17735ecc953bSthorpej static int 17745ecc953bSthorpej split(const char *name, size_t nlen, char *base, size_t bsize, int *aunit) 17755ecc953bSthorpej { 17765ecc953bSthorpej const char *cp; 1777c7295a4cSchristos int c; 1778c7295a4cSchristos size_t l; 17795ecc953bSthorpej 17805ecc953bSthorpej l = nlen; 17815ecc953bSthorpej if (l < 2 || l >= bsize || isdigit((unsigned char)*name)) 17825ecc953bSthorpej return (1); 17835ecc953bSthorpej c = (u_char)name[--l]; 17845ecc953bSthorpej if (!isdigit(c)) { 17855ecc953bSthorpej if (c == '*') 17865ecc953bSthorpej *aunit = STAR; 17875ecc953bSthorpej else if (c == '?') 17885ecc953bSthorpej *aunit = WILD; 17895ecc953bSthorpej else 17905ecc953bSthorpej return (1); 17915ecc953bSthorpej } else { 17925ecc953bSthorpej cp = &name[l]; 17935ecc953bSthorpej while (isdigit((unsigned char)cp[-1])) 17945ecc953bSthorpej l--, cp--; 17955ecc953bSthorpej *aunit = atoi(cp); 17965ecc953bSthorpej } 17975ecc953bSthorpej memmove(base, name, l); 17985ecc953bSthorpej base[l] = 0; 17995ecc953bSthorpej return (0); 18005ecc953bSthorpej } 18015ecc953bSthorpej 18025ecc953bSthorpej void 18035ecc953bSthorpej selectattr(struct attr *a) 18045ecc953bSthorpej { 18055ecc953bSthorpej 1806c7295a4cSchristos (void)ht_insert(selecttab, a->a_name, __UNCONST(a->a_name)); 1807b599bf99Suebayasi CFGDBG(3, "attr selected `%s'", a->a_name); 18085ecc953bSthorpej } 18095ecc953bSthorpej 18105ecc953bSthorpej /* 18115ecc953bSthorpej * We have an instance of the base foo, so select it and all its 18125ecc953bSthorpej * attributes for "optional foo". 18135ecc953bSthorpej */ 18145ecc953bSthorpej static void 18155ecc953bSthorpej selectbase(struct devbase *d, struct deva *da) 18165ecc953bSthorpej { 18175ecc953bSthorpej struct attr *a; 18184caf067bSdholland struct attrlist *al; 18195ecc953bSthorpej 1820c7295a4cSchristos (void)ht_insert(selecttab, d->d_name, __UNCONST(d->d_name)); 1821b599bf99Suebayasi CFGDBG(3, "devbase selected `%s'", d->d_name); 18224caf067bSdholland for (al = d->d_attrs; al != NULL; al = al->al_next) { 18234caf067bSdholland a = al->al_this; 18245ecc953bSthorpej expandattr(a, selectattr); 18255ecc953bSthorpej } 18265ecc953bSthorpej if (da != NULL) { 1827c7295a4cSchristos (void)ht_insert(selecttab, da->d_name, __UNCONST(da->d_name)); 1828b599bf99Suebayasi CFGDBG(3, "devattr selected `%s'", da->d_name); 18294caf067bSdholland for (al = da->d_attrs; al != NULL; al = al->al_next) { 18304caf067bSdholland a = al->al_this; 18315ecc953bSthorpej expandattr(a, selectattr); 18325ecc953bSthorpej } 18335ecc953bSthorpej } 18345ecc953bSthorpej } 18355ecc953bSthorpej 18365ecc953bSthorpej /* 18375ecc953bSthorpej * Is the given pointer on the given list of pointers? 18385ecc953bSthorpej */ 183959c94545Scube int 18405ecc953bSthorpej onlist(struct nvlist *nv, void *ptr) 18415ecc953bSthorpej { 18425ecc953bSthorpej for (; nv != NULL; nv = nv->nv_next) 18435ecc953bSthorpej if (nv->nv_ptr == ptr) 18445ecc953bSthorpej return (1); 18455ecc953bSthorpej return (0); 18465ecc953bSthorpej } 18475ecc953bSthorpej 18485ecc953bSthorpej static char * 18495ecc953bSthorpej extend(char *p, const char *name) 18505ecc953bSthorpej { 1851c7295a4cSchristos size_t l; 18525ecc953bSthorpej 18535ecc953bSthorpej l = strlen(name); 18545ecc953bSthorpej memmove(p, name, l); 18555ecc953bSthorpej p += l; 18565ecc953bSthorpej *p++ = ','; 18575ecc953bSthorpej *p++ = ' '; 18585ecc953bSthorpej return (p); 18595ecc953bSthorpej } 18605ecc953bSthorpej 18615ecc953bSthorpej /* 18625ecc953bSthorpej * Check that we got all required locators, and default any that are 18635ecc953bSthorpej * given as "?" and have defaults. Return 0 on success. 18645ecc953bSthorpej */ 18655ecc953bSthorpej static const char ** 186616a8771bSdholland fixloc(const char *name, struct attr *attr, struct loclist *got) 18675ecc953bSthorpej { 186816a8771bSdholland struct loclist *m, *n; 18695ecc953bSthorpej int ord; 18705ecc953bSthorpej const char **lp; 18715ecc953bSthorpej int nmissing, nextra, nnodefault; 18725ecc953bSthorpej char *mp, *ep, *ndp; 18735ecc953bSthorpej char missing[1000], extra[1000], nodefault[1000]; 18745ecc953bSthorpej static const char *nullvec[1]; 18755ecc953bSthorpej 18765ecc953bSthorpej /* 18775ecc953bSthorpej * Look for all required locators, and number the given ones 18785ecc953bSthorpej * according to the required order. While we are numbering, 18795ecc953bSthorpej * set default values for defaulted locators. 18805ecc953bSthorpej */ 18815ecc953bSthorpej if (attr->a_loclen == 0) /* e.g., "at root" */ 18825ecc953bSthorpej lp = nullvec; 18835ecc953bSthorpej else 18845ecc953bSthorpej lp = emalloc((attr->a_loclen + 1) * sizeof(const char *)); 188516a8771bSdholland for (n = got; n != NULL; n = n->ll_next) 188616a8771bSdholland n->ll_num = -1; 18875ecc953bSthorpej nmissing = 0; 18885ecc953bSthorpej mp = missing; 18895ecc953bSthorpej /* yes, this is O(mn), but m and n should be small */ 189016a8771bSdholland for (ord = 0, m = attr->a_locs; m != NULL; m = m->ll_next, ord++) { 189116a8771bSdholland for (n = got; n != NULL; n = n->ll_next) { 189216a8771bSdholland if (n->ll_name == m->ll_name) { 189316a8771bSdholland n->ll_num = ord; 18945ecc953bSthorpej break; 18955ecc953bSthorpej } 18965ecc953bSthorpej } 189716a8771bSdholland if (n == NULL && m->ll_num == 0) { 18985ecc953bSthorpej nmissing++; 189916a8771bSdholland mp = extend(mp, m->ll_name); 19005ecc953bSthorpej } 190116a8771bSdholland lp[ord] = m->ll_string; 19025ecc953bSthorpej } 19035ecc953bSthorpej if (ord != attr->a_loclen) 19045ecc953bSthorpej panic("fixloc"); 19055ecc953bSthorpej lp[ord] = NULL; 19065ecc953bSthorpej nextra = 0; 19075ecc953bSthorpej ep = extra; 19085ecc953bSthorpej nnodefault = 0; 19095ecc953bSthorpej ndp = nodefault; 191016a8771bSdholland for (n = got; n != NULL; n = n->ll_next) { 191116a8771bSdholland if (n->ll_num >= 0) { 191216a8771bSdholland if (n->ll_string != NULL) 191316a8771bSdholland lp[n->ll_num] = n->ll_string; 191416a8771bSdholland else if (lp[n->ll_num] == NULL) { 19155ecc953bSthorpej nnodefault++; 191616a8771bSdholland ndp = extend(ndp, n->ll_name); 19175ecc953bSthorpej } 19185ecc953bSthorpej } else { 19195ecc953bSthorpej nextra++; 192016a8771bSdholland ep = extend(ep, n->ll_name); 19215ecc953bSthorpej } 19225ecc953bSthorpej } 19235ecc953bSthorpej if (nextra) { 19245ecc953bSthorpej ep[-2] = 0; /* kill ", " */ 1925c7295a4cSchristos cfgerror("%s: extraneous locator%s: %s", 19265ecc953bSthorpej name, nextra > 1 ? "s" : "", extra); 19275ecc953bSthorpej } 19285ecc953bSthorpej if (nmissing) { 19295ecc953bSthorpej mp[-2] = 0; 1930c7295a4cSchristos cfgerror("%s: must specify %s", name, missing); 19315ecc953bSthorpej } 19325ecc953bSthorpej if (nnodefault) { 19335ecc953bSthorpej ndp[-2] = 0; 1934c7295a4cSchristos cfgerror("%s: cannot wildcard %s", name, nodefault); 19355ecc953bSthorpej } 19365ecc953bSthorpej if (nmissing || nnodefault) { 19375ecc953bSthorpej free(lp); 19385ecc953bSthorpej lp = NULL; 19395ecc953bSthorpej } 19405ecc953bSthorpej return (lp); 19415ecc953bSthorpej } 1942437f8925Scube 1943437f8925Scube void 1944437f8925Scube setversion(int newver) 1945437f8925Scube { 1946437f8925Scube if (newver > CONFIG_VERSION) 1947c7295a4cSchristos cfgerror("your sources require a newer version of config(1) " 1948437f8925Scube "-- please rebuild it."); 1949437f8925Scube else if (newver < CONFIG_MINVERSION) 1950c7295a4cSchristos cfgerror("your sources are out of date -- please update."); 1951437f8925Scube else 1952437f8925Scube version = newver; 1953437f8925Scube } 1954