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