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