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