xref: /netbsd/usr.bin/config/sem.c (revision 4b9e5cb1)
1*4b9e5cb1Schristos /*	$NetBSD: sem.c,v 1.82 2017/11/27 00:25:46 christos 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 
479a7f4bbaSchristos #include <sys/cdefs.h>
48*4b9e5cb1Schristos __RCSID("$NetBSD: sem.c,v 1.82 2017/11/27 00:25:46 christos Exp $");
499a7f4bbaSchristos 
505ecc953bSthorpej #include <sys/param.h>
515ecc953bSthorpej #include <ctype.h>
525ecc953bSthorpej #include <stdio.h>
535ecc953bSthorpej #include <stdlib.h>
545ecc953bSthorpej #include <string.h>
55d0fb8901Schristos #include <util.h>
565ecc953bSthorpej #include "defs.h"
575ecc953bSthorpej #include "sem.h"
585ecc953bSthorpej 
595ecc953bSthorpej /*
605ecc953bSthorpej  * config semantics.
615ecc953bSthorpej  */
625ecc953bSthorpej 
635ecc953bSthorpej #define	NAMESIZE	100	/* local name buffers */
645ecc953bSthorpej 
655ecc953bSthorpej const char *s_ifnet;		/* magic attribute */
665ecc953bSthorpej const char *s_qmark;
675ecc953bSthorpej const char *s_none;
685ecc953bSthorpej 
695ecc953bSthorpej static struct hashtab *cfhashtab;	/* for config lookup */
705ecc953bSthorpej struct hashtab *devitab;		/* etc */
717c7d91e6Suebayasi struct attr allattr;
72374a3e4fSchristos size_t nattrs;
735ecc953bSthorpej 
745ecc953bSthorpej static struct attr errattr;
755ecc953bSthorpej static struct devbase errdev;
765ecc953bSthorpej static struct deva errdeva;
775ecc953bSthorpej 
784caf067bSdholland static int has_errobj(struct attrlist *, struct attr *);
795ecc953bSthorpej static struct nvlist *addtoattr(struct nvlist *, struct devbase *);
805ecc953bSthorpej static int resolve(struct nvlist **, const char *, const char *,
815ecc953bSthorpej 		   struct nvlist *, int);
82*4b9e5cb1Schristos static struct pspec *getpspec(struct attr *, struct devbase *, int, int);
835ecc953bSthorpej static struct devi *newdevi(const char *, int, struct devbase *d);
845ecc953bSthorpej static struct devi *getdevi(const char *);
85c130d400Scube static void remove_devi(struct devi *);
865ecc953bSthorpej static const char *concat(const char *, int);
875ecc953bSthorpej static char *extend(char *, const char *);
885ecc953bSthorpej static int split(const char *, size_t, char *, size_t, int *);
895ecc953bSthorpej static void selectbase(struct devbase *, struct deva *);
9016a8771bSdholland static const char **fixloc(const char *, struct attr *, struct loclist *);
91d767912bSdrochner static const char *makedevstr(devmajor_t, devminor_t);
92d767912bSdrochner static const char *major2name(devmajor_t);
93d767912bSdrochner static devmajor_t dev2major(struct devbase *);
945ecc953bSthorpej 
955ecc953bSthorpej extern const char *yyfile;
967aa6070dScube extern int vflag;
975ecc953bSthorpej 
9811a68adeSchristos #define V_ATTRIBUTE	0
9911a68adeSchristos #define V_DEVICE	1
10011a68adeSchristos struct vtype {
10111a68adeSchristos 	int type;
10211a68adeSchristos 	struct attr *attr;
10311a68adeSchristos 	void *value;
10411a68adeSchristos };
10511a68adeSchristos 
10611a68adeSchristos static struct nvlist *
10711a68adeSchristos makedevstack(struct devbase *d)
10811a68adeSchristos {
10911a68adeSchristos 	struct devi *firsti, *i;
11011a68adeSchristos 	struct nvlist *stack = NULL;
11111a68adeSchristos 
11211a68adeSchristos 	for (firsti = d->d_ihead; firsti != NULL; firsti = firsti->i_bsame)
11311a68adeSchristos 		for (i = firsti; i != NULL; i = i->i_alias)
11411a68adeSchristos 			stack = newnv(NULL, NULL, i, 0, stack);
11511a68adeSchristos 	return stack;
11611a68adeSchristos }
11711a68adeSchristos 
11811a68adeSchristos static void
11911a68adeSchristos devcleanup(struct nvlist *stack)
12011a68adeSchristos {
12111a68adeSchristos 	struct nvlist *nv;
12211a68adeSchristos 	for (nv = stack; nv != NULL; nv = nv->nv_next)
12311a68adeSchristos 		remove_devi(nv->nv_ptr);
12411a68adeSchristos 	nvfreel(stack);
12511a68adeSchristos }
12611a68adeSchristos 
12711a68adeSchristos static void *
12811a68adeSchristos addvalue(int type, struct attr *a, void *value)
12911a68adeSchristos {
13011a68adeSchristos 	struct vtype *vt = emalloc(sizeof(*vt));
13111a68adeSchristos 	vt->type = type;
13211a68adeSchristos 	vt->attr = a;
13311a68adeSchristos 	vt->value = value;
13411a68adeSchristos 	return vt;
13511a68adeSchristos }
13611a68adeSchristos 
1375ecc953bSthorpej void
1385ecc953bSthorpej initsem(void)
1395ecc953bSthorpej {
1405ecc953bSthorpej 
1415ecc953bSthorpej 	attrtab = ht_new();
1426d1bba4aSuebayasi 	attrdeptab = ht_new();
1437c7d91e6Suebayasi 
1447c7d91e6Suebayasi 	allattr.a_name = "netbsd";
1457c7d91e6Suebayasi 	TAILQ_INIT(&allattr.a_files);
1467c7d91e6Suebayasi 	(void)ht_insert(attrtab, allattr.a_name, &allattr);
147cd06fea3Suebayasi 	selectattr(&allattr);
1487c7d91e6Suebayasi 
1495ecc953bSthorpej 	errattr.a_name = "<internal>";
1505ecc953bSthorpej 
1515ecc953bSthorpej 	TAILQ_INIT(&allbases);
1525ecc953bSthorpej 
1535ecc953bSthorpej 	TAILQ_INIT(&alldevas);
1545ecc953bSthorpej 
1555ecc953bSthorpej 	TAILQ_INIT(&allpspecs);
1565ecc953bSthorpej 
1575ecc953bSthorpej 	cfhashtab = ht_new();
1585ecc953bSthorpej 	TAILQ_INIT(&allcf);
1595ecc953bSthorpej 
1605ecc953bSthorpej 	TAILQ_INIT(&alldevi);
1615ecc953bSthorpej 	errdev.d_name = "<internal>";
1625ecc953bSthorpej 
1635ecc953bSthorpej 	TAILQ_INIT(&allpseudo);
1645ecc953bSthorpej 
1655ecc953bSthorpej 	TAILQ_INIT(&alldevms);
1665ecc953bSthorpej 
1675ecc953bSthorpej 	s_ifnet = intern("ifnet");
1685ecc953bSthorpej 	s_qmark = intern("?");
1695ecc953bSthorpej 	s_none = intern("none");
1705ecc953bSthorpej }
1715ecc953bSthorpej 
1725ecc953bSthorpej /* Name of include file just ended (set in scan.l) */
1735ecc953bSthorpej extern const char *lastfile;
1745ecc953bSthorpej 
175a87832feSuebayasi static struct attr *
176da706167Suebayasi finddep(struct attr *a, const char *name)
177da706167Suebayasi {
178da706167Suebayasi 	struct attrlist *al;
179da706167Suebayasi 
180da706167Suebayasi 	for (al = a->a_deps; al != NULL; al = al->al_next) {
181da706167Suebayasi 		struct attr *this = al->al_this;
182da706167Suebayasi 		if (strcmp(this->a_name, name) == 0)
183da706167Suebayasi 			return this;
184da706167Suebayasi 	}
185da706167Suebayasi 	return NULL;
186da706167Suebayasi }
187da706167Suebayasi 
188da706167Suebayasi static void
189c80681c9Suebayasi mergedeps(const char *dname, const char *name)
190da706167Suebayasi {
191da706167Suebayasi 	struct attr *a, *newa;
192da706167Suebayasi 
193c80681c9Suebayasi 	CFGDBG(4, "merging attr `%s' to devbase `%s'", name, dname);
194c80681c9Suebayasi 	a = refattr(dname);
195da706167Suebayasi 	if (finddep(a, name) == NULL) {
196da706167Suebayasi 		newa = refattr(name);
197da706167Suebayasi 		a->a_deps = attrlist_cons(a->a_deps, newa);
198da706167Suebayasi 		CFGDBG(3, "attr `%s' merged to attr `%s'", newa->a_name,
199da706167Suebayasi 		    a->a_name);
200da706167Suebayasi 	}
201da706167Suebayasi }
202da706167Suebayasi 
203da706167Suebayasi static void
204da706167Suebayasi fixdev(struct devbase *dev)
205da706167Suebayasi {
206da706167Suebayasi 	struct attrlist *al;
207da706167Suebayasi 	struct attr *devattr, *a;
208da706167Suebayasi 
209da706167Suebayasi 	devattr = refattr(dev->d_name);
210da706167Suebayasi 	if (devattr->a_devclass)
211a87832feSuebayasi 		panic("%s: dev %s is devclass!", __func__, devattr->a_name);
212da706167Suebayasi 
213c4bac1d0Suebayasi 	CFGDBG(4, "fixing devbase `%s'", dev->d_name);
214da706167Suebayasi 	for (al = dev->d_attrs; al != NULL; al = al->al_next) {
215da706167Suebayasi 		a = al->al_this;
216d25ad65eSchristos 		CFGDBG(4, "fixing devbase `%s' attr `%s'", dev->d_name,
217d25ad65eSchristos 		    a->a_name);
218da706167Suebayasi 		if (a->a_iattr) {
219da706167Suebayasi 			a->a_refs = addtoattr(a->a_refs, dev);
220c4bac1d0Suebayasi 			CFGDBG(3, "device `%s' has iattr `%s'", dev->d_name,
221c4bac1d0Suebayasi 			    a->a_name);
222da706167Suebayasi 		} else if (a->a_devclass != NULL) {
223c5a6c986Suebayasi 			if (dev->d_classattr != NULL && dev->d_classattr != a) {
224da706167Suebayasi 				cfgwarn("device `%s' has multiple classes "
225da706167Suebayasi 				    "(`%s' and `%s')",
226da706167Suebayasi 				    dev->d_name, dev->d_classattr->a_name,
227da706167Suebayasi 				    a->a_name);
228da706167Suebayasi 			}
229c5a6c986Suebayasi 			if (dev->d_classattr == NULL) {
230da706167Suebayasi 				dev->d_classattr = a;
231d25ad65eSchristos 				CFGDBG(3, "device `%s' is devclass `%s'",
232d25ad65eSchristos 				    dev->d_name, a->a_name);
233c5a6c986Suebayasi 			}
234da706167Suebayasi 		} else {
235da706167Suebayasi 			if (strcmp(dev->d_name, a->a_name) != 0) {
236bc707469Suebayasi 				mergedeps(dev->d_name, a->a_name);
237da706167Suebayasi 			}
238da706167Suebayasi 		}
239da706167Suebayasi 	}
240da706167Suebayasi }
241da706167Suebayasi 
2425ecc953bSthorpej void
2435ecc953bSthorpej enddefs(void)
2445ecc953bSthorpej {
2455ecc953bSthorpej 	struct devbase *dev;
2465ecc953bSthorpej 
247c4bac1d0Suebayasi 	yyfile = "enddefs";
248c4bac1d0Suebayasi 
2495ecc953bSthorpej 	TAILQ_FOREACH(dev, &allbases, d_next) {
2505ecc953bSthorpej 		if (!dev->d_isdef) {
2515ecc953bSthorpej 			(void)fprintf(stderr,
2525ecc953bSthorpej 			    "%s: device `%s' used but not defined\n",
2535ecc953bSthorpej 			    lastfile, dev->d_name);
2545ecc953bSthorpej 			errors++;
2555ecc953bSthorpej 			continue;
2565ecc953bSthorpej 		}
257bc707469Suebayasi 		fixdev(dev);
2585ecc953bSthorpej 	}
2595ecc953bSthorpej 	if (errors) {
2605ecc953bSthorpej 		(void)fprintf(stderr, "*** Stop.\n");
2615ecc953bSthorpej 		exit(1);
2625ecc953bSthorpej 	}
2635ecc953bSthorpej }
2645ecc953bSthorpej 
2655ecc953bSthorpej void
2665ecc953bSthorpej setdefmaxusers(int min, int def, int max)
2675ecc953bSthorpej {
2685ecc953bSthorpej 
2695ecc953bSthorpej 	if (min < 1 || min > def || def > max)
270c7295a4cSchristos 		cfgerror("maxusers must have 1 <= min (%d) <= default (%d) "
271c7295a4cSchristos 		    "<= max (%d)", min, def, max);
2725ecc953bSthorpej 	else {
2735ecc953bSthorpej 		minmaxusers = min;
2745ecc953bSthorpej 		defmaxusers = def;
2755ecc953bSthorpej 		maxmaxusers = max;
2765ecc953bSthorpej 	}
2775ecc953bSthorpej }
2785ecc953bSthorpej 
2795ecc953bSthorpej void
2805ecc953bSthorpej setmaxusers(int n)
2815ecc953bSthorpej {
2825ecc953bSthorpej 
283895e5687Scube 	if (maxusers == n) {
284c7295a4cSchristos 		cfgerror("duplicate maxusers parameter");
2855ecc953bSthorpej 		return;
2865ecc953bSthorpej 	}
287895e5687Scube 	if (vflag && maxusers != 0)
288c7295a4cSchristos 		cfgwarn("warning: maxusers already defined");
2895ecc953bSthorpej 	maxusers = n;
2905ecc953bSthorpej 	if (n < minmaxusers) {
291c7295a4cSchristos 		cfgerror("warning: minimum of %d maxusers assumed",
292c7295a4cSchristos 		    minmaxusers);
2935ecc953bSthorpej 		errors--;	/* take it away */
2945ecc953bSthorpej 		maxusers = minmaxusers;
2955ecc953bSthorpej 	} else if (n > maxmaxusers) {
296c7295a4cSchristos 		cfgerror("warning: maxusers (%d) > %d", n, maxmaxusers);
2975ecc953bSthorpej 		errors--;
2985ecc953bSthorpej 	}
2995ecc953bSthorpej }
3005ecc953bSthorpej 
3015ecc953bSthorpej void
3025ecc953bSthorpej setident(const char *i)
3035ecc953bSthorpej {
3045ecc953bSthorpej 
3054a7be1f9Smrg 	if (i)
3065ecc953bSthorpej 		ident = intern(i);
3074a7be1f9Smrg 	else
3084a7be1f9Smrg 		ident = NULL;
3095ecc953bSthorpej }
3105ecc953bSthorpej 
3115ecc953bSthorpej /*
3125ecc953bSthorpej  * Define an attribute, optionally with an interface (a locator list)
3135ecc953bSthorpej  * and a set of attribute-dependencies.
3145ecc953bSthorpej  *
3155ecc953bSthorpej  * Attribute dependencies MAY NOT be interface attributes.
3165ecc953bSthorpej  *
3175ecc953bSthorpej  * Since an empty locator list is logically different from "no interface",
3185ecc953bSthorpej  * all locator lists include a dummy head node, which we discard here.
3195ecc953bSthorpej  */
3205ecc953bSthorpej int
321bb4307a6Suebayasi defattr0(const char *name, struct loclist *locs, struct attrlist *deps,
322bb4307a6Suebayasi     int devclass)
323bb4307a6Suebayasi {
324bb4307a6Suebayasi 
325bb4307a6Suebayasi 	if (locs != NULL)
326bb4307a6Suebayasi 		return defiattr(name, locs, deps, devclass);
327bb4307a6Suebayasi 	else if (devclass)
328bb4307a6Suebayasi 		return defdevclass(name, locs, deps, devclass);
329bb4307a6Suebayasi 	else
330bb4307a6Suebayasi 		return defattr(name, locs, deps, devclass);
331bb4307a6Suebayasi }
332bb4307a6Suebayasi 
333bb4307a6Suebayasi int
33416a8771bSdholland defattr(const char *name, struct loclist *locs, struct attrlist *deps,
3355ecc953bSthorpej     int devclass)
3365ecc953bSthorpej {
3375ecc953bSthorpej 	struct attr *a, *dep;
3384caf067bSdholland 	struct attrlist *al;
3395ecc953bSthorpej 
3408955e3e2Schristos 	if (getrefattr(name, &a)) {
3418955e3e2Schristos 		cfgerror("attribute `%s' already defined", name);
3428955e3e2Schristos 		loclist_destroy(locs);
3438955e3e2Schristos 		return (1);
3448955e3e2Schristos 	}
3458955e3e2Schristos 	if (a == NULL)
3468955e3e2Schristos 		a = mkattr(name);
3478955e3e2Schristos 
3485ecc953bSthorpej 	/*
3495ecc953bSthorpej 	 * If this attribute depends on any others, make sure none of
3505ecc953bSthorpej 	 * the dependencies are interface attributes.
3515ecc953bSthorpej 	 */
3524caf067bSdholland 	for (al = deps; al != NULL; al = al->al_next) {
3534caf067bSdholland 		dep = al->al_this;
3545ecc953bSthorpej 		if (dep->a_iattr) {
355c7295a4cSchristos 			cfgerror("`%s' dependency `%s' is an interface "
356c7295a4cSchristos 			    "attribute", name, dep->a_name);
3575ecc953bSthorpej 			return (1);
3585ecc953bSthorpej 		}
35911a68adeSchristos 		(void)ht_insert2(attrdeptab, name, dep->a_name,
36011a68adeSchristos 		    addvalue(V_ATTRIBUTE, a, dep));
361bc707469Suebayasi 		CFGDBG(2, "attr `%s' depends on attr `%s'", name, dep->a_name);
3625ecc953bSthorpej 	}
3635ecc953bSthorpej 
3645ecc953bSthorpej 
365a949e288Suebayasi 	a->a_deps = deps;
366bb4307a6Suebayasi 	expandattr(a, NULL);
3672f2f3382Suebayasi 	CFGDBG(3, "attr `%s' defined", a->a_name);
368bb4307a6Suebayasi 
369bb4307a6Suebayasi 	return (0);
370bb4307a6Suebayasi }
371a949e288Suebayasi 
3722f2f3382Suebayasi struct attr *
3732f2f3382Suebayasi mkattr(const char *name)
3742f2f3382Suebayasi {
3752f2f3382Suebayasi 	struct attr *a;
3762f2f3382Suebayasi 
3772f2f3382Suebayasi 	a = ecalloc(1, sizeof *a);
3782f2f3382Suebayasi 	if (ht_insert(attrtab, name, a)) {
3792f2f3382Suebayasi 		free(a);
3802f2f3382Suebayasi 		return NULL;
3812f2f3382Suebayasi 	}
3822f2f3382Suebayasi 	a->a_name = name;
3832f2f3382Suebayasi 	TAILQ_INIT(&a->a_files);
3842f2f3382Suebayasi 	CFGDBG(3, "attr `%s' allocated", name);
3852f2f3382Suebayasi 
3862f2f3382Suebayasi 	return a;
3872f2f3382Suebayasi }
3882f2f3382Suebayasi 
389a949e288Suebayasi /* "interface attribute" initialization */
390bb4307a6Suebayasi int
391bb4307a6Suebayasi defiattr(const char *name, struct loclist *locs, struct attrlist *deps,
392bb4307a6Suebayasi     int devclass)
393bb4307a6Suebayasi {
394bb4307a6Suebayasi 	struct attr *a;
395bb4307a6Suebayasi 	int len;
396bb4307a6Suebayasi 	struct loclist *ll;
397bb4307a6Suebayasi 
398bb4307a6Suebayasi 	if (devclass)
399d25ad65eSchristos 		panic("%s: %s has both locators and devclass", __func__, name);
400bb4307a6Suebayasi 
401bb4307a6Suebayasi 	if (defattr(name, locs, deps, devclass) != 0)
402bb4307a6Suebayasi 		return (1);
403bb4307a6Suebayasi 
404bb4307a6Suebayasi 	a = getattr(name);
4055ecc953bSthorpej 	a->a_iattr = 1;
40616a8771bSdholland 	/* unwrap */
40716a8771bSdholland 	a->a_locs = locs->ll_next;
40816a8771bSdholland 	locs->ll_next = NULL;
40916a8771bSdholland 	loclist_destroy(locs);
410a949e288Suebayasi 	len = 0;
411a949e288Suebayasi 	for (ll = a->a_locs; ll != NULL; ll = ll->ll_next)
412a949e288Suebayasi 		len++;
413a949e288Suebayasi 	a->a_loclen = len;
414bb4307a6Suebayasi 	if (deps)
415bb4307a6Suebayasi 		CFGDBG(2, "attr `%s' iface with deps", a->a_name);
416bb4307a6Suebayasi 	return (0);
4175ecc953bSthorpej }
418a949e288Suebayasi 
419a949e288Suebayasi /* "device class" initialization */
420bb4307a6Suebayasi int
421bb4307a6Suebayasi defdevclass(const char *name, struct loclist *locs, struct attrlist *deps,
422bb4307a6Suebayasi     int devclass)
423bb4307a6Suebayasi {
424bb4307a6Suebayasi 	struct attr *a;
425bbfbbde1Schristos 	char classenum[256], *cp;
4265ecc953bSthorpej 	int errored = 0;
4275ecc953bSthorpej 
428bb4307a6Suebayasi 	if (deps)
429d25ad65eSchristos 		panic("%s: %s has both dependencies and devclass", __func__,
430d25ad65eSchristos 		    name);
431bb4307a6Suebayasi 
432bb4307a6Suebayasi 	if (defattr(name, locs, deps, devclass) != 0)
433bb4307a6Suebayasi 		return (1);
434bb4307a6Suebayasi 
435bb4307a6Suebayasi 	a = getattr(name);
436bbfbbde1Schristos 	(void)snprintf(classenum, sizeof(classenum), "DV_%s", name);
4375ecc953bSthorpej 	for (cp = classenum + 3; *cp; cp++) {
438d25ad65eSchristos 		if (!errored && (!isalnum((unsigned char)*cp) ||
439d25ad65eSchristos 		      (isalpha((unsigned char)*cp)
440d25ad65eSchristos 		      && !islower((unsigned char)*cp)))) {
441c7295a4cSchristos 			cfgerror("device class names must be "
442c7295a4cSchristos 			    "lower-case alphanumeric characters");
4435ecc953bSthorpej 			errored = 1;
4445ecc953bSthorpej 		}
44564a07b54Sjoerg 		*cp = (char)toupper((unsigned char)*cp);
4465ecc953bSthorpej 	}
4475ecc953bSthorpej 	a->a_devclass = intern(classenum);
4485ecc953bSthorpej 
4495ecc953bSthorpej 	return (0);
4505ecc953bSthorpej }
4515ecc953bSthorpej 
4525ecc953bSthorpej /*
4535ecc953bSthorpej  * Return true if the given `error object' is embedded in the given
4545ecc953bSthorpej  * pointer list.
4555ecc953bSthorpej  */
4565ecc953bSthorpej static int
4574caf067bSdholland has_errobj(struct attrlist *al, struct attr *obj)
4585ecc953bSthorpej {
4595ecc953bSthorpej 
4604caf067bSdholland 	for (; al != NULL; al = al->al_next)
4614caf067bSdholland 		if (al->al_this == obj)
4625ecc953bSthorpej 			return (1);
4635ecc953bSthorpej 	return (0);
4645ecc953bSthorpej }
4655ecc953bSthorpej 
4665ecc953bSthorpej /*
4675ecc953bSthorpej  * Return true if the given attribute is embedded in the given
4685ecc953bSthorpej  * pointer list.
4695ecc953bSthorpej  */
4705ecc953bSthorpej int
4714caf067bSdholland has_attr(struct attrlist *al, const char *attr)
4725ecc953bSthorpej {
4735ecc953bSthorpej 	struct attr *a;
4745ecc953bSthorpej 
4755ecc953bSthorpej 	if ((a = getattr(attr)) == NULL)
4765ecc953bSthorpej 		return (0);
4775ecc953bSthorpej 
4784caf067bSdholland 	for (; al != NULL; al = al->al_next)
4794caf067bSdholland 		if (al->al_this == a)
4805ecc953bSthorpej 			return (1);
4815ecc953bSthorpej 	return (0);
4825ecc953bSthorpej }
4835ecc953bSthorpej 
4845ecc953bSthorpej /*
4855ecc953bSthorpej  * Add a device base to a list in an attribute (actually, to any list).
4865ecc953bSthorpej  * Note that this does not check for duplicates, and does reverse the
4875ecc953bSthorpej  * list order, but no one cares anyway.
4885ecc953bSthorpej  */
4895ecc953bSthorpej static struct nvlist *
4905ecc953bSthorpej addtoattr(struct nvlist *l, struct devbase *dev)
4915ecc953bSthorpej {
4925ecc953bSthorpej 	struct nvlist *n;
4935ecc953bSthorpej 
4945ecc953bSthorpej 	n = newnv(NULL, NULL, dev, 0, l);
4955ecc953bSthorpej 	return (n);
4965ecc953bSthorpej }
4975ecc953bSthorpej 
4985ecc953bSthorpej /*
4995ecc953bSthorpej  * Define a device.  This may (or may not) also define an interface
5005ecc953bSthorpej  * attribute and/or refer to existing attributes.
5015ecc953bSthorpej  */
5025ecc953bSthorpej void
50316a8771bSdholland defdev(struct devbase *dev, struct loclist *loclist, struct attrlist *attrs,
5045ecc953bSthorpej        int ispseudo)
5055ecc953bSthorpej {
50616a8771bSdholland 	struct loclist *ll;
5074caf067bSdholland 	struct attrlist *al;
5085ecc953bSthorpej 
5095ecc953bSthorpej 	if (dev == &errdev)
5105ecc953bSthorpej 		goto bad;
5115ecc953bSthorpej 	if (dev->d_isdef) {
512c7295a4cSchristos 		cfgerror("redefinition of `%s'", dev->d_name);
5135ecc953bSthorpej 		goto bad;
5145ecc953bSthorpej 	}
5155ecc953bSthorpej 
5165ecc953bSthorpej 	dev->d_isdef = 1;
5175ecc953bSthorpej 	if (has_errobj(attrs, &errattr))
5185ecc953bSthorpej 		goto bad;
5195ecc953bSthorpej 
5205ecc953bSthorpej 	/*
5215ecc953bSthorpej 	 * Handle implicit attribute definition from locator list.  Do
5225ecc953bSthorpej 	 * this before scanning the `at' list so that we can have, e.g.:
5235ecc953bSthorpej 	 *	device foo at other, foo { slot = -1 }
5245ecc953bSthorpej 	 * (where you can plug in a foo-bus extender to a foo-bus).
5255ecc953bSthorpej 	 */
5265ecc953bSthorpej 	if (loclist != NULL) {
52716a8771bSdholland 		ll = loclist;
5285ecc953bSthorpej 		loclist = NULL;	/* defattr disposes of them for us */
529bb4307a6Suebayasi 		if (defiattr(dev->d_name, ll, NULL, 0))
5305ecc953bSthorpej 			goto bad;
5314caf067bSdholland 		attrs = attrlist_cons(attrs, getattr(dev->d_name));
5324caf067bSdholland 		/* This used to be stored but was never used */
5334caf067bSdholland 		/* attrs->al_name = dev->d_name; */
534c454f920Scube 	}
535c454f920Scube 
536c130d400Scube 	/*
537c130d400Scube 	 * Pseudo-devices can have children.  Consider them as
538c130d400Scube 	 * attaching at root.
539c130d400Scube 	 */
540c454f920Scube 	if (ispseudo) {
5414caf067bSdholland 		for (al = attrs; al != NULL; al = al->al_next)
5424caf067bSdholland 			if (al->al_this->a_iattr)
543c454f920Scube 				break;
5444caf067bSdholland 		if (al != NULL) {
545b66156c7Sdrochner 			if (ispseudo < 2) {
546b66156c7Sdrochner 				if (version >= 20080610)
547b66156c7Sdrochner 					cfgerror("interface attribute on "
548b66156c7Sdrochner 					 "non-device pseudo `%s'", dev->d_name);
549b66156c7Sdrochner 				else {
550b66156c7Sdrochner 					ispseudo = 2;
551b66156c7Sdrochner 				}
552b66156c7Sdrochner 			}
553c130d400Scube 			ht_insert(devroottab, dev->d_name, dev);
5545ecc953bSthorpej 		}
555b66156c7Sdrochner 	}
5565ecc953bSthorpej 
5575ecc953bSthorpej 	/* Committed!  Set up fields. */
5585ecc953bSthorpej 	dev->d_ispseudo = ispseudo;
5595ecc953bSthorpej 	dev->d_attrs = attrs;
5605ecc953bSthorpej 	dev->d_classattr = NULL;		/* for now */
561da706167Suebayasi 	CFGDBG(3, "dev `%s' defined", dev->d_name);
5625ecc953bSthorpej 
5635ecc953bSthorpej 	/*
564a2dd9676Suebayasi 	 * Implicit attribute definition for device.
565a2dd9676Suebayasi 	 */
56601b2a130Smartin 	refattr(dev->d_name);
567a2dd9676Suebayasi 
568a2dd9676Suebayasi 	/*
5695ecc953bSthorpej 	 * For each interface attribute this device refers to, add this
5705ecc953bSthorpej 	 * device to its reference list.  This makes, e.g., finding all
5715ecc953bSthorpej 	 * "scsi"s easier.
5725ecc953bSthorpej 	 *
5735ecc953bSthorpej 	 * While looking through the attributes, set up the device
5745ecc953bSthorpej 	 * class if any are devclass attributes (and error out if the
5755ecc953bSthorpej 	 * device has two classes).
5765ecc953bSthorpej 	 */
5774caf067bSdholland 	for (al = attrs; al != NULL; al = al->al_next) {
578da706167Suebayasi 		/*
579da706167Suebayasi 		 * Implicit attribute definition for device dependencies.
580da706167Suebayasi 		 */
581bc707469Suebayasi 		refattr(al->al_this->a_name);
58211a68adeSchristos 		(void)ht_insert2(attrdeptab, dev->d_name, al->al_this->a_name,
58311a68adeSchristos 			addvalue(V_DEVICE, al->al_this, dev));
584bc707469Suebayasi 		CFGDBG(2, "device `%s' depends on attr `%s'", dev->d_name,
585bc707469Suebayasi 		    al->al_this->a_name);
5865ecc953bSthorpej 	}
5875ecc953bSthorpej 	return;
5885ecc953bSthorpej  bad:
58916a8771bSdholland 	loclist_destroy(loclist);
5904caf067bSdholland 	attrlist_destroyall(attrs);
5915ecc953bSthorpej }
5925ecc953bSthorpej 
5935ecc953bSthorpej /*
5945ecc953bSthorpej  * Look up a devbase.  Also makes sure it is a reasonable name,
5955ecc953bSthorpej  * i.e., does not end in a digit or contain special characters.
5965ecc953bSthorpej  */
5975ecc953bSthorpej struct devbase *
5985ecc953bSthorpej getdevbase(const char *name)
5995ecc953bSthorpej {
600c7295a4cSchristos 	const u_char *p;
6015ecc953bSthorpej 	struct devbase *dev;
6025ecc953bSthorpej 
603c7295a4cSchristos 	p = (const u_char *)name;
6045ecc953bSthorpej 	if (!isalpha(*p))
6055ecc953bSthorpej 		goto badname;
6065ecc953bSthorpej 	while (*++p) {
6075ecc953bSthorpej 		if (!isalnum(*p) && *p != '_')
6085ecc953bSthorpej 			goto badname;
6095ecc953bSthorpej 	}
6105ecc953bSthorpej 	if (isdigit(*--p)) {
6115ecc953bSthorpej  badname:
612c7295a4cSchristos 		cfgerror("bad device base name `%s'", name);
6135ecc953bSthorpej 		return (&errdev);
6145ecc953bSthorpej 	}
6155ecc953bSthorpej 	dev = ht_lookup(devbasetab, name);
6165ecc953bSthorpej 	if (dev == NULL) {
6175ecc953bSthorpej 		dev = ecalloc(1, sizeof *dev);
6185ecc953bSthorpej 		dev->d_name = name;
6195ecc953bSthorpej 		dev->d_isdef = 0;
620d767912bSdrochner 		dev->d_major = NODEVMAJOR;
6215ecc953bSthorpej 		dev->d_attrs = NULL;
6225ecc953bSthorpej 		dev->d_ihead = NULL;
6235ecc953bSthorpej 		dev->d_ipp = &dev->d_ihead;
6245ecc953bSthorpej 		dev->d_ahead = NULL;
6255ecc953bSthorpej 		dev->d_app = &dev->d_ahead;
6265ecc953bSthorpej 		dev->d_umax = 0;
6275ecc953bSthorpej 		TAILQ_INSERT_TAIL(&allbases, dev, d_next);
6285ecc953bSthorpej 		if (ht_insert(devbasetab, name, dev))
629d25ad65eSchristos 			panic("%s: Can't insert %s", __func__, name);
630da706167Suebayasi 		CFGDBG(3, "devbase defined `%s'", dev->d_name);
6315ecc953bSthorpej 	}
6325ecc953bSthorpej 	return (dev);
6335ecc953bSthorpej }
6345ecc953bSthorpej 
6355ecc953bSthorpej /*
6365ecc953bSthorpej  * Define some of a device's allowable parent attachments.
6375ecc953bSthorpej  * There may be a list of (plain) attributes.
6385ecc953bSthorpej  */
6395ecc953bSthorpej void
6405ecc953bSthorpej defdevattach(struct deva *deva, struct devbase *dev, struct nvlist *atlist,
6414caf067bSdholland 	     struct attrlist *attrs)
6425ecc953bSthorpej {
6435ecc953bSthorpej 	struct nvlist *nv;
6444caf067bSdholland 	struct attrlist *al;
6455ecc953bSthorpej 	struct attr *a;
6465ecc953bSthorpej 
6475ecc953bSthorpej 	if (dev == &errdev)
6485ecc953bSthorpej 		goto bad;
6495ecc953bSthorpej 	if (deva == NULL)
6505ecc953bSthorpej 		deva = getdevattach(dev->d_name);
6515ecc953bSthorpej 	if (deva == &errdeva)
6525ecc953bSthorpej 		goto bad;
6535ecc953bSthorpej 	if (!dev->d_isdef) {
654c7295a4cSchristos 		cfgerror("attaching undefined device `%s'", dev->d_name);
6555ecc953bSthorpej 		goto bad;
6565ecc953bSthorpej 	}
6575ecc953bSthorpej 	if (deva->d_isdef) {
658c7295a4cSchristos 		cfgerror("redefinition of `%s'", deva->d_name);
6595ecc953bSthorpej 		goto bad;
6605ecc953bSthorpej 	}
6615ecc953bSthorpej 	if (dev->d_ispseudo) {
662c7295a4cSchristos 		cfgerror("pseudo-devices can't attach");
6635ecc953bSthorpej 		goto bad;
6645ecc953bSthorpej 	}
6655ecc953bSthorpej 
6665ecc953bSthorpej 	deva->d_isdef = 1;
6675ecc953bSthorpej 	if (has_errobj(attrs, &errattr))
6685ecc953bSthorpej 		goto bad;
6694caf067bSdholland 	for (al = attrs; al != NULL; al = al->al_next) {
6704caf067bSdholland 		a = al->al_this;
6715ecc953bSthorpej 		if (a == &errattr)
6725ecc953bSthorpej 			continue;		/* already complained */
6735ecc953bSthorpej 		if (a->a_iattr || a->a_devclass != NULL)
674c7295a4cSchristos 			cfgerror("`%s' is not a plain attribute", a->a_name);
6755ecc953bSthorpej 	}
6765ecc953bSthorpej 
6775ecc953bSthorpej 	/* Committed!  Set up fields. */
6785ecc953bSthorpej 	deva->d_attrs = attrs;
6795ecc953bSthorpej 	deva->d_atlist = atlist;
6805ecc953bSthorpej 	deva->d_devbase = dev;
6812f2f3382Suebayasi 	CFGDBG(3, "deva `%s' defined", deva->d_name);
6822f2f3382Suebayasi 
6832f2f3382Suebayasi 	/*
684a5a68b71Suebayasi 	 * Implicit attribute definition for device attachment.
6852f2f3382Suebayasi 	 */
6862f2f3382Suebayasi 	refattr(deva->d_name);
6875ecc953bSthorpej 
6885ecc953bSthorpej 	/*
6895ecc953bSthorpej 	 * Turn the `at' list into interface attributes (map each
6905ecc953bSthorpej 	 * nv_name to an attribute, or to NULL for root), and add
6915ecc953bSthorpej 	 * this device to those attributes, so that children can
6925ecc953bSthorpej 	 * be listed at this particular device if they are supported
6935ecc953bSthorpej 	 * by that attribute.
6945ecc953bSthorpej 	 */
6955ecc953bSthorpej 	for (nv = atlist; nv != NULL; nv = nv->nv_next) {
6965ecc953bSthorpej 		if (nv->nv_name == NULL)
6975ecc953bSthorpej 			nv->nv_ptr = a = NULL;	/* at root */
6985ecc953bSthorpej 		else
6995ecc953bSthorpej 			nv->nv_ptr = a = getattr(nv->nv_name);
7005ecc953bSthorpej 		if (a == &errattr)
7015ecc953bSthorpej 			continue;		/* already complained */
7025ecc953bSthorpej 
70353be83e0Schristos #if 0
7045ecc953bSthorpej 		/*
7055ecc953bSthorpej 		 * Make sure that an attachment spec doesn't
7065ecc953bSthorpej 		 * already say how to attach to this attribute.
7075ecc953bSthorpej 		 */
70853be83e0Schristos 		for (struct deva *da = dev->d_ahead; da; da = da->d_bsame)
7095ecc953bSthorpej 			if (onlist(da->d_atlist, a))
710c7295a4cSchristos 				cfgerror("attach at `%s' already done by `%s'",
7115ecc953bSthorpej 				     a ? a->a_name : "root", da->d_name);
71253be83e0Schristos #endif
7135ecc953bSthorpej 
714c130d400Scube 		if (a == NULL) {
715c130d400Scube 			ht_insert(devroottab, dev->d_name, dev);
7165ecc953bSthorpej 			continue;		/* at root; don't add */
717c130d400Scube 		}
7185ecc953bSthorpej 		if (!a->a_iattr)
719c7295a4cSchristos 			cfgerror("%s cannot be at plain attribute `%s'",
7205ecc953bSthorpej 			    dev->d_name, a->a_name);
7215ecc953bSthorpej 		else
7225ecc953bSthorpej 			a->a_devs = addtoattr(a->a_devs, dev);
7235ecc953bSthorpej 	}
7245ecc953bSthorpej 
7255ecc953bSthorpej 	/* attach to parent */
7265ecc953bSthorpej 	*dev->d_app = deva;
7275ecc953bSthorpej 	dev->d_app = &deva->d_bsame;
7285ecc953bSthorpej 	return;
7295ecc953bSthorpej  bad:
7305ecc953bSthorpej 	nvfreel(atlist);
7314caf067bSdholland 	attrlist_destroyall(attrs);
7325ecc953bSthorpej }
7335ecc953bSthorpej 
7345ecc953bSthorpej /*
7355ecc953bSthorpej  * Look up a device attachment.  Also makes sure it is a reasonable
7365ecc953bSthorpej  * name, i.e., does not contain digits or special characters.
7375ecc953bSthorpej  */
7385ecc953bSthorpej struct deva *
7395ecc953bSthorpej getdevattach(const char *name)
7405ecc953bSthorpej {
741c7295a4cSchristos 	const u_char *p;
7425ecc953bSthorpej 	struct deva *deva;
7435ecc953bSthorpej 
744c7295a4cSchristos 	p = (const u_char *)name;
7455ecc953bSthorpej 	if (!isalpha(*p))
7465ecc953bSthorpej 		goto badname;
7475ecc953bSthorpej 	while (*++p) {
7485ecc953bSthorpej 		if (!isalnum(*p) && *p != '_')
7495ecc953bSthorpej 			goto badname;
7505ecc953bSthorpej 	}
7515ecc953bSthorpej 	if (isdigit(*--p)) {
7525ecc953bSthorpej  badname:
753c7295a4cSchristos 		cfgerror("bad device attachment name `%s'", name);
7545ecc953bSthorpej 		return (&errdeva);
7555ecc953bSthorpej 	}
7565ecc953bSthorpej 	deva = ht_lookup(devatab, name);
7575ecc953bSthorpej 	if (deva == NULL) {
7585ecc953bSthorpej 		deva = ecalloc(1, sizeof *deva);
7595ecc953bSthorpej 		deva->d_name = name;
7605ecc953bSthorpej 		deva->d_bsame = NULL;
7615ecc953bSthorpej 		deva->d_isdef = 0;
7625ecc953bSthorpej 		deva->d_devbase = NULL;
7635ecc953bSthorpej 		deva->d_atlist = NULL;
7645ecc953bSthorpej 		deva->d_attrs = NULL;
7655ecc953bSthorpej 		deva->d_ihead = NULL;
7665ecc953bSthorpej 		deva->d_ipp = &deva->d_ihead;
7675ecc953bSthorpej 		TAILQ_INSERT_TAIL(&alldevas, deva, d_next);
7685ecc953bSthorpej 		if (ht_insert(devatab, name, deva))
769d25ad65eSchristos 			panic("%s: Can't insert %s", __func__, name);
7705ecc953bSthorpej 	}
7715ecc953bSthorpej 	return (deva);
7725ecc953bSthorpej }
7735ecc953bSthorpej 
7745ecc953bSthorpej /*
7755ecc953bSthorpej  * Look up an attribute.
7765ecc953bSthorpej  */
7775ecc953bSthorpej struct attr *
7785ecc953bSthorpej getattr(const char *name)
7795ecc953bSthorpej {
7805ecc953bSthorpej 	struct attr *a;
7815ecc953bSthorpej 
7825ecc953bSthorpej 	if ((a = ht_lookup(attrtab, name)) == NULL) {
783c7295a4cSchristos 		cfgerror("undefined attribute `%s'", name);
7845ecc953bSthorpej 		a = &errattr;
7855ecc953bSthorpej 	}
7865ecc953bSthorpej 	return (a);
7875ecc953bSthorpej }
7885ecc953bSthorpej 
7895ecc953bSthorpej /*
7902f2f3382Suebayasi  * Implicit attribute definition.
7912f2f3382Suebayasi  */
79251deed3eSuebayasi struct attr *
7932f2f3382Suebayasi refattr(const char *name)
7942f2f3382Suebayasi {
79551deed3eSuebayasi 	struct attr *a;
7962f2f3382Suebayasi 
79751deed3eSuebayasi 	if ((a = ht_lookup(attrtab, name)) == NULL)
79851deed3eSuebayasi 		a = mkattr(name);
79951deed3eSuebayasi 	return a;
8002f2f3382Suebayasi }
8012f2f3382Suebayasi 
802bb52c144Suebayasi int
803bb52c144Suebayasi getrefattr(const char *name, struct attr **ra)
804bb52c144Suebayasi {
805bb52c144Suebayasi 	struct attr *a;
806bb52c144Suebayasi 
807bb52c144Suebayasi 	a = ht_lookup(attrtab, name);
808bb52c144Suebayasi 	if (a == NULL) {
809bb52c144Suebayasi 		*ra = NULL;
810bb52c144Suebayasi 		return (0);
811bb52c144Suebayasi 	}
812bb52c144Suebayasi 	/*
813bb52c144Suebayasi 	 * Check if the existing attr is only referenced, not really defined.
814bb52c144Suebayasi 	 */
815bb52c144Suebayasi 	if (a->a_deps == NULL &&
816bb52c144Suebayasi 	    a->a_iattr == 0 &&
817bb52c144Suebayasi 	    a->a_devclass == 0) {
818bb52c144Suebayasi 		*ra = a;
819bb52c144Suebayasi 		return (0);
820bb52c144Suebayasi 	}
821bb52c144Suebayasi 	return (1);
822bb52c144Suebayasi }
823bb52c144Suebayasi 
8242f2f3382Suebayasi /*
8255ecc953bSthorpej  * Recursively expand an attribute and its dependencies, checking for
8265ecc953bSthorpej  * cycles, and invoking a callback for each attribute found.
8275ecc953bSthorpej  */
8285ecc953bSthorpej void
8295ecc953bSthorpej expandattr(struct attr *a, void (*callback)(struct attr *))
8305ecc953bSthorpej {
8314caf067bSdholland 	struct attrlist *al;
8325ecc953bSthorpej 	struct attr *dep;
8335ecc953bSthorpej 
8345ecc953bSthorpej 	if (a->a_expanding) {
835c7295a4cSchristos 		cfgerror("circular dependency on attribute `%s'", a->a_name);
8365ecc953bSthorpej 		return;
8375ecc953bSthorpej 	}
8385ecc953bSthorpej 
8395ecc953bSthorpej 	a->a_expanding = 1;
8405ecc953bSthorpej 
8415ecc953bSthorpej 	/* First expand all of this attribute's dependencies. */
8424caf067bSdholland 	for (al = a->a_deps; al != NULL; al = al->al_next) {
8434caf067bSdholland 		dep = al->al_this;
8445ecc953bSthorpej 		expandattr(dep, callback);
8455ecc953bSthorpej 	}
8465ecc953bSthorpej 
8475ecc953bSthorpej 	/* ...and now invoke the callback for ourself. */
8485ecc953bSthorpej 	if (callback != NULL)
8495ecc953bSthorpej 		(*callback)(a);
8505ecc953bSthorpej 
8515ecc953bSthorpej 	a->a_expanding = 0;
8525ecc953bSthorpej }
8535ecc953bSthorpej 
8545ecc953bSthorpej /*
8555ecc953bSthorpej  * Set the major device number for a device, so that it can be used
8565ecc953bSthorpej  * as a root/dumps "on" device in a configuration.
8575ecc953bSthorpej  */
8585ecc953bSthorpej void
859d767912bSdrochner setmajor(struct devbase *d, devmajor_t n)
8605ecc953bSthorpej {
8615ecc953bSthorpej 
862d767912bSdrochner 	if (d != &errdev && d->d_major != NODEVMAJOR)
863d767912bSdrochner 		cfgerror("device `%s' is already major %d",
864d767912bSdrochner 		    d->d_name, d->d_major);
8655ecc953bSthorpej 	else
8665ecc953bSthorpej 		d->d_major = n;
8675ecc953bSthorpej }
8685ecc953bSthorpej 
8695ecc953bSthorpej const char *
870d767912bSdrochner major2name(devmajor_t maj)
8715ecc953bSthorpej {
8725ecc953bSthorpej 	struct devbase *dev;
8735ecc953bSthorpej 	struct devm *dm;
8745ecc953bSthorpej 
8755ecc953bSthorpej 	if (!do_devsw) {
8765ecc953bSthorpej 		TAILQ_FOREACH(dev, &allbases, d_next) {
8775ecc953bSthorpej 			if (dev->d_major == maj)
8785ecc953bSthorpej 				return (dev->d_name);
8795ecc953bSthorpej 		}
8805ecc953bSthorpej 	} else {
8815ecc953bSthorpej 		TAILQ_FOREACH(dm, &alldevms, dm_next) {
8825ecc953bSthorpej 			if (dm->dm_bmajor == maj)
8835ecc953bSthorpej 				return (dm->dm_name);
8845ecc953bSthorpej 		}
8855ecc953bSthorpej 	}
8865ecc953bSthorpej 	return (NULL);
8875ecc953bSthorpej }
8885ecc953bSthorpej 
889d767912bSdrochner devmajor_t
8905ecc953bSthorpej dev2major(struct devbase *dev)
8915ecc953bSthorpej {
8925ecc953bSthorpej 	struct devm *dm;
8935ecc953bSthorpej 
8945ecc953bSthorpej 	if (!do_devsw)
8955ecc953bSthorpej 		return (dev->d_major);
8965ecc953bSthorpej 
8975ecc953bSthorpej 	TAILQ_FOREACH(dm, &alldevms, dm_next) {
8985ecc953bSthorpej 		if (strcmp(dm->dm_name, dev->d_name) == 0)
8995ecc953bSthorpej 			return (dm->dm_bmajor);
9005ecc953bSthorpej 	}
901d767912bSdrochner 	return (NODEVMAJOR);
9025ecc953bSthorpej }
9035ecc953bSthorpej 
9045ecc953bSthorpej /*
9055ecc953bSthorpej  * Make a string description of the device at maj/min.
9065ecc953bSthorpej  */
9075ecc953bSthorpej static const char *
908d767912bSdrochner makedevstr(devmajor_t maj, devminor_t min)
9095ecc953bSthorpej {
910c7295a4cSchristos 	const char *devicename;
9115ecc953bSthorpej 	char buf[32];
9125ecc953bSthorpej 
913c7295a4cSchristos 	devicename = major2name(maj);
914c7295a4cSchristos 	if (devicename == NULL)
915d767912bSdrochner 		(void)snprintf(buf, sizeof(buf), "<%d/%d>", maj, min);
9165ecc953bSthorpej 	else
917d767912bSdrochner 		(void)snprintf(buf, sizeof(buf), "%s%d%c", devicename,
918d767912bSdrochner 		    min / maxpartitions, (min % maxpartitions) + 'a');
9195ecc953bSthorpej 
9205ecc953bSthorpej 	return (intern(buf));
9215ecc953bSthorpej }
9225ecc953bSthorpej 
9235ecc953bSthorpej /*
9245ecc953bSthorpej  * Map things like "ra0b" => makedev(major("ra"), 0*maxpartitions + 'b'-'a').
9255ecc953bSthorpej  * Handle the case where the device number is given but there is no
9265ecc953bSthorpej  * corresponding name, and map NULL to the default.
9275ecc953bSthorpej  */
9285ecc953bSthorpej static int
9295ecc953bSthorpej resolve(struct nvlist **nvp, const char *name, const char *what,
9305ecc953bSthorpej 	struct nvlist *dflt, int part)
9315ecc953bSthorpej {
9325ecc953bSthorpej 	struct nvlist *nv;
9335ecc953bSthorpej 	struct devbase *dev;
9345ecc953bSthorpej 	const char *cp;
935d767912bSdrochner 	devmajor_t maj;
936d767912bSdrochner 	devminor_t min;
9379a7f4bbaSchristos 	size_t i, l;
9385ecc953bSthorpej 	int unit;
9395ecc953bSthorpej 	char buf[NAMESIZE];
9405ecc953bSthorpej 
9415cc303e1Slukem 	if ((part -= 'a') >= maxpartitions || part < 0)
942d25ad65eSchristos 		panic("%s: Bad partition %c", __func__, part);
9435ecc953bSthorpej 	if ((nv = *nvp) == NULL) {
9445ecc953bSthorpej 		dev_t	d = NODEV;
9455ecc953bSthorpej 		/*
9465ecc953bSthorpej 		 * Apply default.  Easiest to do this by number.
9475ecc953bSthorpej 		 * Make sure to retain NODEVness, if this is dflt's disposition.
9485ecc953bSthorpej 		 */
9495cc303e1Slukem 		if ((dev_t)dflt->nv_num != NODEV) {
9500001b928Schristos 			maj = major(dflt->nv_num);
9510001b928Schristos 			min = ((minor(dflt->nv_num) / maxpartitions) *
9525ecc953bSthorpej 			    maxpartitions) + part;
9535ecc953bSthorpej 			d = makedev(maj, min);
9545ecc953bSthorpej 			cp = makedevstr(maj, min);
9555ecc953bSthorpej 		} else
9565ecc953bSthorpej 			cp = NULL;
9579a7f4bbaSchristos 		*nvp = nv = newnv(NULL, cp, NULL, (long long)d, NULL);
9585ecc953bSthorpej 	}
9595cc303e1Slukem 	if ((dev_t)nv->nv_num != NODEV) {
9605ecc953bSthorpej 		/*
9615ecc953bSthorpej 		 * By the numbers.  Find the appropriate major number
9625ecc953bSthorpej 		 * to make a name.
9635ecc953bSthorpej 		 */
9640001b928Schristos 		maj = major(nv->nv_num);
9650001b928Schristos 		min = minor(nv->nv_num);
9665ecc953bSthorpej 		nv->nv_str = makedevstr(maj, min);
9675ecc953bSthorpej 		return (0);
9685ecc953bSthorpej 	}
9695ecc953bSthorpej 
9705ecc953bSthorpej 	if (nv->nv_str == NULL || nv->nv_str == s_qmark)
9715ecc953bSthorpej 		/*
9725ecc953bSthorpej 		 * Wildcarded or unspecified; leave it as NODEV.
9735ecc953bSthorpej 		 */
9745ecc953bSthorpej 		return (0);
9755ecc953bSthorpej 
97679cd2972Smlelstv 	if (nv->nv_ptr != NULL && strcmp(nv->nv_ptr, "spec") == 0)
97779cd2972Smlelstv 		/*
97879cd2972Smlelstv 		 * spec string, interpreted by kernel
97979cd2972Smlelstv 		 */
98079cd2972Smlelstv 		return (0);
98179cd2972Smlelstv 
9825ecc953bSthorpej 	/*
9835ecc953bSthorpej 	 * The normal case: things like "ra2b".  Check for partition
9845ecc953bSthorpej 	 * suffix, remove it if there, and split into name ("ra") and
9855ecc953bSthorpej 	 * unit (2).
9865ecc953bSthorpej 	 */
9875ecc953bSthorpej 	l = i = strlen(nv->nv_str);
9885ecc953bSthorpej 	cp = &nv->nv_str[l];
9895ecc953bSthorpej 	if (l > 1 && *--cp >= 'a' && *cp < 'a' + maxpartitions &&
9905ecc953bSthorpej 	    isdigit((unsigned char)cp[-1])) {
9915ecc953bSthorpej 		l--;
9925ecc953bSthorpej 		part = *cp - 'a';
9935ecc953bSthorpej 	}
9945ecc953bSthorpej 	cp = nv->nv_str;
9955ecc953bSthorpej 	if (split(cp, l, buf, sizeof buf, &unit)) {
996c7295a4cSchristos 		cfgerror("%s: invalid %s device name `%s'", name, what, cp);
9975ecc953bSthorpej 		return (1);
9985ecc953bSthorpej 	}
9995ecc953bSthorpej 	dev = ht_lookup(devbasetab, intern(buf));
10005ecc953bSthorpej 	if (dev == NULL) {
1001c7295a4cSchristos 		cfgerror("%s: device `%s' does not exist", name, buf);
10025ecc953bSthorpej 		return (1);
10035ecc953bSthorpej 	}
10045ecc953bSthorpej 
10055ecc953bSthorpej 	/*
10065ecc953bSthorpej 	 * Check for the magic network interface attribute, and
10075ecc953bSthorpej 	 * don't bother making a device number.
10085ecc953bSthorpej 	 */
10095ecc953bSthorpej 	if (has_attr(dev->d_attrs, s_ifnet)) {
10109a7f4bbaSchristos 		nv->nv_num = (long long)NODEV;
10115ecc953bSthorpej 		nv->nv_ifunit = unit;	/* XXX XXX XXX */
10125ecc953bSthorpej 	} else {
10135ecc953bSthorpej 		maj = dev2major(dev);
10145cc303e1Slukem 		if (maj == NODEVMAJOR) {
1015c7295a4cSchristos 			cfgerror("%s: can't make %s device from `%s'",
10165ecc953bSthorpej 			    name, what, nv->nv_str);
10175ecc953bSthorpej 			return (1);
10185ecc953bSthorpej 		}
10195beb9d5aSjoerg 		nv->nv_num = (long long)makedev(maj, unit * maxpartitions + part);
10205ecc953bSthorpej 	}
10215ecc953bSthorpej 
10225ecc953bSthorpej 	nv->nv_name = dev->d_name;
10235ecc953bSthorpej 	return (0);
10245ecc953bSthorpej }
10255ecc953bSthorpej 
10265ecc953bSthorpej /*
10275ecc953bSthorpej  * Add a completed configuration to the list.
10285ecc953bSthorpej  */
10295ecc953bSthorpej void
10305ecc953bSthorpej addconf(struct config *cf0)
10315ecc953bSthorpej {
10325ecc953bSthorpej 	struct config *cf;
10335ecc953bSthorpej 	const char *name;
10345ecc953bSthorpej 
10355ecc953bSthorpej 	name = cf0->cf_name;
10365ecc953bSthorpej 	cf = ecalloc(1, sizeof *cf);
10375ecc953bSthorpej 	if (ht_insert(cfhashtab, name, cf)) {
1038c7295a4cSchristos 		cfgerror("configuration `%s' already defined", name);
10395ecc953bSthorpej 		free(cf);
10405ecc953bSthorpej 		goto bad;
10415ecc953bSthorpej 	}
10425ecc953bSthorpej 	*cf = *cf0;
10435ecc953bSthorpej 
10445ecc953bSthorpej 	/*
10455ecc953bSthorpej 	 * Resolve the root device.
10465ecc953bSthorpej 	 */
1047cd429362Serh 	if (cf->cf_root == NULL) {
1048c7295a4cSchristos 		cfgerror("%s: no root device specified", name);
10495ecc953bSthorpej 		goto bad;
10505ecc953bSthorpej 	}
1051cd429362Serh 	if (cf->cf_root && cf->cf_root->nv_str != s_qmark) {
1052cd429362Serh 		struct nvlist *nv;
1053cd429362Serh 		nv = cf->cf_root;
10545ecc953bSthorpej 		if (resolve(&cf->cf_root, name, "root", nv, 'a'))
10555ecc953bSthorpej 			goto bad;
10565ecc953bSthorpej 	}
10575ecc953bSthorpej 
10585ecc953bSthorpej 	/*
10595ecc953bSthorpej 	 * Resolve the dump device.
10605ecc953bSthorpej 	 */
10615ecc953bSthorpej 	if (cf->cf_dump == NULL || cf->cf_dump->nv_str == s_qmark) {
10625ecc953bSthorpej 		/*
10635ecc953bSthorpej 		 * Wildcarded dump device is equivalent to unspecified.
10645ecc953bSthorpej 		 */
10655ecc953bSthorpej 		cf->cf_dump = NULL;
10665ecc953bSthorpej 	} else if (cf->cf_dump->nv_str == s_none) {
10675ecc953bSthorpej 		/*
10685ecc953bSthorpej 		 * Operator has requested that no dump device should be
10695ecc953bSthorpej 		 * configured; do nothing.
10705ecc953bSthorpej 		 */
10715ecc953bSthorpej 	} else {
10725ecc953bSthorpej 		if (resolve(&cf->cf_dump, name, "dumps", cf->cf_dump, 'b'))
10735ecc953bSthorpej 			goto bad;
10745ecc953bSthorpej 	}
10755ecc953bSthorpej 
10765ecc953bSthorpej 	/* Wildcarded fstype is `unspecified'. */
10775ecc953bSthorpej 	if (cf->cf_fstype == s_qmark)
10785ecc953bSthorpej 		cf->cf_fstype = NULL;
10795ecc953bSthorpej 
10805ecc953bSthorpej 	TAILQ_INSERT_TAIL(&allcf, cf, cf_next);
10815ecc953bSthorpej 	return;
10825ecc953bSthorpej  bad:
10835ecc953bSthorpej 	nvfreel(cf0->cf_root);
10845ecc953bSthorpej 	nvfreel(cf0->cf_dump);
10855ecc953bSthorpej }
10865ecc953bSthorpej 
10875ecc953bSthorpej void
10885ecc953bSthorpej setconf(struct nvlist **npp, const char *what, struct nvlist *v)
10895ecc953bSthorpej {
10905ecc953bSthorpej 
10915ecc953bSthorpej 	if (*npp != NULL) {
1092c7295a4cSchristos 		cfgerror("duplicate %s specification", what);
10935ecc953bSthorpej 		nvfreel(v);
10945ecc953bSthorpej 	} else
10955ecc953bSthorpej 		*npp = v;
10965ecc953bSthorpej }
10975ecc953bSthorpej 
10985ecc953bSthorpej void
10993dbd1573Schristos delconf(const char *name, int nowarn)
110042b52b8aScube {
110142b52b8aScube 	struct config *cf;
110242b52b8aScube 
110375eb07f2Suebayasi 	CFGDBG(5, "deselecting config `%s'", name);
110442b52b8aScube 	if (ht_lookup(cfhashtab, name) == NULL) {
11053dbd1573Schristos 		if (!nowarn)
1106c7295a4cSchristos 			cfgerror("configuration `%s' undefined", name);
110742b52b8aScube 		return;
110842b52b8aScube 	}
110942b52b8aScube 	(void)ht_remove(cfhashtab, name);
111042b52b8aScube 
111142b52b8aScube 	TAILQ_FOREACH(cf, &allcf, cf_next)
111242b52b8aScube 		if (!strcmp(cf->cf_name, name))
111342b52b8aScube 			break;
111442b52b8aScube 	if (cf == NULL)
1115d25ad65eSchristos 		panic("%s: lost configuration for %s", __func__, name);
111642b52b8aScube 
111742b52b8aScube 	TAILQ_REMOVE(&allcf, cf, cf_next);
111842b52b8aScube }
111942b52b8aScube 
112042b52b8aScube void
11215ecc953bSthorpej setfstype(const char **fstp, const char *v)
11225ecc953bSthorpej {
11235ecc953bSthorpej 
11245ecc953bSthorpej 	if (*fstp != NULL) {
1125c7295a4cSchristos 		cfgerror("multiple fstype specifications");
11265ecc953bSthorpej 		return;
11275ecc953bSthorpej 	}
11285ecc953bSthorpej 
11295ecc953bSthorpej 	if (v != s_qmark && OPT_FSOPT(v)) {
1130c7295a4cSchristos 		cfgerror("\"%s\" is not a configured file system", v);
11315ecc953bSthorpej 		return;
11325ecc953bSthorpej 	}
11335ecc953bSthorpej 
11345ecc953bSthorpej 	*fstp = v;
11355ecc953bSthorpej }
11365ecc953bSthorpej 
11375ecc953bSthorpej static struct devi *
11385ecc953bSthorpej newdevi(const char *name, int unit, struct devbase *d)
11395ecc953bSthorpej {
11405ecc953bSthorpej 	struct devi *i;
11415ecc953bSthorpej 
11425ecc953bSthorpej 	i = ecalloc(1, sizeof *i);
11435ecc953bSthorpej 	i->i_name = name;
11445ecc953bSthorpej 	i->i_unit = unit;
11455ecc953bSthorpej 	i->i_base = d;
11465ecc953bSthorpej 	i->i_bsame = NULL;
11475ecc953bSthorpej 	i->i_asame = NULL;
11485ecc953bSthorpej 	i->i_alias = NULL;
11495ecc953bSthorpej 	i->i_at = NULL;
11505ecc953bSthorpej 	i->i_pspec = NULL;
11515ecc953bSthorpej 	i->i_atdeva = NULL;
11525ecc953bSthorpej 	i->i_locs = NULL;
11535ecc953bSthorpej 	i->i_cfflags = 0;
11545ecc953bSthorpej 	i->i_lineno = currentline();
11550dbd1c0eScube 	i->i_srcfile = yyfile;
11567aa6070dScube 	i->i_active = DEVI_ORPHAN; /* Proper analysis comes later */
1157a31ff6b4Scube 	i->i_level = devilevel;
115890ac64deSpooka 	i->i_pseudoroot = 0;
11595ecc953bSthorpej 	if (unit >= d->d_umax)
11605ecc953bSthorpej 		d->d_umax = unit + 1;
11615ecc953bSthorpej 	return (i);
11625ecc953bSthorpej }
11635ecc953bSthorpej 
116453be83e0Schristos static struct attr *
116553be83e0Schristos finddevattr(const char *name, const char *at, struct devbase *ib,
116653be83e0Schristos     struct devbase **ab, int *atunit)
11675ecc953bSthorpej {
11685ecc953bSthorpej 	const char *cp;
11695ecc953bSthorpej 	char atbuf[NAMESIZE];
117053be83e0Schristos 	struct attrlist *al;
117153be83e0Schristos 	struct attr *attr;
11725ecc953bSthorpej 
11735ecc953bSthorpej 	if (at == NULL) {
117453be83e0Schristos 		*ab = NULL;
117553be83e0Schristos 		*atunit = -1;
117653be83e0Schristos 		return &errattr;	/* a convenient "empty" attr */
11775ecc953bSthorpej 	}
117853be83e0Schristos 	if (split(at, strlen(at), atbuf, sizeof atbuf, atunit)) {
1179c7295a4cSchristos 		cfgerror("invalid attachment name `%s'", at);
11805ecc953bSthorpej 		/* (void)getdevi(name); -- ??? */
118153be83e0Schristos 		return NULL;
11825ecc953bSthorpej 	}
11835ecc953bSthorpej 
11845ecc953bSthorpej 	/*
11855ecc953bSthorpej 	 * Devices can attach to two types of things: Attributes,
11865ecc953bSthorpej 	 * and other devices (which have the appropriate attributes
11875ecc953bSthorpej 	 * to allow attachment).
11885ecc953bSthorpej 	 *
11895ecc953bSthorpej 	 * (1) If we're attached to an attribute, then we don't need
11905ecc953bSthorpej 	 *     look at the parent base device to see what attributes
11915ecc953bSthorpej 	 *     it has, and make sure that we can attach to them.
11925ecc953bSthorpej 	 *
11935ecc953bSthorpej 	 * (2) If we're attached to a real device (i.e. named in
11945ecc953bSthorpej 	 *     the config file), we want to remember that so that
11955ecc953bSthorpej 	 *     at cross-check time, if the device we're attached to
11965ecc953bSthorpej 	 *     is missing but other devices which also provide the
11975ecc953bSthorpej 	 *     attribute are present, we don't get a false "OK."
11985ecc953bSthorpej 	 *
11995ecc953bSthorpej 	 * (3) If the thing we're attached to is an attribute
12005ecc953bSthorpej 	 *     but is actually named in the config file, we still
12015ecc953bSthorpej 	 *     have to remember its devbase.
12025ecc953bSthorpej 	 */
12035ecc953bSthorpej 	cp = intern(atbuf);
12045ecc953bSthorpej 
12055ecc953bSthorpej 	/* Figure out parent's devbase, to satisfy case (3). */
120653be83e0Schristos 	*ab = ht_lookup(devbasetab, cp);
12075ecc953bSthorpej 
12085ecc953bSthorpej 	/* Find out if it's an attribute. */
12095ecc953bSthorpej 	attr = ht_lookup(attrtab, cp);
12105ecc953bSthorpej 
12115ecc953bSthorpej 	/* Make sure we're _really_ attached to the attr.  Case (1). */
12125ecc953bSthorpej 	if (attr != NULL && onlist(attr->a_devs, ib))
121353be83e0Schristos 		return attr;
12145ecc953bSthorpej 
12155ecc953bSthorpej 	/*
12165ecc953bSthorpej 	 * Else a real device, and not just an attribute.  Case (2).
12175ecc953bSthorpej 	 *
12185ecc953bSthorpej 	 * Have to work a bit harder to see whether we have
12195ecc953bSthorpej 	 * something like "tg0 at esp0" (where esp is merely
12205ecc953bSthorpej 	 * not an attribute) or "tg0 at nonesuch0" (where
12215ecc953bSthorpej 	 * nonesuch is not even a device).
12225ecc953bSthorpej 	 */
122353be83e0Schristos 	if (*ab == NULL) {
122453be83e0Schristos 		cfgerror("%s at %s: `%s' unknown", name, at, atbuf);
122553be83e0Schristos 		return NULL;
12265ecc953bSthorpej 	}
12275ecc953bSthorpej 
12285ecc953bSthorpej 	/*
12295ecc953bSthorpej 	 * See if the named parent carries an attribute
12305ecc953bSthorpej 	 * that allows it to supervise device ib.
12315ecc953bSthorpej 	 */
123253be83e0Schristos 	for (al = (*ab)->d_attrs; al != NULL; al = al->al_next) {
12334caf067bSdholland 		attr = al->al_this;
12345ecc953bSthorpej 		if (onlist(attr->a_devs, ib))
123553be83e0Schristos 			return attr;
12365ecc953bSthorpej 	}
1237c7295a4cSchristos 	cfgerror("`%s' cannot attach to `%s'", ib->d_name, atbuf);
123853be83e0Schristos 	return NULL;
123953be83e0Schristos }
124053be83e0Schristos 
124153be83e0Schristos /*
124253be83e0Schristos  * Add the named device as attaching to the named attribute (or perhaps
124353be83e0Schristos  * another device instead) plus unit number.
124453be83e0Schristos  */
124553be83e0Schristos void
124653be83e0Schristos adddev(const char *name, const char *at, struct loclist *loclist, int flags)
124753be83e0Schristos {
124853be83e0Schristos 	struct devi *i;		/* the new instance */
124953be83e0Schristos 	struct pspec *p;	/* and its pspec */
125053be83e0Schristos 	struct attr *attr;	/* attribute that allows attach */
125153be83e0Schristos 	struct devbase *ib;	/* i->i_base */
125253be83e0Schristos 	struct devbase *ab;	/* not NULL => at another dev */
125353be83e0Schristos 	struct deva *iba;	/* devbase attachment used */
125453be83e0Schristos 	struct deva *lastiba;
1255d25ad65eSchristos 	int atunit, first;
125653be83e0Schristos 
125753be83e0Schristos 	lastiba = NULL;
125853be83e0Schristos 	if ((i = getdevi(name)) == NULL)
125953be83e0Schristos 		goto bad;
126053be83e0Schristos 	ib = i->i_base;
126153be83e0Schristos 	attr = finddevattr(name, at, ib, &ab, &atunit);
126253be83e0Schristos 	if (attr == NULL) {
12635ec393e9Scube 		i->i_active = DEVI_BROKEN;
12645ecc953bSthorpej 		goto bad;
126553be83e0Schristos 	}
12665ecc953bSthorpej 
126753be83e0Schristos 	for (lastiba = ib->d_ahead; lastiba; lastiba = iba->d_bsame) {
126853be83e0Schristos 		for (iba = lastiba; iba != NULL; iba = iba->d_bsame)
126953be83e0Schristos 			if (onlist(iba->d_atlist,
127053be83e0Schristos 			    attr == &errattr ? NULL : attr))
12715ecc953bSthorpej 				break;
127253be83e0Schristos 
1273d25ad65eSchristos 		first = lastiba == ib->d_ahead;
127453be83e0Schristos 		if (iba == NULL) {
1275d25ad65eSchristos 			if (!first)
127653be83e0Schristos 				goto bad;
127753be83e0Schristos 			if (attr != &errattr) {
1278d25ad65eSchristos 				panic("%s: can't figure out attachment",
1279d25ad65eSchristos 				    __func__);
128053be83e0Schristos 			} else {
128153be83e0Schristos 				cfgerror("`%s' cannot attach to the root",
128253be83e0Schristos 				    ib->d_name);
128353be83e0Schristos 				i->i_active = DEVI_BROKEN;
12845ecc953bSthorpej 			}
128553be83e0Schristos 		}
128653be83e0Schristos 		// get a new one if it is not the first time
1287d25ad65eSchristos 		if (!first && (i = getdevi(name)) == NULL)
128853be83e0Schristos 			goto bad;
128953be83e0Schristos 
129053be83e0Schristos 		if (attr != &errattr) {
129153be83e0Schristos 			/*
129253be83e0Schristos 			 * Find the parent spec.  If a matching one has not
129353be83e0Schristos 			 * yet been created, create one.
129453be83e0Schristos 			 *
129553be83e0Schristos 			 * XXX: This creates multiple pspecs that look the
129653be83e0Schristos 			 * same in the config file and could be merged.
129753be83e0Schristos 			 */
1298*4b9e5cb1Schristos 			p = getpspec(attr, ab, atunit, first);
129953be83e0Schristos 			p->p_devs = newnv(NULL, NULL, i, 0, p->p_devs);
1300d25ad65eSchristos 		} else
1301d25ad65eSchristos 			p = NULL;
130253be83e0Schristos 
13035ec393e9Scube 		if ((i->i_locs = fixloc(name, attr, loclist)) == NULL) {
13045ec393e9Scube 			i->i_active = DEVI_BROKEN;
13055ecc953bSthorpej 			goto bad;
13065ec393e9Scube 		}
13075ecc953bSthorpej 		i->i_at = at;
13085ecc953bSthorpej 		i->i_pspec = p;
13095ecc953bSthorpej 		i->i_atdeva = iba;
13105ecc953bSthorpej 		i->i_cfflags = flags;
131153be83e0Schristos 		CFGDBG(3, "devi `%s' at '%s' added", i->i_name, iba->d_name);
13125ecc953bSthorpej 
13135ecc953bSthorpej 		*iba->d_ipp = i;
13145ecc953bSthorpej 		iba->d_ipp = &i->i_asame;
131553be83e0Schristos 	}
13165ecc953bSthorpej 
13175ecc953bSthorpej 	/* all done, fall into ... */
13185ecc953bSthorpej  bad:
131916a8771bSdholland 	loclist_destroy(loclist);
13205ecc953bSthorpej 	return;
13215ecc953bSthorpej }
13225ecc953bSthorpej 
13235ecc953bSthorpej void
13243dbd1573Schristos deldevi(const char *name, const char *at, int nowarn)
13255ecc953bSthorpej {
13267b7c582aScube 	struct devi *firsti, *i;
13275ecc953bSthorpej 	struct devbase *d;
13285ecc953bSthorpej 	int unit;
13295ecc953bSthorpej 	char base[NAMESIZE];
13305ecc953bSthorpej 
133175eb07f2Suebayasi 	CFGDBG(5, "deselecting devi `%s'", name);
13325ecc953bSthorpej 	if (split(name, strlen(name), base, sizeof base, &unit)) {
13333dbd1573Schristos 		if (!nowarn) {
1334c7295a4cSchristos 			cfgerror("invalid device name `%s'", name);
13355ecc953bSthorpej 			return;
13365ecc953bSthorpej 		}
13373dbd1573Schristos 	}
13385ecc953bSthorpej 	d = ht_lookup(devbasetab, intern(base));
13395ecc953bSthorpej 	if (d == NULL) {
13403dbd1573Schristos 		if (!nowarn)
1341c7295a4cSchristos 			cfgerror("%s: unknown device `%s'", name, base);
13425ecc953bSthorpej 		return;
13435ecc953bSthorpej 	}
13445ecc953bSthorpej 	if (d->d_ispseudo) {
1345c7295a4cSchristos 		cfgerror("%s: %s is a pseudo-device", name, base);
13465ecc953bSthorpej 		return;
13475ecc953bSthorpej 	}
13485ecc953bSthorpej 	if ((firsti = ht_lookup(devitab, name)) == NULL) {
1349c7295a4cSchristos 		cfgerror("`%s' not defined", name);
13505ecc953bSthorpej 		return;
13515ecc953bSthorpej 	}
13527b7c582aScube 	if (at == NULL && firsti->i_at == NULL) {
1353e499d8b5Scube 		/* 'at root' */
13547b7c582aScube 		remove_devi(firsti);
13557b7c582aScube 		return;
13567b7c582aScube 	} else if (at != NULL)
13577b7c582aScube 		for (i = firsti; i != NULL; i = i->i_alias)
1358bfc250f2Scube 			if (i->i_active != DEVI_BROKEN &&
1359bfc250f2Scube 			    strcmp(at, i->i_at) == 0) {
13607b7c582aScube 				remove_devi(i);
13615ecc953bSthorpej 				return;
13625ecc953bSthorpej 			}
1363c7295a4cSchristos 	cfgerror("`%s' at `%s' not found", name, at ? at : "root");
1364e499d8b5Scube }
13655ecc953bSthorpej 
13667b7c582aScube static void
1367511fbc70Schristos remove_pspec(struct devi *i)
1368511fbc70Schristos {
1369511fbc70Schristos 	struct pspec *p = i->i_pspec;
1370511fbc70Schristos 	struct nvlist *nv, *onv;
1371511fbc70Schristos 
1372511fbc70Schristos 	if (p == NULL)
1373511fbc70Schristos 		return;
1374511fbc70Schristos 
1375511fbc70Schristos 	/* Double-linked nvlist anyone? */
1376511fbc70Schristos 	for (nv = p->p_devs; nv->nv_next != NULL; nv = nv->nv_next) {
1377511fbc70Schristos 		if (nv->nv_next && nv->nv_next->nv_ptr == i) {
1378511fbc70Schristos 			onv = nv->nv_next;
1379511fbc70Schristos 			nv->nv_next = onv->nv_next;
1380511fbc70Schristos 			nvfree(onv);
1381511fbc70Schristos 			break;
1382511fbc70Schristos 		}
1383511fbc70Schristos 		if (nv->nv_ptr == i) {
1384511fbc70Schristos 			/* nv is p->p_devs in that case */
1385511fbc70Schristos 			p->p_devs = nv->nv_next;
1386511fbc70Schristos 			nvfree(nv);
1387511fbc70Schristos 			break;
1388511fbc70Schristos 		}
1389511fbc70Schristos 	}
1390511fbc70Schristos 	if (p->p_devs == NULL)
1391511fbc70Schristos 		TAILQ_REMOVE(&allpspecs, p, p_list);
1392511fbc70Schristos }
1393511fbc70Schristos 
1394511fbc70Schristos static void
13957b7c582aScube remove_devi(struct devi *i)
13967b7c582aScube {
13977b7c582aScube 	struct devbase *d = i->i_base;
13987b7c582aScube 	struct devi *f, *j, **ppi;
13997b7c582aScube 	struct deva *iba;
14007b7c582aScube 
14018d928a75Suebayasi 	CFGDBG(5, "removing devi `%s'", i->i_name);
14027b7c582aScube 	f = ht_lookup(devitab, i->i_name);
14036a6299ebScube 	if (f == NULL)
1404d25ad65eSchristos 		panic("%s: instance %s disappeared from devitab", __func__,
14056a6299ebScube 		    i->i_name);
14067b7c582aScube 
1407bfc250f2Scube 	if (i->i_active == DEVI_BROKEN) {
1408bfc250f2Scube 		cfgerror("not removing broken instance `%s'", i->i_name);
1409bfc250f2Scube 		return;
1410bfc250f2Scube 	}
1411bfc250f2Scube 
1412e499d8b5Scube 	/*
1413e499d8b5Scube 	 * We have the device instance, i.
1414e499d8b5Scube 	 * We have to:
1415e499d8b5Scube 	 *   - delete the alias
1416e499d8b5Scube 	 *
1417e499d8b5Scube 	 *      If the devi was an alias of an already listed devi, all is
1418e499d8b5Scube 	 *      good we don't have to do more.
1419e499d8b5Scube 	 *      If it was the first alias, we have to replace i's entry in
1420e499d8b5Scube 	 *      d's list by its first alias.
1421e499d8b5Scube 	 *      If it was the only entry, we must remove i's entry from d's
1422e499d8b5Scube 	 *      list.
1423e499d8b5Scube 	 */
14247b7c582aScube 	if (i != f) {
1425db7dbdacSuebayasi 		for (j = f; j->i_alias != i; j = j->i_alias)
1426db7dbdacSuebayasi 			continue;
14277b7c582aScube 		j->i_alias = i->i_alias;
14283b405775Scube 	} else {
14293b405775Scube 		if (i->i_alias == NULL) {
1430e499d8b5Scube 			/* No alias, must unlink the entry from devitab */
14317b7c582aScube 			ht_remove(devitab, i->i_name);
14327b7c582aScube 			j = i->i_bsame;
14333b405775Scube 		} else {
1434e499d8b5Scube 			/* Or have the first alias replace i in d's list */
1435e499d8b5Scube 			i->i_alias->i_bsame = i->i_bsame;
14367b7c582aScube 			j = i->i_alias;
14377b7c582aScube 			if (i == f)
14387b7c582aScube 				ht_replace(devitab, i->i_name, i->i_alias);
14393b405775Scube 		}
14403b405775Scube 
1441e499d8b5Scube 		/*
1442e499d8b5Scube 		 *   - remove/replace the instance from the devbase's list
1443e499d8b5Scube 		 *
1444e499d8b5Scube 		 * A double-linked list would make this much easier.  Oh, well,
1445e499d8b5Scube 		 * what is done is done.
1446e499d8b5Scube 		 */
1447e499d8b5Scube 		for (ppi = &d->d_ihead;
1448e499d8b5Scube 		    *ppi != NULL && *ppi != i && (*ppi)->i_bsame != i;
1449db7dbdacSuebayasi 		    ppi = &(*ppi)->i_bsame)
1450db7dbdacSuebayasi 			continue;
1451e499d8b5Scube 		if (*ppi == NULL)
1452d25ad65eSchristos 			panic("%s: dev (%s) doesn't list the devi (%s at %s)",
1453d25ad65eSchristos 			    __func__, d->d_name, i->i_name, i->i_at);
14547b7c582aScube 		f = *ppi;
14557b7c582aScube 		if (f == i)
1456c3414672Scube 			/* That implies d->d_ihead == i */
14577b7c582aScube 			*ppi = j;
1458e499d8b5Scube 		else
14597b7c582aScube 			(*ppi)->i_bsame = j;
1460e499d8b5Scube 		if (d->d_ipp == &i->i_bsame) {
14613b405775Scube 			if (i->i_alias == NULL) {
14627b7c582aScube 				if (f == i)
1463e499d8b5Scube 					d->d_ipp = &d->d_ihead;
1464e499d8b5Scube 				else
14657b7c582aScube 					d->d_ipp = &f->i_bsame;
14663b405775Scube 			} else
14673b405775Scube 				d->d_ipp = &i->i_alias->i_bsame;
1468e499d8b5Scube 		}
1469e499d8b5Scube 	}
1470e499d8b5Scube 	/*
1471e499d8b5Scube 	 *   - delete the attachment instance
1472e499d8b5Scube 	 */
1473e499d8b5Scube 	iba = i->i_atdeva;
1474e499d8b5Scube 	for (ppi = &iba->d_ihead;
1475e499d8b5Scube 	    *ppi != NULL && *ppi != i && (*ppi)->i_asame != i;
1476db7dbdacSuebayasi 	    ppi = &(*ppi)->i_asame)
1477db7dbdacSuebayasi 		continue;
1478e499d8b5Scube 	if (*ppi == NULL)
1479d25ad65eSchristos 		panic("%s: deva (%s) doesn't list the devi (%s)", __func__,
1480e499d8b5Scube 		    iba->d_name, i->i_name);
14817b7c582aScube 	f = *ppi;
14827b7c582aScube 	if (f == i)
1483c3414672Scube 		/* That implies iba->d_ihead == i */
1484e499d8b5Scube 		*ppi = i->i_asame;
1485e499d8b5Scube 	else
1486e499d8b5Scube 		(*ppi)->i_asame = i->i_asame;
1487e499d8b5Scube 	if (iba->d_ipp == &i->i_asame) {
14887b7c582aScube 		if (f == i)
1489e499d8b5Scube 			iba->d_ipp = &iba->d_ihead;
1490e499d8b5Scube 		else
14917b7c582aScube 			iba->d_ipp = &f->i_asame;
1492e499d8b5Scube 	}
1493e499d8b5Scube 	/*
1494e499d8b5Scube 	 *   - delete the pspec
1495e499d8b5Scube 	 */
1496511fbc70Schristos 	remove_pspec(i);
1497e499d8b5Scube 
1498e499d8b5Scube 	/*
1499e499d8b5Scube 	 *   - delete the alldevi entry
1500e499d8b5Scube 	 */
1501e499d8b5Scube 	TAILQ_REMOVE(&alldevi, i, i_next);
1502e499d8b5Scube 	ndevi--;
15037aa6070dScube 	/*
15047aa6070dScube 	 * Put it in deaddevitab
1505a31ff6b4Scube 	 *
1506a31ff6b4Scube 	 * Each time a devi is removed, devilevel is increased so that later on
1507a31ff6b4Scube 	 * it is possible to tell if an instance was added before or after the
1508a31ff6b4Scube 	 * removal of its parent.
1509a31ff6b4Scube 	 *
1510a31ff6b4Scube 	 * For active instances, i_level contains the number of devi removed so
1511a31ff6b4Scube 	 * far, and for dead devis, it contains its index.
15127aa6070dScube 	 */
1513a31ff6b4Scube 	i->i_level = devilevel++;
15147aa6070dScube 	i->i_alias = NULL;
15157aa6070dScube 	f = ht_lookup(deaddevitab, i->i_name);
15167aa6070dScube 	if (f == NULL) {
15177aa6070dScube 		if (ht_insert(deaddevitab, i->i_name, i))
1518d25ad65eSchristos 			panic("%s: can't add %s to deaddevitab", __func__,
15197aa6070dScube 			    i->i_name);
15207aa6070dScube 	} else {
1521db7dbdacSuebayasi 		for (j = f; j->i_alias != NULL; j = j->i_alias)
1522db7dbdacSuebayasi 			continue;
15237aa6070dScube 		j->i_alias = i;
15247aa6070dScube 	}
1525e499d8b5Scube 	/*
15265d1e8b27Swiz 	 *   - reconstruct d->d_umax
1527e499d8b5Scube 	 */
1528e499d8b5Scube 	d->d_umax = 0;
1529e499d8b5Scube 	for (i = d->d_ihead; i != NULL; i = i->i_bsame)
1530e499d8b5Scube 		if (i->i_unit >= d->d_umax)
1531e499d8b5Scube 			d->d_umax = i->i_unit + 1;
15325ecc953bSthorpej }
15335ecc953bSthorpej 
15345ecc953bSthorpej void
15353dbd1573Schristos deldeva(const char *at, int nowarn)
15367b7c582aScube {
15377b7c582aScube 	int unit;
15387b7c582aScube 	const char *cp;
15397b7c582aScube 	struct devbase *d, *ad;
15407b7c582aScube 	struct devi *i, *j;
15417b7c582aScube 	struct attr *a;
15427b7c582aScube 	struct pspec *p;
15437b7c582aScube 	struct nvlist *nv, *stack = NULL;
15447b7c582aScube 
15457b7c582aScube 	if (at == NULL) {
15467b7c582aScube 		TAILQ_FOREACH(i, &alldevi, i_next)
15477b7c582aScube 			if (i->i_at == NULL)
15487b7c582aScube 				stack = newnv(NULL, NULL, i, 0, stack);
15497b7c582aScube 	} else {
15509a7f4bbaSchristos 		size_t l;
15517b7c582aScube 
155275eb07f2Suebayasi 		CFGDBG(5, "deselecting deva `%s'", at);
15539a7f4bbaSchristos 		if (at[0] == '\0')
15549a7f4bbaSchristos 			goto out;
15559a7f4bbaSchristos 
15567b7c582aScube 		l = strlen(at) - 1;
15577b7c582aScube 		if (at[l] == '?' || isdigit((unsigned char)at[l])) {
15587b7c582aScube 			char base[NAMESIZE];
15597b7c582aScube 
15607b7c582aScube 			if (split(at, l+1, base, sizeof base, &unit)) {
15619a7f4bbaSchristos out:
1562c7295a4cSchristos 				cfgerror("invalid attachment name `%s'", at);
15637b7c582aScube 				return;
15647b7c582aScube 			}
15657b7c582aScube 			cp = intern(base);
15667b7c582aScube 		} else {
15677b7c582aScube 			cp = intern(at);
15687b7c582aScube 			unit = STAR;
15697b7c582aScube 		}
15707b7c582aScube 
15717b7c582aScube 		ad = ht_lookup(devbasetab, cp);
15727b7c582aScube 		a = ht_lookup(attrtab, cp);
15737b7c582aScube 		if (a == NULL) {
1574c7295a4cSchristos 			cfgerror("unknown attachment attribute or device `%s'",
15757b7c582aScube 			    cp);
15767b7c582aScube 			return;
15777b7c582aScube 		}
15787b7c582aScube 		if (!a->a_iattr) {
1579c7295a4cSchristos 			cfgerror("plain attribute `%s' cannot have children",
15807b7c582aScube 			    a->a_name);
15817b7c582aScube 			return;
15827b7c582aScube 		}
15837b7c582aScube 
15847b7c582aScube 		/*
15857b7c582aScube 		 * remove_devi() makes changes to the devbase's list and the
15867b7c582aScube 		 * alias list, * so the actual deletion of the instances must
15877b7c582aScube 		 * be delayed.
15887b7c582aScube 		 */
15897b7c582aScube 		for (nv = a->a_devs; nv != NULL; nv = nv->nv_next) {
15907b7c582aScube 			d = nv->nv_ptr;
15917b7c582aScube 			for (i = d->d_ihead; i != NULL; i = i->i_bsame)
15927b7c582aScube 				for (j = i; j != NULL; j = j->i_alias) {
15937b7c582aScube 					/* Ignore devices at root */
15947b7c582aScube 					if (j->i_at == NULL)
15957b7c582aScube 						continue;
15967b7c582aScube 					p = j->i_pspec;
15977b7c582aScube 					/*
15987b7c582aScube 					 * There are three cases:
15997b7c582aScube 					 *
16007b7c582aScube 					 * 1.  unit is not STAR.  Consider 'at'
16017b7c582aScube 					 *     to be explicit, even if it
16027b7c582aScube 					 *     references an interface
16037b7c582aScube 					 *     attribute.
16047b7c582aScube 					 *
16057b7c582aScube 					 * 2.  unit is STAR and 'at' references
16067b7c582aScube 					 *     a real device.  Look for pspec
16077b7c582aScube 					 *     that have a matching p_atdev
16087b7c582aScube 					 *     field.
16097b7c582aScube 					 *
16107b7c582aScube 					 * 3.  unit is STAR and 'at' references
16117b7c582aScube 					 *     an interface attribute.  Look
16127b7c582aScube 					 *     for pspec that have a matching
16137b7c582aScube 					 *     p_iattr field.
16147b7c582aScube 					 */
16157b7c582aScube 					if ((unit != STAR &&        /* Case */
16167b7c582aScube 					     !strcmp(j->i_at, at)) ||  /* 1 */
16177b7c582aScube 					    (unit == STAR &&
16187b7c582aScube 					     ((ad != NULL &&        /* Case */
16197b7c582aScube 					       p->p_atdev == ad) ||    /* 2 */
16207b7c582aScube 					      (ad == NULL &&        /* Case */
16217b7c582aScube 					       p->p_iattr == a))))     /* 3 */
16227b7c582aScube 						stack = newnv(NULL, NULL, j, 0,
16237b7c582aScube 						    stack);
16247b7c582aScube 				}
16257b7c582aScube 		}
16267b7c582aScube 	}
16277b7c582aScube 
162811a68adeSchristos 	devcleanup(stack);
16297b7c582aScube }
16307b7c582aScube 
16317b7c582aScube void
16323dbd1573Schristos deldev(const char *name, int nowarn)
16337b7c582aScube {
16349a7f4bbaSchristos 	size_t l;
16357b7c582aScube 	struct devi *firsti, *i;
163611a68adeSchristos 	struct nvlist *stack = NULL;
16377b7c582aScube 
163875eb07f2Suebayasi 	CFGDBG(5, "deselecting dev `%s'", name);
16399a7f4bbaSchristos 	if (name[0] == '\0')
16409a7f4bbaSchristos 		goto out;
16419a7f4bbaSchristos 
16427b7c582aScube 	l = strlen(name) - 1;
16437b7c582aScube 	if (name[l] == '*' || isdigit((unsigned char)name[l])) {
16447b7c582aScube 		/* `no mydev0' or `no mydev*' */
16457b7c582aScube 		firsti = ht_lookup(devitab, name);
16467b7c582aScube 		if (firsti == NULL) {
16479a7f4bbaSchristos out:
16483dbd1573Schristos 			if (!nowarn)
1649c7295a4cSchristos 				cfgerror("unknown instance %s", name);
16507b7c582aScube 			return;
16517b7c582aScube 		}
16527b7c582aScube 		for (i = firsti; i != NULL; i = i->i_alias)
16537b7c582aScube 			stack = newnv(NULL, NULL, i, 0, stack);
16547b7c582aScube 	} else {
16557b7c582aScube 		struct devbase *d = ht_lookup(devbasetab, name);
16567b7c582aScube 
16577b7c582aScube 		if (d == NULL) {
1658c7295a4cSchristos 			cfgerror("unknown device %s", name);
16597b7c582aScube 			return;
16607b7c582aScube 		}
166171af9028Scube 		if (d->d_ispseudo) {
1662c7295a4cSchristos 			cfgerror("%s is a pseudo-device; "
166371af9028Scube 			    "use \"no pseudo-device %s\" instead", name,
166471af9028Scube 			    name);
166571af9028Scube 			return;
166671af9028Scube 		}
166711a68adeSchristos 		stack = makedevstack(d);
16687b7c582aScube 	}
16697b7c582aScube 
167011a68adeSchristos 	devcleanup(stack);
16717b7c582aScube }
16727b7c582aScube 
167300ed8a38Spooka /*
167400ed8a38Spooka  * Insert given device "name" into devroottab.  In case "name"
167500ed8a38Spooka  * designates a pure interface attribute, create a fake device
167600ed8a38Spooka  * instance for the attribute and insert that into the roottab
167700ed8a38Spooka  * (this scheme avoids mucking around with the orphanage analysis).
167800ed8a38Spooka  */
16797b7c582aScube void
168090ac64deSpooka addpseudoroot(const char *name)
168190ac64deSpooka {
1682194e1c80Spooka 	char buf[NAMESIZE];
1683194e1c80Spooka 	int unit;
1684194e1c80Spooka 	struct attr *attr;
168590ac64deSpooka 	struct devi *i;
168690ac64deSpooka 	struct deva *iba;
168790ac64deSpooka 	struct devbase *ib;
168890ac64deSpooka 
1689194e1c80Spooka 	if (split(name, strlen(name), buf, sizeof(buf), &unit)) {
1690194e1c80Spooka 		cfgerror("invalid pseudo-root name `%s'", name);
1691194e1c80Spooka 		return;
1692194e1c80Spooka 	}
1693194e1c80Spooka 
1694194e1c80Spooka 	/*
169500ed8a38Spooka 	 * Prefer device because devices with locators define an
169600ed8a38Spooka 	 * implicit interface attribute.  However, if a device is
169700ed8a38Spooka 	 * not available, try to attach to the interface attribute.
169800ed8a38Spooka 	 * This makes sure adddev() doesn't get confused when we
169900ed8a38Spooka 	 * are really attaching to a device (alternatively we maybe
170000ed8a38Spooka 	 * could specify a non-NULL atlist to defdevattach() below).
1701194e1c80Spooka 	 */
170200ed8a38Spooka 	ib = ht_lookup(devbasetab, intern(buf));
170300ed8a38Spooka 	if (ib == NULL) {
1704194e1c80Spooka 		struct devbase *fakedev;
1705194e1c80Spooka 		char fakename[NAMESIZE];
1706194e1c80Spooka 
170700ed8a38Spooka 		attr = ht_lookup(attrtab, intern(buf));
170800ed8a38Spooka 		if (!(attr && attr->a_iattr)) {
170900ed8a38Spooka 			cfgerror("pseudo-root `%s' not available", name);
171000ed8a38Spooka 			return;
171100ed8a38Spooka 		}
171200ed8a38Spooka 
1713194e1c80Spooka 		/*
1714194e1c80Spooka 		 * here we cheat a bit: create a fake devbase with the
1715194e1c80Spooka 		 * interface attribute and instantiate it.  quick, cheap,
1716194e1c80Spooka 		 * dirty & bad for you, much like the stuff in the fridge.
1717194e1c80Spooka 		 * and, it works, since the pseudoroot device is not included
1718194e1c80Spooka 		 * in ioconf, just used by config to make sure we start from
1719194e1c80Spooka 		 * the right place.
1720194e1c80Spooka 		 */
172100ed8a38Spooka 		snprintf(fakename, sizeof(fakename), "%s_devattrs", buf);
1722194e1c80Spooka 		fakedev = getdevbase(intern(fakename));
1723194e1c80Spooka 		fakedev->d_isdef = 1;
1724194e1c80Spooka 		fakedev->d_ispseudo = 0;
17254caf067bSdholland 		fakedev->d_attrs = attrlist_cons(NULL, attr);
1726194e1c80Spooka 		defdevattach(NULL, fakedev, NULL, NULL);
1727194e1c80Spooka 
1728194e1c80Spooka 		if (unit == STAR)
1729194e1c80Spooka 			snprintf(buf, sizeof(buf), "%s*", fakename);
1730194e1c80Spooka 		else
1731194e1c80Spooka 			snprintf(buf, sizeof(buf), "%s%d", fakename, unit);
1732194e1c80Spooka 		name = buf;
1733194e1c80Spooka 	}
1734194e1c80Spooka 
1735194e1c80Spooka 	/* ok, everything should be set up, so instantiate a fake device */
173690ac64deSpooka 	i = getdevi(name);
173790ac64deSpooka 	if (i == NULL)
1738d25ad65eSchristos 		panic("%s: device `%s' expected to be present", __func__,
1739d25ad65eSchristos 		    name);
174090ac64deSpooka 	ib = i->i_base;
1741194e1c80Spooka 	iba = ib->d_ahead;
174290ac64deSpooka 
174390ac64deSpooka 	i->i_atdeva = iba;
174490ac64deSpooka 	i->i_cfflags = 0;
174590ac64deSpooka 	i->i_locs = fixloc(name, &errattr, NULL);
174690ac64deSpooka 	i->i_pseudoroot = 1;
174790ac64deSpooka 	i->i_active = DEVI_ORPHAN; /* set active by kill_orphans() */
174890ac64deSpooka 
174990ac64deSpooka 	*iba->d_ipp = i;
175090ac64deSpooka 	iba->d_ipp = &i->i_asame;
175190ac64deSpooka 
175290ac64deSpooka 	ht_insert(devroottab, ib->d_name, ib);
175390ac64deSpooka }
175490ac64deSpooka 
175511a68adeSchristos static void
175611a68adeSchristos deldevbase(struct devbase *d)
175711a68adeSchristos {
175811a68adeSchristos 	struct devi *i;
175911a68adeSchristos 	const char *name = d->d_name;
176011a68adeSchristos 
176111a68adeSchristos 	if (!d->d_ispseudo) {
176211a68adeSchristos 		devcleanup(makedevstack(d));
176311a68adeSchristos 		return;
176411a68adeSchristos 	}
176511a68adeSchristos 
176611a68adeSchristos 	if ((i = ht_lookup(devitab, name)) == NULL)
176711a68adeSchristos 		return;
176811a68adeSchristos 
176911a68adeSchristos 	d->d_umax = 0;		/* clear neads-count entries */
177011a68adeSchristos 	d->d_ihead = NULL;	/* make sure it won't be considered active */
177111a68adeSchristos 	TAILQ_REMOVE(&allpseudo, i, i_next);
177211a68adeSchristos 	if (ht_remove(devitab, name))
1773d25ad65eSchristos 		panic("%s: Can't remove %s from devitab", __func__, name);
177411a68adeSchristos 	if (ht_insert(deaddevitab, name, i))
1775d25ad65eSchristos 		panic("%s: Can't add %s to deaddevitab", __func__, name);
177611a68adeSchristos }
177711a68adeSchristos 
177890ac64deSpooka void
17795ecc953bSthorpej addpseudo(const char *name, int number)
17805ecc953bSthorpej {
17815ecc953bSthorpej 	struct devbase *d;
17825ecc953bSthorpej 	struct devi *i;
17835ecc953bSthorpej 
17845ecc953bSthorpej 	d = ht_lookup(devbasetab, name);
17855ecc953bSthorpej 	if (d == NULL) {
1786c7295a4cSchristos 		cfgerror("undefined pseudo-device %s", name);
17875ecc953bSthorpej 		return;
17885ecc953bSthorpej 	}
17895ecc953bSthorpej 	if (!d->d_ispseudo) {
1790c7295a4cSchristos 		cfgerror("%s is a real device, not a pseudo-device", name);
17915ecc953bSthorpej 		return;
17925ecc953bSthorpej 	}
17935ecc953bSthorpej 	if (ht_lookup(devitab, name) != NULL) {
1794c7295a4cSchristos 		cfgerror("`%s' already defined", name);
17955ecc953bSthorpej 		return;
17965ecc953bSthorpej 	}
17975ecc953bSthorpej 	i = newdevi(name, number - 1, d);	/* foo 16 => "foo0..foo15" */
17985ecc953bSthorpej 	if (ht_insert(devitab, name, i))
1799d25ad65eSchristos 		panic("%s: %s", __func__, name);
18007b7c582aScube 	/* Useful to retrieve the instance from the devbase */
18017b7c582aScube 	d->d_ihead = i;
18027aa6070dScube 	i->i_active = DEVI_ACTIVE;
18035ecc953bSthorpej 	TAILQ_INSERT_TAIL(&allpseudo, i, i_next);
18045ecc953bSthorpej }
18055ecc953bSthorpej 
18065ecc953bSthorpej void
18073dbd1573Schristos delpseudo(const char *name, int nowarn)
18085ecc953bSthorpej {
18095ecc953bSthorpej 	struct devbase *d;
18105ecc953bSthorpej 
181175eb07f2Suebayasi 	CFGDBG(5, "deselecting pseudo `%s'", name);
18125ecc953bSthorpej 	d = ht_lookup(devbasetab, name);
18135ecc953bSthorpej 	if (d == NULL) {
18143dbd1573Schristos 		if (!nowarn)
1815c7295a4cSchristos 			cfgerror("undefined pseudo-device %s", name);
18165ecc953bSthorpej 		return;
18175ecc953bSthorpej 	}
18185ecc953bSthorpej 	if (!d->d_ispseudo) {
1819c7295a4cSchristos 		cfgerror("%s is a real device, not a pseudo-device", name);
18205ecc953bSthorpej 		return;
18215ecc953bSthorpej 	}
182211a68adeSchristos 	deldevbase(d);
18235ecc953bSthorpej }
18245ecc953bSthorpej 
18255ecc953bSthorpej void
1826d767912bSdrochner adddevm(const char *name, devmajor_t cmajor, devmajor_t bmajor,
18279483bda7Sdholland 	struct condexpr *cond, struct nvlist *nv_nodes)
18285ecc953bSthorpej {
18295ecc953bSthorpej 	struct devm *dm;
18305ecc953bSthorpej 
1831d767912bSdrochner 	if (cmajor != NODEVMAJOR && (cmajor < 0 || cmajor >= 4096)) {
1832c7295a4cSchristos 		cfgerror("character major %d is invalid", cmajor);
18339483bda7Sdholland 		condexpr_destroy(cond);
18343da3ab25Spooka 		nvfreel(nv_nodes);
18355ecc953bSthorpej 		return;
18365ecc953bSthorpej 	}
18375ecc953bSthorpej 
1838d767912bSdrochner 	if (bmajor != NODEVMAJOR && (bmajor < 0 || bmajor >= 4096)) {
1839c7295a4cSchristos 		cfgerror("block major %d is invalid", bmajor);
18409483bda7Sdholland 		condexpr_destroy(cond);
18413da3ab25Spooka 		nvfreel(nv_nodes);
18425ecc953bSthorpej 		return;
18435ecc953bSthorpej 	}
1844d767912bSdrochner 	if (cmajor == NODEVMAJOR && bmajor == NODEVMAJOR) {
1845c7295a4cSchristos 		cfgerror("both character/block majors are not specified");
18469483bda7Sdholland 		condexpr_destroy(cond);
18473da3ab25Spooka 		nvfreel(nv_nodes);
18485ecc953bSthorpej 		return;
18495ecc953bSthorpej 	}
18505ecc953bSthorpej 
18515ecc953bSthorpej 	dm = ecalloc(1, sizeof(*dm));
18525ecc953bSthorpej 	dm->dm_srcfile = yyfile;
18535ecc953bSthorpej 	dm->dm_srcline = currentline();
18545ecc953bSthorpej 	dm->dm_name = name;
18555ecc953bSthorpej 	dm->dm_cmajor = cmajor;
18565ecc953bSthorpej 	dm->dm_bmajor = bmajor;
18579483bda7Sdholland 	dm->dm_opts = cond;
18583da3ab25Spooka 	dm->dm_devnodes = nv_nodes;
18595ecc953bSthorpej 
18605ecc953bSthorpej 	TAILQ_INSERT_TAIL(&alldevms, dm, dm_next);
18615ecc953bSthorpej 
18625ecc953bSthorpej 	maxcdevm = MAX(maxcdevm, dm->dm_cmajor);
18635ecc953bSthorpej 	maxbdevm = MAX(maxbdevm, dm->dm_bmajor);
18645ecc953bSthorpej }
18655ecc953bSthorpej 
18667aa6070dScube int
18675ecc953bSthorpej fixdevis(void)
18685ecc953bSthorpej {
1869d25ad65eSchristos 	const char *msg;
18705ecc953bSthorpej 	struct devi *i;
1871511fbc70Schristos 	struct pspec *p;
18727aa6070dScube 	int error = 0;
18735ecc953bSthorpej 
1874bc707469Suebayasi 	TAILQ_FOREACH(i, &alldevi, i_next) {
1875bc707469Suebayasi 		CFGDBG(3, "fixing devis `%s'", i->i_name);
18767aa6070dScube 		if (i->i_active == DEVI_ACTIVE)
18775ecc953bSthorpej 			selectbase(i->i_base, i->i_atdeva);
18787aa6070dScube 		else if (i->i_active == DEVI_ORPHAN) {
1879c130d400Scube 			/*
1880c130d400Scube 			 * At this point, we can't have instances for which
1881c130d400Scube 			 * i_at or i_pspec are NULL.
1882c130d400Scube 			 */
18837aa6070dScube 			++error;
1884511fbc70Schristos 			p = i->i_pspec;
1885d25ad65eSchristos 			msg = p == NULL ? "no parent" :
1886d25ad65eSchristos 			    (p->p_atunit == WILD ? "nothing matching" : "no");
1887c7295a4cSchristos 			cfgxerror(i->i_srcfile, i->i_lineno,
18880dbd1c0eScube 			    "`%s at %s' is orphaned (%s `%s' found)",
1889d25ad65eSchristos 			    i->i_name, i->i_at, msg, i->i_at);
18907aa6070dScube 		} else if (vflag && i->i_active == DEVI_IGNORED)
1891c7295a4cSchristos 			cfgxwarn(i->i_srcfile, i->i_lineno, "ignoring "
1892c7295a4cSchristos 			    "explicitly orphaned instance `%s at %s'",
1893c7295a4cSchristos 			    i->i_name, i->i_at);
1894bc707469Suebayasi 	}
18957aa6070dScube 
18967aa6070dScube 	if (error)
18977aa6070dScube 		return error;
18985ecc953bSthorpej 
18995ecc953bSthorpej 	TAILQ_FOREACH(i, &allpseudo, i_next)
19007aa6070dScube 		if (i->i_active == DEVI_ACTIVE)
19015ecc953bSthorpej 			selectbase(i->i_base, NULL);
19027aa6070dScube 	return 0;
19035ecc953bSthorpej }
19045ecc953bSthorpej 
19055ecc953bSthorpej /*
19065ecc953bSthorpej  * Look up a parent spec, creating a new one if it does not exist.
19075ecc953bSthorpej  */
19085ecc953bSthorpej static struct pspec *
1909*4b9e5cb1Schristos getpspec(struct attr *attr, struct devbase *ab, int atunit, int first)
19105ecc953bSthorpej {
19115ecc953bSthorpej 	struct pspec *p;
191253be83e0Schristos 	int inst = npspecs;
1913*4b9e5cb1Schristos 	int ref = 1;
19145ecc953bSthorpej 
19155ecc953bSthorpej 	TAILQ_FOREACH(p, &allpspecs, p_list) {
191653be83e0Schristos 		if (p->p_iattr == attr && p->p_atdev == ab &&
191753be83e0Schristos 		    p->p_atunit == atunit) {
1918*4b9e5cb1Schristos 			p->p_ref++;
1919*4b9e5cb1Schristos 			if (first)
1920*4b9e5cb1Schristos 				return p;
1921*4b9e5cb1Schristos 			else {
192253be83e0Schristos 				inst = p->p_inst;
1923*4b9e5cb1Schristos 				ref = p->p_ref;
1924*4b9e5cb1Schristos 			}
1925*4b9e5cb1Schristos 
19265ecc953bSthorpej 		}
192753be83e0Schristos 	}
19285ecc953bSthorpej 
19295ecc953bSthorpej 	p = ecalloc(1, sizeof(*p));
19305ecc953bSthorpej 
19315ecc953bSthorpej 	p->p_iattr = attr;
19325ecc953bSthorpej 	p->p_atdev = ab;
19335ecc953bSthorpej 	p->p_atunit = atunit;
193453be83e0Schristos 	p->p_inst = inst;
193553be83e0Schristos 	if (inst == npspecs)
193653be83e0Schristos 		npspecs++;
1937c130d400Scube 	p->p_active = 0;
1938*4b9e5cb1Schristos 	p->p_ref = ref;
19395ecc953bSthorpej 
19405ecc953bSthorpej 	TAILQ_INSERT_TAIL(&allpspecs, p, p_list);
19415ecc953bSthorpej 
19425ecc953bSthorpej 	return (p);
19435ecc953bSthorpej }
19445ecc953bSthorpej 
19455ecc953bSthorpej /*
19465ecc953bSthorpej  * Define a new instance of a specific device.
19475ecc953bSthorpej  */
19485ecc953bSthorpej static struct devi *
19495ecc953bSthorpej getdevi(const char *name)
19505ecc953bSthorpej {
19515ecc953bSthorpej 	struct devi *i, *firsti;
19525ecc953bSthorpej 	struct devbase *d;
19535ecc953bSthorpej 	int unit;
19545ecc953bSthorpej 	char base[NAMESIZE];
19555ecc953bSthorpej 
19565ecc953bSthorpej 	if (split(name, strlen(name), base, sizeof base, &unit)) {
1957c7295a4cSchristos 		cfgerror("invalid device name `%s'", name);
19585ecc953bSthorpej 		return (NULL);
19595ecc953bSthorpej 	}
19605ecc953bSthorpej 	d = ht_lookup(devbasetab, intern(base));
19615ecc953bSthorpej 	if (d == NULL) {
1962c7295a4cSchristos 		cfgerror("%s: unknown device `%s'", name, base);
19635ecc953bSthorpej 		return (NULL);
19645ecc953bSthorpej 	}
19655ecc953bSthorpej 	if (d->d_ispseudo) {
1966c7295a4cSchristos 		cfgerror("%s: %s is a pseudo-device", name, base);
19675ecc953bSthorpej 		return (NULL);
19685ecc953bSthorpej 	}
19695ecc953bSthorpej 	firsti = ht_lookup(devitab, name);
19705ecc953bSthorpej 	i = newdevi(name, unit, d);
19715ecc953bSthorpej 	if (firsti == NULL) {
19725ecc953bSthorpej 		if (ht_insert(devitab, name, i))
1973d25ad65eSchristos 			panic("%s: %s", __func__, name);
19745ecc953bSthorpej 		*d->d_ipp = i;
19755ecc953bSthorpej 		d->d_ipp = &i->i_bsame;
19765ecc953bSthorpej 	} else {
19775ecc953bSthorpej 		while (firsti->i_alias)
19785ecc953bSthorpej 			firsti = firsti->i_alias;
19795ecc953bSthorpej 		firsti->i_alias = i;
19805ecc953bSthorpej 	}
19815ecc953bSthorpej 	TAILQ_INSERT_TAIL(&alldevi, i, i_next);
19825ecc953bSthorpej 	ndevi++;
19835ecc953bSthorpej 	return (i);
19845ecc953bSthorpej }
19855ecc953bSthorpej 
19865ecc953bSthorpej static const char *
19875ecc953bSthorpej concat(const char *name, int c)
19885ecc953bSthorpej {
1989c7295a4cSchristos 	size_t len;
19905ecc953bSthorpej 	char buf[NAMESIZE];
19915ecc953bSthorpej 
19925ecc953bSthorpej 	len = strlen(name);
19935ecc953bSthorpej 	if (len + 2 > sizeof(buf)) {
1994c7295a4cSchristos 		cfgerror("device name `%s%c' too long", name, c);
19955ecc953bSthorpej 		len = sizeof(buf) - 2;
19965ecc953bSthorpej 	}
19975ecc953bSthorpej 	memmove(buf, name, len);
19989a7f4bbaSchristos 	buf[len] = (char)c;
19999a7f4bbaSchristos 	buf[len + 1] = '\0';
20005ecc953bSthorpej 	return (intern(buf));
20015ecc953bSthorpej }
20025ecc953bSthorpej 
20035ecc953bSthorpej const char *
20045ecc953bSthorpej starref(const char *name)
20055ecc953bSthorpej {
20065ecc953bSthorpej 
20075ecc953bSthorpej 	return (concat(name, '*'));
20085ecc953bSthorpej }
20095ecc953bSthorpej 
20105ecc953bSthorpej const char *
20115ecc953bSthorpej wildref(const char *name)
20125ecc953bSthorpej {
20135ecc953bSthorpej 
20145ecc953bSthorpej 	return (concat(name, '?'));
20155ecc953bSthorpej }
20165ecc953bSthorpej 
20175ecc953bSthorpej /*
20185ecc953bSthorpej  * Split a name like "foo0" into base name (foo) and unit number (0).
20195ecc953bSthorpej  * Return 0 on success.  To make this useful for names like "foo0a",
20205ecc953bSthorpej  * the length of the "foo0" part is one of the arguments.
20215ecc953bSthorpej  */
20225ecc953bSthorpej static int
20235ecc953bSthorpej split(const char *name, size_t nlen, char *base, size_t bsize, int *aunit)
20245ecc953bSthorpej {
20255ecc953bSthorpej 	const char *cp;
2026c7295a4cSchristos 	int c;
2027c7295a4cSchristos 	size_t l;
20285ecc953bSthorpej 
20295ecc953bSthorpej 	l = nlen;
20305ecc953bSthorpej 	if (l < 2 || l >= bsize || isdigit((unsigned char)*name))
20315ecc953bSthorpej 		return (1);
20325ecc953bSthorpej 	c = (u_char)name[--l];
20335ecc953bSthorpej 	if (!isdigit(c)) {
20345ecc953bSthorpej 		if (c == '*')
20355ecc953bSthorpej 			*aunit = STAR;
20365ecc953bSthorpej 		else if (c == '?')
20375ecc953bSthorpej 			*aunit = WILD;
20385ecc953bSthorpej 		else
20395ecc953bSthorpej 			return (1);
20405ecc953bSthorpej 	} else {
20415ecc953bSthorpej 		cp = &name[l];
20425ecc953bSthorpej 		while (isdigit((unsigned char)cp[-1]))
20435ecc953bSthorpej 			l--, cp--;
20445ecc953bSthorpej 		*aunit = atoi(cp);
20455ecc953bSthorpej 	}
20465ecc953bSthorpej 	memmove(base, name, l);
20475ecc953bSthorpej 	base[l] = 0;
20485ecc953bSthorpej 	return (0);
20495ecc953bSthorpej }
20505ecc953bSthorpej 
20515ecc953bSthorpej void
20528d928a75Suebayasi addattr(const char *name)
20538d928a75Suebayasi {
20548d928a75Suebayasi 	struct attr *a;
20558d928a75Suebayasi 
20568d928a75Suebayasi 	a = refattr(name);
20578d928a75Suebayasi 	selectattr(a);
20588d928a75Suebayasi }
20598d928a75Suebayasi 
20608d928a75Suebayasi void
20613dbd1573Schristos delattr(const char *name, int nowarn)
20628d928a75Suebayasi {
20638d928a75Suebayasi 	struct attr *a;
20648d928a75Suebayasi 
20658d928a75Suebayasi 	a = refattr(name);
20668d928a75Suebayasi 	deselectattr(a);
20678d928a75Suebayasi }
20688d928a75Suebayasi 
20698d928a75Suebayasi void
20705ecc953bSthorpej selectattr(struct attr *a)
20715ecc953bSthorpej {
20723a03bee6Suebayasi 	struct attrlist *al;
20733a03bee6Suebayasi 	struct attr *dep;
20745ecc953bSthorpej 
20758d928a75Suebayasi 	CFGDBG(5, "selecting attr `%s'", a->a_name);
20763a03bee6Suebayasi 	for (al = a->a_deps; al != NULL; al = al->al_next) {
20773a03bee6Suebayasi 		dep = al->al_this;
20783a03bee6Suebayasi 		selectattr(dep);
20793a03bee6Suebayasi 	}
2080d6687201Suebayasi 	if (ht_insert(selecttab, a->a_name, __UNCONST(a->a_name)) == 0)
2081d6687201Suebayasi 		nattrs++;
2082b599bf99Suebayasi 	CFGDBG(3, "attr selected `%s'", a->a_name);
20835ecc953bSthorpej }
20845ecc953bSthorpej 
20856d1bba4aSuebayasi static int
20868d928a75Suebayasi deselectattrcb2(const char *name1, const char *name2, void *v, void *arg)
20878d928a75Suebayasi {
208811a68adeSchristos 	struct attr *a = arg;
208911a68adeSchristos 	const char *name = a->a_name;
209011a68adeSchristos 	struct vtype *vt = v;
20918d928a75Suebayasi 
209211a68adeSchristos 	if (strcmp(name, name2) == 0) {
20933dbd1573Schristos 		delattr(name1, 0);
20948d928a75Suebayasi 		return 0;
20958d928a75Suebayasi 	}
20968d928a75Suebayasi 
209711a68adeSchristos 	if (!vt->attr->a_deselected)
209811a68adeSchristos 		return 0;
209911a68adeSchristos 
210011a68adeSchristos 	switch (vt->type) {
210111a68adeSchristos 	case V_ATTRIBUTE:
210211a68adeSchristos #ifdef notyet
210311a68adeSchristos 		// XXX: Loops
210411a68adeSchristos 		deselectattr(vt->value);
210511a68adeSchristos #endif
210611a68adeSchristos 		break;
210711a68adeSchristos 	case V_DEVICE:
210811a68adeSchristos 		CFGDBG(5, "removing device `%s' with attr `%s' because attr `%s'"
210911a68adeSchristos 		    " is deselected", name1, name2, name);
211011a68adeSchristos 		deldevbase(vt->value);
211111a68adeSchristos 		break;
211211a68adeSchristos 	default:
211311a68adeSchristos 		abort();
211411a68adeSchristos 	}
211511a68adeSchristos 	return 0;
211611a68adeSchristos }
211711a68adeSchristos 
21188d928a75Suebayasi void
21198d928a75Suebayasi deselectattr(struct attr *a)
21208d928a75Suebayasi {
21218d928a75Suebayasi 	CFGDBG(5, "deselecting attr `%s'", a->a_name);
212211a68adeSchristos 	a->a_deselected = 1;
212311a68adeSchristos 	ht_enumerate2(attrdeptab, deselectattrcb2, a);
2124d6687201Suebayasi 	if (ht_remove(selecttab, a->a_name) == 0)
2125d6687201Suebayasi 		nattrs--;
21268d928a75Suebayasi 	CFGDBG(3, "attr deselected `%s'", a->a_name);
21278d928a75Suebayasi }
21288d928a75Suebayasi 
21298d928a75Suebayasi static int
21306d1bba4aSuebayasi dumpattrdepcb2(const char *name1, const char *name2, void *v, void *arg)
21316d1bba4aSuebayasi {
21326d1bba4aSuebayasi 
21336d1bba4aSuebayasi 	CFGDBG(3, "attr `%s' depends on attr `%s'", name1, name2);
21346d1bba4aSuebayasi 	return 0;
21356d1bba4aSuebayasi }
21366d1bba4aSuebayasi 
21376d1bba4aSuebayasi void
21386d1bba4aSuebayasi dependattrs(void)
21396d1bba4aSuebayasi {
21406d1bba4aSuebayasi 
21416d1bba4aSuebayasi 	ht_enumerate2(attrdeptab, dumpattrdepcb2, NULL);
21426d1bba4aSuebayasi }
21436d1bba4aSuebayasi 
21445ecc953bSthorpej /*
21455ecc953bSthorpej  * We have an instance of the base foo, so select it and all its
21465ecc953bSthorpej  * attributes for "optional foo".
21475ecc953bSthorpej  */
21485ecc953bSthorpej static void
21495ecc953bSthorpej selectbase(struct devbase *d, struct deva *da)
21505ecc953bSthorpej {
21515ecc953bSthorpej 	struct attr *a;
21524caf067bSdholland 	struct attrlist *al;
21535ecc953bSthorpej 
2154c7295a4cSchristos 	(void)ht_insert(selecttab, d->d_name, __UNCONST(d->d_name));
2155b599bf99Suebayasi 	CFGDBG(3, "devbase selected `%s'", d->d_name);
21566d1bba4aSuebayasi 	CFGDBG(5, "selecting dependencies of devbase `%s'", d->d_name);
21574caf067bSdholland 	for (al = d->d_attrs; al != NULL; al = al->al_next) {
21584caf067bSdholland 		a = al->al_this;
21595ecc953bSthorpej 		expandattr(a, selectattr);
21605ecc953bSthorpej 	}
2161da706167Suebayasi 
2162da706167Suebayasi 	struct attr *devattr;
2163da706167Suebayasi 	devattr = refattr(d->d_name);
2164da706167Suebayasi 	expandattr(devattr, selectattr);
2165da706167Suebayasi 
21665ecc953bSthorpej 	if (da != NULL) {
2167c7295a4cSchristos 		(void)ht_insert(selecttab, da->d_name, __UNCONST(da->d_name));
2168b599bf99Suebayasi 		CFGDBG(3, "devattr selected `%s'", da->d_name);
21694caf067bSdholland 		for (al = da->d_attrs; al != NULL; al = al->al_next) {
21704caf067bSdholland 			a = al->al_this;
21715ecc953bSthorpej 			expandattr(a, selectattr);
21725ecc953bSthorpej 		}
21735ecc953bSthorpej 	}
2174c4bac1d0Suebayasi 
2175c4bac1d0Suebayasi 	fixdev(d);
21765ecc953bSthorpej }
21775ecc953bSthorpej 
21785ecc953bSthorpej /*
21795ecc953bSthorpej  * Is the given pointer on the given list of pointers?
21805ecc953bSthorpej  */
218159c94545Scube int
21825ecc953bSthorpej onlist(struct nvlist *nv, void *ptr)
21835ecc953bSthorpej {
21845ecc953bSthorpej 	for (; nv != NULL; nv = nv->nv_next)
21855ecc953bSthorpej 		if (nv->nv_ptr == ptr)
21865ecc953bSthorpej 			return (1);
21875ecc953bSthorpej 	return (0);
21885ecc953bSthorpej }
21895ecc953bSthorpej 
21905ecc953bSthorpej static char *
21915ecc953bSthorpej extend(char *p, const char *name)
21925ecc953bSthorpej {
2193c7295a4cSchristos 	size_t l;
21945ecc953bSthorpej 
21955ecc953bSthorpej 	l = strlen(name);
21965ecc953bSthorpej 	memmove(p, name, l);
21975ecc953bSthorpej 	p += l;
21985ecc953bSthorpej 	*p++ = ',';
21995ecc953bSthorpej 	*p++ = ' ';
22005ecc953bSthorpej 	return (p);
22015ecc953bSthorpej }
22025ecc953bSthorpej 
22035ecc953bSthorpej /*
22045ecc953bSthorpej  * Check that we got all required locators, and default any that are
22055ecc953bSthorpej  * given as "?" and have defaults.  Return 0 on success.
22065ecc953bSthorpej  */
22075ecc953bSthorpej static const char **
220816a8771bSdholland fixloc(const char *name, struct attr *attr, struct loclist *got)
22095ecc953bSthorpej {
221016a8771bSdholland 	struct loclist *m, *n;
22115ecc953bSthorpej 	int ord;
22125ecc953bSthorpej 	const char **lp;
22135ecc953bSthorpej 	int nmissing, nextra, nnodefault;
22145ecc953bSthorpej 	char *mp, *ep, *ndp;
22155ecc953bSthorpej 	char missing[1000], extra[1000], nodefault[1000];
22165ecc953bSthorpej 	static const char *nullvec[1];
22175ecc953bSthorpej 
22185ecc953bSthorpej 	/*
22195ecc953bSthorpej 	 * Look for all required locators, and number the given ones
22205ecc953bSthorpej 	 * according to the required order.  While we are numbering,
22215ecc953bSthorpej 	 * set default values for defaulted locators.
22225ecc953bSthorpej 	 */
22235ecc953bSthorpej 	if (attr->a_loclen == 0)	/* e.g., "at root" */
22245ecc953bSthorpej 		lp = nullvec;
22255ecc953bSthorpej 	else
22269a7f4bbaSchristos 		lp = emalloc((size_t)(attr->a_loclen + 1) * sizeof(const char *));
222716a8771bSdholland 	for (n = got; n != NULL; n = n->ll_next)
222816a8771bSdholland 		n->ll_num = -1;
22295ecc953bSthorpej 	nmissing = 0;
22305ecc953bSthorpej 	mp = missing;
22315ecc953bSthorpej 	/* yes, this is O(mn), but m and n should be small */
223216a8771bSdholland 	for (ord = 0, m = attr->a_locs; m != NULL; m = m->ll_next, ord++) {
223316a8771bSdholland 		for (n = got; n != NULL; n = n->ll_next) {
223416a8771bSdholland 			if (n->ll_name == m->ll_name) {
223516a8771bSdholland 				n->ll_num = ord;
22365ecc953bSthorpej 				break;
22375ecc953bSthorpej 			}
22385ecc953bSthorpej 		}
223916a8771bSdholland 		if (n == NULL && m->ll_num == 0) {
22405ecc953bSthorpej 			nmissing++;
224116a8771bSdholland 			mp = extend(mp, m->ll_name);
22425ecc953bSthorpej 		}
224316a8771bSdholland 		lp[ord] = m->ll_string;
22445ecc953bSthorpej 	}
22455ecc953bSthorpej 	if (ord != attr->a_loclen)
2246d25ad65eSchristos 		panic("%s: bad length", __func__);
22475ecc953bSthorpej 	lp[ord] = NULL;
22485ecc953bSthorpej 	nextra = 0;
22495ecc953bSthorpej 	ep = extra;
22505ecc953bSthorpej 	nnodefault = 0;
22515ecc953bSthorpej 	ndp = nodefault;
225216a8771bSdholland 	for (n = got; n != NULL; n = n->ll_next) {
225316a8771bSdholland 		if (n->ll_num >= 0) {
225416a8771bSdholland 			if (n->ll_string != NULL)
225516a8771bSdholland 				lp[n->ll_num] = n->ll_string;
225616a8771bSdholland 			else if (lp[n->ll_num] == NULL) {
22575ecc953bSthorpej 				nnodefault++;
225816a8771bSdholland 				ndp = extend(ndp, n->ll_name);
22595ecc953bSthorpej 			}
22605ecc953bSthorpej 		} else {
22615ecc953bSthorpej 			nextra++;
226216a8771bSdholland 			ep = extend(ep, n->ll_name);
22635ecc953bSthorpej 		}
22645ecc953bSthorpej 	}
22655ecc953bSthorpej 	if (nextra) {
22665ecc953bSthorpej 		ep[-2] = 0;	/* kill ", " */
2267c7295a4cSchristos 		cfgerror("%s: extraneous locator%s: %s",
22685ecc953bSthorpej 		    name, nextra > 1 ? "s" : "", extra);
22695ecc953bSthorpej 	}
22705ecc953bSthorpej 	if (nmissing) {
22715ecc953bSthorpej 		mp[-2] = 0;
2272c7295a4cSchristos 		cfgerror("%s: must specify %s", name, missing);
22735ecc953bSthorpej 	}
22745ecc953bSthorpej 	if (nnodefault) {
22755ecc953bSthorpej 		ndp[-2] = 0;
2276c7295a4cSchristos 		cfgerror("%s: cannot wildcard %s", name, nodefault);
22775ecc953bSthorpej 	}
22785ecc953bSthorpej 	if (nmissing || nnodefault) {
22795ecc953bSthorpej 		free(lp);
22805ecc953bSthorpej 		lp = NULL;
22815ecc953bSthorpej 	}
22825ecc953bSthorpej 	return (lp);
22835ecc953bSthorpej }
2284437f8925Scube 
2285437f8925Scube void
2286437f8925Scube setversion(int newver)
2287437f8925Scube {
2288437f8925Scube 	if (newver > CONFIG_VERSION)
2289c7295a4cSchristos 		cfgerror("your sources require a newer version of config(1) "
2290437f8925Scube 		    "-- please rebuild it.");
2291437f8925Scube 	else if (newver < CONFIG_MINVERSION)
2292c7295a4cSchristos 		cfgerror("your sources are out of date -- please update.");
2293437f8925Scube 	else
2294437f8925Scube 		version = newver;
2295437f8925Scube }
2296