sem.c revision 1.53 1 /* $NetBSD: sem.c,v 1.53 2014/10/10 08:14:47 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 void
676 refattr(const char *name)
677 {
678
679 if ((ht_lookup(attrtab, name)) == NULL)
680 (void)mkattr(name);
681 }
682
683 int
684 getrefattr(const char *name, struct attr **ra)
685 {
686 struct attr *a;
687
688 a = ht_lookup(attrtab, name);
689 if (a == NULL) {
690 *ra = NULL;
691 return (0);
692 }
693 /*
694 * Check if the existing attr is only referenced, not really defined.
695 */
696 if (a->a_deps == NULL &&
697 a->a_iattr == 0 &&
698 a->a_devclass == 0) {
699 *ra = a;
700 return (0);
701 }
702 return (1);
703 }
704
705 /*
706 * Recursively expand an attribute and its dependencies, checking for
707 * cycles, and invoking a callback for each attribute found.
708 */
709 void
710 expandattr(struct attr *a, void (*callback)(struct attr *))
711 {
712 struct attrlist *al;
713 struct attr *dep;
714
715 if (a->a_expanding) {
716 cfgerror("circular dependency on attribute `%s'", a->a_name);
717 return;
718 }
719
720 a->a_expanding = 1;
721
722 /* First expand all of this attribute's dependencies. */
723 for (al = a->a_deps; al != NULL; al = al->al_next) {
724 dep = al->al_this;
725 expandattr(dep, callback);
726 }
727
728 /* ...and now invoke the callback for ourself. */
729 if (callback != NULL)
730 (*callback)(a);
731
732 a->a_expanding = 0;
733 }
734
735 /*
736 * Set the major device number for a device, so that it can be used
737 * as a root/dumps "on" device in a configuration.
738 */
739 void
740 setmajor(struct devbase *d, devmajor_t n)
741 {
742
743 if (d != &errdev && d->d_major != NODEVMAJOR)
744 cfgerror("device `%s' is already major %d",
745 d->d_name, d->d_major);
746 else
747 d->d_major = n;
748 }
749
750 const char *
751 major2name(devmajor_t maj)
752 {
753 struct devbase *dev;
754 struct devm *dm;
755
756 if (!do_devsw) {
757 TAILQ_FOREACH(dev, &allbases, d_next) {
758 if (dev->d_major == maj)
759 return (dev->d_name);
760 }
761 } else {
762 TAILQ_FOREACH(dm, &alldevms, dm_next) {
763 if (dm->dm_bmajor == maj)
764 return (dm->dm_name);
765 }
766 }
767 return (NULL);
768 }
769
770 devmajor_t
771 dev2major(struct devbase *dev)
772 {
773 struct devm *dm;
774
775 if (!do_devsw)
776 return (dev->d_major);
777
778 TAILQ_FOREACH(dm, &alldevms, dm_next) {
779 if (strcmp(dm->dm_name, dev->d_name) == 0)
780 return (dm->dm_bmajor);
781 }
782 return (NODEVMAJOR);
783 }
784
785 /*
786 * Make a string description of the device at maj/min.
787 */
788 static const char *
789 makedevstr(devmajor_t maj, devminor_t min)
790 {
791 const char *devicename;
792 char buf[32];
793
794 devicename = major2name(maj);
795 if (devicename == NULL)
796 (void)snprintf(buf, sizeof(buf), "<%d/%d>", maj, min);
797 else
798 (void)snprintf(buf, sizeof(buf), "%s%d%c", devicename,
799 min / maxpartitions, (min % maxpartitions) + 'a');
800
801 return (intern(buf));
802 }
803
804 /*
805 * Map things like "ra0b" => makedev(major("ra"), 0*maxpartitions + 'b'-'a').
806 * Handle the case where the device number is given but there is no
807 * corresponding name, and map NULL to the default.
808 */
809 static int
810 resolve(struct nvlist **nvp, const char *name, const char *what,
811 struct nvlist *dflt, int part)
812 {
813 struct nvlist *nv;
814 struct devbase *dev;
815 const char *cp;
816 devmajor_t maj;
817 devminor_t min;
818 int i, l;
819 int unit;
820 char buf[NAMESIZE];
821
822 if ((part -= 'a') >= maxpartitions || part < 0)
823 panic("resolve");
824 if ((nv = *nvp) == NULL) {
825 dev_t d = NODEV;
826 /*
827 * Apply default. Easiest to do this by number.
828 * Make sure to retain NODEVness, if this is dflt's disposition.
829 */
830 if ((dev_t)dflt->nv_num != NODEV) {
831 maj = major(dflt->nv_num);
832 min = ((minor(dflt->nv_num) / maxpartitions) *
833 maxpartitions) + part;
834 d = makedev(maj, min);
835 cp = makedevstr(maj, min);
836 } else
837 cp = NULL;
838 *nvp = nv = newnv(NULL, cp, NULL, d, NULL);
839 }
840 if ((dev_t)nv->nv_num != NODEV) {
841 /*
842 * By the numbers. Find the appropriate major number
843 * to make a name.
844 */
845 maj = major(nv->nv_num);
846 min = minor(nv->nv_num);
847 nv->nv_str = makedevstr(maj, min);
848 return (0);
849 }
850
851 if (nv->nv_str == NULL || nv->nv_str == s_qmark)
852 /*
853 * Wildcarded or unspecified; leave it as NODEV.
854 */
855 return (0);
856
857 /*
858 * The normal case: things like "ra2b". Check for partition
859 * suffix, remove it if there, and split into name ("ra") and
860 * unit (2).
861 */
862 l = i = strlen(nv->nv_str);
863 cp = &nv->nv_str[l];
864 if (l > 1 && *--cp >= 'a' && *cp < 'a' + maxpartitions &&
865 isdigit((unsigned char)cp[-1])) {
866 l--;
867 part = *cp - 'a';
868 }
869 cp = nv->nv_str;
870 if (split(cp, l, buf, sizeof buf, &unit)) {
871 cfgerror("%s: invalid %s device name `%s'", name, what, cp);
872 return (1);
873 }
874 dev = ht_lookup(devbasetab, intern(buf));
875 if (dev == NULL) {
876 cfgerror("%s: device `%s' does not exist", name, buf);
877 return (1);
878 }
879
880 /*
881 * Check for the magic network interface attribute, and
882 * don't bother making a device number.
883 */
884 if (has_attr(dev->d_attrs, s_ifnet)) {
885 nv->nv_num = NODEV;
886 nv->nv_ifunit = unit; /* XXX XXX XXX */
887 } else {
888 maj = dev2major(dev);
889 if (maj == NODEVMAJOR) {
890 cfgerror("%s: can't make %s device from `%s'",
891 name, what, nv->nv_str);
892 return (1);
893 }
894 nv->nv_num = makedev(maj, unit * maxpartitions + part);
895 }
896
897 nv->nv_name = dev->d_name;
898 return (0);
899 }
900
901 /*
902 * Add a completed configuration to the list.
903 */
904 void
905 addconf(struct config *cf0)
906 {
907 struct config *cf;
908 const char *name;
909
910 name = cf0->cf_name;
911 cf = ecalloc(1, sizeof *cf);
912 if (ht_insert(cfhashtab, name, cf)) {
913 cfgerror("configuration `%s' already defined", name);
914 free(cf);
915 goto bad;
916 }
917 *cf = *cf0;
918
919 /*
920 * Resolve the root device.
921 */
922 if (cf->cf_root == NULL) {
923 cfgerror("%s: no root device specified", name);
924 goto bad;
925 }
926 if (cf->cf_root && cf->cf_root->nv_str != s_qmark) {
927 struct nvlist *nv;
928 nv = cf->cf_root;
929 if (resolve(&cf->cf_root, name, "root", nv, 'a'))
930 goto bad;
931 }
932
933 /*
934 * Resolve the dump device.
935 */
936 if (cf->cf_dump == NULL || cf->cf_dump->nv_str == s_qmark) {
937 /*
938 * Wildcarded dump device is equivalent to unspecified.
939 */
940 cf->cf_dump = NULL;
941 } else if (cf->cf_dump->nv_str == s_none) {
942 /*
943 * Operator has requested that no dump device should be
944 * configured; do nothing.
945 */
946 } else {
947 if (resolve(&cf->cf_dump, name, "dumps", cf->cf_dump, 'b'))
948 goto bad;
949 }
950
951 /* Wildcarded fstype is `unspecified'. */
952 if (cf->cf_fstype == s_qmark)
953 cf->cf_fstype = NULL;
954
955 TAILQ_INSERT_TAIL(&allcf, cf, cf_next);
956 return;
957 bad:
958 nvfreel(cf0->cf_root);
959 nvfreel(cf0->cf_dump);
960 }
961
962 void
963 setconf(struct nvlist **npp, const char *what, struct nvlist *v)
964 {
965
966 if (*npp != NULL) {
967 cfgerror("duplicate %s specification", what);
968 nvfreel(v);
969 } else
970 *npp = v;
971 }
972
973 void
974 delconf(const char *name)
975 {
976 struct config *cf;
977
978 if (ht_lookup(cfhashtab, name) == NULL) {
979 cfgerror("configuration `%s' undefined", name);
980 return;
981 }
982 (void)ht_remove(cfhashtab, name);
983
984 TAILQ_FOREACH(cf, &allcf, cf_next)
985 if (!strcmp(cf->cf_name, name))
986 break;
987 if (cf == NULL)
988 panic("lost configuration `%s'", name);
989
990 TAILQ_REMOVE(&allcf, cf, cf_next);
991 }
992
993 void
994 setfstype(const char **fstp, const char *v)
995 {
996
997 if (*fstp != NULL) {
998 cfgerror("multiple fstype specifications");
999 return;
1000 }
1001
1002 if (v != s_qmark && OPT_FSOPT(v)) {
1003 cfgerror("\"%s\" is not a configured file system", v);
1004 return;
1005 }
1006
1007 *fstp = v;
1008 }
1009
1010 static struct devi *
1011 newdevi(const char *name, int unit, struct devbase *d)
1012 {
1013 struct devi *i;
1014
1015 i = ecalloc(1, sizeof *i);
1016 i->i_name = name;
1017 i->i_unit = unit;
1018 i->i_base = d;
1019 i->i_bsame = NULL;
1020 i->i_asame = NULL;
1021 i->i_alias = NULL;
1022 i->i_at = NULL;
1023 i->i_pspec = NULL;
1024 i->i_atdeva = NULL;
1025 i->i_locs = NULL;
1026 i->i_cfflags = 0;
1027 i->i_lineno = currentline();
1028 i->i_srcfile = yyfile;
1029 i->i_active = DEVI_ORPHAN; /* Proper analysis comes later */
1030 i->i_level = devilevel;
1031 i->i_pseudoroot = 0;
1032 if (unit >= d->d_umax)
1033 d->d_umax = unit + 1;
1034 return (i);
1035 }
1036
1037 /*
1038 * Add the named device as attaching to the named attribute (or perhaps
1039 * another device instead) plus unit number.
1040 */
1041 void
1042 adddev(const char *name, const char *at, struct loclist *loclist, int flags)
1043 {
1044 struct devi *i; /* the new instance */
1045 struct pspec *p; /* and its pspec */
1046 struct attr *attr; /* attribute that allows attach */
1047 struct devbase *ib; /* i->i_base */
1048 struct devbase *ab; /* not NULL => at another dev */
1049 struct attrlist *al;
1050 struct deva *iba; /* devbase attachment used */
1051 const char *cp;
1052 int atunit;
1053 char atbuf[NAMESIZE];
1054 int hit;
1055
1056 ab = NULL;
1057 iba = NULL;
1058 if (at == NULL) {
1059 /* "at root" */
1060 p = NULL;
1061 if ((i = getdevi(name)) == NULL)
1062 goto bad;
1063 /*
1064 * Must warn about i_unit > 0 later, after taking care of
1065 * the STAR cases (we could do non-star's here but why
1066 * bother?). Make sure this device can be at root.
1067 */
1068 ib = i->i_base;
1069 hit = 0;
1070 for (iba = ib->d_ahead; iba != NULL; iba = iba->d_bsame)
1071 if (onlist(iba->d_atlist, NULL)) {
1072 hit = 1;
1073 break;
1074 }
1075 if (!hit) {
1076 cfgerror("`%s' cannot attach to the root", ib->d_name);
1077 i->i_active = DEVI_BROKEN;
1078 goto bad;
1079 }
1080 attr = &errattr; /* a convenient "empty" attr */
1081 } else {
1082 if (split(at, strlen(at), atbuf, sizeof atbuf, &atunit)) {
1083 cfgerror("invalid attachment name `%s'", at);
1084 /* (void)getdevi(name); -- ??? */
1085 goto bad;
1086 }
1087 if ((i = getdevi(name)) == NULL)
1088 goto bad;
1089 ib = i->i_base;
1090
1091 /*
1092 * Devices can attach to two types of things: Attributes,
1093 * and other devices (which have the appropriate attributes
1094 * to allow attachment).
1095 *
1096 * (1) If we're attached to an attribute, then we don't need
1097 * look at the parent base device to see what attributes
1098 * it has, and make sure that we can attach to them.
1099 *
1100 * (2) If we're attached to a real device (i.e. named in
1101 * the config file), we want to remember that so that
1102 * at cross-check time, if the device we're attached to
1103 * is missing but other devices which also provide the
1104 * attribute are present, we don't get a false "OK."
1105 *
1106 * (3) If the thing we're attached to is an attribute
1107 * but is actually named in the config file, we still
1108 * have to remember its devbase.
1109 */
1110 cp = intern(atbuf);
1111
1112 /* Figure out parent's devbase, to satisfy case (3). */
1113 ab = ht_lookup(devbasetab, cp);
1114
1115 /* Find out if it's an attribute. */
1116 attr = ht_lookup(attrtab, cp);
1117
1118 /* Make sure we're _really_ attached to the attr. Case (1). */
1119 if (attr != NULL && onlist(attr->a_devs, ib))
1120 goto findattachment;
1121
1122 /*
1123 * Else a real device, and not just an attribute. Case (2).
1124 *
1125 * Have to work a bit harder to see whether we have
1126 * something like "tg0 at esp0" (where esp is merely
1127 * not an attribute) or "tg0 at nonesuch0" (where
1128 * nonesuch is not even a device).
1129 */
1130 if (ab == NULL) {
1131 cfgerror("%s at %s: `%s' unknown",
1132 name, at, atbuf);
1133 i->i_active = DEVI_BROKEN;
1134 goto bad;
1135 }
1136
1137 /*
1138 * See if the named parent carries an attribute
1139 * that allows it to supervise device ib.
1140 */
1141 for (al = ab->d_attrs; al != NULL; al = al->al_next) {
1142 attr = al->al_this;
1143 if (onlist(attr->a_devs, ib))
1144 goto findattachment;
1145 }
1146 cfgerror("`%s' cannot attach to `%s'", ib->d_name, atbuf);
1147 i->i_active = DEVI_BROKEN;
1148 goto bad;
1149
1150 findattachment:
1151 /*
1152 * Find the parent spec. If a matching one has not yet been
1153 * created, create one.
1154 */
1155 p = getpspec(attr, ab, atunit);
1156 p->p_devs = newnv(NULL, NULL, i, 0, p->p_devs);
1157
1158 /* find out which attachment it uses */
1159 hit = 0;
1160 for (iba = ib->d_ahead; iba != NULL; iba = iba->d_bsame)
1161 if (onlist(iba->d_atlist, attr)) {
1162 hit = 1;
1163 break;
1164 }
1165 if (!hit)
1166 panic("adddev: can't figure out attachment");
1167 }
1168 if ((i->i_locs = fixloc(name, attr, loclist)) == NULL) {
1169 i->i_active = DEVI_BROKEN;
1170 goto bad;
1171 }
1172 i->i_at = at;
1173 i->i_pspec = p;
1174 i->i_atdeva = iba;
1175 i->i_cfflags = flags;
1176
1177 *iba->d_ipp = i;
1178 iba->d_ipp = &i->i_asame;
1179
1180 /* all done, fall into ... */
1181 bad:
1182 loclist_destroy(loclist);
1183 return;
1184 }
1185
1186 void
1187 deldevi(const char *name, const char *at)
1188 {
1189 struct devi *firsti, *i;
1190 struct devbase *d;
1191 int unit;
1192 char base[NAMESIZE];
1193
1194 if (split(name, strlen(name), base, sizeof base, &unit)) {
1195 cfgerror("invalid device name `%s'", name);
1196 return;
1197 }
1198 d = ht_lookup(devbasetab, intern(base));
1199 if (d == NULL) {
1200 cfgerror("%s: unknown device `%s'", name, base);
1201 return;
1202 }
1203 if (d->d_ispseudo) {
1204 cfgerror("%s: %s is a pseudo-device", name, base);
1205 return;
1206 }
1207 if ((firsti = ht_lookup(devitab, name)) == NULL) {
1208 cfgerror("`%s' not defined", name);
1209 return;
1210 }
1211 if (at == NULL && firsti->i_at == NULL) {
1212 /* 'at root' */
1213 remove_devi(firsti);
1214 return;
1215 } else if (at != NULL)
1216 for (i = firsti; i != NULL; i = i->i_alias)
1217 if (i->i_active != DEVI_BROKEN &&
1218 strcmp(at, i->i_at) == 0) {
1219 remove_devi(i);
1220 return;
1221 }
1222 cfgerror("`%s' at `%s' not found", name, at ? at : "root");
1223 }
1224
1225 static void
1226 remove_devi(struct devi *i)
1227 {
1228 struct devbase *d = i->i_base;
1229 struct devi *f, *j, **ppi;
1230 struct deva *iba;
1231
1232 f = ht_lookup(devitab, i->i_name);
1233 if (f == NULL)
1234 panic("remove_devi(): instance %s disappeared from devitab",
1235 i->i_name);
1236
1237 if (i->i_active == DEVI_BROKEN) {
1238 cfgerror("not removing broken instance `%s'", i->i_name);
1239 return;
1240 }
1241
1242 /*
1243 * We have the device instance, i.
1244 * We have to:
1245 * - delete the alias
1246 *
1247 * If the devi was an alias of an already listed devi, all is
1248 * good we don't have to do more.
1249 * If it was the first alias, we have to replace i's entry in
1250 * d's list by its first alias.
1251 * If it was the only entry, we must remove i's entry from d's
1252 * list.
1253 */
1254 if (i != f) {
1255 for (j = f; j->i_alias != i; j = j->i_alias);
1256 j->i_alias = i->i_alias;
1257 } else {
1258 if (i->i_alias == NULL) {
1259 /* No alias, must unlink the entry from devitab */
1260 ht_remove(devitab, i->i_name);
1261 j = i->i_bsame;
1262 } else {
1263 /* Or have the first alias replace i in d's list */
1264 i->i_alias->i_bsame = i->i_bsame;
1265 j = i->i_alias;
1266 if (i == f)
1267 ht_replace(devitab, i->i_name, i->i_alias);
1268 }
1269
1270 /*
1271 * - remove/replace the instance from the devbase's list
1272 *
1273 * A double-linked list would make this much easier. Oh, well,
1274 * what is done is done.
1275 */
1276 for (ppi = &d->d_ihead;
1277 *ppi != NULL && *ppi != i && (*ppi)->i_bsame != i;
1278 ppi = &(*ppi)->i_bsame);
1279 if (*ppi == NULL)
1280 panic("deldev: dev (%s) doesn't list the devi"
1281 " (%s at %s)", d->d_name, i->i_name, i->i_at);
1282 f = *ppi;
1283 if (f == i)
1284 /* That implies d->d_ihead == i */
1285 *ppi = j;
1286 else
1287 (*ppi)->i_bsame = j;
1288 if (d->d_ipp == &i->i_bsame) {
1289 if (i->i_alias == NULL) {
1290 if (f == i)
1291 d->d_ipp = &d->d_ihead;
1292 else
1293 d->d_ipp = &f->i_bsame;
1294 } else
1295 d->d_ipp = &i->i_alias->i_bsame;
1296 }
1297 }
1298 /*
1299 * - delete the attachment instance
1300 */
1301 iba = i->i_atdeva;
1302 for (ppi = &iba->d_ihead;
1303 *ppi != NULL && *ppi != i && (*ppi)->i_asame != i;
1304 ppi = &(*ppi)->i_asame);
1305 if (*ppi == NULL)
1306 panic("deldev: deva (%s) doesn't list the devi (%s)",
1307 iba->d_name, i->i_name);
1308 f = *ppi;
1309 if (f == i)
1310 /* That implies iba->d_ihead == i */
1311 *ppi = i->i_asame;
1312 else
1313 (*ppi)->i_asame = i->i_asame;
1314 if (iba->d_ipp == &i->i_asame) {
1315 if (f == i)
1316 iba->d_ipp = &iba->d_ihead;
1317 else
1318 iba->d_ipp = &f->i_asame;
1319 }
1320 /*
1321 * - delete the pspec
1322 */
1323 if (i->i_pspec) {
1324 struct pspec *p = i->i_pspec;
1325 struct nvlist *nv, *onv;
1326
1327 /* Double-linked nvlist anyone? */
1328 for (nv = p->p_devs; nv->nv_next != NULL; nv = nv->nv_next) {
1329 if (nv->nv_next && nv->nv_next->nv_ptr == i) {
1330 onv = nv->nv_next;
1331 nv->nv_next = onv->nv_next;
1332 nvfree(onv);
1333 break;
1334 }
1335 if (nv->nv_ptr == i) {
1336 /* nv is p->p_devs in that case */
1337 p->p_devs = nv->nv_next;
1338 nvfree(nv);
1339 break;
1340 }
1341 }
1342 if (p->p_devs == NULL)
1343 TAILQ_REMOVE(&allpspecs, p, p_list);
1344 }
1345 /*
1346 * - delete the alldevi entry
1347 */
1348 TAILQ_REMOVE(&alldevi, i, i_next);
1349 ndevi--;
1350 /*
1351 * Put it in deaddevitab
1352 *
1353 * Each time a devi is removed, devilevel is increased so that later on
1354 * it is possible to tell if an instance was added before or after the
1355 * removal of its parent.
1356 *
1357 * For active instances, i_level contains the number of devi removed so
1358 * far, and for dead devis, it contains its index.
1359 */
1360 i->i_level = devilevel++;
1361 i->i_alias = NULL;
1362 f = ht_lookup(deaddevitab, i->i_name);
1363 if (f == NULL) {
1364 if (ht_insert(deaddevitab, i->i_name, i))
1365 panic("remove_devi(%s) - can't add to deaddevitab",
1366 i->i_name);
1367 } else {
1368 for (j = f; j->i_alias != NULL; j = j->i_alias);
1369 j->i_alias = i;
1370 }
1371 /*
1372 * - reconstruct d->d_umax
1373 */
1374 d->d_umax = 0;
1375 for (i = d->d_ihead; i != NULL; i = i->i_bsame)
1376 if (i->i_unit >= d->d_umax)
1377 d->d_umax = i->i_unit + 1;
1378 }
1379
1380 void
1381 deldeva(const char *at)
1382 {
1383 int unit;
1384 const char *cp;
1385 struct devbase *d, *ad;
1386 struct devi *i, *j;
1387 struct attr *a;
1388 struct pspec *p;
1389 struct nvlist *nv, *stack = NULL;
1390
1391 if (at == NULL) {
1392 TAILQ_FOREACH(i, &alldevi, i_next)
1393 if (i->i_at == NULL)
1394 stack = newnv(NULL, NULL, i, 0, stack);
1395 } else {
1396 int l;
1397
1398 l = strlen(at) - 1;
1399 if (at[l] == '?' || isdigit((unsigned char)at[l])) {
1400 char base[NAMESIZE];
1401
1402 if (split(at, l+1, base, sizeof base, &unit)) {
1403 cfgerror("invalid attachment name `%s'", at);
1404 return;
1405 }
1406 cp = intern(base);
1407 } else {
1408 cp = intern(at);
1409 unit = STAR;
1410 }
1411
1412 ad = ht_lookup(devbasetab, cp);
1413 a = ht_lookup(attrtab, cp);
1414 if (a == NULL) {
1415 cfgerror("unknown attachment attribute or device `%s'",
1416 cp);
1417 return;
1418 }
1419 if (!a->a_iattr) {
1420 cfgerror("plain attribute `%s' cannot have children",
1421 a->a_name);
1422 return;
1423 }
1424
1425 /*
1426 * remove_devi() makes changes to the devbase's list and the
1427 * alias list, * so the actual deletion of the instances must
1428 * be delayed.
1429 */
1430 for (nv = a->a_devs; nv != NULL; nv = nv->nv_next) {
1431 d = nv->nv_ptr;
1432 for (i = d->d_ihead; i != NULL; i = i->i_bsame)
1433 for (j = i; j != NULL; j = j->i_alias) {
1434 /* Ignore devices at root */
1435 if (j->i_at == NULL)
1436 continue;
1437 p = j->i_pspec;
1438 /*
1439 * There are three cases:
1440 *
1441 * 1. unit is not STAR. Consider 'at'
1442 * to be explicit, even if it
1443 * references an interface
1444 * attribute.
1445 *
1446 * 2. unit is STAR and 'at' references
1447 * a real device. Look for pspec
1448 * that have a matching p_atdev
1449 * field.
1450 *
1451 * 3. unit is STAR and 'at' references
1452 * an interface attribute. Look
1453 * for pspec that have a matching
1454 * p_iattr field.
1455 */
1456 if ((unit != STAR && /* Case */
1457 !strcmp(j->i_at, at)) || /* 1 */
1458 (unit == STAR &&
1459 ((ad != NULL && /* Case */
1460 p->p_atdev == ad) || /* 2 */
1461 (ad == NULL && /* Case */
1462 p->p_iattr == a)))) /* 3 */
1463 stack = newnv(NULL, NULL, j, 0,
1464 stack);
1465 }
1466 }
1467 }
1468
1469 for (nv = stack; nv != NULL; nv = nv->nv_next)
1470 remove_devi(nv->nv_ptr);
1471 nvfreel(stack);
1472 }
1473
1474 void
1475 deldev(const char *name)
1476 {
1477 int l;
1478 struct devi *firsti, *i;
1479 struct nvlist *nv, *stack = NULL;
1480
1481 l = strlen(name) - 1;
1482 if (name[l] == '*' || isdigit((unsigned char)name[l])) {
1483 /* `no mydev0' or `no mydev*' */
1484 firsti = ht_lookup(devitab, name);
1485 if (firsti == NULL) {
1486 cfgerror("unknown instance %s", name);
1487 return;
1488 }
1489 for (i = firsti; i != NULL; i = i->i_alias)
1490 stack = newnv(NULL, NULL, i, 0, stack);
1491 } else {
1492 struct devbase *d = ht_lookup(devbasetab, name);
1493
1494 if (d == NULL) {
1495 cfgerror("unknown device %s", name);
1496 return;
1497 }
1498 if (d->d_ispseudo) {
1499 cfgerror("%s is a pseudo-device; "
1500 "use \"no pseudo-device %s\" instead", name,
1501 name);
1502 return;
1503 }
1504
1505 for (firsti = d->d_ihead; firsti != NULL;
1506 firsti = firsti->i_bsame)
1507 for (i = firsti; i != NULL; i = i->i_alias)
1508 stack = newnv(NULL, NULL, i, 0, stack);
1509 }
1510
1511 for (nv = stack; nv != NULL; nv = nv->nv_next)
1512 remove_devi(nv->nv_ptr);
1513 nvfreel(stack);
1514 }
1515
1516 /*
1517 * Insert given device "name" into devroottab. In case "name"
1518 * designates a pure interface attribute, create a fake device
1519 * instance for the attribute and insert that into the roottab
1520 * (this scheme avoids mucking around with the orphanage analysis).
1521 */
1522 void
1523 addpseudoroot(const char *name)
1524 {
1525 char buf[NAMESIZE];
1526 int unit;
1527 struct attr *attr;
1528 struct devi *i;
1529 struct deva *iba;
1530 struct devbase *ib;
1531
1532 if (split(name, strlen(name), buf, sizeof(buf), &unit)) {
1533 cfgerror("invalid pseudo-root name `%s'", name);
1534 return;
1535 }
1536
1537 /*
1538 * Prefer device because devices with locators define an
1539 * implicit interface attribute. However, if a device is
1540 * not available, try to attach to the interface attribute.
1541 * This makes sure adddev() doesn't get confused when we
1542 * are really attaching to a device (alternatively we maybe
1543 * could specify a non-NULL atlist to defdevattach() below).
1544 */
1545 ib = ht_lookup(devbasetab, intern(buf));
1546 if (ib == NULL) {
1547 struct devbase *fakedev;
1548 char fakename[NAMESIZE];
1549
1550 attr = ht_lookup(attrtab, intern(buf));
1551 if (!(attr && attr->a_iattr)) {
1552 cfgerror("pseudo-root `%s' not available", name);
1553 return;
1554 }
1555
1556 /*
1557 * here we cheat a bit: create a fake devbase with the
1558 * interface attribute and instantiate it. quick, cheap,
1559 * dirty & bad for you, much like the stuff in the fridge.
1560 * and, it works, since the pseudoroot device is not included
1561 * in ioconf, just used by config to make sure we start from
1562 * the right place.
1563 */
1564 snprintf(fakename, sizeof(fakename), "%s_devattrs", buf);
1565 fakedev = getdevbase(intern(fakename));
1566 fakedev->d_isdef = 1;
1567 fakedev->d_ispseudo = 0;
1568 fakedev->d_attrs = attrlist_cons(NULL, attr);
1569 defdevattach(NULL, fakedev, NULL, NULL);
1570
1571 if (unit == STAR)
1572 snprintf(buf, sizeof(buf), "%s*", fakename);
1573 else
1574 snprintf(buf, sizeof(buf), "%s%d", fakename, unit);
1575 name = buf;
1576 }
1577
1578 /* ok, everything should be set up, so instantiate a fake device */
1579 i = getdevi(name);
1580 if (i == NULL)
1581 panic("device `%s' expected to be present", name);
1582 ib = i->i_base;
1583 iba = ib->d_ahead;
1584
1585 i->i_atdeva = iba;
1586 i->i_cfflags = 0;
1587 i->i_locs = fixloc(name, &errattr, NULL);
1588 i->i_pseudoroot = 1;
1589 i->i_active = DEVI_ORPHAN; /* set active by kill_orphans() */
1590
1591 *iba->d_ipp = i;
1592 iba->d_ipp = &i->i_asame;
1593
1594 ht_insert(devroottab, ib->d_name, ib);
1595 }
1596
1597 void
1598 addpseudo(const char *name, int number)
1599 {
1600 struct devbase *d;
1601 struct devi *i;
1602
1603 d = ht_lookup(devbasetab, name);
1604 if (d == NULL) {
1605 cfgerror("undefined pseudo-device %s", name);
1606 return;
1607 }
1608 if (!d->d_ispseudo) {
1609 cfgerror("%s is a real device, not a pseudo-device", name);
1610 return;
1611 }
1612 if (ht_lookup(devitab, name) != NULL) {
1613 cfgerror("`%s' already defined", name);
1614 return;
1615 }
1616 i = newdevi(name, number - 1, d); /* foo 16 => "foo0..foo15" */
1617 if (ht_insert(devitab, name, i))
1618 panic("addpseudo(%s)", name);
1619 /* Useful to retrieve the instance from the devbase */
1620 d->d_ihead = i;
1621 i->i_active = DEVI_ACTIVE;
1622 TAILQ_INSERT_TAIL(&allpseudo, i, i_next);
1623 }
1624
1625 void
1626 delpseudo(const char *name)
1627 {
1628 struct devbase *d;
1629 struct devi *i;
1630
1631 d = ht_lookup(devbasetab, name);
1632 if (d == NULL) {
1633 cfgerror("undefined pseudo-device %s", name);
1634 return;
1635 }
1636 if (!d->d_ispseudo) {
1637 cfgerror("%s is a real device, not a pseudo-device", name);
1638 return;
1639 }
1640 if ((i = ht_lookup(devitab, name)) == NULL) {
1641 cfgerror("`%s' not defined", name);
1642 return;
1643 }
1644 d->d_umax = 0; /* clear neads-count entries */
1645 d->d_ihead = NULL; /* make sure it won't be considered active */
1646 TAILQ_REMOVE(&allpseudo, i, i_next);
1647 if (ht_remove(devitab, name))
1648 panic("delpseudo(%s) - can't remove from devitab", name);
1649 if (ht_insert(deaddevitab, name, i))
1650 panic("delpseudo(%s) - can't add to deaddevitab", name);
1651 }
1652
1653 void
1654 adddevm(const char *name, devmajor_t cmajor, devmajor_t bmajor,
1655 struct condexpr *cond, struct nvlist *nv_nodes)
1656 {
1657 struct devm *dm;
1658
1659 if (cmajor != NODEVMAJOR && (cmajor < 0 || cmajor >= 4096)) {
1660 cfgerror("character major %d is invalid", cmajor);
1661 condexpr_destroy(cond);
1662 nvfreel(nv_nodes);
1663 return;
1664 }
1665
1666 if (bmajor != NODEVMAJOR && (bmajor < 0 || bmajor >= 4096)) {
1667 cfgerror("block major %d is invalid", bmajor);
1668 condexpr_destroy(cond);
1669 nvfreel(nv_nodes);
1670 return;
1671 }
1672 if (cmajor == NODEVMAJOR && bmajor == NODEVMAJOR) {
1673 cfgerror("both character/block majors are not specified");
1674 condexpr_destroy(cond);
1675 nvfreel(nv_nodes);
1676 return;
1677 }
1678
1679 dm = ecalloc(1, sizeof(*dm));
1680 dm->dm_srcfile = yyfile;
1681 dm->dm_srcline = currentline();
1682 dm->dm_name = name;
1683 dm->dm_cmajor = cmajor;
1684 dm->dm_bmajor = bmajor;
1685 dm->dm_opts = cond;
1686 dm->dm_devnodes = nv_nodes;
1687
1688 TAILQ_INSERT_TAIL(&alldevms, dm, dm_next);
1689
1690 maxcdevm = MAX(maxcdevm, dm->dm_cmajor);
1691 maxbdevm = MAX(maxbdevm, dm->dm_bmajor);
1692 }
1693
1694 int
1695 fixdevis(void)
1696 {
1697 struct devi *i;
1698 int error = 0;
1699
1700 TAILQ_FOREACH(i, &alldevi, i_next)
1701 if (i->i_active == DEVI_ACTIVE)
1702 selectbase(i->i_base, i->i_atdeva);
1703 else if (i->i_active == DEVI_ORPHAN) {
1704 /*
1705 * At this point, we can't have instances for which
1706 * i_at or i_pspec are NULL.
1707 */
1708 ++error;
1709 cfgxerror(i->i_srcfile, i->i_lineno,
1710 "`%s at %s' is orphaned (%s `%s' found)",
1711 i->i_name, i->i_at, i->i_pspec->p_atunit == WILD ?
1712 "nothing matching" : "no", i->i_at);
1713 } else if (vflag && i->i_active == DEVI_IGNORED)
1714 cfgxwarn(i->i_srcfile, i->i_lineno, "ignoring "
1715 "explicitly orphaned instance `%s at %s'",
1716 i->i_name, i->i_at);
1717
1718 if (error)
1719 return error;
1720
1721 TAILQ_FOREACH(i, &allpseudo, i_next)
1722 if (i->i_active == DEVI_ACTIVE)
1723 selectbase(i->i_base, NULL);
1724 return 0;
1725 }
1726
1727 /*
1728 * Look up a parent spec, creating a new one if it does not exist.
1729 */
1730 static struct pspec *
1731 getpspec(struct attr *attr, struct devbase *ab, int atunit)
1732 {
1733 struct pspec *p;
1734
1735 TAILQ_FOREACH(p, &allpspecs, p_list) {
1736 if (p->p_iattr == attr &&
1737 p->p_atdev == ab &&
1738 p->p_atunit == atunit)
1739 return (p);
1740 }
1741
1742 p = ecalloc(1, sizeof(*p));
1743
1744 p->p_iattr = attr;
1745 p->p_atdev = ab;
1746 p->p_atunit = atunit;
1747 p->p_inst = npspecs++;
1748 p->p_active = 0;
1749
1750 TAILQ_INSERT_TAIL(&allpspecs, p, p_list);
1751
1752 return (p);
1753 }
1754
1755 /*
1756 * Define a new instance of a specific device.
1757 */
1758 static struct devi *
1759 getdevi(const char *name)
1760 {
1761 struct devi *i, *firsti;
1762 struct devbase *d;
1763 int unit;
1764 char base[NAMESIZE];
1765
1766 if (split(name, strlen(name), base, sizeof base, &unit)) {
1767 cfgerror("invalid device name `%s'", name);
1768 return (NULL);
1769 }
1770 d = ht_lookup(devbasetab, intern(base));
1771 if (d == NULL) {
1772 cfgerror("%s: unknown device `%s'", name, base);
1773 return (NULL);
1774 }
1775 if (d->d_ispseudo) {
1776 cfgerror("%s: %s is a pseudo-device", name, base);
1777 return (NULL);
1778 }
1779 firsti = ht_lookup(devitab, name);
1780 i = newdevi(name, unit, d);
1781 if (firsti == NULL) {
1782 if (ht_insert(devitab, name, i))
1783 panic("getdevi(%s)", name);
1784 *d->d_ipp = i;
1785 d->d_ipp = &i->i_bsame;
1786 } else {
1787 while (firsti->i_alias)
1788 firsti = firsti->i_alias;
1789 firsti->i_alias = i;
1790 }
1791 TAILQ_INSERT_TAIL(&alldevi, i, i_next);
1792 ndevi++;
1793 return (i);
1794 }
1795
1796 static const char *
1797 concat(const char *name, int c)
1798 {
1799 size_t len;
1800 char buf[NAMESIZE];
1801
1802 len = strlen(name);
1803 if (len + 2 > sizeof(buf)) {
1804 cfgerror("device name `%s%c' too long", name, c);
1805 len = sizeof(buf) - 2;
1806 }
1807 memmove(buf, name, len);
1808 buf[len] = c;
1809 buf[len + 1] = 0;
1810 return (intern(buf));
1811 }
1812
1813 const char *
1814 starref(const char *name)
1815 {
1816
1817 return (concat(name, '*'));
1818 }
1819
1820 const char *
1821 wildref(const char *name)
1822 {
1823
1824 return (concat(name, '?'));
1825 }
1826
1827 /*
1828 * Split a name like "foo0" into base name (foo) and unit number (0).
1829 * Return 0 on success. To make this useful for names like "foo0a",
1830 * the length of the "foo0" part is one of the arguments.
1831 */
1832 static int
1833 split(const char *name, size_t nlen, char *base, size_t bsize, int *aunit)
1834 {
1835 const char *cp;
1836 int c;
1837 size_t l;
1838
1839 l = nlen;
1840 if (l < 2 || l >= bsize || isdigit((unsigned char)*name))
1841 return (1);
1842 c = (u_char)name[--l];
1843 if (!isdigit(c)) {
1844 if (c == '*')
1845 *aunit = STAR;
1846 else if (c == '?')
1847 *aunit = WILD;
1848 else
1849 return (1);
1850 } else {
1851 cp = &name[l];
1852 while (isdigit((unsigned char)cp[-1]))
1853 l--, cp--;
1854 *aunit = atoi(cp);
1855 }
1856 memmove(base, name, l);
1857 base[l] = 0;
1858 return (0);
1859 }
1860
1861 void
1862 selectattr(struct attr *a)
1863 {
1864
1865 (void)ht_insert(selecttab, a->a_name, __UNCONST(a->a_name));
1866 CFGDBG(3, "attr selected `%s'", a->a_name);
1867 }
1868
1869 /*
1870 * We have an instance of the base foo, so select it and all its
1871 * attributes for "optional foo".
1872 */
1873 static void
1874 selectbase(struct devbase *d, struct deva *da)
1875 {
1876 struct attr *a;
1877 struct attrlist *al;
1878
1879 (void)ht_insert(selecttab, d->d_name, __UNCONST(d->d_name));
1880 CFGDBG(3, "devbase selected `%s'", d->d_name);
1881 for (al = d->d_attrs; al != NULL; al = al->al_next) {
1882 a = al->al_this;
1883 expandattr(a, selectattr);
1884 }
1885 if (da != NULL) {
1886 (void)ht_insert(selecttab, da->d_name, __UNCONST(da->d_name));
1887 CFGDBG(3, "devattr selected `%s'", da->d_name);
1888 for (al = da->d_attrs; al != NULL; al = al->al_next) {
1889 a = al->al_this;
1890 expandattr(a, selectattr);
1891 }
1892 }
1893 }
1894
1895 /*
1896 * Is the given pointer on the given list of pointers?
1897 */
1898 int
1899 onlist(struct nvlist *nv, void *ptr)
1900 {
1901 for (; nv != NULL; nv = nv->nv_next)
1902 if (nv->nv_ptr == ptr)
1903 return (1);
1904 return (0);
1905 }
1906
1907 static char *
1908 extend(char *p, const char *name)
1909 {
1910 size_t l;
1911
1912 l = strlen(name);
1913 memmove(p, name, l);
1914 p += l;
1915 *p++ = ',';
1916 *p++ = ' ';
1917 return (p);
1918 }
1919
1920 /*
1921 * Check that we got all required locators, and default any that are
1922 * given as "?" and have defaults. Return 0 on success.
1923 */
1924 static const char **
1925 fixloc(const char *name, struct attr *attr, struct loclist *got)
1926 {
1927 struct loclist *m, *n;
1928 int ord;
1929 const char **lp;
1930 int nmissing, nextra, nnodefault;
1931 char *mp, *ep, *ndp;
1932 char missing[1000], extra[1000], nodefault[1000];
1933 static const char *nullvec[1];
1934
1935 /*
1936 * Look for all required locators, and number the given ones
1937 * according to the required order. While we are numbering,
1938 * set default values for defaulted locators.
1939 */
1940 if (attr->a_loclen == 0) /* e.g., "at root" */
1941 lp = nullvec;
1942 else
1943 lp = emalloc((attr->a_loclen + 1) * sizeof(const char *));
1944 for (n = got; n != NULL; n = n->ll_next)
1945 n->ll_num = -1;
1946 nmissing = 0;
1947 mp = missing;
1948 /* yes, this is O(mn), but m and n should be small */
1949 for (ord = 0, m = attr->a_locs; m != NULL; m = m->ll_next, ord++) {
1950 for (n = got; n != NULL; n = n->ll_next) {
1951 if (n->ll_name == m->ll_name) {
1952 n->ll_num = ord;
1953 break;
1954 }
1955 }
1956 if (n == NULL && m->ll_num == 0) {
1957 nmissing++;
1958 mp = extend(mp, m->ll_name);
1959 }
1960 lp[ord] = m->ll_string;
1961 }
1962 if (ord != attr->a_loclen)
1963 panic("fixloc");
1964 lp[ord] = NULL;
1965 nextra = 0;
1966 ep = extra;
1967 nnodefault = 0;
1968 ndp = nodefault;
1969 for (n = got; n != NULL; n = n->ll_next) {
1970 if (n->ll_num >= 0) {
1971 if (n->ll_string != NULL)
1972 lp[n->ll_num] = n->ll_string;
1973 else if (lp[n->ll_num] == NULL) {
1974 nnodefault++;
1975 ndp = extend(ndp, n->ll_name);
1976 }
1977 } else {
1978 nextra++;
1979 ep = extend(ep, n->ll_name);
1980 }
1981 }
1982 if (nextra) {
1983 ep[-2] = 0; /* kill ", " */
1984 cfgerror("%s: extraneous locator%s: %s",
1985 name, nextra > 1 ? "s" : "", extra);
1986 }
1987 if (nmissing) {
1988 mp[-2] = 0;
1989 cfgerror("%s: must specify %s", name, missing);
1990 }
1991 if (nnodefault) {
1992 ndp[-2] = 0;
1993 cfgerror("%s: cannot wildcard %s", name, nodefault);
1994 }
1995 if (nmissing || nnodefault) {
1996 free(lp);
1997 lp = NULL;
1998 }
1999 return (lp);
2000 }
2001
2002 void
2003 setversion(int newver)
2004 {
2005 if (newver > CONFIG_VERSION)
2006 cfgerror("your sources require a newer version of config(1) "
2007 "-- please rebuild it.");
2008 else if (newver < CONFIG_MINVERSION)
2009 cfgerror("your sources are out of date -- please update.");
2010 else
2011 version = newver;
2012 }
2013