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