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