Home | History | Annotate | Line # | Download | only in config
sem.c revision 1.67
      1 /*	$NetBSD: sem.c,v 1.67 2014/11/01 04:34:27 uebayasi Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1992, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This software was developed by the Computer Systems Engineering group
      8  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9  * contributed to Berkeley.
     10  *
     11  * All advertising materials mentioning features or use of this software
     12  * must display the following acknowledgement:
     13  *	This product includes software developed by the University of
     14  *	California, Lawrence Berkeley Laboratories.
     15  *
     16  * Redistribution and use in source and binary forms, with or without
     17  * modification, are permitted provided that the following conditions
     18  * are met:
     19  * 1. Redistributions of source code must retain the above copyright
     20  *    notice, this list of conditions and the following disclaimer.
     21  * 2. Redistributions in binary form must reproduce the above copyright
     22  *    notice, this list of conditions and the following disclaimer in the
     23  *    documentation and/or other materials provided with the distribution.
     24  * 3. Neither the name of the University nor the names of its contributors
     25  *    may be used to endorse or promote products derived from this software
     26  *    without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  * SUCH DAMAGE.
     39  *
     40  *	from: @(#)sem.c	8.1 (Berkeley) 6/6/93
     41  */
     42 
     43 #if HAVE_NBTOOL_CONFIG_H
     44 #include "nbtool_config.h"
     45 #endif
     46 
     47 #include <sys/cdefs.h>
     48 __RCSID("$NetBSD: sem.c,v 1.67 2014/11/01 04:34:27 uebayasi Exp $");
     49 
     50 #include <sys/param.h>
     51 #include <ctype.h>
     52 #include <stdio.h>
     53 #include <stdlib.h>
     54 #include <string.h>
     55 #include <util.h>
     56 #include "defs.h"
     57 #include "sem.h"
     58 
     59 /*
     60  * config semantics.
     61  */
     62 
     63 #define	NAMESIZE	100	/* local name buffers */
     64 
     65 const char *s_ifnet;		/* magic attribute */
     66 const char *s_qmark;
     67 const char *s_none;
     68 
     69 static struct hashtab *cfhashtab;	/* for config lookup */
     70 struct hashtab *devitab;		/* etc */
     71 struct attr allattr;
     72 
     73 static struct attr errattr;
     74 static struct devbase errdev;
     75 static struct deva errdeva;
     76 
     77 static int has_errobj(struct attrlist *, struct attr *);
     78 static struct nvlist *addtoattr(struct nvlist *, struct devbase *);
     79 static int resolve(struct nvlist **, const char *, const char *,
     80 		   struct nvlist *, int);
     81 static struct pspec *getpspec(struct attr *, struct devbase *, int);
     82 static struct devi *newdevi(const char *, int, struct devbase *d);
     83 static struct devi *getdevi(const char *);
     84 static void remove_devi(struct devi *);
     85 static const char *concat(const char *, int);
     86 static char *extend(char *, const char *);
     87 static int split(const char *, size_t, char *, size_t, int *);
     88 static void selectbase(struct devbase *, struct deva *);
     89 static const char **fixloc(const char *, struct attr *, struct loclist *);
     90 static const char *makedevstr(devmajor_t, devminor_t);
     91 static const char *major2name(devmajor_t);
     92 static devmajor_t dev2major(struct devbase *);
     93 
     94 extern const char *yyfile;
     95 extern int vflag;
     96 
     97 void
     98 initsem(void)
     99 {
    100 
    101 	attrtab = ht_new();
    102 	attrdeptab = ht_new();
    103 
    104 	allattr.a_name = "netbsd";
    105 	TAILQ_INIT(&allattr.a_files);
    106 	(void)ht_insert(attrtab, allattr.a_name, &allattr);
    107 	selectattr(&allattr);
    108 
    109 	errattr.a_name = "<internal>";
    110 
    111 	TAILQ_INIT(&allbases);
    112 
    113 	TAILQ_INIT(&alldevas);
    114 
    115 	TAILQ_INIT(&allpspecs);
    116 
    117 	cfhashtab = ht_new();
    118 	TAILQ_INIT(&allcf);
    119 
    120 	TAILQ_INIT(&alldevi);
    121 	errdev.d_name = "<internal>";
    122 
    123 	TAILQ_INIT(&allpseudo);
    124 
    125 	TAILQ_INIT(&alldevms);
    126 
    127 	s_ifnet = intern("ifnet");
    128 	s_qmark = intern("?");
    129 	s_none = intern("none");
    130 }
    131 
    132 /* Name of include file just ended (set in scan.l) */
    133 extern const char *lastfile;
    134 
    135 static struct attr *
    136 finddep(struct attr *a, const char *name)
    137 {
    138 	struct attrlist *al;
    139 
    140 	for (al = a->a_deps; al != NULL; al = al->al_next) {
    141 		struct attr *this = al->al_this;
    142 		if (strcmp(this->a_name, name) == 0)
    143 			return this;
    144 	}
    145 	return NULL;
    146 }
    147 
    148 static void
    149 mergedeps(const char *dname, const char *name)
    150 {
    151 	struct attr *a, *newa;
    152 
    153 	CFGDBG(4, "merging attr `%s' to devbase `%s'", name, dname);
    154 	a = refattr(dname);
    155 	if (finddep(a, name) == NULL) {
    156 		newa = refattr(name);
    157 		a->a_deps = attrlist_cons(a->a_deps, newa);
    158 		CFGDBG(3, "attr `%s' merged to attr `%s'", newa->a_name,
    159 		    a->a_name);
    160 	}
    161 }
    162 
    163 static void
    164 fixdev(struct devbase *dev)
    165 {
    166 	struct attrlist *al;
    167 	struct attr *devattr, *a;
    168 
    169 	devattr = refattr(dev->d_name);
    170 	if (devattr->a_devclass)
    171 		panic("%s: dev %s is devclass!", __func__, devattr->a_name);
    172 
    173 	CFGDBG(4, "fixing devbase `%s'", dev->d_name);
    174 	for (al = dev->d_attrs; al != NULL; al = al->al_next) {
    175 		a = al->al_this;
    176 		CFGDBG(4, "fixing devbase `%s' attr `%s'", dev->d_name, a->a_name);
    177 		if (a->a_iattr) {
    178 			a->a_refs = addtoattr(a->a_refs, dev);
    179 			CFGDBG(3, "device `%s' has iattr `%s'", dev->d_name,
    180 			    a->a_name);
    181 		} else if (a->a_devclass != NULL) {
    182 			if (dev->d_classattr != NULL && dev->d_classattr != a) {
    183 				cfgwarn("device `%s' has multiple classes "
    184 				    "(`%s' and `%s')",
    185 				    dev->d_name, dev->d_classattr->a_name,
    186 				    a->a_name);
    187 			}
    188 			if (dev->d_classattr == NULL) {
    189 				dev->d_classattr = a;
    190 				CFGDBG(3, "device `%s' is devclass `%s'", dev->d_name,
    191 				    a->a_name);
    192 			}
    193 		} else {
    194 			if (strcmp(dev->d_name, a->a_name) != 0) {
    195 				mergedeps(dev->d_name, a->a_name);
    196 			}
    197 		}
    198 	}
    199 }
    200 
    201 void
    202 enddefs(void)
    203 {
    204 	struct devbase *dev;
    205 
    206 	yyfile = "enddefs";
    207 
    208 	TAILQ_FOREACH(dev, &allbases, d_next) {
    209 		if (!dev->d_isdef) {
    210 			(void)fprintf(stderr,
    211 			    "%s: device `%s' used but not defined\n",
    212 			    lastfile, dev->d_name);
    213 			errors++;
    214 			continue;
    215 		}
    216 		fixdev(dev);
    217 	}
    218 	if (errors) {
    219 		(void)fprintf(stderr, "*** Stop.\n");
    220 		exit(1);
    221 	}
    222 }
    223 
    224 void
    225 setdefmaxusers(int min, int def, int max)
    226 {
    227 
    228 	if (min < 1 || min > def || def > max)
    229 		cfgerror("maxusers must have 1 <= min (%d) <= default (%d) "
    230 		    "<= max (%d)", min, def, max);
    231 	else {
    232 		minmaxusers = min;
    233 		defmaxusers = def;
    234 		maxmaxusers = max;
    235 	}
    236 }
    237 
    238 void
    239 setmaxusers(int n)
    240 {
    241 
    242 	if (maxusers == n) {
    243 		cfgerror("duplicate maxusers parameter");
    244 		return;
    245 	}
    246 	if (vflag && maxusers != 0)
    247 		cfgwarn("warning: maxusers already defined");
    248 	maxusers = n;
    249 	if (n < minmaxusers) {
    250 		cfgerror("warning: minimum of %d maxusers assumed",
    251 		    minmaxusers);
    252 		errors--;	/* take it away */
    253 		maxusers = minmaxusers;
    254 	} else if (n > maxmaxusers) {
    255 		cfgerror("warning: maxusers (%d) > %d", n, maxmaxusers);
    256 		errors--;
    257 	}
    258 }
    259 
    260 void
    261 setident(const char *i)
    262 {
    263 
    264 	if (i)
    265 		ident = intern(i);
    266 	else
    267 		ident = NULL;
    268 }
    269 
    270 /*
    271  * Define an attribute, optionally with an interface (a locator list)
    272  * and a set of attribute-dependencies.
    273  *
    274  * Attribute dependencies MAY NOT be interface attributes.
    275  *
    276  * Since an empty locator list is logically different from "no interface",
    277  * all locator lists include a dummy head node, which we discard here.
    278  */
    279 int
    280 defattr0(const char *name, struct loclist *locs, struct attrlist *deps,
    281     int devclass)
    282 {
    283 
    284 	if (locs != NULL)
    285 		return defiattr(name, locs, deps, devclass);
    286 	else if (devclass)
    287 		return defdevclass(name, locs, deps, devclass);
    288 	else
    289 		return defattr(name, locs, deps, devclass);
    290 }
    291 
    292 int
    293 defattr(const char *name, struct loclist *locs, struct attrlist *deps,
    294     int devclass)
    295 {
    296 	struct attr *a, *dep;
    297 	struct attrlist *al;
    298 
    299 	/*
    300 	 * If this attribute depends on any others, make sure none of
    301 	 * the dependencies are interface attributes.
    302 	 */
    303 	for (al = deps; al != NULL; al = al->al_next) {
    304 		dep = al->al_this;
    305 		if (dep->a_iattr) {
    306 			cfgerror("`%s' dependency `%s' is an interface "
    307 			    "attribute", name, dep->a_name);
    308 			return (1);
    309 		}
    310 		(void)ht_insert2(attrdeptab, name, dep->a_name, NULL);
    311 		CFGDBG(2, "attr `%s' depends on attr `%s'", name, dep->a_name);
    312 	}
    313 
    314 	if (getrefattr(name, &a)) {
    315 		cfgerror("attribute `%s' already defined", name);
    316 		loclist_destroy(locs);
    317 		return (1);
    318 	}
    319 	if (a == NULL)
    320 		a = mkattr(name);
    321 
    322 	a->a_deps = deps;
    323 	expandattr(a, NULL);
    324 	CFGDBG(3, "attr `%s' defined", a->a_name);
    325 
    326 	return (0);
    327 }
    328 
    329 struct attr *
    330 mkattr(const char *name)
    331 {
    332 	struct attr *a;
    333 
    334 	a = ecalloc(1, sizeof *a);
    335 	if (ht_insert(attrtab, name, a)) {
    336 		free(a);
    337 		return NULL;
    338 	}
    339 	a->a_name = name;
    340 	TAILQ_INIT(&a->a_files);
    341 	CFGDBG(3, "attr `%s' allocated", name);
    342 
    343 	return a;
    344 }
    345 
    346 /* "interface attribute" initialization */
    347 int
    348 defiattr(const char *name, struct loclist *locs, struct attrlist *deps,
    349     int devclass)
    350 {
    351 	struct attr *a;
    352 	int len;
    353 	struct loclist *ll;
    354 
    355 	if (devclass)
    356 		panic("defattr(%s): locators and devclass", name);
    357 
    358 	if (defattr(name, locs, deps, devclass) != 0)
    359 		return (1);
    360 
    361 	a = getattr(name);
    362 	a->a_iattr = 1;
    363 	/* unwrap */
    364 	a->a_locs = locs->ll_next;
    365 	locs->ll_next = NULL;
    366 	loclist_destroy(locs);
    367 	len = 0;
    368 	for (ll = a->a_locs; ll != NULL; ll = ll->ll_next)
    369 		len++;
    370 	a->a_loclen = len;
    371 	if (deps)
    372 		CFGDBG(2, "attr `%s' iface with deps", a->a_name);
    373 	return (0);
    374 }
    375 
    376 /* "device class" initialization */
    377 int
    378 defdevclass(const char *name, struct loclist *locs, struct attrlist *deps,
    379     int devclass)
    380 {
    381 	struct attr *a;
    382 	char classenum[256], *cp;
    383 	int errored = 0;
    384 
    385 	if (deps)
    386 		panic("defattr(%s): dependencies and devclass", name);
    387 
    388 	if (defattr(name, locs, deps, devclass) != 0)
    389 		return (1);
    390 
    391 	a = getattr(name);
    392 	(void)snprintf(classenum, sizeof(classenum), "DV_%s", name);
    393 	for (cp = classenum + 3; *cp; cp++) {
    394 		if (!errored &&
    395 		    (!isalnum((unsigned char)*cp) ||
    396 		      (isalpha((unsigned char)*cp) && !islower((unsigned char)*cp)))) {
    397 			cfgerror("device class names must be "
    398 			    "lower-case alphanumeric characters");
    399 			errored = 1;
    400 		}
    401 		*cp = toupper((unsigned char)*cp);
    402 	}
    403 	a->a_devclass = intern(classenum);
    404 
    405 	return (0);
    406 }
    407 
    408 /*
    409  * Return true if the given `error object' is embedded in the given
    410  * pointer list.
    411  */
    412 static int
    413 has_errobj(struct attrlist *al, struct attr *obj)
    414 {
    415 
    416 	for (; al != NULL; al = al->al_next)
    417 		if (al->al_this == obj)
    418 			return (1);
    419 	return (0);
    420 }
    421 
    422 /*
    423  * Return true if the given attribute is embedded in the given
    424  * pointer list.
    425  */
    426 int
    427 has_attr(struct attrlist *al, const char *attr)
    428 {
    429 	struct attr *a;
    430 
    431 	if ((a = getattr(attr)) == NULL)
    432 		return (0);
    433 
    434 	for (; al != NULL; al = al->al_next)
    435 		if (al->al_this == a)
    436 			return (1);
    437 	return (0);
    438 }
    439 
    440 /*
    441  * Add a device base to a list in an attribute (actually, to any list).
    442  * Note that this does not check for duplicates, and does reverse the
    443  * list order, but no one cares anyway.
    444  */
    445 static struct nvlist *
    446 addtoattr(struct nvlist *l, struct devbase *dev)
    447 {
    448 	struct nvlist *n;
    449 
    450 	n = newnv(NULL, NULL, dev, 0, l);
    451 	return (n);
    452 }
    453 
    454 /*
    455  * Define a device.  This may (or may not) also define an interface
    456  * attribute and/or refer to existing attributes.
    457  */
    458 void
    459 defdev(struct devbase *dev, struct loclist *loclist, struct attrlist *attrs,
    460        int ispseudo)
    461 {
    462 	struct loclist *ll;
    463 	struct attrlist *al;
    464 
    465 	if (dev == &errdev)
    466 		goto bad;
    467 	if (dev->d_isdef) {
    468 		cfgerror("redefinition of `%s'", dev->d_name);
    469 		goto bad;
    470 	}
    471 
    472 	dev->d_isdef = 1;
    473 	if (has_errobj(attrs, &errattr))
    474 		goto bad;
    475 
    476 	/*
    477 	 * Handle implicit attribute definition from locator list.  Do
    478 	 * this before scanning the `at' list so that we can have, e.g.:
    479 	 *	device foo at other, foo { slot = -1 }
    480 	 * (where you can plug in a foo-bus extender to a foo-bus).
    481 	 */
    482 	if (loclist != NULL) {
    483 		ll = loclist;
    484 		loclist = NULL;	/* defattr disposes of them for us */
    485 		if (defiattr(dev->d_name, ll, NULL, 0))
    486 			goto bad;
    487 		attrs = attrlist_cons(attrs, getattr(dev->d_name));
    488 		/* This used to be stored but was never used */
    489 		/* attrs->al_name = dev->d_name; */
    490 	}
    491 
    492 	/*
    493 	 * Pseudo-devices can have children.  Consider them as
    494 	 * attaching at root.
    495 	 */
    496 	if (ispseudo) {
    497 		for (al = attrs; al != NULL; al = al->al_next)
    498 			if (al->al_this->a_iattr)
    499 				break;
    500 		if (al != NULL) {
    501 			if (ispseudo < 2) {
    502 				if (version >= 20080610)
    503 					cfgerror("interface attribute on "
    504 					 "non-device pseudo `%s'", dev->d_name);
    505 				else {
    506 					ispseudo = 2;
    507 				}
    508 			}
    509 			ht_insert(devroottab, dev->d_name, dev);
    510 		}
    511 	}
    512 
    513 	/* Committed!  Set up fields. */
    514 	dev->d_ispseudo = ispseudo;
    515 	dev->d_attrs = attrs;
    516 	dev->d_classattr = NULL;		/* for now */
    517 	CFGDBG(3, "dev `%s' defined", dev->d_name);
    518 
    519 	/*
    520 	 * Implicit attribute definition for device.
    521 	 */
    522 	refattr(dev->d_name);
    523 
    524 	/*
    525 	 * For each interface attribute this device refers to, add this
    526 	 * device to its reference list.  This makes, e.g., finding all
    527 	 * "scsi"s easier.
    528 	 *
    529 	 * While looking through the attributes, set up the device
    530 	 * class if any are devclass attributes (and error out if the
    531 	 * device has two classes).
    532 	 */
    533 	for (al = attrs; al != NULL; al = al->al_next) {
    534 		/*
    535 		 * Implicit attribute definition for device dependencies.
    536 		 */
    537 		refattr(al->al_this->a_name);
    538 		(void)ht_insert2(attrdeptab, dev->d_name, al->al_this->a_name, NULL);
    539 		CFGDBG(2, "device `%s' depends on attr `%s'", dev->d_name,
    540 		    al->al_this->a_name);
    541 	}
    542 	return;
    543  bad:
    544 	loclist_destroy(loclist);
    545 	attrlist_destroyall(attrs);
    546 }
    547 
    548 /*
    549  * Look up a devbase.  Also makes sure it is a reasonable name,
    550  * i.e., does not end in a digit or contain special characters.
    551  */
    552 struct devbase *
    553 getdevbase(const char *name)
    554 {
    555 	const u_char *p;
    556 	struct devbase *dev;
    557 
    558 	p = (const u_char *)name;
    559 	if (!isalpha(*p))
    560 		goto badname;
    561 	while (*++p) {
    562 		if (!isalnum(*p) && *p != '_')
    563 			goto badname;
    564 	}
    565 	if (isdigit(*--p)) {
    566  badname:
    567 		cfgerror("bad device base name `%s'", name);
    568 		return (&errdev);
    569 	}
    570 	dev = ht_lookup(devbasetab, name);
    571 	if (dev == NULL) {
    572 		dev = ecalloc(1, sizeof *dev);
    573 		dev->d_name = name;
    574 		dev->d_isdef = 0;
    575 		dev->d_major = NODEVMAJOR;
    576 		dev->d_attrs = NULL;
    577 		dev->d_ihead = NULL;
    578 		dev->d_ipp = &dev->d_ihead;
    579 		dev->d_ahead = NULL;
    580 		dev->d_app = &dev->d_ahead;
    581 		dev->d_umax = 0;
    582 		TAILQ_INSERT_TAIL(&allbases, dev, d_next);
    583 		if (ht_insert(devbasetab, name, dev))
    584 			panic("getdevbase(%s)", name);
    585 		CFGDBG(3, "devbase defined `%s'", dev->d_name);
    586 	}
    587 	return (dev);
    588 }
    589 
    590 /*
    591  * Define some of a device's allowable parent attachments.
    592  * There may be a list of (plain) attributes.
    593  */
    594 void
    595 defdevattach(struct deva *deva, struct devbase *dev, struct nvlist *atlist,
    596 	     struct attrlist *attrs)
    597 {
    598 	struct nvlist *nv;
    599 	struct attrlist *al;
    600 	struct attr *a;
    601 	struct deva *da;
    602 
    603 	if (dev == &errdev)
    604 		goto bad;
    605 	if (deva == NULL)
    606 		deva = getdevattach(dev->d_name);
    607 	if (deva == &errdeva)
    608 		goto bad;
    609 	if (!dev->d_isdef) {
    610 		cfgerror("attaching undefined device `%s'", dev->d_name);
    611 		goto bad;
    612 	}
    613 	if (deva->d_isdef) {
    614 		cfgerror("redefinition of `%s'", deva->d_name);
    615 		goto bad;
    616 	}
    617 	if (dev->d_ispseudo) {
    618 		cfgerror("pseudo-devices can't attach");
    619 		goto bad;
    620 	}
    621 
    622 	deva->d_isdef = 1;
    623 	if (has_errobj(attrs, &errattr))
    624 		goto bad;
    625 	for (al = attrs; al != NULL; al = al->al_next) {
    626 		a = al->al_this;
    627 		if (a == &errattr)
    628 			continue;		/* already complained */
    629 		if (a->a_iattr || a->a_devclass != NULL)
    630 			cfgerror("`%s' is not a plain attribute", a->a_name);
    631 	}
    632 
    633 	/* Committed!  Set up fields. */
    634 	deva->d_attrs = attrs;
    635 	deva->d_atlist = atlist;
    636 	deva->d_devbase = dev;
    637 	CFGDBG(3, "deva `%s' defined", deva->d_name);
    638 
    639 	/*
    640 	 * Implicit attribute definition for device attachment.
    641 	 */
    642 	refattr(deva->d_name);
    643 
    644 	/*
    645 	 * Turn the `at' list into interface attributes (map each
    646 	 * nv_name to an attribute, or to NULL for root), and add
    647 	 * this device to those attributes, so that children can
    648 	 * be listed at this particular device if they are supported
    649 	 * by that attribute.
    650 	 */
    651 	for (nv = atlist; nv != NULL; nv = nv->nv_next) {
    652 		if (nv->nv_name == NULL)
    653 			nv->nv_ptr = a = NULL;	/* at root */
    654 		else
    655 			nv->nv_ptr = a = getattr(nv->nv_name);
    656 		if (a == &errattr)
    657 			continue;		/* already complained */
    658 
    659 		/*
    660 		 * Make sure that an attachment spec doesn't
    661 		 * already say how to attach to this attribute.
    662 		 */
    663 		for (da = dev->d_ahead; da != NULL; da = da->d_bsame)
    664 			if (onlist(da->d_atlist, a))
    665 				cfgerror("attach at `%s' already done by `%s'",
    666 				     a ? a->a_name : "root", da->d_name);
    667 
    668 		if (a == NULL) {
    669 			ht_insert(devroottab, dev->d_name, dev);
    670 			continue;		/* at root; don't add */
    671 		}
    672 		if (!a->a_iattr)
    673 			cfgerror("%s cannot be at plain attribute `%s'",
    674 			    dev->d_name, a->a_name);
    675 		else
    676 			a->a_devs = addtoattr(a->a_devs, dev);
    677 	}
    678 
    679 	/* attach to parent */
    680 	*dev->d_app = deva;
    681 	dev->d_app = &deva->d_bsame;
    682 	return;
    683  bad:
    684 	nvfreel(atlist);
    685 	attrlist_destroyall(attrs);
    686 }
    687 
    688 /*
    689  * Look up a device attachment.  Also makes sure it is a reasonable
    690  * name, i.e., does not contain digits or special characters.
    691  */
    692 struct deva *
    693 getdevattach(const char *name)
    694 {
    695 	const u_char *p;
    696 	struct deva *deva;
    697 
    698 	p = (const u_char *)name;
    699 	if (!isalpha(*p))
    700 		goto badname;
    701 	while (*++p) {
    702 		if (!isalnum(*p) && *p != '_')
    703 			goto badname;
    704 	}
    705 	if (isdigit(*--p)) {
    706  badname:
    707 		cfgerror("bad device attachment name `%s'", name);
    708 		return (&errdeva);
    709 	}
    710 	deva = ht_lookup(devatab, name);
    711 	if (deva == NULL) {
    712 		deva = ecalloc(1, sizeof *deva);
    713 		deva->d_name = name;
    714 		deva->d_bsame = NULL;
    715 		deva->d_isdef = 0;
    716 		deva->d_devbase = NULL;
    717 		deva->d_atlist = NULL;
    718 		deva->d_attrs = NULL;
    719 		deva->d_ihead = NULL;
    720 		deva->d_ipp = &deva->d_ihead;
    721 		TAILQ_INSERT_TAIL(&alldevas, deva, d_next);
    722 		if (ht_insert(devatab, name, deva))
    723 			panic("getdeva(%s)", name);
    724 	}
    725 	return (deva);
    726 }
    727 
    728 /*
    729  * Look up an attribute.
    730  */
    731 struct attr *
    732 getattr(const char *name)
    733 {
    734 	struct attr *a;
    735 
    736 	if ((a = ht_lookup(attrtab, name)) == NULL) {
    737 		cfgerror("undefined attribute `%s'", name);
    738 		a = &errattr;
    739 	}
    740 	return (a);
    741 }
    742 
    743 /*
    744  * Implicit attribute definition.
    745  */
    746 struct attr *
    747 refattr(const char *name)
    748 {
    749 	struct attr *a;
    750 
    751 	if ((a = ht_lookup(attrtab, name)) == NULL)
    752 		a = mkattr(name);
    753 	return a;
    754 }
    755 
    756 int
    757 getrefattr(const char *name, struct attr **ra)
    758 {
    759 	struct attr *a;
    760 
    761 	a = ht_lookup(attrtab, name);
    762 	if (a == NULL) {
    763 		*ra = NULL;
    764 		return (0);
    765 	}
    766 	/*
    767 	 * Check if the existing attr is only referenced, not really defined.
    768 	 */
    769 	if (a->a_deps == NULL &&
    770 	    a->a_iattr == 0 &&
    771 	    a->a_devclass == 0) {
    772 		*ra = a;
    773 		return (0);
    774 	}
    775 	return (1);
    776 }
    777 
    778 /*
    779  * Recursively expand an attribute and its dependencies, checking for
    780  * cycles, and invoking a callback for each attribute found.
    781  */
    782 void
    783 expandattr(struct attr *a, void (*callback)(struct attr *))
    784 {
    785 	struct attrlist *al;
    786 	struct attr *dep;
    787 
    788 	if (a->a_expanding) {
    789 		cfgerror("circular dependency on attribute `%s'", a->a_name);
    790 		return;
    791 	}
    792 
    793 	a->a_expanding = 1;
    794 
    795 	/* First expand all of this attribute's dependencies. */
    796 	for (al = a->a_deps; al != NULL; al = al->al_next) {
    797 		dep = al->al_this;
    798 		expandattr(dep, callback);
    799 	}
    800 
    801 	/* ...and now invoke the callback for ourself. */
    802 	if (callback != NULL)
    803 		(*callback)(a);
    804 
    805 	a->a_expanding = 0;
    806 }
    807 
    808 /*
    809  * Set the major device number for a device, so that it can be used
    810  * as a root/dumps "on" device in a configuration.
    811  */
    812 void
    813 setmajor(struct devbase *d, devmajor_t n)
    814 {
    815 
    816 	if (d != &errdev && d->d_major != NODEVMAJOR)
    817 		cfgerror("device `%s' is already major %d",
    818 		    d->d_name, d->d_major);
    819 	else
    820 		d->d_major = n;
    821 }
    822 
    823 const char *
    824 major2name(devmajor_t maj)
    825 {
    826 	struct devbase *dev;
    827 	struct devm *dm;
    828 
    829 	if (!do_devsw) {
    830 		TAILQ_FOREACH(dev, &allbases, d_next) {
    831 			if (dev->d_major == maj)
    832 				return (dev->d_name);
    833 		}
    834 	} else {
    835 		TAILQ_FOREACH(dm, &alldevms, dm_next) {
    836 			if (dm->dm_bmajor == maj)
    837 				return (dm->dm_name);
    838 		}
    839 	}
    840 	return (NULL);
    841 }
    842 
    843 devmajor_t
    844 dev2major(struct devbase *dev)
    845 {
    846 	struct devm *dm;
    847 
    848 	if (!do_devsw)
    849 		return (dev->d_major);
    850 
    851 	TAILQ_FOREACH(dm, &alldevms, dm_next) {
    852 		if (strcmp(dm->dm_name, dev->d_name) == 0)
    853 			return (dm->dm_bmajor);
    854 	}
    855 	return (NODEVMAJOR);
    856 }
    857 
    858 /*
    859  * Make a string description of the device at maj/min.
    860  */
    861 static const char *
    862 makedevstr(devmajor_t maj, devminor_t min)
    863 {
    864 	const char *devicename;
    865 	char buf[32];
    866 
    867 	devicename = major2name(maj);
    868 	if (devicename == NULL)
    869 		(void)snprintf(buf, sizeof(buf), "<%d/%d>", maj, min);
    870 	else
    871 		(void)snprintf(buf, sizeof(buf), "%s%d%c", devicename,
    872 		    min / maxpartitions, (min % maxpartitions) + 'a');
    873 
    874 	return (intern(buf));
    875 }
    876 
    877 /*
    878  * Map things like "ra0b" => makedev(major("ra"), 0*maxpartitions + 'b'-'a').
    879  * Handle the case where the device number is given but there is no
    880  * corresponding name, and map NULL to the default.
    881  */
    882 static int
    883 resolve(struct nvlist **nvp, const char *name, const char *what,
    884 	struct nvlist *dflt, int part)
    885 {
    886 	struct nvlist *nv;
    887 	struct devbase *dev;
    888 	const char *cp;
    889 	devmajor_t maj;
    890 	devminor_t min;
    891 	size_t i, l;
    892 	int unit;
    893 	char buf[NAMESIZE];
    894 
    895 	if ((part -= 'a') >= maxpartitions || part < 0)
    896 		panic("resolve");
    897 	if ((nv = *nvp) == NULL) {
    898 		dev_t	d = NODEV;
    899 		/*
    900 		 * Apply default.  Easiest to do this by number.
    901 		 * Make sure to retain NODEVness, if this is dflt's disposition.
    902 		 */
    903 		if ((dev_t)dflt->nv_num != NODEV) {
    904 			maj = major(dflt->nv_num);
    905 			min = ((minor(dflt->nv_num) / maxpartitions) *
    906 			    maxpartitions) + part;
    907 			d = makedev(maj, min);
    908 			cp = makedevstr(maj, min);
    909 		} else
    910 			cp = NULL;
    911 		*nvp = nv = newnv(NULL, cp, NULL, (long long)d, NULL);
    912 	}
    913 	if ((dev_t)nv->nv_num != NODEV) {
    914 		/*
    915 		 * By the numbers.  Find the appropriate major number
    916 		 * to make a name.
    917 		 */
    918 		maj = major(nv->nv_num);
    919 		min = minor(nv->nv_num);
    920 		nv->nv_str = makedevstr(maj, min);
    921 		return (0);
    922 	}
    923 
    924 	if (nv->nv_str == NULL || nv->nv_str == s_qmark)
    925 		/*
    926 		 * Wildcarded or unspecified; leave it as NODEV.
    927 		 */
    928 		return (0);
    929 
    930 	/*
    931 	 * The normal case: things like "ra2b".  Check for partition
    932 	 * suffix, remove it if there, and split into name ("ra") and
    933 	 * unit (2).
    934 	 */
    935 	l = i = strlen(nv->nv_str);
    936 	cp = &nv->nv_str[l];
    937 	if (l > 1 && *--cp >= 'a' && *cp < 'a' + maxpartitions &&
    938 	    isdigit((unsigned char)cp[-1])) {
    939 		l--;
    940 		part = *cp - 'a';
    941 	}
    942 	cp = nv->nv_str;
    943 	if (split(cp, l, buf, sizeof buf, &unit)) {
    944 		cfgerror("%s: invalid %s device name `%s'", name, what, cp);
    945 		return (1);
    946 	}
    947 	dev = ht_lookup(devbasetab, intern(buf));
    948 	if (dev == NULL) {
    949 		cfgerror("%s: device `%s' does not exist", name, buf);
    950 		return (1);
    951 	}
    952 
    953 	/*
    954 	 * Check for the magic network interface attribute, and
    955 	 * don't bother making a device number.
    956 	 */
    957 	if (has_attr(dev->d_attrs, s_ifnet)) {
    958 		nv->nv_num = (long long)NODEV;
    959 		nv->nv_ifunit = unit;	/* XXX XXX XXX */
    960 	} else {
    961 		maj = dev2major(dev);
    962 		if (maj == NODEVMAJOR) {
    963 			cfgerror("%s: can't make %s device from `%s'",
    964 			    name, what, nv->nv_str);
    965 			return (1);
    966 		}
    967 		nv->nv_num = makedev(maj, unit * maxpartitions + part);
    968 	}
    969 
    970 	nv->nv_name = dev->d_name;
    971 	return (0);
    972 }
    973 
    974 /*
    975  * Add a completed configuration to the list.
    976  */
    977 void
    978 addconf(struct config *cf0)
    979 {
    980 	struct config *cf;
    981 	const char *name;
    982 
    983 	name = cf0->cf_name;
    984 	cf = ecalloc(1, sizeof *cf);
    985 	if (ht_insert(cfhashtab, name, cf)) {
    986 		cfgerror("configuration `%s' already defined", name);
    987 		free(cf);
    988 		goto bad;
    989 	}
    990 	*cf = *cf0;
    991 
    992 	/*
    993 	 * Resolve the root device.
    994 	 */
    995 	if (cf->cf_root == NULL) {
    996 		cfgerror("%s: no root device specified", name);
    997 		goto bad;
    998 	}
    999 	if (cf->cf_root && cf->cf_root->nv_str != s_qmark) {
   1000 		struct nvlist *nv;
   1001 		nv = cf->cf_root;
   1002 		if (resolve(&cf->cf_root, name, "root", nv, 'a'))
   1003 			goto bad;
   1004 	}
   1005 
   1006 	/*
   1007 	 * Resolve the dump device.
   1008 	 */
   1009 	if (cf->cf_dump == NULL || cf->cf_dump->nv_str == s_qmark) {
   1010 		/*
   1011 		 * Wildcarded dump device is equivalent to unspecified.
   1012 		 */
   1013 		cf->cf_dump = NULL;
   1014 	} else if (cf->cf_dump->nv_str == s_none) {
   1015 		/*
   1016 		 * Operator has requested that no dump device should be
   1017 		 * configured; do nothing.
   1018 		 */
   1019 	} else {
   1020 		if (resolve(&cf->cf_dump, name, "dumps", cf->cf_dump, 'b'))
   1021 			goto bad;
   1022 	}
   1023 
   1024 	/* Wildcarded fstype is `unspecified'. */
   1025 	if (cf->cf_fstype == s_qmark)
   1026 		cf->cf_fstype = NULL;
   1027 
   1028 	TAILQ_INSERT_TAIL(&allcf, cf, cf_next);
   1029 	return;
   1030  bad:
   1031 	nvfreel(cf0->cf_root);
   1032 	nvfreel(cf0->cf_dump);
   1033 }
   1034 
   1035 void
   1036 setconf(struct nvlist **npp, const char *what, struct nvlist *v)
   1037 {
   1038 
   1039 	if (*npp != NULL) {
   1040 		cfgerror("duplicate %s specification", what);
   1041 		nvfreel(v);
   1042 	} else
   1043 		*npp = v;
   1044 }
   1045 
   1046 void
   1047 delconf(const char *name)
   1048 {
   1049 	struct config *cf;
   1050 
   1051 	CFGDBG(5, "deselecting config `%s'", name);
   1052 	if (ht_lookup(cfhashtab, name) == NULL) {
   1053 		cfgerror("configuration `%s' undefined", name);
   1054 		return;
   1055 	}
   1056 	(void)ht_remove(cfhashtab, name);
   1057 
   1058 	TAILQ_FOREACH(cf, &allcf, cf_next)
   1059 		if (!strcmp(cf->cf_name, name))
   1060 			break;
   1061 	if (cf == NULL)
   1062 		panic("lost configuration `%s'", name);
   1063 
   1064 	TAILQ_REMOVE(&allcf, cf, cf_next);
   1065 }
   1066 
   1067 void
   1068 setfstype(const char **fstp, const char *v)
   1069 {
   1070 
   1071 	if (*fstp != NULL) {
   1072 		cfgerror("multiple fstype specifications");
   1073 		return;
   1074 	}
   1075 
   1076 	if (v != s_qmark && OPT_FSOPT(v)) {
   1077 		cfgerror("\"%s\" is not a configured file system", v);
   1078 		return;
   1079 	}
   1080 
   1081 	*fstp = v;
   1082 }
   1083 
   1084 static struct devi *
   1085 newdevi(const char *name, int unit, struct devbase *d)
   1086 {
   1087 	struct devi *i;
   1088 
   1089 	i = ecalloc(1, sizeof *i);
   1090 	i->i_name = name;
   1091 	i->i_unit = unit;
   1092 	i->i_base = d;
   1093 	i->i_bsame = NULL;
   1094 	i->i_asame = NULL;
   1095 	i->i_alias = NULL;
   1096 	i->i_at = NULL;
   1097 	i->i_pspec = NULL;
   1098 	i->i_atdeva = NULL;
   1099 	i->i_locs = NULL;
   1100 	i->i_cfflags = 0;
   1101 	i->i_lineno = currentline();
   1102 	i->i_srcfile = yyfile;
   1103 	i->i_active = DEVI_ORPHAN; /* Proper analysis comes later */
   1104 	i->i_level = devilevel;
   1105 	i->i_pseudoroot = 0;
   1106 	if (unit >= d->d_umax)
   1107 		d->d_umax = unit + 1;
   1108 	return (i);
   1109 }
   1110 
   1111 /*
   1112  * Add the named device as attaching to the named attribute (or perhaps
   1113  * another device instead) plus unit number.
   1114  */
   1115 void
   1116 adddev(const char *name, const char *at, struct loclist *loclist, int flags)
   1117 {
   1118 	struct devi *i;		/* the new instance */
   1119 	struct pspec *p;	/* and its pspec */
   1120 	struct attr *attr;	/* attribute that allows attach */
   1121 	struct devbase *ib;	/* i->i_base */
   1122 	struct devbase *ab;	/* not NULL => at another dev */
   1123 	struct attrlist *al;
   1124 	struct deva *iba;	/* devbase attachment used */
   1125 	const char *cp;
   1126 	int atunit;
   1127 	char atbuf[NAMESIZE];
   1128 	int hit;
   1129 
   1130 	ab = NULL;
   1131 	iba = NULL;
   1132 	if (at == NULL) {
   1133 		/* "at root" */
   1134 		p = NULL;
   1135 		if ((i = getdevi(name)) == NULL)
   1136 			goto bad;
   1137 		/*
   1138 		 * Must warn about i_unit > 0 later, after taking care of
   1139 		 * the STAR cases (we could do non-star's here but why
   1140 		 * bother?).  Make sure this device can be at root.
   1141 		 */
   1142 		ib = i->i_base;
   1143 		hit = 0;
   1144 		for (iba = ib->d_ahead; iba != NULL; iba = iba->d_bsame)
   1145 			if (onlist(iba->d_atlist, NULL)) {
   1146 				hit = 1;
   1147 				break;
   1148 			}
   1149 		if (!hit) {
   1150 			cfgerror("`%s' cannot attach to the root", ib->d_name);
   1151 			i->i_active = DEVI_BROKEN;
   1152 			goto bad;
   1153 		}
   1154 		attr = &errattr;	/* a convenient "empty" attr */
   1155 	} else {
   1156 		if (split(at, strlen(at), atbuf, sizeof atbuf, &atunit)) {
   1157 			cfgerror("invalid attachment name `%s'", at);
   1158 			/* (void)getdevi(name); -- ??? */
   1159 			goto bad;
   1160 		}
   1161 		if ((i = getdevi(name)) == NULL)
   1162 			goto bad;
   1163 		ib = i->i_base;
   1164 
   1165 		/*
   1166 		 * Devices can attach to two types of things: Attributes,
   1167 		 * and other devices (which have the appropriate attributes
   1168 		 * to allow attachment).
   1169 		 *
   1170 		 * (1) If we're attached to an attribute, then we don't need
   1171 		 *     look at the parent base device to see what attributes
   1172 		 *     it has, and make sure that we can attach to them.
   1173 		 *
   1174 		 * (2) If we're attached to a real device (i.e. named in
   1175 		 *     the config file), we want to remember that so that
   1176 		 *     at cross-check time, if the device we're attached to
   1177 		 *     is missing but other devices which also provide the
   1178 		 *     attribute are present, we don't get a false "OK."
   1179 		 *
   1180 		 * (3) If the thing we're attached to is an attribute
   1181 		 *     but is actually named in the config file, we still
   1182 		 *     have to remember its devbase.
   1183 		 */
   1184 		cp = intern(atbuf);
   1185 
   1186 		/* Figure out parent's devbase, to satisfy case (3). */
   1187 		ab = ht_lookup(devbasetab, cp);
   1188 
   1189 		/* Find out if it's an attribute. */
   1190 		attr = ht_lookup(attrtab, cp);
   1191 
   1192 		/* Make sure we're _really_ attached to the attr.  Case (1). */
   1193 		if (attr != NULL && onlist(attr->a_devs, ib))
   1194 			goto findattachment;
   1195 
   1196 		/*
   1197 		 * Else a real device, and not just an attribute.  Case (2).
   1198 		 *
   1199 		 * Have to work a bit harder to see whether we have
   1200 		 * something like "tg0 at esp0" (where esp is merely
   1201 		 * not an attribute) or "tg0 at nonesuch0" (where
   1202 		 * nonesuch is not even a device).
   1203 		 */
   1204 		if (ab == NULL) {
   1205 			cfgerror("%s at %s: `%s' unknown",
   1206 			    name, at, atbuf);
   1207 			i->i_active = DEVI_BROKEN;
   1208 			goto bad;
   1209 		}
   1210 
   1211 		/*
   1212 		 * See if the named parent carries an attribute
   1213 		 * that allows it to supervise device ib.
   1214 		 */
   1215 		for (al = ab->d_attrs; al != NULL; al = al->al_next) {
   1216 			attr = al->al_this;
   1217 			if (onlist(attr->a_devs, ib))
   1218 				goto findattachment;
   1219 		}
   1220 		cfgerror("`%s' cannot attach to `%s'", ib->d_name, atbuf);
   1221 		i->i_active = DEVI_BROKEN;
   1222 		goto bad;
   1223 
   1224  findattachment:
   1225 		/*
   1226 		 * Find the parent spec.  If a matching one has not yet been
   1227 		 * created, create one.
   1228 		 */
   1229 		p = getpspec(attr, ab, atunit);
   1230 		p->p_devs = newnv(NULL, NULL, i, 0, p->p_devs);
   1231 
   1232 		/* find out which attachment it uses */
   1233 		hit = 0;
   1234 		for (iba = ib->d_ahead; iba != NULL; iba = iba->d_bsame)
   1235 			if (onlist(iba->d_atlist, attr)) {
   1236 				hit = 1;
   1237 				break;
   1238 			}
   1239 		if (!hit)
   1240 			panic("adddev: can't figure out attachment");
   1241 	}
   1242 	if ((i->i_locs = fixloc(name, attr, loclist)) == NULL) {
   1243 		i->i_active = DEVI_BROKEN;
   1244 		goto bad;
   1245 	}
   1246 	i->i_at = at;
   1247 	i->i_pspec = p;
   1248 	i->i_atdeva = iba;
   1249 	i->i_cfflags = flags;
   1250 	CFGDBG(3, "devi `%s' added", i->i_name);
   1251 
   1252 	*iba->d_ipp = i;
   1253 	iba->d_ipp = &i->i_asame;
   1254 
   1255 	/* all done, fall into ... */
   1256  bad:
   1257 	loclist_destroy(loclist);
   1258 	return;
   1259 }
   1260 
   1261 void
   1262 deldevi(const char *name, const char *at)
   1263 {
   1264 	struct devi *firsti, *i;
   1265 	struct devbase *d;
   1266 	int unit;
   1267 	char base[NAMESIZE];
   1268 
   1269 	CFGDBG(5, "deselecting devi `%s'", name);
   1270 	if (split(name, strlen(name), base, sizeof base, &unit)) {
   1271 		cfgerror("invalid device name `%s'", name);
   1272 		return;
   1273 	}
   1274 	d = ht_lookup(devbasetab, intern(base));
   1275 	if (d == NULL) {
   1276 		cfgerror("%s: unknown device `%s'", name, base);
   1277 		return;
   1278 	}
   1279 	if (d->d_ispseudo) {
   1280 		cfgerror("%s: %s is a pseudo-device", name, base);
   1281 		return;
   1282 	}
   1283 	if ((firsti = ht_lookup(devitab, name)) == NULL) {
   1284 		cfgerror("`%s' not defined", name);
   1285 		return;
   1286 	}
   1287 	if (at == NULL && firsti->i_at == NULL) {
   1288 		/* 'at root' */
   1289 		remove_devi(firsti);
   1290 		return;
   1291 	} else if (at != NULL)
   1292 		for (i = firsti; i != NULL; i = i->i_alias)
   1293 			if (i->i_active != DEVI_BROKEN &&
   1294 			    strcmp(at, i->i_at) == 0) {
   1295 				remove_devi(i);
   1296 				return;
   1297 			}
   1298 	cfgerror("`%s' at `%s' not found", name, at ? at : "root");
   1299 }
   1300 
   1301 static void
   1302 remove_devi(struct devi *i)
   1303 {
   1304 	struct devbase *d = i->i_base;
   1305 	struct devi *f, *j, **ppi;
   1306 	struct deva *iba;
   1307 
   1308 	CFGDBG(5, "removing devi `%s'", i->i_name);
   1309 	f = ht_lookup(devitab, i->i_name);
   1310 	if (f == NULL)
   1311 		panic("remove_devi(): instance %s disappeared from devitab",
   1312 		    i->i_name);
   1313 
   1314 	if (i->i_active == DEVI_BROKEN) {
   1315 		cfgerror("not removing broken instance `%s'", i->i_name);
   1316 		return;
   1317 	}
   1318 
   1319 	/*
   1320 	 * We have the device instance, i.
   1321 	 * We have to:
   1322 	 *   - delete the alias
   1323 	 *
   1324 	 *      If the devi was an alias of an already listed devi, all is
   1325 	 *      good we don't have to do more.
   1326 	 *      If it was the first alias, we have to replace i's entry in
   1327 	 *      d's list by its first alias.
   1328 	 *      If it was the only entry, we must remove i's entry from d's
   1329 	 *      list.
   1330 	 */
   1331 	if (i != f) {
   1332 		for (j = f; j->i_alias != i; j = j->i_alias)
   1333 			continue;
   1334 		j->i_alias = i->i_alias;
   1335 	} else {
   1336 		if (i->i_alias == NULL) {
   1337 			/* No alias, must unlink the entry from devitab */
   1338 			ht_remove(devitab, i->i_name);
   1339 			j = i->i_bsame;
   1340 		} else {
   1341 			/* Or have the first alias replace i in d's list */
   1342 			i->i_alias->i_bsame = i->i_bsame;
   1343 			j = i->i_alias;
   1344 			if (i == f)
   1345 				ht_replace(devitab, i->i_name, i->i_alias);
   1346 		}
   1347 
   1348 		/*
   1349 		 *   - remove/replace the instance from the devbase's list
   1350 		 *
   1351 		 * A double-linked list would make this much easier.  Oh, well,
   1352 		 * what is done is done.
   1353 		 */
   1354 		for (ppi = &d->d_ihead;
   1355 		    *ppi != NULL && *ppi != i && (*ppi)->i_bsame != i;
   1356 		    ppi = &(*ppi)->i_bsame)
   1357 			continue;
   1358 		if (*ppi == NULL)
   1359 			panic("deldev: dev (%s) doesn't list the devi"
   1360 			    " (%s at %s)", d->d_name, i->i_name, i->i_at);
   1361 		f = *ppi;
   1362 		if (f == i)
   1363 			/* That implies d->d_ihead == i */
   1364 			*ppi = j;
   1365 		else
   1366 			(*ppi)->i_bsame = j;
   1367 		if (d->d_ipp == &i->i_bsame) {
   1368 			if (i->i_alias == NULL) {
   1369 				if (f == i)
   1370 					d->d_ipp = &d->d_ihead;
   1371 				else
   1372 					d->d_ipp = &f->i_bsame;
   1373 			} else
   1374 				d->d_ipp = &i->i_alias->i_bsame;
   1375 		}
   1376 	}
   1377 	/*
   1378 	 *   - delete the attachment instance
   1379 	 */
   1380 	iba = i->i_atdeva;
   1381 	for (ppi = &iba->d_ihead;
   1382 	    *ppi != NULL && *ppi != i && (*ppi)->i_asame != i;
   1383 	    ppi = &(*ppi)->i_asame)
   1384 		continue;
   1385 	if (*ppi == NULL)
   1386 		panic("deldev: deva (%s) doesn't list the devi (%s)",
   1387 		    iba->d_name, i->i_name);
   1388 	f = *ppi;
   1389 	if (f == i)
   1390 		/* That implies iba->d_ihead == i */
   1391 		*ppi = i->i_asame;
   1392 	else
   1393 		(*ppi)->i_asame = i->i_asame;
   1394 	if (iba->d_ipp == &i->i_asame) {
   1395 		if (f == i)
   1396 			iba->d_ipp = &iba->d_ihead;
   1397 		else
   1398 			iba->d_ipp = &f->i_asame;
   1399 	}
   1400 	/*
   1401 	 *   - delete the pspec
   1402 	 */
   1403 	if (i->i_pspec) {
   1404 		struct pspec *p = i->i_pspec;
   1405 		struct nvlist *nv, *onv;
   1406 
   1407 		/* Double-linked nvlist anyone? */
   1408 		for (nv = p->p_devs; nv->nv_next != NULL; nv = nv->nv_next) {
   1409 			if (nv->nv_next && nv->nv_next->nv_ptr == i) {
   1410 				onv = nv->nv_next;
   1411 				nv->nv_next = onv->nv_next;
   1412 				nvfree(onv);
   1413 				break;
   1414 			}
   1415 			if (nv->nv_ptr == i) {
   1416 				/* nv is p->p_devs in that case */
   1417 				p->p_devs = nv->nv_next;
   1418 				nvfree(nv);
   1419 				break;
   1420 			}
   1421 		}
   1422 		if (p->p_devs == NULL)
   1423 			TAILQ_REMOVE(&allpspecs, p, p_list);
   1424 	}
   1425 	/*
   1426 	 *   - delete the alldevi entry
   1427 	 */
   1428 	TAILQ_REMOVE(&alldevi, i, i_next);
   1429 	ndevi--;
   1430 	/*
   1431 	 * Put it in deaddevitab
   1432 	 *
   1433 	 * Each time a devi is removed, devilevel is increased so that later on
   1434 	 * it is possible to tell if an instance was added before or after the
   1435 	 * removal of its parent.
   1436 	 *
   1437 	 * For active instances, i_level contains the number of devi removed so
   1438 	 * far, and for dead devis, it contains its index.
   1439 	 */
   1440 	i->i_level = devilevel++;
   1441 	i->i_alias = NULL;
   1442 	f = ht_lookup(deaddevitab, i->i_name);
   1443 	if (f == NULL) {
   1444 		if (ht_insert(deaddevitab, i->i_name, i))
   1445 			panic("remove_devi(%s) - can't add to deaddevitab",
   1446 			    i->i_name);
   1447 	} else {
   1448 		for (j = f; j->i_alias != NULL; j = j->i_alias)
   1449 			continue;
   1450 		j->i_alias = i;
   1451 	}
   1452 	/*
   1453 	 *   - reconstruct d->d_umax
   1454 	 */
   1455 	d->d_umax = 0;
   1456 	for (i = d->d_ihead; i != NULL; i = i->i_bsame)
   1457 		if (i->i_unit >= d->d_umax)
   1458 			d->d_umax = i->i_unit + 1;
   1459 }
   1460 
   1461 void
   1462 deldeva(const char *at)
   1463 {
   1464 	int unit;
   1465 	const char *cp;
   1466 	struct devbase *d, *ad;
   1467 	struct devi *i, *j;
   1468 	struct attr *a;
   1469 	struct pspec *p;
   1470 	struct nvlist *nv, *stack = NULL;
   1471 
   1472 	if (at == NULL) {
   1473 		TAILQ_FOREACH(i, &alldevi, i_next)
   1474 			if (i->i_at == NULL)
   1475 				stack = newnv(NULL, NULL, i, 0, stack);
   1476 	} else {
   1477 		size_t l;
   1478 
   1479 		CFGDBG(5, "deselecting deva `%s'", at);
   1480 		if (at[0] == '\0')
   1481 			goto out;
   1482 
   1483 		l = strlen(at) - 1;
   1484 		if (at[l] == '?' || isdigit((unsigned char)at[l])) {
   1485 			char base[NAMESIZE];
   1486 
   1487 			if (split(at, l+1, base, sizeof base, &unit)) {
   1488 out:
   1489 				cfgerror("invalid attachment name `%s'", at);
   1490 				return;
   1491 			}
   1492 			cp = intern(base);
   1493 		} else {
   1494 			cp = intern(at);
   1495 			unit = STAR;
   1496 		}
   1497 
   1498 		ad = ht_lookup(devbasetab, cp);
   1499 		a = ht_lookup(attrtab, cp);
   1500 		if (a == NULL) {
   1501 			cfgerror("unknown attachment attribute or device `%s'",
   1502 			    cp);
   1503 			return;
   1504 		}
   1505 		if (!a->a_iattr) {
   1506 			cfgerror("plain attribute `%s' cannot have children",
   1507 			    a->a_name);
   1508 			return;
   1509 		}
   1510 
   1511 		/*
   1512 		 * remove_devi() makes changes to the devbase's list and the
   1513 		 * alias list, * so the actual deletion of the instances must
   1514 		 * be delayed.
   1515 		 */
   1516 		for (nv = a->a_devs; nv != NULL; nv = nv->nv_next) {
   1517 			d = nv->nv_ptr;
   1518 			for (i = d->d_ihead; i != NULL; i = i->i_bsame)
   1519 				for (j = i; j != NULL; j = j->i_alias) {
   1520 					/* Ignore devices at root */
   1521 					if (j->i_at == NULL)
   1522 						continue;
   1523 					p = j->i_pspec;
   1524 					/*
   1525 					 * There are three cases:
   1526 					 *
   1527 					 * 1.  unit is not STAR.  Consider 'at'
   1528 					 *     to be explicit, even if it
   1529 					 *     references an interface
   1530 					 *     attribute.
   1531 					 *
   1532 					 * 2.  unit is STAR and 'at' references
   1533 					 *     a real device.  Look for pspec
   1534 					 *     that have a matching p_atdev
   1535 					 *     field.
   1536 					 *
   1537 					 * 3.  unit is STAR and 'at' references
   1538 					 *     an interface attribute.  Look
   1539 					 *     for pspec that have a matching
   1540 					 *     p_iattr field.
   1541 					 */
   1542 					if ((unit != STAR &&        /* Case */
   1543 					     !strcmp(j->i_at, at)) ||  /* 1 */
   1544 					    (unit == STAR &&
   1545 					     ((ad != NULL &&        /* Case */
   1546 					       p->p_atdev == ad) ||    /* 2 */
   1547 					      (ad == NULL &&        /* Case */
   1548 					       p->p_iattr == a))))     /* 3 */
   1549 						stack = newnv(NULL, NULL, j, 0,
   1550 						    stack);
   1551 				}
   1552 		}
   1553 	}
   1554 
   1555 	for (nv = stack; nv != NULL; nv = nv->nv_next)
   1556 		remove_devi(nv->nv_ptr);
   1557 	nvfreel(stack);
   1558 }
   1559 
   1560 void
   1561 deldev(const char *name)
   1562 {
   1563 	size_t l;
   1564 	struct devi *firsti, *i;
   1565 	struct nvlist *nv, *stack = NULL;
   1566 
   1567 	CFGDBG(5, "deselecting dev `%s'", name);
   1568 	if (name[0] == '\0')
   1569 		goto out;
   1570 
   1571 	l = strlen(name) - 1;
   1572 	if (name[l] == '*' || isdigit((unsigned char)name[l])) {
   1573 		/* `no mydev0' or `no mydev*' */
   1574 		firsti = ht_lookup(devitab, name);
   1575 		if (firsti == NULL) {
   1576 out:
   1577 			cfgerror("unknown instance %s", name);
   1578 			return;
   1579 		}
   1580 		for (i = firsti; i != NULL; i = i->i_alias)
   1581 			stack = newnv(NULL, NULL, i, 0, stack);
   1582 	} else {
   1583 		struct devbase *d = ht_lookup(devbasetab, name);
   1584 
   1585 		if (d == NULL) {
   1586 			cfgerror("unknown device %s", name);
   1587 			return;
   1588 		}
   1589 		if (d->d_ispseudo) {
   1590 			cfgerror("%s is a pseudo-device; "
   1591 			    "use \"no pseudo-device %s\" instead", name,
   1592 			    name);
   1593 			return;
   1594 		}
   1595 
   1596 		for (firsti = d->d_ihead; firsti != NULL;
   1597 		    firsti = firsti->i_bsame)
   1598 			for (i = firsti; i != NULL; i = i->i_alias)
   1599 				stack = newnv(NULL, NULL, i, 0, stack);
   1600 	}
   1601 
   1602 	for (nv = stack; nv != NULL; nv = nv->nv_next)
   1603 		remove_devi(nv->nv_ptr);
   1604 	nvfreel(stack);
   1605 }
   1606 
   1607 /*
   1608  * Insert given device "name" into devroottab.  In case "name"
   1609  * designates a pure interface attribute, create a fake device
   1610  * instance for the attribute and insert that into the roottab
   1611  * (this scheme avoids mucking around with the orphanage analysis).
   1612  */
   1613 void
   1614 addpseudoroot(const char *name)
   1615 {
   1616 	char buf[NAMESIZE];
   1617 	int unit;
   1618 	struct attr *attr;
   1619 	struct devi *i;
   1620 	struct deva *iba;
   1621 	struct devbase *ib;
   1622 
   1623 	if (split(name, strlen(name), buf, sizeof(buf), &unit)) {
   1624 		cfgerror("invalid pseudo-root name `%s'", name);
   1625 		return;
   1626 	}
   1627 
   1628 	/*
   1629 	 * Prefer device because devices with locators define an
   1630 	 * implicit interface attribute.  However, if a device is
   1631 	 * not available, try to attach to the interface attribute.
   1632 	 * This makes sure adddev() doesn't get confused when we
   1633 	 * are really attaching to a device (alternatively we maybe
   1634 	 * could specify a non-NULL atlist to defdevattach() below).
   1635 	 */
   1636 	ib = ht_lookup(devbasetab, intern(buf));
   1637 	if (ib == NULL) {
   1638 		struct devbase *fakedev;
   1639 		char fakename[NAMESIZE];
   1640 
   1641 		attr = ht_lookup(attrtab, intern(buf));
   1642 		if (!(attr && attr->a_iattr)) {
   1643 			cfgerror("pseudo-root `%s' not available", name);
   1644 			return;
   1645 		}
   1646 
   1647 		/*
   1648 		 * here we cheat a bit: create a fake devbase with the
   1649 		 * interface attribute and instantiate it.  quick, cheap,
   1650 		 * dirty & bad for you, much like the stuff in the fridge.
   1651 		 * and, it works, since the pseudoroot device is not included
   1652 		 * in ioconf, just used by config to make sure we start from
   1653 		 * the right place.
   1654 		 */
   1655 		snprintf(fakename, sizeof(fakename), "%s_devattrs", buf);
   1656 		fakedev = getdevbase(intern(fakename));
   1657 		fakedev->d_isdef = 1;
   1658 		fakedev->d_ispseudo = 0;
   1659 		fakedev->d_attrs = attrlist_cons(NULL, attr);
   1660 		defdevattach(NULL, fakedev, NULL, NULL);
   1661 
   1662 		if (unit == STAR)
   1663 			snprintf(buf, sizeof(buf), "%s*", fakename);
   1664 		else
   1665 			snprintf(buf, sizeof(buf), "%s%d", fakename, unit);
   1666 		name = buf;
   1667 	}
   1668 
   1669 	/* ok, everything should be set up, so instantiate a fake device */
   1670 	i = getdevi(name);
   1671 	if (i == NULL)
   1672 		panic("device `%s' expected to be present", name);
   1673 	ib = i->i_base;
   1674 	iba = ib->d_ahead;
   1675 
   1676 	i->i_atdeva = iba;
   1677 	i->i_cfflags = 0;
   1678 	i->i_locs = fixloc(name, &errattr, NULL);
   1679 	i->i_pseudoroot = 1;
   1680 	i->i_active = DEVI_ORPHAN; /* set active by kill_orphans() */
   1681 
   1682 	*iba->d_ipp = i;
   1683 	iba->d_ipp = &i->i_asame;
   1684 
   1685 	ht_insert(devroottab, ib->d_name, ib);
   1686 }
   1687 
   1688 void
   1689 addpseudo(const char *name, int number)
   1690 {
   1691 	struct devbase *d;
   1692 	struct devi *i;
   1693 
   1694 	d = ht_lookup(devbasetab, name);
   1695 	if (d == NULL) {
   1696 		cfgerror("undefined pseudo-device %s", name);
   1697 		return;
   1698 	}
   1699 	if (!d->d_ispseudo) {
   1700 		cfgerror("%s is a real device, not a pseudo-device", name);
   1701 		return;
   1702 	}
   1703 	if (ht_lookup(devitab, name) != NULL) {
   1704 		cfgerror("`%s' already defined", name);
   1705 		return;
   1706 	}
   1707 	i = newdevi(name, number - 1, d);	/* foo 16 => "foo0..foo15" */
   1708 	if (ht_insert(devitab, name, i))
   1709 		panic("addpseudo(%s)", name);
   1710 	/* Useful to retrieve the instance from the devbase */
   1711 	d->d_ihead = i;
   1712 	i->i_active = DEVI_ACTIVE;
   1713 	TAILQ_INSERT_TAIL(&allpseudo, i, i_next);
   1714 }
   1715 
   1716 void
   1717 delpseudo(const char *name)
   1718 {
   1719 	struct devbase *d;
   1720 	struct devi *i;
   1721 
   1722 	CFGDBG(5, "deselecting pseudo `%s'", name);
   1723 	d = ht_lookup(devbasetab, name);
   1724 	if (d == NULL) {
   1725 		cfgerror("undefined pseudo-device %s", name);
   1726 		return;
   1727 	}
   1728 	if (!d->d_ispseudo) {
   1729 		cfgerror("%s is a real device, not a pseudo-device", name);
   1730 		return;
   1731 	}
   1732 	if ((i = ht_lookup(devitab, name)) == NULL) {
   1733 		cfgerror("`%s' not defined", name);
   1734 		return;
   1735 	}
   1736 	d->d_umax = 0;		/* clear neads-count entries */
   1737 	d->d_ihead = NULL;	/* make sure it won't be considered active */
   1738 	TAILQ_REMOVE(&allpseudo, i, i_next);
   1739 	if (ht_remove(devitab, name))
   1740 		panic("delpseudo(%s) - can't remove from devitab", name);
   1741 	if (ht_insert(deaddevitab, name, i))
   1742 		panic("delpseudo(%s) - can't add to deaddevitab", name);
   1743 }
   1744 
   1745 void
   1746 adddevm(const char *name, devmajor_t cmajor, devmajor_t bmajor,
   1747 	struct condexpr *cond, struct nvlist *nv_nodes)
   1748 {
   1749 	struct devm *dm;
   1750 
   1751 	if (cmajor != NODEVMAJOR && (cmajor < 0 || cmajor >= 4096)) {
   1752 		cfgerror("character major %d is invalid", cmajor);
   1753 		condexpr_destroy(cond);
   1754 		nvfreel(nv_nodes);
   1755 		return;
   1756 	}
   1757 
   1758 	if (bmajor != NODEVMAJOR && (bmajor < 0 || bmajor >= 4096)) {
   1759 		cfgerror("block major %d is invalid", bmajor);
   1760 		condexpr_destroy(cond);
   1761 		nvfreel(nv_nodes);
   1762 		return;
   1763 	}
   1764 	if (cmajor == NODEVMAJOR && bmajor == NODEVMAJOR) {
   1765 		cfgerror("both character/block majors are not specified");
   1766 		condexpr_destroy(cond);
   1767 		nvfreel(nv_nodes);
   1768 		return;
   1769 	}
   1770 
   1771 	dm = ecalloc(1, sizeof(*dm));
   1772 	dm->dm_srcfile = yyfile;
   1773 	dm->dm_srcline = currentline();
   1774 	dm->dm_name = name;
   1775 	dm->dm_cmajor = cmajor;
   1776 	dm->dm_bmajor = bmajor;
   1777 	dm->dm_opts = cond;
   1778 	dm->dm_devnodes = nv_nodes;
   1779 
   1780 	TAILQ_INSERT_TAIL(&alldevms, dm, dm_next);
   1781 
   1782 	maxcdevm = MAX(maxcdevm, dm->dm_cmajor);
   1783 	maxbdevm = MAX(maxbdevm, dm->dm_bmajor);
   1784 }
   1785 
   1786 int
   1787 fixdevis(void)
   1788 {
   1789 	struct devi *i;
   1790 	int error = 0;
   1791 
   1792 	TAILQ_FOREACH(i, &alldevi, i_next) {
   1793 		CFGDBG(3, "fixing devis `%s'", i->i_name);
   1794 		if (i->i_active == DEVI_ACTIVE)
   1795 			selectbase(i->i_base, i->i_atdeva);
   1796 		else if (i->i_active == DEVI_ORPHAN) {
   1797 			/*
   1798 			 * At this point, we can't have instances for which
   1799 			 * i_at or i_pspec are NULL.
   1800 			 */
   1801 			++error;
   1802 			cfgxerror(i->i_srcfile, i->i_lineno,
   1803 			    "`%s at %s' is orphaned (%s `%s' found)",
   1804 			    i->i_name, i->i_at, i->i_pspec->p_atunit == WILD ?
   1805 			    "nothing matching" : "no", i->i_at);
   1806 		} else if (vflag && i->i_active == DEVI_IGNORED)
   1807 			cfgxwarn(i->i_srcfile, i->i_lineno, "ignoring "
   1808 			    "explicitly orphaned instance `%s at %s'",
   1809 			    i->i_name, i->i_at);
   1810 	}
   1811 
   1812 	if (error)
   1813 		return error;
   1814 
   1815 	TAILQ_FOREACH(i, &allpseudo, i_next)
   1816 		if (i->i_active == DEVI_ACTIVE)
   1817 			selectbase(i->i_base, NULL);
   1818 	return 0;
   1819 }
   1820 
   1821 /*
   1822  * Look up a parent spec, creating a new one if it does not exist.
   1823  */
   1824 static struct pspec *
   1825 getpspec(struct attr *attr, struct devbase *ab, int atunit)
   1826 {
   1827 	struct pspec *p;
   1828 
   1829 	TAILQ_FOREACH(p, &allpspecs, p_list) {
   1830 		if (p->p_iattr == attr &&
   1831 		    p->p_atdev == ab &&
   1832 		    p->p_atunit == atunit)
   1833 			return (p);
   1834 	}
   1835 
   1836 	p = ecalloc(1, sizeof(*p));
   1837 
   1838 	p->p_iattr = attr;
   1839 	p->p_atdev = ab;
   1840 	p->p_atunit = atunit;
   1841 	p->p_inst = npspecs++;
   1842 	p->p_active = 0;
   1843 
   1844 	TAILQ_INSERT_TAIL(&allpspecs, p, p_list);
   1845 
   1846 	return (p);
   1847 }
   1848 
   1849 /*
   1850  * Define a new instance of a specific device.
   1851  */
   1852 static struct devi *
   1853 getdevi(const char *name)
   1854 {
   1855 	struct devi *i, *firsti;
   1856 	struct devbase *d;
   1857 	int unit;
   1858 	char base[NAMESIZE];
   1859 
   1860 	if (split(name, strlen(name), base, sizeof base, &unit)) {
   1861 		cfgerror("invalid device name `%s'", name);
   1862 		return (NULL);
   1863 	}
   1864 	d = ht_lookup(devbasetab, intern(base));
   1865 	if (d == NULL) {
   1866 		cfgerror("%s: unknown device `%s'", name, base);
   1867 		return (NULL);
   1868 	}
   1869 	if (d->d_ispseudo) {
   1870 		cfgerror("%s: %s is a pseudo-device", name, base);
   1871 		return (NULL);
   1872 	}
   1873 	firsti = ht_lookup(devitab, name);
   1874 	i = newdevi(name, unit, d);
   1875 	if (firsti == NULL) {
   1876 		if (ht_insert(devitab, name, i))
   1877 			panic("getdevi(%s)", name);
   1878 		*d->d_ipp = i;
   1879 		d->d_ipp = &i->i_bsame;
   1880 	} else {
   1881 		while (firsti->i_alias)
   1882 			firsti = firsti->i_alias;
   1883 		firsti->i_alias = i;
   1884 	}
   1885 	TAILQ_INSERT_TAIL(&alldevi, i, i_next);
   1886 	ndevi++;
   1887 	return (i);
   1888 }
   1889 
   1890 static const char *
   1891 concat(const char *name, int c)
   1892 {
   1893 	size_t len;
   1894 	char buf[NAMESIZE];
   1895 
   1896 	len = strlen(name);
   1897 	if (len + 2 > sizeof(buf)) {
   1898 		cfgerror("device name `%s%c' too long", name, c);
   1899 		len = sizeof(buf) - 2;
   1900 	}
   1901 	memmove(buf, name, len);
   1902 	buf[len] = (char)c;
   1903 	buf[len + 1] = '\0';
   1904 	return (intern(buf));
   1905 }
   1906 
   1907 const char *
   1908 starref(const char *name)
   1909 {
   1910 
   1911 	return (concat(name, '*'));
   1912 }
   1913 
   1914 const char *
   1915 wildref(const char *name)
   1916 {
   1917 
   1918 	return (concat(name, '?'));
   1919 }
   1920 
   1921 /*
   1922  * Split a name like "foo0" into base name (foo) and unit number (0).
   1923  * Return 0 on success.  To make this useful for names like "foo0a",
   1924  * the length of the "foo0" part is one of the arguments.
   1925  */
   1926 static int
   1927 split(const char *name, size_t nlen, char *base, size_t bsize, int *aunit)
   1928 {
   1929 	const char *cp;
   1930 	int c;
   1931 	size_t l;
   1932 
   1933 	l = nlen;
   1934 	if (l < 2 || l >= bsize || isdigit((unsigned char)*name))
   1935 		return (1);
   1936 	c = (u_char)name[--l];
   1937 	if (!isdigit(c)) {
   1938 		if (c == '*')
   1939 			*aunit = STAR;
   1940 		else if (c == '?')
   1941 			*aunit = WILD;
   1942 		else
   1943 			return (1);
   1944 	} else {
   1945 		cp = &name[l];
   1946 		while (isdigit((unsigned char)cp[-1]))
   1947 			l--, cp--;
   1948 		*aunit = atoi(cp);
   1949 	}
   1950 	memmove(base, name, l);
   1951 	base[l] = 0;
   1952 	return (0);
   1953 }
   1954 
   1955 void
   1956 addattr(const char *name)
   1957 {
   1958 	struct attr *a;
   1959 
   1960 	a = refattr(name);
   1961 	selectattr(a);
   1962 }
   1963 
   1964 void
   1965 delattr(const char *name)
   1966 {
   1967 	struct attr *a;
   1968 
   1969 	a = refattr(name);
   1970 	deselectattr(a);
   1971 }
   1972 
   1973 void
   1974 selectattr(struct attr *a)
   1975 {
   1976 	struct attrlist *al;
   1977 	struct attr *dep;
   1978 
   1979 	CFGDBG(5, "selecting attr `%s'", a->a_name);
   1980 	for (al = a->a_deps; al != NULL; al = al->al_next) {
   1981 		dep = al->al_this;
   1982 		selectattr(dep);
   1983 	}
   1984 	(void)ht_insert(selecttab, a->a_name, __UNCONST(a->a_name));
   1985 	CFGDBG(3, "attr selected `%s'", a->a_name);
   1986 }
   1987 
   1988 static int
   1989 deselectattrcb2(const char *name1, const char *name2, void *v, void *arg)
   1990 {
   1991 	const char *name = arg;
   1992 
   1993 	if (strcmp(name, name2) == 0)
   1994 		delattr(name1);
   1995 	return 0;
   1996 }
   1997 
   1998 void
   1999 deselectattr(struct attr *a)
   2000 {
   2001 
   2002 	CFGDBG(5, "deselecting attr `%s'", a->a_name);
   2003 	ht_enumerate2(attrdeptab, deselectattrcb2, __UNCONST(a->a_name));
   2004 	(void)ht_remove(selecttab, a->a_name);
   2005 	CFGDBG(3, "attr deselected `%s'", a->a_name);
   2006 }
   2007 
   2008 static int
   2009 dumpattrdepcb2(const char *name1, const char *name2, void *v, void *arg)
   2010 {
   2011 
   2012 	CFGDBG(3, "attr `%s' depends on attr `%s'", name1, name2);
   2013 	return 0;
   2014 }
   2015 
   2016 void
   2017 dependattrs(void)
   2018 {
   2019 
   2020 	ht_enumerate2(attrdeptab, dumpattrdepcb2, NULL);
   2021 }
   2022 
   2023 /*
   2024  * We have an instance of the base foo, so select it and all its
   2025  * attributes for "optional foo".
   2026  */
   2027 static void
   2028 selectbase(struct devbase *d, struct deva *da)
   2029 {
   2030 	struct attr *a;
   2031 	struct attrlist *al;
   2032 
   2033 	(void)ht_insert(selecttab, d->d_name, __UNCONST(d->d_name));
   2034 	CFGDBG(3, "devbase selected `%s'", d->d_name);
   2035 	CFGDBG(5, "selecting dependencies of devbase `%s'", d->d_name);
   2036 	for (al = d->d_attrs; al != NULL; al = al->al_next) {
   2037 		a = al->al_this;
   2038 		expandattr(a, selectattr);
   2039 	}
   2040 
   2041 	struct attr *devattr;
   2042 	devattr = refattr(d->d_name);
   2043 	expandattr(devattr, selectattr);
   2044 
   2045 	if (da != NULL) {
   2046 		(void)ht_insert(selecttab, da->d_name, __UNCONST(da->d_name));
   2047 		CFGDBG(3, "devattr selected `%s'", da->d_name);
   2048 		for (al = da->d_attrs; al != NULL; al = al->al_next) {
   2049 			a = al->al_this;
   2050 			expandattr(a, selectattr);
   2051 		}
   2052 	}
   2053 
   2054 	fixdev(d);
   2055 }
   2056 
   2057 /*
   2058  * Is the given pointer on the given list of pointers?
   2059  */
   2060 int
   2061 onlist(struct nvlist *nv, void *ptr)
   2062 {
   2063 	for (; nv != NULL; nv = nv->nv_next)
   2064 		if (nv->nv_ptr == ptr)
   2065 			return (1);
   2066 	return (0);
   2067 }
   2068 
   2069 static char *
   2070 extend(char *p, const char *name)
   2071 {
   2072 	size_t l;
   2073 
   2074 	l = strlen(name);
   2075 	memmove(p, name, l);
   2076 	p += l;
   2077 	*p++ = ',';
   2078 	*p++ = ' ';
   2079 	return (p);
   2080 }
   2081 
   2082 /*
   2083  * Check that we got all required locators, and default any that are
   2084  * given as "?" and have defaults.  Return 0 on success.
   2085  */
   2086 static const char **
   2087 fixloc(const char *name, struct attr *attr, struct loclist *got)
   2088 {
   2089 	struct loclist *m, *n;
   2090 	int ord;
   2091 	const char **lp;
   2092 	int nmissing, nextra, nnodefault;
   2093 	char *mp, *ep, *ndp;
   2094 	char missing[1000], extra[1000], nodefault[1000];
   2095 	static const char *nullvec[1];
   2096 
   2097 	/*
   2098 	 * Look for all required locators, and number the given ones
   2099 	 * according to the required order.  While we are numbering,
   2100 	 * set default values for defaulted locators.
   2101 	 */
   2102 	if (attr->a_loclen == 0)	/* e.g., "at root" */
   2103 		lp = nullvec;
   2104 	else
   2105 		lp = emalloc((size_t)(attr->a_loclen + 1) * sizeof(const char *));
   2106 	for (n = got; n != NULL; n = n->ll_next)
   2107 		n->ll_num = -1;
   2108 	nmissing = 0;
   2109 	mp = missing;
   2110 	/* yes, this is O(mn), but m and n should be small */
   2111 	for (ord = 0, m = attr->a_locs; m != NULL; m = m->ll_next, ord++) {
   2112 		for (n = got; n != NULL; n = n->ll_next) {
   2113 			if (n->ll_name == m->ll_name) {
   2114 				n->ll_num = ord;
   2115 				break;
   2116 			}
   2117 		}
   2118 		if (n == NULL && m->ll_num == 0) {
   2119 			nmissing++;
   2120 			mp = extend(mp, m->ll_name);
   2121 		}
   2122 		lp[ord] = m->ll_string;
   2123 	}
   2124 	if (ord != attr->a_loclen)
   2125 		panic("fixloc");
   2126 	lp[ord] = NULL;
   2127 	nextra = 0;
   2128 	ep = extra;
   2129 	nnodefault = 0;
   2130 	ndp = nodefault;
   2131 	for (n = got; n != NULL; n = n->ll_next) {
   2132 		if (n->ll_num >= 0) {
   2133 			if (n->ll_string != NULL)
   2134 				lp[n->ll_num] = n->ll_string;
   2135 			else if (lp[n->ll_num] == NULL) {
   2136 				nnodefault++;
   2137 				ndp = extend(ndp, n->ll_name);
   2138 			}
   2139 		} else {
   2140 			nextra++;
   2141 			ep = extend(ep, n->ll_name);
   2142 		}
   2143 	}
   2144 	if (nextra) {
   2145 		ep[-2] = 0;	/* kill ", " */
   2146 		cfgerror("%s: extraneous locator%s: %s",
   2147 		    name, nextra > 1 ? "s" : "", extra);
   2148 	}
   2149 	if (nmissing) {
   2150 		mp[-2] = 0;
   2151 		cfgerror("%s: must specify %s", name, missing);
   2152 	}
   2153 	if (nnodefault) {
   2154 		ndp[-2] = 0;
   2155 		cfgerror("%s: cannot wildcard %s", name, nodefault);
   2156 	}
   2157 	if (nmissing || nnodefault) {
   2158 		free(lp);
   2159 		lp = NULL;
   2160 	}
   2161 	return (lp);
   2162 }
   2163 
   2164 void
   2165 setversion(int newver)
   2166 {
   2167 	if (newver > CONFIG_VERSION)
   2168 		cfgerror("your sources require a newer version of config(1) "
   2169 		    "-- please rebuild it.");
   2170 	else if (newver < CONFIG_MINVERSION)
   2171 		cfgerror("your sources are out of date -- please update.");
   2172 	else
   2173 		version = newver;
   2174 }
   2175