xref: /netbsd/usr.bin/config/sem.c (revision a87832fe)
1*a87832feSuebayasi /*	$NetBSD: sem.c,v 1.57 2014/10/11 06:18:29 uebayasi Exp $	*/
25ecc953bSthorpej 
35ecc953bSthorpej /*
45ecc953bSthorpej  * Copyright (c) 1992, 1993
55ecc953bSthorpej  *	The Regents of the University of California.  All rights reserved.
65ecc953bSthorpej  *
75ecc953bSthorpej  * This software was developed by the Computer Systems Engineering group
85ecc953bSthorpej  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
95ecc953bSthorpej  * contributed to Berkeley.
105ecc953bSthorpej  *
115ecc953bSthorpej  * All advertising materials mentioning features or use of this software
125ecc953bSthorpej  * must display the following acknowledgement:
135ecc953bSthorpej  *	This product includes software developed by the University of
145ecc953bSthorpej  *	California, Lawrence Berkeley Laboratories.
155ecc953bSthorpej  *
165ecc953bSthorpej  * Redistribution and use in source and binary forms, with or without
175ecc953bSthorpej  * modification, are permitted provided that the following conditions
185ecc953bSthorpej  * are met:
195ecc953bSthorpej  * 1. Redistributions of source code must retain the above copyright
205ecc953bSthorpej  *    notice, this list of conditions and the following disclaimer.
215ecc953bSthorpej  * 2. Redistributions in binary form must reproduce the above copyright
225ecc953bSthorpej  *    notice, this list of conditions and the following disclaimer in the
235ecc953bSthorpej  *    documentation and/or other materials provided with the distribution.
245ecc953bSthorpej  * 3. Neither the name of the University nor the names of its contributors
255ecc953bSthorpej  *    may be used to endorse or promote products derived from this software
265ecc953bSthorpej  *    without specific prior written permission.
275ecc953bSthorpej  *
285ecc953bSthorpej  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
295ecc953bSthorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
305ecc953bSthorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
315ecc953bSthorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
325ecc953bSthorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
335ecc953bSthorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
345ecc953bSthorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
355ecc953bSthorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
365ecc953bSthorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
375ecc953bSthorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
385ecc953bSthorpej  * SUCH DAMAGE.
395ecc953bSthorpej  *
405ecc953bSthorpej  *	from: @(#)sem.c	8.1 (Berkeley) 6/6/93
415ecc953bSthorpej  */
425ecc953bSthorpej 
435ecc953bSthorpej #if HAVE_NBTOOL_CONFIG_H
445ecc953bSthorpej #include "nbtool_config.h"
455ecc953bSthorpej #endif
465ecc953bSthorpej 
475ecc953bSthorpej #include <sys/param.h>
485ecc953bSthorpej #include <ctype.h>
495ecc953bSthorpej #include <stdio.h>
505ecc953bSthorpej #include <stdlib.h>
515ecc953bSthorpej #include <string.h>
52d0fb8901Schristos #include <util.h>
535ecc953bSthorpej #include "defs.h"
545ecc953bSthorpej #include "sem.h"
555ecc953bSthorpej 
565ecc953bSthorpej /*
575ecc953bSthorpej  * config semantics.
585ecc953bSthorpej  */
595ecc953bSthorpej 
605ecc953bSthorpej #define	NAMESIZE	100	/* local name buffers */
615ecc953bSthorpej 
625ecc953bSthorpej const char *s_ifnet;		/* magic attribute */
635ecc953bSthorpej const char *s_qmark;
645ecc953bSthorpej const char *s_none;
655ecc953bSthorpej 
665ecc953bSthorpej static struct hashtab *cfhashtab;	/* for config lookup */
675ecc953bSthorpej struct hashtab *devitab;		/* etc */
687c7d91e6Suebayasi struct attr allattr;
695ecc953bSthorpej 
705ecc953bSthorpej static struct attr errattr;
715ecc953bSthorpej static struct devbase errdev;
725ecc953bSthorpej static struct deva errdeva;
735ecc953bSthorpej 
744caf067bSdholland static int has_errobj(struct attrlist *, struct attr *);
755ecc953bSthorpej static struct nvlist *addtoattr(struct nvlist *, struct devbase *);
765ecc953bSthorpej static int resolve(struct nvlist **, const char *, const char *,
775ecc953bSthorpej 		   struct nvlist *, int);
785ecc953bSthorpej static struct pspec *getpspec(struct attr *, struct devbase *, int);
795ecc953bSthorpej static struct devi *newdevi(const char *, int, struct devbase *d);
805ecc953bSthorpej static struct devi *getdevi(const char *);
81c130d400Scube static void remove_devi(struct devi *);
825ecc953bSthorpej static const char *concat(const char *, int);
835ecc953bSthorpej static char *extend(char *, const char *);
845ecc953bSthorpej static int split(const char *, size_t, char *, size_t, int *);
855ecc953bSthorpej static void selectbase(struct devbase *, struct deva *);
8616a8771bSdholland static const char **fixloc(const char *, struct attr *, struct loclist *);
87d767912bSdrochner static const char *makedevstr(devmajor_t, devminor_t);
88d767912bSdrochner static const char *major2name(devmajor_t);
89d767912bSdrochner static devmajor_t dev2major(struct devbase *);
905ecc953bSthorpej 
915ecc953bSthorpej extern const char *yyfile;
927aa6070dScube extern int vflag;
935ecc953bSthorpej 
945ecc953bSthorpej void
955ecc953bSthorpej initsem(void)
965ecc953bSthorpej {
975ecc953bSthorpej 
985ecc953bSthorpej 	attrtab = ht_new();
997c7d91e6Suebayasi 
1007c7d91e6Suebayasi 	allattr.a_name = "netbsd";
1017c7d91e6Suebayasi 	TAILQ_INIT(&allattr.a_files);
1027c7d91e6Suebayasi 	(void)ht_insert(attrtab, allattr.a_name, &allattr);
103cd06fea3Suebayasi 	selectattr(&allattr);
1047c7d91e6Suebayasi 
1055ecc953bSthorpej 	errattr.a_name = "<internal>";
1065ecc953bSthorpej 
1075ecc953bSthorpej 	TAILQ_INIT(&allbases);
1085ecc953bSthorpej 
1095ecc953bSthorpej 	TAILQ_INIT(&alldevas);
1105ecc953bSthorpej 
1115ecc953bSthorpej 	TAILQ_INIT(&allpspecs);
1125ecc953bSthorpej 
1135ecc953bSthorpej 	cfhashtab = ht_new();
1145ecc953bSthorpej 	TAILQ_INIT(&allcf);
1155ecc953bSthorpej 
1165ecc953bSthorpej 	TAILQ_INIT(&alldevi);
1175ecc953bSthorpej 	errdev.d_name = "<internal>";
1185ecc953bSthorpej 
1195ecc953bSthorpej 	TAILQ_INIT(&allpseudo);
1205ecc953bSthorpej 
1215ecc953bSthorpej 	TAILQ_INIT(&alldevms);
1225ecc953bSthorpej 
1235ecc953bSthorpej 	s_ifnet = intern("ifnet");
1245ecc953bSthorpej 	s_qmark = intern("?");
1255ecc953bSthorpej 	s_none = intern("none");
1265ecc953bSthorpej }
1275ecc953bSthorpej 
1285ecc953bSthorpej /* Name of include file just ended (set in scan.l) */
1295ecc953bSthorpej extern const char *lastfile;
1305ecc953bSthorpej 
131*a87832feSuebayasi static struct attr *
132da706167Suebayasi finddep(struct attr *a, const char *name)
133da706167Suebayasi {
134da706167Suebayasi 	struct attrlist *al;
135da706167Suebayasi 
136da706167Suebayasi 	for (al = a->a_deps; al != NULL; al = al->al_next) {
137da706167Suebayasi 		struct attr *this = al->al_this;
138da706167Suebayasi 		if (strcmp(this->a_name, name) == 0)
139da706167Suebayasi 			return this;
140da706167Suebayasi 	}
141da706167Suebayasi 	return NULL;
142da706167Suebayasi }
143da706167Suebayasi 
144da706167Suebayasi static void
145da706167Suebayasi mergedeps(struct devbase *dev, const char *name)
146da706167Suebayasi {
147da706167Suebayasi 	struct attr *a, *newa;
148da706167Suebayasi 
149da706167Suebayasi 	a = refattr(dev->d_name);
150da706167Suebayasi 
151da706167Suebayasi 	CFGDBG(3, "merging attr `%s' in attr `%s'", name, a->a_name);
152da706167Suebayasi 	if (finddep(a, name) == NULL) {
153da706167Suebayasi 		newa = refattr(name);
154da706167Suebayasi 		a->a_deps = attrlist_cons(a->a_deps, newa);
155da706167Suebayasi 		CFGDBG(3, "attr `%s' merged to attr `%s'", newa->a_name,
156da706167Suebayasi 		    a->a_name);
157da706167Suebayasi 	}
158da706167Suebayasi }
159da706167Suebayasi 
160da706167Suebayasi static void
161da706167Suebayasi fixdev(struct devbase *dev)
162da706167Suebayasi {
163da706167Suebayasi 	struct attrlist *al;
164da706167Suebayasi 	struct attr *devattr, *a;
165da706167Suebayasi 
166da706167Suebayasi 	devattr = refattr(dev->d_name);
167da706167Suebayasi 	if (devattr->a_devclass)
168*a87832feSuebayasi 		panic("%s: dev %s is devclass!", __func__, devattr->a_name);
169da706167Suebayasi 
170da706167Suebayasi 	/*
171da706167Suebayasi 	 * For each interface attribute this device refers to, add this
172da706167Suebayasi 	 * device to its reference list.  This makes, e.g., finding all
173da706167Suebayasi 	 * "scsi"s easier.
174da706167Suebayasi 	 *
175da706167Suebayasi 	 * While looking through the attributes, set up the device
176da706167Suebayasi 	 * class if any are devclass attributes (and error out if the
177da706167Suebayasi 	 * device has two classes).
178da706167Suebayasi 	 */
179da706167Suebayasi 	for (al = dev->d_attrs; al != NULL; al = al->al_next) {
180da706167Suebayasi 		a = al->al_this;
181da706167Suebayasi 		if (a->a_iattr) {
182da706167Suebayasi 			a->a_refs = addtoattr(a->a_refs, dev);
183da706167Suebayasi 		} else if (a->a_devclass != NULL) {
184da706167Suebayasi 			if (dev->d_classattr != NULL) {
185da706167Suebayasi 				cfgwarn("device `%s' has multiple classes "
186da706167Suebayasi 				    "(`%s' and `%s')",
187da706167Suebayasi 				    dev->d_name, dev->d_classattr->a_name,
188da706167Suebayasi 				    a->a_name);
189da706167Suebayasi 			}
190da706167Suebayasi 			dev->d_classattr = a;
191da706167Suebayasi 		} else {
192da706167Suebayasi 			if (strcmp(dev->d_name, a->a_name) != 0) {
193da706167Suebayasi 				mergedeps(dev, a->a_name);
194da706167Suebayasi 			}
195da706167Suebayasi 		}
196da706167Suebayasi 	}
197da706167Suebayasi }
198da706167Suebayasi 
1995ecc953bSthorpej void
2005ecc953bSthorpej enddefs(void)
2015ecc953bSthorpej {
2025ecc953bSthorpej 	struct devbase *dev;
2035ecc953bSthorpej 
2045ecc953bSthorpej 	TAILQ_FOREACH(dev, &allbases, d_next) {
2055ecc953bSthorpej 		if (!dev->d_isdef) {
2065ecc953bSthorpej 			(void)fprintf(stderr,
2075ecc953bSthorpej 			    "%s: device `%s' used but not defined\n",
2085ecc953bSthorpej 			    lastfile, dev->d_name);
2095ecc953bSthorpej 			errors++;
2105ecc953bSthorpej 			continue;
2115ecc953bSthorpej 		}
212da706167Suebayasi 		fixdev(dev);
2135ecc953bSthorpej 	}
2145ecc953bSthorpej 	if (errors) {
2155ecc953bSthorpej 		(void)fprintf(stderr, "*** Stop.\n");
2165ecc953bSthorpej 		exit(1);
2175ecc953bSthorpej 	}
2185ecc953bSthorpej }
2195ecc953bSthorpej 
2205ecc953bSthorpej void
2215ecc953bSthorpej setdefmaxusers(int min, int def, int max)
2225ecc953bSthorpej {
2235ecc953bSthorpej 
2245ecc953bSthorpej 	if (min < 1 || min > def || def > max)
225c7295a4cSchristos 		cfgerror("maxusers must have 1 <= min (%d) <= default (%d) "
226c7295a4cSchristos 		    "<= max (%d)", min, def, max);
2275ecc953bSthorpej 	else {
2285ecc953bSthorpej 		minmaxusers = min;
2295ecc953bSthorpej 		defmaxusers = def;
2305ecc953bSthorpej 		maxmaxusers = max;
2315ecc953bSthorpej 	}
2325ecc953bSthorpej }
2335ecc953bSthorpej 
2345ecc953bSthorpej void
2355ecc953bSthorpej setmaxusers(int n)
2365ecc953bSthorpej {
2375ecc953bSthorpej 
238895e5687Scube 	if (maxusers == n) {
239c7295a4cSchristos 		cfgerror("duplicate maxusers parameter");
2405ecc953bSthorpej 		return;
2415ecc953bSthorpej 	}
242895e5687Scube 	if (vflag && maxusers != 0)
243c7295a4cSchristos 		cfgwarn("warning: maxusers already defined");
2445ecc953bSthorpej 	maxusers = n;
2455ecc953bSthorpej 	if (n < minmaxusers) {
246c7295a4cSchristos 		cfgerror("warning: minimum of %d maxusers assumed",
247c7295a4cSchristos 		    minmaxusers);
2485ecc953bSthorpej 		errors--;	/* take it away */
2495ecc953bSthorpej 		maxusers = minmaxusers;
2505ecc953bSthorpej 	} else if (n > maxmaxusers) {
251c7295a4cSchristos 		cfgerror("warning: maxusers (%d) > %d", n, maxmaxusers);
2525ecc953bSthorpej 		errors--;
2535ecc953bSthorpej 	}
2545ecc953bSthorpej }
2555ecc953bSthorpej 
2565ecc953bSthorpej void
2575ecc953bSthorpej setident(const char *i)
2585ecc953bSthorpej {
2595ecc953bSthorpej 
2604a7be1f9Smrg 	if (i)
2615ecc953bSthorpej 		ident = intern(i);
2624a7be1f9Smrg 	else
2634a7be1f9Smrg 		ident = NULL;
2645ecc953bSthorpej }
2655ecc953bSthorpej 
2665ecc953bSthorpej /*
2675ecc953bSthorpej  * Define an attribute, optionally with an interface (a locator list)
2685ecc953bSthorpej  * and a set of attribute-dependencies.
2695ecc953bSthorpej  *
2705ecc953bSthorpej  * Attribute dependencies MAY NOT be interface attributes.
2715ecc953bSthorpej  *
2725ecc953bSthorpej  * Since an empty locator list is logically different from "no interface",
2735ecc953bSthorpej  * all locator lists include a dummy head node, which we discard here.
2745ecc953bSthorpej  */
2755ecc953bSthorpej int
276bb4307a6Suebayasi defattr0(const char *name, struct loclist *locs, struct attrlist *deps,
277bb4307a6Suebayasi     int devclass)
278bb4307a6Suebayasi {
279bb4307a6Suebayasi 
280bb4307a6Suebayasi 	if (locs != NULL)
281bb4307a6Suebayasi 		return defiattr(name, locs, deps, devclass);
282bb4307a6Suebayasi 	else if (devclass)
283bb4307a6Suebayasi 		return defdevclass(name, locs, deps, devclass);
284bb4307a6Suebayasi 	else
285bb4307a6Suebayasi 		return defattr(name, locs, deps, devclass);
286bb4307a6Suebayasi }
287bb4307a6Suebayasi 
288bb4307a6Suebayasi int
28916a8771bSdholland defattr(const char *name, struct loclist *locs, struct attrlist *deps,
2905ecc953bSthorpej     int devclass)
2915ecc953bSthorpej {
2925ecc953bSthorpej 	struct attr *a, *dep;
2934caf067bSdholland 	struct attrlist *al;
2945ecc953bSthorpej 
2955ecc953bSthorpej 	/*
2965ecc953bSthorpej 	 * If this attribute depends on any others, make sure none of
2975ecc953bSthorpej 	 * the dependencies are interface attributes.
2985ecc953bSthorpej 	 */
2994caf067bSdholland 	for (al = deps; al != NULL; al = al->al_next) {
3004caf067bSdholland 		dep = al->al_this;
3015ecc953bSthorpej 		if (dep->a_iattr) {
302c7295a4cSchristos 			cfgerror("`%s' dependency `%s' is an interface "
303c7295a4cSchristos 			    "attribute", name, dep->a_name);
3045ecc953bSthorpej 			return (1);
3055ecc953bSthorpej 		}
3065ecc953bSthorpej 	}
3075ecc953bSthorpej 
308bb52c144Suebayasi 	if (getrefattr(name, &a)) {
309c7295a4cSchristos 		cfgerror("attribute `%s' already defined", name);
31016a8771bSdholland 		loclist_destroy(locs);
3115ecc953bSthorpej 		return (1);
3125ecc953bSthorpej 	}
313bb52c144Suebayasi 	if (a == NULL)
314bb52c144Suebayasi 		a = mkattr(name);
3155ecc953bSthorpej 
316a949e288Suebayasi 	a->a_deps = deps;
317bb4307a6Suebayasi 	expandattr(a, NULL);
3182f2f3382Suebayasi 	CFGDBG(3, "attr `%s' defined", a->a_name);
319bb4307a6Suebayasi 
320bb4307a6Suebayasi 	return (0);
321bb4307a6Suebayasi }
322a949e288Suebayasi 
3232f2f3382Suebayasi struct attr *
3242f2f3382Suebayasi mkattr(const char *name)
3252f2f3382Suebayasi {
3262f2f3382Suebayasi 	struct attr *a;
3272f2f3382Suebayasi 
3282f2f3382Suebayasi 	a = ecalloc(1, sizeof *a);
3292f2f3382Suebayasi 	if (ht_insert(attrtab, name, a)) {
3302f2f3382Suebayasi 		free(a);
3312f2f3382Suebayasi 		return NULL;
3322f2f3382Suebayasi 	}
3332f2f3382Suebayasi 	a->a_name = name;
3342f2f3382Suebayasi 	TAILQ_INIT(&a->a_files);
3352f2f3382Suebayasi 	CFGDBG(3, "attr `%s' allocated", name);
3362f2f3382Suebayasi 
3372f2f3382Suebayasi 	return a;
3382f2f3382Suebayasi }
3392f2f3382Suebayasi 
340a949e288Suebayasi /* "interface attribute" initialization */
341bb4307a6Suebayasi int
342bb4307a6Suebayasi defiattr(const char *name, struct loclist *locs, struct attrlist *deps,
343bb4307a6Suebayasi     int devclass)
344bb4307a6Suebayasi {
345bb4307a6Suebayasi 	struct attr *a;
346bb4307a6Suebayasi 	int len;
347bb4307a6Suebayasi 	struct loclist *ll;
348bb4307a6Suebayasi 
349bb4307a6Suebayasi 	if (devclass)
350bb4307a6Suebayasi 		panic("defattr(%s): locators and devclass", name);
351bb4307a6Suebayasi 
352bb4307a6Suebayasi 	if (defattr(name, locs, deps, devclass) != 0)
353bb4307a6Suebayasi 		return (1);
354bb4307a6Suebayasi 
355bb4307a6Suebayasi 	a = getattr(name);
3565ecc953bSthorpej 	a->a_iattr = 1;
35716a8771bSdholland 	/* unwrap */
35816a8771bSdholland 	a->a_locs = locs->ll_next;
35916a8771bSdholland 	locs->ll_next = NULL;
36016a8771bSdholland 	loclist_destroy(locs);
361a949e288Suebayasi 	len = 0;
362a949e288Suebayasi 	for (ll = a->a_locs; ll != NULL; ll = ll->ll_next)
363a949e288Suebayasi 		len++;
364a949e288Suebayasi 	a->a_loclen = len;
365bb4307a6Suebayasi 	if (deps)
366bb4307a6Suebayasi 		CFGDBG(2, "attr `%s' iface with deps", a->a_name);
367bb4307a6Suebayasi 	return (0);
3685ecc953bSthorpej }
369a949e288Suebayasi 
370a949e288Suebayasi /* "device class" initialization */
371bb4307a6Suebayasi int
372bb4307a6Suebayasi defdevclass(const char *name, struct loclist *locs, struct attrlist *deps,
373bb4307a6Suebayasi     int devclass)
374bb4307a6Suebayasi {
375bb4307a6Suebayasi 	struct attr *a;
376bbfbbde1Schristos 	char classenum[256], *cp;
3775ecc953bSthorpej 	int errored = 0;
3785ecc953bSthorpej 
379bb4307a6Suebayasi 	if (deps)
380bb4307a6Suebayasi 		panic("defattr(%s): dependencies and devclass", name);
381bb4307a6Suebayasi 
382bb4307a6Suebayasi 	if (defattr(name, locs, deps, devclass) != 0)
383bb4307a6Suebayasi 		return (1);
384bb4307a6Suebayasi 
385bb4307a6Suebayasi 	a = getattr(name);
386bbfbbde1Schristos 	(void)snprintf(classenum, sizeof(classenum), "DV_%s", name);
3875ecc953bSthorpej 	for (cp = classenum + 3; *cp; cp++) {
3885ecc953bSthorpej 		if (!errored &&
3895ecc953bSthorpej 		    (!isalnum((unsigned char)*cp) ||
3905ecc953bSthorpej 		      (isalpha((unsigned char)*cp) && !islower((unsigned char)*cp)))) {
391c7295a4cSchristos 			cfgerror("device class names must be "
392c7295a4cSchristos 			    "lower-case alphanumeric characters");
3935ecc953bSthorpej 			errored = 1;
3945ecc953bSthorpej 		}
3955ecc953bSthorpej 		*cp = toupper((unsigned char)*cp);
3965ecc953bSthorpej 	}
3975ecc953bSthorpej 	a->a_devclass = intern(classenum);
3985ecc953bSthorpej 
3995ecc953bSthorpej 	return (0);
4005ecc953bSthorpej }
4015ecc953bSthorpej 
4025ecc953bSthorpej /*
4035ecc953bSthorpej  * Return true if the given `error object' is embedded in the given
4045ecc953bSthorpej  * pointer list.
4055ecc953bSthorpej  */
4065ecc953bSthorpej static int
4074caf067bSdholland has_errobj(struct attrlist *al, struct attr *obj)
4085ecc953bSthorpej {
4095ecc953bSthorpej 
4104caf067bSdholland 	for (; al != NULL; al = al->al_next)
4114caf067bSdholland 		if (al->al_this == obj)
4125ecc953bSthorpej 			return (1);
4135ecc953bSthorpej 	return (0);
4145ecc953bSthorpej }
4155ecc953bSthorpej 
4165ecc953bSthorpej /*
4175ecc953bSthorpej  * Return true if the given attribute is embedded in the given
4185ecc953bSthorpej  * pointer list.
4195ecc953bSthorpej  */
4205ecc953bSthorpej int
4214caf067bSdholland has_attr(struct attrlist *al, const char *attr)
4225ecc953bSthorpej {
4235ecc953bSthorpej 	struct attr *a;
4245ecc953bSthorpej 
4255ecc953bSthorpej 	if ((a = getattr(attr)) == NULL)
4265ecc953bSthorpej 		return (0);
4275ecc953bSthorpej 
4284caf067bSdholland 	for (; al != NULL; al = al->al_next)
4294caf067bSdholland 		if (al->al_this == a)
4305ecc953bSthorpej 			return (1);
4315ecc953bSthorpej 	return (0);
4325ecc953bSthorpej }
4335ecc953bSthorpej 
4345ecc953bSthorpej /*
4355ecc953bSthorpej  * Add a device base to a list in an attribute (actually, to any list).
4365ecc953bSthorpej  * Note that this does not check for duplicates, and does reverse the
4375ecc953bSthorpej  * list order, but no one cares anyway.
4385ecc953bSthorpej  */
4395ecc953bSthorpej static struct nvlist *
4405ecc953bSthorpej addtoattr(struct nvlist *l, struct devbase *dev)
4415ecc953bSthorpej {
4425ecc953bSthorpej 	struct nvlist *n;
4435ecc953bSthorpej 
4445ecc953bSthorpej 	n = newnv(NULL, NULL, dev, 0, l);
4455ecc953bSthorpej 	return (n);
4465ecc953bSthorpej }
4475ecc953bSthorpej 
4485ecc953bSthorpej /*
4495ecc953bSthorpej  * Define a device.  This may (or may not) also define an interface
4505ecc953bSthorpej  * attribute and/or refer to existing attributes.
4515ecc953bSthorpej  */
4525ecc953bSthorpej void
45316a8771bSdholland defdev(struct devbase *dev, struct loclist *loclist, struct attrlist *attrs,
4545ecc953bSthorpej        int ispseudo)
4555ecc953bSthorpej {
45616a8771bSdholland 	struct loclist *ll;
4574caf067bSdholland 	struct attrlist *al;
4585ecc953bSthorpej 
4595ecc953bSthorpej 	if (dev == &errdev)
4605ecc953bSthorpej 		goto bad;
4615ecc953bSthorpej 	if (dev->d_isdef) {
462c7295a4cSchristos 		cfgerror("redefinition of `%s'", dev->d_name);
4635ecc953bSthorpej 		goto bad;
4645ecc953bSthorpej 	}
4655ecc953bSthorpej 
4665ecc953bSthorpej 	dev->d_isdef = 1;
4675ecc953bSthorpej 	if (has_errobj(attrs, &errattr))
4685ecc953bSthorpej 		goto bad;
4695ecc953bSthorpej 
4705ecc953bSthorpej 	/*
4715ecc953bSthorpej 	 * Handle implicit attribute definition from locator list.  Do
4725ecc953bSthorpej 	 * this before scanning the `at' list so that we can have, e.g.:
4735ecc953bSthorpej 	 *	device foo at other, foo { slot = -1 }
4745ecc953bSthorpej 	 * (where you can plug in a foo-bus extender to a foo-bus).
4755ecc953bSthorpej 	 */
4765ecc953bSthorpej 	if (loclist != NULL) {
47716a8771bSdholland 		ll = loclist;
4785ecc953bSthorpej 		loclist = NULL;	/* defattr disposes of them for us */
479bb4307a6Suebayasi 		if (defiattr(dev->d_name, ll, NULL, 0))
4805ecc953bSthorpej 			goto bad;
4814caf067bSdholland 		attrs = attrlist_cons(attrs, getattr(dev->d_name));
4824caf067bSdholland 		/* This used to be stored but was never used */
4834caf067bSdholland 		/* attrs->al_name = dev->d_name; */
484c454f920Scube 	}
485c454f920Scube 
486c130d400Scube 	/*
487c130d400Scube 	 * Pseudo-devices can have children.  Consider them as
488c130d400Scube 	 * attaching at root.
489c130d400Scube 	 */
490c454f920Scube 	if (ispseudo) {
4914caf067bSdholland 		for (al = attrs; al != NULL; al = al->al_next)
4924caf067bSdholland 			if (al->al_this->a_iattr)
493c454f920Scube 				break;
4944caf067bSdholland 		if (al != NULL) {
495b66156c7Sdrochner 			if (ispseudo < 2) {
496b66156c7Sdrochner 				if (version >= 20080610)
497b66156c7Sdrochner 					cfgerror("interface attribute on "
498b66156c7Sdrochner 					 "non-device pseudo `%s'", dev->d_name);
499b66156c7Sdrochner 				else {
500b66156c7Sdrochner 					ispseudo = 2;
501b66156c7Sdrochner 				}
502b66156c7Sdrochner 			}
503c130d400Scube 			ht_insert(devroottab, dev->d_name, dev);
5045ecc953bSthorpej 		}
505b66156c7Sdrochner 	}
5065ecc953bSthorpej 
5075ecc953bSthorpej 	/* Committed!  Set up fields. */
5085ecc953bSthorpej 	dev->d_ispseudo = ispseudo;
5095ecc953bSthorpej 	dev->d_attrs = attrs;
5105ecc953bSthorpej 	dev->d_classattr = NULL;		/* for now */
511da706167Suebayasi 	CFGDBG(3, "dev `%s' defined", dev->d_name);
5125ecc953bSthorpej 
5135ecc953bSthorpej 	/*
514a2dd9676Suebayasi 	 * Implicit attribute definition for device.
515a2dd9676Suebayasi 	 */
516a2dd9676Suebayasi 	refattr(dev->d_name);
517a2dd9676Suebayasi 
518a2dd9676Suebayasi 	/*
5195ecc953bSthorpej 	 * For each interface attribute this device refers to, add this
5205ecc953bSthorpej 	 * device to its reference list.  This makes, e.g., finding all
5215ecc953bSthorpej 	 * "scsi"s easier.
5225ecc953bSthorpej 	 *
5235ecc953bSthorpej 	 * While looking through the attributes, set up the device
5245ecc953bSthorpej 	 * class if any are devclass attributes (and error out if the
5255ecc953bSthorpej 	 * device has two classes).
5265ecc953bSthorpej 	 */
5274caf067bSdholland 	for (al = attrs; al != NULL; al = al->al_next) {
528da706167Suebayasi 		/*
529da706167Suebayasi 		 * Implicit attribute definition for device dependencies.
530da706167Suebayasi 		 */
531da706167Suebayasi 		refattr(dev->d_name);
5325ecc953bSthorpej 	}
5335ecc953bSthorpej 	return;
5345ecc953bSthorpej  bad:
53516a8771bSdholland 	loclist_destroy(loclist);
5364caf067bSdholland 	attrlist_destroyall(attrs);
5375ecc953bSthorpej }
5385ecc953bSthorpej 
5395ecc953bSthorpej /*
5405ecc953bSthorpej  * Look up a devbase.  Also makes sure it is a reasonable name,
5415ecc953bSthorpej  * i.e., does not end in a digit or contain special characters.
5425ecc953bSthorpej  */
5435ecc953bSthorpej struct devbase *
5445ecc953bSthorpej getdevbase(const char *name)
5455ecc953bSthorpej {
546c7295a4cSchristos 	const u_char *p;
5475ecc953bSthorpej 	struct devbase *dev;
5485ecc953bSthorpej 
549c7295a4cSchristos 	p = (const u_char *)name;
5505ecc953bSthorpej 	if (!isalpha(*p))
5515ecc953bSthorpej 		goto badname;
5525ecc953bSthorpej 	while (*++p) {
5535ecc953bSthorpej 		if (!isalnum(*p) && *p != '_')
5545ecc953bSthorpej 			goto badname;
5555ecc953bSthorpej 	}
5565ecc953bSthorpej 	if (isdigit(*--p)) {
5575ecc953bSthorpej  badname:
558c7295a4cSchristos 		cfgerror("bad device base name `%s'", name);
5595ecc953bSthorpej 		return (&errdev);
5605ecc953bSthorpej 	}
5615ecc953bSthorpej 	dev = ht_lookup(devbasetab, name);
5625ecc953bSthorpej 	if (dev == NULL) {
5635ecc953bSthorpej 		dev = ecalloc(1, sizeof *dev);
5645ecc953bSthorpej 		dev->d_name = name;
5655ecc953bSthorpej 		dev->d_isdef = 0;
566d767912bSdrochner 		dev->d_major = NODEVMAJOR;
5675ecc953bSthorpej 		dev->d_attrs = NULL;
5685ecc953bSthorpej 		dev->d_ihead = NULL;
5695ecc953bSthorpej 		dev->d_ipp = &dev->d_ihead;
5705ecc953bSthorpej 		dev->d_ahead = NULL;
5715ecc953bSthorpej 		dev->d_app = &dev->d_ahead;
5725ecc953bSthorpej 		dev->d_umax = 0;
5735ecc953bSthorpej 		TAILQ_INSERT_TAIL(&allbases, dev, d_next);
5745ecc953bSthorpej 		if (ht_insert(devbasetab, name, dev))
5755ecc953bSthorpej 			panic("getdevbase(%s)", name);
576da706167Suebayasi 		CFGDBG(3, "devbase defined `%s'", dev->d_name);
5775ecc953bSthorpej 	}
5785ecc953bSthorpej 	return (dev);
5795ecc953bSthorpej }
5805ecc953bSthorpej 
5815ecc953bSthorpej /*
5825ecc953bSthorpej  * Define some of a device's allowable parent attachments.
5835ecc953bSthorpej  * There may be a list of (plain) attributes.
5845ecc953bSthorpej  */
5855ecc953bSthorpej void
5865ecc953bSthorpej defdevattach(struct deva *deva, struct devbase *dev, struct nvlist *atlist,
5874caf067bSdholland 	     struct attrlist *attrs)
5885ecc953bSthorpej {
5895ecc953bSthorpej 	struct nvlist *nv;
5904caf067bSdholland 	struct attrlist *al;
5915ecc953bSthorpej 	struct attr *a;
5925ecc953bSthorpej 	struct deva *da;
5935ecc953bSthorpej 
5945ecc953bSthorpej 	if (dev == &errdev)
5955ecc953bSthorpej 		goto bad;
5965ecc953bSthorpej 	if (deva == NULL)
5975ecc953bSthorpej 		deva = getdevattach(dev->d_name);
5985ecc953bSthorpej 	if (deva == &errdeva)
5995ecc953bSthorpej 		goto bad;
6005ecc953bSthorpej 	if (!dev->d_isdef) {
601c7295a4cSchristos 		cfgerror("attaching undefined device `%s'", dev->d_name);
6025ecc953bSthorpej 		goto bad;
6035ecc953bSthorpej 	}
6045ecc953bSthorpej 	if (deva->d_isdef) {
605c7295a4cSchristos 		cfgerror("redefinition of `%s'", deva->d_name);
6065ecc953bSthorpej 		goto bad;
6075ecc953bSthorpej 	}
6085ecc953bSthorpej 	if (dev->d_ispseudo) {
609c7295a4cSchristos 		cfgerror("pseudo-devices can't attach");
6105ecc953bSthorpej 		goto bad;
6115ecc953bSthorpej 	}
6125ecc953bSthorpej 
6135ecc953bSthorpej 	deva->d_isdef = 1;
6145ecc953bSthorpej 	if (has_errobj(attrs, &errattr))
6155ecc953bSthorpej 		goto bad;
6164caf067bSdholland 	for (al = attrs; al != NULL; al = al->al_next) {
6174caf067bSdholland 		a = al->al_this;
6185ecc953bSthorpej 		if (a == &errattr)
6195ecc953bSthorpej 			continue;		/* already complained */
6205ecc953bSthorpej 		if (a->a_iattr || a->a_devclass != NULL)
621c7295a4cSchristos 			cfgerror("`%s' is not a plain attribute", a->a_name);
6225ecc953bSthorpej 	}
6235ecc953bSthorpej 
6245ecc953bSthorpej 	/* Committed!  Set up fields. */
6255ecc953bSthorpej 	deva->d_attrs = attrs;
6265ecc953bSthorpej 	deva->d_atlist = atlist;
6275ecc953bSthorpej 	deva->d_devbase = dev;
6282f2f3382Suebayasi 	CFGDBG(3, "deva `%s' defined", deva->d_name);
6292f2f3382Suebayasi 
6302f2f3382Suebayasi 	/*
631a5a68b71Suebayasi 	 * Implicit attribute definition for device attachment.
6322f2f3382Suebayasi 	 */
6332f2f3382Suebayasi 	refattr(deva->d_name);
6345ecc953bSthorpej 
6355ecc953bSthorpej 	/*
6365ecc953bSthorpej 	 * Turn the `at' list into interface attributes (map each
6375ecc953bSthorpej 	 * nv_name to an attribute, or to NULL for root), and add
6385ecc953bSthorpej 	 * this device to those attributes, so that children can
6395ecc953bSthorpej 	 * be listed at this particular device if they are supported
6405ecc953bSthorpej 	 * by that attribute.
6415ecc953bSthorpej 	 */
6425ecc953bSthorpej 	for (nv = atlist; nv != NULL; nv = nv->nv_next) {
6435ecc953bSthorpej 		if (nv->nv_name == NULL)
6445ecc953bSthorpej 			nv->nv_ptr = a = NULL;	/* at root */
6455ecc953bSthorpej 		else
6465ecc953bSthorpej 			nv->nv_ptr = a = getattr(nv->nv_name);
6475ecc953bSthorpej 		if (a == &errattr)
6485ecc953bSthorpej 			continue;		/* already complained */
6495ecc953bSthorpej 
6505ecc953bSthorpej 		/*
6515ecc953bSthorpej 		 * Make sure that an attachment spec doesn't
6525ecc953bSthorpej 		 * already say how to attach to this attribute.
6535ecc953bSthorpej 		 */
6545ecc953bSthorpej 		for (da = dev->d_ahead; da != NULL; da = da->d_bsame)
6555ecc953bSthorpej 			if (onlist(da->d_atlist, a))
656c7295a4cSchristos 				cfgerror("attach at `%s' already done by `%s'",
6575ecc953bSthorpej 				     a ? a->a_name : "root", da->d_name);
6585ecc953bSthorpej 
659c130d400Scube 		if (a == NULL) {
660c130d400Scube 			ht_insert(devroottab, dev->d_name, dev);
6615ecc953bSthorpej 			continue;		/* at root; don't add */
662c130d400Scube 		}
6635ecc953bSthorpej 		if (!a->a_iattr)
664c7295a4cSchristos 			cfgerror("%s cannot be at plain attribute `%s'",
6655ecc953bSthorpej 			    dev->d_name, a->a_name);
6665ecc953bSthorpej 		else
6675ecc953bSthorpej 			a->a_devs = addtoattr(a->a_devs, dev);
6685ecc953bSthorpej 	}
6695ecc953bSthorpej 
6705ecc953bSthorpej 	/* attach to parent */
6715ecc953bSthorpej 	*dev->d_app = deva;
6725ecc953bSthorpej 	dev->d_app = &deva->d_bsame;
6735ecc953bSthorpej 	return;
6745ecc953bSthorpej  bad:
6755ecc953bSthorpej 	nvfreel(atlist);
6764caf067bSdholland 	attrlist_destroyall(attrs);
6775ecc953bSthorpej }
6785ecc953bSthorpej 
6795ecc953bSthorpej /*
6805ecc953bSthorpej  * Look up a device attachment.  Also makes sure it is a reasonable
6815ecc953bSthorpej  * name, i.e., does not contain digits or special characters.
6825ecc953bSthorpej  */
6835ecc953bSthorpej struct deva *
6845ecc953bSthorpej getdevattach(const char *name)
6855ecc953bSthorpej {
686c7295a4cSchristos 	const u_char *p;
6875ecc953bSthorpej 	struct deva *deva;
6885ecc953bSthorpej 
689c7295a4cSchristos 	p = (const u_char *)name;
6905ecc953bSthorpej 	if (!isalpha(*p))
6915ecc953bSthorpej 		goto badname;
6925ecc953bSthorpej 	while (*++p) {
6935ecc953bSthorpej 		if (!isalnum(*p) && *p != '_')
6945ecc953bSthorpej 			goto badname;
6955ecc953bSthorpej 	}
6965ecc953bSthorpej 	if (isdigit(*--p)) {
6975ecc953bSthorpej  badname:
698c7295a4cSchristos 		cfgerror("bad device attachment name `%s'", name);
6995ecc953bSthorpej 		return (&errdeva);
7005ecc953bSthorpej 	}
7015ecc953bSthorpej 	deva = ht_lookup(devatab, name);
7025ecc953bSthorpej 	if (deva == NULL) {
7035ecc953bSthorpej 		deva = ecalloc(1, sizeof *deva);
7045ecc953bSthorpej 		deva->d_name = name;
7055ecc953bSthorpej 		deva->d_bsame = NULL;
7065ecc953bSthorpej 		deva->d_isdef = 0;
7075ecc953bSthorpej 		deva->d_devbase = NULL;
7085ecc953bSthorpej 		deva->d_atlist = NULL;
7095ecc953bSthorpej 		deva->d_attrs = NULL;
7105ecc953bSthorpej 		deva->d_ihead = NULL;
7115ecc953bSthorpej 		deva->d_ipp = &deva->d_ihead;
7125ecc953bSthorpej 		TAILQ_INSERT_TAIL(&alldevas, deva, d_next);
7135ecc953bSthorpej 		if (ht_insert(devatab, name, deva))
7145ecc953bSthorpej 			panic("getdeva(%s)", name);
7155ecc953bSthorpej 	}
7165ecc953bSthorpej 	return (deva);
7175ecc953bSthorpej }
7185ecc953bSthorpej 
7195ecc953bSthorpej /*
7205ecc953bSthorpej  * Look up an attribute.
7215ecc953bSthorpej  */
7225ecc953bSthorpej struct attr *
7235ecc953bSthorpej getattr(const char *name)
7245ecc953bSthorpej {
7255ecc953bSthorpej 	struct attr *a;
7265ecc953bSthorpej 
7275ecc953bSthorpej 	if ((a = ht_lookup(attrtab, name)) == NULL) {
728c7295a4cSchristos 		cfgerror("undefined attribute `%s'", name);
7295ecc953bSthorpej 		a = &errattr;
7305ecc953bSthorpej 	}
7315ecc953bSthorpej 	return (a);
7325ecc953bSthorpej }
7335ecc953bSthorpej 
7345ecc953bSthorpej /*
7352f2f3382Suebayasi  * Implicit attribute definition.
7362f2f3382Suebayasi  */
73751deed3eSuebayasi struct attr *
7382f2f3382Suebayasi refattr(const char *name)
7392f2f3382Suebayasi {
74051deed3eSuebayasi 	struct attr *a;
7412f2f3382Suebayasi 
74251deed3eSuebayasi 	if ((a = ht_lookup(attrtab, name)) == NULL)
74351deed3eSuebayasi 		a = mkattr(name);
74451deed3eSuebayasi 	return a;
7452f2f3382Suebayasi }
7462f2f3382Suebayasi 
747bb52c144Suebayasi int
748bb52c144Suebayasi getrefattr(const char *name, struct attr **ra)
749bb52c144Suebayasi {
750bb52c144Suebayasi 	struct attr *a;
751bb52c144Suebayasi 
752bb52c144Suebayasi 	a = ht_lookup(attrtab, name);
753bb52c144Suebayasi 	if (a == NULL) {
754bb52c144Suebayasi 		*ra = NULL;
755bb52c144Suebayasi 		return (0);
756bb52c144Suebayasi 	}
757bb52c144Suebayasi 	/*
758bb52c144Suebayasi 	 * Check if the existing attr is only referenced, not really defined.
759bb52c144Suebayasi 	 */
760bb52c144Suebayasi 	if (a->a_deps == NULL &&
761bb52c144Suebayasi 	    a->a_iattr == 0 &&
762bb52c144Suebayasi 	    a->a_devclass == 0) {
763bb52c144Suebayasi 		*ra = a;
764bb52c144Suebayasi 		return (0);
765bb52c144Suebayasi 	}
766bb52c144Suebayasi 	return (1);
767bb52c144Suebayasi }
768bb52c144Suebayasi 
7692f2f3382Suebayasi /*
7705ecc953bSthorpej  * Recursively expand an attribute and its dependencies, checking for
7715ecc953bSthorpej  * cycles, and invoking a callback for each attribute found.
7725ecc953bSthorpej  */
7735ecc953bSthorpej void
7745ecc953bSthorpej expandattr(struct attr *a, void (*callback)(struct attr *))
7755ecc953bSthorpej {
7764caf067bSdholland 	struct attrlist *al;
7775ecc953bSthorpej 	struct attr *dep;
7785ecc953bSthorpej 
7795ecc953bSthorpej 	if (a->a_expanding) {
780c7295a4cSchristos 		cfgerror("circular dependency on attribute `%s'", a->a_name);
7815ecc953bSthorpej 		return;
7825ecc953bSthorpej 	}
7835ecc953bSthorpej 
7845ecc953bSthorpej 	a->a_expanding = 1;
7855ecc953bSthorpej 
7865ecc953bSthorpej 	/* First expand all of this attribute's dependencies. */
7874caf067bSdholland 	for (al = a->a_deps; al != NULL; al = al->al_next) {
7884caf067bSdholland 		dep = al->al_this;
7895ecc953bSthorpej 		expandattr(dep, callback);
7905ecc953bSthorpej 	}
7915ecc953bSthorpej 
7925ecc953bSthorpej 	/* ...and now invoke the callback for ourself. */
7935ecc953bSthorpej 	if (callback != NULL)
7945ecc953bSthorpej 		(*callback)(a);
7955ecc953bSthorpej 
7965ecc953bSthorpej 	a->a_expanding = 0;
7975ecc953bSthorpej }
7985ecc953bSthorpej 
7995ecc953bSthorpej /*
8005ecc953bSthorpej  * Set the major device number for a device, so that it can be used
8015ecc953bSthorpej  * as a root/dumps "on" device in a configuration.
8025ecc953bSthorpej  */
8035ecc953bSthorpej void
804d767912bSdrochner setmajor(struct devbase *d, devmajor_t n)
8055ecc953bSthorpej {
8065ecc953bSthorpej 
807d767912bSdrochner 	if (d != &errdev && d->d_major != NODEVMAJOR)
808d767912bSdrochner 		cfgerror("device `%s' is already major %d",
809d767912bSdrochner 		    d->d_name, d->d_major);
8105ecc953bSthorpej 	else
8115ecc953bSthorpej 		d->d_major = n;
8125ecc953bSthorpej }
8135ecc953bSthorpej 
8145ecc953bSthorpej const char *
815d767912bSdrochner major2name(devmajor_t maj)
8165ecc953bSthorpej {
8175ecc953bSthorpej 	struct devbase *dev;
8185ecc953bSthorpej 	struct devm *dm;
8195ecc953bSthorpej 
8205ecc953bSthorpej 	if (!do_devsw) {
8215ecc953bSthorpej 		TAILQ_FOREACH(dev, &allbases, d_next) {
8225ecc953bSthorpej 			if (dev->d_major == maj)
8235ecc953bSthorpej 				return (dev->d_name);
8245ecc953bSthorpej 		}
8255ecc953bSthorpej 	} else {
8265ecc953bSthorpej 		TAILQ_FOREACH(dm, &alldevms, dm_next) {
8275ecc953bSthorpej 			if (dm->dm_bmajor == maj)
8285ecc953bSthorpej 				return (dm->dm_name);
8295ecc953bSthorpej 		}
8305ecc953bSthorpej 	}
8315ecc953bSthorpej 	return (NULL);
8325ecc953bSthorpej }
8335ecc953bSthorpej 
834d767912bSdrochner devmajor_t
8355ecc953bSthorpej dev2major(struct devbase *dev)
8365ecc953bSthorpej {
8375ecc953bSthorpej 	struct devm *dm;
8385ecc953bSthorpej 
8395ecc953bSthorpej 	if (!do_devsw)
8405ecc953bSthorpej 		return (dev->d_major);
8415ecc953bSthorpej 
8425ecc953bSthorpej 	TAILQ_FOREACH(dm, &alldevms, dm_next) {
8435ecc953bSthorpej 		if (strcmp(dm->dm_name, dev->d_name) == 0)
8445ecc953bSthorpej 			return (dm->dm_bmajor);
8455ecc953bSthorpej 	}
846d767912bSdrochner 	return (NODEVMAJOR);
8475ecc953bSthorpej }
8485ecc953bSthorpej 
8495ecc953bSthorpej /*
8505ecc953bSthorpej  * Make a string description of the device at maj/min.
8515ecc953bSthorpej  */
8525ecc953bSthorpej static const char *
853d767912bSdrochner makedevstr(devmajor_t maj, devminor_t min)
8545ecc953bSthorpej {
855c7295a4cSchristos 	const char *devicename;
8565ecc953bSthorpej 	char buf[32];
8575ecc953bSthorpej 
858c7295a4cSchristos 	devicename = major2name(maj);
859c7295a4cSchristos 	if (devicename == NULL)
860d767912bSdrochner 		(void)snprintf(buf, sizeof(buf), "<%d/%d>", maj, min);
8615ecc953bSthorpej 	else
862d767912bSdrochner 		(void)snprintf(buf, sizeof(buf), "%s%d%c", devicename,
863d767912bSdrochner 		    min / maxpartitions, (min % maxpartitions) + 'a');
8645ecc953bSthorpej 
8655ecc953bSthorpej 	return (intern(buf));
8665ecc953bSthorpej }
8675ecc953bSthorpej 
8685ecc953bSthorpej /*
8695ecc953bSthorpej  * Map things like "ra0b" => makedev(major("ra"), 0*maxpartitions + 'b'-'a').
8705ecc953bSthorpej  * Handle the case where the device number is given but there is no
8715ecc953bSthorpej  * corresponding name, and map NULL to the default.
8725ecc953bSthorpej  */
8735ecc953bSthorpej static int
8745ecc953bSthorpej resolve(struct nvlist **nvp, const char *name, const char *what,
8755ecc953bSthorpej 	struct nvlist *dflt, int part)
8765ecc953bSthorpej {
8775ecc953bSthorpej 	struct nvlist *nv;
8785ecc953bSthorpej 	struct devbase *dev;
8795ecc953bSthorpej 	const char *cp;
880d767912bSdrochner 	devmajor_t maj;
881d767912bSdrochner 	devminor_t min;
8820001b928Schristos 	int i, l;
8835ecc953bSthorpej 	int unit;
8845ecc953bSthorpej 	char buf[NAMESIZE];
8855ecc953bSthorpej 
8865cc303e1Slukem 	if ((part -= 'a') >= maxpartitions || part < 0)
8875ecc953bSthorpej 		panic("resolve");
8885ecc953bSthorpej 	if ((nv = *nvp) == NULL) {
8895ecc953bSthorpej 		dev_t	d = NODEV;
8905ecc953bSthorpej 		/*
8915ecc953bSthorpej 		 * Apply default.  Easiest to do this by number.
8925ecc953bSthorpej 		 * Make sure to retain NODEVness, if this is dflt's disposition.
8935ecc953bSthorpej 		 */
8945cc303e1Slukem 		if ((dev_t)dflt->nv_num != NODEV) {
8950001b928Schristos 			maj = major(dflt->nv_num);
8960001b928Schristos 			min = ((minor(dflt->nv_num) / maxpartitions) *
8975ecc953bSthorpej 			    maxpartitions) + part;
8985ecc953bSthorpej 			d = makedev(maj, min);
8995ecc953bSthorpej 			cp = makedevstr(maj, min);
9005ecc953bSthorpej 		} else
9015ecc953bSthorpej 			cp = NULL;
9025ecc953bSthorpej 		*nvp = nv = newnv(NULL, cp, NULL, d, NULL);
9035ecc953bSthorpej 	}
9045cc303e1Slukem 	if ((dev_t)nv->nv_num != NODEV) {
9055ecc953bSthorpej 		/*
9065ecc953bSthorpej 		 * By the numbers.  Find the appropriate major number
9075ecc953bSthorpej 		 * to make a name.
9085ecc953bSthorpej 		 */
9090001b928Schristos 		maj = major(nv->nv_num);
9100001b928Schristos 		min = minor(nv->nv_num);
9115ecc953bSthorpej 		nv->nv_str = makedevstr(maj, min);
9125ecc953bSthorpej 		return (0);
9135ecc953bSthorpej 	}
9145ecc953bSthorpej 
9155ecc953bSthorpej 	if (nv->nv_str == NULL || nv->nv_str == s_qmark)
9165ecc953bSthorpej 		/*
9175ecc953bSthorpej 		 * Wildcarded or unspecified; leave it as NODEV.
9185ecc953bSthorpej 		 */
9195ecc953bSthorpej 		return (0);
9205ecc953bSthorpej 
9215ecc953bSthorpej 	/*
9225ecc953bSthorpej 	 * The normal case: things like "ra2b".  Check for partition
9235ecc953bSthorpej 	 * suffix, remove it if there, and split into name ("ra") and
9245ecc953bSthorpej 	 * unit (2).
9255ecc953bSthorpej 	 */
9265ecc953bSthorpej 	l = i = strlen(nv->nv_str);
9275ecc953bSthorpej 	cp = &nv->nv_str[l];
9285ecc953bSthorpej 	if (l > 1 && *--cp >= 'a' && *cp < 'a' + maxpartitions &&
9295ecc953bSthorpej 	    isdigit((unsigned char)cp[-1])) {
9305ecc953bSthorpej 		l--;
9315ecc953bSthorpej 		part = *cp - 'a';
9325ecc953bSthorpej 	}
9335ecc953bSthorpej 	cp = nv->nv_str;
9345ecc953bSthorpej 	if (split(cp, l, buf, sizeof buf, &unit)) {
935c7295a4cSchristos 		cfgerror("%s: invalid %s device name `%s'", name, what, cp);
9365ecc953bSthorpej 		return (1);
9375ecc953bSthorpej 	}
9385ecc953bSthorpej 	dev = ht_lookup(devbasetab, intern(buf));
9395ecc953bSthorpej 	if (dev == NULL) {
940c7295a4cSchristos 		cfgerror("%s: device `%s' does not exist", name, buf);
9415ecc953bSthorpej 		return (1);
9425ecc953bSthorpej 	}
9435ecc953bSthorpej 
9445ecc953bSthorpej 	/*
9455ecc953bSthorpej 	 * Check for the magic network interface attribute, and
9465ecc953bSthorpej 	 * don't bother making a device number.
9475ecc953bSthorpej 	 */
9485ecc953bSthorpej 	if (has_attr(dev->d_attrs, s_ifnet)) {
9490001b928Schristos 		nv->nv_num = NODEV;
9505ecc953bSthorpej 		nv->nv_ifunit = unit;	/* XXX XXX XXX */
9515ecc953bSthorpej 	} else {
9525ecc953bSthorpej 		maj = dev2major(dev);
9535cc303e1Slukem 		if (maj == NODEVMAJOR) {
954c7295a4cSchristos 			cfgerror("%s: can't make %s device from `%s'",
9555ecc953bSthorpej 			    name, what, nv->nv_str);
9565ecc953bSthorpej 			return (1);
9575ecc953bSthorpej 		}
9580001b928Schristos 		nv->nv_num = makedev(maj, unit * maxpartitions + part);
9595ecc953bSthorpej 	}
9605ecc953bSthorpej 
9615ecc953bSthorpej 	nv->nv_name = dev->d_name;
9625ecc953bSthorpej 	return (0);
9635ecc953bSthorpej }
9645ecc953bSthorpej 
9655ecc953bSthorpej /*
9665ecc953bSthorpej  * Add a completed configuration to the list.
9675ecc953bSthorpej  */
9685ecc953bSthorpej void
9695ecc953bSthorpej addconf(struct config *cf0)
9705ecc953bSthorpej {
9715ecc953bSthorpej 	struct config *cf;
9725ecc953bSthorpej 	const char *name;
9735ecc953bSthorpej 
9745ecc953bSthorpej 	name = cf0->cf_name;
9755ecc953bSthorpej 	cf = ecalloc(1, sizeof *cf);
9765ecc953bSthorpej 	if (ht_insert(cfhashtab, name, cf)) {
977c7295a4cSchristos 		cfgerror("configuration `%s' already defined", name);
9785ecc953bSthorpej 		free(cf);
9795ecc953bSthorpej 		goto bad;
9805ecc953bSthorpej 	}
9815ecc953bSthorpej 	*cf = *cf0;
9825ecc953bSthorpej 
9835ecc953bSthorpej 	/*
9845ecc953bSthorpej 	 * Resolve the root device.
9855ecc953bSthorpej 	 */
986cd429362Serh 	if (cf->cf_root == NULL) {
987c7295a4cSchristos 		cfgerror("%s: no root device specified", name);
9885ecc953bSthorpej 		goto bad;
9895ecc953bSthorpej 	}
990cd429362Serh 	if (cf->cf_root && cf->cf_root->nv_str != s_qmark) {
991cd429362Serh 		struct nvlist *nv;
992cd429362Serh 		nv = cf->cf_root;
9935ecc953bSthorpej 		if (resolve(&cf->cf_root, name, "root", nv, 'a'))
9945ecc953bSthorpej 			goto bad;
9955ecc953bSthorpej 	}
9965ecc953bSthorpej 
9975ecc953bSthorpej 	/*
9985ecc953bSthorpej 	 * Resolve the dump device.
9995ecc953bSthorpej 	 */
10005ecc953bSthorpej 	if (cf->cf_dump == NULL || cf->cf_dump->nv_str == s_qmark) {
10015ecc953bSthorpej 		/*
10025ecc953bSthorpej 		 * Wildcarded dump device is equivalent to unspecified.
10035ecc953bSthorpej 		 */
10045ecc953bSthorpej 		cf->cf_dump = NULL;
10055ecc953bSthorpej 	} else if (cf->cf_dump->nv_str == s_none) {
10065ecc953bSthorpej 		/*
10075ecc953bSthorpej 		 * Operator has requested that no dump device should be
10085ecc953bSthorpej 		 * configured; do nothing.
10095ecc953bSthorpej 		 */
10105ecc953bSthorpej 	} else {
10115ecc953bSthorpej 		if (resolve(&cf->cf_dump, name, "dumps", cf->cf_dump, 'b'))
10125ecc953bSthorpej 			goto bad;
10135ecc953bSthorpej 	}
10145ecc953bSthorpej 
10155ecc953bSthorpej 	/* Wildcarded fstype is `unspecified'. */
10165ecc953bSthorpej 	if (cf->cf_fstype == s_qmark)
10175ecc953bSthorpej 		cf->cf_fstype = NULL;
10185ecc953bSthorpej 
10195ecc953bSthorpej 	TAILQ_INSERT_TAIL(&allcf, cf, cf_next);
10205ecc953bSthorpej 	return;
10215ecc953bSthorpej  bad:
10225ecc953bSthorpej 	nvfreel(cf0->cf_root);
10235ecc953bSthorpej 	nvfreel(cf0->cf_dump);
10245ecc953bSthorpej }
10255ecc953bSthorpej 
10265ecc953bSthorpej void
10275ecc953bSthorpej setconf(struct nvlist **npp, const char *what, struct nvlist *v)
10285ecc953bSthorpej {
10295ecc953bSthorpej 
10305ecc953bSthorpej 	if (*npp != NULL) {
1031c7295a4cSchristos 		cfgerror("duplicate %s specification", what);
10325ecc953bSthorpej 		nvfreel(v);
10335ecc953bSthorpej 	} else
10345ecc953bSthorpej 		*npp = v;
10355ecc953bSthorpej }
10365ecc953bSthorpej 
10375ecc953bSthorpej void
103842b52b8aScube delconf(const char *name)
103942b52b8aScube {
104042b52b8aScube 	struct config *cf;
104142b52b8aScube 
104242b52b8aScube 	if (ht_lookup(cfhashtab, name) == NULL) {
1043c7295a4cSchristos 		cfgerror("configuration `%s' undefined", name);
104442b52b8aScube 		return;
104542b52b8aScube 	}
104642b52b8aScube 	(void)ht_remove(cfhashtab, name);
104742b52b8aScube 
104842b52b8aScube 	TAILQ_FOREACH(cf, &allcf, cf_next)
104942b52b8aScube 		if (!strcmp(cf->cf_name, name))
105042b52b8aScube 			break;
105142b52b8aScube 	if (cf == NULL)
105242b52b8aScube 		panic("lost configuration `%s'", name);
105342b52b8aScube 
105442b52b8aScube 	TAILQ_REMOVE(&allcf, cf, cf_next);
105542b52b8aScube }
105642b52b8aScube 
105742b52b8aScube void
10585ecc953bSthorpej setfstype(const char **fstp, const char *v)
10595ecc953bSthorpej {
10605ecc953bSthorpej 
10615ecc953bSthorpej 	if (*fstp != NULL) {
1062c7295a4cSchristos 		cfgerror("multiple fstype specifications");
10635ecc953bSthorpej 		return;
10645ecc953bSthorpej 	}
10655ecc953bSthorpej 
10665ecc953bSthorpej 	if (v != s_qmark && OPT_FSOPT(v)) {
1067c7295a4cSchristos 		cfgerror("\"%s\" is not a configured file system", v);
10685ecc953bSthorpej 		return;
10695ecc953bSthorpej 	}
10705ecc953bSthorpej 
10715ecc953bSthorpej 	*fstp = v;
10725ecc953bSthorpej }
10735ecc953bSthorpej 
10745ecc953bSthorpej static struct devi *
10755ecc953bSthorpej newdevi(const char *name, int unit, struct devbase *d)
10765ecc953bSthorpej {
10775ecc953bSthorpej 	struct devi *i;
10785ecc953bSthorpej 
10795ecc953bSthorpej 	i = ecalloc(1, sizeof *i);
10805ecc953bSthorpej 	i->i_name = name;
10815ecc953bSthorpej 	i->i_unit = unit;
10825ecc953bSthorpej 	i->i_base = d;
10835ecc953bSthorpej 	i->i_bsame = NULL;
10845ecc953bSthorpej 	i->i_asame = NULL;
10855ecc953bSthorpej 	i->i_alias = NULL;
10865ecc953bSthorpej 	i->i_at = NULL;
10875ecc953bSthorpej 	i->i_pspec = NULL;
10885ecc953bSthorpej 	i->i_atdeva = NULL;
10895ecc953bSthorpej 	i->i_locs = NULL;
10905ecc953bSthorpej 	i->i_cfflags = 0;
10915ecc953bSthorpej 	i->i_lineno = currentline();
10920dbd1c0eScube 	i->i_srcfile = yyfile;
10937aa6070dScube 	i->i_active = DEVI_ORPHAN; /* Proper analysis comes later */
1094a31ff6b4Scube 	i->i_level = devilevel;
109590ac64deSpooka 	i->i_pseudoroot = 0;
10965ecc953bSthorpej 	if (unit >= d->d_umax)
10975ecc953bSthorpej 		d->d_umax = unit + 1;
10985ecc953bSthorpej 	return (i);
10995ecc953bSthorpej }
11005ecc953bSthorpej 
11015ecc953bSthorpej /*
11025ecc953bSthorpej  * Add the named device as attaching to the named attribute (or perhaps
11035ecc953bSthorpej  * another device instead) plus unit number.
11045ecc953bSthorpej  */
11055ecc953bSthorpej void
110616a8771bSdholland adddev(const char *name, const char *at, struct loclist *loclist, int flags)
11075ecc953bSthorpej {
11085ecc953bSthorpej 	struct devi *i;		/* the new instance */
11095ecc953bSthorpej 	struct pspec *p;	/* and its pspec */
11105ecc953bSthorpej 	struct attr *attr;	/* attribute that allows attach */
11115ecc953bSthorpej 	struct devbase *ib;	/* i->i_base */
11125ecc953bSthorpej 	struct devbase *ab;	/* not NULL => at another dev */
11134caf067bSdholland 	struct attrlist *al;
11145ecc953bSthorpej 	struct deva *iba;	/* devbase attachment used */
11155ecc953bSthorpej 	const char *cp;
11165ecc953bSthorpej 	int atunit;
11175ecc953bSthorpej 	char atbuf[NAMESIZE];
11185ecc953bSthorpej 	int hit;
11195ecc953bSthorpej 
11205ecc953bSthorpej 	ab = NULL;
11215ecc953bSthorpej 	iba = NULL;
11225ecc953bSthorpej 	if (at == NULL) {
11235ecc953bSthorpej 		/* "at root" */
11245ecc953bSthorpej 		p = NULL;
11255ecc953bSthorpej 		if ((i = getdevi(name)) == NULL)
11265ecc953bSthorpej 			goto bad;
11275ecc953bSthorpej 		/*
11285ecc953bSthorpej 		 * Must warn about i_unit > 0 later, after taking care of
11295ecc953bSthorpej 		 * the STAR cases (we could do non-star's here but why
11305ecc953bSthorpej 		 * bother?).  Make sure this device can be at root.
11315ecc953bSthorpej 		 */
11325ecc953bSthorpej 		ib = i->i_base;
11335ecc953bSthorpej 		hit = 0;
11345ecc953bSthorpej 		for (iba = ib->d_ahead; iba != NULL; iba = iba->d_bsame)
11355ecc953bSthorpej 			if (onlist(iba->d_atlist, NULL)) {
11365ecc953bSthorpej 				hit = 1;
11375ecc953bSthorpej 				break;
11385ecc953bSthorpej 			}
11395ecc953bSthorpej 		if (!hit) {
1140c7295a4cSchristos 			cfgerror("`%s' cannot attach to the root", ib->d_name);
11415ec393e9Scube 			i->i_active = DEVI_BROKEN;
11425ecc953bSthorpej 			goto bad;
11435ecc953bSthorpej 		}
11445ecc953bSthorpej 		attr = &errattr;	/* a convenient "empty" attr */
11455ecc953bSthorpej 	} else {
11465ecc953bSthorpej 		if (split(at, strlen(at), atbuf, sizeof atbuf, &atunit)) {
1147c7295a4cSchristos 			cfgerror("invalid attachment name `%s'", at);
11485ecc953bSthorpej 			/* (void)getdevi(name); -- ??? */
11495ecc953bSthorpej 			goto bad;
11505ecc953bSthorpej 		}
11515ecc953bSthorpej 		if ((i = getdevi(name)) == NULL)
11525ecc953bSthorpej 			goto bad;
11535ecc953bSthorpej 		ib = i->i_base;
11545ecc953bSthorpej 
11555ecc953bSthorpej 		/*
11565ecc953bSthorpej 		 * Devices can attach to two types of things: Attributes,
11575ecc953bSthorpej 		 * and other devices (which have the appropriate attributes
11585ecc953bSthorpej 		 * to allow attachment).
11595ecc953bSthorpej 		 *
11605ecc953bSthorpej 		 * (1) If we're attached to an attribute, then we don't need
11615ecc953bSthorpej 		 *     look at the parent base device to see what attributes
11625ecc953bSthorpej 		 *     it has, and make sure that we can attach to them.
11635ecc953bSthorpej 		 *
11645ecc953bSthorpej 		 * (2) If we're attached to a real device (i.e. named in
11655ecc953bSthorpej 		 *     the config file), we want to remember that so that
11665ecc953bSthorpej 		 *     at cross-check time, if the device we're attached to
11675ecc953bSthorpej 		 *     is missing but other devices which also provide the
11685ecc953bSthorpej 		 *     attribute are present, we don't get a false "OK."
11695ecc953bSthorpej 		 *
11705ecc953bSthorpej 		 * (3) If the thing we're attached to is an attribute
11715ecc953bSthorpej 		 *     but is actually named in the config file, we still
11725ecc953bSthorpej 		 *     have to remember its devbase.
11735ecc953bSthorpej 		 */
11745ecc953bSthorpej 		cp = intern(atbuf);
11755ecc953bSthorpej 
11765ecc953bSthorpej 		/* Figure out parent's devbase, to satisfy case (3). */
11775ecc953bSthorpej 		ab = ht_lookup(devbasetab, cp);
11785ecc953bSthorpej 
11795ecc953bSthorpej 		/* Find out if it's an attribute. */
11805ecc953bSthorpej 		attr = ht_lookup(attrtab, cp);
11815ecc953bSthorpej 
11825ecc953bSthorpej 		/* Make sure we're _really_ attached to the attr.  Case (1). */
11835ecc953bSthorpej 		if (attr != NULL && onlist(attr->a_devs, ib))
11845ecc953bSthorpej 			goto findattachment;
11855ecc953bSthorpej 
11865ecc953bSthorpej 		/*
11875ecc953bSthorpej 		 * Else a real device, and not just an attribute.  Case (2).
11885ecc953bSthorpej 		 *
11895ecc953bSthorpej 		 * Have to work a bit harder to see whether we have
11905ecc953bSthorpej 		 * something like "tg0 at esp0" (where esp is merely
11915ecc953bSthorpej 		 * not an attribute) or "tg0 at nonesuch0" (where
11925ecc953bSthorpej 		 * nonesuch is not even a device).
11935ecc953bSthorpej 		 */
11945ecc953bSthorpej 		if (ab == NULL) {
1195c7295a4cSchristos 			cfgerror("%s at %s: `%s' unknown",
11965ecc953bSthorpej 			    name, at, atbuf);
11975ec393e9Scube 			i->i_active = DEVI_BROKEN;
11985ecc953bSthorpej 			goto bad;
11995ecc953bSthorpej 		}
12005ecc953bSthorpej 
12015ecc953bSthorpej 		/*
12025ecc953bSthorpej 		 * See if the named parent carries an attribute
12035ecc953bSthorpej 		 * that allows it to supervise device ib.
12045ecc953bSthorpej 		 */
12054caf067bSdholland 		for (al = ab->d_attrs; al != NULL; al = al->al_next) {
12064caf067bSdholland 			attr = al->al_this;
12075ecc953bSthorpej 			if (onlist(attr->a_devs, ib))
12085ecc953bSthorpej 				goto findattachment;
12095ecc953bSthorpej 		}
1210c7295a4cSchristos 		cfgerror("`%s' cannot attach to `%s'", ib->d_name, atbuf);
12115ec393e9Scube 		i->i_active = DEVI_BROKEN;
12125ecc953bSthorpej 		goto bad;
12135ecc953bSthorpej 
12145ecc953bSthorpej  findattachment:
12155ecc953bSthorpej 		/*
12165ecc953bSthorpej 		 * Find the parent spec.  If a matching one has not yet been
12175ecc953bSthorpej 		 * created, create one.
12185ecc953bSthorpej 		 */
12195ecc953bSthorpej 		p = getpspec(attr, ab, atunit);
12205ecc953bSthorpej 		p->p_devs = newnv(NULL, NULL, i, 0, p->p_devs);
12215ecc953bSthorpej 
12225ecc953bSthorpej 		/* find out which attachment it uses */
12235ecc953bSthorpej 		hit = 0;
12245ecc953bSthorpej 		for (iba = ib->d_ahead; iba != NULL; iba = iba->d_bsame)
12255ecc953bSthorpej 			if (onlist(iba->d_atlist, attr)) {
12265ecc953bSthorpej 				hit = 1;
12275ecc953bSthorpej 				break;
12285ecc953bSthorpej 			}
12295ecc953bSthorpej 		if (!hit)
12305ecc953bSthorpej 			panic("adddev: can't figure out attachment");
12315ecc953bSthorpej 	}
12325ec393e9Scube 	if ((i->i_locs = fixloc(name, attr, loclist)) == NULL) {
12335ec393e9Scube 		i->i_active = DEVI_BROKEN;
12345ecc953bSthorpej 		goto bad;
12355ec393e9Scube 	}
12365ecc953bSthorpej 	i->i_at = at;
12375ecc953bSthorpej 	i->i_pspec = p;
12385ecc953bSthorpej 	i->i_atdeva = iba;
12395ecc953bSthorpej 	i->i_cfflags = flags;
12405ecc953bSthorpej 
12415ecc953bSthorpej 	*iba->d_ipp = i;
12425ecc953bSthorpej 	iba->d_ipp = &i->i_asame;
12435ecc953bSthorpej 
12445ecc953bSthorpej 	/* all done, fall into ... */
12455ecc953bSthorpej  bad:
124616a8771bSdholland 	loclist_destroy(loclist);
12475ecc953bSthorpej 	return;
12485ecc953bSthorpej }
12495ecc953bSthorpej 
12505ecc953bSthorpej void
12517b7c582aScube deldevi(const char *name, const char *at)
12525ecc953bSthorpej {
12537b7c582aScube 	struct devi *firsti, *i;
12545ecc953bSthorpej 	struct devbase *d;
12555ecc953bSthorpej 	int unit;
12565ecc953bSthorpej 	char base[NAMESIZE];
12575ecc953bSthorpej 
12585ecc953bSthorpej 	if (split(name, strlen(name), base, sizeof base, &unit)) {
1259c7295a4cSchristos 		cfgerror("invalid device name `%s'", name);
12605ecc953bSthorpej 		return;
12615ecc953bSthorpej 	}
12625ecc953bSthorpej 	d = ht_lookup(devbasetab, intern(base));
12635ecc953bSthorpej 	if (d == NULL) {
1264c7295a4cSchristos 		cfgerror("%s: unknown device `%s'", name, base);
12655ecc953bSthorpej 		return;
12665ecc953bSthorpej 	}
12675ecc953bSthorpej 	if (d->d_ispseudo) {
1268c7295a4cSchristos 		cfgerror("%s: %s is a pseudo-device", name, base);
12695ecc953bSthorpej 		return;
12705ecc953bSthorpej 	}
12715ecc953bSthorpej 	if ((firsti = ht_lookup(devitab, name)) == NULL) {
1272c7295a4cSchristos 		cfgerror("`%s' not defined", name);
12735ecc953bSthorpej 		return;
12745ecc953bSthorpej 	}
12757b7c582aScube 	if (at == NULL && firsti->i_at == NULL) {
1276e499d8b5Scube 		/* 'at root' */
12777b7c582aScube 		remove_devi(firsti);
12787b7c582aScube 		return;
12797b7c582aScube 	} else if (at != NULL)
12807b7c582aScube 		for (i = firsti; i != NULL; i = i->i_alias)
1281bfc250f2Scube 			if (i->i_active != DEVI_BROKEN &&
1282bfc250f2Scube 			    strcmp(at, i->i_at) == 0) {
12837b7c582aScube 				remove_devi(i);
12845ecc953bSthorpej 				return;
12855ecc953bSthorpej 			}
1286c7295a4cSchristos 	cfgerror("`%s' at `%s' not found", name, at ? at : "root");
1287e499d8b5Scube }
12885ecc953bSthorpej 
12897b7c582aScube static void
12907b7c582aScube remove_devi(struct devi *i)
12917b7c582aScube {
12927b7c582aScube 	struct devbase *d = i->i_base;
12937b7c582aScube 	struct devi *f, *j, **ppi;
12947b7c582aScube 	struct deva *iba;
12957b7c582aScube 
12967b7c582aScube 	f = ht_lookup(devitab, i->i_name);
12976a6299ebScube 	if (f == NULL)
12986a6299ebScube 		panic("remove_devi(): instance %s disappeared from devitab",
12996a6299ebScube 		    i->i_name);
13007b7c582aScube 
1301bfc250f2Scube 	if (i->i_active == DEVI_BROKEN) {
1302bfc250f2Scube 		cfgerror("not removing broken instance `%s'", i->i_name);
1303bfc250f2Scube 		return;
1304bfc250f2Scube 	}
1305bfc250f2Scube 
1306e499d8b5Scube 	/*
1307e499d8b5Scube 	 * We have the device instance, i.
1308e499d8b5Scube 	 * We have to:
1309e499d8b5Scube 	 *   - delete the alias
1310e499d8b5Scube 	 *
1311e499d8b5Scube 	 *      If the devi was an alias of an already listed devi, all is
1312e499d8b5Scube 	 *      good we don't have to do more.
1313e499d8b5Scube 	 *      If it was the first alias, we have to replace i's entry in
1314e499d8b5Scube 	 *      d's list by its first alias.
1315e499d8b5Scube 	 *      If it was the only entry, we must remove i's entry from d's
1316e499d8b5Scube 	 *      list.
1317e499d8b5Scube 	 */
13187b7c582aScube 	if (i != f) {
13197b7c582aScube 		for (j = f; j->i_alias != i; j = j->i_alias);
13207b7c582aScube 		j->i_alias = i->i_alias;
13213b405775Scube 	} else {
13223b405775Scube 		if (i->i_alias == NULL) {
1323e499d8b5Scube 			/* No alias, must unlink the entry from devitab */
13247b7c582aScube 			ht_remove(devitab, i->i_name);
13257b7c582aScube 			j = i->i_bsame;
13263b405775Scube 		} else {
1327e499d8b5Scube 			/* Or have the first alias replace i in d's list */
1328e499d8b5Scube 			i->i_alias->i_bsame = i->i_bsame;
13297b7c582aScube 			j = i->i_alias;
13307b7c582aScube 			if (i == f)
13317b7c582aScube 				ht_replace(devitab, i->i_name, i->i_alias);
13323b405775Scube 		}
13333b405775Scube 
1334e499d8b5Scube 		/*
1335e499d8b5Scube 		 *   - remove/replace the instance from the devbase's list
1336e499d8b5Scube 		 *
1337e499d8b5Scube 		 * A double-linked list would make this much easier.  Oh, well,
1338e499d8b5Scube 		 * what is done is done.
1339e499d8b5Scube 		 */
1340e499d8b5Scube 		for (ppi = &d->d_ihead;
1341e499d8b5Scube 		    *ppi != NULL && *ppi != i && (*ppi)->i_bsame != i;
1342e499d8b5Scube 		    ppi = &(*ppi)->i_bsame);
1343e499d8b5Scube 		if (*ppi == NULL)
1344c3414672Scube 			panic("deldev: dev (%s) doesn't list the devi"
1345c3414672Scube 			    " (%s at %s)", d->d_name, i->i_name, i->i_at);
13467b7c582aScube 		f = *ppi;
13477b7c582aScube 		if (f == i)
1348c3414672Scube 			/* That implies d->d_ihead == i */
13497b7c582aScube 			*ppi = j;
1350e499d8b5Scube 		else
13517b7c582aScube 			(*ppi)->i_bsame = j;
1352e499d8b5Scube 		if (d->d_ipp == &i->i_bsame) {
13533b405775Scube 			if (i->i_alias == NULL) {
13547b7c582aScube 				if (f == i)
1355e499d8b5Scube 					d->d_ipp = &d->d_ihead;
1356e499d8b5Scube 				else
13577b7c582aScube 					d->d_ipp = &f->i_bsame;
13583b405775Scube 			} else
13593b405775Scube 				d->d_ipp = &i->i_alias->i_bsame;
1360e499d8b5Scube 		}
1361e499d8b5Scube 	}
1362e499d8b5Scube 	/*
1363e499d8b5Scube 	 *   - delete the attachment instance
1364e499d8b5Scube 	 */
1365e499d8b5Scube 	iba = i->i_atdeva;
1366e499d8b5Scube 	for (ppi = &iba->d_ihead;
1367e499d8b5Scube 	    *ppi != NULL && *ppi != i && (*ppi)->i_asame != i;
1368e499d8b5Scube 	    ppi = &(*ppi)->i_asame);
1369e499d8b5Scube 	if (*ppi == NULL)
1370e499d8b5Scube 		panic("deldev: deva (%s) doesn't list the devi (%s)",
1371e499d8b5Scube 		    iba->d_name, i->i_name);
13727b7c582aScube 	f = *ppi;
13737b7c582aScube 	if (f == i)
1374c3414672Scube 		/* That implies iba->d_ihead == i */
1375e499d8b5Scube 		*ppi = i->i_asame;
1376e499d8b5Scube 	else
1377e499d8b5Scube 		(*ppi)->i_asame = i->i_asame;
1378e499d8b5Scube 	if (iba->d_ipp == &i->i_asame) {
13797b7c582aScube 		if (f == i)
1380e499d8b5Scube 			iba->d_ipp = &iba->d_ihead;
1381e499d8b5Scube 		else
13827b7c582aScube 			iba->d_ipp = &f->i_asame;
1383e499d8b5Scube 	}
1384e499d8b5Scube 	/*
1385e499d8b5Scube 	 *   - delete the pspec
1386e499d8b5Scube 	 */
1387e499d8b5Scube 	if (i->i_pspec) {
1388e499d8b5Scube 		struct pspec *p = i->i_pspec;
1389e499d8b5Scube 		struct nvlist *nv, *onv;
1390e499d8b5Scube 
1391e499d8b5Scube 		/* Double-linked nvlist anyone? */
1392c0024066Scube 		for (nv = p->p_devs; nv->nv_next != NULL; nv = nv->nv_next) {
1393e499d8b5Scube 			if (nv->nv_next && nv->nv_next->nv_ptr == i) {
1394e499d8b5Scube 				onv = nv->nv_next;
1395e499d8b5Scube 				nv->nv_next = onv->nv_next;
1396e499d8b5Scube 				nvfree(onv);
1397e499d8b5Scube 				break;
1398c0024066Scube 			}
1399c0024066Scube 			if (nv->nv_ptr == i) {
1400e499d8b5Scube 				/* nv is p->p_devs in that case */
1401e499d8b5Scube 				p->p_devs = nv->nv_next;
1402e499d8b5Scube 				nvfree(nv);
1403e499d8b5Scube 				break;
1404e499d8b5Scube 			}
1405e499d8b5Scube 		}
1406e499d8b5Scube 		if (p->p_devs == NULL)
1407e499d8b5Scube 			TAILQ_REMOVE(&allpspecs, p, p_list);
1408e499d8b5Scube 	}
1409e499d8b5Scube 	/*
1410e499d8b5Scube 	 *   - delete the alldevi entry
1411e499d8b5Scube 	 */
1412e499d8b5Scube 	TAILQ_REMOVE(&alldevi, i, i_next);
1413e499d8b5Scube 	ndevi--;
14147aa6070dScube 	/*
14157aa6070dScube 	 * Put it in deaddevitab
1416a31ff6b4Scube 	 *
1417a31ff6b4Scube 	 * Each time a devi is removed, devilevel is increased so that later on
1418a31ff6b4Scube 	 * it is possible to tell if an instance was added before or after the
1419a31ff6b4Scube 	 * removal of its parent.
1420a31ff6b4Scube 	 *
1421a31ff6b4Scube 	 * For active instances, i_level contains the number of devi removed so
1422a31ff6b4Scube 	 * far, and for dead devis, it contains its index.
14237aa6070dScube 	 */
1424a31ff6b4Scube 	i->i_level = devilevel++;
14257aa6070dScube 	i->i_alias = NULL;
14267aa6070dScube 	f = ht_lookup(deaddevitab, i->i_name);
14277aa6070dScube 	if (f == NULL) {
14287aa6070dScube 		if (ht_insert(deaddevitab, i->i_name, i))
14297aa6070dScube 			panic("remove_devi(%s) - can't add to deaddevitab",
14307aa6070dScube 			    i->i_name);
14317aa6070dScube 	} else {
14327aa6070dScube 		for (j = f; j->i_alias != NULL; j = j->i_alias);
14337aa6070dScube 		j->i_alias = i;
14347aa6070dScube 	}
1435e499d8b5Scube 	/*
14365d1e8b27Swiz 	 *   - reconstruct d->d_umax
1437e499d8b5Scube 	 */
1438e499d8b5Scube 	d->d_umax = 0;
1439e499d8b5Scube 	for (i = d->d_ihead; i != NULL; i = i->i_bsame)
1440e499d8b5Scube 		if (i->i_unit >= d->d_umax)
1441e499d8b5Scube 			d->d_umax = i->i_unit + 1;
14425ecc953bSthorpej }
14435ecc953bSthorpej 
14445ecc953bSthorpej void
14457b7c582aScube deldeva(const char *at)
14467b7c582aScube {
14477b7c582aScube 	int unit;
14487b7c582aScube 	const char *cp;
14497b7c582aScube 	struct devbase *d, *ad;
14507b7c582aScube 	struct devi *i, *j;
14517b7c582aScube 	struct attr *a;
14527b7c582aScube 	struct pspec *p;
14537b7c582aScube 	struct nvlist *nv, *stack = NULL;
14547b7c582aScube 
14557b7c582aScube 	if (at == NULL) {
14567b7c582aScube 		TAILQ_FOREACH(i, &alldevi, i_next)
14577b7c582aScube 			if (i->i_at == NULL)
14587b7c582aScube 				stack = newnv(NULL, NULL, i, 0, stack);
14597b7c582aScube 	} else {
14607b7c582aScube 		int l;
14617b7c582aScube 
14627b7c582aScube 		l = strlen(at) - 1;
14637b7c582aScube 		if (at[l] == '?' || isdigit((unsigned char)at[l])) {
14647b7c582aScube 			char base[NAMESIZE];
14657b7c582aScube 
14667b7c582aScube 			if (split(at, l+1, base, sizeof base, &unit)) {
1467c7295a4cSchristos 				cfgerror("invalid attachment name `%s'", at);
14687b7c582aScube 				return;
14697b7c582aScube 			}
14707b7c582aScube 			cp = intern(base);
14717b7c582aScube 		} else {
14727b7c582aScube 			cp = intern(at);
14737b7c582aScube 			unit = STAR;
14747b7c582aScube 		}
14757b7c582aScube 
14767b7c582aScube 		ad = ht_lookup(devbasetab, cp);
14777b7c582aScube 		a = ht_lookup(attrtab, cp);
14787b7c582aScube 		if (a == NULL) {
1479c7295a4cSchristos 			cfgerror("unknown attachment attribute or device `%s'",
14807b7c582aScube 			    cp);
14817b7c582aScube 			return;
14827b7c582aScube 		}
14837b7c582aScube 		if (!a->a_iattr) {
1484c7295a4cSchristos 			cfgerror("plain attribute `%s' cannot have children",
14857b7c582aScube 			    a->a_name);
14867b7c582aScube 			return;
14877b7c582aScube 		}
14887b7c582aScube 
14897b7c582aScube 		/*
14907b7c582aScube 		 * remove_devi() makes changes to the devbase's list and the
14917b7c582aScube 		 * alias list, * so the actual deletion of the instances must
14927b7c582aScube 		 * be delayed.
14937b7c582aScube 		 */
14947b7c582aScube 		for (nv = a->a_devs; nv != NULL; nv = nv->nv_next) {
14957b7c582aScube 			d = nv->nv_ptr;
14967b7c582aScube 			for (i = d->d_ihead; i != NULL; i = i->i_bsame)
14977b7c582aScube 				for (j = i; j != NULL; j = j->i_alias) {
14987b7c582aScube 					/* Ignore devices at root */
14997b7c582aScube 					if (j->i_at == NULL)
15007b7c582aScube 						continue;
15017b7c582aScube 					p = j->i_pspec;
15027b7c582aScube 					/*
15037b7c582aScube 					 * There are three cases:
15047b7c582aScube 					 *
15057b7c582aScube 					 * 1.  unit is not STAR.  Consider 'at'
15067b7c582aScube 					 *     to be explicit, even if it
15077b7c582aScube 					 *     references an interface
15087b7c582aScube 					 *     attribute.
15097b7c582aScube 					 *
15107b7c582aScube 					 * 2.  unit is STAR and 'at' references
15117b7c582aScube 					 *     a real device.  Look for pspec
15127b7c582aScube 					 *     that have a matching p_atdev
15137b7c582aScube 					 *     field.
15147b7c582aScube 					 *
15157b7c582aScube 					 * 3.  unit is STAR and 'at' references
15167b7c582aScube 					 *     an interface attribute.  Look
15177b7c582aScube 					 *     for pspec that have a matching
15187b7c582aScube 					 *     p_iattr field.
15197b7c582aScube 					 */
15207b7c582aScube 					if ((unit != STAR &&        /* Case */
15217b7c582aScube 					     !strcmp(j->i_at, at)) ||  /* 1 */
15227b7c582aScube 					    (unit == STAR &&
15237b7c582aScube 					     ((ad != NULL &&        /* Case */
15247b7c582aScube 					       p->p_atdev == ad) ||    /* 2 */
15257b7c582aScube 					      (ad == NULL &&        /* Case */
15267b7c582aScube 					       p->p_iattr == a))))     /* 3 */
15277b7c582aScube 						stack = newnv(NULL, NULL, j, 0,
15287b7c582aScube 						    stack);
15297b7c582aScube 				}
15307b7c582aScube 		}
15317b7c582aScube 	}
15327b7c582aScube 
15337b7c582aScube 	for (nv = stack; nv != NULL; nv = nv->nv_next)
15347b7c582aScube 		remove_devi(nv->nv_ptr);
1535e50e4ee4Scube 	nvfreel(stack);
15367b7c582aScube }
15377b7c582aScube 
15387b7c582aScube void
15397b7c582aScube deldev(const char *name)
15407b7c582aScube {
15417b7c582aScube 	int l;
15427b7c582aScube 	struct devi *firsti, *i;
15437b7c582aScube 	struct nvlist *nv, *stack = NULL;
15447b7c582aScube 
15457b7c582aScube 	l = strlen(name) - 1;
15467b7c582aScube 	if (name[l] == '*' || isdigit((unsigned char)name[l])) {
15477b7c582aScube 		/* `no mydev0' or `no mydev*' */
15487b7c582aScube 		firsti = ht_lookup(devitab, name);
15497b7c582aScube 		if (firsti == NULL) {
1550c7295a4cSchristos 			cfgerror("unknown instance %s", name);
15517b7c582aScube 			return;
15527b7c582aScube 		}
15537b7c582aScube 		for (i = firsti; i != NULL; i = i->i_alias)
15547b7c582aScube 			stack = newnv(NULL, NULL, i, 0, stack);
15557b7c582aScube 	} else {
15567b7c582aScube 		struct devbase *d = ht_lookup(devbasetab, name);
15577b7c582aScube 
15587b7c582aScube 		if (d == NULL) {
1559c7295a4cSchristos 			cfgerror("unknown device %s", name);
15607b7c582aScube 			return;
15617b7c582aScube 		}
156271af9028Scube 		if (d->d_ispseudo) {
1563c7295a4cSchristos 			cfgerror("%s is a pseudo-device; "
156471af9028Scube 			    "use \"no pseudo-device %s\" instead", name,
156571af9028Scube 			    name);
156671af9028Scube 			return;
156771af9028Scube 		}
15687b7c582aScube 
15697b7c582aScube 		for (firsti = d->d_ihead; firsti != NULL;
15707b7c582aScube 		    firsti = firsti->i_bsame)
15717b7c582aScube 			for (i = firsti; i != NULL; i = i->i_alias)
15727b7c582aScube 				stack = newnv(NULL, NULL, i, 0, stack);
15737b7c582aScube 	}
15747b7c582aScube 
15757b7c582aScube 	for (nv = stack; nv != NULL; nv = nv->nv_next)
15767b7c582aScube 		remove_devi(nv->nv_ptr);
1577e50e4ee4Scube 	nvfreel(stack);
15787b7c582aScube }
15797b7c582aScube 
158000ed8a38Spooka /*
158100ed8a38Spooka  * Insert given device "name" into devroottab.  In case "name"
158200ed8a38Spooka  * designates a pure interface attribute, create a fake device
158300ed8a38Spooka  * instance for the attribute and insert that into the roottab
158400ed8a38Spooka  * (this scheme avoids mucking around with the orphanage analysis).
158500ed8a38Spooka  */
15867b7c582aScube void
158790ac64deSpooka addpseudoroot(const char *name)
158890ac64deSpooka {
1589194e1c80Spooka 	char buf[NAMESIZE];
1590194e1c80Spooka 	int unit;
1591194e1c80Spooka 	struct attr *attr;
159290ac64deSpooka 	struct devi *i;
159390ac64deSpooka 	struct deva *iba;
159490ac64deSpooka 	struct devbase *ib;
159590ac64deSpooka 
1596194e1c80Spooka 	if (split(name, strlen(name), buf, sizeof(buf), &unit)) {
1597194e1c80Spooka 		cfgerror("invalid pseudo-root name `%s'", name);
1598194e1c80Spooka 		return;
1599194e1c80Spooka 	}
1600194e1c80Spooka 
1601194e1c80Spooka 	/*
160200ed8a38Spooka 	 * Prefer device because devices with locators define an
160300ed8a38Spooka 	 * implicit interface attribute.  However, if a device is
160400ed8a38Spooka 	 * not available, try to attach to the interface attribute.
160500ed8a38Spooka 	 * This makes sure adddev() doesn't get confused when we
160600ed8a38Spooka 	 * are really attaching to a device (alternatively we maybe
160700ed8a38Spooka 	 * could specify a non-NULL atlist to defdevattach() below).
1608194e1c80Spooka 	 */
160900ed8a38Spooka 	ib = ht_lookup(devbasetab, intern(buf));
161000ed8a38Spooka 	if (ib == NULL) {
1611194e1c80Spooka 		struct devbase *fakedev;
1612194e1c80Spooka 		char fakename[NAMESIZE];
1613194e1c80Spooka 
161400ed8a38Spooka 		attr = ht_lookup(attrtab, intern(buf));
161500ed8a38Spooka 		if (!(attr && attr->a_iattr)) {
161600ed8a38Spooka 			cfgerror("pseudo-root `%s' not available", name);
161700ed8a38Spooka 			return;
161800ed8a38Spooka 		}
161900ed8a38Spooka 
1620194e1c80Spooka 		/*
1621194e1c80Spooka 		 * here we cheat a bit: create a fake devbase with the
1622194e1c80Spooka 		 * interface attribute and instantiate it.  quick, cheap,
1623194e1c80Spooka 		 * dirty & bad for you, much like the stuff in the fridge.
1624194e1c80Spooka 		 * and, it works, since the pseudoroot device is not included
1625194e1c80Spooka 		 * in ioconf, just used by config to make sure we start from
1626194e1c80Spooka 		 * the right place.
1627194e1c80Spooka 		 */
162800ed8a38Spooka 		snprintf(fakename, sizeof(fakename), "%s_devattrs", buf);
1629194e1c80Spooka 		fakedev = getdevbase(intern(fakename));
1630194e1c80Spooka 		fakedev->d_isdef = 1;
1631194e1c80Spooka 		fakedev->d_ispseudo = 0;
16324caf067bSdholland 		fakedev->d_attrs = attrlist_cons(NULL, attr);
1633194e1c80Spooka 		defdevattach(NULL, fakedev, NULL, NULL);
1634194e1c80Spooka 
1635194e1c80Spooka 		if (unit == STAR)
1636194e1c80Spooka 			snprintf(buf, sizeof(buf), "%s*", fakename);
1637194e1c80Spooka 		else
1638194e1c80Spooka 			snprintf(buf, sizeof(buf), "%s%d", fakename, unit);
1639194e1c80Spooka 		name = buf;
1640194e1c80Spooka 	}
1641194e1c80Spooka 
1642194e1c80Spooka 	/* ok, everything should be set up, so instantiate a fake device */
164390ac64deSpooka 	i = getdevi(name);
164490ac64deSpooka 	if (i == NULL)
164500ed8a38Spooka 		panic("device `%s' expected to be present", name);
164690ac64deSpooka 	ib = i->i_base;
1647194e1c80Spooka 	iba = ib->d_ahead;
164890ac64deSpooka 
164990ac64deSpooka 	i->i_atdeva = iba;
165090ac64deSpooka 	i->i_cfflags = 0;
165190ac64deSpooka 	i->i_locs = fixloc(name, &errattr, NULL);
165290ac64deSpooka 	i->i_pseudoroot = 1;
165390ac64deSpooka 	i->i_active = DEVI_ORPHAN; /* set active by kill_orphans() */
165490ac64deSpooka 
165590ac64deSpooka 	*iba->d_ipp = i;
165690ac64deSpooka 	iba->d_ipp = &i->i_asame;
165790ac64deSpooka 
165890ac64deSpooka 	ht_insert(devroottab, ib->d_name, ib);
165990ac64deSpooka }
166090ac64deSpooka 
166190ac64deSpooka void
16625ecc953bSthorpej addpseudo(const char *name, int number)
16635ecc953bSthorpej {
16645ecc953bSthorpej 	struct devbase *d;
16655ecc953bSthorpej 	struct devi *i;
16665ecc953bSthorpej 
16675ecc953bSthorpej 	d = ht_lookup(devbasetab, name);
16685ecc953bSthorpej 	if (d == NULL) {
1669c7295a4cSchristos 		cfgerror("undefined pseudo-device %s", name);
16705ecc953bSthorpej 		return;
16715ecc953bSthorpej 	}
16725ecc953bSthorpej 	if (!d->d_ispseudo) {
1673c7295a4cSchristos 		cfgerror("%s is a real device, not a pseudo-device", name);
16745ecc953bSthorpej 		return;
16755ecc953bSthorpej 	}
16765ecc953bSthorpej 	if (ht_lookup(devitab, name) != NULL) {
1677c7295a4cSchristos 		cfgerror("`%s' already defined", name);
16785ecc953bSthorpej 		return;
16795ecc953bSthorpej 	}
16805ecc953bSthorpej 	i = newdevi(name, number - 1, d);	/* foo 16 => "foo0..foo15" */
16815ecc953bSthorpej 	if (ht_insert(devitab, name, i))
16825ecc953bSthorpej 		panic("addpseudo(%s)", name);
16837b7c582aScube 	/* Useful to retrieve the instance from the devbase */
16847b7c582aScube 	d->d_ihead = i;
16857aa6070dScube 	i->i_active = DEVI_ACTIVE;
16865ecc953bSthorpej 	TAILQ_INSERT_TAIL(&allpseudo, i, i_next);
16875ecc953bSthorpej }
16885ecc953bSthorpej 
16895ecc953bSthorpej void
16905ecc953bSthorpej delpseudo(const char *name)
16915ecc953bSthorpej {
16925ecc953bSthorpej 	struct devbase *d;
16935ecc953bSthorpej 	struct devi *i;
16945ecc953bSthorpej 
16955ecc953bSthorpej 	d = ht_lookup(devbasetab, name);
16965ecc953bSthorpej 	if (d == NULL) {
1697c7295a4cSchristos 		cfgerror("undefined pseudo-device %s", name);
16985ecc953bSthorpej 		return;
16995ecc953bSthorpej 	}
17005ecc953bSthorpej 	if (!d->d_ispseudo) {
1701c7295a4cSchristos 		cfgerror("%s is a real device, not a pseudo-device", name);
17025ecc953bSthorpej 		return;
17035ecc953bSthorpej 	}
17045ecc953bSthorpej 	if ((i = ht_lookup(devitab, name)) == NULL) {
1705c7295a4cSchristos 		cfgerror("`%s' not defined", name);
17065ecc953bSthorpej 		return;
17075ecc953bSthorpej 	}
17085ecc953bSthorpej 	d->d_umax = 0;		/* clear neads-count entries */
17097aa6070dScube 	d->d_ihead = NULL;	/* make sure it won't be considered active */
17105ecc953bSthorpej 	TAILQ_REMOVE(&allpseudo, i, i_next);
17115ecc953bSthorpej 	if (ht_remove(devitab, name))
17125ecc953bSthorpej 		panic("delpseudo(%s) - can't remove from devitab", name);
17137aa6070dScube 	if (ht_insert(deaddevitab, name, i))
17147aa6070dScube 		panic("delpseudo(%s) - can't add to deaddevitab", name);
17155ecc953bSthorpej }
17165ecc953bSthorpej 
17175ecc953bSthorpej void
1718d767912bSdrochner adddevm(const char *name, devmajor_t cmajor, devmajor_t bmajor,
17199483bda7Sdholland 	struct condexpr *cond, struct nvlist *nv_nodes)
17205ecc953bSthorpej {
17215ecc953bSthorpej 	struct devm *dm;
17225ecc953bSthorpej 
1723d767912bSdrochner 	if (cmajor != NODEVMAJOR && (cmajor < 0 || cmajor >= 4096)) {
1724c7295a4cSchristos 		cfgerror("character major %d is invalid", cmajor);
17259483bda7Sdholland 		condexpr_destroy(cond);
17263da3ab25Spooka 		nvfreel(nv_nodes);
17275ecc953bSthorpej 		return;
17285ecc953bSthorpej 	}
17295ecc953bSthorpej 
1730d767912bSdrochner 	if (bmajor != NODEVMAJOR && (bmajor < 0 || bmajor >= 4096)) {
1731c7295a4cSchristos 		cfgerror("block major %d is invalid", bmajor);
17329483bda7Sdholland 		condexpr_destroy(cond);
17333da3ab25Spooka 		nvfreel(nv_nodes);
17345ecc953bSthorpej 		return;
17355ecc953bSthorpej 	}
1736d767912bSdrochner 	if (cmajor == NODEVMAJOR && bmajor == NODEVMAJOR) {
1737c7295a4cSchristos 		cfgerror("both character/block majors are not specified");
17389483bda7Sdholland 		condexpr_destroy(cond);
17393da3ab25Spooka 		nvfreel(nv_nodes);
17405ecc953bSthorpej 		return;
17415ecc953bSthorpej 	}
17425ecc953bSthorpej 
17435ecc953bSthorpej 	dm = ecalloc(1, sizeof(*dm));
17445ecc953bSthorpej 	dm->dm_srcfile = yyfile;
17455ecc953bSthorpej 	dm->dm_srcline = currentline();
17465ecc953bSthorpej 	dm->dm_name = name;
17475ecc953bSthorpej 	dm->dm_cmajor = cmajor;
17485ecc953bSthorpej 	dm->dm_bmajor = bmajor;
17499483bda7Sdholland 	dm->dm_opts = cond;
17503da3ab25Spooka 	dm->dm_devnodes = nv_nodes;
17515ecc953bSthorpej 
17525ecc953bSthorpej 	TAILQ_INSERT_TAIL(&alldevms, dm, dm_next);
17535ecc953bSthorpej 
17545ecc953bSthorpej 	maxcdevm = MAX(maxcdevm, dm->dm_cmajor);
17555ecc953bSthorpej 	maxbdevm = MAX(maxbdevm, dm->dm_bmajor);
17565ecc953bSthorpej }
17575ecc953bSthorpej 
17587aa6070dScube int
17595ecc953bSthorpej fixdevis(void)
17605ecc953bSthorpej {
17615ecc953bSthorpej 	struct devi *i;
17627aa6070dScube 	int error = 0;
17635ecc953bSthorpej 
17645ecc953bSthorpej 	TAILQ_FOREACH(i, &alldevi, i_next)
17657aa6070dScube 		if (i->i_active == DEVI_ACTIVE)
17665ecc953bSthorpej 			selectbase(i->i_base, i->i_atdeva);
17677aa6070dScube 		else if (i->i_active == DEVI_ORPHAN) {
1768c130d400Scube 			/*
1769c130d400Scube 			 * At this point, we can't have instances for which
1770c130d400Scube 			 * i_at or i_pspec are NULL.
1771c130d400Scube 			 */
17727aa6070dScube 			++error;
1773c7295a4cSchristos 			cfgxerror(i->i_srcfile, i->i_lineno,
17740dbd1c0eScube 			    "`%s at %s' is orphaned (%s `%s' found)",
1775c130d400Scube 			    i->i_name, i->i_at, i->i_pspec->p_atunit == WILD ?
1776c130d400Scube 			    "nothing matching" : "no", i->i_at);
17777aa6070dScube 		} else if (vflag && i->i_active == DEVI_IGNORED)
1778c7295a4cSchristos 			cfgxwarn(i->i_srcfile, i->i_lineno, "ignoring "
1779c7295a4cSchristos 			    "explicitly orphaned instance `%s at %s'",
1780c7295a4cSchristos 			    i->i_name, i->i_at);
17817aa6070dScube 
17827aa6070dScube 	if (error)
17837aa6070dScube 		return error;
17845ecc953bSthorpej 
17855ecc953bSthorpej 	TAILQ_FOREACH(i, &allpseudo, i_next)
17867aa6070dScube 		if (i->i_active == DEVI_ACTIVE)
17875ecc953bSthorpej 			selectbase(i->i_base, NULL);
17887aa6070dScube 	return 0;
17895ecc953bSthorpej }
17905ecc953bSthorpej 
17915ecc953bSthorpej /*
17925ecc953bSthorpej  * Look up a parent spec, creating a new one if it does not exist.
17935ecc953bSthorpej  */
17945ecc953bSthorpej static struct pspec *
17955ecc953bSthorpej getpspec(struct attr *attr, struct devbase *ab, int atunit)
17965ecc953bSthorpej {
17975ecc953bSthorpej 	struct pspec *p;
17985ecc953bSthorpej 
17995ecc953bSthorpej 	TAILQ_FOREACH(p, &allpspecs, p_list) {
18005ecc953bSthorpej 		if (p->p_iattr == attr &&
18015ecc953bSthorpej 		    p->p_atdev == ab &&
18025ecc953bSthorpej 		    p->p_atunit == atunit)
18035ecc953bSthorpej 			return (p);
18045ecc953bSthorpej 	}
18055ecc953bSthorpej 
18065ecc953bSthorpej 	p = ecalloc(1, sizeof(*p));
18075ecc953bSthorpej 
18085ecc953bSthorpej 	p->p_iattr = attr;
18095ecc953bSthorpej 	p->p_atdev = ab;
18105ecc953bSthorpej 	p->p_atunit = atunit;
18115ecc953bSthorpej 	p->p_inst = npspecs++;
1812c130d400Scube 	p->p_active = 0;
18135ecc953bSthorpej 
18145ecc953bSthorpej 	TAILQ_INSERT_TAIL(&allpspecs, p, p_list);
18155ecc953bSthorpej 
18165ecc953bSthorpej 	return (p);
18175ecc953bSthorpej }
18185ecc953bSthorpej 
18195ecc953bSthorpej /*
18205ecc953bSthorpej  * Define a new instance of a specific device.
18215ecc953bSthorpej  */
18225ecc953bSthorpej static struct devi *
18235ecc953bSthorpej getdevi(const char *name)
18245ecc953bSthorpej {
18255ecc953bSthorpej 	struct devi *i, *firsti;
18265ecc953bSthorpej 	struct devbase *d;
18275ecc953bSthorpej 	int unit;
18285ecc953bSthorpej 	char base[NAMESIZE];
18295ecc953bSthorpej 
18305ecc953bSthorpej 	if (split(name, strlen(name), base, sizeof base, &unit)) {
1831c7295a4cSchristos 		cfgerror("invalid device name `%s'", name);
18325ecc953bSthorpej 		return (NULL);
18335ecc953bSthorpej 	}
18345ecc953bSthorpej 	d = ht_lookup(devbasetab, intern(base));
18355ecc953bSthorpej 	if (d == NULL) {
1836c7295a4cSchristos 		cfgerror("%s: unknown device `%s'", name, base);
18375ecc953bSthorpej 		return (NULL);
18385ecc953bSthorpej 	}
18395ecc953bSthorpej 	if (d->d_ispseudo) {
1840c7295a4cSchristos 		cfgerror("%s: %s is a pseudo-device", name, base);
18415ecc953bSthorpej 		return (NULL);
18425ecc953bSthorpej 	}
18435ecc953bSthorpej 	firsti = ht_lookup(devitab, name);
18445ecc953bSthorpej 	i = newdevi(name, unit, d);
18455ecc953bSthorpej 	if (firsti == NULL) {
18465ecc953bSthorpej 		if (ht_insert(devitab, name, i))
18475ecc953bSthorpej 			panic("getdevi(%s)", name);
18485ecc953bSthorpej 		*d->d_ipp = i;
18495ecc953bSthorpej 		d->d_ipp = &i->i_bsame;
18505ecc953bSthorpej 	} else {
18515ecc953bSthorpej 		while (firsti->i_alias)
18525ecc953bSthorpej 			firsti = firsti->i_alias;
18535ecc953bSthorpej 		firsti->i_alias = i;
18545ecc953bSthorpej 	}
18555ecc953bSthorpej 	TAILQ_INSERT_TAIL(&alldevi, i, i_next);
18565ecc953bSthorpej 	ndevi++;
18575ecc953bSthorpej 	return (i);
18585ecc953bSthorpej }
18595ecc953bSthorpej 
18605ecc953bSthorpej static const char *
18615ecc953bSthorpej concat(const char *name, int c)
18625ecc953bSthorpej {
1863c7295a4cSchristos 	size_t len;
18645ecc953bSthorpej 	char buf[NAMESIZE];
18655ecc953bSthorpej 
18665ecc953bSthorpej 	len = strlen(name);
18675ecc953bSthorpej 	if (len + 2 > sizeof(buf)) {
1868c7295a4cSchristos 		cfgerror("device name `%s%c' too long", name, c);
18695ecc953bSthorpej 		len = sizeof(buf) - 2;
18705ecc953bSthorpej 	}
18715ecc953bSthorpej 	memmove(buf, name, len);
18725ecc953bSthorpej 	buf[len] = c;
18735ecc953bSthorpej 	buf[len + 1] = 0;
18745ecc953bSthorpej 	return (intern(buf));
18755ecc953bSthorpej }
18765ecc953bSthorpej 
18775ecc953bSthorpej const char *
18785ecc953bSthorpej starref(const char *name)
18795ecc953bSthorpej {
18805ecc953bSthorpej 
18815ecc953bSthorpej 	return (concat(name, '*'));
18825ecc953bSthorpej }
18835ecc953bSthorpej 
18845ecc953bSthorpej const char *
18855ecc953bSthorpej wildref(const char *name)
18865ecc953bSthorpej {
18875ecc953bSthorpej 
18885ecc953bSthorpej 	return (concat(name, '?'));
18895ecc953bSthorpej }
18905ecc953bSthorpej 
18915ecc953bSthorpej /*
18925ecc953bSthorpej  * Split a name like "foo0" into base name (foo) and unit number (0).
18935ecc953bSthorpej  * Return 0 on success.  To make this useful for names like "foo0a",
18945ecc953bSthorpej  * the length of the "foo0" part is one of the arguments.
18955ecc953bSthorpej  */
18965ecc953bSthorpej static int
18975ecc953bSthorpej split(const char *name, size_t nlen, char *base, size_t bsize, int *aunit)
18985ecc953bSthorpej {
18995ecc953bSthorpej 	const char *cp;
1900c7295a4cSchristos 	int c;
1901c7295a4cSchristos 	size_t l;
19025ecc953bSthorpej 
19035ecc953bSthorpej 	l = nlen;
19045ecc953bSthorpej 	if (l < 2 || l >= bsize || isdigit((unsigned char)*name))
19055ecc953bSthorpej 		return (1);
19065ecc953bSthorpej 	c = (u_char)name[--l];
19075ecc953bSthorpej 	if (!isdigit(c)) {
19085ecc953bSthorpej 		if (c == '*')
19095ecc953bSthorpej 			*aunit = STAR;
19105ecc953bSthorpej 		else if (c == '?')
19115ecc953bSthorpej 			*aunit = WILD;
19125ecc953bSthorpej 		else
19135ecc953bSthorpej 			return (1);
19145ecc953bSthorpej 	} else {
19155ecc953bSthorpej 		cp = &name[l];
19165ecc953bSthorpej 		while (isdigit((unsigned char)cp[-1]))
19175ecc953bSthorpej 			l--, cp--;
19185ecc953bSthorpej 		*aunit = atoi(cp);
19195ecc953bSthorpej 	}
19205ecc953bSthorpej 	memmove(base, name, l);
19215ecc953bSthorpej 	base[l] = 0;
19225ecc953bSthorpej 	return (0);
19235ecc953bSthorpej }
19245ecc953bSthorpej 
19255ecc953bSthorpej void
19265ecc953bSthorpej selectattr(struct attr *a)
19275ecc953bSthorpej {
19283a03bee6Suebayasi 	struct attrlist *al;
19293a03bee6Suebayasi 	struct attr *dep;
19305ecc953bSthorpej 
19313a03bee6Suebayasi 	for (al = a->a_deps; al != NULL; al = al->al_next) {
19323a03bee6Suebayasi 		dep = al->al_this;
19333a03bee6Suebayasi 		selectattr(dep);
19343a03bee6Suebayasi 	}
1935c7295a4cSchristos 	(void)ht_insert(selecttab, a->a_name, __UNCONST(a->a_name));
1936b599bf99Suebayasi 	CFGDBG(3, "attr selected `%s'", a->a_name);
19375ecc953bSthorpej }
19385ecc953bSthorpej 
19395ecc953bSthorpej /*
19405ecc953bSthorpej  * We have an instance of the base foo, so select it and all its
19415ecc953bSthorpej  * attributes for "optional foo".
19425ecc953bSthorpej  */
19435ecc953bSthorpej static void
19445ecc953bSthorpej selectbase(struct devbase *d, struct deva *da)
19455ecc953bSthorpej {
19465ecc953bSthorpej 	struct attr *a;
19474caf067bSdholland 	struct attrlist *al;
19485ecc953bSthorpej 
1949c7295a4cSchristos 	(void)ht_insert(selecttab, d->d_name, __UNCONST(d->d_name));
1950b599bf99Suebayasi 	CFGDBG(3, "devbase selected `%s'", d->d_name);
19514caf067bSdholland 	for (al = d->d_attrs; al != NULL; al = al->al_next) {
19524caf067bSdholland 		a = al->al_this;
19535ecc953bSthorpej 		expandattr(a, selectattr);
19545ecc953bSthorpej 	}
1955da706167Suebayasi 
1956da706167Suebayasi 	struct attr *devattr;
1957da706167Suebayasi 	devattr = refattr(d->d_name);
1958da706167Suebayasi 	expandattr(devattr, selectattr);
1959da706167Suebayasi 
19605ecc953bSthorpej 	if (da != NULL) {
1961c7295a4cSchristos 		(void)ht_insert(selecttab, da->d_name, __UNCONST(da->d_name));
1962b599bf99Suebayasi 		CFGDBG(3, "devattr selected `%s'", da->d_name);
19634caf067bSdholland 		for (al = da->d_attrs; al != NULL; al = al->al_next) {
19644caf067bSdholland 			a = al->al_this;
19655ecc953bSthorpej 			expandattr(a, selectattr);
19665ecc953bSthorpej 		}
19675ecc953bSthorpej 	}
19685ecc953bSthorpej }
19695ecc953bSthorpej 
19705ecc953bSthorpej /*
19715ecc953bSthorpej  * Is the given pointer on the given list of pointers?
19725ecc953bSthorpej  */
197359c94545Scube int
19745ecc953bSthorpej onlist(struct nvlist *nv, void *ptr)
19755ecc953bSthorpej {
19765ecc953bSthorpej 	for (; nv != NULL; nv = nv->nv_next)
19775ecc953bSthorpej 		if (nv->nv_ptr == ptr)
19785ecc953bSthorpej 			return (1);
19795ecc953bSthorpej 	return (0);
19805ecc953bSthorpej }
19815ecc953bSthorpej 
19825ecc953bSthorpej static char *
19835ecc953bSthorpej extend(char *p, const char *name)
19845ecc953bSthorpej {
1985c7295a4cSchristos 	size_t l;
19865ecc953bSthorpej 
19875ecc953bSthorpej 	l = strlen(name);
19885ecc953bSthorpej 	memmove(p, name, l);
19895ecc953bSthorpej 	p += l;
19905ecc953bSthorpej 	*p++ = ',';
19915ecc953bSthorpej 	*p++ = ' ';
19925ecc953bSthorpej 	return (p);
19935ecc953bSthorpej }
19945ecc953bSthorpej 
19955ecc953bSthorpej /*
19965ecc953bSthorpej  * Check that we got all required locators, and default any that are
19975ecc953bSthorpej  * given as "?" and have defaults.  Return 0 on success.
19985ecc953bSthorpej  */
19995ecc953bSthorpej static const char **
200016a8771bSdholland fixloc(const char *name, struct attr *attr, struct loclist *got)
20015ecc953bSthorpej {
200216a8771bSdholland 	struct loclist *m, *n;
20035ecc953bSthorpej 	int ord;
20045ecc953bSthorpej 	const char **lp;
20055ecc953bSthorpej 	int nmissing, nextra, nnodefault;
20065ecc953bSthorpej 	char *mp, *ep, *ndp;
20075ecc953bSthorpej 	char missing[1000], extra[1000], nodefault[1000];
20085ecc953bSthorpej 	static const char *nullvec[1];
20095ecc953bSthorpej 
20105ecc953bSthorpej 	/*
20115ecc953bSthorpej 	 * Look for all required locators, and number the given ones
20125ecc953bSthorpej 	 * according to the required order.  While we are numbering,
20135ecc953bSthorpej 	 * set default values for defaulted locators.
20145ecc953bSthorpej 	 */
20155ecc953bSthorpej 	if (attr->a_loclen == 0)	/* e.g., "at root" */
20165ecc953bSthorpej 		lp = nullvec;
20175ecc953bSthorpej 	else
20185ecc953bSthorpej 		lp = emalloc((attr->a_loclen + 1) * sizeof(const char *));
201916a8771bSdholland 	for (n = got; n != NULL; n = n->ll_next)
202016a8771bSdholland 		n->ll_num = -1;
20215ecc953bSthorpej 	nmissing = 0;
20225ecc953bSthorpej 	mp = missing;
20235ecc953bSthorpej 	/* yes, this is O(mn), but m and n should be small */
202416a8771bSdholland 	for (ord = 0, m = attr->a_locs; m != NULL; m = m->ll_next, ord++) {
202516a8771bSdholland 		for (n = got; n != NULL; n = n->ll_next) {
202616a8771bSdholland 			if (n->ll_name == m->ll_name) {
202716a8771bSdholland 				n->ll_num = ord;
20285ecc953bSthorpej 				break;
20295ecc953bSthorpej 			}
20305ecc953bSthorpej 		}
203116a8771bSdholland 		if (n == NULL && m->ll_num == 0) {
20325ecc953bSthorpej 			nmissing++;
203316a8771bSdholland 			mp = extend(mp, m->ll_name);
20345ecc953bSthorpej 		}
203516a8771bSdholland 		lp[ord] = m->ll_string;
20365ecc953bSthorpej 	}
20375ecc953bSthorpej 	if (ord != attr->a_loclen)
20385ecc953bSthorpej 		panic("fixloc");
20395ecc953bSthorpej 	lp[ord] = NULL;
20405ecc953bSthorpej 	nextra = 0;
20415ecc953bSthorpej 	ep = extra;
20425ecc953bSthorpej 	nnodefault = 0;
20435ecc953bSthorpej 	ndp = nodefault;
204416a8771bSdholland 	for (n = got; n != NULL; n = n->ll_next) {
204516a8771bSdholland 		if (n->ll_num >= 0) {
204616a8771bSdholland 			if (n->ll_string != NULL)
204716a8771bSdholland 				lp[n->ll_num] = n->ll_string;
204816a8771bSdholland 			else if (lp[n->ll_num] == NULL) {
20495ecc953bSthorpej 				nnodefault++;
205016a8771bSdholland 				ndp = extend(ndp, n->ll_name);
20515ecc953bSthorpej 			}
20525ecc953bSthorpej 		} else {
20535ecc953bSthorpej 			nextra++;
205416a8771bSdholland 			ep = extend(ep, n->ll_name);
20555ecc953bSthorpej 		}
20565ecc953bSthorpej 	}
20575ecc953bSthorpej 	if (nextra) {
20585ecc953bSthorpej 		ep[-2] = 0;	/* kill ", " */
2059c7295a4cSchristos 		cfgerror("%s: extraneous locator%s: %s",
20605ecc953bSthorpej 		    name, nextra > 1 ? "s" : "", extra);
20615ecc953bSthorpej 	}
20625ecc953bSthorpej 	if (nmissing) {
20635ecc953bSthorpej 		mp[-2] = 0;
2064c7295a4cSchristos 		cfgerror("%s: must specify %s", name, missing);
20655ecc953bSthorpej 	}
20665ecc953bSthorpej 	if (nnodefault) {
20675ecc953bSthorpej 		ndp[-2] = 0;
2068c7295a4cSchristos 		cfgerror("%s: cannot wildcard %s", name, nodefault);
20695ecc953bSthorpej 	}
20705ecc953bSthorpej 	if (nmissing || nnodefault) {
20715ecc953bSthorpej 		free(lp);
20725ecc953bSthorpej 		lp = NULL;
20735ecc953bSthorpej 	}
20745ecc953bSthorpej 	return (lp);
20755ecc953bSthorpej }
2076437f8925Scube 
2077437f8925Scube void
2078437f8925Scube setversion(int newver)
2079437f8925Scube {
2080437f8925Scube 	if (newver > CONFIG_VERSION)
2081c7295a4cSchristos 		cfgerror("your sources require a newer version of config(1) "
2082437f8925Scube 		    "-- please rebuild it.");
2083437f8925Scube 	else if (newver < CONFIG_MINVERSION)
2084c7295a4cSchristos 		cfgerror("your sources are out of date -- please update.");
2085437f8925Scube 	else
2086437f8925Scube 		version = newver;
2087437f8925Scube }
2088