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