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