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