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