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