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