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