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