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