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